blob: 9851af776aadd51d1407197d322be11a7df684a2 [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{
Rodrigo Vivi5e56ba42014-10-17 08:05:08 -07001348 struct drm_device *dev = crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001349 int entries;
Gajanan Bhat0948c262014-08-07 01:58:24 +05301350 int clock = to_intel_crtc(crtc)->config.adjusted_mode.crtc_clock;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001351
Gajanan Bhat0948c262014-08-07 01:58:24 +05301352 if (WARN(clock == 0, "Pixel clock is zero!\n"))
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001353 return false;
1354
Gajanan Bhat0948c262014-08-07 01:58:24 +05301355 if (WARN(pixel_size == 0, "Pixel size is zero!\n"))
1356 return false;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001357
Gajanan Bhata398e9c2014-08-05 23:15:54 +05301358 entries = DIV_ROUND_UP(clock, 1000) * pixel_size;
Rodrigo Vivi5e56ba42014-10-17 08:05:08 -07001359 if (IS_CHERRYVIEW(dev))
1360 *prec_mult = (entries > 128) ? DRAIN_LATENCY_PRECISION_32 :
1361 DRAIN_LATENCY_PRECISION_16;
1362 else
1363 *prec_mult = (entries > 128) ? DRAIN_LATENCY_PRECISION_64 :
1364 DRAIN_LATENCY_PRECISION_32;
Gajanan Bhat0948c262014-08-07 01:58:24 +05301365 *drain_latency = (64 * (*prec_mult) * 4) / entries;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001366
Gajanan Bhata398e9c2014-08-05 23:15:54 +05301367 if (*drain_latency > DRAIN_LATENCY_MASK)
1368 *drain_latency = DRAIN_LATENCY_MASK;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001369
1370 return true;
1371}
1372
1373/*
1374 * Update drain latency registers of memory arbiter
1375 *
1376 * Valleyview SoC has a new memory arbiter and needs drain latency registers
1377 * to be programmed. Each plane has a drain latency multiplier and a drain
1378 * latency value.
1379 */
1380
Gajanan Bhat41aad812014-07-16 18:24:03 +05301381static void vlv_update_drain_latency(struct drm_crtc *crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001382{
Rodrigo Vivi5e56ba42014-10-17 08:05:08 -07001383 struct drm_device *dev = crtc->dev;
1384 struct drm_i915_private *dev_priv = dev->dev_private;
Gajanan Bhat0948c262014-08-07 01:58:24 +05301385 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1386 int pixel_size;
1387 int drain_latency;
1388 enum pipe pipe = intel_crtc->pipe;
1389 int plane_prec, prec_mult, plane_dl;
Rodrigo Vivi5e56ba42014-10-17 08:05:08 -07001390 const int high_precision = IS_CHERRYVIEW(dev) ?
1391 DRAIN_LATENCY_PRECISION_32 : DRAIN_LATENCY_PRECISION_64;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001392
Rodrigo Vivi5e56ba42014-10-17 08:05:08 -07001393 plane_dl = I915_READ(VLV_DDL(pipe)) & ~(DDL_PLANE_PRECISION_HIGH |
1394 DRAIN_LATENCY_MASK | DDL_CURSOR_PRECISION_HIGH |
Gajanan Bhat0948c262014-08-07 01:58:24 +05301395 (DRAIN_LATENCY_MASK << DDL_CURSOR_SHIFT));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001396
Gajanan Bhat0948c262014-08-07 01:58:24 +05301397 if (!intel_crtc_active(crtc)) {
1398 I915_WRITE(VLV_DDL(pipe), plane_dl);
1399 return;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001400 }
1401
Gajanan Bhat0948c262014-08-07 01:58:24 +05301402 /* Primary plane Drain Latency */
1403 pixel_size = crtc->primary->fb->bits_per_pixel / 8; /* BPP */
1404 if (vlv_compute_drain_latency(crtc, pixel_size, &prec_mult, &drain_latency)) {
Rodrigo Vivi5e56ba42014-10-17 08:05:08 -07001405 plane_prec = (prec_mult == high_precision) ?
1406 DDL_PLANE_PRECISION_HIGH :
1407 DDL_PLANE_PRECISION_LOW;
Gajanan Bhat0948c262014-08-07 01:58:24 +05301408 plane_dl |= plane_prec | drain_latency;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001409 }
Gajanan Bhat0948c262014-08-07 01:58:24 +05301410
1411 /* Cursor Drain Latency
1412 * BPP is always 4 for cursor
1413 */
1414 pixel_size = 4;
1415
1416 /* Program cursor DL only if it is enabled */
1417 if (intel_crtc->cursor_base &&
1418 vlv_compute_drain_latency(crtc, pixel_size, &prec_mult, &drain_latency)) {
Rodrigo Vivi5e56ba42014-10-17 08:05:08 -07001419 plane_prec = (prec_mult == high_precision) ?
1420 DDL_CURSOR_PRECISION_HIGH :
1421 DDL_CURSOR_PRECISION_LOW;
Gajanan Bhat0948c262014-08-07 01:58:24 +05301422 plane_dl |= plane_prec | (drain_latency << DDL_CURSOR_SHIFT);
1423 }
1424
1425 I915_WRITE(VLV_DDL(pipe), plane_dl);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001426}
1427
1428#define single_plane_enabled(mask) is_power_of_2(mask)
1429
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001430static void valleyview_update_wm(struct drm_crtc *crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001431{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001432 struct drm_device *dev = crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001433 static const int sr_latency_ns = 12000;
1434 struct drm_i915_private *dev_priv = dev->dev_private;
1435 int planea_wm, planeb_wm, cursora_wm, cursorb_wm;
1436 int plane_sr, cursor_sr;
Chris Wilsonaf6c4572012-12-11 12:01:43 +00001437 int ignore_plane_sr, ignore_cursor_sr;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001438 unsigned int enabled = 0;
Imre Deak98584252014-06-13 14:54:20 +03001439 bool cxsr_enabled;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001440
Gajanan Bhat41aad812014-07-16 18:24:03 +05301441 vlv_update_drain_latency(crtc);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001442
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001443 if (g4x_compute_wm0(dev, PIPE_A,
Chris Wilson5aef6002014-09-03 11:56:07 +01001444 &valleyview_wm_info, pessimal_latency_ns,
1445 &valleyview_cursor_wm_info, pessimal_latency_ns,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001446 &planea_wm, &cursora_wm))
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001447 enabled |= 1 << PIPE_A;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001448
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001449 if (g4x_compute_wm0(dev, PIPE_B,
Chris Wilson5aef6002014-09-03 11:56:07 +01001450 &valleyview_wm_info, pessimal_latency_ns,
1451 &valleyview_cursor_wm_info, pessimal_latency_ns,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001452 &planeb_wm, &cursorb_wm))
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001453 enabled |= 1 << PIPE_B;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001454
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001455 if (single_plane_enabled(enabled) &&
1456 g4x_compute_srwm(dev, ffs(enabled) - 1,
1457 sr_latency_ns,
1458 &valleyview_wm_info,
1459 &valleyview_cursor_wm_info,
Chris Wilsonaf6c4572012-12-11 12:01:43 +00001460 &plane_sr, &ignore_cursor_sr) &&
1461 g4x_compute_srwm(dev, ffs(enabled) - 1,
1462 2*sr_latency_ns,
1463 &valleyview_wm_info,
1464 &valleyview_cursor_wm_info,
Chris Wilson52bd02d2012-12-07 10:43:24 +00001465 &ignore_plane_sr, &cursor_sr)) {
Imre Deak98584252014-06-13 14:54:20 +03001466 cxsr_enabled = true;
Chris Wilson52bd02d2012-12-07 10:43:24 +00001467 } else {
Imre Deak98584252014-06-13 14:54:20 +03001468 cxsr_enabled = false;
Imre Deak5209b1f2014-07-01 12:36:17 +03001469 intel_set_memory_cxsr(dev_priv, false);
Chris Wilson52bd02d2012-12-07 10:43:24 +00001470 plane_sr = cursor_sr = 0;
1471 }
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001472
Ville Syrjäläa5043452014-06-28 02:04:18 +03001473 DRM_DEBUG_KMS("Setting FIFO watermarks - A: plane=%d, cursor=%d, "
1474 "B: plane=%d, cursor=%d, SR: plane=%d, cursor=%d\n",
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001475 planea_wm, cursora_wm,
1476 planeb_wm, cursorb_wm,
1477 plane_sr, cursor_sr);
1478
1479 I915_WRITE(DSPFW1,
1480 (plane_sr << DSPFW_SR_SHIFT) |
1481 (cursorb_wm << DSPFW_CURSORB_SHIFT) |
1482 (planeb_wm << DSPFW_PLANEB_SHIFT) |
Ville Syrjälä0a560672014-06-11 16:51:18 +03001483 (planea_wm << DSPFW_PLANEA_SHIFT));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001484 I915_WRITE(DSPFW2,
Chris Wilson8c919b22012-12-04 16:33:19 +00001485 (I915_READ(DSPFW2) & ~DSPFW_CURSORA_MASK) |
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001486 (cursora_wm << DSPFW_CURSORA_SHIFT));
1487 I915_WRITE(DSPFW3,
Chris Wilson8c919b22012-12-04 16:33:19 +00001488 (I915_READ(DSPFW3) & ~DSPFW_CURSOR_SR_MASK) |
1489 (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
Imre Deak98584252014-06-13 14:54:20 +03001490
1491 if (cxsr_enabled)
1492 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001493}
1494
Ville Syrjälä3c2777f2014-06-26 17:03:06 +03001495static void cherryview_update_wm(struct drm_crtc *crtc)
1496{
1497 struct drm_device *dev = crtc->dev;
1498 static const int sr_latency_ns = 12000;
1499 struct drm_i915_private *dev_priv = dev->dev_private;
1500 int planea_wm, planeb_wm, planec_wm;
1501 int cursora_wm, cursorb_wm, cursorc_wm;
1502 int plane_sr, cursor_sr;
1503 int ignore_plane_sr, ignore_cursor_sr;
1504 unsigned int enabled = 0;
1505 bool cxsr_enabled;
1506
1507 vlv_update_drain_latency(crtc);
1508
1509 if (g4x_compute_wm0(dev, PIPE_A,
Chris Wilson5aef6002014-09-03 11:56:07 +01001510 &valleyview_wm_info, pessimal_latency_ns,
1511 &valleyview_cursor_wm_info, pessimal_latency_ns,
Ville Syrjälä3c2777f2014-06-26 17:03:06 +03001512 &planea_wm, &cursora_wm))
1513 enabled |= 1 << PIPE_A;
1514
1515 if (g4x_compute_wm0(dev, PIPE_B,
Chris Wilson5aef6002014-09-03 11:56:07 +01001516 &valleyview_wm_info, pessimal_latency_ns,
1517 &valleyview_cursor_wm_info, pessimal_latency_ns,
Ville Syrjälä3c2777f2014-06-26 17:03:06 +03001518 &planeb_wm, &cursorb_wm))
1519 enabled |= 1 << PIPE_B;
1520
1521 if (g4x_compute_wm0(dev, PIPE_C,
Chris Wilson5aef6002014-09-03 11:56:07 +01001522 &valleyview_wm_info, pessimal_latency_ns,
1523 &valleyview_cursor_wm_info, pessimal_latency_ns,
Ville Syrjälä3c2777f2014-06-26 17:03:06 +03001524 &planec_wm, &cursorc_wm))
1525 enabled |= 1 << PIPE_C;
1526
1527 if (single_plane_enabled(enabled) &&
1528 g4x_compute_srwm(dev, ffs(enabled) - 1,
1529 sr_latency_ns,
1530 &valleyview_wm_info,
1531 &valleyview_cursor_wm_info,
1532 &plane_sr, &ignore_cursor_sr) &&
1533 g4x_compute_srwm(dev, ffs(enabled) - 1,
1534 2*sr_latency_ns,
1535 &valleyview_wm_info,
1536 &valleyview_cursor_wm_info,
1537 &ignore_plane_sr, &cursor_sr)) {
1538 cxsr_enabled = true;
1539 } else {
1540 cxsr_enabled = false;
1541 intel_set_memory_cxsr(dev_priv, false);
1542 plane_sr = cursor_sr = 0;
1543 }
1544
1545 DRM_DEBUG_KMS("Setting FIFO watermarks - A: plane=%d, cursor=%d, "
1546 "B: plane=%d, cursor=%d, C: plane=%d, cursor=%d, "
1547 "SR: plane=%d, cursor=%d\n",
1548 planea_wm, cursora_wm,
1549 planeb_wm, cursorb_wm,
1550 planec_wm, cursorc_wm,
1551 plane_sr, cursor_sr);
1552
1553 I915_WRITE(DSPFW1,
1554 (plane_sr << DSPFW_SR_SHIFT) |
1555 (cursorb_wm << DSPFW_CURSORB_SHIFT) |
1556 (planeb_wm << DSPFW_PLANEB_SHIFT) |
1557 (planea_wm << DSPFW_PLANEA_SHIFT));
1558 I915_WRITE(DSPFW2,
1559 (I915_READ(DSPFW2) & ~DSPFW_CURSORA_MASK) |
1560 (cursora_wm << DSPFW_CURSORA_SHIFT));
1561 I915_WRITE(DSPFW3,
1562 (I915_READ(DSPFW3) & ~DSPFW_CURSOR_SR_MASK) |
1563 (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
1564 I915_WRITE(DSPFW9_CHV,
1565 (I915_READ(DSPFW9_CHV) & ~(DSPFW_PLANEC_MASK |
1566 DSPFW_CURSORC_MASK)) |
1567 (planec_wm << DSPFW_PLANEC_SHIFT) |
1568 (cursorc_wm << DSPFW_CURSORC_SHIFT));
1569
1570 if (cxsr_enabled)
1571 intel_set_memory_cxsr(dev_priv, true);
1572}
1573
Gajanan Bhat01e184c2014-08-07 17:03:30 +05301574static void valleyview_update_sprite_wm(struct drm_plane *plane,
1575 struct drm_crtc *crtc,
1576 uint32_t sprite_width,
1577 uint32_t sprite_height,
1578 int pixel_size,
1579 bool enabled, bool scaled)
1580{
1581 struct drm_device *dev = crtc->dev;
1582 struct drm_i915_private *dev_priv = dev->dev_private;
1583 int pipe = to_intel_plane(plane)->pipe;
1584 int sprite = to_intel_plane(plane)->plane;
1585 int drain_latency;
1586 int plane_prec;
1587 int sprite_dl;
1588 int prec_mult;
Rodrigo Vivi5e56ba42014-10-17 08:05:08 -07001589 const int high_precision = IS_CHERRYVIEW(dev) ?
1590 DRAIN_LATENCY_PRECISION_32 : DRAIN_LATENCY_PRECISION_64;
Gajanan Bhat01e184c2014-08-07 17:03:30 +05301591
Rodrigo Vivi5e56ba42014-10-17 08:05:08 -07001592 sprite_dl = I915_READ(VLV_DDL(pipe)) & ~(DDL_SPRITE_PRECISION_HIGH(sprite) |
Gajanan Bhat01e184c2014-08-07 17:03:30 +05301593 (DRAIN_LATENCY_MASK << DDL_SPRITE_SHIFT(sprite)));
1594
1595 if (enabled && vlv_compute_drain_latency(crtc, pixel_size, &prec_mult,
1596 &drain_latency)) {
Rodrigo Vivi5e56ba42014-10-17 08:05:08 -07001597 plane_prec = (prec_mult == high_precision) ?
1598 DDL_SPRITE_PRECISION_HIGH(sprite) :
1599 DDL_SPRITE_PRECISION_LOW(sprite);
Gajanan Bhat01e184c2014-08-07 17:03:30 +05301600 sprite_dl |= plane_prec |
1601 (drain_latency << DDL_SPRITE_SHIFT(sprite));
1602 }
1603
1604 I915_WRITE(VLV_DDL(pipe), sprite_dl);
1605}
1606
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001607static void g4x_update_wm(struct drm_crtc *crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001608{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001609 struct drm_device *dev = crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001610 static const int sr_latency_ns = 12000;
1611 struct drm_i915_private *dev_priv = dev->dev_private;
1612 int planea_wm, planeb_wm, cursora_wm, cursorb_wm;
1613 int plane_sr, cursor_sr;
1614 unsigned int enabled = 0;
Imre Deak98584252014-06-13 14:54:20 +03001615 bool cxsr_enabled;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001616
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001617 if (g4x_compute_wm0(dev, PIPE_A,
Chris Wilson5aef6002014-09-03 11:56:07 +01001618 &g4x_wm_info, pessimal_latency_ns,
1619 &g4x_cursor_wm_info, pessimal_latency_ns,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001620 &planea_wm, &cursora_wm))
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001621 enabled |= 1 << PIPE_A;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001622
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001623 if (g4x_compute_wm0(dev, PIPE_B,
Chris Wilson5aef6002014-09-03 11:56:07 +01001624 &g4x_wm_info, pessimal_latency_ns,
1625 &g4x_cursor_wm_info, pessimal_latency_ns,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001626 &planeb_wm, &cursorb_wm))
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001627 enabled |= 1 << PIPE_B;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001628
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001629 if (single_plane_enabled(enabled) &&
1630 g4x_compute_srwm(dev, ffs(enabled) - 1,
1631 sr_latency_ns,
1632 &g4x_wm_info,
1633 &g4x_cursor_wm_info,
Chris Wilson52bd02d2012-12-07 10:43:24 +00001634 &plane_sr, &cursor_sr)) {
Imre Deak98584252014-06-13 14:54:20 +03001635 cxsr_enabled = true;
Chris Wilson52bd02d2012-12-07 10:43:24 +00001636 } else {
Imre Deak98584252014-06-13 14:54:20 +03001637 cxsr_enabled = false;
Imre Deak5209b1f2014-07-01 12:36:17 +03001638 intel_set_memory_cxsr(dev_priv, false);
Chris Wilson52bd02d2012-12-07 10:43:24 +00001639 plane_sr = cursor_sr = 0;
1640 }
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001641
Ville Syrjäläa5043452014-06-28 02:04:18 +03001642 DRM_DEBUG_KMS("Setting FIFO watermarks - A: plane=%d, cursor=%d, "
1643 "B: plane=%d, cursor=%d, SR: plane=%d, cursor=%d\n",
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001644 planea_wm, cursora_wm,
1645 planeb_wm, cursorb_wm,
1646 plane_sr, cursor_sr);
1647
1648 I915_WRITE(DSPFW1,
1649 (plane_sr << DSPFW_SR_SHIFT) |
1650 (cursorb_wm << DSPFW_CURSORB_SHIFT) |
1651 (planeb_wm << DSPFW_PLANEB_SHIFT) |
Ville Syrjälä0a560672014-06-11 16:51:18 +03001652 (planea_wm << DSPFW_PLANEA_SHIFT));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001653 I915_WRITE(DSPFW2,
Chris Wilson8c919b22012-12-04 16:33:19 +00001654 (I915_READ(DSPFW2) & ~DSPFW_CURSORA_MASK) |
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001655 (cursora_wm << DSPFW_CURSORA_SHIFT));
1656 /* HPLL off in SR has some issues on G4x... disable it */
1657 I915_WRITE(DSPFW3,
Chris Wilson8c919b22012-12-04 16:33:19 +00001658 (I915_READ(DSPFW3) & ~(DSPFW_HPLL_SR_EN | DSPFW_CURSOR_SR_MASK)) |
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001659 (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
Imre Deak98584252014-06-13 14:54:20 +03001660
1661 if (cxsr_enabled)
1662 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001663}
1664
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001665static void i965_update_wm(struct drm_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001666{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001667 struct drm_device *dev = unused_crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001668 struct drm_i915_private *dev_priv = dev->dev_private;
1669 struct drm_crtc *crtc;
1670 int srwm = 1;
1671 int cursor_sr = 16;
Imre Deak98584252014-06-13 14:54:20 +03001672 bool cxsr_enabled;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001673
1674 /* Calc sr entries for one plane configs */
1675 crtc = single_enabled_crtc(dev);
1676 if (crtc) {
1677 /* self-refresh has much higher latency */
1678 static const int sr_latency_ns = 12000;
Ville Syrjälä4fe85902013-09-04 18:25:22 +03001679 const struct drm_display_mode *adjusted_mode =
1680 &to_intel_crtc(crtc)->config.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001681 int clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -08001682 int htotal = adjusted_mode->crtc_htotal;
Ville Syrjälä37327ab2013-09-04 18:25:28 +03001683 int hdisplay = to_intel_crtc(crtc)->config.pipe_src_w;
Matt Roperf4510a22014-04-01 15:22:40 -07001684 int pixel_size = crtc->primary->fb->bits_per_pixel / 8;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001685 unsigned long line_time_us;
1686 int entries;
1687
Ville Syrjälä922044c2014-02-14 14:18:57 +02001688 line_time_us = max(htotal * 1000 / clock, 1);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001689
1690 /* Use ns/us then divide to preserve precision */
1691 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
1692 pixel_size * hdisplay;
1693 entries = DIV_ROUND_UP(entries, I915_FIFO_LINE_SIZE);
1694 srwm = I965_FIFO_SIZE - entries;
1695 if (srwm < 0)
1696 srwm = 1;
1697 srwm &= 0x1ff;
1698 DRM_DEBUG_KMS("self-refresh entries: %d, wm: %d\n",
1699 entries, srwm);
1700
1701 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
Chris Wilson7bb836d2014-03-26 12:38:14 +00001702 pixel_size * to_intel_crtc(crtc)->cursor_width;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001703 entries = DIV_ROUND_UP(entries,
1704 i965_cursor_wm_info.cacheline_size);
1705 cursor_sr = i965_cursor_wm_info.fifo_size -
1706 (entries + i965_cursor_wm_info.guard_size);
1707
1708 if (cursor_sr > i965_cursor_wm_info.max_wm)
1709 cursor_sr = i965_cursor_wm_info.max_wm;
1710
1711 DRM_DEBUG_KMS("self-refresh watermark: display plane %d "
1712 "cursor %d\n", srwm, cursor_sr);
1713
Imre Deak98584252014-06-13 14:54:20 +03001714 cxsr_enabled = true;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001715 } else {
Imre Deak98584252014-06-13 14:54:20 +03001716 cxsr_enabled = false;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001717 /* Turn off self refresh if both pipes are enabled */
Imre Deak5209b1f2014-07-01 12:36:17 +03001718 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001719 }
1720
1721 DRM_DEBUG_KMS("Setting FIFO watermarks - A: 8, B: 8, C: 8, SR %d\n",
1722 srwm);
1723
1724 /* 965 has limitations... */
1725 I915_WRITE(DSPFW1, (srwm << DSPFW_SR_SHIFT) |
Ville Syrjälä0a560672014-06-11 16:51:18 +03001726 (8 << DSPFW_CURSORB_SHIFT) |
1727 (8 << DSPFW_PLANEB_SHIFT) |
1728 (8 << DSPFW_PLANEA_SHIFT));
1729 I915_WRITE(DSPFW2, (8 << DSPFW_CURSORA_SHIFT) |
1730 (8 << DSPFW_PLANEC_SHIFT_OLD));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001731 /* update cursor SR watermark */
1732 I915_WRITE(DSPFW3, (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
Imre Deak98584252014-06-13 14:54:20 +03001733
1734 if (cxsr_enabled)
1735 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001736}
1737
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001738static void i9xx_update_wm(struct drm_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001739{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001740 struct drm_device *dev = unused_crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001741 struct drm_i915_private *dev_priv = dev->dev_private;
1742 const struct intel_watermark_params *wm_info;
1743 uint32_t fwater_lo;
1744 uint32_t fwater_hi;
1745 int cwm, srwm = 1;
1746 int fifo_size;
1747 int planea_wm, planeb_wm;
1748 struct drm_crtc *crtc, *enabled = NULL;
1749
1750 if (IS_I945GM(dev))
1751 wm_info = &i945_wm_info;
1752 else if (!IS_GEN2(dev))
1753 wm_info = &i915_wm_info;
1754 else
Ville Syrjälä9d539102014-08-15 01:21:53 +03001755 wm_info = &i830_a_wm_info;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001756
1757 fifo_size = dev_priv->display.get_fifo_size(dev, 0);
1758 crtc = intel_get_crtc_for_plane(dev, 0);
Chris Wilson3490ea52013-01-07 10:11:40 +00001759 if (intel_crtc_active(crtc)) {
Damien Lespiau241bfc32013-09-25 16:45:37 +01001760 const struct drm_display_mode *adjusted_mode;
Matt Roperf4510a22014-04-01 15:22:40 -07001761 int cpp = crtc->primary->fb->bits_per_pixel / 8;
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001762 if (IS_GEN2(dev))
1763 cpp = 4;
1764
Damien Lespiau241bfc32013-09-25 16:45:37 +01001765 adjusted_mode = &to_intel_crtc(crtc)->config.adjusted_mode;
1766 planea_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001767 wm_info, fifo_size, cpp,
Chris Wilson5aef6002014-09-03 11:56:07 +01001768 pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001769 enabled = crtc;
Ville Syrjälä9d539102014-08-15 01:21:53 +03001770 } else {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001771 planea_wm = fifo_size - wm_info->guard_size;
Ville Syrjälä9d539102014-08-15 01:21:53 +03001772 if (planea_wm > (long)wm_info->max_wm)
1773 planea_wm = wm_info->max_wm;
1774 }
1775
1776 if (IS_GEN2(dev))
1777 wm_info = &i830_bc_wm_info;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001778
1779 fifo_size = dev_priv->display.get_fifo_size(dev, 1);
1780 crtc = intel_get_crtc_for_plane(dev, 1);
Chris Wilson3490ea52013-01-07 10:11:40 +00001781 if (intel_crtc_active(crtc)) {
Damien Lespiau241bfc32013-09-25 16:45:37 +01001782 const struct drm_display_mode *adjusted_mode;
Matt Roperf4510a22014-04-01 15:22:40 -07001783 int cpp = crtc->primary->fb->bits_per_pixel / 8;
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001784 if (IS_GEN2(dev))
1785 cpp = 4;
1786
Damien Lespiau241bfc32013-09-25 16:45:37 +01001787 adjusted_mode = &to_intel_crtc(crtc)->config.adjusted_mode;
1788 planeb_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001789 wm_info, fifo_size, cpp,
Chris Wilson5aef6002014-09-03 11:56:07 +01001790 pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001791 if (enabled == NULL)
1792 enabled = crtc;
1793 else
1794 enabled = NULL;
Ville Syrjälä9d539102014-08-15 01:21:53 +03001795 } else {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001796 planeb_wm = fifo_size - wm_info->guard_size;
Ville Syrjälä9d539102014-08-15 01:21:53 +03001797 if (planeb_wm > (long)wm_info->max_wm)
1798 planeb_wm = wm_info->max_wm;
1799 }
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001800
1801 DRM_DEBUG_KMS("FIFO watermarks - A: %d, B: %d\n", planea_wm, planeb_wm);
1802
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02001803 if (IS_I915GM(dev) && enabled) {
Matt Roper2ff8fde2014-07-08 07:50:07 -07001804 struct drm_i915_gem_object *obj;
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02001805
Matt Roper2ff8fde2014-07-08 07:50:07 -07001806 obj = intel_fb_obj(enabled->primary->fb);
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02001807
1808 /* self-refresh seems busted with untiled */
Matt Roper2ff8fde2014-07-08 07:50:07 -07001809 if (obj->tiling_mode == I915_TILING_NONE)
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02001810 enabled = NULL;
1811 }
1812
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001813 /*
1814 * Overlay gets an aggressive default since video jitter is bad.
1815 */
1816 cwm = 2;
1817
1818 /* Play safe and disable self-refresh before adjusting watermarks. */
Imre Deak5209b1f2014-07-01 12:36:17 +03001819 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001820
1821 /* Calc sr entries for one plane configs */
1822 if (HAS_FW_BLC(dev) && enabled) {
1823 /* self-refresh has much higher latency */
1824 static const int sr_latency_ns = 6000;
Ville Syrjälä4fe85902013-09-04 18:25:22 +03001825 const struct drm_display_mode *adjusted_mode =
1826 &to_intel_crtc(enabled)->config.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001827 int clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -08001828 int htotal = adjusted_mode->crtc_htotal;
Daniel Vetterf727b492013-11-20 15:02:10 +01001829 int hdisplay = to_intel_crtc(enabled)->config.pipe_src_w;
Matt Roperf4510a22014-04-01 15:22:40 -07001830 int pixel_size = enabled->primary->fb->bits_per_pixel / 8;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001831 unsigned long line_time_us;
1832 int entries;
1833
Ville Syrjälä922044c2014-02-14 14:18:57 +02001834 line_time_us = max(htotal * 1000 / clock, 1);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001835
1836 /* Use ns/us then divide to preserve precision */
1837 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
1838 pixel_size * hdisplay;
1839 entries = DIV_ROUND_UP(entries, wm_info->cacheline_size);
1840 DRM_DEBUG_KMS("self-refresh entries: %d\n", entries);
1841 srwm = wm_info->fifo_size - entries;
1842 if (srwm < 0)
1843 srwm = 1;
1844
1845 if (IS_I945G(dev) || IS_I945GM(dev))
1846 I915_WRITE(FW_BLC_SELF,
1847 FW_BLC_SELF_FIFO_MASK | (srwm & 0xff));
1848 else if (IS_I915GM(dev))
1849 I915_WRITE(FW_BLC_SELF, srwm & 0x3f);
1850 }
1851
1852 DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d, B: %d, C: %d, SR %d\n",
1853 planea_wm, planeb_wm, cwm, srwm);
1854
1855 fwater_lo = ((planeb_wm & 0x3f) << 16) | (planea_wm & 0x3f);
1856 fwater_hi = (cwm & 0x1f);
1857
1858 /* Set request length to 8 cachelines per fetch */
1859 fwater_lo = fwater_lo | (1 << 24) | (1 << 8);
1860 fwater_hi = fwater_hi | (1 << 8);
1861
1862 I915_WRITE(FW_BLC, fwater_lo);
1863 I915_WRITE(FW_BLC2, fwater_hi);
1864
Imre Deak5209b1f2014-07-01 12:36:17 +03001865 if (enabled)
1866 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001867}
1868
Daniel Vetterfeb56b92013-12-14 20:38:30 -02001869static void i845_update_wm(struct drm_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001870{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001871 struct drm_device *dev = unused_crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001872 struct drm_i915_private *dev_priv = dev->dev_private;
1873 struct drm_crtc *crtc;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001874 const struct drm_display_mode *adjusted_mode;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001875 uint32_t fwater_lo;
1876 int planea_wm;
1877
1878 crtc = single_enabled_crtc(dev);
1879 if (crtc == NULL)
1880 return;
1881
Damien Lespiau241bfc32013-09-25 16:45:37 +01001882 adjusted_mode = &to_intel_crtc(crtc)->config.adjusted_mode;
1883 planea_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Daniel Vetterfeb56b92013-12-14 20:38:30 -02001884 &i845_wm_info,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001885 dev_priv->display.get_fifo_size(dev, 0),
Chris Wilson5aef6002014-09-03 11:56:07 +01001886 4, pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001887 fwater_lo = I915_READ(FW_BLC) & ~0xfff;
1888 fwater_lo |= (3<<8) | planea_wm;
1889
1890 DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d\n", planea_wm);
1891
1892 I915_WRITE(FW_BLC, fwater_lo);
1893}
1894
Ville Syrjälä36587292013-07-05 11:57:16 +03001895static uint32_t ilk_pipe_pixel_rate(struct drm_device *dev,
1896 struct drm_crtc *crtc)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001897{
1898 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilsonfd4daa92013-08-27 17:04:17 +01001899 uint32_t pixel_rate;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001900
Damien Lespiau241bfc32013-09-25 16:45:37 +01001901 pixel_rate = intel_crtc->config.adjusted_mode.crtc_clock;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001902
1903 /* We only use IF-ID interlacing. If we ever use PF-ID we'll need to
1904 * adjust the pixel_rate here. */
1905
Chris Wilsonfd4daa92013-08-27 17:04:17 +01001906 if (intel_crtc->config.pch_pfit.enabled) {
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001907 uint64_t pipe_w, pipe_h, pfit_w, pfit_h;
Chris Wilsonfd4daa92013-08-27 17:04:17 +01001908 uint32_t pfit_size = intel_crtc->config.pch_pfit.size;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001909
Ville Syrjälä37327ab2013-09-04 18:25:28 +03001910 pipe_w = intel_crtc->config.pipe_src_w;
1911 pipe_h = intel_crtc->config.pipe_src_h;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001912 pfit_w = (pfit_size >> 16) & 0xFFFF;
1913 pfit_h = pfit_size & 0xFFFF;
1914 if (pipe_w < pfit_w)
1915 pipe_w = pfit_w;
1916 if (pipe_h < pfit_h)
1917 pipe_h = pfit_h;
1918
1919 pixel_rate = div_u64((uint64_t) pixel_rate * pipe_w * pipe_h,
1920 pfit_w * pfit_h);
1921 }
1922
1923 return pixel_rate;
1924}
1925
Ville Syrjälä37126462013-08-01 16:18:55 +03001926/* latency must be in 0.1us units. */
Ville Syrjälä23297042013-07-05 11:57:17 +03001927static uint32_t ilk_wm_method1(uint32_t pixel_rate, uint8_t bytes_per_pixel,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001928 uint32_t latency)
1929{
1930 uint64_t ret;
1931
Ville Syrjälä3312ba62013-08-01 16:18:53 +03001932 if (WARN(latency == 0, "Latency value missing\n"))
1933 return UINT_MAX;
1934
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001935 ret = (uint64_t) pixel_rate * bytes_per_pixel * latency;
1936 ret = DIV_ROUND_UP_ULL(ret, 64 * 10000) + 2;
1937
1938 return ret;
1939}
1940
Ville Syrjälä37126462013-08-01 16:18:55 +03001941/* latency must be in 0.1us units. */
Ville Syrjälä23297042013-07-05 11:57:17 +03001942static uint32_t ilk_wm_method2(uint32_t pixel_rate, uint32_t pipe_htotal,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001943 uint32_t horiz_pixels, uint8_t bytes_per_pixel,
1944 uint32_t latency)
1945{
1946 uint32_t ret;
1947
Ville Syrjälä3312ba62013-08-01 16:18:53 +03001948 if (WARN(latency == 0, "Latency value missing\n"))
1949 return UINT_MAX;
1950
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001951 ret = (latency * pixel_rate) / (pipe_htotal * 10000);
1952 ret = (ret + 1) * horiz_pixels * bytes_per_pixel;
1953 ret = DIV_ROUND_UP(ret, 64) + 2;
1954 return ret;
1955}
1956
Ville Syrjälä23297042013-07-05 11:57:17 +03001957static uint32_t ilk_wm_fbc(uint32_t pri_val, uint32_t horiz_pixels,
Paulo Zanonicca32e92013-05-31 11:45:06 -03001958 uint8_t bytes_per_pixel)
1959{
1960 return DIV_ROUND_UP(pri_val * 64, horiz_pixels * bytes_per_pixel) + 2;
1961}
1962
Pradeep Bhat2ac96d22014-11-04 17:06:40 +00001963struct skl_pipe_wm_parameters {
1964 bool active;
1965 uint32_t pipe_htotal;
1966 uint32_t pixel_rate; /* in KHz */
1967 struct intel_plane_wm_parameters plane[I915_MAX_PLANES];
1968 struct intel_plane_wm_parameters cursor;
1969};
1970
Imre Deak820c1982013-12-17 14:46:36 +02001971struct ilk_pipe_wm_parameters {
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001972 bool active;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001973 uint32_t pipe_htotal;
1974 uint32_t pixel_rate;
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001975 struct intel_plane_wm_parameters pri;
1976 struct intel_plane_wm_parameters spr;
1977 struct intel_plane_wm_parameters cur;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001978};
1979
Imre Deak820c1982013-12-17 14:46:36 +02001980struct ilk_wm_maximums {
Paulo Zanonicca32e92013-05-31 11:45:06 -03001981 uint16_t pri;
1982 uint16_t spr;
1983 uint16_t cur;
1984 uint16_t fbc;
1985};
1986
Ville Syrjälä240264f2013-08-07 13:29:12 +03001987/* used in computing the new watermarks state */
1988struct intel_wm_config {
1989 unsigned int num_pipes_active;
1990 bool sprites_enabled;
1991 bool sprites_scaled;
Ville Syrjälä240264f2013-08-07 13:29:12 +03001992};
1993
Ville Syrjälä37126462013-08-01 16:18:55 +03001994/*
1995 * For both WM_PIPE and WM_LP.
1996 * mem_value must be in 0.1us units.
1997 */
Imre Deak820c1982013-12-17 14:46:36 +02001998static uint32_t ilk_compute_pri_wm(const struct ilk_pipe_wm_parameters *params,
Paulo Zanonicca32e92013-05-31 11:45:06 -03001999 uint32_t mem_value,
2000 bool is_lp)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002001{
Paulo Zanonicca32e92013-05-31 11:45:06 -03002002 uint32_t method1, method2;
2003
Ville Syrjäläc35426d2013-08-07 13:29:50 +03002004 if (!params->active || !params->pri.enabled)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002005 return 0;
2006
Ville Syrjälä23297042013-07-05 11:57:17 +03002007 method1 = ilk_wm_method1(params->pixel_rate,
Ville Syrjäläc35426d2013-08-07 13:29:50 +03002008 params->pri.bytes_per_pixel,
Paulo Zanonicca32e92013-05-31 11:45:06 -03002009 mem_value);
2010
2011 if (!is_lp)
2012 return method1;
2013
Ville Syrjälä23297042013-07-05 11:57:17 +03002014 method2 = ilk_wm_method2(params->pixel_rate,
Paulo Zanonicca32e92013-05-31 11:45:06 -03002015 params->pipe_htotal,
Ville Syrjäläc35426d2013-08-07 13:29:50 +03002016 params->pri.horiz_pixels,
2017 params->pri.bytes_per_pixel,
Paulo Zanonicca32e92013-05-31 11:45:06 -03002018 mem_value);
2019
2020 return min(method1, method2);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002021}
2022
Ville Syrjälä37126462013-08-01 16:18:55 +03002023/*
2024 * For both WM_PIPE and WM_LP.
2025 * mem_value must be in 0.1us units.
2026 */
Imre Deak820c1982013-12-17 14:46:36 +02002027static uint32_t ilk_compute_spr_wm(const struct ilk_pipe_wm_parameters *params,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002028 uint32_t mem_value)
2029{
2030 uint32_t method1, method2;
2031
Ville Syrjäläc35426d2013-08-07 13:29:50 +03002032 if (!params->active || !params->spr.enabled)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002033 return 0;
2034
Ville Syrjälä23297042013-07-05 11:57:17 +03002035 method1 = ilk_wm_method1(params->pixel_rate,
Ville Syrjäläc35426d2013-08-07 13:29:50 +03002036 params->spr.bytes_per_pixel,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002037 mem_value);
Ville Syrjälä23297042013-07-05 11:57:17 +03002038 method2 = 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->spr.horiz_pixels,
2041 params->spr.bytes_per_pixel,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002042 mem_value);
2043 return min(method1, method2);
2044}
2045
Ville Syrjälä37126462013-08-01 16:18:55 +03002046/*
2047 * For both WM_PIPE and WM_LP.
2048 * mem_value must be in 0.1us units.
2049 */
Imre Deak820c1982013-12-17 14:46:36 +02002050static uint32_t ilk_compute_cur_wm(const struct ilk_pipe_wm_parameters *params,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002051 uint32_t mem_value)
2052{
Ville Syrjäläc35426d2013-08-07 13:29:50 +03002053 if (!params->active || !params->cur.enabled)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002054 return 0;
2055
Ville Syrjälä23297042013-07-05 11:57:17 +03002056 return ilk_wm_method2(params->pixel_rate,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002057 params->pipe_htotal,
Ville Syrjäläc35426d2013-08-07 13:29:50 +03002058 params->cur.horiz_pixels,
2059 params->cur.bytes_per_pixel,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002060 mem_value);
2061}
2062
Paulo Zanonicca32e92013-05-31 11:45:06 -03002063/* Only for WM_LP. */
Imre Deak820c1982013-12-17 14:46:36 +02002064static uint32_t ilk_compute_fbc_wm(const struct ilk_pipe_wm_parameters *params,
Ville Syrjälä1fda9882013-07-05 11:57:19 +03002065 uint32_t pri_val)
Paulo Zanonicca32e92013-05-31 11:45:06 -03002066{
Ville Syrjäläc35426d2013-08-07 13:29:50 +03002067 if (!params->active || !params->pri.enabled)
Paulo Zanonicca32e92013-05-31 11:45:06 -03002068 return 0;
2069
Ville Syrjälä23297042013-07-05 11:57:17 +03002070 return ilk_wm_fbc(pri_val,
Ville Syrjäläc35426d2013-08-07 13:29:50 +03002071 params->pri.horiz_pixels,
2072 params->pri.bytes_per_pixel);
Paulo Zanonicca32e92013-05-31 11:45:06 -03002073}
2074
Ville Syrjälä158ae642013-08-07 13:28:19 +03002075static unsigned int ilk_display_fifo_size(const struct drm_device *dev)
2076{
Ville Syrjälä416f4722013-11-02 21:07:46 -07002077 if (INTEL_INFO(dev)->gen >= 8)
2078 return 3072;
2079 else if (INTEL_INFO(dev)->gen >= 7)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002080 return 768;
2081 else
2082 return 512;
2083}
2084
Ville Syrjälä4e975082014-03-07 18:32:11 +02002085static unsigned int ilk_plane_wm_reg_max(const struct drm_device *dev,
2086 int level, bool is_sprite)
2087{
2088 if (INTEL_INFO(dev)->gen >= 8)
2089 /* BDW primary/sprite plane watermarks */
2090 return level == 0 ? 255 : 2047;
2091 else if (INTEL_INFO(dev)->gen >= 7)
2092 /* IVB/HSW primary/sprite plane watermarks */
2093 return level == 0 ? 127 : 1023;
2094 else if (!is_sprite)
2095 /* ILK/SNB primary plane watermarks */
2096 return level == 0 ? 127 : 511;
2097 else
2098 /* ILK/SNB sprite plane watermarks */
2099 return level == 0 ? 63 : 255;
2100}
2101
2102static unsigned int ilk_cursor_wm_reg_max(const struct drm_device *dev,
2103 int level)
2104{
2105 if (INTEL_INFO(dev)->gen >= 7)
2106 return level == 0 ? 63 : 255;
2107 else
2108 return level == 0 ? 31 : 63;
2109}
2110
2111static unsigned int ilk_fbc_wm_reg_max(const struct drm_device *dev)
2112{
2113 if (INTEL_INFO(dev)->gen >= 8)
2114 return 31;
2115 else
2116 return 15;
2117}
2118
Ville Syrjälä158ae642013-08-07 13:28:19 +03002119/* Calculate the maximum primary/sprite plane watermark */
2120static unsigned int ilk_plane_wm_max(const struct drm_device *dev,
2121 int level,
Ville Syrjälä240264f2013-08-07 13:29:12 +03002122 const struct intel_wm_config *config,
Ville Syrjälä158ae642013-08-07 13:28:19 +03002123 enum intel_ddb_partitioning ddb_partitioning,
2124 bool is_sprite)
2125{
2126 unsigned int fifo_size = ilk_display_fifo_size(dev);
Ville Syrjälä158ae642013-08-07 13:28:19 +03002127
2128 /* if sprites aren't enabled, sprites get nothing */
Ville Syrjälä240264f2013-08-07 13:29:12 +03002129 if (is_sprite && !config->sprites_enabled)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002130 return 0;
2131
2132 /* HSW allows LP1+ watermarks even with multiple pipes */
Ville Syrjälä240264f2013-08-07 13:29:12 +03002133 if (level == 0 || config->num_pipes_active > 1) {
Ville Syrjälä158ae642013-08-07 13:28:19 +03002134 fifo_size /= INTEL_INFO(dev)->num_pipes;
2135
2136 /*
2137 * For some reason the non self refresh
2138 * FIFO size is only half of the self
2139 * refresh FIFO size on ILK/SNB.
2140 */
2141 if (INTEL_INFO(dev)->gen <= 6)
2142 fifo_size /= 2;
2143 }
2144
Ville Syrjälä240264f2013-08-07 13:29:12 +03002145 if (config->sprites_enabled) {
Ville Syrjälä158ae642013-08-07 13:28:19 +03002146 /* level 0 is always calculated with 1:1 split */
2147 if (level > 0 && ddb_partitioning == INTEL_DDB_PART_5_6) {
2148 if (is_sprite)
2149 fifo_size *= 5;
2150 fifo_size /= 6;
2151 } else {
2152 fifo_size /= 2;
2153 }
2154 }
2155
2156 /* clamp to max that the registers can hold */
Ville Syrjälä4e975082014-03-07 18:32:11 +02002157 return min(fifo_size, ilk_plane_wm_reg_max(dev, level, is_sprite));
Ville Syrjälä158ae642013-08-07 13:28:19 +03002158}
2159
2160/* Calculate the maximum cursor plane watermark */
2161static unsigned int ilk_cursor_wm_max(const struct drm_device *dev,
Ville Syrjälä240264f2013-08-07 13:29:12 +03002162 int level,
2163 const struct intel_wm_config *config)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002164{
2165 /* HSW LP1+ watermarks w/ multiple pipes */
Ville Syrjälä240264f2013-08-07 13:29:12 +03002166 if (level > 0 && config->num_pipes_active > 1)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002167 return 64;
2168
2169 /* otherwise just report max that registers can hold */
Ville Syrjälä4e975082014-03-07 18:32:11 +02002170 return ilk_cursor_wm_reg_max(dev, level);
Ville Syrjälä158ae642013-08-07 13:28:19 +03002171}
2172
Damien Lespiaud34ff9c2014-01-06 19:17:23 +00002173static void ilk_compute_wm_maximums(const struct drm_device *dev,
Ville Syrjälä34982fe2013-10-09 19:18:09 +03002174 int level,
2175 const struct intel_wm_config *config,
2176 enum intel_ddb_partitioning ddb_partitioning,
Imre Deak820c1982013-12-17 14:46:36 +02002177 struct ilk_wm_maximums *max)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002178{
Ville Syrjälä240264f2013-08-07 13:29:12 +03002179 max->pri = ilk_plane_wm_max(dev, level, config, ddb_partitioning, false);
2180 max->spr = ilk_plane_wm_max(dev, level, config, ddb_partitioning, true);
2181 max->cur = ilk_cursor_wm_max(dev, level, config);
Ville Syrjälä4e975082014-03-07 18:32:11 +02002182 max->fbc = ilk_fbc_wm_reg_max(dev);
Ville Syrjälä158ae642013-08-07 13:28:19 +03002183}
2184
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002185static void ilk_compute_wm_reg_maximums(struct drm_device *dev,
2186 int level,
2187 struct ilk_wm_maximums *max)
2188{
2189 max->pri = ilk_plane_wm_reg_max(dev, level, false);
2190 max->spr = ilk_plane_wm_reg_max(dev, level, true);
2191 max->cur = ilk_cursor_wm_reg_max(dev, level);
2192 max->fbc = ilk_fbc_wm_reg_max(dev);
2193}
2194
Ville Syrjäläd9395652013-10-09 19:18:10 +03002195static bool ilk_validate_wm_level(int level,
Imre Deak820c1982013-12-17 14:46:36 +02002196 const struct ilk_wm_maximums *max,
Ville Syrjäläd9395652013-10-09 19:18:10 +03002197 struct intel_wm_level *result)
Ville Syrjäläa9786a12013-08-07 13:24:47 +03002198{
2199 bool ret;
2200
2201 /* already determined to be invalid? */
2202 if (!result->enable)
2203 return false;
2204
2205 result->enable = result->pri_val <= max->pri &&
2206 result->spr_val <= max->spr &&
2207 result->cur_val <= max->cur;
2208
2209 ret = result->enable;
2210
2211 /*
2212 * HACK until we can pre-compute everything,
2213 * and thus fail gracefully if LP0 watermarks
2214 * are exceeded...
2215 */
2216 if (level == 0 && !result->enable) {
2217 if (result->pri_val > max->pri)
2218 DRM_DEBUG_KMS("Primary WM%d too large %u (max %u)\n",
2219 level, result->pri_val, max->pri);
2220 if (result->spr_val > max->spr)
2221 DRM_DEBUG_KMS("Sprite WM%d too large %u (max %u)\n",
2222 level, result->spr_val, max->spr);
2223 if (result->cur_val > max->cur)
2224 DRM_DEBUG_KMS("Cursor WM%d too large %u (max %u)\n",
2225 level, result->cur_val, max->cur);
2226
2227 result->pri_val = min_t(uint32_t, result->pri_val, max->pri);
2228 result->spr_val = min_t(uint32_t, result->spr_val, max->spr);
2229 result->cur_val = min_t(uint32_t, result->cur_val, max->cur);
2230 result->enable = true;
2231 }
2232
Ville Syrjäläa9786a12013-08-07 13:24:47 +03002233 return ret;
2234}
2235
Damien Lespiaud34ff9c2014-01-06 19:17:23 +00002236static void ilk_compute_wm_level(const struct drm_i915_private *dev_priv,
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03002237 int level,
Imre Deak820c1982013-12-17 14:46:36 +02002238 const struct ilk_pipe_wm_parameters *p,
Ville Syrjälä1fd527c2013-08-06 22:24:05 +03002239 struct intel_wm_level *result)
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03002240{
2241 uint16_t pri_latency = dev_priv->wm.pri_latency[level];
2242 uint16_t spr_latency = dev_priv->wm.spr_latency[level];
2243 uint16_t cur_latency = dev_priv->wm.cur_latency[level];
2244
2245 /* WM1+ latency values stored in 0.5us units */
2246 if (level > 0) {
2247 pri_latency *= 5;
2248 spr_latency *= 5;
2249 cur_latency *= 5;
2250 }
2251
2252 result->pri_val = ilk_compute_pri_wm(p, pri_latency, level);
2253 result->spr_val = ilk_compute_spr_wm(p, spr_latency);
2254 result->cur_val = ilk_compute_cur_wm(p, cur_latency);
2255 result->fbc_val = ilk_compute_fbc_wm(p, result->pri_val);
2256 result->enable = true;
2257}
2258
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002259static uint32_t
2260hsw_compute_linetime_wm(struct drm_device *dev, struct drm_crtc *crtc)
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002261{
2262 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002263 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002264 struct drm_display_mode *mode = &intel_crtc->config.adjusted_mode;
Paulo Zanoni85a02de2013-05-03 17:23:43 -03002265 u32 linetime, ips_linetime;
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002266
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002267 if (!intel_crtc_active(crtc))
2268 return 0;
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002269
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002270 /* The WM are computed with base on how long it takes to fill a single
2271 * row at the given clock rate, multiplied by 8.
2272 * */
Jesse Barnesfec8cba2013-11-27 11:10:26 -08002273 linetime = DIV_ROUND_CLOSEST(mode->crtc_htotal * 1000 * 8,
2274 mode->crtc_clock);
2275 ips_linetime = DIV_ROUND_CLOSEST(mode->crtc_htotal * 1000 * 8,
Paulo Zanoni85a02de2013-05-03 17:23:43 -03002276 intel_ddi_get_cdclk_freq(dev_priv));
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002277
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002278 return PIPE_WM_LINETIME_IPS_LINETIME(ips_linetime) |
2279 PIPE_WM_LINETIME_TIME(linetime);
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002280}
2281
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002282static void intel_read_wm_latency(struct drm_device *dev, uint16_t wm[8])
Ville Syrjälä12b134d2013-07-05 11:57:21 +03002283{
2284 struct drm_i915_private *dev_priv = dev->dev_private;
2285
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002286 if (IS_GEN9(dev)) {
2287 uint32_t val;
Vandana Kannan4f947382014-11-04 17:06:47 +00002288 int ret, i;
Vandana Kannan367294b2014-11-04 17:06:46 +00002289 int level, max_level = ilk_wm_max_level(dev);
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002290
2291 /* read the first set of memory latencies[0:3] */
2292 val = 0; /* data0 to be programmed to 0 for first set */
2293 mutex_lock(&dev_priv->rps.hw_lock);
2294 ret = sandybridge_pcode_read(dev_priv,
2295 GEN9_PCODE_READ_MEM_LATENCY,
2296 &val);
2297 mutex_unlock(&dev_priv->rps.hw_lock);
2298
2299 if (ret) {
2300 DRM_ERROR("SKL Mailbox read error = %d\n", ret);
2301 return;
2302 }
2303
2304 wm[0] = val & GEN9_MEM_LATENCY_LEVEL_MASK;
2305 wm[1] = (val >> GEN9_MEM_LATENCY_LEVEL_1_5_SHIFT) &
2306 GEN9_MEM_LATENCY_LEVEL_MASK;
2307 wm[2] = (val >> GEN9_MEM_LATENCY_LEVEL_2_6_SHIFT) &
2308 GEN9_MEM_LATENCY_LEVEL_MASK;
2309 wm[3] = (val >> GEN9_MEM_LATENCY_LEVEL_3_7_SHIFT) &
2310 GEN9_MEM_LATENCY_LEVEL_MASK;
2311
2312 /* read the second set of memory latencies[4:7] */
2313 val = 1; /* data0 to be programmed to 1 for second set */
2314 mutex_lock(&dev_priv->rps.hw_lock);
2315 ret = sandybridge_pcode_read(dev_priv,
2316 GEN9_PCODE_READ_MEM_LATENCY,
2317 &val);
2318 mutex_unlock(&dev_priv->rps.hw_lock);
2319 if (ret) {
2320 DRM_ERROR("SKL Mailbox read error = %d\n", ret);
2321 return;
2322 }
2323
2324 wm[4] = val & GEN9_MEM_LATENCY_LEVEL_MASK;
2325 wm[5] = (val >> GEN9_MEM_LATENCY_LEVEL_1_5_SHIFT) &
2326 GEN9_MEM_LATENCY_LEVEL_MASK;
2327 wm[6] = (val >> GEN9_MEM_LATENCY_LEVEL_2_6_SHIFT) &
2328 GEN9_MEM_LATENCY_LEVEL_MASK;
2329 wm[7] = (val >> GEN9_MEM_LATENCY_LEVEL_3_7_SHIFT) &
2330 GEN9_MEM_LATENCY_LEVEL_MASK;
2331
Vandana Kannan367294b2014-11-04 17:06:46 +00002332 /*
2333 * punit doesn't take into account the read latency so we need
2334 * to add 2us to the various latency levels we retrieve from
2335 * the punit.
2336 * - W0 is a bit special in that it's the only level that
2337 * can't be disabled if we want to have display working, so
2338 * we always add 2us there.
2339 * - For levels >=1, punit returns 0us latency when they are
2340 * disabled, so we respect that and don't add 2us then
Vandana Kannan4f947382014-11-04 17:06:47 +00002341 *
2342 * Additionally, if a level n (n > 1) has a 0us latency, all
2343 * levels m (m >= n) need to be disabled. We make sure to
2344 * sanitize the values out of the punit to satisfy this
2345 * requirement.
Vandana Kannan367294b2014-11-04 17:06:46 +00002346 */
2347 wm[0] += 2;
2348 for (level = 1; level <= max_level; level++)
2349 if (wm[level] != 0)
2350 wm[level] += 2;
Vandana Kannan4f947382014-11-04 17:06:47 +00002351 else {
2352 for (i = level + 1; i <= max_level; i++)
2353 wm[i] = 0;
Vandana Kannan367294b2014-11-04 17:06:46 +00002354
Vandana Kannan4f947382014-11-04 17:06:47 +00002355 break;
2356 }
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002357 } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Ville Syrjälä12b134d2013-07-05 11:57:21 +03002358 uint64_t sskpd = I915_READ64(MCH_SSKPD);
2359
2360 wm[0] = (sskpd >> 56) & 0xFF;
2361 if (wm[0] == 0)
2362 wm[0] = sskpd & 0xF;
Ville Syrjäläe5d50192013-07-05 11:57:22 +03002363 wm[1] = (sskpd >> 4) & 0xFF;
2364 wm[2] = (sskpd >> 12) & 0xFF;
2365 wm[3] = (sskpd >> 20) & 0x1FF;
2366 wm[4] = (sskpd >> 32) & 0x1FF;
Ville Syrjälä63cf9a12013-07-05 11:57:23 +03002367 } else if (INTEL_INFO(dev)->gen >= 6) {
2368 uint32_t sskpd = I915_READ(MCH_SSKPD);
2369
2370 wm[0] = (sskpd >> SSKPD_WM0_SHIFT) & SSKPD_WM_MASK;
2371 wm[1] = (sskpd >> SSKPD_WM1_SHIFT) & SSKPD_WM_MASK;
2372 wm[2] = (sskpd >> SSKPD_WM2_SHIFT) & SSKPD_WM_MASK;
2373 wm[3] = (sskpd >> SSKPD_WM3_SHIFT) & SSKPD_WM_MASK;
Ville Syrjälä3a88d0a2013-08-01 16:18:49 +03002374 } else if (INTEL_INFO(dev)->gen >= 5) {
2375 uint32_t mltr = I915_READ(MLTR_ILK);
2376
2377 /* ILK primary LP0 latency is 700 ns */
2378 wm[0] = 7;
2379 wm[1] = (mltr >> MLTR_WM1_SHIFT) & ILK_SRLT_MASK;
2380 wm[2] = (mltr >> MLTR_WM2_SHIFT) & ILK_SRLT_MASK;
Ville Syrjälä12b134d2013-07-05 11:57:21 +03002381 }
2382}
2383
Ville Syrjälä53615a52013-08-01 16:18:50 +03002384static void intel_fixup_spr_wm_latency(struct drm_device *dev, uint16_t wm[5])
2385{
2386 /* ILK sprite LP0 latency is 1300 ns */
2387 if (INTEL_INFO(dev)->gen == 5)
2388 wm[0] = 13;
2389}
2390
2391static void intel_fixup_cur_wm_latency(struct drm_device *dev, uint16_t wm[5])
2392{
2393 /* ILK cursor LP0 latency is 1300 ns */
2394 if (INTEL_INFO(dev)->gen == 5)
2395 wm[0] = 13;
2396
2397 /* WaDoubleCursorLP3Latency:ivb */
2398 if (IS_IVYBRIDGE(dev))
2399 wm[3] *= 2;
2400}
2401
Damien Lespiau546c81f2014-05-13 15:30:26 +01002402int ilk_wm_max_level(const struct drm_device *dev)
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03002403{
2404 /* how many WM levels are we expecting */
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002405 if (IS_GEN9(dev))
2406 return 7;
2407 else if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03002408 return 4;
2409 else if (INTEL_INFO(dev)->gen >= 6)
2410 return 3;
2411 else
2412 return 2;
2413}
Daniel Vetter7526ed72014-09-29 15:07:19 +02002414
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002415static void intel_print_wm_latency(struct drm_device *dev,
2416 const char *name,
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002417 const uint16_t wm[8])
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002418{
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03002419 int level, max_level = ilk_wm_max_level(dev);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002420
2421 for (level = 0; level <= max_level; level++) {
2422 unsigned int latency = wm[level];
2423
2424 if (latency == 0) {
2425 DRM_ERROR("%s WM%d latency not provided\n",
2426 name, level);
2427 continue;
2428 }
2429
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002430 /*
2431 * - latencies are in us on gen9.
2432 * - before then, WM1+ latency values are in 0.5us units
2433 */
2434 if (IS_GEN9(dev))
2435 latency *= 10;
2436 else if (level > 0)
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002437 latency *= 5;
2438
2439 DRM_DEBUG_KMS("%s WM%d latency %u (%u.%u usec)\n",
2440 name, level, wm[level],
2441 latency / 10, latency % 10);
2442 }
2443}
2444
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002445static bool ilk_increase_wm_latency(struct drm_i915_private *dev_priv,
2446 uint16_t wm[5], uint16_t min)
2447{
2448 int level, max_level = ilk_wm_max_level(dev_priv->dev);
2449
2450 if (wm[0] >= min)
2451 return false;
2452
2453 wm[0] = max(wm[0], min);
2454 for (level = 1; level <= max_level; level++)
2455 wm[level] = max_t(uint16_t, wm[level], DIV_ROUND_UP(min, 5));
2456
2457 return true;
2458}
2459
2460static void snb_wm_latency_quirk(struct drm_device *dev)
2461{
2462 struct drm_i915_private *dev_priv = dev->dev_private;
2463 bool changed;
2464
2465 /*
2466 * The BIOS provided WM memory latency values are often
2467 * inadequate for high resolution displays. Adjust them.
2468 */
2469 changed = ilk_increase_wm_latency(dev_priv, dev_priv->wm.pri_latency, 12) |
2470 ilk_increase_wm_latency(dev_priv, dev_priv->wm.spr_latency, 12) |
2471 ilk_increase_wm_latency(dev_priv, dev_priv->wm.cur_latency, 12);
2472
2473 if (!changed)
2474 return;
2475
2476 DRM_DEBUG_KMS("WM latency values increased to avoid potential underruns\n");
2477 intel_print_wm_latency(dev, "Primary", dev_priv->wm.pri_latency);
2478 intel_print_wm_latency(dev, "Sprite", dev_priv->wm.spr_latency);
2479 intel_print_wm_latency(dev, "Cursor", dev_priv->wm.cur_latency);
2480}
2481
Damien Lespiaufa50ad62014-03-17 18:01:16 +00002482static void ilk_setup_wm_latency(struct drm_device *dev)
Ville Syrjälä53615a52013-08-01 16:18:50 +03002483{
2484 struct drm_i915_private *dev_priv = dev->dev_private;
2485
2486 intel_read_wm_latency(dev, dev_priv->wm.pri_latency);
2487
2488 memcpy(dev_priv->wm.spr_latency, dev_priv->wm.pri_latency,
2489 sizeof(dev_priv->wm.pri_latency));
2490 memcpy(dev_priv->wm.cur_latency, dev_priv->wm.pri_latency,
2491 sizeof(dev_priv->wm.pri_latency));
2492
2493 intel_fixup_spr_wm_latency(dev, dev_priv->wm.spr_latency);
2494 intel_fixup_cur_wm_latency(dev, dev_priv->wm.cur_latency);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002495
2496 intel_print_wm_latency(dev, "Primary", dev_priv->wm.pri_latency);
2497 intel_print_wm_latency(dev, "Sprite", dev_priv->wm.spr_latency);
2498 intel_print_wm_latency(dev, "Cursor", dev_priv->wm.cur_latency);
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002499
2500 if (IS_GEN6(dev))
2501 snb_wm_latency_quirk(dev);
Ville Syrjälä53615a52013-08-01 16:18:50 +03002502}
2503
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002504static void skl_setup_wm_latency(struct drm_device *dev)
2505{
2506 struct drm_i915_private *dev_priv = dev->dev_private;
2507
2508 intel_read_wm_latency(dev, dev_priv->wm.skl_latency);
2509 intel_print_wm_latency(dev, "Gen9 Plane", dev_priv->wm.skl_latency);
2510}
2511
Imre Deak820c1982013-12-17 14:46:36 +02002512static void ilk_compute_wm_parameters(struct drm_crtc *crtc,
Ville Syrjälä2a44b762014-03-07 18:32:09 +02002513 struct ilk_pipe_wm_parameters *p)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002514{
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03002515 struct drm_device *dev = crtc->dev;
2516 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2517 enum pipe pipe = intel_crtc->pipe;
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03002518 struct drm_plane *plane;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002519
Ville Syrjälä2a44b762014-03-07 18:32:09 +02002520 if (!intel_crtc_active(crtc))
2521 return;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002522
Ville Syrjälä2a44b762014-03-07 18:32:09 +02002523 p->active = true;
2524 p->pipe_htotal = intel_crtc->config.adjusted_mode.crtc_htotal;
2525 p->pixel_rate = ilk_pipe_pixel_rate(dev, crtc);
2526 p->pri.bytes_per_pixel = crtc->primary->fb->bits_per_pixel / 8;
2527 p->cur.bytes_per_pixel = 4;
2528 p->pri.horiz_pixels = intel_crtc->config.pipe_src_w;
2529 p->cur.horiz_pixels = intel_crtc->cursor_width;
2530 /* TODO: for now, assume primary and cursor planes are always enabled. */
2531 p->pri.enabled = true;
2532 p->cur.enabled = true;
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03002533
Matt Roperaf2b6532014-04-01 15:22:32 -07002534 drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002535 struct intel_plane *intel_plane = to_intel_plane(plane);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002536
Ville Syrjälä2a44b762014-03-07 18:32:09 +02002537 if (intel_plane->pipe == pipe) {
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03002538 p->spr = intel_plane->wm;
Ville Syrjälä2a44b762014-03-07 18:32:09 +02002539 break;
2540 }
2541 }
2542}
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002543
Ville Syrjälä2a44b762014-03-07 18:32:09 +02002544static void ilk_compute_wm_config(struct drm_device *dev,
2545 struct intel_wm_config *config)
2546{
2547 struct intel_crtc *intel_crtc;
2548
2549 /* Compute the currently _active_ config */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01002550 for_each_intel_crtc(dev, intel_crtc) {
Ville Syrjälä2a44b762014-03-07 18:32:09 +02002551 const struct intel_pipe_wm *wm = &intel_crtc->wm.active;
2552
2553 if (!wm->pipe_enabled)
2554 continue;
2555
2556 config->sprites_enabled |= wm->sprites_enabled;
2557 config->sprites_scaled |= wm->sprites_scaled;
2558 config->num_pipes_active++;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002559 }
2560}
2561
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002562/* Compute new watermarks for the pipe */
2563static bool intel_compute_pipe_wm(struct drm_crtc *crtc,
Imre Deak820c1982013-12-17 14:46:36 +02002564 const struct ilk_pipe_wm_parameters *params,
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002565 struct intel_pipe_wm *pipe_wm)
2566{
2567 struct drm_device *dev = crtc->dev;
Damien Lespiaud34ff9c2014-01-06 19:17:23 +00002568 const struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002569 int level, max_level = ilk_wm_max_level(dev);
2570 /* LP0 watermark maximums depend on this pipe alone */
2571 struct intel_wm_config config = {
2572 .num_pipes_active = 1,
2573 .sprites_enabled = params->spr.enabled,
2574 .sprites_scaled = params->spr.scaled,
2575 };
Imre Deak820c1982013-12-17 14:46:36 +02002576 struct ilk_wm_maximums max;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002577
Ville Syrjälä2a44b762014-03-07 18:32:09 +02002578 pipe_wm->pipe_enabled = params->active;
2579 pipe_wm->sprites_enabled = params->spr.enabled;
2580 pipe_wm->sprites_scaled = params->spr.scaled;
2581
Ville Syrjälä7b39a0b2013-12-05 15:51:30 +02002582 /* ILK/SNB: LP2+ watermarks only w/o sprites */
2583 if (INTEL_INFO(dev)->gen <= 6 && params->spr.enabled)
2584 max_level = 1;
2585
2586 /* ILK/SNB/IVB: LP1+ watermarks only w/o scaling */
2587 if (params->spr.scaled)
2588 max_level = 0;
2589
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002590 ilk_compute_wm_level(dev_priv, 0, params, &pipe_wm->wm[0]);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002591
Ville Syrjäläa42a5712014-01-07 16:14:08 +02002592 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Ville Syrjäläce0e0712013-12-05 15:51:36 +02002593 pipe_wm->linetime = hsw_compute_linetime_wm(dev, crtc);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002594
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002595 /* LP0 watermarks always use 1/2 DDB partitioning */
2596 ilk_compute_wm_maximums(dev, 0, &config, INTEL_DDB_PART_1_2, &max);
2597
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002598 /* At least LP0 must be valid */
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002599 if (!ilk_validate_wm_level(0, &max, &pipe_wm->wm[0]))
2600 return false;
2601
2602 ilk_compute_wm_reg_maximums(dev, 1, &max);
2603
2604 for (level = 1; level <= max_level; level++) {
2605 struct intel_wm_level wm = {};
2606
2607 ilk_compute_wm_level(dev_priv, level, params, &wm);
2608
2609 /*
2610 * Disable any watermark level that exceeds the
2611 * register maximums since such watermarks are
2612 * always invalid.
2613 */
2614 if (!ilk_validate_wm_level(level, &max, &wm))
2615 break;
2616
2617 pipe_wm->wm[level] = wm;
2618 }
2619
2620 return true;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002621}
2622
2623/*
2624 * Merge the watermarks from all active pipes for a specific level.
2625 */
2626static void ilk_merge_wm_level(struct drm_device *dev,
2627 int level,
2628 struct intel_wm_level *ret_wm)
2629{
2630 const struct intel_crtc *intel_crtc;
2631
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002632 ret_wm->enable = true;
2633
Damien Lespiaud3fcc802014-05-13 23:32:22 +01002634 for_each_intel_crtc(dev, intel_crtc) {
Ville Syrjäläfe392ef2014-03-07 18:32:10 +02002635 const struct intel_pipe_wm *active = &intel_crtc->wm.active;
2636 const struct intel_wm_level *wm = &active->wm[level];
2637
2638 if (!active->pipe_enabled)
2639 continue;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002640
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002641 /*
2642 * The watermark values may have been used in the past,
2643 * so we must maintain them in the registers for some
2644 * time even if the level is now disabled.
2645 */
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002646 if (!wm->enable)
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002647 ret_wm->enable = false;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002648
2649 ret_wm->pri_val = max(ret_wm->pri_val, wm->pri_val);
2650 ret_wm->spr_val = max(ret_wm->spr_val, wm->spr_val);
2651 ret_wm->cur_val = max(ret_wm->cur_val, wm->cur_val);
2652 ret_wm->fbc_val = max(ret_wm->fbc_val, wm->fbc_val);
2653 }
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002654}
2655
2656/*
2657 * Merge all low power watermarks for all active pipes.
2658 */
2659static void ilk_wm_merge(struct drm_device *dev,
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02002660 const struct intel_wm_config *config,
Imre Deak820c1982013-12-17 14:46:36 +02002661 const struct ilk_wm_maximums *max,
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002662 struct intel_pipe_wm *merged)
2663{
2664 int level, max_level = ilk_wm_max_level(dev);
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002665 int last_enabled_level = max_level;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002666
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02002667 /* ILK/SNB/IVB: LP1+ watermarks only w/ single pipe */
2668 if ((INTEL_INFO(dev)->gen <= 6 || IS_IVYBRIDGE(dev)) &&
2669 config->num_pipes_active > 1)
2670 return;
2671
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02002672 /* ILK: FBC WM must be disabled always */
2673 merged->fbc_wm_enabled = INTEL_INFO(dev)->gen >= 6;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002674
2675 /* merge each WM1+ level */
2676 for (level = 1; level <= max_level; level++) {
2677 struct intel_wm_level *wm = &merged->wm[level];
2678
2679 ilk_merge_wm_level(dev, level, wm);
2680
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002681 if (level > last_enabled_level)
2682 wm->enable = false;
2683 else if (!ilk_validate_wm_level(level, max, wm))
2684 /* make sure all following levels get disabled */
2685 last_enabled_level = level - 1;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002686
2687 /*
2688 * The spec says it is preferred to disable
2689 * FBC WMs instead of disabling a WM level.
2690 */
2691 if (wm->fbc_val > max->fbc) {
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002692 if (wm->enable)
2693 merged->fbc_wm_enabled = false;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002694 wm->fbc_val = 0;
2695 }
2696 }
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02002697
2698 /* ILK: LP2+ must be disabled when FBC WM is disabled but FBC enabled */
2699 /*
2700 * FIXME this is racy. FBC might get enabled later.
2701 * What we should check here is whether FBC can be
2702 * enabled sometime later.
2703 */
2704 if (IS_GEN5(dev) && !merged->fbc_wm_enabled && intel_fbc_enabled(dev)) {
2705 for (level = 2; level <= max_level; level++) {
2706 struct intel_wm_level *wm = &merged->wm[level];
2707
2708 wm->enable = false;
2709 }
2710 }
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002711}
2712
Ville Syrjäläb380ca32013-10-09 19:18:01 +03002713static int ilk_wm_lp_to_level(int wm_lp, const struct intel_pipe_wm *pipe_wm)
2714{
2715 /* LP1,LP2,LP3 levels are either 1,2,3 or 1,3,4 */
2716 return wm_lp + (wm_lp >= 2 && pipe_wm->wm[4].enable);
2717}
2718
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02002719/* The value we need to program into the WM_LPx latency field */
2720static unsigned int ilk_wm_lp_latency(struct drm_device *dev, int level)
2721{
2722 struct drm_i915_private *dev_priv = dev->dev_private;
2723
Ville Syrjäläa42a5712014-01-07 16:14:08 +02002724 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02002725 return 2 * level;
2726 else
2727 return dev_priv->wm.pri_latency[level];
2728}
2729
Imre Deak820c1982013-12-17 14:46:36 +02002730static void ilk_compute_wm_results(struct drm_device *dev,
Ville Syrjälä0362c782013-10-09 19:17:57 +03002731 const struct intel_pipe_wm *merged,
Ville Syrjälä609cede2013-10-09 19:18:03 +03002732 enum intel_ddb_partitioning partitioning,
Imre Deak820c1982013-12-17 14:46:36 +02002733 struct ilk_wm_values *results)
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002734{
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002735 struct intel_crtc *intel_crtc;
2736 int level, wm_lp;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002737
Ville Syrjälä0362c782013-10-09 19:17:57 +03002738 results->enable_fbc_wm = merged->fbc_wm_enabled;
Ville Syrjälä609cede2013-10-09 19:18:03 +03002739 results->partitioning = partitioning;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002740
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002741 /* LP1+ register values */
Paulo Zanonicca32e92013-05-31 11:45:06 -03002742 for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
Ville Syrjälä1fd527c2013-08-06 22:24:05 +03002743 const struct intel_wm_level *r;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002744
Ville Syrjäläb380ca32013-10-09 19:18:01 +03002745 level = ilk_wm_lp_to_level(wm_lp, merged);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002746
Ville Syrjälä0362c782013-10-09 19:17:57 +03002747 r = &merged->wm[level];
Paulo Zanonicca32e92013-05-31 11:45:06 -03002748
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002749 /*
2750 * Maintain the watermark values even if the level is
2751 * disabled. Doing otherwise could cause underruns.
2752 */
2753 results->wm_lp[wm_lp - 1] =
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02002754 (ilk_wm_lp_latency(dev, level) << WM1_LP_LATENCY_SHIFT) |
Ville Syrjälä416f4722013-11-02 21:07:46 -07002755 (r->pri_val << WM1_LP_SR_SHIFT) |
2756 r->cur_val;
2757
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002758 if (r->enable)
2759 results->wm_lp[wm_lp - 1] |= WM1_LP_SR_EN;
2760
Ville Syrjälä416f4722013-11-02 21:07:46 -07002761 if (INTEL_INFO(dev)->gen >= 8)
2762 results->wm_lp[wm_lp - 1] |=
2763 r->fbc_val << WM1_LP_FBC_SHIFT_BDW;
2764 else
2765 results->wm_lp[wm_lp - 1] |=
2766 r->fbc_val << WM1_LP_FBC_SHIFT;
2767
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002768 /*
2769 * Always set WM1S_LP_EN when spr_val != 0, even if the
2770 * level is disabled. Doing otherwise could cause underruns.
2771 */
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02002772 if (INTEL_INFO(dev)->gen <= 6 && r->spr_val) {
2773 WARN_ON(wm_lp != 1);
2774 results->wm_lp_spr[wm_lp - 1] = WM1S_LP_EN | r->spr_val;
2775 } else
2776 results->wm_lp_spr[wm_lp - 1] = r->spr_val;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002777 }
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002778
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002779 /* LP0 register values */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01002780 for_each_intel_crtc(dev, intel_crtc) {
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002781 enum pipe pipe = intel_crtc->pipe;
2782 const struct intel_wm_level *r =
2783 &intel_crtc->wm.active.wm[0];
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002784
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002785 if (WARN_ON(!r->enable))
2786 continue;
2787
2788 results->wm_linetime[pipe] = intel_crtc->wm.active.linetime;
2789
2790 results->wm_pipe[pipe] =
2791 (r->pri_val << WM0_PIPE_PLANE_SHIFT) |
2792 (r->spr_val << WM0_PIPE_SPRITE_SHIFT) |
2793 r->cur_val;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002794 }
2795}
2796
Paulo Zanoni861f3382013-05-31 10:19:21 -03002797/* Find the result with the highest level enabled. Check for enable_fbc_wm in
2798 * case both are at the same level. Prefer r1 in case they're the same. */
Imre Deak820c1982013-12-17 14:46:36 +02002799static struct intel_pipe_wm *ilk_find_best_result(struct drm_device *dev,
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002800 struct intel_pipe_wm *r1,
2801 struct intel_pipe_wm *r2)
Paulo Zanoni861f3382013-05-31 10:19:21 -03002802{
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002803 int level, max_level = ilk_wm_max_level(dev);
2804 int level1 = 0, level2 = 0;
Paulo Zanoni861f3382013-05-31 10:19:21 -03002805
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002806 for (level = 1; level <= max_level; level++) {
2807 if (r1->wm[level].enable)
2808 level1 = level;
2809 if (r2->wm[level].enable)
2810 level2 = level;
Paulo Zanoni861f3382013-05-31 10:19:21 -03002811 }
2812
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002813 if (level1 == level2) {
2814 if (r2->fbc_wm_enabled && !r1->fbc_wm_enabled)
Paulo Zanoni861f3382013-05-31 10:19:21 -03002815 return r2;
2816 else
2817 return r1;
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002818 } else if (level1 > level2) {
Paulo Zanoni861f3382013-05-31 10:19:21 -03002819 return r1;
2820 } else {
2821 return r2;
2822 }
2823}
2824
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002825/* dirty bits used to track which watermarks need changes */
2826#define WM_DIRTY_PIPE(pipe) (1 << (pipe))
2827#define WM_DIRTY_LINETIME(pipe) (1 << (8 + (pipe)))
2828#define WM_DIRTY_LP(wm_lp) (1 << (15 + (wm_lp)))
2829#define WM_DIRTY_LP_ALL (WM_DIRTY_LP(1) | WM_DIRTY_LP(2) | WM_DIRTY_LP(3))
2830#define WM_DIRTY_FBC (1 << 24)
2831#define WM_DIRTY_DDB (1 << 25)
2832
Damien Lespiau055e3932014-08-18 13:49:10 +01002833static unsigned int ilk_compute_wm_dirty(struct drm_i915_private *dev_priv,
Imre Deak820c1982013-12-17 14:46:36 +02002834 const struct ilk_wm_values *old,
2835 const struct ilk_wm_values *new)
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002836{
2837 unsigned int dirty = 0;
2838 enum pipe pipe;
2839 int wm_lp;
2840
Damien Lespiau055e3932014-08-18 13:49:10 +01002841 for_each_pipe(dev_priv, pipe) {
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002842 if (old->wm_linetime[pipe] != new->wm_linetime[pipe]) {
2843 dirty |= WM_DIRTY_LINETIME(pipe);
2844 /* Must disable LP1+ watermarks too */
2845 dirty |= WM_DIRTY_LP_ALL;
2846 }
2847
2848 if (old->wm_pipe[pipe] != new->wm_pipe[pipe]) {
2849 dirty |= WM_DIRTY_PIPE(pipe);
2850 /* Must disable LP1+ watermarks too */
2851 dirty |= WM_DIRTY_LP_ALL;
2852 }
2853 }
2854
2855 if (old->enable_fbc_wm != new->enable_fbc_wm) {
2856 dirty |= WM_DIRTY_FBC;
2857 /* Must disable LP1+ watermarks too */
2858 dirty |= WM_DIRTY_LP_ALL;
2859 }
2860
2861 if (old->partitioning != new->partitioning) {
2862 dirty |= WM_DIRTY_DDB;
2863 /* Must disable LP1+ watermarks too */
2864 dirty |= WM_DIRTY_LP_ALL;
2865 }
2866
2867 /* LP1+ watermarks already deemed dirty, no need to continue */
2868 if (dirty & WM_DIRTY_LP_ALL)
2869 return dirty;
2870
2871 /* Find the lowest numbered LP1+ watermark in need of an update... */
2872 for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
2873 if (old->wm_lp[wm_lp - 1] != new->wm_lp[wm_lp - 1] ||
2874 old->wm_lp_spr[wm_lp - 1] != new->wm_lp_spr[wm_lp - 1])
2875 break;
2876 }
2877
2878 /* ...and mark it and all higher numbered LP1+ watermarks as dirty */
2879 for (; wm_lp <= 3; wm_lp++)
2880 dirty |= WM_DIRTY_LP(wm_lp);
2881
2882 return dirty;
2883}
2884
Ville Syrjälä8553c182013-12-05 15:51:39 +02002885static bool _ilk_disable_lp_wm(struct drm_i915_private *dev_priv,
2886 unsigned int dirty)
2887{
Imre Deak820c1982013-12-17 14:46:36 +02002888 struct ilk_wm_values *previous = &dev_priv->wm.hw;
Ville Syrjälä8553c182013-12-05 15:51:39 +02002889 bool changed = false;
2890
2891 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] & WM1_LP_SR_EN) {
2892 previous->wm_lp[2] &= ~WM1_LP_SR_EN;
2893 I915_WRITE(WM3_LP_ILK, previous->wm_lp[2]);
2894 changed = true;
2895 }
2896 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] & WM1_LP_SR_EN) {
2897 previous->wm_lp[1] &= ~WM1_LP_SR_EN;
2898 I915_WRITE(WM2_LP_ILK, previous->wm_lp[1]);
2899 changed = true;
2900 }
2901 if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] & WM1_LP_SR_EN) {
2902 previous->wm_lp[0] &= ~WM1_LP_SR_EN;
2903 I915_WRITE(WM1_LP_ILK, previous->wm_lp[0]);
2904 changed = true;
2905 }
2906
2907 /*
2908 * Don't touch WM1S_LP_EN here.
2909 * Doing so could cause underruns.
2910 */
2911
2912 return changed;
2913}
2914
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002915/*
2916 * The spec says we shouldn't write when we don't need, because every write
2917 * causes WMs to be re-evaluated, expending some power.
2918 */
Imre Deak820c1982013-12-17 14:46:36 +02002919static void ilk_write_wm_values(struct drm_i915_private *dev_priv,
2920 struct ilk_wm_values *results)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002921{
Ville Syrjäläac9545f2013-12-05 15:51:28 +02002922 struct drm_device *dev = dev_priv->dev;
Imre Deak820c1982013-12-17 14:46:36 +02002923 struct ilk_wm_values *previous = &dev_priv->wm.hw;
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002924 unsigned int dirty;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002925 uint32_t val;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002926
Damien Lespiau055e3932014-08-18 13:49:10 +01002927 dirty = ilk_compute_wm_dirty(dev_priv, previous, results);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002928 if (!dirty)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002929 return;
2930
Ville Syrjälä8553c182013-12-05 15:51:39 +02002931 _ilk_disable_lp_wm(dev_priv, dirty);
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02002932
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002933 if (dirty & WM_DIRTY_PIPE(PIPE_A))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002934 I915_WRITE(WM0_PIPEA_ILK, results->wm_pipe[0]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002935 if (dirty & WM_DIRTY_PIPE(PIPE_B))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002936 I915_WRITE(WM0_PIPEB_ILK, results->wm_pipe[1]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002937 if (dirty & WM_DIRTY_PIPE(PIPE_C))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002938 I915_WRITE(WM0_PIPEC_IVB, results->wm_pipe[2]);
2939
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002940 if (dirty & WM_DIRTY_LINETIME(PIPE_A))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002941 I915_WRITE(PIPE_WM_LINETIME(PIPE_A), results->wm_linetime[0]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002942 if (dirty & WM_DIRTY_LINETIME(PIPE_B))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002943 I915_WRITE(PIPE_WM_LINETIME(PIPE_B), results->wm_linetime[1]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002944 if (dirty & WM_DIRTY_LINETIME(PIPE_C))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002945 I915_WRITE(PIPE_WM_LINETIME(PIPE_C), results->wm_linetime[2]);
2946
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002947 if (dirty & WM_DIRTY_DDB) {
Ville Syrjäläa42a5712014-01-07 16:14:08 +02002948 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Ville Syrjäläac9545f2013-12-05 15:51:28 +02002949 val = I915_READ(WM_MISC);
2950 if (results->partitioning == INTEL_DDB_PART_1_2)
2951 val &= ~WM_MISC_DATA_PARTITION_5_6;
2952 else
2953 val |= WM_MISC_DATA_PARTITION_5_6;
2954 I915_WRITE(WM_MISC, val);
2955 } else {
2956 val = I915_READ(DISP_ARB_CTL2);
2957 if (results->partitioning == INTEL_DDB_PART_1_2)
2958 val &= ~DISP_DATA_PARTITION_5_6;
2959 else
2960 val |= DISP_DATA_PARTITION_5_6;
2961 I915_WRITE(DISP_ARB_CTL2, val);
2962 }
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002963 }
2964
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002965 if (dirty & WM_DIRTY_FBC) {
Paulo Zanonicca32e92013-05-31 11:45:06 -03002966 val = I915_READ(DISP_ARB_CTL);
2967 if (results->enable_fbc_wm)
2968 val &= ~DISP_FBC_WM_DIS;
2969 else
2970 val |= DISP_FBC_WM_DIS;
2971 I915_WRITE(DISP_ARB_CTL, val);
2972 }
2973
Imre Deak954911e2013-12-17 14:46:34 +02002974 if (dirty & WM_DIRTY_LP(1) &&
2975 previous->wm_lp_spr[0] != results->wm_lp_spr[0])
2976 I915_WRITE(WM1S_LP_ILK, results->wm_lp_spr[0]);
2977
2978 if (INTEL_INFO(dev)->gen >= 7) {
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02002979 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp_spr[1] != results->wm_lp_spr[1])
2980 I915_WRITE(WM2S_LP_IVB, results->wm_lp_spr[1]);
2981 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp_spr[2] != results->wm_lp_spr[2])
2982 I915_WRITE(WM3S_LP_IVB, results->wm_lp_spr[2]);
2983 }
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002984
Ville Syrjäläfacd6192013-12-05 15:51:33 +02002985 if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] != results->wm_lp[0])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002986 I915_WRITE(WM1_LP_ILK, results->wm_lp[0]);
Ville Syrjäläfacd6192013-12-05 15:51:33 +02002987 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] != results->wm_lp[1])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002988 I915_WRITE(WM2_LP_ILK, results->wm_lp[1]);
Ville Syrjäläfacd6192013-12-05 15:51:33 +02002989 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] != results->wm_lp[2])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002990 I915_WRITE(WM3_LP_ILK, results->wm_lp[2]);
Ville Syrjälä609cede2013-10-09 19:18:03 +03002991
2992 dev_priv->wm.hw = *results;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002993}
2994
Ville Syrjälä8553c182013-12-05 15:51:39 +02002995static bool ilk_disable_lp_wm(struct drm_device *dev)
2996{
2997 struct drm_i915_private *dev_priv = dev->dev_private;
2998
2999 return _ilk_disable_lp_wm(dev_priv, WM_DIRTY_LP_ALL);
3000}
3001
Damien Lespiaub9cec072014-11-04 17:06:43 +00003002/*
3003 * On gen9, we need to allocate Display Data Buffer (DDB) portions to the
3004 * different active planes.
3005 */
3006
3007#define SKL_DDB_SIZE 896 /* in blocks */
3008
3009static void
3010skl_ddb_get_pipe_allocation_limits(struct drm_device *dev,
3011 struct drm_crtc *for_crtc,
3012 const struct intel_wm_config *config,
3013 const struct skl_pipe_wm_parameters *params,
3014 struct skl_ddb_entry *alloc /* out */)
3015{
3016 struct drm_crtc *crtc;
3017 unsigned int pipe_size, ddb_size;
3018 int nth_active_pipe;
3019
3020 if (!params->active) {
3021 alloc->start = 0;
3022 alloc->end = 0;
3023 return;
3024 }
3025
3026 ddb_size = SKL_DDB_SIZE;
3027
3028 ddb_size -= 4; /* 4 blocks for bypass path allocation */
3029
3030 nth_active_pipe = 0;
3031 for_each_crtc(dev, crtc) {
3032 if (!intel_crtc_active(crtc))
3033 continue;
3034
3035 if (crtc == for_crtc)
3036 break;
3037
3038 nth_active_pipe++;
3039 }
3040
3041 pipe_size = ddb_size / config->num_pipes_active;
3042 alloc->start = nth_active_pipe * ddb_size / config->num_pipes_active;
Damien Lespiau16160e32014-11-04 17:06:53 +00003043 alloc->end = alloc->start + pipe_size;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003044}
3045
3046static unsigned int skl_cursor_allocation(const struct intel_wm_config *config)
3047{
3048 if (config->num_pipes_active == 1)
3049 return 32;
3050
3051 return 8;
3052}
3053
Damien Lespiaua269c582014-11-04 17:06:49 +00003054static void skl_ddb_entry_init_from_hw(struct skl_ddb_entry *entry, u32 reg)
3055{
3056 entry->start = reg & 0x3ff;
3057 entry->end = (reg >> 16) & 0x3ff;
Damien Lespiau16160e32014-11-04 17:06:53 +00003058 if (entry->end)
3059 entry->end += 1;
Damien Lespiaua269c582014-11-04 17:06:49 +00003060}
3061
Damien Lespiau08db6652014-11-04 17:06:52 +00003062void skl_ddb_get_hw_state(struct drm_i915_private *dev_priv,
3063 struct skl_ddb_allocation *ddb /* out */)
Damien Lespiaua269c582014-11-04 17:06:49 +00003064{
3065 struct drm_device *dev = dev_priv->dev;
3066 enum pipe pipe;
3067 int plane;
3068 u32 val;
3069
3070 for_each_pipe(dev_priv, pipe) {
3071 for_each_plane(pipe, plane) {
3072 val = I915_READ(PLANE_BUF_CFG(pipe, plane));
3073 skl_ddb_entry_init_from_hw(&ddb->plane[pipe][plane],
3074 val);
3075 }
3076
3077 val = I915_READ(CUR_BUF_CFG(pipe));
3078 skl_ddb_entry_init_from_hw(&ddb->cursor[pipe], val);
3079 }
3080}
3081
Damien Lespiaub9cec072014-11-04 17:06:43 +00003082static unsigned int
3083skl_plane_relative_data_rate(const struct intel_plane_wm_parameters *p)
3084{
3085 return p->horiz_pixels * p->vert_pixels * p->bytes_per_pixel;
3086}
3087
3088/*
3089 * We don't overflow 32 bits. Worst case is 3 planes enabled, each fetching
3090 * a 8192x4096@32bpp framebuffer:
3091 * 3 * 4096 * 8192 * 4 < 2^32
3092 */
3093static unsigned int
3094skl_get_total_relative_data_rate(struct intel_crtc *intel_crtc,
3095 const struct skl_pipe_wm_parameters *params)
3096{
3097 unsigned int total_data_rate = 0;
3098 int plane;
3099
3100 for (plane = 0; plane < intel_num_planes(intel_crtc); plane++) {
3101 const struct intel_plane_wm_parameters *p;
3102
3103 p = &params->plane[plane];
3104 if (!p->enabled)
3105 continue;
3106
3107 total_data_rate += skl_plane_relative_data_rate(p);
3108 }
3109
3110 return total_data_rate;
3111}
3112
3113static void
3114skl_allocate_pipe_ddb(struct drm_crtc *crtc,
3115 const struct intel_wm_config *config,
3116 const struct skl_pipe_wm_parameters *params,
3117 struct skl_ddb_allocation *ddb /* out */)
3118{
3119 struct drm_device *dev = crtc->dev;
3120 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3121 enum pipe pipe = intel_crtc->pipe;
Damien Lespiau34bb56a2014-11-04 17:07:01 +00003122 struct skl_ddb_entry *alloc = &ddb->pipe[pipe];
Damien Lespiaub9cec072014-11-04 17:06:43 +00003123 uint16_t alloc_size, start, cursor_blocks;
3124 unsigned int total_data_rate;
3125 int plane;
3126
Damien Lespiau34bb56a2014-11-04 17:07:01 +00003127 skl_ddb_get_pipe_allocation_limits(dev, crtc, config, params, alloc);
3128 alloc_size = skl_ddb_entry_size(alloc);
Damien Lespiaub9cec072014-11-04 17:06:43 +00003129 if (alloc_size == 0) {
3130 memset(ddb->plane[pipe], 0, sizeof(ddb->plane[pipe]));
3131 memset(&ddb->cursor[pipe], 0, sizeof(ddb->cursor[pipe]));
3132 return;
3133 }
3134
3135 cursor_blocks = skl_cursor_allocation(config);
Damien Lespiau34bb56a2014-11-04 17:07:01 +00003136 ddb->cursor[pipe].start = alloc->end - cursor_blocks;
3137 ddb->cursor[pipe].end = alloc->end;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003138
3139 alloc_size -= cursor_blocks;
Damien Lespiau34bb56a2014-11-04 17:07:01 +00003140 alloc->end -= cursor_blocks;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003141
3142 /*
3143 * Each active plane get a portion of the remaining space, in
3144 * proportion to the amount of data they need to fetch from memory.
3145 *
3146 * FIXME: we may not allocate every single block here.
3147 */
3148 total_data_rate = skl_get_total_relative_data_rate(intel_crtc, params);
3149
Damien Lespiau34bb56a2014-11-04 17:07:01 +00003150 start = alloc->start;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003151 for (plane = 0; plane < intel_num_planes(intel_crtc); plane++) {
3152 const struct intel_plane_wm_parameters *p;
3153 unsigned int data_rate;
3154 uint16_t plane_blocks;
3155
3156 p = &params->plane[plane];
3157 if (!p->enabled)
3158 continue;
3159
3160 data_rate = skl_plane_relative_data_rate(p);
3161
3162 /*
3163 * promote the expression to 64 bits to avoid overflowing, the
3164 * result is < available as data_rate / total_data_rate < 1
3165 */
3166 plane_blocks = div_u64((uint64_t)alloc_size * data_rate,
3167 total_data_rate);
3168
3169 ddb->plane[pipe][plane].start = start;
Damien Lespiau16160e32014-11-04 17:06:53 +00003170 ddb->plane[pipe][plane].end = start + plane_blocks;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003171
3172 start += plane_blocks;
3173 }
3174
3175}
3176
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003177static uint32_t skl_pipe_pixel_rate(const struct intel_crtc_config *config)
3178{
3179 /* TODO: Take into account the scalers once we support them */
3180 return config->adjusted_mode.crtc_clock;
3181}
3182
3183/*
3184 * The max latency should be 257 (max the punit can code is 255 and we add 2us
3185 * for the read latency) and bytes_per_pixel should always be <= 8, so that
3186 * should allow pixel_rate up to ~2 GHz which seems sufficient since max
3187 * 2xcdclk is 1350 MHz and the pixel rate should never exceed that.
3188*/
3189static uint32_t skl_wm_method1(uint32_t pixel_rate, uint8_t bytes_per_pixel,
3190 uint32_t latency)
3191{
3192 uint32_t wm_intermediate_val, ret;
3193
3194 if (latency == 0)
3195 return UINT_MAX;
3196
3197 wm_intermediate_val = latency * pixel_rate * bytes_per_pixel;
3198 ret = DIV_ROUND_UP(wm_intermediate_val, 1000);
3199
3200 return ret;
3201}
3202
3203static uint32_t skl_wm_method2(uint32_t pixel_rate, uint32_t pipe_htotal,
3204 uint32_t horiz_pixels, uint8_t bytes_per_pixel,
3205 uint32_t latency)
3206{
3207 uint32_t ret, plane_bytes_per_line, wm_intermediate_val;
3208
3209 if (latency == 0)
3210 return UINT_MAX;
3211
3212 plane_bytes_per_line = horiz_pixels * bytes_per_pixel;
3213 wm_intermediate_val = latency * pixel_rate;
3214 ret = DIV_ROUND_UP(wm_intermediate_val, pipe_htotal * 1000) *
3215 plane_bytes_per_line;
3216
3217 return ret;
3218}
3219
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003220static bool skl_ddb_allocation_changed(const struct skl_ddb_allocation *new_ddb,
3221 const struct intel_crtc *intel_crtc)
3222{
3223 struct drm_device *dev = intel_crtc->base.dev;
3224 struct drm_i915_private *dev_priv = dev->dev_private;
3225 const struct skl_ddb_allocation *cur_ddb = &dev_priv->wm.skl_hw.ddb;
3226 enum pipe pipe = intel_crtc->pipe;
3227
3228 if (memcmp(new_ddb->plane[pipe], cur_ddb->plane[pipe],
3229 sizeof(new_ddb->plane[pipe])))
3230 return true;
3231
3232 if (memcmp(&new_ddb->cursor[pipe], &cur_ddb->cursor[pipe],
3233 sizeof(new_ddb->cursor[pipe])))
3234 return true;
3235
3236 return false;
3237}
3238
3239static void skl_compute_wm_global_parameters(struct drm_device *dev,
3240 struct intel_wm_config *config)
3241{
3242 struct drm_crtc *crtc;
3243 struct drm_plane *plane;
3244
3245 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
3246 config->num_pipes_active += intel_crtc_active(crtc);
3247
3248 /* FIXME: I don't think we need those two global parameters on SKL */
3249 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
3250 struct intel_plane *intel_plane = to_intel_plane(plane);
3251
3252 config->sprites_enabled |= intel_plane->wm.enabled;
3253 config->sprites_scaled |= intel_plane->wm.scaled;
3254 }
3255}
3256
3257static void skl_compute_wm_pipe_parameters(struct drm_crtc *crtc,
3258 struct skl_pipe_wm_parameters *p)
3259{
3260 struct drm_device *dev = crtc->dev;
3261 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3262 enum pipe pipe = intel_crtc->pipe;
3263 struct drm_plane *plane;
3264 int i = 1; /* Index for sprite planes start */
3265
3266 p->active = intel_crtc_active(crtc);
3267 if (p->active) {
3268 p->pipe_htotal = intel_crtc->config.adjusted_mode.crtc_htotal;
3269 p->pixel_rate = skl_pipe_pixel_rate(&intel_crtc->config);
3270
3271 /*
3272 * For now, assume primary and cursor planes are always enabled.
3273 */
3274 p->plane[0].enabled = true;
3275 p->plane[0].bytes_per_pixel =
3276 crtc->primary->fb->bits_per_pixel / 8;
3277 p->plane[0].horiz_pixels = intel_crtc->config.pipe_src_w;
3278 p->plane[0].vert_pixels = intel_crtc->config.pipe_src_h;
3279
3280 p->cursor.enabled = true;
3281 p->cursor.bytes_per_pixel = 4;
3282 p->cursor.horiz_pixels = intel_crtc->cursor_width ?
3283 intel_crtc->cursor_width : 64;
3284 }
3285
3286 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
3287 struct intel_plane *intel_plane = to_intel_plane(plane);
3288
3289 if (intel_plane->pipe == pipe)
3290 p->plane[i++] = intel_plane->wm;
3291 }
3292}
3293
3294static bool skl_compute_plane_wm(struct skl_pipe_wm_parameters *p,
Damien Lespiauafb024a2014-11-04 17:06:59 +00003295 struct intel_plane_wm_parameters *p_params,
3296 uint16_t ddb_allocation,
3297 uint32_t mem_value,
3298 uint16_t *out_blocks, /* out */
3299 uint8_t *out_lines /* out */)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003300{
Damien Lespiaue6d66172014-11-04 17:06:55 +00003301 uint32_t method1, method2, plane_bytes_per_line, res_blocks, res_lines;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003302 uint32_t result_bytes;
3303
Vandana Kannan4f947382014-11-04 17:06:47 +00003304 if (mem_value == 0 || !p->active || !p_params->enabled)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003305 return false;
3306
3307 method1 = skl_wm_method1(p->pixel_rate,
3308 p_params->bytes_per_pixel,
3309 mem_value);
3310 method2 = skl_wm_method2(p->pixel_rate,
3311 p->pipe_htotal,
3312 p_params->horiz_pixels,
3313 p_params->bytes_per_pixel,
3314 mem_value);
3315
3316 plane_bytes_per_line = p_params->horiz_pixels *
3317 p_params->bytes_per_pixel;
3318
3319 /* For now xtile and linear */
Damien Lespiau21fca252014-11-04 17:06:54 +00003320 if (((ddb_allocation * 512) / plane_bytes_per_line) >= 1)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003321 result_bytes = min(method1, method2);
3322 else
3323 result_bytes = method1;
3324
Damien Lespiaue6d66172014-11-04 17:06:55 +00003325 res_blocks = DIV_ROUND_UP(result_bytes, 512) + 1;
3326 res_lines = DIV_ROUND_UP(result_bytes, plane_bytes_per_line);
3327
3328 if (res_blocks > ddb_allocation || res_lines > 31)
3329 return false;
3330
3331 *out_blocks = res_blocks;
3332 *out_lines = res_lines;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003333
3334 return true;
3335}
3336
3337static void skl_compute_wm_level(const struct drm_i915_private *dev_priv,
3338 struct skl_ddb_allocation *ddb,
3339 struct skl_pipe_wm_parameters *p,
3340 enum pipe pipe,
3341 int level,
3342 int num_planes,
3343 struct skl_wm_level *result)
3344{
3345 uint16_t latency = dev_priv->wm.skl_latency[level];
3346 uint16_t ddb_blocks;
3347 int i;
3348
3349 for (i = 0; i < num_planes; i++) {
3350 ddb_blocks = skl_ddb_entry_size(&ddb->plane[pipe][i]);
3351
3352 result->plane_en[i] = skl_compute_plane_wm(p, &p->plane[i],
3353 ddb_blocks,
3354 latency,
3355 &result->plane_res_b[i],
3356 &result->plane_res_l[i]);
3357 }
3358
3359 ddb_blocks = skl_ddb_entry_size(&ddb->cursor[pipe]);
3360 result->cursor_en = skl_compute_plane_wm(p, &p->cursor, ddb_blocks,
3361 latency, &result->cursor_res_b,
3362 &result->cursor_res_l);
3363}
3364
Damien Lespiau407b50f2014-11-04 17:06:57 +00003365static uint32_t
3366skl_compute_linetime_wm(struct drm_crtc *crtc, struct skl_pipe_wm_parameters *p)
3367{
3368 if (!intel_crtc_active(crtc))
3369 return 0;
3370
3371 return DIV_ROUND_UP(8 * p->pipe_htotal * 1000, p->pixel_rate);
3372
3373}
3374
3375static void skl_compute_transition_wm(struct drm_crtc *crtc,
3376 struct skl_pipe_wm_parameters *params,
Damien Lespiau9414f562014-11-04 17:06:58 +00003377 struct skl_wm_level *trans_wm /* out */)
Damien Lespiau407b50f2014-11-04 17:06:57 +00003378{
Damien Lespiau9414f562014-11-04 17:06:58 +00003379 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3380 int i;
3381
Damien Lespiau407b50f2014-11-04 17:06:57 +00003382 if (!params->active)
3383 return;
Damien Lespiau9414f562014-11-04 17:06:58 +00003384
3385 /* Until we know more, just disable transition WMs */
3386 for (i = 0; i < intel_num_planes(intel_crtc); i++)
3387 trans_wm->plane_en[i] = false;
3388 trans_wm->cursor_en = false;
Damien Lespiau407b50f2014-11-04 17:06:57 +00003389}
3390
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003391static void skl_compute_pipe_wm(struct drm_crtc *crtc,
3392 struct skl_ddb_allocation *ddb,
3393 struct skl_pipe_wm_parameters *params,
3394 struct skl_pipe_wm *pipe_wm)
3395{
3396 struct drm_device *dev = crtc->dev;
3397 const struct drm_i915_private *dev_priv = dev->dev_private;
3398 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3399 int level, max_level = ilk_wm_max_level(dev);
3400
3401 for (level = 0; level <= max_level; level++) {
3402 skl_compute_wm_level(dev_priv, ddb, params, intel_crtc->pipe,
3403 level, intel_num_planes(intel_crtc),
3404 &pipe_wm->wm[level]);
3405 }
3406 pipe_wm->linetime = skl_compute_linetime_wm(crtc, params);
3407
Damien Lespiau9414f562014-11-04 17:06:58 +00003408 skl_compute_transition_wm(crtc, params, &pipe_wm->trans_wm);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003409}
3410
3411static void skl_compute_wm_results(struct drm_device *dev,
3412 struct skl_pipe_wm_parameters *p,
3413 struct skl_pipe_wm *p_wm,
3414 struct skl_wm_values *r,
3415 struct intel_crtc *intel_crtc)
3416{
3417 int level, max_level = ilk_wm_max_level(dev);
3418 enum pipe pipe = intel_crtc->pipe;
Damien Lespiau9414f562014-11-04 17:06:58 +00003419 uint32_t temp;
3420 int i;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003421
3422 for (level = 0; level <= max_level; level++) {
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003423 for (i = 0; i < intel_num_planes(intel_crtc); i++) {
3424 temp = 0;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003425
3426 temp |= p_wm->wm[level].plane_res_l[i] <<
3427 PLANE_WM_LINES_SHIFT;
3428 temp |= p_wm->wm[level].plane_res_b[i];
3429 if (p_wm->wm[level].plane_en[i])
3430 temp |= PLANE_WM_EN;
3431
3432 r->plane[pipe][i][level] = temp;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003433 }
3434
3435 temp = 0;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003436
3437 temp |= p_wm->wm[level].cursor_res_l << PLANE_WM_LINES_SHIFT;
3438 temp |= p_wm->wm[level].cursor_res_b;
3439
3440 if (p_wm->wm[level].cursor_en)
3441 temp |= PLANE_WM_EN;
3442
3443 r->cursor[pipe][level] = temp;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003444
3445 }
3446
Damien Lespiau9414f562014-11-04 17:06:58 +00003447 /* transition WMs */
3448 for (i = 0; i < intel_num_planes(intel_crtc); i++) {
3449 temp = 0;
3450 temp |= p_wm->trans_wm.plane_res_l[i] << PLANE_WM_LINES_SHIFT;
3451 temp |= p_wm->trans_wm.plane_res_b[i];
3452 if (p_wm->trans_wm.plane_en[i])
3453 temp |= PLANE_WM_EN;
3454
3455 r->plane_trans[pipe][i] = temp;
3456 }
3457
3458 temp = 0;
3459 temp |= p_wm->trans_wm.cursor_res_l << PLANE_WM_LINES_SHIFT;
3460 temp |= p_wm->trans_wm.cursor_res_b;
3461 if (p_wm->trans_wm.cursor_en)
3462 temp |= PLANE_WM_EN;
3463
3464 r->cursor_trans[pipe] = temp;
3465
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003466 r->wm_linetime[pipe] = p_wm->linetime;
3467}
3468
Damien Lespiau16160e32014-11-04 17:06:53 +00003469static void skl_ddb_entry_write(struct drm_i915_private *dev_priv, uint32_t reg,
3470 const struct skl_ddb_entry *entry)
3471{
3472 if (entry->end)
3473 I915_WRITE(reg, (entry->end - 1) << 16 | entry->start);
3474 else
3475 I915_WRITE(reg, 0);
3476}
3477
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003478static void skl_write_wm_values(struct drm_i915_private *dev_priv,
3479 const struct skl_wm_values *new)
3480{
3481 struct drm_device *dev = dev_priv->dev;
3482 struct intel_crtc *crtc;
3483
3484 list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head) {
3485 int i, level, max_level = ilk_wm_max_level(dev);
3486 enum pipe pipe = crtc->pipe;
3487
Damien Lespiau5d374d92014-11-04 17:07:00 +00003488 if (!new->dirty[pipe])
3489 continue;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003490
Damien Lespiau5d374d92014-11-04 17:07:00 +00003491 I915_WRITE(PIPE_WM_LINETIME(pipe), new->wm_linetime[pipe]);
3492
3493 for (level = 0; level <= max_level; level++) {
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003494 for (i = 0; i < intel_num_planes(crtc); i++)
Damien Lespiau5d374d92014-11-04 17:07:00 +00003495 I915_WRITE(PLANE_WM(pipe, i, level),
3496 new->plane[pipe][i][level]);
3497 I915_WRITE(CUR_WM(pipe, level),
3498 new->cursor[pipe][level]);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003499 }
Damien Lespiau5d374d92014-11-04 17:07:00 +00003500 for (i = 0; i < intel_num_planes(crtc); i++)
3501 I915_WRITE(PLANE_WM_TRANS(pipe, i),
3502 new->plane_trans[pipe][i]);
3503 I915_WRITE(CUR_WM_TRANS(pipe), new->cursor_trans[pipe]);
3504
3505 for (i = 0; i < intel_num_planes(crtc); i++)
3506 skl_ddb_entry_write(dev_priv,
3507 PLANE_BUF_CFG(pipe, i),
3508 &new->ddb.plane[pipe][i]);
3509
3510 skl_ddb_entry_write(dev_priv, CUR_BUF_CFG(pipe),
3511 &new->ddb.cursor[pipe]);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003512 }
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003513}
3514
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003515/*
3516 * When setting up a new DDB allocation arrangement, we need to correctly
3517 * sequence the times at which the new allocations for the pipes are taken into
3518 * account or we'll have pipes fetching from space previously allocated to
3519 * another pipe.
3520 *
3521 * Roughly the sequence looks like:
3522 * 1. re-allocate the pipe(s) with the allocation being reduced and not
3523 * overlapping with a previous light-up pipe (another way to put it is:
3524 * pipes with their new allocation strickly included into their old ones).
3525 * 2. re-allocate the other pipes that get their allocation reduced
3526 * 3. allocate the pipes having their allocation increased
3527 *
3528 * Steps 1. and 2. are here to take care of the following case:
3529 * - Initially DDB looks like this:
3530 * | B | C |
3531 * - enable pipe A.
3532 * - pipe B has a reduced DDB allocation that overlaps with the old pipe C
3533 * allocation
3534 * | A | B | C |
3535 *
3536 * We need to sequence the re-allocation: C, B, A (and not B, C, A).
3537 */
3538
Damien Lespiaud21b7952014-11-04 17:07:03 +00003539static void
3540skl_wm_flush_pipe(struct drm_i915_private *dev_priv, enum pipe pipe, int pass)
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003541{
3542 struct drm_device *dev = dev_priv->dev;
3543 int plane;
3544
Damien Lespiaud21b7952014-11-04 17:07:03 +00003545 DRM_DEBUG_KMS("flush pipe %c (pass %d)\n", pipe_name(pipe), pass);
3546
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003547 for_each_plane(pipe, plane) {
3548 I915_WRITE(PLANE_SURF(pipe, plane),
3549 I915_READ(PLANE_SURF(pipe, plane)));
3550 }
3551 I915_WRITE(CURBASE(pipe), I915_READ(CURBASE(pipe)));
3552}
3553
3554static bool
3555skl_ddb_allocation_included(const struct skl_ddb_allocation *old,
3556 const struct skl_ddb_allocation *new,
3557 enum pipe pipe)
3558{
3559 uint16_t old_size, new_size;
3560
3561 old_size = skl_ddb_entry_size(&old->pipe[pipe]);
3562 new_size = skl_ddb_entry_size(&new->pipe[pipe]);
3563
3564 return old_size != new_size &&
3565 new->pipe[pipe].start >= old->pipe[pipe].start &&
3566 new->pipe[pipe].end <= old->pipe[pipe].end;
3567}
3568
3569static void skl_flush_wm_values(struct drm_i915_private *dev_priv,
3570 struct skl_wm_values *new_values)
3571{
3572 struct drm_device *dev = dev_priv->dev;
3573 struct skl_ddb_allocation *cur_ddb, *new_ddb;
3574 bool reallocated[I915_MAX_PIPES] = {false, false, false};
3575 struct intel_crtc *crtc;
3576 enum pipe pipe;
3577
3578 new_ddb = &new_values->ddb;
3579 cur_ddb = &dev_priv->wm.skl_hw.ddb;
3580
3581 /*
3582 * First pass: flush the pipes with the new allocation contained into
3583 * the old space.
3584 *
3585 * We'll wait for the vblank on those pipes to ensure we can safely
3586 * re-allocate the freed space without this pipe fetching from it.
3587 */
3588 for_each_intel_crtc(dev, crtc) {
3589 if (!crtc->active)
3590 continue;
3591
3592 pipe = crtc->pipe;
3593
3594 if (!skl_ddb_allocation_included(cur_ddb, new_ddb, pipe))
3595 continue;
3596
Damien Lespiaud21b7952014-11-04 17:07:03 +00003597 skl_wm_flush_pipe(dev_priv, pipe, 1);
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003598 intel_wait_for_vblank(dev, pipe);
3599
3600 reallocated[pipe] = true;
3601 }
3602
3603
3604 /*
3605 * Second pass: flush the pipes that are having their allocation
3606 * reduced, but overlapping with a previous allocation.
3607 *
3608 * Here as well we need to wait for the vblank to make sure the freed
3609 * space is not used anymore.
3610 */
3611 for_each_intel_crtc(dev, crtc) {
3612 if (!crtc->active)
3613 continue;
3614
3615 pipe = crtc->pipe;
3616
3617 if (reallocated[pipe])
3618 continue;
3619
3620 if (skl_ddb_entry_size(&new_ddb->pipe[pipe]) <
3621 skl_ddb_entry_size(&cur_ddb->pipe[pipe])) {
Damien Lespiaud21b7952014-11-04 17:07:03 +00003622 skl_wm_flush_pipe(dev_priv, pipe, 2);
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003623 intel_wait_for_vblank(dev, pipe);
3624 }
3625
3626 reallocated[pipe] = true;
3627 }
3628
3629 /*
3630 * Third pass: flush the pipes that got more space allocated.
3631 *
3632 * We don't need to actively wait for the update here, next vblank
3633 * will just get more DDB space with the correct WM values.
3634 */
3635 for_each_intel_crtc(dev, crtc) {
3636 if (!crtc->active)
3637 continue;
3638
3639 pipe = crtc->pipe;
3640
3641 /*
3642 * At this point, only the pipes more space than before are
3643 * left to re-allocate.
3644 */
3645 if (reallocated[pipe])
3646 continue;
3647
Damien Lespiaud21b7952014-11-04 17:07:03 +00003648 skl_wm_flush_pipe(dev_priv, pipe, 3);
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003649 }
3650}
3651
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003652static bool skl_update_pipe_wm(struct drm_crtc *crtc,
3653 struct skl_pipe_wm_parameters *params,
3654 struct intel_wm_config *config,
3655 struct skl_ddb_allocation *ddb, /* out */
3656 struct skl_pipe_wm *pipe_wm /* out */)
3657{
3658 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3659
3660 skl_compute_wm_pipe_parameters(crtc, params);
Damien Lespiaub9cec072014-11-04 17:06:43 +00003661 skl_allocate_pipe_ddb(crtc, config, params, ddb);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003662 skl_compute_pipe_wm(crtc, ddb, params, pipe_wm);
3663
3664 if (!memcmp(&intel_crtc->wm.skl_active, pipe_wm, sizeof(*pipe_wm)))
3665 return false;
3666
3667 intel_crtc->wm.skl_active = *pipe_wm;
3668 return true;
3669}
3670
3671static void skl_update_other_pipe_wm(struct drm_device *dev,
3672 struct drm_crtc *crtc,
3673 struct intel_wm_config *config,
3674 struct skl_wm_values *r)
3675{
3676 struct intel_crtc *intel_crtc;
3677 struct intel_crtc *this_crtc = to_intel_crtc(crtc);
3678
3679 /*
3680 * If the WM update hasn't changed the allocation for this_crtc (the
3681 * crtc we are currently computing the new WM values for), other
3682 * enabled crtcs will keep the same allocation and we don't need to
3683 * recompute anything for them.
3684 */
3685 if (!skl_ddb_allocation_changed(&r->ddb, this_crtc))
3686 return;
3687
3688 /*
3689 * Otherwise, because of this_crtc being freshly enabled/disabled, the
3690 * other active pipes need new DDB allocation and WM values.
3691 */
3692 list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
3693 base.head) {
3694 struct skl_pipe_wm_parameters params = {};
3695 struct skl_pipe_wm pipe_wm = {};
3696 bool wm_changed;
3697
3698 if (this_crtc->pipe == intel_crtc->pipe)
3699 continue;
3700
3701 if (!intel_crtc->active)
3702 continue;
3703
3704 wm_changed = skl_update_pipe_wm(&intel_crtc->base,
3705 &params, config,
3706 &r->ddb, &pipe_wm);
3707
3708 /*
3709 * If we end up re-computing the other pipe WM values, it's
3710 * because it was really needed, so we expect the WM values to
3711 * be different.
3712 */
3713 WARN_ON(!wm_changed);
3714
3715 skl_compute_wm_results(dev, &params, &pipe_wm, r, intel_crtc);
3716 r->dirty[intel_crtc->pipe] = true;
3717 }
3718}
3719
3720static void skl_update_wm(struct drm_crtc *crtc)
3721{
3722 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3723 struct drm_device *dev = crtc->dev;
3724 struct drm_i915_private *dev_priv = dev->dev_private;
3725 struct skl_pipe_wm_parameters params = {};
3726 struct skl_wm_values *results = &dev_priv->wm.skl_results;
3727 struct skl_pipe_wm pipe_wm = {};
3728 struct intel_wm_config config = {};
3729
3730 memset(results, 0, sizeof(*results));
3731
3732 skl_compute_wm_global_parameters(dev, &config);
3733
3734 if (!skl_update_pipe_wm(crtc, &params, &config,
3735 &results->ddb, &pipe_wm))
3736 return;
3737
3738 skl_compute_wm_results(dev, &params, &pipe_wm, results, intel_crtc);
3739 results->dirty[intel_crtc->pipe] = true;
3740
3741 skl_update_other_pipe_wm(dev, crtc, &config, results);
3742 skl_write_wm_values(dev_priv, results);
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003743 skl_flush_wm_values(dev_priv, results);
Damien Lespiau53b0deb2014-11-04 17:06:48 +00003744
3745 /* store the new configuration */
3746 dev_priv->wm.skl_hw = *results;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003747}
3748
3749static void
3750skl_update_sprite_wm(struct drm_plane *plane, struct drm_crtc *crtc,
3751 uint32_t sprite_width, uint32_t sprite_height,
3752 int pixel_size, bool enabled, bool scaled)
3753{
3754 struct intel_plane *intel_plane = to_intel_plane(plane);
3755
3756 intel_plane->wm.enabled = enabled;
3757 intel_plane->wm.scaled = scaled;
3758 intel_plane->wm.horiz_pixels = sprite_width;
3759 intel_plane->wm.vert_pixels = sprite_height;
3760 intel_plane->wm.bytes_per_pixel = pixel_size;
3761
3762 skl_update_wm(crtc);
3763}
3764
Imre Deak820c1982013-12-17 14:46:36 +02003765static void ilk_update_wm(struct drm_crtc *crtc)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003766{
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03003767 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjälä46ba6142013-09-10 11:40:40 +03003768 struct drm_device *dev = crtc->dev;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003769 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak820c1982013-12-17 14:46:36 +02003770 struct ilk_wm_maximums max;
3771 struct ilk_pipe_wm_parameters params = {};
3772 struct ilk_wm_values results = {};
Ville Syrjälä77c122b2013-08-06 22:24:04 +03003773 enum intel_ddb_partitioning partitioning;
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03003774 struct intel_pipe_wm pipe_wm = {};
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003775 struct intel_pipe_wm lp_wm_1_2 = {}, lp_wm_5_6 = {}, *best_lp_wm;
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03003776 struct intel_wm_config config = {};
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003777
Ville Syrjälä2a44b762014-03-07 18:32:09 +02003778 ilk_compute_wm_parameters(crtc, &params);
Paulo Zanoni861f3382013-05-31 10:19:21 -03003779
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03003780 intel_compute_pipe_wm(crtc, &params, &pipe_wm);
3781
3782 if (!memcmp(&intel_crtc->wm.active, &pipe_wm, sizeof(pipe_wm)))
3783 return;
3784
3785 intel_crtc->wm.active = pipe_wm;
3786
Ville Syrjälä2a44b762014-03-07 18:32:09 +02003787 ilk_compute_wm_config(dev, &config);
3788
Ville Syrjälä34982fe2013-10-09 19:18:09 +03003789 ilk_compute_wm_maximums(dev, 1, &config, INTEL_DDB_PART_1_2, &max);
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02003790 ilk_wm_merge(dev, &config, &max, &lp_wm_1_2);
Ville Syrjälä0362c782013-10-09 19:17:57 +03003791
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03003792 /* 5/6 split only in single pipe config on IVB+ */
Ville Syrjäläec98c8d2013-10-11 15:26:26 +03003793 if (INTEL_INFO(dev)->gen >= 7 &&
3794 config.num_pipes_active == 1 && config.sprites_enabled) {
Ville Syrjälä34982fe2013-10-09 19:18:09 +03003795 ilk_compute_wm_maximums(dev, 1, &config, INTEL_DDB_PART_5_6, &max);
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02003796 ilk_wm_merge(dev, &config, &max, &lp_wm_5_6);
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03003797
Imre Deak820c1982013-12-17 14:46:36 +02003798 best_lp_wm = ilk_find_best_result(dev, &lp_wm_1_2, &lp_wm_5_6);
Paulo Zanoni861f3382013-05-31 10:19:21 -03003799 } else {
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003800 best_lp_wm = &lp_wm_1_2;
Paulo Zanoni861f3382013-05-31 10:19:21 -03003801 }
3802
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003803 partitioning = (best_lp_wm == &lp_wm_1_2) ?
Ville Syrjälä77c122b2013-08-06 22:24:04 +03003804 INTEL_DDB_PART_1_2 : INTEL_DDB_PART_5_6;
Paulo Zanoni861f3382013-05-31 10:19:21 -03003805
Imre Deak820c1982013-12-17 14:46:36 +02003806 ilk_compute_wm_results(dev, best_lp_wm, partitioning, &results);
Ville Syrjälä609cede2013-10-09 19:18:03 +03003807
Imre Deak820c1982013-12-17 14:46:36 +02003808 ilk_write_wm_values(dev_priv, &results);
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03003809}
3810
Damien Lespiaued57cb82014-07-15 09:21:24 +02003811static void
3812ilk_update_sprite_wm(struct drm_plane *plane,
3813 struct drm_crtc *crtc,
3814 uint32_t sprite_width, uint32_t sprite_height,
3815 int pixel_size, bool enabled, bool scaled)
Paulo Zanoni526682e2013-05-24 11:59:18 -03003816{
Ville Syrjälä8553c182013-12-05 15:51:39 +02003817 struct drm_device *dev = plane->dev;
Ville Syrjäläadf3d352013-08-06 22:24:11 +03003818 struct intel_plane *intel_plane = to_intel_plane(plane);
Paulo Zanoni526682e2013-05-24 11:59:18 -03003819
Ville Syrjäläadf3d352013-08-06 22:24:11 +03003820 intel_plane->wm.enabled = enabled;
3821 intel_plane->wm.scaled = scaled;
3822 intel_plane->wm.horiz_pixels = sprite_width;
Damien Lespiaued57cb82014-07-15 09:21:24 +02003823 intel_plane->wm.vert_pixels = sprite_width;
Ville Syrjäläadf3d352013-08-06 22:24:11 +03003824 intel_plane->wm.bytes_per_pixel = pixel_size;
Paulo Zanoni526682e2013-05-24 11:59:18 -03003825
Ville Syrjälä8553c182013-12-05 15:51:39 +02003826 /*
3827 * IVB workaround: must disable low power watermarks for at least
3828 * one frame before enabling scaling. LP watermarks can be re-enabled
3829 * when scaling is disabled.
3830 *
3831 * WaCxSRDisabledForSpriteScaling:ivb
3832 */
3833 if (IS_IVYBRIDGE(dev) && scaled && ilk_disable_lp_wm(dev))
3834 intel_wait_for_vblank(dev, intel_plane->pipe);
3835
Imre Deak820c1982013-12-17 14:46:36 +02003836 ilk_update_wm(crtc);
Paulo Zanoni526682e2013-05-24 11:59:18 -03003837}
3838
Pradeep Bhat30789992014-11-04 17:06:45 +00003839static void skl_pipe_wm_active_state(uint32_t val,
3840 struct skl_pipe_wm *active,
3841 bool is_transwm,
3842 bool is_cursor,
3843 int i,
3844 int level)
3845{
3846 bool is_enabled = (val & PLANE_WM_EN) != 0;
3847
3848 if (!is_transwm) {
3849 if (!is_cursor) {
3850 active->wm[level].plane_en[i] = is_enabled;
3851 active->wm[level].plane_res_b[i] =
3852 val & PLANE_WM_BLOCKS_MASK;
3853 active->wm[level].plane_res_l[i] =
3854 (val >> PLANE_WM_LINES_SHIFT) &
3855 PLANE_WM_LINES_MASK;
3856 } else {
3857 active->wm[level].cursor_en = is_enabled;
3858 active->wm[level].cursor_res_b =
3859 val & PLANE_WM_BLOCKS_MASK;
3860 active->wm[level].cursor_res_l =
3861 (val >> PLANE_WM_LINES_SHIFT) &
3862 PLANE_WM_LINES_MASK;
3863 }
3864 } else {
3865 if (!is_cursor) {
3866 active->trans_wm.plane_en[i] = is_enabled;
3867 active->trans_wm.plane_res_b[i] =
3868 val & PLANE_WM_BLOCKS_MASK;
3869 active->trans_wm.plane_res_l[i] =
3870 (val >> PLANE_WM_LINES_SHIFT) &
3871 PLANE_WM_LINES_MASK;
3872 } else {
3873 active->trans_wm.cursor_en = is_enabled;
3874 active->trans_wm.cursor_res_b =
3875 val & PLANE_WM_BLOCKS_MASK;
3876 active->trans_wm.cursor_res_l =
3877 (val >> PLANE_WM_LINES_SHIFT) &
3878 PLANE_WM_LINES_MASK;
3879 }
3880 }
3881}
3882
3883static void skl_pipe_wm_get_hw_state(struct drm_crtc *crtc)
3884{
3885 struct drm_device *dev = crtc->dev;
3886 struct drm_i915_private *dev_priv = dev->dev_private;
3887 struct skl_wm_values *hw = &dev_priv->wm.skl_hw;
3888 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3889 struct skl_pipe_wm *active = &intel_crtc->wm.skl_active;
3890 enum pipe pipe = intel_crtc->pipe;
3891 int level, i, max_level;
3892 uint32_t temp;
3893
3894 max_level = ilk_wm_max_level(dev);
3895
3896 hw->wm_linetime[pipe] = I915_READ(PIPE_WM_LINETIME(pipe));
3897
3898 for (level = 0; level <= max_level; level++) {
3899 for (i = 0; i < intel_num_planes(intel_crtc); i++)
3900 hw->plane[pipe][i][level] =
3901 I915_READ(PLANE_WM(pipe, i, level));
3902 hw->cursor[pipe][level] = I915_READ(CUR_WM(pipe, level));
3903 }
3904
3905 for (i = 0; i < intel_num_planes(intel_crtc); i++)
3906 hw->plane_trans[pipe][i] = I915_READ(PLANE_WM_TRANS(pipe, i));
3907 hw->cursor_trans[pipe] = I915_READ(CUR_WM_TRANS(pipe));
3908
3909 if (!intel_crtc_active(crtc))
3910 return;
3911
3912 hw->dirty[pipe] = true;
3913
3914 active->linetime = hw->wm_linetime[pipe];
3915
3916 for (level = 0; level <= max_level; level++) {
3917 for (i = 0; i < intel_num_planes(intel_crtc); i++) {
3918 temp = hw->plane[pipe][i][level];
3919 skl_pipe_wm_active_state(temp, active, false,
3920 false, i, level);
3921 }
3922 temp = hw->cursor[pipe][level];
3923 skl_pipe_wm_active_state(temp, active, false, true, i, level);
3924 }
3925
3926 for (i = 0; i < intel_num_planes(intel_crtc); i++) {
3927 temp = hw->plane_trans[pipe][i];
3928 skl_pipe_wm_active_state(temp, active, true, false, i, 0);
3929 }
3930
3931 temp = hw->cursor_trans[pipe];
3932 skl_pipe_wm_active_state(temp, active, true, true, i, 0);
3933}
3934
3935void skl_wm_get_hw_state(struct drm_device *dev)
3936{
Damien Lespiaua269c582014-11-04 17:06:49 +00003937 struct drm_i915_private *dev_priv = dev->dev_private;
3938 struct skl_ddb_allocation *ddb = &dev_priv->wm.skl_hw.ddb;
Pradeep Bhat30789992014-11-04 17:06:45 +00003939 struct drm_crtc *crtc;
3940
Damien Lespiaua269c582014-11-04 17:06:49 +00003941 skl_ddb_get_hw_state(dev_priv, ddb);
Pradeep Bhat30789992014-11-04 17:06:45 +00003942 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
3943 skl_pipe_wm_get_hw_state(crtc);
3944}
3945
Ville Syrjälä243e6a42013-10-14 14:55:24 +03003946static void ilk_pipe_wm_get_hw_state(struct drm_crtc *crtc)
3947{
3948 struct drm_device *dev = crtc->dev;
3949 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak820c1982013-12-17 14:46:36 +02003950 struct ilk_wm_values *hw = &dev_priv->wm.hw;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03003951 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3952 struct intel_pipe_wm *active = &intel_crtc->wm.active;
3953 enum pipe pipe = intel_crtc->pipe;
3954 static const unsigned int wm0_pipe_reg[] = {
3955 [PIPE_A] = WM0_PIPEA_ILK,
3956 [PIPE_B] = WM0_PIPEB_ILK,
3957 [PIPE_C] = WM0_PIPEC_IVB,
3958 };
3959
3960 hw->wm_pipe[pipe] = I915_READ(wm0_pipe_reg[pipe]);
Ville Syrjäläa42a5712014-01-07 16:14:08 +02003961 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Ville Syrjäläce0e0712013-12-05 15:51:36 +02003962 hw->wm_linetime[pipe] = I915_READ(PIPE_WM_LINETIME(pipe));
Ville Syrjälä243e6a42013-10-14 14:55:24 +03003963
Ville Syrjälä2a44b762014-03-07 18:32:09 +02003964 active->pipe_enabled = intel_crtc_active(crtc);
3965
3966 if (active->pipe_enabled) {
Ville Syrjälä243e6a42013-10-14 14:55:24 +03003967 u32 tmp = hw->wm_pipe[pipe];
3968
3969 /*
3970 * For active pipes LP0 watermark is marked as
3971 * enabled, and LP1+ watermaks as disabled since
3972 * we can't really reverse compute them in case
3973 * multiple pipes are active.
3974 */
3975 active->wm[0].enable = true;
3976 active->wm[0].pri_val = (tmp & WM0_PIPE_PLANE_MASK) >> WM0_PIPE_PLANE_SHIFT;
3977 active->wm[0].spr_val = (tmp & WM0_PIPE_SPRITE_MASK) >> WM0_PIPE_SPRITE_SHIFT;
3978 active->wm[0].cur_val = tmp & WM0_PIPE_CURSOR_MASK;
3979 active->linetime = hw->wm_linetime[pipe];
3980 } else {
3981 int level, max_level = ilk_wm_max_level(dev);
3982
3983 /*
3984 * For inactive pipes, all watermark levels
3985 * should be marked as enabled but zeroed,
3986 * which is what we'd compute them to.
3987 */
3988 for (level = 0; level <= max_level; level++)
3989 active->wm[level].enable = true;
3990 }
3991}
3992
3993void ilk_wm_get_hw_state(struct drm_device *dev)
3994{
3995 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak820c1982013-12-17 14:46:36 +02003996 struct ilk_wm_values *hw = &dev_priv->wm.hw;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03003997 struct drm_crtc *crtc;
3998
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01003999 for_each_crtc(dev, crtc)
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004000 ilk_pipe_wm_get_hw_state(crtc);
4001
4002 hw->wm_lp[0] = I915_READ(WM1_LP_ILK);
4003 hw->wm_lp[1] = I915_READ(WM2_LP_ILK);
4004 hw->wm_lp[2] = I915_READ(WM3_LP_ILK);
4005
4006 hw->wm_lp_spr[0] = I915_READ(WM1S_LP_ILK);
Ville Syrjäläcfa76982014-03-07 18:32:08 +02004007 if (INTEL_INFO(dev)->gen >= 7) {
4008 hw->wm_lp_spr[1] = I915_READ(WM2S_LP_IVB);
4009 hw->wm_lp_spr[2] = I915_READ(WM3S_LP_IVB);
4010 }
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004011
Ville Syrjäläa42a5712014-01-07 16:14:08 +02004012 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Ville Syrjäläac9545f2013-12-05 15:51:28 +02004013 hw->partitioning = (I915_READ(WM_MISC) & WM_MISC_DATA_PARTITION_5_6) ?
4014 INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
4015 else if (IS_IVYBRIDGE(dev))
4016 hw->partitioning = (I915_READ(DISP_ARB_CTL2) & DISP_DATA_PARTITION_5_6) ?
4017 INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004018
4019 hw->enable_fbc_wm =
4020 !(I915_READ(DISP_ARB_CTL) & DISP_FBC_WM_DIS);
4021}
4022
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03004023/**
4024 * intel_update_watermarks - update FIFO watermark values based on current modes
4025 *
4026 * Calculate watermark values for the various WM regs based on current mode
4027 * and plane configuration.
4028 *
4029 * There are several cases to deal with here:
4030 * - normal (i.e. non-self-refresh)
4031 * - self-refresh (SR) mode
4032 * - lines are large relative to FIFO size (buffer can hold up to 2)
4033 * - lines are small relative to FIFO size (buffer can hold more than 2
4034 * lines), so need to account for TLB latency
4035 *
4036 * The normal calculation is:
4037 * watermark = dotclock * bytes per pixel * latency
4038 * where latency is platform & configuration dependent (we assume pessimal
4039 * values here).
4040 *
4041 * The SR calculation is:
4042 * watermark = (trunc(latency/line time)+1) * surface width *
4043 * bytes per pixel
4044 * where
4045 * line time = htotal / dotclock
4046 * surface width = hdisplay for normal plane and 64 for cursor
4047 * and latency is assumed to be high, as above.
4048 *
4049 * The final value programmed to the register should always be rounded up,
4050 * and include an extra 2 entries to account for clock crossings.
4051 *
4052 * We don't use the sprite, so we can ignore that. And on Crestline we have
4053 * to set the non-SR watermarks to 8.
4054 */
Ville Syrjälä46ba6142013-09-10 11:40:40 +03004055void intel_update_watermarks(struct drm_crtc *crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03004056{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03004057 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03004058
4059 if (dev_priv->display.update_wm)
Ville Syrjälä46ba6142013-09-10 11:40:40 +03004060 dev_priv->display.update_wm(crtc);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03004061}
4062
Ville Syrjäläadf3d352013-08-06 22:24:11 +03004063void intel_update_sprite_watermarks(struct drm_plane *plane,
4064 struct drm_crtc *crtc,
Damien Lespiaued57cb82014-07-15 09:21:24 +02004065 uint32_t sprite_width,
4066 uint32_t sprite_height,
4067 int pixel_size,
Ville Syrjälä39db4a42013-08-06 22:24:00 +03004068 bool enabled, bool scaled)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03004069{
Ville Syrjäläadf3d352013-08-06 22:24:11 +03004070 struct drm_i915_private *dev_priv = plane->dev->dev_private;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03004071
4072 if (dev_priv->display.update_sprite_wm)
Damien Lespiaued57cb82014-07-15 09:21:24 +02004073 dev_priv->display.update_sprite_wm(plane, crtc,
4074 sprite_width, sprite_height,
Ville Syrjälä39db4a42013-08-06 22:24:00 +03004075 pixel_size, enabled, scaled);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03004076}
4077
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004078static struct drm_i915_gem_object *
4079intel_alloc_context_page(struct drm_device *dev)
4080{
4081 struct drm_i915_gem_object *ctx;
4082 int ret;
4083
4084 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
4085
4086 ctx = i915_gem_alloc_object(dev, 4096);
4087 if (!ctx) {
4088 DRM_DEBUG("failed to alloc power context, RC6 disabled\n");
4089 return NULL;
4090 }
4091
Daniel Vetterc69766f2014-02-14 14:01:17 +01004092 ret = i915_gem_obj_ggtt_pin(ctx, 4096, 0);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004093 if (ret) {
4094 DRM_ERROR("failed to pin power context: %d\n", ret);
4095 goto err_unref;
4096 }
4097
4098 ret = i915_gem_object_set_to_gtt_domain(ctx, 1);
4099 if (ret) {
4100 DRM_ERROR("failed to set-domain on power context: %d\n", ret);
4101 goto err_unpin;
4102 }
4103
4104 return ctx;
4105
4106err_unpin:
Ben Widawskyd7f46fc2013-12-06 14:10:55 -08004107 i915_gem_object_ggtt_unpin(ctx);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004108err_unref:
4109 drm_gem_object_unreference(&ctx->base);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004110 return NULL;
4111}
4112
Daniel Vetter92703882012-08-09 16:46:01 +02004113/**
4114 * Lock protecting IPS related data structures
Daniel Vetter92703882012-08-09 16:46:01 +02004115 */
4116DEFINE_SPINLOCK(mchdev_lock);
4117
4118/* Global for IPS driver to get at the current i915 device. Protected by
4119 * mchdev_lock. */
4120static struct drm_i915_private *i915_mch_dev;
4121
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004122bool ironlake_set_drps(struct drm_device *dev, u8 val)
4123{
4124 struct drm_i915_private *dev_priv = dev->dev_private;
4125 u16 rgvswctl;
4126
Daniel Vetter92703882012-08-09 16:46:01 +02004127 assert_spin_locked(&mchdev_lock);
4128
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004129 rgvswctl = I915_READ16(MEMSWCTL);
4130 if (rgvswctl & MEMCTL_CMD_STS) {
4131 DRM_DEBUG("gpu busy, RCS change rejected\n");
4132 return false; /* still busy with another command */
4133 }
4134
4135 rgvswctl = (MEMCTL_CMD_CHFREQ << MEMCTL_CMD_SHIFT) |
4136 (val << MEMCTL_FREQ_SHIFT) | MEMCTL_SFCAVM;
4137 I915_WRITE16(MEMSWCTL, rgvswctl);
4138 POSTING_READ16(MEMSWCTL);
4139
4140 rgvswctl |= MEMCTL_CMD_STS;
4141 I915_WRITE16(MEMSWCTL, rgvswctl);
4142
4143 return true;
4144}
4145
Daniel Vetter8090c6b2012-06-24 16:42:32 +02004146static void ironlake_enable_drps(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004147{
4148 struct drm_i915_private *dev_priv = dev->dev_private;
4149 u32 rgvmodectl = I915_READ(MEMMODECTL);
4150 u8 fmax, fmin, fstart, vstart;
4151
Daniel Vetter92703882012-08-09 16:46:01 +02004152 spin_lock_irq(&mchdev_lock);
4153
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004154 /* Enable temp reporting */
4155 I915_WRITE16(PMMISC, I915_READ(PMMISC) | MCPPCE_EN);
4156 I915_WRITE16(TSC1, I915_READ(TSC1) | TSE);
4157
4158 /* 100ms RC evaluation intervals */
4159 I915_WRITE(RCUPEI, 100000);
4160 I915_WRITE(RCDNEI, 100000);
4161
4162 /* Set max/min thresholds to 90ms and 80ms respectively */
4163 I915_WRITE(RCBMAXAVG, 90000);
4164 I915_WRITE(RCBMINAVG, 80000);
4165
4166 I915_WRITE(MEMIHYST, 1);
4167
4168 /* Set up min, max, and cur for interrupt handling */
4169 fmax = (rgvmodectl & MEMMODE_FMAX_MASK) >> MEMMODE_FMAX_SHIFT;
4170 fmin = (rgvmodectl & MEMMODE_FMIN_MASK);
4171 fstart = (rgvmodectl & MEMMODE_FSTART_MASK) >>
4172 MEMMODE_FSTART_SHIFT;
4173
4174 vstart = (I915_READ(PXVFREQ_BASE + (fstart * 4)) & PXVFREQ_PX_MASK) >>
4175 PXVFREQ_PX_SHIFT;
4176
Daniel Vetter20e4d402012-08-08 23:35:39 +02004177 dev_priv->ips.fmax = fmax; /* IPS callback will increase this */
4178 dev_priv->ips.fstart = fstart;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004179
Daniel Vetter20e4d402012-08-08 23:35:39 +02004180 dev_priv->ips.max_delay = fstart;
4181 dev_priv->ips.min_delay = fmin;
4182 dev_priv->ips.cur_delay = fstart;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004183
4184 DRM_DEBUG_DRIVER("fmax: %d, fmin: %d, fstart: %d\n",
4185 fmax, fmin, fstart);
4186
4187 I915_WRITE(MEMINTREN, MEMINT_CX_SUPR_EN | MEMINT_EVAL_CHG_EN);
4188
4189 /*
4190 * Interrupts will be enabled in ironlake_irq_postinstall
4191 */
4192
4193 I915_WRITE(VIDSTART, vstart);
4194 POSTING_READ(VIDSTART);
4195
4196 rgvmodectl |= MEMMODE_SWMODE_EN;
4197 I915_WRITE(MEMMODECTL, rgvmodectl);
4198
Daniel Vetter92703882012-08-09 16:46:01 +02004199 if (wait_for_atomic((I915_READ(MEMSWCTL) & MEMCTL_CMD_STS) == 0, 10))
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004200 DRM_ERROR("stuck trying to change perf mode\n");
Daniel Vetter92703882012-08-09 16:46:01 +02004201 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004202
4203 ironlake_set_drps(dev, fstart);
4204
Daniel Vetter20e4d402012-08-08 23:35:39 +02004205 dev_priv->ips.last_count1 = I915_READ(0x112e4) + I915_READ(0x112e8) +
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004206 I915_READ(0x112e0);
Daniel Vetter20e4d402012-08-08 23:35:39 +02004207 dev_priv->ips.last_time1 = jiffies_to_msecs(jiffies);
4208 dev_priv->ips.last_count2 = I915_READ(0x112f4);
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00004209 dev_priv->ips.last_time2 = ktime_get_raw_ns();
Daniel Vetter92703882012-08-09 16:46:01 +02004210
4211 spin_unlock_irq(&mchdev_lock);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004212}
4213
Daniel Vetter8090c6b2012-06-24 16:42:32 +02004214static void ironlake_disable_drps(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004215{
4216 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter92703882012-08-09 16:46:01 +02004217 u16 rgvswctl;
4218
4219 spin_lock_irq(&mchdev_lock);
4220
4221 rgvswctl = I915_READ16(MEMSWCTL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004222
4223 /* Ack interrupts, disable EFC interrupt */
4224 I915_WRITE(MEMINTREN, I915_READ(MEMINTREN) & ~MEMINT_EVAL_CHG_EN);
4225 I915_WRITE(MEMINTRSTS, MEMINT_EVAL_CHG);
4226 I915_WRITE(DEIER, I915_READ(DEIER) & ~DE_PCU_EVENT);
4227 I915_WRITE(DEIIR, DE_PCU_EVENT);
4228 I915_WRITE(DEIMR, I915_READ(DEIMR) | DE_PCU_EVENT);
4229
4230 /* Go back to the starting frequency */
Daniel Vetter20e4d402012-08-08 23:35:39 +02004231 ironlake_set_drps(dev, dev_priv->ips.fstart);
Daniel Vetter92703882012-08-09 16:46:01 +02004232 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004233 rgvswctl |= MEMCTL_CMD_STS;
4234 I915_WRITE(MEMSWCTL, rgvswctl);
Daniel Vetter92703882012-08-09 16:46:01 +02004235 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004236
Daniel Vetter92703882012-08-09 16:46:01 +02004237 spin_unlock_irq(&mchdev_lock);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004238}
4239
Daniel Vetteracbe9472012-07-26 11:50:05 +02004240/* There's a funny hw issue where the hw returns all 0 when reading from
4241 * GEN6_RP_INTERRUPT_LIMITS. Hence we always need to compute the desired value
4242 * ourselves, instead of doing a rmw cycle (which might result in us clearing
4243 * all limits and the gpu stuck at whatever frequency it is at atm).
4244 */
Chris Wilson6917c7b2013-11-06 13:56:26 -02004245static u32 gen6_rps_limits(struct drm_i915_private *dev_priv, u8 val)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004246{
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01004247 u32 limits;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004248
Daniel Vetter20b46e52012-07-26 11:16:14 +02004249 /* Only set the down limit when we've reached the lowest level to avoid
4250 * getting more interrupts, otherwise leave this clear. This prevents a
4251 * race in the hw when coming out of rc6: There's a tiny window where
4252 * the hw runs at the minimal clock before selecting the desired
4253 * frequency, if the down threshold expires in that window we will not
4254 * receive a down interrupt. */
Ben Widawskyb39fb292014-03-19 18:31:11 -07004255 limits = dev_priv->rps.max_freq_softlimit << 24;
4256 if (val <= dev_priv->rps.min_freq_softlimit)
4257 limits |= dev_priv->rps.min_freq_softlimit << 16;
Daniel Vetter20b46e52012-07-26 11:16:14 +02004258
4259 return limits;
4260}
4261
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004262static void gen6_set_rps_thresholds(struct drm_i915_private *dev_priv, u8 val)
4263{
4264 int new_power;
4265
4266 new_power = dev_priv->rps.power;
4267 switch (dev_priv->rps.power) {
4268 case LOW_POWER:
Ben Widawskyb39fb292014-03-19 18:31:11 -07004269 if (val > dev_priv->rps.efficient_freq + 1 && val > dev_priv->rps.cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004270 new_power = BETWEEN;
4271 break;
4272
4273 case BETWEEN:
Ben Widawskyb39fb292014-03-19 18:31:11 -07004274 if (val <= dev_priv->rps.efficient_freq && val < dev_priv->rps.cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004275 new_power = LOW_POWER;
Ben Widawskyb39fb292014-03-19 18:31:11 -07004276 else if (val >= dev_priv->rps.rp0_freq && val > dev_priv->rps.cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004277 new_power = HIGH_POWER;
4278 break;
4279
4280 case HIGH_POWER:
Ben Widawskyb39fb292014-03-19 18:31:11 -07004281 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 +01004282 new_power = BETWEEN;
4283 break;
4284 }
4285 /* Max/min bins are special */
Ben Widawskyb39fb292014-03-19 18:31:11 -07004286 if (val == dev_priv->rps.min_freq_softlimit)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004287 new_power = LOW_POWER;
Ben Widawskyb39fb292014-03-19 18:31:11 -07004288 if (val == dev_priv->rps.max_freq_softlimit)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004289 new_power = HIGH_POWER;
4290 if (new_power == dev_priv->rps.power)
4291 return;
4292
4293 /* Note the units here are not exactly 1us, but 1280ns. */
4294 switch (new_power) {
4295 case LOW_POWER:
4296 /* Upclock if more than 95% busy over 16ms */
4297 I915_WRITE(GEN6_RP_UP_EI, 12500);
4298 I915_WRITE(GEN6_RP_UP_THRESHOLD, 11800);
4299
4300 /* Downclock if less than 85% busy over 32ms */
4301 I915_WRITE(GEN6_RP_DOWN_EI, 25000);
4302 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 21250);
4303
4304 I915_WRITE(GEN6_RP_CONTROL,
4305 GEN6_RP_MEDIA_TURBO |
4306 GEN6_RP_MEDIA_HW_NORMAL_MODE |
4307 GEN6_RP_MEDIA_IS_GFX |
4308 GEN6_RP_ENABLE |
4309 GEN6_RP_UP_BUSY_AVG |
4310 GEN6_RP_DOWN_IDLE_AVG);
4311 break;
4312
4313 case BETWEEN:
4314 /* Upclock if more than 90% busy over 13ms */
4315 I915_WRITE(GEN6_RP_UP_EI, 10250);
4316 I915_WRITE(GEN6_RP_UP_THRESHOLD, 9225);
4317
4318 /* Downclock if less than 75% busy over 32ms */
4319 I915_WRITE(GEN6_RP_DOWN_EI, 25000);
4320 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 18750);
4321
4322 I915_WRITE(GEN6_RP_CONTROL,
4323 GEN6_RP_MEDIA_TURBO |
4324 GEN6_RP_MEDIA_HW_NORMAL_MODE |
4325 GEN6_RP_MEDIA_IS_GFX |
4326 GEN6_RP_ENABLE |
4327 GEN6_RP_UP_BUSY_AVG |
4328 GEN6_RP_DOWN_IDLE_AVG);
4329 break;
4330
4331 case HIGH_POWER:
4332 /* Upclock if more than 85% busy over 10ms */
4333 I915_WRITE(GEN6_RP_UP_EI, 8000);
4334 I915_WRITE(GEN6_RP_UP_THRESHOLD, 6800);
4335
4336 /* Downclock if less than 60% busy over 32ms */
4337 I915_WRITE(GEN6_RP_DOWN_EI, 25000);
4338 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 15000);
4339
4340 I915_WRITE(GEN6_RP_CONTROL,
4341 GEN6_RP_MEDIA_TURBO |
4342 GEN6_RP_MEDIA_HW_NORMAL_MODE |
4343 GEN6_RP_MEDIA_IS_GFX |
4344 GEN6_RP_ENABLE |
4345 GEN6_RP_UP_BUSY_AVG |
4346 GEN6_RP_DOWN_IDLE_AVG);
4347 break;
4348 }
4349
4350 dev_priv->rps.power = new_power;
4351 dev_priv->rps.last_adj = 0;
4352}
4353
Chris Wilson2876ce72014-03-28 08:03:34 +00004354static u32 gen6_rps_pm_mask(struct drm_i915_private *dev_priv, u8 val)
4355{
4356 u32 mask = 0;
4357
4358 if (val > dev_priv->rps.min_freq_softlimit)
4359 mask |= GEN6_PM_RP_DOWN_THRESHOLD | GEN6_PM_RP_DOWN_TIMEOUT;
4360 if (val < dev_priv->rps.max_freq_softlimit)
4361 mask |= GEN6_PM_RP_UP_THRESHOLD;
4362
Chris Wilson7b3c29f2014-07-10 20:31:19 +01004363 mask |= dev_priv->pm_rps_events & (GEN6_PM_RP_DOWN_EI_EXPIRED | GEN6_PM_RP_UP_EI_EXPIRED);
4364 mask &= dev_priv->pm_rps_events;
4365
Chris Wilson2876ce72014-03-28 08:03:34 +00004366 /* IVB and SNB hard hangs on looping batchbuffer
4367 * if GEN6_PM_UP_EI_EXPIRED is masked.
4368 */
4369 if (INTEL_INFO(dev_priv->dev)->gen <= 7 && !IS_HASWELL(dev_priv->dev))
4370 mask |= GEN6_PM_RP_UP_EI_EXPIRED;
4371
Deepak Sbaccd452014-05-15 20:58:09 +03004372 if (IS_GEN8(dev_priv->dev))
4373 mask |= GEN8_PMINTR_REDIRECT_TO_NON_DISP;
4374
Chris Wilson2876ce72014-03-28 08:03:34 +00004375 return ~mask;
4376}
4377
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06004378/* gen6_set_rps is called to update the frequency request, but should also be
4379 * called when the range (min_delay and max_delay) is modified so that we can
4380 * update the GEN6_RP_INTERRUPT_LIMITS register accordingly. */
Daniel Vetter20b46e52012-07-26 11:16:14 +02004381void gen6_set_rps(struct drm_device *dev, u8 val)
4382{
4383 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01004384
Jesse Barnes4fc688c2012-11-02 11:14:01 -07004385 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Ben Widawskyb39fb292014-03-19 18:31:11 -07004386 WARN_ON(val > dev_priv->rps.max_freq_softlimit);
4387 WARN_ON(val < dev_priv->rps.min_freq_softlimit);
Daniel Vetter004777c2012-08-09 15:07:01 +02004388
Chris Wilsoneb64cad2014-03-27 08:24:20 +00004389 /* min/max delay may still have been modified so be sure to
4390 * write the limits value.
4391 */
4392 if (val != dev_priv->rps.cur_freq) {
4393 gen6_set_rps_thresholds(dev_priv, val);
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06004394
Ben Widawsky50e6a2a2014-03-31 17:16:43 -07004395 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Chris Wilsoneb64cad2014-03-27 08:24:20 +00004396 I915_WRITE(GEN6_RPNSWREQ,
4397 HSW_FREQUENCY(val));
4398 else
4399 I915_WRITE(GEN6_RPNSWREQ,
4400 GEN6_FREQUENCY(val) |
4401 GEN6_OFFSET(0) |
4402 GEN6_AGGRESSIVE_TURBO);
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06004403 }
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01004404
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01004405 /* Make sure we continue to get interrupts
4406 * until we hit the minimum or maximum frequencies.
4407 */
Chris Wilsoneb64cad2014-03-27 08:24:20 +00004408 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS, gen6_rps_limits(dev_priv, val));
Chris Wilson2876ce72014-03-28 08:03:34 +00004409 I915_WRITE(GEN6_PMINTRMSK, gen6_rps_pm_mask(dev_priv, val));
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01004410
Ben Widawskyd5570a72012-09-07 19:43:41 -07004411 POSTING_READ(GEN6_RPNSWREQ);
4412
Ben Widawskyb39fb292014-03-19 18:31:11 -07004413 dev_priv->rps.cur_freq = val;
Daniel Vetterbe2cde92012-08-30 13:26:48 +02004414 trace_intel_gpu_freq_change(val * 50);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004415}
4416
Deepak S76c3552f2014-01-30 23:08:16 +05304417/* vlv_set_rps_idle: Set the frequency to Rpn if Gfx clocks are down
4418 *
4419 * * If Gfx is Idle, then
4420 * 1. Mask Turbo interrupts
4421 * 2. Bring up Gfx clock
4422 * 3. Change the freq to Rpn and wait till P-Unit updates freq
4423 * 4. Clear the Force GFX CLK ON bit so that Gfx can down
4424 * 5. Unmask Turbo interrupts
4425*/
4426static void vlv_set_rps_idle(struct drm_i915_private *dev_priv)
4427{
Deepak S5549d252014-06-28 11:26:11 +05304428 struct drm_device *dev = dev_priv->dev;
4429
4430 /* Latest VLV doesn't need to force the gfx clock */
4431 if (dev->pdev->revision >= 0xd) {
4432 valleyview_set_rps(dev_priv->dev, dev_priv->rps.min_freq_softlimit);
4433 return;
4434 }
4435
Deepak S76c3552f2014-01-30 23:08:16 +05304436 /*
4437 * When we are idle. Drop to min voltage state.
4438 */
4439
Ben Widawskyb39fb292014-03-19 18:31:11 -07004440 if (dev_priv->rps.cur_freq <= dev_priv->rps.min_freq_softlimit)
Deepak S76c3552f2014-01-30 23:08:16 +05304441 return;
4442
4443 /* Mask turbo interrupt so that they will not come in between */
4444 I915_WRITE(GEN6_PMINTRMSK, 0xffffffff);
4445
Imre Deak650ad972014-04-18 16:35:02 +03004446 vlv_force_gfx_clock(dev_priv, true);
Deepak S76c3552f2014-01-30 23:08:16 +05304447
Ben Widawskyb39fb292014-03-19 18:31:11 -07004448 dev_priv->rps.cur_freq = dev_priv->rps.min_freq_softlimit;
Deepak S76c3552f2014-01-30 23:08:16 +05304449
4450 vlv_punit_write(dev_priv, PUNIT_REG_GPU_FREQ_REQ,
Ben Widawskyb39fb292014-03-19 18:31:11 -07004451 dev_priv->rps.min_freq_softlimit);
Deepak S76c3552f2014-01-30 23:08:16 +05304452
4453 if (wait_for(((vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS))
4454 & GENFREQSTATUS) == 0, 5))
4455 DRM_ERROR("timed out waiting for Punit\n");
4456
Imre Deak650ad972014-04-18 16:35:02 +03004457 vlv_force_gfx_clock(dev_priv, false);
Deepak S76c3552f2014-01-30 23:08:16 +05304458
Chris Wilson2876ce72014-03-28 08:03:34 +00004459 I915_WRITE(GEN6_PMINTRMSK,
4460 gen6_rps_pm_mask(dev_priv, dev_priv->rps.cur_freq));
Deepak S76c3552f2014-01-30 23:08:16 +05304461}
4462
Chris Wilsonb29c19b2013-09-25 17:34:56 +01004463void gen6_rps_idle(struct drm_i915_private *dev_priv)
4464{
Damien Lespiau691bb712013-12-12 14:36:36 +00004465 struct drm_device *dev = dev_priv->dev;
4466
Chris Wilsonb29c19b2013-09-25 17:34:56 +01004467 mutex_lock(&dev_priv->rps.hw_lock);
Chris Wilsonc0951f02013-10-10 21:58:50 +01004468 if (dev_priv->rps.enabled) {
Deepak S34638112014-06-28 11:26:26 +05304469 if (IS_CHERRYVIEW(dev))
4470 valleyview_set_rps(dev_priv->dev, dev_priv->rps.min_freq_softlimit);
4471 else if (IS_VALLEYVIEW(dev))
Deepak S76c3552f2014-01-30 23:08:16 +05304472 vlv_set_rps_idle(dev_priv);
Daniel Vetter7526ed72014-09-29 15:07:19 +02004473 else
Ben Widawskyb39fb292014-03-19 18:31:11 -07004474 gen6_set_rps(dev_priv->dev, dev_priv->rps.min_freq_softlimit);
Chris Wilsonc0951f02013-10-10 21:58:50 +01004475 dev_priv->rps.last_adj = 0;
4476 }
Chris Wilsonb29c19b2013-09-25 17:34:56 +01004477 mutex_unlock(&dev_priv->rps.hw_lock);
4478}
4479
4480void gen6_rps_boost(struct drm_i915_private *dev_priv)
4481{
Damien Lespiau691bb712013-12-12 14:36:36 +00004482 struct drm_device *dev = dev_priv->dev;
4483
Chris Wilsonb29c19b2013-09-25 17:34:56 +01004484 mutex_lock(&dev_priv->rps.hw_lock);
Chris Wilsonc0951f02013-10-10 21:58:50 +01004485 if (dev_priv->rps.enabled) {
Damien Lespiau691bb712013-12-12 14:36:36 +00004486 if (IS_VALLEYVIEW(dev))
Ben Widawskyb39fb292014-03-19 18:31:11 -07004487 valleyview_set_rps(dev_priv->dev, dev_priv->rps.max_freq_softlimit);
Daniel Vetter7526ed72014-09-29 15:07:19 +02004488 else
Ben Widawskyb39fb292014-03-19 18:31:11 -07004489 gen6_set_rps(dev_priv->dev, dev_priv->rps.max_freq_softlimit);
Chris Wilsonc0951f02013-10-10 21:58:50 +01004490 dev_priv->rps.last_adj = 0;
4491 }
Chris Wilsonb29c19b2013-09-25 17:34:56 +01004492 mutex_unlock(&dev_priv->rps.hw_lock);
4493}
4494
Jesse Barnes0a073b82013-04-17 15:54:58 -07004495void valleyview_set_rps(struct drm_device *dev, u8 val)
4496{
4497 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä7a670922013-06-25 19:21:06 +03004498
Jesse Barnes0a073b82013-04-17 15:54:58 -07004499 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Ben Widawskyb39fb292014-03-19 18:31:11 -07004500 WARN_ON(val > dev_priv->rps.max_freq_softlimit);
4501 WARN_ON(val < dev_priv->rps.min_freq_softlimit);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004502
Ville Syrjälä1c147622014-08-18 14:42:43 +03004503 if (WARN_ONCE(IS_CHERRYVIEW(dev) && (val & 1),
4504 "Odd GPU freq value\n"))
4505 val &= ~1;
4506
Ville Syrjälä67956862014-09-02 15:12:17 +03004507 if (val != dev_priv->rps.cur_freq) {
4508 DRM_DEBUG_DRIVER("GPU freq request from %d MHz (%u) to %d MHz (%u)\n",
4509 vlv_gpu_freq(dev_priv, dev_priv->rps.cur_freq),
4510 dev_priv->rps.cur_freq,
4511 vlv_gpu_freq(dev_priv, val), val);
4512
Chris Wilson2876ce72014-03-28 08:03:34 +00004513 vlv_punit_write(dev_priv, PUNIT_REG_GPU_FREQ_REQ, val);
Ville Syrjälä67956862014-09-02 15:12:17 +03004514 }
Jesse Barnes0a073b82013-04-17 15:54:58 -07004515
Imre Deak09c87db2014-04-03 20:02:42 +03004516 I915_WRITE(GEN6_PMINTRMSK, gen6_rps_pm_mask(dev_priv, val));
Jesse Barnes0a073b82013-04-17 15:54:58 -07004517
Ben Widawskyb39fb292014-03-19 18:31:11 -07004518 dev_priv->rps.cur_freq = val;
Ville Syrjälä2ec38152013-11-05 22:42:29 +02004519 trace_intel_gpu_freq_change(vlv_gpu_freq(dev_priv, val));
Jesse Barnes0a073b82013-04-17 15:54:58 -07004520}
4521
Ben Widawsky09610212014-05-15 20:58:08 +03004522static void gen8_disable_rps_interrupts(struct drm_device *dev)
4523{
4524 struct drm_i915_private *dev_priv = dev->dev_private;
4525
Daniel Vetter7526ed72014-09-29 15:07:19 +02004526 I915_WRITE(GEN6_PMINTRMSK, ~GEN8_PMINTR_REDIRECT_TO_NON_DISP);
4527 I915_WRITE(GEN8_GT_IER(2), I915_READ(GEN8_GT_IER(2)) &
4528 ~dev_priv->pm_rps_events);
4529 /* Complete PM interrupt masking here doesn't race with the rps work
4530 * item again unmasking PM interrupts because that is using a different
4531 * register (GEN8_GT_IMR(2)) to mask PM interrupts. The only risk is in
4532 * leaving stale bits in GEN8_GT_IIR(2) and GEN8_GT_IMR(2) which
4533 * gen8_enable_rps will clean up. */
Ben Widawsky09610212014-05-15 20:58:08 +03004534
Daniel Vetter7526ed72014-09-29 15:07:19 +02004535 spin_lock_irq(&dev_priv->irq_lock);
4536 dev_priv->rps.pm_iir = 0;
4537 spin_unlock_irq(&dev_priv->irq_lock);
4538
4539 I915_WRITE(GEN8_GT_IIR(2), dev_priv->pm_rps_events);
Ben Widawsky09610212014-05-15 20:58:08 +03004540}
4541
Zhe Wang20e49362014-11-04 17:07:05 +00004542static void gen9_disable_rps(struct drm_device *dev)
4543{
4544 struct drm_i915_private *dev_priv = dev->dev_private;
4545
4546 I915_WRITE(GEN6_RC_CONTROL, 0);
4547}
4548
Daniel Vetter44fc7d52013-07-12 22:43:27 +02004549static void gen6_disable_rps_interrupts(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004550{
4551 struct drm_i915_private *dev_priv = dev->dev_private;
4552
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004553 I915_WRITE(GEN6_PMINTRMSK, 0xffffffff);
Deepak Sa6706b42014-03-15 20:23:22 +05304554 I915_WRITE(GEN6_PMIER, I915_READ(GEN6_PMIER) &
4555 ~dev_priv->pm_rps_events);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004556 /* Complete PM interrupt masking here doesn't race with the rps work
4557 * item again unmasking PM interrupts because that is using a different
4558 * register (PMIMR) to mask PM interrupts. The only risk is in leaving
4559 * stale bits in PMIIR and PMIMR which gen6_enable_rps will clean up. */
4560
Daniel Vetter59cdb632013-07-04 23:35:28 +02004561 spin_lock_irq(&dev_priv->irq_lock);
Daniel Vetterc6a828d2012-08-08 23:35:35 +02004562 dev_priv->rps.pm_iir = 0;
Daniel Vetter59cdb632013-07-04 23:35:28 +02004563 spin_unlock_irq(&dev_priv->irq_lock);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004564
Deepak Sa6706b42014-03-15 20:23:22 +05304565 I915_WRITE(GEN6_PMIIR, dev_priv->pm_rps_events);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004566}
4567
Daniel Vetter44fc7d52013-07-12 22:43:27 +02004568static void gen6_disable_rps(struct drm_device *dev)
4569{
4570 struct drm_i915_private *dev_priv = dev->dev_private;
4571
4572 I915_WRITE(GEN6_RC_CONTROL, 0);
4573 I915_WRITE(GEN6_RPNSWREQ, 1 << 31);
4574
Ben Widawsky09610212014-05-15 20:58:08 +03004575 if (IS_BROADWELL(dev))
4576 gen8_disable_rps_interrupts(dev);
4577 else
4578 gen6_disable_rps_interrupts(dev);
Daniel Vetter44fc7d52013-07-12 22:43:27 +02004579}
4580
Deepak S38807742014-05-23 21:00:15 +05304581static void cherryview_disable_rps(struct drm_device *dev)
4582{
4583 struct drm_i915_private *dev_priv = dev->dev_private;
4584
4585 I915_WRITE(GEN6_RC_CONTROL, 0);
Deepak S3497a562014-07-10 13:16:26 +05304586
4587 gen8_disable_rps_interrupts(dev);
Deepak S38807742014-05-23 21:00:15 +05304588}
4589
Jesse Barnesd20d4f02013-04-23 10:09:28 -07004590static void valleyview_disable_rps(struct drm_device *dev)
4591{
4592 struct drm_i915_private *dev_priv = dev->dev_private;
4593
Deepak S98a2e5f2014-08-18 10:35:27 -07004594 /* we're doing forcewake before Disabling RC6,
4595 * This what the BIOS expects when going into suspend */
4596 gen6_gt_force_wake_get(dev_priv, FORCEWAKE_ALL);
4597
Jesse Barnesd20d4f02013-04-23 10:09:28 -07004598 I915_WRITE(GEN6_RC_CONTROL, 0);
Jesse Barnesd20d4f02013-04-23 10:09:28 -07004599
Deepak S98a2e5f2014-08-18 10:35:27 -07004600 gen6_gt_force_wake_put(dev_priv, FORCEWAKE_ALL);
4601
Daniel Vetter44fc7d52013-07-12 22:43:27 +02004602 gen6_disable_rps_interrupts(dev);
Jesse Barnesd20d4f02013-04-23 10:09:28 -07004603}
4604
Ben Widawskydc39fff2013-10-18 12:32:07 -07004605static void intel_print_rc6_info(struct drm_device *dev, u32 mode)
4606{
Imre Deak91ca6892014-04-14 20:24:25 +03004607 if (IS_VALLEYVIEW(dev)) {
4608 if (mode & (GEN7_RC_CTL_TO_MODE | GEN6_RC_CTL_EI_MODE(1)))
4609 mode = GEN6_RC_CTL_RC6_ENABLE;
4610 else
4611 mode = 0;
4612 }
Rodrigo Vivi58abf1d2014-10-07 07:06:50 -07004613 if (HAS_RC6p(dev))
4614 DRM_DEBUG_KMS("Enabling RC6 states: RC6 %s RC6p %s RC6pp %s\n",
4615 (mode & GEN6_RC_CTL_RC6_ENABLE) ? "on" : "off",
4616 (mode & GEN6_RC_CTL_RC6p_ENABLE) ? "on" : "off",
4617 (mode & GEN6_RC_CTL_RC6pp_ENABLE) ? "on" : "off");
4618
4619 else
4620 DRM_DEBUG_KMS("Enabling RC6 states: RC6 %s\n",
4621 (mode & GEN6_RC_CTL_RC6_ENABLE) ? "on" : "off");
Ben Widawskydc39fff2013-10-18 12:32:07 -07004622}
4623
Imre Deake6069ca2014-04-18 16:01:02 +03004624static int sanitize_rc6_option(const struct drm_device *dev, int enable_rc6)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004625{
Damien Lespiaueb4926e2013-06-07 17:41:14 +01004626 /* No RC6 before Ironlake */
4627 if (INTEL_INFO(dev)->gen < 5)
4628 return 0;
4629
Imre Deake6069ca2014-04-18 16:01:02 +03004630 /* RC6 is only on Ironlake mobile not on desktop */
4631 if (INTEL_INFO(dev)->gen == 5 && !IS_IRONLAKE_M(dev))
4632 return 0;
4633
Daniel Vetter456470e2012-08-08 23:35:40 +02004634 /* Respect the kernel parameter if it is set */
Imre Deake6069ca2014-04-18 16:01:02 +03004635 if (enable_rc6 >= 0) {
4636 int mask;
4637
Rodrigo Vivi58abf1d2014-10-07 07:06:50 -07004638 if (HAS_RC6p(dev))
Imre Deake6069ca2014-04-18 16:01:02 +03004639 mask = INTEL_RC6_ENABLE | INTEL_RC6p_ENABLE |
4640 INTEL_RC6pp_ENABLE;
4641 else
4642 mask = INTEL_RC6_ENABLE;
4643
4644 if ((enable_rc6 & mask) != enable_rc6)
Daniel Vetter8dfd1f02014-08-04 11:15:56 +02004645 DRM_DEBUG_KMS("Adjusting RC6 mask to %d (requested %d, valid %d)\n",
4646 enable_rc6 & mask, enable_rc6, mask);
Imre Deake6069ca2014-04-18 16:01:02 +03004647
4648 return enable_rc6 & mask;
4649 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004650
Chris Wilson6567d742012-11-10 10:00:06 +00004651 /* Disable RC6 on Ironlake */
4652 if (INTEL_INFO(dev)->gen == 5)
4653 return 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004654
Ben Widawsky8bade1a2014-01-28 20:25:39 -08004655 if (IS_IVYBRIDGE(dev))
Ben Widawskycca84a12014-01-28 20:25:38 -08004656 return (INTEL_RC6_ENABLE | INTEL_RC6p_ENABLE);
Ben Widawsky8bade1a2014-01-28 20:25:39 -08004657
4658 return INTEL_RC6_ENABLE;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004659}
4660
Imre Deake6069ca2014-04-18 16:01:02 +03004661int intel_enable_rc6(const struct drm_device *dev)
4662{
4663 return i915.enable_rc6;
4664}
4665
Ben Widawsky09610212014-05-15 20:58:08 +03004666static void gen8_enable_rps_interrupts(struct drm_device *dev)
4667{
4668 struct drm_i915_private *dev_priv = dev->dev_private;
4669
4670 spin_lock_irq(&dev_priv->irq_lock);
4671 WARN_ON(dev_priv->rps.pm_iir);
Imre Deaka72fbc32014-11-05 20:48:31 +02004672 gen6_enable_pm_irq(dev_priv, dev_priv->pm_rps_events);
Ben Widawsky09610212014-05-15 20:58:08 +03004673 I915_WRITE(GEN8_GT_IIR(2), dev_priv->pm_rps_events);
4674 spin_unlock_irq(&dev_priv->irq_lock);
4675}
4676
Daniel Vetter44fc7d52013-07-12 22:43:27 +02004677static void gen6_enable_rps_interrupts(struct drm_device *dev)
4678{
4679 struct drm_i915_private *dev_priv = dev->dev_private;
4680
4681 spin_lock_irq(&dev_priv->irq_lock);
Daniel Vettera0b33352013-07-04 23:35:34 +02004682 WARN_ON(dev_priv->rps.pm_iir);
Daniel Vetter480c8032014-07-16 09:49:40 +02004683 gen6_enable_pm_irq(dev_priv, dev_priv->pm_rps_events);
Deepak Sa6706b42014-03-15 20:23:22 +05304684 I915_WRITE(GEN6_PMIIR, dev_priv->pm_rps_events);
Daniel Vetter44fc7d52013-07-12 22:43:27 +02004685 spin_unlock_irq(&dev_priv->irq_lock);
Daniel Vetter44fc7d52013-07-12 22:43:27 +02004686}
4687
Ben Widawsky3280e8b2014-03-31 17:16:42 -07004688static void parse_rp_state_cap(struct drm_i915_private *dev_priv, u32 rp_state_cap)
4689{
4690 /* All of these values are in units of 50MHz */
4691 dev_priv->rps.cur_freq = 0;
4692 /* static values from HW: RP0 < RPe < RP1 < RPn (min_freq) */
4693 dev_priv->rps.rp1_freq = (rp_state_cap >> 8) & 0xff;
4694 dev_priv->rps.rp0_freq = (rp_state_cap >> 0) & 0xff;
4695 dev_priv->rps.min_freq = (rp_state_cap >> 16) & 0xff;
4696 /* XXX: only BYT has a special efficient freq */
4697 dev_priv->rps.efficient_freq = dev_priv->rps.rp1_freq;
4698 /* hw_max = RP0 until we check for overclocking */
4699 dev_priv->rps.max_freq = dev_priv->rps.rp0_freq;
4700
4701 /* Preserve min/max settings in case of re-init */
4702 if (dev_priv->rps.max_freq_softlimit == 0)
4703 dev_priv->rps.max_freq_softlimit = dev_priv->rps.max_freq;
4704
4705 if (dev_priv->rps.min_freq_softlimit == 0)
4706 dev_priv->rps.min_freq_softlimit = dev_priv->rps.min_freq;
4707}
4708
Zhe Wang20e49362014-11-04 17:07:05 +00004709static void gen9_enable_rps(struct drm_device *dev)
4710{
4711 struct drm_i915_private *dev_priv = dev->dev_private;
4712 struct intel_engine_cs *ring;
4713 uint32_t rc6_mask = 0;
4714 int unused;
4715
4716 /* 1a: Software RC state - RC0 */
4717 I915_WRITE(GEN6_RC_STATE, 0);
4718
4719 /* 1b: Get forcewake during program sequence. Although the driver
4720 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
4721 gen6_gt_force_wake_get(dev_priv, FORCEWAKE_ALL);
4722
4723 /* 2a: Disable RC states. */
4724 I915_WRITE(GEN6_RC_CONTROL, 0);
4725
4726 /* 2b: Program RC6 thresholds.*/
4727 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 54 << 16);
4728 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
4729 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
4730 for_each_ring(ring, dev_priv, unused)
4731 I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
4732 I915_WRITE(GEN6_RC_SLEEP, 0);
4733 I915_WRITE(GEN6_RC6_THRESHOLD, 37500); /* 37.5/125ms per EI */
4734
4735 /* 3a: Enable RC6 */
4736 if (intel_enable_rc6(dev) & INTEL_RC6_ENABLE)
4737 rc6_mask = GEN6_RC_CTL_RC6_ENABLE;
4738 DRM_INFO("RC6 %s\n", (rc6_mask & GEN6_RC_CTL_RC6_ENABLE) ?
4739 "on" : "off");
4740 I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
4741 GEN6_RC_CTL_EI_MODE(1) |
4742 rc6_mask);
4743
4744 gen6_gt_force_wake_put(dev_priv, FORCEWAKE_ALL);
4745
4746}
4747
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004748static void gen8_enable_rps(struct drm_device *dev)
4749{
4750 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01004751 struct intel_engine_cs *ring;
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004752 uint32_t rc6_mask = 0, rp_state_cap;
4753 int unused;
4754
4755 /* 1a: Software RC state - RC0 */
4756 I915_WRITE(GEN6_RC_STATE, 0);
4757
4758 /* 1c & 1d: Get forcewake during program sequence. Although the driver
4759 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
Deepak Sc8d9a592013-11-23 14:55:42 +05304760 gen6_gt_force_wake_get(dev_priv, FORCEWAKE_ALL);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004761
4762 /* 2a: Disable RC states. */
4763 I915_WRITE(GEN6_RC_CONTROL, 0);
4764
4765 rp_state_cap = I915_READ(GEN6_RP_STATE_CAP);
Ben Widawsky3280e8b2014-03-31 17:16:42 -07004766 parse_rp_state_cap(dev_priv, rp_state_cap);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004767
4768 /* 2b: Program RC6 thresholds.*/
4769 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16);
4770 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
4771 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
4772 for_each_ring(ring, dev_priv, unused)
4773 I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
4774 I915_WRITE(GEN6_RC_SLEEP, 0);
Tom O'Rourke0d68b252014-04-09 11:44:06 -07004775 if (IS_BROADWELL(dev))
4776 I915_WRITE(GEN6_RC6_THRESHOLD, 625); /* 800us/1.28 for TO */
4777 else
4778 I915_WRITE(GEN6_RC6_THRESHOLD, 50000); /* 50/125ms per EI */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004779
4780 /* 3: Enable RC6 */
4781 if (intel_enable_rc6(dev) & INTEL_RC6_ENABLE)
4782 rc6_mask = GEN6_RC_CTL_RC6_ENABLE;
Ben Widawskyabbf9d22014-01-28 20:25:41 -08004783 intel_print_rc6_info(dev, rc6_mask);
Tom O'Rourke0d68b252014-04-09 11:44:06 -07004784 if (IS_BROADWELL(dev))
4785 I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
4786 GEN7_RC_CTL_TO_MODE |
4787 rc6_mask);
4788 else
4789 I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
4790 GEN6_RC_CTL_EI_MODE(1) |
4791 rc6_mask);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004792
4793 /* 4 Program defaults and thresholds for RPS*/
Ben Widawskyf9bdc582014-03-31 17:16:41 -07004794 I915_WRITE(GEN6_RPNSWREQ,
4795 HSW_FREQUENCY(dev_priv->rps.rp1_freq));
4796 I915_WRITE(GEN6_RC_VIDEO_FREQ,
4797 HSW_FREQUENCY(dev_priv->rps.rp1_freq));
Daniel Vetter7526ed72014-09-29 15:07:19 +02004798 /* NB: Docs say 1s, and 1000000 - which aren't equivalent */
4799 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 100000000 / 128); /* 1 second timeout */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004800
Daniel Vetter7526ed72014-09-29 15:07:19 +02004801 /* Docs recommend 900MHz, and 300 MHz respectively */
4802 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS,
4803 dev_priv->rps.max_freq_softlimit << 24 |
4804 dev_priv->rps.min_freq_softlimit << 16);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004805
Daniel Vetter7526ed72014-09-29 15:07:19 +02004806 I915_WRITE(GEN6_RP_UP_THRESHOLD, 7600000 / 128); /* 76ms busyness per EI, 90% */
4807 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 31300000 / 128); /* 313ms busyness per EI, 70%*/
4808 I915_WRITE(GEN6_RP_UP_EI, 66000); /* 84.48ms, XXX: random? */
4809 I915_WRITE(GEN6_RP_DOWN_EI, 350000); /* 448ms, XXX: random? */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004810
Daniel Vetter7526ed72014-09-29 15:07:19 +02004811 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004812
4813 /* 5: Enable RPS */
Daniel Vetter7526ed72014-09-29 15:07:19 +02004814 I915_WRITE(GEN6_RP_CONTROL,
4815 GEN6_RP_MEDIA_TURBO |
4816 GEN6_RP_MEDIA_HW_NORMAL_MODE |
4817 GEN6_RP_MEDIA_IS_GFX |
4818 GEN6_RP_ENABLE |
4819 GEN6_RP_UP_BUSY_AVG |
4820 GEN6_RP_DOWN_IDLE_AVG);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004821
Daniel Vetter7526ed72014-09-29 15:07:19 +02004822 /* 6: Ring frequency + overclocking (our driver does this later */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004823
4824 gen6_set_rps(dev, (I915_READ(GEN6_GT_PERF_STATUS) & 0xff00) >> 8);
Daniel Vetter7526ed72014-09-29 15:07:19 +02004825
4826 gen8_enable_rps_interrupts(dev);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004827
Deepak Sc8d9a592013-11-23 14:55:42 +05304828 gen6_gt_force_wake_put(dev_priv, FORCEWAKE_ALL);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004829}
4830
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02004831static void gen6_enable_rps(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004832{
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02004833 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01004834 struct intel_engine_cs *ring;
Ben Widawsky2a5913a2014-03-19 18:31:13 -07004835 u32 rp_state_cap;
Ben Widawskyd060c162014-03-19 18:31:08 -07004836 u32 rc6vids, pcu_mbox = 0, rc6_mask = 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004837 u32 gtfifodbg;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004838 int rc6_mode;
Ben Widawsky42c05262012-09-26 10:34:00 -07004839 int i, ret;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004840
Jesse Barnes4fc688c2012-11-02 11:14:01 -07004841 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02004842
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004843 /* Here begins a magic sequence of register writes to enable
4844 * auto-downclocking.
4845 *
4846 * Perhaps there might be some value in exposing these to
4847 * userspace...
4848 */
4849 I915_WRITE(GEN6_RC_STATE, 0);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004850
4851 /* Clear the DBG now so we don't confuse earlier errors */
4852 if ((gtfifodbg = I915_READ(GTFIFODBG))) {
4853 DRM_ERROR("GT fifo had a previous error %x\n", gtfifodbg);
4854 I915_WRITE(GTFIFODBG, gtfifodbg);
4855 }
4856
Deepak Sc8d9a592013-11-23 14:55:42 +05304857 gen6_gt_force_wake_get(dev_priv, FORCEWAKE_ALL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004858
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01004859 rp_state_cap = I915_READ(GEN6_RP_STATE_CAP);
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01004860
Ben Widawsky3280e8b2014-03-31 17:16:42 -07004861 parse_rp_state_cap(dev_priv, rp_state_cap);
Jeff McGeedd0a1aa2014-02-04 11:32:31 -06004862
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004863 /* disable the counters and set deterministic thresholds */
4864 I915_WRITE(GEN6_RC_CONTROL, 0);
4865
4866 I915_WRITE(GEN6_RC1_WAKE_RATE_LIMIT, 1000 << 16);
4867 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16 | 30);
4868 I915_WRITE(GEN6_RC6pp_WAKE_RATE_LIMIT, 30);
4869 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
4870 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
4871
Chris Wilsonb4519512012-05-11 14:29:30 +01004872 for_each_ring(ring, dev_priv, i)
4873 I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004874
4875 I915_WRITE(GEN6_RC_SLEEP, 0);
4876 I915_WRITE(GEN6_RC1e_THRESHOLD, 1000);
Daniel Vetter29c78f62013-11-16 16:04:26 +01004877 if (IS_IVYBRIDGE(dev))
Stéphane Marchesin351aa562013-08-13 11:55:17 -07004878 I915_WRITE(GEN6_RC6_THRESHOLD, 125000);
4879 else
4880 I915_WRITE(GEN6_RC6_THRESHOLD, 50000);
Stéphane Marchesin0920a482013-01-29 19:41:59 -08004881 I915_WRITE(GEN6_RC6p_THRESHOLD, 150000);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004882 I915_WRITE(GEN6_RC6pp_THRESHOLD, 64000); /* unused */
4883
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03004884 /* Check if we are enabling RC6 */
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004885 rc6_mode = intel_enable_rc6(dev_priv->dev);
4886 if (rc6_mode & INTEL_RC6_ENABLE)
4887 rc6_mask |= GEN6_RC_CTL_RC6_ENABLE;
4888
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03004889 /* We don't use those on Haswell */
4890 if (!IS_HASWELL(dev)) {
4891 if (rc6_mode & INTEL_RC6p_ENABLE)
4892 rc6_mask |= GEN6_RC_CTL_RC6p_ENABLE;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004893
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03004894 if (rc6_mode & INTEL_RC6pp_ENABLE)
4895 rc6_mask |= GEN6_RC_CTL_RC6pp_ENABLE;
4896 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004897
Ben Widawskydc39fff2013-10-18 12:32:07 -07004898 intel_print_rc6_info(dev, rc6_mask);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004899
4900 I915_WRITE(GEN6_RC_CONTROL,
4901 rc6_mask |
4902 GEN6_RC_CTL_EI_MODE(1) |
4903 GEN6_RC_CTL_HW_ENABLE);
4904
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004905 /* Power down if completely idle for over 50ms */
4906 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 50000);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004907 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004908
Ben Widawsky42c05262012-09-26 10:34:00 -07004909 ret = sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_MIN_FREQ_TABLE, 0);
Ben Widawskyd060c162014-03-19 18:31:08 -07004910 if (ret)
Ben Widawsky42c05262012-09-26 10:34:00 -07004911 DRM_DEBUG_DRIVER("Failed to set the min frequency\n");
Ben Widawskyd060c162014-03-19 18:31:08 -07004912
4913 ret = sandybridge_pcode_read(dev_priv, GEN6_READ_OC_PARAMS, &pcu_mbox);
4914 if (!ret && (pcu_mbox & (1<<31))) { /* OC supported */
4915 DRM_DEBUG_DRIVER("Overclocking supported. Max: %dMHz, Overclock max: %dMHz\n",
Ben Widawskyb39fb292014-03-19 18:31:11 -07004916 (dev_priv->rps.max_freq_softlimit & 0xff) * 50,
Ben Widawskyd060c162014-03-19 18:31:08 -07004917 (pcu_mbox & 0xff) * 50);
Ben Widawskyb39fb292014-03-19 18:31:11 -07004918 dev_priv->rps.max_freq = pcu_mbox & 0xff;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004919 }
4920
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004921 dev_priv->rps.power = HIGH_POWER; /* force a reset */
Ben Widawskyb39fb292014-03-19 18:31:11 -07004922 gen6_set_rps(dev_priv->dev, dev_priv->rps.min_freq_softlimit);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004923
Daniel Vetter44fc7d52013-07-12 22:43:27 +02004924 gen6_enable_rps_interrupts(dev);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004925
Ben Widawsky31643d52012-09-26 10:34:01 -07004926 rc6vids = 0;
4927 ret = sandybridge_pcode_read(dev_priv, GEN6_PCODE_READ_RC6VIDS, &rc6vids);
4928 if (IS_GEN6(dev) && ret) {
4929 DRM_DEBUG_DRIVER("Couldn't check for BIOS workaround\n");
4930 } else if (IS_GEN6(dev) && (GEN6_DECODE_RC6_VID(rc6vids & 0xff) < 450)) {
4931 DRM_DEBUG_DRIVER("You should update your BIOS. Correcting minimum rc6 voltage (%dmV->%dmV)\n",
4932 GEN6_DECODE_RC6_VID(rc6vids & 0xff), 450);
4933 rc6vids &= 0xffff00;
4934 rc6vids |= GEN6_ENCODE_RC6_VID(450);
4935 ret = sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_RC6VIDS, rc6vids);
4936 if (ret)
4937 DRM_ERROR("Couldn't fix incorrect rc6 voltage\n");
4938 }
4939
Deepak Sc8d9a592013-11-23 14:55:42 +05304940 gen6_gt_force_wake_put(dev_priv, FORCEWAKE_ALL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004941}
4942
Imre Deakc2bc2fc2014-04-18 16:16:23 +03004943static void __gen6_update_ring_freq(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004944{
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02004945 struct drm_i915_private *dev_priv = dev->dev_private;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004946 int min_freq = 15;
Chris Wilson3ebecd02013-04-12 19:10:13 +01004947 unsigned int gpu_freq;
4948 unsigned int max_ia_freq, min_ring_freq;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004949 int scaling_factor = 180;
Ben Widawskyeda79642013-10-07 17:15:48 -03004950 struct cpufreq_policy *policy;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004951
Jesse Barnes4fc688c2012-11-02 11:14:01 -07004952 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02004953
Ben Widawskyeda79642013-10-07 17:15:48 -03004954 policy = cpufreq_cpu_get(0);
4955 if (policy) {
4956 max_ia_freq = policy->cpuinfo.max_freq;
4957 cpufreq_cpu_put(policy);
4958 } else {
4959 /*
4960 * Default to measured freq if none found, PCU will ensure we
4961 * don't go over
4962 */
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004963 max_ia_freq = tsc_khz;
Ben Widawskyeda79642013-10-07 17:15:48 -03004964 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004965
4966 /* Convert from kHz to MHz */
4967 max_ia_freq /= 1000;
4968
Ben Widawsky153b4b952013-10-22 22:05:09 -07004969 min_ring_freq = I915_READ(DCLK) & 0xf;
Ben Widawskyf6aca452013-10-02 09:25:02 -07004970 /* convert DDR frequency from units of 266.6MHz to bandwidth */
4971 min_ring_freq = mult_frac(min_ring_freq, 8, 3);
Chris Wilson3ebecd02013-04-12 19:10:13 +01004972
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004973 /*
4974 * For each potential GPU frequency, load a ring frequency we'd like
4975 * to use for memory access. We do this by specifying the IA frequency
4976 * the PCU should use as a reference to determine the ring frequency.
4977 */
Ben Widawskyb39fb292014-03-19 18:31:11 -07004978 for (gpu_freq = dev_priv->rps.max_freq_softlimit; gpu_freq >= dev_priv->rps.min_freq_softlimit;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004979 gpu_freq--) {
Ben Widawskyb39fb292014-03-19 18:31:11 -07004980 int diff = dev_priv->rps.max_freq_softlimit - gpu_freq;
Chris Wilson3ebecd02013-04-12 19:10:13 +01004981 unsigned int ia_freq = 0, ring_freq = 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004982
Ben Widawsky46c764d2013-11-02 21:07:49 -07004983 if (INTEL_INFO(dev)->gen >= 8) {
4984 /* max(2 * GT, DDR). NB: GT is 50MHz units */
4985 ring_freq = max(min_ring_freq, gpu_freq);
4986 } else if (IS_HASWELL(dev)) {
Ben Widawskyf6aca452013-10-02 09:25:02 -07004987 ring_freq = mult_frac(gpu_freq, 5, 4);
Chris Wilson3ebecd02013-04-12 19:10:13 +01004988 ring_freq = max(min_ring_freq, ring_freq);
4989 /* leave ia_freq as the default, chosen by cpufreq */
4990 } else {
4991 /* On older processors, there is no separate ring
4992 * clock domain, so in order to boost the bandwidth
4993 * of the ring, we need to upclock the CPU (ia_freq).
4994 *
4995 * For GPU frequencies less than 750MHz,
4996 * just use the lowest ring freq.
4997 */
4998 if (gpu_freq < min_freq)
4999 ia_freq = 800;
5000 else
5001 ia_freq = max_ia_freq - ((diff * scaling_factor) / 2);
5002 ia_freq = DIV_ROUND_CLOSEST(ia_freq, 100);
5003 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005004
Ben Widawsky42c05262012-09-26 10:34:00 -07005005 sandybridge_pcode_write(dev_priv,
5006 GEN6_PCODE_WRITE_MIN_FREQ_TABLE,
Chris Wilson3ebecd02013-04-12 19:10:13 +01005007 ia_freq << GEN6_PCODE_FREQ_IA_RATIO_SHIFT |
5008 ring_freq << GEN6_PCODE_FREQ_RING_RATIO_SHIFT |
5009 gpu_freq);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005010 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005011}
5012
Imre Deakc2bc2fc2014-04-18 16:16:23 +03005013void gen6_update_ring_freq(struct drm_device *dev)
5014{
5015 struct drm_i915_private *dev_priv = dev->dev_private;
5016
5017 if (INTEL_INFO(dev)->gen < 6 || IS_VALLEYVIEW(dev))
5018 return;
5019
5020 mutex_lock(&dev_priv->rps.hw_lock);
5021 __gen6_update_ring_freq(dev);
5022 mutex_unlock(&dev_priv->rps.hw_lock);
5023}
5024
Ville Syrjälä03af2042014-06-28 02:03:53 +03005025static int cherryview_rps_max_freq(struct drm_i915_private *dev_priv)
Deepak S2b6b3a02014-05-27 15:59:30 +05305026{
5027 u32 val, rp0;
5028
5029 val = vlv_punit_read(dev_priv, PUNIT_GPU_STATUS_REG);
5030 rp0 = (val >> PUNIT_GPU_STATUS_MAX_FREQ_SHIFT) & PUNIT_GPU_STATUS_MAX_FREQ_MASK;
5031
5032 return rp0;
5033}
5034
5035static int cherryview_rps_rpe_freq(struct drm_i915_private *dev_priv)
5036{
5037 u32 val, rpe;
5038
5039 val = vlv_punit_read(dev_priv, PUNIT_GPU_DUTYCYCLE_REG);
5040 rpe = (val >> PUNIT_GPU_DUTYCYCLE_RPE_FREQ_SHIFT) & PUNIT_GPU_DUTYCYCLE_RPE_FREQ_MASK;
5041
5042 return rpe;
5043}
5044
Deepak S7707df42014-07-12 18:46:14 +05305045static int cherryview_rps_guar_freq(struct drm_i915_private *dev_priv)
5046{
5047 u32 val, rp1;
5048
5049 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
5050 rp1 = (val >> PUNIT_GPU_STATUS_MAX_FREQ_SHIFT) & PUNIT_GPU_STATUS_MAX_FREQ_MASK;
5051
5052 return rp1;
5053}
5054
Ville Syrjälä03af2042014-06-28 02:03:53 +03005055static int cherryview_rps_min_freq(struct drm_i915_private *dev_priv)
Deepak S2b6b3a02014-05-27 15:59:30 +05305056{
5057 u32 val, rpn;
5058
5059 val = vlv_punit_read(dev_priv, PUNIT_GPU_STATUS_REG);
5060 rpn = (val >> PUNIT_GPU_STATIS_GFX_MIN_FREQ_SHIFT) & PUNIT_GPU_STATUS_GFX_MIN_FREQ_MASK;
5061 return rpn;
5062}
5063
Deepak Sf8f2b002014-07-10 13:16:21 +05305064static int valleyview_rps_guar_freq(struct drm_i915_private *dev_priv)
5065{
5066 u32 val, rp1;
5067
5068 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FREQ_FUSE);
5069
5070 rp1 = (val & FB_GFX_FGUARANTEED_FREQ_FUSE_MASK) >> FB_GFX_FGUARANTEED_FREQ_FUSE_SHIFT;
5071
5072 return rp1;
5073}
5074
Ville Syrjälä03af2042014-06-28 02:03:53 +03005075static int valleyview_rps_max_freq(struct drm_i915_private *dev_priv)
Jesse Barnes0a073b82013-04-17 15:54:58 -07005076{
5077 u32 val, rp0;
5078
Jani Nikula64936252013-05-22 15:36:20 +03005079 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FREQ_FUSE);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005080
5081 rp0 = (val & FB_GFX_MAX_FREQ_FUSE_MASK) >> FB_GFX_MAX_FREQ_FUSE_SHIFT;
5082 /* Clamp to max */
5083 rp0 = min_t(u32, rp0, 0xea);
5084
5085 return rp0;
5086}
5087
5088static int valleyview_rps_rpe_freq(struct drm_i915_private *dev_priv)
5089{
5090 u32 val, rpe;
5091
Jani Nikula64936252013-05-22 15:36:20 +03005092 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FMAX_FUSE_LO);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005093 rpe = (val & FB_FMAX_VMIN_FREQ_LO_MASK) >> FB_FMAX_VMIN_FREQ_LO_SHIFT;
Jani Nikula64936252013-05-22 15:36:20 +03005094 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FMAX_FUSE_HI);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005095 rpe |= (val & FB_FMAX_VMIN_FREQ_HI_MASK) << 5;
5096
5097 return rpe;
5098}
5099
Ville Syrjälä03af2042014-06-28 02:03:53 +03005100static int valleyview_rps_min_freq(struct drm_i915_private *dev_priv)
Jesse Barnes0a073b82013-04-17 15:54:58 -07005101{
Jani Nikula64936252013-05-22 15:36:20 +03005102 return vlv_punit_read(dev_priv, PUNIT_REG_GPU_LFM) & 0xff;
Jesse Barnes0a073b82013-04-17 15:54:58 -07005103}
5104
Imre Deakae484342014-03-31 15:10:44 +03005105/* Check that the pctx buffer wasn't move under us. */
5106static void valleyview_check_pctx(struct drm_i915_private *dev_priv)
5107{
5108 unsigned long pctx_addr = I915_READ(VLV_PCBR) & ~4095;
5109
5110 WARN_ON(pctx_addr != dev_priv->mm.stolen_base +
5111 dev_priv->vlv_pctx->stolen->start);
5112}
5113
Deepak S38807742014-05-23 21:00:15 +05305114
5115/* Check that the pcbr address is not empty. */
5116static void cherryview_check_pctx(struct drm_i915_private *dev_priv)
5117{
5118 unsigned long pctx_addr = I915_READ(VLV_PCBR) & ~4095;
5119
5120 WARN_ON((pctx_addr >> VLV_PCBR_ADDR_SHIFT) == 0);
5121}
5122
5123static void cherryview_setup_pctx(struct drm_device *dev)
5124{
5125 struct drm_i915_private *dev_priv = dev->dev_private;
5126 unsigned long pctx_paddr, paddr;
5127 struct i915_gtt *gtt = &dev_priv->gtt;
5128 u32 pcbr;
5129 int pctx_size = 32*1024;
5130
5131 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
5132
5133 pcbr = I915_READ(VLV_PCBR);
5134 if ((pcbr >> VLV_PCBR_ADDR_SHIFT) == 0) {
5135 paddr = (dev_priv->mm.stolen_base +
5136 (gtt->stolen_size - pctx_size));
5137
5138 pctx_paddr = (paddr & (~4095));
5139 I915_WRITE(VLV_PCBR, pctx_paddr);
5140 }
5141}
5142
Jesse Barnesc9cddff2013-05-08 10:45:13 -07005143static void valleyview_setup_pctx(struct drm_device *dev)
5144{
5145 struct drm_i915_private *dev_priv = dev->dev_private;
5146 struct drm_i915_gem_object *pctx;
5147 unsigned long pctx_paddr;
5148 u32 pcbr;
5149 int pctx_size = 24*1024;
5150
Imre Deak17b0c1f2014-02-11 21:39:06 +02005151 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
5152
Jesse Barnesc9cddff2013-05-08 10:45:13 -07005153 pcbr = I915_READ(VLV_PCBR);
5154 if (pcbr) {
5155 /* BIOS set it up already, grab the pre-alloc'd space */
5156 int pcbr_offset;
5157
5158 pcbr_offset = (pcbr & (~4095)) - dev_priv->mm.stolen_base;
5159 pctx = i915_gem_object_create_stolen_for_preallocated(dev_priv->dev,
5160 pcbr_offset,
Daniel Vetter190d6cd2013-07-04 13:06:28 +02005161 I915_GTT_OFFSET_NONE,
Jesse Barnesc9cddff2013-05-08 10:45:13 -07005162 pctx_size);
5163 goto out;
5164 }
5165
5166 /*
5167 * From the Gunit register HAS:
5168 * The Gfx driver is expected to program this register and ensure
5169 * proper allocation within Gfx stolen memory. For example, this
5170 * register should be programmed such than the PCBR range does not
5171 * overlap with other ranges, such as the frame buffer, protected
5172 * memory, or any other relevant ranges.
5173 */
5174 pctx = i915_gem_object_create_stolen(dev, pctx_size);
5175 if (!pctx) {
5176 DRM_DEBUG("not enough stolen space for PCTX, disabling\n");
5177 return;
5178 }
5179
5180 pctx_paddr = dev_priv->mm.stolen_base + pctx->stolen->start;
5181 I915_WRITE(VLV_PCBR, pctx_paddr);
5182
5183out:
5184 dev_priv->vlv_pctx = pctx;
5185}
5186
Imre Deakae484342014-03-31 15:10:44 +03005187static void valleyview_cleanup_pctx(struct drm_device *dev)
5188{
5189 struct drm_i915_private *dev_priv = dev->dev_private;
5190
5191 if (WARN_ON(!dev_priv->vlv_pctx))
5192 return;
5193
5194 drm_gem_object_unreference(&dev_priv->vlv_pctx->base);
5195 dev_priv->vlv_pctx = NULL;
5196}
5197
Imre Deak4e805192014-04-14 20:24:41 +03005198static void valleyview_init_gt_powersave(struct drm_device *dev)
5199{
5200 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03005201 u32 val;
Imre Deak4e805192014-04-14 20:24:41 +03005202
5203 valleyview_setup_pctx(dev);
5204
5205 mutex_lock(&dev_priv->rps.hw_lock);
5206
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03005207 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
5208 switch ((val >> 6) & 3) {
5209 case 0:
5210 case 1:
5211 dev_priv->mem_freq = 800;
5212 break;
5213 case 2:
5214 dev_priv->mem_freq = 1066;
5215 break;
5216 case 3:
5217 dev_priv->mem_freq = 1333;
5218 break;
5219 }
5220 DRM_DEBUG_DRIVER("DDR speed: %d MHz", dev_priv->mem_freq);
5221
Imre Deak4e805192014-04-14 20:24:41 +03005222 dev_priv->rps.max_freq = valleyview_rps_max_freq(dev_priv);
5223 dev_priv->rps.rp0_freq = dev_priv->rps.max_freq;
5224 DRM_DEBUG_DRIVER("max GPU freq: %d MHz (%u)\n",
5225 vlv_gpu_freq(dev_priv, dev_priv->rps.max_freq),
5226 dev_priv->rps.max_freq);
5227
5228 dev_priv->rps.efficient_freq = valleyview_rps_rpe_freq(dev_priv);
5229 DRM_DEBUG_DRIVER("RPe GPU freq: %d MHz (%u)\n",
5230 vlv_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
5231 dev_priv->rps.efficient_freq);
5232
Deepak Sf8f2b002014-07-10 13:16:21 +05305233 dev_priv->rps.rp1_freq = valleyview_rps_guar_freq(dev_priv);
5234 DRM_DEBUG_DRIVER("RP1(Guar Freq) GPU freq: %d MHz (%u)\n",
5235 vlv_gpu_freq(dev_priv, dev_priv->rps.rp1_freq),
5236 dev_priv->rps.rp1_freq);
5237
Imre Deak4e805192014-04-14 20:24:41 +03005238 dev_priv->rps.min_freq = valleyview_rps_min_freq(dev_priv);
5239 DRM_DEBUG_DRIVER("min GPU freq: %d MHz (%u)\n",
5240 vlv_gpu_freq(dev_priv, dev_priv->rps.min_freq),
5241 dev_priv->rps.min_freq);
5242
5243 /* Preserve min/max settings in case of re-init */
5244 if (dev_priv->rps.max_freq_softlimit == 0)
5245 dev_priv->rps.max_freq_softlimit = dev_priv->rps.max_freq;
5246
5247 if (dev_priv->rps.min_freq_softlimit == 0)
5248 dev_priv->rps.min_freq_softlimit = dev_priv->rps.min_freq;
5249
5250 mutex_unlock(&dev_priv->rps.hw_lock);
5251}
5252
Deepak S38807742014-05-23 21:00:15 +05305253static void cherryview_init_gt_powersave(struct drm_device *dev)
5254{
Deepak S2b6b3a02014-05-27 15:59:30 +05305255 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03005256 u32 val;
Deepak S2b6b3a02014-05-27 15:59:30 +05305257
Deepak S38807742014-05-23 21:00:15 +05305258 cherryview_setup_pctx(dev);
Deepak S2b6b3a02014-05-27 15:59:30 +05305259
5260 mutex_lock(&dev_priv->rps.hw_lock);
5261
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03005262 val = vlv_punit_read(dev_priv, CCK_FUSE_REG);
5263 switch ((val >> 2) & 0x7) {
5264 case 0:
5265 case 1:
5266 dev_priv->rps.cz_freq = 200;
5267 dev_priv->mem_freq = 1600;
5268 break;
5269 case 2:
5270 dev_priv->rps.cz_freq = 267;
5271 dev_priv->mem_freq = 1600;
5272 break;
5273 case 3:
5274 dev_priv->rps.cz_freq = 333;
5275 dev_priv->mem_freq = 2000;
5276 break;
5277 case 4:
5278 dev_priv->rps.cz_freq = 320;
5279 dev_priv->mem_freq = 1600;
5280 break;
5281 case 5:
5282 dev_priv->rps.cz_freq = 400;
5283 dev_priv->mem_freq = 1600;
5284 break;
5285 }
5286 DRM_DEBUG_DRIVER("DDR speed: %d MHz", dev_priv->mem_freq);
5287
Deepak S2b6b3a02014-05-27 15:59:30 +05305288 dev_priv->rps.max_freq = cherryview_rps_max_freq(dev_priv);
5289 dev_priv->rps.rp0_freq = dev_priv->rps.max_freq;
5290 DRM_DEBUG_DRIVER("max GPU freq: %d MHz (%u)\n",
5291 vlv_gpu_freq(dev_priv, dev_priv->rps.max_freq),
5292 dev_priv->rps.max_freq);
5293
5294 dev_priv->rps.efficient_freq = cherryview_rps_rpe_freq(dev_priv);
5295 DRM_DEBUG_DRIVER("RPe GPU freq: %d MHz (%u)\n",
5296 vlv_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
5297 dev_priv->rps.efficient_freq);
5298
Deepak S7707df42014-07-12 18:46:14 +05305299 dev_priv->rps.rp1_freq = cherryview_rps_guar_freq(dev_priv);
5300 DRM_DEBUG_DRIVER("RP1(Guar) GPU freq: %d MHz (%u)\n",
5301 vlv_gpu_freq(dev_priv, dev_priv->rps.rp1_freq),
5302 dev_priv->rps.rp1_freq);
5303
Deepak S2b6b3a02014-05-27 15:59:30 +05305304 dev_priv->rps.min_freq = cherryview_rps_min_freq(dev_priv);
5305 DRM_DEBUG_DRIVER("min GPU freq: %d MHz (%u)\n",
5306 vlv_gpu_freq(dev_priv, dev_priv->rps.min_freq),
5307 dev_priv->rps.min_freq);
5308
Ville Syrjälä1c147622014-08-18 14:42:43 +03005309 WARN_ONCE((dev_priv->rps.max_freq |
5310 dev_priv->rps.efficient_freq |
5311 dev_priv->rps.rp1_freq |
5312 dev_priv->rps.min_freq) & 1,
5313 "Odd GPU freq values\n");
5314
Deepak S2b6b3a02014-05-27 15:59:30 +05305315 /* Preserve min/max settings in case of re-init */
5316 if (dev_priv->rps.max_freq_softlimit == 0)
5317 dev_priv->rps.max_freq_softlimit = dev_priv->rps.max_freq;
5318
5319 if (dev_priv->rps.min_freq_softlimit == 0)
5320 dev_priv->rps.min_freq_softlimit = dev_priv->rps.min_freq;
5321
5322 mutex_unlock(&dev_priv->rps.hw_lock);
Deepak S38807742014-05-23 21:00:15 +05305323}
5324
Imre Deak4e805192014-04-14 20:24:41 +03005325static void valleyview_cleanup_gt_powersave(struct drm_device *dev)
5326{
5327 valleyview_cleanup_pctx(dev);
5328}
5329
Deepak S38807742014-05-23 21:00:15 +05305330static void cherryview_enable_rps(struct drm_device *dev)
5331{
5332 struct drm_i915_private *dev_priv = dev->dev_private;
5333 struct intel_engine_cs *ring;
Deepak S2b6b3a02014-05-27 15:59:30 +05305334 u32 gtfifodbg, val, rc6_mode = 0, pcbr;
Deepak S38807742014-05-23 21:00:15 +05305335 int i;
5336
5337 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
5338
5339 gtfifodbg = I915_READ(GTFIFODBG);
5340 if (gtfifodbg) {
5341 DRM_DEBUG_DRIVER("GT fifo had a previous error %x\n",
5342 gtfifodbg);
5343 I915_WRITE(GTFIFODBG, gtfifodbg);
5344 }
5345
5346 cherryview_check_pctx(dev_priv);
5347
5348 /* 1a & 1b: Get forcewake during program sequence. Although the driver
5349 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
5350 gen6_gt_force_wake_get(dev_priv, FORCEWAKE_ALL);
5351
5352 /* 2a: Program RC6 thresholds.*/
5353 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16);
5354 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
5355 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
5356
5357 for_each_ring(ring, dev_priv, i)
5358 I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
5359 I915_WRITE(GEN6_RC_SLEEP, 0);
5360
5361 I915_WRITE(GEN6_RC6_THRESHOLD, 50000); /* 50/125ms per EI */
5362
5363 /* allows RC6 residency counter to work */
5364 I915_WRITE(VLV_COUNTER_CONTROL,
5365 _MASKED_BIT_ENABLE(VLV_COUNT_RANGE_HIGH |
5366 VLV_MEDIA_RC6_COUNT_EN |
5367 VLV_RENDER_RC6_COUNT_EN));
5368
5369 /* For now we assume BIOS is allocating and populating the PCBR */
5370 pcbr = I915_READ(VLV_PCBR);
5371
5372 DRM_DEBUG_DRIVER("PCBR offset : 0x%x\n", pcbr);
5373
5374 /* 3: Enable RC6 */
5375 if ((intel_enable_rc6(dev) & INTEL_RC6_ENABLE) &&
5376 (pcbr >> VLV_PCBR_ADDR_SHIFT))
5377 rc6_mode = GEN6_RC_CTL_EI_MODE(1);
5378
5379 I915_WRITE(GEN6_RC_CONTROL, rc6_mode);
5380
Deepak S2b6b3a02014-05-27 15:59:30 +05305381 /* 4 Program defaults and thresholds for RPS*/
5382 I915_WRITE(GEN6_RP_UP_THRESHOLD, 59400);
5383 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 245000);
5384 I915_WRITE(GEN6_RP_UP_EI, 66000);
5385 I915_WRITE(GEN6_RP_DOWN_EI, 350000);
5386
5387 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
5388
Tom O'Rourke7405f422014-06-10 16:26:34 -07005389 /* WaDisablePwrmtrEvent:chv (pre-production hw) */
5390 I915_WRITE(0xA80C, I915_READ(0xA80C) & 0x00ffffff);
5391 I915_WRITE(0xA810, I915_READ(0xA810) & 0xffffff00);
5392
Deepak S2b6b3a02014-05-27 15:59:30 +05305393 /* 5: Enable RPS */
5394 I915_WRITE(GEN6_RP_CONTROL,
5395 GEN6_RP_MEDIA_HW_NORMAL_MODE |
Tom O'Rourke7405f422014-06-10 16:26:34 -07005396 GEN6_RP_MEDIA_IS_GFX | /* WaSetMaskForGfxBusyness:chv (pre-production hw ?) */
Deepak S2b6b3a02014-05-27 15:59:30 +05305397 GEN6_RP_ENABLE |
5398 GEN6_RP_UP_BUSY_AVG |
5399 GEN6_RP_DOWN_IDLE_AVG);
5400
5401 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
5402
5403 DRM_DEBUG_DRIVER("GPLL enabled? %s\n", val & 0x10 ? "yes" : "no");
5404 DRM_DEBUG_DRIVER("GPU status: 0x%08x\n", val);
5405
5406 dev_priv->rps.cur_freq = (val >> 8) & 0xff;
5407 DRM_DEBUG_DRIVER("current GPU freq: %d MHz (%u)\n",
5408 vlv_gpu_freq(dev_priv, dev_priv->rps.cur_freq),
5409 dev_priv->rps.cur_freq);
5410
5411 DRM_DEBUG_DRIVER("setting GPU freq to %d MHz (%u)\n",
5412 vlv_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
5413 dev_priv->rps.efficient_freq);
5414
5415 valleyview_set_rps(dev_priv->dev, dev_priv->rps.efficient_freq);
5416
Deepak S3497a562014-07-10 13:16:26 +05305417 gen8_enable_rps_interrupts(dev);
5418
Deepak S38807742014-05-23 21:00:15 +05305419 gen6_gt_force_wake_put(dev_priv, FORCEWAKE_ALL);
5420}
5421
Jesse Barnes0a073b82013-04-17 15:54:58 -07005422static void valleyview_enable_rps(struct drm_device *dev)
5423{
5424 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01005425 struct intel_engine_cs *ring;
Ben Widawsky2a5913a2014-03-19 18:31:13 -07005426 u32 gtfifodbg, val, rc6_mode = 0;
Jesse Barnes0a073b82013-04-17 15:54:58 -07005427 int i;
5428
5429 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
5430
Imre Deakae484342014-03-31 15:10:44 +03005431 valleyview_check_pctx(dev_priv);
5432
Jesse Barnes0a073b82013-04-17 15:54:58 -07005433 if ((gtfifodbg = I915_READ(GTFIFODBG))) {
Jesse Barnesf7d85c12013-09-27 10:40:54 -07005434 DRM_DEBUG_DRIVER("GT fifo had a previous error %x\n",
5435 gtfifodbg);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005436 I915_WRITE(GTFIFODBG, gtfifodbg);
5437 }
5438
Deepak Sc8d9a592013-11-23 14:55:42 +05305439 /* If VLV, Forcewake all wells, else re-direct to regular path */
5440 gen6_gt_force_wake_get(dev_priv, FORCEWAKE_ALL);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005441
5442 I915_WRITE(GEN6_RP_UP_THRESHOLD, 59400);
5443 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 245000);
5444 I915_WRITE(GEN6_RP_UP_EI, 66000);
5445 I915_WRITE(GEN6_RP_DOWN_EI, 350000);
5446
5447 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
Deepak S31685c22014-07-03 17:33:01 -04005448 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 0xf4240);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005449
5450 I915_WRITE(GEN6_RP_CONTROL,
5451 GEN6_RP_MEDIA_TURBO |
5452 GEN6_RP_MEDIA_HW_NORMAL_MODE |
5453 GEN6_RP_MEDIA_IS_GFX |
5454 GEN6_RP_ENABLE |
5455 GEN6_RP_UP_BUSY_AVG |
5456 GEN6_RP_DOWN_IDLE_CONT);
5457
5458 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 0x00280000);
5459 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
5460 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
5461
5462 for_each_ring(ring, dev_priv, i)
5463 I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
5464
Jesse Barnes2f0aa302013-11-15 09:32:11 -08005465 I915_WRITE(GEN6_RC6_THRESHOLD, 0x557);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005466
5467 /* allows RC6 residency counter to work */
Jesse Barnes49798eb2013-09-26 17:55:57 -07005468 I915_WRITE(VLV_COUNTER_CONTROL,
Deepak S31685c22014-07-03 17:33:01 -04005469 _MASKED_BIT_ENABLE(VLV_MEDIA_RC0_COUNT_EN |
5470 VLV_RENDER_RC0_COUNT_EN |
Jesse Barnes49798eb2013-09-26 17:55:57 -07005471 VLV_MEDIA_RC6_COUNT_EN |
5472 VLV_RENDER_RC6_COUNT_EN));
Deepak S31685c22014-07-03 17:33:01 -04005473
Jesse Barnesa2b23fe2013-09-19 09:33:13 -07005474 if (intel_enable_rc6(dev) & INTEL_RC6_ENABLE)
Jesse Barnes6b88f292013-11-15 09:32:12 -08005475 rc6_mode = GEN7_RC_CTL_TO_MODE | VLV_RC_CTL_CTX_RST_PARALLEL;
Ben Widawskydc39fff2013-10-18 12:32:07 -07005476
5477 intel_print_rc6_info(dev, rc6_mode);
5478
Jesse Barnesa2b23fe2013-09-19 09:33:13 -07005479 I915_WRITE(GEN6_RC_CONTROL, rc6_mode);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005480
Jani Nikula64936252013-05-22 15:36:20 +03005481 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005482
5483 DRM_DEBUG_DRIVER("GPLL enabled? %s\n", val & 0x10 ? "yes" : "no");
5484 DRM_DEBUG_DRIVER("GPU status: 0x%08x\n", val);
5485
Ben Widawskyb39fb292014-03-19 18:31:11 -07005486 dev_priv->rps.cur_freq = (val >> 8) & 0xff;
Ville Syrjälä73008b92013-06-25 19:21:01 +03005487 DRM_DEBUG_DRIVER("current GPU freq: %d MHz (%u)\n",
Ben Widawskyb39fb292014-03-19 18:31:11 -07005488 vlv_gpu_freq(dev_priv, dev_priv->rps.cur_freq),
5489 dev_priv->rps.cur_freq);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005490
Ville Syrjälä73008b92013-06-25 19:21:01 +03005491 DRM_DEBUG_DRIVER("setting GPU freq to %d MHz (%u)\n",
Ben Widawskyb39fb292014-03-19 18:31:11 -07005492 vlv_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
5493 dev_priv->rps.efficient_freq);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005494
Ben Widawskyb39fb292014-03-19 18:31:11 -07005495 valleyview_set_rps(dev_priv->dev, dev_priv->rps.efficient_freq);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005496
Daniel Vetter44fc7d52013-07-12 22:43:27 +02005497 gen6_enable_rps_interrupts(dev);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005498
Deepak Sc8d9a592013-11-23 14:55:42 +05305499 gen6_gt_force_wake_put(dev_priv, FORCEWAKE_ALL);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005500}
5501
Daniel Vetter930ebb42012-06-29 23:32:16 +02005502void ironlake_teardown_rc6(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005503{
5504 struct drm_i915_private *dev_priv = dev->dev_private;
5505
Daniel Vetter3e373942012-11-02 19:55:04 +01005506 if (dev_priv->ips.renderctx) {
Ben Widawskyd7f46fc2013-12-06 14:10:55 -08005507 i915_gem_object_ggtt_unpin(dev_priv->ips.renderctx);
Daniel Vetter3e373942012-11-02 19:55:04 +01005508 drm_gem_object_unreference(&dev_priv->ips.renderctx->base);
5509 dev_priv->ips.renderctx = NULL;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005510 }
5511
Daniel Vetter3e373942012-11-02 19:55:04 +01005512 if (dev_priv->ips.pwrctx) {
Ben Widawskyd7f46fc2013-12-06 14:10:55 -08005513 i915_gem_object_ggtt_unpin(dev_priv->ips.pwrctx);
Daniel Vetter3e373942012-11-02 19:55:04 +01005514 drm_gem_object_unreference(&dev_priv->ips.pwrctx->base);
5515 dev_priv->ips.pwrctx = NULL;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005516 }
5517}
5518
Daniel Vetter930ebb42012-06-29 23:32:16 +02005519static void ironlake_disable_rc6(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005520{
5521 struct drm_i915_private *dev_priv = dev->dev_private;
5522
5523 if (I915_READ(PWRCTXA)) {
5524 /* Wake the GPU, prevent RC6, then restore RSTDBYCTL */
5525 I915_WRITE(RSTDBYCTL, I915_READ(RSTDBYCTL) | RCX_SW_EXIT);
5526 wait_for(((I915_READ(RSTDBYCTL) & RSX_STATUS_MASK) == RSX_STATUS_ON),
5527 50);
5528
5529 I915_WRITE(PWRCTXA, 0);
5530 POSTING_READ(PWRCTXA);
5531
5532 I915_WRITE(RSTDBYCTL, I915_READ(RSTDBYCTL) & ~RCX_SW_EXIT);
5533 POSTING_READ(RSTDBYCTL);
5534 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005535}
5536
5537static int ironlake_setup_rc6(struct drm_device *dev)
5538{
5539 struct drm_i915_private *dev_priv = dev->dev_private;
5540
Daniel Vetter3e373942012-11-02 19:55:04 +01005541 if (dev_priv->ips.renderctx == NULL)
5542 dev_priv->ips.renderctx = intel_alloc_context_page(dev);
5543 if (!dev_priv->ips.renderctx)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005544 return -ENOMEM;
5545
Daniel Vetter3e373942012-11-02 19:55:04 +01005546 if (dev_priv->ips.pwrctx == NULL)
5547 dev_priv->ips.pwrctx = intel_alloc_context_page(dev);
5548 if (!dev_priv->ips.pwrctx) {
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005549 ironlake_teardown_rc6(dev);
5550 return -ENOMEM;
5551 }
5552
5553 return 0;
5554}
5555
Daniel Vetter930ebb42012-06-29 23:32:16 +02005556static void ironlake_enable_rc6(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005557{
5558 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01005559 struct intel_engine_cs *ring = &dev_priv->ring[RCS];
Chris Wilson3e960502012-11-27 16:22:54 +00005560 bool was_interruptible;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005561 int ret;
5562
5563 /* rc6 disabled by default due to repeated reports of hanging during
5564 * boot and resume.
5565 */
5566 if (!intel_enable_rc6(dev))
5567 return;
5568
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02005569 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
5570
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005571 ret = ironlake_setup_rc6(dev);
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02005572 if (ret)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005573 return;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005574
Chris Wilson3e960502012-11-27 16:22:54 +00005575 was_interruptible = dev_priv->mm.interruptible;
5576 dev_priv->mm.interruptible = false;
5577
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005578 /*
5579 * GPU can automatically power down the render unit if given a page
5580 * to save state.
5581 */
Daniel Vetter6d90c952012-04-26 23:28:05 +02005582 ret = intel_ring_begin(ring, 6);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005583 if (ret) {
5584 ironlake_teardown_rc6(dev);
Chris Wilson3e960502012-11-27 16:22:54 +00005585 dev_priv->mm.interruptible = was_interruptible;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005586 return;
5587 }
5588
Daniel Vetter6d90c952012-04-26 23:28:05 +02005589 intel_ring_emit(ring, MI_SUSPEND_FLUSH | MI_SUSPEND_FLUSH_EN);
5590 intel_ring_emit(ring, MI_SET_CONTEXT);
Ben Widawskyf343c5f2013-07-05 14:41:04 -07005591 intel_ring_emit(ring, i915_gem_obj_ggtt_offset(dev_priv->ips.renderctx) |
Daniel Vetter6d90c952012-04-26 23:28:05 +02005592 MI_MM_SPACE_GTT |
5593 MI_SAVE_EXT_STATE_EN |
5594 MI_RESTORE_EXT_STATE_EN |
5595 MI_RESTORE_INHIBIT);
5596 intel_ring_emit(ring, MI_SUSPEND_FLUSH);
5597 intel_ring_emit(ring, MI_NOOP);
5598 intel_ring_emit(ring, MI_FLUSH);
5599 intel_ring_advance(ring);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005600
5601 /*
5602 * Wait for the command parser to advance past MI_SET_CONTEXT. The HW
5603 * does an implicit flush, combined with MI_FLUSH above, it should be
5604 * safe to assume that renderctx is valid
5605 */
Chris Wilson3e960502012-11-27 16:22:54 +00005606 ret = intel_ring_idle(ring);
5607 dev_priv->mm.interruptible = was_interruptible;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005608 if (ret) {
Jani Nikuladef27a52013-03-12 10:49:19 +02005609 DRM_ERROR("failed to enable ironlake power savings\n");
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005610 ironlake_teardown_rc6(dev);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005611 return;
5612 }
5613
Ben Widawskyf343c5f2013-07-05 14:41:04 -07005614 I915_WRITE(PWRCTXA, i915_gem_obj_ggtt_offset(dev_priv->ips.pwrctx) | PWRCTX_EN);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005615 I915_WRITE(RSTDBYCTL, I915_READ(RSTDBYCTL) & ~RCX_SW_EXIT);
Ben Widawskydc39fff2013-10-18 12:32:07 -07005616
Imre Deak91ca6892014-04-14 20:24:25 +03005617 intel_print_rc6_info(dev, GEN6_RC_CTL_RC6_ENABLE);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005618}
5619
Eugeni Dodonovdde18882012-04-18 15:29:24 -03005620static unsigned long intel_pxfreq(u32 vidfreq)
5621{
5622 unsigned long freq;
5623 int div = (vidfreq & 0x3f0000) >> 16;
5624 int post = (vidfreq & 0x3000) >> 12;
5625 int pre = (vidfreq & 0x7);
5626
5627 if (!pre)
5628 return 0;
5629
5630 freq = ((div * 133333) / ((1<<post) * pre));
5631
5632 return freq;
5633}
5634
Daniel Vettereb48eb02012-04-26 23:28:12 +02005635static const struct cparams {
5636 u16 i;
5637 u16 t;
5638 u16 m;
5639 u16 c;
5640} cparams[] = {
5641 { 1, 1333, 301, 28664 },
5642 { 1, 1066, 294, 24460 },
5643 { 1, 800, 294, 25192 },
5644 { 0, 1333, 276, 27605 },
5645 { 0, 1066, 276, 27605 },
5646 { 0, 800, 231, 23784 },
5647};
5648
Chris Wilsonf531dcb2012-09-25 10:16:12 +01005649static unsigned long __i915_chipset_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02005650{
5651 u64 total_count, diff, ret;
5652 u32 count1, count2, count3, m = 0, c = 0;
5653 unsigned long now = jiffies_to_msecs(jiffies), diff1;
5654 int i;
5655
Daniel Vetter02d71952012-08-09 16:44:54 +02005656 assert_spin_locked(&mchdev_lock);
5657
Daniel Vetter20e4d402012-08-08 23:35:39 +02005658 diff1 = now - dev_priv->ips.last_time1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005659
5660 /* Prevent division-by-zero if we are asking too fast.
5661 * Also, we don't get interesting results if we are polling
5662 * faster than once in 10ms, so just return the saved value
5663 * in such cases.
5664 */
5665 if (diff1 <= 10)
Daniel Vetter20e4d402012-08-08 23:35:39 +02005666 return dev_priv->ips.chipset_power;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005667
5668 count1 = I915_READ(DMIEC);
5669 count2 = I915_READ(DDREC);
5670 count3 = I915_READ(CSIEC);
5671
5672 total_count = count1 + count2 + count3;
5673
5674 /* FIXME: handle per-counter overflow */
Daniel Vetter20e4d402012-08-08 23:35:39 +02005675 if (total_count < dev_priv->ips.last_count1) {
5676 diff = ~0UL - dev_priv->ips.last_count1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005677 diff += total_count;
5678 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +02005679 diff = total_count - dev_priv->ips.last_count1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005680 }
5681
5682 for (i = 0; i < ARRAY_SIZE(cparams); i++) {
Daniel Vetter20e4d402012-08-08 23:35:39 +02005683 if (cparams[i].i == dev_priv->ips.c_m &&
5684 cparams[i].t == dev_priv->ips.r_t) {
Daniel Vettereb48eb02012-04-26 23:28:12 +02005685 m = cparams[i].m;
5686 c = cparams[i].c;
5687 break;
5688 }
5689 }
5690
5691 diff = div_u64(diff, diff1);
5692 ret = ((m * diff) + c);
5693 ret = div_u64(ret, 10);
5694
Daniel Vetter20e4d402012-08-08 23:35:39 +02005695 dev_priv->ips.last_count1 = total_count;
5696 dev_priv->ips.last_time1 = now;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005697
Daniel Vetter20e4d402012-08-08 23:35:39 +02005698 dev_priv->ips.chipset_power = ret;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005699
5700 return ret;
5701}
5702
Chris Wilsonf531dcb2012-09-25 10:16:12 +01005703unsigned long i915_chipset_val(struct drm_i915_private *dev_priv)
5704{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00005705 struct drm_device *dev = dev_priv->dev;
Chris Wilsonf531dcb2012-09-25 10:16:12 +01005706 unsigned long val;
5707
Damien Lespiau3d13ef22014-02-07 19:12:47 +00005708 if (INTEL_INFO(dev)->gen != 5)
Chris Wilsonf531dcb2012-09-25 10:16:12 +01005709 return 0;
5710
5711 spin_lock_irq(&mchdev_lock);
5712
5713 val = __i915_chipset_val(dev_priv);
5714
5715 spin_unlock_irq(&mchdev_lock);
5716
5717 return val;
5718}
5719
Daniel Vettereb48eb02012-04-26 23:28:12 +02005720unsigned long i915_mch_val(struct drm_i915_private *dev_priv)
5721{
5722 unsigned long m, x, b;
5723 u32 tsfs;
5724
5725 tsfs = I915_READ(TSFS);
5726
5727 m = ((tsfs & TSFS_SLOPE_MASK) >> TSFS_SLOPE_SHIFT);
5728 x = I915_READ8(TR1);
5729
5730 b = tsfs & TSFS_INTR_MASK;
5731
5732 return ((m * x) / 127) - b;
5733}
5734
5735static u16 pvid_to_extvid(struct drm_i915_private *dev_priv, u8 pxvid)
5736{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00005737 struct drm_device *dev = dev_priv->dev;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005738 static const struct v_table {
5739 u16 vd; /* in .1 mil */
5740 u16 vm; /* in .1 mil */
5741 } v_table[] = {
5742 { 0, 0, },
5743 { 375, 0, },
5744 { 500, 0, },
5745 { 625, 0, },
5746 { 750, 0, },
5747 { 875, 0, },
5748 { 1000, 0, },
5749 { 1125, 0, },
5750 { 4125, 3000, },
5751 { 4125, 3000, },
5752 { 4125, 3000, },
5753 { 4125, 3000, },
5754 { 4125, 3000, },
5755 { 4125, 3000, },
5756 { 4125, 3000, },
5757 { 4125, 3000, },
5758 { 4125, 3000, },
5759 { 4125, 3000, },
5760 { 4125, 3000, },
5761 { 4125, 3000, },
5762 { 4125, 3000, },
5763 { 4125, 3000, },
5764 { 4125, 3000, },
5765 { 4125, 3000, },
5766 { 4125, 3000, },
5767 { 4125, 3000, },
5768 { 4125, 3000, },
5769 { 4125, 3000, },
5770 { 4125, 3000, },
5771 { 4125, 3000, },
5772 { 4125, 3000, },
5773 { 4125, 3000, },
5774 { 4250, 3125, },
5775 { 4375, 3250, },
5776 { 4500, 3375, },
5777 { 4625, 3500, },
5778 { 4750, 3625, },
5779 { 4875, 3750, },
5780 { 5000, 3875, },
5781 { 5125, 4000, },
5782 { 5250, 4125, },
5783 { 5375, 4250, },
5784 { 5500, 4375, },
5785 { 5625, 4500, },
5786 { 5750, 4625, },
5787 { 5875, 4750, },
5788 { 6000, 4875, },
5789 { 6125, 5000, },
5790 { 6250, 5125, },
5791 { 6375, 5250, },
5792 { 6500, 5375, },
5793 { 6625, 5500, },
5794 { 6750, 5625, },
5795 { 6875, 5750, },
5796 { 7000, 5875, },
5797 { 7125, 6000, },
5798 { 7250, 6125, },
5799 { 7375, 6250, },
5800 { 7500, 6375, },
5801 { 7625, 6500, },
5802 { 7750, 6625, },
5803 { 7875, 6750, },
5804 { 8000, 6875, },
5805 { 8125, 7000, },
5806 { 8250, 7125, },
5807 { 8375, 7250, },
5808 { 8500, 7375, },
5809 { 8625, 7500, },
5810 { 8750, 7625, },
5811 { 8875, 7750, },
5812 { 9000, 7875, },
5813 { 9125, 8000, },
5814 { 9250, 8125, },
5815 { 9375, 8250, },
5816 { 9500, 8375, },
5817 { 9625, 8500, },
5818 { 9750, 8625, },
5819 { 9875, 8750, },
5820 { 10000, 8875, },
5821 { 10125, 9000, },
5822 { 10250, 9125, },
5823 { 10375, 9250, },
5824 { 10500, 9375, },
5825 { 10625, 9500, },
5826 { 10750, 9625, },
5827 { 10875, 9750, },
5828 { 11000, 9875, },
5829 { 11125, 10000, },
5830 { 11250, 10125, },
5831 { 11375, 10250, },
5832 { 11500, 10375, },
5833 { 11625, 10500, },
5834 { 11750, 10625, },
5835 { 11875, 10750, },
5836 { 12000, 10875, },
5837 { 12125, 11000, },
5838 { 12250, 11125, },
5839 { 12375, 11250, },
5840 { 12500, 11375, },
5841 { 12625, 11500, },
5842 { 12750, 11625, },
5843 { 12875, 11750, },
5844 { 13000, 11875, },
5845 { 13125, 12000, },
5846 { 13250, 12125, },
5847 { 13375, 12250, },
5848 { 13500, 12375, },
5849 { 13625, 12500, },
5850 { 13750, 12625, },
5851 { 13875, 12750, },
5852 { 14000, 12875, },
5853 { 14125, 13000, },
5854 { 14250, 13125, },
5855 { 14375, 13250, },
5856 { 14500, 13375, },
5857 { 14625, 13500, },
5858 { 14750, 13625, },
5859 { 14875, 13750, },
5860 { 15000, 13875, },
5861 { 15125, 14000, },
5862 { 15250, 14125, },
5863 { 15375, 14250, },
5864 { 15500, 14375, },
5865 { 15625, 14500, },
5866 { 15750, 14625, },
5867 { 15875, 14750, },
5868 { 16000, 14875, },
5869 { 16125, 15000, },
5870 };
Damien Lespiau3d13ef22014-02-07 19:12:47 +00005871 if (INTEL_INFO(dev)->is_mobile)
Daniel Vettereb48eb02012-04-26 23:28:12 +02005872 return v_table[pxvid].vm;
5873 else
5874 return v_table[pxvid].vd;
5875}
5876
Daniel Vetter02d71952012-08-09 16:44:54 +02005877static void __i915_update_gfx_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02005878{
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00005879 u64 now, diff, diffms;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005880 u32 count;
5881
Daniel Vetter02d71952012-08-09 16:44:54 +02005882 assert_spin_locked(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005883
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00005884 now = ktime_get_raw_ns();
5885 diffms = now - dev_priv->ips.last_time2;
5886 do_div(diffms, NSEC_PER_MSEC);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005887
5888 /* Don't divide by 0 */
Daniel Vettereb48eb02012-04-26 23:28:12 +02005889 if (!diffms)
5890 return;
5891
5892 count = I915_READ(GFXEC);
5893
Daniel Vetter20e4d402012-08-08 23:35:39 +02005894 if (count < dev_priv->ips.last_count2) {
5895 diff = ~0UL - dev_priv->ips.last_count2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005896 diff += count;
5897 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +02005898 diff = count - dev_priv->ips.last_count2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005899 }
5900
Daniel Vetter20e4d402012-08-08 23:35:39 +02005901 dev_priv->ips.last_count2 = count;
5902 dev_priv->ips.last_time2 = now;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005903
5904 /* More magic constants... */
5905 diff = diff * 1181;
5906 diff = div_u64(diff, diffms * 10);
Daniel Vetter20e4d402012-08-08 23:35:39 +02005907 dev_priv->ips.gfx_power = diff;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005908}
5909
Daniel Vetter02d71952012-08-09 16:44:54 +02005910void i915_update_gfx_val(struct drm_i915_private *dev_priv)
5911{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00005912 struct drm_device *dev = dev_priv->dev;
5913
5914 if (INTEL_INFO(dev)->gen != 5)
Daniel Vetter02d71952012-08-09 16:44:54 +02005915 return;
5916
Daniel Vetter92703882012-08-09 16:46:01 +02005917 spin_lock_irq(&mchdev_lock);
Daniel Vetter02d71952012-08-09 16:44:54 +02005918
5919 __i915_update_gfx_val(dev_priv);
5920
Daniel Vetter92703882012-08-09 16:46:01 +02005921 spin_unlock_irq(&mchdev_lock);
Daniel Vetter02d71952012-08-09 16:44:54 +02005922}
5923
Chris Wilsonf531dcb2012-09-25 10:16:12 +01005924static unsigned long __i915_gfx_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02005925{
5926 unsigned long t, corr, state1, corr2, state2;
5927 u32 pxvid, ext_v;
5928
Daniel Vetter02d71952012-08-09 16:44:54 +02005929 assert_spin_locked(&mchdev_lock);
5930
Ben Widawskyb39fb292014-03-19 18:31:11 -07005931 pxvid = I915_READ(PXVFREQ_BASE + (dev_priv->rps.cur_freq * 4));
Daniel Vettereb48eb02012-04-26 23:28:12 +02005932 pxvid = (pxvid >> 24) & 0x7f;
5933 ext_v = pvid_to_extvid(dev_priv, pxvid);
5934
5935 state1 = ext_v;
5936
5937 t = i915_mch_val(dev_priv);
5938
5939 /* Revel in the empirically derived constants */
5940
5941 /* Correction factor in 1/100000 units */
5942 if (t > 80)
5943 corr = ((t * 2349) + 135940);
5944 else if (t >= 50)
5945 corr = ((t * 964) + 29317);
5946 else /* < 50 */
5947 corr = ((t * 301) + 1004);
5948
5949 corr = corr * ((150142 * state1) / 10000 - 78642);
5950 corr /= 100000;
Daniel Vetter20e4d402012-08-08 23:35:39 +02005951 corr2 = (corr * dev_priv->ips.corr);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005952
5953 state2 = (corr2 * state1) / 10000;
5954 state2 /= 100; /* convert to mW */
5955
Daniel Vetter02d71952012-08-09 16:44:54 +02005956 __i915_update_gfx_val(dev_priv);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005957
Daniel Vetter20e4d402012-08-08 23:35:39 +02005958 return dev_priv->ips.gfx_power + state2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005959}
5960
Chris Wilsonf531dcb2012-09-25 10:16:12 +01005961unsigned long i915_gfx_val(struct drm_i915_private *dev_priv)
5962{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00005963 struct drm_device *dev = dev_priv->dev;
Chris Wilsonf531dcb2012-09-25 10:16:12 +01005964 unsigned long val;
5965
Damien Lespiau3d13ef22014-02-07 19:12:47 +00005966 if (INTEL_INFO(dev)->gen != 5)
Chris Wilsonf531dcb2012-09-25 10:16:12 +01005967 return 0;
5968
5969 spin_lock_irq(&mchdev_lock);
5970
5971 val = __i915_gfx_val(dev_priv);
5972
5973 spin_unlock_irq(&mchdev_lock);
5974
5975 return val;
5976}
5977
Daniel Vettereb48eb02012-04-26 23:28:12 +02005978/**
5979 * i915_read_mch_val - return value for IPS use
5980 *
5981 * Calculate and return a value for the IPS driver to use when deciding whether
5982 * we have thermal and power headroom to increase CPU or GPU power budget.
5983 */
5984unsigned long i915_read_mch_val(void)
5985{
5986 struct drm_i915_private *dev_priv;
5987 unsigned long chipset_val, graphics_val, ret = 0;
5988
Daniel Vetter92703882012-08-09 16:46:01 +02005989 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005990 if (!i915_mch_dev)
5991 goto out_unlock;
5992 dev_priv = i915_mch_dev;
5993
Chris Wilsonf531dcb2012-09-25 10:16:12 +01005994 chipset_val = __i915_chipset_val(dev_priv);
5995 graphics_val = __i915_gfx_val(dev_priv);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005996
5997 ret = chipset_val + graphics_val;
5998
5999out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02006000 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006001
6002 return ret;
6003}
6004EXPORT_SYMBOL_GPL(i915_read_mch_val);
6005
6006/**
6007 * i915_gpu_raise - raise GPU frequency limit
6008 *
6009 * Raise the limit; IPS indicates we have thermal headroom.
6010 */
6011bool i915_gpu_raise(void)
6012{
6013 struct drm_i915_private *dev_priv;
6014 bool ret = true;
6015
Daniel Vetter92703882012-08-09 16:46:01 +02006016 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006017 if (!i915_mch_dev) {
6018 ret = false;
6019 goto out_unlock;
6020 }
6021 dev_priv = i915_mch_dev;
6022
Daniel Vetter20e4d402012-08-08 23:35:39 +02006023 if (dev_priv->ips.max_delay > dev_priv->ips.fmax)
6024 dev_priv->ips.max_delay--;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006025
6026out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02006027 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006028
6029 return ret;
6030}
6031EXPORT_SYMBOL_GPL(i915_gpu_raise);
6032
6033/**
6034 * i915_gpu_lower - lower GPU frequency limit
6035 *
6036 * IPS indicates we're close to a thermal limit, so throttle back the GPU
6037 * frequency maximum.
6038 */
6039bool i915_gpu_lower(void)
6040{
6041 struct drm_i915_private *dev_priv;
6042 bool ret = true;
6043
Daniel Vetter92703882012-08-09 16:46:01 +02006044 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006045 if (!i915_mch_dev) {
6046 ret = false;
6047 goto out_unlock;
6048 }
6049 dev_priv = i915_mch_dev;
6050
Daniel Vetter20e4d402012-08-08 23:35:39 +02006051 if (dev_priv->ips.max_delay < dev_priv->ips.min_delay)
6052 dev_priv->ips.max_delay++;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006053
6054out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02006055 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006056
6057 return ret;
6058}
6059EXPORT_SYMBOL_GPL(i915_gpu_lower);
6060
6061/**
6062 * i915_gpu_busy - indicate GPU business to IPS
6063 *
6064 * Tell the IPS driver whether or not the GPU is busy.
6065 */
6066bool i915_gpu_busy(void)
6067{
6068 struct drm_i915_private *dev_priv;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01006069 struct intel_engine_cs *ring;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006070 bool ret = false;
Chris Wilsonf047e392012-07-21 12:31:41 +01006071 int i;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006072
Daniel Vetter92703882012-08-09 16:46:01 +02006073 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006074 if (!i915_mch_dev)
6075 goto out_unlock;
6076 dev_priv = i915_mch_dev;
6077
Chris Wilsonf047e392012-07-21 12:31:41 +01006078 for_each_ring(ring, dev_priv, i)
6079 ret |= !list_empty(&ring->request_list);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006080
6081out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02006082 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006083
6084 return ret;
6085}
6086EXPORT_SYMBOL_GPL(i915_gpu_busy);
6087
6088/**
6089 * i915_gpu_turbo_disable - disable graphics turbo
6090 *
6091 * Disable graphics turbo by resetting the max frequency and setting the
6092 * current frequency to the default.
6093 */
6094bool i915_gpu_turbo_disable(void)
6095{
6096 struct drm_i915_private *dev_priv;
6097 bool ret = true;
6098
Daniel Vetter92703882012-08-09 16:46:01 +02006099 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006100 if (!i915_mch_dev) {
6101 ret = false;
6102 goto out_unlock;
6103 }
6104 dev_priv = i915_mch_dev;
6105
Daniel Vetter20e4d402012-08-08 23:35:39 +02006106 dev_priv->ips.max_delay = dev_priv->ips.fstart;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006107
Daniel Vetter20e4d402012-08-08 23:35:39 +02006108 if (!ironlake_set_drps(dev_priv->dev, dev_priv->ips.fstart))
Daniel Vettereb48eb02012-04-26 23:28:12 +02006109 ret = false;
6110
6111out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02006112 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006113
6114 return ret;
6115}
6116EXPORT_SYMBOL_GPL(i915_gpu_turbo_disable);
6117
6118/**
6119 * Tells the intel_ips driver that the i915 driver is now loaded, if
6120 * IPS got loaded first.
6121 *
6122 * This awkward dance is so that neither module has to depend on the
6123 * other in order for IPS to do the appropriate communication of
6124 * GPU turbo limits to i915.
6125 */
6126static void
6127ips_ping_for_i915_load(void)
6128{
6129 void (*link)(void);
6130
6131 link = symbol_get(ips_link_to_i915_driver);
6132 if (link) {
6133 link();
6134 symbol_put(ips_link_to_i915_driver);
6135 }
6136}
6137
6138void intel_gpu_ips_init(struct drm_i915_private *dev_priv)
6139{
Daniel Vetter02d71952012-08-09 16:44:54 +02006140 /* We only register the i915 ips part with intel-ips once everything is
6141 * set up, to avoid intel-ips sneaking in and reading bogus values. */
Daniel Vetter92703882012-08-09 16:46:01 +02006142 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006143 i915_mch_dev = dev_priv;
Daniel Vetter92703882012-08-09 16:46:01 +02006144 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006145
6146 ips_ping_for_i915_load();
6147}
6148
6149void intel_gpu_ips_teardown(void)
6150{
Daniel Vetter92703882012-08-09 16:46:01 +02006151 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006152 i915_mch_dev = NULL;
Daniel Vetter92703882012-08-09 16:46:01 +02006153 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006154}
Deepak S76c3552f2014-01-30 23:08:16 +05306155
Daniel Vetter8090c6b2012-06-24 16:42:32 +02006156static void intel_init_emon(struct drm_device *dev)
Eugeni Dodonovdde18882012-04-18 15:29:24 -03006157{
6158 struct drm_i915_private *dev_priv = dev->dev_private;
6159 u32 lcfuse;
6160 u8 pxw[16];
6161 int i;
6162
6163 /* Disable to program */
6164 I915_WRITE(ECR, 0);
6165 POSTING_READ(ECR);
6166
6167 /* Program energy weights for various events */
6168 I915_WRITE(SDEW, 0x15040d00);
6169 I915_WRITE(CSIEW0, 0x007f0000);
6170 I915_WRITE(CSIEW1, 0x1e220004);
6171 I915_WRITE(CSIEW2, 0x04000004);
6172
6173 for (i = 0; i < 5; i++)
6174 I915_WRITE(PEW + (i * 4), 0);
6175 for (i = 0; i < 3; i++)
6176 I915_WRITE(DEW + (i * 4), 0);
6177
6178 /* Program P-state weights to account for frequency power adjustment */
6179 for (i = 0; i < 16; i++) {
6180 u32 pxvidfreq = I915_READ(PXVFREQ_BASE + (i * 4));
6181 unsigned long freq = intel_pxfreq(pxvidfreq);
6182 unsigned long vid = (pxvidfreq & PXVFREQ_PX_MASK) >>
6183 PXVFREQ_PX_SHIFT;
6184 unsigned long val;
6185
6186 val = vid * vid;
6187 val *= (freq / 1000);
6188 val *= 255;
6189 val /= (127*127*900);
6190 if (val > 0xff)
6191 DRM_ERROR("bad pxval: %ld\n", val);
6192 pxw[i] = val;
6193 }
6194 /* Render standby states get 0 weight */
6195 pxw[14] = 0;
6196 pxw[15] = 0;
6197
6198 for (i = 0; i < 4; i++) {
6199 u32 val = (pxw[i*4] << 24) | (pxw[(i*4)+1] << 16) |
6200 (pxw[(i*4)+2] << 8) | (pxw[(i*4)+3]);
6201 I915_WRITE(PXW + (i * 4), val);
6202 }
6203
6204 /* Adjust magic regs to magic values (more experimental results) */
6205 I915_WRITE(OGW0, 0);
6206 I915_WRITE(OGW1, 0);
6207 I915_WRITE(EG0, 0x00007f00);
6208 I915_WRITE(EG1, 0x0000000e);
6209 I915_WRITE(EG2, 0x000e0000);
6210 I915_WRITE(EG3, 0x68000300);
6211 I915_WRITE(EG4, 0x42000000);
6212 I915_WRITE(EG5, 0x00140031);
6213 I915_WRITE(EG6, 0);
6214 I915_WRITE(EG7, 0);
6215
6216 for (i = 0; i < 8; i++)
6217 I915_WRITE(PXWL + (i * 4), 0);
6218
6219 /* Enable PMON + select events */
6220 I915_WRITE(ECR, 0x80000019);
6221
6222 lcfuse = I915_READ(LCFUSE02);
6223
Daniel Vetter20e4d402012-08-08 23:35:39 +02006224 dev_priv->ips.corr = (lcfuse & LCFUSE_HIV_MASK);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03006225}
6226
Imre Deakae484342014-03-31 15:10:44 +03006227void intel_init_gt_powersave(struct drm_device *dev)
6228{
Imre Deake6069ca2014-04-18 16:01:02 +03006229 i915.enable_rc6 = sanitize_rc6_option(dev, i915.enable_rc6);
6230
Deepak S38807742014-05-23 21:00:15 +05306231 if (IS_CHERRYVIEW(dev))
6232 cherryview_init_gt_powersave(dev);
6233 else if (IS_VALLEYVIEW(dev))
Imre Deak4e805192014-04-14 20:24:41 +03006234 valleyview_init_gt_powersave(dev);
Imre Deakae484342014-03-31 15:10:44 +03006235}
6236
6237void intel_cleanup_gt_powersave(struct drm_device *dev)
6238{
Deepak S38807742014-05-23 21:00:15 +05306239 if (IS_CHERRYVIEW(dev))
6240 return;
6241 else if (IS_VALLEYVIEW(dev))
Imre Deak4e805192014-04-14 20:24:41 +03006242 valleyview_cleanup_gt_powersave(dev);
Imre Deakae484342014-03-31 15:10:44 +03006243}
6244
Jesse Barnes156c7ca2014-06-12 08:35:45 -07006245/**
6246 * intel_suspend_gt_powersave - suspend PM work and helper threads
6247 * @dev: drm device
6248 *
6249 * We don't want to disable RC6 or other features here, we just want
6250 * to make sure any work we've queued has finished and won't bother
6251 * us while we're suspended.
6252 */
6253void intel_suspend_gt_powersave(struct drm_device *dev)
6254{
6255 struct drm_i915_private *dev_priv = dev->dev_private;
6256
6257 /* Interrupts should be disabled already to avoid re-arming. */
Jesse Barnes9df7575f2014-06-20 09:29:20 -07006258 WARN_ON(intel_irqs_enabled(dev_priv));
Jesse Barnes156c7ca2014-06-12 08:35:45 -07006259
6260 flush_delayed_work(&dev_priv->rps.delayed_resume_work);
6261
6262 cancel_work_sync(&dev_priv->rps.work);
Deepak Sb47adc12014-06-20 20:03:02 +05306263
6264 /* Force GPU to min freq during suspend */
6265 gen6_rps_idle(dev_priv);
Jesse Barnes156c7ca2014-06-12 08:35:45 -07006266}
6267
Daniel Vetter8090c6b2012-06-24 16:42:32 +02006268void intel_disable_gt_powersave(struct drm_device *dev)
6269{
Jesse Barnes1a01ab32012-11-02 11:14:00 -07006270 struct drm_i915_private *dev_priv = dev->dev_private;
6271
Daniel Vetterfd0c0642013-04-24 11:13:35 +02006272 /* Interrupts should be disabled already to avoid re-arming. */
Jesse Barnes9df7575f2014-06-20 09:29:20 -07006273 WARN_ON(intel_irqs_enabled(dev_priv));
Daniel Vetterfd0c0642013-04-24 11:13:35 +02006274
Daniel Vetter930ebb42012-06-29 23:32:16 +02006275 if (IS_IRONLAKE_M(dev)) {
Daniel Vetter8090c6b2012-06-24 16:42:32 +02006276 ironlake_disable_drps(dev);
Daniel Vetter930ebb42012-06-29 23:32:16 +02006277 ironlake_disable_rc6(dev);
Deepak S38807742014-05-23 21:00:15 +05306278 } else if (INTEL_INFO(dev)->gen >= 6) {
Daniel Vetter10d8d362014-06-12 17:48:52 +02006279 intel_suspend_gt_powersave(dev);
Imre Deake4948372014-05-12 18:35:04 +03006280
Jesse Barnes4fc688c2012-11-02 11:14:01 -07006281 mutex_lock(&dev_priv->rps.hw_lock);
Zhe Wang20e49362014-11-04 17:07:05 +00006282 if (INTEL_INFO(dev)->gen >= 9)
6283 gen9_disable_rps(dev);
6284 else if (IS_CHERRYVIEW(dev))
Deepak S38807742014-05-23 21:00:15 +05306285 cherryview_disable_rps(dev);
6286 else if (IS_VALLEYVIEW(dev))
Jesse Barnesd20d4f02013-04-23 10:09:28 -07006287 valleyview_disable_rps(dev);
6288 else
6289 gen6_disable_rps(dev);
Chris Wilsonc0951f02013-10-10 21:58:50 +01006290 dev_priv->rps.enabled = false;
Jesse Barnes4fc688c2012-11-02 11:14:01 -07006291 mutex_unlock(&dev_priv->rps.hw_lock);
Daniel Vetter930ebb42012-06-29 23:32:16 +02006292 }
Daniel Vetter8090c6b2012-06-24 16:42:32 +02006293}
6294
Jesse Barnes1a01ab32012-11-02 11:14:00 -07006295static void intel_gen6_powersave_work(struct work_struct *work)
6296{
6297 struct drm_i915_private *dev_priv =
6298 container_of(work, struct drm_i915_private,
6299 rps.delayed_resume_work.work);
6300 struct drm_device *dev = dev_priv->dev;
6301
Jesse Barnes4fc688c2012-11-02 11:14:01 -07006302 mutex_lock(&dev_priv->rps.hw_lock);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006303
Deepak S38807742014-05-23 21:00:15 +05306304 if (IS_CHERRYVIEW(dev)) {
6305 cherryview_enable_rps(dev);
6306 } else if (IS_VALLEYVIEW(dev)) {
Jesse Barnes0a073b82013-04-17 15:54:58 -07006307 valleyview_enable_rps(dev);
Zhe Wang20e49362014-11-04 17:07:05 +00006308 } else if (INTEL_INFO(dev)->gen >= 9) {
6309 gen9_enable_rps(dev);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006310 } else if (IS_BROADWELL(dev)) {
6311 gen8_enable_rps(dev);
Imre Deakc2bc2fc2014-04-18 16:16:23 +03006312 __gen6_update_ring_freq(dev);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006313 } else {
6314 gen6_enable_rps(dev);
Imre Deakc2bc2fc2014-04-18 16:16:23 +03006315 __gen6_update_ring_freq(dev);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006316 }
Chris Wilsonc0951f02013-10-10 21:58:50 +01006317 dev_priv->rps.enabled = true;
Jesse Barnes4fc688c2012-11-02 11:14:01 -07006318 mutex_unlock(&dev_priv->rps.hw_lock);
Imre Deakc6df39b2014-04-14 20:24:29 +03006319
6320 intel_runtime_pm_put(dev_priv);
Jesse Barnes1a01ab32012-11-02 11:14:00 -07006321}
6322
Daniel Vetter8090c6b2012-06-24 16:42:32 +02006323void intel_enable_gt_powersave(struct drm_device *dev)
6324{
Jesse Barnes1a01ab32012-11-02 11:14:00 -07006325 struct drm_i915_private *dev_priv = dev->dev_private;
6326
Daniel Vetter8090c6b2012-06-24 16:42:32 +02006327 if (IS_IRONLAKE_M(dev)) {
Imre Deakdc1d0132014-04-14 20:24:28 +03006328 mutex_lock(&dev->struct_mutex);
Daniel Vetter8090c6b2012-06-24 16:42:32 +02006329 ironlake_enable_drps(dev);
6330 ironlake_enable_rc6(dev);
6331 intel_init_emon(dev);
Imre Deakdc1d0132014-04-14 20:24:28 +03006332 mutex_unlock(&dev->struct_mutex);
Deepak S38807742014-05-23 21:00:15 +05306333 } else if (INTEL_INFO(dev)->gen >= 6) {
Jesse Barnes1a01ab32012-11-02 11:14:00 -07006334 /*
6335 * PCU communication is slow and this doesn't need to be
6336 * done at any specific time, so do this out of our fast path
6337 * to make resume and init faster.
Imre Deakc6df39b2014-04-14 20:24:29 +03006338 *
6339 * We depend on the HW RC6 power context save/restore
6340 * mechanism when entering D3 through runtime PM suspend. So
6341 * disable RPM until RPS/RC6 is properly setup. We can only
6342 * get here via the driver load/system resume/runtime resume
6343 * paths, so the _noresume version is enough (and in case of
6344 * runtime resume it's necessary).
Jesse Barnes1a01ab32012-11-02 11:14:00 -07006345 */
Imre Deakc6df39b2014-04-14 20:24:29 +03006346 if (schedule_delayed_work(&dev_priv->rps.delayed_resume_work,
6347 round_jiffies_up_relative(HZ)))
6348 intel_runtime_pm_get_noresume(dev_priv);
Daniel Vetter8090c6b2012-06-24 16:42:32 +02006349 }
6350}
6351
Imre Deakc6df39b2014-04-14 20:24:29 +03006352void intel_reset_gt_powersave(struct drm_device *dev)
6353{
6354 struct drm_i915_private *dev_priv = dev->dev_private;
6355
6356 dev_priv->rps.enabled = false;
6357 intel_enable_gt_powersave(dev);
6358}
6359
Daniel Vetter3107bd42012-10-31 22:52:31 +01006360static void ibx_init_clock_gating(struct drm_device *dev)
6361{
6362 struct drm_i915_private *dev_priv = dev->dev_private;
6363
6364 /*
6365 * On Ibex Peak and Cougar Point, we need to disable clock
6366 * gating for the panel power sequencer or it will fail to
6367 * start up when no ports are active.
6368 */
6369 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE);
6370}
6371
Ville Syrjälä0e088b82013-06-07 10:47:04 +03006372static void g4x_disable_trickle_feed(struct drm_device *dev)
6373{
6374 struct drm_i915_private *dev_priv = dev->dev_private;
6375 int pipe;
6376
Damien Lespiau055e3932014-08-18 13:49:10 +01006377 for_each_pipe(dev_priv, pipe) {
Ville Syrjälä0e088b82013-06-07 10:47:04 +03006378 I915_WRITE(DSPCNTR(pipe),
6379 I915_READ(DSPCNTR(pipe)) |
6380 DISPPLANE_TRICKLE_FEED_DISABLE);
Ville Syrjälä1dba99f2013-10-01 18:02:18 +03006381 intel_flush_primary_plane(dev_priv, pipe);
Ville Syrjälä0e088b82013-06-07 10:47:04 +03006382 }
6383}
6384
Ville Syrjälä017636c2013-12-05 15:51:37 +02006385static void ilk_init_lp_watermarks(struct drm_device *dev)
6386{
6387 struct drm_i915_private *dev_priv = dev->dev_private;
6388
6389 I915_WRITE(WM3_LP_ILK, I915_READ(WM3_LP_ILK) & ~WM1_LP_SR_EN);
6390 I915_WRITE(WM2_LP_ILK, I915_READ(WM2_LP_ILK) & ~WM1_LP_SR_EN);
6391 I915_WRITE(WM1_LP_ILK, I915_READ(WM1_LP_ILK) & ~WM1_LP_SR_EN);
6392
6393 /*
6394 * Don't touch WM1S_LP_EN here.
6395 * Doing so could cause underruns.
6396 */
6397}
6398
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006399static void ironlake_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006400{
6401 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau231e54f2012-10-19 17:55:41 +01006402 uint32_t dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006403
Damien Lespiauf1e8fa52013-06-07 17:41:09 +01006404 /*
6405 * Required for FBC
6406 * WaFbcDisableDpfcClockGating:ilk
6407 */
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01006408 dspclk_gate |= ILK_DPFCRUNIT_CLOCK_GATE_DISABLE |
6409 ILK_DPFCUNIT_CLOCK_GATE_DISABLE |
6410 ILK_DPFDUNIT_CLOCK_GATE_ENABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006411
6412 I915_WRITE(PCH_3DCGDIS0,
6413 MARIUNIT_CLOCK_GATE_DISABLE |
6414 SVSMUNIT_CLOCK_GATE_DISABLE);
6415 I915_WRITE(PCH_3DCGDIS1,
6416 VFMUNIT_CLOCK_GATE_DISABLE);
6417
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006418 /*
6419 * According to the spec the following bits should be set in
6420 * order to enable memory self-refresh
6421 * The bit 22/21 of 0x42004
6422 * The bit 5 of 0x42020
6423 * The bit 15 of 0x45000
6424 */
6425 I915_WRITE(ILK_DISPLAY_CHICKEN2,
6426 (I915_READ(ILK_DISPLAY_CHICKEN2) |
6427 ILK_DPARB_GATE | ILK_VSDPFD_FULL));
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01006428 dspclk_gate |= ILK_DPARBUNIT_CLOCK_GATE_ENABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006429 I915_WRITE(DISP_ARB_CTL,
6430 (I915_READ(DISP_ARB_CTL) |
6431 DISP_FBC_WM_DIS));
Ville Syrjälä017636c2013-12-05 15:51:37 +02006432
6433 ilk_init_lp_watermarks(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006434
6435 /*
6436 * Based on the document from hardware guys the following bits
6437 * should be set unconditionally in order to enable FBC.
6438 * The bit 22 of 0x42000
6439 * The bit 22 of 0x42004
6440 * The bit 7,8,9 of 0x42020.
6441 */
6442 if (IS_IRONLAKE_M(dev)) {
Damien Lespiau4bb35332013-06-14 15:23:24 +01006443 /* WaFbcAsynchFlipDisableFbcQueue:ilk */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006444 I915_WRITE(ILK_DISPLAY_CHICKEN1,
6445 I915_READ(ILK_DISPLAY_CHICKEN1) |
6446 ILK_FBCQ_DIS);
6447 I915_WRITE(ILK_DISPLAY_CHICKEN2,
6448 I915_READ(ILK_DISPLAY_CHICKEN2) |
6449 ILK_DPARB_GATE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006450 }
6451
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01006452 I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
6453
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006454 I915_WRITE(ILK_DISPLAY_CHICKEN2,
6455 I915_READ(ILK_DISPLAY_CHICKEN2) |
6456 ILK_ELPIN_409_SELECT);
6457 I915_WRITE(_3D_CHICKEN2,
6458 _3D_CHICKEN2_WM_READ_PIPELINED << 16 |
6459 _3D_CHICKEN2_WM_READ_PIPELINED);
Daniel Vetter4358a372012-10-18 11:49:51 +02006460
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006461 /* WaDisableRenderCachePipelinedFlush:ilk */
Daniel Vetter4358a372012-10-18 11:49:51 +02006462 I915_WRITE(CACHE_MODE_0,
6463 _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
Daniel Vetter3107bd42012-10-31 22:52:31 +01006464
Akash Goel4e046322014-04-04 17:14:38 +05306465 /* WaDisable_RenderCache_OperationalFlush:ilk */
6466 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
6467
Ville Syrjälä0e088b82013-06-07 10:47:04 +03006468 g4x_disable_trickle_feed(dev);
Ville Syrjäläbdad2b22013-06-07 10:47:03 +03006469
Daniel Vetter3107bd42012-10-31 22:52:31 +01006470 ibx_init_clock_gating(dev);
6471}
6472
6473static void cpt_init_clock_gating(struct drm_device *dev)
6474{
6475 struct drm_i915_private *dev_priv = dev->dev_private;
6476 int pipe;
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03006477 uint32_t val;
Daniel Vetter3107bd42012-10-31 22:52:31 +01006478
6479 /*
6480 * On Ibex Peak and Cougar Point, we need to disable clock
6481 * gating for the panel power sequencer or it will fail to
6482 * start up when no ports are active.
6483 */
Jesse Barnescd664072013-10-02 10:34:19 -07006484 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE |
6485 PCH_DPLUNIT_CLOCK_GATE_DISABLE |
6486 PCH_CPUNIT_CLOCK_GATE_DISABLE);
Daniel Vetter3107bd42012-10-31 22:52:31 +01006487 I915_WRITE(SOUTH_CHICKEN2, I915_READ(SOUTH_CHICKEN2) |
6488 DPLS_EDP_PPS_FIX_DIS);
Takashi Iwai335c07b2012-12-11 11:46:29 +01006489 /* The below fixes the weird display corruption, a few pixels shifted
6490 * downward, on (only) LVDS of some HP laptops with IVY.
6491 */
Damien Lespiau055e3932014-08-18 13:49:10 +01006492 for_each_pipe(dev_priv, pipe) {
Paulo Zanonidc4bd2d2013-04-08 15:48:08 -03006493 val = I915_READ(TRANS_CHICKEN2(pipe));
6494 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
6495 val &= ~TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03006496 if (dev_priv->vbt.fdi_rx_polarity_inverted)
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03006497 val |= TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
Paulo Zanonidc4bd2d2013-04-08 15:48:08 -03006498 val &= ~TRANS_CHICKEN2_FRAME_START_DELAY_MASK;
6499 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_COUNTER;
6500 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_MODESWITCH;
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03006501 I915_WRITE(TRANS_CHICKEN2(pipe), val);
6502 }
Daniel Vetter3107bd42012-10-31 22:52:31 +01006503 /* WADP0ClockGatingDisable */
Damien Lespiau055e3932014-08-18 13:49:10 +01006504 for_each_pipe(dev_priv, pipe) {
Daniel Vetter3107bd42012-10-31 22:52:31 +01006505 I915_WRITE(TRANS_CHICKEN1(pipe),
6506 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
6507 }
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006508}
6509
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01006510static void gen6_check_mch_setup(struct drm_device *dev)
6511{
6512 struct drm_i915_private *dev_priv = dev->dev_private;
6513 uint32_t tmp;
6514
6515 tmp = I915_READ(MCH_SSKPD);
Daniel Vetterdf662a22014-08-04 11:17:25 +02006516 if ((tmp & MCH_SSKPD_WM0_MASK) != MCH_SSKPD_WM0_VAL)
6517 DRM_DEBUG_KMS("Wrong MCH_SSKPD value: 0x%08x This can cause underruns.\n",
6518 tmp);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01006519}
6520
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006521static void gen6_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006522{
6523 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau231e54f2012-10-19 17:55:41 +01006524 uint32_t dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006525
Damien Lespiau231e54f2012-10-19 17:55:41 +01006526 I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006527
6528 I915_WRITE(ILK_DISPLAY_CHICKEN2,
6529 I915_READ(ILK_DISPLAY_CHICKEN2) |
6530 ILK_ELPIN_409_SELECT);
6531
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006532 /* WaDisableHiZPlanesWhenMSAAEnabled:snb */
Daniel Vetter42839082012-12-14 23:38:28 +01006533 I915_WRITE(_3D_CHICKEN,
6534 _MASKED_BIT_ENABLE(_3D_CHICKEN_HIZ_PLANE_DISABLE_MSAA_4X_SNB));
6535
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006536 /* WaSetupGtModeTdRowDispatch:snb */
Daniel Vetter6547fbd2012-12-14 23:38:29 +01006537 if (IS_SNB_GT1(dev))
6538 I915_WRITE(GEN6_GT_MODE,
6539 _MASKED_BIT_ENABLE(GEN6_TD_FOUR_ROW_DISPATCH_DISABLE));
6540
Akash Goel4e046322014-04-04 17:14:38 +05306541 /* WaDisable_RenderCache_OperationalFlush:snb */
6542 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
6543
Ville Syrjälä8d85d272014-02-04 21:59:15 +02006544 /*
6545 * BSpec recoomends 8x4 when MSAA is used,
6546 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02006547 *
6548 * Note that PS/WM thread counts depend on the WIZ hashing
6549 * disable bit, which we don't touch here, but it's good
6550 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjälä8d85d272014-02-04 21:59:15 +02006551 */
6552 I915_WRITE(GEN6_GT_MODE,
6553 GEN6_WIZ_HASHING_MASK | GEN6_WIZ_HASHING_16x4);
6554
Ville Syrjälä017636c2013-12-05 15:51:37 +02006555 ilk_init_lp_watermarks(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006556
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006557 I915_WRITE(CACHE_MODE_0,
Daniel Vetter50743292012-04-26 22:02:54 +02006558 _MASKED_BIT_DISABLE(CM0_STC_EVICT_DISABLE_LRA_SNB));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006559
6560 I915_WRITE(GEN6_UCGCTL1,
6561 I915_READ(GEN6_UCGCTL1) |
6562 GEN6_BLBUNIT_CLOCK_GATE_DISABLE |
6563 GEN6_CSUNIT_CLOCK_GATE_DISABLE);
6564
6565 /* According to the BSpec vol1g, bit 12 (RCPBUNIT) clock
6566 * gating disable must be set. Failure to set it results in
6567 * flickering pixels due to Z write ordering failures after
6568 * some amount of runtime in the Mesa "fire" demo, and Unigine
6569 * Sanctuary and Tropics, and apparently anything else with
6570 * alpha test or pixel discard.
6571 *
6572 * According to the spec, bit 11 (RCCUNIT) must also be set,
6573 * but we didn't debug actual testcases to find it out.
Jesse Barnes0f846f82012-06-14 11:04:47 -07006574 *
Ville Syrjäläef593182014-01-22 21:32:47 +02006575 * WaDisableRCCUnitClockGating:snb
6576 * WaDisableRCPBUnitClockGating:snb
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006577 */
6578 I915_WRITE(GEN6_UCGCTL2,
6579 GEN6_RCPBUNIT_CLOCK_GATE_DISABLE |
6580 GEN6_RCCUNIT_CLOCK_GATE_DISABLE);
6581
Ville Syrjälä5eb146d2014-02-04 21:59:16 +02006582 /* WaStripsFansDisableFastClipPerformanceFix:snb */
Ville Syrjälä743b57d2014-02-04 21:59:17 +02006583 I915_WRITE(_3D_CHICKEN3,
6584 _MASKED_BIT_ENABLE(_3D_CHICKEN3_SF_DISABLE_FASTCLIP_CULL));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006585
6586 /*
Ville Syrjäläe927ecd2014-02-04 21:59:18 +02006587 * Bspec says:
6588 * "This bit must be set if 3DSTATE_CLIP clip mode is set to normal and
6589 * 3DSTATE_SF number of SF output attributes is more than 16."
6590 */
6591 I915_WRITE(_3D_CHICKEN3,
6592 _MASKED_BIT_ENABLE(_3D_CHICKEN3_SF_DISABLE_PIPELINED_ATTR_FETCH));
6593
6594 /*
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006595 * According to the spec the following bits should be
6596 * set in order to enable memory self-refresh and fbc:
6597 * The bit21 and bit22 of 0x42000
6598 * The bit21 and bit22 of 0x42004
6599 * The bit5 and bit7 of 0x42020
6600 * The bit14 of 0x70180
6601 * The bit14 of 0x71180
Damien Lespiau4bb35332013-06-14 15:23:24 +01006602 *
6603 * WaFbcAsynchFlipDisableFbcQueue:snb
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006604 */
6605 I915_WRITE(ILK_DISPLAY_CHICKEN1,
6606 I915_READ(ILK_DISPLAY_CHICKEN1) |
6607 ILK_FBCQ_DIS | ILK_PABSTRETCH_DIS);
6608 I915_WRITE(ILK_DISPLAY_CHICKEN2,
6609 I915_READ(ILK_DISPLAY_CHICKEN2) |
6610 ILK_DPARB_GATE | ILK_VSDPFD_FULL);
Damien Lespiau231e54f2012-10-19 17:55:41 +01006611 I915_WRITE(ILK_DSPCLK_GATE_D,
6612 I915_READ(ILK_DSPCLK_GATE_D) |
6613 ILK_DPARBUNIT_CLOCK_GATE_ENABLE |
6614 ILK_DPFDUNIT_CLOCK_GATE_ENABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006615
Ville Syrjälä0e088b82013-06-07 10:47:04 +03006616 g4x_disable_trickle_feed(dev);
Ben Widawskyf8f2ac92012-10-03 19:34:24 -07006617
Daniel Vetter3107bd42012-10-31 22:52:31 +01006618 cpt_init_clock_gating(dev);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01006619
6620 gen6_check_mch_setup(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006621}
6622
6623static void gen7_setup_fixed_func_scheduler(struct drm_i915_private *dev_priv)
6624{
6625 uint32_t reg = I915_READ(GEN7_FF_THREAD_MODE);
6626
Ville Syrjälä3aad9052014-01-22 21:32:59 +02006627 /*
Ville Syrjälä46680e02014-01-22 21:33:01 +02006628 * WaVSThreadDispatchOverride:ivb,vlv
Ville Syrjälä3aad9052014-01-22 21:32:59 +02006629 *
6630 * This actually overrides the dispatch
6631 * mode for all thread types.
6632 */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006633 reg &= ~GEN7_FF_SCHED_MASK;
6634 reg |= GEN7_FF_TS_SCHED_HW;
6635 reg |= GEN7_FF_VS_SCHED_HW;
6636 reg |= GEN7_FF_DS_SCHED_HW;
6637
6638 I915_WRITE(GEN7_FF_THREAD_MODE, reg);
6639}
6640
Paulo Zanoni17a303e2012-11-20 15:12:07 -02006641static void lpt_init_clock_gating(struct drm_device *dev)
6642{
6643 struct drm_i915_private *dev_priv = dev->dev_private;
6644
6645 /*
6646 * TODO: this bit should only be enabled when really needed, then
6647 * disabled when not needed anymore in order to save power.
6648 */
6649 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE)
6650 I915_WRITE(SOUTH_DSPCLK_GATE_D,
6651 I915_READ(SOUTH_DSPCLK_GATE_D) |
6652 PCH_LP_PARTITION_LEVEL_DISABLE);
Paulo Zanoni0a790cd2013-04-17 18:15:49 -03006653
6654 /* WADPOClockGatingDisable:hsw */
6655 I915_WRITE(_TRANSA_CHICKEN1,
6656 I915_READ(_TRANSA_CHICKEN1) |
6657 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
Paulo Zanoni17a303e2012-11-20 15:12:07 -02006658}
6659
Imre Deak7d708ee2013-04-17 14:04:50 +03006660static void lpt_suspend_hw(struct drm_device *dev)
6661{
6662 struct drm_i915_private *dev_priv = dev->dev_private;
6663
6664 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
6665 uint32_t val = I915_READ(SOUTH_DSPCLK_GATE_D);
6666
6667 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
6668 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
6669 }
6670}
6671
Paulo Zanoni47c2bd92014-08-21 17:09:37 -03006672static void broadwell_init_clock_gating(struct drm_device *dev)
Ben Widawsky1020a5c2013-11-02 21:07:06 -07006673{
6674 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau07d27e22014-03-03 17:31:46 +00006675 enum pipe pipe;
Ben Widawsky1020a5c2013-11-02 21:07:06 -07006676
6677 I915_WRITE(WM3_LP_ILK, 0);
6678 I915_WRITE(WM2_LP_ILK, 0);
6679 I915_WRITE(WM1_LP_ILK, 0);
Ben Widawsky50ed5fb2013-11-02 21:07:40 -07006680
Ben Widawskyab57fff2013-12-12 15:28:04 -08006681 /* WaSwitchSolVfFArbitrationPriority:bdw */
Ben Widawsky50ed5fb2013-11-02 21:07:40 -07006682 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07006683
Ben Widawskyab57fff2013-12-12 15:28:04 -08006684 /* WaPsrDPAMaskVBlankInSRD:bdw */
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07006685 I915_WRITE(CHICKEN_PAR1_1,
6686 I915_READ(CHICKEN_PAR1_1) | DPA_MASK_VBLANK_SRD);
6687
Ben Widawskyab57fff2013-12-12 15:28:04 -08006688 /* WaPsrDPRSUnmaskVBlankInSRD:bdw */
Damien Lespiau055e3932014-08-18 13:49:10 +01006689 for_each_pipe(dev_priv, pipe) {
Damien Lespiau07d27e22014-03-03 17:31:46 +00006690 I915_WRITE(CHICKEN_PIPESL_1(pipe),
Ville Syrjäläc7c65622014-03-05 13:05:45 +02006691 I915_READ(CHICKEN_PIPESL_1(pipe)) |
Ville Syrjälä8f670bb2014-03-05 13:05:47 +02006692 BDW_DPRS_MASK_VBLANK_SRD);
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07006693 }
Ben Widawsky63801f22013-12-12 17:26:03 -08006694
Ben Widawskyab57fff2013-12-12 15:28:04 -08006695 /* WaVSRefCountFullforceMissDisable:bdw */
6696 /* WaDSRefCountFullforceMissDisable:bdw */
6697 I915_WRITE(GEN7_FF_THREAD_MODE,
6698 I915_READ(GEN7_FF_THREAD_MODE) &
6699 ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
Ville Syrjälä36075a42014-02-04 21:59:21 +02006700
Ville Syrjälä295e8bb2014-02-27 21:59:01 +02006701 I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
6702 _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
Ville Syrjälä4f1ca9e2014-02-27 21:59:02 +02006703
6704 /* WaDisableSDEUnitClockGating:bdw */
6705 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
6706 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Damien Lespiau5d708682014-03-26 18:41:51 +00006707
Paulo Zanoni89d6b2b2014-08-21 17:09:36 -03006708 lpt_init_clock_gating(dev);
Ben Widawsky1020a5c2013-11-02 21:07:06 -07006709}
6710
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03006711static void haswell_init_clock_gating(struct drm_device *dev)
6712{
6713 struct drm_i915_private *dev_priv = dev->dev_private;
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03006714
Ville Syrjälä017636c2013-12-05 15:51:37 +02006715 ilk_init_lp_watermarks(dev);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03006716
Francisco Jerezf3fc4882013-10-02 15:53:16 -07006717 /* L3 caching of data atomics doesn't work -- disable it. */
6718 I915_WRITE(HSW_SCRATCH1, HSW_SCRATCH1_L3_DATA_ATOMICS_DISABLE);
6719 I915_WRITE(HSW_ROW_CHICKEN3,
6720 _MASKED_BIT_ENABLE(HSW_ROW_CHICKEN3_L3_GLOBAL_ATOMICS_DISABLE));
6721
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006722 /* This is required by WaCatErrorRejectionIssue:hsw */
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03006723 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
6724 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
6725 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
6726
Ville Syrjäläe36ea7f2014-01-22 21:33:00 +02006727 /* WaVSRefCountFullforceMissDisable:hsw */
6728 I915_WRITE(GEN7_FF_THREAD_MODE,
6729 I915_READ(GEN7_FF_THREAD_MODE) & ~GEN7_FF_VS_REF_CNT_FFME);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03006730
Akash Goel4e046322014-04-04 17:14:38 +05306731 /* WaDisable_RenderCache_OperationalFlush:hsw */
6732 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
6733
Chia-I Wufe27c602014-01-28 13:29:33 +08006734 /* enable HiZ Raw Stall Optimization */
6735 I915_WRITE(CACHE_MODE_0_GEN7,
6736 _MASKED_BIT_DISABLE(HIZ_RAW_STALL_OPT_DISABLE));
6737
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006738 /* WaDisable4x2SubspanOptimization:hsw */
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03006739 I915_WRITE(CACHE_MODE_1,
6740 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Eugeni Dodonov1544d9d2012-07-02 11:51:10 -03006741
Ville Syrjäläa12c4962014-02-04 21:59:20 +02006742 /*
6743 * BSpec recommends 8x4 when MSAA is used,
6744 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02006745 *
6746 * Note that PS/WM thread counts depend on the WIZ hashing
6747 * disable bit, which we don't touch here, but it's good
6748 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjäläa12c4962014-02-04 21:59:20 +02006749 */
6750 I915_WRITE(GEN7_GT_MODE,
6751 GEN6_WIZ_HASHING_MASK | GEN6_WIZ_HASHING_16x4);
6752
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006753 /* WaSwitchSolVfFArbitrationPriority:hsw */
Ben Widawskye3dff582013-03-20 14:49:14 -07006754 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
6755
Paulo Zanoni90a88642013-05-03 17:23:45 -03006756 /* WaRsPkgCStateDisplayPMReq:hsw */
6757 I915_WRITE(CHICKEN_PAR1_1,
6758 I915_READ(CHICKEN_PAR1_1) | FORCE_ARB_IDLE_PLANES);
Eugeni Dodonov1544d9d2012-07-02 11:51:10 -03006759
Paulo Zanoni17a303e2012-11-20 15:12:07 -02006760 lpt_init_clock_gating(dev);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03006761}
6762
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006763static void ivybridge_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006764{
6765 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawsky20848222012-05-04 18:58:59 -07006766 uint32_t snpcr;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006767
Ville Syrjälä017636c2013-12-05 15:51:37 +02006768 ilk_init_lp_watermarks(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006769
Damien Lespiau231e54f2012-10-19 17:55:41 +01006770 I915_WRITE(ILK_DSPCLK_GATE_D, ILK_VRHUNIT_CLOCK_GATE_DISABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006771
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006772 /* WaDisableEarlyCull:ivb */
Jesse Barnes87f80202012-10-02 17:43:41 -05006773 I915_WRITE(_3D_CHICKEN3,
6774 _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
6775
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006776 /* WaDisableBackToBackFlipFix:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006777 I915_WRITE(IVB_CHICKEN3,
6778 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
6779 CHICKEN3_DGMG_DONE_FIX_DISABLE);
6780
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006781 /* WaDisablePSDDualDispatchEnable:ivb */
Jesse Barnes12f33822012-10-25 12:15:45 -07006782 if (IS_IVB_GT1(dev))
6783 I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
6784 _MASKED_BIT_ENABLE(GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
Jesse Barnes12f33822012-10-25 12:15:45 -07006785
Akash Goel4e046322014-04-04 17:14:38 +05306786 /* WaDisable_RenderCache_OperationalFlush:ivb */
6787 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
6788
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006789 /* Apply the WaDisableRHWOOptimizationForRenderHang:ivb workaround. */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006790 I915_WRITE(GEN7_COMMON_SLICE_CHICKEN1,
6791 GEN7_CSC1_RHWO_OPT_DISABLE_IN_RCC);
6792
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006793 /* WaApplyL3ControlAndL3ChickenMode:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006794 I915_WRITE(GEN7_L3CNTLREG1,
6795 GEN7_WA_FOR_GEN7_L3_CONTROL);
6796 I915_WRITE(GEN7_L3_CHICKEN_MODE_REGISTER,
Jesse Barnes8ab43972012-10-25 12:15:42 -07006797 GEN7_WA_L3_CHICKEN_MODE);
6798 if (IS_IVB_GT1(dev))
6799 I915_WRITE(GEN7_ROW_CHICKEN2,
6800 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Ville Syrjälä412236c2014-01-22 21:32:44 +02006801 else {
6802 /* must write both registers */
6803 I915_WRITE(GEN7_ROW_CHICKEN2,
6804 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Jesse Barnes8ab43972012-10-25 12:15:42 -07006805 I915_WRITE(GEN7_ROW_CHICKEN2_GT2,
6806 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Ville Syrjälä412236c2014-01-22 21:32:44 +02006807 }
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006808
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006809 /* WaForceL3Serialization:ivb */
Jesse Barnes61939d92012-10-02 17:43:38 -05006810 I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
6811 ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
6812
Ville Syrjälä1b80a19a2014-01-22 21:32:53 +02006813 /*
Jesse Barnes0f846f82012-06-14 11:04:47 -07006814 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006815 * This implements the WaDisableRCZUnitClockGating:ivb workaround.
Jesse Barnes0f846f82012-06-14 11:04:47 -07006816 */
6817 I915_WRITE(GEN6_UCGCTL2,
Ville Syrjälä28acf3b2014-01-22 21:32:48 +02006818 GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
Jesse Barnes0f846f82012-06-14 11:04:47 -07006819
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006820 /* This is required by WaCatErrorRejectionIssue:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006821 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
6822 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
6823 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
6824
Ville Syrjälä0e088b82013-06-07 10:47:04 +03006825 g4x_disable_trickle_feed(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006826
6827 gen7_setup_fixed_func_scheduler(dev_priv);
Daniel Vetter97e19302012-04-24 16:00:21 +02006828
Chris Wilson22721342014-03-04 09:41:43 +00006829 if (0) { /* causes HiZ corruption on ivb:gt1 */
6830 /* enable HiZ Raw Stall Optimization */
6831 I915_WRITE(CACHE_MODE_0_GEN7,
6832 _MASKED_BIT_DISABLE(HIZ_RAW_STALL_OPT_DISABLE));
6833 }
Chia-I Wu116f2b62014-01-28 13:29:34 +08006834
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006835 /* WaDisable4x2SubspanOptimization:ivb */
Daniel Vetter97e19302012-04-24 16:00:21 +02006836 I915_WRITE(CACHE_MODE_1,
6837 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Ben Widawsky20848222012-05-04 18:58:59 -07006838
Ville Syrjäläa607c1a2014-02-04 21:59:19 +02006839 /*
6840 * BSpec recommends 8x4 when MSAA is used,
6841 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02006842 *
6843 * Note that PS/WM thread counts depend on the WIZ hashing
6844 * disable bit, which we don't touch here, but it's good
6845 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjäläa607c1a2014-02-04 21:59:19 +02006846 */
6847 I915_WRITE(GEN7_GT_MODE,
6848 GEN6_WIZ_HASHING_MASK | GEN6_WIZ_HASHING_16x4);
6849
Ben Widawsky20848222012-05-04 18:58:59 -07006850 snpcr = I915_READ(GEN6_MBCUNIT_SNPCR);
6851 snpcr &= ~GEN6_MBC_SNPCR_MASK;
6852 snpcr |= GEN6_MBC_SNPCR_MED;
6853 I915_WRITE(GEN6_MBCUNIT_SNPCR, snpcr);
Daniel Vetter3107bd42012-10-31 22:52:31 +01006854
Ben Widawskyab5c6082013-04-05 13:12:41 -07006855 if (!HAS_PCH_NOP(dev))
6856 cpt_init_clock_gating(dev);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01006857
6858 gen6_check_mch_setup(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006859}
6860
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006861static void valleyview_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006862{
6863 struct drm_i915_private *dev_priv = dev->dev_private;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006864
Ville Syrjäläd7fe0cc2013-05-21 18:01:50 +03006865 I915_WRITE(DSPCLK_GATE_D, VRHUNIT_CLOCK_GATE_DISABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006866
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006867 /* WaDisableEarlyCull:vlv */
Jesse Barnes87f80202012-10-02 17:43:41 -05006868 I915_WRITE(_3D_CHICKEN3,
6869 _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
6870
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006871 /* WaDisableBackToBackFlipFix:vlv */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006872 I915_WRITE(IVB_CHICKEN3,
6873 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
6874 CHICKEN3_DGMG_DONE_FIX_DISABLE);
6875
Ville Syrjäläfad7d362014-01-22 21:32:39 +02006876 /* WaPsdDispatchEnable:vlv */
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006877 /* WaDisablePSDDualDispatchEnable:vlv */
Jesse Barnes12f33822012-10-25 12:15:45 -07006878 I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
Jesse Barnesd3bc0302013-03-08 10:45:51 -08006879 _MASKED_BIT_ENABLE(GEN7_MAX_PS_THREAD_DEP |
6880 GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
Jesse Barnes12f33822012-10-25 12:15:45 -07006881
Akash Goel4e046322014-04-04 17:14:38 +05306882 /* WaDisable_RenderCache_OperationalFlush:vlv */
6883 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
6884
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006885 /* WaForceL3Serialization:vlv */
Jesse Barnes61939d92012-10-02 17:43:38 -05006886 I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
6887 ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
6888
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006889 /* WaDisableDopClockGating:vlv */
Jesse Barnes8ab43972012-10-25 12:15:42 -07006890 I915_WRITE(GEN7_ROW_CHICKEN2,
6891 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
6892
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006893 /* This is required by WaCatErrorRejectionIssue:vlv */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006894 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
6895 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
6896 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
6897
Ville Syrjälä46680e02014-01-22 21:33:01 +02006898 gen7_setup_fixed_func_scheduler(dev_priv);
6899
Ville Syrjälä3c0edae2014-01-22 21:32:56 +02006900 /*
Jesse Barnes0f846f82012-06-14 11:04:47 -07006901 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006902 * This implements the WaDisableRCZUnitClockGating:vlv workaround.
Jesse Barnes0f846f82012-06-14 11:04:47 -07006903 */
6904 I915_WRITE(GEN6_UCGCTL2,
Ville Syrjälä3c0edae2014-01-22 21:32:56 +02006905 GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
Jesse Barnes0f846f82012-06-14 11:04:47 -07006906
Akash Goelc98f5062014-03-24 23:00:07 +05306907 /* WaDisableL3Bank2xClockGate:vlv
6908 * Disabling L3 clock gating- MMIO 940c[25] = 1
6909 * Set bit 25, to disable L3_BANK_2x_CLK_GATING */
6910 I915_WRITE(GEN7_UCGCTL4,
6911 I915_READ(GEN7_UCGCTL4) | GEN7_L3BANK2X_CLOCK_GATE_DISABLE);
Jesse Barnese3f33d42012-06-14 11:04:50 -07006912
Ville Syrjäläe0d8d592013-06-12 22:11:18 +03006913 I915_WRITE(MI_ARB_VLV, MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006914
Ville Syrjäläafd58e72014-01-22 21:33:03 +02006915 /*
6916 * BSpec says this must be set, even though
6917 * WaDisable4x2SubspanOptimization isn't listed for VLV.
6918 */
Daniel Vetter6b26c862012-04-24 14:04:12 +02006919 I915_WRITE(CACHE_MODE_1,
6920 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Jesse Barnes79831172012-06-20 10:53:12 -07006921
6922 /*
Ville Syrjälä031994e2014-01-22 21:32:46 +02006923 * WaIncreaseL3CreditsForVLVB0:vlv
6924 * This is the hardware default actually.
6925 */
6926 I915_WRITE(GEN7_L3SQCREG1, VLV_B0_WA_L3SQCREG1_VALUE);
6927
6928 /*
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006929 * WaDisableVLVClockGating_VBIIssue:vlv
Jesse Barnes2d809572012-10-25 12:15:44 -07006930 * Disable clock gating on th GCFG unit to prevent a delay
6931 * in the reporting of vblank events.
6932 */
Ville Syrjälä7a0d1ee2014-01-22 21:33:04 +02006933 I915_WRITE(VLV_GUNIT_CLOCK_GATE, GCFG_DIS);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006934}
6935
Ville Syrjäläa4565da2014-04-09 13:28:10 +03006936static void cherryview_init_clock_gating(struct drm_device *dev)
6937{
6938 struct drm_i915_private *dev_priv = dev->dev_private;
6939
6940 I915_WRITE(DSPCLK_GATE_D, VRHUNIT_CLOCK_GATE_DISABLE);
6941
6942 I915_WRITE(MI_ARB_VLV, MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE);
Ville Syrjälädd811e72014-04-09 13:28:33 +03006943
Ville Syrjälä232ce332014-04-09 13:28:35 +03006944 /* WaVSRefCountFullforceMissDisable:chv */
6945 /* WaDSRefCountFullforceMissDisable:chv */
6946 I915_WRITE(GEN7_FF_THREAD_MODE,
6947 I915_READ(GEN7_FF_THREAD_MODE) &
6948 ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
Ville Syrjäläacea6f92014-04-09 13:28:36 +03006949
6950 /* WaDisableSemaphoreAndSyncFlipWait:chv */
6951 I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
6952 _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
Ville Syrjälä08466972014-04-09 13:28:37 +03006953
6954 /* WaDisableCSUnitClockGating:chv */
6955 I915_WRITE(GEN6_UCGCTL1, I915_READ(GEN6_UCGCTL1) |
6956 GEN6_CSUNIT_CLOCK_GATE_DISABLE);
Ville Syrjäläc6317802014-04-09 13:28:38 +03006957
6958 /* WaDisableSDEUnitClockGating:chv */
6959 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
6960 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Ville Syrjäläa4565da2014-04-09 13:28:10 +03006961}
6962
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006963static void g4x_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006964{
6965 struct drm_i915_private *dev_priv = dev->dev_private;
6966 uint32_t dspclk_gate;
6967
6968 I915_WRITE(RENCLK_GATE_D1, 0);
6969 I915_WRITE(RENCLK_GATE_D2, VF_UNIT_CLOCK_GATE_DISABLE |
6970 GS_UNIT_CLOCK_GATE_DISABLE |
6971 CL_UNIT_CLOCK_GATE_DISABLE);
6972 I915_WRITE(RAMCLK_GATE_D, 0);
6973 dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE |
6974 OVRUNIT_CLOCK_GATE_DISABLE |
6975 OVCUNIT_CLOCK_GATE_DISABLE;
6976 if (IS_GM45(dev))
6977 dspclk_gate |= DSSUNIT_CLOCK_GATE_DISABLE;
6978 I915_WRITE(DSPCLK_GATE_D, dspclk_gate);
Daniel Vetter4358a372012-10-18 11:49:51 +02006979
6980 /* WaDisableRenderCachePipelinedFlush */
6981 I915_WRITE(CACHE_MODE_0,
6982 _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
Ville Syrjäläde1aa622013-06-07 10:47:01 +03006983
Akash Goel4e046322014-04-04 17:14:38 +05306984 /* WaDisable_RenderCache_OperationalFlush:g4x */
6985 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
6986
Ville Syrjälä0e088b82013-06-07 10:47:04 +03006987 g4x_disable_trickle_feed(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006988}
6989
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006990static void crestline_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006991{
6992 struct drm_i915_private *dev_priv = dev->dev_private;
6993
6994 I915_WRITE(RENCLK_GATE_D1, I965_RCC_CLOCK_GATE_DISABLE);
6995 I915_WRITE(RENCLK_GATE_D2, 0);
6996 I915_WRITE(DSPCLK_GATE_D, 0);
6997 I915_WRITE(RAMCLK_GATE_D, 0);
6998 I915_WRITE16(DEUC, 0);
Ville Syrjälä20f94962013-06-07 10:47:02 +03006999 I915_WRITE(MI_ARB_STATE,
7000 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Akash Goel4e046322014-04-04 17:14:38 +05307001
7002 /* WaDisable_RenderCache_OperationalFlush:gen4 */
7003 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007004}
7005
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007006static void broadwater_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007007{
7008 struct drm_i915_private *dev_priv = dev->dev_private;
7009
7010 I915_WRITE(RENCLK_GATE_D1, I965_RCZ_CLOCK_GATE_DISABLE |
7011 I965_RCC_CLOCK_GATE_DISABLE |
7012 I965_RCPB_CLOCK_GATE_DISABLE |
7013 I965_ISC_CLOCK_GATE_DISABLE |
7014 I965_FBC_CLOCK_GATE_DISABLE);
7015 I915_WRITE(RENCLK_GATE_D2, 0);
Ville Syrjälä20f94962013-06-07 10:47:02 +03007016 I915_WRITE(MI_ARB_STATE,
7017 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Akash Goel4e046322014-04-04 17:14:38 +05307018
7019 /* WaDisable_RenderCache_OperationalFlush:gen4 */
7020 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007021}
7022
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007023static void gen3_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007024{
7025 struct drm_i915_private *dev_priv = dev->dev_private;
7026 u32 dstate = I915_READ(D_STATE);
7027
7028 dstate |= DSTATE_PLL_D3_OFF | DSTATE_GFX_CLOCK_GATING |
7029 DSTATE_DOT_CLOCK_GATING;
7030 I915_WRITE(D_STATE, dstate);
Chris Wilson13a86b82012-04-24 14:51:43 +01007031
7032 if (IS_PINEVIEW(dev))
7033 I915_WRITE(ECOSKPD, _MASKED_BIT_ENABLE(ECO_GATING_CX_ONLY));
Daniel Vetter974a3b02012-09-09 11:54:16 +02007034
7035 /* IIR "flip pending" means done if this bit is set */
7036 I915_WRITE(ECOSKPD, _MASKED_BIT_DISABLE(ECO_FLIP_DONE));
Ville Syrjälä12fabbcb92014-02-25 15:13:38 +02007037
7038 /* interrupts should cause a wake up from C3 */
Ville Syrjälä32992542014-02-25 15:13:39 +02007039 I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_AGPBUSY_INT_EN));
Ville Syrjälädbb42742014-02-25 15:13:41 +02007040
7041 /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
7042 I915_WRITE(MI_ARB_STATE, _MASKED_BIT_ENABLE(MI_ARB_C3_LP_WRITE_ENABLE));
Ville Syrjälä10383922014-08-15 01:21:54 +03007043
7044 I915_WRITE(MI_ARB_STATE,
7045 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007046}
7047
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007048static void i85x_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007049{
7050 struct drm_i915_private *dev_priv = dev->dev_private;
7051
7052 I915_WRITE(RENCLK_GATE_D1, SV_CLOCK_GATE_DISABLE);
Ville Syrjälä54e472a2014-02-25 15:13:40 +02007053
7054 /* interrupts should cause a wake up from C3 */
7055 I915_WRITE(MI_STATE, _MASKED_BIT_ENABLE(MI_AGPBUSY_INT_EN) |
7056 _MASKED_BIT_DISABLE(MI_AGPBUSY_830_MODE));
Ville Syrjälä10383922014-08-15 01:21:54 +03007057
7058 I915_WRITE(MEM_MODE,
7059 _MASKED_BIT_ENABLE(MEM_DISPLAY_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007060}
7061
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007062static void i830_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007063{
7064 struct drm_i915_private *dev_priv = dev->dev_private;
7065
7066 I915_WRITE(DSPCLK_GATE_D, OVRUNIT_CLOCK_GATE_DISABLE);
Ville Syrjälä10383922014-08-15 01:21:54 +03007067
7068 I915_WRITE(MEM_MODE,
7069 _MASKED_BIT_ENABLE(MEM_DISPLAY_A_TRICKLE_FEED_DISABLE) |
7070 _MASKED_BIT_ENABLE(MEM_DISPLAY_B_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007071}
7072
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007073void intel_init_clock_gating(struct drm_device *dev)
7074{
7075 struct drm_i915_private *dev_priv = dev->dev_private;
7076
7077 dev_priv->display.init_clock_gating(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007078}
7079
Imre Deak7d708ee2013-04-17 14:04:50 +03007080void intel_suspend_hw(struct drm_device *dev)
7081{
7082 if (HAS_PCH_LPT(dev))
7083 lpt_suspend_hw(dev);
7084}
7085
Paulo Zanonid2dee862014-09-19 16:04:54 -03007086static void intel_init_fbc(struct drm_i915_private *dev_priv)
7087{
Paulo Zanoni9adccc62014-09-19 16:04:55 -03007088 if (!HAS_FBC(dev_priv)) {
7089 dev_priv->fbc.enabled = false;
Paulo Zanonid2dee862014-09-19 16:04:54 -03007090 return;
Paulo Zanoni9adccc62014-09-19 16:04:55 -03007091 }
Paulo Zanonid2dee862014-09-19 16:04:54 -03007092
7093 if (INTEL_INFO(dev_priv)->gen >= 7) {
7094 dev_priv->display.fbc_enabled = ironlake_fbc_enabled;
7095 dev_priv->display.enable_fbc = gen7_enable_fbc;
7096 dev_priv->display.disable_fbc = ironlake_disable_fbc;
7097 } else if (INTEL_INFO(dev_priv)->gen >= 5) {
7098 dev_priv->display.fbc_enabled = ironlake_fbc_enabled;
7099 dev_priv->display.enable_fbc = ironlake_enable_fbc;
7100 dev_priv->display.disable_fbc = ironlake_disable_fbc;
7101 } else if (IS_GM45(dev_priv)) {
7102 dev_priv->display.fbc_enabled = g4x_fbc_enabled;
7103 dev_priv->display.enable_fbc = g4x_enable_fbc;
7104 dev_priv->display.disable_fbc = g4x_disable_fbc;
7105 } else {
7106 dev_priv->display.fbc_enabled = i8xx_fbc_enabled;
7107 dev_priv->display.enable_fbc = i8xx_enable_fbc;
7108 dev_priv->display.disable_fbc = i8xx_disable_fbc;
7109
7110 /* This value was pulled out of someone's hat */
7111 I915_WRITE(FBC_CONTROL, 500 << FBC_CTL_INTERVAL_SHIFT);
7112 }
Paulo Zanoni9adccc62014-09-19 16:04:55 -03007113
7114 dev_priv->fbc.enabled = dev_priv->display.fbc_enabled(dev_priv->dev);
Paulo Zanonid2dee862014-09-19 16:04:54 -03007115}
7116
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007117/* Set up chip specific power management-related functions */
7118void intel_init_pm(struct drm_device *dev)
7119{
7120 struct drm_i915_private *dev_priv = dev->dev_private;
7121
Paulo Zanonid2dee862014-09-19 16:04:54 -03007122 intel_init_fbc(dev_priv);
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007123
Daniel Vetterc921aba2012-04-26 23:28:17 +02007124 /* For cxsr */
7125 if (IS_PINEVIEW(dev))
7126 i915_pineview_get_mem_freq(dev);
7127 else if (IS_GEN5(dev))
7128 i915_ironlake_get_mem_freq(dev);
7129
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007130 /* For FIFO watermark updates */
Damien Lespiauc83155a2014-03-28 00:18:35 +05307131 if (IS_GEN9(dev)) {
Pradeep Bhat2af30a52014-11-04 17:06:38 +00007132 skl_setup_wm_latency(dev);
7133
Damien Lespiauc83155a2014-03-28 00:18:35 +05307134 dev_priv->display.init_clock_gating = gen9_init_clock_gating;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00007135 dev_priv->display.update_wm = skl_update_wm;
7136 dev_priv->display.update_sprite_wm = skl_update_sprite_wm;
Damien Lespiauc83155a2014-03-28 00:18:35 +05307137 } else if (HAS_PCH_SPLIT(dev)) {
Damien Lespiaufa50ad62014-03-17 18:01:16 +00007138 ilk_setup_wm_latency(dev);
Ville Syrjälä53615a52013-08-01 16:18:50 +03007139
Ville Syrjäläbd602542014-01-07 16:14:10 +02007140 if ((IS_GEN5(dev) && dev_priv->wm.pri_latency[1] &&
7141 dev_priv->wm.spr_latency[1] && dev_priv->wm.cur_latency[1]) ||
7142 (!IS_GEN5(dev) && dev_priv->wm.pri_latency[0] &&
7143 dev_priv->wm.spr_latency[0] && dev_priv->wm.cur_latency[0])) {
7144 dev_priv->display.update_wm = ilk_update_wm;
7145 dev_priv->display.update_sprite_wm = ilk_update_sprite_wm;
7146 } else {
7147 DRM_DEBUG_KMS("Failed to read display plane latency. "
7148 "Disable CxSR\n");
7149 }
7150
7151 if (IS_GEN5(dev))
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007152 dev_priv->display.init_clock_gating = ironlake_init_clock_gating;
Ville Syrjäläbd602542014-01-07 16:14:10 +02007153 else if (IS_GEN6(dev))
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007154 dev_priv->display.init_clock_gating = gen6_init_clock_gating;
Ville Syrjäläbd602542014-01-07 16:14:10 +02007155 else if (IS_IVYBRIDGE(dev))
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007156 dev_priv->display.init_clock_gating = ivybridge_init_clock_gating;
Ville Syrjäläbd602542014-01-07 16:14:10 +02007157 else if (IS_HASWELL(dev))
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03007158 dev_priv->display.init_clock_gating = haswell_init_clock_gating;
Ville Syrjäläbd602542014-01-07 16:14:10 +02007159 else if (INTEL_INFO(dev)->gen == 8)
Paulo Zanoni47c2bd92014-08-21 17:09:37 -03007160 dev_priv->display.init_clock_gating = broadwell_init_clock_gating;
Ville Syrjäläa4565da2014-04-09 13:28:10 +03007161 } else if (IS_CHERRYVIEW(dev)) {
Ville Syrjälä3c2777f2014-06-26 17:03:06 +03007162 dev_priv->display.update_wm = cherryview_update_wm;
Gajanan Bhat01e184c2014-08-07 17:03:30 +05307163 dev_priv->display.update_sprite_wm = valleyview_update_sprite_wm;
Ville Syrjäläa4565da2014-04-09 13:28:10 +03007164 dev_priv->display.init_clock_gating =
7165 cherryview_init_clock_gating;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007166 } else if (IS_VALLEYVIEW(dev)) {
7167 dev_priv->display.update_wm = valleyview_update_wm;
Gajanan Bhat01e184c2014-08-07 17:03:30 +05307168 dev_priv->display.update_sprite_wm = valleyview_update_sprite_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007169 dev_priv->display.init_clock_gating =
7170 valleyview_init_clock_gating;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007171 } else if (IS_PINEVIEW(dev)) {
7172 if (!intel_get_cxsr_latency(IS_PINEVIEW_G(dev),
7173 dev_priv->is_ddr3,
7174 dev_priv->fsb_freq,
7175 dev_priv->mem_freq)) {
7176 DRM_INFO("failed to find known CxSR latency "
7177 "(found ddr%s fsb freq %d, mem freq %d), "
7178 "disabling CxSR\n",
7179 (dev_priv->is_ddr3 == 1) ? "3" : "2",
7180 dev_priv->fsb_freq, dev_priv->mem_freq);
7181 /* Disable CxSR and never update its watermark again */
Imre Deak5209b1f2014-07-01 12:36:17 +03007182 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007183 dev_priv->display.update_wm = NULL;
7184 } else
7185 dev_priv->display.update_wm = pineview_update_wm;
7186 dev_priv->display.init_clock_gating = gen3_init_clock_gating;
7187 } else if (IS_G4X(dev)) {
7188 dev_priv->display.update_wm = g4x_update_wm;
7189 dev_priv->display.init_clock_gating = g4x_init_clock_gating;
7190 } else if (IS_GEN4(dev)) {
7191 dev_priv->display.update_wm = i965_update_wm;
7192 if (IS_CRESTLINE(dev))
7193 dev_priv->display.init_clock_gating = crestline_init_clock_gating;
7194 else if (IS_BROADWATER(dev))
7195 dev_priv->display.init_clock_gating = broadwater_init_clock_gating;
7196 } else if (IS_GEN3(dev)) {
7197 dev_priv->display.update_wm = i9xx_update_wm;
7198 dev_priv->display.get_fifo_size = i9xx_get_fifo_size;
7199 dev_priv->display.init_clock_gating = gen3_init_clock_gating;
Daniel Vetterfeb56b92013-12-14 20:38:30 -02007200 } else if (IS_GEN2(dev)) {
7201 if (INTEL_INFO(dev)->num_pipes == 1) {
7202 dev_priv->display.update_wm = i845_update_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007203 dev_priv->display.get_fifo_size = i845_get_fifo_size;
Daniel Vetterfeb56b92013-12-14 20:38:30 -02007204 } else {
7205 dev_priv->display.update_wm = i9xx_update_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007206 dev_priv->display.get_fifo_size = i830_get_fifo_size;
Daniel Vetterfeb56b92013-12-14 20:38:30 -02007207 }
7208
7209 if (IS_I85X(dev) || IS_I865G(dev))
7210 dev_priv->display.init_clock_gating = i85x_init_clock_gating;
7211 else
7212 dev_priv->display.init_clock_gating = i830_init_clock_gating;
7213 } else {
7214 DRM_ERROR("unexpected fall-through in intel_init_pm\n");
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007215 }
7216}
7217
Ben Widawsky42c05262012-09-26 10:34:00 -07007218int sandybridge_pcode_read(struct drm_i915_private *dev_priv, u8 mbox, u32 *val)
7219{
Jesse Barnes4fc688c2012-11-02 11:14:01 -07007220 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Ben Widawsky42c05262012-09-26 10:34:00 -07007221
7222 if (I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) {
7223 DRM_DEBUG_DRIVER("warning: pcode (read) mailbox access failed\n");
7224 return -EAGAIN;
7225 }
7226
7227 I915_WRITE(GEN6_PCODE_DATA, *val);
Pradeep Bhat2af30a52014-11-04 17:06:38 +00007228 if (INTEL_INFO(dev_priv)->gen >= 9)
7229 I915_WRITE(GEN9_PCODE_DATA1, 0);
Ben Widawsky42c05262012-09-26 10:34:00 -07007230 I915_WRITE(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY | mbox);
7231
7232 if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
7233 500)) {
7234 DRM_ERROR("timeout waiting for pcode read (%d) to finish\n", mbox);
7235 return -ETIMEDOUT;
7236 }
7237
7238 *val = I915_READ(GEN6_PCODE_DATA);
7239 I915_WRITE(GEN6_PCODE_DATA, 0);
7240
7241 return 0;
7242}
7243
7244int sandybridge_pcode_write(struct drm_i915_private *dev_priv, u8 mbox, u32 val)
7245{
Jesse Barnes4fc688c2012-11-02 11:14:01 -07007246 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Ben Widawsky42c05262012-09-26 10:34:00 -07007247
7248 if (I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) {
7249 DRM_DEBUG_DRIVER("warning: pcode (write) mailbox access failed\n");
7250 return -EAGAIN;
7251 }
7252
7253 I915_WRITE(GEN6_PCODE_DATA, val);
7254 I915_WRITE(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY | mbox);
7255
7256 if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
7257 500)) {
7258 DRM_ERROR("timeout waiting for pcode write (%d) to finish\n", mbox);
7259 return -ETIMEDOUT;
7260 }
7261
7262 I915_WRITE(GEN6_PCODE_DATA, 0);
7263
7264 return 0;
7265}
Jesse Barnesa0e4e192013-04-02 11:23:05 -07007266
Fengguang Wub55dd642014-07-12 11:21:39 +02007267static int byt_gpu_freq(struct drm_i915_private *dev_priv, int val)
Jesse Barnes855ba3b2013-04-17 15:54:57 -07007268{
Ville Syrjälä07ab1182013-11-05 22:42:28 +02007269 int div;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07007270
Ville Syrjälä07ab1182013-11-05 22:42:28 +02007271 /* 4 x czclk */
Ville Syrjälä2ec38152013-11-05 22:42:29 +02007272 switch (dev_priv->mem_freq) {
Jesse Barnes855ba3b2013-04-17 15:54:57 -07007273 case 800:
Ville Syrjälä07ab1182013-11-05 22:42:28 +02007274 div = 10;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07007275 break;
7276 case 1066:
Ville Syrjälä07ab1182013-11-05 22:42:28 +02007277 div = 12;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07007278 break;
7279 case 1333:
Ville Syrjälä07ab1182013-11-05 22:42:28 +02007280 div = 16;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07007281 break;
7282 default:
7283 return -1;
7284 }
7285
Ville Syrjälä2ec38152013-11-05 22:42:29 +02007286 return DIV_ROUND_CLOSEST(dev_priv->mem_freq * (val + 6 - 0xbd), 4 * div);
Jesse Barnes855ba3b2013-04-17 15:54:57 -07007287}
7288
Fengguang Wub55dd642014-07-12 11:21:39 +02007289static int byt_freq_opcode(struct drm_i915_private *dev_priv, int val)
Jesse Barnes855ba3b2013-04-17 15:54:57 -07007290{
Ville Syrjälä07ab1182013-11-05 22:42:28 +02007291 int mul;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07007292
Ville Syrjälä07ab1182013-11-05 22:42:28 +02007293 /* 4 x czclk */
Ville Syrjälä2ec38152013-11-05 22:42:29 +02007294 switch (dev_priv->mem_freq) {
Jesse Barnes855ba3b2013-04-17 15:54:57 -07007295 case 800:
Ville Syrjälä07ab1182013-11-05 22:42:28 +02007296 mul = 10;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07007297 break;
7298 case 1066:
Ville Syrjälä07ab1182013-11-05 22:42:28 +02007299 mul = 12;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07007300 break;
7301 case 1333:
Ville Syrjälä07ab1182013-11-05 22:42:28 +02007302 mul = 16;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07007303 break;
7304 default:
7305 return -1;
7306 }
7307
Ville Syrjälä2ec38152013-11-05 22:42:29 +02007308 return DIV_ROUND_CLOSEST(4 * mul * val, dev_priv->mem_freq) + 0xbd - 6;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07007309}
7310
Fengguang Wub55dd642014-07-12 11:21:39 +02007311static int chv_gpu_freq(struct drm_i915_private *dev_priv, int val)
Deepak S22b1b2f2014-07-12 14:54:33 +05307312{
7313 int div, freq;
7314
7315 switch (dev_priv->rps.cz_freq) {
7316 case 200:
7317 div = 5;
7318 break;
7319 case 267:
7320 div = 6;
7321 break;
7322 case 320:
7323 case 333:
7324 case 400:
7325 div = 8;
7326 break;
7327 default:
7328 return -1;
7329 }
7330
7331 freq = (DIV_ROUND_CLOSEST((dev_priv->rps.cz_freq * val), 2 * div) / 2);
7332
7333 return freq;
7334}
7335
Fengguang Wub55dd642014-07-12 11:21:39 +02007336static int chv_freq_opcode(struct drm_i915_private *dev_priv, int val)
Deepak S22b1b2f2014-07-12 14:54:33 +05307337{
7338 int mul, opcode;
7339
7340 switch (dev_priv->rps.cz_freq) {
7341 case 200:
7342 mul = 5;
7343 break;
7344 case 267:
7345 mul = 6;
7346 break;
7347 case 320:
7348 case 333:
7349 case 400:
7350 mul = 8;
7351 break;
7352 default:
7353 return -1;
7354 }
7355
Ville Syrjälä1c147622014-08-18 14:42:43 +03007356 /* CHV needs even values */
Deepak S22b1b2f2014-07-12 14:54:33 +05307357 opcode = (DIV_ROUND_CLOSEST((val * 2 * mul), dev_priv->rps.cz_freq) * 2);
7358
7359 return opcode;
7360}
7361
7362int vlv_gpu_freq(struct drm_i915_private *dev_priv, int val)
7363{
7364 int ret = -1;
7365
7366 if (IS_CHERRYVIEW(dev_priv->dev))
7367 ret = chv_gpu_freq(dev_priv, val);
7368 else if (IS_VALLEYVIEW(dev_priv->dev))
7369 ret = byt_gpu_freq(dev_priv, val);
7370
7371 return ret;
7372}
7373
7374int vlv_freq_opcode(struct drm_i915_private *dev_priv, int val)
7375{
7376 int ret = -1;
7377
7378 if (IS_CHERRYVIEW(dev_priv->dev))
7379 ret = chv_freq_opcode(dev_priv, val);
7380 else if (IS_VALLEYVIEW(dev_priv->dev))
7381 ret = byt_freq_opcode(dev_priv, val);
7382
7383 return ret;
7384}
7385
Daniel Vetterf742a552013-12-06 10:17:53 +01007386void intel_pm_setup(struct drm_device *dev)
Chris Wilson907b28c2013-07-19 20:36:52 +01007387{
7388 struct drm_i915_private *dev_priv = dev->dev_private;
7389
Daniel Vetterf742a552013-12-06 10:17:53 +01007390 mutex_init(&dev_priv->rps.hw_lock);
7391
Chris Wilson907b28c2013-07-19 20:36:52 +01007392 INIT_DELAYED_WORK(&dev_priv->rps.delayed_resume_work,
7393 intel_gen6_powersave_work);
Paulo Zanoni5d584b22014-03-07 20:08:15 -03007394
Paulo Zanoni33688d92014-03-07 20:08:19 -03007395 dev_priv->pm.suspended = false;
Chris Wilson907b28c2013-07-19 20:36:52 +01007396}