blob: 81465e65e87656b0192c5d44b1539460a40da8b5 [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>
Paulo Zanonif9dcb0d2013-12-11 18:50:10 -020033#include <linux/vgaarb.h>
Damien Lespiauf4db9322013-06-24 22:59:50 +010034#include <drm/i915_powerwell.h>
Paulo Zanoni8a187452013-12-06 20:32:13 -020035#include <linux/pm_runtime.h>
Eugeni Dodonov85208be2012-04-16 22:20:34 -030036
Ben Widawskydc39fff2013-10-18 12:32:07 -070037/**
38 * RC6 is a special power stage which allows the GPU to enter an very
39 * low-voltage mode when idle, using down to 0V while at this stage. This
40 * stage is entered automatically when the GPU is idle when RC6 support is
41 * enabled, and as soon as new workload arises GPU wakes up automatically as well.
42 *
43 * There are different RC6 modes available in Intel GPU, which differentiate
44 * among each other with the latency required to enter and leave RC6 and
45 * voltage consumed by the GPU in different states.
46 *
47 * The combination of the following flags define which states GPU is allowed
48 * to enter, while RC6 is the normal RC6 state, RC6p is the deep RC6, and
49 * RC6pp is deepest RC6. Their support by hardware varies according to the
50 * GPU, BIOS, chipset and platform. RC6 is usually the safest one and the one
51 * which brings the most power savings; deeper states save more power, but
52 * require higher latency to switch to and wake up.
53 */
54#define INTEL_RC6_ENABLE (1<<0)
55#define INTEL_RC6p_ENABLE (1<<1)
56#define INTEL_RC6pp_ENABLE (1<<2)
57
Eugeni Dodonovf6750b32012-04-18 11:51:14 -030058/* FBC, or Frame Buffer Compression, is a technique employed to compress the
59 * framebuffer contents in-memory, aiming at reducing the required bandwidth
60 * during in-memory transfers and, therefore, reduce the power packet.
Eugeni Dodonov85208be2012-04-16 22:20:34 -030061 *
Eugeni Dodonovf6750b32012-04-18 11:51:14 -030062 * The benefits of FBC are mostly visible with solid backgrounds and
63 * variation-less patterns.
Eugeni Dodonov85208be2012-04-16 22:20:34 -030064 *
Eugeni Dodonovf6750b32012-04-18 11:51:14 -030065 * FBC-related functionality can be enabled by the means of the
66 * i915.i915_enable_fbc parameter
Eugeni Dodonov85208be2012-04-16 22:20:34 -030067 */
68
Damien Lespiauda2078c2013-02-13 15:27:27 +000069static void gen9_init_clock_gating(struct drm_device *dev)
70{
Damien Lespiauacd5c342014-03-26 16:55:46 +000071 struct drm_i915_private *dev_priv = dev->dev_private;
72
73 /*
74 * WaDisableSDEUnitClockGating:skl
75 * This seems to be a pre-production w/a.
76 */
77 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
78 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Damien Lespiauda2078c2013-02-13 15:27:27 +000079}
80
Eugeni Dodonov1fa61102012-04-18 15:29:26 -030081static void i8xx_disable_fbc(struct drm_device *dev)
Eugeni Dodonov85208be2012-04-16 22:20:34 -030082{
83 struct drm_i915_private *dev_priv = dev->dev_private;
84 u32 fbc_ctl;
85
86 /* Disable compression */
87 fbc_ctl = I915_READ(FBC_CONTROL);
88 if ((fbc_ctl & FBC_CTL_EN) == 0)
89 return;
90
91 fbc_ctl &= ~FBC_CTL_EN;
92 I915_WRITE(FBC_CONTROL, fbc_ctl);
93
94 /* Wait for compressing bit to clear */
95 if (wait_for((I915_READ(FBC_STATUS) & FBC_STAT_COMPRESSING) == 0, 10)) {
96 DRM_DEBUG_KMS("FBC idle timed out\n");
97 return;
98 }
99
100 DRM_DEBUG_KMS("disabled FBC\n");
101}
102
Ville Syrjälä993495a2013-12-12 17:27:40 +0200103static void i8xx_enable_fbc(struct drm_crtc *crtc)
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300104{
105 struct drm_device *dev = crtc->dev;
106 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roperf4510a22014-04-01 15:22:40 -0700107 struct drm_framebuffer *fb = crtc->primary->fb;
Matt Roper2ff8fde2014-07-08 07:50:07 -0700108 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300109 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
110 int cfb_pitch;
Ville Syrjälä7f2cf222014-01-23 16:49:11 +0200111 int i;
Ville Syrjälä159f9872013-11-28 17:29:57 +0200112 u32 fbc_ctl;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300113
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -0700114 cfb_pitch = dev_priv->fbc.size / FBC_LL_SIZE;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300115 if (fb->pitches[0] < cfb_pitch)
116 cfb_pitch = fb->pitches[0];
117
Ville Syrjälä42a430f2013-11-28 17:29:56 +0200118 /* FBC_CTL wants 32B or 64B units */
119 if (IS_GEN2(dev))
120 cfb_pitch = (cfb_pitch / 32) - 1;
121 else
122 cfb_pitch = (cfb_pitch / 64) - 1;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300123
124 /* Clear old tags */
125 for (i = 0; i < (FBC_LL_SIZE / 32) + 1; i++)
126 I915_WRITE(FBC_TAG + (i * 4), 0);
127
Ville Syrjälä159f9872013-11-28 17:29:57 +0200128 if (IS_GEN4(dev)) {
129 u32 fbc_ctl2;
130
131 /* Set it up... */
132 fbc_ctl2 = FBC_CTL_FENCE_DBL | FBC_CTL_IDLE_IMM | FBC_CTL_CPU_FENCE;
Ville Syrjälä7f2cf222014-01-23 16:49:11 +0200133 fbc_ctl2 |= FBC_CTL_PLANE(intel_crtc->plane);
Ville Syrjälä159f9872013-11-28 17:29:57 +0200134 I915_WRITE(FBC_CONTROL2, fbc_ctl2);
135 I915_WRITE(FBC_FENCE_OFF, crtc->y);
136 }
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300137
138 /* enable it... */
Ville Syrjälä993495a2013-12-12 17:27:40 +0200139 fbc_ctl = I915_READ(FBC_CONTROL);
140 fbc_ctl &= 0x3fff << FBC_CTL_INTERVAL_SHIFT;
141 fbc_ctl |= FBC_CTL_EN | FBC_CTL_PERIODIC;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300142 if (IS_I945GM(dev))
143 fbc_ctl |= FBC_CTL_C3_IDLE; /* 945 needs special SR handling */
144 fbc_ctl |= (cfb_pitch & 0xff) << FBC_CTL_STRIDE_SHIFT;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300145 fbc_ctl |= obj->fence_reg;
146 I915_WRITE(FBC_CONTROL, fbc_ctl);
147
Ville Syrjälä5cd54102014-01-23 16:49:16 +0200148 DRM_DEBUG_KMS("enabled FBC, pitch %d, yoff %d, plane %c\n",
Ville Syrjälä84f44ce2013-04-17 17:48:49 +0300149 cfb_pitch, crtc->y, plane_name(intel_crtc->plane));
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300150}
151
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300152static bool i8xx_fbc_enabled(struct drm_device *dev)
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300153{
154 struct drm_i915_private *dev_priv = dev->dev_private;
155
156 return I915_READ(FBC_CONTROL) & FBC_CTL_EN;
157}
158
Ville Syrjälä993495a2013-12-12 17:27:40 +0200159static void g4x_enable_fbc(struct drm_crtc *crtc)
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300160{
161 struct drm_device *dev = crtc->dev;
162 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roperf4510a22014-04-01 15:22:40 -0700163 struct drm_framebuffer *fb = crtc->primary->fb;
Matt Roper2ff8fde2014-07-08 07:50:07 -0700164 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300165 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300166 u32 dpfc_ctl;
167
Ville Syrjälä3fa2e0e2014-01-23 16:49:12 +0200168 dpfc_ctl = DPFC_CTL_PLANE(intel_crtc->plane) | DPFC_SR_EN;
169 if (drm_format_plane_cpp(fb->pixel_format, 0) == 2)
170 dpfc_ctl |= DPFC_CTL_LIMIT_2X;
171 else
172 dpfc_ctl |= DPFC_CTL_LIMIT_1X;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300173 dpfc_ctl |= DPFC_CTL_FENCE_EN | obj->fence_reg;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300174
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300175 I915_WRITE(DPFC_FENCE_YOFF, crtc->y);
176
177 /* enable it... */
Ville Syrjäläfe74c1a2014-01-23 16:49:13 +0200178 I915_WRITE(DPFC_CONTROL, dpfc_ctl | DPFC_CTL_EN);
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300179
Ville Syrjälä84f44ce2013-04-17 17:48:49 +0300180 DRM_DEBUG_KMS("enabled fbc on plane %c\n", plane_name(intel_crtc->plane));
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300181}
182
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300183static void g4x_disable_fbc(struct drm_device *dev)
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300184{
185 struct drm_i915_private *dev_priv = dev->dev_private;
186 u32 dpfc_ctl;
187
188 /* Disable compression */
189 dpfc_ctl = I915_READ(DPFC_CONTROL);
190 if (dpfc_ctl & DPFC_CTL_EN) {
191 dpfc_ctl &= ~DPFC_CTL_EN;
192 I915_WRITE(DPFC_CONTROL, dpfc_ctl);
193
194 DRM_DEBUG_KMS("disabled FBC\n");
195 }
196}
197
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300198static bool g4x_fbc_enabled(struct drm_device *dev)
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300199{
200 struct drm_i915_private *dev_priv = dev->dev_private;
201
202 return I915_READ(DPFC_CONTROL) & DPFC_CTL_EN;
203}
204
205static void sandybridge_blit_fbc_update(struct drm_device *dev)
206{
207 struct drm_i915_private *dev_priv = dev->dev_private;
208 u32 blt_ecoskpd;
209
210 /* Make sure blitter notifies FBC of writes */
Deepak S940aece2013-11-23 14:55:43 +0530211
212 /* Blitter is part of Media powerwell on VLV. No impact of
213 * his param in other platforms for now */
214 gen6_gt_force_wake_get(dev_priv, FORCEWAKE_MEDIA);
Deepak Sc8d9a592013-11-23 14:55:42 +0530215
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300216 blt_ecoskpd = I915_READ(GEN6_BLITTER_ECOSKPD);
217 blt_ecoskpd |= GEN6_BLITTER_FBC_NOTIFY <<
218 GEN6_BLITTER_LOCK_SHIFT;
219 I915_WRITE(GEN6_BLITTER_ECOSKPD, blt_ecoskpd);
220 blt_ecoskpd |= GEN6_BLITTER_FBC_NOTIFY;
221 I915_WRITE(GEN6_BLITTER_ECOSKPD, blt_ecoskpd);
222 blt_ecoskpd &= ~(GEN6_BLITTER_FBC_NOTIFY <<
223 GEN6_BLITTER_LOCK_SHIFT);
224 I915_WRITE(GEN6_BLITTER_ECOSKPD, blt_ecoskpd);
225 POSTING_READ(GEN6_BLITTER_ECOSKPD);
Deepak Sc8d9a592013-11-23 14:55:42 +0530226
Deepak S940aece2013-11-23 14:55:43 +0530227 gen6_gt_force_wake_put(dev_priv, FORCEWAKE_MEDIA);
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300228}
229
Ville Syrjälä993495a2013-12-12 17:27:40 +0200230static void ironlake_enable_fbc(struct drm_crtc *crtc)
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300231{
232 struct drm_device *dev = crtc->dev;
233 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roperf4510a22014-04-01 15:22:40 -0700234 struct drm_framebuffer *fb = crtc->primary->fb;
Matt Roper2ff8fde2014-07-08 07:50:07 -0700235 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300236 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300237 u32 dpfc_ctl;
238
Ville Syrjälä46f3dab2014-01-23 16:49:14 +0200239 dpfc_ctl = DPFC_CTL_PLANE(intel_crtc->plane);
Ville Syrjälä3fa2e0e2014-01-23 16:49:12 +0200240 if (drm_format_plane_cpp(fb->pixel_format, 0) == 2)
Ben Widawsky5e59f712014-06-30 10:41:24 -0700241 dev_priv->fbc.threshold++;
242
243 switch (dev_priv->fbc.threshold) {
244 case 4:
245 case 3:
246 dpfc_ctl |= DPFC_CTL_LIMIT_4X;
247 break;
248 case 2:
Ville Syrjälä3fa2e0e2014-01-23 16:49:12 +0200249 dpfc_ctl |= DPFC_CTL_LIMIT_2X;
Ben Widawsky5e59f712014-06-30 10:41:24 -0700250 break;
251 case 1:
Ville Syrjälä3fa2e0e2014-01-23 16:49:12 +0200252 dpfc_ctl |= DPFC_CTL_LIMIT_1X;
Ben Widawsky5e59f712014-06-30 10:41:24 -0700253 break;
254 }
Ville Syrjäläd6293362013-11-21 21:29:45 +0200255 dpfc_ctl |= DPFC_CTL_FENCE_EN;
256 if (IS_GEN5(dev))
257 dpfc_ctl |= obj->fence_reg;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300258
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300259 I915_WRITE(ILK_DPFC_FENCE_YOFF, crtc->y);
Ben Widawskyf343c5f2013-07-05 14:41:04 -0700260 I915_WRITE(ILK_FBC_RT_BASE, i915_gem_obj_ggtt_offset(obj) | ILK_FBC_RT_VALID);
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300261 /* enable it... */
262 I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl | DPFC_CTL_EN);
263
264 if (IS_GEN6(dev)) {
265 I915_WRITE(SNB_DPFC_CTL_SA,
266 SNB_CPU_FENCE_ENABLE | obj->fence_reg);
267 I915_WRITE(DPFC_CPU_FENCE_OFFSET, crtc->y);
268 sandybridge_blit_fbc_update(dev);
269 }
270
Ville Syrjälä84f44ce2013-04-17 17:48:49 +0300271 DRM_DEBUG_KMS("enabled fbc on plane %c\n", plane_name(intel_crtc->plane));
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300272}
273
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300274static void ironlake_disable_fbc(struct drm_device *dev)
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300275{
276 struct drm_i915_private *dev_priv = dev->dev_private;
277 u32 dpfc_ctl;
278
279 /* Disable compression */
280 dpfc_ctl = I915_READ(ILK_DPFC_CONTROL);
281 if (dpfc_ctl & DPFC_CTL_EN) {
282 dpfc_ctl &= ~DPFC_CTL_EN;
283 I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl);
284
285 DRM_DEBUG_KMS("disabled FBC\n");
286 }
287}
288
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300289static bool ironlake_fbc_enabled(struct drm_device *dev)
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300290{
291 struct drm_i915_private *dev_priv = dev->dev_private;
292
293 return I915_READ(ILK_DPFC_CONTROL) & DPFC_CTL_EN;
294}
295
Ville Syrjälä993495a2013-12-12 17:27:40 +0200296static void gen7_enable_fbc(struct drm_crtc *crtc)
Rodrigo Viviabe959c2013-05-06 19:37:33 -0300297{
298 struct drm_device *dev = crtc->dev;
299 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roperf4510a22014-04-01 15:22:40 -0700300 struct drm_framebuffer *fb = crtc->primary->fb;
Matt Roper2ff8fde2014-07-08 07:50:07 -0700301 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Rodrigo Viviabe959c2013-05-06 19:37:33 -0300302 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjälä3fa2e0e2014-01-23 16:49:12 +0200303 u32 dpfc_ctl;
Rodrigo Viviabe959c2013-05-06 19:37:33 -0300304
Ville Syrjälä3fa2e0e2014-01-23 16:49:12 +0200305 dpfc_ctl = IVB_DPFC_CTL_PLANE(intel_crtc->plane);
306 if (drm_format_plane_cpp(fb->pixel_format, 0) == 2)
Ben Widawsky5e59f712014-06-30 10:41:24 -0700307 dev_priv->fbc.threshold++;
308
309 switch (dev_priv->fbc.threshold) {
310 case 4:
311 case 3:
312 dpfc_ctl |= DPFC_CTL_LIMIT_4X;
313 break;
314 case 2:
Ville Syrjälä3fa2e0e2014-01-23 16:49:12 +0200315 dpfc_ctl |= DPFC_CTL_LIMIT_2X;
Ben Widawsky5e59f712014-06-30 10:41:24 -0700316 break;
317 case 1:
Ville Syrjälä3fa2e0e2014-01-23 16:49:12 +0200318 dpfc_ctl |= DPFC_CTL_LIMIT_1X;
Ben Widawsky5e59f712014-06-30 10:41:24 -0700319 break;
320 }
321
Ville Syrjälä3fa2e0e2014-01-23 16:49:12 +0200322 dpfc_ctl |= IVB_DPFC_CTL_FENCE_EN;
323
Rodrigo Vivida46f932014-08-01 02:04:45 -0700324 if (dev_priv->fbc.false_color)
325 dpfc_ctl |= FBC_CTL_FALSE_COLOR;
326
Ville Syrjälä3fa2e0e2014-01-23 16:49:12 +0200327 I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl | DPFC_CTL_EN);
Rodrigo Viviabe959c2013-05-06 19:37:33 -0300328
Rodrigo Vivi891348b2013-05-06 19:37:36 -0300329 if (IS_IVYBRIDGE(dev)) {
Damien Lespiau7dd23ba2013-05-10 14:33:17 +0100330 /* WaFbcAsynchFlipDisableFbcQueue:ivb */
Ville Syrjälä2adb6db2014-03-05 13:05:46 +0200331 I915_WRITE(ILK_DISPLAY_CHICKEN1,
332 I915_READ(ILK_DISPLAY_CHICKEN1) |
333 ILK_FBCQ_DIS);
Rodrigo Vivi28554162013-05-06 19:37:37 -0300334 } else {
Ville Syrjälä2adb6db2014-03-05 13:05:46 +0200335 /* WaFbcAsynchFlipDisableFbcQueue:hsw,bdw */
Ville Syrjälä8f670bb2014-03-05 13:05:47 +0200336 I915_WRITE(CHICKEN_PIPESL_1(intel_crtc->pipe),
337 I915_READ(CHICKEN_PIPESL_1(intel_crtc->pipe)) |
338 HSW_FBCQ_DIS);
Rodrigo Vivi891348b2013-05-06 19:37:36 -0300339 }
Rodrigo Vivib74ea102013-05-09 14:08:38 -0300340
Rodrigo Viviabe959c2013-05-06 19:37:33 -0300341 I915_WRITE(SNB_DPFC_CTL_SA,
342 SNB_CPU_FENCE_ENABLE | obj->fence_reg);
343 I915_WRITE(DPFC_CPU_FENCE_OFFSET, crtc->y);
344
345 sandybridge_blit_fbc_update(dev);
346
Ville Syrjäläb19870e2013-11-06 23:02:25 +0200347 DRM_DEBUG_KMS("enabled fbc on plane %c\n", plane_name(intel_crtc->plane));
Rodrigo Viviabe959c2013-05-06 19:37:33 -0300348}
349
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300350bool intel_fbc_enabled(struct drm_device *dev)
351{
352 struct drm_i915_private *dev_priv = dev->dev_private;
353
354 if (!dev_priv->display.fbc_enabled)
355 return false;
356
357 return dev_priv->display.fbc_enabled(dev);
358}
359
Rodrigo Vivic5ad0112014-08-04 03:51:38 -0700360void gen8_fbc_sw_flush(struct drm_device *dev, u32 value)
361{
362 struct drm_i915_private *dev_priv = dev->dev_private;
363
364 if (!IS_GEN8(dev))
365 return;
366
367 I915_WRITE(MSG_FBC_REND_STATE, value);
368}
369
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300370static void intel_fbc_work_fn(struct work_struct *__work)
371{
372 struct intel_fbc_work *work =
373 container_of(to_delayed_work(__work),
374 struct intel_fbc_work, work);
375 struct drm_device *dev = work->crtc->dev;
376 struct drm_i915_private *dev_priv = dev->dev_private;
377
378 mutex_lock(&dev->struct_mutex);
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -0700379 if (work == dev_priv->fbc.fbc_work) {
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300380 /* Double check that we haven't switched fb without cancelling
381 * the prior work.
382 */
Matt Roperf4510a22014-04-01 15:22:40 -0700383 if (work->crtc->primary->fb == work->fb) {
Ville Syrjälä993495a2013-12-12 17:27:40 +0200384 dev_priv->display.enable_fbc(work->crtc);
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300385
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -0700386 dev_priv->fbc.plane = to_intel_crtc(work->crtc)->plane;
Matt Roperf4510a22014-04-01 15:22:40 -0700387 dev_priv->fbc.fb_id = work->crtc->primary->fb->base.id;
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -0700388 dev_priv->fbc.y = work->crtc->y;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300389 }
390
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -0700391 dev_priv->fbc.fbc_work = NULL;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300392 }
393 mutex_unlock(&dev->struct_mutex);
394
395 kfree(work);
396}
397
398static void intel_cancel_fbc_work(struct drm_i915_private *dev_priv)
399{
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -0700400 if (dev_priv->fbc.fbc_work == NULL)
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300401 return;
402
403 DRM_DEBUG_KMS("cancelling pending FBC enable\n");
404
405 /* Synchronisation is provided by struct_mutex and checking of
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -0700406 * dev_priv->fbc.fbc_work, so we can perform the cancellation
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300407 * entirely asynchronously.
408 */
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -0700409 if (cancel_delayed_work(&dev_priv->fbc.fbc_work->work))
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300410 /* tasklet was killed before being run, clean up */
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -0700411 kfree(dev_priv->fbc.fbc_work);
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300412
413 /* Mark the work as no longer wanted so that if it does
414 * wake-up (because the work was already running and waiting
415 * for our mutex), it will discover that is no longer
416 * necessary to run.
417 */
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -0700418 dev_priv->fbc.fbc_work = NULL;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300419}
420
Ville Syrjälä993495a2013-12-12 17:27:40 +0200421static void intel_enable_fbc(struct drm_crtc *crtc)
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300422{
423 struct intel_fbc_work *work;
424 struct drm_device *dev = crtc->dev;
425 struct drm_i915_private *dev_priv = dev->dev_private;
426
427 if (!dev_priv->display.enable_fbc)
428 return;
429
430 intel_cancel_fbc_work(dev_priv);
431
Daniel Vetterb14c5672013-09-19 12:18:32 +0200432 work = kzalloc(sizeof(*work), GFP_KERNEL);
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300433 if (work == NULL) {
Paulo Zanoni6cdcb5e2013-06-12 17:27:29 -0300434 DRM_ERROR("Failed to allocate FBC work structure\n");
Ville Syrjälä993495a2013-12-12 17:27:40 +0200435 dev_priv->display.enable_fbc(crtc);
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300436 return;
437 }
438
439 work->crtc = crtc;
Matt Roperf4510a22014-04-01 15:22:40 -0700440 work->fb = crtc->primary->fb;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300441 INIT_DELAYED_WORK(&work->work, intel_fbc_work_fn);
442
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -0700443 dev_priv->fbc.fbc_work = work;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300444
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300445 /* Delay the actual enabling to let pageflipping cease and the
446 * display to settle before starting the compression. Note that
447 * this delay also serves a second purpose: it allows for a
448 * vblank to pass after disabling the FBC before we attempt
449 * to modify the control registers.
450 *
451 * A more complicated solution would involve tracking vblanks
452 * following the termination of the page-flipping sequence
453 * and indeed performing the enable as a co-routine and not
454 * waiting synchronously upon the vblank.
Damien Lespiau7457d612013-06-07 17:41:07 +0100455 *
456 * WaFbcWaitForVBlankBeforeEnable:ilk,snb
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300457 */
458 schedule_delayed_work(&work->work, msecs_to_jiffies(50));
459}
460
461void intel_disable_fbc(struct drm_device *dev)
462{
463 struct drm_i915_private *dev_priv = dev->dev_private;
464
465 intel_cancel_fbc_work(dev_priv);
466
467 if (!dev_priv->display.disable_fbc)
468 return;
469
470 dev_priv->display.disable_fbc(dev);
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -0700471 dev_priv->fbc.plane = -1;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300472}
473
Chris Wilson29ebf902013-07-27 17:23:55 +0100474static bool set_no_fbc_reason(struct drm_i915_private *dev_priv,
475 enum no_fbc_reason reason)
476{
477 if (dev_priv->fbc.no_fbc_reason == reason)
478 return false;
479
480 dev_priv->fbc.no_fbc_reason = reason;
481 return true;
482}
483
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300484/**
485 * intel_update_fbc - enable/disable FBC as needed
486 * @dev: the drm_device
487 *
488 * Set up the framebuffer compression hardware at mode set time. We
489 * enable it if possible:
490 * - plane A only (on pre-965)
491 * - no pixel mulitply/line duplication
492 * - no alpha buffer discard
493 * - no dual wide
Paulo Zanonif85da862013-06-04 16:53:39 -0300494 * - framebuffer <= max_hdisplay in width, max_vdisplay in height
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300495 *
496 * We can't assume that any compression will take place (worst case),
497 * so the compressed buffer has to be the same size as the uncompressed
498 * one. It also must reside (along with the line length buffer) in
499 * stolen memory.
500 *
501 * We need to enable/disable FBC on a global basis.
502 */
503void intel_update_fbc(struct drm_device *dev)
504{
505 struct drm_i915_private *dev_priv = dev->dev_private;
506 struct drm_crtc *crtc = NULL, *tmp_crtc;
507 struct intel_crtc *intel_crtc;
508 struct drm_framebuffer *fb;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300509 struct drm_i915_gem_object *obj;
Ville Syrjäläef644fd2013-09-04 18:25:21 +0300510 const struct drm_display_mode *adjusted_mode;
Ville Syrjälä37327ab2013-09-04 18:25:28 +0300511 unsigned int max_width, max_height;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300512
Daniel Vetter3a77c4c2014-01-10 08:50:12 +0100513 if (!HAS_FBC(dev)) {
Chris Wilson29ebf902013-07-27 17:23:55 +0100514 set_no_fbc_reason(dev_priv, FBC_UNSUPPORTED);
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300515 return;
Chris Wilson29ebf902013-07-27 17:23:55 +0100516 }
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300517
Jani Nikulad330a952014-01-21 11:24:25 +0200518 if (!i915.powersave) {
Chris Wilson29ebf902013-07-27 17:23:55 +0100519 if (set_no_fbc_reason(dev_priv, FBC_MODULE_PARAM))
520 DRM_DEBUG_KMS("fbc disabled per module param\n");
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300521 return;
Chris Wilson29ebf902013-07-27 17:23:55 +0100522 }
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300523
524 /*
525 * If FBC is already on, we just have to verify that we can
526 * keep it that way...
527 * Need to disable if:
528 * - more than one pipe is active
529 * - changing FBC params (stride, fence, mode)
530 * - new fb is too large to fit in compressed buffer
531 * - going to an unsupported config (interlace, pixel multiply, etc.)
532 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +0100533 for_each_crtc(dev, tmp_crtc) {
Chris Wilson3490ea52013-01-07 10:11:40 +0000534 if (intel_crtc_active(tmp_crtc) &&
Ville Syrjälä4c445e02013-10-09 17:24:58 +0300535 to_intel_crtc(tmp_crtc)->primary_enabled) {
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300536 if (crtc) {
Chris Wilson29ebf902013-07-27 17:23:55 +0100537 if (set_no_fbc_reason(dev_priv, FBC_MULTIPLE_PIPES))
538 DRM_DEBUG_KMS("more than one pipe active, disabling compression\n");
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300539 goto out_disable;
540 }
541 crtc = tmp_crtc;
542 }
543 }
544
Matt Roperf4510a22014-04-01 15:22:40 -0700545 if (!crtc || crtc->primary->fb == NULL) {
Chris Wilson29ebf902013-07-27 17:23:55 +0100546 if (set_no_fbc_reason(dev_priv, FBC_NO_OUTPUT))
547 DRM_DEBUG_KMS("no output, disabling\n");
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300548 goto out_disable;
549 }
550
551 intel_crtc = to_intel_crtc(crtc);
Matt Roperf4510a22014-04-01 15:22:40 -0700552 fb = crtc->primary->fb;
Matt Roper2ff8fde2014-07-08 07:50:07 -0700553 obj = intel_fb_obj(fb);
Ville Syrjäläef644fd2013-09-04 18:25:21 +0300554 adjusted_mode = &intel_crtc->config.adjusted_mode;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300555
Chris Wilson03689202014-06-06 10:37:11 +0100556 if (i915.enable_fbc < 0) {
Chris Wilson29ebf902013-07-27 17:23:55 +0100557 if (set_no_fbc_reason(dev_priv, FBC_CHIP_DEFAULT))
558 DRM_DEBUG_KMS("disabled per chip default\n");
Damien Lespiau8a5729a2013-06-24 16:22:02 +0100559 goto out_disable;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300560 }
Jani Nikulad330a952014-01-21 11:24:25 +0200561 if (!i915.enable_fbc) {
Chris Wilson29ebf902013-07-27 17:23:55 +0100562 if (set_no_fbc_reason(dev_priv, FBC_MODULE_PARAM))
563 DRM_DEBUG_KMS("fbc disabled per module param\n");
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300564 goto out_disable;
565 }
Ville Syrjäläef644fd2013-09-04 18:25:21 +0300566 if ((adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) ||
567 (adjusted_mode->flags & DRM_MODE_FLAG_DBLSCAN)) {
Chris Wilson29ebf902013-07-27 17:23:55 +0100568 if (set_no_fbc_reason(dev_priv, FBC_UNSUPPORTED_MODE))
569 DRM_DEBUG_KMS("mode incompatible with compression, "
570 "disabling\n");
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300571 goto out_disable;
572 }
Paulo Zanonif85da862013-06-04 16:53:39 -0300573
Daisy Sun032843a2014-06-16 15:48:18 -0700574 if (INTEL_INFO(dev)->gen >= 8 || IS_HASWELL(dev)) {
575 max_width = 4096;
576 max_height = 4096;
577 } else if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
Ville Syrjälä37327ab2013-09-04 18:25:28 +0300578 max_width = 4096;
579 max_height = 2048;
Paulo Zanonif85da862013-06-04 16:53:39 -0300580 } else {
Ville Syrjälä37327ab2013-09-04 18:25:28 +0300581 max_width = 2048;
582 max_height = 1536;
Paulo Zanonif85da862013-06-04 16:53:39 -0300583 }
Ville Syrjälä37327ab2013-09-04 18:25:28 +0300584 if (intel_crtc->config.pipe_src_w > max_width ||
585 intel_crtc->config.pipe_src_h > max_height) {
Chris Wilson29ebf902013-07-27 17:23:55 +0100586 if (set_no_fbc_reason(dev_priv, FBC_MODE_TOO_LARGE))
587 DRM_DEBUG_KMS("mode too large for compression, disabling\n");
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300588 goto out_disable;
589 }
Ben Widawsky8f94d242014-02-20 16:01:20 -0800590 if ((INTEL_INFO(dev)->gen < 4 || HAS_DDI(dev)) &&
Ville Syrjäläc5a44aa2013-11-28 17:29:58 +0200591 intel_crtc->plane != PLANE_A) {
Chris Wilson29ebf902013-07-27 17:23:55 +0100592 if (set_no_fbc_reason(dev_priv, FBC_BAD_PLANE))
Ville Syrjäläc5a44aa2013-11-28 17:29:58 +0200593 DRM_DEBUG_KMS("plane not A, disabling compression\n");
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300594 goto out_disable;
595 }
596
597 /* The use of a CPU fence is mandatory in order to detect writes
598 * by the CPU to the scanout and trigger updates to the FBC.
599 */
600 if (obj->tiling_mode != I915_TILING_X ||
601 obj->fence_reg == I915_FENCE_REG_NONE) {
Chris Wilson29ebf902013-07-27 17:23:55 +0100602 if (set_no_fbc_reason(dev_priv, FBC_NOT_TILED))
603 DRM_DEBUG_KMS("framebuffer not tiled or fenced, disabling compression\n");
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300604 goto out_disable;
605 }
Sonika Jindal48404c12014-08-22 14:06:04 +0530606 if (INTEL_INFO(dev)->gen <= 4 && !IS_G4X(dev) &&
607 to_intel_plane(crtc->primary)->rotation != BIT(DRM_ROTATE_0)) {
608 if (set_no_fbc_reason(dev_priv, FBC_UNSUPPORTED_MODE))
609 DRM_DEBUG_KMS("Rotation unsupported, disabling\n");
610 goto out_disable;
611 }
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300612
613 /* If the kernel debugger is active, always disable compression */
614 if (in_dbg_master())
615 goto out_disable;
616
Matt Roper2ff8fde2014-07-08 07:50:07 -0700617 if (i915_gem_stolen_setup_compression(dev, obj->base.size,
Ben Widawsky5e59f712014-06-30 10:41:24 -0700618 drm_format_plane_cpp(fb->pixel_format, 0))) {
Chris Wilson29ebf902013-07-27 17:23:55 +0100619 if (set_no_fbc_reason(dev_priv, FBC_STOLEN_TOO_SMALL))
620 DRM_DEBUG_KMS("framebuffer too large, disabling compression\n");
Chris Wilson11be49e2012-11-15 11:32:20 +0000621 goto out_disable;
622 }
623
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300624 /* If the scanout has not changed, don't modify the FBC settings.
625 * Note that we make the fundamental assumption that the fb->obj
626 * cannot be unpinned (and have its GTT offset and fence revoked)
627 * without first being decoupled from the scanout and FBC disabled.
628 */
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -0700629 if (dev_priv->fbc.plane == intel_crtc->plane &&
630 dev_priv->fbc.fb_id == fb->base.id &&
631 dev_priv->fbc.y == crtc->y)
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300632 return;
633
634 if (intel_fbc_enabled(dev)) {
635 /* We update FBC along two paths, after changing fb/crtc
636 * configuration (modeswitching) and after page-flipping
637 * finishes. For the latter, we know that not only did
638 * we disable the FBC at the start of the page-flip
639 * sequence, but also more than one vblank has passed.
640 *
641 * For the former case of modeswitching, it is possible
642 * to switch between two FBC valid configurations
643 * instantaneously so we do need to disable the FBC
644 * before we can modify its control registers. We also
645 * have to wait for the next vblank for that to take
646 * effect. However, since we delay enabling FBC we can
647 * assume that a vblank has passed since disabling and
648 * that we can safely alter the registers in the deferred
649 * callback.
650 *
651 * In the scenario that we go from a valid to invalid
652 * and then back to valid FBC configuration we have
653 * no strict enforcement that a vblank occurred since
654 * disabling the FBC. However, along all current pipe
655 * disabling paths we do need to wait for a vblank at
656 * some point. And we wait before enabling FBC anyway.
657 */
658 DRM_DEBUG_KMS("disabling active FBC for update\n");
659 intel_disable_fbc(dev);
660 }
661
Ville Syrjälä993495a2013-12-12 17:27:40 +0200662 intel_enable_fbc(crtc);
Chris Wilson29ebf902013-07-27 17:23:55 +0100663 dev_priv->fbc.no_fbc_reason = FBC_OK;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300664 return;
665
666out_disable:
667 /* Multiple disables should be harmless */
668 if (intel_fbc_enabled(dev)) {
669 DRM_DEBUG_KMS("unsupported config, disabling FBC\n");
670 intel_disable_fbc(dev);
671 }
Chris Wilson11be49e2012-11-15 11:32:20 +0000672 i915_gem_stolen_cleanup_compression(dev);
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300673}
674
Daniel Vetterc921aba2012-04-26 23:28:17 +0200675static void i915_pineview_get_mem_freq(struct drm_device *dev)
676{
Jani Nikula50227e12014-03-31 14:27:21 +0300677 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200678 u32 tmp;
679
680 tmp = I915_READ(CLKCFG);
681
682 switch (tmp & CLKCFG_FSB_MASK) {
683 case CLKCFG_FSB_533:
684 dev_priv->fsb_freq = 533; /* 133*4 */
685 break;
686 case CLKCFG_FSB_800:
687 dev_priv->fsb_freq = 800; /* 200*4 */
688 break;
689 case CLKCFG_FSB_667:
690 dev_priv->fsb_freq = 667; /* 167*4 */
691 break;
692 case CLKCFG_FSB_400:
693 dev_priv->fsb_freq = 400; /* 100*4 */
694 break;
695 }
696
697 switch (tmp & CLKCFG_MEM_MASK) {
698 case CLKCFG_MEM_533:
699 dev_priv->mem_freq = 533;
700 break;
701 case CLKCFG_MEM_667:
702 dev_priv->mem_freq = 667;
703 break;
704 case CLKCFG_MEM_800:
705 dev_priv->mem_freq = 800;
706 break;
707 }
708
709 /* detect pineview DDR3 setting */
710 tmp = I915_READ(CSHRDDR3CTL);
711 dev_priv->is_ddr3 = (tmp & CSHRDDR3CTL_DDR3) ? 1 : 0;
712}
713
714static void i915_ironlake_get_mem_freq(struct drm_device *dev)
715{
Jani Nikula50227e12014-03-31 14:27:21 +0300716 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200717 u16 ddrpll, csipll;
718
719 ddrpll = I915_READ16(DDRMPLL1);
720 csipll = I915_READ16(CSIPLL0);
721
722 switch (ddrpll & 0xff) {
723 case 0xc:
724 dev_priv->mem_freq = 800;
725 break;
726 case 0x10:
727 dev_priv->mem_freq = 1066;
728 break;
729 case 0x14:
730 dev_priv->mem_freq = 1333;
731 break;
732 case 0x18:
733 dev_priv->mem_freq = 1600;
734 break;
735 default:
736 DRM_DEBUG_DRIVER("unknown memory frequency 0x%02x\n",
737 ddrpll & 0xff);
738 dev_priv->mem_freq = 0;
739 break;
740 }
741
Daniel Vetter20e4d402012-08-08 23:35:39 +0200742 dev_priv->ips.r_t = dev_priv->mem_freq;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200743
744 switch (csipll & 0x3ff) {
745 case 0x00c:
746 dev_priv->fsb_freq = 3200;
747 break;
748 case 0x00e:
749 dev_priv->fsb_freq = 3733;
750 break;
751 case 0x010:
752 dev_priv->fsb_freq = 4266;
753 break;
754 case 0x012:
755 dev_priv->fsb_freq = 4800;
756 break;
757 case 0x014:
758 dev_priv->fsb_freq = 5333;
759 break;
760 case 0x016:
761 dev_priv->fsb_freq = 5866;
762 break;
763 case 0x018:
764 dev_priv->fsb_freq = 6400;
765 break;
766 default:
767 DRM_DEBUG_DRIVER("unknown fsb frequency 0x%04x\n",
768 csipll & 0x3ff);
769 dev_priv->fsb_freq = 0;
770 break;
771 }
772
773 if (dev_priv->fsb_freq == 3200) {
Daniel Vetter20e4d402012-08-08 23:35:39 +0200774 dev_priv->ips.c_m = 0;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200775 } else if (dev_priv->fsb_freq > 3200 && dev_priv->fsb_freq <= 4800) {
Daniel Vetter20e4d402012-08-08 23:35:39 +0200776 dev_priv->ips.c_m = 1;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200777 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +0200778 dev_priv->ips.c_m = 2;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200779 }
780}
781
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300782static const struct cxsr_latency cxsr_latency_table[] = {
783 {1, 0, 800, 400, 3382, 33382, 3983, 33983}, /* DDR2-400 SC */
784 {1, 0, 800, 667, 3354, 33354, 3807, 33807}, /* DDR2-667 SC */
785 {1, 0, 800, 800, 3347, 33347, 3763, 33763}, /* DDR2-800 SC */
786 {1, 1, 800, 667, 6420, 36420, 6873, 36873}, /* DDR3-667 SC */
787 {1, 1, 800, 800, 5902, 35902, 6318, 36318}, /* DDR3-800 SC */
788
789 {1, 0, 667, 400, 3400, 33400, 4021, 34021}, /* DDR2-400 SC */
790 {1, 0, 667, 667, 3372, 33372, 3845, 33845}, /* DDR2-667 SC */
791 {1, 0, 667, 800, 3386, 33386, 3822, 33822}, /* DDR2-800 SC */
792 {1, 1, 667, 667, 6438, 36438, 6911, 36911}, /* DDR3-667 SC */
793 {1, 1, 667, 800, 5941, 35941, 6377, 36377}, /* DDR3-800 SC */
794
795 {1, 0, 400, 400, 3472, 33472, 4173, 34173}, /* DDR2-400 SC */
796 {1, 0, 400, 667, 3443, 33443, 3996, 33996}, /* DDR2-667 SC */
797 {1, 0, 400, 800, 3430, 33430, 3946, 33946}, /* DDR2-800 SC */
798 {1, 1, 400, 667, 6509, 36509, 7062, 37062}, /* DDR3-667 SC */
799 {1, 1, 400, 800, 5985, 35985, 6501, 36501}, /* DDR3-800 SC */
800
801 {0, 0, 800, 400, 3438, 33438, 4065, 34065}, /* DDR2-400 SC */
802 {0, 0, 800, 667, 3410, 33410, 3889, 33889}, /* DDR2-667 SC */
803 {0, 0, 800, 800, 3403, 33403, 3845, 33845}, /* DDR2-800 SC */
804 {0, 1, 800, 667, 6476, 36476, 6955, 36955}, /* DDR3-667 SC */
805 {0, 1, 800, 800, 5958, 35958, 6400, 36400}, /* DDR3-800 SC */
806
807 {0, 0, 667, 400, 3456, 33456, 4103, 34106}, /* DDR2-400 SC */
808 {0, 0, 667, 667, 3428, 33428, 3927, 33927}, /* DDR2-667 SC */
809 {0, 0, 667, 800, 3443, 33443, 3905, 33905}, /* DDR2-800 SC */
810 {0, 1, 667, 667, 6494, 36494, 6993, 36993}, /* DDR3-667 SC */
811 {0, 1, 667, 800, 5998, 35998, 6460, 36460}, /* DDR3-800 SC */
812
813 {0, 0, 400, 400, 3528, 33528, 4255, 34255}, /* DDR2-400 SC */
814 {0, 0, 400, 667, 3500, 33500, 4079, 34079}, /* DDR2-667 SC */
815 {0, 0, 400, 800, 3487, 33487, 4029, 34029}, /* DDR2-800 SC */
816 {0, 1, 400, 667, 6566, 36566, 7145, 37145}, /* DDR3-667 SC */
817 {0, 1, 400, 800, 6042, 36042, 6584, 36584}, /* DDR3-800 SC */
818};
819
Daniel Vetter63c62272012-04-21 23:17:55 +0200820static const struct cxsr_latency *intel_get_cxsr_latency(int is_desktop,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300821 int is_ddr3,
822 int fsb,
823 int mem)
824{
825 const struct cxsr_latency *latency;
826 int i;
827
828 if (fsb == 0 || mem == 0)
829 return NULL;
830
831 for (i = 0; i < ARRAY_SIZE(cxsr_latency_table); i++) {
832 latency = &cxsr_latency_table[i];
833 if (is_desktop == latency->is_desktop &&
834 is_ddr3 == latency->is_ddr3 &&
835 fsb == latency->fsb_freq && mem == latency->mem_freq)
836 return latency;
837 }
838
839 DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
840
841 return NULL;
842}
843
Imre Deak5209b1f2014-07-01 12:36:17 +0300844void intel_set_memory_cxsr(struct drm_i915_private *dev_priv, bool enable)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300845{
Imre Deak5209b1f2014-07-01 12:36:17 +0300846 struct drm_device *dev = dev_priv->dev;
847 u32 val;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300848
Imre Deak5209b1f2014-07-01 12:36:17 +0300849 if (IS_VALLEYVIEW(dev)) {
850 I915_WRITE(FW_BLC_SELF_VLV, enable ? FW_CSPWRDWNEN : 0);
851 } else if (IS_G4X(dev) || IS_CRESTLINE(dev)) {
852 I915_WRITE(FW_BLC_SELF, enable ? FW_BLC_SELF_EN : 0);
853 } else if (IS_PINEVIEW(dev)) {
854 val = I915_READ(DSPFW3) & ~PINEVIEW_SELF_REFRESH_EN;
855 val |= enable ? PINEVIEW_SELF_REFRESH_EN : 0;
856 I915_WRITE(DSPFW3, val);
857 } else if (IS_I945G(dev) || IS_I945GM(dev)) {
858 val = enable ? _MASKED_BIT_ENABLE(FW_BLC_SELF_EN) :
859 _MASKED_BIT_DISABLE(FW_BLC_SELF_EN);
860 I915_WRITE(FW_BLC_SELF, val);
861 } else if (IS_I915GM(dev)) {
862 val = enable ? _MASKED_BIT_ENABLE(INSTPM_SELF_EN) :
863 _MASKED_BIT_DISABLE(INSTPM_SELF_EN);
864 I915_WRITE(INSTPM, val);
865 } else {
866 return;
867 }
868
869 DRM_DEBUG_KMS("memory self-refresh is %s\n",
870 enable ? "enabled" : "disabled");
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300871}
872
873/*
874 * Latency for FIFO fetches is dependent on several factors:
875 * - memory configuration (speed, channels)
876 * - chipset
877 * - current MCH state
878 * It can be fairly high in some situations, so here we assume a fairly
879 * pessimal value. It's a tradeoff between extra memory fetches (if we
880 * set this value too high, the FIFO will fetch frequently to stay full)
881 * and power consumption (set it too low to save power and we might see
882 * FIFO underruns and display "flicker").
883 *
884 * A value of 5us seems to be a good balance; safe for very low end
885 * platforms but not overly aggressive on lower latency configs.
886 */
Chris Wilson5aef6002014-09-03 11:56:07 +0100887static const int pessimal_latency_ns = 5000;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300888
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300889static int i9xx_get_fifo_size(struct drm_device *dev, int plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300890{
891 struct drm_i915_private *dev_priv = dev->dev_private;
892 uint32_t dsparb = I915_READ(DSPARB);
893 int size;
894
895 size = dsparb & 0x7f;
896 if (plane)
897 size = ((dsparb >> DSPARB_CSTART_SHIFT) & 0x7f) - size;
898
899 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
900 plane ? "B" : "A", size);
901
902 return size;
903}
904
Daniel Vetterfeb56b92013-12-14 20:38:30 -0200905static int i830_get_fifo_size(struct drm_device *dev, int plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300906{
907 struct drm_i915_private *dev_priv = dev->dev_private;
908 uint32_t dsparb = I915_READ(DSPARB);
909 int size;
910
911 size = dsparb & 0x1ff;
912 if (plane)
913 size = ((dsparb >> DSPARB_BEND_SHIFT) & 0x1ff) - size;
914 size >>= 1; /* Convert to cachelines */
915
916 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
917 plane ? "B" : "A", size);
918
919 return size;
920}
921
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300922static int i845_get_fifo_size(struct drm_device *dev, int plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300923{
924 struct drm_i915_private *dev_priv = dev->dev_private;
925 uint32_t dsparb = I915_READ(DSPARB);
926 int size;
927
928 size = dsparb & 0x7f;
929 size >>= 2; /* Convert to cachelines */
930
931 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
932 plane ? "B" : "A",
933 size);
934
935 return size;
936}
937
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300938/* Pineview has different values for various configs */
939static const struct intel_watermark_params pineview_display_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300940 .fifo_size = PINEVIEW_DISPLAY_FIFO,
941 .max_wm = PINEVIEW_MAX_WM,
942 .default_wm = PINEVIEW_DFT_WM,
943 .guard_size = PINEVIEW_GUARD_WM,
944 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300945};
946static const struct intel_watermark_params pineview_display_hplloff_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300947 .fifo_size = PINEVIEW_DISPLAY_FIFO,
948 .max_wm = PINEVIEW_MAX_WM,
949 .default_wm = PINEVIEW_DFT_HPLLOFF_WM,
950 .guard_size = PINEVIEW_GUARD_WM,
951 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300952};
953static const struct intel_watermark_params pineview_cursor_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300954 .fifo_size = PINEVIEW_CURSOR_FIFO,
955 .max_wm = PINEVIEW_CURSOR_MAX_WM,
956 .default_wm = PINEVIEW_CURSOR_DFT_WM,
957 .guard_size = PINEVIEW_CURSOR_GUARD_WM,
958 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300959};
960static const struct intel_watermark_params pineview_cursor_hplloff_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300961 .fifo_size = PINEVIEW_CURSOR_FIFO,
962 .max_wm = PINEVIEW_CURSOR_MAX_WM,
963 .default_wm = PINEVIEW_CURSOR_DFT_WM,
964 .guard_size = PINEVIEW_CURSOR_GUARD_WM,
965 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300966};
967static const struct intel_watermark_params g4x_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300968 .fifo_size = G4X_FIFO_SIZE,
969 .max_wm = G4X_MAX_WM,
970 .default_wm = G4X_MAX_WM,
971 .guard_size = 2,
972 .cacheline_size = G4X_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300973};
974static const struct intel_watermark_params g4x_cursor_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300975 .fifo_size = I965_CURSOR_FIFO,
976 .max_wm = I965_CURSOR_MAX_WM,
977 .default_wm = I965_CURSOR_DFT_WM,
978 .guard_size = 2,
979 .cacheline_size = G4X_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300980};
981static const struct intel_watermark_params valleyview_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300982 .fifo_size = VALLEYVIEW_FIFO_SIZE,
983 .max_wm = VALLEYVIEW_MAX_WM,
984 .default_wm = VALLEYVIEW_MAX_WM,
985 .guard_size = 2,
986 .cacheline_size = G4X_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300987};
988static const struct intel_watermark_params valleyview_cursor_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300989 .fifo_size = I965_CURSOR_FIFO,
990 .max_wm = VALLEYVIEW_CURSOR_MAX_WM,
991 .default_wm = I965_CURSOR_DFT_WM,
992 .guard_size = 2,
993 .cacheline_size = G4X_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300994};
995static const struct intel_watermark_params i965_cursor_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300996 .fifo_size = I965_CURSOR_FIFO,
997 .max_wm = I965_CURSOR_MAX_WM,
998 .default_wm = I965_CURSOR_DFT_WM,
999 .guard_size = 2,
1000 .cacheline_size = I915_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001001};
1002static const struct intel_watermark_params i945_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +03001003 .fifo_size = I945_FIFO_SIZE,
1004 .max_wm = I915_MAX_WM,
1005 .default_wm = 1,
1006 .guard_size = 2,
1007 .cacheline_size = I915_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001008};
1009static const struct intel_watermark_params i915_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +03001010 .fifo_size = I915_FIFO_SIZE,
1011 .max_wm = I915_MAX_WM,
1012 .default_wm = 1,
1013 .guard_size = 2,
1014 .cacheline_size = I915_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001015};
Ville Syrjälä9d539102014-08-15 01:21:53 +03001016static const struct intel_watermark_params i830_a_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +03001017 .fifo_size = I855GM_FIFO_SIZE,
1018 .max_wm = I915_MAX_WM,
1019 .default_wm = 1,
1020 .guard_size = 2,
1021 .cacheline_size = I830_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001022};
Ville Syrjälä9d539102014-08-15 01:21:53 +03001023static const struct intel_watermark_params i830_bc_wm_info = {
1024 .fifo_size = I855GM_FIFO_SIZE,
1025 .max_wm = I915_MAX_WM/2,
1026 .default_wm = 1,
1027 .guard_size = 2,
1028 .cacheline_size = I830_FIFO_LINE_SIZE,
1029};
Daniel Vetterfeb56b92013-12-14 20:38:30 -02001030static const struct intel_watermark_params i845_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +03001031 .fifo_size = I830_FIFO_SIZE,
1032 .max_wm = I915_MAX_WM,
1033 .default_wm = 1,
1034 .guard_size = 2,
1035 .cacheline_size = I830_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001036};
1037
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001038/**
1039 * intel_calculate_wm - calculate watermark level
1040 * @clock_in_khz: pixel clock
1041 * @wm: chip FIFO params
1042 * @pixel_size: display pixel size
1043 * @latency_ns: memory latency for the platform
1044 *
1045 * Calculate the watermark level (the level at which the display plane will
1046 * start fetching from memory again). Each chip has a different display
1047 * FIFO size and allocation, so the caller needs to figure that out and pass
1048 * in the correct intel_watermark_params structure.
1049 *
1050 * As the pixel clock runs, the FIFO will be drained at a rate that depends
1051 * on the pixel size. When it reaches the watermark level, it'll start
1052 * fetching FIFO line sized based chunks from memory until the FIFO fills
1053 * past the watermark point. If the FIFO drains completely, a FIFO underrun
1054 * will occur, and a display engine hang could result.
1055 */
1056static unsigned long intel_calculate_wm(unsigned long clock_in_khz,
1057 const struct intel_watermark_params *wm,
1058 int fifo_size,
1059 int pixel_size,
1060 unsigned long latency_ns)
1061{
1062 long entries_required, wm_size;
1063
1064 /*
1065 * Note: we need to make sure we don't overflow for various clock &
1066 * latency values.
1067 * clocks go from a few thousand to several hundred thousand.
1068 * latency is usually a few thousand
1069 */
1070 entries_required = ((clock_in_khz / 1000) * pixel_size * latency_ns) /
1071 1000;
1072 entries_required = DIV_ROUND_UP(entries_required, wm->cacheline_size);
1073
1074 DRM_DEBUG_KMS("FIFO entries required for mode: %ld\n", entries_required);
1075
1076 wm_size = fifo_size - (entries_required + wm->guard_size);
1077
1078 DRM_DEBUG_KMS("FIFO watermark level: %ld\n", wm_size);
1079
1080 /* Don't promote wm_size to unsigned... */
1081 if (wm_size > (long)wm->max_wm)
1082 wm_size = wm->max_wm;
1083 if (wm_size <= 0)
1084 wm_size = wm->default_wm;
1085 return wm_size;
1086}
1087
1088static struct drm_crtc *single_enabled_crtc(struct drm_device *dev)
1089{
1090 struct drm_crtc *crtc, *enabled = NULL;
1091
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01001092 for_each_crtc(dev, crtc) {
Chris Wilson3490ea52013-01-07 10:11:40 +00001093 if (intel_crtc_active(crtc)) {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001094 if (enabled)
1095 return NULL;
1096 enabled = crtc;
1097 }
1098 }
1099
1100 return enabled;
1101}
1102
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001103static void pineview_update_wm(struct drm_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001104{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001105 struct drm_device *dev = unused_crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001106 struct drm_i915_private *dev_priv = dev->dev_private;
1107 struct drm_crtc *crtc;
1108 const struct cxsr_latency *latency;
1109 u32 reg;
1110 unsigned long wm;
1111
1112 latency = intel_get_cxsr_latency(IS_PINEVIEW_G(dev), dev_priv->is_ddr3,
1113 dev_priv->fsb_freq, dev_priv->mem_freq);
1114 if (!latency) {
1115 DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
Imre Deak5209b1f2014-07-01 12:36:17 +03001116 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001117 return;
1118 }
1119
1120 crtc = single_enabled_crtc(dev);
1121 if (crtc) {
Damien Lespiau241bfc32013-09-25 16:45:37 +01001122 const struct drm_display_mode *adjusted_mode;
Matt Roperf4510a22014-04-01 15:22:40 -07001123 int pixel_size = crtc->primary->fb->bits_per_pixel / 8;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001124 int clock;
1125
1126 adjusted_mode = &to_intel_crtc(crtc)->config.adjusted_mode;
1127 clock = adjusted_mode->crtc_clock;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001128
1129 /* Display SR */
1130 wm = intel_calculate_wm(clock, &pineview_display_wm,
1131 pineview_display_wm.fifo_size,
1132 pixel_size, latency->display_sr);
1133 reg = I915_READ(DSPFW1);
1134 reg &= ~DSPFW_SR_MASK;
1135 reg |= wm << DSPFW_SR_SHIFT;
1136 I915_WRITE(DSPFW1, reg);
1137 DRM_DEBUG_KMS("DSPFW1 register is %x\n", reg);
1138
1139 /* cursor SR */
1140 wm = intel_calculate_wm(clock, &pineview_cursor_wm,
1141 pineview_display_wm.fifo_size,
1142 pixel_size, latency->cursor_sr);
1143 reg = I915_READ(DSPFW3);
1144 reg &= ~DSPFW_CURSOR_SR_MASK;
1145 reg |= (wm & 0x3f) << DSPFW_CURSOR_SR_SHIFT;
1146 I915_WRITE(DSPFW3, reg);
1147
1148 /* Display HPLL off SR */
1149 wm = intel_calculate_wm(clock, &pineview_display_hplloff_wm,
1150 pineview_display_hplloff_wm.fifo_size,
1151 pixel_size, latency->display_hpll_disable);
1152 reg = I915_READ(DSPFW3);
1153 reg &= ~DSPFW_HPLL_SR_MASK;
1154 reg |= wm & DSPFW_HPLL_SR_MASK;
1155 I915_WRITE(DSPFW3, reg);
1156
1157 /* cursor HPLL off SR */
1158 wm = intel_calculate_wm(clock, &pineview_cursor_hplloff_wm,
1159 pineview_display_hplloff_wm.fifo_size,
1160 pixel_size, latency->cursor_hpll_disable);
1161 reg = I915_READ(DSPFW3);
1162 reg &= ~DSPFW_HPLL_CURSOR_MASK;
1163 reg |= (wm & 0x3f) << DSPFW_HPLL_CURSOR_SHIFT;
1164 I915_WRITE(DSPFW3, reg);
1165 DRM_DEBUG_KMS("DSPFW3 register is %x\n", reg);
1166
Imre Deak5209b1f2014-07-01 12:36:17 +03001167 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001168 } else {
Imre Deak5209b1f2014-07-01 12:36:17 +03001169 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001170 }
1171}
1172
1173static bool g4x_compute_wm0(struct drm_device *dev,
1174 int plane,
1175 const struct intel_watermark_params *display,
1176 int display_latency_ns,
1177 const struct intel_watermark_params *cursor,
1178 int cursor_latency_ns,
1179 int *plane_wm,
1180 int *cursor_wm)
1181{
1182 struct drm_crtc *crtc;
Ville Syrjälä4fe85902013-09-04 18:25:22 +03001183 const struct drm_display_mode *adjusted_mode;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001184 int htotal, hdisplay, clock, pixel_size;
1185 int line_time_us, line_count;
1186 int entries, tlb_miss;
1187
1188 crtc = intel_get_crtc_for_plane(dev, plane);
Chris Wilson3490ea52013-01-07 10:11:40 +00001189 if (!intel_crtc_active(crtc)) {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001190 *cursor_wm = cursor->guard_size;
1191 *plane_wm = display->guard_size;
1192 return false;
1193 }
1194
Ville Syrjälä4fe85902013-09-04 18:25:22 +03001195 adjusted_mode = &to_intel_crtc(crtc)->config.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001196 clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -08001197 htotal = adjusted_mode->crtc_htotal;
Ville Syrjälä37327ab2013-09-04 18:25:28 +03001198 hdisplay = to_intel_crtc(crtc)->config.pipe_src_w;
Matt Roperf4510a22014-04-01 15:22:40 -07001199 pixel_size = crtc->primary->fb->bits_per_pixel / 8;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001200
1201 /* Use the small buffer method to calculate plane watermark */
1202 entries = ((clock * pixel_size / 1000) * display_latency_ns) / 1000;
1203 tlb_miss = display->fifo_size*display->cacheline_size - hdisplay * 8;
1204 if (tlb_miss > 0)
1205 entries += tlb_miss;
1206 entries = DIV_ROUND_UP(entries, display->cacheline_size);
1207 *plane_wm = entries + display->guard_size;
1208 if (*plane_wm > (int)display->max_wm)
1209 *plane_wm = display->max_wm;
1210
1211 /* Use the large buffer method to calculate cursor watermark */
Ville Syrjälä922044c2014-02-14 14:18:57 +02001212 line_time_us = max(htotal * 1000 / clock, 1);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001213 line_count = (cursor_latency_ns / line_time_us + 1000) / 1000;
Chris Wilson7bb836d2014-03-26 12:38:14 +00001214 entries = line_count * to_intel_crtc(crtc)->cursor_width * pixel_size;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001215 tlb_miss = cursor->fifo_size*cursor->cacheline_size - hdisplay * 8;
1216 if (tlb_miss > 0)
1217 entries += tlb_miss;
1218 entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
1219 *cursor_wm = entries + cursor->guard_size;
1220 if (*cursor_wm > (int)cursor->max_wm)
1221 *cursor_wm = (int)cursor->max_wm;
1222
1223 return true;
1224}
1225
1226/*
1227 * Check the wm result.
1228 *
1229 * If any calculated watermark values is larger than the maximum value that
1230 * can be programmed into the associated watermark register, that watermark
1231 * must be disabled.
1232 */
1233static bool g4x_check_srwm(struct drm_device *dev,
1234 int display_wm, int cursor_wm,
1235 const struct intel_watermark_params *display,
1236 const struct intel_watermark_params *cursor)
1237{
1238 DRM_DEBUG_KMS("SR watermark: display plane %d, cursor %d\n",
1239 display_wm, cursor_wm);
1240
1241 if (display_wm > display->max_wm) {
1242 DRM_DEBUG_KMS("display watermark is too large(%d/%ld), disabling\n",
1243 display_wm, display->max_wm);
1244 return false;
1245 }
1246
1247 if (cursor_wm > cursor->max_wm) {
1248 DRM_DEBUG_KMS("cursor watermark is too large(%d/%ld), disabling\n",
1249 cursor_wm, cursor->max_wm);
1250 return false;
1251 }
1252
1253 if (!(display_wm || cursor_wm)) {
1254 DRM_DEBUG_KMS("SR latency is 0, disabling\n");
1255 return false;
1256 }
1257
1258 return true;
1259}
1260
1261static bool g4x_compute_srwm(struct drm_device *dev,
1262 int plane,
1263 int latency_ns,
1264 const struct intel_watermark_params *display,
1265 const struct intel_watermark_params *cursor,
1266 int *display_wm, int *cursor_wm)
1267{
1268 struct drm_crtc *crtc;
Ville Syrjälä4fe85902013-09-04 18:25:22 +03001269 const struct drm_display_mode *adjusted_mode;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001270 int hdisplay, htotal, pixel_size, clock;
1271 unsigned long line_time_us;
1272 int line_count, line_size;
1273 int small, large;
1274 int entries;
1275
1276 if (!latency_ns) {
1277 *display_wm = *cursor_wm = 0;
1278 return false;
1279 }
1280
1281 crtc = intel_get_crtc_for_plane(dev, plane);
Ville Syrjälä4fe85902013-09-04 18:25:22 +03001282 adjusted_mode = &to_intel_crtc(crtc)->config.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001283 clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -08001284 htotal = adjusted_mode->crtc_htotal;
Ville Syrjälä37327ab2013-09-04 18:25:28 +03001285 hdisplay = to_intel_crtc(crtc)->config.pipe_src_w;
Matt Roperf4510a22014-04-01 15:22:40 -07001286 pixel_size = crtc->primary->fb->bits_per_pixel / 8;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001287
Ville Syrjälä922044c2014-02-14 14:18:57 +02001288 line_time_us = max(htotal * 1000 / clock, 1);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001289 line_count = (latency_ns / line_time_us + 1000) / 1000;
1290 line_size = hdisplay * pixel_size;
1291
1292 /* Use the minimum of the small and large buffer method for primary */
1293 small = ((clock * pixel_size / 1000) * latency_ns) / 1000;
1294 large = line_count * line_size;
1295
1296 entries = DIV_ROUND_UP(min(small, large), display->cacheline_size);
1297 *display_wm = entries + display->guard_size;
1298
1299 /* calculate the self-refresh watermark for display cursor */
Chris Wilson7bb836d2014-03-26 12:38:14 +00001300 entries = line_count * pixel_size * to_intel_crtc(crtc)->cursor_width;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001301 entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
1302 *cursor_wm = entries + cursor->guard_size;
1303
1304 return g4x_check_srwm(dev,
1305 *display_wm, *cursor_wm,
1306 display, cursor);
1307}
1308
Gajanan Bhat0948c262014-08-07 01:58:24 +05301309static bool vlv_compute_drain_latency(struct drm_crtc *crtc,
1310 int pixel_size,
1311 int *prec_mult,
1312 int *drain_latency)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001313{
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001314 int entries;
Gajanan Bhat0948c262014-08-07 01:58:24 +05301315 int clock = to_intel_crtc(crtc)->config.adjusted_mode.crtc_clock;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001316
Gajanan Bhat0948c262014-08-07 01:58:24 +05301317 if (WARN(clock == 0, "Pixel clock is zero!\n"))
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001318 return false;
1319
Gajanan Bhat0948c262014-08-07 01:58:24 +05301320 if (WARN(pixel_size == 0, "Pixel size is zero!\n"))
1321 return false;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001322
Gajanan Bhata398e9c2014-08-05 23:15:54 +05301323 entries = DIV_ROUND_UP(clock, 1000) * pixel_size;
Gajanan Bhat0948c262014-08-07 01:58:24 +05301324 *prec_mult = (entries > 128) ? DRAIN_LATENCY_PRECISION_64 :
1325 DRAIN_LATENCY_PRECISION_32;
1326 *drain_latency = (64 * (*prec_mult) * 4) / entries;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001327
Gajanan Bhata398e9c2014-08-05 23:15:54 +05301328 if (*drain_latency > DRAIN_LATENCY_MASK)
1329 *drain_latency = DRAIN_LATENCY_MASK;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001330
1331 return true;
1332}
1333
1334/*
1335 * Update drain latency registers of memory arbiter
1336 *
1337 * Valleyview SoC has a new memory arbiter and needs drain latency registers
1338 * to be programmed. Each plane has a drain latency multiplier and a drain
1339 * latency value.
1340 */
1341
Gajanan Bhat41aad812014-07-16 18:24:03 +05301342static void vlv_update_drain_latency(struct drm_crtc *crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001343{
Gajanan Bhat0948c262014-08-07 01:58:24 +05301344 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
1345 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1346 int pixel_size;
1347 int drain_latency;
1348 enum pipe pipe = intel_crtc->pipe;
1349 int plane_prec, prec_mult, plane_dl;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001350
Gajanan Bhat0948c262014-08-07 01:58:24 +05301351 plane_dl = I915_READ(VLV_DDL(pipe)) & ~(DDL_PLANE_PRECISION_64 |
1352 DRAIN_LATENCY_MASK | DDL_CURSOR_PRECISION_64 |
1353 (DRAIN_LATENCY_MASK << DDL_CURSOR_SHIFT));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001354
Gajanan Bhat0948c262014-08-07 01:58:24 +05301355 if (!intel_crtc_active(crtc)) {
1356 I915_WRITE(VLV_DDL(pipe), plane_dl);
1357 return;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001358 }
1359
Gajanan Bhat0948c262014-08-07 01:58:24 +05301360 /* Primary plane Drain Latency */
1361 pixel_size = crtc->primary->fb->bits_per_pixel / 8; /* BPP */
1362 if (vlv_compute_drain_latency(crtc, pixel_size, &prec_mult, &drain_latency)) {
1363 plane_prec = (prec_mult == DRAIN_LATENCY_PRECISION_64) ?
1364 DDL_PLANE_PRECISION_64 :
1365 DDL_PLANE_PRECISION_32;
1366 plane_dl |= plane_prec | drain_latency;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001367 }
Gajanan Bhat0948c262014-08-07 01:58:24 +05301368
1369 /* Cursor Drain Latency
1370 * BPP is always 4 for cursor
1371 */
1372 pixel_size = 4;
1373
1374 /* Program cursor DL only if it is enabled */
1375 if (intel_crtc->cursor_base &&
1376 vlv_compute_drain_latency(crtc, pixel_size, &prec_mult, &drain_latency)) {
1377 plane_prec = (prec_mult == DRAIN_LATENCY_PRECISION_64) ?
1378 DDL_CURSOR_PRECISION_64 :
1379 DDL_CURSOR_PRECISION_32;
1380 plane_dl |= plane_prec | (drain_latency << DDL_CURSOR_SHIFT);
1381 }
1382
1383 I915_WRITE(VLV_DDL(pipe), plane_dl);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001384}
1385
1386#define single_plane_enabled(mask) is_power_of_2(mask)
1387
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001388static void valleyview_update_wm(struct drm_crtc *crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001389{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001390 struct drm_device *dev = crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001391 static const int sr_latency_ns = 12000;
1392 struct drm_i915_private *dev_priv = dev->dev_private;
1393 int planea_wm, planeb_wm, cursora_wm, cursorb_wm;
1394 int plane_sr, cursor_sr;
Chris Wilsonaf6c4572012-12-11 12:01:43 +00001395 int ignore_plane_sr, ignore_cursor_sr;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001396 unsigned int enabled = 0;
Imre Deak98584252014-06-13 14:54:20 +03001397 bool cxsr_enabled;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001398
Gajanan Bhat41aad812014-07-16 18:24:03 +05301399 vlv_update_drain_latency(crtc);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001400
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001401 if (g4x_compute_wm0(dev, PIPE_A,
Chris Wilson5aef6002014-09-03 11:56:07 +01001402 &valleyview_wm_info, pessimal_latency_ns,
1403 &valleyview_cursor_wm_info, pessimal_latency_ns,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001404 &planea_wm, &cursora_wm))
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001405 enabled |= 1 << PIPE_A;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001406
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001407 if (g4x_compute_wm0(dev, PIPE_B,
Chris Wilson5aef6002014-09-03 11:56:07 +01001408 &valleyview_wm_info, pessimal_latency_ns,
1409 &valleyview_cursor_wm_info, pessimal_latency_ns,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001410 &planeb_wm, &cursorb_wm))
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001411 enabled |= 1 << PIPE_B;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001412
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001413 if (single_plane_enabled(enabled) &&
1414 g4x_compute_srwm(dev, ffs(enabled) - 1,
1415 sr_latency_ns,
1416 &valleyview_wm_info,
1417 &valleyview_cursor_wm_info,
Chris Wilsonaf6c4572012-12-11 12:01:43 +00001418 &plane_sr, &ignore_cursor_sr) &&
1419 g4x_compute_srwm(dev, ffs(enabled) - 1,
1420 2*sr_latency_ns,
1421 &valleyview_wm_info,
1422 &valleyview_cursor_wm_info,
Chris Wilson52bd02d2012-12-07 10:43:24 +00001423 &ignore_plane_sr, &cursor_sr)) {
Imre Deak98584252014-06-13 14:54:20 +03001424 cxsr_enabled = true;
Chris Wilson52bd02d2012-12-07 10:43:24 +00001425 } else {
Imre Deak98584252014-06-13 14:54:20 +03001426 cxsr_enabled = false;
Imre Deak5209b1f2014-07-01 12:36:17 +03001427 intel_set_memory_cxsr(dev_priv, false);
Chris Wilson52bd02d2012-12-07 10:43:24 +00001428 plane_sr = cursor_sr = 0;
1429 }
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001430
Ville Syrjäläa5043452014-06-28 02:04:18 +03001431 DRM_DEBUG_KMS("Setting FIFO watermarks - A: plane=%d, cursor=%d, "
1432 "B: plane=%d, cursor=%d, SR: plane=%d, cursor=%d\n",
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001433 planea_wm, cursora_wm,
1434 planeb_wm, cursorb_wm,
1435 plane_sr, cursor_sr);
1436
1437 I915_WRITE(DSPFW1,
1438 (plane_sr << DSPFW_SR_SHIFT) |
1439 (cursorb_wm << DSPFW_CURSORB_SHIFT) |
1440 (planeb_wm << DSPFW_PLANEB_SHIFT) |
Ville Syrjälä0a560672014-06-11 16:51:18 +03001441 (planea_wm << DSPFW_PLANEA_SHIFT));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001442 I915_WRITE(DSPFW2,
Chris Wilson8c919b22012-12-04 16:33:19 +00001443 (I915_READ(DSPFW2) & ~DSPFW_CURSORA_MASK) |
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001444 (cursora_wm << DSPFW_CURSORA_SHIFT));
1445 I915_WRITE(DSPFW3,
Chris Wilson8c919b22012-12-04 16:33:19 +00001446 (I915_READ(DSPFW3) & ~DSPFW_CURSOR_SR_MASK) |
1447 (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
Imre Deak98584252014-06-13 14:54:20 +03001448
1449 if (cxsr_enabled)
1450 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001451}
1452
Ville Syrjälä3c2777f2014-06-26 17:03:06 +03001453static void cherryview_update_wm(struct drm_crtc *crtc)
1454{
1455 struct drm_device *dev = crtc->dev;
1456 static const int sr_latency_ns = 12000;
1457 struct drm_i915_private *dev_priv = dev->dev_private;
1458 int planea_wm, planeb_wm, planec_wm;
1459 int cursora_wm, cursorb_wm, cursorc_wm;
1460 int plane_sr, cursor_sr;
1461 int ignore_plane_sr, ignore_cursor_sr;
1462 unsigned int enabled = 0;
1463 bool cxsr_enabled;
1464
1465 vlv_update_drain_latency(crtc);
1466
1467 if (g4x_compute_wm0(dev, PIPE_A,
Chris Wilson5aef6002014-09-03 11:56:07 +01001468 &valleyview_wm_info, pessimal_latency_ns,
1469 &valleyview_cursor_wm_info, pessimal_latency_ns,
Ville Syrjälä3c2777f2014-06-26 17:03:06 +03001470 &planea_wm, &cursora_wm))
1471 enabled |= 1 << PIPE_A;
1472
1473 if (g4x_compute_wm0(dev, PIPE_B,
Chris Wilson5aef6002014-09-03 11:56:07 +01001474 &valleyview_wm_info, pessimal_latency_ns,
1475 &valleyview_cursor_wm_info, pessimal_latency_ns,
Ville Syrjälä3c2777f2014-06-26 17:03:06 +03001476 &planeb_wm, &cursorb_wm))
1477 enabled |= 1 << PIPE_B;
1478
1479 if (g4x_compute_wm0(dev, PIPE_C,
Chris Wilson5aef6002014-09-03 11:56:07 +01001480 &valleyview_wm_info, pessimal_latency_ns,
1481 &valleyview_cursor_wm_info, pessimal_latency_ns,
Ville Syrjälä3c2777f2014-06-26 17:03:06 +03001482 &planec_wm, &cursorc_wm))
1483 enabled |= 1 << PIPE_C;
1484
1485 if (single_plane_enabled(enabled) &&
1486 g4x_compute_srwm(dev, ffs(enabled) - 1,
1487 sr_latency_ns,
1488 &valleyview_wm_info,
1489 &valleyview_cursor_wm_info,
1490 &plane_sr, &ignore_cursor_sr) &&
1491 g4x_compute_srwm(dev, ffs(enabled) - 1,
1492 2*sr_latency_ns,
1493 &valleyview_wm_info,
1494 &valleyview_cursor_wm_info,
1495 &ignore_plane_sr, &cursor_sr)) {
1496 cxsr_enabled = true;
1497 } else {
1498 cxsr_enabled = false;
1499 intel_set_memory_cxsr(dev_priv, false);
1500 plane_sr = cursor_sr = 0;
1501 }
1502
1503 DRM_DEBUG_KMS("Setting FIFO watermarks - A: plane=%d, cursor=%d, "
1504 "B: plane=%d, cursor=%d, C: plane=%d, cursor=%d, "
1505 "SR: plane=%d, cursor=%d\n",
1506 planea_wm, cursora_wm,
1507 planeb_wm, cursorb_wm,
1508 planec_wm, cursorc_wm,
1509 plane_sr, cursor_sr);
1510
1511 I915_WRITE(DSPFW1,
1512 (plane_sr << DSPFW_SR_SHIFT) |
1513 (cursorb_wm << DSPFW_CURSORB_SHIFT) |
1514 (planeb_wm << DSPFW_PLANEB_SHIFT) |
1515 (planea_wm << DSPFW_PLANEA_SHIFT));
1516 I915_WRITE(DSPFW2,
1517 (I915_READ(DSPFW2) & ~DSPFW_CURSORA_MASK) |
1518 (cursora_wm << DSPFW_CURSORA_SHIFT));
1519 I915_WRITE(DSPFW3,
1520 (I915_READ(DSPFW3) & ~DSPFW_CURSOR_SR_MASK) |
1521 (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
1522 I915_WRITE(DSPFW9_CHV,
1523 (I915_READ(DSPFW9_CHV) & ~(DSPFW_PLANEC_MASK |
1524 DSPFW_CURSORC_MASK)) |
1525 (planec_wm << DSPFW_PLANEC_SHIFT) |
1526 (cursorc_wm << DSPFW_CURSORC_SHIFT));
1527
1528 if (cxsr_enabled)
1529 intel_set_memory_cxsr(dev_priv, true);
1530}
1531
Gajanan Bhat01e184c2014-08-07 17:03:30 +05301532static void valleyview_update_sprite_wm(struct drm_plane *plane,
1533 struct drm_crtc *crtc,
1534 uint32_t sprite_width,
1535 uint32_t sprite_height,
1536 int pixel_size,
1537 bool enabled, bool scaled)
1538{
1539 struct drm_device *dev = crtc->dev;
1540 struct drm_i915_private *dev_priv = dev->dev_private;
1541 int pipe = to_intel_plane(plane)->pipe;
1542 int sprite = to_intel_plane(plane)->plane;
1543 int drain_latency;
1544 int plane_prec;
1545 int sprite_dl;
1546 int prec_mult;
1547
1548 sprite_dl = I915_READ(VLV_DDL(pipe)) & ~(DDL_SPRITE_PRECISION_64(sprite) |
1549 (DRAIN_LATENCY_MASK << DDL_SPRITE_SHIFT(sprite)));
1550
1551 if (enabled && vlv_compute_drain_latency(crtc, pixel_size, &prec_mult,
1552 &drain_latency)) {
1553 plane_prec = (prec_mult == DRAIN_LATENCY_PRECISION_64) ?
1554 DDL_SPRITE_PRECISION_64(sprite) :
1555 DDL_SPRITE_PRECISION_32(sprite);
1556 sprite_dl |= plane_prec |
1557 (drain_latency << DDL_SPRITE_SHIFT(sprite));
1558 }
1559
1560 I915_WRITE(VLV_DDL(pipe), sprite_dl);
1561}
1562
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001563static void g4x_update_wm(struct drm_crtc *crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001564{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001565 struct drm_device *dev = crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001566 static const int sr_latency_ns = 12000;
1567 struct drm_i915_private *dev_priv = dev->dev_private;
1568 int planea_wm, planeb_wm, cursora_wm, cursorb_wm;
1569 int plane_sr, cursor_sr;
1570 unsigned int enabled = 0;
Imre Deak98584252014-06-13 14:54:20 +03001571 bool cxsr_enabled;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001572
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001573 if (g4x_compute_wm0(dev, PIPE_A,
Chris Wilson5aef6002014-09-03 11:56:07 +01001574 &g4x_wm_info, pessimal_latency_ns,
1575 &g4x_cursor_wm_info, pessimal_latency_ns,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001576 &planea_wm, &cursora_wm))
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001577 enabled |= 1 << PIPE_A;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001578
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001579 if (g4x_compute_wm0(dev, PIPE_B,
Chris Wilson5aef6002014-09-03 11:56:07 +01001580 &g4x_wm_info, pessimal_latency_ns,
1581 &g4x_cursor_wm_info, pessimal_latency_ns,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001582 &planeb_wm, &cursorb_wm))
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001583 enabled |= 1 << PIPE_B;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001584
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001585 if (single_plane_enabled(enabled) &&
1586 g4x_compute_srwm(dev, ffs(enabled) - 1,
1587 sr_latency_ns,
1588 &g4x_wm_info,
1589 &g4x_cursor_wm_info,
Chris Wilson52bd02d2012-12-07 10:43:24 +00001590 &plane_sr, &cursor_sr)) {
Imre Deak98584252014-06-13 14:54:20 +03001591 cxsr_enabled = true;
Chris Wilson52bd02d2012-12-07 10:43:24 +00001592 } else {
Imre Deak98584252014-06-13 14:54:20 +03001593 cxsr_enabled = false;
Imre Deak5209b1f2014-07-01 12:36:17 +03001594 intel_set_memory_cxsr(dev_priv, false);
Chris Wilson52bd02d2012-12-07 10:43:24 +00001595 plane_sr = cursor_sr = 0;
1596 }
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001597
Ville Syrjäläa5043452014-06-28 02:04:18 +03001598 DRM_DEBUG_KMS("Setting FIFO watermarks - A: plane=%d, cursor=%d, "
1599 "B: plane=%d, cursor=%d, SR: plane=%d, cursor=%d\n",
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001600 planea_wm, cursora_wm,
1601 planeb_wm, cursorb_wm,
1602 plane_sr, cursor_sr);
1603
1604 I915_WRITE(DSPFW1,
1605 (plane_sr << DSPFW_SR_SHIFT) |
1606 (cursorb_wm << DSPFW_CURSORB_SHIFT) |
1607 (planeb_wm << DSPFW_PLANEB_SHIFT) |
Ville Syrjälä0a560672014-06-11 16:51:18 +03001608 (planea_wm << DSPFW_PLANEA_SHIFT));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001609 I915_WRITE(DSPFW2,
Chris Wilson8c919b22012-12-04 16:33:19 +00001610 (I915_READ(DSPFW2) & ~DSPFW_CURSORA_MASK) |
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001611 (cursora_wm << DSPFW_CURSORA_SHIFT));
1612 /* HPLL off in SR has some issues on G4x... disable it */
1613 I915_WRITE(DSPFW3,
Chris Wilson8c919b22012-12-04 16:33:19 +00001614 (I915_READ(DSPFW3) & ~(DSPFW_HPLL_SR_EN | DSPFW_CURSOR_SR_MASK)) |
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001615 (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
Imre Deak98584252014-06-13 14:54:20 +03001616
1617 if (cxsr_enabled)
1618 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001619}
1620
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001621static void i965_update_wm(struct drm_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001622{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001623 struct drm_device *dev = unused_crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001624 struct drm_i915_private *dev_priv = dev->dev_private;
1625 struct drm_crtc *crtc;
1626 int srwm = 1;
1627 int cursor_sr = 16;
Imre Deak98584252014-06-13 14:54:20 +03001628 bool cxsr_enabled;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001629
1630 /* Calc sr entries for one plane configs */
1631 crtc = single_enabled_crtc(dev);
1632 if (crtc) {
1633 /* self-refresh has much higher latency */
1634 static const int sr_latency_ns = 12000;
Ville Syrjälä4fe85902013-09-04 18:25:22 +03001635 const struct drm_display_mode *adjusted_mode =
1636 &to_intel_crtc(crtc)->config.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001637 int clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -08001638 int htotal = adjusted_mode->crtc_htotal;
Ville Syrjälä37327ab2013-09-04 18:25:28 +03001639 int hdisplay = to_intel_crtc(crtc)->config.pipe_src_w;
Matt Roperf4510a22014-04-01 15:22:40 -07001640 int pixel_size = crtc->primary->fb->bits_per_pixel / 8;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001641 unsigned long line_time_us;
1642 int entries;
1643
Ville Syrjälä922044c2014-02-14 14:18:57 +02001644 line_time_us = max(htotal * 1000 / clock, 1);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001645
1646 /* Use ns/us then divide to preserve precision */
1647 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
1648 pixel_size * hdisplay;
1649 entries = DIV_ROUND_UP(entries, I915_FIFO_LINE_SIZE);
1650 srwm = I965_FIFO_SIZE - entries;
1651 if (srwm < 0)
1652 srwm = 1;
1653 srwm &= 0x1ff;
1654 DRM_DEBUG_KMS("self-refresh entries: %d, wm: %d\n",
1655 entries, srwm);
1656
1657 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
Chris Wilson7bb836d2014-03-26 12:38:14 +00001658 pixel_size * to_intel_crtc(crtc)->cursor_width;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001659 entries = DIV_ROUND_UP(entries,
1660 i965_cursor_wm_info.cacheline_size);
1661 cursor_sr = i965_cursor_wm_info.fifo_size -
1662 (entries + i965_cursor_wm_info.guard_size);
1663
1664 if (cursor_sr > i965_cursor_wm_info.max_wm)
1665 cursor_sr = i965_cursor_wm_info.max_wm;
1666
1667 DRM_DEBUG_KMS("self-refresh watermark: display plane %d "
1668 "cursor %d\n", srwm, cursor_sr);
1669
Imre Deak98584252014-06-13 14:54:20 +03001670 cxsr_enabled = true;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001671 } else {
Imre Deak98584252014-06-13 14:54:20 +03001672 cxsr_enabled = false;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001673 /* Turn off self refresh if both pipes are enabled */
Imre Deak5209b1f2014-07-01 12:36:17 +03001674 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001675 }
1676
1677 DRM_DEBUG_KMS("Setting FIFO watermarks - A: 8, B: 8, C: 8, SR %d\n",
1678 srwm);
1679
1680 /* 965 has limitations... */
1681 I915_WRITE(DSPFW1, (srwm << DSPFW_SR_SHIFT) |
Ville Syrjälä0a560672014-06-11 16:51:18 +03001682 (8 << DSPFW_CURSORB_SHIFT) |
1683 (8 << DSPFW_PLANEB_SHIFT) |
1684 (8 << DSPFW_PLANEA_SHIFT));
1685 I915_WRITE(DSPFW2, (8 << DSPFW_CURSORA_SHIFT) |
1686 (8 << DSPFW_PLANEC_SHIFT_OLD));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001687 /* update cursor SR watermark */
1688 I915_WRITE(DSPFW3, (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
Imre Deak98584252014-06-13 14:54:20 +03001689
1690 if (cxsr_enabled)
1691 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001692}
1693
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001694static void i9xx_update_wm(struct drm_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001695{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001696 struct drm_device *dev = unused_crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001697 struct drm_i915_private *dev_priv = dev->dev_private;
1698 const struct intel_watermark_params *wm_info;
1699 uint32_t fwater_lo;
1700 uint32_t fwater_hi;
1701 int cwm, srwm = 1;
1702 int fifo_size;
1703 int planea_wm, planeb_wm;
1704 struct drm_crtc *crtc, *enabled = NULL;
1705
1706 if (IS_I945GM(dev))
1707 wm_info = &i945_wm_info;
1708 else if (!IS_GEN2(dev))
1709 wm_info = &i915_wm_info;
1710 else
Ville Syrjälä9d539102014-08-15 01:21:53 +03001711 wm_info = &i830_a_wm_info;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001712
1713 fifo_size = dev_priv->display.get_fifo_size(dev, 0);
1714 crtc = intel_get_crtc_for_plane(dev, 0);
Chris Wilson3490ea52013-01-07 10:11:40 +00001715 if (intel_crtc_active(crtc)) {
Damien Lespiau241bfc32013-09-25 16:45:37 +01001716 const struct drm_display_mode *adjusted_mode;
Matt Roperf4510a22014-04-01 15:22:40 -07001717 int cpp = crtc->primary->fb->bits_per_pixel / 8;
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001718 if (IS_GEN2(dev))
1719 cpp = 4;
1720
Damien Lespiau241bfc32013-09-25 16:45:37 +01001721 adjusted_mode = &to_intel_crtc(crtc)->config.adjusted_mode;
1722 planea_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001723 wm_info, fifo_size, cpp,
Chris Wilson5aef6002014-09-03 11:56:07 +01001724 pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001725 enabled = crtc;
Ville Syrjälä9d539102014-08-15 01:21:53 +03001726 } else {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001727 planea_wm = fifo_size - wm_info->guard_size;
Ville Syrjälä9d539102014-08-15 01:21:53 +03001728 if (planea_wm > (long)wm_info->max_wm)
1729 planea_wm = wm_info->max_wm;
1730 }
1731
1732 if (IS_GEN2(dev))
1733 wm_info = &i830_bc_wm_info;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001734
1735 fifo_size = dev_priv->display.get_fifo_size(dev, 1);
1736 crtc = intel_get_crtc_for_plane(dev, 1);
Chris Wilson3490ea52013-01-07 10:11:40 +00001737 if (intel_crtc_active(crtc)) {
Damien Lespiau241bfc32013-09-25 16:45:37 +01001738 const struct drm_display_mode *adjusted_mode;
Matt Roperf4510a22014-04-01 15:22:40 -07001739 int cpp = crtc->primary->fb->bits_per_pixel / 8;
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001740 if (IS_GEN2(dev))
1741 cpp = 4;
1742
Damien Lespiau241bfc32013-09-25 16:45:37 +01001743 adjusted_mode = &to_intel_crtc(crtc)->config.adjusted_mode;
1744 planeb_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001745 wm_info, fifo_size, cpp,
Chris Wilson5aef6002014-09-03 11:56:07 +01001746 pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001747 if (enabled == NULL)
1748 enabled = crtc;
1749 else
1750 enabled = NULL;
Ville Syrjälä9d539102014-08-15 01:21:53 +03001751 } else {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001752 planeb_wm = fifo_size - wm_info->guard_size;
Ville Syrjälä9d539102014-08-15 01:21:53 +03001753 if (planeb_wm > (long)wm_info->max_wm)
1754 planeb_wm = wm_info->max_wm;
1755 }
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001756
1757 DRM_DEBUG_KMS("FIFO watermarks - A: %d, B: %d\n", planea_wm, planeb_wm);
1758
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02001759 if (IS_I915GM(dev) && enabled) {
Matt Roper2ff8fde2014-07-08 07:50:07 -07001760 struct drm_i915_gem_object *obj;
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02001761
Matt Roper2ff8fde2014-07-08 07:50:07 -07001762 obj = intel_fb_obj(enabled->primary->fb);
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02001763
1764 /* self-refresh seems busted with untiled */
Matt Roper2ff8fde2014-07-08 07:50:07 -07001765 if (obj->tiling_mode == I915_TILING_NONE)
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02001766 enabled = NULL;
1767 }
1768
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001769 /*
1770 * Overlay gets an aggressive default since video jitter is bad.
1771 */
1772 cwm = 2;
1773
1774 /* Play safe and disable self-refresh before adjusting watermarks. */
Imre Deak5209b1f2014-07-01 12:36:17 +03001775 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001776
1777 /* Calc sr entries for one plane configs */
1778 if (HAS_FW_BLC(dev) && enabled) {
1779 /* self-refresh has much higher latency */
1780 static const int sr_latency_ns = 6000;
Ville Syrjälä4fe85902013-09-04 18:25:22 +03001781 const struct drm_display_mode *adjusted_mode =
1782 &to_intel_crtc(enabled)->config.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001783 int clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -08001784 int htotal = adjusted_mode->crtc_htotal;
Daniel Vetterf727b492013-11-20 15:02:10 +01001785 int hdisplay = to_intel_crtc(enabled)->config.pipe_src_w;
Matt Roperf4510a22014-04-01 15:22:40 -07001786 int pixel_size = enabled->primary->fb->bits_per_pixel / 8;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001787 unsigned long line_time_us;
1788 int entries;
1789
Ville Syrjälä922044c2014-02-14 14:18:57 +02001790 line_time_us = max(htotal * 1000 / clock, 1);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001791
1792 /* Use ns/us then divide to preserve precision */
1793 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
1794 pixel_size * hdisplay;
1795 entries = DIV_ROUND_UP(entries, wm_info->cacheline_size);
1796 DRM_DEBUG_KMS("self-refresh entries: %d\n", entries);
1797 srwm = wm_info->fifo_size - entries;
1798 if (srwm < 0)
1799 srwm = 1;
1800
1801 if (IS_I945G(dev) || IS_I945GM(dev))
1802 I915_WRITE(FW_BLC_SELF,
1803 FW_BLC_SELF_FIFO_MASK | (srwm & 0xff));
1804 else if (IS_I915GM(dev))
1805 I915_WRITE(FW_BLC_SELF, srwm & 0x3f);
1806 }
1807
1808 DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d, B: %d, C: %d, SR %d\n",
1809 planea_wm, planeb_wm, cwm, srwm);
1810
1811 fwater_lo = ((planeb_wm & 0x3f) << 16) | (planea_wm & 0x3f);
1812 fwater_hi = (cwm & 0x1f);
1813
1814 /* Set request length to 8 cachelines per fetch */
1815 fwater_lo = fwater_lo | (1 << 24) | (1 << 8);
1816 fwater_hi = fwater_hi | (1 << 8);
1817
1818 I915_WRITE(FW_BLC, fwater_lo);
1819 I915_WRITE(FW_BLC2, fwater_hi);
1820
Imre Deak5209b1f2014-07-01 12:36:17 +03001821 if (enabled)
1822 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001823}
1824
Daniel Vetterfeb56b92013-12-14 20:38:30 -02001825static void i845_update_wm(struct drm_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001826{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001827 struct drm_device *dev = unused_crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001828 struct drm_i915_private *dev_priv = dev->dev_private;
1829 struct drm_crtc *crtc;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001830 const struct drm_display_mode *adjusted_mode;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001831 uint32_t fwater_lo;
1832 int planea_wm;
1833
1834 crtc = single_enabled_crtc(dev);
1835 if (crtc == NULL)
1836 return;
1837
Damien Lespiau241bfc32013-09-25 16:45:37 +01001838 adjusted_mode = &to_intel_crtc(crtc)->config.adjusted_mode;
1839 planea_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Daniel Vetterfeb56b92013-12-14 20:38:30 -02001840 &i845_wm_info,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001841 dev_priv->display.get_fifo_size(dev, 0),
Chris Wilson5aef6002014-09-03 11:56:07 +01001842 4, pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001843 fwater_lo = I915_READ(FW_BLC) & ~0xfff;
1844 fwater_lo |= (3<<8) | planea_wm;
1845
1846 DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d\n", planea_wm);
1847
1848 I915_WRITE(FW_BLC, fwater_lo);
1849}
1850
Ville Syrjälä36587292013-07-05 11:57:16 +03001851static uint32_t ilk_pipe_pixel_rate(struct drm_device *dev,
1852 struct drm_crtc *crtc)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001853{
1854 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilsonfd4daa92013-08-27 17:04:17 +01001855 uint32_t pixel_rate;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001856
Damien Lespiau241bfc32013-09-25 16:45:37 +01001857 pixel_rate = intel_crtc->config.adjusted_mode.crtc_clock;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001858
1859 /* We only use IF-ID interlacing. If we ever use PF-ID we'll need to
1860 * adjust the pixel_rate here. */
1861
Chris Wilsonfd4daa92013-08-27 17:04:17 +01001862 if (intel_crtc->config.pch_pfit.enabled) {
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001863 uint64_t pipe_w, pipe_h, pfit_w, pfit_h;
Chris Wilsonfd4daa92013-08-27 17:04:17 +01001864 uint32_t pfit_size = intel_crtc->config.pch_pfit.size;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001865
Ville Syrjälä37327ab2013-09-04 18:25:28 +03001866 pipe_w = intel_crtc->config.pipe_src_w;
1867 pipe_h = intel_crtc->config.pipe_src_h;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001868 pfit_w = (pfit_size >> 16) & 0xFFFF;
1869 pfit_h = pfit_size & 0xFFFF;
1870 if (pipe_w < pfit_w)
1871 pipe_w = pfit_w;
1872 if (pipe_h < pfit_h)
1873 pipe_h = pfit_h;
1874
1875 pixel_rate = div_u64((uint64_t) pixel_rate * pipe_w * pipe_h,
1876 pfit_w * pfit_h);
1877 }
1878
1879 return pixel_rate;
1880}
1881
Ville Syrjälä37126462013-08-01 16:18:55 +03001882/* latency must be in 0.1us units. */
Ville Syrjälä23297042013-07-05 11:57:17 +03001883static uint32_t ilk_wm_method1(uint32_t pixel_rate, uint8_t bytes_per_pixel,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001884 uint32_t latency)
1885{
1886 uint64_t ret;
1887
Ville Syrjälä3312ba62013-08-01 16:18:53 +03001888 if (WARN(latency == 0, "Latency value missing\n"))
1889 return UINT_MAX;
1890
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001891 ret = (uint64_t) pixel_rate * bytes_per_pixel * latency;
1892 ret = DIV_ROUND_UP_ULL(ret, 64 * 10000) + 2;
1893
1894 return ret;
1895}
1896
Ville Syrjälä37126462013-08-01 16:18:55 +03001897/* latency must be in 0.1us units. */
Ville Syrjälä23297042013-07-05 11:57:17 +03001898static uint32_t ilk_wm_method2(uint32_t pixel_rate, uint32_t pipe_htotal,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001899 uint32_t horiz_pixels, uint8_t bytes_per_pixel,
1900 uint32_t latency)
1901{
1902 uint32_t ret;
1903
Ville Syrjälä3312ba62013-08-01 16:18:53 +03001904 if (WARN(latency == 0, "Latency value missing\n"))
1905 return UINT_MAX;
1906
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001907 ret = (latency * pixel_rate) / (pipe_htotal * 10000);
1908 ret = (ret + 1) * horiz_pixels * bytes_per_pixel;
1909 ret = DIV_ROUND_UP(ret, 64) + 2;
1910 return ret;
1911}
1912
Ville Syrjälä23297042013-07-05 11:57:17 +03001913static uint32_t ilk_wm_fbc(uint32_t pri_val, uint32_t horiz_pixels,
Paulo Zanonicca32e92013-05-31 11:45:06 -03001914 uint8_t bytes_per_pixel)
1915{
1916 return DIV_ROUND_UP(pri_val * 64, horiz_pixels * bytes_per_pixel) + 2;
1917}
1918
Imre Deak820c1982013-12-17 14:46:36 +02001919struct ilk_pipe_wm_parameters {
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001920 bool active;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001921 uint32_t pipe_htotal;
1922 uint32_t pixel_rate;
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001923 struct intel_plane_wm_parameters pri;
1924 struct intel_plane_wm_parameters spr;
1925 struct intel_plane_wm_parameters cur;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001926};
1927
Imre Deak820c1982013-12-17 14:46:36 +02001928struct ilk_wm_maximums {
Paulo Zanonicca32e92013-05-31 11:45:06 -03001929 uint16_t pri;
1930 uint16_t spr;
1931 uint16_t cur;
1932 uint16_t fbc;
1933};
1934
Ville Syrjälä240264f2013-08-07 13:29:12 +03001935/* used in computing the new watermarks state */
1936struct intel_wm_config {
1937 unsigned int num_pipes_active;
1938 bool sprites_enabled;
1939 bool sprites_scaled;
Ville Syrjälä240264f2013-08-07 13:29:12 +03001940};
1941
Ville Syrjälä37126462013-08-01 16:18:55 +03001942/*
1943 * For both WM_PIPE and WM_LP.
1944 * mem_value must be in 0.1us units.
1945 */
Imre Deak820c1982013-12-17 14:46:36 +02001946static uint32_t ilk_compute_pri_wm(const struct ilk_pipe_wm_parameters *params,
Paulo Zanonicca32e92013-05-31 11:45:06 -03001947 uint32_t mem_value,
1948 bool is_lp)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001949{
Paulo Zanonicca32e92013-05-31 11:45:06 -03001950 uint32_t method1, method2;
1951
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001952 if (!params->active || !params->pri.enabled)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001953 return 0;
1954
Ville Syrjälä23297042013-07-05 11:57:17 +03001955 method1 = ilk_wm_method1(params->pixel_rate,
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001956 params->pri.bytes_per_pixel,
Paulo Zanonicca32e92013-05-31 11:45:06 -03001957 mem_value);
1958
1959 if (!is_lp)
1960 return method1;
1961
Ville Syrjälä23297042013-07-05 11:57:17 +03001962 method2 = ilk_wm_method2(params->pixel_rate,
Paulo Zanonicca32e92013-05-31 11:45:06 -03001963 params->pipe_htotal,
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001964 params->pri.horiz_pixels,
1965 params->pri.bytes_per_pixel,
Paulo Zanonicca32e92013-05-31 11:45:06 -03001966 mem_value);
1967
1968 return min(method1, method2);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001969}
1970
Ville Syrjälä37126462013-08-01 16:18:55 +03001971/*
1972 * For both WM_PIPE and WM_LP.
1973 * mem_value must be in 0.1us units.
1974 */
Imre Deak820c1982013-12-17 14:46:36 +02001975static uint32_t ilk_compute_spr_wm(const struct ilk_pipe_wm_parameters *params,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001976 uint32_t mem_value)
1977{
1978 uint32_t method1, method2;
1979
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001980 if (!params->active || !params->spr.enabled)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001981 return 0;
1982
Ville Syrjälä23297042013-07-05 11:57:17 +03001983 method1 = ilk_wm_method1(params->pixel_rate,
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001984 params->spr.bytes_per_pixel,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001985 mem_value);
Ville Syrjälä23297042013-07-05 11:57:17 +03001986 method2 = ilk_wm_method2(params->pixel_rate,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001987 params->pipe_htotal,
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001988 params->spr.horiz_pixels,
1989 params->spr.bytes_per_pixel,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001990 mem_value);
1991 return min(method1, method2);
1992}
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_cur_wm(const struct ilk_pipe_wm_parameters *params,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001999 uint32_t mem_value)
2000{
Ville Syrjäläc35426d2013-08-07 13:29:50 +03002001 if (!params->active || !params->cur.enabled)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002002 return 0;
2003
Ville Syrjälä23297042013-07-05 11:57:17 +03002004 return ilk_wm_method2(params->pixel_rate,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002005 params->pipe_htotal,
Ville Syrjäläc35426d2013-08-07 13:29:50 +03002006 params->cur.horiz_pixels,
2007 params->cur.bytes_per_pixel,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002008 mem_value);
2009}
2010
Paulo Zanonicca32e92013-05-31 11:45:06 -03002011/* Only for WM_LP. */
Imre Deak820c1982013-12-17 14:46:36 +02002012static uint32_t ilk_compute_fbc_wm(const struct ilk_pipe_wm_parameters *params,
Ville Syrjälä1fda9882013-07-05 11:57:19 +03002013 uint32_t pri_val)
Paulo Zanonicca32e92013-05-31 11:45:06 -03002014{
Ville Syrjäläc35426d2013-08-07 13:29:50 +03002015 if (!params->active || !params->pri.enabled)
Paulo Zanonicca32e92013-05-31 11:45:06 -03002016 return 0;
2017
Ville Syrjälä23297042013-07-05 11:57:17 +03002018 return ilk_wm_fbc(pri_val,
Ville Syrjäläc35426d2013-08-07 13:29:50 +03002019 params->pri.horiz_pixels,
2020 params->pri.bytes_per_pixel);
Paulo Zanonicca32e92013-05-31 11:45:06 -03002021}
2022
Ville Syrjälä158ae642013-08-07 13:28:19 +03002023static unsigned int ilk_display_fifo_size(const struct drm_device *dev)
2024{
Ville Syrjälä416f4722013-11-02 21:07:46 -07002025 if (INTEL_INFO(dev)->gen >= 8)
2026 return 3072;
2027 else if (INTEL_INFO(dev)->gen >= 7)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002028 return 768;
2029 else
2030 return 512;
2031}
2032
Ville Syrjälä4e975082014-03-07 18:32:11 +02002033static unsigned int ilk_plane_wm_reg_max(const struct drm_device *dev,
2034 int level, bool is_sprite)
2035{
2036 if (INTEL_INFO(dev)->gen >= 8)
2037 /* BDW primary/sprite plane watermarks */
2038 return level == 0 ? 255 : 2047;
2039 else if (INTEL_INFO(dev)->gen >= 7)
2040 /* IVB/HSW primary/sprite plane watermarks */
2041 return level == 0 ? 127 : 1023;
2042 else if (!is_sprite)
2043 /* ILK/SNB primary plane watermarks */
2044 return level == 0 ? 127 : 511;
2045 else
2046 /* ILK/SNB sprite plane watermarks */
2047 return level == 0 ? 63 : 255;
2048}
2049
2050static unsigned int ilk_cursor_wm_reg_max(const struct drm_device *dev,
2051 int level)
2052{
2053 if (INTEL_INFO(dev)->gen >= 7)
2054 return level == 0 ? 63 : 255;
2055 else
2056 return level == 0 ? 31 : 63;
2057}
2058
2059static unsigned int ilk_fbc_wm_reg_max(const struct drm_device *dev)
2060{
2061 if (INTEL_INFO(dev)->gen >= 8)
2062 return 31;
2063 else
2064 return 15;
2065}
2066
Ville Syrjälä158ae642013-08-07 13:28:19 +03002067/* Calculate the maximum primary/sprite plane watermark */
2068static unsigned int ilk_plane_wm_max(const struct drm_device *dev,
2069 int level,
Ville Syrjälä240264f2013-08-07 13:29:12 +03002070 const struct intel_wm_config *config,
Ville Syrjälä158ae642013-08-07 13:28:19 +03002071 enum intel_ddb_partitioning ddb_partitioning,
2072 bool is_sprite)
2073{
2074 unsigned int fifo_size = ilk_display_fifo_size(dev);
Ville Syrjälä158ae642013-08-07 13:28:19 +03002075
2076 /* if sprites aren't enabled, sprites get nothing */
Ville Syrjälä240264f2013-08-07 13:29:12 +03002077 if (is_sprite && !config->sprites_enabled)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002078 return 0;
2079
2080 /* HSW allows LP1+ watermarks even with multiple pipes */
Ville Syrjälä240264f2013-08-07 13:29:12 +03002081 if (level == 0 || config->num_pipes_active > 1) {
Ville Syrjälä158ae642013-08-07 13:28:19 +03002082 fifo_size /= INTEL_INFO(dev)->num_pipes;
2083
2084 /*
2085 * For some reason the non self refresh
2086 * FIFO size is only half of the self
2087 * refresh FIFO size on ILK/SNB.
2088 */
2089 if (INTEL_INFO(dev)->gen <= 6)
2090 fifo_size /= 2;
2091 }
2092
Ville Syrjälä240264f2013-08-07 13:29:12 +03002093 if (config->sprites_enabled) {
Ville Syrjälä158ae642013-08-07 13:28:19 +03002094 /* level 0 is always calculated with 1:1 split */
2095 if (level > 0 && ddb_partitioning == INTEL_DDB_PART_5_6) {
2096 if (is_sprite)
2097 fifo_size *= 5;
2098 fifo_size /= 6;
2099 } else {
2100 fifo_size /= 2;
2101 }
2102 }
2103
2104 /* clamp to max that the registers can hold */
Ville Syrjälä4e975082014-03-07 18:32:11 +02002105 return min(fifo_size, ilk_plane_wm_reg_max(dev, level, is_sprite));
Ville Syrjälä158ae642013-08-07 13:28:19 +03002106}
2107
2108/* Calculate the maximum cursor plane watermark */
2109static unsigned int ilk_cursor_wm_max(const struct drm_device *dev,
Ville Syrjälä240264f2013-08-07 13:29:12 +03002110 int level,
2111 const struct intel_wm_config *config)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002112{
2113 /* HSW LP1+ watermarks w/ multiple pipes */
Ville Syrjälä240264f2013-08-07 13:29:12 +03002114 if (level > 0 && config->num_pipes_active > 1)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002115 return 64;
2116
2117 /* otherwise just report max that registers can hold */
Ville Syrjälä4e975082014-03-07 18:32:11 +02002118 return ilk_cursor_wm_reg_max(dev, level);
Ville Syrjälä158ae642013-08-07 13:28:19 +03002119}
2120
Damien Lespiaud34ff9c2014-01-06 19:17:23 +00002121static void ilk_compute_wm_maximums(const struct drm_device *dev,
Ville Syrjälä34982fe2013-10-09 19:18:09 +03002122 int level,
2123 const struct intel_wm_config *config,
2124 enum intel_ddb_partitioning ddb_partitioning,
Imre Deak820c1982013-12-17 14:46:36 +02002125 struct ilk_wm_maximums *max)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002126{
Ville Syrjälä240264f2013-08-07 13:29:12 +03002127 max->pri = ilk_plane_wm_max(dev, level, config, ddb_partitioning, false);
2128 max->spr = ilk_plane_wm_max(dev, level, config, ddb_partitioning, true);
2129 max->cur = ilk_cursor_wm_max(dev, level, config);
Ville Syrjälä4e975082014-03-07 18:32:11 +02002130 max->fbc = ilk_fbc_wm_reg_max(dev);
Ville Syrjälä158ae642013-08-07 13:28:19 +03002131}
2132
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002133static void ilk_compute_wm_reg_maximums(struct drm_device *dev,
2134 int level,
2135 struct ilk_wm_maximums *max)
2136{
2137 max->pri = ilk_plane_wm_reg_max(dev, level, false);
2138 max->spr = ilk_plane_wm_reg_max(dev, level, true);
2139 max->cur = ilk_cursor_wm_reg_max(dev, level);
2140 max->fbc = ilk_fbc_wm_reg_max(dev);
2141}
2142
Ville Syrjäläd9395652013-10-09 19:18:10 +03002143static bool ilk_validate_wm_level(int level,
Imre Deak820c1982013-12-17 14:46:36 +02002144 const struct ilk_wm_maximums *max,
Ville Syrjäläd9395652013-10-09 19:18:10 +03002145 struct intel_wm_level *result)
Ville Syrjäläa9786a12013-08-07 13:24:47 +03002146{
2147 bool ret;
2148
2149 /* already determined to be invalid? */
2150 if (!result->enable)
2151 return false;
2152
2153 result->enable = result->pri_val <= max->pri &&
2154 result->spr_val <= max->spr &&
2155 result->cur_val <= max->cur;
2156
2157 ret = result->enable;
2158
2159 /*
2160 * HACK until we can pre-compute everything,
2161 * and thus fail gracefully if LP0 watermarks
2162 * are exceeded...
2163 */
2164 if (level == 0 && !result->enable) {
2165 if (result->pri_val > max->pri)
2166 DRM_DEBUG_KMS("Primary WM%d too large %u (max %u)\n",
2167 level, result->pri_val, max->pri);
2168 if (result->spr_val > max->spr)
2169 DRM_DEBUG_KMS("Sprite WM%d too large %u (max %u)\n",
2170 level, result->spr_val, max->spr);
2171 if (result->cur_val > max->cur)
2172 DRM_DEBUG_KMS("Cursor WM%d too large %u (max %u)\n",
2173 level, result->cur_val, max->cur);
2174
2175 result->pri_val = min_t(uint32_t, result->pri_val, max->pri);
2176 result->spr_val = min_t(uint32_t, result->spr_val, max->spr);
2177 result->cur_val = min_t(uint32_t, result->cur_val, max->cur);
2178 result->enable = true;
2179 }
2180
Ville Syrjäläa9786a12013-08-07 13:24:47 +03002181 return ret;
2182}
2183
Damien Lespiaud34ff9c2014-01-06 19:17:23 +00002184static void ilk_compute_wm_level(const struct drm_i915_private *dev_priv,
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03002185 int level,
Imre Deak820c1982013-12-17 14:46:36 +02002186 const struct ilk_pipe_wm_parameters *p,
Ville Syrjälä1fd527c2013-08-06 22:24:05 +03002187 struct intel_wm_level *result)
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03002188{
2189 uint16_t pri_latency = dev_priv->wm.pri_latency[level];
2190 uint16_t spr_latency = dev_priv->wm.spr_latency[level];
2191 uint16_t cur_latency = dev_priv->wm.cur_latency[level];
2192
2193 /* WM1+ latency values stored in 0.5us units */
2194 if (level > 0) {
2195 pri_latency *= 5;
2196 spr_latency *= 5;
2197 cur_latency *= 5;
2198 }
2199
2200 result->pri_val = ilk_compute_pri_wm(p, pri_latency, level);
2201 result->spr_val = ilk_compute_spr_wm(p, spr_latency);
2202 result->cur_val = ilk_compute_cur_wm(p, cur_latency);
2203 result->fbc_val = ilk_compute_fbc_wm(p, result->pri_val);
2204 result->enable = true;
2205}
2206
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002207static uint32_t
2208hsw_compute_linetime_wm(struct drm_device *dev, struct drm_crtc *crtc)
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002209{
2210 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002211 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002212 struct drm_display_mode *mode = &intel_crtc->config.adjusted_mode;
Paulo Zanoni85a02de2013-05-03 17:23:43 -03002213 u32 linetime, ips_linetime;
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002214
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002215 if (!intel_crtc_active(crtc))
2216 return 0;
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002217
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002218 /* The WM are computed with base on how long it takes to fill a single
2219 * row at the given clock rate, multiplied by 8.
2220 * */
Jesse Barnesfec8cba2013-11-27 11:10:26 -08002221 linetime = DIV_ROUND_CLOSEST(mode->crtc_htotal * 1000 * 8,
2222 mode->crtc_clock);
2223 ips_linetime = DIV_ROUND_CLOSEST(mode->crtc_htotal * 1000 * 8,
Paulo Zanoni85a02de2013-05-03 17:23:43 -03002224 intel_ddi_get_cdclk_freq(dev_priv));
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002225
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002226 return PIPE_WM_LINETIME_IPS_LINETIME(ips_linetime) |
2227 PIPE_WM_LINETIME_TIME(linetime);
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002228}
2229
Ville Syrjälä12b134d2013-07-05 11:57:21 +03002230static void intel_read_wm_latency(struct drm_device *dev, uint16_t wm[5])
2231{
2232 struct drm_i915_private *dev_priv = dev->dev_private;
2233
Ville Syrjäläa42a5712014-01-07 16:14:08 +02002234 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Ville Syrjälä12b134d2013-07-05 11:57:21 +03002235 uint64_t sskpd = I915_READ64(MCH_SSKPD);
2236
2237 wm[0] = (sskpd >> 56) & 0xFF;
2238 if (wm[0] == 0)
2239 wm[0] = sskpd & 0xF;
Ville Syrjäläe5d50192013-07-05 11:57:22 +03002240 wm[1] = (sskpd >> 4) & 0xFF;
2241 wm[2] = (sskpd >> 12) & 0xFF;
2242 wm[3] = (sskpd >> 20) & 0x1FF;
2243 wm[4] = (sskpd >> 32) & 0x1FF;
Ville Syrjälä63cf9a12013-07-05 11:57:23 +03002244 } else if (INTEL_INFO(dev)->gen >= 6) {
2245 uint32_t sskpd = I915_READ(MCH_SSKPD);
2246
2247 wm[0] = (sskpd >> SSKPD_WM0_SHIFT) & SSKPD_WM_MASK;
2248 wm[1] = (sskpd >> SSKPD_WM1_SHIFT) & SSKPD_WM_MASK;
2249 wm[2] = (sskpd >> SSKPD_WM2_SHIFT) & SSKPD_WM_MASK;
2250 wm[3] = (sskpd >> SSKPD_WM3_SHIFT) & SSKPD_WM_MASK;
Ville Syrjälä3a88d0a2013-08-01 16:18:49 +03002251 } else if (INTEL_INFO(dev)->gen >= 5) {
2252 uint32_t mltr = I915_READ(MLTR_ILK);
2253
2254 /* ILK primary LP0 latency is 700 ns */
2255 wm[0] = 7;
2256 wm[1] = (mltr >> MLTR_WM1_SHIFT) & ILK_SRLT_MASK;
2257 wm[2] = (mltr >> MLTR_WM2_SHIFT) & ILK_SRLT_MASK;
Ville Syrjälä12b134d2013-07-05 11:57:21 +03002258 }
2259}
2260
Ville Syrjälä53615a52013-08-01 16:18:50 +03002261static void intel_fixup_spr_wm_latency(struct drm_device *dev, uint16_t wm[5])
2262{
2263 /* ILK sprite LP0 latency is 1300 ns */
2264 if (INTEL_INFO(dev)->gen == 5)
2265 wm[0] = 13;
2266}
2267
2268static void intel_fixup_cur_wm_latency(struct drm_device *dev, uint16_t wm[5])
2269{
2270 /* ILK cursor LP0 latency is 1300 ns */
2271 if (INTEL_INFO(dev)->gen == 5)
2272 wm[0] = 13;
2273
2274 /* WaDoubleCursorLP3Latency:ivb */
2275 if (IS_IVYBRIDGE(dev))
2276 wm[3] *= 2;
2277}
2278
Damien Lespiau546c81f2014-05-13 15:30:26 +01002279int ilk_wm_max_level(const struct drm_device *dev)
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03002280{
2281 /* how many WM levels are we expecting */
Ville Syrjäläa42a5712014-01-07 16:14:08 +02002282 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03002283 return 4;
2284 else if (INTEL_INFO(dev)->gen >= 6)
2285 return 3;
2286 else
2287 return 2;
2288}
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002289static void intel_print_wm_latency(struct drm_device *dev,
2290 const char *name,
2291 const uint16_t wm[5])
2292{
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03002293 int level, max_level = ilk_wm_max_level(dev);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002294
2295 for (level = 0; level <= max_level; level++) {
2296 unsigned int latency = wm[level];
2297
2298 if (latency == 0) {
2299 DRM_ERROR("%s WM%d latency not provided\n",
2300 name, level);
2301 continue;
2302 }
2303
2304 /* WM1+ latency values in 0.5us units */
2305 if (level > 0)
2306 latency *= 5;
2307
2308 DRM_DEBUG_KMS("%s WM%d latency %u (%u.%u usec)\n",
2309 name, level, wm[level],
2310 latency / 10, latency % 10);
2311 }
2312}
2313
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002314static bool ilk_increase_wm_latency(struct drm_i915_private *dev_priv,
2315 uint16_t wm[5], uint16_t min)
2316{
2317 int level, max_level = ilk_wm_max_level(dev_priv->dev);
2318
2319 if (wm[0] >= min)
2320 return false;
2321
2322 wm[0] = max(wm[0], min);
2323 for (level = 1; level <= max_level; level++)
2324 wm[level] = max_t(uint16_t, wm[level], DIV_ROUND_UP(min, 5));
2325
2326 return true;
2327}
2328
2329static void snb_wm_latency_quirk(struct drm_device *dev)
2330{
2331 struct drm_i915_private *dev_priv = dev->dev_private;
2332 bool changed;
2333
2334 /*
2335 * The BIOS provided WM memory latency values are often
2336 * inadequate for high resolution displays. Adjust them.
2337 */
2338 changed = ilk_increase_wm_latency(dev_priv, dev_priv->wm.pri_latency, 12) |
2339 ilk_increase_wm_latency(dev_priv, dev_priv->wm.spr_latency, 12) |
2340 ilk_increase_wm_latency(dev_priv, dev_priv->wm.cur_latency, 12);
2341
2342 if (!changed)
2343 return;
2344
2345 DRM_DEBUG_KMS("WM latency values increased to avoid potential underruns\n");
2346 intel_print_wm_latency(dev, "Primary", dev_priv->wm.pri_latency);
2347 intel_print_wm_latency(dev, "Sprite", dev_priv->wm.spr_latency);
2348 intel_print_wm_latency(dev, "Cursor", dev_priv->wm.cur_latency);
2349}
2350
Damien Lespiaufa50ad62014-03-17 18:01:16 +00002351static void ilk_setup_wm_latency(struct drm_device *dev)
Ville Syrjälä53615a52013-08-01 16:18:50 +03002352{
2353 struct drm_i915_private *dev_priv = dev->dev_private;
2354
2355 intel_read_wm_latency(dev, dev_priv->wm.pri_latency);
2356
2357 memcpy(dev_priv->wm.spr_latency, dev_priv->wm.pri_latency,
2358 sizeof(dev_priv->wm.pri_latency));
2359 memcpy(dev_priv->wm.cur_latency, dev_priv->wm.pri_latency,
2360 sizeof(dev_priv->wm.pri_latency));
2361
2362 intel_fixup_spr_wm_latency(dev, dev_priv->wm.spr_latency);
2363 intel_fixup_cur_wm_latency(dev, dev_priv->wm.cur_latency);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002364
2365 intel_print_wm_latency(dev, "Primary", dev_priv->wm.pri_latency);
2366 intel_print_wm_latency(dev, "Sprite", dev_priv->wm.spr_latency);
2367 intel_print_wm_latency(dev, "Cursor", dev_priv->wm.cur_latency);
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002368
2369 if (IS_GEN6(dev))
2370 snb_wm_latency_quirk(dev);
Ville Syrjälä53615a52013-08-01 16:18:50 +03002371}
2372
Imre Deak820c1982013-12-17 14:46:36 +02002373static void ilk_compute_wm_parameters(struct drm_crtc *crtc,
Ville Syrjälä2a44b762014-03-07 18:32:09 +02002374 struct ilk_pipe_wm_parameters *p)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002375{
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03002376 struct drm_device *dev = crtc->dev;
2377 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2378 enum pipe pipe = intel_crtc->pipe;
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03002379 struct drm_plane *plane;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002380
Ville Syrjälä2a44b762014-03-07 18:32:09 +02002381 if (!intel_crtc_active(crtc))
2382 return;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002383
Ville Syrjälä2a44b762014-03-07 18:32:09 +02002384 p->active = true;
2385 p->pipe_htotal = intel_crtc->config.adjusted_mode.crtc_htotal;
2386 p->pixel_rate = ilk_pipe_pixel_rate(dev, crtc);
2387 p->pri.bytes_per_pixel = crtc->primary->fb->bits_per_pixel / 8;
2388 p->cur.bytes_per_pixel = 4;
2389 p->pri.horiz_pixels = intel_crtc->config.pipe_src_w;
2390 p->cur.horiz_pixels = intel_crtc->cursor_width;
2391 /* TODO: for now, assume primary and cursor planes are always enabled. */
2392 p->pri.enabled = true;
2393 p->cur.enabled = true;
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03002394
Matt Roperaf2b6532014-04-01 15:22:32 -07002395 drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002396 struct intel_plane *intel_plane = to_intel_plane(plane);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002397
Ville Syrjälä2a44b762014-03-07 18:32:09 +02002398 if (intel_plane->pipe == pipe) {
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03002399 p->spr = intel_plane->wm;
Ville Syrjälä2a44b762014-03-07 18:32:09 +02002400 break;
2401 }
2402 }
2403}
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002404
Ville Syrjälä2a44b762014-03-07 18:32:09 +02002405static void ilk_compute_wm_config(struct drm_device *dev,
2406 struct intel_wm_config *config)
2407{
2408 struct intel_crtc *intel_crtc;
2409
2410 /* Compute the currently _active_ config */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01002411 for_each_intel_crtc(dev, intel_crtc) {
Ville Syrjälä2a44b762014-03-07 18:32:09 +02002412 const struct intel_pipe_wm *wm = &intel_crtc->wm.active;
2413
2414 if (!wm->pipe_enabled)
2415 continue;
2416
2417 config->sprites_enabled |= wm->sprites_enabled;
2418 config->sprites_scaled |= wm->sprites_scaled;
2419 config->num_pipes_active++;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002420 }
2421}
2422
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002423/* Compute new watermarks for the pipe */
2424static bool intel_compute_pipe_wm(struct drm_crtc *crtc,
Imre Deak820c1982013-12-17 14:46:36 +02002425 const struct ilk_pipe_wm_parameters *params,
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002426 struct intel_pipe_wm *pipe_wm)
2427{
2428 struct drm_device *dev = crtc->dev;
Damien Lespiaud34ff9c2014-01-06 19:17:23 +00002429 const struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002430 int level, max_level = ilk_wm_max_level(dev);
2431 /* LP0 watermark maximums depend on this pipe alone */
2432 struct intel_wm_config config = {
2433 .num_pipes_active = 1,
2434 .sprites_enabled = params->spr.enabled,
2435 .sprites_scaled = params->spr.scaled,
2436 };
Imre Deak820c1982013-12-17 14:46:36 +02002437 struct ilk_wm_maximums max;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002438
Ville Syrjälä2a44b762014-03-07 18:32:09 +02002439 pipe_wm->pipe_enabled = params->active;
2440 pipe_wm->sprites_enabled = params->spr.enabled;
2441 pipe_wm->sprites_scaled = params->spr.scaled;
2442
Ville Syrjälä7b39a0b2013-12-05 15:51:30 +02002443 /* ILK/SNB: LP2+ watermarks only w/o sprites */
2444 if (INTEL_INFO(dev)->gen <= 6 && params->spr.enabled)
2445 max_level = 1;
2446
2447 /* ILK/SNB/IVB: LP1+ watermarks only w/o scaling */
2448 if (params->spr.scaled)
2449 max_level = 0;
2450
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002451 ilk_compute_wm_level(dev_priv, 0, params, &pipe_wm->wm[0]);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002452
Ville Syrjäläa42a5712014-01-07 16:14:08 +02002453 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Ville Syrjäläce0e0712013-12-05 15:51:36 +02002454 pipe_wm->linetime = hsw_compute_linetime_wm(dev, crtc);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002455
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002456 /* LP0 watermarks always use 1/2 DDB partitioning */
2457 ilk_compute_wm_maximums(dev, 0, &config, INTEL_DDB_PART_1_2, &max);
2458
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002459 /* At least LP0 must be valid */
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002460 if (!ilk_validate_wm_level(0, &max, &pipe_wm->wm[0]))
2461 return false;
2462
2463 ilk_compute_wm_reg_maximums(dev, 1, &max);
2464
2465 for (level = 1; level <= max_level; level++) {
2466 struct intel_wm_level wm = {};
2467
2468 ilk_compute_wm_level(dev_priv, level, params, &wm);
2469
2470 /*
2471 * Disable any watermark level that exceeds the
2472 * register maximums since such watermarks are
2473 * always invalid.
2474 */
2475 if (!ilk_validate_wm_level(level, &max, &wm))
2476 break;
2477
2478 pipe_wm->wm[level] = wm;
2479 }
2480
2481 return true;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002482}
2483
2484/*
2485 * Merge the watermarks from all active pipes for a specific level.
2486 */
2487static void ilk_merge_wm_level(struct drm_device *dev,
2488 int level,
2489 struct intel_wm_level *ret_wm)
2490{
2491 const struct intel_crtc *intel_crtc;
2492
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002493 ret_wm->enable = true;
2494
Damien Lespiaud3fcc802014-05-13 23:32:22 +01002495 for_each_intel_crtc(dev, intel_crtc) {
Ville Syrjäläfe392ef2014-03-07 18:32:10 +02002496 const struct intel_pipe_wm *active = &intel_crtc->wm.active;
2497 const struct intel_wm_level *wm = &active->wm[level];
2498
2499 if (!active->pipe_enabled)
2500 continue;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002501
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002502 /*
2503 * The watermark values may have been used in the past,
2504 * so we must maintain them in the registers for some
2505 * time even if the level is now disabled.
2506 */
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002507 if (!wm->enable)
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002508 ret_wm->enable = false;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002509
2510 ret_wm->pri_val = max(ret_wm->pri_val, wm->pri_val);
2511 ret_wm->spr_val = max(ret_wm->spr_val, wm->spr_val);
2512 ret_wm->cur_val = max(ret_wm->cur_val, wm->cur_val);
2513 ret_wm->fbc_val = max(ret_wm->fbc_val, wm->fbc_val);
2514 }
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002515}
2516
2517/*
2518 * Merge all low power watermarks for all active pipes.
2519 */
2520static void ilk_wm_merge(struct drm_device *dev,
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02002521 const struct intel_wm_config *config,
Imre Deak820c1982013-12-17 14:46:36 +02002522 const struct ilk_wm_maximums *max,
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002523 struct intel_pipe_wm *merged)
2524{
2525 int level, max_level = ilk_wm_max_level(dev);
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002526 int last_enabled_level = max_level;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002527
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02002528 /* ILK/SNB/IVB: LP1+ watermarks only w/ single pipe */
2529 if ((INTEL_INFO(dev)->gen <= 6 || IS_IVYBRIDGE(dev)) &&
2530 config->num_pipes_active > 1)
2531 return;
2532
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02002533 /* ILK: FBC WM must be disabled always */
2534 merged->fbc_wm_enabled = INTEL_INFO(dev)->gen >= 6;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002535
2536 /* merge each WM1+ level */
2537 for (level = 1; level <= max_level; level++) {
2538 struct intel_wm_level *wm = &merged->wm[level];
2539
2540 ilk_merge_wm_level(dev, level, wm);
2541
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002542 if (level > last_enabled_level)
2543 wm->enable = false;
2544 else if (!ilk_validate_wm_level(level, max, wm))
2545 /* make sure all following levels get disabled */
2546 last_enabled_level = level - 1;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002547
2548 /*
2549 * The spec says it is preferred to disable
2550 * FBC WMs instead of disabling a WM level.
2551 */
2552 if (wm->fbc_val > max->fbc) {
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002553 if (wm->enable)
2554 merged->fbc_wm_enabled = false;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002555 wm->fbc_val = 0;
2556 }
2557 }
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02002558
2559 /* ILK: LP2+ must be disabled when FBC WM is disabled but FBC enabled */
2560 /*
2561 * FIXME this is racy. FBC might get enabled later.
2562 * What we should check here is whether FBC can be
2563 * enabled sometime later.
2564 */
2565 if (IS_GEN5(dev) && !merged->fbc_wm_enabled && intel_fbc_enabled(dev)) {
2566 for (level = 2; level <= max_level; level++) {
2567 struct intel_wm_level *wm = &merged->wm[level];
2568
2569 wm->enable = false;
2570 }
2571 }
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002572}
2573
Ville Syrjäläb380ca32013-10-09 19:18:01 +03002574static int ilk_wm_lp_to_level(int wm_lp, const struct intel_pipe_wm *pipe_wm)
2575{
2576 /* LP1,LP2,LP3 levels are either 1,2,3 or 1,3,4 */
2577 return wm_lp + (wm_lp >= 2 && pipe_wm->wm[4].enable);
2578}
2579
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02002580/* The value we need to program into the WM_LPx latency field */
2581static unsigned int ilk_wm_lp_latency(struct drm_device *dev, int level)
2582{
2583 struct drm_i915_private *dev_priv = dev->dev_private;
2584
Ville Syrjäläa42a5712014-01-07 16:14:08 +02002585 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02002586 return 2 * level;
2587 else
2588 return dev_priv->wm.pri_latency[level];
2589}
2590
Imre Deak820c1982013-12-17 14:46:36 +02002591static void ilk_compute_wm_results(struct drm_device *dev,
Ville Syrjälä0362c782013-10-09 19:17:57 +03002592 const struct intel_pipe_wm *merged,
Ville Syrjälä609cede2013-10-09 19:18:03 +03002593 enum intel_ddb_partitioning partitioning,
Imre Deak820c1982013-12-17 14:46:36 +02002594 struct ilk_wm_values *results)
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002595{
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002596 struct intel_crtc *intel_crtc;
2597 int level, wm_lp;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002598
Ville Syrjälä0362c782013-10-09 19:17:57 +03002599 results->enable_fbc_wm = merged->fbc_wm_enabled;
Ville Syrjälä609cede2013-10-09 19:18:03 +03002600 results->partitioning = partitioning;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002601
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002602 /* LP1+ register values */
Paulo Zanonicca32e92013-05-31 11:45:06 -03002603 for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
Ville Syrjälä1fd527c2013-08-06 22:24:05 +03002604 const struct intel_wm_level *r;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002605
Ville Syrjäläb380ca32013-10-09 19:18:01 +03002606 level = ilk_wm_lp_to_level(wm_lp, merged);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002607
Ville Syrjälä0362c782013-10-09 19:17:57 +03002608 r = &merged->wm[level];
Paulo Zanonicca32e92013-05-31 11:45:06 -03002609
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002610 /*
2611 * Maintain the watermark values even if the level is
2612 * disabled. Doing otherwise could cause underruns.
2613 */
2614 results->wm_lp[wm_lp - 1] =
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02002615 (ilk_wm_lp_latency(dev, level) << WM1_LP_LATENCY_SHIFT) |
Ville Syrjälä416f4722013-11-02 21:07:46 -07002616 (r->pri_val << WM1_LP_SR_SHIFT) |
2617 r->cur_val;
2618
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002619 if (r->enable)
2620 results->wm_lp[wm_lp - 1] |= WM1_LP_SR_EN;
2621
Ville Syrjälä416f4722013-11-02 21:07:46 -07002622 if (INTEL_INFO(dev)->gen >= 8)
2623 results->wm_lp[wm_lp - 1] |=
2624 r->fbc_val << WM1_LP_FBC_SHIFT_BDW;
2625 else
2626 results->wm_lp[wm_lp - 1] |=
2627 r->fbc_val << WM1_LP_FBC_SHIFT;
2628
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002629 /*
2630 * Always set WM1S_LP_EN when spr_val != 0, even if the
2631 * level is disabled. Doing otherwise could cause underruns.
2632 */
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02002633 if (INTEL_INFO(dev)->gen <= 6 && r->spr_val) {
2634 WARN_ON(wm_lp != 1);
2635 results->wm_lp_spr[wm_lp - 1] = WM1S_LP_EN | r->spr_val;
2636 } else
2637 results->wm_lp_spr[wm_lp - 1] = r->spr_val;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002638 }
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002639
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002640 /* LP0 register values */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01002641 for_each_intel_crtc(dev, intel_crtc) {
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002642 enum pipe pipe = intel_crtc->pipe;
2643 const struct intel_wm_level *r =
2644 &intel_crtc->wm.active.wm[0];
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002645
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002646 if (WARN_ON(!r->enable))
2647 continue;
2648
2649 results->wm_linetime[pipe] = intel_crtc->wm.active.linetime;
2650
2651 results->wm_pipe[pipe] =
2652 (r->pri_val << WM0_PIPE_PLANE_SHIFT) |
2653 (r->spr_val << WM0_PIPE_SPRITE_SHIFT) |
2654 r->cur_val;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002655 }
2656}
2657
Paulo Zanoni861f3382013-05-31 10:19:21 -03002658/* Find the result with the highest level enabled. Check for enable_fbc_wm in
2659 * case both are at the same level. Prefer r1 in case they're the same. */
Imre Deak820c1982013-12-17 14:46:36 +02002660static struct intel_pipe_wm *ilk_find_best_result(struct drm_device *dev,
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002661 struct intel_pipe_wm *r1,
2662 struct intel_pipe_wm *r2)
Paulo Zanoni861f3382013-05-31 10:19:21 -03002663{
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002664 int level, max_level = ilk_wm_max_level(dev);
2665 int level1 = 0, level2 = 0;
Paulo Zanoni861f3382013-05-31 10:19:21 -03002666
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002667 for (level = 1; level <= max_level; level++) {
2668 if (r1->wm[level].enable)
2669 level1 = level;
2670 if (r2->wm[level].enable)
2671 level2 = level;
Paulo Zanoni861f3382013-05-31 10:19:21 -03002672 }
2673
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002674 if (level1 == level2) {
2675 if (r2->fbc_wm_enabled && !r1->fbc_wm_enabled)
Paulo Zanoni861f3382013-05-31 10:19:21 -03002676 return r2;
2677 else
2678 return r1;
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002679 } else if (level1 > level2) {
Paulo Zanoni861f3382013-05-31 10:19:21 -03002680 return r1;
2681 } else {
2682 return r2;
2683 }
2684}
2685
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002686/* dirty bits used to track which watermarks need changes */
2687#define WM_DIRTY_PIPE(pipe) (1 << (pipe))
2688#define WM_DIRTY_LINETIME(pipe) (1 << (8 + (pipe)))
2689#define WM_DIRTY_LP(wm_lp) (1 << (15 + (wm_lp)))
2690#define WM_DIRTY_LP_ALL (WM_DIRTY_LP(1) | WM_DIRTY_LP(2) | WM_DIRTY_LP(3))
2691#define WM_DIRTY_FBC (1 << 24)
2692#define WM_DIRTY_DDB (1 << 25)
2693
Damien Lespiau055e3932014-08-18 13:49:10 +01002694static unsigned int ilk_compute_wm_dirty(struct drm_i915_private *dev_priv,
Imre Deak820c1982013-12-17 14:46:36 +02002695 const struct ilk_wm_values *old,
2696 const struct ilk_wm_values *new)
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002697{
2698 unsigned int dirty = 0;
2699 enum pipe pipe;
2700 int wm_lp;
2701
Damien Lespiau055e3932014-08-18 13:49:10 +01002702 for_each_pipe(dev_priv, pipe) {
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002703 if (old->wm_linetime[pipe] != new->wm_linetime[pipe]) {
2704 dirty |= WM_DIRTY_LINETIME(pipe);
2705 /* Must disable LP1+ watermarks too */
2706 dirty |= WM_DIRTY_LP_ALL;
2707 }
2708
2709 if (old->wm_pipe[pipe] != new->wm_pipe[pipe]) {
2710 dirty |= WM_DIRTY_PIPE(pipe);
2711 /* Must disable LP1+ watermarks too */
2712 dirty |= WM_DIRTY_LP_ALL;
2713 }
2714 }
2715
2716 if (old->enable_fbc_wm != new->enable_fbc_wm) {
2717 dirty |= WM_DIRTY_FBC;
2718 /* Must disable LP1+ watermarks too */
2719 dirty |= WM_DIRTY_LP_ALL;
2720 }
2721
2722 if (old->partitioning != new->partitioning) {
2723 dirty |= WM_DIRTY_DDB;
2724 /* Must disable LP1+ watermarks too */
2725 dirty |= WM_DIRTY_LP_ALL;
2726 }
2727
2728 /* LP1+ watermarks already deemed dirty, no need to continue */
2729 if (dirty & WM_DIRTY_LP_ALL)
2730 return dirty;
2731
2732 /* Find the lowest numbered LP1+ watermark in need of an update... */
2733 for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
2734 if (old->wm_lp[wm_lp - 1] != new->wm_lp[wm_lp - 1] ||
2735 old->wm_lp_spr[wm_lp - 1] != new->wm_lp_spr[wm_lp - 1])
2736 break;
2737 }
2738
2739 /* ...and mark it and all higher numbered LP1+ watermarks as dirty */
2740 for (; wm_lp <= 3; wm_lp++)
2741 dirty |= WM_DIRTY_LP(wm_lp);
2742
2743 return dirty;
2744}
2745
Ville Syrjälä8553c182013-12-05 15:51:39 +02002746static bool _ilk_disable_lp_wm(struct drm_i915_private *dev_priv,
2747 unsigned int dirty)
2748{
Imre Deak820c1982013-12-17 14:46:36 +02002749 struct ilk_wm_values *previous = &dev_priv->wm.hw;
Ville Syrjälä8553c182013-12-05 15:51:39 +02002750 bool changed = false;
2751
2752 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] & WM1_LP_SR_EN) {
2753 previous->wm_lp[2] &= ~WM1_LP_SR_EN;
2754 I915_WRITE(WM3_LP_ILK, previous->wm_lp[2]);
2755 changed = true;
2756 }
2757 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] & WM1_LP_SR_EN) {
2758 previous->wm_lp[1] &= ~WM1_LP_SR_EN;
2759 I915_WRITE(WM2_LP_ILK, previous->wm_lp[1]);
2760 changed = true;
2761 }
2762 if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] & WM1_LP_SR_EN) {
2763 previous->wm_lp[0] &= ~WM1_LP_SR_EN;
2764 I915_WRITE(WM1_LP_ILK, previous->wm_lp[0]);
2765 changed = true;
2766 }
2767
2768 /*
2769 * Don't touch WM1S_LP_EN here.
2770 * Doing so could cause underruns.
2771 */
2772
2773 return changed;
2774}
2775
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002776/*
2777 * The spec says we shouldn't write when we don't need, because every write
2778 * causes WMs to be re-evaluated, expending some power.
2779 */
Imre Deak820c1982013-12-17 14:46:36 +02002780static void ilk_write_wm_values(struct drm_i915_private *dev_priv,
2781 struct ilk_wm_values *results)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002782{
Ville Syrjäläac9545f2013-12-05 15:51:28 +02002783 struct drm_device *dev = dev_priv->dev;
Imre Deak820c1982013-12-17 14:46:36 +02002784 struct ilk_wm_values *previous = &dev_priv->wm.hw;
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002785 unsigned int dirty;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002786 uint32_t val;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002787
Damien Lespiau055e3932014-08-18 13:49:10 +01002788 dirty = ilk_compute_wm_dirty(dev_priv, previous, results);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002789 if (!dirty)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002790 return;
2791
Ville Syrjälä8553c182013-12-05 15:51:39 +02002792 _ilk_disable_lp_wm(dev_priv, dirty);
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02002793
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002794 if (dirty & WM_DIRTY_PIPE(PIPE_A))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002795 I915_WRITE(WM0_PIPEA_ILK, results->wm_pipe[0]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002796 if (dirty & WM_DIRTY_PIPE(PIPE_B))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002797 I915_WRITE(WM0_PIPEB_ILK, results->wm_pipe[1]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002798 if (dirty & WM_DIRTY_PIPE(PIPE_C))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002799 I915_WRITE(WM0_PIPEC_IVB, results->wm_pipe[2]);
2800
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002801 if (dirty & WM_DIRTY_LINETIME(PIPE_A))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002802 I915_WRITE(PIPE_WM_LINETIME(PIPE_A), results->wm_linetime[0]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002803 if (dirty & WM_DIRTY_LINETIME(PIPE_B))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002804 I915_WRITE(PIPE_WM_LINETIME(PIPE_B), results->wm_linetime[1]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002805 if (dirty & WM_DIRTY_LINETIME(PIPE_C))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002806 I915_WRITE(PIPE_WM_LINETIME(PIPE_C), results->wm_linetime[2]);
2807
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002808 if (dirty & WM_DIRTY_DDB) {
Ville Syrjäläa42a5712014-01-07 16:14:08 +02002809 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Ville Syrjäläac9545f2013-12-05 15:51:28 +02002810 val = I915_READ(WM_MISC);
2811 if (results->partitioning == INTEL_DDB_PART_1_2)
2812 val &= ~WM_MISC_DATA_PARTITION_5_6;
2813 else
2814 val |= WM_MISC_DATA_PARTITION_5_6;
2815 I915_WRITE(WM_MISC, val);
2816 } else {
2817 val = I915_READ(DISP_ARB_CTL2);
2818 if (results->partitioning == INTEL_DDB_PART_1_2)
2819 val &= ~DISP_DATA_PARTITION_5_6;
2820 else
2821 val |= DISP_DATA_PARTITION_5_6;
2822 I915_WRITE(DISP_ARB_CTL2, val);
2823 }
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002824 }
2825
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002826 if (dirty & WM_DIRTY_FBC) {
Paulo Zanonicca32e92013-05-31 11:45:06 -03002827 val = I915_READ(DISP_ARB_CTL);
2828 if (results->enable_fbc_wm)
2829 val &= ~DISP_FBC_WM_DIS;
2830 else
2831 val |= DISP_FBC_WM_DIS;
2832 I915_WRITE(DISP_ARB_CTL, val);
2833 }
2834
Imre Deak954911e2013-12-17 14:46:34 +02002835 if (dirty & WM_DIRTY_LP(1) &&
2836 previous->wm_lp_spr[0] != results->wm_lp_spr[0])
2837 I915_WRITE(WM1S_LP_ILK, results->wm_lp_spr[0]);
2838
2839 if (INTEL_INFO(dev)->gen >= 7) {
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02002840 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp_spr[1] != results->wm_lp_spr[1])
2841 I915_WRITE(WM2S_LP_IVB, results->wm_lp_spr[1]);
2842 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp_spr[2] != results->wm_lp_spr[2])
2843 I915_WRITE(WM3S_LP_IVB, results->wm_lp_spr[2]);
2844 }
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002845
Ville Syrjäläfacd619b2013-12-05 15:51:33 +02002846 if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] != results->wm_lp[0])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002847 I915_WRITE(WM1_LP_ILK, results->wm_lp[0]);
Ville Syrjäläfacd619b2013-12-05 15:51:33 +02002848 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] != results->wm_lp[1])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002849 I915_WRITE(WM2_LP_ILK, results->wm_lp[1]);
Ville Syrjäläfacd619b2013-12-05 15:51:33 +02002850 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] != results->wm_lp[2])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002851 I915_WRITE(WM3_LP_ILK, results->wm_lp[2]);
Ville Syrjälä609cede2013-10-09 19:18:03 +03002852
2853 dev_priv->wm.hw = *results;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002854}
2855
Ville Syrjälä8553c182013-12-05 15:51:39 +02002856static bool ilk_disable_lp_wm(struct drm_device *dev)
2857{
2858 struct drm_i915_private *dev_priv = dev->dev_private;
2859
2860 return _ilk_disable_lp_wm(dev_priv, WM_DIRTY_LP_ALL);
2861}
2862
Imre Deak820c1982013-12-17 14:46:36 +02002863static void ilk_update_wm(struct drm_crtc *crtc)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002864{
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03002865 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjälä46ba6142013-09-10 11:40:40 +03002866 struct drm_device *dev = crtc->dev;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002867 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak820c1982013-12-17 14:46:36 +02002868 struct ilk_wm_maximums max;
2869 struct ilk_pipe_wm_parameters params = {};
2870 struct ilk_wm_values results = {};
Ville Syrjälä77c122b2013-08-06 22:24:04 +03002871 enum intel_ddb_partitioning partitioning;
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03002872 struct intel_pipe_wm pipe_wm = {};
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002873 struct intel_pipe_wm lp_wm_1_2 = {}, lp_wm_5_6 = {}, *best_lp_wm;
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03002874 struct intel_wm_config config = {};
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002875
Ville Syrjälä2a44b762014-03-07 18:32:09 +02002876 ilk_compute_wm_parameters(crtc, &params);
Paulo Zanoni861f3382013-05-31 10:19:21 -03002877
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03002878 intel_compute_pipe_wm(crtc, &params, &pipe_wm);
2879
2880 if (!memcmp(&intel_crtc->wm.active, &pipe_wm, sizeof(pipe_wm)))
2881 return;
2882
2883 intel_crtc->wm.active = pipe_wm;
2884
Ville Syrjälä2a44b762014-03-07 18:32:09 +02002885 ilk_compute_wm_config(dev, &config);
2886
Ville Syrjälä34982fe2013-10-09 19:18:09 +03002887 ilk_compute_wm_maximums(dev, 1, &config, INTEL_DDB_PART_1_2, &max);
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02002888 ilk_wm_merge(dev, &config, &max, &lp_wm_1_2);
Ville Syrjälä0362c782013-10-09 19:17:57 +03002889
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03002890 /* 5/6 split only in single pipe config on IVB+ */
Ville Syrjäläec98c8d2013-10-11 15:26:26 +03002891 if (INTEL_INFO(dev)->gen >= 7 &&
2892 config.num_pipes_active == 1 && config.sprites_enabled) {
Ville Syrjälä34982fe2013-10-09 19:18:09 +03002893 ilk_compute_wm_maximums(dev, 1, &config, INTEL_DDB_PART_5_6, &max);
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02002894 ilk_wm_merge(dev, &config, &max, &lp_wm_5_6);
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03002895
Imre Deak820c1982013-12-17 14:46:36 +02002896 best_lp_wm = ilk_find_best_result(dev, &lp_wm_1_2, &lp_wm_5_6);
Paulo Zanoni861f3382013-05-31 10:19:21 -03002897 } else {
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002898 best_lp_wm = &lp_wm_1_2;
Paulo Zanoni861f3382013-05-31 10:19:21 -03002899 }
2900
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002901 partitioning = (best_lp_wm == &lp_wm_1_2) ?
Ville Syrjälä77c122b2013-08-06 22:24:04 +03002902 INTEL_DDB_PART_1_2 : INTEL_DDB_PART_5_6;
Paulo Zanoni861f3382013-05-31 10:19:21 -03002903
Imre Deak820c1982013-12-17 14:46:36 +02002904 ilk_compute_wm_results(dev, best_lp_wm, partitioning, &results);
Ville Syrjälä609cede2013-10-09 19:18:03 +03002905
Imre Deak820c1982013-12-17 14:46:36 +02002906 ilk_write_wm_values(dev_priv, &results);
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002907}
2908
Damien Lespiaued57cb82014-07-15 09:21:24 +02002909static void
2910ilk_update_sprite_wm(struct drm_plane *plane,
2911 struct drm_crtc *crtc,
2912 uint32_t sprite_width, uint32_t sprite_height,
2913 int pixel_size, bool enabled, bool scaled)
Paulo Zanoni526682e2013-05-24 11:59:18 -03002914{
Ville Syrjälä8553c182013-12-05 15:51:39 +02002915 struct drm_device *dev = plane->dev;
Ville Syrjäläadf3d352013-08-06 22:24:11 +03002916 struct intel_plane *intel_plane = to_intel_plane(plane);
Paulo Zanoni526682e2013-05-24 11:59:18 -03002917
Ville Syrjäläadf3d352013-08-06 22:24:11 +03002918 intel_plane->wm.enabled = enabled;
2919 intel_plane->wm.scaled = scaled;
2920 intel_plane->wm.horiz_pixels = sprite_width;
Damien Lespiaued57cb82014-07-15 09:21:24 +02002921 intel_plane->wm.vert_pixels = sprite_width;
Ville Syrjäläadf3d352013-08-06 22:24:11 +03002922 intel_plane->wm.bytes_per_pixel = pixel_size;
Paulo Zanoni526682e2013-05-24 11:59:18 -03002923
Ville Syrjälä8553c182013-12-05 15:51:39 +02002924 /*
2925 * IVB workaround: must disable low power watermarks for at least
2926 * one frame before enabling scaling. LP watermarks can be re-enabled
2927 * when scaling is disabled.
2928 *
2929 * WaCxSRDisabledForSpriteScaling:ivb
2930 */
2931 if (IS_IVYBRIDGE(dev) && scaled && ilk_disable_lp_wm(dev))
2932 intel_wait_for_vblank(dev, intel_plane->pipe);
2933
Imre Deak820c1982013-12-17 14:46:36 +02002934 ilk_update_wm(crtc);
Paulo Zanoni526682e2013-05-24 11:59:18 -03002935}
2936
Ville Syrjälä243e6a42013-10-14 14:55:24 +03002937static void ilk_pipe_wm_get_hw_state(struct drm_crtc *crtc)
2938{
2939 struct drm_device *dev = crtc->dev;
2940 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak820c1982013-12-17 14:46:36 +02002941 struct ilk_wm_values *hw = &dev_priv->wm.hw;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03002942 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2943 struct intel_pipe_wm *active = &intel_crtc->wm.active;
2944 enum pipe pipe = intel_crtc->pipe;
2945 static const unsigned int wm0_pipe_reg[] = {
2946 [PIPE_A] = WM0_PIPEA_ILK,
2947 [PIPE_B] = WM0_PIPEB_ILK,
2948 [PIPE_C] = WM0_PIPEC_IVB,
2949 };
2950
2951 hw->wm_pipe[pipe] = I915_READ(wm0_pipe_reg[pipe]);
Ville Syrjäläa42a5712014-01-07 16:14:08 +02002952 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Ville Syrjäläce0e0712013-12-05 15:51:36 +02002953 hw->wm_linetime[pipe] = I915_READ(PIPE_WM_LINETIME(pipe));
Ville Syrjälä243e6a42013-10-14 14:55:24 +03002954
Ville Syrjälä2a44b762014-03-07 18:32:09 +02002955 active->pipe_enabled = intel_crtc_active(crtc);
2956
2957 if (active->pipe_enabled) {
Ville Syrjälä243e6a42013-10-14 14:55:24 +03002958 u32 tmp = hw->wm_pipe[pipe];
2959
2960 /*
2961 * For active pipes LP0 watermark is marked as
2962 * enabled, and LP1+ watermaks as disabled since
2963 * we can't really reverse compute them in case
2964 * multiple pipes are active.
2965 */
2966 active->wm[0].enable = true;
2967 active->wm[0].pri_val = (tmp & WM0_PIPE_PLANE_MASK) >> WM0_PIPE_PLANE_SHIFT;
2968 active->wm[0].spr_val = (tmp & WM0_PIPE_SPRITE_MASK) >> WM0_PIPE_SPRITE_SHIFT;
2969 active->wm[0].cur_val = tmp & WM0_PIPE_CURSOR_MASK;
2970 active->linetime = hw->wm_linetime[pipe];
2971 } else {
2972 int level, max_level = ilk_wm_max_level(dev);
2973
2974 /*
2975 * For inactive pipes, all watermark levels
2976 * should be marked as enabled but zeroed,
2977 * which is what we'd compute them to.
2978 */
2979 for (level = 0; level <= max_level; level++)
2980 active->wm[level].enable = true;
2981 }
2982}
2983
2984void ilk_wm_get_hw_state(struct drm_device *dev)
2985{
2986 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak820c1982013-12-17 14:46:36 +02002987 struct ilk_wm_values *hw = &dev_priv->wm.hw;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03002988 struct drm_crtc *crtc;
2989
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01002990 for_each_crtc(dev, crtc)
Ville Syrjälä243e6a42013-10-14 14:55:24 +03002991 ilk_pipe_wm_get_hw_state(crtc);
2992
2993 hw->wm_lp[0] = I915_READ(WM1_LP_ILK);
2994 hw->wm_lp[1] = I915_READ(WM2_LP_ILK);
2995 hw->wm_lp[2] = I915_READ(WM3_LP_ILK);
2996
2997 hw->wm_lp_spr[0] = I915_READ(WM1S_LP_ILK);
Ville Syrjäläcfa76982014-03-07 18:32:08 +02002998 if (INTEL_INFO(dev)->gen >= 7) {
2999 hw->wm_lp_spr[1] = I915_READ(WM2S_LP_IVB);
3000 hw->wm_lp_spr[2] = I915_READ(WM3S_LP_IVB);
3001 }
Ville Syrjälä243e6a42013-10-14 14:55:24 +03003002
Ville Syrjäläa42a5712014-01-07 16:14:08 +02003003 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Ville Syrjäläac9545f2013-12-05 15:51:28 +02003004 hw->partitioning = (I915_READ(WM_MISC) & WM_MISC_DATA_PARTITION_5_6) ?
3005 INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
3006 else if (IS_IVYBRIDGE(dev))
3007 hw->partitioning = (I915_READ(DISP_ARB_CTL2) & DISP_DATA_PARTITION_5_6) ?
3008 INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03003009
3010 hw->enable_fbc_wm =
3011 !(I915_READ(DISP_ARB_CTL) & DISP_FBC_WM_DIS);
3012}
3013
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03003014/**
3015 * intel_update_watermarks - update FIFO watermark values based on current modes
3016 *
3017 * Calculate watermark values for the various WM regs based on current mode
3018 * and plane configuration.
3019 *
3020 * There are several cases to deal with here:
3021 * - normal (i.e. non-self-refresh)
3022 * - self-refresh (SR) mode
3023 * - lines are large relative to FIFO size (buffer can hold up to 2)
3024 * - lines are small relative to FIFO size (buffer can hold more than 2
3025 * lines), so need to account for TLB latency
3026 *
3027 * The normal calculation is:
3028 * watermark = dotclock * bytes per pixel * latency
3029 * where latency is platform & configuration dependent (we assume pessimal
3030 * values here).
3031 *
3032 * The SR calculation is:
3033 * watermark = (trunc(latency/line time)+1) * surface width *
3034 * bytes per pixel
3035 * where
3036 * line time = htotal / dotclock
3037 * surface width = hdisplay for normal plane and 64 for cursor
3038 * and latency is assumed to be high, as above.
3039 *
3040 * The final value programmed to the register should always be rounded up,
3041 * and include an extra 2 entries to account for clock crossings.
3042 *
3043 * We don't use the sprite, so we can ignore that. And on Crestline we have
3044 * to set the non-SR watermarks to 8.
3045 */
Ville Syrjälä46ba6142013-09-10 11:40:40 +03003046void intel_update_watermarks(struct drm_crtc *crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03003047{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03003048 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03003049
3050 if (dev_priv->display.update_wm)
Ville Syrjälä46ba6142013-09-10 11:40:40 +03003051 dev_priv->display.update_wm(crtc);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03003052}
3053
Ville Syrjäläadf3d352013-08-06 22:24:11 +03003054void intel_update_sprite_watermarks(struct drm_plane *plane,
3055 struct drm_crtc *crtc,
Damien Lespiaued57cb82014-07-15 09:21:24 +02003056 uint32_t sprite_width,
3057 uint32_t sprite_height,
3058 int pixel_size,
Ville Syrjälä39db4a42013-08-06 22:24:00 +03003059 bool enabled, bool scaled)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03003060{
Ville Syrjäläadf3d352013-08-06 22:24:11 +03003061 struct drm_i915_private *dev_priv = plane->dev->dev_private;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03003062
3063 if (dev_priv->display.update_sprite_wm)
Damien Lespiaued57cb82014-07-15 09:21:24 +02003064 dev_priv->display.update_sprite_wm(plane, crtc,
3065 sprite_width, sprite_height,
Ville Syrjälä39db4a42013-08-06 22:24:00 +03003066 pixel_size, enabled, scaled);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03003067}
3068
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003069static struct drm_i915_gem_object *
3070intel_alloc_context_page(struct drm_device *dev)
3071{
3072 struct drm_i915_gem_object *ctx;
3073 int ret;
3074
3075 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
3076
3077 ctx = i915_gem_alloc_object(dev, 4096);
3078 if (!ctx) {
3079 DRM_DEBUG("failed to alloc power context, RC6 disabled\n");
3080 return NULL;
3081 }
3082
Daniel Vetterc69766f2014-02-14 14:01:17 +01003083 ret = i915_gem_obj_ggtt_pin(ctx, 4096, 0);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003084 if (ret) {
3085 DRM_ERROR("failed to pin power context: %d\n", ret);
3086 goto err_unref;
3087 }
3088
3089 ret = i915_gem_object_set_to_gtt_domain(ctx, 1);
3090 if (ret) {
3091 DRM_ERROR("failed to set-domain on power context: %d\n", ret);
3092 goto err_unpin;
3093 }
3094
3095 return ctx;
3096
3097err_unpin:
Ben Widawskyd7f46fc2013-12-06 14:10:55 -08003098 i915_gem_object_ggtt_unpin(ctx);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003099err_unref:
3100 drm_gem_object_unreference(&ctx->base);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003101 return NULL;
3102}
3103
Daniel Vetter92703882012-08-09 16:46:01 +02003104/**
3105 * Lock protecting IPS related data structures
Daniel Vetter92703882012-08-09 16:46:01 +02003106 */
3107DEFINE_SPINLOCK(mchdev_lock);
3108
3109/* Global for IPS driver to get at the current i915 device. Protected by
3110 * mchdev_lock. */
3111static struct drm_i915_private *i915_mch_dev;
3112
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003113bool ironlake_set_drps(struct drm_device *dev, u8 val)
3114{
3115 struct drm_i915_private *dev_priv = dev->dev_private;
3116 u16 rgvswctl;
3117
Daniel Vetter92703882012-08-09 16:46:01 +02003118 assert_spin_locked(&mchdev_lock);
3119
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003120 rgvswctl = I915_READ16(MEMSWCTL);
3121 if (rgvswctl & MEMCTL_CMD_STS) {
3122 DRM_DEBUG("gpu busy, RCS change rejected\n");
3123 return false; /* still busy with another command */
3124 }
3125
3126 rgvswctl = (MEMCTL_CMD_CHFREQ << MEMCTL_CMD_SHIFT) |
3127 (val << MEMCTL_FREQ_SHIFT) | MEMCTL_SFCAVM;
3128 I915_WRITE16(MEMSWCTL, rgvswctl);
3129 POSTING_READ16(MEMSWCTL);
3130
3131 rgvswctl |= MEMCTL_CMD_STS;
3132 I915_WRITE16(MEMSWCTL, rgvswctl);
3133
3134 return true;
3135}
3136
Daniel Vetter8090c6b2012-06-24 16:42:32 +02003137static void ironlake_enable_drps(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003138{
3139 struct drm_i915_private *dev_priv = dev->dev_private;
3140 u32 rgvmodectl = I915_READ(MEMMODECTL);
3141 u8 fmax, fmin, fstart, vstart;
3142
Daniel Vetter92703882012-08-09 16:46:01 +02003143 spin_lock_irq(&mchdev_lock);
3144
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003145 /* Enable temp reporting */
3146 I915_WRITE16(PMMISC, I915_READ(PMMISC) | MCPPCE_EN);
3147 I915_WRITE16(TSC1, I915_READ(TSC1) | TSE);
3148
3149 /* 100ms RC evaluation intervals */
3150 I915_WRITE(RCUPEI, 100000);
3151 I915_WRITE(RCDNEI, 100000);
3152
3153 /* Set max/min thresholds to 90ms and 80ms respectively */
3154 I915_WRITE(RCBMAXAVG, 90000);
3155 I915_WRITE(RCBMINAVG, 80000);
3156
3157 I915_WRITE(MEMIHYST, 1);
3158
3159 /* Set up min, max, and cur for interrupt handling */
3160 fmax = (rgvmodectl & MEMMODE_FMAX_MASK) >> MEMMODE_FMAX_SHIFT;
3161 fmin = (rgvmodectl & MEMMODE_FMIN_MASK);
3162 fstart = (rgvmodectl & MEMMODE_FSTART_MASK) >>
3163 MEMMODE_FSTART_SHIFT;
3164
3165 vstart = (I915_READ(PXVFREQ_BASE + (fstart * 4)) & PXVFREQ_PX_MASK) >>
3166 PXVFREQ_PX_SHIFT;
3167
Daniel Vetter20e4d402012-08-08 23:35:39 +02003168 dev_priv->ips.fmax = fmax; /* IPS callback will increase this */
3169 dev_priv->ips.fstart = fstart;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003170
Daniel Vetter20e4d402012-08-08 23:35:39 +02003171 dev_priv->ips.max_delay = fstart;
3172 dev_priv->ips.min_delay = fmin;
3173 dev_priv->ips.cur_delay = fstart;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003174
3175 DRM_DEBUG_DRIVER("fmax: %d, fmin: %d, fstart: %d\n",
3176 fmax, fmin, fstart);
3177
3178 I915_WRITE(MEMINTREN, MEMINT_CX_SUPR_EN | MEMINT_EVAL_CHG_EN);
3179
3180 /*
3181 * Interrupts will be enabled in ironlake_irq_postinstall
3182 */
3183
3184 I915_WRITE(VIDSTART, vstart);
3185 POSTING_READ(VIDSTART);
3186
3187 rgvmodectl |= MEMMODE_SWMODE_EN;
3188 I915_WRITE(MEMMODECTL, rgvmodectl);
3189
Daniel Vetter92703882012-08-09 16:46:01 +02003190 if (wait_for_atomic((I915_READ(MEMSWCTL) & MEMCTL_CMD_STS) == 0, 10))
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003191 DRM_ERROR("stuck trying to change perf mode\n");
Daniel Vetter92703882012-08-09 16:46:01 +02003192 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003193
3194 ironlake_set_drps(dev, fstart);
3195
Daniel Vetter20e4d402012-08-08 23:35:39 +02003196 dev_priv->ips.last_count1 = I915_READ(0x112e4) + I915_READ(0x112e8) +
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003197 I915_READ(0x112e0);
Daniel Vetter20e4d402012-08-08 23:35:39 +02003198 dev_priv->ips.last_time1 = jiffies_to_msecs(jiffies);
3199 dev_priv->ips.last_count2 = I915_READ(0x112f4);
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00003200 dev_priv->ips.last_time2 = ktime_get_raw_ns();
Daniel Vetter92703882012-08-09 16:46:01 +02003201
3202 spin_unlock_irq(&mchdev_lock);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003203}
3204
Daniel Vetter8090c6b2012-06-24 16:42:32 +02003205static void ironlake_disable_drps(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003206{
3207 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter92703882012-08-09 16:46:01 +02003208 u16 rgvswctl;
3209
3210 spin_lock_irq(&mchdev_lock);
3211
3212 rgvswctl = I915_READ16(MEMSWCTL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003213
3214 /* Ack interrupts, disable EFC interrupt */
3215 I915_WRITE(MEMINTREN, I915_READ(MEMINTREN) & ~MEMINT_EVAL_CHG_EN);
3216 I915_WRITE(MEMINTRSTS, MEMINT_EVAL_CHG);
3217 I915_WRITE(DEIER, I915_READ(DEIER) & ~DE_PCU_EVENT);
3218 I915_WRITE(DEIIR, DE_PCU_EVENT);
3219 I915_WRITE(DEIMR, I915_READ(DEIMR) | DE_PCU_EVENT);
3220
3221 /* Go back to the starting frequency */
Daniel Vetter20e4d402012-08-08 23:35:39 +02003222 ironlake_set_drps(dev, dev_priv->ips.fstart);
Daniel Vetter92703882012-08-09 16:46:01 +02003223 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003224 rgvswctl |= MEMCTL_CMD_STS;
3225 I915_WRITE(MEMSWCTL, rgvswctl);
Daniel Vetter92703882012-08-09 16:46:01 +02003226 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003227
Daniel Vetter92703882012-08-09 16:46:01 +02003228 spin_unlock_irq(&mchdev_lock);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003229}
3230
Daniel Vetteracbe9472012-07-26 11:50:05 +02003231/* There's a funny hw issue where the hw returns all 0 when reading from
3232 * GEN6_RP_INTERRUPT_LIMITS. Hence we always need to compute the desired value
3233 * ourselves, instead of doing a rmw cycle (which might result in us clearing
3234 * all limits and the gpu stuck at whatever frequency it is at atm).
3235 */
Chris Wilson6917c7b2013-11-06 13:56:26 -02003236static u32 gen6_rps_limits(struct drm_i915_private *dev_priv, u8 val)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003237{
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01003238 u32 limits;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003239
Daniel Vetter20b46e52012-07-26 11:16:14 +02003240 /* Only set the down limit when we've reached the lowest level to avoid
3241 * getting more interrupts, otherwise leave this clear. This prevents a
3242 * race in the hw when coming out of rc6: There's a tiny window where
3243 * the hw runs at the minimal clock before selecting the desired
3244 * frequency, if the down threshold expires in that window we will not
3245 * receive a down interrupt. */
Ben Widawskyb39fb292014-03-19 18:31:11 -07003246 limits = dev_priv->rps.max_freq_softlimit << 24;
3247 if (val <= dev_priv->rps.min_freq_softlimit)
3248 limits |= dev_priv->rps.min_freq_softlimit << 16;
Daniel Vetter20b46e52012-07-26 11:16:14 +02003249
3250 return limits;
3251}
3252
Chris Wilsondd75fdc2013-09-25 17:34:57 +01003253static void gen6_set_rps_thresholds(struct drm_i915_private *dev_priv, u8 val)
3254{
3255 int new_power;
3256
Daisy Sunc76bb612014-08-11 11:08:38 -07003257 if (dev_priv->rps.is_bdw_sw_turbo)
3258 return;
3259
Chris Wilsondd75fdc2013-09-25 17:34:57 +01003260 new_power = dev_priv->rps.power;
3261 switch (dev_priv->rps.power) {
3262 case LOW_POWER:
Ben Widawskyb39fb292014-03-19 18:31:11 -07003263 if (val > dev_priv->rps.efficient_freq + 1 && val > dev_priv->rps.cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01003264 new_power = BETWEEN;
3265 break;
3266
3267 case BETWEEN:
Ben Widawskyb39fb292014-03-19 18:31:11 -07003268 if (val <= dev_priv->rps.efficient_freq && val < dev_priv->rps.cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01003269 new_power = LOW_POWER;
Ben Widawskyb39fb292014-03-19 18:31:11 -07003270 else if (val >= dev_priv->rps.rp0_freq && val > dev_priv->rps.cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01003271 new_power = HIGH_POWER;
3272 break;
3273
3274 case HIGH_POWER:
Ben Widawskyb39fb292014-03-19 18:31:11 -07003275 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 +01003276 new_power = BETWEEN;
3277 break;
3278 }
3279 /* Max/min bins are special */
Ben Widawskyb39fb292014-03-19 18:31:11 -07003280 if (val == dev_priv->rps.min_freq_softlimit)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01003281 new_power = LOW_POWER;
Ben Widawskyb39fb292014-03-19 18:31:11 -07003282 if (val == dev_priv->rps.max_freq_softlimit)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01003283 new_power = HIGH_POWER;
3284 if (new_power == dev_priv->rps.power)
3285 return;
3286
3287 /* Note the units here are not exactly 1us, but 1280ns. */
3288 switch (new_power) {
3289 case LOW_POWER:
3290 /* Upclock if more than 95% busy over 16ms */
3291 I915_WRITE(GEN6_RP_UP_EI, 12500);
3292 I915_WRITE(GEN6_RP_UP_THRESHOLD, 11800);
3293
3294 /* Downclock if less than 85% busy over 32ms */
3295 I915_WRITE(GEN6_RP_DOWN_EI, 25000);
3296 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 21250);
3297
3298 I915_WRITE(GEN6_RP_CONTROL,
3299 GEN6_RP_MEDIA_TURBO |
3300 GEN6_RP_MEDIA_HW_NORMAL_MODE |
3301 GEN6_RP_MEDIA_IS_GFX |
3302 GEN6_RP_ENABLE |
3303 GEN6_RP_UP_BUSY_AVG |
3304 GEN6_RP_DOWN_IDLE_AVG);
3305 break;
3306
3307 case BETWEEN:
3308 /* Upclock if more than 90% busy over 13ms */
3309 I915_WRITE(GEN6_RP_UP_EI, 10250);
3310 I915_WRITE(GEN6_RP_UP_THRESHOLD, 9225);
3311
3312 /* Downclock if less than 75% busy over 32ms */
3313 I915_WRITE(GEN6_RP_DOWN_EI, 25000);
3314 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 18750);
3315
3316 I915_WRITE(GEN6_RP_CONTROL,
3317 GEN6_RP_MEDIA_TURBO |
3318 GEN6_RP_MEDIA_HW_NORMAL_MODE |
3319 GEN6_RP_MEDIA_IS_GFX |
3320 GEN6_RP_ENABLE |
3321 GEN6_RP_UP_BUSY_AVG |
3322 GEN6_RP_DOWN_IDLE_AVG);
3323 break;
3324
3325 case HIGH_POWER:
3326 /* Upclock if more than 85% busy over 10ms */
3327 I915_WRITE(GEN6_RP_UP_EI, 8000);
3328 I915_WRITE(GEN6_RP_UP_THRESHOLD, 6800);
3329
3330 /* Downclock if less than 60% busy over 32ms */
3331 I915_WRITE(GEN6_RP_DOWN_EI, 25000);
3332 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 15000);
3333
3334 I915_WRITE(GEN6_RP_CONTROL,
3335 GEN6_RP_MEDIA_TURBO |
3336 GEN6_RP_MEDIA_HW_NORMAL_MODE |
3337 GEN6_RP_MEDIA_IS_GFX |
3338 GEN6_RP_ENABLE |
3339 GEN6_RP_UP_BUSY_AVG |
3340 GEN6_RP_DOWN_IDLE_AVG);
3341 break;
3342 }
3343
3344 dev_priv->rps.power = new_power;
3345 dev_priv->rps.last_adj = 0;
3346}
3347
Chris Wilson2876ce72014-03-28 08:03:34 +00003348static u32 gen6_rps_pm_mask(struct drm_i915_private *dev_priv, u8 val)
3349{
3350 u32 mask = 0;
3351
3352 if (val > dev_priv->rps.min_freq_softlimit)
3353 mask |= GEN6_PM_RP_DOWN_THRESHOLD | GEN6_PM_RP_DOWN_TIMEOUT;
3354 if (val < dev_priv->rps.max_freq_softlimit)
3355 mask |= GEN6_PM_RP_UP_THRESHOLD;
3356
Chris Wilson7b3c29f2014-07-10 20:31:19 +01003357 mask |= dev_priv->pm_rps_events & (GEN6_PM_RP_DOWN_EI_EXPIRED | GEN6_PM_RP_UP_EI_EXPIRED);
3358 mask &= dev_priv->pm_rps_events;
3359
Chris Wilson2876ce72014-03-28 08:03:34 +00003360 /* IVB and SNB hard hangs on looping batchbuffer
3361 * if GEN6_PM_UP_EI_EXPIRED is masked.
3362 */
3363 if (INTEL_INFO(dev_priv->dev)->gen <= 7 && !IS_HASWELL(dev_priv->dev))
3364 mask |= GEN6_PM_RP_UP_EI_EXPIRED;
3365
Deepak Sbaccd452014-05-15 20:58:09 +03003366 if (IS_GEN8(dev_priv->dev))
3367 mask |= GEN8_PMINTR_REDIRECT_TO_NON_DISP;
3368
Chris Wilson2876ce72014-03-28 08:03:34 +00003369 return ~mask;
3370}
3371
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06003372/* gen6_set_rps is called to update the frequency request, but should also be
3373 * called when the range (min_delay and max_delay) is modified so that we can
3374 * update the GEN6_RP_INTERRUPT_LIMITS register accordingly. */
Daniel Vetter20b46e52012-07-26 11:16:14 +02003375void gen6_set_rps(struct drm_device *dev, u8 val)
3376{
3377 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01003378
Jesse Barnes4fc688c2012-11-02 11:14:01 -07003379 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Ben Widawskyb39fb292014-03-19 18:31:11 -07003380 WARN_ON(val > dev_priv->rps.max_freq_softlimit);
3381 WARN_ON(val < dev_priv->rps.min_freq_softlimit);
Daniel Vetter004777c2012-08-09 15:07:01 +02003382
Chris Wilsoneb64cad2014-03-27 08:24:20 +00003383 /* min/max delay may still have been modified so be sure to
3384 * write the limits value.
3385 */
3386 if (val != dev_priv->rps.cur_freq) {
3387 gen6_set_rps_thresholds(dev_priv, val);
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06003388
Ben Widawsky50e6a2a2014-03-31 17:16:43 -07003389 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Chris Wilsoneb64cad2014-03-27 08:24:20 +00003390 I915_WRITE(GEN6_RPNSWREQ,
3391 HSW_FREQUENCY(val));
3392 else
3393 I915_WRITE(GEN6_RPNSWREQ,
3394 GEN6_FREQUENCY(val) |
3395 GEN6_OFFSET(0) |
3396 GEN6_AGGRESSIVE_TURBO);
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06003397 }
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01003398
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01003399 /* Make sure we continue to get interrupts
3400 * until we hit the minimum or maximum frequencies.
3401 */
Chris Wilsoneb64cad2014-03-27 08:24:20 +00003402 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS, gen6_rps_limits(dev_priv, val));
Chris Wilson2876ce72014-03-28 08:03:34 +00003403 I915_WRITE(GEN6_PMINTRMSK, gen6_rps_pm_mask(dev_priv, val));
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01003404
Ben Widawskyd5570a72012-09-07 19:43:41 -07003405 POSTING_READ(GEN6_RPNSWREQ);
3406
Ben Widawskyb39fb292014-03-19 18:31:11 -07003407 dev_priv->rps.cur_freq = val;
Daniel Vetterbe2cde9a2012-08-30 13:26:48 +02003408 trace_intel_gpu_freq_change(val * 50);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003409}
3410
Deepak S76c3552f2014-01-30 23:08:16 +05303411/* vlv_set_rps_idle: Set the frequency to Rpn if Gfx clocks are down
3412 *
3413 * * If Gfx is Idle, then
3414 * 1. Mask Turbo interrupts
3415 * 2. Bring up Gfx clock
3416 * 3. Change the freq to Rpn and wait till P-Unit updates freq
3417 * 4. Clear the Force GFX CLK ON bit so that Gfx can down
3418 * 5. Unmask Turbo interrupts
3419*/
3420static void vlv_set_rps_idle(struct drm_i915_private *dev_priv)
3421{
Deepak S5549d252014-06-28 11:26:11 +05303422 struct drm_device *dev = dev_priv->dev;
3423
3424 /* Latest VLV doesn't need to force the gfx clock */
3425 if (dev->pdev->revision >= 0xd) {
3426 valleyview_set_rps(dev_priv->dev, dev_priv->rps.min_freq_softlimit);
3427 return;
3428 }
3429
Deepak S76c3552f2014-01-30 23:08:16 +05303430 /*
3431 * When we are idle. Drop to min voltage state.
3432 */
3433
Ben Widawskyb39fb292014-03-19 18:31:11 -07003434 if (dev_priv->rps.cur_freq <= dev_priv->rps.min_freq_softlimit)
Deepak S76c3552f2014-01-30 23:08:16 +05303435 return;
3436
3437 /* Mask turbo interrupt so that they will not come in between */
3438 I915_WRITE(GEN6_PMINTRMSK, 0xffffffff);
3439
Imre Deak650ad972014-04-18 16:35:02 +03003440 vlv_force_gfx_clock(dev_priv, true);
Deepak S76c3552f2014-01-30 23:08:16 +05303441
Ben Widawskyb39fb292014-03-19 18:31:11 -07003442 dev_priv->rps.cur_freq = dev_priv->rps.min_freq_softlimit;
Deepak S76c3552f2014-01-30 23:08:16 +05303443
3444 vlv_punit_write(dev_priv, PUNIT_REG_GPU_FREQ_REQ,
Ben Widawskyb39fb292014-03-19 18:31:11 -07003445 dev_priv->rps.min_freq_softlimit);
Deepak S76c3552f2014-01-30 23:08:16 +05303446
3447 if (wait_for(((vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS))
3448 & GENFREQSTATUS) == 0, 5))
3449 DRM_ERROR("timed out waiting for Punit\n");
3450
Imre Deak650ad972014-04-18 16:35:02 +03003451 vlv_force_gfx_clock(dev_priv, false);
Deepak S76c3552f2014-01-30 23:08:16 +05303452
Chris Wilson2876ce72014-03-28 08:03:34 +00003453 I915_WRITE(GEN6_PMINTRMSK,
3454 gen6_rps_pm_mask(dev_priv, dev_priv->rps.cur_freq));
Deepak S76c3552f2014-01-30 23:08:16 +05303455}
3456
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003457void gen6_rps_idle(struct drm_i915_private *dev_priv)
3458{
Damien Lespiau691bb712013-12-12 14:36:36 +00003459 struct drm_device *dev = dev_priv->dev;
3460
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003461 mutex_lock(&dev_priv->rps.hw_lock);
Chris Wilsonc0951f02013-10-10 21:58:50 +01003462 if (dev_priv->rps.enabled) {
Deepak S34638112014-06-28 11:26:26 +05303463 if (IS_CHERRYVIEW(dev))
3464 valleyview_set_rps(dev_priv->dev, dev_priv->rps.min_freq_softlimit);
3465 else if (IS_VALLEYVIEW(dev))
Deepak S76c3552f2014-01-30 23:08:16 +05303466 vlv_set_rps_idle(dev_priv);
Daisy Sunc76bb612014-08-11 11:08:38 -07003467 else if (!dev_priv->rps.is_bdw_sw_turbo
3468 || atomic_read(&dev_priv->rps.sw_turbo.flip_received)){
Ben Widawskyb39fb292014-03-19 18:31:11 -07003469 gen6_set_rps(dev_priv->dev, dev_priv->rps.min_freq_softlimit);
Daisy Sunc76bb612014-08-11 11:08:38 -07003470 }
3471
Chris Wilsonc0951f02013-10-10 21:58:50 +01003472 dev_priv->rps.last_adj = 0;
3473 }
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003474 mutex_unlock(&dev_priv->rps.hw_lock);
3475}
3476
3477void gen6_rps_boost(struct drm_i915_private *dev_priv)
3478{
Damien Lespiau691bb712013-12-12 14:36:36 +00003479 struct drm_device *dev = dev_priv->dev;
3480
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003481 mutex_lock(&dev_priv->rps.hw_lock);
Chris Wilsonc0951f02013-10-10 21:58:50 +01003482 if (dev_priv->rps.enabled) {
Damien Lespiau691bb712013-12-12 14:36:36 +00003483 if (IS_VALLEYVIEW(dev))
Ben Widawskyb39fb292014-03-19 18:31:11 -07003484 valleyview_set_rps(dev_priv->dev, dev_priv->rps.max_freq_softlimit);
Daisy Sunc76bb612014-08-11 11:08:38 -07003485 else if (!dev_priv->rps.is_bdw_sw_turbo
3486 || atomic_read(&dev_priv->rps.sw_turbo.flip_received)){
Ben Widawskyb39fb292014-03-19 18:31:11 -07003487 gen6_set_rps(dev_priv->dev, dev_priv->rps.max_freq_softlimit);
Daisy Sunc76bb612014-08-11 11:08:38 -07003488 }
3489
Chris Wilsonc0951f02013-10-10 21:58:50 +01003490 dev_priv->rps.last_adj = 0;
3491 }
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003492 mutex_unlock(&dev_priv->rps.hw_lock);
3493}
3494
Jesse Barnes0a073b82013-04-17 15:54:58 -07003495void valleyview_set_rps(struct drm_device *dev, u8 val)
3496{
3497 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä7a670922013-06-25 19:21:06 +03003498
Jesse Barnes0a073b82013-04-17 15:54:58 -07003499 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Ben Widawskyb39fb292014-03-19 18:31:11 -07003500 WARN_ON(val > dev_priv->rps.max_freq_softlimit);
3501 WARN_ON(val < dev_priv->rps.min_freq_softlimit);
Jesse Barnes0a073b82013-04-17 15:54:58 -07003502
Ville Syrjälä73008b92013-06-25 19:21:01 +03003503 DRM_DEBUG_DRIVER("GPU freq request from %d MHz (%u) to %d MHz (%u)\n",
Ben Widawskyb39fb292014-03-19 18:31:11 -07003504 vlv_gpu_freq(dev_priv, dev_priv->rps.cur_freq),
3505 dev_priv->rps.cur_freq,
Ville Syrjälä2ec38152013-11-05 22:42:29 +02003506 vlv_gpu_freq(dev_priv, val), val);
Jesse Barnes0a073b82013-04-17 15:54:58 -07003507
Ville Syrjälä1c147622014-08-18 14:42:43 +03003508 if (WARN_ONCE(IS_CHERRYVIEW(dev) && (val & 1),
3509 "Odd GPU freq value\n"))
3510 val &= ~1;
3511
Chris Wilson2876ce72014-03-28 08:03:34 +00003512 if (val != dev_priv->rps.cur_freq)
3513 vlv_punit_write(dev_priv, PUNIT_REG_GPU_FREQ_REQ, val);
Jesse Barnes0a073b82013-04-17 15:54:58 -07003514
Imre Deak09c87db2014-04-03 20:02:42 +03003515 I915_WRITE(GEN6_PMINTRMSK, gen6_rps_pm_mask(dev_priv, val));
Jesse Barnes0a073b82013-04-17 15:54:58 -07003516
Ben Widawskyb39fb292014-03-19 18:31:11 -07003517 dev_priv->rps.cur_freq = val;
Ville Syrjälä2ec38152013-11-05 22:42:29 +02003518 trace_intel_gpu_freq_change(vlv_gpu_freq(dev_priv, val));
Jesse Barnes0a073b82013-04-17 15:54:58 -07003519}
3520
Ben Widawsky09610212014-05-15 20:58:08 +03003521static void gen8_disable_rps_interrupts(struct drm_device *dev)
3522{
3523 struct drm_i915_private *dev_priv = dev->dev_private;
Daisy Sunc76bb612014-08-11 11:08:38 -07003524 if (IS_BROADWELL(dev) && dev_priv->rps.is_bdw_sw_turbo){
3525 if (atomic_read(&dev_priv->rps.sw_turbo.flip_received))
3526 del_timer(&dev_priv->rps.sw_turbo.flip_timer);
3527 dev_priv-> rps.is_bdw_sw_turbo = false;
3528 } else {
3529 I915_WRITE(GEN6_PMINTRMSK, ~GEN8_PMINTR_REDIRECT_TO_NON_DISP);
3530 I915_WRITE(GEN8_GT_IER(2), I915_READ(GEN8_GT_IER(2)) &
3531 ~dev_priv->pm_rps_events);
3532 /* Complete PM interrupt masking here doesn't race with the rps work
3533 * item again unmasking PM interrupts because that is using a different
3534 * register (GEN8_GT_IMR(2)) to mask PM interrupts. The only risk is in
3535 * leaving stale bits in GEN8_GT_IIR(2) and GEN8_GT_IMR(2) which
3536 * gen8_enable_rps will clean up. */
Ben Widawsky09610212014-05-15 20:58:08 +03003537
Daisy Sunc76bb612014-08-11 11:08:38 -07003538 spin_lock_irq(&dev_priv->irq_lock);
3539 dev_priv->rps.pm_iir = 0;
3540 spin_unlock_irq(&dev_priv->irq_lock);
Ben Widawsky09610212014-05-15 20:58:08 +03003541
Daisy Sunc76bb612014-08-11 11:08:38 -07003542 I915_WRITE(GEN8_GT_IIR(2), dev_priv->pm_rps_events);
3543 }
Ben Widawsky09610212014-05-15 20:58:08 +03003544}
3545
Daniel Vetter44fc7d52013-07-12 22:43:27 +02003546static void gen6_disable_rps_interrupts(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003547{
3548 struct drm_i915_private *dev_priv = dev->dev_private;
3549
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003550 I915_WRITE(GEN6_PMINTRMSK, 0xffffffff);
Deepak Sa6706b42014-03-15 20:23:22 +05303551 I915_WRITE(GEN6_PMIER, I915_READ(GEN6_PMIER) &
3552 ~dev_priv->pm_rps_events);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003553 /* Complete PM interrupt masking here doesn't race with the rps work
3554 * item again unmasking PM interrupts because that is using a different
3555 * register (PMIMR) to mask PM interrupts. The only risk is in leaving
3556 * stale bits in PMIIR and PMIMR which gen6_enable_rps will clean up. */
3557
Daniel Vetter59cdb632013-07-04 23:35:28 +02003558 spin_lock_irq(&dev_priv->irq_lock);
Daniel Vetterc6a828d2012-08-08 23:35:35 +02003559 dev_priv->rps.pm_iir = 0;
Daniel Vetter59cdb632013-07-04 23:35:28 +02003560 spin_unlock_irq(&dev_priv->irq_lock);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003561
Deepak Sa6706b42014-03-15 20:23:22 +05303562 I915_WRITE(GEN6_PMIIR, dev_priv->pm_rps_events);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003563}
3564
Daniel Vetter44fc7d52013-07-12 22:43:27 +02003565static void gen6_disable_rps(struct drm_device *dev)
3566{
3567 struct drm_i915_private *dev_priv = dev->dev_private;
3568
3569 I915_WRITE(GEN6_RC_CONTROL, 0);
3570 I915_WRITE(GEN6_RPNSWREQ, 1 << 31);
3571
Ben Widawsky09610212014-05-15 20:58:08 +03003572 if (IS_BROADWELL(dev))
3573 gen8_disable_rps_interrupts(dev);
3574 else
3575 gen6_disable_rps_interrupts(dev);
Daniel Vetter44fc7d52013-07-12 22:43:27 +02003576}
3577
Deepak S38807742014-05-23 21:00:15 +05303578static void cherryview_disable_rps(struct drm_device *dev)
3579{
3580 struct drm_i915_private *dev_priv = dev->dev_private;
3581
3582 I915_WRITE(GEN6_RC_CONTROL, 0);
Deepak S3497a562014-07-10 13:16:26 +05303583
3584 gen8_disable_rps_interrupts(dev);
Deepak S38807742014-05-23 21:00:15 +05303585}
3586
Jesse Barnesd20d4f02013-04-23 10:09:28 -07003587static void valleyview_disable_rps(struct drm_device *dev)
3588{
3589 struct drm_i915_private *dev_priv = dev->dev_private;
3590
Deepak S98a2e5f2014-08-18 10:35:27 -07003591 /* we're doing forcewake before Disabling RC6,
3592 * This what the BIOS expects when going into suspend */
3593 gen6_gt_force_wake_get(dev_priv, FORCEWAKE_ALL);
3594
Jesse Barnesd20d4f02013-04-23 10:09:28 -07003595 I915_WRITE(GEN6_RC_CONTROL, 0);
Jesse Barnesd20d4f02013-04-23 10:09:28 -07003596
Deepak S98a2e5f2014-08-18 10:35:27 -07003597 gen6_gt_force_wake_put(dev_priv, FORCEWAKE_ALL);
3598
Daniel Vetter44fc7d52013-07-12 22:43:27 +02003599 gen6_disable_rps_interrupts(dev);
Jesse Barnesd20d4f02013-04-23 10:09:28 -07003600}
3601
Ben Widawskydc39fff2013-10-18 12:32:07 -07003602static void intel_print_rc6_info(struct drm_device *dev, u32 mode)
3603{
Imre Deak91ca6892014-04-14 20:24:25 +03003604 if (IS_VALLEYVIEW(dev)) {
3605 if (mode & (GEN7_RC_CTL_TO_MODE | GEN6_RC_CTL_EI_MODE(1)))
3606 mode = GEN6_RC_CTL_RC6_ENABLE;
3607 else
3608 mode = 0;
3609 }
Daniel Vetter8dfd1f02014-08-04 11:15:56 +02003610 DRM_DEBUG_KMS("Enabling RC6 states: RC6 %s, RC6p %s, RC6pp %s\n",
3611 (mode & GEN6_RC_CTL_RC6_ENABLE) ? "on" : "off",
3612 (mode & GEN6_RC_CTL_RC6p_ENABLE) ? "on" : "off",
3613 (mode & GEN6_RC_CTL_RC6pp_ENABLE) ? "on" : "off");
Ben Widawskydc39fff2013-10-18 12:32:07 -07003614}
3615
Imre Deake6069ca2014-04-18 16:01:02 +03003616static int sanitize_rc6_option(const struct drm_device *dev, int enable_rc6)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003617{
Damien Lespiaueb4926e2013-06-07 17:41:14 +01003618 /* No RC6 before Ironlake */
3619 if (INTEL_INFO(dev)->gen < 5)
3620 return 0;
3621
Imre Deake6069ca2014-04-18 16:01:02 +03003622 /* RC6 is only on Ironlake mobile not on desktop */
3623 if (INTEL_INFO(dev)->gen == 5 && !IS_IRONLAKE_M(dev))
3624 return 0;
3625
Daniel Vetter456470e2012-08-08 23:35:40 +02003626 /* Respect the kernel parameter if it is set */
Imre Deake6069ca2014-04-18 16:01:02 +03003627 if (enable_rc6 >= 0) {
3628 int mask;
3629
3630 if (INTEL_INFO(dev)->gen == 6 || IS_IVYBRIDGE(dev))
3631 mask = INTEL_RC6_ENABLE | INTEL_RC6p_ENABLE |
3632 INTEL_RC6pp_ENABLE;
3633 else
3634 mask = INTEL_RC6_ENABLE;
3635
3636 if ((enable_rc6 & mask) != enable_rc6)
Daniel Vetter8dfd1f02014-08-04 11:15:56 +02003637 DRM_DEBUG_KMS("Adjusting RC6 mask to %d (requested %d, valid %d)\n",
3638 enable_rc6 & mask, enable_rc6, mask);
Imre Deake6069ca2014-04-18 16:01:02 +03003639
3640 return enable_rc6 & mask;
3641 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003642
Chris Wilson6567d742012-11-10 10:00:06 +00003643 /* Disable RC6 on Ironlake */
3644 if (INTEL_INFO(dev)->gen == 5)
3645 return 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003646
Ben Widawsky8bade1a2014-01-28 20:25:39 -08003647 if (IS_IVYBRIDGE(dev))
Ben Widawskycca84a12014-01-28 20:25:38 -08003648 return (INTEL_RC6_ENABLE | INTEL_RC6p_ENABLE);
Ben Widawsky8bade1a2014-01-28 20:25:39 -08003649
3650 return INTEL_RC6_ENABLE;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003651}
3652
Imre Deake6069ca2014-04-18 16:01:02 +03003653int intel_enable_rc6(const struct drm_device *dev)
3654{
3655 return i915.enable_rc6;
3656}
3657
Ben Widawsky09610212014-05-15 20:58:08 +03003658static void gen8_enable_rps_interrupts(struct drm_device *dev)
3659{
3660 struct drm_i915_private *dev_priv = dev->dev_private;
3661
3662 spin_lock_irq(&dev_priv->irq_lock);
3663 WARN_ON(dev_priv->rps.pm_iir);
Daniel Vetter480c8032014-07-16 09:49:40 +02003664 gen8_enable_pm_irq(dev_priv, dev_priv->pm_rps_events);
Ben Widawsky09610212014-05-15 20:58:08 +03003665 I915_WRITE(GEN8_GT_IIR(2), dev_priv->pm_rps_events);
3666 spin_unlock_irq(&dev_priv->irq_lock);
3667}
3668
Daniel Vetter44fc7d52013-07-12 22:43:27 +02003669static void gen6_enable_rps_interrupts(struct drm_device *dev)
3670{
3671 struct drm_i915_private *dev_priv = dev->dev_private;
3672
3673 spin_lock_irq(&dev_priv->irq_lock);
Daniel Vettera0b33352013-07-04 23:35:34 +02003674 WARN_ON(dev_priv->rps.pm_iir);
Daniel Vetter480c8032014-07-16 09:49:40 +02003675 gen6_enable_pm_irq(dev_priv, dev_priv->pm_rps_events);
Deepak Sa6706b42014-03-15 20:23:22 +05303676 I915_WRITE(GEN6_PMIIR, dev_priv->pm_rps_events);
Daniel Vetter44fc7d52013-07-12 22:43:27 +02003677 spin_unlock_irq(&dev_priv->irq_lock);
Daniel Vetter44fc7d52013-07-12 22:43:27 +02003678}
3679
Ben Widawsky3280e8b2014-03-31 17:16:42 -07003680static void parse_rp_state_cap(struct drm_i915_private *dev_priv, u32 rp_state_cap)
3681{
3682 /* All of these values are in units of 50MHz */
3683 dev_priv->rps.cur_freq = 0;
3684 /* static values from HW: RP0 < RPe < RP1 < RPn (min_freq) */
3685 dev_priv->rps.rp1_freq = (rp_state_cap >> 8) & 0xff;
3686 dev_priv->rps.rp0_freq = (rp_state_cap >> 0) & 0xff;
3687 dev_priv->rps.min_freq = (rp_state_cap >> 16) & 0xff;
3688 /* XXX: only BYT has a special efficient freq */
3689 dev_priv->rps.efficient_freq = dev_priv->rps.rp1_freq;
3690 /* hw_max = RP0 until we check for overclocking */
3691 dev_priv->rps.max_freq = dev_priv->rps.rp0_freq;
3692
3693 /* Preserve min/max settings in case of re-init */
3694 if (dev_priv->rps.max_freq_softlimit == 0)
3695 dev_priv->rps.max_freq_softlimit = dev_priv->rps.max_freq;
3696
3697 if (dev_priv->rps.min_freq_softlimit == 0)
3698 dev_priv->rps.min_freq_softlimit = dev_priv->rps.min_freq;
3699}
3700
Daisy Sunc76bb612014-08-11 11:08:38 -07003701static void bdw_sw_calculate_freq(struct drm_device *dev,
3702 struct intel_rps_bdw_cal *c, u32 *cur_time, u32 *c0)
3703{
3704 struct drm_i915_private *dev_priv = dev->dev_private;
3705 u64 busy = 0;
3706 u32 busyness_pct = 0;
3707 u32 elapsed_time = 0;
3708 u16 new_freq = 0;
3709
3710 if (!c || !cur_time || !c0)
3711 return;
3712
3713 if (0 == c->last_c0)
3714 goto out;
3715
3716 /* Check Evaluation interval */
3717 elapsed_time = *cur_time - c->last_ts;
3718 if (elapsed_time < c->eval_interval)
3719 return;
3720
3721 mutex_lock(&dev_priv->rps.hw_lock);
3722
3723 /*
3724 * c0 unit in 32*1.28 usec, elapsed_time unit in 1 usec.
3725 * Whole busyness_pct calculation should be
3726 * busy = ((u64)(*c0 - c->last_c0) << 5 << 7) / 100;
3727 * busyness_pct = (u32)(busy * 100 / elapsed_time);
3728 * The final formula is to simplify CPU calculation
3729 */
3730 busy = (u64)(*c0 - c->last_c0) << 12;
3731 do_div(busy, elapsed_time);
3732 busyness_pct = (u32)busy;
3733
3734 if (c->is_up && busyness_pct >= c->it_threshold_pct)
3735 new_freq = (u16)dev_priv->rps.cur_freq + 3;
3736 if (!c->is_up && busyness_pct <= c->it_threshold_pct)
3737 new_freq = (u16)dev_priv->rps.cur_freq - 1;
3738
3739 /* Adjust to new frequency busyness and compare with threshold */
3740 if (0 != new_freq) {
3741 if (new_freq > dev_priv->rps.max_freq_softlimit)
3742 new_freq = dev_priv->rps.max_freq_softlimit;
3743 else if (new_freq < dev_priv->rps.min_freq_softlimit)
3744 new_freq = dev_priv->rps.min_freq_softlimit;
3745
3746 gen6_set_rps(dev, new_freq);
3747 }
3748
3749 mutex_unlock(&dev_priv->rps.hw_lock);
3750
3751out:
3752 c->last_c0 = *c0;
3753 c->last_ts = *cur_time;
3754}
3755
3756static void gen8_set_frequency_RP0(struct work_struct *work)
3757{
3758 struct intel_rps_bdw_turbo *p_bdw_turbo =
3759 container_of(work, struct intel_rps_bdw_turbo, work_max_freq);
3760 struct intel_gen6_power_mgmt *p_power_mgmt =
3761 container_of(p_bdw_turbo, struct intel_gen6_power_mgmt, sw_turbo);
3762 struct drm_i915_private *dev_priv =
3763 container_of(p_power_mgmt, struct drm_i915_private, rps);
3764
3765 mutex_lock(&dev_priv->rps.hw_lock);
3766 gen6_set_rps(dev_priv->dev, dev_priv->rps.rp0_freq);
3767 mutex_unlock(&dev_priv->rps.hw_lock);
3768}
3769
3770static void flip_active_timeout_handler(unsigned long var)
3771{
3772 struct drm_i915_private *dev_priv = (struct drm_i915_private *) var;
3773
3774 del_timer(&dev_priv->rps.sw_turbo.flip_timer);
3775 atomic_set(&dev_priv->rps.sw_turbo.flip_received, false);
3776
3777 queue_work(dev_priv->wq, &dev_priv->rps.sw_turbo.work_max_freq);
3778}
3779
3780void bdw_software_turbo(struct drm_device *dev)
3781{
3782 struct drm_i915_private *dev_priv = dev->dev_private;
3783
3784 u32 current_time = I915_READ(TIMESTAMP_CTR); /* unit in usec */
3785 u32 current_c0 = I915_READ(MCHBAR_PCU_C0); /* unit in 32*1.28 usec */
3786
3787 bdw_sw_calculate_freq(dev, &dev_priv->rps.sw_turbo.up,
3788 &current_time, &current_c0);
3789 bdw_sw_calculate_freq(dev, &dev_priv->rps.sw_turbo.down,
3790 &current_time, &current_c0);
3791}
3792
Ben Widawsky6edee7f2013-11-02 21:07:52 -07003793static void gen8_enable_rps(struct drm_device *dev)
3794{
3795 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01003796 struct intel_engine_cs *ring;
Ben Widawsky6edee7f2013-11-02 21:07:52 -07003797 uint32_t rc6_mask = 0, rp_state_cap;
Daisy Sunc76bb612014-08-11 11:08:38 -07003798 uint32_t threshold_up_pct, threshold_down_pct;
3799 uint32_t ei_up, ei_down; /* up and down evaluation interval */
3800 u32 rp_ctl_flag;
Ben Widawsky6edee7f2013-11-02 21:07:52 -07003801 int unused;
3802
Daisy Sunc76bb612014-08-11 11:08:38 -07003803 /* Use software Turbo for BDW */
3804 dev_priv->rps.is_bdw_sw_turbo = IS_BROADWELL(dev);
3805
Ben Widawsky6edee7f2013-11-02 21:07:52 -07003806 /* 1a: Software RC state - RC0 */
3807 I915_WRITE(GEN6_RC_STATE, 0);
3808
3809 /* 1c & 1d: Get forcewake during program sequence. Although the driver
3810 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
Deepak Sc8d9a592013-11-23 14:55:42 +05303811 gen6_gt_force_wake_get(dev_priv, FORCEWAKE_ALL);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07003812
3813 /* 2a: Disable RC states. */
3814 I915_WRITE(GEN6_RC_CONTROL, 0);
3815
3816 rp_state_cap = I915_READ(GEN6_RP_STATE_CAP);
Ben Widawsky3280e8b2014-03-31 17:16:42 -07003817 parse_rp_state_cap(dev_priv, rp_state_cap);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07003818
3819 /* 2b: Program RC6 thresholds.*/
3820 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16);
3821 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
3822 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
3823 for_each_ring(ring, dev_priv, unused)
3824 I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
3825 I915_WRITE(GEN6_RC_SLEEP, 0);
Tom O'Rourke0d68b252014-04-09 11:44:06 -07003826 if (IS_BROADWELL(dev))
3827 I915_WRITE(GEN6_RC6_THRESHOLD, 625); /* 800us/1.28 for TO */
3828 else
3829 I915_WRITE(GEN6_RC6_THRESHOLD, 50000); /* 50/125ms per EI */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07003830
3831 /* 3: Enable RC6 */
3832 if (intel_enable_rc6(dev) & INTEL_RC6_ENABLE)
3833 rc6_mask = GEN6_RC_CTL_RC6_ENABLE;
Ben Widawskyabbf9d22014-01-28 20:25:41 -08003834 intel_print_rc6_info(dev, rc6_mask);
Tom O'Rourke0d68b252014-04-09 11:44:06 -07003835 if (IS_BROADWELL(dev))
3836 I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
3837 GEN7_RC_CTL_TO_MODE |
3838 rc6_mask);
3839 else
3840 I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
3841 GEN6_RC_CTL_EI_MODE(1) |
3842 rc6_mask);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07003843
3844 /* 4 Program defaults and thresholds for RPS*/
Ben Widawskyf9bdc582014-03-31 17:16:41 -07003845 I915_WRITE(GEN6_RPNSWREQ,
3846 HSW_FREQUENCY(dev_priv->rps.rp1_freq));
3847 I915_WRITE(GEN6_RC_VIDEO_FREQ,
3848 HSW_FREQUENCY(dev_priv->rps.rp1_freq));
Daisy Sunc76bb612014-08-11 11:08:38 -07003849 ei_up = 84480; /* 84.48ms */
3850 ei_down = 448000;
3851 threshold_up_pct = 90; /* x percent busy */
3852 threshold_down_pct = 70;
Ben Widawsky6edee7f2013-11-02 21:07:52 -07003853
Daisy Sunc76bb612014-08-11 11:08:38 -07003854 if (dev_priv->rps.is_bdw_sw_turbo) {
3855 dev_priv->rps.sw_turbo.up.it_threshold_pct = threshold_up_pct;
3856 dev_priv->rps.sw_turbo.up.eval_interval = ei_up;
3857 dev_priv->rps.sw_turbo.up.is_up = true;
3858 dev_priv->rps.sw_turbo.up.last_ts = 0;
3859 dev_priv->rps.sw_turbo.up.last_c0 = 0;
Ben Widawsky6edee7f2013-11-02 21:07:52 -07003860
Daisy Sunc76bb612014-08-11 11:08:38 -07003861 dev_priv->rps.sw_turbo.down.it_threshold_pct = threshold_down_pct;
3862 dev_priv->rps.sw_turbo.down.eval_interval = ei_down;
3863 dev_priv->rps.sw_turbo.down.is_up = false;
3864 dev_priv->rps.sw_turbo.down.last_ts = 0;
3865 dev_priv->rps.sw_turbo.down.last_c0 = 0;
Ben Widawsky6edee7f2013-11-02 21:07:52 -07003866
Daisy Sunc76bb612014-08-11 11:08:38 -07003867 /* Start the timer to track if flip comes*/
3868 dev_priv->rps.sw_turbo.timeout = 200*1000; /* in us */
3869
3870 init_timer(&dev_priv->rps.sw_turbo.flip_timer);
3871 dev_priv->rps.sw_turbo.flip_timer.function = flip_active_timeout_handler;
3872 dev_priv->rps.sw_turbo.flip_timer.data = (unsigned long) dev_priv;
3873 dev_priv->rps.sw_turbo.flip_timer.expires =
3874 usecs_to_jiffies(dev_priv->rps.sw_turbo.timeout) + jiffies;
3875 add_timer(&dev_priv->rps.sw_turbo.flip_timer);
3876 INIT_WORK(&dev_priv->rps.sw_turbo.work_max_freq, gen8_set_frequency_RP0);
3877
3878 atomic_set(&dev_priv->rps.sw_turbo.flip_received, true);
3879 } else {
3880 /* NB: Docs say 1s, and 1000000 - which aren't equivalent
3881 * 1 second timeout*/
3882 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, FREQ_1_28_US(1000000));
3883
3884 /* Docs recommend 900MHz, and 300 MHz respectively */
3885 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS,
3886 dev_priv->rps.max_freq_softlimit << 24 |
3887 dev_priv->rps.min_freq_softlimit << 16);
3888
3889 I915_WRITE(GEN6_RP_UP_THRESHOLD,
3890 FREQ_1_28_US(ei_up * threshold_up_pct / 100));
3891 I915_WRITE(GEN6_RP_DOWN_THRESHOLD,
3892 FREQ_1_28_US(ei_down * threshold_down_pct / 100));
3893 I915_WRITE(GEN6_RP_UP_EI,
3894 FREQ_1_28_US(ei_up));
3895 I915_WRITE(GEN6_RP_DOWN_EI,
3896 FREQ_1_28_US(ei_down));
3897
3898 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
3899 }
Ben Widawsky6edee7f2013-11-02 21:07:52 -07003900
3901 /* 5: Enable RPS */
Daisy Sunc76bb612014-08-11 11:08:38 -07003902 rp_ctl_flag = GEN6_RP_MEDIA_TURBO |
3903 GEN6_RP_MEDIA_HW_NORMAL_MODE |
3904 GEN6_RP_MEDIA_IS_GFX |
3905 GEN6_RP_UP_BUSY_AVG |
3906 GEN6_RP_DOWN_IDLE_AVG;
3907 if (!dev_priv->rps.is_bdw_sw_turbo)
3908 rp_ctl_flag |= GEN6_RP_ENABLE;
Ben Widawsky6edee7f2013-11-02 21:07:52 -07003909
Daisy Sunc76bb612014-08-11 11:08:38 -07003910 I915_WRITE(GEN6_RP_CONTROL, rp_ctl_flag);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07003911
Daisy Sunc76bb612014-08-11 11:08:38 -07003912 /* 6: Ring frequency + overclocking
3913 * (our driver does this later */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07003914 gen6_set_rps(dev, (I915_READ(GEN6_GT_PERF_STATUS) & 0xff00) >> 8);
Daisy Sunc76bb612014-08-11 11:08:38 -07003915 if (!dev_priv->rps.is_bdw_sw_turbo)
3916 gen8_enable_rps_interrupts(dev);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07003917
Deepak Sc8d9a592013-11-23 14:55:42 +05303918 gen6_gt_force_wake_put(dev_priv, FORCEWAKE_ALL);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07003919}
3920
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02003921static void gen6_enable_rps(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003922{
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02003923 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01003924 struct intel_engine_cs *ring;
Ben Widawsky2a5913a2014-03-19 18:31:13 -07003925 u32 rp_state_cap;
Ben Widawskyd060c162014-03-19 18:31:08 -07003926 u32 rc6vids, pcu_mbox = 0, rc6_mask = 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003927 u32 gtfifodbg;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003928 int rc6_mode;
Ben Widawsky42c05262012-09-26 10:34:00 -07003929 int i, ret;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003930
Jesse Barnes4fc688c2012-11-02 11:14:01 -07003931 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02003932
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003933 /* Here begins a magic sequence of register writes to enable
3934 * auto-downclocking.
3935 *
3936 * Perhaps there might be some value in exposing these to
3937 * userspace...
3938 */
3939 I915_WRITE(GEN6_RC_STATE, 0);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003940
3941 /* Clear the DBG now so we don't confuse earlier errors */
3942 if ((gtfifodbg = I915_READ(GTFIFODBG))) {
3943 DRM_ERROR("GT fifo had a previous error %x\n", gtfifodbg);
3944 I915_WRITE(GTFIFODBG, gtfifodbg);
3945 }
3946
Deepak Sc8d9a592013-11-23 14:55:42 +05303947 gen6_gt_force_wake_get(dev_priv, FORCEWAKE_ALL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003948
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01003949 rp_state_cap = I915_READ(GEN6_RP_STATE_CAP);
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01003950
Ben Widawsky3280e8b2014-03-31 17:16:42 -07003951 parse_rp_state_cap(dev_priv, rp_state_cap);
Jeff McGeedd0a1aa2014-02-04 11:32:31 -06003952
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003953 /* disable the counters and set deterministic thresholds */
3954 I915_WRITE(GEN6_RC_CONTROL, 0);
3955
3956 I915_WRITE(GEN6_RC1_WAKE_RATE_LIMIT, 1000 << 16);
3957 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16 | 30);
3958 I915_WRITE(GEN6_RC6pp_WAKE_RATE_LIMIT, 30);
3959 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
3960 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
3961
Chris Wilsonb4519512012-05-11 14:29:30 +01003962 for_each_ring(ring, dev_priv, i)
3963 I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003964
3965 I915_WRITE(GEN6_RC_SLEEP, 0);
3966 I915_WRITE(GEN6_RC1e_THRESHOLD, 1000);
Daniel Vetter29c78f62013-11-16 16:04:26 +01003967 if (IS_IVYBRIDGE(dev))
Stéphane Marchesin351aa562013-08-13 11:55:17 -07003968 I915_WRITE(GEN6_RC6_THRESHOLD, 125000);
3969 else
3970 I915_WRITE(GEN6_RC6_THRESHOLD, 50000);
Stéphane Marchesin0920a482013-01-29 19:41:59 -08003971 I915_WRITE(GEN6_RC6p_THRESHOLD, 150000);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003972 I915_WRITE(GEN6_RC6pp_THRESHOLD, 64000); /* unused */
3973
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03003974 /* Check if we are enabling RC6 */
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003975 rc6_mode = intel_enable_rc6(dev_priv->dev);
3976 if (rc6_mode & INTEL_RC6_ENABLE)
3977 rc6_mask |= GEN6_RC_CTL_RC6_ENABLE;
3978
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03003979 /* We don't use those on Haswell */
3980 if (!IS_HASWELL(dev)) {
3981 if (rc6_mode & INTEL_RC6p_ENABLE)
3982 rc6_mask |= GEN6_RC_CTL_RC6p_ENABLE;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003983
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03003984 if (rc6_mode & INTEL_RC6pp_ENABLE)
3985 rc6_mask |= GEN6_RC_CTL_RC6pp_ENABLE;
3986 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003987
Ben Widawskydc39fff2013-10-18 12:32:07 -07003988 intel_print_rc6_info(dev, rc6_mask);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003989
3990 I915_WRITE(GEN6_RC_CONTROL,
3991 rc6_mask |
3992 GEN6_RC_CTL_EI_MODE(1) |
3993 GEN6_RC_CTL_HW_ENABLE);
3994
Chris Wilsondd75fdc2013-09-25 17:34:57 +01003995 /* Power down if completely idle for over 50ms */
3996 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 50000);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003997 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003998
Ben Widawsky42c05262012-09-26 10:34:00 -07003999 ret = sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_MIN_FREQ_TABLE, 0);
Ben Widawskyd060c162014-03-19 18:31:08 -07004000 if (ret)
Ben Widawsky42c05262012-09-26 10:34:00 -07004001 DRM_DEBUG_DRIVER("Failed to set the min frequency\n");
Ben Widawskyd060c162014-03-19 18:31:08 -07004002
4003 ret = sandybridge_pcode_read(dev_priv, GEN6_READ_OC_PARAMS, &pcu_mbox);
4004 if (!ret && (pcu_mbox & (1<<31))) { /* OC supported */
4005 DRM_DEBUG_DRIVER("Overclocking supported. Max: %dMHz, Overclock max: %dMHz\n",
Ben Widawskyb39fb292014-03-19 18:31:11 -07004006 (dev_priv->rps.max_freq_softlimit & 0xff) * 50,
Ben Widawskyd060c162014-03-19 18:31:08 -07004007 (pcu_mbox & 0xff) * 50);
Ben Widawskyb39fb292014-03-19 18:31:11 -07004008 dev_priv->rps.max_freq = pcu_mbox & 0xff;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004009 }
4010
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004011 dev_priv->rps.power = HIGH_POWER; /* force a reset */
Ben Widawskyb39fb292014-03-19 18:31:11 -07004012 gen6_set_rps(dev_priv->dev, dev_priv->rps.min_freq_softlimit);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004013
Daniel Vetter44fc7d52013-07-12 22:43:27 +02004014 gen6_enable_rps_interrupts(dev);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004015
Ben Widawsky31643d52012-09-26 10:34:01 -07004016 rc6vids = 0;
4017 ret = sandybridge_pcode_read(dev_priv, GEN6_PCODE_READ_RC6VIDS, &rc6vids);
4018 if (IS_GEN6(dev) && ret) {
4019 DRM_DEBUG_DRIVER("Couldn't check for BIOS workaround\n");
4020 } else if (IS_GEN6(dev) && (GEN6_DECODE_RC6_VID(rc6vids & 0xff) < 450)) {
4021 DRM_DEBUG_DRIVER("You should update your BIOS. Correcting minimum rc6 voltage (%dmV->%dmV)\n",
4022 GEN6_DECODE_RC6_VID(rc6vids & 0xff), 450);
4023 rc6vids &= 0xffff00;
4024 rc6vids |= GEN6_ENCODE_RC6_VID(450);
4025 ret = sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_RC6VIDS, rc6vids);
4026 if (ret)
4027 DRM_ERROR("Couldn't fix incorrect rc6 voltage\n");
4028 }
4029
Deepak Sc8d9a592013-11-23 14:55:42 +05304030 gen6_gt_force_wake_put(dev_priv, FORCEWAKE_ALL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004031}
4032
Imre Deakc2bc2fc2014-04-18 16:16:23 +03004033static void __gen6_update_ring_freq(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004034{
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02004035 struct drm_i915_private *dev_priv = dev->dev_private;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004036 int min_freq = 15;
Chris Wilson3ebecd02013-04-12 19:10:13 +01004037 unsigned int gpu_freq;
4038 unsigned int max_ia_freq, min_ring_freq;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004039 int scaling_factor = 180;
Ben Widawskyeda79642013-10-07 17:15:48 -03004040 struct cpufreq_policy *policy;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004041
Jesse Barnes4fc688c2012-11-02 11:14:01 -07004042 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02004043
Ben Widawskyeda79642013-10-07 17:15:48 -03004044 policy = cpufreq_cpu_get(0);
4045 if (policy) {
4046 max_ia_freq = policy->cpuinfo.max_freq;
4047 cpufreq_cpu_put(policy);
4048 } else {
4049 /*
4050 * Default to measured freq if none found, PCU will ensure we
4051 * don't go over
4052 */
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004053 max_ia_freq = tsc_khz;
Ben Widawskyeda79642013-10-07 17:15:48 -03004054 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004055
4056 /* Convert from kHz to MHz */
4057 max_ia_freq /= 1000;
4058
Ben Widawsky153b4b952013-10-22 22:05:09 -07004059 min_ring_freq = I915_READ(DCLK) & 0xf;
Ben Widawskyf6aca452013-10-02 09:25:02 -07004060 /* convert DDR frequency from units of 266.6MHz to bandwidth */
4061 min_ring_freq = mult_frac(min_ring_freq, 8, 3);
Chris Wilson3ebecd02013-04-12 19:10:13 +01004062
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004063 /*
4064 * For each potential GPU frequency, load a ring frequency we'd like
4065 * to use for memory access. We do this by specifying the IA frequency
4066 * the PCU should use as a reference to determine the ring frequency.
4067 */
Ben Widawskyb39fb292014-03-19 18:31:11 -07004068 for (gpu_freq = dev_priv->rps.max_freq_softlimit; gpu_freq >= dev_priv->rps.min_freq_softlimit;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004069 gpu_freq--) {
Ben Widawskyb39fb292014-03-19 18:31:11 -07004070 int diff = dev_priv->rps.max_freq_softlimit - gpu_freq;
Chris Wilson3ebecd02013-04-12 19:10:13 +01004071 unsigned int ia_freq = 0, ring_freq = 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004072
Ben Widawsky46c764d2013-11-02 21:07:49 -07004073 if (INTEL_INFO(dev)->gen >= 8) {
4074 /* max(2 * GT, DDR). NB: GT is 50MHz units */
4075 ring_freq = max(min_ring_freq, gpu_freq);
4076 } else if (IS_HASWELL(dev)) {
Ben Widawskyf6aca452013-10-02 09:25:02 -07004077 ring_freq = mult_frac(gpu_freq, 5, 4);
Chris Wilson3ebecd02013-04-12 19:10:13 +01004078 ring_freq = max(min_ring_freq, ring_freq);
4079 /* leave ia_freq as the default, chosen by cpufreq */
4080 } else {
4081 /* On older processors, there is no separate ring
4082 * clock domain, so in order to boost the bandwidth
4083 * of the ring, we need to upclock the CPU (ia_freq).
4084 *
4085 * For GPU frequencies less than 750MHz,
4086 * just use the lowest ring freq.
4087 */
4088 if (gpu_freq < min_freq)
4089 ia_freq = 800;
4090 else
4091 ia_freq = max_ia_freq - ((diff * scaling_factor) / 2);
4092 ia_freq = DIV_ROUND_CLOSEST(ia_freq, 100);
4093 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004094
Ben Widawsky42c05262012-09-26 10:34:00 -07004095 sandybridge_pcode_write(dev_priv,
4096 GEN6_PCODE_WRITE_MIN_FREQ_TABLE,
Chris Wilson3ebecd02013-04-12 19:10:13 +01004097 ia_freq << GEN6_PCODE_FREQ_IA_RATIO_SHIFT |
4098 ring_freq << GEN6_PCODE_FREQ_RING_RATIO_SHIFT |
4099 gpu_freq);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004100 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004101}
4102
Imre Deakc2bc2fc2014-04-18 16:16:23 +03004103void gen6_update_ring_freq(struct drm_device *dev)
4104{
4105 struct drm_i915_private *dev_priv = dev->dev_private;
4106
4107 if (INTEL_INFO(dev)->gen < 6 || IS_VALLEYVIEW(dev))
4108 return;
4109
4110 mutex_lock(&dev_priv->rps.hw_lock);
4111 __gen6_update_ring_freq(dev);
4112 mutex_unlock(&dev_priv->rps.hw_lock);
4113}
4114
Ville Syrjälä03af2042014-06-28 02:03:53 +03004115static int cherryview_rps_max_freq(struct drm_i915_private *dev_priv)
Deepak S2b6b3a02014-05-27 15:59:30 +05304116{
4117 u32 val, rp0;
4118
4119 val = vlv_punit_read(dev_priv, PUNIT_GPU_STATUS_REG);
4120 rp0 = (val >> PUNIT_GPU_STATUS_MAX_FREQ_SHIFT) & PUNIT_GPU_STATUS_MAX_FREQ_MASK;
4121
4122 return rp0;
4123}
4124
4125static int cherryview_rps_rpe_freq(struct drm_i915_private *dev_priv)
4126{
4127 u32 val, rpe;
4128
4129 val = vlv_punit_read(dev_priv, PUNIT_GPU_DUTYCYCLE_REG);
4130 rpe = (val >> PUNIT_GPU_DUTYCYCLE_RPE_FREQ_SHIFT) & PUNIT_GPU_DUTYCYCLE_RPE_FREQ_MASK;
4131
4132 return rpe;
4133}
4134
Deepak S7707df42014-07-12 18:46:14 +05304135static int cherryview_rps_guar_freq(struct drm_i915_private *dev_priv)
4136{
4137 u32 val, rp1;
4138
4139 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
4140 rp1 = (val >> PUNIT_GPU_STATUS_MAX_FREQ_SHIFT) & PUNIT_GPU_STATUS_MAX_FREQ_MASK;
4141
4142 return rp1;
4143}
4144
Ville Syrjälä03af2042014-06-28 02:03:53 +03004145static int cherryview_rps_min_freq(struct drm_i915_private *dev_priv)
Deepak S2b6b3a02014-05-27 15:59:30 +05304146{
4147 u32 val, rpn;
4148
4149 val = vlv_punit_read(dev_priv, PUNIT_GPU_STATUS_REG);
4150 rpn = (val >> PUNIT_GPU_STATIS_GFX_MIN_FREQ_SHIFT) & PUNIT_GPU_STATUS_GFX_MIN_FREQ_MASK;
4151 return rpn;
4152}
4153
Deepak Sf8f2b002014-07-10 13:16:21 +05304154static int valleyview_rps_guar_freq(struct drm_i915_private *dev_priv)
4155{
4156 u32 val, rp1;
4157
4158 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FREQ_FUSE);
4159
4160 rp1 = (val & FB_GFX_FGUARANTEED_FREQ_FUSE_MASK) >> FB_GFX_FGUARANTEED_FREQ_FUSE_SHIFT;
4161
4162 return rp1;
4163}
4164
Ville Syrjälä03af2042014-06-28 02:03:53 +03004165static int valleyview_rps_max_freq(struct drm_i915_private *dev_priv)
Jesse Barnes0a073b82013-04-17 15:54:58 -07004166{
4167 u32 val, rp0;
4168
Jani Nikula64936252013-05-22 15:36:20 +03004169 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FREQ_FUSE);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004170
4171 rp0 = (val & FB_GFX_MAX_FREQ_FUSE_MASK) >> FB_GFX_MAX_FREQ_FUSE_SHIFT;
4172 /* Clamp to max */
4173 rp0 = min_t(u32, rp0, 0xea);
4174
4175 return rp0;
4176}
4177
4178static int valleyview_rps_rpe_freq(struct drm_i915_private *dev_priv)
4179{
4180 u32 val, rpe;
4181
Jani Nikula64936252013-05-22 15:36:20 +03004182 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FMAX_FUSE_LO);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004183 rpe = (val & FB_FMAX_VMIN_FREQ_LO_MASK) >> FB_FMAX_VMIN_FREQ_LO_SHIFT;
Jani Nikula64936252013-05-22 15:36:20 +03004184 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FMAX_FUSE_HI);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004185 rpe |= (val & FB_FMAX_VMIN_FREQ_HI_MASK) << 5;
4186
4187 return rpe;
4188}
4189
Ville Syrjälä03af2042014-06-28 02:03:53 +03004190static int valleyview_rps_min_freq(struct drm_i915_private *dev_priv)
Jesse Barnes0a073b82013-04-17 15:54:58 -07004191{
Jani Nikula64936252013-05-22 15:36:20 +03004192 return vlv_punit_read(dev_priv, PUNIT_REG_GPU_LFM) & 0xff;
Jesse Barnes0a073b82013-04-17 15:54:58 -07004193}
4194
Imre Deakae484342014-03-31 15:10:44 +03004195/* Check that the pctx buffer wasn't move under us. */
4196static void valleyview_check_pctx(struct drm_i915_private *dev_priv)
4197{
4198 unsigned long pctx_addr = I915_READ(VLV_PCBR) & ~4095;
4199
4200 WARN_ON(pctx_addr != dev_priv->mm.stolen_base +
4201 dev_priv->vlv_pctx->stolen->start);
4202}
4203
Deepak S38807742014-05-23 21:00:15 +05304204
4205/* Check that the pcbr address is not empty. */
4206static void cherryview_check_pctx(struct drm_i915_private *dev_priv)
4207{
4208 unsigned long pctx_addr = I915_READ(VLV_PCBR) & ~4095;
4209
4210 WARN_ON((pctx_addr >> VLV_PCBR_ADDR_SHIFT) == 0);
4211}
4212
4213static void cherryview_setup_pctx(struct drm_device *dev)
4214{
4215 struct drm_i915_private *dev_priv = dev->dev_private;
4216 unsigned long pctx_paddr, paddr;
4217 struct i915_gtt *gtt = &dev_priv->gtt;
4218 u32 pcbr;
4219 int pctx_size = 32*1024;
4220
4221 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
4222
4223 pcbr = I915_READ(VLV_PCBR);
4224 if ((pcbr >> VLV_PCBR_ADDR_SHIFT) == 0) {
4225 paddr = (dev_priv->mm.stolen_base +
4226 (gtt->stolen_size - pctx_size));
4227
4228 pctx_paddr = (paddr & (~4095));
4229 I915_WRITE(VLV_PCBR, pctx_paddr);
4230 }
4231}
4232
Jesse Barnesc9cddff2013-05-08 10:45:13 -07004233static void valleyview_setup_pctx(struct drm_device *dev)
4234{
4235 struct drm_i915_private *dev_priv = dev->dev_private;
4236 struct drm_i915_gem_object *pctx;
4237 unsigned long pctx_paddr;
4238 u32 pcbr;
4239 int pctx_size = 24*1024;
4240
Imre Deak17b0c1f2014-02-11 21:39:06 +02004241 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
4242
Jesse Barnesc9cddff2013-05-08 10:45:13 -07004243 pcbr = I915_READ(VLV_PCBR);
4244 if (pcbr) {
4245 /* BIOS set it up already, grab the pre-alloc'd space */
4246 int pcbr_offset;
4247
4248 pcbr_offset = (pcbr & (~4095)) - dev_priv->mm.stolen_base;
4249 pctx = i915_gem_object_create_stolen_for_preallocated(dev_priv->dev,
4250 pcbr_offset,
Daniel Vetter190d6cd2013-07-04 13:06:28 +02004251 I915_GTT_OFFSET_NONE,
Jesse Barnesc9cddff2013-05-08 10:45:13 -07004252 pctx_size);
4253 goto out;
4254 }
4255
4256 /*
4257 * From the Gunit register HAS:
4258 * The Gfx driver is expected to program this register and ensure
4259 * proper allocation within Gfx stolen memory. For example, this
4260 * register should be programmed such than the PCBR range does not
4261 * overlap with other ranges, such as the frame buffer, protected
4262 * memory, or any other relevant ranges.
4263 */
4264 pctx = i915_gem_object_create_stolen(dev, pctx_size);
4265 if (!pctx) {
4266 DRM_DEBUG("not enough stolen space for PCTX, disabling\n");
4267 return;
4268 }
4269
4270 pctx_paddr = dev_priv->mm.stolen_base + pctx->stolen->start;
4271 I915_WRITE(VLV_PCBR, pctx_paddr);
4272
4273out:
4274 dev_priv->vlv_pctx = pctx;
4275}
4276
Imre Deakae484342014-03-31 15:10:44 +03004277static void valleyview_cleanup_pctx(struct drm_device *dev)
4278{
4279 struct drm_i915_private *dev_priv = dev->dev_private;
4280
4281 if (WARN_ON(!dev_priv->vlv_pctx))
4282 return;
4283
4284 drm_gem_object_unreference(&dev_priv->vlv_pctx->base);
4285 dev_priv->vlv_pctx = NULL;
4286}
4287
Imre Deak4e805192014-04-14 20:24:41 +03004288static void valleyview_init_gt_powersave(struct drm_device *dev)
4289{
4290 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03004291 u32 val;
Imre Deak4e805192014-04-14 20:24:41 +03004292
4293 valleyview_setup_pctx(dev);
4294
4295 mutex_lock(&dev_priv->rps.hw_lock);
4296
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03004297 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
4298 switch ((val >> 6) & 3) {
4299 case 0:
4300 case 1:
4301 dev_priv->mem_freq = 800;
4302 break;
4303 case 2:
4304 dev_priv->mem_freq = 1066;
4305 break;
4306 case 3:
4307 dev_priv->mem_freq = 1333;
4308 break;
4309 }
4310 DRM_DEBUG_DRIVER("DDR speed: %d MHz", dev_priv->mem_freq);
4311
Imre Deak4e805192014-04-14 20:24:41 +03004312 dev_priv->rps.max_freq = valleyview_rps_max_freq(dev_priv);
4313 dev_priv->rps.rp0_freq = dev_priv->rps.max_freq;
4314 DRM_DEBUG_DRIVER("max GPU freq: %d MHz (%u)\n",
4315 vlv_gpu_freq(dev_priv, dev_priv->rps.max_freq),
4316 dev_priv->rps.max_freq);
4317
4318 dev_priv->rps.efficient_freq = valleyview_rps_rpe_freq(dev_priv);
4319 DRM_DEBUG_DRIVER("RPe GPU freq: %d MHz (%u)\n",
4320 vlv_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
4321 dev_priv->rps.efficient_freq);
4322
Deepak Sf8f2b002014-07-10 13:16:21 +05304323 dev_priv->rps.rp1_freq = valleyview_rps_guar_freq(dev_priv);
4324 DRM_DEBUG_DRIVER("RP1(Guar Freq) GPU freq: %d MHz (%u)\n",
4325 vlv_gpu_freq(dev_priv, dev_priv->rps.rp1_freq),
4326 dev_priv->rps.rp1_freq);
4327
Imre Deak4e805192014-04-14 20:24:41 +03004328 dev_priv->rps.min_freq = valleyview_rps_min_freq(dev_priv);
4329 DRM_DEBUG_DRIVER("min GPU freq: %d MHz (%u)\n",
4330 vlv_gpu_freq(dev_priv, dev_priv->rps.min_freq),
4331 dev_priv->rps.min_freq);
4332
4333 /* Preserve min/max settings in case of re-init */
4334 if (dev_priv->rps.max_freq_softlimit == 0)
4335 dev_priv->rps.max_freq_softlimit = dev_priv->rps.max_freq;
4336
4337 if (dev_priv->rps.min_freq_softlimit == 0)
4338 dev_priv->rps.min_freq_softlimit = dev_priv->rps.min_freq;
4339
4340 mutex_unlock(&dev_priv->rps.hw_lock);
4341}
4342
Deepak S38807742014-05-23 21:00:15 +05304343static void cherryview_init_gt_powersave(struct drm_device *dev)
4344{
Deepak S2b6b3a02014-05-27 15:59:30 +05304345 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03004346 u32 val;
Deepak S2b6b3a02014-05-27 15:59:30 +05304347
Deepak S38807742014-05-23 21:00:15 +05304348 cherryview_setup_pctx(dev);
Deepak S2b6b3a02014-05-27 15:59:30 +05304349
4350 mutex_lock(&dev_priv->rps.hw_lock);
4351
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03004352 val = vlv_punit_read(dev_priv, CCK_FUSE_REG);
4353 switch ((val >> 2) & 0x7) {
4354 case 0:
4355 case 1:
4356 dev_priv->rps.cz_freq = 200;
4357 dev_priv->mem_freq = 1600;
4358 break;
4359 case 2:
4360 dev_priv->rps.cz_freq = 267;
4361 dev_priv->mem_freq = 1600;
4362 break;
4363 case 3:
4364 dev_priv->rps.cz_freq = 333;
4365 dev_priv->mem_freq = 2000;
4366 break;
4367 case 4:
4368 dev_priv->rps.cz_freq = 320;
4369 dev_priv->mem_freq = 1600;
4370 break;
4371 case 5:
4372 dev_priv->rps.cz_freq = 400;
4373 dev_priv->mem_freq = 1600;
4374 break;
4375 }
4376 DRM_DEBUG_DRIVER("DDR speed: %d MHz", dev_priv->mem_freq);
4377
Deepak S2b6b3a02014-05-27 15:59:30 +05304378 dev_priv->rps.max_freq = cherryview_rps_max_freq(dev_priv);
4379 dev_priv->rps.rp0_freq = dev_priv->rps.max_freq;
4380 DRM_DEBUG_DRIVER("max GPU freq: %d MHz (%u)\n",
4381 vlv_gpu_freq(dev_priv, dev_priv->rps.max_freq),
4382 dev_priv->rps.max_freq);
4383
4384 dev_priv->rps.efficient_freq = cherryview_rps_rpe_freq(dev_priv);
4385 DRM_DEBUG_DRIVER("RPe GPU freq: %d MHz (%u)\n",
4386 vlv_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
4387 dev_priv->rps.efficient_freq);
4388
Deepak S7707df42014-07-12 18:46:14 +05304389 dev_priv->rps.rp1_freq = cherryview_rps_guar_freq(dev_priv);
4390 DRM_DEBUG_DRIVER("RP1(Guar) GPU freq: %d MHz (%u)\n",
4391 vlv_gpu_freq(dev_priv, dev_priv->rps.rp1_freq),
4392 dev_priv->rps.rp1_freq);
4393
Deepak S2b6b3a02014-05-27 15:59:30 +05304394 dev_priv->rps.min_freq = cherryview_rps_min_freq(dev_priv);
4395 DRM_DEBUG_DRIVER("min GPU freq: %d MHz (%u)\n",
4396 vlv_gpu_freq(dev_priv, dev_priv->rps.min_freq),
4397 dev_priv->rps.min_freq);
4398
Ville Syrjälä1c147622014-08-18 14:42:43 +03004399 WARN_ONCE((dev_priv->rps.max_freq |
4400 dev_priv->rps.efficient_freq |
4401 dev_priv->rps.rp1_freq |
4402 dev_priv->rps.min_freq) & 1,
4403 "Odd GPU freq values\n");
4404
Deepak S2b6b3a02014-05-27 15:59:30 +05304405 /* Preserve min/max settings in case of re-init */
4406 if (dev_priv->rps.max_freq_softlimit == 0)
4407 dev_priv->rps.max_freq_softlimit = dev_priv->rps.max_freq;
4408
4409 if (dev_priv->rps.min_freq_softlimit == 0)
4410 dev_priv->rps.min_freq_softlimit = dev_priv->rps.min_freq;
4411
4412 mutex_unlock(&dev_priv->rps.hw_lock);
Deepak S38807742014-05-23 21:00:15 +05304413}
4414
Imre Deak4e805192014-04-14 20:24:41 +03004415static void valleyview_cleanup_gt_powersave(struct drm_device *dev)
4416{
4417 valleyview_cleanup_pctx(dev);
4418}
4419
Deepak S38807742014-05-23 21:00:15 +05304420static void cherryview_enable_rps(struct drm_device *dev)
4421{
4422 struct drm_i915_private *dev_priv = dev->dev_private;
4423 struct intel_engine_cs *ring;
Deepak S2b6b3a02014-05-27 15:59:30 +05304424 u32 gtfifodbg, val, rc6_mode = 0, pcbr;
Deepak S38807742014-05-23 21:00:15 +05304425 int i;
4426
4427 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
4428
4429 gtfifodbg = I915_READ(GTFIFODBG);
4430 if (gtfifodbg) {
4431 DRM_DEBUG_DRIVER("GT fifo had a previous error %x\n",
4432 gtfifodbg);
4433 I915_WRITE(GTFIFODBG, gtfifodbg);
4434 }
4435
4436 cherryview_check_pctx(dev_priv);
4437
4438 /* 1a & 1b: Get forcewake during program sequence. Although the driver
4439 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
4440 gen6_gt_force_wake_get(dev_priv, FORCEWAKE_ALL);
4441
4442 /* 2a: Program RC6 thresholds.*/
4443 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16);
4444 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
4445 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
4446
4447 for_each_ring(ring, dev_priv, i)
4448 I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
4449 I915_WRITE(GEN6_RC_SLEEP, 0);
4450
4451 I915_WRITE(GEN6_RC6_THRESHOLD, 50000); /* 50/125ms per EI */
4452
4453 /* allows RC6 residency counter to work */
4454 I915_WRITE(VLV_COUNTER_CONTROL,
4455 _MASKED_BIT_ENABLE(VLV_COUNT_RANGE_HIGH |
4456 VLV_MEDIA_RC6_COUNT_EN |
4457 VLV_RENDER_RC6_COUNT_EN));
4458
4459 /* For now we assume BIOS is allocating and populating the PCBR */
4460 pcbr = I915_READ(VLV_PCBR);
4461
4462 DRM_DEBUG_DRIVER("PCBR offset : 0x%x\n", pcbr);
4463
4464 /* 3: Enable RC6 */
4465 if ((intel_enable_rc6(dev) & INTEL_RC6_ENABLE) &&
4466 (pcbr >> VLV_PCBR_ADDR_SHIFT))
4467 rc6_mode = GEN6_RC_CTL_EI_MODE(1);
4468
4469 I915_WRITE(GEN6_RC_CONTROL, rc6_mode);
4470
Deepak S2b6b3a02014-05-27 15:59:30 +05304471 /* 4 Program defaults and thresholds for RPS*/
4472 I915_WRITE(GEN6_RP_UP_THRESHOLD, 59400);
4473 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 245000);
4474 I915_WRITE(GEN6_RP_UP_EI, 66000);
4475 I915_WRITE(GEN6_RP_DOWN_EI, 350000);
4476
4477 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
4478
Tom O'Rourke7405f422014-06-10 16:26:34 -07004479 /* WaDisablePwrmtrEvent:chv (pre-production hw) */
4480 I915_WRITE(0xA80C, I915_READ(0xA80C) & 0x00ffffff);
4481 I915_WRITE(0xA810, I915_READ(0xA810) & 0xffffff00);
4482
Deepak S2b6b3a02014-05-27 15:59:30 +05304483 /* 5: Enable RPS */
4484 I915_WRITE(GEN6_RP_CONTROL,
4485 GEN6_RP_MEDIA_HW_NORMAL_MODE |
Tom O'Rourke7405f422014-06-10 16:26:34 -07004486 GEN6_RP_MEDIA_IS_GFX | /* WaSetMaskForGfxBusyness:chv (pre-production hw ?) */
Deepak S2b6b3a02014-05-27 15:59:30 +05304487 GEN6_RP_ENABLE |
4488 GEN6_RP_UP_BUSY_AVG |
4489 GEN6_RP_DOWN_IDLE_AVG);
4490
4491 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
4492
4493 DRM_DEBUG_DRIVER("GPLL enabled? %s\n", val & 0x10 ? "yes" : "no");
4494 DRM_DEBUG_DRIVER("GPU status: 0x%08x\n", val);
4495
4496 dev_priv->rps.cur_freq = (val >> 8) & 0xff;
4497 DRM_DEBUG_DRIVER("current GPU freq: %d MHz (%u)\n",
4498 vlv_gpu_freq(dev_priv, dev_priv->rps.cur_freq),
4499 dev_priv->rps.cur_freq);
4500
4501 DRM_DEBUG_DRIVER("setting GPU freq to %d MHz (%u)\n",
4502 vlv_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
4503 dev_priv->rps.efficient_freq);
4504
4505 valleyview_set_rps(dev_priv->dev, dev_priv->rps.efficient_freq);
4506
Deepak S3497a562014-07-10 13:16:26 +05304507 gen8_enable_rps_interrupts(dev);
4508
Deepak S38807742014-05-23 21:00:15 +05304509 gen6_gt_force_wake_put(dev_priv, FORCEWAKE_ALL);
4510}
4511
Jesse Barnes0a073b82013-04-17 15:54:58 -07004512static void valleyview_enable_rps(struct drm_device *dev)
4513{
4514 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01004515 struct intel_engine_cs *ring;
Ben Widawsky2a5913a2014-03-19 18:31:13 -07004516 u32 gtfifodbg, val, rc6_mode = 0;
Jesse Barnes0a073b82013-04-17 15:54:58 -07004517 int i;
4518
4519 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
4520
Imre Deakae484342014-03-31 15:10:44 +03004521 valleyview_check_pctx(dev_priv);
4522
Jesse Barnes0a073b82013-04-17 15:54:58 -07004523 if ((gtfifodbg = I915_READ(GTFIFODBG))) {
Jesse Barnesf7d85c12013-09-27 10:40:54 -07004524 DRM_DEBUG_DRIVER("GT fifo had a previous error %x\n",
4525 gtfifodbg);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004526 I915_WRITE(GTFIFODBG, gtfifodbg);
4527 }
4528
Deepak Sc8d9a592013-11-23 14:55:42 +05304529 /* If VLV, Forcewake all wells, else re-direct to regular path */
4530 gen6_gt_force_wake_get(dev_priv, FORCEWAKE_ALL);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004531
4532 I915_WRITE(GEN6_RP_UP_THRESHOLD, 59400);
4533 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 245000);
4534 I915_WRITE(GEN6_RP_UP_EI, 66000);
4535 I915_WRITE(GEN6_RP_DOWN_EI, 350000);
4536
4537 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
Deepak S31685c22014-07-03 17:33:01 -04004538 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 0xf4240);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004539
4540 I915_WRITE(GEN6_RP_CONTROL,
4541 GEN6_RP_MEDIA_TURBO |
4542 GEN6_RP_MEDIA_HW_NORMAL_MODE |
4543 GEN6_RP_MEDIA_IS_GFX |
4544 GEN6_RP_ENABLE |
4545 GEN6_RP_UP_BUSY_AVG |
4546 GEN6_RP_DOWN_IDLE_CONT);
4547
4548 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 0x00280000);
4549 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
4550 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
4551
4552 for_each_ring(ring, dev_priv, i)
4553 I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
4554
Jesse Barnes2f0aa3042013-11-15 09:32:11 -08004555 I915_WRITE(GEN6_RC6_THRESHOLD, 0x557);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004556
4557 /* allows RC6 residency counter to work */
Jesse Barnes49798eb2013-09-26 17:55:57 -07004558 I915_WRITE(VLV_COUNTER_CONTROL,
Deepak S31685c22014-07-03 17:33:01 -04004559 _MASKED_BIT_ENABLE(VLV_MEDIA_RC0_COUNT_EN |
4560 VLV_RENDER_RC0_COUNT_EN |
Jesse Barnes49798eb2013-09-26 17:55:57 -07004561 VLV_MEDIA_RC6_COUNT_EN |
4562 VLV_RENDER_RC6_COUNT_EN));
Deepak S31685c22014-07-03 17:33:01 -04004563
Jesse Barnesa2b23fe2013-09-19 09:33:13 -07004564 if (intel_enable_rc6(dev) & INTEL_RC6_ENABLE)
Jesse Barnes6b88f292013-11-15 09:32:12 -08004565 rc6_mode = GEN7_RC_CTL_TO_MODE | VLV_RC_CTL_CTX_RST_PARALLEL;
Ben Widawskydc39fff2013-10-18 12:32:07 -07004566
4567 intel_print_rc6_info(dev, rc6_mode);
4568
Jesse Barnesa2b23fe2013-09-19 09:33:13 -07004569 I915_WRITE(GEN6_RC_CONTROL, rc6_mode);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004570
Jani Nikula64936252013-05-22 15:36:20 +03004571 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004572
4573 DRM_DEBUG_DRIVER("GPLL enabled? %s\n", val & 0x10 ? "yes" : "no");
4574 DRM_DEBUG_DRIVER("GPU status: 0x%08x\n", val);
4575
Ben Widawskyb39fb292014-03-19 18:31:11 -07004576 dev_priv->rps.cur_freq = (val >> 8) & 0xff;
Ville Syrjälä73008b92013-06-25 19:21:01 +03004577 DRM_DEBUG_DRIVER("current GPU freq: %d MHz (%u)\n",
Ben Widawskyb39fb292014-03-19 18:31:11 -07004578 vlv_gpu_freq(dev_priv, dev_priv->rps.cur_freq),
4579 dev_priv->rps.cur_freq);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004580
Ville Syrjälä73008b92013-06-25 19:21:01 +03004581 DRM_DEBUG_DRIVER("setting GPU freq to %d MHz (%u)\n",
Ben Widawskyb39fb292014-03-19 18:31:11 -07004582 vlv_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
4583 dev_priv->rps.efficient_freq);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004584
Ben Widawskyb39fb292014-03-19 18:31:11 -07004585 valleyview_set_rps(dev_priv->dev, dev_priv->rps.efficient_freq);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004586
Daniel Vetter44fc7d52013-07-12 22:43:27 +02004587 gen6_enable_rps_interrupts(dev);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004588
Deepak Sc8d9a592013-11-23 14:55:42 +05304589 gen6_gt_force_wake_put(dev_priv, FORCEWAKE_ALL);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004590}
4591
Daniel Vetter930ebb42012-06-29 23:32:16 +02004592void ironlake_teardown_rc6(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004593{
4594 struct drm_i915_private *dev_priv = dev->dev_private;
4595
Daniel Vetter3e373942012-11-02 19:55:04 +01004596 if (dev_priv->ips.renderctx) {
Ben Widawskyd7f46fc2013-12-06 14:10:55 -08004597 i915_gem_object_ggtt_unpin(dev_priv->ips.renderctx);
Daniel Vetter3e373942012-11-02 19:55:04 +01004598 drm_gem_object_unreference(&dev_priv->ips.renderctx->base);
4599 dev_priv->ips.renderctx = NULL;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004600 }
4601
Daniel Vetter3e373942012-11-02 19:55:04 +01004602 if (dev_priv->ips.pwrctx) {
Ben Widawskyd7f46fc2013-12-06 14:10:55 -08004603 i915_gem_object_ggtt_unpin(dev_priv->ips.pwrctx);
Daniel Vetter3e373942012-11-02 19:55:04 +01004604 drm_gem_object_unreference(&dev_priv->ips.pwrctx->base);
4605 dev_priv->ips.pwrctx = NULL;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004606 }
4607}
4608
Daniel Vetter930ebb42012-06-29 23:32:16 +02004609static void ironlake_disable_rc6(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004610{
4611 struct drm_i915_private *dev_priv = dev->dev_private;
4612
4613 if (I915_READ(PWRCTXA)) {
4614 /* Wake the GPU, prevent RC6, then restore RSTDBYCTL */
4615 I915_WRITE(RSTDBYCTL, I915_READ(RSTDBYCTL) | RCX_SW_EXIT);
4616 wait_for(((I915_READ(RSTDBYCTL) & RSX_STATUS_MASK) == RSX_STATUS_ON),
4617 50);
4618
4619 I915_WRITE(PWRCTXA, 0);
4620 POSTING_READ(PWRCTXA);
4621
4622 I915_WRITE(RSTDBYCTL, I915_READ(RSTDBYCTL) & ~RCX_SW_EXIT);
4623 POSTING_READ(RSTDBYCTL);
4624 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004625}
4626
4627static int ironlake_setup_rc6(struct drm_device *dev)
4628{
4629 struct drm_i915_private *dev_priv = dev->dev_private;
4630
Daniel Vetter3e373942012-11-02 19:55:04 +01004631 if (dev_priv->ips.renderctx == NULL)
4632 dev_priv->ips.renderctx = intel_alloc_context_page(dev);
4633 if (!dev_priv->ips.renderctx)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004634 return -ENOMEM;
4635
Daniel Vetter3e373942012-11-02 19:55:04 +01004636 if (dev_priv->ips.pwrctx == NULL)
4637 dev_priv->ips.pwrctx = intel_alloc_context_page(dev);
4638 if (!dev_priv->ips.pwrctx) {
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004639 ironlake_teardown_rc6(dev);
4640 return -ENOMEM;
4641 }
4642
4643 return 0;
4644}
4645
Daniel Vetter930ebb42012-06-29 23:32:16 +02004646static void ironlake_enable_rc6(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004647{
4648 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01004649 struct intel_engine_cs *ring = &dev_priv->ring[RCS];
Chris Wilson3e960502012-11-27 16:22:54 +00004650 bool was_interruptible;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004651 int ret;
4652
4653 /* rc6 disabled by default due to repeated reports of hanging during
4654 * boot and resume.
4655 */
4656 if (!intel_enable_rc6(dev))
4657 return;
4658
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02004659 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
4660
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004661 ret = ironlake_setup_rc6(dev);
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02004662 if (ret)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004663 return;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004664
Chris Wilson3e960502012-11-27 16:22:54 +00004665 was_interruptible = dev_priv->mm.interruptible;
4666 dev_priv->mm.interruptible = false;
4667
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004668 /*
4669 * GPU can automatically power down the render unit if given a page
4670 * to save state.
4671 */
Daniel Vetter6d90c952012-04-26 23:28:05 +02004672 ret = intel_ring_begin(ring, 6);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004673 if (ret) {
4674 ironlake_teardown_rc6(dev);
Chris Wilson3e960502012-11-27 16:22:54 +00004675 dev_priv->mm.interruptible = was_interruptible;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004676 return;
4677 }
4678
Daniel Vetter6d90c952012-04-26 23:28:05 +02004679 intel_ring_emit(ring, MI_SUSPEND_FLUSH | MI_SUSPEND_FLUSH_EN);
4680 intel_ring_emit(ring, MI_SET_CONTEXT);
Ben Widawskyf343c5f2013-07-05 14:41:04 -07004681 intel_ring_emit(ring, i915_gem_obj_ggtt_offset(dev_priv->ips.renderctx) |
Daniel Vetter6d90c952012-04-26 23:28:05 +02004682 MI_MM_SPACE_GTT |
4683 MI_SAVE_EXT_STATE_EN |
4684 MI_RESTORE_EXT_STATE_EN |
4685 MI_RESTORE_INHIBIT);
4686 intel_ring_emit(ring, MI_SUSPEND_FLUSH);
4687 intel_ring_emit(ring, MI_NOOP);
4688 intel_ring_emit(ring, MI_FLUSH);
4689 intel_ring_advance(ring);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004690
4691 /*
4692 * Wait for the command parser to advance past MI_SET_CONTEXT. The HW
4693 * does an implicit flush, combined with MI_FLUSH above, it should be
4694 * safe to assume that renderctx is valid
4695 */
Chris Wilson3e960502012-11-27 16:22:54 +00004696 ret = intel_ring_idle(ring);
4697 dev_priv->mm.interruptible = was_interruptible;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004698 if (ret) {
Jani Nikuladef27a52013-03-12 10:49:19 +02004699 DRM_ERROR("failed to enable ironlake power savings\n");
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004700 ironlake_teardown_rc6(dev);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004701 return;
4702 }
4703
Ben Widawskyf343c5f2013-07-05 14:41:04 -07004704 I915_WRITE(PWRCTXA, i915_gem_obj_ggtt_offset(dev_priv->ips.pwrctx) | PWRCTX_EN);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004705 I915_WRITE(RSTDBYCTL, I915_READ(RSTDBYCTL) & ~RCX_SW_EXIT);
Ben Widawskydc39fff2013-10-18 12:32:07 -07004706
Imre Deak91ca6892014-04-14 20:24:25 +03004707 intel_print_rc6_info(dev, GEN6_RC_CTL_RC6_ENABLE);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004708}
4709
Eugeni Dodonovdde18882012-04-18 15:29:24 -03004710static unsigned long intel_pxfreq(u32 vidfreq)
4711{
4712 unsigned long freq;
4713 int div = (vidfreq & 0x3f0000) >> 16;
4714 int post = (vidfreq & 0x3000) >> 12;
4715 int pre = (vidfreq & 0x7);
4716
4717 if (!pre)
4718 return 0;
4719
4720 freq = ((div * 133333) / ((1<<post) * pre));
4721
4722 return freq;
4723}
4724
Daniel Vettereb48eb02012-04-26 23:28:12 +02004725static const struct cparams {
4726 u16 i;
4727 u16 t;
4728 u16 m;
4729 u16 c;
4730} cparams[] = {
4731 { 1, 1333, 301, 28664 },
4732 { 1, 1066, 294, 24460 },
4733 { 1, 800, 294, 25192 },
4734 { 0, 1333, 276, 27605 },
4735 { 0, 1066, 276, 27605 },
4736 { 0, 800, 231, 23784 },
4737};
4738
Chris Wilsonf531dcb22012-09-25 10:16:12 +01004739static unsigned long __i915_chipset_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02004740{
4741 u64 total_count, diff, ret;
4742 u32 count1, count2, count3, m = 0, c = 0;
4743 unsigned long now = jiffies_to_msecs(jiffies), diff1;
4744 int i;
4745
Daniel Vetter02d71952012-08-09 16:44:54 +02004746 assert_spin_locked(&mchdev_lock);
4747
Daniel Vetter20e4d402012-08-08 23:35:39 +02004748 diff1 = now - dev_priv->ips.last_time1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02004749
4750 /* Prevent division-by-zero if we are asking too fast.
4751 * Also, we don't get interesting results if we are polling
4752 * faster than once in 10ms, so just return the saved value
4753 * in such cases.
4754 */
4755 if (diff1 <= 10)
Daniel Vetter20e4d402012-08-08 23:35:39 +02004756 return dev_priv->ips.chipset_power;
Daniel Vettereb48eb02012-04-26 23:28:12 +02004757
4758 count1 = I915_READ(DMIEC);
4759 count2 = I915_READ(DDREC);
4760 count3 = I915_READ(CSIEC);
4761
4762 total_count = count1 + count2 + count3;
4763
4764 /* FIXME: handle per-counter overflow */
Daniel Vetter20e4d402012-08-08 23:35:39 +02004765 if (total_count < dev_priv->ips.last_count1) {
4766 diff = ~0UL - dev_priv->ips.last_count1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02004767 diff += total_count;
4768 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +02004769 diff = total_count - dev_priv->ips.last_count1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02004770 }
4771
4772 for (i = 0; i < ARRAY_SIZE(cparams); i++) {
Daniel Vetter20e4d402012-08-08 23:35:39 +02004773 if (cparams[i].i == dev_priv->ips.c_m &&
4774 cparams[i].t == dev_priv->ips.r_t) {
Daniel Vettereb48eb02012-04-26 23:28:12 +02004775 m = cparams[i].m;
4776 c = cparams[i].c;
4777 break;
4778 }
4779 }
4780
4781 diff = div_u64(diff, diff1);
4782 ret = ((m * diff) + c);
4783 ret = div_u64(ret, 10);
4784
Daniel Vetter20e4d402012-08-08 23:35:39 +02004785 dev_priv->ips.last_count1 = total_count;
4786 dev_priv->ips.last_time1 = now;
Daniel Vettereb48eb02012-04-26 23:28:12 +02004787
Daniel Vetter20e4d402012-08-08 23:35:39 +02004788 dev_priv->ips.chipset_power = ret;
Daniel Vettereb48eb02012-04-26 23:28:12 +02004789
4790 return ret;
4791}
4792
Chris Wilsonf531dcb22012-09-25 10:16:12 +01004793unsigned long i915_chipset_val(struct drm_i915_private *dev_priv)
4794{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00004795 struct drm_device *dev = dev_priv->dev;
Chris Wilsonf531dcb22012-09-25 10:16:12 +01004796 unsigned long val;
4797
Damien Lespiau3d13ef22014-02-07 19:12:47 +00004798 if (INTEL_INFO(dev)->gen != 5)
Chris Wilsonf531dcb22012-09-25 10:16:12 +01004799 return 0;
4800
4801 spin_lock_irq(&mchdev_lock);
4802
4803 val = __i915_chipset_val(dev_priv);
4804
4805 spin_unlock_irq(&mchdev_lock);
4806
4807 return val;
4808}
4809
Daniel Vettereb48eb02012-04-26 23:28:12 +02004810unsigned long i915_mch_val(struct drm_i915_private *dev_priv)
4811{
4812 unsigned long m, x, b;
4813 u32 tsfs;
4814
4815 tsfs = I915_READ(TSFS);
4816
4817 m = ((tsfs & TSFS_SLOPE_MASK) >> TSFS_SLOPE_SHIFT);
4818 x = I915_READ8(TR1);
4819
4820 b = tsfs & TSFS_INTR_MASK;
4821
4822 return ((m * x) / 127) - b;
4823}
4824
4825static u16 pvid_to_extvid(struct drm_i915_private *dev_priv, u8 pxvid)
4826{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00004827 struct drm_device *dev = dev_priv->dev;
Daniel Vettereb48eb02012-04-26 23:28:12 +02004828 static const struct v_table {
4829 u16 vd; /* in .1 mil */
4830 u16 vm; /* in .1 mil */
4831 } v_table[] = {
4832 { 0, 0, },
4833 { 375, 0, },
4834 { 500, 0, },
4835 { 625, 0, },
4836 { 750, 0, },
4837 { 875, 0, },
4838 { 1000, 0, },
4839 { 1125, 0, },
4840 { 4125, 3000, },
4841 { 4125, 3000, },
4842 { 4125, 3000, },
4843 { 4125, 3000, },
4844 { 4125, 3000, },
4845 { 4125, 3000, },
4846 { 4125, 3000, },
4847 { 4125, 3000, },
4848 { 4125, 3000, },
4849 { 4125, 3000, },
4850 { 4125, 3000, },
4851 { 4125, 3000, },
4852 { 4125, 3000, },
4853 { 4125, 3000, },
4854 { 4125, 3000, },
4855 { 4125, 3000, },
4856 { 4125, 3000, },
4857 { 4125, 3000, },
4858 { 4125, 3000, },
4859 { 4125, 3000, },
4860 { 4125, 3000, },
4861 { 4125, 3000, },
4862 { 4125, 3000, },
4863 { 4125, 3000, },
4864 { 4250, 3125, },
4865 { 4375, 3250, },
4866 { 4500, 3375, },
4867 { 4625, 3500, },
4868 { 4750, 3625, },
4869 { 4875, 3750, },
4870 { 5000, 3875, },
4871 { 5125, 4000, },
4872 { 5250, 4125, },
4873 { 5375, 4250, },
4874 { 5500, 4375, },
4875 { 5625, 4500, },
4876 { 5750, 4625, },
4877 { 5875, 4750, },
4878 { 6000, 4875, },
4879 { 6125, 5000, },
4880 { 6250, 5125, },
4881 { 6375, 5250, },
4882 { 6500, 5375, },
4883 { 6625, 5500, },
4884 { 6750, 5625, },
4885 { 6875, 5750, },
4886 { 7000, 5875, },
4887 { 7125, 6000, },
4888 { 7250, 6125, },
4889 { 7375, 6250, },
4890 { 7500, 6375, },
4891 { 7625, 6500, },
4892 { 7750, 6625, },
4893 { 7875, 6750, },
4894 { 8000, 6875, },
4895 { 8125, 7000, },
4896 { 8250, 7125, },
4897 { 8375, 7250, },
4898 { 8500, 7375, },
4899 { 8625, 7500, },
4900 { 8750, 7625, },
4901 { 8875, 7750, },
4902 { 9000, 7875, },
4903 { 9125, 8000, },
4904 { 9250, 8125, },
4905 { 9375, 8250, },
4906 { 9500, 8375, },
4907 { 9625, 8500, },
4908 { 9750, 8625, },
4909 { 9875, 8750, },
4910 { 10000, 8875, },
4911 { 10125, 9000, },
4912 { 10250, 9125, },
4913 { 10375, 9250, },
4914 { 10500, 9375, },
4915 { 10625, 9500, },
4916 { 10750, 9625, },
4917 { 10875, 9750, },
4918 { 11000, 9875, },
4919 { 11125, 10000, },
4920 { 11250, 10125, },
4921 { 11375, 10250, },
4922 { 11500, 10375, },
4923 { 11625, 10500, },
4924 { 11750, 10625, },
4925 { 11875, 10750, },
4926 { 12000, 10875, },
4927 { 12125, 11000, },
4928 { 12250, 11125, },
4929 { 12375, 11250, },
4930 { 12500, 11375, },
4931 { 12625, 11500, },
4932 { 12750, 11625, },
4933 { 12875, 11750, },
4934 { 13000, 11875, },
4935 { 13125, 12000, },
4936 { 13250, 12125, },
4937 { 13375, 12250, },
4938 { 13500, 12375, },
4939 { 13625, 12500, },
4940 { 13750, 12625, },
4941 { 13875, 12750, },
4942 { 14000, 12875, },
4943 { 14125, 13000, },
4944 { 14250, 13125, },
4945 { 14375, 13250, },
4946 { 14500, 13375, },
4947 { 14625, 13500, },
4948 { 14750, 13625, },
4949 { 14875, 13750, },
4950 { 15000, 13875, },
4951 { 15125, 14000, },
4952 { 15250, 14125, },
4953 { 15375, 14250, },
4954 { 15500, 14375, },
4955 { 15625, 14500, },
4956 { 15750, 14625, },
4957 { 15875, 14750, },
4958 { 16000, 14875, },
4959 { 16125, 15000, },
4960 };
Damien Lespiau3d13ef22014-02-07 19:12:47 +00004961 if (INTEL_INFO(dev)->is_mobile)
Daniel Vettereb48eb02012-04-26 23:28:12 +02004962 return v_table[pxvid].vm;
4963 else
4964 return v_table[pxvid].vd;
4965}
4966
Daniel Vetter02d71952012-08-09 16:44:54 +02004967static void __i915_update_gfx_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02004968{
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00004969 u64 now, diff, diffms;
Daniel Vettereb48eb02012-04-26 23:28:12 +02004970 u32 count;
4971
Daniel Vetter02d71952012-08-09 16:44:54 +02004972 assert_spin_locked(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004973
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00004974 now = ktime_get_raw_ns();
4975 diffms = now - dev_priv->ips.last_time2;
4976 do_div(diffms, NSEC_PER_MSEC);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004977
4978 /* Don't divide by 0 */
Daniel Vettereb48eb02012-04-26 23:28:12 +02004979 if (!diffms)
4980 return;
4981
4982 count = I915_READ(GFXEC);
4983
Daniel Vetter20e4d402012-08-08 23:35:39 +02004984 if (count < dev_priv->ips.last_count2) {
4985 diff = ~0UL - dev_priv->ips.last_count2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02004986 diff += count;
4987 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +02004988 diff = count - dev_priv->ips.last_count2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02004989 }
4990
Daniel Vetter20e4d402012-08-08 23:35:39 +02004991 dev_priv->ips.last_count2 = count;
4992 dev_priv->ips.last_time2 = now;
Daniel Vettereb48eb02012-04-26 23:28:12 +02004993
4994 /* More magic constants... */
4995 diff = diff * 1181;
4996 diff = div_u64(diff, diffms * 10);
Daniel Vetter20e4d402012-08-08 23:35:39 +02004997 dev_priv->ips.gfx_power = diff;
Daniel Vettereb48eb02012-04-26 23:28:12 +02004998}
4999
Daniel Vetter02d71952012-08-09 16:44:54 +02005000void i915_update_gfx_val(struct drm_i915_private *dev_priv)
5001{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00005002 struct drm_device *dev = dev_priv->dev;
5003
5004 if (INTEL_INFO(dev)->gen != 5)
Daniel Vetter02d71952012-08-09 16:44:54 +02005005 return;
5006
Daniel Vetter92703882012-08-09 16:46:01 +02005007 spin_lock_irq(&mchdev_lock);
Daniel Vetter02d71952012-08-09 16:44:54 +02005008
5009 __i915_update_gfx_val(dev_priv);
5010
Daniel Vetter92703882012-08-09 16:46:01 +02005011 spin_unlock_irq(&mchdev_lock);
Daniel Vetter02d71952012-08-09 16:44:54 +02005012}
5013
Chris Wilsonf531dcb22012-09-25 10:16:12 +01005014static unsigned long __i915_gfx_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02005015{
5016 unsigned long t, corr, state1, corr2, state2;
5017 u32 pxvid, ext_v;
5018
Daniel Vetter02d71952012-08-09 16:44:54 +02005019 assert_spin_locked(&mchdev_lock);
5020
Ben Widawskyb39fb292014-03-19 18:31:11 -07005021 pxvid = I915_READ(PXVFREQ_BASE + (dev_priv->rps.cur_freq * 4));
Daniel Vettereb48eb02012-04-26 23:28:12 +02005022 pxvid = (pxvid >> 24) & 0x7f;
5023 ext_v = pvid_to_extvid(dev_priv, pxvid);
5024
5025 state1 = ext_v;
5026
5027 t = i915_mch_val(dev_priv);
5028
5029 /* Revel in the empirically derived constants */
5030
5031 /* Correction factor in 1/100000 units */
5032 if (t > 80)
5033 corr = ((t * 2349) + 135940);
5034 else if (t >= 50)
5035 corr = ((t * 964) + 29317);
5036 else /* < 50 */
5037 corr = ((t * 301) + 1004);
5038
5039 corr = corr * ((150142 * state1) / 10000 - 78642);
5040 corr /= 100000;
Daniel Vetter20e4d402012-08-08 23:35:39 +02005041 corr2 = (corr * dev_priv->ips.corr);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005042
5043 state2 = (corr2 * state1) / 10000;
5044 state2 /= 100; /* convert to mW */
5045
Daniel Vetter02d71952012-08-09 16:44:54 +02005046 __i915_update_gfx_val(dev_priv);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005047
Daniel Vetter20e4d402012-08-08 23:35:39 +02005048 return dev_priv->ips.gfx_power + state2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005049}
5050
Chris Wilsonf531dcb22012-09-25 10:16:12 +01005051unsigned long i915_gfx_val(struct drm_i915_private *dev_priv)
5052{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00005053 struct drm_device *dev = dev_priv->dev;
Chris Wilsonf531dcb22012-09-25 10:16:12 +01005054 unsigned long val;
5055
Damien Lespiau3d13ef22014-02-07 19:12:47 +00005056 if (INTEL_INFO(dev)->gen != 5)
Chris Wilsonf531dcb22012-09-25 10:16:12 +01005057 return 0;
5058
5059 spin_lock_irq(&mchdev_lock);
5060
5061 val = __i915_gfx_val(dev_priv);
5062
5063 spin_unlock_irq(&mchdev_lock);
5064
5065 return val;
5066}
5067
Daniel Vettereb48eb02012-04-26 23:28:12 +02005068/**
5069 * i915_read_mch_val - return value for IPS use
5070 *
5071 * Calculate and return a value for the IPS driver to use when deciding whether
5072 * we have thermal and power headroom to increase CPU or GPU power budget.
5073 */
5074unsigned long i915_read_mch_val(void)
5075{
5076 struct drm_i915_private *dev_priv;
5077 unsigned long chipset_val, graphics_val, ret = 0;
5078
Daniel Vetter92703882012-08-09 16:46:01 +02005079 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005080 if (!i915_mch_dev)
5081 goto out_unlock;
5082 dev_priv = i915_mch_dev;
5083
Chris Wilsonf531dcb22012-09-25 10:16:12 +01005084 chipset_val = __i915_chipset_val(dev_priv);
5085 graphics_val = __i915_gfx_val(dev_priv);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005086
5087 ret = chipset_val + graphics_val;
5088
5089out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02005090 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005091
5092 return ret;
5093}
5094EXPORT_SYMBOL_GPL(i915_read_mch_val);
5095
5096/**
5097 * i915_gpu_raise - raise GPU frequency limit
5098 *
5099 * Raise the limit; IPS indicates we have thermal headroom.
5100 */
5101bool i915_gpu_raise(void)
5102{
5103 struct drm_i915_private *dev_priv;
5104 bool ret = true;
5105
Daniel Vetter92703882012-08-09 16:46:01 +02005106 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005107 if (!i915_mch_dev) {
5108 ret = false;
5109 goto out_unlock;
5110 }
5111 dev_priv = i915_mch_dev;
5112
Daniel Vetter20e4d402012-08-08 23:35:39 +02005113 if (dev_priv->ips.max_delay > dev_priv->ips.fmax)
5114 dev_priv->ips.max_delay--;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005115
5116out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02005117 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005118
5119 return ret;
5120}
5121EXPORT_SYMBOL_GPL(i915_gpu_raise);
5122
5123/**
5124 * i915_gpu_lower - lower GPU frequency limit
5125 *
5126 * IPS indicates we're close to a thermal limit, so throttle back the GPU
5127 * frequency maximum.
5128 */
5129bool i915_gpu_lower(void)
5130{
5131 struct drm_i915_private *dev_priv;
5132 bool ret = true;
5133
Daniel Vetter92703882012-08-09 16:46:01 +02005134 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005135 if (!i915_mch_dev) {
5136 ret = false;
5137 goto out_unlock;
5138 }
5139 dev_priv = i915_mch_dev;
5140
Daniel Vetter20e4d402012-08-08 23:35:39 +02005141 if (dev_priv->ips.max_delay < dev_priv->ips.min_delay)
5142 dev_priv->ips.max_delay++;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005143
5144out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02005145 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005146
5147 return ret;
5148}
5149EXPORT_SYMBOL_GPL(i915_gpu_lower);
5150
5151/**
5152 * i915_gpu_busy - indicate GPU business to IPS
5153 *
5154 * Tell the IPS driver whether or not the GPU is busy.
5155 */
5156bool i915_gpu_busy(void)
5157{
5158 struct drm_i915_private *dev_priv;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01005159 struct intel_engine_cs *ring;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005160 bool ret = false;
Chris Wilsonf047e392012-07-21 12:31:41 +01005161 int i;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005162
Daniel Vetter92703882012-08-09 16:46:01 +02005163 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005164 if (!i915_mch_dev)
5165 goto out_unlock;
5166 dev_priv = i915_mch_dev;
5167
Chris Wilsonf047e392012-07-21 12:31:41 +01005168 for_each_ring(ring, dev_priv, i)
5169 ret |= !list_empty(&ring->request_list);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005170
5171out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02005172 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005173
5174 return ret;
5175}
5176EXPORT_SYMBOL_GPL(i915_gpu_busy);
5177
5178/**
5179 * i915_gpu_turbo_disable - disable graphics turbo
5180 *
5181 * Disable graphics turbo by resetting the max frequency and setting the
5182 * current frequency to the default.
5183 */
5184bool i915_gpu_turbo_disable(void)
5185{
5186 struct drm_i915_private *dev_priv;
5187 bool ret = true;
5188
Daniel Vetter92703882012-08-09 16:46:01 +02005189 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005190 if (!i915_mch_dev) {
5191 ret = false;
5192 goto out_unlock;
5193 }
5194 dev_priv = i915_mch_dev;
5195
Daniel Vetter20e4d402012-08-08 23:35:39 +02005196 dev_priv->ips.max_delay = dev_priv->ips.fstart;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005197
Daniel Vetter20e4d402012-08-08 23:35:39 +02005198 if (!ironlake_set_drps(dev_priv->dev, dev_priv->ips.fstart))
Daniel Vettereb48eb02012-04-26 23:28:12 +02005199 ret = false;
5200
5201out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02005202 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005203
5204 return ret;
5205}
5206EXPORT_SYMBOL_GPL(i915_gpu_turbo_disable);
5207
5208/**
5209 * Tells the intel_ips driver that the i915 driver is now loaded, if
5210 * IPS got loaded first.
5211 *
5212 * This awkward dance is so that neither module has to depend on the
5213 * other in order for IPS to do the appropriate communication of
5214 * GPU turbo limits to i915.
5215 */
5216static void
5217ips_ping_for_i915_load(void)
5218{
5219 void (*link)(void);
5220
5221 link = symbol_get(ips_link_to_i915_driver);
5222 if (link) {
5223 link();
5224 symbol_put(ips_link_to_i915_driver);
5225 }
5226}
5227
5228void intel_gpu_ips_init(struct drm_i915_private *dev_priv)
5229{
Daniel Vetter02d71952012-08-09 16:44:54 +02005230 /* We only register the i915 ips part with intel-ips once everything is
5231 * set up, to avoid intel-ips sneaking in and reading bogus values. */
Daniel Vetter92703882012-08-09 16:46:01 +02005232 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005233 i915_mch_dev = dev_priv;
Daniel Vetter92703882012-08-09 16:46:01 +02005234 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005235
5236 ips_ping_for_i915_load();
5237}
5238
5239void intel_gpu_ips_teardown(void)
5240{
Daniel Vetter92703882012-08-09 16:46:01 +02005241 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005242 i915_mch_dev = NULL;
Daniel Vetter92703882012-08-09 16:46:01 +02005243 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005244}
Deepak S76c3552f2014-01-30 23:08:16 +05305245
Daniel Vetter8090c6b2012-06-24 16:42:32 +02005246static void intel_init_emon(struct drm_device *dev)
Eugeni Dodonovdde18882012-04-18 15:29:24 -03005247{
5248 struct drm_i915_private *dev_priv = dev->dev_private;
5249 u32 lcfuse;
5250 u8 pxw[16];
5251 int i;
5252
5253 /* Disable to program */
5254 I915_WRITE(ECR, 0);
5255 POSTING_READ(ECR);
5256
5257 /* Program energy weights for various events */
5258 I915_WRITE(SDEW, 0x15040d00);
5259 I915_WRITE(CSIEW0, 0x007f0000);
5260 I915_WRITE(CSIEW1, 0x1e220004);
5261 I915_WRITE(CSIEW2, 0x04000004);
5262
5263 for (i = 0; i < 5; i++)
5264 I915_WRITE(PEW + (i * 4), 0);
5265 for (i = 0; i < 3; i++)
5266 I915_WRITE(DEW + (i * 4), 0);
5267
5268 /* Program P-state weights to account for frequency power adjustment */
5269 for (i = 0; i < 16; i++) {
5270 u32 pxvidfreq = I915_READ(PXVFREQ_BASE + (i * 4));
5271 unsigned long freq = intel_pxfreq(pxvidfreq);
5272 unsigned long vid = (pxvidfreq & PXVFREQ_PX_MASK) >>
5273 PXVFREQ_PX_SHIFT;
5274 unsigned long val;
5275
5276 val = vid * vid;
5277 val *= (freq / 1000);
5278 val *= 255;
5279 val /= (127*127*900);
5280 if (val > 0xff)
5281 DRM_ERROR("bad pxval: %ld\n", val);
5282 pxw[i] = val;
5283 }
5284 /* Render standby states get 0 weight */
5285 pxw[14] = 0;
5286 pxw[15] = 0;
5287
5288 for (i = 0; i < 4; i++) {
5289 u32 val = (pxw[i*4] << 24) | (pxw[(i*4)+1] << 16) |
5290 (pxw[(i*4)+2] << 8) | (pxw[(i*4)+3]);
5291 I915_WRITE(PXW + (i * 4), val);
5292 }
5293
5294 /* Adjust magic regs to magic values (more experimental results) */
5295 I915_WRITE(OGW0, 0);
5296 I915_WRITE(OGW1, 0);
5297 I915_WRITE(EG0, 0x00007f00);
5298 I915_WRITE(EG1, 0x0000000e);
5299 I915_WRITE(EG2, 0x000e0000);
5300 I915_WRITE(EG3, 0x68000300);
5301 I915_WRITE(EG4, 0x42000000);
5302 I915_WRITE(EG5, 0x00140031);
5303 I915_WRITE(EG6, 0);
5304 I915_WRITE(EG7, 0);
5305
5306 for (i = 0; i < 8; i++)
5307 I915_WRITE(PXWL + (i * 4), 0);
5308
5309 /* Enable PMON + select events */
5310 I915_WRITE(ECR, 0x80000019);
5311
5312 lcfuse = I915_READ(LCFUSE02);
5313
Daniel Vetter20e4d402012-08-08 23:35:39 +02005314 dev_priv->ips.corr = (lcfuse & LCFUSE_HIV_MASK);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03005315}
5316
Imre Deakae484342014-03-31 15:10:44 +03005317void intel_init_gt_powersave(struct drm_device *dev)
5318{
Imre Deake6069ca2014-04-18 16:01:02 +03005319 i915.enable_rc6 = sanitize_rc6_option(dev, i915.enable_rc6);
5320
Deepak S38807742014-05-23 21:00:15 +05305321 if (IS_CHERRYVIEW(dev))
5322 cherryview_init_gt_powersave(dev);
5323 else if (IS_VALLEYVIEW(dev))
Imre Deak4e805192014-04-14 20:24:41 +03005324 valleyview_init_gt_powersave(dev);
Imre Deakae484342014-03-31 15:10:44 +03005325}
5326
5327void intel_cleanup_gt_powersave(struct drm_device *dev)
5328{
Deepak S38807742014-05-23 21:00:15 +05305329 if (IS_CHERRYVIEW(dev))
5330 return;
5331 else if (IS_VALLEYVIEW(dev))
Imre Deak4e805192014-04-14 20:24:41 +03005332 valleyview_cleanup_gt_powersave(dev);
Imre Deakae484342014-03-31 15:10:44 +03005333}
5334
Jesse Barnes156c7ca2014-06-12 08:35:45 -07005335/**
5336 * intel_suspend_gt_powersave - suspend PM work and helper threads
5337 * @dev: drm device
5338 *
5339 * We don't want to disable RC6 or other features here, we just want
5340 * to make sure any work we've queued has finished and won't bother
5341 * us while we're suspended.
5342 */
5343void intel_suspend_gt_powersave(struct drm_device *dev)
5344{
5345 struct drm_i915_private *dev_priv = dev->dev_private;
5346
5347 /* Interrupts should be disabled already to avoid re-arming. */
Jesse Barnes9df7575f2014-06-20 09:29:20 -07005348 WARN_ON(intel_irqs_enabled(dev_priv));
Jesse Barnes156c7ca2014-06-12 08:35:45 -07005349
5350 flush_delayed_work(&dev_priv->rps.delayed_resume_work);
5351
5352 cancel_work_sync(&dev_priv->rps.work);
Deepak Sb47adc12014-06-20 20:03:02 +05305353
5354 /* Force GPU to min freq during suspend */
5355 gen6_rps_idle(dev_priv);
Jesse Barnes156c7ca2014-06-12 08:35:45 -07005356}
5357
Daniel Vetter8090c6b2012-06-24 16:42:32 +02005358void intel_disable_gt_powersave(struct drm_device *dev)
5359{
Jesse Barnes1a01ab32012-11-02 11:14:00 -07005360 struct drm_i915_private *dev_priv = dev->dev_private;
5361
Daniel Vetterfd0c0642013-04-24 11:13:35 +02005362 /* Interrupts should be disabled already to avoid re-arming. */
Jesse Barnes9df7575f2014-06-20 09:29:20 -07005363 WARN_ON(intel_irqs_enabled(dev_priv));
Daniel Vetterfd0c0642013-04-24 11:13:35 +02005364
Daniel Vetter930ebb42012-06-29 23:32:16 +02005365 if (IS_IRONLAKE_M(dev)) {
Daniel Vetter8090c6b2012-06-24 16:42:32 +02005366 ironlake_disable_drps(dev);
Daniel Vetter930ebb42012-06-29 23:32:16 +02005367 ironlake_disable_rc6(dev);
Deepak S38807742014-05-23 21:00:15 +05305368 } else if (INTEL_INFO(dev)->gen >= 6) {
Daniel Vetter10d8d362014-06-12 17:48:52 +02005369 intel_suspend_gt_powersave(dev);
Imre Deake4948372014-05-12 18:35:04 +03005370
Jesse Barnes4fc688c2012-11-02 11:14:01 -07005371 mutex_lock(&dev_priv->rps.hw_lock);
Deepak S38807742014-05-23 21:00:15 +05305372 if (IS_CHERRYVIEW(dev))
5373 cherryview_disable_rps(dev);
5374 else if (IS_VALLEYVIEW(dev))
Jesse Barnesd20d4f02013-04-23 10:09:28 -07005375 valleyview_disable_rps(dev);
5376 else
5377 gen6_disable_rps(dev);
Chris Wilsonc0951f02013-10-10 21:58:50 +01005378 dev_priv->rps.enabled = false;
Jesse Barnes4fc688c2012-11-02 11:14:01 -07005379 mutex_unlock(&dev_priv->rps.hw_lock);
Daniel Vetter930ebb42012-06-29 23:32:16 +02005380 }
Daniel Vetter8090c6b2012-06-24 16:42:32 +02005381}
5382
Jesse Barnes1a01ab32012-11-02 11:14:00 -07005383static void intel_gen6_powersave_work(struct work_struct *work)
5384{
5385 struct drm_i915_private *dev_priv =
5386 container_of(work, struct drm_i915_private,
5387 rps.delayed_resume_work.work);
5388 struct drm_device *dev = dev_priv->dev;
5389
Daisy Sunc76bb612014-08-11 11:08:38 -07005390 dev_priv->rps.is_bdw_sw_turbo = false;
5391
Jesse Barnes4fc688c2012-11-02 11:14:01 -07005392 mutex_lock(&dev_priv->rps.hw_lock);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005393
Deepak S38807742014-05-23 21:00:15 +05305394 if (IS_CHERRYVIEW(dev)) {
5395 cherryview_enable_rps(dev);
5396 } else if (IS_VALLEYVIEW(dev)) {
Jesse Barnes0a073b82013-04-17 15:54:58 -07005397 valleyview_enable_rps(dev);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005398 } else if (IS_BROADWELL(dev)) {
5399 gen8_enable_rps(dev);
Imre Deakc2bc2fc2014-04-18 16:16:23 +03005400 __gen6_update_ring_freq(dev);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005401 } else {
5402 gen6_enable_rps(dev);
Imre Deakc2bc2fc2014-04-18 16:16:23 +03005403 __gen6_update_ring_freq(dev);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005404 }
Chris Wilsonc0951f02013-10-10 21:58:50 +01005405 dev_priv->rps.enabled = true;
Jesse Barnes4fc688c2012-11-02 11:14:01 -07005406 mutex_unlock(&dev_priv->rps.hw_lock);
Imre Deakc6df39b2014-04-14 20:24:29 +03005407
5408 intel_runtime_pm_put(dev_priv);
Jesse Barnes1a01ab32012-11-02 11:14:00 -07005409}
5410
Daniel Vetter8090c6b2012-06-24 16:42:32 +02005411void intel_enable_gt_powersave(struct drm_device *dev)
5412{
Jesse Barnes1a01ab32012-11-02 11:14:00 -07005413 struct drm_i915_private *dev_priv = dev->dev_private;
5414
Daniel Vetter8090c6b2012-06-24 16:42:32 +02005415 if (IS_IRONLAKE_M(dev)) {
Imre Deakdc1d0132014-04-14 20:24:28 +03005416 mutex_lock(&dev->struct_mutex);
Daniel Vetter8090c6b2012-06-24 16:42:32 +02005417 ironlake_enable_drps(dev);
5418 ironlake_enable_rc6(dev);
5419 intel_init_emon(dev);
Imre Deakdc1d0132014-04-14 20:24:28 +03005420 mutex_unlock(&dev->struct_mutex);
Deepak S38807742014-05-23 21:00:15 +05305421 } else if (INTEL_INFO(dev)->gen >= 6) {
Jesse Barnes1a01ab32012-11-02 11:14:00 -07005422 /*
5423 * PCU communication is slow and this doesn't need to be
5424 * done at any specific time, so do this out of our fast path
5425 * to make resume and init faster.
Imre Deakc6df39b2014-04-14 20:24:29 +03005426 *
5427 * We depend on the HW RC6 power context save/restore
5428 * mechanism when entering D3 through runtime PM suspend. So
5429 * disable RPM until RPS/RC6 is properly setup. We can only
5430 * get here via the driver load/system resume/runtime resume
5431 * paths, so the _noresume version is enough (and in case of
5432 * runtime resume it's necessary).
Jesse Barnes1a01ab32012-11-02 11:14:00 -07005433 */
Imre Deakc6df39b2014-04-14 20:24:29 +03005434 if (schedule_delayed_work(&dev_priv->rps.delayed_resume_work,
5435 round_jiffies_up_relative(HZ)))
5436 intel_runtime_pm_get_noresume(dev_priv);
Daniel Vetter8090c6b2012-06-24 16:42:32 +02005437 }
5438}
5439
Imre Deakc6df39b2014-04-14 20:24:29 +03005440void intel_reset_gt_powersave(struct drm_device *dev)
5441{
5442 struct drm_i915_private *dev_priv = dev->dev_private;
5443
5444 dev_priv->rps.enabled = false;
5445 intel_enable_gt_powersave(dev);
5446}
5447
Daniel Vetter3107bd42012-10-31 22:52:31 +01005448static void ibx_init_clock_gating(struct drm_device *dev)
5449{
5450 struct drm_i915_private *dev_priv = dev->dev_private;
5451
5452 /*
5453 * On Ibex Peak and Cougar Point, we need to disable clock
5454 * gating for the panel power sequencer or it will fail to
5455 * start up when no ports are active.
5456 */
5457 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE);
5458}
5459
Ville Syrjälä0e088b82013-06-07 10:47:04 +03005460static void g4x_disable_trickle_feed(struct drm_device *dev)
5461{
5462 struct drm_i915_private *dev_priv = dev->dev_private;
5463 int pipe;
5464
Damien Lespiau055e3932014-08-18 13:49:10 +01005465 for_each_pipe(dev_priv, pipe) {
Ville Syrjälä0e088b82013-06-07 10:47:04 +03005466 I915_WRITE(DSPCNTR(pipe),
5467 I915_READ(DSPCNTR(pipe)) |
5468 DISPPLANE_TRICKLE_FEED_DISABLE);
Ville Syrjälä1dba99f2013-10-01 18:02:18 +03005469 intel_flush_primary_plane(dev_priv, pipe);
Ville Syrjälä0e088b82013-06-07 10:47:04 +03005470 }
5471}
5472
Ville Syrjälä017636c2013-12-05 15:51:37 +02005473static void ilk_init_lp_watermarks(struct drm_device *dev)
5474{
5475 struct drm_i915_private *dev_priv = dev->dev_private;
5476
5477 I915_WRITE(WM3_LP_ILK, I915_READ(WM3_LP_ILK) & ~WM1_LP_SR_EN);
5478 I915_WRITE(WM2_LP_ILK, I915_READ(WM2_LP_ILK) & ~WM1_LP_SR_EN);
5479 I915_WRITE(WM1_LP_ILK, I915_READ(WM1_LP_ILK) & ~WM1_LP_SR_EN);
5480
5481 /*
5482 * Don't touch WM1S_LP_EN here.
5483 * Doing so could cause underruns.
5484 */
5485}
5486
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005487static void ironlake_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005488{
5489 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau231e54f2012-10-19 17:55:41 +01005490 uint32_t dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005491
Damien Lespiauf1e8fa52013-06-07 17:41:09 +01005492 /*
5493 * Required for FBC
5494 * WaFbcDisableDpfcClockGating:ilk
5495 */
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01005496 dspclk_gate |= ILK_DPFCRUNIT_CLOCK_GATE_DISABLE |
5497 ILK_DPFCUNIT_CLOCK_GATE_DISABLE |
5498 ILK_DPFDUNIT_CLOCK_GATE_ENABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005499
5500 I915_WRITE(PCH_3DCGDIS0,
5501 MARIUNIT_CLOCK_GATE_DISABLE |
5502 SVSMUNIT_CLOCK_GATE_DISABLE);
5503 I915_WRITE(PCH_3DCGDIS1,
5504 VFMUNIT_CLOCK_GATE_DISABLE);
5505
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005506 /*
5507 * According to the spec the following bits should be set in
5508 * order to enable memory self-refresh
5509 * The bit 22/21 of 0x42004
5510 * The bit 5 of 0x42020
5511 * The bit 15 of 0x45000
5512 */
5513 I915_WRITE(ILK_DISPLAY_CHICKEN2,
5514 (I915_READ(ILK_DISPLAY_CHICKEN2) |
5515 ILK_DPARB_GATE | ILK_VSDPFD_FULL));
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01005516 dspclk_gate |= ILK_DPARBUNIT_CLOCK_GATE_ENABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005517 I915_WRITE(DISP_ARB_CTL,
5518 (I915_READ(DISP_ARB_CTL) |
5519 DISP_FBC_WM_DIS));
Ville Syrjälä017636c2013-12-05 15:51:37 +02005520
5521 ilk_init_lp_watermarks(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005522
5523 /*
5524 * Based on the document from hardware guys the following bits
5525 * should be set unconditionally in order to enable FBC.
5526 * The bit 22 of 0x42000
5527 * The bit 22 of 0x42004
5528 * The bit 7,8,9 of 0x42020.
5529 */
5530 if (IS_IRONLAKE_M(dev)) {
Damien Lespiau4bb35332013-06-14 15:23:24 +01005531 /* WaFbcAsynchFlipDisableFbcQueue:ilk */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005532 I915_WRITE(ILK_DISPLAY_CHICKEN1,
5533 I915_READ(ILK_DISPLAY_CHICKEN1) |
5534 ILK_FBCQ_DIS);
5535 I915_WRITE(ILK_DISPLAY_CHICKEN2,
5536 I915_READ(ILK_DISPLAY_CHICKEN2) |
5537 ILK_DPARB_GATE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005538 }
5539
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01005540 I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
5541
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005542 I915_WRITE(ILK_DISPLAY_CHICKEN2,
5543 I915_READ(ILK_DISPLAY_CHICKEN2) |
5544 ILK_ELPIN_409_SELECT);
5545 I915_WRITE(_3D_CHICKEN2,
5546 _3D_CHICKEN2_WM_READ_PIPELINED << 16 |
5547 _3D_CHICKEN2_WM_READ_PIPELINED);
Daniel Vetter4358a372012-10-18 11:49:51 +02005548
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005549 /* WaDisableRenderCachePipelinedFlush:ilk */
Daniel Vetter4358a372012-10-18 11:49:51 +02005550 I915_WRITE(CACHE_MODE_0,
5551 _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
Daniel Vetter3107bd42012-10-31 22:52:31 +01005552
Akash Goel4e046322014-04-04 17:14:38 +05305553 /* WaDisable_RenderCache_OperationalFlush:ilk */
5554 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
5555
Ville Syrjälä0e088b82013-06-07 10:47:04 +03005556 g4x_disable_trickle_feed(dev);
Ville Syrjäläbdad2b22013-06-07 10:47:03 +03005557
Daniel Vetter3107bd42012-10-31 22:52:31 +01005558 ibx_init_clock_gating(dev);
5559}
5560
5561static void cpt_init_clock_gating(struct drm_device *dev)
5562{
5563 struct drm_i915_private *dev_priv = dev->dev_private;
5564 int pipe;
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03005565 uint32_t val;
Daniel Vetter3107bd42012-10-31 22:52:31 +01005566
5567 /*
5568 * On Ibex Peak and Cougar Point, we need to disable clock
5569 * gating for the panel power sequencer or it will fail to
5570 * start up when no ports are active.
5571 */
Jesse Barnescd664072013-10-02 10:34:19 -07005572 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE |
5573 PCH_DPLUNIT_CLOCK_GATE_DISABLE |
5574 PCH_CPUNIT_CLOCK_GATE_DISABLE);
Daniel Vetter3107bd42012-10-31 22:52:31 +01005575 I915_WRITE(SOUTH_CHICKEN2, I915_READ(SOUTH_CHICKEN2) |
5576 DPLS_EDP_PPS_FIX_DIS);
Takashi Iwai335c07b2012-12-11 11:46:29 +01005577 /* The below fixes the weird display corruption, a few pixels shifted
5578 * downward, on (only) LVDS of some HP laptops with IVY.
5579 */
Damien Lespiau055e3932014-08-18 13:49:10 +01005580 for_each_pipe(dev_priv, pipe) {
Paulo Zanonidc4bd2d2013-04-08 15:48:08 -03005581 val = I915_READ(TRANS_CHICKEN2(pipe));
5582 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
5583 val &= ~TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03005584 if (dev_priv->vbt.fdi_rx_polarity_inverted)
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03005585 val |= TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
Paulo Zanonidc4bd2d2013-04-08 15:48:08 -03005586 val &= ~TRANS_CHICKEN2_FRAME_START_DELAY_MASK;
5587 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_COUNTER;
5588 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_MODESWITCH;
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03005589 I915_WRITE(TRANS_CHICKEN2(pipe), val);
5590 }
Daniel Vetter3107bd42012-10-31 22:52:31 +01005591 /* WADP0ClockGatingDisable */
Damien Lespiau055e3932014-08-18 13:49:10 +01005592 for_each_pipe(dev_priv, pipe) {
Daniel Vetter3107bd42012-10-31 22:52:31 +01005593 I915_WRITE(TRANS_CHICKEN1(pipe),
5594 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
5595 }
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005596}
5597
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01005598static void gen6_check_mch_setup(struct drm_device *dev)
5599{
5600 struct drm_i915_private *dev_priv = dev->dev_private;
5601 uint32_t tmp;
5602
5603 tmp = I915_READ(MCH_SSKPD);
Daniel Vetterdf662a22014-08-04 11:17:25 +02005604 if ((tmp & MCH_SSKPD_WM0_MASK) != MCH_SSKPD_WM0_VAL)
5605 DRM_DEBUG_KMS("Wrong MCH_SSKPD value: 0x%08x This can cause underruns.\n",
5606 tmp);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01005607}
5608
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005609static void gen6_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005610{
5611 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau231e54f2012-10-19 17:55:41 +01005612 uint32_t dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005613
Damien Lespiau231e54f2012-10-19 17:55:41 +01005614 I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005615
5616 I915_WRITE(ILK_DISPLAY_CHICKEN2,
5617 I915_READ(ILK_DISPLAY_CHICKEN2) |
5618 ILK_ELPIN_409_SELECT);
5619
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005620 /* WaDisableHiZPlanesWhenMSAAEnabled:snb */
Daniel Vetter42839082012-12-14 23:38:28 +01005621 I915_WRITE(_3D_CHICKEN,
5622 _MASKED_BIT_ENABLE(_3D_CHICKEN_HIZ_PLANE_DISABLE_MSAA_4X_SNB));
5623
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005624 /* WaSetupGtModeTdRowDispatch:snb */
Daniel Vetter6547fbd2012-12-14 23:38:29 +01005625 if (IS_SNB_GT1(dev))
5626 I915_WRITE(GEN6_GT_MODE,
5627 _MASKED_BIT_ENABLE(GEN6_TD_FOUR_ROW_DISPATCH_DISABLE));
5628
Akash Goel4e046322014-04-04 17:14:38 +05305629 /* WaDisable_RenderCache_OperationalFlush:snb */
5630 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
5631
Ville Syrjälä8d85d272014-02-04 21:59:15 +02005632 /*
5633 * BSpec recoomends 8x4 when MSAA is used,
5634 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02005635 *
5636 * Note that PS/WM thread counts depend on the WIZ hashing
5637 * disable bit, which we don't touch here, but it's good
5638 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjälä8d85d272014-02-04 21:59:15 +02005639 */
5640 I915_WRITE(GEN6_GT_MODE,
5641 GEN6_WIZ_HASHING_MASK | GEN6_WIZ_HASHING_16x4);
5642
Ville Syrjälä017636c2013-12-05 15:51:37 +02005643 ilk_init_lp_watermarks(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005644
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005645 I915_WRITE(CACHE_MODE_0,
Daniel Vetter50743292012-04-26 22:02:54 +02005646 _MASKED_BIT_DISABLE(CM0_STC_EVICT_DISABLE_LRA_SNB));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005647
5648 I915_WRITE(GEN6_UCGCTL1,
5649 I915_READ(GEN6_UCGCTL1) |
5650 GEN6_BLBUNIT_CLOCK_GATE_DISABLE |
5651 GEN6_CSUNIT_CLOCK_GATE_DISABLE);
5652
5653 /* According to the BSpec vol1g, bit 12 (RCPBUNIT) clock
5654 * gating disable must be set. Failure to set it results in
5655 * flickering pixels due to Z write ordering failures after
5656 * some amount of runtime in the Mesa "fire" demo, and Unigine
5657 * Sanctuary and Tropics, and apparently anything else with
5658 * alpha test or pixel discard.
5659 *
5660 * According to the spec, bit 11 (RCCUNIT) must also be set,
5661 * but we didn't debug actual testcases to find it out.
Jesse Barnes0f846f82012-06-14 11:04:47 -07005662 *
Ville Syrjäläef593182014-01-22 21:32:47 +02005663 * WaDisableRCCUnitClockGating:snb
5664 * WaDisableRCPBUnitClockGating:snb
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005665 */
5666 I915_WRITE(GEN6_UCGCTL2,
5667 GEN6_RCPBUNIT_CLOCK_GATE_DISABLE |
5668 GEN6_RCCUNIT_CLOCK_GATE_DISABLE);
5669
Ville Syrjälä5eb146d2014-02-04 21:59:16 +02005670 /* WaStripsFansDisableFastClipPerformanceFix:snb */
Ville Syrjälä743b57d2014-02-04 21:59:17 +02005671 I915_WRITE(_3D_CHICKEN3,
5672 _MASKED_BIT_ENABLE(_3D_CHICKEN3_SF_DISABLE_FASTCLIP_CULL));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005673
5674 /*
Ville Syrjäläe927ecd2014-02-04 21:59:18 +02005675 * Bspec says:
5676 * "This bit must be set if 3DSTATE_CLIP clip mode is set to normal and
5677 * 3DSTATE_SF number of SF output attributes is more than 16."
5678 */
5679 I915_WRITE(_3D_CHICKEN3,
5680 _MASKED_BIT_ENABLE(_3D_CHICKEN3_SF_DISABLE_PIPELINED_ATTR_FETCH));
5681
5682 /*
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005683 * According to the spec the following bits should be
5684 * set in order to enable memory self-refresh and fbc:
5685 * The bit21 and bit22 of 0x42000
5686 * The bit21 and bit22 of 0x42004
5687 * The bit5 and bit7 of 0x42020
5688 * The bit14 of 0x70180
5689 * The bit14 of 0x71180
Damien Lespiau4bb35332013-06-14 15:23:24 +01005690 *
5691 * WaFbcAsynchFlipDisableFbcQueue:snb
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005692 */
5693 I915_WRITE(ILK_DISPLAY_CHICKEN1,
5694 I915_READ(ILK_DISPLAY_CHICKEN1) |
5695 ILK_FBCQ_DIS | ILK_PABSTRETCH_DIS);
5696 I915_WRITE(ILK_DISPLAY_CHICKEN2,
5697 I915_READ(ILK_DISPLAY_CHICKEN2) |
5698 ILK_DPARB_GATE | ILK_VSDPFD_FULL);
Damien Lespiau231e54f2012-10-19 17:55:41 +01005699 I915_WRITE(ILK_DSPCLK_GATE_D,
5700 I915_READ(ILK_DSPCLK_GATE_D) |
5701 ILK_DPARBUNIT_CLOCK_GATE_ENABLE |
5702 ILK_DPFDUNIT_CLOCK_GATE_ENABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005703
Ville Syrjälä0e088b82013-06-07 10:47:04 +03005704 g4x_disable_trickle_feed(dev);
Ben Widawskyf8f2ac92012-10-03 19:34:24 -07005705
Daniel Vetter3107bd42012-10-31 22:52:31 +01005706 cpt_init_clock_gating(dev);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01005707
5708 gen6_check_mch_setup(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005709}
5710
5711static void gen7_setup_fixed_func_scheduler(struct drm_i915_private *dev_priv)
5712{
5713 uint32_t reg = I915_READ(GEN7_FF_THREAD_MODE);
5714
Ville Syrjälä3aad9052014-01-22 21:32:59 +02005715 /*
Ville Syrjälä46680e02014-01-22 21:33:01 +02005716 * WaVSThreadDispatchOverride:ivb,vlv
Ville Syrjälä3aad9052014-01-22 21:32:59 +02005717 *
5718 * This actually overrides the dispatch
5719 * mode for all thread types.
5720 */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005721 reg &= ~GEN7_FF_SCHED_MASK;
5722 reg |= GEN7_FF_TS_SCHED_HW;
5723 reg |= GEN7_FF_VS_SCHED_HW;
5724 reg |= GEN7_FF_DS_SCHED_HW;
5725
5726 I915_WRITE(GEN7_FF_THREAD_MODE, reg);
5727}
5728
Paulo Zanoni17a303e2012-11-20 15:12:07 -02005729static void lpt_init_clock_gating(struct drm_device *dev)
5730{
5731 struct drm_i915_private *dev_priv = dev->dev_private;
5732
5733 /*
5734 * TODO: this bit should only be enabled when really needed, then
5735 * disabled when not needed anymore in order to save power.
5736 */
5737 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE)
5738 I915_WRITE(SOUTH_DSPCLK_GATE_D,
5739 I915_READ(SOUTH_DSPCLK_GATE_D) |
5740 PCH_LP_PARTITION_LEVEL_DISABLE);
Paulo Zanoni0a790cd2013-04-17 18:15:49 -03005741
5742 /* WADPOClockGatingDisable:hsw */
5743 I915_WRITE(_TRANSA_CHICKEN1,
5744 I915_READ(_TRANSA_CHICKEN1) |
5745 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
Paulo Zanoni17a303e2012-11-20 15:12:07 -02005746}
5747
Imre Deak7d708ee2013-04-17 14:04:50 +03005748static void lpt_suspend_hw(struct drm_device *dev)
5749{
5750 struct drm_i915_private *dev_priv = dev->dev_private;
5751
5752 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
5753 uint32_t val = I915_READ(SOUTH_DSPCLK_GATE_D);
5754
5755 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
5756 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
5757 }
5758}
5759
Paulo Zanoni47c2bd92014-08-21 17:09:37 -03005760static void broadwell_init_clock_gating(struct drm_device *dev)
Ben Widawsky1020a5c2013-11-02 21:07:06 -07005761{
5762 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau07d27e22014-03-03 17:31:46 +00005763 enum pipe pipe;
Ben Widawsky1020a5c2013-11-02 21:07:06 -07005764
5765 I915_WRITE(WM3_LP_ILK, 0);
5766 I915_WRITE(WM2_LP_ILK, 0);
5767 I915_WRITE(WM1_LP_ILK, 0);
Ben Widawsky50ed5fb2013-11-02 21:07:40 -07005768
5769 /* FIXME(BDW): Check all the w/a, some might only apply to
5770 * pre-production hw. */
5771
Kenneth Graunkec8966e12014-02-26 23:59:30 -08005772
Ben Widawsky4afe8d32013-11-02 21:07:55 -07005773 I915_WRITE(GAMTARBMODE, _MASKED_BIT_ENABLE(ARB_MODE_BWGTLB_DISABLE));
5774
Ben Widawsky7f88da02013-11-02 21:07:58 -07005775 I915_WRITE(_3D_CHICKEN3,
Michel Thierryb3f9ad92014-07-07 12:40:17 +01005776 _MASKED_BIT_ENABLE(_3D_CHICKEN_SDE_LIMIT_FIFO_POLY_DEPTH(2)));
Ben Widawsky7f88da02013-11-02 21:07:58 -07005777
Ben Widawsky242a4012014-04-18 18:04:29 -03005778
Ben Widawskyab57fff2013-12-12 15:28:04 -08005779 /* WaSwitchSolVfFArbitrationPriority:bdw */
Ben Widawsky50ed5fb2013-11-02 21:07:40 -07005780 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07005781
Ben Widawskyab57fff2013-12-12 15:28:04 -08005782 /* WaPsrDPAMaskVBlankInSRD:bdw */
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07005783 I915_WRITE(CHICKEN_PAR1_1,
5784 I915_READ(CHICKEN_PAR1_1) | DPA_MASK_VBLANK_SRD);
5785
Ben Widawskyab57fff2013-12-12 15:28:04 -08005786 /* WaPsrDPRSUnmaskVBlankInSRD:bdw */
Damien Lespiau055e3932014-08-18 13:49:10 +01005787 for_each_pipe(dev_priv, pipe) {
Damien Lespiau07d27e22014-03-03 17:31:46 +00005788 I915_WRITE(CHICKEN_PIPESL_1(pipe),
Ville Syrjäläc7c65622014-03-05 13:05:45 +02005789 I915_READ(CHICKEN_PIPESL_1(pipe)) |
Ville Syrjälä8f670bb2014-03-05 13:05:47 +02005790 BDW_DPRS_MASK_VBLANK_SRD);
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07005791 }
Ben Widawsky63801f22013-12-12 17:26:03 -08005792
Ben Widawskyab57fff2013-12-12 15:28:04 -08005793 /* WaVSRefCountFullforceMissDisable:bdw */
5794 /* WaDSRefCountFullforceMissDisable:bdw */
5795 I915_WRITE(GEN7_FF_THREAD_MODE,
5796 I915_READ(GEN7_FF_THREAD_MODE) &
5797 ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
Ville Syrjälä36075a42014-02-04 21:59:21 +02005798
Ville Syrjälä295e8bb2014-02-27 21:59:01 +02005799 I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
5800 _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
Ville Syrjälä4f1ca9e2014-02-27 21:59:02 +02005801
5802 /* WaDisableSDEUnitClockGating:bdw */
5803 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
5804 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Damien Lespiau5d708682014-03-26 18:41:51 +00005805
Paulo Zanoni89d6b2b2014-08-21 17:09:36 -03005806 lpt_init_clock_gating(dev);
Ben Widawsky1020a5c2013-11-02 21:07:06 -07005807}
5808
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03005809static void haswell_init_clock_gating(struct drm_device *dev)
5810{
5811 struct drm_i915_private *dev_priv = dev->dev_private;
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03005812
Ville Syrjälä017636c2013-12-05 15:51:37 +02005813 ilk_init_lp_watermarks(dev);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03005814
Francisco Jerezf3fc4882013-10-02 15:53:16 -07005815 /* L3 caching of data atomics doesn't work -- disable it. */
5816 I915_WRITE(HSW_SCRATCH1, HSW_SCRATCH1_L3_DATA_ATOMICS_DISABLE);
5817 I915_WRITE(HSW_ROW_CHICKEN3,
5818 _MASKED_BIT_ENABLE(HSW_ROW_CHICKEN3_L3_GLOBAL_ATOMICS_DISABLE));
5819
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005820 /* This is required by WaCatErrorRejectionIssue:hsw */
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03005821 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
5822 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
5823 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
5824
Ville Syrjäläe36ea7f2014-01-22 21:33:00 +02005825 /* WaVSRefCountFullforceMissDisable:hsw */
5826 I915_WRITE(GEN7_FF_THREAD_MODE,
5827 I915_READ(GEN7_FF_THREAD_MODE) & ~GEN7_FF_VS_REF_CNT_FFME);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03005828
Akash Goel4e046322014-04-04 17:14:38 +05305829 /* WaDisable_RenderCache_OperationalFlush:hsw */
5830 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
5831
Chia-I Wufe27c602014-01-28 13:29:33 +08005832 /* enable HiZ Raw Stall Optimization */
5833 I915_WRITE(CACHE_MODE_0_GEN7,
5834 _MASKED_BIT_DISABLE(HIZ_RAW_STALL_OPT_DISABLE));
5835
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005836 /* WaDisable4x2SubspanOptimization:hsw */
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03005837 I915_WRITE(CACHE_MODE_1,
5838 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Eugeni Dodonov1544d9d2012-07-02 11:51:10 -03005839
Ville Syrjäläa12c4962014-02-04 21:59:20 +02005840 /*
5841 * BSpec recommends 8x4 when MSAA is used,
5842 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02005843 *
5844 * Note that PS/WM thread counts depend on the WIZ hashing
5845 * disable bit, which we don't touch here, but it's good
5846 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjäläa12c4962014-02-04 21:59:20 +02005847 */
5848 I915_WRITE(GEN7_GT_MODE,
5849 GEN6_WIZ_HASHING_MASK | GEN6_WIZ_HASHING_16x4);
5850
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005851 /* WaSwitchSolVfFArbitrationPriority:hsw */
Ben Widawskye3dff582013-03-20 14:49:14 -07005852 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
5853
Paulo Zanoni90a88642013-05-03 17:23:45 -03005854 /* WaRsPkgCStateDisplayPMReq:hsw */
5855 I915_WRITE(CHICKEN_PAR1_1,
5856 I915_READ(CHICKEN_PAR1_1) | FORCE_ARB_IDLE_PLANES);
Eugeni Dodonov1544d9d2012-07-02 11:51:10 -03005857
Paulo Zanoni17a303e2012-11-20 15:12:07 -02005858 lpt_init_clock_gating(dev);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03005859}
5860
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005861static void ivybridge_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005862{
5863 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawsky20848222012-05-04 18:58:59 -07005864 uint32_t snpcr;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005865
Ville Syrjälä017636c2013-12-05 15:51:37 +02005866 ilk_init_lp_watermarks(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005867
Damien Lespiau231e54f2012-10-19 17:55:41 +01005868 I915_WRITE(ILK_DSPCLK_GATE_D, ILK_VRHUNIT_CLOCK_GATE_DISABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005869
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005870 /* WaDisableEarlyCull:ivb */
Jesse Barnes87f80202012-10-02 17:43:41 -05005871 I915_WRITE(_3D_CHICKEN3,
5872 _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
5873
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005874 /* WaDisableBackToBackFlipFix:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005875 I915_WRITE(IVB_CHICKEN3,
5876 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
5877 CHICKEN3_DGMG_DONE_FIX_DISABLE);
5878
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005879 /* WaDisablePSDDualDispatchEnable:ivb */
Jesse Barnes12f33822012-10-25 12:15:45 -07005880 if (IS_IVB_GT1(dev))
5881 I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
5882 _MASKED_BIT_ENABLE(GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
Jesse Barnes12f33822012-10-25 12:15:45 -07005883
Akash Goel4e046322014-04-04 17:14:38 +05305884 /* WaDisable_RenderCache_OperationalFlush:ivb */
5885 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
5886
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005887 /* Apply the WaDisableRHWOOptimizationForRenderHang:ivb workaround. */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005888 I915_WRITE(GEN7_COMMON_SLICE_CHICKEN1,
5889 GEN7_CSC1_RHWO_OPT_DISABLE_IN_RCC);
5890
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005891 /* WaApplyL3ControlAndL3ChickenMode:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005892 I915_WRITE(GEN7_L3CNTLREG1,
5893 GEN7_WA_FOR_GEN7_L3_CONTROL);
5894 I915_WRITE(GEN7_L3_CHICKEN_MODE_REGISTER,
Jesse Barnes8ab43972012-10-25 12:15:42 -07005895 GEN7_WA_L3_CHICKEN_MODE);
5896 if (IS_IVB_GT1(dev))
5897 I915_WRITE(GEN7_ROW_CHICKEN2,
5898 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Ville Syrjälä412236c2014-01-22 21:32:44 +02005899 else {
5900 /* must write both registers */
5901 I915_WRITE(GEN7_ROW_CHICKEN2,
5902 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Jesse Barnes8ab43972012-10-25 12:15:42 -07005903 I915_WRITE(GEN7_ROW_CHICKEN2_GT2,
5904 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Ville Syrjälä412236c2014-01-22 21:32:44 +02005905 }
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005906
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005907 /* WaForceL3Serialization:ivb */
Jesse Barnes61939d92012-10-02 17:43:38 -05005908 I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
5909 ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
5910
Ville Syrjälä1b80a19a2014-01-22 21:32:53 +02005911 /*
Jesse Barnes0f846f82012-06-14 11:04:47 -07005912 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005913 * This implements the WaDisableRCZUnitClockGating:ivb workaround.
Jesse Barnes0f846f82012-06-14 11:04:47 -07005914 */
5915 I915_WRITE(GEN6_UCGCTL2,
Ville Syrjälä28acf3b2014-01-22 21:32:48 +02005916 GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
Jesse Barnes0f846f82012-06-14 11:04:47 -07005917
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005918 /* This is required by WaCatErrorRejectionIssue:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005919 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
5920 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
5921 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
5922
Ville Syrjälä0e088b82013-06-07 10:47:04 +03005923 g4x_disable_trickle_feed(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005924
5925 gen7_setup_fixed_func_scheduler(dev_priv);
Daniel Vetter97e19302012-04-24 16:00:21 +02005926
Chris Wilson22721342014-03-04 09:41:43 +00005927 if (0) { /* causes HiZ corruption on ivb:gt1 */
5928 /* enable HiZ Raw Stall Optimization */
5929 I915_WRITE(CACHE_MODE_0_GEN7,
5930 _MASKED_BIT_DISABLE(HIZ_RAW_STALL_OPT_DISABLE));
5931 }
Chia-I Wu116f2b62014-01-28 13:29:34 +08005932
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005933 /* WaDisable4x2SubspanOptimization:ivb */
Daniel Vetter97e19302012-04-24 16:00:21 +02005934 I915_WRITE(CACHE_MODE_1,
5935 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Ben Widawsky20848222012-05-04 18:58:59 -07005936
Ville Syrjäläa607c1a2014-02-04 21:59:19 +02005937 /*
5938 * BSpec recommends 8x4 when MSAA is used,
5939 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02005940 *
5941 * Note that PS/WM thread counts depend on the WIZ hashing
5942 * disable bit, which we don't touch here, but it's good
5943 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjäläa607c1a2014-02-04 21:59:19 +02005944 */
5945 I915_WRITE(GEN7_GT_MODE,
5946 GEN6_WIZ_HASHING_MASK | GEN6_WIZ_HASHING_16x4);
5947
Ben Widawsky20848222012-05-04 18:58:59 -07005948 snpcr = I915_READ(GEN6_MBCUNIT_SNPCR);
5949 snpcr &= ~GEN6_MBC_SNPCR_MASK;
5950 snpcr |= GEN6_MBC_SNPCR_MED;
5951 I915_WRITE(GEN6_MBCUNIT_SNPCR, snpcr);
Daniel Vetter3107bd42012-10-31 22:52:31 +01005952
Ben Widawskyab5c6082013-04-05 13:12:41 -07005953 if (!HAS_PCH_NOP(dev))
5954 cpt_init_clock_gating(dev);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01005955
5956 gen6_check_mch_setup(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005957}
5958
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005959static void valleyview_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005960{
5961 struct drm_i915_private *dev_priv = dev->dev_private;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005962
Ville Syrjäläd7fe0cc2013-05-21 18:01:50 +03005963 I915_WRITE(DSPCLK_GATE_D, VRHUNIT_CLOCK_GATE_DISABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005964
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005965 /* WaDisableEarlyCull:vlv */
Jesse Barnes87f80202012-10-02 17:43:41 -05005966 I915_WRITE(_3D_CHICKEN3,
5967 _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
5968
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005969 /* WaDisableBackToBackFlipFix:vlv */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005970 I915_WRITE(IVB_CHICKEN3,
5971 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
5972 CHICKEN3_DGMG_DONE_FIX_DISABLE);
5973
Ville Syrjäläfad7d362014-01-22 21:32:39 +02005974 /* WaPsdDispatchEnable:vlv */
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005975 /* WaDisablePSDDualDispatchEnable:vlv */
Jesse Barnes12f33822012-10-25 12:15:45 -07005976 I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
Jesse Barnesd3bc0302013-03-08 10:45:51 -08005977 _MASKED_BIT_ENABLE(GEN7_MAX_PS_THREAD_DEP |
5978 GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
Jesse Barnes12f33822012-10-25 12:15:45 -07005979
Akash Goel4e046322014-04-04 17:14:38 +05305980 /* WaDisable_RenderCache_OperationalFlush:vlv */
5981 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
5982
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005983 /* WaForceL3Serialization:vlv */
Jesse Barnes61939d92012-10-02 17:43:38 -05005984 I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
5985 ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
5986
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005987 /* WaDisableDopClockGating:vlv */
Jesse Barnes8ab43972012-10-25 12:15:42 -07005988 I915_WRITE(GEN7_ROW_CHICKEN2,
5989 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
5990
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005991 /* This is required by WaCatErrorRejectionIssue:vlv */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005992 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
5993 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
5994 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
5995
Ville Syrjälä46680e02014-01-22 21:33:01 +02005996 gen7_setup_fixed_func_scheduler(dev_priv);
5997
Ville Syrjälä3c0edae2014-01-22 21:32:56 +02005998 /*
Jesse Barnes0f846f82012-06-14 11:04:47 -07005999 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006000 * This implements the WaDisableRCZUnitClockGating:vlv workaround.
Jesse Barnes0f846f82012-06-14 11:04:47 -07006001 */
6002 I915_WRITE(GEN6_UCGCTL2,
Ville Syrjälä3c0edae2014-01-22 21:32:56 +02006003 GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
Jesse Barnes0f846f82012-06-14 11:04:47 -07006004
Akash Goelc98f5062014-03-24 23:00:07 +05306005 /* WaDisableL3Bank2xClockGate:vlv
6006 * Disabling L3 clock gating- MMIO 940c[25] = 1
6007 * Set bit 25, to disable L3_BANK_2x_CLK_GATING */
6008 I915_WRITE(GEN7_UCGCTL4,
6009 I915_READ(GEN7_UCGCTL4) | GEN7_L3BANK2X_CLOCK_GATE_DISABLE);
Jesse Barnese3f33d42012-06-14 11:04:50 -07006010
Ville Syrjäläe0d8d592013-06-12 22:11:18 +03006011 I915_WRITE(MI_ARB_VLV, MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006012
Ville Syrjäläafd58e72014-01-22 21:33:03 +02006013 /*
6014 * BSpec says this must be set, even though
6015 * WaDisable4x2SubspanOptimization isn't listed for VLV.
6016 */
Daniel Vetter6b26c862012-04-24 14:04:12 +02006017 I915_WRITE(CACHE_MODE_1,
6018 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Jesse Barnes79831172012-06-20 10:53:12 -07006019
6020 /*
Ville Syrjälä031994e2014-01-22 21:32:46 +02006021 * WaIncreaseL3CreditsForVLVB0:vlv
6022 * This is the hardware default actually.
6023 */
6024 I915_WRITE(GEN7_L3SQCREG1, VLV_B0_WA_L3SQCREG1_VALUE);
6025
6026 /*
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006027 * WaDisableVLVClockGating_VBIIssue:vlv
Jesse Barnes2d809572012-10-25 12:15:44 -07006028 * Disable clock gating on th GCFG unit to prevent a delay
6029 * in the reporting of vblank events.
6030 */
Ville Syrjälä7a0d1ee2014-01-22 21:33:04 +02006031 I915_WRITE(VLV_GUNIT_CLOCK_GATE, GCFG_DIS);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006032}
6033
Ville Syrjäläa4565da2014-04-09 13:28:10 +03006034static void cherryview_init_clock_gating(struct drm_device *dev)
6035{
6036 struct drm_i915_private *dev_priv = dev->dev_private;
6037
6038 I915_WRITE(DSPCLK_GATE_D, VRHUNIT_CLOCK_GATE_DISABLE);
6039
6040 I915_WRITE(MI_ARB_VLV, MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE);
Ville Syrjälädd811e72014-04-09 13:28:33 +03006041
Ville Syrjälä232ce332014-04-09 13:28:35 +03006042 /* WaVSRefCountFullforceMissDisable:chv */
6043 /* WaDSRefCountFullforceMissDisable:chv */
6044 I915_WRITE(GEN7_FF_THREAD_MODE,
6045 I915_READ(GEN7_FF_THREAD_MODE) &
6046 ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
Ville Syrjäläacea6f92014-04-09 13:28:36 +03006047
6048 /* WaDisableSemaphoreAndSyncFlipWait:chv */
6049 I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
6050 _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
Ville Syrjälä08466972014-04-09 13:28:37 +03006051
6052 /* WaDisableCSUnitClockGating:chv */
6053 I915_WRITE(GEN6_UCGCTL1, I915_READ(GEN6_UCGCTL1) |
6054 GEN6_CSUNIT_CLOCK_GATE_DISABLE);
Ville Syrjäläc6317802014-04-09 13:28:38 +03006055
6056 /* WaDisableSDEUnitClockGating:chv */
6057 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
6058 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Rafael Barbalhoe0d34ce2014-04-09 13:28:40 +03006059
Ville Syrjäläe4443e42014-04-09 13:28:41 +03006060 /* WaDisableGunitClockGating:chv (pre-production hw) */
6061 I915_WRITE(VLV_GUNIT_CLOCK_GATE, I915_READ(VLV_GUNIT_CLOCK_GATE) |
6062 GINT_DIS);
6063
6064 /* WaDisableFfDopClockGating:chv (pre-production hw) */
6065 I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
6066 _MASKED_BIT_ENABLE(GEN8_FF_DOP_CLOCK_GATE_DISABLE));
6067
6068 /* WaDisableDopClockGating:chv (pre-production hw) */
Ville Syrjäläe4443e42014-04-09 13:28:41 +03006069 I915_WRITE(GEN6_UCGCTL1, I915_READ(GEN6_UCGCTL1) |
6070 GEN6_EU_TCUNIT_CLOCK_GATE_DISABLE);
Ville Syrjäläa4565da2014-04-09 13:28:10 +03006071}
6072
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006073static void g4x_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006074{
6075 struct drm_i915_private *dev_priv = dev->dev_private;
6076 uint32_t dspclk_gate;
6077
6078 I915_WRITE(RENCLK_GATE_D1, 0);
6079 I915_WRITE(RENCLK_GATE_D2, VF_UNIT_CLOCK_GATE_DISABLE |
6080 GS_UNIT_CLOCK_GATE_DISABLE |
6081 CL_UNIT_CLOCK_GATE_DISABLE);
6082 I915_WRITE(RAMCLK_GATE_D, 0);
6083 dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE |
6084 OVRUNIT_CLOCK_GATE_DISABLE |
6085 OVCUNIT_CLOCK_GATE_DISABLE;
6086 if (IS_GM45(dev))
6087 dspclk_gate |= DSSUNIT_CLOCK_GATE_DISABLE;
6088 I915_WRITE(DSPCLK_GATE_D, dspclk_gate);
Daniel Vetter4358a372012-10-18 11:49:51 +02006089
6090 /* WaDisableRenderCachePipelinedFlush */
6091 I915_WRITE(CACHE_MODE_0,
6092 _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
Ville Syrjäläde1aa622013-06-07 10:47:01 +03006093
Akash Goel4e046322014-04-04 17:14:38 +05306094 /* WaDisable_RenderCache_OperationalFlush:g4x */
6095 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
6096
Ville Syrjälä0e088b82013-06-07 10:47:04 +03006097 g4x_disable_trickle_feed(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006098}
6099
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006100static void crestline_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006101{
6102 struct drm_i915_private *dev_priv = dev->dev_private;
6103
6104 I915_WRITE(RENCLK_GATE_D1, I965_RCC_CLOCK_GATE_DISABLE);
6105 I915_WRITE(RENCLK_GATE_D2, 0);
6106 I915_WRITE(DSPCLK_GATE_D, 0);
6107 I915_WRITE(RAMCLK_GATE_D, 0);
6108 I915_WRITE16(DEUC, 0);
Ville Syrjälä20f94962013-06-07 10:47:02 +03006109 I915_WRITE(MI_ARB_STATE,
6110 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Akash Goel4e046322014-04-04 17:14:38 +05306111
6112 /* WaDisable_RenderCache_OperationalFlush:gen4 */
6113 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006114}
6115
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006116static void broadwater_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006117{
6118 struct drm_i915_private *dev_priv = dev->dev_private;
6119
6120 I915_WRITE(RENCLK_GATE_D1, I965_RCZ_CLOCK_GATE_DISABLE |
6121 I965_RCC_CLOCK_GATE_DISABLE |
6122 I965_RCPB_CLOCK_GATE_DISABLE |
6123 I965_ISC_CLOCK_GATE_DISABLE |
6124 I965_FBC_CLOCK_GATE_DISABLE);
6125 I915_WRITE(RENCLK_GATE_D2, 0);
Ville Syrjälä20f94962013-06-07 10:47:02 +03006126 I915_WRITE(MI_ARB_STATE,
6127 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Akash Goel4e046322014-04-04 17:14:38 +05306128
6129 /* WaDisable_RenderCache_OperationalFlush:gen4 */
6130 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006131}
6132
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006133static void gen3_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006134{
6135 struct drm_i915_private *dev_priv = dev->dev_private;
6136 u32 dstate = I915_READ(D_STATE);
6137
6138 dstate |= DSTATE_PLL_D3_OFF | DSTATE_GFX_CLOCK_GATING |
6139 DSTATE_DOT_CLOCK_GATING;
6140 I915_WRITE(D_STATE, dstate);
Chris Wilson13a86b82012-04-24 14:51:43 +01006141
6142 if (IS_PINEVIEW(dev))
6143 I915_WRITE(ECOSKPD, _MASKED_BIT_ENABLE(ECO_GATING_CX_ONLY));
Daniel Vetter974a3b02012-09-09 11:54:16 +02006144
6145 /* IIR "flip pending" means done if this bit is set */
6146 I915_WRITE(ECOSKPD, _MASKED_BIT_DISABLE(ECO_FLIP_DONE));
Ville Syrjälä12fabbcb92014-02-25 15:13:38 +02006147
6148 /* interrupts should cause a wake up from C3 */
Ville Syrjälä32992542014-02-25 15:13:39 +02006149 I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_AGPBUSY_INT_EN));
Ville Syrjälädbb42742014-02-25 15:13:41 +02006150
6151 /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
6152 I915_WRITE(MI_ARB_STATE, _MASKED_BIT_ENABLE(MI_ARB_C3_LP_WRITE_ENABLE));
Ville Syrjälä10383922014-08-15 01:21:54 +03006153
6154 I915_WRITE(MI_ARB_STATE,
6155 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006156}
6157
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006158static void i85x_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006159{
6160 struct drm_i915_private *dev_priv = dev->dev_private;
6161
6162 I915_WRITE(RENCLK_GATE_D1, SV_CLOCK_GATE_DISABLE);
Ville Syrjälä54e472a2014-02-25 15:13:40 +02006163
6164 /* interrupts should cause a wake up from C3 */
6165 I915_WRITE(MI_STATE, _MASKED_BIT_ENABLE(MI_AGPBUSY_INT_EN) |
6166 _MASKED_BIT_DISABLE(MI_AGPBUSY_830_MODE));
Ville Syrjälä10383922014-08-15 01:21:54 +03006167
6168 I915_WRITE(MEM_MODE,
6169 _MASKED_BIT_ENABLE(MEM_DISPLAY_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006170}
6171
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006172static void i830_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006173{
6174 struct drm_i915_private *dev_priv = dev->dev_private;
6175
6176 I915_WRITE(DSPCLK_GATE_D, OVRUNIT_CLOCK_GATE_DISABLE);
Ville Syrjälä10383922014-08-15 01:21:54 +03006177
6178 I915_WRITE(MEM_MODE,
6179 _MASKED_BIT_ENABLE(MEM_DISPLAY_A_TRICKLE_FEED_DISABLE) |
6180 _MASKED_BIT_ENABLE(MEM_DISPLAY_B_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006181}
6182
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006183void intel_init_clock_gating(struct drm_device *dev)
6184{
6185 struct drm_i915_private *dev_priv = dev->dev_private;
6186
6187 dev_priv->display.init_clock_gating(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006188}
6189
Imre Deak7d708ee2013-04-17 14:04:50 +03006190void intel_suspend_hw(struct drm_device *dev)
6191{
6192 if (HAS_PCH_LPT(dev))
6193 lpt_suspend_hw(dev);
6194}
6195
Imre Deakc1ca7272013-11-25 17:15:29 +02006196#define for_each_power_well(i, power_well, domain_mask, power_domains) \
6197 for (i = 0; \
6198 i < (power_domains)->power_well_count && \
6199 ((power_well) = &(power_domains)->power_wells[i]); \
6200 i++) \
6201 if ((power_well)->domains & (domain_mask))
6202
6203#define for_each_power_well_rev(i, power_well, domain_mask, power_domains) \
6204 for (i = (power_domains)->power_well_count - 1; \
6205 i >= 0 && ((power_well) = &(power_domains)->power_wells[i]);\
6206 i--) \
6207 if ((power_well)->domains & (domain_mask))
6208
Paulo Zanoni15d199e2013-03-22 14:14:13 -03006209/**
6210 * We should only use the power well if we explicitly asked the hardware to
6211 * enable it, so check if it's enabled and also check if we've requested it to
6212 * be enabled.
6213 */
Imre Deakda7e29b2014-02-18 00:02:02 +02006214static bool hsw_power_well_enabled(struct drm_i915_private *dev_priv,
Imre Deakc1ca7272013-11-25 17:15:29 +02006215 struct i915_power_well *power_well)
6216{
Imre Deakc1ca7272013-11-25 17:15:29 +02006217 return I915_READ(HSW_PWR_WELL_DRIVER) ==
6218 (HSW_PWR_WELL_ENABLE_REQUEST | HSW_PWR_WELL_STATE_ENABLED);
6219}
6220
Imre Deakbfafe932014-06-05 20:31:47 +03006221bool intel_display_power_enabled_unlocked(struct drm_i915_private *dev_priv,
6222 enum intel_display_power_domain domain)
Imre Deakddf9c532013-11-27 22:02:02 +02006223{
Imre Deakddf9c532013-11-27 22:02:02 +02006224 struct i915_power_domains *power_domains;
Imre Deakb8c000d2014-06-02 14:21:10 +03006225 struct i915_power_well *power_well;
6226 bool is_enabled;
6227 int i;
6228
6229 if (dev_priv->pm.suspended)
6230 return false;
Imre Deakddf9c532013-11-27 22:02:02 +02006231
6232 power_domains = &dev_priv->power_domains;
Imre Deakbfafe932014-06-05 20:31:47 +03006233
Imre Deakb8c000d2014-06-02 14:21:10 +03006234 is_enabled = true;
Imre Deakbfafe932014-06-05 20:31:47 +03006235
Imre Deakb8c000d2014-06-02 14:21:10 +03006236 for_each_power_well_rev(i, power_well, BIT(domain), power_domains) {
6237 if (power_well->always_on)
6238 continue;
Imre Deakddf9c532013-11-27 22:02:02 +02006239
Imre Deakbfafe932014-06-05 20:31:47 +03006240 if (!power_well->hw_enabled) {
Imre Deakb8c000d2014-06-02 14:21:10 +03006241 is_enabled = false;
6242 break;
6243 }
6244 }
Imre Deakbfafe932014-06-05 20:31:47 +03006245
Imre Deakb8c000d2014-06-02 14:21:10 +03006246 return is_enabled;
Imre Deakddf9c532013-11-27 22:02:02 +02006247}
6248
Imre Deakda7e29b2014-02-18 00:02:02 +02006249bool intel_display_power_enabled(struct drm_i915_private *dev_priv,
Paulo Zanonib97186f2013-05-03 12:15:36 -03006250 enum intel_display_power_domain domain)
Paulo Zanoni15d199e2013-03-22 14:14:13 -03006251{
Imre Deakc1ca7272013-11-25 17:15:29 +02006252 struct i915_power_domains *power_domains;
Imre Deakbfafe932014-06-05 20:31:47 +03006253 bool ret;
Paulo Zanoni882244a2014-04-01 14:55:12 -03006254
Imre Deakc1ca7272013-11-25 17:15:29 +02006255 power_domains = &dev_priv->power_domains;
6256
Imre Deakc1ca7272013-11-25 17:15:29 +02006257 mutex_lock(&power_domains->lock);
Imre Deakbfafe932014-06-05 20:31:47 +03006258 ret = intel_display_power_enabled_unlocked(dev_priv, domain);
Imre Deakc1ca7272013-11-25 17:15:29 +02006259 mutex_unlock(&power_domains->lock);
6260
Imre Deakbfafe932014-06-05 20:31:47 +03006261 return ret;
Paulo Zanoni15d199e2013-03-22 14:14:13 -03006262}
6263
Imre Deak93c73e82014-02-18 00:02:19 +02006264/*
6265 * Starting with Haswell, we have a "Power Down Well" that can be turned off
6266 * when not needed anymore. We have 4 registers that can request the power well
6267 * to be enabled, and it will only be disabled if none of the registers is
6268 * requesting it to be enabled.
6269 */
Paulo Zanonid5e8fdc2013-12-11 18:50:09 -02006270static void hsw_power_well_post_enable(struct drm_i915_private *dev_priv)
6271{
6272 struct drm_device *dev = dev_priv->dev;
Paulo Zanonid5e8fdc2013-12-11 18:50:09 -02006273
Paulo Zanonif9dcb0d2013-12-11 18:50:10 -02006274 /*
6275 * After we re-enable the power well, if we touch VGA register 0x3d5
6276 * we'll get unclaimed register interrupts. This stops after we write
6277 * anything to the VGA MSR register. The vgacon module uses this
6278 * register all the time, so if we unbind our driver and, as a
6279 * consequence, bind vgacon, we'll get stuck in an infinite loop at
6280 * console_unlock(). So make here we touch the VGA MSR register, making
6281 * sure vgacon can keep working normally without triggering interrupts
6282 * and error messages.
6283 */
6284 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
6285 outb(inb(VGA_MSR_READ), VGA_MSR_WRITE);
6286 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
6287
Satheeshakrishna M08524a9f2014-04-08 15:46:56 +05306288 if (IS_BROADWELL(dev) || (INTEL_INFO(dev)->gen >= 9))
Paulo Zanonid49bdb02014-07-04 11:50:31 -03006289 gen8_irq_power_well_post_enable(dev_priv);
Paulo Zanonid5e8fdc2013-12-11 18:50:09 -02006290}
6291
Imre Deakda7e29b2014-02-18 00:02:02 +02006292static void hsw_set_power_well(struct drm_i915_private *dev_priv,
Imre Deakc1ca7272013-11-25 17:15:29 +02006293 struct i915_power_well *power_well, bool enable)
Eugeni Dodonovd0d3e512012-05-09 15:37:16 -03006294{
Paulo Zanonifa42e232013-01-25 16:59:11 -02006295 bool is_enabled, enable_requested;
6296 uint32_t tmp;
Eugeni Dodonovd0d3e512012-05-09 15:37:16 -03006297
Paulo Zanonifa42e232013-01-25 16:59:11 -02006298 tmp = I915_READ(HSW_PWR_WELL_DRIVER);
Paulo Zanoni6aedd1f2013-08-02 16:22:25 -03006299 is_enabled = tmp & HSW_PWR_WELL_STATE_ENABLED;
6300 enable_requested = tmp & HSW_PWR_WELL_ENABLE_REQUEST;
Eugeni Dodonovd0d3e512012-05-09 15:37:16 -03006301
Paulo Zanonifa42e232013-01-25 16:59:11 -02006302 if (enable) {
6303 if (!enable_requested)
Paulo Zanoni6aedd1f2013-08-02 16:22:25 -03006304 I915_WRITE(HSW_PWR_WELL_DRIVER,
6305 HSW_PWR_WELL_ENABLE_REQUEST);
Eugeni Dodonovd0d3e512012-05-09 15:37:16 -03006306
Paulo Zanonifa42e232013-01-25 16:59:11 -02006307 if (!is_enabled) {
6308 DRM_DEBUG_KMS("Enabling power well\n");
6309 if (wait_for((I915_READ(HSW_PWR_WELL_DRIVER) &
Paulo Zanoni6aedd1f2013-08-02 16:22:25 -03006310 HSW_PWR_WELL_STATE_ENABLED), 20))
Paulo Zanonifa42e232013-01-25 16:59:11 -02006311 DRM_ERROR("Timeout enabling power well\n");
6312 }
Ben Widawsky596cc112013-11-11 14:46:28 -08006313
Paulo Zanonid5e8fdc2013-12-11 18:50:09 -02006314 hsw_power_well_post_enable(dev_priv);
Paulo Zanonifa42e232013-01-25 16:59:11 -02006315 } else {
6316 if (enable_requested) {
6317 I915_WRITE(HSW_PWR_WELL_DRIVER, 0);
Paulo Zanoni9dbd8fe2013-07-23 10:48:11 -03006318 POSTING_READ(HSW_PWR_WELL_DRIVER);
Paulo Zanonifa42e232013-01-25 16:59:11 -02006319 DRM_DEBUG_KMS("Requesting to disable the power well\n");
Eugeni Dodonovd0d3e512012-05-09 15:37:16 -03006320 }
6321 }
Paulo Zanonifa42e232013-01-25 16:59:11 -02006322}
Eugeni Dodonovd0d3e512012-05-09 15:37:16 -03006323
Imre Deakc6cb5822014-03-04 19:22:55 +02006324static void hsw_power_well_sync_hw(struct drm_i915_private *dev_priv,
6325 struct i915_power_well *power_well)
6326{
6327 hsw_set_power_well(dev_priv, power_well, power_well->count > 0);
6328
6329 /*
6330 * We're taking over the BIOS, so clear any requests made by it since
6331 * the driver is in charge now.
6332 */
6333 if (I915_READ(HSW_PWR_WELL_BIOS) & HSW_PWR_WELL_ENABLE_REQUEST)
6334 I915_WRITE(HSW_PWR_WELL_BIOS, 0);
6335}
6336
6337static void hsw_power_well_enable(struct drm_i915_private *dev_priv,
6338 struct i915_power_well *power_well)
6339{
Imre Deakc6cb5822014-03-04 19:22:55 +02006340 hsw_set_power_well(dev_priv, power_well, true);
6341}
6342
6343static void hsw_power_well_disable(struct drm_i915_private *dev_priv,
6344 struct i915_power_well *power_well)
6345{
6346 hsw_set_power_well(dev_priv, power_well, false);
Imre Deakc6cb5822014-03-04 19:22:55 +02006347}
6348
Imre Deaka45f44662014-03-04 19:22:56 +02006349static void i9xx_always_on_power_well_noop(struct drm_i915_private *dev_priv,
6350 struct i915_power_well *power_well)
6351{
6352}
6353
6354static bool i9xx_always_on_power_well_enabled(struct drm_i915_private *dev_priv,
6355 struct i915_power_well *power_well)
6356{
6357 return true;
6358}
6359
Ville Syrjäläd2011dc2014-06-13 13:37:56 +03006360static void vlv_set_power_well(struct drm_i915_private *dev_priv,
6361 struct i915_power_well *power_well, bool enable)
Imre Deak77961eb2014-03-05 16:20:56 +02006362{
Ville Syrjäläd2011dc2014-06-13 13:37:56 +03006363 enum punit_power_well power_well_id = power_well->data;
Imre Deak77961eb2014-03-05 16:20:56 +02006364 u32 mask;
6365 u32 state;
6366 u32 ctrl;
6367
6368 mask = PUNIT_PWRGT_MASK(power_well_id);
6369 state = enable ? PUNIT_PWRGT_PWR_ON(power_well_id) :
6370 PUNIT_PWRGT_PWR_GATE(power_well_id);
6371
6372 mutex_lock(&dev_priv->rps.hw_lock);
6373
6374#define COND \
6375 ((vlv_punit_read(dev_priv, PUNIT_REG_PWRGT_STATUS) & mask) == state)
6376
6377 if (COND)
6378 goto out;
6379
6380 ctrl = vlv_punit_read(dev_priv, PUNIT_REG_PWRGT_CTRL);
6381 ctrl &= ~mask;
6382 ctrl |= state;
6383 vlv_punit_write(dev_priv, PUNIT_REG_PWRGT_CTRL, ctrl);
6384
6385 if (wait_for(COND, 100))
6386 DRM_ERROR("timout setting power well state %08x (%08x)\n",
6387 state,
6388 vlv_punit_read(dev_priv, PUNIT_REG_PWRGT_CTRL));
6389
6390#undef COND
6391
6392out:
6393 mutex_unlock(&dev_priv->rps.hw_lock);
6394}
6395
6396static void vlv_power_well_sync_hw(struct drm_i915_private *dev_priv,
6397 struct i915_power_well *power_well)
6398{
6399 vlv_set_power_well(dev_priv, power_well, power_well->count > 0);
6400}
6401
6402static void vlv_power_well_enable(struct drm_i915_private *dev_priv,
6403 struct i915_power_well *power_well)
6404{
6405 vlv_set_power_well(dev_priv, power_well, true);
6406}
6407
6408static void vlv_power_well_disable(struct drm_i915_private *dev_priv,
6409 struct i915_power_well *power_well)
6410{
6411 vlv_set_power_well(dev_priv, power_well, false);
6412}
6413
6414static bool vlv_power_well_enabled(struct drm_i915_private *dev_priv,
6415 struct i915_power_well *power_well)
6416{
6417 int power_well_id = power_well->data;
6418 bool enabled = false;
6419 u32 mask;
6420 u32 state;
6421 u32 ctrl;
6422
6423 mask = PUNIT_PWRGT_MASK(power_well_id);
6424 ctrl = PUNIT_PWRGT_PWR_ON(power_well_id);
6425
6426 mutex_lock(&dev_priv->rps.hw_lock);
6427
6428 state = vlv_punit_read(dev_priv, PUNIT_REG_PWRGT_STATUS) & mask;
6429 /*
6430 * We only ever set the power-on and power-gate states, anything
6431 * else is unexpected.
6432 */
6433 WARN_ON(state != PUNIT_PWRGT_PWR_ON(power_well_id) &&
6434 state != PUNIT_PWRGT_PWR_GATE(power_well_id));
6435 if (state == ctrl)
6436 enabled = true;
6437
6438 /*
6439 * A transient state at this point would mean some unexpected party
6440 * is poking at the power controls too.
6441 */
6442 ctrl = vlv_punit_read(dev_priv, PUNIT_REG_PWRGT_CTRL) & mask;
6443 WARN_ON(ctrl != state);
6444
6445 mutex_unlock(&dev_priv->rps.hw_lock);
6446
6447 return enabled;
6448}
6449
6450static void vlv_display_power_well_enable(struct drm_i915_private *dev_priv,
6451 struct i915_power_well *power_well)
6452{
6453 WARN_ON_ONCE(power_well->data != PUNIT_POWER_WELL_DISP2D);
6454
6455 vlv_set_power_well(dev_priv, power_well, true);
6456
6457 spin_lock_irq(&dev_priv->irq_lock);
6458 valleyview_enable_display_irqs(dev_priv);
6459 spin_unlock_irq(&dev_priv->irq_lock);
6460
6461 /*
Imre Deak0d116a22014-04-25 13:19:05 +03006462 * During driver initialization/resume we can avoid restoring the
6463 * part of the HW/SW state that will be inited anyway explicitly.
Imre Deak77961eb2014-03-05 16:20:56 +02006464 */
Imre Deak0d116a22014-04-25 13:19:05 +03006465 if (dev_priv->power_domains.initializing)
6466 return;
6467
6468 intel_hpd_init(dev_priv->dev);
Imre Deak77961eb2014-03-05 16:20:56 +02006469
6470 i915_redisable_vga_power_on(dev_priv->dev);
6471}
6472
6473static void vlv_display_power_well_disable(struct drm_i915_private *dev_priv,
6474 struct i915_power_well *power_well)
6475{
Imre Deak77961eb2014-03-05 16:20:56 +02006476 WARN_ON_ONCE(power_well->data != PUNIT_POWER_WELL_DISP2D);
6477
6478 spin_lock_irq(&dev_priv->irq_lock);
Imre Deak77961eb2014-03-05 16:20:56 +02006479 valleyview_disable_display_irqs(dev_priv);
6480 spin_unlock_irq(&dev_priv->irq_lock);
6481
Imre Deak77961eb2014-03-05 16:20:56 +02006482 vlv_set_power_well(dev_priv, power_well, false);
Ville Syrjälä773538e82014-09-04 14:54:56 +03006483
6484 vlv_power_sequencer_reset(dev_priv);
Imre Deak77961eb2014-03-05 16:20:56 +02006485}
6486
Ville Syrjäläaa519f22014-06-13 13:37:55 +03006487static void vlv_dpio_cmn_power_well_enable(struct drm_i915_private *dev_priv,
6488 struct i915_power_well *power_well)
6489{
6490 WARN_ON_ONCE(power_well->data != PUNIT_POWER_WELL_DPIO_CMN_BC);
6491
6492 /*
6493 * Enable the CRI clock source so we can get at the
6494 * display and the reference clock for VGA
6495 * hotplug / manual detection.
6496 */
6497 I915_WRITE(DPLL(PIPE_B), I915_READ(DPLL(PIPE_B)) |
6498 DPLL_REFA_CLK_ENABLE_VLV | DPLL_INTEGRATED_CRI_CLK_VLV);
6499 udelay(1); /* >10ns for cmnreset, >0ns for sidereset */
6500
6501 vlv_set_power_well(dev_priv, power_well, true);
6502
6503 /*
6504 * From VLV2A0_DP_eDP_DPIO_driver_vbios_notes_10.docx -
6505 * 6. De-assert cmn_reset/side_reset. Same as VLV X0.
6506 * a. GUnit 0x2110 bit[0] set to 1 (def 0)
6507 * b. The other bits such as sfr settings / modesel may all
6508 * be set to 0.
6509 *
6510 * This should only be done on init and resume from S3 with
6511 * both PLLs disabled, or we risk losing DPIO and PLL
6512 * synchronization.
6513 */
6514 I915_WRITE(DPIO_CTL, I915_READ(DPIO_CTL) | DPIO_CMNRST);
6515}
6516
6517static void vlv_dpio_cmn_power_well_disable(struct drm_i915_private *dev_priv,
6518 struct i915_power_well *power_well)
6519{
Ville Syrjäläaa519f22014-06-13 13:37:55 +03006520 enum pipe pipe;
6521
6522 WARN_ON_ONCE(power_well->data != PUNIT_POWER_WELL_DPIO_CMN_BC);
6523
Damien Lespiau055e3932014-08-18 13:49:10 +01006524 for_each_pipe(dev_priv, pipe)
Ville Syrjäläaa519f22014-06-13 13:37:55 +03006525 assert_pll_disabled(dev_priv, pipe);
6526
6527 /* Assert common reset */
6528 I915_WRITE(DPIO_CTL, I915_READ(DPIO_CTL) & ~DPIO_CMNRST);
6529
6530 vlv_set_power_well(dev_priv, power_well, false);
6531}
6532
Ville Syrjälä5d6f7ea2014-06-28 02:04:08 +03006533static void chv_dpio_cmn_power_well_enable(struct drm_i915_private *dev_priv,
6534 struct i915_power_well *power_well)
6535{
6536 enum dpio_phy phy;
6537
6538 WARN_ON_ONCE(power_well->data != PUNIT_POWER_WELL_DPIO_CMN_BC &&
6539 power_well->data != PUNIT_POWER_WELL_DPIO_CMN_D);
6540
6541 /*
6542 * Enable the CRI clock source so we can get at the
6543 * display and the reference clock for VGA
6544 * hotplug / manual detection.
6545 */
6546 if (power_well->data == PUNIT_POWER_WELL_DPIO_CMN_BC) {
6547 phy = DPIO_PHY0;
6548 I915_WRITE(DPLL(PIPE_B), I915_READ(DPLL(PIPE_B)) |
6549 DPLL_REFA_CLK_ENABLE_VLV);
6550 I915_WRITE(DPLL(PIPE_B), I915_READ(DPLL(PIPE_B)) |
6551 DPLL_REFA_CLK_ENABLE_VLV | DPLL_INTEGRATED_CRI_CLK_VLV);
6552 } else {
6553 phy = DPIO_PHY1;
6554 I915_WRITE(DPLL(PIPE_C), I915_READ(DPLL(PIPE_C)) |
6555 DPLL_REFA_CLK_ENABLE_VLV | DPLL_INTEGRATED_CRI_CLK_VLV);
6556 }
6557 udelay(1); /* >10ns for cmnreset, >0ns for sidereset */
6558 vlv_set_power_well(dev_priv, power_well, true);
6559
6560 /* Poll for phypwrgood signal */
6561 if (wait_for(I915_READ(DISPLAY_PHY_STATUS) & PHY_POWERGOOD(phy), 1))
6562 DRM_ERROR("Display PHY %d is not power up\n", phy);
6563
Ville Syrjäläefd814b2014-06-27 19:52:13 +03006564 I915_WRITE(DISPLAY_PHY_CONTROL, I915_READ(DISPLAY_PHY_CONTROL) |
6565 PHY_COM_LANE_RESET_DEASSERT(phy));
Ville Syrjälä5d6f7ea2014-06-28 02:04:08 +03006566}
6567
6568static void chv_dpio_cmn_power_well_disable(struct drm_i915_private *dev_priv,
6569 struct i915_power_well *power_well)
6570{
6571 enum dpio_phy phy;
6572
6573 WARN_ON_ONCE(power_well->data != PUNIT_POWER_WELL_DPIO_CMN_BC &&
6574 power_well->data != PUNIT_POWER_WELL_DPIO_CMN_D);
6575
6576 if (power_well->data == PUNIT_POWER_WELL_DPIO_CMN_BC) {
6577 phy = DPIO_PHY0;
6578 assert_pll_disabled(dev_priv, PIPE_A);
6579 assert_pll_disabled(dev_priv, PIPE_B);
6580 } else {
6581 phy = DPIO_PHY1;
6582 assert_pll_disabled(dev_priv, PIPE_C);
6583 }
6584
Ville Syrjäläefd814b2014-06-27 19:52:13 +03006585 I915_WRITE(DISPLAY_PHY_CONTROL, I915_READ(DISPLAY_PHY_CONTROL) &
6586 ~PHY_COM_LANE_RESET_DEASSERT(phy));
Ville Syrjälä5d6f7ea2014-06-28 02:04:08 +03006587
6588 vlv_set_power_well(dev_priv, power_well, false);
6589}
6590
Ville Syrjälä26972b02014-06-28 02:04:11 +03006591static bool chv_pipe_power_well_enabled(struct drm_i915_private *dev_priv,
6592 struct i915_power_well *power_well)
6593{
6594 enum pipe pipe = power_well->data;
6595 bool enabled;
6596 u32 state, ctrl;
6597
6598 mutex_lock(&dev_priv->rps.hw_lock);
6599
6600 state = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) & DP_SSS_MASK(pipe);
6601 /*
6602 * We only ever set the power-on and power-gate states, anything
6603 * else is unexpected.
6604 */
6605 WARN_ON(state != DP_SSS_PWR_ON(pipe) && state != DP_SSS_PWR_GATE(pipe));
6606 enabled = state == DP_SSS_PWR_ON(pipe);
6607
6608 /*
6609 * A transient state at this point would mean some unexpected party
6610 * is poking at the power controls too.
6611 */
6612 ctrl = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) & DP_SSC_MASK(pipe);
6613 WARN_ON(ctrl << 16 != state);
6614
6615 mutex_unlock(&dev_priv->rps.hw_lock);
6616
6617 return enabled;
6618}
6619
6620static void chv_set_pipe_power_well(struct drm_i915_private *dev_priv,
6621 struct i915_power_well *power_well,
6622 bool enable)
6623{
6624 enum pipe pipe = power_well->data;
6625 u32 state;
6626 u32 ctrl;
6627
6628 state = enable ? DP_SSS_PWR_ON(pipe) : DP_SSS_PWR_GATE(pipe);
6629
6630 mutex_lock(&dev_priv->rps.hw_lock);
6631
6632#define COND \
6633 ((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) & DP_SSS_MASK(pipe)) == state)
6634
6635 if (COND)
6636 goto out;
6637
6638 ctrl = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
6639 ctrl &= ~DP_SSC_MASK(pipe);
6640 ctrl |= enable ? DP_SSC_PWR_ON(pipe) : DP_SSC_PWR_GATE(pipe);
6641 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, ctrl);
6642
6643 if (wait_for(COND, 100))
6644 DRM_ERROR("timout setting power well state %08x (%08x)\n",
6645 state,
6646 vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ));
6647
6648#undef COND
6649
6650out:
6651 mutex_unlock(&dev_priv->rps.hw_lock);
6652}
6653
6654static void chv_pipe_power_well_sync_hw(struct drm_i915_private *dev_priv,
6655 struct i915_power_well *power_well)
6656{
6657 chv_set_pipe_power_well(dev_priv, power_well, power_well->count > 0);
6658}
6659
6660static void chv_pipe_power_well_enable(struct drm_i915_private *dev_priv,
6661 struct i915_power_well *power_well)
6662{
6663 WARN_ON_ONCE(power_well->data != PIPE_A &&
6664 power_well->data != PIPE_B &&
6665 power_well->data != PIPE_C);
6666
6667 chv_set_pipe_power_well(dev_priv, power_well, true);
6668}
6669
6670static void chv_pipe_power_well_disable(struct drm_i915_private *dev_priv,
6671 struct i915_power_well *power_well)
6672{
6673 WARN_ON_ONCE(power_well->data != PIPE_A &&
6674 power_well->data != PIPE_B &&
6675 power_well->data != PIPE_C);
6676
6677 chv_set_pipe_power_well(dev_priv, power_well, false);
6678}
6679
Imre Deak25eaa002014-03-04 19:23:06 +02006680static void check_power_well_state(struct drm_i915_private *dev_priv,
6681 struct i915_power_well *power_well)
6682{
6683 bool enabled = power_well->ops->is_enabled(dev_priv, power_well);
6684
6685 if (power_well->always_on || !i915.disable_power_well) {
6686 if (!enabled)
6687 goto mismatch;
6688
6689 return;
6690 }
6691
6692 if (enabled != (power_well->count > 0))
6693 goto mismatch;
6694
6695 return;
6696
6697mismatch:
6698 WARN(1, "state mismatch for '%s' (always_on %d hw state %d use-count %d disable_power_well %d\n",
6699 power_well->name, power_well->always_on, enabled,
6700 power_well->count, i915.disable_power_well);
6701}
6702
Imre Deakda7e29b2014-02-18 00:02:02 +02006703void intel_display_power_get(struct drm_i915_private *dev_priv,
Ville Syrjälä67656252013-09-16 17:38:28 +03006704 enum intel_display_power_domain domain)
6705{
Imre Deak83c00f52013-10-25 17:36:47 +03006706 struct i915_power_domains *power_domains;
Imre Deakc1ca7272013-11-25 17:15:29 +02006707 struct i915_power_well *power_well;
6708 int i;
Ville Syrjälä67656252013-09-16 17:38:28 +03006709
Paulo Zanoni9e6ea712014-03-07 20:08:06 -03006710 intel_runtime_pm_get(dev_priv);
6711
Imre Deak83c00f52013-10-25 17:36:47 +03006712 power_domains = &dev_priv->power_domains;
6713
6714 mutex_lock(&power_domains->lock);
Imre Deak1da51582013-11-25 17:15:35 +02006715
Imre Deak25eaa002014-03-04 19:23:06 +02006716 for_each_power_well(i, power_well, BIT(domain), power_domains) {
6717 if (!power_well->count++) {
6718 DRM_DEBUG_KMS("enabling %s\n", power_well->name);
Imre Deakc6cb5822014-03-04 19:22:55 +02006719 power_well->ops->enable(dev_priv, power_well);
Imre Deakbfafe932014-06-05 20:31:47 +03006720 power_well->hw_enabled = true;
Imre Deak25eaa002014-03-04 19:23:06 +02006721 }
6722
6723 check_power_well_state(dev_priv, power_well);
6724 }
Imre Deak1da51582013-11-25 17:15:35 +02006725
Imre Deakddf9c532013-11-27 22:02:02 +02006726 power_domains->domain_use_count[domain]++;
6727
Imre Deak83c00f52013-10-25 17:36:47 +03006728 mutex_unlock(&power_domains->lock);
Ville Syrjälä67656252013-09-16 17:38:28 +03006729}
6730
Imre Deakda7e29b2014-02-18 00:02:02 +02006731void intel_display_power_put(struct drm_i915_private *dev_priv,
Ville Syrjälä67656252013-09-16 17:38:28 +03006732 enum intel_display_power_domain domain)
6733{
Imre Deak83c00f52013-10-25 17:36:47 +03006734 struct i915_power_domains *power_domains;
Imre Deakc1ca7272013-11-25 17:15:29 +02006735 struct i915_power_well *power_well;
6736 int i;
Ville Syrjälä67656252013-09-16 17:38:28 +03006737
Imre Deak83c00f52013-10-25 17:36:47 +03006738 power_domains = &dev_priv->power_domains;
6739
6740 mutex_lock(&power_domains->lock);
Imre Deak1da51582013-11-25 17:15:35 +02006741
Imre Deak1da51582013-11-25 17:15:35 +02006742 WARN_ON(!power_domains->domain_use_count[domain]);
6743 power_domains->domain_use_count[domain]--;
Imre Deakddf9c532013-11-27 22:02:02 +02006744
Imre Deak70bf4072014-03-04 19:22:51 +02006745 for_each_power_well_rev(i, power_well, BIT(domain), power_domains) {
6746 WARN_ON(!power_well->count);
6747
Imre Deak25eaa002014-03-04 19:23:06 +02006748 if (!--power_well->count && i915.disable_power_well) {
6749 DRM_DEBUG_KMS("disabling %s\n", power_well->name);
Imre Deakbfafe932014-06-05 20:31:47 +03006750 power_well->hw_enabled = false;
Imre Deakc6cb5822014-03-04 19:22:55 +02006751 power_well->ops->disable(dev_priv, power_well);
Imre Deak25eaa002014-03-04 19:23:06 +02006752 }
6753
6754 check_power_well_state(dev_priv, power_well);
Imre Deak70bf4072014-03-04 19:22:51 +02006755 }
Imre Deak1da51582013-11-25 17:15:35 +02006756
Imre Deak83c00f52013-10-25 17:36:47 +03006757 mutex_unlock(&power_domains->lock);
Paulo Zanoni9e6ea712014-03-07 20:08:06 -03006758
6759 intel_runtime_pm_put(dev_priv);
Ville Syrjälä67656252013-09-16 17:38:28 +03006760}
6761
Imre Deak83c00f52013-10-25 17:36:47 +03006762static struct i915_power_domains *hsw_pwr;
Wang Xingchaoa38911a2013-05-30 22:07:11 +08006763
6764/* Display audio driver power well request */
Takashi Iwai74b0c2d2014-06-13 15:14:34 +02006765int i915_request_power_well(void)
Wang Xingchaoa38911a2013-05-30 22:07:11 +08006766{
Imre Deakb4ed4482013-10-25 17:36:49 +03006767 struct drm_i915_private *dev_priv;
6768
Takashi Iwai74b0c2d2014-06-13 15:14:34 +02006769 if (!hsw_pwr)
6770 return -ENODEV;
Wang Xingchaoa38911a2013-05-30 22:07:11 +08006771
Imre Deakb4ed4482013-10-25 17:36:49 +03006772 dev_priv = container_of(hsw_pwr, struct drm_i915_private,
6773 power_domains);
Imre Deakda7e29b2014-02-18 00:02:02 +02006774 intel_display_power_get(dev_priv, POWER_DOMAIN_AUDIO);
Takashi Iwai74b0c2d2014-06-13 15:14:34 +02006775 return 0;
Wang Xingchaoa38911a2013-05-30 22:07:11 +08006776}
6777EXPORT_SYMBOL_GPL(i915_request_power_well);
6778
6779/* Display audio driver power well release */
Takashi Iwai74b0c2d2014-06-13 15:14:34 +02006780int i915_release_power_well(void)
Wang Xingchaoa38911a2013-05-30 22:07:11 +08006781{
Imre Deakb4ed4482013-10-25 17:36:49 +03006782 struct drm_i915_private *dev_priv;
6783
Takashi Iwai74b0c2d2014-06-13 15:14:34 +02006784 if (!hsw_pwr)
6785 return -ENODEV;
Wang Xingchaoa38911a2013-05-30 22:07:11 +08006786
Imre Deakb4ed4482013-10-25 17:36:49 +03006787 dev_priv = container_of(hsw_pwr, struct drm_i915_private,
6788 power_domains);
Imre Deakda7e29b2014-02-18 00:02:02 +02006789 intel_display_power_put(dev_priv, POWER_DOMAIN_AUDIO);
Takashi Iwai74b0c2d2014-06-13 15:14:34 +02006790 return 0;
Wang Xingchaoa38911a2013-05-30 22:07:11 +08006791}
6792EXPORT_SYMBOL_GPL(i915_release_power_well);
6793
Jani Nikulac149dcb2014-07-04 10:00:37 +08006794/*
6795 * Private interface for the audio driver to get CDCLK in kHz.
6796 *
6797 * Caller must request power well using i915_request_power_well() prior to
6798 * making the call.
6799 */
6800int i915_get_cdclk_freq(void)
6801{
6802 struct drm_i915_private *dev_priv;
6803
6804 if (!hsw_pwr)
6805 return -ENODEV;
6806
6807 dev_priv = container_of(hsw_pwr, struct drm_i915_private,
6808 power_domains);
6809
6810 return intel_ddi_get_cdclk_freq(dev_priv);
6811}
6812EXPORT_SYMBOL_GPL(i915_get_cdclk_freq);
6813
6814
Imre Deakefcad912014-03-04 19:22:53 +02006815#define POWER_DOMAIN_MASK (BIT(POWER_DOMAIN_NUM) - 1)
6816
6817#define HSW_ALWAYS_ON_POWER_DOMAINS ( \
6818 BIT(POWER_DOMAIN_PIPE_A) | \
Imre Deakf5938f32014-03-04 19:22:54 +02006819 BIT(POWER_DOMAIN_TRANSCODER_EDP) | \
Imre Deak319be8a2014-03-04 19:22:57 +02006820 BIT(POWER_DOMAIN_PORT_DDI_A_2_LANES) | \
6821 BIT(POWER_DOMAIN_PORT_DDI_A_4_LANES) | \
6822 BIT(POWER_DOMAIN_PORT_DDI_B_2_LANES) | \
6823 BIT(POWER_DOMAIN_PORT_DDI_B_4_LANES) | \
6824 BIT(POWER_DOMAIN_PORT_DDI_C_2_LANES) | \
6825 BIT(POWER_DOMAIN_PORT_DDI_C_4_LANES) | \
6826 BIT(POWER_DOMAIN_PORT_DDI_D_2_LANES) | \
6827 BIT(POWER_DOMAIN_PORT_DDI_D_4_LANES) | \
6828 BIT(POWER_DOMAIN_PORT_CRT) | \
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -03006829 BIT(POWER_DOMAIN_PLLS) | \
Imre Deakf5938f32014-03-04 19:22:54 +02006830 BIT(POWER_DOMAIN_INIT))
Imre Deakefcad912014-03-04 19:22:53 +02006831#define HSW_DISPLAY_POWER_DOMAINS ( \
6832 (POWER_DOMAIN_MASK & ~HSW_ALWAYS_ON_POWER_DOMAINS) | \
6833 BIT(POWER_DOMAIN_INIT))
6834
6835#define BDW_ALWAYS_ON_POWER_DOMAINS ( \
6836 HSW_ALWAYS_ON_POWER_DOMAINS | \
6837 BIT(POWER_DOMAIN_PIPE_A_PANEL_FITTER))
6838#define BDW_DISPLAY_POWER_DOMAINS ( \
6839 (POWER_DOMAIN_MASK & ~BDW_ALWAYS_ON_POWER_DOMAINS) | \
6840 BIT(POWER_DOMAIN_INIT))
6841
Imre Deak77961eb2014-03-05 16:20:56 +02006842#define VLV_ALWAYS_ON_POWER_DOMAINS BIT(POWER_DOMAIN_INIT)
6843#define VLV_DISPLAY_POWER_DOMAINS POWER_DOMAIN_MASK
6844
6845#define VLV_DPIO_CMN_BC_POWER_DOMAINS ( \
6846 BIT(POWER_DOMAIN_PORT_DDI_B_2_LANES) | \
6847 BIT(POWER_DOMAIN_PORT_DDI_B_4_LANES) | \
6848 BIT(POWER_DOMAIN_PORT_DDI_C_2_LANES) | \
6849 BIT(POWER_DOMAIN_PORT_DDI_C_4_LANES) | \
6850 BIT(POWER_DOMAIN_PORT_CRT) | \
6851 BIT(POWER_DOMAIN_INIT))
6852
6853#define VLV_DPIO_TX_B_LANES_01_POWER_DOMAINS ( \
6854 BIT(POWER_DOMAIN_PORT_DDI_B_2_LANES) | \
6855 BIT(POWER_DOMAIN_PORT_DDI_B_4_LANES) | \
6856 BIT(POWER_DOMAIN_INIT))
6857
6858#define VLV_DPIO_TX_B_LANES_23_POWER_DOMAINS ( \
6859 BIT(POWER_DOMAIN_PORT_DDI_B_4_LANES) | \
6860 BIT(POWER_DOMAIN_INIT))
6861
6862#define VLV_DPIO_TX_C_LANES_01_POWER_DOMAINS ( \
6863 BIT(POWER_DOMAIN_PORT_DDI_C_2_LANES) | \
6864 BIT(POWER_DOMAIN_PORT_DDI_C_4_LANES) | \
6865 BIT(POWER_DOMAIN_INIT))
6866
6867#define VLV_DPIO_TX_C_LANES_23_POWER_DOMAINS ( \
6868 BIT(POWER_DOMAIN_PORT_DDI_C_4_LANES) | \
6869 BIT(POWER_DOMAIN_INIT))
6870
Ville Syrjälä26972b02014-06-28 02:04:11 +03006871#define CHV_PIPE_A_POWER_DOMAINS ( \
6872 BIT(POWER_DOMAIN_PIPE_A) | \
6873 BIT(POWER_DOMAIN_INIT))
6874
6875#define CHV_PIPE_B_POWER_DOMAINS ( \
6876 BIT(POWER_DOMAIN_PIPE_B) | \
6877 BIT(POWER_DOMAIN_INIT))
6878
6879#define CHV_PIPE_C_POWER_DOMAINS ( \
6880 BIT(POWER_DOMAIN_PIPE_C) | \
6881 BIT(POWER_DOMAIN_INIT))
6882
Ville Syrjälä5d6f7ea2014-06-28 02:04:08 +03006883#define CHV_DPIO_CMN_BC_POWER_DOMAINS ( \
6884 BIT(POWER_DOMAIN_PORT_DDI_B_2_LANES) | \
6885 BIT(POWER_DOMAIN_PORT_DDI_B_4_LANES) | \
6886 BIT(POWER_DOMAIN_PORT_DDI_C_2_LANES) | \
6887 BIT(POWER_DOMAIN_PORT_DDI_C_4_LANES) | \
6888 BIT(POWER_DOMAIN_INIT))
6889
6890#define CHV_DPIO_CMN_D_POWER_DOMAINS ( \
6891 BIT(POWER_DOMAIN_PORT_DDI_D_2_LANES) | \
6892 BIT(POWER_DOMAIN_PORT_DDI_D_4_LANES) | \
6893 BIT(POWER_DOMAIN_INIT))
6894
Ville Syrjälä2ce147f2014-06-28 02:04:13 +03006895#define CHV_DPIO_TX_D_LANES_01_POWER_DOMAINS ( \
6896 BIT(POWER_DOMAIN_PORT_DDI_D_2_LANES) | \
6897 BIT(POWER_DOMAIN_PORT_DDI_D_4_LANES) | \
6898 BIT(POWER_DOMAIN_INIT))
6899
6900#define CHV_DPIO_TX_D_LANES_23_POWER_DOMAINS ( \
6901 BIT(POWER_DOMAIN_PORT_DDI_D_4_LANES) | \
6902 BIT(POWER_DOMAIN_INIT))
6903
Imre Deaka45f44662014-03-04 19:22:56 +02006904static const struct i915_power_well_ops i9xx_always_on_power_well_ops = {
6905 .sync_hw = i9xx_always_on_power_well_noop,
6906 .enable = i9xx_always_on_power_well_noop,
6907 .disable = i9xx_always_on_power_well_noop,
6908 .is_enabled = i9xx_always_on_power_well_enabled,
6909};
Imre Deakc6cb5822014-03-04 19:22:55 +02006910
Ville Syrjälä26972b02014-06-28 02:04:11 +03006911static const struct i915_power_well_ops chv_pipe_power_well_ops = {
6912 .sync_hw = chv_pipe_power_well_sync_hw,
6913 .enable = chv_pipe_power_well_enable,
6914 .disable = chv_pipe_power_well_disable,
6915 .is_enabled = chv_pipe_power_well_enabled,
6916};
6917
Ville Syrjälä5d6f7ea2014-06-28 02:04:08 +03006918static const struct i915_power_well_ops chv_dpio_cmn_power_well_ops = {
6919 .sync_hw = vlv_power_well_sync_hw,
6920 .enable = chv_dpio_cmn_power_well_enable,
6921 .disable = chv_dpio_cmn_power_well_disable,
6922 .is_enabled = vlv_power_well_enabled,
6923};
6924
Imre Deak1c2256d2013-11-25 17:15:34 +02006925static struct i915_power_well i9xx_always_on_power_well[] = {
6926 {
6927 .name = "always-on",
6928 .always_on = 1,
6929 .domains = POWER_DOMAIN_MASK,
Imre Deakc6cb5822014-03-04 19:22:55 +02006930 .ops = &i9xx_always_on_power_well_ops,
Imre Deak1c2256d2013-11-25 17:15:34 +02006931 },
6932};
6933
Imre Deakc6cb5822014-03-04 19:22:55 +02006934static const struct i915_power_well_ops hsw_power_well_ops = {
6935 .sync_hw = hsw_power_well_sync_hw,
6936 .enable = hsw_power_well_enable,
6937 .disable = hsw_power_well_disable,
6938 .is_enabled = hsw_power_well_enabled,
6939};
6940
Imre Deakc1ca7272013-11-25 17:15:29 +02006941static struct i915_power_well hsw_power_wells[] = {
6942 {
Imre Deak6f3ef5d2013-11-25 17:15:30 +02006943 .name = "always-on",
6944 .always_on = 1,
6945 .domains = HSW_ALWAYS_ON_POWER_DOMAINS,
Imre Deakc6cb5822014-03-04 19:22:55 +02006946 .ops = &i9xx_always_on_power_well_ops,
Imre Deak6f3ef5d2013-11-25 17:15:30 +02006947 },
6948 {
Imre Deakc1ca7272013-11-25 17:15:29 +02006949 .name = "display",
Imre Deakefcad912014-03-04 19:22:53 +02006950 .domains = HSW_DISPLAY_POWER_DOMAINS,
Imre Deakc6cb5822014-03-04 19:22:55 +02006951 .ops = &hsw_power_well_ops,
Imre Deakc1ca7272013-11-25 17:15:29 +02006952 },
6953};
6954
6955static struct i915_power_well bdw_power_wells[] = {
6956 {
Imre Deak6f3ef5d2013-11-25 17:15:30 +02006957 .name = "always-on",
6958 .always_on = 1,
6959 .domains = BDW_ALWAYS_ON_POWER_DOMAINS,
Imre Deakc6cb5822014-03-04 19:22:55 +02006960 .ops = &i9xx_always_on_power_well_ops,
Imre Deak6f3ef5d2013-11-25 17:15:30 +02006961 },
6962 {
Imre Deakc1ca7272013-11-25 17:15:29 +02006963 .name = "display",
Imre Deakefcad912014-03-04 19:22:53 +02006964 .domains = BDW_DISPLAY_POWER_DOMAINS,
Imre Deakc6cb5822014-03-04 19:22:55 +02006965 .ops = &hsw_power_well_ops,
Imre Deakc1ca7272013-11-25 17:15:29 +02006966 },
6967};
6968
Imre Deak77961eb2014-03-05 16:20:56 +02006969static const struct i915_power_well_ops vlv_display_power_well_ops = {
6970 .sync_hw = vlv_power_well_sync_hw,
6971 .enable = vlv_display_power_well_enable,
6972 .disable = vlv_display_power_well_disable,
6973 .is_enabled = vlv_power_well_enabled,
6974};
6975
Ville Syrjäläaa519f22014-06-13 13:37:55 +03006976static const struct i915_power_well_ops vlv_dpio_cmn_power_well_ops = {
6977 .sync_hw = vlv_power_well_sync_hw,
6978 .enable = vlv_dpio_cmn_power_well_enable,
6979 .disable = vlv_dpio_cmn_power_well_disable,
6980 .is_enabled = vlv_power_well_enabled,
6981};
6982
Imre Deak77961eb2014-03-05 16:20:56 +02006983static const struct i915_power_well_ops vlv_dpio_power_well_ops = {
6984 .sync_hw = vlv_power_well_sync_hw,
6985 .enable = vlv_power_well_enable,
6986 .disable = vlv_power_well_disable,
6987 .is_enabled = vlv_power_well_enabled,
6988};
6989
6990static struct i915_power_well vlv_power_wells[] = {
6991 {
6992 .name = "always-on",
6993 .always_on = 1,
6994 .domains = VLV_ALWAYS_ON_POWER_DOMAINS,
6995 .ops = &i9xx_always_on_power_well_ops,
6996 },
6997 {
6998 .name = "display",
6999 .domains = VLV_DISPLAY_POWER_DOMAINS,
7000 .data = PUNIT_POWER_WELL_DISP2D,
7001 .ops = &vlv_display_power_well_ops,
7002 },
7003 {
Imre Deak77961eb2014-03-05 16:20:56 +02007004 .name = "dpio-tx-b-01",
7005 .domains = VLV_DPIO_TX_B_LANES_01_POWER_DOMAINS |
7006 VLV_DPIO_TX_B_LANES_23_POWER_DOMAINS |
7007 VLV_DPIO_TX_C_LANES_01_POWER_DOMAINS |
7008 VLV_DPIO_TX_C_LANES_23_POWER_DOMAINS,
7009 .ops = &vlv_dpio_power_well_ops,
7010 .data = PUNIT_POWER_WELL_DPIO_TX_B_LANES_01,
7011 },
7012 {
7013 .name = "dpio-tx-b-23",
7014 .domains = VLV_DPIO_TX_B_LANES_01_POWER_DOMAINS |
7015 VLV_DPIO_TX_B_LANES_23_POWER_DOMAINS |
7016 VLV_DPIO_TX_C_LANES_01_POWER_DOMAINS |
7017 VLV_DPIO_TX_C_LANES_23_POWER_DOMAINS,
7018 .ops = &vlv_dpio_power_well_ops,
7019 .data = PUNIT_POWER_WELL_DPIO_TX_B_LANES_23,
7020 },
7021 {
7022 .name = "dpio-tx-c-01",
7023 .domains = VLV_DPIO_TX_B_LANES_01_POWER_DOMAINS |
7024 VLV_DPIO_TX_B_LANES_23_POWER_DOMAINS |
7025 VLV_DPIO_TX_C_LANES_01_POWER_DOMAINS |
7026 VLV_DPIO_TX_C_LANES_23_POWER_DOMAINS,
7027 .ops = &vlv_dpio_power_well_ops,
7028 .data = PUNIT_POWER_WELL_DPIO_TX_C_LANES_01,
7029 },
7030 {
7031 .name = "dpio-tx-c-23",
7032 .domains = VLV_DPIO_TX_B_LANES_01_POWER_DOMAINS |
7033 VLV_DPIO_TX_B_LANES_23_POWER_DOMAINS |
7034 VLV_DPIO_TX_C_LANES_01_POWER_DOMAINS |
7035 VLV_DPIO_TX_C_LANES_23_POWER_DOMAINS,
7036 .ops = &vlv_dpio_power_well_ops,
7037 .data = PUNIT_POWER_WELL_DPIO_TX_C_LANES_23,
7038 },
Jesse Barnesf099a3c2014-05-23 13:16:43 -07007039 {
7040 .name = "dpio-common",
7041 .domains = VLV_DPIO_CMN_BC_POWER_DOMAINS,
7042 .data = PUNIT_POWER_WELL_DPIO_CMN_BC,
Ville Syrjäläaa519f22014-06-13 13:37:55 +03007043 .ops = &vlv_dpio_cmn_power_well_ops,
Jesse Barnesf099a3c2014-05-23 13:16:43 -07007044 },
Imre Deak77961eb2014-03-05 16:20:56 +02007045};
7046
Ville Syrjälä4811ff42014-06-28 02:04:07 +03007047static struct i915_power_well chv_power_wells[] = {
7048 {
7049 .name = "always-on",
7050 .always_on = 1,
7051 .domains = VLV_ALWAYS_ON_POWER_DOMAINS,
7052 .ops = &i9xx_always_on_power_well_ops,
7053 },
Ville Syrjäläf07057d2014-06-28 02:04:10 +03007054#if 0
7055 {
7056 .name = "display",
7057 .domains = VLV_DISPLAY_POWER_DOMAINS,
7058 .data = PUNIT_POWER_WELL_DISP2D,
7059 .ops = &vlv_display_power_well_ops,
7060 },
Ville Syrjälä26972b02014-06-28 02:04:11 +03007061 {
7062 .name = "pipe-a",
7063 .domains = CHV_PIPE_A_POWER_DOMAINS,
7064 .data = PIPE_A,
7065 .ops = &chv_pipe_power_well_ops,
7066 },
7067 {
7068 .name = "pipe-b",
7069 .domains = CHV_PIPE_B_POWER_DOMAINS,
7070 .data = PIPE_B,
7071 .ops = &chv_pipe_power_well_ops,
7072 },
7073 {
7074 .name = "pipe-c",
7075 .domains = CHV_PIPE_C_POWER_DOMAINS,
7076 .data = PIPE_C,
7077 .ops = &chv_pipe_power_well_ops,
7078 },
Ville Syrjäläf07057d2014-06-28 02:04:10 +03007079#endif
Ville Syrjälä5d6f7ea2014-06-28 02:04:08 +03007080 {
7081 .name = "dpio-common-bc",
Ville Syrjälä3dd7b9742014-06-27 19:49:57 +03007082 /*
7083 * XXX: cmnreset for one PHY seems to disturb the other.
7084 * As a workaround keep both powered on at the same
7085 * time for now.
7086 */
7087 .domains = CHV_DPIO_CMN_BC_POWER_DOMAINS | CHV_DPIO_CMN_D_POWER_DOMAINS,
Ville Syrjälä5d6f7ea2014-06-28 02:04:08 +03007088 .data = PUNIT_POWER_WELL_DPIO_CMN_BC,
7089 .ops = &chv_dpio_cmn_power_well_ops,
7090 },
7091 {
7092 .name = "dpio-common-d",
Ville Syrjälä3dd7b9742014-06-27 19:49:57 +03007093 /*
7094 * XXX: cmnreset for one PHY seems to disturb the other.
7095 * As a workaround keep both powered on at the same
7096 * time for now.
7097 */
7098 .domains = CHV_DPIO_CMN_BC_POWER_DOMAINS | CHV_DPIO_CMN_D_POWER_DOMAINS,
Ville Syrjälä5d6f7ea2014-06-28 02:04:08 +03007099 .data = PUNIT_POWER_WELL_DPIO_CMN_D,
7100 .ops = &chv_dpio_cmn_power_well_ops,
7101 },
Ville Syrjälä82583562014-06-28 02:04:12 +03007102#if 0
7103 {
7104 .name = "dpio-tx-b-01",
7105 .domains = VLV_DPIO_TX_B_LANES_01_POWER_DOMAINS |
7106 VLV_DPIO_TX_B_LANES_23_POWER_DOMAINS,
7107 .ops = &vlv_dpio_power_well_ops,
7108 .data = PUNIT_POWER_WELL_DPIO_TX_B_LANES_01,
7109 },
7110 {
7111 .name = "dpio-tx-b-23",
7112 .domains = VLV_DPIO_TX_B_LANES_01_POWER_DOMAINS |
7113 VLV_DPIO_TX_B_LANES_23_POWER_DOMAINS,
7114 .ops = &vlv_dpio_power_well_ops,
7115 .data = PUNIT_POWER_WELL_DPIO_TX_B_LANES_23,
7116 },
7117 {
7118 .name = "dpio-tx-c-01",
7119 .domains = VLV_DPIO_TX_C_LANES_01_POWER_DOMAINS |
7120 VLV_DPIO_TX_C_LANES_23_POWER_DOMAINS,
7121 .ops = &vlv_dpio_power_well_ops,
7122 .data = PUNIT_POWER_WELL_DPIO_TX_C_LANES_01,
7123 },
7124 {
7125 .name = "dpio-tx-c-23",
7126 .domains = VLV_DPIO_TX_C_LANES_01_POWER_DOMAINS |
7127 VLV_DPIO_TX_C_LANES_23_POWER_DOMAINS,
7128 .ops = &vlv_dpio_power_well_ops,
7129 .data = PUNIT_POWER_WELL_DPIO_TX_C_LANES_23,
7130 },
Ville Syrjälä2ce147f2014-06-28 02:04:13 +03007131 {
7132 .name = "dpio-tx-d-01",
7133 .domains = CHV_DPIO_TX_D_LANES_01_POWER_DOMAINS |
7134 CHV_DPIO_TX_D_LANES_23_POWER_DOMAINS,
7135 .ops = &vlv_dpio_power_well_ops,
7136 .data = PUNIT_POWER_WELL_DPIO_TX_D_LANES_01,
7137 },
7138 {
7139 .name = "dpio-tx-d-23",
7140 .domains = CHV_DPIO_TX_D_LANES_01_POWER_DOMAINS |
7141 CHV_DPIO_TX_D_LANES_23_POWER_DOMAINS,
7142 .ops = &vlv_dpio_power_well_ops,
7143 .data = PUNIT_POWER_WELL_DPIO_TX_D_LANES_23,
7144 },
Ville Syrjälä82583562014-06-28 02:04:12 +03007145#endif
Ville Syrjälä4811ff42014-06-28 02:04:07 +03007146};
7147
Ville Syrjäläd2011dc2014-06-13 13:37:56 +03007148static struct i915_power_well *lookup_power_well(struct drm_i915_private *dev_priv,
7149 enum punit_power_well power_well_id)
7150{
7151 struct i915_power_domains *power_domains = &dev_priv->power_domains;
7152 struct i915_power_well *power_well;
7153 int i;
7154
7155 for_each_power_well(i, power_well, POWER_DOMAIN_MASK, power_domains) {
7156 if (power_well->data == power_well_id)
7157 return power_well;
7158 }
7159
7160 return NULL;
7161}
7162
Imre Deakc1ca7272013-11-25 17:15:29 +02007163#define set_power_wells(power_domains, __power_wells) ({ \
7164 (power_domains)->power_wells = (__power_wells); \
7165 (power_domains)->power_well_count = ARRAY_SIZE(__power_wells); \
7166})
7167
Imre Deakda7e29b2014-02-18 00:02:02 +02007168int intel_power_domains_init(struct drm_i915_private *dev_priv)
Wang Xingchaoa38911a2013-05-30 22:07:11 +08007169{
Imre Deak83c00f52013-10-25 17:36:47 +03007170 struct i915_power_domains *power_domains = &dev_priv->power_domains;
Imre Deakc1ca7272013-11-25 17:15:29 +02007171
Imre Deak83c00f52013-10-25 17:36:47 +03007172 mutex_init(&power_domains->lock);
Wang Xingchaoa38911a2013-05-30 22:07:11 +08007173
Imre Deakc1ca7272013-11-25 17:15:29 +02007174 /*
7175 * The enabling order will be from lower to higher indexed wells,
7176 * the disabling order is reversed.
7177 */
Imre Deakda7e29b2014-02-18 00:02:02 +02007178 if (IS_HASWELL(dev_priv->dev)) {
Imre Deakc1ca7272013-11-25 17:15:29 +02007179 set_power_wells(power_domains, hsw_power_wells);
7180 hsw_pwr = power_domains;
Imre Deakda7e29b2014-02-18 00:02:02 +02007181 } else if (IS_BROADWELL(dev_priv->dev)) {
Imre Deakc1ca7272013-11-25 17:15:29 +02007182 set_power_wells(power_domains, bdw_power_wells);
7183 hsw_pwr = power_domains;
Ville Syrjälä4811ff42014-06-28 02:04:07 +03007184 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
7185 set_power_wells(power_domains, chv_power_wells);
Imre Deak77961eb2014-03-05 16:20:56 +02007186 } else if (IS_VALLEYVIEW(dev_priv->dev)) {
7187 set_power_wells(power_domains, vlv_power_wells);
Imre Deakc1ca7272013-11-25 17:15:29 +02007188 } else {
Imre Deak1c2256d2013-11-25 17:15:34 +02007189 set_power_wells(power_domains, i9xx_always_on_power_well);
Imre Deakc1ca7272013-11-25 17:15:29 +02007190 }
Wang Xingchaoa38911a2013-05-30 22:07:11 +08007191
7192 return 0;
7193}
7194
Imre Deakda7e29b2014-02-18 00:02:02 +02007195void intel_power_domains_remove(struct drm_i915_private *dev_priv)
Wang Xingchaoa38911a2013-05-30 22:07:11 +08007196{
7197 hsw_pwr = NULL;
7198}
7199
Imre Deakda7e29b2014-02-18 00:02:02 +02007200static void intel_power_domains_resume(struct drm_i915_private *dev_priv)
Ville Syrjälä9cdb8262013-09-16 17:38:27 +03007201{
Imre Deak83c00f52013-10-25 17:36:47 +03007202 struct i915_power_domains *power_domains = &dev_priv->power_domains;
7203 struct i915_power_well *power_well;
Imre Deakc1ca7272013-11-25 17:15:29 +02007204 int i;
Ville Syrjälä9cdb8262013-09-16 17:38:27 +03007205
Imre Deak83c00f52013-10-25 17:36:47 +03007206 mutex_lock(&power_domains->lock);
Imre Deakbfafe932014-06-05 20:31:47 +03007207 for_each_power_well(i, power_well, POWER_DOMAIN_MASK, power_domains) {
Imre Deaka45f44662014-03-04 19:22:56 +02007208 power_well->ops->sync_hw(dev_priv, power_well);
Imre Deakbfafe932014-06-05 20:31:47 +03007209 power_well->hw_enabled = power_well->ops->is_enabled(dev_priv,
7210 power_well);
7211 }
Imre Deak83c00f52013-10-25 17:36:47 +03007212 mutex_unlock(&power_domains->lock);
Wang Xingchaoa38911a2013-05-30 22:07:11 +08007213}
7214
Ville Syrjäläd2011dc2014-06-13 13:37:56 +03007215static void vlv_cmnlane_wa(struct drm_i915_private *dev_priv)
7216{
7217 struct i915_power_well *cmn =
7218 lookup_power_well(dev_priv, PUNIT_POWER_WELL_DPIO_CMN_BC);
7219 struct i915_power_well *disp2d =
7220 lookup_power_well(dev_priv, PUNIT_POWER_WELL_DISP2D);
7221
7222 /* nothing to do if common lane is already off */
7223 if (!cmn->ops->is_enabled(dev_priv, cmn))
7224 return;
7225
7226 /* If the display might be already active skip this */
7227 if (disp2d->ops->is_enabled(dev_priv, disp2d) &&
7228 I915_READ(DPIO_CTL) & DPIO_CMNRST)
7229 return;
7230
7231 DRM_DEBUG_KMS("toggling display PHY side reset\n");
7232
7233 /* cmnlane needs DPLL registers */
7234 disp2d->ops->enable(dev_priv, disp2d);
7235
7236 /*
7237 * From VLV2A0_DP_eDP_HDMI_DPIO_driver_vbios_notes_11.docx:
7238 * Need to assert and de-assert PHY SB reset by gating the
7239 * common lane power, then un-gating it.
7240 * Simply ungating isn't enough to reset the PHY enough to get
7241 * ports and lanes running.
7242 */
7243 cmn->ops->disable(dev_priv, cmn);
7244}
7245
Imre Deakda7e29b2014-02-18 00:02:02 +02007246void intel_power_domains_init_hw(struct drm_i915_private *dev_priv)
Paulo Zanonifa42e232013-01-25 16:59:11 -02007247{
Ville Syrjäläd2011dc2014-06-13 13:37:56 +03007248 struct drm_device *dev = dev_priv->dev;
Imre Deak0d116a22014-04-25 13:19:05 +03007249 struct i915_power_domains *power_domains = &dev_priv->power_domains;
7250
7251 power_domains->initializing = true;
Ville Syrjäläd2011dc2014-06-13 13:37:56 +03007252
7253 if (IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
7254 mutex_lock(&power_domains->lock);
7255 vlv_cmnlane_wa(dev_priv);
7256 mutex_unlock(&power_domains->lock);
7257 }
7258
Paulo Zanonifa42e232013-01-25 16:59:11 -02007259 /* For now, we need the power well to be always enabled. */
Imre Deakda7e29b2014-02-18 00:02:02 +02007260 intel_display_set_init_power(dev_priv, true);
7261 intel_power_domains_resume(dev_priv);
Imre Deak0d116a22014-04-25 13:19:05 +03007262 power_domains->initializing = false;
Eugeni Dodonovd0d3e512012-05-09 15:37:16 -03007263}
7264
Paulo Zanonic67a4702013-08-19 13:18:09 -03007265void intel_aux_display_runtime_get(struct drm_i915_private *dev_priv)
7266{
Paulo Zanonid361ae22014-03-07 20:08:12 -03007267 intel_runtime_pm_get(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -03007268}
7269
7270void intel_aux_display_runtime_put(struct drm_i915_private *dev_priv)
7271{
Paulo Zanonid361ae22014-03-07 20:08:12 -03007272 intel_runtime_pm_put(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -03007273}
7274
Paulo Zanoni8a187452013-12-06 20:32:13 -02007275void intel_runtime_pm_get(struct drm_i915_private *dev_priv)
7276{
7277 struct drm_device *dev = dev_priv->dev;
7278 struct device *device = &dev->pdev->dev;
7279
7280 if (!HAS_RUNTIME_PM(dev))
7281 return;
7282
7283 pm_runtime_get_sync(device);
7284 WARN(dev_priv->pm.suspended, "Device still suspended.\n");
7285}
7286
Imre Deakc6df39b2014-04-14 20:24:29 +03007287void intel_runtime_pm_get_noresume(struct drm_i915_private *dev_priv)
7288{
7289 struct drm_device *dev = dev_priv->dev;
7290 struct device *device = &dev->pdev->dev;
7291
7292 if (!HAS_RUNTIME_PM(dev))
7293 return;
7294
7295 WARN(dev_priv->pm.suspended, "Getting nosync-ref while suspended.\n");
7296 pm_runtime_get_noresume(device);
7297}
7298
Paulo Zanoni8a187452013-12-06 20:32:13 -02007299void intel_runtime_pm_put(struct drm_i915_private *dev_priv)
7300{
7301 struct drm_device *dev = dev_priv->dev;
7302 struct device *device = &dev->pdev->dev;
7303
7304 if (!HAS_RUNTIME_PM(dev))
7305 return;
7306
7307 pm_runtime_mark_last_busy(device);
7308 pm_runtime_put_autosuspend(device);
7309}
7310
7311void intel_init_runtime_pm(struct drm_i915_private *dev_priv)
7312{
7313 struct drm_device *dev = dev_priv->dev;
7314 struct device *device = &dev->pdev->dev;
7315
Paulo Zanoni8a187452013-12-06 20:32:13 -02007316 if (!HAS_RUNTIME_PM(dev))
7317 return;
7318
7319 pm_runtime_set_active(device);
7320
Imre Deakaeab0b52014-04-14 20:24:36 +03007321 /*
7322 * RPM depends on RC6 to save restore the GT HW context, so make RC6 a
7323 * requirement.
7324 */
7325 if (!intel_enable_rc6(dev)) {
7326 DRM_INFO("RC6 disabled, disabling runtime PM support\n");
7327 return;
7328 }
7329
Paulo Zanoni8a187452013-12-06 20:32:13 -02007330 pm_runtime_set_autosuspend_delay(device, 10000); /* 10s */
7331 pm_runtime_mark_last_busy(device);
7332 pm_runtime_use_autosuspend(device);
Paulo Zanoniba0239e2014-03-07 20:08:07 -03007333
7334 pm_runtime_put_autosuspend(device);
Paulo Zanoni8a187452013-12-06 20:32:13 -02007335}
7336
7337void intel_fini_runtime_pm(struct drm_i915_private *dev_priv)
7338{
7339 struct drm_device *dev = dev_priv->dev;
7340 struct device *device = &dev->pdev->dev;
7341
7342 if (!HAS_RUNTIME_PM(dev))
7343 return;
7344
Imre Deakaeab0b52014-04-14 20:24:36 +03007345 if (!intel_enable_rc6(dev))
7346 return;
7347
Paulo Zanoni8a187452013-12-06 20:32:13 -02007348 /* Make sure we're not suspended first. */
7349 pm_runtime_get_sync(device);
7350 pm_runtime_disable(device);
7351}
7352
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007353/* Set up chip specific power management-related functions */
7354void intel_init_pm(struct drm_device *dev)
7355{
7356 struct drm_i915_private *dev_priv = dev->dev_private;
7357
Daniel Vetter3a77c4c2014-01-10 08:50:12 +01007358 if (HAS_FBC(dev)) {
Ville Syrjälä40045462013-11-28 17:29:59 +02007359 if (INTEL_INFO(dev)->gen >= 7) {
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007360 dev_priv->display.fbc_enabled = ironlake_fbc_enabled;
Ville Syrjälä40045462013-11-28 17:29:59 +02007361 dev_priv->display.enable_fbc = gen7_enable_fbc;
7362 dev_priv->display.disable_fbc = ironlake_disable_fbc;
7363 } else if (INTEL_INFO(dev)->gen >= 5) {
7364 dev_priv->display.fbc_enabled = ironlake_fbc_enabled;
7365 dev_priv->display.enable_fbc = ironlake_enable_fbc;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007366 dev_priv->display.disable_fbc = ironlake_disable_fbc;
7367 } else if (IS_GM45(dev)) {
7368 dev_priv->display.fbc_enabled = g4x_fbc_enabled;
7369 dev_priv->display.enable_fbc = g4x_enable_fbc;
7370 dev_priv->display.disable_fbc = g4x_disable_fbc;
Ville Syrjälä40045462013-11-28 17:29:59 +02007371 } else {
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007372 dev_priv->display.fbc_enabled = i8xx_fbc_enabled;
7373 dev_priv->display.enable_fbc = i8xx_enable_fbc;
7374 dev_priv->display.disable_fbc = i8xx_disable_fbc;
Ville Syrjälä993495a2013-12-12 17:27:40 +02007375
7376 /* This value was pulled out of someone's hat */
7377 I915_WRITE(FBC_CONTROL, 500 << FBC_CTL_INTERVAL_SHIFT);
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007378 }
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007379 }
7380
Daniel Vetterc921aba2012-04-26 23:28:17 +02007381 /* For cxsr */
7382 if (IS_PINEVIEW(dev))
7383 i915_pineview_get_mem_freq(dev);
7384 else if (IS_GEN5(dev))
7385 i915_ironlake_get_mem_freq(dev);
7386
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007387 /* For FIFO watermark updates */
7388 if (HAS_PCH_SPLIT(dev)) {
Damien Lespiaufa50ad62014-03-17 18:01:16 +00007389 ilk_setup_wm_latency(dev);
Ville Syrjälä53615a52013-08-01 16:18:50 +03007390
Ville Syrjäläbd602542014-01-07 16:14:10 +02007391 if ((IS_GEN5(dev) && dev_priv->wm.pri_latency[1] &&
7392 dev_priv->wm.spr_latency[1] && dev_priv->wm.cur_latency[1]) ||
7393 (!IS_GEN5(dev) && dev_priv->wm.pri_latency[0] &&
7394 dev_priv->wm.spr_latency[0] && dev_priv->wm.cur_latency[0])) {
7395 dev_priv->display.update_wm = ilk_update_wm;
7396 dev_priv->display.update_sprite_wm = ilk_update_sprite_wm;
7397 } else {
7398 DRM_DEBUG_KMS("Failed to read display plane latency. "
7399 "Disable CxSR\n");
7400 }
7401
7402 if (IS_GEN5(dev))
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007403 dev_priv->display.init_clock_gating = ironlake_init_clock_gating;
Ville Syrjäläbd602542014-01-07 16:14:10 +02007404 else if (IS_GEN6(dev))
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007405 dev_priv->display.init_clock_gating = gen6_init_clock_gating;
Ville Syrjäläbd602542014-01-07 16:14:10 +02007406 else if (IS_IVYBRIDGE(dev))
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007407 dev_priv->display.init_clock_gating = ivybridge_init_clock_gating;
Ville Syrjäläbd602542014-01-07 16:14:10 +02007408 else if (IS_HASWELL(dev))
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03007409 dev_priv->display.init_clock_gating = haswell_init_clock_gating;
Ville Syrjäläbd602542014-01-07 16:14:10 +02007410 else if (INTEL_INFO(dev)->gen == 8)
Paulo Zanoni47c2bd92014-08-21 17:09:37 -03007411 dev_priv->display.init_clock_gating = broadwell_init_clock_gating;
Damien Lespiauda2078c2013-02-13 15:27:27 +00007412 else if (INTEL_INFO(dev)->gen == 9)
7413 dev_priv->display.init_clock_gating = gen9_init_clock_gating;
Ville Syrjäläa4565da2014-04-09 13:28:10 +03007414 } else if (IS_CHERRYVIEW(dev)) {
Ville Syrjälä3c2777f2014-06-26 17:03:06 +03007415 dev_priv->display.update_wm = cherryview_update_wm;
Gajanan Bhat01e184c2014-08-07 17:03:30 +05307416 dev_priv->display.update_sprite_wm = valleyview_update_sprite_wm;
Ville Syrjäläa4565da2014-04-09 13:28:10 +03007417 dev_priv->display.init_clock_gating =
7418 cherryview_init_clock_gating;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007419 } else if (IS_VALLEYVIEW(dev)) {
7420 dev_priv->display.update_wm = valleyview_update_wm;
Gajanan Bhat01e184c2014-08-07 17:03:30 +05307421 dev_priv->display.update_sprite_wm = valleyview_update_sprite_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007422 dev_priv->display.init_clock_gating =
7423 valleyview_init_clock_gating;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007424 } else if (IS_PINEVIEW(dev)) {
7425 if (!intel_get_cxsr_latency(IS_PINEVIEW_G(dev),
7426 dev_priv->is_ddr3,
7427 dev_priv->fsb_freq,
7428 dev_priv->mem_freq)) {
7429 DRM_INFO("failed to find known CxSR latency "
7430 "(found ddr%s fsb freq %d, mem freq %d), "
7431 "disabling CxSR\n",
7432 (dev_priv->is_ddr3 == 1) ? "3" : "2",
7433 dev_priv->fsb_freq, dev_priv->mem_freq);
7434 /* Disable CxSR and never update its watermark again */
Imre Deak5209b1f2014-07-01 12:36:17 +03007435 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007436 dev_priv->display.update_wm = NULL;
7437 } else
7438 dev_priv->display.update_wm = pineview_update_wm;
7439 dev_priv->display.init_clock_gating = gen3_init_clock_gating;
7440 } else if (IS_G4X(dev)) {
7441 dev_priv->display.update_wm = g4x_update_wm;
7442 dev_priv->display.init_clock_gating = g4x_init_clock_gating;
7443 } else if (IS_GEN4(dev)) {
7444 dev_priv->display.update_wm = i965_update_wm;
7445 if (IS_CRESTLINE(dev))
7446 dev_priv->display.init_clock_gating = crestline_init_clock_gating;
7447 else if (IS_BROADWATER(dev))
7448 dev_priv->display.init_clock_gating = broadwater_init_clock_gating;
7449 } else if (IS_GEN3(dev)) {
7450 dev_priv->display.update_wm = i9xx_update_wm;
7451 dev_priv->display.get_fifo_size = i9xx_get_fifo_size;
7452 dev_priv->display.init_clock_gating = gen3_init_clock_gating;
Daniel Vetterfeb56b92013-12-14 20:38:30 -02007453 } else if (IS_GEN2(dev)) {
7454 if (INTEL_INFO(dev)->num_pipes == 1) {
7455 dev_priv->display.update_wm = i845_update_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007456 dev_priv->display.get_fifo_size = i845_get_fifo_size;
Daniel Vetterfeb56b92013-12-14 20:38:30 -02007457 } else {
7458 dev_priv->display.update_wm = i9xx_update_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007459 dev_priv->display.get_fifo_size = i830_get_fifo_size;
Daniel Vetterfeb56b92013-12-14 20:38:30 -02007460 }
7461
7462 if (IS_I85X(dev) || IS_I865G(dev))
7463 dev_priv->display.init_clock_gating = i85x_init_clock_gating;
7464 else
7465 dev_priv->display.init_clock_gating = i830_init_clock_gating;
7466 } else {
7467 DRM_ERROR("unexpected fall-through in intel_init_pm\n");
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007468 }
7469}
7470
Ben Widawsky42c05262012-09-26 10:34:00 -07007471int sandybridge_pcode_read(struct drm_i915_private *dev_priv, u8 mbox, u32 *val)
7472{
Jesse Barnes4fc688c2012-11-02 11:14:01 -07007473 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Ben Widawsky42c05262012-09-26 10:34:00 -07007474
7475 if (I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) {
7476 DRM_DEBUG_DRIVER("warning: pcode (read) mailbox access failed\n");
7477 return -EAGAIN;
7478 }
7479
7480 I915_WRITE(GEN6_PCODE_DATA, *val);
7481 I915_WRITE(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY | mbox);
7482
7483 if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
7484 500)) {
7485 DRM_ERROR("timeout waiting for pcode read (%d) to finish\n", mbox);
7486 return -ETIMEDOUT;
7487 }
7488
7489 *val = I915_READ(GEN6_PCODE_DATA);
7490 I915_WRITE(GEN6_PCODE_DATA, 0);
7491
7492 return 0;
7493}
7494
7495int sandybridge_pcode_write(struct drm_i915_private *dev_priv, u8 mbox, u32 val)
7496{
Jesse Barnes4fc688c2012-11-02 11:14:01 -07007497 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Ben Widawsky42c05262012-09-26 10:34:00 -07007498
7499 if (I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) {
7500 DRM_DEBUG_DRIVER("warning: pcode (write) mailbox access failed\n");
7501 return -EAGAIN;
7502 }
7503
7504 I915_WRITE(GEN6_PCODE_DATA, val);
7505 I915_WRITE(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY | mbox);
7506
7507 if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
7508 500)) {
7509 DRM_ERROR("timeout waiting for pcode write (%d) to finish\n", mbox);
7510 return -ETIMEDOUT;
7511 }
7512
7513 I915_WRITE(GEN6_PCODE_DATA, 0);
7514
7515 return 0;
7516}
Jesse Barnesa0e4e192013-04-02 11:23:05 -07007517
Fengguang Wub55dd642014-07-12 11:21:39 +02007518static int byt_gpu_freq(struct drm_i915_private *dev_priv, int val)
Jesse Barnes855ba3b2013-04-17 15:54:57 -07007519{
Ville Syrjälä07ab1182013-11-05 22:42:28 +02007520 int div;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07007521
Ville Syrjälä07ab1182013-11-05 22:42:28 +02007522 /* 4 x czclk */
Ville Syrjälä2ec38152013-11-05 22:42:29 +02007523 switch (dev_priv->mem_freq) {
Jesse Barnes855ba3b2013-04-17 15:54:57 -07007524 case 800:
Ville Syrjälä07ab1182013-11-05 22:42:28 +02007525 div = 10;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07007526 break;
7527 case 1066:
Ville Syrjälä07ab1182013-11-05 22:42:28 +02007528 div = 12;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07007529 break;
7530 case 1333:
Ville Syrjälä07ab1182013-11-05 22:42:28 +02007531 div = 16;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07007532 break;
7533 default:
7534 return -1;
7535 }
7536
Ville Syrjälä2ec38152013-11-05 22:42:29 +02007537 return DIV_ROUND_CLOSEST(dev_priv->mem_freq * (val + 6 - 0xbd), 4 * div);
Jesse Barnes855ba3b2013-04-17 15:54:57 -07007538}
7539
Fengguang Wub55dd642014-07-12 11:21:39 +02007540static int byt_freq_opcode(struct drm_i915_private *dev_priv, int val)
Jesse Barnes855ba3b2013-04-17 15:54:57 -07007541{
Ville Syrjälä07ab1182013-11-05 22:42:28 +02007542 int mul;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07007543
Ville Syrjälä07ab1182013-11-05 22:42:28 +02007544 /* 4 x czclk */
Ville Syrjälä2ec38152013-11-05 22:42:29 +02007545 switch (dev_priv->mem_freq) {
Jesse Barnes855ba3b2013-04-17 15:54:57 -07007546 case 800:
Ville Syrjälä07ab1182013-11-05 22:42:28 +02007547 mul = 10;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07007548 break;
7549 case 1066:
Ville Syrjälä07ab1182013-11-05 22:42:28 +02007550 mul = 12;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07007551 break;
7552 case 1333:
Ville Syrjälä07ab1182013-11-05 22:42:28 +02007553 mul = 16;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07007554 break;
7555 default:
7556 return -1;
7557 }
7558
Ville Syrjälä2ec38152013-11-05 22:42:29 +02007559 return DIV_ROUND_CLOSEST(4 * mul * val, dev_priv->mem_freq) + 0xbd - 6;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07007560}
7561
Fengguang Wub55dd642014-07-12 11:21:39 +02007562static int chv_gpu_freq(struct drm_i915_private *dev_priv, int val)
Deepak S22b1b2f2014-07-12 14:54:33 +05307563{
7564 int div, freq;
7565
7566 switch (dev_priv->rps.cz_freq) {
7567 case 200:
7568 div = 5;
7569 break;
7570 case 267:
7571 div = 6;
7572 break;
7573 case 320:
7574 case 333:
7575 case 400:
7576 div = 8;
7577 break;
7578 default:
7579 return -1;
7580 }
7581
7582 freq = (DIV_ROUND_CLOSEST((dev_priv->rps.cz_freq * val), 2 * div) / 2);
7583
7584 return freq;
7585}
7586
Fengguang Wub55dd642014-07-12 11:21:39 +02007587static int chv_freq_opcode(struct drm_i915_private *dev_priv, int val)
Deepak S22b1b2f2014-07-12 14:54:33 +05307588{
7589 int mul, opcode;
7590
7591 switch (dev_priv->rps.cz_freq) {
7592 case 200:
7593 mul = 5;
7594 break;
7595 case 267:
7596 mul = 6;
7597 break;
7598 case 320:
7599 case 333:
7600 case 400:
7601 mul = 8;
7602 break;
7603 default:
7604 return -1;
7605 }
7606
Ville Syrjälä1c147622014-08-18 14:42:43 +03007607 /* CHV needs even values */
Deepak S22b1b2f2014-07-12 14:54:33 +05307608 opcode = (DIV_ROUND_CLOSEST((val * 2 * mul), dev_priv->rps.cz_freq) * 2);
7609
7610 return opcode;
7611}
7612
7613int vlv_gpu_freq(struct drm_i915_private *dev_priv, int val)
7614{
7615 int ret = -1;
7616
7617 if (IS_CHERRYVIEW(dev_priv->dev))
7618 ret = chv_gpu_freq(dev_priv, val);
7619 else if (IS_VALLEYVIEW(dev_priv->dev))
7620 ret = byt_gpu_freq(dev_priv, val);
7621
7622 return ret;
7623}
7624
7625int vlv_freq_opcode(struct drm_i915_private *dev_priv, int val)
7626{
7627 int ret = -1;
7628
7629 if (IS_CHERRYVIEW(dev_priv->dev))
7630 ret = chv_freq_opcode(dev_priv, val);
7631 else if (IS_VALLEYVIEW(dev_priv->dev))
7632 ret = byt_freq_opcode(dev_priv, val);
7633
7634 return ret;
7635}
7636
Daniel Vetterf742a552013-12-06 10:17:53 +01007637void intel_pm_setup(struct drm_device *dev)
Chris Wilson907b28c2013-07-19 20:36:52 +01007638{
7639 struct drm_i915_private *dev_priv = dev->dev_private;
7640
Daniel Vetterf742a552013-12-06 10:17:53 +01007641 mutex_init(&dev_priv->rps.hw_lock);
7642
Chris Wilson907b28c2013-07-19 20:36:52 +01007643 INIT_DELAYED_WORK(&dev_priv->rps.delayed_resume_work,
7644 intel_gen6_powersave_work);
Paulo Zanoni5d584b22014-03-07 20:08:15 -03007645
Paulo Zanoni33688d92014-03-07 20:08:19 -03007646 dev_priv->pm.suspended = false;
Jesse Barnes9df7575f2014-06-20 09:29:20 -07007647 dev_priv->pm._irqs_disabled = false;
Chris Wilson907b28c2013-07-19 20:36:52 +01007648}