blob: 4f51f8656cf65f6b5c8a448943d6eea98d75687e [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 Deakd965e7a2015-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{
Chris Wilson91c8a322016-07-05 10:40:23 +0100323 struct drm_device *dev = &dev_priv->drm;
Imre Deak5209b1f2014-07-01 12:36:17 +0300324 u32 val;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300325
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +0100326 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Imre Deak5209b1f2014-07-01 12:36:17 +0300327 I915_WRITE(FW_BLC_SELF_VLV, enable ? FW_CSPWRDWNEN : 0);
Ville Syrjäläa7a6c492015-06-24 22:00:01 +0300328 POSTING_READ(FW_BLC_SELF_VLV);
Ville Syrjälä852eb002015-06-24 22:00:07 +0300329 dev_priv->wm.vlv.cxsr = enable;
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +0100330 } else if (IS_G4X(dev_priv) || IS_CRESTLINE(dev_priv)) {
Imre Deak5209b1f2014-07-01 12:36:17 +0300331 I915_WRITE(FW_BLC_SELF, enable ? FW_BLC_SELF_EN : 0);
Ville Syrjäläa7a6c492015-06-24 22:00:01 +0300332 POSTING_READ(FW_BLC_SELF);
Imre Deak5209b1f2014-07-01 12:36:17 +0300333 } else if (IS_PINEVIEW(dev)) {
334 val = I915_READ(DSPFW3) & ~PINEVIEW_SELF_REFRESH_EN;
335 val |= enable ? PINEVIEW_SELF_REFRESH_EN : 0;
336 I915_WRITE(DSPFW3, val);
Ville Syrjäläa7a6c492015-06-24 22:00:01 +0300337 POSTING_READ(DSPFW3);
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +0100338 } else if (IS_I945G(dev_priv) || IS_I945GM(dev_priv)) {
Imre Deak5209b1f2014-07-01 12:36:17 +0300339 val = enable ? _MASKED_BIT_ENABLE(FW_BLC_SELF_EN) :
340 _MASKED_BIT_DISABLE(FW_BLC_SELF_EN);
341 I915_WRITE(FW_BLC_SELF, val);
Ville Syrjäläa7a6c492015-06-24 22:00:01 +0300342 POSTING_READ(FW_BLC_SELF);
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +0100343 } else if (IS_I915GM(dev_priv)) {
Ville Syrjäläacb91352016-07-29 17:57:02 +0300344 /*
345 * FIXME can't find a bit like this for 915G, and
346 * and yet it does have the related watermark in
347 * FW_BLC_SELF. What's going on?
348 */
Imre Deak5209b1f2014-07-01 12:36:17 +0300349 val = enable ? _MASKED_BIT_ENABLE(INSTPM_SELF_EN) :
350 _MASKED_BIT_DISABLE(INSTPM_SELF_EN);
351 I915_WRITE(INSTPM, val);
Ville Syrjäläa7a6c492015-06-24 22:00:01 +0300352 POSTING_READ(INSTPM);
Imre Deak5209b1f2014-07-01 12:36:17 +0300353 } else {
354 return;
355 }
356
357 DRM_DEBUG_KMS("memory self-refresh is %s\n",
358 enable ? "enabled" : "disabled");
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300359}
360
Ville Syrjäläfc1ac8d2015-03-05 21:19:52 +0200361
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300362/*
363 * Latency for FIFO fetches is dependent on several factors:
364 * - memory configuration (speed, channels)
365 * - chipset
366 * - current MCH state
367 * It can be fairly high in some situations, so here we assume a fairly
368 * pessimal value. It's a tradeoff between extra memory fetches (if we
369 * set this value too high, the FIFO will fetch frequently to stay full)
370 * and power consumption (set it too low to save power and we might see
371 * FIFO underruns and display "flicker").
372 *
373 * A value of 5us seems to be a good balance; safe for very low end
374 * platforms but not overly aggressive on lower latency configs.
375 */
Chris Wilson5aef6002014-09-03 11:56:07 +0100376static const int pessimal_latency_ns = 5000;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300377
Ville Syrjäläb5004722015-03-05 21:19:47 +0200378#define VLV_FIFO_START(dsparb, dsparb2, lo_shift, hi_shift) \
379 ((((dsparb) >> (lo_shift)) & 0xff) | ((((dsparb2) >> (hi_shift)) & 0x1) << 8))
380
381static int vlv_get_fifo_size(struct drm_device *dev,
382 enum pipe pipe, int plane)
383{
Chris Wilsonfac5e232016-07-04 11:34:36 +0100384 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläb5004722015-03-05 21:19:47 +0200385 int sprite0_start, sprite1_start, size;
386
387 switch (pipe) {
388 uint32_t dsparb, dsparb2, dsparb3;
389 case PIPE_A:
390 dsparb = I915_READ(DSPARB);
391 dsparb2 = I915_READ(DSPARB2);
392 sprite0_start = VLV_FIFO_START(dsparb, dsparb2, 0, 0);
393 sprite1_start = VLV_FIFO_START(dsparb, dsparb2, 8, 4);
394 break;
395 case PIPE_B:
396 dsparb = I915_READ(DSPARB);
397 dsparb2 = I915_READ(DSPARB2);
398 sprite0_start = VLV_FIFO_START(dsparb, dsparb2, 16, 8);
399 sprite1_start = VLV_FIFO_START(dsparb, dsparb2, 24, 12);
400 break;
401 case PIPE_C:
402 dsparb2 = I915_READ(DSPARB2);
403 dsparb3 = I915_READ(DSPARB3);
404 sprite0_start = VLV_FIFO_START(dsparb3, dsparb2, 0, 16);
405 sprite1_start = VLV_FIFO_START(dsparb3, dsparb2, 8, 20);
406 break;
407 default:
408 return 0;
409 }
410
411 switch (plane) {
412 case 0:
413 size = sprite0_start;
414 break;
415 case 1:
416 size = sprite1_start - sprite0_start;
417 break;
418 case 2:
419 size = 512 - 1 - sprite1_start;
420 break;
421 default:
422 return 0;
423 }
424
425 DRM_DEBUG_KMS("Pipe %c %s %c FIFO size: %d\n",
426 pipe_name(pipe), plane == 0 ? "primary" : "sprite",
427 plane == 0 ? plane_name(pipe) : sprite_name(pipe, plane - 1),
428 size);
429
430 return size;
431}
432
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300433static int i9xx_get_fifo_size(struct drm_device *dev, int plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300434{
Chris Wilsonfac5e232016-07-04 11:34:36 +0100435 struct drm_i915_private *dev_priv = to_i915(dev);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300436 uint32_t dsparb = I915_READ(DSPARB);
437 int size;
438
439 size = dsparb & 0x7f;
440 if (plane)
441 size = ((dsparb >> DSPARB_CSTART_SHIFT) & 0x7f) - size;
442
443 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
444 plane ? "B" : "A", size);
445
446 return size;
447}
448
Daniel Vetterfeb56b92013-12-14 20:38:30 -0200449static int i830_get_fifo_size(struct drm_device *dev, int plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300450{
Chris Wilsonfac5e232016-07-04 11:34:36 +0100451 struct drm_i915_private *dev_priv = to_i915(dev);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300452 uint32_t dsparb = I915_READ(DSPARB);
453 int size;
454
455 size = dsparb & 0x1ff;
456 if (plane)
457 size = ((dsparb >> DSPARB_BEND_SHIFT) & 0x1ff) - size;
458 size >>= 1; /* Convert to cachelines */
459
460 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
461 plane ? "B" : "A", size);
462
463 return size;
464}
465
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300466static int i845_get_fifo_size(struct drm_device *dev, int plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300467{
Chris Wilsonfac5e232016-07-04 11:34:36 +0100468 struct drm_i915_private *dev_priv = to_i915(dev);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300469 uint32_t dsparb = I915_READ(DSPARB);
470 int size;
471
472 size = dsparb & 0x7f;
473 size >>= 2; /* Convert to cachelines */
474
475 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
476 plane ? "B" : "A",
477 size);
478
479 return size;
480}
481
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300482/* Pineview has different values for various configs */
483static const struct intel_watermark_params pineview_display_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300484 .fifo_size = PINEVIEW_DISPLAY_FIFO,
485 .max_wm = PINEVIEW_MAX_WM,
486 .default_wm = PINEVIEW_DFT_WM,
487 .guard_size = PINEVIEW_GUARD_WM,
488 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300489};
490static const struct intel_watermark_params pineview_display_hplloff_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300491 .fifo_size = PINEVIEW_DISPLAY_FIFO,
492 .max_wm = PINEVIEW_MAX_WM,
493 .default_wm = PINEVIEW_DFT_HPLLOFF_WM,
494 .guard_size = PINEVIEW_GUARD_WM,
495 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300496};
497static const struct intel_watermark_params pineview_cursor_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300498 .fifo_size = PINEVIEW_CURSOR_FIFO,
499 .max_wm = PINEVIEW_CURSOR_MAX_WM,
500 .default_wm = PINEVIEW_CURSOR_DFT_WM,
501 .guard_size = PINEVIEW_CURSOR_GUARD_WM,
502 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300503};
504static const struct intel_watermark_params pineview_cursor_hplloff_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300505 .fifo_size = PINEVIEW_CURSOR_FIFO,
506 .max_wm = PINEVIEW_CURSOR_MAX_WM,
507 .default_wm = PINEVIEW_CURSOR_DFT_WM,
508 .guard_size = PINEVIEW_CURSOR_GUARD_WM,
509 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300510};
511static const struct intel_watermark_params g4x_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300512 .fifo_size = G4X_FIFO_SIZE,
513 .max_wm = G4X_MAX_WM,
514 .default_wm = G4X_MAX_WM,
515 .guard_size = 2,
516 .cacheline_size = G4X_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300517};
518static const struct intel_watermark_params g4x_cursor_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300519 .fifo_size = I965_CURSOR_FIFO,
520 .max_wm = I965_CURSOR_MAX_WM,
521 .default_wm = I965_CURSOR_DFT_WM,
522 .guard_size = 2,
523 .cacheline_size = G4X_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300524};
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300525static const struct intel_watermark_params i965_cursor_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300526 .fifo_size = I965_CURSOR_FIFO,
527 .max_wm = I965_CURSOR_MAX_WM,
528 .default_wm = I965_CURSOR_DFT_WM,
529 .guard_size = 2,
530 .cacheline_size = I915_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300531};
532static const struct intel_watermark_params i945_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300533 .fifo_size = I945_FIFO_SIZE,
534 .max_wm = I915_MAX_WM,
535 .default_wm = 1,
536 .guard_size = 2,
537 .cacheline_size = I915_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300538};
539static const struct intel_watermark_params i915_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300540 .fifo_size = I915_FIFO_SIZE,
541 .max_wm = I915_MAX_WM,
542 .default_wm = 1,
543 .guard_size = 2,
544 .cacheline_size = I915_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300545};
Ville Syrjälä9d539102014-08-15 01:21:53 +0300546static const struct intel_watermark_params i830_a_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300547 .fifo_size = I855GM_FIFO_SIZE,
548 .max_wm = I915_MAX_WM,
549 .default_wm = 1,
550 .guard_size = 2,
551 .cacheline_size = I830_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300552};
Ville Syrjälä9d539102014-08-15 01:21:53 +0300553static const struct intel_watermark_params i830_bc_wm_info = {
554 .fifo_size = I855GM_FIFO_SIZE,
555 .max_wm = I915_MAX_WM/2,
556 .default_wm = 1,
557 .guard_size = 2,
558 .cacheline_size = I830_FIFO_LINE_SIZE,
559};
Daniel Vetterfeb56b92013-12-14 20:38:30 -0200560static const struct intel_watermark_params i845_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300561 .fifo_size = I830_FIFO_SIZE,
562 .max_wm = I915_MAX_WM,
563 .default_wm = 1,
564 .guard_size = 2,
565 .cacheline_size = I830_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300566};
567
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300568/**
569 * intel_calculate_wm - calculate watermark level
570 * @clock_in_khz: pixel clock
571 * @wm: chip FIFO params
Ville Syrjäläac484962016-01-20 21:05:26 +0200572 * @cpp: bytes per pixel
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300573 * @latency_ns: memory latency for the platform
574 *
575 * Calculate the watermark level (the level at which the display plane will
576 * start fetching from memory again). Each chip has a different display
577 * FIFO size and allocation, so the caller needs to figure that out and pass
578 * in the correct intel_watermark_params structure.
579 *
580 * As the pixel clock runs, the FIFO will be drained at a rate that depends
581 * on the pixel size. When it reaches the watermark level, it'll start
582 * fetching FIFO line sized based chunks from memory until the FIFO fills
583 * past the watermark point. If the FIFO drains completely, a FIFO underrun
584 * will occur, and a display engine hang could result.
585 */
586static unsigned long intel_calculate_wm(unsigned long clock_in_khz,
587 const struct intel_watermark_params *wm,
Ville Syrjäläac484962016-01-20 21:05:26 +0200588 int fifo_size, int cpp,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300589 unsigned long latency_ns)
590{
591 long entries_required, wm_size;
592
593 /*
594 * Note: we need to make sure we don't overflow for various clock &
595 * latency values.
596 * clocks go from a few thousand to several hundred thousand.
597 * latency is usually a few thousand
598 */
Ville Syrjäläac484962016-01-20 21:05:26 +0200599 entries_required = ((clock_in_khz / 1000) * cpp * latency_ns) /
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300600 1000;
601 entries_required = DIV_ROUND_UP(entries_required, wm->cacheline_size);
602
603 DRM_DEBUG_KMS("FIFO entries required for mode: %ld\n", entries_required);
604
605 wm_size = fifo_size - (entries_required + wm->guard_size);
606
607 DRM_DEBUG_KMS("FIFO watermark level: %ld\n", wm_size);
608
609 /* Don't promote wm_size to unsigned... */
610 if (wm_size > (long)wm->max_wm)
611 wm_size = wm->max_wm;
612 if (wm_size <= 0)
613 wm_size = wm->default_wm;
Ville Syrjäläd6feb192014-09-05 21:54:13 +0300614
615 /*
616 * Bspec seems to indicate that the value shouldn't be lower than
617 * 'burst size + 1'. Certainly 830 is quite unhappy with low values.
618 * Lets go for 8 which is the burst size since certain platforms
619 * already use a hardcoded 8 (which is what the spec says should be
620 * done).
621 */
622 if (wm_size <= 8)
623 wm_size = 8;
624
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300625 return wm_size;
626}
627
Ville Syrjäläefc26112016-10-31 22:37:04 +0200628static struct intel_crtc *single_enabled_crtc(struct drm_device *dev)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300629{
Ville Syrjäläefc26112016-10-31 22:37:04 +0200630 struct intel_crtc *crtc, *enabled = NULL;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300631
Ville Syrjäläefc26112016-10-31 22:37:04 +0200632 for_each_intel_crtc(dev, crtc) {
633 if (intel_crtc_active(crtc)) {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300634 if (enabled)
635 return NULL;
636 enabled = crtc;
637 }
638 }
639
640 return enabled;
641}
642
Ville Syrjälä432081b2016-10-31 22:37:03 +0200643static void pineview_update_wm(struct intel_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300644{
Ville Syrjälä432081b2016-10-31 22:37:03 +0200645 struct drm_device *dev = unused_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +0100646 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläefc26112016-10-31 22:37:04 +0200647 struct intel_crtc *crtc;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300648 const struct cxsr_latency *latency;
649 u32 reg;
650 unsigned long wm;
651
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +0100652 latency = intel_get_cxsr_latency(IS_PINEVIEW_G(dev_priv),
653 dev_priv->is_ddr3,
654 dev_priv->fsb_freq,
655 dev_priv->mem_freq);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300656 if (!latency) {
657 DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
Imre Deak5209b1f2014-07-01 12:36:17 +0300658 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300659 return;
660 }
661
662 crtc = single_enabled_crtc(dev);
663 if (crtc) {
Ville Syrjäläefc26112016-10-31 22:37:04 +0200664 const struct drm_display_mode *adjusted_mode =
665 &crtc->config->base.adjusted_mode;
666 const struct drm_framebuffer *fb =
667 crtc->base.primary->state->fb;
668 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +0300669 int clock = adjusted_mode->crtc_clock;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300670
671 /* Display SR */
672 wm = intel_calculate_wm(clock, &pineview_display_wm,
673 pineview_display_wm.fifo_size,
Ville Syrjäläac484962016-01-20 21:05:26 +0200674 cpp, latency->display_sr);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300675 reg = I915_READ(DSPFW1);
676 reg &= ~DSPFW_SR_MASK;
Ville Syrjäläf4998962015-03-10 17:02:21 +0200677 reg |= FW_WM(wm, SR);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300678 I915_WRITE(DSPFW1, reg);
679 DRM_DEBUG_KMS("DSPFW1 register is %x\n", reg);
680
681 /* cursor SR */
682 wm = intel_calculate_wm(clock, &pineview_cursor_wm,
683 pineview_display_wm.fifo_size,
Ville Syrjäläac484962016-01-20 21:05:26 +0200684 cpp, latency->cursor_sr);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300685 reg = I915_READ(DSPFW3);
686 reg &= ~DSPFW_CURSOR_SR_MASK;
Ville Syrjäläf4998962015-03-10 17:02:21 +0200687 reg |= FW_WM(wm, CURSOR_SR);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300688 I915_WRITE(DSPFW3, reg);
689
690 /* Display HPLL off SR */
691 wm = intel_calculate_wm(clock, &pineview_display_hplloff_wm,
692 pineview_display_hplloff_wm.fifo_size,
Ville Syrjäläac484962016-01-20 21:05:26 +0200693 cpp, latency->display_hpll_disable);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300694 reg = I915_READ(DSPFW3);
695 reg &= ~DSPFW_HPLL_SR_MASK;
Ville Syrjäläf4998962015-03-10 17:02:21 +0200696 reg |= FW_WM(wm, HPLL_SR);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300697 I915_WRITE(DSPFW3, reg);
698
699 /* cursor HPLL off SR */
700 wm = intel_calculate_wm(clock, &pineview_cursor_hplloff_wm,
701 pineview_display_hplloff_wm.fifo_size,
Ville Syrjäläac484962016-01-20 21:05:26 +0200702 cpp, latency->cursor_hpll_disable);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300703 reg = I915_READ(DSPFW3);
704 reg &= ~DSPFW_HPLL_CURSOR_MASK;
Ville Syrjäläf4998962015-03-10 17:02:21 +0200705 reg |= FW_WM(wm, HPLL_CURSOR);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300706 I915_WRITE(DSPFW3, reg);
707 DRM_DEBUG_KMS("DSPFW3 register is %x\n", reg);
708
Imre Deak5209b1f2014-07-01 12:36:17 +0300709 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300710 } else {
Imre Deak5209b1f2014-07-01 12:36:17 +0300711 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300712 }
713}
714
Ville Syrjäläf0ce2312016-10-31 22:37:08 +0200715static bool g4x_compute_wm0(struct drm_i915_private *dev_priv,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300716 int plane,
717 const struct intel_watermark_params *display,
718 int display_latency_ns,
719 const struct intel_watermark_params *cursor,
720 int cursor_latency_ns,
721 int *plane_wm,
722 int *cursor_wm)
723{
Ville Syrjäläefc26112016-10-31 22:37:04 +0200724 struct intel_crtc *crtc;
Ville Syrjälä4fe85902013-09-04 18:25:22 +0300725 const struct drm_display_mode *adjusted_mode;
Ville Syrjäläefc26112016-10-31 22:37:04 +0200726 const struct drm_framebuffer *fb;
Ville Syrjäläac484962016-01-20 21:05:26 +0200727 int htotal, hdisplay, clock, cpp;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300728 int line_time_us, line_count;
729 int entries, tlb_miss;
730
Ville Syrjäläb91eb5c2016-10-31 22:37:09 +0200731 crtc = intel_get_crtc_for_plane(dev_priv, plane);
Ville Syrjäläefc26112016-10-31 22:37:04 +0200732 if (!intel_crtc_active(crtc)) {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300733 *cursor_wm = cursor->guard_size;
734 *plane_wm = display->guard_size;
735 return false;
736 }
737
Ville Syrjäläefc26112016-10-31 22:37:04 +0200738 adjusted_mode = &crtc->config->base.adjusted_mode;
739 fb = crtc->base.primary->state->fb;
Damien Lespiau241bfc32013-09-25 16:45:37 +0100740 clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -0800741 htotal = adjusted_mode->crtc_htotal;
Ville Syrjäläefc26112016-10-31 22:37:04 +0200742 hdisplay = crtc->config->pipe_src_w;
743 cpp = drm_format_plane_cpp(fb->pixel_format, 0);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300744
745 /* Use the small buffer method to calculate plane watermark */
Ville Syrjäläac484962016-01-20 21:05:26 +0200746 entries = ((clock * cpp / 1000) * display_latency_ns) / 1000;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300747 tlb_miss = display->fifo_size*display->cacheline_size - hdisplay * 8;
748 if (tlb_miss > 0)
749 entries += tlb_miss;
750 entries = DIV_ROUND_UP(entries, display->cacheline_size);
751 *plane_wm = entries + display->guard_size;
752 if (*plane_wm > (int)display->max_wm)
753 *plane_wm = display->max_wm;
754
755 /* Use the large buffer method to calculate cursor watermark */
Ville Syrjälä922044c2014-02-14 14:18:57 +0200756 line_time_us = max(htotal * 1000 / clock, 1);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300757 line_count = (cursor_latency_ns / line_time_us + 1000) / 1000;
Ville Syrjäläefc26112016-10-31 22:37:04 +0200758 entries = line_count * crtc->base.cursor->state->crtc_w * cpp;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300759 tlb_miss = cursor->fifo_size*cursor->cacheline_size - hdisplay * 8;
760 if (tlb_miss > 0)
761 entries += tlb_miss;
762 entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
763 *cursor_wm = entries + cursor->guard_size;
764 if (*cursor_wm > (int)cursor->max_wm)
765 *cursor_wm = (int)cursor->max_wm;
766
767 return true;
768}
769
770/*
771 * Check the wm result.
772 *
773 * If any calculated watermark values is larger than the maximum value that
774 * can be programmed into the associated watermark register, that watermark
775 * must be disabled.
776 */
Ville Syrjäläf0ce2312016-10-31 22:37:08 +0200777static bool g4x_check_srwm(struct drm_i915_private *dev_priv,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300778 int display_wm, int cursor_wm,
779 const struct intel_watermark_params *display,
780 const struct intel_watermark_params *cursor)
781{
782 DRM_DEBUG_KMS("SR watermark: display plane %d, cursor %d\n",
783 display_wm, cursor_wm);
784
785 if (display_wm > display->max_wm) {
Tvrtko Ursulinae9400c2016-10-13 11:09:25 +0100786 DRM_DEBUG_KMS("display watermark is too large(%d/%u), disabling\n",
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300787 display_wm, display->max_wm);
788 return false;
789 }
790
791 if (cursor_wm > cursor->max_wm) {
Tvrtko Ursulinae9400c2016-10-13 11:09:25 +0100792 DRM_DEBUG_KMS("cursor watermark is too large(%d/%u), disabling\n",
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300793 cursor_wm, cursor->max_wm);
794 return false;
795 }
796
797 if (!(display_wm || cursor_wm)) {
798 DRM_DEBUG_KMS("SR latency is 0, disabling\n");
799 return false;
800 }
801
802 return true;
803}
804
Ville Syrjäläf0ce2312016-10-31 22:37:08 +0200805static bool g4x_compute_srwm(struct drm_i915_private *dev_priv,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300806 int plane,
807 int latency_ns,
808 const struct intel_watermark_params *display,
809 const struct intel_watermark_params *cursor,
810 int *display_wm, int *cursor_wm)
811{
Ville Syrjäläefc26112016-10-31 22:37:04 +0200812 struct intel_crtc *crtc;
Ville Syrjälä4fe85902013-09-04 18:25:22 +0300813 const struct drm_display_mode *adjusted_mode;
Ville Syrjäläefc26112016-10-31 22:37:04 +0200814 const struct drm_framebuffer *fb;
Ville Syrjäläac484962016-01-20 21:05:26 +0200815 int hdisplay, htotal, cpp, clock;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300816 unsigned long line_time_us;
817 int line_count, line_size;
818 int small, large;
819 int entries;
820
821 if (!latency_ns) {
822 *display_wm = *cursor_wm = 0;
823 return false;
824 }
825
Ville Syrjäläb91eb5c2016-10-31 22:37:09 +0200826 crtc = intel_get_crtc_for_plane(dev_priv, plane);
Ville Syrjäläefc26112016-10-31 22:37:04 +0200827 adjusted_mode = &crtc->config->base.adjusted_mode;
828 fb = crtc->base.primary->state->fb;
Damien Lespiau241bfc32013-09-25 16:45:37 +0100829 clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -0800830 htotal = adjusted_mode->crtc_htotal;
Ville Syrjäläefc26112016-10-31 22:37:04 +0200831 hdisplay = crtc->config->pipe_src_w;
832 cpp = drm_format_plane_cpp(fb->pixel_format, 0);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300833
Ville Syrjälä922044c2014-02-14 14:18:57 +0200834 line_time_us = max(htotal * 1000 / clock, 1);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300835 line_count = (latency_ns / line_time_us + 1000) / 1000;
Ville Syrjäläac484962016-01-20 21:05:26 +0200836 line_size = hdisplay * cpp;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300837
838 /* Use the minimum of the small and large buffer method for primary */
Ville Syrjäläac484962016-01-20 21:05:26 +0200839 small = ((clock * cpp / 1000) * latency_ns) / 1000;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300840 large = line_count * line_size;
841
842 entries = DIV_ROUND_UP(min(small, large), display->cacheline_size);
843 *display_wm = entries + display->guard_size;
844
845 /* calculate the self-refresh watermark for display cursor */
Ville Syrjäläefc26112016-10-31 22:37:04 +0200846 entries = line_count * cpp * crtc->base.cursor->state->crtc_w;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300847 entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
848 *cursor_wm = entries + cursor->guard_size;
849
Ville Syrjäläf0ce2312016-10-31 22:37:08 +0200850 return g4x_check_srwm(dev_priv,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300851 *display_wm, *cursor_wm,
852 display, cursor);
853}
854
Ville Syrjälä15665972015-03-10 16:16:28 +0200855#define FW_WM_VLV(value, plane) \
856 (((value) << DSPFW_ ## plane ## _SHIFT) & DSPFW_ ## plane ## _MASK_VLV)
857
Ville Syrjälä0018fda2015-03-05 21:19:45 +0200858static void vlv_write_wm_values(struct intel_crtc *crtc,
859 const struct vlv_wm_values *wm)
860{
861 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
862 enum pipe pipe = crtc->pipe;
863
864 I915_WRITE(VLV_DDL(pipe),
865 (wm->ddl[pipe].cursor << DDL_CURSOR_SHIFT) |
866 (wm->ddl[pipe].sprite[1] << DDL_SPRITE_SHIFT(1)) |
867 (wm->ddl[pipe].sprite[0] << DDL_SPRITE_SHIFT(0)) |
868 (wm->ddl[pipe].primary << DDL_PLANE_SHIFT));
869
Ville Syrjäläae801522015-03-05 21:19:49 +0200870 I915_WRITE(DSPFW1,
Ville Syrjälä15665972015-03-10 16:16:28 +0200871 FW_WM(wm->sr.plane, SR) |
872 FW_WM(wm->pipe[PIPE_B].cursor, CURSORB) |
873 FW_WM_VLV(wm->pipe[PIPE_B].primary, PLANEB) |
874 FW_WM_VLV(wm->pipe[PIPE_A].primary, PLANEA));
Ville Syrjäläae801522015-03-05 21:19:49 +0200875 I915_WRITE(DSPFW2,
Ville Syrjälä15665972015-03-10 16:16:28 +0200876 FW_WM_VLV(wm->pipe[PIPE_A].sprite[1], SPRITEB) |
877 FW_WM(wm->pipe[PIPE_A].cursor, CURSORA) |
878 FW_WM_VLV(wm->pipe[PIPE_A].sprite[0], SPRITEA));
Ville Syrjäläae801522015-03-05 21:19:49 +0200879 I915_WRITE(DSPFW3,
Ville Syrjälä15665972015-03-10 16:16:28 +0200880 FW_WM(wm->sr.cursor, CURSOR_SR));
Ville Syrjäläae801522015-03-05 21:19:49 +0200881
882 if (IS_CHERRYVIEW(dev_priv)) {
883 I915_WRITE(DSPFW7_CHV,
Ville Syrjälä15665972015-03-10 16:16:28 +0200884 FW_WM_VLV(wm->pipe[PIPE_B].sprite[1], SPRITED) |
885 FW_WM_VLV(wm->pipe[PIPE_B].sprite[0], SPRITEC));
Ville Syrjäläae801522015-03-05 21:19:49 +0200886 I915_WRITE(DSPFW8_CHV,
Ville Syrjälä15665972015-03-10 16:16:28 +0200887 FW_WM_VLV(wm->pipe[PIPE_C].sprite[1], SPRITEF) |
888 FW_WM_VLV(wm->pipe[PIPE_C].sprite[0], SPRITEE));
Ville Syrjäläae801522015-03-05 21:19:49 +0200889 I915_WRITE(DSPFW9_CHV,
Ville Syrjälä15665972015-03-10 16:16:28 +0200890 FW_WM_VLV(wm->pipe[PIPE_C].primary, PLANEC) |
891 FW_WM(wm->pipe[PIPE_C].cursor, CURSORC));
Ville Syrjäläae801522015-03-05 21:19:49 +0200892 I915_WRITE(DSPHOWM,
Ville Syrjälä15665972015-03-10 16:16:28 +0200893 FW_WM(wm->sr.plane >> 9, SR_HI) |
894 FW_WM(wm->pipe[PIPE_C].sprite[1] >> 8, SPRITEF_HI) |
895 FW_WM(wm->pipe[PIPE_C].sprite[0] >> 8, SPRITEE_HI) |
896 FW_WM(wm->pipe[PIPE_C].primary >> 8, PLANEC_HI) |
897 FW_WM(wm->pipe[PIPE_B].sprite[1] >> 8, SPRITED_HI) |
898 FW_WM(wm->pipe[PIPE_B].sprite[0] >> 8, SPRITEC_HI) |
899 FW_WM(wm->pipe[PIPE_B].primary >> 8, PLANEB_HI) |
900 FW_WM(wm->pipe[PIPE_A].sprite[1] >> 8, SPRITEB_HI) |
901 FW_WM(wm->pipe[PIPE_A].sprite[0] >> 8, SPRITEA_HI) |
902 FW_WM(wm->pipe[PIPE_A].primary >> 8, PLANEA_HI));
Ville Syrjäläae801522015-03-05 21:19:49 +0200903 } else {
904 I915_WRITE(DSPFW7,
Ville Syrjälä15665972015-03-10 16:16:28 +0200905 FW_WM_VLV(wm->pipe[PIPE_B].sprite[1], SPRITED) |
906 FW_WM_VLV(wm->pipe[PIPE_B].sprite[0], SPRITEC));
Ville Syrjäläae801522015-03-05 21:19:49 +0200907 I915_WRITE(DSPHOWM,
Ville Syrjälä15665972015-03-10 16:16:28 +0200908 FW_WM(wm->sr.plane >> 9, SR_HI) |
909 FW_WM(wm->pipe[PIPE_B].sprite[1] >> 8, SPRITED_HI) |
910 FW_WM(wm->pipe[PIPE_B].sprite[0] >> 8, SPRITEC_HI) |
911 FW_WM(wm->pipe[PIPE_B].primary >> 8, PLANEB_HI) |
912 FW_WM(wm->pipe[PIPE_A].sprite[1] >> 8, SPRITEB_HI) |
913 FW_WM(wm->pipe[PIPE_A].sprite[0] >> 8, SPRITEA_HI) |
914 FW_WM(wm->pipe[PIPE_A].primary >> 8, PLANEA_HI));
Ville Syrjäläae801522015-03-05 21:19:49 +0200915 }
916
Ville Syrjälä2cb389b2015-06-24 22:00:10 +0300917 /* zero (unused) WM1 watermarks */
918 I915_WRITE(DSPFW4, 0);
919 I915_WRITE(DSPFW5, 0);
920 I915_WRITE(DSPFW6, 0);
921 I915_WRITE(DSPHOWM1, 0);
922
Ville Syrjäläae801522015-03-05 21:19:49 +0200923 POSTING_READ(DSPFW1);
Ville Syrjälä0018fda2015-03-05 21:19:45 +0200924}
925
Ville Syrjälä15665972015-03-10 16:16:28 +0200926#undef FW_WM_VLV
927
Ville Syrjälä6eb1a682015-06-24 22:00:03 +0300928enum vlv_wm_level {
929 VLV_WM_LEVEL_PM2,
930 VLV_WM_LEVEL_PM5,
931 VLV_WM_LEVEL_DDR_DVFS,
Ville Syrjälä6eb1a682015-06-24 22:00:03 +0300932};
933
Ville Syrjälä262cd2e2015-06-24 22:00:04 +0300934/* latency must be in 0.1us units. */
935static unsigned int vlv_wm_method2(unsigned int pixel_rate,
936 unsigned int pipe_htotal,
937 unsigned int horiz_pixels,
Ville Syrjäläac484962016-01-20 21:05:26 +0200938 unsigned int cpp,
Ville Syrjälä262cd2e2015-06-24 22:00:04 +0300939 unsigned int latency)
940{
941 unsigned int ret;
942
943 ret = (latency * pixel_rate) / (pipe_htotal * 10000);
Ville Syrjäläac484962016-01-20 21:05:26 +0200944 ret = (ret + 1) * horiz_pixels * cpp;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +0300945 ret = DIV_ROUND_UP(ret, 64);
946
947 return ret;
948}
949
950static void vlv_setup_wm_latency(struct drm_device *dev)
951{
Chris Wilsonfac5e232016-07-04 11:34:36 +0100952 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +0300953
954 /* all latencies in usec */
955 dev_priv->wm.pri_latency[VLV_WM_LEVEL_PM2] = 3;
956
Ville Syrjälä58590c12015-09-08 21:05:12 +0300957 dev_priv->wm.max_level = VLV_WM_LEVEL_PM2;
958
Ville Syrjälä262cd2e2015-06-24 22:00:04 +0300959 if (IS_CHERRYVIEW(dev_priv)) {
960 dev_priv->wm.pri_latency[VLV_WM_LEVEL_PM5] = 12;
961 dev_priv->wm.pri_latency[VLV_WM_LEVEL_DDR_DVFS] = 33;
Ville Syrjälä58590c12015-09-08 21:05:12 +0300962
963 dev_priv->wm.max_level = VLV_WM_LEVEL_DDR_DVFS;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +0300964 }
965}
966
967static uint16_t vlv_compute_wm_level(struct intel_plane *plane,
968 struct intel_crtc *crtc,
969 const struct intel_plane_state *state,
970 int level)
971{
972 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
Ville Syrjäläac484962016-01-20 21:05:26 +0200973 int clock, htotal, cpp, width, wm;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +0300974
975 if (dev_priv->wm.pri_latency[level] == 0)
976 return USHRT_MAX;
977
Ville Syrjälä936e71e2016-07-26 19:06:59 +0300978 if (!state->base.visible)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +0300979 return 0;
980
Ville Syrjäläac484962016-01-20 21:05:26 +0200981 cpp = drm_format_plane_cpp(state->base.fb->pixel_format, 0);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +0300982 clock = crtc->config->base.adjusted_mode.crtc_clock;
983 htotal = crtc->config->base.adjusted_mode.crtc_htotal;
984 width = crtc->config->pipe_src_w;
985 if (WARN_ON(htotal == 0))
986 htotal = 1;
987
988 if (plane->base.type == DRM_PLANE_TYPE_CURSOR) {
989 /*
990 * FIXME the formula gives values that are
991 * too big for the cursor FIFO, and hence we
992 * would never be able to use cursors. For
993 * now just hardcode the watermark.
994 */
995 wm = 63;
996 } else {
Ville Syrjäläac484962016-01-20 21:05:26 +0200997 wm = vlv_wm_method2(clock, htotal, width, cpp,
Ville Syrjälä262cd2e2015-06-24 22:00:04 +0300998 dev_priv->wm.pri_latency[level] * 10);
999 }
1000
1001 return min_t(int, wm, USHRT_MAX);
1002}
1003
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001004static void vlv_compute_fifo(struct intel_crtc *crtc)
1005{
1006 struct drm_device *dev = crtc->base.dev;
1007 struct vlv_wm_state *wm_state = &crtc->wm_state;
1008 struct intel_plane *plane;
1009 unsigned int total_rate = 0;
1010 const int fifo_size = 512 - 1;
1011 int fifo_extra, fifo_left = fifo_size;
1012
1013 for_each_intel_plane_on_crtc(dev, crtc, plane) {
1014 struct intel_plane_state *state =
1015 to_intel_plane_state(plane->base.state);
1016
1017 if (plane->base.type == DRM_PLANE_TYPE_CURSOR)
1018 continue;
1019
Ville Syrjälä936e71e2016-07-26 19:06:59 +03001020 if (state->base.visible) {
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001021 wm_state->num_active_planes++;
1022 total_rate += drm_format_plane_cpp(state->base.fb->pixel_format, 0);
1023 }
1024 }
1025
1026 for_each_intel_plane_on_crtc(dev, crtc, plane) {
1027 struct intel_plane_state *state =
1028 to_intel_plane_state(plane->base.state);
1029 unsigned int rate;
1030
1031 if (plane->base.type == DRM_PLANE_TYPE_CURSOR) {
1032 plane->wm.fifo_size = 63;
1033 continue;
1034 }
1035
Ville Syrjälä936e71e2016-07-26 19:06:59 +03001036 if (!state->base.visible) {
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001037 plane->wm.fifo_size = 0;
1038 continue;
1039 }
1040
1041 rate = drm_format_plane_cpp(state->base.fb->pixel_format, 0);
1042 plane->wm.fifo_size = fifo_size * rate / total_rate;
1043 fifo_left -= plane->wm.fifo_size;
1044 }
1045
1046 fifo_extra = DIV_ROUND_UP(fifo_left, wm_state->num_active_planes ?: 1);
1047
1048 /* spread the remainder evenly */
1049 for_each_intel_plane_on_crtc(dev, crtc, plane) {
1050 int plane_extra;
1051
1052 if (fifo_left == 0)
1053 break;
1054
1055 if (plane->base.type == DRM_PLANE_TYPE_CURSOR)
1056 continue;
1057
1058 /* give it all to the first plane if none are active */
1059 if (plane->wm.fifo_size == 0 &&
1060 wm_state->num_active_planes)
1061 continue;
1062
1063 plane_extra = min(fifo_extra, fifo_left);
1064 plane->wm.fifo_size += plane_extra;
1065 fifo_left -= plane_extra;
1066 }
1067
1068 WARN_ON(fifo_left != 0);
1069}
1070
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001071static void vlv_invert_wms(struct intel_crtc *crtc)
1072{
1073 struct vlv_wm_state *wm_state = &crtc->wm_state;
1074 int level;
1075
1076 for (level = 0; level < wm_state->num_levels; level++) {
1077 struct drm_device *dev = crtc->base.dev;
1078 const int sr_fifo_size = INTEL_INFO(dev)->num_pipes * 512 - 1;
1079 struct intel_plane *plane;
1080
1081 wm_state->sr[level].plane = sr_fifo_size - wm_state->sr[level].plane;
1082 wm_state->sr[level].cursor = 63 - wm_state->sr[level].cursor;
1083
1084 for_each_intel_plane_on_crtc(dev, crtc, plane) {
1085 switch (plane->base.type) {
1086 int sprite;
1087 case DRM_PLANE_TYPE_CURSOR:
1088 wm_state->wm[level].cursor = plane->wm.fifo_size -
1089 wm_state->wm[level].cursor;
1090 break;
1091 case DRM_PLANE_TYPE_PRIMARY:
1092 wm_state->wm[level].primary = plane->wm.fifo_size -
1093 wm_state->wm[level].primary;
1094 break;
1095 case DRM_PLANE_TYPE_OVERLAY:
1096 sprite = plane->plane;
1097 wm_state->wm[level].sprite[sprite] = plane->wm.fifo_size -
1098 wm_state->wm[level].sprite[sprite];
1099 break;
1100 }
1101 }
1102 }
1103}
1104
Ville Syrjälä26e1fe42015-06-24 22:00:06 +03001105static void vlv_compute_wm(struct intel_crtc *crtc)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001106{
1107 struct drm_device *dev = crtc->base.dev;
1108 struct vlv_wm_state *wm_state = &crtc->wm_state;
1109 struct intel_plane *plane;
1110 int sr_fifo_size = INTEL_INFO(dev)->num_pipes * 512 - 1;
1111 int level;
1112
1113 memset(wm_state, 0, sizeof(*wm_state));
1114
Ville Syrjälä852eb002015-06-24 22:00:07 +03001115 wm_state->cxsr = crtc->pipe != PIPE_C && crtc->wm.cxsr_allowed;
Ville Syrjälä58590c12015-09-08 21:05:12 +03001116 wm_state->num_levels = to_i915(dev)->wm.max_level + 1;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001117
1118 wm_state->num_active_planes = 0;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001119
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001120 vlv_compute_fifo(crtc);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001121
1122 if (wm_state->num_active_planes != 1)
1123 wm_state->cxsr = false;
1124
1125 if (wm_state->cxsr) {
1126 for (level = 0; level < wm_state->num_levels; level++) {
1127 wm_state->sr[level].plane = sr_fifo_size;
1128 wm_state->sr[level].cursor = 63;
1129 }
1130 }
1131
1132 for_each_intel_plane_on_crtc(dev, crtc, plane) {
1133 struct intel_plane_state *state =
1134 to_intel_plane_state(plane->base.state);
1135
Ville Syrjälä936e71e2016-07-26 19:06:59 +03001136 if (!state->base.visible)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001137 continue;
1138
1139 /* normal watermarks */
1140 for (level = 0; level < wm_state->num_levels; level++) {
1141 int wm = vlv_compute_wm_level(plane, crtc, state, level);
1142 int max_wm = plane->base.type == DRM_PLANE_TYPE_CURSOR ? 63 : 511;
1143
1144 /* hack */
1145 if (WARN_ON(level == 0 && wm > max_wm))
1146 wm = max_wm;
1147
1148 if (wm > plane->wm.fifo_size)
1149 break;
1150
1151 switch (plane->base.type) {
1152 int sprite;
1153 case DRM_PLANE_TYPE_CURSOR:
1154 wm_state->wm[level].cursor = wm;
1155 break;
1156 case DRM_PLANE_TYPE_PRIMARY:
1157 wm_state->wm[level].primary = wm;
1158 break;
1159 case DRM_PLANE_TYPE_OVERLAY:
1160 sprite = plane->plane;
1161 wm_state->wm[level].sprite[sprite] = wm;
1162 break;
1163 }
1164 }
1165
1166 wm_state->num_levels = level;
1167
1168 if (!wm_state->cxsr)
1169 continue;
1170
1171 /* maxfifo watermarks */
1172 switch (plane->base.type) {
1173 int sprite, level;
1174 case DRM_PLANE_TYPE_CURSOR:
1175 for (level = 0; level < wm_state->num_levels; level++)
1176 wm_state->sr[level].cursor =
Thomas Daniel5a37ed02015-10-23 14:55:38 +01001177 wm_state->wm[level].cursor;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001178 break;
1179 case DRM_PLANE_TYPE_PRIMARY:
1180 for (level = 0; level < wm_state->num_levels; level++)
1181 wm_state->sr[level].plane =
1182 min(wm_state->sr[level].plane,
1183 wm_state->wm[level].primary);
1184 break;
1185 case DRM_PLANE_TYPE_OVERLAY:
1186 sprite = plane->plane;
1187 for (level = 0; level < wm_state->num_levels; level++)
1188 wm_state->sr[level].plane =
1189 min(wm_state->sr[level].plane,
1190 wm_state->wm[level].sprite[sprite]);
1191 break;
1192 }
1193 }
1194
1195 /* clear any (partially) filled invalid levels */
Ville Syrjälä58590c12015-09-08 21:05:12 +03001196 for (level = wm_state->num_levels; level < to_i915(dev)->wm.max_level + 1; level++) {
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001197 memset(&wm_state->wm[level], 0, sizeof(wm_state->wm[level]));
1198 memset(&wm_state->sr[level], 0, sizeof(wm_state->sr[level]));
1199 }
1200
1201 vlv_invert_wms(crtc);
1202}
1203
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001204#define VLV_FIFO(plane, value) \
1205 (((value) << DSPARB_ ## plane ## _SHIFT_VLV) & DSPARB_ ## plane ## _MASK_VLV)
1206
1207static void vlv_pipe_set_fifo_size(struct intel_crtc *crtc)
1208{
1209 struct drm_device *dev = crtc->base.dev;
1210 struct drm_i915_private *dev_priv = to_i915(dev);
1211 struct intel_plane *plane;
1212 int sprite0_start = 0, sprite1_start = 0, fifo_size = 0;
1213
1214 for_each_intel_plane_on_crtc(dev, crtc, plane) {
1215 if (plane->base.type == DRM_PLANE_TYPE_CURSOR) {
1216 WARN_ON(plane->wm.fifo_size != 63);
1217 continue;
1218 }
1219
1220 if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
1221 sprite0_start = plane->wm.fifo_size;
1222 else if (plane->plane == 0)
1223 sprite1_start = sprite0_start + plane->wm.fifo_size;
1224 else
1225 fifo_size = sprite1_start + plane->wm.fifo_size;
1226 }
1227
1228 WARN_ON(fifo_size != 512 - 1);
1229
1230 DRM_DEBUG_KMS("Pipe %c FIFO split %d / %d / %d\n",
1231 pipe_name(crtc->pipe), sprite0_start,
1232 sprite1_start, fifo_size);
1233
1234 switch (crtc->pipe) {
1235 uint32_t dsparb, dsparb2, dsparb3;
1236 case PIPE_A:
1237 dsparb = I915_READ(DSPARB);
1238 dsparb2 = I915_READ(DSPARB2);
1239
1240 dsparb &= ~(VLV_FIFO(SPRITEA, 0xff) |
1241 VLV_FIFO(SPRITEB, 0xff));
1242 dsparb |= (VLV_FIFO(SPRITEA, sprite0_start) |
1243 VLV_FIFO(SPRITEB, sprite1_start));
1244
1245 dsparb2 &= ~(VLV_FIFO(SPRITEA_HI, 0x1) |
1246 VLV_FIFO(SPRITEB_HI, 0x1));
1247 dsparb2 |= (VLV_FIFO(SPRITEA_HI, sprite0_start >> 8) |
1248 VLV_FIFO(SPRITEB_HI, sprite1_start >> 8));
1249
1250 I915_WRITE(DSPARB, dsparb);
1251 I915_WRITE(DSPARB2, dsparb2);
1252 break;
1253 case PIPE_B:
1254 dsparb = I915_READ(DSPARB);
1255 dsparb2 = I915_READ(DSPARB2);
1256
1257 dsparb &= ~(VLV_FIFO(SPRITEC, 0xff) |
1258 VLV_FIFO(SPRITED, 0xff));
1259 dsparb |= (VLV_FIFO(SPRITEC, sprite0_start) |
1260 VLV_FIFO(SPRITED, sprite1_start));
1261
1262 dsparb2 &= ~(VLV_FIFO(SPRITEC_HI, 0xff) |
1263 VLV_FIFO(SPRITED_HI, 0xff));
1264 dsparb2 |= (VLV_FIFO(SPRITEC_HI, sprite0_start >> 8) |
1265 VLV_FIFO(SPRITED_HI, sprite1_start >> 8));
1266
1267 I915_WRITE(DSPARB, dsparb);
1268 I915_WRITE(DSPARB2, dsparb2);
1269 break;
1270 case PIPE_C:
1271 dsparb3 = I915_READ(DSPARB3);
1272 dsparb2 = I915_READ(DSPARB2);
1273
1274 dsparb3 &= ~(VLV_FIFO(SPRITEE, 0xff) |
1275 VLV_FIFO(SPRITEF, 0xff));
1276 dsparb3 |= (VLV_FIFO(SPRITEE, sprite0_start) |
1277 VLV_FIFO(SPRITEF, sprite1_start));
1278
1279 dsparb2 &= ~(VLV_FIFO(SPRITEE_HI, 0xff) |
1280 VLV_FIFO(SPRITEF_HI, 0xff));
1281 dsparb2 |= (VLV_FIFO(SPRITEE_HI, sprite0_start >> 8) |
1282 VLV_FIFO(SPRITEF_HI, sprite1_start >> 8));
1283
1284 I915_WRITE(DSPARB3, dsparb3);
1285 I915_WRITE(DSPARB2, dsparb2);
1286 break;
1287 default:
1288 break;
1289 }
1290}
1291
1292#undef VLV_FIFO
1293
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001294static void vlv_merge_wm(struct drm_device *dev,
1295 struct vlv_wm_values *wm)
1296{
1297 struct intel_crtc *crtc;
1298 int num_active_crtcs = 0;
1299
Ville Syrjälä58590c12015-09-08 21:05:12 +03001300 wm->level = to_i915(dev)->wm.max_level;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001301 wm->cxsr = true;
1302
1303 for_each_intel_crtc(dev, crtc) {
1304 const struct vlv_wm_state *wm_state = &crtc->wm_state;
1305
1306 if (!crtc->active)
1307 continue;
1308
1309 if (!wm_state->cxsr)
1310 wm->cxsr = false;
1311
1312 num_active_crtcs++;
1313 wm->level = min_t(int, wm->level, wm_state->num_levels - 1);
1314 }
1315
1316 if (num_active_crtcs != 1)
1317 wm->cxsr = false;
1318
Ville Syrjälä6f9c7842015-06-24 22:00:08 +03001319 if (num_active_crtcs > 1)
1320 wm->level = VLV_WM_LEVEL_PM2;
1321
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001322 for_each_intel_crtc(dev, crtc) {
1323 struct vlv_wm_state *wm_state = &crtc->wm_state;
1324 enum pipe pipe = crtc->pipe;
1325
1326 if (!crtc->active)
1327 continue;
1328
1329 wm->pipe[pipe] = wm_state->wm[wm->level];
1330 if (wm->cxsr)
1331 wm->sr = wm_state->sr[wm->level];
1332
1333 wm->ddl[pipe].primary = DDL_PRECISION_HIGH | 2;
1334 wm->ddl[pipe].sprite[0] = DDL_PRECISION_HIGH | 2;
1335 wm->ddl[pipe].sprite[1] = DDL_PRECISION_HIGH | 2;
1336 wm->ddl[pipe].cursor = DDL_PRECISION_HIGH | 2;
1337 }
1338}
1339
Ville Syrjälä432081b2016-10-31 22:37:03 +02001340static void vlv_update_wm(struct intel_crtc *crtc)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001341{
Ville Syrjälä432081b2016-10-31 22:37:03 +02001342 struct drm_device *dev = crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01001343 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä432081b2016-10-31 22:37:03 +02001344 enum pipe pipe = crtc->pipe;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001345 struct vlv_wm_values wm = {};
1346
Ville Syrjälä432081b2016-10-31 22:37:03 +02001347 vlv_compute_wm(crtc);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001348 vlv_merge_wm(dev, &wm);
1349
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001350 if (memcmp(&dev_priv->wm.vlv, &wm, sizeof(wm)) == 0) {
1351 /* FIXME should be part of crtc atomic commit */
Ville Syrjälä432081b2016-10-31 22:37:03 +02001352 vlv_pipe_set_fifo_size(crtc);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001353 return;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001354 }
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001355
1356 if (wm.level < VLV_WM_LEVEL_DDR_DVFS &&
1357 dev_priv->wm.vlv.level >= VLV_WM_LEVEL_DDR_DVFS)
1358 chv_set_memory_dvfs(dev_priv, false);
1359
1360 if (wm.level < VLV_WM_LEVEL_PM5 &&
1361 dev_priv->wm.vlv.level >= VLV_WM_LEVEL_PM5)
1362 chv_set_memory_pm5(dev_priv, false);
1363
Ville Syrjälä852eb002015-06-24 22:00:07 +03001364 if (!wm.cxsr && dev_priv->wm.vlv.cxsr)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001365 intel_set_memory_cxsr(dev_priv, false);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001366
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001367 /* FIXME should be part of crtc atomic commit */
Ville Syrjälä432081b2016-10-31 22:37:03 +02001368 vlv_pipe_set_fifo_size(crtc);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001369
Ville Syrjälä432081b2016-10-31 22:37:03 +02001370 vlv_write_wm_values(crtc, &wm);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001371
1372 DRM_DEBUG_KMS("Setting FIFO watermarks - %c: plane=%d, cursor=%d, "
1373 "sprite0=%d, sprite1=%d, SR: plane=%d, cursor=%d level=%d cxsr=%d\n",
1374 pipe_name(pipe), wm.pipe[pipe].primary, wm.pipe[pipe].cursor,
1375 wm.pipe[pipe].sprite[0], wm.pipe[pipe].sprite[1],
1376 wm.sr.plane, wm.sr.cursor, wm.level, wm.cxsr);
1377
Ville Syrjälä852eb002015-06-24 22:00:07 +03001378 if (wm.cxsr && !dev_priv->wm.vlv.cxsr)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001379 intel_set_memory_cxsr(dev_priv, true);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001380
1381 if (wm.level >= VLV_WM_LEVEL_PM5 &&
1382 dev_priv->wm.vlv.level < VLV_WM_LEVEL_PM5)
1383 chv_set_memory_pm5(dev_priv, true);
1384
1385 if (wm.level >= VLV_WM_LEVEL_DDR_DVFS &&
1386 dev_priv->wm.vlv.level < VLV_WM_LEVEL_DDR_DVFS)
1387 chv_set_memory_dvfs(dev_priv, true);
1388
1389 dev_priv->wm.vlv = wm;
Ville Syrjälä3c2777f2014-06-26 17:03:06 +03001390}
1391
Ville Syrjäläae801522015-03-05 21:19:49 +02001392#define single_plane_enabled(mask) is_power_of_2(mask)
1393
Ville Syrjälä432081b2016-10-31 22:37:03 +02001394static void g4x_update_wm(struct intel_crtc *crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001395{
Ville Syrjäläb91eb5c2016-10-31 22:37:09 +02001396 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001397 static const int sr_latency_ns = 12000;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001398 int planea_wm, planeb_wm, cursora_wm, cursorb_wm;
1399 int plane_sr, cursor_sr;
1400 unsigned int enabled = 0;
Imre Deak98584252014-06-13 14:54:20 +03001401 bool cxsr_enabled;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001402
Ville Syrjäläf0ce2312016-10-31 22:37:08 +02001403 if (g4x_compute_wm0(dev_priv, PIPE_A,
Chris Wilson5aef6002014-09-03 11:56:07 +01001404 &g4x_wm_info, pessimal_latency_ns,
1405 &g4x_cursor_wm_info, pessimal_latency_ns,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001406 &planea_wm, &cursora_wm))
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001407 enabled |= 1 << PIPE_A;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001408
Ville Syrjäläf0ce2312016-10-31 22:37:08 +02001409 if (g4x_compute_wm0(dev_priv, PIPE_B,
Chris Wilson5aef6002014-09-03 11:56:07 +01001410 &g4x_wm_info, pessimal_latency_ns,
1411 &g4x_cursor_wm_info, pessimal_latency_ns,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001412 &planeb_wm, &cursorb_wm))
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001413 enabled |= 1 << PIPE_B;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001414
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001415 if (single_plane_enabled(enabled) &&
Ville Syrjäläf0ce2312016-10-31 22:37:08 +02001416 g4x_compute_srwm(dev_priv, ffs(enabled) - 1,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001417 sr_latency_ns,
1418 &g4x_wm_info,
1419 &g4x_cursor_wm_info,
Chris Wilson52bd02d2012-12-07 10:43:24 +00001420 &plane_sr, &cursor_sr)) {
Imre Deak98584252014-06-13 14:54:20 +03001421 cxsr_enabled = true;
Chris Wilson52bd02d2012-12-07 10:43:24 +00001422 } else {
Imre Deak98584252014-06-13 14:54:20 +03001423 cxsr_enabled = false;
Imre Deak5209b1f2014-07-01 12:36:17 +03001424 intel_set_memory_cxsr(dev_priv, false);
Chris Wilson52bd02d2012-12-07 10:43:24 +00001425 plane_sr = cursor_sr = 0;
1426 }
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001427
Ville Syrjäläa5043452014-06-28 02:04:18 +03001428 DRM_DEBUG_KMS("Setting FIFO watermarks - A: plane=%d, cursor=%d, "
1429 "B: plane=%d, cursor=%d, SR: plane=%d, cursor=%d\n",
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001430 planea_wm, cursora_wm,
1431 planeb_wm, cursorb_wm,
1432 plane_sr, cursor_sr);
1433
1434 I915_WRITE(DSPFW1,
Ville Syrjäläf4998962015-03-10 17:02:21 +02001435 FW_WM(plane_sr, SR) |
1436 FW_WM(cursorb_wm, CURSORB) |
1437 FW_WM(planeb_wm, PLANEB) |
1438 FW_WM(planea_wm, PLANEA));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001439 I915_WRITE(DSPFW2,
Chris Wilson8c919b22012-12-04 16:33:19 +00001440 (I915_READ(DSPFW2) & ~DSPFW_CURSORA_MASK) |
Ville Syrjäläf4998962015-03-10 17:02:21 +02001441 FW_WM(cursora_wm, CURSORA));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001442 /* HPLL off in SR has some issues on G4x... disable it */
1443 I915_WRITE(DSPFW3,
Chris Wilson8c919b22012-12-04 16:33:19 +00001444 (I915_READ(DSPFW3) & ~(DSPFW_HPLL_SR_EN | DSPFW_CURSOR_SR_MASK)) |
Ville Syrjäläf4998962015-03-10 17:02:21 +02001445 FW_WM(cursor_sr, CURSOR_SR));
Imre Deak98584252014-06-13 14:54:20 +03001446
1447 if (cxsr_enabled)
1448 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001449}
1450
Ville Syrjälä432081b2016-10-31 22:37:03 +02001451static void i965_update_wm(struct intel_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001452{
Ville Syrjälä432081b2016-10-31 22:37:03 +02001453 struct drm_device *dev = unused_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01001454 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläefc26112016-10-31 22:37:04 +02001455 struct intel_crtc *crtc;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001456 int srwm = 1;
1457 int cursor_sr = 16;
Imre Deak98584252014-06-13 14:54:20 +03001458 bool cxsr_enabled;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001459
1460 /* Calc sr entries for one plane configs */
1461 crtc = single_enabled_crtc(dev);
1462 if (crtc) {
1463 /* self-refresh has much higher latency */
1464 static const int sr_latency_ns = 12000;
Ville Syrjäläefc26112016-10-31 22:37:04 +02001465 const struct drm_display_mode *adjusted_mode =
1466 &crtc->config->base.adjusted_mode;
1467 const struct drm_framebuffer *fb =
1468 crtc->base.primary->state->fb;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001469 int clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -08001470 int htotal = adjusted_mode->crtc_htotal;
Ville Syrjäläefc26112016-10-31 22:37:04 +02001471 int hdisplay = crtc->config->pipe_src_w;
1472 int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001473 unsigned long line_time_us;
1474 int entries;
1475
Ville Syrjälä922044c2014-02-14 14:18:57 +02001476 line_time_us = max(htotal * 1000 / clock, 1);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001477
1478 /* Use ns/us then divide to preserve precision */
1479 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
Ville Syrjäläac484962016-01-20 21:05:26 +02001480 cpp * hdisplay;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001481 entries = DIV_ROUND_UP(entries, I915_FIFO_LINE_SIZE);
1482 srwm = I965_FIFO_SIZE - entries;
1483 if (srwm < 0)
1484 srwm = 1;
1485 srwm &= 0x1ff;
1486 DRM_DEBUG_KMS("self-refresh entries: %d, wm: %d\n",
1487 entries, srwm);
1488
1489 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
Ville Syrjäläefc26112016-10-31 22:37:04 +02001490 cpp * crtc->base.cursor->state->crtc_w;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001491 entries = DIV_ROUND_UP(entries,
1492 i965_cursor_wm_info.cacheline_size);
1493 cursor_sr = i965_cursor_wm_info.fifo_size -
1494 (entries + i965_cursor_wm_info.guard_size);
1495
1496 if (cursor_sr > i965_cursor_wm_info.max_wm)
1497 cursor_sr = i965_cursor_wm_info.max_wm;
1498
1499 DRM_DEBUG_KMS("self-refresh watermark: display plane %d "
1500 "cursor %d\n", srwm, cursor_sr);
1501
Imre Deak98584252014-06-13 14:54:20 +03001502 cxsr_enabled = true;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001503 } else {
Imre Deak98584252014-06-13 14:54:20 +03001504 cxsr_enabled = false;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001505 /* Turn off self refresh if both pipes are enabled */
Imre Deak5209b1f2014-07-01 12:36:17 +03001506 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001507 }
1508
1509 DRM_DEBUG_KMS("Setting FIFO watermarks - A: 8, B: 8, C: 8, SR %d\n",
1510 srwm);
1511
1512 /* 965 has limitations... */
Ville Syrjäläf4998962015-03-10 17:02:21 +02001513 I915_WRITE(DSPFW1, FW_WM(srwm, SR) |
1514 FW_WM(8, CURSORB) |
1515 FW_WM(8, PLANEB) |
1516 FW_WM(8, PLANEA));
1517 I915_WRITE(DSPFW2, FW_WM(8, CURSORA) |
1518 FW_WM(8, PLANEC_OLD));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001519 /* update cursor SR watermark */
Ville Syrjäläf4998962015-03-10 17:02:21 +02001520 I915_WRITE(DSPFW3, FW_WM(cursor_sr, CURSOR_SR));
Imre Deak98584252014-06-13 14:54:20 +03001521
1522 if (cxsr_enabled)
1523 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001524}
1525
Ville Syrjäläf4998962015-03-10 17:02:21 +02001526#undef FW_WM
1527
Ville Syrjälä432081b2016-10-31 22:37:03 +02001528static void i9xx_update_wm(struct intel_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001529{
Ville Syrjälä432081b2016-10-31 22:37:03 +02001530 struct drm_device *dev = unused_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01001531 struct drm_i915_private *dev_priv = to_i915(dev);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001532 const struct intel_watermark_params *wm_info;
1533 uint32_t fwater_lo;
1534 uint32_t fwater_hi;
1535 int cwm, srwm = 1;
1536 int fifo_size;
1537 int planea_wm, planeb_wm;
Ville Syrjäläefc26112016-10-31 22:37:04 +02001538 struct intel_crtc *crtc, *enabled = NULL;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001539
1540 if (IS_I945GM(dev))
1541 wm_info = &i945_wm_info;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01001542 else if (!IS_GEN2(dev_priv))
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001543 wm_info = &i915_wm_info;
1544 else
Ville Syrjälä9d539102014-08-15 01:21:53 +03001545 wm_info = &i830_a_wm_info;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001546
1547 fifo_size = dev_priv->display.get_fifo_size(dev, 0);
Ville Syrjäläb91eb5c2016-10-31 22:37:09 +02001548 crtc = intel_get_crtc_for_plane(dev_priv, 0);
Ville Syrjäläefc26112016-10-31 22:37:04 +02001549 if (intel_crtc_active(crtc)) {
1550 const struct drm_display_mode *adjusted_mode =
1551 &crtc->config->base.adjusted_mode;
1552 const struct drm_framebuffer *fb =
1553 crtc->base.primary->state->fb;
1554 int cpp;
1555
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01001556 if (IS_GEN2(dev_priv))
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001557 cpp = 4;
Ville Syrjäläefc26112016-10-31 22:37:04 +02001558 else
1559 cpp = drm_format_plane_cpp(fb->pixel_format, 0);
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001560
Damien Lespiau241bfc32013-09-25 16:45:37 +01001561 planea_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001562 wm_info, fifo_size, cpp,
Chris Wilson5aef6002014-09-03 11:56:07 +01001563 pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001564 enabled = crtc;
Ville Syrjälä9d539102014-08-15 01:21:53 +03001565 } else {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001566 planea_wm = fifo_size - wm_info->guard_size;
Ville Syrjälä9d539102014-08-15 01:21:53 +03001567 if (planea_wm > (long)wm_info->max_wm)
1568 planea_wm = wm_info->max_wm;
1569 }
1570
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01001571 if (IS_GEN2(dev_priv))
Ville Syrjälä9d539102014-08-15 01:21:53 +03001572 wm_info = &i830_bc_wm_info;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001573
1574 fifo_size = dev_priv->display.get_fifo_size(dev, 1);
Ville Syrjäläb91eb5c2016-10-31 22:37:09 +02001575 crtc = intel_get_crtc_for_plane(dev_priv, 1);
Ville Syrjäläefc26112016-10-31 22:37:04 +02001576 if (intel_crtc_active(crtc)) {
1577 const struct drm_display_mode *adjusted_mode =
1578 &crtc->config->base.adjusted_mode;
1579 const struct drm_framebuffer *fb =
1580 crtc->base.primary->state->fb;
1581 int cpp;
1582
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01001583 if (IS_GEN2(dev_priv))
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001584 cpp = 4;
Ville Syrjäläefc26112016-10-31 22:37:04 +02001585 else
1586 cpp = drm_format_plane_cpp(fb->pixel_format, 0);
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001587
Damien Lespiau241bfc32013-09-25 16:45:37 +01001588 planeb_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001589 wm_info, fifo_size, cpp,
Chris Wilson5aef6002014-09-03 11:56:07 +01001590 pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001591 if (enabled == NULL)
1592 enabled = crtc;
1593 else
1594 enabled = NULL;
Ville Syrjälä9d539102014-08-15 01:21:53 +03001595 } else {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001596 planeb_wm = fifo_size - wm_info->guard_size;
Ville Syrjälä9d539102014-08-15 01:21:53 +03001597 if (planeb_wm > (long)wm_info->max_wm)
1598 planeb_wm = wm_info->max_wm;
1599 }
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001600
1601 DRM_DEBUG_KMS("FIFO watermarks - A: %d, B: %d\n", planea_wm, planeb_wm);
1602
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01001603 if (IS_I915GM(dev_priv) && enabled) {
Matt Roper2ff8fde2014-07-08 07:50:07 -07001604 struct drm_i915_gem_object *obj;
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02001605
Ville Syrjäläefc26112016-10-31 22:37:04 +02001606 obj = intel_fb_obj(enabled->base.primary->state->fb);
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02001607
1608 /* self-refresh seems busted with untiled */
Chris Wilson3e510a82016-08-05 10:14:23 +01001609 if (!i915_gem_object_is_tiled(obj))
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02001610 enabled = NULL;
1611 }
1612
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001613 /*
1614 * Overlay gets an aggressive default since video jitter is bad.
1615 */
1616 cwm = 2;
1617
1618 /* Play safe and disable self-refresh before adjusting watermarks. */
Imre Deak5209b1f2014-07-01 12:36:17 +03001619 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001620
1621 /* Calc sr entries for one plane configs */
1622 if (HAS_FW_BLC(dev) && enabled) {
1623 /* self-refresh has much higher latency */
1624 static const int sr_latency_ns = 6000;
Ville Syrjäläefc26112016-10-31 22:37:04 +02001625 const struct drm_display_mode *adjusted_mode =
1626 &enabled->config->base.adjusted_mode;
1627 const struct drm_framebuffer *fb =
1628 enabled->base.primary->state->fb;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001629 int clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -08001630 int htotal = adjusted_mode->crtc_htotal;
Ville Syrjäläefc26112016-10-31 22:37:04 +02001631 int hdisplay = enabled->config->pipe_src_w;
1632 int cpp;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001633 unsigned long line_time_us;
1634 int entries;
1635
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01001636 if (IS_I915GM(dev_priv) || IS_I945GM(dev_priv))
Ville Syrjälä2d1b5052016-07-29 17:57:01 +03001637 cpp = 4;
Ville Syrjäläefc26112016-10-31 22:37:04 +02001638 else
1639 cpp = drm_format_plane_cpp(fb->pixel_format, 0);
Ville Syrjälä2d1b5052016-07-29 17:57:01 +03001640
Ville Syrjälä922044c2014-02-14 14:18:57 +02001641 line_time_us = max(htotal * 1000 / clock, 1);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001642
1643 /* Use ns/us then divide to preserve precision */
1644 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
Ville Syrjäläac484962016-01-20 21:05:26 +02001645 cpp * hdisplay;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001646 entries = DIV_ROUND_UP(entries, wm_info->cacheline_size);
1647 DRM_DEBUG_KMS("self-refresh entries: %d\n", entries);
1648 srwm = wm_info->fifo_size - entries;
1649 if (srwm < 0)
1650 srwm = 1;
1651
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01001652 if (IS_I945G(dev_priv) || IS_I945GM(dev_priv))
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001653 I915_WRITE(FW_BLC_SELF,
1654 FW_BLC_SELF_FIFO_MASK | (srwm & 0xff));
Ville Syrjäläacb91352016-07-29 17:57:02 +03001655 else
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001656 I915_WRITE(FW_BLC_SELF, srwm & 0x3f);
1657 }
1658
1659 DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d, B: %d, C: %d, SR %d\n",
1660 planea_wm, planeb_wm, cwm, srwm);
1661
1662 fwater_lo = ((planeb_wm & 0x3f) << 16) | (planea_wm & 0x3f);
1663 fwater_hi = (cwm & 0x1f);
1664
1665 /* Set request length to 8 cachelines per fetch */
1666 fwater_lo = fwater_lo | (1 << 24) | (1 << 8);
1667 fwater_hi = fwater_hi | (1 << 8);
1668
1669 I915_WRITE(FW_BLC, fwater_lo);
1670 I915_WRITE(FW_BLC2, fwater_hi);
1671
Imre Deak5209b1f2014-07-01 12:36:17 +03001672 if (enabled)
1673 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001674}
1675
Ville Syrjälä432081b2016-10-31 22:37:03 +02001676static void i845_update_wm(struct intel_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001677{
Ville Syrjälä432081b2016-10-31 22:37:03 +02001678 struct drm_device *dev = unused_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01001679 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläefc26112016-10-31 22:37:04 +02001680 struct intel_crtc *crtc;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001681 const struct drm_display_mode *adjusted_mode;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001682 uint32_t fwater_lo;
1683 int planea_wm;
1684
1685 crtc = single_enabled_crtc(dev);
1686 if (crtc == NULL)
1687 return;
1688
Ville Syrjäläefc26112016-10-31 22:37:04 +02001689 adjusted_mode = &crtc->config->base.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001690 planea_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Daniel Vetterfeb56b92013-12-14 20:38:30 -02001691 &i845_wm_info,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001692 dev_priv->display.get_fifo_size(dev, 0),
Chris Wilson5aef6002014-09-03 11:56:07 +01001693 4, pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001694 fwater_lo = I915_READ(FW_BLC) & ~0xfff;
1695 fwater_lo |= (3<<8) | planea_wm;
1696
1697 DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d\n", planea_wm);
1698
1699 I915_WRITE(FW_BLC, fwater_lo);
1700}
1701
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03001702uint32_t ilk_pipe_pixel_rate(const struct intel_crtc_state *pipe_config)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001703{
Chris Wilsonfd4daa92013-08-27 17:04:17 +01001704 uint32_t pixel_rate;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001705
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03001706 pixel_rate = pipe_config->base.adjusted_mode.crtc_clock;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001707
1708 /* We only use IF-ID interlacing. If we ever use PF-ID we'll need to
1709 * adjust the pixel_rate here. */
1710
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03001711 if (pipe_config->pch_pfit.enabled) {
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001712 uint64_t pipe_w, pipe_h, pfit_w, pfit_h;
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03001713 uint32_t pfit_size = pipe_config->pch_pfit.size;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001714
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03001715 pipe_w = pipe_config->pipe_src_w;
1716 pipe_h = pipe_config->pipe_src_h;
1717
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001718 pfit_w = (pfit_size >> 16) & 0xFFFF;
1719 pfit_h = pfit_size & 0xFFFF;
1720 if (pipe_w < pfit_w)
1721 pipe_w = pfit_w;
1722 if (pipe_h < pfit_h)
1723 pipe_h = pfit_h;
1724
Matt Roper15126882015-12-03 11:37:40 -08001725 if (WARN_ON(!pfit_w || !pfit_h))
1726 return pixel_rate;
1727
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001728 pixel_rate = div_u64((uint64_t) pixel_rate * pipe_w * pipe_h,
1729 pfit_w * pfit_h);
1730 }
1731
1732 return pixel_rate;
1733}
1734
Ville Syrjälä37126462013-08-01 16:18:55 +03001735/* latency must be in 0.1us units. */
Ville Syrjäläac484962016-01-20 21:05:26 +02001736static uint32_t ilk_wm_method1(uint32_t pixel_rate, uint8_t cpp, uint32_t latency)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001737{
1738 uint64_t ret;
1739
Ville Syrjälä3312ba62013-08-01 16:18:53 +03001740 if (WARN(latency == 0, "Latency value missing\n"))
1741 return UINT_MAX;
1742
Ville Syrjäläac484962016-01-20 21:05:26 +02001743 ret = (uint64_t) pixel_rate * cpp * latency;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001744 ret = DIV_ROUND_UP_ULL(ret, 64 * 10000) + 2;
1745
1746 return ret;
1747}
1748
Ville Syrjälä37126462013-08-01 16:18:55 +03001749/* latency must be in 0.1us units. */
Ville Syrjälä23297042013-07-05 11:57:17 +03001750static uint32_t ilk_wm_method2(uint32_t pixel_rate, uint32_t pipe_htotal,
Ville Syrjäläac484962016-01-20 21:05:26 +02001751 uint32_t horiz_pixels, uint8_t cpp,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001752 uint32_t latency)
1753{
1754 uint32_t ret;
1755
Ville Syrjälä3312ba62013-08-01 16:18:53 +03001756 if (WARN(latency == 0, "Latency value missing\n"))
1757 return UINT_MAX;
Matt Roper15126882015-12-03 11:37:40 -08001758 if (WARN_ON(!pipe_htotal))
1759 return UINT_MAX;
Ville Syrjälä3312ba62013-08-01 16:18:53 +03001760
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001761 ret = (latency * pixel_rate) / (pipe_htotal * 10000);
Ville Syrjäläac484962016-01-20 21:05:26 +02001762 ret = (ret + 1) * horiz_pixels * cpp;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001763 ret = DIV_ROUND_UP(ret, 64) + 2;
1764 return ret;
1765}
1766
Ville Syrjälä23297042013-07-05 11:57:17 +03001767static uint32_t ilk_wm_fbc(uint32_t pri_val, uint32_t horiz_pixels,
Ville Syrjäläac484962016-01-20 21:05:26 +02001768 uint8_t cpp)
Paulo Zanonicca32e92013-05-31 11:45:06 -03001769{
Matt Roper15126882015-12-03 11:37:40 -08001770 /*
1771 * Neither of these should be possible since this function shouldn't be
1772 * called if the CRTC is off or the plane is invisible. But let's be
1773 * extra paranoid to avoid a potential divide-by-zero if we screw up
1774 * elsewhere in the driver.
1775 */
Ville Syrjäläac484962016-01-20 21:05:26 +02001776 if (WARN_ON(!cpp))
Matt Roper15126882015-12-03 11:37:40 -08001777 return 0;
1778 if (WARN_ON(!horiz_pixels))
1779 return 0;
1780
Ville Syrjäläac484962016-01-20 21:05:26 +02001781 return DIV_ROUND_UP(pri_val * 64, horiz_pixels * cpp) + 2;
Paulo Zanonicca32e92013-05-31 11:45:06 -03001782}
1783
Imre Deak820c1982013-12-17 14:46:36 +02001784struct ilk_wm_maximums {
Paulo Zanonicca32e92013-05-31 11:45:06 -03001785 uint16_t pri;
1786 uint16_t spr;
1787 uint16_t cur;
1788 uint16_t fbc;
1789};
1790
Ville Syrjälä37126462013-08-01 16:18:55 +03001791/*
1792 * For both WM_PIPE and WM_LP.
1793 * mem_value must be in 0.1us units.
1794 */
Matt Roper7221fc32015-09-24 15:53:08 -07001795static uint32_t ilk_compute_pri_wm(const struct intel_crtc_state *cstate,
Matt Roper43d59ed2015-09-24 15:53:07 -07001796 const struct intel_plane_state *pstate,
Paulo Zanonicca32e92013-05-31 11:45:06 -03001797 uint32_t mem_value,
1798 bool is_lp)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001799{
Ville Syrjäläac484962016-01-20 21:05:26 +02001800 int cpp = pstate->base.fb ?
1801 drm_format_plane_cpp(pstate->base.fb->pixel_format, 0) : 0;
Paulo Zanonicca32e92013-05-31 11:45:06 -03001802 uint32_t method1, method2;
1803
Ville Syrjälä936e71e2016-07-26 19:06:59 +03001804 if (!cstate->base.active || !pstate->base.visible)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001805 return 0;
1806
Ville Syrjäläac484962016-01-20 21:05:26 +02001807 method1 = ilk_wm_method1(ilk_pipe_pixel_rate(cstate), cpp, mem_value);
Paulo Zanonicca32e92013-05-31 11:45:06 -03001808
1809 if (!is_lp)
1810 return method1;
1811
Matt Roper7221fc32015-09-24 15:53:08 -07001812 method2 = ilk_wm_method2(ilk_pipe_pixel_rate(cstate),
1813 cstate->base.adjusted_mode.crtc_htotal,
Ville Syrjälä936e71e2016-07-26 19:06:59 +03001814 drm_rect_width(&pstate->base.dst),
Ville Syrjäläac484962016-01-20 21:05:26 +02001815 cpp, mem_value);
Paulo Zanonicca32e92013-05-31 11:45:06 -03001816
1817 return min(method1, method2);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001818}
1819
Ville Syrjälä37126462013-08-01 16:18:55 +03001820/*
1821 * For both WM_PIPE and WM_LP.
1822 * mem_value must be in 0.1us units.
1823 */
Matt Roper7221fc32015-09-24 15:53:08 -07001824static uint32_t ilk_compute_spr_wm(const struct intel_crtc_state *cstate,
Matt Roper43d59ed2015-09-24 15:53:07 -07001825 const struct intel_plane_state *pstate,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001826 uint32_t mem_value)
1827{
Ville Syrjäläac484962016-01-20 21:05:26 +02001828 int cpp = pstate->base.fb ?
1829 drm_format_plane_cpp(pstate->base.fb->pixel_format, 0) : 0;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001830 uint32_t method1, method2;
1831
Ville Syrjälä936e71e2016-07-26 19:06:59 +03001832 if (!cstate->base.active || !pstate->base.visible)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001833 return 0;
1834
Ville Syrjäläac484962016-01-20 21:05:26 +02001835 method1 = ilk_wm_method1(ilk_pipe_pixel_rate(cstate), cpp, mem_value);
Matt Roper7221fc32015-09-24 15:53:08 -07001836 method2 = ilk_wm_method2(ilk_pipe_pixel_rate(cstate),
1837 cstate->base.adjusted_mode.crtc_htotal,
Ville Syrjälä936e71e2016-07-26 19:06:59 +03001838 drm_rect_width(&pstate->base.dst),
Ville Syrjäläac484962016-01-20 21:05:26 +02001839 cpp, mem_value);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001840 return min(method1, method2);
1841}
1842
Ville Syrjälä37126462013-08-01 16:18:55 +03001843/*
1844 * For both WM_PIPE and WM_LP.
1845 * mem_value must be in 0.1us units.
1846 */
Matt Roper7221fc32015-09-24 15:53:08 -07001847static uint32_t ilk_compute_cur_wm(const struct intel_crtc_state *cstate,
Matt Roper43d59ed2015-09-24 15:53:07 -07001848 const struct intel_plane_state *pstate,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001849 uint32_t mem_value)
1850{
Matt Roperb2435692016-02-02 22:06:51 -08001851 /*
1852 * We treat the cursor plane as always-on for the purposes of watermark
1853 * calculation. Until we have two-stage watermark programming merged,
1854 * this is necessary to avoid flickering.
1855 */
1856 int cpp = 4;
Ville Syrjälä936e71e2016-07-26 19:06:59 +03001857 int width = pstate->base.visible ? pstate->base.crtc_w : 64;
Matt Roper43d59ed2015-09-24 15:53:07 -07001858
Matt Roperb2435692016-02-02 22:06:51 -08001859 if (!cstate->base.active)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001860 return 0;
1861
Matt Roper7221fc32015-09-24 15:53:08 -07001862 return ilk_wm_method2(ilk_pipe_pixel_rate(cstate),
1863 cstate->base.adjusted_mode.crtc_htotal,
Matt Roperb2435692016-02-02 22:06:51 -08001864 width, cpp, mem_value);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001865}
1866
Paulo Zanonicca32e92013-05-31 11:45:06 -03001867/* Only for WM_LP. */
Matt Roper7221fc32015-09-24 15:53:08 -07001868static uint32_t ilk_compute_fbc_wm(const struct intel_crtc_state *cstate,
Matt Roper43d59ed2015-09-24 15:53:07 -07001869 const struct intel_plane_state *pstate,
Ville Syrjälä1fda9882013-07-05 11:57:19 +03001870 uint32_t pri_val)
Paulo Zanonicca32e92013-05-31 11:45:06 -03001871{
Ville Syrjäläac484962016-01-20 21:05:26 +02001872 int cpp = pstate->base.fb ?
1873 drm_format_plane_cpp(pstate->base.fb->pixel_format, 0) : 0;
Matt Roper43d59ed2015-09-24 15:53:07 -07001874
Ville Syrjälä936e71e2016-07-26 19:06:59 +03001875 if (!cstate->base.active || !pstate->base.visible)
Paulo Zanonicca32e92013-05-31 11:45:06 -03001876 return 0;
1877
Ville Syrjälä936e71e2016-07-26 19:06:59 +03001878 return ilk_wm_fbc(pri_val, drm_rect_width(&pstate->base.dst), cpp);
Paulo Zanonicca32e92013-05-31 11:45:06 -03001879}
1880
Ville Syrjälä158ae642013-08-07 13:28:19 +03001881static unsigned int ilk_display_fifo_size(const struct drm_device *dev)
1882{
Ville Syrjälä416f4722013-11-02 21:07:46 -07001883 if (INTEL_INFO(dev)->gen >= 8)
1884 return 3072;
1885 else if (INTEL_INFO(dev)->gen >= 7)
Ville Syrjälä158ae642013-08-07 13:28:19 +03001886 return 768;
1887 else
1888 return 512;
1889}
1890
Ville Syrjälä4e975082014-03-07 18:32:11 +02001891static unsigned int ilk_plane_wm_reg_max(const struct drm_device *dev,
1892 int level, bool is_sprite)
1893{
1894 if (INTEL_INFO(dev)->gen >= 8)
1895 /* BDW primary/sprite plane watermarks */
1896 return level == 0 ? 255 : 2047;
1897 else if (INTEL_INFO(dev)->gen >= 7)
1898 /* IVB/HSW primary/sprite plane watermarks */
1899 return level == 0 ? 127 : 1023;
1900 else if (!is_sprite)
1901 /* ILK/SNB primary plane watermarks */
1902 return level == 0 ? 127 : 511;
1903 else
1904 /* ILK/SNB sprite plane watermarks */
1905 return level == 0 ? 63 : 255;
1906}
1907
1908static unsigned int ilk_cursor_wm_reg_max(const struct drm_device *dev,
1909 int level)
1910{
1911 if (INTEL_INFO(dev)->gen >= 7)
1912 return level == 0 ? 63 : 255;
1913 else
1914 return level == 0 ? 31 : 63;
1915}
1916
1917static unsigned int ilk_fbc_wm_reg_max(const struct drm_device *dev)
1918{
1919 if (INTEL_INFO(dev)->gen >= 8)
1920 return 31;
1921 else
1922 return 15;
1923}
1924
Ville Syrjälä158ae642013-08-07 13:28:19 +03001925/* Calculate the maximum primary/sprite plane watermark */
1926static unsigned int ilk_plane_wm_max(const struct drm_device *dev,
1927 int level,
Ville Syrjälä240264f2013-08-07 13:29:12 +03001928 const struct intel_wm_config *config,
Ville Syrjälä158ae642013-08-07 13:28:19 +03001929 enum intel_ddb_partitioning ddb_partitioning,
1930 bool is_sprite)
1931{
1932 unsigned int fifo_size = ilk_display_fifo_size(dev);
Ville Syrjälä158ae642013-08-07 13:28:19 +03001933
1934 /* if sprites aren't enabled, sprites get nothing */
Ville Syrjälä240264f2013-08-07 13:29:12 +03001935 if (is_sprite && !config->sprites_enabled)
Ville Syrjälä158ae642013-08-07 13:28:19 +03001936 return 0;
1937
1938 /* HSW allows LP1+ watermarks even with multiple pipes */
Ville Syrjälä240264f2013-08-07 13:29:12 +03001939 if (level == 0 || config->num_pipes_active > 1) {
Ville Syrjälä158ae642013-08-07 13:28:19 +03001940 fifo_size /= INTEL_INFO(dev)->num_pipes;
1941
1942 /*
1943 * For some reason the non self refresh
1944 * FIFO size is only half of the self
1945 * refresh FIFO size on ILK/SNB.
1946 */
1947 if (INTEL_INFO(dev)->gen <= 6)
1948 fifo_size /= 2;
1949 }
1950
Ville Syrjälä240264f2013-08-07 13:29:12 +03001951 if (config->sprites_enabled) {
Ville Syrjälä158ae642013-08-07 13:28:19 +03001952 /* level 0 is always calculated with 1:1 split */
1953 if (level > 0 && ddb_partitioning == INTEL_DDB_PART_5_6) {
1954 if (is_sprite)
1955 fifo_size *= 5;
1956 fifo_size /= 6;
1957 } else {
1958 fifo_size /= 2;
1959 }
1960 }
1961
1962 /* clamp to max that the registers can hold */
Ville Syrjälä4e975082014-03-07 18:32:11 +02001963 return min(fifo_size, ilk_plane_wm_reg_max(dev, level, is_sprite));
Ville Syrjälä158ae642013-08-07 13:28:19 +03001964}
1965
1966/* Calculate the maximum cursor plane watermark */
1967static unsigned int ilk_cursor_wm_max(const struct drm_device *dev,
Ville Syrjälä240264f2013-08-07 13:29:12 +03001968 int level,
1969 const struct intel_wm_config *config)
Ville Syrjälä158ae642013-08-07 13:28:19 +03001970{
1971 /* HSW LP1+ watermarks w/ multiple pipes */
Ville Syrjälä240264f2013-08-07 13:29:12 +03001972 if (level > 0 && config->num_pipes_active > 1)
Ville Syrjälä158ae642013-08-07 13:28:19 +03001973 return 64;
1974
1975 /* otherwise just report max that registers can hold */
Ville Syrjälä4e975082014-03-07 18:32:11 +02001976 return ilk_cursor_wm_reg_max(dev, level);
Ville Syrjälä158ae642013-08-07 13:28:19 +03001977}
1978
Damien Lespiaud34ff9c2014-01-06 19:17:23 +00001979static void ilk_compute_wm_maximums(const struct drm_device *dev,
Ville Syrjälä34982fe2013-10-09 19:18:09 +03001980 int level,
1981 const struct intel_wm_config *config,
1982 enum intel_ddb_partitioning ddb_partitioning,
Imre Deak820c1982013-12-17 14:46:36 +02001983 struct ilk_wm_maximums *max)
Ville Syrjälä158ae642013-08-07 13:28:19 +03001984{
Ville Syrjälä240264f2013-08-07 13:29:12 +03001985 max->pri = ilk_plane_wm_max(dev, level, config, ddb_partitioning, false);
1986 max->spr = ilk_plane_wm_max(dev, level, config, ddb_partitioning, true);
1987 max->cur = ilk_cursor_wm_max(dev, level, config);
Ville Syrjälä4e975082014-03-07 18:32:11 +02001988 max->fbc = ilk_fbc_wm_reg_max(dev);
Ville Syrjälä158ae642013-08-07 13:28:19 +03001989}
1990
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03001991static void ilk_compute_wm_reg_maximums(struct drm_device *dev,
1992 int level,
1993 struct ilk_wm_maximums *max)
1994{
1995 max->pri = ilk_plane_wm_reg_max(dev, level, false);
1996 max->spr = ilk_plane_wm_reg_max(dev, level, true);
1997 max->cur = ilk_cursor_wm_reg_max(dev, level);
1998 max->fbc = ilk_fbc_wm_reg_max(dev);
1999}
2000
Ville Syrjäläd9395652013-10-09 19:18:10 +03002001static bool ilk_validate_wm_level(int level,
Imre Deak820c1982013-12-17 14:46:36 +02002002 const struct ilk_wm_maximums *max,
Ville Syrjäläd9395652013-10-09 19:18:10 +03002003 struct intel_wm_level *result)
Ville Syrjäläa9786a12013-08-07 13:24:47 +03002004{
2005 bool ret;
2006
2007 /* already determined to be invalid? */
2008 if (!result->enable)
2009 return false;
2010
2011 result->enable = result->pri_val <= max->pri &&
2012 result->spr_val <= max->spr &&
2013 result->cur_val <= max->cur;
2014
2015 ret = result->enable;
2016
2017 /*
2018 * HACK until we can pre-compute everything,
2019 * and thus fail gracefully if LP0 watermarks
2020 * are exceeded...
2021 */
2022 if (level == 0 && !result->enable) {
2023 if (result->pri_val > max->pri)
2024 DRM_DEBUG_KMS("Primary WM%d too large %u (max %u)\n",
2025 level, result->pri_val, max->pri);
2026 if (result->spr_val > max->spr)
2027 DRM_DEBUG_KMS("Sprite WM%d too large %u (max %u)\n",
2028 level, result->spr_val, max->spr);
2029 if (result->cur_val > max->cur)
2030 DRM_DEBUG_KMS("Cursor WM%d too large %u (max %u)\n",
2031 level, result->cur_val, max->cur);
2032
2033 result->pri_val = min_t(uint32_t, result->pri_val, max->pri);
2034 result->spr_val = min_t(uint32_t, result->spr_val, max->spr);
2035 result->cur_val = min_t(uint32_t, result->cur_val, max->cur);
2036 result->enable = true;
2037 }
2038
Ville Syrjäläa9786a12013-08-07 13:24:47 +03002039 return ret;
2040}
2041
Damien Lespiaud34ff9c2014-01-06 19:17:23 +00002042static void ilk_compute_wm_level(const struct drm_i915_private *dev_priv,
Matt Roper43d59ed2015-09-24 15:53:07 -07002043 const struct intel_crtc *intel_crtc,
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03002044 int level,
Matt Roper7221fc32015-09-24 15:53:08 -07002045 struct intel_crtc_state *cstate,
Matt Roper86c8bbb2015-09-24 15:53:16 -07002046 struct intel_plane_state *pristate,
2047 struct intel_plane_state *sprstate,
2048 struct intel_plane_state *curstate,
Ville Syrjälä1fd527c2013-08-06 22:24:05 +03002049 struct intel_wm_level *result)
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03002050{
2051 uint16_t pri_latency = dev_priv->wm.pri_latency[level];
2052 uint16_t spr_latency = dev_priv->wm.spr_latency[level];
2053 uint16_t cur_latency = dev_priv->wm.cur_latency[level];
2054
2055 /* WM1+ latency values stored in 0.5us units */
2056 if (level > 0) {
2057 pri_latency *= 5;
2058 spr_latency *= 5;
2059 cur_latency *= 5;
2060 }
2061
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002062 if (pristate) {
2063 result->pri_val = ilk_compute_pri_wm(cstate, pristate,
2064 pri_latency, level);
2065 result->fbc_val = ilk_compute_fbc_wm(cstate, pristate, result->pri_val);
2066 }
2067
2068 if (sprstate)
2069 result->spr_val = ilk_compute_spr_wm(cstate, sprstate, spr_latency);
2070
2071 if (curstate)
2072 result->cur_val = ilk_compute_cur_wm(cstate, curstate, cur_latency);
2073
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03002074 result->enable = true;
2075}
2076
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002077static uint32_t
Ville Syrjälä532f7a72016-04-29 17:31:17 +03002078hsw_compute_linetime_wm(const struct intel_crtc_state *cstate)
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002079{
Ville Syrjälä532f7a72016-04-29 17:31:17 +03002080 const struct intel_atomic_state *intel_state =
2081 to_intel_atomic_state(cstate->base.state);
Matt Roperee91a152015-12-03 11:37:39 -08002082 const struct drm_display_mode *adjusted_mode =
2083 &cstate->base.adjusted_mode;
Paulo Zanoni85a02de2013-05-03 17:23:43 -03002084 u32 linetime, ips_linetime;
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002085
Matt Roperee91a152015-12-03 11:37:39 -08002086 if (!cstate->base.active)
2087 return 0;
2088 if (WARN_ON(adjusted_mode->crtc_clock == 0))
2089 return 0;
Ville Syrjälä532f7a72016-04-29 17:31:17 +03002090 if (WARN_ON(intel_state->cdclk == 0))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002091 return 0;
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002092
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002093 /* The WM are computed with base on how long it takes to fill a single
2094 * row at the given clock rate, multiplied by 8.
2095 * */
Ville Syrjälä124abe02015-09-08 13:40:45 +03002096 linetime = DIV_ROUND_CLOSEST(adjusted_mode->crtc_htotal * 1000 * 8,
2097 adjusted_mode->crtc_clock);
2098 ips_linetime = DIV_ROUND_CLOSEST(adjusted_mode->crtc_htotal * 1000 * 8,
Ville Syrjälä532f7a72016-04-29 17:31:17 +03002099 intel_state->cdclk);
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002100
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002101 return PIPE_WM_LINETIME_IPS_LINETIME(ips_linetime) |
2102 PIPE_WM_LINETIME_TIME(linetime);
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002103}
2104
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002105static void intel_read_wm_latency(struct drm_device *dev, uint16_t wm[8])
Ville Syrjälä12b134d2013-07-05 11:57:21 +03002106{
Chris Wilsonfac5e232016-07-04 11:34:36 +01002107 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä12b134d2013-07-05 11:57:21 +03002108
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002109 if (IS_GEN9(dev_priv)) {
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002110 uint32_t val;
Vandana Kannan4f947382014-11-04 17:06:47 +00002111 int ret, i;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002112 int level, max_level = ilk_wm_max_level(dev_priv);
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002113
2114 /* read the first set of memory latencies[0:3] */
2115 val = 0; /* data0 to be programmed to 0 for first set */
2116 mutex_lock(&dev_priv->rps.hw_lock);
2117 ret = sandybridge_pcode_read(dev_priv,
2118 GEN9_PCODE_READ_MEM_LATENCY,
2119 &val);
2120 mutex_unlock(&dev_priv->rps.hw_lock);
2121
2122 if (ret) {
2123 DRM_ERROR("SKL Mailbox read error = %d\n", ret);
2124 return;
2125 }
2126
2127 wm[0] = val & GEN9_MEM_LATENCY_LEVEL_MASK;
2128 wm[1] = (val >> GEN9_MEM_LATENCY_LEVEL_1_5_SHIFT) &
2129 GEN9_MEM_LATENCY_LEVEL_MASK;
2130 wm[2] = (val >> GEN9_MEM_LATENCY_LEVEL_2_6_SHIFT) &
2131 GEN9_MEM_LATENCY_LEVEL_MASK;
2132 wm[3] = (val >> GEN9_MEM_LATENCY_LEVEL_3_7_SHIFT) &
2133 GEN9_MEM_LATENCY_LEVEL_MASK;
2134
2135 /* read the second set of memory latencies[4:7] */
2136 val = 1; /* data0 to be programmed to 1 for second set */
2137 mutex_lock(&dev_priv->rps.hw_lock);
2138 ret = sandybridge_pcode_read(dev_priv,
2139 GEN9_PCODE_READ_MEM_LATENCY,
2140 &val);
2141 mutex_unlock(&dev_priv->rps.hw_lock);
2142 if (ret) {
2143 DRM_ERROR("SKL Mailbox read error = %d\n", ret);
2144 return;
2145 }
2146
2147 wm[4] = val & GEN9_MEM_LATENCY_LEVEL_MASK;
2148 wm[5] = (val >> GEN9_MEM_LATENCY_LEVEL_1_5_SHIFT) &
2149 GEN9_MEM_LATENCY_LEVEL_MASK;
2150 wm[6] = (val >> GEN9_MEM_LATENCY_LEVEL_2_6_SHIFT) &
2151 GEN9_MEM_LATENCY_LEVEL_MASK;
2152 wm[7] = (val >> GEN9_MEM_LATENCY_LEVEL_3_7_SHIFT) &
2153 GEN9_MEM_LATENCY_LEVEL_MASK;
2154
Vandana Kannan367294b2014-11-04 17:06:46 +00002155 /*
Paulo Zanoni0727e402016-09-22 18:00:30 -03002156 * If a level n (n > 1) has a 0us latency, all levels m (m >= n)
2157 * need to be disabled. We make sure to sanitize the values out
2158 * of the punit to satisfy this requirement.
2159 */
2160 for (level = 1; level <= max_level; level++) {
2161 if (wm[level] == 0) {
2162 for (i = level + 1; i <= max_level; i++)
2163 wm[i] = 0;
2164 break;
2165 }
2166 }
2167
2168 /*
Damien Lespiau6f972352015-02-09 19:33:07 +00002169 * WaWmMemoryReadLatency:skl
2170 *
Vandana Kannan367294b2014-11-04 17:06:46 +00002171 * punit doesn't take into account the read latency so we need
Paulo Zanoni0727e402016-09-22 18:00:30 -03002172 * to add 2us to the various latency levels we retrieve from the
2173 * punit when level 0 response data us 0us.
Vandana Kannan367294b2014-11-04 17:06:46 +00002174 */
Paulo Zanoni0727e402016-09-22 18:00:30 -03002175 if (wm[0] == 0) {
2176 wm[0] += 2;
2177 for (level = 1; level <= max_level; level++) {
2178 if (wm[level] == 0)
2179 break;
Vandana Kannan367294b2014-11-04 17:06:46 +00002180 wm[level] += 2;
Vandana Kannan4f947382014-11-04 17:06:47 +00002181 }
Paulo Zanoni0727e402016-09-22 18:00:30 -03002182 }
2183
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002184 } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
Ville Syrjälä12b134d2013-07-05 11:57:21 +03002185 uint64_t sskpd = I915_READ64(MCH_SSKPD);
2186
2187 wm[0] = (sskpd >> 56) & 0xFF;
2188 if (wm[0] == 0)
2189 wm[0] = sskpd & 0xF;
Ville Syrjäläe5d50192013-07-05 11:57:22 +03002190 wm[1] = (sskpd >> 4) & 0xFF;
2191 wm[2] = (sskpd >> 12) & 0xFF;
2192 wm[3] = (sskpd >> 20) & 0x1FF;
2193 wm[4] = (sskpd >> 32) & 0x1FF;
Ville Syrjälä63cf9a12013-07-05 11:57:23 +03002194 } else if (INTEL_INFO(dev)->gen >= 6) {
2195 uint32_t sskpd = I915_READ(MCH_SSKPD);
2196
2197 wm[0] = (sskpd >> SSKPD_WM0_SHIFT) & SSKPD_WM_MASK;
2198 wm[1] = (sskpd >> SSKPD_WM1_SHIFT) & SSKPD_WM_MASK;
2199 wm[2] = (sskpd >> SSKPD_WM2_SHIFT) & SSKPD_WM_MASK;
2200 wm[3] = (sskpd >> SSKPD_WM3_SHIFT) & SSKPD_WM_MASK;
Ville Syrjälä3a88d0a2013-08-01 16:18:49 +03002201 } else if (INTEL_INFO(dev)->gen >= 5) {
2202 uint32_t mltr = I915_READ(MLTR_ILK);
2203
2204 /* ILK primary LP0 latency is 700 ns */
2205 wm[0] = 7;
2206 wm[1] = (mltr >> MLTR_WM1_SHIFT) & ILK_SRLT_MASK;
2207 wm[2] = (mltr >> MLTR_WM2_SHIFT) & ILK_SRLT_MASK;
Ville Syrjälä12b134d2013-07-05 11:57:21 +03002208 }
2209}
2210
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002211static void intel_fixup_spr_wm_latency(struct drm_i915_private *dev_priv,
2212 uint16_t wm[5])
Ville Syrjälä53615a52013-08-01 16:18:50 +03002213{
2214 /* ILK sprite LP0 latency is 1300 ns */
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002215 if (IS_GEN5(dev_priv))
Ville Syrjälä53615a52013-08-01 16:18:50 +03002216 wm[0] = 13;
2217}
2218
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01002219static void intel_fixup_cur_wm_latency(struct drm_i915_private *dev_priv,
2220 uint16_t wm[5])
Ville Syrjälä53615a52013-08-01 16:18:50 +03002221{
2222 /* ILK cursor LP0 latency is 1300 ns */
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01002223 if (IS_GEN5(dev_priv))
Ville Syrjälä53615a52013-08-01 16:18:50 +03002224 wm[0] = 13;
2225
2226 /* WaDoubleCursorLP3Latency:ivb */
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01002227 if (IS_IVYBRIDGE(dev_priv))
Ville Syrjälä53615a52013-08-01 16:18:50 +03002228 wm[3] *= 2;
2229}
2230
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002231int ilk_wm_max_level(const struct drm_i915_private *dev_priv)
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03002232{
2233 /* how many WM levels are we expecting */
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002234 if (INTEL_GEN(dev_priv) >= 9)
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002235 return 7;
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002236 else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03002237 return 4;
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002238 else if (INTEL_GEN(dev_priv) >= 6)
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03002239 return 3;
2240 else
2241 return 2;
2242}
Daniel Vetter7526ed72014-09-29 15:07:19 +02002243
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002244static void intel_print_wm_latency(struct drm_i915_private *dev_priv,
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002245 const char *name,
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002246 const uint16_t wm[8])
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002247{
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002248 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002249
2250 for (level = 0; level <= max_level; level++) {
2251 unsigned int latency = wm[level];
2252
2253 if (latency == 0) {
2254 DRM_ERROR("%s WM%d latency not provided\n",
2255 name, level);
2256 continue;
2257 }
2258
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002259 /*
2260 * - latencies are in us on gen9.
2261 * - before then, WM1+ latency values are in 0.5us units
2262 */
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002263 if (IS_GEN9(dev_priv))
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002264 latency *= 10;
2265 else if (level > 0)
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002266 latency *= 5;
2267
2268 DRM_DEBUG_KMS("%s WM%d latency %u (%u.%u usec)\n",
2269 name, level, wm[level],
2270 latency / 10, latency % 10);
2271 }
2272}
2273
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002274static bool ilk_increase_wm_latency(struct drm_i915_private *dev_priv,
2275 uint16_t wm[5], uint16_t min)
2276{
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002277 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002278
2279 if (wm[0] >= min)
2280 return false;
2281
2282 wm[0] = max(wm[0], min);
2283 for (level = 1; level <= max_level; level++)
2284 wm[level] = max_t(uint16_t, wm[level], DIV_ROUND_UP(min, 5));
2285
2286 return true;
2287}
2288
2289static void snb_wm_latency_quirk(struct drm_device *dev)
2290{
Chris Wilsonfac5e232016-07-04 11:34:36 +01002291 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002292 bool changed;
2293
2294 /*
2295 * The BIOS provided WM memory latency values are often
2296 * inadequate for high resolution displays. Adjust them.
2297 */
2298 changed = ilk_increase_wm_latency(dev_priv, dev_priv->wm.pri_latency, 12) |
2299 ilk_increase_wm_latency(dev_priv, dev_priv->wm.spr_latency, 12) |
2300 ilk_increase_wm_latency(dev_priv, dev_priv->wm.cur_latency, 12);
2301
2302 if (!changed)
2303 return;
2304
2305 DRM_DEBUG_KMS("WM latency values increased to avoid potential underruns\n");
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002306 intel_print_wm_latency(dev_priv, "Primary", dev_priv->wm.pri_latency);
2307 intel_print_wm_latency(dev_priv, "Sprite", dev_priv->wm.spr_latency);
2308 intel_print_wm_latency(dev_priv, "Cursor", dev_priv->wm.cur_latency);
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002309}
2310
Damien Lespiaufa50ad62014-03-17 18:01:16 +00002311static void ilk_setup_wm_latency(struct drm_device *dev)
Ville Syrjälä53615a52013-08-01 16:18:50 +03002312{
Chris Wilsonfac5e232016-07-04 11:34:36 +01002313 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä53615a52013-08-01 16:18:50 +03002314
2315 intel_read_wm_latency(dev, dev_priv->wm.pri_latency);
2316
2317 memcpy(dev_priv->wm.spr_latency, dev_priv->wm.pri_latency,
2318 sizeof(dev_priv->wm.pri_latency));
2319 memcpy(dev_priv->wm.cur_latency, dev_priv->wm.pri_latency,
2320 sizeof(dev_priv->wm.pri_latency));
2321
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002322 intel_fixup_spr_wm_latency(dev_priv, dev_priv->wm.spr_latency);
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01002323 intel_fixup_cur_wm_latency(dev_priv, dev_priv->wm.cur_latency);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002324
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002325 intel_print_wm_latency(dev_priv, "Primary", dev_priv->wm.pri_latency);
2326 intel_print_wm_latency(dev_priv, "Sprite", dev_priv->wm.spr_latency);
2327 intel_print_wm_latency(dev_priv, "Cursor", dev_priv->wm.cur_latency);
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002328
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002329 if (IS_GEN6(dev_priv))
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002330 snb_wm_latency_quirk(dev);
Ville Syrjälä53615a52013-08-01 16:18:50 +03002331}
2332
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002333static void skl_setup_wm_latency(struct drm_device *dev)
2334{
Chris Wilsonfac5e232016-07-04 11:34:36 +01002335 struct drm_i915_private *dev_priv = to_i915(dev);
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002336
2337 intel_read_wm_latency(dev, dev_priv->wm.skl_latency);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002338 intel_print_wm_latency(dev_priv, "Gen9 Plane", dev_priv->wm.skl_latency);
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002339}
2340
Matt Ropered4a6a72016-02-23 17:20:13 -08002341static bool ilk_validate_pipe_wm(struct drm_device *dev,
2342 struct intel_pipe_wm *pipe_wm)
2343{
2344 /* LP0 watermark maximums depend on this pipe alone */
2345 const struct intel_wm_config config = {
2346 .num_pipes_active = 1,
2347 .sprites_enabled = pipe_wm->sprites_enabled,
2348 .sprites_scaled = pipe_wm->sprites_scaled,
2349 };
2350 struct ilk_wm_maximums max;
2351
2352 /* LP0 watermarks always use 1/2 DDB partitioning */
2353 ilk_compute_wm_maximums(dev, 0, &config, INTEL_DDB_PART_1_2, &max);
2354
2355 /* At least LP0 must be valid */
2356 if (!ilk_validate_wm_level(0, &max, &pipe_wm->wm[0])) {
2357 DRM_DEBUG_KMS("LP0 watermark invalid\n");
2358 return false;
2359 }
2360
2361 return true;
2362}
2363
Matt Roper261a27d2015-10-08 15:28:25 -07002364/* Compute new watermarks for the pipe */
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002365static int ilk_compute_pipe_wm(struct intel_crtc_state *cstate)
Matt Roper261a27d2015-10-08 15:28:25 -07002366{
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002367 struct drm_atomic_state *state = cstate->base.state;
2368 struct intel_crtc *intel_crtc = to_intel_crtc(cstate->base.crtc);
Matt Roper86c8bbb2015-09-24 15:53:16 -07002369 struct intel_pipe_wm *pipe_wm;
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002370 struct drm_device *dev = state->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01002371 const struct drm_i915_private *dev_priv = to_i915(dev);
Matt Roper43d59ed2015-09-24 15:53:07 -07002372 struct intel_plane *intel_plane;
Matt Roper86c8bbb2015-09-24 15:53:16 -07002373 struct intel_plane_state *pristate = NULL;
Matt Roper43d59ed2015-09-24 15:53:07 -07002374 struct intel_plane_state *sprstate = NULL;
Matt Roper86c8bbb2015-09-24 15:53:16 -07002375 struct intel_plane_state *curstate = NULL;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002376 int level, max_level = ilk_wm_max_level(dev_priv), usable_level;
Imre Deak820c1982013-12-17 14:46:36 +02002377 struct ilk_wm_maximums max;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002378
Matt Ropere8f1f022016-05-12 07:05:55 -07002379 pipe_wm = &cstate->wm.ilk.optimal;
Matt Roper86c8bbb2015-09-24 15:53:16 -07002380
Matt Roper43d59ed2015-09-24 15:53:07 -07002381 for_each_intel_plane_on_crtc(dev, intel_crtc, intel_plane) {
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002382 struct intel_plane_state *ps;
2383
2384 ps = intel_atomic_get_existing_plane_state(state,
2385 intel_plane);
2386 if (!ps)
2387 continue;
Matt Roper86c8bbb2015-09-24 15:53:16 -07002388
2389 if (intel_plane->base.type == DRM_PLANE_TYPE_PRIMARY)
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002390 pristate = ps;
Matt Roper86c8bbb2015-09-24 15:53:16 -07002391 else if (intel_plane->base.type == DRM_PLANE_TYPE_OVERLAY)
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002392 sprstate = ps;
Matt Roper86c8bbb2015-09-24 15:53:16 -07002393 else if (intel_plane->base.type == DRM_PLANE_TYPE_CURSOR)
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002394 curstate = ps;
Matt Roper43d59ed2015-09-24 15:53:07 -07002395 }
2396
Matt Ropered4a6a72016-02-23 17:20:13 -08002397 pipe_wm->pipe_enabled = cstate->base.active;
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002398 if (sprstate) {
Ville Syrjälä936e71e2016-07-26 19:06:59 +03002399 pipe_wm->sprites_enabled = sprstate->base.visible;
2400 pipe_wm->sprites_scaled = sprstate->base.visible &&
2401 (drm_rect_width(&sprstate->base.dst) != drm_rect_width(&sprstate->base.src) >> 16 ||
2402 drm_rect_height(&sprstate->base.dst) != drm_rect_height(&sprstate->base.src) >> 16);
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002403 }
2404
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01002405 usable_level = max_level;
2406
Ville Syrjälä7b39a0b2013-12-05 15:51:30 +02002407 /* ILK/SNB: LP2+ watermarks only w/o sprites */
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002408 if (INTEL_INFO(dev)->gen <= 6 && pipe_wm->sprites_enabled)
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01002409 usable_level = 1;
Ville Syrjälä7b39a0b2013-12-05 15:51:30 +02002410
2411 /* ILK/SNB/IVB: LP1+ watermarks only w/o scaling */
Matt Ropered4a6a72016-02-23 17:20:13 -08002412 if (pipe_wm->sprites_scaled)
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01002413 usable_level = 0;
Ville Syrjälä7b39a0b2013-12-05 15:51:30 +02002414
Matt Roper86c8bbb2015-09-24 15:53:16 -07002415 ilk_compute_wm_level(dev_priv, intel_crtc, 0, cstate,
Maarten Lankhorst71f0a622016-03-08 10:57:16 +01002416 pristate, sprstate, curstate, &pipe_wm->raw_wm[0]);
2417
2418 memset(&pipe_wm->wm, 0, sizeof(pipe_wm->wm));
2419 pipe_wm->wm[0] = pipe_wm->raw_wm[0];
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002420
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002421 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjälä532f7a72016-04-29 17:31:17 +03002422 pipe_wm->linetime = hsw_compute_linetime_wm(cstate);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002423
Matt Ropered4a6a72016-02-23 17:20:13 -08002424 if (!ilk_validate_pipe_wm(dev, pipe_wm))
Maarten Lankhorst1a426d62016-03-02 12:36:03 +01002425 return -EINVAL;
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002426
2427 ilk_compute_wm_reg_maximums(dev, 1, &max);
2428
2429 for (level = 1; level <= max_level; level++) {
Maarten Lankhorst71f0a622016-03-08 10:57:16 +01002430 struct intel_wm_level *wm = &pipe_wm->raw_wm[level];
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002431
Matt Roper86c8bbb2015-09-24 15:53:16 -07002432 ilk_compute_wm_level(dev_priv, intel_crtc, level, cstate,
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01002433 pristate, sprstate, curstate, wm);
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002434
2435 /*
2436 * Disable any watermark level that exceeds the
2437 * register maximums since such watermarks are
2438 * always invalid.
2439 */
Maarten Lankhorst71f0a622016-03-08 10:57:16 +01002440 if (level > usable_level)
2441 continue;
2442
2443 if (ilk_validate_wm_level(level, &max, wm))
2444 pipe_wm->wm[level] = *wm;
2445 else
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01002446 usable_level = level;
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002447 }
2448
Matt Roper86c8bbb2015-09-24 15:53:16 -07002449 return 0;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002450}
2451
2452/*
Matt Ropered4a6a72016-02-23 17:20:13 -08002453 * Build a set of 'intermediate' watermark values that satisfy both the old
2454 * state and the new state. These can be programmed to the hardware
2455 * immediately.
2456 */
2457static int ilk_compute_intermediate_wm(struct drm_device *dev,
2458 struct intel_crtc *intel_crtc,
2459 struct intel_crtc_state *newstate)
2460{
Matt Ropere8f1f022016-05-12 07:05:55 -07002461 struct intel_pipe_wm *a = &newstate->wm.ilk.intermediate;
Matt Ropered4a6a72016-02-23 17:20:13 -08002462 struct intel_pipe_wm *b = &intel_crtc->wm.active.ilk;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002463 int level, max_level = ilk_wm_max_level(to_i915(dev));
Matt Ropered4a6a72016-02-23 17:20:13 -08002464
2465 /*
2466 * Start with the final, target watermarks, then combine with the
2467 * currently active watermarks to get values that are safe both before
2468 * and after the vblank.
2469 */
Matt Ropere8f1f022016-05-12 07:05:55 -07002470 *a = newstate->wm.ilk.optimal;
Matt Ropered4a6a72016-02-23 17:20:13 -08002471 a->pipe_enabled |= b->pipe_enabled;
2472 a->sprites_enabled |= b->sprites_enabled;
2473 a->sprites_scaled |= b->sprites_scaled;
2474
2475 for (level = 0; level <= max_level; level++) {
2476 struct intel_wm_level *a_wm = &a->wm[level];
2477 const struct intel_wm_level *b_wm = &b->wm[level];
2478
2479 a_wm->enable &= b_wm->enable;
2480 a_wm->pri_val = max(a_wm->pri_val, b_wm->pri_val);
2481 a_wm->spr_val = max(a_wm->spr_val, b_wm->spr_val);
2482 a_wm->cur_val = max(a_wm->cur_val, b_wm->cur_val);
2483 a_wm->fbc_val = max(a_wm->fbc_val, b_wm->fbc_val);
2484 }
2485
2486 /*
2487 * We need to make sure that these merged watermark values are
2488 * actually a valid configuration themselves. If they're not,
2489 * there's no safe way to transition from the old state to
2490 * the new state, so we need to fail the atomic transaction.
2491 */
2492 if (!ilk_validate_pipe_wm(dev, a))
2493 return -EINVAL;
2494
2495 /*
2496 * If our intermediate WM are identical to the final WM, then we can
2497 * omit the post-vblank programming; only update if it's different.
2498 */
Matt Ropere8f1f022016-05-12 07:05:55 -07002499 if (memcmp(a, &newstate->wm.ilk.optimal, sizeof(*a)) == 0)
Matt Ropered4a6a72016-02-23 17:20:13 -08002500 newstate->wm.need_postvbl_update = false;
2501
2502 return 0;
2503}
2504
2505/*
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002506 * Merge the watermarks from all active pipes for a specific level.
2507 */
2508static void ilk_merge_wm_level(struct drm_device *dev,
2509 int level,
2510 struct intel_wm_level *ret_wm)
2511{
2512 const struct intel_crtc *intel_crtc;
2513
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002514 ret_wm->enable = true;
2515
Damien Lespiaud3fcc802014-05-13 23:32:22 +01002516 for_each_intel_crtc(dev, intel_crtc) {
Matt Ropered4a6a72016-02-23 17:20:13 -08002517 const struct intel_pipe_wm *active = &intel_crtc->wm.active.ilk;
Ville Syrjäläfe392ef2014-03-07 18:32:10 +02002518 const struct intel_wm_level *wm = &active->wm[level];
2519
2520 if (!active->pipe_enabled)
2521 continue;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002522
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002523 /*
2524 * The watermark values may have been used in the past,
2525 * so we must maintain them in the registers for some
2526 * time even if the level is now disabled.
2527 */
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002528 if (!wm->enable)
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002529 ret_wm->enable = false;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002530
2531 ret_wm->pri_val = max(ret_wm->pri_val, wm->pri_val);
2532 ret_wm->spr_val = max(ret_wm->spr_val, wm->spr_val);
2533 ret_wm->cur_val = max(ret_wm->cur_val, wm->cur_val);
2534 ret_wm->fbc_val = max(ret_wm->fbc_val, wm->fbc_val);
2535 }
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002536}
2537
2538/*
2539 * Merge all low power watermarks for all active pipes.
2540 */
2541static void ilk_wm_merge(struct drm_device *dev,
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02002542 const struct intel_wm_config *config,
Imre Deak820c1982013-12-17 14:46:36 +02002543 const struct ilk_wm_maximums *max,
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002544 struct intel_pipe_wm *merged)
2545{
Chris Wilsonfac5e232016-07-04 11:34:36 +01002546 struct drm_i915_private *dev_priv = to_i915(dev);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002547 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002548 int last_enabled_level = max_level;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002549
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02002550 /* ILK/SNB/IVB: LP1+ watermarks only w/ single pipe */
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01002551 if ((INTEL_GEN(dev_priv) <= 6 || IS_IVYBRIDGE(dev_priv)) &&
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02002552 config->num_pipes_active > 1)
Ville Syrjälä1204d5b2016-04-01 21:53:18 +03002553 last_enabled_level = 0;
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02002554
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02002555 /* ILK: FBC WM must be disabled always */
2556 merged->fbc_wm_enabled = INTEL_INFO(dev)->gen >= 6;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002557
2558 /* merge each WM1+ level */
2559 for (level = 1; level <= max_level; level++) {
2560 struct intel_wm_level *wm = &merged->wm[level];
2561
2562 ilk_merge_wm_level(dev, level, wm);
2563
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002564 if (level > last_enabled_level)
2565 wm->enable = false;
2566 else if (!ilk_validate_wm_level(level, max, wm))
2567 /* make sure all following levels get disabled */
2568 last_enabled_level = level - 1;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002569
2570 /*
2571 * The spec says it is preferred to disable
2572 * FBC WMs instead of disabling a WM level.
2573 */
2574 if (wm->fbc_val > max->fbc) {
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002575 if (wm->enable)
2576 merged->fbc_wm_enabled = false;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002577 wm->fbc_val = 0;
2578 }
2579 }
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02002580
2581 /* ILK: LP2+ must be disabled when FBC WM is disabled but FBC enabled */
2582 /*
2583 * FIXME this is racy. FBC might get enabled later.
2584 * What we should check here is whether FBC can be
2585 * enabled sometime later.
2586 */
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002587 if (IS_GEN5(dev_priv) && !merged->fbc_wm_enabled &&
Paulo Zanoni0e631ad2015-10-14 17:45:36 -03002588 intel_fbc_is_active(dev_priv)) {
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02002589 for (level = 2; level <= max_level; level++) {
2590 struct intel_wm_level *wm = &merged->wm[level];
2591
2592 wm->enable = false;
2593 }
2594 }
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002595}
2596
Ville Syrjäläb380ca32013-10-09 19:18:01 +03002597static int ilk_wm_lp_to_level(int wm_lp, const struct intel_pipe_wm *pipe_wm)
2598{
2599 /* LP1,LP2,LP3 levels are either 1,2,3 or 1,3,4 */
2600 return wm_lp + (wm_lp >= 2 && pipe_wm->wm[4].enable);
2601}
2602
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02002603/* The value we need to program into the WM_LPx latency field */
2604static unsigned int ilk_wm_lp_latency(struct drm_device *dev, int level)
2605{
Chris Wilsonfac5e232016-07-04 11:34:36 +01002606 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02002607
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002608 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02002609 return 2 * level;
2610 else
2611 return dev_priv->wm.pri_latency[level];
2612}
2613
Imre Deak820c1982013-12-17 14:46:36 +02002614static void ilk_compute_wm_results(struct drm_device *dev,
Ville Syrjälä0362c782013-10-09 19:17:57 +03002615 const struct intel_pipe_wm *merged,
Ville Syrjälä609cede2013-10-09 19:18:03 +03002616 enum intel_ddb_partitioning partitioning,
Imre Deak820c1982013-12-17 14:46:36 +02002617 struct ilk_wm_values *results)
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002618{
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002619 struct intel_crtc *intel_crtc;
2620 int level, wm_lp;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002621
Ville Syrjälä0362c782013-10-09 19:17:57 +03002622 results->enable_fbc_wm = merged->fbc_wm_enabled;
Ville Syrjälä609cede2013-10-09 19:18:03 +03002623 results->partitioning = partitioning;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002624
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002625 /* LP1+ register values */
Paulo Zanonicca32e92013-05-31 11:45:06 -03002626 for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
Ville Syrjälä1fd527c2013-08-06 22:24:05 +03002627 const struct intel_wm_level *r;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002628
Ville Syrjäläb380ca32013-10-09 19:18:01 +03002629 level = ilk_wm_lp_to_level(wm_lp, merged);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002630
Ville Syrjälä0362c782013-10-09 19:17:57 +03002631 r = &merged->wm[level];
Paulo Zanonicca32e92013-05-31 11:45:06 -03002632
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002633 /*
2634 * Maintain the watermark values even if the level is
2635 * disabled. Doing otherwise could cause underruns.
2636 */
2637 results->wm_lp[wm_lp - 1] =
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02002638 (ilk_wm_lp_latency(dev, level) << WM1_LP_LATENCY_SHIFT) |
Ville Syrjälä416f4722013-11-02 21:07:46 -07002639 (r->pri_val << WM1_LP_SR_SHIFT) |
2640 r->cur_val;
2641
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002642 if (r->enable)
2643 results->wm_lp[wm_lp - 1] |= WM1_LP_SR_EN;
2644
Ville Syrjälä416f4722013-11-02 21:07:46 -07002645 if (INTEL_INFO(dev)->gen >= 8)
2646 results->wm_lp[wm_lp - 1] |=
2647 r->fbc_val << WM1_LP_FBC_SHIFT_BDW;
2648 else
2649 results->wm_lp[wm_lp - 1] |=
2650 r->fbc_val << WM1_LP_FBC_SHIFT;
2651
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002652 /*
2653 * Always set WM1S_LP_EN when spr_val != 0, even if the
2654 * level is disabled. Doing otherwise could cause underruns.
2655 */
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02002656 if (INTEL_INFO(dev)->gen <= 6 && r->spr_val) {
2657 WARN_ON(wm_lp != 1);
2658 results->wm_lp_spr[wm_lp - 1] = WM1S_LP_EN | r->spr_val;
2659 } else
2660 results->wm_lp_spr[wm_lp - 1] = r->spr_val;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002661 }
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002662
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002663 /* LP0 register values */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01002664 for_each_intel_crtc(dev, intel_crtc) {
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002665 enum pipe pipe = intel_crtc->pipe;
Matt Ropered4a6a72016-02-23 17:20:13 -08002666 const struct intel_wm_level *r =
2667 &intel_crtc->wm.active.ilk.wm[0];
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002668
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002669 if (WARN_ON(!r->enable))
2670 continue;
2671
Matt Ropered4a6a72016-02-23 17:20:13 -08002672 results->wm_linetime[pipe] = intel_crtc->wm.active.ilk.linetime;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002673
2674 results->wm_pipe[pipe] =
2675 (r->pri_val << WM0_PIPE_PLANE_SHIFT) |
2676 (r->spr_val << WM0_PIPE_SPRITE_SHIFT) |
2677 r->cur_val;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002678 }
2679}
2680
Paulo Zanoni861f3382013-05-31 10:19:21 -03002681/* Find the result with the highest level enabled. Check for enable_fbc_wm in
2682 * case both are at the same level. Prefer r1 in case they're the same. */
Imre Deak820c1982013-12-17 14:46:36 +02002683static struct intel_pipe_wm *ilk_find_best_result(struct drm_device *dev,
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002684 struct intel_pipe_wm *r1,
2685 struct intel_pipe_wm *r2)
Paulo Zanoni861f3382013-05-31 10:19:21 -03002686{
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002687 int level, max_level = ilk_wm_max_level(to_i915(dev));
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002688 int level1 = 0, level2 = 0;
Paulo Zanoni861f3382013-05-31 10:19:21 -03002689
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002690 for (level = 1; level <= max_level; level++) {
2691 if (r1->wm[level].enable)
2692 level1 = level;
2693 if (r2->wm[level].enable)
2694 level2 = level;
Paulo Zanoni861f3382013-05-31 10:19:21 -03002695 }
2696
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002697 if (level1 == level2) {
2698 if (r2->fbc_wm_enabled && !r1->fbc_wm_enabled)
Paulo Zanoni861f3382013-05-31 10:19:21 -03002699 return r2;
2700 else
2701 return r1;
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002702 } else if (level1 > level2) {
Paulo Zanoni861f3382013-05-31 10:19:21 -03002703 return r1;
2704 } else {
2705 return r2;
2706 }
2707}
2708
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002709/* dirty bits used to track which watermarks need changes */
2710#define WM_DIRTY_PIPE(pipe) (1 << (pipe))
2711#define WM_DIRTY_LINETIME(pipe) (1 << (8 + (pipe)))
2712#define WM_DIRTY_LP(wm_lp) (1 << (15 + (wm_lp)))
2713#define WM_DIRTY_LP_ALL (WM_DIRTY_LP(1) | WM_DIRTY_LP(2) | WM_DIRTY_LP(3))
2714#define WM_DIRTY_FBC (1 << 24)
2715#define WM_DIRTY_DDB (1 << 25)
2716
Damien Lespiau055e3932014-08-18 13:49:10 +01002717static unsigned int ilk_compute_wm_dirty(struct drm_i915_private *dev_priv,
Imre Deak820c1982013-12-17 14:46:36 +02002718 const struct ilk_wm_values *old,
2719 const struct ilk_wm_values *new)
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002720{
2721 unsigned int dirty = 0;
2722 enum pipe pipe;
2723 int wm_lp;
2724
Damien Lespiau055e3932014-08-18 13:49:10 +01002725 for_each_pipe(dev_priv, pipe) {
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002726 if (old->wm_linetime[pipe] != new->wm_linetime[pipe]) {
2727 dirty |= WM_DIRTY_LINETIME(pipe);
2728 /* Must disable LP1+ watermarks too */
2729 dirty |= WM_DIRTY_LP_ALL;
2730 }
2731
2732 if (old->wm_pipe[pipe] != new->wm_pipe[pipe]) {
2733 dirty |= WM_DIRTY_PIPE(pipe);
2734 /* Must disable LP1+ watermarks too */
2735 dirty |= WM_DIRTY_LP_ALL;
2736 }
2737 }
2738
2739 if (old->enable_fbc_wm != new->enable_fbc_wm) {
2740 dirty |= WM_DIRTY_FBC;
2741 /* Must disable LP1+ watermarks too */
2742 dirty |= WM_DIRTY_LP_ALL;
2743 }
2744
2745 if (old->partitioning != new->partitioning) {
2746 dirty |= WM_DIRTY_DDB;
2747 /* Must disable LP1+ watermarks too */
2748 dirty |= WM_DIRTY_LP_ALL;
2749 }
2750
2751 /* LP1+ watermarks already deemed dirty, no need to continue */
2752 if (dirty & WM_DIRTY_LP_ALL)
2753 return dirty;
2754
2755 /* Find the lowest numbered LP1+ watermark in need of an update... */
2756 for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
2757 if (old->wm_lp[wm_lp - 1] != new->wm_lp[wm_lp - 1] ||
2758 old->wm_lp_spr[wm_lp - 1] != new->wm_lp_spr[wm_lp - 1])
2759 break;
2760 }
2761
2762 /* ...and mark it and all higher numbered LP1+ watermarks as dirty */
2763 for (; wm_lp <= 3; wm_lp++)
2764 dirty |= WM_DIRTY_LP(wm_lp);
2765
2766 return dirty;
2767}
2768
Ville Syrjälä8553c182013-12-05 15:51:39 +02002769static bool _ilk_disable_lp_wm(struct drm_i915_private *dev_priv,
2770 unsigned int dirty)
2771{
Imre Deak820c1982013-12-17 14:46:36 +02002772 struct ilk_wm_values *previous = &dev_priv->wm.hw;
Ville Syrjälä8553c182013-12-05 15:51:39 +02002773 bool changed = false;
2774
2775 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] & WM1_LP_SR_EN) {
2776 previous->wm_lp[2] &= ~WM1_LP_SR_EN;
2777 I915_WRITE(WM3_LP_ILK, previous->wm_lp[2]);
2778 changed = true;
2779 }
2780 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] & WM1_LP_SR_EN) {
2781 previous->wm_lp[1] &= ~WM1_LP_SR_EN;
2782 I915_WRITE(WM2_LP_ILK, previous->wm_lp[1]);
2783 changed = true;
2784 }
2785 if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] & WM1_LP_SR_EN) {
2786 previous->wm_lp[0] &= ~WM1_LP_SR_EN;
2787 I915_WRITE(WM1_LP_ILK, previous->wm_lp[0]);
2788 changed = true;
2789 }
2790
2791 /*
2792 * Don't touch WM1S_LP_EN here.
2793 * Doing so could cause underruns.
2794 */
2795
2796 return changed;
2797}
2798
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002799/*
2800 * The spec says we shouldn't write when we don't need, because every write
2801 * causes WMs to be re-evaluated, expending some power.
2802 */
Imre Deak820c1982013-12-17 14:46:36 +02002803static void ilk_write_wm_values(struct drm_i915_private *dev_priv,
2804 struct ilk_wm_values *results)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002805{
Chris Wilson91c8a322016-07-05 10:40:23 +01002806 struct drm_device *dev = &dev_priv->drm;
Imre Deak820c1982013-12-17 14:46:36 +02002807 struct ilk_wm_values *previous = &dev_priv->wm.hw;
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002808 unsigned int dirty;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002809 uint32_t val;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002810
Damien Lespiau055e3932014-08-18 13:49:10 +01002811 dirty = ilk_compute_wm_dirty(dev_priv, previous, results);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002812 if (!dirty)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002813 return;
2814
Ville Syrjälä8553c182013-12-05 15:51:39 +02002815 _ilk_disable_lp_wm(dev_priv, dirty);
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02002816
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002817 if (dirty & WM_DIRTY_PIPE(PIPE_A))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002818 I915_WRITE(WM0_PIPEA_ILK, results->wm_pipe[0]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002819 if (dirty & WM_DIRTY_PIPE(PIPE_B))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002820 I915_WRITE(WM0_PIPEB_ILK, results->wm_pipe[1]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002821 if (dirty & WM_DIRTY_PIPE(PIPE_C))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002822 I915_WRITE(WM0_PIPEC_IVB, results->wm_pipe[2]);
2823
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002824 if (dirty & WM_DIRTY_LINETIME(PIPE_A))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002825 I915_WRITE(PIPE_WM_LINETIME(PIPE_A), results->wm_linetime[0]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002826 if (dirty & WM_DIRTY_LINETIME(PIPE_B))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002827 I915_WRITE(PIPE_WM_LINETIME(PIPE_B), results->wm_linetime[1]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002828 if (dirty & WM_DIRTY_LINETIME(PIPE_C))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002829 I915_WRITE(PIPE_WM_LINETIME(PIPE_C), results->wm_linetime[2]);
2830
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002831 if (dirty & WM_DIRTY_DDB) {
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002832 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
Ville Syrjäläac9545f2013-12-05 15:51:28 +02002833 val = I915_READ(WM_MISC);
2834 if (results->partitioning == INTEL_DDB_PART_1_2)
2835 val &= ~WM_MISC_DATA_PARTITION_5_6;
2836 else
2837 val |= WM_MISC_DATA_PARTITION_5_6;
2838 I915_WRITE(WM_MISC, val);
2839 } else {
2840 val = I915_READ(DISP_ARB_CTL2);
2841 if (results->partitioning == INTEL_DDB_PART_1_2)
2842 val &= ~DISP_DATA_PARTITION_5_6;
2843 else
2844 val |= DISP_DATA_PARTITION_5_6;
2845 I915_WRITE(DISP_ARB_CTL2, val);
2846 }
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002847 }
2848
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002849 if (dirty & WM_DIRTY_FBC) {
Paulo Zanonicca32e92013-05-31 11:45:06 -03002850 val = I915_READ(DISP_ARB_CTL);
2851 if (results->enable_fbc_wm)
2852 val &= ~DISP_FBC_WM_DIS;
2853 else
2854 val |= DISP_FBC_WM_DIS;
2855 I915_WRITE(DISP_ARB_CTL, val);
2856 }
2857
Imre Deak954911e2013-12-17 14:46:34 +02002858 if (dirty & WM_DIRTY_LP(1) &&
2859 previous->wm_lp_spr[0] != results->wm_lp_spr[0])
2860 I915_WRITE(WM1S_LP_ILK, results->wm_lp_spr[0]);
2861
2862 if (INTEL_INFO(dev)->gen >= 7) {
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02002863 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp_spr[1] != results->wm_lp_spr[1])
2864 I915_WRITE(WM2S_LP_IVB, results->wm_lp_spr[1]);
2865 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp_spr[2] != results->wm_lp_spr[2])
2866 I915_WRITE(WM3S_LP_IVB, results->wm_lp_spr[2]);
2867 }
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002868
Ville Syrjäläfacd6192013-12-05 15:51:33 +02002869 if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] != results->wm_lp[0])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002870 I915_WRITE(WM1_LP_ILK, results->wm_lp[0]);
Ville Syrjäläfacd6192013-12-05 15:51:33 +02002871 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] != results->wm_lp[1])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002872 I915_WRITE(WM2_LP_ILK, results->wm_lp[1]);
Ville Syrjäläfacd6192013-12-05 15:51:33 +02002873 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] != results->wm_lp[2])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002874 I915_WRITE(WM3_LP_ILK, results->wm_lp[2]);
Ville Syrjälä609cede2013-10-09 19:18:03 +03002875
2876 dev_priv->wm.hw = *results;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002877}
2878
Matt Ropered4a6a72016-02-23 17:20:13 -08002879bool ilk_disable_lp_wm(struct drm_device *dev)
Ville Syrjälä8553c182013-12-05 15:51:39 +02002880{
Chris Wilsonfac5e232016-07-04 11:34:36 +01002881 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä8553c182013-12-05 15:51:39 +02002882
2883 return _ilk_disable_lp_wm(dev_priv, WM_DIRTY_LP_ALL);
2884}
2885
Lyude656d1b82016-08-17 15:55:54 -04002886#define SKL_SAGV_BLOCK_TIME 30 /* µs */
Damien Lespiaub9cec072014-11-04 17:06:43 +00002887
Matt Roper024c9042015-09-24 15:53:11 -07002888/*
2889 * Return the index of a plane in the SKL DDB and wm result arrays. Primary
2890 * plane is always in slot 0, cursor is always in slot I915_MAX_PLANES-1, and
2891 * other universal planes are in indices 1..n. Note that this may leave unused
2892 * indices between the top "sprite" plane and the cursor.
2893 */
2894static int
2895skl_wm_plane_id(const struct intel_plane *plane)
2896{
2897 switch (plane->base.type) {
2898 case DRM_PLANE_TYPE_PRIMARY:
2899 return 0;
2900 case DRM_PLANE_TYPE_CURSOR:
2901 return PLANE_CURSOR;
2902 case DRM_PLANE_TYPE_OVERLAY:
2903 return plane->plane + 1;
2904 default:
2905 MISSING_CASE(plane->base.type);
2906 return plane->plane;
2907 }
2908}
2909
Paulo Zanoniee3d5322016-10-11 15:25:38 -03002910/*
2911 * FIXME: We still don't have the proper code detect if we need to apply the WA,
2912 * so assume we'll always need it in order to avoid underruns.
2913 */
2914static bool skl_needs_memory_bw_wa(struct intel_atomic_state *state)
2915{
2916 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
2917
2918 if (IS_SKYLAKE(dev_priv) || IS_BROXTON(dev_priv) ||
2919 IS_KABYLAKE(dev_priv))
2920 return true;
2921
2922 return false;
2923}
2924
Paulo Zanoni56feca92016-09-22 18:00:28 -03002925static bool
2926intel_has_sagv(struct drm_i915_private *dev_priv)
2927{
Paulo Zanoni6e3100e2016-09-22 18:00:29 -03002928 if (IS_KABYLAKE(dev_priv))
2929 return true;
2930
2931 if (IS_SKYLAKE(dev_priv) &&
2932 dev_priv->sagv_status != I915_SAGV_NOT_CONTROLLED)
2933 return true;
2934
2935 return false;
Paulo Zanoni56feca92016-09-22 18:00:28 -03002936}
2937
Lyude656d1b82016-08-17 15:55:54 -04002938/*
2939 * SAGV dynamically adjusts the system agent voltage and clock frequencies
2940 * depending on power and performance requirements. The display engine access
2941 * to system memory is blocked during the adjustment time. Because of the
2942 * blocking time, having this enabled can cause full system hangs and/or pipe
2943 * underruns if we don't meet all of the following requirements:
2944 *
2945 * - <= 1 pipe enabled
2946 * - All planes can enable watermarks for latencies >= SAGV engine block time
2947 * - We're not using an interlaced display configuration
2948 */
2949int
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03002950intel_enable_sagv(struct drm_i915_private *dev_priv)
Lyude656d1b82016-08-17 15:55:54 -04002951{
2952 int ret;
2953
Paulo Zanoni56feca92016-09-22 18:00:28 -03002954 if (!intel_has_sagv(dev_priv))
2955 return 0;
2956
2957 if (dev_priv->sagv_status == I915_SAGV_ENABLED)
Lyude656d1b82016-08-17 15:55:54 -04002958 return 0;
2959
2960 DRM_DEBUG_KMS("Enabling the SAGV\n");
2961 mutex_lock(&dev_priv->rps.hw_lock);
2962
2963 ret = sandybridge_pcode_write(dev_priv, GEN9_PCODE_SAGV_CONTROL,
2964 GEN9_SAGV_ENABLE);
2965
2966 /* We don't need to wait for the SAGV when enabling */
2967 mutex_unlock(&dev_priv->rps.hw_lock);
2968
2969 /*
2970 * Some skl systems, pre-release machines in particular,
2971 * don't actually have an SAGV.
2972 */
Paulo Zanoni6e3100e2016-09-22 18:00:29 -03002973 if (IS_SKYLAKE(dev_priv) && ret == -ENXIO) {
Lyude656d1b82016-08-17 15:55:54 -04002974 DRM_DEBUG_DRIVER("No SAGV found on system, ignoring\n");
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03002975 dev_priv->sagv_status = I915_SAGV_NOT_CONTROLLED;
Lyude656d1b82016-08-17 15:55:54 -04002976 return 0;
2977 } else if (ret < 0) {
2978 DRM_ERROR("Failed to enable the SAGV\n");
2979 return ret;
2980 }
2981
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03002982 dev_priv->sagv_status = I915_SAGV_ENABLED;
Lyude656d1b82016-08-17 15:55:54 -04002983 return 0;
2984}
2985
2986static int
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03002987intel_do_sagv_disable(struct drm_i915_private *dev_priv)
Lyude656d1b82016-08-17 15:55:54 -04002988{
2989 int ret;
2990 uint32_t temp = GEN9_SAGV_DISABLE;
2991
2992 ret = sandybridge_pcode_read(dev_priv, GEN9_PCODE_SAGV_CONTROL,
2993 &temp);
2994 if (ret)
2995 return ret;
2996 else
2997 return temp & GEN9_SAGV_IS_DISABLED;
2998}
2999
3000int
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003001intel_disable_sagv(struct drm_i915_private *dev_priv)
Lyude656d1b82016-08-17 15:55:54 -04003002{
3003 int ret, result;
3004
Paulo Zanoni56feca92016-09-22 18:00:28 -03003005 if (!intel_has_sagv(dev_priv))
3006 return 0;
3007
3008 if (dev_priv->sagv_status == I915_SAGV_DISABLED)
Lyude656d1b82016-08-17 15:55:54 -04003009 return 0;
3010
3011 DRM_DEBUG_KMS("Disabling the SAGV\n");
3012 mutex_lock(&dev_priv->rps.hw_lock);
3013
3014 /* bspec says to keep retrying for at least 1 ms */
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003015 ret = wait_for(result = intel_do_sagv_disable(dev_priv), 1);
Lyude656d1b82016-08-17 15:55:54 -04003016 mutex_unlock(&dev_priv->rps.hw_lock);
3017
3018 if (ret == -ETIMEDOUT) {
3019 DRM_ERROR("Request to disable SAGV timed out\n");
3020 return -ETIMEDOUT;
3021 }
3022
3023 /*
3024 * Some skl systems, pre-release machines in particular,
3025 * don't actually have an SAGV.
3026 */
Paulo Zanoni6e3100e2016-09-22 18:00:29 -03003027 if (IS_SKYLAKE(dev_priv) && result == -ENXIO) {
Lyude656d1b82016-08-17 15:55:54 -04003028 DRM_DEBUG_DRIVER("No SAGV found on system, ignoring\n");
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003029 dev_priv->sagv_status = I915_SAGV_NOT_CONTROLLED;
Lyude656d1b82016-08-17 15:55:54 -04003030 return 0;
3031 } else if (result < 0) {
3032 DRM_ERROR("Failed to disable the SAGV\n");
3033 return result;
3034 }
3035
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003036 dev_priv->sagv_status = I915_SAGV_DISABLED;
Lyude656d1b82016-08-17 15:55:54 -04003037 return 0;
3038}
3039
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003040bool intel_can_enable_sagv(struct drm_atomic_state *state)
Lyude656d1b82016-08-17 15:55:54 -04003041{
3042 struct drm_device *dev = state->dev;
3043 struct drm_i915_private *dev_priv = to_i915(dev);
3044 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003045 struct intel_crtc *crtc;
3046 struct intel_plane *plane;
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003047 struct intel_crtc_state *cstate;
3048 struct skl_plane_wm *wm;
Lyude656d1b82016-08-17 15:55:54 -04003049 enum pipe pipe;
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003050 int level, latency;
Lyude656d1b82016-08-17 15:55:54 -04003051
Paulo Zanoni56feca92016-09-22 18:00:28 -03003052 if (!intel_has_sagv(dev_priv))
3053 return false;
3054
Lyude656d1b82016-08-17 15:55:54 -04003055 /*
3056 * SKL workaround: bspec recommends we disable the SAGV when we have
3057 * more then one pipe enabled
3058 *
3059 * If there are no active CRTCs, no additional checks need be performed
3060 */
3061 if (hweight32(intel_state->active_crtcs) == 0)
3062 return true;
3063 else if (hweight32(intel_state->active_crtcs) > 1)
3064 return false;
3065
3066 /* Since we're now guaranteed to only have one active CRTC... */
3067 pipe = ffs(intel_state->active_crtcs) - 1;
Ville Syrjälä98187832016-10-31 22:37:10 +02003068 crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003069 cstate = to_intel_crtc_state(crtc->base.state);
Lyude656d1b82016-08-17 15:55:54 -04003070
Paulo Zanonic89cadd2016-10-10 17:30:59 -03003071 if (crtc->base.state->adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Lyude656d1b82016-08-17 15:55:54 -04003072 return false;
3073
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003074 for_each_intel_plane_on_crtc(dev, crtc, plane) {
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003075 wm = &cstate->wm.skl.optimal.planes[skl_wm_plane_id(plane)];
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003076
Lyude656d1b82016-08-17 15:55:54 -04003077 /* Skip this plane if it's not enabled */
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003078 if (!wm->wm[0].plane_en)
Lyude656d1b82016-08-17 15:55:54 -04003079 continue;
3080
3081 /* Find the highest enabled wm level for this plane */
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003082 for (level = ilk_wm_max_level(dev_priv);
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003083 !wm->wm[level].plane_en; --level)
Lyude656d1b82016-08-17 15:55:54 -04003084 { }
3085
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003086 latency = dev_priv->wm.skl_latency[level];
3087
3088 if (skl_needs_memory_bw_wa(intel_state) &&
3089 plane->base.state->fb->modifier[0] ==
3090 I915_FORMAT_MOD_X_TILED)
3091 latency += 15;
3092
Lyude656d1b82016-08-17 15:55:54 -04003093 /*
3094 * If any of the planes on this pipe don't enable wm levels
3095 * that incur memory latencies higher then 30µs we can't enable
3096 * the SAGV
3097 */
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003098 if (latency < SKL_SAGV_BLOCK_TIME)
Lyude656d1b82016-08-17 15:55:54 -04003099 return false;
3100 }
3101
3102 return true;
3103}
3104
Damien Lespiaub9cec072014-11-04 17:06:43 +00003105static void
3106skl_ddb_get_pipe_allocation_limits(struct drm_device *dev,
Matt Roper024c9042015-09-24 15:53:11 -07003107 const struct intel_crtc_state *cstate,
Matt Roperc107acf2016-05-12 07:06:01 -07003108 struct skl_ddb_entry *alloc, /* out */
3109 int *num_active /* out */)
Damien Lespiaub9cec072014-11-04 17:06:43 +00003110{
Matt Roperc107acf2016-05-12 07:06:01 -07003111 struct drm_atomic_state *state = cstate->base.state;
3112 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
3113 struct drm_i915_private *dev_priv = to_i915(dev);
Matt Roper024c9042015-09-24 15:53:11 -07003114 struct drm_crtc *for_crtc = cstate->base.crtc;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003115 unsigned int pipe_size, ddb_size;
3116 int nth_active_pipe;
Matt Roperc107acf2016-05-12 07:06:01 -07003117
Matt Ropera6d3460e2016-05-12 07:06:04 -07003118 if (WARN_ON(!state) || !cstate->base.active) {
Damien Lespiaub9cec072014-11-04 17:06:43 +00003119 alloc->start = 0;
3120 alloc->end = 0;
Matt Ropera6d3460e2016-05-12 07:06:04 -07003121 *num_active = hweight32(dev_priv->active_crtcs);
Damien Lespiaub9cec072014-11-04 17:06:43 +00003122 return;
3123 }
3124
Matt Ropera6d3460e2016-05-12 07:06:04 -07003125 if (intel_state->active_pipe_changes)
3126 *num_active = hweight32(intel_state->active_crtcs);
3127 else
3128 *num_active = hweight32(dev_priv->active_crtcs);
3129
Deepak M6f3fff62016-09-15 15:01:10 +05303130 ddb_size = INTEL_INFO(dev_priv)->ddb_size;
3131 WARN_ON(ddb_size == 0);
Damien Lespiaub9cec072014-11-04 17:06:43 +00003132
3133 ddb_size -= 4; /* 4 blocks for bypass path allocation */
3134
Matt Roperc107acf2016-05-12 07:06:01 -07003135 /*
Matt Ropera6d3460e2016-05-12 07:06:04 -07003136 * If the state doesn't change the active CRTC's, then there's
3137 * no need to recalculate; the existing pipe allocation limits
3138 * should remain unchanged. Note that we're safe from racing
3139 * commits since any racing commit that changes the active CRTC
3140 * list would need to grab _all_ crtc locks, including the one
3141 * we currently hold.
Matt Roperc107acf2016-05-12 07:06:01 -07003142 */
Matt Ropera6d3460e2016-05-12 07:06:04 -07003143 if (!intel_state->active_pipe_changes) {
Lyudece0ba282016-09-15 10:46:35 -04003144 *alloc = to_intel_crtc(for_crtc)->hw_ddb;
Matt Ropera6d3460e2016-05-12 07:06:04 -07003145 return;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003146 }
Matt Ropera6d3460e2016-05-12 07:06:04 -07003147
3148 nth_active_pipe = hweight32(intel_state->active_crtcs &
3149 (drm_crtc_mask(for_crtc) - 1));
3150 pipe_size = ddb_size / hweight32(intel_state->active_crtcs);
3151 alloc->start = nth_active_pipe * ddb_size / *num_active;
3152 alloc->end = alloc->start + pipe_size;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003153}
3154
Matt Roperc107acf2016-05-12 07:06:01 -07003155static unsigned int skl_cursor_allocation(int num_active)
Damien Lespiaub9cec072014-11-04 17:06:43 +00003156{
Matt Roperc107acf2016-05-12 07:06:01 -07003157 if (num_active == 1)
Damien Lespiaub9cec072014-11-04 17:06:43 +00003158 return 32;
3159
3160 return 8;
3161}
3162
Damien Lespiaua269c582014-11-04 17:06:49 +00003163static void skl_ddb_entry_init_from_hw(struct skl_ddb_entry *entry, u32 reg)
3164{
3165 entry->start = reg & 0x3ff;
3166 entry->end = (reg >> 16) & 0x3ff;
Damien Lespiau16160e32014-11-04 17:06:53 +00003167 if (entry->end)
3168 entry->end += 1;
Damien Lespiaua269c582014-11-04 17:06:49 +00003169}
3170
Damien Lespiau08db6652014-11-04 17:06:52 +00003171void skl_ddb_get_hw_state(struct drm_i915_private *dev_priv,
3172 struct skl_ddb_allocation *ddb /* out */)
Damien Lespiaua269c582014-11-04 17:06:49 +00003173{
Damien Lespiaua269c582014-11-04 17:06:49 +00003174 enum pipe pipe;
3175 int plane;
3176 u32 val;
3177
Maarten Lankhorstb10f1b22015-10-22 13:56:34 +02003178 memset(ddb, 0, sizeof(*ddb));
3179
Damien Lespiaua269c582014-11-04 17:06:49 +00003180 for_each_pipe(dev_priv, pipe) {
Imre Deak4d800032016-02-17 16:31:29 +02003181 enum intel_display_power_domain power_domain;
3182
3183 power_domain = POWER_DOMAIN_PIPE(pipe);
3184 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
Maarten Lankhorstb10f1b22015-10-22 13:56:34 +02003185 continue;
3186
Matt Roper8b364b42016-10-26 15:51:28 -07003187 for_each_universal_plane(dev_priv, pipe, plane) {
Damien Lespiaua269c582014-11-04 17:06:49 +00003188 val = I915_READ(PLANE_BUF_CFG(pipe, plane));
3189 skl_ddb_entry_init_from_hw(&ddb->plane[pipe][plane],
3190 val);
3191 }
3192
3193 val = I915_READ(CUR_BUF_CFG(pipe));
Matt Roper4969d332015-09-24 15:53:10 -07003194 skl_ddb_entry_init_from_hw(&ddb->plane[pipe][PLANE_CURSOR],
3195 val);
Imre Deak4d800032016-02-17 16:31:29 +02003196
3197 intel_display_power_put(dev_priv, power_domain);
Damien Lespiaua269c582014-11-04 17:06:49 +00003198 }
3199}
3200
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07003201/*
3202 * Determines the downscale amount of a plane for the purposes of watermark calculations.
3203 * The bspec defines downscale amount as:
3204 *
3205 * """
3206 * Horizontal down scale amount = maximum[1, Horizontal source size /
3207 * Horizontal destination size]
3208 * Vertical down scale amount = maximum[1, Vertical source size /
3209 * Vertical destination size]
3210 * Total down scale amount = Horizontal down scale amount *
3211 * Vertical down scale amount
3212 * """
3213 *
3214 * Return value is provided in 16.16 fixed point form to retain fractional part.
3215 * Caller should take care of dividing & rounding off the value.
3216 */
3217static uint32_t
3218skl_plane_downscale_amount(const struct intel_plane_state *pstate)
3219{
3220 uint32_t downscale_h, downscale_w;
3221 uint32_t src_w, src_h, dst_w, dst_h;
3222
Ville Syrjälä936e71e2016-07-26 19:06:59 +03003223 if (WARN_ON(!pstate->base.visible))
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07003224 return DRM_PLANE_HELPER_NO_SCALING;
3225
3226 /* n.b., src is 16.16 fixed point, dst is whole integer */
Ville Syrjälä936e71e2016-07-26 19:06:59 +03003227 src_w = drm_rect_width(&pstate->base.src);
3228 src_h = drm_rect_height(&pstate->base.src);
3229 dst_w = drm_rect_width(&pstate->base.dst);
3230 dst_h = drm_rect_height(&pstate->base.dst);
Ville Syrjäläbd2ef252016-09-26 19:30:46 +03003231 if (drm_rotation_90_or_270(pstate->base.rotation))
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07003232 swap(dst_w, dst_h);
3233
3234 downscale_h = max(src_h / dst_h, (uint32_t)DRM_PLANE_HELPER_NO_SCALING);
3235 downscale_w = max(src_w / dst_w, (uint32_t)DRM_PLANE_HELPER_NO_SCALING);
3236
3237 /* Provide result in 16.16 fixed point */
3238 return (uint64_t)downscale_w * downscale_h >> 16;
3239}
3240
Damien Lespiaub9cec072014-11-04 17:06:43 +00003241static unsigned int
Matt Roper024c9042015-09-24 15:53:11 -07003242skl_plane_relative_data_rate(const struct intel_crtc_state *cstate,
3243 const struct drm_plane_state *pstate,
3244 int y)
Damien Lespiaub9cec072014-11-04 17:06:43 +00003245{
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07003246 struct intel_plane_state *intel_pstate = to_intel_plane_state(pstate);
Matt Roper024c9042015-09-24 15:53:11 -07003247 struct drm_framebuffer *fb = pstate->fb;
Kumar, Mahesh8d19d7d2016-05-19 15:03:01 -07003248 uint32_t down_scale_amount, data_rate;
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07003249 uint32_t width = 0, height = 0;
Matt Ropera1de91e2016-05-12 07:05:57 -07003250 unsigned format = fb ? fb->pixel_format : DRM_FORMAT_XRGB8888;
3251
Ville Syrjälä936e71e2016-07-26 19:06:59 +03003252 if (!intel_pstate->base.visible)
Matt Ropera1de91e2016-05-12 07:05:57 -07003253 return 0;
3254 if (pstate->plane->type == DRM_PLANE_TYPE_CURSOR)
3255 return 0;
3256 if (y && format != DRM_FORMAT_NV12)
3257 return 0;
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07003258
Ville Syrjälä936e71e2016-07-26 19:06:59 +03003259 width = drm_rect_width(&intel_pstate->base.src) >> 16;
3260 height = drm_rect_height(&intel_pstate->base.src) >> 16;
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07003261
Ville Syrjäläbd2ef252016-09-26 19:30:46 +03003262 if (drm_rotation_90_or_270(pstate->rotation))
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07003263 swap(width, height);
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003264
3265 /* for planar format */
Matt Ropera1de91e2016-05-12 07:05:57 -07003266 if (format == DRM_FORMAT_NV12) {
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003267 if (y) /* y-plane data rate */
Kumar, Mahesh8d19d7d2016-05-19 15:03:01 -07003268 data_rate = width * height *
Matt Ropera1de91e2016-05-12 07:05:57 -07003269 drm_format_plane_cpp(format, 0);
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003270 else /* uv-plane data rate */
Kumar, Mahesh8d19d7d2016-05-19 15:03:01 -07003271 data_rate = (width / 2) * (height / 2) *
Matt Ropera1de91e2016-05-12 07:05:57 -07003272 drm_format_plane_cpp(format, 1);
Kumar, Mahesh8d19d7d2016-05-19 15:03:01 -07003273 } else {
3274 /* for packed formats */
3275 data_rate = width * height * drm_format_plane_cpp(format, 0);
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003276 }
3277
Kumar, Mahesh8d19d7d2016-05-19 15:03:01 -07003278 down_scale_amount = skl_plane_downscale_amount(intel_pstate);
3279
3280 return (uint64_t)data_rate * down_scale_amount >> 16;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003281}
3282
3283/*
3284 * We don't overflow 32 bits. Worst case is 3 planes enabled, each fetching
3285 * a 8192x4096@32bpp framebuffer:
3286 * 3 * 4096 * 8192 * 4 < 2^32
3287 */
3288static unsigned int
Maarten Lankhorst1e6ee542016-10-26 15:41:32 +02003289skl_get_total_relative_data_rate(struct intel_crtc_state *intel_cstate,
3290 unsigned *plane_data_rate,
3291 unsigned *plane_y_data_rate)
Damien Lespiaub9cec072014-11-04 17:06:43 +00003292{
Matt Roper9c74d822016-05-12 07:05:58 -07003293 struct drm_crtc_state *cstate = &intel_cstate->base;
3294 struct drm_atomic_state *state = cstate->state;
Maarten Lankhorstc8fe32c2016-10-26 15:41:29 +02003295 struct drm_plane *plane;
Matt Roper024c9042015-09-24 15:53:11 -07003296 const struct intel_plane *intel_plane;
Maarten Lankhorstc8fe32c2016-10-26 15:41:29 +02003297 const struct drm_plane_state *pstate;
Matt Ropera1de91e2016-05-12 07:05:57 -07003298 unsigned int rate, total_data_rate = 0;
Matt Roper9c74d822016-05-12 07:05:58 -07003299 int id;
Matt Ropera6d3460e2016-05-12 07:06:04 -07003300
3301 if (WARN_ON(!state))
3302 return 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003303
Matt Ropera1de91e2016-05-12 07:05:57 -07003304 /* Calculate and cache data rate for each plane */
Maarten Lankhorstc8fe32c2016-10-26 15:41:29 +02003305 drm_atomic_crtc_state_for_each_plane_state(plane, pstate, cstate) {
Matt Ropera6d3460e2016-05-12 07:06:04 -07003306 id = skl_wm_plane_id(to_intel_plane(plane));
3307 intel_plane = to_intel_plane(plane);
Matt Roper024c9042015-09-24 15:53:11 -07003308
Matt Ropera6d3460e2016-05-12 07:06:04 -07003309 /* packed/uv */
3310 rate = skl_plane_relative_data_rate(intel_cstate,
3311 pstate, 0);
Maarten Lankhorst1e6ee542016-10-26 15:41:32 +02003312 plane_data_rate[id] = rate;
3313
3314 total_data_rate += rate;
Matt Roper9c74d822016-05-12 07:05:58 -07003315
Matt Ropera6d3460e2016-05-12 07:06:04 -07003316 /* y-plane */
3317 rate = skl_plane_relative_data_rate(intel_cstate,
3318 pstate, 1);
Maarten Lankhorst1e6ee542016-10-26 15:41:32 +02003319 plane_y_data_rate[id] = rate;
Matt Ropera1de91e2016-05-12 07:05:57 -07003320
Maarten Lankhorst1e6ee542016-10-26 15:41:32 +02003321 total_data_rate += rate;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003322 }
3323
3324 return total_data_rate;
3325}
3326
Kumar, Maheshcbcfd142016-05-31 09:58:59 -07003327static uint16_t
3328skl_ddb_min_alloc(const struct drm_plane_state *pstate,
3329 const int y)
3330{
3331 struct drm_framebuffer *fb = pstate->fb;
3332 struct intel_plane_state *intel_pstate = to_intel_plane_state(pstate);
3333 uint32_t src_w, src_h;
3334 uint32_t min_scanlines = 8;
3335 uint8_t plane_bpp;
3336
3337 if (WARN_ON(!fb))
3338 return 0;
3339
3340 /* For packed formats, no y-plane, return 0 */
3341 if (y && fb->pixel_format != DRM_FORMAT_NV12)
3342 return 0;
3343
3344 /* For Non Y-tile return 8-blocks */
3345 if (fb->modifier[0] != I915_FORMAT_MOD_Y_TILED &&
3346 fb->modifier[0] != I915_FORMAT_MOD_Yf_TILED)
3347 return 8;
3348
Ville Syrjälä936e71e2016-07-26 19:06:59 +03003349 src_w = drm_rect_width(&intel_pstate->base.src) >> 16;
3350 src_h = drm_rect_height(&intel_pstate->base.src) >> 16;
Kumar, Maheshcbcfd142016-05-31 09:58:59 -07003351
Ville Syrjäläbd2ef252016-09-26 19:30:46 +03003352 if (drm_rotation_90_or_270(pstate->rotation))
Kumar, Maheshcbcfd142016-05-31 09:58:59 -07003353 swap(src_w, src_h);
3354
3355 /* Halve UV plane width and height for NV12 */
3356 if (fb->pixel_format == DRM_FORMAT_NV12 && !y) {
3357 src_w /= 2;
3358 src_h /= 2;
3359 }
3360
3361 if (fb->pixel_format == DRM_FORMAT_NV12 && !y)
3362 plane_bpp = drm_format_plane_cpp(fb->pixel_format, 1);
3363 else
3364 plane_bpp = drm_format_plane_cpp(fb->pixel_format, 0);
3365
Ville Syrjäläbd2ef252016-09-26 19:30:46 +03003366 if (drm_rotation_90_or_270(pstate->rotation)) {
Kumar, Maheshcbcfd142016-05-31 09:58:59 -07003367 switch (plane_bpp) {
3368 case 1:
3369 min_scanlines = 32;
3370 break;
3371 case 2:
3372 min_scanlines = 16;
3373 break;
3374 case 4:
3375 min_scanlines = 8;
3376 break;
3377 case 8:
3378 min_scanlines = 4;
3379 break;
3380 default:
3381 WARN(1, "Unsupported pixel depth %u for rotation",
3382 plane_bpp);
3383 min_scanlines = 32;
3384 }
3385 }
3386
3387 return DIV_ROUND_UP((4 * src_w * plane_bpp), 512) * min_scanlines/4 + 3;
3388}
3389
Maarten Lankhorst49845a72016-10-26 15:41:34 +02003390static void
3391skl_ddb_calc_min(const struct intel_crtc_state *cstate, int num_active,
3392 uint16_t *minimum, uint16_t *y_minimum)
3393{
3394 const struct drm_plane_state *pstate;
3395 struct drm_plane *plane;
3396
3397 drm_atomic_crtc_state_for_each_plane_state(plane, pstate, &cstate->base) {
3398 struct intel_plane *intel_plane = to_intel_plane(plane);
3399 int id = skl_wm_plane_id(intel_plane);
3400
3401 if (id == PLANE_CURSOR)
3402 continue;
3403
3404 if (!pstate->visible)
3405 continue;
3406
3407 minimum[id] = skl_ddb_min_alloc(pstate, 0);
3408 y_minimum[id] = skl_ddb_min_alloc(pstate, 1);
3409 }
3410
3411 minimum[PLANE_CURSOR] = skl_cursor_allocation(num_active);
3412}
3413
Matt Roperc107acf2016-05-12 07:06:01 -07003414static int
Matt Roper024c9042015-09-24 15:53:11 -07003415skl_allocate_pipe_ddb(struct intel_crtc_state *cstate,
Damien Lespiaub9cec072014-11-04 17:06:43 +00003416 struct skl_ddb_allocation *ddb /* out */)
3417{
Matt Roperc107acf2016-05-12 07:06:01 -07003418 struct drm_atomic_state *state = cstate->base.state;
Matt Roper024c9042015-09-24 15:53:11 -07003419 struct drm_crtc *crtc = cstate->base.crtc;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003420 struct drm_device *dev = crtc->dev;
3421 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3422 enum pipe pipe = intel_crtc->pipe;
Lyudece0ba282016-09-15 10:46:35 -04003423 struct skl_ddb_entry *alloc = &cstate->wm.skl.ddb;
Maarten Lankhorst49845a72016-10-26 15:41:34 +02003424 uint16_t alloc_size, start;
Maarten Lankhorstfefdd812016-10-26 15:41:33 +02003425 uint16_t minimum[I915_MAX_PLANES] = {};
3426 uint16_t y_minimum[I915_MAX_PLANES] = {};
Damien Lespiaub9cec072014-11-04 17:06:43 +00003427 unsigned int total_data_rate;
Matt Roperc107acf2016-05-12 07:06:01 -07003428 int num_active;
3429 int id, i;
Maarten Lankhorst1e6ee542016-10-26 15:41:32 +02003430 unsigned plane_data_rate[I915_MAX_PLANES] = {};
3431 unsigned plane_y_data_rate[I915_MAX_PLANES] = {};
Damien Lespiaub9cec072014-11-04 17:06:43 +00003432
Paulo Zanoni5a920b82016-10-04 14:37:32 -03003433 /* Clear the partitioning for disabled planes. */
3434 memset(ddb->plane[pipe], 0, sizeof(ddb->plane[pipe]));
3435 memset(ddb->y_plane[pipe], 0, sizeof(ddb->y_plane[pipe]));
3436
Matt Ropera6d3460e2016-05-12 07:06:04 -07003437 if (WARN_ON(!state))
3438 return 0;
3439
Matt Roperc107acf2016-05-12 07:06:01 -07003440 if (!cstate->base.active) {
Lyudece0ba282016-09-15 10:46:35 -04003441 alloc->start = alloc->end = 0;
Matt Roperc107acf2016-05-12 07:06:01 -07003442 return 0;
3443 }
3444
Matt Ropera6d3460e2016-05-12 07:06:04 -07003445 skl_ddb_get_pipe_allocation_limits(dev, cstate, alloc, &num_active);
Damien Lespiau34bb56a2014-11-04 17:07:01 +00003446 alloc_size = skl_ddb_entry_size(alloc);
Damien Lespiaub9cec072014-11-04 17:06:43 +00003447 if (alloc_size == 0) {
3448 memset(ddb->plane[pipe], 0, sizeof(ddb->plane[pipe]));
Matt Roperc107acf2016-05-12 07:06:01 -07003449 return 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003450 }
3451
Maarten Lankhorst49845a72016-10-26 15:41:34 +02003452 skl_ddb_calc_min(cstate, num_active, minimum, y_minimum);
Damien Lespiaub9cec072014-11-04 17:06:43 +00003453
Maarten Lankhorst49845a72016-10-26 15:41:34 +02003454 /*
3455 * 1. Allocate the mininum required blocks for each active plane
3456 * and allocate the cursor, it doesn't require extra allocation
3457 * proportional to the data rate.
3458 */
Damien Lespiaub9cec072014-11-04 17:06:43 +00003459
Maarten Lankhorst49845a72016-10-26 15:41:34 +02003460 for (i = 0; i < I915_MAX_PLANES; i++) {
Matt Roperc107acf2016-05-12 07:06:01 -07003461 alloc_size -= minimum[i];
3462 alloc_size -= y_minimum[i];
Damien Lespiau80958152015-02-09 13:35:10 +00003463 }
3464
Maarten Lankhorst49845a72016-10-26 15:41:34 +02003465 ddb->plane[pipe][PLANE_CURSOR].start = alloc->end - minimum[PLANE_CURSOR];
3466 ddb->plane[pipe][PLANE_CURSOR].end = alloc->end;
3467
Damien Lespiaub9cec072014-11-04 17:06:43 +00003468 /*
Damien Lespiau80958152015-02-09 13:35:10 +00003469 * 2. Distribute the remaining space in proportion to the amount of
3470 * data each plane needs to fetch from memory.
Damien Lespiaub9cec072014-11-04 17:06:43 +00003471 *
3472 * FIXME: we may not allocate every single block here.
3473 */
Maarten Lankhorst1e6ee542016-10-26 15:41:32 +02003474 total_data_rate = skl_get_total_relative_data_rate(cstate,
3475 plane_data_rate,
3476 plane_y_data_rate);
Matt Ropera1de91e2016-05-12 07:05:57 -07003477 if (total_data_rate == 0)
Matt Roperc107acf2016-05-12 07:06:01 -07003478 return 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003479
Damien Lespiau34bb56a2014-11-04 17:07:01 +00003480 start = alloc->start;
Maarten Lankhorst1e6ee542016-10-26 15:41:32 +02003481 for (id = 0; id < I915_MAX_PLANES; id++) {
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003482 unsigned int data_rate, y_data_rate;
3483 uint16_t plane_blocks, y_plane_blocks = 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003484
Maarten Lankhorst49845a72016-10-26 15:41:34 +02003485 if (id == PLANE_CURSOR)
3486 continue;
3487
Maarten Lankhorst1e6ee542016-10-26 15:41:32 +02003488 data_rate = plane_data_rate[id];
Damien Lespiaub9cec072014-11-04 17:06:43 +00003489
3490 /*
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003491 * allocation for (packed formats) or (uv-plane part of planar format):
Damien Lespiaub9cec072014-11-04 17:06:43 +00003492 * promote the expression to 64 bits to avoid overflowing, the
3493 * result is < available as data_rate / total_data_rate < 1
3494 */
Matt Roper024c9042015-09-24 15:53:11 -07003495 plane_blocks = minimum[id];
Damien Lespiau80958152015-02-09 13:35:10 +00003496 plane_blocks += div_u64((uint64_t)alloc_size * data_rate,
3497 total_data_rate);
Damien Lespiaub9cec072014-11-04 17:06:43 +00003498
Matt Roperc107acf2016-05-12 07:06:01 -07003499 /* Leave disabled planes at (0,0) */
3500 if (data_rate) {
3501 ddb->plane[pipe][id].start = start;
3502 ddb->plane[pipe][id].end = start + plane_blocks;
3503 }
Damien Lespiaub9cec072014-11-04 17:06:43 +00003504
3505 start += plane_blocks;
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003506
3507 /*
3508 * allocation for y_plane part of planar format:
3509 */
Maarten Lankhorst1e6ee542016-10-26 15:41:32 +02003510 y_data_rate = plane_y_data_rate[id];
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003511
Matt Ropera1de91e2016-05-12 07:05:57 -07003512 y_plane_blocks = y_minimum[id];
3513 y_plane_blocks += div_u64((uint64_t)alloc_size * y_data_rate,
3514 total_data_rate);
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003515
Matt Roperc107acf2016-05-12 07:06:01 -07003516 if (y_data_rate) {
3517 ddb->y_plane[pipe][id].start = start;
3518 ddb->y_plane[pipe][id].end = start + y_plane_blocks;
3519 }
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003520
Matt Ropera1de91e2016-05-12 07:05:57 -07003521 start += y_plane_blocks;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003522 }
3523
Matt Roperc107acf2016-05-12 07:06:01 -07003524 return 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003525}
3526
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003527/*
3528 * 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 +02003529 * for the read latency) and cpp should always be <= 8, so that
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003530 * should allow pixel_rate up to ~2 GHz which seems sufficient since max
3531 * 2xcdclk is 1350 MHz and the pixel rate should never exceed that.
3532*/
Ville Syrjäläac484962016-01-20 21:05:26 +02003533static uint32_t skl_wm_method1(uint32_t pixel_rate, uint8_t cpp, uint32_t latency)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003534{
3535 uint32_t wm_intermediate_val, ret;
3536
3537 if (latency == 0)
3538 return UINT_MAX;
3539
Ville Syrjäläac484962016-01-20 21:05:26 +02003540 wm_intermediate_val = latency * pixel_rate * cpp / 512;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003541 ret = DIV_ROUND_UP(wm_intermediate_val, 1000);
3542
3543 return ret;
3544}
3545
3546static uint32_t skl_wm_method2(uint32_t pixel_rate, uint32_t pipe_htotal,
Paulo Zanoni7a1a8ae2016-09-22 18:00:32 -03003547 uint32_t latency, uint32_t plane_blocks_per_line)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003548{
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00003549 uint32_t ret;
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00003550 uint32_t wm_intermediate_val;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003551
3552 if (latency == 0)
3553 return UINT_MAX;
3554
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003555 wm_intermediate_val = latency * pixel_rate;
3556 ret = DIV_ROUND_UP(wm_intermediate_val, pipe_htotal * 1000) *
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00003557 plane_blocks_per_line;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003558
3559 return ret;
3560}
3561
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07003562static uint32_t skl_adjusted_plane_pixel_rate(const struct intel_crtc_state *cstate,
3563 struct intel_plane_state *pstate)
3564{
3565 uint64_t adjusted_pixel_rate;
3566 uint64_t downscale_amount;
3567 uint64_t pixel_rate;
3568
3569 /* Shouldn't reach here on disabled planes... */
Ville Syrjälä936e71e2016-07-26 19:06:59 +03003570 if (WARN_ON(!pstate->base.visible))
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07003571 return 0;
3572
3573 /*
3574 * Adjusted plane pixel rate is just the pipe's adjusted pixel rate
3575 * with additional adjustments for plane-specific scaling.
3576 */
Paulo Zanonicfd7e3a2016-10-07 17:28:57 -03003577 adjusted_pixel_rate = ilk_pipe_pixel_rate(cstate);
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07003578 downscale_amount = skl_plane_downscale_amount(pstate);
3579
3580 pixel_rate = adjusted_pixel_rate * downscale_amount >> 16;
3581 WARN_ON(pixel_rate != clamp_t(uint32_t, pixel_rate, 0, ~0));
3582
3583 return pixel_rate;
3584}
3585
Matt Roper55994c22016-05-12 07:06:08 -07003586static int skl_compute_plane_wm(const struct drm_i915_private *dev_priv,
3587 struct intel_crtc_state *cstate,
3588 struct intel_plane_state *intel_pstate,
3589 uint16_t ddb_allocation,
3590 int level,
3591 uint16_t *out_blocks, /* out */
3592 uint8_t *out_lines, /* out */
3593 bool *enabled /* out */)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003594{
Matt Roper33815fa2016-05-12 07:06:05 -07003595 struct drm_plane_state *pstate = &intel_pstate->base;
3596 struct drm_framebuffer *fb = pstate->fb;
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00003597 uint32_t latency = dev_priv->wm.skl_latency[level];
3598 uint32_t method1, method2;
3599 uint32_t plane_bytes_per_line, plane_blocks_per_line;
3600 uint32_t res_blocks, res_lines;
3601 uint32_t selected_result;
Ville Syrjäläac484962016-01-20 21:05:26 +02003602 uint8_t cpp;
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07003603 uint32_t width = 0, height = 0;
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07003604 uint32_t plane_pixel_rate;
Paulo Zanoni75676ed2016-09-22 18:00:33 -03003605 uint32_t y_tile_minimum, y_min_scanlines;
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003606 struct intel_atomic_state *state =
3607 to_intel_atomic_state(cstate->base.state);
3608 bool apply_memory_bw_wa = skl_needs_memory_bw_wa(state);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003609
Ville Syrjälä936e71e2016-07-26 19:06:59 +03003610 if (latency == 0 || !cstate->base.active || !intel_pstate->base.visible) {
Matt Roper55994c22016-05-12 07:06:08 -07003611 *enabled = false;
3612 return 0;
3613 }
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003614
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003615 if (apply_memory_bw_wa && fb->modifier[0] == I915_FORMAT_MOD_X_TILED)
3616 latency += 15;
3617
Ville Syrjälä936e71e2016-07-26 19:06:59 +03003618 width = drm_rect_width(&intel_pstate->base.src) >> 16;
3619 height = drm_rect_height(&intel_pstate->base.src) >> 16;
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07003620
Ville Syrjäläbd2ef252016-09-26 19:30:46 +03003621 if (drm_rotation_90_or_270(pstate->rotation))
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07003622 swap(width, height);
3623
Ville Syrjäläac484962016-01-20 21:05:26 +02003624 cpp = drm_format_plane_cpp(fb->pixel_format, 0);
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07003625 plane_pixel_rate = skl_adjusted_plane_pixel_rate(cstate, intel_pstate);
3626
Dave Airlie61d0a042016-10-25 16:35:20 +10003627 if (drm_rotation_90_or_270(pstate->rotation)) {
Paulo Zanoni1186fa82016-09-22 18:00:31 -03003628 int cpp = (fb->pixel_format == DRM_FORMAT_NV12) ?
3629 drm_format_plane_cpp(fb->pixel_format, 1) :
3630 drm_format_plane_cpp(fb->pixel_format, 0);
3631
3632 switch (cpp) {
3633 case 1:
3634 y_min_scanlines = 16;
3635 break;
3636 case 2:
3637 y_min_scanlines = 8;
3638 break;
Paulo Zanoni1186fa82016-09-22 18:00:31 -03003639 case 4:
3640 y_min_scanlines = 4;
3641 break;
Paulo Zanoni86a462b2016-09-22 18:00:35 -03003642 default:
3643 MISSING_CASE(cpp);
3644 return -EINVAL;
Paulo Zanoni1186fa82016-09-22 18:00:31 -03003645 }
3646 } else {
3647 y_min_scanlines = 4;
3648 }
3649
Paulo Zanoni7a1a8ae2016-09-22 18:00:32 -03003650 plane_bytes_per_line = width * cpp;
3651 if (fb->modifier[0] == I915_FORMAT_MOD_Y_TILED ||
3652 fb->modifier[0] == I915_FORMAT_MOD_Yf_TILED) {
3653 plane_blocks_per_line =
3654 DIV_ROUND_UP(plane_bytes_per_line * y_min_scanlines, 512);
3655 plane_blocks_per_line /= y_min_scanlines;
3656 } else if (fb->modifier[0] == DRM_FORMAT_MOD_NONE) {
3657 plane_blocks_per_line = DIV_ROUND_UP(plane_bytes_per_line, 512)
3658 + 1;
3659 } else {
3660 plane_blocks_per_line = DIV_ROUND_UP(plane_bytes_per_line, 512);
3661 }
3662
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07003663 method1 = skl_wm_method1(plane_pixel_rate, cpp, latency);
3664 method2 = skl_wm_method2(plane_pixel_rate,
Matt Roper024c9042015-09-24 15:53:11 -07003665 cstate->base.adjusted_mode.crtc_htotal,
Paulo Zanoni1186fa82016-09-22 18:00:31 -03003666 latency,
Paulo Zanoni7a1a8ae2016-09-22 18:00:32 -03003667 plane_blocks_per_line);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003668
Paulo Zanoni75676ed2016-09-22 18:00:33 -03003669 y_tile_minimum = plane_blocks_per_line * y_min_scanlines;
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003670 if (apply_memory_bw_wa)
3671 y_tile_minimum *= 2;
Paulo Zanoni75676ed2016-09-22 18:00:33 -03003672
Matt Roper024c9042015-09-24 15:53:11 -07003673 if (fb->modifier[0] == I915_FORMAT_MOD_Y_TILED ||
3674 fb->modifier[0] == I915_FORMAT_MOD_Yf_TILED) {
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00003675 selected_result = max(method2, y_tile_minimum);
3676 } else {
Paulo Zanonif1db3ea2016-09-22 18:00:34 -03003677 if ((cpp * cstate->base.adjusted_mode.crtc_htotal / 512 < 1) &&
3678 (plane_bytes_per_line / 512 < 1))
3679 selected_result = method2;
3680 else if ((ddb_allocation / plane_blocks_per_line) >= 1)
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00003681 selected_result = min(method1, method2);
3682 else
3683 selected_result = method1;
3684 }
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003685
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00003686 res_blocks = selected_result + 1;
3687 res_lines = DIV_ROUND_UP(selected_result, plane_blocks_per_line);
Damien Lespiaue6d66172014-11-04 17:06:55 +00003688
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00003689 if (level >= 1 && level <= 7) {
Matt Roper024c9042015-09-24 15:53:11 -07003690 if (fb->modifier[0] == I915_FORMAT_MOD_Y_TILED ||
Paulo Zanoni75676ed2016-09-22 18:00:33 -03003691 fb->modifier[0] == I915_FORMAT_MOD_Yf_TILED) {
3692 res_blocks += y_tile_minimum;
Paulo Zanoni1186fa82016-09-22 18:00:31 -03003693 res_lines += y_min_scanlines;
Paulo Zanoni75676ed2016-09-22 18:00:33 -03003694 } else {
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00003695 res_blocks++;
Paulo Zanoni75676ed2016-09-22 18:00:33 -03003696 }
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00003697 }
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00003698
Matt Roper55994c22016-05-12 07:06:08 -07003699 if (res_blocks >= ddb_allocation || res_lines > 31) {
3700 *enabled = false;
Matt Roper6b6bada2016-05-12 07:06:10 -07003701
3702 /*
3703 * If there are no valid level 0 watermarks, then we can't
3704 * support this display configuration.
3705 */
3706 if (level) {
3707 return 0;
3708 } else {
3709 DRM_DEBUG_KMS("Requested display configuration exceeds system watermark limitations\n");
3710 DRM_DEBUG_KMS("Plane %d.%d: blocks required = %u/%u, lines required = %u/31\n",
3711 to_intel_crtc(cstate->base.crtc)->pipe,
3712 skl_wm_plane_id(to_intel_plane(pstate->plane)),
3713 res_blocks, ddb_allocation, res_lines);
3714
3715 return -EINVAL;
3716 }
Matt Roper55994c22016-05-12 07:06:08 -07003717 }
Damien Lespiaue6d66172014-11-04 17:06:55 +00003718
3719 *out_blocks = res_blocks;
3720 *out_lines = res_lines;
Matt Roper55994c22016-05-12 07:06:08 -07003721 *enabled = true;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003722
Matt Roper55994c22016-05-12 07:06:08 -07003723 return 0;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003724}
3725
Matt Roperf4a96752016-05-12 07:06:06 -07003726static int
3727skl_compute_wm_level(const struct drm_i915_private *dev_priv,
3728 struct skl_ddb_allocation *ddb,
3729 struct intel_crtc_state *cstate,
Lyudea62163e2016-10-04 14:28:20 -04003730 struct intel_plane *intel_plane,
Matt Roperf4a96752016-05-12 07:06:06 -07003731 int level,
3732 struct skl_wm_level *result)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003733{
Matt Roperf4a96752016-05-12 07:06:06 -07003734 struct drm_atomic_state *state = cstate->base.state;
Matt Roper024c9042015-09-24 15:53:11 -07003735 struct intel_crtc *intel_crtc = to_intel_crtc(cstate->base.crtc);
Lyudea62163e2016-10-04 14:28:20 -04003736 struct drm_plane *plane = &intel_plane->base;
3737 struct intel_plane_state *intel_pstate = NULL;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003738 uint16_t ddb_blocks;
Matt Roper024c9042015-09-24 15:53:11 -07003739 enum pipe pipe = intel_crtc->pipe;
Matt Roper55994c22016-05-12 07:06:08 -07003740 int ret;
Lyudea62163e2016-10-04 14:28:20 -04003741 int i = skl_wm_plane_id(intel_plane);
3742
3743 if (state)
3744 intel_pstate =
3745 intel_atomic_get_existing_plane_state(state,
3746 intel_plane);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003747
Matt Roperf4a96752016-05-12 07:06:06 -07003748 /*
Lyudea62163e2016-10-04 14:28:20 -04003749 * Note: If we start supporting multiple pending atomic commits against
3750 * the same planes/CRTC's in the future, plane->state will no longer be
3751 * the correct pre-state to use for the calculations here and we'll
3752 * need to change where we get the 'unchanged' plane data from.
3753 *
3754 * For now this is fine because we only allow one queued commit against
3755 * a CRTC. Even if the plane isn't modified by this transaction and we
3756 * don't have a plane lock, we still have the CRTC's lock, so we know
3757 * that no other transactions are racing with us to update it.
Matt Roperf4a96752016-05-12 07:06:06 -07003758 */
Lyudea62163e2016-10-04 14:28:20 -04003759 if (!intel_pstate)
3760 intel_pstate = to_intel_plane_state(plane->state);
Matt Roperf4a96752016-05-12 07:06:06 -07003761
Lyudea62163e2016-10-04 14:28:20 -04003762 WARN_ON(!intel_pstate->base.fb);
Matt Roper024c9042015-09-24 15:53:11 -07003763
Lyudea62163e2016-10-04 14:28:20 -04003764 ddb_blocks = skl_ddb_entry_size(&ddb->plane[pipe][i]);
Matt Roperf4a96752016-05-12 07:06:06 -07003765
Lyudea62163e2016-10-04 14:28:20 -04003766 ret = skl_compute_plane_wm(dev_priv,
3767 cstate,
3768 intel_pstate,
3769 ddb_blocks,
3770 level,
3771 &result->plane_res_b,
3772 &result->plane_res_l,
3773 &result->plane_en);
3774 if (ret)
3775 return ret;
Matt Roperf4a96752016-05-12 07:06:06 -07003776
3777 return 0;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003778}
3779
Damien Lespiau407b50f2014-11-04 17:06:57 +00003780static uint32_t
Matt Roper024c9042015-09-24 15:53:11 -07003781skl_compute_linetime_wm(struct intel_crtc_state *cstate)
Damien Lespiau407b50f2014-11-04 17:06:57 +00003782{
Paulo Zanoni30d1b5f2016-10-07 17:28:58 -03003783 uint32_t pixel_rate;
3784
Matt Roper024c9042015-09-24 15:53:11 -07003785 if (!cstate->base.active)
Damien Lespiau407b50f2014-11-04 17:06:57 +00003786 return 0;
3787
Paulo Zanoni30d1b5f2016-10-07 17:28:58 -03003788 pixel_rate = ilk_pipe_pixel_rate(cstate);
3789
3790 if (WARN_ON(pixel_rate == 0))
Mika Kuoppala661abfc2015-07-16 19:36:51 +03003791 return 0;
Damien Lespiau407b50f2014-11-04 17:06:57 +00003792
Matt Roper024c9042015-09-24 15:53:11 -07003793 return DIV_ROUND_UP(8 * cstate->base.adjusted_mode.crtc_htotal * 1000,
Paulo Zanoni30d1b5f2016-10-07 17:28:58 -03003794 pixel_rate);
Damien Lespiau407b50f2014-11-04 17:06:57 +00003795}
3796
Matt Roper024c9042015-09-24 15:53:11 -07003797static void skl_compute_transition_wm(struct intel_crtc_state *cstate,
Damien Lespiau9414f562014-11-04 17:06:58 +00003798 struct skl_wm_level *trans_wm /* out */)
Damien Lespiau407b50f2014-11-04 17:06:57 +00003799{
Matt Roper024c9042015-09-24 15:53:11 -07003800 if (!cstate->base.active)
Damien Lespiau407b50f2014-11-04 17:06:57 +00003801 return;
Damien Lespiau9414f562014-11-04 17:06:58 +00003802
3803 /* Until we know more, just disable transition WMs */
Lyudea62163e2016-10-04 14:28:20 -04003804 trans_wm->plane_en = false;
Damien Lespiau407b50f2014-11-04 17:06:57 +00003805}
3806
Matt Roper55994c22016-05-12 07:06:08 -07003807static int skl_build_pipe_wm(struct intel_crtc_state *cstate,
3808 struct skl_ddb_allocation *ddb,
3809 struct skl_pipe_wm *pipe_wm)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003810{
Matt Roper024c9042015-09-24 15:53:11 -07003811 struct drm_device *dev = cstate->base.crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003812 const struct drm_i915_private *dev_priv = to_i915(dev);
Lyudea62163e2016-10-04 14:28:20 -04003813 struct intel_plane *intel_plane;
3814 struct skl_plane_wm *wm;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003815 int level, max_level = ilk_wm_max_level(dev_priv);
Matt Roper55994c22016-05-12 07:06:08 -07003816 int ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003817
Lyudea62163e2016-10-04 14:28:20 -04003818 /*
3819 * We'll only calculate watermarks for planes that are actually
3820 * enabled, so make sure all other planes are set as disabled.
3821 */
3822 memset(pipe_wm->planes, 0, sizeof(pipe_wm->planes));
3823
3824 for_each_intel_plane_mask(&dev_priv->drm,
3825 intel_plane,
3826 cstate->base.plane_mask) {
3827 wm = &pipe_wm->planes[skl_wm_plane_id(intel_plane)];
3828
3829 for (level = 0; level <= max_level; level++) {
3830 ret = skl_compute_wm_level(dev_priv, ddb, cstate,
3831 intel_plane, level,
3832 &wm->wm[level]);
3833 if (ret)
3834 return ret;
3835 }
3836 skl_compute_transition_wm(cstate, &wm->trans_wm);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003837 }
Matt Roper024c9042015-09-24 15:53:11 -07003838 pipe_wm->linetime = skl_compute_linetime_wm(cstate);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003839
Matt Roper55994c22016-05-12 07:06:08 -07003840 return 0;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003841}
3842
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003843static void skl_ddb_entry_write(struct drm_i915_private *dev_priv,
3844 i915_reg_t reg,
Damien Lespiau16160e32014-11-04 17:06:53 +00003845 const struct skl_ddb_entry *entry)
3846{
3847 if (entry->end)
3848 I915_WRITE(reg, (entry->end - 1) << 16 | entry->start);
3849 else
3850 I915_WRITE(reg, 0);
3851}
3852
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003853static void skl_write_wm_level(struct drm_i915_private *dev_priv,
3854 i915_reg_t reg,
3855 const struct skl_wm_level *level)
3856{
3857 uint32_t val = 0;
3858
3859 if (level->plane_en) {
3860 val |= PLANE_WM_EN;
3861 val |= level->plane_res_b;
3862 val |= level->plane_res_l << PLANE_WM_LINES_SHIFT;
3863 }
3864
3865 I915_WRITE(reg, val);
3866}
3867
Lyude62e0fb82016-08-22 12:50:08 -04003868void skl_write_plane_wm(struct intel_crtc *intel_crtc,
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003869 const struct skl_plane_wm *wm,
3870 const struct skl_ddb_allocation *ddb,
Lyude62e0fb82016-08-22 12:50:08 -04003871 int plane)
3872{
3873 struct drm_crtc *crtc = &intel_crtc->base;
3874 struct drm_device *dev = crtc->dev;
3875 struct drm_i915_private *dev_priv = to_i915(dev);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003876 int level, max_level = ilk_wm_max_level(dev_priv);
Lyude62e0fb82016-08-22 12:50:08 -04003877 enum pipe pipe = intel_crtc->pipe;
3878
3879 for (level = 0; level <= max_level; level++) {
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003880 skl_write_wm_level(dev_priv, PLANE_WM(pipe, plane, level),
3881 &wm->wm[level]);
Lyude62e0fb82016-08-22 12:50:08 -04003882 }
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003883 skl_write_wm_level(dev_priv, PLANE_WM_TRANS(pipe, plane),
3884 &wm->trans_wm);
Lyude27082492016-08-24 07:48:10 +02003885
3886 skl_ddb_entry_write(dev_priv, PLANE_BUF_CFG(pipe, plane),
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003887 &ddb->plane[pipe][plane]);
Lyude27082492016-08-24 07:48:10 +02003888 skl_ddb_entry_write(dev_priv, PLANE_NV12_BUF_CFG(pipe, plane),
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003889 &ddb->y_plane[pipe][plane]);
Lyude62e0fb82016-08-22 12:50:08 -04003890}
3891
3892void skl_write_cursor_wm(struct intel_crtc *intel_crtc,
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003893 const struct skl_plane_wm *wm,
3894 const struct skl_ddb_allocation *ddb)
Lyude62e0fb82016-08-22 12:50:08 -04003895{
3896 struct drm_crtc *crtc = &intel_crtc->base;
3897 struct drm_device *dev = crtc->dev;
3898 struct drm_i915_private *dev_priv = to_i915(dev);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003899 int level, max_level = ilk_wm_max_level(dev_priv);
Lyude62e0fb82016-08-22 12:50:08 -04003900 enum pipe pipe = intel_crtc->pipe;
3901
3902 for (level = 0; level <= max_level; level++) {
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003903 skl_write_wm_level(dev_priv, CUR_WM(pipe, level),
3904 &wm->wm[level]);
Lyude62e0fb82016-08-22 12:50:08 -04003905 }
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003906 skl_write_wm_level(dev_priv, CUR_WM_TRANS(pipe), &wm->trans_wm);
Lyude27082492016-08-24 07:48:10 +02003907
3908 skl_ddb_entry_write(dev_priv, CUR_BUF_CFG(pipe),
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003909 &ddb->plane[pipe][PLANE_CURSOR]);
Lyude62e0fb82016-08-22 12:50:08 -04003910}
3911
cpaul@redhat.com45ece232016-10-14 17:31:56 -04003912bool skl_wm_level_equals(const struct skl_wm_level *l1,
3913 const struct skl_wm_level *l2)
3914{
3915 if (l1->plane_en != l2->plane_en)
3916 return false;
3917
3918 /* If both planes aren't enabled, the rest shouldn't matter */
3919 if (!l1->plane_en)
3920 return true;
3921
3922 return (l1->plane_res_l == l2->plane_res_l &&
3923 l1->plane_res_b == l2->plane_res_b);
3924}
3925
Lyude27082492016-08-24 07:48:10 +02003926static inline bool skl_ddb_entries_overlap(const struct skl_ddb_entry *a,
3927 const struct skl_ddb_entry *b)
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003928{
Lyude27082492016-08-24 07:48:10 +02003929 return a->start < b->end && b->start < a->end;
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003930}
3931
Lyude27082492016-08-24 07:48:10 +02003932bool skl_ddb_allocation_overlaps(struct drm_atomic_state *state,
Lyudece0ba282016-09-15 10:46:35 -04003933 struct intel_crtc *intel_crtc)
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003934{
Lyudece0ba282016-09-15 10:46:35 -04003935 struct drm_crtc *other_crtc;
3936 struct drm_crtc_state *other_cstate;
3937 struct intel_crtc *other_intel_crtc;
3938 const struct skl_ddb_entry *ddb =
3939 &to_intel_crtc_state(intel_crtc->base.state)->wm.skl.ddb;
3940 int i;
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003941
Lyudece0ba282016-09-15 10:46:35 -04003942 for_each_crtc_in_state(state, other_crtc, other_cstate, i) {
3943 other_intel_crtc = to_intel_crtc(other_crtc);
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003944
Lyudece0ba282016-09-15 10:46:35 -04003945 if (other_intel_crtc == intel_crtc)
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003946 continue;
3947
Lyudece0ba282016-09-15 10:46:35 -04003948 if (skl_ddb_entries_overlap(ddb, &other_intel_crtc->hw_ddb))
Lyude27082492016-08-24 07:48:10 +02003949 return true;
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003950 }
3951
Lyude27082492016-08-24 07:48:10 +02003952 return false;
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003953}
3954
Matt Roper55994c22016-05-12 07:06:08 -07003955static int skl_update_pipe_wm(struct drm_crtc_state *cstate,
Maarten Lankhorst03af79e2016-10-26 15:41:36 +02003956 const struct skl_pipe_wm *old_pipe_wm,
Matt Roper55994c22016-05-12 07:06:08 -07003957 struct skl_pipe_wm *pipe_wm, /* out */
Maarten Lankhorst03af79e2016-10-26 15:41:36 +02003958 struct skl_ddb_allocation *ddb, /* out */
Matt Roper55994c22016-05-12 07:06:08 -07003959 bool *changed /* out */)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003960{
Matt Roperf4a96752016-05-12 07:06:06 -07003961 struct intel_crtc_state *intel_cstate = to_intel_crtc_state(cstate);
Matt Roper55994c22016-05-12 07:06:08 -07003962 int ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003963
Matt Roper55994c22016-05-12 07:06:08 -07003964 ret = skl_build_pipe_wm(intel_cstate, ddb, pipe_wm);
3965 if (ret)
3966 return ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003967
Maarten Lankhorst03af79e2016-10-26 15:41:36 +02003968 if (!memcmp(old_pipe_wm, pipe_wm, sizeof(*pipe_wm)))
Matt Roper55994c22016-05-12 07:06:08 -07003969 *changed = false;
3970 else
3971 *changed = true;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003972
Matt Roper55994c22016-05-12 07:06:08 -07003973 return 0;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003974}
3975
Matt Roper9b613022016-06-27 16:42:44 -07003976static uint32_t
3977pipes_modified(struct drm_atomic_state *state)
3978{
3979 struct drm_crtc *crtc;
3980 struct drm_crtc_state *cstate;
3981 uint32_t i, ret = 0;
3982
3983 for_each_crtc_in_state(state, crtc, cstate, i)
3984 ret |= drm_crtc_mask(crtc);
3985
3986 return ret;
3987}
3988
Jani Nikulabb7791b2016-10-04 12:29:17 +03003989static int
Paulo Zanoni7f60e202016-09-29 16:36:48 -03003990skl_ddb_add_affected_planes(struct intel_crtc_state *cstate)
3991{
3992 struct drm_atomic_state *state = cstate->base.state;
3993 struct drm_device *dev = state->dev;
3994 struct drm_crtc *crtc = cstate->base.crtc;
3995 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3996 struct drm_i915_private *dev_priv = to_i915(dev);
3997 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
3998 struct skl_ddb_allocation *new_ddb = &intel_state->wm_results.ddb;
3999 struct skl_ddb_allocation *cur_ddb = &dev_priv->wm.skl_hw.ddb;
4000 struct drm_plane_state *plane_state;
4001 struct drm_plane *plane;
4002 enum pipe pipe = intel_crtc->pipe;
4003 int id;
4004
4005 WARN_ON(!drm_atomic_get_existing_crtc_state(state, crtc));
4006
Maarten Lankhorst220b0962016-10-26 15:41:30 +02004007 drm_for_each_plane_mask(plane, dev, cstate->base.plane_mask) {
Paulo Zanoni7f60e202016-09-29 16:36:48 -03004008 id = skl_wm_plane_id(to_intel_plane(plane));
4009
4010 if (skl_ddb_entry_equal(&cur_ddb->plane[pipe][id],
4011 &new_ddb->plane[pipe][id]) &&
4012 skl_ddb_entry_equal(&cur_ddb->y_plane[pipe][id],
4013 &new_ddb->y_plane[pipe][id]))
4014 continue;
4015
4016 plane_state = drm_atomic_get_plane_state(state, plane);
4017 if (IS_ERR(plane_state))
4018 return PTR_ERR(plane_state);
4019 }
4020
4021 return 0;
4022}
4023
Matt Roper98d39492016-05-12 07:06:03 -07004024static int
4025skl_compute_ddb(struct drm_atomic_state *state)
4026{
4027 struct drm_device *dev = state->dev;
4028 struct drm_i915_private *dev_priv = to_i915(dev);
4029 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
4030 struct intel_crtc *intel_crtc;
Matt Roper734fa012016-05-12 15:11:40 -07004031 struct skl_ddb_allocation *ddb = &intel_state->wm_results.ddb;
Matt Roper9b613022016-06-27 16:42:44 -07004032 uint32_t realloc_pipes = pipes_modified(state);
Matt Roper98d39492016-05-12 07:06:03 -07004033 int ret;
4034
4035 /*
4036 * If this is our first atomic update following hardware readout,
4037 * we can't trust the DDB that the BIOS programmed for us. Let's
4038 * pretend that all pipes switched active status so that we'll
4039 * ensure a full DDB recompute.
4040 */
Matt Roper1b54a882016-06-17 13:42:18 -07004041 if (dev_priv->wm.distrust_bios_wm) {
4042 ret = drm_modeset_lock(&dev->mode_config.connection_mutex,
4043 state->acquire_ctx);
4044 if (ret)
4045 return ret;
4046
Matt Roper98d39492016-05-12 07:06:03 -07004047 intel_state->active_pipe_changes = ~0;
4048
Matt Roper1b54a882016-06-17 13:42:18 -07004049 /*
4050 * We usually only initialize intel_state->active_crtcs if we
4051 * we're doing a modeset; make sure this field is always
4052 * initialized during the sanitization process that happens
4053 * on the first commit too.
4054 */
4055 if (!intel_state->modeset)
4056 intel_state->active_crtcs = dev_priv->active_crtcs;
4057 }
4058
Matt Roper98d39492016-05-12 07:06:03 -07004059 /*
4060 * If the modeset changes which CRTC's are active, we need to
4061 * recompute the DDB allocation for *all* active pipes, even
4062 * those that weren't otherwise being modified in any way by this
4063 * atomic commit. Due to the shrinking of the per-pipe allocations
4064 * when new active CRTC's are added, it's possible for a pipe that
4065 * we were already using and aren't changing at all here to suddenly
4066 * become invalid if its DDB needs exceeds its new allocation.
4067 *
4068 * Note that if we wind up doing a full DDB recompute, we can't let
4069 * any other display updates race with this transaction, so we need
4070 * to grab the lock on *all* CRTC's.
4071 */
Matt Roper734fa012016-05-12 15:11:40 -07004072 if (intel_state->active_pipe_changes) {
Matt Roper98d39492016-05-12 07:06:03 -07004073 realloc_pipes = ~0;
Matt Roper734fa012016-05-12 15:11:40 -07004074 intel_state->wm_results.dirty_pipes = ~0;
4075 }
Matt Roper98d39492016-05-12 07:06:03 -07004076
Paulo Zanoni5a920b82016-10-04 14:37:32 -03004077 /*
4078 * We're not recomputing for the pipes not included in the commit, so
4079 * make sure we start with the current state.
4080 */
4081 memcpy(ddb, &dev_priv->wm.skl_hw.ddb, sizeof(*ddb));
4082
Matt Roper98d39492016-05-12 07:06:03 -07004083 for_each_intel_crtc_mask(dev, intel_crtc, realloc_pipes) {
4084 struct intel_crtc_state *cstate;
4085
4086 cstate = intel_atomic_get_crtc_state(state, intel_crtc);
4087 if (IS_ERR(cstate))
4088 return PTR_ERR(cstate);
4089
Matt Roper734fa012016-05-12 15:11:40 -07004090 ret = skl_allocate_pipe_ddb(cstate, ddb);
Matt Roper98d39492016-05-12 07:06:03 -07004091 if (ret)
4092 return ret;
Lyude05a76d32016-08-17 15:55:57 -04004093
Paulo Zanoni7f60e202016-09-29 16:36:48 -03004094 ret = skl_ddb_add_affected_planes(cstate);
Lyude05a76d32016-08-17 15:55:57 -04004095 if (ret)
4096 return ret;
Matt Roper98d39492016-05-12 07:06:03 -07004097 }
4098
4099 return 0;
4100}
4101
Matt Roper2722efb2016-08-17 15:55:55 -04004102static void
4103skl_copy_wm_for_pipe(struct skl_wm_values *dst,
4104 struct skl_wm_values *src,
4105 enum pipe pipe)
4106{
Matt Roper2722efb2016-08-17 15:55:55 -04004107 memcpy(dst->ddb.y_plane[pipe], src->ddb.y_plane[pipe],
4108 sizeof(dst->ddb.y_plane[pipe]));
4109 memcpy(dst->ddb.plane[pipe], src->ddb.plane[pipe],
4110 sizeof(dst->ddb.plane[pipe]));
4111}
4112
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004113static void
4114skl_print_wm_changes(const struct drm_atomic_state *state)
4115{
4116 const struct drm_device *dev = state->dev;
4117 const struct drm_i915_private *dev_priv = to_i915(dev);
4118 const struct intel_atomic_state *intel_state =
4119 to_intel_atomic_state(state);
4120 const struct drm_crtc *crtc;
4121 const struct drm_crtc_state *cstate;
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004122 const struct intel_plane *intel_plane;
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004123 const struct skl_ddb_allocation *old_ddb = &dev_priv->wm.skl_hw.ddb;
4124 const struct skl_ddb_allocation *new_ddb = &intel_state->wm_results.ddb;
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004125 int id;
Maarten Lankhorst75704982016-11-01 12:04:10 +01004126 int i;
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004127
4128 for_each_crtc_in_state(state, crtc, cstate, i) {
Maarten Lankhorst75704982016-11-01 12:04:10 +01004129 const struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4130 enum pipe pipe = intel_crtc->pipe;
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004131
Maarten Lankhorst75704982016-11-01 12:04:10 +01004132 for_each_intel_plane_on_crtc(dev, intel_crtc, intel_plane) {
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004133 const struct skl_ddb_entry *old, *new;
4134
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004135 id = skl_wm_plane_id(intel_plane);
4136 old = &old_ddb->plane[pipe][id];
4137 new = &new_ddb->plane[pipe][id];
4138
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004139 if (skl_ddb_entry_equal(old, new))
4140 continue;
4141
Maarten Lankhorst75704982016-11-01 12:04:10 +01004142 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] ddb (%d - %d) -> (%d - %d)\n",
4143 intel_plane->base.base.id,
4144 intel_plane->base.name,
4145 old->start, old->end,
4146 new->start, new->end);
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004147 }
4148 }
4149}
4150
Matt Roper98d39492016-05-12 07:06:03 -07004151static int
4152skl_compute_wm(struct drm_atomic_state *state)
4153{
4154 struct drm_crtc *crtc;
4155 struct drm_crtc_state *cstate;
Matt Roper734fa012016-05-12 15:11:40 -07004156 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
4157 struct skl_wm_values *results = &intel_state->wm_results;
4158 struct skl_pipe_wm *pipe_wm;
Matt Roper98d39492016-05-12 07:06:03 -07004159 bool changed = false;
Matt Roper734fa012016-05-12 15:11:40 -07004160 int ret, i;
Matt Roper98d39492016-05-12 07:06:03 -07004161
4162 /*
4163 * If this transaction isn't actually touching any CRTC's, don't
4164 * bother with watermark calculation. Note that if we pass this
4165 * test, we're guaranteed to hold at least one CRTC state mutex,
4166 * which means we can safely use values like dev_priv->active_crtcs
4167 * since any racing commits that want to update them would need to
4168 * hold _all_ CRTC state mutexes.
4169 */
4170 for_each_crtc_in_state(state, crtc, cstate, i)
4171 changed = true;
4172 if (!changed)
4173 return 0;
4174
Matt Roper734fa012016-05-12 15:11:40 -07004175 /* Clear all dirty flags */
4176 results->dirty_pipes = 0;
4177
Matt Roper98d39492016-05-12 07:06:03 -07004178 ret = skl_compute_ddb(state);
4179 if (ret)
4180 return ret;
4181
Matt Roper734fa012016-05-12 15:11:40 -07004182 /*
4183 * Calculate WM's for all pipes that are part of this transaction.
4184 * Note that the DDB allocation above may have added more CRTC's that
4185 * weren't otherwise being modified (and set bits in dirty_pipes) if
4186 * pipe allocations had to change.
4187 *
4188 * FIXME: Now that we're doing this in the atomic check phase, we
4189 * should allow skl_update_pipe_wm() to return failure in cases where
4190 * no suitable watermark values can be found.
4191 */
4192 for_each_crtc_in_state(state, crtc, cstate, i) {
Matt Roper734fa012016-05-12 15:11:40 -07004193 struct intel_crtc_state *intel_cstate =
4194 to_intel_crtc_state(cstate);
Maarten Lankhorst03af79e2016-10-26 15:41:36 +02004195 const struct skl_pipe_wm *old_pipe_wm =
4196 &to_intel_crtc_state(crtc->state)->wm.skl.optimal;
Matt Roper734fa012016-05-12 15:11:40 -07004197
4198 pipe_wm = &intel_cstate->wm.skl.optimal;
Maarten Lankhorst03af79e2016-10-26 15:41:36 +02004199 ret = skl_update_pipe_wm(cstate, old_pipe_wm, pipe_wm,
4200 &results->ddb, &changed);
Matt Roper734fa012016-05-12 15:11:40 -07004201 if (ret)
4202 return ret;
4203
4204 if (changed)
4205 results->dirty_pipes |= drm_crtc_mask(crtc);
4206
4207 if ((results->dirty_pipes & drm_crtc_mask(crtc)) == 0)
4208 /* This pipe's WM's did not change */
4209 continue;
4210
4211 intel_cstate->update_wm_pre = true;
Matt Roper734fa012016-05-12 15:11:40 -07004212 }
4213
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004214 skl_print_wm_changes(state);
4215
Matt Roper98d39492016-05-12 07:06:03 -07004216 return 0;
4217}
4218
Ville Syrjälä432081b2016-10-31 22:37:03 +02004219static void skl_update_wm(struct intel_crtc *intel_crtc)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004220{
Ville Syrjälä432081b2016-10-31 22:37:03 +02004221 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004222 struct drm_i915_private *dev_priv = to_i915(dev);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004223 struct skl_wm_values *results = &dev_priv->wm.skl_results;
Matt Roper2722efb2016-08-17 15:55:55 -04004224 struct skl_wm_values *hw_vals = &dev_priv->wm.skl_hw;
Ville Syrjälä432081b2016-10-31 22:37:03 +02004225 struct intel_crtc_state *cstate = to_intel_crtc_state(intel_crtc->base.state);
Matt Ropere8f1f022016-05-12 07:05:55 -07004226 struct skl_pipe_wm *pipe_wm = &cstate->wm.skl.optimal;
Lyude27082492016-08-24 07:48:10 +02004227 enum pipe pipe = intel_crtc->pipe;
Bob Paauweadda50b2015-07-21 10:42:53 -07004228
Ville Syrjälä432081b2016-10-31 22:37:03 +02004229 if ((results->dirty_pipes & drm_crtc_mask(&intel_crtc->base)) == 0)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004230 return;
4231
Matt Roper734fa012016-05-12 15:11:40 -07004232 mutex_lock(&dev_priv->wm.wm_mutex);
4233
Matt Roper2722efb2016-08-17 15:55:55 -04004234 /*
Lyude27082492016-08-24 07:48:10 +02004235 * If this pipe isn't active already, we're going to be enabling it
4236 * very soon. Since it's safe to update a pipe's ddb allocation while
4237 * the pipe's shut off, just do so here. Already active pipes will have
4238 * their watermarks updated once we update their planes.
Matt Roper2722efb2016-08-17 15:55:55 -04004239 */
Ville Syrjälä432081b2016-10-31 22:37:03 +02004240 if (intel_crtc->base.state->active_changed) {
Lyude27082492016-08-24 07:48:10 +02004241 int plane;
4242
Matt Roper2c4b49a2016-10-26 15:51:29 -07004243 for_each_universal_plane(dev_priv, pipe, plane)
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004244 skl_write_plane_wm(intel_crtc, &pipe_wm->planes[plane],
4245 &results->ddb, plane);
Lyude27082492016-08-24 07:48:10 +02004246
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004247 skl_write_cursor_wm(intel_crtc, &pipe_wm->planes[PLANE_CURSOR],
4248 &results->ddb);
Lyude27082492016-08-24 07:48:10 +02004249 }
4250
4251 skl_copy_wm_for_pipe(hw_vals, results, pipe);
Matt Roper734fa012016-05-12 15:11:40 -07004252
Lyudece0ba282016-09-15 10:46:35 -04004253 intel_crtc->hw_ddb = cstate->wm.skl.ddb;
4254
Matt Roper734fa012016-05-12 15:11:40 -07004255 mutex_unlock(&dev_priv->wm.wm_mutex);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004256}
4257
Ville Syrjäläd8905652016-01-14 14:53:35 +02004258static void ilk_compute_wm_config(struct drm_device *dev,
4259 struct intel_wm_config *config)
4260{
4261 struct intel_crtc *crtc;
4262
4263 /* Compute the currently _active_ config */
4264 for_each_intel_crtc(dev, crtc) {
4265 const struct intel_pipe_wm *wm = &crtc->wm.active.ilk;
4266
4267 if (!wm->pipe_enabled)
4268 continue;
4269
4270 config->sprites_enabled |= wm->sprites_enabled;
4271 config->sprites_scaled |= wm->sprites_scaled;
4272 config->num_pipes_active++;
4273 }
4274}
4275
Matt Ropered4a6a72016-02-23 17:20:13 -08004276static void ilk_program_watermarks(struct drm_i915_private *dev_priv)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03004277{
Chris Wilson91c8a322016-07-05 10:40:23 +01004278 struct drm_device *dev = &dev_priv->drm;
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07004279 struct intel_pipe_wm lp_wm_1_2 = {}, lp_wm_5_6 = {}, *best_lp_wm;
Imre Deak820c1982013-12-17 14:46:36 +02004280 struct ilk_wm_maximums max;
Ville Syrjäläd8905652016-01-14 14:53:35 +02004281 struct intel_wm_config config = {};
Imre Deak820c1982013-12-17 14:46:36 +02004282 struct ilk_wm_values results = {};
Ville Syrjälä77c122b2013-08-06 22:24:04 +03004283 enum intel_ddb_partitioning partitioning;
Matt Roper261a27d2015-10-08 15:28:25 -07004284
Ville Syrjäläd8905652016-01-14 14:53:35 +02004285 ilk_compute_wm_config(dev, &config);
4286
4287 ilk_compute_wm_maximums(dev, 1, &config, INTEL_DDB_PART_1_2, &max);
4288 ilk_wm_merge(dev, &config, &max, &lp_wm_1_2);
Ville Syrjälä0362c782013-10-09 19:17:57 +03004289
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03004290 /* 5/6 split only in single pipe config on IVB+ */
Ville Syrjäläec98c8d2013-10-11 15:26:26 +03004291 if (INTEL_INFO(dev)->gen >= 7 &&
Ville Syrjäläd8905652016-01-14 14:53:35 +02004292 config.num_pipes_active == 1 && config.sprites_enabled) {
4293 ilk_compute_wm_maximums(dev, 1, &config, INTEL_DDB_PART_5_6, &max);
4294 ilk_wm_merge(dev, &config, &max, &lp_wm_5_6);
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03004295
Imre Deak820c1982013-12-17 14:46:36 +02004296 best_lp_wm = ilk_find_best_result(dev, &lp_wm_1_2, &lp_wm_5_6);
Paulo Zanoni861f3382013-05-31 10:19:21 -03004297 } else {
Ville Syrjälä198a1e92013-10-09 19:17:58 +03004298 best_lp_wm = &lp_wm_1_2;
Paulo Zanoni861f3382013-05-31 10:19:21 -03004299 }
4300
Ville Syrjälä198a1e92013-10-09 19:17:58 +03004301 partitioning = (best_lp_wm == &lp_wm_1_2) ?
Ville Syrjälä77c122b2013-08-06 22:24:04 +03004302 INTEL_DDB_PART_1_2 : INTEL_DDB_PART_5_6;
Paulo Zanoni861f3382013-05-31 10:19:21 -03004303
Imre Deak820c1982013-12-17 14:46:36 +02004304 ilk_compute_wm_results(dev, best_lp_wm, partitioning, &results);
Ville Syrjälä609cede2013-10-09 19:18:03 +03004305
Imre Deak820c1982013-12-17 14:46:36 +02004306 ilk_write_wm_values(dev_priv, &results);
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03004307}
4308
Matt Ropered4a6a72016-02-23 17:20:13 -08004309static void ilk_initial_watermarks(struct intel_crtc_state *cstate)
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07004310{
Matt Ropered4a6a72016-02-23 17:20:13 -08004311 struct drm_i915_private *dev_priv = to_i915(cstate->base.crtc->dev);
4312 struct intel_crtc *intel_crtc = to_intel_crtc(cstate->base.crtc);
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07004313
Matt Ropered4a6a72016-02-23 17:20:13 -08004314 mutex_lock(&dev_priv->wm.wm_mutex);
Matt Ropere8f1f022016-05-12 07:05:55 -07004315 intel_crtc->wm.active.ilk = cstate->wm.ilk.intermediate;
Matt Ropered4a6a72016-02-23 17:20:13 -08004316 ilk_program_watermarks(dev_priv);
4317 mutex_unlock(&dev_priv->wm.wm_mutex);
4318}
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07004319
Matt Ropered4a6a72016-02-23 17:20:13 -08004320static void ilk_optimize_watermarks(struct intel_crtc_state *cstate)
4321{
4322 struct drm_i915_private *dev_priv = to_i915(cstate->base.crtc->dev);
4323 struct intel_crtc *intel_crtc = to_intel_crtc(cstate->base.crtc);
4324
4325 mutex_lock(&dev_priv->wm.wm_mutex);
4326 if (cstate->wm.need_postvbl_update) {
Matt Ropere8f1f022016-05-12 07:05:55 -07004327 intel_crtc->wm.active.ilk = cstate->wm.ilk.optimal;
Matt Ropered4a6a72016-02-23 17:20:13 -08004328 ilk_program_watermarks(dev_priv);
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07004329 }
Matt Ropered4a6a72016-02-23 17:20:13 -08004330 mutex_unlock(&dev_priv->wm.wm_mutex);
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07004331}
4332
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004333static inline void skl_wm_level_from_reg_val(uint32_t val,
4334 struct skl_wm_level *level)
Pradeep Bhat30789992014-11-04 17:06:45 +00004335{
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004336 level->plane_en = val & PLANE_WM_EN;
4337 level->plane_res_b = val & PLANE_WM_BLOCKS_MASK;
4338 level->plane_res_l = (val >> PLANE_WM_LINES_SHIFT) &
4339 PLANE_WM_LINES_MASK;
Pradeep Bhat30789992014-11-04 17:06:45 +00004340}
4341
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04004342void skl_pipe_wm_get_hw_state(struct drm_crtc *crtc,
4343 struct skl_pipe_wm *out)
Pradeep Bhat30789992014-11-04 17:06:45 +00004344{
4345 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004346 struct drm_i915_private *dev_priv = to_i915(dev);
Pradeep Bhat30789992014-11-04 17:06:45 +00004347 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004348 struct intel_plane *intel_plane;
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004349 struct skl_plane_wm *wm;
Pradeep Bhat30789992014-11-04 17:06:45 +00004350 enum pipe pipe = intel_crtc->pipe;
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004351 int level, id, max_level;
4352 uint32_t val;
Pradeep Bhat30789992014-11-04 17:06:45 +00004353
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01004354 max_level = ilk_wm_max_level(dev_priv);
Pradeep Bhat30789992014-11-04 17:06:45 +00004355
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004356 for_each_intel_plane_on_crtc(dev, intel_crtc, intel_plane) {
4357 id = skl_wm_plane_id(intel_plane);
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04004358 wm = &out->planes[id];
Pradeep Bhat30789992014-11-04 17:06:45 +00004359
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004360 for (level = 0; level <= max_level; level++) {
4361 if (id != PLANE_CURSOR)
4362 val = I915_READ(PLANE_WM(pipe, id, level));
4363 else
4364 val = I915_READ(CUR_WM(pipe, level));
4365
4366 skl_wm_level_from_reg_val(val, &wm->wm[level]);
4367 }
4368
4369 if (id != PLANE_CURSOR)
4370 val = I915_READ(PLANE_WM_TRANS(pipe, id));
4371 else
4372 val = I915_READ(CUR_WM_TRANS(pipe));
4373
4374 skl_wm_level_from_reg_val(val, &wm->trans_wm);
4375 }
Pradeep Bhat30789992014-11-04 17:06:45 +00004376
Matt Roper3ef00282015-03-09 10:19:24 -07004377 if (!intel_crtc->active)
Pradeep Bhat30789992014-11-04 17:06:45 +00004378 return;
4379
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04004380 out->linetime = I915_READ(PIPE_WM_LINETIME(pipe));
Pradeep Bhat30789992014-11-04 17:06:45 +00004381}
4382
4383void skl_wm_get_hw_state(struct drm_device *dev)
4384{
Chris Wilsonfac5e232016-07-04 11:34:36 +01004385 struct drm_i915_private *dev_priv = to_i915(dev);
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04004386 struct skl_wm_values *hw = &dev_priv->wm.skl_hw;
Damien Lespiaua269c582014-11-04 17:06:49 +00004387 struct skl_ddb_allocation *ddb = &dev_priv->wm.skl_hw.ddb;
Pradeep Bhat30789992014-11-04 17:06:45 +00004388 struct drm_crtc *crtc;
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04004389 struct intel_crtc *intel_crtc;
4390 struct intel_crtc_state *cstate;
Pradeep Bhat30789992014-11-04 17:06:45 +00004391
Damien Lespiaua269c582014-11-04 17:06:49 +00004392 skl_ddb_get_hw_state(dev_priv, ddb);
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04004393 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
4394 intel_crtc = to_intel_crtc(crtc);
4395 cstate = to_intel_crtc_state(crtc->state);
4396
4397 skl_pipe_wm_get_hw_state(crtc, &cstate->wm.skl.optimal);
4398
Maarten Lankhorst03af79e2016-10-26 15:41:36 +02004399 if (intel_crtc->active)
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04004400 hw->dirty_pipes |= drm_crtc_mask(crtc);
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04004401 }
Matt Ropera1de91e2016-05-12 07:05:57 -07004402
Matt Roper279e99d2016-05-12 07:06:02 -07004403 if (dev_priv->active_crtcs) {
4404 /* Fully recompute DDB on first atomic commit */
4405 dev_priv->wm.distrust_bios_wm = true;
4406 } else {
4407 /* Easy/common case; just sanitize DDB now if everything off */
4408 memset(ddb, 0, sizeof(*ddb));
4409 }
Pradeep Bhat30789992014-11-04 17:06:45 +00004410}
4411
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004412static void ilk_pipe_wm_get_hw_state(struct drm_crtc *crtc)
4413{
4414 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004415 struct drm_i915_private *dev_priv = to_i915(dev);
Imre Deak820c1982013-12-17 14:46:36 +02004416 struct ilk_wm_values *hw = &dev_priv->wm.hw;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004417 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Matt Roper4e0963c2015-09-24 15:53:15 -07004418 struct intel_crtc_state *cstate = to_intel_crtc_state(crtc->state);
Matt Ropere8f1f022016-05-12 07:05:55 -07004419 struct intel_pipe_wm *active = &cstate->wm.ilk.optimal;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004420 enum pipe pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004421 static const i915_reg_t wm0_pipe_reg[] = {
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004422 [PIPE_A] = WM0_PIPEA_ILK,
4423 [PIPE_B] = WM0_PIPEB_ILK,
4424 [PIPE_C] = WM0_PIPEC_IVB,
4425 };
4426
4427 hw->wm_pipe[pipe] = I915_READ(wm0_pipe_reg[pipe]);
Tvrtko Ursulin86527442016-10-13 11:03:00 +01004428 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjäläce0e0712013-12-05 15:51:36 +02004429 hw->wm_linetime[pipe] = I915_READ(PIPE_WM_LINETIME(pipe));
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004430
Ville Syrjälä15606532016-05-13 17:55:17 +03004431 memset(active, 0, sizeof(*active));
4432
Matt Roper3ef00282015-03-09 10:19:24 -07004433 active->pipe_enabled = intel_crtc->active;
Ville Syrjälä2a44b762014-03-07 18:32:09 +02004434
4435 if (active->pipe_enabled) {
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004436 u32 tmp = hw->wm_pipe[pipe];
4437
4438 /*
4439 * For active pipes LP0 watermark is marked as
4440 * enabled, and LP1+ watermaks as disabled since
4441 * we can't really reverse compute them in case
4442 * multiple pipes are active.
4443 */
4444 active->wm[0].enable = true;
4445 active->wm[0].pri_val = (tmp & WM0_PIPE_PLANE_MASK) >> WM0_PIPE_PLANE_SHIFT;
4446 active->wm[0].spr_val = (tmp & WM0_PIPE_SPRITE_MASK) >> WM0_PIPE_SPRITE_SHIFT;
4447 active->wm[0].cur_val = tmp & WM0_PIPE_CURSOR_MASK;
4448 active->linetime = hw->wm_linetime[pipe];
4449 } else {
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01004450 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004451
4452 /*
4453 * For inactive pipes, all watermark levels
4454 * should be marked as enabled but zeroed,
4455 * which is what we'd compute them to.
4456 */
4457 for (level = 0; level <= max_level; level++)
4458 active->wm[level].enable = true;
4459 }
Matt Roper4e0963c2015-09-24 15:53:15 -07004460
4461 intel_crtc->wm.active.ilk = *active;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004462}
4463
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03004464#define _FW_WM(value, plane) \
4465 (((value) & DSPFW_ ## plane ## _MASK) >> DSPFW_ ## plane ## _SHIFT)
4466#define _FW_WM_VLV(value, plane) \
4467 (((value) & DSPFW_ ## plane ## _MASK_VLV) >> DSPFW_ ## plane ## _SHIFT)
4468
4469static void vlv_read_wm_values(struct drm_i915_private *dev_priv,
4470 struct vlv_wm_values *wm)
4471{
4472 enum pipe pipe;
4473 uint32_t tmp;
4474
4475 for_each_pipe(dev_priv, pipe) {
4476 tmp = I915_READ(VLV_DDL(pipe));
4477
4478 wm->ddl[pipe].primary =
4479 (tmp >> DDL_PLANE_SHIFT) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
4480 wm->ddl[pipe].cursor =
4481 (tmp >> DDL_CURSOR_SHIFT) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
4482 wm->ddl[pipe].sprite[0] =
4483 (tmp >> DDL_SPRITE_SHIFT(0)) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
4484 wm->ddl[pipe].sprite[1] =
4485 (tmp >> DDL_SPRITE_SHIFT(1)) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
4486 }
4487
4488 tmp = I915_READ(DSPFW1);
4489 wm->sr.plane = _FW_WM(tmp, SR);
4490 wm->pipe[PIPE_B].cursor = _FW_WM(tmp, CURSORB);
4491 wm->pipe[PIPE_B].primary = _FW_WM_VLV(tmp, PLANEB);
4492 wm->pipe[PIPE_A].primary = _FW_WM_VLV(tmp, PLANEA);
4493
4494 tmp = I915_READ(DSPFW2);
4495 wm->pipe[PIPE_A].sprite[1] = _FW_WM_VLV(tmp, SPRITEB);
4496 wm->pipe[PIPE_A].cursor = _FW_WM(tmp, CURSORA);
4497 wm->pipe[PIPE_A].sprite[0] = _FW_WM_VLV(tmp, SPRITEA);
4498
4499 tmp = I915_READ(DSPFW3);
4500 wm->sr.cursor = _FW_WM(tmp, CURSOR_SR);
4501
4502 if (IS_CHERRYVIEW(dev_priv)) {
4503 tmp = I915_READ(DSPFW7_CHV);
4504 wm->pipe[PIPE_B].sprite[1] = _FW_WM_VLV(tmp, SPRITED);
4505 wm->pipe[PIPE_B].sprite[0] = _FW_WM_VLV(tmp, SPRITEC);
4506
4507 tmp = I915_READ(DSPFW8_CHV);
4508 wm->pipe[PIPE_C].sprite[1] = _FW_WM_VLV(tmp, SPRITEF);
4509 wm->pipe[PIPE_C].sprite[0] = _FW_WM_VLV(tmp, SPRITEE);
4510
4511 tmp = I915_READ(DSPFW9_CHV);
4512 wm->pipe[PIPE_C].primary = _FW_WM_VLV(tmp, PLANEC);
4513 wm->pipe[PIPE_C].cursor = _FW_WM(tmp, CURSORC);
4514
4515 tmp = I915_READ(DSPHOWM);
4516 wm->sr.plane |= _FW_WM(tmp, SR_HI) << 9;
4517 wm->pipe[PIPE_C].sprite[1] |= _FW_WM(tmp, SPRITEF_HI) << 8;
4518 wm->pipe[PIPE_C].sprite[0] |= _FW_WM(tmp, SPRITEE_HI) << 8;
4519 wm->pipe[PIPE_C].primary |= _FW_WM(tmp, PLANEC_HI) << 8;
4520 wm->pipe[PIPE_B].sprite[1] |= _FW_WM(tmp, SPRITED_HI) << 8;
4521 wm->pipe[PIPE_B].sprite[0] |= _FW_WM(tmp, SPRITEC_HI) << 8;
4522 wm->pipe[PIPE_B].primary |= _FW_WM(tmp, PLANEB_HI) << 8;
4523 wm->pipe[PIPE_A].sprite[1] |= _FW_WM(tmp, SPRITEB_HI) << 8;
4524 wm->pipe[PIPE_A].sprite[0] |= _FW_WM(tmp, SPRITEA_HI) << 8;
4525 wm->pipe[PIPE_A].primary |= _FW_WM(tmp, PLANEA_HI) << 8;
4526 } else {
4527 tmp = I915_READ(DSPFW7);
4528 wm->pipe[PIPE_B].sprite[1] = _FW_WM_VLV(tmp, SPRITED);
4529 wm->pipe[PIPE_B].sprite[0] = _FW_WM_VLV(tmp, SPRITEC);
4530
4531 tmp = I915_READ(DSPHOWM);
4532 wm->sr.plane |= _FW_WM(tmp, SR_HI) << 9;
4533 wm->pipe[PIPE_B].sprite[1] |= _FW_WM(tmp, SPRITED_HI) << 8;
4534 wm->pipe[PIPE_B].sprite[0] |= _FW_WM(tmp, SPRITEC_HI) << 8;
4535 wm->pipe[PIPE_B].primary |= _FW_WM(tmp, PLANEB_HI) << 8;
4536 wm->pipe[PIPE_A].sprite[1] |= _FW_WM(tmp, SPRITEB_HI) << 8;
4537 wm->pipe[PIPE_A].sprite[0] |= _FW_WM(tmp, SPRITEA_HI) << 8;
4538 wm->pipe[PIPE_A].primary |= _FW_WM(tmp, PLANEA_HI) << 8;
4539 }
4540}
4541
4542#undef _FW_WM
4543#undef _FW_WM_VLV
4544
4545void vlv_wm_get_hw_state(struct drm_device *dev)
4546{
4547 struct drm_i915_private *dev_priv = to_i915(dev);
4548 struct vlv_wm_values *wm = &dev_priv->wm.vlv;
4549 struct intel_plane *plane;
4550 enum pipe pipe;
4551 u32 val;
4552
4553 vlv_read_wm_values(dev_priv, wm);
4554
4555 for_each_intel_plane(dev, plane) {
4556 switch (plane->base.type) {
4557 int sprite;
4558 case DRM_PLANE_TYPE_CURSOR:
4559 plane->wm.fifo_size = 63;
4560 break;
4561 case DRM_PLANE_TYPE_PRIMARY:
4562 plane->wm.fifo_size = vlv_get_fifo_size(dev, plane->pipe, 0);
4563 break;
4564 case DRM_PLANE_TYPE_OVERLAY:
4565 sprite = plane->plane;
4566 plane->wm.fifo_size = vlv_get_fifo_size(dev, plane->pipe, sprite + 1);
4567 break;
4568 }
4569 }
4570
4571 wm->cxsr = I915_READ(FW_BLC_SELF_VLV) & FW_CSPWRDWNEN;
4572 wm->level = VLV_WM_LEVEL_PM2;
4573
4574 if (IS_CHERRYVIEW(dev_priv)) {
4575 mutex_lock(&dev_priv->rps.hw_lock);
4576
4577 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
4578 if (val & DSP_MAXFIFO_PM5_ENABLE)
4579 wm->level = VLV_WM_LEVEL_PM5;
4580
Ville Syrjälä58590c12015-09-08 21:05:12 +03004581 /*
4582 * If DDR DVFS is disabled in the BIOS, Punit
4583 * will never ack the request. So if that happens
4584 * assume we don't have to enable/disable DDR DVFS
4585 * dynamically. To test that just set the REQ_ACK
4586 * bit to poke the Punit, but don't change the
4587 * HIGH/LOW bits so that we don't actually change
4588 * the current state.
4589 */
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03004590 val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
Ville Syrjälä58590c12015-09-08 21:05:12 +03004591 val |= FORCE_DDR_FREQ_REQ_ACK;
4592 vlv_punit_write(dev_priv, PUNIT_REG_DDR_SETUP2, val);
4593
4594 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2) &
4595 FORCE_DDR_FREQ_REQ_ACK) == 0, 3)) {
4596 DRM_DEBUG_KMS("Punit not acking DDR DVFS request, "
4597 "assuming DDR DVFS is disabled\n");
4598 dev_priv->wm.max_level = VLV_WM_LEVEL_PM5;
4599 } else {
4600 val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
4601 if ((val & FORCE_DDR_HIGH_FREQ) == 0)
4602 wm->level = VLV_WM_LEVEL_DDR_DVFS;
4603 }
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03004604
4605 mutex_unlock(&dev_priv->rps.hw_lock);
4606 }
4607
4608 for_each_pipe(dev_priv, pipe)
4609 DRM_DEBUG_KMS("Initial watermarks: pipe %c, plane=%d, cursor=%d, sprite0=%d, sprite1=%d\n",
4610 pipe_name(pipe), wm->pipe[pipe].primary, wm->pipe[pipe].cursor,
4611 wm->pipe[pipe].sprite[0], wm->pipe[pipe].sprite[1]);
4612
4613 DRM_DEBUG_KMS("Initial watermarks: SR plane=%d, SR cursor=%d level=%d cxsr=%d\n",
4614 wm->sr.plane, wm->sr.cursor, wm->level, wm->cxsr);
4615}
4616
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004617void ilk_wm_get_hw_state(struct drm_device *dev)
4618{
Chris Wilsonfac5e232016-07-04 11:34:36 +01004619 struct drm_i915_private *dev_priv = to_i915(dev);
Imre Deak820c1982013-12-17 14:46:36 +02004620 struct ilk_wm_values *hw = &dev_priv->wm.hw;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004621 struct drm_crtc *crtc;
4622
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01004623 for_each_crtc(dev, crtc)
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004624 ilk_pipe_wm_get_hw_state(crtc);
4625
4626 hw->wm_lp[0] = I915_READ(WM1_LP_ILK);
4627 hw->wm_lp[1] = I915_READ(WM2_LP_ILK);
4628 hw->wm_lp[2] = I915_READ(WM3_LP_ILK);
4629
4630 hw->wm_lp_spr[0] = I915_READ(WM1S_LP_ILK);
Ville Syrjäläcfa76982014-03-07 18:32:08 +02004631 if (INTEL_INFO(dev)->gen >= 7) {
4632 hw->wm_lp_spr[1] = I915_READ(WM2S_LP_IVB);
4633 hw->wm_lp_spr[2] = I915_READ(WM3S_LP_IVB);
4634 }
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004635
Tvrtko Ursulin86527442016-10-13 11:03:00 +01004636 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjäläac9545f2013-12-05 15:51:28 +02004637 hw->partitioning = (I915_READ(WM_MISC) & WM_MISC_DATA_PARTITION_5_6) ?
4638 INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01004639 else if (IS_IVYBRIDGE(dev_priv))
Ville Syrjäläac9545f2013-12-05 15:51:28 +02004640 hw->partitioning = (I915_READ(DISP_ARB_CTL2) & DISP_DATA_PARTITION_5_6) ?
4641 INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004642
4643 hw->enable_fbc_wm =
4644 !(I915_READ(DISP_ARB_CTL) & DISP_FBC_WM_DIS);
4645}
4646
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03004647/**
4648 * intel_update_watermarks - update FIFO watermark values based on current modes
4649 *
4650 * Calculate watermark values for the various WM regs based on current mode
4651 * and plane configuration.
4652 *
4653 * There are several cases to deal with here:
4654 * - normal (i.e. non-self-refresh)
4655 * - self-refresh (SR) mode
4656 * - lines are large relative to FIFO size (buffer can hold up to 2)
4657 * - lines are small relative to FIFO size (buffer can hold more than 2
4658 * lines), so need to account for TLB latency
4659 *
4660 * The normal calculation is:
4661 * watermark = dotclock * bytes per pixel * latency
4662 * where latency is platform & configuration dependent (we assume pessimal
4663 * values here).
4664 *
4665 * The SR calculation is:
4666 * watermark = (trunc(latency/line time)+1) * surface width *
4667 * bytes per pixel
4668 * where
4669 * line time = htotal / dotclock
4670 * surface width = hdisplay for normal plane and 64 for cursor
4671 * and latency is assumed to be high, as above.
4672 *
4673 * The final value programmed to the register should always be rounded up,
4674 * and include an extra 2 entries to account for clock crossings.
4675 *
4676 * We don't use the sprite, so we can ignore that. And on Crestline we have
4677 * to set the non-SR watermarks to 8.
4678 */
Ville Syrjälä432081b2016-10-31 22:37:03 +02004679void intel_update_watermarks(struct intel_crtc *crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03004680{
Ville Syrjälä432081b2016-10-31 22:37:03 +02004681 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03004682
4683 if (dev_priv->display.update_wm)
Ville Syrjälä46ba6142013-09-10 11:40:40 +03004684 dev_priv->display.update_wm(crtc);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03004685}
4686
Jani Nikulae2828912016-01-18 09:19:47 +02004687/*
Daniel Vetter92703882012-08-09 16:46:01 +02004688 * Lock protecting IPS related data structures
Daniel Vetter92703882012-08-09 16:46:01 +02004689 */
4690DEFINE_SPINLOCK(mchdev_lock);
4691
4692/* Global for IPS driver to get at the current i915 device. Protected by
4693 * mchdev_lock. */
4694static struct drm_i915_private *i915_mch_dev;
4695
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01004696bool ironlake_set_drps(struct drm_i915_private *dev_priv, u8 val)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004697{
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004698 u16 rgvswctl;
4699
Daniel Vetter92703882012-08-09 16:46:01 +02004700 assert_spin_locked(&mchdev_lock);
4701
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004702 rgvswctl = I915_READ16(MEMSWCTL);
4703 if (rgvswctl & MEMCTL_CMD_STS) {
4704 DRM_DEBUG("gpu busy, RCS change rejected\n");
4705 return false; /* still busy with another command */
4706 }
4707
4708 rgvswctl = (MEMCTL_CMD_CHFREQ << MEMCTL_CMD_SHIFT) |
4709 (val << MEMCTL_FREQ_SHIFT) | MEMCTL_SFCAVM;
4710 I915_WRITE16(MEMSWCTL, rgvswctl);
4711 POSTING_READ16(MEMSWCTL);
4712
4713 rgvswctl |= MEMCTL_CMD_STS;
4714 I915_WRITE16(MEMSWCTL, rgvswctl);
4715
4716 return true;
4717}
4718
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01004719static void ironlake_enable_drps(struct drm_i915_private *dev_priv)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004720{
Tvrtko Ursulin84f1b202016-02-11 10:27:32 +00004721 u32 rgvmodectl;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004722 u8 fmax, fmin, fstart, vstart;
4723
Daniel Vetter92703882012-08-09 16:46:01 +02004724 spin_lock_irq(&mchdev_lock);
4725
Tvrtko Ursulin84f1b202016-02-11 10:27:32 +00004726 rgvmodectl = I915_READ(MEMMODECTL);
4727
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004728 /* Enable temp reporting */
4729 I915_WRITE16(PMMISC, I915_READ(PMMISC) | MCPPCE_EN);
4730 I915_WRITE16(TSC1, I915_READ(TSC1) | TSE);
4731
4732 /* 100ms RC evaluation intervals */
4733 I915_WRITE(RCUPEI, 100000);
4734 I915_WRITE(RCDNEI, 100000);
4735
4736 /* Set max/min thresholds to 90ms and 80ms respectively */
4737 I915_WRITE(RCBMAXAVG, 90000);
4738 I915_WRITE(RCBMINAVG, 80000);
4739
4740 I915_WRITE(MEMIHYST, 1);
4741
4742 /* Set up min, max, and cur for interrupt handling */
4743 fmax = (rgvmodectl & MEMMODE_FMAX_MASK) >> MEMMODE_FMAX_SHIFT;
4744 fmin = (rgvmodectl & MEMMODE_FMIN_MASK);
4745 fstart = (rgvmodectl & MEMMODE_FSTART_MASK) >>
4746 MEMMODE_FSTART_SHIFT;
4747
Ville Syrjälä616847e2015-09-18 20:03:19 +03004748 vstart = (I915_READ(PXVFREQ(fstart)) & PXVFREQ_PX_MASK) >>
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004749 PXVFREQ_PX_SHIFT;
4750
Daniel Vetter20e4d402012-08-08 23:35:39 +02004751 dev_priv->ips.fmax = fmax; /* IPS callback will increase this */
4752 dev_priv->ips.fstart = fstart;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004753
Daniel Vetter20e4d402012-08-08 23:35:39 +02004754 dev_priv->ips.max_delay = fstart;
4755 dev_priv->ips.min_delay = fmin;
4756 dev_priv->ips.cur_delay = fstart;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004757
4758 DRM_DEBUG_DRIVER("fmax: %d, fmin: %d, fstart: %d\n",
4759 fmax, fmin, fstart);
4760
4761 I915_WRITE(MEMINTREN, MEMINT_CX_SUPR_EN | MEMINT_EVAL_CHG_EN);
4762
4763 /*
4764 * Interrupts will be enabled in ironlake_irq_postinstall
4765 */
4766
4767 I915_WRITE(VIDSTART, vstart);
4768 POSTING_READ(VIDSTART);
4769
4770 rgvmodectl |= MEMMODE_SWMODE_EN;
4771 I915_WRITE(MEMMODECTL, rgvmodectl);
4772
Daniel Vetter92703882012-08-09 16:46:01 +02004773 if (wait_for_atomic((I915_READ(MEMSWCTL) & MEMCTL_CMD_STS) == 0, 10))
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004774 DRM_ERROR("stuck trying to change perf mode\n");
Daniel Vetterdd92d8d2015-07-20 10:58:21 +02004775 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004776
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01004777 ironlake_set_drps(dev_priv, fstart);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004778
Ville Syrjälä7d81c3e2015-09-18 20:03:20 +03004779 dev_priv->ips.last_count1 = I915_READ(DMIEC) +
4780 I915_READ(DDREC) + I915_READ(CSIEC);
Daniel Vetter20e4d402012-08-08 23:35:39 +02004781 dev_priv->ips.last_time1 = jiffies_to_msecs(jiffies);
Ville Syrjälä7d81c3e2015-09-18 20:03:20 +03004782 dev_priv->ips.last_count2 = I915_READ(GFXEC);
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00004783 dev_priv->ips.last_time2 = ktime_get_raw_ns();
Daniel Vetter92703882012-08-09 16:46:01 +02004784
4785 spin_unlock_irq(&mchdev_lock);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004786}
4787
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01004788static void ironlake_disable_drps(struct drm_i915_private *dev_priv)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004789{
Daniel Vetter92703882012-08-09 16:46:01 +02004790 u16 rgvswctl;
4791
4792 spin_lock_irq(&mchdev_lock);
4793
4794 rgvswctl = I915_READ16(MEMSWCTL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004795
4796 /* Ack interrupts, disable EFC interrupt */
4797 I915_WRITE(MEMINTREN, I915_READ(MEMINTREN) & ~MEMINT_EVAL_CHG_EN);
4798 I915_WRITE(MEMINTRSTS, MEMINT_EVAL_CHG);
4799 I915_WRITE(DEIER, I915_READ(DEIER) & ~DE_PCU_EVENT);
4800 I915_WRITE(DEIIR, DE_PCU_EVENT);
4801 I915_WRITE(DEIMR, I915_READ(DEIMR) | DE_PCU_EVENT);
4802
4803 /* Go back to the starting frequency */
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01004804 ironlake_set_drps(dev_priv, dev_priv->ips.fstart);
Daniel Vetterdd92d8d2015-07-20 10:58:21 +02004805 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004806 rgvswctl |= MEMCTL_CMD_STS;
4807 I915_WRITE(MEMSWCTL, rgvswctl);
Daniel Vetterdd92d8d2015-07-20 10:58:21 +02004808 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004809
Daniel Vetter92703882012-08-09 16:46:01 +02004810 spin_unlock_irq(&mchdev_lock);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004811}
4812
Daniel Vetteracbe9472012-07-26 11:50:05 +02004813/* There's a funny hw issue where the hw returns all 0 when reading from
4814 * GEN6_RP_INTERRUPT_LIMITS. Hence we always need to compute the desired value
4815 * ourselves, instead of doing a rmw cycle (which might result in us clearing
4816 * all limits and the gpu stuck at whatever frequency it is at atm).
4817 */
Akash Goel74ef1172015-03-06 11:07:19 +05304818static u32 intel_rps_limits(struct drm_i915_private *dev_priv, u8 val)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004819{
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01004820 u32 limits;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004821
Daniel Vetter20b46e52012-07-26 11:16:14 +02004822 /* Only set the down limit when we've reached the lowest level to avoid
4823 * getting more interrupts, otherwise leave this clear. This prevents a
4824 * race in the hw when coming out of rc6: There's a tiny window where
4825 * the hw runs at the minimal clock before selecting the desired
4826 * frequency, if the down threshold expires in that window we will not
4827 * receive a down interrupt. */
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03004828 if (IS_GEN9(dev_priv)) {
Akash Goel74ef1172015-03-06 11:07:19 +05304829 limits = (dev_priv->rps.max_freq_softlimit) << 23;
4830 if (val <= dev_priv->rps.min_freq_softlimit)
4831 limits |= (dev_priv->rps.min_freq_softlimit) << 14;
4832 } else {
4833 limits = dev_priv->rps.max_freq_softlimit << 24;
4834 if (val <= dev_priv->rps.min_freq_softlimit)
4835 limits |= dev_priv->rps.min_freq_softlimit << 16;
4836 }
Daniel Vetter20b46e52012-07-26 11:16:14 +02004837
4838 return limits;
4839}
4840
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004841static void gen6_set_rps_thresholds(struct drm_i915_private *dev_priv, u8 val)
4842{
4843 int new_power;
Akash Goel8a586432015-03-06 11:07:18 +05304844 u32 threshold_up = 0, threshold_down = 0; /* in % */
4845 u32 ei_up = 0, ei_down = 0;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004846
4847 new_power = dev_priv->rps.power;
4848 switch (dev_priv->rps.power) {
4849 case LOW_POWER:
Chris Wilsona72b5622016-07-02 15:35:59 +01004850 if (val > dev_priv->rps.efficient_freq + 1 &&
4851 val > dev_priv->rps.cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004852 new_power = BETWEEN;
4853 break;
4854
4855 case BETWEEN:
Chris Wilsona72b5622016-07-02 15:35:59 +01004856 if (val <= dev_priv->rps.efficient_freq &&
4857 val < dev_priv->rps.cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004858 new_power = LOW_POWER;
Chris Wilsona72b5622016-07-02 15:35:59 +01004859 else if (val >= dev_priv->rps.rp0_freq &&
4860 val > dev_priv->rps.cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004861 new_power = HIGH_POWER;
4862 break;
4863
4864 case HIGH_POWER:
Chris Wilsona72b5622016-07-02 15:35:59 +01004865 if (val < (dev_priv->rps.rp1_freq + dev_priv->rps.rp0_freq) >> 1 &&
4866 val < dev_priv->rps.cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004867 new_power = BETWEEN;
4868 break;
4869 }
4870 /* Max/min bins are special */
Chris Wilsonaed242f2015-03-18 09:48:21 +00004871 if (val <= dev_priv->rps.min_freq_softlimit)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004872 new_power = LOW_POWER;
Chris Wilsonaed242f2015-03-18 09:48:21 +00004873 if (val >= dev_priv->rps.max_freq_softlimit)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004874 new_power = HIGH_POWER;
4875 if (new_power == dev_priv->rps.power)
4876 return;
4877
4878 /* Note the units here are not exactly 1us, but 1280ns. */
4879 switch (new_power) {
4880 case LOW_POWER:
4881 /* Upclock if more than 95% busy over 16ms */
Akash Goel8a586432015-03-06 11:07:18 +05304882 ei_up = 16000;
4883 threshold_up = 95;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004884
4885 /* Downclock if less than 85% busy over 32ms */
Akash Goel8a586432015-03-06 11:07:18 +05304886 ei_down = 32000;
4887 threshold_down = 85;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004888 break;
4889
4890 case BETWEEN:
4891 /* Upclock if more than 90% busy over 13ms */
Akash Goel8a586432015-03-06 11:07:18 +05304892 ei_up = 13000;
4893 threshold_up = 90;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004894
4895 /* Downclock if less than 75% busy over 32ms */
Akash Goel8a586432015-03-06 11:07:18 +05304896 ei_down = 32000;
4897 threshold_down = 75;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004898 break;
4899
4900 case HIGH_POWER:
4901 /* Upclock if more than 85% busy over 10ms */
Akash Goel8a586432015-03-06 11:07:18 +05304902 ei_up = 10000;
4903 threshold_up = 85;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004904
4905 /* Downclock if less than 60% busy over 32ms */
Akash Goel8a586432015-03-06 11:07:18 +05304906 ei_down = 32000;
4907 threshold_down = 60;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004908 break;
4909 }
4910
Akash Goel8a586432015-03-06 11:07:18 +05304911 I915_WRITE(GEN6_RP_UP_EI,
Chris Wilsona72b5622016-07-02 15:35:59 +01004912 GT_INTERVAL_FROM_US(dev_priv, ei_up));
Akash Goel8a586432015-03-06 11:07:18 +05304913 I915_WRITE(GEN6_RP_UP_THRESHOLD,
Chris Wilsona72b5622016-07-02 15:35:59 +01004914 GT_INTERVAL_FROM_US(dev_priv,
4915 ei_up * threshold_up / 100));
Akash Goel8a586432015-03-06 11:07:18 +05304916
4917 I915_WRITE(GEN6_RP_DOWN_EI,
Chris Wilsona72b5622016-07-02 15:35:59 +01004918 GT_INTERVAL_FROM_US(dev_priv, ei_down));
Akash Goel8a586432015-03-06 11:07:18 +05304919 I915_WRITE(GEN6_RP_DOWN_THRESHOLD,
Chris Wilsona72b5622016-07-02 15:35:59 +01004920 GT_INTERVAL_FROM_US(dev_priv,
4921 ei_down * threshold_down / 100));
Akash Goel8a586432015-03-06 11:07:18 +05304922
Chris Wilsona72b5622016-07-02 15:35:59 +01004923 I915_WRITE(GEN6_RP_CONTROL,
4924 GEN6_RP_MEDIA_TURBO |
4925 GEN6_RP_MEDIA_HW_NORMAL_MODE |
4926 GEN6_RP_MEDIA_IS_GFX |
4927 GEN6_RP_ENABLE |
4928 GEN6_RP_UP_BUSY_AVG |
4929 GEN6_RP_DOWN_IDLE_AVG);
Akash Goel8a586432015-03-06 11:07:18 +05304930
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004931 dev_priv->rps.power = new_power;
Chris Wilson8fb55192015-04-07 16:20:28 +01004932 dev_priv->rps.up_threshold = threshold_up;
4933 dev_priv->rps.down_threshold = threshold_down;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004934 dev_priv->rps.last_adj = 0;
4935}
4936
Chris Wilson2876ce72014-03-28 08:03:34 +00004937static u32 gen6_rps_pm_mask(struct drm_i915_private *dev_priv, u8 val)
4938{
4939 u32 mask = 0;
4940
4941 if (val > dev_priv->rps.min_freq_softlimit)
Chris Wilson6f4b12f82015-03-18 09:48:23 +00004942 mask |= GEN6_PM_RP_DOWN_EI_EXPIRED | GEN6_PM_RP_DOWN_THRESHOLD | GEN6_PM_RP_DOWN_TIMEOUT;
Chris Wilson2876ce72014-03-28 08:03:34 +00004943 if (val < dev_priv->rps.max_freq_softlimit)
Chris Wilson6f4b12f82015-03-18 09:48:23 +00004944 mask |= GEN6_PM_RP_UP_EI_EXPIRED | GEN6_PM_RP_UP_THRESHOLD;
Chris Wilson2876ce72014-03-28 08:03:34 +00004945
Chris Wilson7b3c29f2014-07-10 20:31:19 +01004946 mask &= dev_priv->pm_rps_events;
4947
Imre Deak59d02a12014-12-19 19:33:26 +02004948 return gen6_sanitize_rps_pm_mask(dev_priv, ~mask);
Chris Wilson2876ce72014-03-28 08:03:34 +00004949}
4950
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06004951/* gen6_set_rps is called to update the frequency request, but should also be
4952 * called when the range (min_delay and max_delay) is modified so that we can
4953 * update the GEN6_RP_INTERRUPT_LIMITS register accordingly. */
Chris Wilsondc979972016-05-10 14:10:04 +01004954static void gen6_set_rps(struct drm_i915_private *dev_priv, u8 val)
Daniel Vetter20b46e52012-07-26 11:16:14 +02004955{
Sagar Arun Kamble23eafea2015-08-23 17:52:48 +05304956 /* WaGsvDisableTurbo: Workaround to disable turbo on BXT A* */
Chris Wilsondc979972016-05-10 14:10:04 +01004957 if (IS_BXT_REVID(dev_priv, 0, BXT_REVID_A1))
Sagar Arun Kamble23eafea2015-08-23 17:52:48 +05304958 return;
4959
Jesse Barnes4fc688c2012-11-02 11:14:01 -07004960 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Chris Wilsonaed242f2015-03-18 09:48:21 +00004961 WARN_ON(val > dev_priv->rps.max_freq);
4962 WARN_ON(val < dev_priv->rps.min_freq);
Daniel Vetter004777c2012-08-09 15:07:01 +02004963
Chris Wilsoneb64cad2014-03-27 08:24:20 +00004964 /* min/max delay may still have been modified so be sure to
4965 * write the limits value.
4966 */
4967 if (val != dev_priv->rps.cur_freq) {
4968 gen6_set_rps_thresholds(dev_priv, val);
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06004969
Chris Wilsondc979972016-05-10 14:10:04 +01004970 if (IS_GEN9(dev_priv))
Akash Goel57041952015-03-06 11:07:17 +05304971 I915_WRITE(GEN6_RPNSWREQ,
4972 GEN9_FREQUENCY(val));
Chris Wilsondc979972016-05-10 14:10:04 +01004973 else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Chris Wilsoneb64cad2014-03-27 08:24:20 +00004974 I915_WRITE(GEN6_RPNSWREQ,
4975 HSW_FREQUENCY(val));
4976 else
4977 I915_WRITE(GEN6_RPNSWREQ,
4978 GEN6_FREQUENCY(val) |
4979 GEN6_OFFSET(0) |
4980 GEN6_AGGRESSIVE_TURBO);
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06004981 }
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01004982
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01004983 /* Make sure we continue to get interrupts
4984 * until we hit the minimum or maximum frequencies.
4985 */
Akash Goel74ef1172015-03-06 11:07:19 +05304986 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS, intel_rps_limits(dev_priv, val));
Chris Wilson2876ce72014-03-28 08:03:34 +00004987 I915_WRITE(GEN6_PMINTRMSK, gen6_rps_pm_mask(dev_priv, val));
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01004988
Ben Widawskyd5570a72012-09-07 19:43:41 -07004989 POSTING_READ(GEN6_RPNSWREQ);
4990
Ben Widawskyb39fb292014-03-19 18:31:11 -07004991 dev_priv->rps.cur_freq = val;
Mika Kuoppala0f945922015-11-17 18:14:26 +02004992 trace_intel_gpu_freq_change(intel_gpu_freq(dev_priv, val));
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004993}
4994
Chris Wilsondc979972016-05-10 14:10:04 +01004995static void valleyview_set_rps(struct drm_i915_private *dev_priv, u8 val)
Ville Syrjäläffe02b42015-02-02 19:09:50 +02004996{
Ville Syrjäläffe02b42015-02-02 19:09:50 +02004997 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Chris Wilsonaed242f2015-03-18 09:48:21 +00004998 WARN_ON(val > dev_priv->rps.max_freq);
4999 WARN_ON(val < dev_priv->rps.min_freq);
Ville Syrjäläffe02b42015-02-02 19:09:50 +02005000
Chris Wilsondc979972016-05-10 14:10:04 +01005001 if (WARN_ONCE(IS_CHERRYVIEW(dev_priv) && (val & 1),
Ville Syrjäläffe02b42015-02-02 19:09:50 +02005002 "Odd GPU freq value\n"))
5003 val &= ~1;
5004
Deepak Scd25dd52015-07-10 18:31:40 +05305005 I915_WRITE(GEN6_PMINTRMSK, gen6_rps_pm_mask(dev_priv, val));
5006
Chris Wilson8fb55192015-04-07 16:20:28 +01005007 if (val != dev_priv->rps.cur_freq) {
Ville Syrjäläffe02b42015-02-02 19:09:50 +02005008 vlv_punit_write(dev_priv, PUNIT_REG_GPU_FREQ_REQ, val);
Chris Wilson8fb55192015-04-07 16:20:28 +01005009 if (!IS_CHERRYVIEW(dev_priv))
5010 gen6_set_rps_thresholds(dev_priv, val);
5011 }
Ville Syrjäläffe02b42015-02-02 19:09:50 +02005012
Ville Syrjäläffe02b42015-02-02 19:09:50 +02005013 dev_priv->rps.cur_freq = val;
5014 trace_intel_gpu_freq_change(intel_gpu_freq(dev_priv, val));
5015}
5016
Deepak Sa7f6e232015-05-09 18:04:44 +05305017/* vlv_set_rps_idle: Set the frequency to idle, if Gfx clocks are down
Deepak S76c3552f2014-01-30 23:08:16 +05305018 *
5019 * * If Gfx is Idle, then
Deepak Sa7f6e232015-05-09 18:04:44 +05305020 * 1. Forcewake Media well.
5021 * 2. Request idle freq.
5022 * 3. Release Forcewake of Media well.
Deepak S76c3552f2014-01-30 23:08:16 +05305023*/
5024static void vlv_set_rps_idle(struct drm_i915_private *dev_priv)
5025{
Chris Wilsonaed242f2015-03-18 09:48:21 +00005026 u32 val = dev_priv->rps.idle_freq;
Deepak S5549d252014-06-28 11:26:11 +05305027
Chris Wilsonaed242f2015-03-18 09:48:21 +00005028 if (dev_priv->rps.cur_freq <= val)
Deepak S76c3552f2014-01-30 23:08:16 +05305029 return;
5030
Deepak Sa7f6e232015-05-09 18:04:44 +05305031 /* Wake up the media well, as that takes a lot less
5032 * power than the Render well. */
5033 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_MEDIA);
Chris Wilsondc979972016-05-10 14:10:04 +01005034 valleyview_set_rps(dev_priv, val);
Deepak Sa7f6e232015-05-09 18:04:44 +05305035 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_MEDIA);
Deepak S76c3552f2014-01-30 23:08:16 +05305036}
5037
Chris Wilson43cf3bf2015-03-18 09:48:22 +00005038void gen6_rps_busy(struct drm_i915_private *dev_priv)
5039{
5040 mutex_lock(&dev_priv->rps.hw_lock);
5041 if (dev_priv->rps.enabled) {
5042 if (dev_priv->pm_rps_events & (GEN6_PM_RP_DOWN_EI_EXPIRED | GEN6_PM_RP_UP_EI_EXPIRED))
5043 gen6_rps_reset_ei(dev_priv);
5044 I915_WRITE(GEN6_PMINTRMSK,
5045 gen6_rps_pm_mask(dev_priv, dev_priv->rps.cur_freq));
Michał Winiarski2b83c4c2016-06-20 11:58:27 +02005046
Chris Wilsonc33d2472016-07-04 08:08:36 +01005047 gen6_enable_rps_interrupts(dev_priv);
5048
Michał Winiarski2b83c4c2016-06-20 11:58:27 +02005049 /* Ensure we start at the user's desired frequency */
5050 intel_set_rps(dev_priv,
5051 clamp(dev_priv->rps.cur_freq,
5052 dev_priv->rps.min_freq_softlimit,
5053 dev_priv->rps.max_freq_softlimit));
Chris Wilson43cf3bf2015-03-18 09:48:22 +00005054 }
5055 mutex_unlock(&dev_priv->rps.hw_lock);
5056}
5057
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005058void gen6_rps_idle(struct drm_i915_private *dev_priv)
5059{
Chris Wilsonc33d2472016-07-04 08:08:36 +01005060 /* Flush our bottom-half so that it does not race with us
5061 * setting the idle frequency and so that it is bounded by
5062 * our rpm wakeref. And then disable the interrupts to stop any
5063 * futher RPS reclocking whilst we are asleep.
5064 */
5065 gen6_disable_rps_interrupts(dev_priv);
5066
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005067 mutex_lock(&dev_priv->rps.hw_lock);
Chris Wilsonc0951f02013-10-10 21:58:50 +01005068 if (dev_priv->rps.enabled) {
Chris Wilsondc979972016-05-10 14:10:04 +01005069 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Deepak S76c3552f2014-01-30 23:08:16 +05305070 vlv_set_rps_idle(dev_priv);
Daniel Vetter7526ed72014-09-29 15:07:19 +02005071 else
Chris Wilsondc979972016-05-10 14:10:04 +01005072 gen6_set_rps(dev_priv, dev_priv->rps.idle_freq);
Chris Wilsonc0951f02013-10-10 21:58:50 +01005073 dev_priv->rps.last_adj = 0;
Ville Syrjälä12c100b2016-05-23 17:42:48 +03005074 I915_WRITE(GEN6_PMINTRMSK,
5075 gen6_sanitize_rps_pm_mask(dev_priv, ~0));
Chris Wilsonc0951f02013-10-10 21:58:50 +01005076 }
Chris Wilson8d3afd72015-05-21 21:01:47 +01005077 mutex_unlock(&dev_priv->rps.hw_lock);
Chris Wilson1854d5c2015-04-07 16:20:32 +01005078
Chris Wilson8d3afd72015-05-21 21:01:47 +01005079 spin_lock(&dev_priv->rps.client_lock);
Chris Wilson1854d5c2015-04-07 16:20:32 +01005080 while (!list_empty(&dev_priv->rps.clients))
5081 list_del_init(dev_priv->rps.clients.next);
Chris Wilson8d3afd72015-05-21 21:01:47 +01005082 spin_unlock(&dev_priv->rps.client_lock);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005083}
5084
Chris Wilson1854d5c2015-04-07 16:20:32 +01005085void gen6_rps_boost(struct drm_i915_private *dev_priv,
Chris Wilsone61b9952015-04-27 13:41:24 +01005086 struct intel_rps_client *rps,
5087 unsigned long submitted)
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005088{
Chris Wilson8d3afd72015-05-21 21:01:47 +01005089 /* This is intentionally racy! We peek at the state here, then
5090 * validate inside the RPS worker.
5091 */
Chris Wilson67d97da2016-07-04 08:08:31 +01005092 if (!(dev_priv->gt.awake &&
Chris Wilson8d3afd72015-05-21 21:01:47 +01005093 dev_priv->rps.enabled &&
Chris Wilson29ecd78d2016-07-13 09:10:35 +01005094 dev_priv->rps.cur_freq < dev_priv->rps.boost_freq))
Chris Wilson8d3afd72015-05-21 21:01:47 +01005095 return;
Chris Wilson43cf3bf2015-03-18 09:48:22 +00005096
Chris Wilsone61b9952015-04-27 13:41:24 +01005097 /* Force a RPS boost (and don't count it against the client) if
5098 * the GPU is severely congested.
5099 */
Chris Wilsond0bc54f2015-05-21 21:01:48 +01005100 if (rps && time_after(jiffies, submitted + DRM_I915_THROTTLE_JIFFIES))
Chris Wilsone61b9952015-04-27 13:41:24 +01005101 rps = NULL;
5102
Chris Wilson8d3afd72015-05-21 21:01:47 +01005103 spin_lock(&dev_priv->rps.client_lock);
5104 if (rps == NULL || list_empty(&rps->link)) {
5105 spin_lock_irq(&dev_priv->irq_lock);
5106 if (dev_priv->rps.interrupts_enabled) {
5107 dev_priv->rps.client_boost = true;
Chris Wilsonc33d2472016-07-04 08:08:36 +01005108 schedule_work(&dev_priv->rps.work);
Chris Wilson8d3afd72015-05-21 21:01:47 +01005109 }
5110 spin_unlock_irq(&dev_priv->irq_lock);
Chris Wilson1854d5c2015-04-07 16:20:32 +01005111
Chris Wilson2e1b8732015-04-27 13:41:22 +01005112 if (rps != NULL) {
5113 list_add(&rps->link, &dev_priv->rps.clients);
5114 rps->boosts++;
Chris Wilson1854d5c2015-04-07 16:20:32 +01005115 } else
5116 dev_priv->rps.boosts++;
Chris Wilsonc0951f02013-10-10 21:58:50 +01005117 }
Chris Wilson8d3afd72015-05-21 21:01:47 +01005118 spin_unlock(&dev_priv->rps.client_lock);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005119}
5120
Chris Wilsondc979972016-05-10 14:10:04 +01005121void intel_set_rps(struct drm_i915_private *dev_priv, u8 val)
Jesse Barnes0a073b82013-04-17 15:54:58 -07005122{
Chris Wilsondc979972016-05-10 14:10:04 +01005123 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
5124 valleyview_set_rps(dev_priv, val);
Ville Syrjäläffe02b42015-02-02 19:09:50 +02005125 else
Chris Wilsondc979972016-05-10 14:10:04 +01005126 gen6_set_rps(dev_priv, val);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005127}
5128
Chris Wilsondc979972016-05-10 14:10:04 +01005129static void gen9_disable_rc6(struct drm_i915_private *dev_priv)
Zhe Wang20e49362014-11-04 17:07:05 +00005130{
Zhe Wang20e49362014-11-04 17:07:05 +00005131 I915_WRITE(GEN6_RC_CONTROL, 0);
Zhe Wang38c23522015-01-20 12:23:04 +00005132 I915_WRITE(GEN9_PG_ENABLE, 0);
Zhe Wang20e49362014-11-04 17:07:05 +00005133}
5134
Chris Wilsondc979972016-05-10 14:10:04 +01005135static void gen9_disable_rps(struct drm_i915_private *dev_priv)
Akash Goel2030d682016-04-23 00:05:45 +05305136{
Akash Goel2030d682016-04-23 00:05:45 +05305137 I915_WRITE(GEN6_RP_CONTROL, 0);
5138}
5139
Chris Wilsondc979972016-05-10 14:10:04 +01005140static void gen6_disable_rps(struct drm_i915_private *dev_priv)
Daniel Vetter44fc7d52013-07-12 22:43:27 +02005141{
Daniel Vetter44fc7d52013-07-12 22:43:27 +02005142 I915_WRITE(GEN6_RC_CONTROL, 0);
5143 I915_WRITE(GEN6_RPNSWREQ, 1 << 31);
Akash Goel2030d682016-04-23 00:05:45 +05305144 I915_WRITE(GEN6_RP_CONTROL, 0);
Daniel Vetter44fc7d52013-07-12 22:43:27 +02005145}
5146
Chris Wilsondc979972016-05-10 14:10:04 +01005147static void cherryview_disable_rps(struct drm_i915_private *dev_priv)
Deepak S38807742014-05-23 21:00:15 +05305148{
Deepak S38807742014-05-23 21:00:15 +05305149 I915_WRITE(GEN6_RC_CONTROL, 0);
5150}
5151
Chris Wilsondc979972016-05-10 14:10:04 +01005152static void valleyview_disable_rps(struct drm_i915_private *dev_priv)
Jesse Barnesd20d4f02013-04-23 10:09:28 -07005153{
Deepak S98a2e5f2014-08-18 10:35:27 -07005154 /* we're doing forcewake before Disabling RC6,
5155 * This what the BIOS expects when going into suspend */
Mika Kuoppala59bad942015-01-16 11:34:40 +02005156 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Deepak S98a2e5f2014-08-18 10:35:27 -07005157
Jesse Barnesd20d4f02013-04-23 10:09:28 -07005158 I915_WRITE(GEN6_RC_CONTROL, 0);
Jesse Barnesd20d4f02013-04-23 10:09:28 -07005159
Mika Kuoppala59bad942015-01-16 11:34:40 +02005160 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Jesse Barnesd20d4f02013-04-23 10:09:28 -07005161}
5162
Chris Wilsondc979972016-05-10 14:10:04 +01005163static void intel_print_rc6_info(struct drm_i915_private *dev_priv, u32 mode)
Ben Widawskydc39fff2013-10-18 12:32:07 -07005164{
Chris Wilsondc979972016-05-10 14:10:04 +01005165 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Imre Deak91ca6892014-04-14 20:24:25 +03005166 if (mode & (GEN7_RC_CTL_TO_MODE | GEN6_RC_CTL_EI_MODE(1)))
5167 mode = GEN6_RC_CTL_RC6_ENABLE;
5168 else
5169 mode = 0;
5170 }
Chris Wilsondc979972016-05-10 14:10:04 +01005171 if (HAS_RC6p(dev_priv))
Imre Deakb99d49c2016-06-29 19:13:54 +03005172 DRM_DEBUG_DRIVER("Enabling RC6 states: "
5173 "RC6 %s RC6p %s RC6pp %s\n",
5174 onoff(mode & GEN6_RC_CTL_RC6_ENABLE),
5175 onoff(mode & GEN6_RC_CTL_RC6p_ENABLE),
5176 onoff(mode & GEN6_RC_CTL_RC6pp_ENABLE));
Rodrigo Vivi58abf1d2014-10-07 07:06:50 -07005177
5178 else
Imre Deakb99d49c2016-06-29 19:13:54 +03005179 DRM_DEBUG_DRIVER("Enabling RC6 states: RC6 %s\n",
5180 onoff(mode & GEN6_RC_CTL_RC6_ENABLE));
Ben Widawskydc39fff2013-10-18 12:32:07 -07005181}
5182
Chris Wilsondc979972016-05-10 14:10:04 +01005183static bool bxt_check_bios_rc6_setup(struct drm_i915_private *dev_priv)
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05305184{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03005185 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05305186 bool enable_rc6 = true;
5187 unsigned long rc6_ctx_base;
Imre Deakfc619842016-06-29 19:13:55 +03005188 u32 rc_ctl;
5189 int rc_sw_target;
5190
5191 rc_ctl = I915_READ(GEN6_RC_CONTROL);
5192 rc_sw_target = (I915_READ(GEN6_RC_STATE) & RC_SW_TARGET_STATE_MASK) >>
5193 RC_SW_TARGET_STATE_SHIFT;
5194 DRM_DEBUG_DRIVER("BIOS enabled RC states: "
5195 "HW_CTRL %s HW_RC6 %s SW_TARGET_STATE %x\n",
5196 onoff(rc_ctl & GEN6_RC_CTL_HW_ENABLE),
5197 onoff(rc_ctl & GEN6_RC_CTL_RC6_ENABLE),
5198 rc_sw_target);
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05305199
5200 if (!(I915_READ(RC6_LOCATION) & RC6_CTX_IN_DRAM)) {
Imre Deakb99d49c2016-06-29 19:13:54 +03005201 DRM_DEBUG_DRIVER("RC6 Base location not set properly.\n");
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05305202 enable_rc6 = false;
5203 }
5204
5205 /*
5206 * The exact context size is not known for BXT, so assume a page size
5207 * for this check.
5208 */
5209 rc6_ctx_base = I915_READ(RC6_CTX_BASE) & RC6_CTX_BASE_MASK;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03005210 if (!((rc6_ctx_base >= ggtt->stolen_reserved_base) &&
5211 (rc6_ctx_base + PAGE_SIZE <= ggtt->stolen_reserved_base +
5212 ggtt->stolen_reserved_size))) {
Imre Deakb99d49c2016-06-29 19:13:54 +03005213 DRM_DEBUG_DRIVER("RC6 Base address not as expected.\n");
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05305214 enable_rc6 = false;
5215 }
5216
5217 if (!(((I915_READ(PWRCTX_MAXCNT_RCSUNIT) & IDLE_TIME_MASK) > 1) &&
5218 ((I915_READ(PWRCTX_MAXCNT_VCSUNIT0) & IDLE_TIME_MASK) > 1) &&
5219 ((I915_READ(PWRCTX_MAXCNT_BCSUNIT) & IDLE_TIME_MASK) > 1) &&
5220 ((I915_READ(PWRCTX_MAXCNT_VECSUNIT) & IDLE_TIME_MASK) > 1))) {
Imre Deakb99d49c2016-06-29 19:13:54 +03005221 DRM_DEBUG_DRIVER("Engine Idle wait time not set properly.\n");
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05305222 enable_rc6 = false;
5223 }
5224
Imre Deakfc619842016-06-29 19:13:55 +03005225 if (!I915_READ(GEN8_PUSHBUS_CONTROL) ||
5226 !I915_READ(GEN8_PUSHBUS_ENABLE) ||
5227 !I915_READ(GEN8_PUSHBUS_SHIFT)) {
5228 DRM_DEBUG_DRIVER("Pushbus not setup properly.\n");
5229 enable_rc6 = false;
5230 }
5231
5232 if (!I915_READ(GEN6_GFXPAUSE)) {
5233 DRM_DEBUG_DRIVER("GFX pause not setup properly.\n");
5234 enable_rc6 = false;
5235 }
5236
5237 if (!I915_READ(GEN8_MISC_CTRL0)) {
5238 DRM_DEBUG_DRIVER("GPM control not setup properly.\n");
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05305239 enable_rc6 = false;
5240 }
5241
5242 return enable_rc6;
5243}
5244
Chris Wilsondc979972016-05-10 14:10:04 +01005245int sanitize_rc6_option(struct drm_i915_private *dev_priv, int enable_rc6)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005246{
Daniel Vettere7d66d82015-06-15 23:23:54 +02005247 /* No RC6 before Ironlake and code is gone for ilk. */
Chris Wilsondc979972016-05-10 14:10:04 +01005248 if (INTEL_INFO(dev_priv)->gen < 6)
Imre Deake6069ca2014-04-18 16:01:02 +03005249 return 0;
5250
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05305251 if (!enable_rc6)
5252 return 0;
5253
Chris Wilsondc979972016-05-10 14:10:04 +01005254 if (IS_BROXTON(dev_priv) && !bxt_check_bios_rc6_setup(dev_priv)) {
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05305255 DRM_INFO("RC6 disabled by BIOS\n");
5256 return 0;
5257 }
5258
Daniel Vetter456470e2012-08-08 23:35:40 +02005259 /* Respect the kernel parameter if it is set */
Imre Deake6069ca2014-04-18 16:01:02 +03005260 if (enable_rc6 >= 0) {
5261 int mask;
5262
Chris Wilsondc979972016-05-10 14:10:04 +01005263 if (HAS_RC6p(dev_priv))
Imre Deake6069ca2014-04-18 16:01:02 +03005264 mask = INTEL_RC6_ENABLE | INTEL_RC6p_ENABLE |
5265 INTEL_RC6pp_ENABLE;
5266 else
5267 mask = INTEL_RC6_ENABLE;
5268
5269 if ((enable_rc6 & mask) != enable_rc6)
Imre Deakb99d49c2016-06-29 19:13:54 +03005270 DRM_DEBUG_DRIVER("Adjusting RC6 mask to %d "
5271 "(requested %d, valid %d)\n",
5272 enable_rc6 & mask, enable_rc6, mask);
Imre Deake6069ca2014-04-18 16:01:02 +03005273
5274 return enable_rc6 & mask;
5275 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005276
Chris Wilsondc979972016-05-10 14:10:04 +01005277 if (IS_IVYBRIDGE(dev_priv))
Ben Widawskycca84a12014-01-28 20:25:38 -08005278 return (INTEL_RC6_ENABLE | INTEL_RC6p_ENABLE);
Ben Widawsky8bade1a2014-01-28 20:25:39 -08005279
5280 return INTEL_RC6_ENABLE;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005281}
5282
Chris Wilsondc979972016-05-10 14:10:04 +01005283static void gen6_init_rps_frequencies(struct drm_i915_private *dev_priv)
Imre Deake6069ca2014-04-18 16:01:02 +03005284{
Ben Widawsky3280e8b2014-03-31 17:16:42 -07005285 /* All of these values are in units of 50MHz */
Chris Wilson773ea9a2016-07-13 09:10:33 +01005286
Tom O'Rourke93ee2922014-11-19 14:21:52 -08005287 /* static values from HW: RP0 > RP1 > RPn (min_freq) */
Chris Wilsondc979972016-05-10 14:10:04 +01005288 if (IS_BROXTON(dev_priv)) {
Chris Wilson773ea9a2016-07-13 09:10:33 +01005289 u32 rp_state_cap = I915_READ(BXT_RP_STATE_CAP);
Bob Paauwe35040562015-06-25 14:54:07 -07005290 dev_priv->rps.rp0_freq = (rp_state_cap >> 16) & 0xff;
5291 dev_priv->rps.rp1_freq = (rp_state_cap >> 8) & 0xff;
5292 dev_priv->rps.min_freq = (rp_state_cap >> 0) & 0xff;
5293 } else {
Chris Wilson773ea9a2016-07-13 09:10:33 +01005294 u32 rp_state_cap = I915_READ(GEN6_RP_STATE_CAP);
Bob Paauwe35040562015-06-25 14:54:07 -07005295 dev_priv->rps.rp0_freq = (rp_state_cap >> 0) & 0xff;
5296 dev_priv->rps.rp1_freq = (rp_state_cap >> 8) & 0xff;
5297 dev_priv->rps.min_freq = (rp_state_cap >> 16) & 0xff;
5298 }
Ben Widawsky3280e8b2014-03-31 17:16:42 -07005299 /* hw_max = RP0 until we check for overclocking */
Chris Wilson773ea9a2016-07-13 09:10:33 +01005300 dev_priv->rps.max_freq = dev_priv->rps.rp0_freq;
Ben Widawsky3280e8b2014-03-31 17:16:42 -07005301
Tom O'Rourke93ee2922014-11-19 14:21:52 -08005302 dev_priv->rps.efficient_freq = dev_priv->rps.rp1_freq;
Chris Wilsondc979972016-05-10 14:10:04 +01005303 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv) ||
5304 IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) {
Chris Wilson773ea9a2016-07-13 09:10:33 +01005305 u32 ddcc_status = 0;
5306
5307 if (sandybridge_pcode_read(dev_priv,
5308 HSW_PCODE_DYNAMIC_DUTY_CYCLE_CONTROL,
5309 &ddcc_status) == 0)
Tom O'Rourke93ee2922014-11-19 14:21:52 -08005310 dev_priv->rps.efficient_freq =
Tom O'Rourke46efa4a2015-02-10 23:06:46 -08005311 clamp_t(u8,
5312 ((ddcc_status >> 8) & 0xff),
5313 dev_priv->rps.min_freq,
5314 dev_priv->rps.max_freq);
Tom O'Rourke93ee2922014-11-19 14:21:52 -08005315 }
5316
Chris Wilsondc979972016-05-10 14:10:04 +01005317 if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) {
Akash Goelc5e06882015-06-29 14:50:19 +05305318 /* Store the frequency values in 16.66 MHZ units, which is
Chris Wilson773ea9a2016-07-13 09:10:33 +01005319 * the natural hardware unit for SKL
5320 */
Akash Goelc5e06882015-06-29 14:50:19 +05305321 dev_priv->rps.rp0_freq *= GEN9_FREQ_SCALER;
5322 dev_priv->rps.rp1_freq *= GEN9_FREQ_SCALER;
5323 dev_priv->rps.min_freq *= GEN9_FREQ_SCALER;
5324 dev_priv->rps.max_freq *= GEN9_FREQ_SCALER;
5325 dev_priv->rps.efficient_freq *= GEN9_FREQ_SCALER;
5326 }
Ben Widawsky3280e8b2014-03-31 17:16:42 -07005327}
5328
Chris Wilson3a45b052016-07-13 09:10:32 +01005329static void reset_rps(struct drm_i915_private *dev_priv,
5330 void (*set)(struct drm_i915_private *, u8))
5331{
5332 u8 freq = dev_priv->rps.cur_freq;
5333
5334 /* force a reset */
5335 dev_priv->rps.power = -1;
5336 dev_priv->rps.cur_freq = -1;
5337
5338 set(dev_priv, freq);
5339}
5340
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00005341/* See the Gen9_GT_PM_Programming_Guide doc for the below */
Chris Wilsondc979972016-05-10 14:10:04 +01005342static void gen9_enable_rps(struct drm_i915_private *dev_priv)
Zhe Wang20e49362014-11-04 17:07:05 +00005343{
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00005344 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
5345
Sagar Arun Kamble23eafea2015-08-23 17:52:48 +05305346 /* WaGsvDisableTurbo: Workaround to disable turbo on BXT A* */
Chris Wilsondc979972016-05-10 14:10:04 +01005347 if (IS_BXT_REVID(dev_priv, 0, BXT_REVID_A1)) {
Akash Goel2030d682016-04-23 00:05:45 +05305348 /*
5349 * BIOS could leave the Hw Turbo enabled, so need to explicitly
5350 * clear out the Control register just to avoid inconsitency
5351 * with debugfs interface, which will show Turbo as enabled
5352 * only and that is not expected by the User after adding the
5353 * WaGsvDisableTurbo. Apart from this there is no problem even
5354 * if the Turbo is left enabled in the Control register, as the
5355 * Up/Down interrupts would remain masked.
5356 */
Chris Wilsondc979972016-05-10 14:10:04 +01005357 gen9_disable_rps(dev_priv);
Sagar Arun Kamble23eafea2015-08-23 17:52:48 +05305358 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
5359 return;
5360 }
5361
Akash Goel0beb0592015-03-06 11:07:20 +05305362 /* Program defaults and thresholds for RPS*/
5363 I915_WRITE(GEN6_RC_VIDEO_FREQ,
5364 GEN9_FREQUENCY(dev_priv->rps.rp1_freq));
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00005365
Akash Goel0beb0592015-03-06 11:07:20 +05305366 /* 1 second timeout*/
5367 I915_WRITE(GEN6_RP_DOWN_TIMEOUT,
5368 GT_INTERVAL_FROM_US(dev_priv, 1000000));
5369
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00005370 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 0xa);
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00005371
Akash Goel0beb0592015-03-06 11:07:20 +05305372 /* Leaning on the below call to gen6_set_rps to program/setup the
5373 * Up/Down EI & threshold registers, as well as the RP_CONTROL,
5374 * RP_INTERRUPT_LIMITS & RPNSWREQ registers */
Chris Wilson3a45b052016-07-13 09:10:32 +01005375 reset_rps(dev_priv, gen6_set_rps);
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00005376
5377 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
5378}
5379
Chris Wilsondc979972016-05-10 14:10:04 +01005380static void gen9_enable_rc6(struct drm_i915_private *dev_priv)
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00005381{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00005382 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05305383 enum intel_engine_id id;
Zhe Wang20e49362014-11-04 17:07:05 +00005384 uint32_t rc6_mask = 0;
Zhe Wang20e49362014-11-04 17:07:05 +00005385
5386 /* 1a: Software RC state - RC0 */
5387 I915_WRITE(GEN6_RC_STATE, 0);
5388
5389 /* 1b: Get forcewake during program sequence. Although the driver
5390 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
Mika Kuoppala59bad942015-01-16 11:34:40 +02005391 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Zhe Wang20e49362014-11-04 17:07:05 +00005392
5393 /* 2a: Disable RC states. */
5394 I915_WRITE(GEN6_RC_CONTROL, 0);
5395
5396 /* 2b: Program RC6 thresholds.*/
Sagar Arun Kamble63a4dec2015-09-12 10:17:53 +05305397
5398 /* WaRsDoubleRc6WrlWithCoarsePowerGating: Doubling WRL only when CPG is enabled */
Chris Wilsondc979972016-05-10 14:10:04 +01005399 if (IS_SKYLAKE(dev_priv))
Sagar Arun Kamble63a4dec2015-09-12 10:17:53 +05305400 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 108 << 16);
5401 else
5402 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 54 << 16);
Zhe Wang20e49362014-11-04 17:07:05 +00005403 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
5404 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
Akash Goel3b3f1652016-10-13 22:44:48 +05305405 for_each_engine(engine, dev_priv, id)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00005406 I915_WRITE(RING_MAX_IDLE(engine->mmio_base), 10);
Sagar Arun Kamble97c322e2015-09-12 10:17:54 +05305407
Dave Gordon1a3d1892016-05-13 15:36:30 +01005408 if (HAS_GUC(dev_priv))
Sagar Arun Kamble97c322e2015-09-12 10:17:54 +05305409 I915_WRITE(GUC_MAX_IDLE_COUNT, 0xA);
5410
Zhe Wang20e49362014-11-04 17:07:05 +00005411 I915_WRITE(GEN6_RC_SLEEP, 0);
Zhe Wang20e49362014-11-04 17:07:05 +00005412
Zhe Wang38c23522015-01-20 12:23:04 +00005413 /* 2c: Program Coarse Power Gating Policies. */
5414 I915_WRITE(GEN9_MEDIA_PG_IDLE_HYSTERESIS, 25);
5415 I915_WRITE(GEN9_RENDER_PG_IDLE_HYSTERESIS, 25);
5416
Zhe Wang20e49362014-11-04 17:07:05 +00005417 /* 3a: Enable RC6 */
Chris Wilsondc979972016-05-10 14:10:04 +01005418 if (intel_enable_rc6() & INTEL_RC6_ENABLE)
Zhe Wang20e49362014-11-04 17:07:05 +00005419 rc6_mask = GEN6_RC_CTL_RC6_ENABLE;
Jani Nikula87ad3212016-01-14 12:53:34 +02005420 DRM_INFO("RC6 %s\n", onoff(rc6_mask & GEN6_RC_CTL_RC6_ENABLE));
Jani Nikula4ff40a42016-09-26 15:07:51 +03005421 /* WaRsUseTimeoutMode:bxt */
Jani Nikula9fc736e2016-09-16 16:59:46 +03005422 if (IS_BXT_REVID(dev_priv, 0, BXT_REVID_A1)) {
Sagar Arun Kamble3e7732a2015-10-01 20:29:27 +05305423 I915_WRITE(GEN6_RC6_THRESHOLD, 625); /* 800us */
Sagar Arun Kamblee3429cd2015-09-12 10:17:52 +05305424 I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
5425 GEN7_RC_CTL_TO_MODE |
5426 rc6_mask);
Sagar Arun Kamble3e7732a2015-10-01 20:29:27 +05305427 } else {
5428 I915_WRITE(GEN6_RC6_THRESHOLD, 37500); /* 37.5/125ms per EI */
Sagar Arun Kamblee3429cd2015-09-12 10:17:52 +05305429 I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
5430 GEN6_RC_CTL_EI_MODE(1) |
5431 rc6_mask);
Sagar Arun Kamble3e7732a2015-10-01 20:29:27 +05305432 }
Zhe Wang20e49362014-11-04 17:07:05 +00005433
Sagar Kamblecb07bae2015-04-12 11:28:14 +05305434 /*
5435 * 3b: Enable Coarse Power Gating only when RC6 is enabled.
Sagar Arun Kamblef2d2fe92015-09-12 10:17:51 +05305436 * WaRsDisableCoarsePowerGating:skl,bxt - Render/Media PG need to be disabled with RC6.
Sagar Kamblecb07bae2015-04-12 11:28:14 +05305437 */
Chris Wilsondc979972016-05-10 14:10:04 +01005438 if (NEEDS_WaRsDisableCoarsePowerGating(dev_priv))
Sagar Arun Kamblef2d2fe92015-09-12 10:17:51 +05305439 I915_WRITE(GEN9_PG_ENABLE, 0);
5440 else
5441 I915_WRITE(GEN9_PG_ENABLE, (rc6_mask & GEN6_RC_CTL_RC6_ENABLE) ?
5442 (GEN9_RENDER_PG_ENABLE | GEN9_MEDIA_PG_ENABLE) : 0);
Zhe Wang38c23522015-01-20 12:23:04 +00005443
Mika Kuoppala59bad942015-01-16 11:34:40 +02005444 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Zhe Wang20e49362014-11-04 17:07:05 +00005445}
5446
Chris Wilsondc979972016-05-10 14:10:04 +01005447static void gen8_enable_rps(struct drm_i915_private *dev_priv)
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005448{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00005449 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05305450 enum intel_engine_id id;
Tom O'Rourke93ee2922014-11-19 14:21:52 -08005451 uint32_t rc6_mask = 0;
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005452
5453 /* 1a: Software RC state - RC0 */
5454 I915_WRITE(GEN6_RC_STATE, 0);
5455
5456 /* 1c & 1d: Get forcewake during program sequence. Although the driver
5457 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
Mika Kuoppala59bad942015-01-16 11:34:40 +02005458 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005459
5460 /* 2a: Disable RC states. */
5461 I915_WRITE(GEN6_RC_CONTROL, 0);
5462
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005463 /* 2b: Program RC6 thresholds.*/
5464 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16);
5465 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
5466 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
Akash Goel3b3f1652016-10-13 22:44:48 +05305467 for_each_engine(engine, dev_priv, id)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00005468 I915_WRITE(RING_MAX_IDLE(engine->mmio_base), 10);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005469 I915_WRITE(GEN6_RC_SLEEP, 0);
Chris Wilsondc979972016-05-10 14:10:04 +01005470 if (IS_BROADWELL(dev_priv))
Tom O'Rourke0d68b252014-04-09 11:44:06 -07005471 I915_WRITE(GEN6_RC6_THRESHOLD, 625); /* 800us/1.28 for TO */
5472 else
5473 I915_WRITE(GEN6_RC6_THRESHOLD, 50000); /* 50/125ms per EI */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005474
5475 /* 3: Enable RC6 */
Chris Wilsondc979972016-05-10 14:10:04 +01005476 if (intel_enable_rc6() & INTEL_RC6_ENABLE)
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005477 rc6_mask = GEN6_RC_CTL_RC6_ENABLE;
Chris Wilsondc979972016-05-10 14:10:04 +01005478 intel_print_rc6_info(dev_priv, rc6_mask);
5479 if (IS_BROADWELL(dev_priv))
Tom O'Rourke0d68b252014-04-09 11:44:06 -07005480 I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
5481 GEN7_RC_CTL_TO_MODE |
5482 rc6_mask);
5483 else
5484 I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
5485 GEN6_RC_CTL_EI_MODE(1) |
5486 rc6_mask);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005487
5488 /* 4 Program defaults and thresholds for RPS*/
Ben Widawskyf9bdc582014-03-31 17:16:41 -07005489 I915_WRITE(GEN6_RPNSWREQ,
5490 HSW_FREQUENCY(dev_priv->rps.rp1_freq));
5491 I915_WRITE(GEN6_RC_VIDEO_FREQ,
5492 HSW_FREQUENCY(dev_priv->rps.rp1_freq));
Daniel Vetter7526ed72014-09-29 15:07:19 +02005493 /* NB: Docs say 1s, and 1000000 - which aren't equivalent */
5494 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 100000000 / 128); /* 1 second timeout */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005495
Daniel Vetter7526ed72014-09-29 15:07:19 +02005496 /* Docs recommend 900MHz, and 300 MHz respectively */
5497 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS,
5498 dev_priv->rps.max_freq_softlimit << 24 |
5499 dev_priv->rps.min_freq_softlimit << 16);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005500
Daniel Vetter7526ed72014-09-29 15:07:19 +02005501 I915_WRITE(GEN6_RP_UP_THRESHOLD, 7600000 / 128); /* 76ms busyness per EI, 90% */
5502 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 31300000 / 128); /* 313ms busyness per EI, 70%*/
5503 I915_WRITE(GEN6_RP_UP_EI, 66000); /* 84.48ms, XXX: random? */
5504 I915_WRITE(GEN6_RP_DOWN_EI, 350000); /* 448ms, XXX: random? */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005505
Daniel Vetter7526ed72014-09-29 15:07:19 +02005506 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005507
5508 /* 5: Enable RPS */
Daniel Vetter7526ed72014-09-29 15:07:19 +02005509 I915_WRITE(GEN6_RP_CONTROL,
5510 GEN6_RP_MEDIA_TURBO |
5511 GEN6_RP_MEDIA_HW_NORMAL_MODE |
5512 GEN6_RP_MEDIA_IS_GFX |
5513 GEN6_RP_ENABLE |
5514 GEN6_RP_UP_BUSY_AVG |
5515 GEN6_RP_DOWN_IDLE_AVG);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005516
Daniel Vetter7526ed72014-09-29 15:07:19 +02005517 /* 6: Ring frequency + overclocking (our driver does this later */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005518
Chris Wilson3a45b052016-07-13 09:10:32 +01005519 reset_rps(dev_priv, gen6_set_rps);
Daniel Vetter7526ed72014-09-29 15:07:19 +02005520
Mika Kuoppala59bad942015-01-16 11:34:40 +02005521 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005522}
5523
Chris Wilsondc979972016-05-10 14:10:04 +01005524static void gen6_enable_rps(struct drm_i915_private *dev_priv)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005525{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00005526 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05305527 enum intel_engine_id id;
Chris Wilson99ac9612016-07-13 09:10:34 +01005528 u32 rc6vids, rc6_mask = 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005529 u32 gtfifodbg;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005530 int rc6_mode;
Dave Gordonb4ac5af2016-03-24 11:20:38 +00005531 int ret;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005532
Jesse Barnes4fc688c2012-11-02 11:14:01 -07005533 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02005534
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005535 /* Here begins a magic sequence of register writes to enable
5536 * auto-downclocking.
5537 *
5538 * Perhaps there might be some value in exposing these to
5539 * userspace...
5540 */
5541 I915_WRITE(GEN6_RC_STATE, 0);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005542
5543 /* Clear the DBG now so we don't confuse earlier errors */
Ville Syrjälä297b32e2016-04-13 21:09:30 +03005544 gtfifodbg = I915_READ(GTFIFODBG);
5545 if (gtfifodbg) {
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005546 DRM_ERROR("GT fifo had a previous error %x\n", gtfifodbg);
5547 I915_WRITE(GTFIFODBG, gtfifodbg);
5548 }
5549
Mika Kuoppala59bad942015-01-16 11:34:40 +02005550 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005551
5552 /* disable the counters and set deterministic thresholds */
5553 I915_WRITE(GEN6_RC_CONTROL, 0);
5554
5555 I915_WRITE(GEN6_RC1_WAKE_RATE_LIMIT, 1000 << 16);
5556 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16 | 30);
5557 I915_WRITE(GEN6_RC6pp_WAKE_RATE_LIMIT, 30);
5558 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
5559 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
5560
Akash Goel3b3f1652016-10-13 22:44:48 +05305561 for_each_engine(engine, dev_priv, id)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00005562 I915_WRITE(RING_MAX_IDLE(engine->mmio_base), 10);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005563
5564 I915_WRITE(GEN6_RC_SLEEP, 0);
5565 I915_WRITE(GEN6_RC1e_THRESHOLD, 1000);
Chris Wilsondc979972016-05-10 14:10:04 +01005566 if (IS_IVYBRIDGE(dev_priv))
Stéphane Marchesin351aa562013-08-13 11:55:17 -07005567 I915_WRITE(GEN6_RC6_THRESHOLD, 125000);
5568 else
5569 I915_WRITE(GEN6_RC6_THRESHOLD, 50000);
Stéphane Marchesin0920a482013-01-29 19:41:59 -08005570 I915_WRITE(GEN6_RC6p_THRESHOLD, 150000);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005571 I915_WRITE(GEN6_RC6pp_THRESHOLD, 64000); /* unused */
5572
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03005573 /* Check if we are enabling RC6 */
Chris Wilsondc979972016-05-10 14:10:04 +01005574 rc6_mode = intel_enable_rc6();
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005575 if (rc6_mode & INTEL_RC6_ENABLE)
5576 rc6_mask |= GEN6_RC_CTL_RC6_ENABLE;
5577
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03005578 /* We don't use those on Haswell */
Chris Wilsondc979972016-05-10 14:10:04 +01005579 if (!IS_HASWELL(dev_priv)) {
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03005580 if (rc6_mode & INTEL_RC6p_ENABLE)
5581 rc6_mask |= GEN6_RC_CTL_RC6p_ENABLE;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005582
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03005583 if (rc6_mode & INTEL_RC6pp_ENABLE)
5584 rc6_mask |= GEN6_RC_CTL_RC6pp_ENABLE;
5585 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005586
Chris Wilsondc979972016-05-10 14:10:04 +01005587 intel_print_rc6_info(dev_priv, rc6_mask);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005588
5589 I915_WRITE(GEN6_RC_CONTROL,
5590 rc6_mask |
5591 GEN6_RC_CTL_EI_MODE(1) |
5592 GEN6_RC_CTL_HW_ENABLE);
5593
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005594 /* Power down if completely idle for over 50ms */
5595 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 50000);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005596 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005597
Chris Wilson3a45b052016-07-13 09:10:32 +01005598 reset_rps(dev_priv, gen6_set_rps);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005599
Ben Widawsky31643d52012-09-26 10:34:01 -07005600 rc6vids = 0;
5601 ret = sandybridge_pcode_read(dev_priv, GEN6_PCODE_READ_RC6VIDS, &rc6vids);
Chris Wilsondc979972016-05-10 14:10:04 +01005602 if (IS_GEN6(dev_priv) && ret) {
Ben Widawsky31643d52012-09-26 10:34:01 -07005603 DRM_DEBUG_DRIVER("Couldn't check for BIOS workaround\n");
Chris Wilsondc979972016-05-10 14:10:04 +01005604 } else if (IS_GEN6(dev_priv) && (GEN6_DECODE_RC6_VID(rc6vids & 0xff) < 450)) {
Ben Widawsky31643d52012-09-26 10:34:01 -07005605 DRM_DEBUG_DRIVER("You should update your BIOS. Correcting minimum rc6 voltage (%dmV->%dmV)\n",
5606 GEN6_DECODE_RC6_VID(rc6vids & 0xff), 450);
5607 rc6vids &= 0xffff00;
5608 rc6vids |= GEN6_ENCODE_RC6_VID(450);
5609 ret = sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_RC6VIDS, rc6vids);
5610 if (ret)
5611 DRM_ERROR("Couldn't fix incorrect rc6 voltage\n");
5612 }
5613
Mika Kuoppala59bad942015-01-16 11:34:40 +02005614 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005615}
5616
Chris Wilsonfb7404e2016-07-13 09:10:38 +01005617static void gen6_update_ring_freq(struct drm_i915_private *dev_priv)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005618{
5619 int min_freq = 15;
Chris Wilson3ebecd02013-04-12 19:10:13 +01005620 unsigned int gpu_freq;
5621 unsigned int max_ia_freq, min_ring_freq;
Akash Goel4c8c7742015-06-29 14:50:20 +05305622 unsigned int max_gpu_freq, min_gpu_freq;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005623 int scaling_factor = 180;
Ben Widawskyeda79642013-10-07 17:15:48 -03005624 struct cpufreq_policy *policy;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005625
Jesse Barnes4fc688c2012-11-02 11:14:01 -07005626 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02005627
Ben Widawskyeda79642013-10-07 17:15:48 -03005628 policy = cpufreq_cpu_get(0);
5629 if (policy) {
5630 max_ia_freq = policy->cpuinfo.max_freq;
5631 cpufreq_cpu_put(policy);
5632 } else {
5633 /*
5634 * Default to measured freq if none found, PCU will ensure we
5635 * don't go over
5636 */
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005637 max_ia_freq = tsc_khz;
Ben Widawskyeda79642013-10-07 17:15:48 -03005638 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005639
5640 /* Convert from kHz to MHz */
5641 max_ia_freq /= 1000;
5642
Ben Widawsky153b4b952013-10-22 22:05:09 -07005643 min_ring_freq = I915_READ(DCLK) & 0xf;
Ben Widawskyf6aca452013-10-02 09:25:02 -07005644 /* convert DDR frequency from units of 266.6MHz to bandwidth */
5645 min_ring_freq = mult_frac(min_ring_freq, 8, 3);
Chris Wilson3ebecd02013-04-12 19:10:13 +01005646
Chris Wilsondc979972016-05-10 14:10:04 +01005647 if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) {
Akash Goel4c8c7742015-06-29 14:50:20 +05305648 /* Convert GT frequency to 50 HZ units */
5649 min_gpu_freq = dev_priv->rps.min_freq / GEN9_FREQ_SCALER;
5650 max_gpu_freq = dev_priv->rps.max_freq / GEN9_FREQ_SCALER;
5651 } else {
5652 min_gpu_freq = dev_priv->rps.min_freq;
5653 max_gpu_freq = dev_priv->rps.max_freq;
5654 }
5655
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005656 /*
5657 * For each potential GPU frequency, load a ring frequency we'd like
5658 * to use for memory access. We do this by specifying the IA frequency
5659 * the PCU should use as a reference to determine the ring frequency.
5660 */
Akash Goel4c8c7742015-06-29 14:50:20 +05305661 for (gpu_freq = max_gpu_freq; gpu_freq >= min_gpu_freq; gpu_freq--) {
5662 int diff = max_gpu_freq - gpu_freq;
Chris Wilson3ebecd02013-04-12 19:10:13 +01005663 unsigned int ia_freq = 0, ring_freq = 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005664
Chris Wilsondc979972016-05-10 14:10:04 +01005665 if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) {
Akash Goel4c8c7742015-06-29 14:50:20 +05305666 /*
5667 * ring_freq = 2 * GT. ring_freq is in 100MHz units
5668 * No floor required for ring frequency on SKL.
5669 */
5670 ring_freq = gpu_freq;
Chris Wilsondc979972016-05-10 14:10:04 +01005671 } else if (INTEL_INFO(dev_priv)->gen >= 8) {
Ben Widawsky46c764d2013-11-02 21:07:49 -07005672 /* max(2 * GT, DDR). NB: GT is 50MHz units */
5673 ring_freq = max(min_ring_freq, gpu_freq);
Chris Wilsondc979972016-05-10 14:10:04 +01005674 } else if (IS_HASWELL(dev_priv)) {
Ben Widawskyf6aca452013-10-02 09:25:02 -07005675 ring_freq = mult_frac(gpu_freq, 5, 4);
Chris Wilson3ebecd02013-04-12 19:10:13 +01005676 ring_freq = max(min_ring_freq, ring_freq);
5677 /* leave ia_freq as the default, chosen by cpufreq */
5678 } else {
5679 /* On older processors, there is no separate ring
5680 * clock domain, so in order to boost the bandwidth
5681 * of the ring, we need to upclock the CPU (ia_freq).
5682 *
5683 * For GPU frequencies less than 750MHz,
5684 * just use the lowest ring freq.
5685 */
5686 if (gpu_freq < min_freq)
5687 ia_freq = 800;
5688 else
5689 ia_freq = max_ia_freq - ((diff * scaling_factor) / 2);
5690 ia_freq = DIV_ROUND_CLOSEST(ia_freq, 100);
5691 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005692
Ben Widawsky42c05262012-09-26 10:34:00 -07005693 sandybridge_pcode_write(dev_priv,
5694 GEN6_PCODE_WRITE_MIN_FREQ_TABLE,
Chris Wilson3ebecd02013-04-12 19:10:13 +01005695 ia_freq << GEN6_PCODE_FREQ_IA_RATIO_SHIFT |
5696 ring_freq << GEN6_PCODE_FREQ_RING_RATIO_SHIFT |
5697 gpu_freq);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005698 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005699}
5700
Ville Syrjälä03af2042014-06-28 02:03:53 +03005701static int cherryview_rps_max_freq(struct drm_i915_private *dev_priv)
Deepak S2b6b3a02014-05-27 15:59:30 +05305702{
5703 u32 val, rp0;
5704
Jani Nikula5b5929c2015-10-07 11:17:46 +03005705 val = vlv_punit_read(dev_priv, FB_GFX_FMAX_AT_VMAX_FUSE);
Deepak S2b6b3a02014-05-27 15:59:30 +05305706
Imre Deak43b67992016-08-31 19:13:02 +03005707 switch (INTEL_INFO(dev_priv)->sseu.eu_total) {
Jani Nikula5b5929c2015-10-07 11:17:46 +03005708 case 8:
5709 /* (2 * 4) config */
5710 rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS4EU_FUSE_SHIFT);
5711 break;
5712 case 12:
5713 /* (2 * 6) config */
5714 rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS6EU_FUSE_SHIFT);
5715 break;
5716 case 16:
5717 /* (2 * 8) config */
5718 default:
5719 /* Setting (2 * 8) Min RP0 for any other combination */
5720 rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS8EU_FUSE_SHIFT);
5721 break;
Deepak S095acd52015-01-17 11:05:59 +05305722 }
Jani Nikula5b5929c2015-10-07 11:17:46 +03005723
5724 rp0 = (rp0 & FB_GFX_FREQ_FUSE_MASK);
5725
Deepak S2b6b3a02014-05-27 15:59:30 +05305726 return rp0;
5727}
5728
5729static int cherryview_rps_rpe_freq(struct drm_i915_private *dev_priv)
5730{
5731 u32 val, rpe;
5732
5733 val = vlv_punit_read(dev_priv, PUNIT_GPU_DUTYCYCLE_REG);
5734 rpe = (val >> PUNIT_GPU_DUTYCYCLE_RPE_FREQ_SHIFT) & PUNIT_GPU_DUTYCYCLE_RPE_FREQ_MASK;
5735
5736 return rpe;
5737}
5738
Deepak S7707df42014-07-12 18:46:14 +05305739static int cherryview_rps_guar_freq(struct drm_i915_private *dev_priv)
5740{
5741 u32 val, rp1;
5742
Jani Nikula5b5929c2015-10-07 11:17:46 +03005743 val = vlv_punit_read(dev_priv, FB_GFX_FMAX_AT_VMAX_FUSE);
5744 rp1 = (val & FB_GFX_FREQ_FUSE_MASK);
5745
Deepak S7707df42014-07-12 18:46:14 +05305746 return rp1;
5747}
5748
Deepak Sf8f2b002014-07-10 13:16:21 +05305749static int valleyview_rps_guar_freq(struct drm_i915_private *dev_priv)
5750{
5751 u32 val, rp1;
5752
5753 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FREQ_FUSE);
5754
5755 rp1 = (val & FB_GFX_FGUARANTEED_FREQ_FUSE_MASK) >> FB_GFX_FGUARANTEED_FREQ_FUSE_SHIFT;
5756
5757 return rp1;
5758}
5759
Ville Syrjälä03af2042014-06-28 02:03:53 +03005760static int valleyview_rps_max_freq(struct drm_i915_private *dev_priv)
Jesse Barnes0a073b82013-04-17 15:54:58 -07005761{
5762 u32 val, rp0;
5763
Jani Nikula64936252013-05-22 15:36:20 +03005764 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FREQ_FUSE);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005765
5766 rp0 = (val & FB_GFX_MAX_FREQ_FUSE_MASK) >> FB_GFX_MAX_FREQ_FUSE_SHIFT;
5767 /* Clamp to max */
5768 rp0 = min_t(u32, rp0, 0xea);
5769
5770 return rp0;
5771}
5772
5773static int valleyview_rps_rpe_freq(struct drm_i915_private *dev_priv)
5774{
5775 u32 val, rpe;
5776
Jani Nikula64936252013-05-22 15:36:20 +03005777 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FMAX_FUSE_LO);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005778 rpe = (val & FB_FMAX_VMIN_FREQ_LO_MASK) >> FB_FMAX_VMIN_FREQ_LO_SHIFT;
Jani Nikula64936252013-05-22 15:36:20 +03005779 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FMAX_FUSE_HI);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005780 rpe |= (val & FB_FMAX_VMIN_FREQ_HI_MASK) << 5;
5781
5782 return rpe;
5783}
5784
Ville Syrjälä03af2042014-06-28 02:03:53 +03005785static int valleyview_rps_min_freq(struct drm_i915_private *dev_priv)
Jesse Barnes0a073b82013-04-17 15:54:58 -07005786{
Imre Deak36146032014-12-04 18:39:35 +02005787 u32 val;
5788
5789 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_LFM) & 0xff;
5790 /*
5791 * According to the BYT Punit GPU turbo HAS 1.1.6.3 the minimum value
5792 * for the minimum frequency in GPLL mode is 0xc1. Contrary to this on
5793 * a BYT-M B0 the above register contains 0xbf. Moreover when setting
5794 * a frequency Punit will not allow values below 0xc0. Clamp it 0xc0
5795 * to make sure it matches what Punit accepts.
5796 */
5797 return max_t(u32, val, 0xc0);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005798}
5799
Imre Deakae484342014-03-31 15:10:44 +03005800/* Check that the pctx buffer wasn't move under us. */
5801static void valleyview_check_pctx(struct drm_i915_private *dev_priv)
5802{
5803 unsigned long pctx_addr = I915_READ(VLV_PCBR) & ~4095;
5804
5805 WARN_ON(pctx_addr != dev_priv->mm.stolen_base +
5806 dev_priv->vlv_pctx->stolen->start);
5807}
5808
Deepak S38807742014-05-23 21:00:15 +05305809
5810/* Check that the pcbr address is not empty. */
5811static void cherryview_check_pctx(struct drm_i915_private *dev_priv)
5812{
5813 unsigned long pctx_addr = I915_READ(VLV_PCBR) & ~4095;
5814
5815 WARN_ON((pctx_addr >> VLV_PCBR_ADDR_SHIFT) == 0);
5816}
5817
Chris Wilsondc979972016-05-10 14:10:04 +01005818static void cherryview_setup_pctx(struct drm_i915_private *dev_priv)
Deepak S38807742014-05-23 21:00:15 +05305819{
Joonas Lahtinen62106b42016-03-18 10:42:57 +02005820 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03005821 unsigned long pctx_paddr, paddr;
Deepak S38807742014-05-23 21:00:15 +05305822 u32 pcbr;
5823 int pctx_size = 32*1024;
5824
Deepak S38807742014-05-23 21:00:15 +05305825 pcbr = I915_READ(VLV_PCBR);
5826 if ((pcbr >> VLV_PCBR_ADDR_SHIFT) == 0) {
Ville Syrjäläce611ef2014-11-07 21:33:46 +02005827 DRM_DEBUG_DRIVER("BIOS didn't set up PCBR, fixing up\n");
Deepak S38807742014-05-23 21:00:15 +05305828 paddr = (dev_priv->mm.stolen_base +
Joonas Lahtinen62106b42016-03-18 10:42:57 +02005829 (ggtt->stolen_size - pctx_size));
Deepak S38807742014-05-23 21:00:15 +05305830
5831 pctx_paddr = (paddr & (~4095));
5832 I915_WRITE(VLV_PCBR, pctx_paddr);
5833 }
Ville Syrjäläce611ef2014-11-07 21:33:46 +02005834
5835 DRM_DEBUG_DRIVER("PCBR: 0x%08x\n", I915_READ(VLV_PCBR));
Deepak S38807742014-05-23 21:00:15 +05305836}
5837
Chris Wilsondc979972016-05-10 14:10:04 +01005838static void valleyview_setup_pctx(struct drm_i915_private *dev_priv)
Jesse Barnesc9cddff2013-05-08 10:45:13 -07005839{
Jesse Barnesc9cddff2013-05-08 10:45:13 -07005840 struct drm_i915_gem_object *pctx;
5841 unsigned long pctx_paddr;
5842 u32 pcbr;
5843 int pctx_size = 24*1024;
5844
5845 pcbr = I915_READ(VLV_PCBR);
5846 if (pcbr) {
5847 /* BIOS set it up already, grab the pre-alloc'd space */
5848 int pcbr_offset;
5849
5850 pcbr_offset = (pcbr & (~4095)) - dev_priv->mm.stolen_base;
Chris Wilson91c8a322016-07-05 10:40:23 +01005851 pctx = i915_gem_object_create_stolen_for_preallocated(&dev_priv->drm,
Jesse Barnesc9cddff2013-05-08 10:45:13 -07005852 pcbr_offset,
Daniel Vetter190d6cd2013-07-04 13:06:28 +02005853 I915_GTT_OFFSET_NONE,
Jesse Barnesc9cddff2013-05-08 10:45:13 -07005854 pctx_size);
5855 goto out;
5856 }
5857
Ville Syrjäläce611ef2014-11-07 21:33:46 +02005858 DRM_DEBUG_DRIVER("BIOS didn't set up PCBR, fixing up\n");
5859
Jesse Barnesc9cddff2013-05-08 10:45:13 -07005860 /*
5861 * From the Gunit register HAS:
5862 * The Gfx driver is expected to program this register and ensure
5863 * proper allocation within Gfx stolen memory. For example, this
5864 * register should be programmed such than the PCBR range does not
5865 * overlap with other ranges, such as the frame buffer, protected
5866 * memory, or any other relevant ranges.
5867 */
Chris Wilson91c8a322016-07-05 10:40:23 +01005868 pctx = i915_gem_object_create_stolen(&dev_priv->drm, pctx_size);
Jesse Barnesc9cddff2013-05-08 10:45:13 -07005869 if (!pctx) {
5870 DRM_DEBUG("not enough stolen space for PCTX, disabling\n");
Tvrtko Ursulinee504892016-02-11 10:27:30 +00005871 goto out;
Jesse Barnesc9cddff2013-05-08 10:45:13 -07005872 }
5873
5874 pctx_paddr = dev_priv->mm.stolen_base + pctx->stolen->start;
5875 I915_WRITE(VLV_PCBR, pctx_paddr);
5876
5877out:
Ville Syrjäläce611ef2014-11-07 21:33:46 +02005878 DRM_DEBUG_DRIVER("PCBR: 0x%08x\n", I915_READ(VLV_PCBR));
Jesse Barnesc9cddff2013-05-08 10:45:13 -07005879 dev_priv->vlv_pctx = pctx;
5880}
5881
Chris Wilsondc979972016-05-10 14:10:04 +01005882static void valleyview_cleanup_pctx(struct drm_i915_private *dev_priv)
Imre Deakae484342014-03-31 15:10:44 +03005883{
Imre Deakae484342014-03-31 15:10:44 +03005884 if (WARN_ON(!dev_priv->vlv_pctx))
5885 return;
5886
Chris Wilsonf0cd5182016-10-28 13:58:43 +01005887 i915_gem_object_put(dev_priv->vlv_pctx);
Imre Deakae484342014-03-31 15:10:44 +03005888 dev_priv->vlv_pctx = NULL;
5889}
5890
Ville Syrjäläc30fec62016-03-04 21:43:02 +02005891static void vlv_init_gpll_ref_freq(struct drm_i915_private *dev_priv)
5892{
5893 dev_priv->rps.gpll_ref_freq =
5894 vlv_get_cck_clock(dev_priv, "GPLL ref",
5895 CCK_GPLL_CLOCK_CONTROL,
5896 dev_priv->czclk_freq);
5897
5898 DRM_DEBUG_DRIVER("GPLL reference freq: %d kHz\n",
5899 dev_priv->rps.gpll_ref_freq);
5900}
5901
Chris Wilsondc979972016-05-10 14:10:04 +01005902static void valleyview_init_gt_powersave(struct drm_i915_private *dev_priv)
Imre Deak4e805192014-04-14 20:24:41 +03005903{
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03005904 u32 val;
Imre Deak4e805192014-04-14 20:24:41 +03005905
Chris Wilsondc979972016-05-10 14:10:04 +01005906 valleyview_setup_pctx(dev_priv);
Imre Deak4e805192014-04-14 20:24:41 +03005907
Ville Syrjäläc30fec62016-03-04 21:43:02 +02005908 vlv_init_gpll_ref_freq(dev_priv);
5909
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03005910 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
5911 switch ((val >> 6) & 3) {
5912 case 0:
5913 case 1:
5914 dev_priv->mem_freq = 800;
5915 break;
5916 case 2:
5917 dev_priv->mem_freq = 1066;
5918 break;
5919 case 3:
5920 dev_priv->mem_freq = 1333;
5921 break;
5922 }
Ville Syrjälä80b83b62014-11-10 22:55:14 +02005923 DRM_DEBUG_DRIVER("DDR speed: %d MHz\n", dev_priv->mem_freq);
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03005924
Imre Deak4e805192014-04-14 20:24:41 +03005925 dev_priv->rps.max_freq = valleyview_rps_max_freq(dev_priv);
5926 dev_priv->rps.rp0_freq = dev_priv->rps.max_freq;
5927 DRM_DEBUG_DRIVER("max GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02005928 intel_gpu_freq(dev_priv, dev_priv->rps.max_freq),
Imre Deak4e805192014-04-14 20:24:41 +03005929 dev_priv->rps.max_freq);
5930
5931 dev_priv->rps.efficient_freq = valleyview_rps_rpe_freq(dev_priv);
5932 DRM_DEBUG_DRIVER("RPe GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02005933 intel_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
Imre Deak4e805192014-04-14 20:24:41 +03005934 dev_priv->rps.efficient_freq);
5935
Deepak Sf8f2b002014-07-10 13:16:21 +05305936 dev_priv->rps.rp1_freq = valleyview_rps_guar_freq(dev_priv);
5937 DRM_DEBUG_DRIVER("RP1(Guar Freq) GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02005938 intel_gpu_freq(dev_priv, dev_priv->rps.rp1_freq),
Deepak Sf8f2b002014-07-10 13:16:21 +05305939 dev_priv->rps.rp1_freq);
5940
Imre Deak4e805192014-04-14 20:24:41 +03005941 dev_priv->rps.min_freq = valleyview_rps_min_freq(dev_priv);
5942 DRM_DEBUG_DRIVER("min GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02005943 intel_gpu_freq(dev_priv, dev_priv->rps.min_freq),
Imre Deak4e805192014-04-14 20:24:41 +03005944 dev_priv->rps.min_freq);
Imre Deak4e805192014-04-14 20:24:41 +03005945}
5946
Chris Wilsondc979972016-05-10 14:10:04 +01005947static void cherryview_init_gt_powersave(struct drm_i915_private *dev_priv)
Deepak S38807742014-05-23 21:00:15 +05305948{
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03005949 u32 val;
Deepak S2b6b3a02014-05-27 15:59:30 +05305950
Chris Wilsondc979972016-05-10 14:10:04 +01005951 cherryview_setup_pctx(dev_priv);
Deepak S2b6b3a02014-05-27 15:59:30 +05305952
Ville Syrjäläc30fec62016-03-04 21:43:02 +02005953 vlv_init_gpll_ref_freq(dev_priv);
5954
Ville Syrjäläa5805162015-05-26 20:42:30 +03005955 mutex_lock(&dev_priv->sb_lock);
Ville Syrjäläc6e8f392014-11-07 21:33:43 +02005956 val = vlv_cck_read(dev_priv, CCK_FUSE_REG);
Ville Syrjäläa5805162015-05-26 20:42:30 +03005957 mutex_unlock(&dev_priv->sb_lock);
Ville Syrjäläc6e8f392014-11-07 21:33:43 +02005958
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03005959 switch ((val >> 2) & 0x7) {
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03005960 case 3:
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03005961 dev_priv->mem_freq = 2000;
5962 break;
Ville Syrjäläbfa7df02015-09-24 23:29:18 +03005963 default:
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03005964 dev_priv->mem_freq = 1600;
5965 break;
5966 }
Ville Syrjälä80b83b62014-11-10 22:55:14 +02005967 DRM_DEBUG_DRIVER("DDR speed: %d MHz\n", dev_priv->mem_freq);
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03005968
Deepak S2b6b3a02014-05-27 15:59:30 +05305969 dev_priv->rps.max_freq = cherryview_rps_max_freq(dev_priv);
5970 dev_priv->rps.rp0_freq = dev_priv->rps.max_freq;
5971 DRM_DEBUG_DRIVER("max GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02005972 intel_gpu_freq(dev_priv, dev_priv->rps.max_freq),
Deepak S2b6b3a02014-05-27 15:59:30 +05305973 dev_priv->rps.max_freq);
5974
5975 dev_priv->rps.efficient_freq = cherryview_rps_rpe_freq(dev_priv);
5976 DRM_DEBUG_DRIVER("RPe GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02005977 intel_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
Deepak S2b6b3a02014-05-27 15:59:30 +05305978 dev_priv->rps.efficient_freq);
5979
Deepak S7707df42014-07-12 18:46:14 +05305980 dev_priv->rps.rp1_freq = cherryview_rps_guar_freq(dev_priv);
5981 DRM_DEBUG_DRIVER("RP1(Guar) GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02005982 intel_gpu_freq(dev_priv, dev_priv->rps.rp1_freq),
Deepak S7707df42014-07-12 18:46:14 +05305983 dev_priv->rps.rp1_freq);
5984
Deepak S5b7c91b2015-05-09 18:15:46 +05305985 /* PUnit validated range is only [RPe, RP0] */
5986 dev_priv->rps.min_freq = dev_priv->rps.efficient_freq;
Deepak S2b6b3a02014-05-27 15:59:30 +05305987 DRM_DEBUG_DRIVER("min GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02005988 intel_gpu_freq(dev_priv, dev_priv->rps.min_freq),
Deepak S2b6b3a02014-05-27 15:59:30 +05305989 dev_priv->rps.min_freq);
5990
Ville Syrjälä1c147622014-08-18 14:42:43 +03005991 WARN_ONCE((dev_priv->rps.max_freq |
5992 dev_priv->rps.efficient_freq |
5993 dev_priv->rps.rp1_freq |
5994 dev_priv->rps.min_freq) & 1,
5995 "Odd GPU freq values\n");
Deepak S38807742014-05-23 21:00:15 +05305996}
5997
Chris Wilsondc979972016-05-10 14:10:04 +01005998static void valleyview_cleanup_gt_powersave(struct drm_i915_private *dev_priv)
Imre Deak4e805192014-04-14 20:24:41 +03005999{
Chris Wilsondc979972016-05-10 14:10:04 +01006000 valleyview_cleanup_pctx(dev_priv);
Imre Deak4e805192014-04-14 20:24:41 +03006001}
6002
Chris Wilsondc979972016-05-10 14:10:04 +01006003static void cherryview_enable_rps(struct drm_i915_private *dev_priv)
Deepak S38807742014-05-23 21:00:15 +05306004{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00006005 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05306006 enum intel_engine_id id;
Deepak S2b6b3a02014-05-27 15:59:30 +05306007 u32 gtfifodbg, val, rc6_mode = 0, pcbr;
Deepak S38807742014-05-23 21:00:15 +05306008
6009 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
6010
Ville Syrjälä297b32e2016-04-13 21:09:30 +03006011 gtfifodbg = I915_READ(GTFIFODBG) & ~(GT_FIFO_SBDEDICATE_FREE_ENTRY_CHV |
6012 GT_FIFO_FREE_ENTRIES_CHV);
Deepak S38807742014-05-23 21:00:15 +05306013 if (gtfifodbg) {
6014 DRM_DEBUG_DRIVER("GT fifo had a previous error %x\n",
6015 gtfifodbg);
6016 I915_WRITE(GTFIFODBG, gtfifodbg);
6017 }
6018
6019 cherryview_check_pctx(dev_priv);
6020
6021 /* 1a & 1b: Get forcewake during program sequence. Although the driver
6022 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
Mika Kuoppala59bad942015-01-16 11:34:40 +02006023 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Deepak S38807742014-05-23 21:00:15 +05306024
Ville Syrjälä160614a2015-01-19 13:50:47 +02006025 /* Disable RC states. */
6026 I915_WRITE(GEN6_RC_CONTROL, 0);
6027
Deepak S38807742014-05-23 21:00:15 +05306028 /* 2a: Program RC6 thresholds.*/
6029 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16);
6030 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
6031 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
6032
Akash Goel3b3f1652016-10-13 22:44:48 +05306033 for_each_engine(engine, dev_priv, id)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00006034 I915_WRITE(RING_MAX_IDLE(engine->mmio_base), 10);
Deepak S38807742014-05-23 21:00:15 +05306035 I915_WRITE(GEN6_RC_SLEEP, 0);
6036
Deepak Sf4f71c72015-03-28 15:23:35 +05306037 /* TO threshold set to 500 us ( 0x186 * 1.28 us) */
6038 I915_WRITE(GEN6_RC6_THRESHOLD, 0x186);
Deepak S38807742014-05-23 21:00:15 +05306039
6040 /* allows RC6 residency counter to work */
6041 I915_WRITE(VLV_COUNTER_CONTROL,
6042 _MASKED_BIT_ENABLE(VLV_COUNT_RANGE_HIGH |
6043 VLV_MEDIA_RC6_COUNT_EN |
6044 VLV_RENDER_RC6_COUNT_EN));
6045
6046 /* For now we assume BIOS is allocating and populating the PCBR */
6047 pcbr = I915_READ(VLV_PCBR);
6048
Deepak S38807742014-05-23 21:00:15 +05306049 /* 3: Enable RC6 */
Chris Wilsondc979972016-05-10 14:10:04 +01006050 if ((intel_enable_rc6() & INTEL_RC6_ENABLE) &&
6051 (pcbr >> VLV_PCBR_ADDR_SHIFT))
Ville Syrjäläaf5a75a2015-01-19 13:50:50 +02006052 rc6_mode = GEN7_RC_CTL_TO_MODE;
Deepak S38807742014-05-23 21:00:15 +05306053
6054 I915_WRITE(GEN6_RC_CONTROL, rc6_mode);
6055
Deepak S2b6b3a02014-05-27 15:59:30 +05306056 /* 4 Program defaults and thresholds for RPS*/
Ville Syrjälä3cbdb482015-01-19 13:50:49 +02006057 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 1000000);
Deepak S2b6b3a02014-05-27 15:59:30 +05306058 I915_WRITE(GEN6_RP_UP_THRESHOLD, 59400);
6059 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 245000);
6060 I915_WRITE(GEN6_RP_UP_EI, 66000);
6061 I915_WRITE(GEN6_RP_DOWN_EI, 350000);
6062
6063 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
6064
6065 /* 5: Enable RPS */
6066 I915_WRITE(GEN6_RP_CONTROL,
6067 GEN6_RP_MEDIA_HW_NORMAL_MODE |
Ville Syrjäläeb973a52015-01-21 19:37:59 +02006068 GEN6_RP_MEDIA_IS_GFX |
Deepak S2b6b3a02014-05-27 15:59:30 +05306069 GEN6_RP_ENABLE |
6070 GEN6_RP_UP_BUSY_AVG |
6071 GEN6_RP_DOWN_IDLE_AVG);
6072
Deepak S3ef62342015-04-29 08:36:24 +05306073 /* Setting Fixed Bias */
6074 val = VLV_OVERRIDE_EN |
6075 VLV_SOC_TDP_EN |
6076 CHV_BIAS_CPU_50_SOC_50;
6077 vlv_punit_write(dev_priv, VLV_TURBO_SOC_OVERRIDE, val);
6078
Deepak S2b6b3a02014-05-27 15:59:30 +05306079 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
6080
Ville Syrjälä8d40c3a2014-11-07 21:33:45 +02006081 /* RPS code assumes GPLL is used */
6082 WARN_ONCE((val & GPLLENABLE) == 0, "GPLL not enabled\n");
6083
Jani Nikula742f4912015-09-03 11:16:09 +03006084 DRM_DEBUG_DRIVER("GPLL enabled? %s\n", yesno(val & GPLLENABLE));
Deepak S2b6b3a02014-05-27 15:59:30 +05306085 DRM_DEBUG_DRIVER("GPU status: 0x%08x\n", val);
6086
Chris Wilson3a45b052016-07-13 09:10:32 +01006087 reset_rps(dev_priv, valleyview_set_rps);
Deepak S2b6b3a02014-05-27 15:59:30 +05306088
Mika Kuoppala59bad942015-01-16 11:34:40 +02006089 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Deepak S38807742014-05-23 21:00:15 +05306090}
6091
Chris Wilsondc979972016-05-10 14:10:04 +01006092static void valleyview_enable_rps(struct drm_i915_private *dev_priv)
Jesse Barnes0a073b82013-04-17 15:54:58 -07006093{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00006094 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05306095 enum intel_engine_id id;
Ben Widawsky2a5913a2014-03-19 18:31:13 -07006096 u32 gtfifodbg, val, rc6_mode = 0;
Jesse Barnes0a073b82013-04-17 15:54:58 -07006097
6098 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
6099
Imre Deakae484342014-03-31 15:10:44 +03006100 valleyview_check_pctx(dev_priv);
6101
Ville Syrjälä297b32e2016-04-13 21:09:30 +03006102 gtfifodbg = I915_READ(GTFIFODBG);
6103 if (gtfifodbg) {
Jesse Barnesf7d85c12013-09-27 10:40:54 -07006104 DRM_DEBUG_DRIVER("GT fifo had a previous error %x\n",
6105 gtfifodbg);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006106 I915_WRITE(GTFIFODBG, gtfifodbg);
6107 }
6108
Deepak Sc8d9a592013-11-23 14:55:42 +05306109 /* If VLV, Forcewake all wells, else re-direct to regular path */
Mika Kuoppala59bad942015-01-16 11:34:40 +02006110 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006111
Ville Syrjälä160614a2015-01-19 13:50:47 +02006112 /* Disable RC states. */
6113 I915_WRITE(GEN6_RC_CONTROL, 0);
6114
Ville Syrjäläcad725f2015-01-19 13:50:48 +02006115 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 1000000);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006116 I915_WRITE(GEN6_RP_UP_THRESHOLD, 59400);
6117 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 245000);
6118 I915_WRITE(GEN6_RP_UP_EI, 66000);
6119 I915_WRITE(GEN6_RP_DOWN_EI, 350000);
6120
6121 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
6122
6123 I915_WRITE(GEN6_RP_CONTROL,
6124 GEN6_RP_MEDIA_TURBO |
6125 GEN6_RP_MEDIA_HW_NORMAL_MODE |
6126 GEN6_RP_MEDIA_IS_GFX |
6127 GEN6_RP_ENABLE |
6128 GEN6_RP_UP_BUSY_AVG |
6129 GEN6_RP_DOWN_IDLE_CONT);
6130
6131 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 0x00280000);
6132 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
6133 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
6134
Akash Goel3b3f1652016-10-13 22:44:48 +05306135 for_each_engine(engine, dev_priv, id)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00006136 I915_WRITE(RING_MAX_IDLE(engine->mmio_base), 10);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006137
Jesse Barnes2f0aa302013-11-15 09:32:11 -08006138 I915_WRITE(GEN6_RC6_THRESHOLD, 0x557);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006139
6140 /* allows RC6 residency counter to work */
Jesse Barnes49798eb2013-09-26 17:55:57 -07006141 I915_WRITE(VLV_COUNTER_CONTROL,
Deepak S31685c22014-07-03 17:33:01 -04006142 _MASKED_BIT_ENABLE(VLV_MEDIA_RC0_COUNT_EN |
6143 VLV_RENDER_RC0_COUNT_EN |
Jesse Barnes49798eb2013-09-26 17:55:57 -07006144 VLV_MEDIA_RC6_COUNT_EN |
6145 VLV_RENDER_RC6_COUNT_EN));
Deepak S31685c22014-07-03 17:33:01 -04006146
Chris Wilsondc979972016-05-10 14:10:04 +01006147 if (intel_enable_rc6() & INTEL_RC6_ENABLE)
Jesse Barnes6b88f292013-11-15 09:32:12 -08006148 rc6_mode = GEN7_RC_CTL_TO_MODE | VLV_RC_CTL_CTX_RST_PARALLEL;
Ben Widawskydc39fff2013-10-18 12:32:07 -07006149
Chris Wilsondc979972016-05-10 14:10:04 +01006150 intel_print_rc6_info(dev_priv, rc6_mode);
Ben Widawskydc39fff2013-10-18 12:32:07 -07006151
Jesse Barnesa2b23fe2013-09-19 09:33:13 -07006152 I915_WRITE(GEN6_RC_CONTROL, rc6_mode);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006153
Deepak S3ef62342015-04-29 08:36:24 +05306154 /* Setting Fixed Bias */
6155 val = VLV_OVERRIDE_EN |
6156 VLV_SOC_TDP_EN |
6157 VLV_BIAS_CPU_125_SOC_875;
6158 vlv_punit_write(dev_priv, VLV_TURBO_SOC_OVERRIDE, val);
6159
Jani Nikula64936252013-05-22 15:36:20 +03006160 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006161
Ville Syrjälä8d40c3a2014-11-07 21:33:45 +02006162 /* RPS code assumes GPLL is used */
6163 WARN_ONCE((val & GPLLENABLE) == 0, "GPLL not enabled\n");
6164
Jani Nikula742f4912015-09-03 11:16:09 +03006165 DRM_DEBUG_DRIVER("GPLL enabled? %s\n", yesno(val & GPLLENABLE));
Jesse Barnes0a073b82013-04-17 15:54:58 -07006166 DRM_DEBUG_DRIVER("GPU status: 0x%08x\n", val);
6167
Chris Wilson3a45b052016-07-13 09:10:32 +01006168 reset_rps(dev_priv, valleyview_set_rps);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006169
Mika Kuoppala59bad942015-01-16 11:34:40 +02006170 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006171}
6172
Eugeni Dodonovdde18882012-04-18 15:29:24 -03006173static unsigned long intel_pxfreq(u32 vidfreq)
6174{
6175 unsigned long freq;
6176 int div = (vidfreq & 0x3f0000) >> 16;
6177 int post = (vidfreq & 0x3000) >> 12;
6178 int pre = (vidfreq & 0x7);
6179
6180 if (!pre)
6181 return 0;
6182
6183 freq = ((div * 133333) / ((1<<post) * pre));
6184
6185 return freq;
6186}
6187
Daniel Vettereb48eb02012-04-26 23:28:12 +02006188static const struct cparams {
6189 u16 i;
6190 u16 t;
6191 u16 m;
6192 u16 c;
6193} cparams[] = {
6194 { 1, 1333, 301, 28664 },
6195 { 1, 1066, 294, 24460 },
6196 { 1, 800, 294, 25192 },
6197 { 0, 1333, 276, 27605 },
6198 { 0, 1066, 276, 27605 },
6199 { 0, 800, 231, 23784 },
6200};
6201
Chris Wilsonf531dcb2012-09-25 10:16:12 +01006202static unsigned long __i915_chipset_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02006203{
6204 u64 total_count, diff, ret;
6205 u32 count1, count2, count3, m = 0, c = 0;
6206 unsigned long now = jiffies_to_msecs(jiffies), diff1;
6207 int i;
6208
Daniel Vetter02d71952012-08-09 16:44:54 +02006209 assert_spin_locked(&mchdev_lock);
6210
Daniel Vetter20e4d402012-08-08 23:35:39 +02006211 diff1 = now - dev_priv->ips.last_time1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006212
6213 /* Prevent division-by-zero if we are asking too fast.
6214 * Also, we don't get interesting results if we are polling
6215 * faster than once in 10ms, so just return the saved value
6216 * in such cases.
6217 */
6218 if (diff1 <= 10)
Daniel Vetter20e4d402012-08-08 23:35:39 +02006219 return dev_priv->ips.chipset_power;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006220
6221 count1 = I915_READ(DMIEC);
6222 count2 = I915_READ(DDREC);
6223 count3 = I915_READ(CSIEC);
6224
6225 total_count = count1 + count2 + count3;
6226
6227 /* FIXME: handle per-counter overflow */
Daniel Vetter20e4d402012-08-08 23:35:39 +02006228 if (total_count < dev_priv->ips.last_count1) {
6229 diff = ~0UL - dev_priv->ips.last_count1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006230 diff += total_count;
6231 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +02006232 diff = total_count - dev_priv->ips.last_count1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006233 }
6234
6235 for (i = 0; i < ARRAY_SIZE(cparams); i++) {
Daniel Vetter20e4d402012-08-08 23:35:39 +02006236 if (cparams[i].i == dev_priv->ips.c_m &&
6237 cparams[i].t == dev_priv->ips.r_t) {
Daniel Vettereb48eb02012-04-26 23:28:12 +02006238 m = cparams[i].m;
6239 c = cparams[i].c;
6240 break;
6241 }
6242 }
6243
6244 diff = div_u64(diff, diff1);
6245 ret = ((m * diff) + c);
6246 ret = div_u64(ret, 10);
6247
Daniel Vetter20e4d402012-08-08 23:35:39 +02006248 dev_priv->ips.last_count1 = total_count;
6249 dev_priv->ips.last_time1 = now;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006250
Daniel Vetter20e4d402012-08-08 23:35:39 +02006251 dev_priv->ips.chipset_power = ret;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006252
6253 return ret;
6254}
6255
Chris Wilsonf531dcb2012-09-25 10:16:12 +01006256unsigned long i915_chipset_val(struct drm_i915_private *dev_priv)
6257{
6258 unsigned long val;
6259
Chris Wilsondc979972016-05-10 14:10:04 +01006260 if (INTEL_INFO(dev_priv)->gen != 5)
Chris Wilsonf531dcb2012-09-25 10:16:12 +01006261 return 0;
6262
6263 spin_lock_irq(&mchdev_lock);
6264
6265 val = __i915_chipset_val(dev_priv);
6266
6267 spin_unlock_irq(&mchdev_lock);
6268
6269 return val;
6270}
6271
Daniel Vettereb48eb02012-04-26 23:28:12 +02006272unsigned long i915_mch_val(struct drm_i915_private *dev_priv)
6273{
6274 unsigned long m, x, b;
6275 u32 tsfs;
6276
6277 tsfs = I915_READ(TSFS);
6278
6279 m = ((tsfs & TSFS_SLOPE_MASK) >> TSFS_SLOPE_SHIFT);
6280 x = I915_READ8(TR1);
6281
6282 b = tsfs & TSFS_INTR_MASK;
6283
6284 return ((m * x) / 127) - b;
6285}
6286
Mika Kuoppalad972d6e2014-12-01 18:01:05 +02006287static int _pxvid_to_vd(u8 pxvid)
6288{
6289 if (pxvid == 0)
6290 return 0;
6291
6292 if (pxvid >= 8 && pxvid < 31)
6293 pxvid = 31;
6294
6295 return (pxvid + 2) * 125;
6296}
6297
6298static u32 pvid_to_extvid(struct drm_i915_private *dev_priv, u8 pxvid)
Daniel Vettereb48eb02012-04-26 23:28:12 +02006299{
Mika Kuoppalad972d6e2014-12-01 18:01:05 +02006300 const int vd = _pxvid_to_vd(pxvid);
6301 const int vm = vd - 1125;
6302
Chris Wilsondc979972016-05-10 14:10:04 +01006303 if (INTEL_INFO(dev_priv)->is_mobile)
Mika Kuoppalad972d6e2014-12-01 18:01:05 +02006304 return vm > 0 ? vm : 0;
6305
6306 return vd;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006307}
6308
Daniel Vetter02d71952012-08-09 16:44:54 +02006309static void __i915_update_gfx_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02006310{
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00006311 u64 now, diff, diffms;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006312 u32 count;
6313
Daniel Vetter02d71952012-08-09 16:44:54 +02006314 assert_spin_locked(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006315
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00006316 now = ktime_get_raw_ns();
6317 diffms = now - dev_priv->ips.last_time2;
6318 do_div(diffms, NSEC_PER_MSEC);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006319
6320 /* Don't divide by 0 */
Daniel Vettereb48eb02012-04-26 23:28:12 +02006321 if (!diffms)
6322 return;
6323
6324 count = I915_READ(GFXEC);
6325
Daniel Vetter20e4d402012-08-08 23:35:39 +02006326 if (count < dev_priv->ips.last_count2) {
6327 diff = ~0UL - dev_priv->ips.last_count2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006328 diff += count;
6329 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +02006330 diff = count - dev_priv->ips.last_count2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006331 }
6332
Daniel Vetter20e4d402012-08-08 23:35:39 +02006333 dev_priv->ips.last_count2 = count;
6334 dev_priv->ips.last_time2 = now;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006335
6336 /* More magic constants... */
6337 diff = diff * 1181;
6338 diff = div_u64(diff, diffms * 10);
Daniel Vetter20e4d402012-08-08 23:35:39 +02006339 dev_priv->ips.gfx_power = diff;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006340}
6341
Daniel Vetter02d71952012-08-09 16:44:54 +02006342void i915_update_gfx_val(struct drm_i915_private *dev_priv)
6343{
Chris Wilsondc979972016-05-10 14:10:04 +01006344 if (INTEL_INFO(dev_priv)->gen != 5)
Daniel Vetter02d71952012-08-09 16:44:54 +02006345 return;
6346
Daniel Vetter92703882012-08-09 16:46:01 +02006347 spin_lock_irq(&mchdev_lock);
Daniel Vetter02d71952012-08-09 16:44:54 +02006348
6349 __i915_update_gfx_val(dev_priv);
6350
Daniel Vetter92703882012-08-09 16:46:01 +02006351 spin_unlock_irq(&mchdev_lock);
Daniel Vetter02d71952012-08-09 16:44:54 +02006352}
6353
Chris Wilsonf531dcb2012-09-25 10:16:12 +01006354static unsigned long __i915_gfx_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02006355{
6356 unsigned long t, corr, state1, corr2, state2;
6357 u32 pxvid, ext_v;
6358
Daniel Vetter02d71952012-08-09 16:44:54 +02006359 assert_spin_locked(&mchdev_lock);
6360
Ville Syrjälä616847e2015-09-18 20:03:19 +03006361 pxvid = I915_READ(PXVFREQ(dev_priv->rps.cur_freq));
Daniel Vettereb48eb02012-04-26 23:28:12 +02006362 pxvid = (pxvid >> 24) & 0x7f;
6363 ext_v = pvid_to_extvid(dev_priv, pxvid);
6364
6365 state1 = ext_v;
6366
6367 t = i915_mch_val(dev_priv);
6368
6369 /* Revel in the empirically derived constants */
6370
6371 /* Correction factor in 1/100000 units */
6372 if (t > 80)
6373 corr = ((t * 2349) + 135940);
6374 else if (t >= 50)
6375 corr = ((t * 964) + 29317);
6376 else /* < 50 */
6377 corr = ((t * 301) + 1004);
6378
6379 corr = corr * ((150142 * state1) / 10000 - 78642);
6380 corr /= 100000;
Daniel Vetter20e4d402012-08-08 23:35:39 +02006381 corr2 = (corr * dev_priv->ips.corr);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006382
6383 state2 = (corr2 * state1) / 10000;
6384 state2 /= 100; /* convert to mW */
6385
Daniel Vetter02d71952012-08-09 16:44:54 +02006386 __i915_update_gfx_val(dev_priv);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006387
Daniel Vetter20e4d402012-08-08 23:35:39 +02006388 return dev_priv->ips.gfx_power + state2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006389}
6390
Chris Wilsonf531dcb2012-09-25 10:16:12 +01006391unsigned long i915_gfx_val(struct drm_i915_private *dev_priv)
6392{
6393 unsigned long val;
6394
Chris Wilsondc979972016-05-10 14:10:04 +01006395 if (INTEL_INFO(dev_priv)->gen != 5)
Chris Wilsonf531dcb2012-09-25 10:16:12 +01006396 return 0;
6397
6398 spin_lock_irq(&mchdev_lock);
6399
6400 val = __i915_gfx_val(dev_priv);
6401
6402 spin_unlock_irq(&mchdev_lock);
6403
6404 return val;
6405}
6406
Daniel Vettereb48eb02012-04-26 23:28:12 +02006407/**
6408 * i915_read_mch_val - return value for IPS use
6409 *
6410 * Calculate and return a value for the IPS driver to use when deciding whether
6411 * we have thermal and power headroom to increase CPU or GPU power budget.
6412 */
6413unsigned long i915_read_mch_val(void)
6414{
6415 struct drm_i915_private *dev_priv;
6416 unsigned long chipset_val, graphics_val, ret = 0;
6417
Daniel Vetter92703882012-08-09 16:46:01 +02006418 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006419 if (!i915_mch_dev)
6420 goto out_unlock;
6421 dev_priv = i915_mch_dev;
6422
Chris Wilsonf531dcb2012-09-25 10:16:12 +01006423 chipset_val = __i915_chipset_val(dev_priv);
6424 graphics_val = __i915_gfx_val(dev_priv);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006425
6426 ret = chipset_val + graphics_val;
6427
6428out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02006429 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006430
6431 return ret;
6432}
6433EXPORT_SYMBOL_GPL(i915_read_mch_val);
6434
6435/**
6436 * i915_gpu_raise - raise GPU frequency limit
6437 *
6438 * Raise the limit; IPS indicates we have thermal headroom.
6439 */
6440bool i915_gpu_raise(void)
6441{
6442 struct drm_i915_private *dev_priv;
6443 bool ret = true;
6444
Daniel Vetter92703882012-08-09 16:46:01 +02006445 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006446 if (!i915_mch_dev) {
6447 ret = false;
6448 goto out_unlock;
6449 }
6450 dev_priv = i915_mch_dev;
6451
Daniel Vetter20e4d402012-08-08 23:35:39 +02006452 if (dev_priv->ips.max_delay > dev_priv->ips.fmax)
6453 dev_priv->ips.max_delay--;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006454
6455out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02006456 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006457
6458 return ret;
6459}
6460EXPORT_SYMBOL_GPL(i915_gpu_raise);
6461
6462/**
6463 * i915_gpu_lower - lower GPU frequency limit
6464 *
6465 * IPS indicates we're close to a thermal limit, so throttle back the GPU
6466 * frequency maximum.
6467 */
6468bool i915_gpu_lower(void)
6469{
6470 struct drm_i915_private *dev_priv;
6471 bool ret = true;
6472
Daniel Vetter92703882012-08-09 16:46:01 +02006473 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006474 if (!i915_mch_dev) {
6475 ret = false;
6476 goto out_unlock;
6477 }
6478 dev_priv = i915_mch_dev;
6479
Daniel Vetter20e4d402012-08-08 23:35:39 +02006480 if (dev_priv->ips.max_delay < dev_priv->ips.min_delay)
6481 dev_priv->ips.max_delay++;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006482
6483out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02006484 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006485
6486 return ret;
6487}
6488EXPORT_SYMBOL_GPL(i915_gpu_lower);
6489
6490/**
6491 * i915_gpu_busy - indicate GPU business to IPS
6492 *
6493 * Tell the IPS driver whether or not the GPU is busy.
6494 */
6495bool i915_gpu_busy(void)
6496{
Daniel Vettereb48eb02012-04-26 23:28:12 +02006497 bool ret = false;
6498
Daniel Vetter92703882012-08-09 16:46:01 +02006499 spin_lock_irq(&mchdev_lock);
Chris Wilsondcff85c2016-08-05 10:14:11 +01006500 if (i915_mch_dev)
6501 ret = i915_mch_dev->gt.awake;
Daniel Vetter92703882012-08-09 16:46:01 +02006502 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006503
6504 return ret;
6505}
6506EXPORT_SYMBOL_GPL(i915_gpu_busy);
6507
6508/**
6509 * i915_gpu_turbo_disable - disable graphics turbo
6510 *
6511 * Disable graphics turbo by resetting the max frequency and setting the
6512 * current frequency to the default.
6513 */
6514bool i915_gpu_turbo_disable(void)
6515{
6516 struct drm_i915_private *dev_priv;
6517 bool ret = true;
6518
Daniel Vetter92703882012-08-09 16:46:01 +02006519 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006520 if (!i915_mch_dev) {
6521 ret = false;
6522 goto out_unlock;
6523 }
6524 dev_priv = i915_mch_dev;
6525
Daniel Vetter20e4d402012-08-08 23:35:39 +02006526 dev_priv->ips.max_delay = dev_priv->ips.fstart;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006527
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01006528 if (!ironlake_set_drps(dev_priv, dev_priv->ips.fstart))
Daniel Vettereb48eb02012-04-26 23:28:12 +02006529 ret = false;
6530
6531out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02006532 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006533
6534 return ret;
6535}
6536EXPORT_SYMBOL_GPL(i915_gpu_turbo_disable);
6537
6538/**
6539 * Tells the intel_ips driver that the i915 driver is now loaded, if
6540 * IPS got loaded first.
6541 *
6542 * This awkward dance is so that neither module has to depend on the
6543 * other in order for IPS to do the appropriate communication of
6544 * GPU turbo limits to i915.
6545 */
6546static void
6547ips_ping_for_i915_load(void)
6548{
6549 void (*link)(void);
6550
6551 link = symbol_get(ips_link_to_i915_driver);
6552 if (link) {
6553 link();
6554 symbol_put(ips_link_to_i915_driver);
6555 }
6556}
6557
6558void intel_gpu_ips_init(struct drm_i915_private *dev_priv)
6559{
Daniel Vetter02d71952012-08-09 16:44:54 +02006560 /* We only register the i915 ips part with intel-ips once everything is
6561 * set up, to avoid intel-ips sneaking in and reading bogus values. */
Daniel Vetter92703882012-08-09 16:46:01 +02006562 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006563 i915_mch_dev = dev_priv;
Daniel Vetter92703882012-08-09 16:46:01 +02006564 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006565
6566 ips_ping_for_i915_load();
6567}
6568
6569void intel_gpu_ips_teardown(void)
6570{
Daniel Vetter92703882012-08-09 16:46:01 +02006571 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006572 i915_mch_dev = NULL;
Daniel Vetter92703882012-08-09 16:46:01 +02006573 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006574}
Deepak S76c3552f2014-01-30 23:08:16 +05306575
Chris Wilsondc979972016-05-10 14:10:04 +01006576static void intel_init_emon(struct drm_i915_private *dev_priv)
Eugeni Dodonovdde18882012-04-18 15:29:24 -03006577{
Eugeni Dodonovdde18882012-04-18 15:29:24 -03006578 u32 lcfuse;
6579 u8 pxw[16];
6580 int i;
6581
6582 /* Disable to program */
6583 I915_WRITE(ECR, 0);
6584 POSTING_READ(ECR);
6585
6586 /* Program energy weights for various events */
6587 I915_WRITE(SDEW, 0x15040d00);
6588 I915_WRITE(CSIEW0, 0x007f0000);
6589 I915_WRITE(CSIEW1, 0x1e220004);
6590 I915_WRITE(CSIEW2, 0x04000004);
6591
6592 for (i = 0; i < 5; i++)
Ville Syrjälä616847e2015-09-18 20:03:19 +03006593 I915_WRITE(PEW(i), 0);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03006594 for (i = 0; i < 3; i++)
Ville Syrjälä616847e2015-09-18 20:03:19 +03006595 I915_WRITE(DEW(i), 0);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03006596
6597 /* Program P-state weights to account for frequency power adjustment */
6598 for (i = 0; i < 16; i++) {
Ville Syrjälä616847e2015-09-18 20:03:19 +03006599 u32 pxvidfreq = I915_READ(PXVFREQ(i));
Eugeni Dodonovdde18882012-04-18 15:29:24 -03006600 unsigned long freq = intel_pxfreq(pxvidfreq);
6601 unsigned long vid = (pxvidfreq & PXVFREQ_PX_MASK) >>
6602 PXVFREQ_PX_SHIFT;
6603 unsigned long val;
6604
6605 val = vid * vid;
6606 val *= (freq / 1000);
6607 val *= 255;
6608 val /= (127*127*900);
6609 if (val > 0xff)
6610 DRM_ERROR("bad pxval: %ld\n", val);
6611 pxw[i] = val;
6612 }
6613 /* Render standby states get 0 weight */
6614 pxw[14] = 0;
6615 pxw[15] = 0;
6616
6617 for (i = 0; i < 4; i++) {
6618 u32 val = (pxw[i*4] << 24) | (pxw[(i*4)+1] << 16) |
6619 (pxw[(i*4)+2] << 8) | (pxw[(i*4)+3]);
Ville Syrjälä616847e2015-09-18 20:03:19 +03006620 I915_WRITE(PXW(i), val);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03006621 }
6622
6623 /* Adjust magic regs to magic values (more experimental results) */
6624 I915_WRITE(OGW0, 0);
6625 I915_WRITE(OGW1, 0);
6626 I915_WRITE(EG0, 0x00007f00);
6627 I915_WRITE(EG1, 0x0000000e);
6628 I915_WRITE(EG2, 0x000e0000);
6629 I915_WRITE(EG3, 0x68000300);
6630 I915_WRITE(EG4, 0x42000000);
6631 I915_WRITE(EG5, 0x00140031);
6632 I915_WRITE(EG6, 0);
6633 I915_WRITE(EG7, 0);
6634
6635 for (i = 0; i < 8; i++)
Ville Syrjälä616847e2015-09-18 20:03:19 +03006636 I915_WRITE(PXWL(i), 0);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03006637
6638 /* Enable PMON + select events */
6639 I915_WRITE(ECR, 0x80000019);
6640
6641 lcfuse = I915_READ(LCFUSE02);
6642
Daniel Vetter20e4d402012-08-08 23:35:39 +02006643 dev_priv->ips.corr = (lcfuse & LCFUSE_HIV_MASK);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03006644}
6645
Chris Wilsondc979972016-05-10 14:10:04 +01006646void intel_init_gt_powersave(struct drm_i915_private *dev_priv)
Imre Deakae484342014-03-31 15:10:44 +03006647{
Imre Deakb268c692015-12-15 20:10:31 +02006648 /*
6649 * RPM depends on RC6 to save restore the GT HW context, so make RC6 a
6650 * requirement.
6651 */
6652 if (!i915.enable_rc6) {
6653 DRM_INFO("RC6 disabled, disabling runtime PM support\n");
6654 intel_runtime_pm_get(dev_priv);
6655 }
Imre Deake6069ca2014-04-18 16:01:02 +03006656
Chris Wilsonb5163db2016-08-10 13:58:24 +01006657 mutex_lock(&dev_priv->drm.struct_mutex);
Chris Wilson773ea9a2016-07-13 09:10:33 +01006658 mutex_lock(&dev_priv->rps.hw_lock);
6659
6660 /* Initialize RPS limits (for userspace) */
Chris Wilsondc979972016-05-10 14:10:04 +01006661 if (IS_CHERRYVIEW(dev_priv))
6662 cherryview_init_gt_powersave(dev_priv);
6663 else if (IS_VALLEYVIEW(dev_priv))
6664 valleyview_init_gt_powersave(dev_priv);
Chris Wilson2a13ae72016-08-02 11:15:27 +01006665 else if (INTEL_GEN(dev_priv) >= 6)
Chris Wilson773ea9a2016-07-13 09:10:33 +01006666 gen6_init_rps_frequencies(dev_priv);
6667
6668 /* Derive initial user preferences/limits from the hardware limits */
6669 dev_priv->rps.idle_freq = dev_priv->rps.min_freq;
6670 dev_priv->rps.cur_freq = dev_priv->rps.idle_freq;
6671
6672 dev_priv->rps.max_freq_softlimit = dev_priv->rps.max_freq;
6673 dev_priv->rps.min_freq_softlimit = dev_priv->rps.min_freq;
6674
6675 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
6676 dev_priv->rps.min_freq_softlimit =
6677 max_t(int,
6678 dev_priv->rps.efficient_freq,
6679 intel_freq_opcode(dev_priv, 450));
6680
Chris Wilson99ac9612016-07-13 09:10:34 +01006681 /* After setting max-softlimit, find the overclock max freq */
6682 if (IS_GEN6(dev_priv) ||
6683 IS_IVYBRIDGE(dev_priv) || IS_HASWELL(dev_priv)) {
6684 u32 params = 0;
6685
6686 sandybridge_pcode_read(dev_priv, GEN6_READ_OC_PARAMS, &params);
6687 if (params & BIT(31)) { /* OC supported */
6688 DRM_DEBUG_DRIVER("Overclocking supported, max: %dMHz, overclock: %dMHz\n",
6689 (dev_priv->rps.max_freq & 0xff) * 50,
6690 (params & 0xff) * 50);
6691 dev_priv->rps.max_freq = params & 0xff;
6692 }
6693 }
6694
Chris Wilson29ecd78d2016-07-13 09:10:35 +01006695 /* Finally allow us to boost to max by default */
6696 dev_priv->rps.boost_freq = dev_priv->rps.max_freq;
6697
Chris Wilson773ea9a2016-07-13 09:10:33 +01006698 mutex_unlock(&dev_priv->rps.hw_lock);
Chris Wilsonb5163db2016-08-10 13:58:24 +01006699 mutex_unlock(&dev_priv->drm.struct_mutex);
Chris Wilson54b4f682016-07-21 21:16:19 +01006700
6701 intel_autoenable_gt_powersave(dev_priv);
Imre Deakae484342014-03-31 15:10:44 +03006702}
6703
Chris Wilsondc979972016-05-10 14:10:04 +01006704void intel_cleanup_gt_powersave(struct drm_i915_private *dev_priv)
Imre Deakae484342014-03-31 15:10:44 +03006705{
Ville Syrjälä8dac1e12016-08-02 14:07:33 +03006706 if (IS_VALLEYVIEW(dev_priv))
Chris Wilsondc979972016-05-10 14:10:04 +01006707 valleyview_cleanup_gt_powersave(dev_priv);
Imre Deakb268c692015-12-15 20:10:31 +02006708
6709 if (!i915.enable_rc6)
6710 intel_runtime_pm_put(dev_priv);
Imre Deakae484342014-03-31 15:10:44 +03006711}
6712
Chris Wilson54b4f682016-07-21 21:16:19 +01006713/**
6714 * intel_suspend_gt_powersave - suspend PM work and helper threads
6715 * @dev_priv: i915 device
6716 *
6717 * We don't want to disable RC6 or other features here, we just want
6718 * to make sure any work we've queued has finished and won't bother
6719 * us while we're suspended.
6720 */
6721void intel_suspend_gt_powersave(struct drm_i915_private *dev_priv)
6722{
6723 if (INTEL_GEN(dev_priv) < 6)
6724 return;
6725
6726 if (cancel_delayed_work_sync(&dev_priv->rps.autoenable_work))
6727 intel_runtime_pm_put(dev_priv);
6728
6729 /* gen6_rps_idle() will be called later to disable interrupts */
6730}
6731
Chris Wilsonb7137e02016-07-13 09:10:37 +01006732void intel_sanitize_gt_powersave(struct drm_i915_private *dev_priv)
6733{
6734 dev_priv->rps.enabled = true; /* force disabling */
6735 intel_disable_gt_powersave(dev_priv);
Chris Wilson54b4f682016-07-21 21:16:19 +01006736
6737 gen6_reset_rps_interrupts(dev_priv);
Jesse Barnes156c7ca2014-06-12 08:35:45 -07006738}
6739
Chris Wilsondc979972016-05-10 14:10:04 +01006740void intel_disable_gt_powersave(struct drm_i915_private *dev_priv)
Daniel Vetter8090c6b2012-06-24 16:42:32 +02006741{
Chris Wilsonb7137e02016-07-13 09:10:37 +01006742 if (!READ_ONCE(dev_priv->rps.enabled))
6743 return;
Jesse Barnes1a01ab32012-11-02 11:14:00 -07006744
Jesse Barnes4fc688c2012-11-02 11:14:01 -07006745 mutex_lock(&dev_priv->rps.hw_lock);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006746
Chris Wilsonb7137e02016-07-13 09:10:37 +01006747 if (INTEL_GEN(dev_priv) >= 9) {
6748 gen9_disable_rc6(dev_priv);
6749 gen9_disable_rps(dev_priv);
6750 } else if (IS_CHERRYVIEW(dev_priv)) {
6751 cherryview_disable_rps(dev_priv);
6752 } else if (IS_VALLEYVIEW(dev_priv)) {
6753 valleyview_disable_rps(dev_priv);
6754 } else if (INTEL_GEN(dev_priv) >= 6) {
6755 gen6_disable_rps(dev_priv);
6756 } else if (IS_IRONLAKE_M(dev_priv)) {
6757 ironlake_disable_drps(dev_priv);
6758 }
6759
6760 dev_priv->rps.enabled = false;
6761 mutex_unlock(&dev_priv->rps.hw_lock);
6762}
6763
6764void intel_enable_gt_powersave(struct drm_i915_private *dev_priv)
6765{
Chris Wilson54b4f682016-07-21 21:16:19 +01006766 /* We shouldn't be disabling as we submit, so this should be less
6767 * racy than it appears!
6768 */
Chris Wilsonb7137e02016-07-13 09:10:37 +01006769 if (READ_ONCE(dev_priv->rps.enabled))
6770 return;
6771
6772 /* Powersaving is controlled by the host when inside a VM */
6773 if (intel_vgpu_active(dev_priv))
6774 return;
6775
6776 mutex_lock(&dev_priv->rps.hw_lock);
Imre Deak3cc134e2014-11-19 15:30:03 +02006777
Chris Wilsondc979972016-05-10 14:10:04 +01006778 if (IS_CHERRYVIEW(dev_priv)) {
6779 cherryview_enable_rps(dev_priv);
6780 } else if (IS_VALLEYVIEW(dev_priv)) {
6781 valleyview_enable_rps(dev_priv);
Chris Wilsonb7137e02016-07-13 09:10:37 +01006782 } else if (INTEL_GEN(dev_priv) >= 9) {
Chris Wilsondc979972016-05-10 14:10:04 +01006783 gen9_enable_rc6(dev_priv);
6784 gen9_enable_rps(dev_priv);
6785 if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv))
Chris Wilsonfb7404e2016-07-13 09:10:38 +01006786 gen6_update_ring_freq(dev_priv);
Chris Wilsondc979972016-05-10 14:10:04 +01006787 } else if (IS_BROADWELL(dev_priv)) {
6788 gen8_enable_rps(dev_priv);
Chris Wilsonfb7404e2016-07-13 09:10:38 +01006789 gen6_update_ring_freq(dev_priv);
Chris Wilsonb7137e02016-07-13 09:10:37 +01006790 } else if (INTEL_GEN(dev_priv) >= 6) {
Chris Wilsondc979972016-05-10 14:10:04 +01006791 gen6_enable_rps(dev_priv);
Chris Wilsonfb7404e2016-07-13 09:10:38 +01006792 gen6_update_ring_freq(dev_priv);
Chris Wilsonb7137e02016-07-13 09:10:37 +01006793 } else if (IS_IRONLAKE_M(dev_priv)) {
6794 ironlake_enable_drps(dev_priv);
6795 intel_init_emon(dev_priv);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006796 }
Chris Wilsonaed242f2015-03-18 09:48:21 +00006797
6798 WARN_ON(dev_priv->rps.max_freq < dev_priv->rps.min_freq);
6799 WARN_ON(dev_priv->rps.idle_freq > dev_priv->rps.max_freq);
6800
6801 WARN_ON(dev_priv->rps.efficient_freq < dev_priv->rps.min_freq);
6802 WARN_ON(dev_priv->rps.efficient_freq > dev_priv->rps.max_freq);
6803
Chris Wilson54b4f682016-07-21 21:16:19 +01006804 dev_priv->rps.enabled = true;
Jesse Barnes4fc688c2012-11-02 11:14:01 -07006805 mutex_unlock(&dev_priv->rps.hw_lock);
Chris Wilsonb7137e02016-07-13 09:10:37 +01006806}
Imre Deakc6df39b2014-04-14 20:24:29 +03006807
Chris Wilson54b4f682016-07-21 21:16:19 +01006808static void __intel_autoenable_gt_powersave(struct work_struct *work)
6809{
6810 struct drm_i915_private *dev_priv =
6811 container_of(work, typeof(*dev_priv), rps.autoenable_work.work);
6812 struct intel_engine_cs *rcs;
6813 struct drm_i915_gem_request *req;
6814
6815 if (READ_ONCE(dev_priv->rps.enabled))
6816 goto out;
6817
Akash Goel3b3f1652016-10-13 22:44:48 +05306818 rcs = dev_priv->engine[RCS];
Chris Wilson54b4f682016-07-21 21:16:19 +01006819 if (rcs->last_context)
6820 goto out;
6821
6822 if (!rcs->init_context)
6823 goto out;
6824
6825 mutex_lock(&dev_priv->drm.struct_mutex);
6826
6827 req = i915_gem_request_alloc(rcs, dev_priv->kernel_context);
6828 if (IS_ERR(req))
6829 goto unlock;
6830
6831 if (!i915.enable_execlists && i915_switch_context(req) == 0)
6832 rcs->init_context(req);
6833
6834 /* Mark the device busy, calling intel_enable_gt_powersave() */
6835 i915_add_request_no_flush(req);
6836
6837unlock:
6838 mutex_unlock(&dev_priv->drm.struct_mutex);
6839out:
6840 intel_runtime_pm_put(dev_priv);
6841}
6842
6843void intel_autoenable_gt_powersave(struct drm_i915_private *dev_priv)
6844{
6845 if (READ_ONCE(dev_priv->rps.enabled))
6846 return;
6847
6848 if (IS_IRONLAKE_M(dev_priv)) {
6849 ironlake_enable_drps(dev_priv);
Chris Wilson54b4f682016-07-21 21:16:19 +01006850 intel_init_emon(dev_priv);
Chris Wilson54b4f682016-07-21 21:16:19 +01006851 } else if (INTEL_INFO(dev_priv)->gen >= 6) {
6852 /*
6853 * PCU communication is slow and this doesn't need to be
6854 * done at any specific time, so do this out of our fast path
6855 * to make resume and init faster.
6856 *
6857 * We depend on the HW RC6 power context save/restore
6858 * mechanism when entering D3 through runtime PM suspend. So
6859 * disable RPM until RPS/RC6 is properly setup. We can only
6860 * get here via the driver load/system resume/runtime resume
6861 * paths, so the _noresume version is enough (and in case of
6862 * runtime resume it's necessary).
6863 */
6864 if (queue_delayed_work(dev_priv->wq,
6865 &dev_priv->rps.autoenable_work,
6866 round_jiffies_up_relative(HZ)))
6867 intel_runtime_pm_get_noresume(dev_priv);
6868 }
6869}
6870
Daniel Vetter3107bd42012-10-31 22:52:31 +01006871static void ibx_init_clock_gating(struct drm_device *dev)
6872{
Chris Wilsonfac5e232016-07-04 11:34:36 +01006873 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter3107bd42012-10-31 22:52:31 +01006874
6875 /*
6876 * On Ibex Peak and Cougar Point, we need to disable clock
6877 * gating for the panel power sequencer or it will fail to
6878 * start up when no ports are active.
6879 */
6880 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE);
6881}
6882
Ville Syrjälä0e088b82013-06-07 10:47:04 +03006883static void g4x_disable_trickle_feed(struct drm_device *dev)
6884{
Chris Wilsonfac5e232016-07-04 11:34:36 +01006885 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläb12ce1d2015-05-26 20:27:23 +03006886 enum pipe pipe;
Ville Syrjälä0e088b82013-06-07 10:47:04 +03006887
Damien Lespiau055e3932014-08-18 13:49:10 +01006888 for_each_pipe(dev_priv, pipe) {
Ville Syrjälä0e088b82013-06-07 10:47:04 +03006889 I915_WRITE(DSPCNTR(pipe),
6890 I915_READ(DSPCNTR(pipe)) |
6891 DISPPLANE_TRICKLE_FEED_DISABLE);
Ville Syrjäläb12ce1d2015-05-26 20:27:23 +03006892
6893 I915_WRITE(DSPSURF(pipe), I915_READ(DSPSURF(pipe)));
6894 POSTING_READ(DSPSURF(pipe));
Ville Syrjälä0e088b82013-06-07 10:47:04 +03006895 }
6896}
6897
Ville Syrjälä017636c2013-12-05 15:51:37 +02006898static void ilk_init_lp_watermarks(struct drm_device *dev)
6899{
Chris Wilsonfac5e232016-07-04 11:34:36 +01006900 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä017636c2013-12-05 15:51:37 +02006901
6902 I915_WRITE(WM3_LP_ILK, I915_READ(WM3_LP_ILK) & ~WM1_LP_SR_EN);
6903 I915_WRITE(WM2_LP_ILK, I915_READ(WM2_LP_ILK) & ~WM1_LP_SR_EN);
6904 I915_WRITE(WM1_LP_ILK, I915_READ(WM1_LP_ILK) & ~WM1_LP_SR_EN);
6905
6906 /*
6907 * Don't touch WM1S_LP_EN here.
6908 * Doing so could cause underruns.
6909 */
6910}
6911
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006912static void ironlake_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006913{
Chris Wilsonfac5e232016-07-04 11:34:36 +01006914 struct drm_i915_private *dev_priv = to_i915(dev);
Damien Lespiau231e54f2012-10-19 17:55:41 +01006915 uint32_t dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006916
Damien Lespiauf1e8fa52013-06-07 17:41:09 +01006917 /*
6918 * Required for FBC
6919 * WaFbcDisableDpfcClockGating:ilk
6920 */
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01006921 dspclk_gate |= ILK_DPFCRUNIT_CLOCK_GATE_DISABLE |
6922 ILK_DPFCUNIT_CLOCK_GATE_DISABLE |
6923 ILK_DPFDUNIT_CLOCK_GATE_ENABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006924
6925 I915_WRITE(PCH_3DCGDIS0,
6926 MARIUNIT_CLOCK_GATE_DISABLE |
6927 SVSMUNIT_CLOCK_GATE_DISABLE);
6928 I915_WRITE(PCH_3DCGDIS1,
6929 VFMUNIT_CLOCK_GATE_DISABLE);
6930
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006931 /*
6932 * According to the spec the following bits should be set in
6933 * order to enable memory self-refresh
6934 * The bit 22/21 of 0x42004
6935 * The bit 5 of 0x42020
6936 * The bit 15 of 0x45000
6937 */
6938 I915_WRITE(ILK_DISPLAY_CHICKEN2,
6939 (I915_READ(ILK_DISPLAY_CHICKEN2) |
6940 ILK_DPARB_GATE | ILK_VSDPFD_FULL));
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01006941 dspclk_gate |= ILK_DPARBUNIT_CLOCK_GATE_ENABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006942 I915_WRITE(DISP_ARB_CTL,
6943 (I915_READ(DISP_ARB_CTL) |
6944 DISP_FBC_WM_DIS));
Ville Syrjälä017636c2013-12-05 15:51:37 +02006945
6946 ilk_init_lp_watermarks(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006947
6948 /*
6949 * Based on the document from hardware guys the following bits
6950 * should be set unconditionally in order to enable FBC.
6951 * The bit 22 of 0x42000
6952 * The bit 22 of 0x42004
6953 * The bit 7,8,9 of 0x42020.
6954 */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01006955 if (IS_IRONLAKE_M(dev_priv)) {
Damien Lespiau4bb35332013-06-14 15:23:24 +01006956 /* WaFbcAsynchFlipDisableFbcQueue:ilk */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006957 I915_WRITE(ILK_DISPLAY_CHICKEN1,
6958 I915_READ(ILK_DISPLAY_CHICKEN1) |
6959 ILK_FBCQ_DIS);
6960 I915_WRITE(ILK_DISPLAY_CHICKEN2,
6961 I915_READ(ILK_DISPLAY_CHICKEN2) |
6962 ILK_DPARB_GATE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006963 }
6964
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01006965 I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
6966
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006967 I915_WRITE(ILK_DISPLAY_CHICKEN2,
6968 I915_READ(ILK_DISPLAY_CHICKEN2) |
6969 ILK_ELPIN_409_SELECT);
6970 I915_WRITE(_3D_CHICKEN2,
6971 _3D_CHICKEN2_WM_READ_PIPELINED << 16 |
6972 _3D_CHICKEN2_WM_READ_PIPELINED);
Daniel Vetter4358a372012-10-18 11:49:51 +02006973
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006974 /* WaDisableRenderCachePipelinedFlush:ilk */
Daniel Vetter4358a372012-10-18 11:49:51 +02006975 I915_WRITE(CACHE_MODE_0,
6976 _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
Daniel Vetter3107bd42012-10-31 22:52:31 +01006977
Akash Goel4e046322014-04-04 17:14:38 +05306978 /* WaDisable_RenderCache_OperationalFlush:ilk */
6979 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
6980
Ville Syrjälä0e088b82013-06-07 10:47:04 +03006981 g4x_disable_trickle_feed(dev);
Ville Syrjäläbdad2b22013-06-07 10:47:03 +03006982
Daniel Vetter3107bd42012-10-31 22:52:31 +01006983 ibx_init_clock_gating(dev);
6984}
6985
6986static void cpt_init_clock_gating(struct drm_device *dev)
6987{
Chris Wilsonfac5e232016-07-04 11:34:36 +01006988 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter3107bd42012-10-31 22:52:31 +01006989 int pipe;
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03006990 uint32_t val;
Daniel Vetter3107bd42012-10-31 22:52:31 +01006991
6992 /*
6993 * On Ibex Peak and Cougar Point, we need to disable clock
6994 * gating for the panel power sequencer or it will fail to
6995 * start up when no ports are active.
6996 */
Jesse Barnescd664072013-10-02 10:34:19 -07006997 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE |
6998 PCH_DPLUNIT_CLOCK_GATE_DISABLE |
6999 PCH_CPUNIT_CLOCK_GATE_DISABLE);
Daniel Vetter3107bd42012-10-31 22:52:31 +01007000 I915_WRITE(SOUTH_CHICKEN2, I915_READ(SOUTH_CHICKEN2) |
7001 DPLS_EDP_PPS_FIX_DIS);
Takashi Iwai335c07b2012-12-11 11:46:29 +01007002 /* The below fixes the weird display corruption, a few pixels shifted
7003 * downward, on (only) LVDS of some HP laptops with IVY.
7004 */
Damien Lespiau055e3932014-08-18 13:49:10 +01007005 for_each_pipe(dev_priv, pipe) {
Paulo Zanonidc4bd2d2013-04-08 15:48:08 -03007006 val = I915_READ(TRANS_CHICKEN2(pipe));
7007 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
7008 val &= ~TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03007009 if (dev_priv->vbt.fdi_rx_polarity_inverted)
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03007010 val |= TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
Paulo Zanonidc4bd2d2013-04-08 15:48:08 -03007011 val &= ~TRANS_CHICKEN2_FRAME_START_DELAY_MASK;
7012 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_COUNTER;
7013 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_MODESWITCH;
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03007014 I915_WRITE(TRANS_CHICKEN2(pipe), val);
7015 }
Daniel Vetter3107bd42012-10-31 22:52:31 +01007016 /* WADP0ClockGatingDisable */
Damien Lespiau055e3932014-08-18 13:49:10 +01007017 for_each_pipe(dev_priv, pipe) {
Daniel Vetter3107bd42012-10-31 22:52:31 +01007018 I915_WRITE(TRANS_CHICKEN1(pipe),
7019 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
7020 }
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007021}
7022
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01007023static void gen6_check_mch_setup(struct drm_device *dev)
7024{
Chris Wilsonfac5e232016-07-04 11:34:36 +01007025 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01007026 uint32_t tmp;
7027
7028 tmp = I915_READ(MCH_SSKPD);
Daniel Vetterdf662a22014-08-04 11:17:25 +02007029 if ((tmp & MCH_SSKPD_WM0_MASK) != MCH_SSKPD_WM0_VAL)
7030 DRM_DEBUG_KMS("Wrong MCH_SSKPD value: 0x%08x This can cause underruns.\n",
7031 tmp);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01007032}
7033
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007034static void gen6_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007035{
Chris Wilsonfac5e232016-07-04 11:34:36 +01007036 struct drm_i915_private *dev_priv = to_i915(dev);
Damien Lespiau231e54f2012-10-19 17:55:41 +01007037 uint32_t dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007038
Damien Lespiau231e54f2012-10-19 17:55:41 +01007039 I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007040
7041 I915_WRITE(ILK_DISPLAY_CHICKEN2,
7042 I915_READ(ILK_DISPLAY_CHICKEN2) |
7043 ILK_ELPIN_409_SELECT);
7044
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007045 /* WaDisableHiZPlanesWhenMSAAEnabled:snb */
Daniel Vetter42839082012-12-14 23:38:28 +01007046 I915_WRITE(_3D_CHICKEN,
7047 _MASKED_BIT_ENABLE(_3D_CHICKEN_HIZ_PLANE_DISABLE_MSAA_4X_SNB));
7048
Akash Goel4e046322014-04-04 17:14:38 +05307049 /* WaDisable_RenderCache_OperationalFlush:snb */
7050 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
7051
Ville Syrjälä8d85d272014-02-04 21:59:15 +02007052 /*
7053 * BSpec recoomends 8x4 when MSAA is used,
7054 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02007055 *
7056 * Note that PS/WM thread counts depend on the WIZ hashing
7057 * disable bit, which we don't touch here, but it's good
7058 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjälä8d85d272014-02-04 21:59:15 +02007059 */
7060 I915_WRITE(GEN6_GT_MODE,
Damien Lespiau98533252014-12-08 17:33:51 +00007061 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
Ville Syrjälä8d85d272014-02-04 21:59:15 +02007062
Ville Syrjälä017636c2013-12-05 15:51:37 +02007063 ilk_init_lp_watermarks(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007064
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007065 I915_WRITE(CACHE_MODE_0,
Daniel Vetter50743292012-04-26 22:02:54 +02007066 _MASKED_BIT_DISABLE(CM0_STC_EVICT_DISABLE_LRA_SNB));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007067
7068 I915_WRITE(GEN6_UCGCTL1,
7069 I915_READ(GEN6_UCGCTL1) |
7070 GEN6_BLBUNIT_CLOCK_GATE_DISABLE |
7071 GEN6_CSUNIT_CLOCK_GATE_DISABLE);
7072
7073 /* According to the BSpec vol1g, bit 12 (RCPBUNIT) clock
7074 * gating disable must be set. Failure to set it results in
7075 * flickering pixels due to Z write ordering failures after
7076 * some amount of runtime in the Mesa "fire" demo, and Unigine
7077 * Sanctuary and Tropics, and apparently anything else with
7078 * alpha test or pixel discard.
7079 *
7080 * According to the spec, bit 11 (RCCUNIT) must also be set,
7081 * but we didn't debug actual testcases to find it out.
Jesse Barnes0f846f82012-06-14 11:04:47 -07007082 *
Ville Syrjäläef593182014-01-22 21:32:47 +02007083 * WaDisableRCCUnitClockGating:snb
7084 * WaDisableRCPBUnitClockGating:snb
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007085 */
7086 I915_WRITE(GEN6_UCGCTL2,
7087 GEN6_RCPBUNIT_CLOCK_GATE_DISABLE |
7088 GEN6_RCCUNIT_CLOCK_GATE_DISABLE);
7089
Ville Syrjälä5eb146d2014-02-04 21:59:16 +02007090 /* WaStripsFansDisableFastClipPerformanceFix:snb */
Ville Syrjälä743b57d2014-02-04 21:59:17 +02007091 I915_WRITE(_3D_CHICKEN3,
7092 _MASKED_BIT_ENABLE(_3D_CHICKEN3_SF_DISABLE_FASTCLIP_CULL));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007093
7094 /*
Ville Syrjäläe927ecd2014-02-04 21:59:18 +02007095 * Bspec says:
7096 * "This bit must be set if 3DSTATE_CLIP clip mode is set to normal and
7097 * 3DSTATE_SF number of SF output attributes is more than 16."
7098 */
7099 I915_WRITE(_3D_CHICKEN3,
7100 _MASKED_BIT_ENABLE(_3D_CHICKEN3_SF_DISABLE_PIPELINED_ATTR_FETCH));
7101
7102 /*
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007103 * According to the spec the following bits should be
7104 * set in order to enable memory self-refresh and fbc:
7105 * The bit21 and bit22 of 0x42000
7106 * The bit21 and bit22 of 0x42004
7107 * The bit5 and bit7 of 0x42020
7108 * The bit14 of 0x70180
7109 * The bit14 of 0x71180
Damien Lespiau4bb35332013-06-14 15:23:24 +01007110 *
7111 * WaFbcAsynchFlipDisableFbcQueue:snb
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007112 */
7113 I915_WRITE(ILK_DISPLAY_CHICKEN1,
7114 I915_READ(ILK_DISPLAY_CHICKEN1) |
7115 ILK_FBCQ_DIS | ILK_PABSTRETCH_DIS);
7116 I915_WRITE(ILK_DISPLAY_CHICKEN2,
7117 I915_READ(ILK_DISPLAY_CHICKEN2) |
7118 ILK_DPARB_GATE | ILK_VSDPFD_FULL);
Damien Lespiau231e54f2012-10-19 17:55:41 +01007119 I915_WRITE(ILK_DSPCLK_GATE_D,
7120 I915_READ(ILK_DSPCLK_GATE_D) |
7121 ILK_DPARBUNIT_CLOCK_GATE_ENABLE |
7122 ILK_DPFDUNIT_CLOCK_GATE_ENABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007123
Ville Syrjälä0e088b82013-06-07 10:47:04 +03007124 g4x_disable_trickle_feed(dev);
Ben Widawskyf8f2ac92012-10-03 19:34:24 -07007125
Daniel Vetter3107bd42012-10-31 22:52:31 +01007126 cpt_init_clock_gating(dev);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01007127
7128 gen6_check_mch_setup(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007129}
7130
7131static void gen7_setup_fixed_func_scheduler(struct drm_i915_private *dev_priv)
7132{
7133 uint32_t reg = I915_READ(GEN7_FF_THREAD_MODE);
7134
Ville Syrjälä3aad9052014-01-22 21:32:59 +02007135 /*
Ville Syrjälä46680e02014-01-22 21:33:01 +02007136 * WaVSThreadDispatchOverride:ivb,vlv
Ville Syrjälä3aad9052014-01-22 21:32:59 +02007137 *
7138 * This actually overrides the dispatch
7139 * mode for all thread types.
7140 */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007141 reg &= ~GEN7_FF_SCHED_MASK;
7142 reg |= GEN7_FF_TS_SCHED_HW;
7143 reg |= GEN7_FF_VS_SCHED_HW;
7144 reg |= GEN7_FF_DS_SCHED_HW;
7145
7146 I915_WRITE(GEN7_FF_THREAD_MODE, reg);
7147}
7148
Paulo Zanoni17a303e2012-11-20 15:12:07 -02007149static void lpt_init_clock_gating(struct drm_device *dev)
7150{
Chris Wilsonfac5e232016-07-04 11:34:36 +01007151 struct drm_i915_private *dev_priv = to_i915(dev);
Paulo Zanoni17a303e2012-11-20 15:12:07 -02007152
7153 /*
7154 * TODO: this bit should only be enabled when really needed, then
7155 * disabled when not needed anymore in order to save power.
7156 */
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01007157 if (HAS_PCH_LPT_LP(dev_priv))
Paulo Zanoni17a303e2012-11-20 15:12:07 -02007158 I915_WRITE(SOUTH_DSPCLK_GATE_D,
7159 I915_READ(SOUTH_DSPCLK_GATE_D) |
7160 PCH_LP_PARTITION_LEVEL_DISABLE);
Paulo Zanoni0a790cd2013-04-17 18:15:49 -03007161
7162 /* WADPOClockGatingDisable:hsw */
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03007163 I915_WRITE(TRANS_CHICKEN1(PIPE_A),
7164 I915_READ(TRANS_CHICKEN1(PIPE_A)) |
Paulo Zanoni0a790cd2013-04-17 18:15:49 -03007165 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
Paulo Zanoni17a303e2012-11-20 15:12:07 -02007166}
7167
Imre Deak7d708ee2013-04-17 14:04:50 +03007168static void lpt_suspend_hw(struct drm_device *dev)
7169{
Chris Wilsonfac5e232016-07-04 11:34:36 +01007170 struct drm_i915_private *dev_priv = to_i915(dev);
Imre Deak7d708ee2013-04-17 14:04:50 +03007171
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01007172 if (HAS_PCH_LPT_LP(dev_priv)) {
Imre Deak7d708ee2013-04-17 14:04:50 +03007173 uint32_t val = I915_READ(SOUTH_DSPCLK_GATE_D);
7174
7175 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
7176 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
7177 }
7178}
7179
Imre Deak450174f2016-05-03 15:54:21 +03007180static void gen8_set_l3sqc_credits(struct drm_i915_private *dev_priv,
7181 int general_prio_credits,
7182 int high_prio_credits)
7183{
7184 u32 misccpctl;
7185
7186 /* WaTempDisableDOPClkGating:bdw */
7187 misccpctl = I915_READ(GEN7_MISCCPCTL);
7188 I915_WRITE(GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
7189
7190 I915_WRITE(GEN8_L3SQCREG1,
7191 L3_GENERAL_PRIO_CREDITS(general_prio_credits) |
7192 L3_HIGH_PRIO_CREDITS(high_prio_credits));
7193
7194 /*
7195 * Wait at least 100 clocks before re-enabling clock gating.
7196 * See the definition of L3SQCREG1 in BSpec.
7197 */
7198 POSTING_READ(GEN8_L3SQCREG1);
7199 udelay(1);
7200 I915_WRITE(GEN7_MISCCPCTL, misccpctl);
7201}
7202
Mika Kuoppala9498dba2016-06-07 17:19:01 +03007203static void kabylake_init_clock_gating(struct drm_device *dev)
7204{
Mika Kuoppala9146f302016-06-07 17:19:01 +03007205 struct drm_i915_private *dev_priv = dev->dev_private;
Mika Kuoppala9498dba2016-06-07 17:19:01 +03007206
Mika Kuoppalab033bb62016-06-07 17:19:04 +03007207 gen9_init_clock_gating(dev);
Mika Kuoppala9498dba2016-06-07 17:19:01 +03007208
7209 /* WaDisableSDEUnitClockGating:kbl */
7210 if (IS_KBL_REVID(dev_priv, 0, KBL_REVID_B0))
7211 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
7212 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Mika Kuoppala8aeb7f62016-06-07 17:19:05 +03007213
7214 /* WaDisableGamClockGating:kbl */
7215 if (IS_KBL_REVID(dev_priv, 0, KBL_REVID_B0))
7216 I915_WRITE(GEN6_UCGCTL1, I915_READ(GEN6_UCGCTL1) |
7217 GEN6_GAMUNIT_CLOCK_GATE_DISABLE);
Mika Kuoppala031cd8c2016-06-07 17:19:18 +03007218
7219 /* WaFbcNukeOnHostModify:kbl */
7220 I915_WRITE(ILK_DPFC_CHICKEN, I915_READ(ILK_DPFC_CHICKEN) |
7221 ILK_DPFC_NUKE_ON_ANY_MODIFICATION);
Mika Kuoppala9498dba2016-06-07 17:19:01 +03007222}
7223
Daniel Vetterdc00b6a2016-05-19 09:14:20 +02007224static void skylake_init_clock_gating(struct drm_device *dev)
7225{
Mika Kuoppalac584e2d2016-06-07 17:19:18 +03007226 struct drm_i915_private *dev_priv = dev->dev_private;
Mika Kuoppala44fff992016-06-07 17:19:09 +03007227
Mika Kuoppalab033bb62016-06-07 17:19:04 +03007228 gen9_init_clock_gating(dev);
Mika Kuoppala44fff992016-06-07 17:19:09 +03007229
7230 /* WAC6entrylatency:skl */
7231 I915_WRITE(FBC_LLC_READ_CTRL, I915_READ(FBC_LLC_READ_CTRL) |
7232 FBC_LLC_FULLY_OPEN);
Mika Kuoppala031cd8c2016-06-07 17:19:18 +03007233
7234 /* WaFbcNukeOnHostModify:skl */
7235 I915_WRITE(ILK_DPFC_CHICKEN, I915_READ(ILK_DPFC_CHICKEN) |
7236 ILK_DPFC_NUKE_ON_ANY_MODIFICATION);
Daniel Vetterdc00b6a2016-05-19 09:14:20 +02007237}
7238
Paulo Zanoni47c2bd92014-08-21 17:09:37 -03007239static void broadwell_init_clock_gating(struct drm_device *dev)
Ben Widawsky1020a5c2013-11-02 21:07:06 -07007240{
Chris Wilsonfac5e232016-07-04 11:34:36 +01007241 struct drm_i915_private *dev_priv = to_i915(dev);
Damien Lespiau07d27e22014-03-03 17:31:46 +00007242 enum pipe pipe;
Ben Widawsky1020a5c2013-11-02 21:07:06 -07007243
Ville Syrjälä7ad0dba2015-05-19 20:32:55 +03007244 ilk_init_lp_watermarks(dev);
Ben Widawsky50ed5fb2013-11-02 21:07:40 -07007245
Ben Widawskyab57fff2013-12-12 15:28:04 -08007246 /* WaSwitchSolVfFArbitrationPriority:bdw */
Ben Widawsky50ed5fb2013-11-02 21:07:40 -07007247 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07007248
Ben Widawskyab57fff2013-12-12 15:28:04 -08007249 /* WaPsrDPAMaskVBlankInSRD:bdw */
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07007250 I915_WRITE(CHICKEN_PAR1_1,
7251 I915_READ(CHICKEN_PAR1_1) | DPA_MASK_VBLANK_SRD);
7252
Ben Widawskyab57fff2013-12-12 15:28:04 -08007253 /* WaPsrDPRSUnmaskVBlankInSRD:bdw */
Damien Lespiau055e3932014-08-18 13:49:10 +01007254 for_each_pipe(dev_priv, pipe) {
Damien Lespiau07d27e22014-03-03 17:31:46 +00007255 I915_WRITE(CHICKEN_PIPESL_1(pipe),
Ville Syrjäläc7c65622014-03-05 13:05:45 +02007256 I915_READ(CHICKEN_PIPESL_1(pipe)) |
Ville Syrjälä8f670bb2014-03-05 13:05:47 +02007257 BDW_DPRS_MASK_VBLANK_SRD);
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07007258 }
Ben Widawsky63801f22013-12-12 17:26:03 -08007259
Ben Widawskyab57fff2013-12-12 15:28:04 -08007260 /* WaVSRefCountFullforceMissDisable:bdw */
7261 /* WaDSRefCountFullforceMissDisable:bdw */
7262 I915_WRITE(GEN7_FF_THREAD_MODE,
7263 I915_READ(GEN7_FF_THREAD_MODE) &
7264 ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
Ville Syrjälä36075a42014-02-04 21:59:21 +02007265
Ville Syrjälä295e8bb2014-02-27 21:59:01 +02007266 I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
7267 _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
Ville Syrjälä4f1ca9e2014-02-27 21:59:02 +02007268
7269 /* WaDisableSDEUnitClockGating:bdw */
7270 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
7271 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Damien Lespiau5d708682014-03-26 18:41:51 +00007272
Imre Deak450174f2016-05-03 15:54:21 +03007273 /* WaProgramL3SqcReg1Default:bdw */
7274 gen8_set_l3sqc_credits(dev_priv, 30, 2);
Ville Syrjälä4d487cf2015-05-19 20:32:56 +03007275
Ville Syrjälä6d50b062015-05-19 20:32:57 +03007276 /*
7277 * WaGttCachingOffByDefault:bdw
7278 * GTT cache may not work with big pages, so if those
7279 * are ever enabled GTT cache may need to be disabled.
7280 */
7281 I915_WRITE(HSW_GTT_CACHE_EN, GTT_CACHE_EN_ALL);
7282
Mika Kuoppala17e0adf2016-06-07 17:19:02 +03007283 /* WaKVMNotificationOnConfigChange:bdw */
7284 I915_WRITE(CHICKEN_PAR2_1, I915_READ(CHICKEN_PAR2_1)
7285 | KVM_CONFIG_CHANGE_NOTIFICATION_SELECT);
7286
Paulo Zanoni89d6b2b2014-08-21 17:09:36 -03007287 lpt_init_clock_gating(dev);
Ben Widawsky1020a5c2013-11-02 21:07:06 -07007288}
7289
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03007290static void haswell_init_clock_gating(struct drm_device *dev)
7291{
Chris Wilsonfac5e232016-07-04 11:34:36 +01007292 struct drm_i915_private *dev_priv = to_i915(dev);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03007293
Ville Syrjälä017636c2013-12-05 15:51:37 +02007294 ilk_init_lp_watermarks(dev);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03007295
Francisco Jerezf3fc4882013-10-02 15:53:16 -07007296 /* L3 caching of data atomics doesn't work -- disable it. */
7297 I915_WRITE(HSW_SCRATCH1, HSW_SCRATCH1_L3_DATA_ATOMICS_DISABLE);
7298 I915_WRITE(HSW_ROW_CHICKEN3,
7299 _MASKED_BIT_ENABLE(HSW_ROW_CHICKEN3_L3_GLOBAL_ATOMICS_DISABLE));
7300
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007301 /* This is required by WaCatErrorRejectionIssue:hsw */
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03007302 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
7303 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
7304 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
7305
Ville Syrjäläe36ea7f2014-01-22 21:33:00 +02007306 /* WaVSRefCountFullforceMissDisable:hsw */
7307 I915_WRITE(GEN7_FF_THREAD_MODE,
7308 I915_READ(GEN7_FF_THREAD_MODE) & ~GEN7_FF_VS_REF_CNT_FFME);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03007309
Akash Goel4e046322014-04-04 17:14:38 +05307310 /* WaDisable_RenderCache_OperationalFlush:hsw */
7311 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
7312
Chia-I Wufe27c602014-01-28 13:29:33 +08007313 /* enable HiZ Raw Stall Optimization */
7314 I915_WRITE(CACHE_MODE_0_GEN7,
7315 _MASKED_BIT_DISABLE(HIZ_RAW_STALL_OPT_DISABLE));
7316
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007317 /* WaDisable4x2SubspanOptimization:hsw */
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03007318 I915_WRITE(CACHE_MODE_1,
7319 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Eugeni Dodonov1544d9d2012-07-02 11:51:10 -03007320
Ville Syrjäläa12c4962014-02-04 21:59:20 +02007321 /*
7322 * BSpec recommends 8x4 when MSAA is used,
7323 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02007324 *
7325 * Note that PS/WM thread counts depend on the WIZ hashing
7326 * disable bit, which we don't touch here, but it's good
7327 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjäläa12c4962014-02-04 21:59:20 +02007328 */
7329 I915_WRITE(GEN7_GT_MODE,
Damien Lespiau98533252014-12-08 17:33:51 +00007330 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
Ville Syrjäläa12c4962014-02-04 21:59:20 +02007331
Kenneth Graunke94411592014-12-31 16:23:00 -08007332 /* WaSampleCChickenBitEnable:hsw */
7333 I915_WRITE(HALF_SLICE_CHICKEN3,
7334 _MASKED_BIT_ENABLE(HSW_SAMPLE_C_PERFORMANCE));
7335
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007336 /* WaSwitchSolVfFArbitrationPriority:hsw */
Ben Widawskye3dff582013-03-20 14:49:14 -07007337 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
7338
Paulo Zanoni90a88642013-05-03 17:23:45 -03007339 /* WaRsPkgCStateDisplayPMReq:hsw */
7340 I915_WRITE(CHICKEN_PAR1_1,
7341 I915_READ(CHICKEN_PAR1_1) | FORCE_ARB_IDLE_PLANES);
Eugeni Dodonov1544d9d2012-07-02 11:51:10 -03007342
Paulo Zanoni17a303e2012-11-20 15:12:07 -02007343 lpt_init_clock_gating(dev);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03007344}
7345
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007346static void ivybridge_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007347{
Chris Wilsonfac5e232016-07-04 11:34:36 +01007348 struct drm_i915_private *dev_priv = to_i915(dev);
Ben Widawsky20848222012-05-04 18:58:59 -07007349 uint32_t snpcr;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007350
Ville Syrjälä017636c2013-12-05 15:51:37 +02007351 ilk_init_lp_watermarks(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007352
Damien Lespiau231e54f2012-10-19 17:55:41 +01007353 I915_WRITE(ILK_DSPCLK_GATE_D, ILK_VRHUNIT_CLOCK_GATE_DISABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007354
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007355 /* WaDisableEarlyCull:ivb */
Jesse Barnes87f80202012-10-02 17:43:41 -05007356 I915_WRITE(_3D_CHICKEN3,
7357 _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
7358
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007359 /* WaDisableBackToBackFlipFix:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007360 I915_WRITE(IVB_CHICKEN3,
7361 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
7362 CHICKEN3_DGMG_DONE_FIX_DISABLE);
7363
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007364 /* WaDisablePSDDualDispatchEnable:ivb */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01007365 if (IS_IVB_GT1(dev_priv))
Jesse Barnes12f33822012-10-25 12:15:45 -07007366 I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
7367 _MASKED_BIT_ENABLE(GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
Jesse Barnes12f33822012-10-25 12:15:45 -07007368
Akash Goel4e046322014-04-04 17:14:38 +05307369 /* WaDisable_RenderCache_OperationalFlush:ivb */
7370 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
7371
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007372 /* Apply the WaDisableRHWOOptimizationForRenderHang:ivb workaround. */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007373 I915_WRITE(GEN7_COMMON_SLICE_CHICKEN1,
7374 GEN7_CSC1_RHWO_OPT_DISABLE_IN_RCC);
7375
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007376 /* WaApplyL3ControlAndL3ChickenMode:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007377 I915_WRITE(GEN7_L3CNTLREG1,
7378 GEN7_WA_FOR_GEN7_L3_CONTROL);
7379 I915_WRITE(GEN7_L3_CHICKEN_MODE_REGISTER,
Jesse Barnes8ab43972012-10-25 12:15:42 -07007380 GEN7_WA_L3_CHICKEN_MODE);
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01007381 if (IS_IVB_GT1(dev_priv))
Jesse Barnes8ab43972012-10-25 12:15:42 -07007382 I915_WRITE(GEN7_ROW_CHICKEN2,
7383 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Ville Syrjälä412236c2014-01-22 21:32:44 +02007384 else {
7385 /* must write both registers */
7386 I915_WRITE(GEN7_ROW_CHICKEN2,
7387 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Jesse Barnes8ab43972012-10-25 12:15:42 -07007388 I915_WRITE(GEN7_ROW_CHICKEN2_GT2,
7389 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Ville Syrjälä412236c2014-01-22 21:32:44 +02007390 }
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007391
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007392 /* WaForceL3Serialization:ivb */
Jesse Barnes61939d92012-10-02 17:43:38 -05007393 I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
7394 ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
7395
Ville Syrjälä1b80a19a2014-01-22 21:32:53 +02007396 /*
Jesse Barnes0f846f82012-06-14 11:04:47 -07007397 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007398 * This implements the WaDisableRCZUnitClockGating:ivb workaround.
Jesse Barnes0f846f82012-06-14 11:04:47 -07007399 */
7400 I915_WRITE(GEN6_UCGCTL2,
Ville Syrjälä28acf3b2014-01-22 21:32:48 +02007401 GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
Jesse Barnes0f846f82012-06-14 11:04:47 -07007402
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007403 /* This is required by WaCatErrorRejectionIssue:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007404 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
7405 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
7406 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
7407
Ville Syrjälä0e088b82013-06-07 10:47:04 +03007408 g4x_disable_trickle_feed(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007409
7410 gen7_setup_fixed_func_scheduler(dev_priv);
Daniel Vetter97e19302012-04-24 16:00:21 +02007411
Chris Wilson22721342014-03-04 09:41:43 +00007412 if (0) { /* causes HiZ corruption on ivb:gt1 */
7413 /* enable HiZ Raw Stall Optimization */
7414 I915_WRITE(CACHE_MODE_0_GEN7,
7415 _MASKED_BIT_DISABLE(HIZ_RAW_STALL_OPT_DISABLE));
7416 }
Chia-I Wu116f2b62014-01-28 13:29:34 +08007417
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007418 /* WaDisable4x2SubspanOptimization:ivb */
Daniel Vetter97e19302012-04-24 16:00:21 +02007419 I915_WRITE(CACHE_MODE_1,
7420 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Ben Widawsky20848222012-05-04 18:58:59 -07007421
Ville Syrjäläa607c1a2014-02-04 21:59:19 +02007422 /*
7423 * BSpec recommends 8x4 when MSAA is used,
7424 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02007425 *
7426 * Note that PS/WM thread counts depend on the WIZ hashing
7427 * disable bit, which we don't touch here, but it's good
7428 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjäläa607c1a2014-02-04 21:59:19 +02007429 */
7430 I915_WRITE(GEN7_GT_MODE,
Damien Lespiau98533252014-12-08 17:33:51 +00007431 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
Ville Syrjäläa607c1a2014-02-04 21:59:19 +02007432
Ben Widawsky20848222012-05-04 18:58:59 -07007433 snpcr = I915_READ(GEN6_MBCUNIT_SNPCR);
7434 snpcr &= ~GEN6_MBC_SNPCR_MASK;
7435 snpcr |= GEN6_MBC_SNPCR_MED;
7436 I915_WRITE(GEN6_MBCUNIT_SNPCR, snpcr);
Daniel Vetter3107bd42012-10-31 22:52:31 +01007437
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01007438 if (!HAS_PCH_NOP(dev_priv))
Ben Widawskyab5c6082013-04-05 13:12:41 -07007439 cpt_init_clock_gating(dev);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01007440
7441 gen6_check_mch_setup(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007442}
7443
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007444static void valleyview_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007445{
Chris Wilsonfac5e232016-07-04 11:34:36 +01007446 struct drm_i915_private *dev_priv = to_i915(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007447
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007448 /* WaDisableEarlyCull:vlv */
Jesse Barnes87f80202012-10-02 17:43:41 -05007449 I915_WRITE(_3D_CHICKEN3,
7450 _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
7451
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007452 /* WaDisableBackToBackFlipFix:vlv */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007453 I915_WRITE(IVB_CHICKEN3,
7454 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
7455 CHICKEN3_DGMG_DONE_FIX_DISABLE);
7456
Ville Syrjäläfad7d362014-01-22 21:32:39 +02007457 /* WaPsdDispatchEnable:vlv */
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007458 /* WaDisablePSDDualDispatchEnable:vlv */
Jesse Barnes12f33822012-10-25 12:15:45 -07007459 I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
Jesse Barnesd3bc0302013-03-08 10:45:51 -08007460 _MASKED_BIT_ENABLE(GEN7_MAX_PS_THREAD_DEP |
7461 GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
Jesse Barnes12f33822012-10-25 12:15:45 -07007462
Akash Goel4e046322014-04-04 17:14:38 +05307463 /* WaDisable_RenderCache_OperationalFlush:vlv */
7464 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
7465
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007466 /* WaForceL3Serialization:vlv */
Jesse Barnes61939d92012-10-02 17:43:38 -05007467 I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
7468 ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
7469
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007470 /* WaDisableDopClockGating:vlv */
Jesse Barnes8ab43972012-10-25 12:15:42 -07007471 I915_WRITE(GEN7_ROW_CHICKEN2,
7472 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
7473
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007474 /* This is required by WaCatErrorRejectionIssue:vlv */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007475 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
7476 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
7477 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
7478
Ville Syrjälä46680e02014-01-22 21:33:01 +02007479 gen7_setup_fixed_func_scheduler(dev_priv);
7480
Ville Syrjälä3c0edae2014-01-22 21:32:56 +02007481 /*
Jesse Barnes0f846f82012-06-14 11:04:47 -07007482 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007483 * This implements the WaDisableRCZUnitClockGating:vlv workaround.
Jesse Barnes0f846f82012-06-14 11:04:47 -07007484 */
7485 I915_WRITE(GEN6_UCGCTL2,
Ville Syrjälä3c0edae2014-01-22 21:32:56 +02007486 GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
Jesse Barnes0f846f82012-06-14 11:04:47 -07007487
Akash Goelc98f5062014-03-24 23:00:07 +05307488 /* WaDisableL3Bank2xClockGate:vlv
7489 * Disabling L3 clock gating- MMIO 940c[25] = 1
7490 * Set bit 25, to disable L3_BANK_2x_CLK_GATING */
7491 I915_WRITE(GEN7_UCGCTL4,
7492 I915_READ(GEN7_UCGCTL4) | GEN7_L3BANK2X_CLOCK_GATE_DISABLE);
Jesse Barnese3f33d42012-06-14 11:04:50 -07007493
Ville Syrjäläafd58e72014-01-22 21:33:03 +02007494 /*
7495 * BSpec says this must be set, even though
7496 * WaDisable4x2SubspanOptimization isn't listed for VLV.
7497 */
Daniel Vetter6b26c862012-04-24 14:04:12 +02007498 I915_WRITE(CACHE_MODE_1,
7499 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Jesse Barnes79831172012-06-20 10:53:12 -07007500
7501 /*
Ville Syrjäläda2518f2015-01-21 19:38:01 +02007502 * BSpec recommends 8x4 when MSAA is used,
7503 * however in practice 16x4 seems fastest.
7504 *
7505 * Note that PS/WM thread counts depend on the WIZ hashing
7506 * disable bit, which we don't touch here, but it's good
7507 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
7508 */
7509 I915_WRITE(GEN7_GT_MODE,
7510 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
7511
7512 /*
Ville Syrjälä031994e2014-01-22 21:32:46 +02007513 * WaIncreaseL3CreditsForVLVB0:vlv
7514 * This is the hardware default actually.
7515 */
7516 I915_WRITE(GEN7_L3SQCREG1, VLV_B0_WA_L3SQCREG1_VALUE);
7517
7518 /*
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007519 * WaDisableVLVClockGating_VBIIssue:vlv
Jesse Barnes2d809572012-10-25 12:15:44 -07007520 * Disable clock gating on th GCFG unit to prevent a delay
7521 * in the reporting of vblank events.
7522 */
Ville Syrjälä7a0d1ee2014-01-22 21:33:04 +02007523 I915_WRITE(VLV_GUNIT_CLOCK_GATE, GCFG_DIS);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007524}
7525
Ville Syrjäläa4565da2014-04-09 13:28:10 +03007526static void cherryview_init_clock_gating(struct drm_device *dev)
7527{
Chris Wilsonfac5e232016-07-04 11:34:36 +01007528 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläa4565da2014-04-09 13:28:10 +03007529
Ville Syrjälä232ce332014-04-09 13:28:35 +03007530 /* WaVSRefCountFullforceMissDisable:chv */
7531 /* WaDSRefCountFullforceMissDisable:chv */
7532 I915_WRITE(GEN7_FF_THREAD_MODE,
7533 I915_READ(GEN7_FF_THREAD_MODE) &
7534 ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
Ville Syrjäläacea6f92014-04-09 13:28:36 +03007535
7536 /* WaDisableSemaphoreAndSyncFlipWait:chv */
7537 I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
7538 _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
Ville Syrjälä08466972014-04-09 13:28:37 +03007539
7540 /* WaDisableCSUnitClockGating:chv */
7541 I915_WRITE(GEN6_UCGCTL1, I915_READ(GEN6_UCGCTL1) |
7542 GEN6_CSUNIT_CLOCK_GATE_DISABLE);
Ville Syrjäläc6317802014-04-09 13:28:38 +03007543
7544 /* WaDisableSDEUnitClockGating:chv */
7545 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
7546 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Ville Syrjälä6d50b062015-05-19 20:32:57 +03007547
7548 /*
Imre Deak450174f2016-05-03 15:54:21 +03007549 * WaProgramL3SqcReg1Default:chv
7550 * See gfxspecs/Related Documents/Performance Guide/
7551 * LSQC Setting Recommendations.
7552 */
7553 gen8_set_l3sqc_credits(dev_priv, 38, 2);
7554
7555 /*
Ville Syrjälä6d50b062015-05-19 20:32:57 +03007556 * GTT cache may not work with big pages, so if those
7557 * are ever enabled GTT cache may need to be disabled.
7558 */
7559 I915_WRITE(HSW_GTT_CACHE_EN, GTT_CACHE_EN_ALL);
Ville Syrjäläa4565da2014-04-09 13:28:10 +03007560}
7561
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007562static void g4x_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007563{
Chris Wilsonfac5e232016-07-04 11:34:36 +01007564 struct drm_i915_private *dev_priv = to_i915(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007565 uint32_t dspclk_gate;
7566
7567 I915_WRITE(RENCLK_GATE_D1, 0);
7568 I915_WRITE(RENCLK_GATE_D2, VF_UNIT_CLOCK_GATE_DISABLE |
7569 GS_UNIT_CLOCK_GATE_DISABLE |
7570 CL_UNIT_CLOCK_GATE_DISABLE);
7571 I915_WRITE(RAMCLK_GATE_D, 0);
7572 dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE |
7573 OVRUNIT_CLOCK_GATE_DISABLE |
7574 OVCUNIT_CLOCK_GATE_DISABLE;
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01007575 if (IS_GM45(dev_priv))
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007576 dspclk_gate |= DSSUNIT_CLOCK_GATE_DISABLE;
7577 I915_WRITE(DSPCLK_GATE_D, dspclk_gate);
Daniel Vetter4358a372012-10-18 11:49:51 +02007578
7579 /* WaDisableRenderCachePipelinedFlush */
7580 I915_WRITE(CACHE_MODE_0,
7581 _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
Ville Syrjäläde1aa622013-06-07 10:47:01 +03007582
Akash Goel4e046322014-04-04 17:14:38 +05307583 /* WaDisable_RenderCache_OperationalFlush:g4x */
7584 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
7585
Ville Syrjälä0e088b82013-06-07 10:47:04 +03007586 g4x_disable_trickle_feed(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007587}
7588
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007589static void crestline_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007590{
Chris Wilsonfac5e232016-07-04 11:34:36 +01007591 struct drm_i915_private *dev_priv = to_i915(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007592
7593 I915_WRITE(RENCLK_GATE_D1, I965_RCC_CLOCK_GATE_DISABLE);
7594 I915_WRITE(RENCLK_GATE_D2, 0);
7595 I915_WRITE(DSPCLK_GATE_D, 0);
7596 I915_WRITE(RAMCLK_GATE_D, 0);
7597 I915_WRITE16(DEUC, 0);
Ville Syrjälä20f94962013-06-07 10:47:02 +03007598 I915_WRITE(MI_ARB_STATE,
7599 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Akash Goel4e046322014-04-04 17:14:38 +05307600
7601 /* WaDisable_RenderCache_OperationalFlush:gen4 */
7602 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007603}
7604
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007605static void broadwater_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007606{
Chris Wilsonfac5e232016-07-04 11:34:36 +01007607 struct drm_i915_private *dev_priv = to_i915(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007608
7609 I915_WRITE(RENCLK_GATE_D1, I965_RCZ_CLOCK_GATE_DISABLE |
7610 I965_RCC_CLOCK_GATE_DISABLE |
7611 I965_RCPB_CLOCK_GATE_DISABLE |
7612 I965_ISC_CLOCK_GATE_DISABLE |
7613 I965_FBC_CLOCK_GATE_DISABLE);
7614 I915_WRITE(RENCLK_GATE_D2, 0);
Ville Syrjälä20f94962013-06-07 10:47:02 +03007615 I915_WRITE(MI_ARB_STATE,
7616 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Akash Goel4e046322014-04-04 17:14:38 +05307617
7618 /* WaDisable_RenderCache_OperationalFlush:gen4 */
7619 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007620}
7621
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007622static void gen3_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007623{
Chris Wilsonfac5e232016-07-04 11:34:36 +01007624 struct drm_i915_private *dev_priv = to_i915(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007625 u32 dstate = I915_READ(D_STATE);
7626
7627 dstate |= DSTATE_PLL_D3_OFF | DSTATE_GFX_CLOCK_GATING |
7628 DSTATE_DOT_CLOCK_GATING;
7629 I915_WRITE(D_STATE, dstate);
Chris Wilson13a86b82012-04-24 14:51:43 +01007630
7631 if (IS_PINEVIEW(dev))
7632 I915_WRITE(ECOSKPD, _MASKED_BIT_ENABLE(ECO_GATING_CX_ONLY));
Daniel Vetter974a3b02012-09-09 11:54:16 +02007633
7634 /* IIR "flip pending" means done if this bit is set */
7635 I915_WRITE(ECOSKPD, _MASKED_BIT_DISABLE(ECO_FLIP_DONE));
Ville Syrjälä12fabbcb92014-02-25 15:13:38 +02007636
7637 /* interrupts should cause a wake up from C3 */
Ville Syrjälä32992542014-02-25 15:13:39 +02007638 I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_AGPBUSY_INT_EN));
Ville Syrjälädbb42742014-02-25 15:13:41 +02007639
7640 /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
7641 I915_WRITE(MI_ARB_STATE, _MASKED_BIT_ENABLE(MI_ARB_C3_LP_WRITE_ENABLE));
Ville Syrjälä10383922014-08-15 01:21:54 +03007642
7643 I915_WRITE(MI_ARB_STATE,
7644 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007645}
7646
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007647static void i85x_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007648{
Chris Wilsonfac5e232016-07-04 11:34:36 +01007649 struct drm_i915_private *dev_priv = to_i915(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007650
7651 I915_WRITE(RENCLK_GATE_D1, SV_CLOCK_GATE_DISABLE);
Ville Syrjälä54e472a2014-02-25 15:13:40 +02007652
7653 /* interrupts should cause a wake up from C3 */
7654 I915_WRITE(MI_STATE, _MASKED_BIT_ENABLE(MI_AGPBUSY_INT_EN) |
7655 _MASKED_BIT_DISABLE(MI_AGPBUSY_830_MODE));
Ville Syrjälä10383922014-08-15 01:21:54 +03007656
7657 I915_WRITE(MEM_MODE,
7658 _MASKED_BIT_ENABLE(MEM_DISPLAY_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007659}
7660
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007661static void i830_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007662{
Chris Wilsonfac5e232016-07-04 11:34:36 +01007663 struct drm_i915_private *dev_priv = to_i915(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007664
7665 I915_WRITE(DSPCLK_GATE_D, OVRUNIT_CLOCK_GATE_DISABLE);
Ville Syrjälä10383922014-08-15 01:21:54 +03007666
7667 I915_WRITE(MEM_MODE,
7668 _MASKED_BIT_ENABLE(MEM_DISPLAY_A_TRICKLE_FEED_DISABLE) |
7669 _MASKED_BIT_ENABLE(MEM_DISPLAY_B_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007670}
7671
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007672void intel_init_clock_gating(struct drm_device *dev)
7673{
Chris Wilsonfac5e232016-07-04 11:34:36 +01007674 struct drm_i915_private *dev_priv = to_i915(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007675
Imre Deakbb400da2016-03-16 13:38:54 +02007676 dev_priv->display.init_clock_gating(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007677}
7678
Imre Deak7d708ee2013-04-17 14:04:50 +03007679void intel_suspend_hw(struct drm_device *dev)
7680{
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01007681 if (HAS_PCH_LPT(to_i915(dev)))
Imre Deak7d708ee2013-04-17 14:04:50 +03007682 lpt_suspend_hw(dev);
7683}
7684
Imre Deakbb400da2016-03-16 13:38:54 +02007685static void nop_init_clock_gating(struct drm_device *dev)
7686{
7687 DRM_DEBUG_KMS("No clock gating settings or workarounds applied.\n");
7688}
7689
7690/**
7691 * intel_init_clock_gating_hooks - setup the clock gating hooks
7692 * @dev_priv: device private
7693 *
7694 * Setup the hooks that configure which clocks of a given platform can be
7695 * gated and also apply various GT and display specific workarounds for these
7696 * platforms. Note that some GT specific workarounds are applied separately
7697 * when GPU contexts or batchbuffers start their execution.
7698 */
7699void intel_init_clock_gating_hooks(struct drm_i915_private *dev_priv)
7700{
7701 if (IS_SKYLAKE(dev_priv))
Daniel Vetterdc00b6a2016-05-19 09:14:20 +02007702 dev_priv->display.init_clock_gating = skylake_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02007703 else if (IS_KABYLAKE(dev_priv))
Mika Kuoppala9498dba2016-06-07 17:19:01 +03007704 dev_priv->display.init_clock_gating = kabylake_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02007705 else if (IS_BROXTON(dev_priv))
7706 dev_priv->display.init_clock_gating = bxt_init_clock_gating;
7707 else if (IS_BROADWELL(dev_priv))
7708 dev_priv->display.init_clock_gating = broadwell_init_clock_gating;
7709 else if (IS_CHERRYVIEW(dev_priv))
7710 dev_priv->display.init_clock_gating = cherryview_init_clock_gating;
7711 else if (IS_HASWELL(dev_priv))
7712 dev_priv->display.init_clock_gating = haswell_init_clock_gating;
7713 else if (IS_IVYBRIDGE(dev_priv))
7714 dev_priv->display.init_clock_gating = ivybridge_init_clock_gating;
7715 else if (IS_VALLEYVIEW(dev_priv))
7716 dev_priv->display.init_clock_gating = valleyview_init_clock_gating;
7717 else if (IS_GEN6(dev_priv))
7718 dev_priv->display.init_clock_gating = gen6_init_clock_gating;
7719 else if (IS_GEN5(dev_priv))
7720 dev_priv->display.init_clock_gating = ironlake_init_clock_gating;
7721 else if (IS_G4X(dev_priv))
7722 dev_priv->display.init_clock_gating = g4x_init_clock_gating;
7723 else if (IS_CRESTLINE(dev_priv))
7724 dev_priv->display.init_clock_gating = crestline_init_clock_gating;
7725 else if (IS_BROADWATER(dev_priv))
7726 dev_priv->display.init_clock_gating = broadwater_init_clock_gating;
7727 else if (IS_GEN3(dev_priv))
7728 dev_priv->display.init_clock_gating = gen3_init_clock_gating;
7729 else if (IS_I85X(dev_priv) || IS_I865G(dev_priv))
7730 dev_priv->display.init_clock_gating = i85x_init_clock_gating;
7731 else if (IS_GEN2(dev_priv))
7732 dev_priv->display.init_clock_gating = i830_init_clock_gating;
7733 else {
7734 MISSING_CASE(INTEL_DEVID(dev_priv));
7735 dev_priv->display.init_clock_gating = nop_init_clock_gating;
7736 }
7737}
7738
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007739/* Set up chip specific power management-related functions */
7740void intel_init_pm(struct drm_device *dev)
7741{
Chris Wilsonfac5e232016-07-04 11:34:36 +01007742 struct drm_i915_private *dev_priv = to_i915(dev);
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007743
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02007744 intel_fbc_init(dev_priv);
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007745
Daniel Vetterc921aba2012-04-26 23:28:17 +02007746 /* For cxsr */
7747 if (IS_PINEVIEW(dev))
7748 i915_pineview_get_mem_freq(dev);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01007749 else if (IS_GEN5(dev_priv))
Daniel Vetterc921aba2012-04-26 23:28:17 +02007750 i915_ironlake_get_mem_freq(dev);
7751
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007752 /* For FIFO watermark updates */
Damien Lespiauf5ed50c2014-11-13 17:51:52 +00007753 if (INTEL_INFO(dev)->gen >= 9) {
Pradeep Bhat2af30a52014-11-04 17:06:38 +00007754 skl_setup_wm_latency(dev);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00007755 dev_priv->display.update_wm = skl_update_wm;
Matt Roper98d39492016-05-12 07:06:03 -07007756 dev_priv->display.compute_global_watermarks = skl_compute_wm;
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01007757 } else if (HAS_PCH_SPLIT(dev_priv)) {
Damien Lespiaufa50ad62014-03-17 18:01:16 +00007758 ilk_setup_wm_latency(dev);
Ville Syrjälä53615a52013-08-01 16:18:50 +03007759
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01007760 if ((IS_GEN5(dev_priv) && dev_priv->wm.pri_latency[1] &&
Ville Syrjäläbd6025442014-01-07 16:14:10 +02007761 dev_priv->wm.spr_latency[1] && dev_priv->wm.cur_latency[1]) ||
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01007762 (!IS_GEN5(dev_priv) && dev_priv->wm.pri_latency[0] &&
Ville Syrjäläbd6025442014-01-07 16:14:10 +02007763 dev_priv->wm.spr_latency[0] && dev_priv->wm.cur_latency[0])) {
Matt Roper86c8bbb2015-09-24 15:53:16 -07007764 dev_priv->display.compute_pipe_wm = ilk_compute_pipe_wm;
Matt Ropered4a6a72016-02-23 17:20:13 -08007765 dev_priv->display.compute_intermediate_wm =
7766 ilk_compute_intermediate_wm;
7767 dev_priv->display.initial_watermarks =
7768 ilk_initial_watermarks;
7769 dev_priv->display.optimize_watermarks =
7770 ilk_optimize_watermarks;
Ville Syrjäläbd6025442014-01-07 16:14:10 +02007771 } else {
7772 DRM_DEBUG_KMS("Failed to read display plane latency. "
7773 "Disable CxSR\n");
7774 }
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +01007775 } else if (IS_CHERRYVIEW(dev_priv)) {
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03007776 vlv_setup_wm_latency(dev);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03007777 dev_priv->display.update_wm = vlv_update_wm;
Tvrtko Ursulin11a914c2016-10-13 11:03:08 +01007778 } else if (IS_VALLEYVIEW(dev_priv)) {
Ville Syrjälä26e1fe42015-06-24 22:00:06 +03007779 vlv_setup_wm_latency(dev);
Ville Syrjälä26e1fe42015-06-24 22:00:06 +03007780 dev_priv->display.update_wm = vlv_update_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007781 } else if (IS_PINEVIEW(dev)) {
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01007782 if (!intel_get_cxsr_latency(IS_PINEVIEW_G(dev_priv),
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007783 dev_priv->is_ddr3,
7784 dev_priv->fsb_freq,
7785 dev_priv->mem_freq)) {
7786 DRM_INFO("failed to find known CxSR latency "
7787 "(found ddr%s fsb freq %d, mem freq %d), "
7788 "disabling CxSR\n",
7789 (dev_priv->is_ddr3 == 1) ? "3" : "2",
7790 dev_priv->fsb_freq, dev_priv->mem_freq);
7791 /* Disable CxSR and never update its watermark again */
Imre Deak5209b1f2014-07-01 12:36:17 +03007792 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007793 dev_priv->display.update_wm = NULL;
7794 } else
7795 dev_priv->display.update_wm = pineview_update_wm;
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +01007796 } else if (IS_G4X(dev_priv)) {
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007797 dev_priv->display.update_wm = g4x_update_wm;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01007798 } else if (IS_GEN4(dev_priv)) {
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007799 dev_priv->display.update_wm = i965_update_wm;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01007800 } else if (IS_GEN3(dev_priv)) {
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007801 dev_priv->display.update_wm = i9xx_update_wm;
7802 dev_priv->display.get_fifo_size = i9xx_get_fifo_size;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01007803 } else if (IS_GEN2(dev_priv)) {
Daniel Vetterfeb56b92013-12-14 20:38:30 -02007804 if (INTEL_INFO(dev)->num_pipes == 1) {
7805 dev_priv->display.update_wm = i845_update_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007806 dev_priv->display.get_fifo_size = i845_get_fifo_size;
Daniel Vetterfeb56b92013-12-14 20:38:30 -02007807 } else {
7808 dev_priv->display.update_wm = i9xx_update_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007809 dev_priv->display.get_fifo_size = i830_get_fifo_size;
Daniel Vetterfeb56b92013-12-14 20:38:30 -02007810 }
Daniel Vetterfeb56b92013-12-14 20:38:30 -02007811 } else {
7812 DRM_ERROR("unexpected fall-through in intel_init_pm\n");
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007813 }
7814}
7815
Lyude87660502016-08-17 15:55:53 -04007816static inline int gen6_check_mailbox_status(struct drm_i915_private *dev_priv)
7817{
7818 uint32_t flags =
7819 I915_READ_FW(GEN6_PCODE_MAILBOX) & GEN6_PCODE_ERROR_MASK;
7820
7821 switch (flags) {
7822 case GEN6_PCODE_SUCCESS:
7823 return 0;
7824 case GEN6_PCODE_UNIMPLEMENTED_CMD:
7825 case GEN6_PCODE_ILLEGAL_CMD:
7826 return -ENXIO;
7827 case GEN6_PCODE_MIN_FREQ_TABLE_GT_RATIO_OUT_OF_RANGE:
Chris Wilson7850d1c2016-08-26 11:59:26 +01007828 case GEN7_PCODE_MIN_FREQ_TABLE_GT_RATIO_OUT_OF_RANGE:
Lyude87660502016-08-17 15:55:53 -04007829 return -EOVERFLOW;
7830 case GEN6_PCODE_TIMEOUT:
7831 return -ETIMEDOUT;
7832 default:
7833 MISSING_CASE(flags)
7834 return 0;
7835 }
7836}
7837
7838static inline int gen7_check_mailbox_status(struct drm_i915_private *dev_priv)
7839{
7840 uint32_t flags =
7841 I915_READ_FW(GEN6_PCODE_MAILBOX) & GEN6_PCODE_ERROR_MASK;
7842
7843 switch (flags) {
7844 case GEN6_PCODE_SUCCESS:
7845 return 0;
7846 case GEN6_PCODE_ILLEGAL_CMD:
7847 return -ENXIO;
7848 case GEN7_PCODE_TIMEOUT:
7849 return -ETIMEDOUT;
7850 case GEN7_PCODE_ILLEGAL_DATA:
7851 return -EINVAL;
7852 case GEN7_PCODE_MIN_FREQ_TABLE_GT_RATIO_OUT_OF_RANGE:
7853 return -EOVERFLOW;
7854 default:
7855 MISSING_CASE(flags);
7856 return 0;
7857 }
7858}
7859
Tom O'Rourke151a49d2014-11-13 18:50:10 -08007860int sandybridge_pcode_read(struct drm_i915_private *dev_priv, u32 mbox, u32 *val)
Ben Widawsky42c05262012-09-26 10:34:00 -07007861{
Lyude87660502016-08-17 15:55:53 -04007862 int status;
7863
Jesse Barnes4fc688c2012-11-02 11:14:01 -07007864 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Ben Widawsky42c05262012-09-26 10:34:00 -07007865
Chris Wilson3f5582d2016-06-30 15:32:45 +01007866 /* GEN6_PCODE_* are outside of the forcewake domain, we can
7867 * use te fw I915_READ variants to reduce the amount of work
7868 * required when reading/writing.
7869 */
7870
7871 if (I915_READ_FW(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) {
Ben Widawsky42c05262012-09-26 10:34:00 -07007872 DRM_DEBUG_DRIVER("warning: pcode (read) mailbox access failed\n");
7873 return -EAGAIN;
7874 }
7875
Chris Wilson3f5582d2016-06-30 15:32:45 +01007876 I915_WRITE_FW(GEN6_PCODE_DATA, *val);
7877 I915_WRITE_FW(GEN6_PCODE_DATA1, 0);
7878 I915_WRITE_FW(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY | mbox);
Ben Widawsky42c05262012-09-26 10:34:00 -07007879
Chris Wilson3f5582d2016-06-30 15:32:45 +01007880 if (intel_wait_for_register_fw(dev_priv,
7881 GEN6_PCODE_MAILBOX, GEN6_PCODE_READY, 0,
7882 500)) {
Ben Widawsky42c05262012-09-26 10:34:00 -07007883 DRM_ERROR("timeout waiting for pcode read (%d) to finish\n", mbox);
7884 return -ETIMEDOUT;
7885 }
7886
Chris Wilson3f5582d2016-06-30 15:32:45 +01007887 *val = I915_READ_FW(GEN6_PCODE_DATA);
7888 I915_WRITE_FW(GEN6_PCODE_DATA, 0);
Ben Widawsky42c05262012-09-26 10:34:00 -07007889
Lyude87660502016-08-17 15:55:53 -04007890 if (INTEL_GEN(dev_priv) > 6)
7891 status = gen7_check_mailbox_status(dev_priv);
7892 else
7893 status = gen6_check_mailbox_status(dev_priv);
7894
7895 if (status) {
7896 DRM_DEBUG_DRIVER("warning: pcode (read) mailbox access failed: %d\n",
7897 status);
7898 return status;
7899 }
7900
Ben Widawsky42c05262012-09-26 10:34:00 -07007901 return 0;
7902}
7903
Chris Wilson3f5582d2016-06-30 15:32:45 +01007904int sandybridge_pcode_write(struct drm_i915_private *dev_priv,
Lyude87660502016-08-17 15:55:53 -04007905 u32 mbox, u32 val)
Ben Widawsky42c05262012-09-26 10:34:00 -07007906{
Lyude87660502016-08-17 15:55:53 -04007907 int status;
7908
Jesse Barnes4fc688c2012-11-02 11:14:01 -07007909 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Ben Widawsky42c05262012-09-26 10:34:00 -07007910
Chris Wilson3f5582d2016-06-30 15:32:45 +01007911 /* GEN6_PCODE_* are outside of the forcewake domain, we can
7912 * use te fw I915_READ variants to reduce the amount of work
7913 * required when reading/writing.
7914 */
7915
7916 if (I915_READ_FW(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) {
Ben Widawsky42c05262012-09-26 10:34:00 -07007917 DRM_DEBUG_DRIVER("warning: pcode (write) mailbox access failed\n");
7918 return -EAGAIN;
7919 }
7920
Chris Wilson3f5582d2016-06-30 15:32:45 +01007921 I915_WRITE_FW(GEN6_PCODE_DATA, val);
7922 I915_WRITE_FW(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY | mbox);
Ben Widawsky42c05262012-09-26 10:34:00 -07007923
Chris Wilson3f5582d2016-06-30 15:32:45 +01007924 if (intel_wait_for_register_fw(dev_priv,
7925 GEN6_PCODE_MAILBOX, GEN6_PCODE_READY, 0,
7926 500)) {
Ben Widawsky42c05262012-09-26 10:34:00 -07007927 DRM_ERROR("timeout waiting for pcode write (%d) to finish\n", mbox);
7928 return -ETIMEDOUT;
7929 }
7930
Chris Wilson3f5582d2016-06-30 15:32:45 +01007931 I915_WRITE_FW(GEN6_PCODE_DATA, 0);
Ben Widawsky42c05262012-09-26 10:34:00 -07007932
Lyude87660502016-08-17 15:55:53 -04007933 if (INTEL_GEN(dev_priv) > 6)
7934 status = gen7_check_mailbox_status(dev_priv);
7935 else
7936 status = gen6_check_mailbox_status(dev_priv);
7937
7938 if (status) {
7939 DRM_DEBUG_DRIVER("warning: pcode (write) mailbox access failed: %d\n",
7940 status);
7941 return status;
7942 }
7943
Ben Widawsky42c05262012-09-26 10:34:00 -07007944 return 0;
7945}
Jesse Barnesa0e4e192013-04-02 11:23:05 -07007946
Ville Syrjälädd06f882014-11-10 22:55:12 +02007947static int byt_gpu_freq(struct drm_i915_private *dev_priv, int val)
7948{
Ville Syrjäläc30fec62016-03-04 21:43:02 +02007949 /*
7950 * N = val - 0xb7
7951 * Slow = Fast = GPLL ref * N
7952 */
7953 return DIV_ROUND_CLOSEST(dev_priv->rps.gpll_ref_freq * (val - 0xb7), 1000);
Jesse Barnes855ba3b2013-04-17 15:54:57 -07007954}
7955
Fengguang Wub55dd642014-07-12 11:21:39 +02007956static int byt_freq_opcode(struct drm_i915_private *dev_priv, int val)
Jesse Barnes855ba3b2013-04-17 15:54:57 -07007957{
Ville Syrjäläc30fec62016-03-04 21:43:02 +02007958 return DIV_ROUND_CLOSEST(1000 * val, dev_priv->rps.gpll_ref_freq) + 0xb7;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07007959}
7960
Fengguang Wub55dd642014-07-12 11:21:39 +02007961static int chv_gpu_freq(struct drm_i915_private *dev_priv, int val)
Deepak S22b1b2f2014-07-12 14:54:33 +05307962{
Ville Syrjäläc30fec62016-03-04 21:43:02 +02007963 /*
7964 * N = val / 2
7965 * CU (slow) = CU2x (fast) / 2 = GPLL ref * N / 2
7966 */
7967 return DIV_ROUND_CLOSEST(dev_priv->rps.gpll_ref_freq * val, 2 * 2 * 1000);
Deepak S22b1b2f2014-07-12 14:54:33 +05307968}
7969
Fengguang Wub55dd642014-07-12 11:21:39 +02007970static int chv_freq_opcode(struct drm_i915_private *dev_priv, int val)
Deepak S22b1b2f2014-07-12 14:54:33 +05307971{
Ville Syrjälä1c147622014-08-18 14:42:43 +03007972 /* CHV needs even values */
Ville Syrjäläc30fec62016-03-04 21:43:02 +02007973 return DIV_ROUND_CLOSEST(2 * 1000 * val, dev_priv->rps.gpll_ref_freq) * 2;
Deepak S22b1b2f2014-07-12 14:54:33 +05307974}
7975
Ville Syrjälä616bc822015-01-23 21:04:25 +02007976int intel_gpu_freq(struct drm_i915_private *dev_priv, int val)
7977{
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03007978 if (IS_GEN9(dev_priv))
Mika Kuoppala500a3d22015-11-13 19:29:41 +02007979 return DIV_ROUND_CLOSEST(val * GT_FREQUENCY_MULTIPLIER,
7980 GEN9_FREQ_SCALER);
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03007981 else if (IS_CHERRYVIEW(dev_priv))
Ville Syrjälä616bc822015-01-23 21:04:25 +02007982 return chv_gpu_freq(dev_priv, val);
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03007983 else if (IS_VALLEYVIEW(dev_priv))
Ville Syrjälä616bc822015-01-23 21:04:25 +02007984 return byt_gpu_freq(dev_priv, val);
7985 else
7986 return val * GT_FREQUENCY_MULTIPLIER;
7987}
7988
Ville Syrjälä616bc822015-01-23 21:04:25 +02007989int intel_freq_opcode(struct drm_i915_private *dev_priv, int val)
7990{
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03007991 if (IS_GEN9(dev_priv))
Mika Kuoppala500a3d22015-11-13 19:29:41 +02007992 return DIV_ROUND_CLOSEST(val * GEN9_FREQ_SCALER,
7993 GT_FREQUENCY_MULTIPLIER);
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03007994 else if (IS_CHERRYVIEW(dev_priv))
Ville Syrjälä616bc822015-01-23 21:04:25 +02007995 return chv_freq_opcode(dev_priv, val);
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03007996 else if (IS_VALLEYVIEW(dev_priv))
Ville Syrjälä616bc822015-01-23 21:04:25 +02007997 return byt_freq_opcode(dev_priv, val);
7998 else
Mika Kuoppala500a3d22015-11-13 19:29:41 +02007999 return DIV_ROUND_CLOSEST(val, GT_FREQUENCY_MULTIPLIER);
Deepak S22b1b2f2014-07-12 14:54:33 +05308000}
8001
Chris Wilson6ad790c2015-04-07 16:20:31 +01008002struct request_boost {
8003 struct work_struct work;
Daniel Vettereed29a52015-05-21 14:21:25 +02008004 struct drm_i915_gem_request *req;
Chris Wilson6ad790c2015-04-07 16:20:31 +01008005};
8006
8007static void __intel_rps_boost_work(struct work_struct *work)
8008{
8009 struct request_boost *boost = container_of(work, struct request_boost, work);
Chris Wilsone61b9952015-04-27 13:41:24 +01008010 struct drm_i915_gem_request *req = boost->req;
Chris Wilson6ad790c2015-04-07 16:20:31 +01008011
Chris Wilsonf69a02c2016-07-01 17:23:16 +01008012 if (!i915_gem_request_completed(req))
Chris Wilsonc0336662016-05-06 15:40:21 +01008013 gen6_rps_boost(req->i915, NULL, req->emitted_jiffies);
Chris Wilson6ad790c2015-04-07 16:20:31 +01008014
Chris Wilsone8a261e2016-07-20 13:31:49 +01008015 i915_gem_request_put(req);
Chris Wilson6ad790c2015-04-07 16:20:31 +01008016 kfree(boost);
8017}
8018
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01008019void intel_queue_rps_boost_for_request(struct drm_i915_gem_request *req)
Chris Wilson6ad790c2015-04-07 16:20:31 +01008020{
8021 struct request_boost *boost;
8022
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01008023 if (req == NULL || INTEL_GEN(req->i915) < 6)
Chris Wilson6ad790c2015-04-07 16:20:31 +01008024 return;
8025
Chris Wilsonf69a02c2016-07-01 17:23:16 +01008026 if (i915_gem_request_completed(req))
Chris Wilsone61b9952015-04-27 13:41:24 +01008027 return;
8028
Chris Wilson6ad790c2015-04-07 16:20:31 +01008029 boost = kmalloc(sizeof(*boost), GFP_ATOMIC);
8030 if (boost == NULL)
8031 return;
8032
Chris Wilsone8a261e2016-07-20 13:31:49 +01008033 boost->req = i915_gem_request_get(req);
Chris Wilson6ad790c2015-04-07 16:20:31 +01008034
8035 INIT_WORK(&boost->work, __intel_rps_boost_work);
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01008036 queue_work(req->i915->wq, &boost->work);
Chris Wilson6ad790c2015-04-07 16:20:31 +01008037}
8038
Daniel Vetterf742a552013-12-06 10:17:53 +01008039void intel_pm_setup(struct drm_device *dev)
Chris Wilson907b28c2013-07-19 20:36:52 +01008040{
Chris Wilsonfac5e232016-07-04 11:34:36 +01008041 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilson907b28c2013-07-19 20:36:52 +01008042
Daniel Vetterf742a552013-12-06 10:17:53 +01008043 mutex_init(&dev_priv->rps.hw_lock);
Chris Wilson8d3afd72015-05-21 21:01:47 +01008044 spin_lock_init(&dev_priv->rps.client_lock);
Daniel Vetterf742a552013-12-06 10:17:53 +01008045
Chris Wilson54b4f682016-07-21 21:16:19 +01008046 INIT_DELAYED_WORK(&dev_priv->rps.autoenable_work,
8047 __intel_autoenable_gt_powersave);
Chris Wilson1854d5c2015-04-07 16:20:32 +01008048 INIT_LIST_HEAD(&dev_priv->rps.clients);
Paulo Zanoni5d584b22014-03-07 20:08:15 -03008049
Paulo Zanoni33688d92014-03-07 20:08:19 -03008050 dev_priv->pm.suspended = false;
Imre Deak1f814da2015-12-16 02:52:19 +02008051 atomic_set(&dev_priv->pm.wakeref_count, 0);
Chris Wilson907b28c2013-07-19 20:36:52 +01008052}