blob: daa99e7e805b96197d57f5149a0ec831627b0ff7 [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>
Eugeni Dodonov85208be2012-04-16 22:20:34 -030029#include "i915_drv.h"
30#include "intel_drv.h"
Daniel Vettereb48eb02012-04-26 23:28:12 +020031#include "../../../platform/x86/intel_ips.h"
32#include <linux/module.h>
Eugeni Dodonov85208be2012-04-16 22:20:34 -030033
Ben Widawskydc39fff2013-10-18 12:32:07 -070034/**
35 * RC6 is a special power stage which allows the GPU to enter an very
36 * low-voltage mode when idle, using down to 0V while at this stage. This
37 * stage is entered automatically when the GPU is idle when RC6 support is
38 * enabled, and as soon as new workload arises GPU wakes up automatically as well.
39 *
40 * There are different RC6 modes available in Intel GPU, which differentiate
41 * among each other with the latency required to enter and leave RC6 and
42 * voltage consumed by the GPU in different states.
43 *
44 * The combination of the following flags define which states GPU is allowed
45 * to enter, while RC6 is the normal RC6 state, RC6p is the deep RC6, and
46 * RC6pp is deepest RC6. Their support by hardware varies according to the
47 * GPU, BIOS, chipset and platform. RC6 is usually the safest one and the one
48 * which brings the most power savings; deeper states save more power, but
49 * require higher latency to switch to and wake up.
50 */
51#define INTEL_RC6_ENABLE (1<<0)
52#define INTEL_RC6p_ENABLE (1<<1)
53#define INTEL_RC6pp_ENABLE (1<<2)
54
Eugeni Dodonovf6750b32012-04-18 11:51:14 -030055/* FBC, or Frame Buffer Compression, is a technique employed to compress the
56 * framebuffer contents in-memory, aiming at reducing the required bandwidth
57 * during in-memory transfers and, therefore, reduce the power packet.
Eugeni Dodonov85208be2012-04-16 22:20:34 -030058 *
Eugeni Dodonovf6750b32012-04-18 11:51:14 -030059 * The benefits of FBC are mostly visible with solid backgrounds and
60 * variation-less patterns.
Eugeni Dodonov85208be2012-04-16 22:20:34 -030061 *
Eugeni Dodonovf6750b32012-04-18 11:51:14 -030062 * FBC-related functionality can be enabled by the means of the
63 * i915.i915_enable_fbc parameter
Eugeni Dodonov85208be2012-04-16 22:20:34 -030064 */
65
Damien Lespiauda2078c2013-02-13 15:27:27 +000066static void gen9_init_clock_gating(struct drm_device *dev)
67{
Damien Lespiauacd5c342014-03-26 16:55:46 +000068 struct drm_i915_private *dev_priv = dev->dev_private;
69
70 /*
71 * WaDisableSDEUnitClockGating:skl
72 * This seems to be a pre-production w/a.
73 */
74 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
75 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Damien Lespiau91e41d12014-03-26 17:42:50 +000076
Damien Lespiau3ca5da42014-03-26 18:18:01 +000077 /*
78 * WaDisableDgMirrorFixInHalfSliceChicken5:skl
79 * This is a pre-production w/a.
80 */
81 I915_WRITE(GEN9_HALF_SLICE_CHICKEN5,
82 I915_READ(GEN9_HALF_SLICE_CHICKEN5) &
83 ~GEN9_DG_MIRROR_FIX_ENABLE);
84
Damien Lespiau91e41d12014-03-26 17:42:50 +000085 /* Wa4x4STCOptimizationDisable:skl */
86 I915_WRITE(CACHE_MODE_1,
87 _MASKED_BIT_ENABLE(GEN8_4x4_STC_OPTIMIZATION_DISABLE));
Damien Lespiauda2078c2013-02-13 15:27:27 +000088}
89
Eugeni Dodonov1fa61102012-04-18 15:29:26 -030090static void i8xx_disable_fbc(struct drm_device *dev)
Eugeni Dodonov85208be2012-04-16 22:20:34 -030091{
92 struct drm_i915_private *dev_priv = dev->dev_private;
93 u32 fbc_ctl;
94
Paulo Zanoni9adccc62014-09-19 16:04:55 -030095 dev_priv->fbc.enabled = false;
96
Eugeni Dodonov85208be2012-04-16 22:20:34 -030097 /* Disable compression */
98 fbc_ctl = I915_READ(FBC_CONTROL);
99 if ((fbc_ctl & FBC_CTL_EN) == 0)
100 return;
101
102 fbc_ctl &= ~FBC_CTL_EN;
103 I915_WRITE(FBC_CONTROL, fbc_ctl);
104
105 /* Wait for compressing bit to clear */
106 if (wait_for((I915_READ(FBC_STATUS) & FBC_STAT_COMPRESSING) == 0, 10)) {
107 DRM_DEBUG_KMS("FBC idle timed out\n");
108 return;
109 }
110
111 DRM_DEBUG_KMS("disabled FBC\n");
112}
113
Ville Syrjälä993495a2013-12-12 17:27:40 +0200114static void i8xx_enable_fbc(struct drm_crtc *crtc)
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300115{
116 struct drm_device *dev = crtc->dev;
117 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roperf4510a22014-04-01 15:22:40 -0700118 struct drm_framebuffer *fb = crtc->primary->fb;
Matt Roper2ff8fde2014-07-08 07:50:07 -0700119 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300120 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
121 int cfb_pitch;
Ville Syrjälä7f2cf222014-01-23 16:49:11 +0200122 int i;
Ville Syrjälä159f9872013-11-28 17:29:57 +0200123 u32 fbc_ctl;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300124
Paulo Zanoni9adccc62014-09-19 16:04:55 -0300125 dev_priv->fbc.enabled = true;
126
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -0700127 cfb_pitch = dev_priv->fbc.size / FBC_LL_SIZE;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300128 if (fb->pitches[0] < cfb_pitch)
129 cfb_pitch = fb->pitches[0];
130
Ville Syrjälä42a430f2013-11-28 17:29:56 +0200131 /* FBC_CTL wants 32B or 64B units */
132 if (IS_GEN2(dev))
133 cfb_pitch = (cfb_pitch / 32) - 1;
134 else
135 cfb_pitch = (cfb_pitch / 64) - 1;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300136
137 /* Clear old tags */
138 for (i = 0; i < (FBC_LL_SIZE / 32) + 1; i++)
139 I915_WRITE(FBC_TAG + (i * 4), 0);
140
Ville Syrjälä159f9872013-11-28 17:29:57 +0200141 if (IS_GEN4(dev)) {
142 u32 fbc_ctl2;
143
144 /* Set it up... */
145 fbc_ctl2 = FBC_CTL_FENCE_DBL | FBC_CTL_IDLE_IMM | FBC_CTL_CPU_FENCE;
Ville Syrjälä7f2cf222014-01-23 16:49:11 +0200146 fbc_ctl2 |= FBC_CTL_PLANE(intel_crtc->plane);
Ville Syrjälä159f9872013-11-28 17:29:57 +0200147 I915_WRITE(FBC_CONTROL2, fbc_ctl2);
148 I915_WRITE(FBC_FENCE_OFF, crtc->y);
149 }
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300150
151 /* enable it... */
Ville Syrjälä993495a2013-12-12 17:27:40 +0200152 fbc_ctl = I915_READ(FBC_CONTROL);
153 fbc_ctl &= 0x3fff << FBC_CTL_INTERVAL_SHIFT;
154 fbc_ctl |= FBC_CTL_EN | FBC_CTL_PERIODIC;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300155 if (IS_I945GM(dev))
156 fbc_ctl |= FBC_CTL_C3_IDLE; /* 945 needs special SR handling */
157 fbc_ctl |= (cfb_pitch & 0xff) << FBC_CTL_STRIDE_SHIFT;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300158 fbc_ctl |= obj->fence_reg;
159 I915_WRITE(FBC_CONTROL, fbc_ctl);
160
Ville Syrjälä5cd54102014-01-23 16:49:16 +0200161 DRM_DEBUG_KMS("enabled FBC, pitch %d, yoff %d, plane %c\n",
Ville Syrjälä84f44ce2013-04-17 17:48:49 +0300162 cfb_pitch, crtc->y, plane_name(intel_crtc->plane));
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300163}
164
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300165static bool i8xx_fbc_enabled(struct drm_device *dev)
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300166{
167 struct drm_i915_private *dev_priv = dev->dev_private;
168
169 return I915_READ(FBC_CONTROL) & FBC_CTL_EN;
170}
171
Ville Syrjälä993495a2013-12-12 17:27:40 +0200172static void g4x_enable_fbc(struct drm_crtc *crtc)
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300173{
174 struct drm_device *dev = crtc->dev;
175 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roperf4510a22014-04-01 15:22:40 -0700176 struct drm_framebuffer *fb = crtc->primary->fb;
Matt Roper2ff8fde2014-07-08 07:50:07 -0700177 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300178 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300179 u32 dpfc_ctl;
180
Paulo Zanoni9adccc62014-09-19 16:04:55 -0300181 dev_priv->fbc.enabled = true;
182
Ville Syrjälä3fa2e0e2014-01-23 16:49:12 +0200183 dpfc_ctl = DPFC_CTL_PLANE(intel_crtc->plane) | DPFC_SR_EN;
184 if (drm_format_plane_cpp(fb->pixel_format, 0) == 2)
185 dpfc_ctl |= DPFC_CTL_LIMIT_2X;
186 else
187 dpfc_ctl |= DPFC_CTL_LIMIT_1X;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300188 dpfc_ctl |= DPFC_CTL_FENCE_EN | obj->fence_reg;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300189
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300190 I915_WRITE(DPFC_FENCE_YOFF, crtc->y);
191
192 /* enable it... */
Ville Syrjäläfe74c1a2014-01-23 16:49:13 +0200193 I915_WRITE(DPFC_CONTROL, dpfc_ctl | DPFC_CTL_EN);
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300194
Ville Syrjälä84f44ce2013-04-17 17:48:49 +0300195 DRM_DEBUG_KMS("enabled fbc on plane %c\n", plane_name(intel_crtc->plane));
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300196}
197
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300198static void g4x_disable_fbc(struct drm_device *dev)
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300199{
200 struct drm_i915_private *dev_priv = dev->dev_private;
201 u32 dpfc_ctl;
202
Paulo Zanoni9adccc62014-09-19 16:04:55 -0300203 dev_priv->fbc.enabled = false;
204
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300205 /* Disable compression */
206 dpfc_ctl = I915_READ(DPFC_CONTROL);
207 if (dpfc_ctl & DPFC_CTL_EN) {
208 dpfc_ctl &= ~DPFC_CTL_EN;
209 I915_WRITE(DPFC_CONTROL, dpfc_ctl);
210
211 DRM_DEBUG_KMS("disabled FBC\n");
212 }
213}
214
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300215static bool g4x_fbc_enabled(struct drm_device *dev)
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300216{
217 struct drm_i915_private *dev_priv = dev->dev_private;
218
219 return I915_READ(DPFC_CONTROL) & DPFC_CTL_EN;
220}
221
222static void sandybridge_blit_fbc_update(struct drm_device *dev)
223{
224 struct drm_i915_private *dev_priv = dev->dev_private;
225 u32 blt_ecoskpd;
226
227 /* Make sure blitter notifies FBC of writes */
Deepak S940aece2013-11-23 14:55:43 +0530228
229 /* Blitter is part of Media powerwell on VLV. No impact of
230 * his param in other platforms for now */
231 gen6_gt_force_wake_get(dev_priv, FORCEWAKE_MEDIA);
Deepak Sc8d9a592013-11-23 14:55:42 +0530232
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300233 blt_ecoskpd = I915_READ(GEN6_BLITTER_ECOSKPD);
234 blt_ecoskpd |= GEN6_BLITTER_FBC_NOTIFY <<
235 GEN6_BLITTER_LOCK_SHIFT;
236 I915_WRITE(GEN6_BLITTER_ECOSKPD, blt_ecoskpd);
237 blt_ecoskpd |= GEN6_BLITTER_FBC_NOTIFY;
238 I915_WRITE(GEN6_BLITTER_ECOSKPD, blt_ecoskpd);
239 blt_ecoskpd &= ~(GEN6_BLITTER_FBC_NOTIFY <<
240 GEN6_BLITTER_LOCK_SHIFT);
241 I915_WRITE(GEN6_BLITTER_ECOSKPD, blt_ecoskpd);
242 POSTING_READ(GEN6_BLITTER_ECOSKPD);
Deepak Sc8d9a592013-11-23 14:55:42 +0530243
Deepak S940aece2013-11-23 14:55:43 +0530244 gen6_gt_force_wake_put(dev_priv, FORCEWAKE_MEDIA);
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300245}
246
Ville Syrjälä993495a2013-12-12 17:27:40 +0200247static void ironlake_enable_fbc(struct drm_crtc *crtc)
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300248{
249 struct drm_device *dev = crtc->dev;
250 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roperf4510a22014-04-01 15:22:40 -0700251 struct drm_framebuffer *fb = crtc->primary->fb;
Matt Roper2ff8fde2014-07-08 07:50:07 -0700252 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300253 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300254 u32 dpfc_ctl;
255
Paulo Zanoni9adccc62014-09-19 16:04:55 -0300256 dev_priv->fbc.enabled = true;
257
Ville Syrjälä46f3dab2014-01-23 16:49:14 +0200258 dpfc_ctl = DPFC_CTL_PLANE(intel_crtc->plane);
Ville Syrjälä3fa2e0e2014-01-23 16:49:12 +0200259 if (drm_format_plane_cpp(fb->pixel_format, 0) == 2)
Ben Widawsky5e59f712014-06-30 10:41:24 -0700260 dev_priv->fbc.threshold++;
261
262 switch (dev_priv->fbc.threshold) {
263 case 4:
264 case 3:
265 dpfc_ctl |= DPFC_CTL_LIMIT_4X;
266 break;
267 case 2:
Ville Syrjälä3fa2e0e2014-01-23 16:49:12 +0200268 dpfc_ctl |= DPFC_CTL_LIMIT_2X;
Ben Widawsky5e59f712014-06-30 10:41:24 -0700269 break;
270 case 1:
Ville Syrjälä3fa2e0e2014-01-23 16:49:12 +0200271 dpfc_ctl |= DPFC_CTL_LIMIT_1X;
Ben Widawsky5e59f712014-06-30 10:41:24 -0700272 break;
273 }
Ville Syrjäläd6293362013-11-21 21:29:45 +0200274 dpfc_ctl |= DPFC_CTL_FENCE_EN;
275 if (IS_GEN5(dev))
276 dpfc_ctl |= obj->fence_reg;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300277
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300278 I915_WRITE(ILK_DPFC_FENCE_YOFF, crtc->y);
Ben Widawskyf343c5f2013-07-05 14:41:04 -0700279 I915_WRITE(ILK_FBC_RT_BASE, i915_gem_obj_ggtt_offset(obj) | ILK_FBC_RT_VALID);
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300280 /* enable it... */
281 I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl | DPFC_CTL_EN);
282
283 if (IS_GEN6(dev)) {
284 I915_WRITE(SNB_DPFC_CTL_SA,
285 SNB_CPU_FENCE_ENABLE | obj->fence_reg);
286 I915_WRITE(DPFC_CPU_FENCE_OFFSET, crtc->y);
287 sandybridge_blit_fbc_update(dev);
288 }
289
Ville Syrjälä84f44ce2013-04-17 17:48:49 +0300290 DRM_DEBUG_KMS("enabled fbc on plane %c\n", plane_name(intel_crtc->plane));
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300291}
292
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300293static void ironlake_disable_fbc(struct drm_device *dev)
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300294{
295 struct drm_i915_private *dev_priv = dev->dev_private;
296 u32 dpfc_ctl;
297
Paulo Zanoni9adccc62014-09-19 16:04:55 -0300298 dev_priv->fbc.enabled = false;
299
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300300 /* Disable compression */
301 dpfc_ctl = I915_READ(ILK_DPFC_CONTROL);
302 if (dpfc_ctl & DPFC_CTL_EN) {
303 dpfc_ctl &= ~DPFC_CTL_EN;
304 I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl);
305
306 DRM_DEBUG_KMS("disabled FBC\n");
307 }
308}
309
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300310static bool ironlake_fbc_enabled(struct drm_device *dev)
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300311{
312 struct drm_i915_private *dev_priv = dev->dev_private;
313
314 return I915_READ(ILK_DPFC_CONTROL) & DPFC_CTL_EN;
315}
316
Ville Syrjälä993495a2013-12-12 17:27:40 +0200317static void gen7_enable_fbc(struct drm_crtc *crtc)
Rodrigo Viviabe959c2013-05-06 19:37:33 -0300318{
319 struct drm_device *dev = crtc->dev;
320 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roperf4510a22014-04-01 15:22:40 -0700321 struct drm_framebuffer *fb = crtc->primary->fb;
Matt Roper2ff8fde2014-07-08 07:50:07 -0700322 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Rodrigo Viviabe959c2013-05-06 19:37:33 -0300323 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjälä3fa2e0e2014-01-23 16:49:12 +0200324 u32 dpfc_ctl;
Rodrigo Viviabe959c2013-05-06 19:37:33 -0300325
Paulo Zanoni9adccc62014-09-19 16:04:55 -0300326 dev_priv->fbc.enabled = true;
327
Ville Syrjälä3fa2e0e2014-01-23 16:49:12 +0200328 dpfc_ctl = IVB_DPFC_CTL_PLANE(intel_crtc->plane);
329 if (drm_format_plane_cpp(fb->pixel_format, 0) == 2)
Ben Widawsky5e59f712014-06-30 10:41:24 -0700330 dev_priv->fbc.threshold++;
331
332 switch (dev_priv->fbc.threshold) {
333 case 4:
334 case 3:
335 dpfc_ctl |= DPFC_CTL_LIMIT_4X;
336 break;
337 case 2:
Ville Syrjälä3fa2e0e2014-01-23 16:49:12 +0200338 dpfc_ctl |= DPFC_CTL_LIMIT_2X;
Ben Widawsky5e59f712014-06-30 10:41:24 -0700339 break;
340 case 1:
Ville Syrjälä3fa2e0e2014-01-23 16:49:12 +0200341 dpfc_ctl |= DPFC_CTL_LIMIT_1X;
Ben Widawsky5e59f712014-06-30 10:41:24 -0700342 break;
343 }
344
Ville Syrjälä3fa2e0e2014-01-23 16:49:12 +0200345 dpfc_ctl |= IVB_DPFC_CTL_FENCE_EN;
346
Rodrigo Vivida46f932014-08-01 02:04:45 -0700347 if (dev_priv->fbc.false_color)
348 dpfc_ctl |= FBC_CTL_FALSE_COLOR;
349
Ville Syrjälä3fa2e0e2014-01-23 16:49:12 +0200350 I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl | DPFC_CTL_EN);
Rodrigo Viviabe959c2013-05-06 19:37:33 -0300351
Rodrigo Vivi891348b2013-05-06 19:37:36 -0300352 if (IS_IVYBRIDGE(dev)) {
Damien Lespiau7dd23ba2013-05-10 14:33:17 +0100353 /* WaFbcAsynchFlipDisableFbcQueue:ivb */
Ville Syrjälä2adb6db2014-03-05 13:05:46 +0200354 I915_WRITE(ILK_DISPLAY_CHICKEN1,
355 I915_READ(ILK_DISPLAY_CHICKEN1) |
356 ILK_FBCQ_DIS);
Rodrigo Vivi28554162013-05-06 19:37:37 -0300357 } else {
Ville Syrjälä2adb6db2014-03-05 13:05:46 +0200358 /* WaFbcAsynchFlipDisableFbcQueue:hsw,bdw */
Ville Syrjälä8f670bb2014-03-05 13:05:47 +0200359 I915_WRITE(CHICKEN_PIPESL_1(intel_crtc->pipe),
360 I915_READ(CHICKEN_PIPESL_1(intel_crtc->pipe)) |
361 HSW_FBCQ_DIS);
Rodrigo Vivi891348b2013-05-06 19:37:36 -0300362 }
Rodrigo Vivib74ea102013-05-09 14:08:38 -0300363
Rodrigo Viviabe959c2013-05-06 19:37:33 -0300364 I915_WRITE(SNB_DPFC_CTL_SA,
365 SNB_CPU_FENCE_ENABLE | obj->fence_reg);
366 I915_WRITE(DPFC_CPU_FENCE_OFFSET, crtc->y);
367
368 sandybridge_blit_fbc_update(dev);
369
Ville Syrjäläb19870e2013-11-06 23:02:25 +0200370 DRM_DEBUG_KMS("enabled fbc on plane %c\n", plane_name(intel_crtc->plane));
Rodrigo Viviabe959c2013-05-06 19:37:33 -0300371}
372
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300373bool intel_fbc_enabled(struct drm_device *dev)
374{
375 struct drm_i915_private *dev_priv = dev->dev_private;
376
Paulo Zanoni9adccc62014-09-19 16:04:55 -0300377 return dev_priv->fbc.enabled;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300378}
379
Rodrigo Vivi1d73c2a2014-09-24 19:50:59 -0400380void bdw_fbc_sw_flush(struct drm_device *dev, u32 value)
Rodrigo Vivic5ad0112014-08-04 03:51:38 -0700381{
382 struct drm_i915_private *dev_priv = dev->dev_private;
383
384 if (!IS_GEN8(dev))
385 return;
386
Rodrigo Vivi01d06e92014-09-05 16:57:20 -0400387 if (!intel_fbc_enabled(dev))
388 return;
389
Rodrigo Vivic5ad0112014-08-04 03:51:38 -0700390 I915_WRITE(MSG_FBC_REND_STATE, value);
391}
392
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300393static void intel_fbc_work_fn(struct work_struct *__work)
394{
395 struct intel_fbc_work *work =
396 container_of(to_delayed_work(__work),
397 struct intel_fbc_work, work);
398 struct drm_device *dev = work->crtc->dev;
399 struct drm_i915_private *dev_priv = dev->dev_private;
400
401 mutex_lock(&dev->struct_mutex);
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -0700402 if (work == dev_priv->fbc.fbc_work) {
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300403 /* Double check that we haven't switched fb without cancelling
404 * the prior work.
405 */
Matt Roperf4510a22014-04-01 15:22:40 -0700406 if (work->crtc->primary->fb == work->fb) {
Ville Syrjälä993495a2013-12-12 17:27:40 +0200407 dev_priv->display.enable_fbc(work->crtc);
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300408
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -0700409 dev_priv->fbc.plane = to_intel_crtc(work->crtc)->plane;
Matt Roperf4510a22014-04-01 15:22:40 -0700410 dev_priv->fbc.fb_id = work->crtc->primary->fb->base.id;
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -0700411 dev_priv->fbc.y = work->crtc->y;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300412 }
413
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -0700414 dev_priv->fbc.fbc_work = NULL;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300415 }
416 mutex_unlock(&dev->struct_mutex);
417
418 kfree(work);
419}
420
421static void intel_cancel_fbc_work(struct drm_i915_private *dev_priv)
422{
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -0700423 if (dev_priv->fbc.fbc_work == NULL)
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300424 return;
425
426 DRM_DEBUG_KMS("cancelling pending FBC enable\n");
427
428 /* Synchronisation is provided by struct_mutex and checking of
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -0700429 * dev_priv->fbc.fbc_work, so we can perform the cancellation
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300430 * entirely asynchronously.
431 */
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -0700432 if (cancel_delayed_work(&dev_priv->fbc.fbc_work->work))
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300433 /* tasklet was killed before being run, clean up */
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -0700434 kfree(dev_priv->fbc.fbc_work);
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300435
436 /* Mark the work as no longer wanted so that if it does
437 * wake-up (because the work was already running and waiting
438 * for our mutex), it will discover that is no longer
439 * necessary to run.
440 */
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -0700441 dev_priv->fbc.fbc_work = NULL;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300442}
443
Ville Syrjälä993495a2013-12-12 17:27:40 +0200444static void intel_enable_fbc(struct drm_crtc *crtc)
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300445{
446 struct intel_fbc_work *work;
447 struct drm_device *dev = crtc->dev;
448 struct drm_i915_private *dev_priv = dev->dev_private;
449
450 if (!dev_priv->display.enable_fbc)
451 return;
452
453 intel_cancel_fbc_work(dev_priv);
454
Daniel Vetterb14c5672013-09-19 12:18:32 +0200455 work = kzalloc(sizeof(*work), GFP_KERNEL);
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300456 if (work == NULL) {
Paulo Zanoni6cdcb5e2013-06-12 17:27:29 -0300457 DRM_ERROR("Failed to allocate FBC work structure\n");
Ville Syrjälä993495a2013-12-12 17:27:40 +0200458 dev_priv->display.enable_fbc(crtc);
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300459 return;
460 }
461
462 work->crtc = crtc;
Matt Roperf4510a22014-04-01 15:22:40 -0700463 work->fb = crtc->primary->fb;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300464 INIT_DELAYED_WORK(&work->work, intel_fbc_work_fn);
465
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -0700466 dev_priv->fbc.fbc_work = work;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300467
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300468 /* Delay the actual enabling to let pageflipping cease and the
469 * display to settle before starting the compression. Note that
470 * this delay also serves a second purpose: it allows for a
471 * vblank to pass after disabling the FBC before we attempt
472 * to modify the control registers.
473 *
474 * A more complicated solution would involve tracking vblanks
475 * following the termination of the page-flipping sequence
476 * and indeed performing the enable as a co-routine and not
477 * waiting synchronously upon the vblank.
Damien Lespiau7457d612013-06-07 17:41:07 +0100478 *
479 * WaFbcWaitForVBlankBeforeEnable:ilk,snb
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300480 */
481 schedule_delayed_work(&work->work, msecs_to_jiffies(50));
482}
483
484void intel_disable_fbc(struct drm_device *dev)
485{
486 struct drm_i915_private *dev_priv = dev->dev_private;
487
488 intel_cancel_fbc_work(dev_priv);
489
490 if (!dev_priv->display.disable_fbc)
491 return;
492
493 dev_priv->display.disable_fbc(dev);
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -0700494 dev_priv->fbc.plane = -1;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300495}
496
Chris Wilson29ebf902013-07-27 17:23:55 +0100497static bool set_no_fbc_reason(struct drm_i915_private *dev_priv,
498 enum no_fbc_reason reason)
499{
500 if (dev_priv->fbc.no_fbc_reason == reason)
501 return false;
502
503 dev_priv->fbc.no_fbc_reason = reason;
504 return true;
505}
506
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300507/**
508 * intel_update_fbc - enable/disable FBC as needed
509 * @dev: the drm_device
510 *
511 * Set up the framebuffer compression hardware at mode set time. We
512 * enable it if possible:
513 * - plane A only (on pre-965)
514 * - no pixel mulitply/line duplication
515 * - no alpha buffer discard
516 * - no dual wide
Paulo Zanonif85da862013-06-04 16:53:39 -0300517 * - framebuffer <= max_hdisplay in width, max_vdisplay in height
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300518 *
519 * We can't assume that any compression will take place (worst case),
520 * so the compressed buffer has to be the same size as the uncompressed
521 * one. It also must reside (along with the line length buffer) in
522 * stolen memory.
523 *
524 * We need to enable/disable FBC on a global basis.
525 */
526void intel_update_fbc(struct drm_device *dev)
527{
528 struct drm_i915_private *dev_priv = dev->dev_private;
529 struct drm_crtc *crtc = NULL, *tmp_crtc;
530 struct intel_crtc *intel_crtc;
531 struct drm_framebuffer *fb;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300532 struct drm_i915_gem_object *obj;
Ville Syrjäläef644fd2013-09-04 18:25:21 +0300533 const struct drm_display_mode *adjusted_mode;
Ville Syrjälä37327ab2013-09-04 18:25:28 +0300534 unsigned int max_width, max_height;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300535
Daniel Vetter3a77c4c2014-01-10 08:50:12 +0100536 if (!HAS_FBC(dev)) {
Chris Wilson29ebf902013-07-27 17:23:55 +0100537 set_no_fbc_reason(dev_priv, FBC_UNSUPPORTED);
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300538 return;
Chris Wilson29ebf902013-07-27 17:23:55 +0100539 }
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300540
Jani Nikulad330a952014-01-21 11:24:25 +0200541 if (!i915.powersave) {
Chris Wilson29ebf902013-07-27 17:23:55 +0100542 if (set_no_fbc_reason(dev_priv, FBC_MODULE_PARAM))
543 DRM_DEBUG_KMS("fbc disabled per module param\n");
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300544 return;
Chris Wilson29ebf902013-07-27 17:23:55 +0100545 }
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300546
547 /*
548 * If FBC is already on, we just have to verify that we can
549 * keep it that way...
550 * Need to disable if:
551 * - more than one pipe is active
552 * - changing FBC params (stride, fence, mode)
553 * - new fb is too large to fit in compressed buffer
554 * - going to an unsupported config (interlace, pixel multiply, etc.)
555 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +0100556 for_each_crtc(dev, tmp_crtc) {
Chris Wilson3490ea52013-01-07 10:11:40 +0000557 if (intel_crtc_active(tmp_crtc) &&
Ville Syrjälä4c445e02013-10-09 17:24:58 +0300558 to_intel_crtc(tmp_crtc)->primary_enabled) {
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300559 if (crtc) {
Chris Wilson29ebf902013-07-27 17:23:55 +0100560 if (set_no_fbc_reason(dev_priv, FBC_MULTIPLE_PIPES))
561 DRM_DEBUG_KMS("more than one pipe active, disabling compression\n");
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300562 goto out_disable;
563 }
564 crtc = tmp_crtc;
565 }
566 }
567
Matt Roperf4510a22014-04-01 15:22:40 -0700568 if (!crtc || crtc->primary->fb == NULL) {
Chris Wilson29ebf902013-07-27 17:23:55 +0100569 if (set_no_fbc_reason(dev_priv, FBC_NO_OUTPUT))
570 DRM_DEBUG_KMS("no output, disabling\n");
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300571 goto out_disable;
572 }
573
574 intel_crtc = to_intel_crtc(crtc);
Matt Roperf4510a22014-04-01 15:22:40 -0700575 fb = crtc->primary->fb;
Matt Roper2ff8fde2014-07-08 07:50:07 -0700576 obj = intel_fb_obj(fb);
Ville Syrjäläef644fd2013-09-04 18:25:21 +0300577 adjusted_mode = &intel_crtc->config.adjusted_mode;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300578
Chris Wilson03689202014-06-06 10:37:11 +0100579 if (i915.enable_fbc < 0) {
Chris Wilson29ebf902013-07-27 17:23:55 +0100580 if (set_no_fbc_reason(dev_priv, FBC_CHIP_DEFAULT))
581 DRM_DEBUG_KMS("disabled per chip default\n");
Damien Lespiau8a5729a2013-06-24 16:22:02 +0100582 goto out_disable;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300583 }
Jani Nikulad330a952014-01-21 11:24:25 +0200584 if (!i915.enable_fbc) {
Chris Wilson29ebf902013-07-27 17:23:55 +0100585 if (set_no_fbc_reason(dev_priv, FBC_MODULE_PARAM))
586 DRM_DEBUG_KMS("fbc disabled per module param\n");
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300587 goto out_disable;
588 }
Ville Syrjäläef644fd2013-09-04 18:25:21 +0300589 if ((adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) ||
590 (adjusted_mode->flags & DRM_MODE_FLAG_DBLSCAN)) {
Chris Wilson29ebf902013-07-27 17:23:55 +0100591 if (set_no_fbc_reason(dev_priv, FBC_UNSUPPORTED_MODE))
592 DRM_DEBUG_KMS("mode incompatible with compression, "
593 "disabling\n");
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300594 goto out_disable;
595 }
Paulo Zanonif85da862013-06-04 16:53:39 -0300596
Daisy Sun032843a2014-06-16 15:48:18 -0700597 if (INTEL_INFO(dev)->gen >= 8 || IS_HASWELL(dev)) {
598 max_width = 4096;
599 max_height = 4096;
600 } else if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
Ville Syrjälä37327ab2013-09-04 18:25:28 +0300601 max_width = 4096;
602 max_height = 2048;
Paulo Zanonif85da862013-06-04 16:53:39 -0300603 } else {
Ville Syrjälä37327ab2013-09-04 18:25:28 +0300604 max_width = 2048;
605 max_height = 1536;
Paulo Zanonif85da862013-06-04 16:53:39 -0300606 }
Ville Syrjälä37327ab2013-09-04 18:25:28 +0300607 if (intel_crtc->config.pipe_src_w > max_width ||
608 intel_crtc->config.pipe_src_h > max_height) {
Chris Wilson29ebf902013-07-27 17:23:55 +0100609 if (set_no_fbc_reason(dev_priv, FBC_MODE_TOO_LARGE))
610 DRM_DEBUG_KMS("mode too large for compression, disabling\n");
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300611 goto out_disable;
612 }
Ben Widawsky8f94d242014-02-20 16:01:20 -0800613 if ((INTEL_INFO(dev)->gen < 4 || HAS_DDI(dev)) &&
Ville Syrjäläc5a44aa2013-11-28 17:29:58 +0200614 intel_crtc->plane != PLANE_A) {
Chris Wilson29ebf902013-07-27 17:23:55 +0100615 if (set_no_fbc_reason(dev_priv, FBC_BAD_PLANE))
Ville Syrjäläc5a44aa2013-11-28 17:29:58 +0200616 DRM_DEBUG_KMS("plane not A, disabling compression\n");
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300617 goto out_disable;
618 }
619
620 /* The use of a CPU fence is mandatory in order to detect writes
621 * by the CPU to the scanout and trigger updates to the FBC.
622 */
623 if (obj->tiling_mode != I915_TILING_X ||
624 obj->fence_reg == I915_FENCE_REG_NONE) {
Chris Wilson29ebf902013-07-27 17:23:55 +0100625 if (set_no_fbc_reason(dev_priv, FBC_NOT_TILED))
626 DRM_DEBUG_KMS("framebuffer not tiled or fenced, disabling compression\n");
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300627 goto out_disable;
628 }
Sonika Jindal48404c12014-08-22 14:06:04 +0530629 if (INTEL_INFO(dev)->gen <= 4 && !IS_G4X(dev) &&
630 to_intel_plane(crtc->primary)->rotation != BIT(DRM_ROTATE_0)) {
631 if (set_no_fbc_reason(dev_priv, FBC_UNSUPPORTED_MODE))
632 DRM_DEBUG_KMS("Rotation unsupported, disabling\n");
633 goto out_disable;
634 }
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300635
636 /* If the kernel debugger is active, always disable compression */
637 if (in_dbg_master())
638 goto out_disable;
639
Matt Roper2ff8fde2014-07-08 07:50:07 -0700640 if (i915_gem_stolen_setup_compression(dev, obj->base.size,
Ben Widawsky5e59f712014-06-30 10:41:24 -0700641 drm_format_plane_cpp(fb->pixel_format, 0))) {
Chris Wilson29ebf902013-07-27 17:23:55 +0100642 if (set_no_fbc_reason(dev_priv, FBC_STOLEN_TOO_SMALL))
643 DRM_DEBUG_KMS("framebuffer too large, disabling compression\n");
Chris Wilson11be49e2012-11-15 11:32:20 +0000644 goto out_disable;
645 }
646
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300647 /* If the scanout has not changed, don't modify the FBC settings.
648 * Note that we make the fundamental assumption that the fb->obj
649 * cannot be unpinned (and have its GTT offset and fence revoked)
650 * without first being decoupled from the scanout and FBC disabled.
651 */
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -0700652 if (dev_priv->fbc.plane == intel_crtc->plane &&
653 dev_priv->fbc.fb_id == fb->base.id &&
654 dev_priv->fbc.y == crtc->y)
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300655 return;
656
657 if (intel_fbc_enabled(dev)) {
658 /* We update FBC along two paths, after changing fb/crtc
659 * configuration (modeswitching) and after page-flipping
660 * finishes. For the latter, we know that not only did
661 * we disable the FBC at the start of the page-flip
662 * sequence, but also more than one vblank has passed.
663 *
664 * For the former case of modeswitching, it is possible
665 * to switch between two FBC valid configurations
666 * instantaneously so we do need to disable the FBC
667 * before we can modify its control registers. We also
668 * have to wait for the next vblank for that to take
669 * effect. However, since we delay enabling FBC we can
670 * assume that a vblank has passed since disabling and
671 * that we can safely alter the registers in the deferred
672 * callback.
673 *
674 * In the scenario that we go from a valid to invalid
675 * and then back to valid FBC configuration we have
676 * no strict enforcement that a vblank occurred since
677 * disabling the FBC. However, along all current pipe
678 * disabling paths we do need to wait for a vblank at
679 * some point. And we wait before enabling FBC anyway.
680 */
681 DRM_DEBUG_KMS("disabling active FBC for update\n");
682 intel_disable_fbc(dev);
683 }
684
Ville Syrjälä993495a2013-12-12 17:27:40 +0200685 intel_enable_fbc(crtc);
Chris Wilson29ebf902013-07-27 17:23:55 +0100686 dev_priv->fbc.no_fbc_reason = FBC_OK;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300687 return;
688
689out_disable:
690 /* Multiple disables should be harmless */
691 if (intel_fbc_enabled(dev)) {
692 DRM_DEBUG_KMS("unsupported config, disabling FBC\n");
693 intel_disable_fbc(dev);
694 }
Chris Wilson11be49e2012-11-15 11:32:20 +0000695 i915_gem_stolen_cleanup_compression(dev);
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300696}
697
Daniel Vetterc921aba2012-04-26 23:28:17 +0200698static void i915_pineview_get_mem_freq(struct drm_device *dev)
699{
Jani Nikula50227e12014-03-31 14:27:21 +0300700 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200701 u32 tmp;
702
703 tmp = I915_READ(CLKCFG);
704
705 switch (tmp & CLKCFG_FSB_MASK) {
706 case CLKCFG_FSB_533:
707 dev_priv->fsb_freq = 533; /* 133*4 */
708 break;
709 case CLKCFG_FSB_800:
710 dev_priv->fsb_freq = 800; /* 200*4 */
711 break;
712 case CLKCFG_FSB_667:
713 dev_priv->fsb_freq = 667; /* 167*4 */
714 break;
715 case CLKCFG_FSB_400:
716 dev_priv->fsb_freq = 400; /* 100*4 */
717 break;
718 }
719
720 switch (tmp & CLKCFG_MEM_MASK) {
721 case CLKCFG_MEM_533:
722 dev_priv->mem_freq = 533;
723 break;
724 case CLKCFG_MEM_667:
725 dev_priv->mem_freq = 667;
726 break;
727 case CLKCFG_MEM_800:
728 dev_priv->mem_freq = 800;
729 break;
730 }
731
732 /* detect pineview DDR3 setting */
733 tmp = I915_READ(CSHRDDR3CTL);
734 dev_priv->is_ddr3 = (tmp & CSHRDDR3CTL_DDR3) ? 1 : 0;
735}
736
737static void i915_ironlake_get_mem_freq(struct drm_device *dev)
738{
Jani Nikula50227e12014-03-31 14:27:21 +0300739 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200740 u16 ddrpll, csipll;
741
742 ddrpll = I915_READ16(DDRMPLL1);
743 csipll = I915_READ16(CSIPLL0);
744
745 switch (ddrpll & 0xff) {
746 case 0xc:
747 dev_priv->mem_freq = 800;
748 break;
749 case 0x10:
750 dev_priv->mem_freq = 1066;
751 break;
752 case 0x14:
753 dev_priv->mem_freq = 1333;
754 break;
755 case 0x18:
756 dev_priv->mem_freq = 1600;
757 break;
758 default:
759 DRM_DEBUG_DRIVER("unknown memory frequency 0x%02x\n",
760 ddrpll & 0xff);
761 dev_priv->mem_freq = 0;
762 break;
763 }
764
Daniel Vetter20e4d402012-08-08 23:35:39 +0200765 dev_priv->ips.r_t = dev_priv->mem_freq;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200766
767 switch (csipll & 0x3ff) {
768 case 0x00c:
769 dev_priv->fsb_freq = 3200;
770 break;
771 case 0x00e:
772 dev_priv->fsb_freq = 3733;
773 break;
774 case 0x010:
775 dev_priv->fsb_freq = 4266;
776 break;
777 case 0x012:
778 dev_priv->fsb_freq = 4800;
779 break;
780 case 0x014:
781 dev_priv->fsb_freq = 5333;
782 break;
783 case 0x016:
784 dev_priv->fsb_freq = 5866;
785 break;
786 case 0x018:
787 dev_priv->fsb_freq = 6400;
788 break;
789 default:
790 DRM_DEBUG_DRIVER("unknown fsb frequency 0x%04x\n",
791 csipll & 0x3ff);
792 dev_priv->fsb_freq = 0;
793 break;
794 }
795
796 if (dev_priv->fsb_freq == 3200) {
Daniel Vetter20e4d402012-08-08 23:35:39 +0200797 dev_priv->ips.c_m = 0;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200798 } else if (dev_priv->fsb_freq > 3200 && dev_priv->fsb_freq <= 4800) {
Daniel Vetter20e4d402012-08-08 23:35:39 +0200799 dev_priv->ips.c_m = 1;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200800 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +0200801 dev_priv->ips.c_m = 2;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200802 }
803}
804
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300805static const struct cxsr_latency cxsr_latency_table[] = {
806 {1, 0, 800, 400, 3382, 33382, 3983, 33983}, /* DDR2-400 SC */
807 {1, 0, 800, 667, 3354, 33354, 3807, 33807}, /* DDR2-667 SC */
808 {1, 0, 800, 800, 3347, 33347, 3763, 33763}, /* DDR2-800 SC */
809 {1, 1, 800, 667, 6420, 36420, 6873, 36873}, /* DDR3-667 SC */
810 {1, 1, 800, 800, 5902, 35902, 6318, 36318}, /* DDR3-800 SC */
811
812 {1, 0, 667, 400, 3400, 33400, 4021, 34021}, /* DDR2-400 SC */
813 {1, 0, 667, 667, 3372, 33372, 3845, 33845}, /* DDR2-667 SC */
814 {1, 0, 667, 800, 3386, 33386, 3822, 33822}, /* DDR2-800 SC */
815 {1, 1, 667, 667, 6438, 36438, 6911, 36911}, /* DDR3-667 SC */
816 {1, 1, 667, 800, 5941, 35941, 6377, 36377}, /* DDR3-800 SC */
817
818 {1, 0, 400, 400, 3472, 33472, 4173, 34173}, /* DDR2-400 SC */
819 {1, 0, 400, 667, 3443, 33443, 3996, 33996}, /* DDR2-667 SC */
820 {1, 0, 400, 800, 3430, 33430, 3946, 33946}, /* DDR2-800 SC */
821 {1, 1, 400, 667, 6509, 36509, 7062, 37062}, /* DDR3-667 SC */
822 {1, 1, 400, 800, 5985, 35985, 6501, 36501}, /* DDR3-800 SC */
823
824 {0, 0, 800, 400, 3438, 33438, 4065, 34065}, /* DDR2-400 SC */
825 {0, 0, 800, 667, 3410, 33410, 3889, 33889}, /* DDR2-667 SC */
826 {0, 0, 800, 800, 3403, 33403, 3845, 33845}, /* DDR2-800 SC */
827 {0, 1, 800, 667, 6476, 36476, 6955, 36955}, /* DDR3-667 SC */
828 {0, 1, 800, 800, 5958, 35958, 6400, 36400}, /* DDR3-800 SC */
829
830 {0, 0, 667, 400, 3456, 33456, 4103, 34106}, /* DDR2-400 SC */
831 {0, 0, 667, 667, 3428, 33428, 3927, 33927}, /* DDR2-667 SC */
832 {0, 0, 667, 800, 3443, 33443, 3905, 33905}, /* DDR2-800 SC */
833 {0, 1, 667, 667, 6494, 36494, 6993, 36993}, /* DDR3-667 SC */
834 {0, 1, 667, 800, 5998, 35998, 6460, 36460}, /* DDR3-800 SC */
835
836 {0, 0, 400, 400, 3528, 33528, 4255, 34255}, /* DDR2-400 SC */
837 {0, 0, 400, 667, 3500, 33500, 4079, 34079}, /* DDR2-667 SC */
838 {0, 0, 400, 800, 3487, 33487, 4029, 34029}, /* DDR2-800 SC */
839 {0, 1, 400, 667, 6566, 36566, 7145, 37145}, /* DDR3-667 SC */
840 {0, 1, 400, 800, 6042, 36042, 6584, 36584}, /* DDR3-800 SC */
841};
842
Daniel Vetter63c62272012-04-21 23:17:55 +0200843static const struct cxsr_latency *intel_get_cxsr_latency(int is_desktop,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300844 int is_ddr3,
845 int fsb,
846 int mem)
847{
848 const struct cxsr_latency *latency;
849 int i;
850
851 if (fsb == 0 || mem == 0)
852 return NULL;
853
854 for (i = 0; i < ARRAY_SIZE(cxsr_latency_table); i++) {
855 latency = &cxsr_latency_table[i];
856 if (is_desktop == latency->is_desktop &&
857 is_ddr3 == latency->is_ddr3 &&
858 fsb == latency->fsb_freq && mem == latency->mem_freq)
859 return latency;
860 }
861
862 DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
863
864 return NULL;
865}
866
Imre Deak5209b1f2014-07-01 12:36:17 +0300867void intel_set_memory_cxsr(struct drm_i915_private *dev_priv, bool enable)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300868{
Imre Deak5209b1f2014-07-01 12:36:17 +0300869 struct drm_device *dev = dev_priv->dev;
870 u32 val;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300871
Imre Deak5209b1f2014-07-01 12:36:17 +0300872 if (IS_VALLEYVIEW(dev)) {
873 I915_WRITE(FW_BLC_SELF_VLV, enable ? FW_CSPWRDWNEN : 0);
874 } else if (IS_G4X(dev) || IS_CRESTLINE(dev)) {
875 I915_WRITE(FW_BLC_SELF, enable ? FW_BLC_SELF_EN : 0);
876 } else if (IS_PINEVIEW(dev)) {
877 val = I915_READ(DSPFW3) & ~PINEVIEW_SELF_REFRESH_EN;
878 val |= enable ? PINEVIEW_SELF_REFRESH_EN : 0;
879 I915_WRITE(DSPFW3, val);
880 } else if (IS_I945G(dev) || IS_I945GM(dev)) {
881 val = enable ? _MASKED_BIT_ENABLE(FW_BLC_SELF_EN) :
882 _MASKED_BIT_DISABLE(FW_BLC_SELF_EN);
883 I915_WRITE(FW_BLC_SELF, val);
884 } else if (IS_I915GM(dev)) {
885 val = enable ? _MASKED_BIT_ENABLE(INSTPM_SELF_EN) :
886 _MASKED_BIT_DISABLE(INSTPM_SELF_EN);
887 I915_WRITE(INSTPM, val);
888 } else {
889 return;
890 }
891
892 DRM_DEBUG_KMS("memory self-refresh is %s\n",
893 enable ? "enabled" : "disabled");
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300894}
895
896/*
897 * Latency for FIFO fetches is dependent on several factors:
898 * - memory configuration (speed, channels)
899 * - chipset
900 * - current MCH state
901 * It can be fairly high in some situations, so here we assume a fairly
902 * pessimal value. It's a tradeoff between extra memory fetches (if we
903 * set this value too high, the FIFO will fetch frequently to stay full)
904 * and power consumption (set it too low to save power and we might see
905 * FIFO underruns and display "flicker").
906 *
907 * A value of 5us seems to be a good balance; safe for very low end
908 * platforms but not overly aggressive on lower latency configs.
909 */
Chris Wilson5aef6002014-09-03 11:56:07 +0100910static const int pessimal_latency_ns = 5000;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300911
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300912static int i9xx_get_fifo_size(struct drm_device *dev, int plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300913{
914 struct drm_i915_private *dev_priv = dev->dev_private;
915 uint32_t dsparb = I915_READ(DSPARB);
916 int size;
917
918 size = dsparb & 0x7f;
919 if (plane)
920 size = ((dsparb >> DSPARB_CSTART_SHIFT) & 0x7f) - size;
921
922 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
923 plane ? "B" : "A", size);
924
925 return size;
926}
927
Daniel Vetterfeb56b92013-12-14 20:38:30 -0200928static int i830_get_fifo_size(struct drm_device *dev, int plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300929{
930 struct drm_i915_private *dev_priv = dev->dev_private;
931 uint32_t dsparb = I915_READ(DSPARB);
932 int size;
933
934 size = dsparb & 0x1ff;
935 if (plane)
936 size = ((dsparb >> DSPARB_BEND_SHIFT) & 0x1ff) - size;
937 size >>= 1; /* Convert to cachelines */
938
939 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
940 plane ? "B" : "A", size);
941
942 return size;
943}
944
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300945static int i845_get_fifo_size(struct drm_device *dev, int plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300946{
947 struct drm_i915_private *dev_priv = dev->dev_private;
948 uint32_t dsparb = I915_READ(DSPARB);
949 int size;
950
951 size = dsparb & 0x7f;
952 size >>= 2; /* Convert to cachelines */
953
954 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
955 plane ? "B" : "A",
956 size);
957
958 return size;
959}
960
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300961/* Pineview has different values for various configs */
962static const struct intel_watermark_params pineview_display_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300963 .fifo_size = PINEVIEW_DISPLAY_FIFO,
964 .max_wm = PINEVIEW_MAX_WM,
965 .default_wm = PINEVIEW_DFT_WM,
966 .guard_size = PINEVIEW_GUARD_WM,
967 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300968};
969static const struct intel_watermark_params pineview_display_hplloff_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300970 .fifo_size = PINEVIEW_DISPLAY_FIFO,
971 .max_wm = PINEVIEW_MAX_WM,
972 .default_wm = PINEVIEW_DFT_HPLLOFF_WM,
973 .guard_size = PINEVIEW_GUARD_WM,
974 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300975};
976static const struct intel_watermark_params pineview_cursor_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300977 .fifo_size = PINEVIEW_CURSOR_FIFO,
978 .max_wm = PINEVIEW_CURSOR_MAX_WM,
979 .default_wm = PINEVIEW_CURSOR_DFT_WM,
980 .guard_size = PINEVIEW_CURSOR_GUARD_WM,
981 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300982};
983static const struct intel_watermark_params pineview_cursor_hplloff_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300984 .fifo_size = PINEVIEW_CURSOR_FIFO,
985 .max_wm = PINEVIEW_CURSOR_MAX_WM,
986 .default_wm = PINEVIEW_CURSOR_DFT_WM,
987 .guard_size = PINEVIEW_CURSOR_GUARD_WM,
988 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300989};
990static const struct intel_watermark_params g4x_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300991 .fifo_size = G4X_FIFO_SIZE,
992 .max_wm = G4X_MAX_WM,
993 .default_wm = G4X_MAX_WM,
994 .guard_size = 2,
995 .cacheline_size = G4X_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300996};
997static const struct intel_watermark_params g4x_cursor_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300998 .fifo_size = I965_CURSOR_FIFO,
999 .max_wm = I965_CURSOR_MAX_WM,
1000 .default_wm = I965_CURSOR_DFT_WM,
1001 .guard_size = 2,
1002 .cacheline_size = G4X_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001003};
1004static const struct intel_watermark_params valleyview_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +03001005 .fifo_size = VALLEYVIEW_FIFO_SIZE,
1006 .max_wm = VALLEYVIEW_MAX_WM,
1007 .default_wm = VALLEYVIEW_MAX_WM,
1008 .guard_size = 2,
1009 .cacheline_size = G4X_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001010};
1011static const struct intel_watermark_params valleyview_cursor_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +03001012 .fifo_size = I965_CURSOR_FIFO,
1013 .max_wm = VALLEYVIEW_CURSOR_MAX_WM,
1014 .default_wm = I965_CURSOR_DFT_WM,
1015 .guard_size = 2,
1016 .cacheline_size = G4X_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001017};
1018static const struct intel_watermark_params i965_cursor_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +03001019 .fifo_size = I965_CURSOR_FIFO,
1020 .max_wm = I965_CURSOR_MAX_WM,
1021 .default_wm = I965_CURSOR_DFT_WM,
1022 .guard_size = 2,
1023 .cacheline_size = I915_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001024};
1025static const struct intel_watermark_params i945_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +03001026 .fifo_size = I945_FIFO_SIZE,
1027 .max_wm = I915_MAX_WM,
1028 .default_wm = 1,
1029 .guard_size = 2,
1030 .cacheline_size = I915_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001031};
1032static const struct intel_watermark_params i915_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +03001033 .fifo_size = I915_FIFO_SIZE,
1034 .max_wm = I915_MAX_WM,
1035 .default_wm = 1,
1036 .guard_size = 2,
1037 .cacheline_size = I915_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001038};
Ville Syrjälä9d539102014-08-15 01:21:53 +03001039static const struct intel_watermark_params i830_a_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +03001040 .fifo_size = I855GM_FIFO_SIZE,
1041 .max_wm = I915_MAX_WM,
1042 .default_wm = 1,
1043 .guard_size = 2,
1044 .cacheline_size = I830_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001045};
Ville Syrjälä9d539102014-08-15 01:21:53 +03001046static const struct intel_watermark_params i830_bc_wm_info = {
1047 .fifo_size = I855GM_FIFO_SIZE,
1048 .max_wm = I915_MAX_WM/2,
1049 .default_wm = 1,
1050 .guard_size = 2,
1051 .cacheline_size = I830_FIFO_LINE_SIZE,
1052};
Daniel Vetterfeb56b92013-12-14 20:38:30 -02001053static const struct intel_watermark_params i845_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +03001054 .fifo_size = I830_FIFO_SIZE,
1055 .max_wm = I915_MAX_WM,
1056 .default_wm = 1,
1057 .guard_size = 2,
1058 .cacheline_size = I830_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001059};
1060
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001061/**
1062 * intel_calculate_wm - calculate watermark level
1063 * @clock_in_khz: pixel clock
1064 * @wm: chip FIFO params
1065 * @pixel_size: display pixel size
1066 * @latency_ns: memory latency for the platform
1067 *
1068 * Calculate the watermark level (the level at which the display plane will
1069 * start fetching from memory again). Each chip has a different display
1070 * FIFO size and allocation, so the caller needs to figure that out and pass
1071 * in the correct intel_watermark_params structure.
1072 *
1073 * As the pixel clock runs, the FIFO will be drained at a rate that depends
1074 * on the pixel size. When it reaches the watermark level, it'll start
1075 * fetching FIFO line sized based chunks from memory until the FIFO fills
1076 * past the watermark point. If the FIFO drains completely, a FIFO underrun
1077 * will occur, and a display engine hang could result.
1078 */
1079static unsigned long intel_calculate_wm(unsigned long clock_in_khz,
1080 const struct intel_watermark_params *wm,
1081 int fifo_size,
1082 int pixel_size,
1083 unsigned long latency_ns)
1084{
1085 long entries_required, wm_size;
1086
1087 /*
1088 * Note: we need to make sure we don't overflow for various clock &
1089 * latency values.
1090 * clocks go from a few thousand to several hundred thousand.
1091 * latency is usually a few thousand
1092 */
1093 entries_required = ((clock_in_khz / 1000) * pixel_size * latency_ns) /
1094 1000;
1095 entries_required = DIV_ROUND_UP(entries_required, wm->cacheline_size);
1096
1097 DRM_DEBUG_KMS("FIFO entries required for mode: %ld\n", entries_required);
1098
1099 wm_size = fifo_size - (entries_required + wm->guard_size);
1100
1101 DRM_DEBUG_KMS("FIFO watermark level: %ld\n", wm_size);
1102
1103 /* Don't promote wm_size to unsigned... */
1104 if (wm_size > (long)wm->max_wm)
1105 wm_size = wm->max_wm;
1106 if (wm_size <= 0)
1107 wm_size = wm->default_wm;
Ville Syrjäläd6feb192014-09-05 21:54:13 +03001108
1109 /*
1110 * Bspec seems to indicate that the value shouldn't be lower than
1111 * 'burst size + 1'. Certainly 830 is quite unhappy with low values.
1112 * Lets go for 8 which is the burst size since certain platforms
1113 * already use a hardcoded 8 (which is what the spec says should be
1114 * done).
1115 */
1116 if (wm_size <= 8)
1117 wm_size = 8;
1118
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001119 return wm_size;
1120}
1121
1122static struct drm_crtc *single_enabled_crtc(struct drm_device *dev)
1123{
1124 struct drm_crtc *crtc, *enabled = NULL;
1125
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01001126 for_each_crtc(dev, crtc) {
Chris Wilson3490ea52013-01-07 10:11:40 +00001127 if (intel_crtc_active(crtc)) {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001128 if (enabled)
1129 return NULL;
1130 enabled = crtc;
1131 }
1132 }
1133
1134 return enabled;
1135}
1136
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001137static void pineview_update_wm(struct drm_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001138{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001139 struct drm_device *dev = unused_crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001140 struct drm_i915_private *dev_priv = dev->dev_private;
1141 struct drm_crtc *crtc;
1142 const struct cxsr_latency *latency;
1143 u32 reg;
1144 unsigned long wm;
1145
1146 latency = intel_get_cxsr_latency(IS_PINEVIEW_G(dev), dev_priv->is_ddr3,
1147 dev_priv->fsb_freq, dev_priv->mem_freq);
1148 if (!latency) {
1149 DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
Imre Deak5209b1f2014-07-01 12:36:17 +03001150 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001151 return;
1152 }
1153
1154 crtc = single_enabled_crtc(dev);
1155 if (crtc) {
Damien Lespiau241bfc32013-09-25 16:45:37 +01001156 const struct drm_display_mode *adjusted_mode;
Matt Roperf4510a22014-04-01 15:22:40 -07001157 int pixel_size = crtc->primary->fb->bits_per_pixel / 8;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001158 int clock;
1159
1160 adjusted_mode = &to_intel_crtc(crtc)->config.adjusted_mode;
1161 clock = adjusted_mode->crtc_clock;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001162
1163 /* Display SR */
1164 wm = intel_calculate_wm(clock, &pineview_display_wm,
1165 pineview_display_wm.fifo_size,
1166 pixel_size, latency->display_sr);
1167 reg = I915_READ(DSPFW1);
1168 reg &= ~DSPFW_SR_MASK;
1169 reg |= wm << DSPFW_SR_SHIFT;
1170 I915_WRITE(DSPFW1, reg);
1171 DRM_DEBUG_KMS("DSPFW1 register is %x\n", reg);
1172
1173 /* cursor SR */
1174 wm = intel_calculate_wm(clock, &pineview_cursor_wm,
1175 pineview_display_wm.fifo_size,
1176 pixel_size, latency->cursor_sr);
1177 reg = I915_READ(DSPFW3);
1178 reg &= ~DSPFW_CURSOR_SR_MASK;
1179 reg |= (wm & 0x3f) << DSPFW_CURSOR_SR_SHIFT;
1180 I915_WRITE(DSPFW3, reg);
1181
1182 /* Display HPLL off SR */
1183 wm = intel_calculate_wm(clock, &pineview_display_hplloff_wm,
1184 pineview_display_hplloff_wm.fifo_size,
1185 pixel_size, latency->display_hpll_disable);
1186 reg = I915_READ(DSPFW3);
1187 reg &= ~DSPFW_HPLL_SR_MASK;
1188 reg |= wm & DSPFW_HPLL_SR_MASK;
1189 I915_WRITE(DSPFW3, reg);
1190
1191 /* cursor HPLL off SR */
1192 wm = intel_calculate_wm(clock, &pineview_cursor_hplloff_wm,
1193 pineview_display_hplloff_wm.fifo_size,
1194 pixel_size, latency->cursor_hpll_disable);
1195 reg = I915_READ(DSPFW3);
1196 reg &= ~DSPFW_HPLL_CURSOR_MASK;
1197 reg |= (wm & 0x3f) << DSPFW_HPLL_CURSOR_SHIFT;
1198 I915_WRITE(DSPFW3, reg);
1199 DRM_DEBUG_KMS("DSPFW3 register is %x\n", reg);
1200
Imre Deak5209b1f2014-07-01 12:36:17 +03001201 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001202 } else {
Imre Deak5209b1f2014-07-01 12:36:17 +03001203 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001204 }
1205}
1206
1207static bool g4x_compute_wm0(struct drm_device *dev,
1208 int plane,
1209 const struct intel_watermark_params *display,
1210 int display_latency_ns,
1211 const struct intel_watermark_params *cursor,
1212 int cursor_latency_ns,
1213 int *plane_wm,
1214 int *cursor_wm)
1215{
1216 struct drm_crtc *crtc;
Ville Syrjälä4fe85902013-09-04 18:25:22 +03001217 const struct drm_display_mode *adjusted_mode;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001218 int htotal, hdisplay, clock, pixel_size;
1219 int line_time_us, line_count;
1220 int entries, tlb_miss;
1221
1222 crtc = intel_get_crtc_for_plane(dev, plane);
Chris Wilson3490ea52013-01-07 10:11:40 +00001223 if (!intel_crtc_active(crtc)) {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001224 *cursor_wm = cursor->guard_size;
1225 *plane_wm = display->guard_size;
1226 return false;
1227 }
1228
Ville Syrjälä4fe85902013-09-04 18:25:22 +03001229 adjusted_mode = &to_intel_crtc(crtc)->config.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001230 clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -08001231 htotal = adjusted_mode->crtc_htotal;
Ville Syrjälä37327ab2013-09-04 18:25:28 +03001232 hdisplay = to_intel_crtc(crtc)->config.pipe_src_w;
Matt Roperf4510a22014-04-01 15:22:40 -07001233 pixel_size = crtc->primary->fb->bits_per_pixel / 8;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001234
1235 /* Use the small buffer method to calculate plane watermark */
1236 entries = ((clock * pixel_size / 1000) * display_latency_ns) / 1000;
1237 tlb_miss = display->fifo_size*display->cacheline_size - hdisplay * 8;
1238 if (tlb_miss > 0)
1239 entries += tlb_miss;
1240 entries = DIV_ROUND_UP(entries, display->cacheline_size);
1241 *plane_wm = entries + display->guard_size;
1242 if (*plane_wm > (int)display->max_wm)
1243 *plane_wm = display->max_wm;
1244
1245 /* Use the large buffer method to calculate cursor watermark */
Ville Syrjälä922044c2014-02-14 14:18:57 +02001246 line_time_us = max(htotal * 1000 / clock, 1);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001247 line_count = (cursor_latency_ns / line_time_us + 1000) / 1000;
Chris Wilson7bb836d2014-03-26 12:38:14 +00001248 entries = line_count * to_intel_crtc(crtc)->cursor_width * pixel_size;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001249 tlb_miss = cursor->fifo_size*cursor->cacheline_size - hdisplay * 8;
1250 if (tlb_miss > 0)
1251 entries += tlb_miss;
1252 entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
1253 *cursor_wm = entries + cursor->guard_size;
1254 if (*cursor_wm > (int)cursor->max_wm)
1255 *cursor_wm = (int)cursor->max_wm;
1256
1257 return true;
1258}
1259
1260/*
1261 * Check the wm result.
1262 *
1263 * If any calculated watermark values is larger than the maximum value that
1264 * can be programmed into the associated watermark register, that watermark
1265 * must be disabled.
1266 */
1267static bool g4x_check_srwm(struct drm_device *dev,
1268 int display_wm, int cursor_wm,
1269 const struct intel_watermark_params *display,
1270 const struct intel_watermark_params *cursor)
1271{
1272 DRM_DEBUG_KMS("SR watermark: display plane %d, cursor %d\n",
1273 display_wm, cursor_wm);
1274
1275 if (display_wm > display->max_wm) {
1276 DRM_DEBUG_KMS("display watermark is too large(%d/%ld), disabling\n",
1277 display_wm, display->max_wm);
1278 return false;
1279 }
1280
1281 if (cursor_wm > cursor->max_wm) {
1282 DRM_DEBUG_KMS("cursor watermark is too large(%d/%ld), disabling\n",
1283 cursor_wm, cursor->max_wm);
1284 return false;
1285 }
1286
1287 if (!(display_wm || cursor_wm)) {
1288 DRM_DEBUG_KMS("SR latency is 0, disabling\n");
1289 return false;
1290 }
1291
1292 return true;
1293}
1294
1295static bool g4x_compute_srwm(struct drm_device *dev,
1296 int plane,
1297 int latency_ns,
1298 const struct intel_watermark_params *display,
1299 const struct intel_watermark_params *cursor,
1300 int *display_wm, int *cursor_wm)
1301{
1302 struct drm_crtc *crtc;
Ville Syrjälä4fe85902013-09-04 18:25:22 +03001303 const struct drm_display_mode *adjusted_mode;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001304 int hdisplay, htotal, pixel_size, clock;
1305 unsigned long line_time_us;
1306 int line_count, line_size;
1307 int small, large;
1308 int entries;
1309
1310 if (!latency_ns) {
1311 *display_wm = *cursor_wm = 0;
1312 return false;
1313 }
1314
1315 crtc = intel_get_crtc_for_plane(dev, plane);
Ville Syrjälä4fe85902013-09-04 18:25:22 +03001316 adjusted_mode = &to_intel_crtc(crtc)->config.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001317 clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -08001318 htotal = adjusted_mode->crtc_htotal;
Ville Syrjälä37327ab2013-09-04 18:25:28 +03001319 hdisplay = to_intel_crtc(crtc)->config.pipe_src_w;
Matt Roperf4510a22014-04-01 15:22:40 -07001320 pixel_size = crtc->primary->fb->bits_per_pixel / 8;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001321
Ville Syrjälä922044c2014-02-14 14:18:57 +02001322 line_time_us = max(htotal * 1000 / clock, 1);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001323 line_count = (latency_ns / line_time_us + 1000) / 1000;
1324 line_size = hdisplay * pixel_size;
1325
1326 /* Use the minimum of the small and large buffer method for primary */
1327 small = ((clock * pixel_size / 1000) * latency_ns) / 1000;
1328 large = line_count * line_size;
1329
1330 entries = DIV_ROUND_UP(min(small, large), display->cacheline_size);
1331 *display_wm = entries + display->guard_size;
1332
1333 /* calculate the self-refresh watermark for display cursor */
Chris Wilson7bb836d2014-03-26 12:38:14 +00001334 entries = line_count * pixel_size * to_intel_crtc(crtc)->cursor_width;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001335 entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
1336 *cursor_wm = entries + cursor->guard_size;
1337
1338 return g4x_check_srwm(dev,
1339 *display_wm, *cursor_wm,
1340 display, cursor);
1341}
1342
Gajanan Bhat0948c262014-08-07 01:58:24 +05301343static bool vlv_compute_drain_latency(struct drm_crtc *crtc,
1344 int pixel_size,
1345 int *prec_mult,
1346 int *drain_latency)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001347{
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001348 int entries;
Gajanan Bhat0948c262014-08-07 01:58:24 +05301349 int clock = to_intel_crtc(crtc)->config.adjusted_mode.crtc_clock;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001350
Gajanan Bhat0948c262014-08-07 01:58:24 +05301351 if (WARN(clock == 0, "Pixel clock is zero!\n"))
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001352 return false;
1353
Gajanan Bhat0948c262014-08-07 01:58:24 +05301354 if (WARN(pixel_size == 0, "Pixel size is zero!\n"))
1355 return false;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001356
Gajanan Bhata398e9c2014-08-05 23:15:54 +05301357 entries = DIV_ROUND_UP(clock, 1000) * pixel_size;
Gajanan Bhat0948c262014-08-07 01:58:24 +05301358 *prec_mult = (entries > 128) ? DRAIN_LATENCY_PRECISION_64 :
1359 DRAIN_LATENCY_PRECISION_32;
1360 *drain_latency = (64 * (*prec_mult) * 4) / entries;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001361
Gajanan Bhata398e9c2014-08-05 23:15:54 +05301362 if (*drain_latency > DRAIN_LATENCY_MASK)
1363 *drain_latency = DRAIN_LATENCY_MASK;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001364
1365 return true;
1366}
1367
1368/*
1369 * Update drain latency registers of memory arbiter
1370 *
1371 * Valleyview SoC has a new memory arbiter and needs drain latency registers
1372 * to be programmed. Each plane has a drain latency multiplier and a drain
1373 * latency value.
1374 */
1375
Gajanan Bhat41aad812014-07-16 18:24:03 +05301376static void vlv_update_drain_latency(struct drm_crtc *crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001377{
Gajanan Bhat0948c262014-08-07 01:58:24 +05301378 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
1379 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1380 int pixel_size;
1381 int drain_latency;
1382 enum pipe pipe = intel_crtc->pipe;
1383 int plane_prec, prec_mult, plane_dl;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001384
Gajanan Bhat0948c262014-08-07 01:58:24 +05301385 plane_dl = I915_READ(VLV_DDL(pipe)) & ~(DDL_PLANE_PRECISION_64 |
1386 DRAIN_LATENCY_MASK | DDL_CURSOR_PRECISION_64 |
1387 (DRAIN_LATENCY_MASK << DDL_CURSOR_SHIFT));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001388
Gajanan Bhat0948c262014-08-07 01:58:24 +05301389 if (!intel_crtc_active(crtc)) {
1390 I915_WRITE(VLV_DDL(pipe), plane_dl);
1391 return;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001392 }
1393
Gajanan Bhat0948c262014-08-07 01:58:24 +05301394 /* Primary plane Drain Latency */
1395 pixel_size = crtc->primary->fb->bits_per_pixel / 8; /* BPP */
1396 if (vlv_compute_drain_latency(crtc, pixel_size, &prec_mult, &drain_latency)) {
1397 plane_prec = (prec_mult == DRAIN_LATENCY_PRECISION_64) ?
1398 DDL_PLANE_PRECISION_64 :
1399 DDL_PLANE_PRECISION_32;
1400 plane_dl |= plane_prec | drain_latency;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001401 }
Gajanan Bhat0948c262014-08-07 01:58:24 +05301402
1403 /* Cursor Drain Latency
1404 * BPP is always 4 for cursor
1405 */
1406 pixel_size = 4;
1407
1408 /* Program cursor DL only if it is enabled */
1409 if (intel_crtc->cursor_base &&
1410 vlv_compute_drain_latency(crtc, pixel_size, &prec_mult, &drain_latency)) {
1411 plane_prec = (prec_mult == DRAIN_LATENCY_PRECISION_64) ?
1412 DDL_CURSOR_PRECISION_64 :
1413 DDL_CURSOR_PRECISION_32;
1414 plane_dl |= plane_prec | (drain_latency << DDL_CURSOR_SHIFT);
1415 }
1416
1417 I915_WRITE(VLV_DDL(pipe), plane_dl);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001418}
1419
1420#define single_plane_enabled(mask) is_power_of_2(mask)
1421
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001422static void valleyview_update_wm(struct drm_crtc *crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001423{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001424 struct drm_device *dev = crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001425 static const int sr_latency_ns = 12000;
1426 struct drm_i915_private *dev_priv = dev->dev_private;
1427 int planea_wm, planeb_wm, cursora_wm, cursorb_wm;
1428 int plane_sr, cursor_sr;
Chris Wilsonaf6c4572012-12-11 12:01:43 +00001429 int ignore_plane_sr, ignore_cursor_sr;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001430 unsigned int enabled = 0;
Imre Deak98584252014-06-13 14:54:20 +03001431 bool cxsr_enabled;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001432
Gajanan Bhat41aad812014-07-16 18:24:03 +05301433 vlv_update_drain_latency(crtc);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001434
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001435 if (g4x_compute_wm0(dev, PIPE_A,
Chris Wilson5aef6002014-09-03 11:56:07 +01001436 &valleyview_wm_info, pessimal_latency_ns,
1437 &valleyview_cursor_wm_info, pessimal_latency_ns,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001438 &planea_wm, &cursora_wm))
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001439 enabled |= 1 << PIPE_A;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001440
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001441 if (g4x_compute_wm0(dev, PIPE_B,
Chris Wilson5aef6002014-09-03 11:56:07 +01001442 &valleyview_wm_info, pessimal_latency_ns,
1443 &valleyview_cursor_wm_info, pessimal_latency_ns,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001444 &planeb_wm, &cursorb_wm))
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001445 enabled |= 1 << PIPE_B;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001446
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001447 if (single_plane_enabled(enabled) &&
1448 g4x_compute_srwm(dev, ffs(enabled) - 1,
1449 sr_latency_ns,
1450 &valleyview_wm_info,
1451 &valleyview_cursor_wm_info,
Chris Wilsonaf6c4572012-12-11 12:01:43 +00001452 &plane_sr, &ignore_cursor_sr) &&
1453 g4x_compute_srwm(dev, ffs(enabled) - 1,
1454 2*sr_latency_ns,
1455 &valleyview_wm_info,
1456 &valleyview_cursor_wm_info,
Chris Wilson52bd02d2012-12-07 10:43:24 +00001457 &ignore_plane_sr, &cursor_sr)) {
Imre Deak98584252014-06-13 14:54:20 +03001458 cxsr_enabled = true;
Chris Wilson52bd02d2012-12-07 10:43:24 +00001459 } else {
Imre Deak98584252014-06-13 14:54:20 +03001460 cxsr_enabled = false;
Imre Deak5209b1f2014-07-01 12:36:17 +03001461 intel_set_memory_cxsr(dev_priv, false);
Chris Wilson52bd02d2012-12-07 10:43:24 +00001462 plane_sr = cursor_sr = 0;
1463 }
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001464
Ville Syrjäläa5043452014-06-28 02:04:18 +03001465 DRM_DEBUG_KMS("Setting FIFO watermarks - A: plane=%d, cursor=%d, "
1466 "B: plane=%d, cursor=%d, SR: plane=%d, cursor=%d\n",
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001467 planea_wm, cursora_wm,
1468 planeb_wm, cursorb_wm,
1469 plane_sr, cursor_sr);
1470
1471 I915_WRITE(DSPFW1,
1472 (plane_sr << DSPFW_SR_SHIFT) |
1473 (cursorb_wm << DSPFW_CURSORB_SHIFT) |
1474 (planeb_wm << DSPFW_PLANEB_SHIFT) |
Ville Syrjälä0a560672014-06-11 16:51:18 +03001475 (planea_wm << DSPFW_PLANEA_SHIFT));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001476 I915_WRITE(DSPFW2,
Chris Wilson8c919b22012-12-04 16:33:19 +00001477 (I915_READ(DSPFW2) & ~DSPFW_CURSORA_MASK) |
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001478 (cursora_wm << DSPFW_CURSORA_SHIFT));
1479 I915_WRITE(DSPFW3,
Chris Wilson8c919b22012-12-04 16:33:19 +00001480 (I915_READ(DSPFW3) & ~DSPFW_CURSOR_SR_MASK) |
1481 (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
Imre Deak98584252014-06-13 14:54:20 +03001482
1483 if (cxsr_enabled)
1484 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001485}
1486
Ville Syrjälä3c2777f2014-06-26 17:03:06 +03001487static void cherryview_update_wm(struct drm_crtc *crtc)
1488{
1489 struct drm_device *dev = crtc->dev;
1490 static const int sr_latency_ns = 12000;
1491 struct drm_i915_private *dev_priv = dev->dev_private;
1492 int planea_wm, planeb_wm, planec_wm;
1493 int cursora_wm, cursorb_wm, cursorc_wm;
1494 int plane_sr, cursor_sr;
1495 int ignore_plane_sr, ignore_cursor_sr;
1496 unsigned int enabled = 0;
1497 bool cxsr_enabled;
1498
1499 vlv_update_drain_latency(crtc);
1500
1501 if (g4x_compute_wm0(dev, PIPE_A,
Chris Wilson5aef6002014-09-03 11:56:07 +01001502 &valleyview_wm_info, pessimal_latency_ns,
1503 &valleyview_cursor_wm_info, pessimal_latency_ns,
Ville Syrjälä3c2777f2014-06-26 17:03:06 +03001504 &planea_wm, &cursora_wm))
1505 enabled |= 1 << PIPE_A;
1506
1507 if (g4x_compute_wm0(dev, PIPE_B,
Chris Wilson5aef6002014-09-03 11:56:07 +01001508 &valleyview_wm_info, pessimal_latency_ns,
1509 &valleyview_cursor_wm_info, pessimal_latency_ns,
Ville Syrjälä3c2777f2014-06-26 17:03:06 +03001510 &planeb_wm, &cursorb_wm))
1511 enabled |= 1 << PIPE_B;
1512
1513 if (g4x_compute_wm0(dev, PIPE_C,
Chris Wilson5aef6002014-09-03 11:56:07 +01001514 &valleyview_wm_info, pessimal_latency_ns,
1515 &valleyview_cursor_wm_info, pessimal_latency_ns,
Ville Syrjälä3c2777f2014-06-26 17:03:06 +03001516 &planec_wm, &cursorc_wm))
1517 enabled |= 1 << PIPE_C;
1518
1519 if (single_plane_enabled(enabled) &&
1520 g4x_compute_srwm(dev, ffs(enabled) - 1,
1521 sr_latency_ns,
1522 &valleyview_wm_info,
1523 &valleyview_cursor_wm_info,
1524 &plane_sr, &ignore_cursor_sr) &&
1525 g4x_compute_srwm(dev, ffs(enabled) - 1,
1526 2*sr_latency_ns,
1527 &valleyview_wm_info,
1528 &valleyview_cursor_wm_info,
1529 &ignore_plane_sr, &cursor_sr)) {
1530 cxsr_enabled = true;
1531 } else {
1532 cxsr_enabled = false;
1533 intel_set_memory_cxsr(dev_priv, false);
1534 plane_sr = cursor_sr = 0;
1535 }
1536
1537 DRM_DEBUG_KMS("Setting FIFO watermarks - A: plane=%d, cursor=%d, "
1538 "B: plane=%d, cursor=%d, C: plane=%d, cursor=%d, "
1539 "SR: plane=%d, cursor=%d\n",
1540 planea_wm, cursora_wm,
1541 planeb_wm, cursorb_wm,
1542 planec_wm, cursorc_wm,
1543 plane_sr, cursor_sr);
1544
1545 I915_WRITE(DSPFW1,
1546 (plane_sr << DSPFW_SR_SHIFT) |
1547 (cursorb_wm << DSPFW_CURSORB_SHIFT) |
1548 (planeb_wm << DSPFW_PLANEB_SHIFT) |
1549 (planea_wm << DSPFW_PLANEA_SHIFT));
1550 I915_WRITE(DSPFW2,
1551 (I915_READ(DSPFW2) & ~DSPFW_CURSORA_MASK) |
1552 (cursora_wm << DSPFW_CURSORA_SHIFT));
1553 I915_WRITE(DSPFW3,
1554 (I915_READ(DSPFW3) & ~DSPFW_CURSOR_SR_MASK) |
1555 (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
1556 I915_WRITE(DSPFW9_CHV,
1557 (I915_READ(DSPFW9_CHV) & ~(DSPFW_PLANEC_MASK |
1558 DSPFW_CURSORC_MASK)) |
1559 (planec_wm << DSPFW_PLANEC_SHIFT) |
1560 (cursorc_wm << DSPFW_CURSORC_SHIFT));
1561
1562 if (cxsr_enabled)
1563 intel_set_memory_cxsr(dev_priv, true);
1564}
1565
Gajanan Bhat01e184c2014-08-07 17:03:30 +05301566static void valleyview_update_sprite_wm(struct drm_plane *plane,
1567 struct drm_crtc *crtc,
1568 uint32_t sprite_width,
1569 uint32_t sprite_height,
1570 int pixel_size,
1571 bool enabled, bool scaled)
1572{
1573 struct drm_device *dev = crtc->dev;
1574 struct drm_i915_private *dev_priv = dev->dev_private;
1575 int pipe = to_intel_plane(plane)->pipe;
1576 int sprite = to_intel_plane(plane)->plane;
1577 int drain_latency;
1578 int plane_prec;
1579 int sprite_dl;
1580 int prec_mult;
1581
1582 sprite_dl = I915_READ(VLV_DDL(pipe)) & ~(DDL_SPRITE_PRECISION_64(sprite) |
1583 (DRAIN_LATENCY_MASK << DDL_SPRITE_SHIFT(sprite)));
1584
1585 if (enabled && vlv_compute_drain_latency(crtc, pixel_size, &prec_mult,
1586 &drain_latency)) {
1587 plane_prec = (prec_mult == DRAIN_LATENCY_PRECISION_64) ?
1588 DDL_SPRITE_PRECISION_64(sprite) :
1589 DDL_SPRITE_PRECISION_32(sprite);
1590 sprite_dl |= plane_prec |
1591 (drain_latency << DDL_SPRITE_SHIFT(sprite));
1592 }
1593
1594 I915_WRITE(VLV_DDL(pipe), sprite_dl);
1595}
1596
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001597static void g4x_update_wm(struct drm_crtc *crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001598{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001599 struct drm_device *dev = crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001600 static const int sr_latency_ns = 12000;
1601 struct drm_i915_private *dev_priv = dev->dev_private;
1602 int planea_wm, planeb_wm, cursora_wm, cursorb_wm;
1603 int plane_sr, cursor_sr;
1604 unsigned int enabled = 0;
Imre Deak98584252014-06-13 14:54:20 +03001605 bool cxsr_enabled;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001606
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001607 if (g4x_compute_wm0(dev, PIPE_A,
Chris Wilson5aef6002014-09-03 11:56:07 +01001608 &g4x_wm_info, pessimal_latency_ns,
1609 &g4x_cursor_wm_info, pessimal_latency_ns,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001610 &planea_wm, &cursora_wm))
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001611 enabled |= 1 << PIPE_A;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001612
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001613 if (g4x_compute_wm0(dev, PIPE_B,
Chris Wilson5aef6002014-09-03 11:56:07 +01001614 &g4x_wm_info, pessimal_latency_ns,
1615 &g4x_cursor_wm_info, pessimal_latency_ns,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001616 &planeb_wm, &cursorb_wm))
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001617 enabled |= 1 << PIPE_B;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001618
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001619 if (single_plane_enabled(enabled) &&
1620 g4x_compute_srwm(dev, ffs(enabled) - 1,
1621 sr_latency_ns,
1622 &g4x_wm_info,
1623 &g4x_cursor_wm_info,
Chris Wilson52bd02d2012-12-07 10:43:24 +00001624 &plane_sr, &cursor_sr)) {
Imre Deak98584252014-06-13 14:54:20 +03001625 cxsr_enabled = true;
Chris Wilson52bd02d2012-12-07 10:43:24 +00001626 } else {
Imre Deak98584252014-06-13 14:54:20 +03001627 cxsr_enabled = false;
Imre Deak5209b1f2014-07-01 12:36:17 +03001628 intel_set_memory_cxsr(dev_priv, false);
Chris Wilson52bd02d2012-12-07 10:43:24 +00001629 plane_sr = cursor_sr = 0;
1630 }
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001631
Ville Syrjäläa5043452014-06-28 02:04:18 +03001632 DRM_DEBUG_KMS("Setting FIFO watermarks - A: plane=%d, cursor=%d, "
1633 "B: plane=%d, cursor=%d, SR: plane=%d, cursor=%d\n",
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001634 planea_wm, cursora_wm,
1635 planeb_wm, cursorb_wm,
1636 plane_sr, cursor_sr);
1637
1638 I915_WRITE(DSPFW1,
1639 (plane_sr << DSPFW_SR_SHIFT) |
1640 (cursorb_wm << DSPFW_CURSORB_SHIFT) |
1641 (planeb_wm << DSPFW_PLANEB_SHIFT) |
Ville Syrjälä0a560672014-06-11 16:51:18 +03001642 (planea_wm << DSPFW_PLANEA_SHIFT));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001643 I915_WRITE(DSPFW2,
Chris Wilson8c919b22012-12-04 16:33:19 +00001644 (I915_READ(DSPFW2) & ~DSPFW_CURSORA_MASK) |
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001645 (cursora_wm << DSPFW_CURSORA_SHIFT));
1646 /* HPLL off in SR has some issues on G4x... disable it */
1647 I915_WRITE(DSPFW3,
Chris Wilson8c919b22012-12-04 16:33:19 +00001648 (I915_READ(DSPFW3) & ~(DSPFW_HPLL_SR_EN | DSPFW_CURSOR_SR_MASK)) |
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001649 (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
Imre Deak98584252014-06-13 14:54:20 +03001650
1651 if (cxsr_enabled)
1652 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001653}
1654
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001655static void i965_update_wm(struct drm_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001656{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001657 struct drm_device *dev = unused_crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001658 struct drm_i915_private *dev_priv = dev->dev_private;
1659 struct drm_crtc *crtc;
1660 int srwm = 1;
1661 int cursor_sr = 16;
Imre Deak98584252014-06-13 14:54:20 +03001662 bool cxsr_enabled;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001663
1664 /* Calc sr entries for one plane configs */
1665 crtc = single_enabled_crtc(dev);
1666 if (crtc) {
1667 /* self-refresh has much higher latency */
1668 static const int sr_latency_ns = 12000;
Ville Syrjälä4fe85902013-09-04 18:25:22 +03001669 const struct drm_display_mode *adjusted_mode =
1670 &to_intel_crtc(crtc)->config.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001671 int clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -08001672 int htotal = adjusted_mode->crtc_htotal;
Ville Syrjälä37327ab2013-09-04 18:25:28 +03001673 int hdisplay = to_intel_crtc(crtc)->config.pipe_src_w;
Matt Roperf4510a22014-04-01 15:22:40 -07001674 int pixel_size = crtc->primary->fb->bits_per_pixel / 8;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001675 unsigned long line_time_us;
1676 int entries;
1677
Ville Syrjälä922044c2014-02-14 14:18:57 +02001678 line_time_us = max(htotal * 1000 / clock, 1);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001679
1680 /* Use ns/us then divide to preserve precision */
1681 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
1682 pixel_size * hdisplay;
1683 entries = DIV_ROUND_UP(entries, I915_FIFO_LINE_SIZE);
1684 srwm = I965_FIFO_SIZE - entries;
1685 if (srwm < 0)
1686 srwm = 1;
1687 srwm &= 0x1ff;
1688 DRM_DEBUG_KMS("self-refresh entries: %d, wm: %d\n",
1689 entries, srwm);
1690
1691 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
Chris Wilson7bb836d2014-03-26 12:38:14 +00001692 pixel_size * to_intel_crtc(crtc)->cursor_width;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001693 entries = DIV_ROUND_UP(entries,
1694 i965_cursor_wm_info.cacheline_size);
1695 cursor_sr = i965_cursor_wm_info.fifo_size -
1696 (entries + i965_cursor_wm_info.guard_size);
1697
1698 if (cursor_sr > i965_cursor_wm_info.max_wm)
1699 cursor_sr = i965_cursor_wm_info.max_wm;
1700
1701 DRM_DEBUG_KMS("self-refresh watermark: display plane %d "
1702 "cursor %d\n", srwm, cursor_sr);
1703
Imre Deak98584252014-06-13 14:54:20 +03001704 cxsr_enabled = true;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001705 } else {
Imre Deak98584252014-06-13 14:54:20 +03001706 cxsr_enabled = false;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001707 /* Turn off self refresh if both pipes are enabled */
Imre Deak5209b1f2014-07-01 12:36:17 +03001708 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001709 }
1710
1711 DRM_DEBUG_KMS("Setting FIFO watermarks - A: 8, B: 8, C: 8, SR %d\n",
1712 srwm);
1713
1714 /* 965 has limitations... */
1715 I915_WRITE(DSPFW1, (srwm << DSPFW_SR_SHIFT) |
Ville Syrjälä0a560672014-06-11 16:51:18 +03001716 (8 << DSPFW_CURSORB_SHIFT) |
1717 (8 << DSPFW_PLANEB_SHIFT) |
1718 (8 << DSPFW_PLANEA_SHIFT));
1719 I915_WRITE(DSPFW2, (8 << DSPFW_CURSORA_SHIFT) |
1720 (8 << DSPFW_PLANEC_SHIFT_OLD));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001721 /* update cursor SR watermark */
1722 I915_WRITE(DSPFW3, (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
Imre Deak98584252014-06-13 14:54:20 +03001723
1724 if (cxsr_enabled)
1725 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001726}
1727
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001728static void i9xx_update_wm(struct drm_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001729{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001730 struct drm_device *dev = unused_crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001731 struct drm_i915_private *dev_priv = dev->dev_private;
1732 const struct intel_watermark_params *wm_info;
1733 uint32_t fwater_lo;
1734 uint32_t fwater_hi;
1735 int cwm, srwm = 1;
1736 int fifo_size;
1737 int planea_wm, planeb_wm;
1738 struct drm_crtc *crtc, *enabled = NULL;
1739
1740 if (IS_I945GM(dev))
1741 wm_info = &i945_wm_info;
1742 else if (!IS_GEN2(dev))
1743 wm_info = &i915_wm_info;
1744 else
Ville Syrjälä9d539102014-08-15 01:21:53 +03001745 wm_info = &i830_a_wm_info;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001746
1747 fifo_size = dev_priv->display.get_fifo_size(dev, 0);
1748 crtc = intel_get_crtc_for_plane(dev, 0);
Chris Wilson3490ea52013-01-07 10:11:40 +00001749 if (intel_crtc_active(crtc)) {
Damien Lespiau241bfc32013-09-25 16:45:37 +01001750 const struct drm_display_mode *adjusted_mode;
Matt Roperf4510a22014-04-01 15:22:40 -07001751 int cpp = crtc->primary->fb->bits_per_pixel / 8;
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001752 if (IS_GEN2(dev))
1753 cpp = 4;
1754
Damien Lespiau241bfc32013-09-25 16:45:37 +01001755 adjusted_mode = &to_intel_crtc(crtc)->config.adjusted_mode;
1756 planea_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001757 wm_info, fifo_size, cpp,
Chris Wilson5aef6002014-09-03 11:56:07 +01001758 pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001759 enabled = crtc;
Ville Syrjälä9d539102014-08-15 01:21:53 +03001760 } else {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001761 planea_wm = fifo_size - wm_info->guard_size;
Ville Syrjälä9d539102014-08-15 01:21:53 +03001762 if (planea_wm > (long)wm_info->max_wm)
1763 planea_wm = wm_info->max_wm;
1764 }
1765
1766 if (IS_GEN2(dev))
1767 wm_info = &i830_bc_wm_info;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001768
1769 fifo_size = dev_priv->display.get_fifo_size(dev, 1);
1770 crtc = intel_get_crtc_for_plane(dev, 1);
Chris Wilson3490ea52013-01-07 10:11:40 +00001771 if (intel_crtc_active(crtc)) {
Damien Lespiau241bfc32013-09-25 16:45:37 +01001772 const struct drm_display_mode *adjusted_mode;
Matt Roperf4510a22014-04-01 15:22:40 -07001773 int cpp = crtc->primary->fb->bits_per_pixel / 8;
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001774 if (IS_GEN2(dev))
1775 cpp = 4;
1776
Damien Lespiau241bfc32013-09-25 16:45:37 +01001777 adjusted_mode = &to_intel_crtc(crtc)->config.adjusted_mode;
1778 planeb_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001779 wm_info, fifo_size, cpp,
Chris Wilson5aef6002014-09-03 11:56:07 +01001780 pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001781 if (enabled == NULL)
1782 enabled = crtc;
1783 else
1784 enabled = NULL;
Ville Syrjälä9d539102014-08-15 01:21:53 +03001785 } else {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001786 planeb_wm = fifo_size - wm_info->guard_size;
Ville Syrjälä9d539102014-08-15 01:21:53 +03001787 if (planeb_wm > (long)wm_info->max_wm)
1788 planeb_wm = wm_info->max_wm;
1789 }
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001790
1791 DRM_DEBUG_KMS("FIFO watermarks - A: %d, B: %d\n", planea_wm, planeb_wm);
1792
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02001793 if (IS_I915GM(dev) && enabled) {
Matt Roper2ff8fde2014-07-08 07:50:07 -07001794 struct drm_i915_gem_object *obj;
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02001795
Matt Roper2ff8fde2014-07-08 07:50:07 -07001796 obj = intel_fb_obj(enabled->primary->fb);
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02001797
1798 /* self-refresh seems busted with untiled */
Matt Roper2ff8fde2014-07-08 07:50:07 -07001799 if (obj->tiling_mode == I915_TILING_NONE)
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02001800 enabled = NULL;
1801 }
1802
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001803 /*
1804 * Overlay gets an aggressive default since video jitter is bad.
1805 */
1806 cwm = 2;
1807
1808 /* Play safe and disable self-refresh before adjusting watermarks. */
Imre Deak5209b1f2014-07-01 12:36:17 +03001809 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001810
1811 /* Calc sr entries for one plane configs */
1812 if (HAS_FW_BLC(dev) && enabled) {
1813 /* self-refresh has much higher latency */
1814 static const int sr_latency_ns = 6000;
Ville Syrjälä4fe85902013-09-04 18:25:22 +03001815 const struct drm_display_mode *adjusted_mode =
1816 &to_intel_crtc(enabled)->config.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001817 int clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -08001818 int htotal = adjusted_mode->crtc_htotal;
Daniel Vetterf727b492013-11-20 15:02:10 +01001819 int hdisplay = to_intel_crtc(enabled)->config.pipe_src_w;
Matt Roperf4510a22014-04-01 15:22:40 -07001820 int pixel_size = enabled->primary->fb->bits_per_pixel / 8;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001821 unsigned long line_time_us;
1822 int entries;
1823
Ville Syrjälä922044c2014-02-14 14:18:57 +02001824 line_time_us = max(htotal * 1000 / clock, 1);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001825
1826 /* Use ns/us then divide to preserve precision */
1827 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
1828 pixel_size * hdisplay;
1829 entries = DIV_ROUND_UP(entries, wm_info->cacheline_size);
1830 DRM_DEBUG_KMS("self-refresh entries: %d\n", entries);
1831 srwm = wm_info->fifo_size - entries;
1832 if (srwm < 0)
1833 srwm = 1;
1834
1835 if (IS_I945G(dev) || IS_I945GM(dev))
1836 I915_WRITE(FW_BLC_SELF,
1837 FW_BLC_SELF_FIFO_MASK | (srwm & 0xff));
1838 else if (IS_I915GM(dev))
1839 I915_WRITE(FW_BLC_SELF, srwm & 0x3f);
1840 }
1841
1842 DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d, B: %d, C: %d, SR %d\n",
1843 planea_wm, planeb_wm, cwm, srwm);
1844
1845 fwater_lo = ((planeb_wm & 0x3f) << 16) | (planea_wm & 0x3f);
1846 fwater_hi = (cwm & 0x1f);
1847
1848 /* Set request length to 8 cachelines per fetch */
1849 fwater_lo = fwater_lo | (1 << 24) | (1 << 8);
1850 fwater_hi = fwater_hi | (1 << 8);
1851
1852 I915_WRITE(FW_BLC, fwater_lo);
1853 I915_WRITE(FW_BLC2, fwater_hi);
1854
Imre Deak5209b1f2014-07-01 12:36:17 +03001855 if (enabled)
1856 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001857}
1858
Daniel Vetterfeb56b92013-12-14 20:38:30 -02001859static void i845_update_wm(struct drm_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001860{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001861 struct drm_device *dev = unused_crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001862 struct drm_i915_private *dev_priv = dev->dev_private;
1863 struct drm_crtc *crtc;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001864 const struct drm_display_mode *adjusted_mode;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001865 uint32_t fwater_lo;
1866 int planea_wm;
1867
1868 crtc = single_enabled_crtc(dev);
1869 if (crtc == NULL)
1870 return;
1871
Damien Lespiau241bfc32013-09-25 16:45:37 +01001872 adjusted_mode = &to_intel_crtc(crtc)->config.adjusted_mode;
1873 planea_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Daniel Vetterfeb56b92013-12-14 20:38:30 -02001874 &i845_wm_info,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001875 dev_priv->display.get_fifo_size(dev, 0),
Chris Wilson5aef6002014-09-03 11:56:07 +01001876 4, pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001877 fwater_lo = I915_READ(FW_BLC) & ~0xfff;
1878 fwater_lo |= (3<<8) | planea_wm;
1879
1880 DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d\n", planea_wm);
1881
1882 I915_WRITE(FW_BLC, fwater_lo);
1883}
1884
Ville Syrjälä36587292013-07-05 11:57:16 +03001885static uint32_t ilk_pipe_pixel_rate(struct drm_device *dev,
1886 struct drm_crtc *crtc)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001887{
1888 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilsonfd4daa92013-08-27 17:04:17 +01001889 uint32_t pixel_rate;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001890
Damien Lespiau241bfc32013-09-25 16:45:37 +01001891 pixel_rate = intel_crtc->config.adjusted_mode.crtc_clock;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001892
1893 /* We only use IF-ID interlacing. If we ever use PF-ID we'll need to
1894 * adjust the pixel_rate here. */
1895
Chris Wilsonfd4daa92013-08-27 17:04:17 +01001896 if (intel_crtc->config.pch_pfit.enabled) {
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001897 uint64_t pipe_w, pipe_h, pfit_w, pfit_h;
Chris Wilsonfd4daa92013-08-27 17:04:17 +01001898 uint32_t pfit_size = intel_crtc->config.pch_pfit.size;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001899
Ville Syrjälä37327ab2013-09-04 18:25:28 +03001900 pipe_w = intel_crtc->config.pipe_src_w;
1901 pipe_h = intel_crtc->config.pipe_src_h;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001902 pfit_w = (pfit_size >> 16) & 0xFFFF;
1903 pfit_h = pfit_size & 0xFFFF;
1904 if (pipe_w < pfit_w)
1905 pipe_w = pfit_w;
1906 if (pipe_h < pfit_h)
1907 pipe_h = pfit_h;
1908
1909 pixel_rate = div_u64((uint64_t) pixel_rate * pipe_w * pipe_h,
1910 pfit_w * pfit_h);
1911 }
1912
1913 return pixel_rate;
1914}
1915
Ville Syrjälä37126462013-08-01 16:18:55 +03001916/* latency must be in 0.1us units. */
Ville Syrjälä23297042013-07-05 11:57:17 +03001917static uint32_t ilk_wm_method1(uint32_t pixel_rate, uint8_t bytes_per_pixel,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001918 uint32_t latency)
1919{
1920 uint64_t ret;
1921
Ville Syrjälä3312ba62013-08-01 16:18:53 +03001922 if (WARN(latency == 0, "Latency value missing\n"))
1923 return UINT_MAX;
1924
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001925 ret = (uint64_t) pixel_rate * bytes_per_pixel * latency;
1926 ret = DIV_ROUND_UP_ULL(ret, 64 * 10000) + 2;
1927
1928 return ret;
1929}
1930
Ville Syrjälä37126462013-08-01 16:18:55 +03001931/* latency must be in 0.1us units. */
Ville Syrjälä23297042013-07-05 11:57:17 +03001932static uint32_t ilk_wm_method2(uint32_t pixel_rate, uint32_t pipe_htotal,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001933 uint32_t horiz_pixels, uint8_t bytes_per_pixel,
1934 uint32_t latency)
1935{
1936 uint32_t ret;
1937
Ville Syrjälä3312ba62013-08-01 16:18:53 +03001938 if (WARN(latency == 0, "Latency value missing\n"))
1939 return UINT_MAX;
1940
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001941 ret = (latency * pixel_rate) / (pipe_htotal * 10000);
1942 ret = (ret + 1) * horiz_pixels * bytes_per_pixel;
1943 ret = DIV_ROUND_UP(ret, 64) + 2;
1944 return ret;
1945}
1946
Ville Syrjälä23297042013-07-05 11:57:17 +03001947static uint32_t ilk_wm_fbc(uint32_t pri_val, uint32_t horiz_pixels,
Paulo Zanonicca32e92013-05-31 11:45:06 -03001948 uint8_t bytes_per_pixel)
1949{
1950 return DIV_ROUND_UP(pri_val * 64, horiz_pixels * bytes_per_pixel) + 2;
1951}
1952
Imre Deak820c1982013-12-17 14:46:36 +02001953struct ilk_pipe_wm_parameters {
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001954 bool active;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001955 uint32_t pipe_htotal;
1956 uint32_t pixel_rate;
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001957 struct intel_plane_wm_parameters pri;
1958 struct intel_plane_wm_parameters spr;
1959 struct intel_plane_wm_parameters cur;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001960};
1961
Imre Deak820c1982013-12-17 14:46:36 +02001962struct ilk_wm_maximums {
Paulo Zanonicca32e92013-05-31 11:45:06 -03001963 uint16_t pri;
1964 uint16_t spr;
1965 uint16_t cur;
1966 uint16_t fbc;
1967};
1968
Ville Syrjälä240264f2013-08-07 13:29:12 +03001969/* used in computing the new watermarks state */
1970struct intel_wm_config {
1971 unsigned int num_pipes_active;
1972 bool sprites_enabled;
1973 bool sprites_scaled;
Ville Syrjälä240264f2013-08-07 13:29:12 +03001974};
1975
Ville Syrjälä37126462013-08-01 16:18:55 +03001976/*
1977 * For both WM_PIPE and WM_LP.
1978 * mem_value must be in 0.1us units.
1979 */
Imre Deak820c1982013-12-17 14:46:36 +02001980static uint32_t ilk_compute_pri_wm(const struct ilk_pipe_wm_parameters *params,
Paulo Zanonicca32e92013-05-31 11:45:06 -03001981 uint32_t mem_value,
1982 bool is_lp)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001983{
Paulo Zanonicca32e92013-05-31 11:45:06 -03001984 uint32_t method1, method2;
1985
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001986 if (!params->active || !params->pri.enabled)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001987 return 0;
1988
Ville Syrjälä23297042013-07-05 11:57:17 +03001989 method1 = ilk_wm_method1(params->pixel_rate,
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001990 params->pri.bytes_per_pixel,
Paulo Zanonicca32e92013-05-31 11:45:06 -03001991 mem_value);
1992
1993 if (!is_lp)
1994 return method1;
1995
Ville Syrjälä23297042013-07-05 11:57:17 +03001996 method2 = ilk_wm_method2(params->pixel_rate,
Paulo Zanonicca32e92013-05-31 11:45:06 -03001997 params->pipe_htotal,
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001998 params->pri.horiz_pixels,
1999 params->pri.bytes_per_pixel,
Paulo Zanonicca32e92013-05-31 11:45:06 -03002000 mem_value);
2001
2002 return min(method1, method2);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002003}
2004
Ville Syrjälä37126462013-08-01 16:18:55 +03002005/*
2006 * For both WM_PIPE and WM_LP.
2007 * mem_value must be in 0.1us units.
2008 */
Imre Deak820c1982013-12-17 14:46:36 +02002009static uint32_t ilk_compute_spr_wm(const struct ilk_pipe_wm_parameters *params,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002010 uint32_t mem_value)
2011{
2012 uint32_t method1, method2;
2013
Ville Syrjäläc35426d2013-08-07 13:29:50 +03002014 if (!params->active || !params->spr.enabled)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002015 return 0;
2016
Ville Syrjälä23297042013-07-05 11:57:17 +03002017 method1 = ilk_wm_method1(params->pixel_rate,
Ville Syrjäläc35426d2013-08-07 13:29:50 +03002018 params->spr.bytes_per_pixel,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002019 mem_value);
Ville Syrjälä23297042013-07-05 11:57:17 +03002020 method2 = ilk_wm_method2(params->pixel_rate,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002021 params->pipe_htotal,
Ville Syrjäläc35426d2013-08-07 13:29:50 +03002022 params->spr.horiz_pixels,
2023 params->spr.bytes_per_pixel,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002024 mem_value);
2025 return min(method1, method2);
2026}
2027
Ville Syrjälä37126462013-08-01 16:18:55 +03002028/*
2029 * For both WM_PIPE and WM_LP.
2030 * mem_value must be in 0.1us units.
2031 */
Imre Deak820c1982013-12-17 14:46:36 +02002032static uint32_t ilk_compute_cur_wm(const struct ilk_pipe_wm_parameters *params,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002033 uint32_t mem_value)
2034{
Ville Syrjäläc35426d2013-08-07 13:29:50 +03002035 if (!params->active || !params->cur.enabled)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002036 return 0;
2037
Ville Syrjälä23297042013-07-05 11:57:17 +03002038 return ilk_wm_method2(params->pixel_rate,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002039 params->pipe_htotal,
Ville Syrjäläc35426d2013-08-07 13:29:50 +03002040 params->cur.horiz_pixels,
2041 params->cur.bytes_per_pixel,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002042 mem_value);
2043}
2044
Paulo Zanonicca32e92013-05-31 11:45:06 -03002045/* Only for WM_LP. */
Imre Deak820c1982013-12-17 14:46:36 +02002046static uint32_t ilk_compute_fbc_wm(const struct ilk_pipe_wm_parameters *params,
Ville Syrjälä1fda9882013-07-05 11:57:19 +03002047 uint32_t pri_val)
Paulo Zanonicca32e92013-05-31 11:45:06 -03002048{
Ville Syrjäläc35426d2013-08-07 13:29:50 +03002049 if (!params->active || !params->pri.enabled)
Paulo Zanonicca32e92013-05-31 11:45:06 -03002050 return 0;
2051
Ville Syrjälä23297042013-07-05 11:57:17 +03002052 return ilk_wm_fbc(pri_val,
Ville Syrjäläc35426d2013-08-07 13:29:50 +03002053 params->pri.horiz_pixels,
2054 params->pri.bytes_per_pixel);
Paulo Zanonicca32e92013-05-31 11:45:06 -03002055}
2056
Ville Syrjälä158ae642013-08-07 13:28:19 +03002057static unsigned int ilk_display_fifo_size(const struct drm_device *dev)
2058{
Ville Syrjälä416f4722013-11-02 21:07:46 -07002059 if (INTEL_INFO(dev)->gen >= 8)
2060 return 3072;
2061 else if (INTEL_INFO(dev)->gen >= 7)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002062 return 768;
2063 else
2064 return 512;
2065}
2066
Ville Syrjälä4e975082014-03-07 18:32:11 +02002067static unsigned int ilk_plane_wm_reg_max(const struct drm_device *dev,
2068 int level, bool is_sprite)
2069{
2070 if (INTEL_INFO(dev)->gen >= 8)
2071 /* BDW primary/sprite plane watermarks */
2072 return level == 0 ? 255 : 2047;
2073 else if (INTEL_INFO(dev)->gen >= 7)
2074 /* IVB/HSW primary/sprite plane watermarks */
2075 return level == 0 ? 127 : 1023;
2076 else if (!is_sprite)
2077 /* ILK/SNB primary plane watermarks */
2078 return level == 0 ? 127 : 511;
2079 else
2080 /* ILK/SNB sprite plane watermarks */
2081 return level == 0 ? 63 : 255;
2082}
2083
2084static unsigned int ilk_cursor_wm_reg_max(const struct drm_device *dev,
2085 int level)
2086{
2087 if (INTEL_INFO(dev)->gen >= 7)
2088 return level == 0 ? 63 : 255;
2089 else
2090 return level == 0 ? 31 : 63;
2091}
2092
2093static unsigned int ilk_fbc_wm_reg_max(const struct drm_device *dev)
2094{
2095 if (INTEL_INFO(dev)->gen >= 8)
2096 return 31;
2097 else
2098 return 15;
2099}
2100
Ville Syrjälä158ae642013-08-07 13:28:19 +03002101/* Calculate the maximum primary/sprite plane watermark */
2102static unsigned int ilk_plane_wm_max(const struct drm_device *dev,
2103 int level,
Ville Syrjälä240264f2013-08-07 13:29:12 +03002104 const struct intel_wm_config *config,
Ville Syrjälä158ae642013-08-07 13:28:19 +03002105 enum intel_ddb_partitioning ddb_partitioning,
2106 bool is_sprite)
2107{
2108 unsigned int fifo_size = ilk_display_fifo_size(dev);
Ville Syrjälä158ae642013-08-07 13:28:19 +03002109
2110 /* if sprites aren't enabled, sprites get nothing */
Ville Syrjälä240264f2013-08-07 13:29:12 +03002111 if (is_sprite && !config->sprites_enabled)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002112 return 0;
2113
2114 /* HSW allows LP1+ watermarks even with multiple pipes */
Ville Syrjälä240264f2013-08-07 13:29:12 +03002115 if (level == 0 || config->num_pipes_active > 1) {
Ville Syrjälä158ae642013-08-07 13:28:19 +03002116 fifo_size /= INTEL_INFO(dev)->num_pipes;
2117
2118 /*
2119 * For some reason the non self refresh
2120 * FIFO size is only half of the self
2121 * refresh FIFO size on ILK/SNB.
2122 */
2123 if (INTEL_INFO(dev)->gen <= 6)
2124 fifo_size /= 2;
2125 }
2126
Ville Syrjälä240264f2013-08-07 13:29:12 +03002127 if (config->sprites_enabled) {
Ville Syrjälä158ae642013-08-07 13:28:19 +03002128 /* level 0 is always calculated with 1:1 split */
2129 if (level > 0 && ddb_partitioning == INTEL_DDB_PART_5_6) {
2130 if (is_sprite)
2131 fifo_size *= 5;
2132 fifo_size /= 6;
2133 } else {
2134 fifo_size /= 2;
2135 }
2136 }
2137
2138 /* clamp to max that the registers can hold */
Ville Syrjälä4e975082014-03-07 18:32:11 +02002139 return min(fifo_size, ilk_plane_wm_reg_max(dev, level, is_sprite));
Ville Syrjälä158ae642013-08-07 13:28:19 +03002140}
2141
2142/* Calculate the maximum cursor plane watermark */
2143static unsigned int ilk_cursor_wm_max(const struct drm_device *dev,
Ville Syrjälä240264f2013-08-07 13:29:12 +03002144 int level,
2145 const struct intel_wm_config *config)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002146{
2147 /* HSW LP1+ watermarks w/ multiple pipes */
Ville Syrjälä240264f2013-08-07 13:29:12 +03002148 if (level > 0 && config->num_pipes_active > 1)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002149 return 64;
2150
2151 /* otherwise just report max that registers can hold */
Ville Syrjälä4e975082014-03-07 18:32:11 +02002152 return ilk_cursor_wm_reg_max(dev, level);
Ville Syrjälä158ae642013-08-07 13:28:19 +03002153}
2154
Damien Lespiaud34ff9c2014-01-06 19:17:23 +00002155static void ilk_compute_wm_maximums(const struct drm_device *dev,
Ville Syrjälä34982fe2013-10-09 19:18:09 +03002156 int level,
2157 const struct intel_wm_config *config,
2158 enum intel_ddb_partitioning ddb_partitioning,
Imre Deak820c1982013-12-17 14:46:36 +02002159 struct ilk_wm_maximums *max)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002160{
Ville Syrjälä240264f2013-08-07 13:29:12 +03002161 max->pri = ilk_plane_wm_max(dev, level, config, ddb_partitioning, false);
2162 max->spr = ilk_plane_wm_max(dev, level, config, ddb_partitioning, true);
2163 max->cur = ilk_cursor_wm_max(dev, level, config);
Ville Syrjälä4e975082014-03-07 18:32:11 +02002164 max->fbc = ilk_fbc_wm_reg_max(dev);
Ville Syrjälä158ae642013-08-07 13:28:19 +03002165}
2166
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002167static void ilk_compute_wm_reg_maximums(struct drm_device *dev,
2168 int level,
2169 struct ilk_wm_maximums *max)
2170{
2171 max->pri = ilk_plane_wm_reg_max(dev, level, false);
2172 max->spr = ilk_plane_wm_reg_max(dev, level, true);
2173 max->cur = ilk_cursor_wm_reg_max(dev, level);
2174 max->fbc = ilk_fbc_wm_reg_max(dev);
2175}
2176
Ville Syrjäläd9395652013-10-09 19:18:10 +03002177static bool ilk_validate_wm_level(int level,
Imre Deak820c1982013-12-17 14:46:36 +02002178 const struct ilk_wm_maximums *max,
Ville Syrjäläd9395652013-10-09 19:18:10 +03002179 struct intel_wm_level *result)
Ville Syrjäläa9786a12013-08-07 13:24:47 +03002180{
2181 bool ret;
2182
2183 /* already determined to be invalid? */
2184 if (!result->enable)
2185 return false;
2186
2187 result->enable = result->pri_val <= max->pri &&
2188 result->spr_val <= max->spr &&
2189 result->cur_val <= max->cur;
2190
2191 ret = result->enable;
2192
2193 /*
2194 * HACK until we can pre-compute everything,
2195 * and thus fail gracefully if LP0 watermarks
2196 * are exceeded...
2197 */
2198 if (level == 0 && !result->enable) {
2199 if (result->pri_val > max->pri)
2200 DRM_DEBUG_KMS("Primary WM%d too large %u (max %u)\n",
2201 level, result->pri_val, max->pri);
2202 if (result->spr_val > max->spr)
2203 DRM_DEBUG_KMS("Sprite WM%d too large %u (max %u)\n",
2204 level, result->spr_val, max->spr);
2205 if (result->cur_val > max->cur)
2206 DRM_DEBUG_KMS("Cursor WM%d too large %u (max %u)\n",
2207 level, result->cur_val, max->cur);
2208
2209 result->pri_val = min_t(uint32_t, result->pri_val, max->pri);
2210 result->spr_val = min_t(uint32_t, result->spr_val, max->spr);
2211 result->cur_val = min_t(uint32_t, result->cur_val, max->cur);
2212 result->enable = true;
2213 }
2214
Ville Syrjäläa9786a12013-08-07 13:24:47 +03002215 return ret;
2216}
2217
Damien Lespiaud34ff9c2014-01-06 19:17:23 +00002218static void ilk_compute_wm_level(const struct drm_i915_private *dev_priv,
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03002219 int level,
Imre Deak820c1982013-12-17 14:46:36 +02002220 const struct ilk_pipe_wm_parameters *p,
Ville Syrjälä1fd527c2013-08-06 22:24:05 +03002221 struct intel_wm_level *result)
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03002222{
2223 uint16_t pri_latency = dev_priv->wm.pri_latency[level];
2224 uint16_t spr_latency = dev_priv->wm.spr_latency[level];
2225 uint16_t cur_latency = dev_priv->wm.cur_latency[level];
2226
2227 /* WM1+ latency values stored in 0.5us units */
2228 if (level > 0) {
2229 pri_latency *= 5;
2230 spr_latency *= 5;
2231 cur_latency *= 5;
2232 }
2233
2234 result->pri_val = ilk_compute_pri_wm(p, pri_latency, level);
2235 result->spr_val = ilk_compute_spr_wm(p, spr_latency);
2236 result->cur_val = ilk_compute_cur_wm(p, cur_latency);
2237 result->fbc_val = ilk_compute_fbc_wm(p, result->pri_val);
2238 result->enable = true;
2239}
2240
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002241static uint32_t
2242hsw_compute_linetime_wm(struct drm_device *dev, struct drm_crtc *crtc)
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002243{
2244 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002245 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002246 struct drm_display_mode *mode = &intel_crtc->config.adjusted_mode;
Paulo Zanoni85a02de2013-05-03 17:23:43 -03002247 u32 linetime, ips_linetime;
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002248
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002249 if (!intel_crtc_active(crtc))
2250 return 0;
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002251
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002252 /* The WM are computed with base on how long it takes to fill a single
2253 * row at the given clock rate, multiplied by 8.
2254 * */
Jesse Barnesfec8cba2013-11-27 11:10:26 -08002255 linetime = DIV_ROUND_CLOSEST(mode->crtc_htotal * 1000 * 8,
2256 mode->crtc_clock);
2257 ips_linetime = DIV_ROUND_CLOSEST(mode->crtc_htotal * 1000 * 8,
Paulo Zanoni85a02de2013-05-03 17:23:43 -03002258 intel_ddi_get_cdclk_freq(dev_priv));
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002259
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002260 return PIPE_WM_LINETIME_IPS_LINETIME(ips_linetime) |
2261 PIPE_WM_LINETIME_TIME(linetime);
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002262}
2263
Ville Syrjälä12b134d2013-07-05 11:57:21 +03002264static void intel_read_wm_latency(struct drm_device *dev, uint16_t wm[5])
2265{
2266 struct drm_i915_private *dev_priv = dev->dev_private;
2267
Ville Syrjäläa42a5712014-01-07 16:14:08 +02002268 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Ville Syrjälä12b134d2013-07-05 11:57:21 +03002269 uint64_t sskpd = I915_READ64(MCH_SSKPD);
2270
2271 wm[0] = (sskpd >> 56) & 0xFF;
2272 if (wm[0] == 0)
2273 wm[0] = sskpd & 0xF;
Ville Syrjäläe5d50192013-07-05 11:57:22 +03002274 wm[1] = (sskpd >> 4) & 0xFF;
2275 wm[2] = (sskpd >> 12) & 0xFF;
2276 wm[3] = (sskpd >> 20) & 0x1FF;
2277 wm[4] = (sskpd >> 32) & 0x1FF;
Ville Syrjälä63cf9a12013-07-05 11:57:23 +03002278 } else if (INTEL_INFO(dev)->gen >= 6) {
2279 uint32_t sskpd = I915_READ(MCH_SSKPD);
2280
2281 wm[0] = (sskpd >> SSKPD_WM0_SHIFT) & SSKPD_WM_MASK;
2282 wm[1] = (sskpd >> SSKPD_WM1_SHIFT) & SSKPD_WM_MASK;
2283 wm[2] = (sskpd >> SSKPD_WM2_SHIFT) & SSKPD_WM_MASK;
2284 wm[3] = (sskpd >> SSKPD_WM3_SHIFT) & SSKPD_WM_MASK;
Ville Syrjälä3a88d0a2013-08-01 16:18:49 +03002285 } else if (INTEL_INFO(dev)->gen >= 5) {
2286 uint32_t mltr = I915_READ(MLTR_ILK);
2287
2288 /* ILK primary LP0 latency is 700 ns */
2289 wm[0] = 7;
2290 wm[1] = (mltr >> MLTR_WM1_SHIFT) & ILK_SRLT_MASK;
2291 wm[2] = (mltr >> MLTR_WM2_SHIFT) & ILK_SRLT_MASK;
Ville Syrjälä12b134d2013-07-05 11:57:21 +03002292 }
2293}
2294
Ville Syrjälä53615a52013-08-01 16:18:50 +03002295static void intel_fixup_spr_wm_latency(struct drm_device *dev, uint16_t wm[5])
2296{
2297 /* ILK sprite LP0 latency is 1300 ns */
2298 if (INTEL_INFO(dev)->gen == 5)
2299 wm[0] = 13;
2300}
2301
2302static void intel_fixup_cur_wm_latency(struct drm_device *dev, uint16_t wm[5])
2303{
2304 /* ILK cursor LP0 latency is 1300 ns */
2305 if (INTEL_INFO(dev)->gen == 5)
2306 wm[0] = 13;
2307
2308 /* WaDoubleCursorLP3Latency:ivb */
2309 if (IS_IVYBRIDGE(dev))
2310 wm[3] *= 2;
2311}
2312
Damien Lespiau546c81f2014-05-13 15:30:26 +01002313int ilk_wm_max_level(const struct drm_device *dev)
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03002314{
2315 /* how many WM levels are we expecting */
Ville Syrjäläa42a5712014-01-07 16:14:08 +02002316 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03002317 return 4;
2318 else if (INTEL_INFO(dev)->gen >= 6)
2319 return 3;
2320 else
2321 return 2;
2322}
Daniel Vetter7526ed72014-09-29 15:07:19 +02002323
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002324static void intel_print_wm_latency(struct drm_device *dev,
2325 const char *name,
2326 const uint16_t wm[5])
2327{
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03002328 int level, max_level = ilk_wm_max_level(dev);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002329
2330 for (level = 0; level <= max_level; level++) {
2331 unsigned int latency = wm[level];
2332
2333 if (latency == 0) {
2334 DRM_ERROR("%s WM%d latency not provided\n",
2335 name, level);
2336 continue;
2337 }
2338
2339 /* WM1+ latency values in 0.5us units */
2340 if (level > 0)
2341 latency *= 5;
2342
2343 DRM_DEBUG_KMS("%s WM%d latency %u (%u.%u usec)\n",
2344 name, level, wm[level],
2345 latency / 10, latency % 10);
2346 }
2347}
2348
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002349static bool ilk_increase_wm_latency(struct drm_i915_private *dev_priv,
2350 uint16_t wm[5], uint16_t min)
2351{
2352 int level, max_level = ilk_wm_max_level(dev_priv->dev);
2353
2354 if (wm[0] >= min)
2355 return false;
2356
2357 wm[0] = max(wm[0], min);
2358 for (level = 1; level <= max_level; level++)
2359 wm[level] = max_t(uint16_t, wm[level], DIV_ROUND_UP(min, 5));
2360
2361 return true;
2362}
2363
2364static void snb_wm_latency_quirk(struct drm_device *dev)
2365{
2366 struct drm_i915_private *dev_priv = dev->dev_private;
2367 bool changed;
2368
2369 /*
2370 * The BIOS provided WM memory latency values are often
2371 * inadequate for high resolution displays. Adjust them.
2372 */
2373 changed = ilk_increase_wm_latency(dev_priv, dev_priv->wm.pri_latency, 12) |
2374 ilk_increase_wm_latency(dev_priv, dev_priv->wm.spr_latency, 12) |
2375 ilk_increase_wm_latency(dev_priv, dev_priv->wm.cur_latency, 12);
2376
2377 if (!changed)
2378 return;
2379
2380 DRM_DEBUG_KMS("WM latency values increased to avoid potential underruns\n");
2381 intel_print_wm_latency(dev, "Primary", dev_priv->wm.pri_latency);
2382 intel_print_wm_latency(dev, "Sprite", dev_priv->wm.spr_latency);
2383 intel_print_wm_latency(dev, "Cursor", dev_priv->wm.cur_latency);
2384}
2385
Damien Lespiaufa50ad62014-03-17 18:01:16 +00002386static void ilk_setup_wm_latency(struct drm_device *dev)
Ville Syrjälä53615a52013-08-01 16:18:50 +03002387{
2388 struct drm_i915_private *dev_priv = dev->dev_private;
2389
2390 intel_read_wm_latency(dev, dev_priv->wm.pri_latency);
2391
2392 memcpy(dev_priv->wm.spr_latency, dev_priv->wm.pri_latency,
2393 sizeof(dev_priv->wm.pri_latency));
2394 memcpy(dev_priv->wm.cur_latency, dev_priv->wm.pri_latency,
2395 sizeof(dev_priv->wm.pri_latency));
2396
2397 intel_fixup_spr_wm_latency(dev, dev_priv->wm.spr_latency);
2398 intel_fixup_cur_wm_latency(dev, dev_priv->wm.cur_latency);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002399
2400 intel_print_wm_latency(dev, "Primary", dev_priv->wm.pri_latency);
2401 intel_print_wm_latency(dev, "Sprite", dev_priv->wm.spr_latency);
2402 intel_print_wm_latency(dev, "Cursor", dev_priv->wm.cur_latency);
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002403
2404 if (IS_GEN6(dev))
2405 snb_wm_latency_quirk(dev);
Ville Syrjälä53615a52013-08-01 16:18:50 +03002406}
2407
Imre Deak820c1982013-12-17 14:46:36 +02002408static void ilk_compute_wm_parameters(struct drm_crtc *crtc,
Ville Syrjälä2a44b762014-03-07 18:32:09 +02002409 struct ilk_pipe_wm_parameters *p)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002410{
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03002411 struct drm_device *dev = crtc->dev;
2412 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2413 enum pipe pipe = intel_crtc->pipe;
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03002414 struct drm_plane *plane;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002415
Ville Syrjälä2a44b762014-03-07 18:32:09 +02002416 if (!intel_crtc_active(crtc))
2417 return;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002418
Ville Syrjälä2a44b762014-03-07 18:32:09 +02002419 p->active = true;
2420 p->pipe_htotal = intel_crtc->config.adjusted_mode.crtc_htotal;
2421 p->pixel_rate = ilk_pipe_pixel_rate(dev, crtc);
2422 p->pri.bytes_per_pixel = crtc->primary->fb->bits_per_pixel / 8;
2423 p->cur.bytes_per_pixel = 4;
2424 p->pri.horiz_pixels = intel_crtc->config.pipe_src_w;
2425 p->cur.horiz_pixels = intel_crtc->cursor_width;
2426 /* TODO: for now, assume primary and cursor planes are always enabled. */
2427 p->pri.enabled = true;
2428 p->cur.enabled = true;
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03002429
Matt Roperaf2b6532014-04-01 15:22:32 -07002430 drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002431 struct intel_plane *intel_plane = to_intel_plane(plane);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002432
Ville Syrjälä2a44b762014-03-07 18:32:09 +02002433 if (intel_plane->pipe == pipe) {
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03002434 p->spr = intel_plane->wm;
Ville Syrjälä2a44b762014-03-07 18:32:09 +02002435 break;
2436 }
2437 }
2438}
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002439
Ville Syrjälä2a44b762014-03-07 18:32:09 +02002440static void ilk_compute_wm_config(struct drm_device *dev,
2441 struct intel_wm_config *config)
2442{
2443 struct intel_crtc *intel_crtc;
2444
2445 /* Compute the currently _active_ config */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01002446 for_each_intel_crtc(dev, intel_crtc) {
Ville Syrjälä2a44b762014-03-07 18:32:09 +02002447 const struct intel_pipe_wm *wm = &intel_crtc->wm.active;
2448
2449 if (!wm->pipe_enabled)
2450 continue;
2451
2452 config->sprites_enabled |= wm->sprites_enabled;
2453 config->sprites_scaled |= wm->sprites_scaled;
2454 config->num_pipes_active++;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002455 }
2456}
2457
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002458/* Compute new watermarks for the pipe */
2459static bool intel_compute_pipe_wm(struct drm_crtc *crtc,
Imre Deak820c1982013-12-17 14:46:36 +02002460 const struct ilk_pipe_wm_parameters *params,
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002461 struct intel_pipe_wm *pipe_wm)
2462{
2463 struct drm_device *dev = crtc->dev;
Damien Lespiaud34ff9c2014-01-06 19:17:23 +00002464 const struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002465 int level, max_level = ilk_wm_max_level(dev);
2466 /* LP0 watermark maximums depend on this pipe alone */
2467 struct intel_wm_config config = {
2468 .num_pipes_active = 1,
2469 .sprites_enabled = params->spr.enabled,
2470 .sprites_scaled = params->spr.scaled,
2471 };
Imre Deak820c1982013-12-17 14:46:36 +02002472 struct ilk_wm_maximums max;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002473
Ville Syrjälä2a44b762014-03-07 18:32:09 +02002474 pipe_wm->pipe_enabled = params->active;
2475 pipe_wm->sprites_enabled = params->spr.enabled;
2476 pipe_wm->sprites_scaled = params->spr.scaled;
2477
Ville Syrjälä7b39a0b2013-12-05 15:51:30 +02002478 /* ILK/SNB: LP2+ watermarks only w/o sprites */
2479 if (INTEL_INFO(dev)->gen <= 6 && params->spr.enabled)
2480 max_level = 1;
2481
2482 /* ILK/SNB/IVB: LP1+ watermarks only w/o scaling */
2483 if (params->spr.scaled)
2484 max_level = 0;
2485
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002486 ilk_compute_wm_level(dev_priv, 0, params, &pipe_wm->wm[0]);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002487
Ville Syrjäläa42a5712014-01-07 16:14:08 +02002488 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Ville Syrjäläce0e0712013-12-05 15:51:36 +02002489 pipe_wm->linetime = hsw_compute_linetime_wm(dev, crtc);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002490
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002491 /* LP0 watermarks always use 1/2 DDB partitioning */
2492 ilk_compute_wm_maximums(dev, 0, &config, INTEL_DDB_PART_1_2, &max);
2493
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002494 /* At least LP0 must be valid */
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002495 if (!ilk_validate_wm_level(0, &max, &pipe_wm->wm[0]))
2496 return false;
2497
2498 ilk_compute_wm_reg_maximums(dev, 1, &max);
2499
2500 for (level = 1; level <= max_level; level++) {
2501 struct intel_wm_level wm = {};
2502
2503 ilk_compute_wm_level(dev_priv, level, params, &wm);
2504
2505 /*
2506 * Disable any watermark level that exceeds the
2507 * register maximums since such watermarks are
2508 * always invalid.
2509 */
2510 if (!ilk_validate_wm_level(level, &max, &wm))
2511 break;
2512
2513 pipe_wm->wm[level] = wm;
2514 }
2515
2516 return true;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002517}
2518
2519/*
2520 * Merge the watermarks from all active pipes for a specific level.
2521 */
2522static void ilk_merge_wm_level(struct drm_device *dev,
2523 int level,
2524 struct intel_wm_level *ret_wm)
2525{
2526 const struct intel_crtc *intel_crtc;
2527
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002528 ret_wm->enable = true;
2529
Damien Lespiaud3fcc802014-05-13 23:32:22 +01002530 for_each_intel_crtc(dev, intel_crtc) {
Ville Syrjäläfe392ef2014-03-07 18:32:10 +02002531 const struct intel_pipe_wm *active = &intel_crtc->wm.active;
2532 const struct intel_wm_level *wm = &active->wm[level];
2533
2534 if (!active->pipe_enabled)
2535 continue;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002536
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002537 /*
2538 * The watermark values may have been used in the past,
2539 * so we must maintain them in the registers for some
2540 * time even if the level is now disabled.
2541 */
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002542 if (!wm->enable)
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002543 ret_wm->enable = false;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002544
2545 ret_wm->pri_val = max(ret_wm->pri_val, wm->pri_val);
2546 ret_wm->spr_val = max(ret_wm->spr_val, wm->spr_val);
2547 ret_wm->cur_val = max(ret_wm->cur_val, wm->cur_val);
2548 ret_wm->fbc_val = max(ret_wm->fbc_val, wm->fbc_val);
2549 }
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002550}
2551
2552/*
2553 * Merge all low power watermarks for all active pipes.
2554 */
2555static void ilk_wm_merge(struct drm_device *dev,
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02002556 const struct intel_wm_config *config,
Imre Deak820c1982013-12-17 14:46:36 +02002557 const struct ilk_wm_maximums *max,
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002558 struct intel_pipe_wm *merged)
2559{
2560 int level, max_level = ilk_wm_max_level(dev);
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002561 int last_enabled_level = max_level;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002562
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02002563 /* ILK/SNB/IVB: LP1+ watermarks only w/ single pipe */
2564 if ((INTEL_INFO(dev)->gen <= 6 || IS_IVYBRIDGE(dev)) &&
2565 config->num_pipes_active > 1)
2566 return;
2567
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02002568 /* ILK: FBC WM must be disabled always */
2569 merged->fbc_wm_enabled = INTEL_INFO(dev)->gen >= 6;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002570
2571 /* merge each WM1+ level */
2572 for (level = 1; level <= max_level; level++) {
2573 struct intel_wm_level *wm = &merged->wm[level];
2574
2575 ilk_merge_wm_level(dev, level, wm);
2576
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002577 if (level > last_enabled_level)
2578 wm->enable = false;
2579 else if (!ilk_validate_wm_level(level, max, wm))
2580 /* make sure all following levels get disabled */
2581 last_enabled_level = level - 1;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002582
2583 /*
2584 * The spec says it is preferred to disable
2585 * FBC WMs instead of disabling a WM level.
2586 */
2587 if (wm->fbc_val > max->fbc) {
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002588 if (wm->enable)
2589 merged->fbc_wm_enabled = false;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002590 wm->fbc_val = 0;
2591 }
2592 }
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02002593
2594 /* ILK: LP2+ must be disabled when FBC WM is disabled but FBC enabled */
2595 /*
2596 * FIXME this is racy. FBC might get enabled later.
2597 * What we should check here is whether FBC can be
2598 * enabled sometime later.
2599 */
2600 if (IS_GEN5(dev) && !merged->fbc_wm_enabled && intel_fbc_enabled(dev)) {
2601 for (level = 2; level <= max_level; level++) {
2602 struct intel_wm_level *wm = &merged->wm[level];
2603
2604 wm->enable = false;
2605 }
2606 }
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002607}
2608
Ville Syrjäläb380ca32013-10-09 19:18:01 +03002609static int ilk_wm_lp_to_level(int wm_lp, const struct intel_pipe_wm *pipe_wm)
2610{
2611 /* LP1,LP2,LP3 levels are either 1,2,3 or 1,3,4 */
2612 return wm_lp + (wm_lp >= 2 && pipe_wm->wm[4].enable);
2613}
2614
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02002615/* The value we need to program into the WM_LPx latency field */
2616static unsigned int ilk_wm_lp_latency(struct drm_device *dev, int level)
2617{
2618 struct drm_i915_private *dev_priv = dev->dev_private;
2619
Ville Syrjäläa42a5712014-01-07 16:14:08 +02002620 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02002621 return 2 * level;
2622 else
2623 return dev_priv->wm.pri_latency[level];
2624}
2625
Imre Deak820c1982013-12-17 14:46:36 +02002626static void ilk_compute_wm_results(struct drm_device *dev,
Ville Syrjälä0362c782013-10-09 19:17:57 +03002627 const struct intel_pipe_wm *merged,
Ville Syrjälä609cede2013-10-09 19:18:03 +03002628 enum intel_ddb_partitioning partitioning,
Imre Deak820c1982013-12-17 14:46:36 +02002629 struct ilk_wm_values *results)
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002630{
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002631 struct intel_crtc *intel_crtc;
2632 int level, wm_lp;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002633
Ville Syrjälä0362c782013-10-09 19:17:57 +03002634 results->enable_fbc_wm = merged->fbc_wm_enabled;
Ville Syrjälä609cede2013-10-09 19:18:03 +03002635 results->partitioning = partitioning;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002636
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002637 /* LP1+ register values */
Paulo Zanonicca32e92013-05-31 11:45:06 -03002638 for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
Ville Syrjälä1fd527c2013-08-06 22:24:05 +03002639 const struct intel_wm_level *r;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002640
Ville Syrjäläb380ca32013-10-09 19:18:01 +03002641 level = ilk_wm_lp_to_level(wm_lp, merged);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002642
Ville Syrjälä0362c782013-10-09 19:17:57 +03002643 r = &merged->wm[level];
Paulo Zanonicca32e92013-05-31 11:45:06 -03002644
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002645 /*
2646 * Maintain the watermark values even if the level is
2647 * disabled. Doing otherwise could cause underruns.
2648 */
2649 results->wm_lp[wm_lp - 1] =
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02002650 (ilk_wm_lp_latency(dev, level) << WM1_LP_LATENCY_SHIFT) |
Ville Syrjälä416f4722013-11-02 21:07:46 -07002651 (r->pri_val << WM1_LP_SR_SHIFT) |
2652 r->cur_val;
2653
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002654 if (r->enable)
2655 results->wm_lp[wm_lp - 1] |= WM1_LP_SR_EN;
2656
Ville Syrjälä416f4722013-11-02 21:07:46 -07002657 if (INTEL_INFO(dev)->gen >= 8)
2658 results->wm_lp[wm_lp - 1] |=
2659 r->fbc_val << WM1_LP_FBC_SHIFT_BDW;
2660 else
2661 results->wm_lp[wm_lp - 1] |=
2662 r->fbc_val << WM1_LP_FBC_SHIFT;
2663
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002664 /*
2665 * Always set WM1S_LP_EN when spr_val != 0, even if the
2666 * level is disabled. Doing otherwise could cause underruns.
2667 */
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02002668 if (INTEL_INFO(dev)->gen <= 6 && r->spr_val) {
2669 WARN_ON(wm_lp != 1);
2670 results->wm_lp_spr[wm_lp - 1] = WM1S_LP_EN | r->spr_val;
2671 } else
2672 results->wm_lp_spr[wm_lp - 1] = r->spr_val;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002673 }
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002674
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002675 /* LP0 register values */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01002676 for_each_intel_crtc(dev, intel_crtc) {
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002677 enum pipe pipe = intel_crtc->pipe;
2678 const struct intel_wm_level *r =
2679 &intel_crtc->wm.active.wm[0];
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002680
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002681 if (WARN_ON(!r->enable))
2682 continue;
2683
2684 results->wm_linetime[pipe] = intel_crtc->wm.active.linetime;
2685
2686 results->wm_pipe[pipe] =
2687 (r->pri_val << WM0_PIPE_PLANE_SHIFT) |
2688 (r->spr_val << WM0_PIPE_SPRITE_SHIFT) |
2689 r->cur_val;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002690 }
2691}
2692
Paulo Zanoni861f3382013-05-31 10:19:21 -03002693/* Find the result with the highest level enabled. Check for enable_fbc_wm in
2694 * case both are at the same level. Prefer r1 in case they're the same. */
Imre Deak820c1982013-12-17 14:46:36 +02002695static struct intel_pipe_wm *ilk_find_best_result(struct drm_device *dev,
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002696 struct intel_pipe_wm *r1,
2697 struct intel_pipe_wm *r2)
Paulo Zanoni861f3382013-05-31 10:19:21 -03002698{
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002699 int level, max_level = ilk_wm_max_level(dev);
2700 int level1 = 0, level2 = 0;
Paulo Zanoni861f3382013-05-31 10:19:21 -03002701
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002702 for (level = 1; level <= max_level; level++) {
2703 if (r1->wm[level].enable)
2704 level1 = level;
2705 if (r2->wm[level].enable)
2706 level2 = level;
Paulo Zanoni861f3382013-05-31 10:19:21 -03002707 }
2708
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002709 if (level1 == level2) {
2710 if (r2->fbc_wm_enabled && !r1->fbc_wm_enabled)
Paulo Zanoni861f3382013-05-31 10:19:21 -03002711 return r2;
2712 else
2713 return r1;
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002714 } else if (level1 > level2) {
Paulo Zanoni861f3382013-05-31 10:19:21 -03002715 return r1;
2716 } else {
2717 return r2;
2718 }
2719}
2720
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002721/* dirty bits used to track which watermarks need changes */
2722#define WM_DIRTY_PIPE(pipe) (1 << (pipe))
2723#define WM_DIRTY_LINETIME(pipe) (1 << (8 + (pipe)))
2724#define WM_DIRTY_LP(wm_lp) (1 << (15 + (wm_lp)))
2725#define WM_DIRTY_LP_ALL (WM_DIRTY_LP(1) | WM_DIRTY_LP(2) | WM_DIRTY_LP(3))
2726#define WM_DIRTY_FBC (1 << 24)
2727#define WM_DIRTY_DDB (1 << 25)
2728
Damien Lespiau055e3932014-08-18 13:49:10 +01002729static unsigned int ilk_compute_wm_dirty(struct drm_i915_private *dev_priv,
Imre Deak820c1982013-12-17 14:46:36 +02002730 const struct ilk_wm_values *old,
2731 const struct ilk_wm_values *new)
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002732{
2733 unsigned int dirty = 0;
2734 enum pipe pipe;
2735 int wm_lp;
2736
Damien Lespiau055e3932014-08-18 13:49:10 +01002737 for_each_pipe(dev_priv, pipe) {
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002738 if (old->wm_linetime[pipe] != new->wm_linetime[pipe]) {
2739 dirty |= WM_DIRTY_LINETIME(pipe);
2740 /* Must disable LP1+ watermarks too */
2741 dirty |= WM_DIRTY_LP_ALL;
2742 }
2743
2744 if (old->wm_pipe[pipe] != new->wm_pipe[pipe]) {
2745 dirty |= WM_DIRTY_PIPE(pipe);
2746 /* Must disable LP1+ watermarks too */
2747 dirty |= WM_DIRTY_LP_ALL;
2748 }
2749 }
2750
2751 if (old->enable_fbc_wm != new->enable_fbc_wm) {
2752 dirty |= WM_DIRTY_FBC;
2753 /* Must disable LP1+ watermarks too */
2754 dirty |= WM_DIRTY_LP_ALL;
2755 }
2756
2757 if (old->partitioning != new->partitioning) {
2758 dirty |= WM_DIRTY_DDB;
2759 /* Must disable LP1+ watermarks too */
2760 dirty |= WM_DIRTY_LP_ALL;
2761 }
2762
2763 /* LP1+ watermarks already deemed dirty, no need to continue */
2764 if (dirty & WM_DIRTY_LP_ALL)
2765 return dirty;
2766
2767 /* Find the lowest numbered LP1+ watermark in need of an update... */
2768 for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
2769 if (old->wm_lp[wm_lp - 1] != new->wm_lp[wm_lp - 1] ||
2770 old->wm_lp_spr[wm_lp - 1] != new->wm_lp_spr[wm_lp - 1])
2771 break;
2772 }
2773
2774 /* ...and mark it and all higher numbered LP1+ watermarks as dirty */
2775 for (; wm_lp <= 3; wm_lp++)
2776 dirty |= WM_DIRTY_LP(wm_lp);
2777
2778 return dirty;
2779}
2780
Ville Syrjälä8553c182013-12-05 15:51:39 +02002781static bool _ilk_disable_lp_wm(struct drm_i915_private *dev_priv,
2782 unsigned int dirty)
2783{
Imre Deak820c1982013-12-17 14:46:36 +02002784 struct ilk_wm_values *previous = &dev_priv->wm.hw;
Ville Syrjälä8553c182013-12-05 15:51:39 +02002785 bool changed = false;
2786
2787 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] & WM1_LP_SR_EN) {
2788 previous->wm_lp[2] &= ~WM1_LP_SR_EN;
2789 I915_WRITE(WM3_LP_ILK, previous->wm_lp[2]);
2790 changed = true;
2791 }
2792 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] & WM1_LP_SR_EN) {
2793 previous->wm_lp[1] &= ~WM1_LP_SR_EN;
2794 I915_WRITE(WM2_LP_ILK, previous->wm_lp[1]);
2795 changed = true;
2796 }
2797 if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] & WM1_LP_SR_EN) {
2798 previous->wm_lp[0] &= ~WM1_LP_SR_EN;
2799 I915_WRITE(WM1_LP_ILK, previous->wm_lp[0]);
2800 changed = true;
2801 }
2802
2803 /*
2804 * Don't touch WM1S_LP_EN here.
2805 * Doing so could cause underruns.
2806 */
2807
2808 return changed;
2809}
2810
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002811/*
2812 * The spec says we shouldn't write when we don't need, because every write
2813 * causes WMs to be re-evaluated, expending some power.
2814 */
Imre Deak820c1982013-12-17 14:46:36 +02002815static void ilk_write_wm_values(struct drm_i915_private *dev_priv,
2816 struct ilk_wm_values *results)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002817{
Ville Syrjäläac9545f2013-12-05 15:51:28 +02002818 struct drm_device *dev = dev_priv->dev;
Imre Deak820c1982013-12-17 14:46:36 +02002819 struct ilk_wm_values *previous = &dev_priv->wm.hw;
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002820 unsigned int dirty;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002821 uint32_t val;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002822
Damien Lespiau055e3932014-08-18 13:49:10 +01002823 dirty = ilk_compute_wm_dirty(dev_priv, previous, results);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002824 if (!dirty)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002825 return;
2826
Ville Syrjälä8553c182013-12-05 15:51:39 +02002827 _ilk_disable_lp_wm(dev_priv, dirty);
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02002828
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002829 if (dirty & WM_DIRTY_PIPE(PIPE_A))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002830 I915_WRITE(WM0_PIPEA_ILK, results->wm_pipe[0]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002831 if (dirty & WM_DIRTY_PIPE(PIPE_B))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002832 I915_WRITE(WM0_PIPEB_ILK, results->wm_pipe[1]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002833 if (dirty & WM_DIRTY_PIPE(PIPE_C))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002834 I915_WRITE(WM0_PIPEC_IVB, results->wm_pipe[2]);
2835
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002836 if (dirty & WM_DIRTY_LINETIME(PIPE_A))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002837 I915_WRITE(PIPE_WM_LINETIME(PIPE_A), results->wm_linetime[0]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002838 if (dirty & WM_DIRTY_LINETIME(PIPE_B))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002839 I915_WRITE(PIPE_WM_LINETIME(PIPE_B), results->wm_linetime[1]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002840 if (dirty & WM_DIRTY_LINETIME(PIPE_C))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002841 I915_WRITE(PIPE_WM_LINETIME(PIPE_C), results->wm_linetime[2]);
2842
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002843 if (dirty & WM_DIRTY_DDB) {
Ville Syrjäläa42a5712014-01-07 16:14:08 +02002844 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Ville Syrjäläac9545f2013-12-05 15:51:28 +02002845 val = I915_READ(WM_MISC);
2846 if (results->partitioning == INTEL_DDB_PART_1_2)
2847 val &= ~WM_MISC_DATA_PARTITION_5_6;
2848 else
2849 val |= WM_MISC_DATA_PARTITION_5_6;
2850 I915_WRITE(WM_MISC, val);
2851 } else {
2852 val = I915_READ(DISP_ARB_CTL2);
2853 if (results->partitioning == INTEL_DDB_PART_1_2)
2854 val &= ~DISP_DATA_PARTITION_5_6;
2855 else
2856 val |= DISP_DATA_PARTITION_5_6;
2857 I915_WRITE(DISP_ARB_CTL2, val);
2858 }
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002859 }
2860
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002861 if (dirty & WM_DIRTY_FBC) {
Paulo Zanonicca32e92013-05-31 11:45:06 -03002862 val = I915_READ(DISP_ARB_CTL);
2863 if (results->enable_fbc_wm)
2864 val &= ~DISP_FBC_WM_DIS;
2865 else
2866 val |= DISP_FBC_WM_DIS;
2867 I915_WRITE(DISP_ARB_CTL, val);
2868 }
2869
Imre Deak954911e2013-12-17 14:46:34 +02002870 if (dirty & WM_DIRTY_LP(1) &&
2871 previous->wm_lp_spr[0] != results->wm_lp_spr[0])
2872 I915_WRITE(WM1S_LP_ILK, results->wm_lp_spr[0]);
2873
2874 if (INTEL_INFO(dev)->gen >= 7) {
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02002875 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp_spr[1] != results->wm_lp_spr[1])
2876 I915_WRITE(WM2S_LP_IVB, results->wm_lp_spr[1]);
2877 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp_spr[2] != results->wm_lp_spr[2])
2878 I915_WRITE(WM3S_LP_IVB, results->wm_lp_spr[2]);
2879 }
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002880
Ville Syrjäläfacd6192013-12-05 15:51:33 +02002881 if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] != results->wm_lp[0])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002882 I915_WRITE(WM1_LP_ILK, results->wm_lp[0]);
Ville Syrjäläfacd6192013-12-05 15:51:33 +02002883 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] != results->wm_lp[1])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002884 I915_WRITE(WM2_LP_ILK, results->wm_lp[1]);
Ville Syrjäläfacd6192013-12-05 15:51:33 +02002885 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] != results->wm_lp[2])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002886 I915_WRITE(WM3_LP_ILK, results->wm_lp[2]);
Ville Syrjälä609cede2013-10-09 19:18:03 +03002887
2888 dev_priv->wm.hw = *results;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002889}
2890
Ville Syrjälä8553c182013-12-05 15:51:39 +02002891static bool ilk_disable_lp_wm(struct drm_device *dev)
2892{
2893 struct drm_i915_private *dev_priv = dev->dev_private;
2894
2895 return _ilk_disable_lp_wm(dev_priv, WM_DIRTY_LP_ALL);
2896}
2897
Imre Deak820c1982013-12-17 14:46:36 +02002898static void ilk_update_wm(struct drm_crtc *crtc)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002899{
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03002900 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjälä46ba6142013-09-10 11:40:40 +03002901 struct drm_device *dev = crtc->dev;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002902 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak820c1982013-12-17 14:46:36 +02002903 struct ilk_wm_maximums max;
2904 struct ilk_pipe_wm_parameters params = {};
2905 struct ilk_wm_values results = {};
Ville Syrjälä77c122b2013-08-06 22:24:04 +03002906 enum intel_ddb_partitioning partitioning;
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03002907 struct intel_pipe_wm pipe_wm = {};
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002908 struct intel_pipe_wm lp_wm_1_2 = {}, lp_wm_5_6 = {}, *best_lp_wm;
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03002909 struct intel_wm_config config = {};
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002910
Ville Syrjälä2a44b762014-03-07 18:32:09 +02002911 ilk_compute_wm_parameters(crtc, &params);
Paulo Zanoni861f3382013-05-31 10:19:21 -03002912
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03002913 intel_compute_pipe_wm(crtc, &params, &pipe_wm);
2914
2915 if (!memcmp(&intel_crtc->wm.active, &pipe_wm, sizeof(pipe_wm)))
2916 return;
2917
2918 intel_crtc->wm.active = pipe_wm;
2919
Ville Syrjälä2a44b762014-03-07 18:32:09 +02002920 ilk_compute_wm_config(dev, &config);
2921
Ville Syrjälä34982fe2013-10-09 19:18:09 +03002922 ilk_compute_wm_maximums(dev, 1, &config, INTEL_DDB_PART_1_2, &max);
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02002923 ilk_wm_merge(dev, &config, &max, &lp_wm_1_2);
Ville Syrjälä0362c782013-10-09 19:17:57 +03002924
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03002925 /* 5/6 split only in single pipe config on IVB+ */
Ville Syrjäläec98c8d2013-10-11 15:26:26 +03002926 if (INTEL_INFO(dev)->gen >= 7 &&
2927 config.num_pipes_active == 1 && config.sprites_enabled) {
Ville Syrjälä34982fe2013-10-09 19:18:09 +03002928 ilk_compute_wm_maximums(dev, 1, &config, INTEL_DDB_PART_5_6, &max);
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02002929 ilk_wm_merge(dev, &config, &max, &lp_wm_5_6);
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03002930
Imre Deak820c1982013-12-17 14:46:36 +02002931 best_lp_wm = ilk_find_best_result(dev, &lp_wm_1_2, &lp_wm_5_6);
Paulo Zanoni861f3382013-05-31 10:19:21 -03002932 } else {
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002933 best_lp_wm = &lp_wm_1_2;
Paulo Zanoni861f3382013-05-31 10:19:21 -03002934 }
2935
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002936 partitioning = (best_lp_wm == &lp_wm_1_2) ?
Ville Syrjälä77c122b2013-08-06 22:24:04 +03002937 INTEL_DDB_PART_1_2 : INTEL_DDB_PART_5_6;
Paulo Zanoni861f3382013-05-31 10:19:21 -03002938
Imre Deak820c1982013-12-17 14:46:36 +02002939 ilk_compute_wm_results(dev, best_lp_wm, partitioning, &results);
Ville Syrjälä609cede2013-10-09 19:18:03 +03002940
Imre Deak820c1982013-12-17 14:46:36 +02002941 ilk_write_wm_values(dev_priv, &results);
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002942}
2943
Damien Lespiaued57cb82014-07-15 09:21:24 +02002944static void
2945ilk_update_sprite_wm(struct drm_plane *plane,
2946 struct drm_crtc *crtc,
2947 uint32_t sprite_width, uint32_t sprite_height,
2948 int pixel_size, bool enabled, bool scaled)
Paulo Zanoni526682e2013-05-24 11:59:18 -03002949{
Ville Syrjälä8553c182013-12-05 15:51:39 +02002950 struct drm_device *dev = plane->dev;
Ville Syrjäläadf3d352013-08-06 22:24:11 +03002951 struct intel_plane *intel_plane = to_intel_plane(plane);
Paulo Zanoni526682e2013-05-24 11:59:18 -03002952
Ville Syrjäläadf3d352013-08-06 22:24:11 +03002953 intel_plane->wm.enabled = enabled;
2954 intel_plane->wm.scaled = scaled;
2955 intel_plane->wm.horiz_pixels = sprite_width;
Damien Lespiaued57cb82014-07-15 09:21:24 +02002956 intel_plane->wm.vert_pixels = sprite_width;
Ville Syrjäläadf3d352013-08-06 22:24:11 +03002957 intel_plane->wm.bytes_per_pixel = pixel_size;
Paulo Zanoni526682e2013-05-24 11:59:18 -03002958
Ville Syrjälä8553c182013-12-05 15:51:39 +02002959 /*
2960 * IVB workaround: must disable low power watermarks for at least
2961 * one frame before enabling scaling. LP watermarks can be re-enabled
2962 * when scaling is disabled.
2963 *
2964 * WaCxSRDisabledForSpriteScaling:ivb
2965 */
2966 if (IS_IVYBRIDGE(dev) && scaled && ilk_disable_lp_wm(dev))
2967 intel_wait_for_vblank(dev, intel_plane->pipe);
2968
Imre Deak820c1982013-12-17 14:46:36 +02002969 ilk_update_wm(crtc);
Paulo Zanoni526682e2013-05-24 11:59:18 -03002970}
2971
Ville Syrjälä243e6a42013-10-14 14:55:24 +03002972static void ilk_pipe_wm_get_hw_state(struct drm_crtc *crtc)
2973{
2974 struct drm_device *dev = crtc->dev;
2975 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak820c1982013-12-17 14:46:36 +02002976 struct ilk_wm_values *hw = &dev_priv->wm.hw;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03002977 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2978 struct intel_pipe_wm *active = &intel_crtc->wm.active;
2979 enum pipe pipe = intel_crtc->pipe;
2980 static const unsigned int wm0_pipe_reg[] = {
2981 [PIPE_A] = WM0_PIPEA_ILK,
2982 [PIPE_B] = WM0_PIPEB_ILK,
2983 [PIPE_C] = WM0_PIPEC_IVB,
2984 };
2985
2986 hw->wm_pipe[pipe] = I915_READ(wm0_pipe_reg[pipe]);
Ville Syrjäläa42a5712014-01-07 16:14:08 +02002987 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Ville Syrjäläce0e0712013-12-05 15:51:36 +02002988 hw->wm_linetime[pipe] = I915_READ(PIPE_WM_LINETIME(pipe));
Ville Syrjälä243e6a42013-10-14 14:55:24 +03002989
Ville Syrjälä2a44b762014-03-07 18:32:09 +02002990 active->pipe_enabled = intel_crtc_active(crtc);
2991
2992 if (active->pipe_enabled) {
Ville Syrjälä243e6a42013-10-14 14:55:24 +03002993 u32 tmp = hw->wm_pipe[pipe];
2994
2995 /*
2996 * For active pipes LP0 watermark is marked as
2997 * enabled, and LP1+ watermaks as disabled since
2998 * we can't really reverse compute them in case
2999 * multiple pipes are active.
3000 */
3001 active->wm[0].enable = true;
3002 active->wm[0].pri_val = (tmp & WM0_PIPE_PLANE_MASK) >> WM0_PIPE_PLANE_SHIFT;
3003 active->wm[0].spr_val = (tmp & WM0_PIPE_SPRITE_MASK) >> WM0_PIPE_SPRITE_SHIFT;
3004 active->wm[0].cur_val = tmp & WM0_PIPE_CURSOR_MASK;
3005 active->linetime = hw->wm_linetime[pipe];
3006 } else {
3007 int level, max_level = ilk_wm_max_level(dev);
3008
3009 /*
3010 * For inactive pipes, all watermark levels
3011 * should be marked as enabled but zeroed,
3012 * which is what we'd compute them to.
3013 */
3014 for (level = 0; level <= max_level; level++)
3015 active->wm[level].enable = true;
3016 }
3017}
3018
3019void ilk_wm_get_hw_state(struct drm_device *dev)
3020{
3021 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak820c1982013-12-17 14:46:36 +02003022 struct ilk_wm_values *hw = &dev_priv->wm.hw;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03003023 struct drm_crtc *crtc;
3024
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01003025 for_each_crtc(dev, crtc)
Ville Syrjälä243e6a42013-10-14 14:55:24 +03003026 ilk_pipe_wm_get_hw_state(crtc);
3027
3028 hw->wm_lp[0] = I915_READ(WM1_LP_ILK);
3029 hw->wm_lp[1] = I915_READ(WM2_LP_ILK);
3030 hw->wm_lp[2] = I915_READ(WM3_LP_ILK);
3031
3032 hw->wm_lp_spr[0] = I915_READ(WM1S_LP_ILK);
Ville Syrjäläcfa76982014-03-07 18:32:08 +02003033 if (INTEL_INFO(dev)->gen >= 7) {
3034 hw->wm_lp_spr[1] = I915_READ(WM2S_LP_IVB);
3035 hw->wm_lp_spr[2] = I915_READ(WM3S_LP_IVB);
3036 }
Ville Syrjälä243e6a42013-10-14 14:55:24 +03003037
Ville Syrjäläa42a5712014-01-07 16:14:08 +02003038 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Ville Syrjäläac9545f2013-12-05 15:51:28 +02003039 hw->partitioning = (I915_READ(WM_MISC) & WM_MISC_DATA_PARTITION_5_6) ?
3040 INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
3041 else if (IS_IVYBRIDGE(dev))
3042 hw->partitioning = (I915_READ(DISP_ARB_CTL2) & DISP_DATA_PARTITION_5_6) ?
3043 INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03003044
3045 hw->enable_fbc_wm =
3046 !(I915_READ(DISP_ARB_CTL) & DISP_FBC_WM_DIS);
3047}
3048
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03003049/**
3050 * intel_update_watermarks - update FIFO watermark values based on current modes
3051 *
3052 * Calculate watermark values for the various WM regs based on current mode
3053 * and plane configuration.
3054 *
3055 * There are several cases to deal with here:
3056 * - normal (i.e. non-self-refresh)
3057 * - self-refresh (SR) mode
3058 * - lines are large relative to FIFO size (buffer can hold up to 2)
3059 * - lines are small relative to FIFO size (buffer can hold more than 2
3060 * lines), so need to account for TLB latency
3061 *
3062 * The normal calculation is:
3063 * watermark = dotclock * bytes per pixel * latency
3064 * where latency is platform & configuration dependent (we assume pessimal
3065 * values here).
3066 *
3067 * The SR calculation is:
3068 * watermark = (trunc(latency/line time)+1) * surface width *
3069 * bytes per pixel
3070 * where
3071 * line time = htotal / dotclock
3072 * surface width = hdisplay for normal plane and 64 for cursor
3073 * and latency is assumed to be high, as above.
3074 *
3075 * The final value programmed to the register should always be rounded up,
3076 * and include an extra 2 entries to account for clock crossings.
3077 *
3078 * We don't use the sprite, so we can ignore that. And on Crestline we have
3079 * to set the non-SR watermarks to 8.
3080 */
Ville Syrjälä46ba6142013-09-10 11:40:40 +03003081void intel_update_watermarks(struct drm_crtc *crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03003082{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03003083 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03003084
3085 if (dev_priv->display.update_wm)
Ville Syrjälä46ba6142013-09-10 11:40:40 +03003086 dev_priv->display.update_wm(crtc);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03003087}
3088
Ville Syrjäläadf3d352013-08-06 22:24:11 +03003089void intel_update_sprite_watermarks(struct drm_plane *plane,
3090 struct drm_crtc *crtc,
Damien Lespiaued57cb82014-07-15 09:21:24 +02003091 uint32_t sprite_width,
3092 uint32_t sprite_height,
3093 int pixel_size,
Ville Syrjälä39db4a42013-08-06 22:24:00 +03003094 bool enabled, bool scaled)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03003095{
Ville Syrjäläadf3d352013-08-06 22:24:11 +03003096 struct drm_i915_private *dev_priv = plane->dev->dev_private;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03003097
3098 if (dev_priv->display.update_sprite_wm)
Damien Lespiaued57cb82014-07-15 09:21:24 +02003099 dev_priv->display.update_sprite_wm(plane, crtc,
3100 sprite_width, sprite_height,
Ville Syrjälä39db4a42013-08-06 22:24:00 +03003101 pixel_size, enabled, scaled);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03003102}
3103
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003104static struct drm_i915_gem_object *
3105intel_alloc_context_page(struct drm_device *dev)
3106{
3107 struct drm_i915_gem_object *ctx;
3108 int ret;
3109
3110 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
3111
3112 ctx = i915_gem_alloc_object(dev, 4096);
3113 if (!ctx) {
3114 DRM_DEBUG("failed to alloc power context, RC6 disabled\n");
3115 return NULL;
3116 }
3117
Daniel Vetterc69766f2014-02-14 14:01:17 +01003118 ret = i915_gem_obj_ggtt_pin(ctx, 4096, 0);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003119 if (ret) {
3120 DRM_ERROR("failed to pin power context: %d\n", ret);
3121 goto err_unref;
3122 }
3123
3124 ret = i915_gem_object_set_to_gtt_domain(ctx, 1);
3125 if (ret) {
3126 DRM_ERROR("failed to set-domain on power context: %d\n", ret);
3127 goto err_unpin;
3128 }
3129
3130 return ctx;
3131
3132err_unpin:
Ben Widawskyd7f46fc2013-12-06 14:10:55 -08003133 i915_gem_object_ggtt_unpin(ctx);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003134err_unref:
3135 drm_gem_object_unreference(&ctx->base);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003136 return NULL;
3137}
3138
Daniel Vetter92703882012-08-09 16:46:01 +02003139/**
3140 * Lock protecting IPS related data structures
Daniel Vetter92703882012-08-09 16:46:01 +02003141 */
3142DEFINE_SPINLOCK(mchdev_lock);
3143
3144/* Global for IPS driver to get at the current i915 device. Protected by
3145 * mchdev_lock. */
3146static struct drm_i915_private *i915_mch_dev;
3147
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003148bool ironlake_set_drps(struct drm_device *dev, u8 val)
3149{
3150 struct drm_i915_private *dev_priv = dev->dev_private;
3151 u16 rgvswctl;
3152
Daniel Vetter92703882012-08-09 16:46:01 +02003153 assert_spin_locked(&mchdev_lock);
3154
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003155 rgvswctl = I915_READ16(MEMSWCTL);
3156 if (rgvswctl & MEMCTL_CMD_STS) {
3157 DRM_DEBUG("gpu busy, RCS change rejected\n");
3158 return false; /* still busy with another command */
3159 }
3160
3161 rgvswctl = (MEMCTL_CMD_CHFREQ << MEMCTL_CMD_SHIFT) |
3162 (val << MEMCTL_FREQ_SHIFT) | MEMCTL_SFCAVM;
3163 I915_WRITE16(MEMSWCTL, rgvswctl);
3164 POSTING_READ16(MEMSWCTL);
3165
3166 rgvswctl |= MEMCTL_CMD_STS;
3167 I915_WRITE16(MEMSWCTL, rgvswctl);
3168
3169 return true;
3170}
3171
Daniel Vetter8090c6b2012-06-24 16:42:32 +02003172static void ironlake_enable_drps(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003173{
3174 struct drm_i915_private *dev_priv = dev->dev_private;
3175 u32 rgvmodectl = I915_READ(MEMMODECTL);
3176 u8 fmax, fmin, fstart, vstart;
3177
Daniel Vetter92703882012-08-09 16:46:01 +02003178 spin_lock_irq(&mchdev_lock);
3179
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003180 /* Enable temp reporting */
3181 I915_WRITE16(PMMISC, I915_READ(PMMISC) | MCPPCE_EN);
3182 I915_WRITE16(TSC1, I915_READ(TSC1) | TSE);
3183
3184 /* 100ms RC evaluation intervals */
3185 I915_WRITE(RCUPEI, 100000);
3186 I915_WRITE(RCDNEI, 100000);
3187
3188 /* Set max/min thresholds to 90ms and 80ms respectively */
3189 I915_WRITE(RCBMAXAVG, 90000);
3190 I915_WRITE(RCBMINAVG, 80000);
3191
3192 I915_WRITE(MEMIHYST, 1);
3193
3194 /* Set up min, max, and cur for interrupt handling */
3195 fmax = (rgvmodectl & MEMMODE_FMAX_MASK) >> MEMMODE_FMAX_SHIFT;
3196 fmin = (rgvmodectl & MEMMODE_FMIN_MASK);
3197 fstart = (rgvmodectl & MEMMODE_FSTART_MASK) >>
3198 MEMMODE_FSTART_SHIFT;
3199
3200 vstart = (I915_READ(PXVFREQ_BASE + (fstart * 4)) & PXVFREQ_PX_MASK) >>
3201 PXVFREQ_PX_SHIFT;
3202
Daniel Vetter20e4d402012-08-08 23:35:39 +02003203 dev_priv->ips.fmax = fmax; /* IPS callback will increase this */
3204 dev_priv->ips.fstart = fstart;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003205
Daniel Vetter20e4d402012-08-08 23:35:39 +02003206 dev_priv->ips.max_delay = fstart;
3207 dev_priv->ips.min_delay = fmin;
3208 dev_priv->ips.cur_delay = fstart;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003209
3210 DRM_DEBUG_DRIVER("fmax: %d, fmin: %d, fstart: %d\n",
3211 fmax, fmin, fstart);
3212
3213 I915_WRITE(MEMINTREN, MEMINT_CX_SUPR_EN | MEMINT_EVAL_CHG_EN);
3214
3215 /*
3216 * Interrupts will be enabled in ironlake_irq_postinstall
3217 */
3218
3219 I915_WRITE(VIDSTART, vstart);
3220 POSTING_READ(VIDSTART);
3221
3222 rgvmodectl |= MEMMODE_SWMODE_EN;
3223 I915_WRITE(MEMMODECTL, rgvmodectl);
3224
Daniel Vetter92703882012-08-09 16:46:01 +02003225 if (wait_for_atomic((I915_READ(MEMSWCTL) & MEMCTL_CMD_STS) == 0, 10))
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003226 DRM_ERROR("stuck trying to change perf mode\n");
Daniel Vetter92703882012-08-09 16:46:01 +02003227 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003228
3229 ironlake_set_drps(dev, fstart);
3230
Daniel Vetter20e4d402012-08-08 23:35:39 +02003231 dev_priv->ips.last_count1 = I915_READ(0x112e4) + I915_READ(0x112e8) +
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003232 I915_READ(0x112e0);
Daniel Vetter20e4d402012-08-08 23:35:39 +02003233 dev_priv->ips.last_time1 = jiffies_to_msecs(jiffies);
3234 dev_priv->ips.last_count2 = I915_READ(0x112f4);
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00003235 dev_priv->ips.last_time2 = ktime_get_raw_ns();
Daniel Vetter92703882012-08-09 16:46:01 +02003236
3237 spin_unlock_irq(&mchdev_lock);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003238}
3239
Daniel Vetter8090c6b2012-06-24 16:42:32 +02003240static void ironlake_disable_drps(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003241{
3242 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter92703882012-08-09 16:46:01 +02003243 u16 rgvswctl;
3244
3245 spin_lock_irq(&mchdev_lock);
3246
3247 rgvswctl = I915_READ16(MEMSWCTL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003248
3249 /* Ack interrupts, disable EFC interrupt */
3250 I915_WRITE(MEMINTREN, I915_READ(MEMINTREN) & ~MEMINT_EVAL_CHG_EN);
3251 I915_WRITE(MEMINTRSTS, MEMINT_EVAL_CHG);
3252 I915_WRITE(DEIER, I915_READ(DEIER) & ~DE_PCU_EVENT);
3253 I915_WRITE(DEIIR, DE_PCU_EVENT);
3254 I915_WRITE(DEIMR, I915_READ(DEIMR) | DE_PCU_EVENT);
3255
3256 /* Go back to the starting frequency */
Daniel Vetter20e4d402012-08-08 23:35:39 +02003257 ironlake_set_drps(dev, dev_priv->ips.fstart);
Daniel Vetter92703882012-08-09 16:46:01 +02003258 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003259 rgvswctl |= MEMCTL_CMD_STS;
3260 I915_WRITE(MEMSWCTL, rgvswctl);
Daniel Vetter92703882012-08-09 16:46:01 +02003261 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003262
Daniel Vetter92703882012-08-09 16:46:01 +02003263 spin_unlock_irq(&mchdev_lock);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003264}
3265
Daniel Vetteracbe9472012-07-26 11:50:05 +02003266/* There's a funny hw issue where the hw returns all 0 when reading from
3267 * GEN6_RP_INTERRUPT_LIMITS. Hence we always need to compute the desired value
3268 * ourselves, instead of doing a rmw cycle (which might result in us clearing
3269 * all limits and the gpu stuck at whatever frequency it is at atm).
3270 */
Chris Wilson6917c7b2013-11-06 13:56:26 -02003271static u32 gen6_rps_limits(struct drm_i915_private *dev_priv, u8 val)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003272{
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01003273 u32 limits;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003274
Daniel Vetter20b46e52012-07-26 11:16:14 +02003275 /* Only set the down limit when we've reached the lowest level to avoid
3276 * getting more interrupts, otherwise leave this clear. This prevents a
3277 * race in the hw when coming out of rc6: There's a tiny window where
3278 * the hw runs at the minimal clock before selecting the desired
3279 * frequency, if the down threshold expires in that window we will not
3280 * receive a down interrupt. */
Ben Widawskyb39fb292014-03-19 18:31:11 -07003281 limits = dev_priv->rps.max_freq_softlimit << 24;
3282 if (val <= dev_priv->rps.min_freq_softlimit)
3283 limits |= dev_priv->rps.min_freq_softlimit << 16;
Daniel Vetter20b46e52012-07-26 11:16:14 +02003284
3285 return limits;
3286}
3287
Chris Wilsondd75fdc2013-09-25 17:34:57 +01003288static void gen6_set_rps_thresholds(struct drm_i915_private *dev_priv, u8 val)
3289{
3290 int new_power;
3291
3292 new_power = dev_priv->rps.power;
3293 switch (dev_priv->rps.power) {
3294 case LOW_POWER:
Ben Widawskyb39fb292014-03-19 18:31:11 -07003295 if (val > dev_priv->rps.efficient_freq + 1 && val > dev_priv->rps.cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01003296 new_power = BETWEEN;
3297 break;
3298
3299 case BETWEEN:
Ben Widawskyb39fb292014-03-19 18:31:11 -07003300 if (val <= dev_priv->rps.efficient_freq && val < dev_priv->rps.cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01003301 new_power = LOW_POWER;
Ben Widawskyb39fb292014-03-19 18:31:11 -07003302 else if (val >= dev_priv->rps.rp0_freq && val > dev_priv->rps.cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01003303 new_power = HIGH_POWER;
3304 break;
3305
3306 case HIGH_POWER:
Ben Widawskyb39fb292014-03-19 18:31:11 -07003307 if (val < (dev_priv->rps.rp1_freq + dev_priv->rps.rp0_freq) >> 1 && val < dev_priv->rps.cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01003308 new_power = BETWEEN;
3309 break;
3310 }
3311 /* Max/min bins are special */
Ben Widawskyb39fb292014-03-19 18:31:11 -07003312 if (val == dev_priv->rps.min_freq_softlimit)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01003313 new_power = LOW_POWER;
Ben Widawskyb39fb292014-03-19 18:31:11 -07003314 if (val == dev_priv->rps.max_freq_softlimit)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01003315 new_power = HIGH_POWER;
3316 if (new_power == dev_priv->rps.power)
3317 return;
3318
3319 /* Note the units here are not exactly 1us, but 1280ns. */
3320 switch (new_power) {
3321 case LOW_POWER:
3322 /* Upclock if more than 95% busy over 16ms */
3323 I915_WRITE(GEN6_RP_UP_EI, 12500);
3324 I915_WRITE(GEN6_RP_UP_THRESHOLD, 11800);
3325
3326 /* Downclock if less than 85% busy over 32ms */
3327 I915_WRITE(GEN6_RP_DOWN_EI, 25000);
3328 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 21250);
3329
3330 I915_WRITE(GEN6_RP_CONTROL,
3331 GEN6_RP_MEDIA_TURBO |
3332 GEN6_RP_MEDIA_HW_NORMAL_MODE |
3333 GEN6_RP_MEDIA_IS_GFX |
3334 GEN6_RP_ENABLE |
3335 GEN6_RP_UP_BUSY_AVG |
3336 GEN6_RP_DOWN_IDLE_AVG);
3337 break;
3338
3339 case BETWEEN:
3340 /* Upclock if more than 90% busy over 13ms */
3341 I915_WRITE(GEN6_RP_UP_EI, 10250);
3342 I915_WRITE(GEN6_RP_UP_THRESHOLD, 9225);
3343
3344 /* Downclock if less than 75% busy over 32ms */
3345 I915_WRITE(GEN6_RP_DOWN_EI, 25000);
3346 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 18750);
3347
3348 I915_WRITE(GEN6_RP_CONTROL,
3349 GEN6_RP_MEDIA_TURBO |
3350 GEN6_RP_MEDIA_HW_NORMAL_MODE |
3351 GEN6_RP_MEDIA_IS_GFX |
3352 GEN6_RP_ENABLE |
3353 GEN6_RP_UP_BUSY_AVG |
3354 GEN6_RP_DOWN_IDLE_AVG);
3355 break;
3356
3357 case HIGH_POWER:
3358 /* Upclock if more than 85% busy over 10ms */
3359 I915_WRITE(GEN6_RP_UP_EI, 8000);
3360 I915_WRITE(GEN6_RP_UP_THRESHOLD, 6800);
3361
3362 /* Downclock if less than 60% busy over 32ms */
3363 I915_WRITE(GEN6_RP_DOWN_EI, 25000);
3364 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 15000);
3365
3366 I915_WRITE(GEN6_RP_CONTROL,
3367 GEN6_RP_MEDIA_TURBO |
3368 GEN6_RP_MEDIA_HW_NORMAL_MODE |
3369 GEN6_RP_MEDIA_IS_GFX |
3370 GEN6_RP_ENABLE |
3371 GEN6_RP_UP_BUSY_AVG |
3372 GEN6_RP_DOWN_IDLE_AVG);
3373 break;
3374 }
3375
3376 dev_priv->rps.power = new_power;
3377 dev_priv->rps.last_adj = 0;
3378}
3379
Chris Wilson2876ce72014-03-28 08:03:34 +00003380static u32 gen6_rps_pm_mask(struct drm_i915_private *dev_priv, u8 val)
3381{
3382 u32 mask = 0;
3383
3384 if (val > dev_priv->rps.min_freq_softlimit)
3385 mask |= GEN6_PM_RP_DOWN_THRESHOLD | GEN6_PM_RP_DOWN_TIMEOUT;
3386 if (val < dev_priv->rps.max_freq_softlimit)
3387 mask |= GEN6_PM_RP_UP_THRESHOLD;
3388
Chris Wilson7b3c29f2014-07-10 20:31:19 +01003389 mask |= dev_priv->pm_rps_events & (GEN6_PM_RP_DOWN_EI_EXPIRED | GEN6_PM_RP_UP_EI_EXPIRED);
3390 mask &= dev_priv->pm_rps_events;
3391
Chris Wilson2876ce72014-03-28 08:03:34 +00003392 /* IVB and SNB hard hangs on looping batchbuffer
3393 * if GEN6_PM_UP_EI_EXPIRED is masked.
3394 */
3395 if (INTEL_INFO(dev_priv->dev)->gen <= 7 && !IS_HASWELL(dev_priv->dev))
3396 mask |= GEN6_PM_RP_UP_EI_EXPIRED;
3397
Deepak Sbaccd452014-05-15 20:58:09 +03003398 if (IS_GEN8(dev_priv->dev))
3399 mask |= GEN8_PMINTR_REDIRECT_TO_NON_DISP;
3400
Chris Wilson2876ce72014-03-28 08:03:34 +00003401 return ~mask;
3402}
3403
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06003404/* gen6_set_rps is called to update the frequency request, but should also be
3405 * called when the range (min_delay and max_delay) is modified so that we can
3406 * update the GEN6_RP_INTERRUPT_LIMITS register accordingly. */
Daniel Vetter20b46e52012-07-26 11:16:14 +02003407void gen6_set_rps(struct drm_device *dev, u8 val)
3408{
3409 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01003410
Jesse Barnes4fc688c2012-11-02 11:14:01 -07003411 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Ben Widawskyb39fb292014-03-19 18:31:11 -07003412 WARN_ON(val > dev_priv->rps.max_freq_softlimit);
3413 WARN_ON(val < dev_priv->rps.min_freq_softlimit);
Daniel Vetter004777c2012-08-09 15:07:01 +02003414
Chris Wilsoneb64cad2014-03-27 08:24:20 +00003415 /* min/max delay may still have been modified so be sure to
3416 * write the limits value.
3417 */
3418 if (val != dev_priv->rps.cur_freq) {
3419 gen6_set_rps_thresholds(dev_priv, val);
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06003420
Ben Widawsky50e6a2a2014-03-31 17:16:43 -07003421 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Chris Wilsoneb64cad2014-03-27 08:24:20 +00003422 I915_WRITE(GEN6_RPNSWREQ,
3423 HSW_FREQUENCY(val));
3424 else
3425 I915_WRITE(GEN6_RPNSWREQ,
3426 GEN6_FREQUENCY(val) |
3427 GEN6_OFFSET(0) |
3428 GEN6_AGGRESSIVE_TURBO);
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06003429 }
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01003430
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01003431 /* Make sure we continue to get interrupts
3432 * until we hit the minimum or maximum frequencies.
3433 */
Chris Wilsoneb64cad2014-03-27 08:24:20 +00003434 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS, gen6_rps_limits(dev_priv, val));
Chris Wilson2876ce72014-03-28 08:03:34 +00003435 I915_WRITE(GEN6_PMINTRMSK, gen6_rps_pm_mask(dev_priv, val));
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01003436
Ben Widawskyd5570a72012-09-07 19:43:41 -07003437 POSTING_READ(GEN6_RPNSWREQ);
3438
Ben Widawskyb39fb292014-03-19 18:31:11 -07003439 dev_priv->rps.cur_freq = val;
Daniel Vetterbe2cde92012-08-30 13:26:48 +02003440 trace_intel_gpu_freq_change(val * 50);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003441}
3442
Deepak S76c3552f2014-01-30 23:08:16 +05303443/* vlv_set_rps_idle: Set the frequency to Rpn if Gfx clocks are down
3444 *
3445 * * If Gfx is Idle, then
3446 * 1. Mask Turbo interrupts
3447 * 2. Bring up Gfx clock
3448 * 3. Change the freq to Rpn and wait till P-Unit updates freq
3449 * 4. Clear the Force GFX CLK ON bit so that Gfx can down
3450 * 5. Unmask Turbo interrupts
3451*/
3452static void vlv_set_rps_idle(struct drm_i915_private *dev_priv)
3453{
Deepak S5549d252014-06-28 11:26:11 +05303454 struct drm_device *dev = dev_priv->dev;
3455
3456 /* Latest VLV doesn't need to force the gfx clock */
3457 if (dev->pdev->revision >= 0xd) {
3458 valleyview_set_rps(dev_priv->dev, dev_priv->rps.min_freq_softlimit);
3459 return;
3460 }
3461
Deepak S76c3552f2014-01-30 23:08:16 +05303462 /*
3463 * When we are idle. Drop to min voltage state.
3464 */
3465
Ben Widawskyb39fb292014-03-19 18:31:11 -07003466 if (dev_priv->rps.cur_freq <= dev_priv->rps.min_freq_softlimit)
Deepak S76c3552f2014-01-30 23:08:16 +05303467 return;
3468
3469 /* Mask turbo interrupt so that they will not come in between */
3470 I915_WRITE(GEN6_PMINTRMSK, 0xffffffff);
3471
Imre Deak650ad972014-04-18 16:35:02 +03003472 vlv_force_gfx_clock(dev_priv, true);
Deepak S76c3552f2014-01-30 23:08:16 +05303473
Ben Widawskyb39fb292014-03-19 18:31:11 -07003474 dev_priv->rps.cur_freq = dev_priv->rps.min_freq_softlimit;
Deepak S76c3552f2014-01-30 23:08:16 +05303475
3476 vlv_punit_write(dev_priv, PUNIT_REG_GPU_FREQ_REQ,
Ben Widawskyb39fb292014-03-19 18:31:11 -07003477 dev_priv->rps.min_freq_softlimit);
Deepak S76c3552f2014-01-30 23:08:16 +05303478
3479 if (wait_for(((vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS))
3480 & GENFREQSTATUS) == 0, 5))
3481 DRM_ERROR("timed out waiting for Punit\n");
3482
Imre Deak650ad972014-04-18 16:35:02 +03003483 vlv_force_gfx_clock(dev_priv, false);
Deepak S76c3552f2014-01-30 23:08:16 +05303484
Chris Wilson2876ce72014-03-28 08:03:34 +00003485 I915_WRITE(GEN6_PMINTRMSK,
3486 gen6_rps_pm_mask(dev_priv, dev_priv->rps.cur_freq));
Deepak S76c3552f2014-01-30 23:08:16 +05303487}
3488
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003489void gen6_rps_idle(struct drm_i915_private *dev_priv)
3490{
Damien Lespiau691bb712013-12-12 14:36:36 +00003491 struct drm_device *dev = dev_priv->dev;
3492
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003493 mutex_lock(&dev_priv->rps.hw_lock);
Chris Wilsonc0951f02013-10-10 21:58:50 +01003494 if (dev_priv->rps.enabled) {
Deepak S34638112014-06-28 11:26:26 +05303495 if (IS_CHERRYVIEW(dev))
3496 valleyview_set_rps(dev_priv->dev, dev_priv->rps.min_freq_softlimit);
3497 else if (IS_VALLEYVIEW(dev))
Deepak S76c3552f2014-01-30 23:08:16 +05303498 vlv_set_rps_idle(dev_priv);
Daniel Vetter7526ed72014-09-29 15:07:19 +02003499 else
Ben Widawskyb39fb292014-03-19 18:31:11 -07003500 gen6_set_rps(dev_priv->dev, dev_priv->rps.min_freq_softlimit);
Chris Wilsonc0951f02013-10-10 21:58:50 +01003501 dev_priv->rps.last_adj = 0;
3502 }
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003503 mutex_unlock(&dev_priv->rps.hw_lock);
3504}
3505
3506void gen6_rps_boost(struct drm_i915_private *dev_priv)
3507{
Damien Lespiau691bb712013-12-12 14:36:36 +00003508 struct drm_device *dev = dev_priv->dev;
3509
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003510 mutex_lock(&dev_priv->rps.hw_lock);
Chris Wilsonc0951f02013-10-10 21:58:50 +01003511 if (dev_priv->rps.enabled) {
Damien Lespiau691bb712013-12-12 14:36:36 +00003512 if (IS_VALLEYVIEW(dev))
Ben Widawskyb39fb292014-03-19 18:31:11 -07003513 valleyview_set_rps(dev_priv->dev, dev_priv->rps.max_freq_softlimit);
Daniel Vetter7526ed72014-09-29 15:07:19 +02003514 else
Ben Widawskyb39fb292014-03-19 18:31:11 -07003515 gen6_set_rps(dev_priv->dev, dev_priv->rps.max_freq_softlimit);
Chris Wilsonc0951f02013-10-10 21:58:50 +01003516 dev_priv->rps.last_adj = 0;
3517 }
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003518 mutex_unlock(&dev_priv->rps.hw_lock);
3519}
3520
Jesse Barnes0a073b82013-04-17 15:54:58 -07003521void valleyview_set_rps(struct drm_device *dev, u8 val)
3522{
3523 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä7a670922013-06-25 19:21:06 +03003524
Jesse Barnes0a073b82013-04-17 15:54:58 -07003525 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Ben Widawskyb39fb292014-03-19 18:31:11 -07003526 WARN_ON(val > dev_priv->rps.max_freq_softlimit);
3527 WARN_ON(val < dev_priv->rps.min_freq_softlimit);
Jesse Barnes0a073b82013-04-17 15:54:58 -07003528
Ville Syrjälä1c147622014-08-18 14:42:43 +03003529 if (WARN_ONCE(IS_CHERRYVIEW(dev) && (val & 1),
3530 "Odd GPU freq value\n"))
3531 val &= ~1;
3532
Ville Syrjälä67956862014-09-02 15:12:17 +03003533 if (val != dev_priv->rps.cur_freq) {
3534 DRM_DEBUG_DRIVER("GPU freq request from %d MHz (%u) to %d MHz (%u)\n",
3535 vlv_gpu_freq(dev_priv, dev_priv->rps.cur_freq),
3536 dev_priv->rps.cur_freq,
3537 vlv_gpu_freq(dev_priv, val), val);
3538
Chris Wilson2876ce72014-03-28 08:03:34 +00003539 vlv_punit_write(dev_priv, PUNIT_REG_GPU_FREQ_REQ, val);
Ville Syrjälä67956862014-09-02 15:12:17 +03003540 }
Jesse Barnes0a073b82013-04-17 15:54:58 -07003541
Imre Deak09c87db2014-04-03 20:02:42 +03003542 I915_WRITE(GEN6_PMINTRMSK, gen6_rps_pm_mask(dev_priv, val));
Jesse Barnes0a073b82013-04-17 15:54:58 -07003543
Ben Widawskyb39fb292014-03-19 18:31:11 -07003544 dev_priv->rps.cur_freq = val;
Ville Syrjälä2ec38152013-11-05 22:42:29 +02003545 trace_intel_gpu_freq_change(vlv_gpu_freq(dev_priv, val));
Jesse Barnes0a073b82013-04-17 15:54:58 -07003546}
3547
Ben Widawsky09610212014-05-15 20:58:08 +03003548static void gen8_disable_rps_interrupts(struct drm_device *dev)
3549{
3550 struct drm_i915_private *dev_priv = dev->dev_private;
3551
Daniel Vetter7526ed72014-09-29 15:07:19 +02003552 I915_WRITE(GEN6_PMINTRMSK, ~GEN8_PMINTR_REDIRECT_TO_NON_DISP);
3553 I915_WRITE(GEN8_GT_IER(2), I915_READ(GEN8_GT_IER(2)) &
3554 ~dev_priv->pm_rps_events);
3555 /* Complete PM interrupt masking here doesn't race with the rps work
3556 * item again unmasking PM interrupts because that is using a different
3557 * register (GEN8_GT_IMR(2)) to mask PM interrupts. The only risk is in
3558 * leaving stale bits in GEN8_GT_IIR(2) and GEN8_GT_IMR(2) which
3559 * gen8_enable_rps will clean up. */
Ben Widawsky09610212014-05-15 20:58:08 +03003560
Daniel Vetter7526ed72014-09-29 15:07:19 +02003561 spin_lock_irq(&dev_priv->irq_lock);
3562 dev_priv->rps.pm_iir = 0;
3563 spin_unlock_irq(&dev_priv->irq_lock);
3564
3565 I915_WRITE(GEN8_GT_IIR(2), dev_priv->pm_rps_events);
Ben Widawsky09610212014-05-15 20:58:08 +03003566}
3567
Daniel Vetter44fc7d52013-07-12 22:43:27 +02003568static void gen6_disable_rps_interrupts(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003569{
3570 struct drm_i915_private *dev_priv = dev->dev_private;
3571
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003572 I915_WRITE(GEN6_PMINTRMSK, 0xffffffff);
Deepak Sa6706b42014-03-15 20:23:22 +05303573 I915_WRITE(GEN6_PMIER, I915_READ(GEN6_PMIER) &
3574 ~dev_priv->pm_rps_events);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003575 /* Complete PM interrupt masking here doesn't race with the rps work
3576 * item again unmasking PM interrupts because that is using a different
3577 * register (PMIMR) to mask PM interrupts. The only risk is in leaving
3578 * stale bits in PMIIR and PMIMR which gen6_enable_rps will clean up. */
3579
Daniel Vetter59cdb632013-07-04 23:35:28 +02003580 spin_lock_irq(&dev_priv->irq_lock);
Daniel Vetterc6a828d2012-08-08 23:35:35 +02003581 dev_priv->rps.pm_iir = 0;
Daniel Vetter59cdb632013-07-04 23:35:28 +02003582 spin_unlock_irq(&dev_priv->irq_lock);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003583
Deepak Sa6706b42014-03-15 20:23:22 +05303584 I915_WRITE(GEN6_PMIIR, dev_priv->pm_rps_events);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003585}
3586
Daniel Vetter44fc7d52013-07-12 22:43:27 +02003587static void gen6_disable_rps(struct drm_device *dev)
3588{
3589 struct drm_i915_private *dev_priv = dev->dev_private;
3590
3591 I915_WRITE(GEN6_RC_CONTROL, 0);
3592 I915_WRITE(GEN6_RPNSWREQ, 1 << 31);
3593
Ben Widawsky09610212014-05-15 20:58:08 +03003594 if (IS_BROADWELL(dev))
3595 gen8_disable_rps_interrupts(dev);
3596 else
3597 gen6_disable_rps_interrupts(dev);
Daniel Vetter44fc7d52013-07-12 22:43:27 +02003598}
3599
Deepak S38807742014-05-23 21:00:15 +05303600static void cherryview_disable_rps(struct drm_device *dev)
3601{
3602 struct drm_i915_private *dev_priv = dev->dev_private;
3603
3604 I915_WRITE(GEN6_RC_CONTROL, 0);
Deepak S3497a562014-07-10 13:16:26 +05303605
3606 gen8_disable_rps_interrupts(dev);
Deepak S38807742014-05-23 21:00:15 +05303607}
3608
Jesse Barnesd20d4f02013-04-23 10:09:28 -07003609static void valleyview_disable_rps(struct drm_device *dev)
3610{
3611 struct drm_i915_private *dev_priv = dev->dev_private;
3612
Deepak S98a2e5f2014-08-18 10:35:27 -07003613 /* we're doing forcewake before Disabling RC6,
3614 * This what the BIOS expects when going into suspend */
3615 gen6_gt_force_wake_get(dev_priv, FORCEWAKE_ALL);
3616
Jesse Barnesd20d4f02013-04-23 10:09:28 -07003617 I915_WRITE(GEN6_RC_CONTROL, 0);
Jesse Barnesd20d4f02013-04-23 10:09:28 -07003618
Deepak S98a2e5f2014-08-18 10:35:27 -07003619 gen6_gt_force_wake_put(dev_priv, FORCEWAKE_ALL);
3620
Daniel Vetter44fc7d52013-07-12 22:43:27 +02003621 gen6_disable_rps_interrupts(dev);
Jesse Barnesd20d4f02013-04-23 10:09:28 -07003622}
3623
Ben Widawskydc39fff2013-10-18 12:32:07 -07003624static void intel_print_rc6_info(struct drm_device *dev, u32 mode)
3625{
Imre Deak91ca6892014-04-14 20:24:25 +03003626 if (IS_VALLEYVIEW(dev)) {
3627 if (mode & (GEN7_RC_CTL_TO_MODE | GEN6_RC_CTL_EI_MODE(1)))
3628 mode = GEN6_RC_CTL_RC6_ENABLE;
3629 else
3630 mode = 0;
3631 }
Rodrigo Vivi58abf1d2014-10-07 07:06:50 -07003632 if (HAS_RC6p(dev))
3633 DRM_DEBUG_KMS("Enabling RC6 states: RC6 %s RC6p %s RC6pp %s\n",
3634 (mode & GEN6_RC_CTL_RC6_ENABLE) ? "on" : "off",
3635 (mode & GEN6_RC_CTL_RC6p_ENABLE) ? "on" : "off",
3636 (mode & GEN6_RC_CTL_RC6pp_ENABLE) ? "on" : "off");
3637
3638 else
3639 DRM_DEBUG_KMS("Enabling RC6 states: RC6 %s\n",
3640 (mode & GEN6_RC_CTL_RC6_ENABLE) ? "on" : "off");
Ben Widawskydc39fff2013-10-18 12:32:07 -07003641}
3642
Imre Deake6069ca2014-04-18 16:01:02 +03003643static int sanitize_rc6_option(const struct drm_device *dev, int enable_rc6)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003644{
Damien Lespiaueb4926e2013-06-07 17:41:14 +01003645 /* No RC6 before Ironlake */
3646 if (INTEL_INFO(dev)->gen < 5)
3647 return 0;
3648
Imre Deake6069ca2014-04-18 16:01:02 +03003649 /* RC6 is only on Ironlake mobile not on desktop */
3650 if (INTEL_INFO(dev)->gen == 5 && !IS_IRONLAKE_M(dev))
3651 return 0;
3652
Daniel Vetter456470e2012-08-08 23:35:40 +02003653 /* Respect the kernel parameter if it is set */
Imre Deake6069ca2014-04-18 16:01:02 +03003654 if (enable_rc6 >= 0) {
3655 int mask;
3656
Rodrigo Vivi58abf1d2014-10-07 07:06:50 -07003657 if (HAS_RC6p(dev))
Imre Deake6069ca2014-04-18 16:01:02 +03003658 mask = INTEL_RC6_ENABLE | INTEL_RC6p_ENABLE |
3659 INTEL_RC6pp_ENABLE;
3660 else
3661 mask = INTEL_RC6_ENABLE;
3662
3663 if ((enable_rc6 & mask) != enable_rc6)
Daniel Vetter8dfd1f02014-08-04 11:15:56 +02003664 DRM_DEBUG_KMS("Adjusting RC6 mask to %d (requested %d, valid %d)\n",
3665 enable_rc6 & mask, enable_rc6, mask);
Imre Deake6069ca2014-04-18 16:01:02 +03003666
3667 return enable_rc6 & mask;
3668 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003669
Chris Wilson6567d742012-11-10 10:00:06 +00003670 /* Disable RC6 on Ironlake */
3671 if (INTEL_INFO(dev)->gen == 5)
3672 return 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003673
Ben Widawsky8bade1a2014-01-28 20:25:39 -08003674 if (IS_IVYBRIDGE(dev))
Ben Widawskycca84a12014-01-28 20:25:38 -08003675 return (INTEL_RC6_ENABLE | INTEL_RC6p_ENABLE);
Ben Widawsky8bade1a2014-01-28 20:25:39 -08003676
3677 return INTEL_RC6_ENABLE;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003678}
3679
Imre Deake6069ca2014-04-18 16:01:02 +03003680int intel_enable_rc6(const struct drm_device *dev)
3681{
3682 return i915.enable_rc6;
3683}
3684
Ben Widawsky09610212014-05-15 20:58:08 +03003685static void gen8_enable_rps_interrupts(struct drm_device *dev)
3686{
3687 struct drm_i915_private *dev_priv = dev->dev_private;
3688
3689 spin_lock_irq(&dev_priv->irq_lock);
3690 WARN_ON(dev_priv->rps.pm_iir);
Daniel Vetter480c8032014-07-16 09:49:40 +02003691 gen8_enable_pm_irq(dev_priv, dev_priv->pm_rps_events);
Ben Widawsky09610212014-05-15 20:58:08 +03003692 I915_WRITE(GEN8_GT_IIR(2), dev_priv->pm_rps_events);
3693 spin_unlock_irq(&dev_priv->irq_lock);
3694}
3695
Daniel Vetter44fc7d52013-07-12 22:43:27 +02003696static void gen6_enable_rps_interrupts(struct drm_device *dev)
3697{
3698 struct drm_i915_private *dev_priv = dev->dev_private;
3699
3700 spin_lock_irq(&dev_priv->irq_lock);
Daniel Vettera0b33352013-07-04 23:35:34 +02003701 WARN_ON(dev_priv->rps.pm_iir);
Daniel Vetter480c8032014-07-16 09:49:40 +02003702 gen6_enable_pm_irq(dev_priv, dev_priv->pm_rps_events);
Deepak Sa6706b42014-03-15 20:23:22 +05303703 I915_WRITE(GEN6_PMIIR, dev_priv->pm_rps_events);
Daniel Vetter44fc7d52013-07-12 22:43:27 +02003704 spin_unlock_irq(&dev_priv->irq_lock);
Daniel Vetter44fc7d52013-07-12 22:43:27 +02003705}
3706
Ben Widawsky3280e8b2014-03-31 17:16:42 -07003707static void parse_rp_state_cap(struct drm_i915_private *dev_priv, u32 rp_state_cap)
3708{
3709 /* All of these values are in units of 50MHz */
3710 dev_priv->rps.cur_freq = 0;
3711 /* static values from HW: RP0 < RPe < RP1 < RPn (min_freq) */
3712 dev_priv->rps.rp1_freq = (rp_state_cap >> 8) & 0xff;
3713 dev_priv->rps.rp0_freq = (rp_state_cap >> 0) & 0xff;
3714 dev_priv->rps.min_freq = (rp_state_cap >> 16) & 0xff;
3715 /* XXX: only BYT has a special efficient freq */
3716 dev_priv->rps.efficient_freq = dev_priv->rps.rp1_freq;
3717 /* hw_max = RP0 until we check for overclocking */
3718 dev_priv->rps.max_freq = dev_priv->rps.rp0_freq;
3719
3720 /* Preserve min/max settings in case of re-init */
3721 if (dev_priv->rps.max_freq_softlimit == 0)
3722 dev_priv->rps.max_freq_softlimit = dev_priv->rps.max_freq;
3723
3724 if (dev_priv->rps.min_freq_softlimit == 0)
3725 dev_priv->rps.min_freq_softlimit = dev_priv->rps.min_freq;
3726}
3727
Ben Widawsky6edee7f2013-11-02 21:07:52 -07003728static void gen8_enable_rps(struct drm_device *dev)
3729{
3730 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01003731 struct intel_engine_cs *ring;
Ben Widawsky6edee7f2013-11-02 21:07:52 -07003732 uint32_t rc6_mask = 0, rp_state_cap;
3733 int unused;
3734
3735 /* 1a: Software RC state - RC0 */
3736 I915_WRITE(GEN6_RC_STATE, 0);
3737
3738 /* 1c & 1d: Get forcewake during program sequence. Although the driver
3739 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
Deepak Sc8d9a592013-11-23 14:55:42 +05303740 gen6_gt_force_wake_get(dev_priv, FORCEWAKE_ALL);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07003741
3742 /* 2a: Disable RC states. */
3743 I915_WRITE(GEN6_RC_CONTROL, 0);
3744
3745 rp_state_cap = I915_READ(GEN6_RP_STATE_CAP);
Ben Widawsky3280e8b2014-03-31 17:16:42 -07003746 parse_rp_state_cap(dev_priv, rp_state_cap);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07003747
3748 /* 2b: Program RC6 thresholds.*/
3749 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16);
3750 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
3751 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
3752 for_each_ring(ring, dev_priv, unused)
3753 I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
3754 I915_WRITE(GEN6_RC_SLEEP, 0);
Tom O'Rourke0d68b252014-04-09 11:44:06 -07003755 if (IS_BROADWELL(dev))
3756 I915_WRITE(GEN6_RC6_THRESHOLD, 625); /* 800us/1.28 for TO */
3757 else
3758 I915_WRITE(GEN6_RC6_THRESHOLD, 50000); /* 50/125ms per EI */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07003759
3760 /* 3: Enable RC6 */
3761 if (intel_enable_rc6(dev) & INTEL_RC6_ENABLE)
3762 rc6_mask = GEN6_RC_CTL_RC6_ENABLE;
Ben Widawskyabbf9d22014-01-28 20:25:41 -08003763 intel_print_rc6_info(dev, rc6_mask);
Tom O'Rourke0d68b252014-04-09 11:44:06 -07003764 if (IS_BROADWELL(dev))
3765 I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
3766 GEN7_RC_CTL_TO_MODE |
3767 rc6_mask);
3768 else
3769 I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
3770 GEN6_RC_CTL_EI_MODE(1) |
3771 rc6_mask);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07003772
3773 /* 4 Program defaults and thresholds for RPS*/
Ben Widawskyf9bdc582014-03-31 17:16:41 -07003774 I915_WRITE(GEN6_RPNSWREQ,
3775 HSW_FREQUENCY(dev_priv->rps.rp1_freq));
3776 I915_WRITE(GEN6_RC_VIDEO_FREQ,
3777 HSW_FREQUENCY(dev_priv->rps.rp1_freq));
Daniel Vetter7526ed72014-09-29 15:07:19 +02003778 /* NB: Docs say 1s, and 1000000 - which aren't equivalent */
3779 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 100000000 / 128); /* 1 second timeout */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07003780
Daniel Vetter7526ed72014-09-29 15:07:19 +02003781 /* Docs recommend 900MHz, and 300 MHz respectively */
3782 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS,
3783 dev_priv->rps.max_freq_softlimit << 24 |
3784 dev_priv->rps.min_freq_softlimit << 16);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07003785
Daniel Vetter7526ed72014-09-29 15:07:19 +02003786 I915_WRITE(GEN6_RP_UP_THRESHOLD, 7600000 / 128); /* 76ms busyness per EI, 90% */
3787 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 31300000 / 128); /* 313ms busyness per EI, 70%*/
3788 I915_WRITE(GEN6_RP_UP_EI, 66000); /* 84.48ms, XXX: random? */
3789 I915_WRITE(GEN6_RP_DOWN_EI, 350000); /* 448ms, XXX: random? */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07003790
Daniel Vetter7526ed72014-09-29 15:07:19 +02003791 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07003792
3793 /* 5: Enable RPS */
Daniel Vetter7526ed72014-09-29 15:07:19 +02003794 I915_WRITE(GEN6_RP_CONTROL,
3795 GEN6_RP_MEDIA_TURBO |
3796 GEN6_RP_MEDIA_HW_NORMAL_MODE |
3797 GEN6_RP_MEDIA_IS_GFX |
3798 GEN6_RP_ENABLE |
3799 GEN6_RP_UP_BUSY_AVG |
3800 GEN6_RP_DOWN_IDLE_AVG);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07003801
Daniel Vetter7526ed72014-09-29 15:07:19 +02003802 /* 6: Ring frequency + overclocking (our driver does this later */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07003803
3804 gen6_set_rps(dev, (I915_READ(GEN6_GT_PERF_STATUS) & 0xff00) >> 8);
Daniel Vetter7526ed72014-09-29 15:07:19 +02003805
3806 gen8_enable_rps_interrupts(dev);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07003807
Deepak Sc8d9a592013-11-23 14:55:42 +05303808 gen6_gt_force_wake_put(dev_priv, FORCEWAKE_ALL);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07003809}
3810
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02003811static void gen6_enable_rps(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003812{
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02003813 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01003814 struct intel_engine_cs *ring;
Ben Widawsky2a5913a2014-03-19 18:31:13 -07003815 u32 rp_state_cap;
Ben Widawskyd060c162014-03-19 18:31:08 -07003816 u32 rc6vids, pcu_mbox = 0, rc6_mask = 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003817 u32 gtfifodbg;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003818 int rc6_mode;
Ben Widawsky42c05262012-09-26 10:34:00 -07003819 int i, ret;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003820
Jesse Barnes4fc688c2012-11-02 11:14:01 -07003821 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02003822
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003823 /* Here begins a magic sequence of register writes to enable
3824 * auto-downclocking.
3825 *
3826 * Perhaps there might be some value in exposing these to
3827 * userspace...
3828 */
3829 I915_WRITE(GEN6_RC_STATE, 0);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003830
3831 /* Clear the DBG now so we don't confuse earlier errors */
3832 if ((gtfifodbg = I915_READ(GTFIFODBG))) {
3833 DRM_ERROR("GT fifo had a previous error %x\n", gtfifodbg);
3834 I915_WRITE(GTFIFODBG, gtfifodbg);
3835 }
3836
Deepak Sc8d9a592013-11-23 14:55:42 +05303837 gen6_gt_force_wake_get(dev_priv, FORCEWAKE_ALL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003838
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01003839 rp_state_cap = I915_READ(GEN6_RP_STATE_CAP);
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01003840
Ben Widawsky3280e8b2014-03-31 17:16:42 -07003841 parse_rp_state_cap(dev_priv, rp_state_cap);
Jeff McGeedd0a1aa2014-02-04 11:32:31 -06003842
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003843 /* disable the counters and set deterministic thresholds */
3844 I915_WRITE(GEN6_RC_CONTROL, 0);
3845
3846 I915_WRITE(GEN6_RC1_WAKE_RATE_LIMIT, 1000 << 16);
3847 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16 | 30);
3848 I915_WRITE(GEN6_RC6pp_WAKE_RATE_LIMIT, 30);
3849 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
3850 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
3851
Chris Wilsonb4519512012-05-11 14:29:30 +01003852 for_each_ring(ring, dev_priv, i)
3853 I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003854
3855 I915_WRITE(GEN6_RC_SLEEP, 0);
3856 I915_WRITE(GEN6_RC1e_THRESHOLD, 1000);
Daniel Vetter29c78f62013-11-16 16:04:26 +01003857 if (IS_IVYBRIDGE(dev))
Stéphane Marchesin351aa562013-08-13 11:55:17 -07003858 I915_WRITE(GEN6_RC6_THRESHOLD, 125000);
3859 else
3860 I915_WRITE(GEN6_RC6_THRESHOLD, 50000);
Stéphane Marchesin0920a482013-01-29 19:41:59 -08003861 I915_WRITE(GEN6_RC6p_THRESHOLD, 150000);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003862 I915_WRITE(GEN6_RC6pp_THRESHOLD, 64000); /* unused */
3863
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03003864 /* Check if we are enabling RC6 */
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003865 rc6_mode = intel_enable_rc6(dev_priv->dev);
3866 if (rc6_mode & INTEL_RC6_ENABLE)
3867 rc6_mask |= GEN6_RC_CTL_RC6_ENABLE;
3868
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03003869 /* We don't use those on Haswell */
3870 if (!IS_HASWELL(dev)) {
3871 if (rc6_mode & INTEL_RC6p_ENABLE)
3872 rc6_mask |= GEN6_RC_CTL_RC6p_ENABLE;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003873
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03003874 if (rc6_mode & INTEL_RC6pp_ENABLE)
3875 rc6_mask |= GEN6_RC_CTL_RC6pp_ENABLE;
3876 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003877
Ben Widawskydc39fff2013-10-18 12:32:07 -07003878 intel_print_rc6_info(dev, rc6_mask);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003879
3880 I915_WRITE(GEN6_RC_CONTROL,
3881 rc6_mask |
3882 GEN6_RC_CTL_EI_MODE(1) |
3883 GEN6_RC_CTL_HW_ENABLE);
3884
Chris Wilsondd75fdc2013-09-25 17:34:57 +01003885 /* Power down if completely idle for over 50ms */
3886 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 50000);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003887 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003888
Ben Widawsky42c05262012-09-26 10:34:00 -07003889 ret = sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_MIN_FREQ_TABLE, 0);
Ben Widawskyd060c162014-03-19 18:31:08 -07003890 if (ret)
Ben Widawsky42c05262012-09-26 10:34:00 -07003891 DRM_DEBUG_DRIVER("Failed to set the min frequency\n");
Ben Widawskyd060c162014-03-19 18:31:08 -07003892
3893 ret = sandybridge_pcode_read(dev_priv, GEN6_READ_OC_PARAMS, &pcu_mbox);
3894 if (!ret && (pcu_mbox & (1<<31))) { /* OC supported */
3895 DRM_DEBUG_DRIVER("Overclocking supported. Max: %dMHz, Overclock max: %dMHz\n",
Ben Widawskyb39fb292014-03-19 18:31:11 -07003896 (dev_priv->rps.max_freq_softlimit & 0xff) * 50,
Ben Widawskyd060c162014-03-19 18:31:08 -07003897 (pcu_mbox & 0xff) * 50);
Ben Widawskyb39fb292014-03-19 18:31:11 -07003898 dev_priv->rps.max_freq = pcu_mbox & 0xff;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003899 }
3900
Chris Wilsondd75fdc2013-09-25 17:34:57 +01003901 dev_priv->rps.power = HIGH_POWER; /* force a reset */
Ben Widawskyb39fb292014-03-19 18:31:11 -07003902 gen6_set_rps(dev_priv->dev, dev_priv->rps.min_freq_softlimit);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003903
Daniel Vetter44fc7d52013-07-12 22:43:27 +02003904 gen6_enable_rps_interrupts(dev);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003905
Ben Widawsky31643d52012-09-26 10:34:01 -07003906 rc6vids = 0;
3907 ret = sandybridge_pcode_read(dev_priv, GEN6_PCODE_READ_RC6VIDS, &rc6vids);
3908 if (IS_GEN6(dev) && ret) {
3909 DRM_DEBUG_DRIVER("Couldn't check for BIOS workaround\n");
3910 } else if (IS_GEN6(dev) && (GEN6_DECODE_RC6_VID(rc6vids & 0xff) < 450)) {
3911 DRM_DEBUG_DRIVER("You should update your BIOS. Correcting minimum rc6 voltage (%dmV->%dmV)\n",
3912 GEN6_DECODE_RC6_VID(rc6vids & 0xff), 450);
3913 rc6vids &= 0xffff00;
3914 rc6vids |= GEN6_ENCODE_RC6_VID(450);
3915 ret = sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_RC6VIDS, rc6vids);
3916 if (ret)
3917 DRM_ERROR("Couldn't fix incorrect rc6 voltage\n");
3918 }
3919
Deepak Sc8d9a592013-11-23 14:55:42 +05303920 gen6_gt_force_wake_put(dev_priv, FORCEWAKE_ALL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003921}
3922
Imre Deakc2bc2fc2014-04-18 16:16:23 +03003923static void __gen6_update_ring_freq(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003924{
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02003925 struct drm_i915_private *dev_priv = dev->dev_private;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003926 int min_freq = 15;
Chris Wilson3ebecd02013-04-12 19:10:13 +01003927 unsigned int gpu_freq;
3928 unsigned int max_ia_freq, min_ring_freq;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003929 int scaling_factor = 180;
Ben Widawskyeda79642013-10-07 17:15:48 -03003930 struct cpufreq_policy *policy;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003931
Jesse Barnes4fc688c2012-11-02 11:14:01 -07003932 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02003933
Ben Widawskyeda79642013-10-07 17:15:48 -03003934 policy = cpufreq_cpu_get(0);
3935 if (policy) {
3936 max_ia_freq = policy->cpuinfo.max_freq;
3937 cpufreq_cpu_put(policy);
3938 } else {
3939 /*
3940 * Default to measured freq if none found, PCU will ensure we
3941 * don't go over
3942 */
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003943 max_ia_freq = tsc_khz;
Ben Widawskyeda79642013-10-07 17:15:48 -03003944 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003945
3946 /* Convert from kHz to MHz */
3947 max_ia_freq /= 1000;
3948
Ben Widawsky153b4b952013-10-22 22:05:09 -07003949 min_ring_freq = I915_READ(DCLK) & 0xf;
Ben Widawskyf6aca452013-10-02 09:25:02 -07003950 /* convert DDR frequency from units of 266.6MHz to bandwidth */
3951 min_ring_freq = mult_frac(min_ring_freq, 8, 3);
Chris Wilson3ebecd02013-04-12 19:10:13 +01003952
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003953 /*
3954 * For each potential GPU frequency, load a ring frequency we'd like
3955 * to use for memory access. We do this by specifying the IA frequency
3956 * the PCU should use as a reference to determine the ring frequency.
3957 */
Ben Widawskyb39fb292014-03-19 18:31:11 -07003958 for (gpu_freq = dev_priv->rps.max_freq_softlimit; gpu_freq >= dev_priv->rps.min_freq_softlimit;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003959 gpu_freq--) {
Ben Widawskyb39fb292014-03-19 18:31:11 -07003960 int diff = dev_priv->rps.max_freq_softlimit - gpu_freq;
Chris Wilson3ebecd02013-04-12 19:10:13 +01003961 unsigned int ia_freq = 0, ring_freq = 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003962
Ben Widawsky46c764d2013-11-02 21:07:49 -07003963 if (INTEL_INFO(dev)->gen >= 8) {
3964 /* max(2 * GT, DDR). NB: GT is 50MHz units */
3965 ring_freq = max(min_ring_freq, gpu_freq);
3966 } else if (IS_HASWELL(dev)) {
Ben Widawskyf6aca452013-10-02 09:25:02 -07003967 ring_freq = mult_frac(gpu_freq, 5, 4);
Chris Wilson3ebecd02013-04-12 19:10:13 +01003968 ring_freq = max(min_ring_freq, ring_freq);
3969 /* leave ia_freq as the default, chosen by cpufreq */
3970 } else {
3971 /* On older processors, there is no separate ring
3972 * clock domain, so in order to boost the bandwidth
3973 * of the ring, we need to upclock the CPU (ia_freq).
3974 *
3975 * For GPU frequencies less than 750MHz,
3976 * just use the lowest ring freq.
3977 */
3978 if (gpu_freq < min_freq)
3979 ia_freq = 800;
3980 else
3981 ia_freq = max_ia_freq - ((diff * scaling_factor) / 2);
3982 ia_freq = DIV_ROUND_CLOSEST(ia_freq, 100);
3983 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003984
Ben Widawsky42c05262012-09-26 10:34:00 -07003985 sandybridge_pcode_write(dev_priv,
3986 GEN6_PCODE_WRITE_MIN_FREQ_TABLE,
Chris Wilson3ebecd02013-04-12 19:10:13 +01003987 ia_freq << GEN6_PCODE_FREQ_IA_RATIO_SHIFT |
3988 ring_freq << GEN6_PCODE_FREQ_RING_RATIO_SHIFT |
3989 gpu_freq);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003990 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003991}
3992
Imre Deakc2bc2fc2014-04-18 16:16:23 +03003993void gen6_update_ring_freq(struct drm_device *dev)
3994{
3995 struct drm_i915_private *dev_priv = dev->dev_private;
3996
3997 if (INTEL_INFO(dev)->gen < 6 || IS_VALLEYVIEW(dev))
3998 return;
3999
4000 mutex_lock(&dev_priv->rps.hw_lock);
4001 __gen6_update_ring_freq(dev);
4002 mutex_unlock(&dev_priv->rps.hw_lock);
4003}
4004
Ville Syrjälä03af2042014-06-28 02:03:53 +03004005static int cherryview_rps_max_freq(struct drm_i915_private *dev_priv)
Deepak S2b6b3a02014-05-27 15:59:30 +05304006{
4007 u32 val, rp0;
4008
4009 val = vlv_punit_read(dev_priv, PUNIT_GPU_STATUS_REG);
4010 rp0 = (val >> PUNIT_GPU_STATUS_MAX_FREQ_SHIFT) & PUNIT_GPU_STATUS_MAX_FREQ_MASK;
4011
4012 return rp0;
4013}
4014
4015static int cherryview_rps_rpe_freq(struct drm_i915_private *dev_priv)
4016{
4017 u32 val, rpe;
4018
4019 val = vlv_punit_read(dev_priv, PUNIT_GPU_DUTYCYCLE_REG);
4020 rpe = (val >> PUNIT_GPU_DUTYCYCLE_RPE_FREQ_SHIFT) & PUNIT_GPU_DUTYCYCLE_RPE_FREQ_MASK;
4021
4022 return rpe;
4023}
4024
Deepak S7707df42014-07-12 18:46:14 +05304025static int cherryview_rps_guar_freq(struct drm_i915_private *dev_priv)
4026{
4027 u32 val, rp1;
4028
4029 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
4030 rp1 = (val >> PUNIT_GPU_STATUS_MAX_FREQ_SHIFT) & PUNIT_GPU_STATUS_MAX_FREQ_MASK;
4031
4032 return rp1;
4033}
4034
Ville Syrjälä03af2042014-06-28 02:03:53 +03004035static int cherryview_rps_min_freq(struct drm_i915_private *dev_priv)
Deepak S2b6b3a02014-05-27 15:59:30 +05304036{
4037 u32 val, rpn;
4038
4039 val = vlv_punit_read(dev_priv, PUNIT_GPU_STATUS_REG);
4040 rpn = (val >> PUNIT_GPU_STATIS_GFX_MIN_FREQ_SHIFT) & PUNIT_GPU_STATUS_GFX_MIN_FREQ_MASK;
4041 return rpn;
4042}
4043
Deepak Sf8f2b002014-07-10 13:16:21 +05304044static int valleyview_rps_guar_freq(struct drm_i915_private *dev_priv)
4045{
4046 u32 val, rp1;
4047
4048 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FREQ_FUSE);
4049
4050 rp1 = (val & FB_GFX_FGUARANTEED_FREQ_FUSE_MASK) >> FB_GFX_FGUARANTEED_FREQ_FUSE_SHIFT;
4051
4052 return rp1;
4053}
4054
Ville Syrjälä03af2042014-06-28 02:03:53 +03004055static int valleyview_rps_max_freq(struct drm_i915_private *dev_priv)
Jesse Barnes0a073b82013-04-17 15:54:58 -07004056{
4057 u32 val, rp0;
4058
Jani Nikula64936252013-05-22 15:36:20 +03004059 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FREQ_FUSE);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004060
4061 rp0 = (val & FB_GFX_MAX_FREQ_FUSE_MASK) >> FB_GFX_MAX_FREQ_FUSE_SHIFT;
4062 /* Clamp to max */
4063 rp0 = min_t(u32, rp0, 0xea);
4064
4065 return rp0;
4066}
4067
4068static int valleyview_rps_rpe_freq(struct drm_i915_private *dev_priv)
4069{
4070 u32 val, rpe;
4071
Jani Nikula64936252013-05-22 15:36:20 +03004072 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FMAX_FUSE_LO);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004073 rpe = (val & FB_FMAX_VMIN_FREQ_LO_MASK) >> FB_FMAX_VMIN_FREQ_LO_SHIFT;
Jani Nikula64936252013-05-22 15:36:20 +03004074 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FMAX_FUSE_HI);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004075 rpe |= (val & FB_FMAX_VMIN_FREQ_HI_MASK) << 5;
4076
4077 return rpe;
4078}
4079
Ville Syrjälä03af2042014-06-28 02:03:53 +03004080static int valleyview_rps_min_freq(struct drm_i915_private *dev_priv)
Jesse Barnes0a073b82013-04-17 15:54:58 -07004081{
Jani Nikula64936252013-05-22 15:36:20 +03004082 return vlv_punit_read(dev_priv, PUNIT_REG_GPU_LFM) & 0xff;
Jesse Barnes0a073b82013-04-17 15:54:58 -07004083}
4084
Imre Deakae484342014-03-31 15:10:44 +03004085/* Check that the pctx buffer wasn't move under us. */
4086static void valleyview_check_pctx(struct drm_i915_private *dev_priv)
4087{
4088 unsigned long pctx_addr = I915_READ(VLV_PCBR) & ~4095;
4089
4090 WARN_ON(pctx_addr != dev_priv->mm.stolen_base +
4091 dev_priv->vlv_pctx->stolen->start);
4092}
4093
Deepak S38807742014-05-23 21:00:15 +05304094
4095/* Check that the pcbr address is not empty. */
4096static void cherryview_check_pctx(struct drm_i915_private *dev_priv)
4097{
4098 unsigned long pctx_addr = I915_READ(VLV_PCBR) & ~4095;
4099
4100 WARN_ON((pctx_addr >> VLV_PCBR_ADDR_SHIFT) == 0);
4101}
4102
4103static void cherryview_setup_pctx(struct drm_device *dev)
4104{
4105 struct drm_i915_private *dev_priv = dev->dev_private;
4106 unsigned long pctx_paddr, paddr;
4107 struct i915_gtt *gtt = &dev_priv->gtt;
4108 u32 pcbr;
4109 int pctx_size = 32*1024;
4110
4111 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
4112
4113 pcbr = I915_READ(VLV_PCBR);
4114 if ((pcbr >> VLV_PCBR_ADDR_SHIFT) == 0) {
4115 paddr = (dev_priv->mm.stolen_base +
4116 (gtt->stolen_size - pctx_size));
4117
4118 pctx_paddr = (paddr & (~4095));
4119 I915_WRITE(VLV_PCBR, pctx_paddr);
4120 }
4121}
4122
Jesse Barnesc9cddff2013-05-08 10:45:13 -07004123static void valleyview_setup_pctx(struct drm_device *dev)
4124{
4125 struct drm_i915_private *dev_priv = dev->dev_private;
4126 struct drm_i915_gem_object *pctx;
4127 unsigned long pctx_paddr;
4128 u32 pcbr;
4129 int pctx_size = 24*1024;
4130
Imre Deak17b0c1f2014-02-11 21:39:06 +02004131 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
4132
Jesse Barnesc9cddff2013-05-08 10:45:13 -07004133 pcbr = I915_READ(VLV_PCBR);
4134 if (pcbr) {
4135 /* BIOS set it up already, grab the pre-alloc'd space */
4136 int pcbr_offset;
4137
4138 pcbr_offset = (pcbr & (~4095)) - dev_priv->mm.stolen_base;
4139 pctx = i915_gem_object_create_stolen_for_preallocated(dev_priv->dev,
4140 pcbr_offset,
Daniel Vetter190d6cd2013-07-04 13:06:28 +02004141 I915_GTT_OFFSET_NONE,
Jesse Barnesc9cddff2013-05-08 10:45:13 -07004142 pctx_size);
4143 goto out;
4144 }
4145
4146 /*
4147 * From the Gunit register HAS:
4148 * The Gfx driver is expected to program this register and ensure
4149 * proper allocation within Gfx stolen memory. For example, this
4150 * register should be programmed such than the PCBR range does not
4151 * overlap with other ranges, such as the frame buffer, protected
4152 * memory, or any other relevant ranges.
4153 */
4154 pctx = i915_gem_object_create_stolen(dev, pctx_size);
4155 if (!pctx) {
4156 DRM_DEBUG("not enough stolen space for PCTX, disabling\n");
4157 return;
4158 }
4159
4160 pctx_paddr = dev_priv->mm.stolen_base + pctx->stolen->start;
4161 I915_WRITE(VLV_PCBR, pctx_paddr);
4162
4163out:
4164 dev_priv->vlv_pctx = pctx;
4165}
4166
Imre Deakae484342014-03-31 15:10:44 +03004167static void valleyview_cleanup_pctx(struct drm_device *dev)
4168{
4169 struct drm_i915_private *dev_priv = dev->dev_private;
4170
4171 if (WARN_ON(!dev_priv->vlv_pctx))
4172 return;
4173
4174 drm_gem_object_unreference(&dev_priv->vlv_pctx->base);
4175 dev_priv->vlv_pctx = NULL;
4176}
4177
Imre Deak4e805192014-04-14 20:24:41 +03004178static void valleyview_init_gt_powersave(struct drm_device *dev)
4179{
4180 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03004181 u32 val;
Imre Deak4e805192014-04-14 20:24:41 +03004182
4183 valleyview_setup_pctx(dev);
4184
4185 mutex_lock(&dev_priv->rps.hw_lock);
4186
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03004187 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
4188 switch ((val >> 6) & 3) {
4189 case 0:
4190 case 1:
4191 dev_priv->mem_freq = 800;
4192 break;
4193 case 2:
4194 dev_priv->mem_freq = 1066;
4195 break;
4196 case 3:
4197 dev_priv->mem_freq = 1333;
4198 break;
4199 }
4200 DRM_DEBUG_DRIVER("DDR speed: %d MHz", dev_priv->mem_freq);
4201
Imre Deak4e805192014-04-14 20:24:41 +03004202 dev_priv->rps.max_freq = valleyview_rps_max_freq(dev_priv);
4203 dev_priv->rps.rp0_freq = dev_priv->rps.max_freq;
4204 DRM_DEBUG_DRIVER("max GPU freq: %d MHz (%u)\n",
4205 vlv_gpu_freq(dev_priv, dev_priv->rps.max_freq),
4206 dev_priv->rps.max_freq);
4207
4208 dev_priv->rps.efficient_freq = valleyview_rps_rpe_freq(dev_priv);
4209 DRM_DEBUG_DRIVER("RPe GPU freq: %d MHz (%u)\n",
4210 vlv_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
4211 dev_priv->rps.efficient_freq);
4212
Deepak Sf8f2b002014-07-10 13:16:21 +05304213 dev_priv->rps.rp1_freq = valleyview_rps_guar_freq(dev_priv);
4214 DRM_DEBUG_DRIVER("RP1(Guar Freq) GPU freq: %d MHz (%u)\n",
4215 vlv_gpu_freq(dev_priv, dev_priv->rps.rp1_freq),
4216 dev_priv->rps.rp1_freq);
4217
Imre Deak4e805192014-04-14 20:24:41 +03004218 dev_priv->rps.min_freq = valleyview_rps_min_freq(dev_priv);
4219 DRM_DEBUG_DRIVER("min GPU freq: %d MHz (%u)\n",
4220 vlv_gpu_freq(dev_priv, dev_priv->rps.min_freq),
4221 dev_priv->rps.min_freq);
4222
4223 /* Preserve min/max settings in case of re-init */
4224 if (dev_priv->rps.max_freq_softlimit == 0)
4225 dev_priv->rps.max_freq_softlimit = dev_priv->rps.max_freq;
4226
4227 if (dev_priv->rps.min_freq_softlimit == 0)
4228 dev_priv->rps.min_freq_softlimit = dev_priv->rps.min_freq;
4229
4230 mutex_unlock(&dev_priv->rps.hw_lock);
4231}
4232
Deepak S38807742014-05-23 21:00:15 +05304233static void cherryview_init_gt_powersave(struct drm_device *dev)
4234{
Deepak S2b6b3a02014-05-27 15:59:30 +05304235 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03004236 u32 val;
Deepak S2b6b3a02014-05-27 15:59:30 +05304237
Deepak S38807742014-05-23 21:00:15 +05304238 cherryview_setup_pctx(dev);
Deepak S2b6b3a02014-05-27 15:59:30 +05304239
4240 mutex_lock(&dev_priv->rps.hw_lock);
4241
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03004242 val = vlv_punit_read(dev_priv, CCK_FUSE_REG);
4243 switch ((val >> 2) & 0x7) {
4244 case 0:
4245 case 1:
4246 dev_priv->rps.cz_freq = 200;
4247 dev_priv->mem_freq = 1600;
4248 break;
4249 case 2:
4250 dev_priv->rps.cz_freq = 267;
4251 dev_priv->mem_freq = 1600;
4252 break;
4253 case 3:
4254 dev_priv->rps.cz_freq = 333;
4255 dev_priv->mem_freq = 2000;
4256 break;
4257 case 4:
4258 dev_priv->rps.cz_freq = 320;
4259 dev_priv->mem_freq = 1600;
4260 break;
4261 case 5:
4262 dev_priv->rps.cz_freq = 400;
4263 dev_priv->mem_freq = 1600;
4264 break;
4265 }
4266 DRM_DEBUG_DRIVER("DDR speed: %d MHz", dev_priv->mem_freq);
4267
Deepak S2b6b3a02014-05-27 15:59:30 +05304268 dev_priv->rps.max_freq = cherryview_rps_max_freq(dev_priv);
4269 dev_priv->rps.rp0_freq = dev_priv->rps.max_freq;
4270 DRM_DEBUG_DRIVER("max GPU freq: %d MHz (%u)\n",
4271 vlv_gpu_freq(dev_priv, dev_priv->rps.max_freq),
4272 dev_priv->rps.max_freq);
4273
4274 dev_priv->rps.efficient_freq = cherryview_rps_rpe_freq(dev_priv);
4275 DRM_DEBUG_DRIVER("RPe GPU freq: %d MHz (%u)\n",
4276 vlv_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
4277 dev_priv->rps.efficient_freq);
4278
Deepak S7707df42014-07-12 18:46:14 +05304279 dev_priv->rps.rp1_freq = cherryview_rps_guar_freq(dev_priv);
4280 DRM_DEBUG_DRIVER("RP1(Guar) GPU freq: %d MHz (%u)\n",
4281 vlv_gpu_freq(dev_priv, dev_priv->rps.rp1_freq),
4282 dev_priv->rps.rp1_freq);
4283
Deepak S2b6b3a02014-05-27 15:59:30 +05304284 dev_priv->rps.min_freq = cherryview_rps_min_freq(dev_priv);
4285 DRM_DEBUG_DRIVER("min GPU freq: %d MHz (%u)\n",
4286 vlv_gpu_freq(dev_priv, dev_priv->rps.min_freq),
4287 dev_priv->rps.min_freq);
4288
Ville Syrjälä1c147622014-08-18 14:42:43 +03004289 WARN_ONCE((dev_priv->rps.max_freq |
4290 dev_priv->rps.efficient_freq |
4291 dev_priv->rps.rp1_freq |
4292 dev_priv->rps.min_freq) & 1,
4293 "Odd GPU freq values\n");
4294
Deepak S2b6b3a02014-05-27 15:59:30 +05304295 /* Preserve min/max settings in case of re-init */
4296 if (dev_priv->rps.max_freq_softlimit == 0)
4297 dev_priv->rps.max_freq_softlimit = dev_priv->rps.max_freq;
4298
4299 if (dev_priv->rps.min_freq_softlimit == 0)
4300 dev_priv->rps.min_freq_softlimit = dev_priv->rps.min_freq;
4301
4302 mutex_unlock(&dev_priv->rps.hw_lock);
Deepak S38807742014-05-23 21:00:15 +05304303}
4304
Imre Deak4e805192014-04-14 20:24:41 +03004305static void valleyview_cleanup_gt_powersave(struct drm_device *dev)
4306{
4307 valleyview_cleanup_pctx(dev);
4308}
4309
Deepak S38807742014-05-23 21:00:15 +05304310static void cherryview_enable_rps(struct drm_device *dev)
4311{
4312 struct drm_i915_private *dev_priv = dev->dev_private;
4313 struct intel_engine_cs *ring;
Deepak S2b6b3a02014-05-27 15:59:30 +05304314 u32 gtfifodbg, val, rc6_mode = 0, pcbr;
Deepak S38807742014-05-23 21:00:15 +05304315 int i;
4316
4317 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
4318
4319 gtfifodbg = I915_READ(GTFIFODBG);
4320 if (gtfifodbg) {
4321 DRM_DEBUG_DRIVER("GT fifo had a previous error %x\n",
4322 gtfifodbg);
4323 I915_WRITE(GTFIFODBG, gtfifodbg);
4324 }
4325
4326 cherryview_check_pctx(dev_priv);
4327
4328 /* 1a & 1b: Get forcewake during program sequence. Although the driver
4329 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
4330 gen6_gt_force_wake_get(dev_priv, FORCEWAKE_ALL);
4331
4332 /* 2a: Program RC6 thresholds.*/
4333 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16);
4334 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
4335 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
4336
4337 for_each_ring(ring, dev_priv, i)
4338 I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
4339 I915_WRITE(GEN6_RC_SLEEP, 0);
4340
4341 I915_WRITE(GEN6_RC6_THRESHOLD, 50000); /* 50/125ms per EI */
4342
4343 /* allows RC6 residency counter to work */
4344 I915_WRITE(VLV_COUNTER_CONTROL,
4345 _MASKED_BIT_ENABLE(VLV_COUNT_RANGE_HIGH |
4346 VLV_MEDIA_RC6_COUNT_EN |
4347 VLV_RENDER_RC6_COUNT_EN));
4348
4349 /* For now we assume BIOS is allocating and populating the PCBR */
4350 pcbr = I915_READ(VLV_PCBR);
4351
4352 DRM_DEBUG_DRIVER("PCBR offset : 0x%x\n", pcbr);
4353
4354 /* 3: Enable RC6 */
4355 if ((intel_enable_rc6(dev) & INTEL_RC6_ENABLE) &&
4356 (pcbr >> VLV_PCBR_ADDR_SHIFT))
4357 rc6_mode = GEN6_RC_CTL_EI_MODE(1);
4358
4359 I915_WRITE(GEN6_RC_CONTROL, rc6_mode);
4360
Deepak S2b6b3a02014-05-27 15:59:30 +05304361 /* 4 Program defaults and thresholds for RPS*/
4362 I915_WRITE(GEN6_RP_UP_THRESHOLD, 59400);
4363 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 245000);
4364 I915_WRITE(GEN6_RP_UP_EI, 66000);
4365 I915_WRITE(GEN6_RP_DOWN_EI, 350000);
4366
4367 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
4368
Tom O'Rourke7405f422014-06-10 16:26:34 -07004369 /* WaDisablePwrmtrEvent:chv (pre-production hw) */
4370 I915_WRITE(0xA80C, I915_READ(0xA80C) & 0x00ffffff);
4371 I915_WRITE(0xA810, I915_READ(0xA810) & 0xffffff00);
4372
Deepak S2b6b3a02014-05-27 15:59:30 +05304373 /* 5: Enable RPS */
4374 I915_WRITE(GEN6_RP_CONTROL,
4375 GEN6_RP_MEDIA_HW_NORMAL_MODE |
Tom O'Rourke7405f422014-06-10 16:26:34 -07004376 GEN6_RP_MEDIA_IS_GFX | /* WaSetMaskForGfxBusyness:chv (pre-production hw ?) */
Deepak S2b6b3a02014-05-27 15:59:30 +05304377 GEN6_RP_ENABLE |
4378 GEN6_RP_UP_BUSY_AVG |
4379 GEN6_RP_DOWN_IDLE_AVG);
4380
4381 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
4382
4383 DRM_DEBUG_DRIVER("GPLL enabled? %s\n", val & 0x10 ? "yes" : "no");
4384 DRM_DEBUG_DRIVER("GPU status: 0x%08x\n", val);
4385
4386 dev_priv->rps.cur_freq = (val >> 8) & 0xff;
4387 DRM_DEBUG_DRIVER("current GPU freq: %d MHz (%u)\n",
4388 vlv_gpu_freq(dev_priv, dev_priv->rps.cur_freq),
4389 dev_priv->rps.cur_freq);
4390
4391 DRM_DEBUG_DRIVER("setting GPU freq to %d MHz (%u)\n",
4392 vlv_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
4393 dev_priv->rps.efficient_freq);
4394
4395 valleyview_set_rps(dev_priv->dev, dev_priv->rps.efficient_freq);
4396
Deepak S3497a562014-07-10 13:16:26 +05304397 gen8_enable_rps_interrupts(dev);
4398
Deepak S38807742014-05-23 21:00:15 +05304399 gen6_gt_force_wake_put(dev_priv, FORCEWAKE_ALL);
4400}
4401
Jesse Barnes0a073b82013-04-17 15:54:58 -07004402static void valleyview_enable_rps(struct drm_device *dev)
4403{
4404 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01004405 struct intel_engine_cs *ring;
Ben Widawsky2a5913a2014-03-19 18:31:13 -07004406 u32 gtfifodbg, val, rc6_mode = 0;
Jesse Barnes0a073b82013-04-17 15:54:58 -07004407 int i;
4408
4409 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
4410
Imre Deakae484342014-03-31 15:10:44 +03004411 valleyview_check_pctx(dev_priv);
4412
Jesse Barnes0a073b82013-04-17 15:54:58 -07004413 if ((gtfifodbg = I915_READ(GTFIFODBG))) {
Jesse Barnesf7d85c12013-09-27 10:40:54 -07004414 DRM_DEBUG_DRIVER("GT fifo had a previous error %x\n",
4415 gtfifodbg);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004416 I915_WRITE(GTFIFODBG, gtfifodbg);
4417 }
4418
Deepak Sc8d9a592013-11-23 14:55:42 +05304419 /* If VLV, Forcewake all wells, else re-direct to regular path */
4420 gen6_gt_force_wake_get(dev_priv, FORCEWAKE_ALL);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004421
4422 I915_WRITE(GEN6_RP_UP_THRESHOLD, 59400);
4423 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 245000);
4424 I915_WRITE(GEN6_RP_UP_EI, 66000);
4425 I915_WRITE(GEN6_RP_DOWN_EI, 350000);
4426
4427 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
Deepak S31685c22014-07-03 17:33:01 -04004428 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 0xf4240);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004429
4430 I915_WRITE(GEN6_RP_CONTROL,
4431 GEN6_RP_MEDIA_TURBO |
4432 GEN6_RP_MEDIA_HW_NORMAL_MODE |
4433 GEN6_RP_MEDIA_IS_GFX |
4434 GEN6_RP_ENABLE |
4435 GEN6_RP_UP_BUSY_AVG |
4436 GEN6_RP_DOWN_IDLE_CONT);
4437
4438 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 0x00280000);
4439 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
4440 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
4441
4442 for_each_ring(ring, dev_priv, i)
4443 I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
4444
Jesse Barnes2f0aa302013-11-15 09:32:11 -08004445 I915_WRITE(GEN6_RC6_THRESHOLD, 0x557);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004446
4447 /* allows RC6 residency counter to work */
Jesse Barnes49798eb2013-09-26 17:55:57 -07004448 I915_WRITE(VLV_COUNTER_CONTROL,
Deepak S31685c22014-07-03 17:33:01 -04004449 _MASKED_BIT_ENABLE(VLV_MEDIA_RC0_COUNT_EN |
4450 VLV_RENDER_RC0_COUNT_EN |
Jesse Barnes49798eb2013-09-26 17:55:57 -07004451 VLV_MEDIA_RC6_COUNT_EN |
4452 VLV_RENDER_RC6_COUNT_EN));
Deepak S31685c22014-07-03 17:33:01 -04004453
Jesse Barnesa2b23fe2013-09-19 09:33:13 -07004454 if (intel_enable_rc6(dev) & INTEL_RC6_ENABLE)
Jesse Barnes6b88f292013-11-15 09:32:12 -08004455 rc6_mode = GEN7_RC_CTL_TO_MODE | VLV_RC_CTL_CTX_RST_PARALLEL;
Ben Widawskydc39fff2013-10-18 12:32:07 -07004456
4457 intel_print_rc6_info(dev, rc6_mode);
4458
Jesse Barnesa2b23fe2013-09-19 09:33:13 -07004459 I915_WRITE(GEN6_RC_CONTROL, rc6_mode);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004460
Jani Nikula64936252013-05-22 15:36:20 +03004461 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004462
4463 DRM_DEBUG_DRIVER("GPLL enabled? %s\n", val & 0x10 ? "yes" : "no");
4464 DRM_DEBUG_DRIVER("GPU status: 0x%08x\n", val);
4465
Ben Widawskyb39fb292014-03-19 18:31:11 -07004466 dev_priv->rps.cur_freq = (val >> 8) & 0xff;
Ville Syrjälä73008b92013-06-25 19:21:01 +03004467 DRM_DEBUG_DRIVER("current GPU freq: %d MHz (%u)\n",
Ben Widawskyb39fb292014-03-19 18:31:11 -07004468 vlv_gpu_freq(dev_priv, dev_priv->rps.cur_freq),
4469 dev_priv->rps.cur_freq);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004470
Ville Syrjälä73008b92013-06-25 19:21:01 +03004471 DRM_DEBUG_DRIVER("setting GPU freq to %d MHz (%u)\n",
Ben Widawskyb39fb292014-03-19 18:31:11 -07004472 vlv_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
4473 dev_priv->rps.efficient_freq);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004474
Ben Widawskyb39fb292014-03-19 18:31:11 -07004475 valleyview_set_rps(dev_priv->dev, dev_priv->rps.efficient_freq);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004476
Daniel Vetter44fc7d52013-07-12 22:43:27 +02004477 gen6_enable_rps_interrupts(dev);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004478
Deepak Sc8d9a592013-11-23 14:55:42 +05304479 gen6_gt_force_wake_put(dev_priv, FORCEWAKE_ALL);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004480}
4481
Daniel Vetter930ebb42012-06-29 23:32:16 +02004482void ironlake_teardown_rc6(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004483{
4484 struct drm_i915_private *dev_priv = dev->dev_private;
4485
Daniel Vetter3e373942012-11-02 19:55:04 +01004486 if (dev_priv->ips.renderctx) {
Ben Widawskyd7f46fc2013-12-06 14:10:55 -08004487 i915_gem_object_ggtt_unpin(dev_priv->ips.renderctx);
Daniel Vetter3e373942012-11-02 19:55:04 +01004488 drm_gem_object_unreference(&dev_priv->ips.renderctx->base);
4489 dev_priv->ips.renderctx = NULL;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004490 }
4491
Daniel Vetter3e373942012-11-02 19:55:04 +01004492 if (dev_priv->ips.pwrctx) {
Ben Widawskyd7f46fc2013-12-06 14:10:55 -08004493 i915_gem_object_ggtt_unpin(dev_priv->ips.pwrctx);
Daniel Vetter3e373942012-11-02 19:55:04 +01004494 drm_gem_object_unreference(&dev_priv->ips.pwrctx->base);
4495 dev_priv->ips.pwrctx = NULL;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004496 }
4497}
4498
Daniel Vetter930ebb42012-06-29 23:32:16 +02004499static void ironlake_disable_rc6(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004500{
4501 struct drm_i915_private *dev_priv = dev->dev_private;
4502
4503 if (I915_READ(PWRCTXA)) {
4504 /* Wake the GPU, prevent RC6, then restore RSTDBYCTL */
4505 I915_WRITE(RSTDBYCTL, I915_READ(RSTDBYCTL) | RCX_SW_EXIT);
4506 wait_for(((I915_READ(RSTDBYCTL) & RSX_STATUS_MASK) == RSX_STATUS_ON),
4507 50);
4508
4509 I915_WRITE(PWRCTXA, 0);
4510 POSTING_READ(PWRCTXA);
4511
4512 I915_WRITE(RSTDBYCTL, I915_READ(RSTDBYCTL) & ~RCX_SW_EXIT);
4513 POSTING_READ(RSTDBYCTL);
4514 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004515}
4516
4517static int ironlake_setup_rc6(struct drm_device *dev)
4518{
4519 struct drm_i915_private *dev_priv = dev->dev_private;
4520
Daniel Vetter3e373942012-11-02 19:55:04 +01004521 if (dev_priv->ips.renderctx == NULL)
4522 dev_priv->ips.renderctx = intel_alloc_context_page(dev);
4523 if (!dev_priv->ips.renderctx)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004524 return -ENOMEM;
4525
Daniel Vetter3e373942012-11-02 19:55:04 +01004526 if (dev_priv->ips.pwrctx == NULL)
4527 dev_priv->ips.pwrctx = intel_alloc_context_page(dev);
4528 if (!dev_priv->ips.pwrctx) {
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004529 ironlake_teardown_rc6(dev);
4530 return -ENOMEM;
4531 }
4532
4533 return 0;
4534}
4535
Daniel Vetter930ebb42012-06-29 23:32:16 +02004536static void ironlake_enable_rc6(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004537{
4538 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01004539 struct intel_engine_cs *ring = &dev_priv->ring[RCS];
Chris Wilson3e960502012-11-27 16:22:54 +00004540 bool was_interruptible;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004541 int ret;
4542
4543 /* rc6 disabled by default due to repeated reports of hanging during
4544 * boot and resume.
4545 */
4546 if (!intel_enable_rc6(dev))
4547 return;
4548
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02004549 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
4550
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004551 ret = ironlake_setup_rc6(dev);
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02004552 if (ret)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004553 return;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004554
Chris Wilson3e960502012-11-27 16:22:54 +00004555 was_interruptible = dev_priv->mm.interruptible;
4556 dev_priv->mm.interruptible = false;
4557
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004558 /*
4559 * GPU can automatically power down the render unit if given a page
4560 * to save state.
4561 */
Daniel Vetter6d90c952012-04-26 23:28:05 +02004562 ret = intel_ring_begin(ring, 6);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004563 if (ret) {
4564 ironlake_teardown_rc6(dev);
Chris Wilson3e960502012-11-27 16:22:54 +00004565 dev_priv->mm.interruptible = was_interruptible;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004566 return;
4567 }
4568
Daniel Vetter6d90c952012-04-26 23:28:05 +02004569 intel_ring_emit(ring, MI_SUSPEND_FLUSH | MI_SUSPEND_FLUSH_EN);
4570 intel_ring_emit(ring, MI_SET_CONTEXT);
Ben Widawskyf343c5f2013-07-05 14:41:04 -07004571 intel_ring_emit(ring, i915_gem_obj_ggtt_offset(dev_priv->ips.renderctx) |
Daniel Vetter6d90c952012-04-26 23:28:05 +02004572 MI_MM_SPACE_GTT |
4573 MI_SAVE_EXT_STATE_EN |
4574 MI_RESTORE_EXT_STATE_EN |
4575 MI_RESTORE_INHIBIT);
4576 intel_ring_emit(ring, MI_SUSPEND_FLUSH);
4577 intel_ring_emit(ring, MI_NOOP);
4578 intel_ring_emit(ring, MI_FLUSH);
4579 intel_ring_advance(ring);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004580
4581 /*
4582 * Wait for the command parser to advance past MI_SET_CONTEXT. The HW
4583 * does an implicit flush, combined with MI_FLUSH above, it should be
4584 * safe to assume that renderctx is valid
4585 */
Chris Wilson3e960502012-11-27 16:22:54 +00004586 ret = intel_ring_idle(ring);
4587 dev_priv->mm.interruptible = was_interruptible;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004588 if (ret) {
Jani Nikuladef27a52013-03-12 10:49:19 +02004589 DRM_ERROR("failed to enable ironlake power savings\n");
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004590 ironlake_teardown_rc6(dev);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004591 return;
4592 }
4593
Ben Widawskyf343c5f2013-07-05 14:41:04 -07004594 I915_WRITE(PWRCTXA, i915_gem_obj_ggtt_offset(dev_priv->ips.pwrctx) | PWRCTX_EN);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004595 I915_WRITE(RSTDBYCTL, I915_READ(RSTDBYCTL) & ~RCX_SW_EXIT);
Ben Widawskydc39fff2013-10-18 12:32:07 -07004596
Imre Deak91ca6892014-04-14 20:24:25 +03004597 intel_print_rc6_info(dev, GEN6_RC_CTL_RC6_ENABLE);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004598}
4599
Eugeni Dodonovdde18882012-04-18 15:29:24 -03004600static unsigned long intel_pxfreq(u32 vidfreq)
4601{
4602 unsigned long freq;
4603 int div = (vidfreq & 0x3f0000) >> 16;
4604 int post = (vidfreq & 0x3000) >> 12;
4605 int pre = (vidfreq & 0x7);
4606
4607 if (!pre)
4608 return 0;
4609
4610 freq = ((div * 133333) / ((1<<post) * pre));
4611
4612 return freq;
4613}
4614
Daniel Vettereb48eb02012-04-26 23:28:12 +02004615static const struct cparams {
4616 u16 i;
4617 u16 t;
4618 u16 m;
4619 u16 c;
4620} cparams[] = {
4621 { 1, 1333, 301, 28664 },
4622 { 1, 1066, 294, 24460 },
4623 { 1, 800, 294, 25192 },
4624 { 0, 1333, 276, 27605 },
4625 { 0, 1066, 276, 27605 },
4626 { 0, 800, 231, 23784 },
4627};
4628
Chris Wilsonf531dcb2012-09-25 10:16:12 +01004629static unsigned long __i915_chipset_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02004630{
4631 u64 total_count, diff, ret;
4632 u32 count1, count2, count3, m = 0, c = 0;
4633 unsigned long now = jiffies_to_msecs(jiffies), diff1;
4634 int i;
4635
Daniel Vetter02d71952012-08-09 16:44:54 +02004636 assert_spin_locked(&mchdev_lock);
4637
Daniel Vetter20e4d402012-08-08 23:35:39 +02004638 diff1 = now - dev_priv->ips.last_time1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02004639
4640 /* Prevent division-by-zero if we are asking too fast.
4641 * Also, we don't get interesting results if we are polling
4642 * faster than once in 10ms, so just return the saved value
4643 * in such cases.
4644 */
4645 if (diff1 <= 10)
Daniel Vetter20e4d402012-08-08 23:35:39 +02004646 return dev_priv->ips.chipset_power;
Daniel Vettereb48eb02012-04-26 23:28:12 +02004647
4648 count1 = I915_READ(DMIEC);
4649 count2 = I915_READ(DDREC);
4650 count3 = I915_READ(CSIEC);
4651
4652 total_count = count1 + count2 + count3;
4653
4654 /* FIXME: handle per-counter overflow */
Daniel Vetter20e4d402012-08-08 23:35:39 +02004655 if (total_count < dev_priv->ips.last_count1) {
4656 diff = ~0UL - dev_priv->ips.last_count1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02004657 diff += total_count;
4658 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +02004659 diff = total_count - dev_priv->ips.last_count1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02004660 }
4661
4662 for (i = 0; i < ARRAY_SIZE(cparams); i++) {
Daniel Vetter20e4d402012-08-08 23:35:39 +02004663 if (cparams[i].i == dev_priv->ips.c_m &&
4664 cparams[i].t == dev_priv->ips.r_t) {
Daniel Vettereb48eb02012-04-26 23:28:12 +02004665 m = cparams[i].m;
4666 c = cparams[i].c;
4667 break;
4668 }
4669 }
4670
4671 diff = div_u64(diff, diff1);
4672 ret = ((m * diff) + c);
4673 ret = div_u64(ret, 10);
4674
Daniel Vetter20e4d402012-08-08 23:35:39 +02004675 dev_priv->ips.last_count1 = total_count;
4676 dev_priv->ips.last_time1 = now;
Daniel Vettereb48eb02012-04-26 23:28:12 +02004677
Daniel Vetter20e4d402012-08-08 23:35:39 +02004678 dev_priv->ips.chipset_power = ret;
Daniel Vettereb48eb02012-04-26 23:28:12 +02004679
4680 return ret;
4681}
4682
Chris Wilsonf531dcb2012-09-25 10:16:12 +01004683unsigned long i915_chipset_val(struct drm_i915_private *dev_priv)
4684{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00004685 struct drm_device *dev = dev_priv->dev;
Chris Wilsonf531dcb2012-09-25 10:16:12 +01004686 unsigned long val;
4687
Damien Lespiau3d13ef22014-02-07 19:12:47 +00004688 if (INTEL_INFO(dev)->gen != 5)
Chris Wilsonf531dcb2012-09-25 10:16:12 +01004689 return 0;
4690
4691 spin_lock_irq(&mchdev_lock);
4692
4693 val = __i915_chipset_val(dev_priv);
4694
4695 spin_unlock_irq(&mchdev_lock);
4696
4697 return val;
4698}
4699
Daniel Vettereb48eb02012-04-26 23:28:12 +02004700unsigned long i915_mch_val(struct drm_i915_private *dev_priv)
4701{
4702 unsigned long m, x, b;
4703 u32 tsfs;
4704
4705 tsfs = I915_READ(TSFS);
4706
4707 m = ((tsfs & TSFS_SLOPE_MASK) >> TSFS_SLOPE_SHIFT);
4708 x = I915_READ8(TR1);
4709
4710 b = tsfs & TSFS_INTR_MASK;
4711
4712 return ((m * x) / 127) - b;
4713}
4714
4715static u16 pvid_to_extvid(struct drm_i915_private *dev_priv, u8 pxvid)
4716{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00004717 struct drm_device *dev = dev_priv->dev;
Daniel Vettereb48eb02012-04-26 23:28:12 +02004718 static const struct v_table {
4719 u16 vd; /* in .1 mil */
4720 u16 vm; /* in .1 mil */
4721 } v_table[] = {
4722 { 0, 0, },
4723 { 375, 0, },
4724 { 500, 0, },
4725 { 625, 0, },
4726 { 750, 0, },
4727 { 875, 0, },
4728 { 1000, 0, },
4729 { 1125, 0, },
4730 { 4125, 3000, },
4731 { 4125, 3000, },
4732 { 4125, 3000, },
4733 { 4125, 3000, },
4734 { 4125, 3000, },
4735 { 4125, 3000, },
4736 { 4125, 3000, },
4737 { 4125, 3000, },
4738 { 4125, 3000, },
4739 { 4125, 3000, },
4740 { 4125, 3000, },
4741 { 4125, 3000, },
4742 { 4125, 3000, },
4743 { 4125, 3000, },
4744 { 4125, 3000, },
4745 { 4125, 3000, },
4746 { 4125, 3000, },
4747 { 4125, 3000, },
4748 { 4125, 3000, },
4749 { 4125, 3000, },
4750 { 4125, 3000, },
4751 { 4125, 3000, },
4752 { 4125, 3000, },
4753 { 4125, 3000, },
4754 { 4250, 3125, },
4755 { 4375, 3250, },
4756 { 4500, 3375, },
4757 { 4625, 3500, },
4758 { 4750, 3625, },
4759 { 4875, 3750, },
4760 { 5000, 3875, },
4761 { 5125, 4000, },
4762 { 5250, 4125, },
4763 { 5375, 4250, },
4764 { 5500, 4375, },
4765 { 5625, 4500, },
4766 { 5750, 4625, },
4767 { 5875, 4750, },
4768 { 6000, 4875, },
4769 { 6125, 5000, },
4770 { 6250, 5125, },
4771 { 6375, 5250, },
4772 { 6500, 5375, },
4773 { 6625, 5500, },
4774 { 6750, 5625, },
4775 { 6875, 5750, },
4776 { 7000, 5875, },
4777 { 7125, 6000, },
4778 { 7250, 6125, },
4779 { 7375, 6250, },
4780 { 7500, 6375, },
4781 { 7625, 6500, },
4782 { 7750, 6625, },
4783 { 7875, 6750, },
4784 { 8000, 6875, },
4785 { 8125, 7000, },
4786 { 8250, 7125, },
4787 { 8375, 7250, },
4788 { 8500, 7375, },
4789 { 8625, 7500, },
4790 { 8750, 7625, },
4791 { 8875, 7750, },
4792 { 9000, 7875, },
4793 { 9125, 8000, },
4794 { 9250, 8125, },
4795 { 9375, 8250, },
4796 { 9500, 8375, },
4797 { 9625, 8500, },
4798 { 9750, 8625, },
4799 { 9875, 8750, },
4800 { 10000, 8875, },
4801 { 10125, 9000, },
4802 { 10250, 9125, },
4803 { 10375, 9250, },
4804 { 10500, 9375, },
4805 { 10625, 9500, },
4806 { 10750, 9625, },
4807 { 10875, 9750, },
4808 { 11000, 9875, },
4809 { 11125, 10000, },
4810 { 11250, 10125, },
4811 { 11375, 10250, },
4812 { 11500, 10375, },
4813 { 11625, 10500, },
4814 { 11750, 10625, },
4815 { 11875, 10750, },
4816 { 12000, 10875, },
4817 { 12125, 11000, },
4818 { 12250, 11125, },
4819 { 12375, 11250, },
4820 { 12500, 11375, },
4821 { 12625, 11500, },
4822 { 12750, 11625, },
4823 { 12875, 11750, },
4824 { 13000, 11875, },
4825 { 13125, 12000, },
4826 { 13250, 12125, },
4827 { 13375, 12250, },
4828 { 13500, 12375, },
4829 { 13625, 12500, },
4830 { 13750, 12625, },
4831 { 13875, 12750, },
4832 { 14000, 12875, },
4833 { 14125, 13000, },
4834 { 14250, 13125, },
4835 { 14375, 13250, },
4836 { 14500, 13375, },
4837 { 14625, 13500, },
4838 { 14750, 13625, },
4839 { 14875, 13750, },
4840 { 15000, 13875, },
4841 { 15125, 14000, },
4842 { 15250, 14125, },
4843 { 15375, 14250, },
4844 { 15500, 14375, },
4845 { 15625, 14500, },
4846 { 15750, 14625, },
4847 { 15875, 14750, },
4848 { 16000, 14875, },
4849 { 16125, 15000, },
4850 };
Damien Lespiau3d13ef22014-02-07 19:12:47 +00004851 if (INTEL_INFO(dev)->is_mobile)
Daniel Vettereb48eb02012-04-26 23:28:12 +02004852 return v_table[pxvid].vm;
4853 else
4854 return v_table[pxvid].vd;
4855}
4856
Daniel Vetter02d71952012-08-09 16:44:54 +02004857static void __i915_update_gfx_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02004858{
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00004859 u64 now, diff, diffms;
Daniel Vettereb48eb02012-04-26 23:28:12 +02004860 u32 count;
4861
Daniel Vetter02d71952012-08-09 16:44:54 +02004862 assert_spin_locked(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004863
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00004864 now = ktime_get_raw_ns();
4865 diffms = now - dev_priv->ips.last_time2;
4866 do_div(diffms, NSEC_PER_MSEC);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004867
4868 /* Don't divide by 0 */
Daniel Vettereb48eb02012-04-26 23:28:12 +02004869 if (!diffms)
4870 return;
4871
4872 count = I915_READ(GFXEC);
4873
Daniel Vetter20e4d402012-08-08 23:35:39 +02004874 if (count < dev_priv->ips.last_count2) {
4875 diff = ~0UL - dev_priv->ips.last_count2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02004876 diff += count;
4877 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +02004878 diff = count - dev_priv->ips.last_count2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02004879 }
4880
Daniel Vetter20e4d402012-08-08 23:35:39 +02004881 dev_priv->ips.last_count2 = count;
4882 dev_priv->ips.last_time2 = now;
Daniel Vettereb48eb02012-04-26 23:28:12 +02004883
4884 /* More magic constants... */
4885 diff = diff * 1181;
4886 diff = div_u64(diff, diffms * 10);
Daniel Vetter20e4d402012-08-08 23:35:39 +02004887 dev_priv->ips.gfx_power = diff;
Daniel Vettereb48eb02012-04-26 23:28:12 +02004888}
4889
Daniel Vetter02d71952012-08-09 16:44:54 +02004890void i915_update_gfx_val(struct drm_i915_private *dev_priv)
4891{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00004892 struct drm_device *dev = dev_priv->dev;
4893
4894 if (INTEL_INFO(dev)->gen != 5)
Daniel Vetter02d71952012-08-09 16:44:54 +02004895 return;
4896
Daniel Vetter92703882012-08-09 16:46:01 +02004897 spin_lock_irq(&mchdev_lock);
Daniel Vetter02d71952012-08-09 16:44:54 +02004898
4899 __i915_update_gfx_val(dev_priv);
4900
Daniel Vetter92703882012-08-09 16:46:01 +02004901 spin_unlock_irq(&mchdev_lock);
Daniel Vetter02d71952012-08-09 16:44:54 +02004902}
4903
Chris Wilsonf531dcb2012-09-25 10:16:12 +01004904static unsigned long __i915_gfx_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02004905{
4906 unsigned long t, corr, state1, corr2, state2;
4907 u32 pxvid, ext_v;
4908
Daniel Vetter02d71952012-08-09 16:44:54 +02004909 assert_spin_locked(&mchdev_lock);
4910
Ben Widawskyb39fb292014-03-19 18:31:11 -07004911 pxvid = I915_READ(PXVFREQ_BASE + (dev_priv->rps.cur_freq * 4));
Daniel Vettereb48eb02012-04-26 23:28:12 +02004912 pxvid = (pxvid >> 24) & 0x7f;
4913 ext_v = pvid_to_extvid(dev_priv, pxvid);
4914
4915 state1 = ext_v;
4916
4917 t = i915_mch_val(dev_priv);
4918
4919 /* Revel in the empirically derived constants */
4920
4921 /* Correction factor in 1/100000 units */
4922 if (t > 80)
4923 corr = ((t * 2349) + 135940);
4924 else if (t >= 50)
4925 corr = ((t * 964) + 29317);
4926 else /* < 50 */
4927 corr = ((t * 301) + 1004);
4928
4929 corr = corr * ((150142 * state1) / 10000 - 78642);
4930 corr /= 100000;
Daniel Vetter20e4d402012-08-08 23:35:39 +02004931 corr2 = (corr * dev_priv->ips.corr);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004932
4933 state2 = (corr2 * state1) / 10000;
4934 state2 /= 100; /* convert to mW */
4935
Daniel Vetter02d71952012-08-09 16:44:54 +02004936 __i915_update_gfx_val(dev_priv);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004937
Daniel Vetter20e4d402012-08-08 23:35:39 +02004938 return dev_priv->ips.gfx_power + state2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02004939}
4940
Chris Wilsonf531dcb2012-09-25 10:16:12 +01004941unsigned long i915_gfx_val(struct drm_i915_private *dev_priv)
4942{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00004943 struct drm_device *dev = dev_priv->dev;
Chris Wilsonf531dcb2012-09-25 10:16:12 +01004944 unsigned long val;
4945
Damien Lespiau3d13ef22014-02-07 19:12:47 +00004946 if (INTEL_INFO(dev)->gen != 5)
Chris Wilsonf531dcb2012-09-25 10:16:12 +01004947 return 0;
4948
4949 spin_lock_irq(&mchdev_lock);
4950
4951 val = __i915_gfx_val(dev_priv);
4952
4953 spin_unlock_irq(&mchdev_lock);
4954
4955 return val;
4956}
4957
Daniel Vettereb48eb02012-04-26 23:28:12 +02004958/**
4959 * i915_read_mch_val - return value for IPS use
4960 *
4961 * Calculate and return a value for the IPS driver to use when deciding whether
4962 * we have thermal and power headroom to increase CPU or GPU power budget.
4963 */
4964unsigned long i915_read_mch_val(void)
4965{
4966 struct drm_i915_private *dev_priv;
4967 unsigned long chipset_val, graphics_val, ret = 0;
4968
Daniel Vetter92703882012-08-09 16:46:01 +02004969 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004970 if (!i915_mch_dev)
4971 goto out_unlock;
4972 dev_priv = i915_mch_dev;
4973
Chris Wilsonf531dcb2012-09-25 10:16:12 +01004974 chipset_val = __i915_chipset_val(dev_priv);
4975 graphics_val = __i915_gfx_val(dev_priv);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004976
4977 ret = chipset_val + graphics_val;
4978
4979out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02004980 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004981
4982 return ret;
4983}
4984EXPORT_SYMBOL_GPL(i915_read_mch_val);
4985
4986/**
4987 * i915_gpu_raise - raise GPU frequency limit
4988 *
4989 * Raise the limit; IPS indicates we have thermal headroom.
4990 */
4991bool i915_gpu_raise(void)
4992{
4993 struct drm_i915_private *dev_priv;
4994 bool ret = true;
4995
Daniel Vetter92703882012-08-09 16:46:01 +02004996 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004997 if (!i915_mch_dev) {
4998 ret = false;
4999 goto out_unlock;
5000 }
5001 dev_priv = i915_mch_dev;
5002
Daniel Vetter20e4d402012-08-08 23:35:39 +02005003 if (dev_priv->ips.max_delay > dev_priv->ips.fmax)
5004 dev_priv->ips.max_delay--;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005005
5006out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02005007 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005008
5009 return ret;
5010}
5011EXPORT_SYMBOL_GPL(i915_gpu_raise);
5012
5013/**
5014 * i915_gpu_lower - lower GPU frequency limit
5015 *
5016 * IPS indicates we're close to a thermal limit, so throttle back the GPU
5017 * frequency maximum.
5018 */
5019bool i915_gpu_lower(void)
5020{
5021 struct drm_i915_private *dev_priv;
5022 bool ret = true;
5023
Daniel Vetter92703882012-08-09 16:46:01 +02005024 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005025 if (!i915_mch_dev) {
5026 ret = false;
5027 goto out_unlock;
5028 }
5029 dev_priv = i915_mch_dev;
5030
Daniel Vetter20e4d402012-08-08 23:35:39 +02005031 if (dev_priv->ips.max_delay < dev_priv->ips.min_delay)
5032 dev_priv->ips.max_delay++;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005033
5034out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02005035 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005036
5037 return ret;
5038}
5039EXPORT_SYMBOL_GPL(i915_gpu_lower);
5040
5041/**
5042 * i915_gpu_busy - indicate GPU business to IPS
5043 *
5044 * Tell the IPS driver whether or not the GPU is busy.
5045 */
5046bool i915_gpu_busy(void)
5047{
5048 struct drm_i915_private *dev_priv;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01005049 struct intel_engine_cs *ring;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005050 bool ret = false;
Chris Wilsonf047e392012-07-21 12:31:41 +01005051 int i;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005052
Daniel Vetter92703882012-08-09 16:46:01 +02005053 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005054 if (!i915_mch_dev)
5055 goto out_unlock;
5056 dev_priv = i915_mch_dev;
5057
Chris Wilsonf047e392012-07-21 12:31:41 +01005058 for_each_ring(ring, dev_priv, i)
5059 ret |= !list_empty(&ring->request_list);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005060
5061out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02005062 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005063
5064 return ret;
5065}
5066EXPORT_SYMBOL_GPL(i915_gpu_busy);
5067
5068/**
5069 * i915_gpu_turbo_disable - disable graphics turbo
5070 *
5071 * Disable graphics turbo by resetting the max frequency and setting the
5072 * current frequency to the default.
5073 */
5074bool i915_gpu_turbo_disable(void)
5075{
5076 struct drm_i915_private *dev_priv;
5077 bool ret = true;
5078
Daniel Vetter92703882012-08-09 16:46:01 +02005079 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005080 if (!i915_mch_dev) {
5081 ret = false;
5082 goto out_unlock;
5083 }
5084 dev_priv = i915_mch_dev;
5085
Daniel Vetter20e4d402012-08-08 23:35:39 +02005086 dev_priv->ips.max_delay = dev_priv->ips.fstart;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005087
Daniel Vetter20e4d402012-08-08 23:35:39 +02005088 if (!ironlake_set_drps(dev_priv->dev, dev_priv->ips.fstart))
Daniel Vettereb48eb02012-04-26 23:28:12 +02005089 ret = false;
5090
5091out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02005092 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005093
5094 return ret;
5095}
5096EXPORT_SYMBOL_GPL(i915_gpu_turbo_disable);
5097
5098/**
5099 * Tells the intel_ips driver that the i915 driver is now loaded, if
5100 * IPS got loaded first.
5101 *
5102 * This awkward dance is so that neither module has to depend on the
5103 * other in order for IPS to do the appropriate communication of
5104 * GPU turbo limits to i915.
5105 */
5106static void
5107ips_ping_for_i915_load(void)
5108{
5109 void (*link)(void);
5110
5111 link = symbol_get(ips_link_to_i915_driver);
5112 if (link) {
5113 link();
5114 symbol_put(ips_link_to_i915_driver);
5115 }
5116}
5117
5118void intel_gpu_ips_init(struct drm_i915_private *dev_priv)
5119{
Daniel Vetter02d71952012-08-09 16:44:54 +02005120 /* We only register the i915 ips part with intel-ips once everything is
5121 * set up, to avoid intel-ips sneaking in and reading bogus values. */
Daniel Vetter92703882012-08-09 16:46:01 +02005122 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005123 i915_mch_dev = dev_priv;
Daniel Vetter92703882012-08-09 16:46:01 +02005124 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005125
5126 ips_ping_for_i915_load();
5127}
5128
5129void intel_gpu_ips_teardown(void)
5130{
Daniel Vetter92703882012-08-09 16:46:01 +02005131 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005132 i915_mch_dev = NULL;
Daniel Vetter92703882012-08-09 16:46:01 +02005133 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005134}
Deepak S76c3552f2014-01-30 23:08:16 +05305135
Daniel Vetter8090c6b2012-06-24 16:42:32 +02005136static void intel_init_emon(struct drm_device *dev)
Eugeni Dodonovdde18882012-04-18 15:29:24 -03005137{
5138 struct drm_i915_private *dev_priv = dev->dev_private;
5139 u32 lcfuse;
5140 u8 pxw[16];
5141 int i;
5142
5143 /* Disable to program */
5144 I915_WRITE(ECR, 0);
5145 POSTING_READ(ECR);
5146
5147 /* Program energy weights for various events */
5148 I915_WRITE(SDEW, 0x15040d00);
5149 I915_WRITE(CSIEW0, 0x007f0000);
5150 I915_WRITE(CSIEW1, 0x1e220004);
5151 I915_WRITE(CSIEW2, 0x04000004);
5152
5153 for (i = 0; i < 5; i++)
5154 I915_WRITE(PEW + (i * 4), 0);
5155 for (i = 0; i < 3; i++)
5156 I915_WRITE(DEW + (i * 4), 0);
5157
5158 /* Program P-state weights to account for frequency power adjustment */
5159 for (i = 0; i < 16; i++) {
5160 u32 pxvidfreq = I915_READ(PXVFREQ_BASE + (i * 4));
5161 unsigned long freq = intel_pxfreq(pxvidfreq);
5162 unsigned long vid = (pxvidfreq & PXVFREQ_PX_MASK) >>
5163 PXVFREQ_PX_SHIFT;
5164 unsigned long val;
5165
5166 val = vid * vid;
5167 val *= (freq / 1000);
5168 val *= 255;
5169 val /= (127*127*900);
5170 if (val > 0xff)
5171 DRM_ERROR("bad pxval: %ld\n", val);
5172 pxw[i] = val;
5173 }
5174 /* Render standby states get 0 weight */
5175 pxw[14] = 0;
5176 pxw[15] = 0;
5177
5178 for (i = 0; i < 4; i++) {
5179 u32 val = (pxw[i*4] << 24) | (pxw[(i*4)+1] << 16) |
5180 (pxw[(i*4)+2] << 8) | (pxw[(i*4)+3]);
5181 I915_WRITE(PXW + (i * 4), val);
5182 }
5183
5184 /* Adjust magic regs to magic values (more experimental results) */
5185 I915_WRITE(OGW0, 0);
5186 I915_WRITE(OGW1, 0);
5187 I915_WRITE(EG0, 0x00007f00);
5188 I915_WRITE(EG1, 0x0000000e);
5189 I915_WRITE(EG2, 0x000e0000);
5190 I915_WRITE(EG3, 0x68000300);
5191 I915_WRITE(EG4, 0x42000000);
5192 I915_WRITE(EG5, 0x00140031);
5193 I915_WRITE(EG6, 0);
5194 I915_WRITE(EG7, 0);
5195
5196 for (i = 0; i < 8; i++)
5197 I915_WRITE(PXWL + (i * 4), 0);
5198
5199 /* Enable PMON + select events */
5200 I915_WRITE(ECR, 0x80000019);
5201
5202 lcfuse = I915_READ(LCFUSE02);
5203
Daniel Vetter20e4d402012-08-08 23:35:39 +02005204 dev_priv->ips.corr = (lcfuse & LCFUSE_HIV_MASK);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03005205}
5206
Imre Deakae484342014-03-31 15:10:44 +03005207void intel_init_gt_powersave(struct drm_device *dev)
5208{
Imre Deake6069ca2014-04-18 16:01:02 +03005209 i915.enable_rc6 = sanitize_rc6_option(dev, i915.enable_rc6);
5210
Deepak S38807742014-05-23 21:00:15 +05305211 if (IS_CHERRYVIEW(dev))
5212 cherryview_init_gt_powersave(dev);
5213 else if (IS_VALLEYVIEW(dev))
Imre Deak4e805192014-04-14 20:24:41 +03005214 valleyview_init_gt_powersave(dev);
Imre Deakae484342014-03-31 15:10:44 +03005215}
5216
5217void intel_cleanup_gt_powersave(struct drm_device *dev)
5218{
Deepak S38807742014-05-23 21:00:15 +05305219 if (IS_CHERRYVIEW(dev))
5220 return;
5221 else if (IS_VALLEYVIEW(dev))
Imre Deak4e805192014-04-14 20:24:41 +03005222 valleyview_cleanup_gt_powersave(dev);
Imre Deakae484342014-03-31 15:10:44 +03005223}
5224
Jesse Barnes156c7ca2014-06-12 08:35:45 -07005225/**
5226 * intel_suspend_gt_powersave - suspend PM work and helper threads
5227 * @dev: drm device
5228 *
5229 * We don't want to disable RC6 or other features here, we just want
5230 * to make sure any work we've queued has finished and won't bother
5231 * us while we're suspended.
5232 */
5233void intel_suspend_gt_powersave(struct drm_device *dev)
5234{
5235 struct drm_i915_private *dev_priv = dev->dev_private;
5236
5237 /* Interrupts should be disabled already to avoid re-arming. */
Jesse Barnes9df7575f2014-06-20 09:29:20 -07005238 WARN_ON(intel_irqs_enabled(dev_priv));
Jesse Barnes156c7ca2014-06-12 08:35:45 -07005239
5240 flush_delayed_work(&dev_priv->rps.delayed_resume_work);
5241
5242 cancel_work_sync(&dev_priv->rps.work);
Deepak Sb47adc12014-06-20 20:03:02 +05305243
5244 /* Force GPU to min freq during suspend */
5245 gen6_rps_idle(dev_priv);
Jesse Barnes156c7ca2014-06-12 08:35:45 -07005246}
5247
Daniel Vetter8090c6b2012-06-24 16:42:32 +02005248void intel_disable_gt_powersave(struct drm_device *dev)
5249{
Jesse Barnes1a01ab32012-11-02 11:14:00 -07005250 struct drm_i915_private *dev_priv = dev->dev_private;
5251
Daniel Vetterfd0c0642013-04-24 11:13:35 +02005252 /* Interrupts should be disabled already to avoid re-arming. */
Jesse Barnes9df7575f2014-06-20 09:29:20 -07005253 WARN_ON(intel_irqs_enabled(dev_priv));
Daniel Vetterfd0c0642013-04-24 11:13:35 +02005254
Daniel Vetter930ebb42012-06-29 23:32:16 +02005255 if (IS_IRONLAKE_M(dev)) {
Daniel Vetter8090c6b2012-06-24 16:42:32 +02005256 ironlake_disable_drps(dev);
Daniel Vetter930ebb42012-06-29 23:32:16 +02005257 ironlake_disable_rc6(dev);
Deepak S38807742014-05-23 21:00:15 +05305258 } else if (INTEL_INFO(dev)->gen >= 6) {
Daniel Vetter10d8d362014-06-12 17:48:52 +02005259 intel_suspend_gt_powersave(dev);
Imre Deake4948372014-05-12 18:35:04 +03005260
Jesse Barnes4fc688c2012-11-02 11:14:01 -07005261 mutex_lock(&dev_priv->rps.hw_lock);
Deepak S38807742014-05-23 21:00:15 +05305262 if (IS_CHERRYVIEW(dev))
5263 cherryview_disable_rps(dev);
5264 else if (IS_VALLEYVIEW(dev))
Jesse Barnesd20d4f02013-04-23 10:09:28 -07005265 valleyview_disable_rps(dev);
5266 else
5267 gen6_disable_rps(dev);
Chris Wilsonc0951f02013-10-10 21:58:50 +01005268 dev_priv->rps.enabled = false;
Jesse Barnes4fc688c2012-11-02 11:14:01 -07005269 mutex_unlock(&dev_priv->rps.hw_lock);
Daniel Vetter930ebb42012-06-29 23:32:16 +02005270 }
Daniel Vetter8090c6b2012-06-24 16:42:32 +02005271}
5272
Jesse Barnes1a01ab32012-11-02 11:14:00 -07005273static void intel_gen6_powersave_work(struct work_struct *work)
5274{
5275 struct drm_i915_private *dev_priv =
5276 container_of(work, struct drm_i915_private,
5277 rps.delayed_resume_work.work);
5278 struct drm_device *dev = dev_priv->dev;
5279
Jesse Barnes4fc688c2012-11-02 11:14:01 -07005280 mutex_lock(&dev_priv->rps.hw_lock);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005281
Deepak S38807742014-05-23 21:00:15 +05305282 if (IS_CHERRYVIEW(dev)) {
5283 cherryview_enable_rps(dev);
5284 } else if (IS_VALLEYVIEW(dev)) {
Jesse Barnes0a073b82013-04-17 15:54:58 -07005285 valleyview_enable_rps(dev);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005286 } else if (IS_BROADWELL(dev)) {
5287 gen8_enable_rps(dev);
Imre Deakc2bc2fc2014-04-18 16:16:23 +03005288 __gen6_update_ring_freq(dev);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005289 } else {
5290 gen6_enable_rps(dev);
Imre Deakc2bc2fc2014-04-18 16:16:23 +03005291 __gen6_update_ring_freq(dev);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005292 }
Chris Wilsonc0951f02013-10-10 21:58:50 +01005293 dev_priv->rps.enabled = true;
Jesse Barnes4fc688c2012-11-02 11:14:01 -07005294 mutex_unlock(&dev_priv->rps.hw_lock);
Imre Deakc6df39b2014-04-14 20:24:29 +03005295
5296 intel_runtime_pm_put(dev_priv);
Jesse Barnes1a01ab32012-11-02 11:14:00 -07005297}
5298
Daniel Vetter8090c6b2012-06-24 16:42:32 +02005299void intel_enable_gt_powersave(struct drm_device *dev)
5300{
Jesse Barnes1a01ab32012-11-02 11:14:00 -07005301 struct drm_i915_private *dev_priv = dev->dev_private;
5302
Daniel Vetter8090c6b2012-06-24 16:42:32 +02005303 if (IS_IRONLAKE_M(dev)) {
Imre Deakdc1d0132014-04-14 20:24:28 +03005304 mutex_lock(&dev->struct_mutex);
Daniel Vetter8090c6b2012-06-24 16:42:32 +02005305 ironlake_enable_drps(dev);
5306 ironlake_enable_rc6(dev);
5307 intel_init_emon(dev);
Imre Deakdc1d0132014-04-14 20:24:28 +03005308 mutex_unlock(&dev->struct_mutex);
Deepak S38807742014-05-23 21:00:15 +05305309 } else if (INTEL_INFO(dev)->gen >= 6) {
Jesse Barnes1a01ab32012-11-02 11:14:00 -07005310 /*
5311 * PCU communication is slow and this doesn't need to be
5312 * done at any specific time, so do this out of our fast path
5313 * to make resume and init faster.
Imre Deakc6df39b2014-04-14 20:24:29 +03005314 *
5315 * We depend on the HW RC6 power context save/restore
5316 * mechanism when entering D3 through runtime PM suspend. So
5317 * disable RPM until RPS/RC6 is properly setup. We can only
5318 * get here via the driver load/system resume/runtime resume
5319 * paths, so the _noresume version is enough (and in case of
5320 * runtime resume it's necessary).
Jesse Barnes1a01ab32012-11-02 11:14:00 -07005321 */
Imre Deakc6df39b2014-04-14 20:24:29 +03005322 if (schedule_delayed_work(&dev_priv->rps.delayed_resume_work,
5323 round_jiffies_up_relative(HZ)))
5324 intel_runtime_pm_get_noresume(dev_priv);
Daniel Vetter8090c6b2012-06-24 16:42:32 +02005325 }
5326}
5327
Imre Deakc6df39b2014-04-14 20:24:29 +03005328void intel_reset_gt_powersave(struct drm_device *dev)
5329{
5330 struct drm_i915_private *dev_priv = dev->dev_private;
5331
5332 dev_priv->rps.enabled = false;
5333 intel_enable_gt_powersave(dev);
5334}
5335
Daniel Vetter3107bd42012-10-31 22:52:31 +01005336static void ibx_init_clock_gating(struct drm_device *dev)
5337{
5338 struct drm_i915_private *dev_priv = dev->dev_private;
5339
5340 /*
5341 * On Ibex Peak and Cougar Point, we need to disable clock
5342 * gating for the panel power sequencer or it will fail to
5343 * start up when no ports are active.
5344 */
5345 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE);
5346}
5347
Ville Syrjälä0e088b82013-06-07 10:47:04 +03005348static void g4x_disable_trickle_feed(struct drm_device *dev)
5349{
5350 struct drm_i915_private *dev_priv = dev->dev_private;
5351 int pipe;
5352
Damien Lespiau055e3932014-08-18 13:49:10 +01005353 for_each_pipe(dev_priv, pipe) {
Ville Syrjälä0e088b82013-06-07 10:47:04 +03005354 I915_WRITE(DSPCNTR(pipe),
5355 I915_READ(DSPCNTR(pipe)) |
5356 DISPPLANE_TRICKLE_FEED_DISABLE);
Ville Syrjälä1dba99f2013-10-01 18:02:18 +03005357 intel_flush_primary_plane(dev_priv, pipe);
Ville Syrjälä0e088b82013-06-07 10:47:04 +03005358 }
5359}
5360
Ville Syrjälä017636c2013-12-05 15:51:37 +02005361static void ilk_init_lp_watermarks(struct drm_device *dev)
5362{
5363 struct drm_i915_private *dev_priv = dev->dev_private;
5364
5365 I915_WRITE(WM3_LP_ILK, I915_READ(WM3_LP_ILK) & ~WM1_LP_SR_EN);
5366 I915_WRITE(WM2_LP_ILK, I915_READ(WM2_LP_ILK) & ~WM1_LP_SR_EN);
5367 I915_WRITE(WM1_LP_ILK, I915_READ(WM1_LP_ILK) & ~WM1_LP_SR_EN);
5368
5369 /*
5370 * Don't touch WM1S_LP_EN here.
5371 * Doing so could cause underruns.
5372 */
5373}
5374
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005375static void ironlake_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005376{
5377 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau231e54f2012-10-19 17:55:41 +01005378 uint32_t dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005379
Damien Lespiauf1e8fa52013-06-07 17:41:09 +01005380 /*
5381 * Required for FBC
5382 * WaFbcDisableDpfcClockGating:ilk
5383 */
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01005384 dspclk_gate |= ILK_DPFCRUNIT_CLOCK_GATE_DISABLE |
5385 ILK_DPFCUNIT_CLOCK_GATE_DISABLE |
5386 ILK_DPFDUNIT_CLOCK_GATE_ENABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005387
5388 I915_WRITE(PCH_3DCGDIS0,
5389 MARIUNIT_CLOCK_GATE_DISABLE |
5390 SVSMUNIT_CLOCK_GATE_DISABLE);
5391 I915_WRITE(PCH_3DCGDIS1,
5392 VFMUNIT_CLOCK_GATE_DISABLE);
5393
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005394 /*
5395 * According to the spec the following bits should be set in
5396 * order to enable memory self-refresh
5397 * The bit 22/21 of 0x42004
5398 * The bit 5 of 0x42020
5399 * The bit 15 of 0x45000
5400 */
5401 I915_WRITE(ILK_DISPLAY_CHICKEN2,
5402 (I915_READ(ILK_DISPLAY_CHICKEN2) |
5403 ILK_DPARB_GATE | ILK_VSDPFD_FULL));
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01005404 dspclk_gate |= ILK_DPARBUNIT_CLOCK_GATE_ENABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005405 I915_WRITE(DISP_ARB_CTL,
5406 (I915_READ(DISP_ARB_CTL) |
5407 DISP_FBC_WM_DIS));
Ville Syrjälä017636c2013-12-05 15:51:37 +02005408
5409 ilk_init_lp_watermarks(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005410
5411 /*
5412 * Based on the document from hardware guys the following bits
5413 * should be set unconditionally in order to enable FBC.
5414 * The bit 22 of 0x42000
5415 * The bit 22 of 0x42004
5416 * The bit 7,8,9 of 0x42020.
5417 */
5418 if (IS_IRONLAKE_M(dev)) {
Damien Lespiau4bb35332013-06-14 15:23:24 +01005419 /* WaFbcAsynchFlipDisableFbcQueue:ilk */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005420 I915_WRITE(ILK_DISPLAY_CHICKEN1,
5421 I915_READ(ILK_DISPLAY_CHICKEN1) |
5422 ILK_FBCQ_DIS);
5423 I915_WRITE(ILK_DISPLAY_CHICKEN2,
5424 I915_READ(ILK_DISPLAY_CHICKEN2) |
5425 ILK_DPARB_GATE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005426 }
5427
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01005428 I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
5429
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005430 I915_WRITE(ILK_DISPLAY_CHICKEN2,
5431 I915_READ(ILK_DISPLAY_CHICKEN2) |
5432 ILK_ELPIN_409_SELECT);
5433 I915_WRITE(_3D_CHICKEN2,
5434 _3D_CHICKEN2_WM_READ_PIPELINED << 16 |
5435 _3D_CHICKEN2_WM_READ_PIPELINED);
Daniel Vetter4358a372012-10-18 11:49:51 +02005436
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005437 /* WaDisableRenderCachePipelinedFlush:ilk */
Daniel Vetter4358a372012-10-18 11:49:51 +02005438 I915_WRITE(CACHE_MODE_0,
5439 _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
Daniel Vetter3107bd42012-10-31 22:52:31 +01005440
Akash Goel4e046322014-04-04 17:14:38 +05305441 /* WaDisable_RenderCache_OperationalFlush:ilk */
5442 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
5443
Ville Syrjälä0e088b82013-06-07 10:47:04 +03005444 g4x_disable_trickle_feed(dev);
Ville Syrjäläbdad2b22013-06-07 10:47:03 +03005445
Daniel Vetter3107bd42012-10-31 22:52:31 +01005446 ibx_init_clock_gating(dev);
5447}
5448
5449static void cpt_init_clock_gating(struct drm_device *dev)
5450{
5451 struct drm_i915_private *dev_priv = dev->dev_private;
5452 int pipe;
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03005453 uint32_t val;
Daniel Vetter3107bd42012-10-31 22:52:31 +01005454
5455 /*
5456 * On Ibex Peak and Cougar Point, we need to disable clock
5457 * gating for the panel power sequencer or it will fail to
5458 * start up when no ports are active.
5459 */
Jesse Barnescd664072013-10-02 10:34:19 -07005460 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE |
5461 PCH_DPLUNIT_CLOCK_GATE_DISABLE |
5462 PCH_CPUNIT_CLOCK_GATE_DISABLE);
Daniel Vetter3107bd42012-10-31 22:52:31 +01005463 I915_WRITE(SOUTH_CHICKEN2, I915_READ(SOUTH_CHICKEN2) |
5464 DPLS_EDP_PPS_FIX_DIS);
Takashi Iwai335c07b2012-12-11 11:46:29 +01005465 /* The below fixes the weird display corruption, a few pixels shifted
5466 * downward, on (only) LVDS of some HP laptops with IVY.
5467 */
Damien Lespiau055e3932014-08-18 13:49:10 +01005468 for_each_pipe(dev_priv, pipe) {
Paulo Zanonidc4bd2d2013-04-08 15:48:08 -03005469 val = I915_READ(TRANS_CHICKEN2(pipe));
5470 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
5471 val &= ~TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03005472 if (dev_priv->vbt.fdi_rx_polarity_inverted)
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03005473 val |= TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
Paulo Zanonidc4bd2d2013-04-08 15:48:08 -03005474 val &= ~TRANS_CHICKEN2_FRAME_START_DELAY_MASK;
5475 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_COUNTER;
5476 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_MODESWITCH;
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03005477 I915_WRITE(TRANS_CHICKEN2(pipe), val);
5478 }
Daniel Vetter3107bd42012-10-31 22:52:31 +01005479 /* WADP0ClockGatingDisable */
Damien Lespiau055e3932014-08-18 13:49:10 +01005480 for_each_pipe(dev_priv, pipe) {
Daniel Vetter3107bd42012-10-31 22:52:31 +01005481 I915_WRITE(TRANS_CHICKEN1(pipe),
5482 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
5483 }
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005484}
5485
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01005486static void gen6_check_mch_setup(struct drm_device *dev)
5487{
5488 struct drm_i915_private *dev_priv = dev->dev_private;
5489 uint32_t tmp;
5490
5491 tmp = I915_READ(MCH_SSKPD);
Daniel Vetterdf662a22014-08-04 11:17:25 +02005492 if ((tmp & MCH_SSKPD_WM0_MASK) != MCH_SSKPD_WM0_VAL)
5493 DRM_DEBUG_KMS("Wrong MCH_SSKPD value: 0x%08x This can cause underruns.\n",
5494 tmp);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01005495}
5496
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005497static void gen6_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005498{
5499 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau231e54f2012-10-19 17:55:41 +01005500 uint32_t dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005501
Damien Lespiau231e54f2012-10-19 17:55:41 +01005502 I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005503
5504 I915_WRITE(ILK_DISPLAY_CHICKEN2,
5505 I915_READ(ILK_DISPLAY_CHICKEN2) |
5506 ILK_ELPIN_409_SELECT);
5507
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005508 /* WaDisableHiZPlanesWhenMSAAEnabled:snb */
Daniel Vetter42839082012-12-14 23:38:28 +01005509 I915_WRITE(_3D_CHICKEN,
5510 _MASKED_BIT_ENABLE(_3D_CHICKEN_HIZ_PLANE_DISABLE_MSAA_4X_SNB));
5511
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005512 /* WaSetupGtModeTdRowDispatch:snb */
Daniel Vetter6547fbd2012-12-14 23:38:29 +01005513 if (IS_SNB_GT1(dev))
5514 I915_WRITE(GEN6_GT_MODE,
5515 _MASKED_BIT_ENABLE(GEN6_TD_FOUR_ROW_DISPATCH_DISABLE));
5516
Akash Goel4e046322014-04-04 17:14:38 +05305517 /* WaDisable_RenderCache_OperationalFlush:snb */
5518 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
5519
Ville Syrjälä8d85d272014-02-04 21:59:15 +02005520 /*
5521 * BSpec recoomends 8x4 when MSAA is used,
5522 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02005523 *
5524 * Note that PS/WM thread counts depend on the WIZ hashing
5525 * disable bit, which we don't touch here, but it's good
5526 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjälä8d85d272014-02-04 21:59:15 +02005527 */
5528 I915_WRITE(GEN6_GT_MODE,
5529 GEN6_WIZ_HASHING_MASK | GEN6_WIZ_HASHING_16x4);
5530
Ville Syrjälä017636c2013-12-05 15:51:37 +02005531 ilk_init_lp_watermarks(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005532
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005533 I915_WRITE(CACHE_MODE_0,
Daniel Vetter50743292012-04-26 22:02:54 +02005534 _MASKED_BIT_DISABLE(CM0_STC_EVICT_DISABLE_LRA_SNB));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005535
5536 I915_WRITE(GEN6_UCGCTL1,
5537 I915_READ(GEN6_UCGCTL1) |
5538 GEN6_BLBUNIT_CLOCK_GATE_DISABLE |
5539 GEN6_CSUNIT_CLOCK_GATE_DISABLE);
5540
5541 /* According to the BSpec vol1g, bit 12 (RCPBUNIT) clock
5542 * gating disable must be set. Failure to set it results in
5543 * flickering pixels due to Z write ordering failures after
5544 * some amount of runtime in the Mesa "fire" demo, and Unigine
5545 * Sanctuary and Tropics, and apparently anything else with
5546 * alpha test or pixel discard.
5547 *
5548 * According to the spec, bit 11 (RCCUNIT) must also be set,
5549 * but we didn't debug actual testcases to find it out.
Jesse Barnes0f846f82012-06-14 11:04:47 -07005550 *
Ville Syrjäläef593182014-01-22 21:32:47 +02005551 * WaDisableRCCUnitClockGating:snb
5552 * WaDisableRCPBUnitClockGating:snb
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005553 */
5554 I915_WRITE(GEN6_UCGCTL2,
5555 GEN6_RCPBUNIT_CLOCK_GATE_DISABLE |
5556 GEN6_RCCUNIT_CLOCK_GATE_DISABLE);
5557
Ville Syrjälä5eb146d2014-02-04 21:59:16 +02005558 /* WaStripsFansDisableFastClipPerformanceFix:snb */
Ville Syrjälä743b57d2014-02-04 21:59:17 +02005559 I915_WRITE(_3D_CHICKEN3,
5560 _MASKED_BIT_ENABLE(_3D_CHICKEN3_SF_DISABLE_FASTCLIP_CULL));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005561
5562 /*
Ville Syrjäläe927ecd2014-02-04 21:59:18 +02005563 * Bspec says:
5564 * "This bit must be set if 3DSTATE_CLIP clip mode is set to normal and
5565 * 3DSTATE_SF number of SF output attributes is more than 16."
5566 */
5567 I915_WRITE(_3D_CHICKEN3,
5568 _MASKED_BIT_ENABLE(_3D_CHICKEN3_SF_DISABLE_PIPELINED_ATTR_FETCH));
5569
5570 /*
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005571 * According to the spec the following bits should be
5572 * set in order to enable memory self-refresh and fbc:
5573 * The bit21 and bit22 of 0x42000
5574 * The bit21 and bit22 of 0x42004
5575 * The bit5 and bit7 of 0x42020
5576 * The bit14 of 0x70180
5577 * The bit14 of 0x71180
Damien Lespiau4bb35332013-06-14 15:23:24 +01005578 *
5579 * WaFbcAsynchFlipDisableFbcQueue:snb
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005580 */
5581 I915_WRITE(ILK_DISPLAY_CHICKEN1,
5582 I915_READ(ILK_DISPLAY_CHICKEN1) |
5583 ILK_FBCQ_DIS | ILK_PABSTRETCH_DIS);
5584 I915_WRITE(ILK_DISPLAY_CHICKEN2,
5585 I915_READ(ILK_DISPLAY_CHICKEN2) |
5586 ILK_DPARB_GATE | ILK_VSDPFD_FULL);
Damien Lespiau231e54f2012-10-19 17:55:41 +01005587 I915_WRITE(ILK_DSPCLK_GATE_D,
5588 I915_READ(ILK_DSPCLK_GATE_D) |
5589 ILK_DPARBUNIT_CLOCK_GATE_ENABLE |
5590 ILK_DPFDUNIT_CLOCK_GATE_ENABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005591
Ville Syrjälä0e088b82013-06-07 10:47:04 +03005592 g4x_disable_trickle_feed(dev);
Ben Widawskyf8f2ac92012-10-03 19:34:24 -07005593
Daniel Vetter3107bd42012-10-31 22:52:31 +01005594 cpt_init_clock_gating(dev);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01005595
5596 gen6_check_mch_setup(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005597}
5598
5599static void gen7_setup_fixed_func_scheduler(struct drm_i915_private *dev_priv)
5600{
5601 uint32_t reg = I915_READ(GEN7_FF_THREAD_MODE);
5602
Ville Syrjälä3aad9052014-01-22 21:32:59 +02005603 /*
Ville Syrjälä46680e02014-01-22 21:33:01 +02005604 * WaVSThreadDispatchOverride:ivb,vlv
Ville Syrjälä3aad9052014-01-22 21:32:59 +02005605 *
5606 * This actually overrides the dispatch
5607 * mode for all thread types.
5608 */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005609 reg &= ~GEN7_FF_SCHED_MASK;
5610 reg |= GEN7_FF_TS_SCHED_HW;
5611 reg |= GEN7_FF_VS_SCHED_HW;
5612 reg |= GEN7_FF_DS_SCHED_HW;
5613
5614 I915_WRITE(GEN7_FF_THREAD_MODE, reg);
5615}
5616
Paulo Zanoni17a303e2012-11-20 15:12:07 -02005617static void lpt_init_clock_gating(struct drm_device *dev)
5618{
5619 struct drm_i915_private *dev_priv = dev->dev_private;
5620
5621 /*
5622 * TODO: this bit should only be enabled when really needed, then
5623 * disabled when not needed anymore in order to save power.
5624 */
5625 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE)
5626 I915_WRITE(SOUTH_DSPCLK_GATE_D,
5627 I915_READ(SOUTH_DSPCLK_GATE_D) |
5628 PCH_LP_PARTITION_LEVEL_DISABLE);
Paulo Zanoni0a790cd2013-04-17 18:15:49 -03005629
5630 /* WADPOClockGatingDisable:hsw */
5631 I915_WRITE(_TRANSA_CHICKEN1,
5632 I915_READ(_TRANSA_CHICKEN1) |
5633 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
Paulo Zanoni17a303e2012-11-20 15:12:07 -02005634}
5635
Imre Deak7d708ee2013-04-17 14:04:50 +03005636static void lpt_suspend_hw(struct drm_device *dev)
5637{
5638 struct drm_i915_private *dev_priv = dev->dev_private;
5639
5640 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
5641 uint32_t val = I915_READ(SOUTH_DSPCLK_GATE_D);
5642
5643 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
5644 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
5645 }
5646}
5647
Paulo Zanoni47c2bd92014-08-21 17:09:37 -03005648static void broadwell_init_clock_gating(struct drm_device *dev)
Ben Widawsky1020a5c2013-11-02 21:07:06 -07005649{
5650 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau07d27e22014-03-03 17:31:46 +00005651 enum pipe pipe;
Ben Widawsky1020a5c2013-11-02 21:07:06 -07005652
5653 I915_WRITE(WM3_LP_ILK, 0);
5654 I915_WRITE(WM2_LP_ILK, 0);
5655 I915_WRITE(WM1_LP_ILK, 0);
Ben Widawsky50ed5fb2013-11-02 21:07:40 -07005656
5657 /* FIXME(BDW): Check all the w/a, some might only apply to
5658 * pre-production hw. */
5659
Kenneth Graunkec8966e12014-02-26 23:59:30 -08005660
Ben Widawsky4afe8d32013-11-02 21:07:55 -07005661 I915_WRITE(GAMTARBMODE, _MASKED_BIT_ENABLE(ARB_MODE_BWGTLB_DISABLE));
5662
Ben Widawsky7f88da02013-11-02 21:07:58 -07005663 I915_WRITE(_3D_CHICKEN3,
Michel Thierryb3f9ad92014-07-07 12:40:17 +01005664 _MASKED_BIT_ENABLE(_3D_CHICKEN_SDE_LIMIT_FIFO_POLY_DEPTH(2)));
Ben Widawsky7f88da02013-11-02 21:07:58 -07005665
Ben Widawsky242a4012014-04-18 18:04:29 -03005666
Ben Widawskyab57fff2013-12-12 15:28:04 -08005667 /* WaSwitchSolVfFArbitrationPriority:bdw */
Ben Widawsky50ed5fb2013-11-02 21:07:40 -07005668 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07005669
Ben Widawskyab57fff2013-12-12 15:28:04 -08005670 /* WaPsrDPAMaskVBlankInSRD:bdw */
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07005671 I915_WRITE(CHICKEN_PAR1_1,
5672 I915_READ(CHICKEN_PAR1_1) | DPA_MASK_VBLANK_SRD);
5673
Ben Widawskyab57fff2013-12-12 15:28:04 -08005674 /* WaPsrDPRSUnmaskVBlankInSRD:bdw */
Damien Lespiau055e3932014-08-18 13:49:10 +01005675 for_each_pipe(dev_priv, pipe) {
Damien Lespiau07d27e22014-03-03 17:31:46 +00005676 I915_WRITE(CHICKEN_PIPESL_1(pipe),
Ville Syrjäläc7c65622014-03-05 13:05:45 +02005677 I915_READ(CHICKEN_PIPESL_1(pipe)) |
Ville Syrjälä8f670bb2014-03-05 13:05:47 +02005678 BDW_DPRS_MASK_VBLANK_SRD);
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07005679 }
Ben Widawsky63801f22013-12-12 17:26:03 -08005680
Ben Widawskyab57fff2013-12-12 15:28:04 -08005681 /* WaVSRefCountFullforceMissDisable:bdw */
5682 /* WaDSRefCountFullforceMissDisable:bdw */
5683 I915_WRITE(GEN7_FF_THREAD_MODE,
5684 I915_READ(GEN7_FF_THREAD_MODE) &
5685 ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
Ville Syrjälä36075a42014-02-04 21:59:21 +02005686
Ville Syrjälä295e8bb2014-02-27 21:59:01 +02005687 I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
5688 _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
Ville Syrjälä4f1ca9e2014-02-27 21:59:02 +02005689
5690 /* WaDisableSDEUnitClockGating:bdw */
5691 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
5692 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Damien Lespiau5d708682014-03-26 18:41:51 +00005693
Paulo Zanoni89d6b2b2014-08-21 17:09:36 -03005694 lpt_init_clock_gating(dev);
Ben Widawsky1020a5c2013-11-02 21:07:06 -07005695}
5696
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03005697static void haswell_init_clock_gating(struct drm_device *dev)
5698{
5699 struct drm_i915_private *dev_priv = dev->dev_private;
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03005700
Ville Syrjälä017636c2013-12-05 15:51:37 +02005701 ilk_init_lp_watermarks(dev);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03005702
Francisco Jerezf3fc4882013-10-02 15:53:16 -07005703 /* L3 caching of data atomics doesn't work -- disable it. */
5704 I915_WRITE(HSW_SCRATCH1, HSW_SCRATCH1_L3_DATA_ATOMICS_DISABLE);
5705 I915_WRITE(HSW_ROW_CHICKEN3,
5706 _MASKED_BIT_ENABLE(HSW_ROW_CHICKEN3_L3_GLOBAL_ATOMICS_DISABLE));
5707
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005708 /* This is required by WaCatErrorRejectionIssue:hsw */
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03005709 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
5710 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
5711 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
5712
Ville Syrjäläe36ea7f2014-01-22 21:33:00 +02005713 /* WaVSRefCountFullforceMissDisable:hsw */
5714 I915_WRITE(GEN7_FF_THREAD_MODE,
5715 I915_READ(GEN7_FF_THREAD_MODE) & ~GEN7_FF_VS_REF_CNT_FFME);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03005716
Akash Goel4e046322014-04-04 17:14:38 +05305717 /* WaDisable_RenderCache_OperationalFlush:hsw */
5718 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
5719
Chia-I Wufe27c602014-01-28 13:29:33 +08005720 /* enable HiZ Raw Stall Optimization */
5721 I915_WRITE(CACHE_MODE_0_GEN7,
5722 _MASKED_BIT_DISABLE(HIZ_RAW_STALL_OPT_DISABLE));
5723
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005724 /* WaDisable4x2SubspanOptimization:hsw */
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03005725 I915_WRITE(CACHE_MODE_1,
5726 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Eugeni Dodonov1544d9d2012-07-02 11:51:10 -03005727
Ville Syrjäläa12c4962014-02-04 21:59:20 +02005728 /*
5729 * BSpec recommends 8x4 when MSAA is used,
5730 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02005731 *
5732 * Note that PS/WM thread counts depend on the WIZ hashing
5733 * disable bit, which we don't touch here, but it's good
5734 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjäläa12c4962014-02-04 21:59:20 +02005735 */
5736 I915_WRITE(GEN7_GT_MODE,
5737 GEN6_WIZ_HASHING_MASK | GEN6_WIZ_HASHING_16x4);
5738
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005739 /* WaSwitchSolVfFArbitrationPriority:hsw */
Ben Widawskye3dff582013-03-20 14:49:14 -07005740 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
5741
Paulo Zanoni90a88642013-05-03 17:23:45 -03005742 /* WaRsPkgCStateDisplayPMReq:hsw */
5743 I915_WRITE(CHICKEN_PAR1_1,
5744 I915_READ(CHICKEN_PAR1_1) | FORCE_ARB_IDLE_PLANES);
Eugeni Dodonov1544d9d2012-07-02 11:51:10 -03005745
Paulo Zanoni17a303e2012-11-20 15:12:07 -02005746 lpt_init_clock_gating(dev);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03005747}
5748
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005749static void ivybridge_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005750{
5751 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawsky20848222012-05-04 18:58:59 -07005752 uint32_t snpcr;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005753
Ville Syrjälä017636c2013-12-05 15:51:37 +02005754 ilk_init_lp_watermarks(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005755
Damien Lespiau231e54f2012-10-19 17:55:41 +01005756 I915_WRITE(ILK_DSPCLK_GATE_D, ILK_VRHUNIT_CLOCK_GATE_DISABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005757
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005758 /* WaDisableEarlyCull:ivb */
Jesse Barnes87f80202012-10-02 17:43:41 -05005759 I915_WRITE(_3D_CHICKEN3,
5760 _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
5761
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005762 /* WaDisableBackToBackFlipFix:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005763 I915_WRITE(IVB_CHICKEN3,
5764 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
5765 CHICKEN3_DGMG_DONE_FIX_DISABLE);
5766
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005767 /* WaDisablePSDDualDispatchEnable:ivb */
Jesse Barnes12f33822012-10-25 12:15:45 -07005768 if (IS_IVB_GT1(dev))
5769 I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
5770 _MASKED_BIT_ENABLE(GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
Jesse Barnes12f33822012-10-25 12:15:45 -07005771
Akash Goel4e046322014-04-04 17:14:38 +05305772 /* WaDisable_RenderCache_OperationalFlush:ivb */
5773 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
5774
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005775 /* Apply the WaDisableRHWOOptimizationForRenderHang:ivb workaround. */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005776 I915_WRITE(GEN7_COMMON_SLICE_CHICKEN1,
5777 GEN7_CSC1_RHWO_OPT_DISABLE_IN_RCC);
5778
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005779 /* WaApplyL3ControlAndL3ChickenMode:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005780 I915_WRITE(GEN7_L3CNTLREG1,
5781 GEN7_WA_FOR_GEN7_L3_CONTROL);
5782 I915_WRITE(GEN7_L3_CHICKEN_MODE_REGISTER,
Jesse Barnes8ab43972012-10-25 12:15:42 -07005783 GEN7_WA_L3_CHICKEN_MODE);
5784 if (IS_IVB_GT1(dev))
5785 I915_WRITE(GEN7_ROW_CHICKEN2,
5786 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Ville Syrjälä412236c2014-01-22 21:32:44 +02005787 else {
5788 /* must write both registers */
5789 I915_WRITE(GEN7_ROW_CHICKEN2,
5790 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Jesse Barnes8ab43972012-10-25 12:15:42 -07005791 I915_WRITE(GEN7_ROW_CHICKEN2_GT2,
5792 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Ville Syrjälä412236c2014-01-22 21:32:44 +02005793 }
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005794
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005795 /* WaForceL3Serialization:ivb */
Jesse Barnes61939d92012-10-02 17:43:38 -05005796 I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
5797 ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
5798
Ville Syrjälä1b80a19a2014-01-22 21:32:53 +02005799 /*
Jesse Barnes0f846f82012-06-14 11:04:47 -07005800 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005801 * This implements the WaDisableRCZUnitClockGating:ivb workaround.
Jesse Barnes0f846f82012-06-14 11:04:47 -07005802 */
5803 I915_WRITE(GEN6_UCGCTL2,
Ville Syrjälä28acf3b2014-01-22 21:32:48 +02005804 GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
Jesse Barnes0f846f82012-06-14 11:04:47 -07005805
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005806 /* This is required by WaCatErrorRejectionIssue:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005807 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
5808 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
5809 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
5810
Ville Syrjälä0e088b82013-06-07 10:47:04 +03005811 g4x_disable_trickle_feed(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005812
5813 gen7_setup_fixed_func_scheduler(dev_priv);
Daniel Vetter97e19302012-04-24 16:00:21 +02005814
Chris Wilson22721342014-03-04 09:41:43 +00005815 if (0) { /* causes HiZ corruption on ivb:gt1 */
5816 /* enable HiZ Raw Stall Optimization */
5817 I915_WRITE(CACHE_MODE_0_GEN7,
5818 _MASKED_BIT_DISABLE(HIZ_RAW_STALL_OPT_DISABLE));
5819 }
Chia-I Wu116f2b62014-01-28 13:29:34 +08005820
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005821 /* WaDisable4x2SubspanOptimization:ivb */
Daniel Vetter97e19302012-04-24 16:00:21 +02005822 I915_WRITE(CACHE_MODE_1,
5823 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Ben Widawsky20848222012-05-04 18:58:59 -07005824
Ville Syrjäläa607c1a2014-02-04 21:59:19 +02005825 /*
5826 * BSpec recommends 8x4 when MSAA is used,
5827 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02005828 *
5829 * Note that PS/WM thread counts depend on the WIZ hashing
5830 * disable bit, which we don't touch here, but it's good
5831 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjäläa607c1a2014-02-04 21:59:19 +02005832 */
5833 I915_WRITE(GEN7_GT_MODE,
5834 GEN6_WIZ_HASHING_MASK | GEN6_WIZ_HASHING_16x4);
5835
Ben Widawsky20848222012-05-04 18:58:59 -07005836 snpcr = I915_READ(GEN6_MBCUNIT_SNPCR);
5837 snpcr &= ~GEN6_MBC_SNPCR_MASK;
5838 snpcr |= GEN6_MBC_SNPCR_MED;
5839 I915_WRITE(GEN6_MBCUNIT_SNPCR, snpcr);
Daniel Vetter3107bd42012-10-31 22:52:31 +01005840
Ben Widawskyab5c6082013-04-05 13:12:41 -07005841 if (!HAS_PCH_NOP(dev))
5842 cpt_init_clock_gating(dev);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01005843
5844 gen6_check_mch_setup(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005845}
5846
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005847static void valleyview_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005848{
5849 struct drm_i915_private *dev_priv = dev->dev_private;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005850
Ville Syrjäläd7fe0cc2013-05-21 18:01:50 +03005851 I915_WRITE(DSPCLK_GATE_D, VRHUNIT_CLOCK_GATE_DISABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005852
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005853 /* WaDisableEarlyCull:vlv */
Jesse Barnes87f80202012-10-02 17:43:41 -05005854 I915_WRITE(_3D_CHICKEN3,
5855 _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
5856
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005857 /* WaDisableBackToBackFlipFix:vlv */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005858 I915_WRITE(IVB_CHICKEN3,
5859 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
5860 CHICKEN3_DGMG_DONE_FIX_DISABLE);
5861
Ville Syrjäläfad7d362014-01-22 21:32:39 +02005862 /* WaPsdDispatchEnable:vlv */
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005863 /* WaDisablePSDDualDispatchEnable:vlv */
Jesse Barnes12f33822012-10-25 12:15:45 -07005864 I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
Jesse Barnesd3bc0302013-03-08 10:45:51 -08005865 _MASKED_BIT_ENABLE(GEN7_MAX_PS_THREAD_DEP |
5866 GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
Jesse Barnes12f33822012-10-25 12:15:45 -07005867
Akash Goel4e046322014-04-04 17:14:38 +05305868 /* WaDisable_RenderCache_OperationalFlush:vlv */
5869 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
5870
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005871 /* WaForceL3Serialization:vlv */
Jesse Barnes61939d92012-10-02 17:43:38 -05005872 I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
5873 ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
5874
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005875 /* WaDisableDopClockGating:vlv */
Jesse Barnes8ab43972012-10-25 12:15:42 -07005876 I915_WRITE(GEN7_ROW_CHICKEN2,
5877 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
5878
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005879 /* This is required by WaCatErrorRejectionIssue:vlv */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005880 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
5881 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
5882 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
5883
Ville Syrjälä46680e02014-01-22 21:33:01 +02005884 gen7_setup_fixed_func_scheduler(dev_priv);
5885
Ville Syrjälä3c0edae2014-01-22 21:32:56 +02005886 /*
Jesse Barnes0f846f82012-06-14 11:04:47 -07005887 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005888 * This implements the WaDisableRCZUnitClockGating:vlv workaround.
Jesse Barnes0f846f82012-06-14 11:04:47 -07005889 */
5890 I915_WRITE(GEN6_UCGCTL2,
Ville Syrjälä3c0edae2014-01-22 21:32:56 +02005891 GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
Jesse Barnes0f846f82012-06-14 11:04:47 -07005892
Akash Goelc98f5062014-03-24 23:00:07 +05305893 /* WaDisableL3Bank2xClockGate:vlv
5894 * Disabling L3 clock gating- MMIO 940c[25] = 1
5895 * Set bit 25, to disable L3_BANK_2x_CLK_GATING */
5896 I915_WRITE(GEN7_UCGCTL4,
5897 I915_READ(GEN7_UCGCTL4) | GEN7_L3BANK2X_CLOCK_GATE_DISABLE);
Jesse Barnese3f33d42012-06-14 11:04:50 -07005898
Ville Syrjäläe0d8d592013-06-12 22:11:18 +03005899 I915_WRITE(MI_ARB_VLV, MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005900
Ville Syrjäläafd58e72014-01-22 21:33:03 +02005901 /*
5902 * BSpec says this must be set, even though
5903 * WaDisable4x2SubspanOptimization isn't listed for VLV.
5904 */
Daniel Vetter6b26c862012-04-24 14:04:12 +02005905 I915_WRITE(CACHE_MODE_1,
5906 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Jesse Barnes79831172012-06-20 10:53:12 -07005907
5908 /*
Ville Syrjälä031994e2014-01-22 21:32:46 +02005909 * WaIncreaseL3CreditsForVLVB0:vlv
5910 * This is the hardware default actually.
5911 */
5912 I915_WRITE(GEN7_L3SQCREG1, VLV_B0_WA_L3SQCREG1_VALUE);
5913
5914 /*
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005915 * WaDisableVLVClockGating_VBIIssue:vlv
Jesse Barnes2d809572012-10-25 12:15:44 -07005916 * Disable clock gating on th GCFG unit to prevent a delay
5917 * in the reporting of vblank events.
5918 */
Ville Syrjälä7a0d1ee2014-01-22 21:33:04 +02005919 I915_WRITE(VLV_GUNIT_CLOCK_GATE, GCFG_DIS);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005920}
5921
Ville Syrjäläa4565da2014-04-09 13:28:10 +03005922static void cherryview_init_clock_gating(struct drm_device *dev)
5923{
5924 struct drm_i915_private *dev_priv = dev->dev_private;
5925
5926 I915_WRITE(DSPCLK_GATE_D, VRHUNIT_CLOCK_GATE_DISABLE);
5927
5928 I915_WRITE(MI_ARB_VLV, MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE);
Ville Syrjälädd811e72014-04-09 13:28:33 +03005929
Ville Syrjälä232ce332014-04-09 13:28:35 +03005930 /* WaVSRefCountFullforceMissDisable:chv */
5931 /* WaDSRefCountFullforceMissDisable:chv */
5932 I915_WRITE(GEN7_FF_THREAD_MODE,
5933 I915_READ(GEN7_FF_THREAD_MODE) &
5934 ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
Ville Syrjäläacea6f92014-04-09 13:28:36 +03005935
5936 /* WaDisableSemaphoreAndSyncFlipWait:chv */
5937 I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
5938 _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
Ville Syrjälä08466972014-04-09 13:28:37 +03005939
5940 /* WaDisableCSUnitClockGating:chv */
5941 I915_WRITE(GEN6_UCGCTL1, I915_READ(GEN6_UCGCTL1) |
5942 GEN6_CSUNIT_CLOCK_GATE_DISABLE);
Ville Syrjäläc6317802014-04-09 13:28:38 +03005943
5944 /* WaDisableSDEUnitClockGating:chv */
5945 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
5946 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Rafael Barbalhoe0d34ce2014-04-09 13:28:40 +03005947
Ville Syrjäläe4443e42014-04-09 13:28:41 +03005948 /* WaDisableGunitClockGating:chv (pre-production hw) */
5949 I915_WRITE(VLV_GUNIT_CLOCK_GATE, I915_READ(VLV_GUNIT_CLOCK_GATE) |
5950 GINT_DIS);
5951
5952 /* WaDisableFfDopClockGating:chv (pre-production hw) */
5953 I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
5954 _MASKED_BIT_ENABLE(GEN8_FF_DOP_CLOCK_GATE_DISABLE));
5955
5956 /* WaDisableDopClockGating:chv (pre-production hw) */
Ville Syrjäläe4443e42014-04-09 13:28:41 +03005957 I915_WRITE(GEN6_UCGCTL1, I915_READ(GEN6_UCGCTL1) |
5958 GEN6_EU_TCUNIT_CLOCK_GATE_DISABLE);
Ville Syrjäläa4565da2014-04-09 13:28:10 +03005959}
5960
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005961static void g4x_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005962{
5963 struct drm_i915_private *dev_priv = dev->dev_private;
5964 uint32_t dspclk_gate;
5965
5966 I915_WRITE(RENCLK_GATE_D1, 0);
5967 I915_WRITE(RENCLK_GATE_D2, VF_UNIT_CLOCK_GATE_DISABLE |
5968 GS_UNIT_CLOCK_GATE_DISABLE |
5969 CL_UNIT_CLOCK_GATE_DISABLE);
5970 I915_WRITE(RAMCLK_GATE_D, 0);
5971 dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE |
5972 OVRUNIT_CLOCK_GATE_DISABLE |
5973 OVCUNIT_CLOCK_GATE_DISABLE;
5974 if (IS_GM45(dev))
5975 dspclk_gate |= DSSUNIT_CLOCK_GATE_DISABLE;
5976 I915_WRITE(DSPCLK_GATE_D, dspclk_gate);
Daniel Vetter4358a372012-10-18 11:49:51 +02005977
5978 /* WaDisableRenderCachePipelinedFlush */
5979 I915_WRITE(CACHE_MODE_0,
5980 _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
Ville Syrjäläde1aa622013-06-07 10:47:01 +03005981
Akash Goel4e046322014-04-04 17:14:38 +05305982 /* WaDisable_RenderCache_OperationalFlush:g4x */
5983 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
5984
Ville Syrjälä0e088b82013-06-07 10:47:04 +03005985 g4x_disable_trickle_feed(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005986}
5987
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005988static void crestline_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005989{
5990 struct drm_i915_private *dev_priv = dev->dev_private;
5991
5992 I915_WRITE(RENCLK_GATE_D1, I965_RCC_CLOCK_GATE_DISABLE);
5993 I915_WRITE(RENCLK_GATE_D2, 0);
5994 I915_WRITE(DSPCLK_GATE_D, 0);
5995 I915_WRITE(RAMCLK_GATE_D, 0);
5996 I915_WRITE16(DEUC, 0);
Ville Syrjälä20f94962013-06-07 10:47:02 +03005997 I915_WRITE(MI_ARB_STATE,
5998 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Akash Goel4e046322014-04-04 17:14:38 +05305999
6000 /* WaDisable_RenderCache_OperationalFlush:gen4 */
6001 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006002}
6003
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006004static void broadwater_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006005{
6006 struct drm_i915_private *dev_priv = dev->dev_private;
6007
6008 I915_WRITE(RENCLK_GATE_D1, I965_RCZ_CLOCK_GATE_DISABLE |
6009 I965_RCC_CLOCK_GATE_DISABLE |
6010 I965_RCPB_CLOCK_GATE_DISABLE |
6011 I965_ISC_CLOCK_GATE_DISABLE |
6012 I965_FBC_CLOCK_GATE_DISABLE);
6013 I915_WRITE(RENCLK_GATE_D2, 0);
Ville Syrjälä20f94962013-06-07 10:47:02 +03006014 I915_WRITE(MI_ARB_STATE,
6015 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Akash Goel4e046322014-04-04 17:14:38 +05306016
6017 /* WaDisable_RenderCache_OperationalFlush:gen4 */
6018 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006019}
6020
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006021static void gen3_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006022{
6023 struct drm_i915_private *dev_priv = dev->dev_private;
6024 u32 dstate = I915_READ(D_STATE);
6025
6026 dstate |= DSTATE_PLL_D3_OFF | DSTATE_GFX_CLOCK_GATING |
6027 DSTATE_DOT_CLOCK_GATING;
6028 I915_WRITE(D_STATE, dstate);
Chris Wilson13a86b82012-04-24 14:51:43 +01006029
6030 if (IS_PINEVIEW(dev))
6031 I915_WRITE(ECOSKPD, _MASKED_BIT_ENABLE(ECO_GATING_CX_ONLY));
Daniel Vetter974a3b02012-09-09 11:54:16 +02006032
6033 /* IIR "flip pending" means done if this bit is set */
6034 I915_WRITE(ECOSKPD, _MASKED_BIT_DISABLE(ECO_FLIP_DONE));
Ville Syrjälä12fabbcb92014-02-25 15:13:38 +02006035
6036 /* interrupts should cause a wake up from C3 */
Ville Syrjälä32992542014-02-25 15:13:39 +02006037 I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_AGPBUSY_INT_EN));
Ville Syrjälädbb42742014-02-25 15:13:41 +02006038
6039 /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
6040 I915_WRITE(MI_ARB_STATE, _MASKED_BIT_ENABLE(MI_ARB_C3_LP_WRITE_ENABLE));
Ville Syrjälä10383922014-08-15 01:21:54 +03006041
6042 I915_WRITE(MI_ARB_STATE,
6043 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006044}
6045
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006046static void i85x_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006047{
6048 struct drm_i915_private *dev_priv = dev->dev_private;
6049
6050 I915_WRITE(RENCLK_GATE_D1, SV_CLOCK_GATE_DISABLE);
Ville Syrjälä54e472a2014-02-25 15:13:40 +02006051
6052 /* interrupts should cause a wake up from C3 */
6053 I915_WRITE(MI_STATE, _MASKED_BIT_ENABLE(MI_AGPBUSY_INT_EN) |
6054 _MASKED_BIT_DISABLE(MI_AGPBUSY_830_MODE));
Ville Syrjälä10383922014-08-15 01:21:54 +03006055
6056 I915_WRITE(MEM_MODE,
6057 _MASKED_BIT_ENABLE(MEM_DISPLAY_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006058}
6059
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006060static void i830_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006061{
6062 struct drm_i915_private *dev_priv = dev->dev_private;
6063
6064 I915_WRITE(DSPCLK_GATE_D, OVRUNIT_CLOCK_GATE_DISABLE);
Ville Syrjälä10383922014-08-15 01:21:54 +03006065
6066 I915_WRITE(MEM_MODE,
6067 _MASKED_BIT_ENABLE(MEM_DISPLAY_A_TRICKLE_FEED_DISABLE) |
6068 _MASKED_BIT_ENABLE(MEM_DISPLAY_B_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006069}
6070
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006071void intel_init_clock_gating(struct drm_device *dev)
6072{
6073 struct drm_i915_private *dev_priv = dev->dev_private;
6074
6075 dev_priv->display.init_clock_gating(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006076}
6077
Imre Deak7d708ee2013-04-17 14:04:50 +03006078void intel_suspend_hw(struct drm_device *dev)
6079{
6080 if (HAS_PCH_LPT(dev))
6081 lpt_suspend_hw(dev);
6082}
6083
Paulo Zanonid2dee862014-09-19 16:04:54 -03006084static void intel_init_fbc(struct drm_i915_private *dev_priv)
6085{
Paulo Zanoni9adccc62014-09-19 16:04:55 -03006086 if (!HAS_FBC(dev_priv)) {
6087 dev_priv->fbc.enabled = false;
Paulo Zanonid2dee862014-09-19 16:04:54 -03006088 return;
Paulo Zanoni9adccc62014-09-19 16:04:55 -03006089 }
Paulo Zanonid2dee862014-09-19 16:04:54 -03006090
6091 if (INTEL_INFO(dev_priv)->gen >= 7) {
6092 dev_priv->display.fbc_enabled = ironlake_fbc_enabled;
6093 dev_priv->display.enable_fbc = gen7_enable_fbc;
6094 dev_priv->display.disable_fbc = ironlake_disable_fbc;
6095 } else if (INTEL_INFO(dev_priv)->gen >= 5) {
6096 dev_priv->display.fbc_enabled = ironlake_fbc_enabled;
6097 dev_priv->display.enable_fbc = ironlake_enable_fbc;
6098 dev_priv->display.disable_fbc = ironlake_disable_fbc;
6099 } else if (IS_GM45(dev_priv)) {
6100 dev_priv->display.fbc_enabled = g4x_fbc_enabled;
6101 dev_priv->display.enable_fbc = g4x_enable_fbc;
6102 dev_priv->display.disable_fbc = g4x_disable_fbc;
6103 } else {
6104 dev_priv->display.fbc_enabled = i8xx_fbc_enabled;
6105 dev_priv->display.enable_fbc = i8xx_enable_fbc;
6106 dev_priv->display.disable_fbc = i8xx_disable_fbc;
6107
6108 /* This value was pulled out of someone's hat */
6109 I915_WRITE(FBC_CONTROL, 500 << FBC_CTL_INTERVAL_SHIFT);
6110 }
Paulo Zanoni9adccc62014-09-19 16:04:55 -03006111
6112 dev_priv->fbc.enabled = dev_priv->display.fbc_enabled(dev_priv->dev);
Paulo Zanonid2dee862014-09-19 16:04:54 -03006113}
6114
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006115/* Set up chip specific power management-related functions */
6116void intel_init_pm(struct drm_device *dev)
6117{
6118 struct drm_i915_private *dev_priv = dev->dev_private;
6119
Paulo Zanonid2dee862014-09-19 16:04:54 -03006120 intel_init_fbc(dev_priv);
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006121
Daniel Vetterc921aba2012-04-26 23:28:17 +02006122 /* For cxsr */
6123 if (IS_PINEVIEW(dev))
6124 i915_pineview_get_mem_freq(dev);
6125 else if (IS_GEN5(dev))
6126 i915_ironlake_get_mem_freq(dev);
6127
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006128 /* For FIFO watermark updates */
Damien Lespiauc83155a2014-03-28 00:18:35 +05306129 if (IS_GEN9(dev)) {
6130 dev_priv->display.init_clock_gating = gen9_init_clock_gating;
6131 } else if (HAS_PCH_SPLIT(dev)) {
Damien Lespiaufa50ad62014-03-17 18:01:16 +00006132 ilk_setup_wm_latency(dev);
Ville Syrjälä53615a52013-08-01 16:18:50 +03006133
Ville Syrjäläbd602542014-01-07 16:14:10 +02006134 if ((IS_GEN5(dev) && dev_priv->wm.pri_latency[1] &&
6135 dev_priv->wm.spr_latency[1] && dev_priv->wm.cur_latency[1]) ||
6136 (!IS_GEN5(dev) && dev_priv->wm.pri_latency[0] &&
6137 dev_priv->wm.spr_latency[0] && dev_priv->wm.cur_latency[0])) {
6138 dev_priv->display.update_wm = ilk_update_wm;
6139 dev_priv->display.update_sprite_wm = ilk_update_sprite_wm;
6140 } else {
6141 DRM_DEBUG_KMS("Failed to read display plane latency. "
6142 "Disable CxSR\n");
6143 }
6144
6145 if (IS_GEN5(dev))
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006146 dev_priv->display.init_clock_gating = ironlake_init_clock_gating;
Ville Syrjäläbd602542014-01-07 16:14:10 +02006147 else if (IS_GEN6(dev))
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006148 dev_priv->display.init_clock_gating = gen6_init_clock_gating;
Ville Syrjäläbd602542014-01-07 16:14:10 +02006149 else if (IS_IVYBRIDGE(dev))
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006150 dev_priv->display.init_clock_gating = ivybridge_init_clock_gating;
Ville Syrjäläbd602542014-01-07 16:14:10 +02006151 else if (IS_HASWELL(dev))
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03006152 dev_priv->display.init_clock_gating = haswell_init_clock_gating;
Ville Syrjäläbd602542014-01-07 16:14:10 +02006153 else if (INTEL_INFO(dev)->gen == 8)
Paulo Zanoni47c2bd92014-08-21 17:09:37 -03006154 dev_priv->display.init_clock_gating = broadwell_init_clock_gating;
Ville Syrjäläa4565da2014-04-09 13:28:10 +03006155 } else if (IS_CHERRYVIEW(dev)) {
Ville Syrjälä3c2777f2014-06-26 17:03:06 +03006156 dev_priv->display.update_wm = cherryview_update_wm;
Gajanan Bhat01e184c2014-08-07 17:03:30 +05306157 dev_priv->display.update_sprite_wm = valleyview_update_sprite_wm;
Ville Syrjäläa4565da2014-04-09 13:28:10 +03006158 dev_priv->display.init_clock_gating =
6159 cherryview_init_clock_gating;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006160 } else if (IS_VALLEYVIEW(dev)) {
6161 dev_priv->display.update_wm = valleyview_update_wm;
Gajanan Bhat01e184c2014-08-07 17:03:30 +05306162 dev_priv->display.update_sprite_wm = valleyview_update_sprite_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006163 dev_priv->display.init_clock_gating =
6164 valleyview_init_clock_gating;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006165 } else if (IS_PINEVIEW(dev)) {
6166 if (!intel_get_cxsr_latency(IS_PINEVIEW_G(dev),
6167 dev_priv->is_ddr3,
6168 dev_priv->fsb_freq,
6169 dev_priv->mem_freq)) {
6170 DRM_INFO("failed to find known CxSR latency "
6171 "(found ddr%s fsb freq %d, mem freq %d), "
6172 "disabling CxSR\n",
6173 (dev_priv->is_ddr3 == 1) ? "3" : "2",
6174 dev_priv->fsb_freq, dev_priv->mem_freq);
6175 /* Disable CxSR and never update its watermark again */
Imre Deak5209b1f2014-07-01 12:36:17 +03006176 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006177 dev_priv->display.update_wm = NULL;
6178 } else
6179 dev_priv->display.update_wm = pineview_update_wm;
6180 dev_priv->display.init_clock_gating = gen3_init_clock_gating;
6181 } else if (IS_G4X(dev)) {
6182 dev_priv->display.update_wm = g4x_update_wm;
6183 dev_priv->display.init_clock_gating = g4x_init_clock_gating;
6184 } else if (IS_GEN4(dev)) {
6185 dev_priv->display.update_wm = i965_update_wm;
6186 if (IS_CRESTLINE(dev))
6187 dev_priv->display.init_clock_gating = crestline_init_clock_gating;
6188 else if (IS_BROADWATER(dev))
6189 dev_priv->display.init_clock_gating = broadwater_init_clock_gating;
6190 } else if (IS_GEN3(dev)) {
6191 dev_priv->display.update_wm = i9xx_update_wm;
6192 dev_priv->display.get_fifo_size = i9xx_get_fifo_size;
6193 dev_priv->display.init_clock_gating = gen3_init_clock_gating;
Daniel Vetterfeb56b92013-12-14 20:38:30 -02006194 } else if (IS_GEN2(dev)) {
6195 if (INTEL_INFO(dev)->num_pipes == 1) {
6196 dev_priv->display.update_wm = i845_update_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006197 dev_priv->display.get_fifo_size = i845_get_fifo_size;
Daniel Vetterfeb56b92013-12-14 20:38:30 -02006198 } else {
6199 dev_priv->display.update_wm = i9xx_update_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006200 dev_priv->display.get_fifo_size = i830_get_fifo_size;
Daniel Vetterfeb56b92013-12-14 20:38:30 -02006201 }
6202
6203 if (IS_I85X(dev) || IS_I865G(dev))
6204 dev_priv->display.init_clock_gating = i85x_init_clock_gating;
6205 else
6206 dev_priv->display.init_clock_gating = i830_init_clock_gating;
6207 } else {
6208 DRM_ERROR("unexpected fall-through in intel_init_pm\n");
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006209 }
6210}
6211
Ben Widawsky42c05262012-09-26 10:34:00 -07006212int sandybridge_pcode_read(struct drm_i915_private *dev_priv, u8 mbox, u32 *val)
6213{
Jesse Barnes4fc688c2012-11-02 11:14:01 -07006214 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Ben Widawsky42c05262012-09-26 10:34:00 -07006215
6216 if (I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) {
6217 DRM_DEBUG_DRIVER("warning: pcode (read) mailbox access failed\n");
6218 return -EAGAIN;
6219 }
6220
6221 I915_WRITE(GEN6_PCODE_DATA, *val);
6222 I915_WRITE(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY | mbox);
6223
6224 if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
6225 500)) {
6226 DRM_ERROR("timeout waiting for pcode read (%d) to finish\n", mbox);
6227 return -ETIMEDOUT;
6228 }
6229
6230 *val = I915_READ(GEN6_PCODE_DATA);
6231 I915_WRITE(GEN6_PCODE_DATA, 0);
6232
6233 return 0;
6234}
6235
6236int sandybridge_pcode_write(struct drm_i915_private *dev_priv, u8 mbox, u32 val)
6237{
Jesse Barnes4fc688c2012-11-02 11:14:01 -07006238 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Ben Widawsky42c05262012-09-26 10:34:00 -07006239
6240 if (I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) {
6241 DRM_DEBUG_DRIVER("warning: pcode (write) mailbox access failed\n");
6242 return -EAGAIN;
6243 }
6244
6245 I915_WRITE(GEN6_PCODE_DATA, val);
6246 I915_WRITE(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY | mbox);
6247
6248 if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
6249 500)) {
6250 DRM_ERROR("timeout waiting for pcode write (%d) to finish\n", mbox);
6251 return -ETIMEDOUT;
6252 }
6253
6254 I915_WRITE(GEN6_PCODE_DATA, 0);
6255
6256 return 0;
6257}
Jesse Barnesa0e4e192013-04-02 11:23:05 -07006258
Fengguang Wub55dd642014-07-12 11:21:39 +02006259static int byt_gpu_freq(struct drm_i915_private *dev_priv, int val)
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006260{
Ville Syrjälä07ab1182013-11-05 22:42:28 +02006261 int div;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006262
Ville Syrjälä07ab1182013-11-05 22:42:28 +02006263 /* 4 x czclk */
Ville Syrjälä2ec38152013-11-05 22:42:29 +02006264 switch (dev_priv->mem_freq) {
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006265 case 800:
Ville Syrjälä07ab1182013-11-05 22:42:28 +02006266 div = 10;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006267 break;
6268 case 1066:
Ville Syrjälä07ab1182013-11-05 22:42:28 +02006269 div = 12;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006270 break;
6271 case 1333:
Ville Syrjälä07ab1182013-11-05 22:42:28 +02006272 div = 16;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006273 break;
6274 default:
6275 return -1;
6276 }
6277
Ville Syrjälä2ec38152013-11-05 22:42:29 +02006278 return DIV_ROUND_CLOSEST(dev_priv->mem_freq * (val + 6 - 0xbd), 4 * div);
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006279}
6280
Fengguang Wub55dd642014-07-12 11:21:39 +02006281static int byt_freq_opcode(struct drm_i915_private *dev_priv, int val)
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006282{
Ville Syrjälä07ab1182013-11-05 22:42:28 +02006283 int mul;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006284
Ville Syrjälä07ab1182013-11-05 22:42:28 +02006285 /* 4 x czclk */
Ville Syrjälä2ec38152013-11-05 22:42:29 +02006286 switch (dev_priv->mem_freq) {
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006287 case 800:
Ville Syrjälä07ab1182013-11-05 22:42:28 +02006288 mul = 10;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006289 break;
6290 case 1066:
Ville Syrjälä07ab1182013-11-05 22:42:28 +02006291 mul = 12;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006292 break;
6293 case 1333:
Ville Syrjälä07ab1182013-11-05 22:42:28 +02006294 mul = 16;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006295 break;
6296 default:
6297 return -1;
6298 }
6299
Ville Syrjälä2ec38152013-11-05 22:42:29 +02006300 return DIV_ROUND_CLOSEST(4 * mul * val, dev_priv->mem_freq) + 0xbd - 6;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006301}
6302
Fengguang Wub55dd642014-07-12 11:21:39 +02006303static int chv_gpu_freq(struct drm_i915_private *dev_priv, int val)
Deepak S22b1b2f2014-07-12 14:54:33 +05306304{
6305 int div, freq;
6306
6307 switch (dev_priv->rps.cz_freq) {
6308 case 200:
6309 div = 5;
6310 break;
6311 case 267:
6312 div = 6;
6313 break;
6314 case 320:
6315 case 333:
6316 case 400:
6317 div = 8;
6318 break;
6319 default:
6320 return -1;
6321 }
6322
6323 freq = (DIV_ROUND_CLOSEST((dev_priv->rps.cz_freq * val), 2 * div) / 2);
6324
6325 return freq;
6326}
6327
Fengguang Wub55dd642014-07-12 11:21:39 +02006328static int chv_freq_opcode(struct drm_i915_private *dev_priv, int val)
Deepak S22b1b2f2014-07-12 14:54:33 +05306329{
6330 int mul, opcode;
6331
6332 switch (dev_priv->rps.cz_freq) {
6333 case 200:
6334 mul = 5;
6335 break;
6336 case 267:
6337 mul = 6;
6338 break;
6339 case 320:
6340 case 333:
6341 case 400:
6342 mul = 8;
6343 break;
6344 default:
6345 return -1;
6346 }
6347
Ville Syrjälä1c147622014-08-18 14:42:43 +03006348 /* CHV needs even values */
Deepak S22b1b2f2014-07-12 14:54:33 +05306349 opcode = (DIV_ROUND_CLOSEST((val * 2 * mul), dev_priv->rps.cz_freq) * 2);
6350
6351 return opcode;
6352}
6353
6354int vlv_gpu_freq(struct drm_i915_private *dev_priv, int val)
6355{
6356 int ret = -1;
6357
6358 if (IS_CHERRYVIEW(dev_priv->dev))
6359 ret = chv_gpu_freq(dev_priv, val);
6360 else if (IS_VALLEYVIEW(dev_priv->dev))
6361 ret = byt_gpu_freq(dev_priv, val);
6362
6363 return ret;
6364}
6365
6366int vlv_freq_opcode(struct drm_i915_private *dev_priv, int val)
6367{
6368 int ret = -1;
6369
6370 if (IS_CHERRYVIEW(dev_priv->dev))
6371 ret = chv_freq_opcode(dev_priv, val);
6372 else if (IS_VALLEYVIEW(dev_priv->dev))
6373 ret = byt_freq_opcode(dev_priv, val);
6374
6375 return ret;
6376}
6377
Daniel Vetterf742a552013-12-06 10:17:53 +01006378void intel_pm_setup(struct drm_device *dev)
Chris Wilson907b28c2013-07-19 20:36:52 +01006379{
6380 struct drm_i915_private *dev_priv = dev->dev_private;
6381
Daniel Vetterf742a552013-12-06 10:17:53 +01006382 mutex_init(&dev_priv->rps.hw_lock);
6383
Chris Wilson907b28c2013-07-19 20:36:52 +01006384 INIT_DELAYED_WORK(&dev_priv->rps.delayed_resume_work,
6385 intel_gen6_powersave_work);
Paulo Zanoni5d584b22014-03-07 20:08:15 -03006386
Paulo Zanoni33688d92014-03-07 20:08:19 -03006387 dev_priv->pm.suspended = false;
Chris Wilson907b28c2013-07-19 20:36:52 +01006388}