blob: 4a9e1f36d8ffe4f044c9d476904c99b4ed6f41c3 [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{
Mika Kuoppalab033bb62016-06-07 17:19:04 +030061 /* See Bspec note for PSR2_CTL bit 31, Wa#828:skl,bxt,kbl */
62 I915_WRITE(CHICKEN_PAR1_1,
63 I915_READ(CHICKEN_PAR1_1) | SKL_EDP_PSR_FIX_RDWRAP);
64
65 I915_WRITE(GEN8_CONFIG0,
66 I915_READ(GEN8_CONFIG0) | GEN9_DEFAULT_FIXES);
Mika Kuoppala590e8ff2016-06-07 17:19:13 +030067
68 /* WaEnableChickenDCPR:skl,bxt,kbl */
69 I915_WRITE(GEN8_CHICKEN_DCPR_1,
70 I915_READ(GEN8_CHICKEN_DCPR_1) | MASK_WAKEMEM);
Mika Kuoppala0f78dee2016-06-07 17:19:16 +030071
72 /* WaFbcTurnOffFbcWatermark:skl,bxt,kbl */
Mika Kuoppala303d4ea2016-06-07 17:19:17 +030073 /* WaFbcWakeMemOn:skl,bxt,kbl */
74 I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) |
75 DISP_FBC_WM_DIS |
76 DISP_FBC_MEMORY_WAKE);
Mika Kuoppalad1b4eef2016-06-07 17:19:19 +030077
78 /* WaFbcHighMemBwCorruptionAvoidance:skl,bxt,kbl */
79 I915_WRITE(ILK_DPFC_CHICKEN, I915_READ(ILK_DPFC_CHICKEN) |
80 ILK_DPFC_DISABLE_DUMMY0);
Mika Kuoppalab033bb62016-06-07 17:19:04 +030081}
82
Ville Syrjälä46f16e62016-10-31 22:37:22 +020083static void bxt_init_clock_gating(struct drm_i915_private *dev_priv)
Imre Deaka82abe42015-03-27 14:00:04 +020084{
Ville Syrjälä46f16e62016-10-31 22:37:22 +020085 gen9_init_clock_gating(dev_priv);
Daniel Vetterdc00b6a2016-05-19 09:14:20 +020086
Nick Hoatha7546152015-06-29 14:07:32 +010087 /* WaDisableSDEUnitClockGating:bxt */
88 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
89 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
90
Imre Deak32608ca2015-03-11 11:10:27 +020091 /*
92 * FIXME:
Ben Widawsky868434c2015-03-11 10:49:32 +020093 * GEN8_HDCUNIT_CLOCK_GATE_DISABLE_HDCREQ applies on 3x6 GT SKUs only.
Imre Deak32608ca2015-03-11 11:10:27 +020094 */
Imre Deak32608ca2015-03-11 11:10:27 +020095 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
Ben Widawsky868434c2015-03-11 10:49:32 +020096 GEN8_HDCUNIT_CLOCK_GATE_DISABLE_HDCREQ);
Imre Deakd965e7ac2015-12-01 10:23:52 +020097
98 /*
99 * Wa: Backlight PWM may stop in the asserted state, causing backlight
100 * to stay fully on.
101 */
102 if (IS_BXT_REVID(dev_priv, BXT_REVID_B0, REVID_FOREVER))
103 I915_WRITE(GEN9_CLKGATE_DIS_0, I915_READ(GEN9_CLKGATE_DIS_0) |
104 PWM1_GATING_DIS | PWM2_GATING_DIS);
Imre Deaka82abe42015-03-27 14:00:04 +0200105}
106
Ville Syrjälä148ac1f2016-10-31 22:37:16 +0200107static void i915_pineview_get_mem_freq(struct drm_i915_private *dev_priv)
Daniel Vetterc921aba2012-04-26 23:28:17 +0200108{
Daniel Vetterc921aba2012-04-26 23:28:17 +0200109 u32 tmp;
110
111 tmp = I915_READ(CLKCFG);
112
113 switch (tmp & CLKCFG_FSB_MASK) {
114 case CLKCFG_FSB_533:
115 dev_priv->fsb_freq = 533; /* 133*4 */
116 break;
117 case CLKCFG_FSB_800:
118 dev_priv->fsb_freq = 800; /* 200*4 */
119 break;
120 case CLKCFG_FSB_667:
121 dev_priv->fsb_freq = 667; /* 167*4 */
122 break;
123 case CLKCFG_FSB_400:
124 dev_priv->fsb_freq = 400; /* 100*4 */
125 break;
126 }
127
128 switch (tmp & CLKCFG_MEM_MASK) {
129 case CLKCFG_MEM_533:
130 dev_priv->mem_freq = 533;
131 break;
132 case CLKCFG_MEM_667:
133 dev_priv->mem_freq = 667;
134 break;
135 case CLKCFG_MEM_800:
136 dev_priv->mem_freq = 800;
137 break;
138 }
139
140 /* detect pineview DDR3 setting */
141 tmp = I915_READ(CSHRDDR3CTL);
142 dev_priv->is_ddr3 = (tmp & CSHRDDR3CTL_DDR3) ? 1 : 0;
143}
144
Ville Syrjälä148ac1f2016-10-31 22:37:16 +0200145static void i915_ironlake_get_mem_freq(struct drm_i915_private *dev_priv)
Daniel Vetterc921aba2012-04-26 23:28:17 +0200146{
Daniel Vetterc921aba2012-04-26 23:28:17 +0200147 u16 ddrpll, csipll;
148
149 ddrpll = I915_READ16(DDRMPLL1);
150 csipll = I915_READ16(CSIPLL0);
151
152 switch (ddrpll & 0xff) {
153 case 0xc:
154 dev_priv->mem_freq = 800;
155 break;
156 case 0x10:
157 dev_priv->mem_freq = 1066;
158 break;
159 case 0x14:
160 dev_priv->mem_freq = 1333;
161 break;
162 case 0x18:
163 dev_priv->mem_freq = 1600;
164 break;
165 default:
166 DRM_DEBUG_DRIVER("unknown memory frequency 0x%02x\n",
167 ddrpll & 0xff);
168 dev_priv->mem_freq = 0;
169 break;
170 }
171
Daniel Vetter20e4d402012-08-08 23:35:39 +0200172 dev_priv->ips.r_t = dev_priv->mem_freq;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200173
174 switch (csipll & 0x3ff) {
175 case 0x00c:
176 dev_priv->fsb_freq = 3200;
177 break;
178 case 0x00e:
179 dev_priv->fsb_freq = 3733;
180 break;
181 case 0x010:
182 dev_priv->fsb_freq = 4266;
183 break;
184 case 0x012:
185 dev_priv->fsb_freq = 4800;
186 break;
187 case 0x014:
188 dev_priv->fsb_freq = 5333;
189 break;
190 case 0x016:
191 dev_priv->fsb_freq = 5866;
192 break;
193 case 0x018:
194 dev_priv->fsb_freq = 6400;
195 break;
196 default:
197 DRM_DEBUG_DRIVER("unknown fsb frequency 0x%04x\n",
198 csipll & 0x3ff);
199 dev_priv->fsb_freq = 0;
200 break;
201 }
202
203 if (dev_priv->fsb_freq == 3200) {
Daniel Vetter20e4d402012-08-08 23:35:39 +0200204 dev_priv->ips.c_m = 0;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200205 } else if (dev_priv->fsb_freq > 3200 && dev_priv->fsb_freq <= 4800) {
Daniel Vetter20e4d402012-08-08 23:35:39 +0200206 dev_priv->ips.c_m = 1;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200207 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +0200208 dev_priv->ips.c_m = 2;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200209 }
210}
211
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300212static const struct cxsr_latency cxsr_latency_table[] = {
213 {1, 0, 800, 400, 3382, 33382, 3983, 33983}, /* DDR2-400 SC */
214 {1, 0, 800, 667, 3354, 33354, 3807, 33807}, /* DDR2-667 SC */
215 {1, 0, 800, 800, 3347, 33347, 3763, 33763}, /* DDR2-800 SC */
216 {1, 1, 800, 667, 6420, 36420, 6873, 36873}, /* DDR3-667 SC */
217 {1, 1, 800, 800, 5902, 35902, 6318, 36318}, /* DDR3-800 SC */
218
219 {1, 0, 667, 400, 3400, 33400, 4021, 34021}, /* DDR2-400 SC */
220 {1, 0, 667, 667, 3372, 33372, 3845, 33845}, /* DDR2-667 SC */
221 {1, 0, 667, 800, 3386, 33386, 3822, 33822}, /* DDR2-800 SC */
222 {1, 1, 667, 667, 6438, 36438, 6911, 36911}, /* DDR3-667 SC */
223 {1, 1, 667, 800, 5941, 35941, 6377, 36377}, /* DDR3-800 SC */
224
225 {1, 0, 400, 400, 3472, 33472, 4173, 34173}, /* DDR2-400 SC */
226 {1, 0, 400, 667, 3443, 33443, 3996, 33996}, /* DDR2-667 SC */
227 {1, 0, 400, 800, 3430, 33430, 3946, 33946}, /* DDR2-800 SC */
228 {1, 1, 400, 667, 6509, 36509, 7062, 37062}, /* DDR3-667 SC */
229 {1, 1, 400, 800, 5985, 35985, 6501, 36501}, /* DDR3-800 SC */
230
231 {0, 0, 800, 400, 3438, 33438, 4065, 34065}, /* DDR2-400 SC */
232 {0, 0, 800, 667, 3410, 33410, 3889, 33889}, /* DDR2-667 SC */
233 {0, 0, 800, 800, 3403, 33403, 3845, 33845}, /* DDR2-800 SC */
234 {0, 1, 800, 667, 6476, 36476, 6955, 36955}, /* DDR3-667 SC */
235 {0, 1, 800, 800, 5958, 35958, 6400, 36400}, /* DDR3-800 SC */
236
237 {0, 0, 667, 400, 3456, 33456, 4103, 34106}, /* DDR2-400 SC */
238 {0, 0, 667, 667, 3428, 33428, 3927, 33927}, /* DDR2-667 SC */
239 {0, 0, 667, 800, 3443, 33443, 3905, 33905}, /* DDR2-800 SC */
240 {0, 1, 667, 667, 6494, 36494, 6993, 36993}, /* DDR3-667 SC */
241 {0, 1, 667, 800, 5998, 35998, 6460, 36460}, /* DDR3-800 SC */
242
243 {0, 0, 400, 400, 3528, 33528, 4255, 34255}, /* DDR2-400 SC */
244 {0, 0, 400, 667, 3500, 33500, 4079, 34079}, /* DDR2-667 SC */
245 {0, 0, 400, 800, 3487, 33487, 4029, 34029}, /* DDR2-800 SC */
246 {0, 1, 400, 667, 6566, 36566, 7145, 37145}, /* DDR3-667 SC */
247 {0, 1, 400, 800, 6042, 36042, 6584, 36584}, /* DDR3-800 SC */
248};
249
Tvrtko Ursulin44a655c2016-10-13 11:09:23 +0100250static const struct cxsr_latency *intel_get_cxsr_latency(bool is_desktop,
251 bool is_ddr3,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300252 int fsb,
253 int mem)
254{
255 const struct cxsr_latency *latency;
256 int i;
257
258 if (fsb == 0 || mem == 0)
259 return NULL;
260
261 for (i = 0; i < ARRAY_SIZE(cxsr_latency_table); i++) {
262 latency = &cxsr_latency_table[i];
263 if (is_desktop == latency->is_desktop &&
264 is_ddr3 == latency->is_ddr3 &&
265 fsb == latency->fsb_freq && mem == latency->mem_freq)
266 return latency;
267 }
268
269 DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
270
271 return NULL;
272}
273
Ville Syrjäläfc1ac8d2015-03-05 21:19:52 +0200274static void chv_set_memory_dvfs(struct drm_i915_private *dev_priv, bool enable)
275{
276 u32 val;
277
278 mutex_lock(&dev_priv->rps.hw_lock);
279
280 val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
281 if (enable)
282 val &= ~FORCE_DDR_HIGH_FREQ;
283 else
284 val |= FORCE_DDR_HIGH_FREQ;
285 val &= ~FORCE_DDR_LOW_FREQ;
286 val |= FORCE_DDR_FREQ_REQ_ACK;
287 vlv_punit_write(dev_priv, PUNIT_REG_DDR_SETUP2, val);
288
289 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2) &
290 FORCE_DDR_FREQ_REQ_ACK) == 0, 3))
291 DRM_ERROR("timed out waiting for Punit DDR DVFS request\n");
292
293 mutex_unlock(&dev_priv->rps.hw_lock);
294}
295
Ville Syrjäläcfb41412015-03-05 21:19:51 +0200296static void chv_set_memory_pm5(struct drm_i915_private *dev_priv, bool enable)
297{
298 u32 val;
299
300 mutex_lock(&dev_priv->rps.hw_lock);
301
302 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
303 if (enable)
304 val |= DSP_MAXFIFO_PM5_ENABLE;
305 else
306 val &= ~DSP_MAXFIFO_PM5_ENABLE;
307 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
308
309 mutex_unlock(&dev_priv->rps.hw_lock);
310}
311
Ville Syrjäläf4998962015-03-10 17:02:21 +0200312#define FW_WM(value, plane) \
313 (((value) << DSPFW_ ## plane ## _SHIFT) & DSPFW_ ## plane ## _MASK)
314
Ville Syrjälä3d90e642016-11-28 19:37:11 +0200315static void _intel_set_memory_cxsr(struct drm_i915_private *dev_priv, bool enable)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300316{
Imre Deak5209b1f2014-07-01 12:36:17 +0300317 u32 val;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300318
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +0100319 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Imre Deak5209b1f2014-07-01 12:36:17 +0300320 I915_WRITE(FW_BLC_SELF_VLV, enable ? FW_CSPWRDWNEN : 0);
Ville Syrjäläa7a6c492015-06-24 22:00:01 +0300321 POSTING_READ(FW_BLC_SELF_VLV);
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +0100322 } else if (IS_G4X(dev_priv) || IS_CRESTLINE(dev_priv)) {
Imre Deak5209b1f2014-07-01 12:36:17 +0300323 I915_WRITE(FW_BLC_SELF, enable ? FW_BLC_SELF_EN : 0);
Ville Syrjäläa7a6c492015-06-24 22:00:01 +0300324 POSTING_READ(FW_BLC_SELF);
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +0200325 } else if (IS_PINEVIEW(dev_priv)) {
Imre Deak5209b1f2014-07-01 12:36:17 +0300326 val = I915_READ(DSPFW3) & ~PINEVIEW_SELF_REFRESH_EN;
327 val |= enable ? PINEVIEW_SELF_REFRESH_EN : 0;
328 I915_WRITE(DSPFW3, val);
Ville Syrjäläa7a6c492015-06-24 22:00:01 +0300329 POSTING_READ(DSPFW3);
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +0100330 } else if (IS_I945G(dev_priv) || IS_I945GM(dev_priv)) {
Imre Deak5209b1f2014-07-01 12:36:17 +0300331 val = enable ? _MASKED_BIT_ENABLE(FW_BLC_SELF_EN) :
332 _MASKED_BIT_DISABLE(FW_BLC_SELF_EN);
333 I915_WRITE(FW_BLC_SELF, val);
Ville Syrjäläa7a6c492015-06-24 22:00:01 +0300334 POSTING_READ(FW_BLC_SELF);
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +0100335 } else if (IS_I915GM(dev_priv)) {
Ville Syrjäläacb91352016-07-29 17:57:02 +0300336 /*
337 * FIXME can't find a bit like this for 915G, and
338 * and yet it does have the related watermark in
339 * FW_BLC_SELF. What's going on?
340 */
Imre Deak5209b1f2014-07-01 12:36:17 +0300341 val = enable ? _MASKED_BIT_ENABLE(INSTPM_SELF_EN) :
342 _MASKED_BIT_DISABLE(INSTPM_SELF_EN);
343 I915_WRITE(INSTPM, val);
Ville Syrjäläa7a6c492015-06-24 22:00:01 +0300344 POSTING_READ(INSTPM);
Imre Deak5209b1f2014-07-01 12:36:17 +0300345 } else {
346 return;
347 }
348
Tvrtko Ursulin08c4d7f2016-11-17 12:30:14 +0000349 DRM_DEBUG_KMS("memory self-refresh is %s\n", enableddisabled(enable));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300350}
351
Ville Syrjälä3d90e642016-11-28 19:37:11 +0200352void intel_set_memory_cxsr(struct drm_i915_private *dev_priv, bool enable)
353{
354 mutex_lock(&dev_priv->wm.wm_mutex);
355 _intel_set_memory_cxsr(dev_priv, enable);
356 dev_priv->wm.vlv.cxsr = enable;
357 mutex_unlock(&dev_priv->wm.wm_mutex);
358}
Ville Syrjäläfc1ac8d2015-03-05 21:19:52 +0200359
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300360/*
361 * Latency for FIFO fetches is dependent on several factors:
362 * - memory configuration (speed, channels)
363 * - chipset
364 * - current MCH state
365 * It can be fairly high in some situations, so here we assume a fairly
366 * pessimal value. It's a tradeoff between extra memory fetches (if we
367 * set this value too high, the FIFO will fetch frequently to stay full)
368 * and power consumption (set it too low to save power and we might see
369 * FIFO underruns and display "flicker").
370 *
371 * A value of 5us seems to be a good balance; safe for very low end
372 * platforms but not overly aggressive on lower latency configs.
373 */
Chris Wilson5aef6002014-09-03 11:56:07 +0100374static const int pessimal_latency_ns = 5000;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300375
Ville Syrjäläb5004722015-03-05 21:19:47 +0200376#define VLV_FIFO_START(dsparb, dsparb2, lo_shift, hi_shift) \
377 ((((dsparb) >> (lo_shift)) & 0xff) | ((((dsparb2) >> (hi_shift)) & 0x1) << 8))
378
Ville Syrjälä49845a22016-11-22 18:02:01 +0200379static int vlv_get_fifo_size(struct intel_plane *plane)
Ville Syrjäläb5004722015-03-05 21:19:47 +0200380{
Ville Syrjälä49845a22016-11-22 18:02:01 +0200381 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
Ville Syrjäläb5004722015-03-05 21:19:47 +0200382 int sprite0_start, sprite1_start, size;
383
Ville Syrjälä49845a22016-11-22 18:02:01 +0200384 if (plane->id == PLANE_CURSOR)
385 return 63;
386
387 switch (plane->pipe) {
Ville Syrjäläb5004722015-03-05 21:19:47 +0200388 uint32_t dsparb, dsparb2, dsparb3;
389 case PIPE_A:
390 dsparb = I915_READ(DSPARB);
391 dsparb2 = I915_READ(DSPARB2);
392 sprite0_start = VLV_FIFO_START(dsparb, dsparb2, 0, 0);
393 sprite1_start = VLV_FIFO_START(dsparb, dsparb2, 8, 4);
394 break;
395 case PIPE_B:
396 dsparb = I915_READ(DSPARB);
397 dsparb2 = I915_READ(DSPARB2);
398 sprite0_start = VLV_FIFO_START(dsparb, dsparb2, 16, 8);
399 sprite1_start = VLV_FIFO_START(dsparb, dsparb2, 24, 12);
400 break;
401 case PIPE_C:
402 dsparb2 = I915_READ(DSPARB2);
403 dsparb3 = I915_READ(DSPARB3);
404 sprite0_start = VLV_FIFO_START(dsparb3, dsparb2, 0, 16);
405 sprite1_start = VLV_FIFO_START(dsparb3, dsparb2, 8, 20);
406 break;
407 default:
408 return 0;
409 }
410
Ville Syrjälä49845a22016-11-22 18:02:01 +0200411 switch (plane->id) {
412 case PLANE_PRIMARY:
Ville Syrjäläb5004722015-03-05 21:19:47 +0200413 size = sprite0_start;
414 break;
Ville Syrjälä49845a22016-11-22 18:02:01 +0200415 case PLANE_SPRITE0:
Ville Syrjäläb5004722015-03-05 21:19:47 +0200416 size = sprite1_start - sprite0_start;
417 break;
Ville Syrjälä49845a22016-11-22 18:02:01 +0200418 case PLANE_SPRITE1:
Ville Syrjäläb5004722015-03-05 21:19:47 +0200419 size = 512 - 1 - sprite1_start;
420 break;
421 default:
422 return 0;
423 }
424
Ville Syrjälä49845a22016-11-22 18:02:01 +0200425 DRM_DEBUG_KMS("%s FIFO size: %d\n", plane->base.name, size);
Ville Syrjäläb5004722015-03-05 21:19:47 +0200426
427 return size;
428}
429
Ville Syrjäläef0f5e92016-10-31 22:37:17 +0200430static int i9xx_get_fifo_size(struct drm_i915_private *dev_priv, int plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300431{
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300432 uint32_t dsparb = I915_READ(DSPARB);
433 int size;
434
435 size = dsparb & 0x7f;
436 if (plane)
437 size = ((dsparb >> DSPARB_CSTART_SHIFT) & 0x7f) - size;
438
439 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
440 plane ? "B" : "A", size);
441
442 return size;
443}
444
Ville Syrjäläef0f5e92016-10-31 22:37:17 +0200445static int i830_get_fifo_size(struct drm_i915_private *dev_priv, int plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300446{
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300447 uint32_t dsparb = I915_READ(DSPARB);
448 int size;
449
450 size = dsparb & 0x1ff;
451 if (plane)
452 size = ((dsparb >> DSPARB_BEND_SHIFT) & 0x1ff) - size;
453 size >>= 1; /* Convert to cachelines */
454
455 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
456 plane ? "B" : "A", size);
457
458 return size;
459}
460
Ville Syrjäläef0f5e92016-10-31 22:37:17 +0200461static int i845_get_fifo_size(struct drm_i915_private *dev_priv, int plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300462{
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300463 uint32_t dsparb = I915_READ(DSPARB);
464 int size;
465
466 size = dsparb & 0x7f;
467 size >>= 2; /* Convert to cachelines */
468
469 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
470 plane ? "B" : "A",
471 size);
472
473 return size;
474}
475
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300476/* Pineview has different values for various configs */
477static const struct intel_watermark_params pineview_display_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300478 .fifo_size = PINEVIEW_DISPLAY_FIFO,
479 .max_wm = PINEVIEW_MAX_WM,
480 .default_wm = PINEVIEW_DFT_WM,
481 .guard_size = PINEVIEW_GUARD_WM,
482 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300483};
484static const struct intel_watermark_params pineview_display_hplloff_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300485 .fifo_size = PINEVIEW_DISPLAY_FIFO,
486 .max_wm = PINEVIEW_MAX_WM,
487 .default_wm = PINEVIEW_DFT_HPLLOFF_WM,
488 .guard_size = PINEVIEW_GUARD_WM,
489 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300490};
491static const struct intel_watermark_params pineview_cursor_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300492 .fifo_size = PINEVIEW_CURSOR_FIFO,
493 .max_wm = PINEVIEW_CURSOR_MAX_WM,
494 .default_wm = PINEVIEW_CURSOR_DFT_WM,
495 .guard_size = PINEVIEW_CURSOR_GUARD_WM,
496 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300497};
498static const struct intel_watermark_params pineview_cursor_hplloff_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300499 .fifo_size = PINEVIEW_CURSOR_FIFO,
500 .max_wm = PINEVIEW_CURSOR_MAX_WM,
501 .default_wm = PINEVIEW_CURSOR_DFT_WM,
502 .guard_size = PINEVIEW_CURSOR_GUARD_WM,
503 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300504};
505static const struct intel_watermark_params g4x_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300506 .fifo_size = G4X_FIFO_SIZE,
507 .max_wm = G4X_MAX_WM,
508 .default_wm = G4X_MAX_WM,
509 .guard_size = 2,
510 .cacheline_size = G4X_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300511};
512static const struct intel_watermark_params g4x_cursor_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300513 .fifo_size = I965_CURSOR_FIFO,
514 .max_wm = I965_CURSOR_MAX_WM,
515 .default_wm = I965_CURSOR_DFT_WM,
516 .guard_size = 2,
517 .cacheline_size = G4X_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300518};
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300519static const struct intel_watermark_params i965_cursor_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300520 .fifo_size = I965_CURSOR_FIFO,
521 .max_wm = I965_CURSOR_MAX_WM,
522 .default_wm = I965_CURSOR_DFT_WM,
523 .guard_size = 2,
524 .cacheline_size = I915_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300525};
526static const struct intel_watermark_params i945_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300527 .fifo_size = I945_FIFO_SIZE,
528 .max_wm = I915_MAX_WM,
529 .default_wm = 1,
530 .guard_size = 2,
531 .cacheline_size = I915_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300532};
533static const struct intel_watermark_params i915_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300534 .fifo_size = I915_FIFO_SIZE,
535 .max_wm = I915_MAX_WM,
536 .default_wm = 1,
537 .guard_size = 2,
538 .cacheline_size = I915_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300539};
Ville Syrjälä9d539102014-08-15 01:21:53 +0300540static const struct intel_watermark_params i830_a_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300541 .fifo_size = I855GM_FIFO_SIZE,
542 .max_wm = I915_MAX_WM,
543 .default_wm = 1,
544 .guard_size = 2,
545 .cacheline_size = I830_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300546};
Ville Syrjälä9d539102014-08-15 01:21:53 +0300547static const struct intel_watermark_params i830_bc_wm_info = {
548 .fifo_size = I855GM_FIFO_SIZE,
549 .max_wm = I915_MAX_WM/2,
550 .default_wm = 1,
551 .guard_size = 2,
552 .cacheline_size = I830_FIFO_LINE_SIZE,
553};
Daniel Vetterfeb56b92013-12-14 20:38:30 -0200554static const struct intel_watermark_params i845_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300555 .fifo_size = I830_FIFO_SIZE,
556 .max_wm = I915_MAX_WM,
557 .default_wm = 1,
558 .guard_size = 2,
559 .cacheline_size = I830_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300560};
561
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300562/**
563 * intel_calculate_wm - calculate watermark level
564 * @clock_in_khz: pixel clock
565 * @wm: chip FIFO params
Ville Syrjäläac484962016-01-20 21:05:26 +0200566 * @cpp: bytes per pixel
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300567 * @latency_ns: memory latency for the platform
568 *
569 * Calculate the watermark level (the level at which the display plane will
570 * start fetching from memory again). Each chip has a different display
571 * FIFO size and allocation, so the caller needs to figure that out and pass
572 * in the correct intel_watermark_params structure.
573 *
574 * As the pixel clock runs, the FIFO will be drained at a rate that depends
575 * on the pixel size. When it reaches the watermark level, it'll start
576 * fetching FIFO line sized based chunks from memory until the FIFO fills
577 * past the watermark point. If the FIFO drains completely, a FIFO underrun
578 * will occur, and a display engine hang could result.
579 */
580static unsigned long intel_calculate_wm(unsigned long clock_in_khz,
581 const struct intel_watermark_params *wm,
Ville Syrjäläac484962016-01-20 21:05:26 +0200582 int fifo_size, int cpp,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300583 unsigned long latency_ns)
584{
585 long entries_required, wm_size;
586
587 /*
588 * Note: we need to make sure we don't overflow for various clock &
589 * latency values.
590 * clocks go from a few thousand to several hundred thousand.
591 * latency is usually a few thousand
592 */
Ville Syrjäläac484962016-01-20 21:05:26 +0200593 entries_required = ((clock_in_khz / 1000) * cpp * latency_ns) /
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300594 1000;
595 entries_required = DIV_ROUND_UP(entries_required, wm->cacheline_size);
596
597 DRM_DEBUG_KMS("FIFO entries required for mode: %ld\n", entries_required);
598
599 wm_size = fifo_size - (entries_required + wm->guard_size);
600
601 DRM_DEBUG_KMS("FIFO watermark level: %ld\n", wm_size);
602
603 /* Don't promote wm_size to unsigned... */
604 if (wm_size > (long)wm->max_wm)
605 wm_size = wm->max_wm;
606 if (wm_size <= 0)
607 wm_size = wm->default_wm;
Ville Syrjäläd6feb192014-09-05 21:54:13 +0300608
609 /*
610 * Bspec seems to indicate that the value shouldn't be lower than
611 * 'burst size + 1'. Certainly 830 is quite unhappy with low values.
612 * Lets go for 8 which is the burst size since certain platforms
613 * already use a hardcoded 8 (which is what the spec says should be
614 * done).
615 */
616 if (wm_size <= 8)
617 wm_size = 8;
618
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300619 return wm_size;
620}
621
Ville Syrjäläffc7a762016-10-31 22:37:21 +0200622static struct intel_crtc *single_enabled_crtc(struct drm_i915_private *dev_priv)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300623{
Ville Syrjäläefc26112016-10-31 22:37:04 +0200624 struct intel_crtc *crtc, *enabled = NULL;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300625
Ville Syrjäläffc7a762016-10-31 22:37:21 +0200626 for_each_intel_crtc(&dev_priv->drm, crtc) {
Ville Syrjäläefc26112016-10-31 22:37:04 +0200627 if (intel_crtc_active(crtc)) {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300628 if (enabled)
629 return NULL;
630 enabled = crtc;
631 }
632 }
633
634 return enabled;
635}
636
Ville Syrjälä432081b2016-10-31 22:37:03 +0200637static void pineview_update_wm(struct intel_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300638{
Ville Syrjäläffc7a762016-10-31 22:37:21 +0200639 struct drm_i915_private *dev_priv = to_i915(unused_crtc->base.dev);
Ville Syrjäläefc26112016-10-31 22:37:04 +0200640 struct intel_crtc *crtc;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300641 const struct cxsr_latency *latency;
642 u32 reg;
643 unsigned long wm;
644
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +0100645 latency = intel_get_cxsr_latency(IS_PINEVIEW_G(dev_priv),
646 dev_priv->is_ddr3,
647 dev_priv->fsb_freq,
648 dev_priv->mem_freq);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300649 if (!latency) {
650 DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
Imre Deak5209b1f2014-07-01 12:36:17 +0300651 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300652 return;
653 }
654
Ville Syrjäläffc7a762016-10-31 22:37:21 +0200655 crtc = single_enabled_crtc(dev_priv);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300656 if (crtc) {
Ville Syrjäläefc26112016-10-31 22:37:04 +0200657 const struct drm_display_mode *adjusted_mode =
658 &crtc->config->base.adjusted_mode;
659 const struct drm_framebuffer *fb =
660 crtc->base.primary->state->fb;
661 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +0300662 int clock = adjusted_mode->crtc_clock;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300663
664 /* Display SR */
665 wm = intel_calculate_wm(clock, &pineview_display_wm,
666 pineview_display_wm.fifo_size,
Ville Syrjäläac484962016-01-20 21:05:26 +0200667 cpp, latency->display_sr);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300668 reg = I915_READ(DSPFW1);
669 reg &= ~DSPFW_SR_MASK;
Ville Syrjäläf4998962015-03-10 17:02:21 +0200670 reg |= FW_WM(wm, SR);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300671 I915_WRITE(DSPFW1, reg);
672 DRM_DEBUG_KMS("DSPFW1 register is %x\n", reg);
673
674 /* cursor SR */
675 wm = intel_calculate_wm(clock, &pineview_cursor_wm,
676 pineview_display_wm.fifo_size,
Ville Syrjäläac484962016-01-20 21:05:26 +0200677 cpp, latency->cursor_sr);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300678 reg = I915_READ(DSPFW3);
679 reg &= ~DSPFW_CURSOR_SR_MASK;
Ville Syrjäläf4998962015-03-10 17:02:21 +0200680 reg |= FW_WM(wm, CURSOR_SR);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300681 I915_WRITE(DSPFW3, reg);
682
683 /* Display HPLL off SR */
684 wm = intel_calculate_wm(clock, &pineview_display_hplloff_wm,
685 pineview_display_hplloff_wm.fifo_size,
Ville Syrjäläac484962016-01-20 21:05:26 +0200686 cpp, latency->display_hpll_disable);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300687 reg = I915_READ(DSPFW3);
688 reg &= ~DSPFW_HPLL_SR_MASK;
Ville Syrjäläf4998962015-03-10 17:02:21 +0200689 reg |= FW_WM(wm, HPLL_SR);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300690 I915_WRITE(DSPFW3, reg);
691
692 /* cursor HPLL off SR */
693 wm = intel_calculate_wm(clock, &pineview_cursor_hplloff_wm,
694 pineview_display_hplloff_wm.fifo_size,
Ville Syrjäläac484962016-01-20 21:05:26 +0200695 cpp, latency->cursor_hpll_disable);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300696 reg = I915_READ(DSPFW3);
697 reg &= ~DSPFW_HPLL_CURSOR_MASK;
Ville Syrjäläf4998962015-03-10 17:02:21 +0200698 reg |= FW_WM(wm, HPLL_CURSOR);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300699 I915_WRITE(DSPFW3, reg);
700 DRM_DEBUG_KMS("DSPFW3 register is %x\n", reg);
701
Imre Deak5209b1f2014-07-01 12:36:17 +0300702 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300703 } else {
Imre Deak5209b1f2014-07-01 12:36:17 +0300704 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300705 }
706}
707
Ville Syrjäläf0ce2312016-10-31 22:37:08 +0200708static bool g4x_compute_wm0(struct drm_i915_private *dev_priv,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300709 int plane,
710 const struct intel_watermark_params *display,
711 int display_latency_ns,
712 const struct intel_watermark_params *cursor,
713 int cursor_latency_ns,
714 int *plane_wm,
715 int *cursor_wm)
716{
Ville Syrjäläefc26112016-10-31 22:37:04 +0200717 struct intel_crtc *crtc;
Ville Syrjälä4fe85902013-09-04 18:25:22 +0300718 const struct drm_display_mode *adjusted_mode;
Ville Syrjäläefc26112016-10-31 22:37:04 +0200719 const struct drm_framebuffer *fb;
Ville Syrjäläac484962016-01-20 21:05:26 +0200720 int htotal, hdisplay, clock, cpp;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300721 int line_time_us, line_count;
722 int entries, tlb_miss;
723
Ville Syrjäläb91eb5c2016-10-31 22:37:09 +0200724 crtc = intel_get_crtc_for_plane(dev_priv, plane);
Ville Syrjäläefc26112016-10-31 22:37:04 +0200725 if (!intel_crtc_active(crtc)) {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300726 *cursor_wm = cursor->guard_size;
727 *plane_wm = display->guard_size;
728 return false;
729 }
730
Ville Syrjäläefc26112016-10-31 22:37:04 +0200731 adjusted_mode = &crtc->config->base.adjusted_mode;
732 fb = crtc->base.primary->state->fb;
Damien Lespiau241bfc32013-09-25 16:45:37 +0100733 clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -0800734 htotal = adjusted_mode->crtc_htotal;
Ville Syrjäläefc26112016-10-31 22:37:04 +0200735 hdisplay = crtc->config->pipe_src_w;
736 cpp = drm_format_plane_cpp(fb->pixel_format, 0);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300737
738 /* Use the small buffer method to calculate plane watermark */
Ville Syrjäläac484962016-01-20 21:05:26 +0200739 entries = ((clock * cpp / 1000) * display_latency_ns) / 1000;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300740 tlb_miss = display->fifo_size*display->cacheline_size - hdisplay * 8;
741 if (tlb_miss > 0)
742 entries += tlb_miss;
743 entries = DIV_ROUND_UP(entries, display->cacheline_size);
744 *plane_wm = entries + display->guard_size;
745 if (*plane_wm > (int)display->max_wm)
746 *plane_wm = display->max_wm;
747
748 /* Use the large buffer method to calculate cursor watermark */
Ville Syrjälä922044c2014-02-14 14:18:57 +0200749 line_time_us = max(htotal * 1000 / clock, 1);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300750 line_count = (cursor_latency_ns / line_time_us + 1000) / 1000;
Ville Syrjäläefc26112016-10-31 22:37:04 +0200751 entries = line_count * crtc->base.cursor->state->crtc_w * cpp;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300752 tlb_miss = cursor->fifo_size*cursor->cacheline_size - hdisplay * 8;
753 if (tlb_miss > 0)
754 entries += tlb_miss;
755 entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
756 *cursor_wm = entries + cursor->guard_size;
757 if (*cursor_wm > (int)cursor->max_wm)
758 *cursor_wm = (int)cursor->max_wm;
759
760 return true;
761}
762
763/*
764 * Check the wm result.
765 *
766 * If any calculated watermark values is larger than the maximum value that
767 * can be programmed into the associated watermark register, that watermark
768 * must be disabled.
769 */
Ville Syrjäläf0ce2312016-10-31 22:37:08 +0200770static bool g4x_check_srwm(struct drm_i915_private *dev_priv,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300771 int display_wm, int cursor_wm,
772 const struct intel_watermark_params *display,
773 const struct intel_watermark_params *cursor)
774{
775 DRM_DEBUG_KMS("SR watermark: display plane %d, cursor %d\n",
776 display_wm, cursor_wm);
777
778 if (display_wm > display->max_wm) {
Tvrtko Ursulinae9400c2016-10-13 11:09:25 +0100779 DRM_DEBUG_KMS("display watermark is too large(%d/%u), disabling\n",
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300780 display_wm, display->max_wm);
781 return false;
782 }
783
784 if (cursor_wm > cursor->max_wm) {
Tvrtko Ursulinae9400c2016-10-13 11:09:25 +0100785 DRM_DEBUG_KMS("cursor watermark is too large(%d/%u), disabling\n",
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300786 cursor_wm, cursor->max_wm);
787 return false;
788 }
789
790 if (!(display_wm || cursor_wm)) {
791 DRM_DEBUG_KMS("SR latency is 0, disabling\n");
792 return false;
793 }
794
795 return true;
796}
797
Ville Syrjäläf0ce2312016-10-31 22:37:08 +0200798static bool g4x_compute_srwm(struct drm_i915_private *dev_priv,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300799 int plane,
800 int latency_ns,
801 const struct intel_watermark_params *display,
802 const struct intel_watermark_params *cursor,
803 int *display_wm, int *cursor_wm)
804{
Ville Syrjäläefc26112016-10-31 22:37:04 +0200805 struct intel_crtc *crtc;
Ville Syrjälä4fe85902013-09-04 18:25:22 +0300806 const struct drm_display_mode *adjusted_mode;
Ville Syrjäläefc26112016-10-31 22:37:04 +0200807 const struct drm_framebuffer *fb;
Ville Syrjäläac484962016-01-20 21:05:26 +0200808 int hdisplay, htotal, cpp, clock;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300809 unsigned long line_time_us;
810 int line_count, line_size;
811 int small, large;
812 int entries;
813
814 if (!latency_ns) {
815 *display_wm = *cursor_wm = 0;
816 return false;
817 }
818
Ville Syrjäläb91eb5c2016-10-31 22:37:09 +0200819 crtc = intel_get_crtc_for_plane(dev_priv, plane);
Ville Syrjäläefc26112016-10-31 22:37:04 +0200820 adjusted_mode = &crtc->config->base.adjusted_mode;
821 fb = crtc->base.primary->state->fb;
Damien Lespiau241bfc32013-09-25 16:45:37 +0100822 clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -0800823 htotal = adjusted_mode->crtc_htotal;
Ville Syrjäläefc26112016-10-31 22:37:04 +0200824 hdisplay = crtc->config->pipe_src_w;
825 cpp = drm_format_plane_cpp(fb->pixel_format, 0);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300826
Ville Syrjälä922044c2014-02-14 14:18:57 +0200827 line_time_us = max(htotal * 1000 / clock, 1);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300828 line_count = (latency_ns / line_time_us + 1000) / 1000;
Ville Syrjäläac484962016-01-20 21:05:26 +0200829 line_size = hdisplay * cpp;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300830
831 /* Use the minimum of the small and large buffer method for primary */
Ville Syrjäläac484962016-01-20 21:05:26 +0200832 small = ((clock * cpp / 1000) * latency_ns) / 1000;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300833 large = line_count * line_size;
834
835 entries = DIV_ROUND_UP(min(small, large), display->cacheline_size);
836 *display_wm = entries + display->guard_size;
837
838 /* calculate the self-refresh watermark for display cursor */
Ville Syrjäläefc26112016-10-31 22:37:04 +0200839 entries = line_count * cpp * crtc->base.cursor->state->crtc_w;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300840 entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
841 *cursor_wm = entries + cursor->guard_size;
842
Ville Syrjäläf0ce2312016-10-31 22:37:08 +0200843 return g4x_check_srwm(dev_priv,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300844 *display_wm, *cursor_wm,
845 display, cursor);
846}
847
Ville Syrjälä15665972015-03-10 16:16:28 +0200848#define FW_WM_VLV(value, plane) \
849 (((value) << DSPFW_ ## plane ## _SHIFT) & DSPFW_ ## plane ## _MASK_VLV)
850
Ville Syrjälä0018fda2015-03-05 21:19:45 +0200851static void vlv_write_wm_values(struct intel_crtc *crtc,
852 const struct vlv_wm_values *wm)
853{
854 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
855 enum pipe pipe = crtc->pipe;
856
857 I915_WRITE(VLV_DDL(pipe),
Ville Syrjälä1b313892016-11-28 19:37:08 +0200858 (wm->ddl[pipe].plane[PLANE_CURSOR] << DDL_CURSOR_SHIFT) |
859 (wm->ddl[pipe].plane[PLANE_SPRITE1] << DDL_SPRITE_SHIFT(1)) |
860 (wm->ddl[pipe].plane[PLANE_SPRITE0] << DDL_SPRITE_SHIFT(0)) |
861 (wm->ddl[pipe].plane[PLANE_PRIMARY] << DDL_PLANE_SHIFT));
Ville Syrjälä0018fda2015-03-05 21:19:45 +0200862
Ville Syrjäläae801522015-03-05 21:19:49 +0200863 I915_WRITE(DSPFW1,
Ville Syrjälä15665972015-03-10 16:16:28 +0200864 FW_WM(wm->sr.plane, SR) |
Ville Syrjälä1b313892016-11-28 19:37:08 +0200865 FW_WM(wm->pipe[PIPE_B].plane[PLANE_CURSOR], CURSORB) |
866 FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_PRIMARY], PLANEB) |
867 FW_WM_VLV(wm->pipe[PIPE_A].plane[PLANE_PRIMARY], PLANEA));
Ville Syrjäläae801522015-03-05 21:19:49 +0200868 I915_WRITE(DSPFW2,
Ville Syrjälä1b313892016-11-28 19:37:08 +0200869 FW_WM_VLV(wm->pipe[PIPE_A].plane[PLANE_SPRITE1], SPRITEB) |
870 FW_WM(wm->pipe[PIPE_A].plane[PLANE_CURSOR], CURSORA) |
871 FW_WM_VLV(wm->pipe[PIPE_A].plane[PLANE_SPRITE0], SPRITEA));
Ville Syrjäläae801522015-03-05 21:19:49 +0200872 I915_WRITE(DSPFW3,
Ville Syrjälä15665972015-03-10 16:16:28 +0200873 FW_WM(wm->sr.cursor, CURSOR_SR));
Ville Syrjäläae801522015-03-05 21:19:49 +0200874
875 if (IS_CHERRYVIEW(dev_priv)) {
876 I915_WRITE(DSPFW7_CHV,
Ville Syrjälä1b313892016-11-28 19:37:08 +0200877 FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_SPRITE1], SPRITED) |
878 FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_SPRITE0], SPRITEC));
Ville Syrjäläae801522015-03-05 21:19:49 +0200879 I915_WRITE(DSPFW8_CHV,
Ville Syrjälä1b313892016-11-28 19:37:08 +0200880 FW_WM_VLV(wm->pipe[PIPE_C].plane[PLANE_SPRITE1], SPRITEF) |
881 FW_WM_VLV(wm->pipe[PIPE_C].plane[PLANE_SPRITE0], SPRITEE));
Ville Syrjäläae801522015-03-05 21:19:49 +0200882 I915_WRITE(DSPFW9_CHV,
Ville Syrjälä1b313892016-11-28 19:37:08 +0200883 FW_WM_VLV(wm->pipe[PIPE_C].plane[PLANE_PRIMARY], PLANEC) |
884 FW_WM(wm->pipe[PIPE_C].plane[PLANE_CURSOR], CURSORC));
Ville Syrjäläae801522015-03-05 21:19:49 +0200885 I915_WRITE(DSPHOWM,
Ville Syrjälä15665972015-03-10 16:16:28 +0200886 FW_WM(wm->sr.plane >> 9, SR_HI) |
Ville Syrjälä1b313892016-11-28 19:37:08 +0200887 FW_WM(wm->pipe[PIPE_C].plane[PLANE_SPRITE1] >> 8, SPRITEF_HI) |
888 FW_WM(wm->pipe[PIPE_C].plane[PLANE_SPRITE0] >> 8, SPRITEE_HI) |
889 FW_WM(wm->pipe[PIPE_C].plane[PLANE_PRIMARY] >> 8, PLANEC_HI) |
890 FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE1] >> 8, SPRITED_HI) |
891 FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE0] >> 8, SPRITEC_HI) |
892 FW_WM(wm->pipe[PIPE_B].plane[PLANE_PRIMARY] >> 8, PLANEB_HI) |
893 FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE1] >> 8, SPRITEB_HI) |
894 FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE0] >> 8, SPRITEA_HI) |
895 FW_WM(wm->pipe[PIPE_A].plane[PLANE_PRIMARY] >> 8, PLANEA_HI));
Ville Syrjäläae801522015-03-05 21:19:49 +0200896 } else {
897 I915_WRITE(DSPFW7,
Ville Syrjälä1b313892016-11-28 19:37:08 +0200898 FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_SPRITE1], SPRITED) |
899 FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_SPRITE0], SPRITEC));
Ville Syrjäläae801522015-03-05 21:19:49 +0200900 I915_WRITE(DSPHOWM,
Ville Syrjälä15665972015-03-10 16:16:28 +0200901 FW_WM(wm->sr.plane >> 9, SR_HI) |
Ville Syrjälä1b313892016-11-28 19:37:08 +0200902 FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE1] >> 8, SPRITED_HI) |
903 FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE0] >> 8, SPRITEC_HI) |
904 FW_WM(wm->pipe[PIPE_B].plane[PLANE_PRIMARY] >> 8, PLANEB_HI) |
905 FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE1] >> 8, SPRITEB_HI) |
906 FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE0] >> 8, SPRITEA_HI) |
907 FW_WM(wm->pipe[PIPE_A].plane[PLANE_PRIMARY] >> 8, PLANEA_HI));
Ville Syrjäläae801522015-03-05 21:19:49 +0200908 }
909
Ville Syrjälä2cb389b2015-06-24 22:00:10 +0300910 /* zero (unused) WM1 watermarks */
911 I915_WRITE(DSPFW4, 0);
912 I915_WRITE(DSPFW5, 0);
913 I915_WRITE(DSPFW6, 0);
914 I915_WRITE(DSPHOWM1, 0);
915
Ville Syrjäläae801522015-03-05 21:19:49 +0200916 POSTING_READ(DSPFW1);
Ville Syrjälä0018fda2015-03-05 21:19:45 +0200917}
918
Ville Syrjälä15665972015-03-10 16:16:28 +0200919#undef FW_WM_VLV
920
Ville Syrjälä6eb1a682015-06-24 22:00:03 +0300921enum vlv_wm_level {
922 VLV_WM_LEVEL_PM2,
923 VLV_WM_LEVEL_PM5,
924 VLV_WM_LEVEL_DDR_DVFS,
Ville Syrjälä6eb1a682015-06-24 22:00:03 +0300925};
926
Ville Syrjälä262cd2e2015-06-24 22:00:04 +0300927/* latency must be in 0.1us units. */
928static unsigned int vlv_wm_method2(unsigned int pixel_rate,
929 unsigned int pipe_htotal,
930 unsigned int horiz_pixels,
Ville Syrjäläac484962016-01-20 21:05:26 +0200931 unsigned int cpp,
Ville Syrjälä262cd2e2015-06-24 22:00:04 +0300932 unsigned int latency)
933{
934 unsigned int ret;
935
936 ret = (latency * pixel_rate) / (pipe_htotal * 10000);
Ville Syrjäläac484962016-01-20 21:05:26 +0200937 ret = (ret + 1) * horiz_pixels * cpp;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +0300938 ret = DIV_ROUND_UP(ret, 64);
939
940 return ret;
941}
942
Ville Syrjäläbb726512016-10-31 22:37:24 +0200943static void vlv_setup_wm_latency(struct drm_i915_private *dev_priv)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +0300944{
Ville Syrjälä262cd2e2015-06-24 22:00:04 +0300945 /* all latencies in usec */
946 dev_priv->wm.pri_latency[VLV_WM_LEVEL_PM2] = 3;
947
Ville Syrjälä58590c12015-09-08 21:05:12 +0300948 dev_priv->wm.max_level = VLV_WM_LEVEL_PM2;
949
Ville Syrjälä262cd2e2015-06-24 22:00:04 +0300950 if (IS_CHERRYVIEW(dev_priv)) {
951 dev_priv->wm.pri_latency[VLV_WM_LEVEL_PM5] = 12;
952 dev_priv->wm.pri_latency[VLV_WM_LEVEL_DDR_DVFS] = 33;
Ville Syrjälä58590c12015-09-08 21:05:12 +0300953
954 dev_priv->wm.max_level = VLV_WM_LEVEL_DDR_DVFS;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +0300955 }
956}
957
958static uint16_t vlv_compute_wm_level(struct intel_plane *plane,
959 struct intel_crtc *crtc,
960 const struct intel_plane_state *state,
961 int level)
962{
963 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
Ville Syrjäläac484962016-01-20 21:05:26 +0200964 int clock, htotal, cpp, width, wm;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +0300965
966 if (dev_priv->wm.pri_latency[level] == 0)
967 return USHRT_MAX;
968
Ville Syrjälä936e71e2016-07-26 19:06:59 +0300969 if (!state->base.visible)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +0300970 return 0;
971
Ville Syrjäläac484962016-01-20 21:05:26 +0200972 cpp = drm_format_plane_cpp(state->base.fb->pixel_format, 0);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +0300973 clock = crtc->config->base.adjusted_mode.crtc_clock;
974 htotal = crtc->config->base.adjusted_mode.crtc_htotal;
975 width = crtc->config->pipe_src_w;
976 if (WARN_ON(htotal == 0))
977 htotal = 1;
978
979 if (plane->base.type == DRM_PLANE_TYPE_CURSOR) {
980 /*
981 * FIXME the formula gives values that are
982 * too big for the cursor FIFO, and hence we
983 * would never be able to use cursors. For
984 * now just hardcode the watermark.
985 */
986 wm = 63;
987 } else {
Ville Syrjäläac484962016-01-20 21:05:26 +0200988 wm = vlv_wm_method2(clock, htotal, width, cpp,
Ville Syrjälä262cd2e2015-06-24 22:00:04 +0300989 dev_priv->wm.pri_latency[level] * 10);
990 }
991
992 return min_t(int, wm, USHRT_MAX);
993}
994
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +0300995static void vlv_compute_fifo(struct intel_crtc *crtc)
996{
997 struct drm_device *dev = crtc->base.dev;
998 struct vlv_wm_state *wm_state = &crtc->wm_state;
999 struct intel_plane *plane;
1000 unsigned int total_rate = 0;
1001 const int fifo_size = 512 - 1;
1002 int fifo_extra, fifo_left = fifo_size;
1003
1004 for_each_intel_plane_on_crtc(dev, crtc, plane) {
1005 struct intel_plane_state *state =
1006 to_intel_plane_state(plane->base.state);
1007
1008 if (plane->base.type == DRM_PLANE_TYPE_CURSOR)
1009 continue;
1010
Ville Syrjälä936e71e2016-07-26 19:06:59 +03001011 if (state->base.visible) {
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001012 wm_state->num_active_planes++;
1013 total_rate += drm_format_plane_cpp(state->base.fb->pixel_format, 0);
1014 }
1015 }
1016
1017 for_each_intel_plane_on_crtc(dev, crtc, plane) {
1018 struct intel_plane_state *state =
1019 to_intel_plane_state(plane->base.state);
1020 unsigned int rate;
1021
1022 if (plane->base.type == DRM_PLANE_TYPE_CURSOR) {
1023 plane->wm.fifo_size = 63;
1024 continue;
1025 }
1026
Ville Syrjälä936e71e2016-07-26 19:06:59 +03001027 if (!state->base.visible) {
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001028 plane->wm.fifo_size = 0;
1029 continue;
1030 }
1031
1032 rate = drm_format_plane_cpp(state->base.fb->pixel_format, 0);
1033 plane->wm.fifo_size = fifo_size * rate / total_rate;
1034 fifo_left -= plane->wm.fifo_size;
1035 }
1036
1037 fifo_extra = DIV_ROUND_UP(fifo_left, wm_state->num_active_planes ?: 1);
1038
1039 /* spread the remainder evenly */
1040 for_each_intel_plane_on_crtc(dev, crtc, plane) {
1041 int plane_extra;
1042
1043 if (fifo_left == 0)
1044 break;
1045
1046 if (plane->base.type == DRM_PLANE_TYPE_CURSOR)
1047 continue;
1048
1049 /* give it all to the first plane if none are active */
1050 if (plane->wm.fifo_size == 0 &&
1051 wm_state->num_active_planes)
1052 continue;
1053
1054 plane_extra = min(fifo_extra, fifo_left);
1055 plane->wm.fifo_size += plane_extra;
1056 fifo_left -= plane_extra;
1057 }
1058
1059 WARN_ON(fifo_left != 0);
1060}
1061
Ville Syrjälä26cca0e2016-11-28 19:37:09 +02001062static u16 vlv_invert_wm_value(u16 wm, u16 fifo_size)
1063{
1064 if (wm > fifo_size)
1065 return USHRT_MAX;
1066 else
1067 return fifo_size - wm;
1068}
1069
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001070static void vlv_invert_wms(struct intel_crtc *crtc)
1071{
1072 struct vlv_wm_state *wm_state = &crtc->wm_state;
1073 int level;
1074
1075 for (level = 0; level < wm_state->num_levels; level++) {
Ville Syrjälä7c951c02016-11-28 19:37:10 +02001076 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Tvrtko Ursulinb7f05d42016-11-09 11:30:45 +00001077 const int sr_fifo_size =
Ville Syrjälä7c951c02016-11-28 19:37:10 +02001078 INTEL_INFO(dev_priv)->num_pipes * 512 - 1;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001079 struct intel_plane *plane;
1080
Ville Syrjälä26cca0e2016-11-28 19:37:09 +02001081 wm_state->sr[level].plane =
1082 vlv_invert_wm_value(wm_state->sr[level].plane,
1083 sr_fifo_size);
1084 wm_state->sr[level].cursor =
1085 vlv_invert_wm_value(wm_state->sr[level].cursor,
1086 63);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001087
Ville Syrjälä7c951c02016-11-28 19:37:10 +02001088 for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane) {
Ville Syrjälä26cca0e2016-11-28 19:37:09 +02001089 wm_state->wm[level].plane[plane->id] =
1090 vlv_invert_wm_value(wm_state->wm[level].plane[plane->id],
1091 plane->wm.fifo_size);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001092 }
1093 }
1094}
1095
Ville Syrjälä26e1fe42015-06-24 22:00:06 +03001096static void vlv_compute_wm(struct intel_crtc *crtc)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001097{
Ville Syrjälä7c951c02016-11-28 19:37:10 +02001098 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001099 struct vlv_wm_state *wm_state = &crtc->wm_state;
1100 struct intel_plane *plane;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001101 int level;
1102
1103 memset(wm_state, 0, sizeof(*wm_state));
1104
Ville Syrjälä852eb002015-06-24 22:00:07 +03001105 wm_state->cxsr = crtc->pipe != PIPE_C && crtc->wm.cxsr_allowed;
Tvrtko Ursulinb7f05d42016-11-09 11:30:45 +00001106 wm_state->num_levels = dev_priv->wm.max_level + 1;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001107
1108 wm_state->num_active_planes = 0;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001109
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001110 vlv_compute_fifo(crtc);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001111
1112 if (wm_state->num_active_planes != 1)
1113 wm_state->cxsr = false;
1114
Ville Syrjälä7c951c02016-11-28 19:37:10 +02001115 for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane) {
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001116 struct intel_plane_state *state =
1117 to_intel_plane_state(plane->base.state);
Ville Syrjälä1b313892016-11-28 19:37:08 +02001118 int level;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001119
Ville Syrjälä936e71e2016-07-26 19:06:59 +03001120 if (!state->base.visible)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001121 continue;
1122
1123 /* normal watermarks */
1124 for (level = 0; level < wm_state->num_levels; level++) {
1125 int wm = vlv_compute_wm_level(plane, crtc, state, level);
Ville Syrjälä1be4d372016-11-28 19:37:05 +02001126 int max_wm = plane->wm.fifo_size;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001127
1128 /* hack */
1129 if (WARN_ON(level == 0 && wm > max_wm))
1130 wm = max_wm;
1131
Ville Syrjälä1be4d372016-11-28 19:37:05 +02001132 if (wm > max_wm)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001133 break;
1134
Ville Syrjälä1b313892016-11-28 19:37:08 +02001135 wm_state->wm[level].plane[plane->id] = wm;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001136 }
1137
1138 wm_state->num_levels = level;
1139
1140 if (!wm_state->cxsr)
1141 continue;
1142
1143 /* maxfifo watermarks */
Ville Syrjälä1b313892016-11-28 19:37:08 +02001144 if (plane->id == PLANE_CURSOR) {
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001145 for (level = 0; level < wm_state->num_levels; level++)
1146 wm_state->sr[level].cursor =
Ville Syrjälä1b313892016-11-28 19:37:08 +02001147 wm_state->wm[level].plane[PLANE_CURSOR];
1148 } else {
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001149 for (level = 0; level < wm_state->num_levels; level++)
1150 wm_state->sr[level].plane =
Ville Syrjälä50a9dd32016-11-28 19:37:06 +02001151 max(wm_state->sr[level].plane,
Ville Syrjälä1b313892016-11-28 19:37:08 +02001152 wm_state->wm[level].plane[plane->id]);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001153 }
1154 }
1155
1156 /* clear any (partially) filled invalid levels */
Tvrtko Ursulinb7f05d42016-11-09 11:30:45 +00001157 for (level = wm_state->num_levels; level < dev_priv->wm.max_level + 1; level++) {
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001158 memset(&wm_state->wm[level], 0, sizeof(wm_state->wm[level]));
1159 memset(&wm_state->sr[level], 0, sizeof(wm_state->sr[level]));
1160 }
1161
1162 vlv_invert_wms(crtc);
1163}
1164
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001165#define VLV_FIFO(plane, value) \
1166 (((value) << DSPARB_ ## plane ## _SHIFT_VLV) & DSPARB_ ## plane ## _MASK_VLV)
1167
1168static void vlv_pipe_set_fifo_size(struct intel_crtc *crtc)
1169{
1170 struct drm_device *dev = crtc->base.dev;
1171 struct drm_i915_private *dev_priv = to_i915(dev);
1172 struct intel_plane *plane;
1173 int sprite0_start = 0, sprite1_start = 0, fifo_size = 0;
1174
1175 for_each_intel_plane_on_crtc(dev, crtc, plane) {
Ville Syrjälä49845a22016-11-22 18:02:01 +02001176 switch (plane->id) {
1177 case PLANE_PRIMARY:
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001178 sprite0_start = plane->wm.fifo_size;
Ville Syrjälä49845a22016-11-22 18:02:01 +02001179 break;
1180 case PLANE_SPRITE0:
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001181 sprite1_start = sprite0_start + plane->wm.fifo_size;
Ville Syrjälä49845a22016-11-22 18:02:01 +02001182 break;
1183 case PLANE_SPRITE1:
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001184 fifo_size = sprite1_start + plane->wm.fifo_size;
Ville Syrjälä49845a22016-11-22 18:02:01 +02001185 break;
1186 case PLANE_CURSOR:
1187 WARN_ON(plane->wm.fifo_size != 63);
1188 break;
1189 default:
1190 MISSING_CASE(plane->id);
1191 break;
1192 }
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001193 }
1194
1195 WARN_ON(fifo_size != 512 - 1);
1196
1197 DRM_DEBUG_KMS("Pipe %c FIFO split %d / %d / %d\n",
1198 pipe_name(crtc->pipe), sprite0_start,
1199 sprite1_start, fifo_size);
1200
1201 switch (crtc->pipe) {
1202 uint32_t dsparb, dsparb2, dsparb3;
1203 case PIPE_A:
1204 dsparb = I915_READ(DSPARB);
1205 dsparb2 = I915_READ(DSPARB2);
1206
1207 dsparb &= ~(VLV_FIFO(SPRITEA, 0xff) |
1208 VLV_FIFO(SPRITEB, 0xff));
1209 dsparb |= (VLV_FIFO(SPRITEA, sprite0_start) |
1210 VLV_FIFO(SPRITEB, sprite1_start));
1211
1212 dsparb2 &= ~(VLV_FIFO(SPRITEA_HI, 0x1) |
1213 VLV_FIFO(SPRITEB_HI, 0x1));
1214 dsparb2 |= (VLV_FIFO(SPRITEA_HI, sprite0_start >> 8) |
1215 VLV_FIFO(SPRITEB_HI, sprite1_start >> 8));
1216
1217 I915_WRITE(DSPARB, dsparb);
1218 I915_WRITE(DSPARB2, dsparb2);
1219 break;
1220 case PIPE_B:
1221 dsparb = I915_READ(DSPARB);
1222 dsparb2 = I915_READ(DSPARB2);
1223
1224 dsparb &= ~(VLV_FIFO(SPRITEC, 0xff) |
1225 VLV_FIFO(SPRITED, 0xff));
1226 dsparb |= (VLV_FIFO(SPRITEC, sprite0_start) |
1227 VLV_FIFO(SPRITED, sprite1_start));
1228
1229 dsparb2 &= ~(VLV_FIFO(SPRITEC_HI, 0xff) |
1230 VLV_FIFO(SPRITED_HI, 0xff));
1231 dsparb2 |= (VLV_FIFO(SPRITEC_HI, sprite0_start >> 8) |
1232 VLV_FIFO(SPRITED_HI, sprite1_start >> 8));
1233
1234 I915_WRITE(DSPARB, dsparb);
1235 I915_WRITE(DSPARB2, dsparb2);
1236 break;
1237 case PIPE_C:
1238 dsparb3 = I915_READ(DSPARB3);
1239 dsparb2 = I915_READ(DSPARB2);
1240
1241 dsparb3 &= ~(VLV_FIFO(SPRITEE, 0xff) |
1242 VLV_FIFO(SPRITEF, 0xff));
1243 dsparb3 |= (VLV_FIFO(SPRITEE, sprite0_start) |
1244 VLV_FIFO(SPRITEF, sprite1_start));
1245
1246 dsparb2 &= ~(VLV_FIFO(SPRITEE_HI, 0xff) |
1247 VLV_FIFO(SPRITEF_HI, 0xff));
1248 dsparb2 |= (VLV_FIFO(SPRITEE_HI, sprite0_start >> 8) |
1249 VLV_FIFO(SPRITEF_HI, sprite1_start >> 8));
1250
1251 I915_WRITE(DSPARB3, dsparb3);
1252 I915_WRITE(DSPARB2, dsparb2);
1253 break;
1254 default:
1255 break;
1256 }
1257}
1258
1259#undef VLV_FIFO
1260
Ville Syrjälä7c951c02016-11-28 19:37:10 +02001261static void vlv_merge_wm(struct drm_i915_private *dev_priv,
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001262 struct vlv_wm_values *wm)
1263{
1264 struct intel_crtc *crtc;
1265 int num_active_crtcs = 0;
1266
Ville Syrjälä7c951c02016-11-28 19:37:10 +02001267 wm->level = dev_priv->wm.max_level;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001268 wm->cxsr = true;
1269
Ville Syrjälä7c951c02016-11-28 19:37:10 +02001270 for_each_intel_crtc(&dev_priv->drm, crtc) {
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001271 const struct vlv_wm_state *wm_state = &crtc->wm_state;
1272
1273 if (!crtc->active)
1274 continue;
1275
1276 if (!wm_state->cxsr)
1277 wm->cxsr = false;
1278
1279 num_active_crtcs++;
1280 wm->level = min_t(int, wm->level, wm_state->num_levels - 1);
1281 }
1282
1283 if (num_active_crtcs != 1)
1284 wm->cxsr = false;
1285
Ville Syrjälä6f9c7842015-06-24 22:00:08 +03001286 if (num_active_crtcs > 1)
1287 wm->level = VLV_WM_LEVEL_PM2;
1288
Ville Syrjälä7c951c02016-11-28 19:37:10 +02001289 for_each_intel_crtc(&dev_priv->drm, crtc) {
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001290 struct vlv_wm_state *wm_state = &crtc->wm_state;
1291 enum pipe pipe = crtc->pipe;
1292
1293 if (!crtc->active)
1294 continue;
1295
1296 wm->pipe[pipe] = wm_state->wm[wm->level];
1297 if (wm->cxsr)
1298 wm->sr = wm_state->sr[wm->level];
1299
Ville Syrjälä1b313892016-11-28 19:37:08 +02001300 wm->ddl[pipe].plane[PLANE_PRIMARY] = DDL_PRECISION_HIGH | 2;
1301 wm->ddl[pipe].plane[PLANE_SPRITE0] = DDL_PRECISION_HIGH | 2;
1302 wm->ddl[pipe].plane[PLANE_SPRITE1] = DDL_PRECISION_HIGH | 2;
1303 wm->ddl[pipe].plane[PLANE_CURSOR] = DDL_PRECISION_HIGH | 2;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001304 }
1305}
1306
Ville Syrjälä432081b2016-10-31 22:37:03 +02001307static void vlv_update_wm(struct intel_crtc *crtc)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001308{
Ville Syrjälä7c951c02016-11-28 19:37:10 +02001309 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä432081b2016-10-31 22:37:03 +02001310 enum pipe pipe = crtc->pipe;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001311 struct vlv_wm_values wm = {};
1312
Ville Syrjälä432081b2016-10-31 22:37:03 +02001313 vlv_compute_wm(crtc);
Ville Syrjälä7c951c02016-11-28 19:37:10 +02001314 vlv_merge_wm(dev_priv, &wm);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001315
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001316 if (memcmp(&dev_priv->wm.vlv, &wm, sizeof(wm)) == 0) {
1317 /* FIXME should be part of crtc atomic commit */
Ville Syrjälä432081b2016-10-31 22:37:03 +02001318 vlv_pipe_set_fifo_size(crtc);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001319 return;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001320 }
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001321
1322 if (wm.level < VLV_WM_LEVEL_DDR_DVFS &&
1323 dev_priv->wm.vlv.level >= VLV_WM_LEVEL_DDR_DVFS)
1324 chv_set_memory_dvfs(dev_priv, false);
1325
1326 if (wm.level < VLV_WM_LEVEL_PM5 &&
1327 dev_priv->wm.vlv.level >= VLV_WM_LEVEL_PM5)
1328 chv_set_memory_pm5(dev_priv, false);
1329
Ville Syrjälä852eb002015-06-24 22:00:07 +03001330 if (!wm.cxsr && dev_priv->wm.vlv.cxsr)
Ville Syrjälä3d90e642016-11-28 19:37:11 +02001331 _intel_set_memory_cxsr(dev_priv, false);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001332
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001333 /* FIXME should be part of crtc atomic commit */
Ville Syrjälä432081b2016-10-31 22:37:03 +02001334 vlv_pipe_set_fifo_size(crtc);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001335
Ville Syrjälä432081b2016-10-31 22:37:03 +02001336 vlv_write_wm_values(crtc, &wm);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001337
1338 DRM_DEBUG_KMS("Setting FIFO watermarks - %c: plane=%d, cursor=%d, "
1339 "sprite0=%d, sprite1=%d, SR: plane=%d, cursor=%d level=%d cxsr=%d\n",
Ville Syrjälä1b313892016-11-28 19:37:08 +02001340 pipe_name(pipe), wm.pipe[pipe].plane[PLANE_PRIMARY], wm.pipe[pipe].plane[PLANE_CURSOR],
1341 wm.pipe[pipe].plane[PLANE_SPRITE0], wm.pipe[pipe].plane[PLANE_SPRITE1],
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001342 wm.sr.plane, wm.sr.cursor, wm.level, wm.cxsr);
1343
Ville Syrjälä852eb002015-06-24 22:00:07 +03001344 if (wm.cxsr && !dev_priv->wm.vlv.cxsr)
Ville Syrjälä3d90e642016-11-28 19:37:11 +02001345 _intel_set_memory_cxsr(dev_priv, true);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001346
1347 if (wm.level >= VLV_WM_LEVEL_PM5 &&
1348 dev_priv->wm.vlv.level < VLV_WM_LEVEL_PM5)
1349 chv_set_memory_pm5(dev_priv, true);
1350
1351 if (wm.level >= VLV_WM_LEVEL_DDR_DVFS &&
1352 dev_priv->wm.vlv.level < VLV_WM_LEVEL_DDR_DVFS)
1353 chv_set_memory_dvfs(dev_priv, true);
1354
1355 dev_priv->wm.vlv = wm;
Ville Syrjälä3c2777f2014-06-26 17:03:06 +03001356}
1357
Ville Syrjäläae801522015-03-05 21:19:49 +02001358#define single_plane_enabled(mask) is_power_of_2(mask)
1359
Ville Syrjälä432081b2016-10-31 22:37:03 +02001360static void g4x_update_wm(struct intel_crtc *crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001361{
Ville Syrjäläb91eb5c2016-10-31 22:37:09 +02001362 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001363 static const int sr_latency_ns = 12000;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001364 int planea_wm, planeb_wm, cursora_wm, cursorb_wm;
1365 int plane_sr, cursor_sr;
1366 unsigned int enabled = 0;
Imre Deak98584252014-06-13 14:54:20 +03001367 bool cxsr_enabled;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001368
Ville Syrjäläf0ce2312016-10-31 22:37:08 +02001369 if (g4x_compute_wm0(dev_priv, PIPE_A,
Chris Wilson5aef6002014-09-03 11:56:07 +01001370 &g4x_wm_info, pessimal_latency_ns,
1371 &g4x_cursor_wm_info, pessimal_latency_ns,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001372 &planea_wm, &cursora_wm))
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001373 enabled |= 1 << PIPE_A;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001374
Ville Syrjäläf0ce2312016-10-31 22:37:08 +02001375 if (g4x_compute_wm0(dev_priv, PIPE_B,
Chris Wilson5aef6002014-09-03 11:56:07 +01001376 &g4x_wm_info, pessimal_latency_ns,
1377 &g4x_cursor_wm_info, pessimal_latency_ns,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001378 &planeb_wm, &cursorb_wm))
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001379 enabled |= 1 << PIPE_B;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001380
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001381 if (single_plane_enabled(enabled) &&
Ville Syrjäläf0ce2312016-10-31 22:37:08 +02001382 g4x_compute_srwm(dev_priv, ffs(enabled) - 1,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001383 sr_latency_ns,
1384 &g4x_wm_info,
1385 &g4x_cursor_wm_info,
Chris Wilson52bd02d2012-12-07 10:43:24 +00001386 &plane_sr, &cursor_sr)) {
Imre Deak98584252014-06-13 14:54:20 +03001387 cxsr_enabled = true;
Chris Wilson52bd02d2012-12-07 10:43:24 +00001388 } else {
Imre Deak98584252014-06-13 14:54:20 +03001389 cxsr_enabled = false;
Imre Deak5209b1f2014-07-01 12:36:17 +03001390 intel_set_memory_cxsr(dev_priv, false);
Chris Wilson52bd02d2012-12-07 10:43:24 +00001391 plane_sr = cursor_sr = 0;
1392 }
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001393
Ville Syrjäläa5043452014-06-28 02:04:18 +03001394 DRM_DEBUG_KMS("Setting FIFO watermarks - A: plane=%d, cursor=%d, "
1395 "B: plane=%d, cursor=%d, SR: plane=%d, cursor=%d\n",
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001396 planea_wm, cursora_wm,
1397 planeb_wm, cursorb_wm,
1398 plane_sr, cursor_sr);
1399
1400 I915_WRITE(DSPFW1,
Ville Syrjäläf4998962015-03-10 17:02:21 +02001401 FW_WM(plane_sr, SR) |
1402 FW_WM(cursorb_wm, CURSORB) |
1403 FW_WM(planeb_wm, PLANEB) |
1404 FW_WM(planea_wm, PLANEA));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001405 I915_WRITE(DSPFW2,
Chris Wilson8c919b22012-12-04 16:33:19 +00001406 (I915_READ(DSPFW2) & ~DSPFW_CURSORA_MASK) |
Ville Syrjäläf4998962015-03-10 17:02:21 +02001407 FW_WM(cursora_wm, CURSORA));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001408 /* HPLL off in SR has some issues on G4x... disable it */
1409 I915_WRITE(DSPFW3,
Chris Wilson8c919b22012-12-04 16:33:19 +00001410 (I915_READ(DSPFW3) & ~(DSPFW_HPLL_SR_EN | DSPFW_CURSOR_SR_MASK)) |
Ville Syrjäläf4998962015-03-10 17:02:21 +02001411 FW_WM(cursor_sr, CURSOR_SR));
Imre Deak98584252014-06-13 14:54:20 +03001412
1413 if (cxsr_enabled)
1414 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001415}
1416
Ville Syrjälä432081b2016-10-31 22:37:03 +02001417static void i965_update_wm(struct intel_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001418{
Ville Syrjäläffc7a762016-10-31 22:37:21 +02001419 struct drm_i915_private *dev_priv = to_i915(unused_crtc->base.dev);
Ville Syrjäläefc26112016-10-31 22:37:04 +02001420 struct intel_crtc *crtc;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001421 int srwm = 1;
1422 int cursor_sr = 16;
Imre Deak98584252014-06-13 14:54:20 +03001423 bool cxsr_enabled;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001424
1425 /* Calc sr entries for one plane configs */
Ville Syrjäläffc7a762016-10-31 22:37:21 +02001426 crtc = single_enabled_crtc(dev_priv);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001427 if (crtc) {
1428 /* self-refresh has much higher latency */
1429 static const int sr_latency_ns = 12000;
Ville Syrjäläefc26112016-10-31 22:37:04 +02001430 const struct drm_display_mode *adjusted_mode =
1431 &crtc->config->base.adjusted_mode;
1432 const struct drm_framebuffer *fb =
1433 crtc->base.primary->state->fb;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001434 int clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -08001435 int htotal = adjusted_mode->crtc_htotal;
Ville Syrjäläefc26112016-10-31 22:37:04 +02001436 int hdisplay = crtc->config->pipe_src_w;
1437 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001438 unsigned long line_time_us;
1439 int entries;
1440
Ville Syrjälä922044c2014-02-14 14:18:57 +02001441 line_time_us = max(htotal * 1000 / clock, 1);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001442
1443 /* Use ns/us then divide to preserve precision */
1444 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
Ville Syrjäläac484962016-01-20 21:05:26 +02001445 cpp * hdisplay;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001446 entries = DIV_ROUND_UP(entries, I915_FIFO_LINE_SIZE);
1447 srwm = I965_FIFO_SIZE - entries;
1448 if (srwm < 0)
1449 srwm = 1;
1450 srwm &= 0x1ff;
1451 DRM_DEBUG_KMS("self-refresh entries: %d, wm: %d\n",
1452 entries, srwm);
1453
1454 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
Ville Syrjäläefc26112016-10-31 22:37:04 +02001455 cpp * crtc->base.cursor->state->crtc_w;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001456 entries = DIV_ROUND_UP(entries,
1457 i965_cursor_wm_info.cacheline_size);
1458 cursor_sr = i965_cursor_wm_info.fifo_size -
1459 (entries + i965_cursor_wm_info.guard_size);
1460
1461 if (cursor_sr > i965_cursor_wm_info.max_wm)
1462 cursor_sr = i965_cursor_wm_info.max_wm;
1463
1464 DRM_DEBUG_KMS("self-refresh watermark: display plane %d "
1465 "cursor %d\n", srwm, cursor_sr);
1466
Imre Deak98584252014-06-13 14:54:20 +03001467 cxsr_enabled = true;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001468 } else {
Imre Deak98584252014-06-13 14:54:20 +03001469 cxsr_enabled = false;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001470 /* Turn off self refresh if both pipes are enabled */
Imre Deak5209b1f2014-07-01 12:36:17 +03001471 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001472 }
1473
1474 DRM_DEBUG_KMS("Setting FIFO watermarks - A: 8, B: 8, C: 8, SR %d\n",
1475 srwm);
1476
1477 /* 965 has limitations... */
Ville Syrjäläf4998962015-03-10 17:02:21 +02001478 I915_WRITE(DSPFW1, FW_WM(srwm, SR) |
1479 FW_WM(8, CURSORB) |
1480 FW_WM(8, PLANEB) |
1481 FW_WM(8, PLANEA));
1482 I915_WRITE(DSPFW2, FW_WM(8, CURSORA) |
1483 FW_WM(8, PLANEC_OLD));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001484 /* update cursor SR watermark */
Ville Syrjäläf4998962015-03-10 17:02:21 +02001485 I915_WRITE(DSPFW3, FW_WM(cursor_sr, CURSOR_SR));
Imre Deak98584252014-06-13 14:54:20 +03001486
1487 if (cxsr_enabled)
1488 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001489}
1490
Ville Syrjäläf4998962015-03-10 17:02:21 +02001491#undef FW_WM
1492
Ville Syrjälä432081b2016-10-31 22:37:03 +02001493static void i9xx_update_wm(struct intel_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001494{
Ville Syrjäläffc7a762016-10-31 22:37:21 +02001495 struct drm_i915_private *dev_priv = to_i915(unused_crtc->base.dev);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001496 const struct intel_watermark_params *wm_info;
1497 uint32_t fwater_lo;
1498 uint32_t fwater_hi;
1499 int cwm, srwm = 1;
1500 int fifo_size;
1501 int planea_wm, planeb_wm;
Ville Syrjäläefc26112016-10-31 22:37:04 +02001502 struct intel_crtc *crtc, *enabled = NULL;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001503
Ville Syrjäläa9097be2016-10-31 22:37:20 +02001504 if (IS_I945GM(dev_priv))
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001505 wm_info = &i945_wm_info;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01001506 else if (!IS_GEN2(dev_priv))
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001507 wm_info = &i915_wm_info;
1508 else
Ville Syrjälä9d539102014-08-15 01:21:53 +03001509 wm_info = &i830_a_wm_info;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001510
Ville Syrjäläef0f5e92016-10-31 22:37:17 +02001511 fifo_size = dev_priv->display.get_fifo_size(dev_priv, 0);
Ville Syrjäläb91eb5c2016-10-31 22:37:09 +02001512 crtc = intel_get_crtc_for_plane(dev_priv, 0);
Ville Syrjäläefc26112016-10-31 22:37:04 +02001513 if (intel_crtc_active(crtc)) {
1514 const struct drm_display_mode *adjusted_mode =
1515 &crtc->config->base.adjusted_mode;
1516 const struct drm_framebuffer *fb =
1517 crtc->base.primary->state->fb;
1518 int cpp;
1519
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01001520 if (IS_GEN2(dev_priv))
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001521 cpp = 4;
Ville Syrjäläefc26112016-10-31 22:37:04 +02001522 else
1523 cpp = drm_format_plane_cpp(fb->pixel_format, 0);
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001524
Damien Lespiau241bfc32013-09-25 16:45:37 +01001525 planea_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001526 wm_info, fifo_size, cpp,
Chris Wilson5aef6002014-09-03 11:56:07 +01001527 pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001528 enabled = crtc;
Ville Syrjälä9d539102014-08-15 01:21:53 +03001529 } else {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001530 planea_wm = fifo_size - wm_info->guard_size;
Ville Syrjälä9d539102014-08-15 01:21:53 +03001531 if (planea_wm > (long)wm_info->max_wm)
1532 planea_wm = wm_info->max_wm;
1533 }
1534
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01001535 if (IS_GEN2(dev_priv))
Ville Syrjälä9d539102014-08-15 01:21:53 +03001536 wm_info = &i830_bc_wm_info;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001537
Ville Syrjäläef0f5e92016-10-31 22:37:17 +02001538 fifo_size = dev_priv->display.get_fifo_size(dev_priv, 1);
Ville Syrjäläb91eb5c2016-10-31 22:37:09 +02001539 crtc = intel_get_crtc_for_plane(dev_priv, 1);
Ville Syrjäläefc26112016-10-31 22:37:04 +02001540 if (intel_crtc_active(crtc)) {
1541 const struct drm_display_mode *adjusted_mode =
1542 &crtc->config->base.adjusted_mode;
1543 const struct drm_framebuffer *fb =
1544 crtc->base.primary->state->fb;
1545 int cpp;
1546
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01001547 if (IS_GEN2(dev_priv))
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001548 cpp = 4;
Ville Syrjäläefc26112016-10-31 22:37:04 +02001549 else
1550 cpp = drm_format_plane_cpp(fb->pixel_format, 0);
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001551
Damien Lespiau241bfc32013-09-25 16:45:37 +01001552 planeb_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001553 wm_info, fifo_size, cpp,
Chris Wilson5aef6002014-09-03 11:56:07 +01001554 pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001555 if (enabled == NULL)
1556 enabled = crtc;
1557 else
1558 enabled = NULL;
Ville Syrjälä9d539102014-08-15 01:21:53 +03001559 } else {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001560 planeb_wm = fifo_size - wm_info->guard_size;
Ville Syrjälä9d539102014-08-15 01:21:53 +03001561 if (planeb_wm > (long)wm_info->max_wm)
1562 planeb_wm = wm_info->max_wm;
1563 }
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001564
1565 DRM_DEBUG_KMS("FIFO watermarks - A: %d, B: %d\n", planea_wm, planeb_wm);
1566
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01001567 if (IS_I915GM(dev_priv) && enabled) {
Matt Roper2ff8fde2014-07-08 07:50:07 -07001568 struct drm_i915_gem_object *obj;
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02001569
Ville Syrjäläefc26112016-10-31 22:37:04 +02001570 obj = intel_fb_obj(enabled->base.primary->state->fb);
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02001571
1572 /* self-refresh seems busted with untiled */
Chris Wilson3e510a82016-08-05 10:14:23 +01001573 if (!i915_gem_object_is_tiled(obj))
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02001574 enabled = NULL;
1575 }
1576
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001577 /*
1578 * Overlay gets an aggressive default since video jitter is bad.
1579 */
1580 cwm = 2;
1581
1582 /* Play safe and disable self-refresh before adjusting watermarks. */
Imre Deak5209b1f2014-07-01 12:36:17 +03001583 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001584
1585 /* Calc sr entries for one plane configs */
Ville Syrjälä03427fc2016-10-31 22:37:18 +02001586 if (HAS_FW_BLC(dev_priv) && enabled) {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001587 /* self-refresh has much higher latency */
1588 static const int sr_latency_ns = 6000;
Ville Syrjäläefc26112016-10-31 22:37:04 +02001589 const struct drm_display_mode *adjusted_mode =
1590 &enabled->config->base.adjusted_mode;
1591 const struct drm_framebuffer *fb =
1592 enabled->base.primary->state->fb;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001593 int clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -08001594 int htotal = adjusted_mode->crtc_htotal;
Ville Syrjäläefc26112016-10-31 22:37:04 +02001595 int hdisplay = enabled->config->pipe_src_w;
1596 int cpp;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001597 unsigned long line_time_us;
1598 int entries;
1599
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01001600 if (IS_I915GM(dev_priv) || IS_I945GM(dev_priv))
Ville Syrjälä2d1b5052016-07-29 17:57:01 +03001601 cpp = 4;
Ville Syrjäläefc26112016-10-31 22:37:04 +02001602 else
1603 cpp = drm_format_plane_cpp(fb->pixel_format, 0);
Ville Syrjälä2d1b5052016-07-29 17:57:01 +03001604
Ville Syrjälä922044c2014-02-14 14:18:57 +02001605 line_time_us = max(htotal * 1000 / clock, 1);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001606
1607 /* Use ns/us then divide to preserve precision */
1608 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
Ville Syrjäläac484962016-01-20 21:05:26 +02001609 cpp * hdisplay;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001610 entries = DIV_ROUND_UP(entries, wm_info->cacheline_size);
1611 DRM_DEBUG_KMS("self-refresh entries: %d\n", entries);
1612 srwm = wm_info->fifo_size - entries;
1613 if (srwm < 0)
1614 srwm = 1;
1615
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01001616 if (IS_I945G(dev_priv) || IS_I945GM(dev_priv))
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001617 I915_WRITE(FW_BLC_SELF,
1618 FW_BLC_SELF_FIFO_MASK | (srwm & 0xff));
Ville Syrjäläacb91352016-07-29 17:57:02 +03001619 else
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001620 I915_WRITE(FW_BLC_SELF, srwm & 0x3f);
1621 }
1622
1623 DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d, B: %d, C: %d, SR %d\n",
1624 planea_wm, planeb_wm, cwm, srwm);
1625
1626 fwater_lo = ((planeb_wm & 0x3f) << 16) | (planea_wm & 0x3f);
1627 fwater_hi = (cwm & 0x1f);
1628
1629 /* Set request length to 8 cachelines per fetch */
1630 fwater_lo = fwater_lo | (1 << 24) | (1 << 8);
1631 fwater_hi = fwater_hi | (1 << 8);
1632
1633 I915_WRITE(FW_BLC, fwater_lo);
1634 I915_WRITE(FW_BLC2, fwater_hi);
1635
Imre Deak5209b1f2014-07-01 12:36:17 +03001636 if (enabled)
1637 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001638}
1639
Ville Syrjälä432081b2016-10-31 22:37:03 +02001640static void i845_update_wm(struct intel_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001641{
Ville Syrjäläffc7a762016-10-31 22:37:21 +02001642 struct drm_i915_private *dev_priv = to_i915(unused_crtc->base.dev);
Ville Syrjäläefc26112016-10-31 22:37:04 +02001643 struct intel_crtc *crtc;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001644 const struct drm_display_mode *adjusted_mode;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001645 uint32_t fwater_lo;
1646 int planea_wm;
1647
Ville Syrjäläffc7a762016-10-31 22:37:21 +02001648 crtc = single_enabled_crtc(dev_priv);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001649 if (crtc == NULL)
1650 return;
1651
Ville Syrjäläefc26112016-10-31 22:37:04 +02001652 adjusted_mode = &crtc->config->base.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001653 planea_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Daniel Vetterfeb56b92013-12-14 20:38:30 -02001654 &i845_wm_info,
Ville Syrjäläef0f5e92016-10-31 22:37:17 +02001655 dev_priv->display.get_fifo_size(dev_priv, 0),
Chris Wilson5aef6002014-09-03 11:56:07 +01001656 4, pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001657 fwater_lo = I915_READ(FW_BLC) & ~0xfff;
1658 fwater_lo |= (3<<8) | planea_wm;
1659
1660 DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d\n", planea_wm);
1661
1662 I915_WRITE(FW_BLC, fwater_lo);
1663}
1664
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03001665uint32_t ilk_pipe_pixel_rate(const struct intel_crtc_state *pipe_config)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001666{
Chris Wilsonfd4daa92013-08-27 17:04:17 +01001667 uint32_t pixel_rate;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001668
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03001669 pixel_rate = pipe_config->base.adjusted_mode.crtc_clock;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001670
1671 /* We only use IF-ID interlacing. If we ever use PF-ID we'll need to
1672 * adjust the pixel_rate here. */
1673
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03001674 if (pipe_config->pch_pfit.enabled) {
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001675 uint64_t pipe_w, pipe_h, pfit_w, pfit_h;
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03001676 uint32_t pfit_size = pipe_config->pch_pfit.size;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001677
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03001678 pipe_w = pipe_config->pipe_src_w;
1679 pipe_h = pipe_config->pipe_src_h;
1680
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001681 pfit_w = (pfit_size >> 16) & 0xFFFF;
1682 pfit_h = pfit_size & 0xFFFF;
1683 if (pipe_w < pfit_w)
1684 pipe_w = pfit_w;
1685 if (pipe_h < pfit_h)
1686 pipe_h = pfit_h;
1687
Matt Roper15126882015-12-03 11:37:40 -08001688 if (WARN_ON(!pfit_w || !pfit_h))
1689 return pixel_rate;
1690
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001691 pixel_rate = div_u64((uint64_t) pixel_rate * pipe_w * pipe_h,
1692 pfit_w * pfit_h);
1693 }
1694
1695 return pixel_rate;
1696}
1697
Ville Syrjälä37126462013-08-01 16:18:55 +03001698/* latency must be in 0.1us units. */
Ville Syrjäläac484962016-01-20 21:05:26 +02001699static uint32_t ilk_wm_method1(uint32_t pixel_rate, uint8_t cpp, uint32_t latency)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001700{
1701 uint64_t ret;
1702
Ville Syrjälä3312ba62013-08-01 16:18:53 +03001703 if (WARN(latency == 0, "Latency value missing\n"))
1704 return UINT_MAX;
1705
Ville Syrjäläac484962016-01-20 21:05:26 +02001706 ret = (uint64_t) pixel_rate * cpp * latency;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001707 ret = DIV_ROUND_UP_ULL(ret, 64 * 10000) + 2;
1708
1709 return ret;
1710}
1711
Ville Syrjälä37126462013-08-01 16:18:55 +03001712/* latency must be in 0.1us units. */
Ville Syrjälä23297042013-07-05 11:57:17 +03001713static uint32_t ilk_wm_method2(uint32_t pixel_rate, uint32_t pipe_htotal,
Ville Syrjäläac484962016-01-20 21:05:26 +02001714 uint32_t horiz_pixels, uint8_t cpp,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001715 uint32_t latency)
1716{
1717 uint32_t ret;
1718
Ville Syrjälä3312ba62013-08-01 16:18:53 +03001719 if (WARN(latency == 0, "Latency value missing\n"))
1720 return UINT_MAX;
Matt Roper15126882015-12-03 11:37:40 -08001721 if (WARN_ON(!pipe_htotal))
1722 return UINT_MAX;
Ville Syrjälä3312ba62013-08-01 16:18:53 +03001723
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001724 ret = (latency * pixel_rate) / (pipe_htotal * 10000);
Ville Syrjäläac484962016-01-20 21:05:26 +02001725 ret = (ret + 1) * horiz_pixels * cpp;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001726 ret = DIV_ROUND_UP(ret, 64) + 2;
1727 return ret;
1728}
1729
Ville Syrjälä23297042013-07-05 11:57:17 +03001730static uint32_t ilk_wm_fbc(uint32_t pri_val, uint32_t horiz_pixels,
Ville Syrjäläac484962016-01-20 21:05:26 +02001731 uint8_t cpp)
Paulo Zanonicca32e92013-05-31 11:45:06 -03001732{
Matt Roper15126882015-12-03 11:37:40 -08001733 /*
1734 * Neither of these should be possible since this function shouldn't be
1735 * called if the CRTC is off or the plane is invisible. But let's be
1736 * extra paranoid to avoid a potential divide-by-zero if we screw up
1737 * elsewhere in the driver.
1738 */
Ville Syrjäläac484962016-01-20 21:05:26 +02001739 if (WARN_ON(!cpp))
Matt Roper15126882015-12-03 11:37:40 -08001740 return 0;
1741 if (WARN_ON(!horiz_pixels))
1742 return 0;
1743
Ville Syrjäläac484962016-01-20 21:05:26 +02001744 return DIV_ROUND_UP(pri_val * 64, horiz_pixels * cpp) + 2;
Paulo Zanonicca32e92013-05-31 11:45:06 -03001745}
1746
Imre Deak820c1982013-12-17 14:46:36 +02001747struct ilk_wm_maximums {
Paulo Zanonicca32e92013-05-31 11:45:06 -03001748 uint16_t pri;
1749 uint16_t spr;
1750 uint16_t cur;
1751 uint16_t fbc;
1752};
1753
Ville Syrjälä37126462013-08-01 16:18:55 +03001754/*
1755 * For both WM_PIPE and WM_LP.
1756 * mem_value must be in 0.1us units.
1757 */
Matt Roper7221fc32015-09-24 15:53:08 -07001758static uint32_t ilk_compute_pri_wm(const struct intel_crtc_state *cstate,
Matt Roper43d59ed2015-09-24 15:53:07 -07001759 const struct intel_plane_state *pstate,
Paulo Zanonicca32e92013-05-31 11:45:06 -03001760 uint32_t mem_value,
1761 bool is_lp)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001762{
Ville Syrjäläac484962016-01-20 21:05:26 +02001763 int cpp = pstate->base.fb ?
1764 drm_format_plane_cpp(pstate->base.fb->pixel_format, 0) : 0;
Paulo Zanonicca32e92013-05-31 11:45:06 -03001765 uint32_t method1, method2;
1766
Ville Syrjälä936e71e2016-07-26 19:06:59 +03001767 if (!cstate->base.active || !pstate->base.visible)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001768 return 0;
1769
Ville Syrjäläac484962016-01-20 21:05:26 +02001770 method1 = ilk_wm_method1(ilk_pipe_pixel_rate(cstate), cpp, mem_value);
Paulo Zanonicca32e92013-05-31 11:45:06 -03001771
1772 if (!is_lp)
1773 return method1;
1774
Matt Roper7221fc32015-09-24 15:53:08 -07001775 method2 = ilk_wm_method2(ilk_pipe_pixel_rate(cstate),
1776 cstate->base.adjusted_mode.crtc_htotal,
Ville Syrjälä936e71e2016-07-26 19:06:59 +03001777 drm_rect_width(&pstate->base.dst),
Ville Syrjäläac484962016-01-20 21:05:26 +02001778 cpp, mem_value);
Paulo Zanonicca32e92013-05-31 11:45:06 -03001779
1780 return min(method1, method2);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001781}
1782
Ville Syrjälä37126462013-08-01 16:18:55 +03001783/*
1784 * For both WM_PIPE and WM_LP.
1785 * mem_value must be in 0.1us units.
1786 */
Matt Roper7221fc32015-09-24 15:53:08 -07001787static uint32_t ilk_compute_spr_wm(const struct intel_crtc_state *cstate,
Matt Roper43d59ed2015-09-24 15:53:07 -07001788 const struct intel_plane_state *pstate,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001789 uint32_t mem_value)
1790{
Ville Syrjäläac484962016-01-20 21:05:26 +02001791 int cpp = pstate->base.fb ?
1792 drm_format_plane_cpp(pstate->base.fb->pixel_format, 0) : 0;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001793 uint32_t method1, method2;
1794
Ville Syrjälä936e71e2016-07-26 19:06:59 +03001795 if (!cstate->base.active || !pstate->base.visible)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001796 return 0;
1797
Ville Syrjäläac484962016-01-20 21:05:26 +02001798 method1 = ilk_wm_method1(ilk_pipe_pixel_rate(cstate), cpp, mem_value);
Matt Roper7221fc32015-09-24 15:53:08 -07001799 method2 = ilk_wm_method2(ilk_pipe_pixel_rate(cstate),
1800 cstate->base.adjusted_mode.crtc_htotal,
Ville Syrjälä936e71e2016-07-26 19:06:59 +03001801 drm_rect_width(&pstate->base.dst),
Ville Syrjäläac484962016-01-20 21:05:26 +02001802 cpp, mem_value);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001803 return min(method1, method2);
1804}
1805
Ville Syrjälä37126462013-08-01 16:18:55 +03001806/*
1807 * For both WM_PIPE and WM_LP.
1808 * mem_value must be in 0.1us units.
1809 */
Matt Roper7221fc32015-09-24 15:53:08 -07001810static uint32_t ilk_compute_cur_wm(const struct intel_crtc_state *cstate,
Matt Roper43d59ed2015-09-24 15:53:07 -07001811 const struct intel_plane_state *pstate,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001812 uint32_t mem_value)
1813{
Matt Roperb2435692016-02-02 22:06:51 -08001814 /*
1815 * We treat the cursor plane as always-on for the purposes of watermark
1816 * calculation. Until we have two-stage watermark programming merged,
1817 * this is necessary to avoid flickering.
1818 */
1819 int cpp = 4;
Ville Syrjälä936e71e2016-07-26 19:06:59 +03001820 int width = pstate->base.visible ? pstate->base.crtc_w : 64;
Matt Roper43d59ed2015-09-24 15:53:07 -07001821
Matt Roperb2435692016-02-02 22:06:51 -08001822 if (!cstate->base.active)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001823 return 0;
1824
Matt Roper7221fc32015-09-24 15:53:08 -07001825 return ilk_wm_method2(ilk_pipe_pixel_rate(cstate),
1826 cstate->base.adjusted_mode.crtc_htotal,
Matt Roperb2435692016-02-02 22:06:51 -08001827 width, cpp, mem_value);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001828}
1829
Paulo Zanonicca32e92013-05-31 11:45:06 -03001830/* Only for WM_LP. */
Matt Roper7221fc32015-09-24 15:53:08 -07001831static uint32_t ilk_compute_fbc_wm(const struct intel_crtc_state *cstate,
Matt Roper43d59ed2015-09-24 15:53:07 -07001832 const struct intel_plane_state *pstate,
Ville Syrjälä1fda9882013-07-05 11:57:19 +03001833 uint32_t pri_val)
Paulo Zanonicca32e92013-05-31 11:45:06 -03001834{
Ville Syrjäläac484962016-01-20 21:05:26 +02001835 int cpp = pstate->base.fb ?
1836 drm_format_plane_cpp(pstate->base.fb->pixel_format, 0) : 0;
Matt Roper43d59ed2015-09-24 15:53:07 -07001837
Ville Syrjälä936e71e2016-07-26 19:06:59 +03001838 if (!cstate->base.active || !pstate->base.visible)
Paulo Zanonicca32e92013-05-31 11:45:06 -03001839 return 0;
1840
Ville Syrjälä936e71e2016-07-26 19:06:59 +03001841 return ilk_wm_fbc(pri_val, drm_rect_width(&pstate->base.dst), cpp);
Paulo Zanonicca32e92013-05-31 11:45:06 -03001842}
1843
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00001844static unsigned int
1845ilk_display_fifo_size(const struct drm_i915_private *dev_priv)
Ville Syrjälä158ae642013-08-07 13:28:19 +03001846{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00001847 if (INTEL_GEN(dev_priv) >= 8)
Ville Syrjälä416f4722013-11-02 21:07:46 -07001848 return 3072;
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00001849 else if (INTEL_GEN(dev_priv) >= 7)
Ville Syrjälä158ae642013-08-07 13:28:19 +03001850 return 768;
1851 else
1852 return 512;
1853}
1854
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00001855static unsigned int
1856ilk_plane_wm_reg_max(const struct drm_i915_private *dev_priv,
1857 int level, bool is_sprite)
Ville Syrjälä4e975082014-03-07 18:32:11 +02001858{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00001859 if (INTEL_GEN(dev_priv) >= 8)
Ville Syrjälä4e975082014-03-07 18:32:11 +02001860 /* BDW primary/sprite plane watermarks */
1861 return level == 0 ? 255 : 2047;
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00001862 else if (INTEL_GEN(dev_priv) >= 7)
Ville Syrjälä4e975082014-03-07 18:32:11 +02001863 /* IVB/HSW primary/sprite plane watermarks */
1864 return level == 0 ? 127 : 1023;
1865 else if (!is_sprite)
1866 /* ILK/SNB primary plane watermarks */
1867 return level == 0 ? 127 : 511;
1868 else
1869 /* ILK/SNB sprite plane watermarks */
1870 return level == 0 ? 63 : 255;
1871}
1872
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00001873static unsigned int
1874ilk_cursor_wm_reg_max(const struct drm_i915_private *dev_priv, int level)
Ville Syrjälä4e975082014-03-07 18:32:11 +02001875{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00001876 if (INTEL_GEN(dev_priv) >= 7)
Ville Syrjälä4e975082014-03-07 18:32:11 +02001877 return level == 0 ? 63 : 255;
1878 else
1879 return level == 0 ? 31 : 63;
1880}
1881
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00001882static unsigned int ilk_fbc_wm_reg_max(const struct drm_i915_private *dev_priv)
Ville Syrjälä4e975082014-03-07 18:32:11 +02001883{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00001884 if (INTEL_GEN(dev_priv) >= 8)
Ville Syrjälä4e975082014-03-07 18:32:11 +02001885 return 31;
1886 else
1887 return 15;
1888}
1889
Ville Syrjälä158ae642013-08-07 13:28:19 +03001890/* Calculate the maximum primary/sprite plane watermark */
1891static unsigned int ilk_plane_wm_max(const struct drm_device *dev,
1892 int level,
Ville Syrjälä240264f2013-08-07 13:29:12 +03001893 const struct intel_wm_config *config,
Ville Syrjälä158ae642013-08-07 13:28:19 +03001894 enum intel_ddb_partitioning ddb_partitioning,
1895 bool is_sprite)
1896{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00001897 struct drm_i915_private *dev_priv = to_i915(dev);
1898 unsigned int fifo_size = ilk_display_fifo_size(dev_priv);
Ville Syrjälä158ae642013-08-07 13:28:19 +03001899
1900 /* if sprites aren't enabled, sprites get nothing */
Ville Syrjälä240264f2013-08-07 13:29:12 +03001901 if (is_sprite && !config->sprites_enabled)
Ville Syrjälä158ae642013-08-07 13:28:19 +03001902 return 0;
1903
1904 /* HSW allows LP1+ watermarks even with multiple pipes */
Ville Syrjälä240264f2013-08-07 13:29:12 +03001905 if (level == 0 || config->num_pipes_active > 1) {
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00001906 fifo_size /= INTEL_INFO(dev_priv)->num_pipes;
Ville Syrjälä158ae642013-08-07 13:28:19 +03001907
1908 /*
1909 * For some reason the non self refresh
1910 * FIFO size is only half of the self
1911 * refresh FIFO size on ILK/SNB.
1912 */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00001913 if (INTEL_GEN(dev_priv) <= 6)
Ville Syrjälä158ae642013-08-07 13:28:19 +03001914 fifo_size /= 2;
1915 }
1916
Ville Syrjälä240264f2013-08-07 13:29:12 +03001917 if (config->sprites_enabled) {
Ville Syrjälä158ae642013-08-07 13:28:19 +03001918 /* level 0 is always calculated with 1:1 split */
1919 if (level > 0 && ddb_partitioning == INTEL_DDB_PART_5_6) {
1920 if (is_sprite)
1921 fifo_size *= 5;
1922 fifo_size /= 6;
1923 } else {
1924 fifo_size /= 2;
1925 }
1926 }
1927
1928 /* clamp to max that the registers can hold */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00001929 return min(fifo_size, ilk_plane_wm_reg_max(dev_priv, level, is_sprite));
Ville Syrjälä158ae642013-08-07 13:28:19 +03001930}
1931
1932/* Calculate the maximum cursor plane watermark */
1933static unsigned int ilk_cursor_wm_max(const struct drm_device *dev,
Ville Syrjälä240264f2013-08-07 13:29:12 +03001934 int level,
1935 const struct intel_wm_config *config)
Ville Syrjälä158ae642013-08-07 13:28:19 +03001936{
1937 /* HSW LP1+ watermarks w/ multiple pipes */
Ville Syrjälä240264f2013-08-07 13:29:12 +03001938 if (level > 0 && config->num_pipes_active > 1)
Ville Syrjälä158ae642013-08-07 13:28:19 +03001939 return 64;
1940
1941 /* otherwise just report max that registers can hold */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00001942 return ilk_cursor_wm_reg_max(to_i915(dev), level);
Ville Syrjälä158ae642013-08-07 13:28:19 +03001943}
1944
Damien Lespiaud34ff9c2014-01-06 19:17:23 +00001945static void ilk_compute_wm_maximums(const struct drm_device *dev,
Ville Syrjälä34982fe2013-10-09 19:18:09 +03001946 int level,
1947 const struct intel_wm_config *config,
1948 enum intel_ddb_partitioning ddb_partitioning,
Imre Deak820c1982013-12-17 14:46:36 +02001949 struct ilk_wm_maximums *max)
Ville Syrjälä158ae642013-08-07 13:28:19 +03001950{
Ville Syrjälä240264f2013-08-07 13:29:12 +03001951 max->pri = ilk_plane_wm_max(dev, level, config, ddb_partitioning, false);
1952 max->spr = ilk_plane_wm_max(dev, level, config, ddb_partitioning, true);
1953 max->cur = ilk_cursor_wm_max(dev, level, config);
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00001954 max->fbc = ilk_fbc_wm_reg_max(to_i915(dev));
Ville Syrjälä158ae642013-08-07 13:28:19 +03001955}
1956
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00001957static void ilk_compute_wm_reg_maximums(const struct drm_i915_private *dev_priv,
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03001958 int level,
1959 struct ilk_wm_maximums *max)
1960{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00001961 max->pri = ilk_plane_wm_reg_max(dev_priv, level, false);
1962 max->spr = ilk_plane_wm_reg_max(dev_priv, level, true);
1963 max->cur = ilk_cursor_wm_reg_max(dev_priv, level);
1964 max->fbc = ilk_fbc_wm_reg_max(dev_priv);
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03001965}
1966
Ville Syrjäläd9395652013-10-09 19:18:10 +03001967static bool ilk_validate_wm_level(int level,
Imre Deak820c1982013-12-17 14:46:36 +02001968 const struct ilk_wm_maximums *max,
Ville Syrjäläd9395652013-10-09 19:18:10 +03001969 struct intel_wm_level *result)
Ville Syrjäläa9786a12013-08-07 13:24:47 +03001970{
1971 bool ret;
1972
1973 /* already determined to be invalid? */
1974 if (!result->enable)
1975 return false;
1976
1977 result->enable = result->pri_val <= max->pri &&
1978 result->spr_val <= max->spr &&
1979 result->cur_val <= max->cur;
1980
1981 ret = result->enable;
1982
1983 /*
1984 * HACK until we can pre-compute everything,
1985 * and thus fail gracefully if LP0 watermarks
1986 * are exceeded...
1987 */
1988 if (level == 0 && !result->enable) {
1989 if (result->pri_val > max->pri)
1990 DRM_DEBUG_KMS("Primary WM%d too large %u (max %u)\n",
1991 level, result->pri_val, max->pri);
1992 if (result->spr_val > max->spr)
1993 DRM_DEBUG_KMS("Sprite WM%d too large %u (max %u)\n",
1994 level, result->spr_val, max->spr);
1995 if (result->cur_val > max->cur)
1996 DRM_DEBUG_KMS("Cursor WM%d too large %u (max %u)\n",
1997 level, result->cur_val, max->cur);
1998
1999 result->pri_val = min_t(uint32_t, result->pri_val, max->pri);
2000 result->spr_val = min_t(uint32_t, result->spr_val, max->spr);
2001 result->cur_val = min_t(uint32_t, result->cur_val, max->cur);
2002 result->enable = true;
2003 }
2004
Ville Syrjäläa9786a12013-08-07 13:24:47 +03002005 return ret;
2006}
2007
Damien Lespiaud34ff9c2014-01-06 19:17:23 +00002008static void ilk_compute_wm_level(const struct drm_i915_private *dev_priv,
Matt Roper43d59ed2015-09-24 15:53:07 -07002009 const struct intel_crtc *intel_crtc,
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03002010 int level,
Matt Roper7221fc32015-09-24 15:53:08 -07002011 struct intel_crtc_state *cstate,
Matt Roper86c8bbb2015-09-24 15:53:16 -07002012 struct intel_plane_state *pristate,
2013 struct intel_plane_state *sprstate,
2014 struct intel_plane_state *curstate,
Ville Syrjälä1fd527c2013-08-06 22:24:05 +03002015 struct intel_wm_level *result)
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03002016{
2017 uint16_t pri_latency = dev_priv->wm.pri_latency[level];
2018 uint16_t spr_latency = dev_priv->wm.spr_latency[level];
2019 uint16_t cur_latency = dev_priv->wm.cur_latency[level];
2020
2021 /* WM1+ latency values stored in 0.5us units */
2022 if (level > 0) {
2023 pri_latency *= 5;
2024 spr_latency *= 5;
2025 cur_latency *= 5;
2026 }
2027
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002028 if (pristate) {
2029 result->pri_val = ilk_compute_pri_wm(cstate, pristate,
2030 pri_latency, level);
2031 result->fbc_val = ilk_compute_fbc_wm(cstate, pristate, result->pri_val);
2032 }
2033
2034 if (sprstate)
2035 result->spr_val = ilk_compute_spr_wm(cstate, sprstate, spr_latency);
2036
2037 if (curstate)
2038 result->cur_val = ilk_compute_cur_wm(cstate, curstate, cur_latency);
2039
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03002040 result->enable = true;
2041}
2042
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002043static uint32_t
Ville Syrjälä532f7a72016-04-29 17:31:17 +03002044hsw_compute_linetime_wm(const struct intel_crtc_state *cstate)
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002045{
Ville Syrjälä532f7a72016-04-29 17:31:17 +03002046 const struct intel_atomic_state *intel_state =
2047 to_intel_atomic_state(cstate->base.state);
Matt Roperee91a152015-12-03 11:37:39 -08002048 const struct drm_display_mode *adjusted_mode =
2049 &cstate->base.adjusted_mode;
Paulo Zanoni85a02de2013-05-03 17:23:43 -03002050 u32 linetime, ips_linetime;
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002051
Matt Roperee91a152015-12-03 11:37:39 -08002052 if (!cstate->base.active)
2053 return 0;
2054 if (WARN_ON(adjusted_mode->crtc_clock == 0))
2055 return 0;
Ville Syrjälä532f7a72016-04-29 17:31:17 +03002056 if (WARN_ON(intel_state->cdclk == 0))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002057 return 0;
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002058
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002059 /* The WM are computed with base on how long it takes to fill a single
2060 * row at the given clock rate, multiplied by 8.
2061 * */
Ville Syrjälä124abe02015-09-08 13:40:45 +03002062 linetime = DIV_ROUND_CLOSEST(adjusted_mode->crtc_htotal * 1000 * 8,
2063 adjusted_mode->crtc_clock);
2064 ips_linetime = DIV_ROUND_CLOSEST(adjusted_mode->crtc_htotal * 1000 * 8,
Ville Syrjälä532f7a72016-04-29 17:31:17 +03002065 intel_state->cdclk);
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002066
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002067 return PIPE_WM_LINETIME_IPS_LINETIME(ips_linetime) |
2068 PIPE_WM_LINETIME_TIME(linetime);
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002069}
2070
Ville Syrjäläbb726512016-10-31 22:37:24 +02002071static void intel_read_wm_latency(struct drm_i915_private *dev_priv,
2072 uint16_t wm[8])
Ville Syrjälä12b134d2013-07-05 11:57:21 +03002073{
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002074 if (IS_GEN9(dev_priv)) {
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002075 uint32_t val;
Vandana Kannan4f947382014-11-04 17:06:47 +00002076 int ret, i;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002077 int level, max_level = ilk_wm_max_level(dev_priv);
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002078
2079 /* read the first set of memory latencies[0:3] */
2080 val = 0; /* data0 to be programmed to 0 for first set */
2081 mutex_lock(&dev_priv->rps.hw_lock);
2082 ret = sandybridge_pcode_read(dev_priv,
2083 GEN9_PCODE_READ_MEM_LATENCY,
2084 &val);
2085 mutex_unlock(&dev_priv->rps.hw_lock);
2086
2087 if (ret) {
2088 DRM_ERROR("SKL Mailbox read error = %d\n", ret);
2089 return;
2090 }
2091
2092 wm[0] = val & GEN9_MEM_LATENCY_LEVEL_MASK;
2093 wm[1] = (val >> GEN9_MEM_LATENCY_LEVEL_1_5_SHIFT) &
2094 GEN9_MEM_LATENCY_LEVEL_MASK;
2095 wm[2] = (val >> GEN9_MEM_LATENCY_LEVEL_2_6_SHIFT) &
2096 GEN9_MEM_LATENCY_LEVEL_MASK;
2097 wm[3] = (val >> GEN9_MEM_LATENCY_LEVEL_3_7_SHIFT) &
2098 GEN9_MEM_LATENCY_LEVEL_MASK;
2099
2100 /* read the second set of memory latencies[4:7] */
2101 val = 1; /* data0 to be programmed to 1 for second set */
2102 mutex_lock(&dev_priv->rps.hw_lock);
2103 ret = sandybridge_pcode_read(dev_priv,
2104 GEN9_PCODE_READ_MEM_LATENCY,
2105 &val);
2106 mutex_unlock(&dev_priv->rps.hw_lock);
2107 if (ret) {
2108 DRM_ERROR("SKL Mailbox read error = %d\n", ret);
2109 return;
2110 }
2111
2112 wm[4] = val & GEN9_MEM_LATENCY_LEVEL_MASK;
2113 wm[5] = (val >> GEN9_MEM_LATENCY_LEVEL_1_5_SHIFT) &
2114 GEN9_MEM_LATENCY_LEVEL_MASK;
2115 wm[6] = (val >> GEN9_MEM_LATENCY_LEVEL_2_6_SHIFT) &
2116 GEN9_MEM_LATENCY_LEVEL_MASK;
2117 wm[7] = (val >> GEN9_MEM_LATENCY_LEVEL_3_7_SHIFT) &
2118 GEN9_MEM_LATENCY_LEVEL_MASK;
2119
Vandana Kannan367294b2014-11-04 17:06:46 +00002120 /*
Paulo Zanoni0727e402016-09-22 18:00:30 -03002121 * If a level n (n > 1) has a 0us latency, all levels m (m >= n)
2122 * need to be disabled. We make sure to sanitize the values out
2123 * of the punit to satisfy this requirement.
2124 */
2125 for (level = 1; level <= max_level; level++) {
2126 if (wm[level] == 0) {
2127 for (i = level + 1; i <= max_level; i++)
2128 wm[i] = 0;
2129 break;
2130 }
2131 }
2132
2133 /*
Damien Lespiau6f972352015-02-09 19:33:07 +00002134 * WaWmMemoryReadLatency:skl
2135 *
Vandana Kannan367294b2014-11-04 17:06:46 +00002136 * punit doesn't take into account the read latency so we need
Paulo Zanoni0727e402016-09-22 18:00:30 -03002137 * to add 2us to the various latency levels we retrieve from the
2138 * punit when level 0 response data us 0us.
Vandana Kannan367294b2014-11-04 17:06:46 +00002139 */
Paulo Zanoni0727e402016-09-22 18:00:30 -03002140 if (wm[0] == 0) {
2141 wm[0] += 2;
2142 for (level = 1; level <= max_level; level++) {
2143 if (wm[level] == 0)
2144 break;
Vandana Kannan367294b2014-11-04 17:06:46 +00002145 wm[level] += 2;
Vandana Kannan4f947382014-11-04 17:06:47 +00002146 }
Paulo Zanoni0727e402016-09-22 18:00:30 -03002147 }
2148
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002149 } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
Ville Syrjälä12b134d2013-07-05 11:57:21 +03002150 uint64_t sskpd = I915_READ64(MCH_SSKPD);
2151
2152 wm[0] = (sskpd >> 56) & 0xFF;
2153 if (wm[0] == 0)
2154 wm[0] = sskpd & 0xF;
Ville Syrjäläe5d50192013-07-05 11:57:22 +03002155 wm[1] = (sskpd >> 4) & 0xFF;
2156 wm[2] = (sskpd >> 12) & 0xFF;
2157 wm[3] = (sskpd >> 20) & 0x1FF;
2158 wm[4] = (sskpd >> 32) & 0x1FF;
Ville Syrjäläbb726512016-10-31 22:37:24 +02002159 } else if (INTEL_GEN(dev_priv) >= 6) {
Ville Syrjälä63cf9a12013-07-05 11:57:23 +03002160 uint32_t sskpd = I915_READ(MCH_SSKPD);
2161
2162 wm[0] = (sskpd >> SSKPD_WM0_SHIFT) & SSKPD_WM_MASK;
2163 wm[1] = (sskpd >> SSKPD_WM1_SHIFT) & SSKPD_WM_MASK;
2164 wm[2] = (sskpd >> SSKPD_WM2_SHIFT) & SSKPD_WM_MASK;
2165 wm[3] = (sskpd >> SSKPD_WM3_SHIFT) & SSKPD_WM_MASK;
Ville Syrjäläbb726512016-10-31 22:37:24 +02002166 } else if (INTEL_GEN(dev_priv) >= 5) {
Ville Syrjälä3a88d0a2013-08-01 16:18:49 +03002167 uint32_t mltr = I915_READ(MLTR_ILK);
2168
2169 /* ILK primary LP0 latency is 700 ns */
2170 wm[0] = 7;
2171 wm[1] = (mltr >> MLTR_WM1_SHIFT) & ILK_SRLT_MASK;
2172 wm[2] = (mltr >> MLTR_WM2_SHIFT) & ILK_SRLT_MASK;
Ville Syrjälä12b134d2013-07-05 11:57:21 +03002173 }
2174}
2175
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002176static void intel_fixup_spr_wm_latency(struct drm_i915_private *dev_priv,
2177 uint16_t wm[5])
Ville Syrjälä53615a52013-08-01 16:18:50 +03002178{
2179 /* ILK sprite LP0 latency is 1300 ns */
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002180 if (IS_GEN5(dev_priv))
Ville Syrjälä53615a52013-08-01 16:18:50 +03002181 wm[0] = 13;
2182}
2183
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01002184static void intel_fixup_cur_wm_latency(struct drm_i915_private *dev_priv,
2185 uint16_t wm[5])
Ville Syrjälä53615a52013-08-01 16:18:50 +03002186{
2187 /* ILK cursor LP0 latency is 1300 ns */
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01002188 if (IS_GEN5(dev_priv))
Ville Syrjälä53615a52013-08-01 16:18:50 +03002189 wm[0] = 13;
2190
2191 /* WaDoubleCursorLP3Latency:ivb */
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01002192 if (IS_IVYBRIDGE(dev_priv))
Ville Syrjälä53615a52013-08-01 16:18:50 +03002193 wm[3] *= 2;
2194}
2195
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002196int ilk_wm_max_level(const struct drm_i915_private *dev_priv)
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03002197{
2198 /* how many WM levels are we expecting */
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002199 if (INTEL_GEN(dev_priv) >= 9)
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002200 return 7;
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002201 else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03002202 return 4;
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002203 else if (INTEL_GEN(dev_priv) >= 6)
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03002204 return 3;
2205 else
2206 return 2;
2207}
Daniel Vetter7526ed72014-09-29 15:07:19 +02002208
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002209static void intel_print_wm_latency(struct drm_i915_private *dev_priv,
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002210 const char *name,
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002211 const uint16_t wm[8])
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002212{
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002213 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002214
2215 for (level = 0; level <= max_level; level++) {
2216 unsigned int latency = wm[level];
2217
2218 if (latency == 0) {
2219 DRM_ERROR("%s WM%d latency not provided\n",
2220 name, level);
2221 continue;
2222 }
2223
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002224 /*
2225 * - latencies are in us on gen9.
2226 * - before then, WM1+ latency values are in 0.5us units
2227 */
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002228 if (IS_GEN9(dev_priv))
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002229 latency *= 10;
2230 else if (level > 0)
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002231 latency *= 5;
2232
2233 DRM_DEBUG_KMS("%s WM%d latency %u (%u.%u usec)\n",
2234 name, level, wm[level],
2235 latency / 10, latency % 10);
2236 }
2237}
2238
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002239static bool ilk_increase_wm_latency(struct drm_i915_private *dev_priv,
2240 uint16_t wm[5], uint16_t min)
2241{
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002242 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002243
2244 if (wm[0] >= min)
2245 return false;
2246
2247 wm[0] = max(wm[0], min);
2248 for (level = 1; level <= max_level; level++)
2249 wm[level] = max_t(uint16_t, wm[level], DIV_ROUND_UP(min, 5));
2250
2251 return true;
2252}
2253
Ville Syrjäläbb726512016-10-31 22:37:24 +02002254static void snb_wm_latency_quirk(struct drm_i915_private *dev_priv)
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002255{
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002256 bool changed;
2257
2258 /*
2259 * The BIOS provided WM memory latency values are often
2260 * inadequate for high resolution displays. Adjust them.
2261 */
2262 changed = ilk_increase_wm_latency(dev_priv, dev_priv->wm.pri_latency, 12) |
2263 ilk_increase_wm_latency(dev_priv, dev_priv->wm.spr_latency, 12) |
2264 ilk_increase_wm_latency(dev_priv, dev_priv->wm.cur_latency, 12);
2265
2266 if (!changed)
2267 return;
2268
2269 DRM_DEBUG_KMS("WM latency values increased to avoid potential underruns\n");
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002270 intel_print_wm_latency(dev_priv, "Primary", dev_priv->wm.pri_latency);
2271 intel_print_wm_latency(dev_priv, "Sprite", dev_priv->wm.spr_latency);
2272 intel_print_wm_latency(dev_priv, "Cursor", dev_priv->wm.cur_latency);
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002273}
2274
Ville Syrjäläbb726512016-10-31 22:37:24 +02002275static void ilk_setup_wm_latency(struct drm_i915_private *dev_priv)
Ville Syrjälä53615a52013-08-01 16:18:50 +03002276{
Ville Syrjäläbb726512016-10-31 22:37:24 +02002277 intel_read_wm_latency(dev_priv, dev_priv->wm.pri_latency);
Ville Syrjälä53615a52013-08-01 16:18:50 +03002278
2279 memcpy(dev_priv->wm.spr_latency, dev_priv->wm.pri_latency,
2280 sizeof(dev_priv->wm.pri_latency));
2281 memcpy(dev_priv->wm.cur_latency, dev_priv->wm.pri_latency,
2282 sizeof(dev_priv->wm.pri_latency));
2283
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002284 intel_fixup_spr_wm_latency(dev_priv, dev_priv->wm.spr_latency);
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01002285 intel_fixup_cur_wm_latency(dev_priv, dev_priv->wm.cur_latency);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002286
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002287 intel_print_wm_latency(dev_priv, "Primary", dev_priv->wm.pri_latency);
2288 intel_print_wm_latency(dev_priv, "Sprite", dev_priv->wm.spr_latency);
2289 intel_print_wm_latency(dev_priv, "Cursor", dev_priv->wm.cur_latency);
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002290
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002291 if (IS_GEN6(dev_priv))
Ville Syrjäläbb726512016-10-31 22:37:24 +02002292 snb_wm_latency_quirk(dev_priv);
Ville Syrjälä53615a52013-08-01 16:18:50 +03002293}
2294
Ville Syrjäläbb726512016-10-31 22:37:24 +02002295static void skl_setup_wm_latency(struct drm_i915_private *dev_priv)
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002296{
Ville Syrjäläbb726512016-10-31 22:37:24 +02002297 intel_read_wm_latency(dev_priv, dev_priv->wm.skl_latency);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002298 intel_print_wm_latency(dev_priv, "Gen9 Plane", dev_priv->wm.skl_latency);
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002299}
2300
Matt Ropered4a6a72016-02-23 17:20:13 -08002301static bool ilk_validate_pipe_wm(struct drm_device *dev,
2302 struct intel_pipe_wm *pipe_wm)
2303{
2304 /* LP0 watermark maximums depend on this pipe alone */
2305 const struct intel_wm_config config = {
2306 .num_pipes_active = 1,
2307 .sprites_enabled = pipe_wm->sprites_enabled,
2308 .sprites_scaled = pipe_wm->sprites_scaled,
2309 };
2310 struct ilk_wm_maximums max;
2311
2312 /* LP0 watermarks always use 1/2 DDB partitioning */
2313 ilk_compute_wm_maximums(dev, 0, &config, INTEL_DDB_PART_1_2, &max);
2314
2315 /* At least LP0 must be valid */
2316 if (!ilk_validate_wm_level(0, &max, &pipe_wm->wm[0])) {
2317 DRM_DEBUG_KMS("LP0 watermark invalid\n");
2318 return false;
2319 }
2320
2321 return true;
2322}
2323
Matt Roper261a27d2015-10-08 15:28:25 -07002324/* Compute new watermarks for the pipe */
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002325static int ilk_compute_pipe_wm(struct intel_crtc_state *cstate)
Matt Roper261a27d2015-10-08 15:28:25 -07002326{
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002327 struct drm_atomic_state *state = cstate->base.state;
2328 struct intel_crtc *intel_crtc = to_intel_crtc(cstate->base.crtc);
Matt Roper86c8bbb2015-09-24 15:53:16 -07002329 struct intel_pipe_wm *pipe_wm;
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002330 struct drm_device *dev = state->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01002331 const struct drm_i915_private *dev_priv = to_i915(dev);
Matt Roper43d59ed2015-09-24 15:53:07 -07002332 struct intel_plane *intel_plane;
Matt Roper86c8bbb2015-09-24 15:53:16 -07002333 struct intel_plane_state *pristate = NULL;
Matt Roper43d59ed2015-09-24 15:53:07 -07002334 struct intel_plane_state *sprstate = NULL;
Matt Roper86c8bbb2015-09-24 15:53:16 -07002335 struct intel_plane_state *curstate = NULL;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002336 int level, max_level = ilk_wm_max_level(dev_priv), usable_level;
Imre Deak820c1982013-12-17 14:46:36 +02002337 struct ilk_wm_maximums max;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002338
Matt Ropere8f1f022016-05-12 07:05:55 -07002339 pipe_wm = &cstate->wm.ilk.optimal;
Matt Roper86c8bbb2015-09-24 15:53:16 -07002340
Matt Roper43d59ed2015-09-24 15:53:07 -07002341 for_each_intel_plane_on_crtc(dev, intel_crtc, intel_plane) {
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002342 struct intel_plane_state *ps;
2343
2344 ps = intel_atomic_get_existing_plane_state(state,
2345 intel_plane);
2346 if (!ps)
2347 continue;
Matt Roper86c8bbb2015-09-24 15:53:16 -07002348
2349 if (intel_plane->base.type == DRM_PLANE_TYPE_PRIMARY)
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002350 pristate = ps;
Matt Roper86c8bbb2015-09-24 15:53:16 -07002351 else if (intel_plane->base.type == DRM_PLANE_TYPE_OVERLAY)
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002352 sprstate = ps;
Matt Roper86c8bbb2015-09-24 15:53:16 -07002353 else if (intel_plane->base.type == DRM_PLANE_TYPE_CURSOR)
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002354 curstate = ps;
Matt Roper43d59ed2015-09-24 15:53:07 -07002355 }
2356
Matt Ropered4a6a72016-02-23 17:20:13 -08002357 pipe_wm->pipe_enabled = cstate->base.active;
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002358 if (sprstate) {
Ville Syrjälä936e71e2016-07-26 19:06:59 +03002359 pipe_wm->sprites_enabled = sprstate->base.visible;
2360 pipe_wm->sprites_scaled = sprstate->base.visible &&
2361 (drm_rect_width(&sprstate->base.dst) != drm_rect_width(&sprstate->base.src) >> 16 ||
2362 drm_rect_height(&sprstate->base.dst) != drm_rect_height(&sprstate->base.src) >> 16);
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002363 }
2364
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01002365 usable_level = max_level;
2366
Ville Syrjälä7b39a0b2013-12-05 15:51:30 +02002367 /* ILK/SNB: LP2+ watermarks only w/o sprites */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002368 if (INTEL_GEN(dev_priv) <= 6 && pipe_wm->sprites_enabled)
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01002369 usable_level = 1;
Ville Syrjälä7b39a0b2013-12-05 15:51:30 +02002370
2371 /* ILK/SNB/IVB: LP1+ watermarks only w/o scaling */
Matt Ropered4a6a72016-02-23 17:20:13 -08002372 if (pipe_wm->sprites_scaled)
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01002373 usable_level = 0;
Ville Syrjälä7b39a0b2013-12-05 15:51:30 +02002374
Matt Roper86c8bbb2015-09-24 15:53:16 -07002375 ilk_compute_wm_level(dev_priv, intel_crtc, 0, cstate,
Maarten Lankhorst71f0a622016-03-08 10:57:16 +01002376 pristate, sprstate, curstate, &pipe_wm->raw_wm[0]);
2377
2378 memset(&pipe_wm->wm, 0, sizeof(pipe_wm->wm));
2379 pipe_wm->wm[0] = pipe_wm->raw_wm[0];
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002380
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002381 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjälä532f7a72016-04-29 17:31:17 +03002382 pipe_wm->linetime = hsw_compute_linetime_wm(cstate);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002383
Matt Ropered4a6a72016-02-23 17:20:13 -08002384 if (!ilk_validate_pipe_wm(dev, pipe_wm))
Maarten Lankhorst1a426d62016-03-02 12:36:03 +01002385 return -EINVAL;
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002386
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002387 ilk_compute_wm_reg_maximums(dev_priv, 1, &max);
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002388
2389 for (level = 1; level <= max_level; level++) {
Maarten Lankhorst71f0a622016-03-08 10:57:16 +01002390 struct intel_wm_level *wm = &pipe_wm->raw_wm[level];
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002391
Matt Roper86c8bbb2015-09-24 15:53:16 -07002392 ilk_compute_wm_level(dev_priv, intel_crtc, level, cstate,
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01002393 pristate, sprstate, curstate, wm);
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002394
2395 /*
2396 * Disable any watermark level that exceeds the
2397 * register maximums since such watermarks are
2398 * always invalid.
2399 */
Maarten Lankhorst71f0a622016-03-08 10:57:16 +01002400 if (level > usable_level)
2401 continue;
2402
2403 if (ilk_validate_wm_level(level, &max, wm))
2404 pipe_wm->wm[level] = *wm;
2405 else
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01002406 usable_level = level;
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002407 }
2408
Matt Roper86c8bbb2015-09-24 15:53:16 -07002409 return 0;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002410}
2411
2412/*
Matt Ropered4a6a72016-02-23 17:20:13 -08002413 * Build a set of 'intermediate' watermark values that satisfy both the old
2414 * state and the new state. These can be programmed to the hardware
2415 * immediately.
2416 */
2417static int ilk_compute_intermediate_wm(struct drm_device *dev,
2418 struct intel_crtc *intel_crtc,
2419 struct intel_crtc_state *newstate)
2420{
Matt Ropere8f1f022016-05-12 07:05:55 -07002421 struct intel_pipe_wm *a = &newstate->wm.ilk.intermediate;
Matt Ropered4a6a72016-02-23 17:20:13 -08002422 struct intel_pipe_wm *b = &intel_crtc->wm.active.ilk;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002423 int level, max_level = ilk_wm_max_level(to_i915(dev));
Matt Ropered4a6a72016-02-23 17:20:13 -08002424
2425 /*
2426 * Start with the final, target watermarks, then combine with the
2427 * currently active watermarks to get values that are safe both before
2428 * and after the vblank.
2429 */
Matt Ropere8f1f022016-05-12 07:05:55 -07002430 *a = newstate->wm.ilk.optimal;
Matt Ropered4a6a72016-02-23 17:20:13 -08002431 a->pipe_enabled |= b->pipe_enabled;
2432 a->sprites_enabled |= b->sprites_enabled;
2433 a->sprites_scaled |= b->sprites_scaled;
2434
2435 for (level = 0; level <= max_level; level++) {
2436 struct intel_wm_level *a_wm = &a->wm[level];
2437 const struct intel_wm_level *b_wm = &b->wm[level];
2438
2439 a_wm->enable &= b_wm->enable;
2440 a_wm->pri_val = max(a_wm->pri_val, b_wm->pri_val);
2441 a_wm->spr_val = max(a_wm->spr_val, b_wm->spr_val);
2442 a_wm->cur_val = max(a_wm->cur_val, b_wm->cur_val);
2443 a_wm->fbc_val = max(a_wm->fbc_val, b_wm->fbc_val);
2444 }
2445
2446 /*
2447 * We need to make sure that these merged watermark values are
2448 * actually a valid configuration themselves. If they're not,
2449 * there's no safe way to transition from the old state to
2450 * the new state, so we need to fail the atomic transaction.
2451 */
2452 if (!ilk_validate_pipe_wm(dev, a))
2453 return -EINVAL;
2454
2455 /*
2456 * If our intermediate WM are identical to the final WM, then we can
2457 * omit the post-vblank programming; only update if it's different.
2458 */
Matt Ropere8f1f022016-05-12 07:05:55 -07002459 if (memcmp(a, &newstate->wm.ilk.optimal, sizeof(*a)) == 0)
Matt Ropered4a6a72016-02-23 17:20:13 -08002460 newstate->wm.need_postvbl_update = false;
2461
2462 return 0;
2463}
2464
2465/*
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002466 * Merge the watermarks from all active pipes for a specific level.
2467 */
2468static void ilk_merge_wm_level(struct drm_device *dev,
2469 int level,
2470 struct intel_wm_level *ret_wm)
2471{
2472 const struct intel_crtc *intel_crtc;
2473
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002474 ret_wm->enable = true;
2475
Damien Lespiaud3fcc802014-05-13 23:32:22 +01002476 for_each_intel_crtc(dev, intel_crtc) {
Matt Ropered4a6a72016-02-23 17:20:13 -08002477 const struct intel_pipe_wm *active = &intel_crtc->wm.active.ilk;
Ville Syrjäläfe392ef2014-03-07 18:32:10 +02002478 const struct intel_wm_level *wm = &active->wm[level];
2479
2480 if (!active->pipe_enabled)
2481 continue;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002482
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002483 /*
2484 * The watermark values may have been used in the past,
2485 * so we must maintain them in the registers for some
2486 * time even if the level is now disabled.
2487 */
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002488 if (!wm->enable)
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002489 ret_wm->enable = false;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002490
2491 ret_wm->pri_val = max(ret_wm->pri_val, wm->pri_val);
2492 ret_wm->spr_val = max(ret_wm->spr_val, wm->spr_val);
2493 ret_wm->cur_val = max(ret_wm->cur_val, wm->cur_val);
2494 ret_wm->fbc_val = max(ret_wm->fbc_val, wm->fbc_val);
2495 }
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002496}
2497
2498/*
2499 * Merge all low power watermarks for all active pipes.
2500 */
2501static void ilk_wm_merge(struct drm_device *dev,
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02002502 const struct intel_wm_config *config,
Imre Deak820c1982013-12-17 14:46:36 +02002503 const struct ilk_wm_maximums *max,
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002504 struct intel_pipe_wm *merged)
2505{
Chris Wilsonfac5e232016-07-04 11:34:36 +01002506 struct drm_i915_private *dev_priv = to_i915(dev);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002507 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002508 int last_enabled_level = max_level;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002509
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02002510 /* ILK/SNB/IVB: LP1+ watermarks only w/ single pipe */
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01002511 if ((INTEL_GEN(dev_priv) <= 6 || IS_IVYBRIDGE(dev_priv)) &&
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02002512 config->num_pipes_active > 1)
Ville Syrjälä1204d5b2016-04-01 21:53:18 +03002513 last_enabled_level = 0;
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02002514
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02002515 /* ILK: FBC WM must be disabled always */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002516 merged->fbc_wm_enabled = INTEL_GEN(dev_priv) >= 6;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002517
2518 /* merge each WM1+ level */
2519 for (level = 1; level <= max_level; level++) {
2520 struct intel_wm_level *wm = &merged->wm[level];
2521
2522 ilk_merge_wm_level(dev, level, wm);
2523
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002524 if (level > last_enabled_level)
2525 wm->enable = false;
2526 else if (!ilk_validate_wm_level(level, max, wm))
2527 /* make sure all following levels get disabled */
2528 last_enabled_level = level - 1;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002529
2530 /*
2531 * The spec says it is preferred to disable
2532 * FBC WMs instead of disabling a WM level.
2533 */
2534 if (wm->fbc_val > max->fbc) {
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002535 if (wm->enable)
2536 merged->fbc_wm_enabled = false;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002537 wm->fbc_val = 0;
2538 }
2539 }
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02002540
2541 /* ILK: LP2+ must be disabled when FBC WM is disabled but FBC enabled */
2542 /*
2543 * FIXME this is racy. FBC might get enabled later.
2544 * What we should check here is whether FBC can be
2545 * enabled sometime later.
2546 */
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002547 if (IS_GEN5(dev_priv) && !merged->fbc_wm_enabled &&
Paulo Zanoni0e631ad2015-10-14 17:45:36 -03002548 intel_fbc_is_active(dev_priv)) {
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02002549 for (level = 2; level <= max_level; level++) {
2550 struct intel_wm_level *wm = &merged->wm[level];
2551
2552 wm->enable = false;
2553 }
2554 }
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002555}
2556
Ville Syrjäläb380ca32013-10-09 19:18:01 +03002557static int ilk_wm_lp_to_level(int wm_lp, const struct intel_pipe_wm *pipe_wm)
2558{
2559 /* LP1,LP2,LP3 levels are either 1,2,3 or 1,3,4 */
2560 return wm_lp + (wm_lp >= 2 && pipe_wm->wm[4].enable);
2561}
2562
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02002563/* The value we need to program into the WM_LPx latency field */
2564static unsigned int ilk_wm_lp_latency(struct drm_device *dev, int level)
2565{
Chris Wilsonfac5e232016-07-04 11:34:36 +01002566 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02002567
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002568 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02002569 return 2 * level;
2570 else
2571 return dev_priv->wm.pri_latency[level];
2572}
2573
Imre Deak820c1982013-12-17 14:46:36 +02002574static void ilk_compute_wm_results(struct drm_device *dev,
Ville Syrjälä0362c782013-10-09 19:17:57 +03002575 const struct intel_pipe_wm *merged,
Ville Syrjälä609cede2013-10-09 19:18:03 +03002576 enum intel_ddb_partitioning partitioning,
Imre Deak820c1982013-12-17 14:46:36 +02002577 struct ilk_wm_values *results)
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002578{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002579 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002580 struct intel_crtc *intel_crtc;
2581 int level, wm_lp;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002582
Ville Syrjälä0362c782013-10-09 19:17:57 +03002583 results->enable_fbc_wm = merged->fbc_wm_enabled;
Ville Syrjälä609cede2013-10-09 19:18:03 +03002584 results->partitioning = partitioning;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002585
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002586 /* LP1+ register values */
Paulo Zanonicca32e92013-05-31 11:45:06 -03002587 for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
Ville Syrjälä1fd527c2013-08-06 22:24:05 +03002588 const struct intel_wm_level *r;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002589
Ville Syrjäläb380ca32013-10-09 19:18:01 +03002590 level = ilk_wm_lp_to_level(wm_lp, merged);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002591
Ville Syrjälä0362c782013-10-09 19:17:57 +03002592 r = &merged->wm[level];
Paulo Zanonicca32e92013-05-31 11:45:06 -03002593
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002594 /*
2595 * Maintain the watermark values even if the level is
2596 * disabled. Doing otherwise could cause underruns.
2597 */
2598 results->wm_lp[wm_lp - 1] =
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02002599 (ilk_wm_lp_latency(dev, level) << WM1_LP_LATENCY_SHIFT) |
Ville Syrjälä416f4722013-11-02 21:07:46 -07002600 (r->pri_val << WM1_LP_SR_SHIFT) |
2601 r->cur_val;
2602
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002603 if (r->enable)
2604 results->wm_lp[wm_lp - 1] |= WM1_LP_SR_EN;
2605
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002606 if (INTEL_GEN(dev_priv) >= 8)
Ville Syrjälä416f4722013-11-02 21:07:46 -07002607 results->wm_lp[wm_lp - 1] |=
2608 r->fbc_val << WM1_LP_FBC_SHIFT_BDW;
2609 else
2610 results->wm_lp[wm_lp - 1] |=
2611 r->fbc_val << WM1_LP_FBC_SHIFT;
2612
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002613 /*
2614 * Always set WM1S_LP_EN when spr_val != 0, even if the
2615 * level is disabled. Doing otherwise could cause underruns.
2616 */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002617 if (INTEL_GEN(dev_priv) <= 6 && r->spr_val) {
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02002618 WARN_ON(wm_lp != 1);
2619 results->wm_lp_spr[wm_lp - 1] = WM1S_LP_EN | r->spr_val;
2620 } else
2621 results->wm_lp_spr[wm_lp - 1] = r->spr_val;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002622 }
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002623
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002624 /* LP0 register values */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01002625 for_each_intel_crtc(dev, intel_crtc) {
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002626 enum pipe pipe = intel_crtc->pipe;
Matt Ropered4a6a72016-02-23 17:20:13 -08002627 const struct intel_wm_level *r =
2628 &intel_crtc->wm.active.ilk.wm[0];
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002629
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002630 if (WARN_ON(!r->enable))
2631 continue;
2632
Matt Ropered4a6a72016-02-23 17:20:13 -08002633 results->wm_linetime[pipe] = intel_crtc->wm.active.ilk.linetime;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002634
2635 results->wm_pipe[pipe] =
2636 (r->pri_val << WM0_PIPE_PLANE_SHIFT) |
2637 (r->spr_val << WM0_PIPE_SPRITE_SHIFT) |
2638 r->cur_val;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002639 }
2640}
2641
Paulo Zanoni861f3382013-05-31 10:19:21 -03002642/* Find the result with the highest level enabled. Check for enable_fbc_wm in
2643 * case both are at the same level. Prefer r1 in case they're the same. */
Imre Deak820c1982013-12-17 14:46:36 +02002644static struct intel_pipe_wm *ilk_find_best_result(struct drm_device *dev,
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002645 struct intel_pipe_wm *r1,
2646 struct intel_pipe_wm *r2)
Paulo Zanoni861f3382013-05-31 10:19:21 -03002647{
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002648 int level, max_level = ilk_wm_max_level(to_i915(dev));
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002649 int level1 = 0, level2 = 0;
Paulo Zanoni861f3382013-05-31 10:19:21 -03002650
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002651 for (level = 1; level <= max_level; level++) {
2652 if (r1->wm[level].enable)
2653 level1 = level;
2654 if (r2->wm[level].enable)
2655 level2 = level;
Paulo Zanoni861f3382013-05-31 10:19:21 -03002656 }
2657
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002658 if (level1 == level2) {
2659 if (r2->fbc_wm_enabled && !r1->fbc_wm_enabled)
Paulo Zanoni861f3382013-05-31 10:19:21 -03002660 return r2;
2661 else
2662 return r1;
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002663 } else if (level1 > level2) {
Paulo Zanoni861f3382013-05-31 10:19:21 -03002664 return r1;
2665 } else {
2666 return r2;
2667 }
2668}
2669
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002670/* dirty bits used to track which watermarks need changes */
2671#define WM_DIRTY_PIPE(pipe) (1 << (pipe))
2672#define WM_DIRTY_LINETIME(pipe) (1 << (8 + (pipe)))
2673#define WM_DIRTY_LP(wm_lp) (1 << (15 + (wm_lp)))
2674#define WM_DIRTY_LP_ALL (WM_DIRTY_LP(1) | WM_DIRTY_LP(2) | WM_DIRTY_LP(3))
2675#define WM_DIRTY_FBC (1 << 24)
2676#define WM_DIRTY_DDB (1 << 25)
2677
Damien Lespiau055e3932014-08-18 13:49:10 +01002678static unsigned int ilk_compute_wm_dirty(struct drm_i915_private *dev_priv,
Imre Deak820c1982013-12-17 14:46:36 +02002679 const struct ilk_wm_values *old,
2680 const struct ilk_wm_values *new)
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002681{
2682 unsigned int dirty = 0;
2683 enum pipe pipe;
2684 int wm_lp;
2685
Damien Lespiau055e3932014-08-18 13:49:10 +01002686 for_each_pipe(dev_priv, pipe) {
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002687 if (old->wm_linetime[pipe] != new->wm_linetime[pipe]) {
2688 dirty |= WM_DIRTY_LINETIME(pipe);
2689 /* Must disable LP1+ watermarks too */
2690 dirty |= WM_DIRTY_LP_ALL;
2691 }
2692
2693 if (old->wm_pipe[pipe] != new->wm_pipe[pipe]) {
2694 dirty |= WM_DIRTY_PIPE(pipe);
2695 /* Must disable LP1+ watermarks too */
2696 dirty |= WM_DIRTY_LP_ALL;
2697 }
2698 }
2699
2700 if (old->enable_fbc_wm != new->enable_fbc_wm) {
2701 dirty |= WM_DIRTY_FBC;
2702 /* Must disable LP1+ watermarks too */
2703 dirty |= WM_DIRTY_LP_ALL;
2704 }
2705
2706 if (old->partitioning != new->partitioning) {
2707 dirty |= WM_DIRTY_DDB;
2708 /* Must disable LP1+ watermarks too */
2709 dirty |= WM_DIRTY_LP_ALL;
2710 }
2711
2712 /* LP1+ watermarks already deemed dirty, no need to continue */
2713 if (dirty & WM_DIRTY_LP_ALL)
2714 return dirty;
2715
2716 /* Find the lowest numbered LP1+ watermark in need of an update... */
2717 for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
2718 if (old->wm_lp[wm_lp - 1] != new->wm_lp[wm_lp - 1] ||
2719 old->wm_lp_spr[wm_lp - 1] != new->wm_lp_spr[wm_lp - 1])
2720 break;
2721 }
2722
2723 /* ...and mark it and all higher numbered LP1+ watermarks as dirty */
2724 for (; wm_lp <= 3; wm_lp++)
2725 dirty |= WM_DIRTY_LP(wm_lp);
2726
2727 return dirty;
2728}
2729
Ville Syrjälä8553c182013-12-05 15:51:39 +02002730static bool _ilk_disable_lp_wm(struct drm_i915_private *dev_priv,
2731 unsigned int dirty)
2732{
Imre Deak820c1982013-12-17 14:46:36 +02002733 struct ilk_wm_values *previous = &dev_priv->wm.hw;
Ville Syrjälä8553c182013-12-05 15:51:39 +02002734 bool changed = false;
2735
2736 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] & WM1_LP_SR_EN) {
2737 previous->wm_lp[2] &= ~WM1_LP_SR_EN;
2738 I915_WRITE(WM3_LP_ILK, previous->wm_lp[2]);
2739 changed = true;
2740 }
2741 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] & WM1_LP_SR_EN) {
2742 previous->wm_lp[1] &= ~WM1_LP_SR_EN;
2743 I915_WRITE(WM2_LP_ILK, previous->wm_lp[1]);
2744 changed = true;
2745 }
2746 if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] & WM1_LP_SR_EN) {
2747 previous->wm_lp[0] &= ~WM1_LP_SR_EN;
2748 I915_WRITE(WM1_LP_ILK, previous->wm_lp[0]);
2749 changed = true;
2750 }
2751
2752 /*
2753 * Don't touch WM1S_LP_EN here.
2754 * Doing so could cause underruns.
2755 */
2756
2757 return changed;
2758}
2759
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002760/*
2761 * The spec says we shouldn't write when we don't need, because every write
2762 * causes WMs to be re-evaluated, expending some power.
2763 */
Imre Deak820c1982013-12-17 14:46:36 +02002764static void ilk_write_wm_values(struct drm_i915_private *dev_priv,
2765 struct ilk_wm_values *results)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002766{
Imre Deak820c1982013-12-17 14:46:36 +02002767 struct ilk_wm_values *previous = &dev_priv->wm.hw;
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002768 unsigned int dirty;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002769 uint32_t val;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002770
Damien Lespiau055e3932014-08-18 13:49:10 +01002771 dirty = ilk_compute_wm_dirty(dev_priv, previous, results);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002772 if (!dirty)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002773 return;
2774
Ville Syrjälä8553c182013-12-05 15:51:39 +02002775 _ilk_disable_lp_wm(dev_priv, dirty);
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02002776
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002777 if (dirty & WM_DIRTY_PIPE(PIPE_A))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002778 I915_WRITE(WM0_PIPEA_ILK, results->wm_pipe[0]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002779 if (dirty & WM_DIRTY_PIPE(PIPE_B))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002780 I915_WRITE(WM0_PIPEB_ILK, results->wm_pipe[1]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002781 if (dirty & WM_DIRTY_PIPE(PIPE_C))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002782 I915_WRITE(WM0_PIPEC_IVB, results->wm_pipe[2]);
2783
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002784 if (dirty & WM_DIRTY_LINETIME(PIPE_A))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002785 I915_WRITE(PIPE_WM_LINETIME(PIPE_A), results->wm_linetime[0]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002786 if (dirty & WM_DIRTY_LINETIME(PIPE_B))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002787 I915_WRITE(PIPE_WM_LINETIME(PIPE_B), results->wm_linetime[1]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002788 if (dirty & WM_DIRTY_LINETIME(PIPE_C))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002789 I915_WRITE(PIPE_WM_LINETIME(PIPE_C), results->wm_linetime[2]);
2790
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002791 if (dirty & WM_DIRTY_DDB) {
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002792 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
Ville Syrjäläac9545f2013-12-05 15:51:28 +02002793 val = I915_READ(WM_MISC);
2794 if (results->partitioning == INTEL_DDB_PART_1_2)
2795 val &= ~WM_MISC_DATA_PARTITION_5_6;
2796 else
2797 val |= WM_MISC_DATA_PARTITION_5_6;
2798 I915_WRITE(WM_MISC, val);
2799 } else {
2800 val = I915_READ(DISP_ARB_CTL2);
2801 if (results->partitioning == INTEL_DDB_PART_1_2)
2802 val &= ~DISP_DATA_PARTITION_5_6;
2803 else
2804 val |= DISP_DATA_PARTITION_5_6;
2805 I915_WRITE(DISP_ARB_CTL2, val);
2806 }
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002807 }
2808
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002809 if (dirty & WM_DIRTY_FBC) {
Paulo Zanonicca32e92013-05-31 11:45:06 -03002810 val = I915_READ(DISP_ARB_CTL);
2811 if (results->enable_fbc_wm)
2812 val &= ~DISP_FBC_WM_DIS;
2813 else
2814 val |= DISP_FBC_WM_DIS;
2815 I915_WRITE(DISP_ARB_CTL, val);
2816 }
2817
Imre Deak954911e2013-12-17 14:46:34 +02002818 if (dirty & WM_DIRTY_LP(1) &&
2819 previous->wm_lp_spr[0] != results->wm_lp_spr[0])
2820 I915_WRITE(WM1S_LP_ILK, results->wm_lp_spr[0]);
2821
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002822 if (INTEL_GEN(dev_priv) >= 7) {
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02002823 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp_spr[1] != results->wm_lp_spr[1])
2824 I915_WRITE(WM2S_LP_IVB, results->wm_lp_spr[1]);
2825 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp_spr[2] != results->wm_lp_spr[2])
2826 I915_WRITE(WM3S_LP_IVB, results->wm_lp_spr[2]);
2827 }
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002828
Ville Syrjäläfacd619b2013-12-05 15:51:33 +02002829 if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] != results->wm_lp[0])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002830 I915_WRITE(WM1_LP_ILK, results->wm_lp[0]);
Ville Syrjäläfacd619b2013-12-05 15:51:33 +02002831 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] != results->wm_lp[1])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002832 I915_WRITE(WM2_LP_ILK, results->wm_lp[1]);
Ville Syrjäläfacd619b2013-12-05 15:51:33 +02002833 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] != results->wm_lp[2])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002834 I915_WRITE(WM3_LP_ILK, results->wm_lp[2]);
Ville Syrjälä609cede2013-10-09 19:18:03 +03002835
2836 dev_priv->wm.hw = *results;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002837}
2838
Matt Ropered4a6a72016-02-23 17:20:13 -08002839bool ilk_disable_lp_wm(struct drm_device *dev)
Ville Syrjälä8553c182013-12-05 15:51:39 +02002840{
Chris Wilsonfac5e232016-07-04 11:34:36 +01002841 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä8553c182013-12-05 15:51:39 +02002842
2843 return _ilk_disable_lp_wm(dev_priv, WM_DIRTY_LP_ALL);
2844}
2845
Lyude656d1b82016-08-17 15:55:54 -04002846#define SKL_SAGV_BLOCK_TIME 30 /* µs */
Damien Lespiaub9cec072014-11-04 17:06:43 +00002847
Matt Roper024c9042015-09-24 15:53:11 -07002848/*
Paulo Zanoniee3d5322016-10-11 15:25:38 -03002849 * FIXME: We still don't have the proper code detect if we need to apply the WA,
2850 * so assume we'll always need it in order to avoid underruns.
2851 */
2852static bool skl_needs_memory_bw_wa(struct intel_atomic_state *state)
2853{
2854 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
2855
2856 if (IS_SKYLAKE(dev_priv) || IS_BROXTON(dev_priv) ||
2857 IS_KABYLAKE(dev_priv))
2858 return true;
2859
2860 return false;
2861}
2862
Paulo Zanoni56feca92016-09-22 18:00:28 -03002863static bool
2864intel_has_sagv(struct drm_i915_private *dev_priv)
2865{
Paulo Zanoni6e3100e2016-09-22 18:00:29 -03002866 if (IS_KABYLAKE(dev_priv))
2867 return true;
2868
2869 if (IS_SKYLAKE(dev_priv) &&
2870 dev_priv->sagv_status != I915_SAGV_NOT_CONTROLLED)
2871 return true;
2872
2873 return false;
Paulo Zanoni56feca92016-09-22 18:00:28 -03002874}
2875
Lyude656d1b82016-08-17 15:55:54 -04002876/*
2877 * SAGV dynamically adjusts the system agent voltage and clock frequencies
2878 * depending on power and performance requirements. The display engine access
2879 * to system memory is blocked during the adjustment time. Because of the
2880 * blocking time, having this enabled can cause full system hangs and/or pipe
2881 * underruns if we don't meet all of the following requirements:
2882 *
2883 * - <= 1 pipe enabled
2884 * - All planes can enable watermarks for latencies >= SAGV engine block time
2885 * - We're not using an interlaced display configuration
2886 */
2887int
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03002888intel_enable_sagv(struct drm_i915_private *dev_priv)
Lyude656d1b82016-08-17 15:55:54 -04002889{
2890 int ret;
2891
Paulo Zanoni56feca92016-09-22 18:00:28 -03002892 if (!intel_has_sagv(dev_priv))
2893 return 0;
2894
2895 if (dev_priv->sagv_status == I915_SAGV_ENABLED)
Lyude656d1b82016-08-17 15:55:54 -04002896 return 0;
2897
2898 DRM_DEBUG_KMS("Enabling the SAGV\n");
2899 mutex_lock(&dev_priv->rps.hw_lock);
2900
2901 ret = sandybridge_pcode_write(dev_priv, GEN9_PCODE_SAGV_CONTROL,
2902 GEN9_SAGV_ENABLE);
2903
2904 /* We don't need to wait for the SAGV when enabling */
2905 mutex_unlock(&dev_priv->rps.hw_lock);
2906
2907 /*
2908 * Some skl systems, pre-release machines in particular,
2909 * don't actually have an SAGV.
2910 */
Paulo Zanoni6e3100e2016-09-22 18:00:29 -03002911 if (IS_SKYLAKE(dev_priv) && ret == -ENXIO) {
Lyude656d1b82016-08-17 15:55:54 -04002912 DRM_DEBUG_DRIVER("No SAGV found on system, ignoring\n");
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03002913 dev_priv->sagv_status = I915_SAGV_NOT_CONTROLLED;
Lyude656d1b82016-08-17 15:55:54 -04002914 return 0;
2915 } else if (ret < 0) {
2916 DRM_ERROR("Failed to enable the SAGV\n");
2917 return ret;
2918 }
2919
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03002920 dev_priv->sagv_status = I915_SAGV_ENABLED;
Lyude656d1b82016-08-17 15:55:54 -04002921 return 0;
2922}
2923
2924static int
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03002925intel_do_sagv_disable(struct drm_i915_private *dev_priv)
Lyude656d1b82016-08-17 15:55:54 -04002926{
2927 int ret;
2928 uint32_t temp = GEN9_SAGV_DISABLE;
2929
2930 ret = sandybridge_pcode_read(dev_priv, GEN9_PCODE_SAGV_CONTROL,
2931 &temp);
2932 if (ret)
2933 return ret;
2934 else
2935 return temp & GEN9_SAGV_IS_DISABLED;
2936}
2937
2938int
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03002939intel_disable_sagv(struct drm_i915_private *dev_priv)
Lyude656d1b82016-08-17 15:55:54 -04002940{
2941 int ret, result;
2942
Paulo Zanoni56feca92016-09-22 18:00:28 -03002943 if (!intel_has_sagv(dev_priv))
2944 return 0;
2945
2946 if (dev_priv->sagv_status == I915_SAGV_DISABLED)
Lyude656d1b82016-08-17 15:55:54 -04002947 return 0;
2948
2949 DRM_DEBUG_KMS("Disabling the SAGV\n");
2950 mutex_lock(&dev_priv->rps.hw_lock);
2951
2952 /* bspec says to keep retrying for at least 1 ms */
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03002953 ret = wait_for(result = intel_do_sagv_disable(dev_priv), 1);
Lyude656d1b82016-08-17 15:55:54 -04002954 mutex_unlock(&dev_priv->rps.hw_lock);
2955
2956 if (ret == -ETIMEDOUT) {
2957 DRM_ERROR("Request to disable SAGV timed out\n");
2958 return -ETIMEDOUT;
2959 }
2960
2961 /*
2962 * Some skl systems, pre-release machines in particular,
2963 * don't actually have an SAGV.
2964 */
Paulo Zanoni6e3100e2016-09-22 18:00:29 -03002965 if (IS_SKYLAKE(dev_priv) && result == -ENXIO) {
Lyude656d1b82016-08-17 15:55:54 -04002966 DRM_DEBUG_DRIVER("No SAGV found on system, ignoring\n");
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03002967 dev_priv->sagv_status = I915_SAGV_NOT_CONTROLLED;
Lyude656d1b82016-08-17 15:55:54 -04002968 return 0;
2969 } else if (result < 0) {
2970 DRM_ERROR("Failed to disable the SAGV\n");
2971 return result;
2972 }
2973
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03002974 dev_priv->sagv_status = I915_SAGV_DISABLED;
Lyude656d1b82016-08-17 15:55:54 -04002975 return 0;
2976}
2977
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03002978bool intel_can_enable_sagv(struct drm_atomic_state *state)
Lyude656d1b82016-08-17 15:55:54 -04002979{
2980 struct drm_device *dev = state->dev;
2981 struct drm_i915_private *dev_priv = to_i915(dev);
2982 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Paulo Zanoniee3d5322016-10-11 15:25:38 -03002983 struct intel_crtc *crtc;
2984 struct intel_plane *plane;
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02002985 struct intel_crtc_state *cstate;
Lyude656d1b82016-08-17 15:55:54 -04002986 enum pipe pipe;
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02002987 int level, latency;
Lyude656d1b82016-08-17 15:55:54 -04002988
Paulo Zanoni56feca92016-09-22 18:00:28 -03002989 if (!intel_has_sagv(dev_priv))
2990 return false;
2991
Lyude656d1b82016-08-17 15:55:54 -04002992 /*
2993 * SKL workaround: bspec recommends we disable the SAGV when we have
2994 * more then one pipe enabled
2995 *
2996 * If there are no active CRTCs, no additional checks need be performed
2997 */
2998 if (hweight32(intel_state->active_crtcs) == 0)
2999 return true;
3000 else if (hweight32(intel_state->active_crtcs) > 1)
3001 return false;
3002
3003 /* Since we're now guaranteed to only have one active CRTC... */
3004 pipe = ffs(intel_state->active_crtcs) - 1;
Ville Syrjälä98187832016-10-31 22:37:10 +02003005 crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003006 cstate = to_intel_crtc_state(crtc->base.state);
Lyude656d1b82016-08-17 15:55:54 -04003007
Paulo Zanonic89cadd2016-10-10 17:30:59 -03003008 if (crtc->base.state->adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Lyude656d1b82016-08-17 15:55:54 -04003009 return false;
3010
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003011 for_each_intel_plane_on_crtc(dev, crtc, plane) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003012 struct skl_plane_wm *wm =
3013 &cstate->wm.skl.optimal.planes[plane->id];
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003014
Lyude656d1b82016-08-17 15:55:54 -04003015 /* Skip this plane if it's not enabled */
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003016 if (!wm->wm[0].plane_en)
Lyude656d1b82016-08-17 15:55:54 -04003017 continue;
3018
3019 /* Find the highest enabled wm level for this plane */
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003020 for (level = ilk_wm_max_level(dev_priv);
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003021 !wm->wm[level].plane_en; --level)
Lyude656d1b82016-08-17 15:55:54 -04003022 { }
3023
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003024 latency = dev_priv->wm.skl_latency[level];
3025
3026 if (skl_needs_memory_bw_wa(intel_state) &&
Ville Syrjäläbae781b2016-11-16 13:33:16 +02003027 plane->base.state->fb->modifier ==
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003028 I915_FORMAT_MOD_X_TILED)
3029 latency += 15;
3030
Lyude656d1b82016-08-17 15:55:54 -04003031 /*
3032 * If any of the planes on this pipe don't enable wm levels
3033 * that incur memory latencies higher then 30µs we can't enable
3034 * the SAGV
3035 */
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003036 if (latency < SKL_SAGV_BLOCK_TIME)
Lyude656d1b82016-08-17 15:55:54 -04003037 return false;
3038 }
3039
3040 return true;
3041}
3042
Damien Lespiaub9cec072014-11-04 17:06:43 +00003043static void
3044skl_ddb_get_pipe_allocation_limits(struct drm_device *dev,
Matt Roper024c9042015-09-24 15:53:11 -07003045 const struct intel_crtc_state *cstate,
Matt Roperc107acf2016-05-12 07:06:01 -07003046 struct skl_ddb_entry *alloc, /* out */
3047 int *num_active /* out */)
Damien Lespiaub9cec072014-11-04 17:06:43 +00003048{
Matt Roperc107acf2016-05-12 07:06:01 -07003049 struct drm_atomic_state *state = cstate->base.state;
3050 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
3051 struct drm_i915_private *dev_priv = to_i915(dev);
Matt Roper024c9042015-09-24 15:53:11 -07003052 struct drm_crtc *for_crtc = cstate->base.crtc;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003053 unsigned int pipe_size, ddb_size;
3054 int nth_active_pipe;
Matt Roperc107acf2016-05-12 07:06:01 -07003055
Matt Ropera6d3460e2016-05-12 07:06:04 -07003056 if (WARN_ON(!state) || !cstate->base.active) {
Damien Lespiaub9cec072014-11-04 17:06:43 +00003057 alloc->start = 0;
3058 alloc->end = 0;
Matt Ropera6d3460e2016-05-12 07:06:04 -07003059 *num_active = hweight32(dev_priv->active_crtcs);
Damien Lespiaub9cec072014-11-04 17:06:43 +00003060 return;
3061 }
3062
Matt Ropera6d3460e2016-05-12 07:06:04 -07003063 if (intel_state->active_pipe_changes)
3064 *num_active = hweight32(intel_state->active_crtcs);
3065 else
3066 *num_active = hweight32(dev_priv->active_crtcs);
3067
Deepak M6f3fff62016-09-15 15:01:10 +05303068 ddb_size = INTEL_INFO(dev_priv)->ddb_size;
3069 WARN_ON(ddb_size == 0);
Damien Lespiaub9cec072014-11-04 17:06:43 +00003070
3071 ddb_size -= 4; /* 4 blocks for bypass path allocation */
3072
Matt Roperc107acf2016-05-12 07:06:01 -07003073 /*
Matt Ropera6d3460e2016-05-12 07:06:04 -07003074 * If the state doesn't change the active CRTC's, then there's
3075 * no need to recalculate; the existing pipe allocation limits
3076 * should remain unchanged. Note that we're safe from racing
3077 * commits since any racing commit that changes the active CRTC
3078 * list would need to grab _all_ crtc locks, including the one
3079 * we currently hold.
Matt Roperc107acf2016-05-12 07:06:01 -07003080 */
Matt Ropera6d3460e2016-05-12 07:06:04 -07003081 if (!intel_state->active_pipe_changes) {
Maarten Lankhorst512b5522016-11-08 13:55:34 +01003082 /*
3083 * alloc may be cleared by clear_intel_crtc_state,
3084 * copy from old state to be sure
3085 */
3086 *alloc = to_intel_crtc_state(for_crtc->state)->wm.skl.ddb;
Matt Ropera6d3460e2016-05-12 07:06:04 -07003087 return;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003088 }
Matt Ropera6d3460e2016-05-12 07:06:04 -07003089
3090 nth_active_pipe = hweight32(intel_state->active_crtcs &
3091 (drm_crtc_mask(for_crtc) - 1));
3092 pipe_size = ddb_size / hweight32(intel_state->active_crtcs);
3093 alloc->start = nth_active_pipe * ddb_size / *num_active;
3094 alloc->end = alloc->start + pipe_size;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003095}
3096
Matt Roperc107acf2016-05-12 07:06:01 -07003097static unsigned int skl_cursor_allocation(int num_active)
Damien Lespiaub9cec072014-11-04 17:06:43 +00003098{
Matt Roperc107acf2016-05-12 07:06:01 -07003099 if (num_active == 1)
Damien Lespiaub9cec072014-11-04 17:06:43 +00003100 return 32;
3101
3102 return 8;
3103}
3104
Damien Lespiaua269c582014-11-04 17:06:49 +00003105static void skl_ddb_entry_init_from_hw(struct skl_ddb_entry *entry, u32 reg)
3106{
3107 entry->start = reg & 0x3ff;
3108 entry->end = (reg >> 16) & 0x3ff;
Damien Lespiau16160e32014-11-04 17:06:53 +00003109 if (entry->end)
3110 entry->end += 1;
Damien Lespiaua269c582014-11-04 17:06:49 +00003111}
3112
Damien Lespiau08db6652014-11-04 17:06:52 +00003113void skl_ddb_get_hw_state(struct drm_i915_private *dev_priv,
3114 struct skl_ddb_allocation *ddb /* out */)
Damien Lespiaua269c582014-11-04 17:06:49 +00003115{
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003116 struct intel_crtc *crtc;
Damien Lespiaua269c582014-11-04 17:06:49 +00003117
Maarten Lankhorstb10f1b22015-10-22 13:56:34 +02003118 memset(ddb, 0, sizeof(*ddb));
3119
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003120 for_each_intel_crtc(&dev_priv->drm, crtc) {
Imre Deak4d800032016-02-17 16:31:29 +02003121 enum intel_display_power_domain power_domain;
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003122 enum plane_id plane_id;
3123 enum pipe pipe = crtc->pipe;
Imre Deak4d800032016-02-17 16:31:29 +02003124
3125 power_domain = POWER_DOMAIN_PIPE(pipe);
3126 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
Maarten Lankhorstb10f1b22015-10-22 13:56:34 +02003127 continue;
3128
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003129 for_each_plane_id_on_crtc(crtc, plane_id) {
3130 u32 val;
Damien Lespiaua269c582014-11-04 17:06:49 +00003131
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003132 if (plane_id != PLANE_CURSOR)
3133 val = I915_READ(PLANE_BUF_CFG(pipe, plane_id));
3134 else
3135 val = I915_READ(CUR_BUF_CFG(pipe));
3136
3137 skl_ddb_entry_init_from_hw(&ddb->plane[pipe][plane_id], val);
3138 }
Imre Deak4d800032016-02-17 16:31:29 +02003139
3140 intel_display_power_put(dev_priv, power_domain);
Damien Lespiaua269c582014-11-04 17:06:49 +00003141 }
3142}
3143
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07003144/*
3145 * Determines the downscale amount of a plane for the purposes of watermark calculations.
3146 * The bspec defines downscale amount as:
3147 *
3148 * """
3149 * Horizontal down scale amount = maximum[1, Horizontal source size /
3150 * Horizontal destination size]
3151 * Vertical down scale amount = maximum[1, Vertical source size /
3152 * Vertical destination size]
3153 * Total down scale amount = Horizontal down scale amount *
3154 * Vertical down scale amount
3155 * """
3156 *
3157 * Return value is provided in 16.16 fixed point form to retain fractional part.
3158 * Caller should take care of dividing & rounding off the value.
3159 */
3160static uint32_t
3161skl_plane_downscale_amount(const struct intel_plane_state *pstate)
3162{
3163 uint32_t downscale_h, downscale_w;
3164 uint32_t src_w, src_h, dst_w, dst_h;
3165
Ville Syrjälä936e71e2016-07-26 19:06:59 +03003166 if (WARN_ON(!pstate->base.visible))
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07003167 return DRM_PLANE_HELPER_NO_SCALING;
3168
3169 /* n.b., src is 16.16 fixed point, dst is whole integer */
Ville Syrjälä936e71e2016-07-26 19:06:59 +03003170 src_w = drm_rect_width(&pstate->base.src);
3171 src_h = drm_rect_height(&pstate->base.src);
3172 dst_w = drm_rect_width(&pstate->base.dst);
3173 dst_h = drm_rect_height(&pstate->base.dst);
Ville Syrjäläbd2ef252016-09-26 19:30:46 +03003174 if (drm_rotation_90_or_270(pstate->base.rotation))
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07003175 swap(dst_w, dst_h);
3176
3177 downscale_h = max(src_h / dst_h, (uint32_t)DRM_PLANE_HELPER_NO_SCALING);
3178 downscale_w = max(src_w / dst_w, (uint32_t)DRM_PLANE_HELPER_NO_SCALING);
3179
3180 /* Provide result in 16.16 fixed point */
3181 return (uint64_t)downscale_w * downscale_h >> 16;
3182}
3183
Damien Lespiaub9cec072014-11-04 17:06:43 +00003184static unsigned int
Matt Roper024c9042015-09-24 15:53:11 -07003185skl_plane_relative_data_rate(const struct intel_crtc_state *cstate,
3186 const struct drm_plane_state *pstate,
3187 int y)
Damien Lespiaub9cec072014-11-04 17:06:43 +00003188{
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07003189 struct intel_plane_state *intel_pstate = to_intel_plane_state(pstate);
Matt Roper024c9042015-09-24 15:53:11 -07003190 struct drm_framebuffer *fb = pstate->fb;
Kumar, Mahesh8d19d7d2016-05-19 15:03:01 -07003191 uint32_t down_scale_amount, data_rate;
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07003192 uint32_t width = 0, height = 0;
Matt Ropera1de91e2016-05-12 07:05:57 -07003193 unsigned format = fb ? fb->pixel_format : DRM_FORMAT_XRGB8888;
3194
Ville Syrjälä936e71e2016-07-26 19:06:59 +03003195 if (!intel_pstate->base.visible)
Matt Ropera1de91e2016-05-12 07:05:57 -07003196 return 0;
3197 if (pstate->plane->type == DRM_PLANE_TYPE_CURSOR)
3198 return 0;
3199 if (y && format != DRM_FORMAT_NV12)
3200 return 0;
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07003201
Ville Syrjälä936e71e2016-07-26 19:06:59 +03003202 width = drm_rect_width(&intel_pstate->base.src) >> 16;
3203 height = drm_rect_height(&intel_pstate->base.src) >> 16;
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07003204
Ville Syrjäläbd2ef252016-09-26 19:30:46 +03003205 if (drm_rotation_90_or_270(pstate->rotation))
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07003206 swap(width, height);
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003207
3208 /* for planar format */
Matt Ropera1de91e2016-05-12 07:05:57 -07003209 if (format == DRM_FORMAT_NV12) {
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003210 if (y) /* y-plane data rate */
Kumar, Mahesh8d19d7d2016-05-19 15:03:01 -07003211 data_rate = width * height *
Matt Ropera1de91e2016-05-12 07:05:57 -07003212 drm_format_plane_cpp(format, 0);
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003213 else /* uv-plane data rate */
Kumar, Mahesh8d19d7d2016-05-19 15:03:01 -07003214 data_rate = (width / 2) * (height / 2) *
Matt Ropera1de91e2016-05-12 07:05:57 -07003215 drm_format_plane_cpp(format, 1);
Kumar, Mahesh8d19d7d2016-05-19 15:03:01 -07003216 } else {
3217 /* for packed formats */
3218 data_rate = width * height * drm_format_plane_cpp(format, 0);
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003219 }
3220
Kumar, Mahesh8d19d7d2016-05-19 15:03:01 -07003221 down_scale_amount = skl_plane_downscale_amount(intel_pstate);
3222
3223 return (uint64_t)data_rate * down_scale_amount >> 16;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003224}
3225
3226/*
3227 * We don't overflow 32 bits. Worst case is 3 planes enabled, each fetching
3228 * a 8192x4096@32bpp framebuffer:
3229 * 3 * 4096 * 8192 * 4 < 2^32
3230 */
3231static unsigned int
Maarten Lankhorst1e6ee542016-10-26 15:41:32 +02003232skl_get_total_relative_data_rate(struct intel_crtc_state *intel_cstate,
3233 unsigned *plane_data_rate,
3234 unsigned *plane_y_data_rate)
Damien Lespiaub9cec072014-11-04 17:06:43 +00003235{
Matt Roper9c74d822016-05-12 07:05:58 -07003236 struct drm_crtc_state *cstate = &intel_cstate->base;
3237 struct drm_atomic_state *state = cstate->state;
Maarten Lankhorstc8fe32c2016-10-26 15:41:29 +02003238 struct drm_plane *plane;
Maarten Lankhorstc8fe32c2016-10-26 15:41:29 +02003239 const struct drm_plane_state *pstate;
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003240 unsigned int total_data_rate = 0;
Matt Ropera6d3460e2016-05-12 07:06:04 -07003241
3242 if (WARN_ON(!state))
3243 return 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003244
Matt Ropera1de91e2016-05-12 07:05:57 -07003245 /* Calculate and cache data rate for each plane */
Maarten Lankhorstc8fe32c2016-10-26 15:41:29 +02003246 drm_atomic_crtc_state_for_each_plane_state(plane, pstate, cstate) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003247 enum plane_id plane_id = to_intel_plane(plane)->id;
3248 unsigned int rate;
Matt Roper024c9042015-09-24 15:53:11 -07003249
Matt Ropera6d3460e2016-05-12 07:06:04 -07003250 /* packed/uv */
3251 rate = skl_plane_relative_data_rate(intel_cstate,
3252 pstate, 0);
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003253 plane_data_rate[plane_id] = rate;
Maarten Lankhorst1e6ee542016-10-26 15:41:32 +02003254
3255 total_data_rate += rate;
Matt Roper9c74d822016-05-12 07:05:58 -07003256
Matt Ropera6d3460e2016-05-12 07:06:04 -07003257 /* y-plane */
3258 rate = skl_plane_relative_data_rate(intel_cstate,
3259 pstate, 1);
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003260 plane_y_data_rate[plane_id] = rate;
Matt Ropera1de91e2016-05-12 07:05:57 -07003261
Maarten Lankhorst1e6ee542016-10-26 15:41:32 +02003262 total_data_rate += rate;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003263 }
3264
3265 return total_data_rate;
3266}
3267
Kumar, Maheshcbcfd142016-05-31 09:58:59 -07003268static uint16_t
3269skl_ddb_min_alloc(const struct drm_plane_state *pstate,
3270 const int y)
3271{
3272 struct drm_framebuffer *fb = pstate->fb;
3273 struct intel_plane_state *intel_pstate = to_intel_plane_state(pstate);
3274 uint32_t src_w, src_h;
3275 uint32_t min_scanlines = 8;
3276 uint8_t plane_bpp;
3277
3278 if (WARN_ON(!fb))
3279 return 0;
3280
3281 /* For packed formats, no y-plane, return 0 */
3282 if (y && fb->pixel_format != DRM_FORMAT_NV12)
3283 return 0;
3284
3285 /* For Non Y-tile return 8-blocks */
Ville Syrjäläbae781b2016-11-16 13:33:16 +02003286 if (fb->modifier != I915_FORMAT_MOD_Y_TILED &&
3287 fb->modifier != I915_FORMAT_MOD_Yf_TILED)
Kumar, Maheshcbcfd142016-05-31 09:58:59 -07003288 return 8;
3289
Ville Syrjälä936e71e2016-07-26 19:06:59 +03003290 src_w = drm_rect_width(&intel_pstate->base.src) >> 16;
3291 src_h = drm_rect_height(&intel_pstate->base.src) >> 16;
Kumar, Maheshcbcfd142016-05-31 09:58:59 -07003292
Ville Syrjäläbd2ef252016-09-26 19:30:46 +03003293 if (drm_rotation_90_or_270(pstate->rotation))
Kumar, Maheshcbcfd142016-05-31 09:58:59 -07003294 swap(src_w, src_h);
3295
3296 /* Halve UV plane width and height for NV12 */
3297 if (fb->pixel_format == DRM_FORMAT_NV12 && !y) {
3298 src_w /= 2;
3299 src_h /= 2;
3300 }
3301
3302 if (fb->pixel_format == DRM_FORMAT_NV12 && !y)
3303 plane_bpp = drm_format_plane_cpp(fb->pixel_format, 1);
3304 else
3305 plane_bpp = drm_format_plane_cpp(fb->pixel_format, 0);
3306
Ville Syrjäläbd2ef252016-09-26 19:30:46 +03003307 if (drm_rotation_90_or_270(pstate->rotation)) {
Kumar, Maheshcbcfd142016-05-31 09:58:59 -07003308 switch (plane_bpp) {
3309 case 1:
3310 min_scanlines = 32;
3311 break;
3312 case 2:
3313 min_scanlines = 16;
3314 break;
3315 case 4:
3316 min_scanlines = 8;
3317 break;
3318 case 8:
3319 min_scanlines = 4;
3320 break;
3321 default:
3322 WARN(1, "Unsupported pixel depth %u for rotation",
3323 plane_bpp);
3324 min_scanlines = 32;
3325 }
3326 }
3327
3328 return DIV_ROUND_UP((4 * src_w * plane_bpp), 512) * min_scanlines/4 + 3;
3329}
3330
Maarten Lankhorst49845a72016-10-26 15:41:34 +02003331static void
3332skl_ddb_calc_min(const struct intel_crtc_state *cstate, int num_active,
3333 uint16_t *minimum, uint16_t *y_minimum)
3334{
3335 const struct drm_plane_state *pstate;
3336 struct drm_plane *plane;
3337
3338 drm_atomic_crtc_state_for_each_plane_state(plane, pstate, &cstate->base) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003339 enum plane_id plane_id = to_intel_plane(plane)->id;
Maarten Lankhorst49845a72016-10-26 15:41:34 +02003340
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003341 if (plane_id == PLANE_CURSOR)
Maarten Lankhorst49845a72016-10-26 15:41:34 +02003342 continue;
3343
3344 if (!pstate->visible)
3345 continue;
3346
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003347 minimum[plane_id] = skl_ddb_min_alloc(pstate, 0);
3348 y_minimum[plane_id] = skl_ddb_min_alloc(pstate, 1);
Maarten Lankhorst49845a72016-10-26 15:41:34 +02003349 }
3350
3351 minimum[PLANE_CURSOR] = skl_cursor_allocation(num_active);
3352}
3353
Matt Roperc107acf2016-05-12 07:06:01 -07003354static int
Matt Roper024c9042015-09-24 15:53:11 -07003355skl_allocate_pipe_ddb(struct intel_crtc_state *cstate,
Damien Lespiaub9cec072014-11-04 17:06:43 +00003356 struct skl_ddb_allocation *ddb /* out */)
3357{
Matt Roperc107acf2016-05-12 07:06:01 -07003358 struct drm_atomic_state *state = cstate->base.state;
Matt Roper024c9042015-09-24 15:53:11 -07003359 struct drm_crtc *crtc = cstate->base.crtc;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003360 struct drm_device *dev = crtc->dev;
3361 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3362 enum pipe pipe = intel_crtc->pipe;
Lyudece0ba282016-09-15 10:46:35 -04003363 struct skl_ddb_entry *alloc = &cstate->wm.skl.ddb;
Maarten Lankhorst49845a72016-10-26 15:41:34 +02003364 uint16_t alloc_size, start;
Maarten Lankhorstfefdd812016-10-26 15:41:33 +02003365 uint16_t minimum[I915_MAX_PLANES] = {};
3366 uint16_t y_minimum[I915_MAX_PLANES] = {};
Damien Lespiaub9cec072014-11-04 17:06:43 +00003367 unsigned int total_data_rate;
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003368 enum plane_id plane_id;
Matt Roperc107acf2016-05-12 07:06:01 -07003369 int num_active;
Maarten Lankhorst1e6ee542016-10-26 15:41:32 +02003370 unsigned plane_data_rate[I915_MAX_PLANES] = {};
3371 unsigned plane_y_data_rate[I915_MAX_PLANES] = {};
Damien Lespiaub9cec072014-11-04 17:06:43 +00003372
Paulo Zanoni5a920b82016-10-04 14:37:32 -03003373 /* Clear the partitioning for disabled planes. */
3374 memset(ddb->plane[pipe], 0, sizeof(ddb->plane[pipe]));
3375 memset(ddb->y_plane[pipe], 0, sizeof(ddb->y_plane[pipe]));
3376
Matt Ropera6d3460e2016-05-12 07:06:04 -07003377 if (WARN_ON(!state))
3378 return 0;
3379
Matt Roperc107acf2016-05-12 07:06:01 -07003380 if (!cstate->base.active) {
Lyudece0ba282016-09-15 10:46:35 -04003381 alloc->start = alloc->end = 0;
Matt Roperc107acf2016-05-12 07:06:01 -07003382 return 0;
3383 }
3384
Matt Ropera6d3460e2016-05-12 07:06:04 -07003385 skl_ddb_get_pipe_allocation_limits(dev, cstate, alloc, &num_active);
Damien Lespiau34bb56a2014-11-04 17:07:01 +00003386 alloc_size = skl_ddb_entry_size(alloc);
Damien Lespiaub9cec072014-11-04 17:06:43 +00003387 if (alloc_size == 0) {
3388 memset(ddb->plane[pipe], 0, sizeof(ddb->plane[pipe]));
Matt Roperc107acf2016-05-12 07:06:01 -07003389 return 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003390 }
3391
Maarten Lankhorst49845a72016-10-26 15:41:34 +02003392 skl_ddb_calc_min(cstate, num_active, minimum, y_minimum);
Damien Lespiaub9cec072014-11-04 17:06:43 +00003393
Maarten Lankhorst49845a72016-10-26 15:41:34 +02003394 /*
3395 * 1. Allocate the mininum required blocks for each active plane
3396 * and allocate the cursor, it doesn't require extra allocation
3397 * proportional to the data rate.
3398 */
Damien Lespiaub9cec072014-11-04 17:06:43 +00003399
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003400 for_each_plane_id_on_crtc(intel_crtc, plane_id) {
3401 alloc_size -= minimum[plane_id];
3402 alloc_size -= y_minimum[plane_id];
Damien Lespiau80958152015-02-09 13:35:10 +00003403 }
3404
Maarten Lankhorst49845a72016-10-26 15:41:34 +02003405 ddb->plane[pipe][PLANE_CURSOR].start = alloc->end - minimum[PLANE_CURSOR];
3406 ddb->plane[pipe][PLANE_CURSOR].end = alloc->end;
3407
Damien Lespiaub9cec072014-11-04 17:06:43 +00003408 /*
Damien Lespiau80958152015-02-09 13:35:10 +00003409 * 2. Distribute the remaining space in proportion to the amount of
3410 * data each plane needs to fetch from memory.
Damien Lespiaub9cec072014-11-04 17:06:43 +00003411 *
3412 * FIXME: we may not allocate every single block here.
3413 */
Maarten Lankhorst1e6ee542016-10-26 15:41:32 +02003414 total_data_rate = skl_get_total_relative_data_rate(cstate,
3415 plane_data_rate,
3416 plane_y_data_rate);
Matt Ropera1de91e2016-05-12 07:05:57 -07003417 if (total_data_rate == 0)
Matt Roperc107acf2016-05-12 07:06:01 -07003418 return 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003419
Damien Lespiau34bb56a2014-11-04 17:07:01 +00003420 start = alloc->start;
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003421 for_each_plane_id_on_crtc(intel_crtc, plane_id) {
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003422 unsigned int data_rate, y_data_rate;
3423 uint16_t plane_blocks, y_plane_blocks = 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003424
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003425 if (plane_id == PLANE_CURSOR)
Maarten Lankhorst49845a72016-10-26 15:41:34 +02003426 continue;
3427
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003428 data_rate = plane_data_rate[plane_id];
Damien Lespiaub9cec072014-11-04 17:06:43 +00003429
3430 /*
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003431 * allocation for (packed formats) or (uv-plane part of planar format):
Damien Lespiaub9cec072014-11-04 17:06:43 +00003432 * promote the expression to 64 bits to avoid overflowing, the
3433 * result is < available as data_rate / total_data_rate < 1
3434 */
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003435 plane_blocks = minimum[plane_id];
Damien Lespiau80958152015-02-09 13:35:10 +00003436 plane_blocks += div_u64((uint64_t)alloc_size * data_rate,
3437 total_data_rate);
Damien Lespiaub9cec072014-11-04 17:06:43 +00003438
Matt Roperc107acf2016-05-12 07:06:01 -07003439 /* Leave disabled planes at (0,0) */
3440 if (data_rate) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003441 ddb->plane[pipe][plane_id].start = start;
3442 ddb->plane[pipe][plane_id].end = start + plane_blocks;
Matt Roperc107acf2016-05-12 07:06:01 -07003443 }
Damien Lespiaub9cec072014-11-04 17:06:43 +00003444
3445 start += plane_blocks;
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003446
3447 /*
3448 * allocation for y_plane part of planar format:
3449 */
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003450 y_data_rate = plane_y_data_rate[plane_id];
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003451
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003452 y_plane_blocks = y_minimum[plane_id];
Matt Ropera1de91e2016-05-12 07:05:57 -07003453 y_plane_blocks += div_u64((uint64_t)alloc_size * y_data_rate,
3454 total_data_rate);
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003455
Matt Roperc107acf2016-05-12 07:06:01 -07003456 if (y_data_rate) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003457 ddb->y_plane[pipe][plane_id].start = start;
3458 ddb->y_plane[pipe][plane_id].end = start + y_plane_blocks;
Matt Roperc107acf2016-05-12 07:06:01 -07003459 }
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003460
Matt Ropera1de91e2016-05-12 07:05:57 -07003461 start += y_plane_blocks;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003462 }
3463
Matt Roperc107acf2016-05-12 07:06:01 -07003464 return 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003465}
3466
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003467/*
3468 * 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 +02003469 * for the read latency) and cpp should always be <= 8, so that
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003470 * should allow pixel_rate up to ~2 GHz which seems sufficient since max
3471 * 2xcdclk is 1350 MHz and the pixel rate should never exceed that.
3472*/
Ville Syrjäläac484962016-01-20 21:05:26 +02003473static uint32_t skl_wm_method1(uint32_t pixel_rate, uint8_t cpp, uint32_t latency)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003474{
3475 uint32_t wm_intermediate_val, ret;
3476
3477 if (latency == 0)
3478 return UINT_MAX;
3479
Ville Syrjäläac484962016-01-20 21:05:26 +02003480 wm_intermediate_val = latency * pixel_rate * cpp / 512;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003481 ret = DIV_ROUND_UP(wm_intermediate_val, 1000);
3482
3483 return ret;
3484}
3485
3486static uint32_t skl_wm_method2(uint32_t pixel_rate, uint32_t pipe_htotal,
Paulo Zanoni7a1a8ae2016-09-22 18:00:32 -03003487 uint32_t latency, uint32_t plane_blocks_per_line)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003488{
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00003489 uint32_t ret;
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00003490 uint32_t wm_intermediate_val;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003491
3492 if (latency == 0)
3493 return UINT_MAX;
3494
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003495 wm_intermediate_val = latency * pixel_rate;
3496 ret = DIV_ROUND_UP(wm_intermediate_val, pipe_htotal * 1000) *
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00003497 plane_blocks_per_line;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003498
3499 return ret;
3500}
3501
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07003502static uint32_t skl_adjusted_plane_pixel_rate(const struct intel_crtc_state *cstate,
3503 struct intel_plane_state *pstate)
3504{
3505 uint64_t adjusted_pixel_rate;
3506 uint64_t downscale_amount;
3507 uint64_t pixel_rate;
3508
3509 /* Shouldn't reach here on disabled planes... */
Ville Syrjälä936e71e2016-07-26 19:06:59 +03003510 if (WARN_ON(!pstate->base.visible))
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07003511 return 0;
3512
3513 /*
3514 * Adjusted plane pixel rate is just the pipe's adjusted pixel rate
3515 * with additional adjustments for plane-specific scaling.
3516 */
Paulo Zanonicfd7e3a2016-10-07 17:28:57 -03003517 adjusted_pixel_rate = ilk_pipe_pixel_rate(cstate);
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07003518 downscale_amount = skl_plane_downscale_amount(pstate);
3519
3520 pixel_rate = adjusted_pixel_rate * downscale_amount >> 16;
3521 WARN_ON(pixel_rate != clamp_t(uint32_t, pixel_rate, 0, ~0));
3522
3523 return pixel_rate;
3524}
3525
Matt Roper55994c22016-05-12 07:06:08 -07003526static int skl_compute_plane_wm(const struct drm_i915_private *dev_priv,
3527 struct intel_crtc_state *cstate,
3528 struct intel_plane_state *intel_pstate,
3529 uint16_t ddb_allocation,
3530 int level,
3531 uint16_t *out_blocks, /* out */
3532 uint8_t *out_lines, /* out */
3533 bool *enabled /* out */)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003534{
Matt Roper33815fa2016-05-12 07:06:05 -07003535 struct drm_plane_state *pstate = &intel_pstate->base;
3536 struct drm_framebuffer *fb = pstate->fb;
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00003537 uint32_t latency = dev_priv->wm.skl_latency[level];
3538 uint32_t method1, method2;
3539 uint32_t plane_bytes_per_line, plane_blocks_per_line;
3540 uint32_t res_blocks, res_lines;
3541 uint32_t selected_result;
Ville Syrjäläac484962016-01-20 21:05:26 +02003542 uint8_t cpp;
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07003543 uint32_t width = 0, height = 0;
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07003544 uint32_t plane_pixel_rate;
Paulo Zanoni75676ed2016-09-22 18:00:33 -03003545 uint32_t y_tile_minimum, y_min_scanlines;
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003546 struct intel_atomic_state *state =
3547 to_intel_atomic_state(cstate->base.state);
3548 bool apply_memory_bw_wa = skl_needs_memory_bw_wa(state);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003549
Ville Syrjälä936e71e2016-07-26 19:06:59 +03003550 if (latency == 0 || !cstate->base.active || !intel_pstate->base.visible) {
Matt Roper55994c22016-05-12 07:06:08 -07003551 *enabled = false;
3552 return 0;
3553 }
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003554
Ville Syrjäläbae781b2016-11-16 13:33:16 +02003555 if (apply_memory_bw_wa && fb->modifier == I915_FORMAT_MOD_X_TILED)
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003556 latency += 15;
3557
Ville Syrjälä936e71e2016-07-26 19:06:59 +03003558 width = drm_rect_width(&intel_pstate->base.src) >> 16;
3559 height = drm_rect_height(&intel_pstate->base.src) >> 16;
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07003560
Ville Syrjäläbd2ef252016-09-26 19:30:46 +03003561 if (drm_rotation_90_or_270(pstate->rotation))
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07003562 swap(width, height);
3563
Ville Syrjäläac484962016-01-20 21:05:26 +02003564 cpp = drm_format_plane_cpp(fb->pixel_format, 0);
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07003565 plane_pixel_rate = skl_adjusted_plane_pixel_rate(cstate, intel_pstate);
3566
Dave Airlie61d0a042016-10-25 16:35:20 +10003567 if (drm_rotation_90_or_270(pstate->rotation)) {
Paulo Zanoni1186fa82016-09-22 18:00:31 -03003568 int cpp = (fb->pixel_format == DRM_FORMAT_NV12) ?
3569 drm_format_plane_cpp(fb->pixel_format, 1) :
3570 drm_format_plane_cpp(fb->pixel_format, 0);
3571
3572 switch (cpp) {
3573 case 1:
3574 y_min_scanlines = 16;
3575 break;
3576 case 2:
3577 y_min_scanlines = 8;
3578 break;
Paulo Zanoni1186fa82016-09-22 18:00:31 -03003579 case 4:
3580 y_min_scanlines = 4;
3581 break;
Paulo Zanoni86a462b2016-09-22 18:00:35 -03003582 default:
3583 MISSING_CASE(cpp);
3584 return -EINVAL;
Paulo Zanoni1186fa82016-09-22 18:00:31 -03003585 }
3586 } else {
3587 y_min_scanlines = 4;
3588 }
3589
Paulo Zanoni2ef32de2016-11-08 18:22:11 -02003590 if (apply_memory_bw_wa)
3591 y_min_scanlines *= 2;
3592
Paulo Zanoni7a1a8ae2016-09-22 18:00:32 -03003593 plane_bytes_per_line = width * cpp;
Ville Syrjäläbae781b2016-11-16 13:33:16 +02003594 if (fb->modifier == I915_FORMAT_MOD_Y_TILED ||
3595 fb->modifier == I915_FORMAT_MOD_Yf_TILED) {
Paulo Zanoni7a1a8ae2016-09-22 18:00:32 -03003596 plane_blocks_per_line =
3597 DIV_ROUND_UP(plane_bytes_per_line * y_min_scanlines, 512);
3598 plane_blocks_per_line /= y_min_scanlines;
Ville Syrjäläbae781b2016-11-16 13:33:16 +02003599 } else if (fb->modifier == DRM_FORMAT_MOD_NONE) {
Paulo Zanoni7a1a8ae2016-09-22 18:00:32 -03003600 plane_blocks_per_line = DIV_ROUND_UP(plane_bytes_per_line, 512)
3601 + 1;
3602 } else {
3603 plane_blocks_per_line = DIV_ROUND_UP(plane_bytes_per_line, 512);
3604 }
3605
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07003606 method1 = skl_wm_method1(plane_pixel_rate, cpp, latency);
3607 method2 = skl_wm_method2(plane_pixel_rate,
Matt Roper024c9042015-09-24 15:53:11 -07003608 cstate->base.adjusted_mode.crtc_htotal,
Paulo Zanoni1186fa82016-09-22 18:00:31 -03003609 latency,
Paulo Zanoni7a1a8ae2016-09-22 18:00:32 -03003610 plane_blocks_per_line);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003611
Paulo Zanoni75676ed2016-09-22 18:00:33 -03003612 y_tile_minimum = plane_blocks_per_line * y_min_scanlines;
3613
Ville Syrjäläbae781b2016-11-16 13:33:16 +02003614 if (fb->modifier == I915_FORMAT_MOD_Y_TILED ||
3615 fb->modifier == I915_FORMAT_MOD_Yf_TILED) {
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00003616 selected_result = max(method2, y_tile_minimum);
3617 } else {
Paulo Zanonif1db3ea2016-09-22 18:00:34 -03003618 if ((cpp * cstate->base.adjusted_mode.crtc_htotal / 512 < 1) &&
3619 (plane_bytes_per_line / 512 < 1))
3620 selected_result = method2;
3621 else if ((ddb_allocation / plane_blocks_per_line) >= 1)
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00003622 selected_result = min(method1, method2);
3623 else
3624 selected_result = method1;
3625 }
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003626
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00003627 res_blocks = selected_result + 1;
3628 res_lines = DIV_ROUND_UP(selected_result, plane_blocks_per_line);
Damien Lespiaue6d66172014-11-04 17:06:55 +00003629
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00003630 if (level >= 1 && level <= 7) {
Ville Syrjäläbae781b2016-11-16 13:33:16 +02003631 if (fb->modifier == I915_FORMAT_MOD_Y_TILED ||
3632 fb->modifier == I915_FORMAT_MOD_Yf_TILED) {
Paulo Zanoni75676ed2016-09-22 18:00:33 -03003633 res_blocks += y_tile_minimum;
Paulo Zanoni1186fa82016-09-22 18:00:31 -03003634 res_lines += y_min_scanlines;
Paulo Zanoni75676ed2016-09-22 18:00:33 -03003635 } else {
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00003636 res_blocks++;
Paulo Zanoni75676ed2016-09-22 18:00:33 -03003637 }
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00003638 }
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00003639
Matt Roper55994c22016-05-12 07:06:08 -07003640 if (res_blocks >= ddb_allocation || res_lines > 31) {
3641 *enabled = false;
Matt Roper6b6bada2016-05-12 07:06:10 -07003642
3643 /*
3644 * If there are no valid level 0 watermarks, then we can't
3645 * support this display configuration.
3646 */
3647 if (level) {
3648 return 0;
3649 } else {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003650 struct drm_plane *plane = pstate->plane;
Matt Roper6b6bada2016-05-12 07:06:10 -07003651
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003652 DRM_DEBUG_KMS("Requested display configuration exceeds system watermark limitations\n");
3653 DRM_DEBUG_KMS("[PLANE:%d:%s] blocks required = %u/%u, lines required = %u/31\n",
3654 plane->base.id, plane->name,
3655 res_blocks, ddb_allocation, res_lines);
Matt Roper6b6bada2016-05-12 07:06:10 -07003656 return -EINVAL;
3657 }
Matt Roper55994c22016-05-12 07:06:08 -07003658 }
Damien Lespiaue6d66172014-11-04 17:06:55 +00003659
3660 *out_blocks = res_blocks;
3661 *out_lines = res_lines;
Matt Roper55994c22016-05-12 07:06:08 -07003662 *enabled = true;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003663
Matt Roper55994c22016-05-12 07:06:08 -07003664 return 0;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003665}
3666
Matt Roperf4a96752016-05-12 07:06:06 -07003667static int
3668skl_compute_wm_level(const struct drm_i915_private *dev_priv,
3669 struct skl_ddb_allocation *ddb,
3670 struct intel_crtc_state *cstate,
Lyudea62163e2016-10-04 14:28:20 -04003671 struct intel_plane *intel_plane,
Matt Roperf4a96752016-05-12 07:06:06 -07003672 int level,
3673 struct skl_wm_level *result)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003674{
Matt Roperf4a96752016-05-12 07:06:06 -07003675 struct drm_atomic_state *state = cstate->base.state;
Matt Roper024c9042015-09-24 15:53:11 -07003676 struct intel_crtc *intel_crtc = to_intel_crtc(cstate->base.crtc);
Lyudea62163e2016-10-04 14:28:20 -04003677 struct drm_plane *plane = &intel_plane->base;
3678 struct intel_plane_state *intel_pstate = NULL;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003679 uint16_t ddb_blocks;
Matt Roper024c9042015-09-24 15:53:11 -07003680 enum pipe pipe = intel_crtc->pipe;
Matt Roper55994c22016-05-12 07:06:08 -07003681 int ret;
Lyudea62163e2016-10-04 14:28:20 -04003682
3683 if (state)
3684 intel_pstate =
3685 intel_atomic_get_existing_plane_state(state,
3686 intel_plane);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003687
Matt Roperf4a96752016-05-12 07:06:06 -07003688 /*
Lyudea62163e2016-10-04 14:28:20 -04003689 * Note: If we start supporting multiple pending atomic commits against
3690 * the same planes/CRTC's in the future, plane->state will no longer be
3691 * the correct pre-state to use for the calculations here and we'll
3692 * need to change where we get the 'unchanged' plane data from.
3693 *
3694 * For now this is fine because we only allow one queued commit against
3695 * a CRTC. Even if the plane isn't modified by this transaction and we
3696 * don't have a plane lock, we still have the CRTC's lock, so we know
3697 * that no other transactions are racing with us to update it.
Matt Roperf4a96752016-05-12 07:06:06 -07003698 */
Lyudea62163e2016-10-04 14:28:20 -04003699 if (!intel_pstate)
3700 intel_pstate = to_intel_plane_state(plane->state);
Matt Roperf4a96752016-05-12 07:06:06 -07003701
Lyudea62163e2016-10-04 14:28:20 -04003702 WARN_ON(!intel_pstate->base.fb);
Matt Roper024c9042015-09-24 15:53:11 -07003703
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003704 ddb_blocks = skl_ddb_entry_size(&ddb->plane[pipe][intel_plane->id]);
Matt Roperf4a96752016-05-12 07:06:06 -07003705
Lyudea62163e2016-10-04 14:28:20 -04003706 ret = skl_compute_plane_wm(dev_priv,
3707 cstate,
3708 intel_pstate,
3709 ddb_blocks,
3710 level,
3711 &result->plane_res_b,
3712 &result->plane_res_l,
3713 &result->plane_en);
3714 if (ret)
3715 return ret;
Matt Roperf4a96752016-05-12 07:06:06 -07003716
3717 return 0;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003718}
3719
Damien Lespiau407b50f2014-11-04 17:06:57 +00003720static uint32_t
Matt Roper024c9042015-09-24 15:53:11 -07003721skl_compute_linetime_wm(struct intel_crtc_state *cstate)
Damien Lespiau407b50f2014-11-04 17:06:57 +00003722{
Paulo Zanoni30d1b5f2016-10-07 17:28:58 -03003723 uint32_t pixel_rate;
3724
Matt Roper024c9042015-09-24 15:53:11 -07003725 if (!cstate->base.active)
Damien Lespiau407b50f2014-11-04 17:06:57 +00003726 return 0;
3727
Paulo Zanoni30d1b5f2016-10-07 17:28:58 -03003728 pixel_rate = ilk_pipe_pixel_rate(cstate);
3729
3730 if (WARN_ON(pixel_rate == 0))
Mika Kuoppala661abfc2015-07-16 19:36:51 +03003731 return 0;
Damien Lespiau407b50f2014-11-04 17:06:57 +00003732
Matt Roper024c9042015-09-24 15:53:11 -07003733 return DIV_ROUND_UP(8 * cstate->base.adjusted_mode.crtc_htotal * 1000,
Paulo Zanoni30d1b5f2016-10-07 17:28:58 -03003734 pixel_rate);
Damien Lespiau407b50f2014-11-04 17:06:57 +00003735}
3736
Matt Roper024c9042015-09-24 15:53:11 -07003737static void skl_compute_transition_wm(struct intel_crtc_state *cstate,
Damien Lespiau9414f562014-11-04 17:06:58 +00003738 struct skl_wm_level *trans_wm /* out */)
Damien Lespiau407b50f2014-11-04 17:06:57 +00003739{
Matt Roper024c9042015-09-24 15:53:11 -07003740 if (!cstate->base.active)
Damien Lespiau407b50f2014-11-04 17:06:57 +00003741 return;
Damien Lespiau9414f562014-11-04 17:06:58 +00003742
3743 /* Until we know more, just disable transition WMs */
Lyudea62163e2016-10-04 14:28:20 -04003744 trans_wm->plane_en = false;
Damien Lespiau407b50f2014-11-04 17:06:57 +00003745}
3746
Matt Roper55994c22016-05-12 07:06:08 -07003747static int skl_build_pipe_wm(struct intel_crtc_state *cstate,
3748 struct skl_ddb_allocation *ddb,
3749 struct skl_pipe_wm *pipe_wm)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003750{
Matt Roper024c9042015-09-24 15:53:11 -07003751 struct drm_device *dev = cstate->base.crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003752 const struct drm_i915_private *dev_priv = to_i915(dev);
Lyudea62163e2016-10-04 14:28:20 -04003753 struct intel_plane *intel_plane;
3754 struct skl_plane_wm *wm;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003755 int level, max_level = ilk_wm_max_level(dev_priv);
Matt Roper55994c22016-05-12 07:06:08 -07003756 int ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003757
Lyudea62163e2016-10-04 14:28:20 -04003758 /*
3759 * We'll only calculate watermarks for planes that are actually
3760 * enabled, so make sure all other planes are set as disabled.
3761 */
3762 memset(pipe_wm->planes, 0, sizeof(pipe_wm->planes));
3763
3764 for_each_intel_plane_mask(&dev_priv->drm,
3765 intel_plane,
3766 cstate->base.plane_mask) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003767 wm = &pipe_wm->planes[intel_plane->id];
Lyudea62163e2016-10-04 14:28:20 -04003768
3769 for (level = 0; level <= max_level; level++) {
3770 ret = skl_compute_wm_level(dev_priv, ddb, cstate,
3771 intel_plane, level,
3772 &wm->wm[level]);
3773 if (ret)
3774 return ret;
3775 }
3776 skl_compute_transition_wm(cstate, &wm->trans_wm);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003777 }
Matt Roper024c9042015-09-24 15:53:11 -07003778 pipe_wm->linetime = skl_compute_linetime_wm(cstate);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003779
Matt Roper55994c22016-05-12 07:06:08 -07003780 return 0;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003781}
3782
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003783static void skl_ddb_entry_write(struct drm_i915_private *dev_priv,
3784 i915_reg_t reg,
Damien Lespiau16160e32014-11-04 17:06:53 +00003785 const struct skl_ddb_entry *entry)
3786{
3787 if (entry->end)
3788 I915_WRITE(reg, (entry->end - 1) << 16 | entry->start);
3789 else
3790 I915_WRITE(reg, 0);
3791}
3792
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003793static void skl_write_wm_level(struct drm_i915_private *dev_priv,
3794 i915_reg_t reg,
3795 const struct skl_wm_level *level)
3796{
3797 uint32_t val = 0;
3798
3799 if (level->plane_en) {
3800 val |= PLANE_WM_EN;
3801 val |= level->plane_res_b;
3802 val |= level->plane_res_l << PLANE_WM_LINES_SHIFT;
3803 }
3804
3805 I915_WRITE(reg, val);
3806}
3807
Ville Syrjäläd9348de2016-11-22 22:21:53 +02003808static void skl_write_plane_wm(struct intel_crtc *intel_crtc,
3809 const struct skl_plane_wm *wm,
3810 const struct skl_ddb_allocation *ddb,
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003811 enum plane_id plane_id)
Lyude62e0fb82016-08-22 12:50:08 -04003812{
3813 struct drm_crtc *crtc = &intel_crtc->base;
3814 struct drm_device *dev = crtc->dev;
3815 struct drm_i915_private *dev_priv = to_i915(dev);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003816 int level, max_level = ilk_wm_max_level(dev_priv);
Lyude62e0fb82016-08-22 12:50:08 -04003817 enum pipe pipe = intel_crtc->pipe;
3818
3819 for (level = 0; level <= max_level; level++) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003820 skl_write_wm_level(dev_priv, PLANE_WM(pipe, plane_id, level),
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003821 &wm->wm[level]);
Lyude62e0fb82016-08-22 12:50:08 -04003822 }
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003823 skl_write_wm_level(dev_priv, PLANE_WM_TRANS(pipe, plane_id),
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003824 &wm->trans_wm);
Lyude27082492016-08-24 07:48:10 +02003825
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003826 skl_ddb_entry_write(dev_priv, PLANE_BUF_CFG(pipe, plane_id),
3827 &ddb->plane[pipe][plane_id]);
3828 skl_ddb_entry_write(dev_priv, PLANE_NV12_BUF_CFG(pipe, plane_id),
3829 &ddb->y_plane[pipe][plane_id]);
Lyude62e0fb82016-08-22 12:50:08 -04003830}
3831
Ville Syrjäläd9348de2016-11-22 22:21:53 +02003832static void skl_write_cursor_wm(struct intel_crtc *intel_crtc,
3833 const struct skl_plane_wm *wm,
3834 const struct skl_ddb_allocation *ddb)
Lyude62e0fb82016-08-22 12:50:08 -04003835{
3836 struct drm_crtc *crtc = &intel_crtc->base;
3837 struct drm_device *dev = crtc->dev;
3838 struct drm_i915_private *dev_priv = to_i915(dev);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003839 int level, max_level = ilk_wm_max_level(dev_priv);
Lyude62e0fb82016-08-22 12:50:08 -04003840 enum pipe pipe = intel_crtc->pipe;
3841
3842 for (level = 0; level <= max_level; level++) {
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003843 skl_write_wm_level(dev_priv, CUR_WM(pipe, level),
3844 &wm->wm[level]);
Lyude62e0fb82016-08-22 12:50:08 -04003845 }
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003846 skl_write_wm_level(dev_priv, CUR_WM_TRANS(pipe), &wm->trans_wm);
Lyude27082492016-08-24 07:48:10 +02003847
3848 skl_ddb_entry_write(dev_priv, CUR_BUF_CFG(pipe),
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003849 &ddb->plane[pipe][PLANE_CURSOR]);
Lyude62e0fb82016-08-22 12:50:08 -04003850}
3851
cpaul@redhat.com45ece232016-10-14 17:31:56 -04003852bool skl_wm_level_equals(const struct skl_wm_level *l1,
3853 const struct skl_wm_level *l2)
3854{
3855 if (l1->plane_en != l2->plane_en)
3856 return false;
3857
3858 /* If both planes aren't enabled, the rest shouldn't matter */
3859 if (!l1->plane_en)
3860 return true;
3861
3862 return (l1->plane_res_l == l2->plane_res_l &&
3863 l1->plane_res_b == l2->plane_res_b);
3864}
3865
Lyude27082492016-08-24 07:48:10 +02003866static inline bool skl_ddb_entries_overlap(const struct skl_ddb_entry *a,
3867 const struct skl_ddb_entry *b)
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003868{
Lyude27082492016-08-24 07:48:10 +02003869 return a->start < b->end && b->start < a->end;
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003870}
3871
Maarten Lankhorst5eff5032016-11-08 13:55:35 +01003872bool skl_ddb_allocation_overlaps(const struct skl_ddb_entry **entries,
3873 const struct skl_ddb_entry *ddb,
3874 int ignore)
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003875{
Lyudece0ba282016-09-15 10:46:35 -04003876 int i;
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003877
Maarten Lankhorst5eff5032016-11-08 13:55:35 +01003878 for (i = 0; i < I915_MAX_PIPES; i++)
3879 if (i != ignore && entries[i] &&
3880 skl_ddb_entries_overlap(ddb, entries[i]))
Lyude27082492016-08-24 07:48:10 +02003881 return true;
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003882
Lyude27082492016-08-24 07:48:10 +02003883 return false;
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003884}
3885
Matt Roper55994c22016-05-12 07:06:08 -07003886static int skl_update_pipe_wm(struct drm_crtc_state *cstate,
Maarten Lankhorst03af79e2016-10-26 15:41:36 +02003887 const struct skl_pipe_wm *old_pipe_wm,
Matt Roper55994c22016-05-12 07:06:08 -07003888 struct skl_pipe_wm *pipe_wm, /* out */
Maarten Lankhorst03af79e2016-10-26 15:41:36 +02003889 struct skl_ddb_allocation *ddb, /* out */
Matt Roper55994c22016-05-12 07:06:08 -07003890 bool *changed /* out */)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003891{
Matt Roperf4a96752016-05-12 07:06:06 -07003892 struct intel_crtc_state *intel_cstate = to_intel_crtc_state(cstate);
Matt Roper55994c22016-05-12 07:06:08 -07003893 int ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003894
Matt Roper55994c22016-05-12 07:06:08 -07003895 ret = skl_build_pipe_wm(intel_cstate, ddb, pipe_wm);
3896 if (ret)
3897 return ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003898
Maarten Lankhorst03af79e2016-10-26 15:41:36 +02003899 if (!memcmp(old_pipe_wm, pipe_wm, sizeof(*pipe_wm)))
Matt Roper55994c22016-05-12 07:06:08 -07003900 *changed = false;
3901 else
3902 *changed = true;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003903
Matt Roper55994c22016-05-12 07:06:08 -07003904 return 0;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003905}
3906
Matt Roper9b613022016-06-27 16:42:44 -07003907static uint32_t
3908pipes_modified(struct drm_atomic_state *state)
3909{
3910 struct drm_crtc *crtc;
3911 struct drm_crtc_state *cstate;
3912 uint32_t i, ret = 0;
3913
3914 for_each_crtc_in_state(state, crtc, cstate, i)
3915 ret |= drm_crtc_mask(crtc);
3916
3917 return ret;
3918}
3919
Jani Nikulabb7791b2016-10-04 12:29:17 +03003920static int
Paulo Zanoni7f60e202016-09-29 16:36:48 -03003921skl_ddb_add_affected_planes(struct intel_crtc_state *cstate)
3922{
3923 struct drm_atomic_state *state = cstate->base.state;
3924 struct drm_device *dev = state->dev;
3925 struct drm_crtc *crtc = cstate->base.crtc;
3926 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3927 struct drm_i915_private *dev_priv = to_i915(dev);
3928 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
3929 struct skl_ddb_allocation *new_ddb = &intel_state->wm_results.ddb;
3930 struct skl_ddb_allocation *cur_ddb = &dev_priv->wm.skl_hw.ddb;
3931 struct drm_plane_state *plane_state;
3932 struct drm_plane *plane;
3933 enum pipe pipe = intel_crtc->pipe;
Paulo Zanoni7f60e202016-09-29 16:36:48 -03003934
3935 WARN_ON(!drm_atomic_get_existing_crtc_state(state, crtc));
3936
Maarten Lankhorst220b0962016-10-26 15:41:30 +02003937 drm_for_each_plane_mask(plane, dev, cstate->base.plane_mask) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003938 enum plane_id plane_id = to_intel_plane(plane)->id;
Paulo Zanoni7f60e202016-09-29 16:36:48 -03003939
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003940 if (skl_ddb_entry_equal(&cur_ddb->plane[pipe][plane_id],
3941 &new_ddb->plane[pipe][plane_id]) &&
3942 skl_ddb_entry_equal(&cur_ddb->y_plane[pipe][plane_id],
3943 &new_ddb->y_plane[pipe][plane_id]))
Paulo Zanoni7f60e202016-09-29 16:36:48 -03003944 continue;
3945
3946 plane_state = drm_atomic_get_plane_state(state, plane);
3947 if (IS_ERR(plane_state))
3948 return PTR_ERR(plane_state);
3949 }
3950
3951 return 0;
3952}
3953
Matt Roper98d39492016-05-12 07:06:03 -07003954static int
3955skl_compute_ddb(struct drm_atomic_state *state)
3956{
3957 struct drm_device *dev = state->dev;
3958 struct drm_i915_private *dev_priv = to_i915(dev);
3959 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
3960 struct intel_crtc *intel_crtc;
Matt Roper734fa012016-05-12 15:11:40 -07003961 struct skl_ddb_allocation *ddb = &intel_state->wm_results.ddb;
Matt Roper9b613022016-06-27 16:42:44 -07003962 uint32_t realloc_pipes = pipes_modified(state);
Matt Roper98d39492016-05-12 07:06:03 -07003963 int ret;
3964
3965 /*
3966 * If this is our first atomic update following hardware readout,
3967 * we can't trust the DDB that the BIOS programmed for us. Let's
3968 * pretend that all pipes switched active status so that we'll
3969 * ensure a full DDB recompute.
3970 */
Matt Roper1b54a882016-06-17 13:42:18 -07003971 if (dev_priv->wm.distrust_bios_wm) {
3972 ret = drm_modeset_lock(&dev->mode_config.connection_mutex,
3973 state->acquire_ctx);
3974 if (ret)
3975 return ret;
3976
Matt Roper98d39492016-05-12 07:06:03 -07003977 intel_state->active_pipe_changes = ~0;
3978
Matt Roper1b54a882016-06-17 13:42:18 -07003979 /*
3980 * We usually only initialize intel_state->active_crtcs if we
3981 * we're doing a modeset; make sure this field is always
3982 * initialized during the sanitization process that happens
3983 * on the first commit too.
3984 */
3985 if (!intel_state->modeset)
3986 intel_state->active_crtcs = dev_priv->active_crtcs;
3987 }
3988
Matt Roper98d39492016-05-12 07:06:03 -07003989 /*
3990 * If the modeset changes which CRTC's are active, we need to
3991 * recompute the DDB allocation for *all* active pipes, even
3992 * those that weren't otherwise being modified in any way by this
3993 * atomic commit. Due to the shrinking of the per-pipe allocations
3994 * when new active CRTC's are added, it's possible for a pipe that
3995 * we were already using and aren't changing at all here to suddenly
3996 * become invalid if its DDB needs exceeds its new allocation.
3997 *
3998 * Note that if we wind up doing a full DDB recompute, we can't let
3999 * any other display updates race with this transaction, so we need
4000 * to grab the lock on *all* CRTC's.
4001 */
Matt Roper734fa012016-05-12 15:11:40 -07004002 if (intel_state->active_pipe_changes) {
Matt Roper98d39492016-05-12 07:06:03 -07004003 realloc_pipes = ~0;
Matt Roper734fa012016-05-12 15:11:40 -07004004 intel_state->wm_results.dirty_pipes = ~0;
4005 }
Matt Roper98d39492016-05-12 07:06:03 -07004006
Paulo Zanoni5a920b82016-10-04 14:37:32 -03004007 /*
4008 * We're not recomputing for the pipes not included in the commit, so
4009 * make sure we start with the current state.
4010 */
4011 memcpy(ddb, &dev_priv->wm.skl_hw.ddb, sizeof(*ddb));
4012
Matt Roper98d39492016-05-12 07:06:03 -07004013 for_each_intel_crtc_mask(dev, intel_crtc, realloc_pipes) {
4014 struct intel_crtc_state *cstate;
4015
4016 cstate = intel_atomic_get_crtc_state(state, intel_crtc);
4017 if (IS_ERR(cstate))
4018 return PTR_ERR(cstate);
4019
Matt Roper734fa012016-05-12 15:11:40 -07004020 ret = skl_allocate_pipe_ddb(cstate, ddb);
Matt Roper98d39492016-05-12 07:06:03 -07004021 if (ret)
4022 return ret;
Lyude05a76d32016-08-17 15:55:57 -04004023
Paulo Zanoni7f60e202016-09-29 16:36:48 -03004024 ret = skl_ddb_add_affected_planes(cstate);
Lyude05a76d32016-08-17 15:55:57 -04004025 if (ret)
4026 return ret;
Matt Roper98d39492016-05-12 07:06:03 -07004027 }
4028
4029 return 0;
4030}
4031
Matt Roper2722efb2016-08-17 15:55:55 -04004032static void
4033skl_copy_wm_for_pipe(struct skl_wm_values *dst,
4034 struct skl_wm_values *src,
4035 enum pipe pipe)
4036{
Matt Roper2722efb2016-08-17 15:55:55 -04004037 memcpy(dst->ddb.y_plane[pipe], src->ddb.y_plane[pipe],
4038 sizeof(dst->ddb.y_plane[pipe]));
4039 memcpy(dst->ddb.plane[pipe], src->ddb.plane[pipe],
4040 sizeof(dst->ddb.plane[pipe]));
4041}
4042
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004043static void
4044skl_print_wm_changes(const struct drm_atomic_state *state)
4045{
4046 const struct drm_device *dev = state->dev;
4047 const struct drm_i915_private *dev_priv = to_i915(dev);
4048 const struct intel_atomic_state *intel_state =
4049 to_intel_atomic_state(state);
4050 const struct drm_crtc *crtc;
4051 const struct drm_crtc_state *cstate;
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004052 const struct intel_plane *intel_plane;
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004053 const struct skl_ddb_allocation *old_ddb = &dev_priv->wm.skl_hw.ddb;
4054 const struct skl_ddb_allocation *new_ddb = &intel_state->wm_results.ddb;
Maarten Lankhorst75704982016-11-01 12:04:10 +01004055 int i;
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004056
4057 for_each_crtc_in_state(state, crtc, cstate, i) {
Maarten Lankhorst75704982016-11-01 12:04:10 +01004058 const struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4059 enum pipe pipe = intel_crtc->pipe;
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004060
Maarten Lankhorst75704982016-11-01 12:04:10 +01004061 for_each_intel_plane_on_crtc(dev, intel_crtc, intel_plane) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004062 enum plane_id plane_id = intel_plane->id;
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004063 const struct skl_ddb_entry *old, *new;
4064
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004065 old = &old_ddb->plane[pipe][plane_id];
4066 new = &new_ddb->plane[pipe][plane_id];
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004067
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004068 if (skl_ddb_entry_equal(old, new))
4069 continue;
4070
Maarten Lankhorst75704982016-11-01 12:04:10 +01004071 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] ddb (%d - %d) -> (%d - %d)\n",
4072 intel_plane->base.base.id,
4073 intel_plane->base.name,
4074 old->start, old->end,
4075 new->start, new->end);
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004076 }
4077 }
4078}
4079
Matt Roper98d39492016-05-12 07:06:03 -07004080static int
4081skl_compute_wm(struct drm_atomic_state *state)
4082{
4083 struct drm_crtc *crtc;
4084 struct drm_crtc_state *cstate;
Matt Roper734fa012016-05-12 15:11:40 -07004085 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
4086 struct skl_wm_values *results = &intel_state->wm_results;
4087 struct skl_pipe_wm *pipe_wm;
Matt Roper98d39492016-05-12 07:06:03 -07004088 bool changed = false;
Matt Roper734fa012016-05-12 15:11:40 -07004089 int ret, i;
Matt Roper98d39492016-05-12 07:06:03 -07004090
4091 /*
4092 * If this transaction isn't actually touching any CRTC's, don't
4093 * bother with watermark calculation. Note that if we pass this
4094 * test, we're guaranteed to hold at least one CRTC state mutex,
4095 * which means we can safely use values like dev_priv->active_crtcs
4096 * since any racing commits that want to update them would need to
4097 * hold _all_ CRTC state mutexes.
4098 */
4099 for_each_crtc_in_state(state, crtc, cstate, i)
4100 changed = true;
4101 if (!changed)
4102 return 0;
4103
Matt Roper734fa012016-05-12 15:11:40 -07004104 /* Clear all dirty flags */
4105 results->dirty_pipes = 0;
4106
Matt Roper98d39492016-05-12 07:06:03 -07004107 ret = skl_compute_ddb(state);
4108 if (ret)
4109 return ret;
4110
Matt Roper734fa012016-05-12 15:11:40 -07004111 /*
4112 * Calculate WM's for all pipes that are part of this transaction.
4113 * Note that the DDB allocation above may have added more CRTC's that
4114 * weren't otherwise being modified (and set bits in dirty_pipes) if
4115 * pipe allocations had to change.
4116 *
4117 * FIXME: Now that we're doing this in the atomic check phase, we
4118 * should allow skl_update_pipe_wm() to return failure in cases where
4119 * no suitable watermark values can be found.
4120 */
4121 for_each_crtc_in_state(state, crtc, cstate, i) {
Matt Roper734fa012016-05-12 15:11:40 -07004122 struct intel_crtc_state *intel_cstate =
4123 to_intel_crtc_state(cstate);
Maarten Lankhorst03af79e2016-10-26 15:41:36 +02004124 const struct skl_pipe_wm *old_pipe_wm =
4125 &to_intel_crtc_state(crtc->state)->wm.skl.optimal;
Matt Roper734fa012016-05-12 15:11:40 -07004126
4127 pipe_wm = &intel_cstate->wm.skl.optimal;
Maarten Lankhorst03af79e2016-10-26 15:41:36 +02004128 ret = skl_update_pipe_wm(cstate, old_pipe_wm, pipe_wm,
4129 &results->ddb, &changed);
Matt Roper734fa012016-05-12 15:11:40 -07004130 if (ret)
4131 return ret;
4132
4133 if (changed)
4134 results->dirty_pipes |= drm_crtc_mask(crtc);
4135
4136 if ((results->dirty_pipes & drm_crtc_mask(crtc)) == 0)
4137 /* This pipe's WM's did not change */
4138 continue;
4139
4140 intel_cstate->update_wm_pre = true;
Matt Roper734fa012016-05-12 15:11:40 -07004141 }
4142
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004143 skl_print_wm_changes(state);
4144
Matt Roper98d39492016-05-12 07:06:03 -07004145 return 0;
4146}
4147
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01004148static void skl_atomic_update_crtc_wm(struct intel_atomic_state *state,
4149 struct intel_crtc_state *cstate)
4150{
4151 struct intel_crtc *crtc = to_intel_crtc(cstate->base.crtc);
4152 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
4153 struct skl_pipe_wm *pipe_wm = &cstate->wm.skl.optimal;
Maarten Lankhorste62929b2016-11-08 13:55:33 +01004154 const struct skl_ddb_allocation *ddb = &state->wm_results.ddb;
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01004155 enum pipe pipe = crtc->pipe;
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004156 enum plane_id plane_id;
Maarten Lankhorste62929b2016-11-08 13:55:33 +01004157
4158 if (!(state->wm_results.dirty_pipes & drm_crtc_mask(&crtc->base)))
4159 return;
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01004160
4161 I915_WRITE(PIPE_WM_LINETIME(pipe), pipe_wm->linetime);
Maarten Lankhorste62929b2016-11-08 13:55:33 +01004162
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004163 for_each_plane_id_on_crtc(crtc, plane_id) {
4164 if (plane_id != PLANE_CURSOR)
4165 skl_write_plane_wm(crtc, &pipe_wm->planes[plane_id],
4166 ddb, plane_id);
4167 else
4168 skl_write_cursor_wm(crtc, &pipe_wm->planes[plane_id],
4169 ddb);
4170 }
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01004171}
4172
Maarten Lankhorste62929b2016-11-08 13:55:33 +01004173static void skl_initial_wm(struct intel_atomic_state *state,
4174 struct intel_crtc_state *cstate)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004175{
Maarten Lankhorste62929b2016-11-08 13:55:33 +01004176 struct intel_crtc *intel_crtc = to_intel_crtc(cstate->base.crtc);
Ville Syrjälä432081b2016-10-31 22:37:03 +02004177 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004178 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorste62929b2016-11-08 13:55:33 +01004179 struct skl_wm_values *results = &state->wm_results;
Matt Roper2722efb2016-08-17 15:55:55 -04004180 struct skl_wm_values *hw_vals = &dev_priv->wm.skl_hw;
Lyude27082492016-08-24 07:48:10 +02004181 enum pipe pipe = intel_crtc->pipe;
Bob Paauweadda50b2015-07-21 10:42:53 -07004182
Ville Syrjälä432081b2016-10-31 22:37:03 +02004183 if ((results->dirty_pipes & drm_crtc_mask(&intel_crtc->base)) == 0)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004184 return;
4185
Matt Roper734fa012016-05-12 15:11:40 -07004186 mutex_lock(&dev_priv->wm.wm_mutex);
4187
Maarten Lankhorste62929b2016-11-08 13:55:33 +01004188 if (cstate->base.active_changed)
4189 skl_atomic_update_crtc_wm(state, cstate);
Lyude27082492016-08-24 07:48:10 +02004190
4191 skl_copy_wm_for_pipe(hw_vals, results, pipe);
Matt Roper734fa012016-05-12 15:11:40 -07004192
4193 mutex_unlock(&dev_priv->wm.wm_mutex);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004194}
4195
Ville Syrjäläd8905652016-01-14 14:53:35 +02004196static void ilk_compute_wm_config(struct drm_device *dev,
4197 struct intel_wm_config *config)
4198{
4199 struct intel_crtc *crtc;
4200
4201 /* Compute the currently _active_ config */
4202 for_each_intel_crtc(dev, crtc) {
4203 const struct intel_pipe_wm *wm = &crtc->wm.active.ilk;
4204
4205 if (!wm->pipe_enabled)
4206 continue;
4207
4208 config->sprites_enabled |= wm->sprites_enabled;
4209 config->sprites_scaled |= wm->sprites_scaled;
4210 config->num_pipes_active++;
4211 }
4212}
4213
Matt Ropered4a6a72016-02-23 17:20:13 -08004214static void ilk_program_watermarks(struct drm_i915_private *dev_priv)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03004215{
Chris Wilson91c8a322016-07-05 10:40:23 +01004216 struct drm_device *dev = &dev_priv->drm;
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07004217 struct intel_pipe_wm lp_wm_1_2 = {}, lp_wm_5_6 = {}, *best_lp_wm;
Imre Deak820c1982013-12-17 14:46:36 +02004218 struct ilk_wm_maximums max;
Ville Syrjäläd8905652016-01-14 14:53:35 +02004219 struct intel_wm_config config = {};
Imre Deak820c1982013-12-17 14:46:36 +02004220 struct ilk_wm_values results = {};
Ville Syrjälä77c122b2013-08-06 22:24:04 +03004221 enum intel_ddb_partitioning partitioning;
Matt Roper261a27d2015-10-08 15:28:25 -07004222
Ville Syrjäläd8905652016-01-14 14:53:35 +02004223 ilk_compute_wm_config(dev, &config);
4224
4225 ilk_compute_wm_maximums(dev, 1, &config, INTEL_DDB_PART_1_2, &max);
4226 ilk_wm_merge(dev, &config, &max, &lp_wm_1_2);
Ville Syrjälä0362c782013-10-09 19:17:57 +03004227
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03004228 /* 5/6 split only in single pipe config on IVB+ */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00004229 if (INTEL_GEN(dev_priv) >= 7 &&
Ville Syrjäläd8905652016-01-14 14:53:35 +02004230 config.num_pipes_active == 1 && config.sprites_enabled) {
4231 ilk_compute_wm_maximums(dev, 1, &config, INTEL_DDB_PART_5_6, &max);
4232 ilk_wm_merge(dev, &config, &max, &lp_wm_5_6);
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03004233
Imre Deak820c1982013-12-17 14:46:36 +02004234 best_lp_wm = ilk_find_best_result(dev, &lp_wm_1_2, &lp_wm_5_6);
Paulo Zanoni861f3382013-05-31 10:19:21 -03004235 } else {
Ville Syrjälä198a1e92013-10-09 19:17:58 +03004236 best_lp_wm = &lp_wm_1_2;
Paulo Zanoni861f3382013-05-31 10:19:21 -03004237 }
4238
Ville Syrjälä198a1e92013-10-09 19:17:58 +03004239 partitioning = (best_lp_wm == &lp_wm_1_2) ?
Ville Syrjälä77c122b2013-08-06 22:24:04 +03004240 INTEL_DDB_PART_1_2 : INTEL_DDB_PART_5_6;
Paulo Zanoni861f3382013-05-31 10:19:21 -03004241
Imre Deak820c1982013-12-17 14:46:36 +02004242 ilk_compute_wm_results(dev, best_lp_wm, partitioning, &results);
Ville Syrjälä609cede2013-10-09 19:18:03 +03004243
Imre Deak820c1982013-12-17 14:46:36 +02004244 ilk_write_wm_values(dev_priv, &results);
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03004245}
4246
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01004247static void ilk_initial_watermarks(struct intel_atomic_state *state,
4248 struct intel_crtc_state *cstate)
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07004249{
Matt Ropered4a6a72016-02-23 17:20:13 -08004250 struct drm_i915_private *dev_priv = to_i915(cstate->base.crtc->dev);
4251 struct intel_crtc *intel_crtc = to_intel_crtc(cstate->base.crtc);
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07004252
Matt Ropered4a6a72016-02-23 17:20:13 -08004253 mutex_lock(&dev_priv->wm.wm_mutex);
Matt Ropere8f1f022016-05-12 07:05:55 -07004254 intel_crtc->wm.active.ilk = cstate->wm.ilk.intermediate;
Matt Ropered4a6a72016-02-23 17:20:13 -08004255 ilk_program_watermarks(dev_priv);
4256 mutex_unlock(&dev_priv->wm.wm_mutex);
4257}
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07004258
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01004259static void ilk_optimize_watermarks(struct intel_atomic_state *state,
4260 struct intel_crtc_state *cstate)
Matt Ropered4a6a72016-02-23 17:20:13 -08004261{
4262 struct drm_i915_private *dev_priv = to_i915(cstate->base.crtc->dev);
4263 struct intel_crtc *intel_crtc = to_intel_crtc(cstate->base.crtc);
4264
4265 mutex_lock(&dev_priv->wm.wm_mutex);
4266 if (cstate->wm.need_postvbl_update) {
Matt Ropere8f1f022016-05-12 07:05:55 -07004267 intel_crtc->wm.active.ilk = cstate->wm.ilk.optimal;
Matt Ropered4a6a72016-02-23 17:20:13 -08004268 ilk_program_watermarks(dev_priv);
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07004269 }
Matt Ropered4a6a72016-02-23 17:20:13 -08004270 mutex_unlock(&dev_priv->wm.wm_mutex);
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07004271}
4272
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004273static inline void skl_wm_level_from_reg_val(uint32_t val,
4274 struct skl_wm_level *level)
Pradeep Bhat30789992014-11-04 17:06:45 +00004275{
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004276 level->plane_en = val & PLANE_WM_EN;
4277 level->plane_res_b = val & PLANE_WM_BLOCKS_MASK;
4278 level->plane_res_l = (val >> PLANE_WM_LINES_SHIFT) &
4279 PLANE_WM_LINES_MASK;
Pradeep Bhat30789992014-11-04 17:06:45 +00004280}
4281
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04004282void skl_pipe_wm_get_hw_state(struct drm_crtc *crtc,
4283 struct skl_pipe_wm *out)
Pradeep Bhat30789992014-11-04 17:06:45 +00004284{
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004285 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Pradeep Bhat30789992014-11-04 17:06:45 +00004286 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Pradeep Bhat30789992014-11-04 17:06:45 +00004287 enum pipe pipe = intel_crtc->pipe;
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004288 int level, max_level;
4289 enum plane_id plane_id;
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004290 uint32_t val;
Pradeep Bhat30789992014-11-04 17:06:45 +00004291
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01004292 max_level = ilk_wm_max_level(dev_priv);
Pradeep Bhat30789992014-11-04 17:06:45 +00004293
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004294 for_each_plane_id_on_crtc(intel_crtc, plane_id) {
4295 struct skl_plane_wm *wm = &out->planes[plane_id];
Pradeep Bhat30789992014-11-04 17:06:45 +00004296
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004297 for (level = 0; level <= max_level; level++) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004298 if (plane_id != PLANE_CURSOR)
4299 val = I915_READ(PLANE_WM(pipe, plane_id, level));
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004300 else
4301 val = I915_READ(CUR_WM(pipe, level));
4302
4303 skl_wm_level_from_reg_val(val, &wm->wm[level]);
4304 }
4305
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004306 if (plane_id != PLANE_CURSOR)
4307 val = I915_READ(PLANE_WM_TRANS(pipe, plane_id));
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004308 else
4309 val = I915_READ(CUR_WM_TRANS(pipe));
4310
4311 skl_wm_level_from_reg_val(val, &wm->trans_wm);
4312 }
Pradeep Bhat30789992014-11-04 17:06:45 +00004313
Matt Roper3ef00282015-03-09 10:19:24 -07004314 if (!intel_crtc->active)
Pradeep Bhat30789992014-11-04 17:06:45 +00004315 return;
4316
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04004317 out->linetime = I915_READ(PIPE_WM_LINETIME(pipe));
Pradeep Bhat30789992014-11-04 17:06:45 +00004318}
4319
4320void skl_wm_get_hw_state(struct drm_device *dev)
4321{
Chris Wilsonfac5e232016-07-04 11:34:36 +01004322 struct drm_i915_private *dev_priv = to_i915(dev);
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04004323 struct skl_wm_values *hw = &dev_priv->wm.skl_hw;
Damien Lespiaua269c582014-11-04 17:06:49 +00004324 struct skl_ddb_allocation *ddb = &dev_priv->wm.skl_hw.ddb;
Pradeep Bhat30789992014-11-04 17:06:45 +00004325 struct drm_crtc *crtc;
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04004326 struct intel_crtc *intel_crtc;
4327 struct intel_crtc_state *cstate;
Pradeep Bhat30789992014-11-04 17:06:45 +00004328
Damien Lespiaua269c582014-11-04 17:06:49 +00004329 skl_ddb_get_hw_state(dev_priv, ddb);
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04004330 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
4331 intel_crtc = to_intel_crtc(crtc);
4332 cstate = to_intel_crtc_state(crtc->state);
4333
4334 skl_pipe_wm_get_hw_state(crtc, &cstate->wm.skl.optimal);
4335
Maarten Lankhorst03af79e2016-10-26 15:41:36 +02004336 if (intel_crtc->active)
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04004337 hw->dirty_pipes |= drm_crtc_mask(crtc);
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04004338 }
Matt Ropera1de91e2016-05-12 07:05:57 -07004339
Matt Roper279e99d2016-05-12 07:06:02 -07004340 if (dev_priv->active_crtcs) {
4341 /* Fully recompute DDB on first atomic commit */
4342 dev_priv->wm.distrust_bios_wm = true;
4343 } else {
4344 /* Easy/common case; just sanitize DDB now if everything off */
4345 memset(ddb, 0, sizeof(*ddb));
4346 }
Pradeep Bhat30789992014-11-04 17:06:45 +00004347}
4348
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004349static void ilk_pipe_wm_get_hw_state(struct drm_crtc *crtc)
4350{
4351 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004352 struct drm_i915_private *dev_priv = to_i915(dev);
Imre Deak820c1982013-12-17 14:46:36 +02004353 struct ilk_wm_values *hw = &dev_priv->wm.hw;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004354 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Matt Roper4e0963c2015-09-24 15:53:15 -07004355 struct intel_crtc_state *cstate = to_intel_crtc_state(crtc->state);
Matt Ropere8f1f022016-05-12 07:05:55 -07004356 struct intel_pipe_wm *active = &cstate->wm.ilk.optimal;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004357 enum pipe pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004358 static const i915_reg_t wm0_pipe_reg[] = {
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004359 [PIPE_A] = WM0_PIPEA_ILK,
4360 [PIPE_B] = WM0_PIPEB_ILK,
4361 [PIPE_C] = WM0_PIPEC_IVB,
4362 };
4363
4364 hw->wm_pipe[pipe] = I915_READ(wm0_pipe_reg[pipe]);
Tvrtko Ursulin86527442016-10-13 11:03:00 +01004365 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjäläce0e0712013-12-05 15:51:36 +02004366 hw->wm_linetime[pipe] = I915_READ(PIPE_WM_LINETIME(pipe));
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004367
Ville Syrjälä15606532016-05-13 17:55:17 +03004368 memset(active, 0, sizeof(*active));
4369
Matt Roper3ef00282015-03-09 10:19:24 -07004370 active->pipe_enabled = intel_crtc->active;
Ville Syrjälä2a44b762014-03-07 18:32:09 +02004371
4372 if (active->pipe_enabled) {
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004373 u32 tmp = hw->wm_pipe[pipe];
4374
4375 /*
4376 * For active pipes LP0 watermark is marked as
4377 * enabled, and LP1+ watermaks as disabled since
4378 * we can't really reverse compute them in case
4379 * multiple pipes are active.
4380 */
4381 active->wm[0].enable = true;
4382 active->wm[0].pri_val = (tmp & WM0_PIPE_PLANE_MASK) >> WM0_PIPE_PLANE_SHIFT;
4383 active->wm[0].spr_val = (tmp & WM0_PIPE_SPRITE_MASK) >> WM0_PIPE_SPRITE_SHIFT;
4384 active->wm[0].cur_val = tmp & WM0_PIPE_CURSOR_MASK;
4385 active->linetime = hw->wm_linetime[pipe];
4386 } else {
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01004387 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004388
4389 /*
4390 * For inactive pipes, all watermark levels
4391 * should be marked as enabled but zeroed,
4392 * which is what we'd compute them to.
4393 */
4394 for (level = 0; level <= max_level; level++)
4395 active->wm[level].enable = true;
4396 }
Matt Roper4e0963c2015-09-24 15:53:15 -07004397
4398 intel_crtc->wm.active.ilk = *active;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004399}
4400
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03004401#define _FW_WM(value, plane) \
4402 (((value) & DSPFW_ ## plane ## _MASK) >> DSPFW_ ## plane ## _SHIFT)
4403#define _FW_WM_VLV(value, plane) \
4404 (((value) & DSPFW_ ## plane ## _MASK_VLV) >> DSPFW_ ## plane ## _SHIFT)
4405
4406static void vlv_read_wm_values(struct drm_i915_private *dev_priv,
4407 struct vlv_wm_values *wm)
4408{
4409 enum pipe pipe;
4410 uint32_t tmp;
4411
4412 for_each_pipe(dev_priv, pipe) {
4413 tmp = I915_READ(VLV_DDL(pipe));
4414
Ville Syrjälä1b313892016-11-28 19:37:08 +02004415 wm->ddl[pipe].plane[PLANE_PRIMARY] =
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03004416 (tmp >> DDL_PLANE_SHIFT) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
Ville Syrjälä1b313892016-11-28 19:37:08 +02004417 wm->ddl[pipe].plane[PLANE_CURSOR] =
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03004418 (tmp >> DDL_CURSOR_SHIFT) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
Ville Syrjälä1b313892016-11-28 19:37:08 +02004419 wm->ddl[pipe].plane[PLANE_SPRITE0] =
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03004420 (tmp >> DDL_SPRITE_SHIFT(0)) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
Ville Syrjälä1b313892016-11-28 19:37:08 +02004421 wm->ddl[pipe].plane[PLANE_SPRITE1] =
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03004422 (tmp >> DDL_SPRITE_SHIFT(1)) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
4423 }
4424
4425 tmp = I915_READ(DSPFW1);
4426 wm->sr.plane = _FW_WM(tmp, SR);
Ville Syrjälä1b313892016-11-28 19:37:08 +02004427 wm->pipe[PIPE_B].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORB);
4428 wm->pipe[PIPE_B].plane[PLANE_PRIMARY] = _FW_WM_VLV(tmp, PLANEB);
4429 wm->pipe[PIPE_A].plane[PLANE_PRIMARY] = _FW_WM_VLV(tmp, PLANEA);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03004430
4431 tmp = I915_READ(DSPFW2);
Ville Syrjälä1b313892016-11-28 19:37:08 +02004432 wm->pipe[PIPE_A].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITEB);
4433 wm->pipe[PIPE_A].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORA);
4434 wm->pipe[PIPE_A].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEA);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03004435
4436 tmp = I915_READ(DSPFW3);
4437 wm->sr.cursor = _FW_WM(tmp, CURSOR_SR);
4438
4439 if (IS_CHERRYVIEW(dev_priv)) {
4440 tmp = I915_READ(DSPFW7_CHV);
Ville Syrjälä1b313892016-11-28 19:37:08 +02004441 wm->pipe[PIPE_B].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITED);
4442 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEC);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03004443
4444 tmp = I915_READ(DSPFW8_CHV);
Ville Syrjälä1b313892016-11-28 19:37:08 +02004445 wm->pipe[PIPE_C].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITEF);
4446 wm->pipe[PIPE_C].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEE);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03004447
4448 tmp = I915_READ(DSPFW9_CHV);
Ville Syrjälä1b313892016-11-28 19:37:08 +02004449 wm->pipe[PIPE_C].plane[PLANE_PRIMARY] = _FW_WM_VLV(tmp, PLANEC);
4450 wm->pipe[PIPE_C].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORC);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03004451
4452 tmp = I915_READ(DSPHOWM);
4453 wm->sr.plane |= _FW_WM(tmp, SR_HI) << 9;
Ville Syrjälä1b313892016-11-28 19:37:08 +02004454 wm->pipe[PIPE_C].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITEF_HI) << 8;
4455 wm->pipe[PIPE_C].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEE_HI) << 8;
4456 wm->pipe[PIPE_C].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEC_HI) << 8;
4457 wm->pipe[PIPE_B].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITED_HI) << 8;
4458 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEC_HI) << 8;
4459 wm->pipe[PIPE_B].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEB_HI) << 8;
4460 wm->pipe[PIPE_A].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITEB_HI) << 8;
4461 wm->pipe[PIPE_A].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEA_HI) << 8;
4462 wm->pipe[PIPE_A].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEA_HI) << 8;
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03004463 } else {
4464 tmp = I915_READ(DSPFW7);
Ville Syrjälä1b313892016-11-28 19:37:08 +02004465 wm->pipe[PIPE_B].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITED);
4466 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEC);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03004467
4468 tmp = I915_READ(DSPHOWM);
4469 wm->sr.plane |= _FW_WM(tmp, SR_HI) << 9;
Ville Syrjälä1b313892016-11-28 19:37:08 +02004470 wm->pipe[PIPE_B].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITED_HI) << 8;
4471 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEC_HI) << 8;
4472 wm->pipe[PIPE_B].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEB_HI) << 8;
4473 wm->pipe[PIPE_A].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITEB_HI) << 8;
4474 wm->pipe[PIPE_A].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEA_HI) << 8;
4475 wm->pipe[PIPE_A].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEA_HI) << 8;
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03004476 }
4477}
4478
4479#undef _FW_WM
4480#undef _FW_WM_VLV
4481
4482void vlv_wm_get_hw_state(struct drm_device *dev)
4483{
4484 struct drm_i915_private *dev_priv = to_i915(dev);
4485 struct vlv_wm_values *wm = &dev_priv->wm.vlv;
4486 struct intel_plane *plane;
4487 enum pipe pipe;
4488 u32 val;
4489
4490 vlv_read_wm_values(dev_priv, wm);
4491
Ville Syrjälä49845a22016-11-22 18:02:01 +02004492 for_each_intel_plane(dev, plane)
4493 plane->wm.fifo_size = vlv_get_fifo_size(plane);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03004494
4495 wm->cxsr = I915_READ(FW_BLC_SELF_VLV) & FW_CSPWRDWNEN;
4496 wm->level = VLV_WM_LEVEL_PM2;
4497
4498 if (IS_CHERRYVIEW(dev_priv)) {
4499 mutex_lock(&dev_priv->rps.hw_lock);
4500
4501 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
4502 if (val & DSP_MAXFIFO_PM5_ENABLE)
4503 wm->level = VLV_WM_LEVEL_PM5;
4504
Ville Syrjälä58590c12015-09-08 21:05:12 +03004505 /*
4506 * If DDR DVFS is disabled in the BIOS, Punit
4507 * will never ack the request. So if that happens
4508 * assume we don't have to enable/disable DDR DVFS
4509 * dynamically. To test that just set the REQ_ACK
4510 * bit to poke the Punit, but don't change the
4511 * HIGH/LOW bits so that we don't actually change
4512 * the current state.
4513 */
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03004514 val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
Ville Syrjälä58590c12015-09-08 21:05:12 +03004515 val |= FORCE_DDR_FREQ_REQ_ACK;
4516 vlv_punit_write(dev_priv, PUNIT_REG_DDR_SETUP2, val);
4517
4518 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2) &
4519 FORCE_DDR_FREQ_REQ_ACK) == 0, 3)) {
4520 DRM_DEBUG_KMS("Punit not acking DDR DVFS request, "
4521 "assuming DDR DVFS is disabled\n");
4522 dev_priv->wm.max_level = VLV_WM_LEVEL_PM5;
4523 } else {
4524 val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
4525 if ((val & FORCE_DDR_HIGH_FREQ) == 0)
4526 wm->level = VLV_WM_LEVEL_DDR_DVFS;
4527 }
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03004528
4529 mutex_unlock(&dev_priv->rps.hw_lock);
4530 }
4531
4532 for_each_pipe(dev_priv, pipe)
4533 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 +02004534 pipe_name(pipe),
4535 wm->pipe[pipe].plane[PLANE_PRIMARY],
4536 wm->pipe[pipe].plane[PLANE_CURSOR],
4537 wm->pipe[pipe].plane[PLANE_SPRITE0],
4538 wm->pipe[pipe].plane[PLANE_SPRITE1]);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03004539
4540 DRM_DEBUG_KMS("Initial watermarks: SR plane=%d, SR cursor=%d level=%d cxsr=%d\n",
4541 wm->sr.plane, wm->sr.cursor, wm->level, wm->cxsr);
4542}
4543
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004544void ilk_wm_get_hw_state(struct drm_device *dev)
4545{
Chris Wilsonfac5e232016-07-04 11:34:36 +01004546 struct drm_i915_private *dev_priv = to_i915(dev);
Imre Deak820c1982013-12-17 14:46:36 +02004547 struct ilk_wm_values *hw = &dev_priv->wm.hw;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004548 struct drm_crtc *crtc;
4549
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01004550 for_each_crtc(dev, crtc)
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004551 ilk_pipe_wm_get_hw_state(crtc);
4552
4553 hw->wm_lp[0] = I915_READ(WM1_LP_ILK);
4554 hw->wm_lp[1] = I915_READ(WM2_LP_ILK);
4555 hw->wm_lp[2] = I915_READ(WM3_LP_ILK);
4556
4557 hw->wm_lp_spr[0] = I915_READ(WM1S_LP_ILK);
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00004558 if (INTEL_GEN(dev_priv) >= 7) {
Ville Syrjäläcfa76982014-03-07 18:32:08 +02004559 hw->wm_lp_spr[1] = I915_READ(WM2S_LP_IVB);
4560 hw->wm_lp_spr[2] = I915_READ(WM3S_LP_IVB);
4561 }
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004562
Tvrtko Ursulin86527442016-10-13 11:03:00 +01004563 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjäläac9545f2013-12-05 15:51:28 +02004564 hw->partitioning = (I915_READ(WM_MISC) & WM_MISC_DATA_PARTITION_5_6) ?
4565 INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01004566 else if (IS_IVYBRIDGE(dev_priv))
Ville Syrjäläac9545f2013-12-05 15:51:28 +02004567 hw->partitioning = (I915_READ(DISP_ARB_CTL2) & DISP_DATA_PARTITION_5_6) ?
4568 INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004569
4570 hw->enable_fbc_wm =
4571 !(I915_READ(DISP_ARB_CTL) & DISP_FBC_WM_DIS);
4572}
4573
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03004574/**
4575 * intel_update_watermarks - update FIFO watermark values based on current modes
4576 *
4577 * Calculate watermark values for the various WM regs based on current mode
4578 * and plane configuration.
4579 *
4580 * There are several cases to deal with here:
4581 * - normal (i.e. non-self-refresh)
4582 * - self-refresh (SR) mode
4583 * - lines are large relative to FIFO size (buffer can hold up to 2)
4584 * - lines are small relative to FIFO size (buffer can hold more than 2
4585 * lines), so need to account for TLB latency
4586 *
4587 * The normal calculation is:
4588 * watermark = dotclock * bytes per pixel * latency
4589 * where latency is platform & configuration dependent (we assume pessimal
4590 * values here).
4591 *
4592 * The SR calculation is:
4593 * watermark = (trunc(latency/line time)+1) * surface width *
4594 * bytes per pixel
4595 * where
4596 * line time = htotal / dotclock
4597 * surface width = hdisplay for normal plane and 64 for cursor
4598 * and latency is assumed to be high, as above.
4599 *
4600 * The final value programmed to the register should always be rounded up,
4601 * and include an extra 2 entries to account for clock crossings.
4602 *
4603 * We don't use the sprite, so we can ignore that. And on Crestline we have
4604 * to set the non-SR watermarks to 8.
4605 */
Ville Syrjälä432081b2016-10-31 22:37:03 +02004606void intel_update_watermarks(struct intel_crtc *crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03004607{
Ville Syrjälä432081b2016-10-31 22:37:03 +02004608 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03004609
4610 if (dev_priv->display.update_wm)
Ville Syrjälä46ba6142013-09-10 11:40:40 +03004611 dev_priv->display.update_wm(crtc);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03004612}
4613
Jani Nikulae2828912016-01-18 09:19:47 +02004614/*
Daniel Vetter92703882012-08-09 16:46:01 +02004615 * Lock protecting IPS related data structures
Daniel Vetter92703882012-08-09 16:46:01 +02004616 */
4617DEFINE_SPINLOCK(mchdev_lock);
4618
4619/* Global for IPS driver to get at the current i915 device. Protected by
4620 * mchdev_lock. */
4621static struct drm_i915_private *i915_mch_dev;
4622
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01004623bool ironlake_set_drps(struct drm_i915_private *dev_priv, u8 val)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004624{
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004625 u16 rgvswctl;
4626
Daniel Vetter92703882012-08-09 16:46:01 +02004627 assert_spin_locked(&mchdev_lock);
4628
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004629 rgvswctl = I915_READ16(MEMSWCTL);
4630 if (rgvswctl & MEMCTL_CMD_STS) {
4631 DRM_DEBUG("gpu busy, RCS change rejected\n");
4632 return false; /* still busy with another command */
4633 }
4634
4635 rgvswctl = (MEMCTL_CMD_CHFREQ << MEMCTL_CMD_SHIFT) |
4636 (val << MEMCTL_FREQ_SHIFT) | MEMCTL_SFCAVM;
4637 I915_WRITE16(MEMSWCTL, rgvswctl);
4638 POSTING_READ16(MEMSWCTL);
4639
4640 rgvswctl |= MEMCTL_CMD_STS;
4641 I915_WRITE16(MEMSWCTL, rgvswctl);
4642
4643 return true;
4644}
4645
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01004646static void ironlake_enable_drps(struct drm_i915_private *dev_priv)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004647{
Tvrtko Ursulin84f1b202016-02-11 10:27:32 +00004648 u32 rgvmodectl;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004649 u8 fmax, fmin, fstart, vstart;
4650
Daniel Vetter92703882012-08-09 16:46:01 +02004651 spin_lock_irq(&mchdev_lock);
4652
Tvrtko Ursulin84f1b202016-02-11 10:27:32 +00004653 rgvmodectl = I915_READ(MEMMODECTL);
4654
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004655 /* Enable temp reporting */
4656 I915_WRITE16(PMMISC, I915_READ(PMMISC) | MCPPCE_EN);
4657 I915_WRITE16(TSC1, I915_READ(TSC1) | TSE);
4658
4659 /* 100ms RC evaluation intervals */
4660 I915_WRITE(RCUPEI, 100000);
4661 I915_WRITE(RCDNEI, 100000);
4662
4663 /* Set max/min thresholds to 90ms and 80ms respectively */
4664 I915_WRITE(RCBMAXAVG, 90000);
4665 I915_WRITE(RCBMINAVG, 80000);
4666
4667 I915_WRITE(MEMIHYST, 1);
4668
4669 /* Set up min, max, and cur for interrupt handling */
4670 fmax = (rgvmodectl & MEMMODE_FMAX_MASK) >> MEMMODE_FMAX_SHIFT;
4671 fmin = (rgvmodectl & MEMMODE_FMIN_MASK);
4672 fstart = (rgvmodectl & MEMMODE_FSTART_MASK) >>
4673 MEMMODE_FSTART_SHIFT;
4674
Ville Syrjälä616847e2015-09-18 20:03:19 +03004675 vstart = (I915_READ(PXVFREQ(fstart)) & PXVFREQ_PX_MASK) >>
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004676 PXVFREQ_PX_SHIFT;
4677
Daniel Vetter20e4d402012-08-08 23:35:39 +02004678 dev_priv->ips.fmax = fmax; /* IPS callback will increase this */
4679 dev_priv->ips.fstart = fstart;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004680
Daniel Vetter20e4d402012-08-08 23:35:39 +02004681 dev_priv->ips.max_delay = fstart;
4682 dev_priv->ips.min_delay = fmin;
4683 dev_priv->ips.cur_delay = fstart;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004684
4685 DRM_DEBUG_DRIVER("fmax: %d, fmin: %d, fstart: %d\n",
4686 fmax, fmin, fstart);
4687
4688 I915_WRITE(MEMINTREN, MEMINT_CX_SUPR_EN | MEMINT_EVAL_CHG_EN);
4689
4690 /*
4691 * Interrupts will be enabled in ironlake_irq_postinstall
4692 */
4693
4694 I915_WRITE(VIDSTART, vstart);
4695 POSTING_READ(VIDSTART);
4696
4697 rgvmodectl |= MEMMODE_SWMODE_EN;
4698 I915_WRITE(MEMMODECTL, rgvmodectl);
4699
Daniel Vetter92703882012-08-09 16:46:01 +02004700 if (wait_for_atomic((I915_READ(MEMSWCTL) & MEMCTL_CMD_STS) == 0, 10))
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004701 DRM_ERROR("stuck trying to change perf mode\n");
Daniel Vetterdd92d8d2015-07-20 10:58:21 +02004702 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004703
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01004704 ironlake_set_drps(dev_priv, fstart);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004705
Ville Syrjälä7d81c3e2015-09-18 20:03:20 +03004706 dev_priv->ips.last_count1 = I915_READ(DMIEC) +
4707 I915_READ(DDREC) + I915_READ(CSIEC);
Daniel Vetter20e4d402012-08-08 23:35:39 +02004708 dev_priv->ips.last_time1 = jiffies_to_msecs(jiffies);
Ville Syrjälä7d81c3e2015-09-18 20:03:20 +03004709 dev_priv->ips.last_count2 = I915_READ(GFXEC);
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00004710 dev_priv->ips.last_time2 = ktime_get_raw_ns();
Daniel Vetter92703882012-08-09 16:46:01 +02004711
4712 spin_unlock_irq(&mchdev_lock);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004713}
4714
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01004715static void ironlake_disable_drps(struct drm_i915_private *dev_priv)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004716{
Daniel Vetter92703882012-08-09 16:46:01 +02004717 u16 rgvswctl;
4718
4719 spin_lock_irq(&mchdev_lock);
4720
4721 rgvswctl = I915_READ16(MEMSWCTL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004722
4723 /* Ack interrupts, disable EFC interrupt */
4724 I915_WRITE(MEMINTREN, I915_READ(MEMINTREN) & ~MEMINT_EVAL_CHG_EN);
4725 I915_WRITE(MEMINTRSTS, MEMINT_EVAL_CHG);
4726 I915_WRITE(DEIER, I915_READ(DEIER) & ~DE_PCU_EVENT);
4727 I915_WRITE(DEIIR, DE_PCU_EVENT);
4728 I915_WRITE(DEIMR, I915_READ(DEIMR) | DE_PCU_EVENT);
4729
4730 /* Go back to the starting frequency */
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01004731 ironlake_set_drps(dev_priv, dev_priv->ips.fstart);
Daniel Vetterdd92d8d2015-07-20 10:58:21 +02004732 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004733 rgvswctl |= MEMCTL_CMD_STS;
4734 I915_WRITE(MEMSWCTL, rgvswctl);
Daniel Vetterdd92d8d2015-07-20 10:58:21 +02004735 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004736
Daniel Vetter92703882012-08-09 16:46:01 +02004737 spin_unlock_irq(&mchdev_lock);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004738}
4739
Daniel Vetteracbe9472012-07-26 11:50:05 +02004740/* There's a funny hw issue where the hw returns all 0 when reading from
4741 * GEN6_RP_INTERRUPT_LIMITS. Hence we always need to compute the desired value
4742 * ourselves, instead of doing a rmw cycle (which might result in us clearing
4743 * all limits and the gpu stuck at whatever frequency it is at atm).
4744 */
Akash Goel74ef1172015-03-06 11:07:19 +05304745static u32 intel_rps_limits(struct drm_i915_private *dev_priv, u8 val)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004746{
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01004747 u32 limits;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004748
Daniel Vetter20b46e52012-07-26 11:16:14 +02004749 /* Only set the down limit when we've reached the lowest level to avoid
4750 * getting more interrupts, otherwise leave this clear. This prevents a
4751 * race in the hw when coming out of rc6: There's a tiny window where
4752 * the hw runs at the minimal clock before selecting the desired
4753 * frequency, if the down threshold expires in that window we will not
4754 * receive a down interrupt. */
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03004755 if (IS_GEN9(dev_priv)) {
Akash Goel74ef1172015-03-06 11:07:19 +05304756 limits = (dev_priv->rps.max_freq_softlimit) << 23;
4757 if (val <= dev_priv->rps.min_freq_softlimit)
4758 limits |= (dev_priv->rps.min_freq_softlimit) << 14;
4759 } else {
4760 limits = dev_priv->rps.max_freq_softlimit << 24;
4761 if (val <= dev_priv->rps.min_freq_softlimit)
4762 limits |= dev_priv->rps.min_freq_softlimit << 16;
4763 }
Daniel Vetter20b46e52012-07-26 11:16:14 +02004764
4765 return limits;
4766}
4767
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004768static void gen6_set_rps_thresholds(struct drm_i915_private *dev_priv, u8 val)
4769{
4770 int new_power;
Akash Goel8a586432015-03-06 11:07:18 +05304771 u32 threshold_up = 0, threshold_down = 0; /* in % */
4772 u32 ei_up = 0, ei_down = 0;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004773
4774 new_power = dev_priv->rps.power;
4775 switch (dev_priv->rps.power) {
4776 case LOW_POWER:
Chris Wilsona72b5622016-07-02 15:35:59 +01004777 if (val > dev_priv->rps.efficient_freq + 1 &&
4778 val > dev_priv->rps.cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004779 new_power = BETWEEN;
4780 break;
4781
4782 case BETWEEN:
Chris Wilsona72b5622016-07-02 15:35:59 +01004783 if (val <= dev_priv->rps.efficient_freq &&
4784 val < dev_priv->rps.cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004785 new_power = LOW_POWER;
Chris Wilsona72b5622016-07-02 15:35:59 +01004786 else if (val >= dev_priv->rps.rp0_freq &&
4787 val > dev_priv->rps.cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004788 new_power = HIGH_POWER;
4789 break;
4790
4791 case HIGH_POWER:
Chris Wilsona72b5622016-07-02 15:35:59 +01004792 if (val < (dev_priv->rps.rp1_freq + dev_priv->rps.rp0_freq) >> 1 &&
4793 val < dev_priv->rps.cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004794 new_power = BETWEEN;
4795 break;
4796 }
4797 /* Max/min bins are special */
Chris Wilsonaed242f2015-03-18 09:48:21 +00004798 if (val <= dev_priv->rps.min_freq_softlimit)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004799 new_power = LOW_POWER;
Chris Wilsonaed242f2015-03-18 09:48:21 +00004800 if (val >= dev_priv->rps.max_freq_softlimit)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004801 new_power = HIGH_POWER;
4802 if (new_power == dev_priv->rps.power)
4803 return;
4804
4805 /* Note the units here are not exactly 1us, but 1280ns. */
4806 switch (new_power) {
4807 case LOW_POWER:
4808 /* Upclock if more than 95% busy over 16ms */
Akash Goel8a586432015-03-06 11:07:18 +05304809 ei_up = 16000;
4810 threshold_up = 95;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004811
4812 /* Downclock if less than 85% busy over 32ms */
Akash Goel8a586432015-03-06 11:07:18 +05304813 ei_down = 32000;
4814 threshold_down = 85;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004815 break;
4816
4817 case BETWEEN:
4818 /* Upclock if more than 90% busy over 13ms */
Akash Goel8a586432015-03-06 11:07:18 +05304819 ei_up = 13000;
4820 threshold_up = 90;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004821
4822 /* Downclock if less than 75% busy over 32ms */
Akash Goel8a586432015-03-06 11:07:18 +05304823 ei_down = 32000;
4824 threshold_down = 75;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004825 break;
4826
4827 case HIGH_POWER:
4828 /* Upclock if more than 85% busy over 10ms */
Akash Goel8a586432015-03-06 11:07:18 +05304829 ei_up = 10000;
4830 threshold_up = 85;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004831
4832 /* Downclock if less than 60% busy over 32ms */
Akash Goel8a586432015-03-06 11:07:18 +05304833 ei_down = 32000;
4834 threshold_down = 60;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004835 break;
4836 }
4837
Akash Goel8a586432015-03-06 11:07:18 +05304838 I915_WRITE(GEN6_RP_UP_EI,
Chris Wilsona72b5622016-07-02 15:35:59 +01004839 GT_INTERVAL_FROM_US(dev_priv, ei_up));
Akash Goel8a586432015-03-06 11:07:18 +05304840 I915_WRITE(GEN6_RP_UP_THRESHOLD,
Chris Wilsona72b5622016-07-02 15:35:59 +01004841 GT_INTERVAL_FROM_US(dev_priv,
4842 ei_up * threshold_up / 100));
Akash Goel8a586432015-03-06 11:07:18 +05304843
4844 I915_WRITE(GEN6_RP_DOWN_EI,
Chris Wilsona72b5622016-07-02 15:35:59 +01004845 GT_INTERVAL_FROM_US(dev_priv, ei_down));
Akash Goel8a586432015-03-06 11:07:18 +05304846 I915_WRITE(GEN6_RP_DOWN_THRESHOLD,
Chris Wilsona72b5622016-07-02 15:35:59 +01004847 GT_INTERVAL_FROM_US(dev_priv,
4848 ei_down * threshold_down / 100));
Akash Goel8a586432015-03-06 11:07:18 +05304849
Chris Wilsona72b5622016-07-02 15:35:59 +01004850 I915_WRITE(GEN6_RP_CONTROL,
4851 GEN6_RP_MEDIA_TURBO |
4852 GEN6_RP_MEDIA_HW_NORMAL_MODE |
4853 GEN6_RP_MEDIA_IS_GFX |
4854 GEN6_RP_ENABLE |
4855 GEN6_RP_UP_BUSY_AVG |
4856 GEN6_RP_DOWN_IDLE_AVG);
Akash Goel8a586432015-03-06 11:07:18 +05304857
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004858 dev_priv->rps.power = new_power;
Chris Wilson8fb55192015-04-07 16:20:28 +01004859 dev_priv->rps.up_threshold = threshold_up;
4860 dev_priv->rps.down_threshold = threshold_down;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004861 dev_priv->rps.last_adj = 0;
4862}
4863
Chris Wilson2876ce72014-03-28 08:03:34 +00004864static u32 gen6_rps_pm_mask(struct drm_i915_private *dev_priv, u8 val)
4865{
4866 u32 mask = 0;
4867
4868 if (val > dev_priv->rps.min_freq_softlimit)
Chris Wilson6f4b12f82015-03-18 09:48:23 +00004869 mask |= GEN6_PM_RP_DOWN_EI_EXPIRED | GEN6_PM_RP_DOWN_THRESHOLD | GEN6_PM_RP_DOWN_TIMEOUT;
Chris Wilson2876ce72014-03-28 08:03:34 +00004870 if (val < dev_priv->rps.max_freq_softlimit)
Chris Wilson6f4b12f82015-03-18 09:48:23 +00004871 mask |= GEN6_PM_RP_UP_EI_EXPIRED | GEN6_PM_RP_UP_THRESHOLD;
Chris Wilson2876ce72014-03-28 08:03:34 +00004872
Chris Wilson7b3c29f2014-07-10 20:31:19 +01004873 mask &= dev_priv->pm_rps_events;
4874
Imre Deak59d02a12014-12-19 19:33:26 +02004875 return gen6_sanitize_rps_pm_mask(dev_priv, ~mask);
Chris Wilson2876ce72014-03-28 08:03:34 +00004876}
4877
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06004878/* gen6_set_rps is called to update the frequency request, but should also be
4879 * called when the range (min_delay and max_delay) is modified so that we can
4880 * update the GEN6_RP_INTERRUPT_LIMITS register accordingly. */
Chris Wilsondc979972016-05-10 14:10:04 +01004881static void gen6_set_rps(struct drm_i915_private *dev_priv, u8 val)
Daniel Vetter20b46e52012-07-26 11:16:14 +02004882{
Sagar Arun Kamble23eafea2015-08-23 17:52:48 +05304883 /* WaGsvDisableTurbo: Workaround to disable turbo on BXT A* */
Chris Wilsondc979972016-05-10 14:10:04 +01004884 if (IS_BXT_REVID(dev_priv, 0, BXT_REVID_A1))
Sagar Arun Kamble23eafea2015-08-23 17:52:48 +05304885 return;
4886
Jesse Barnes4fc688c2012-11-02 11:14:01 -07004887 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Chris Wilsonaed242f2015-03-18 09:48:21 +00004888 WARN_ON(val > dev_priv->rps.max_freq);
4889 WARN_ON(val < dev_priv->rps.min_freq);
Daniel Vetter004777c2012-08-09 15:07:01 +02004890
Chris Wilsoneb64cad2014-03-27 08:24:20 +00004891 /* min/max delay may still have been modified so be sure to
4892 * write the limits value.
4893 */
4894 if (val != dev_priv->rps.cur_freq) {
4895 gen6_set_rps_thresholds(dev_priv, val);
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06004896
Chris Wilsondc979972016-05-10 14:10:04 +01004897 if (IS_GEN9(dev_priv))
Akash Goel57041952015-03-06 11:07:17 +05304898 I915_WRITE(GEN6_RPNSWREQ,
4899 GEN9_FREQUENCY(val));
Chris Wilsondc979972016-05-10 14:10:04 +01004900 else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Chris Wilsoneb64cad2014-03-27 08:24:20 +00004901 I915_WRITE(GEN6_RPNSWREQ,
4902 HSW_FREQUENCY(val));
4903 else
4904 I915_WRITE(GEN6_RPNSWREQ,
4905 GEN6_FREQUENCY(val) |
4906 GEN6_OFFSET(0) |
4907 GEN6_AGGRESSIVE_TURBO);
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06004908 }
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01004909
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01004910 /* Make sure we continue to get interrupts
4911 * until we hit the minimum or maximum frequencies.
4912 */
Akash Goel74ef1172015-03-06 11:07:19 +05304913 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS, intel_rps_limits(dev_priv, val));
Chris Wilson2876ce72014-03-28 08:03:34 +00004914 I915_WRITE(GEN6_PMINTRMSK, gen6_rps_pm_mask(dev_priv, val));
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01004915
Ben Widawskyd5570a72012-09-07 19:43:41 -07004916 POSTING_READ(GEN6_RPNSWREQ);
4917
Ben Widawskyb39fb292014-03-19 18:31:11 -07004918 dev_priv->rps.cur_freq = val;
Mika Kuoppala0f945922015-11-17 18:14:26 +02004919 trace_intel_gpu_freq_change(intel_gpu_freq(dev_priv, val));
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004920}
4921
Chris Wilsondc979972016-05-10 14:10:04 +01004922static void valleyview_set_rps(struct drm_i915_private *dev_priv, u8 val)
Ville Syrjäläffe02b42015-02-02 19:09:50 +02004923{
Ville Syrjäläffe02b42015-02-02 19:09:50 +02004924 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Chris Wilsonaed242f2015-03-18 09:48:21 +00004925 WARN_ON(val > dev_priv->rps.max_freq);
4926 WARN_ON(val < dev_priv->rps.min_freq);
Ville Syrjäläffe02b42015-02-02 19:09:50 +02004927
Chris Wilsondc979972016-05-10 14:10:04 +01004928 if (WARN_ONCE(IS_CHERRYVIEW(dev_priv) && (val & 1),
Ville Syrjäläffe02b42015-02-02 19:09:50 +02004929 "Odd GPU freq value\n"))
4930 val &= ~1;
4931
Deepak Scd25dd52015-07-10 18:31:40 +05304932 I915_WRITE(GEN6_PMINTRMSK, gen6_rps_pm_mask(dev_priv, val));
4933
Chris Wilson8fb55192015-04-07 16:20:28 +01004934 if (val != dev_priv->rps.cur_freq) {
Ville Syrjäläffe02b42015-02-02 19:09:50 +02004935 vlv_punit_write(dev_priv, PUNIT_REG_GPU_FREQ_REQ, val);
Chris Wilson8fb55192015-04-07 16:20:28 +01004936 if (!IS_CHERRYVIEW(dev_priv))
4937 gen6_set_rps_thresholds(dev_priv, val);
4938 }
Ville Syrjäläffe02b42015-02-02 19:09:50 +02004939
Ville Syrjäläffe02b42015-02-02 19:09:50 +02004940 dev_priv->rps.cur_freq = val;
4941 trace_intel_gpu_freq_change(intel_gpu_freq(dev_priv, val));
4942}
4943
Deepak Sa7f6e232015-05-09 18:04:44 +05304944/* vlv_set_rps_idle: Set the frequency to idle, if Gfx clocks are down
Deepak S76c3552f2014-01-30 23:08:16 +05304945 *
4946 * * If Gfx is Idle, then
Deepak Sa7f6e232015-05-09 18:04:44 +05304947 * 1. Forcewake Media well.
4948 * 2. Request idle freq.
4949 * 3. Release Forcewake of Media well.
Deepak S76c3552f2014-01-30 23:08:16 +05304950*/
4951static void vlv_set_rps_idle(struct drm_i915_private *dev_priv)
4952{
Chris Wilsonaed242f2015-03-18 09:48:21 +00004953 u32 val = dev_priv->rps.idle_freq;
Deepak S5549d252014-06-28 11:26:11 +05304954
Chris Wilsonaed242f2015-03-18 09:48:21 +00004955 if (dev_priv->rps.cur_freq <= val)
Deepak S76c3552f2014-01-30 23:08:16 +05304956 return;
4957
Deepak Sa7f6e232015-05-09 18:04:44 +05304958 /* Wake up the media well, as that takes a lot less
4959 * power than the Render well. */
4960 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_MEDIA);
Chris Wilsondc979972016-05-10 14:10:04 +01004961 valleyview_set_rps(dev_priv, val);
Deepak Sa7f6e232015-05-09 18:04:44 +05304962 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_MEDIA);
Deepak S76c3552f2014-01-30 23:08:16 +05304963}
4964
Chris Wilson43cf3bf2015-03-18 09:48:22 +00004965void gen6_rps_busy(struct drm_i915_private *dev_priv)
4966{
4967 mutex_lock(&dev_priv->rps.hw_lock);
4968 if (dev_priv->rps.enabled) {
4969 if (dev_priv->pm_rps_events & (GEN6_PM_RP_DOWN_EI_EXPIRED | GEN6_PM_RP_UP_EI_EXPIRED))
4970 gen6_rps_reset_ei(dev_priv);
4971 I915_WRITE(GEN6_PMINTRMSK,
4972 gen6_rps_pm_mask(dev_priv, dev_priv->rps.cur_freq));
Michał Winiarski2b83c4c2016-06-20 11:58:27 +02004973
Chris Wilsonc33d2472016-07-04 08:08:36 +01004974 gen6_enable_rps_interrupts(dev_priv);
4975
Michał Winiarski2b83c4c2016-06-20 11:58:27 +02004976 /* Ensure we start at the user's desired frequency */
4977 intel_set_rps(dev_priv,
4978 clamp(dev_priv->rps.cur_freq,
4979 dev_priv->rps.min_freq_softlimit,
4980 dev_priv->rps.max_freq_softlimit));
Chris Wilson43cf3bf2015-03-18 09:48:22 +00004981 }
4982 mutex_unlock(&dev_priv->rps.hw_lock);
4983}
4984
Chris Wilsonb29c19b2013-09-25 17:34:56 +01004985void gen6_rps_idle(struct drm_i915_private *dev_priv)
4986{
Chris Wilsonc33d2472016-07-04 08:08:36 +01004987 /* Flush our bottom-half so that it does not race with us
4988 * setting the idle frequency and so that it is bounded by
4989 * our rpm wakeref. And then disable the interrupts to stop any
4990 * futher RPS reclocking whilst we are asleep.
4991 */
4992 gen6_disable_rps_interrupts(dev_priv);
4993
Chris Wilsonb29c19b2013-09-25 17:34:56 +01004994 mutex_lock(&dev_priv->rps.hw_lock);
Chris Wilsonc0951f02013-10-10 21:58:50 +01004995 if (dev_priv->rps.enabled) {
Chris Wilsondc979972016-05-10 14:10:04 +01004996 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Deepak S76c3552f2014-01-30 23:08:16 +05304997 vlv_set_rps_idle(dev_priv);
Daniel Vetter7526ed72014-09-29 15:07:19 +02004998 else
Chris Wilsondc979972016-05-10 14:10:04 +01004999 gen6_set_rps(dev_priv, dev_priv->rps.idle_freq);
Chris Wilsonc0951f02013-10-10 21:58:50 +01005000 dev_priv->rps.last_adj = 0;
Ville Syrjälä12c100b2016-05-23 17:42:48 +03005001 I915_WRITE(GEN6_PMINTRMSK,
5002 gen6_sanitize_rps_pm_mask(dev_priv, ~0));
Chris Wilsonc0951f02013-10-10 21:58:50 +01005003 }
Chris Wilson8d3afd72015-05-21 21:01:47 +01005004 mutex_unlock(&dev_priv->rps.hw_lock);
Chris Wilson1854d5c2015-04-07 16:20:32 +01005005
Chris Wilson8d3afd72015-05-21 21:01:47 +01005006 spin_lock(&dev_priv->rps.client_lock);
Chris Wilson1854d5c2015-04-07 16:20:32 +01005007 while (!list_empty(&dev_priv->rps.clients))
5008 list_del_init(dev_priv->rps.clients.next);
Chris Wilson8d3afd72015-05-21 21:01:47 +01005009 spin_unlock(&dev_priv->rps.client_lock);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005010}
5011
Chris Wilson1854d5c2015-04-07 16:20:32 +01005012void gen6_rps_boost(struct drm_i915_private *dev_priv,
Chris Wilsone61b9952015-04-27 13:41:24 +01005013 struct intel_rps_client *rps,
5014 unsigned long submitted)
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005015{
Chris Wilson8d3afd72015-05-21 21:01:47 +01005016 /* This is intentionally racy! We peek at the state here, then
5017 * validate inside the RPS worker.
5018 */
Chris Wilson67d97da2016-07-04 08:08:31 +01005019 if (!(dev_priv->gt.awake &&
Chris Wilson8d3afd72015-05-21 21:01:47 +01005020 dev_priv->rps.enabled &&
Chris Wilson29ecd78d2016-07-13 09:10:35 +01005021 dev_priv->rps.cur_freq < dev_priv->rps.boost_freq))
Chris Wilson8d3afd72015-05-21 21:01:47 +01005022 return;
Chris Wilson43cf3bf2015-03-18 09:48:22 +00005023
Chris Wilsone61b9952015-04-27 13:41:24 +01005024 /* Force a RPS boost (and don't count it against the client) if
5025 * the GPU is severely congested.
5026 */
Chris Wilsond0bc54f2015-05-21 21:01:48 +01005027 if (rps && time_after(jiffies, submitted + DRM_I915_THROTTLE_JIFFIES))
Chris Wilsone61b9952015-04-27 13:41:24 +01005028 rps = NULL;
5029
Chris Wilson8d3afd72015-05-21 21:01:47 +01005030 spin_lock(&dev_priv->rps.client_lock);
5031 if (rps == NULL || list_empty(&rps->link)) {
5032 spin_lock_irq(&dev_priv->irq_lock);
5033 if (dev_priv->rps.interrupts_enabled) {
5034 dev_priv->rps.client_boost = true;
Chris Wilsonc33d2472016-07-04 08:08:36 +01005035 schedule_work(&dev_priv->rps.work);
Chris Wilson8d3afd72015-05-21 21:01:47 +01005036 }
5037 spin_unlock_irq(&dev_priv->irq_lock);
Chris Wilson1854d5c2015-04-07 16:20:32 +01005038
Chris Wilson2e1b8732015-04-27 13:41:22 +01005039 if (rps != NULL) {
5040 list_add(&rps->link, &dev_priv->rps.clients);
5041 rps->boosts++;
Chris Wilson1854d5c2015-04-07 16:20:32 +01005042 } else
5043 dev_priv->rps.boosts++;
Chris Wilsonc0951f02013-10-10 21:58:50 +01005044 }
Chris Wilson8d3afd72015-05-21 21:01:47 +01005045 spin_unlock(&dev_priv->rps.client_lock);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005046}
5047
Chris Wilsondc979972016-05-10 14:10:04 +01005048void intel_set_rps(struct drm_i915_private *dev_priv, u8 val)
Jesse Barnes0a073b82013-04-17 15:54:58 -07005049{
Chris Wilsondc979972016-05-10 14:10:04 +01005050 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
5051 valleyview_set_rps(dev_priv, val);
Ville Syrjäläffe02b42015-02-02 19:09:50 +02005052 else
Chris Wilsondc979972016-05-10 14:10:04 +01005053 gen6_set_rps(dev_priv, val);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005054}
5055
Chris Wilsondc979972016-05-10 14:10:04 +01005056static void gen9_disable_rc6(struct drm_i915_private *dev_priv)
Zhe Wang20e49362014-11-04 17:07:05 +00005057{
Zhe Wang20e49362014-11-04 17:07:05 +00005058 I915_WRITE(GEN6_RC_CONTROL, 0);
Zhe Wang38c23522015-01-20 12:23:04 +00005059 I915_WRITE(GEN9_PG_ENABLE, 0);
Zhe Wang20e49362014-11-04 17:07:05 +00005060}
5061
Chris Wilsondc979972016-05-10 14:10:04 +01005062static void gen9_disable_rps(struct drm_i915_private *dev_priv)
Akash Goel2030d682016-04-23 00:05:45 +05305063{
Akash Goel2030d682016-04-23 00:05:45 +05305064 I915_WRITE(GEN6_RP_CONTROL, 0);
5065}
5066
Chris Wilsondc979972016-05-10 14:10:04 +01005067static void gen6_disable_rps(struct drm_i915_private *dev_priv)
Daniel Vetter44fc7d52013-07-12 22:43:27 +02005068{
Daniel Vetter44fc7d52013-07-12 22:43:27 +02005069 I915_WRITE(GEN6_RC_CONTROL, 0);
5070 I915_WRITE(GEN6_RPNSWREQ, 1 << 31);
Akash Goel2030d682016-04-23 00:05:45 +05305071 I915_WRITE(GEN6_RP_CONTROL, 0);
Daniel Vetter44fc7d52013-07-12 22:43:27 +02005072}
5073
Chris Wilsondc979972016-05-10 14:10:04 +01005074static void cherryview_disable_rps(struct drm_i915_private *dev_priv)
Deepak S38807742014-05-23 21:00:15 +05305075{
Deepak S38807742014-05-23 21:00:15 +05305076 I915_WRITE(GEN6_RC_CONTROL, 0);
5077}
5078
Chris Wilsondc979972016-05-10 14:10:04 +01005079static void valleyview_disable_rps(struct drm_i915_private *dev_priv)
Jesse Barnesd20d4f02013-04-23 10:09:28 -07005080{
Deepak S98a2e5f2014-08-18 10:35:27 -07005081 /* we're doing forcewake before Disabling RC6,
5082 * This what the BIOS expects when going into suspend */
Mika Kuoppala59bad942015-01-16 11:34:40 +02005083 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Deepak S98a2e5f2014-08-18 10:35:27 -07005084
Jesse Barnesd20d4f02013-04-23 10:09:28 -07005085 I915_WRITE(GEN6_RC_CONTROL, 0);
Jesse Barnesd20d4f02013-04-23 10:09:28 -07005086
Mika Kuoppala59bad942015-01-16 11:34:40 +02005087 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Jesse Barnesd20d4f02013-04-23 10:09:28 -07005088}
5089
Chris Wilsondc979972016-05-10 14:10:04 +01005090static void intel_print_rc6_info(struct drm_i915_private *dev_priv, u32 mode)
Ben Widawskydc39fff2013-10-18 12:32:07 -07005091{
Chris Wilsondc979972016-05-10 14:10:04 +01005092 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Imre Deak91ca6892014-04-14 20:24:25 +03005093 if (mode & (GEN7_RC_CTL_TO_MODE | GEN6_RC_CTL_EI_MODE(1)))
5094 mode = GEN6_RC_CTL_RC6_ENABLE;
5095 else
5096 mode = 0;
5097 }
Chris Wilsondc979972016-05-10 14:10:04 +01005098 if (HAS_RC6p(dev_priv))
Imre Deakb99d49c2016-06-29 19:13:54 +03005099 DRM_DEBUG_DRIVER("Enabling RC6 states: "
5100 "RC6 %s RC6p %s RC6pp %s\n",
5101 onoff(mode & GEN6_RC_CTL_RC6_ENABLE),
5102 onoff(mode & GEN6_RC_CTL_RC6p_ENABLE),
5103 onoff(mode & GEN6_RC_CTL_RC6pp_ENABLE));
Rodrigo Vivi58abf1d2014-10-07 07:06:50 -07005104
5105 else
Imre Deakb99d49c2016-06-29 19:13:54 +03005106 DRM_DEBUG_DRIVER("Enabling RC6 states: RC6 %s\n",
5107 onoff(mode & GEN6_RC_CTL_RC6_ENABLE));
Ben Widawskydc39fff2013-10-18 12:32:07 -07005108}
5109
Chris Wilsondc979972016-05-10 14:10:04 +01005110static bool bxt_check_bios_rc6_setup(struct drm_i915_private *dev_priv)
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05305111{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03005112 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05305113 bool enable_rc6 = true;
5114 unsigned long rc6_ctx_base;
Imre Deakfc619842016-06-29 19:13:55 +03005115 u32 rc_ctl;
5116 int rc_sw_target;
5117
5118 rc_ctl = I915_READ(GEN6_RC_CONTROL);
5119 rc_sw_target = (I915_READ(GEN6_RC_STATE) & RC_SW_TARGET_STATE_MASK) >>
5120 RC_SW_TARGET_STATE_SHIFT;
5121 DRM_DEBUG_DRIVER("BIOS enabled RC states: "
5122 "HW_CTRL %s HW_RC6 %s SW_TARGET_STATE %x\n",
5123 onoff(rc_ctl & GEN6_RC_CTL_HW_ENABLE),
5124 onoff(rc_ctl & GEN6_RC_CTL_RC6_ENABLE),
5125 rc_sw_target);
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05305126
5127 if (!(I915_READ(RC6_LOCATION) & RC6_CTX_IN_DRAM)) {
Imre Deakb99d49c2016-06-29 19:13:54 +03005128 DRM_DEBUG_DRIVER("RC6 Base location not set properly.\n");
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05305129 enable_rc6 = false;
5130 }
5131
5132 /*
5133 * The exact context size is not known for BXT, so assume a page size
5134 * for this check.
5135 */
5136 rc6_ctx_base = I915_READ(RC6_CTX_BASE) & RC6_CTX_BASE_MASK;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03005137 if (!((rc6_ctx_base >= ggtt->stolen_reserved_base) &&
5138 (rc6_ctx_base + PAGE_SIZE <= ggtt->stolen_reserved_base +
5139 ggtt->stolen_reserved_size))) {
Imre Deakb99d49c2016-06-29 19:13:54 +03005140 DRM_DEBUG_DRIVER("RC6 Base address not as expected.\n");
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05305141 enable_rc6 = false;
5142 }
5143
5144 if (!(((I915_READ(PWRCTX_MAXCNT_RCSUNIT) & IDLE_TIME_MASK) > 1) &&
5145 ((I915_READ(PWRCTX_MAXCNT_VCSUNIT0) & IDLE_TIME_MASK) > 1) &&
5146 ((I915_READ(PWRCTX_MAXCNT_BCSUNIT) & IDLE_TIME_MASK) > 1) &&
5147 ((I915_READ(PWRCTX_MAXCNT_VECSUNIT) & IDLE_TIME_MASK) > 1))) {
Imre Deakb99d49c2016-06-29 19:13:54 +03005148 DRM_DEBUG_DRIVER("Engine Idle wait time not set properly.\n");
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05305149 enable_rc6 = false;
5150 }
5151
Imre Deakfc619842016-06-29 19:13:55 +03005152 if (!I915_READ(GEN8_PUSHBUS_CONTROL) ||
5153 !I915_READ(GEN8_PUSHBUS_ENABLE) ||
5154 !I915_READ(GEN8_PUSHBUS_SHIFT)) {
5155 DRM_DEBUG_DRIVER("Pushbus not setup properly.\n");
5156 enable_rc6 = false;
5157 }
5158
5159 if (!I915_READ(GEN6_GFXPAUSE)) {
5160 DRM_DEBUG_DRIVER("GFX pause not setup properly.\n");
5161 enable_rc6 = false;
5162 }
5163
5164 if (!I915_READ(GEN8_MISC_CTRL0)) {
5165 DRM_DEBUG_DRIVER("GPM control not setup properly.\n");
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05305166 enable_rc6 = false;
5167 }
5168
5169 return enable_rc6;
5170}
5171
Chris Wilsondc979972016-05-10 14:10:04 +01005172int sanitize_rc6_option(struct drm_i915_private *dev_priv, int enable_rc6)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005173{
Daniel Vettere7d66d82015-06-15 23:23:54 +02005174 /* No RC6 before Ironlake and code is gone for ilk. */
Chris Wilsondc979972016-05-10 14:10:04 +01005175 if (INTEL_INFO(dev_priv)->gen < 6)
Imre Deake6069ca2014-04-18 16:01:02 +03005176 return 0;
5177
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05305178 if (!enable_rc6)
5179 return 0;
5180
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +02005181 if (IS_GEN9_LP(dev_priv) && !bxt_check_bios_rc6_setup(dev_priv)) {
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05305182 DRM_INFO("RC6 disabled by BIOS\n");
5183 return 0;
5184 }
5185
Daniel Vetter456470e2012-08-08 23:35:40 +02005186 /* Respect the kernel parameter if it is set */
Imre Deake6069ca2014-04-18 16:01:02 +03005187 if (enable_rc6 >= 0) {
5188 int mask;
5189
Chris Wilsondc979972016-05-10 14:10:04 +01005190 if (HAS_RC6p(dev_priv))
Imre Deake6069ca2014-04-18 16:01:02 +03005191 mask = INTEL_RC6_ENABLE | INTEL_RC6p_ENABLE |
5192 INTEL_RC6pp_ENABLE;
5193 else
5194 mask = INTEL_RC6_ENABLE;
5195
5196 if ((enable_rc6 & mask) != enable_rc6)
Imre Deakb99d49c2016-06-29 19:13:54 +03005197 DRM_DEBUG_DRIVER("Adjusting RC6 mask to %d "
5198 "(requested %d, valid %d)\n",
5199 enable_rc6 & mask, enable_rc6, mask);
Imre Deake6069ca2014-04-18 16:01:02 +03005200
5201 return enable_rc6 & mask;
5202 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005203
Chris Wilsondc979972016-05-10 14:10:04 +01005204 if (IS_IVYBRIDGE(dev_priv))
Ben Widawskycca84a12014-01-28 20:25:38 -08005205 return (INTEL_RC6_ENABLE | INTEL_RC6p_ENABLE);
Ben Widawsky8bade1a2014-01-28 20:25:39 -08005206
5207 return INTEL_RC6_ENABLE;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005208}
5209
Chris Wilsondc979972016-05-10 14:10:04 +01005210static void gen6_init_rps_frequencies(struct drm_i915_private *dev_priv)
Imre Deake6069ca2014-04-18 16:01:02 +03005211{
Ben Widawsky3280e8b2014-03-31 17:16:42 -07005212 /* All of these values are in units of 50MHz */
Chris Wilson773ea9a2016-07-13 09:10:33 +01005213
Tom O'Rourke93ee2922014-11-19 14:21:52 -08005214 /* static values from HW: RP0 > RP1 > RPn (min_freq) */
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +02005215 if (IS_GEN9_LP(dev_priv)) {
Chris Wilson773ea9a2016-07-13 09:10:33 +01005216 u32 rp_state_cap = I915_READ(BXT_RP_STATE_CAP);
Bob Paauwe35040562015-06-25 14:54:07 -07005217 dev_priv->rps.rp0_freq = (rp_state_cap >> 16) & 0xff;
5218 dev_priv->rps.rp1_freq = (rp_state_cap >> 8) & 0xff;
5219 dev_priv->rps.min_freq = (rp_state_cap >> 0) & 0xff;
5220 } else {
Chris Wilson773ea9a2016-07-13 09:10:33 +01005221 u32 rp_state_cap = I915_READ(GEN6_RP_STATE_CAP);
Bob Paauwe35040562015-06-25 14:54:07 -07005222 dev_priv->rps.rp0_freq = (rp_state_cap >> 0) & 0xff;
5223 dev_priv->rps.rp1_freq = (rp_state_cap >> 8) & 0xff;
5224 dev_priv->rps.min_freq = (rp_state_cap >> 16) & 0xff;
5225 }
Ben Widawsky3280e8b2014-03-31 17:16:42 -07005226 /* hw_max = RP0 until we check for overclocking */
Chris Wilson773ea9a2016-07-13 09:10:33 +01005227 dev_priv->rps.max_freq = dev_priv->rps.rp0_freq;
Ben Widawsky3280e8b2014-03-31 17:16:42 -07005228
Tom O'Rourke93ee2922014-11-19 14:21:52 -08005229 dev_priv->rps.efficient_freq = dev_priv->rps.rp1_freq;
Chris Wilsondc979972016-05-10 14:10:04 +01005230 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv) ||
5231 IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) {
Chris Wilson773ea9a2016-07-13 09:10:33 +01005232 u32 ddcc_status = 0;
5233
5234 if (sandybridge_pcode_read(dev_priv,
5235 HSW_PCODE_DYNAMIC_DUTY_CYCLE_CONTROL,
5236 &ddcc_status) == 0)
Tom O'Rourke93ee2922014-11-19 14:21:52 -08005237 dev_priv->rps.efficient_freq =
Tom O'Rourke46efa4a2015-02-10 23:06:46 -08005238 clamp_t(u8,
5239 ((ddcc_status >> 8) & 0xff),
5240 dev_priv->rps.min_freq,
5241 dev_priv->rps.max_freq);
Tom O'Rourke93ee2922014-11-19 14:21:52 -08005242 }
5243
Chris Wilsondc979972016-05-10 14:10:04 +01005244 if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) {
Akash Goelc5e06882015-06-29 14:50:19 +05305245 /* Store the frequency values in 16.66 MHZ units, which is
Chris Wilson773ea9a2016-07-13 09:10:33 +01005246 * the natural hardware unit for SKL
5247 */
Akash Goelc5e06882015-06-29 14:50:19 +05305248 dev_priv->rps.rp0_freq *= GEN9_FREQ_SCALER;
5249 dev_priv->rps.rp1_freq *= GEN9_FREQ_SCALER;
5250 dev_priv->rps.min_freq *= GEN9_FREQ_SCALER;
5251 dev_priv->rps.max_freq *= GEN9_FREQ_SCALER;
5252 dev_priv->rps.efficient_freq *= GEN9_FREQ_SCALER;
5253 }
Ben Widawsky3280e8b2014-03-31 17:16:42 -07005254}
5255
Chris Wilson3a45b052016-07-13 09:10:32 +01005256static void reset_rps(struct drm_i915_private *dev_priv,
5257 void (*set)(struct drm_i915_private *, u8))
5258{
5259 u8 freq = dev_priv->rps.cur_freq;
5260
5261 /* force a reset */
5262 dev_priv->rps.power = -1;
5263 dev_priv->rps.cur_freq = -1;
5264
5265 set(dev_priv, freq);
5266}
5267
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00005268/* See the Gen9_GT_PM_Programming_Guide doc for the below */
Chris Wilsondc979972016-05-10 14:10:04 +01005269static void gen9_enable_rps(struct drm_i915_private *dev_priv)
Zhe Wang20e49362014-11-04 17:07:05 +00005270{
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00005271 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
5272
Sagar Arun Kamble23eafea2015-08-23 17:52:48 +05305273 /* WaGsvDisableTurbo: Workaround to disable turbo on BXT A* */
Chris Wilsondc979972016-05-10 14:10:04 +01005274 if (IS_BXT_REVID(dev_priv, 0, BXT_REVID_A1)) {
Akash Goel2030d682016-04-23 00:05:45 +05305275 /*
5276 * BIOS could leave the Hw Turbo enabled, so need to explicitly
5277 * clear out the Control register just to avoid inconsitency
5278 * with debugfs interface, which will show Turbo as enabled
5279 * only and that is not expected by the User after adding the
5280 * WaGsvDisableTurbo. Apart from this there is no problem even
5281 * if the Turbo is left enabled in the Control register, as the
5282 * Up/Down interrupts would remain masked.
5283 */
Chris Wilsondc979972016-05-10 14:10:04 +01005284 gen9_disable_rps(dev_priv);
Sagar Arun Kamble23eafea2015-08-23 17:52:48 +05305285 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
5286 return;
5287 }
5288
Akash Goel0beb0592015-03-06 11:07:20 +05305289 /* Program defaults and thresholds for RPS*/
5290 I915_WRITE(GEN6_RC_VIDEO_FREQ,
5291 GEN9_FREQUENCY(dev_priv->rps.rp1_freq));
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00005292
Akash Goel0beb0592015-03-06 11:07:20 +05305293 /* 1 second timeout*/
5294 I915_WRITE(GEN6_RP_DOWN_TIMEOUT,
5295 GT_INTERVAL_FROM_US(dev_priv, 1000000));
5296
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00005297 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 0xa);
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00005298
Akash Goel0beb0592015-03-06 11:07:20 +05305299 /* Leaning on the below call to gen6_set_rps to program/setup the
5300 * Up/Down EI & threshold registers, as well as the RP_CONTROL,
5301 * RP_INTERRUPT_LIMITS & RPNSWREQ registers */
Chris Wilson3a45b052016-07-13 09:10:32 +01005302 reset_rps(dev_priv, gen6_set_rps);
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00005303
5304 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
5305}
5306
Chris Wilsondc979972016-05-10 14:10:04 +01005307static void gen9_enable_rc6(struct drm_i915_private *dev_priv)
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00005308{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00005309 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05305310 enum intel_engine_id id;
Zhe Wang20e49362014-11-04 17:07:05 +00005311 uint32_t rc6_mask = 0;
Zhe Wang20e49362014-11-04 17:07:05 +00005312
5313 /* 1a: Software RC state - RC0 */
5314 I915_WRITE(GEN6_RC_STATE, 0);
5315
5316 /* 1b: Get forcewake during program sequence. Although the driver
5317 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
Mika Kuoppala59bad942015-01-16 11:34:40 +02005318 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Zhe Wang20e49362014-11-04 17:07:05 +00005319
5320 /* 2a: Disable RC states. */
5321 I915_WRITE(GEN6_RC_CONTROL, 0);
5322
5323 /* 2b: Program RC6 thresholds.*/
Sagar Arun Kamble63a4dec2015-09-12 10:17:53 +05305324
5325 /* WaRsDoubleRc6WrlWithCoarsePowerGating: Doubling WRL only when CPG is enabled */
Chris Wilsondc979972016-05-10 14:10:04 +01005326 if (IS_SKYLAKE(dev_priv))
Sagar Arun Kamble63a4dec2015-09-12 10:17:53 +05305327 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 108 << 16);
5328 else
5329 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 54 << 16);
Zhe Wang20e49362014-11-04 17:07:05 +00005330 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
5331 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
Akash Goel3b3f1652016-10-13 22:44:48 +05305332 for_each_engine(engine, dev_priv, id)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00005333 I915_WRITE(RING_MAX_IDLE(engine->mmio_base), 10);
Sagar Arun Kamble97c322e2015-09-12 10:17:54 +05305334
Dave Gordon1a3d1892016-05-13 15:36:30 +01005335 if (HAS_GUC(dev_priv))
Sagar Arun Kamble97c322e2015-09-12 10:17:54 +05305336 I915_WRITE(GUC_MAX_IDLE_COUNT, 0xA);
5337
Zhe Wang20e49362014-11-04 17:07:05 +00005338 I915_WRITE(GEN6_RC_SLEEP, 0);
Zhe Wang20e49362014-11-04 17:07:05 +00005339
Zhe Wang38c23522015-01-20 12:23:04 +00005340 /* 2c: Program Coarse Power Gating Policies. */
5341 I915_WRITE(GEN9_MEDIA_PG_IDLE_HYSTERESIS, 25);
5342 I915_WRITE(GEN9_RENDER_PG_IDLE_HYSTERESIS, 25);
5343
Zhe Wang20e49362014-11-04 17:07:05 +00005344 /* 3a: Enable RC6 */
Chris Wilsondc979972016-05-10 14:10:04 +01005345 if (intel_enable_rc6() & INTEL_RC6_ENABLE)
Zhe Wang20e49362014-11-04 17:07:05 +00005346 rc6_mask = GEN6_RC_CTL_RC6_ENABLE;
Jani Nikula87ad3212016-01-14 12:53:34 +02005347 DRM_INFO("RC6 %s\n", onoff(rc6_mask & GEN6_RC_CTL_RC6_ENABLE));
Jani Nikula4ff40a42016-09-26 15:07:51 +03005348 /* WaRsUseTimeoutMode:bxt */
Jani Nikula9fc736e2016-09-16 16:59:46 +03005349 if (IS_BXT_REVID(dev_priv, 0, BXT_REVID_A1)) {
Sagar Arun Kamble3e7732a2015-10-01 20:29:27 +05305350 I915_WRITE(GEN6_RC6_THRESHOLD, 625); /* 800us */
Sagar Arun Kamblee3429cd2015-09-12 10:17:52 +05305351 I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
5352 GEN7_RC_CTL_TO_MODE |
5353 rc6_mask);
Sagar Arun Kamble3e7732a2015-10-01 20:29:27 +05305354 } else {
5355 I915_WRITE(GEN6_RC6_THRESHOLD, 37500); /* 37.5/125ms per EI */
Sagar Arun Kamblee3429cd2015-09-12 10:17:52 +05305356 I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
5357 GEN6_RC_CTL_EI_MODE(1) |
5358 rc6_mask);
Sagar Arun Kamble3e7732a2015-10-01 20:29:27 +05305359 }
Zhe Wang20e49362014-11-04 17:07:05 +00005360
Sagar Kamblecb07bae2015-04-12 11:28:14 +05305361 /*
5362 * 3b: Enable Coarse Power Gating only when RC6 is enabled.
Sagar Arun Kamblef2d2fe92015-09-12 10:17:51 +05305363 * WaRsDisableCoarsePowerGating:skl,bxt - Render/Media PG need to be disabled with RC6.
Sagar Kamblecb07bae2015-04-12 11:28:14 +05305364 */
Chris Wilsondc979972016-05-10 14:10:04 +01005365 if (NEEDS_WaRsDisableCoarsePowerGating(dev_priv))
Sagar Arun Kamblef2d2fe92015-09-12 10:17:51 +05305366 I915_WRITE(GEN9_PG_ENABLE, 0);
5367 else
5368 I915_WRITE(GEN9_PG_ENABLE, (rc6_mask & GEN6_RC_CTL_RC6_ENABLE) ?
5369 (GEN9_RENDER_PG_ENABLE | GEN9_MEDIA_PG_ENABLE) : 0);
Zhe Wang38c23522015-01-20 12:23:04 +00005370
Mika Kuoppala59bad942015-01-16 11:34:40 +02005371 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Zhe Wang20e49362014-11-04 17:07:05 +00005372}
5373
Chris Wilsondc979972016-05-10 14:10:04 +01005374static void gen8_enable_rps(struct drm_i915_private *dev_priv)
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005375{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00005376 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05305377 enum intel_engine_id id;
Tom O'Rourke93ee2922014-11-19 14:21:52 -08005378 uint32_t rc6_mask = 0;
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005379
5380 /* 1a: Software RC state - RC0 */
5381 I915_WRITE(GEN6_RC_STATE, 0);
5382
5383 /* 1c & 1d: Get forcewake during program sequence. Although the driver
5384 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
Mika Kuoppala59bad942015-01-16 11:34:40 +02005385 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005386
5387 /* 2a: Disable RC states. */
5388 I915_WRITE(GEN6_RC_CONTROL, 0);
5389
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005390 /* 2b: Program RC6 thresholds.*/
5391 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16);
5392 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
5393 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
Akash Goel3b3f1652016-10-13 22:44:48 +05305394 for_each_engine(engine, dev_priv, id)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00005395 I915_WRITE(RING_MAX_IDLE(engine->mmio_base), 10);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005396 I915_WRITE(GEN6_RC_SLEEP, 0);
Chris Wilsondc979972016-05-10 14:10:04 +01005397 if (IS_BROADWELL(dev_priv))
Tom O'Rourke0d68b252014-04-09 11:44:06 -07005398 I915_WRITE(GEN6_RC6_THRESHOLD, 625); /* 800us/1.28 for TO */
5399 else
5400 I915_WRITE(GEN6_RC6_THRESHOLD, 50000); /* 50/125ms per EI */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005401
5402 /* 3: Enable RC6 */
Chris Wilsondc979972016-05-10 14:10:04 +01005403 if (intel_enable_rc6() & INTEL_RC6_ENABLE)
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005404 rc6_mask = GEN6_RC_CTL_RC6_ENABLE;
Chris Wilsondc979972016-05-10 14:10:04 +01005405 intel_print_rc6_info(dev_priv, rc6_mask);
5406 if (IS_BROADWELL(dev_priv))
Tom O'Rourke0d68b252014-04-09 11:44:06 -07005407 I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
5408 GEN7_RC_CTL_TO_MODE |
5409 rc6_mask);
5410 else
5411 I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
5412 GEN6_RC_CTL_EI_MODE(1) |
5413 rc6_mask);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005414
5415 /* 4 Program defaults and thresholds for RPS*/
Ben Widawskyf9bdc582014-03-31 17:16:41 -07005416 I915_WRITE(GEN6_RPNSWREQ,
5417 HSW_FREQUENCY(dev_priv->rps.rp1_freq));
5418 I915_WRITE(GEN6_RC_VIDEO_FREQ,
5419 HSW_FREQUENCY(dev_priv->rps.rp1_freq));
Daniel Vetter7526ed72014-09-29 15:07:19 +02005420 /* NB: Docs say 1s, and 1000000 - which aren't equivalent */
5421 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 100000000 / 128); /* 1 second timeout */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005422
Daniel Vetter7526ed72014-09-29 15:07:19 +02005423 /* Docs recommend 900MHz, and 300 MHz respectively */
5424 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS,
5425 dev_priv->rps.max_freq_softlimit << 24 |
5426 dev_priv->rps.min_freq_softlimit << 16);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005427
Daniel Vetter7526ed72014-09-29 15:07:19 +02005428 I915_WRITE(GEN6_RP_UP_THRESHOLD, 7600000 / 128); /* 76ms busyness per EI, 90% */
5429 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 31300000 / 128); /* 313ms busyness per EI, 70%*/
5430 I915_WRITE(GEN6_RP_UP_EI, 66000); /* 84.48ms, XXX: random? */
5431 I915_WRITE(GEN6_RP_DOWN_EI, 350000); /* 448ms, XXX: random? */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005432
Daniel Vetter7526ed72014-09-29 15:07:19 +02005433 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005434
5435 /* 5: Enable RPS */
Daniel Vetter7526ed72014-09-29 15:07:19 +02005436 I915_WRITE(GEN6_RP_CONTROL,
5437 GEN6_RP_MEDIA_TURBO |
5438 GEN6_RP_MEDIA_HW_NORMAL_MODE |
5439 GEN6_RP_MEDIA_IS_GFX |
5440 GEN6_RP_ENABLE |
5441 GEN6_RP_UP_BUSY_AVG |
5442 GEN6_RP_DOWN_IDLE_AVG);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005443
Daniel Vetter7526ed72014-09-29 15:07:19 +02005444 /* 6: Ring frequency + overclocking (our driver does this later */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005445
Chris Wilson3a45b052016-07-13 09:10:32 +01005446 reset_rps(dev_priv, gen6_set_rps);
Daniel Vetter7526ed72014-09-29 15:07:19 +02005447
Mika Kuoppala59bad942015-01-16 11:34:40 +02005448 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005449}
5450
Chris Wilsondc979972016-05-10 14:10:04 +01005451static void gen6_enable_rps(struct drm_i915_private *dev_priv)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005452{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00005453 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05305454 enum intel_engine_id id;
Chris Wilson99ac9612016-07-13 09:10:34 +01005455 u32 rc6vids, rc6_mask = 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005456 u32 gtfifodbg;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005457 int rc6_mode;
Dave Gordonb4ac5af2016-03-24 11:20:38 +00005458 int ret;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005459
Jesse Barnes4fc688c2012-11-02 11:14:01 -07005460 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02005461
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005462 /* Here begins a magic sequence of register writes to enable
5463 * auto-downclocking.
5464 *
5465 * Perhaps there might be some value in exposing these to
5466 * userspace...
5467 */
5468 I915_WRITE(GEN6_RC_STATE, 0);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005469
5470 /* Clear the DBG now so we don't confuse earlier errors */
Ville Syrjälä297b32e2016-04-13 21:09:30 +03005471 gtfifodbg = I915_READ(GTFIFODBG);
5472 if (gtfifodbg) {
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005473 DRM_ERROR("GT fifo had a previous error %x\n", gtfifodbg);
5474 I915_WRITE(GTFIFODBG, gtfifodbg);
5475 }
5476
Mika Kuoppala59bad942015-01-16 11:34:40 +02005477 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005478
5479 /* disable the counters and set deterministic thresholds */
5480 I915_WRITE(GEN6_RC_CONTROL, 0);
5481
5482 I915_WRITE(GEN6_RC1_WAKE_RATE_LIMIT, 1000 << 16);
5483 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16 | 30);
5484 I915_WRITE(GEN6_RC6pp_WAKE_RATE_LIMIT, 30);
5485 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
5486 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
5487
Akash Goel3b3f1652016-10-13 22:44:48 +05305488 for_each_engine(engine, dev_priv, id)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00005489 I915_WRITE(RING_MAX_IDLE(engine->mmio_base), 10);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005490
5491 I915_WRITE(GEN6_RC_SLEEP, 0);
5492 I915_WRITE(GEN6_RC1e_THRESHOLD, 1000);
Chris Wilsondc979972016-05-10 14:10:04 +01005493 if (IS_IVYBRIDGE(dev_priv))
Stéphane Marchesin351aa562013-08-13 11:55:17 -07005494 I915_WRITE(GEN6_RC6_THRESHOLD, 125000);
5495 else
5496 I915_WRITE(GEN6_RC6_THRESHOLD, 50000);
Stéphane Marchesin0920a482013-01-29 19:41:59 -08005497 I915_WRITE(GEN6_RC6p_THRESHOLD, 150000);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005498 I915_WRITE(GEN6_RC6pp_THRESHOLD, 64000); /* unused */
5499
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03005500 /* Check if we are enabling RC6 */
Chris Wilsondc979972016-05-10 14:10:04 +01005501 rc6_mode = intel_enable_rc6();
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005502 if (rc6_mode & INTEL_RC6_ENABLE)
5503 rc6_mask |= GEN6_RC_CTL_RC6_ENABLE;
5504
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03005505 /* We don't use those on Haswell */
Chris Wilsondc979972016-05-10 14:10:04 +01005506 if (!IS_HASWELL(dev_priv)) {
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03005507 if (rc6_mode & INTEL_RC6p_ENABLE)
5508 rc6_mask |= GEN6_RC_CTL_RC6p_ENABLE;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005509
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03005510 if (rc6_mode & INTEL_RC6pp_ENABLE)
5511 rc6_mask |= GEN6_RC_CTL_RC6pp_ENABLE;
5512 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005513
Chris Wilsondc979972016-05-10 14:10:04 +01005514 intel_print_rc6_info(dev_priv, rc6_mask);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005515
5516 I915_WRITE(GEN6_RC_CONTROL,
5517 rc6_mask |
5518 GEN6_RC_CTL_EI_MODE(1) |
5519 GEN6_RC_CTL_HW_ENABLE);
5520
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005521 /* Power down if completely idle for over 50ms */
5522 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 50000);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005523 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005524
Chris Wilson3a45b052016-07-13 09:10:32 +01005525 reset_rps(dev_priv, gen6_set_rps);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005526
Ben Widawsky31643d52012-09-26 10:34:01 -07005527 rc6vids = 0;
5528 ret = sandybridge_pcode_read(dev_priv, GEN6_PCODE_READ_RC6VIDS, &rc6vids);
Chris Wilsondc979972016-05-10 14:10:04 +01005529 if (IS_GEN6(dev_priv) && ret) {
Ben Widawsky31643d52012-09-26 10:34:01 -07005530 DRM_DEBUG_DRIVER("Couldn't check for BIOS workaround\n");
Chris Wilsondc979972016-05-10 14:10:04 +01005531 } else if (IS_GEN6(dev_priv) && (GEN6_DECODE_RC6_VID(rc6vids & 0xff) < 450)) {
Ben Widawsky31643d52012-09-26 10:34:01 -07005532 DRM_DEBUG_DRIVER("You should update your BIOS. Correcting minimum rc6 voltage (%dmV->%dmV)\n",
5533 GEN6_DECODE_RC6_VID(rc6vids & 0xff), 450);
5534 rc6vids &= 0xffff00;
5535 rc6vids |= GEN6_ENCODE_RC6_VID(450);
5536 ret = sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_RC6VIDS, rc6vids);
5537 if (ret)
5538 DRM_ERROR("Couldn't fix incorrect rc6 voltage\n");
5539 }
5540
Mika Kuoppala59bad942015-01-16 11:34:40 +02005541 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005542}
5543
Chris Wilsonfb7404e2016-07-13 09:10:38 +01005544static void gen6_update_ring_freq(struct drm_i915_private *dev_priv)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005545{
5546 int min_freq = 15;
Chris Wilson3ebecd02013-04-12 19:10:13 +01005547 unsigned int gpu_freq;
5548 unsigned int max_ia_freq, min_ring_freq;
Akash Goel4c8c7742015-06-29 14:50:20 +05305549 unsigned int max_gpu_freq, min_gpu_freq;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005550 int scaling_factor = 180;
Ben Widawskyeda79642013-10-07 17:15:48 -03005551 struct cpufreq_policy *policy;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005552
Jesse Barnes4fc688c2012-11-02 11:14:01 -07005553 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02005554
Ben Widawskyeda79642013-10-07 17:15:48 -03005555 policy = cpufreq_cpu_get(0);
5556 if (policy) {
5557 max_ia_freq = policy->cpuinfo.max_freq;
5558 cpufreq_cpu_put(policy);
5559 } else {
5560 /*
5561 * Default to measured freq if none found, PCU will ensure we
5562 * don't go over
5563 */
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005564 max_ia_freq = tsc_khz;
Ben Widawskyeda79642013-10-07 17:15:48 -03005565 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005566
5567 /* Convert from kHz to MHz */
5568 max_ia_freq /= 1000;
5569
Ben Widawsky153b4b952013-10-22 22:05:09 -07005570 min_ring_freq = I915_READ(DCLK) & 0xf;
Ben Widawskyf6aca452013-10-02 09:25:02 -07005571 /* convert DDR frequency from units of 266.6MHz to bandwidth */
5572 min_ring_freq = mult_frac(min_ring_freq, 8, 3);
Chris Wilson3ebecd02013-04-12 19:10:13 +01005573
Chris Wilsondc979972016-05-10 14:10:04 +01005574 if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) {
Akash Goel4c8c7742015-06-29 14:50:20 +05305575 /* Convert GT frequency to 50 HZ units */
5576 min_gpu_freq = dev_priv->rps.min_freq / GEN9_FREQ_SCALER;
5577 max_gpu_freq = dev_priv->rps.max_freq / GEN9_FREQ_SCALER;
5578 } else {
5579 min_gpu_freq = dev_priv->rps.min_freq;
5580 max_gpu_freq = dev_priv->rps.max_freq;
5581 }
5582
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005583 /*
5584 * For each potential GPU frequency, load a ring frequency we'd like
5585 * to use for memory access. We do this by specifying the IA frequency
5586 * the PCU should use as a reference to determine the ring frequency.
5587 */
Akash Goel4c8c7742015-06-29 14:50:20 +05305588 for (gpu_freq = max_gpu_freq; gpu_freq >= min_gpu_freq; gpu_freq--) {
5589 int diff = max_gpu_freq - gpu_freq;
Chris Wilson3ebecd02013-04-12 19:10:13 +01005590 unsigned int ia_freq = 0, ring_freq = 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005591
Chris Wilsondc979972016-05-10 14:10:04 +01005592 if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) {
Akash Goel4c8c7742015-06-29 14:50:20 +05305593 /*
5594 * ring_freq = 2 * GT. ring_freq is in 100MHz units
5595 * No floor required for ring frequency on SKL.
5596 */
5597 ring_freq = gpu_freq;
Chris Wilsondc979972016-05-10 14:10:04 +01005598 } else if (INTEL_INFO(dev_priv)->gen >= 8) {
Ben Widawsky46c764d2013-11-02 21:07:49 -07005599 /* max(2 * GT, DDR). NB: GT is 50MHz units */
5600 ring_freq = max(min_ring_freq, gpu_freq);
Chris Wilsondc979972016-05-10 14:10:04 +01005601 } else if (IS_HASWELL(dev_priv)) {
Ben Widawskyf6aca452013-10-02 09:25:02 -07005602 ring_freq = mult_frac(gpu_freq, 5, 4);
Chris Wilson3ebecd02013-04-12 19:10:13 +01005603 ring_freq = max(min_ring_freq, ring_freq);
5604 /* leave ia_freq as the default, chosen by cpufreq */
5605 } else {
5606 /* On older processors, there is no separate ring
5607 * clock domain, so in order to boost the bandwidth
5608 * of the ring, we need to upclock the CPU (ia_freq).
5609 *
5610 * For GPU frequencies less than 750MHz,
5611 * just use the lowest ring freq.
5612 */
5613 if (gpu_freq < min_freq)
5614 ia_freq = 800;
5615 else
5616 ia_freq = max_ia_freq - ((diff * scaling_factor) / 2);
5617 ia_freq = DIV_ROUND_CLOSEST(ia_freq, 100);
5618 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005619
Ben Widawsky42c05262012-09-26 10:34:00 -07005620 sandybridge_pcode_write(dev_priv,
5621 GEN6_PCODE_WRITE_MIN_FREQ_TABLE,
Chris Wilson3ebecd02013-04-12 19:10:13 +01005622 ia_freq << GEN6_PCODE_FREQ_IA_RATIO_SHIFT |
5623 ring_freq << GEN6_PCODE_FREQ_RING_RATIO_SHIFT |
5624 gpu_freq);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005625 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005626}
5627
Ville Syrjälä03af2042014-06-28 02:03:53 +03005628static int cherryview_rps_max_freq(struct drm_i915_private *dev_priv)
Deepak S2b6b3a02014-05-27 15:59:30 +05305629{
5630 u32 val, rp0;
5631
Jani Nikula5b5929c2015-10-07 11:17:46 +03005632 val = vlv_punit_read(dev_priv, FB_GFX_FMAX_AT_VMAX_FUSE);
Deepak S2b6b3a02014-05-27 15:59:30 +05305633
Imre Deak43b67992016-08-31 19:13:02 +03005634 switch (INTEL_INFO(dev_priv)->sseu.eu_total) {
Jani Nikula5b5929c2015-10-07 11:17:46 +03005635 case 8:
5636 /* (2 * 4) config */
5637 rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS4EU_FUSE_SHIFT);
5638 break;
5639 case 12:
5640 /* (2 * 6) config */
5641 rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS6EU_FUSE_SHIFT);
5642 break;
5643 case 16:
5644 /* (2 * 8) config */
5645 default:
5646 /* Setting (2 * 8) Min RP0 for any other combination */
5647 rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS8EU_FUSE_SHIFT);
5648 break;
Deepak S095acd52015-01-17 11:05:59 +05305649 }
Jani Nikula5b5929c2015-10-07 11:17:46 +03005650
5651 rp0 = (rp0 & FB_GFX_FREQ_FUSE_MASK);
5652
Deepak S2b6b3a02014-05-27 15:59:30 +05305653 return rp0;
5654}
5655
5656static int cherryview_rps_rpe_freq(struct drm_i915_private *dev_priv)
5657{
5658 u32 val, rpe;
5659
5660 val = vlv_punit_read(dev_priv, PUNIT_GPU_DUTYCYCLE_REG);
5661 rpe = (val >> PUNIT_GPU_DUTYCYCLE_RPE_FREQ_SHIFT) & PUNIT_GPU_DUTYCYCLE_RPE_FREQ_MASK;
5662
5663 return rpe;
5664}
5665
Deepak S7707df42014-07-12 18:46:14 +05305666static int cherryview_rps_guar_freq(struct drm_i915_private *dev_priv)
5667{
5668 u32 val, rp1;
5669
Jani Nikula5b5929c2015-10-07 11:17:46 +03005670 val = vlv_punit_read(dev_priv, FB_GFX_FMAX_AT_VMAX_FUSE);
5671 rp1 = (val & FB_GFX_FREQ_FUSE_MASK);
5672
Deepak S7707df42014-07-12 18:46:14 +05305673 return rp1;
5674}
5675
Deepak Sf8f2b002014-07-10 13:16:21 +05305676static int valleyview_rps_guar_freq(struct drm_i915_private *dev_priv)
5677{
5678 u32 val, rp1;
5679
5680 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FREQ_FUSE);
5681
5682 rp1 = (val & FB_GFX_FGUARANTEED_FREQ_FUSE_MASK) >> FB_GFX_FGUARANTEED_FREQ_FUSE_SHIFT;
5683
5684 return rp1;
5685}
5686
Ville Syrjälä03af2042014-06-28 02:03:53 +03005687static int valleyview_rps_max_freq(struct drm_i915_private *dev_priv)
Jesse Barnes0a073b82013-04-17 15:54:58 -07005688{
5689 u32 val, rp0;
5690
Jani Nikula64936252013-05-22 15:36:20 +03005691 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FREQ_FUSE);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005692
5693 rp0 = (val & FB_GFX_MAX_FREQ_FUSE_MASK) >> FB_GFX_MAX_FREQ_FUSE_SHIFT;
5694 /* Clamp to max */
5695 rp0 = min_t(u32, rp0, 0xea);
5696
5697 return rp0;
5698}
5699
5700static int valleyview_rps_rpe_freq(struct drm_i915_private *dev_priv)
5701{
5702 u32 val, rpe;
5703
Jani Nikula64936252013-05-22 15:36:20 +03005704 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FMAX_FUSE_LO);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005705 rpe = (val & FB_FMAX_VMIN_FREQ_LO_MASK) >> FB_FMAX_VMIN_FREQ_LO_SHIFT;
Jani Nikula64936252013-05-22 15:36:20 +03005706 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FMAX_FUSE_HI);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005707 rpe |= (val & FB_FMAX_VMIN_FREQ_HI_MASK) << 5;
5708
5709 return rpe;
5710}
5711
Ville Syrjälä03af2042014-06-28 02:03:53 +03005712static int valleyview_rps_min_freq(struct drm_i915_private *dev_priv)
Jesse Barnes0a073b82013-04-17 15:54:58 -07005713{
Imre Deak36146032014-12-04 18:39:35 +02005714 u32 val;
5715
5716 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_LFM) & 0xff;
5717 /*
5718 * According to the BYT Punit GPU turbo HAS 1.1.6.3 the minimum value
5719 * for the minimum frequency in GPLL mode is 0xc1. Contrary to this on
5720 * a BYT-M B0 the above register contains 0xbf. Moreover when setting
5721 * a frequency Punit will not allow values below 0xc0. Clamp it 0xc0
5722 * to make sure it matches what Punit accepts.
5723 */
5724 return max_t(u32, val, 0xc0);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005725}
5726
Imre Deakae484342014-03-31 15:10:44 +03005727/* Check that the pctx buffer wasn't move under us. */
5728static void valleyview_check_pctx(struct drm_i915_private *dev_priv)
5729{
5730 unsigned long pctx_addr = I915_READ(VLV_PCBR) & ~4095;
5731
5732 WARN_ON(pctx_addr != dev_priv->mm.stolen_base +
5733 dev_priv->vlv_pctx->stolen->start);
5734}
5735
Deepak S38807742014-05-23 21:00:15 +05305736
5737/* Check that the pcbr address is not empty. */
5738static void cherryview_check_pctx(struct drm_i915_private *dev_priv)
5739{
5740 unsigned long pctx_addr = I915_READ(VLV_PCBR) & ~4095;
5741
5742 WARN_ON((pctx_addr >> VLV_PCBR_ADDR_SHIFT) == 0);
5743}
5744
Chris Wilsondc979972016-05-10 14:10:04 +01005745static void cherryview_setup_pctx(struct drm_i915_private *dev_priv)
Deepak S38807742014-05-23 21:00:15 +05305746{
Joonas Lahtinen62106b42016-03-18 10:42:57 +02005747 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03005748 unsigned long pctx_paddr, paddr;
Deepak S38807742014-05-23 21:00:15 +05305749 u32 pcbr;
5750 int pctx_size = 32*1024;
5751
Deepak S38807742014-05-23 21:00:15 +05305752 pcbr = I915_READ(VLV_PCBR);
5753 if ((pcbr >> VLV_PCBR_ADDR_SHIFT) == 0) {
Ville Syrjäläce611ef2014-11-07 21:33:46 +02005754 DRM_DEBUG_DRIVER("BIOS didn't set up PCBR, fixing up\n");
Deepak S38807742014-05-23 21:00:15 +05305755 paddr = (dev_priv->mm.stolen_base +
Joonas Lahtinen62106b42016-03-18 10:42:57 +02005756 (ggtt->stolen_size - pctx_size));
Deepak S38807742014-05-23 21:00:15 +05305757
5758 pctx_paddr = (paddr & (~4095));
5759 I915_WRITE(VLV_PCBR, pctx_paddr);
5760 }
Ville Syrjäläce611ef2014-11-07 21:33:46 +02005761
5762 DRM_DEBUG_DRIVER("PCBR: 0x%08x\n", I915_READ(VLV_PCBR));
Deepak S38807742014-05-23 21:00:15 +05305763}
5764
Chris Wilsondc979972016-05-10 14:10:04 +01005765static void valleyview_setup_pctx(struct drm_i915_private *dev_priv)
Jesse Barnesc9cddff2013-05-08 10:45:13 -07005766{
Jesse Barnesc9cddff2013-05-08 10:45:13 -07005767 struct drm_i915_gem_object *pctx;
5768 unsigned long pctx_paddr;
5769 u32 pcbr;
5770 int pctx_size = 24*1024;
5771
5772 pcbr = I915_READ(VLV_PCBR);
5773 if (pcbr) {
5774 /* BIOS set it up already, grab the pre-alloc'd space */
5775 int pcbr_offset;
5776
5777 pcbr_offset = (pcbr & (~4095)) - dev_priv->mm.stolen_base;
Tvrtko Ursulin187685c2016-12-01 14:16:36 +00005778 pctx = i915_gem_object_create_stolen_for_preallocated(dev_priv,
Jesse Barnesc9cddff2013-05-08 10:45:13 -07005779 pcbr_offset,
Daniel Vetter190d6cd2013-07-04 13:06:28 +02005780 I915_GTT_OFFSET_NONE,
Jesse Barnesc9cddff2013-05-08 10:45:13 -07005781 pctx_size);
5782 goto out;
5783 }
5784
Ville Syrjäläce611ef2014-11-07 21:33:46 +02005785 DRM_DEBUG_DRIVER("BIOS didn't set up PCBR, fixing up\n");
5786
Jesse Barnesc9cddff2013-05-08 10:45:13 -07005787 /*
5788 * From the Gunit register HAS:
5789 * The Gfx driver is expected to program this register and ensure
5790 * proper allocation within Gfx stolen memory. For example, this
5791 * register should be programmed such than the PCBR range does not
5792 * overlap with other ranges, such as the frame buffer, protected
5793 * memory, or any other relevant ranges.
5794 */
Tvrtko Ursulin187685c2016-12-01 14:16:36 +00005795 pctx = i915_gem_object_create_stolen(dev_priv, pctx_size);
Jesse Barnesc9cddff2013-05-08 10:45:13 -07005796 if (!pctx) {
5797 DRM_DEBUG("not enough stolen space for PCTX, disabling\n");
Tvrtko Ursulinee504892016-02-11 10:27:30 +00005798 goto out;
Jesse Barnesc9cddff2013-05-08 10:45:13 -07005799 }
5800
5801 pctx_paddr = dev_priv->mm.stolen_base + pctx->stolen->start;
5802 I915_WRITE(VLV_PCBR, pctx_paddr);
5803
5804out:
Ville Syrjäläce611ef2014-11-07 21:33:46 +02005805 DRM_DEBUG_DRIVER("PCBR: 0x%08x\n", I915_READ(VLV_PCBR));
Jesse Barnesc9cddff2013-05-08 10:45:13 -07005806 dev_priv->vlv_pctx = pctx;
5807}
5808
Chris Wilsondc979972016-05-10 14:10:04 +01005809static void valleyview_cleanup_pctx(struct drm_i915_private *dev_priv)
Imre Deakae484342014-03-31 15:10:44 +03005810{
Imre Deakae484342014-03-31 15:10:44 +03005811 if (WARN_ON(!dev_priv->vlv_pctx))
5812 return;
5813
Chris Wilsonf0cd5182016-10-28 13:58:43 +01005814 i915_gem_object_put(dev_priv->vlv_pctx);
Imre Deakae484342014-03-31 15:10:44 +03005815 dev_priv->vlv_pctx = NULL;
5816}
5817
Ville Syrjäläc30fec62016-03-04 21:43:02 +02005818static void vlv_init_gpll_ref_freq(struct drm_i915_private *dev_priv)
5819{
5820 dev_priv->rps.gpll_ref_freq =
5821 vlv_get_cck_clock(dev_priv, "GPLL ref",
5822 CCK_GPLL_CLOCK_CONTROL,
5823 dev_priv->czclk_freq);
5824
5825 DRM_DEBUG_DRIVER("GPLL reference freq: %d kHz\n",
5826 dev_priv->rps.gpll_ref_freq);
5827}
5828
Chris Wilsondc979972016-05-10 14:10:04 +01005829static void valleyview_init_gt_powersave(struct drm_i915_private *dev_priv)
Imre Deak4e805192014-04-14 20:24:41 +03005830{
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03005831 u32 val;
Imre Deak4e805192014-04-14 20:24:41 +03005832
Chris Wilsondc979972016-05-10 14:10:04 +01005833 valleyview_setup_pctx(dev_priv);
Imre Deak4e805192014-04-14 20:24:41 +03005834
Ville Syrjäläc30fec62016-03-04 21:43:02 +02005835 vlv_init_gpll_ref_freq(dev_priv);
5836
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03005837 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
5838 switch ((val >> 6) & 3) {
5839 case 0:
5840 case 1:
5841 dev_priv->mem_freq = 800;
5842 break;
5843 case 2:
5844 dev_priv->mem_freq = 1066;
5845 break;
5846 case 3:
5847 dev_priv->mem_freq = 1333;
5848 break;
5849 }
Ville Syrjälä80b83b62014-11-10 22:55:14 +02005850 DRM_DEBUG_DRIVER("DDR speed: %d MHz\n", dev_priv->mem_freq);
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03005851
Imre Deak4e805192014-04-14 20:24:41 +03005852 dev_priv->rps.max_freq = valleyview_rps_max_freq(dev_priv);
5853 dev_priv->rps.rp0_freq = dev_priv->rps.max_freq;
5854 DRM_DEBUG_DRIVER("max GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02005855 intel_gpu_freq(dev_priv, dev_priv->rps.max_freq),
Imre Deak4e805192014-04-14 20:24:41 +03005856 dev_priv->rps.max_freq);
5857
5858 dev_priv->rps.efficient_freq = valleyview_rps_rpe_freq(dev_priv);
5859 DRM_DEBUG_DRIVER("RPe GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02005860 intel_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
Imre Deak4e805192014-04-14 20:24:41 +03005861 dev_priv->rps.efficient_freq);
5862
Deepak Sf8f2b002014-07-10 13:16:21 +05305863 dev_priv->rps.rp1_freq = valleyview_rps_guar_freq(dev_priv);
5864 DRM_DEBUG_DRIVER("RP1(Guar Freq) GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02005865 intel_gpu_freq(dev_priv, dev_priv->rps.rp1_freq),
Deepak Sf8f2b002014-07-10 13:16:21 +05305866 dev_priv->rps.rp1_freq);
5867
Imre Deak4e805192014-04-14 20:24:41 +03005868 dev_priv->rps.min_freq = valleyview_rps_min_freq(dev_priv);
5869 DRM_DEBUG_DRIVER("min GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02005870 intel_gpu_freq(dev_priv, dev_priv->rps.min_freq),
Imre Deak4e805192014-04-14 20:24:41 +03005871 dev_priv->rps.min_freq);
Imre Deak4e805192014-04-14 20:24:41 +03005872}
5873
Chris Wilsondc979972016-05-10 14:10:04 +01005874static void cherryview_init_gt_powersave(struct drm_i915_private *dev_priv)
Deepak S38807742014-05-23 21:00:15 +05305875{
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03005876 u32 val;
Deepak S2b6b3a02014-05-27 15:59:30 +05305877
Chris Wilsondc979972016-05-10 14:10:04 +01005878 cherryview_setup_pctx(dev_priv);
Deepak S2b6b3a02014-05-27 15:59:30 +05305879
Ville Syrjäläc30fec62016-03-04 21:43:02 +02005880 vlv_init_gpll_ref_freq(dev_priv);
5881
Ville Syrjäläa5805162015-05-26 20:42:30 +03005882 mutex_lock(&dev_priv->sb_lock);
Ville Syrjäläc6e8f392014-11-07 21:33:43 +02005883 val = vlv_cck_read(dev_priv, CCK_FUSE_REG);
Ville Syrjäläa5805162015-05-26 20:42:30 +03005884 mutex_unlock(&dev_priv->sb_lock);
Ville Syrjäläc6e8f392014-11-07 21:33:43 +02005885
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03005886 switch ((val >> 2) & 0x7) {
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03005887 case 3:
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03005888 dev_priv->mem_freq = 2000;
5889 break;
Ville Syrjäläbfa7df02015-09-24 23:29:18 +03005890 default:
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03005891 dev_priv->mem_freq = 1600;
5892 break;
5893 }
Ville Syrjälä80b83b62014-11-10 22:55:14 +02005894 DRM_DEBUG_DRIVER("DDR speed: %d MHz\n", dev_priv->mem_freq);
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03005895
Deepak S2b6b3a02014-05-27 15:59:30 +05305896 dev_priv->rps.max_freq = cherryview_rps_max_freq(dev_priv);
5897 dev_priv->rps.rp0_freq = dev_priv->rps.max_freq;
5898 DRM_DEBUG_DRIVER("max GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02005899 intel_gpu_freq(dev_priv, dev_priv->rps.max_freq),
Deepak S2b6b3a02014-05-27 15:59:30 +05305900 dev_priv->rps.max_freq);
5901
5902 dev_priv->rps.efficient_freq = cherryview_rps_rpe_freq(dev_priv);
5903 DRM_DEBUG_DRIVER("RPe GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02005904 intel_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
Deepak S2b6b3a02014-05-27 15:59:30 +05305905 dev_priv->rps.efficient_freq);
5906
Deepak S7707df42014-07-12 18:46:14 +05305907 dev_priv->rps.rp1_freq = cherryview_rps_guar_freq(dev_priv);
5908 DRM_DEBUG_DRIVER("RP1(Guar) GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02005909 intel_gpu_freq(dev_priv, dev_priv->rps.rp1_freq),
Deepak S7707df42014-07-12 18:46:14 +05305910 dev_priv->rps.rp1_freq);
5911
Deepak S5b7c91b2015-05-09 18:15:46 +05305912 /* PUnit validated range is only [RPe, RP0] */
5913 dev_priv->rps.min_freq = dev_priv->rps.efficient_freq;
Deepak S2b6b3a02014-05-27 15:59:30 +05305914 DRM_DEBUG_DRIVER("min GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02005915 intel_gpu_freq(dev_priv, dev_priv->rps.min_freq),
Deepak S2b6b3a02014-05-27 15:59:30 +05305916 dev_priv->rps.min_freq);
5917
Ville Syrjälä1c147622014-08-18 14:42:43 +03005918 WARN_ONCE((dev_priv->rps.max_freq |
5919 dev_priv->rps.efficient_freq |
5920 dev_priv->rps.rp1_freq |
5921 dev_priv->rps.min_freq) & 1,
5922 "Odd GPU freq values\n");
Deepak S38807742014-05-23 21:00:15 +05305923}
5924
Chris Wilsondc979972016-05-10 14:10:04 +01005925static void valleyview_cleanup_gt_powersave(struct drm_i915_private *dev_priv)
Imre Deak4e805192014-04-14 20:24:41 +03005926{
Chris Wilsondc979972016-05-10 14:10:04 +01005927 valleyview_cleanup_pctx(dev_priv);
Imre Deak4e805192014-04-14 20:24:41 +03005928}
5929
Chris Wilsondc979972016-05-10 14:10:04 +01005930static void cherryview_enable_rps(struct drm_i915_private *dev_priv)
Deepak S38807742014-05-23 21:00:15 +05305931{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00005932 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05305933 enum intel_engine_id id;
Deepak S2b6b3a02014-05-27 15:59:30 +05305934 u32 gtfifodbg, val, rc6_mode = 0, pcbr;
Deepak S38807742014-05-23 21:00:15 +05305935
5936 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
5937
Ville Syrjälä297b32e2016-04-13 21:09:30 +03005938 gtfifodbg = I915_READ(GTFIFODBG) & ~(GT_FIFO_SBDEDICATE_FREE_ENTRY_CHV |
5939 GT_FIFO_FREE_ENTRIES_CHV);
Deepak S38807742014-05-23 21:00:15 +05305940 if (gtfifodbg) {
5941 DRM_DEBUG_DRIVER("GT fifo had a previous error %x\n",
5942 gtfifodbg);
5943 I915_WRITE(GTFIFODBG, gtfifodbg);
5944 }
5945
5946 cherryview_check_pctx(dev_priv);
5947
5948 /* 1a & 1b: Get forcewake during program sequence. Although the driver
5949 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
Mika Kuoppala59bad942015-01-16 11:34:40 +02005950 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Deepak S38807742014-05-23 21:00:15 +05305951
Ville Syrjälä160614a2015-01-19 13:50:47 +02005952 /* Disable RC states. */
5953 I915_WRITE(GEN6_RC_CONTROL, 0);
5954
Deepak S38807742014-05-23 21:00:15 +05305955 /* 2a: Program RC6 thresholds.*/
5956 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16);
5957 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
5958 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
5959
Akash Goel3b3f1652016-10-13 22:44:48 +05305960 for_each_engine(engine, dev_priv, id)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00005961 I915_WRITE(RING_MAX_IDLE(engine->mmio_base), 10);
Deepak S38807742014-05-23 21:00:15 +05305962 I915_WRITE(GEN6_RC_SLEEP, 0);
5963
Deepak Sf4f71c72015-03-28 15:23:35 +05305964 /* TO threshold set to 500 us ( 0x186 * 1.28 us) */
5965 I915_WRITE(GEN6_RC6_THRESHOLD, 0x186);
Deepak S38807742014-05-23 21:00:15 +05305966
5967 /* allows RC6 residency counter to work */
5968 I915_WRITE(VLV_COUNTER_CONTROL,
5969 _MASKED_BIT_ENABLE(VLV_COUNT_RANGE_HIGH |
5970 VLV_MEDIA_RC6_COUNT_EN |
5971 VLV_RENDER_RC6_COUNT_EN));
5972
5973 /* For now we assume BIOS is allocating and populating the PCBR */
5974 pcbr = I915_READ(VLV_PCBR);
5975
Deepak S38807742014-05-23 21:00:15 +05305976 /* 3: Enable RC6 */
Chris Wilsondc979972016-05-10 14:10:04 +01005977 if ((intel_enable_rc6() & INTEL_RC6_ENABLE) &&
5978 (pcbr >> VLV_PCBR_ADDR_SHIFT))
Ville Syrjäläaf5a75a2015-01-19 13:50:50 +02005979 rc6_mode = GEN7_RC_CTL_TO_MODE;
Deepak S38807742014-05-23 21:00:15 +05305980
5981 I915_WRITE(GEN6_RC_CONTROL, rc6_mode);
5982
Deepak S2b6b3a02014-05-27 15:59:30 +05305983 /* 4 Program defaults and thresholds for RPS*/
Ville Syrjälä3cbdb482015-01-19 13:50:49 +02005984 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 1000000);
Deepak S2b6b3a02014-05-27 15:59:30 +05305985 I915_WRITE(GEN6_RP_UP_THRESHOLD, 59400);
5986 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 245000);
5987 I915_WRITE(GEN6_RP_UP_EI, 66000);
5988 I915_WRITE(GEN6_RP_DOWN_EI, 350000);
5989
5990 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
5991
5992 /* 5: Enable RPS */
5993 I915_WRITE(GEN6_RP_CONTROL,
5994 GEN6_RP_MEDIA_HW_NORMAL_MODE |
Ville Syrjäläeb973a52015-01-21 19:37:59 +02005995 GEN6_RP_MEDIA_IS_GFX |
Deepak S2b6b3a02014-05-27 15:59:30 +05305996 GEN6_RP_ENABLE |
5997 GEN6_RP_UP_BUSY_AVG |
5998 GEN6_RP_DOWN_IDLE_AVG);
5999
Deepak S3ef62342015-04-29 08:36:24 +05306000 /* Setting Fixed Bias */
6001 val = VLV_OVERRIDE_EN |
6002 VLV_SOC_TDP_EN |
6003 CHV_BIAS_CPU_50_SOC_50;
6004 vlv_punit_write(dev_priv, VLV_TURBO_SOC_OVERRIDE, val);
6005
Deepak S2b6b3a02014-05-27 15:59:30 +05306006 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
6007
Ville Syrjälä8d40c3a2014-11-07 21:33:45 +02006008 /* RPS code assumes GPLL is used */
6009 WARN_ONCE((val & GPLLENABLE) == 0, "GPLL not enabled\n");
6010
Jani Nikula742f4912015-09-03 11:16:09 +03006011 DRM_DEBUG_DRIVER("GPLL enabled? %s\n", yesno(val & GPLLENABLE));
Deepak S2b6b3a02014-05-27 15:59:30 +05306012 DRM_DEBUG_DRIVER("GPU status: 0x%08x\n", val);
6013
Chris Wilson3a45b052016-07-13 09:10:32 +01006014 reset_rps(dev_priv, valleyview_set_rps);
Deepak S2b6b3a02014-05-27 15:59:30 +05306015
Mika Kuoppala59bad942015-01-16 11:34:40 +02006016 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Deepak S38807742014-05-23 21:00:15 +05306017}
6018
Chris Wilsondc979972016-05-10 14:10:04 +01006019static void valleyview_enable_rps(struct drm_i915_private *dev_priv)
Jesse Barnes0a073b82013-04-17 15:54:58 -07006020{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00006021 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05306022 enum intel_engine_id id;
Ben Widawsky2a5913a2014-03-19 18:31:13 -07006023 u32 gtfifodbg, val, rc6_mode = 0;
Jesse Barnes0a073b82013-04-17 15:54:58 -07006024
6025 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
6026
Imre Deakae484342014-03-31 15:10:44 +03006027 valleyview_check_pctx(dev_priv);
6028
Ville Syrjälä297b32e2016-04-13 21:09:30 +03006029 gtfifodbg = I915_READ(GTFIFODBG);
6030 if (gtfifodbg) {
Jesse Barnesf7d85c12013-09-27 10:40:54 -07006031 DRM_DEBUG_DRIVER("GT fifo had a previous error %x\n",
6032 gtfifodbg);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006033 I915_WRITE(GTFIFODBG, gtfifodbg);
6034 }
6035
Deepak Sc8d9a592013-11-23 14:55:42 +05306036 /* If VLV, Forcewake all wells, else re-direct to regular path */
Mika Kuoppala59bad942015-01-16 11:34:40 +02006037 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006038
Ville Syrjälä160614a2015-01-19 13:50:47 +02006039 /* Disable RC states. */
6040 I915_WRITE(GEN6_RC_CONTROL, 0);
6041
Ville Syrjäläcad725f2015-01-19 13:50:48 +02006042 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 1000000);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006043 I915_WRITE(GEN6_RP_UP_THRESHOLD, 59400);
6044 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 245000);
6045 I915_WRITE(GEN6_RP_UP_EI, 66000);
6046 I915_WRITE(GEN6_RP_DOWN_EI, 350000);
6047
6048 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
6049
6050 I915_WRITE(GEN6_RP_CONTROL,
6051 GEN6_RP_MEDIA_TURBO |
6052 GEN6_RP_MEDIA_HW_NORMAL_MODE |
6053 GEN6_RP_MEDIA_IS_GFX |
6054 GEN6_RP_ENABLE |
6055 GEN6_RP_UP_BUSY_AVG |
6056 GEN6_RP_DOWN_IDLE_CONT);
6057
6058 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 0x00280000);
6059 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
6060 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
6061
Akash Goel3b3f1652016-10-13 22:44:48 +05306062 for_each_engine(engine, dev_priv, id)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00006063 I915_WRITE(RING_MAX_IDLE(engine->mmio_base), 10);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006064
Jesse Barnes2f0aa3042013-11-15 09:32:11 -08006065 I915_WRITE(GEN6_RC6_THRESHOLD, 0x557);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006066
6067 /* allows RC6 residency counter to work */
Jesse Barnes49798eb2013-09-26 17:55:57 -07006068 I915_WRITE(VLV_COUNTER_CONTROL,
Deepak S31685c22014-07-03 17:33:01 -04006069 _MASKED_BIT_ENABLE(VLV_MEDIA_RC0_COUNT_EN |
6070 VLV_RENDER_RC0_COUNT_EN |
Jesse Barnes49798eb2013-09-26 17:55:57 -07006071 VLV_MEDIA_RC6_COUNT_EN |
6072 VLV_RENDER_RC6_COUNT_EN));
Deepak S31685c22014-07-03 17:33:01 -04006073
Chris Wilsondc979972016-05-10 14:10:04 +01006074 if (intel_enable_rc6() & INTEL_RC6_ENABLE)
Jesse Barnes6b88f292013-11-15 09:32:12 -08006075 rc6_mode = GEN7_RC_CTL_TO_MODE | VLV_RC_CTL_CTX_RST_PARALLEL;
Ben Widawskydc39fff2013-10-18 12:32:07 -07006076
Chris Wilsondc979972016-05-10 14:10:04 +01006077 intel_print_rc6_info(dev_priv, rc6_mode);
Ben Widawskydc39fff2013-10-18 12:32:07 -07006078
Jesse Barnesa2b23fe2013-09-19 09:33:13 -07006079 I915_WRITE(GEN6_RC_CONTROL, rc6_mode);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006080
Deepak S3ef62342015-04-29 08:36:24 +05306081 /* Setting Fixed Bias */
6082 val = VLV_OVERRIDE_EN |
6083 VLV_SOC_TDP_EN |
6084 VLV_BIAS_CPU_125_SOC_875;
6085 vlv_punit_write(dev_priv, VLV_TURBO_SOC_OVERRIDE, val);
6086
Jani Nikula64936252013-05-22 15:36:20 +03006087 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006088
Ville Syrjälä8d40c3a2014-11-07 21:33:45 +02006089 /* RPS code assumes GPLL is used */
6090 WARN_ONCE((val & GPLLENABLE) == 0, "GPLL not enabled\n");
6091
Jani Nikula742f4912015-09-03 11:16:09 +03006092 DRM_DEBUG_DRIVER("GPLL enabled? %s\n", yesno(val & GPLLENABLE));
Jesse Barnes0a073b82013-04-17 15:54:58 -07006093 DRM_DEBUG_DRIVER("GPU status: 0x%08x\n", val);
6094
Chris Wilson3a45b052016-07-13 09:10:32 +01006095 reset_rps(dev_priv, valleyview_set_rps);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006096
Mika Kuoppala59bad942015-01-16 11:34:40 +02006097 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006098}
6099
Eugeni Dodonovdde18882012-04-18 15:29:24 -03006100static unsigned long intel_pxfreq(u32 vidfreq)
6101{
6102 unsigned long freq;
6103 int div = (vidfreq & 0x3f0000) >> 16;
6104 int post = (vidfreq & 0x3000) >> 12;
6105 int pre = (vidfreq & 0x7);
6106
6107 if (!pre)
6108 return 0;
6109
6110 freq = ((div * 133333) / ((1<<post) * pre));
6111
6112 return freq;
6113}
6114
Daniel Vettereb48eb02012-04-26 23:28:12 +02006115static const struct cparams {
6116 u16 i;
6117 u16 t;
6118 u16 m;
6119 u16 c;
6120} cparams[] = {
6121 { 1, 1333, 301, 28664 },
6122 { 1, 1066, 294, 24460 },
6123 { 1, 800, 294, 25192 },
6124 { 0, 1333, 276, 27605 },
6125 { 0, 1066, 276, 27605 },
6126 { 0, 800, 231, 23784 },
6127};
6128
Chris Wilsonf531dcb22012-09-25 10:16:12 +01006129static unsigned long __i915_chipset_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02006130{
6131 u64 total_count, diff, ret;
6132 u32 count1, count2, count3, m = 0, c = 0;
6133 unsigned long now = jiffies_to_msecs(jiffies), diff1;
6134 int i;
6135
Daniel Vetter02d71952012-08-09 16:44:54 +02006136 assert_spin_locked(&mchdev_lock);
6137
Daniel Vetter20e4d402012-08-08 23:35:39 +02006138 diff1 = now - dev_priv->ips.last_time1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006139
6140 /* Prevent division-by-zero if we are asking too fast.
6141 * Also, we don't get interesting results if we are polling
6142 * faster than once in 10ms, so just return the saved value
6143 * in such cases.
6144 */
6145 if (diff1 <= 10)
Daniel Vetter20e4d402012-08-08 23:35:39 +02006146 return dev_priv->ips.chipset_power;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006147
6148 count1 = I915_READ(DMIEC);
6149 count2 = I915_READ(DDREC);
6150 count3 = I915_READ(CSIEC);
6151
6152 total_count = count1 + count2 + count3;
6153
6154 /* FIXME: handle per-counter overflow */
Daniel Vetter20e4d402012-08-08 23:35:39 +02006155 if (total_count < dev_priv->ips.last_count1) {
6156 diff = ~0UL - dev_priv->ips.last_count1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006157 diff += total_count;
6158 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +02006159 diff = total_count - dev_priv->ips.last_count1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006160 }
6161
6162 for (i = 0; i < ARRAY_SIZE(cparams); i++) {
Daniel Vetter20e4d402012-08-08 23:35:39 +02006163 if (cparams[i].i == dev_priv->ips.c_m &&
6164 cparams[i].t == dev_priv->ips.r_t) {
Daniel Vettereb48eb02012-04-26 23:28:12 +02006165 m = cparams[i].m;
6166 c = cparams[i].c;
6167 break;
6168 }
6169 }
6170
6171 diff = div_u64(diff, diff1);
6172 ret = ((m * diff) + c);
6173 ret = div_u64(ret, 10);
6174
Daniel Vetter20e4d402012-08-08 23:35:39 +02006175 dev_priv->ips.last_count1 = total_count;
6176 dev_priv->ips.last_time1 = now;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006177
Daniel Vetter20e4d402012-08-08 23:35:39 +02006178 dev_priv->ips.chipset_power = ret;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006179
6180 return ret;
6181}
6182
Chris Wilsonf531dcb22012-09-25 10:16:12 +01006183unsigned long i915_chipset_val(struct drm_i915_private *dev_priv)
6184{
6185 unsigned long val;
6186
Chris Wilsondc979972016-05-10 14:10:04 +01006187 if (INTEL_INFO(dev_priv)->gen != 5)
Chris Wilsonf531dcb22012-09-25 10:16:12 +01006188 return 0;
6189
6190 spin_lock_irq(&mchdev_lock);
6191
6192 val = __i915_chipset_val(dev_priv);
6193
6194 spin_unlock_irq(&mchdev_lock);
6195
6196 return val;
6197}
6198
Daniel Vettereb48eb02012-04-26 23:28:12 +02006199unsigned long i915_mch_val(struct drm_i915_private *dev_priv)
6200{
6201 unsigned long m, x, b;
6202 u32 tsfs;
6203
6204 tsfs = I915_READ(TSFS);
6205
6206 m = ((tsfs & TSFS_SLOPE_MASK) >> TSFS_SLOPE_SHIFT);
6207 x = I915_READ8(TR1);
6208
6209 b = tsfs & TSFS_INTR_MASK;
6210
6211 return ((m * x) / 127) - b;
6212}
6213
Mika Kuoppalad972d6e2014-12-01 18:01:05 +02006214static int _pxvid_to_vd(u8 pxvid)
6215{
6216 if (pxvid == 0)
6217 return 0;
6218
6219 if (pxvid >= 8 && pxvid < 31)
6220 pxvid = 31;
6221
6222 return (pxvid + 2) * 125;
6223}
6224
6225static u32 pvid_to_extvid(struct drm_i915_private *dev_priv, u8 pxvid)
Daniel Vettereb48eb02012-04-26 23:28:12 +02006226{
Mika Kuoppalad972d6e2014-12-01 18:01:05 +02006227 const int vd = _pxvid_to_vd(pxvid);
6228 const int vm = vd - 1125;
6229
Chris Wilsondc979972016-05-10 14:10:04 +01006230 if (INTEL_INFO(dev_priv)->is_mobile)
Mika Kuoppalad972d6e2014-12-01 18:01:05 +02006231 return vm > 0 ? vm : 0;
6232
6233 return vd;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006234}
6235
Daniel Vetter02d71952012-08-09 16:44:54 +02006236static void __i915_update_gfx_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02006237{
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00006238 u64 now, diff, diffms;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006239 u32 count;
6240
Daniel Vetter02d71952012-08-09 16:44:54 +02006241 assert_spin_locked(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006242
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00006243 now = ktime_get_raw_ns();
6244 diffms = now - dev_priv->ips.last_time2;
6245 do_div(diffms, NSEC_PER_MSEC);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006246
6247 /* Don't divide by 0 */
Daniel Vettereb48eb02012-04-26 23:28:12 +02006248 if (!diffms)
6249 return;
6250
6251 count = I915_READ(GFXEC);
6252
Daniel Vetter20e4d402012-08-08 23:35:39 +02006253 if (count < dev_priv->ips.last_count2) {
6254 diff = ~0UL - dev_priv->ips.last_count2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006255 diff += count;
6256 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +02006257 diff = count - dev_priv->ips.last_count2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006258 }
6259
Daniel Vetter20e4d402012-08-08 23:35:39 +02006260 dev_priv->ips.last_count2 = count;
6261 dev_priv->ips.last_time2 = now;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006262
6263 /* More magic constants... */
6264 diff = diff * 1181;
6265 diff = div_u64(diff, diffms * 10);
Daniel Vetter20e4d402012-08-08 23:35:39 +02006266 dev_priv->ips.gfx_power = diff;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006267}
6268
Daniel Vetter02d71952012-08-09 16:44:54 +02006269void i915_update_gfx_val(struct drm_i915_private *dev_priv)
6270{
Chris Wilsondc979972016-05-10 14:10:04 +01006271 if (INTEL_INFO(dev_priv)->gen != 5)
Daniel Vetter02d71952012-08-09 16:44:54 +02006272 return;
6273
Daniel Vetter92703882012-08-09 16:46:01 +02006274 spin_lock_irq(&mchdev_lock);
Daniel Vetter02d71952012-08-09 16:44:54 +02006275
6276 __i915_update_gfx_val(dev_priv);
6277
Daniel Vetter92703882012-08-09 16:46:01 +02006278 spin_unlock_irq(&mchdev_lock);
Daniel Vetter02d71952012-08-09 16:44:54 +02006279}
6280
Chris Wilsonf531dcb22012-09-25 10:16:12 +01006281static unsigned long __i915_gfx_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02006282{
6283 unsigned long t, corr, state1, corr2, state2;
6284 u32 pxvid, ext_v;
6285
Daniel Vetter02d71952012-08-09 16:44:54 +02006286 assert_spin_locked(&mchdev_lock);
6287
Ville Syrjälä616847e2015-09-18 20:03:19 +03006288 pxvid = I915_READ(PXVFREQ(dev_priv->rps.cur_freq));
Daniel Vettereb48eb02012-04-26 23:28:12 +02006289 pxvid = (pxvid >> 24) & 0x7f;
6290 ext_v = pvid_to_extvid(dev_priv, pxvid);
6291
6292 state1 = ext_v;
6293
6294 t = i915_mch_val(dev_priv);
6295
6296 /* Revel in the empirically derived constants */
6297
6298 /* Correction factor in 1/100000 units */
6299 if (t > 80)
6300 corr = ((t * 2349) + 135940);
6301 else if (t >= 50)
6302 corr = ((t * 964) + 29317);
6303 else /* < 50 */
6304 corr = ((t * 301) + 1004);
6305
6306 corr = corr * ((150142 * state1) / 10000 - 78642);
6307 corr /= 100000;
Daniel Vetter20e4d402012-08-08 23:35:39 +02006308 corr2 = (corr * dev_priv->ips.corr);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006309
6310 state2 = (corr2 * state1) / 10000;
6311 state2 /= 100; /* convert to mW */
6312
Daniel Vetter02d71952012-08-09 16:44:54 +02006313 __i915_update_gfx_val(dev_priv);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006314
Daniel Vetter20e4d402012-08-08 23:35:39 +02006315 return dev_priv->ips.gfx_power + state2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006316}
6317
Chris Wilsonf531dcb22012-09-25 10:16:12 +01006318unsigned long i915_gfx_val(struct drm_i915_private *dev_priv)
6319{
6320 unsigned long val;
6321
Chris Wilsondc979972016-05-10 14:10:04 +01006322 if (INTEL_INFO(dev_priv)->gen != 5)
Chris Wilsonf531dcb22012-09-25 10:16:12 +01006323 return 0;
6324
6325 spin_lock_irq(&mchdev_lock);
6326
6327 val = __i915_gfx_val(dev_priv);
6328
6329 spin_unlock_irq(&mchdev_lock);
6330
6331 return val;
6332}
6333
Daniel Vettereb48eb02012-04-26 23:28:12 +02006334/**
6335 * i915_read_mch_val - return value for IPS use
6336 *
6337 * Calculate and return a value for the IPS driver to use when deciding whether
6338 * we have thermal and power headroom to increase CPU or GPU power budget.
6339 */
6340unsigned long i915_read_mch_val(void)
6341{
6342 struct drm_i915_private *dev_priv;
6343 unsigned long chipset_val, graphics_val, ret = 0;
6344
Daniel Vetter92703882012-08-09 16:46:01 +02006345 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006346 if (!i915_mch_dev)
6347 goto out_unlock;
6348 dev_priv = i915_mch_dev;
6349
Chris Wilsonf531dcb22012-09-25 10:16:12 +01006350 chipset_val = __i915_chipset_val(dev_priv);
6351 graphics_val = __i915_gfx_val(dev_priv);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006352
6353 ret = chipset_val + graphics_val;
6354
6355out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02006356 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006357
6358 return ret;
6359}
6360EXPORT_SYMBOL_GPL(i915_read_mch_val);
6361
6362/**
6363 * i915_gpu_raise - raise GPU frequency limit
6364 *
6365 * Raise the limit; IPS indicates we have thermal headroom.
6366 */
6367bool i915_gpu_raise(void)
6368{
6369 struct drm_i915_private *dev_priv;
6370 bool ret = true;
6371
Daniel Vetter92703882012-08-09 16:46:01 +02006372 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006373 if (!i915_mch_dev) {
6374 ret = false;
6375 goto out_unlock;
6376 }
6377 dev_priv = i915_mch_dev;
6378
Daniel Vetter20e4d402012-08-08 23:35:39 +02006379 if (dev_priv->ips.max_delay > dev_priv->ips.fmax)
6380 dev_priv->ips.max_delay--;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006381
6382out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02006383 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006384
6385 return ret;
6386}
6387EXPORT_SYMBOL_GPL(i915_gpu_raise);
6388
6389/**
6390 * i915_gpu_lower - lower GPU frequency limit
6391 *
6392 * IPS indicates we're close to a thermal limit, so throttle back the GPU
6393 * frequency maximum.
6394 */
6395bool i915_gpu_lower(void)
6396{
6397 struct drm_i915_private *dev_priv;
6398 bool ret = true;
6399
Daniel Vetter92703882012-08-09 16:46:01 +02006400 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006401 if (!i915_mch_dev) {
6402 ret = false;
6403 goto out_unlock;
6404 }
6405 dev_priv = i915_mch_dev;
6406
Daniel Vetter20e4d402012-08-08 23:35:39 +02006407 if (dev_priv->ips.max_delay < dev_priv->ips.min_delay)
6408 dev_priv->ips.max_delay++;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006409
6410out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02006411 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006412
6413 return ret;
6414}
6415EXPORT_SYMBOL_GPL(i915_gpu_lower);
6416
6417/**
6418 * i915_gpu_busy - indicate GPU business to IPS
6419 *
6420 * Tell the IPS driver whether or not the GPU is busy.
6421 */
6422bool i915_gpu_busy(void)
6423{
Daniel Vettereb48eb02012-04-26 23:28:12 +02006424 bool ret = false;
6425
Daniel Vetter92703882012-08-09 16:46:01 +02006426 spin_lock_irq(&mchdev_lock);
Chris Wilsondcff85c2016-08-05 10:14:11 +01006427 if (i915_mch_dev)
6428 ret = i915_mch_dev->gt.awake;
Daniel Vetter92703882012-08-09 16:46:01 +02006429 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006430
6431 return ret;
6432}
6433EXPORT_SYMBOL_GPL(i915_gpu_busy);
6434
6435/**
6436 * i915_gpu_turbo_disable - disable graphics turbo
6437 *
6438 * Disable graphics turbo by resetting the max frequency and setting the
6439 * current frequency to the default.
6440 */
6441bool i915_gpu_turbo_disable(void)
6442{
6443 struct drm_i915_private *dev_priv;
6444 bool ret = true;
6445
Daniel Vetter92703882012-08-09 16:46:01 +02006446 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006447 if (!i915_mch_dev) {
6448 ret = false;
6449 goto out_unlock;
6450 }
6451 dev_priv = i915_mch_dev;
6452
Daniel Vetter20e4d402012-08-08 23:35:39 +02006453 dev_priv->ips.max_delay = dev_priv->ips.fstart;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006454
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01006455 if (!ironlake_set_drps(dev_priv, dev_priv->ips.fstart))
Daniel Vettereb48eb02012-04-26 23:28:12 +02006456 ret = false;
6457
6458out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02006459 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006460
6461 return ret;
6462}
6463EXPORT_SYMBOL_GPL(i915_gpu_turbo_disable);
6464
6465/**
6466 * Tells the intel_ips driver that the i915 driver is now loaded, if
6467 * IPS got loaded first.
6468 *
6469 * This awkward dance is so that neither module has to depend on the
6470 * other in order for IPS to do the appropriate communication of
6471 * GPU turbo limits to i915.
6472 */
6473static void
6474ips_ping_for_i915_load(void)
6475{
6476 void (*link)(void);
6477
6478 link = symbol_get(ips_link_to_i915_driver);
6479 if (link) {
6480 link();
6481 symbol_put(ips_link_to_i915_driver);
6482 }
6483}
6484
6485void intel_gpu_ips_init(struct drm_i915_private *dev_priv)
6486{
Daniel Vetter02d71952012-08-09 16:44:54 +02006487 /* We only register the i915 ips part with intel-ips once everything is
6488 * set up, to avoid intel-ips sneaking in and reading bogus values. */
Daniel Vetter92703882012-08-09 16:46:01 +02006489 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006490 i915_mch_dev = dev_priv;
Daniel Vetter92703882012-08-09 16:46:01 +02006491 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006492
6493 ips_ping_for_i915_load();
6494}
6495
6496void intel_gpu_ips_teardown(void)
6497{
Daniel Vetter92703882012-08-09 16:46:01 +02006498 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006499 i915_mch_dev = NULL;
Daniel Vetter92703882012-08-09 16:46:01 +02006500 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006501}
Deepak S76c3552f2014-01-30 23:08:16 +05306502
Chris Wilsondc979972016-05-10 14:10:04 +01006503static void intel_init_emon(struct drm_i915_private *dev_priv)
Eugeni Dodonovdde18882012-04-18 15:29:24 -03006504{
Eugeni Dodonovdde18882012-04-18 15:29:24 -03006505 u32 lcfuse;
6506 u8 pxw[16];
6507 int i;
6508
6509 /* Disable to program */
6510 I915_WRITE(ECR, 0);
6511 POSTING_READ(ECR);
6512
6513 /* Program energy weights for various events */
6514 I915_WRITE(SDEW, 0x15040d00);
6515 I915_WRITE(CSIEW0, 0x007f0000);
6516 I915_WRITE(CSIEW1, 0x1e220004);
6517 I915_WRITE(CSIEW2, 0x04000004);
6518
6519 for (i = 0; i < 5; i++)
Ville Syrjälä616847e2015-09-18 20:03:19 +03006520 I915_WRITE(PEW(i), 0);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03006521 for (i = 0; i < 3; i++)
Ville Syrjälä616847e2015-09-18 20:03:19 +03006522 I915_WRITE(DEW(i), 0);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03006523
6524 /* Program P-state weights to account for frequency power adjustment */
6525 for (i = 0; i < 16; i++) {
Ville Syrjälä616847e2015-09-18 20:03:19 +03006526 u32 pxvidfreq = I915_READ(PXVFREQ(i));
Eugeni Dodonovdde18882012-04-18 15:29:24 -03006527 unsigned long freq = intel_pxfreq(pxvidfreq);
6528 unsigned long vid = (pxvidfreq & PXVFREQ_PX_MASK) >>
6529 PXVFREQ_PX_SHIFT;
6530 unsigned long val;
6531
6532 val = vid * vid;
6533 val *= (freq / 1000);
6534 val *= 255;
6535 val /= (127*127*900);
6536 if (val > 0xff)
6537 DRM_ERROR("bad pxval: %ld\n", val);
6538 pxw[i] = val;
6539 }
6540 /* Render standby states get 0 weight */
6541 pxw[14] = 0;
6542 pxw[15] = 0;
6543
6544 for (i = 0; i < 4; i++) {
6545 u32 val = (pxw[i*4] << 24) | (pxw[(i*4)+1] << 16) |
6546 (pxw[(i*4)+2] << 8) | (pxw[(i*4)+3]);
Ville Syrjälä616847e2015-09-18 20:03:19 +03006547 I915_WRITE(PXW(i), val);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03006548 }
6549
6550 /* Adjust magic regs to magic values (more experimental results) */
6551 I915_WRITE(OGW0, 0);
6552 I915_WRITE(OGW1, 0);
6553 I915_WRITE(EG0, 0x00007f00);
6554 I915_WRITE(EG1, 0x0000000e);
6555 I915_WRITE(EG2, 0x000e0000);
6556 I915_WRITE(EG3, 0x68000300);
6557 I915_WRITE(EG4, 0x42000000);
6558 I915_WRITE(EG5, 0x00140031);
6559 I915_WRITE(EG6, 0);
6560 I915_WRITE(EG7, 0);
6561
6562 for (i = 0; i < 8; i++)
Ville Syrjälä616847e2015-09-18 20:03:19 +03006563 I915_WRITE(PXWL(i), 0);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03006564
6565 /* Enable PMON + select events */
6566 I915_WRITE(ECR, 0x80000019);
6567
6568 lcfuse = I915_READ(LCFUSE02);
6569
Daniel Vetter20e4d402012-08-08 23:35:39 +02006570 dev_priv->ips.corr = (lcfuse & LCFUSE_HIV_MASK);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03006571}
6572
Chris Wilsondc979972016-05-10 14:10:04 +01006573void intel_init_gt_powersave(struct drm_i915_private *dev_priv)
Imre Deakae484342014-03-31 15:10:44 +03006574{
Imre Deakb268c692015-12-15 20:10:31 +02006575 /*
6576 * RPM depends on RC6 to save restore the GT HW context, so make RC6 a
6577 * requirement.
6578 */
6579 if (!i915.enable_rc6) {
6580 DRM_INFO("RC6 disabled, disabling runtime PM support\n");
6581 intel_runtime_pm_get(dev_priv);
6582 }
Imre Deake6069ca2014-04-18 16:01:02 +03006583
Chris Wilsonb5163db2016-08-10 13:58:24 +01006584 mutex_lock(&dev_priv->drm.struct_mutex);
Chris Wilson773ea9a2016-07-13 09:10:33 +01006585 mutex_lock(&dev_priv->rps.hw_lock);
6586
6587 /* Initialize RPS limits (for userspace) */
Chris Wilsondc979972016-05-10 14:10:04 +01006588 if (IS_CHERRYVIEW(dev_priv))
6589 cherryview_init_gt_powersave(dev_priv);
6590 else if (IS_VALLEYVIEW(dev_priv))
6591 valleyview_init_gt_powersave(dev_priv);
Chris Wilson2a13ae72016-08-02 11:15:27 +01006592 else if (INTEL_GEN(dev_priv) >= 6)
Chris Wilson773ea9a2016-07-13 09:10:33 +01006593 gen6_init_rps_frequencies(dev_priv);
6594
6595 /* Derive initial user preferences/limits from the hardware limits */
6596 dev_priv->rps.idle_freq = dev_priv->rps.min_freq;
6597 dev_priv->rps.cur_freq = dev_priv->rps.idle_freq;
6598
6599 dev_priv->rps.max_freq_softlimit = dev_priv->rps.max_freq;
6600 dev_priv->rps.min_freq_softlimit = dev_priv->rps.min_freq;
6601
6602 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
6603 dev_priv->rps.min_freq_softlimit =
6604 max_t(int,
6605 dev_priv->rps.efficient_freq,
6606 intel_freq_opcode(dev_priv, 450));
6607
Chris Wilson99ac9612016-07-13 09:10:34 +01006608 /* After setting max-softlimit, find the overclock max freq */
6609 if (IS_GEN6(dev_priv) ||
6610 IS_IVYBRIDGE(dev_priv) || IS_HASWELL(dev_priv)) {
6611 u32 params = 0;
6612
6613 sandybridge_pcode_read(dev_priv, GEN6_READ_OC_PARAMS, &params);
6614 if (params & BIT(31)) { /* OC supported */
6615 DRM_DEBUG_DRIVER("Overclocking supported, max: %dMHz, overclock: %dMHz\n",
6616 (dev_priv->rps.max_freq & 0xff) * 50,
6617 (params & 0xff) * 50);
6618 dev_priv->rps.max_freq = params & 0xff;
6619 }
6620 }
6621
Chris Wilson29ecd78d2016-07-13 09:10:35 +01006622 /* Finally allow us to boost to max by default */
6623 dev_priv->rps.boost_freq = dev_priv->rps.max_freq;
6624
Chris Wilson773ea9a2016-07-13 09:10:33 +01006625 mutex_unlock(&dev_priv->rps.hw_lock);
Chris Wilsonb5163db2016-08-10 13:58:24 +01006626 mutex_unlock(&dev_priv->drm.struct_mutex);
Chris Wilson54b4f682016-07-21 21:16:19 +01006627
6628 intel_autoenable_gt_powersave(dev_priv);
Imre Deakae484342014-03-31 15:10:44 +03006629}
6630
Chris Wilsondc979972016-05-10 14:10:04 +01006631void intel_cleanup_gt_powersave(struct drm_i915_private *dev_priv)
Imre Deakae484342014-03-31 15:10:44 +03006632{
Ville Syrjälä8dac1e12016-08-02 14:07:33 +03006633 if (IS_VALLEYVIEW(dev_priv))
Chris Wilsondc979972016-05-10 14:10:04 +01006634 valleyview_cleanup_gt_powersave(dev_priv);
Imre Deakb268c692015-12-15 20:10:31 +02006635
6636 if (!i915.enable_rc6)
6637 intel_runtime_pm_put(dev_priv);
Imre Deakae484342014-03-31 15:10:44 +03006638}
6639
Chris Wilson54b4f682016-07-21 21:16:19 +01006640/**
6641 * intel_suspend_gt_powersave - suspend PM work and helper threads
6642 * @dev_priv: i915 device
6643 *
6644 * We don't want to disable RC6 or other features here, we just want
6645 * to make sure any work we've queued has finished and won't bother
6646 * us while we're suspended.
6647 */
6648void intel_suspend_gt_powersave(struct drm_i915_private *dev_priv)
6649{
6650 if (INTEL_GEN(dev_priv) < 6)
6651 return;
6652
6653 if (cancel_delayed_work_sync(&dev_priv->rps.autoenable_work))
6654 intel_runtime_pm_put(dev_priv);
6655
6656 /* gen6_rps_idle() will be called later to disable interrupts */
6657}
6658
Chris Wilsonb7137e02016-07-13 09:10:37 +01006659void intel_sanitize_gt_powersave(struct drm_i915_private *dev_priv)
6660{
6661 dev_priv->rps.enabled = true; /* force disabling */
6662 intel_disable_gt_powersave(dev_priv);
Chris Wilson54b4f682016-07-21 21:16:19 +01006663
6664 gen6_reset_rps_interrupts(dev_priv);
Jesse Barnes156c7ca2014-06-12 08:35:45 -07006665}
6666
Chris Wilsondc979972016-05-10 14:10:04 +01006667void intel_disable_gt_powersave(struct drm_i915_private *dev_priv)
Daniel Vetter8090c6b2012-06-24 16:42:32 +02006668{
Chris Wilsonb7137e02016-07-13 09:10:37 +01006669 if (!READ_ONCE(dev_priv->rps.enabled))
6670 return;
Jesse Barnes1a01ab32012-11-02 11:14:00 -07006671
Jesse Barnes4fc688c2012-11-02 11:14:01 -07006672 mutex_lock(&dev_priv->rps.hw_lock);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006673
Chris Wilsonb7137e02016-07-13 09:10:37 +01006674 if (INTEL_GEN(dev_priv) >= 9) {
6675 gen9_disable_rc6(dev_priv);
6676 gen9_disable_rps(dev_priv);
6677 } else if (IS_CHERRYVIEW(dev_priv)) {
6678 cherryview_disable_rps(dev_priv);
6679 } else if (IS_VALLEYVIEW(dev_priv)) {
6680 valleyview_disable_rps(dev_priv);
6681 } else if (INTEL_GEN(dev_priv) >= 6) {
6682 gen6_disable_rps(dev_priv);
6683 } else if (IS_IRONLAKE_M(dev_priv)) {
6684 ironlake_disable_drps(dev_priv);
6685 }
6686
6687 dev_priv->rps.enabled = false;
6688 mutex_unlock(&dev_priv->rps.hw_lock);
6689}
6690
6691void intel_enable_gt_powersave(struct drm_i915_private *dev_priv)
6692{
Chris Wilson54b4f682016-07-21 21:16:19 +01006693 /* We shouldn't be disabling as we submit, so this should be less
6694 * racy than it appears!
6695 */
Chris Wilsonb7137e02016-07-13 09:10:37 +01006696 if (READ_ONCE(dev_priv->rps.enabled))
6697 return;
6698
6699 /* Powersaving is controlled by the host when inside a VM */
6700 if (intel_vgpu_active(dev_priv))
6701 return;
6702
6703 mutex_lock(&dev_priv->rps.hw_lock);
Imre Deak3cc134e2014-11-19 15:30:03 +02006704
Chris Wilsondc979972016-05-10 14:10:04 +01006705 if (IS_CHERRYVIEW(dev_priv)) {
6706 cherryview_enable_rps(dev_priv);
6707 } else if (IS_VALLEYVIEW(dev_priv)) {
6708 valleyview_enable_rps(dev_priv);
Chris Wilsonb7137e02016-07-13 09:10:37 +01006709 } else if (INTEL_GEN(dev_priv) >= 9) {
Chris Wilsondc979972016-05-10 14:10:04 +01006710 gen9_enable_rc6(dev_priv);
6711 gen9_enable_rps(dev_priv);
6712 if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv))
Chris Wilsonfb7404e2016-07-13 09:10:38 +01006713 gen6_update_ring_freq(dev_priv);
Chris Wilsondc979972016-05-10 14:10:04 +01006714 } else if (IS_BROADWELL(dev_priv)) {
6715 gen8_enable_rps(dev_priv);
Chris Wilsonfb7404e2016-07-13 09:10:38 +01006716 gen6_update_ring_freq(dev_priv);
Chris Wilsonb7137e02016-07-13 09:10:37 +01006717 } else if (INTEL_GEN(dev_priv) >= 6) {
Chris Wilsondc979972016-05-10 14:10:04 +01006718 gen6_enable_rps(dev_priv);
Chris Wilsonfb7404e2016-07-13 09:10:38 +01006719 gen6_update_ring_freq(dev_priv);
Chris Wilsonb7137e02016-07-13 09:10:37 +01006720 } else if (IS_IRONLAKE_M(dev_priv)) {
6721 ironlake_enable_drps(dev_priv);
6722 intel_init_emon(dev_priv);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006723 }
Chris Wilsonaed242f2015-03-18 09:48:21 +00006724
6725 WARN_ON(dev_priv->rps.max_freq < dev_priv->rps.min_freq);
6726 WARN_ON(dev_priv->rps.idle_freq > dev_priv->rps.max_freq);
6727
6728 WARN_ON(dev_priv->rps.efficient_freq < dev_priv->rps.min_freq);
6729 WARN_ON(dev_priv->rps.efficient_freq > dev_priv->rps.max_freq);
6730
Chris Wilson54b4f682016-07-21 21:16:19 +01006731 dev_priv->rps.enabled = true;
Jesse Barnes4fc688c2012-11-02 11:14:01 -07006732 mutex_unlock(&dev_priv->rps.hw_lock);
Chris Wilsonb7137e02016-07-13 09:10:37 +01006733}
Imre Deakc6df39b2014-04-14 20:24:29 +03006734
Chris Wilson54b4f682016-07-21 21:16:19 +01006735static void __intel_autoenable_gt_powersave(struct work_struct *work)
6736{
6737 struct drm_i915_private *dev_priv =
6738 container_of(work, typeof(*dev_priv), rps.autoenable_work.work);
6739 struct intel_engine_cs *rcs;
6740 struct drm_i915_gem_request *req;
6741
6742 if (READ_ONCE(dev_priv->rps.enabled))
6743 goto out;
6744
Akash Goel3b3f1652016-10-13 22:44:48 +05306745 rcs = dev_priv->engine[RCS];
Chris Wilson54b4f682016-07-21 21:16:19 +01006746 if (rcs->last_context)
6747 goto out;
6748
6749 if (!rcs->init_context)
6750 goto out;
6751
6752 mutex_lock(&dev_priv->drm.struct_mutex);
6753
6754 req = i915_gem_request_alloc(rcs, dev_priv->kernel_context);
6755 if (IS_ERR(req))
6756 goto unlock;
6757
6758 if (!i915.enable_execlists && i915_switch_context(req) == 0)
6759 rcs->init_context(req);
6760
6761 /* Mark the device busy, calling intel_enable_gt_powersave() */
6762 i915_add_request_no_flush(req);
6763
6764unlock:
6765 mutex_unlock(&dev_priv->drm.struct_mutex);
6766out:
6767 intel_runtime_pm_put(dev_priv);
6768}
6769
6770void intel_autoenable_gt_powersave(struct drm_i915_private *dev_priv)
6771{
6772 if (READ_ONCE(dev_priv->rps.enabled))
6773 return;
6774
6775 if (IS_IRONLAKE_M(dev_priv)) {
6776 ironlake_enable_drps(dev_priv);
Chris Wilson54b4f682016-07-21 21:16:19 +01006777 intel_init_emon(dev_priv);
Chris Wilson54b4f682016-07-21 21:16:19 +01006778 } else if (INTEL_INFO(dev_priv)->gen >= 6) {
6779 /*
6780 * PCU communication is slow and this doesn't need to be
6781 * done at any specific time, so do this out of our fast path
6782 * to make resume and init faster.
6783 *
6784 * We depend on the HW RC6 power context save/restore
6785 * mechanism when entering D3 through runtime PM suspend. So
6786 * disable RPM until RPS/RC6 is properly setup. We can only
6787 * get here via the driver load/system resume/runtime resume
6788 * paths, so the _noresume version is enough (and in case of
6789 * runtime resume it's necessary).
6790 */
6791 if (queue_delayed_work(dev_priv->wq,
6792 &dev_priv->rps.autoenable_work,
6793 round_jiffies_up_relative(HZ)))
6794 intel_runtime_pm_get_noresume(dev_priv);
6795 }
6796}
6797
Ville Syrjälä46f16e62016-10-31 22:37:22 +02006798static void ibx_init_clock_gating(struct drm_i915_private *dev_priv)
Daniel Vetter3107bd42012-10-31 22:52:31 +01006799{
Daniel Vetter3107bd42012-10-31 22:52:31 +01006800 /*
6801 * On Ibex Peak and Cougar Point, we need to disable clock
6802 * gating for the panel power sequencer or it will fail to
6803 * start up when no ports are active.
6804 */
6805 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE);
6806}
6807
Ville Syrjälä46f16e62016-10-31 22:37:22 +02006808static void g4x_disable_trickle_feed(struct drm_i915_private *dev_priv)
Ville Syrjälä0e088b82013-06-07 10:47:04 +03006809{
Ville Syrjäläb12ce1d2015-05-26 20:27:23 +03006810 enum pipe pipe;
Ville Syrjälä0e088b82013-06-07 10:47:04 +03006811
Damien Lespiau055e3932014-08-18 13:49:10 +01006812 for_each_pipe(dev_priv, pipe) {
Ville Syrjälä0e088b82013-06-07 10:47:04 +03006813 I915_WRITE(DSPCNTR(pipe),
6814 I915_READ(DSPCNTR(pipe)) |
6815 DISPPLANE_TRICKLE_FEED_DISABLE);
Ville Syrjäläb12ce1d2015-05-26 20:27:23 +03006816
6817 I915_WRITE(DSPSURF(pipe), I915_READ(DSPSURF(pipe)));
6818 POSTING_READ(DSPSURF(pipe));
Ville Syrjälä0e088b82013-06-07 10:47:04 +03006819 }
6820}
6821
Ville Syrjälä46f16e62016-10-31 22:37:22 +02006822static void ilk_init_lp_watermarks(struct drm_i915_private *dev_priv)
Ville Syrjälä017636c2013-12-05 15:51:37 +02006823{
Ville Syrjälä017636c2013-12-05 15:51:37 +02006824 I915_WRITE(WM3_LP_ILK, I915_READ(WM3_LP_ILK) & ~WM1_LP_SR_EN);
6825 I915_WRITE(WM2_LP_ILK, I915_READ(WM2_LP_ILK) & ~WM1_LP_SR_EN);
6826 I915_WRITE(WM1_LP_ILK, I915_READ(WM1_LP_ILK) & ~WM1_LP_SR_EN);
6827
6828 /*
6829 * Don't touch WM1S_LP_EN here.
6830 * Doing so could cause underruns.
6831 */
6832}
6833
Ville Syrjälä46f16e62016-10-31 22:37:22 +02006834static void ironlake_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006835{
Damien Lespiau231e54f2012-10-19 17:55:41 +01006836 uint32_t dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006837
Damien Lespiauf1e8fa52013-06-07 17:41:09 +01006838 /*
6839 * Required for FBC
6840 * WaFbcDisableDpfcClockGating:ilk
6841 */
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01006842 dspclk_gate |= ILK_DPFCRUNIT_CLOCK_GATE_DISABLE |
6843 ILK_DPFCUNIT_CLOCK_GATE_DISABLE |
6844 ILK_DPFDUNIT_CLOCK_GATE_ENABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006845
6846 I915_WRITE(PCH_3DCGDIS0,
6847 MARIUNIT_CLOCK_GATE_DISABLE |
6848 SVSMUNIT_CLOCK_GATE_DISABLE);
6849 I915_WRITE(PCH_3DCGDIS1,
6850 VFMUNIT_CLOCK_GATE_DISABLE);
6851
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006852 /*
6853 * According to the spec the following bits should be set in
6854 * order to enable memory self-refresh
6855 * The bit 22/21 of 0x42004
6856 * The bit 5 of 0x42020
6857 * The bit 15 of 0x45000
6858 */
6859 I915_WRITE(ILK_DISPLAY_CHICKEN2,
6860 (I915_READ(ILK_DISPLAY_CHICKEN2) |
6861 ILK_DPARB_GATE | ILK_VSDPFD_FULL));
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01006862 dspclk_gate |= ILK_DPARBUNIT_CLOCK_GATE_ENABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006863 I915_WRITE(DISP_ARB_CTL,
6864 (I915_READ(DISP_ARB_CTL) |
6865 DISP_FBC_WM_DIS));
Ville Syrjälä017636c2013-12-05 15:51:37 +02006866
Ville Syrjälä46f16e62016-10-31 22:37:22 +02006867 ilk_init_lp_watermarks(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006868
6869 /*
6870 * Based on the document from hardware guys the following bits
6871 * should be set unconditionally in order to enable FBC.
6872 * The bit 22 of 0x42000
6873 * The bit 22 of 0x42004
6874 * The bit 7,8,9 of 0x42020.
6875 */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01006876 if (IS_IRONLAKE_M(dev_priv)) {
Damien Lespiau4bb35332013-06-14 15:23:24 +01006877 /* WaFbcAsynchFlipDisableFbcQueue:ilk */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006878 I915_WRITE(ILK_DISPLAY_CHICKEN1,
6879 I915_READ(ILK_DISPLAY_CHICKEN1) |
6880 ILK_FBCQ_DIS);
6881 I915_WRITE(ILK_DISPLAY_CHICKEN2,
6882 I915_READ(ILK_DISPLAY_CHICKEN2) |
6883 ILK_DPARB_GATE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006884 }
6885
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01006886 I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
6887
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006888 I915_WRITE(ILK_DISPLAY_CHICKEN2,
6889 I915_READ(ILK_DISPLAY_CHICKEN2) |
6890 ILK_ELPIN_409_SELECT);
6891 I915_WRITE(_3D_CHICKEN2,
6892 _3D_CHICKEN2_WM_READ_PIPELINED << 16 |
6893 _3D_CHICKEN2_WM_READ_PIPELINED);
Daniel Vetter4358a372012-10-18 11:49:51 +02006894
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006895 /* WaDisableRenderCachePipelinedFlush:ilk */
Daniel Vetter4358a372012-10-18 11:49:51 +02006896 I915_WRITE(CACHE_MODE_0,
6897 _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
Daniel Vetter3107bd42012-10-31 22:52:31 +01006898
Akash Goel4e046322014-04-04 17:14:38 +05306899 /* WaDisable_RenderCache_OperationalFlush:ilk */
6900 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
6901
Ville Syrjälä46f16e62016-10-31 22:37:22 +02006902 g4x_disable_trickle_feed(dev_priv);
Ville Syrjäläbdad2b22013-06-07 10:47:03 +03006903
Ville Syrjälä46f16e62016-10-31 22:37:22 +02006904 ibx_init_clock_gating(dev_priv);
Daniel Vetter3107bd42012-10-31 22:52:31 +01006905}
6906
Ville Syrjälä46f16e62016-10-31 22:37:22 +02006907static void cpt_init_clock_gating(struct drm_i915_private *dev_priv)
Daniel Vetter3107bd42012-10-31 22:52:31 +01006908{
Daniel Vetter3107bd42012-10-31 22:52:31 +01006909 int pipe;
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03006910 uint32_t val;
Daniel Vetter3107bd42012-10-31 22:52:31 +01006911
6912 /*
6913 * On Ibex Peak and Cougar Point, we need to disable clock
6914 * gating for the panel power sequencer or it will fail to
6915 * start up when no ports are active.
6916 */
Jesse Barnescd664072013-10-02 10:34:19 -07006917 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE |
6918 PCH_DPLUNIT_CLOCK_GATE_DISABLE |
6919 PCH_CPUNIT_CLOCK_GATE_DISABLE);
Daniel Vetter3107bd42012-10-31 22:52:31 +01006920 I915_WRITE(SOUTH_CHICKEN2, I915_READ(SOUTH_CHICKEN2) |
6921 DPLS_EDP_PPS_FIX_DIS);
Takashi Iwai335c07b2012-12-11 11:46:29 +01006922 /* The below fixes the weird display corruption, a few pixels shifted
6923 * downward, on (only) LVDS of some HP laptops with IVY.
6924 */
Damien Lespiau055e3932014-08-18 13:49:10 +01006925 for_each_pipe(dev_priv, pipe) {
Paulo Zanonidc4bd2d2013-04-08 15:48:08 -03006926 val = I915_READ(TRANS_CHICKEN2(pipe));
6927 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
6928 val &= ~TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03006929 if (dev_priv->vbt.fdi_rx_polarity_inverted)
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03006930 val |= TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
Paulo Zanonidc4bd2d2013-04-08 15:48:08 -03006931 val &= ~TRANS_CHICKEN2_FRAME_START_DELAY_MASK;
6932 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_COUNTER;
6933 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_MODESWITCH;
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03006934 I915_WRITE(TRANS_CHICKEN2(pipe), val);
6935 }
Daniel Vetter3107bd42012-10-31 22:52:31 +01006936 /* WADP0ClockGatingDisable */
Damien Lespiau055e3932014-08-18 13:49:10 +01006937 for_each_pipe(dev_priv, pipe) {
Daniel Vetter3107bd42012-10-31 22:52:31 +01006938 I915_WRITE(TRANS_CHICKEN1(pipe),
6939 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
6940 }
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006941}
6942
Ville Syrjälä46f16e62016-10-31 22:37:22 +02006943static void gen6_check_mch_setup(struct drm_i915_private *dev_priv)
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01006944{
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01006945 uint32_t tmp;
6946
6947 tmp = I915_READ(MCH_SSKPD);
Daniel Vetterdf662a22014-08-04 11:17:25 +02006948 if ((tmp & MCH_SSKPD_WM0_MASK) != MCH_SSKPD_WM0_VAL)
6949 DRM_DEBUG_KMS("Wrong MCH_SSKPD value: 0x%08x This can cause underruns.\n",
6950 tmp);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01006951}
6952
Ville Syrjälä46f16e62016-10-31 22:37:22 +02006953static void gen6_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006954{
Damien Lespiau231e54f2012-10-19 17:55:41 +01006955 uint32_t dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006956
Damien Lespiau231e54f2012-10-19 17:55:41 +01006957 I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006958
6959 I915_WRITE(ILK_DISPLAY_CHICKEN2,
6960 I915_READ(ILK_DISPLAY_CHICKEN2) |
6961 ILK_ELPIN_409_SELECT);
6962
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006963 /* WaDisableHiZPlanesWhenMSAAEnabled:snb */
Daniel Vetter42839082012-12-14 23:38:28 +01006964 I915_WRITE(_3D_CHICKEN,
6965 _MASKED_BIT_ENABLE(_3D_CHICKEN_HIZ_PLANE_DISABLE_MSAA_4X_SNB));
6966
Akash Goel4e046322014-04-04 17:14:38 +05306967 /* WaDisable_RenderCache_OperationalFlush:snb */
6968 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
6969
Ville Syrjälä8d85d272014-02-04 21:59:15 +02006970 /*
6971 * BSpec recoomends 8x4 when MSAA is used,
6972 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02006973 *
6974 * Note that PS/WM thread counts depend on the WIZ hashing
6975 * disable bit, which we don't touch here, but it's good
6976 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjälä8d85d272014-02-04 21:59:15 +02006977 */
6978 I915_WRITE(GEN6_GT_MODE,
Damien Lespiau98533252014-12-08 17:33:51 +00006979 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
Ville Syrjälä8d85d272014-02-04 21:59:15 +02006980
Ville Syrjälä46f16e62016-10-31 22:37:22 +02006981 ilk_init_lp_watermarks(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006982
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006983 I915_WRITE(CACHE_MODE_0,
Daniel Vetter50743292012-04-26 22:02:54 +02006984 _MASKED_BIT_DISABLE(CM0_STC_EVICT_DISABLE_LRA_SNB));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006985
6986 I915_WRITE(GEN6_UCGCTL1,
6987 I915_READ(GEN6_UCGCTL1) |
6988 GEN6_BLBUNIT_CLOCK_GATE_DISABLE |
6989 GEN6_CSUNIT_CLOCK_GATE_DISABLE);
6990
6991 /* According to the BSpec vol1g, bit 12 (RCPBUNIT) clock
6992 * gating disable must be set. Failure to set it results in
6993 * flickering pixels due to Z write ordering failures after
6994 * some amount of runtime in the Mesa "fire" demo, and Unigine
6995 * Sanctuary and Tropics, and apparently anything else with
6996 * alpha test or pixel discard.
6997 *
6998 * According to the spec, bit 11 (RCCUNIT) must also be set,
6999 * but we didn't debug actual testcases to find it out.
Jesse Barnes0f846f82012-06-14 11:04:47 -07007000 *
Ville Syrjäläef593182014-01-22 21:32:47 +02007001 * WaDisableRCCUnitClockGating:snb
7002 * WaDisableRCPBUnitClockGating:snb
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007003 */
7004 I915_WRITE(GEN6_UCGCTL2,
7005 GEN6_RCPBUNIT_CLOCK_GATE_DISABLE |
7006 GEN6_RCCUNIT_CLOCK_GATE_DISABLE);
7007
Ville Syrjälä5eb146d2014-02-04 21:59:16 +02007008 /* WaStripsFansDisableFastClipPerformanceFix:snb */
Ville Syrjälä743b57d2014-02-04 21:59:17 +02007009 I915_WRITE(_3D_CHICKEN3,
7010 _MASKED_BIT_ENABLE(_3D_CHICKEN3_SF_DISABLE_FASTCLIP_CULL));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007011
7012 /*
Ville Syrjäläe927ecd2014-02-04 21:59:18 +02007013 * Bspec says:
7014 * "This bit must be set if 3DSTATE_CLIP clip mode is set to normal and
7015 * 3DSTATE_SF number of SF output attributes is more than 16."
7016 */
7017 I915_WRITE(_3D_CHICKEN3,
7018 _MASKED_BIT_ENABLE(_3D_CHICKEN3_SF_DISABLE_PIPELINED_ATTR_FETCH));
7019
7020 /*
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007021 * According to the spec the following bits should be
7022 * set in order to enable memory self-refresh and fbc:
7023 * The bit21 and bit22 of 0x42000
7024 * The bit21 and bit22 of 0x42004
7025 * The bit5 and bit7 of 0x42020
7026 * The bit14 of 0x70180
7027 * The bit14 of 0x71180
Damien Lespiau4bb35332013-06-14 15:23:24 +01007028 *
7029 * WaFbcAsynchFlipDisableFbcQueue:snb
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007030 */
7031 I915_WRITE(ILK_DISPLAY_CHICKEN1,
7032 I915_READ(ILK_DISPLAY_CHICKEN1) |
7033 ILK_FBCQ_DIS | ILK_PABSTRETCH_DIS);
7034 I915_WRITE(ILK_DISPLAY_CHICKEN2,
7035 I915_READ(ILK_DISPLAY_CHICKEN2) |
7036 ILK_DPARB_GATE | ILK_VSDPFD_FULL);
Damien Lespiau231e54f2012-10-19 17:55:41 +01007037 I915_WRITE(ILK_DSPCLK_GATE_D,
7038 I915_READ(ILK_DSPCLK_GATE_D) |
7039 ILK_DPARBUNIT_CLOCK_GATE_ENABLE |
7040 ILK_DPFDUNIT_CLOCK_GATE_ENABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007041
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007042 g4x_disable_trickle_feed(dev_priv);
Ben Widawskyf8f2ac92012-10-03 19:34:24 -07007043
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007044 cpt_init_clock_gating(dev_priv);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01007045
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007046 gen6_check_mch_setup(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007047}
7048
7049static void gen7_setup_fixed_func_scheduler(struct drm_i915_private *dev_priv)
7050{
7051 uint32_t reg = I915_READ(GEN7_FF_THREAD_MODE);
7052
Ville Syrjälä3aad9052014-01-22 21:32:59 +02007053 /*
Ville Syrjälä46680e02014-01-22 21:33:01 +02007054 * WaVSThreadDispatchOverride:ivb,vlv
Ville Syrjälä3aad9052014-01-22 21:32:59 +02007055 *
7056 * This actually overrides the dispatch
7057 * mode for all thread types.
7058 */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007059 reg &= ~GEN7_FF_SCHED_MASK;
7060 reg |= GEN7_FF_TS_SCHED_HW;
7061 reg |= GEN7_FF_VS_SCHED_HW;
7062 reg |= GEN7_FF_DS_SCHED_HW;
7063
7064 I915_WRITE(GEN7_FF_THREAD_MODE, reg);
7065}
7066
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007067static void lpt_init_clock_gating(struct drm_i915_private *dev_priv)
Paulo Zanoni17a303e2012-11-20 15:12:07 -02007068{
Paulo Zanoni17a303e2012-11-20 15:12:07 -02007069 /*
7070 * TODO: this bit should only be enabled when really needed, then
7071 * disabled when not needed anymore in order to save power.
7072 */
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01007073 if (HAS_PCH_LPT_LP(dev_priv))
Paulo Zanoni17a303e2012-11-20 15:12:07 -02007074 I915_WRITE(SOUTH_DSPCLK_GATE_D,
7075 I915_READ(SOUTH_DSPCLK_GATE_D) |
7076 PCH_LP_PARTITION_LEVEL_DISABLE);
Paulo Zanoni0a790cd2013-04-17 18:15:49 -03007077
7078 /* WADPOClockGatingDisable:hsw */
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03007079 I915_WRITE(TRANS_CHICKEN1(PIPE_A),
7080 I915_READ(TRANS_CHICKEN1(PIPE_A)) |
Paulo Zanoni0a790cd2013-04-17 18:15:49 -03007081 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
Paulo Zanoni17a303e2012-11-20 15:12:07 -02007082}
7083
Ville Syrjälä712bf362016-10-31 22:37:23 +02007084static void lpt_suspend_hw(struct drm_i915_private *dev_priv)
Imre Deak7d708ee2013-04-17 14:04:50 +03007085{
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01007086 if (HAS_PCH_LPT_LP(dev_priv)) {
Imre Deak7d708ee2013-04-17 14:04:50 +03007087 uint32_t val = I915_READ(SOUTH_DSPCLK_GATE_D);
7088
7089 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
7090 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
7091 }
7092}
7093
Imre Deak450174f2016-05-03 15:54:21 +03007094static void gen8_set_l3sqc_credits(struct drm_i915_private *dev_priv,
7095 int general_prio_credits,
7096 int high_prio_credits)
7097{
7098 u32 misccpctl;
7099
7100 /* WaTempDisableDOPClkGating:bdw */
7101 misccpctl = I915_READ(GEN7_MISCCPCTL);
7102 I915_WRITE(GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
7103
7104 I915_WRITE(GEN8_L3SQCREG1,
7105 L3_GENERAL_PRIO_CREDITS(general_prio_credits) |
7106 L3_HIGH_PRIO_CREDITS(high_prio_credits));
7107
7108 /*
7109 * Wait at least 100 clocks before re-enabling clock gating.
7110 * See the definition of L3SQCREG1 in BSpec.
7111 */
7112 POSTING_READ(GEN8_L3SQCREG1);
7113 udelay(1);
7114 I915_WRITE(GEN7_MISCCPCTL, misccpctl);
7115}
7116
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007117static void kabylake_init_clock_gating(struct drm_i915_private *dev_priv)
Mika Kuoppala9498dba2016-06-07 17:19:01 +03007118{
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007119 gen9_init_clock_gating(dev_priv);
Mika Kuoppala9498dba2016-06-07 17:19:01 +03007120
7121 /* WaDisableSDEUnitClockGating:kbl */
7122 if (IS_KBL_REVID(dev_priv, 0, KBL_REVID_B0))
7123 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
7124 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Mika Kuoppala8aeb7f62016-06-07 17:19:05 +03007125
7126 /* WaDisableGamClockGating:kbl */
7127 if (IS_KBL_REVID(dev_priv, 0, KBL_REVID_B0))
7128 I915_WRITE(GEN6_UCGCTL1, I915_READ(GEN6_UCGCTL1) |
7129 GEN6_GAMUNIT_CLOCK_GATE_DISABLE);
Mika Kuoppala031cd8c2016-06-07 17:19:18 +03007130
7131 /* WaFbcNukeOnHostModify:kbl */
7132 I915_WRITE(ILK_DPFC_CHICKEN, I915_READ(ILK_DPFC_CHICKEN) |
7133 ILK_DPFC_NUKE_ON_ANY_MODIFICATION);
Mika Kuoppala9498dba2016-06-07 17:19:01 +03007134}
7135
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007136static void skylake_init_clock_gating(struct drm_i915_private *dev_priv)
Daniel Vetterdc00b6a2016-05-19 09:14:20 +02007137{
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007138 gen9_init_clock_gating(dev_priv);
Mika Kuoppala44fff992016-06-07 17:19:09 +03007139
7140 /* WAC6entrylatency:skl */
7141 I915_WRITE(FBC_LLC_READ_CTRL, I915_READ(FBC_LLC_READ_CTRL) |
7142 FBC_LLC_FULLY_OPEN);
Mika Kuoppala031cd8c2016-06-07 17:19:18 +03007143
7144 /* WaFbcNukeOnHostModify:skl */
7145 I915_WRITE(ILK_DPFC_CHICKEN, I915_READ(ILK_DPFC_CHICKEN) |
7146 ILK_DPFC_NUKE_ON_ANY_MODIFICATION);
Daniel Vetterdc00b6a2016-05-19 09:14:20 +02007147}
7148
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007149static void broadwell_init_clock_gating(struct drm_i915_private *dev_priv)
Ben Widawsky1020a5c2013-11-02 21:07:06 -07007150{
Damien Lespiau07d27e22014-03-03 17:31:46 +00007151 enum pipe pipe;
Ben Widawsky1020a5c2013-11-02 21:07:06 -07007152
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007153 ilk_init_lp_watermarks(dev_priv);
Ben Widawsky50ed5fb2013-11-02 21:07:40 -07007154
Ben Widawskyab57fff2013-12-12 15:28:04 -08007155 /* WaSwitchSolVfFArbitrationPriority:bdw */
Ben Widawsky50ed5fb2013-11-02 21:07:40 -07007156 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07007157
Ben Widawskyab57fff2013-12-12 15:28:04 -08007158 /* WaPsrDPAMaskVBlankInSRD:bdw */
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07007159 I915_WRITE(CHICKEN_PAR1_1,
7160 I915_READ(CHICKEN_PAR1_1) | DPA_MASK_VBLANK_SRD);
7161
Ben Widawskyab57fff2013-12-12 15:28:04 -08007162 /* WaPsrDPRSUnmaskVBlankInSRD:bdw */
Damien Lespiau055e3932014-08-18 13:49:10 +01007163 for_each_pipe(dev_priv, pipe) {
Damien Lespiau07d27e22014-03-03 17:31:46 +00007164 I915_WRITE(CHICKEN_PIPESL_1(pipe),
Ville Syrjäläc7c65622014-03-05 13:05:45 +02007165 I915_READ(CHICKEN_PIPESL_1(pipe)) |
Ville Syrjälä8f670bb2014-03-05 13:05:47 +02007166 BDW_DPRS_MASK_VBLANK_SRD);
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07007167 }
Ben Widawsky63801f22013-12-12 17:26:03 -08007168
Ben Widawskyab57fff2013-12-12 15:28:04 -08007169 /* WaVSRefCountFullforceMissDisable:bdw */
7170 /* WaDSRefCountFullforceMissDisable:bdw */
7171 I915_WRITE(GEN7_FF_THREAD_MODE,
7172 I915_READ(GEN7_FF_THREAD_MODE) &
7173 ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
Ville Syrjälä36075a42014-02-04 21:59:21 +02007174
Ville Syrjälä295e8bb2014-02-27 21:59:01 +02007175 I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
7176 _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
Ville Syrjälä4f1ca9e2014-02-27 21:59:02 +02007177
7178 /* WaDisableSDEUnitClockGating:bdw */
7179 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
7180 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Damien Lespiau5d708682014-03-26 18:41:51 +00007181
Imre Deak450174f2016-05-03 15:54:21 +03007182 /* WaProgramL3SqcReg1Default:bdw */
7183 gen8_set_l3sqc_credits(dev_priv, 30, 2);
Ville Syrjälä4d487cf2015-05-19 20:32:56 +03007184
Ville Syrjälä6d50b062015-05-19 20:32:57 +03007185 /*
7186 * WaGttCachingOffByDefault:bdw
7187 * GTT cache may not work with big pages, so if those
7188 * are ever enabled GTT cache may need to be disabled.
7189 */
7190 I915_WRITE(HSW_GTT_CACHE_EN, GTT_CACHE_EN_ALL);
7191
Mika Kuoppala17e0adf2016-06-07 17:19:02 +03007192 /* WaKVMNotificationOnConfigChange:bdw */
7193 I915_WRITE(CHICKEN_PAR2_1, I915_READ(CHICKEN_PAR2_1)
7194 | KVM_CONFIG_CHANGE_NOTIFICATION_SELECT);
7195
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007196 lpt_init_clock_gating(dev_priv);
Ben Widawsky1020a5c2013-11-02 21:07:06 -07007197}
7198
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007199static void haswell_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03007200{
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007201 ilk_init_lp_watermarks(dev_priv);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03007202
Francisco Jerezf3fc4882013-10-02 15:53:16 -07007203 /* L3 caching of data atomics doesn't work -- disable it. */
7204 I915_WRITE(HSW_SCRATCH1, HSW_SCRATCH1_L3_DATA_ATOMICS_DISABLE);
7205 I915_WRITE(HSW_ROW_CHICKEN3,
7206 _MASKED_BIT_ENABLE(HSW_ROW_CHICKEN3_L3_GLOBAL_ATOMICS_DISABLE));
7207
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007208 /* This is required by WaCatErrorRejectionIssue:hsw */
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03007209 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
7210 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
7211 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
7212
Ville Syrjäläe36ea7f2014-01-22 21:33:00 +02007213 /* WaVSRefCountFullforceMissDisable:hsw */
7214 I915_WRITE(GEN7_FF_THREAD_MODE,
7215 I915_READ(GEN7_FF_THREAD_MODE) & ~GEN7_FF_VS_REF_CNT_FFME);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03007216
Akash Goel4e046322014-04-04 17:14:38 +05307217 /* WaDisable_RenderCache_OperationalFlush:hsw */
7218 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
7219
Chia-I Wufe27c602014-01-28 13:29:33 +08007220 /* enable HiZ Raw Stall Optimization */
7221 I915_WRITE(CACHE_MODE_0_GEN7,
7222 _MASKED_BIT_DISABLE(HIZ_RAW_STALL_OPT_DISABLE));
7223
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007224 /* WaDisable4x2SubspanOptimization:hsw */
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03007225 I915_WRITE(CACHE_MODE_1,
7226 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Eugeni Dodonov1544d9d2012-07-02 11:51:10 -03007227
Ville Syrjäläa12c4962014-02-04 21:59:20 +02007228 /*
7229 * BSpec recommends 8x4 when MSAA is used,
7230 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02007231 *
7232 * Note that PS/WM thread counts depend on the WIZ hashing
7233 * disable bit, which we don't touch here, but it's good
7234 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjäläa12c4962014-02-04 21:59:20 +02007235 */
7236 I915_WRITE(GEN7_GT_MODE,
Damien Lespiau98533252014-12-08 17:33:51 +00007237 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
Ville Syrjäläa12c4962014-02-04 21:59:20 +02007238
Kenneth Graunke94411592014-12-31 16:23:00 -08007239 /* WaSampleCChickenBitEnable:hsw */
7240 I915_WRITE(HALF_SLICE_CHICKEN3,
7241 _MASKED_BIT_ENABLE(HSW_SAMPLE_C_PERFORMANCE));
7242
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007243 /* WaSwitchSolVfFArbitrationPriority:hsw */
Ben Widawskye3dff582013-03-20 14:49:14 -07007244 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
7245
Paulo Zanoni90a88642013-05-03 17:23:45 -03007246 /* WaRsPkgCStateDisplayPMReq:hsw */
7247 I915_WRITE(CHICKEN_PAR1_1,
7248 I915_READ(CHICKEN_PAR1_1) | FORCE_ARB_IDLE_PLANES);
Eugeni Dodonov1544d9d2012-07-02 11:51:10 -03007249
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007250 lpt_init_clock_gating(dev_priv);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03007251}
7252
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007253static void ivybridge_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007254{
Ben Widawsky20848222012-05-04 18:58:59 -07007255 uint32_t snpcr;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007256
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007257 ilk_init_lp_watermarks(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007258
Damien Lespiau231e54f2012-10-19 17:55:41 +01007259 I915_WRITE(ILK_DSPCLK_GATE_D, ILK_VRHUNIT_CLOCK_GATE_DISABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007260
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007261 /* WaDisableEarlyCull:ivb */
Jesse Barnes87f80202012-10-02 17:43:41 -05007262 I915_WRITE(_3D_CHICKEN3,
7263 _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
7264
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007265 /* WaDisableBackToBackFlipFix:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007266 I915_WRITE(IVB_CHICKEN3,
7267 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
7268 CHICKEN3_DGMG_DONE_FIX_DISABLE);
7269
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007270 /* WaDisablePSDDualDispatchEnable:ivb */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01007271 if (IS_IVB_GT1(dev_priv))
Jesse Barnes12f33822012-10-25 12:15:45 -07007272 I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
7273 _MASKED_BIT_ENABLE(GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
Jesse Barnes12f33822012-10-25 12:15:45 -07007274
Akash Goel4e046322014-04-04 17:14:38 +05307275 /* WaDisable_RenderCache_OperationalFlush:ivb */
7276 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
7277
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007278 /* Apply the WaDisableRHWOOptimizationForRenderHang:ivb workaround. */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007279 I915_WRITE(GEN7_COMMON_SLICE_CHICKEN1,
7280 GEN7_CSC1_RHWO_OPT_DISABLE_IN_RCC);
7281
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007282 /* WaApplyL3ControlAndL3ChickenMode:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007283 I915_WRITE(GEN7_L3CNTLREG1,
7284 GEN7_WA_FOR_GEN7_L3_CONTROL);
7285 I915_WRITE(GEN7_L3_CHICKEN_MODE_REGISTER,
Jesse Barnes8ab43972012-10-25 12:15:42 -07007286 GEN7_WA_L3_CHICKEN_MODE);
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01007287 if (IS_IVB_GT1(dev_priv))
Jesse Barnes8ab43972012-10-25 12:15:42 -07007288 I915_WRITE(GEN7_ROW_CHICKEN2,
7289 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Ville Syrjälä412236c2014-01-22 21:32:44 +02007290 else {
7291 /* must write both registers */
7292 I915_WRITE(GEN7_ROW_CHICKEN2,
7293 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Jesse Barnes8ab43972012-10-25 12:15:42 -07007294 I915_WRITE(GEN7_ROW_CHICKEN2_GT2,
7295 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Ville Syrjälä412236c2014-01-22 21:32:44 +02007296 }
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007297
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007298 /* WaForceL3Serialization:ivb */
Jesse Barnes61939d92012-10-02 17:43:38 -05007299 I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
7300 ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
7301
Ville Syrjälä1b80a19a2014-01-22 21:32:53 +02007302 /*
Jesse Barnes0f846f82012-06-14 11:04:47 -07007303 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007304 * This implements the WaDisableRCZUnitClockGating:ivb workaround.
Jesse Barnes0f846f82012-06-14 11:04:47 -07007305 */
7306 I915_WRITE(GEN6_UCGCTL2,
Ville Syrjälä28acf3b2014-01-22 21:32:48 +02007307 GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
Jesse Barnes0f846f82012-06-14 11:04:47 -07007308
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007309 /* This is required by WaCatErrorRejectionIssue:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007310 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
7311 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
7312 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
7313
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007314 g4x_disable_trickle_feed(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007315
7316 gen7_setup_fixed_func_scheduler(dev_priv);
Daniel Vetter97e19302012-04-24 16:00:21 +02007317
Chris Wilson22721342014-03-04 09:41:43 +00007318 if (0) { /* causes HiZ corruption on ivb:gt1 */
7319 /* enable HiZ Raw Stall Optimization */
7320 I915_WRITE(CACHE_MODE_0_GEN7,
7321 _MASKED_BIT_DISABLE(HIZ_RAW_STALL_OPT_DISABLE));
7322 }
Chia-I Wu116f2b62014-01-28 13:29:34 +08007323
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007324 /* WaDisable4x2SubspanOptimization:ivb */
Daniel Vetter97e19302012-04-24 16:00:21 +02007325 I915_WRITE(CACHE_MODE_1,
7326 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Ben Widawsky20848222012-05-04 18:58:59 -07007327
Ville Syrjäläa607c1a2014-02-04 21:59:19 +02007328 /*
7329 * BSpec recommends 8x4 when MSAA is used,
7330 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02007331 *
7332 * Note that PS/WM thread counts depend on the WIZ hashing
7333 * disable bit, which we don't touch here, but it's good
7334 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjäläa607c1a2014-02-04 21:59:19 +02007335 */
7336 I915_WRITE(GEN7_GT_MODE,
Damien Lespiau98533252014-12-08 17:33:51 +00007337 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
Ville Syrjäläa607c1a2014-02-04 21:59:19 +02007338
Ben Widawsky20848222012-05-04 18:58:59 -07007339 snpcr = I915_READ(GEN6_MBCUNIT_SNPCR);
7340 snpcr &= ~GEN6_MBC_SNPCR_MASK;
7341 snpcr |= GEN6_MBC_SNPCR_MED;
7342 I915_WRITE(GEN6_MBCUNIT_SNPCR, snpcr);
Daniel Vetter3107bd42012-10-31 22:52:31 +01007343
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01007344 if (!HAS_PCH_NOP(dev_priv))
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007345 cpt_init_clock_gating(dev_priv);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01007346
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007347 gen6_check_mch_setup(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007348}
7349
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007350static void valleyview_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007351{
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007352 /* WaDisableEarlyCull:vlv */
Jesse Barnes87f80202012-10-02 17:43:41 -05007353 I915_WRITE(_3D_CHICKEN3,
7354 _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
7355
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007356 /* WaDisableBackToBackFlipFix:vlv */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007357 I915_WRITE(IVB_CHICKEN3,
7358 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
7359 CHICKEN3_DGMG_DONE_FIX_DISABLE);
7360
Ville Syrjäläfad7d362014-01-22 21:32:39 +02007361 /* WaPsdDispatchEnable:vlv */
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007362 /* WaDisablePSDDualDispatchEnable:vlv */
Jesse Barnes12f33822012-10-25 12:15:45 -07007363 I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
Jesse Barnesd3bc0302013-03-08 10:45:51 -08007364 _MASKED_BIT_ENABLE(GEN7_MAX_PS_THREAD_DEP |
7365 GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
Jesse Barnes12f33822012-10-25 12:15:45 -07007366
Akash Goel4e046322014-04-04 17:14:38 +05307367 /* WaDisable_RenderCache_OperationalFlush:vlv */
7368 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
7369
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007370 /* WaForceL3Serialization:vlv */
Jesse Barnes61939d92012-10-02 17:43:38 -05007371 I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
7372 ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
7373
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007374 /* WaDisableDopClockGating:vlv */
Jesse Barnes8ab43972012-10-25 12:15:42 -07007375 I915_WRITE(GEN7_ROW_CHICKEN2,
7376 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
7377
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007378 /* This is required by WaCatErrorRejectionIssue:vlv */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007379 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
7380 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
7381 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
7382
Ville Syrjälä46680e02014-01-22 21:33:01 +02007383 gen7_setup_fixed_func_scheduler(dev_priv);
7384
Ville Syrjälä3c0edae2014-01-22 21:32:56 +02007385 /*
Jesse Barnes0f846f82012-06-14 11:04:47 -07007386 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007387 * This implements the WaDisableRCZUnitClockGating:vlv workaround.
Jesse Barnes0f846f82012-06-14 11:04:47 -07007388 */
7389 I915_WRITE(GEN6_UCGCTL2,
Ville Syrjälä3c0edae2014-01-22 21:32:56 +02007390 GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
Jesse Barnes0f846f82012-06-14 11:04:47 -07007391
Akash Goelc98f5062014-03-24 23:00:07 +05307392 /* WaDisableL3Bank2xClockGate:vlv
7393 * Disabling L3 clock gating- MMIO 940c[25] = 1
7394 * Set bit 25, to disable L3_BANK_2x_CLK_GATING */
7395 I915_WRITE(GEN7_UCGCTL4,
7396 I915_READ(GEN7_UCGCTL4) | GEN7_L3BANK2X_CLOCK_GATE_DISABLE);
Jesse Barnese3f33d42012-06-14 11:04:50 -07007397
Ville Syrjäläafd58e72014-01-22 21:33:03 +02007398 /*
7399 * BSpec says this must be set, even though
7400 * WaDisable4x2SubspanOptimization isn't listed for VLV.
7401 */
Daniel Vetter6b26c862012-04-24 14:04:12 +02007402 I915_WRITE(CACHE_MODE_1,
7403 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Jesse Barnes79831172012-06-20 10:53:12 -07007404
7405 /*
Ville Syrjäläda2518f2015-01-21 19:38:01 +02007406 * BSpec recommends 8x4 when MSAA is used,
7407 * however in practice 16x4 seems fastest.
7408 *
7409 * Note that PS/WM thread counts depend on the WIZ hashing
7410 * disable bit, which we don't touch here, but it's good
7411 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
7412 */
7413 I915_WRITE(GEN7_GT_MODE,
7414 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
7415
7416 /*
Ville Syrjälä031994e2014-01-22 21:32:46 +02007417 * WaIncreaseL3CreditsForVLVB0:vlv
7418 * This is the hardware default actually.
7419 */
7420 I915_WRITE(GEN7_L3SQCREG1, VLV_B0_WA_L3SQCREG1_VALUE);
7421
7422 /*
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007423 * WaDisableVLVClockGating_VBIIssue:vlv
Jesse Barnes2d809572012-10-25 12:15:44 -07007424 * Disable clock gating on th GCFG unit to prevent a delay
7425 * in the reporting of vblank events.
7426 */
Ville Syrjälä7a0d1ee2014-01-22 21:33:04 +02007427 I915_WRITE(VLV_GUNIT_CLOCK_GATE, GCFG_DIS);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007428}
7429
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007430static void cherryview_init_clock_gating(struct drm_i915_private *dev_priv)
Ville Syrjäläa4565da2014-04-09 13:28:10 +03007431{
Ville Syrjälä232ce332014-04-09 13:28:35 +03007432 /* WaVSRefCountFullforceMissDisable:chv */
7433 /* WaDSRefCountFullforceMissDisable:chv */
7434 I915_WRITE(GEN7_FF_THREAD_MODE,
7435 I915_READ(GEN7_FF_THREAD_MODE) &
7436 ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
Ville Syrjäläacea6f92014-04-09 13:28:36 +03007437
7438 /* WaDisableSemaphoreAndSyncFlipWait:chv */
7439 I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
7440 _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
Ville Syrjälä08466972014-04-09 13:28:37 +03007441
7442 /* WaDisableCSUnitClockGating:chv */
7443 I915_WRITE(GEN6_UCGCTL1, I915_READ(GEN6_UCGCTL1) |
7444 GEN6_CSUNIT_CLOCK_GATE_DISABLE);
Ville Syrjäläc6317802014-04-09 13:28:38 +03007445
7446 /* WaDisableSDEUnitClockGating:chv */
7447 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
7448 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Ville Syrjälä6d50b062015-05-19 20:32:57 +03007449
7450 /*
Imre Deak450174f2016-05-03 15:54:21 +03007451 * WaProgramL3SqcReg1Default:chv
7452 * See gfxspecs/Related Documents/Performance Guide/
7453 * LSQC Setting Recommendations.
7454 */
7455 gen8_set_l3sqc_credits(dev_priv, 38, 2);
7456
7457 /*
Ville Syrjälä6d50b062015-05-19 20:32:57 +03007458 * GTT cache may not work with big pages, so if those
7459 * are ever enabled GTT cache may need to be disabled.
7460 */
7461 I915_WRITE(HSW_GTT_CACHE_EN, GTT_CACHE_EN_ALL);
Ville Syrjäläa4565da2014-04-09 13:28:10 +03007462}
7463
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007464static void g4x_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007465{
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007466 uint32_t dspclk_gate;
7467
7468 I915_WRITE(RENCLK_GATE_D1, 0);
7469 I915_WRITE(RENCLK_GATE_D2, VF_UNIT_CLOCK_GATE_DISABLE |
7470 GS_UNIT_CLOCK_GATE_DISABLE |
7471 CL_UNIT_CLOCK_GATE_DISABLE);
7472 I915_WRITE(RAMCLK_GATE_D, 0);
7473 dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE |
7474 OVRUNIT_CLOCK_GATE_DISABLE |
7475 OVCUNIT_CLOCK_GATE_DISABLE;
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01007476 if (IS_GM45(dev_priv))
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007477 dspclk_gate |= DSSUNIT_CLOCK_GATE_DISABLE;
7478 I915_WRITE(DSPCLK_GATE_D, dspclk_gate);
Daniel Vetter4358a372012-10-18 11:49:51 +02007479
7480 /* WaDisableRenderCachePipelinedFlush */
7481 I915_WRITE(CACHE_MODE_0,
7482 _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
Ville Syrjäläde1aa622013-06-07 10:47:01 +03007483
Akash Goel4e046322014-04-04 17:14:38 +05307484 /* WaDisable_RenderCache_OperationalFlush:g4x */
7485 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
7486
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007487 g4x_disable_trickle_feed(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007488}
7489
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007490static void crestline_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007491{
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007492 I915_WRITE(RENCLK_GATE_D1, I965_RCC_CLOCK_GATE_DISABLE);
7493 I915_WRITE(RENCLK_GATE_D2, 0);
7494 I915_WRITE(DSPCLK_GATE_D, 0);
7495 I915_WRITE(RAMCLK_GATE_D, 0);
7496 I915_WRITE16(DEUC, 0);
Ville Syrjälä20f94962013-06-07 10:47:02 +03007497 I915_WRITE(MI_ARB_STATE,
7498 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Akash Goel4e046322014-04-04 17:14:38 +05307499
7500 /* WaDisable_RenderCache_OperationalFlush:gen4 */
7501 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007502}
7503
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007504static void broadwater_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007505{
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007506 I915_WRITE(RENCLK_GATE_D1, I965_RCZ_CLOCK_GATE_DISABLE |
7507 I965_RCC_CLOCK_GATE_DISABLE |
7508 I965_RCPB_CLOCK_GATE_DISABLE |
7509 I965_ISC_CLOCK_GATE_DISABLE |
7510 I965_FBC_CLOCK_GATE_DISABLE);
7511 I915_WRITE(RENCLK_GATE_D2, 0);
Ville Syrjälä20f94962013-06-07 10:47:02 +03007512 I915_WRITE(MI_ARB_STATE,
7513 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Akash Goel4e046322014-04-04 17:14:38 +05307514
7515 /* WaDisable_RenderCache_OperationalFlush:gen4 */
7516 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007517}
7518
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007519static void gen3_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007520{
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007521 u32 dstate = I915_READ(D_STATE);
7522
7523 dstate |= DSTATE_PLL_D3_OFF | DSTATE_GFX_CLOCK_GATING |
7524 DSTATE_DOT_CLOCK_GATING;
7525 I915_WRITE(D_STATE, dstate);
Chris Wilson13a86b82012-04-24 14:51:43 +01007526
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02007527 if (IS_PINEVIEW(dev_priv))
Chris Wilson13a86b82012-04-24 14:51:43 +01007528 I915_WRITE(ECOSKPD, _MASKED_BIT_ENABLE(ECO_GATING_CX_ONLY));
Daniel Vetter974a3b02012-09-09 11:54:16 +02007529
7530 /* IIR "flip pending" means done if this bit is set */
7531 I915_WRITE(ECOSKPD, _MASKED_BIT_DISABLE(ECO_FLIP_DONE));
Ville Syrjälä12fabbcb92014-02-25 15:13:38 +02007532
7533 /* interrupts should cause a wake up from C3 */
Ville Syrjälä32992542014-02-25 15:13:39 +02007534 I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_AGPBUSY_INT_EN));
Ville Syrjälädbb42742014-02-25 15:13:41 +02007535
7536 /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
7537 I915_WRITE(MI_ARB_STATE, _MASKED_BIT_ENABLE(MI_ARB_C3_LP_WRITE_ENABLE));
Ville Syrjälä10383922014-08-15 01:21:54 +03007538
7539 I915_WRITE(MI_ARB_STATE,
7540 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007541}
7542
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007543static void i85x_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007544{
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007545 I915_WRITE(RENCLK_GATE_D1, SV_CLOCK_GATE_DISABLE);
Ville Syrjälä54e472a2014-02-25 15:13:40 +02007546
7547 /* interrupts should cause a wake up from C3 */
7548 I915_WRITE(MI_STATE, _MASKED_BIT_ENABLE(MI_AGPBUSY_INT_EN) |
7549 _MASKED_BIT_DISABLE(MI_AGPBUSY_830_MODE));
Ville Syrjälä10383922014-08-15 01:21:54 +03007550
7551 I915_WRITE(MEM_MODE,
7552 _MASKED_BIT_ENABLE(MEM_DISPLAY_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007553}
7554
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007555static void i830_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007556{
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007557 I915_WRITE(DSPCLK_GATE_D, OVRUNIT_CLOCK_GATE_DISABLE);
Ville Syrjälä10383922014-08-15 01:21:54 +03007558
7559 I915_WRITE(MEM_MODE,
7560 _MASKED_BIT_ENABLE(MEM_DISPLAY_A_TRICKLE_FEED_DISABLE) |
7561 _MASKED_BIT_ENABLE(MEM_DISPLAY_B_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007562}
7563
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007564void intel_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007565{
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007566 dev_priv->display.init_clock_gating(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007567}
7568
Ville Syrjälä712bf362016-10-31 22:37:23 +02007569void intel_suspend_hw(struct drm_i915_private *dev_priv)
Imre Deak7d708ee2013-04-17 14:04:50 +03007570{
Ville Syrjälä712bf362016-10-31 22:37:23 +02007571 if (HAS_PCH_LPT(dev_priv))
7572 lpt_suspend_hw(dev_priv);
Imre Deak7d708ee2013-04-17 14:04:50 +03007573}
7574
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007575static void nop_init_clock_gating(struct drm_i915_private *dev_priv)
Imre Deakbb400da2016-03-16 13:38:54 +02007576{
7577 DRM_DEBUG_KMS("No clock gating settings or workarounds applied.\n");
7578}
7579
7580/**
7581 * intel_init_clock_gating_hooks - setup the clock gating hooks
7582 * @dev_priv: device private
7583 *
7584 * Setup the hooks that configure which clocks of a given platform can be
7585 * gated and also apply various GT and display specific workarounds for these
7586 * platforms. Note that some GT specific workarounds are applied separately
7587 * when GPU contexts or batchbuffers start their execution.
7588 */
7589void intel_init_clock_gating_hooks(struct drm_i915_private *dev_priv)
7590{
7591 if (IS_SKYLAKE(dev_priv))
Daniel Vetterdc00b6a2016-05-19 09:14:20 +02007592 dev_priv->display.init_clock_gating = skylake_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02007593 else if (IS_KABYLAKE(dev_priv))
Mika Kuoppala9498dba2016-06-07 17:19:01 +03007594 dev_priv->display.init_clock_gating = kabylake_init_clock_gating;
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +02007595 else if (IS_GEN9_LP(dev_priv))
Imre Deakbb400da2016-03-16 13:38:54 +02007596 dev_priv->display.init_clock_gating = bxt_init_clock_gating;
7597 else if (IS_BROADWELL(dev_priv))
7598 dev_priv->display.init_clock_gating = broadwell_init_clock_gating;
7599 else if (IS_CHERRYVIEW(dev_priv))
7600 dev_priv->display.init_clock_gating = cherryview_init_clock_gating;
7601 else if (IS_HASWELL(dev_priv))
7602 dev_priv->display.init_clock_gating = haswell_init_clock_gating;
7603 else if (IS_IVYBRIDGE(dev_priv))
7604 dev_priv->display.init_clock_gating = ivybridge_init_clock_gating;
7605 else if (IS_VALLEYVIEW(dev_priv))
7606 dev_priv->display.init_clock_gating = valleyview_init_clock_gating;
7607 else if (IS_GEN6(dev_priv))
7608 dev_priv->display.init_clock_gating = gen6_init_clock_gating;
7609 else if (IS_GEN5(dev_priv))
7610 dev_priv->display.init_clock_gating = ironlake_init_clock_gating;
7611 else if (IS_G4X(dev_priv))
7612 dev_priv->display.init_clock_gating = g4x_init_clock_gating;
7613 else if (IS_CRESTLINE(dev_priv))
7614 dev_priv->display.init_clock_gating = crestline_init_clock_gating;
7615 else if (IS_BROADWATER(dev_priv))
7616 dev_priv->display.init_clock_gating = broadwater_init_clock_gating;
7617 else if (IS_GEN3(dev_priv))
7618 dev_priv->display.init_clock_gating = gen3_init_clock_gating;
7619 else if (IS_I85X(dev_priv) || IS_I865G(dev_priv))
7620 dev_priv->display.init_clock_gating = i85x_init_clock_gating;
7621 else if (IS_GEN2(dev_priv))
7622 dev_priv->display.init_clock_gating = i830_init_clock_gating;
7623 else {
7624 MISSING_CASE(INTEL_DEVID(dev_priv));
7625 dev_priv->display.init_clock_gating = nop_init_clock_gating;
7626 }
7627}
7628
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007629/* Set up chip specific power management-related functions */
Ville Syrjälä62d75df2016-10-31 22:37:25 +02007630void intel_init_pm(struct drm_i915_private *dev_priv)
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007631{
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02007632 intel_fbc_init(dev_priv);
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007633
Daniel Vetterc921aba2012-04-26 23:28:17 +02007634 /* For cxsr */
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02007635 if (IS_PINEVIEW(dev_priv))
Ville Syrjälä148ac1f2016-10-31 22:37:16 +02007636 i915_pineview_get_mem_freq(dev_priv);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01007637 else if (IS_GEN5(dev_priv))
Ville Syrjälä148ac1f2016-10-31 22:37:16 +02007638 i915_ironlake_get_mem_freq(dev_priv);
Daniel Vetterc921aba2012-04-26 23:28:17 +02007639
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007640 /* For FIFO watermark updates */
Ville Syrjälä62d75df2016-10-31 22:37:25 +02007641 if (INTEL_GEN(dev_priv) >= 9) {
Ville Syrjäläbb726512016-10-31 22:37:24 +02007642 skl_setup_wm_latency(dev_priv);
Maarten Lankhorste62929b2016-11-08 13:55:33 +01007643 dev_priv->display.initial_watermarks = skl_initial_wm;
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01007644 dev_priv->display.atomic_update_watermarks = skl_atomic_update_crtc_wm;
Matt Roper98d39492016-05-12 07:06:03 -07007645 dev_priv->display.compute_global_watermarks = skl_compute_wm;
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01007646 } else if (HAS_PCH_SPLIT(dev_priv)) {
Ville Syrjäläbb726512016-10-31 22:37:24 +02007647 ilk_setup_wm_latency(dev_priv);
Ville Syrjälä53615a52013-08-01 16:18:50 +03007648
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01007649 if ((IS_GEN5(dev_priv) && dev_priv->wm.pri_latency[1] &&
Ville Syrjäläbd602542014-01-07 16:14:10 +02007650 dev_priv->wm.spr_latency[1] && dev_priv->wm.cur_latency[1]) ||
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01007651 (!IS_GEN5(dev_priv) && dev_priv->wm.pri_latency[0] &&
Ville Syrjäläbd602542014-01-07 16:14:10 +02007652 dev_priv->wm.spr_latency[0] && dev_priv->wm.cur_latency[0])) {
Matt Roper86c8bbb2015-09-24 15:53:16 -07007653 dev_priv->display.compute_pipe_wm = ilk_compute_pipe_wm;
Matt Ropered4a6a72016-02-23 17:20:13 -08007654 dev_priv->display.compute_intermediate_wm =
7655 ilk_compute_intermediate_wm;
7656 dev_priv->display.initial_watermarks =
7657 ilk_initial_watermarks;
7658 dev_priv->display.optimize_watermarks =
7659 ilk_optimize_watermarks;
Ville Syrjäläbd602542014-01-07 16:14:10 +02007660 } else {
7661 DRM_DEBUG_KMS("Failed to read display plane latency. "
7662 "Disable CxSR\n");
7663 }
Ville Syrjälä6b6b3ee2016-11-28 19:37:07 +02007664 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Ville Syrjäläbb726512016-10-31 22:37:24 +02007665 vlv_setup_wm_latency(dev_priv);
Ville Syrjälä26e1fe42015-06-24 22:00:06 +03007666 dev_priv->display.update_wm = vlv_update_wm;
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02007667 } else if (IS_PINEVIEW(dev_priv)) {
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01007668 if (!intel_get_cxsr_latency(IS_PINEVIEW_G(dev_priv),
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007669 dev_priv->is_ddr3,
7670 dev_priv->fsb_freq,
7671 dev_priv->mem_freq)) {
7672 DRM_INFO("failed to find known CxSR latency "
7673 "(found ddr%s fsb freq %d, mem freq %d), "
7674 "disabling CxSR\n",
7675 (dev_priv->is_ddr3 == 1) ? "3" : "2",
7676 dev_priv->fsb_freq, dev_priv->mem_freq);
7677 /* Disable CxSR and never update its watermark again */
Imre Deak5209b1f2014-07-01 12:36:17 +03007678 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007679 dev_priv->display.update_wm = NULL;
7680 } else
7681 dev_priv->display.update_wm = pineview_update_wm;
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +01007682 } else if (IS_G4X(dev_priv)) {
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007683 dev_priv->display.update_wm = g4x_update_wm;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01007684 } else if (IS_GEN4(dev_priv)) {
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007685 dev_priv->display.update_wm = i965_update_wm;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01007686 } else if (IS_GEN3(dev_priv)) {
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007687 dev_priv->display.update_wm = i9xx_update_wm;
7688 dev_priv->display.get_fifo_size = i9xx_get_fifo_size;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01007689 } else if (IS_GEN2(dev_priv)) {
Ville Syrjälä62d75df2016-10-31 22:37:25 +02007690 if (INTEL_INFO(dev_priv)->num_pipes == 1) {
Daniel Vetterfeb56b92013-12-14 20:38:30 -02007691 dev_priv->display.update_wm = i845_update_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007692 dev_priv->display.get_fifo_size = i845_get_fifo_size;
Daniel Vetterfeb56b92013-12-14 20:38:30 -02007693 } else {
7694 dev_priv->display.update_wm = i9xx_update_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007695 dev_priv->display.get_fifo_size = i830_get_fifo_size;
Daniel Vetterfeb56b92013-12-14 20:38:30 -02007696 }
Daniel Vetterfeb56b92013-12-14 20:38:30 -02007697 } else {
7698 DRM_ERROR("unexpected fall-through in intel_init_pm\n");
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007699 }
7700}
7701
Lyude87660502016-08-17 15:55:53 -04007702static inline int gen6_check_mailbox_status(struct drm_i915_private *dev_priv)
7703{
7704 uint32_t flags =
7705 I915_READ_FW(GEN6_PCODE_MAILBOX) & GEN6_PCODE_ERROR_MASK;
7706
7707 switch (flags) {
7708 case GEN6_PCODE_SUCCESS:
7709 return 0;
7710 case GEN6_PCODE_UNIMPLEMENTED_CMD:
7711 case GEN6_PCODE_ILLEGAL_CMD:
7712 return -ENXIO;
7713 case GEN6_PCODE_MIN_FREQ_TABLE_GT_RATIO_OUT_OF_RANGE:
Chris Wilson7850d1c2016-08-26 11:59:26 +01007714 case GEN7_PCODE_MIN_FREQ_TABLE_GT_RATIO_OUT_OF_RANGE:
Lyude87660502016-08-17 15:55:53 -04007715 return -EOVERFLOW;
7716 case GEN6_PCODE_TIMEOUT:
7717 return -ETIMEDOUT;
7718 default:
7719 MISSING_CASE(flags)
7720 return 0;
7721 }
7722}
7723
7724static inline int gen7_check_mailbox_status(struct drm_i915_private *dev_priv)
7725{
7726 uint32_t flags =
7727 I915_READ_FW(GEN6_PCODE_MAILBOX) & GEN6_PCODE_ERROR_MASK;
7728
7729 switch (flags) {
7730 case GEN6_PCODE_SUCCESS:
7731 return 0;
7732 case GEN6_PCODE_ILLEGAL_CMD:
7733 return -ENXIO;
7734 case GEN7_PCODE_TIMEOUT:
7735 return -ETIMEDOUT;
7736 case GEN7_PCODE_ILLEGAL_DATA:
7737 return -EINVAL;
7738 case GEN7_PCODE_MIN_FREQ_TABLE_GT_RATIO_OUT_OF_RANGE:
7739 return -EOVERFLOW;
7740 default:
7741 MISSING_CASE(flags);
7742 return 0;
7743 }
7744}
7745
Tom O'Rourke151a49d2014-11-13 18:50:10 -08007746int sandybridge_pcode_read(struct drm_i915_private *dev_priv, u32 mbox, u32 *val)
Ben Widawsky42c05262012-09-26 10:34:00 -07007747{
Lyude87660502016-08-17 15:55:53 -04007748 int status;
7749
Jesse Barnes4fc688c2012-11-02 11:14:01 -07007750 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Ben Widawsky42c05262012-09-26 10:34:00 -07007751
Chris Wilson3f5582d2016-06-30 15:32:45 +01007752 /* GEN6_PCODE_* are outside of the forcewake domain, we can
7753 * use te fw I915_READ variants to reduce the amount of work
7754 * required when reading/writing.
7755 */
7756
7757 if (I915_READ_FW(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) {
Ben Widawsky42c05262012-09-26 10:34:00 -07007758 DRM_DEBUG_DRIVER("warning: pcode (read) mailbox access failed\n");
7759 return -EAGAIN;
7760 }
7761
Chris Wilson3f5582d2016-06-30 15:32:45 +01007762 I915_WRITE_FW(GEN6_PCODE_DATA, *val);
7763 I915_WRITE_FW(GEN6_PCODE_DATA1, 0);
7764 I915_WRITE_FW(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY | mbox);
Ben Widawsky42c05262012-09-26 10:34:00 -07007765
Chris Wilson3f5582d2016-06-30 15:32:45 +01007766 if (intel_wait_for_register_fw(dev_priv,
7767 GEN6_PCODE_MAILBOX, GEN6_PCODE_READY, 0,
7768 500)) {
Ben Widawsky42c05262012-09-26 10:34:00 -07007769 DRM_ERROR("timeout waiting for pcode read (%d) to finish\n", mbox);
7770 return -ETIMEDOUT;
7771 }
7772
Chris Wilson3f5582d2016-06-30 15:32:45 +01007773 *val = I915_READ_FW(GEN6_PCODE_DATA);
7774 I915_WRITE_FW(GEN6_PCODE_DATA, 0);
Ben Widawsky42c05262012-09-26 10:34:00 -07007775
Lyude87660502016-08-17 15:55:53 -04007776 if (INTEL_GEN(dev_priv) > 6)
7777 status = gen7_check_mailbox_status(dev_priv);
7778 else
7779 status = gen6_check_mailbox_status(dev_priv);
7780
7781 if (status) {
7782 DRM_DEBUG_DRIVER("warning: pcode (read) mailbox access failed: %d\n",
7783 status);
7784 return status;
7785 }
7786
Ben Widawsky42c05262012-09-26 10:34:00 -07007787 return 0;
7788}
7789
Chris Wilson3f5582d2016-06-30 15:32:45 +01007790int sandybridge_pcode_write(struct drm_i915_private *dev_priv,
Lyude87660502016-08-17 15:55:53 -04007791 u32 mbox, u32 val)
Ben Widawsky42c05262012-09-26 10:34:00 -07007792{
Lyude87660502016-08-17 15:55:53 -04007793 int status;
7794
Jesse Barnes4fc688c2012-11-02 11:14:01 -07007795 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Ben Widawsky42c05262012-09-26 10:34:00 -07007796
Chris Wilson3f5582d2016-06-30 15:32:45 +01007797 /* GEN6_PCODE_* are outside of the forcewake domain, we can
7798 * use te fw I915_READ variants to reduce the amount of work
7799 * required when reading/writing.
7800 */
7801
7802 if (I915_READ_FW(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) {
Ben Widawsky42c05262012-09-26 10:34:00 -07007803 DRM_DEBUG_DRIVER("warning: pcode (write) mailbox access failed\n");
7804 return -EAGAIN;
7805 }
7806
Chris Wilson3f5582d2016-06-30 15:32:45 +01007807 I915_WRITE_FW(GEN6_PCODE_DATA, val);
Imre Deak8bf41b72016-11-28 17:29:27 +02007808 I915_WRITE_FW(GEN6_PCODE_DATA1, 0);
Chris Wilson3f5582d2016-06-30 15:32:45 +01007809 I915_WRITE_FW(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY | mbox);
Ben Widawsky42c05262012-09-26 10:34:00 -07007810
Chris Wilson3f5582d2016-06-30 15:32:45 +01007811 if (intel_wait_for_register_fw(dev_priv,
7812 GEN6_PCODE_MAILBOX, GEN6_PCODE_READY, 0,
7813 500)) {
Ben Widawsky42c05262012-09-26 10:34:00 -07007814 DRM_ERROR("timeout waiting for pcode write (%d) to finish\n", mbox);
7815 return -ETIMEDOUT;
7816 }
7817
Chris Wilson3f5582d2016-06-30 15:32:45 +01007818 I915_WRITE_FW(GEN6_PCODE_DATA, 0);
Ben Widawsky42c05262012-09-26 10:34:00 -07007819
Lyude87660502016-08-17 15:55:53 -04007820 if (INTEL_GEN(dev_priv) > 6)
7821 status = gen7_check_mailbox_status(dev_priv);
7822 else
7823 status = gen6_check_mailbox_status(dev_priv);
7824
7825 if (status) {
7826 DRM_DEBUG_DRIVER("warning: pcode (write) mailbox access failed: %d\n",
7827 status);
7828 return status;
7829 }
7830
Ben Widawsky42c05262012-09-26 10:34:00 -07007831 return 0;
7832}
Jesse Barnesa0e4e192013-04-02 11:23:05 -07007833
Ville Syrjälädd06f882014-11-10 22:55:12 +02007834static int byt_gpu_freq(struct drm_i915_private *dev_priv, int val)
7835{
Ville Syrjäläc30fec62016-03-04 21:43:02 +02007836 /*
7837 * N = val - 0xb7
7838 * Slow = Fast = GPLL ref * N
7839 */
7840 return DIV_ROUND_CLOSEST(dev_priv->rps.gpll_ref_freq * (val - 0xb7), 1000);
Jesse Barnes855ba3b2013-04-17 15:54:57 -07007841}
7842
Fengguang Wub55dd642014-07-12 11:21:39 +02007843static int byt_freq_opcode(struct drm_i915_private *dev_priv, int val)
Jesse Barnes855ba3b2013-04-17 15:54:57 -07007844{
Ville Syrjäläc30fec62016-03-04 21:43:02 +02007845 return DIV_ROUND_CLOSEST(1000 * val, dev_priv->rps.gpll_ref_freq) + 0xb7;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07007846}
7847
Fengguang Wub55dd642014-07-12 11:21:39 +02007848static int chv_gpu_freq(struct drm_i915_private *dev_priv, int val)
Deepak S22b1b2f2014-07-12 14:54:33 +05307849{
Ville Syrjäläc30fec62016-03-04 21:43:02 +02007850 /*
7851 * N = val / 2
7852 * CU (slow) = CU2x (fast) / 2 = GPLL ref * N / 2
7853 */
7854 return DIV_ROUND_CLOSEST(dev_priv->rps.gpll_ref_freq * val, 2 * 2 * 1000);
Deepak S22b1b2f2014-07-12 14:54:33 +05307855}
7856
Fengguang Wub55dd642014-07-12 11:21:39 +02007857static int chv_freq_opcode(struct drm_i915_private *dev_priv, int val)
Deepak S22b1b2f2014-07-12 14:54:33 +05307858{
Ville Syrjälä1c147622014-08-18 14:42:43 +03007859 /* CHV needs even values */
Ville Syrjäläc30fec62016-03-04 21:43:02 +02007860 return DIV_ROUND_CLOSEST(2 * 1000 * val, dev_priv->rps.gpll_ref_freq) * 2;
Deepak S22b1b2f2014-07-12 14:54:33 +05307861}
7862
Ville Syrjälä616bc822015-01-23 21:04:25 +02007863int intel_gpu_freq(struct drm_i915_private *dev_priv, int val)
7864{
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03007865 if (IS_GEN9(dev_priv))
Mika Kuoppala500a3d22015-11-13 19:29:41 +02007866 return DIV_ROUND_CLOSEST(val * GT_FREQUENCY_MULTIPLIER,
7867 GEN9_FREQ_SCALER);
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03007868 else if (IS_CHERRYVIEW(dev_priv))
Ville Syrjälä616bc822015-01-23 21:04:25 +02007869 return chv_gpu_freq(dev_priv, val);
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03007870 else if (IS_VALLEYVIEW(dev_priv))
Ville Syrjälä616bc822015-01-23 21:04:25 +02007871 return byt_gpu_freq(dev_priv, val);
7872 else
7873 return val * GT_FREQUENCY_MULTIPLIER;
7874}
7875
Ville Syrjälä616bc822015-01-23 21:04:25 +02007876int intel_freq_opcode(struct drm_i915_private *dev_priv, int val)
7877{
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03007878 if (IS_GEN9(dev_priv))
Mika Kuoppala500a3d22015-11-13 19:29:41 +02007879 return DIV_ROUND_CLOSEST(val * GEN9_FREQ_SCALER,
7880 GT_FREQUENCY_MULTIPLIER);
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03007881 else if (IS_CHERRYVIEW(dev_priv))
Ville Syrjälä616bc822015-01-23 21:04:25 +02007882 return chv_freq_opcode(dev_priv, val);
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03007883 else if (IS_VALLEYVIEW(dev_priv))
Ville Syrjälä616bc822015-01-23 21:04:25 +02007884 return byt_freq_opcode(dev_priv, val);
7885 else
Mika Kuoppala500a3d22015-11-13 19:29:41 +02007886 return DIV_ROUND_CLOSEST(val, GT_FREQUENCY_MULTIPLIER);
Deepak S22b1b2f2014-07-12 14:54:33 +05307887}
7888
Chris Wilson6ad790c2015-04-07 16:20:31 +01007889struct request_boost {
7890 struct work_struct work;
Daniel Vettereed29a52015-05-21 14:21:25 +02007891 struct drm_i915_gem_request *req;
Chris Wilson6ad790c2015-04-07 16:20:31 +01007892};
7893
7894static void __intel_rps_boost_work(struct work_struct *work)
7895{
7896 struct request_boost *boost = container_of(work, struct request_boost, work);
Chris Wilsone61b9952015-04-27 13:41:24 +01007897 struct drm_i915_gem_request *req = boost->req;
Chris Wilson6ad790c2015-04-07 16:20:31 +01007898
Chris Wilsonf69a02c2016-07-01 17:23:16 +01007899 if (!i915_gem_request_completed(req))
Chris Wilsonc0336662016-05-06 15:40:21 +01007900 gen6_rps_boost(req->i915, NULL, req->emitted_jiffies);
Chris Wilson6ad790c2015-04-07 16:20:31 +01007901
Chris Wilsone8a261e2016-07-20 13:31:49 +01007902 i915_gem_request_put(req);
Chris Wilson6ad790c2015-04-07 16:20:31 +01007903 kfree(boost);
7904}
7905
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01007906void intel_queue_rps_boost_for_request(struct drm_i915_gem_request *req)
Chris Wilson6ad790c2015-04-07 16:20:31 +01007907{
7908 struct request_boost *boost;
7909
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01007910 if (req == NULL || INTEL_GEN(req->i915) < 6)
Chris Wilson6ad790c2015-04-07 16:20:31 +01007911 return;
7912
Chris Wilsonf69a02c2016-07-01 17:23:16 +01007913 if (i915_gem_request_completed(req))
Chris Wilsone61b9952015-04-27 13:41:24 +01007914 return;
7915
Chris Wilson6ad790c2015-04-07 16:20:31 +01007916 boost = kmalloc(sizeof(*boost), GFP_ATOMIC);
7917 if (boost == NULL)
7918 return;
7919
Chris Wilsone8a261e2016-07-20 13:31:49 +01007920 boost->req = i915_gem_request_get(req);
Chris Wilson6ad790c2015-04-07 16:20:31 +01007921
7922 INIT_WORK(&boost->work, __intel_rps_boost_work);
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01007923 queue_work(req->i915->wq, &boost->work);
Chris Wilson6ad790c2015-04-07 16:20:31 +01007924}
7925
Tvrtko Ursulin192aa182016-12-01 14:16:45 +00007926void intel_pm_setup(struct drm_i915_private *dev_priv)
Chris Wilson907b28c2013-07-19 20:36:52 +01007927{
Daniel Vetterf742a552013-12-06 10:17:53 +01007928 mutex_init(&dev_priv->rps.hw_lock);
Chris Wilson8d3afd72015-05-21 21:01:47 +01007929 spin_lock_init(&dev_priv->rps.client_lock);
Daniel Vetterf742a552013-12-06 10:17:53 +01007930
Chris Wilson54b4f682016-07-21 21:16:19 +01007931 INIT_DELAYED_WORK(&dev_priv->rps.autoenable_work,
7932 __intel_autoenable_gt_powersave);
Chris Wilson1854d5c2015-04-07 16:20:32 +01007933 INIT_LIST_HEAD(&dev_priv->rps.clients);
Paulo Zanoni5d584b22014-03-07 20:08:15 -03007934
Paulo Zanoni33688d92014-03-07 20:08:19 -03007935 dev_priv->pm.suspended = false;
Imre Deak1f814da2015-12-16 02:52:19 +02007936 atomic_set(&dev_priv->pm.wakeref_count, 0);
Chris Wilson907b28c2013-07-19 20:36:52 +01007937}