blob: 11bbc35a4c840097733d7fd052f4623730f1145f [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
Mika Kuoppalab033bb62016-06-07 17:19:04 +030059static void gen9_init_clock_gating(struct drm_device *dev)
60{
Mika Kuoppala11b28342016-06-07 17:19:04 +030061 struct drm_i915_private *dev_priv = dev->dev_private;
Mika Kuoppalab033bb62016-06-07 17:19:04 +030062
63 /* See Bspec note for PSR2_CTL bit 31, Wa#828:skl,bxt,kbl */
64 I915_WRITE(CHICKEN_PAR1_1,
65 I915_READ(CHICKEN_PAR1_1) | SKL_EDP_PSR_FIX_RDWRAP);
66
67 I915_WRITE(GEN8_CONFIG0,
68 I915_READ(GEN8_CONFIG0) | GEN9_DEFAULT_FIXES);
Mika Kuoppala590e8ff2016-06-07 17:19:13 +030069
70 /* WaEnableChickenDCPR:skl,bxt,kbl */
71 I915_WRITE(GEN8_CHICKEN_DCPR_1,
72 I915_READ(GEN8_CHICKEN_DCPR_1) | MASK_WAKEMEM);
Mika Kuoppala0f78dee2016-06-07 17:19:16 +030073
74 /* WaFbcTurnOffFbcWatermark:skl,bxt,kbl */
Mika Kuoppala303d4ea2016-06-07 17:19:17 +030075 /* WaFbcWakeMemOn:skl,bxt,kbl */
76 I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) |
77 DISP_FBC_WM_DIS |
78 DISP_FBC_MEMORY_WAKE);
Mika Kuoppalad1b4eef2016-06-07 17:19:19 +030079
80 /* WaFbcHighMemBwCorruptionAvoidance:skl,bxt,kbl */
81 I915_WRITE(ILK_DPFC_CHICKEN, I915_READ(ILK_DPFC_CHICKEN) |
82 ILK_DPFC_DISABLE_DUMMY0);
Mika Kuoppalab033bb62016-06-07 17:19:04 +030083}
84
Imre Deaka82abe42015-03-27 14:00:04 +020085static void bxt_init_clock_gating(struct drm_device *dev)
86{
Chris Wilsonfac5e232016-07-04 11:34:36 +010087 struct drm_i915_private *dev_priv = to_i915(dev);
Imre Deak32608ca2015-03-11 11:10:27 +020088
Mika Kuoppalab033bb62016-06-07 17:19:04 +030089 gen9_init_clock_gating(dev);
Daniel Vetterdc00b6a2016-05-19 09:14:20 +020090
Nick Hoatha7546152015-06-29 14:07:32 +010091 /* WaDisableSDEUnitClockGating:bxt */
92 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
93 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
94
Imre Deak32608ca2015-03-11 11:10:27 +020095 /*
96 * FIXME:
Ben Widawsky868434c2015-03-11 10:49:32 +020097 * GEN8_HDCUNIT_CLOCK_GATE_DISABLE_HDCREQ applies on 3x6 GT SKUs only.
Imre Deak32608ca2015-03-11 11:10:27 +020098 */
Imre Deak32608ca2015-03-11 11:10:27 +020099 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
Ben Widawsky868434c2015-03-11 10:49:32 +0200100 GEN8_HDCUNIT_CLOCK_GATE_DISABLE_HDCREQ);
Imre Deakd965e7ac2015-12-01 10:23:52 +0200101
102 /*
103 * Wa: Backlight PWM may stop in the asserted state, causing backlight
104 * to stay fully on.
105 */
106 if (IS_BXT_REVID(dev_priv, BXT_REVID_B0, REVID_FOREVER))
107 I915_WRITE(GEN9_CLKGATE_DIS_0, I915_READ(GEN9_CLKGATE_DIS_0) |
108 PWM1_GATING_DIS | PWM2_GATING_DIS);
Imre Deaka82abe42015-03-27 14:00:04 +0200109}
110
Daniel Vetterc921aba2012-04-26 23:28:17 +0200111static void i915_pineview_get_mem_freq(struct drm_device *dev)
112{
Chris Wilsonfac5e232016-07-04 11:34:36 +0100113 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetterc921aba2012-04-26 23:28:17 +0200114 u32 tmp;
115
116 tmp = I915_READ(CLKCFG);
117
118 switch (tmp & CLKCFG_FSB_MASK) {
119 case CLKCFG_FSB_533:
120 dev_priv->fsb_freq = 533; /* 133*4 */
121 break;
122 case CLKCFG_FSB_800:
123 dev_priv->fsb_freq = 800; /* 200*4 */
124 break;
125 case CLKCFG_FSB_667:
126 dev_priv->fsb_freq = 667; /* 167*4 */
127 break;
128 case CLKCFG_FSB_400:
129 dev_priv->fsb_freq = 400; /* 100*4 */
130 break;
131 }
132
133 switch (tmp & CLKCFG_MEM_MASK) {
134 case CLKCFG_MEM_533:
135 dev_priv->mem_freq = 533;
136 break;
137 case CLKCFG_MEM_667:
138 dev_priv->mem_freq = 667;
139 break;
140 case CLKCFG_MEM_800:
141 dev_priv->mem_freq = 800;
142 break;
143 }
144
145 /* detect pineview DDR3 setting */
146 tmp = I915_READ(CSHRDDR3CTL);
147 dev_priv->is_ddr3 = (tmp & CSHRDDR3CTL_DDR3) ? 1 : 0;
148}
149
150static void i915_ironlake_get_mem_freq(struct drm_device *dev)
151{
Chris Wilsonfac5e232016-07-04 11:34:36 +0100152 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetterc921aba2012-04-26 23:28:17 +0200153 u16 ddrpll, csipll;
154
155 ddrpll = I915_READ16(DDRMPLL1);
156 csipll = I915_READ16(CSIPLL0);
157
158 switch (ddrpll & 0xff) {
159 case 0xc:
160 dev_priv->mem_freq = 800;
161 break;
162 case 0x10:
163 dev_priv->mem_freq = 1066;
164 break;
165 case 0x14:
166 dev_priv->mem_freq = 1333;
167 break;
168 case 0x18:
169 dev_priv->mem_freq = 1600;
170 break;
171 default:
172 DRM_DEBUG_DRIVER("unknown memory frequency 0x%02x\n",
173 ddrpll & 0xff);
174 dev_priv->mem_freq = 0;
175 break;
176 }
177
Daniel Vetter20e4d402012-08-08 23:35:39 +0200178 dev_priv->ips.r_t = dev_priv->mem_freq;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200179
180 switch (csipll & 0x3ff) {
181 case 0x00c:
182 dev_priv->fsb_freq = 3200;
183 break;
184 case 0x00e:
185 dev_priv->fsb_freq = 3733;
186 break;
187 case 0x010:
188 dev_priv->fsb_freq = 4266;
189 break;
190 case 0x012:
191 dev_priv->fsb_freq = 4800;
192 break;
193 case 0x014:
194 dev_priv->fsb_freq = 5333;
195 break;
196 case 0x016:
197 dev_priv->fsb_freq = 5866;
198 break;
199 case 0x018:
200 dev_priv->fsb_freq = 6400;
201 break;
202 default:
203 DRM_DEBUG_DRIVER("unknown fsb frequency 0x%04x\n",
204 csipll & 0x3ff);
205 dev_priv->fsb_freq = 0;
206 break;
207 }
208
209 if (dev_priv->fsb_freq == 3200) {
Daniel Vetter20e4d402012-08-08 23:35:39 +0200210 dev_priv->ips.c_m = 0;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200211 } else if (dev_priv->fsb_freq > 3200 && dev_priv->fsb_freq <= 4800) {
Daniel Vetter20e4d402012-08-08 23:35:39 +0200212 dev_priv->ips.c_m = 1;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200213 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +0200214 dev_priv->ips.c_m = 2;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200215 }
216}
217
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300218static const struct cxsr_latency cxsr_latency_table[] = {
219 {1, 0, 800, 400, 3382, 33382, 3983, 33983}, /* DDR2-400 SC */
220 {1, 0, 800, 667, 3354, 33354, 3807, 33807}, /* DDR2-667 SC */
221 {1, 0, 800, 800, 3347, 33347, 3763, 33763}, /* DDR2-800 SC */
222 {1, 1, 800, 667, 6420, 36420, 6873, 36873}, /* DDR3-667 SC */
223 {1, 1, 800, 800, 5902, 35902, 6318, 36318}, /* DDR3-800 SC */
224
225 {1, 0, 667, 400, 3400, 33400, 4021, 34021}, /* DDR2-400 SC */
226 {1, 0, 667, 667, 3372, 33372, 3845, 33845}, /* DDR2-667 SC */
227 {1, 0, 667, 800, 3386, 33386, 3822, 33822}, /* DDR2-800 SC */
228 {1, 1, 667, 667, 6438, 36438, 6911, 36911}, /* DDR3-667 SC */
229 {1, 1, 667, 800, 5941, 35941, 6377, 36377}, /* DDR3-800 SC */
230
231 {1, 0, 400, 400, 3472, 33472, 4173, 34173}, /* DDR2-400 SC */
232 {1, 0, 400, 667, 3443, 33443, 3996, 33996}, /* DDR2-667 SC */
233 {1, 0, 400, 800, 3430, 33430, 3946, 33946}, /* DDR2-800 SC */
234 {1, 1, 400, 667, 6509, 36509, 7062, 37062}, /* DDR3-667 SC */
235 {1, 1, 400, 800, 5985, 35985, 6501, 36501}, /* DDR3-800 SC */
236
237 {0, 0, 800, 400, 3438, 33438, 4065, 34065}, /* DDR2-400 SC */
238 {0, 0, 800, 667, 3410, 33410, 3889, 33889}, /* DDR2-667 SC */
239 {0, 0, 800, 800, 3403, 33403, 3845, 33845}, /* DDR2-800 SC */
240 {0, 1, 800, 667, 6476, 36476, 6955, 36955}, /* DDR3-667 SC */
241 {0, 1, 800, 800, 5958, 35958, 6400, 36400}, /* DDR3-800 SC */
242
243 {0, 0, 667, 400, 3456, 33456, 4103, 34106}, /* DDR2-400 SC */
244 {0, 0, 667, 667, 3428, 33428, 3927, 33927}, /* DDR2-667 SC */
245 {0, 0, 667, 800, 3443, 33443, 3905, 33905}, /* DDR2-800 SC */
246 {0, 1, 667, 667, 6494, 36494, 6993, 36993}, /* DDR3-667 SC */
247 {0, 1, 667, 800, 5998, 35998, 6460, 36460}, /* DDR3-800 SC */
248
249 {0, 0, 400, 400, 3528, 33528, 4255, 34255}, /* DDR2-400 SC */
250 {0, 0, 400, 667, 3500, 33500, 4079, 34079}, /* DDR2-667 SC */
251 {0, 0, 400, 800, 3487, 33487, 4029, 34029}, /* DDR2-800 SC */
252 {0, 1, 400, 667, 6566, 36566, 7145, 37145}, /* DDR3-667 SC */
253 {0, 1, 400, 800, 6042, 36042, 6584, 36584}, /* DDR3-800 SC */
254};
255
Tvrtko Ursulin44a655c2016-10-13 11:09:23 +0100256static const struct cxsr_latency *intel_get_cxsr_latency(bool is_desktop,
257 bool is_ddr3,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300258 int fsb,
259 int mem)
260{
261 const struct cxsr_latency *latency;
262 int i;
263
264 if (fsb == 0 || mem == 0)
265 return NULL;
266
267 for (i = 0; i < ARRAY_SIZE(cxsr_latency_table); i++) {
268 latency = &cxsr_latency_table[i];
269 if (is_desktop == latency->is_desktop &&
270 is_ddr3 == latency->is_ddr3 &&
271 fsb == latency->fsb_freq && mem == latency->mem_freq)
272 return latency;
273 }
274
275 DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
276
277 return NULL;
278}
279
Ville Syrjäläfc1ac8d2015-03-05 21:19:52 +0200280static void chv_set_memory_dvfs(struct drm_i915_private *dev_priv, bool enable)
281{
282 u32 val;
283
284 mutex_lock(&dev_priv->rps.hw_lock);
285
286 val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
287 if (enable)
288 val &= ~FORCE_DDR_HIGH_FREQ;
289 else
290 val |= FORCE_DDR_HIGH_FREQ;
291 val &= ~FORCE_DDR_LOW_FREQ;
292 val |= FORCE_DDR_FREQ_REQ_ACK;
293 vlv_punit_write(dev_priv, PUNIT_REG_DDR_SETUP2, val);
294
295 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2) &
296 FORCE_DDR_FREQ_REQ_ACK) == 0, 3))
297 DRM_ERROR("timed out waiting for Punit DDR DVFS request\n");
298
299 mutex_unlock(&dev_priv->rps.hw_lock);
300}
301
Ville Syrjäläcfb41412015-03-05 21:19:51 +0200302static void chv_set_memory_pm5(struct drm_i915_private *dev_priv, bool enable)
303{
304 u32 val;
305
306 mutex_lock(&dev_priv->rps.hw_lock);
307
308 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
309 if (enable)
310 val |= DSP_MAXFIFO_PM5_ENABLE;
311 else
312 val &= ~DSP_MAXFIFO_PM5_ENABLE;
313 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
314
315 mutex_unlock(&dev_priv->rps.hw_lock);
316}
317
Ville Syrjäläf4998962015-03-10 17:02:21 +0200318#define FW_WM(value, plane) \
319 (((value) << DSPFW_ ## plane ## _SHIFT) & DSPFW_ ## plane ## _MASK)
320
Imre Deak5209b1f2014-07-01 12:36:17 +0300321void intel_set_memory_cxsr(struct drm_i915_private *dev_priv, bool enable)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300322{
Imre Deak5209b1f2014-07-01 12:36:17 +0300323 u32 val;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300324
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +0100325 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Imre Deak5209b1f2014-07-01 12:36:17 +0300326 I915_WRITE(FW_BLC_SELF_VLV, enable ? FW_CSPWRDWNEN : 0);
Ville Syrjäläa7a6c492015-06-24 22:00:01 +0300327 POSTING_READ(FW_BLC_SELF_VLV);
Ville Syrjälä852eb002015-06-24 22:00:07 +0300328 dev_priv->wm.vlv.cxsr = enable;
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +0100329 } else if (IS_G4X(dev_priv) || IS_CRESTLINE(dev_priv)) {
Imre Deak5209b1f2014-07-01 12:36:17 +0300330 I915_WRITE(FW_BLC_SELF, enable ? FW_BLC_SELF_EN : 0);
Ville Syrjäläa7a6c492015-06-24 22:00:01 +0300331 POSTING_READ(FW_BLC_SELF);
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +0200332 } else if (IS_PINEVIEW(dev_priv)) {
Imre Deak5209b1f2014-07-01 12:36:17 +0300333 val = I915_READ(DSPFW3) & ~PINEVIEW_SELF_REFRESH_EN;
334 val |= enable ? PINEVIEW_SELF_REFRESH_EN : 0;
335 I915_WRITE(DSPFW3, val);
Ville Syrjäläa7a6c492015-06-24 22:00:01 +0300336 POSTING_READ(DSPFW3);
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +0100337 } else if (IS_I945G(dev_priv) || IS_I945GM(dev_priv)) {
Imre Deak5209b1f2014-07-01 12:36:17 +0300338 val = enable ? _MASKED_BIT_ENABLE(FW_BLC_SELF_EN) :
339 _MASKED_BIT_DISABLE(FW_BLC_SELF_EN);
340 I915_WRITE(FW_BLC_SELF, val);
Ville Syrjäläa7a6c492015-06-24 22:00:01 +0300341 POSTING_READ(FW_BLC_SELF);
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +0100342 } else if (IS_I915GM(dev_priv)) {
Ville Syrjäläacb91352016-07-29 17:57:02 +0300343 /*
344 * FIXME can't find a bit like this for 915G, and
345 * and yet it does have the related watermark in
346 * FW_BLC_SELF. What's going on?
347 */
Imre Deak5209b1f2014-07-01 12:36:17 +0300348 val = enable ? _MASKED_BIT_ENABLE(INSTPM_SELF_EN) :
349 _MASKED_BIT_DISABLE(INSTPM_SELF_EN);
350 I915_WRITE(INSTPM, val);
Ville Syrjäläa7a6c492015-06-24 22:00:01 +0300351 POSTING_READ(INSTPM);
Imre Deak5209b1f2014-07-01 12:36:17 +0300352 } else {
353 return;
354 }
355
356 DRM_DEBUG_KMS("memory self-refresh is %s\n",
357 enable ? "enabled" : "disabled");
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300358}
359
Ville Syrjäläfc1ac8d2015-03-05 21:19:52 +0200360
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300361/*
362 * Latency for FIFO fetches is dependent on several factors:
363 * - memory configuration (speed, channels)
364 * - chipset
365 * - current MCH state
366 * It can be fairly high in some situations, so here we assume a fairly
367 * pessimal value. It's a tradeoff between extra memory fetches (if we
368 * set this value too high, the FIFO will fetch frequently to stay full)
369 * and power consumption (set it too low to save power and we might see
370 * FIFO underruns and display "flicker").
371 *
372 * A value of 5us seems to be a good balance; safe for very low end
373 * platforms but not overly aggressive on lower latency configs.
374 */
Chris Wilson5aef6002014-09-03 11:56:07 +0100375static const int pessimal_latency_ns = 5000;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300376
Ville Syrjäläb5004722015-03-05 21:19:47 +0200377#define VLV_FIFO_START(dsparb, dsparb2, lo_shift, hi_shift) \
378 ((((dsparb) >> (lo_shift)) & 0xff) | ((((dsparb2) >> (hi_shift)) & 0x1) << 8))
379
380static int vlv_get_fifo_size(struct drm_device *dev,
381 enum pipe pipe, int plane)
382{
Chris Wilsonfac5e232016-07-04 11:34:36 +0100383 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläb5004722015-03-05 21:19:47 +0200384 int sprite0_start, sprite1_start, size;
385
386 switch (pipe) {
387 uint32_t dsparb, dsparb2, dsparb3;
388 case PIPE_A:
389 dsparb = I915_READ(DSPARB);
390 dsparb2 = I915_READ(DSPARB2);
391 sprite0_start = VLV_FIFO_START(dsparb, dsparb2, 0, 0);
392 sprite1_start = VLV_FIFO_START(dsparb, dsparb2, 8, 4);
393 break;
394 case PIPE_B:
395 dsparb = I915_READ(DSPARB);
396 dsparb2 = I915_READ(DSPARB2);
397 sprite0_start = VLV_FIFO_START(dsparb, dsparb2, 16, 8);
398 sprite1_start = VLV_FIFO_START(dsparb, dsparb2, 24, 12);
399 break;
400 case PIPE_C:
401 dsparb2 = I915_READ(DSPARB2);
402 dsparb3 = I915_READ(DSPARB3);
403 sprite0_start = VLV_FIFO_START(dsparb3, dsparb2, 0, 16);
404 sprite1_start = VLV_FIFO_START(dsparb3, dsparb2, 8, 20);
405 break;
406 default:
407 return 0;
408 }
409
410 switch (plane) {
411 case 0:
412 size = sprite0_start;
413 break;
414 case 1:
415 size = sprite1_start - sprite0_start;
416 break;
417 case 2:
418 size = 512 - 1 - sprite1_start;
419 break;
420 default:
421 return 0;
422 }
423
424 DRM_DEBUG_KMS("Pipe %c %s %c FIFO size: %d\n",
425 pipe_name(pipe), plane == 0 ? "primary" : "sprite",
426 plane == 0 ? plane_name(pipe) : sprite_name(pipe, plane - 1),
427 size);
428
429 return size;
430}
431
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300432static int i9xx_get_fifo_size(struct drm_device *dev, int plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300433{
Chris Wilsonfac5e232016-07-04 11:34:36 +0100434 struct drm_i915_private *dev_priv = to_i915(dev);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300435 uint32_t dsparb = I915_READ(DSPARB);
436 int size;
437
438 size = dsparb & 0x7f;
439 if (plane)
440 size = ((dsparb >> DSPARB_CSTART_SHIFT) & 0x7f) - size;
441
442 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
443 plane ? "B" : "A", size);
444
445 return size;
446}
447
Daniel Vetterfeb56b92013-12-14 20:38:30 -0200448static int i830_get_fifo_size(struct drm_device *dev, int plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300449{
Chris Wilsonfac5e232016-07-04 11:34:36 +0100450 struct drm_i915_private *dev_priv = to_i915(dev);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300451 uint32_t dsparb = I915_READ(DSPARB);
452 int size;
453
454 size = dsparb & 0x1ff;
455 if (plane)
456 size = ((dsparb >> DSPARB_BEND_SHIFT) & 0x1ff) - size;
457 size >>= 1; /* Convert to cachelines */
458
459 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
460 plane ? "B" : "A", size);
461
462 return size;
463}
464
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300465static int i845_get_fifo_size(struct drm_device *dev, int plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300466{
Chris Wilsonfac5e232016-07-04 11:34:36 +0100467 struct drm_i915_private *dev_priv = to_i915(dev);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300468 uint32_t dsparb = I915_READ(DSPARB);
469 int size;
470
471 size = dsparb & 0x7f;
472 size >>= 2; /* Convert to cachelines */
473
474 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
475 plane ? "B" : "A",
476 size);
477
478 return size;
479}
480
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300481/* Pineview has different values for various configs */
482static const struct intel_watermark_params pineview_display_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300483 .fifo_size = PINEVIEW_DISPLAY_FIFO,
484 .max_wm = PINEVIEW_MAX_WM,
485 .default_wm = PINEVIEW_DFT_WM,
486 .guard_size = PINEVIEW_GUARD_WM,
487 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300488};
489static const struct intel_watermark_params pineview_display_hplloff_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300490 .fifo_size = PINEVIEW_DISPLAY_FIFO,
491 .max_wm = PINEVIEW_MAX_WM,
492 .default_wm = PINEVIEW_DFT_HPLLOFF_WM,
493 .guard_size = PINEVIEW_GUARD_WM,
494 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300495};
496static const struct intel_watermark_params pineview_cursor_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300497 .fifo_size = PINEVIEW_CURSOR_FIFO,
498 .max_wm = PINEVIEW_CURSOR_MAX_WM,
499 .default_wm = PINEVIEW_CURSOR_DFT_WM,
500 .guard_size = PINEVIEW_CURSOR_GUARD_WM,
501 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300502};
503static const struct intel_watermark_params pineview_cursor_hplloff_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300504 .fifo_size = PINEVIEW_CURSOR_FIFO,
505 .max_wm = PINEVIEW_CURSOR_MAX_WM,
506 .default_wm = PINEVIEW_CURSOR_DFT_WM,
507 .guard_size = PINEVIEW_CURSOR_GUARD_WM,
508 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300509};
510static const struct intel_watermark_params g4x_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300511 .fifo_size = G4X_FIFO_SIZE,
512 .max_wm = G4X_MAX_WM,
513 .default_wm = G4X_MAX_WM,
514 .guard_size = 2,
515 .cacheline_size = G4X_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300516};
517static const struct intel_watermark_params g4x_cursor_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300518 .fifo_size = I965_CURSOR_FIFO,
519 .max_wm = I965_CURSOR_MAX_WM,
520 .default_wm = I965_CURSOR_DFT_WM,
521 .guard_size = 2,
522 .cacheline_size = G4X_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300523};
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300524static const struct intel_watermark_params i965_cursor_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300525 .fifo_size = I965_CURSOR_FIFO,
526 .max_wm = I965_CURSOR_MAX_WM,
527 .default_wm = I965_CURSOR_DFT_WM,
528 .guard_size = 2,
529 .cacheline_size = I915_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300530};
531static const struct intel_watermark_params i945_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300532 .fifo_size = I945_FIFO_SIZE,
533 .max_wm = I915_MAX_WM,
534 .default_wm = 1,
535 .guard_size = 2,
536 .cacheline_size = I915_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300537};
538static const struct intel_watermark_params i915_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300539 .fifo_size = I915_FIFO_SIZE,
540 .max_wm = I915_MAX_WM,
541 .default_wm = 1,
542 .guard_size = 2,
543 .cacheline_size = I915_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300544};
Ville Syrjälä9d539102014-08-15 01:21:53 +0300545static const struct intel_watermark_params i830_a_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300546 .fifo_size = I855GM_FIFO_SIZE,
547 .max_wm = I915_MAX_WM,
548 .default_wm = 1,
549 .guard_size = 2,
550 .cacheline_size = I830_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300551};
Ville Syrjälä9d539102014-08-15 01:21:53 +0300552static const struct intel_watermark_params i830_bc_wm_info = {
553 .fifo_size = I855GM_FIFO_SIZE,
554 .max_wm = I915_MAX_WM/2,
555 .default_wm = 1,
556 .guard_size = 2,
557 .cacheline_size = I830_FIFO_LINE_SIZE,
558};
Daniel Vetterfeb56b92013-12-14 20:38:30 -0200559static const struct intel_watermark_params i845_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300560 .fifo_size = I830_FIFO_SIZE,
561 .max_wm = I915_MAX_WM,
562 .default_wm = 1,
563 .guard_size = 2,
564 .cacheline_size = I830_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300565};
566
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300567/**
568 * intel_calculate_wm - calculate watermark level
569 * @clock_in_khz: pixel clock
570 * @wm: chip FIFO params
Ville Syrjäläac484962016-01-20 21:05:26 +0200571 * @cpp: bytes per pixel
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300572 * @latency_ns: memory latency for the platform
573 *
574 * Calculate the watermark level (the level at which the display plane will
575 * start fetching from memory again). Each chip has a different display
576 * FIFO size and allocation, so the caller needs to figure that out and pass
577 * in the correct intel_watermark_params structure.
578 *
579 * As the pixel clock runs, the FIFO will be drained at a rate that depends
580 * on the pixel size. When it reaches the watermark level, it'll start
581 * fetching FIFO line sized based chunks from memory until the FIFO fills
582 * past the watermark point. If the FIFO drains completely, a FIFO underrun
583 * will occur, and a display engine hang could result.
584 */
585static unsigned long intel_calculate_wm(unsigned long clock_in_khz,
586 const struct intel_watermark_params *wm,
Ville Syrjäläac484962016-01-20 21:05:26 +0200587 int fifo_size, int cpp,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300588 unsigned long latency_ns)
589{
590 long entries_required, wm_size;
591
592 /*
593 * Note: we need to make sure we don't overflow for various clock &
594 * latency values.
595 * clocks go from a few thousand to several hundred thousand.
596 * latency is usually a few thousand
597 */
Ville Syrjäläac484962016-01-20 21:05:26 +0200598 entries_required = ((clock_in_khz / 1000) * cpp * latency_ns) /
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300599 1000;
600 entries_required = DIV_ROUND_UP(entries_required, wm->cacheline_size);
601
602 DRM_DEBUG_KMS("FIFO entries required for mode: %ld\n", entries_required);
603
604 wm_size = fifo_size - (entries_required + wm->guard_size);
605
606 DRM_DEBUG_KMS("FIFO watermark level: %ld\n", wm_size);
607
608 /* Don't promote wm_size to unsigned... */
609 if (wm_size > (long)wm->max_wm)
610 wm_size = wm->max_wm;
611 if (wm_size <= 0)
612 wm_size = wm->default_wm;
Ville Syrjäläd6feb192014-09-05 21:54:13 +0300613
614 /*
615 * Bspec seems to indicate that the value shouldn't be lower than
616 * 'burst size + 1'. Certainly 830 is quite unhappy with low values.
617 * Lets go for 8 which is the burst size since certain platforms
618 * already use a hardcoded 8 (which is what the spec says should be
619 * done).
620 */
621 if (wm_size <= 8)
622 wm_size = 8;
623
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300624 return wm_size;
625}
626
Ville Syrjäläefc26112016-10-31 22:37:04 +0200627static struct intel_crtc *single_enabled_crtc(struct drm_device *dev)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300628{
Ville Syrjäläefc26112016-10-31 22:37:04 +0200629 struct intel_crtc *crtc, *enabled = NULL;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300630
Ville Syrjäläefc26112016-10-31 22:37:04 +0200631 for_each_intel_crtc(dev, crtc) {
632 if (intel_crtc_active(crtc)) {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300633 if (enabled)
634 return NULL;
635 enabled = crtc;
636 }
637 }
638
639 return enabled;
640}
641
Ville Syrjälä432081b2016-10-31 22:37:03 +0200642static void pineview_update_wm(struct intel_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300643{
Ville Syrjälä432081b2016-10-31 22:37:03 +0200644 struct drm_device *dev = unused_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +0100645 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläefc26112016-10-31 22:37:04 +0200646 struct intel_crtc *crtc;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300647 const struct cxsr_latency *latency;
648 u32 reg;
649 unsigned long wm;
650
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +0100651 latency = intel_get_cxsr_latency(IS_PINEVIEW_G(dev_priv),
652 dev_priv->is_ddr3,
653 dev_priv->fsb_freq,
654 dev_priv->mem_freq);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300655 if (!latency) {
656 DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
Imre Deak5209b1f2014-07-01 12:36:17 +0300657 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300658 return;
659 }
660
661 crtc = single_enabled_crtc(dev);
662 if (crtc) {
Ville Syrjäläefc26112016-10-31 22:37:04 +0200663 const struct drm_display_mode *adjusted_mode =
664 &crtc->config->base.adjusted_mode;
665 const struct drm_framebuffer *fb =
666 crtc->base.primary->state->fb;
667 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +0300668 int clock = adjusted_mode->crtc_clock;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300669
670 /* Display SR */
671 wm = intel_calculate_wm(clock, &pineview_display_wm,
672 pineview_display_wm.fifo_size,
Ville Syrjäläac484962016-01-20 21:05:26 +0200673 cpp, latency->display_sr);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300674 reg = I915_READ(DSPFW1);
675 reg &= ~DSPFW_SR_MASK;
Ville Syrjäläf4998962015-03-10 17:02:21 +0200676 reg |= FW_WM(wm, SR);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300677 I915_WRITE(DSPFW1, reg);
678 DRM_DEBUG_KMS("DSPFW1 register is %x\n", reg);
679
680 /* cursor SR */
681 wm = intel_calculate_wm(clock, &pineview_cursor_wm,
682 pineview_display_wm.fifo_size,
Ville Syrjäläac484962016-01-20 21:05:26 +0200683 cpp, latency->cursor_sr);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300684 reg = I915_READ(DSPFW3);
685 reg &= ~DSPFW_CURSOR_SR_MASK;
Ville Syrjäläf4998962015-03-10 17:02:21 +0200686 reg |= FW_WM(wm, CURSOR_SR);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300687 I915_WRITE(DSPFW3, reg);
688
689 /* Display HPLL off SR */
690 wm = intel_calculate_wm(clock, &pineview_display_hplloff_wm,
691 pineview_display_hplloff_wm.fifo_size,
Ville Syrjäläac484962016-01-20 21:05:26 +0200692 cpp, latency->display_hpll_disable);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300693 reg = I915_READ(DSPFW3);
694 reg &= ~DSPFW_HPLL_SR_MASK;
Ville Syrjäläf4998962015-03-10 17:02:21 +0200695 reg |= FW_WM(wm, HPLL_SR);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300696 I915_WRITE(DSPFW3, reg);
697
698 /* cursor HPLL off SR */
699 wm = intel_calculate_wm(clock, &pineview_cursor_hplloff_wm,
700 pineview_display_hplloff_wm.fifo_size,
Ville Syrjäläac484962016-01-20 21:05:26 +0200701 cpp, latency->cursor_hpll_disable);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300702 reg = I915_READ(DSPFW3);
703 reg &= ~DSPFW_HPLL_CURSOR_MASK;
Ville Syrjäläf4998962015-03-10 17:02:21 +0200704 reg |= FW_WM(wm, HPLL_CURSOR);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300705 I915_WRITE(DSPFW3, reg);
706 DRM_DEBUG_KMS("DSPFW3 register is %x\n", reg);
707
Imre Deak5209b1f2014-07-01 12:36:17 +0300708 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300709 } else {
Imre Deak5209b1f2014-07-01 12:36:17 +0300710 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300711 }
712}
713
Ville Syrjäläf0ce2312016-10-31 22:37:08 +0200714static bool g4x_compute_wm0(struct drm_i915_private *dev_priv,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300715 int plane,
716 const struct intel_watermark_params *display,
717 int display_latency_ns,
718 const struct intel_watermark_params *cursor,
719 int cursor_latency_ns,
720 int *plane_wm,
721 int *cursor_wm)
722{
Ville Syrjäläefc26112016-10-31 22:37:04 +0200723 struct intel_crtc *crtc;
Ville Syrjälä4fe85902013-09-04 18:25:22 +0300724 const struct drm_display_mode *adjusted_mode;
Ville Syrjäläefc26112016-10-31 22:37:04 +0200725 const struct drm_framebuffer *fb;
Ville Syrjäläac484962016-01-20 21:05:26 +0200726 int htotal, hdisplay, clock, cpp;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300727 int line_time_us, line_count;
728 int entries, tlb_miss;
729
Ville Syrjäläb91eb5c2016-10-31 22:37:09 +0200730 crtc = intel_get_crtc_for_plane(dev_priv, plane);
Ville Syrjäläefc26112016-10-31 22:37:04 +0200731 if (!intel_crtc_active(crtc)) {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300732 *cursor_wm = cursor->guard_size;
733 *plane_wm = display->guard_size;
734 return false;
735 }
736
Ville Syrjäläefc26112016-10-31 22:37:04 +0200737 adjusted_mode = &crtc->config->base.adjusted_mode;
738 fb = crtc->base.primary->state->fb;
Damien Lespiau241bfc32013-09-25 16:45:37 +0100739 clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -0800740 htotal = adjusted_mode->crtc_htotal;
Ville Syrjäläefc26112016-10-31 22:37:04 +0200741 hdisplay = crtc->config->pipe_src_w;
742 cpp = drm_format_plane_cpp(fb->pixel_format, 0);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300743
744 /* Use the small buffer method to calculate plane watermark */
Ville Syrjäläac484962016-01-20 21:05:26 +0200745 entries = ((clock * cpp / 1000) * display_latency_ns) / 1000;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300746 tlb_miss = display->fifo_size*display->cacheline_size - hdisplay * 8;
747 if (tlb_miss > 0)
748 entries += tlb_miss;
749 entries = DIV_ROUND_UP(entries, display->cacheline_size);
750 *plane_wm = entries + display->guard_size;
751 if (*plane_wm > (int)display->max_wm)
752 *plane_wm = display->max_wm;
753
754 /* Use the large buffer method to calculate cursor watermark */
Ville Syrjälä922044c2014-02-14 14:18:57 +0200755 line_time_us = max(htotal * 1000 / clock, 1);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300756 line_count = (cursor_latency_ns / line_time_us + 1000) / 1000;
Ville Syrjäläefc26112016-10-31 22:37:04 +0200757 entries = line_count * crtc->base.cursor->state->crtc_w * cpp;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300758 tlb_miss = cursor->fifo_size*cursor->cacheline_size - hdisplay * 8;
759 if (tlb_miss > 0)
760 entries += tlb_miss;
761 entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
762 *cursor_wm = entries + cursor->guard_size;
763 if (*cursor_wm > (int)cursor->max_wm)
764 *cursor_wm = (int)cursor->max_wm;
765
766 return true;
767}
768
769/*
770 * Check the wm result.
771 *
772 * If any calculated watermark values is larger than the maximum value that
773 * can be programmed into the associated watermark register, that watermark
774 * must be disabled.
775 */
Ville Syrjäläf0ce2312016-10-31 22:37:08 +0200776static bool g4x_check_srwm(struct drm_i915_private *dev_priv,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300777 int display_wm, int cursor_wm,
778 const struct intel_watermark_params *display,
779 const struct intel_watermark_params *cursor)
780{
781 DRM_DEBUG_KMS("SR watermark: display plane %d, cursor %d\n",
782 display_wm, cursor_wm);
783
784 if (display_wm > display->max_wm) {
Tvrtko Ursulinae9400c2016-10-13 11:09:25 +0100785 DRM_DEBUG_KMS("display watermark is too large(%d/%u), disabling\n",
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300786 display_wm, display->max_wm);
787 return false;
788 }
789
790 if (cursor_wm > cursor->max_wm) {
Tvrtko Ursulinae9400c2016-10-13 11:09:25 +0100791 DRM_DEBUG_KMS("cursor watermark is too large(%d/%u), disabling\n",
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300792 cursor_wm, cursor->max_wm);
793 return false;
794 }
795
796 if (!(display_wm || cursor_wm)) {
797 DRM_DEBUG_KMS("SR latency is 0, disabling\n");
798 return false;
799 }
800
801 return true;
802}
803
Ville Syrjäläf0ce2312016-10-31 22:37:08 +0200804static bool g4x_compute_srwm(struct drm_i915_private *dev_priv,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300805 int plane,
806 int latency_ns,
807 const struct intel_watermark_params *display,
808 const struct intel_watermark_params *cursor,
809 int *display_wm, int *cursor_wm)
810{
Ville Syrjäläefc26112016-10-31 22:37:04 +0200811 struct intel_crtc *crtc;
Ville Syrjälä4fe85902013-09-04 18:25:22 +0300812 const struct drm_display_mode *adjusted_mode;
Ville Syrjäläefc26112016-10-31 22:37:04 +0200813 const struct drm_framebuffer *fb;
Ville Syrjäläac484962016-01-20 21:05:26 +0200814 int hdisplay, htotal, cpp, clock;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300815 unsigned long line_time_us;
816 int line_count, line_size;
817 int small, large;
818 int entries;
819
820 if (!latency_ns) {
821 *display_wm = *cursor_wm = 0;
822 return false;
823 }
824
Ville Syrjäläb91eb5c2016-10-31 22:37:09 +0200825 crtc = intel_get_crtc_for_plane(dev_priv, plane);
Ville Syrjäläefc26112016-10-31 22:37:04 +0200826 adjusted_mode = &crtc->config->base.adjusted_mode;
827 fb = crtc->base.primary->state->fb;
Damien Lespiau241bfc32013-09-25 16:45:37 +0100828 clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -0800829 htotal = adjusted_mode->crtc_htotal;
Ville Syrjäläefc26112016-10-31 22:37:04 +0200830 hdisplay = crtc->config->pipe_src_w;
831 cpp = drm_format_plane_cpp(fb->pixel_format, 0);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300832
Ville Syrjälä922044c2014-02-14 14:18:57 +0200833 line_time_us = max(htotal * 1000 / clock, 1);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300834 line_count = (latency_ns / line_time_us + 1000) / 1000;
Ville Syrjäläac484962016-01-20 21:05:26 +0200835 line_size = hdisplay * cpp;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300836
837 /* Use the minimum of the small and large buffer method for primary */
Ville Syrjäläac484962016-01-20 21:05:26 +0200838 small = ((clock * cpp / 1000) * latency_ns) / 1000;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300839 large = line_count * line_size;
840
841 entries = DIV_ROUND_UP(min(small, large), display->cacheline_size);
842 *display_wm = entries + display->guard_size;
843
844 /* calculate the self-refresh watermark for display cursor */
Ville Syrjäläefc26112016-10-31 22:37:04 +0200845 entries = line_count * cpp * crtc->base.cursor->state->crtc_w;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300846 entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
847 *cursor_wm = entries + cursor->guard_size;
848
Ville Syrjäläf0ce2312016-10-31 22:37:08 +0200849 return g4x_check_srwm(dev_priv,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300850 *display_wm, *cursor_wm,
851 display, cursor);
852}
853
Ville Syrjälä15665972015-03-10 16:16:28 +0200854#define FW_WM_VLV(value, plane) \
855 (((value) << DSPFW_ ## plane ## _SHIFT) & DSPFW_ ## plane ## _MASK_VLV)
856
Ville Syrjälä0018fda2015-03-05 21:19:45 +0200857static void vlv_write_wm_values(struct intel_crtc *crtc,
858 const struct vlv_wm_values *wm)
859{
860 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
861 enum pipe pipe = crtc->pipe;
862
863 I915_WRITE(VLV_DDL(pipe),
864 (wm->ddl[pipe].cursor << DDL_CURSOR_SHIFT) |
865 (wm->ddl[pipe].sprite[1] << DDL_SPRITE_SHIFT(1)) |
866 (wm->ddl[pipe].sprite[0] << DDL_SPRITE_SHIFT(0)) |
867 (wm->ddl[pipe].primary << DDL_PLANE_SHIFT));
868
Ville Syrjäläae801522015-03-05 21:19:49 +0200869 I915_WRITE(DSPFW1,
Ville Syrjälä15665972015-03-10 16:16:28 +0200870 FW_WM(wm->sr.plane, SR) |
871 FW_WM(wm->pipe[PIPE_B].cursor, CURSORB) |
872 FW_WM_VLV(wm->pipe[PIPE_B].primary, PLANEB) |
873 FW_WM_VLV(wm->pipe[PIPE_A].primary, PLANEA));
Ville Syrjäläae801522015-03-05 21:19:49 +0200874 I915_WRITE(DSPFW2,
Ville Syrjälä15665972015-03-10 16:16:28 +0200875 FW_WM_VLV(wm->pipe[PIPE_A].sprite[1], SPRITEB) |
876 FW_WM(wm->pipe[PIPE_A].cursor, CURSORA) |
877 FW_WM_VLV(wm->pipe[PIPE_A].sprite[0], SPRITEA));
Ville Syrjäläae801522015-03-05 21:19:49 +0200878 I915_WRITE(DSPFW3,
Ville Syrjälä15665972015-03-10 16:16:28 +0200879 FW_WM(wm->sr.cursor, CURSOR_SR));
Ville Syrjäläae801522015-03-05 21:19:49 +0200880
881 if (IS_CHERRYVIEW(dev_priv)) {
882 I915_WRITE(DSPFW7_CHV,
Ville Syrjälä15665972015-03-10 16:16:28 +0200883 FW_WM_VLV(wm->pipe[PIPE_B].sprite[1], SPRITED) |
884 FW_WM_VLV(wm->pipe[PIPE_B].sprite[0], SPRITEC));
Ville Syrjäläae801522015-03-05 21:19:49 +0200885 I915_WRITE(DSPFW8_CHV,
Ville Syrjälä15665972015-03-10 16:16:28 +0200886 FW_WM_VLV(wm->pipe[PIPE_C].sprite[1], SPRITEF) |
887 FW_WM_VLV(wm->pipe[PIPE_C].sprite[0], SPRITEE));
Ville Syrjäläae801522015-03-05 21:19:49 +0200888 I915_WRITE(DSPFW9_CHV,
Ville Syrjälä15665972015-03-10 16:16:28 +0200889 FW_WM_VLV(wm->pipe[PIPE_C].primary, PLANEC) |
890 FW_WM(wm->pipe[PIPE_C].cursor, CURSORC));
Ville Syrjäläae801522015-03-05 21:19:49 +0200891 I915_WRITE(DSPHOWM,
Ville Syrjälä15665972015-03-10 16:16:28 +0200892 FW_WM(wm->sr.plane >> 9, SR_HI) |
893 FW_WM(wm->pipe[PIPE_C].sprite[1] >> 8, SPRITEF_HI) |
894 FW_WM(wm->pipe[PIPE_C].sprite[0] >> 8, SPRITEE_HI) |
895 FW_WM(wm->pipe[PIPE_C].primary >> 8, PLANEC_HI) |
896 FW_WM(wm->pipe[PIPE_B].sprite[1] >> 8, SPRITED_HI) |
897 FW_WM(wm->pipe[PIPE_B].sprite[0] >> 8, SPRITEC_HI) |
898 FW_WM(wm->pipe[PIPE_B].primary >> 8, PLANEB_HI) |
899 FW_WM(wm->pipe[PIPE_A].sprite[1] >> 8, SPRITEB_HI) |
900 FW_WM(wm->pipe[PIPE_A].sprite[0] >> 8, SPRITEA_HI) |
901 FW_WM(wm->pipe[PIPE_A].primary >> 8, PLANEA_HI));
Ville Syrjäläae801522015-03-05 21:19:49 +0200902 } else {
903 I915_WRITE(DSPFW7,
Ville Syrjälä15665972015-03-10 16:16:28 +0200904 FW_WM_VLV(wm->pipe[PIPE_B].sprite[1], SPRITED) |
905 FW_WM_VLV(wm->pipe[PIPE_B].sprite[0], SPRITEC));
Ville Syrjäläae801522015-03-05 21:19:49 +0200906 I915_WRITE(DSPHOWM,
Ville Syrjälä15665972015-03-10 16:16:28 +0200907 FW_WM(wm->sr.plane >> 9, SR_HI) |
908 FW_WM(wm->pipe[PIPE_B].sprite[1] >> 8, SPRITED_HI) |
909 FW_WM(wm->pipe[PIPE_B].sprite[0] >> 8, SPRITEC_HI) |
910 FW_WM(wm->pipe[PIPE_B].primary >> 8, PLANEB_HI) |
911 FW_WM(wm->pipe[PIPE_A].sprite[1] >> 8, SPRITEB_HI) |
912 FW_WM(wm->pipe[PIPE_A].sprite[0] >> 8, SPRITEA_HI) |
913 FW_WM(wm->pipe[PIPE_A].primary >> 8, PLANEA_HI));
Ville Syrjäläae801522015-03-05 21:19:49 +0200914 }
915
Ville Syrjälä2cb389b2015-06-24 22:00:10 +0300916 /* zero (unused) WM1 watermarks */
917 I915_WRITE(DSPFW4, 0);
918 I915_WRITE(DSPFW5, 0);
919 I915_WRITE(DSPFW6, 0);
920 I915_WRITE(DSPHOWM1, 0);
921
Ville Syrjäläae801522015-03-05 21:19:49 +0200922 POSTING_READ(DSPFW1);
Ville Syrjälä0018fda2015-03-05 21:19:45 +0200923}
924
Ville Syrjälä15665972015-03-10 16:16:28 +0200925#undef FW_WM_VLV
926
Ville Syrjälä6eb1a682015-06-24 22:00:03 +0300927enum vlv_wm_level {
928 VLV_WM_LEVEL_PM2,
929 VLV_WM_LEVEL_PM5,
930 VLV_WM_LEVEL_DDR_DVFS,
Ville Syrjälä6eb1a682015-06-24 22:00:03 +0300931};
932
Ville Syrjälä262cd2e2015-06-24 22:00:04 +0300933/* latency must be in 0.1us units. */
934static unsigned int vlv_wm_method2(unsigned int pixel_rate,
935 unsigned int pipe_htotal,
936 unsigned int horiz_pixels,
Ville Syrjäläac484962016-01-20 21:05:26 +0200937 unsigned int cpp,
Ville Syrjälä262cd2e2015-06-24 22:00:04 +0300938 unsigned int latency)
939{
940 unsigned int ret;
941
942 ret = (latency * pixel_rate) / (pipe_htotal * 10000);
Ville Syrjäläac484962016-01-20 21:05:26 +0200943 ret = (ret + 1) * horiz_pixels * cpp;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +0300944 ret = DIV_ROUND_UP(ret, 64);
945
946 return ret;
947}
948
949static void vlv_setup_wm_latency(struct drm_device *dev)
950{
Chris Wilsonfac5e232016-07-04 11:34:36 +0100951 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +0300952
953 /* all latencies in usec */
954 dev_priv->wm.pri_latency[VLV_WM_LEVEL_PM2] = 3;
955
Ville Syrjälä58590c12015-09-08 21:05:12 +0300956 dev_priv->wm.max_level = VLV_WM_LEVEL_PM2;
957
Ville Syrjälä262cd2e2015-06-24 22:00:04 +0300958 if (IS_CHERRYVIEW(dev_priv)) {
959 dev_priv->wm.pri_latency[VLV_WM_LEVEL_PM5] = 12;
960 dev_priv->wm.pri_latency[VLV_WM_LEVEL_DDR_DVFS] = 33;
Ville Syrjälä58590c12015-09-08 21:05:12 +0300961
962 dev_priv->wm.max_level = VLV_WM_LEVEL_DDR_DVFS;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +0300963 }
964}
965
966static uint16_t vlv_compute_wm_level(struct intel_plane *plane,
967 struct intel_crtc *crtc,
968 const struct intel_plane_state *state,
969 int level)
970{
971 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
Ville Syrjäläac484962016-01-20 21:05:26 +0200972 int clock, htotal, cpp, width, wm;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +0300973
974 if (dev_priv->wm.pri_latency[level] == 0)
975 return USHRT_MAX;
976
Ville Syrjälä936e71e2016-07-26 19:06:59 +0300977 if (!state->base.visible)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +0300978 return 0;
979
Ville Syrjäläac484962016-01-20 21:05:26 +0200980 cpp = drm_format_plane_cpp(state->base.fb->pixel_format, 0);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +0300981 clock = crtc->config->base.adjusted_mode.crtc_clock;
982 htotal = crtc->config->base.adjusted_mode.crtc_htotal;
983 width = crtc->config->pipe_src_w;
984 if (WARN_ON(htotal == 0))
985 htotal = 1;
986
987 if (plane->base.type == DRM_PLANE_TYPE_CURSOR) {
988 /*
989 * FIXME the formula gives values that are
990 * too big for the cursor FIFO, and hence we
991 * would never be able to use cursors. For
992 * now just hardcode the watermark.
993 */
994 wm = 63;
995 } else {
Ville Syrjäläac484962016-01-20 21:05:26 +0200996 wm = vlv_wm_method2(clock, htotal, width, cpp,
Ville Syrjälä262cd2e2015-06-24 22:00:04 +0300997 dev_priv->wm.pri_latency[level] * 10);
998 }
999
1000 return min_t(int, wm, USHRT_MAX);
1001}
1002
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001003static void vlv_compute_fifo(struct intel_crtc *crtc)
1004{
1005 struct drm_device *dev = crtc->base.dev;
1006 struct vlv_wm_state *wm_state = &crtc->wm_state;
1007 struct intel_plane *plane;
1008 unsigned int total_rate = 0;
1009 const int fifo_size = 512 - 1;
1010 int fifo_extra, fifo_left = fifo_size;
1011
1012 for_each_intel_plane_on_crtc(dev, crtc, plane) {
1013 struct intel_plane_state *state =
1014 to_intel_plane_state(plane->base.state);
1015
1016 if (plane->base.type == DRM_PLANE_TYPE_CURSOR)
1017 continue;
1018
Ville Syrjälä936e71e2016-07-26 19:06:59 +03001019 if (state->base.visible) {
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001020 wm_state->num_active_planes++;
1021 total_rate += drm_format_plane_cpp(state->base.fb->pixel_format, 0);
1022 }
1023 }
1024
1025 for_each_intel_plane_on_crtc(dev, crtc, plane) {
1026 struct intel_plane_state *state =
1027 to_intel_plane_state(plane->base.state);
1028 unsigned int rate;
1029
1030 if (plane->base.type == DRM_PLANE_TYPE_CURSOR) {
1031 plane->wm.fifo_size = 63;
1032 continue;
1033 }
1034
Ville Syrjälä936e71e2016-07-26 19:06:59 +03001035 if (!state->base.visible) {
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001036 plane->wm.fifo_size = 0;
1037 continue;
1038 }
1039
1040 rate = drm_format_plane_cpp(state->base.fb->pixel_format, 0);
1041 plane->wm.fifo_size = fifo_size * rate / total_rate;
1042 fifo_left -= plane->wm.fifo_size;
1043 }
1044
1045 fifo_extra = DIV_ROUND_UP(fifo_left, wm_state->num_active_planes ?: 1);
1046
1047 /* spread the remainder evenly */
1048 for_each_intel_plane_on_crtc(dev, crtc, plane) {
1049 int plane_extra;
1050
1051 if (fifo_left == 0)
1052 break;
1053
1054 if (plane->base.type == DRM_PLANE_TYPE_CURSOR)
1055 continue;
1056
1057 /* give it all to the first plane if none are active */
1058 if (plane->wm.fifo_size == 0 &&
1059 wm_state->num_active_planes)
1060 continue;
1061
1062 plane_extra = min(fifo_extra, fifo_left);
1063 plane->wm.fifo_size += plane_extra;
1064 fifo_left -= plane_extra;
1065 }
1066
1067 WARN_ON(fifo_left != 0);
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++) {
1076 struct drm_device *dev = crtc->base.dev;
1077 const int sr_fifo_size = INTEL_INFO(dev)->num_pipes * 512 - 1;
1078 struct intel_plane *plane;
1079
1080 wm_state->sr[level].plane = sr_fifo_size - wm_state->sr[level].plane;
1081 wm_state->sr[level].cursor = 63 - wm_state->sr[level].cursor;
1082
1083 for_each_intel_plane_on_crtc(dev, crtc, plane) {
1084 switch (plane->base.type) {
1085 int sprite;
1086 case DRM_PLANE_TYPE_CURSOR:
1087 wm_state->wm[level].cursor = plane->wm.fifo_size -
1088 wm_state->wm[level].cursor;
1089 break;
1090 case DRM_PLANE_TYPE_PRIMARY:
1091 wm_state->wm[level].primary = plane->wm.fifo_size -
1092 wm_state->wm[level].primary;
1093 break;
1094 case DRM_PLANE_TYPE_OVERLAY:
1095 sprite = plane->plane;
1096 wm_state->wm[level].sprite[sprite] = plane->wm.fifo_size -
1097 wm_state->wm[level].sprite[sprite];
1098 break;
1099 }
1100 }
1101 }
1102}
1103
Ville Syrjälä26e1fe42015-06-24 22:00:06 +03001104static void vlv_compute_wm(struct intel_crtc *crtc)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001105{
1106 struct drm_device *dev = crtc->base.dev;
1107 struct vlv_wm_state *wm_state = &crtc->wm_state;
1108 struct intel_plane *plane;
1109 int sr_fifo_size = INTEL_INFO(dev)->num_pipes * 512 - 1;
1110 int level;
1111
1112 memset(wm_state, 0, sizeof(*wm_state));
1113
Ville Syrjälä852eb002015-06-24 22:00:07 +03001114 wm_state->cxsr = crtc->pipe != PIPE_C && crtc->wm.cxsr_allowed;
Ville Syrjälä58590c12015-09-08 21:05:12 +03001115 wm_state->num_levels = to_i915(dev)->wm.max_level + 1;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001116
1117 wm_state->num_active_planes = 0;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001118
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001119 vlv_compute_fifo(crtc);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001120
1121 if (wm_state->num_active_planes != 1)
1122 wm_state->cxsr = false;
1123
1124 if (wm_state->cxsr) {
1125 for (level = 0; level < wm_state->num_levels; level++) {
1126 wm_state->sr[level].plane = sr_fifo_size;
1127 wm_state->sr[level].cursor = 63;
1128 }
1129 }
1130
1131 for_each_intel_plane_on_crtc(dev, crtc, plane) {
1132 struct intel_plane_state *state =
1133 to_intel_plane_state(plane->base.state);
1134
Ville Syrjälä936e71e2016-07-26 19:06:59 +03001135 if (!state->base.visible)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001136 continue;
1137
1138 /* normal watermarks */
1139 for (level = 0; level < wm_state->num_levels; level++) {
1140 int wm = vlv_compute_wm_level(plane, crtc, state, level);
1141 int max_wm = plane->base.type == DRM_PLANE_TYPE_CURSOR ? 63 : 511;
1142
1143 /* hack */
1144 if (WARN_ON(level == 0 && wm > max_wm))
1145 wm = max_wm;
1146
1147 if (wm > plane->wm.fifo_size)
1148 break;
1149
1150 switch (plane->base.type) {
1151 int sprite;
1152 case DRM_PLANE_TYPE_CURSOR:
1153 wm_state->wm[level].cursor = wm;
1154 break;
1155 case DRM_PLANE_TYPE_PRIMARY:
1156 wm_state->wm[level].primary = wm;
1157 break;
1158 case DRM_PLANE_TYPE_OVERLAY:
1159 sprite = plane->plane;
1160 wm_state->wm[level].sprite[sprite] = wm;
1161 break;
1162 }
1163 }
1164
1165 wm_state->num_levels = level;
1166
1167 if (!wm_state->cxsr)
1168 continue;
1169
1170 /* maxfifo watermarks */
1171 switch (plane->base.type) {
1172 int sprite, level;
1173 case DRM_PLANE_TYPE_CURSOR:
1174 for (level = 0; level < wm_state->num_levels; level++)
1175 wm_state->sr[level].cursor =
Thomas Daniel5a37ed02015-10-23 14:55:38 +01001176 wm_state->wm[level].cursor;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001177 break;
1178 case DRM_PLANE_TYPE_PRIMARY:
1179 for (level = 0; level < wm_state->num_levels; level++)
1180 wm_state->sr[level].plane =
1181 min(wm_state->sr[level].plane,
1182 wm_state->wm[level].primary);
1183 break;
1184 case DRM_PLANE_TYPE_OVERLAY:
1185 sprite = plane->plane;
1186 for (level = 0; level < wm_state->num_levels; level++)
1187 wm_state->sr[level].plane =
1188 min(wm_state->sr[level].plane,
1189 wm_state->wm[level].sprite[sprite]);
1190 break;
1191 }
1192 }
1193
1194 /* clear any (partially) filled invalid levels */
Ville Syrjälä58590c12015-09-08 21:05:12 +03001195 for (level = wm_state->num_levels; level < to_i915(dev)->wm.max_level + 1; level++) {
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001196 memset(&wm_state->wm[level], 0, sizeof(wm_state->wm[level]));
1197 memset(&wm_state->sr[level], 0, sizeof(wm_state->sr[level]));
1198 }
1199
1200 vlv_invert_wms(crtc);
1201}
1202
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001203#define VLV_FIFO(plane, value) \
1204 (((value) << DSPARB_ ## plane ## _SHIFT_VLV) & DSPARB_ ## plane ## _MASK_VLV)
1205
1206static void vlv_pipe_set_fifo_size(struct intel_crtc *crtc)
1207{
1208 struct drm_device *dev = crtc->base.dev;
1209 struct drm_i915_private *dev_priv = to_i915(dev);
1210 struct intel_plane *plane;
1211 int sprite0_start = 0, sprite1_start = 0, fifo_size = 0;
1212
1213 for_each_intel_plane_on_crtc(dev, crtc, plane) {
1214 if (plane->base.type == DRM_PLANE_TYPE_CURSOR) {
1215 WARN_ON(plane->wm.fifo_size != 63);
1216 continue;
1217 }
1218
1219 if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
1220 sprite0_start = plane->wm.fifo_size;
1221 else if (plane->plane == 0)
1222 sprite1_start = sprite0_start + plane->wm.fifo_size;
1223 else
1224 fifo_size = sprite1_start + plane->wm.fifo_size;
1225 }
1226
1227 WARN_ON(fifo_size != 512 - 1);
1228
1229 DRM_DEBUG_KMS("Pipe %c FIFO split %d / %d / %d\n",
1230 pipe_name(crtc->pipe), sprite0_start,
1231 sprite1_start, fifo_size);
1232
1233 switch (crtc->pipe) {
1234 uint32_t dsparb, dsparb2, dsparb3;
1235 case PIPE_A:
1236 dsparb = I915_READ(DSPARB);
1237 dsparb2 = I915_READ(DSPARB2);
1238
1239 dsparb &= ~(VLV_FIFO(SPRITEA, 0xff) |
1240 VLV_FIFO(SPRITEB, 0xff));
1241 dsparb |= (VLV_FIFO(SPRITEA, sprite0_start) |
1242 VLV_FIFO(SPRITEB, sprite1_start));
1243
1244 dsparb2 &= ~(VLV_FIFO(SPRITEA_HI, 0x1) |
1245 VLV_FIFO(SPRITEB_HI, 0x1));
1246 dsparb2 |= (VLV_FIFO(SPRITEA_HI, sprite0_start >> 8) |
1247 VLV_FIFO(SPRITEB_HI, sprite1_start >> 8));
1248
1249 I915_WRITE(DSPARB, dsparb);
1250 I915_WRITE(DSPARB2, dsparb2);
1251 break;
1252 case PIPE_B:
1253 dsparb = I915_READ(DSPARB);
1254 dsparb2 = I915_READ(DSPARB2);
1255
1256 dsparb &= ~(VLV_FIFO(SPRITEC, 0xff) |
1257 VLV_FIFO(SPRITED, 0xff));
1258 dsparb |= (VLV_FIFO(SPRITEC, sprite0_start) |
1259 VLV_FIFO(SPRITED, sprite1_start));
1260
1261 dsparb2 &= ~(VLV_FIFO(SPRITEC_HI, 0xff) |
1262 VLV_FIFO(SPRITED_HI, 0xff));
1263 dsparb2 |= (VLV_FIFO(SPRITEC_HI, sprite0_start >> 8) |
1264 VLV_FIFO(SPRITED_HI, sprite1_start >> 8));
1265
1266 I915_WRITE(DSPARB, dsparb);
1267 I915_WRITE(DSPARB2, dsparb2);
1268 break;
1269 case PIPE_C:
1270 dsparb3 = I915_READ(DSPARB3);
1271 dsparb2 = I915_READ(DSPARB2);
1272
1273 dsparb3 &= ~(VLV_FIFO(SPRITEE, 0xff) |
1274 VLV_FIFO(SPRITEF, 0xff));
1275 dsparb3 |= (VLV_FIFO(SPRITEE, sprite0_start) |
1276 VLV_FIFO(SPRITEF, sprite1_start));
1277
1278 dsparb2 &= ~(VLV_FIFO(SPRITEE_HI, 0xff) |
1279 VLV_FIFO(SPRITEF_HI, 0xff));
1280 dsparb2 |= (VLV_FIFO(SPRITEE_HI, sprite0_start >> 8) |
1281 VLV_FIFO(SPRITEF_HI, sprite1_start >> 8));
1282
1283 I915_WRITE(DSPARB3, dsparb3);
1284 I915_WRITE(DSPARB2, dsparb2);
1285 break;
1286 default:
1287 break;
1288 }
1289}
1290
1291#undef VLV_FIFO
1292
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001293static void vlv_merge_wm(struct drm_device *dev,
1294 struct vlv_wm_values *wm)
1295{
1296 struct intel_crtc *crtc;
1297 int num_active_crtcs = 0;
1298
Ville Syrjälä58590c12015-09-08 21:05:12 +03001299 wm->level = to_i915(dev)->wm.max_level;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001300 wm->cxsr = true;
1301
1302 for_each_intel_crtc(dev, crtc) {
1303 const struct vlv_wm_state *wm_state = &crtc->wm_state;
1304
1305 if (!crtc->active)
1306 continue;
1307
1308 if (!wm_state->cxsr)
1309 wm->cxsr = false;
1310
1311 num_active_crtcs++;
1312 wm->level = min_t(int, wm->level, wm_state->num_levels - 1);
1313 }
1314
1315 if (num_active_crtcs != 1)
1316 wm->cxsr = false;
1317
Ville Syrjälä6f9c7842015-06-24 22:00:08 +03001318 if (num_active_crtcs > 1)
1319 wm->level = VLV_WM_LEVEL_PM2;
1320
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001321 for_each_intel_crtc(dev, crtc) {
1322 struct vlv_wm_state *wm_state = &crtc->wm_state;
1323 enum pipe pipe = crtc->pipe;
1324
1325 if (!crtc->active)
1326 continue;
1327
1328 wm->pipe[pipe] = wm_state->wm[wm->level];
1329 if (wm->cxsr)
1330 wm->sr = wm_state->sr[wm->level];
1331
1332 wm->ddl[pipe].primary = DDL_PRECISION_HIGH | 2;
1333 wm->ddl[pipe].sprite[0] = DDL_PRECISION_HIGH | 2;
1334 wm->ddl[pipe].sprite[1] = DDL_PRECISION_HIGH | 2;
1335 wm->ddl[pipe].cursor = DDL_PRECISION_HIGH | 2;
1336 }
1337}
1338
Ville Syrjälä432081b2016-10-31 22:37:03 +02001339static void vlv_update_wm(struct intel_crtc *crtc)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001340{
Ville Syrjälä432081b2016-10-31 22:37:03 +02001341 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01001342 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä432081b2016-10-31 22:37:03 +02001343 enum pipe pipe = crtc->pipe;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001344 struct vlv_wm_values wm = {};
1345
Ville Syrjälä432081b2016-10-31 22:37:03 +02001346 vlv_compute_wm(crtc);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001347 vlv_merge_wm(dev, &wm);
1348
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001349 if (memcmp(&dev_priv->wm.vlv, &wm, sizeof(wm)) == 0) {
1350 /* FIXME should be part of crtc atomic commit */
Ville Syrjälä432081b2016-10-31 22:37:03 +02001351 vlv_pipe_set_fifo_size(crtc);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001352 return;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001353 }
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001354
1355 if (wm.level < VLV_WM_LEVEL_DDR_DVFS &&
1356 dev_priv->wm.vlv.level >= VLV_WM_LEVEL_DDR_DVFS)
1357 chv_set_memory_dvfs(dev_priv, false);
1358
1359 if (wm.level < VLV_WM_LEVEL_PM5 &&
1360 dev_priv->wm.vlv.level >= VLV_WM_LEVEL_PM5)
1361 chv_set_memory_pm5(dev_priv, false);
1362
Ville Syrjälä852eb002015-06-24 22:00:07 +03001363 if (!wm.cxsr && dev_priv->wm.vlv.cxsr)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001364 intel_set_memory_cxsr(dev_priv, false);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001365
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001366 /* FIXME should be part of crtc atomic commit */
Ville Syrjälä432081b2016-10-31 22:37:03 +02001367 vlv_pipe_set_fifo_size(crtc);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001368
Ville Syrjälä432081b2016-10-31 22:37:03 +02001369 vlv_write_wm_values(crtc, &wm);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001370
1371 DRM_DEBUG_KMS("Setting FIFO watermarks - %c: plane=%d, cursor=%d, "
1372 "sprite0=%d, sprite1=%d, SR: plane=%d, cursor=%d level=%d cxsr=%d\n",
1373 pipe_name(pipe), wm.pipe[pipe].primary, wm.pipe[pipe].cursor,
1374 wm.pipe[pipe].sprite[0], wm.pipe[pipe].sprite[1],
1375 wm.sr.plane, wm.sr.cursor, wm.level, wm.cxsr);
1376
Ville Syrjälä852eb002015-06-24 22:00:07 +03001377 if (wm.cxsr && !dev_priv->wm.vlv.cxsr)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001378 intel_set_memory_cxsr(dev_priv, true);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001379
1380 if (wm.level >= VLV_WM_LEVEL_PM5 &&
1381 dev_priv->wm.vlv.level < VLV_WM_LEVEL_PM5)
1382 chv_set_memory_pm5(dev_priv, true);
1383
1384 if (wm.level >= VLV_WM_LEVEL_DDR_DVFS &&
1385 dev_priv->wm.vlv.level < VLV_WM_LEVEL_DDR_DVFS)
1386 chv_set_memory_dvfs(dev_priv, true);
1387
1388 dev_priv->wm.vlv = wm;
Ville Syrjälä3c2777f2014-06-26 17:03:06 +03001389}
1390
Ville Syrjäläae801522015-03-05 21:19:49 +02001391#define single_plane_enabled(mask) is_power_of_2(mask)
1392
Ville Syrjälä432081b2016-10-31 22:37:03 +02001393static void g4x_update_wm(struct intel_crtc *crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001394{
Ville Syrjäläb91eb5c2016-10-31 22:37:09 +02001395 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001396 static const int sr_latency_ns = 12000;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001397 int planea_wm, planeb_wm, cursora_wm, cursorb_wm;
1398 int plane_sr, cursor_sr;
1399 unsigned int enabled = 0;
Imre Deak98584252014-06-13 14:54:20 +03001400 bool cxsr_enabled;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001401
Ville Syrjäläf0ce2312016-10-31 22:37:08 +02001402 if (g4x_compute_wm0(dev_priv, PIPE_A,
Chris Wilson5aef6002014-09-03 11:56:07 +01001403 &g4x_wm_info, pessimal_latency_ns,
1404 &g4x_cursor_wm_info, pessimal_latency_ns,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001405 &planea_wm, &cursora_wm))
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001406 enabled |= 1 << PIPE_A;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001407
Ville Syrjäläf0ce2312016-10-31 22:37:08 +02001408 if (g4x_compute_wm0(dev_priv, PIPE_B,
Chris Wilson5aef6002014-09-03 11:56:07 +01001409 &g4x_wm_info, pessimal_latency_ns,
1410 &g4x_cursor_wm_info, pessimal_latency_ns,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001411 &planeb_wm, &cursorb_wm))
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001412 enabled |= 1 << PIPE_B;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001413
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001414 if (single_plane_enabled(enabled) &&
Ville Syrjäläf0ce2312016-10-31 22:37:08 +02001415 g4x_compute_srwm(dev_priv, ffs(enabled) - 1,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001416 sr_latency_ns,
1417 &g4x_wm_info,
1418 &g4x_cursor_wm_info,
Chris Wilson52bd02d2012-12-07 10:43:24 +00001419 &plane_sr, &cursor_sr)) {
Imre Deak98584252014-06-13 14:54:20 +03001420 cxsr_enabled = true;
Chris Wilson52bd02d2012-12-07 10:43:24 +00001421 } else {
Imre Deak98584252014-06-13 14:54:20 +03001422 cxsr_enabled = false;
Imre Deak5209b1f2014-07-01 12:36:17 +03001423 intel_set_memory_cxsr(dev_priv, false);
Chris Wilson52bd02d2012-12-07 10:43:24 +00001424 plane_sr = cursor_sr = 0;
1425 }
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001426
Ville Syrjäläa5043452014-06-28 02:04:18 +03001427 DRM_DEBUG_KMS("Setting FIFO watermarks - A: plane=%d, cursor=%d, "
1428 "B: plane=%d, cursor=%d, SR: plane=%d, cursor=%d\n",
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001429 planea_wm, cursora_wm,
1430 planeb_wm, cursorb_wm,
1431 plane_sr, cursor_sr);
1432
1433 I915_WRITE(DSPFW1,
Ville Syrjäläf4998962015-03-10 17:02:21 +02001434 FW_WM(plane_sr, SR) |
1435 FW_WM(cursorb_wm, CURSORB) |
1436 FW_WM(planeb_wm, PLANEB) |
1437 FW_WM(planea_wm, PLANEA));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001438 I915_WRITE(DSPFW2,
Chris Wilson8c919b22012-12-04 16:33:19 +00001439 (I915_READ(DSPFW2) & ~DSPFW_CURSORA_MASK) |
Ville Syrjäläf4998962015-03-10 17:02:21 +02001440 FW_WM(cursora_wm, CURSORA));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001441 /* HPLL off in SR has some issues on G4x... disable it */
1442 I915_WRITE(DSPFW3,
Chris Wilson8c919b22012-12-04 16:33:19 +00001443 (I915_READ(DSPFW3) & ~(DSPFW_HPLL_SR_EN | DSPFW_CURSOR_SR_MASK)) |
Ville Syrjäläf4998962015-03-10 17:02:21 +02001444 FW_WM(cursor_sr, CURSOR_SR));
Imre Deak98584252014-06-13 14:54:20 +03001445
1446 if (cxsr_enabled)
1447 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001448}
1449
Ville Syrjälä432081b2016-10-31 22:37:03 +02001450static void i965_update_wm(struct intel_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001451{
Ville Syrjälä432081b2016-10-31 22:37:03 +02001452 struct drm_device *dev = unused_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01001453 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläefc26112016-10-31 22:37:04 +02001454 struct intel_crtc *crtc;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001455 int srwm = 1;
1456 int cursor_sr = 16;
Imre Deak98584252014-06-13 14:54:20 +03001457 bool cxsr_enabled;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001458
1459 /* Calc sr entries for one plane configs */
1460 crtc = single_enabled_crtc(dev);
1461 if (crtc) {
1462 /* self-refresh has much higher latency */
1463 static const int sr_latency_ns = 12000;
Ville Syrjäläefc26112016-10-31 22:37:04 +02001464 const struct drm_display_mode *adjusted_mode =
1465 &crtc->config->base.adjusted_mode;
1466 const struct drm_framebuffer *fb =
1467 crtc->base.primary->state->fb;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001468 int clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -08001469 int htotal = adjusted_mode->crtc_htotal;
Ville Syrjäläefc26112016-10-31 22:37:04 +02001470 int hdisplay = crtc->config->pipe_src_w;
1471 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001472 unsigned long line_time_us;
1473 int entries;
1474
Ville Syrjälä922044c2014-02-14 14:18:57 +02001475 line_time_us = max(htotal * 1000 / clock, 1);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001476
1477 /* Use ns/us then divide to preserve precision */
1478 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
Ville Syrjäläac484962016-01-20 21:05:26 +02001479 cpp * hdisplay;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001480 entries = DIV_ROUND_UP(entries, I915_FIFO_LINE_SIZE);
1481 srwm = I965_FIFO_SIZE - entries;
1482 if (srwm < 0)
1483 srwm = 1;
1484 srwm &= 0x1ff;
1485 DRM_DEBUG_KMS("self-refresh entries: %d, wm: %d\n",
1486 entries, srwm);
1487
1488 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
Ville Syrjäläefc26112016-10-31 22:37:04 +02001489 cpp * crtc->base.cursor->state->crtc_w;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001490 entries = DIV_ROUND_UP(entries,
1491 i965_cursor_wm_info.cacheline_size);
1492 cursor_sr = i965_cursor_wm_info.fifo_size -
1493 (entries + i965_cursor_wm_info.guard_size);
1494
1495 if (cursor_sr > i965_cursor_wm_info.max_wm)
1496 cursor_sr = i965_cursor_wm_info.max_wm;
1497
1498 DRM_DEBUG_KMS("self-refresh watermark: display plane %d "
1499 "cursor %d\n", srwm, cursor_sr);
1500
Imre Deak98584252014-06-13 14:54:20 +03001501 cxsr_enabled = true;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001502 } else {
Imre Deak98584252014-06-13 14:54:20 +03001503 cxsr_enabled = false;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001504 /* Turn off self refresh if both pipes are enabled */
Imre Deak5209b1f2014-07-01 12:36:17 +03001505 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001506 }
1507
1508 DRM_DEBUG_KMS("Setting FIFO watermarks - A: 8, B: 8, C: 8, SR %d\n",
1509 srwm);
1510
1511 /* 965 has limitations... */
Ville Syrjäläf4998962015-03-10 17:02:21 +02001512 I915_WRITE(DSPFW1, FW_WM(srwm, SR) |
1513 FW_WM(8, CURSORB) |
1514 FW_WM(8, PLANEB) |
1515 FW_WM(8, PLANEA));
1516 I915_WRITE(DSPFW2, FW_WM(8, CURSORA) |
1517 FW_WM(8, PLANEC_OLD));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001518 /* update cursor SR watermark */
Ville Syrjäläf4998962015-03-10 17:02:21 +02001519 I915_WRITE(DSPFW3, FW_WM(cursor_sr, CURSOR_SR));
Imre Deak98584252014-06-13 14:54:20 +03001520
1521 if (cxsr_enabled)
1522 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001523}
1524
Ville Syrjäläf4998962015-03-10 17:02:21 +02001525#undef FW_WM
1526
Ville Syrjälä432081b2016-10-31 22:37:03 +02001527static void i9xx_update_wm(struct intel_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001528{
Ville Syrjälä432081b2016-10-31 22:37:03 +02001529 struct drm_device *dev = unused_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01001530 struct drm_i915_private *dev_priv = to_i915(dev);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001531 const struct intel_watermark_params *wm_info;
1532 uint32_t fwater_lo;
1533 uint32_t fwater_hi;
1534 int cwm, srwm = 1;
1535 int fifo_size;
1536 int planea_wm, planeb_wm;
Ville Syrjäläefc26112016-10-31 22:37:04 +02001537 struct intel_crtc *crtc, *enabled = NULL;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001538
1539 if (IS_I945GM(dev))
1540 wm_info = &i945_wm_info;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01001541 else if (!IS_GEN2(dev_priv))
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001542 wm_info = &i915_wm_info;
1543 else
Ville Syrjälä9d539102014-08-15 01:21:53 +03001544 wm_info = &i830_a_wm_info;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001545
1546 fifo_size = dev_priv->display.get_fifo_size(dev, 0);
Ville Syrjäläb91eb5c2016-10-31 22:37:09 +02001547 crtc = intel_get_crtc_for_plane(dev_priv, 0);
Ville Syrjäläefc26112016-10-31 22:37:04 +02001548 if (intel_crtc_active(crtc)) {
1549 const struct drm_display_mode *adjusted_mode =
1550 &crtc->config->base.adjusted_mode;
1551 const struct drm_framebuffer *fb =
1552 crtc->base.primary->state->fb;
1553 int cpp;
1554
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01001555 if (IS_GEN2(dev_priv))
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001556 cpp = 4;
Ville Syrjäläefc26112016-10-31 22:37:04 +02001557 else
1558 cpp = drm_format_plane_cpp(fb->pixel_format, 0);
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001559
Damien Lespiau241bfc32013-09-25 16:45:37 +01001560 planea_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001561 wm_info, fifo_size, cpp,
Chris Wilson5aef6002014-09-03 11:56:07 +01001562 pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001563 enabled = crtc;
Ville Syrjälä9d539102014-08-15 01:21:53 +03001564 } else {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001565 planea_wm = fifo_size - wm_info->guard_size;
Ville Syrjälä9d539102014-08-15 01:21:53 +03001566 if (planea_wm > (long)wm_info->max_wm)
1567 planea_wm = wm_info->max_wm;
1568 }
1569
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01001570 if (IS_GEN2(dev_priv))
Ville Syrjälä9d539102014-08-15 01:21:53 +03001571 wm_info = &i830_bc_wm_info;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001572
1573 fifo_size = dev_priv->display.get_fifo_size(dev, 1);
Ville Syrjäläb91eb5c2016-10-31 22:37:09 +02001574 crtc = intel_get_crtc_for_plane(dev_priv, 1);
Ville Syrjäläefc26112016-10-31 22:37:04 +02001575 if (intel_crtc_active(crtc)) {
1576 const struct drm_display_mode *adjusted_mode =
1577 &crtc->config->base.adjusted_mode;
1578 const struct drm_framebuffer *fb =
1579 crtc->base.primary->state->fb;
1580 int cpp;
1581
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01001582 if (IS_GEN2(dev_priv))
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001583 cpp = 4;
Ville Syrjäläefc26112016-10-31 22:37:04 +02001584 else
1585 cpp = drm_format_plane_cpp(fb->pixel_format, 0);
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001586
Damien Lespiau241bfc32013-09-25 16:45:37 +01001587 planeb_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001588 wm_info, fifo_size, cpp,
Chris Wilson5aef6002014-09-03 11:56:07 +01001589 pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001590 if (enabled == NULL)
1591 enabled = crtc;
1592 else
1593 enabled = NULL;
Ville Syrjälä9d539102014-08-15 01:21:53 +03001594 } else {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001595 planeb_wm = fifo_size - wm_info->guard_size;
Ville Syrjälä9d539102014-08-15 01:21:53 +03001596 if (planeb_wm > (long)wm_info->max_wm)
1597 planeb_wm = wm_info->max_wm;
1598 }
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001599
1600 DRM_DEBUG_KMS("FIFO watermarks - A: %d, B: %d\n", planea_wm, planeb_wm);
1601
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01001602 if (IS_I915GM(dev_priv) && enabled) {
Matt Roper2ff8fde2014-07-08 07:50:07 -07001603 struct drm_i915_gem_object *obj;
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02001604
Ville Syrjäläefc26112016-10-31 22:37:04 +02001605 obj = intel_fb_obj(enabled->base.primary->state->fb);
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02001606
1607 /* self-refresh seems busted with untiled */
Chris Wilson3e510a82016-08-05 10:14:23 +01001608 if (!i915_gem_object_is_tiled(obj))
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02001609 enabled = NULL;
1610 }
1611
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001612 /*
1613 * Overlay gets an aggressive default since video jitter is bad.
1614 */
1615 cwm = 2;
1616
1617 /* Play safe and disable self-refresh before adjusting watermarks. */
Imre Deak5209b1f2014-07-01 12:36:17 +03001618 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001619
1620 /* Calc sr entries for one plane configs */
1621 if (HAS_FW_BLC(dev) && enabled) {
1622 /* self-refresh has much higher latency */
1623 static const int sr_latency_ns = 6000;
Ville Syrjäläefc26112016-10-31 22:37:04 +02001624 const struct drm_display_mode *adjusted_mode =
1625 &enabled->config->base.adjusted_mode;
1626 const struct drm_framebuffer *fb =
1627 enabled->base.primary->state->fb;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001628 int clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -08001629 int htotal = adjusted_mode->crtc_htotal;
Ville Syrjäläefc26112016-10-31 22:37:04 +02001630 int hdisplay = enabled->config->pipe_src_w;
1631 int cpp;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001632 unsigned long line_time_us;
1633 int entries;
1634
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01001635 if (IS_I915GM(dev_priv) || IS_I945GM(dev_priv))
Ville Syrjälä2d1b5052016-07-29 17:57:01 +03001636 cpp = 4;
Ville Syrjäläefc26112016-10-31 22:37:04 +02001637 else
1638 cpp = drm_format_plane_cpp(fb->pixel_format, 0);
Ville Syrjälä2d1b5052016-07-29 17:57:01 +03001639
Ville Syrjälä922044c2014-02-14 14:18:57 +02001640 line_time_us = max(htotal * 1000 / clock, 1);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001641
1642 /* Use ns/us then divide to preserve precision */
1643 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
Ville Syrjäläac484962016-01-20 21:05:26 +02001644 cpp * hdisplay;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001645 entries = DIV_ROUND_UP(entries, wm_info->cacheline_size);
1646 DRM_DEBUG_KMS("self-refresh entries: %d\n", entries);
1647 srwm = wm_info->fifo_size - entries;
1648 if (srwm < 0)
1649 srwm = 1;
1650
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01001651 if (IS_I945G(dev_priv) || IS_I945GM(dev_priv))
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001652 I915_WRITE(FW_BLC_SELF,
1653 FW_BLC_SELF_FIFO_MASK | (srwm & 0xff));
Ville Syrjäläacb91352016-07-29 17:57:02 +03001654 else
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001655 I915_WRITE(FW_BLC_SELF, srwm & 0x3f);
1656 }
1657
1658 DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d, B: %d, C: %d, SR %d\n",
1659 planea_wm, planeb_wm, cwm, srwm);
1660
1661 fwater_lo = ((planeb_wm & 0x3f) << 16) | (planea_wm & 0x3f);
1662 fwater_hi = (cwm & 0x1f);
1663
1664 /* Set request length to 8 cachelines per fetch */
1665 fwater_lo = fwater_lo | (1 << 24) | (1 << 8);
1666 fwater_hi = fwater_hi | (1 << 8);
1667
1668 I915_WRITE(FW_BLC, fwater_lo);
1669 I915_WRITE(FW_BLC2, fwater_hi);
1670
Imre Deak5209b1f2014-07-01 12:36:17 +03001671 if (enabled)
1672 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001673}
1674
Ville Syrjälä432081b2016-10-31 22:37:03 +02001675static void i845_update_wm(struct intel_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001676{
Ville Syrjälä432081b2016-10-31 22:37:03 +02001677 struct drm_device *dev = unused_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01001678 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläefc26112016-10-31 22:37:04 +02001679 struct intel_crtc *crtc;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001680 const struct drm_display_mode *adjusted_mode;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001681 uint32_t fwater_lo;
1682 int planea_wm;
1683
1684 crtc = single_enabled_crtc(dev);
1685 if (crtc == NULL)
1686 return;
1687
Ville Syrjäläefc26112016-10-31 22:37:04 +02001688 adjusted_mode = &crtc->config->base.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001689 planea_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Daniel Vetterfeb56b92013-12-14 20:38:30 -02001690 &i845_wm_info,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001691 dev_priv->display.get_fifo_size(dev, 0),
Chris Wilson5aef6002014-09-03 11:56:07 +01001692 4, pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001693 fwater_lo = I915_READ(FW_BLC) & ~0xfff;
1694 fwater_lo |= (3<<8) | planea_wm;
1695
1696 DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d\n", planea_wm);
1697
1698 I915_WRITE(FW_BLC, fwater_lo);
1699}
1700
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03001701uint32_t ilk_pipe_pixel_rate(const struct intel_crtc_state *pipe_config)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001702{
Chris Wilsonfd4daa92013-08-27 17:04:17 +01001703 uint32_t pixel_rate;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001704
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03001705 pixel_rate = pipe_config->base.adjusted_mode.crtc_clock;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001706
1707 /* We only use IF-ID interlacing. If we ever use PF-ID we'll need to
1708 * adjust the pixel_rate here. */
1709
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03001710 if (pipe_config->pch_pfit.enabled) {
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001711 uint64_t pipe_w, pipe_h, pfit_w, pfit_h;
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03001712 uint32_t pfit_size = pipe_config->pch_pfit.size;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001713
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03001714 pipe_w = pipe_config->pipe_src_w;
1715 pipe_h = pipe_config->pipe_src_h;
1716
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001717 pfit_w = (pfit_size >> 16) & 0xFFFF;
1718 pfit_h = pfit_size & 0xFFFF;
1719 if (pipe_w < pfit_w)
1720 pipe_w = pfit_w;
1721 if (pipe_h < pfit_h)
1722 pipe_h = pfit_h;
1723
Matt Roper15126882015-12-03 11:37:40 -08001724 if (WARN_ON(!pfit_w || !pfit_h))
1725 return pixel_rate;
1726
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001727 pixel_rate = div_u64((uint64_t) pixel_rate * pipe_w * pipe_h,
1728 pfit_w * pfit_h);
1729 }
1730
1731 return pixel_rate;
1732}
1733
Ville Syrjälä37126462013-08-01 16:18:55 +03001734/* latency must be in 0.1us units. */
Ville Syrjäläac484962016-01-20 21:05:26 +02001735static uint32_t ilk_wm_method1(uint32_t pixel_rate, uint8_t cpp, uint32_t latency)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001736{
1737 uint64_t ret;
1738
Ville Syrjälä3312ba62013-08-01 16:18:53 +03001739 if (WARN(latency == 0, "Latency value missing\n"))
1740 return UINT_MAX;
1741
Ville Syrjäläac484962016-01-20 21:05:26 +02001742 ret = (uint64_t) pixel_rate * cpp * latency;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001743 ret = DIV_ROUND_UP_ULL(ret, 64 * 10000) + 2;
1744
1745 return ret;
1746}
1747
Ville Syrjälä37126462013-08-01 16:18:55 +03001748/* latency must be in 0.1us units. */
Ville Syrjälä23297042013-07-05 11:57:17 +03001749static uint32_t ilk_wm_method2(uint32_t pixel_rate, uint32_t pipe_htotal,
Ville Syrjäläac484962016-01-20 21:05:26 +02001750 uint32_t horiz_pixels, uint8_t cpp,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001751 uint32_t latency)
1752{
1753 uint32_t ret;
1754
Ville Syrjälä3312ba62013-08-01 16:18:53 +03001755 if (WARN(latency == 0, "Latency value missing\n"))
1756 return UINT_MAX;
Matt Roper15126882015-12-03 11:37:40 -08001757 if (WARN_ON(!pipe_htotal))
1758 return UINT_MAX;
Ville Syrjälä3312ba62013-08-01 16:18:53 +03001759
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001760 ret = (latency * pixel_rate) / (pipe_htotal * 10000);
Ville Syrjäläac484962016-01-20 21:05:26 +02001761 ret = (ret + 1) * horiz_pixels * cpp;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001762 ret = DIV_ROUND_UP(ret, 64) + 2;
1763 return ret;
1764}
1765
Ville Syrjälä23297042013-07-05 11:57:17 +03001766static uint32_t ilk_wm_fbc(uint32_t pri_val, uint32_t horiz_pixels,
Ville Syrjäläac484962016-01-20 21:05:26 +02001767 uint8_t cpp)
Paulo Zanonicca32e92013-05-31 11:45:06 -03001768{
Matt Roper15126882015-12-03 11:37:40 -08001769 /*
1770 * Neither of these should be possible since this function shouldn't be
1771 * called if the CRTC is off or the plane is invisible. But let's be
1772 * extra paranoid to avoid a potential divide-by-zero if we screw up
1773 * elsewhere in the driver.
1774 */
Ville Syrjäläac484962016-01-20 21:05:26 +02001775 if (WARN_ON(!cpp))
Matt Roper15126882015-12-03 11:37:40 -08001776 return 0;
1777 if (WARN_ON(!horiz_pixels))
1778 return 0;
1779
Ville Syrjäläac484962016-01-20 21:05:26 +02001780 return DIV_ROUND_UP(pri_val * 64, horiz_pixels * cpp) + 2;
Paulo Zanonicca32e92013-05-31 11:45:06 -03001781}
1782
Imre Deak820c1982013-12-17 14:46:36 +02001783struct ilk_wm_maximums {
Paulo Zanonicca32e92013-05-31 11:45:06 -03001784 uint16_t pri;
1785 uint16_t spr;
1786 uint16_t cur;
1787 uint16_t fbc;
1788};
1789
Ville Syrjälä37126462013-08-01 16:18:55 +03001790/*
1791 * For both WM_PIPE and WM_LP.
1792 * mem_value must be in 0.1us units.
1793 */
Matt Roper7221fc32015-09-24 15:53:08 -07001794static uint32_t ilk_compute_pri_wm(const struct intel_crtc_state *cstate,
Matt Roper43d59ed2015-09-24 15:53:07 -07001795 const struct intel_plane_state *pstate,
Paulo Zanonicca32e92013-05-31 11:45:06 -03001796 uint32_t mem_value,
1797 bool is_lp)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001798{
Ville Syrjäläac484962016-01-20 21:05:26 +02001799 int cpp = pstate->base.fb ?
1800 drm_format_plane_cpp(pstate->base.fb->pixel_format, 0) : 0;
Paulo Zanonicca32e92013-05-31 11:45:06 -03001801 uint32_t method1, method2;
1802
Ville Syrjälä936e71e2016-07-26 19:06:59 +03001803 if (!cstate->base.active || !pstate->base.visible)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001804 return 0;
1805
Ville Syrjäläac484962016-01-20 21:05:26 +02001806 method1 = ilk_wm_method1(ilk_pipe_pixel_rate(cstate), cpp, mem_value);
Paulo Zanonicca32e92013-05-31 11:45:06 -03001807
1808 if (!is_lp)
1809 return method1;
1810
Matt Roper7221fc32015-09-24 15:53:08 -07001811 method2 = ilk_wm_method2(ilk_pipe_pixel_rate(cstate),
1812 cstate->base.adjusted_mode.crtc_htotal,
Ville Syrjälä936e71e2016-07-26 19:06:59 +03001813 drm_rect_width(&pstate->base.dst),
Ville Syrjäläac484962016-01-20 21:05:26 +02001814 cpp, mem_value);
Paulo Zanonicca32e92013-05-31 11:45:06 -03001815
1816 return min(method1, method2);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001817}
1818
Ville Syrjälä37126462013-08-01 16:18:55 +03001819/*
1820 * For both WM_PIPE and WM_LP.
1821 * mem_value must be in 0.1us units.
1822 */
Matt Roper7221fc32015-09-24 15:53:08 -07001823static uint32_t ilk_compute_spr_wm(const struct intel_crtc_state *cstate,
Matt Roper43d59ed2015-09-24 15:53:07 -07001824 const struct intel_plane_state *pstate,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001825 uint32_t mem_value)
1826{
Ville Syrjäläac484962016-01-20 21:05:26 +02001827 int cpp = pstate->base.fb ?
1828 drm_format_plane_cpp(pstate->base.fb->pixel_format, 0) : 0;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001829 uint32_t method1, method2;
1830
Ville Syrjälä936e71e2016-07-26 19:06:59 +03001831 if (!cstate->base.active || !pstate->base.visible)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001832 return 0;
1833
Ville Syrjäläac484962016-01-20 21:05:26 +02001834 method1 = ilk_wm_method1(ilk_pipe_pixel_rate(cstate), cpp, mem_value);
Matt Roper7221fc32015-09-24 15:53:08 -07001835 method2 = ilk_wm_method2(ilk_pipe_pixel_rate(cstate),
1836 cstate->base.adjusted_mode.crtc_htotal,
Ville Syrjälä936e71e2016-07-26 19:06:59 +03001837 drm_rect_width(&pstate->base.dst),
Ville Syrjäläac484962016-01-20 21:05:26 +02001838 cpp, mem_value);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001839 return min(method1, method2);
1840}
1841
Ville Syrjälä37126462013-08-01 16:18:55 +03001842/*
1843 * For both WM_PIPE and WM_LP.
1844 * mem_value must be in 0.1us units.
1845 */
Matt Roper7221fc32015-09-24 15:53:08 -07001846static uint32_t ilk_compute_cur_wm(const struct intel_crtc_state *cstate,
Matt Roper43d59ed2015-09-24 15:53:07 -07001847 const struct intel_plane_state *pstate,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001848 uint32_t mem_value)
1849{
Matt Roperb2435692016-02-02 22:06:51 -08001850 /*
1851 * We treat the cursor plane as always-on for the purposes of watermark
1852 * calculation. Until we have two-stage watermark programming merged,
1853 * this is necessary to avoid flickering.
1854 */
1855 int cpp = 4;
Ville Syrjälä936e71e2016-07-26 19:06:59 +03001856 int width = pstate->base.visible ? pstate->base.crtc_w : 64;
Matt Roper43d59ed2015-09-24 15:53:07 -07001857
Matt Roperb2435692016-02-02 22:06:51 -08001858 if (!cstate->base.active)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001859 return 0;
1860
Matt Roper7221fc32015-09-24 15:53:08 -07001861 return ilk_wm_method2(ilk_pipe_pixel_rate(cstate),
1862 cstate->base.adjusted_mode.crtc_htotal,
Matt Roperb2435692016-02-02 22:06:51 -08001863 width, cpp, mem_value);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001864}
1865
Paulo Zanonicca32e92013-05-31 11:45:06 -03001866/* Only for WM_LP. */
Matt Roper7221fc32015-09-24 15:53:08 -07001867static uint32_t ilk_compute_fbc_wm(const struct intel_crtc_state *cstate,
Matt Roper43d59ed2015-09-24 15:53:07 -07001868 const struct intel_plane_state *pstate,
Ville Syrjälä1fda9882013-07-05 11:57:19 +03001869 uint32_t pri_val)
Paulo Zanonicca32e92013-05-31 11:45:06 -03001870{
Ville Syrjäläac484962016-01-20 21:05:26 +02001871 int cpp = pstate->base.fb ?
1872 drm_format_plane_cpp(pstate->base.fb->pixel_format, 0) : 0;
Matt Roper43d59ed2015-09-24 15:53:07 -07001873
Ville Syrjälä936e71e2016-07-26 19:06:59 +03001874 if (!cstate->base.active || !pstate->base.visible)
Paulo Zanonicca32e92013-05-31 11:45:06 -03001875 return 0;
1876
Ville Syrjälä936e71e2016-07-26 19:06:59 +03001877 return ilk_wm_fbc(pri_val, drm_rect_width(&pstate->base.dst), cpp);
Paulo Zanonicca32e92013-05-31 11:45:06 -03001878}
1879
Ville Syrjälä158ae642013-08-07 13:28:19 +03001880static unsigned int ilk_display_fifo_size(const struct drm_device *dev)
1881{
Ville Syrjälä416f4722013-11-02 21:07:46 -07001882 if (INTEL_INFO(dev)->gen >= 8)
1883 return 3072;
1884 else if (INTEL_INFO(dev)->gen >= 7)
Ville Syrjälä158ae642013-08-07 13:28:19 +03001885 return 768;
1886 else
1887 return 512;
1888}
1889
Ville Syrjälä4e975082014-03-07 18:32:11 +02001890static unsigned int ilk_plane_wm_reg_max(const struct drm_device *dev,
1891 int level, bool is_sprite)
1892{
1893 if (INTEL_INFO(dev)->gen >= 8)
1894 /* BDW primary/sprite plane watermarks */
1895 return level == 0 ? 255 : 2047;
1896 else if (INTEL_INFO(dev)->gen >= 7)
1897 /* IVB/HSW primary/sprite plane watermarks */
1898 return level == 0 ? 127 : 1023;
1899 else if (!is_sprite)
1900 /* ILK/SNB primary plane watermarks */
1901 return level == 0 ? 127 : 511;
1902 else
1903 /* ILK/SNB sprite plane watermarks */
1904 return level == 0 ? 63 : 255;
1905}
1906
1907static unsigned int ilk_cursor_wm_reg_max(const struct drm_device *dev,
1908 int level)
1909{
1910 if (INTEL_INFO(dev)->gen >= 7)
1911 return level == 0 ? 63 : 255;
1912 else
1913 return level == 0 ? 31 : 63;
1914}
1915
1916static unsigned int ilk_fbc_wm_reg_max(const struct drm_device *dev)
1917{
1918 if (INTEL_INFO(dev)->gen >= 8)
1919 return 31;
1920 else
1921 return 15;
1922}
1923
Ville Syrjälä158ae642013-08-07 13:28:19 +03001924/* Calculate the maximum primary/sprite plane watermark */
1925static unsigned int ilk_plane_wm_max(const struct drm_device *dev,
1926 int level,
Ville Syrjälä240264f2013-08-07 13:29:12 +03001927 const struct intel_wm_config *config,
Ville Syrjälä158ae642013-08-07 13:28:19 +03001928 enum intel_ddb_partitioning ddb_partitioning,
1929 bool is_sprite)
1930{
1931 unsigned int fifo_size = ilk_display_fifo_size(dev);
Ville Syrjälä158ae642013-08-07 13:28:19 +03001932
1933 /* if sprites aren't enabled, sprites get nothing */
Ville Syrjälä240264f2013-08-07 13:29:12 +03001934 if (is_sprite && !config->sprites_enabled)
Ville Syrjälä158ae642013-08-07 13:28:19 +03001935 return 0;
1936
1937 /* HSW allows LP1+ watermarks even with 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 fifo_size /= INTEL_INFO(dev)->num_pipes;
1940
1941 /*
1942 * For some reason the non self refresh
1943 * FIFO size is only half of the self
1944 * refresh FIFO size on ILK/SNB.
1945 */
1946 if (INTEL_INFO(dev)->gen <= 6)
1947 fifo_size /= 2;
1948 }
1949
Ville Syrjälä240264f2013-08-07 13:29:12 +03001950 if (config->sprites_enabled) {
Ville Syrjälä158ae642013-08-07 13:28:19 +03001951 /* level 0 is always calculated with 1:1 split */
1952 if (level > 0 && ddb_partitioning == INTEL_DDB_PART_5_6) {
1953 if (is_sprite)
1954 fifo_size *= 5;
1955 fifo_size /= 6;
1956 } else {
1957 fifo_size /= 2;
1958 }
1959 }
1960
1961 /* clamp to max that the registers can hold */
Ville Syrjälä4e975082014-03-07 18:32:11 +02001962 return min(fifo_size, ilk_plane_wm_reg_max(dev, level, is_sprite));
Ville Syrjälä158ae642013-08-07 13:28:19 +03001963}
1964
1965/* Calculate the maximum cursor plane watermark */
1966static unsigned int ilk_cursor_wm_max(const struct drm_device *dev,
Ville Syrjälä240264f2013-08-07 13:29:12 +03001967 int level,
1968 const struct intel_wm_config *config)
Ville Syrjälä158ae642013-08-07 13:28:19 +03001969{
1970 /* HSW LP1+ watermarks w/ multiple pipes */
Ville Syrjälä240264f2013-08-07 13:29:12 +03001971 if (level > 0 && config->num_pipes_active > 1)
Ville Syrjälä158ae642013-08-07 13:28:19 +03001972 return 64;
1973
1974 /* otherwise just report max that registers can hold */
Ville Syrjälä4e975082014-03-07 18:32:11 +02001975 return ilk_cursor_wm_reg_max(dev, level);
Ville Syrjälä158ae642013-08-07 13:28:19 +03001976}
1977
Damien Lespiaud34ff9c2014-01-06 19:17:23 +00001978static void ilk_compute_wm_maximums(const struct drm_device *dev,
Ville Syrjälä34982fe2013-10-09 19:18:09 +03001979 int level,
1980 const struct intel_wm_config *config,
1981 enum intel_ddb_partitioning ddb_partitioning,
Imre Deak820c1982013-12-17 14:46:36 +02001982 struct ilk_wm_maximums *max)
Ville Syrjälä158ae642013-08-07 13:28:19 +03001983{
Ville Syrjälä240264f2013-08-07 13:29:12 +03001984 max->pri = ilk_plane_wm_max(dev, level, config, ddb_partitioning, false);
1985 max->spr = ilk_plane_wm_max(dev, level, config, ddb_partitioning, true);
1986 max->cur = ilk_cursor_wm_max(dev, level, config);
Ville Syrjälä4e975082014-03-07 18:32:11 +02001987 max->fbc = ilk_fbc_wm_reg_max(dev);
Ville Syrjälä158ae642013-08-07 13:28:19 +03001988}
1989
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03001990static void ilk_compute_wm_reg_maximums(struct drm_device *dev,
1991 int level,
1992 struct ilk_wm_maximums *max)
1993{
1994 max->pri = ilk_plane_wm_reg_max(dev, level, false);
1995 max->spr = ilk_plane_wm_reg_max(dev, level, true);
1996 max->cur = ilk_cursor_wm_reg_max(dev, level);
1997 max->fbc = ilk_fbc_wm_reg_max(dev);
1998}
1999
Ville Syrjäläd9395652013-10-09 19:18:10 +03002000static bool ilk_validate_wm_level(int level,
Imre Deak820c1982013-12-17 14:46:36 +02002001 const struct ilk_wm_maximums *max,
Ville Syrjäläd9395652013-10-09 19:18:10 +03002002 struct intel_wm_level *result)
Ville Syrjäläa9786a12013-08-07 13:24:47 +03002003{
2004 bool ret;
2005
2006 /* already determined to be invalid? */
2007 if (!result->enable)
2008 return false;
2009
2010 result->enable = result->pri_val <= max->pri &&
2011 result->spr_val <= max->spr &&
2012 result->cur_val <= max->cur;
2013
2014 ret = result->enable;
2015
2016 /*
2017 * HACK until we can pre-compute everything,
2018 * and thus fail gracefully if LP0 watermarks
2019 * are exceeded...
2020 */
2021 if (level == 0 && !result->enable) {
2022 if (result->pri_val > max->pri)
2023 DRM_DEBUG_KMS("Primary WM%d too large %u (max %u)\n",
2024 level, result->pri_val, max->pri);
2025 if (result->spr_val > max->spr)
2026 DRM_DEBUG_KMS("Sprite WM%d too large %u (max %u)\n",
2027 level, result->spr_val, max->spr);
2028 if (result->cur_val > max->cur)
2029 DRM_DEBUG_KMS("Cursor WM%d too large %u (max %u)\n",
2030 level, result->cur_val, max->cur);
2031
2032 result->pri_val = min_t(uint32_t, result->pri_val, max->pri);
2033 result->spr_val = min_t(uint32_t, result->spr_val, max->spr);
2034 result->cur_val = min_t(uint32_t, result->cur_val, max->cur);
2035 result->enable = true;
2036 }
2037
Ville Syrjäläa9786a12013-08-07 13:24:47 +03002038 return ret;
2039}
2040
Damien Lespiaud34ff9c2014-01-06 19:17:23 +00002041static void ilk_compute_wm_level(const struct drm_i915_private *dev_priv,
Matt Roper43d59ed2015-09-24 15:53:07 -07002042 const struct intel_crtc *intel_crtc,
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03002043 int level,
Matt Roper7221fc32015-09-24 15:53:08 -07002044 struct intel_crtc_state *cstate,
Matt Roper86c8bbb2015-09-24 15:53:16 -07002045 struct intel_plane_state *pristate,
2046 struct intel_plane_state *sprstate,
2047 struct intel_plane_state *curstate,
Ville Syrjälä1fd527c2013-08-06 22:24:05 +03002048 struct intel_wm_level *result)
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03002049{
2050 uint16_t pri_latency = dev_priv->wm.pri_latency[level];
2051 uint16_t spr_latency = dev_priv->wm.spr_latency[level];
2052 uint16_t cur_latency = dev_priv->wm.cur_latency[level];
2053
2054 /* WM1+ latency values stored in 0.5us units */
2055 if (level > 0) {
2056 pri_latency *= 5;
2057 spr_latency *= 5;
2058 cur_latency *= 5;
2059 }
2060
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002061 if (pristate) {
2062 result->pri_val = ilk_compute_pri_wm(cstate, pristate,
2063 pri_latency, level);
2064 result->fbc_val = ilk_compute_fbc_wm(cstate, pristate, result->pri_val);
2065 }
2066
2067 if (sprstate)
2068 result->spr_val = ilk_compute_spr_wm(cstate, sprstate, spr_latency);
2069
2070 if (curstate)
2071 result->cur_val = ilk_compute_cur_wm(cstate, curstate, cur_latency);
2072
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03002073 result->enable = true;
2074}
2075
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002076static uint32_t
Ville Syrjälä532f7a72016-04-29 17:31:17 +03002077hsw_compute_linetime_wm(const struct intel_crtc_state *cstate)
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002078{
Ville Syrjälä532f7a72016-04-29 17:31:17 +03002079 const struct intel_atomic_state *intel_state =
2080 to_intel_atomic_state(cstate->base.state);
Matt Roperee91a152015-12-03 11:37:39 -08002081 const struct drm_display_mode *adjusted_mode =
2082 &cstate->base.adjusted_mode;
Paulo Zanoni85a02de2013-05-03 17:23:43 -03002083 u32 linetime, ips_linetime;
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002084
Matt Roperee91a152015-12-03 11:37:39 -08002085 if (!cstate->base.active)
2086 return 0;
2087 if (WARN_ON(adjusted_mode->crtc_clock == 0))
2088 return 0;
Ville Syrjälä532f7a72016-04-29 17:31:17 +03002089 if (WARN_ON(intel_state->cdclk == 0))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002090 return 0;
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002091
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002092 /* The WM are computed with base on how long it takes to fill a single
2093 * row at the given clock rate, multiplied by 8.
2094 * */
Ville Syrjälä124abe02015-09-08 13:40:45 +03002095 linetime = DIV_ROUND_CLOSEST(adjusted_mode->crtc_htotal * 1000 * 8,
2096 adjusted_mode->crtc_clock);
2097 ips_linetime = DIV_ROUND_CLOSEST(adjusted_mode->crtc_htotal * 1000 * 8,
Ville Syrjälä532f7a72016-04-29 17:31:17 +03002098 intel_state->cdclk);
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002099
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002100 return PIPE_WM_LINETIME_IPS_LINETIME(ips_linetime) |
2101 PIPE_WM_LINETIME_TIME(linetime);
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002102}
2103
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002104static void intel_read_wm_latency(struct drm_device *dev, uint16_t wm[8])
Ville Syrjälä12b134d2013-07-05 11:57:21 +03002105{
Chris Wilsonfac5e232016-07-04 11:34:36 +01002106 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä12b134d2013-07-05 11:57:21 +03002107
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002108 if (IS_GEN9(dev_priv)) {
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002109 uint32_t val;
Vandana Kannan4f947382014-11-04 17:06:47 +00002110 int ret, i;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002111 int level, max_level = ilk_wm_max_level(dev_priv);
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002112
2113 /* read the first set of memory latencies[0:3] */
2114 val = 0; /* data0 to be programmed to 0 for first set */
2115 mutex_lock(&dev_priv->rps.hw_lock);
2116 ret = sandybridge_pcode_read(dev_priv,
2117 GEN9_PCODE_READ_MEM_LATENCY,
2118 &val);
2119 mutex_unlock(&dev_priv->rps.hw_lock);
2120
2121 if (ret) {
2122 DRM_ERROR("SKL Mailbox read error = %d\n", ret);
2123 return;
2124 }
2125
2126 wm[0] = val & GEN9_MEM_LATENCY_LEVEL_MASK;
2127 wm[1] = (val >> GEN9_MEM_LATENCY_LEVEL_1_5_SHIFT) &
2128 GEN9_MEM_LATENCY_LEVEL_MASK;
2129 wm[2] = (val >> GEN9_MEM_LATENCY_LEVEL_2_6_SHIFT) &
2130 GEN9_MEM_LATENCY_LEVEL_MASK;
2131 wm[3] = (val >> GEN9_MEM_LATENCY_LEVEL_3_7_SHIFT) &
2132 GEN9_MEM_LATENCY_LEVEL_MASK;
2133
2134 /* read the second set of memory latencies[4:7] */
2135 val = 1; /* data0 to be programmed to 1 for second set */
2136 mutex_lock(&dev_priv->rps.hw_lock);
2137 ret = sandybridge_pcode_read(dev_priv,
2138 GEN9_PCODE_READ_MEM_LATENCY,
2139 &val);
2140 mutex_unlock(&dev_priv->rps.hw_lock);
2141 if (ret) {
2142 DRM_ERROR("SKL Mailbox read error = %d\n", ret);
2143 return;
2144 }
2145
2146 wm[4] = val & GEN9_MEM_LATENCY_LEVEL_MASK;
2147 wm[5] = (val >> GEN9_MEM_LATENCY_LEVEL_1_5_SHIFT) &
2148 GEN9_MEM_LATENCY_LEVEL_MASK;
2149 wm[6] = (val >> GEN9_MEM_LATENCY_LEVEL_2_6_SHIFT) &
2150 GEN9_MEM_LATENCY_LEVEL_MASK;
2151 wm[7] = (val >> GEN9_MEM_LATENCY_LEVEL_3_7_SHIFT) &
2152 GEN9_MEM_LATENCY_LEVEL_MASK;
2153
Vandana Kannan367294b2014-11-04 17:06:46 +00002154 /*
Paulo Zanoni0727e402016-09-22 18:00:30 -03002155 * If a level n (n > 1) has a 0us latency, all levels m (m >= n)
2156 * need to be disabled. We make sure to sanitize the values out
2157 * of the punit to satisfy this requirement.
2158 */
2159 for (level = 1; level <= max_level; level++) {
2160 if (wm[level] == 0) {
2161 for (i = level + 1; i <= max_level; i++)
2162 wm[i] = 0;
2163 break;
2164 }
2165 }
2166
2167 /*
Damien Lespiau6f972352015-02-09 19:33:07 +00002168 * WaWmMemoryReadLatency:skl
2169 *
Vandana Kannan367294b2014-11-04 17:06:46 +00002170 * punit doesn't take into account the read latency so we need
Paulo Zanoni0727e402016-09-22 18:00:30 -03002171 * to add 2us to the various latency levels we retrieve from the
2172 * punit when level 0 response data us 0us.
Vandana Kannan367294b2014-11-04 17:06:46 +00002173 */
Paulo Zanoni0727e402016-09-22 18:00:30 -03002174 if (wm[0] == 0) {
2175 wm[0] += 2;
2176 for (level = 1; level <= max_level; level++) {
2177 if (wm[level] == 0)
2178 break;
Vandana Kannan367294b2014-11-04 17:06:46 +00002179 wm[level] += 2;
Vandana Kannan4f947382014-11-04 17:06:47 +00002180 }
Paulo Zanoni0727e402016-09-22 18:00:30 -03002181 }
2182
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002183 } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
Ville Syrjälä12b134d2013-07-05 11:57:21 +03002184 uint64_t sskpd = I915_READ64(MCH_SSKPD);
2185
2186 wm[0] = (sskpd >> 56) & 0xFF;
2187 if (wm[0] == 0)
2188 wm[0] = sskpd & 0xF;
Ville Syrjäläe5d50192013-07-05 11:57:22 +03002189 wm[1] = (sskpd >> 4) & 0xFF;
2190 wm[2] = (sskpd >> 12) & 0xFF;
2191 wm[3] = (sskpd >> 20) & 0x1FF;
2192 wm[4] = (sskpd >> 32) & 0x1FF;
Ville Syrjälä63cf9a12013-07-05 11:57:23 +03002193 } else if (INTEL_INFO(dev)->gen >= 6) {
2194 uint32_t sskpd = I915_READ(MCH_SSKPD);
2195
2196 wm[0] = (sskpd >> SSKPD_WM0_SHIFT) & SSKPD_WM_MASK;
2197 wm[1] = (sskpd >> SSKPD_WM1_SHIFT) & SSKPD_WM_MASK;
2198 wm[2] = (sskpd >> SSKPD_WM2_SHIFT) & SSKPD_WM_MASK;
2199 wm[3] = (sskpd >> SSKPD_WM3_SHIFT) & SSKPD_WM_MASK;
Ville Syrjälä3a88d0a2013-08-01 16:18:49 +03002200 } else if (INTEL_INFO(dev)->gen >= 5) {
2201 uint32_t mltr = I915_READ(MLTR_ILK);
2202
2203 /* ILK primary LP0 latency is 700 ns */
2204 wm[0] = 7;
2205 wm[1] = (mltr >> MLTR_WM1_SHIFT) & ILK_SRLT_MASK;
2206 wm[2] = (mltr >> MLTR_WM2_SHIFT) & ILK_SRLT_MASK;
Ville Syrjälä12b134d2013-07-05 11:57:21 +03002207 }
2208}
2209
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002210static void intel_fixup_spr_wm_latency(struct drm_i915_private *dev_priv,
2211 uint16_t wm[5])
Ville Syrjälä53615a52013-08-01 16:18:50 +03002212{
2213 /* ILK sprite LP0 latency is 1300 ns */
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002214 if (IS_GEN5(dev_priv))
Ville Syrjälä53615a52013-08-01 16:18:50 +03002215 wm[0] = 13;
2216}
2217
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01002218static void intel_fixup_cur_wm_latency(struct drm_i915_private *dev_priv,
2219 uint16_t wm[5])
Ville Syrjälä53615a52013-08-01 16:18:50 +03002220{
2221 /* ILK cursor LP0 latency is 1300 ns */
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01002222 if (IS_GEN5(dev_priv))
Ville Syrjälä53615a52013-08-01 16:18:50 +03002223 wm[0] = 13;
2224
2225 /* WaDoubleCursorLP3Latency:ivb */
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01002226 if (IS_IVYBRIDGE(dev_priv))
Ville Syrjälä53615a52013-08-01 16:18:50 +03002227 wm[3] *= 2;
2228}
2229
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002230int ilk_wm_max_level(const struct drm_i915_private *dev_priv)
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03002231{
2232 /* how many WM levels are we expecting */
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002233 if (INTEL_GEN(dev_priv) >= 9)
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002234 return 7;
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002235 else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03002236 return 4;
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002237 else if (INTEL_GEN(dev_priv) >= 6)
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03002238 return 3;
2239 else
2240 return 2;
2241}
Daniel Vetter7526ed72014-09-29 15:07:19 +02002242
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002243static void intel_print_wm_latency(struct drm_i915_private *dev_priv,
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002244 const char *name,
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002245 const uint16_t wm[8])
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002246{
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002247 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002248
2249 for (level = 0; level <= max_level; level++) {
2250 unsigned int latency = wm[level];
2251
2252 if (latency == 0) {
2253 DRM_ERROR("%s WM%d latency not provided\n",
2254 name, level);
2255 continue;
2256 }
2257
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002258 /*
2259 * - latencies are in us on gen9.
2260 * - before then, WM1+ latency values are in 0.5us units
2261 */
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002262 if (IS_GEN9(dev_priv))
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002263 latency *= 10;
2264 else if (level > 0)
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002265 latency *= 5;
2266
2267 DRM_DEBUG_KMS("%s WM%d latency %u (%u.%u usec)\n",
2268 name, level, wm[level],
2269 latency / 10, latency % 10);
2270 }
2271}
2272
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002273static bool ilk_increase_wm_latency(struct drm_i915_private *dev_priv,
2274 uint16_t wm[5], uint16_t min)
2275{
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002276 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002277
2278 if (wm[0] >= min)
2279 return false;
2280
2281 wm[0] = max(wm[0], min);
2282 for (level = 1; level <= max_level; level++)
2283 wm[level] = max_t(uint16_t, wm[level], DIV_ROUND_UP(min, 5));
2284
2285 return true;
2286}
2287
2288static void snb_wm_latency_quirk(struct drm_device *dev)
2289{
Chris Wilsonfac5e232016-07-04 11:34:36 +01002290 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002291 bool changed;
2292
2293 /*
2294 * The BIOS provided WM memory latency values are often
2295 * inadequate for high resolution displays. Adjust them.
2296 */
2297 changed = ilk_increase_wm_latency(dev_priv, dev_priv->wm.pri_latency, 12) |
2298 ilk_increase_wm_latency(dev_priv, dev_priv->wm.spr_latency, 12) |
2299 ilk_increase_wm_latency(dev_priv, dev_priv->wm.cur_latency, 12);
2300
2301 if (!changed)
2302 return;
2303
2304 DRM_DEBUG_KMS("WM latency values increased to avoid potential underruns\n");
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002305 intel_print_wm_latency(dev_priv, "Primary", dev_priv->wm.pri_latency);
2306 intel_print_wm_latency(dev_priv, "Sprite", dev_priv->wm.spr_latency);
2307 intel_print_wm_latency(dev_priv, "Cursor", dev_priv->wm.cur_latency);
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002308}
2309
Damien Lespiaufa50ad62014-03-17 18:01:16 +00002310static void ilk_setup_wm_latency(struct drm_device *dev)
Ville Syrjälä53615a52013-08-01 16:18:50 +03002311{
Chris Wilsonfac5e232016-07-04 11:34:36 +01002312 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä53615a52013-08-01 16:18:50 +03002313
2314 intel_read_wm_latency(dev, dev_priv->wm.pri_latency);
2315
2316 memcpy(dev_priv->wm.spr_latency, dev_priv->wm.pri_latency,
2317 sizeof(dev_priv->wm.pri_latency));
2318 memcpy(dev_priv->wm.cur_latency, dev_priv->wm.pri_latency,
2319 sizeof(dev_priv->wm.pri_latency));
2320
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002321 intel_fixup_spr_wm_latency(dev_priv, dev_priv->wm.spr_latency);
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01002322 intel_fixup_cur_wm_latency(dev_priv, dev_priv->wm.cur_latency);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002323
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002324 intel_print_wm_latency(dev_priv, "Primary", dev_priv->wm.pri_latency);
2325 intel_print_wm_latency(dev_priv, "Sprite", dev_priv->wm.spr_latency);
2326 intel_print_wm_latency(dev_priv, "Cursor", dev_priv->wm.cur_latency);
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002327
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002328 if (IS_GEN6(dev_priv))
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002329 snb_wm_latency_quirk(dev);
Ville Syrjälä53615a52013-08-01 16:18:50 +03002330}
2331
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002332static void skl_setup_wm_latency(struct drm_device *dev)
2333{
Chris Wilsonfac5e232016-07-04 11:34:36 +01002334 struct drm_i915_private *dev_priv = to_i915(dev);
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002335
2336 intel_read_wm_latency(dev, dev_priv->wm.skl_latency);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002337 intel_print_wm_latency(dev_priv, "Gen9 Plane", dev_priv->wm.skl_latency);
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002338}
2339
Matt Ropered4a6a72016-02-23 17:20:13 -08002340static bool ilk_validate_pipe_wm(struct drm_device *dev,
2341 struct intel_pipe_wm *pipe_wm)
2342{
2343 /* LP0 watermark maximums depend on this pipe alone */
2344 const struct intel_wm_config config = {
2345 .num_pipes_active = 1,
2346 .sprites_enabled = pipe_wm->sprites_enabled,
2347 .sprites_scaled = pipe_wm->sprites_scaled,
2348 };
2349 struct ilk_wm_maximums max;
2350
2351 /* LP0 watermarks always use 1/2 DDB partitioning */
2352 ilk_compute_wm_maximums(dev, 0, &config, INTEL_DDB_PART_1_2, &max);
2353
2354 /* At least LP0 must be valid */
2355 if (!ilk_validate_wm_level(0, &max, &pipe_wm->wm[0])) {
2356 DRM_DEBUG_KMS("LP0 watermark invalid\n");
2357 return false;
2358 }
2359
2360 return true;
2361}
2362
Matt Roper261a27d2015-10-08 15:28:25 -07002363/* Compute new watermarks for the pipe */
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002364static int ilk_compute_pipe_wm(struct intel_crtc_state *cstate)
Matt Roper261a27d2015-10-08 15:28:25 -07002365{
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002366 struct drm_atomic_state *state = cstate->base.state;
2367 struct intel_crtc *intel_crtc = to_intel_crtc(cstate->base.crtc);
Matt Roper86c8bbb2015-09-24 15:53:16 -07002368 struct intel_pipe_wm *pipe_wm;
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002369 struct drm_device *dev = state->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01002370 const struct drm_i915_private *dev_priv = to_i915(dev);
Matt Roper43d59ed2015-09-24 15:53:07 -07002371 struct intel_plane *intel_plane;
Matt Roper86c8bbb2015-09-24 15:53:16 -07002372 struct intel_plane_state *pristate = NULL;
Matt Roper43d59ed2015-09-24 15:53:07 -07002373 struct intel_plane_state *sprstate = NULL;
Matt Roper86c8bbb2015-09-24 15:53:16 -07002374 struct intel_plane_state *curstate = NULL;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002375 int level, max_level = ilk_wm_max_level(dev_priv), usable_level;
Imre Deak820c1982013-12-17 14:46:36 +02002376 struct ilk_wm_maximums max;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002377
Matt Ropere8f1f022016-05-12 07:05:55 -07002378 pipe_wm = &cstate->wm.ilk.optimal;
Matt Roper86c8bbb2015-09-24 15:53:16 -07002379
Matt Roper43d59ed2015-09-24 15:53:07 -07002380 for_each_intel_plane_on_crtc(dev, intel_crtc, intel_plane) {
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002381 struct intel_plane_state *ps;
2382
2383 ps = intel_atomic_get_existing_plane_state(state,
2384 intel_plane);
2385 if (!ps)
2386 continue;
Matt Roper86c8bbb2015-09-24 15:53:16 -07002387
2388 if (intel_plane->base.type == DRM_PLANE_TYPE_PRIMARY)
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002389 pristate = ps;
Matt Roper86c8bbb2015-09-24 15:53:16 -07002390 else if (intel_plane->base.type == DRM_PLANE_TYPE_OVERLAY)
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002391 sprstate = ps;
Matt Roper86c8bbb2015-09-24 15:53:16 -07002392 else if (intel_plane->base.type == DRM_PLANE_TYPE_CURSOR)
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002393 curstate = ps;
Matt Roper43d59ed2015-09-24 15:53:07 -07002394 }
2395
Matt Ropered4a6a72016-02-23 17:20:13 -08002396 pipe_wm->pipe_enabled = cstate->base.active;
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002397 if (sprstate) {
Ville Syrjälä936e71e2016-07-26 19:06:59 +03002398 pipe_wm->sprites_enabled = sprstate->base.visible;
2399 pipe_wm->sprites_scaled = sprstate->base.visible &&
2400 (drm_rect_width(&sprstate->base.dst) != drm_rect_width(&sprstate->base.src) >> 16 ||
2401 drm_rect_height(&sprstate->base.dst) != drm_rect_height(&sprstate->base.src) >> 16);
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002402 }
2403
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01002404 usable_level = max_level;
2405
Ville Syrjälä7b39a0b2013-12-05 15:51:30 +02002406 /* ILK/SNB: LP2+ watermarks only w/o sprites */
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002407 if (INTEL_INFO(dev)->gen <= 6 && pipe_wm->sprites_enabled)
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01002408 usable_level = 1;
Ville Syrjälä7b39a0b2013-12-05 15:51:30 +02002409
2410 /* ILK/SNB/IVB: LP1+ watermarks only w/o scaling */
Matt Ropered4a6a72016-02-23 17:20:13 -08002411 if (pipe_wm->sprites_scaled)
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01002412 usable_level = 0;
Ville Syrjälä7b39a0b2013-12-05 15:51:30 +02002413
Matt Roper86c8bbb2015-09-24 15:53:16 -07002414 ilk_compute_wm_level(dev_priv, intel_crtc, 0, cstate,
Maarten Lankhorst71f0a622016-03-08 10:57:16 +01002415 pristate, sprstate, curstate, &pipe_wm->raw_wm[0]);
2416
2417 memset(&pipe_wm->wm, 0, sizeof(pipe_wm->wm));
2418 pipe_wm->wm[0] = pipe_wm->raw_wm[0];
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002419
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002420 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjälä532f7a72016-04-29 17:31:17 +03002421 pipe_wm->linetime = hsw_compute_linetime_wm(cstate);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002422
Matt Ropered4a6a72016-02-23 17:20:13 -08002423 if (!ilk_validate_pipe_wm(dev, pipe_wm))
Maarten Lankhorst1a426d62016-03-02 12:36:03 +01002424 return -EINVAL;
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002425
2426 ilk_compute_wm_reg_maximums(dev, 1, &max);
2427
2428 for (level = 1; level <= max_level; level++) {
Maarten Lankhorst71f0a622016-03-08 10:57:16 +01002429 struct intel_wm_level *wm = &pipe_wm->raw_wm[level];
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002430
Matt Roper86c8bbb2015-09-24 15:53:16 -07002431 ilk_compute_wm_level(dev_priv, intel_crtc, level, cstate,
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01002432 pristate, sprstate, curstate, wm);
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002433
2434 /*
2435 * Disable any watermark level that exceeds the
2436 * register maximums since such watermarks are
2437 * always invalid.
2438 */
Maarten Lankhorst71f0a622016-03-08 10:57:16 +01002439 if (level > usable_level)
2440 continue;
2441
2442 if (ilk_validate_wm_level(level, &max, wm))
2443 pipe_wm->wm[level] = *wm;
2444 else
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01002445 usable_level = level;
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002446 }
2447
Matt Roper86c8bbb2015-09-24 15:53:16 -07002448 return 0;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002449}
2450
2451/*
Matt Ropered4a6a72016-02-23 17:20:13 -08002452 * Build a set of 'intermediate' watermark values that satisfy both the old
2453 * state and the new state. These can be programmed to the hardware
2454 * immediately.
2455 */
2456static int ilk_compute_intermediate_wm(struct drm_device *dev,
2457 struct intel_crtc *intel_crtc,
2458 struct intel_crtc_state *newstate)
2459{
Matt Ropere8f1f022016-05-12 07:05:55 -07002460 struct intel_pipe_wm *a = &newstate->wm.ilk.intermediate;
Matt Ropered4a6a72016-02-23 17:20:13 -08002461 struct intel_pipe_wm *b = &intel_crtc->wm.active.ilk;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002462 int level, max_level = ilk_wm_max_level(to_i915(dev));
Matt Ropered4a6a72016-02-23 17:20:13 -08002463
2464 /*
2465 * Start with the final, target watermarks, then combine with the
2466 * currently active watermarks to get values that are safe both before
2467 * and after the vblank.
2468 */
Matt Ropere8f1f022016-05-12 07:05:55 -07002469 *a = newstate->wm.ilk.optimal;
Matt Ropered4a6a72016-02-23 17:20:13 -08002470 a->pipe_enabled |= b->pipe_enabled;
2471 a->sprites_enabled |= b->sprites_enabled;
2472 a->sprites_scaled |= b->sprites_scaled;
2473
2474 for (level = 0; level <= max_level; level++) {
2475 struct intel_wm_level *a_wm = &a->wm[level];
2476 const struct intel_wm_level *b_wm = &b->wm[level];
2477
2478 a_wm->enable &= b_wm->enable;
2479 a_wm->pri_val = max(a_wm->pri_val, b_wm->pri_val);
2480 a_wm->spr_val = max(a_wm->spr_val, b_wm->spr_val);
2481 a_wm->cur_val = max(a_wm->cur_val, b_wm->cur_val);
2482 a_wm->fbc_val = max(a_wm->fbc_val, b_wm->fbc_val);
2483 }
2484
2485 /*
2486 * We need to make sure that these merged watermark values are
2487 * actually a valid configuration themselves. If they're not,
2488 * there's no safe way to transition from the old state to
2489 * the new state, so we need to fail the atomic transaction.
2490 */
2491 if (!ilk_validate_pipe_wm(dev, a))
2492 return -EINVAL;
2493
2494 /*
2495 * If our intermediate WM are identical to the final WM, then we can
2496 * omit the post-vblank programming; only update if it's different.
2497 */
Matt Ropere8f1f022016-05-12 07:05:55 -07002498 if (memcmp(a, &newstate->wm.ilk.optimal, sizeof(*a)) == 0)
Matt Ropered4a6a72016-02-23 17:20:13 -08002499 newstate->wm.need_postvbl_update = false;
2500
2501 return 0;
2502}
2503
2504/*
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002505 * Merge the watermarks from all active pipes for a specific level.
2506 */
2507static void ilk_merge_wm_level(struct drm_device *dev,
2508 int level,
2509 struct intel_wm_level *ret_wm)
2510{
2511 const struct intel_crtc *intel_crtc;
2512
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002513 ret_wm->enable = true;
2514
Damien Lespiaud3fcc802014-05-13 23:32:22 +01002515 for_each_intel_crtc(dev, intel_crtc) {
Matt Ropered4a6a72016-02-23 17:20:13 -08002516 const struct intel_pipe_wm *active = &intel_crtc->wm.active.ilk;
Ville Syrjäläfe392ef2014-03-07 18:32:10 +02002517 const struct intel_wm_level *wm = &active->wm[level];
2518
2519 if (!active->pipe_enabled)
2520 continue;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002521
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002522 /*
2523 * The watermark values may have been used in the past,
2524 * so we must maintain them in the registers for some
2525 * time even if the level is now disabled.
2526 */
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002527 if (!wm->enable)
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002528 ret_wm->enable = false;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002529
2530 ret_wm->pri_val = max(ret_wm->pri_val, wm->pri_val);
2531 ret_wm->spr_val = max(ret_wm->spr_val, wm->spr_val);
2532 ret_wm->cur_val = max(ret_wm->cur_val, wm->cur_val);
2533 ret_wm->fbc_val = max(ret_wm->fbc_val, wm->fbc_val);
2534 }
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002535}
2536
2537/*
2538 * Merge all low power watermarks for all active pipes.
2539 */
2540static void ilk_wm_merge(struct drm_device *dev,
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02002541 const struct intel_wm_config *config,
Imre Deak820c1982013-12-17 14:46:36 +02002542 const struct ilk_wm_maximums *max,
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002543 struct intel_pipe_wm *merged)
2544{
Chris Wilsonfac5e232016-07-04 11:34:36 +01002545 struct drm_i915_private *dev_priv = to_i915(dev);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002546 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002547 int last_enabled_level = max_level;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002548
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02002549 /* ILK/SNB/IVB: LP1+ watermarks only w/ single pipe */
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01002550 if ((INTEL_GEN(dev_priv) <= 6 || IS_IVYBRIDGE(dev_priv)) &&
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02002551 config->num_pipes_active > 1)
Ville Syrjälä1204d5b2016-04-01 21:53:18 +03002552 last_enabled_level = 0;
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02002553
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02002554 /* ILK: FBC WM must be disabled always */
2555 merged->fbc_wm_enabled = INTEL_INFO(dev)->gen >= 6;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002556
2557 /* merge each WM1+ level */
2558 for (level = 1; level <= max_level; level++) {
2559 struct intel_wm_level *wm = &merged->wm[level];
2560
2561 ilk_merge_wm_level(dev, level, wm);
2562
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002563 if (level > last_enabled_level)
2564 wm->enable = false;
2565 else if (!ilk_validate_wm_level(level, max, wm))
2566 /* make sure all following levels get disabled */
2567 last_enabled_level = level - 1;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002568
2569 /*
2570 * The spec says it is preferred to disable
2571 * FBC WMs instead of disabling a WM level.
2572 */
2573 if (wm->fbc_val > max->fbc) {
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002574 if (wm->enable)
2575 merged->fbc_wm_enabled = false;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002576 wm->fbc_val = 0;
2577 }
2578 }
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02002579
2580 /* ILK: LP2+ must be disabled when FBC WM is disabled but FBC enabled */
2581 /*
2582 * FIXME this is racy. FBC might get enabled later.
2583 * What we should check here is whether FBC can be
2584 * enabled sometime later.
2585 */
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002586 if (IS_GEN5(dev_priv) && !merged->fbc_wm_enabled &&
Paulo Zanoni0e631ad2015-10-14 17:45:36 -03002587 intel_fbc_is_active(dev_priv)) {
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02002588 for (level = 2; level <= max_level; level++) {
2589 struct intel_wm_level *wm = &merged->wm[level];
2590
2591 wm->enable = false;
2592 }
2593 }
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002594}
2595
Ville Syrjäläb380ca32013-10-09 19:18:01 +03002596static int ilk_wm_lp_to_level(int wm_lp, const struct intel_pipe_wm *pipe_wm)
2597{
2598 /* LP1,LP2,LP3 levels are either 1,2,3 or 1,3,4 */
2599 return wm_lp + (wm_lp >= 2 && pipe_wm->wm[4].enable);
2600}
2601
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02002602/* The value we need to program into the WM_LPx latency field */
2603static unsigned int ilk_wm_lp_latency(struct drm_device *dev, int level)
2604{
Chris Wilsonfac5e232016-07-04 11:34:36 +01002605 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02002606
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002607 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02002608 return 2 * level;
2609 else
2610 return dev_priv->wm.pri_latency[level];
2611}
2612
Imre Deak820c1982013-12-17 14:46:36 +02002613static void ilk_compute_wm_results(struct drm_device *dev,
Ville Syrjälä0362c782013-10-09 19:17:57 +03002614 const struct intel_pipe_wm *merged,
Ville Syrjälä609cede2013-10-09 19:18:03 +03002615 enum intel_ddb_partitioning partitioning,
Imre Deak820c1982013-12-17 14:46:36 +02002616 struct ilk_wm_values *results)
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002617{
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002618 struct intel_crtc *intel_crtc;
2619 int level, wm_lp;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002620
Ville Syrjälä0362c782013-10-09 19:17:57 +03002621 results->enable_fbc_wm = merged->fbc_wm_enabled;
Ville Syrjälä609cede2013-10-09 19:18:03 +03002622 results->partitioning = partitioning;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002623
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002624 /* LP1+ register values */
Paulo Zanonicca32e92013-05-31 11:45:06 -03002625 for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
Ville Syrjälä1fd527c2013-08-06 22:24:05 +03002626 const struct intel_wm_level *r;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002627
Ville Syrjäläb380ca32013-10-09 19:18:01 +03002628 level = ilk_wm_lp_to_level(wm_lp, merged);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002629
Ville Syrjälä0362c782013-10-09 19:17:57 +03002630 r = &merged->wm[level];
Paulo Zanonicca32e92013-05-31 11:45:06 -03002631
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002632 /*
2633 * Maintain the watermark values even if the level is
2634 * disabled. Doing otherwise could cause underruns.
2635 */
2636 results->wm_lp[wm_lp - 1] =
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02002637 (ilk_wm_lp_latency(dev, level) << WM1_LP_LATENCY_SHIFT) |
Ville Syrjälä416f4722013-11-02 21:07:46 -07002638 (r->pri_val << WM1_LP_SR_SHIFT) |
2639 r->cur_val;
2640
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002641 if (r->enable)
2642 results->wm_lp[wm_lp - 1] |= WM1_LP_SR_EN;
2643
Ville Syrjälä416f4722013-11-02 21:07:46 -07002644 if (INTEL_INFO(dev)->gen >= 8)
2645 results->wm_lp[wm_lp - 1] |=
2646 r->fbc_val << WM1_LP_FBC_SHIFT_BDW;
2647 else
2648 results->wm_lp[wm_lp - 1] |=
2649 r->fbc_val << WM1_LP_FBC_SHIFT;
2650
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002651 /*
2652 * Always set WM1S_LP_EN when spr_val != 0, even if the
2653 * level is disabled. Doing otherwise could cause underruns.
2654 */
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02002655 if (INTEL_INFO(dev)->gen <= 6 && r->spr_val) {
2656 WARN_ON(wm_lp != 1);
2657 results->wm_lp_spr[wm_lp - 1] = WM1S_LP_EN | r->spr_val;
2658 } else
2659 results->wm_lp_spr[wm_lp - 1] = r->spr_val;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002660 }
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002661
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002662 /* LP0 register values */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01002663 for_each_intel_crtc(dev, intel_crtc) {
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002664 enum pipe pipe = intel_crtc->pipe;
Matt Ropered4a6a72016-02-23 17:20:13 -08002665 const struct intel_wm_level *r =
2666 &intel_crtc->wm.active.ilk.wm[0];
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002667
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002668 if (WARN_ON(!r->enable))
2669 continue;
2670
Matt Ropered4a6a72016-02-23 17:20:13 -08002671 results->wm_linetime[pipe] = intel_crtc->wm.active.ilk.linetime;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002672
2673 results->wm_pipe[pipe] =
2674 (r->pri_val << WM0_PIPE_PLANE_SHIFT) |
2675 (r->spr_val << WM0_PIPE_SPRITE_SHIFT) |
2676 r->cur_val;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002677 }
2678}
2679
Paulo Zanoni861f3382013-05-31 10:19:21 -03002680/* Find the result with the highest level enabled. Check for enable_fbc_wm in
2681 * case both are at the same level. Prefer r1 in case they're the same. */
Imre Deak820c1982013-12-17 14:46:36 +02002682static struct intel_pipe_wm *ilk_find_best_result(struct drm_device *dev,
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002683 struct intel_pipe_wm *r1,
2684 struct intel_pipe_wm *r2)
Paulo Zanoni861f3382013-05-31 10:19:21 -03002685{
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002686 int level, max_level = ilk_wm_max_level(to_i915(dev));
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002687 int level1 = 0, level2 = 0;
Paulo Zanoni861f3382013-05-31 10:19:21 -03002688
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002689 for (level = 1; level <= max_level; level++) {
2690 if (r1->wm[level].enable)
2691 level1 = level;
2692 if (r2->wm[level].enable)
2693 level2 = level;
Paulo Zanoni861f3382013-05-31 10:19:21 -03002694 }
2695
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002696 if (level1 == level2) {
2697 if (r2->fbc_wm_enabled && !r1->fbc_wm_enabled)
Paulo Zanoni861f3382013-05-31 10:19:21 -03002698 return r2;
2699 else
2700 return r1;
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002701 } else if (level1 > level2) {
Paulo Zanoni861f3382013-05-31 10:19:21 -03002702 return r1;
2703 } else {
2704 return r2;
2705 }
2706}
2707
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002708/* dirty bits used to track which watermarks need changes */
2709#define WM_DIRTY_PIPE(pipe) (1 << (pipe))
2710#define WM_DIRTY_LINETIME(pipe) (1 << (8 + (pipe)))
2711#define WM_DIRTY_LP(wm_lp) (1 << (15 + (wm_lp)))
2712#define WM_DIRTY_LP_ALL (WM_DIRTY_LP(1) | WM_DIRTY_LP(2) | WM_DIRTY_LP(3))
2713#define WM_DIRTY_FBC (1 << 24)
2714#define WM_DIRTY_DDB (1 << 25)
2715
Damien Lespiau055e3932014-08-18 13:49:10 +01002716static unsigned int ilk_compute_wm_dirty(struct drm_i915_private *dev_priv,
Imre Deak820c1982013-12-17 14:46:36 +02002717 const struct ilk_wm_values *old,
2718 const struct ilk_wm_values *new)
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002719{
2720 unsigned int dirty = 0;
2721 enum pipe pipe;
2722 int wm_lp;
2723
Damien Lespiau055e3932014-08-18 13:49:10 +01002724 for_each_pipe(dev_priv, pipe) {
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002725 if (old->wm_linetime[pipe] != new->wm_linetime[pipe]) {
2726 dirty |= WM_DIRTY_LINETIME(pipe);
2727 /* Must disable LP1+ watermarks too */
2728 dirty |= WM_DIRTY_LP_ALL;
2729 }
2730
2731 if (old->wm_pipe[pipe] != new->wm_pipe[pipe]) {
2732 dirty |= WM_DIRTY_PIPE(pipe);
2733 /* Must disable LP1+ watermarks too */
2734 dirty |= WM_DIRTY_LP_ALL;
2735 }
2736 }
2737
2738 if (old->enable_fbc_wm != new->enable_fbc_wm) {
2739 dirty |= WM_DIRTY_FBC;
2740 /* Must disable LP1+ watermarks too */
2741 dirty |= WM_DIRTY_LP_ALL;
2742 }
2743
2744 if (old->partitioning != new->partitioning) {
2745 dirty |= WM_DIRTY_DDB;
2746 /* Must disable LP1+ watermarks too */
2747 dirty |= WM_DIRTY_LP_ALL;
2748 }
2749
2750 /* LP1+ watermarks already deemed dirty, no need to continue */
2751 if (dirty & WM_DIRTY_LP_ALL)
2752 return dirty;
2753
2754 /* Find the lowest numbered LP1+ watermark in need of an update... */
2755 for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
2756 if (old->wm_lp[wm_lp - 1] != new->wm_lp[wm_lp - 1] ||
2757 old->wm_lp_spr[wm_lp - 1] != new->wm_lp_spr[wm_lp - 1])
2758 break;
2759 }
2760
2761 /* ...and mark it and all higher numbered LP1+ watermarks as dirty */
2762 for (; wm_lp <= 3; wm_lp++)
2763 dirty |= WM_DIRTY_LP(wm_lp);
2764
2765 return dirty;
2766}
2767
Ville Syrjälä8553c182013-12-05 15:51:39 +02002768static bool _ilk_disable_lp_wm(struct drm_i915_private *dev_priv,
2769 unsigned int dirty)
2770{
Imre Deak820c1982013-12-17 14:46:36 +02002771 struct ilk_wm_values *previous = &dev_priv->wm.hw;
Ville Syrjälä8553c182013-12-05 15:51:39 +02002772 bool changed = false;
2773
2774 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] & WM1_LP_SR_EN) {
2775 previous->wm_lp[2] &= ~WM1_LP_SR_EN;
2776 I915_WRITE(WM3_LP_ILK, previous->wm_lp[2]);
2777 changed = true;
2778 }
2779 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] & WM1_LP_SR_EN) {
2780 previous->wm_lp[1] &= ~WM1_LP_SR_EN;
2781 I915_WRITE(WM2_LP_ILK, previous->wm_lp[1]);
2782 changed = true;
2783 }
2784 if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] & WM1_LP_SR_EN) {
2785 previous->wm_lp[0] &= ~WM1_LP_SR_EN;
2786 I915_WRITE(WM1_LP_ILK, previous->wm_lp[0]);
2787 changed = true;
2788 }
2789
2790 /*
2791 * Don't touch WM1S_LP_EN here.
2792 * Doing so could cause underruns.
2793 */
2794
2795 return changed;
2796}
2797
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002798/*
2799 * The spec says we shouldn't write when we don't need, because every write
2800 * causes WMs to be re-evaluated, expending some power.
2801 */
Imre Deak820c1982013-12-17 14:46:36 +02002802static void ilk_write_wm_values(struct drm_i915_private *dev_priv,
2803 struct ilk_wm_values *results)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002804{
Chris Wilson91c8a322016-07-05 10:40:23 +01002805 struct drm_device *dev = &dev_priv->drm;
Imre Deak820c1982013-12-17 14:46:36 +02002806 struct ilk_wm_values *previous = &dev_priv->wm.hw;
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002807 unsigned int dirty;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002808 uint32_t val;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002809
Damien Lespiau055e3932014-08-18 13:49:10 +01002810 dirty = ilk_compute_wm_dirty(dev_priv, previous, results);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002811 if (!dirty)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002812 return;
2813
Ville Syrjälä8553c182013-12-05 15:51:39 +02002814 _ilk_disable_lp_wm(dev_priv, dirty);
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02002815
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002816 if (dirty & WM_DIRTY_PIPE(PIPE_A))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002817 I915_WRITE(WM0_PIPEA_ILK, results->wm_pipe[0]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002818 if (dirty & WM_DIRTY_PIPE(PIPE_B))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002819 I915_WRITE(WM0_PIPEB_ILK, results->wm_pipe[1]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002820 if (dirty & WM_DIRTY_PIPE(PIPE_C))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002821 I915_WRITE(WM0_PIPEC_IVB, results->wm_pipe[2]);
2822
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002823 if (dirty & WM_DIRTY_LINETIME(PIPE_A))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002824 I915_WRITE(PIPE_WM_LINETIME(PIPE_A), results->wm_linetime[0]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002825 if (dirty & WM_DIRTY_LINETIME(PIPE_B))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002826 I915_WRITE(PIPE_WM_LINETIME(PIPE_B), results->wm_linetime[1]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002827 if (dirty & WM_DIRTY_LINETIME(PIPE_C))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002828 I915_WRITE(PIPE_WM_LINETIME(PIPE_C), results->wm_linetime[2]);
2829
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002830 if (dirty & WM_DIRTY_DDB) {
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002831 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
Ville Syrjäläac9545f2013-12-05 15:51:28 +02002832 val = I915_READ(WM_MISC);
2833 if (results->partitioning == INTEL_DDB_PART_1_2)
2834 val &= ~WM_MISC_DATA_PARTITION_5_6;
2835 else
2836 val |= WM_MISC_DATA_PARTITION_5_6;
2837 I915_WRITE(WM_MISC, val);
2838 } else {
2839 val = I915_READ(DISP_ARB_CTL2);
2840 if (results->partitioning == INTEL_DDB_PART_1_2)
2841 val &= ~DISP_DATA_PARTITION_5_6;
2842 else
2843 val |= DISP_DATA_PARTITION_5_6;
2844 I915_WRITE(DISP_ARB_CTL2, val);
2845 }
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002846 }
2847
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002848 if (dirty & WM_DIRTY_FBC) {
Paulo Zanonicca32e92013-05-31 11:45:06 -03002849 val = I915_READ(DISP_ARB_CTL);
2850 if (results->enable_fbc_wm)
2851 val &= ~DISP_FBC_WM_DIS;
2852 else
2853 val |= DISP_FBC_WM_DIS;
2854 I915_WRITE(DISP_ARB_CTL, val);
2855 }
2856
Imre Deak954911e2013-12-17 14:46:34 +02002857 if (dirty & WM_DIRTY_LP(1) &&
2858 previous->wm_lp_spr[0] != results->wm_lp_spr[0])
2859 I915_WRITE(WM1S_LP_ILK, results->wm_lp_spr[0]);
2860
2861 if (INTEL_INFO(dev)->gen >= 7) {
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02002862 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp_spr[1] != results->wm_lp_spr[1])
2863 I915_WRITE(WM2S_LP_IVB, results->wm_lp_spr[1]);
2864 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp_spr[2] != results->wm_lp_spr[2])
2865 I915_WRITE(WM3S_LP_IVB, results->wm_lp_spr[2]);
2866 }
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002867
Ville Syrjäläfacd619b2013-12-05 15:51:33 +02002868 if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] != results->wm_lp[0])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002869 I915_WRITE(WM1_LP_ILK, results->wm_lp[0]);
Ville Syrjäläfacd619b2013-12-05 15:51:33 +02002870 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] != results->wm_lp[1])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002871 I915_WRITE(WM2_LP_ILK, results->wm_lp[1]);
Ville Syrjäläfacd619b2013-12-05 15:51:33 +02002872 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] != results->wm_lp[2])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002873 I915_WRITE(WM3_LP_ILK, results->wm_lp[2]);
Ville Syrjälä609cede2013-10-09 19:18:03 +03002874
2875 dev_priv->wm.hw = *results;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002876}
2877
Matt Ropered4a6a72016-02-23 17:20:13 -08002878bool ilk_disable_lp_wm(struct drm_device *dev)
Ville Syrjälä8553c182013-12-05 15:51:39 +02002879{
Chris Wilsonfac5e232016-07-04 11:34:36 +01002880 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä8553c182013-12-05 15:51:39 +02002881
2882 return _ilk_disable_lp_wm(dev_priv, WM_DIRTY_LP_ALL);
2883}
2884
Lyude656d1b82016-08-17 15:55:54 -04002885#define SKL_SAGV_BLOCK_TIME 30 /* µs */
Damien Lespiaub9cec072014-11-04 17:06:43 +00002886
Matt Roper024c9042015-09-24 15:53:11 -07002887/*
2888 * Return the index of a plane in the SKL DDB and wm result arrays. Primary
2889 * plane is always in slot 0, cursor is always in slot I915_MAX_PLANES-1, and
2890 * other universal planes are in indices 1..n. Note that this may leave unused
2891 * indices between the top "sprite" plane and the cursor.
2892 */
2893static int
2894skl_wm_plane_id(const struct intel_plane *plane)
2895{
2896 switch (plane->base.type) {
2897 case DRM_PLANE_TYPE_PRIMARY:
2898 return 0;
2899 case DRM_PLANE_TYPE_CURSOR:
2900 return PLANE_CURSOR;
2901 case DRM_PLANE_TYPE_OVERLAY:
2902 return plane->plane + 1;
2903 default:
2904 MISSING_CASE(plane->base.type);
2905 return plane->plane;
2906 }
2907}
2908
Paulo Zanoniee3d5322016-10-11 15:25:38 -03002909/*
2910 * FIXME: We still don't have the proper code detect if we need to apply the WA,
2911 * so assume we'll always need it in order to avoid underruns.
2912 */
2913static bool skl_needs_memory_bw_wa(struct intel_atomic_state *state)
2914{
2915 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
2916
2917 if (IS_SKYLAKE(dev_priv) || IS_BROXTON(dev_priv) ||
2918 IS_KABYLAKE(dev_priv))
2919 return true;
2920
2921 return false;
2922}
2923
Paulo Zanoni56feca92016-09-22 18:00:28 -03002924static bool
2925intel_has_sagv(struct drm_i915_private *dev_priv)
2926{
Paulo Zanoni6e3100e2016-09-22 18:00:29 -03002927 if (IS_KABYLAKE(dev_priv))
2928 return true;
2929
2930 if (IS_SKYLAKE(dev_priv) &&
2931 dev_priv->sagv_status != I915_SAGV_NOT_CONTROLLED)
2932 return true;
2933
2934 return false;
Paulo Zanoni56feca92016-09-22 18:00:28 -03002935}
2936
Lyude656d1b82016-08-17 15:55:54 -04002937/*
2938 * SAGV dynamically adjusts the system agent voltage and clock frequencies
2939 * depending on power and performance requirements. The display engine access
2940 * to system memory is blocked during the adjustment time. Because of the
2941 * blocking time, having this enabled can cause full system hangs and/or pipe
2942 * underruns if we don't meet all of the following requirements:
2943 *
2944 * - <= 1 pipe enabled
2945 * - All planes can enable watermarks for latencies >= SAGV engine block time
2946 * - We're not using an interlaced display configuration
2947 */
2948int
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03002949intel_enable_sagv(struct drm_i915_private *dev_priv)
Lyude656d1b82016-08-17 15:55:54 -04002950{
2951 int ret;
2952
Paulo Zanoni56feca92016-09-22 18:00:28 -03002953 if (!intel_has_sagv(dev_priv))
2954 return 0;
2955
2956 if (dev_priv->sagv_status == I915_SAGV_ENABLED)
Lyude656d1b82016-08-17 15:55:54 -04002957 return 0;
2958
2959 DRM_DEBUG_KMS("Enabling the SAGV\n");
2960 mutex_lock(&dev_priv->rps.hw_lock);
2961
2962 ret = sandybridge_pcode_write(dev_priv, GEN9_PCODE_SAGV_CONTROL,
2963 GEN9_SAGV_ENABLE);
2964
2965 /* We don't need to wait for the SAGV when enabling */
2966 mutex_unlock(&dev_priv->rps.hw_lock);
2967
2968 /*
2969 * Some skl systems, pre-release machines in particular,
2970 * don't actually have an SAGV.
2971 */
Paulo Zanoni6e3100e2016-09-22 18:00:29 -03002972 if (IS_SKYLAKE(dev_priv) && ret == -ENXIO) {
Lyude656d1b82016-08-17 15:55:54 -04002973 DRM_DEBUG_DRIVER("No SAGV found on system, ignoring\n");
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03002974 dev_priv->sagv_status = I915_SAGV_NOT_CONTROLLED;
Lyude656d1b82016-08-17 15:55:54 -04002975 return 0;
2976 } else if (ret < 0) {
2977 DRM_ERROR("Failed to enable the SAGV\n");
2978 return ret;
2979 }
2980
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03002981 dev_priv->sagv_status = I915_SAGV_ENABLED;
Lyude656d1b82016-08-17 15:55:54 -04002982 return 0;
2983}
2984
2985static int
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03002986intel_do_sagv_disable(struct drm_i915_private *dev_priv)
Lyude656d1b82016-08-17 15:55:54 -04002987{
2988 int ret;
2989 uint32_t temp = GEN9_SAGV_DISABLE;
2990
2991 ret = sandybridge_pcode_read(dev_priv, GEN9_PCODE_SAGV_CONTROL,
2992 &temp);
2993 if (ret)
2994 return ret;
2995 else
2996 return temp & GEN9_SAGV_IS_DISABLED;
2997}
2998
2999int
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003000intel_disable_sagv(struct drm_i915_private *dev_priv)
Lyude656d1b82016-08-17 15:55:54 -04003001{
3002 int ret, result;
3003
Paulo Zanoni56feca92016-09-22 18:00:28 -03003004 if (!intel_has_sagv(dev_priv))
3005 return 0;
3006
3007 if (dev_priv->sagv_status == I915_SAGV_DISABLED)
Lyude656d1b82016-08-17 15:55:54 -04003008 return 0;
3009
3010 DRM_DEBUG_KMS("Disabling the SAGV\n");
3011 mutex_lock(&dev_priv->rps.hw_lock);
3012
3013 /* bspec says to keep retrying for at least 1 ms */
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003014 ret = wait_for(result = intel_do_sagv_disable(dev_priv), 1);
Lyude656d1b82016-08-17 15:55:54 -04003015 mutex_unlock(&dev_priv->rps.hw_lock);
3016
3017 if (ret == -ETIMEDOUT) {
3018 DRM_ERROR("Request to disable SAGV timed out\n");
3019 return -ETIMEDOUT;
3020 }
3021
3022 /*
3023 * Some skl systems, pre-release machines in particular,
3024 * don't actually have an SAGV.
3025 */
Paulo Zanoni6e3100e2016-09-22 18:00:29 -03003026 if (IS_SKYLAKE(dev_priv) && result == -ENXIO) {
Lyude656d1b82016-08-17 15:55:54 -04003027 DRM_DEBUG_DRIVER("No SAGV found on system, ignoring\n");
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003028 dev_priv->sagv_status = I915_SAGV_NOT_CONTROLLED;
Lyude656d1b82016-08-17 15:55:54 -04003029 return 0;
3030 } else if (result < 0) {
3031 DRM_ERROR("Failed to disable the SAGV\n");
3032 return result;
3033 }
3034
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003035 dev_priv->sagv_status = I915_SAGV_DISABLED;
Lyude656d1b82016-08-17 15:55:54 -04003036 return 0;
3037}
3038
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003039bool intel_can_enable_sagv(struct drm_atomic_state *state)
Lyude656d1b82016-08-17 15:55:54 -04003040{
3041 struct drm_device *dev = state->dev;
3042 struct drm_i915_private *dev_priv = to_i915(dev);
3043 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003044 struct intel_crtc *crtc;
3045 struct intel_plane *plane;
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003046 struct intel_crtc_state *cstate;
3047 struct skl_plane_wm *wm;
Lyude656d1b82016-08-17 15:55:54 -04003048 enum pipe pipe;
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003049 int level, latency;
Lyude656d1b82016-08-17 15:55:54 -04003050
Paulo Zanoni56feca92016-09-22 18:00:28 -03003051 if (!intel_has_sagv(dev_priv))
3052 return false;
3053
Lyude656d1b82016-08-17 15:55:54 -04003054 /*
3055 * SKL workaround: bspec recommends we disable the SAGV when we have
3056 * more then one pipe enabled
3057 *
3058 * If there are no active CRTCs, no additional checks need be performed
3059 */
3060 if (hweight32(intel_state->active_crtcs) == 0)
3061 return true;
3062 else if (hweight32(intel_state->active_crtcs) > 1)
3063 return false;
3064
3065 /* Since we're now guaranteed to only have one active CRTC... */
3066 pipe = ffs(intel_state->active_crtcs) - 1;
Ville Syrjälä98187832016-10-31 22:37:10 +02003067 crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003068 cstate = to_intel_crtc_state(crtc->base.state);
Lyude656d1b82016-08-17 15:55:54 -04003069
Paulo Zanonic89cadd2016-10-10 17:30:59 -03003070 if (crtc->base.state->adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Lyude656d1b82016-08-17 15:55:54 -04003071 return false;
3072
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003073 for_each_intel_plane_on_crtc(dev, crtc, plane) {
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003074 wm = &cstate->wm.skl.optimal.planes[skl_wm_plane_id(plane)];
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003075
Lyude656d1b82016-08-17 15:55:54 -04003076 /* Skip this plane if it's not enabled */
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003077 if (!wm->wm[0].plane_en)
Lyude656d1b82016-08-17 15:55:54 -04003078 continue;
3079
3080 /* Find the highest enabled wm level for this plane */
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003081 for (level = ilk_wm_max_level(dev_priv);
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003082 !wm->wm[level].plane_en; --level)
Lyude656d1b82016-08-17 15:55:54 -04003083 { }
3084
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003085 latency = dev_priv->wm.skl_latency[level];
3086
3087 if (skl_needs_memory_bw_wa(intel_state) &&
3088 plane->base.state->fb->modifier[0] ==
3089 I915_FORMAT_MOD_X_TILED)
3090 latency += 15;
3091
Lyude656d1b82016-08-17 15:55:54 -04003092 /*
3093 * If any of the planes on this pipe don't enable wm levels
3094 * that incur memory latencies higher then 30µs we can't enable
3095 * the SAGV
3096 */
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003097 if (latency < SKL_SAGV_BLOCK_TIME)
Lyude656d1b82016-08-17 15:55:54 -04003098 return false;
3099 }
3100
3101 return true;
3102}
3103
Damien Lespiaub9cec072014-11-04 17:06:43 +00003104static void
3105skl_ddb_get_pipe_allocation_limits(struct drm_device *dev,
Matt Roper024c9042015-09-24 15:53:11 -07003106 const struct intel_crtc_state *cstate,
Matt Roperc107acf2016-05-12 07:06:01 -07003107 struct skl_ddb_entry *alloc, /* out */
3108 int *num_active /* out */)
Damien Lespiaub9cec072014-11-04 17:06:43 +00003109{
Matt Roperc107acf2016-05-12 07:06:01 -07003110 struct drm_atomic_state *state = cstate->base.state;
3111 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
3112 struct drm_i915_private *dev_priv = to_i915(dev);
Matt Roper024c9042015-09-24 15:53:11 -07003113 struct drm_crtc *for_crtc = cstate->base.crtc;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003114 unsigned int pipe_size, ddb_size;
3115 int nth_active_pipe;
Matt Roperc107acf2016-05-12 07:06:01 -07003116
Matt Ropera6d3460e2016-05-12 07:06:04 -07003117 if (WARN_ON(!state) || !cstate->base.active) {
Damien Lespiaub9cec072014-11-04 17:06:43 +00003118 alloc->start = 0;
3119 alloc->end = 0;
Matt Ropera6d3460e2016-05-12 07:06:04 -07003120 *num_active = hweight32(dev_priv->active_crtcs);
Damien Lespiaub9cec072014-11-04 17:06:43 +00003121 return;
3122 }
3123
Matt Ropera6d3460e2016-05-12 07:06:04 -07003124 if (intel_state->active_pipe_changes)
3125 *num_active = hweight32(intel_state->active_crtcs);
3126 else
3127 *num_active = hweight32(dev_priv->active_crtcs);
3128
Deepak M6f3fff62016-09-15 15:01:10 +05303129 ddb_size = INTEL_INFO(dev_priv)->ddb_size;
3130 WARN_ON(ddb_size == 0);
Damien Lespiaub9cec072014-11-04 17:06:43 +00003131
3132 ddb_size -= 4; /* 4 blocks for bypass path allocation */
3133
Matt Roperc107acf2016-05-12 07:06:01 -07003134 /*
Matt Ropera6d3460e2016-05-12 07:06:04 -07003135 * If the state doesn't change the active CRTC's, then there's
3136 * no need to recalculate; the existing pipe allocation limits
3137 * should remain unchanged. Note that we're safe from racing
3138 * commits since any racing commit that changes the active CRTC
3139 * list would need to grab _all_ crtc locks, including the one
3140 * we currently hold.
Matt Roperc107acf2016-05-12 07:06:01 -07003141 */
Matt Ropera6d3460e2016-05-12 07:06:04 -07003142 if (!intel_state->active_pipe_changes) {
Lyudece0ba282016-09-15 10:46:35 -04003143 *alloc = to_intel_crtc(for_crtc)->hw_ddb;
Matt Ropera6d3460e2016-05-12 07:06:04 -07003144 return;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003145 }
Matt Ropera6d3460e2016-05-12 07:06:04 -07003146
3147 nth_active_pipe = hweight32(intel_state->active_crtcs &
3148 (drm_crtc_mask(for_crtc) - 1));
3149 pipe_size = ddb_size / hweight32(intel_state->active_crtcs);
3150 alloc->start = nth_active_pipe * ddb_size / *num_active;
3151 alloc->end = alloc->start + pipe_size;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003152}
3153
Matt Roperc107acf2016-05-12 07:06:01 -07003154static unsigned int skl_cursor_allocation(int num_active)
Damien Lespiaub9cec072014-11-04 17:06:43 +00003155{
Matt Roperc107acf2016-05-12 07:06:01 -07003156 if (num_active == 1)
Damien Lespiaub9cec072014-11-04 17:06:43 +00003157 return 32;
3158
3159 return 8;
3160}
3161
Damien Lespiaua269c582014-11-04 17:06:49 +00003162static void skl_ddb_entry_init_from_hw(struct skl_ddb_entry *entry, u32 reg)
3163{
3164 entry->start = reg & 0x3ff;
3165 entry->end = (reg >> 16) & 0x3ff;
Damien Lespiau16160e32014-11-04 17:06:53 +00003166 if (entry->end)
3167 entry->end += 1;
Damien Lespiaua269c582014-11-04 17:06:49 +00003168}
3169
Damien Lespiau08db6652014-11-04 17:06:52 +00003170void skl_ddb_get_hw_state(struct drm_i915_private *dev_priv,
3171 struct skl_ddb_allocation *ddb /* out */)
Damien Lespiaua269c582014-11-04 17:06:49 +00003172{
Damien Lespiaua269c582014-11-04 17:06:49 +00003173 enum pipe pipe;
3174 int plane;
3175 u32 val;
3176
Maarten Lankhorstb10f1b22015-10-22 13:56:34 +02003177 memset(ddb, 0, sizeof(*ddb));
3178
Damien Lespiaua269c582014-11-04 17:06:49 +00003179 for_each_pipe(dev_priv, pipe) {
Imre Deak4d800032016-02-17 16:31:29 +02003180 enum intel_display_power_domain power_domain;
3181
3182 power_domain = POWER_DOMAIN_PIPE(pipe);
3183 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
Maarten Lankhorstb10f1b22015-10-22 13:56:34 +02003184 continue;
3185
Matt Roper8b364b42016-10-26 15:51:28 -07003186 for_each_universal_plane(dev_priv, pipe, plane) {
Damien Lespiaua269c582014-11-04 17:06:49 +00003187 val = I915_READ(PLANE_BUF_CFG(pipe, plane));
3188 skl_ddb_entry_init_from_hw(&ddb->plane[pipe][plane],
3189 val);
3190 }
3191
3192 val = I915_READ(CUR_BUF_CFG(pipe));
Matt Roper4969d332015-09-24 15:53:10 -07003193 skl_ddb_entry_init_from_hw(&ddb->plane[pipe][PLANE_CURSOR],
3194 val);
Imre Deak4d800032016-02-17 16:31:29 +02003195
3196 intel_display_power_put(dev_priv, power_domain);
Damien Lespiaua269c582014-11-04 17:06:49 +00003197 }
3198}
3199
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07003200/*
3201 * Determines the downscale amount of a plane for the purposes of watermark calculations.
3202 * The bspec defines downscale amount as:
3203 *
3204 * """
3205 * Horizontal down scale amount = maximum[1, Horizontal source size /
3206 * Horizontal destination size]
3207 * Vertical down scale amount = maximum[1, Vertical source size /
3208 * Vertical destination size]
3209 * Total down scale amount = Horizontal down scale amount *
3210 * Vertical down scale amount
3211 * """
3212 *
3213 * Return value is provided in 16.16 fixed point form to retain fractional part.
3214 * Caller should take care of dividing & rounding off the value.
3215 */
3216static uint32_t
3217skl_plane_downscale_amount(const struct intel_plane_state *pstate)
3218{
3219 uint32_t downscale_h, downscale_w;
3220 uint32_t src_w, src_h, dst_w, dst_h;
3221
Ville Syrjälä936e71e2016-07-26 19:06:59 +03003222 if (WARN_ON(!pstate->base.visible))
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07003223 return DRM_PLANE_HELPER_NO_SCALING;
3224
3225 /* n.b., src is 16.16 fixed point, dst is whole integer */
Ville Syrjälä936e71e2016-07-26 19:06:59 +03003226 src_w = drm_rect_width(&pstate->base.src);
3227 src_h = drm_rect_height(&pstate->base.src);
3228 dst_w = drm_rect_width(&pstate->base.dst);
3229 dst_h = drm_rect_height(&pstate->base.dst);
Ville Syrjäläbd2ef252016-09-26 19:30:46 +03003230 if (drm_rotation_90_or_270(pstate->base.rotation))
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07003231 swap(dst_w, dst_h);
3232
3233 downscale_h = max(src_h / dst_h, (uint32_t)DRM_PLANE_HELPER_NO_SCALING);
3234 downscale_w = max(src_w / dst_w, (uint32_t)DRM_PLANE_HELPER_NO_SCALING);
3235
3236 /* Provide result in 16.16 fixed point */
3237 return (uint64_t)downscale_w * downscale_h >> 16;
3238}
3239
Damien Lespiaub9cec072014-11-04 17:06:43 +00003240static unsigned int
Matt Roper024c9042015-09-24 15:53:11 -07003241skl_plane_relative_data_rate(const struct intel_crtc_state *cstate,
3242 const struct drm_plane_state *pstate,
3243 int y)
Damien Lespiaub9cec072014-11-04 17:06:43 +00003244{
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07003245 struct intel_plane_state *intel_pstate = to_intel_plane_state(pstate);
Matt Roper024c9042015-09-24 15:53:11 -07003246 struct drm_framebuffer *fb = pstate->fb;
Kumar, Mahesh8d19d7d2016-05-19 15:03:01 -07003247 uint32_t down_scale_amount, data_rate;
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07003248 uint32_t width = 0, height = 0;
Matt Ropera1de91e2016-05-12 07:05:57 -07003249 unsigned format = fb ? fb->pixel_format : DRM_FORMAT_XRGB8888;
3250
Ville Syrjälä936e71e2016-07-26 19:06:59 +03003251 if (!intel_pstate->base.visible)
Matt Ropera1de91e2016-05-12 07:05:57 -07003252 return 0;
3253 if (pstate->plane->type == DRM_PLANE_TYPE_CURSOR)
3254 return 0;
3255 if (y && format != DRM_FORMAT_NV12)
3256 return 0;
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07003257
Ville Syrjälä936e71e2016-07-26 19:06:59 +03003258 width = drm_rect_width(&intel_pstate->base.src) >> 16;
3259 height = drm_rect_height(&intel_pstate->base.src) >> 16;
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07003260
Ville Syrjäläbd2ef252016-09-26 19:30:46 +03003261 if (drm_rotation_90_or_270(pstate->rotation))
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07003262 swap(width, height);
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003263
3264 /* for planar format */
Matt Ropera1de91e2016-05-12 07:05:57 -07003265 if (format == DRM_FORMAT_NV12) {
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003266 if (y) /* y-plane data rate */
Kumar, Mahesh8d19d7d2016-05-19 15:03:01 -07003267 data_rate = width * height *
Matt Ropera1de91e2016-05-12 07:05:57 -07003268 drm_format_plane_cpp(format, 0);
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003269 else /* uv-plane data rate */
Kumar, Mahesh8d19d7d2016-05-19 15:03:01 -07003270 data_rate = (width / 2) * (height / 2) *
Matt Ropera1de91e2016-05-12 07:05:57 -07003271 drm_format_plane_cpp(format, 1);
Kumar, Mahesh8d19d7d2016-05-19 15:03:01 -07003272 } else {
3273 /* for packed formats */
3274 data_rate = width * height * drm_format_plane_cpp(format, 0);
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003275 }
3276
Kumar, Mahesh8d19d7d2016-05-19 15:03:01 -07003277 down_scale_amount = skl_plane_downscale_amount(intel_pstate);
3278
3279 return (uint64_t)data_rate * down_scale_amount >> 16;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003280}
3281
3282/*
3283 * We don't overflow 32 bits. Worst case is 3 planes enabled, each fetching
3284 * a 8192x4096@32bpp framebuffer:
3285 * 3 * 4096 * 8192 * 4 < 2^32
3286 */
3287static unsigned int
Maarten Lankhorst1e6ee542016-10-26 15:41:32 +02003288skl_get_total_relative_data_rate(struct intel_crtc_state *intel_cstate,
3289 unsigned *plane_data_rate,
3290 unsigned *plane_y_data_rate)
Damien Lespiaub9cec072014-11-04 17:06:43 +00003291{
Matt Roper9c74d822016-05-12 07:05:58 -07003292 struct drm_crtc_state *cstate = &intel_cstate->base;
3293 struct drm_atomic_state *state = cstate->state;
Maarten Lankhorstc8fe32c2016-10-26 15:41:29 +02003294 struct drm_plane *plane;
Matt Roper024c9042015-09-24 15:53:11 -07003295 const struct intel_plane *intel_plane;
Maarten Lankhorstc8fe32c2016-10-26 15:41:29 +02003296 const struct drm_plane_state *pstate;
Matt Ropera1de91e2016-05-12 07:05:57 -07003297 unsigned int rate, total_data_rate = 0;
Matt Roper9c74d822016-05-12 07:05:58 -07003298 int id;
Matt Ropera6d3460e2016-05-12 07:06:04 -07003299
3300 if (WARN_ON(!state))
3301 return 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003302
Matt Ropera1de91e2016-05-12 07:05:57 -07003303 /* Calculate and cache data rate for each plane */
Maarten Lankhorstc8fe32c2016-10-26 15:41:29 +02003304 drm_atomic_crtc_state_for_each_plane_state(plane, pstate, cstate) {
Matt Ropera6d3460e2016-05-12 07:06:04 -07003305 id = skl_wm_plane_id(to_intel_plane(plane));
3306 intel_plane = to_intel_plane(plane);
Matt Roper024c9042015-09-24 15:53:11 -07003307
Matt Ropera6d3460e2016-05-12 07:06:04 -07003308 /* packed/uv */
3309 rate = skl_plane_relative_data_rate(intel_cstate,
3310 pstate, 0);
Maarten Lankhorst1e6ee542016-10-26 15:41:32 +02003311 plane_data_rate[id] = rate;
3312
3313 total_data_rate += rate;
Matt Roper9c74d822016-05-12 07:05:58 -07003314
Matt Ropera6d3460e2016-05-12 07:06:04 -07003315 /* y-plane */
3316 rate = skl_plane_relative_data_rate(intel_cstate,
3317 pstate, 1);
Maarten Lankhorst1e6ee542016-10-26 15:41:32 +02003318 plane_y_data_rate[id] = rate;
Matt Ropera1de91e2016-05-12 07:05:57 -07003319
Maarten Lankhorst1e6ee542016-10-26 15:41:32 +02003320 total_data_rate += rate;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003321 }
3322
3323 return total_data_rate;
3324}
3325
Kumar, Maheshcbcfd142016-05-31 09:58:59 -07003326static uint16_t
3327skl_ddb_min_alloc(const struct drm_plane_state *pstate,
3328 const int y)
3329{
3330 struct drm_framebuffer *fb = pstate->fb;
3331 struct intel_plane_state *intel_pstate = to_intel_plane_state(pstate);
3332 uint32_t src_w, src_h;
3333 uint32_t min_scanlines = 8;
3334 uint8_t plane_bpp;
3335
3336 if (WARN_ON(!fb))
3337 return 0;
3338
3339 /* For packed formats, no y-plane, return 0 */
3340 if (y && fb->pixel_format != DRM_FORMAT_NV12)
3341 return 0;
3342
3343 /* For Non Y-tile return 8-blocks */
3344 if (fb->modifier[0] != I915_FORMAT_MOD_Y_TILED &&
3345 fb->modifier[0] != I915_FORMAT_MOD_Yf_TILED)
3346 return 8;
3347
Ville Syrjälä936e71e2016-07-26 19:06:59 +03003348 src_w = drm_rect_width(&intel_pstate->base.src) >> 16;
3349 src_h = drm_rect_height(&intel_pstate->base.src) >> 16;
Kumar, Maheshcbcfd142016-05-31 09:58:59 -07003350
Ville Syrjäläbd2ef252016-09-26 19:30:46 +03003351 if (drm_rotation_90_or_270(pstate->rotation))
Kumar, Maheshcbcfd142016-05-31 09:58:59 -07003352 swap(src_w, src_h);
3353
3354 /* Halve UV plane width and height for NV12 */
3355 if (fb->pixel_format == DRM_FORMAT_NV12 && !y) {
3356 src_w /= 2;
3357 src_h /= 2;
3358 }
3359
3360 if (fb->pixel_format == DRM_FORMAT_NV12 && !y)
3361 plane_bpp = drm_format_plane_cpp(fb->pixel_format, 1);
3362 else
3363 plane_bpp = drm_format_plane_cpp(fb->pixel_format, 0);
3364
Ville Syrjäläbd2ef252016-09-26 19:30:46 +03003365 if (drm_rotation_90_or_270(pstate->rotation)) {
Kumar, Maheshcbcfd142016-05-31 09:58:59 -07003366 switch (plane_bpp) {
3367 case 1:
3368 min_scanlines = 32;
3369 break;
3370 case 2:
3371 min_scanlines = 16;
3372 break;
3373 case 4:
3374 min_scanlines = 8;
3375 break;
3376 case 8:
3377 min_scanlines = 4;
3378 break;
3379 default:
3380 WARN(1, "Unsupported pixel depth %u for rotation",
3381 plane_bpp);
3382 min_scanlines = 32;
3383 }
3384 }
3385
3386 return DIV_ROUND_UP((4 * src_w * plane_bpp), 512) * min_scanlines/4 + 3;
3387}
3388
Maarten Lankhorst49845a72016-10-26 15:41:34 +02003389static void
3390skl_ddb_calc_min(const struct intel_crtc_state *cstate, int num_active,
3391 uint16_t *minimum, uint16_t *y_minimum)
3392{
3393 const struct drm_plane_state *pstate;
3394 struct drm_plane *plane;
3395
3396 drm_atomic_crtc_state_for_each_plane_state(plane, pstate, &cstate->base) {
3397 struct intel_plane *intel_plane = to_intel_plane(plane);
3398 int id = skl_wm_plane_id(intel_plane);
3399
3400 if (id == PLANE_CURSOR)
3401 continue;
3402
3403 if (!pstate->visible)
3404 continue;
3405
3406 minimum[id] = skl_ddb_min_alloc(pstate, 0);
3407 y_minimum[id] = skl_ddb_min_alloc(pstate, 1);
3408 }
3409
3410 minimum[PLANE_CURSOR] = skl_cursor_allocation(num_active);
3411}
3412
Matt Roperc107acf2016-05-12 07:06:01 -07003413static int
Matt Roper024c9042015-09-24 15:53:11 -07003414skl_allocate_pipe_ddb(struct intel_crtc_state *cstate,
Damien Lespiaub9cec072014-11-04 17:06:43 +00003415 struct skl_ddb_allocation *ddb /* out */)
3416{
Matt Roperc107acf2016-05-12 07:06:01 -07003417 struct drm_atomic_state *state = cstate->base.state;
Matt Roper024c9042015-09-24 15:53:11 -07003418 struct drm_crtc *crtc = cstate->base.crtc;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003419 struct drm_device *dev = crtc->dev;
3420 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3421 enum pipe pipe = intel_crtc->pipe;
Lyudece0ba282016-09-15 10:46:35 -04003422 struct skl_ddb_entry *alloc = &cstate->wm.skl.ddb;
Maarten Lankhorst49845a72016-10-26 15:41:34 +02003423 uint16_t alloc_size, start;
Maarten Lankhorstfefdd812016-10-26 15:41:33 +02003424 uint16_t minimum[I915_MAX_PLANES] = {};
3425 uint16_t y_minimum[I915_MAX_PLANES] = {};
Damien Lespiaub9cec072014-11-04 17:06:43 +00003426 unsigned int total_data_rate;
Matt Roperc107acf2016-05-12 07:06:01 -07003427 int num_active;
3428 int id, i;
Maarten Lankhorst1e6ee542016-10-26 15:41:32 +02003429 unsigned plane_data_rate[I915_MAX_PLANES] = {};
3430 unsigned plane_y_data_rate[I915_MAX_PLANES] = {};
Damien Lespiaub9cec072014-11-04 17:06:43 +00003431
Paulo Zanoni5a920b82016-10-04 14:37:32 -03003432 /* Clear the partitioning for disabled planes. */
3433 memset(ddb->plane[pipe], 0, sizeof(ddb->plane[pipe]));
3434 memset(ddb->y_plane[pipe], 0, sizeof(ddb->y_plane[pipe]));
3435
Matt Ropera6d3460e2016-05-12 07:06:04 -07003436 if (WARN_ON(!state))
3437 return 0;
3438
Matt Roperc107acf2016-05-12 07:06:01 -07003439 if (!cstate->base.active) {
Lyudece0ba282016-09-15 10:46:35 -04003440 alloc->start = alloc->end = 0;
Matt Roperc107acf2016-05-12 07:06:01 -07003441 return 0;
3442 }
3443
Matt Ropera6d3460e2016-05-12 07:06:04 -07003444 skl_ddb_get_pipe_allocation_limits(dev, cstate, alloc, &num_active);
Damien Lespiau34bb56a2014-11-04 17:07:01 +00003445 alloc_size = skl_ddb_entry_size(alloc);
Damien Lespiaub9cec072014-11-04 17:06:43 +00003446 if (alloc_size == 0) {
3447 memset(ddb->plane[pipe], 0, sizeof(ddb->plane[pipe]));
Matt Roperc107acf2016-05-12 07:06:01 -07003448 return 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003449 }
3450
Maarten Lankhorst49845a72016-10-26 15:41:34 +02003451 skl_ddb_calc_min(cstate, num_active, minimum, y_minimum);
Damien Lespiaub9cec072014-11-04 17:06:43 +00003452
Maarten Lankhorst49845a72016-10-26 15:41:34 +02003453 /*
3454 * 1. Allocate the mininum required blocks for each active plane
3455 * and allocate the cursor, it doesn't require extra allocation
3456 * proportional to the data rate.
3457 */
Damien Lespiaub9cec072014-11-04 17:06:43 +00003458
Maarten Lankhorst49845a72016-10-26 15:41:34 +02003459 for (i = 0; i < I915_MAX_PLANES; i++) {
Matt Roperc107acf2016-05-12 07:06:01 -07003460 alloc_size -= minimum[i];
3461 alloc_size -= y_minimum[i];
Damien Lespiau80958152015-02-09 13:35:10 +00003462 }
3463
Maarten Lankhorst49845a72016-10-26 15:41:34 +02003464 ddb->plane[pipe][PLANE_CURSOR].start = alloc->end - minimum[PLANE_CURSOR];
3465 ddb->plane[pipe][PLANE_CURSOR].end = alloc->end;
3466
Damien Lespiaub9cec072014-11-04 17:06:43 +00003467 /*
Damien Lespiau80958152015-02-09 13:35:10 +00003468 * 2. Distribute the remaining space in proportion to the amount of
3469 * data each plane needs to fetch from memory.
Damien Lespiaub9cec072014-11-04 17:06:43 +00003470 *
3471 * FIXME: we may not allocate every single block here.
3472 */
Maarten Lankhorst1e6ee542016-10-26 15:41:32 +02003473 total_data_rate = skl_get_total_relative_data_rate(cstate,
3474 plane_data_rate,
3475 plane_y_data_rate);
Matt Ropera1de91e2016-05-12 07:05:57 -07003476 if (total_data_rate == 0)
Matt Roperc107acf2016-05-12 07:06:01 -07003477 return 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003478
Damien Lespiau34bb56a2014-11-04 17:07:01 +00003479 start = alloc->start;
Maarten Lankhorst1e6ee542016-10-26 15:41:32 +02003480 for (id = 0; id < I915_MAX_PLANES; id++) {
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003481 unsigned int data_rate, y_data_rate;
3482 uint16_t plane_blocks, y_plane_blocks = 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003483
Maarten Lankhorst49845a72016-10-26 15:41:34 +02003484 if (id == PLANE_CURSOR)
3485 continue;
3486
Maarten Lankhorst1e6ee542016-10-26 15:41:32 +02003487 data_rate = plane_data_rate[id];
Damien Lespiaub9cec072014-11-04 17:06:43 +00003488
3489 /*
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003490 * allocation for (packed formats) or (uv-plane part of planar format):
Damien Lespiaub9cec072014-11-04 17:06:43 +00003491 * promote the expression to 64 bits to avoid overflowing, the
3492 * result is < available as data_rate / total_data_rate < 1
3493 */
Matt Roper024c9042015-09-24 15:53:11 -07003494 plane_blocks = minimum[id];
Damien Lespiau80958152015-02-09 13:35:10 +00003495 plane_blocks += div_u64((uint64_t)alloc_size * data_rate,
3496 total_data_rate);
Damien Lespiaub9cec072014-11-04 17:06:43 +00003497
Matt Roperc107acf2016-05-12 07:06:01 -07003498 /* Leave disabled planes at (0,0) */
3499 if (data_rate) {
3500 ddb->plane[pipe][id].start = start;
3501 ddb->plane[pipe][id].end = start + plane_blocks;
3502 }
Damien Lespiaub9cec072014-11-04 17:06:43 +00003503
3504 start += plane_blocks;
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003505
3506 /*
3507 * allocation for y_plane part of planar format:
3508 */
Maarten Lankhorst1e6ee542016-10-26 15:41:32 +02003509 y_data_rate = plane_y_data_rate[id];
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003510
Matt Ropera1de91e2016-05-12 07:05:57 -07003511 y_plane_blocks = y_minimum[id];
3512 y_plane_blocks += div_u64((uint64_t)alloc_size * y_data_rate,
3513 total_data_rate);
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003514
Matt Roperc107acf2016-05-12 07:06:01 -07003515 if (y_data_rate) {
3516 ddb->y_plane[pipe][id].start = start;
3517 ddb->y_plane[pipe][id].end = start + y_plane_blocks;
3518 }
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003519
Matt Ropera1de91e2016-05-12 07:05:57 -07003520 start += y_plane_blocks;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003521 }
3522
Matt Roperc107acf2016-05-12 07:06:01 -07003523 return 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003524}
3525
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003526/*
3527 * 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 +02003528 * for the read latency) and cpp should always be <= 8, so that
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003529 * should allow pixel_rate up to ~2 GHz which seems sufficient since max
3530 * 2xcdclk is 1350 MHz and the pixel rate should never exceed that.
3531*/
Ville Syrjäläac484962016-01-20 21:05:26 +02003532static uint32_t skl_wm_method1(uint32_t pixel_rate, uint8_t cpp, uint32_t latency)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003533{
3534 uint32_t wm_intermediate_val, ret;
3535
3536 if (latency == 0)
3537 return UINT_MAX;
3538
Ville Syrjäläac484962016-01-20 21:05:26 +02003539 wm_intermediate_val = latency * pixel_rate * cpp / 512;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003540 ret = DIV_ROUND_UP(wm_intermediate_val, 1000);
3541
3542 return ret;
3543}
3544
3545static uint32_t skl_wm_method2(uint32_t pixel_rate, uint32_t pipe_htotal,
Paulo Zanoni7a1a8ae2016-09-22 18:00:32 -03003546 uint32_t latency, uint32_t plane_blocks_per_line)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003547{
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00003548 uint32_t ret;
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00003549 uint32_t wm_intermediate_val;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003550
3551 if (latency == 0)
3552 return UINT_MAX;
3553
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003554 wm_intermediate_val = latency * pixel_rate;
3555 ret = DIV_ROUND_UP(wm_intermediate_val, pipe_htotal * 1000) *
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00003556 plane_blocks_per_line;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003557
3558 return ret;
3559}
3560
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07003561static uint32_t skl_adjusted_plane_pixel_rate(const struct intel_crtc_state *cstate,
3562 struct intel_plane_state *pstate)
3563{
3564 uint64_t adjusted_pixel_rate;
3565 uint64_t downscale_amount;
3566 uint64_t pixel_rate;
3567
3568 /* Shouldn't reach here on disabled planes... */
Ville Syrjälä936e71e2016-07-26 19:06:59 +03003569 if (WARN_ON(!pstate->base.visible))
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07003570 return 0;
3571
3572 /*
3573 * Adjusted plane pixel rate is just the pipe's adjusted pixel rate
3574 * with additional adjustments for plane-specific scaling.
3575 */
Paulo Zanonicfd7e3a2016-10-07 17:28:57 -03003576 adjusted_pixel_rate = ilk_pipe_pixel_rate(cstate);
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07003577 downscale_amount = skl_plane_downscale_amount(pstate);
3578
3579 pixel_rate = adjusted_pixel_rate * downscale_amount >> 16;
3580 WARN_ON(pixel_rate != clamp_t(uint32_t, pixel_rate, 0, ~0));
3581
3582 return pixel_rate;
3583}
3584
Matt Roper55994c22016-05-12 07:06:08 -07003585static int skl_compute_plane_wm(const struct drm_i915_private *dev_priv,
3586 struct intel_crtc_state *cstate,
3587 struct intel_plane_state *intel_pstate,
3588 uint16_t ddb_allocation,
3589 int level,
3590 uint16_t *out_blocks, /* out */
3591 uint8_t *out_lines, /* out */
3592 bool *enabled /* out */)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003593{
Matt Roper33815fa2016-05-12 07:06:05 -07003594 struct drm_plane_state *pstate = &intel_pstate->base;
3595 struct drm_framebuffer *fb = pstate->fb;
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00003596 uint32_t latency = dev_priv->wm.skl_latency[level];
3597 uint32_t method1, method2;
3598 uint32_t plane_bytes_per_line, plane_blocks_per_line;
3599 uint32_t res_blocks, res_lines;
3600 uint32_t selected_result;
Ville Syrjäläac484962016-01-20 21:05:26 +02003601 uint8_t cpp;
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07003602 uint32_t width = 0, height = 0;
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07003603 uint32_t plane_pixel_rate;
Paulo Zanoni75676ed2016-09-22 18:00:33 -03003604 uint32_t y_tile_minimum, y_min_scanlines;
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003605 struct intel_atomic_state *state =
3606 to_intel_atomic_state(cstate->base.state);
3607 bool apply_memory_bw_wa = skl_needs_memory_bw_wa(state);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003608
Ville Syrjälä936e71e2016-07-26 19:06:59 +03003609 if (latency == 0 || !cstate->base.active || !intel_pstate->base.visible) {
Matt Roper55994c22016-05-12 07:06:08 -07003610 *enabled = false;
3611 return 0;
3612 }
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003613
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003614 if (apply_memory_bw_wa && fb->modifier[0] == I915_FORMAT_MOD_X_TILED)
3615 latency += 15;
3616
Ville Syrjälä936e71e2016-07-26 19:06:59 +03003617 width = drm_rect_width(&intel_pstate->base.src) >> 16;
3618 height = drm_rect_height(&intel_pstate->base.src) >> 16;
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07003619
Ville Syrjäläbd2ef252016-09-26 19:30:46 +03003620 if (drm_rotation_90_or_270(pstate->rotation))
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07003621 swap(width, height);
3622
Ville Syrjäläac484962016-01-20 21:05:26 +02003623 cpp = drm_format_plane_cpp(fb->pixel_format, 0);
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07003624 plane_pixel_rate = skl_adjusted_plane_pixel_rate(cstate, intel_pstate);
3625
Dave Airlie61d0a042016-10-25 16:35:20 +10003626 if (drm_rotation_90_or_270(pstate->rotation)) {
Paulo Zanoni1186fa82016-09-22 18:00:31 -03003627 int cpp = (fb->pixel_format == DRM_FORMAT_NV12) ?
3628 drm_format_plane_cpp(fb->pixel_format, 1) :
3629 drm_format_plane_cpp(fb->pixel_format, 0);
3630
3631 switch (cpp) {
3632 case 1:
3633 y_min_scanlines = 16;
3634 break;
3635 case 2:
3636 y_min_scanlines = 8;
3637 break;
Paulo Zanoni1186fa82016-09-22 18:00:31 -03003638 case 4:
3639 y_min_scanlines = 4;
3640 break;
Paulo Zanoni86a462b2016-09-22 18:00:35 -03003641 default:
3642 MISSING_CASE(cpp);
3643 return -EINVAL;
Paulo Zanoni1186fa82016-09-22 18:00:31 -03003644 }
3645 } else {
3646 y_min_scanlines = 4;
3647 }
3648
Paulo Zanoni7a1a8ae2016-09-22 18:00:32 -03003649 plane_bytes_per_line = width * cpp;
3650 if (fb->modifier[0] == I915_FORMAT_MOD_Y_TILED ||
3651 fb->modifier[0] == I915_FORMAT_MOD_Yf_TILED) {
3652 plane_blocks_per_line =
3653 DIV_ROUND_UP(plane_bytes_per_line * y_min_scanlines, 512);
3654 plane_blocks_per_line /= y_min_scanlines;
3655 } else if (fb->modifier[0] == DRM_FORMAT_MOD_NONE) {
3656 plane_blocks_per_line = DIV_ROUND_UP(plane_bytes_per_line, 512)
3657 + 1;
3658 } else {
3659 plane_blocks_per_line = DIV_ROUND_UP(plane_bytes_per_line, 512);
3660 }
3661
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07003662 method1 = skl_wm_method1(plane_pixel_rate, cpp, latency);
3663 method2 = skl_wm_method2(plane_pixel_rate,
Matt Roper024c9042015-09-24 15:53:11 -07003664 cstate->base.adjusted_mode.crtc_htotal,
Paulo Zanoni1186fa82016-09-22 18:00:31 -03003665 latency,
Paulo Zanoni7a1a8ae2016-09-22 18:00:32 -03003666 plane_blocks_per_line);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003667
Paulo Zanoni75676ed2016-09-22 18:00:33 -03003668 y_tile_minimum = plane_blocks_per_line * y_min_scanlines;
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003669 if (apply_memory_bw_wa)
3670 y_tile_minimum *= 2;
Paulo Zanoni75676ed2016-09-22 18:00:33 -03003671
Matt Roper024c9042015-09-24 15:53:11 -07003672 if (fb->modifier[0] == I915_FORMAT_MOD_Y_TILED ||
3673 fb->modifier[0] == I915_FORMAT_MOD_Yf_TILED) {
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00003674 selected_result = max(method2, y_tile_minimum);
3675 } else {
Paulo Zanonif1db3ea2016-09-22 18:00:34 -03003676 if ((cpp * cstate->base.adjusted_mode.crtc_htotal / 512 < 1) &&
3677 (plane_bytes_per_line / 512 < 1))
3678 selected_result = method2;
3679 else if ((ddb_allocation / plane_blocks_per_line) >= 1)
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00003680 selected_result = min(method1, method2);
3681 else
3682 selected_result = method1;
3683 }
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003684
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00003685 res_blocks = selected_result + 1;
3686 res_lines = DIV_ROUND_UP(selected_result, plane_blocks_per_line);
Damien Lespiaue6d66172014-11-04 17:06:55 +00003687
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00003688 if (level >= 1 && level <= 7) {
Matt Roper024c9042015-09-24 15:53:11 -07003689 if (fb->modifier[0] == I915_FORMAT_MOD_Y_TILED ||
Paulo Zanoni75676ed2016-09-22 18:00:33 -03003690 fb->modifier[0] == I915_FORMAT_MOD_Yf_TILED) {
3691 res_blocks += y_tile_minimum;
Paulo Zanoni1186fa82016-09-22 18:00:31 -03003692 res_lines += y_min_scanlines;
Paulo Zanoni75676ed2016-09-22 18:00:33 -03003693 } else {
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00003694 res_blocks++;
Paulo Zanoni75676ed2016-09-22 18:00:33 -03003695 }
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00003696 }
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00003697
Matt Roper55994c22016-05-12 07:06:08 -07003698 if (res_blocks >= ddb_allocation || res_lines > 31) {
3699 *enabled = false;
Matt Roper6b6bada2016-05-12 07:06:10 -07003700
3701 /*
3702 * If there are no valid level 0 watermarks, then we can't
3703 * support this display configuration.
3704 */
3705 if (level) {
3706 return 0;
3707 } else {
3708 DRM_DEBUG_KMS("Requested display configuration exceeds system watermark limitations\n");
3709 DRM_DEBUG_KMS("Plane %d.%d: blocks required = %u/%u, lines required = %u/31\n",
3710 to_intel_crtc(cstate->base.crtc)->pipe,
3711 skl_wm_plane_id(to_intel_plane(pstate->plane)),
3712 res_blocks, ddb_allocation, res_lines);
3713
3714 return -EINVAL;
3715 }
Matt Roper55994c22016-05-12 07:06:08 -07003716 }
Damien Lespiaue6d66172014-11-04 17:06:55 +00003717
3718 *out_blocks = res_blocks;
3719 *out_lines = res_lines;
Matt Roper55994c22016-05-12 07:06:08 -07003720 *enabled = true;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003721
Matt Roper55994c22016-05-12 07:06:08 -07003722 return 0;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003723}
3724
Matt Roperf4a96752016-05-12 07:06:06 -07003725static int
3726skl_compute_wm_level(const struct drm_i915_private *dev_priv,
3727 struct skl_ddb_allocation *ddb,
3728 struct intel_crtc_state *cstate,
Lyudea62163e2016-10-04 14:28:20 -04003729 struct intel_plane *intel_plane,
Matt Roperf4a96752016-05-12 07:06:06 -07003730 int level,
3731 struct skl_wm_level *result)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003732{
Matt Roperf4a96752016-05-12 07:06:06 -07003733 struct drm_atomic_state *state = cstate->base.state;
Matt Roper024c9042015-09-24 15:53:11 -07003734 struct intel_crtc *intel_crtc = to_intel_crtc(cstate->base.crtc);
Lyudea62163e2016-10-04 14:28:20 -04003735 struct drm_plane *plane = &intel_plane->base;
3736 struct intel_plane_state *intel_pstate = NULL;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003737 uint16_t ddb_blocks;
Matt Roper024c9042015-09-24 15:53:11 -07003738 enum pipe pipe = intel_crtc->pipe;
Matt Roper55994c22016-05-12 07:06:08 -07003739 int ret;
Lyudea62163e2016-10-04 14:28:20 -04003740 int i = skl_wm_plane_id(intel_plane);
3741
3742 if (state)
3743 intel_pstate =
3744 intel_atomic_get_existing_plane_state(state,
3745 intel_plane);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003746
Matt Roperf4a96752016-05-12 07:06:06 -07003747 /*
Lyudea62163e2016-10-04 14:28:20 -04003748 * Note: If we start supporting multiple pending atomic commits against
3749 * the same planes/CRTC's in the future, plane->state will no longer be
3750 * the correct pre-state to use for the calculations here and we'll
3751 * need to change where we get the 'unchanged' plane data from.
3752 *
3753 * For now this is fine because we only allow one queued commit against
3754 * a CRTC. Even if the plane isn't modified by this transaction and we
3755 * don't have a plane lock, we still have the CRTC's lock, so we know
3756 * that no other transactions are racing with us to update it.
Matt Roperf4a96752016-05-12 07:06:06 -07003757 */
Lyudea62163e2016-10-04 14:28:20 -04003758 if (!intel_pstate)
3759 intel_pstate = to_intel_plane_state(plane->state);
Matt Roperf4a96752016-05-12 07:06:06 -07003760
Lyudea62163e2016-10-04 14:28:20 -04003761 WARN_ON(!intel_pstate->base.fb);
Matt Roper024c9042015-09-24 15:53:11 -07003762
Lyudea62163e2016-10-04 14:28:20 -04003763 ddb_blocks = skl_ddb_entry_size(&ddb->plane[pipe][i]);
Matt Roperf4a96752016-05-12 07:06:06 -07003764
Lyudea62163e2016-10-04 14:28:20 -04003765 ret = skl_compute_plane_wm(dev_priv,
3766 cstate,
3767 intel_pstate,
3768 ddb_blocks,
3769 level,
3770 &result->plane_res_b,
3771 &result->plane_res_l,
3772 &result->plane_en);
3773 if (ret)
3774 return ret;
Matt Roperf4a96752016-05-12 07:06:06 -07003775
3776 return 0;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003777}
3778
Damien Lespiau407b50f2014-11-04 17:06:57 +00003779static uint32_t
Matt Roper024c9042015-09-24 15:53:11 -07003780skl_compute_linetime_wm(struct intel_crtc_state *cstate)
Damien Lespiau407b50f2014-11-04 17:06:57 +00003781{
Paulo Zanoni30d1b5f2016-10-07 17:28:58 -03003782 uint32_t pixel_rate;
3783
Matt Roper024c9042015-09-24 15:53:11 -07003784 if (!cstate->base.active)
Damien Lespiau407b50f2014-11-04 17:06:57 +00003785 return 0;
3786
Paulo Zanoni30d1b5f2016-10-07 17:28:58 -03003787 pixel_rate = ilk_pipe_pixel_rate(cstate);
3788
3789 if (WARN_ON(pixel_rate == 0))
Mika Kuoppala661abfc2015-07-16 19:36:51 +03003790 return 0;
Damien Lespiau407b50f2014-11-04 17:06:57 +00003791
Matt Roper024c9042015-09-24 15:53:11 -07003792 return DIV_ROUND_UP(8 * cstate->base.adjusted_mode.crtc_htotal * 1000,
Paulo Zanoni30d1b5f2016-10-07 17:28:58 -03003793 pixel_rate);
Damien Lespiau407b50f2014-11-04 17:06:57 +00003794}
3795
Matt Roper024c9042015-09-24 15:53:11 -07003796static void skl_compute_transition_wm(struct intel_crtc_state *cstate,
Damien Lespiau9414f562014-11-04 17:06:58 +00003797 struct skl_wm_level *trans_wm /* out */)
Damien Lespiau407b50f2014-11-04 17:06:57 +00003798{
Matt Roper024c9042015-09-24 15:53:11 -07003799 if (!cstate->base.active)
Damien Lespiau407b50f2014-11-04 17:06:57 +00003800 return;
Damien Lespiau9414f562014-11-04 17:06:58 +00003801
3802 /* Until we know more, just disable transition WMs */
Lyudea62163e2016-10-04 14:28:20 -04003803 trans_wm->plane_en = false;
Damien Lespiau407b50f2014-11-04 17:06:57 +00003804}
3805
Matt Roper55994c22016-05-12 07:06:08 -07003806static int skl_build_pipe_wm(struct intel_crtc_state *cstate,
3807 struct skl_ddb_allocation *ddb,
3808 struct skl_pipe_wm *pipe_wm)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003809{
Matt Roper024c9042015-09-24 15:53:11 -07003810 struct drm_device *dev = cstate->base.crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003811 const struct drm_i915_private *dev_priv = to_i915(dev);
Lyudea62163e2016-10-04 14:28:20 -04003812 struct intel_plane *intel_plane;
3813 struct skl_plane_wm *wm;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003814 int level, max_level = ilk_wm_max_level(dev_priv);
Matt Roper55994c22016-05-12 07:06:08 -07003815 int ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003816
Lyudea62163e2016-10-04 14:28:20 -04003817 /*
3818 * We'll only calculate watermarks for planes that are actually
3819 * enabled, so make sure all other planes are set as disabled.
3820 */
3821 memset(pipe_wm->planes, 0, sizeof(pipe_wm->planes));
3822
3823 for_each_intel_plane_mask(&dev_priv->drm,
3824 intel_plane,
3825 cstate->base.plane_mask) {
3826 wm = &pipe_wm->planes[skl_wm_plane_id(intel_plane)];
3827
3828 for (level = 0; level <= max_level; level++) {
3829 ret = skl_compute_wm_level(dev_priv, ddb, cstate,
3830 intel_plane, level,
3831 &wm->wm[level]);
3832 if (ret)
3833 return ret;
3834 }
3835 skl_compute_transition_wm(cstate, &wm->trans_wm);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003836 }
Matt Roper024c9042015-09-24 15:53:11 -07003837 pipe_wm->linetime = skl_compute_linetime_wm(cstate);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003838
Matt Roper55994c22016-05-12 07:06:08 -07003839 return 0;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003840}
3841
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003842static void skl_ddb_entry_write(struct drm_i915_private *dev_priv,
3843 i915_reg_t reg,
Damien Lespiau16160e32014-11-04 17:06:53 +00003844 const struct skl_ddb_entry *entry)
3845{
3846 if (entry->end)
3847 I915_WRITE(reg, (entry->end - 1) << 16 | entry->start);
3848 else
3849 I915_WRITE(reg, 0);
3850}
3851
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003852static void skl_write_wm_level(struct drm_i915_private *dev_priv,
3853 i915_reg_t reg,
3854 const struct skl_wm_level *level)
3855{
3856 uint32_t val = 0;
3857
3858 if (level->plane_en) {
3859 val |= PLANE_WM_EN;
3860 val |= level->plane_res_b;
3861 val |= level->plane_res_l << PLANE_WM_LINES_SHIFT;
3862 }
3863
3864 I915_WRITE(reg, val);
3865}
3866
Lyude62e0fb82016-08-22 12:50:08 -04003867void skl_write_plane_wm(struct intel_crtc *intel_crtc,
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003868 const struct skl_plane_wm *wm,
3869 const struct skl_ddb_allocation *ddb,
Lyude62e0fb82016-08-22 12:50:08 -04003870 int plane)
3871{
3872 struct drm_crtc *crtc = &intel_crtc->base;
3873 struct drm_device *dev = crtc->dev;
3874 struct drm_i915_private *dev_priv = to_i915(dev);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003875 int level, max_level = ilk_wm_max_level(dev_priv);
Lyude62e0fb82016-08-22 12:50:08 -04003876 enum pipe pipe = intel_crtc->pipe;
3877
3878 for (level = 0; level <= max_level; level++) {
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003879 skl_write_wm_level(dev_priv, PLANE_WM(pipe, plane, level),
3880 &wm->wm[level]);
Lyude62e0fb82016-08-22 12:50:08 -04003881 }
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003882 skl_write_wm_level(dev_priv, PLANE_WM_TRANS(pipe, plane),
3883 &wm->trans_wm);
Lyude27082492016-08-24 07:48:10 +02003884
3885 skl_ddb_entry_write(dev_priv, PLANE_BUF_CFG(pipe, plane),
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003886 &ddb->plane[pipe][plane]);
Lyude27082492016-08-24 07:48:10 +02003887 skl_ddb_entry_write(dev_priv, PLANE_NV12_BUF_CFG(pipe, plane),
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003888 &ddb->y_plane[pipe][plane]);
Lyude62e0fb82016-08-22 12:50:08 -04003889}
3890
3891void skl_write_cursor_wm(struct intel_crtc *intel_crtc,
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003892 const struct skl_plane_wm *wm,
3893 const struct skl_ddb_allocation *ddb)
Lyude62e0fb82016-08-22 12:50:08 -04003894{
3895 struct drm_crtc *crtc = &intel_crtc->base;
3896 struct drm_device *dev = crtc->dev;
3897 struct drm_i915_private *dev_priv = to_i915(dev);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003898 int level, max_level = ilk_wm_max_level(dev_priv);
Lyude62e0fb82016-08-22 12:50:08 -04003899 enum pipe pipe = intel_crtc->pipe;
3900
3901 for (level = 0; level <= max_level; level++) {
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003902 skl_write_wm_level(dev_priv, CUR_WM(pipe, level),
3903 &wm->wm[level]);
Lyude62e0fb82016-08-22 12:50:08 -04003904 }
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003905 skl_write_wm_level(dev_priv, CUR_WM_TRANS(pipe), &wm->trans_wm);
Lyude27082492016-08-24 07:48:10 +02003906
3907 skl_ddb_entry_write(dev_priv, CUR_BUF_CFG(pipe),
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003908 &ddb->plane[pipe][PLANE_CURSOR]);
Lyude62e0fb82016-08-22 12:50:08 -04003909}
3910
cpaul@redhat.com45ece232016-10-14 17:31:56 -04003911bool skl_wm_level_equals(const struct skl_wm_level *l1,
3912 const struct skl_wm_level *l2)
3913{
3914 if (l1->plane_en != l2->plane_en)
3915 return false;
3916
3917 /* If both planes aren't enabled, the rest shouldn't matter */
3918 if (!l1->plane_en)
3919 return true;
3920
3921 return (l1->plane_res_l == l2->plane_res_l &&
3922 l1->plane_res_b == l2->plane_res_b);
3923}
3924
Lyude27082492016-08-24 07:48:10 +02003925static inline bool skl_ddb_entries_overlap(const struct skl_ddb_entry *a,
3926 const struct skl_ddb_entry *b)
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003927{
Lyude27082492016-08-24 07:48:10 +02003928 return a->start < b->end && b->start < a->end;
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003929}
3930
Lyude27082492016-08-24 07:48:10 +02003931bool skl_ddb_allocation_overlaps(struct drm_atomic_state *state,
Lyudece0ba282016-09-15 10:46:35 -04003932 struct intel_crtc *intel_crtc)
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003933{
Lyudece0ba282016-09-15 10:46:35 -04003934 struct drm_crtc *other_crtc;
3935 struct drm_crtc_state *other_cstate;
3936 struct intel_crtc *other_intel_crtc;
3937 const struct skl_ddb_entry *ddb =
3938 &to_intel_crtc_state(intel_crtc->base.state)->wm.skl.ddb;
3939 int i;
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003940
Lyudece0ba282016-09-15 10:46:35 -04003941 for_each_crtc_in_state(state, other_crtc, other_cstate, i) {
3942 other_intel_crtc = to_intel_crtc(other_crtc);
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003943
Lyudece0ba282016-09-15 10:46:35 -04003944 if (other_intel_crtc == intel_crtc)
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003945 continue;
3946
Lyudece0ba282016-09-15 10:46:35 -04003947 if (skl_ddb_entries_overlap(ddb, &other_intel_crtc->hw_ddb))
Lyude27082492016-08-24 07:48:10 +02003948 return true;
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003949 }
3950
Lyude27082492016-08-24 07:48:10 +02003951 return false;
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003952}
3953
Matt Roper55994c22016-05-12 07:06:08 -07003954static int skl_update_pipe_wm(struct drm_crtc_state *cstate,
Maarten Lankhorst03af79e2016-10-26 15:41:36 +02003955 const struct skl_pipe_wm *old_pipe_wm,
Matt Roper55994c22016-05-12 07:06:08 -07003956 struct skl_pipe_wm *pipe_wm, /* out */
Maarten Lankhorst03af79e2016-10-26 15:41:36 +02003957 struct skl_ddb_allocation *ddb, /* out */
Matt Roper55994c22016-05-12 07:06:08 -07003958 bool *changed /* out */)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003959{
Matt Roperf4a96752016-05-12 07:06:06 -07003960 struct intel_crtc_state *intel_cstate = to_intel_crtc_state(cstate);
Matt Roper55994c22016-05-12 07:06:08 -07003961 int ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003962
Matt Roper55994c22016-05-12 07:06:08 -07003963 ret = skl_build_pipe_wm(intel_cstate, ddb, pipe_wm);
3964 if (ret)
3965 return ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003966
Maarten Lankhorst03af79e2016-10-26 15:41:36 +02003967 if (!memcmp(old_pipe_wm, pipe_wm, sizeof(*pipe_wm)))
Matt Roper55994c22016-05-12 07:06:08 -07003968 *changed = false;
3969 else
3970 *changed = true;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003971
Matt Roper55994c22016-05-12 07:06:08 -07003972 return 0;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003973}
3974
Matt Roper9b613022016-06-27 16:42:44 -07003975static uint32_t
3976pipes_modified(struct drm_atomic_state *state)
3977{
3978 struct drm_crtc *crtc;
3979 struct drm_crtc_state *cstate;
3980 uint32_t i, ret = 0;
3981
3982 for_each_crtc_in_state(state, crtc, cstate, i)
3983 ret |= drm_crtc_mask(crtc);
3984
3985 return ret;
3986}
3987
Jani Nikulabb7791b2016-10-04 12:29:17 +03003988static int
Paulo Zanoni7f60e202016-09-29 16:36:48 -03003989skl_ddb_add_affected_planes(struct intel_crtc_state *cstate)
3990{
3991 struct drm_atomic_state *state = cstate->base.state;
3992 struct drm_device *dev = state->dev;
3993 struct drm_crtc *crtc = cstate->base.crtc;
3994 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3995 struct drm_i915_private *dev_priv = to_i915(dev);
3996 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
3997 struct skl_ddb_allocation *new_ddb = &intel_state->wm_results.ddb;
3998 struct skl_ddb_allocation *cur_ddb = &dev_priv->wm.skl_hw.ddb;
3999 struct drm_plane_state *plane_state;
4000 struct drm_plane *plane;
4001 enum pipe pipe = intel_crtc->pipe;
4002 int id;
4003
4004 WARN_ON(!drm_atomic_get_existing_crtc_state(state, crtc));
4005
Maarten Lankhorst220b0962016-10-26 15:41:30 +02004006 drm_for_each_plane_mask(plane, dev, cstate->base.plane_mask) {
Paulo Zanoni7f60e202016-09-29 16:36:48 -03004007 id = skl_wm_plane_id(to_intel_plane(plane));
4008
4009 if (skl_ddb_entry_equal(&cur_ddb->plane[pipe][id],
4010 &new_ddb->plane[pipe][id]) &&
4011 skl_ddb_entry_equal(&cur_ddb->y_plane[pipe][id],
4012 &new_ddb->y_plane[pipe][id]))
4013 continue;
4014
4015 plane_state = drm_atomic_get_plane_state(state, plane);
4016 if (IS_ERR(plane_state))
4017 return PTR_ERR(plane_state);
4018 }
4019
4020 return 0;
4021}
4022
Matt Roper98d39492016-05-12 07:06:03 -07004023static int
4024skl_compute_ddb(struct drm_atomic_state *state)
4025{
4026 struct drm_device *dev = state->dev;
4027 struct drm_i915_private *dev_priv = to_i915(dev);
4028 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
4029 struct intel_crtc *intel_crtc;
Matt Roper734fa012016-05-12 15:11:40 -07004030 struct skl_ddb_allocation *ddb = &intel_state->wm_results.ddb;
Matt Roper9b613022016-06-27 16:42:44 -07004031 uint32_t realloc_pipes = pipes_modified(state);
Matt Roper98d39492016-05-12 07:06:03 -07004032 int ret;
4033
4034 /*
4035 * If this is our first atomic update following hardware readout,
4036 * we can't trust the DDB that the BIOS programmed for us. Let's
4037 * pretend that all pipes switched active status so that we'll
4038 * ensure a full DDB recompute.
4039 */
Matt Roper1b54a882016-06-17 13:42:18 -07004040 if (dev_priv->wm.distrust_bios_wm) {
4041 ret = drm_modeset_lock(&dev->mode_config.connection_mutex,
4042 state->acquire_ctx);
4043 if (ret)
4044 return ret;
4045
Matt Roper98d39492016-05-12 07:06:03 -07004046 intel_state->active_pipe_changes = ~0;
4047
Matt Roper1b54a882016-06-17 13:42:18 -07004048 /*
4049 * We usually only initialize intel_state->active_crtcs if we
4050 * we're doing a modeset; make sure this field is always
4051 * initialized during the sanitization process that happens
4052 * on the first commit too.
4053 */
4054 if (!intel_state->modeset)
4055 intel_state->active_crtcs = dev_priv->active_crtcs;
4056 }
4057
Matt Roper98d39492016-05-12 07:06:03 -07004058 /*
4059 * If the modeset changes which CRTC's are active, we need to
4060 * recompute the DDB allocation for *all* active pipes, even
4061 * those that weren't otherwise being modified in any way by this
4062 * atomic commit. Due to the shrinking of the per-pipe allocations
4063 * when new active CRTC's are added, it's possible for a pipe that
4064 * we were already using and aren't changing at all here to suddenly
4065 * become invalid if its DDB needs exceeds its new allocation.
4066 *
4067 * Note that if we wind up doing a full DDB recompute, we can't let
4068 * any other display updates race with this transaction, so we need
4069 * to grab the lock on *all* CRTC's.
4070 */
Matt Roper734fa012016-05-12 15:11:40 -07004071 if (intel_state->active_pipe_changes) {
Matt Roper98d39492016-05-12 07:06:03 -07004072 realloc_pipes = ~0;
Matt Roper734fa012016-05-12 15:11:40 -07004073 intel_state->wm_results.dirty_pipes = ~0;
4074 }
Matt Roper98d39492016-05-12 07:06:03 -07004075
Paulo Zanoni5a920b82016-10-04 14:37:32 -03004076 /*
4077 * We're not recomputing for the pipes not included in the commit, so
4078 * make sure we start with the current state.
4079 */
4080 memcpy(ddb, &dev_priv->wm.skl_hw.ddb, sizeof(*ddb));
4081
Matt Roper98d39492016-05-12 07:06:03 -07004082 for_each_intel_crtc_mask(dev, intel_crtc, realloc_pipes) {
4083 struct intel_crtc_state *cstate;
4084
4085 cstate = intel_atomic_get_crtc_state(state, intel_crtc);
4086 if (IS_ERR(cstate))
4087 return PTR_ERR(cstate);
4088
Matt Roper734fa012016-05-12 15:11:40 -07004089 ret = skl_allocate_pipe_ddb(cstate, ddb);
Matt Roper98d39492016-05-12 07:06:03 -07004090 if (ret)
4091 return ret;
Lyude05a76d32016-08-17 15:55:57 -04004092
Paulo Zanoni7f60e202016-09-29 16:36:48 -03004093 ret = skl_ddb_add_affected_planes(cstate);
Lyude05a76d32016-08-17 15:55:57 -04004094 if (ret)
4095 return ret;
Matt Roper98d39492016-05-12 07:06:03 -07004096 }
4097
4098 return 0;
4099}
4100
Matt Roper2722efb2016-08-17 15:55:55 -04004101static void
4102skl_copy_wm_for_pipe(struct skl_wm_values *dst,
4103 struct skl_wm_values *src,
4104 enum pipe pipe)
4105{
Matt Roper2722efb2016-08-17 15:55:55 -04004106 memcpy(dst->ddb.y_plane[pipe], src->ddb.y_plane[pipe],
4107 sizeof(dst->ddb.y_plane[pipe]));
4108 memcpy(dst->ddb.plane[pipe], src->ddb.plane[pipe],
4109 sizeof(dst->ddb.plane[pipe]));
4110}
4111
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004112static void
4113skl_print_wm_changes(const struct drm_atomic_state *state)
4114{
4115 const struct drm_device *dev = state->dev;
4116 const struct drm_i915_private *dev_priv = to_i915(dev);
4117 const struct intel_atomic_state *intel_state =
4118 to_intel_atomic_state(state);
4119 const struct drm_crtc *crtc;
4120 const struct drm_crtc_state *cstate;
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004121 const struct intel_plane *intel_plane;
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004122 const struct skl_ddb_allocation *old_ddb = &dev_priv->wm.skl_hw.ddb;
4123 const struct skl_ddb_allocation *new_ddb = &intel_state->wm_results.ddb;
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004124 int id;
Maarten Lankhorst75704982016-11-01 12:04:10 +01004125 int i;
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004126
4127 for_each_crtc_in_state(state, crtc, cstate, i) {
Maarten Lankhorst75704982016-11-01 12:04:10 +01004128 const struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4129 enum pipe pipe = intel_crtc->pipe;
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004130
Maarten Lankhorst75704982016-11-01 12:04:10 +01004131 for_each_intel_plane_on_crtc(dev, intel_crtc, intel_plane) {
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004132 const struct skl_ddb_entry *old, *new;
4133
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004134 id = skl_wm_plane_id(intel_plane);
4135 old = &old_ddb->plane[pipe][id];
4136 new = &new_ddb->plane[pipe][id];
4137
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004138 if (skl_ddb_entry_equal(old, new))
4139 continue;
4140
Maarten Lankhorst75704982016-11-01 12:04:10 +01004141 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] ddb (%d - %d) -> (%d - %d)\n",
4142 intel_plane->base.base.id,
4143 intel_plane->base.name,
4144 old->start, old->end,
4145 new->start, new->end);
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004146 }
4147 }
4148}
4149
Matt Roper98d39492016-05-12 07:06:03 -07004150static int
4151skl_compute_wm(struct drm_atomic_state *state)
4152{
4153 struct drm_crtc *crtc;
4154 struct drm_crtc_state *cstate;
Matt Roper734fa012016-05-12 15:11:40 -07004155 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
4156 struct skl_wm_values *results = &intel_state->wm_results;
4157 struct skl_pipe_wm *pipe_wm;
Matt Roper98d39492016-05-12 07:06:03 -07004158 bool changed = false;
Matt Roper734fa012016-05-12 15:11:40 -07004159 int ret, i;
Matt Roper98d39492016-05-12 07:06:03 -07004160
4161 /*
4162 * If this transaction isn't actually touching any CRTC's, don't
4163 * bother with watermark calculation. Note that if we pass this
4164 * test, we're guaranteed to hold at least one CRTC state mutex,
4165 * which means we can safely use values like dev_priv->active_crtcs
4166 * since any racing commits that want to update them would need to
4167 * hold _all_ CRTC state mutexes.
4168 */
4169 for_each_crtc_in_state(state, crtc, cstate, i)
4170 changed = true;
4171 if (!changed)
4172 return 0;
4173
Matt Roper734fa012016-05-12 15:11:40 -07004174 /* Clear all dirty flags */
4175 results->dirty_pipes = 0;
4176
Matt Roper98d39492016-05-12 07:06:03 -07004177 ret = skl_compute_ddb(state);
4178 if (ret)
4179 return ret;
4180
Matt Roper734fa012016-05-12 15:11:40 -07004181 /*
4182 * Calculate WM's for all pipes that are part of this transaction.
4183 * Note that the DDB allocation above may have added more CRTC's that
4184 * weren't otherwise being modified (and set bits in dirty_pipes) if
4185 * pipe allocations had to change.
4186 *
4187 * FIXME: Now that we're doing this in the atomic check phase, we
4188 * should allow skl_update_pipe_wm() to return failure in cases where
4189 * no suitable watermark values can be found.
4190 */
4191 for_each_crtc_in_state(state, crtc, cstate, i) {
Matt Roper734fa012016-05-12 15:11:40 -07004192 struct intel_crtc_state *intel_cstate =
4193 to_intel_crtc_state(cstate);
Maarten Lankhorst03af79e2016-10-26 15:41:36 +02004194 const struct skl_pipe_wm *old_pipe_wm =
4195 &to_intel_crtc_state(crtc->state)->wm.skl.optimal;
Matt Roper734fa012016-05-12 15:11:40 -07004196
4197 pipe_wm = &intel_cstate->wm.skl.optimal;
Maarten Lankhorst03af79e2016-10-26 15:41:36 +02004198 ret = skl_update_pipe_wm(cstate, old_pipe_wm, pipe_wm,
4199 &results->ddb, &changed);
Matt Roper734fa012016-05-12 15:11:40 -07004200 if (ret)
4201 return ret;
4202
4203 if (changed)
4204 results->dirty_pipes |= drm_crtc_mask(crtc);
4205
4206 if ((results->dirty_pipes & drm_crtc_mask(crtc)) == 0)
4207 /* This pipe's WM's did not change */
4208 continue;
4209
4210 intel_cstate->update_wm_pre = true;
Matt Roper734fa012016-05-12 15:11:40 -07004211 }
4212
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004213 skl_print_wm_changes(state);
4214
Matt Roper98d39492016-05-12 07:06:03 -07004215 return 0;
4216}
4217
Ville Syrjälä432081b2016-10-31 22:37:03 +02004218static void skl_update_wm(struct intel_crtc *intel_crtc)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004219{
Ville Syrjälä432081b2016-10-31 22:37:03 +02004220 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004221 struct drm_i915_private *dev_priv = to_i915(dev);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004222 struct skl_wm_values *results = &dev_priv->wm.skl_results;
Matt Roper2722efb2016-08-17 15:55:55 -04004223 struct skl_wm_values *hw_vals = &dev_priv->wm.skl_hw;
Ville Syrjälä432081b2016-10-31 22:37:03 +02004224 struct intel_crtc_state *cstate = to_intel_crtc_state(intel_crtc->base.state);
Matt Ropere8f1f022016-05-12 07:05:55 -07004225 struct skl_pipe_wm *pipe_wm = &cstate->wm.skl.optimal;
Lyude27082492016-08-24 07:48:10 +02004226 enum pipe pipe = intel_crtc->pipe;
Bob Paauweadda50b2015-07-21 10:42:53 -07004227
Ville Syrjälä432081b2016-10-31 22:37:03 +02004228 if ((results->dirty_pipes & drm_crtc_mask(&intel_crtc->base)) == 0)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004229 return;
4230
Matt Roper734fa012016-05-12 15:11:40 -07004231 mutex_lock(&dev_priv->wm.wm_mutex);
4232
Matt Roper2722efb2016-08-17 15:55:55 -04004233 /*
Lyude27082492016-08-24 07:48:10 +02004234 * If this pipe isn't active already, we're going to be enabling it
4235 * very soon. Since it's safe to update a pipe's ddb allocation while
4236 * the pipe's shut off, just do so here. Already active pipes will have
4237 * their watermarks updated once we update their planes.
Matt Roper2722efb2016-08-17 15:55:55 -04004238 */
Ville Syrjälä432081b2016-10-31 22:37:03 +02004239 if (intel_crtc->base.state->active_changed) {
Lyude27082492016-08-24 07:48:10 +02004240 int plane;
4241
Matt Roper2c4b49a2016-10-26 15:51:29 -07004242 for_each_universal_plane(dev_priv, pipe, plane)
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004243 skl_write_plane_wm(intel_crtc, &pipe_wm->planes[plane],
4244 &results->ddb, plane);
Lyude27082492016-08-24 07:48:10 +02004245
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004246 skl_write_cursor_wm(intel_crtc, &pipe_wm->planes[PLANE_CURSOR],
4247 &results->ddb);
Lyude27082492016-08-24 07:48:10 +02004248 }
4249
4250 skl_copy_wm_for_pipe(hw_vals, results, pipe);
Matt Roper734fa012016-05-12 15:11:40 -07004251
Lyudece0ba282016-09-15 10:46:35 -04004252 intel_crtc->hw_ddb = cstate->wm.skl.ddb;
4253
Matt Roper734fa012016-05-12 15:11:40 -07004254 mutex_unlock(&dev_priv->wm.wm_mutex);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004255}
4256
Ville Syrjäläd8905652016-01-14 14:53:35 +02004257static void ilk_compute_wm_config(struct drm_device *dev,
4258 struct intel_wm_config *config)
4259{
4260 struct intel_crtc *crtc;
4261
4262 /* Compute the currently _active_ config */
4263 for_each_intel_crtc(dev, crtc) {
4264 const struct intel_pipe_wm *wm = &crtc->wm.active.ilk;
4265
4266 if (!wm->pipe_enabled)
4267 continue;
4268
4269 config->sprites_enabled |= wm->sprites_enabled;
4270 config->sprites_scaled |= wm->sprites_scaled;
4271 config->num_pipes_active++;
4272 }
4273}
4274
Matt Ropered4a6a72016-02-23 17:20:13 -08004275static void ilk_program_watermarks(struct drm_i915_private *dev_priv)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03004276{
Chris Wilson91c8a322016-07-05 10:40:23 +01004277 struct drm_device *dev = &dev_priv->drm;
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07004278 struct intel_pipe_wm lp_wm_1_2 = {}, lp_wm_5_6 = {}, *best_lp_wm;
Imre Deak820c1982013-12-17 14:46:36 +02004279 struct ilk_wm_maximums max;
Ville Syrjäläd8905652016-01-14 14:53:35 +02004280 struct intel_wm_config config = {};
Imre Deak820c1982013-12-17 14:46:36 +02004281 struct ilk_wm_values results = {};
Ville Syrjälä77c122b2013-08-06 22:24:04 +03004282 enum intel_ddb_partitioning partitioning;
Matt Roper261a27d2015-10-08 15:28:25 -07004283
Ville Syrjäläd8905652016-01-14 14:53:35 +02004284 ilk_compute_wm_config(dev, &config);
4285
4286 ilk_compute_wm_maximums(dev, 1, &config, INTEL_DDB_PART_1_2, &max);
4287 ilk_wm_merge(dev, &config, &max, &lp_wm_1_2);
Ville Syrjälä0362c782013-10-09 19:17:57 +03004288
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03004289 /* 5/6 split only in single pipe config on IVB+ */
Ville Syrjäläec98c8d2013-10-11 15:26:26 +03004290 if (INTEL_INFO(dev)->gen >= 7 &&
Ville Syrjäläd8905652016-01-14 14:53:35 +02004291 config.num_pipes_active == 1 && config.sprites_enabled) {
4292 ilk_compute_wm_maximums(dev, 1, &config, INTEL_DDB_PART_5_6, &max);
4293 ilk_wm_merge(dev, &config, &max, &lp_wm_5_6);
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03004294
Imre Deak820c1982013-12-17 14:46:36 +02004295 best_lp_wm = ilk_find_best_result(dev, &lp_wm_1_2, &lp_wm_5_6);
Paulo Zanoni861f3382013-05-31 10:19:21 -03004296 } else {
Ville Syrjälä198a1e92013-10-09 19:17:58 +03004297 best_lp_wm = &lp_wm_1_2;
Paulo Zanoni861f3382013-05-31 10:19:21 -03004298 }
4299
Ville Syrjälä198a1e92013-10-09 19:17:58 +03004300 partitioning = (best_lp_wm == &lp_wm_1_2) ?
Ville Syrjälä77c122b2013-08-06 22:24:04 +03004301 INTEL_DDB_PART_1_2 : INTEL_DDB_PART_5_6;
Paulo Zanoni861f3382013-05-31 10:19:21 -03004302
Imre Deak820c1982013-12-17 14:46:36 +02004303 ilk_compute_wm_results(dev, best_lp_wm, partitioning, &results);
Ville Syrjälä609cede2013-10-09 19:18:03 +03004304
Imre Deak820c1982013-12-17 14:46:36 +02004305 ilk_write_wm_values(dev_priv, &results);
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03004306}
4307
Matt Ropered4a6a72016-02-23 17:20:13 -08004308static void ilk_initial_watermarks(struct intel_crtc_state *cstate)
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07004309{
Matt Ropered4a6a72016-02-23 17:20:13 -08004310 struct drm_i915_private *dev_priv = to_i915(cstate->base.crtc->dev);
4311 struct intel_crtc *intel_crtc = to_intel_crtc(cstate->base.crtc);
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07004312
Matt Ropered4a6a72016-02-23 17:20:13 -08004313 mutex_lock(&dev_priv->wm.wm_mutex);
Matt Ropere8f1f022016-05-12 07:05:55 -07004314 intel_crtc->wm.active.ilk = cstate->wm.ilk.intermediate;
Matt Ropered4a6a72016-02-23 17:20:13 -08004315 ilk_program_watermarks(dev_priv);
4316 mutex_unlock(&dev_priv->wm.wm_mutex);
4317}
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07004318
Matt Ropered4a6a72016-02-23 17:20:13 -08004319static void ilk_optimize_watermarks(struct intel_crtc_state *cstate)
4320{
4321 struct drm_i915_private *dev_priv = to_i915(cstate->base.crtc->dev);
4322 struct intel_crtc *intel_crtc = to_intel_crtc(cstate->base.crtc);
4323
4324 mutex_lock(&dev_priv->wm.wm_mutex);
4325 if (cstate->wm.need_postvbl_update) {
Matt Ropere8f1f022016-05-12 07:05:55 -07004326 intel_crtc->wm.active.ilk = cstate->wm.ilk.optimal;
Matt Ropered4a6a72016-02-23 17:20:13 -08004327 ilk_program_watermarks(dev_priv);
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07004328 }
Matt Ropered4a6a72016-02-23 17:20:13 -08004329 mutex_unlock(&dev_priv->wm.wm_mutex);
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07004330}
4331
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004332static inline void skl_wm_level_from_reg_val(uint32_t val,
4333 struct skl_wm_level *level)
Pradeep Bhat30789992014-11-04 17:06:45 +00004334{
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004335 level->plane_en = val & PLANE_WM_EN;
4336 level->plane_res_b = val & PLANE_WM_BLOCKS_MASK;
4337 level->plane_res_l = (val >> PLANE_WM_LINES_SHIFT) &
4338 PLANE_WM_LINES_MASK;
Pradeep Bhat30789992014-11-04 17:06:45 +00004339}
4340
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04004341void skl_pipe_wm_get_hw_state(struct drm_crtc *crtc,
4342 struct skl_pipe_wm *out)
Pradeep Bhat30789992014-11-04 17:06:45 +00004343{
4344 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004345 struct drm_i915_private *dev_priv = to_i915(dev);
Pradeep Bhat30789992014-11-04 17:06:45 +00004346 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004347 struct intel_plane *intel_plane;
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004348 struct skl_plane_wm *wm;
Pradeep Bhat30789992014-11-04 17:06:45 +00004349 enum pipe pipe = intel_crtc->pipe;
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004350 int level, id, max_level;
4351 uint32_t val;
Pradeep Bhat30789992014-11-04 17:06:45 +00004352
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01004353 max_level = ilk_wm_max_level(dev_priv);
Pradeep Bhat30789992014-11-04 17:06:45 +00004354
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004355 for_each_intel_plane_on_crtc(dev, intel_crtc, intel_plane) {
4356 id = skl_wm_plane_id(intel_plane);
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04004357 wm = &out->planes[id];
Pradeep Bhat30789992014-11-04 17:06:45 +00004358
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004359 for (level = 0; level <= max_level; level++) {
4360 if (id != PLANE_CURSOR)
4361 val = I915_READ(PLANE_WM(pipe, id, level));
4362 else
4363 val = I915_READ(CUR_WM(pipe, level));
4364
4365 skl_wm_level_from_reg_val(val, &wm->wm[level]);
4366 }
4367
4368 if (id != PLANE_CURSOR)
4369 val = I915_READ(PLANE_WM_TRANS(pipe, id));
4370 else
4371 val = I915_READ(CUR_WM_TRANS(pipe));
4372
4373 skl_wm_level_from_reg_val(val, &wm->trans_wm);
4374 }
Pradeep Bhat30789992014-11-04 17:06:45 +00004375
Matt Roper3ef00282015-03-09 10:19:24 -07004376 if (!intel_crtc->active)
Pradeep Bhat30789992014-11-04 17:06:45 +00004377 return;
4378
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04004379 out->linetime = I915_READ(PIPE_WM_LINETIME(pipe));
Pradeep Bhat30789992014-11-04 17:06:45 +00004380}
4381
4382void skl_wm_get_hw_state(struct drm_device *dev)
4383{
Chris Wilsonfac5e232016-07-04 11:34:36 +01004384 struct drm_i915_private *dev_priv = to_i915(dev);
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04004385 struct skl_wm_values *hw = &dev_priv->wm.skl_hw;
Damien Lespiaua269c582014-11-04 17:06:49 +00004386 struct skl_ddb_allocation *ddb = &dev_priv->wm.skl_hw.ddb;
Pradeep Bhat30789992014-11-04 17:06:45 +00004387 struct drm_crtc *crtc;
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04004388 struct intel_crtc *intel_crtc;
4389 struct intel_crtc_state *cstate;
Pradeep Bhat30789992014-11-04 17:06:45 +00004390
Damien Lespiaua269c582014-11-04 17:06:49 +00004391 skl_ddb_get_hw_state(dev_priv, ddb);
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04004392 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
4393 intel_crtc = to_intel_crtc(crtc);
4394 cstate = to_intel_crtc_state(crtc->state);
4395
4396 skl_pipe_wm_get_hw_state(crtc, &cstate->wm.skl.optimal);
4397
Maarten Lankhorst03af79e2016-10-26 15:41:36 +02004398 if (intel_crtc->active)
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04004399 hw->dirty_pipes |= drm_crtc_mask(crtc);
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04004400 }
Matt Ropera1de91e2016-05-12 07:05:57 -07004401
Matt Roper279e99d2016-05-12 07:06:02 -07004402 if (dev_priv->active_crtcs) {
4403 /* Fully recompute DDB on first atomic commit */
4404 dev_priv->wm.distrust_bios_wm = true;
4405 } else {
4406 /* Easy/common case; just sanitize DDB now if everything off */
4407 memset(ddb, 0, sizeof(*ddb));
4408 }
Pradeep Bhat30789992014-11-04 17:06:45 +00004409}
4410
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004411static void ilk_pipe_wm_get_hw_state(struct drm_crtc *crtc)
4412{
4413 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004414 struct drm_i915_private *dev_priv = to_i915(dev);
Imre Deak820c1982013-12-17 14:46:36 +02004415 struct ilk_wm_values *hw = &dev_priv->wm.hw;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004416 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Matt Roper4e0963c2015-09-24 15:53:15 -07004417 struct intel_crtc_state *cstate = to_intel_crtc_state(crtc->state);
Matt Ropere8f1f022016-05-12 07:05:55 -07004418 struct intel_pipe_wm *active = &cstate->wm.ilk.optimal;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004419 enum pipe pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004420 static const i915_reg_t wm0_pipe_reg[] = {
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004421 [PIPE_A] = WM0_PIPEA_ILK,
4422 [PIPE_B] = WM0_PIPEB_ILK,
4423 [PIPE_C] = WM0_PIPEC_IVB,
4424 };
4425
4426 hw->wm_pipe[pipe] = I915_READ(wm0_pipe_reg[pipe]);
Tvrtko Ursulin86527442016-10-13 11:03:00 +01004427 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjäläce0e0712013-12-05 15:51:36 +02004428 hw->wm_linetime[pipe] = I915_READ(PIPE_WM_LINETIME(pipe));
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004429
Ville Syrjälä15606532016-05-13 17:55:17 +03004430 memset(active, 0, sizeof(*active));
4431
Matt Roper3ef00282015-03-09 10:19:24 -07004432 active->pipe_enabled = intel_crtc->active;
Ville Syrjälä2a44b762014-03-07 18:32:09 +02004433
4434 if (active->pipe_enabled) {
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004435 u32 tmp = hw->wm_pipe[pipe];
4436
4437 /*
4438 * For active pipes LP0 watermark is marked as
4439 * enabled, and LP1+ watermaks as disabled since
4440 * we can't really reverse compute them in case
4441 * multiple pipes are active.
4442 */
4443 active->wm[0].enable = true;
4444 active->wm[0].pri_val = (tmp & WM0_PIPE_PLANE_MASK) >> WM0_PIPE_PLANE_SHIFT;
4445 active->wm[0].spr_val = (tmp & WM0_PIPE_SPRITE_MASK) >> WM0_PIPE_SPRITE_SHIFT;
4446 active->wm[0].cur_val = tmp & WM0_PIPE_CURSOR_MASK;
4447 active->linetime = hw->wm_linetime[pipe];
4448 } else {
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01004449 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004450
4451 /*
4452 * For inactive pipes, all watermark levels
4453 * should be marked as enabled but zeroed,
4454 * which is what we'd compute them to.
4455 */
4456 for (level = 0; level <= max_level; level++)
4457 active->wm[level].enable = true;
4458 }
Matt Roper4e0963c2015-09-24 15:53:15 -07004459
4460 intel_crtc->wm.active.ilk = *active;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004461}
4462
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03004463#define _FW_WM(value, plane) \
4464 (((value) & DSPFW_ ## plane ## _MASK) >> DSPFW_ ## plane ## _SHIFT)
4465#define _FW_WM_VLV(value, plane) \
4466 (((value) & DSPFW_ ## plane ## _MASK_VLV) >> DSPFW_ ## plane ## _SHIFT)
4467
4468static void vlv_read_wm_values(struct drm_i915_private *dev_priv,
4469 struct vlv_wm_values *wm)
4470{
4471 enum pipe pipe;
4472 uint32_t tmp;
4473
4474 for_each_pipe(dev_priv, pipe) {
4475 tmp = I915_READ(VLV_DDL(pipe));
4476
4477 wm->ddl[pipe].primary =
4478 (tmp >> DDL_PLANE_SHIFT) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
4479 wm->ddl[pipe].cursor =
4480 (tmp >> DDL_CURSOR_SHIFT) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
4481 wm->ddl[pipe].sprite[0] =
4482 (tmp >> DDL_SPRITE_SHIFT(0)) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
4483 wm->ddl[pipe].sprite[1] =
4484 (tmp >> DDL_SPRITE_SHIFT(1)) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
4485 }
4486
4487 tmp = I915_READ(DSPFW1);
4488 wm->sr.plane = _FW_WM(tmp, SR);
4489 wm->pipe[PIPE_B].cursor = _FW_WM(tmp, CURSORB);
4490 wm->pipe[PIPE_B].primary = _FW_WM_VLV(tmp, PLANEB);
4491 wm->pipe[PIPE_A].primary = _FW_WM_VLV(tmp, PLANEA);
4492
4493 tmp = I915_READ(DSPFW2);
4494 wm->pipe[PIPE_A].sprite[1] = _FW_WM_VLV(tmp, SPRITEB);
4495 wm->pipe[PIPE_A].cursor = _FW_WM(tmp, CURSORA);
4496 wm->pipe[PIPE_A].sprite[0] = _FW_WM_VLV(tmp, SPRITEA);
4497
4498 tmp = I915_READ(DSPFW3);
4499 wm->sr.cursor = _FW_WM(tmp, CURSOR_SR);
4500
4501 if (IS_CHERRYVIEW(dev_priv)) {
4502 tmp = I915_READ(DSPFW7_CHV);
4503 wm->pipe[PIPE_B].sprite[1] = _FW_WM_VLV(tmp, SPRITED);
4504 wm->pipe[PIPE_B].sprite[0] = _FW_WM_VLV(tmp, SPRITEC);
4505
4506 tmp = I915_READ(DSPFW8_CHV);
4507 wm->pipe[PIPE_C].sprite[1] = _FW_WM_VLV(tmp, SPRITEF);
4508 wm->pipe[PIPE_C].sprite[0] = _FW_WM_VLV(tmp, SPRITEE);
4509
4510 tmp = I915_READ(DSPFW9_CHV);
4511 wm->pipe[PIPE_C].primary = _FW_WM_VLV(tmp, PLANEC);
4512 wm->pipe[PIPE_C].cursor = _FW_WM(tmp, CURSORC);
4513
4514 tmp = I915_READ(DSPHOWM);
4515 wm->sr.plane |= _FW_WM(tmp, SR_HI) << 9;
4516 wm->pipe[PIPE_C].sprite[1] |= _FW_WM(tmp, SPRITEF_HI) << 8;
4517 wm->pipe[PIPE_C].sprite[0] |= _FW_WM(tmp, SPRITEE_HI) << 8;
4518 wm->pipe[PIPE_C].primary |= _FW_WM(tmp, PLANEC_HI) << 8;
4519 wm->pipe[PIPE_B].sprite[1] |= _FW_WM(tmp, SPRITED_HI) << 8;
4520 wm->pipe[PIPE_B].sprite[0] |= _FW_WM(tmp, SPRITEC_HI) << 8;
4521 wm->pipe[PIPE_B].primary |= _FW_WM(tmp, PLANEB_HI) << 8;
4522 wm->pipe[PIPE_A].sprite[1] |= _FW_WM(tmp, SPRITEB_HI) << 8;
4523 wm->pipe[PIPE_A].sprite[0] |= _FW_WM(tmp, SPRITEA_HI) << 8;
4524 wm->pipe[PIPE_A].primary |= _FW_WM(tmp, PLANEA_HI) << 8;
4525 } else {
4526 tmp = I915_READ(DSPFW7);
4527 wm->pipe[PIPE_B].sprite[1] = _FW_WM_VLV(tmp, SPRITED);
4528 wm->pipe[PIPE_B].sprite[0] = _FW_WM_VLV(tmp, SPRITEC);
4529
4530 tmp = I915_READ(DSPHOWM);
4531 wm->sr.plane |= _FW_WM(tmp, SR_HI) << 9;
4532 wm->pipe[PIPE_B].sprite[1] |= _FW_WM(tmp, SPRITED_HI) << 8;
4533 wm->pipe[PIPE_B].sprite[0] |= _FW_WM(tmp, SPRITEC_HI) << 8;
4534 wm->pipe[PIPE_B].primary |= _FW_WM(tmp, PLANEB_HI) << 8;
4535 wm->pipe[PIPE_A].sprite[1] |= _FW_WM(tmp, SPRITEB_HI) << 8;
4536 wm->pipe[PIPE_A].sprite[0] |= _FW_WM(tmp, SPRITEA_HI) << 8;
4537 wm->pipe[PIPE_A].primary |= _FW_WM(tmp, PLANEA_HI) << 8;
4538 }
4539}
4540
4541#undef _FW_WM
4542#undef _FW_WM_VLV
4543
4544void vlv_wm_get_hw_state(struct drm_device *dev)
4545{
4546 struct drm_i915_private *dev_priv = to_i915(dev);
4547 struct vlv_wm_values *wm = &dev_priv->wm.vlv;
4548 struct intel_plane *plane;
4549 enum pipe pipe;
4550 u32 val;
4551
4552 vlv_read_wm_values(dev_priv, wm);
4553
4554 for_each_intel_plane(dev, plane) {
4555 switch (plane->base.type) {
4556 int sprite;
4557 case DRM_PLANE_TYPE_CURSOR:
4558 plane->wm.fifo_size = 63;
4559 break;
4560 case DRM_PLANE_TYPE_PRIMARY:
4561 plane->wm.fifo_size = vlv_get_fifo_size(dev, plane->pipe, 0);
4562 break;
4563 case DRM_PLANE_TYPE_OVERLAY:
4564 sprite = plane->plane;
4565 plane->wm.fifo_size = vlv_get_fifo_size(dev, plane->pipe, sprite + 1);
4566 break;
4567 }
4568 }
4569
4570 wm->cxsr = I915_READ(FW_BLC_SELF_VLV) & FW_CSPWRDWNEN;
4571 wm->level = VLV_WM_LEVEL_PM2;
4572
4573 if (IS_CHERRYVIEW(dev_priv)) {
4574 mutex_lock(&dev_priv->rps.hw_lock);
4575
4576 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
4577 if (val & DSP_MAXFIFO_PM5_ENABLE)
4578 wm->level = VLV_WM_LEVEL_PM5;
4579
Ville Syrjälä58590c12015-09-08 21:05:12 +03004580 /*
4581 * If DDR DVFS is disabled in the BIOS, Punit
4582 * will never ack the request. So if that happens
4583 * assume we don't have to enable/disable DDR DVFS
4584 * dynamically. To test that just set the REQ_ACK
4585 * bit to poke the Punit, but don't change the
4586 * HIGH/LOW bits so that we don't actually change
4587 * the current state.
4588 */
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03004589 val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
Ville Syrjälä58590c12015-09-08 21:05:12 +03004590 val |= FORCE_DDR_FREQ_REQ_ACK;
4591 vlv_punit_write(dev_priv, PUNIT_REG_DDR_SETUP2, val);
4592
4593 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2) &
4594 FORCE_DDR_FREQ_REQ_ACK) == 0, 3)) {
4595 DRM_DEBUG_KMS("Punit not acking DDR DVFS request, "
4596 "assuming DDR DVFS is disabled\n");
4597 dev_priv->wm.max_level = VLV_WM_LEVEL_PM5;
4598 } else {
4599 val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
4600 if ((val & FORCE_DDR_HIGH_FREQ) == 0)
4601 wm->level = VLV_WM_LEVEL_DDR_DVFS;
4602 }
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03004603
4604 mutex_unlock(&dev_priv->rps.hw_lock);
4605 }
4606
4607 for_each_pipe(dev_priv, pipe)
4608 DRM_DEBUG_KMS("Initial watermarks: pipe %c, plane=%d, cursor=%d, sprite0=%d, sprite1=%d\n",
4609 pipe_name(pipe), wm->pipe[pipe].primary, wm->pipe[pipe].cursor,
4610 wm->pipe[pipe].sprite[0], wm->pipe[pipe].sprite[1]);
4611
4612 DRM_DEBUG_KMS("Initial watermarks: SR plane=%d, SR cursor=%d level=%d cxsr=%d\n",
4613 wm->sr.plane, wm->sr.cursor, wm->level, wm->cxsr);
4614}
4615
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004616void ilk_wm_get_hw_state(struct drm_device *dev)
4617{
Chris Wilsonfac5e232016-07-04 11:34:36 +01004618 struct drm_i915_private *dev_priv = to_i915(dev);
Imre Deak820c1982013-12-17 14:46:36 +02004619 struct ilk_wm_values *hw = &dev_priv->wm.hw;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004620 struct drm_crtc *crtc;
4621
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01004622 for_each_crtc(dev, crtc)
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004623 ilk_pipe_wm_get_hw_state(crtc);
4624
4625 hw->wm_lp[0] = I915_READ(WM1_LP_ILK);
4626 hw->wm_lp[1] = I915_READ(WM2_LP_ILK);
4627 hw->wm_lp[2] = I915_READ(WM3_LP_ILK);
4628
4629 hw->wm_lp_spr[0] = I915_READ(WM1S_LP_ILK);
Ville Syrjäläcfa76982014-03-07 18:32:08 +02004630 if (INTEL_INFO(dev)->gen >= 7) {
4631 hw->wm_lp_spr[1] = I915_READ(WM2S_LP_IVB);
4632 hw->wm_lp_spr[2] = I915_READ(WM3S_LP_IVB);
4633 }
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004634
Tvrtko Ursulin86527442016-10-13 11:03:00 +01004635 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjäläac9545f2013-12-05 15:51:28 +02004636 hw->partitioning = (I915_READ(WM_MISC) & WM_MISC_DATA_PARTITION_5_6) ?
4637 INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01004638 else if (IS_IVYBRIDGE(dev_priv))
Ville Syrjäläac9545f2013-12-05 15:51:28 +02004639 hw->partitioning = (I915_READ(DISP_ARB_CTL2) & DISP_DATA_PARTITION_5_6) ?
4640 INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004641
4642 hw->enable_fbc_wm =
4643 !(I915_READ(DISP_ARB_CTL) & DISP_FBC_WM_DIS);
4644}
4645
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03004646/**
4647 * intel_update_watermarks - update FIFO watermark values based on current modes
4648 *
4649 * Calculate watermark values for the various WM regs based on current mode
4650 * and plane configuration.
4651 *
4652 * There are several cases to deal with here:
4653 * - normal (i.e. non-self-refresh)
4654 * - self-refresh (SR) mode
4655 * - lines are large relative to FIFO size (buffer can hold up to 2)
4656 * - lines are small relative to FIFO size (buffer can hold more than 2
4657 * lines), so need to account for TLB latency
4658 *
4659 * The normal calculation is:
4660 * watermark = dotclock * bytes per pixel * latency
4661 * where latency is platform & configuration dependent (we assume pessimal
4662 * values here).
4663 *
4664 * The SR calculation is:
4665 * watermark = (trunc(latency/line time)+1) * surface width *
4666 * bytes per pixel
4667 * where
4668 * line time = htotal / dotclock
4669 * surface width = hdisplay for normal plane and 64 for cursor
4670 * and latency is assumed to be high, as above.
4671 *
4672 * The final value programmed to the register should always be rounded up,
4673 * and include an extra 2 entries to account for clock crossings.
4674 *
4675 * We don't use the sprite, so we can ignore that. And on Crestline we have
4676 * to set the non-SR watermarks to 8.
4677 */
Ville Syrjälä432081b2016-10-31 22:37:03 +02004678void intel_update_watermarks(struct intel_crtc *crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03004679{
Ville Syrjälä432081b2016-10-31 22:37:03 +02004680 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03004681
4682 if (dev_priv->display.update_wm)
Ville Syrjälä46ba6142013-09-10 11:40:40 +03004683 dev_priv->display.update_wm(crtc);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03004684}
4685
Jani Nikulae2828912016-01-18 09:19:47 +02004686/*
Daniel Vetter92703882012-08-09 16:46:01 +02004687 * Lock protecting IPS related data structures
Daniel Vetter92703882012-08-09 16:46:01 +02004688 */
4689DEFINE_SPINLOCK(mchdev_lock);
4690
4691/* Global for IPS driver to get at the current i915 device. Protected by
4692 * mchdev_lock. */
4693static struct drm_i915_private *i915_mch_dev;
4694
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01004695bool ironlake_set_drps(struct drm_i915_private *dev_priv, u8 val)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004696{
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004697 u16 rgvswctl;
4698
Daniel Vetter92703882012-08-09 16:46:01 +02004699 assert_spin_locked(&mchdev_lock);
4700
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004701 rgvswctl = I915_READ16(MEMSWCTL);
4702 if (rgvswctl & MEMCTL_CMD_STS) {
4703 DRM_DEBUG("gpu busy, RCS change rejected\n");
4704 return false; /* still busy with another command */
4705 }
4706
4707 rgvswctl = (MEMCTL_CMD_CHFREQ << MEMCTL_CMD_SHIFT) |
4708 (val << MEMCTL_FREQ_SHIFT) | MEMCTL_SFCAVM;
4709 I915_WRITE16(MEMSWCTL, rgvswctl);
4710 POSTING_READ16(MEMSWCTL);
4711
4712 rgvswctl |= MEMCTL_CMD_STS;
4713 I915_WRITE16(MEMSWCTL, rgvswctl);
4714
4715 return true;
4716}
4717
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01004718static void ironlake_enable_drps(struct drm_i915_private *dev_priv)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004719{
Tvrtko Ursulin84f1b202016-02-11 10:27:32 +00004720 u32 rgvmodectl;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004721 u8 fmax, fmin, fstart, vstart;
4722
Daniel Vetter92703882012-08-09 16:46:01 +02004723 spin_lock_irq(&mchdev_lock);
4724
Tvrtko Ursulin84f1b202016-02-11 10:27:32 +00004725 rgvmodectl = I915_READ(MEMMODECTL);
4726
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004727 /* Enable temp reporting */
4728 I915_WRITE16(PMMISC, I915_READ(PMMISC) | MCPPCE_EN);
4729 I915_WRITE16(TSC1, I915_READ(TSC1) | TSE);
4730
4731 /* 100ms RC evaluation intervals */
4732 I915_WRITE(RCUPEI, 100000);
4733 I915_WRITE(RCDNEI, 100000);
4734
4735 /* Set max/min thresholds to 90ms and 80ms respectively */
4736 I915_WRITE(RCBMAXAVG, 90000);
4737 I915_WRITE(RCBMINAVG, 80000);
4738
4739 I915_WRITE(MEMIHYST, 1);
4740
4741 /* Set up min, max, and cur for interrupt handling */
4742 fmax = (rgvmodectl & MEMMODE_FMAX_MASK) >> MEMMODE_FMAX_SHIFT;
4743 fmin = (rgvmodectl & MEMMODE_FMIN_MASK);
4744 fstart = (rgvmodectl & MEMMODE_FSTART_MASK) >>
4745 MEMMODE_FSTART_SHIFT;
4746
Ville Syrjälä616847e2015-09-18 20:03:19 +03004747 vstart = (I915_READ(PXVFREQ(fstart)) & PXVFREQ_PX_MASK) >>
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004748 PXVFREQ_PX_SHIFT;
4749
Daniel Vetter20e4d402012-08-08 23:35:39 +02004750 dev_priv->ips.fmax = fmax; /* IPS callback will increase this */
4751 dev_priv->ips.fstart = fstart;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004752
Daniel Vetter20e4d402012-08-08 23:35:39 +02004753 dev_priv->ips.max_delay = fstart;
4754 dev_priv->ips.min_delay = fmin;
4755 dev_priv->ips.cur_delay = fstart;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004756
4757 DRM_DEBUG_DRIVER("fmax: %d, fmin: %d, fstart: %d\n",
4758 fmax, fmin, fstart);
4759
4760 I915_WRITE(MEMINTREN, MEMINT_CX_SUPR_EN | MEMINT_EVAL_CHG_EN);
4761
4762 /*
4763 * Interrupts will be enabled in ironlake_irq_postinstall
4764 */
4765
4766 I915_WRITE(VIDSTART, vstart);
4767 POSTING_READ(VIDSTART);
4768
4769 rgvmodectl |= MEMMODE_SWMODE_EN;
4770 I915_WRITE(MEMMODECTL, rgvmodectl);
4771
Daniel Vetter92703882012-08-09 16:46:01 +02004772 if (wait_for_atomic((I915_READ(MEMSWCTL) & MEMCTL_CMD_STS) == 0, 10))
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004773 DRM_ERROR("stuck trying to change perf mode\n");
Daniel Vetterdd92d8d2015-07-20 10:58:21 +02004774 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004775
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01004776 ironlake_set_drps(dev_priv, fstart);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004777
Ville Syrjälä7d81c3e2015-09-18 20:03:20 +03004778 dev_priv->ips.last_count1 = I915_READ(DMIEC) +
4779 I915_READ(DDREC) + I915_READ(CSIEC);
Daniel Vetter20e4d402012-08-08 23:35:39 +02004780 dev_priv->ips.last_time1 = jiffies_to_msecs(jiffies);
Ville Syrjälä7d81c3e2015-09-18 20:03:20 +03004781 dev_priv->ips.last_count2 = I915_READ(GFXEC);
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00004782 dev_priv->ips.last_time2 = ktime_get_raw_ns();
Daniel Vetter92703882012-08-09 16:46:01 +02004783
4784 spin_unlock_irq(&mchdev_lock);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004785}
4786
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01004787static void ironlake_disable_drps(struct drm_i915_private *dev_priv)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004788{
Daniel Vetter92703882012-08-09 16:46:01 +02004789 u16 rgvswctl;
4790
4791 spin_lock_irq(&mchdev_lock);
4792
4793 rgvswctl = I915_READ16(MEMSWCTL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004794
4795 /* Ack interrupts, disable EFC interrupt */
4796 I915_WRITE(MEMINTREN, I915_READ(MEMINTREN) & ~MEMINT_EVAL_CHG_EN);
4797 I915_WRITE(MEMINTRSTS, MEMINT_EVAL_CHG);
4798 I915_WRITE(DEIER, I915_READ(DEIER) & ~DE_PCU_EVENT);
4799 I915_WRITE(DEIIR, DE_PCU_EVENT);
4800 I915_WRITE(DEIMR, I915_READ(DEIMR) | DE_PCU_EVENT);
4801
4802 /* Go back to the starting frequency */
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01004803 ironlake_set_drps(dev_priv, dev_priv->ips.fstart);
Daniel Vetterdd92d8d2015-07-20 10:58:21 +02004804 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004805 rgvswctl |= MEMCTL_CMD_STS;
4806 I915_WRITE(MEMSWCTL, rgvswctl);
Daniel Vetterdd92d8d2015-07-20 10:58:21 +02004807 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004808
Daniel Vetter92703882012-08-09 16:46:01 +02004809 spin_unlock_irq(&mchdev_lock);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004810}
4811
Daniel Vetteracbe9472012-07-26 11:50:05 +02004812/* There's a funny hw issue where the hw returns all 0 when reading from
4813 * GEN6_RP_INTERRUPT_LIMITS. Hence we always need to compute the desired value
4814 * ourselves, instead of doing a rmw cycle (which might result in us clearing
4815 * all limits and the gpu stuck at whatever frequency it is at atm).
4816 */
Akash Goel74ef1172015-03-06 11:07:19 +05304817static u32 intel_rps_limits(struct drm_i915_private *dev_priv, u8 val)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004818{
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01004819 u32 limits;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004820
Daniel Vetter20b46e52012-07-26 11:16:14 +02004821 /* Only set the down limit when we've reached the lowest level to avoid
4822 * getting more interrupts, otherwise leave this clear. This prevents a
4823 * race in the hw when coming out of rc6: There's a tiny window where
4824 * the hw runs at the minimal clock before selecting the desired
4825 * frequency, if the down threshold expires in that window we will not
4826 * receive a down interrupt. */
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03004827 if (IS_GEN9(dev_priv)) {
Akash Goel74ef1172015-03-06 11:07:19 +05304828 limits = (dev_priv->rps.max_freq_softlimit) << 23;
4829 if (val <= dev_priv->rps.min_freq_softlimit)
4830 limits |= (dev_priv->rps.min_freq_softlimit) << 14;
4831 } else {
4832 limits = dev_priv->rps.max_freq_softlimit << 24;
4833 if (val <= dev_priv->rps.min_freq_softlimit)
4834 limits |= dev_priv->rps.min_freq_softlimit << 16;
4835 }
Daniel Vetter20b46e52012-07-26 11:16:14 +02004836
4837 return limits;
4838}
4839
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004840static void gen6_set_rps_thresholds(struct drm_i915_private *dev_priv, u8 val)
4841{
4842 int new_power;
Akash Goel8a586432015-03-06 11:07:18 +05304843 u32 threshold_up = 0, threshold_down = 0; /* in % */
4844 u32 ei_up = 0, ei_down = 0;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004845
4846 new_power = dev_priv->rps.power;
4847 switch (dev_priv->rps.power) {
4848 case LOW_POWER:
Chris Wilsona72b5622016-07-02 15:35:59 +01004849 if (val > dev_priv->rps.efficient_freq + 1 &&
4850 val > dev_priv->rps.cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004851 new_power = BETWEEN;
4852 break;
4853
4854 case BETWEEN:
Chris Wilsona72b5622016-07-02 15:35:59 +01004855 if (val <= dev_priv->rps.efficient_freq &&
4856 val < dev_priv->rps.cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004857 new_power = LOW_POWER;
Chris Wilsona72b5622016-07-02 15:35:59 +01004858 else if (val >= dev_priv->rps.rp0_freq &&
4859 val > dev_priv->rps.cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004860 new_power = HIGH_POWER;
4861 break;
4862
4863 case HIGH_POWER:
Chris Wilsona72b5622016-07-02 15:35:59 +01004864 if (val < (dev_priv->rps.rp1_freq + dev_priv->rps.rp0_freq) >> 1 &&
4865 val < dev_priv->rps.cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004866 new_power = BETWEEN;
4867 break;
4868 }
4869 /* Max/min bins are special */
Chris Wilsonaed242f2015-03-18 09:48:21 +00004870 if (val <= dev_priv->rps.min_freq_softlimit)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004871 new_power = LOW_POWER;
Chris Wilsonaed242f2015-03-18 09:48:21 +00004872 if (val >= dev_priv->rps.max_freq_softlimit)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004873 new_power = HIGH_POWER;
4874 if (new_power == dev_priv->rps.power)
4875 return;
4876
4877 /* Note the units here are not exactly 1us, but 1280ns. */
4878 switch (new_power) {
4879 case LOW_POWER:
4880 /* Upclock if more than 95% busy over 16ms */
Akash Goel8a586432015-03-06 11:07:18 +05304881 ei_up = 16000;
4882 threshold_up = 95;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004883
4884 /* Downclock if less than 85% busy over 32ms */
Akash Goel8a586432015-03-06 11:07:18 +05304885 ei_down = 32000;
4886 threshold_down = 85;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004887 break;
4888
4889 case BETWEEN:
4890 /* Upclock if more than 90% busy over 13ms */
Akash Goel8a586432015-03-06 11:07:18 +05304891 ei_up = 13000;
4892 threshold_up = 90;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004893
4894 /* Downclock if less than 75% busy over 32ms */
Akash Goel8a586432015-03-06 11:07:18 +05304895 ei_down = 32000;
4896 threshold_down = 75;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004897 break;
4898
4899 case HIGH_POWER:
4900 /* Upclock if more than 85% busy over 10ms */
Akash Goel8a586432015-03-06 11:07:18 +05304901 ei_up = 10000;
4902 threshold_up = 85;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004903
4904 /* Downclock if less than 60% busy over 32ms */
Akash Goel8a586432015-03-06 11:07:18 +05304905 ei_down = 32000;
4906 threshold_down = 60;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004907 break;
4908 }
4909
Akash Goel8a586432015-03-06 11:07:18 +05304910 I915_WRITE(GEN6_RP_UP_EI,
Chris Wilsona72b5622016-07-02 15:35:59 +01004911 GT_INTERVAL_FROM_US(dev_priv, ei_up));
Akash Goel8a586432015-03-06 11:07:18 +05304912 I915_WRITE(GEN6_RP_UP_THRESHOLD,
Chris Wilsona72b5622016-07-02 15:35:59 +01004913 GT_INTERVAL_FROM_US(dev_priv,
4914 ei_up * threshold_up / 100));
Akash Goel8a586432015-03-06 11:07:18 +05304915
4916 I915_WRITE(GEN6_RP_DOWN_EI,
Chris Wilsona72b5622016-07-02 15:35:59 +01004917 GT_INTERVAL_FROM_US(dev_priv, ei_down));
Akash Goel8a586432015-03-06 11:07:18 +05304918 I915_WRITE(GEN6_RP_DOWN_THRESHOLD,
Chris Wilsona72b5622016-07-02 15:35:59 +01004919 GT_INTERVAL_FROM_US(dev_priv,
4920 ei_down * threshold_down / 100));
Akash Goel8a586432015-03-06 11:07:18 +05304921
Chris Wilsona72b5622016-07-02 15:35:59 +01004922 I915_WRITE(GEN6_RP_CONTROL,
4923 GEN6_RP_MEDIA_TURBO |
4924 GEN6_RP_MEDIA_HW_NORMAL_MODE |
4925 GEN6_RP_MEDIA_IS_GFX |
4926 GEN6_RP_ENABLE |
4927 GEN6_RP_UP_BUSY_AVG |
4928 GEN6_RP_DOWN_IDLE_AVG);
Akash Goel8a586432015-03-06 11:07:18 +05304929
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004930 dev_priv->rps.power = new_power;
Chris Wilson8fb55192015-04-07 16:20:28 +01004931 dev_priv->rps.up_threshold = threshold_up;
4932 dev_priv->rps.down_threshold = threshold_down;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004933 dev_priv->rps.last_adj = 0;
4934}
4935
Chris Wilson2876ce72014-03-28 08:03:34 +00004936static u32 gen6_rps_pm_mask(struct drm_i915_private *dev_priv, u8 val)
4937{
4938 u32 mask = 0;
4939
4940 if (val > dev_priv->rps.min_freq_softlimit)
Chris Wilson6f4b12f82015-03-18 09:48:23 +00004941 mask |= GEN6_PM_RP_DOWN_EI_EXPIRED | GEN6_PM_RP_DOWN_THRESHOLD | GEN6_PM_RP_DOWN_TIMEOUT;
Chris Wilson2876ce72014-03-28 08:03:34 +00004942 if (val < dev_priv->rps.max_freq_softlimit)
Chris Wilson6f4b12f82015-03-18 09:48:23 +00004943 mask |= GEN6_PM_RP_UP_EI_EXPIRED | GEN6_PM_RP_UP_THRESHOLD;
Chris Wilson2876ce72014-03-28 08:03:34 +00004944
Chris Wilson7b3c29f2014-07-10 20:31:19 +01004945 mask &= dev_priv->pm_rps_events;
4946
Imre Deak59d02a12014-12-19 19:33:26 +02004947 return gen6_sanitize_rps_pm_mask(dev_priv, ~mask);
Chris Wilson2876ce72014-03-28 08:03:34 +00004948}
4949
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06004950/* gen6_set_rps is called to update the frequency request, but should also be
4951 * called when the range (min_delay and max_delay) is modified so that we can
4952 * update the GEN6_RP_INTERRUPT_LIMITS register accordingly. */
Chris Wilsondc979972016-05-10 14:10:04 +01004953static void gen6_set_rps(struct drm_i915_private *dev_priv, u8 val)
Daniel Vetter20b46e52012-07-26 11:16:14 +02004954{
Sagar Arun Kamble23eafea2015-08-23 17:52:48 +05304955 /* WaGsvDisableTurbo: Workaround to disable turbo on BXT A* */
Chris Wilsondc979972016-05-10 14:10:04 +01004956 if (IS_BXT_REVID(dev_priv, 0, BXT_REVID_A1))
Sagar Arun Kamble23eafea2015-08-23 17:52:48 +05304957 return;
4958
Jesse Barnes4fc688c2012-11-02 11:14:01 -07004959 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Chris Wilsonaed242f2015-03-18 09:48:21 +00004960 WARN_ON(val > dev_priv->rps.max_freq);
4961 WARN_ON(val < dev_priv->rps.min_freq);
Daniel Vetter004777c2012-08-09 15:07:01 +02004962
Chris Wilsoneb64cad2014-03-27 08:24:20 +00004963 /* min/max delay may still have been modified so be sure to
4964 * write the limits value.
4965 */
4966 if (val != dev_priv->rps.cur_freq) {
4967 gen6_set_rps_thresholds(dev_priv, val);
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06004968
Chris Wilsondc979972016-05-10 14:10:04 +01004969 if (IS_GEN9(dev_priv))
Akash Goel57041952015-03-06 11:07:17 +05304970 I915_WRITE(GEN6_RPNSWREQ,
4971 GEN9_FREQUENCY(val));
Chris Wilsondc979972016-05-10 14:10:04 +01004972 else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Chris Wilsoneb64cad2014-03-27 08:24:20 +00004973 I915_WRITE(GEN6_RPNSWREQ,
4974 HSW_FREQUENCY(val));
4975 else
4976 I915_WRITE(GEN6_RPNSWREQ,
4977 GEN6_FREQUENCY(val) |
4978 GEN6_OFFSET(0) |
4979 GEN6_AGGRESSIVE_TURBO);
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06004980 }
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01004981
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01004982 /* Make sure we continue to get interrupts
4983 * until we hit the minimum or maximum frequencies.
4984 */
Akash Goel74ef1172015-03-06 11:07:19 +05304985 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS, intel_rps_limits(dev_priv, val));
Chris Wilson2876ce72014-03-28 08:03:34 +00004986 I915_WRITE(GEN6_PMINTRMSK, gen6_rps_pm_mask(dev_priv, val));
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01004987
Ben Widawskyd5570a72012-09-07 19:43:41 -07004988 POSTING_READ(GEN6_RPNSWREQ);
4989
Ben Widawskyb39fb292014-03-19 18:31:11 -07004990 dev_priv->rps.cur_freq = val;
Mika Kuoppala0f945922015-11-17 18:14:26 +02004991 trace_intel_gpu_freq_change(intel_gpu_freq(dev_priv, val));
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004992}
4993
Chris Wilsondc979972016-05-10 14:10:04 +01004994static void valleyview_set_rps(struct drm_i915_private *dev_priv, u8 val)
Ville Syrjäläffe02b42015-02-02 19:09:50 +02004995{
Ville Syrjäläffe02b42015-02-02 19:09:50 +02004996 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Chris Wilsonaed242f2015-03-18 09:48:21 +00004997 WARN_ON(val > dev_priv->rps.max_freq);
4998 WARN_ON(val < dev_priv->rps.min_freq);
Ville Syrjäläffe02b42015-02-02 19:09:50 +02004999
Chris Wilsondc979972016-05-10 14:10:04 +01005000 if (WARN_ONCE(IS_CHERRYVIEW(dev_priv) && (val & 1),
Ville Syrjäläffe02b42015-02-02 19:09:50 +02005001 "Odd GPU freq value\n"))
5002 val &= ~1;
5003
Deepak Scd25dd52015-07-10 18:31:40 +05305004 I915_WRITE(GEN6_PMINTRMSK, gen6_rps_pm_mask(dev_priv, val));
5005
Chris Wilson8fb55192015-04-07 16:20:28 +01005006 if (val != dev_priv->rps.cur_freq) {
Ville Syrjäläffe02b42015-02-02 19:09:50 +02005007 vlv_punit_write(dev_priv, PUNIT_REG_GPU_FREQ_REQ, val);
Chris Wilson8fb55192015-04-07 16:20:28 +01005008 if (!IS_CHERRYVIEW(dev_priv))
5009 gen6_set_rps_thresholds(dev_priv, val);
5010 }
Ville Syrjäläffe02b42015-02-02 19:09:50 +02005011
Ville Syrjäläffe02b42015-02-02 19:09:50 +02005012 dev_priv->rps.cur_freq = val;
5013 trace_intel_gpu_freq_change(intel_gpu_freq(dev_priv, val));
5014}
5015
Deepak Sa7f6e232015-05-09 18:04:44 +05305016/* vlv_set_rps_idle: Set the frequency to idle, if Gfx clocks are down
Deepak S76c3552f2014-01-30 23:08:16 +05305017 *
5018 * * If Gfx is Idle, then
Deepak Sa7f6e232015-05-09 18:04:44 +05305019 * 1. Forcewake Media well.
5020 * 2. Request idle freq.
5021 * 3. Release Forcewake of Media well.
Deepak S76c3552f2014-01-30 23:08:16 +05305022*/
5023static void vlv_set_rps_idle(struct drm_i915_private *dev_priv)
5024{
Chris Wilsonaed242f2015-03-18 09:48:21 +00005025 u32 val = dev_priv->rps.idle_freq;
Deepak S5549d252014-06-28 11:26:11 +05305026
Chris Wilsonaed242f2015-03-18 09:48:21 +00005027 if (dev_priv->rps.cur_freq <= val)
Deepak S76c3552f2014-01-30 23:08:16 +05305028 return;
5029
Deepak Sa7f6e232015-05-09 18:04:44 +05305030 /* Wake up the media well, as that takes a lot less
5031 * power than the Render well. */
5032 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_MEDIA);
Chris Wilsondc979972016-05-10 14:10:04 +01005033 valleyview_set_rps(dev_priv, val);
Deepak Sa7f6e232015-05-09 18:04:44 +05305034 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_MEDIA);
Deepak S76c3552f2014-01-30 23:08:16 +05305035}
5036
Chris Wilson43cf3bf2015-03-18 09:48:22 +00005037void gen6_rps_busy(struct drm_i915_private *dev_priv)
5038{
5039 mutex_lock(&dev_priv->rps.hw_lock);
5040 if (dev_priv->rps.enabled) {
5041 if (dev_priv->pm_rps_events & (GEN6_PM_RP_DOWN_EI_EXPIRED | GEN6_PM_RP_UP_EI_EXPIRED))
5042 gen6_rps_reset_ei(dev_priv);
5043 I915_WRITE(GEN6_PMINTRMSK,
5044 gen6_rps_pm_mask(dev_priv, dev_priv->rps.cur_freq));
Michał Winiarski2b83c4c2016-06-20 11:58:27 +02005045
Chris Wilsonc33d2472016-07-04 08:08:36 +01005046 gen6_enable_rps_interrupts(dev_priv);
5047
Michał Winiarski2b83c4c2016-06-20 11:58:27 +02005048 /* Ensure we start at the user's desired frequency */
5049 intel_set_rps(dev_priv,
5050 clamp(dev_priv->rps.cur_freq,
5051 dev_priv->rps.min_freq_softlimit,
5052 dev_priv->rps.max_freq_softlimit));
Chris Wilson43cf3bf2015-03-18 09:48:22 +00005053 }
5054 mutex_unlock(&dev_priv->rps.hw_lock);
5055}
5056
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005057void gen6_rps_idle(struct drm_i915_private *dev_priv)
5058{
Chris Wilsonc33d2472016-07-04 08:08:36 +01005059 /* Flush our bottom-half so that it does not race with us
5060 * setting the idle frequency and so that it is bounded by
5061 * our rpm wakeref. And then disable the interrupts to stop any
5062 * futher RPS reclocking whilst we are asleep.
5063 */
5064 gen6_disable_rps_interrupts(dev_priv);
5065
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005066 mutex_lock(&dev_priv->rps.hw_lock);
Chris Wilsonc0951f02013-10-10 21:58:50 +01005067 if (dev_priv->rps.enabled) {
Chris Wilsondc979972016-05-10 14:10:04 +01005068 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Deepak S76c3552f2014-01-30 23:08:16 +05305069 vlv_set_rps_idle(dev_priv);
Daniel Vetter7526ed72014-09-29 15:07:19 +02005070 else
Chris Wilsondc979972016-05-10 14:10:04 +01005071 gen6_set_rps(dev_priv, dev_priv->rps.idle_freq);
Chris Wilsonc0951f02013-10-10 21:58:50 +01005072 dev_priv->rps.last_adj = 0;
Ville Syrjälä12c100b2016-05-23 17:42:48 +03005073 I915_WRITE(GEN6_PMINTRMSK,
5074 gen6_sanitize_rps_pm_mask(dev_priv, ~0));
Chris Wilsonc0951f02013-10-10 21:58:50 +01005075 }
Chris Wilson8d3afd72015-05-21 21:01:47 +01005076 mutex_unlock(&dev_priv->rps.hw_lock);
Chris Wilson1854d5c2015-04-07 16:20:32 +01005077
Chris Wilson8d3afd72015-05-21 21:01:47 +01005078 spin_lock(&dev_priv->rps.client_lock);
Chris Wilson1854d5c2015-04-07 16:20:32 +01005079 while (!list_empty(&dev_priv->rps.clients))
5080 list_del_init(dev_priv->rps.clients.next);
Chris Wilson8d3afd72015-05-21 21:01:47 +01005081 spin_unlock(&dev_priv->rps.client_lock);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005082}
5083
Chris Wilson1854d5c2015-04-07 16:20:32 +01005084void gen6_rps_boost(struct drm_i915_private *dev_priv,
Chris Wilsone61b9952015-04-27 13:41:24 +01005085 struct intel_rps_client *rps,
5086 unsigned long submitted)
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005087{
Chris Wilson8d3afd72015-05-21 21:01:47 +01005088 /* This is intentionally racy! We peek at the state here, then
5089 * validate inside the RPS worker.
5090 */
Chris Wilson67d97da2016-07-04 08:08:31 +01005091 if (!(dev_priv->gt.awake &&
Chris Wilson8d3afd72015-05-21 21:01:47 +01005092 dev_priv->rps.enabled &&
Chris Wilson29ecd78d2016-07-13 09:10:35 +01005093 dev_priv->rps.cur_freq < dev_priv->rps.boost_freq))
Chris Wilson8d3afd72015-05-21 21:01:47 +01005094 return;
Chris Wilson43cf3bf2015-03-18 09:48:22 +00005095
Chris Wilsone61b9952015-04-27 13:41:24 +01005096 /* Force a RPS boost (and don't count it against the client) if
5097 * the GPU is severely congested.
5098 */
Chris Wilsond0bc54f2015-05-21 21:01:48 +01005099 if (rps && time_after(jiffies, submitted + DRM_I915_THROTTLE_JIFFIES))
Chris Wilsone61b9952015-04-27 13:41:24 +01005100 rps = NULL;
5101
Chris Wilson8d3afd72015-05-21 21:01:47 +01005102 spin_lock(&dev_priv->rps.client_lock);
5103 if (rps == NULL || list_empty(&rps->link)) {
5104 spin_lock_irq(&dev_priv->irq_lock);
5105 if (dev_priv->rps.interrupts_enabled) {
5106 dev_priv->rps.client_boost = true;
Chris Wilsonc33d2472016-07-04 08:08:36 +01005107 schedule_work(&dev_priv->rps.work);
Chris Wilson8d3afd72015-05-21 21:01:47 +01005108 }
5109 spin_unlock_irq(&dev_priv->irq_lock);
Chris Wilson1854d5c2015-04-07 16:20:32 +01005110
Chris Wilson2e1b8732015-04-27 13:41:22 +01005111 if (rps != NULL) {
5112 list_add(&rps->link, &dev_priv->rps.clients);
5113 rps->boosts++;
Chris Wilson1854d5c2015-04-07 16:20:32 +01005114 } else
5115 dev_priv->rps.boosts++;
Chris Wilsonc0951f02013-10-10 21:58:50 +01005116 }
Chris Wilson8d3afd72015-05-21 21:01:47 +01005117 spin_unlock(&dev_priv->rps.client_lock);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005118}
5119
Chris Wilsondc979972016-05-10 14:10:04 +01005120void intel_set_rps(struct drm_i915_private *dev_priv, u8 val)
Jesse Barnes0a073b82013-04-17 15:54:58 -07005121{
Chris Wilsondc979972016-05-10 14:10:04 +01005122 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
5123 valleyview_set_rps(dev_priv, val);
Ville Syrjäläffe02b42015-02-02 19:09:50 +02005124 else
Chris Wilsondc979972016-05-10 14:10:04 +01005125 gen6_set_rps(dev_priv, val);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005126}
5127
Chris Wilsondc979972016-05-10 14:10:04 +01005128static void gen9_disable_rc6(struct drm_i915_private *dev_priv)
Zhe Wang20e49362014-11-04 17:07:05 +00005129{
Zhe Wang20e49362014-11-04 17:07:05 +00005130 I915_WRITE(GEN6_RC_CONTROL, 0);
Zhe Wang38c23522015-01-20 12:23:04 +00005131 I915_WRITE(GEN9_PG_ENABLE, 0);
Zhe Wang20e49362014-11-04 17:07:05 +00005132}
5133
Chris Wilsondc979972016-05-10 14:10:04 +01005134static void gen9_disable_rps(struct drm_i915_private *dev_priv)
Akash Goel2030d682016-04-23 00:05:45 +05305135{
Akash Goel2030d682016-04-23 00:05:45 +05305136 I915_WRITE(GEN6_RP_CONTROL, 0);
5137}
5138
Chris Wilsondc979972016-05-10 14:10:04 +01005139static void gen6_disable_rps(struct drm_i915_private *dev_priv)
Daniel Vetter44fc7d52013-07-12 22:43:27 +02005140{
Daniel Vetter44fc7d52013-07-12 22:43:27 +02005141 I915_WRITE(GEN6_RC_CONTROL, 0);
5142 I915_WRITE(GEN6_RPNSWREQ, 1 << 31);
Akash Goel2030d682016-04-23 00:05:45 +05305143 I915_WRITE(GEN6_RP_CONTROL, 0);
Daniel Vetter44fc7d52013-07-12 22:43:27 +02005144}
5145
Chris Wilsondc979972016-05-10 14:10:04 +01005146static void cherryview_disable_rps(struct drm_i915_private *dev_priv)
Deepak S38807742014-05-23 21:00:15 +05305147{
Deepak S38807742014-05-23 21:00:15 +05305148 I915_WRITE(GEN6_RC_CONTROL, 0);
5149}
5150
Chris Wilsondc979972016-05-10 14:10:04 +01005151static void valleyview_disable_rps(struct drm_i915_private *dev_priv)
Jesse Barnesd20d4f02013-04-23 10:09:28 -07005152{
Deepak S98a2e5f2014-08-18 10:35:27 -07005153 /* we're doing forcewake before Disabling RC6,
5154 * This what the BIOS expects when going into suspend */
Mika Kuoppala59bad942015-01-16 11:34:40 +02005155 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Deepak S98a2e5f2014-08-18 10:35:27 -07005156
Jesse Barnesd20d4f02013-04-23 10:09:28 -07005157 I915_WRITE(GEN6_RC_CONTROL, 0);
Jesse Barnesd20d4f02013-04-23 10:09:28 -07005158
Mika Kuoppala59bad942015-01-16 11:34:40 +02005159 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Jesse Barnesd20d4f02013-04-23 10:09:28 -07005160}
5161
Chris Wilsondc979972016-05-10 14:10:04 +01005162static void intel_print_rc6_info(struct drm_i915_private *dev_priv, u32 mode)
Ben Widawskydc39fff2013-10-18 12:32:07 -07005163{
Chris Wilsondc979972016-05-10 14:10:04 +01005164 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Imre Deak91ca6892014-04-14 20:24:25 +03005165 if (mode & (GEN7_RC_CTL_TO_MODE | GEN6_RC_CTL_EI_MODE(1)))
5166 mode = GEN6_RC_CTL_RC6_ENABLE;
5167 else
5168 mode = 0;
5169 }
Chris Wilsondc979972016-05-10 14:10:04 +01005170 if (HAS_RC6p(dev_priv))
Imre Deakb99d49c2016-06-29 19:13:54 +03005171 DRM_DEBUG_DRIVER("Enabling RC6 states: "
5172 "RC6 %s RC6p %s RC6pp %s\n",
5173 onoff(mode & GEN6_RC_CTL_RC6_ENABLE),
5174 onoff(mode & GEN6_RC_CTL_RC6p_ENABLE),
5175 onoff(mode & GEN6_RC_CTL_RC6pp_ENABLE));
Rodrigo Vivi58abf1d2014-10-07 07:06:50 -07005176
5177 else
Imre Deakb99d49c2016-06-29 19:13:54 +03005178 DRM_DEBUG_DRIVER("Enabling RC6 states: RC6 %s\n",
5179 onoff(mode & GEN6_RC_CTL_RC6_ENABLE));
Ben Widawskydc39fff2013-10-18 12:32:07 -07005180}
5181
Chris Wilsondc979972016-05-10 14:10:04 +01005182static bool bxt_check_bios_rc6_setup(struct drm_i915_private *dev_priv)
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05305183{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03005184 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05305185 bool enable_rc6 = true;
5186 unsigned long rc6_ctx_base;
Imre Deakfc619842016-06-29 19:13:55 +03005187 u32 rc_ctl;
5188 int rc_sw_target;
5189
5190 rc_ctl = I915_READ(GEN6_RC_CONTROL);
5191 rc_sw_target = (I915_READ(GEN6_RC_STATE) & RC_SW_TARGET_STATE_MASK) >>
5192 RC_SW_TARGET_STATE_SHIFT;
5193 DRM_DEBUG_DRIVER("BIOS enabled RC states: "
5194 "HW_CTRL %s HW_RC6 %s SW_TARGET_STATE %x\n",
5195 onoff(rc_ctl & GEN6_RC_CTL_HW_ENABLE),
5196 onoff(rc_ctl & GEN6_RC_CTL_RC6_ENABLE),
5197 rc_sw_target);
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05305198
5199 if (!(I915_READ(RC6_LOCATION) & RC6_CTX_IN_DRAM)) {
Imre Deakb99d49c2016-06-29 19:13:54 +03005200 DRM_DEBUG_DRIVER("RC6 Base location not set properly.\n");
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05305201 enable_rc6 = false;
5202 }
5203
5204 /*
5205 * The exact context size is not known for BXT, so assume a page size
5206 * for this check.
5207 */
5208 rc6_ctx_base = I915_READ(RC6_CTX_BASE) & RC6_CTX_BASE_MASK;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03005209 if (!((rc6_ctx_base >= ggtt->stolen_reserved_base) &&
5210 (rc6_ctx_base + PAGE_SIZE <= ggtt->stolen_reserved_base +
5211 ggtt->stolen_reserved_size))) {
Imre Deakb99d49c2016-06-29 19:13:54 +03005212 DRM_DEBUG_DRIVER("RC6 Base address not as expected.\n");
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05305213 enable_rc6 = false;
5214 }
5215
5216 if (!(((I915_READ(PWRCTX_MAXCNT_RCSUNIT) & IDLE_TIME_MASK) > 1) &&
5217 ((I915_READ(PWRCTX_MAXCNT_VCSUNIT0) & IDLE_TIME_MASK) > 1) &&
5218 ((I915_READ(PWRCTX_MAXCNT_BCSUNIT) & IDLE_TIME_MASK) > 1) &&
5219 ((I915_READ(PWRCTX_MAXCNT_VECSUNIT) & IDLE_TIME_MASK) > 1))) {
Imre Deakb99d49c2016-06-29 19:13:54 +03005220 DRM_DEBUG_DRIVER("Engine Idle wait time not set properly.\n");
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05305221 enable_rc6 = false;
5222 }
5223
Imre Deakfc619842016-06-29 19:13:55 +03005224 if (!I915_READ(GEN8_PUSHBUS_CONTROL) ||
5225 !I915_READ(GEN8_PUSHBUS_ENABLE) ||
5226 !I915_READ(GEN8_PUSHBUS_SHIFT)) {
5227 DRM_DEBUG_DRIVER("Pushbus not setup properly.\n");
5228 enable_rc6 = false;
5229 }
5230
5231 if (!I915_READ(GEN6_GFXPAUSE)) {
5232 DRM_DEBUG_DRIVER("GFX pause not setup properly.\n");
5233 enable_rc6 = false;
5234 }
5235
5236 if (!I915_READ(GEN8_MISC_CTRL0)) {
5237 DRM_DEBUG_DRIVER("GPM control not setup properly.\n");
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05305238 enable_rc6 = false;
5239 }
5240
5241 return enable_rc6;
5242}
5243
Chris Wilsondc979972016-05-10 14:10:04 +01005244int sanitize_rc6_option(struct drm_i915_private *dev_priv, int enable_rc6)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005245{
Daniel Vettere7d66d82015-06-15 23:23:54 +02005246 /* No RC6 before Ironlake and code is gone for ilk. */
Chris Wilsondc979972016-05-10 14:10:04 +01005247 if (INTEL_INFO(dev_priv)->gen < 6)
Imre Deake6069ca2014-04-18 16:01:02 +03005248 return 0;
5249
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05305250 if (!enable_rc6)
5251 return 0;
5252
Chris Wilsondc979972016-05-10 14:10:04 +01005253 if (IS_BROXTON(dev_priv) && !bxt_check_bios_rc6_setup(dev_priv)) {
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05305254 DRM_INFO("RC6 disabled by BIOS\n");
5255 return 0;
5256 }
5257
Daniel Vetter456470e2012-08-08 23:35:40 +02005258 /* Respect the kernel parameter if it is set */
Imre Deake6069ca2014-04-18 16:01:02 +03005259 if (enable_rc6 >= 0) {
5260 int mask;
5261
Chris Wilsondc979972016-05-10 14:10:04 +01005262 if (HAS_RC6p(dev_priv))
Imre Deake6069ca2014-04-18 16:01:02 +03005263 mask = INTEL_RC6_ENABLE | INTEL_RC6p_ENABLE |
5264 INTEL_RC6pp_ENABLE;
5265 else
5266 mask = INTEL_RC6_ENABLE;
5267
5268 if ((enable_rc6 & mask) != enable_rc6)
Imre Deakb99d49c2016-06-29 19:13:54 +03005269 DRM_DEBUG_DRIVER("Adjusting RC6 mask to %d "
5270 "(requested %d, valid %d)\n",
5271 enable_rc6 & mask, enable_rc6, mask);
Imre Deake6069ca2014-04-18 16:01:02 +03005272
5273 return enable_rc6 & mask;
5274 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005275
Chris Wilsondc979972016-05-10 14:10:04 +01005276 if (IS_IVYBRIDGE(dev_priv))
Ben Widawskycca84a12014-01-28 20:25:38 -08005277 return (INTEL_RC6_ENABLE | INTEL_RC6p_ENABLE);
Ben Widawsky8bade1a2014-01-28 20:25:39 -08005278
5279 return INTEL_RC6_ENABLE;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005280}
5281
Chris Wilsondc979972016-05-10 14:10:04 +01005282static void gen6_init_rps_frequencies(struct drm_i915_private *dev_priv)
Imre Deake6069ca2014-04-18 16:01:02 +03005283{
Ben Widawsky3280e8b2014-03-31 17:16:42 -07005284 /* All of these values are in units of 50MHz */
Chris Wilson773ea9a2016-07-13 09:10:33 +01005285
Tom O'Rourke93ee2922014-11-19 14:21:52 -08005286 /* static values from HW: RP0 > RP1 > RPn (min_freq) */
Chris Wilsondc979972016-05-10 14:10:04 +01005287 if (IS_BROXTON(dev_priv)) {
Chris Wilson773ea9a2016-07-13 09:10:33 +01005288 u32 rp_state_cap = I915_READ(BXT_RP_STATE_CAP);
Bob Paauwe35040562015-06-25 14:54:07 -07005289 dev_priv->rps.rp0_freq = (rp_state_cap >> 16) & 0xff;
5290 dev_priv->rps.rp1_freq = (rp_state_cap >> 8) & 0xff;
5291 dev_priv->rps.min_freq = (rp_state_cap >> 0) & 0xff;
5292 } else {
Chris Wilson773ea9a2016-07-13 09:10:33 +01005293 u32 rp_state_cap = I915_READ(GEN6_RP_STATE_CAP);
Bob Paauwe35040562015-06-25 14:54:07 -07005294 dev_priv->rps.rp0_freq = (rp_state_cap >> 0) & 0xff;
5295 dev_priv->rps.rp1_freq = (rp_state_cap >> 8) & 0xff;
5296 dev_priv->rps.min_freq = (rp_state_cap >> 16) & 0xff;
5297 }
Ben Widawsky3280e8b2014-03-31 17:16:42 -07005298 /* hw_max = RP0 until we check for overclocking */
Chris Wilson773ea9a2016-07-13 09:10:33 +01005299 dev_priv->rps.max_freq = dev_priv->rps.rp0_freq;
Ben Widawsky3280e8b2014-03-31 17:16:42 -07005300
Tom O'Rourke93ee2922014-11-19 14:21:52 -08005301 dev_priv->rps.efficient_freq = dev_priv->rps.rp1_freq;
Chris Wilsondc979972016-05-10 14:10:04 +01005302 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv) ||
5303 IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) {
Chris Wilson773ea9a2016-07-13 09:10:33 +01005304 u32 ddcc_status = 0;
5305
5306 if (sandybridge_pcode_read(dev_priv,
5307 HSW_PCODE_DYNAMIC_DUTY_CYCLE_CONTROL,
5308 &ddcc_status) == 0)
Tom O'Rourke93ee2922014-11-19 14:21:52 -08005309 dev_priv->rps.efficient_freq =
Tom O'Rourke46efa4a2015-02-10 23:06:46 -08005310 clamp_t(u8,
5311 ((ddcc_status >> 8) & 0xff),
5312 dev_priv->rps.min_freq,
5313 dev_priv->rps.max_freq);
Tom O'Rourke93ee2922014-11-19 14:21:52 -08005314 }
5315
Chris Wilsondc979972016-05-10 14:10:04 +01005316 if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) {
Akash Goelc5e06882015-06-29 14:50:19 +05305317 /* Store the frequency values in 16.66 MHZ units, which is
Chris Wilson773ea9a2016-07-13 09:10:33 +01005318 * the natural hardware unit for SKL
5319 */
Akash Goelc5e06882015-06-29 14:50:19 +05305320 dev_priv->rps.rp0_freq *= GEN9_FREQ_SCALER;
5321 dev_priv->rps.rp1_freq *= GEN9_FREQ_SCALER;
5322 dev_priv->rps.min_freq *= GEN9_FREQ_SCALER;
5323 dev_priv->rps.max_freq *= GEN9_FREQ_SCALER;
5324 dev_priv->rps.efficient_freq *= GEN9_FREQ_SCALER;
5325 }
Ben Widawsky3280e8b2014-03-31 17:16:42 -07005326}
5327
Chris Wilson3a45b052016-07-13 09:10:32 +01005328static void reset_rps(struct drm_i915_private *dev_priv,
5329 void (*set)(struct drm_i915_private *, u8))
5330{
5331 u8 freq = dev_priv->rps.cur_freq;
5332
5333 /* force a reset */
5334 dev_priv->rps.power = -1;
5335 dev_priv->rps.cur_freq = -1;
5336
5337 set(dev_priv, freq);
5338}
5339
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00005340/* See the Gen9_GT_PM_Programming_Guide doc for the below */
Chris Wilsondc979972016-05-10 14:10:04 +01005341static void gen9_enable_rps(struct drm_i915_private *dev_priv)
Zhe Wang20e49362014-11-04 17:07:05 +00005342{
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00005343 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
5344
Sagar Arun Kamble23eafea2015-08-23 17:52:48 +05305345 /* WaGsvDisableTurbo: Workaround to disable turbo on BXT A* */
Chris Wilsondc979972016-05-10 14:10:04 +01005346 if (IS_BXT_REVID(dev_priv, 0, BXT_REVID_A1)) {
Akash Goel2030d682016-04-23 00:05:45 +05305347 /*
5348 * BIOS could leave the Hw Turbo enabled, so need to explicitly
5349 * clear out the Control register just to avoid inconsitency
5350 * with debugfs interface, which will show Turbo as enabled
5351 * only and that is not expected by the User after adding the
5352 * WaGsvDisableTurbo. Apart from this there is no problem even
5353 * if the Turbo is left enabled in the Control register, as the
5354 * Up/Down interrupts would remain masked.
5355 */
Chris Wilsondc979972016-05-10 14:10:04 +01005356 gen9_disable_rps(dev_priv);
Sagar Arun Kamble23eafea2015-08-23 17:52:48 +05305357 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
5358 return;
5359 }
5360
Akash Goel0beb0592015-03-06 11:07:20 +05305361 /* Program defaults and thresholds for RPS*/
5362 I915_WRITE(GEN6_RC_VIDEO_FREQ,
5363 GEN9_FREQUENCY(dev_priv->rps.rp1_freq));
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00005364
Akash Goel0beb0592015-03-06 11:07:20 +05305365 /* 1 second timeout*/
5366 I915_WRITE(GEN6_RP_DOWN_TIMEOUT,
5367 GT_INTERVAL_FROM_US(dev_priv, 1000000));
5368
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00005369 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 0xa);
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00005370
Akash Goel0beb0592015-03-06 11:07:20 +05305371 /* Leaning on the below call to gen6_set_rps to program/setup the
5372 * Up/Down EI & threshold registers, as well as the RP_CONTROL,
5373 * RP_INTERRUPT_LIMITS & RPNSWREQ registers */
Chris Wilson3a45b052016-07-13 09:10:32 +01005374 reset_rps(dev_priv, gen6_set_rps);
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00005375
5376 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
5377}
5378
Chris Wilsondc979972016-05-10 14:10:04 +01005379static void gen9_enable_rc6(struct drm_i915_private *dev_priv)
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00005380{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00005381 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05305382 enum intel_engine_id id;
Zhe Wang20e49362014-11-04 17:07:05 +00005383 uint32_t rc6_mask = 0;
Zhe Wang20e49362014-11-04 17:07:05 +00005384
5385 /* 1a: Software RC state - RC0 */
5386 I915_WRITE(GEN6_RC_STATE, 0);
5387
5388 /* 1b: Get forcewake during program sequence. Although the driver
5389 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
Mika Kuoppala59bad942015-01-16 11:34:40 +02005390 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Zhe Wang20e49362014-11-04 17:07:05 +00005391
5392 /* 2a: Disable RC states. */
5393 I915_WRITE(GEN6_RC_CONTROL, 0);
5394
5395 /* 2b: Program RC6 thresholds.*/
Sagar Arun Kamble63a4dec2015-09-12 10:17:53 +05305396
5397 /* WaRsDoubleRc6WrlWithCoarsePowerGating: Doubling WRL only when CPG is enabled */
Chris Wilsondc979972016-05-10 14:10:04 +01005398 if (IS_SKYLAKE(dev_priv))
Sagar Arun Kamble63a4dec2015-09-12 10:17:53 +05305399 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 108 << 16);
5400 else
5401 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 54 << 16);
Zhe Wang20e49362014-11-04 17:07:05 +00005402 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
5403 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
Akash Goel3b3f1652016-10-13 22:44:48 +05305404 for_each_engine(engine, dev_priv, id)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00005405 I915_WRITE(RING_MAX_IDLE(engine->mmio_base), 10);
Sagar Arun Kamble97c322e2015-09-12 10:17:54 +05305406
Dave Gordon1a3d1892016-05-13 15:36:30 +01005407 if (HAS_GUC(dev_priv))
Sagar Arun Kamble97c322e2015-09-12 10:17:54 +05305408 I915_WRITE(GUC_MAX_IDLE_COUNT, 0xA);
5409
Zhe Wang20e49362014-11-04 17:07:05 +00005410 I915_WRITE(GEN6_RC_SLEEP, 0);
Zhe Wang20e49362014-11-04 17:07:05 +00005411
Zhe Wang38c23522015-01-20 12:23:04 +00005412 /* 2c: Program Coarse Power Gating Policies. */
5413 I915_WRITE(GEN9_MEDIA_PG_IDLE_HYSTERESIS, 25);
5414 I915_WRITE(GEN9_RENDER_PG_IDLE_HYSTERESIS, 25);
5415
Zhe Wang20e49362014-11-04 17:07:05 +00005416 /* 3a: Enable RC6 */
Chris Wilsondc979972016-05-10 14:10:04 +01005417 if (intel_enable_rc6() & INTEL_RC6_ENABLE)
Zhe Wang20e49362014-11-04 17:07:05 +00005418 rc6_mask = GEN6_RC_CTL_RC6_ENABLE;
Jani Nikula87ad3212016-01-14 12:53:34 +02005419 DRM_INFO("RC6 %s\n", onoff(rc6_mask & GEN6_RC_CTL_RC6_ENABLE));
Jani Nikula4ff40a42016-09-26 15:07:51 +03005420 /* WaRsUseTimeoutMode:bxt */
Jani Nikula9fc736e2016-09-16 16:59:46 +03005421 if (IS_BXT_REVID(dev_priv, 0, BXT_REVID_A1)) {
Sagar Arun Kamble3e7732a2015-10-01 20:29:27 +05305422 I915_WRITE(GEN6_RC6_THRESHOLD, 625); /* 800us */
Sagar Arun Kamblee3429cd2015-09-12 10:17:52 +05305423 I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
5424 GEN7_RC_CTL_TO_MODE |
5425 rc6_mask);
Sagar Arun Kamble3e7732a2015-10-01 20:29:27 +05305426 } else {
5427 I915_WRITE(GEN6_RC6_THRESHOLD, 37500); /* 37.5/125ms per EI */
Sagar Arun Kamblee3429cd2015-09-12 10:17:52 +05305428 I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
5429 GEN6_RC_CTL_EI_MODE(1) |
5430 rc6_mask);
Sagar Arun Kamble3e7732a2015-10-01 20:29:27 +05305431 }
Zhe Wang20e49362014-11-04 17:07:05 +00005432
Sagar Kamblecb07bae2015-04-12 11:28:14 +05305433 /*
5434 * 3b: Enable Coarse Power Gating only when RC6 is enabled.
Sagar Arun Kamblef2d2fe92015-09-12 10:17:51 +05305435 * WaRsDisableCoarsePowerGating:skl,bxt - Render/Media PG need to be disabled with RC6.
Sagar Kamblecb07bae2015-04-12 11:28:14 +05305436 */
Chris Wilsondc979972016-05-10 14:10:04 +01005437 if (NEEDS_WaRsDisableCoarsePowerGating(dev_priv))
Sagar Arun Kamblef2d2fe92015-09-12 10:17:51 +05305438 I915_WRITE(GEN9_PG_ENABLE, 0);
5439 else
5440 I915_WRITE(GEN9_PG_ENABLE, (rc6_mask & GEN6_RC_CTL_RC6_ENABLE) ?
5441 (GEN9_RENDER_PG_ENABLE | GEN9_MEDIA_PG_ENABLE) : 0);
Zhe Wang38c23522015-01-20 12:23:04 +00005442
Mika Kuoppala59bad942015-01-16 11:34:40 +02005443 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Zhe Wang20e49362014-11-04 17:07:05 +00005444}
5445
Chris Wilsondc979972016-05-10 14:10:04 +01005446static void gen8_enable_rps(struct drm_i915_private *dev_priv)
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005447{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00005448 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05305449 enum intel_engine_id id;
Tom O'Rourke93ee2922014-11-19 14:21:52 -08005450 uint32_t rc6_mask = 0;
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005451
5452 /* 1a: Software RC state - RC0 */
5453 I915_WRITE(GEN6_RC_STATE, 0);
5454
5455 /* 1c & 1d: Get forcewake during program sequence. Although the driver
5456 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
Mika Kuoppala59bad942015-01-16 11:34:40 +02005457 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005458
5459 /* 2a: Disable RC states. */
5460 I915_WRITE(GEN6_RC_CONTROL, 0);
5461
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005462 /* 2b: Program RC6 thresholds.*/
5463 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16);
5464 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
5465 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
Akash Goel3b3f1652016-10-13 22:44:48 +05305466 for_each_engine(engine, dev_priv, id)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00005467 I915_WRITE(RING_MAX_IDLE(engine->mmio_base), 10);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005468 I915_WRITE(GEN6_RC_SLEEP, 0);
Chris Wilsondc979972016-05-10 14:10:04 +01005469 if (IS_BROADWELL(dev_priv))
Tom O'Rourke0d68b252014-04-09 11:44:06 -07005470 I915_WRITE(GEN6_RC6_THRESHOLD, 625); /* 800us/1.28 for TO */
5471 else
5472 I915_WRITE(GEN6_RC6_THRESHOLD, 50000); /* 50/125ms per EI */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005473
5474 /* 3: Enable RC6 */
Chris Wilsondc979972016-05-10 14:10:04 +01005475 if (intel_enable_rc6() & INTEL_RC6_ENABLE)
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005476 rc6_mask = GEN6_RC_CTL_RC6_ENABLE;
Chris Wilsondc979972016-05-10 14:10:04 +01005477 intel_print_rc6_info(dev_priv, rc6_mask);
5478 if (IS_BROADWELL(dev_priv))
Tom O'Rourke0d68b252014-04-09 11:44:06 -07005479 I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
5480 GEN7_RC_CTL_TO_MODE |
5481 rc6_mask);
5482 else
5483 I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
5484 GEN6_RC_CTL_EI_MODE(1) |
5485 rc6_mask);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005486
5487 /* 4 Program defaults and thresholds for RPS*/
Ben Widawskyf9bdc582014-03-31 17:16:41 -07005488 I915_WRITE(GEN6_RPNSWREQ,
5489 HSW_FREQUENCY(dev_priv->rps.rp1_freq));
5490 I915_WRITE(GEN6_RC_VIDEO_FREQ,
5491 HSW_FREQUENCY(dev_priv->rps.rp1_freq));
Daniel Vetter7526ed72014-09-29 15:07:19 +02005492 /* NB: Docs say 1s, and 1000000 - which aren't equivalent */
5493 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 100000000 / 128); /* 1 second timeout */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005494
Daniel Vetter7526ed72014-09-29 15:07:19 +02005495 /* Docs recommend 900MHz, and 300 MHz respectively */
5496 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS,
5497 dev_priv->rps.max_freq_softlimit << 24 |
5498 dev_priv->rps.min_freq_softlimit << 16);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005499
Daniel Vetter7526ed72014-09-29 15:07:19 +02005500 I915_WRITE(GEN6_RP_UP_THRESHOLD, 7600000 / 128); /* 76ms busyness per EI, 90% */
5501 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 31300000 / 128); /* 313ms busyness per EI, 70%*/
5502 I915_WRITE(GEN6_RP_UP_EI, 66000); /* 84.48ms, XXX: random? */
5503 I915_WRITE(GEN6_RP_DOWN_EI, 350000); /* 448ms, XXX: random? */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005504
Daniel Vetter7526ed72014-09-29 15:07:19 +02005505 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005506
5507 /* 5: Enable RPS */
Daniel Vetter7526ed72014-09-29 15:07:19 +02005508 I915_WRITE(GEN6_RP_CONTROL,
5509 GEN6_RP_MEDIA_TURBO |
5510 GEN6_RP_MEDIA_HW_NORMAL_MODE |
5511 GEN6_RP_MEDIA_IS_GFX |
5512 GEN6_RP_ENABLE |
5513 GEN6_RP_UP_BUSY_AVG |
5514 GEN6_RP_DOWN_IDLE_AVG);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005515
Daniel Vetter7526ed72014-09-29 15:07:19 +02005516 /* 6: Ring frequency + overclocking (our driver does this later */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005517
Chris Wilson3a45b052016-07-13 09:10:32 +01005518 reset_rps(dev_priv, gen6_set_rps);
Daniel Vetter7526ed72014-09-29 15:07:19 +02005519
Mika Kuoppala59bad942015-01-16 11:34:40 +02005520 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005521}
5522
Chris Wilsondc979972016-05-10 14:10:04 +01005523static void gen6_enable_rps(struct drm_i915_private *dev_priv)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005524{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00005525 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05305526 enum intel_engine_id id;
Chris Wilson99ac9612016-07-13 09:10:34 +01005527 u32 rc6vids, rc6_mask = 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005528 u32 gtfifodbg;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005529 int rc6_mode;
Dave Gordonb4ac5af2016-03-24 11:20:38 +00005530 int ret;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005531
Jesse Barnes4fc688c2012-11-02 11:14:01 -07005532 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02005533
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005534 /* Here begins a magic sequence of register writes to enable
5535 * auto-downclocking.
5536 *
5537 * Perhaps there might be some value in exposing these to
5538 * userspace...
5539 */
5540 I915_WRITE(GEN6_RC_STATE, 0);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005541
5542 /* Clear the DBG now so we don't confuse earlier errors */
Ville Syrjälä297b32e2016-04-13 21:09:30 +03005543 gtfifodbg = I915_READ(GTFIFODBG);
5544 if (gtfifodbg) {
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005545 DRM_ERROR("GT fifo had a previous error %x\n", gtfifodbg);
5546 I915_WRITE(GTFIFODBG, gtfifodbg);
5547 }
5548
Mika Kuoppala59bad942015-01-16 11:34:40 +02005549 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005550
5551 /* disable the counters and set deterministic thresholds */
5552 I915_WRITE(GEN6_RC_CONTROL, 0);
5553
5554 I915_WRITE(GEN6_RC1_WAKE_RATE_LIMIT, 1000 << 16);
5555 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16 | 30);
5556 I915_WRITE(GEN6_RC6pp_WAKE_RATE_LIMIT, 30);
5557 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
5558 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
5559
Akash Goel3b3f1652016-10-13 22:44:48 +05305560 for_each_engine(engine, dev_priv, id)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00005561 I915_WRITE(RING_MAX_IDLE(engine->mmio_base), 10);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005562
5563 I915_WRITE(GEN6_RC_SLEEP, 0);
5564 I915_WRITE(GEN6_RC1e_THRESHOLD, 1000);
Chris Wilsondc979972016-05-10 14:10:04 +01005565 if (IS_IVYBRIDGE(dev_priv))
Stéphane Marchesin351aa562013-08-13 11:55:17 -07005566 I915_WRITE(GEN6_RC6_THRESHOLD, 125000);
5567 else
5568 I915_WRITE(GEN6_RC6_THRESHOLD, 50000);
Stéphane Marchesin0920a482013-01-29 19:41:59 -08005569 I915_WRITE(GEN6_RC6p_THRESHOLD, 150000);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005570 I915_WRITE(GEN6_RC6pp_THRESHOLD, 64000); /* unused */
5571
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03005572 /* Check if we are enabling RC6 */
Chris Wilsondc979972016-05-10 14:10:04 +01005573 rc6_mode = intel_enable_rc6();
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005574 if (rc6_mode & INTEL_RC6_ENABLE)
5575 rc6_mask |= GEN6_RC_CTL_RC6_ENABLE;
5576
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03005577 /* We don't use those on Haswell */
Chris Wilsondc979972016-05-10 14:10:04 +01005578 if (!IS_HASWELL(dev_priv)) {
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03005579 if (rc6_mode & INTEL_RC6p_ENABLE)
5580 rc6_mask |= GEN6_RC_CTL_RC6p_ENABLE;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005581
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03005582 if (rc6_mode & INTEL_RC6pp_ENABLE)
5583 rc6_mask |= GEN6_RC_CTL_RC6pp_ENABLE;
5584 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005585
Chris Wilsondc979972016-05-10 14:10:04 +01005586 intel_print_rc6_info(dev_priv, rc6_mask);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005587
5588 I915_WRITE(GEN6_RC_CONTROL,
5589 rc6_mask |
5590 GEN6_RC_CTL_EI_MODE(1) |
5591 GEN6_RC_CTL_HW_ENABLE);
5592
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005593 /* Power down if completely idle for over 50ms */
5594 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 50000);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005595 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005596
Chris Wilson3a45b052016-07-13 09:10:32 +01005597 reset_rps(dev_priv, gen6_set_rps);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005598
Ben Widawsky31643d52012-09-26 10:34:01 -07005599 rc6vids = 0;
5600 ret = sandybridge_pcode_read(dev_priv, GEN6_PCODE_READ_RC6VIDS, &rc6vids);
Chris Wilsondc979972016-05-10 14:10:04 +01005601 if (IS_GEN6(dev_priv) && ret) {
Ben Widawsky31643d52012-09-26 10:34:01 -07005602 DRM_DEBUG_DRIVER("Couldn't check for BIOS workaround\n");
Chris Wilsondc979972016-05-10 14:10:04 +01005603 } else if (IS_GEN6(dev_priv) && (GEN6_DECODE_RC6_VID(rc6vids & 0xff) < 450)) {
Ben Widawsky31643d52012-09-26 10:34:01 -07005604 DRM_DEBUG_DRIVER("You should update your BIOS. Correcting minimum rc6 voltage (%dmV->%dmV)\n",
5605 GEN6_DECODE_RC6_VID(rc6vids & 0xff), 450);
5606 rc6vids &= 0xffff00;
5607 rc6vids |= GEN6_ENCODE_RC6_VID(450);
5608 ret = sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_RC6VIDS, rc6vids);
5609 if (ret)
5610 DRM_ERROR("Couldn't fix incorrect rc6 voltage\n");
5611 }
5612
Mika Kuoppala59bad942015-01-16 11:34:40 +02005613 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005614}
5615
Chris Wilsonfb7404e2016-07-13 09:10:38 +01005616static void gen6_update_ring_freq(struct drm_i915_private *dev_priv)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005617{
5618 int min_freq = 15;
Chris Wilson3ebecd02013-04-12 19:10:13 +01005619 unsigned int gpu_freq;
5620 unsigned int max_ia_freq, min_ring_freq;
Akash Goel4c8c7742015-06-29 14:50:20 +05305621 unsigned int max_gpu_freq, min_gpu_freq;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005622 int scaling_factor = 180;
Ben Widawskyeda79642013-10-07 17:15:48 -03005623 struct cpufreq_policy *policy;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005624
Jesse Barnes4fc688c2012-11-02 11:14:01 -07005625 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02005626
Ben Widawskyeda79642013-10-07 17:15:48 -03005627 policy = cpufreq_cpu_get(0);
5628 if (policy) {
5629 max_ia_freq = policy->cpuinfo.max_freq;
5630 cpufreq_cpu_put(policy);
5631 } else {
5632 /*
5633 * Default to measured freq if none found, PCU will ensure we
5634 * don't go over
5635 */
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005636 max_ia_freq = tsc_khz;
Ben Widawskyeda79642013-10-07 17:15:48 -03005637 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005638
5639 /* Convert from kHz to MHz */
5640 max_ia_freq /= 1000;
5641
Ben Widawsky153b4b952013-10-22 22:05:09 -07005642 min_ring_freq = I915_READ(DCLK) & 0xf;
Ben Widawskyf6aca452013-10-02 09:25:02 -07005643 /* convert DDR frequency from units of 266.6MHz to bandwidth */
5644 min_ring_freq = mult_frac(min_ring_freq, 8, 3);
Chris Wilson3ebecd02013-04-12 19:10:13 +01005645
Chris Wilsondc979972016-05-10 14:10:04 +01005646 if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) {
Akash Goel4c8c7742015-06-29 14:50:20 +05305647 /* Convert GT frequency to 50 HZ units */
5648 min_gpu_freq = dev_priv->rps.min_freq / GEN9_FREQ_SCALER;
5649 max_gpu_freq = dev_priv->rps.max_freq / GEN9_FREQ_SCALER;
5650 } else {
5651 min_gpu_freq = dev_priv->rps.min_freq;
5652 max_gpu_freq = dev_priv->rps.max_freq;
5653 }
5654
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005655 /*
5656 * For each potential GPU frequency, load a ring frequency we'd like
5657 * to use for memory access. We do this by specifying the IA frequency
5658 * the PCU should use as a reference to determine the ring frequency.
5659 */
Akash Goel4c8c7742015-06-29 14:50:20 +05305660 for (gpu_freq = max_gpu_freq; gpu_freq >= min_gpu_freq; gpu_freq--) {
5661 int diff = max_gpu_freq - gpu_freq;
Chris Wilson3ebecd02013-04-12 19:10:13 +01005662 unsigned int ia_freq = 0, ring_freq = 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005663
Chris Wilsondc979972016-05-10 14:10:04 +01005664 if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) {
Akash Goel4c8c7742015-06-29 14:50:20 +05305665 /*
5666 * ring_freq = 2 * GT. ring_freq is in 100MHz units
5667 * No floor required for ring frequency on SKL.
5668 */
5669 ring_freq = gpu_freq;
Chris Wilsondc979972016-05-10 14:10:04 +01005670 } else if (INTEL_INFO(dev_priv)->gen >= 8) {
Ben Widawsky46c764d2013-11-02 21:07:49 -07005671 /* max(2 * GT, DDR). NB: GT is 50MHz units */
5672 ring_freq = max(min_ring_freq, gpu_freq);
Chris Wilsondc979972016-05-10 14:10:04 +01005673 } else if (IS_HASWELL(dev_priv)) {
Ben Widawskyf6aca452013-10-02 09:25:02 -07005674 ring_freq = mult_frac(gpu_freq, 5, 4);
Chris Wilson3ebecd02013-04-12 19:10:13 +01005675 ring_freq = max(min_ring_freq, ring_freq);
5676 /* leave ia_freq as the default, chosen by cpufreq */
5677 } else {
5678 /* On older processors, there is no separate ring
5679 * clock domain, so in order to boost the bandwidth
5680 * of the ring, we need to upclock the CPU (ia_freq).
5681 *
5682 * For GPU frequencies less than 750MHz,
5683 * just use the lowest ring freq.
5684 */
5685 if (gpu_freq < min_freq)
5686 ia_freq = 800;
5687 else
5688 ia_freq = max_ia_freq - ((diff * scaling_factor) / 2);
5689 ia_freq = DIV_ROUND_CLOSEST(ia_freq, 100);
5690 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005691
Ben Widawsky42c05262012-09-26 10:34:00 -07005692 sandybridge_pcode_write(dev_priv,
5693 GEN6_PCODE_WRITE_MIN_FREQ_TABLE,
Chris Wilson3ebecd02013-04-12 19:10:13 +01005694 ia_freq << GEN6_PCODE_FREQ_IA_RATIO_SHIFT |
5695 ring_freq << GEN6_PCODE_FREQ_RING_RATIO_SHIFT |
5696 gpu_freq);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005697 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005698}
5699
Ville Syrjälä03af2042014-06-28 02:03:53 +03005700static int cherryview_rps_max_freq(struct drm_i915_private *dev_priv)
Deepak S2b6b3a02014-05-27 15:59:30 +05305701{
5702 u32 val, rp0;
5703
Jani Nikula5b5929c2015-10-07 11:17:46 +03005704 val = vlv_punit_read(dev_priv, FB_GFX_FMAX_AT_VMAX_FUSE);
Deepak S2b6b3a02014-05-27 15:59:30 +05305705
Imre Deak43b67992016-08-31 19:13:02 +03005706 switch (INTEL_INFO(dev_priv)->sseu.eu_total) {
Jani Nikula5b5929c2015-10-07 11:17:46 +03005707 case 8:
5708 /* (2 * 4) config */
5709 rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS4EU_FUSE_SHIFT);
5710 break;
5711 case 12:
5712 /* (2 * 6) config */
5713 rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS6EU_FUSE_SHIFT);
5714 break;
5715 case 16:
5716 /* (2 * 8) config */
5717 default:
5718 /* Setting (2 * 8) Min RP0 for any other combination */
5719 rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS8EU_FUSE_SHIFT);
5720 break;
Deepak S095acd52015-01-17 11:05:59 +05305721 }
Jani Nikula5b5929c2015-10-07 11:17:46 +03005722
5723 rp0 = (rp0 & FB_GFX_FREQ_FUSE_MASK);
5724
Deepak S2b6b3a02014-05-27 15:59:30 +05305725 return rp0;
5726}
5727
5728static int cherryview_rps_rpe_freq(struct drm_i915_private *dev_priv)
5729{
5730 u32 val, rpe;
5731
5732 val = vlv_punit_read(dev_priv, PUNIT_GPU_DUTYCYCLE_REG);
5733 rpe = (val >> PUNIT_GPU_DUTYCYCLE_RPE_FREQ_SHIFT) & PUNIT_GPU_DUTYCYCLE_RPE_FREQ_MASK;
5734
5735 return rpe;
5736}
5737
Deepak S7707df42014-07-12 18:46:14 +05305738static int cherryview_rps_guar_freq(struct drm_i915_private *dev_priv)
5739{
5740 u32 val, rp1;
5741
Jani Nikula5b5929c2015-10-07 11:17:46 +03005742 val = vlv_punit_read(dev_priv, FB_GFX_FMAX_AT_VMAX_FUSE);
5743 rp1 = (val & FB_GFX_FREQ_FUSE_MASK);
5744
Deepak S7707df42014-07-12 18:46:14 +05305745 return rp1;
5746}
5747
Deepak Sf8f2b002014-07-10 13:16:21 +05305748static int valleyview_rps_guar_freq(struct drm_i915_private *dev_priv)
5749{
5750 u32 val, rp1;
5751
5752 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FREQ_FUSE);
5753
5754 rp1 = (val & FB_GFX_FGUARANTEED_FREQ_FUSE_MASK) >> FB_GFX_FGUARANTEED_FREQ_FUSE_SHIFT;
5755
5756 return rp1;
5757}
5758
Ville Syrjälä03af2042014-06-28 02:03:53 +03005759static int valleyview_rps_max_freq(struct drm_i915_private *dev_priv)
Jesse Barnes0a073b82013-04-17 15:54:58 -07005760{
5761 u32 val, rp0;
5762
Jani Nikula64936252013-05-22 15:36:20 +03005763 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FREQ_FUSE);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005764
5765 rp0 = (val & FB_GFX_MAX_FREQ_FUSE_MASK) >> FB_GFX_MAX_FREQ_FUSE_SHIFT;
5766 /* Clamp to max */
5767 rp0 = min_t(u32, rp0, 0xea);
5768
5769 return rp0;
5770}
5771
5772static int valleyview_rps_rpe_freq(struct drm_i915_private *dev_priv)
5773{
5774 u32 val, rpe;
5775
Jani Nikula64936252013-05-22 15:36:20 +03005776 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FMAX_FUSE_LO);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005777 rpe = (val & FB_FMAX_VMIN_FREQ_LO_MASK) >> FB_FMAX_VMIN_FREQ_LO_SHIFT;
Jani Nikula64936252013-05-22 15:36:20 +03005778 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FMAX_FUSE_HI);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005779 rpe |= (val & FB_FMAX_VMIN_FREQ_HI_MASK) << 5;
5780
5781 return rpe;
5782}
5783
Ville Syrjälä03af2042014-06-28 02:03:53 +03005784static int valleyview_rps_min_freq(struct drm_i915_private *dev_priv)
Jesse Barnes0a073b82013-04-17 15:54:58 -07005785{
Imre Deak36146032014-12-04 18:39:35 +02005786 u32 val;
5787
5788 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_LFM) & 0xff;
5789 /*
5790 * According to the BYT Punit GPU turbo HAS 1.1.6.3 the minimum value
5791 * for the minimum frequency in GPLL mode is 0xc1. Contrary to this on
5792 * a BYT-M B0 the above register contains 0xbf. Moreover when setting
5793 * a frequency Punit will not allow values below 0xc0. Clamp it 0xc0
5794 * to make sure it matches what Punit accepts.
5795 */
5796 return max_t(u32, val, 0xc0);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005797}
5798
Imre Deakae484342014-03-31 15:10:44 +03005799/* Check that the pctx buffer wasn't move under us. */
5800static void valleyview_check_pctx(struct drm_i915_private *dev_priv)
5801{
5802 unsigned long pctx_addr = I915_READ(VLV_PCBR) & ~4095;
5803
5804 WARN_ON(pctx_addr != dev_priv->mm.stolen_base +
5805 dev_priv->vlv_pctx->stolen->start);
5806}
5807
Deepak S38807742014-05-23 21:00:15 +05305808
5809/* Check that the pcbr address is not empty. */
5810static void cherryview_check_pctx(struct drm_i915_private *dev_priv)
5811{
5812 unsigned long pctx_addr = I915_READ(VLV_PCBR) & ~4095;
5813
5814 WARN_ON((pctx_addr >> VLV_PCBR_ADDR_SHIFT) == 0);
5815}
5816
Chris Wilsondc979972016-05-10 14:10:04 +01005817static void cherryview_setup_pctx(struct drm_i915_private *dev_priv)
Deepak S38807742014-05-23 21:00:15 +05305818{
Joonas Lahtinen62106b42016-03-18 10:42:57 +02005819 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03005820 unsigned long pctx_paddr, paddr;
Deepak S38807742014-05-23 21:00:15 +05305821 u32 pcbr;
5822 int pctx_size = 32*1024;
5823
Deepak S38807742014-05-23 21:00:15 +05305824 pcbr = I915_READ(VLV_PCBR);
5825 if ((pcbr >> VLV_PCBR_ADDR_SHIFT) == 0) {
Ville Syrjäläce611ef2014-11-07 21:33:46 +02005826 DRM_DEBUG_DRIVER("BIOS didn't set up PCBR, fixing up\n");
Deepak S38807742014-05-23 21:00:15 +05305827 paddr = (dev_priv->mm.stolen_base +
Joonas Lahtinen62106b42016-03-18 10:42:57 +02005828 (ggtt->stolen_size - pctx_size));
Deepak S38807742014-05-23 21:00:15 +05305829
5830 pctx_paddr = (paddr & (~4095));
5831 I915_WRITE(VLV_PCBR, pctx_paddr);
5832 }
Ville Syrjäläce611ef2014-11-07 21:33:46 +02005833
5834 DRM_DEBUG_DRIVER("PCBR: 0x%08x\n", I915_READ(VLV_PCBR));
Deepak S38807742014-05-23 21:00:15 +05305835}
5836
Chris Wilsondc979972016-05-10 14:10:04 +01005837static void valleyview_setup_pctx(struct drm_i915_private *dev_priv)
Jesse Barnesc9cddff2013-05-08 10:45:13 -07005838{
Jesse Barnesc9cddff2013-05-08 10:45:13 -07005839 struct drm_i915_gem_object *pctx;
5840 unsigned long pctx_paddr;
5841 u32 pcbr;
5842 int pctx_size = 24*1024;
5843
5844 pcbr = I915_READ(VLV_PCBR);
5845 if (pcbr) {
5846 /* BIOS set it up already, grab the pre-alloc'd space */
5847 int pcbr_offset;
5848
5849 pcbr_offset = (pcbr & (~4095)) - dev_priv->mm.stolen_base;
Chris Wilson91c8a322016-07-05 10:40:23 +01005850 pctx = i915_gem_object_create_stolen_for_preallocated(&dev_priv->drm,
Jesse Barnesc9cddff2013-05-08 10:45:13 -07005851 pcbr_offset,
Daniel Vetter190d6cd2013-07-04 13:06:28 +02005852 I915_GTT_OFFSET_NONE,
Jesse Barnesc9cddff2013-05-08 10:45:13 -07005853 pctx_size);
5854 goto out;
5855 }
5856
Ville Syrjäläce611ef2014-11-07 21:33:46 +02005857 DRM_DEBUG_DRIVER("BIOS didn't set up PCBR, fixing up\n");
5858
Jesse Barnesc9cddff2013-05-08 10:45:13 -07005859 /*
5860 * From the Gunit register HAS:
5861 * The Gfx driver is expected to program this register and ensure
5862 * proper allocation within Gfx stolen memory. For example, this
5863 * register should be programmed such than the PCBR range does not
5864 * overlap with other ranges, such as the frame buffer, protected
5865 * memory, or any other relevant ranges.
5866 */
Chris Wilson91c8a322016-07-05 10:40:23 +01005867 pctx = i915_gem_object_create_stolen(&dev_priv->drm, pctx_size);
Jesse Barnesc9cddff2013-05-08 10:45:13 -07005868 if (!pctx) {
5869 DRM_DEBUG("not enough stolen space for PCTX, disabling\n");
Tvrtko Ursulinee504892016-02-11 10:27:30 +00005870 goto out;
Jesse Barnesc9cddff2013-05-08 10:45:13 -07005871 }
5872
5873 pctx_paddr = dev_priv->mm.stolen_base + pctx->stolen->start;
5874 I915_WRITE(VLV_PCBR, pctx_paddr);
5875
5876out:
Ville Syrjäläce611ef2014-11-07 21:33:46 +02005877 DRM_DEBUG_DRIVER("PCBR: 0x%08x\n", I915_READ(VLV_PCBR));
Jesse Barnesc9cddff2013-05-08 10:45:13 -07005878 dev_priv->vlv_pctx = pctx;
5879}
5880
Chris Wilsondc979972016-05-10 14:10:04 +01005881static void valleyview_cleanup_pctx(struct drm_i915_private *dev_priv)
Imre Deakae484342014-03-31 15:10:44 +03005882{
Imre Deakae484342014-03-31 15:10:44 +03005883 if (WARN_ON(!dev_priv->vlv_pctx))
5884 return;
5885
Chris Wilsonf0cd5182016-10-28 13:58:43 +01005886 i915_gem_object_put(dev_priv->vlv_pctx);
Imre Deakae484342014-03-31 15:10:44 +03005887 dev_priv->vlv_pctx = NULL;
5888}
5889
Ville Syrjäläc30fec62016-03-04 21:43:02 +02005890static void vlv_init_gpll_ref_freq(struct drm_i915_private *dev_priv)
5891{
5892 dev_priv->rps.gpll_ref_freq =
5893 vlv_get_cck_clock(dev_priv, "GPLL ref",
5894 CCK_GPLL_CLOCK_CONTROL,
5895 dev_priv->czclk_freq);
5896
5897 DRM_DEBUG_DRIVER("GPLL reference freq: %d kHz\n",
5898 dev_priv->rps.gpll_ref_freq);
5899}
5900
Chris Wilsondc979972016-05-10 14:10:04 +01005901static void valleyview_init_gt_powersave(struct drm_i915_private *dev_priv)
Imre Deak4e805192014-04-14 20:24:41 +03005902{
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03005903 u32 val;
Imre Deak4e805192014-04-14 20:24:41 +03005904
Chris Wilsondc979972016-05-10 14:10:04 +01005905 valleyview_setup_pctx(dev_priv);
Imre Deak4e805192014-04-14 20:24:41 +03005906
Ville Syrjäläc30fec62016-03-04 21:43:02 +02005907 vlv_init_gpll_ref_freq(dev_priv);
5908
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03005909 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
5910 switch ((val >> 6) & 3) {
5911 case 0:
5912 case 1:
5913 dev_priv->mem_freq = 800;
5914 break;
5915 case 2:
5916 dev_priv->mem_freq = 1066;
5917 break;
5918 case 3:
5919 dev_priv->mem_freq = 1333;
5920 break;
5921 }
Ville Syrjälä80b83b62014-11-10 22:55:14 +02005922 DRM_DEBUG_DRIVER("DDR speed: %d MHz\n", dev_priv->mem_freq);
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03005923
Imre Deak4e805192014-04-14 20:24:41 +03005924 dev_priv->rps.max_freq = valleyview_rps_max_freq(dev_priv);
5925 dev_priv->rps.rp0_freq = dev_priv->rps.max_freq;
5926 DRM_DEBUG_DRIVER("max GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02005927 intel_gpu_freq(dev_priv, dev_priv->rps.max_freq),
Imre Deak4e805192014-04-14 20:24:41 +03005928 dev_priv->rps.max_freq);
5929
5930 dev_priv->rps.efficient_freq = valleyview_rps_rpe_freq(dev_priv);
5931 DRM_DEBUG_DRIVER("RPe GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02005932 intel_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
Imre Deak4e805192014-04-14 20:24:41 +03005933 dev_priv->rps.efficient_freq);
5934
Deepak Sf8f2b002014-07-10 13:16:21 +05305935 dev_priv->rps.rp1_freq = valleyview_rps_guar_freq(dev_priv);
5936 DRM_DEBUG_DRIVER("RP1(Guar Freq) GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02005937 intel_gpu_freq(dev_priv, dev_priv->rps.rp1_freq),
Deepak Sf8f2b002014-07-10 13:16:21 +05305938 dev_priv->rps.rp1_freq);
5939
Imre Deak4e805192014-04-14 20:24:41 +03005940 dev_priv->rps.min_freq = valleyview_rps_min_freq(dev_priv);
5941 DRM_DEBUG_DRIVER("min GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02005942 intel_gpu_freq(dev_priv, dev_priv->rps.min_freq),
Imre Deak4e805192014-04-14 20:24:41 +03005943 dev_priv->rps.min_freq);
Imre Deak4e805192014-04-14 20:24:41 +03005944}
5945
Chris Wilsondc979972016-05-10 14:10:04 +01005946static void cherryview_init_gt_powersave(struct drm_i915_private *dev_priv)
Deepak S38807742014-05-23 21:00:15 +05305947{
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03005948 u32 val;
Deepak S2b6b3a02014-05-27 15:59:30 +05305949
Chris Wilsondc979972016-05-10 14:10:04 +01005950 cherryview_setup_pctx(dev_priv);
Deepak S2b6b3a02014-05-27 15:59:30 +05305951
Ville Syrjäläc30fec62016-03-04 21:43:02 +02005952 vlv_init_gpll_ref_freq(dev_priv);
5953
Ville Syrjäläa5805162015-05-26 20:42:30 +03005954 mutex_lock(&dev_priv->sb_lock);
Ville Syrjäläc6e8f392014-11-07 21:33:43 +02005955 val = vlv_cck_read(dev_priv, CCK_FUSE_REG);
Ville Syrjäläa5805162015-05-26 20:42:30 +03005956 mutex_unlock(&dev_priv->sb_lock);
Ville Syrjäläc6e8f392014-11-07 21:33:43 +02005957
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03005958 switch ((val >> 2) & 0x7) {
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03005959 case 3:
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03005960 dev_priv->mem_freq = 2000;
5961 break;
Ville Syrjäläbfa7df02015-09-24 23:29:18 +03005962 default:
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03005963 dev_priv->mem_freq = 1600;
5964 break;
5965 }
Ville Syrjälä80b83b62014-11-10 22:55:14 +02005966 DRM_DEBUG_DRIVER("DDR speed: %d MHz\n", dev_priv->mem_freq);
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03005967
Deepak S2b6b3a02014-05-27 15:59:30 +05305968 dev_priv->rps.max_freq = cherryview_rps_max_freq(dev_priv);
5969 dev_priv->rps.rp0_freq = dev_priv->rps.max_freq;
5970 DRM_DEBUG_DRIVER("max GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02005971 intel_gpu_freq(dev_priv, dev_priv->rps.max_freq),
Deepak S2b6b3a02014-05-27 15:59:30 +05305972 dev_priv->rps.max_freq);
5973
5974 dev_priv->rps.efficient_freq = cherryview_rps_rpe_freq(dev_priv);
5975 DRM_DEBUG_DRIVER("RPe GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02005976 intel_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
Deepak S2b6b3a02014-05-27 15:59:30 +05305977 dev_priv->rps.efficient_freq);
5978
Deepak S7707df42014-07-12 18:46:14 +05305979 dev_priv->rps.rp1_freq = cherryview_rps_guar_freq(dev_priv);
5980 DRM_DEBUG_DRIVER("RP1(Guar) GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02005981 intel_gpu_freq(dev_priv, dev_priv->rps.rp1_freq),
Deepak S7707df42014-07-12 18:46:14 +05305982 dev_priv->rps.rp1_freq);
5983
Deepak S5b7c91b2015-05-09 18:15:46 +05305984 /* PUnit validated range is only [RPe, RP0] */
5985 dev_priv->rps.min_freq = dev_priv->rps.efficient_freq;
Deepak S2b6b3a02014-05-27 15:59:30 +05305986 DRM_DEBUG_DRIVER("min GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02005987 intel_gpu_freq(dev_priv, dev_priv->rps.min_freq),
Deepak S2b6b3a02014-05-27 15:59:30 +05305988 dev_priv->rps.min_freq);
5989
Ville Syrjälä1c147622014-08-18 14:42:43 +03005990 WARN_ONCE((dev_priv->rps.max_freq |
5991 dev_priv->rps.efficient_freq |
5992 dev_priv->rps.rp1_freq |
5993 dev_priv->rps.min_freq) & 1,
5994 "Odd GPU freq values\n");
Deepak S38807742014-05-23 21:00:15 +05305995}
5996
Chris Wilsondc979972016-05-10 14:10:04 +01005997static void valleyview_cleanup_gt_powersave(struct drm_i915_private *dev_priv)
Imre Deak4e805192014-04-14 20:24:41 +03005998{
Chris Wilsondc979972016-05-10 14:10:04 +01005999 valleyview_cleanup_pctx(dev_priv);
Imre Deak4e805192014-04-14 20:24:41 +03006000}
6001
Chris Wilsondc979972016-05-10 14:10:04 +01006002static void cherryview_enable_rps(struct drm_i915_private *dev_priv)
Deepak S38807742014-05-23 21:00:15 +05306003{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00006004 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05306005 enum intel_engine_id id;
Deepak S2b6b3a02014-05-27 15:59:30 +05306006 u32 gtfifodbg, val, rc6_mode = 0, pcbr;
Deepak S38807742014-05-23 21:00:15 +05306007
6008 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
6009
Ville Syrjälä297b32e2016-04-13 21:09:30 +03006010 gtfifodbg = I915_READ(GTFIFODBG) & ~(GT_FIFO_SBDEDICATE_FREE_ENTRY_CHV |
6011 GT_FIFO_FREE_ENTRIES_CHV);
Deepak S38807742014-05-23 21:00:15 +05306012 if (gtfifodbg) {
6013 DRM_DEBUG_DRIVER("GT fifo had a previous error %x\n",
6014 gtfifodbg);
6015 I915_WRITE(GTFIFODBG, gtfifodbg);
6016 }
6017
6018 cherryview_check_pctx(dev_priv);
6019
6020 /* 1a & 1b: Get forcewake during program sequence. Although the driver
6021 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
Mika Kuoppala59bad942015-01-16 11:34:40 +02006022 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Deepak S38807742014-05-23 21:00:15 +05306023
Ville Syrjälä160614a2015-01-19 13:50:47 +02006024 /* Disable RC states. */
6025 I915_WRITE(GEN6_RC_CONTROL, 0);
6026
Deepak S38807742014-05-23 21:00:15 +05306027 /* 2a: Program RC6 thresholds.*/
6028 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16);
6029 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
6030 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
6031
Akash Goel3b3f1652016-10-13 22:44:48 +05306032 for_each_engine(engine, dev_priv, id)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00006033 I915_WRITE(RING_MAX_IDLE(engine->mmio_base), 10);
Deepak S38807742014-05-23 21:00:15 +05306034 I915_WRITE(GEN6_RC_SLEEP, 0);
6035
Deepak Sf4f71c72015-03-28 15:23:35 +05306036 /* TO threshold set to 500 us ( 0x186 * 1.28 us) */
6037 I915_WRITE(GEN6_RC6_THRESHOLD, 0x186);
Deepak S38807742014-05-23 21:00:15 +05306038
6039 /* allows RC6 residency counter to work */
6040 I915_WRITE(VLV_COUNTER_CONTROL,
6041 _MASKED_BIT_ENABLE(VLV_COUNT_RANGE_HIGH |
6042 VLV_MEDIA_RC6_COUNT_EN |
6043 VLV_RENDER_RC6_COUNT_EN));
6044
6045 /* For now we assume BIOS is allocating and populating the PCBR */
6046 pcbr = I915_READ(VLV_PCBR);
6047
Deepak S38807742014-05-23 21:00:15 +05306048 /* 3: Enable RC6 */
Chris Wilsondc979972016-05-10 14:10:04 +01006049 if ((intel_enable_rc6() & INTEL_RC6_ENABLE) &&
6050 (pcbr >> VLV_PCBR_ADDR_SHIFT))
Ville Syrjäläaf5a75a2015-01-19 13:50:50 +02006051 rc6_mode = GEN7_RC_CTL_TO_MODE;
Deepak S38807742014-05-23 21:00:15 +05306052
6053 I915_WRITE(GEN6_RC_CONTROL, rc6_mode);
6054
Deepak S2b6b3a02014-05-27 15:59:30 +05306055 /* 4 Program defaults and thresholds for RPS*/
Ville Syrjälä3cbdb482015-01-19 13:50:49 +02006056 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 1000000);
Deepak S2b6b3a02014-05-27 15:59:30 +05306057 I915_WRITE(GEN6_RP_UP_THRESHOLD, 59400);
6058 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 245000);
6059 I915_WRITE(GEN6_RP_UP_EI, 66000);
6060 I915_WRITE(GEN6_RP_DOWN_EI, 350000);
6061
6062 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
6063
6064 /* 5: Enable RPS */
6065 I915_WRITE(GEN6_RP_CONTROL,
6066 GEN6_RP_MEDIA_HW_NORMAL_MODE |
Ville Syrjäläeb973a52015-01-21 19:37:59 +02006067 GEN6_RP_MEDIA_IS_GFX |
Deepak S2b6b3a02014-05-27 15:59:30 +05306068 GEN6_RP_ENABLE |
6069 GEN6_RP_UP_BUSY_AVG |
6070 GEN6_RP_DOWN_IDLE_AVG);
6071
Deepak S3ef62342015-04-29 08:36:24 +05306072 /* Setting Fixed Bias */
6073 val = VLV_OVERRIDE_EN |
6074 VLV_SOC_TDP_EN |
6075 CHV_BIAS_CPU_50_SOC_50;
6076 vlv_punit_write(dev_priv, VLV_TURBO_SOC_OVERRIDE, val);
6077
Deepak S2b6b3a02014-05-27 15:59:30 +05306078 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
6079
Ville Syrjälä8d40c3a2014-11-07 21:33:45 +02006080 /* RPS code assumes GPLL is used */
6081 WARN_ONCE((val & GPLLENABLE) == 0, "GPLL not enabled\n");
6082
Jani Nikula742f4912015-09-03 11:16:09 +03006083 DRM_DEBUG_DRIVER("GPLL enabled? %s\n", yesno(val & GPLLENABLE));
Deepak S2b6b3a02014-05-27 15:59:30 +05306084 DRM_DEBUG_DRIVER("GPU status: 0x%08x\n", val);
6085
Chris Wilson3a45b052016-07-13 09:10:32 +01006086 reset_rps(dev_priv, valleyview_set_rps);
Deepak S2b6b3a02014-05-27 15:59:30 +05306087
Mika Kuoppala59bad942015-01-16 11:34:40 +02006088 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Deepak S38807742014-05-23 21:00:15 +05306089}
6090
Chris Wilsondc979972016-05-10 14:10:04 +01006091static void valleyview_enable_rps(struct drm_i915_private *dev_priv)
Jesse Barnes0a073b82013-04-17 15:54:58 -07006092{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00006093 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05306094 enum intel_engine_id id;
Ben Widawsky2a5913a2014-03-19 18:31:13 -07006095 u32 gtfifodbg, val, rc6_mode = 0;
Jesse Barnes0a073b82013-04-17 15:54:58 -07006096
6097 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
6098
Imre Deakae484342014-03-31 15:10:44 +03006099 valleyview_check_pctx(dev_priv);
6100
Ville Syrjälä297b32e2016-04-13 21:09:30 +03006101 gtfifodbg = I915_READ(GTFIFODBG);
6102 if (gtfifodbg) {
Jesse Barnesf7d85c12013-09-27 10:40:54 -07006103 DRM_DEBUG_DRIVER("GT fifo had a previous error %x\n",
6104 gtfifodbg);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006105 I915_WRITE(GTFIFODBG, gtfifodbg);
6106 }
6107
Deepak Sc8d9a592013-11-23 14:55:42 +05306108 /* If VLV, Forcewake all wells, else re-direct to regular path */
Mika Kuoppala59bad942015-01-16 11:34:40 +02006109 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006110
Ville Syrjälä160614a2015-01-19 13:50:47 +02006111 /* Disable RC states. */
6112 I915_WRITE(GEN6_RC_CONTROL, 0);
6113
Ville Syrjäläcad725f2015-01-19 13:50:48 +02006114 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 1000000);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006115 I915_WRITE(GEN6_RP_UP_THRESHOLD, 59400);
6116 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 245000);
6117 I915_WRITE(GEN6_RP_UP_EI, 66000);
6118 I915_WRITE(GEN6_RP_DOWN_EI, 350000);
6119
6120 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
6121
6122 I915_WRITE(GEN6_RP_CONTROL,
6123 GEN6_RP_MEDIA_TURBO |
6124 GEN6_RP_MEDIA_HW_NORMAL_MODE |
6125 GEN6_RP_MEDIA_IS_GFX |
6126 GEN6_RP_ENABLE |
6127 GEN6_RP_UP_BUSY_AVG |
6128 GEN6_RP_DOWN_IDLE_CONT);
6129
6130 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 0x00280000);
6131 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
6132 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
6133
Akash Goel3b3f1652016-10-13 22:44:48 +05306134 for_each_engine(engine, dev_priv, id)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00006135 I915_WRITE(RING_MAX_IDLE(engine->mmio_base), 10);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006136
Jesse Barnes2f0aa3042013-11-15 09:32:11 -08006137 I915_WRITE(GEN6_RC6_THRESHOLD, 0x557);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006138
6139 /* allows RC6 residency counter to work */
Jesse Barnes49798eb2013-09-26 17:55:57 -07006140 I915_WRITE(VLV_COUNTER_CONTROL,
Deepak S31685c22014-07-03 17:33:01 -04006141 _MASKED_BIT_ENABLE(VLV_MEDIA_RC0_COUNT_EN |
6142 VLV_RENDER_RC0_COUNT_EN |
Jesse Barnes49798eb2013-09-26 17:55:57 -07006143 VLV_MEDIA_RC6_COUNT_EN |
6144 VLV_RENDER_RC6_COUNT_EN));
Deepak S31685c22014-07-03 17:33:01 -04006145
Chris Wilsondc979972016-05-10 14:10:04 +01006146 if (intel_enable_rc6() & INTEL_RC6_ENABLE)
Jesse Barnes6b88f292013-11-15 09:32:12 -08006147 rc6_mode = GEN7_RC_CTL_TO_MODE | VLV_RC_CTL_CTX_RST_PARALLEL;
Ben Widawskydc39fff2013-10-18 12:32:07 -07006148
Chris Wilsondc979972016-05-10 14:10:04 +01006149 intel_print_rc6_info(dev_priv, rc6_mode);
Ben Widawskydc39fff2013-10-18 12:32:07 -07006150
Jesse Barnesa2b23fe2013-09-19 09:33:13 -07006151 I915_WRITE(GEN6_RC_CONTROL, rc6_mode);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006152
Deepak S3ef62342015-04-29 08:36:24 +05306153 /* Setting Fixed Bias */
6154 val = VLV_OVERRIDE_EN |
6155 VLV_SOC_TDP_EN |
6156 VLV_BIAS_CPU_125_SOC_875;
6157 vlv_punit_write(dev_priv, VLV_TURBO_SOC_OVERRIDE, val);
6158
Jani Nikula64936252013-05-22 15:36:20 +03006159 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006160
Ville Syrjälä8d40c3a2014-11-07 21:33:45 +02006161 /* RPS code assumes GPLL is used */
6162 WARN_ONCE((val & GPLLENABLE) == 0, "GPLL not enabled\n");
6163
Jani Nikula742f4912015-09-03 11:16:09 +03006164 DRM_DEBUG_DRIVER("GPLL enabled? %s\n", yesno(val & GPLLENABLE));
Jesse Barnes0a073b82013-04-17 15:54:58 -07006165 DRM_DEBUG_DRIVER("GPU status: 0x%08x\n", val);
6166
Chris Wilson3a45b052016-07-13 09:10:32 +01006167 reset_rps(dev_priv, valleyview_set_rps);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006168
Mika Kuoppala59bad942015-01-16 11:34:40 +02006169 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006170}
6171
Eugeni Dodonovdde18882012-04-18 15:29:24 -03006172static unsigned long intel_pxfreq(u32 vidfreq)
6173{
6174 unsigned long freq;
6175 int div = (vidfreq & 0x3f0000) >> 16;
6176 int post = (vidfreq & 0x3000) >> 12;
6177 int pre = (vidfreq & 0x7);
6178
6179 if (!pre)
6180 return 0;
6181
6182 freq = ((div * 133333) / ((1<<post) * pre));
6183
6184 return freq;
6185}
6186
Daniel Vettereb48eb02012-04-26 23:28:12 +02006187static const struct cparams {
6188 u16 i;
6189 u16 t;
6190 u16 m;
6191 u16 c;
6192} cparams[] = {
6193 { 1, 1333, 301, 28664 },
6194 { 1, 1066, 294, 24460 },
6195 { 1, 800, 294, 25192 },
6196 { 0, 1333, 276, 27605 },
6197 { 0, 1066, 276, 27605 },
6198 { 0, 800, 231, 23784 },
6199};
6200
Chris Wilsonf531dcb22012-09-25 10:16:12 +01006201static unsigned long __i915_chipset_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02006202{
6203 u64 total_count, diff, ret;
6204 u32 count1, count2, count3, m = 0, c = 0;
6205 unsigned long now = jiffies_to_msecs(jiffies), diff1;
6206 int i;
6207
Daniel Vetter02d71952012-08-09 16:44:54 +02006208 assert_spin_locked(&mchdev_lock);
6209
Daniel Vetter20e4d402012-08-08 23:35:39 +02006210 diff1 = now - dev_priv->ips.last_time1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006211
6212 /* Prevent division-by-zero if we are asking too fast.
6213 * Also, we don't get interesting results if we are polling
6214 * faster than once in 10ms, so just return the saved value
6215 * in such cases.
6216 */
6217 if (diff1 <= 10)
Daniel Vetter20e4d402012-08-08 23:35:39 +02006218 return dev_priv->ips.chipset_power;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006219
6220 count1 = I915_READ(DMIEC);
6221 count2 = I915_READ(DDREC);
6222 count3 = I915_READ(CSIEC);
6223
6224 total_count = count1 + count2 + count3;
6225
6226 /* FIXME: handle per-counter overflow */
Daniel Vetter20e4d402012-08-08 23:35:39 +02006227 if (total_count < dev_priv->ips.last_count1) {
6228 diff = ~0UL - dev_priv->ips.last_count1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006229 diff += total_count;
6230 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +02006231 diff = total_count - dev_priv->ips.last_count1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006232 }
6233
6234 for (i = 0; i < ARRAY_SIZE(cparams); i++) {
Daniel Vetter20e4d402012-08-08 23:35:39 +02006235 if (cparams[i].i == dev_priv->ips.c_m &&
6236 cparams[i].t == dev_priv->ips.r_t) {
Daniel Vettereb48eb02012-04-26 23:28:12 +02006237 m = cparams[i].m;
6238 c = cparams[i].c;
6239 break;
6240 }
6241 }
6242
6243 diff = div_u64(diff, diff1);
6244 ret = ((m * diff) + c);
6245 ret = div_u64(ret, 10);
6246
Daniel Vetter20e4d402012-08-08 23:35:39 +02006247 dev_priv->ips.last_count1 = total_count;
6248 dev_priv->ips.last_time1 = now;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006249
Daniel Vetter20e4d402012-08-08 23:35:39 +02006250 dev_priv->ips.chipset_power = ret;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006251
6252 return ret;
6253}
6254
Chris Wilsonf531dcb22012-09-25 10:16:12 +01006255unsigned long i915_chipset_val(struct drm_i915_private *dev_priv)
6256{
6257 unsigned long val;
6258
Chris Wilsondc979972016-05-10 14:10:04 +01006259 if (INTEL_INFO(dev_priv)->gen != 5)
Chris Wilsonf531dcb22012-09-25 10:16:12 +01006260 return 0;
6261
6262 spin_lock_irq(&mchdev_lock);
6263
6264 val = __i915_chipset_val(dev_priv);
6265
6266 spin_unlock_irq(&mchdev_lock);
6267
6268 return val;
6269}
6270
Daniel Vettereb48eb02012-04-26 23:28:12 +02006271unsigned long i915_mch_val(struct drm_i915_private *dev_priv)
6272{
6273 unsigned long m, x, b;
6274 u32 tsfs;
6275
6276 tsfs = I915_READ(TSFS);
6277
6278 m = ((tsfs & TSFS_SLOPE_MASK) >> TSFS_SLOPE_SHIFT);
6279 x = I915_READ8(TR1);
6280
6281 b = tsfs & TSFS_INTR_MASK;
6282
6283 return ((m * x) / 127) - b;
6284}
6285
Mika Kuoppalad972d6e2014-12-01 18:01:05 +02006286static int _pxvid_to_vd(u8 pxvid)
6287{
6288 if (pxvid == 0)
6289 return 0;
6290
6291 if (pxvid >= 8 && pxvid < 31)
6292 pxvid = 31;
6293
6294 return (pxvid + 2) * 125;
6295}
6296
6297static u32 pvid_to_extvid(struct drm_i915_private *dev_priv, u8 pxvid)
Daniel Vettereb48eb02012-04-26 23:28:12 +02006298{
Mika Kuoppalad972d6e2014-12-01 18:01:05 +02006299 const int vd = _pxvid_to_vd(pxvid);
6300 const int vm = vd - 1125;
6301
Chris Wilsondc979972016-05-10 14:10:04 +01006302 if (INTEL_INFO(dev_priv)->is_mobile)
Mika Kuoppalad972d6e2014-12-01 18:01:05 +02006303 return vm > 0 ? vm : 0;
6304
6305 return vd;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006306}
6307
Daniel Vetter02d71952012-08-09 16:44:54 +02006308static void __i915_update_gfx_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02006309{
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00006310 u64 now, diff, diffms;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006311 u32 count;
6312
Daniel Vetter02d71952012-08-09 16:44:54 +02006313 assert_spin_locked(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006314
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00006315 now = ktime_get_raw_ns();
6316 diffms = now - dev_priv->ips.last_time2;
6317 do_div(diffms, NSEC_PER_MSEC);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006318
6319 /* Don't divide by 0 */
Daniel Vettereb48eb02012-04-26 23:28:12 +02006320 if (!diffms)
6321 return;
6322
6323 count = I915_READ(GFXEC);
6324
Daniel Vetter20e4d402012-08-08 23:35:39 +02006325 if (count < dev_priv->ips.last_count2) {
6326 diff = ~0UL - dev_priv->ips.last_count2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006327 diff += count;
6328 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +02006329 diff = count - dev_priv->ips.last_count2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006330 }
6331
Daniel Vetter20e4d402012-08-08 23:35:39 +02006332 dev_priv->ips.last_count2 = count;
6333 dev_priv->ips.last_time2 = now;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006334
6335 /* More magic constants... */
6336 diff = diff * 1181;
6337 diff = div_u64(diff, diffms * 10);
Daniel Vetter20e4d402012-08-08 23:35:39 +02006338 dev_priv->ips.gfx_power = diff;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006339}
6340
Daniel Vetter02d71952012-08-09 16:44:54 +02006341void i915_update_gfx_val(struct drm_i915_private *dev_priv)
6342{
Chris Wilsondc979972016-05-10 14:10:04 +01006343 if (INTEL_INFO(dev_priv)->gen != 5)
Daniel Vetter02d71952012-08-09 16:44:54 +02006344 return;
6345
Daniel Vetter92703882012-08-09 16:46:01 +02006346 spin_lock_irq(&mchdev_lock);
Daniel Vetter02d71952012-08-09 16:44:54 +02006347
6348 __i915_update_gfx_val(dev_priv);
6349
Daniel Vetter92703882012-08-09 16:46:01 +02006350 spin_unlock_irq(&mchdev_lock);
Daniel Vetter02d71952012-08-09 16:44:54 +02006351}
6352
Chris Wilsonf531dcb22012-09-25 10:16:12 +01006353static unsigned long __i915_gfx_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02006354{
6355 unsigned long t, corr, state1, corr2, state2;
6356 u32 pxvid, ext_v;
6357
Daniel Vetter02d71952012-08-09 16:44:54 +02006358 assert_spin_locked(&mchdev_lock);
6359
Ville Syrjälä616847e2015-09-18 20:03:19 +03006360 pxvid = I915_READ(PXVFREQ(dev_priv->rps.cur_freq));
Daniel Vettereb48eb02012-04-26 23:28:12 +02006361 pxvid = (pxvid >> 24) & 0x7f;
6362 ext_v = pvid_to_extvid(dev_priv, pxvid);
6363
6364 state1 = ext_v;
6365
6366 t = i915_mch_val(dev_priv);
6367
6368 /* Revel in the empirically derived constants */
6369
6370 /* Correction factor in 1/100000 units */
6371 if (t > 80)
6372 corr = ((t * 2349) + 135940);
6373 else if (t >= 50)
6374 corr = ((t * 964) + 29317);
6375 else /* < 50 */
6376 corr = ((t * 301) + 1004);
6377
6378 corr = corr * ((150142 * state1) / 10000 - 78642);
6379 corr /= 100000;
Daniel Vetter20e4d402012-08-08 23:35:39 +02006380 corr2 = (corr * dev_priv->ips.corr);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006381
6382 state2 = (corr2 * state1) / 10000;
6383 state2 /= 100; /* convert to mW */
6384
Daniel Vetter02d71952012-08-09 16:44:54 +02006385 __i915_update_gfx_val(dev_priv);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006386
Daniel Vetter20e4d402012-08-08 23:35:39 +02006387 return dev_priv->ips.gfx_power + state2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006388}
6389
Chris Wilsonf531dcb22012-09-25 10:16:12 +01006390unsigned long i915_gfx_val(struct drm_i915_private *dev_priv)
6391{
6392 unsigned long val;
6393
Chris Wilsondc979972016-05-10 14:10:04 +01006394 if (INTEL_INFO(dev_priv)->gen != 5)
Chris Wilsonf531dcb22012-09-25 10:16:12 +01006395 return 0;
6396
6397 spin_lock_irq(&mchdev_lock);
6398
6399 val = __i915_gfx_val(dev_priv);
6400
6401 spin_unlock_irq(&mchdev_lock);
6402
6403 return val;
6404}
6405
Daniel Vettereb48eb02012-04-26 23:28:12 +02006406/**
6407 * i915_read_mch_val - return value for IPS use
6408 *
6409 * Calculate and return a value for the IPS driver to use when deciding whether
6410 * we have thermal and power headroom to increase CPU or GPU power budget.
6411 */
6412unsigned long i915_read_mch_val(void)
6413{
6414 struct drm_i915_private *dev_priv;
6415 unsigned long chipset_val, graphics_val, ret = 0;
6416
Daniel Vetter92703882012-08-09 16:46:01 +02006417 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006418 if (!i915_mch_dev)
6419 goto out_unlock;
6420 dev_priv = i915_mch_dev;
6421
Chris Wilsonf531dcb22012-09-25 10:16:12 +01006422 chipset_val = __i915_chipset_val(dev_priv);
6423 graphics_val = __i915_gfx_val(dev_priv);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006424
6425 ret = chipset_val + graphics_val;
6426
6427out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02006428 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006429
6430 return ret;
6431}
6432EXPORT_SYMBOL_GPL(i915_read_mch_val);
6433
6434/**
6435 * i915_gpu_raise - raise GPU frequency limit
6436 *
6437 * Raise the limit; IPS indicates we have thermal headroom.
6438 */
6439bool i915_gpu_raise(void)
6440{
6441 struct drm_i915_private *dev_priv;
6442 bool ret = true;
6443
Daniel Vetter92703882012-08-09 16:46:01 +02006444 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006445 if (!i915_mch_dev) {
6446 ret = false;
6447 goto out_unlock;
6448 }
6449 dev_priv = i915_mch_dev;
6450
Daniel Vetter20e4d402012-08-08 23:35:39 +02006451 if (dev_priv->ips.max_delay > dev_priv->ips.fmax)
6452 dev_priv->ips.max_delay--;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006453
6454out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02006455 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006456
6457 return ret;
6458}
6459EXPORT_SYMBOL_GPL(i915_gpu_raise);
6460
6461/**
6462 * i915_gpu_lower - lower GPU frequency limit
6463 *
6464 * IPS indicates we're close to a thermal limit, so throttle back the GPU
6465 * frequency maximum.
6466 */
6467bool i915_gpu_lower(void)
6468{
6469 struct drm_i915_private *dev_priv;
6470 bool ret = true;
6471
Daniel Vetter92703882012-08-09 16:46:01 +02006472 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006473 if (!i915_mch_dev) {
6474 ret = false;
6475 goto out_unlock;
6476 }
6477 dev_priv = i915_mch_dev;
6478
Daniel Vetter20e4d402012-08-08 23:35:39 +02006479 if (dev_priv->ips.max_delay < dev_priv->ips.min_delay)
6480 dev_priv->ips.max_delay++;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006481
6482out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02006483 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006484
6485 return ret;
6486}
6487EXPORT_SYMBOL_GPL(i915_gpu_lower);
6488
6489/**
6490 * i915_gpu_busy - indicate GPU business to IPS
6491 *
6492 * Tell the IPS driver whether or not the GPU is busy.
6493 */
6494bool i915_gpu_busy(void)
6495{
Daniel Vettereb48eb02012-04-26 23:28:12 +02006496 bool ret = false;
6497
Daniel Vetter92703882012-08-09 16:46:01 +02006498 spin_lock_irq(&mchdev_lock);
Chris Wilsondcff85c2016-08-05 10:14:11 +01006499 if (i915_mch_dev)
6500 ret = i915_mch_dev->gt.awake;
Daniel Vetter92703882012-08-09 16:46:01 +02006501 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006502
6503 return ret;
6504}
6505EXPORT_SYMBOL_GPL(i915_gpu_busy);
6506
6507/**
6508 * i915_gpu_turbo_disable - disable graphics turbo
6509 *
6510 * Disable graphics turbo by resetting the max frequency and setting the
6511 * current frequency to the default.
6512 */
6513bool i915_gpu_turbo_disable(void)
6514{
6515 struct drm_i915_private *dev_priv;
6516 bool ret = true;
6517
Daniel Vetter92703882012-08-09 16:46:01 +02006518 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006519 if (!i915_mch_dev) {
6520 ret = false;
6521 goto out_unlock;
6522 }
6523 dev_priv = i915_mch_dev;
6524
Daniel Vetter20e4d402012-08-08 23:35:39 +02006525 dev_priv->ips.max_delay = dev_priv->ips.fstart;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006526
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01006527 if (!ironlake_set_drps(dev_priv, dev_priv->ips.fstart))
Daniel Vettereb48eb02012-04-26 23:28:12 +02006528 ret = false;
6529
6530out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02006531 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006532
6533 return ret;
6534}
6535EXPORT_SYMBOL_GPL(i915_gpu_turbo_disable);
6536
6537/**
6538 * Tells the intel_ips driver that the i915 driver is now loaded, if
6539 * IPS got loaded first.
6540 *
6541 * This awkward dance is so that neither module has to depend on the
6542 * other in order for IPS to do the appropriate communication of
6543 * GPU turbo limits to i915.
6544 */
6545static void
6546ips_ping_for_i915_load(void)
6547{
6548 void (*link)(void);
6549
6550 link = symbol_get(ips_link_to_i915_driver);
6551 if (link) {
6552 link();
6553 symbol_put(ips_link_to_i915_driver);
6554 }
6555}
6556
6557void intel_gpu_ips_init(struct drm_i915_private *dev_priv)
6558{
Daniel Vetter02d71952012-08-09 16:44:54 +02006559 /* We only register the i915 ips part with intel-ips once everything is
6560 * set up, to avoid intel-ips sneaking in and reading bogus values. */
Daniel Vetter92703882012-08-09 16:46:01 +02006561 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006562 i915_mch_dev = dev_priv;
Daniel Vetter92703882012-08-09 16:46:01 +02006563 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006564
6565 ips_ping_for_i915_load();
6566}
6567
6568void intel_gpu_ips_teardown(void)
6569{
Daniel Vetter92703882012-08-09 16:46:01 +02006570 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006571 i915_mch_dev = NULL;
Daniel Vetter92703882012-08-09 16:46:01 +02006572 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006573}
Deepak S76c3552f2014-01-30 23:08:16 +05306574
Chris Wilsondc979972016-05-10 14:10:04 +01006575static void intel_init_emon(struct drm_i915_private *dev_priv)
Eugeni Dodonovdde18882012-04-18 15:29:24 -03006576{
Eugeni Dodonovdde18882012-04-18 15:29:24 -03006577 u32 lcfuse;
6578 u8 pxw[16];
6579 int i;
6580
6581 /* Disable to program */
6582 I915_WRITE(ECR, 0);
6583 POSTING_READ(ECR);
6584
6585 /* Program energy weights for various events */
6586 I915_WRITE(SDEW, 0x15040d00);
6587 I915_WRITE(CSIEW0, 0x007f0000);
6588 I915_WRITE(CSIEW1, 0x1e220004);
6589 I915_WRITE(CSIEW2, 0x04000004);
6590
6591 for (i = 0; i < 5; i++)
Ville Syrjälä616847e2015-09-18 20:03:19 +03006592 I915_WRITE(PEW(i), 0);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03006593 for (i = 0; i < 3; i++)
Ville Syrjälä616847e2015-09-18 20:03:19 +03006594 I915_WRITE(DEW(i), 0);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03006595
6596 /* Program P-state weights to account for frequency power adjustment */
6597 for (i = 0; i < 16; i++) {
Ville Syrjälä616847e2015-09-18 20:03:19 +03006598 u32 pxvidfreq = I915_READ(PXVFREQ(i));
Eugeni Dodonovdde18882012-04-18 15:29:24 -03006599 unsigned long freq = intel_pxfreq(pxvidfreq);
6600 unsigned long vid = (pxvidfreq & PXVFREQ_PX_MASK) >>
6601 PXVFREQ_PX_SHIFT;
6602 unsigned long val;
6603
6604 val = vid * vid;
6605 val *= (freq / 1000);
6606 val *= 255;
6607 val /= (127*127*900);
6608 if (val > 0xff)
6609 DRM_ERROR("bad pxval: %ld\n", val);
6610 pxw[i] = val;
6611 }
6612 /* Render standby states get 0 weight */
6613 pxw[14] = 0;
6614 pxw[15] = 0;
6615
6616 for (i = 0; i < 4; i++) {
6617 u32 val = (pxw[i*4] << 24) | (pxw[(i*4)+1] << 16) |
6618 (pxw[(i*4)+2] << 8) | (pxw[(i*4)+3]);
Ville Syrjälä616847e2015-09-18 20:03:19 +03006619 I915_WRITE(PXW(i), val);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03006620 }
6621
6622 /* Adjust magic regs to magic values (more experimental results) */
6623 I915_WRITE(OGW0, 0);
6624 I915_WRITE(OGW1, 0);
6625 I915_WRITE(EG0, 0x00007f00);
6626 I915_WRITE(EG1, 0x0000000e);
6627 I915_WRITE(EG2, 0x000e0000);
6628 I915_WRITE(EG3, 0x68000300);
6629 I915_WRITE(EG4, 0x42000000);
6630 I915_WRITE(EG5, 0x00140031);
6631 I915_WRITE(EG6, 0);
6632 I915_WRITE(EG7, 0);
6633
6634 for (i = 0; i < 8; i++)
Ville Syrjälä616847e2015-09-18 20:03:19 +03006635 I915_WRITE(PXWL(i), 0);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03006636
6637 /* Enable PMON + select events */
6638 I915_WRITE(ECR, 0x80000019);
6639
6640 lcfuse = I915_READ(LCFUSE02);
6641
Daniel Vetter20e4d402012-08-08 23:35:39 +02006642 dev_priv->ips.corr = (lcfuse & LCFUSE_HIV_MASK);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03006643}
6644
Chris Wilsondc979972016-05-10 14:10:04 +01006645void intel_init_gt_powersave(struct drm_i915_private *dev_priv)
Imre Deakae484342014-03-31 15:10:44 +03006646{
Imre Deakb268c692015-12-15 20:10:31 +02006647 /*
6648 * RPM depends on RC6 to save restore the GT HW context, so make RC6 a
6649 * requirement.
6650 */
6651 if (!i915.enable_rc6) {
6652 DRM_INFO("RC6 disabled, disabling runtime PM support\n");
6653 intel_runtime_pm_get(dev_priv);
6654 }
Imre Deake6069ca2014-04-18 16:01:02 +03006655
Chris Wilsonb5163db2016-08-10 13:58:24 +01006656 mutex_lock(&dev_priv->drm.struct_mutex);
Chris Wilson773ea9a2016-07-13 09:10:33 +01006657 mutex_lock(&dev_priv->rps.hw_lock);
6658
6659 /* Initialize RPS limits (for userspace) */
Chris Wilsondc979972016-05-10 14:10:04 +01006660 if (IS_CHERRYVIEW(dev_priv))
6661 cherryview_init_gt_powersave(dev_priv);
6662 else if (IS_VALLEYVIEW(dev_priv))
6663 valleyview_init_gt_powersave(dev_priv);
Chris Wilson2a13ae72016-08-02 11:15:27 +01006664 else if (INTEL_GEN(dev_priv) >= 6)
Chris Wilson773ea9a2016-07-13 09:10:33 +01006665 gen6_init_rps_frequencies(dev_priv);
6666
6667 /* Derive initial user preferences/limits from the hardware limits */
6668 dev_priv->rps.idle_freq = dev_priv->rps.min_freq;
6669 dev_priv->rps.cur_freq = dev_priv->rps.idle_freq;
6670
6671 dev_priv->rps.max_freq_softlimit = dev_priv->rps.max_freq;
6672 dev_priv->rps.min_freq_softlimit = dev_priv->rps.min_freq;
6673
6674 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
6675 dev_priv->rps.min_freq_softlimit =
6676 max_t(int,
6677 dev_priv->rps.efficient_freq,
6678 intel_freq_opcode(dev_priv, 450));
6679
Chris Wilson99ac9612016-07-13 09:10:34 +01006680 /* After setting max-softlimit, find the overclock max freq */
6681 if (IS_GEN6(dev_priv) ||
6682 IS_IVYBRIDGE(dev_priv) || IS_HASWELL(dev_priv)) {
6683 u32 params = 0;
6684
6685 sandybridge_pcode_read(dev_priv, GEN6_READ_OC_PARAMS, &params);
6686 if (params & BIT(31)) { /* OC supported */
6687 DRM_DEBUG_DRIVER("Overclocking supported, max: %dMHz, overclock: %dMHz\n",
6688 (dev_priv->rps.max_freq & 0xff) * 50,
6689 (params & 0xff) * 50);
6690 dev_priv->rps.max_freq = params & 0xff;
6691 }
6692 }
6693
Chris Wilson29ecd78d2016-07-13 09:10:35 +01006694 /* Finally allow us to boost to max by default */
6695 dev_priv->rps.boost_freq = dev_priv->rps.max_freq;
6696
Chris Wilson773ea9a2016-07-13 09:10:33 +01006697 mutex_unlock(&dev_priv->rps.hw_lock);
Chris Wilsonb5163db2016-08-10 13:58:24 +01006698 mutex_unlock(&dev_priv->drm.struct_mutex);
Chris Wilson54b4f682016-07-21 21:16:19 +01006699
6700 intel_autoenable_gt_powersave(dev_priv);
Imre Deakae484342014-03-31 15:10:44 +03006701}
6702
Chris Wilsondc979972016-05-10 14:10:04 +01006703void intel_cleanup_gt_powersave(struct drm_i915_private *dev_priv)
Imre Deakae484342014-03-31 15:10:44 +03006704{
Ville Syrjälä8dac1e12016-08-02 14:07:33 +03006705 if (IS_VALLEYVIEW(dev_priv))
Chris Wilsondc979972016-05-10 14:10:04 +01006706 valleyview_cleanup_gt_powersave(dev_priv);
Imre Deakb268c692015-12-15 20:10:31 +02006707
6708 if (!i915.enable_rc6)
6709 intel_runtime_pm_put(dev_priv);
Imre Deakae484342014-03-31 15:10:44 +03006710}
6711
Chris Wilson54b4f682016-07-21 21:16:19 +01006712/**
6713 * intel_suspend_gt_powersave - suspend PM work and helper threads
6714 * @dev_priv: i915 device
6715 *
6716 * We don't want to disable RC6 or other features here, we just want
6717 * to make sure any work we've queued has finished and won't bother
6718 * us while we're suspended.
6719 */
6720void intel_suspend_gt_powersave(struct drm_i915_private *dev_priv)
6721{
6722 if (INTEL_GEN(dev_priv) < 6)
6723 return;
6724
6725 if (cancel_delayed_work_sync(&dev_priv->rps.autoenable_work))
6726 intel_runtime_pm_put(dev_priv);
6727
6728 /* gen6_rps_idle() will be called later to disable interrupts */
6729}
6730
Chris Wilsonb7137e02016-07-13 09:10:37 +01006731void intel_sanitize_gt_powersave(struct drm_i915_private *dev_priv)
6732{
6733 dev_priv->rps.enabled = true; /* force disabling */
6734 intel_disable_gt_powersave(dev_priv);
Chris Wilson54b4f682016-07-21 21:16:19 +01006735
6736 gen6_reset_rps_interrupts(dev_priv);
Jesse Barnes156c7ca2014-06-12 08:35:45 -07006737}
6738
Chris Wilsondc979972016-05-10 14:10:04 +01006739void intel_disable_gt_powersave(struct drm_i915_private *dev_priv)
Daniel Vetter8090c6b2012-06-24 16:42:32 +02006740{
Chris Wilsonb7137e02016-07-13 09:10:37 +01006741 if (!READ_ONCE(dev_priv->rps.enabled))
6742 return;
Jesse Barnes1a01ab32012-11-02 11:14:00 -07006743
Jesse Barnes4fc688c2012-11-02 11:14:01 -07006744 mutex_lock(&dev_priv->rps.hw_lock);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006745
Chris Wilsonb7137e02016-07-13 09:10:37 +01006746 if (INTEL_GEN(dev_priv) >= 9) {
6747 gen9_disable_rc6(dev_priv);
6748 gen9_disable_rps(dev_priv);
6749 } else if (IS_CHERRYVIEW(dev_priv)) {
6750 cherryview_disable_rps(dev_priv);
6751 } else if (IS_VALLEYVIEW(dev_priv)) {
6752 valleyview_disable_rps(dev_priv);
6753 } else if (INTEL_GEN(dev_priv) >= 6) {
6754 gen6_disable_rps(dev_priv);
6755 } else if (IS_IRONLAKE_M(dev_priv)) {
6756 ironlake_disable_drps(dev_priv);
6757 }
6758
6759 dev_priv->rps.enabled = false;
6760 mutex_unlock(&dev_priv->rps.hw_lock);
6761}
6762
6763void intel_enable_gt_powersave(struct drm_i915_private *dev_priv)
6764{
Chris Wilson54b4f682016-07-21 21:16:19 +01006765 /* We shouldn't be disabling as we submit, so this should be less
6766 * racy than it appears!
6767 */
Chris Wilsonb7137e02016-07-13 09:10:37 +01006768 if (READ_ONCE(dev_priv->rps.enabled))
6769 return;
6770
6771 /* Powersaving is controlled by the host when inside a VM */
6772 if (intel_vgpu_active(dev_priv))
6773 return;
6774
6775 mutex_lock(&dev_priv->rps.hw_lock);
Imre Deak3cc134e2014-11-19 15:30:03 +02006776
Chris Wilsondc979972016-05-10 14:10:04 +01006777 if (IS_CHERRYVIEW(dev_priv)) {
6778 cherryview_enable_rps(dev_priv);
6779 } else if (IS_VALLEYVIEW(dev_priv)) {
6780 valleyview_enable_rps(dev_priv);
Chris Wilsonb7137e02016-07-13 09:10:37 +01006781 } else if (INTEL_GEN(dev_priv) >= 9) {
Chris Wilsondc979972016-05-10 14:10:04 +01006782 gen9_enable_rc6(dev_priv);
6783 gen9_enable_rps(dev_priv);
6784 if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv))
Chris Wilsonfb7404e2016-07-13 09:10:38 +01006785 gen6_update_ring_freq(dev_priv);
Chris Wilsondc979972016-05-10 14:10:04 +01006786 } else if (IS_BROADWELL(dev_priv)) {
6787 gen8_enable_rps(dev_priv);
Chris Wilsonfb7404e2016-07-13 09:10:38 +01006788 gen6_update_ring_freq(dev_priv);
Chris Wilsonb7137e02016-07-13 09:10:37 +01006789 } else if (INTEL_GEN(dev_priv) >= 6) {
Chris Wilsondc979972016-05-10 14:10:04 +01006790 gen6_enable_rps(dev_priv);
Chris Wilsonfb7404e2016-07-13 09:10:38 +01006791 gen6_update_ring_freq(dev_priv);
Chris Wilsonb7137e02016-07-13 09:10:37 +01006792 } else if (IS_IRONLAKE_M(dev_priv)) {
6793 ironlake_enable_drps(dev_priv);
6794 intel_init_emon(dev_priv);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006795 }
Chris Wilsonaed242f2015-03-18 09:48:21 +00006796
6797 WARN_ON(dev_priv->rps.max_freq < dev_priv->rps.min_freq);
6798 WARN_ON(dev_priv->rps.idle_freq > dev_priv->rps.max_freq);
6799
6800 WARN_ON(dev_priv->rps.efficient_freq < dev_priv->rps.min_freq);
6801 WARN_ON(dev_priv->rps.efficient_freq > dev_priv->rps.max_freq);
6802
Chris Wilson54b4f682016-07-21 21:16:19 +01006803 dev_priv->rps.enabled = true;
Jesse Barnes4fc688c2012-11-02 11:14:01 -07006804 mutex_unlock(&dev_priv->rps.hw_lock);
Chris Wilsonb7137e02016-07-13 09:10:37 +01006805}
Imre Deakc6df39b2014-04-14 20:24:29 +03006806
Chris Wilson54b4f682016-07-21 21:16:19 +01006807static void __intel_autoenable_gt_powersave(struct work_struct *work)
6808{
6809 struct drm_i915_private *dev_priv =
6810 container_of(work, typeof(*dev_priv), rps.autoenable_work.work);
6811 struct intel_engine_cs *rcs;
6812 struct drm_i915_gem_request *req;
6813
6814 if (READ_ONCE(dev_priv->rps.enabled))
6815 goto out;
6816
Akash Goel3b3f1652016-10-13 22:44:48 +05306817 rcs = dev_priv->engine[RCS];
Chris Wilson54b4f682016-07-21 21:16:19 +01006818 if (rcs->last_context)
6819 goto out;
6820
6821 if (!rcs->init_context)
6822 goto out;
6823
6824 mutex_lock(&dev_priv->drm.struct_mutex);
6825
6826 req = i915_gem_request_alloc(rcs, dev_priv->kernel_context);
6827 if (IS_ERR(req))
6828 goto unlock;
6829
6830 if (!i915.enable_execlists && i915_switch_context(req) == 0)
6831 rcs->init_context(req);
6832
6833 /* Mark the device busy, calling intel_enable_gt_powersave() */
6834 i915_add_request_no_flush(req);
6835
6836unlock:
6837 mutex_unlock(&dev_priv->drm.struct_mutex);
6838out:
6839 intel_runtime_pm_put(dev_priv);
6840}
6841
6842void intel_autoenable_gt_powersave(struct drm_i915_private *dev_priv)
6843{
6844 if (READ_ONCE(dev_priv->rps.enabled))
6845 return;
6846
6847 if (IS_IRONLAKE_M(dev_priv)) {
6848 ironlake_enable_drps(dev_priv);
Chris Wilson54b4f682016-07-21 21:16:19 +01006849 intel_init_emon(dev_priv);
Chris Wilson54b4f682016-07-21 21:16:19 +01006850 } else if (INTEL_INFO(dev_priv)->gen >= 6) {
6851 /*
6852 * PCU communication is slow and this doesn't need to be
6853 * done at any specific time, so do this out of our fast path
6854 * to make resume and init faster.
6855 *
6856 * We depend on the HW RC6 power context save/restore
6857 * mechanism when entering D3 through runtime PM suspend. So
6858 * disable RPM until RPS/RC6 is properly setup. We can only
6859 * get here via the driver load/system resume/runtime resume
6860 * paths, so the _noresume version is enough (and in case of
6861 * runtime resume it's necessary).
6862 */
6863 if (queue_delayed_work(dev_priv->wq,
6864 &dev_priv->rps.autoenable_work,
6865 round_jiffies_up_relative(HZ)))
6866 intel_runtime_pm_get_noresume(dev_priv);
6867 }
6868}
6869
Daniel Vetter3107bd42012-10-31 22:52:31 +01006870static void ibx_init_clock_gating(struct drm_device *dev)
6871{
Chris Wilsonfac5e232016-07-04 11:34:36 +01006872 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter3107bd42012-10-31 22:52:31 +01006873
6874 /*
6875 * On Ibex Peak and Cougar Point, we need to disable clock
6876 * gating for the panel power sequencer or it will fail to
6877 * start up when no ports are active.
6878 */
6879 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE);
6880}
6881
Ville Syrjälä0e088b82013-06-07 10:47:04 +03006882static void g4x_disable_trickle_feed(struct drm_device *dev)
6883{
Chris Wilsonfac5e232016-07-04 11:34:36 +01006884 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläb12ce1d2015-05-26 20:27:23 +03006885 enum pipe pipe;
Ville Syrjälä0e088b82013-06-07 10:47:04 +03006886
Damien Lespiau055e3932014-08-18 13:49:10 +01006887 for_each_pipe(dev_priv, pipe) {
Ville Syrjälä0e088b82013-06-07 10:47:04 +03006888 I915_WRITE(DSPCNTR(pipe),
6889 I915_READ(DSPCNTR(pipe)) |
6890 DISPPLANE_TRICKLE_FEED_DISABLE);
Ville Syrjäläb12ce1d2015-05-26 20:27:23 +03006891
6892 I915_WRITE(DSPSURF(pipe), I915_READ(DSPSURF(pipe)));
6893 POSTING_READ(DSPSURF(pipe));
Ville Syrjälä0e088b82013-06-07 10:47:04 +03006894 }
6895}
6896
Ville Syrjälä017636c2013-12-05 15:51:37 +02006897static void ilk_init_lp_watermarks(struct drm_device *dev)
6898{
Chris Wilsonfac5e232016-07-04 11:34:36 +01006899 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä017636c2013-12-05 15:51:37 +02006900
6901 I915_WRITE(WM3_LP_ILK, I915_READ(WM3_LP_ILK) & ~WM1_LP_SR_EN);
6902 I915_WRITE(WM2_LP_ILK, I915_READ(WM2_LP_ILK) & ~WM1_LP_SR_EN);
6903 I915_WRITE(WM1_LP_ILK, I915_READ(WM1_LP_ILK) & ~WM1_LP_SR_EN);
6904
6905 /*
6906 * Don't touch WM1S_LP_EN here.
6907 * Doing so could cause underruns.
6908 */
6909}
6910
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006911static void ironlake_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006912{
Chris Wilsonfac5e232016-07-04 11:34:36 +01006913 struct drm_i915_private *dev_priv = to_i915(dev);
Damien Lespiau231e54f2012-10-19 17:55:41 +01006914 uint32_t dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006915
Damien Lespiauf1e8fa52013-06-07 17:41:09 +01006916 /*
6917 * Required for FBC
6918 * WaFbcDisableDpfcClockGating:ilk
6919 */
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01006920 dspclk_gate |= ILK_DPFCRUNIT_CLOCK_GATE_DISABLE |
6921 ILK_DPFCUNIT_CLOCK_GATE_DISABLE |
6922 ILK_DPFDUNIT_CLOCK_GATE_ENABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006923
6924 I915_WRITE(PCH_3DCGDIS0,
6925 MARIUNIT_CLOCK_GATE_DISABLE |
6926 SVSMUNIT_CLOCK_GATE_DISABLE);
6927 I915_WRITE(PCH_3DCGDIS1,
6928 VFMUNIT_CLOCK_GATE_DISABLE);
6929
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006930 /*
6931 * According to the spec the following bits should be set in
6932 * order to enable memory self-refresh
6933 * The bit 22/21 of 0x42004
6934 * The bit 5 of 0x42020
6935 * The bit 15 of 0x45000
6936 */
6937 I915_WRITE(ILK_DISPLAY_CHICKEN2,
6938 (I915_READ(ILK_DISPLAY_CHICKEN2) |
6939 ILK_DPARB_GATE | ILK_VSDPFD_FULL));
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01006940 dspclk_gate |= ILK_DPARBUNIT_CLOCK_GATE_ENABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006941 I915_WRITE(DISP_ARB_CTL,
6942 (I915_READ(DISP_ARB_CTL) |
6943 DISP_FBC_WM_DIS));
Ville Syrjälä017636c2013-12-05 15:51:37 +02006944
6945 ilk_init_lp_watermarks(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006946
6947 /*
6948 * Based on the document from hardware guys the following bits
6949 * should be set unconditionally in order to enable FBC.
6950 * The bit 22 of 0x42000
6951 * The bit 22 of 0x42004
6952 * The bit 7,8,9 of 0x42020.
6953 */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01006954 if (IS_IRONLAKE_M(dev_priv)) {
Damien Lespiau4bb35332013-06-14 15:23:24 +01006955 /* WaFbcAsynchFlipDisableFbcQueue:ilk */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006956 I915_WRITE(ILK_DISPLAY_CHICKEN1,
6957 I915_READ(ILK_DISPLAY_CHICKEN1) |
6958 ILK_FBCQ_DIS);
6959 I915_WRITE(ILK_DISPLAY_CHICKEN2,
6960 I915_READ(ILK_DISPLAY_CHICKEN2) |
6961 ILK_DPARB_GATE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006962 }
6963
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01006964 I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
6965
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006966 I915_WRITE(ILK_DISPLAY_CHICKEN2,
6967 I915_READ(ILK_DISPLAY_CHICKEN2) |
6968 ILK_ELPIN_409_SELECT);
6969 I915_WRITE(_3D_CHICKEN2,
6970 _3D_CHICKEN2_WM_READ_PIPELINED << 16 |
6971 _3D_CHICKEN2_WM_READ_PIPELINED);
Daniel Vetter4358a372012-10-18 11:49:51 +02006972
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006973 /* WaDisableRenderCachePipelinedFlush:ilk */
Daniel Vetter4358a372012-10-18 11:49:51 +02006974 I915_WRITE(CACHE_MODE_0,
6975 _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
Daniel Vetter3107bd42012-10-31 22:52:31 +01006976
Akash Goel4e046322014-04-04 17:14:38 +05306977 /* WaDisable_RenderCache_OperationalFlush:ilk */
6978 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
6979
Ville Syrjälä0e088b82013-06-07 10:47:04 +03006980 g4x_disable_trickle_feed(dev);
Ville Syrjäläbdad2b22013-06-07 10:47:03 +03006981
Daniel Vetter3107bd42012-10-31 22:52:31 +01006982 ibx_init_clock_gating(dev);
6983}
6984
6985static void cpt_init_clock_gating(struct drm_device *dev)
6986{
Chris Wilsonfac5e232016-07-04 11:34:36 +01006987 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter3107bd42012-10-31 22:52:31 +01006988 int pipe;
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03006989 uint32_t val;
Daniel Vetter3107bd42012-10-31 22:52:31 +01006990
6991 /*
6992 * On Ibex Peak and Cougar Point, we need to disable clock
6993 * gating for the panel power sequencer or it will fail to
6994 * start up when no ports are active.
6995 */
Jesse Barnescd664072013-10-02 10:34:19 -07006996 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE |
6997 PCH_DPLUNIT_CLOCK_GATE_DISABLE |
6998 PCH_CPUNIT_CLOCK_GATE_DISABLE);
Daniel Vetter3107bd42012-10-31 22:52:31 +01006999 I915_WRITE(SOUTH_CHICKEN2, I915_READ(SOUTH_CHICKEN2) |
7000 DPLS_EDP_PPS_FIX_DIS);
Takashi Iwai335c07b2012-12-11 11:46:29 +01007001 /* The below fixes the weird display corruption, a few pixels shifted
7002 * downward, on (only) LVDS of some HP laptops with IVY.
7003 */
Damien Lespiau055e3932014-08-18 13:49:10 +01007004 for_each_pipe(dev_priv, pipe) {
Paulo Zanonidc4bd2d2013-04-08 15:48:08 -03007005 val = I915_READ(TRANS_CHICKEN2(pipe));
7006 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
7007 val &= ~TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03007008 if (dev_priv->vbt.fdi_rx_polarity_inverted)
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03007009 val |= TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
Paulo Zanonidc4bd2d2013-04-08 15:48:08 -03007010 val &= ~TRANS_CHICKEN2_FRAME_START_DELAY_MASK;
7011 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_COUNTER;
7012 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_MODESWITCH;
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03007013 I915_WRITE(TRANS_CHICKEN2(pipe), val);
7014 }
Daniel Vetter3107bd42012-10-31 22:52:31 +01007015 /* WADP0ClockGatingDisable */
Damien Lespiau055e3932014-08-18 13:49:10 +01007016 for_each_pipe(dev_priv, pipe) {
Daniel Vetter3107bd42012-10-31 22:52:31 +01007017 I915_WRITE(TRANS_CHICKEN1(pipe),
7018 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
7019 }
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007020}
7021
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01007022static void gen6_check_mch_setup(struct drm_device *dev)
7023{
Chris Wilsonfac5e232016-07-04 11:34:36 +01007024 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01007025 uint32_t tmp;
7026
7027 tmp = I915_READ(MCH_SSKPD);
Daniel Vetterdf662a22014-08-04 11:17:25 +02007028 if ((tmp & MCH_SSKPD_WM0_MASK) != MCH_SSKPD_WM0_VAL)
7029 DRM_DEBUG_KMS("Wrong MCH_SSKPD value: 0x%08x This can cause underruns.\n",
7030 tmp);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01007031}
7032
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007033static void gen6_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007034{
Chris Wilsonfac5e232016-07-04 11:34:36 +01007035 struct drm_i915_private *dev_priv = to_i915(dev);
Damien Lespiau231e54f2012-10-19 17:55:41 +01007036 uint32_t dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007037
Damien Lespiau231e54f2012-10-19 17:55:41 +01007038 I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007039
7040 I915_WRITE(ILK_DISPLAY_CHICKEN2,
7041 I915_READ(ILK_DISPLAY_CHICKEN2) |
7042 ILK_ELPIN_409_SELECT);
7043
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007044 /* WaDisableHiZPlanesWhenMSAAEnabled:snb */
Daniel Vetter42839082012-12-14 23:38:28 +01007045 I915_WRITE(_3D_CHICKEN,
7046 _MASKED_BIT_ENABLE(_3D_CHICKEN_HIZ_PLANE_DISABLE_MSAA_4X_SNB));
7047
Akash Goel4e046322014-04-04 17:14:38 +05307048 /* WaDisable_RenderCache_OperationalFlush:snb */
7049 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
7050
Ville Syrjälä8d85d272014-02-04 21:59:15 +02007051 /*
7052 * BSpec recoomends 8x4 when MSAA is used,
7053 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02007054 *
7055 * Note that PS/WM thread counts depend on the WIZ hashing
7056 * disable bit, which we don't touch here, but it's good
7057 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjälä8d85d272014-02-04 21:59:15 +02007058 */
7059 I915_WRITE(GEN6_GT_MODE,
Damien Lespiau98533252014-12-08 17:33:51 +00007060 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
Ville Syrjälä8d85d272014-02-04 21:59:15 +02007061
Ville Syrjälä017636c2013-12-05 15:51:37 +02007062 ilk_init_lp_watermarks(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007063
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007064 I915_WRITE(CACHE_MODE_0,
Daniel Vetter50743292012-04-26 22:02:54 +02007065 _MASKED_BIT_DISABLE(CM0_STC_EVICT_DISABLE_LRA_SNB));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007066
7067 I915_WRITE(GEN6_UCGCTL1,
7068 I915_READ(GEN6_UCGCTL1) |
7069 GEN6_BLBUNIT_CLOCK_GATE_DISABLE |
7070 GEN6_CSUNIT_CLOCK_GATE_DISABLE);
7071
7072 /* According to the BSpec vol1g, bit 12 (RCPBUNIT) clock
7073 * gating disable must be set. Failure to set it results in
7074 * flickering pixels due to Z write ordering failures after
7075 * some amount of runtime in the Mesa "fire" demo, and Unigine
7076 * Sanctuary and Tropics, and apparently anything else with
7077 * alpha test or pixel discard.
7078 *
7079 * According to the spec, bit 11 (RCCUNIT) must also be set,
7080 * but we didn't debug actual testcases to find it out.
Jesse Barnes0f846f82012-06-14 11:04:47 -07007081 *
Ville Syrjäläef593182014-01-22 21:32:47 +02007082 * WaDisableRCCUnitClockGating:snb
7083 * WaDisableRCPBUnitClockGating:snb
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007084 */
7085 I915_WRITE(GEN6_UCGCTL2,
7086 GEN6_RCPBUNIT_CLOCK_GATE_DISABLE |
7087 GEN6_RCCUNIT_CLOCK_GATE_DISABLE);
7088
Ville Syrjälä5eb146d2014-02-04 21:59:16 +02007089 /* WaStripsFansDisableFastClipPerformanceFix:snb */
Ville Syrjälä743b57d2014-02-04 21:59:17 +02007090 I915_WRITE(_3D_CHICKEN3,
7091 _MASKED_BIT_ENABLE(_3D_CHICKEN3_SF_DISABLE_FASTCLIP_CULL));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007092
7093 /*
Ville Syrjäläe927ecd2014-02-04 21:59:18 +02007094 * Bspec says:
7095 * "This bit must be set if 3DSTATE_CLIP clip mode is set to normal and
7096 * 3DSTATE_SF number of SF output attributes is more than 16."
7097 */
7098 I915_WRITE(_3D_CHICKEN3,
7099 _MASKED_BIT_ENABLE(_3D_CHICKEN3_SF_DISABLE_PIPELINED_ATTR_FETCH));
7100
7101 /*
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007102 * According to the spec the following bits should be
7103 * set in order to enable memory self-refresh and fbc:
7104 * The bit21 and bit22 of 0x42000
7105 * The bit21 and bit22 of 0x42004
7106 * The bit5 and bit7 of 0x42020
7107 * The bit14 of 0x70180
7108 * The bit14 of 0x71180
Damien Lespiau4bb35332013-06-14 15:23:24 +01007109 *
7110 * WaFbcAsynchFlipDisableFbcQueue:snb
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007111 */
7112 I915_WRITE(ILK_DISPLAY_CHICKEN1,
7113 I915_READ(ILK_DISPLAY_CHICKEN1) |
7114 ILK_FBCQ_DIS | ILK_PABSTRETCH_DIS);
7115 I915_WRITE(ILK_DISPLAY_CHICKEN2,
7116 I915_READ(ILK_DISPLAY_CHICKEN2) |
7117 ILK_DPARB_GATE | ILK_VSDPFD_FULL);
Damien Lespiau231e54f2012-10-19 17:55:41 +01007118 I915_WRITE(ILK_DSPCLK_GATE_D,
7119 I915_READ(ILK_DSPCLK_GATE_D) |
7120 ILK_DPARBUNIT_CLOCK_GATE_ENABLE |
7121 ILK_DPFDUNIT_CLOCK_GATE_ENABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007122
Ville Syrjälä0e088b82013-06-07 10:47:04 +03007123 g4x_disable_trickle_feed(dev);
Ben Widawskyf8f2ac92012-10-03 19:34:24 -07007124
Daniel Vetter3107bd42012-10-31 22:52:31 +01007125 cpt_init_clock_gating(dev);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01007126
7127 gen6_check_mch_setup(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007128}
7129
7130static void gen7_setup_fixed_func_scheduler(struct drm_i915_private *dev_priv)
7131{
7132 uint32_t reg = I915_READ(GEN7_FF_THREAD_MODE);
7133
Ville Syrjälä3aad9052014-01-22 21:32:59 +02007134 /*
Ville Syrjälä46680e02014-01-22 21:33:01 +02007135 * WaVSThreadDispatchOverride:ivb,vlv
Ville Syrjälä3aad9052014-01-22 21:32:59 +02007136 *
7137 * This actually overrides the dispatch
7138 * mode for all thread types.
7139 */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007140 reg &= ~GEN7_FF_SCHED_MASK;
7141 reg |= GEN7_FF_TS_SCHED_HW;
7142 reg |= GEN7_FF_VS_SCHED_HW;
7143 reg |= GEN7_FF_DS_SCHED_HW;
7144
7145 I915_WRITE(GEN7_FF_THREAD_MODE, reg);
7146}
7147
Paulo Zanoni17a303e2012-11-20 15:12:07 -02007148static void lpt_init_clock_gating(struct drm_device *dev)
7149{
Chris Wilsonfac5e232016-07-04 11:34:36 +01007150 struct drm_i915_private *dev_priv = to_i915(dev);
Paulo Zanoni17a303e2012-11-20 15:12:07 -02007151
7152 /*
7153 * TODO: this bit should only be enabled when really needed, then
7154 * disabled when not needed anymore in order to save power.
7155 */
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01007156 if (HAS_PCH_LPT_LP(dev_priv))
Paulo Zanoni17a303e2012-11-20 15:12:07 -02007157 I915_WRITE(SOUTH_DSPCLK_GATE_D,
7158 I915_READ(SOUTH_DSPCLK_GATE_D) |
7159 PCH_LP_PARTITION_LEVEL_DISABLE);
Paulo Zanoni0a790cd2013-04-17 18:15:49 -03007160
7161 /* WADPOClockGatingDisable:hsw */
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03007162 I915_WRITE(TRANS_CHICKEN1(PIPE_A),
7163 I915_READ(TRANS_CHICKEN1(PIPE_A)) |
Paulo Zanoni0a790cd2013-04-17 18:15:49 -03007164 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
Paulo Zanoni17a303e2012-11-20 15:12:07 -02007165}
7166
Imre Deak7d708ee2013-04-17 14:04:50 +03007167static void lpt_suspend_hw(struct drm_device *dev)
7168{
Chris Wilsonfac5e232016-07-04 11:34:36 +01007169 struct drm_i915_private *dev_priv = to_i915(dev);
Imre Deak7d708ee2013-04-17 14:04:50 +03007170
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01007171 if (HAS_PCH_LPT_LP(dev_priv)) {
Imre Deak7d708ee2013-04-17 14:04:50 +03007172 uint32_t val = I915_READ(SOUTH_DSPCLK_GATE_D);
7173
7174 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
7175 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
7176 }
7177}
7178
Imre Deak450174f2016-05-03 15:54:21 +03007179static void gen8_set_l3sqc_credits(struct drm_i915_private *dev_priv,
7180 int general_prio_credits,
7181 int high_prio_credits)
7182{
7183 u32 misccpctl;
7184
7185 /* WaTempDisableDOPClkGating:bdw */
7186 misccpctl = I915_READ(GEN7_MISCCPCTL);
7187 I915_WRITE(GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
7188
7189 I915_WRITE(GEN8_L3SQCREG1,
7190 L3_GENERAL_PRIO_CREDITS(general_prio_credits) |
7191 L3_HIGH_PRIO_CREDITS(high_prio_credits));
7192
7193 /*
7194 * Wait at least 100 clocks before re-enabling clock gating.
7195 * See the definition of L3SQCREG1 in BSpec.
7196 */
7197 POSTING_READ(GEN8_L3SQCREG1);
7198 udelay(1);
7199 I915_WRITE(GEN7_MISCCPCTL, misccpctl);
7200}
7201
Mika Kuoppala9498dba2016-06-07 17:19:01 +03007202static void kabylake_init_clock_gating(struct drm_device *dev)
7203{
Mika Kuoppala9146f302016-06-07 17:19:01 +03007204 struct drm_i915_private *dev_priv = dev->dev_private;
Mika Kuoppala9498dba2016-06-07 17:19:01 +03007205
Mika Kuoppalab033bb62016-06-07 17:19:04 +03007206 gen9_init_clock_gating(dev);
Mika Kuoppala9498dba2016-06-07 17:19:01 +03007207
7208 /* WaDisableSDEUnitClockGating:kbl */
7209 if (IS_KBL_REVID(dev_priv, 0, KBL_REVID_B0))
7210 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
7211 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Mika Kuoppala8aeb7f62016-06-07 17:19:05 +03007212
7213 /* WaDisableGamClockGating:kbl */
7214 if (IS_KBL_REVID(dev_priv, 0, KBL_REVID_B0))
7215 I915_WRITE(GEN6_UCGCTL1, I915_READ(GEN6_UCGCTL1) |
7216 GEN6_GAMUNIT_CLOCK_GATE_DISABLE);
Mika Kuoppala031cd8c2016-06-07 17:19:18 +03007217
7218 /* WaFbcNukeOnHostModify:kbl */
7219 I915_WRITE(ILK_DPFC_CHICKEN, I915_READ(ILK_DPFC_CHICKEN) |
7220 ILK_DPFC_NUKE_ON_ANY_MODIFICATION);
Mika Kuoppala9498dba2016-06-07 17:19:01 +03007221}
7222
Daniel Vetterdc00b6a2016-05-19 09:14:20 +02007223static void skylake_init_clock_gating(struct drm_device *dev)
7224{
Mika Kuoppalac584e2d2016-06-07 17:19:18 +03007225 struct drm_i915_private *dev_priv = dev->dev_private;
Mika Kuoppala44fff992016-06-07 17:19:09 +03007226
Mika Kuoppalab033bb62016-06-07 17:19:04 +03007227 gen9_init_clock_gating(dev);
Mika Kuoppala44fff992016-06-07 17:19:09 +03007228
7229 /* WAC6entrylatency:skl */
7230 I915_WRITE(FBC_LLC_READ_CTRL, I915_READ(FBC_LLC_READ_CTRL) |
7231 FBC_LLC_FULLY_OPEN);
Mika Kuoppala031cd8c2016-06-07 17:19:18 +03007232
7233 /* WaFbcNukeOnHostModify:skl */
7234 I915_WRITE(ILK_DPFC_CHICKEN, I915_READ(ILK_DPFC_CHICKEN) |
7235 ILK_DPFC_NUKE_ON_ANY_MODIFICATION);
Daniel Vetterdc00b6a2016-05-19 09:14:20 +02007236}
7237
Paulo Zanoni47c2bd92014-08-21 17:09:37 -03007238static void broadwell_init_clock_gating(struct drm_device *dev)
Ben Widawsky1020a5c2013-11-02 21:07:06 -07007239{
Chris Wilsonfac5e232016-07-04 11:34:36 +01007240 struct drm_i915_private *dev_priv = to_i915(dev);
Damien Lespiau07d27e22014-03-03 17:31:46 +00007241 enum pipe pipe;
Ben Widawsky1020a5c2013-11-02 21:07:06 -07007242
Ville Syrjälä7ad0dba2015-05-19 20:32:55 +03007243 ilk_init_lp_watermarks(dev);
Ben Widawsky50ed5fb2013-11-02 21:07:40 -07007244
Ben Widawskyab57fff2013-12-12 15:28:04 -08007245 /* WaSwitchSolVfFArbitrationPriority:bdw */
Ben Widawsky50ed5fb2013-11-02 21:07:40 -07007246 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07007247
Ben Widawskyab57fff2013-12-12 15:28:04 -08007248 /* WaPsrDPAMaskVBlankInSRD:bdw */
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07007249 I915_WRITE(CHICKEN_PAR1_1,
7250 I915_READ(CHICKEN_PAR1_1) | DPA_MASK_VBLANK_SRD);
7251
Ben Widawskyab57fff2013-12-12 15:28:04 -08007252 /* WaPsrDPRSUnmaskVBlankInSRD:bdw */
Damien Lespiau055e3932014-08-18 13:49:10 +01007253 for_each_pipe(dev_priv, pipe) {
Damien Lespiau07d27e22014-03-03 17:31:46 +00007254 I915_WRITE(CHICKEN_PIPESL_1(pipe),
Ville Syrjäläc7c65622014-03-05 13:05:45 +02007255 I915_READ(CHICKEN_PIPESL_1(pipe)) |
Ville Syrjälä8f670bb2014-03-05 13:05:47 +02007256 BDW_DPRS_MASK_VBLANK_SRD);
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07007257 }
Ben Widawsky63801f22013-12-12 17:26:03 -08007258
Ben Widawskyab57fff2013-12-12 15:28:04 -08007259 /* WaVSRefCountFullforceMissDisable:bdw */
7260 /* WaDSRefCountFullforceMissDisable:bdw */
7261 I915_WRITE(GEN7_FF_THREAD_MODE,
7262 I915_READ(GEN7_FF_THREAD_MODE) &
7263 ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
Ville Syrjälä36075a42014-02-04 21:59:21 +02007264
Ville Syrjälä295e8bb2014-02-27 21:59:01 +02007265 I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
7266 _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
Ville Syrjälä4f1ca9e2014-02-27 21:59:02 +02007267
7268 /* WaDisableSDEUnitClockGating:bdw */
7269 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
7270 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Damien Lespiau5d708682014-03-26 18:41:51 +00007271
Imre Deak450174f2016-05-03 15:54:21 +03007272 /* WaProgramL3SqcReg1Default:bdw */
7273 gen8_set_l3sqc_credits(dev_priv, 30, 2);
Ville Syrjälä4d487cf2015-05-19 20:32:56 +03007274
Ville Syrjälä6d50b062015-05-19 20:32:57 +03007275 /*
7276 * WaGttCachingOffByDefault:bdw
7277 * GTT cache may not work with big pages, so if those
7278 * are ever enabled GTT cache may need to be disabled.
7279 */
7280 I915_WRITE(HSW_GTT_CACHE_EN, GTT_CACHE_EN_ALL);
7281
Mika Kuoppala17e0adf2016-06-07 17:19:02 +03007282 /* WaKVMNotificationOnConfigChange:bdw */
7283 I915_WRITE(CHICKEN_PAR2_1, I915_READ(CHICKEN_PAR2_1)
7284 | KVM_CONFIG_CHANGE_NOTIFICATION_SELECT);
7285
Paulo Zanoni89d6b2b2014-08-21 17:09:36 -03007286 lpt_init_clock_gating(dev);
Ben Widawsky1020a5c2013-11-02 21:07:06 -07007287}
7288
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03007289static void haswell_init_clock_gating(struct drm_device *dev)
7290{
Chris Wilsonfac5e232016-07-04 11:34:36 +01007291 struct drm_i915_private *dev_priv = to_i915(dev);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03007292
Ville Syrjälä017636c2013-12-05 15:51:37 +02007293 ilk_init_lp_watermarks(dev);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03007294
Francisco Jerezf3fc4882013-10-02 15:53:16 -07007295 /* L3 caching of data atomics doesn't work -- disable it. */
7296 I915_WRITE(HSW_SCRATCH1, HSW_SCRATCH1_L3_DATA_ATOMICS_DISABLE);
7297 I915_WRITE(HSW_ROW_CHICKEN3,
7298 _MASKED_BIT_ENABLE(HSW_ROW_CHICKEN3_L3_GLOBAL_ATOMICS_DISABLE));
7299
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007300 /* This is required by WaCatErrorRejectionIssue:hsw */
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03007301 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
7302 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
7303 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
7304
Ville Syrjäläe36ea7f2014-01-22 21:33:00 +02007305 /* WaVSRefCountFullforceMissDisable:hsw */
7306 I915_WRITE(GEN7_FF_THREAD_MODE,
7307 I915_READ(GEN7_FF_THREAD_MODE) & ~GEN7_FF_VS_REF_CNT_FFME);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03007308
Akash Goel4e046322014-04-04 17:14:38 +05307309 /* WaDisable_RenderCache_OperationalFlush:hsw */
7310 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
7311
Chia-I Wufe27c602014-01-28 13:29:33 +08007312 /* enable HiZ Raw Stall Optimization */
7313 I915_WRITE(CACHE_MODE_0_GEN7,
7314 _MASKED_BIT_DISABLE(HIZ_RAW_STALL_OPT_DISABLE));
7315
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007316 /* WaDisable4x2SubspanOptimization:hsw */
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03007317 I915_WRITE(CACHE_MODE_1,
7318 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Eugeni Dodonov1544d9d2012-07-02 11:51:10 -03007319
Ville Syrjäläa12c4962014-02-04 21:59:20 +02007320 /*
7321 * BSpec recommends 8x4 when MSAA is used,
7322 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02007323 *
7324 * Note that PS/WM thread counts depend on the WIZ hashing
7325 * disable bit, which we don't touch here, but it's good
7326 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjäläa12c4962014-02-04 21:59:20 +02007327 */
7328 I915_WRITE(GEN7_GT_MODE,
Damien Lespiau98533252014-12-08 17:33:51 +00007329 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
Ville Syrjäläa12c4962014-02-04 21:59:20 +02007330
Kenneth Graunke94411592014-12-31 16:23:00 -08007331 /* WaSampleCChickenBitEnable:hsw */
7332 I915_WRITE(HALF_SLICE_CHICKEN3,
7333 _MASKED_BIT_ENABLE(HSW_SAMPLE_C_PERFORMANCE));
7334
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007335 /* WaSwitchSolVfFArbitrationPriority:hsw */
Ben Widawskye3dff582013-03-20 14:49:14 -07007336 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
7337
Paulo Zanoni90a88642013-05-03 17:23:45 -03007338 /* WaRsPkgCStateDisplayPMReq:hsw */
7339 I915_WRITE(CHICKEN_PAR1_1,
7340 I915_READ(CHICKEN_PAR1_1) | FORCE_ARB_IDLE_PLANES);
Eugeni Dodonov1544d9d2012-07-02 11:51:10 -03007341
Paulo Zanoni17a303e2012-11-20 15:12:07 -02007342 lpt_init_clock_gating(dev);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03007343}
7344
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007345static void ivybridge_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007346{
Chris Wilsonfac5e232016-07-04 11:34:36 +01007347 struct drm_i915_private *dev_priv = to_i915(dev);
Ben Widawsky20848222012-05-04 18:58:59 -07007348 uint32_t snpcr;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007349
Ville Syrjälä017636c2013-12-05 15:51:37 +02007350 ilk_init_lp_watermarks(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007351
Damien Lespiau231e54f2012-10-19 17:55:41 +01007352 I915_WRITE(ILK_DSPCLK_GATE_D, ILK_VRHUNIT_CLOCK_GATE_DISABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007353
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007354 /* WaDisableEarlyCull:ivb */
Jesse Barnes87f80202012-10-02 17:43:41 -05007355 I915_WRITE(_3D_CHICKEN3,
7356 _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
7357
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007358 /* WaDisableBackToBackFlipFix:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007359 I915_WRITE(IVB_CHICKEN3,
7360 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
7361 CHICKEN3_DGMG_DONE_FIX_DISABLE);
7362
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007363 /* WaDisablePSDDualDispatchEnable:ivb */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01007364 if (IS_IVB_GT1(dev_priv))
Jesse Barnes12f33822012-10-25 12:15:45 -07007365 I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
7366 _MASKED_BIT_ENABLE(GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
Jesse Barnes12f33822012-10-25 12:15:45 -07007367
Akash Goel4e046322014-04-04 17:14:38 +05307368 /* WaDisable_RenderCache_OperationalFlush:ivb */
7369 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
7370
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007371 /* Apply the WaDisableRHWOOptimizationForRenderHang:ivb workaround. */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007372 I915_WRITE(GEN7_COMMON_SLICE_CHICKEN1,
7373 GEN7_CSC1_RHWO_OPT_DISABLE_IN_RCC);
7374
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007375 /* WaApplyL3ControlAndL3ChickenMode:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007376 I915_WRITE(GEN7_L3CNTLREG1,
7377 GEN7_WA_FOR_GEN7_L3_CONTROL);
7378 I915_WRITE(GEN7_L3_CHICKEN_MODE_REGISTER,
Jesse Barnes8ab43972012-10-25 12:15:42 -07007379 GEN7_WA_L3_CHICKEN_MODE);
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01007380 if (IS_IVB_GT1(dev_priv))
Jesse Barnes8ab43972012-10-25 12:15:42 -07007381 I915_WRITE(GEN7_ROW_CHICKEN2,
7382 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Ville Syrjälä412236c2014-01-22 21:32:44 +02007383 else {
7384 /* must write both registers */
7385 I915_WRITE(GEN7_ROW_CHICKEN2,
7386 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Jesse Barnes8ab43972012-10-25 12:15:42 -07007387 I915_WRITE(GEN7_ROW_CHICKEN2_GT2,
7388 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Ville Syrjälä412236c2014-01-22 21:32:44 +02007389 }
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007390
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007391 /* WaForceL3Serialization:ivb */
Jesse Barnes61939d92012-10-02 17:43:38 -05007392 I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
7393 ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
7394
Ville Syrjälä1b80a19a2014-01-22 21:32:53 +02007395 /*
Jesse Barnes0f846f82012-06-14 11:04:47 -07007396 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007397 * This implements the WaDisableRCZUnitClockGating:ivb workaround.
Jesse Barnes0f846f82012-06-14 11:04:47 -07007398 */
7399 I915_WRITE(GEN6_UCGCTL2,
Ville Syrjälä28acf3b2014-01-22 21:32:48 +02007400 GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
Jesse Barnes0f846f82012-06-14 11:04:47 -07007401
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007402 /* This is required by WaCatErrorRejectionIssue:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007403 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
7404 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
7405 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
7406
Ville Syrjälä0e088b82013-06-07 10:47:04 +03007407 g4x_disable_trickle_feed(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007408
7409 gen7_setup_fixed_func_scheduler(dev_priv);
Daniel Vetter97e19302012-04-24 16:00:21 +02007410
Chris Wilson22721342014-03-04 09:41:43 +00007411 if (0) { /* causes HiZ corruption on ivb:gt1 */
7412 /* enable HiZ Raw Stall Optimization */
7413 I915_WRITE(CACHE_MODE_0_GEN7,
7414 _MASKED_BIT_DISABLE(HIZ_RAW_STALL_OPT_DISABLE));
7415 }
Chia-I Wu116f2b62014-01-28 13:29:34 +08007416
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007417 /* WaDisable4x2SubspanOptimization:ivb */
Daniel Vetter97e19302012-04-24 16:00:21 +02007418 I915_WRITE(CACHE_MODE_1,
7419 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Ben Widawsky20848222012-05-04 18:58:59 -07007420
Ville Syrjäläa607c1a2014-02-04 21:59:19 +02007421 /*
7422 * BSpec recommends 8x4 when MSAA is used,
7423 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02007424 *
7425 * Note that PS/WM thread counts depend on the WIZ hashing
7426 * disable bit, which we don't touch here, but it's good
7427 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjäläa607c1a2014-02-04 21:59:19 +02007428 */
7429 I915_WRITE(GEN7_GT_MODE,
Damien Lespiau98533252014-12-08 17:33:51 +00007430 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
Ville Syrjäläa607c1a2014-02-04 21:59:19 +02007431
Ben Widawsky20848222012-05-04 18:58:59 -07007432 snpcr = I915_READ(GEN6_MBCUNIT_SNPCR);
7433 snpcr &= ~GEN6_MBC_SNPCR_MASK;
7434 snpcr |= GEN6_MBC_SNPCR_MED;
7435 I915_WRITE(GEN6_MBCUNIT_SNPCR, snpcr);
Daniel Vetter3107bd42012-10-31 22:52:31 +01007436
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01007437 if (!HAS_PCH_NOP(dev_priv))
Ben Widawskyab5c6082013-04-05 13:12:41 -07007438 cpt_init_clock_gating(dev);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01007439
7440 gen6_check_mch_setup(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007441}
7442
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007443static void valleyview_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007444{
Chris Wilsonfac5e232016-07-04 11:34:36 +01007445 struct drm_i915_private *dev_priv = to_i915(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007446
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007447 /* WaDisableEarlyCull:vlv */
Jesse Barnes87f80202012-10-02 17:43:41 -05007448 I915_WRITE(_3D_CHICKEN3,
7449 _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
7450
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007451 /* WaDisableBackToBackFlipFix:vlv */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007452 I915_WRITE(IVB_CHICKEN3,
7453 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
7454 CHICKEN3_DGMG_DONE_FIX_DISABLE);
7455
Ville Syrjäläfad7d362014-01-22 21:32:39 +02007456 /* WaPsdDispatchEnable:vlv */
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007457 /* WaDisablePSDDualDispatchEnable:vlv */
Jesse Barnes12f33822012-10-25 12:15:45 -07007458 I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
Jesse Barnesd3bc0302013-03-08 10:45:51 -08007459 _MASKED_BIT_ENABLE(GEN7_MAX_PS_THREAD_DEP |
7460 GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
Jesse Barnes12f33822012-10-25 12:15:45 -07007461
Akash Goel4e046322014-04-04 17:14:38 +05307462 /* WaDisable_RenderCache_OperationalFlush:vlv */
7463 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
7464
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007465 /* WaForceL3Serialization:vlv */
Jesse Barnes61939d92012-10-02 17:43:38 -05007466 I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
7467 ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
7468
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007469 /* WaDisableDopClockGating:vlv */
Jesse Barnes8ab43972012-10-25 12:15:42 -07007470 I915_WRITE(GEN7_ROW_CHICKEN2,
7471 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
7472
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007473 /* This is required by WaCatErrorRejectionIssue:vlv */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007474 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
7475 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
7476 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
7477
Ville Syrjälä46680e02014-01-22 21:33:01 +02007478 gen7_setup_fixed_func_scheduler(dev_priv);
7479
Ville Syrjälä3c0edae2014-01-22 21:32:56 +02007480 /*
Jesse Barnes0f846f82012-06-14 11:04:47 -07007481 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007482 * This implements the WaDisableRCZUnitClockGating:vlv workaround.
Jesse Barnes0f846f82012-06-14 11:04:47 -07007483 */
7484 I915_WRITE(GEN6_UCGCTL2,
Ville Syrjälä3c0edae2014-01-22 21:32:56 +02007485 GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
Jesse Barnes0f846f82012-06-14 11:04:47 -07007486
Akash Goelc98f5062014-03-24 23:00:07 +05307487 /* WaDisableL3Bank2xClockGate:vlv
7488 * Disabling L3 clock gating- MMIO 940c[25] = 1
7489 * Set bit 25, to disable L3_BANK_2x_CLK_GATING */
7490 I915_WRITE(GEN7_UCGCTL4,
7491 I915_READ(GEN7_UCGCTL4) | GEN7_L3BANK2X_CLOCK_GATE_DISABLE);
Jesse Barnese3f33d42012-06-14 11:04:50 -07007492
Ville Syrjäläafd58e72014-01-22 21:33:03 +02007493 /*
7494 * BSpec says this must be set, even though
7495 * WaDisable4x2SubspanOptimization isn't listed for VLV.
7496 */
Daniel Vetter6b26c862012-04-24 14:04:12 +02007497 I915_WRITE(CACHE_MODE_1,
7498 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Jesse Barnes79831172012-06-20 10:53:12 -07007499
7500 /*
Ville Syrjäläda2518f2015-01-21 19:38:01 +02007501 * BSpec recommends 8x4 when MSAA is used,
7502 * however in practice 16x4 seems fastest.
7503 *
7504 * Note that PS/WM thread counts depend on the WIZ hashing
7505 * disable bit, which we don't touch here, but it's good
7506 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
7507 */
7508 I915_WRITE(GEN7_GT_MODE,
7509 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
7510
7511 /*
Ville Syrjälä031994e2014-01-22 21:32:46 +02007512 * WaIncreaseL3CreditsForVLVB0:vlv
7513 * This is the hardware default actually.
7514 */
7515 I915_WRITE(GEN7_L3SQCREG1, VLV_B0_WA_L3SQCREG1_VALUE);
7516
7517 /*
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007518 * WaDisableVLVClockGating_VBIIssue:vlv
Jesse Barnes2d809572012-10-25 12:15:44 -07007519 * Disable clock gating on th GCFG unit to prevent a delay
7520 * in the reporting of vblank events.
7521 */
Ville Syrjälä7a0d1ee2014-01-22 21:33:04 +02007522 I915_WRITE(VLV_GUNIT_CLOCK_GATE, GCFG_DIS);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007523}
7524
Ville Syrjäläa4565da2014-04-09 13:28:10 +03007525static void cherryview_init_clock_gating(struct drm_device *dev)
7526{
Chris Wilsonfac5e232016-07-04 11:34:36 +01007527 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläa4565da2014-04-09 13:28:10 +03007528
Ville Syrjälä232ce332014-04-09 13:28:35 +03007529 /* WaVSRefCountFullforceMissDisable:chv */
7530 /* WaDSRefCountFullforceMissDisable:chv */
7531 I915_WRITE(GEN7_FF_THREAD_MODE,
7532 I915_READ(GEN7_FF_THREAD_MODE) &
7533 ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
Ville Syrjäläacea6f92014-04-09 13:28:36 +03007534
7535 /* WaDisableSemaphoreAndSyncFlipWait:chv */
7536 I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
7537 _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
Ville Syrjälä08466972014-04-09 13:28:37 +03007538
7539 /* WaDisableCSUnitClockGating:chv */
7540 I915_WRITE(GEN6_UCGCTL1, I915_READ(GEN6_UCGCTL1) |
7541 GEN6_CSUNIT_CLOCK_GATE_DISABLE);
Ville Syrjäläc6317802014-04-09 13:28:38 +03007542
7543 /* WaDisableSDEUnitClockGating:chv */
7544 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
7545 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Ville Syrjälä6d50b062015-05-19 20:32:57 +03007546
7547 /*
Imre Deak450174f2016-05-03 15:54:21 +03007548 * WaProgramL3SqcReg1Default:chv
7549 * See gfxspecs/Related Documents/Performance Guide/
7550 * LSQC Setting Recommendations.
7551 */
7552 gen8_set_l3sqc_credits(dev_priv, 38, 2);
7553
7554 /*
Ville Syrjälä6d50b062015-05-19 20:32:57 +03007555 * GTT cache may not work with big pages, so if those
7556 * are ever enabled GTT cache may need to be disabled.
7557 */
7558 I915_WRITE(HSW_GTT_CACHE_EN, GTT_CACHE_EN_ALL);
Ville Syrjäläa4565da2014-04-09 13:28:10 +03007559}
7560
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007561static void g4x_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007562{
Chris Wilsonfac5e232016-07-04 11:34:36 +01007563 struct drm_i915_private *dev_priv = to_i915(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007564 uint32_t dspclk_gate;
7565
7566 I915_WRITE(RENCLK_GATE_D1, 0);
7567 I915_WRITE(RENCLK_GATE_D2, VF_UNIT_CLOCK_GATE_DISABLE |
7568 GS_UNIT_CLOCK_GATE_DISABLE |
7569 CL_UNIT_CLOCK_GATE_DISABLE);
7570 I915_WRITE(RAMCLK_GATE_D, 0);
7571 dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE |
7572 OVRUNIT_CLOCK_GATE_DISABLE |
7573 OVCUNIT_CLOCK_GATE_DISABLE;
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01007574 if (IS_GM45(dev_priv))
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007575 dspclk_gate |= DSSUNIT_CLOCK_GATE_DISABLE;
7576 I915_WRITE(DSPCLK_GATE_D, dspclk_gate);
Daniel Vetter4358a372012-10-18 11:49:51 +02007577
7578 /* WaDisableRenderCachePipelinedFlush */
7579 I915_WRITE(CACHE_MODE_0,
7580 _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
Ville Syrjäläde1aa622013-06-07 10:47:01 +03007581
Akash Goel4e046322014-04-04 17:14:38 +05307582 /* WaDisable_RenderCache_OperationalFlush:g4x */
7583 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
7584
Ville Syrjälä0e088b82013-06-07 10:47:04 +03007585 g4x_disable_trickle_feed(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007586}
7587
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007588static void crestline_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007589{
Chris Wilsonfac5e232016-07-04 11:34:36 +01007590 struct drm_i915_private *dev_priv = to_i915(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007591
7592 I915_WRITE(RENCLK_GATE_D1, I965_RCC_CLOCK_GATE_DISABLE);
7593 I915_WRITE(RENCLK_GATE_D2, 0);
7594 I915_WRITE(DSPCLK_GATE_D, 0);
7595 I915_WRITE(RAMCLK_GATE_D, 0);
7596 I915_WRITE16(DEUC, 0);
Ville Syrjälä20f94962013-06-07 10:47:02 +03007597 I915_WRITE(MI_ARB_STATE,
7598 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Akash Goel4e046322014-04-04 17:14:38 +05307599
7600 /* WaDisable_RenderCache_OperationalFlush:gen4 */
7601 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007602}
7603
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007604static void broadwater_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007605{
Chris Wilsonfac5e232016-07-04 11:34:36 +01007606 struct drm_i915_private *dev_priv = to_i915(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007607
7608 I915_WRITE(RENCLK_GATE_D1, I965_RCZ_CLOCK_GATE_DISABLE |
7609 I965_RCC_CLOCK_GATE_DISABLE |
7610 I965_RCPB_CLOCK_GATE_DISABLE |
7611 I965_ISC_CLOCK_GATE_DISABLE |
7612 I965_FBC_CLOCK_GATE_DISABLE);
7613 I915_WRITE(RENCLK_GATE_D2, 0);
Ville Syrjälä20f94962013-06-07 10:47:02 +03007614 I915_WRITE(MI_ARB_STATE,
7615 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Akash Goel4e046322014-04-04 17:14:38 +05307616
7617 /* WaDisable_RenderCache_OperationalFlush:gen4 */
7618 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007619}
7620
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007621static void gen3_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007622{
Chris Wilsonfac5e232016-07-04 11:34:36 +01007623 struct drm_i915_private *dev_priv = to_i915(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007624 u32 dstate = I915_READ(D_STATE);
7625
7626 dstate |= DSTATE_PLL_D3_OFF | DSTATE_GFX_CLOCK_GATING |
7627 DSTATE_DOT_CLOCK_GATING;
7628 I915_WRITE(D_STATE, dstate);
Chris Wilson13a86b82012-04-24 14:51:43 +01007629
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02007630 if (IS_PINEVIEW(dev_priv))
Chris Wilson13a86b82012-04-24 14:51:43 +01007631 I915_WRITE(ECOSKPD, _MASKED_BIT_ENABLE(ECO_GATING_CX_ONLY));
Daniel Vetter974a3b02012-09-09 11:54:16 +02007632
7633 /* IIR "flip pending" means done if this bit is set */
7634 I915_WRITE(ECOSKPD, _MASKED_BIT_DISABLE(ECO_FLIP_DONE));
Ville Syrjälä12fabbcb92014-02-25 15:13:38 +02007635
7636 /* interrupts should cause a wake up from C3 */
Ville Syrjälä32992542014-02-25 15:13:39 +02007637 I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_AGPBUSY_INT_EN));
Ville Syrjälädbb42742014-02-25 15:13:41 +02007638
7639 /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
7640 I915_WRITE(MI_ARB_STATE, _MASKED_BIT_ENABLE(MI_ARB_C3_LP_WRITE_ENABLE));
Ville Syrjälä10383922014-08-15 01:21:54 +03007641
7642 I915_WRITE(MI_ARB_STATE,
7643 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007644}
7645
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007646static void i85x_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007647{
Chris Wilsonfac5e232016-07-04 11:34:36 +01007648 struct drm_i915_private *dev_priv = to_i915(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007649
7650 I915_WRITE(RENCLK_GATE_D1, SV_CLOCK_GATE_DISABLE);
Ville Syrjälä54e472a2014-02-25 15:13:40 +02007651
7652 /* interrupts should cause a wake up from C3 */
7653 I915_WRITE(MI_STATE, _MASKED_BIT_ENABLE(MI_AGPBUSY_INT_EN) |
7654 _MASKED_BIT_DISABLE(MI_AGPBUSY_830_MODE));
Ville Syrjälä10383922014-08-15 01:21:54 +03007655
7656 I915_WRITE(MEM_MODE,
7657 _MASKED_BIT_ENABLE(MEM_DISPLAY_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007658}
7659
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007660static void i830_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007661{
Chris Wilsonfac5e232016-07-04 11:34:36 +01007662 struct drm_i915_private *dev_priv = to_i915(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007663
7664 I915_WRITE(DSPCLK_GATE_D, OVRUNIT_CLOCK_GATE_DISABLE);
Ville Syrjälä10383922014-08-15 01:21:54 +03007665
7666 I915_WRITE(MEM_MODE,
7667 _MASKED_BIT_ENABLE(MEM_DISPLAY_A_TRICKLE_FEED_DISABLE) |
7668 _MASKED_BIT_ENABLE(MEM_DISPLAY_B_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007669}
7670
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007671void intel_init_clock_gating(struct drm_device *dev)
7672{
Chris Wilsonfac5e232016-07-04 11:34:36 +01007673 struct drm_i915_private *dev_priv = to_i915(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007674
Imre Deakbb400da2016-03-16 13:38:54 +02007675 dev_priv->display.init_clock_gating(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007676}
7677
Imre Deak7d708ee2013-04-17 14:04:50 +03007678void intel_suspend_hw(struct drm_device *dev)
7679{
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01007680 if (HAS_PCH_LPT(to_i915(dev)))
Imre Deak7d708ee2013-04-17 14:04:50 +03007681 lpt_suspend_hw(dev);
7682}
7683
Imre Deakbb400da2016-03-16 13:38:54 +02007684static void nop_init_clock_gating(struct drm_device *dev)
7685{
7686 DRM_DEBUG_KMS("No clock gating settings or workarounds applied.\n");
7687}
7688
7689/**
7690 * intel_init_clock_gating_hooks - setup the clock gating hooks
7691 * @dev_priv: device private
7692 *
7693 * Setup the hooks that configure which clocks of a given platform can be
7694 * gated and also apply various GT and display specific workarounds for these
7695 * platforms. Note that some GT specific workarounds are applied separately
7696 * when GPU contexts or batchbuffers start their execution.
7697 */
7698void intel_init_clock_gating_hooks(struct drm_i915_private *dev_priv)
7699{
7700 if (IS_SKYLAKE(dev_priv))
Daniel Vetterdc00b6a2016-05-19 09:14:20 +02007701 dev_priv->display.init_clock_gating = skylake_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02007702 else if (IS_KABYLAKE(dev_priv))
Mika Kuoppala9498dba2016-06-07 17:19:01 +03007703 dev_priv->display.init_clock_gating = kabylake_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02007704 else if (IS_BROXTON(dev_priv))
7705 dev_priv->display.init_clock_gating = bxt_init_clock_gating;
7706 else if (IS_BROADWELL(dev_priv))
7707 dev_priv->display.init_clock_gating = broadwell_init_clock_gating;
7708 else if (IS_CHERRYVIEW(dev_priv))
7709 dev_priv->display.init_clock_gating = cherryview_init_clock_gating;
7710 else if (IS_HASWELL(dev_priv))
7711 dev_priv->display.init_clock_gating = haswell_init_clock_gating;
7712 else if (IS_IVYBRIDGE(dev_priv))
7713 dev_priv->display.init_clock_gating = ivybridge_init_clock_gating;
7714 else if (IS_VALLEYVIEW(dev_priv))
7715 dev_priv->display.init_clock_gating = valleyview_init_clock_gating;
7716 else if (IS_GEN6(dev_priv))
7717 dev_priv->display.init_clock_gating = gen6_init_clock_gating;
7718 else if (IS_GEN5(dev_priv))
7719 dev_priv->display.init_clock_gating = ironlake_init_clock_gating;
7720 else if (IS_G4X(dev_priv))
7721 dev_priv->display.init_clock_gating = g4x_init_clock_gating;
7722 else if (IS_CRESTLINE(dev_priv))
7723 dev_priv->display.init_clock_gating = crestline_init_clock_gating;
7724 else if (IS_BROADWATER(dev_priv))
7725 dev_priv->display.init_clock_gating = broadwater_init_clock_gating;
7726 else if (IS_GEN3(dev_priv))
7727 dev_priv->display.init_clock_gating = gen3_init_clock_gating;
7728 else if (IS_I85X(dev_priv) || IS_I865G(dev_priv))
7729 dev_priv->display.init_clock_gating = i85x_init_clock_gating;
7730 else if (IS_GEN2(dev_priv))
7731 dev_priv->display.init_clock_gating = i830_init_clock_gating;
7732 else {
7733 MISSING_CASE(INTEL_DEVID(dev_priv));
7734 dev_priv->display.init_clock_gating = nop_init_clock_gating;
7735 }
7736}
7737
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007738/* Set up chip specific power management-related functions */
7739void intel_init_pm(struct drm_device *dev)
7740{
Chris Wilsonfac5e232016-07-04 11:34:36 +01007741 struct drm_i915_private *dev_priv = to_i915(dev);
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007742
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02007743 intel_fbc_init(dev_priv);
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007744
Daniel Vetterc921aba2012-04-26 23:28:17 +02007745 /* For cxsr */
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02007746 if (IS_PINEVIEW(dev_priv))
Daniel Vetterc921aba2012-04-26 23:28:17 +02007747 i915_pineview_get_mem_freq(dev);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01007748 else if (IS_GEN5(dev_priv))
Daniel Vetterc921aba2012-04-26 23:28:17 +02007749 i915_ironlake_get_mem_freq(dev);
7750
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007751 /* For FIFO watermark updates */
Damien Lespiauf5ed50c2014-11-13 17:51:52 +00007752 if (INTEL_INFO(dev)->gen >= 9) {
Pradeep Bhat2af30a52014-11-04 17:06:38 +00007753 skl_setup_wm_latency(dev);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00007754 dev_priv->display.update_wm = skl_update_wm;
Matt Roper98d39492016-05-12 07:06:03 -07007755 dev_priv->display.compute_global_watermarks = skl_compute_wm;
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01007756 } else if (HAS_PCH_SPLIT(dev_priv)) {
Damien Lespiaufa50ad62014-03-17 18:01:16 +00007757 ilk_setup_wm_latency(dev);
Ville Syrjälä53615a52013-08-01 16:18:50 +03007758
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01007759 if ((IS_GEN5(dev_priv) && dev_priv->wm.pri_latency[1] &&
Ville Syrjäläbd602542014-01-07 16:14:10 +02007760 dev_priv->wm.spr_latency[1] && dev_priv->wm.cur_latency[1]) ||
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01007761 (!IS_GEN5(dev_priv) && dev_priv->wm.pri_latency[0] &&
Ville Syrjäläbd602542014-01-07 16:14:10 +02007762 dev_priv->wm.spr_latency[0] && dev_priv->wm.cur_latency[0])) {
Matt Roper86c8bbb2015-09-24 15:53:16 -07007763 dev_priv->display.compute_pipe_wm = ilk_compute_pipe_wm;
Matt Ropered4a6a72016-02-23 17:20:13 -08007764 dev_priv->display.compute_intermediate_wm =
7765 ilk_compute_intermediate_wm;
7766 dev_priv->display.initial_watermarks =
7767 ilk_initial_watermarks;
7768 dev_priv->display.optimize_watermarks =
7769 ilk_optimize_watermarks;
Ville Syrjäläbd602542014-01-07 16:14:10 +02007770 } else {
7771 DRM_DEBUG_KMS("Failed to read display plane latency. "
7772 "Disable CxSR\n");
7773 }
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +01007774 } else if (IS_CHERRYVIEW(dev_priv)) {
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03007775 vlv_setup_wm_latency(dev);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03007776 dev_priv->display.update_wm = vlv_update_wm;
Tvrtko Ursulin11a914c2016-10-13 11:03:08 +01007777 } else if (IS_VALLEYVIEW(dev_priv)) {
Ville Syrjälä26e1fe42015-06-24 22:00:06 +03007778 vlv_setup_wm_latency(dev);
Ville Syrjälä26e1fe42015-06-24 22:00:06 +03007779 dev_priv->display.update_wm = vlv_update_wm;
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02007780 } else if (IS_PINEVIEW(dev_priv)) {
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01007781 if (!intel_get_cxsr_latency(IS_PINEVIEW_G(dev_priv),
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007782 dev_priv->is_ddr3,
7783 dev_priv->fsb_freq,
7784 dev_priv->mem_freq)) {
7785 DRM_INFO("failed to find known CxSR latency "
7786 "(found ddr%s fsb freq %d, mem freq %d), "
7787 "disabling CxSR\n",
7788 (dev_priv->is_ddr3 == 1) ? "3" : "2",
7789 dev_priv->fsb_freq, dev_priv->mem_freq);
7790 /* Disable CxSR and never update its watermark again */
Imre Deak5209b1f2014-07-01 12:36:17 +03007791 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007792 dev_priv->display.update_wm = NULL;
7793 } else
7794 dev_priv->display.update_wm = pineview_update_wm;
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +01007795 } else if (IS_G4X(dev_priv)) {
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007796 dev_priv->display.update_wm = g4x_update_wm;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01007797 } else if (IS_GEN4(dev_priv)) {
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007798 dev_priv->display.update_wm = i965_update_wm;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01007799 } else if (IS_GEN3(dev_priv)) {
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007800 dev_priv->display.update_wm = i9xx_update_wm;
7801 dev_priv->display.get_fifo_size = i9xx_get_fifo_size;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01007802 } else if (IS_GEN2(dev_priv)) {
Daniel Vetterfeb56b92013-12-14 20:38:30 -02007803 if (INTEL_INFO(dev)->num_pipes == 1) {
7804 dev_priv->display.update_wm = i845_update_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007805 dev_priv->display.get_fifo_size = i845_get_fifo_size;
Daniel Vetterfeb56b92013-12-14 20:38:30 -02007806 } else {
7807 dev_priv->display.update_wm = i9xx_update_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007808 dev_priv->display.get_fifo_size = i830_get_fifo_size;
Daniel Vetterfeb56b92013-12-14 20:38:30 -02007809 }
Daniel Vetterfeb56b92013-12-14 20:38:30 -02007810 } else {
7811 DRM_ERROR("unexpected fall-through in intel_init_pm\n");
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007812 }
7813}
7814
Lyude87660502016-08-17 15:55:53 -04007815static inline int gen6_check_mailbox_status(struct drm_i915_private *dev_priv)
7816{
7817 uint32_t flags =
7818 I915_READ_FW(GEN6_PCODE_MAILBOX) & GEN6_PCODE_ERROR_MASK;
7819
7820 switch (flags) {
7821 case GEN6_PCODE_SUCCESS:
7822 return 0;
7823 case GEN6_PCODE_UNIMPLEMENTED_CMD:
7824 case GEN6_PCODE_ILLEGAL_CMD:
7825 return -ENXIO;
7826 case GEN6_PCODE_MIN_FREQ_TABLE_GT_RATIO_OUT_OF_RANGE:
Chris Wilson7850d1c2016-08-26 11:59:26 +01007827 case GEN7_PCODE_MIN_FREQ_TABLE_GT_RATIO_OUT_OF_RANGE:
Lyude87660502016-08-17 15:55:53 -04007828 return -EOVERFLOW;
7829 case GEN6_PCODE_TIMEOUT:
7830 return -ETIMEDOUT;
7831 default:
7832 MISSING_CASE(flags)
7833 return 0;
7834 }
7835}
7836
7837static inline int gen7_check_mailbox_status(struct drm_i915_private *dev_priv)
7838{
7839 uint32_t flags =
7840 I915_READ_FW(GEN6_PCODE_MAILBOX) & GEN6_PCODE_ERROR_MASK;
7841
7842 switch (flags) {
7843 case GEN6_PCODE_SUCCESS:
7844 return 0;
7845 case GEN6_PCODE_ILLEGAL_CMD:
7846 return -ENXIO;
7847 case GEN7_PCODE_TIMEOUT:
7848 return -ETIMEDOUT;
7849 case GEN7_PCODE_ILLEGAL_DATA:
7850 return -EINVAL;
7851 case GEN7_PCODE_MIN_FREQ_TABLE_GT_RATIO_OUT_OF_RANGE:
7852 return -EOVERFLOW;
7853 default:
7854 MISSING_CASE(flags);
7855 return 0;
7856 }
7857}
7858
Tom O'Rourke151a49d2014-11-13 18:50:10 -08007859int sandybridge_pcode_read(struct drm_i915_private *dev_priv, u32 mbox, u32 *val)
Ben Widawsky42c05262012-09-26 10:34:00 -07007860{
Lyude87660502016-08-17 15:55:53 -04007861 int status;
7862
Jesse Barnes4fc688c2012-11-02 11:14:01 -07007863 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Ben Widawsky42c05262012-09-26 10:34:00 -07007864
Chris Wilson3f5582d2016-06-30 15:32:45 +01007865 /* GEN6_PCODE_* are outside of the forcewake domain, we can
7866 * use te fw I915_READ variants to reduce the amount of work
7867 * required when reading/writing.
7868 */
7869
7870 if (I915_READ_FW(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) {
Ben Widawsky42c05262012-09-26 10:34:00 -07007871 DRM_DEBUG_DRIVER("warning: pcode (read) mailbox access failed\n");
7872 return -EAGAIN;
7873 }
7874
Chris Wilson3f5582d2016-06-30 15:32:45 +01007875 I915_WRITE_FW(GEN6_PCODE_DATA, *val);
7876 I915_WRITE_FW(GEN6_PCODE_DATA1, 0);
7877 I915_WRITE_FW(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY | mbox);
Ben Widawsky42c05262012-09-26 10:34:00 -07007878
Chris Wilson3f5582d2016-06-30 15:32:45 +01007879 if (intel_wait_for_register_fw(dev_priv,
7880 GEN6_PCODE_MAILBOX, GEN6_PCODE_READY, 0,
7881 500)) {
Ben Widawsky42c05262012-09-26 10:34:00 -07007882 DRM_ERROR("timeout waiting for pcode read (%d) to finish\n", mbox);
7883 return -ETIMEDOUT;
7884 }
7885
Chris Wilson3f5582d2016-06-30 15:32:45 +01007886 *val = I915_READ_FW(GEN6_PCODE_DATA);
7887 I915_WRITE_FW(GEN6_PCODE_DATA, 0);
Ben Widawsky42c05262012-09-26 10:34:00 -07007888
Lyude87660502016-08-17 15:55:53 -04007889 if (INTEL_GEN(dev_priv) > 6)
7890 status = gen7_check_mailbox_status(dev_priv);
7891 else
7892 status = gen6_check_mailbox_status(dev_priv);
7893
7894 if (status) {
7895 DRM_DEBUG_DRIVER("warning: pcode (read) mailbox access failed: %d\n",
7896 status);
7897 return status;
7898 }
7899
Ben Widawsky42c05262012-09-26 10:34:00 -07007900 return 0;
7901}
7902
Chris Wilson3f5582d2016-06-30 15:32:45 +01007903int sandybridge_pcode_write(struct drm_i915_private *dev_priv,
Lyude87660502016-08-17 15:55:53 -04007904 u32 mbox, u32 val)
Ben Widawsky42c05262012-09-26 10:34:00 -07007905{
Lyude87660502016-08-17 15:55:53 -04007906 int status;
7907
Jesse Barnes4fc688c2012-11-02 11:14:01 -07007908 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Ben Widawsky42c05262012-09-26 10:34:00 -07007909
Chris Wilson3f5582d2016-06-30 15:32:45 +01007910 /* GEN6_PCODE_* are outside of the forcewake domain, we can
7911 * use te fw I915_READ variants to reduce the amount of work
7912 * required when reading/writing.
7913 */
7914
7915 if (I915_READ_FW(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) {
Ben Widawsky42c05262012-09-26 10:34:00 -07007916 DRM_DEBUG_DRIVER("warning: pcode (write) mailbox access failed\n");
7917 return -EAGAIN;
7918 }
7919
Chris Wilson3f5582d2016-06-30 15:32:45 +01007920 I915_WRITE_FW(GEN6_PCODE_DATA, val);
7921 I915_WRITE_FW(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY | mbox);
Ben Widawsky42c05262012-09-26 10:34:00 -07007922
Chris Wilson3f5582d2016-06-30 15:32:45 +01007923 if (intel_wait_for_register_fw(dev_priv,
7924 GEN6_PCODE_MAILBOX, GEN6_PCODE_READY, 0,
7925 500)) {
Ben Widawsky42c05262012-09-26 10:34:00 -07007926 DRM_ERROR("timeout waiting for pcode write (%d) to finish\n", mbox);
7927 return -ETIMEDOUT;
7928 }
7929
Chris Wilson3f5582d2016-06-30 15:32:45 +01007930 I915_WRITE_FW(GEN6_PCODE_DATA, 0);
Ben Widawsky42c05262012-09-26 10:34:00 -07007931
Lyude87660502016-08-17 15:55:53 -04007932 if (INTEL_GEN(dev_priv) > 6)
7933 status = gen7_check_mailbox_status(dev_priv);
7934 else
7935 status = gen6_check_mailbox_status(dev_priv);
7936
7937 if (status) {
7938 DRM_DEBUG_DRIVER("warning: pcode (write) mailbox access failed: %d\n",
7939 status);
7940 return status;
7941 }
7942
Ben Widawsky42c05262012-09-26 10:34:00 -07007943 return 0;
7944}
Jesse Barnesa0e4e192013-04-02 11:23:05 -07007945
Ville Syrjälädd06f882014-11-10 22:55:12 +02007946static int byt_gpu_freq(struct drm_i915_private *dev_priv, int val)
7947{
Ville Syrjäläc30fec62016-03-04 21:43:02 +02007948 /*
7949 * N = val - 0xb7
7950 * Slow = Fast = GPLL ref * N
7951 */
7952 return DIV_ROUND_CLOSEST(dev_priv->rps.gpll_ref_freq * (val - 0xb7), 1000);
Jesse Barnes855ba3b2013-04-17 15:54:57 -07007953}
7954
Fengguang Wub55dd642014-07-12 11:21:39 +02007955static int byt_freq_opcode(struct drm_i915_private *dev_priv, int val)
Jesse Barnes855ba3b2013-04-17 15:54:57 -07007956{
Ville Syrjäläc30fec62016-03-04 21:43:02 +02007957 return DIV_ROUND_CLOSEST(1000 * val, dev_priv->rps.gpll_ref_freq) + 0xb7;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07007958}
7959
Fengguang Wub55dd642014-07-12 11:21:39 +02007960static int chv_gpu_freq(struct drm_i915_private *dev_priv, int val)
Deepak S22b1b2f2014-07-12 14:54:33 +05307961{
Ville Syrjäläc30fec62016-03-04 21:43:02 +02007962 /*
7963 * N = val / 2
7964 * CU (slow) = CU2x (fast) / 2 = GPLL ref * N / 2
7965 */
7966 return DIV_ROUND_CLOSEST(dev_priv->rps.gpll_ref_freq * val, 2 * 2 * 1000);
Deepak S22b1b2f2014-07-12 14:54:33 +05307967}
7968
Fengguang Wub55dd642014-07-12 11:21:39 +02007969static int chv_freq_opcode(struct drm_i915_private *dev_priv, int val)
Deepak S22b1b2f2014-07-12 14:54:33 +05307970{
Ville Syrjälä1c147622014-08-18 14:42:43 +03007971 /* CHV needs even values */
Ville Syrjäläc30fec62016-03-04 21:43:02 +02007972 return DIV_ROUND_CLOSEST(2 * 1000 * val, dev_priv->rps.gpll_ref_freq) * 2;
Deepak S22b1b2f2014-07-12 14:54:33 +05307973}
7974
Ville Syrjälä616bc822015-01-23 21:04:25 +02007975int intel_gpu_freq(struct drm_i915_private *dev_priv, int val)
7976{
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03007977 if (IS_GEN9(dev_priv))
Mika Kuoppala500a3d22015-11-13 19:29:41 +02007978 return DIV_ROUND_CLOSEST(val * GT_FREQUENCY_MULTIPLIER,
7979 GEN9_FREQ_SCALER);
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03007980 else if (IS_CHERRYVIEW(dev_priv))
Ville Syrjälä616bc822015-01-23 21:04:25 +02007981 return chv_gpu_freq(dev_priv, val);
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03007982 else if (IS_VALLEYVIEW(dev_priv))
Ville Syrjälä616bc822015-01-23 21:04:25 +02007983 return byt_gpu_freq(dev_priv, val);
7984 else
7985 return val * GT_FREQUENCY_MULTIPLIER;
7986}
7987
Ville Syrjälä616bc822015-01-23 21:04:25 +02007988int intel_freq_opcode(struct drm_i915_private *dev_priv, int val)
7989{
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03007990 if (IS_GEN9(dev_priv))
Mika Kuoppala500a3d22015-11-13 19:29:41 +02007991 return DIV_ROUND_CLOSEST(val * GEN9_FREQ_SCALER,
7992 GT_FREQUENCY_MULTIPLIER);
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03007993 else if (IS_CHERRYVIEW(dev_priv))
Ville Syrjälä616bc822015-01-23 21:04:25 +02007994 return chv_freq_opcode(dev_priv, val);
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03007995 else if (IS_VALLEYVIEW(dev_priv))
Ville Syrjälä616bc822015-01-23 21:04:25 +02007996 return byt_freq_opcode(dev_priv, val);
7997 else
Mika Kuoppala500a3d22015-11-13 19:29:41 +02007998 return DIV_ROUND_CLOSEST(val, GT_FREQUENCY_MULTIPLIER);
Deepak S22b1b2f2014-07-12 14:54:33 +05307999}
8000
Chris Wilson6ad790c2015-04-07 16:20:31 +01008001struct request_boost {
8002 struct work_struct work;
Daniel Vettereed29a52015-05-21 14:21:25 +02008003 struct drm_i915_gem_request *req;
Chris Wilson6ad790c2015-04-07 16:20:31 +01008004};
8005
8006static void __intel_rps_boost_work(struct work_struct *work)
8007{
8008 struct request_boost *boost = container_of(work, struct request_boost, work);
Chris Wilsone61b9952015-04-27 13:41:24 +01008009 struct drm_i915_gem_request *req = boost->req;
Chris Wilson6ad790c2015-04-07 16:20:31 +01008010
Chris Wilsonf69a02c2016-07-01 17:23:16 +01008011 if (!i915_gem_request_completed(req))
Chris Wilsonc0336662016-05-06 15:40:21 +01008012 gen6_rps_boost(req->i915, NULL, req->emitted_jiffies);
Chris Wilson6ad790c2015-04-07 16:20:31 +01008013
Chris Wilsone8a261e2016-07-20 13:31:49 +01008014 i915_gem_request_put(req);
Chris Wilson6ad790c2015-04-07 16:20:31 +01008015 kfree(boost);
8016}
8017
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01008018void intel_queue_rps_boost_for_request(struct drm_i915_gem_request *req)
Chris Wilson6ad790c2015-04-07 16:20:31 +01008019{
8020 struct request_boost *boost;
8021
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01008022 if (req == NULL || INTEL_GEN(req->i915) < 6)
Chris Wilson6ad790c2015-04-07 16:20:31 +01008023 return;
8024
Chris Wilsonf69a02c2016-07-01 17:23:16 +01008025 if (i915_gem_request_completed(req))
Chris Wilsone61b9952015-04-27 13:41:24 +01008026 return;
8027
Chris Wilson6ad790c2015-04-07 16:20:31 +01008028 boost = kmalloc(sizeof(*boost), GFP_ATOMIC);
8029 if (boost == NULL)
8030 return;
8031
Chris Wilsone8a261e2016-07-20 13:31:49 +01008032 boost->req = i915_gem_request_get(req);
Chris Wilson6ad790c2015-04-07 16:20:31 +01008033
8034 INIT_WORK(&boost->work, __intel_rps_boost_work);
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01008035 queue_work(req->i915->wq, &boost->work);
Chris Wilson6ad790c2015-04-07 16:20:31 +01008036}
8037
Daniel Vetterf742a552013-12-06 10:17:53 +01008038void intel_pm_setup(struct drm_device *dev)
Chris Wilson907b28c2013-07-19 20:36:52 +01008039{
Chris Wilsonfac5e232016-07-04 11:34:36 +01008040 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilson907b28c2013-07-19 20:36:52 +01008041
Daniel Vetterf742a552013-12-06 10:17:53 +01008042 mutex_init(&dev_priv->rps.hw_lock);
Chris Wilson8d3afd72015-05-21 21:01:47 +01008043 spin_lock_init(&dev_priv->rps.client_lock);
Daniel Vetterf742a552013-12-06 10:17:53 +01008044
Chris Wilson54b4f682016-07-21 21:16:19 +01008045 INIT_DELAYED_WORK(&dev_priv->rps.autoenable_work,
8046 __intel_autoenable_gt_powersave);
Chris Wilson1854d5c2015-04-07 16:20:32 +01008047 INIT_LIST_HEAD(&dev_priv->rps.clients);
Paulo Zanoni5d584b22014-03-07 20:08:15 -03008048
Paulo Zanoni33688d92014-03-07 20:08:19 -03008049 dev_priv->pm.suspended = false;
Imre Deak1f814da2015-12-16 02:52:19 +02008050 atomic_set(&dev_priv->pm.wakeref_count, 0);
Chris Wilson907b28c2013-07-19 20:36:52 +01008051}