blob: 2091a9ce1375d1bf73b1ceed6e28d9e4236a4dca [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
Eugeni Dodonov1fa61102012-04-18 15:29:26 -030069static void i8xx_disable_fbc(struct drm_device *dev)
Eugeni Dodonov85208be2012-04-16 22:20:34 -030070{
71 struct drm_i915_private *dev_priv = dev->dev_private;
72 u32 fbc_ctl;
73
74 /* Disable compression */
75 fbc_ctl = I915_READ(FBC_CONTROL);
76 if ((fbc_ctl & FBC_CTL_EN) == 0)
77 return;
78
79 fbc_ctl &= ~FBC_CTL_EN;
80 I915_WRITE(FBC_CONTROL, fbc_ctl);
81
82 /* Wait for compressing bit to clear */
83 if (wait_for((I915_READ(FBC_STATUS) & FBC_STAT_COMPRESSING) == 0, 10)) {
84 DRM_DEBUG_KMS("FBC idle timed out\n");
85 return;
86 }
87
88 DRM_DEBUG_KMS("disabled FBC\n");
89}
90
Ville Syrjälä993495a2013-12-12 17:27:40 +020091static void i8xx_enable_fbc(struct drm_crtc *crtc)
Eugeni Dodonov85208be2012-04-16 22:20:34 -030092{
93 struct drm_device *dev = crtc->dev;
94 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roperf4510a22014-04-01 15:22:40 -070095 struct drm_framebuffer *fb = crtc->primary->fb;
Eugeni Dodonov85208be2012-04-16 22:20:34 -030096 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
97 struct drm_i915_gem_object *obj = intel_fb->obj;
98 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
99 int cfb_pitch;
Ville Syrjälä7f2cf222014-01-23 16:49:11 +0200100 int i;
Ville Syrjälä159f9872013-11-28 17:29:57 +0200101 u32 fbc_ctl;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300102
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -0700103 cfb_pitch = dev_priv->fbc.size / FBC_LL_SIZE;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300104 if (fb->pitches[0] < cfb_pitch)
105 cfb_pitch = fb->pitches[0];
106
Ville Syrjälä42a430f2013-11-28 17:29:56 +0200107 /* FBC_CTL wants 32B or 64B units */
108 if (IS_GEN2(dev))
109 cfb_pitch = (cfb_pitch / 32) - 1;
110 else
111 cfb_pitch = (cfb_pitch / 64) - 1;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300112
113 /* Clear old tags */
114 for (i = 0; i < (FBC_LL_SIZE / 32) + 1; i++)
115 I915_WRITE(FBC_TAG + (i * 4), 0);
116
Ville Syrjälä159f9872013-11-28 17:29:57 +0200117 if (IS_GEN4(dev)) {
118 u32 fbc_ctl2;
119
120 /* Set it up... */
121 fbc_ctl2 = FBC_CTL_FENCE_DBL | FBC_CTL_IDLE_IMM | FBC_CTL_CPU_FENCE;
Ville Syrjälä7f2cf222014-01-23 16:49:11 +0200122 fbc_ctl2 |= FBC_CTL_PLANE(intel_crtc->plane);
Ville Syrjälä159f9872013-11-28 17:29:57 +0200123 I915_WRITE(FBC_CONTROL2, fbc_ctl2);
124 I915_WRITE(FBC_FENCE_OFF, crtc->y);
125 }
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300126
127 /* enable it... */
Ville Syrjälä993495a2013-12-12 17:27:40 +0200128 fbc_ctl = I915_READ(FBC_CONTROL);
129 fbc_ctl &= 0x3fff << FBC_CTL_INTERVAL_SHIFT;
130 fbc_ctl |= FBC_CTL_EN | FBC_CTL_PERIODIC;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300131 if (IS_I945GM(dev))
132 fbc_ctl |= FBC_CTL_C3_IDLE; /* 945 needs special SR handling */
133 fbc_ctl |= (cfb_pitch & 0xff) << FBC_CTL_STRIDE_SHIFT;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300134 fbc_ctl |= obj->fence_reg;
135 I915_WRITE(FBC_CONTROL, fbc_ctl);
136
Ville Syrjälä5cd54102014-01-23 16:49:16 +0200137 DRM_DEBUG_KMS("enabled FBC, pitch %d, yoff %d, plane %c\n",
Ville Syrjälä84f44ce2013-04-17 17:48:49 +0300138 cfb_pitch, crtc->y, plane_name(intel_crtc->plane));
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300139}
140
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300141static bool i8xx_fbc_enabled(struct drm_device *dev)
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300142{
143 struct drm_i915_private *dev_priv = dev->dev_private;
144
145 return I915_READ(FBC_CONTROL) & FBC_CTL_EN;
146}
147
Ville Syrjälä993495a2013-12-12 17:27:40 +0200148static void g4x_enable_fbc(struct drm_crtc *crtc)
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300149{
150 struct drm_device *dev = crtc->dev;
151 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roperf4510a22014-04-01 15:22:40 -0700152 struct drm_framebuffer *fb = crtc->primary->fb;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300153 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
154 struct drm_i915_gem_object *obj = intel_fb->obj;
155 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300156 u32 dpfc_ctl;
157
Ville Syrjälä3fa2e0e2014-01-23 16:49:12 +0200158 dpfc_ctl = DPFC_CTL_PLANE(intel_crtc->plane) | DPFC_SR_EN;
159 if (drm_format_plane_cpp(fb->pixel_format, 0) == 2)
160 dpfc_ctl |= DPFC_CTL_LIMIT_2X;
161 else
162 dpfc_ctl |= DPFC_CTL_LIMIT_1X;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300163 dpfc_ctl |= DPFC_CTL_FENCE_EN | obj->fence_reg;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300164
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300165 I915_WRITE(DPFC_FENCE_YOFF, crtc->y);
166
167 /* enable it... */
Ville Syrjäläfe74c1a2014-01-23 16:49:13 +0200168 I915_WRITE(DPFC_CONTROL, dpfc_ctl | DPFC_CTL_EN);
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300169
Ville Syrjälä84f44ce2013-04-17 17:48:49 +0300170 DRM_DEBUG_KMS("enabled fbc on plane %c\n", plane_name(intel_crtc->plane));
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300171}
172
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300173static void g4x_disable_fbc(struct drm_device *dev)
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300174{
175 struct drm_i915_private *dev_priv = dev->dev_private;
176 u32 dpfc_ctl;
177
178 /* Disable compression */
179 dpfc_ctl = I915_READ(DPFC_CONTROL);
180 if (dpfc_ctl & DPFC_CTL_EN) {
181 dpfc_ctl &= ~DPFC_CTL_EN;
182 I915_WRITE(DPFC_CONTROL, dpfc_ctl);
183
184 DRM_DEBUG_KMS("disabled FBC\n");
185 }
186}
187
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300188static bool g4x_fbc_enabled(struct drm_device *dev)
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300189{
190 struct drm_i915_private *dev_priv = dev->dev_private;
191
192 return I915_READ(DPFC_CONTROL) & DPFC_CTL_EN;
193}
194
195static void sandybridge_blit_fbc_update(struct drm_device *dev)
196{
197 struct drm_i915_private *dev_priv = dev->dev_private;
198 u32 blt_ecoskpd;
199
200 /* Make sure blitter notifies FBC of writes */
Deepak S940aece2013-11-23 14:55:43 +0530201
202 /* Blitter is part of Media powerwell on VLV. No impact of
203 * his param in other platforms for now */
204 gen6_gt_force_wake_get(dev_priv, FORCEWAKE_MEDIA);
Deepak Sc8d9a592013-11-23 14:55:42 +0530205
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300206 blt_ecoskpd = I915_READ(GEN6_BLITTER_ECOSKPD);
207 blt_ecoskpd |= GEN6_BLITTER_FBC_NOTIFY <<
208 GEN6_BLITTER_LOCK_SHIFT;
209 I915_WRITE(GEN6_BLITTER_ECOSKPD, blt_ecoskpd);
210 blt_ecoskpd |= GEN6_BLITTER_FBC_NOTIFY;
211 I915_WRITE(GEN6_BLITTER_ECOSKPD, blt_ecoskpd);
212 blt_ecoskpd &= ~(GEN6_BLITTER_FBC_NOTIFY <<
213 GEN6_BLITTER_LOCK_SHIFT);
214 I915_WRITE(GEN6_BLITTER_ECOSKPD, blt_ecoskpd);
215 POSTING_READ(GEN6_BLITTER_ECOSKPD);
Deepak Sc8d9a592013-11-23 14:55:42 +0530216
Deepak S940aece2013-11-23 14:55:43 +0530217 gen6_gt_force_wake_put(dev_priv, FORCEWAKE_MEDIA);
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300218}
219
Ville Syrjälä993495a2013-12-12 17:27:40 +0200220static void ironlake_enable_fbc(struct drm_crtc *crtc)
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300221{
222 struct drm_device *dev = crtc->dev;
223 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roperf4510a22014-04-01 15:22:40 -0700224 struct drm_framebuffer *fb = crtc->primary->fb;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300225 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
226 struct drm_i915_gem_object *obj = intel_fb->obj;
227 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300228 u32 dpfc_ctl;
229
Ville Syrjälä46f3dab2014-01-23 16:49:14 +0200230 dpfc_ctl = DPFC_CTL_PLANE(intel_crtc->plane);
Ville Syrjälä3fa2e0e2014-01-23 16:49:12 +0200231 if (drm_format_plane_cpp(fb->pixel_format, 0) == 2)
232 dpfc_ctl |= DPFC_CTL_LIMIT_2X;
233 else
234 dpfc_ctl |= DPFC_CTL_LIMIT_1X;
Ville Syrjäläd6293362013-11-21 21:29:45 +0200235 dpfc_ctl |= DPFC_CTL_FENCE_EN;
236 if (IS_GEN5(dev))
237 dpfc_ctl |= obj->fence_reg;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300238
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300239 I915_WRITE(ILK_DPFC_FENCE_YOFF, crtc->y);
Ben Widawskyf343c5f2013-07-05 14:41:04 -0700240 I915_WRITE(ILK_FBC_RT_BASE, i915_gem_obj_ggtt_offset(obj) | ILK_FBC_RT_VALID);
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300241 /* enable it... */
242 I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl | DPFC_CTL_EN);
243
244 if (IS_GEN6(dev)) {
245 I915_WRITE(SNB_DPFC_CTL_SA,
246 SNB_CPU_FENCE_ENABLE | obj->fence_reg);
247 I915_WRITE(DPFC_CPU_FENCE_OFFSET, crtc->y);
248 sandybridge_blit_fbc_update(dev);
249 }
250
Ville Syrjälä84f44ce2013-04-17 17:48:49 +0300251 DRM_DEBUG_KMS("enabled fbc on plane %c\n", plane_name(intel_crtc->plane));
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300252}
253
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300254static void ironlake_disable_fbc(struct drm_device *dev)
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300255{
256 struct drm_i915_private *dev_priv = dev->dev_private;
257 u32 dpfc_ctl;
258
259 /* Disable compression */
260 dpfc_ctl = I915_READ(ILK_DPFC_CONTROL);
261 if (dpfc_ctl & DPFC_CTL_EN) {
262 dpfc_ctl &= ~DPFC_CTL_EN;
263 I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl);
264
265 DRM_DEBUG_KMS("disabled FBC\n");
266 }
267}
268
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300269static bool ironlake_fbc_enabled(struct drm_device *dev)
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300270{
271 struct drm_i915_private *dev_priv = dev->dev_private;
272
273 return I915_READ(ILK_DPFC_CONTROL) & DPFC_CTL_EN;
274}
275
Ville Syrjälä993495a2013-12-12 17:27:40 +0200276static void gen7_enable_fbc(struct drm_crtc *crtc)
Rodrigo Viviabe959c2013-05-06 19:37:33 -0300277{
278 struct drm_device *dev = crtc->dev;
279 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roperf4510a22014-04-01 15:22:40 -0700280 struct drm_framebuffer *fb = crtc->primary->fb;
Rodrigo Viviabe959c2013-05-06 19:37:33 -0300281 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
282 struct drm_i915_gem_object *obj = intel_fb->obj;
283 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjälä3fa2e0e2014-01-23 16:49:12 +0200284 u32 dpfc_ctl;
Rodrigo Viviabe959c2013-05-06 19:37:33 -0300285
Ville Syrjälä3fa2e0e2014-01-23 16:49:12 +0200286 dpfc_ctl = IVB_DPFC_CTL_PLANE(intel_crtc->plane);
287 if (drm_format_plane_cpp(fb->pixel_format, 0) == 2)
288 dpfc_ctl |= DPFC_CTL_LIMIT_2X;
289 else
290 dpfc_ctl |= DPFC_CTL_LIMIT_1X;
291 dpfc_ctl |= IVB_DPFC_CTL_FENCE_EN;
292
293 I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl | DPFC_CTL_EN);
Rodrigo Viviabe959c2013-05-06 19:37:33 -0300294
Rodrigo Vivi891348b2013-05-06 19:37:36 -0300295 if (IS_IVYBRIDGE(dev)) {
Damien Lespiau7dd23ba2013-05-10 14:33:17 +0100296 /* WaFbcAsynchFlipDisableFbcQueue:ivb */
Ville Syrjälä2adb6db2014-03-05 13:05:46 +0200297 I915_WRITE(ILK_DISPLAY_CHICKEN1,
298 I915_READ(ILK_DISPLAY_CHICKEN1) |
299 ILK_FBCQ_DIS);
Rodrigo Vivi28554162013-05-06 19:37:37 -0300300 } else {
Ville Syrjälä2adb6db2014-03-05 13:05:46 +0200301 /* WaFbcAsynchFlipDisableFbcQueue:hsw,bdw */
Ville Syrjälä8f670bb2014-03-05 13:05:47 +0200302 I915_WRITE(CHICKEN_PIPESL_1(intel_crtc->pipe),
303 I915_READ(CHICKEN_PIPESL_1(intel_crtc->pipe)) |
304 HSW_FBCQ_DIS);
Rodrigo Vivi891348b2013-05-06 19:37:36 -0300305 }
Rodrigo Vivib74ea102013-05-09 14:08:38 -0300306
Rodrigo Viviabe959c2013-05-06 19:37:33 -0300307 I915_WRITE(SNB_DPFC_CTL_SA,
308 SNB_CPU_FENCE_ENABLE | obj->fence_reg);
309 I915_WRITE(DPFC_CPU_FENCE_OFFSET, crtc->y);
310
311 sandybridge_blit_fbc_update(dev);
312
Ville Syrjäläb19870e2013-11-06 23:02:25 +0200313 DRM_DEBUG_KMS("enabled fbc on plane %c\n", plane_name(intel_crtc->plane));
Rodrigo Viviabe959c2013-05-06 19:37:33 -0300314}
315
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300316bool intel_fbc_enabled(struct drm_device *dev)
317{
318 struct drm_i915_private *dev_priv = dev->dev_private;
319
320 if (!dev_priv->display.fbc_enabled)
321 return false;
322
323 return dev_priv->display.fbc_enabled(dev);
324}
325
326static void intel_fbc_work_fn(struct work_struct *__work)
327{
328 struct intel_fbc_work *work =
329 container_of(to_delayed_work(__work),
330 struct intel_fbc_work, work);
331 struct drm_device *dev = work->crtc->dev;
332 struct drm_i915_private *dev_priv = dev->dev_private;
333
334 mutex_lock(&dev->struct_mutex);
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -0700335 if (work == dev_priv->fbc.fbc_work) {
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300336 /* Double check that we haven't switched fb without cancelling
337 * the prior work.
338 */
Matt Roperf4510a22014-04-01 15:22:40 -0700339 if (work->crtc->primary->fb == work->fb) {
Ville Syrjälä993495a2013-12-12 17:27:40 +0200340 dev_priv->display.enable_fbc(work->crtc);
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300341
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -0700342 dev_priv->fbc.plane = to_intel_crtc(work->crtc)->plane;
Matt Roperf4510a22014-04-01 15:22:40 -0700343 dev_priv->fbc.fb_id = work->crtc->primary->fb->base.id;
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -0700344 dev_priv->fbc.y = work->crtc->y;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300345 }
346
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -0700347 dev_priv->fbc.fbc_work = NULL;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300348 }
349 mutex_unlock(&dev->struct_mutex);
350
351 kfree(work);
352}
353
354static void intel_cancel_fbc_work(struct drm_i915_private *dev_priv)
355{
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -0700356 if (dev_priv->fbc.fbc_work == NULL)
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300357 return;
358
359 DRM_DEBUG_KMS("cancelling pending FBC enable\n");
360
361 /* Synchronisation is provided by struct_mutex and checking of
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -0700362 * dev_priv->fbc.fbc_work, so we can perform the cancellation
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300363 * entirely asynchronously.
364 */
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -0700365 if (cancel_delayed_work(&dev_priv->fbc.fbc_work->work))
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300366 /* tasklet was killed before being run, clean up */
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -0700367 kfree(dev_priv->fbc.fbc_work);
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300368
369 /* Mark the work as no longer wanted so that if it does
370 * wake-up (because the work was already running and waiting
371 * for our mutex), it will discover that is no longer
372 * necessary to run.
373 */
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -0700374 dev_priv->fbc.fbc_work = NULL;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300375}
376
Ville Syrjälä993495a2013-12-12 17:27:40 +0200377static void intel_enable_fbc(struct drm_crtc *crtc)
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300378{
379 struct intel_fbc_work *work;
380 struct drm_device *dev = crtc->dev;
381 struct drm_i915_private *dev_priv = dev->dev_private;
382
383 if (!dev_priv->display.enable_fbc)
384 return;
385
386 intel_cancel_fbc_work(dev_priv);
387
Daniel Vetterb14c5672013-09-19 12:18:32 +0200388 work = kzalloc(sizeof(*work), GFP_KERNEL);
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300389 if (work == NULL) {
Paulo Zanoni6cdcb5e2013-06-12 17:27:29 -0300390 DRM_ERROR("Failed to allocate FBC work structure\n");
Ville Syrjälä993495a2013-12-12 17:27:40 +0200391 dev_priv->display.enable_fbc(crtc);
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300392 return;
393 }
394
395 work->crtc = crtc;
Matt Roperf4510a22014-04-01 15:22:40 -0700396 work->fb = crtc->primary->fb;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300397 INIT_DELAYED_WORK(&work->work, intel_fbc_work_fn);
398
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -0700399 dev_priv->fbc.fbc_work = work;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300400
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300401 /* Delay the actual enabling to let pageflipping cease and the
402 * display to settle before starting the compression. Note that
403 * this delay also serves a second purpose: it allows for a
404 * vblank to pass after disabling the FBC before we attempt
405 * to modify the control registers.
406 *
407 * A more complicated solution would involve tracking vblanks
408 * following the termination of the page-flipping sequence
409 * and indeed performing the enable as a co-routine and not
410 * waiting synchronously upon the vblank.
Damien Lespiau7457d612013-06-07 17:41:07 +0100411 *
412 * WaFbcWaitForVBlankBeforeEnable:ilk,snb
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300413 */
414 schedule_delayed_work(&work->work, msecs_to_jiffies(50));
415}
416
417void intel_disable_fbc(struct drm_device *dev)
418{
419 struct drm_i915_private *dev_priv = dev->dev_private;
420
421 intel_cancel_fbc_work(dev_priv);
422
423 if (!dev_priv->display.disable_fbc)
424 return;
425
426 dev_priv->display.disable_fbc(dev);
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -0700427 dev_priv->fbc.plane = -1;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300428}
429
Chris Wilson29ebf902013-07-27 17:23:55 +0100430static bool set_no_fbc_reason(struct drm_i915_private *dev_priv,
431 enum no_fbc_reason reason)
432{
433 if (dev_priv->fbc.no_fbc_reason == reason)
434 return false;
435
436 dev_priv->fbc.no_fbc_reason = reason;
437 return true;
438}
439
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300440/**
441 * intel_update_fbc - enable/disable FBC as needed
442 * @dev: the drm_device
443 *
444 * Set up the framebuffer compression hardware at mode set time. We
445 * enable it if possible:
446 * - plane A only (on pre-965)
447 * - no pixel mulitply/line duplication
448 * - no alpha buffer discard
449 * - no dual wide
Paulo Zanonif85da862013-06-04 16:53:39 -0300450 * - framebuffer <= max_hdisplay in width, max_vdisplay in height
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300451 *
452 * We can't assume that any compression will take place (worst case),
453 * so the compressed buffer has to be the same size as the uncompressed
454 * one. It also must reside (along with the line length buffer) in
455 * stolen memory.
456 *
457 * We need to enable/disable FBC on a global basis.
458 */
459void intel_update_fbc(struct drm_device *dev)
460{
461 struct drm_i915_private *dev_priv = dev->dev_private;
462 struct drm_crtc *crtc = NULL, *tmp_crtc;
463 struct intel_crtc *intel_crtc;
464 struct drm_framebuffer *fb;
465 struct intel_framebuffer *intel_fb;
466 struct drm_i915_gem_object *obj;
Ville Syrjäläef644fd2013-09-04 18:25:21 +0300467 const struct drm_display_mode *adjusted_mode;
Ville Syrjälä37327ab2013-09-04 18:25:28 +0300468 unsigned int max_width, max_height;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300469
Daniel Vetter3a77c4c2014-01-10 08:50:12 +0100470 if (!HAS_FBC(dev)) {
Chris Wilson29ebf902013-07-27 17:23:55 +0100471 set_no_fbc_reason(dev_priv, FBC_UNSUPPORTED);
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300472 return;
Chris Wilson29ebf902013-07-27 17:23:55 +0100473 }
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300474
Jani Nikulad330a952014-01-21 11:24:25 +0200475 if (!i915.powersave) {
Chris Wilson29ebf902013-07-27 17:23:55 +0100476 if (set_no_fbc_reason(dev_priv, FBC_MODULE_PARAM))
477 DRM_DEBUG_KMS("fbc disabled per module param\n");
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300478 return;
Chris Wilson29ebf902013-07-27 17:23:55 +0100479 }
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300480
481 /*
482 * If FBC is already on, we just have to verify that we can
483 * keep it that way...
484 * Need to disable if:
485 * - more than one pipe is active
486 * - changing FBC params (stride, fence, mode)
487 * - new fb is too large to fit in compressed buffer
488 * - going to an unsupported config (interlace, pixel multiply, etc.)
489 */
490 list_for_each_entry(tmp_crtc, &dev->mode_config.crtc_list, head) {
Chris Wilson3490ea52013-01-07 10:11:40 +0000491 if (intel_crtc_active(tmp_crtc) &&
Ville Syrjälä4c445e02013-10-09 17:24:58 +0300492 to_intel_crtc(tmp_crtc)->primary_enabled) {
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300493 if (crtc) {
Chris Wilson29ebf902013-07-27 17:23:55 +0100494 if (set_no_fbc_reason(dev_priv, FBC_MULTIPLE_PIPES))
495 DRM_DEBUG_KMS("more than one pipe active, disabling compression\n");
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300496 goto out_disable;
497 }
498 crtc = tmp_crtc;
499 }
500 }
501
Matt Roperf4510a22014-04-01 15:22:40 -0700502 if (!crtc || crtc->primary->fb == NULL) {
Chris Wilson29ebf902013-07-27 17:23:55 +0100503 if (set_no_fbc_reason(dev_priv, FBC_NO_OUTPUT))
504 DRM_DEBUG_KMS("no output, disabling\n");
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300505 goto out_disable;
506 }
507
508 intel_crtc = to_intel_crtc(crtc);
Matt Roperf4510a22014-04-01 15:22:40 -0700509 fb = crtc->primary->fb;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300510 intel_fb = to_intel_framebuffer(fb);
511 obj = intel_fb->obj;
Ville Syrjäläef644fd2013-09-04 18:25:21 +0300512 adjusted_mode = &intel_crtc->config.adjusted_mode;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300513
Jani Nikulad330a952014-01-21 11:24:25 +0200514 if (i915.enable_fbc < 0 &&
Damien Lespiau8a5729a2013-06-24 16:22:02 +0100515 INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev)) {
Chris Wilson29ebf902013-07-27 17:23:55 +0100516 if (set_no_fbc_reason(dev_priv, FBC_CHIP_DEFAULT))
517 DRM_DEBUG_KMS("disabled per chip default\n");
Damien Lespiau8a5729a2013-06-24 16:22:02 +0100518 goto out_disable;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300519 }
Jani Nikulad330a952014-01-21 11:24:25 +0200520 if (!i915.enable_fbc) {
Chris Wilson29ebf902013-07-27 17:23:55 +0100521 if (set_no_fbc_reason(dev_priv, FBC_MODULE_PARAM))
522 DRM_DEBUG_KMS("fbc disabled per module param\n");
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300523 goto out_disable;
524 }
Ville Syrjäläef644fd2013-09-04 18:25:21 +0300525 if ((adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) ||
526 (adjusted_mode->flags & DRM_MODE_FLAG_DBLSCAN)) {
Chris Wilson29ebf902013-07-27 17:23:55 +0100527 if (set_no_fbc_reason(dev_priv, FBC_UNSUPPORTED_MODE))
528 DRM_DEBUG_KMS("mode incompatible with compression, "
529 "disabling\n");
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300530 goto out_disable;
531 }
Paulo Zanonif85da862013-06-04 16:53:39 -0300532
533 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
Ville Syrjälä37327ab2013-09-04 18:25:28 +0300534 max_width = 4096;
535 max_height = 2048;
Paulo Zanonif85da862013-06-04 16:53:39 -0300536 } else {
Ville Syrjälä37327ab2013-09-04 18:25:28 +0300537 max_width = 2048;
538 max_height = 1536;
Paulo Zanonif85da862013-06-04 16:53:39 -0300539 }
Ville Syrjälä37327ab2013-09-04 18:25:28 +0300540 if (intel_crtc->config.pipe_src_w > max_width ||
541 intel_crtc->config.pipe_src_h > max_height) {
Chris Wilson29ebf902013-07-27 17:23:55 +0100542 if (set_no_fbc_reason(dev_priv, FBC_MODE_TOO_LARGE))
543 DRM_DEBUG_KMS("mode too large for compression, disabling\n");
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300544 goto out_disable;
545 }
Ben Widawsky8f94d242014-02-20 16:01:20 -0800546 if ((INTEL_INFO(dev)->gen < 4 || HAS_DDI(dev)) &&
Ville Syrjäläc5a44aa2013-11-28 17:29:58 +0200547 intel_crtc->plane != PLANE_A) {
Chris Wilson29ebf902013-07-27 17:23:55 +0100548 if (set_no_fbc_reason(dev_priv, FBC_BAD_PLANE))
Ville Syrjäläc5a44aa2013-11-28 17:29:58 +0200549 DRM_DEBUG_KMS("plane not A, disabling compression\n");
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300550 goto out_disable;
551 }
552
553 /* The use of a CPU fence is mandatory in order to detect writes
554 * by the CPU to the scanout and trigger updates to the FBC.
555 */
556 if (obj->tiling_mode != I915_TILING_X ||
557 obj->fence_reg == I915_FENCE_REG_NONE) {
Chris Wilson29ebf902013-07-27 17:23:55 +0100558 if (set_no_fbc_reason(dev_priv, FBC_NOT_TILED))
559 DRM_DEBUG_KMS("framebuffer not tiled or fenced, disabling compression\n");
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300560 goto out_disable;
561 }
562
563 /* If the kernel debugger is active, always disable compression */
564 if (in_dbg_master())
565 goto out_disable;
566
Chris Wilson11be49e2012-11-15 11:32:20 +0000567 if (i915_gem_stolen_setup_compression(dev, intel_fb->obj->base.size)) {
Chris Wilson29ebf902013-07-27 17:23:55 +0100568 if (set_no_fbc_reason(dev_priv, FBC_STOLEN_TOO_SMALL))
569 DRM_DEBUG_KMS("framebuffer too large, disabling compression\n");
Chris Wilson11be49e2012-11-15 11:32:20 +0000570 goto out_disable;
571 }
572
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300573 /* If the scanout has not changed, don't modify the FBC settings.
574 * Note that we make the fundamental assumption that the fb->obj
575 * cannot be unpinned (and have its GTT offset and fence revoked)
576 * without first being decoupled from the scanout and FBC disabled.
577 */
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -0700578 if (dev_priv->fbc.plane == intel_crtc->plane &&
579 dev_priv->fbc.fb_id == fb->base.id &&
580 dev_priv->fbc.y == crtc->y)
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300581 return;
582
583 if (intel_fbc_enabled(dev)) {
584 /* We update FBC along two paths, after changing fb/crtc
585 * configuration (modeswitching) and after page-flipping
586 * finishes. For the latter, we know that not only did
587 * we disable the FBC at the start of the page-flip
588 * sequence, but also more than one vblank has passed.
589 *
590 * For the former case of modeswitching, it is possible
591 * to switch between two FBC valid configurations
592 * instantaneously so we do need to disable the FBC
593 * before we can modify its control registers. We also
594 * have to wait for the next vblank for that to take
595 * effect. However, since we delay enabling FBC we can
596 * assume that a vblank has passed since disabling and
597 * that we can safely alter the registers in the deferred
598 * callback.
599 *
600 * In the scenario that we go from a valid to invalid
601 * and then back to valid FBC configuration we have
602 * no strict enforcement that a vblank occurred since
603 * disabling the FBC. However, along all current pipe
604 * disabling paths we do need to wait for a vblank at
605 * some point. And we wait before enabling FBC anyway.
606 */
607 DRM_DEBUG_KMS("disabling active FBC for update\n");
608 intel_disable_fbc(dev);
609 }
610
Ville Syrjälä993495a2013-12-12 17:27:40 +0200611 intel_enable_fbc(crtc);
Chris Wilson29ebf902013-07-27 17:23:55 +0100612 dev_priv->fbc.no_fbc_reason = FBC_OK;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300613 return;
614
615out_disable:
616 /* Multiple disables should be harmless */
617 if (intel_fbc_enabled(dev)) {
618 DRM_DEBUG_KMS("unsupported config, disabling FBC\n");
619 intel_disable_fbc(dev);
620 }
Chris Wilson11be49e2012-11-15 11:32:20 +0000621 i915_gem_stolen_cleanup_compression(dev);
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300622}
623
Daniel Vetterc921aba2012-04-26 23:28:17 +0200624static void i915_pineview_get_mem_freq(struct drm_device *dev)
625{
Jani Nikula50227e12014-03-31 14:27:21 +0300626 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200627 u32 tmp;
628
629 tmp = I915_READ(CLKCFG);
630
631 switch (tmp & CLKCFG_FSB_MASK) {
632 case CLKCFG_FSB_533:
633 dev_priv->fsb_freq = 533; /* 133*4 */
634 break;
635 case CLKCFG_FSB_800:
636 dev_priv->fsb_freq = 800; /* 200*4 */
637 break;
638 case CLKCFG_FSB_667:
639 dev_priv->fsb_freq = 667; /* 167*4 */
640 break;
641 case CLKCFG_FSB_400:
642 dev_priv->fsb_freq = 400; /* 100*4 */
643 break;
644 }
645
646 switch (tmp & CLKCFG_MEM_MASK) {
647 case CLKCFG_MEM_533:
648 dev_priv->mem_freq = 533;
649 break;
650 case CLKCFG_MEM_667:
651 dev_priv->mem_freq = 667;
652 break;
653 case CLKCFG_MEM_800:
654 dev_priv->mem_freq = 800;
655 break;
656 }
657
658 /* detect pineview DDR3 setting */
659 tmp = I915_READ(CSHRDDR3CTL);
660 dev_priv->is_ddr3 = (tmp & CSHRDDR3CTL_DDR3) ? 1 : 0;
661}
662
663static void i915_ironlake_get_mem_freq(struct drm_device *dev)
664{
Jani Nikula50227e12014-03-31 14:27:21 +0300665 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200666 u16 ddrpll, csipll;
667
668 ddrpll = I915_READ16(DDRMPLL1);
669 csipll = I915_READ16(CSIPLL0);
670
671 switch (ddrpll & 0xff) {
672 case 0xc:
673 dev_priv->mem_freq = 800;
674 break;
675 case 0x10:
676 dev_priv->mem_freq = 1066;
677 break;
678 case 0x14:
679 dev_priv->mem_freq = 1333;
680 break;
681 case 0x18:
682 dev_priv->mem_freq = 1600;
683 break;
684 default:
685 DRM_DEBUG_DRIVER("unknown memory frequency 0x%02x\n",
686 ddrpll & 0xff);
687 dev_priv->mem_freq = 0;
688 break;
689 }
690
Daniel Vetter20e4d402012-08-08 23:35:39 +0200691 dev_priv->ips.r_t = dev_priv->mem_freq;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200692
693 switch (csipll & 0x3ff) {
694 case 0x00c:
695 dev_priv->fsb_freq = 3200;
696 break;
697 case 0x00e:
698 dev_priv->fsb_freq = 3733;
699 break;
700 case 0x010:
701 dev_priv->fsb_freq = 4266;
702 break;
703 case 0x012:
704 dev_priv->fsb_freq = 4800;
705 break;
706 case 0x014:
707 dev_priv->fsb_freq = 5333;
708 break;
709 case 0x016:
710 dev_priv->fsb_freq = 5866;
711 break;
712 case 0x018:
713 dev_priv->fsb_freq = 6400;
714 break;
715 default:
716 DRM_DEBUG_DRIVER("unknown fsb frequency 0x%04x\n",
717 csipll & 0x3ff);
718 dev_priv->fsb_freq = 0;
719 break;
720 }
721
722 if (dev_priv->fsb_freq == 3200) {
Daniel Vetter20e4d402012-08-08 23:35:39 +0200723 dev_priv->ips.c_m = 0;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200724 } else if (dev_priv->fsb_freq > 3200 && dev_priv->fsb_freq <= 4800) {
Daniel Vetter20e4d402012-08-08 23:35:39 +0200725 dev_priv->ips.c_m = 1;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200726 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +0200727 dev_priv->ips.c_m = 2;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200728 }
729}
730
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300731static const struct cxsr_latency cxsr_latency_table[] = {
732 {1, 0, 800, 400, 3382, 33382, 3983, 33983}, /* DDR2-400 SC */
733 {1, 0, 800, 667, 3354, 33354, 3807, 33807}, /* DDR2-667 SC */
734 {1, 0, 800, 800, 3347, 33347, 3763, 33763}, /* DDR2-800 SC */
735 {1, 1, 800, 667, 6420, 36420, 6873, 36873}, /* DDR3-667 SC */
736 {1, 1, 800, 800, 5902, 35902, 6318, 36318}, /* DDR3-800 SC */
737
738 {1, 0, 667, 400, 3400, 33400, 4021, 34021}, /* DDR2-400 SC */
739 {1, 0, 667, 667, 3372, 33372, 3845, 33845}, /* DDR2-667 SC */
740 {1, 0, 667, 800, 3386, 33386, 3822, 33822}, /* DDR2-800 SC */
741 {1, 1, 667, 667, 6438, 36438, 6911, 36911}, /* DDR3-667 SC */
742 {1, 1, 667, 800, 5941, 35941, 6377, 36377}, /* DDR3-800 SC */
743
744 {1, 0, 400, 400, 3472, 33472, 4173, 34173}, /* DDR2-400 SC */
745 {1, 0, 400, 667, 3443, 33443, 3996, 33996}, /* DDR2-667 SC */
746 {1, 0, 400, 800, 3430, 33430, 3946, 33946}, /* DDR2-800 SC */
747 {1, 1, 400, 667, 6509, 36509, 7062, 37062}, /* DDR3-667 SC */
748 {1, 1, 400, 800, 5985, 35985, 6501, 36501}, /* DDR3-800 SC */
749
750 {0, 0, 800, 400, 3438, 33438, 4065, 34065}, /* DDR2-400 SC */
751 {0, 0, 800, 667, 3410, 33410, 3889, 33889}, /* DDR2-667 SC */
752 {0, 0, 800, 800, 3403, 33403, 3845, 33845}, /* DDR2-800 SC */
753 {0, 1, 800, 667, 6476, 36476, 6955, 36955}, /* DDR3-667 SC */
754 {0, 1, 800, 800, 5958, 35958, 6400, 36400}, /* DDR3-800 SC */
755
756 {0, 0, 667, 400, 3456, 33456, 4103, 34106}, /* DDR2-400 SC */
757 {0, 0, 667, 667, 3428, 33428, 3927, 33927}, /* DDR2-667 SC */
758 {0, 0, 667, 800, 3443, 33443, 3905, 33905}, /* DDR2-800 SC */
759 {0, 1, 667, 667, 6494, 36494, 6993, 36993}, /* DDR3-667 SC */
760 {0, 1, 667, 800, 5998, 35998, 6460, 36460}, /* DDR3-800 SC */
761
762 {0, 0, 400, 400, 3528, 33528, 4255, 34255}, /* DDR2-400 SC */
763 {0, 0, 400, 667, 3500, 33500, 4079, 34079}, /* DDR2-667 SC */
764 {0, 0, 400, 800, 3487, 33487, 4029, 34029}, /* DDR2-800 SC */
765 {0, 1, 400, 667, 6566, 36566, 7145, 37145}, /* DDR3-667 SC */
766 {0, 1, 400, 800, 6042, 36042, 6584, 36584}, /* DDR3-800 SC */
767};
768
Daniel Vetter63c62272012-04-21 23:17:55 +0200769static const struct cxsr_latency *intel_get_cxsr_latency(int is_desktop,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300770 int is_ddr3,
771 int fsb,
772 int mem)
773{
774 const struct cxsr_latency *latency;
775 int i;
776
777 if (fsb == 0 || mem == 0)
778 return NULL;
779
780 for (i = 0; i < ARRAY_SIZE(cxsr_latency_table); i++) {
781 latency = &cxsr_latency_table[i];
782 if (is_desktop == latency->is_desktop &&
783 is_ddr3 == latency->is_ddr3 &&
784 fsb == latency->fsb_freq && mem == latency->mem_freq)
785 return latency;
786 }
787
788 DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
789
790 return NULL;
791}
792
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300793static void pineview_disable_cxsr(struct drm_device *dev)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300794{
795 struct drm_i915_private *dev_priv = dev->dev_private;
796
797 /* deactivate cxsr */
798 I915_WRITE(DSPFW3, I915_READ(DSPFW3) & ~PINEVIEW_SELF_REFRESH_EN);
799}
800
801/*
802 * Latency for FIFO fetches is dependent on several factors:
803 * - memory configuration (speed, channels)
804 * - chipset
805 * - current MCH state
806 * It can be fairly high in some situations, so here we assume a fairly
807 * pessimal value. It's a tradeoff between extra memory fetches (if we
808 * set this value too high, the FIFO will fetch frequently to stay full)
809 * and power consumption (set it too low to save power and we might see
810 * FIFO underruns and display "flicker").
811 *
812 * A value of 5us seems to be a good balance; safe for very low end
813 * platforms but not overly aggressive on lower latency configs.
814 */
815static const int latency_ns = 5000;
816
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300817static int i9xx_get_fifo_size(struct drm_device *dev, int plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300818{
819 struct drm_i915_private *dev_priv = dev->dev_private;
820 uint32_t dsparb = I915_READ(DSPARB);
821 int size;
822
823 size = dsparb & 0x7f;
824 if (plane)
825 size = ((dsparb >> DSPARB_CSTART_SHIFT) & 0x7f) - size;
826
827 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
828 plane ? "B" : "A", size);
829
830 return size;
831}
832
Daniel Vetterfeb56b92013-12-14 20:38:30 -0200833static int i830_get_fifo_size(struct drm_device *dev, int plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300834{
835 struct drm_i915_private *dev_priv = dev->dev_private;
836 uint32_t dsparb = I915_READ(DSPARB);
837 int size;
838
839 size = dsparb & 0x1ff;
840 if (plane)
841 size = ((dsparb >> DSPARB_BEND_SHIFT) & 0x1ff) - size;
842 size >>= 1; /* Convert to cachelines */
843
844 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
845 plane ? "B" : "A", size);
846
847 return size;
848}
849
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300850static int i845_get_fifo_size(struct drm_device *dev, int plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300851{
852 struct drm_i915_private *dev_priv = dev->dev_private;
853 uint32_t dsparb = I915_READ(DSPARB);
854 int size;
855
856 size = dsparb & 0x7f;
857 size >>= 2; /* Convert to cachelines */
858
859 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
860 plane ? "B" : "A",
861 size);
862
863 return size;
864}
865
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300866/* Pineview has different values for various configs */
867static const struct intel_watermark_params pineview_display_wm = {
868 PINEVIEW_DISPLAY_FIFO,
869 PINEVIEW_MAX_WM,
870 PINEVIEW_DFT_WM,
871 PINEVIEW_GUARD_WM,
872 PINEVIEW_FIFO_LINE_SIZE
873};
874static const struct intel_watermark_params pineview_display_hplloff_wm = {
875 PINEVIEW_DISPLAY_FIFO,
876 PINEVIEW_MAX_WM,
877 PINEVIEW_DFT_HPLLOFF_WM,
878 PINEVIEW_GUARD_WM,
879 PINEVIEW_FIFO_LINE_SIZE
880};
881static const struct intel_watermark_params pineview_cursor_wm = {
882 PINEVIEW_CURSOR_FIFO,
883 PINEVIEW_CURSOR_MAX_WM,
884 PINEVIEW_CURSOR_DFT_WM,
885 PINEVIEW_CURSOR_GUARD_WM,
886 PINEVIEW_FIFO_LINE_SIZE,
887};
888static const struct intel_watermark_params pineview_cursor_hplloff_wm = {
889 PINEVIEW_CURSOR_FIFO,
890 PINEVIEW_CURSOR_MAX_WM,
891 PINEVIEW_CURSOR_DFT_WM,
892 PINEVIEW_CURSOR_GUARD_WM,
893 PINEVIEW_FIFO_LINE_SIZE
894};
895static const struct intel_watermark_params g4x_wm_info = {
896 G4X_FIFO_SIZE,
897 G4X_MAX_WM,
898 G4X_MAX_WM,
899 2,
900 G4X_FIFO_LINE_SIZE,
901};
902static const struct intel_watermark_params g4x_cursor_wm_info = {
903 I965_CURSOR_FIFO,
904 I965_CURSOR_MAX_WM,
905 I965_CURSOR_DFT_WM,
906 2,
907 G4X_FIFO_LINE_SIZE,
908};
909static const struct intel_watermark_params valleyview_wm_info = {
910 VALLEYVIEW_FIFO_SIZE,
911 VALLEYVIEW_MAX_WM,
912 VALLEYVIEW_MAX_WM,
913 2,
914 G4X_FIFO_LINE_SIZE,
915};
916static const struct intel_watermark_params valleyview_cursor_wm_info = {
917 I965_CURSOR_FIFO,
918 VALLEYVIEW_CURSOR_MAX_WM,
919 I965_CURSOR_DFT_WM,
920 2,
921 G4X_FIFO_LINE_SIZE,
922};
923static const struct intel_watermark_params i965_cursor_wm_info = {
924 I965_CURSOR_FIFO,
925 I965_CURSOR_MAX_WM,
926 I965_CURSOR_DFT_WM,
927 2,
928 I915_FIFO_LINE_SIZE,
929};
930static const struct intel_watermark_params i945_wm_info = {
931 I945_FIFO_SIZE,
932 I915_MAX_WM,
933 1,
934 2,
935 I915_FIFO_LINE_SIZE
936};
937static const struct intel_watermark_params i915_wm_info = {
938 I915_FIFO_SIZE,
939 I915_MAX_WM,
940 1,
941 2,
942 I915_FIFO_LINE_SIZE
943};
Daniel Vetterfeb56b92013-12-14 20:38:30 -0200944static const struct intel_watermark_params i830_wm_info = {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300945 I855GM_FIFO_SIZE,
946 I915_MAX_WM,
947 1,
948 2,
949 I830_FIFO_LINE_SIZE
950};
Daniel Vetterfeb56b92013-12-14 20:38:30 -0200951static const struct intel_watermark_params i845_wm_info = {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300952 I830_FIFO_SIZE,
953 I915_MAX_WM,
954 1,
955 2,
956 I830_FIFO_LINE_SIZE
957};
958
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300959/**
960 * intel_calculate_wm - calculate watermark level
961 * @clock_in_khz: pixel clock
962 * @wm: chip FIFO params
963 * @pixel_size: display pixel size
964 * @latency_ns: memory latency for the platform
965 *
966 * Calculate the watermark level (the level at which the display plane will
967 * start fetching from memory again). Each chip has a different display
968 * FIFO size and allocation, so the caller needs to figure that out and pass
969 * in the correct intel_watermark_params structure.
970 *
971 * As the pixel clock runs, the FIFO will be drained at a rate that depends
972 * on the pixel size. When it reaches the watermark level, it'll start
973 * fetching FIFO line sized based chunks from memory until the FIFO fills
974 * past the watermark point. If the FIFO drains completely, a FIFO underrun
975 * will occur, and a display engine hang could result.
976 */
977static unsigned long intel_calculate_wm(unsigned long clock_in_khz,
978 const struct intel_watermark_params *wm,
979 int fifo_size,
980 int pixel_size,
981 unsigned long latency_ns)
982{
983 long entries_required, wm_size;
984
985 /*
986 * Note: we need to make sure we don't overflow for various clock &
987 * latency values.
988 * clocks go from a few thousand to several hundred thousand.
989 * latency is usually a few thousand
990 */
991 entries_required = ((clock_in_khz / 1000) * pixel_size * latency_ns) /
992 1000;
993 entries_required = DIV_ROUND_UP(entries_required, wm->cacheline_size);
994
995 DRM_DEBUG_KMS("FIFO entries required for mode: %ld\n", entries_required);
996
997 wm_size = fifo_size - (entries_required + wm->guard_size);
998
999 DRM_DEBUG_KMS("FIFO watermark level: %ld\n", wm_size);
1000
1001 /* Don't promote wm_size to unsigned... */
1002 if (wm_size > (long)wm->max_wm)
1003 wm_size = wm->max_wm;
1004 if (wm_size <= 0)
1005 wm_size = wm->default_wm;
1006 return wm_size;
1007}
1008
1009static struct drm_crtc *single_enabled_crtc(struct drm_device *dev)
1010{
1011 struct drm_crtc *crtc, *enabled = NULL;
1012
1013 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Chris Wilson3490ea52013-01-07 10:11:40 +00001014 if (intel_crtc_active(crtc)) {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001015 if (enabled)
1016 return NULL;
1017 enabled = crtc;
1018 }
1019 }
1020
1021 return enabled;
1022}
1023
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001024static void pineview_update_wm(struct drm_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001025{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001026 struct drm_device *dev = unused_crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001027 struct drm_i915_private *dev_priv = dev->dev_private;
1028 struct drm_crtc *crtc;
1029 const struct cxsr_latency *latency;
1030 u32 reg;
1031 unsigned long wm;
1032
1033 latency = intel_get_cxsr_latency(IS_PINEVIEW_G(dev), dev_priv->is_ddr3,
1034 dev_priv->fsb_freq, dev_priv->mem_freq);
1035 if (!latency) {
1036 DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
1037 pineview_disable_cxsr(dev);
1038 return;
1039 }
1040
1041 crtc = single_enabled_crtc(dev);
1042 if (crtc) {
Damien Lespiau241bfc32013-09-25 16:45:37 +01001043 const struct drm_display_mode *adjusted_mode;
Matt Roperf4510a22014-04-01 15:22:40 -07001044 int pixel_size = crtc->primary->fb->bits_per_pixel / 8;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001045 int clock;
1046
1047 adjusted_mode = &to_intel_crtc(crtc)->config.adjusted_mode;
1048 clock = adjusted_mode->crtc_clock;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001049
1050 /* Display SR */
1051 wm = intel_calculate_wm(clock, &pineview_display_wm,
1052 pineview_display_wm.fifo_size,
1053 pixel_size, latency->display_sr);
1054 reg = I915_READ(DSPFW1);
1055 reg &= ~DSPFW_SR_MASK;
1056 reg |= wm << DSPFW_SR_SHIFT;
1057 I915_WRITE(DSPFW1, reg);
1058 DRM_DEBUG_KMS("DSPFW1 register is %x\n", reg);
1059
1060 /* cursor SR */
1061 wm = intel_calculate_wm(clock, &pineview_cursor_wm,
1062 pineview_display_wm.fifo_size,
1063 pixel_size, latency->cursor_sr);
1064 reg = I915_READ(DSPFW3);
1065 reg &= ~DSPFW_CURSOR_SR_MASK;
1066 reg |= (wm & 0x3f) << DSPFW_CURSOR_SR_SHIFT;
1067 I915_WRITE(DSPFW3, reg);
1068
1069 /* Display HPLL off SR */
1070 wm = intel_calculate_wm(clock, &pineview_display_hplloff_wm,
1071 pineview_display_hplloff_wm.fifo_size,
1072 pixel_size, latency->display_hpll_disable);
1073 reg = I915_READ(DSPFW3);
1074 reg &= ~DSPFW_HPLL_SR_MASK;
1075 reg |= wm & DSPFW_HPLL_SR_MASK;
1076 I915_WRITE(DSPFW3, reg);
1077
1078 /* cursor HPLL off SR */
1079 wm = intel_calculate_wm(clock, &pineview_cursor_hplloff_wm,
1080 pineview_display_hplloff_wm.fifo_size,
1081 pixel_size, latency->cursor_hpll_disable);
1082 reg = I915_READ(DSPFW3);
1083 reg &= ~DSPFW_HPLL_CURSOR_MASK;
1084 reg |= (wm & 0x3f) << DSPFW_HPLL_CURSOR_SHIFT;
1085 I915_WRITE(DSPFW3, reg);
1086 DRM_DEBUG_KMS("DSPFW3 register is %x\n", reg);
1087
1088 /* activate cxsr */
1089 I915_WRITE(DSPFW3,
1090 I915_READ(DSPFW3) | PINEVIEW_SELF_REFRESH_EN);
1091 DRM_DEBUG_KMS("Self-refresh is enabled\n");
1092 } else {
1093 pineview_disable_cxsr(dev);
1094 DRM_DEBUG_KMS("Self-refresh is disabled\n");
1095 }
1096}
1097
1098static bool g4x_compute_wm0(struct drm_device *dev,
1099 int plane,
1100 const struct intel_watermark_params *display,
1101 int display_latency_ns,
1102 const struct intel_watermark_params *cursor,
1103 int cursor_latency_ns,
1104 int *plane_wm,
1105 int *cursor_wm)
1106{
1107 struct drm_crtc *crtc;
Ville Syrjälä4fe85902013-09-04 18:25:22 +03001108 const struct drm_display_mode *adjusted_mode;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001109 int htotal, hdisplay, clock, pixel_size;
1110 int line_time_us, line_count;
1111 int entries, tlb_miss;
1112
1113 crtc = intel_get_crtc_for_plane(dev, plane);
Chris Wilson3490ea52013-01-07 10:11:40 +00001114 if (!intel_crtc_active(crtc)) {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001115 *cursor_wm = cursor->guard_size;
1116 *plane_wm = display->guard_size;
1117 return false;
1118 }
1119
Ville Syrjälä4fe85902013-09-04 18:25:22 +03001120 adjusted_mode = &to_intel_crtc(crtc)->config.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001121 clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -08001122 htotal = adjusted_mode->crtc_htotal;
Ville Syrjälä37327ab2013-09-04 18:25:28 +03001123 hdisplay = to_intel_crtc(crtc)->config.pipe_src_w;
Matt Roperf4510a22014-04-01 15:22:40 -07001124 pixel_size = crtc->primary->fb->bits_per_pixel / 8;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001125
1126 /* Use the small buffer method to calculate plane watermark */
1127 entries = ((clock * pixel_size / 1000) * display_latency_ns) / 1000;
1128 tlb_miss = display->fifo_size*display->cacheline_size - hdisplay * 8;
1129 if (tlb_miss > 0)
1130 entries += tlb_miss;
1131 entries = DIV_ROUND_UP(entries, display->cacheline_size);
1132 *plane_wm = entries + display->guard_size;
1133 if (*plane_wm > (int)display->max_wm)
1134 *plane_wm = display->max_wm;
1135
1136 /* Use the large buffer method to calculate cursor watermark */
Ville Syrjälä922044c2014-02-14 14:18:57 +02001137 line_time_us = max(htotal * 1000 / clock, 1);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001138 line_count = (cursor_latency_ns / line_time_us + 1000) / 1000;
Chris Wilson7bb836d2014-03-26 12:38:14 +00001139 entries = line_count * to_intel_crtc(crtc)->cursor_width * pixel_size;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001140 tlb_miss = cursor->fifo_size*cursor->cacheline_size - hdisplay * 8;
1141 if (tlb_miss > 0)
1142 entries += tlb_miss;
1143 entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
1144 *cursor_wm = entries + cursor->guard_size;
1145 if (*cursor_wm > (int)cursor->max_wm)
1146 *cursor_wm = (int)cursor->max_wm;
1147
1148 return true;
1149}
1150
1151/*
1152 * Check the wm result.
1153 *
1154 * If any calculated watermark values is larger than the maximum value that
1155 * can be programmed into the associated watermark register, that watermark
1156 * must be disabled.
1157 */
1158static bool g4x_check_srwm(struct drm_device *dev,
1159 int display_wm, int cursor_wm,
1160 const struct intel_watermark_params *display,
1161 const struct intel_watermark_params *cursor)
1162{
1163 DRM_DEBUG_KMS("SR watermark: display plane %d, cursor %d\n",
1164 display_wm, cursor_wm);
1165
1166 if (display_wm > display->max_wm) {
1167 DRM_DEBUG_KMS("display watermark is too large(%d/%ld), disabling\n",
1168 display_wm, display->max_wm);
1169 return false;
1170 }
1171
1172 if (cursor_wm > cursor->max_wm) {
1173 DRM_DEBUG_KMS("cursor watermark is too large(%d/%ld), disabling\n",
1174 cursor_wm, cursor->max_wm);
1175 return false;
1176 }
1177
1178 if (!(display_wm || cursor_wm)) {
1179 DRM_DEBUG_KMS("SR latency is 0, disabling\n");
1180 return false;
1181 }
1182
1183 return true;
1184}
1185
1186static bool g4x_compute_srwm(struct drm_device *dev,
1187 int plane,
1188 int latency_ns,
1189 const struct intel_watermark_params *display,
1190 const struct intel_watermark_params *cursor,
1191 int *display_wm, int *cursor_wm)
1192{
1193 struct drm_crtc *crtc;
Ville Syrjälä4fe85902013-09-04 18:25:22 +03001194 const struct drm_display_mode *adjusted_mode;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001195 int hdisplay, htotal, pixel_size, clock;
1196 unsigned long line_time_us;
1197 int line_count, line_size;
1198 int small, large;
1199 int entries;
1200
1201 if (!latency_ns) {
1202 *display_wm = *cursor_wm = 0;
1203 return false;
1204 }
1205
1206 crtc = intel_get_crtc_for_plane(dev, plane);
Ville Syrjälä4fe85902013-09-04 18:25:22 +03001207 adjusted_mode = &to_intel_crtc(crtc)->config.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001208 clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -08001209 htotal = adjusted_mode->crtc_htotal;
Ville Syrjälä37327ab2013-09-04 18:25:28 +03001210 hdisplay = to_intel_crtc(crtc)->config.pipe_src_w;
Matt Roperf4510a22014-04-01 15:22:40 -07001211 pixel_size = crtc->primary->fb->bits_per_pixel / 8;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001212
Ville Syrjälä922044c2014-02-14 14:18:57 +02001213 line_time_us = max(htotal * 1000 / clock, 1);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001214 line_count = (latency_ns / line_time_us + 1000) / 1000;
1215 line_size = hdisplay * pixel_size;
1216
1217 /* Use the minimum of the small and large buffer method for primary */
1218 small = ((clock * pixel_size / 1000) * latency_ns) / 1000;
1219 large = line_count * line_size;
1220
1221 entries = DIV_ROUND_UP(min(small, large), display->cacheline_size);
1222 *display_wm = entries + display->guard_size;
1223
1224 /* calculate the self-refresh watermark for display cursor */
Chris Wilson7bb836d2014-03-26 12:38:14 +00001225 entries = line_count * pixel_size * to_intel_crtc(crtc)->cursor_width;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001226 entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
1227 *cursor_wm = entries + cursor->guard_size;
1228
1229 return g4x_check_srwm(dev,
1230 *display_wm, *cursor_wm,
1231 display, cursor);
1232}
1233
1234static bool vlv_compute_drain_latency(struct drm_device *dev,
1235 int plane,
1236 int *plane_prec_mult,
1237 int *plane_dl,
1238 int *cursor_prec_mult,
1239 int *cursor_dl)
1240{
1241 struct drm_crtc *crtc;
1242 int clock, pixel_size;
1243 int entries;
1244
1245 crtc = intel_get_crtc_for_plane(dev, plane);
Chris Wilson3490ea52013-01-07 10:11:40 +00001246 if (!intel_crtc_active(crtc))
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001247 return false;
1248
Damien Lespiau241bfc32013-09-25 16:45:37 +01001249 clock = to_intel_crtc(crtc)->config.adjusted_mode.crtc_clock;
Matt Roperf4510a22014-04-01 15:22:40 -07001250 pixel_size = crtc->primary->fb->bits_per_pixel / 8; /* BPP */
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001251
1252 entries = (clock / 1000) * pixel_size;
1253 *plane_prec_mult = (entries > 256) ?
1254 DRAIN_LATENCY_PRECISION_32 : DRAIN_LATENCY_PRECISION_16;
1255 *plane_dl = (64 * (*plane_prec_mult) * 4) / ((clock / 1000) *
1256 pixel_size);
1257
1258 entries = (clock / 1000) * 4; /* BPP is always 4 for cursor */
1259 *cursor_prec_mult = (entries > 256) ?
1260 DRAIN_LATENCY_PRECISION_32 : DRAIN_LATENCY_PRECISION_16;
1261 *cursor_dl = (64 * (*cursor_prec_mult) * 4) / ((clock / 1000) * 4);
1262
1263 return true;
1264}
1265
1266/*
1267 * Update drain latency registers of memory arbiter
1268 *
1269 * Valleyview SoC has a new memory arbiter and needs drain latency registers
1270 * to be programmed. Each plane has a drain latency multiplier and a drain
1271 * latency value.
1272 */
1273
1274static void vlv_update_drain_latency(struct drm_device *dev)
1275{
1276 struct drm_i915_private *dev_priv = dev->dev_private;
1277 int planea_prec, planea_dl, planeb_prec, planeb_dl;
1278 int cursora_prec, cursora_dl, cursorb_prec, cursorb_dl;
1279 int plane_prec_mult, cursor_prec_mult; /* Precision multiplier is
1280 either 16 or 32 */
1281
1282 /* For plane A, Cursor A */
1283 if (vlv_compute_drain_latency(dev, 0, &plane_prec_mult, &planea_dl,
1284 &cursor_prec_mult, &cursora_dl)) {
1285 cursora_prec = (cursor_prec_mult == DRAIN_LATENCY_PRECISION_32) ?
1286 DDL_CURSORA_PRECISION_32 : DDL_CURSORA_PRECISION_16;
1287 planea_prec = (plane_prec_mult == DRAIN_LATENCY_PRECISION_32) ?
1288 DDL_PLANEA_PRECISION_32 : DDL_PLANEA_PRECISION_16;
1289
1290 I915_WRITE(VLV_DDL1, cursora_prec |
1291 (cursora_dl << DDL_CURSORA_SHIFT) |
1292 planea_prec | planea_dl);
1293 }
1294
1295 /* For plane B, Cursor B */
1296 if (vlv_compute_drain_latency(dev, 1, &plane_prec_mult, &planeb_dl,
1297 &cursor_prec_mult, &cursorb_dl)) {
1298 cursorb_prec = (cursor_prec_mult == DRAIN_LATENCY_PRECISION_32) ?
1299 DDL_CURSORB_PRECISION_32 : DDL_CURSORB_PRECISION_16;
1300 planeb_prec = (plane_prec_mult == DRAIN_LATENCY_PRECISION_32) ?
1301 DDL_PLANEB_PRECISION_32 : DDL_PLANEB_PRECISION_16;
1302
1303 I915_WRITE(VLV_DDL2, cursorb_prec |
1304 (cursorb_dl << DDL_CURSORB_SHIFT) |
1305 planeb_prec | planeb_dl);
1306 }
1307}
1308
1309#define single_plane_enabled(mask) is_power_of_2(mask)
1310
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001311static void valleyview_update_wm(struct drm_crtc *crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001312{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001313 struct drm_device *dev = crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001314 static const int sr_latency_ns = 12000;
1315 struct drm_i915_private *dev_priv = dev->dev_private;
1316 int planea_wm, planeb_wm, cursora_wm, cursorb_wm;
1317 int plane_sr, cursor_sr;
Chris Wilsonaf6c4572012-12-11 12:01:43 +00001318 int ignore_plane_sr, ignore_cursor_sr;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001319 unsigned int enabled = 0;
1320
1321 vlv_update_drain_latency(dev);
1322
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001323 if (g4x_compute_wm0(dev, PIPE_A,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001324 &valleyview_wm_info, latency_ns,
1325 &valleyview_cursor_wm_info, latency_ns,
1326 &planea_wm, &cursora_wm))
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001327 enabled |= 1 << PIPE_A;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001328
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001329 if (g4x_compute_wm0(dev, PIPE_B,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001330 &valleyview_wm_info, latency_ns,
1331 &valleyview_cursor_wm_info, latency_ns,
1332 &planeb_wm, &cursorb_wm))
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001333 enabled |= 1 << PIPE_B;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001334
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001335 if (single_plane_enabled(enabled) &&
1336 g4x_compute_srwm(dev, ffs(enabled) - 1,
1337 sr_latency_ns,
1338 &valleyview_wm_info,
1339 &valleyview_cursor_wm_info,
Chris Wilsonaf6c4572012-12-11 12:01:43 +00001340 &plane_sr, &ignore_cursor_sr) &&
1341 g4x_compute_srwm(dev, ffs(enabled) - 1,
1342 2*sr_latency_ns,
1343 &valleyview_wm_info,
1344 &valleyview_cursor_wm_info,
Chris Wilson52bd02d2012-12-07 10:43:24 +00001345 &ignore_plane_sr, &cursor_sr)) {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001346 I915_WRITE(FW_BLC_SELF_VLV, FW_CSPWRDWNEN);
Chris Wilson52bd02d2012-12-07 10:43:24 +00001347 } else {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001348 I915_WRITE(FW_BLC_SELF_VLV,
1349 I915_READ(FW_BLC_SELF_VLV) & ~FW_CSPWRDWNEN);
Chris Wilson52bd02d2012-12-07 10:43:24 +00001350 plane_sr = cursor_sr = 0;
1351 }
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001352
1353 DRM_DEBUG_KMS("Setting FIFO watermarks - A: plane=%d, cursor=%d, B: plane=%d, cursor=%d, SR: plane=%d, cursor=%d\n",
1354 planea_wm, cursora_wm,
1355 planeb_wm, cursorb_wm,
1356 plane_sr, cursor_sr);
1357
1358 I915_WRITE(DSPFW1,
1359 (plane_sr << DSPFW_SR_SHIFT) |
1360 (cursorb_wm << DSPFW_CURSORB_SHIFT) |
1361 (planeb_wm << DSPFW_PLANEB_SHIFT) |
1362 planea_wm);
1363 I915_WRITE(DSPFW2,
Chris Wilson8c919b22012-12-04 16:33:19 +00001364 (I915_READ(DSPFW2) & ~DSPFW_CURSORA_MASK) |
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001365 (cursora_wm << DSPFW_CURSORA_SHIFT));
1366 I915_WRITE(DSPFW3,
Chris Wilson8c919b22012-12-04 16:33:19 +00001367 (I915_READ(DSPFW3) & ~DSPFW_CURSOR_SR_MASK) |
1368 (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001369}
1370
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001371static void g4x_update_wm(struct drm_crtc *crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001372{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001373 struct drm_device *dev = crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001374 static const int sr_latency_ns = 12000;
1375 struct drm_i915_private *dev_priv = dev->dev_private;
1376 int planea_wm, planeb_wm, cursora_wm, cursorb_wm;
1377 int plane_sr, cursor_sr;
1378 unsigned int enabled = 0;
1379
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001380 if (g4x_compute_wm0(dev, PIPE_A,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001381 &g4x_wm_info, latency_ns,
1382 &g4x_cursor_wm_info, latency_ns,
1383 &planea_wm, &cursora_wm))
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001384 enabled |= 1 << PIPE_A;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001385
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001386 if (g4x_compute_wm0(dev, PIPE_B,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001387 &g4x_wm_info, latency_ns,
1388 &g4x_cursor_wm_info, latency_ns,
1389 &planeb_wm, &cursorb_wm))
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001390 enabled |= 1 << PIPE_B;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001391
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001392 if (single_plane_enabled(enabled) &&
1393 g4x_compute_srwm(dev, ffs(enabled) - 1,
1394 sr_latency_ns,
1395 &g4x_wm_info,
1396 &g4x_cursor_wm_info,
Chris Wilson52bd02d2012-12-07 10:43:24 +00001397 &plane_sr, &cursor_sr)) {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001398 I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_EN);
Chris Wilson52bd02d2012-12-07 10:43:24 +00001399 } else {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001400 I915_WRITE(FW_BLC_SELF,
1401 I915_READ(FW_BLC_SELF) & ~FW_BLC_SELF_EN);
Chris Wilson52bd02d2012-12-07 10:43:24 +00001402 plane_sr = cursor_sr = 0;
1403 }
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001404
1405 DRM_DEBUG_KMS("Setting FIFO watermarks - A: plane=%d, cursor=%d, B: plane=%d, cursor=%d, SR: plane=%d, cursor=%d\n",
1406 planea_wm, cursora_wm,
1407 planeb_wm, cursorb_wm,
1408 plane_sr, cursor_sr);
1409
1410 I915_WRITE(DSPFW1,
1411 (plane_sr << DSPFW_SR_SHIFT) |
1412 (cursorb_wm << DSPFW_CURSORB_SHIFT) |
1413 (planeb_wm << DSPFW_PLANEB_SHIFT) |
1414 planea_wm);
1415 I915_WRITE(DSPFW2,
Chris Wilson8c919b22012-12-04 16:33:19 +00001416 (I915_READ(DSPFW2) & ~DSPFW_CURSORA_MASK) |
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001417 (cursora_wm << DSPFW_CURSORA_SHIFT));
1418 /* HPLL off in SR has some issues on G4x... disable it */
1419 I915_WRITE(DSPFW3,
Chris Wilson8c919b22012-12-04 16:33:19 +00001420 (I915_READ(DSPFW3) & ~(DSPFW_HPLL_SR_EN | DSPFW_CURSOR_SR_MASK)) |
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001421 (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
1422}
1423
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001424static void i965_update_wm(struct drm_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001425{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001426 struct drm_device *dev = unused_crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001427 struct drm_i915_private *dev_priv = dev->dev_private;
1428 struct drm_crtc *crtc;
1429 int srwm = 1;
1430 int cursor_sr = 16;
1431
1432 /* Calc sr entries for one plane configs */
1433 crtc = single_enabled_crtc(dev);
1434 if (crtc) {
1435 /* self-refresh has much higher latency */
1436 static const int sr_latency_ns = 12000;
Ville Syrjälä4fe85902013-09-04 18:25:22 +03001437 const struct drm_display_mode *adjusted_mode =
1438 &to_intel_crtc(crtc)->config.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001439 int clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -08001440 int htotal = adjusted_mode->crtc_htotal;
Ville Syrjälä37327ab2013-09-04 18:25:28 +03001441 int hdisplay = to_intel_crtc(crtc)->config.pipe_src_w;
Matt Roperf4510a22014-04-01 15:22:40 -07001442 int pixel_size = crtc->primary->fb->bits_per_pixel / 8;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001443 unsigned long line_time_us;
1444 int entries;
1445
Ville Syrjälä922044c2014-02-14 14:18:57 +02001446 line_time_us = max(htotal * 1000 / clock, 1);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001447
1448 /* Use ns/us then divide to preserve precision */
1449 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
1450 pixel_size * hdisplay;
1451 entries = DIV_ROUND_UP(entries, I915_FIFO_LINE_SIZE);
1452 srwm = I965_FIFO_SIZE - entries;
1453 if (srwm < 0)
1454 srwm = 1;
1455 srwm &= 0x1ff;
1456 DRM_DEBUG_KMS("self-refresh entries: %d, wm: %d\n",
1457 entries, srwm);
1458
1459 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
Chris Wilson7bb836d2014-03-26 12:38:14 +00001460 pixel_size * to_intel_crtc(crtc)->cursor_width;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001461 entries = DIV_ROUND_UP(entries,
1462 i965_cursor_wm_info.cacheline_size);
1463 cursor_sr = i965_cursor_wm_info.fifo_size -
1464 (entries + i965_cursor_wm_info.guard_size);
1465
1466 if (cursor_sr > i965_cursor_wm_info.max_wm)
1467 cursor_sr = i965_cursor_wm_info.max_wm;
1468
1469 DRM_DEBUG_KMS("self-refresh watermark: display plane %d "
1470 "cursor %d\n", srwm, cursor_sr);
1471
1472 if (IS_CRESTLINE(dev))
1473 I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_EN);
1474 } else {
1475 /* Turn off self refresh if both pipes are enabled */
1476 if (IS_CRESTLINE(dev))
1477 I915_WRITE(FW_BLC_SELF, I915_READ(FW_BLC_SELF)
1478 & ~FW_BLC_SELF_EN);
1479 }
1480
1481 DRM_DEBUG_KMS("Setting FIFO watermarks - A: 8, B: 8, C: 8, SR %d\n",
1482 srwm);
1483
1484 /* 965 has limitations... */
1485 I915_WRITE(DSPFW1, (srwm << DSPFW_SR_SHIFT) |
1486 (8 << 16) | (8 << 8) | (8 << 0));
1487 I915_WRITE(DSPFW2, (8 << 8) | (8 << 0));
1488 /* update cursor SR watermark */
1489 I915_WRITE(DSPFW3, (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
1490}
1491
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001492static void i9xx_update_wm(struct drm_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001493{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001494 struct drm_device *dev = unused_crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001495 struct drm_i915_private *dev_priv = dev->dev_private;
1496 const struct intel_watermark_params *wm_info;
1497 uint32_t fwater_lo;
1498 uint32_t fwater_hi;
1499 int cwm, srwm = 1;
1500 int fifo_size;
1501 int planea_wm, planeb_wm;
1502 struct drm_crtc *crtc, *enabled = NULL;
1503
1504 if (IS_I945GM(dev))
1505 wm_info = &i945_wm_info;
1506 else if (!IS_GEN2(dev))
1507 wm_info = &i915_wm_info;
1508 else
Daniel Vetterfeb56b92013-12-14 20:38:30 -02001509 wm_info = &i830_wm_info;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001510
1511 fifo_size = dev_priv->display.get_fifo_size(dev, 0);
1512 crtc = intel_get_crtc_for_plane(dev, 0);
Chris Wilson3490ea52013-01-07 10:11:40 +00001513 if (intel_crtc_active(crtc)) {
Damien Lespiau241bfc32013-09-25 16:45:37 +01001514 const struct drm_display_mode *adjusted_mode;
Matt Roperf4510a22014-04-01 15:22:40 -07001515 int cpp = crtc->primary->fb->bits_per_pixel / 8;
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001516 if (IS_GEN2(dev))
1517 cpp = 4;
1518
Damien Lespiau241bfc32013-09-25 16:45:37 +01001519 adjusted_mode = &to_intel_crtc(crtc)->config.adjusted_mode;
1520 planea_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001521 wm_info, fifo_size, cpp,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001522 latency_ns);
1523 enabled = crtc;
1524 } else
1525 planea_wm = fifo_size - wm_info->guard_size;
1526
1527 fifo_size = dev_priv->display.get_fifo_size(dev, 1);
1528 crtc = intel_get_crtc_for_plane(dev, 1);
Chris Wilson3490ea52013-01-07 10:11:40 +00001529 if (intel_crtc_active(crtc)) {
Damien Lespiau241bfc32013-09-25 16:45:37 +01001530 const struct drm_display_mode *adjusted_mode;
Matt Roperf4510a22014-04-01 15:22:40 -07001531 int cpp = crtc->primary->fb->bits_per_pixel / 8;
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001532 if (IS_GEN2(dev))
1533 cpp = 4;
1534
Damien Lespiau241bfc32013-09-25 16:45:37 +01001535 adjusted_mode = &to_intel_crtc(crtc)->config.adjusted_mode;
1536 planeb_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001537 wm_info, fifo_size, cpp,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001538 latency_ns);
1539 if (enabled == NULL)
1540 enabled = crtc;
1541 else
1542 enabled = NULL;
1543 } else
1544 planeb_wm = fifo_size - wm_info->guard_size;
1545
1546 DRM_DEBUG_KMS("FIFO watermarks - A: %d, B: %d\n", planea_wm, planeb_wm);
1547
1548 /*
1549 * Overlay gets an aggressive default since video jitter is bad.
1550 */
1551 cwm = 2;
1552
1553 /* Play safe and disable self-refresh before adjusting watermarks. */
1554 if (IS_I945G(dev) || IS_I945GM(dev))
1555 I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_EN_MASK | 0);
1556 else if (IS_I915GM(dev))
Ville Syrjälä3f2dc5a2014-01-10 14:06:47 +02001557 I915_WRITE(INSTPM, _MASKED_BIT_DISABLE(INSTPM_SELF_EN));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001558
1559 /* Calc sr entries for one plane configs */
1560 if (HAS_FW_BLC(dev) && enabled) {
1561 /* self-refresh has much higher latency */
1562 static const int sr_latency_ns = 6000;
Ville Syrjälä4fe85902013-09-04 18:25:22 +03001563 const struct drm_display_mode *adjusted_mode =
1564 &to_intel_crtc(enabled)->config.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001565 int clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -08001566 int htotal = adjusted_mode->crtc_htotal;
Daniel Vetterf727b492013-11-20 15:02:10 +01001567 int hdisplay = to_intel_crtc(enabled)->config.pipe_src_w;
Matt Roperf4510a22014-04-01 15:22:40 -07001568 int pixel_size = enabled->primary->fb->bits_per_pixel / 8;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001569 unsigned long line_time_us;
1570 int entries;
1571
Ville Syrjälä922044c2014-02-14 14:18:57 +02001572 line_time_us = max(htotal * 1000 / clock, 1);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001573
1574 /* Use ns/us then divide to preserve precision */
1575 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
1576 pixel_size * hdisplay;
1577 entries = DIV_ROUND_UP(entries, wm_info->cacheline_size);
1578 DRM_DEBUG_KMS("self-refresh entries: %d\n", entries);
1579 srwm = wm_info->fifo_size - entries;
1580 if (srwm < 0)
1581 srwm = 1;
1582
1583 if (IS_I945G(dev) || IS_I945GM(dev))
1584 I915_WRITE(FW_BLC_SELF,
1585 FW_BLC_SELF_FIFO_MASK | (srwm & 0xff));
1586 else if (IS_I915GM(dev))
1587 I915_WRITE(FW_BLC_SELF, srwm & 0x3f);
1588 }
1589
1590 DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d, B: %d, C: %d, SR %d\n",
1591 planea_wm, planeb_wm, cwm, srwm);
1592
1593 fwater_lo = ((planeb_wm & 0x3f) << 16) | (planea_wm & 0x3f);
1594 fwater_hi = (cwm & 0x1f);
1595
1596 /* Set request length to 8 cachelines per fetch */
1597 fwater_lo = fwater_lo | (1 << 24) | (1 << 8);
1598 fwater_hi = fwater_hi | (1 << 8);
1599
1600 I915_WRITE(FW_BLC, fwater_lo);
1601 I915_WRITE(FW_BLC2, fwater_hi);
1602
1603 if (HAS_FW_BLC(dev)) {
1604 if (enabled) {
1605 if (IS_I945G(dev) || IS_I945GM(dev))
1606 I915_WRITE(FW_BLC_SELF,
1607 FW_BLC_SELF_EN_MASK | FW_BLC_SELF_EN);
1608 else if (IS_I915GM(dev))
Ville Syrjälä3f2dc5a2014-01-10 14:06:47 +02001609 I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_SELF_EN));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001610 DRM_DEBUG_KMS("memory self refresh enabled\n");
1611 } else
1612 DRM_DEBUG_KMS("memory self refresh disabled\n");
1613 }
1614}
1615
Daniel Vetterfeb56b92013-12-14 20:38:30 -02001616static void i845_update_wm(struct drm_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001617{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001618 struct drm_device *dev = unused_crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001619 struct drm_i915_private *dev_priv = dev->dev_private;
1620 struct drm_crtc *crtc;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001621 const struct drm_display_mode *adjusted_mode;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001622 uint32_t fwater_lo;
1623 int planea_wm;
1624
1625 crtc = single_enabled_crtc(dev);
1626 if (crtc == NULL)
1627 return;
1628
Damien Lespiau241bfc32013-09-25 16:45:37 +01001629 adjusted_mode = &to_intel_crtc(crtc)->config.adjusted_mode;
1630 planea_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Daniel Vetterfeb56b92013-12-14 20:38:30 -02001631 &i845_wm_info,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001632 dev_priv->display.get_fifo_size(dev, 0),
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001633 4, latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001634 fwater_lo = I915_READ(FW_BLC) & ~0xfff;
1635 fwater_lo |= (3<<8) | planea_wm;
1636
1637 DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d\n", planea_wm);
1638
1639 I915_WRITE(FW_BLC, fwater_lo);
1640}
1641
Ville Syrjälä36587292013-07-05 11:57:16 +03001642static uint32_t ilk_pipe_pixel_rate(struct drm_device *dev,
1643 struct drm_crtc *crtc)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001644{
1645 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilsonfd4daa92013-08-27 17:04:17 +01001646 uint32_t pixel_rate;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001647
Damien Lespiau241bfc32013-09-25 16:45:37 +01001648 pixel_rate = intel_crtc->config.adjusted_mode.crtc_clock;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001649
1650 /* We only use IF-ID interlacing. If we ever use PF-ID we'll need to
1651 * adjust the pixel_rate here. */
1652
Chris Wilsonfd4daa92013-08-27 17:04:17 +01001653 if (intel_crtc->config.pch_pfit.enabled) {
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001654 uint64_t pipe_w, pipe_h, pfit_w, pfit_h;
Chris Wilsonfd4daa92013-08-27 17:04:17 +01001655 uint32_t pfit_size = intel_crtc->config.pch_pfit.size;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001656
Ville Syrjälä37327ab2013-09-04 18:25:28 +03001657 pipe_w = intel_crtc->config.pipe_src_w;
1658 pipe_h = intel_crtc->config.pipe_src_h;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001659 pfit_w = (pfit_size >> 16) & 0xFFFF;
1660 pfit_h = pfit_size & 0xFFFF;
1661 if (pipe_w < pfit_w)
1662 pipe_w = pfit_w;
1663 if (pipe_h < pfit_h)
1664 pipe_h = pfit_h;
1665
1666 pixel_rate = div_u64((uint64_t) pixel_rate * pipe_w * pipe_h,
1667 pfit_w * pfit_h);
1668 }
1669
1670 return pixel_rate;
1671}
1672
Ville Syrjälä37126462013-08-01 16:18:55 +03001673/* latency must be in 0.1us units. */
Ville Syrjälä23297042013-07-05 11:57:17 +03001674static uint32_t ilk_wm_method1(uint32_t pixel_rate, uint8_t bytes_per_pixel,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001675 uint32_t latency)
1676{
1677 uint64_t ret;
1678
Ville Syrjälä3312ba62013-08-01 16:18:53 +03001679 if (WARN(latency == 0, "Latency value missing\n"))
1680 return UINT_MAX;
1681
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001682 ret = (uint64_t) pixel_rate * bytes_per_pixel * latency;
1683 ret = DIV_ROUND_UP_ULL(ret, 64 * 10000) + 2;
1684
1685 return ret;
1686}
1687
Ville Syrjälä37126462013-08-01 16:18:55 +03001688/* latency must be in 0.1us units. */
Ville Syrjälä23297042013-07-05 11:57:17 +03001689static uint32_t ilk_wm_method2(uint32_t pixel_rate, uint32_t pipe_htotal,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001690 uint32_t horiz_pixels, uint8_t bytes_per_pixel,
1691 uint32_t latency)
1692{
1693 uint32_t ret;
1694
Ville Syrjälä3312ba62013-08-01 16:18:53 +03001695 if (WARN(latency == 0, "Latency value missing\n"))
1696 return UINT_MAX;
1697
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001698 ret = (latency * pixel_rate) / (pipe_htotal * 10000);
1699 ret = (ret + 1) * horiz_pixels * bytes_per_pixel;
1700 ret = DIV_ROUND_UP(ret, 64) + 2;
1701 return ret;
1702}
1703
Ville Syrjälä23297042013-07-05 11:57:17 +03001704static uint32_t ilk_wm_fbc(uint32_t pri_val, uint32_t horiz_pixels,
Paulo Zanonicca32e92013-05-31 11:45:06 -03001705 uint8_t bytes_per_pixel)
1706{
1707 return DIV_ROUND_UP(pri_val * 64, horiz_pixels * bytes_per_pixel) + 2;
1708}
1709
Imre Deak820c1982013-12-17 14:46:36 +02001710struct ilk_pipe_wm_parameters {
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001711 bool active;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001712 uint32_t pipe_htotal;
1713 uint32_t pixel_rate;
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001714 struct intel_plane_wm_parameters pri;
1715 struct intel_plane_wm_parameters spr;
1716 struct intel_plane_wm_parameters cur;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001717};
1718
Imre Deak820c1982013-12-17 14:46:36 +02001719struct ilk_wm_maximums {
Paulo Zanonicca32e92013-05-31 11:45:06 -03001720 uint16_t pri;
1721 uint16_t spr;
1722 uint16_t cur;
1723 uint16_t fbc;
1724};
1725
Ville Syrjälä240264f2013-08-07 13:29:12 +03001726/* used in computing the new watermarks state */
1727struct intel_wm_config {
1728 unsigned int num_pipes_active;
1729 bool sprites_enabled;
1730 bool sprites_scaled;
Ville Syrjälä240264f2013-08-07 13:29:12 +03001731};
1732
Ville Syrjälä37126462013-08-01 16:18:55 +03001733/*
1734 * For both WM_PIPE and WM_LP.
1735 * mem_value must be in 0.1us units.
1736 */
Imre Deak820c1982013-12-17 14:46:36 +02001737static uint32_t ilk_compute_pri_wm(const struct ilk_pipe_wm_parameters *params,
Paulo Zanonicca32e92013-05-31 11:45:06 -03001738 uint32_t mem_value,
1739 bool is_lp)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001740{
Paulo Zanonicca32e92013-05-31 11:45:06 -03001741 uint32_t method1, method2;
1742
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001743 if (!params->active || !params->pri.enabled)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001744 return 0;
1745
Ville Syrjälä23297042013-07-05 11:57:17 +03001746 method1 = ilk_wm_method1(params->pixel_rate,
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001747 params->pri.bytes_per_pixel,
Paulo Zanonicca32e92013-05-31 11:45:06 -03001748 mem_value);
1749
1750 if (!is_lp)
1751 return method1;
1752
Ville Syrjälä23297042013-07-05 11:57:17 +03001753 method2 = ilk_wm_method2(params->pixel_rate,
Paulo Zanonicca32e92013-05-31 11:45:06 -03001754 params->pipe_htotal,
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001755 params->pri.horiz_pixels,
1756 params->pri.bytes_per_pixel,
Paulo Zanonicca32e92013-05-31 11:45:06 -03001757 mem_value);
1758
1759 return min(method1, method2);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001760}
1761
Ville Syrjälä37126462013-08-01 16:18:55 +03001762/*
1763 * For both WM_PIPE and WM_LP.
1764 * mem_value must be in 0.1us units.
1765 */
Imre Deak820c1982013-12-17 14:46:36 +02001766static uint32_t ilk_compute_spr_wm(const struct ilk_pipe_wm_parameters *params,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001767 uint32_t mem_value)
1768{
1769 uint32_t method1, method2;
1770
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001771 if (!params->active || !params->spr.enabled)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001772 return 0;
1773
Ville Syrjälä23297042013-07-05 11:57:17 +03001774 method1 = ilk_wm_method1(params->pixel_rate,
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001775 params->spr.bytes_per_pixel,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001776 mem_value);
Ville Syrjälä23297042013-07-05 11:57:17 +03001777 method2 = ilk_wm_method2(params->pixel_rate,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001778 params->pipe_htotal,
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001779 params->spr.horiz_pixels,
1780 params->spr.bytes_per_pixel,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001781 mem_value);
1782 return min(method1, method2);
1783}
1784
Ville Syrjälä37126462013-08-01 16:18:55 +03001785/*
1786 * For both WM_PIPE and WM_LP.
1787 * mem_value must be in 0.1us units.
1788 */
Imre Deak820c1982013-12-17 14:46:36 +02001789static uint32_t ilk_compute_cur_wm(const struct ilk_pipe_wm_parameters *params,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001790 uint32_t mem_value)
1791{
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001792 if (!params->active || !params->cur.enabled)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001793 return 0;
1794
Ville Syrjälä23297042013-07-05 11:57:17 +03001795 return ilk_wm_method2(params->pixel_rate,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001796 params->pipe_htotal,
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001797 params->cur.horiz_pixels,
1798 params->cur.bytes_per_pixel,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001799 mem_value);
1800}
1801
Paulo Zanonicca32e92013-05-31 11:45:06 -03001802/* Only for WM_LP. */
Imre Deak820c1982013-12-17 14:46:36 +02001803static uint32_t ilk_compute_fbc_wm(const struct ilk_pipe_wm_parameters *params,
Ville Syrjälä1fda9882013-07-05 11:57:19 +03001804 uint32_t pri_val)
Paulo Zanonicca32e92013-05-31 11:45:06 -03001805{
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001806 if (!params->active || !params->pri.enabled)
Paulo Zanonicca32e92013-05-31 11:45:06 -03001807 return 0;
1808
Ville Syrjälä23297042013-07-05 11:57:17 +03001809 return ilk_wm_fbc(pri_val,
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001810 params->pri.horiz_pixels,
1811 params->pri.bytes_per_pixel);
Paulo Zanonicca32e92013-05-31 11:45:06 -03001812}
1813
Ville Syrjälä158ae642013-08-07 13:28:19 +03001814static unsigned int ilk_display_fifo_size(const struct drm_device *dev)
1815{
Ville Syrjälä416f4722013-11-02 21:07:46 -07001816 if (INTEL_INFO(dev)->gen >= 8)
1817 return 3072;
1818 else if (INTEL_INFO(dev)->gen >= 7)
Ville Syrjälä158ae642013-08-07 13:28:19 +03001819 return 768;
1820 else
1821 return 512;
1822}
1823
1824/* Calculate the maximum primary/sprite plane watermark */
1825static unsigned int ilk_plane_wm_max(const struct drm_device *dev,
1826 int level,
Ville Syrjälä240264f2013-08-07 13:29:12 +03001827 const struct intel_wm_config *config,
Ville Syrjälä158ae642013-08-07 13:28:19 +03001828 enum intel_ddb_partitioning ddb_partitioning,
1829 bool is_sprite)
1830{
1831 unsigned int fifo_size = ilk_display_fifo_size(dev);
1832 unsigned int max;
1833
1834 /* if sprites aren't enabled, sprites get nothing */
Ville Syrjälä240264f2013-08-07 13:29:12 +03001835 if (is_sprite && !config->sprites_enabled)
Ville Syrjälä158ae642013-08-07 13:28:19 +03001836 return 0;
1837
1838 /* HSW allows LP1+ watermarks even with multiple pipes */
Ville Syrjälä240264f2013-08-07 13:29:12 +03001839 if (level == 0 || config->num_pipes_active > 1) {
Ville Syrjälä158ae642013-08-07 13:28:19 +03001840 fifo_size /= INTEL_INFO(dev)->num_pipes;
1841
1842 /*
1843 * For some reason the non self refresh
1844 * FIFO size is only half of the self
1845 * refresh FIFO size on ILK/SNB.
1846 */
1847 if (INTEL_INFO(dev)->gen <= 6)
1848 fifo_size /= 2;
1849 }
1850
Ville Syrjälä240264f2013-08-07 13:29:12 +03001851 if (config->sprites_enabled) {
Ville Syrjälä158ae642013-08-07 13:28:19 +03001852 /* level 0 is always calculated with 1:1 split */
1853 if (level > 0 && ddb_partitioning == INTEL_DDB_PART_5_6) {
1854 if (is_sprite)
1855 fifo_size *= 5;
1856 fifo_size /= 6;
1857 } else {
1858 fifo_size /= 2;
1859 }
1860 }
1861
1862 /* clamp to max that the registers can hold */
Ville Syrjälä416f4722013-11-02 21:07:46 -07001863 if (INTEL_INFO(dev)->gen >= 8)
1864 max = level == 0 ? 255 : 2047;
1865 else if (INTEL_INFO(dev)->gen >= 7)
Ville Syrjälä158ae642013-08-07 13:28:19 +03001866 /* IVB/HSW primary/sprite plane watermarks */
1867 max = level == 0 ? 127 : 1023;
1868 else if (!is_sprite)
1869 /* ILK/SNB primary plane watermarks */
1870 max = level == 0 ? 127 : 511;
1871 else
1872 /* ILK/SNB sprite plane watermarks */
1873 max = level == 0 ? 63 : 255;
1874
1875 return min(fifo_size, max);
1876}
1877
1878/* Calculate the maximum cursor plane watermark */
1879static unsigned int ilk_cursor_wm_max(const struct drm_device *dev,
Ville Syrjälä240264f2013-08-07 13:29:12 +03001880 int level,
1881 const struct intel_wm_config *config)
Ville Syrjälä158ae642013-08-07 13:28:19 +03001882{
1883 /* HSW LP1+ watermarks w/ multiple pipes */
Ville Syrjälä240264f2013-08-07 13:29:12 +03001884 if (level > 0 && config->num_pipes_active > 1)
Ville Syrjälä158ae642013-08-07 13:28:19 +03001885 return 64;
1886
1887 /* otherwise just report max that registers can hold */
1888 if (INTEL_INFO(dev)->gen >= 7)
1889 return level == 0 ? 63 : 255;
1890 else
1891 return level == 0 ? 31 : 63;
1892}
1893
1894/* Calculate the maximum FBC watermark */
Damien Lespiaud34ff9c2014-01-06 19:17:23 +00001895static unsigned int ilk_fbc_wm_max(const struct drm_device *dev)
Ville Syrjälä158ae642013-08-07 13:28:19 +03001896{
1897 /* max that registers can hold */
Ville Syrjälä416f4722013-11-02 21:07:46 -07001898 if (INTEL_INFO(dev)->gen >= 8)
1899 return 31;
1900 else
1901 return 15;
Ville Syrjälä158ae642013-08-07 13:28:19 +03001902}
1903
Damien Lespiaud34ff9c2014-01-06 19:17:23 +00001904static void ilk_compute_wm_maximums(const struct drm_device *dev,
Ville Syrjälä34982fe2013-10-09 19:18:09 +03001905 int level,
1906 const struct intel_wm_config *config,
1907 enum intel_ddb_partitioning ddb_partitioning,
Imre Deak820c1982013-12-17 14:46:36 +02001908 struct ilk_wm_maximums *max)
Ville Syrjälä158ae642013-08-07 13:28:19 +03001909{
Ville Syrjälä240264f2013-08-07 13:29:12 +03001910 max->pri = ilk_plane_wm_max(dev, level, config, ddb_partitioning, false);
1911 max->spr = ilk_plane_wm_max(dev, level, config, ddb_partitioning, true);
1912 max->cur = ilk_cursor_wm_max(dev, level, config);
Ville Syrjälä416f4722013-11-02 21:07:46 -07001913 max->fbc = ilk_fbc_wm_max(dev);
Ville Syrjälä158ae642013-08-07 13:28:19 +03001914}
1915
Ville Syrjäläd9395652013-10-09 19:18:10 +03001916static bool ilk_validate_wm_level(int level,
Imre Deak820c1982013-12-17 14:46:36 +02001917 const struct ilk_wm_maximums *max,
Ville Syrjäläd9395652013-10-09 19:18:10 +03001918 struct intel_wm_level *result)
Ville Syrjäläa9786a12013-08-07 13:24:47 +03001919{
1920 bool ret;
1921
1922 /* already determined to be invalid? */
1923 if (!result->enable)
1924 return false;
1925
1926 result->enable = result->pri_val <= max->pri &&
1927 result->spr_val <= max->spr &&
1928 result->cur_val <= max->cur;
1929
1930 ret = result->enable;
1931
1932 /*
1933 * HACK until we can pre-compute everything,
1934 * and thus fail gracefully if LP0 watermarks
1935 * are exceeded...
1936 */
1937 if (level == 0 && !result->enable) {
1938 if (result->pri_val > max->pri)
1939 DRM_DEBUG_KMS("Primary WM%d too large %u (max %u)\n",
1940 level, result->pri_val, max->pri);
1941 if (result->spr_val > max->spr)
1942 DRM_DEBUG_KMS("Sprite WM%d too large %u (max %u)\n",
1943 level, result->spr_val, max->spr);
1944 if (result->cur_val > max->cur)
1945 DRM_DEBUG_KMS("Cursor WM%d too large %u (max %u)\n",
1946 level, result->cur_val, max->cur);
1947
1948 result->pri_val = min_t(uint32_t, result->pri_val, max->pri);
1949 result->spr_val = min_t(uint32_t, result->spr_val, max->spr);
1950 result->cur_val = min_t(uint32_t, result->cur_val, max->cur);
1951 result->enable = true;
1952 }
1953
Ville Syrjäläa9786a12013-08-07 13:24:47 +03001954 return ret;
1955}
1956
Damien Lespiaud34ff9c2014-01-06 19:17:23 +00001957static void ilk_compute_wm_level(const struct drm_i915_private *dev_priv,
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03001958 int level,
Imre Deak820c1982013-12-17 14:46:36 +02001959 const struct ilk_pipe_wm_parameters *p,
Ville Syrjälä1fd527c2013-08-06 22:24:05 +03001960 struct intel_wm_level *result)
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03001961{
1962 uint16_t pri_latency = dev_priv->wm.pri_latency[level];
1963 uint16_t spr_latency = dev_priv->wm.spr_latency[level];
1964 uint16_t cur_latency = dev_priv->wm.cur_latency[level];
1965
1966 /* WM1+ latency values stored in 0.5us units */
1967 if (level > 0) {
1968 pri_latency *= 5;
1969 spr_latency *= 5;
1970 cur_latency *= 5;
1971 }
1972
1973 result->pri_val = ilk_compute_pri_wm(p, pri_latency, level);
1974 result->spr_val = ilk_compute_spr_wm(p, spr_latency);
1975 result->cur_val = ilk_compute_cur_wm(p, cur_latency);
1976 result->fbc_val = ilk_compute_fbc_wm(p, result->pri_val);
1977 result->enable = true;
1978}
1979
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001980static uint32_t
1981hsw_compute_linetime_wm(struct drm_device *dev, struct drm_crtc *crtc)
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03001982{
1983 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03001984 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03001985 struct drm_display_mode *mode = &intel_crtc->config.adjusted_mode;
Paulo Zanoni85a02de2013-05-03 17:23:43 -03001986 u32 linetime, ips_linetime;
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03001987
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001988 if (!intel_crtc_active(crtc))
1989 return 0;
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03001990
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03001991 /* The WM are computed with base on how long it takes to fill a single
1992 * row at the given clock rate, multiplied by 8.
1993 * */
Jesse Barnesfec8cba2013-11-27 11:10:26 -08001994 linetime = DIV_ROUND_CLOSEST(mode->crtc_htotal * 1000 * 8,
1995 mode->crtc_clock);
1996 ips_linetime = DIV_ROUND_CLOSEST(mode->crtc_htotal * 1000 * 8,
Paulo Zanoni85a02de2013-05-03 17:23:43 -03001997 intel_ddi_get_cdclk_freq(dev_priv));
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03001998
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001999 return PIPE_WM_LINETIME_IPS_LINETIME(ips_linetime) |
2000 PIPE_WM_LINETIME_TIME(linetime);
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002001}
2002
Ville Syrjälä12b134d2013-07-05 11:57:21 +03002003static void intel_read_wm_latency(struct drm_device *dev, uint16_t wm[5])
2004{
2005 struct drm_i915_private *dev_priv = dev->dev_private;
2006
Ville Syrjäläa42a5712014-01-07 16:14:08 +02002007 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Ville Syrjälä12b134d2013-07-05 11:57:21 +03002008 uint64_t sskpd = I915_READ64(MCH_SSKPD);
2009
2010 wm[0] = (sskpd >> 56) & 0xFF;
2011 if (wm[0] == 0)
2012 wm[0] = sskpd & 0xF;
Ville Syrjäläe5d50192013-07-05 11:57:22 +03002013 wm[1] = (sskpd >> 4) & 0xFF;
2014 wm[2] = (sskpd >> 12) & 0xFF;
2015 wm[3] = (sskpd >> 20) & 0x1FF;
2016 wm[4] = (sskpd >> 32) & 0x1FF;
Ville Syrjälä63cf9a12013-07-05 11:57:23 +03002017 } else if (INTEL_INFO(dev)->gen >= 6) {
2018 uint32_t sskpd = I915_READ(MCH_SSKPD);
2019
2020 wm[0] = (sskpd >> SSKPD_WM0_SHIFT) & SSKPD_WM_MASK;
2021 wm[1] = (sskpd >> SSKPD_WM1_SHIFT) & SSKPD_WM_MASK;
2022 wm[2] = (sskpd >> SSKPD_WM2_SHIFT) & SSKPD_WM_MASK;
2023 wm[3] = (sskpd >> SSKPD_WM3_SHIFT) & SSKPD_WM_MASK;
Ville Syrjälä3a88d0a2013-08-01 16:18:49 +03002024 } else if (INTEL_INFO(dev)->gen >= 5) {
2025 uint32_t mltr = I915_READ(MLTR_ILK);
2026
2027 /* ILK primary LP0 latency is 700 ns */
2028 wm[0] = 7;
2029 wm[1] = (mltr >> MLTR_WM1_SHIFT) & ILK_SRLT_MASK;
2030 wm[2] = (mltr >> MLTR_WM2_SHIFT) & ILK_SRLT_MASK;
Ville Syrjälä12b134d2013-07-05 11:57:21 +03002031 }
2032}
2033
Ville Syrjälä53615a52013-08-01 16:18:50 +03002034static void intel_fixup_spr_wm_latency(struct drm_device *dev, uint16_t wm[5])
2035{
2036 /* ILK sprite LP0 latency is 1300 ns */
2037 if (INTEL_INFO(dev)->gen == 5)
2038 wm[0] = 13;
2039}
2040
2041static void intel_fixup_cur_wm_latency(struct drm_device *dev, uint16_t wm[5])
2042{
2043 /* ILK cursor LP0 latency is 1300 ns */
2044 if (INTEL_INFO(dev)->gen == 5)
2045 wm[0] = 13;
2046
2047 /* WaDoubleCursorLP3Latency:ivb */
2048 if (IS_IVYBRIDGE(dev))
2049 wm[3] *= 2;
2050}
2051
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03002052static int ilk_wm_max_level(const struct drm_device *dev)
2053{
2054 /* how many WM levels are we expecting */
Ville Syrjäläa42a5712014-01-07 16:14:08 +02002055 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03002056 return 4;
2057 else if (INTEL_INFO(dev)->gen >= 6)
2058 return 3;
2059 else
2060 return 2;
2061}
2062
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002063static void intel_print_wm_latency(struct drm_device *dev,
2064 const char *name,
2065 const uint16_t wm[5])
2066{
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03002067 int level, max_level = ilk_wm_max_level(dev);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002068
2069 for (level = 0; level <= max_level; level++) {
2070 unsigned int latency = wm[level];
2071
2072 if (latency == 0) {
2073 DRM_ERROR("%s WM%d latency not provided\n",
2074 name, level);
2075 continue;
2076 }
2077
2078 /* WM1+ latency values in 0.5us units */
2079 if (level > 0)
2080 latency *= 5;
2081
2082 DRM_DEBUG_KMS("%s WM%d latency %u (%u.%u usec)\n",
2083 name, level, wm[level],
2084 latency / 10, latency % 10);
2085 }
2086}
2087
Damien Lespiaufa50ad62014-03-17 18:01:16 +00002088static void ilk_setup_wm_latency(struct drm_device *dev)
Ville Syrjälä53615a52013-08-01 16:18:50 +03002089{
2090 struct drm_i915_private *dev_priv = dev->dev_private;
2091
2092 intel_read_wm_latency(dev, dev_priv->wm.pri_latency);
2093
2094 memcpy(dev_priv->wm.spr_latency, dev_priv->wm.pri_latency,
2095 sizeof(dev_priv->wm.pri_latency));
2096 memcpy(dev_priv->wm.cur_latency, dev_priv->wm.pri_latency,
2097 sizeof(dev_priv->wm.pri_latency));
2098
2099 intel_fixup_spr_wm_latency(dev, dev_priv->wm.spr_latency);
2100 intel_fixup_cur_wm_latency(dev, dev_priv->wm.cur_latency);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002101
2102 intel_print_wm_latency(dev, "Primary", dev_priv->wm.pri_latency);
2103 intel_print_wm_latency(dev, "Sprite", dev_priv->wm.spr_latency);
2104 intel_print_wm_latency(dev, "Cursor", dev_priv->wm.cur_latency);
Ville Syrjälä53615a52013-08-01 16:18:50 +03002105}
2106
Imre Deak820c1982013-12-17 14:46:36 +02002107static void ilk_compute_wm_parameters(struct drm_crtc *crtc,
2108 struct ilk_pipe_wm_parameters *p,
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03002109 struct intel_wm_config *config)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002110{
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03002111 struct drm_device *dev = crtc->dev;
2112 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2113 enum pipe pipe = intel_crtc->pipe;
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03002114 struct drm_plane *plane;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002115
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03002116 p->active = intel_crtc_active(crtc);
2117 if (p->active) {
Jesse Barnes576b2592013-12-20 13:08:00 -08002118 p->pipe_htotal = intel_crtc->config.adjusted_mode.crtc_htotal;
Ville Syrjälä36587292013-07-05 11:57:16 +03002119 p->pixel_rate = ilk_pipe_pixel_rate(dev, crtc);
Matt Roperf4510a22014-04-01 15:22:40 -07002120 p->pri.bytes_per_pixel = crtc->primary->fb->bits_per_pixel / 8;
Ville Syrjäläc35426d2013-08-07 13:29:50 +03002121 p->cur.bytes_per_pixel = 4;
Ville Syrjälä37327ab2013-09-04 18:25:28 +03002122 p->pri.horiz_pixels = intel_crtc->config.pipe_src_w;
Chris Wilson7bb836d2014-03-26 12:38:14 +00002123 p->cur.horiz_pixels = intel_crtc->cursor_width;
Ville Syrjäläc35426d2013-08-07 13:29:50 +03002124 /* TODO: for now, assume primary and cursor planes are always enabled. */
2125 p->pri.enabled = true;
2126 p->cur.enabled = true;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002127 }
2128
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03002129 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03002130 config->num_pipes_active += intel_crtc_active(crtc);
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03002131
Matt Roperaf2b6532014-04-01 15:22:32 -07002132 drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002133 struct intel_plane *intel_plane = to_intel_plane(plane);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002134
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03002135 if (intel_plane->pipe == pipe)
2136 p->spr = intel_plane->wm;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002137
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03002138 config->sprites_enabled |= intel_plane->wm.enabled;
2139 config->sprites_scaled |= intel_plane->wm.scaled;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002140 }
2141}
2142
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002143/* Compute new watermarks for the pipe */
2144static bool intel_compute_pipe_wm(struct drm_crtc *crtc,
Imre Deak820c1982013-12-17 14:46:36 +02002145 const struct ilk_pipe_wm_parameters *params,
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002146 struct intel_pipe_wm *pipe_wm)
2147{
2148 struct drm_device *dev = crtc->dev;
Damien Lespiaud34ff9c2014-01-06 19:17:23 +00002149 const struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002150 int level, max_level = ilk_wm_max_level(dev);
2151 /* LP0 watermark maximums depend on this pipe alone */
2152 struct intel_wm_config config = {
2153 .num_pipes_active = 1,
2154 .sprites_enabled = params->spr.enabled,
2155 .sprites_scaled = params->spr.scaled,
2156 };
Imre Deak820c1982013-12-17 14:46:36 +02002157 struct ilk_wm_maximums max;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002158
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002159 /* LP0 watermarks always use 1/2 DDB partitioning */
Ville Syrjälä34982fe2013-10-09 19:18:09 +03002160 ilk_compute_wm_maximums(dev, 0, &config, INTEL_DDB_PART_1_2, &max);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002161
Ville Syrjälä7b39a0b2013-12-05 15:51:30 +02002162 /* ILK/SNB: LP2+ watermarks only w/o sprites */
2163 if (INTEL_INFO(dev)->gen <= 6 && params->spr.enabled)
2164 max_level = 1;
2165
2166 /* ILK/SNB/IVB: LP1+ watermarks only w/o scaling */
2167 if (params->spr.scaled)
2168 max_level = 0;
2169
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002170 for (level = 0; level <= max_level; level++)
2171 ilk_compute_wm_level(dev_priv, level, params,
2172 &pipe_wm->wm[level]);
2173
Ville Syrjäläa42a5712014-01-07 16:14:08 +02002174 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Ville Syrjäläce0e0712013-12-05 15:51:36 +02002175 pipe_wm->linetime = hsw_compute_linetime_wm(dev, crtc);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002176
2177 /* At least LP0 must be valid */
Ville Syrjäläd9395652013-10-09 19:18:10 +03002178 return ilk_validate_wm_level(0, &max, &pipe_wm->wm[0]);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002179}
2180
2181/*
2182 * Merge the watermarks from all active pipes for a specific level.
2183 */
2184static void ilk_merge_wm_level(struct drm_device *dev,
2185 int level,
2186 struct intel_wm_level *ret_wm)
2187{
2188 const struct intel_crtc *intel_crtc;
2189
2190 list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list, base.head) {
2191 const struct intel_wm_level *wm =
2192 &intel_crtc->wm.active.wm[level];
2193
2194 if (!wm->enable)
2195 return;
2196
2197 ret_wm->pri_val = max(ret_wm->pri_val, wm->pri_val);
2198 ret_wm->spr_val = max(ret_wm->spr_val, wm->spr_val);
2199 ret_wm->cur_val = max(ret_wm->cur_val, wm->cur_val);
2200 ret_wm->fbc_val = max(ret_wm->fbc_val, wm->fbc_val);
2201 }
2202
2203 ret_wm->enable = true;
2204}
2205
2206/*
2207 * Merge all low power watermarks for all active pipes.
2208 */
2209static void ilk_wm_merge(struct drm_device *dev,
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02002210 const struct intel_wm_config *config,
Imre Deak820c1982013-12-17 14:46:36 +02002211 const struct ilk_wm_maximums *max,
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002212 struct intel_pipe_wm *merged)
2213{
2214 int level, max_level = ilk_wm_max_level(dev);
2215
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02002216 /* ILK/SNB/IVB: LP1+ watermarks only w/ single pipe */
2217 if ((INTEL_INFO(dev)->gen <= 6 || IS_IVYBRIDGE(dev)) &&
2218 config->num_pipes_active > 1)
2219 return;
2220
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02002221 /* ILK: FBC WM must be disabled always */
2222 merged->fbc_wm_enabled = INTEL_INFO(dev)->gen >= 6;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002223
2224 /* merge each WM1+ level */
2225 for (level = 1; level <= max_level; level++) {
2226 struct intel_wm_level *wm = &merged->wm[level];
2227
2228 ilk_merge_wm_level(dev, level, wm);
2229
Ville Syrjäläd9395652013-10-09 19:18:10 +03002230 if (!ilk_validate_wm_level(level, max, wm))
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002231 break;
2232
2233 /*
2234 * The spec says it is preferred to disable
2235 * FBC WMs instead of disabling a WM level.
2236 */
2237 if (wm->fbc_val > max->fbc) {
2238 merged->fbc_wm_enabled = false;
2239 wm->fbc_val = 0;
2240 }
2241 }
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02002242
2243 /* ILK: LP2+ must be disabled when FBC WM is disabled but FBC enabled */
2244 /*
2245 * FIXME this is racy. FBC might get enabled later.
2246 * What we should check here is whether FBC can be
2247 * enabled sometime later.
2248 */
2249 if (IS_GEN5(dev) && !merged->fbc_wm_enabled && intel_fbc_enabled(dev)) {
2250 for (level = 2; level <= max_level; level++) {
2251 struct intel_wm_level *wm = &merged->wm[level];
2252
2253 wm->enable = false;
2254 }
2255 }
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002256}
2257
Ville Syrjäläb380ca32013-10-09 19:18:01 +03002258static int ilk_wm_lp_to_level(int wm_lp, const struct intel_pipe_wm *pipe_wm)
2259{
2260 /* LP1,LP2,LP3 levels are either 1,2,3 or 1,3,4 */
2261 return wm_lp + (wm_lp >= 2 && pipe_wm->wm[4].enable);
2262}
2263
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02002264/* The value we need to program into the WM_LPx latency field */
2265static unsigned int ilk_wm_lp_latency(struct drm_device *dev, int level)
2266{
2267 struct drm_i915_private *dev_priv = dev->dev_private;
2268
Ville Syrjäläa42a5712014-01-07 16:14:08 +02002269 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02002270 return 2 * level;
2271 else
2272 return dev_priv->wm.pri_latency[level];
2273}
2274
Imre Deak820c1982013-12-17 14:46:36 +02002275static void ilk_compute_wm_results(struct drm_device *dev,
Ville Syrjälä0362c782013-10-09 19:17:57 +03002276 const struct intel_pipe_wm *merged,
Ville Syrjälä609cede2013-10-09 19:18:03 +03002277 enum intel_ddb_partitioning partitioning,
Imre Deak820c1982013-12-17 14:46:36 +02002278 struct ilk_wm_values *results)
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002279{
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002280 struct intel_crtc *intel_crtc;
2281 int level, wm_lp;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002282
Ville Syrjälä0362c782013-10-09 19:17:57 +03002283 results->enable_fbc_wm = merged->fbc_wm_enabled;
Ville Syrjälä609cede2013-10-09 19:18:03 +03002284 results->partitioning = partitioning;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002285
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002286 /* LP1+ register values */
Paulo Zanonicca32e92013-05-31 11:45:06 -03002287 for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
Ville Syrjälä1fd527c2013-08-06 22:24:05 +03002288 const struct intel_wm_level *r;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002289
Ville Syrjäläb380ca32013-10-09 19:18:01 +03002290 level = ilk_wm_lp_to_level(wm_lp, merged);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002291
Ville Syrjälä0362c782013-10-09 19:17:57 +03002292 r = &merged->wm[level];
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002293 if (!r->enable)
Paulo Zanonicca32e92013-05-31 11:45:06 -03002294 break;
2295
Ville Syrjälä416f4722013-11-02 21:07:46 -07002296 results->wm_lp[wm_lp - 1] = WM3_LP_EN |
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02002297 (ilk_wm_lp_latency(dev, level) << WM1_LP_LATENCY_SHIFT) |
Ville Syrjälä416f4722013-11-02 21:07:46 -07002298 (r->pri_val << WM1_LP_SR_SHIFT) |
2299 r->cur_val;
2300
2301 if (INTEL_INFO(dev)->gen >= 8)
2302 results->wm_lp[wm_lp - 1] |=
2303 r->fbc_val << WM1_LP_FBC_SHIFT_BDW;
2304 else
2305 results->wm_lp[wm_lp - 1] |=
2306 r->fbc_val << WM1_LP_FBC_SHIFT;
2307
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02002308 if (INTEL_INFO(dev)->gen <= 6 && r->spr_val) {
2309 WARN_ON(wm_lp != 1);
2310 results->wm_lp_spr[wm_lp - 1] = WM1S_LP_EN | r->spr_val;
2311 } else
2312 results->wm_lp_spr[wm_lp - 1] = r->spr_val;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002313 }
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002314
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002315 /* LP0 register values */
2316 list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list, base.head) {
2317 enum pipe pipe = intel_crtc->pipe;
2318 const struct intel_wm_level *r =
2319 &intel_crtc->wm.active.wm[0];
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002320
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002321 if (WARN_ON(!r->enable))
2322 continue;
2323
2324 results->wm_linetime[pipe] = intel_crtc->wm.active.linetime;
2325
2326 results->wm_pipe[pipe] =
2327 (r->pri_val << WM0_PIPE_PLANE_SHIFT) |
2328 (r->spr_val << WM0_PIPE_SPRITE_SHIFT) |
2329 r->cur_val;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002330 }
2331}
2332
Paulo Zanoni861f3382013-05-31 10:19:21 -03002333/* Find the result with the highest level enabled. Check for enable_fbc_wm in
2334 * case both are at the same level. Prefer r1 in case they're the same. */
Imre Deak820c1982013-12-17 14:46:36 +02002335static struct intel_pipe_wm *ilk_find_best_result(struct drm_device *dev,
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002336 struct intel_pipe_wm *r1,
2337 struct intel_pipe_wm *r2)
Paulo Zanoni861f3382013-05-31 10:19:21 -03002338{
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002339 int level, max_level = ilk_wm_max_level(dev);
2340 int level1 = 0, level2 = 0;
Paulo Zanoni861f3382013-05-31 10:19:21 -03002341
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002342 for (level = 1; level <= max_level; level++) {
2343 if (r1->wm[level].enable)
2344 level1 = level;
2345 if (r2->wm[level].enable)
2346 level2 = level;
Paulo Zanoni861f3382013-05-31 10:19:21 -03002347 }
2348
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002349 if (level1 == level2) {
2350 if (r2->fbc_wm_enabled && !r1->fbc_wm_enabled)
Paulo Zanoni861f3382013-05-31 10:19:21 -03002351 return r2;
2352 else
2353 return r1;
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002354 } else if (level1 > level2) {
Paulo Zanoni861f3382013-05-31 10:19:21 -03002355 return r1;
2356 } else {
2357 return r2;
2358 }
2359}
2360
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002361/* dirty bits used to track which watermarks need changes */
2362#define WM_DIRTY_PIPE(pipe) (1 << (pipe))
2363#define WM_DIRTY_LINETIME(pipe) (1 << (8 + (pipe)))
2364#define WM_DIRTY_LP(wm_lp) (1 << (15 + (wm_lp)))
2365#define WM_DIRTY_LP_ALL (WM_DIRTY_LP(1) | WM_DIRTY_LP(2) | WM_DIRTY_LP(3))
2366#define WM_DIRTY_FBC (1 << 24)
2367#define WM_DIRTY_DDB (1 << 25)
2368
2369static unsigned int ilk_compute_wm_dirty(struct drm_device *dev,
Imre Deak820c1982013-12-17 14:46:36 +02002370 const struct ilk_wm_values *old,
2371 const struct ilk_wm_values *new)
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002372{
2373 unsigned int dirty = 0;
2374 enum pipe pipe;
2375 int wm_lp;
2376
2377 for_each_pipe(pipe) {
2378 if (old->wm_linetime[pipe] != new->wm_linetime[pipe]) {
2379 dirty |= WM_DIRTY_LINETIME(pipe);
2380 /* Must disable LP1+ watermarks too */
2381 dirty |= WM_DIRTY_LP_ALL;
2382 }
2383
2384 if (old->wm_pipe[pipe] != new->wm_pipe[pipe]) {
2385 dirty |= WM_DIRTY_PIPE(pipe);
2386 /* Must disable LP1+ watermarks too */
2387 dirty |= WM_DIRTY_LP_ALL;
2388 }
2389 }
2390
2391 if (old->enable_fbc_wm != new->enable_fbc_wm) {
2392 dirty |= WM_DIRTY_FBC;
2393 /* Must disable LP1+ watermarks too */
2394 dirty |= WM_DIRTY_LP_ALL;
2395 }
2396
2397 if (old->partitioning != new->partitioning) {
2398 dirty |= WM_DIRTY_DDB;
2399 /* Must disable LP1+ watermarks too */
2400 dirty |= WM_DIRTY_LP_ALL;
2401 }
2402
2403 /* LP1+ watermarks already deemed dirty, no need to continue */
2404 if (dirty & WM_DIRTY_LP_ALL)
2405 return dirty;
2406
2407 /* Find the lowest numbered LP1+ watermark in need of an update... */
2408 for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
2409 if (old->wm_lp[wm_lp - 1] != new->wm_lp[wm_lp - 1] ||
2410 old->wm_lp_spr[wm_lp - 1] != new->wm_lp_spr[wm_lp - 1])
2411 break;
2412 }
2413
2414 /* ...and mark it and all higher numbered LP1+ watermarks as dirty */
2415 for (; wm_lp <= 3; wm_lp++)
2416 dirty |= WM_DIRTY_LP(wm_lp);
2417
2418 return dirty;
2419}
2420
Ville Syrjälä8553c182013-12-05 15:51:39 +02002421static bool _ilk_disable_lp_wm(struct drm_i915_private *dev_priv,
2422 unsigned int dirty)
2423{
Imre Deak820c1982013-12-17 14:46:36 +02002424 struct ilk_wm_values *previous = &dev_priv->wm.hw;
Ville Syrjälä8553c182013-12-05 15:51:39 +02002425 bool changed = false;
2426
2427 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] & WM1_LP_SR_EN) {
2428 previous->wm_lp[2] &= ~WM1_LP_SR_EN;
2429 I915_WRITE(WM3_LP_ILK, previous->wm_lp[2]);
2430 changed = true;
2431 }
2432 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] & WM1_LP_SR_EN) {
2433 previous->wm_lp[1] &= ~WM1_LP_SR_EN;
2434 I915_WRITE(WM2_LP_ILK, previous->wm_lp[1]);
2435 changed = true;
2436 }
2437 if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] & WM1_LP_SR_EN) {
2438 previous->wm_lp[0] &= ~WM1_LP_SR_EN;
2439 I915_WRITE(WM1_LP_ILK, previous->wm_lp[0]);
2440 changed = true;
2441 }
2442
2443 /*
2444 * Don't touch WM1S_LP_EN here.
2445 * Doing so could cause underruns.
2446 */
2447
2448 return changed;
2449}
2450
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002451/*
2452 * The spec says we shouldn't write when we don't need, because every write
2453 * causes WMs to be re-evaluated, expending some power.
2454 */
Imre Deak820c1982013-12-17 14:46:36 +02002455static void ilk_write_wm_values(struct drm_i915_private *dev_priv,
2456 struct ilk_wm_values *results)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002457{
Ville Syrjäläac9545f2013-12-05 15:51:28 +02002458 struct drm_device *dev = dev_priv->dev;
Imre Deak820c1982013-12-17 14:46:36 +02002459 struct ilk_wm_values *previous = &dev_priv->wm.hw;
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002460 unsigned int dirty;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002461 uint32_t val;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002462
Ville Syrjälä8553c182013-12-05 15:51:39 +02002463 dirty = ilk_compute_wm_dirty(dev, previous, results);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002464 if (!dirty)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002465 return;
2466
Ville Syrjälä8553c182013-12-05 15:51:39 +02002467 _ilk_disable_lp_wm(dev_priv, dirty);
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02002468
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002469 if (dirty & WM_DIRTY_PIPE(PIPE_A))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002470 I915_WRITE(WM0_PIPEA_ILK, results->wm_pipe[0]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002471 if (dirty & WM_DIRTY_PIPE(PIPE_B))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002472 I915_WRITE(WM0_PIPEB_ILK, results->wm_pipe[1]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002473 if (dirty & WM_DIRTY_PIPE(PIPE_C))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002474 I915_WRITE(WM0_PIPEC_IVB, results->wm_pipe[2]);
2475
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002476 if (dirty & WM_DIRTY_LINETIME(PIPE_A))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002477 I915_WRITE(PIPE_WM_LINETIME(PIPE_A), results->wm_linetime[0]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002478 if (dirty & WM_DIRTY_LINETIME(PIPE_B))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002479 I915_WRITE(PIPE_WM_LINETIME(PIPE_B), results->wm_linetime[1]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002480 if (dirty & WM_DIRTY_LINETIME(PIPE_C))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002481 I915_WRITE(PIPE_WM_LINETIME(PIPE_C), results->wm_linetime[2]);
2482
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002483 if (dirty & WM_DIRTY_DDB) {
Ville Syrjäläa42a5712014-01-07 16:14:08 +02002484 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Ville Syrjäläac9545f2013-12-05 15:51:28 +02002485 val = I915_READ(WM_MISC);
2486 if (results->partitioning == INTEL_DDB_PART_1_2)
2487 val &= ~WM_MISC_DATA_PARTITION_5_6;
2488 else
2489 val |= WM_MISC_DATA_PARTITION_5_6;
2490 I915_WRITE(WM_MISC, val);
2491 } else {
2492 val = I915_READ(DISP_ARB_CTL2);
2493 if (results->partitioning == INTEL_DDB_PART_1_2)
2494 val &= ~DISP_DATA_PARTITION_5_6;
2495 else
2496 val |= DISP_DATA_PARTITION_5_6;
2497 I915_WRITE(DISP_ARB_CTL2, val);
2498 }
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002499 }
2500
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002501 if (dirty & WM_DIRTY_FBC) {
Paulo Zanonicca32e92013-05-31 11:45:06 -03002502 val = I915_READ(DISP_ARB_CTL);
2503 if (results->enable_fbc_wm)
2504 val &= ~DISP_FBC_WM_DIS;
2505 else
2506 val |= DISP_FBC_WM_DIS;
2507 I915_WRITE(DISP_ARB_CTL, val);
2508 }
2509
Imre Deak954911e2013-12-17 14:46:34 +02002510 if (dirty & WM_DIRTY_LP(1) &&
2511 previous->wm_lp_spr[0] != results->wm_lp_spr[0])
2512 I915_WRITE(WM1S_LP_ILK, results->wm_lp_spr[0]);
2513
2514 if (INTEL_INFO(dev)->gen >= 7) {
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02002515 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp_spr[1] != results->wm_lp_spr[1])
2516 I915_WRITE(WM2S_LP_IVB, results->wm_lp_spr[1]);
2517 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp_spr[2] != results->wm_lp_spr[2])
2518 I915_WRITE(WM3S_LP_IVB, results->wm_lp_spr[2]);
2519 }
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002520
Ville Syrjäläfacd619b2013-12-05 15:51:33 +02002521 if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] != results->wm_lp[0])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002522 I915_WRITE(WM1_LP_ILK, results->wm_lp[0]);
Ville Syrjäläfacd619b2013-12-05 15:51:33 +02002523 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] != results->wm_lp[1])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002524 I915_WRITE(WM2_LP_ILK, results->wm_lp[1]);
Ville Syrjäläfacd619b2013-12-05 15:51:33 +02002525 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] != results->wm_lp[2])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002526 I915_WRITE(WM3_LP_ILK, results->wm_lp[2]);
Ville Syrjälä609cede2013-10-09 19:18:03 +03002527
2528 dev_priv->wm.hw = *results;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002529}
2530
Ville Syrjälä8553c182013-12-05 15:51:39 +02002531static bool ilk_disable_lp_wm(struct drm_device *dev)
2532{
2533 struct drm_i915_private *dev_priv = dev->dev_private;
2534
2535 return _ilk_disable_lp_wm(dev_priv, WM_DIRTY_LP_ALL);
2536}
2537
Imre Deak820c1982013-12-17 14:46:36 +02002538static void ilk_update_wm(struct drm_crtc *crtc)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002539{
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03002540 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjälä46ba6142013-09-10 11:40:40 +03002541 struct drm_device *dev = crtc->dev;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002542 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak820c1982013-12-17 14:46:36 +02002543 struct ilk_wm_maximums max;
2544 struct ilk_pipe_wm_parameters params = {};
2545 struct ilk_wm_values results = {};
Ville Syrjälä77c122b2013-08-06 22:24:04 +03002546 enum intel_ddb_partitioning partitioning;
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03002547 struct intel_pipe_wm pipe_wm = {};
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002548 struct intel_pipe_wm lp_wm_1_2 = {}, lp_wm_5_6 = {}, *best_lp_wm;
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03002549 struct intel_wm_config config = {};
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002550
Imre Deak820c1982013-12-17 14:46:36 +02002551 ilk_compute_wm_parameters(crtc, &params, &config);
Paulo Zanoni861f3382013-05-31 10:19:21 -03002552
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03002553 intel_compute_pipe_wm(crtc, &params, &pipe_wm);
2554
2555 if (!memcmp(&intel_crtc->wm.active, &pipe_wm, sizeof(pipe_wm)))
2556 return;
2557
2558 intel_crtc->wm.active = pipe_wm;
2559
Ville Syrjälä34982fe2013-10-09 19:18:09 +03002560 ilk_compute_wm_maximums(dev, 1, &config, INTEL_DDB_PART_1_2, &max);
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02002561 ilk_wm_merge(dev, &config, &max, &lp_wm_1_2);
Ville Syrjälä0362c782013-10-09 19:17:57 +03002562
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03002563 /* 5/6 split only in single pipe config on IVB+ */
Ville Syrjäläec98c8d2013-10-11 15:26:26 +03002564 if (INTEL_INFO(dev)->gen >= 7 &&
2565 config.num_pipes_active == 1 && config.sprites_enabled) {
Ville Syrjälä34982fe2013-10-09 19:18:09 +03002566 ilk_compute_wm_maximums(dev, 1, &config, INTEL_DDB_PART_5_6, &max);
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02002567 ilk_wm_merge(dev, &config, &max, &lp_wm_5_6);
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03002568
Imre Deak820c1982013-12-17 14:46:36 +02002569 best_lp_wm = ilk_find_best_result(dev, &lp_wm_1_2, &lp_wm_5_6);
Paulo Zanoni861f3382013-05-31 10:19:21 -03002570 } else {
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002571 best_lp_wm = &lp_wm_1_2;
Paulo Zanoni861f3382013-05-31 10:19:21 -03002572 }
2573
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002574 partitioning = (best_lp_wm == &lp_wm_1_2) ?
Ville Syrjälä77c122b2013-08-06 22:24:04 +03002575 INTEL_DDB_PART_1_2 : INTEL_DDB_PART_5_6;
Paulo Zanoni861f3382013-05-31 10:19:21 -03002576
Imre Deak820c1982013-12-17 14:46:36 +02002577 ilk_compute_wm_results(dev, best_lp_wm, partitioning, &results);
Ville Syrjälä609cede2013-10-09 19:18:03 +03002578
Imre Deak820c1982013-12-17 14:46:36 +02002579 ilk_write_wm_values(dev_priv, &results);
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002580}
2581
Imre Deak820c1982013-12-17 14:46:36 +02002582static void ilk_update_sprite_wm(struct drm_plane *plane,
Ville Syrjäläadf3d352013-08-06 22:24:11 +03002583 struct drm_crtc *crtc,
Paulo Zanoni526682e2013-05-24 11:59:18 -03002584 uint32_t sprite_width, int pixel_size,
Ville Syrjäläbdd57d02013-07-05 11:57:13 +03002585 bool enabled, bool scaled)
Paulo Zanoni526682e2013-05-24 11:59:18 -03002586{
Ville Syrjälä8553c182013-12-05 15:51:39 +02002587 struct drm_device *dev = plane->dev;
Ville Syrjäläadf3d352013-08-06 22:24:11 +03002588 struct intel_plane *intel_plane = to_intel_plane(plane);
Paulo Zanoni526682e2013-05-24 11:59:18 -03002589
Ville Syrjäläadf3d352013-08-06 22:24:11 +03002590 intel_plane->wm.enabled = enabled;
2591 intel_plane->wm.scaled = scaled;
2592 intel_plane->wm.horiz_pixels = sprite_width;
2593 intel_plane->wm.bytes_per_pixel = pixel_size;
Paulo Zanoni526682e2013-05-24 11:59:18 -03002594
Ville Syrjälä8553c182013-12-05 15:51:39 +02002595 /*
2596 * IVB workaround: must disable low power watermarks for at least
2597 * one frame before enabling scaling. LP watermarks can be re-enabled
2598 * when scaling is disabled.
2599 *
2600 * WaCxSRDisabledForSpriteScaling:ivb
2601 */
2602 if (IS_IVYBRIDGE(dev) && scaled && ilk_disable_lp_wm(dev))
2603 intel_wait_for_vblank(dev, intel_plane->pipe);
2604
Imre Deak820c1982013-12-17 14:46:36 +02002605 ilk_update_wm(crtc);
Paulo Zanoni526682e2013-05-24 11:59:18 -03002606}
2607
Ville Syrjälä243e6a42013-10-14 14:55:24 +03002608static void ilk_pipe_wm_get_hw_state(struct drm_crtc *crtc)
2609{
2610 struct drm_device *dev = crtc->dev;
2611 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak820c1982013-12-17 14:46:36 +02002612 struct ilk_wm_values *hw = &dev_priv->wm.hw;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03002613 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2614 struct intel_pipe_wm *active = &intel_crtc->wm.active;
2615 enum pipe pipe = intel_crtc->pipe;
2616 static const unsigned int wm0_pipe_reg[] = {
2617 [PIPE_A] = WM0_PIPEA_ILK,
2618 [PIPE_B] = WM0_PIPEB_ILK,
2619 [PIPE_C] = WM0_PIPEC_IVB,
2620 };
2621
2622 hw->wm_pipe[pipe] = I915_READ(wm0_pipe_reg[pipe]);
Ville Syrjäläa42a5712014-01-07 16:14:08 +02002623 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Ville Syrjäläce0e0712013-12-05 15:51:36 +02002624 hw->wm_linetime[pipe] = I915_READ(PIPE_WM_LINETIME(pipe));
Ville Syrjälä243e6a42013-10-14 14:55:24 +03002625
2626 if (intel_crtc_active(crtc)) {
2627 u32 tmp = hw->wm_pipe[pipe];
2628
2629 /*
2630 * For active pipes LP0 watermark is marked as
2631 * enabled, and LP1+ watermaks as disabled since
2632 * we can't really reverse compute them in case
2633 * multiple pipes are active.
2634 */
2635 active->wm[0].enable = true;
2636 active->wm[0].pri_val = (tmp & WM0_PIPE_PLANE_MASK) >> WM0_PIPE_PLANE_SHIFT;
2637 active->wm[0].spr_val = (tmp & WM0_PIPE_SPRITE_MASK) >> WM0_PIPE_SPRITE_SHIFT;
2638 active->wm[0].cur_val = tmp & WM0_PIPE_CURSOR_MASK;
2639 active->linetime = hw->wm_linetime[pipe];
2640 } else {
2641 int level, max_level = ilk_wm_max_level(dev);
2642
2643 /*
2644 * For inactive pipes, all watermark levels
2645 * should be marked as enabled but zeroed,
2646 * which is what we'd compute them to.
2647 */
2648 for (level = 0; level <= max_level; level++)
2649 active->wm[level].enable = true;
2650 }
2651}
2652
2653void ilk_wm_get_hw_state(struct drm_device *dev)
2654{
2655 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak820c1982013-12-17 14:46:36 +02002656 struct ilk_wm_values *hw = &dev_priv->wm.hw;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03002657 struct drm_crtc *crtc;
2658
2659 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
2660 ilk_pipe_wm_get_hw_state(crtc);
2661
2662 hw->wm_lp[0] = I915_READ(WM1_LP_ILK);
2663 hw->wm_lp[1] = I915_READ(WM2_LP_ILK);
2664 hw->wm_lp[2] = I915_READ(WM3_LP_ILK);
2665
2666 hw->wm_lp_spr[0] = I915_READ(WM1S_LP_ILK);
Ville Syrjäläcfa76982014-03-07 18:32:08 +02002667 if (INTEL_INFO(dev)->gen >= 7) {
2668 hw->wm_lp_spr[1] = I915_READ(WM2S_LP_IVB);
2669 hw->wm_lp_spr[2] = I915_READ(WM3S_LP_IVB);
2670 }
Ville Syrjälä243e6a42013-10-14 14:55:24 +03002671
Ville Syrjäläa42a5712014-01-07 16:14:08 +02002672 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Ville Syrjäläac9545f2013-12-05 15:51:28 +02002673 hw->partitioning = (I915_READ(WM_MISC) & WM_MISC_DATA_PARTITION_5_6) ?
2674 INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
2675 else if (IS_IVYBRIDGE(dev))
2676 hw->partitioning = (I915_READ(DISP_ARB_CTL2) & DISP_DATA_PARTITION_5_6) ?
2677 INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03002678
2679 hw->enable_fbc_wm =
2680 !(I915_READ(DISP_ARB_CTL) & DISP_FBC_WM_DIS);
2681}
2682
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002683/**
2684 * intel_update_watermarks - update FIFO watermark values based on current modes
2685 *
2686 * Calculate watermark values for the various WM regs based on current mode
2687 * and plane configuration.
2688 *
2689 * There are several cases to deal with here:
2690 * - normal (i.e. non-self-refresh)
2691 * - self-refresh (SR) mode
2692 * - lines are large relative to FIFO size (buffer can hold up to 2)
2693 * - lines are small relative to FIFO size (buffer can hold more than 2
2694 * lines), so need to account for TLB latency
2695 *
2696 * The normal calculation is:
2697 * watermark = dotclock * bytes per pixel * latency
2698 * where latency is platform & configuration dependent (we assume pessimal
2699 * values here).
2700 *
2701 * The SR calculation is:
2702 * watermark = (trunc(latency/line time)+1) * surface width *
2703 * bytes per pixel
2704 * where
2705 * line time = htotal / dotclock
2706 * surface width = hdisplay for normal plane and 64 for cursor
2707 * and latency is assumed to be high, as above.
2708 *
2709 * The final value programmed to the register should always be rounded up,
2710 * and include an extra 2 entries to account for clock crossings.
2711 *
2712 * We don't use the sprite, so we can ignore that. And on Crestline we have
2713 * to set the non-SR watermarks to 8.
2714 */
Ville Syrjälä46ba6142013-09-10 11:40:40 +03002715void intel_update_watermarks(struct drm_crtc *crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002716{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03002717 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002718
2719 if (dev_priv->display.update_wm)
Ville Syrjälä46ba6142013-09-10 11:40:40 +03002720 dev_priv->display.update_wm(crtc);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002721}
2722
Ville Syrjäläadf3d352013-08-06 22:24:11 +03002723void intel_update_sprite_watermarks(struct drm_plane *plane,
2724 struct drm_crtc *crtc,
Paulo Zanoni4c4ff432013-05-24 11:59:17 -03002725 uint32_t sprite_width, int pixel_size,
Ville Syrjälä39db4a42013-08-06 22:24:00 +03002726 bool enabled, bool scaled)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002727{
Ville Syrjäläadf3d352013-08-06 22:24:11 +03002728 struct drm_i915_private *dev_priv = plane->dev->dev_private;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002729
2730 if (dev_priv->display.update_sprite_wm)
Ville Syrjäläadf3d352013-08-06 22:24:11 +03002731 dev_priv->display.update_sprite_wm(plane, crtc, sprite_width,
Ville Syrjälä39db4a42013-08-06 22:24:00 +03002732 pixel_size, enabled, scaled);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002733}
2734
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002735static struct drm_i915_gem_object *
2736intel_alloc_context_page(struct drm_device *dev)
2737{
2738 struct drm_i915_gem_object *ctx;
2739 int ret;
2740
2741 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2742
2743 ctx = i915_gem_alloc_object(dev, 4096);
2744 if (!ctx) {
2745 DRM_DEBUG("failed to alloc power context, RC6 disabled\n");
2746 return NULL;
2747 }
2748
Daniel Vetterc69766f2014-02-14 14:01:17 +01002749 ret = i915_gem_obj_ggtt_pin(ctx, 4096, 0);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002750 if (ret) {
2751 DRM_ERROR("failed to pin power context: %d\n", ret);
2752 goto err_unref;
2753 }
2754
2755 ret = i915_gem_object_set_to_gtt_domain(ctx, 1);
2756 if (ret) {
2757 DRM_ERROR("failed to set-domain on power context: %d\n", ret);
2758 goto err_unpin;
2759 }
2760
2761 return ctx;
2762
2763err_unpin:
Ben Widawskyd7f46fc2013-12-06 14:10:55 -08002764 i915_gem_object_ggtt_unpin(ctx);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002765err_unref:
2766 drm_gem_object_unreference(&ctx->base);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002767 return NULL;
2768}
2769
Daniel Vetter92703882012-08-09 16:46:01 +02002770/**
2771 * Lock protecting IPS related data structures
Daniel Vetter92703882012-08-09 16:46:01 +02002772 */
2773DEFINE_SPINLOCK(mchdev_lock);
2774
2775/* Global for IPS driver to get at the current i915 device. Protected by
2776 * mchdev_lock. */
2777static struct drm_i915_private *i915_mch_dev;
2778
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002779bool ironlake_set_drps(struct drm_device *dev, u8 val)
2780{
2781 struct drm_i915_private *dev_priv = dev->dev_private;
2782 u16 rgvswctl;
2783
Daniel Vetter92703882012-08-09 16:46:01 +02002784 assert_spin_locked(&mchdev_lock);
2785
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002786 rgvswctl = I915_READ16(MEMSWCTL);
2787 if (rgvswctl & MEMCTL_CMD_STS) {
2788 DRM_DEBUG("gpu busy, RCS change rejected\n");
2789 return false; /* still busy with another command */
2790 }
2791
2792 rgvswctl = (MEMCTL_CMD_CHFREQ << MEMCTL_CMD_SHIFT) |
2793 (val << MEMCTL_FREQ_SHIFT) | MEMCTL_SFCAVM;
2794 I915_WRITE16(MEMSWCTL, rgvswctl);
2795 POSTING_READ16(MEMSWCTL);
2796
2797 rgvswctl |= MEMCTL_CMD_STS;
2798 I915_WRITE16(MEMSWCTL, rgvswctl);
2799
2800 return true;
2801}
2802
Daniel Vetter8090c6b2012-06-24 16:42:32 +02002803static void ironlake_enable_drps(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002804{
2805 struct drm_i915_private *dev_priv = dev->dev_private;
2806 u32 rgvmodectl = I915_READ(MEMMODECTL);
2807 u8 fmax, fmin, fstart, vstart;
2808
Daniel Vetter92703882012-08-09 16:46:01 +02002809 spin_lock_irq(&mchdev_lock);
2810
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002811 /* Enable temp reporting */
2812 I915_WRITE16(PMMISC, I915_READ(PMMISC) | MCPPCE_EN);
2813 I915_WRITE16(TSC1, I915_READ(TSC1) | TSE);
2814
2815 /* 100ms RC evaluation intervals */
2816 I915_WRITE(RCUPEI, 100000);
2817 I915_WRITE(RCDNEI, 100000);
2818
2819 /* Set max/min thresholds to 90ms and 80ms respectively */
2820 I915_WRITE(RCBMAXAVG, 90000);
2821 I915_WRITE(RCBMINAVG, 80000);
2822
2823 I915_WRITE(MEMIHYST, 1);
2824
2825 /* Set up min, max, and cur for interrupt handling */
2826 fmax = (rgvmodectl & MEMMODE_FMAX_MASK) >> MEMMODE_FMAX_SHIFT;
2827 fmin = (rgvmodectl & MEMMODE_FMIN_MASK);
2828 fstart = (rgvmodectl & MEMMODE_FSTART_MASK) >>
2829 MEMMODE_FSTART_SHIFT;
2830
2831 vstart = (I915_READ(PXVFREQ_BASE + (fstart * 4)) & PXVFREQ_PX_MASK) >>
2832 PXVFREQ_PX_SHIFT;
2833
Daniel Vetter20e4d402012-08-08 23:35:39 +02002834 dev_priv->ips.fmax = fmax; /* IPS callback will increase this */
2835 dev_priv->ips.fstart = fstart;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002836
Daniel Vetter20e4d402012-08-08 23:35:39 +02002837 dev_priv->ips.max_delay = fstart;
2838 dev_priv->ips.min_delay = fmin;
2839 dev_priv->ips.cur_delay = fstart;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002840
2841 DRM_DEBUG_DRIVER("fmax: %d, fmin: %d, fstart: %d\n",
2842 fmax, fmin, fstart);
2843
2844 I915_WRITE(MEMINTREN, MEMINT_CX_SUPR_EN | MEMINT_EVAL_CHG_EN);
2845
2846 /*
2847 * Interrupts will be enabled in ironlake_irq_postinstall
2848 */
2849
2850 I915_WRITE(VIDSTART, vstart);
2851 POSTING_READ(VIDSTART);
2852
2853 rgvmodectl |= MEMMODE_SWMODE_EN;
2854 I915_WRITE(MEMMODECTL, rgvmodectl);
2855
Daniel Vetter92703882012-08-09 16:46:01 +02002856 if (wait_for_atomic((I915_READ(MEMSWCTL) & MEMCTL_CMD_STS) == 0, 10))
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002857 DRM_ERROR("stuck trying to change perf mode\n");
Daniel Vetter92703882012-08-09 16:46:01 +02002858 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002859
2860 ironlake_set_drps(dev, fstart);
2861
Daniel Vetter20e4d402012-08-08 23:35:39 +02002862 dev_priv->ips.last_count1 = I915_READ(0x112e4) + I915_READ(0x112e8) +
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002863 I915_READ(0x112e0);
Daniel Vetter20e4d402012-08-08 23:35:39 +02002864 dev_priv->ips.last_time1 = jiffies_to_msecs(jiffies);
2865 dev_priv->ips.last_count2 = I915_READ(0x112f4);
2866 getrawmonotonic(&dev_priv->ips.last_time2);
Daniel Vetter92703882012-08-09 16:46:01 +02002867
2868 spin_unlock_irq(&mchdev_lock);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002869}
2870
Daniel Vetter8090c6b2012-06-24 16:42:32 +02002871static void ironlake_disable_drps(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002872{
2873 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter92703882012-08-09 16:46:01 +02002874 u16 rgvswctl;
2875
2876 spin_lock_irq(&mchdev_lock);
2877
2878 rgvswctl = I915_READ16(MEMSWCTL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002879
2880 /* Ack interrupts, disable EFC interrupt */
2881 I915_WRITE(MEMINTREN, I915_READ(MEMINTREN) & ~MEMINT_EVAL_CHG_EN);
2882 I915_WRITE(MEMINTRSTS, MEMINT_EVAL_CHG);
2883 I915_WRITE(DEIER, I915_READ(DEIER) & ~DE_PCU_EVENT);
2884 I915_WRITE(DEIIR, DE_PCU_EVENT);
2885 I915_WRITE(DEIMR, I915_READ(DEIMR) | DE_PCU_EVENT);
2886
2887 /* Go back to the starting frequency */
Daniel Vetter20e4d402012-08-08 23:35:39 +02002888 ironlake_set_drps(dev, dev_priv->ips.fstart);
Daniel Vetter92703882012-08-09 16:46:01 +02002889 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002890 rgvswctl |= MEMCTL_CMD_STS;
2891 I915_WRITE(MEMSWCTL, rgvswctl);
Daniel Vetter92703882012-08-09 16:46:01 +02002892 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002893
Daniel Vetter92703882012-08-09 16:46:01 +02002894 spin_unlock_irq(&mchdev_lock);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002895}
2896
Daniel Vetteracbe9472012-07-26 11:50:05 +02002897/* There's a funny hw issue where the hw returns all 0 when reading from
2898 * GEN6_RP_INTERRUPT_LIMITS. Hence we always need to compute the desired value
2899 * ourselves, instead of doing a rmw cycle (which might result in us clearing
2900 * all limits and the gpu stuck at whatever frequency it is at atm).
2901 */
Chris Wilson6917c7b2013-11-06 13:56:26 -02002902static u32 gen6_rps_limits(struct drm_i915_private *dev_priv, u8 val)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002903{
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01002904 u32 limits;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002905
Daniel Vetter20b46e52012-07-26 11:16:14 +02002906 /* Only set the down limit when we've reached the lowest level to avoid
2907 * getting more interrupts, otherwise leave this clear. This prevents a
2908 * race in the hw when coming out of rc6: There's a tiny window where
2909 * the hw runs at the minimal clock before selecting the desired
2910 * frequency, if the down threshold expires in that window we will not
2911 * receive a down interrupt. */
Ben Widawskyb39fb292014-03-19 18:31:11 -07002912 limits = dev_priv->rps.max_freq_softlimit << 24;
2913 if (val <= dev_priv->rps.min_freq_softlimit)
2914 limits |= dev_priv->rps.min_freq_softlimit << 16;
Daniel Vetter20b46e52012-07-26 11:16:14 +02002915
2916 return limits;
2917}
2918
Chris Wilsondd75fdc2013-09-25 17:34:57 +01002919static void gen6_set_rps_thresholds(struct drm_i915_private *dev_priv, u8 val)
2920{
2921 int new_power;
2922
2923 new_power = dev_priv->rps.power;
2924 switch (dev_priv->rps.power) {
2925 case LOW_POWER:
Ben Widawskyb39fb292014-03-19 18:31:11 -07002926 if (val > dev_priv->rps.efficient_freq + 1 && val > dev_priv->rps.cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01002927 new_power = BETWEEN;
2928 break;
2929
2930 case BETWEEN:
Ben Widawskyb39fb292014-03-19 18:31:11 -07002931 if (val <= dev_priv->rps.efficient_freq && val < dev_priv->rps.cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01002932 new_power = LOW_POWER;
Ben Widawskyb39fb292014-03-19 18:31:11 -07002933 else if (val >= dev_priv->rps.rp0_freq && val > dev_priv->rps.cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01002934 new_power = HIGH_POWER;
2935 break;
2936
2937 case HIGH_POWER:
Ben Widawskyb39fb292014-03-19 18:31:11 -07002938 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 +01002939 new_power = BETWEEN;
2940 break;
2941 }
2942 /* Max/min bins are special */
Ben Widawskyb39fb292014-03-19 18:31:11 -07002943 if (val == dev_priv->rps.min_freq_softlimit)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01002944 new_power = LOW_POWER;
Ben Widawskyb39fb292014-03-19 18:31:11 -07002945 if (val == dev_priv->rps.max_freq_softlimit)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01002946 new_power = HIGH_POWER;
2947 if (new_power == dev_priv->rps.power)
2948 return;
2949
2950 /* Note the units here are not exactly 1us, but 1280ns. */
2951 switch (new_power) {
2952 case LOW_POWER:
2953 /* Upclock if more than 95% busy over 16ms */
2954 I915_WRITE(GEN6_RP_UP_EI, 12500);
2955 I915_WRITE(GEN6_RP_UP_THRESHOLD, 11800);
2956
2957 /* Downclock if less than 85% busy over 32ms */
2958 I915_WRITE(GEN6_RP_DOWN_EI, 25000);
2959 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 21250);
2960
2961 I915_WRITE(GEN6_RP_CONTROL,
2962 GEN6_RP_MEDIA_TURBO |
2963 GEN6_RP_MEDIA_HW_NORMAL_MODE |
2964 GEN6_RP_MEDIA_IS_GFX |
2965 GEN6_RP_ENABLE |
2966 GEN6_RP_UP_BUSY_AVG |
2967 GEN6_RP_DOWN_IDLE_AVG);
2968 break;
2969
2970 case BETWEEN:
2971 /* Upclock if more than 90% busy over 13ms */
2972 I915_WRITE(GEN6_RP_UP_EI, 10250);
2973 I915_WRITE(GEN6_RP_UP_THRESHOLD, 9225);
2974
2975 /* Downclock if less than 75% busy over 32ms */
2976 I915_WRITE(GEN6_RP_DOWN_EI, 25000);
2977 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 18750);
2978
2979 I915_WRITE(GEN6_RP_CONTROL,
2980 GEN6_RP_MEDIA_TURBO |
2981 GEN6_RP_MEDIA_HW_NORMAL_MODE |
2982 GEN6_RP_MEDIA_IS_GFX |
2983 GEN6_RP_ENABLE |
2984 GEN6_RP_UP_BUSY_AVG |
2985 GEN6_RP_DOWN_IDLE_AVG);
2986 break;
2987
2988 case HIGH_POWER:
2989 /* Upclock if more than 85% busy over 10ms */
2990 I915_WRITE(GEN6_RP_UP_EI, 8000);
2991 I915_WRITE(GEN6_RP_UP_THRESHOLD, 6800);
2992
2993 /* Downclock if less than 60% busy over 32ms */
2994 I915_WRITE(GEN6_RP_DOWN_EI, 25000);
2995 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 15000);
2996
2997 I915_WRITE(GEN6_RP_CONTROL,
2998 GEN6_RP_MEDIA_TURBO |
2999 GEN6_RP_MEDIA_HW_NORMAL_MODE |
3000 GEN6_RP_MEDIA_IS_GFX |
3001 GEN6_RP_ENABLE |
3002 GEN6_RP_UP_BUSY_AVG |
3003 GEN6_RP_DOWN_IDLE_AVG);
3004 break;
3005 }
3006
3007 dev_priv->rps.power = new_power;
3008 dev_priv->rps.last_adj = 0;
3009}
3010
Chris Wilson2876ce72014-03-28 08:03:34 +00003011static u32 gen6_rps_pm_mask(struct drm_i915_private *dev_priv, u8 val)
3012{
3013 u32 mask = 0;
3014
3015 if (val > dev_priv->rps.min_freq_softlimit)
3016 mask |= GEN6_PM_RP_DOWN_THRESHOLD | GEN6_PM_RP_DOWN_TIMEOUT;
3017 if (val < dev_priv->rps.max_freq_softlimit)
3018 mask |= GEN6_PM_RP_UP_THRESHOLD;
3019
3020 /* IVB and SNB hard hangs on looping batchbuffer
3021 * if GEN6_PM_UP_EI_EXPIRED is masked.
3022 */
3023 if (INTEL_INFO(dev_priv->dev)->gen <= 7 && !IS_HASWELL(dev_priv->dev))
3024 mask |= GEN6_PM_RP_UP_EI_EXPIRED;
3025
3026 return ~mask;
3027}
3028
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06003029/* gen6_set_rps is called to update the frequency request, but should also be
3030 * called when the range (min_delay and max_delay) is modified so that we can
3031 * update the GEN6_RP_INTERRUPT_LIMITS register accordingly. */
Daniel Vetter20b46e52012-07-26 11:16:14 +02003032void gen6_set_rps(struct drm_device *dev, u8 val)
3033{
3034 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01003035
Jesse Barnes4fc688c2012-11-02 11:14:01 -07003036 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Ben Widawskyb39fb292014-03-19 18:31:11 -07003037 WARN_ON(val > dev_priv->rps.max_freq_softlimit);
3038 WARN_ON(val < dev_priv->rps.min_freq_softlimit);
Daniel Vetter004777c2012-08-09 15:07:01 +02003039
Chris Wilsoneb64cad2014-03-27 08:24:20 +00003040 /* min/max delay may still have been modified so be sure to
3041 * write the limits value.
3042 */
3043 if (val != dev_priv->rps.cur_freq) {
3044 gen6_set_rps_thresholds(dev_priv, val);
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06003045
Ben Widawsky50e6a2a2014-03-31 17:16:43 -07003046 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Chris Wilsoneb64cad2014-03-27 08:24:20 +00003047 I915_WRITE(GEN6_RPNSWREQ,
3048 HSW_FREQUENCY(val));
3049 else
3050 I915_WRITE(GEN6_RPNSWREQ,
3051 GEN6_FREQUENCY(val) |
3052 GEN6_OFFSET(0) |
3053 GEN6_AGGRESSIVE_TURBO);
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06003054 }
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01003055
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01003056 /* Make sure we continue to get interrupts
3057 * until we hit the minimum or maximum frequencies.
3058 */
Chris Wilsoneb64cad2014-03-27 08:24:20 +00003059 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS, gen6_rps_limits(dev_priv, val));
Chris Wilson2876ce72014-03-28 08:03:34 +00003060 I915_WRITE(GEN6_PMINTRMSK, gen6_rps_pm_mask(dev_priv, val));
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01003061
Ben Widawskyd5570a72012-09-07 19:43:41 -07003062 POSTING_READ(GEN6_RPNSWREQ);
3063
Ben Widawskyb39fb292014-03-19 18:31:11 -07003064 dev_priv->rps.cur_freq = val;
Daniel Vetterbe2cde9a2012-08-30 13:26:48 +02003065 trace_intel_gpu_freq_change(val * 50);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003066}
3067
Deepak S76c3552f2014-01-30 23:08:16 +05303068/* vlv_set_rps_idle: Set the frequency to Rpn if Gfx clocks are down
3069 *
3070 * * If Gfx is Idle, then
3071 * 1. Mask Turbo interrupts
3072 * 2. Bring up Gfx clock
3073 * 3. Change the freq to Rpn and wait till P-Unit updates freq
3074 * 4. Clear the Force GFX CLK ON bit so that Gfx can down
3075 * 5. Unmask Turbo interrupts
3076*/
3077static void vlv_set_rps_idle(struct drm_i915_private *dev_priv)
3078{
3079 /*
3080 * When we are idle. Drop to min voltage state.
3081 */
3082
Ben Widawskyb39fb292014-03-19 18:31:11 -07003083 if (dev_priv->rps.cur_freq <= dev_priv->rps.min_freq_softlimit)
Deepak S76c3552f2014-01-30 23:08:16 +05303084 return;
3085
3086 /* Mask turbo interrupt so that they will not come in between */
3087 I915_WRITE(GEN6_PMINTRMSK, 0xffffffff);
3088
3089 /* Bring up the Gfx clock */
3090 I915_WRITE(VLV_GTLC_SURVIVABILITY_REG,
3091 I915_READ(VLV_GTLC_SURVIVABILITY_REG) |
3092 VLV_GFX_CLK_FORCE_ON_BIT);
3093
3094 if (wait_for(((VLV_GFX_CLK_STATUS_BIT &
3095 I915_READ(VLV_GTLC_SURVIVABILITY_REG)) != 0), 5)) {
3096 DRM_ERROR("GFX_CLK_ON request timed out\n");
3097 return;
3098 }
3099
Ben Widawskyb39fb292014-03-19 18:31:11 -07003100 dev_priv->rps.cur_freq = dev_priv->rps.min_freq_softlimit;
Deepak S76c3552f2014-01-30 23:08:16 +05303101
3102 vlv_punit_write(dev_priv, PUNIT_REG_GPU_FREQ_REQ,
Ben Widawskyb39fb292014-03-19 18:31:11 -07003103 dev_priv->rps.min_freq_softlimit);
Deepak S76c3552f2014-01-30 23:08:16 +05303104
3105 if (wait_for(((vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS))
3106 & GENFREQSTATUS) == 0, 5))
3107 DRM_ERROR("timed out waiting for Punit\n");
3108
3109 /* Release the Gfx clock */
3110 I915_WRITE(VLV_GTLC_SURVIVABILITY_REG,
3111 I915_READ(VLV_GTLC_SURVIVABILITY_REG) &
3112 ~VLV_GFX_CLK_FORCE_ON_BIT);
Chris Wilson2876ce72014-03-28 08:03:34 +00003113
3114 I915_WRITE(GEN6_PMINTRMSK,
3115 gen6_rps_pm_mask(dev_priv, dev_priv->rps.cur_freq));
Deepak S76c3552f2014-01-30 23:08:16 +05303116}
3117
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003118void gen6_rps_idle(struct drm_i915_private *dev_priv)
3119{
Damien Lespiau691bb712013-12-12 14:36:36 +00003120 struct drm_device *dev = dev_priv->dev;
3121
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003122 mutex_lock(&dev_priv->rps.hw_lock);
Chris Wilsonc0951f02013-10-10 21:58:50 +01003123 if (dev_priv->rps.enabled) {
Damien Lespiau691bb712013-12-12 14:36:36 +00003124 if (IS_VALLEYVIEW(dev))
Deepak S76c3552f2014-01-30 23:08:16 +05303125 vlv_set_rps_idle(dev_priv);
Chris Wilsonc0951f02013-10-10 21:58:50 +01003126 else
Ben Widawskyb39fb292014-03-19 18:31:11 -07003127 gen6_set_rps(dev_priv->dev, dev_priv->rps.min_freq_softlimit);
Chris Wilsonc0951f02013-10-10 21:58:50 +01003128 dev_priv->rps.last_adj = 0;
3129 }
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003130 mutex_unlock(&dev_priv->rps.hw_lock);
3131}
3132
3133void gen6_rps_boost(struct drm_i915_private *dev_priv)
3134{
Damien Lespiau691bb712013-12-12 14:36:36 +00003135 struct drm_device *dev = dev_priv->dev;
3136
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003137 mutex_lock(&dev_priv->rps.hw_lock);
Chris Wilsonc0951f02013-10-10 21:58:50 +01003138 if (dev_priv->rps.enabled) {
Damien Lespiau691bb712013-12-12 14:36:36 +00003139 if (IS_VALLEYVIEW(dev))
Ben Widawskyb39fb292014-03-19 18:31:11 -07003140 valleyview_set_rps(dev_priv->dev, dev_priv->rps.max_freq_softlimit);
Chris Wilsonc0951f02013-10-10 21:58:50 +01003141 else
Ben Widawskyb39fb292014-03-19 18:31:11 -07003142 gen6_set_rps(dev_priv->dev, dev_priv->rps.max_freq_softlimit);
Chris Wilsonc0951f02013-10-10 21:58:50 +01003143 dev_priv->rps.last_adj = 0;
3144 }
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003145 mutex_unlock(&dev_priv->rps.hw_lock);
3146}
3147
Jesse Barnes0a073b82013-04-17 15:54:58 -07003148void valleyview_set_rps(struct drm_device *dev, u8 val)
3149{
3150 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä7a670922013-06-25 19:21:06 +03003151
Jesse Barnes0a073b82013-04-17 15:54:58 -07003152 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Ben Widawskyb39fb292014-03-19 18:31:11 -07003153 WARN_ON(val > dev_priv->rps.max_freq_softlimit);
3154 WARN_ON(val < dev_priv->rps.min_freq_softlimit);
Jesse Barnes0a073b82013-04-17 15:54:58 -07003155
Ville Syrjälä73008b92013-06-25 19:21:01 +03003156 DRM_DEBUG_DRIVER("GPU freq request from %d MHz (%u) to %d MHz (%u)\n",
Ben Widawskyb39fb292014-03-19 18:31:11 -07003157 vlv_gpu_freq(dev_priv, dev_priv->rps.cur_freq),
3158 dev_priv->rps.cur_freq,
Ville Syrjälä2ec38152013-11-05 22:42:29 +02003159 vlv_gpu_freq(dev_priv, val), val);
Jesse Barnes0a073b82013-04-17 15:54:58 -07003160
Chris Wilson2876ce72014-03-28 08:03:34 +00003161 if (val != dev_priv->rps.cur_freq)
3162 vlv_punit_write(dev_priv, PUNIT_REG_GPU_FREQ_REQ, val);
Jesse Barnes0a073b82013-04-17 15:54:58 -07003163
Imre Deak09c87db2014-04-03 20:02:42 +03003164 I915_WRITE(GEN6_PMINTRMSK, gen6_rps_pm_mask(dev_priv, val));
Jesse Barnes0a073b82013-04-17 15:54:58 -07003165
Ben Widawskyb39fb292014-03-19 18:31:11 -07003166 dev_priv->rps.cur_freq = val;
Ville Syrjälä2ec38152013-11-05 22:42:29 +02003167 trace_intel_gpu_freq_change(vlv_gpu_freq(dev_priv, val));
Jesse Barnes0a073b82013-04-17 15:54:58 -07003168}
3169
Daniel Vetter44fc7d52013-07-12 22:43:27 +02003170static void gen6_disable_rps_interrupts(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003171{
3172 struct drm_i915_private *dev_priv = dev->dev_private;
3173
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003174 I915_WRITE(GEN6_PMINTRMSK, 0xffffffff);
Deepak Sa6706b42014-03-15 20:23:22 +05303175 I915_WRITE(GEN6_PMIER, I915_READ(GEN6_PMIER) &
3176 ~dev_priv->pm_rps_events);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003177 /* Complete PM interrupt masking here doesn't race with the rps work
3178 * item again unmasking PM interrupts because that is using a different
3179 * register (PMIMR) to mask PM interrupts. The only risk is in leaving
3180 * stale bits in PMIIR and PMIMR which gen6_enable_rps will clean up. */
3181
Daniel Vetter59cdb632013-07-04 23:35:28 +02003182 spin_lock_irq(&dev_priv->irq_lock);
Daniel Vetterc6a828d2012-08-08 23:35:35 +02003183 dev_priv->rps.pm_iir = 0;
Daniel Vetter59cdb632013-07-04 23:35:28 +02003184 spin_unlock_irq(&dev_priv->irq_lock);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003185
Deepak Sa6706b42014-03-15 20:23:22 +05303186 I915_WRITE(GEN6_PMIIR, dev_priv->pm_rps_events);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003187}
3188
Daniel Vetter44fc7d52013-07-12 22:43:27 +02003189static void gen6_disable_rps(struct drm_device *dev)
3190{
3191 struct drm_i915_private *dev_priv = dev->dev_private;
3192
3193 I915_WRITE(GEN6_RC_CONTROL, 0);
3194 I915_WRITE(GEN6_RPNSWREQ, 1 << 31);
3195
3196 gen6_disable_rps_interrupts(dev);
3197}
3198
Jesse Barnesd20d4f02013-04-23 10:09:28 -07003199static void valleyview_disable_rps(struct drm_device *dev)
3200{
3201 struct drm_i915_private *dev_priv = dev->dev_private;
3202
3203 I915_WRITE(GEN6_RC_CONTROL, 0);
Jesse Barnesd20d4f02013-04-23 10:09:28 -07003204
Daniel Vetter44fc7d52013-07-12 22:43:27 +02003205 gen6_disable_rps_interrupts(dev);
Jesse Barnesd20d4f02013-04-23 10:09:28 -07003206}
3207
Ben Widawskydc39fff2013-10-18 12:32:07 -07003208static void intel_print_rc6_info(struct drm_device *dev, u32 mode)
3209{
Ben Widawskydc39fff2013-10-18 12:32:07 -07003210 DRM_INFO("Enabling RC6 states: RC6 %s, RC6p %s, RC6pp %s\n",
Ben Widawsky1c79b422014-01-28 20:25:40 -08003211 (mode & GEN6_RC_CTL_RC6_ENABLE) ? "on" : "off",
3212 (mode & GEN6_RC_CTL_RC6p_ENABLE) ? "on" : "off",
3213 (mode & GEN6_RC_CTL_RC6pp_ENABLE) ? "on" : "off");
Ben Widawskydc39fff2013-10-18 12:32:07 -07003214}
3215
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003216int intel_enable_rc6(const struct drm_device *dev)
3217{
Damien Lespiaueb4926e2013-06-07 17:41:14 +01003218 /* No RC6 before Ironlake */
3219 if (INTEL_INFO(dev)->gen < 5)
3220 return 0;
3221
Daniel Vetter456470e2012-08-08 23:35:40 +02003222 /* Respect the kernel parameter if it is set */
Jani Nikulad330a952014-01-21 11:24:25 +02003223 if (i915.enable_rc6 >= 0)
3224 return i915.enable_rc6;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003225
Chris Wilson6567d742012-11-10 10:00:06 +00003226 /* Disable RC6 on Ironlake */
3227 if (INTEL_INFO(dev)->gen == 5)
3228 return 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003229
Ben Widawsky8bade1a2014-01-28 20:25:39 -08003230 if (IS_IVYBRIDGE(dev))
Ben Widawskycca84a12014-01-28 20:25:38 -08003231 return (INTEL_RC6_ENABLE | INTEL_RC6p_ENABLE);
Ben Widawsky8bade1a2014-01-28 20:25:39 -08003232
3233 return INTEL_RC6_ENABLE;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003234}
3235
Daniel Vetter44fc7d52013-07-12 22:43:27 +02003236static void gen6_enable_rps_interrupts(struct drm_device *dev)
3237{
3238 struct drm_i915_private *dev_priv = dev->dev_private;
3239
3240 spin_lock_irq(&dev_priv->irq_lock);
Daniel Vettera0b33352013-07-04 23:35:34 +02003241 WARN_ON(dev_priv->rps.pm_iir);
Deepak Sa6706b42014-03-15 20:23:22 +05303242 snb_enable_pm_irq(dev_priv, dev_priv->pm_rps_events);
3243 I915_WRITE(GEN6_PMIIR, dev_priv->pm_rps_events);
Daniel Vetter44fc7d52013-07-12 22:43:27 +02003244 spin_unlock_irq(&dev_priv->irq_lock);
Daniel Vetter44fc7d52013-07-12 22:43:27 +02003245}
3246
Ben Widawsky3280e8b2014-03-31 17:16:42 -07003247static void parse_rp_state_cap(struct drm_i915_private *dev_priv, u32 rp_state_cap)
3248{
3249 /* All of these values are in units of 50MHz */
3250 dev_priv->rps.cur_freq = 0;
3251 /* static values from HW: RP0 < RPe < RP1 < RPn (min_freq) */
3252 dev_priv->rps.rp1_freq = (rp_state_cap >> 8) & 0xff;
3253 dev_priv->rps.rp0_freq = (rp_state_cap >> 0) & 0xff;
3254 dev_priv->rps.min_freq = (rp_state_cap >> 16) & 0xff;
3255 /* XXX: only BYT has a special efficient freq */
3256 dev_priv->rps.efficient_freq = dev_priv->rps.rp1_freq;
3257 /* hw_max = RP0 until we check for overclocking */
3258 dev_priv->rps.max_freq = dev_priv->rps.rp0_freq;
3259
3260 /* Preserve min/max settings in case of re-init */
3261 if (dev_priv->rps.max_freq_softlimit == 0)
3262 dev_priv->rps.max_freq_softlimit = dev_priv->rps.max_freq;
3263
3264 if (dev_priv->rps.min_freq_softlimit == 0)
3265 dev_priv->rps.min_freq_softlimit = dev_priv->rps.min_freq;
3266}
3267
Ben Widawsky6edee7f2013-11-02 21:07:52 -07003268static void gen8_enable_rps(struct drm_device *dev)
3269{
3270 struct drm_i915_private *dev_priv = dev->dev_private;
3271 struct intel_ring_buffer *ring;
3272 uint32_t rc6_mask = 0, rp_state_cap;
3273 int unused;
3274
3275 /* 1a: Software RC state - RC0 */
3276 I915_WRITE(GEN6_RC_STATE, 0);
3277
3278 /* 1c & 1d: Get forcewake during program sequence. Although the driver
3279 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
Deepak Sc8d9a592013-11-23 14:55:42 +05303280 gen6_gt_force_wake_get(dev_priv, FORCEWAKE_ALL);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07003281
3282 /* 2a: Disable RC states. */
3283 I915_WRITE(GEN6_RC_CONTROL, 0);
3284
3285 rp_state_cap = I915_READ(GEN6_RP_STATE_CAP);
Ben Widawsky3280e8b2014-03-31 17:16:42 -07003286 parse_rp_state_cap(dev_priv, rp_state_cap);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07003287
3288 /* 2b: Program RC6 thresholds.*/
3289 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16);
3290 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
3291 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
3292 for_each_ring(ring, dev_priv, unused)
3293 I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
3294 I915_WRITE(GEN6_RC_SLEEP, 0);
3295 I915_WRITE(GEN6_RC6_THRESHOLD, 50000); /* 50/125ms per EI */
3296
3297 /* 3: Enable RC6 */
3298 if (intel_enable_rc6(dev) & INTEL_RC6_ENABLE)
3299 rc6_mask = GEN6_RC_CTL_RC6_ENABLE;
Ben Widawskyabbf9d22014-01-28 20:25:41 -08003300 intel_print_rc6_info(dev, rc6_mask);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07003301 I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
Ben Widawskyabbf9d22014-01-28 20:25:41 -08003302 GEN6_RC_CTL_EI_MODE(1) |
3303 rc6_mask);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07003304
3305 /* 4 Program defaults and thresholds for RPS*/
Ben Widawskyf9bdc582014-03-31 17:16:41 -07003306 I915_WRITE(GEN6_RPNSWREQ,
3307 HSW_FREQUENCY(dev_priv->rps.rp1_freq));
3308 I915_WRITE(GEN6_RC_VIDEO_FREQ,
3309 HSW_FREQUENCY(dev_priv->rps.rp1_freq));
Ben Widawsky6edee7f2013-11-02 21:07:52 -07003310 /* NB: Docs say 1s, and 1000000 - which aren't equivalent */
3311 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 100000000 / 128); /* 1 second timeout */
3312
3313 /* Docs recommend 900MHz, and 300 MHz respectively */
3314 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS,
Ben Widawskyb39fb292014-03-19 18:31:11 -07003315 dev_priv->rps.max_freq_softlimit << 24 |
3316 dev_priv->rps.min_freq_softlimit << 16);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07003317
3318 I915_WRITE(GEN6_RP_UP_THRESHOLD, 7600000 / 128); /* 76ms busyness per EI, 90% */
3319 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 31300000 / 128); /* 313ms busyness per EI, 70%*/
3320 I915_WRITE(GEN6_RP_UP_EI, 66000); /* 84.48ms, XXX: random? */
3321 I915_WRITE(GEN6_RP_DOWN_EI, 350000); /* 448ms, XXX: random? */
3322
3323 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
3324
3325 /* 5: Enable RPS */
3326 I915_WRITE(GEN6_RP_CONTROL,
3327 GEN6_RP_MEDIA_TURBO |
3328 GEN6_RP_MEDIA_HW_NORMAL_MODE |
3329 GEN6_RP_MEDIA_IS_GFX |
3330 GEN6_RP_ENABLE |
3331 GEN6_RP_UP_BUSY_AVG |
3332 GEN6_RP_DOWN_IDLE_AVG);
3333
3334 /* 6: Ring frequency + overclocking (our driver does this later */
3335
3336 gen6_set_rps(dev, (I915_READ(GEN6_GT_PERF_STATUS) & 0xff00) >> 8);
3337
3338 gen6_enable_rps_interrupts(dev);
3339
Deepak Sc8d9a592013-11-23 14:55:42 +05303340 gen6_gt_force_wake_put(dev_priv, FORCEWAKE_ALL);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07003341}
3342
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02003343static void gen6_enable_rps(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003344{
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02003345 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonb4519512012-05-11 14:29:30 +01003346 struct intel_ring_buffer *ring;
Ben Widawsky2a5913a2014-03-19 18:31:13 -07003347 u32 rp_state_cap;
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01003348 u32 gt_perf_status;
Ben Widawskyd060c162014-03-19 18:31:08 -07003349 u32 rc6vids, pcu_mbox = 0, rc6_mask = 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003350 u32 gtfifodbg;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003351 int rc6_mode;
Ben Widawsky42c05262012-09-26 10:34:00 -07003352 int i, ret;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003353
Jesse Barnes4fc688c2012-11-02 11:14:01 -07003354 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02003355
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003356 /* Here begins a magic sequence of register writes to enable
3357 * auto-downclocking.
3358 *
3359 * Perhaps there might be some value in exposing these to
3360 * userspace...
3361 */
3362 I915_WRITE(GEN6_RC_STATE, 0);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003363
3364 /* Clear the DBG now so we don't confuse earlier errors */
3365 if ((gtfifodbg = I915_READ(GTFIFODBG))) {
3366 DRM_ERROR("GT fifo had a previous error %x\n", gtfifodbg);
3367 I915_WRITE(GTFIFODBG, gtfifodbg);
3368 }
3369
Deepak Sc8d9a592013-11-23 14:55:42 +05303370 gen6_gt_force_wake_get(dev_priv, FORCEWAKE_ALL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003371
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01003372 rp_state_cap = I915_READ(GEN6_RP_STATE_CAP);
3373 gt_perf_status = I915_READ(GEN6_GT_PERF_STATUS);
3374
Ben Widawsky3280e8b2014-03-31 17:16:42 -07003375 parse_rp_state_cap(dev_priv, rp_state_cap);
Jeff McGeedd0a1aa2014-02-04 11:32:31 -06003376
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003377 /* disable the counters and set deterministic thresholds */
3378 I915_WRITE(GEN6_RC_CONTROL, 0);
3379
3380 I915_WRITE(GEN6_RC1_WAKE_RATE_LIMIT, 1000 << 16);
3381 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16 | 30);
3382 I915_WRITE(GEN6_RC6pp_WAKE_RATE_LIMIT, 30);
3383 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
3384 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
3385
Chris Wilsonb4519512012-05-11 14:29:30 +01003386 for_each_ring(ring, dev_priv, i)
3387 I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003388
3389 I915_WRITE(GEN6_RC_SLEEP, 0);
3390 I915_WRITE(GEN6_RC1e_THRESHOLD, 1000);
Daniel Vetter29c78f62013-11-16 16:04:26 +01003391 if (IS_IVYBRIDGE(dev))
Stéphane Marchesin351aa562013-08-13 11:55:17 -07003392 I915_WRITE(GEN6_RC6_THRESHOLD, 125000);
3393 else
3394 I915_WRITE(GEN6_RC6_THRESHOLD, 50000);
Stéphane Marchesin0920a482013-01-29 19:41:59 -08003395 I915_WRITE(GEN6_RC6p_THRESHOLD, 150000);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003396 I915_WRITE(GEN6_RC6pp_THRESHOLD, 64000); /* unused */
3397
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03003398 /* Check if we are enabling RC6 */
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003399 rc6_mode = intel_enable_rc6(dev_priv->dev);
3400 if (rc6_mode & INTEL_RC6_ENABLE)
3401 rc6_mask |= GEN6_RC_CTL_RC6_ENABLE;
3402
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03003403 /* We don't use those on Haswell */
3404 if (!IS_HASWELL(dev)) {
3405 if (rc6_mode & INTEL_RC6p_ENABLE)
3406 rc6_mask |= GEN6_RC_CTL_RC6p_ENABLE;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003407
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03003408 if (rc6_mode & INTEL_RC6pp_ENABLE)
3409 rc6_mask |= GEN6_RC_CTL_RC6pp_ENABLE;
3410 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003411
Ben Widawskydc39fff2013-10-18 12:32:07 -07003412 intel_print_rc6_info(dev, rc6_mask);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003413
3414 I915_WRITE(GEN6_RC_CONTROL,
3415 rc6_mask |
3416 GEN6_RC_CTL_EI_MODE(1) |
3417 GEN6_RC_CTL_HW_ENABLE);
3418
Chris Wilsondd75fdc2013-09-25 17:34:57 +01003419 /* Power down if completely idle for over 50ms */
3420 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 50000);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003421 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003422
Ben Widawsky42c05262012-09-26 10:34:00 -07003423 ret = sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_MIN_FREQ_TABLE, 0);
Ben Widawskyd060c162014-03-19 18:31:08 -07003424 if (ret)
Ben Widawsky42c05262012-09-26 10:34:00 -07003425 DRM_DEBUG_DRIVER("Failed to set the min frequency\n");
Ben Widawskyd060c162014-03-19 18:31:08 -07003426
3427 ret = sandybridge_pcode_read(dev_priv, GEN6_READ_OC_PARAMS, &pcu_mbox);
3428 if (!ret && (pcu_mbox & (1<<31))) { /* OC supported */
3429 DRM_DEBUG_DRIVER("Overclocking supported. Max: %dMHz, Overclock max: %dMHz\n",
Ben Widawskyb39fb292014-03-19 18:31:11 -07003430 (dev_priv->rps.max_freq_softlimit & 0xff) * 50,
Ben Widawskyd060c162014-03-19 18:31:08 -07003431 (pcu_mbox & 0xff) * 50);
Ben Widawskyb39fb292014-03-19 18:31:11 -07003432 dev_priv->rps.max_freq = pcu_mbox & 0xff;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003433 }
3434
Chris Wilsondd75fdc2013-09-25 17:34:57 +01003435 dev_priv->rps.power = HIGH_POWER; /* force a reset */
Ben Widawskyb39fb292014-03-19 18:31:11 -07003436 gen6_set_rps(dev_priv->dev, dev_priv->rps.min_freq_softlimit);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003437
Daniel Vetter44fc7d52013-07-12 22:43:27 +02003438 gen6_enable_rps_interrupts(dev);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003439
Ben Widawsky31643d52012-09-26 10:34:01 -07003440 rc6vids = 0;
3441 ret = sandybridge_pcode_read(dev_priv, GEN6_PCODE_READ_RC6VIDS, &rc6vids);
3442 if (IS_GEN6(dev) && ret) {
3443 DRM_DEBUG_DRIVER("Couldn't check for BIOS workaround\n");
3444 } else if (IS_GEN6(dev) && (GEN6_DECODE_RC6_VID(rc6vids & 0xff) < 450)) {
3445 DRM_DEBUG_DRIVER("You should update your BIOS. Correcting minimum rc6 voltage (%dmV->%dmV)\n",
3446 GEN6_DECODE_RC6_VID(rc6vids & 0xff), 450);
3447 rc6vids &= 0xffff00;
3448 rc6vids |= GEN6_ENCODE_RC6_VID(450);
3449 ret = sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_RC6VIDS, rc6vids);
3450 if (ret)
3451 DRM_ERROR("Couldn't fix incorrect rc6 voltage\n");
3452 }
3453
Deepak Sc8d9a592013-11-23 14:55:42 +05303454 gen6_gt_force_wake_put(dev_priv, FORCEWAKE_ALL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003455}
3456
Paulo Zanonic67a4702013-08-19 13:18:09 -03003457void gen6_update_ring_freq(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003458{
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02003459 struct drm_i915_private *dev_priv = dev->dev_private;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003460 int min_freq = 15;
Chris Wilson3ebecd02013-04-12 19:10:13 +01003461 unsigned int gpu_freq;
3462 unsigned int max_ia_freq, min_ring_freq;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003463 int scaling_factor = 180;
Ben Widawskyeda79642013-10-07 17:15:48 -03003464 struct cpufreq_policy *policy;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003465
Jesse Barnes4fc688c2012-11-02 11:14:01 -07003466 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02003467
Ben Widawskyeda79642013-10-07 17:15:48 -03003468 policy = cpufreq_cpu_get(0);
3469 if (policy) {
3470 max_ia_freq = policy->cpuinfo.max_freq;
3471 cpufreq_cpu_put(policy);
3472 } else {
3473 /*
3474 * Default to measured freq if none found, PCU will ensure we
3475 * don't go over
3476 */
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003477 max_ia_freq = tsc_khz;
Ben Widawskyeda79642013-10-07 17:15:48 -03003478 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003479
3480 /* Convert from kHz to MHz */
3481 max_ia_freq /= 1000;
3482
Ben Widawsky153b4b952013-10-22 22:05:09 -07003483 min_ring_freq = I915_READ(DCLK) & 0xf;
Ben Widawskyf6aca452013-10-02 09:25:02 -07003484 /* convert DDR frequency from units of 266.6MHz to bandwidth */
3485 min_ring_freq = mult_frac(min_ring_freq, 8, 3);
Chris Wilson3ebecd02013-04-12 19:10:13 +01003486
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003487 /*
3488 * For each potential GPU frequency, load a ring frequency we'd like
3489 * to use for memory access. We do this by specifying the IA frequency
3490 * the PCU should use as a reference to determine the ring frequency.
3491 */
Paulo Zanoni4b28a1f2014-04-01 19:39:49 -03003492 for (gpu_freq = dev_priv->rps.max_freq_softlimit;
3493 gpu_freq >= dev_priv->rps.min_freq_softlimit && gpu_freq != 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003494 gpu_freq--) {
Ben Widawskyb39fb292014-03-19 18:31:11 -07003495 int diff = dev_priv->rps.max_freq_softlimit - gpu_freq;
Chris Wilson3ebecd02013-04-12 19:10:13 +01003496 unsigned int ia_freq = 0, ring_freq = 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003497
Ben Widawsky46c764d2013-11-02 21:07:49 -07003498 if (INTEL_INFO(dev)->gen >= 8) {
3499 /* max(2 * GT, DDR). NB: GT is 50MHz units */
3500 ring_freq = max(min_ring_freq, gpu_freq);
3501 } else if (IS_HASWELL(dev)) {
Ben Widawskyf6aca452013-10-02 09:25:02 -07003502 ring_freq = mult_frac(gpu_freq, 5, 4);
Chris Wilson3ebecd02013-04-12 19:10:13 +01003503 ring_freq = max(min_ring_freq, ring_freq);
3504 /* leave ia_freq as the default, chosen by cpufreq */
3505 } else {
3506 /* On older processors, there is no separate ring
3507 * clock domain, so in order to boost the bandwidth
3508 * of the ring, we need to upclock the CPU (ia_freq).
3509 *
3510 * For GPU frequencies less than 750MHz,
3511 * just use the lowest ring freq.
3512 */
3513 if (gpu_freq < min_freq)
3514 ia_freq = 800;
3515 else
3516 ia_freq = max_ia_freq - ((diff * scaling_factor) / 2);
3517 ia_freq = DIV_ROUND_CLOSEST(ia_freq, 100);
3518 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003519
Ben Widawsky42c05262012-09-26 10:34:00 -07003520 sandybridge_pcode_write(dev_priv,
3521 GEN6_PCODE_WRITE_MIN_FREQ_TABLE,
Chris Wilson3ebecd02013-04-12 19:10:13 +01003522 ia_freq << GEN6_PCODE_FREQ_IA_RATIO_SHIFT |
3523 ring_freq << GEN6_PCODE_FREQ_RING_RATIO_SHIFT |
3524 gpu_freq);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003525 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003526}
3527
Jesse Barnes0a073b82013-04-17 15:54:58 -07003528int valleyview_rps_max_freq(struct drm_i915_private *dev_priv)
3529{
3530 u32 val, rp0;
3531
Jani Nikula64936252013-05-22 15:36:20 +03003532 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FREQ_FUSE);
Jesse Barnes0a073b82013-04-17 15:54:58 -07003533
3534 rp0 = (val & FB_GFX_MAX_FREQ_FUSE_MASK) >> FB_GFX_MAX_FREQ_FUSE_SHIFT;
3535 /* Clamp to max */
3536 rp0 = min_t(u32, rp0, 0xea);
3537
3538 return rp0;
3539}
3540
3541static int valleyview_rps_rpe_freq(struct drm_i915_private *dev_priv)
3542{
3543 u32 val, rpe;
3544
Jani Nikula64936252013-05-22 15:36:20 +03003545 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FMAX_FUSE_LO);
Jesse Barnes0a073b82013-04-17 15:54:58 -07003546 rpe = (val & FB_FMAX_VMIN_FREQ_LO_MASK) >> FB_FMAX_VMIN_FREQ_LO_SHIFT;
Jani Nikula64936252013-05-22 15:36:20 +03003547 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FMAX_FUSE_HI);
Jesse Barnes0a073b82013-04-17 15:54:58 -07003548 rpe |= (val & FB_FMAX_VMIN_FREQ_HI_MASK) << 5;
3549
3550 return rpe;
3551}
3552
3553int valleyview_rps_min_freq(struct drm_i915_private *dev_priv)
3554{
Jani Nikula64936252013-05-22 15:36:20 +03003555 return vlv_punit_read(dev_priv, PUNIT_REG_GPU_LFM) & 0xff;
Jesse Barnes0a073b82013-04-17 15:54:58 -07003556}
3557
Imre Deakae484342014-03-31 15:10:44 +03003558/* Check that the pctx buffer wasn't move under us. */
3559static void valleyview_check_pctx(struct drm_i915_private *dev_priv)
3560{
3561 unsigned long pctx_addr = I915_READ(VLV_PCBR) & ~4095;
3562
3563 WARN_ON(pctx_addr != dev_priv->mm.stolen_base +
3564 dev_priv->vlv_pctx->stolen->start);
3565}
3566
Jesse Barnesc9cddff2013-05-08 10:45:13 -07003567static void valleyview_setup_pctx(struct drm_device *dev)
3568{
3569 struct drm_i915_private *dev_priv = dev->dev_private;
3570 struct drm_i915_gem_object *pctx;
3571 unsigned long pctx_paddr;
3572 u32 pcbr;
3573 int pctx_size = 24*1024;
3574
Imre Deak17b0c1f2014-02-11 21:39:06 +02003575 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
3576
Jesse Barnesc9cddff2013-05-08 10:45:13 -07003577 pcbr = I915_READ(VLV_PCBR);
3578 if (pcbr) {
3579 /* BIOS set it up already, grab the pre-alloc'd space */
3580 int pcbr_offset;
3581
3582 pcbr_offset = (pcbr & (~4095)) - dev_priv->mm.stolen_base;
3583 pctx = i915_gem_object_create_stolen_for_preallocated(dev_priv->dev,
3584 pcbr_offset,
Daniel Vetter190d6cd2013-07-04 13:06:28 +02003585 I915_GTT_OFFSET_NONE,
Jesse Barnesc9cddff2013-05-08 10:45:13 -07003586 pctx_size);
3587 goto out;
3588 }
3589
3590 /*
3591 * From the Gunit register HAS:
3592 * The Gfx driver is expected to program this register and ensure
3593 * proper allocation within Gfx stolen memory. For example, this
3594 * register should be programmed such than the PCBR range does not
3595 * overlap with other ranges, such as the frame buffer, protected
3596 * memory, or any other relevant ranges.
3597 */
3598 pctx = i915_gem_object_create_stolen(dev, pctx_size);
3599 if (!pctx) {
3600 DRM_DEBUG("not enough stolen space for PCTX, disabling\n");
3601 return;
3602 }
3603
3604 pctx_paddr = dev_priv->mm.stolen_base + pctx->stolen->start;
3605 I915_WRITE(VLV_PCBR, pctx_paddr);
3606
3607out:
3608 dev_priv->vlv_pctx = pctx;
3609}
3610
Imre Deakae484342014-03-31 15:10:44 +03003611static void valleyview_cleanup_pctx(struct drm_device *dev)
3612{
3613 struct drm_i915_private *dev_priv = dev->dev_private;
3614
3615 if (WARN_ON(!dev_priv->vlv_pctx))
3616 return;
3617
3618 drm_gem_object_unreference(&dev_priv->vlv_pctx->base);
3619 dev_priv->vlv_pctx = NULL;
3620}
3621
Jesse Barnes0a073b82013-04-17 15:54:58 -07003622static void valleyview_enable_rps(struct drm_device *dev)
3623{
3624 struct drm_i915_private *dev_priv = dev->dev_private;
3625 struct intel_ring_buffer *ring;
Ben Widawsky2a5913a2014-03-19 18:31:13 -07003626 u32 gtfifodbg, val, rc6_mode = 0;
Jesse Barnes0a073b82013-04-17 15:54:58 -07003627 int i;
3628
3629 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
3630
Imre Deakae484342014-03-31 15:10:44 +03003631 valleyview_check_pctx(dev_priv);
3632
Jesse Barnes0a073b82013-04-17 15:54:58 -07003633 if ((gtfifodbg = I915_READ(GTFIFODBG))) {
Jesse Barnesf7d85c12013-09-27 10:40:54 -07003634 DRM_DEBUG_DRIVER("GT fifo had a previous error %x\n",
3635 gtfifodbg);
Jesse Barnes0a073b82013-04-17 15:54:58 -07003636 I915_WRITE(GTFIFODBG, gtfifodbg);
3637 }
3638
Deepak Sc8d9a592013-11-23 14:55:42 +05303639 /* If VLV, Forcewake all wells, else re-direct to regular path */
3640 gen6_gt_force_wake_get(dev_priv, FORCEWAKE_ALL);
Jesse Barnes0a073b82013-04-17 15:54:58 -07003641
3642 I915_WRITE(GEN6_RP_UP_THRESHOLD, 59400);
3643 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 245000);
3644 I915_WRITE(GEN6_RP_UP_EI, 66000);
3645 I915_WRITE(GEN6_RP_DOWN_EI, 350000);
3646
3647 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
3648
3649 I915_WRITE(GEN6_RP_CONTROL,
3650 GEN6_RP_MEDIA_TURBO |
3651 GEN6_RP_MEDIA_HW_NORMAL_MODE |
3652 GEN6_RP_MEDIA_IS_GFX |
3653 GEN6_RP_ENABLE |
3654 GEN6_RP_UP_BUSY_AVG |
3655 GEN6_RP_DOWN_IDLE_CONT);
3656
3657 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 0x00280000);
3658 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
3659 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
3660
3661 for_each_ring(ring, dev_priv, i)
3662 I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
3663
Jesse Barnes2f0aa3042013-11-15 09:32:11 -08003664 I915_WRITE(GEN6_RC6_THRESHOLD, 0x557);
Jesse Barnes0a073b82013-04-17 15:54:58 -07003665
3666 /* allows RC6 residency counter to work */
Jesse Barnes49798eb2013-09-26 17:55:57 -07003667 I915_WRITE(VLV_COUNTER_CONTROL,
3668 _MASKED_BIT_ENABLE(VLV_COUNT_RANGE_HIGH |
3669 VLV_MEDIA_RC6_COUNT_EN |
3670 VLV_RENDER_RC6_COUNT_EN));
Jesse Barnesa2b23fe2013-09-19 09:33:13 -07003671 if (intel_enable_rc6(dev) & INTEL_RC6_ENABLE)
Jesse Barnes6b88f292013-11-15 09:32:12 -08003672 rc6_mode = GEN7_RC_CTL_TO_MODE | VLV_RC_CTL_CTX_RST_PARALLEL;
Ben Widawskydc39fff2013-10-18 12:32:07 -07003673
3674 intel_print_rc6_info(dev, rc6_mode);
3675
Jesse Barnesa2b23fe2013-09-19 09:33:13 -07003676 I915_WRITE(GEN6_RC_CONTROL, rc6_mode);
Jesse Barnes0a073b82013-04-17 15:54:58 -07003677
Jani Nikula64936252013-05-22 15:36:20 +03003678 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
Jesse Barnes0a073b82013-04-17 15:54:58 -07003679
3680 DRM_DEBUG_DRIVER("GPLL enabled? %s\n", val & 0x10 ? "yes" : "no");
3681 DRM_DEBUG_DRIVER("GPU status: 0x%08x\n", val);
3682
Ben Widawskyb39fb292014-03-19 18:31:11 -07003683 dev_priv->rps.cur_freq = (val >> 8) & 0xff;
Ville Syrjälä73008b92013-06-25 19:21:01 +03003684 DRM_DEBUG_DRIVER("current GPU freq: %d MHz (%u)\n",
Ben Widawskyb39fb292014-03-19 18:31:11 -07003685 vlv_gpu_freq(dev_priv, dev_priv->rps.cur_freq),
3686 dev_priv->rps.cur_freq);
Jesse Barnes0a073b82013-04-17 15:54:58 -07003687
Ben Widawsky2a5913a2014-03-19 18:31:13 -07003688 dev_priv->rps.max_freq = valleyview_rps_max_freq(dev_priv);
3689 dev_priv->rps.rp0_freq = dev_priv->rps.max_freq;
Ville Syrjälä73008b92013-06-25 19:21:01 +03003690 DRM_DEBUG_DRIVER("max GPU freq: %d MHz (%u)\n",
Ben Widawsky2a5913a2014-03-19 18:31:13 -07003691 vlv_gpu_freq(dev_priv, dev_priv->rps.max_freq),
3692 dev_priv->rps.max_freq);
Jesse Barnes0a073b82013-04-17 15:54:58 -07003693
Ben Widawskyb39fb292014-03-19 18:31:11 -07003694 dev_priv->rps.efficient_freq = valleyview_rps_rpe_freq(dev_priv);
Ville Syrjälä73008b92013-06-25 19:21:01 +03003695 DRM_DEBUG_DRIVER("RPe GPU freq: %d MHz (%u)\n",
Ben Widawskyb39fb292014-03-19 18:31:11 -07003696 vlv_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
3697 dev_priv->rps.efficient_freq);
Jesse Barnes0a073b82013-04-17 15:54:58 -07003698
Ben Widawsky2a5913a2014-03-19 18:31:13 -07003699 dev_priv->rps.min_freq = valleyview_rps_min_freq(dev_priv);
Ville Syrjälä73008b92013-06-25 19:21:01 +03003700 DRM_DEBUG_DRIVER("min GPU freq: %d MHz (%u)\n",
Ben Widawsky2a5913a2014-03-19 18:31:13 -07003701 vlv_gpu_freq(dev_priv, dev_priv->rps.min_freq),
3702 dev_priv->rps.min_freq);
Jeff McGeedd0a1aa2014-02-04 11:32:31 -06003703
3704 /* Preserve min/max settings in case of re-init */
Ben Widawskyb39fb292014-03-19 18:31:11 -07003705 if (dev_priv->rps.max_freq_softlimit == 0)
Ben Widawsky2a5913a2014-03-19 18:31:13 -07003706 dev_priv->rps.max_freq_softlimit = dev_priv->rps.max_freq;
Jeff McGeedd0a1aa2014-02-04 11:32:31 -06003707
Ben Widawskyb39fb292014-03-19 18:31:11 -07003708 if (dev_priv->rps.min_freq_softlimit == 0)
Ben Widawsky2a5913a2014-03-19 18:31:13 -07003709 dev_priv->rps.min_freq_softlimit = dev_priv->rps.min_freq;
Jesse Barnes0a073b82013-04-17 15:54:58 -07003710
Ville Syrjälä73008b92013-06-25 19:21:01 +03003711 DRM_DEBUG_DRIVER("setting GPU freq to %d MHz (%u)\n",
Ben Widawskyb39fb292014-03-19 18:31:11 -07003712 vlv_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
3713 dev_priv->rps.efficient_freq);
Jesse Barnes0a073b82013-04-17 15:54:58 -07003714
Ben Widawskyb39fb292014-03-19 18:31:11 -07003715 valleyview_set_rps(dev_priv->dev, dev_priv->rps.efficient_freq);
Jesse Barnes0a073b82013-04-17 15:54:58 -07003716
Daniel Vetter44fc7d52013-07-12 22:43:27 +02003717 gen6_enable_rps_interrupts(dev);
Jesse Barnes0a073b82013-04-17 15:54:58 -07003718
Deepak Sc8d9a592013-11-23 14:55:42 +05303719 gen6_gt_force_wake_put(dev_priv, FORCEWAKE_ALL);
Jesse Barnes0a073b82013-04-17 15:54:58 -07003720}
3721
Daniel Vetter930ebb42012-06-29 23:32:16 +02003722void ironlake_teardown_rc6(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003723{
3724 struct drm_i915_private *dev_priv = dev->dev_private;
3725
Daniel Vetter3e373942012-11-02 19:55:04 +01003726 if (dev_priv->ips.renderctx) {
Ben Widawskyd7f46fc2013-12-06 14:10:55 -08003727 i915_gem_object_ggtt_unpin(dev_priv->ips.renderctx);
Daniel Vetter3e373942012-11-02 19:55:04 +01003728 drm_gem_object_unreference(&dev_priv->ips.renderctx->base);
3729 dev_priv->ips.renderctx = NULL;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003730 }
3731
Daniel Vetter3e373942012-11-02 19:55:04 +01003732 if (dev_priv->ips.pwrctx) {
Ben Widawskyd7f46fc2013-12-06 14:10:55 -08003733 i915_gem_object_ggtt_unpin(dev_priv->ips.pwrctx);
Daniel Vetter3e373942012-11-02 19:55:04 +01003734 drm_gem_object_unreference(&dev_priv->ips.pwrctx->base);
3735 dev_priv->ips.pwrctx = NULL;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003736 }
3737}
3738
Daniel Vetter930ebb42012-06-29 23:32:16 +02003739static void ironlake_disable_rc6(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003740{
3741 struct drm_i915_private *dev_priv = dev->dev_private;
3742
3743 if (I915_READ(PWRCTXA)) {
3744 /* Wake the GPU, prevent RC6, then restore RSTDBYCTL */
3745 I915_WRITE(RSTDBYCTL, I915_READ(RSTDBYCTL) | RCX_SW_EXIT);
3746 wait_for(((I915_READ(RSTDBYCTL) & RSX_STATUS_MASK) == RSX_STATUS_ON),
3747 50);
3748
3749 I915_WRITE(PWRCTXA, 0);
3750 POSTING_READ(PWRCTXA);
3751
3752 I915_WRITE(RSTDBYCTL, I915_READ(RSTDBYCTL) & ~RCX_SW_EXIT);
3753 POSTING_READ(RSTDBYCTL);
3754 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003755}
3756
3757static int ironlake_setup_rc6(struct drm_device *dev)
3758{
3759 struct drm_i915_private *dev_priv = dev->dev_private;
3760
Daniel Vetter3e373942012-11-02 19:55:04 +01003761 if (dev_priv->ips.renderctx == NULL)
3762 dev_priv->ips.renderctx = intel_alloc_context_page(dev);
3763 if (!dev_priv->ips.renderctx)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003764 return -ENOMEM;
3765
Daniel Vetter3e373942012-11-02 19:55:04 +01003766 if (dev_priv->ips.pwrctx == NULL)
3767 dev_priv->ips.pwrctx = intel_alloc_context_page(dev);
3768 if (!dev_priv->ips.pwrctx) {
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003769 ironlake_teardown_rc6(dev);
3770 return -ENOMEM;
3771 }
3772
3773 return 0;
3774}
3775
Daniel Vetter930ebb42012-06-29 23:32:16 +02003776static void ironlake_enable_rc6(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003777{
3778 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter6d90c952012-04-26 23:28:05 +02003779 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Chris Wilson3e960502012-11-27 16:22:54 +00003780 bool was_interruptible;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003781 int ret;
3782
3783 /* rc6 disabled by default due to repeated reports of hanging during
3784 * boot and resume.
3785 */
3786 if (!intel_enable_rc6(dev))
3787 return;
3788
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02003789 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
3790
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003791 ret = ironlake_setup_rc6(dev);
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02003792 if (ret)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003793 return;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003794
Chris Wilson3e960502012-11-27 16:22:54 +00003795 was_interruptible = dev_priv->mm.interruptible;
3796 dev_priv->mm.interruptible = false;
3797
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003798 /*
3799 * GPU can automatically power down the render unit if given a page
3800 * to save state.
3801 */
Daniel Vetter6d90c952012-04-26 23:28:05 +02003802 ret = intel_ring_begin(ring, 6);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003803 if (ret) {
3804 ironlake_teardown_rc6(dev);
Chris Wilson3e960502012-11-27 16:22:54 +00003805 dev_priv->mm.interruptible = was_interruptible;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003806 return;
3807 }
3808
Daniel Vetter6d90c952012-04-26 23:28:05 +02003809 intel_ring_emit(ring, MI_SUSPEND_FLUSH | MI_SUSPEND_FLUSH_EN);
3810 intel_ring_emit(ring, MI_SET_CONTEXT);
Ben Widawskyf343c5f2013-07-05 14:41:04 -07003811 intel_ring_emit(ring, i915_gem_obj_ggtt_offset(dev_priv->ips.renderctx) |
Daniel Vetter6d90c952012-04-26 23:28:05 +02003812 MI_MM_SPACE_GTT |
3813 MI_SAVE_EXT_STATE_EN |
3814 MI_RESTORE_EXT_STATE_EN |
3815 MI_RESTORE_INHIBIT);
3816 intel_ring_emit(ring, MI_SUSPEND_FLUSH);
3817 intel_ring_emit(ring, MI_NOOP);
3818 intel_ring_emit(ring, MI_FLUSH);
3819 intel_ring_advance(ring);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003820
3821 /*
3822 * Wait for the command parser to advance past MI_SET_CONTEXT. The HW
3823 * does an implicit flush, combined with MI_FLUSH above, it should be
3824 * safe to assume that renderctx is valid
3825 */
Chris Wilson3e960502012-11-27 16:22:54 +00003826 ret = intel_ring_idle(ring);
3827 dev_priv->mm.interruptible = was_interruptible;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003828 if (ret) {
Jani Nikuladef27a52013-03-12 10:49:19 +02003829 DRM_ERROR("failed to enable ironlake power savings\n");
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003830 ironlake_teardown_rc6(dev);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003831 return;
3832 }
3833
Ben Widawskyf343c5f2013-07-05 14:41:04 -07003834 I915_WRITE(PWRCTXA, i915_gem_obj_ggtt_offset(dev_priv->ips.pwrctx) | PWRCTX_EN);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003835 I915_WRITE(RSTDBYCTL, I915_READ(RSTDBYCTL) & ~RCX_SW_EXIT);
Ben Widawskydc39fff2013-10-18 12:32:07 -07003836
3837 intel_print_rc6_info(dev, INTEL_RC6_ENABLE);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003838}
3839
Eugeni Dodonovdde18882012-04-18 15:29:24 -03003840static unsigned long intel_pxfreq(u32 vidfreq)
3841{
3842 unsigned long freq;
3843 int div = (vidfreq & 0x3f0000) >> 16;
3844 int post = (vidfreq & 0x3000) >> 12;
3845 int pre = (vidfreq & 0x7);
3846
3847 if (!pre)
3848 return 0;
3849
3850 freq = ((div * 133333) / ((1<<post) * pre));
3851
3852 return freq;
3853}
3854
Daniel Vettereb48eb02012-04-26 23:28:12 +02003855static const struct cparams {
3856 u16 i;
3857 u16 t;
3858 u16 m;
3859 u16 c;
3860} cparams[] = {
3861 { 1, 1333, 301, 28664 },
3862 { 1, 1066, 294, 24460 },
3863 { 1, 800, 294, 25192 },
3864 { 0, 1333, 276, 27605 },
3865 { 0, 1066, 276, 27605 },
3866 { 0, 800, 231, 23784 },
3867};
3868
Chris Wilsonf531dcb22012-09-25 10:16:12 +01003869static unsigned long __i915_chipset_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02003870{
3871 u64 total_count, diff, ret;
3872 u32 count1, count2, count3, m = 0, c = 0;
3873 unsigned long now = jiffies_to_msecs(jiffies), diff1;
3874 int i;
3875
Daniel Vetter02d71952012-08-09 16:44:54 +02003876 assert_spin_locked(&mchdev_lock);
3877
Daniel Vetter20e4d402012-08-08 23:35:39 +02003878 diff1 = now - dev_priv->ips.last_time1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02003879
3880 /* Prevent division-by-zero if we are asking too fast.
3881 * Also, we don't get interesting results if we are polling
3882 * faster than once in 10ms, so just return the saved value
3883 * in such cases.
3884 */
3885 if (diff1 <= 10)
Daniel Vetter20e4d402012-08-08 23:35:39 +02003886 return dev_priv->ips.chipset_power;
Daniel Vettereb48eb02012-04-26 23:28:12 +02003887
3888 count1 = I915_READ(DMIEC);
3889 count2 = I915_READ(DDREC);
3890 count3 = I915_READ(CSIEC);
3891
3892 total_count = count1 + count2 + count3;
3893
3894 /* FIXME: handle per-counter overflow */
Daniel Vetter20e4d402012-08-08 23:35:39 +02003895 if (total_count < dev_priv->ips.last_count1) {
3896 diff = ~0UL - dev_priv->ips.last_count1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02003897 diff += total_count;
3898 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +02003899 diff = total_count - dev_priv->ips.last_count1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02003900 }
3901
3902 for (i = 0; i < ARRAY_SIZE(cparams); i++) {
Daniel Vetter20e4d402012-08-08 23:35:39 +02003903 if (cparams[i].i == dev_priv->ips.c_m &&
3904 cparams[i].t == dev_priv->ips.r_t) {
Daniel Vettereb48eb02012-04-26 23:28:12 +02003905 m = cparams[i].m;
3906 c = cparams[i].c;
3907 break;
3908 }
3909 }
3910
3911 diff = div_u64(diff, diff1);
3912 ret = ((m * diff) + c);
3913 ret = div_u64(ret, 10);
3914
Daniel Vetter20e4d402012-08-08 23:35:39 +02003915 dev_priv->ips.last_count1 = total_count;
3916 dev_priv->ips.last_time1 = now;
Daniel Vettereb48eb02012-04-26 23:28:12 +02003917
Daniel Vetter20e4d402012-08-08 23:35:39 +02003918 dev_priv->ips.chipset_power = ret;
Daniel Vettereb48eb02012-04-26 23:28:12 +02003919
3920 return ret;
3921}
3922
Chris Wilsonf531dcb22012-09-25 10:16:12 +01003923unsigned long i915_chipset_val(struct drm_i915_private *dev_priv)
3924{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00003925 struct drm_device *dev = dev_priv->dev;
Chris Wilsonf531dcb22012-09-25 10:16:12 +01003926 unsigned long val;
3927
Damien Lespiau3d13ef22014-02-07 19:12:47 +00003928 if (INTEL_INFO(dev)->gen != 5)
Chris Wilsonf531dcb22012-09-25 10:16:12 +01003929 return 0;
3930
3931 spin_lock_irq(&mchdev_lock);
3932
3933 val = __i915_chipset_val(dev_priv);
3934
3935 spin_unlock_irq(&mchdev_lock);
3936
3937 return val;
3938}
3939
Daniel Vettereb48eb02012-04-26 23:28:12 +02003940unsigned long i915_mch_val(struct drm_i915_private *dev_priv)
3941{
3942 unsigned long m, x, b;
3943 u32 tsfs;
3944
3945 tsfs = I915_READ(TSFS);
3946
3947 m = ((tsfs & TSFS_SLOPE_MASK) >> TSFS_SLOPE_SHIFT);
3948 x = I915_READ8(TR1);
3949
3950 b = tsfs & TSFS_INTR_MASK;
3951
3952 return ((m * x) / 127) - b;
3953}
3954
3955static u16 pvid_to_extvid(struct drm_i915_private *dev_priv, u8 pxvid)
3956{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00003957 struct drm_device *dev = dev_priv->dev;
Daniel Vettereb48eb02012-04-26 23:28:12 +02003958 static const struct v_table {
3959 u16 vd; /* in .1 mil */
3960 u16 vm; /* in .1 mil */
3961 } v_table[] = {
3962 { 0, 0, },
3963 { 375, 0, },
3964 { 500, 0, },
3965 { 625, 0, },
3966 { 750, 0, },
3967 { 875, 0, },
3968 { 1000, 0, },
3969 { 1125, 0, },
3970 { 4125, 3000, },
3971 { 4125, 3000, },
3972 { 4125, 3000, },
3973 { 4125, 3000, },
3974 { 4125, 3000, },
3975 { 4125, 3000, },
3976 { 4125, 3000, },
3977 { 4125, 3000, },
3978 { 4125, 3000, },
3979 { 4125, 3000, },
3980 { 4125, 3000, },
3981 { 4125, 3000, },
3982 { 4125, 3000, },
3983 { 4125, 3000, },
3984 { 4125, 3000, },
3985 { 4125, 3000, },
3986 { 4125, 3000, },
3987 { 4125, 3000, },
3988 { 4125, 3000, },
3989 { 4125, 3000, },
3990 { 4125, 3000, },
3991 { 4125, 3000, },
3992 { 4125, 3000, },
3993 { 4125, 3000, },
3994 { 4250, 3125, },
3995 { 4375, 3250, },
3996 { 4500, 3375, },
3997 { 4625, 3500, },
3998 { 4750, 3625, },
3999 { 4875, 3750, },
4000 { 5000, 3875, },
4001 { 5125, 4000, },
4002 { 5250, 4125, },
4003 { 5375, 4250, },
4004 { 5500, 4375, },
4005 { 5625, 4500, },
4006 { 5750, 4625, },
4007 { 5875, 4750, },
4008 { 6000, 4875, },
4009 { 6125, 5000, },
4010 { 6250, 5125, },
4011 { 6375, 5250, },
4012 { 6500, 5375, },
4013 { 6625, 5500, },
4014 { 6750, 5625, },
4015 { 6875, 5750, },
4016 { 7000, 5875, },
4017 { 7125, 6000, },
4018 { 7250, 6125, },
4019 { 7375, 6250, },
4020 { 7500, 6375, },
4021 { 7625, 6500, },
4022 { 7750, 6625, },
4023 { 7875, 6750, },
4024 { 8000, 6875, },
4025 { 8125, 7000, },
4026 { 8250, 7125, },
4027 { 8375, 7250, },
4028 { 8500, 7375, },
4029 { 8625, 7500, },
4030 { 8750, 7625, },
4031 { 8875, 7750, },
4032 { 9000, 7875, },
4033 { 9125, 8000, },
4034 { 9250, 8125, },
4035 { 9375, 8250, },
4036 { 9500, 8375, },
4037 { 9625, 8500, },
4038 { 9750, 8625, },
4039 { 9875, 8750, },
4040 { 10000, 8875, },
4041 { 10125, 9000, },
4042 { 10250, 9125, },
4043 { 10375, 9250, },
4044 { 10500, 9375, },
4045 { 10625, 9500, },
4046 { 10750, 9625, },
4047 { 10875, 9750, },
4048 { 11000, 9875, },
4049 { 11125, 10000, },
4050 { 11250, 10125, },
4051 { 11375, 10250, },
4052 { 11500, 10375, },
4053 { 11625, 10500, },
4054 { 11750, 10625, },
4055 { 11875, 10750, },
4056 { 12000, 10875, },
4057 { 12125, 11000, },
4058 { 12250, 11125, },
4059 { 12375, 11250, },
4060 { 12500, 11375, },
4061 { 12625, 11500, },
4062 { 12750, 11625, },
4063 { 12875, 11750, },
4064 { 13000, 11875, },
4065 { 13125, 12000, },
4066 { 13250, 12125, },
4067 { 13375, 12250, },
4068 { 13500, 12375, },
4069 { 13625, 12500, },
4070 { 13750, 12625, },
4071 { 13875, 12750, },
4072 { 14000, 12875, },
4073 { 14125, 13000, },
4074 { 14250, 13125, },
4075 { 14375, 13250, },
4076 { 14500, 13375, },
4077 { 14625, 13500, },
4078 { 14750, 13625, },
4079 { 14875, 13750, },
4080 { 15000, 13875, },
4081 { 15125, 14000, },
4082 { 15250, 14125, },
4083 { 15375, 14250, },
4084 { 15500, 14375, },
4085 { 15625, 14500, },
4086 { 15750, 14625, },
4087 { 15875, 14750, },
4088 { 16000, 14875, },
4089 { 16125, 15000, },
4090 };
Damien Lespiau3d13ef22014-02-07 19:12:47 +00004091 if (INTEL_INFO(dev)->is_mobile)
Daniel Vettereb48eb02012-04-26 23:28:12 +02004092 return v_table[pxvid].vm;
4093 else
4094 return v_table[pxvid].vd;
4095}
4096
Daniel Vetter02d71952012-08-09 16:44:54 +02004097static void __i915_update_gfx_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02004098{
4099 struct timespec now, diff1;
4100 u64 diff;
4101 unsigned long diffms;
4102 u32 count;
4103
Daniel Vetter02d71952012-08-09 16:44:54 +02004104 assert_spin_locked(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004105
4106 getrawmonotonic(&now);
Daniel Vetter20e4d402012-08-08 23:35:39 +02004107 diff1 = timespec_sub(now, dev_priv->ips.last_time2);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004108
4109 /* Don't divide by 0 */
4110 diffms = diff1.tv_sec * 1000 + diff1.tv_nsec / 1000000;
4111 if (!diffms)
4112 return;
4113
4114 count = I915_READ(GFXEC);
4115
Daniel Vetter20e4d402012-08-08 23:35:39 +02004116 if (count < dev_priv->ips.last_count2) {
4117 diff = ~0UL - dev_priv->ips.last_count2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02004118 diff += count;
4119 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +02004120 diff = count - dev_priv->ips.last_count2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02004121 }
4122
Daniel Vetter20e4d402012-08-08 23:35:39 +02004123 dev_priv->ips.last_count2 = count;
4124 dev_priv->ips.last_time2 = now;
Daniel Vettereb48eb02012-04-26 23:28:12 +02004125
4126 /* More magic constants... */
4127 diff = diff * 1181;
4128 diff = div_u64(diff, diffms * 10);
Daniel Vetter20e4d402012-08-08 23:35:39 +02004129 dev_priv->ips.gfx_power = diff;
Daniel Vettereb48eb02012-04-26 23:28:12 +02004130}
4131
Daniel Vetter02d71952012-08-09 16:44:54 +02004132void i915_update_gfx_val(struct drm_i915_private *dev_priv)
4133{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00004134 struct drm_device *dev = dev_priv->dev;
4135
4136 if (INTEL_INFO(dev)->gen != 5)
Daniel Vetter02d71952012-08-09 16:44:54 +02004137 return;
4138
Daniel Vetter92703882012-08-09 16:46:01 +02004139 spin_lock_irq(&mchdev_lock);
Daniel Vetter02d71952012-08-09 16:44:54 +02004140
4141 __i915_update_gfx_val(dev_priv);
4142
Daniel Vetter92703882012-08-09 16:46:01 +02004143 spin_unlock_irq(&mchdev_lock);
Daniel Vetter02d71952012-08-09 16:44:54 +02004144}
4145
Chris Wilsonf531dcb22012-09-25 10:16:12 +01004146static unsigned long __i915_gfx_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02004147{
4148 unsigned long t, corr, state1, corr2, state2;
4149 u32 pxvid, ext_v;
4150
Daniel Vetter02d71952012-08-09 16:44:54 +02004151 assert_spin_locked(&mchdev_lock);
4152
Ben Widawskyb39fb292014-03-19 18:31:11 -07004153 pxvid = I915_READ(PXVFREQ_BASE + (dev_priv->rps.cur_freq * 4));
Daniel Vettereb48eb02012-04-26 23:28:12 +02004154 pxvid = (pxvid >> 24) & 0x7f;
4155 ext_v = pvid_to_extvid(dev_priv, pxvid);
4156
4157 state1 = ext_v;
4158
4159 t = i915_mch_val(dev_priv);
4160
4161 /* Revel in the empirically derived constants */
4162
4163 /* Correction factor in 1/100000 units */
4164 if (t > 80)
4165 corr = ((t * 2349) + 135940);
4166 else if (t >= 50)
4167 corr = ((t * 964) + 29317);
4168 else /* < 50 */
4169 corr = ((t * 301) + 1004);
4170
4171 corr = corr * ((150142 * state1) / 10000 - 78642);
4172 corr /= 100000;
Daniel Vetter20e4d402012-08-08 23:35:39 +02004173 corr2 = (corr * dev_priv->ips.corr);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004174
4175 state2 = (corr2 * state1) / 10000;
4176 state2 /= 100; /* convert to mW */
4177
Daniel Vetter02d71952012-08-09 16:44:54 +02004178 __i915_update_gfx_val(dev_priv);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004179
Daniel Vetter20e4d402012-08-08 23:35:39 +02004180 return dev_priv->ips.gfx_power + state2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02004181}
4182
Chris Wilsonf531dcb22012-09-25 10:16:12 +01004183unsigned long i915_gfx_val(struct drm_i915_private *dev_priv)
4184{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00004185 struct drm_device *dev = dev_priv->dev;
Chris Wilsonf531dcb22012-09-25 10:16:12 +01004186 unsigned long val;
4187
Damien Lespiau3d13ef22014-02-07 19:12:47 +00004188 if (INTEL_INFO(dev)->gen != 5)
Chris Wilsonf531dcb22012-09-25 10:16:12 +01004189 return 0;
4190
4191 spin_lock_irq(&mchdev_lock);
4192
4193 val = __i915_gfx_val(dev_priv);
4194
4195 spin_unlock_irq(&mchdev_lock);
4196
4197 return val;
4198}
4199
Daniel Vettereb48eb02012-04-26 23:28:12 +02004200/**
4201 * i915_read_mch_val - return value for IPS use
4202 *
4203 * Calculate and return a value for the IPS driver to use when deciding whether
4204 * we have thermal and power headroom to increase CPU or GPU power budget.
4205 */
4206unsigned long i915_read_mch_val(void)
4207{
4208 struct drm_i915_private *dev_priv;
4209 unsigned long chipset_val, graphics_val, ret = 0;
4210
Daniel Vetter92703882012-08-09 16:46:01 +02004211 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004212 if (!i915_mch_dev)
4213 goto out_unlock;
4214 dev_priv = i915_mch_dev;
4215
Chris Wilsonf531dcb22012-09-25 10:16:12 +01004216 chipset_val = __i915_chipset_val(dev_priv);
4217 graphics_val = __i915_gfx_val(dev_priv);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004218
4219 ret = chipset_val + graphics_val;
4220
4221out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02004222 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004223
4224 return ret;
4225}
4226EXPORT_SYMBOL_GPL(i915_read_mch_val);
4227
4228/**
4229 * i915_gpu_raise - raise GPU frequency limit
4230 *
4231 * Raise the limit; IPS indicates we have thermal headroom.
4232 */
4233bool i915_gpu_raise(void)
4234{
4235 struct drm_i915_private *dev_priv;
4236 bool ret = true;
4237
Daniel Vetter92703882012-08-09 16:46:01 +02004238 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004239 if (!i915_mch_dev) {
4240 ret = false;
4241 goto out_unlock;
4242 }
4243 dev_priv = i915_mch_dev;
4244
Daniel Vetter20e4d402012-08-08 23:35:39 +02004245 if (dev_priv->ips.max_delay > dev_priv->ips.fmax)
4246 dev_priv->ips.max_delay--;
Daniel Vettereb48eb02012-04-26 23:28:12 +02004247
4248out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02004249 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004250
4251 return ret;
4252}
4253EXPORT_SYMBOL_GPL(i915_gpu_raise);
4254
4255/**
4256 * i915_gpu_lower - lower GPU frequency limit
4257 *
4258 * IPS indicates we're close to a thermal limit, so throttle back the GPU
4259 * frequency maximum.
4260 */
4261bool i915_gpu_lower(void)
4262{
4263 struct drm_i915_private *dev_priv;
4264 bool ret = true;
4265
Daniel Vetter92703882012-08-09 16:46:01 +02004266 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004267 if (!i915_mch_dev) {
4268 ret = false;
4269 goto out_unlock;
4270 }
4271 dev_priv = i915_mch_dev;
4272
Daniel Vetter20e4d402012-08-08 23:35:39 +02004273 if (dev_priv->ips.max_delay < dev_priv->ips.min_delay)
4274 dev_priv->ips.max_delay++;
Daniel Vettereb48eb02012-04-26 23:28:12 +02004275
4276out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02004277 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004278
4279 return ret;
4280}
4281EXPORT_SYMBOL_GPL(i915_gpu_lower);
4282
4283/**
4284 * i915_gpu_busy - indicate GPU business to IPS
4285 *
4286 * Tell the IPS driver whether or not the GPU is busy.
4287 */
4288bool i915_gpu_busy(void)
4289{
4290 struct drm_i915_private *dev_priv;
Chris Wilsonf047e392012-07-21 12:31:41 +01004291 struct intel_ring_buffer *ring;
Daniel Vettereb48eb02012-04-26 23:28:12 +02004292 bool ret = false;
Chris Wilsonf047e392012-07-21 12:31:41 +01004293 int i;
Daniel Vettereb48eb02012-04-26 23:28:12 +02004294
Daniel Vetter92703882012-08-09 16:46:01 +02004295 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004296 if (!i915_mch_dev)
4297 goto out_unlock;
4298 dev_priv = i915_mch_dev;
4299
Chris Wilsonf047e392012-07-21 12:31:41 +01004300 for_each_ring(ring, dev_priv, i)
4301 ret |= !list_empty(&ring->request_list);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004302
4303out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02004304 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004305
4306 return ret;
4307}
4308EXPORT_SYMBOL_GPL(i915_gpu_busy);
4309
4310/**
4311 * i915_gpu_turbo_disable - disable graphics turbo
4312 *
4313 * Disable graphics turbo by resetting the max frequency and setting the
4314 * current frequency to the default.
4315 */
4316bool i915_gpu_turbo_disable(void)
4317{
4318 struct drm_i915_private *dev_priv;
4319 bool ret = true;
4320
Daniel Vetter92703882012-08-09 16:46:01 +02004321 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004322 if (!i915_mch_dev) {
4323 ret = false;
4324 goto out_unlock;
4325 }
4326 dev_priv = i915_mch_dev;
4327
Daniel Vetter20e4d402012-08-08 23:35:39 +02004328 dev_priv->ips.max_delay = dev_priv->ips.fstart;
Daniel Vettereb48eb02012-04-26 23:28:12 +02004329
Daniel Vetter20e4d402012-08-08 23:35:39 +02004330 if (!ironlake_set_drps(dev_priv->dev, dev_priv->ips.fstart))
Daniel Vettereb48eb02012-04-26 23:28:12 +02004331 ret = false;
4332
4333out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02004334 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004335
4336 return ret;
4337}
4338EXPORT_SYMBOL_GPL(i915_gpu_turbo_disable);
4339
4340/**
4341 * Tells the intel_ips driver that the i915 driver is now loaded, if
4342 * IPS got loaded first.
4343 *
4344 * This awkward dance is so that neither module has to depend on the
4345 * other in order for IPS to do the appropriate communication of
4346 * GPU turbo limits to i915.
4347 */
4348static void
4349ips_ping_for_i915_load(void)
4350{
4351 void (*link)(void);
4352
4353 link = symbol_get(ips_link_to_i915_driver);
4354 if (link) {
4355 link();
4356 symbol_put(ips_link_to_i915_driver);
4357 }
4358}
4359
4360void intel_gpu_ips_init(struct drm_i915_private *dev_priv)
4361{
Daniel Vetter02d71952012-08-09 16:44:54 +02004362 /* We only register the i915 ips part with intel-ips once everything is
4363 * set up, to avoid intel-ips sneaking in and reading bogus values. */
Daniel Vetter92703882012-08-09 16:46:01 +02004364 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004365 i915_mch_dev = dev_priv;
Daniel Vetter92703882012-08-09 16:46:01 +02004366 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004367
4368 ips_ping_for_i915_load();
4369}
4370
4371void intel_gpu_ips_teardown(void)
4372{
Daniel Vetter92703882012-08-09 16:46:01 +02004373 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004374 i915_mch_dev = NULL;
Daniel Vetter92703882012-08-09 16:46:01 +02004375 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004376}
Deepak S76c3552f2014-01-30 23:08:16 +05304377
Daniel Vetter8090c6b2012-06-24 16:42:32 +02004378static void intel_init_emon(struct drm_device *dev)
Eugeni Dodonovdde18882012-04-18 15:29:24 -03004379{
4380 struct drm_i915_private *dev_priv = dev->dev_private;
4381 u32 lcfuse;
4382 u8 pxw[16];
4383 int i;
4384
4385 /* Disable to program */
4386 I915_WRITE(ECR, 0);
4387 POSTING_READ(ECR);
4388
4389 /* Program energy weights for various events */
4390 I915_WRITE(SDEW, 0x15040d00);
4391 I915_WRITE(CSIEW0, 0x007f0000);
4392 I915_WRITE(CSIEW1, 0x1e220004);
4393 I915_WRITE(CSIEW2, 0x04000004);
4394
4395 for (i = 0; i < 5; i++)
4396 I915_WRITE(PEW + (i * 4), 0);
4397 for (i = 0; i < 3; i++)
4398 I915_WRITE(DEW + (i * 4), 0);
4399
4400 /* Program P-state weights to account for frequency power adjustment */
4401 for (i = 0; i < 16; i++) {
4402 u32 pxvidfreq = I915_READ(PXVFREQ_BASE + (i * 4));
4403 unsigned long freq = intel_pxfreq(pxvidfreq);
4404 unsigned long vid = (pxvidfreq & PXVFREQ_PX_MASK) >>
4405 PXVFREQ_PX_SHIFT;
4406 unsigned long val;
4407
4408 val = vid * vid;
4409 val *= (freq / 1000);
4410 val *= 255;
4411 val /= (127*127*900);
4412 if (val > 0xff)
4413 DRM_ERROR("bad pxval: %ld\n", val);
4414 pxw[i] = val;
4415 }
4416 /* Render standby states get 0 weight */
4417 pxw[14] = 0;
4418 pxw[15] = 0;
4419
4420 for (i = 0; i < 4; i++) {
4421 u32 val = (pxw[i*4] << 24) | (pxw[(i*4)+1] << 16) |
4422 (pxw[(i*4)+2] << 8) | (pxw[(i*4)+3]);
4423 I915_WRITE(PXW + (i * 4), val);
4424 }
4425
4426 /* Adjust magic regs to magic values (more experimental results) */
4427 I915_WRITE(OGW0, 0);
4428 I915_WRITE(OGW1, 0);
4429 I915_WRITE(EG0, 0x00007f00);
4430 I915_WRITE(EG1, 0x0000000e);
4431 I915_WRITE(EG2, 0x000e0000);
4432 I915_WRITE(EG3, 0x68000300);
4433 I915_WRITE(EG4, 0x42000000);
4434 I915_WRITE(EG5, 0x00140031);
4435 I915_WRITE(EG6, 0);
4436 I915_WRITE(EG7, 0);
4437
4438 for (i = 0; i < 8; i++)
4439 I915_WRITE(PXWL + (i * 4), 0);
4440
4441 /* Enable PMON + select events */
4442 I915_WRITE(ECR, 0x80000019);
4443
4444 lcfuse = I915_READ(LCFUSE02);
4445
Daniel Vetter20e4d402012-08-08 23:35:39 +02004446 dev_priv->ips.corr = (lcfuse & LCFUSE_HIV_MASK);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03004447}
4448
Imre Deakae484342014-03-31 15:10:44 +03004449void intel_init_gt_powersave(struct drm_device *dev)
4450{
4451 if (IS_VALLEYVIEW(dev))
4452 valleyview_setup_pctx(dev);
4453}
4454
4455void intel_cleanup_gt_powersave(struct drm_device *dev)
4456{
4457 if (IS_VALLEYVIEW(dev))
4458 valleyview_cleanup_pctx(dev);
4459}
4460
Daniel Vetter8090c6b2012-06-24 16:42:32 +02004461void intel_disable_gt_powersave(struct drm_device *dev)
4462{
Jesse Barnes1a01ab32012-11-02 11:14:00 -07004463 struct drm_i915_private *dev_priv = dev->dev_private;
4464
Daniel Vetterfd0c0642013-04-24 11:13:35 +02004465 /* Interrupts should be disabled already to avoid re-arming. */
4466 WARN_ON(dev->irq_enabled);
4467
Daniel Vetter930ebb42012-06-29 23:32:16 +02004468 if (IS_IRONLAKE_M(dev)) {
Daniel Vetter8090c6b2012-06-24 16:42:32 +02004469 ironlake_disable_drps(dev);
Daniel Vetter930ebb42012-06-29 23:32:16 +02004470 ironlake_disable_rc6(dev);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004471 } else if (INTEL_INFO(dev)->gen >= 6) {
Jesse Barnes1a01ab32012-11-02 11:14:00 -07004472 cancel_delayed_work_sync(&dev_priv->rps.delayed_resume_work);
Jesse Barnes250848c2013-04-23 10:09:27 -07004473 cancel_work_sync(&dev_priv->rps.work);
Jesse Barnes4fc688c2012-11-02 11:14:01 -07004474 mutex_lock(&dev_priv->rps.hw_lock);
Jesse Barnesd20d4f02013-04-23 10:09:28 -07004475 if (IS_VALLEYVIEW(dev))
4476 valleyview_disable_rps(dev);
4477 else
4478 gen6_disable_rps(dev);
Chris Wilsonc0951f02013-10-10 21:58:50 +01004479 dev_priv->rps.enabled = false;
Jesse Barnes4fc688c2012-11-02 11:14:01 -07004480 mutex_unlock(&dev_priv->rps.hw_lock);
Daniel Vetter930ebb42012-06-29 23:32:16 +02004481 }
Daniel Vetter8090c6b2012-06-24 16:42:32 +02004482}
4483
Jesse Barnes1a01ab32012-11-02 11:14:00 -07004484static void intel_gen6_powersave_work(struct work_struct *work)
4485{
4486 struct drm_i915_private *dev_priv =
4487 container_of(work, struct drm_i915_private,
4488 rps.delayed_resume_work.work);
4489 struct drm_device *dev = dev_priv->dev;
4490
Jesse Barnes4fc688c2012-11-02 11:14:01 -07004491 mutex_lock(&dev_priv->rps.hw_lock);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004492
4493 if (IS_VALLEYVIEW(dev)) {
4494 valleyview_enable_rps(dev);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004495 } else if (IS_BROADWELL(dev)) {
4496 gen8_enable_rps(dev);
4497 gen6_update_ring_freq(dev);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004498 } else {
4499 gen6_enable_rps(dev);
4500 gen6_update_ring_freq(dev);
4501 }
Chris Wilsonc0951f02013-10-10 21:58:50 +01004502 dev_priv->rps.enabled = true;
Jesse Barnes4fc688c2012-11-02 11:14:01 -07004503 mutex_unlock(&dev_priv->rps.hw_lock);
Jesse Barnes1a01ab32012-11-02 11:14:00 -07004504}
4505
Daniel Vetter8090c6b2012-06-24 16:42:32 +02004506void intel_enable_gt_powersave(struct drm_device *dev)
4507{
Jesse Barnes1a01ab32012-11-02 11:14:00 -07004508 struct drm_i915_private *dev_priv = dev->dev_private;
4509
Daniel Vetter8090c6b2012-06-24 16:42:32 +02004510 if (IS_IRONLAKE_M(dev)) {
4511 ironlake_enable_drps(dev);
4512 ironlake_enable_rc6(dev);
4513 intel_init_emon(dev);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004514 } else if (IS_GEN6(dev) || IS_GEN7(dev)) {
Jesse Barnes1a01ab32012-11-02 11:14:00 -07004515 /*
4516 * PCU communication is slow and this doesn't need to be
4517 * done at any specific time, so do this out of our fast path
4518 * to make resume and init faster.
4519 */
4520 schedule_delayed_work(&dev_priv->rps.delayed_resume_work,
4521 round_jiffies_up_relative(HZ));
Daniel Vetter8090c6b2012-06-24 16:42:32 +02004522 }
4523}
4524
Daniel Vetter3107bd42012-10-31 22:52:31 +01004525static void ibx_init_clock_gating(struct drm_device *dev)
4526{
4527 struct drm_i915_private *dev_priv = dev->dev_private;
4528
4529 /*
4530 * On Ibex Peak and Cougar Point, we need to disable clock
4531 * gating for the panel power sequencer or it will fail to
4532 * start up when no ports are active.
4533 */
4534 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE);
4535}
4536
Ville Syrjälä0e088b82013-06-07 10:47:04 +03004537static void g4x_disable_trickle_feed(struct drm_device *dev)
4538{
4539 struct drm_i915_private *dev_priv = dev->dev_private;
4540 int pipe;
4541
4542 for_each_pipe(pipe) {
4543 I915_WRITE(DSPCNTR(pipe),
4544 I915_READ(DSPCNTR(pipe)) |
4545 DISPPLANE_TRICKLE_FEED_DISABLE);
Ville Syrjälä1dba99f2013-10-01 18:02:18 +03004546 intel_flush_primary_plane(dev_priv, pipe);
Ville Syrjälä0e088b82013-06-07 10:47:04 +03004547 }
4548}
4549
Ville Syrjälä017636c2013-12-05 15:51:37 +02004550static void ilk_init_lp_watermarks(struct drm_device *dev)
4551{
4552 struct drm_i915_private *dev_priv = dev->dev_private;
4553
4554 I915_WRITE(WM3_LP_ILK, I915_READ(WM3_LP_ILK) & ~WM1_LP_SR_EN);
4555 I915_WRITE(WM2_LP_ILK, I915_READ(WM2_LP_ILK) & ~WM1_LP_SR_EN);
4556 I915_WRITE(WM1_LP_ILK, I915_READ(WM1_LP_ILK) & ~WM1_LP_SR_EN);
4557
4558 /*
4559 * Don't touch WM1S_LP_EN here.
4560 * Doing so could cause underruns.
4561 */
4562}
4563
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03004564static void ironlake_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004565{
4566 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau231e54f2012-10-19 17:55:41 +01004567 uint32_t dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004568
Damien Lespiauf1e8fa52013-06-07 17:41:09 +01004569 /*
4570 * Required for FBC
4571 * WaFbcDisableDpfcClockGating:ilk
4572 */
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01004573 dspclk_gate |= ILK_DPFCRUNIT_CLOCK_GATE_DISABLE |
4574 ILK_DPFCUNIT_CLOCK_GATE_DISABLE |
4575 ILK_DPFDUNIT_CLOCK_GATE_ENABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004576
4577 I915_WRITE(PCH_3DCGDIS0,
4578 MARIUNIT_CLOCK_GATE_DISABLE |
4579 SVSMUNIT_CLOCK_GATE_DISABLE);
4580 I915_WRITE(PCH_3DCGDIS1,
4581 VFMUNIT_CLOCK_GATE_DISABLE);
4582
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004583 /*
4584 * According to the spec the following bits should be set in
4585 * order to enable memory self-refresh
4586 * The bit 22/21 of 0x42004
4587 * The bit 5 of 0x42020
4588 * The bit 15 of 0x45000
4589 */
4590 I915_WRITE(ILK_DISPLAY_CHICKEN2,
4591 (I915_READ(ILK_DISPLAY_CHICKEN2) |
4592 ILK_DPARB_GATE | ILK_VSDPFD_FULL));
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01004593 dspclk_gate |= ILK_DPARBUNIT_CLOCK_GATE_ENABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004594 I915_WRITE(DISP_ARB_CTL,
4595 (I915_READ(DISP_ARB_CTL) |
4596 DISP_FBC_WM_DIS));
Ville Syrjälä017636c2013-12-05 15:51:37 +02004597
4598 ilk_init_lp_watermarks(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004599
4600 /*
4601 * Based on the document from hardware guys the following bits
4602 * should be set unconditionally in order to enable FBC.
4603 * The bit 22 of 0x42000
4604 * The bit 22 of 0x42004
4605 * The bit 7,8,9 of 0x42020.
4606 */
4607 if (IS_IRONLAKE_M(dev)) {
Damien Lespiau4bb35332013-06-14 15:23:24 +01004608 /* WaFbcAsynchFlipDisableFbcQueue:ilk */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004609 I915_WRITE(ILK_DISPLAY_CHICKEN1,
4610 I915_READ(ILK_DISPLAY_CHICKEN1) |
4611 ILK_FBCQ_DIS);
4612 I915_WRITE(ILK_DISPLAY_CHICKEN2,
4613 I915_READ(ILK_DISPLAY_CHICKEN2) |
4614 ILK_DPARB_GATE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004615 }
4616
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01004617 I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
4618
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004619 I915_WRITE(ILK_DISPLAY_CHICKEN2,
4620 I915_READ(ILK_DISPLAY_CHICKEN2) |
4621 ILK_ELPIN_409_SELECT);
4622 I915_WRITE(_3D_CHICKEN2,
4623 _3D_CHICKEN2_WM_READ_PIPELINED << 16 |
4624 _3D_CHICKEN2_WM_READ_PIPELINED);
Daniel Vetter4358a372012-10-18 11:49:51 +02004625
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01004626 /* WaDisableRenderCachePipelinedFlush:ilk */
Daniel Vetter4358a372012-10-18 11:49:51 +02004627 I915_WRITE(CACHE_MODE_0,
4628 _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
Daniel Vetter3107bd42012-10-31 22:52:31 +01004629
Akash Goel4e046322014-04-04 17:14:38 +05304630 /* WaDisable_RenderCache_OperationalFlush:ilk */
4631 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
4632
Ville Syrjälä0e088b82013-06-07 10:47:04 +03004633 g4x_disable_trickle_feed(dev);
Ville Syrjäläbdad2b22013-06-07 10:47:03 +03004634
Daniel Vetter3107bd42012-10-31 22:52:31 +01004635 ibx_init_clock_gating(dev);
4636}
4637
4638static void cpt_init_clock_gating(struct drm_device *dev)
4639{
4640 struct drm_i915_private *dev_priv = dev->dev_private;
4641 int pipe;
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03004642 uint32_t val;
Daniel Vetter3107bd42012-10-31 22:52:31 +01004643
4644 /*
4645 * On Ibex Peak and Cougar Point, we need to disable clock
4646 * gating for the panel power sequencer or it will fail to
4647 * start up when no ports are active.
4648 */
Jesse Barnescd664072013-10-02 10:34:19 -07004649 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE |
4650 PCH_DPLUNIT_CLOCK_GATE_DISABLE |
4651 PCH_CPUNIT_CLOCK_GATE_DISABLE);
Daniel Vetter3107bd42012-10-31 22:52:31 +01004652 I915_WRITE(SOUTH_CHICKEN2, I915_READ(SOUTH_CHICKEN2) |
4653 DPLS_EDP_PPS_FIX_DIS);
Takashi Iwai335c07b2012-12-11 11:46:29 +01004654 /* The below fixes the weird display corruption, a few pixels shifted
4655 * downward, on (only) LVDS of some HP laptops with IVY.
4656 */
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03004657 for_each_pipe(pipe) {
Paulo Zanonidc4bd2d2013-04-08 15:48:08 -03004658 val = I915_READ(TRANS_CHICKEN2(pipe));
4659 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
4660 val &= ~TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03004661 if (dev_priv->vbt.fdi_rx_polarity_inverted)
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03004662 val |= TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
Paulo Zanonidc4bd2d2013-04-08 15:48:08 -03004663 val &= ~TRANS_CHICKEN2_FRAME_START_DELAY_MASK;
4664 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_COUNTER;
4665 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_MODESWITCH;
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03004666 I915_WRITE(TRANS_CHICKEN2(pipe), val);
4667 }
Daniel Vetter3107bd42012-10-31 22:52:31 +01004668 /* WADP0ClockGatingDisable */
4669 for_each_pipe(pipe) {
4670 I915_WRITE(TRANS_CHICKEN1(pipe),
4671 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
4672 }
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004673}
4674
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01004675static void gen6_check_mch_setup(struct drm_device *dev)
4676{
4677 struct drm_i915_private *dev_priv = dev->dev_private;
4678 uint32_t tmp;
4679
4680 tmp = I915_READ(MCH_SSKPD);
4681 if ((tmp & MCH_SSKPD_WM0_MASK) != MCH_SSKPD_WM0_VAL) {
4682 DRM_INFO("Wrong MCH_SSKPD value: 0x%08x\n", tmp);
4683 DRM_INFO("This can cause pipe underruns and display issues.\n");
4684 DRM_INFO("Please upgrade your BIOS to fix this.\n");
4685 }
4686}
4687
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03004688static void gen6_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004689{
4690 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau231e54f2012-10-19 17:55:41 +01004691 uint32_t dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004692
Damien Lespiau231e54f2012-10-19 17:55:41 +01004693 I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004694
4695 I915_WRITE(ILK_DISPLAY_CHICKEN2,
4696 I915_READ(ILK_DISPLAY_CHICKEN2) |
4697 ILK_ELPIN_409_SELECT);
4698
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01004699 /* WaDisableHiZPlanesWhenMSAAEnabled:snb */
Daniel Vetter42839082012-12-14 23:38:28 +01004700 I915_WRITE(_3D_CHICKEN,
4701 _MASKED_BIT_ENABLE(_3D_CHICKEN_HIZ_PLANE_DISABLE_MSAA_4X_SNB));
4702
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01004703 /* WaSetupGtModeTdRowDispatch:snb */
Daniel Vetter6547fbd2012-12-14 23:38:29 +01004704 if (IS_SNB_GT1(dev))
4705 I915_WRITE(GEN6_GT_MODE,
4706 _MASKED_BIT_ENABLE(GEN6_TD_FOUR_ROW_DISPATCH_DISABLE));
4707
Akash Goel4e046322014-04-04 17:14:38 +05304708 /* WaDisable_RenderCache_OperationalFlush:snb */
4709 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
4710
Ville Syrjälä8d85d272014-02-04 21:59:15 +02004711 /*
4712 * BSpec recoomends 8x4 when MSAA is used,
4713 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02004714 *
4715 * Note that PS/WM thread counts depend on the WIZ hashing
4716 * disable bit, which we don't touch here, but it's good
4717 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjälä8d85d272014-02-04 21:59:15 +02004718 */
4719 I915_WRITE(GEN6_GT_MODE,
4720 GEN6_WIZ_HASHING_MASK | GEN6_WIZ_HASHING_16x4);
4721
Ville Syrjälä017636c2013-12-05 15:51:37 +02004722 ilk_init_lp_watermarks(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004723
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004724 I915_WRITE(CACHE_MODE_0,
Daniel Vetter50743292012-04-26 22:02:54 +02004725 _MASKED_BIT_DISABLE(CM0_STC_EVICT_DISABLE_LRA_SNB));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004726
4727 I915_WRITE(GEN6_UCGCTL1,
4728 I915_READ(GEN6_UCGCTL1) |
4729 GEN6_BLBUNIT_CLOCK_GATE_DISABLE |
4730 GEN6_CSUNIT_CLOCK_GATE_DISABLE);
4731
4732 /* According to the BSpec vol1g, bit 12 (RCPBUNIT) clock
4733 * gating disable must be set. Failure to set it results in
4734 * flickering pixels due to Z write ordering failures after
4735 * some amount of runtime in the Mesa "fire" demo, and Unigine
4736 * Sanctuary and Tropics, and apparently anything else with
4737 * alpha test or pixel discard.
4738 *
4739 * According to the spec, bit 11 (RCCUNIT) must also be set,
4740 * but we didn't debug actual testcases to find it out.
Jesse Barnes0f846f82012-06-14 11:04:47 -07004741 *
Ville Syrjäläef593182014-01-22 21:32:47 +02004742 * WaDisableRCCUnitClockGating:snb
4743 * WaDisableRCPBUnitClockGating:snb
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004744 */
4745 I915_WRITE(GEN6_UCGCTL2,
4746 GEN6_RCPBUNIT_CLOCK_GATE_DISABLE |
4747 GEN6_RCCUNIT_CLOCK_GATE_DISABLE);
4748
Ville Syrjälä5eb146d2014-02-04 21:59:16 +02004749 /* WaStripsFansDisableFastClipPerformanceFix:snb */
Ville Syrjälä743b57d2014-02-04 21:59:17 +02004750 I915_WRITE(_3D_CHICKEN3,
4751 _MASKED_BIT_ENABLE(_3D_CHICKEN3_SF_DISABLE_FASTCLIP_CULL));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004752
4753 /*
Ville Syrjäläe927ecd2014-02-04 21:59:18 +02004754 * Bspec says:
4755 * "This bit must be set if 3DSTATE_CLIP clip mode is set to normal and
4756 * 3DSTATE_SF number of SF output attributes is more than 16."
4757 */
4758 I915_WRITE(_3D_CHICKEN3,
4759 _MASKED_BIT_ENABLE(_3D_CHICKEN3_SF_DISABLE_PIPELINED_ATTR_FETCH));
4760
4761 /*
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004762 * According to the spec the following bits should be
4763 * set in order to enable memory self-refresh and fbc:
4764 * The bit21 and bit22 of 0x42000
4765 * The bit21 and bit22 of 0x42004
4766 * The bit5 and bit7 of 0x42020
4767 * The bit14 of 0x70180
4768 * The bit14 of 0x71180
Damien Lespiau4bb35332013-06-14 15:23:24 +01004769 *
4770 * WaFbcAsynchFlipDisableFbcQueue:snb
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004771 */
4772 I915_WRITE(ILK_DISPLAY_CHICKEN1,
4773 I915_READ(ILK_DISPLAY_CHICKEN1) |
4774 ILK_FBCQ_DIS | ILK_PABSTRETCH_DIS);
4775 I915_WRITE(ILK_DISPLAY_CHICKEN2,
4776 I915_READ(ILK_DISPLAY_CHICKEN2) |
4777 ILK_DPARB_GATE | ILK_VSDPFD_FULL);
Damien Lespiau231e54f2012-10-19 17:55:41 +01004778 I915_WRITE(ILK_DSPCLK_GATE_D,
4779 I915_READ(ILK_DSPCLK_GATE_D) |
4780 ILK_DPARBUNIT_CLOCK_GATE_ENABLE |
4781 ILK_DPFDUNIT_CLOCK_GATE_ENABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004782
Ville Syrjälä0e088b82013-06-07 10:47:04 +03004783 g4x_disable_trickle_feed(dev);
Ben Widawskyf8f2ac92012-10-03 19:34:24 -07004784
Daniel Vetter3107bd42012-10-31 22:52:31 +01004785 cpt_init_clock_gating(dev);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01004786
4787 gen6_check_mch_setup(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004788}
4789
4790static void gen7_setup_fixed_func_scheduler(struct drm_i915_private *dev_priv)
4791{
4792 uint32_t reg = I915_READ(GEN7_FF_THREAD_MODE);
4793
Ville Syrjälä3aad9052014-01-22 21:32:59 +02004794 /*
Ville Syrjälä46680e02014-01-22 21:33:01 +02004795 * WaVSThreadDispatchOverride:ivb,vlv
Ville Syrjälä3aad9052014-01-22 21:32:59 +02004796 *
4797 * This actually overrides the dispatch
4798 * mode for all thread types.
4799 */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004800 reg &= ~GEN7_FF_SCHED_MASK;
4801 reg |= GEN7_FF_TS_SCHED_HW;
4802 reg |= GEN7_FF_VS_SCHED_HW;
4803 reg |= GEN7_FF_DS_SCHED_HW;
4804
4805 I915_WRITE(GEN7_FF_THREAD_MODE, reg);
4806}
4807
Paulo Zanoni17a303e2012-11-20 15:12:07 -02004808static void lpt_init_clock_gating(struct drm_device *dev)
4809{
4810 struct drm_i915_private *dev_priv = dev->dev_private;
4811
4812 /*
4813 * TODO: this bit should only be enabled when really needed, then
4814 * disabled when not needed anymore in order to save power.
4815 */
4816 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE)
4817 I915_WRITE(SOUTH_DSPCLK_GATE_D,
4818 I915_READ(SOUTH_DSPCLK_GATE_D) |
4819 PCH_LP_PARTITION_LEVEL_DISABLE);
Paulo Zanoni0a790cd2013-04-17 18:15:49 -03004820
4821 /* WADPOClockGatingDisable:hsw */
4822 I915_WRITE(_TRANSA_CHICKEN1,
4823 I915_READ(_TRANSA_CHICKEN1) |
4824 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
Paulo Zanoni17a303e2012-11-20 15:12:07 -02004825}
4826
Imre Deak7d708ee2013-04-17 14:04:50 +03004827static void lpt_suspend_hw(struct drm_device *dev)
4828{
4829 struct drm_i915_private *dev_priv = dev->dev_private;
4830
4831 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
4832 uint32_t val = I915_READ(SOUTH_DSPCLK_GATE_D);
4833
4834 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
4835 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
4836 }
4837}
4838
Ben Widawsky1020a5c2013-11-02 21:07:06 -07004839static void gen8_init_clock_gating(struct drm_device *dev)
4840{
4841 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau07d27e22014-03-03 17:31:46 +00004842 enum pipe pipe;
Ben Widawsky1020a5c2013-11-02 21:07:06 -07004843
4844 I915_WRITE(WM3_LP_ILK, 0);
4845 I915_WRITE(WM2_LP_ILK, 0);
4846 I915_WRITE(WM1_LP_ILK, 0);
Ben Widawsky50ed5fb2013-11-02 21:07:40 -07004847
4848 /* FIXME(BDW): Check all the w/a, some might only apply to
4849 * pre-production hw. */
4850
Kenneth Graunkec8966e12014-02-26 23:59:30 -08004851 /* WaDisablePartialInstShootdown:bdw */
4852 I915_WRITE(GEN8_ROW_CHICKEN,
4853 _MASKED_BIT_ENABLE(PARTIAL_INSTRUCTION_SHOOTDOWN_DISABLE));
4854
Kenneth Graunke1411e6a2014-02-26 23:59:31 -08004855 /* WaDisableThreadStallDopClockGating:bdw */
4856 /* FIXME: Unclear whether we really need this on production bdw. */
4857 I915_WRITE(GEN8_ROW_CHICKEN,
4858 _MASKED_BIT_ENABLE(STALL_DOP_GATING_DISABLE));
4859
Damien Lespiau4167e322014-01-16 16:51:35 +00004860 /*
4861 * This GEN8_CENTROID_PIXEL_OPT_DIS W/A is only needed for
4862 * pre-production hardware
4863 */
Ben Widawskyfd392b62013-11-04 22:52:39 -08004864 I915_WRITE(HALF_SLICE_CHICKEN3,
4865 _MASKED_BIT_ENABLE(GEN8_CENTROID_PIXEL_OPT_DIS));
Ben Widawskybf663472013-11-02 21:07:57 -07004866 I915_WRITE(HALF_SLICE_CHICKEN3,
4867 _MASKED_BIT_ENABLE(GEN8_SAMPLER_POWER_BYPASS_DIS));
Ben Widawsky4afe8d32013-11-02 21:07:55 -07004868 I915_WRITE(GAMTARBMODE, _MASKED_BIT_ENABLE(ARB_MODE_BWGTLB_DISABLE));
4869
Ben Widawsky7f88da02013-11-02 21:07:58 -07004870 I915_WRITE(_3D_CHICKEN3,
4871 _3D_CHICKEN_SDE_LIMIT_FIFO_POLY_DEPTH(2));
4872
Ben Widawskya75f3622013-11-02 21:07:59 -07004873 I915_WRITE(COMMON_SLICE_CHICKEN2,
4874 _MASKED_BIT_ENABLE(GEN8_CSC2_SBE_VUE_CACHE_CONSERVATIVE));
4875
Ben Widawsky4c2e7a52013-11-02 21:08:00 -07004876 I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
4877 _MASKED_BIT_ENABLE(GEN7_SINGLE_SUBSCAN_DISPATCH_ENABLE));
4878
Ben Widawskyab57fff2013-12-12 15:28:04 -08004879 /* WaSwitchSolVfFArbitrationPriority:bdw */
Ben Widawsky50ed5fb2013-11-02 21:07:40 -07004880 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07004881
Ben Widawskyab57fff2013-12-12 15:28:04 -08004882 /* WaPsrDPAMaskVBlankInSRD:bdw */
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07004883 I915_WRITE(CHICKEN_PAR1_1,
4884 I915_READ(CHICKEN_PAR1_1) | DPA_MASK_VBLANK_SRD);
4885
Ben Widawskyab57fff2013-12-12 15:28:04 -08004886 /* WaPsrDPRSUnmaskVBlankInSRD:bdw */
Damien Lespiau07d27e22014-03-03 17:31:46 +00004887 for_each_pipe(pipe) {
4888 I915_WRITE(CHICKEN_PIPESL_1(pipe),
Ville Syrjäläc7c65622014-03-05 13:05:45 +02004889 I915_READ(CHICKEN_PIPESL_1(pipe)) |
Ville Syrjälä8f670bb2014-03-05 13:05:47 +02004890 BDW_DPRS_MASK_VBLANK_SRD);
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07004891 }
Ben Widawsky63801f22013-12-12 17:26:03 -08004892
4893 /* Use Force Non-Coherent whenever executing a 3D context. This is a
4894 * workaround for for a possible hang in the unlikely event a TLB
4895 * invalidation occurs during a PSD flush.
4896 */
4897 I915_WRITE(HDC_CHICKEN0,
4898 I915_READ(HDC_CHICKEN0) |
4899 _MASKED_BIT_ENABLE(HDC_FORCE_NON_COHERENT));
Ben Widawskyab57fff2013-12-12 15:28:04 -08004900
4901 /* WaVSRefCountFullforceMissDisable:bdw */
4902 /* WaDSRefCountFullforceMissDisable:bdw */
4903 I915_WRITE(GEN7_FF_THREAD_MODE,
4904 I915_READ(GEN7_FF_THREAD_MODE) &
4905 ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
Ville Syrjälä36075a42014-02-04 21:59:21 +02004906
4907 /*
4908 * BSpec recommends 8x4 when MSAA is used,
4909 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02004910 *
4911 * Note that PS/WM thread counts depend on the WIZ hashing
4912 * disable bit, which we don't touch here, but it's good
4913 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjälä36075a42014-02-04 21:59:21 +02004914 */
4915 I915_WRITE(GEN7_GT_MODE,
4916 GEN6_WIZ_HASHING_MASK | GEN6_WIZ_HASHING_16x4);
Ville Syrjälä295e8bb2014-02-27 21:59:01 +02004917
4918 I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
4919 _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
Ville Syrjälä4f1ca9e2014-02-27 21:59:02 +02004920
4921 /* WaDisableSDEUnitClockGating:bdw */
4922 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
4923 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Damien Lespiau5d708682014-03-26 18:41:51 +00004924
4925 /* Wa4x4STCOptimizationDisable:bdw */
4926 I915_WRITE(CACHE_MODE_1,
4927 _MASKED_BIT_ENABLE(GEN8_4x4_STC_OPTIMIZATION_DISABLE));
Ben Widawsky1020a5c2013-11-02 21:07:06 -07004928}
4929
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03004930static void haswell_init_clock_gating(struct drm_device *dev)
4931{
4932 struct drm_i915_private *dev_priv = dev->dev_private;
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03004933
Ville Syrjälä017636c2013-12-05 15:51:37 +02004934 ilk_init_lp_watermarks(dev);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03004935
Francisco Jerezf3fc4882013-10-02 15:53:16 -07004936 /* L3 caching of data atomics doesn't work -- disable it. */
4937 I915_WRITE(HSW_SCRATCH1, HSW_SCRATCH1_L3_DATA_ATOMICS_DISABLE);
4938 I915_WRITE(HSW_ROW_CHICKEN3,
4939 _MASKED_BIT_ENABLE(HSW_ROW_CHICKEN3_L3_GLOBAL_ATOMICS_DISABLE));
4940
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01004941 /* This is required by WaCatErrorRejectionIssue:hsw */
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03004942 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
4943 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
4944 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
4945
Ville Syrjäläe36ea7f2014-01-22 21:33:00 +02004946 /* WaVSRefCountFullforceMissDisable:hsw */
4947 I915_WRITE(GEN7_FF_THREAD_MODE,
4948 I915_READ(GEN7_FF_THREAD_MODE) & ~GEN7_FF_VS_REF_CNT_FFME);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03004949
Akash Goel4e046322014-04-04 17:14:38 +05304950 /* WaDisable_RenderCache_OperationalFlush:hsw */
4951 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
4952
Chia-I Wufe27c602014-01-28 13:29:33 +08004953 /* enable HiZ Raw Stall Optimization */
4954 I915_WRITE(CACHE_MODE_0_GEN7,
4955 _MASKED_BIT_DISABLE(HIZ_RAW_STALL_OPT_DISABLE));
4956
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01004957 /* WaDisable4x2SubspanOptimization:hsw */
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03004958 I915_WRITE(CACHE_MODE_1,
4959 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Eugeni Dodonov1544d9d2012-07-02 11:51:10 -03004960
Ville Syrjäläa12c4962014-02-04 21:59:20 +02004961 /*
4962 * BSpec recommends 8x4 when MSAA is used,
4963 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02004964 *
4965 * Note that PS/WM thread counts depend on the WIZ hashing
4966 * disable bit, which we don't touch here, but it's good
4967 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjäläa12c4962014-02-04 21:59:20 +02004968 */
4969 I915_WRITE(GEN7_GT_MODE,
4970 GEN6_WIZ_HASHING_MASK | GEN6_WIZ_HASHING_16x4);
4971
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01004972 /* WaSwitchSolVfFArbitrationPriority:hsw */
Ben Widawskye3dff582013-03-20 14:49:14 -07004973 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
4974
Paulo Zanoni90a88642013-05-03 17:23:45 -03004975 /* WaRsPkgCStateDisplayPMReq:hsw */
4976 I915_WRITE(CHICKEN_PAR1_1,
4977 I915_READ(CHICKEN_PAR1_1) | FORCE_ARB_IDLE_PLANES);
Eugeni Dodonov1544d9d2012-07-02 11:51:10 -03004978
Paulo Zanoni17a303e2012-11-20 15:12:07 -02004979 lpt_init_clock_gating(dev);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03004980}
4981
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03004982static void ivybridge_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004983{
4984 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawsky20848222012-05-04 18:58:59 -07004985 uint32_t snpcr;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004986
Ville Syrjälä017636c2013-12-05 15:51:37 +02004987 ilk_init_lp_watermarks(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004988
Damien Lespiau231e54f2012-10-19 17:55:41 +01004989 I915_WRITE(ILK_DSPCLK_GATE_D, ILK_VRHUNIT_CLOCK_GATE_DISABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004990
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01004991 /* WaDisableEarlyCull:ivb */
Jesse Barnes87f80202012-10-02 17:43:41 -05004992 I915_WRITE(_3D_CHICKEN3,
4993 _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
4994
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01004995 /* WaDisableBackToBackFlipFix:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004996 I915_WRITE(IVB_CHICKEN3,
4997 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
4998 CHICKEN3_DGMG_DONE_FIX_DISABLE);
4999
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005000 /* WaDisablePSDDualDispatchEnable:ivb */
Jesse Barnes12f33822012-10-25 12:15:45 -07005001 if (IS_IVB_GT1(dev))
5002 I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
5003 _MASKED_BIT_ENABLE(GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
Jesse Barnes12f33822012-10-25 12:15:45 -07005004
Akash Goel4e046322014-04-04 17:14:38 +05305005 /* WaDisable_RenderCache_OperationalFlush:ivb */
5006 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
5007
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005008 /* Apply the WaDisableRHWOOptimizationForRenderHang:ivb workaround. */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005009 I915_WRITE(GEN7_COMMON_SLICE_CHICKEN1,
5010 GEN7_CSC1_RHWO_OPT_DISABLE_IN_RCC);
5011
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005012 /* WaApplyL3ControlAndL3ChickenMode:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005013 I915_WRITE(GEN7_L3CNTLREG1,
5014 GEN7_WA_FOR_GEN7_L3_CONTROL);
5015 I915_WRITE(GEN7_L3_CHICKEN_MODE_REGISTER,
Jesse Barnes8ab43972012-10-25 12:15:42 -07005016 GEN7_WA_L3_CHICKEN_MODE);
5017 if (IS_IVB_GT1(dev))
5018 I915_WRITE(GEN7_ROW_CHICKEN2,
5019 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Ville Syrjälä412236c2014-01-22 21:32:44 +02005020 else {
5021 /* must write both registers */
5022 I915_WRITE(GEN7_ROW_CHICKEN2,
5023 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Jesse Barnes8ab43972012-10-25 12:15:42 -07005024 I915_WRITE(GEN7_ROW_CHICKEN2_GT2,
5025 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Ville Syrjälä412236c2014-01-22 21:32:44 +02005026 }
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005027
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005028 /* WaForceL3Serialization:ivb */
Jesse Barnes61939d92012-10-02 17:43:38 -05005029 I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
5030 ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
5031
Ville Syrjälä1b80a19a2014-01-22 21:32:53 +02005032 /*
Jesse Barnes0f846f82012-06-14 11:04:47 -07005033 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005034 * This implements the WaDisableRCZUnitClockGating:ivb workaround.
Jesse Barnes0f846f82012-06-14 11:04:47 -07005035 */
5036 I915_WRITE(GEN6_UCGCTL2,
Ville Syrjälä28acf3b2014-01-22 21:32:48 +02005037 GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
Jesse Barnes0f846f82012-06-14 11:04:47 -07005038
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005039 /* This is required by WaCatErrorRejectionIssue:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005040 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
5041 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
5042 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
5043
Ville Syrjälä0e088b82013-06-07 10:47:04 +03005044 g4x_disable_trickle_feed(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005045
5046 gen7_setup_fixed_func_scheduler(dev_priv);
Daniel Vetter97e19302012-04-24 16:00:21 +02005047
Chris Wilson22721342014-03-04 09:41:43 +00005048 if (0) { /* causes HiZ corruption on ivb:gt1 */
5049 /* enable HiZ Raw Stall Optimization */
5050 I915_WRITE(CACHE_MODE_0_GEN7,
5051 _MASKED_BIT_DISABLE(HIZ_RAW_STALL_OPT_DISABLE));
5052 }
Chia-I Wu116f2b62014-01-28 13:29:34 +08005053
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005054 /* WaDisable4x2SubspanOptimization:ivb */
Daniel Vetter97e19302012-04-24 16:00:21 +02005055 I915_WRITE(CACHE_MODE_1,
5056 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Ben Widawsky20848222012-05-04 18:58:59 -07005057
Ville Syrjäläa607c1a2014-02-04 21:59:19 +02005058 /*
5059 * BSpec recommends 8x4 when MSAA is used,
5060 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02005061 *
5062 * Note that PS/WM thread counts depend on the WIZ hashing
5063 * disable bit, which we don't touch here, but it's good
5064 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjäläa607c1a2014-02-04 21:59:19 +02005065 */
5066 I915_WRITE(GEN7_GT_MODE,
5067 GEN6_WIZ_HASHING_MASK | GEN6_WIZ_HASHING_16x4);
5068
Ben Widawsky20848222012-05-04 18:58:59 -07005069 snpcr = I915_READ(GEN6_MBCUNIT_SNPCR);
5070 snpcr &= ~GEN6_MBC_SNPCR_MASK;
5071 snpcr |= GEN6_MBC_SNPCR_MED;
5072 I915_WRITE(GEN6_MBCUNIT_SNPCR, snpcr);
Daniel Vetter3107bd42012-10-31 22:52:31 +01005073
Ben Widawskyab5c6082013-04-05 13:12:41 -07005074 if (!HAS_PCH_NOP(dev))
5075 cpt_init_clock_gating(dev);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01005076
5077 gen6_check_mch_setup(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005078}
5079
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005080static void valleyview_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005081{
5082 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes85b1d7b2013-11-04 11:52:45 -08005083 u32 val;
5084
5085 mutex_lock(&dev_priv->rps.hw_lock);
5086 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
5087 mutex_unlock(&dev_priv->rps.hw_lock);
5088 switch ((val >> 6) & 3) {
5089 case 0:
Deepak Sf6d51942014-04-03 21:01:28 +05305090 case 1:
Jesse Barnes85b1d7b2013-11-04 11:52:45 -08005091 dev_priv->mem_freq = 800;
5092 break;
Jesse Barnesf64a28a2013-11-04 16:07:00 -08005093 case 2:
Deepak Sf6d51942014-04-03 21:01:28 +05305094 dev_priv->mem_freq = 1066;
Jesse Barnes85b1d7b2013-11-04 11:52:45 -08005095 break;
Jesse Barnesf64a28a2013-11-04 16:07:00 -08005096 case 3:
Chon Ming Lee23259912013-11-07 15:23:26 +08005097 dev_priv->mem_freq = 1333;
Jesse Barnesf64a28a2013-11-04 16:07:00 -08005098 break;
Jesse Barnes85b1d7b2013-11-04 11:52:45 -08005099 }
5100 DRM_DEBUG_DRIVER("DDR speed: %d MHz", dev_priv->mem_freq);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005101
Imre Deakd60c4472014-03-27 17:45:10 +02005102 dev_priv->vlv_cdclk_freq = valleyview_cur_cdclk(dev_priv);
5103 DRM_DEBUG_DRIVER("Current CD clock rate: %d MHz",
5104 dev_priv->vlv_cdclk_freq);
5105
Ville Syrjäläd7fe0cc2013-05-21 18:01:50 +03005106 I915_WRITE(DSPCLK_GATE_D, VRHUNIT_CLOCK_GATE_DISABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005107
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005108 /* WaDisableEarlyCull:vlv */
Jesse Barnes87f80202012-10-02 17:43:41 -05005109 I915_WRITE(_3D_CHICKEN3,
5110 _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
5111
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005112 /* WaDisableBackToBackFlipFix:vlv */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005113 I915_WRITE(IVB_CHICKEN3,
5114 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
5115 CHICKEN3_DGMG_DONE_FIX_DISABLE);
5116
Ville Syrjäläfad7d362014-01-22 21:32:39 +02005117 /* WaPsdDispatchEnable:vlv */
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005118 /* WaDisablePSDDualDispatchEnable:vlv */
Jesse Barnes12f33822012-10-25 12:15:45 -07005119 I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
Jesse Barnesd3bc0302013-03-08 10:45:51 -08005120 _MASKED_BIT_ENABLE(GEN7_MAX_PS_THREAD_DEP |
5121 GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
Jesse Barnes12f33822012-10-25 12:15:45 -07005122
Akash Goel4e046322014-04-04 17:14:38 +05305123 /* WaDisable_RenderCache_OperationalFlush:vlv */
5124 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
5125
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005126 /* WaForceL3Serialization:vlv */
Jesse Barnes61939d92012-10-02 17:43:38 -05005127 I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
5128 ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
5129
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005130 /* WaDisableDopClockGating:vlv */
Jesse Barnes8ab43972012-10-25 12:15:42 -07005131 I915_WRITE(GEN7_ROW_CHICKEN2,
5132 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
5133
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005134 /* This is required by WaCatErrorRejectionIssue:vlv */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005135 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
5136 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
5137 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
5138
Ville Syrjälä46680e02014-01-22 21:33:01 +02005139 gen7_setup_fixed_func_scheduler(dev_priv);
5140
Ville Syrjälä3c0edae2014-01-22 21:32:56 +02005141 /*
Jesse Barnes0f846f82012-06-14 11:04:47 -07005142 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005143 * This implements the WaDisableRCZUnitClockGating:vlv workaround.
Jesse Barnes0f846f82012-06-14 11:04:47 -07005144 */
5145 I915_WRITE(GEN6_UCGCTL2,
Ville Syrjälä3c0edae2014-01-22 21:32:56 +02005146 GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
Jesse Barnes0f846f82012-06-14 11:04:47 -07005147
Ville Syrjäläc5c32cd2014-01-22 21:32:37 +02005148 /* WaDisableL3Bank2xClockGate:vlv */
Jesse Barnese3f33d42012-06-14 11:04:50 -07005149 I915_WRITE(GEN7_UCGCTL4, GEN7_L3BANK2X_CLOCK_GATE_DISABLE);
5150
Ville Syrjäläe0d8d592013-06-12 22:11:18 +03005151 I915_WRITE(MI_ARB_VLV, MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005152
Ville Syrjäläafd58e72014-01-22 21:33:03 +02005153 /*
5154 * BSpec says this must be set, even though
5155 * WaDisable4x2SubspanOptimization isn't listed for VLV.
5156 */
Daniel Vetter6b26c862012-04-24 14:04:12 +02005157 I915_WRITE(CACHE_MODE_1,
5158 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Jesse Barnes79831172012-06-20 10:53:12 -07005159
5160 /*
Ville Syrjälä031994e2014-01-22 21:32:46 +02005161 * WaIncreaseL3CreditsForVLVB0:vlv
5162 * This is the hardware default actually.
5163 */
5164 I915_WRITE(GEN7_L3SQCREG1, VLV_B0_WA_L3SQCREG1_VALUE);
5165
5166 /*
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005167 * WaDisableVLVClockGating_VBIIssue:vlv
Jesse Barnes2d809572012-10-25 12:15:44 -07005168 * Disable clock gating on th GCFG unit to prevent a delay
5169 * in the reporting of vblank events.
5170 */
Ville Syrjälä7a0d1ee2014-01-22 21:33:04 +02005171 I915_WRITE(VLV_GUNIT_CLOCK_GATE, GCFG_DIS);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005172}
5173
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005174static void g4x_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005175{
5176 struct drm_i915_private *dev_priv = dev->dev_private;
5177 uint32_t dspclk_gate;
5178
5179 I915_WRITE(RENCLK_GATE_D1, 0);
5180 I915_WRITE(RENCLK_GATE_D2, VF_UNIT_CLOCK_GATE_DISABLE |
5181 GS_UNIT_CLOCK_GATE_DISABLE |
5182 CL_UNIT_CLOCK_GATE_DISABLE);
5183 I915_WRITE(RAMCLK_GATE_D, 0);
5184 dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE |
5185 OVRUNIT_CLOCK_GATE_DISABLE |
5186 OVCUNIT_CLOCK_GATE_DISABLE;
5187 if (IS_GM45(dev))
5188 dspclk_gate |= DSSUNIT_CLOCK_GATE_DISABLE;
5189 I915_WRITE(DSPCLK_GATE_D, dspclk_gate);
Daniel Vetter4358a372012-10-18 11:49:51 +02005190
5191 /* WaDisableRenderCachePipelinedFlush */
5192 I915_WRITE(CACHE_MODE_0,
5193 _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
Ville Syrjäläde1aa622013-06-07 10:47:01 +03005194
Akash Goel4e046322014-04-04 17:14:38 +05305195 /* WaDisable_RenderCache_OperationalFlush:g4x */
5196 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
5197
Ville Syrjälä0e088b82013-06-07 10:47:04 +03005198 g4x_disable_trickle_feed(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005199}
5200
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005201static void crestline_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005202{
5203 struct drm_i915_private *dev_priv = dev->dev_private;
5204
5205 I915_WRITE(RENCLK_GATE_D1, I965_RCC_CLOCK_GATE_DISABLE);
5206 I915_WRITE(RENCLK_GATE_D2, 0);
5207 I915_WRITE(DSPCLK_GATE_D, 0);
5208 I915_WRITE(RAMCLK_GATE_D, 0);
5209 I915_WRITE16(DEUC, 0);
Ville Syrjälä20f94962013-06-07 10:47:02 +03005210 I915_WRITE(MI_ARB_STATE,
5211 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Akash Goel4e046322014-04-04 17:14:38 +05305212
5213 /* WaDisable_RenderCache_OperationalFlush:gen4 */
5214 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005215}
5216
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005217static void broadwater_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005218{
5219 struct drm_i915_private *dev_priv = dev->dev_private;
5220
5221 I915_WRITE(RENCLK_GATE_D1, I965_RCZ_CLOCK_GATE_DISABLE |
5222 I965_RCC_CLOCK_GATE_DISABLE |
5223 I965_RCPB_CLOCK_GATE_DISABLE |
5224 I965_ISC_CLOCK_GATE_DISABLE |
5225 I965_FBC_CLOCK_GATE_DISABLE);
5226 I915_WRITE(RENCLK_GATE_D2, 0);
Ville Syrjälä20f94962013-06-07 10:47:02 +03005227 I915_WRITE(MI_ARB_STATE,
5228 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Akash Goel4e046322014-04-04 17:14:38 +05305229
5230 /* WaDisable_RenderCache_OperationalFlush:gen4 */
5231 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005232}
5233
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005234static void gen3_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005235{
5236 struct drm_i915_private *dev_priv = dev->dev_private;
5237 u32 dstate = I915_READ(D_STATE);
5238
5239 dstate |= DSTATE_PLL_D3_OFF | DSTATE_GFX_CLOCK_GATING |
5240 DSTATE_DOT_CLOCK_GATING;
5241 I915_WRITE(D_STATE, dstate);
Chris Wilson13a86b82012-04-24 14:51:43 +01005242
5243 if (IS_PINEVIEW(dev))
5244 I915_WRITE(ECOSKPD, _MASKED_BIT_ENABLE(ECO_GATING_CX_ONLY));
Daniel Vetter974a3b02012-09-09 11:54:16 +02005245
5246 /* IIR "flip pending" means done if this bit is set */
5247 I915_WRITE(ECOSKPD, _MASKED_BIT_DISABLE(ECO_FLIP_DONE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005248}
5249
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005250static void i85x_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005251{
5252 struct drm_i915_private *dev_priv = dev->dev_private;
5253
5254 I915_WRITE(RENCLK_GATE_D1, SV_CLOCK_GATE_DISABLE);
5255}
5256
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005257static void i830_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005258{
5259 struct drm_i915_private *dev_priv = dev->dev_private;
5260
5261 I915_WRITE(DSPCLK_GATE_D, OVRUNIT_CLOCK_GATE_DISABLE);
5262}
5263
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005264void intel_init_clock_gating(struct drm_device *dev)
5265{
5266 struct drm_i915_private *dev_priv = dev->dev_private;
5267
5268 dev_priv->display.init_clock_gating(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005269}
5270
Imre Deak7d708ee2013-04-17 14:04:50 +03005271void intel_suspend_hw(struct drm_device *dev)
5272{
5273 if (HAS_PCH_LPT(dev))
5274 lpt_suspend_hw(dev);
5275}
5276
Imre Deakc1ca7272013-11-25 17:15:29 +02005277#define for_each_power_well(i, power_well, domain_mask, power_domains) \
5278 for (i = 0; \
5279 i < (power_domains)->power_well_count && \
5280 ((power_well) = &(power_domains)->power_wells[i]); \
5281 i++) \
5282 if ((power_well)->domains & (domain_mask))
5283
5284#define for_each_power_well_rev(i, power_well, domain_mask, power_domains) \
5285 for (i = (power_domains)->power_well_count - 1; \
5286 i >= 0 && ((power_well) = &(power_domains)->power_wells[i]);\
5287 i--) \
5288 if ((power_well)->domains & (domain_mask))
5289
Paulo Zanoni15d199e2013-03-22 14:14:13 -03005290/**
5291 * We should only use the power well if we explicitly asked the hardware to
5292 * enable it, so check if it's enabled and also check if we've requested it to
5293 * be enabled.
5294 */
Imre Deakda7e29b2014-02-18 00:02:02 +02005295static bool hsw_power_well_enabled(struct drm_i915_private *dev_priv,
Imre Deakc1ca7272013-11-25 17:15:29 +02005296 struct i915_power_well *power_well)
5297{
Imre Deakc1ca7272013-11-25 17:15:29 +02005298 return I915_READ(HSW_PWR_WELL_DRIVER) ==
5299 (HSW_PWR_WELL_ENABLE_REQUEST | HSW_PWR_WELL_STATE_ENABLED);
5300}
5301
Imre Deakda7e29b2014-02-18 00:02:02 +02005302bool intel_display_power_enabled_sw(struct drm_i915_private *dev_priv,
Imre Deakddf9c532013-11-27 22:02:02 +02005303 enum intel_display_power_domain domain)
5304{
Imre Deakddf9c532013-11-27 22:02:02 +02005305 struct i915_power_domains *power_domains;
5306
5307 power_domains = &dev_priv->power_domains;
5308
5309 return power_domains->domain_use_count[domain];
5310}
5311
Imre Deakda7e29b2014-02-18 00:02:02 +02005312bool intel_display_power_enabled(struct drm_i915_private *dev_priv,
Paulo Zanonib97186f2013-05-03 12:15:36 -03005313 enum intel_display_power_domain domain)
Paulo Zanoni15d199e2013-03-22 14:14:13 -03005314{
Imre Deakc1ca7272013-11-25 17:15:29 +02005315 struct i915_power_domains *power_domains;
5316 struct i915_power_well *power_well;
5317 bool is_enabled;
5318 int i;
Paulo Zanoni15d199e2013-03-22 14:14:13 -03005319
Paulo Zanoni882244a2014-04-01 14:55:12 -03005320 if (dev_priv->pm.suspended)
5321 return false;
5322
Imre Deakc1ca7272013-11-25 17:15:29 +02005323 power_domains = &dev_priv->power_domains;
5324
5325 is_enabled = true;
5326
5327 mutex_lock(&power_domains->lock);
5328 for_each_power_well_rev(i, power_well, BIT(domain), power_domains) {
Imre Deak6f3ef5d2013-11-25 17:15:30 +02005329 if (power_well->always_on)
5330 continue;
5331
Imre Deakc6cb5822014-03-04 19:22:55 +02005332 if (!power_well->ops->is_enabled(dev_priv, power_well)) {
Imre Deakc1ca7272013-11-25 17:15:29 +02005333 is_enabled = false;
5334 break;
5335 }
5336 }
5337 mutex_unlock(&power_domains->lock);
5338
5339 return is_enabled;
Paulo Zanoni15d199e2013-03-22 14:14:13 -03005340}
5341
Imre Deak93c73e82014-02-18 00:02:19 +02005342/*
5343 * Starting with Haswell, we have a "Power Down Well" that can be turned off
5344 * when not needed anymore. We have 4 registers that can request the power well
5345 * to be enabled, and it will only be disabled if none of the registers is
5346 * requesting it to be enabled.
5347 */
Paulo Zanonid5e8fdc2013-12-11 18:50:09 -02005348static void hsw_power_well_post_enable(struct drm_i915_private *dev_priv)
5349{
5350 struct drm_device *dev = dev_priv->dev;
5351 unsigned long irqflags;
5352
Paulo Zanonif9dcb0d2013-12-11 18:50:10 -02005353 /*
5354 * After we re-enable the power well, if we touch VGA register 0x3d5
5355 * we'll get unclaimed register interrupts. This stops after we write
5356 * anything to the VGA MSR register. The vgacon module uses this
5357 * register all the time, so if we unbind our driver and, as a
5358 * consequence, bind vgacon, we'll get stuck in an infinite loop at
5359 * console_unlock(). So make here we touch the VGA MSR register, making
5360 * sure vgacon can keep working normally without triggering interrupts
5361 * and error messages.
5362 */
5363 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
5364 outb(inb(VGA_MSR_READ), VGA_MSR_WRITE);
5365 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
5366
Paulo Zanonid5e8fdc2013-12-11 18:50:09 -02005367 if (IS_BROADWELL(dev)) {
5368 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
5369 I915_WRITE(GEN8_DE_PIPE_IMR(PIPE_B),
5370 dev_priv->de_irq_mask[PIPE_B]);
5371 I915_WRITE(GEN8_DE_PIPE_IER(PIPE_B),
5372 ~dev_priv->de_irq_mask[PIPE_B] |
5373 GEN8_PIPE_VBLANK);
5374 I915_WRITE(GEN8_DE_PIPE_IMR(PIPE_C),
5375 dev_priv->de_irq_mask[PIPE_C]);
5376 I915_WRITE(GEN8_DE_PIPE_IER(PIPE_C),
5377 ~dev_priv->de_irq_mask[PIPE_C] |
5378 GEN8_PIPE_VBLANK);
5379 POSTING_READ(GEN8_DE_PIPE_IER(PIPE_C));
5380 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
5381 }
5382}
5383
Imre Deakdd7c0b62014-03-04 19:23:03 +02005384static void reset_vblank_counter(struct drm_device *dev, enum pipe pipe)
5385{
5386 assert_spin_locked(&dev->vbl_lock);
5387
5388 dev->vblank[pipe].last = 0;
5389}
5390
Paulo Zanonid5e8fdc2013-12-11 18:50:09 -02005391static void hsw_power_well_post_disable(struct drm_i915_private *dev_priv)
5392{
5393 struct drm_device *dev = dev_priv->dev;
Damien Lespiau07d27e22014-03-03 17:31:46 +00005394 enum pipe pipe;
Paulo Zanonid5e8fdc2013-12-11 18:50:09 -02005395 unsigned long irqflags;
5396
5397 /*
5398 * After this, the registers on the pipes that are part of the power
5399 * well will become zero, so we have to adjust our counters according to
5400 * that.
5401 *
5402 * FIXME: Should we do this in general in drm_vblank_post_modeset?
5403 */
5404 spin_lock_irqsave(&dev->vbl_lock, irqflags);
Damien Lespiau07d27e22014-03-03 17:31:46 +00005405 for_each_pipe(pipe)
5406 if (pipe != PIPE_A)
Imre Deakdd7c0b62014-03-04 19:23:03 +02005407 reset_vblank_counter(dev, pipe);
Paulo Zanonid5e8fdc2013-12-11 18:50:09 -02005408 spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
5409}
5410
Imre Deakda7e29b2014-02-18 00:02:02 +02005411static void hsw_set_power_well(struct drm_i915_private *dev_priv,
Imre Deakc1ca7272013-11-25 17:15:29 +02005412 struct i915_power_well *power_well, bool enable)
Eugeni Dodonovd0d3e512012-05-09 15:37:16 -03005413{
Paulo Zanonifa42e232013-01-25 16:59:11 -02005414 bool is_enabled, enable_requested;
5415 uint32_t tmp;
Eugeni Dodonovd0d3e512012-05-09 15:37:16 -03005416
Paulo Zanonifa42e232013-01-25 16:59:11 -02005417 tmp = I915_READ(HSW_PWR_WELL_DRIVER);
Paulo Zanoni6aedd1f2013-08-02 16:22:25 -03005418 is_enabled = tmp & HSW_PWR_WELL_STATE_ENABLED;
5419 enable_requested = tmp & HSW_PWR_WELL_ENABLE_REQUEST;
Eugeni Dodonovd0d3e512012-05-09 15:37:16 -03005420
Paulo Zanonifa42e232013-01-25 16:59:11 -02005421 if (enable) {
5422 if (!enable_requested)
Paulo Zanoni6aedd1f2013-08-02 16:22:25 -03005423 I915_WRITE(HSW_PWR_WELL_DRIVER,
5424 HSW_PWR_WELL_ENABLE_REQUEST);
Eugeni Dodonovd0d3e512012-05-09 15:37:16 -03005425
Paulo Zanonifa42e232013-01-25 16:59:11 -02005426 if (!is_enabled) {
5427 DRM_DEBUG_KMS("Enabling power well\n");
5428 if (wait_for((I915_READ(HSW_PWR_WELL_DRIVER) &
Paulo Zanoni6aedd1f2013-08-02 16:22:25 -03005429 HSW_PWR_WELL_STATE_ENABLED), 20))
Paulo Zanonifa42e232013-01-25 16:59:11 -02005430 DRM_ERROR("Timeout enabling power well\n");
5431 }
Ben Widawsky596cc112013-11-11 14:46:28 -08005432
Paulo Zanonid5e8fdc2013-12-11 18:50:09 -02005433 hsw_power_well_post_enable(dev_priv);
Paulo Zanonifa42e232013-01-25 16:59:11 -02005434 } else {
5435 if (enable_requested) {
5436 I915_WRITE(HSW_PWR_WELL_DRIVER, 0);
Paulo Zanoni9dbd8fe2013-07-23 10:48:11 -03005437 POSTING_READ(HSW_PWR_WELL_DRIVER);
Paulo Zanonifa42e232013-01-25 16:59:11 -02005438 DRM_DEBUG_KMS("Requesting to disable the power well\n");
Paulo Zanoni9dbd8fe2013-07-23 10:48:11 -03005439
Paulo Zanonid5e8fdc2013-12-11 18:50:09 -02005440 hsw_power_well_post_disable(dev_priv);
Eugeni Dodonovd0d3e512012-05-09 15:37:16 -03005441 }
5442 }
Paulo Zanonifa42e232013-01-25 16:59:11 -02005443}
Eugeni Dodonovd0d3e512012-05-09 15:37:16 -03005444
Imre Deakc6cb5822014-03-04 19:22:55 +02005445static void hsw_power_well_sync_hw(struct drm_i915_private *dev_priv,
5446 struct i915_power_well *power_well)
5447{
5448 hsw_set_power_well(dev_priv, power_well, power_well->count > 0);
5449
5450 /*
5451 * We're taking over the BIOS, so clear any requests made by it since
5452 * the driver is in charge now.
5453 */
5454 if (I915_READ(HSW_PWR_WELL_BIOS) & HSW_PWR_WELL_ENABLE_REQUEST)
5455 I915_WRITE(HSW_PWR_WELL_BIOS, 0);
5456}
5457
5458static void hsw_power_well_enable(struct drm_i915_private *dev_priv,
5459 struct i915_power_well *power_well)
5460{
Imre Deakc6cb5822014-03-04 19:22:55 +02005461 hsw_set_power_well(dev_priv, power_well, true);
5462}
5463
5464static void hsw_power_well_disable(struct drm_i915_private *dev_priv,
5465 struct i915_power_well *power_well)
5466{
5467 hsw_set_power_well(dev_priv, power_well, false);
Imre Deakc6cb5822014-03-04 19:22:55 +02005468}
5469
Imre Deaka45f44662014-03-04 19:22:56 +02005470static void i9xx_always_on_power_well_noop(struct drm_i915_private *dev_priv,
5471 struct i915_power_well *power_well)
5472{
5473}
5474
5475static bool i9xx_always_on_power_well_enabled(struct drm_i915_private *dev_priv,
5476 struct i915_power_well *power_well)
5477{
5478 return true;
5479}
5480
Imre Deak77961eb2014-03-05 16:20:56 +02005481static void vlv_set_power_well(struct drm_i915_private *dev_priv,
5482 struct i915_power_well *power_well, bool enable)
5483{
5484 enum punit_power_well power_well_id = power_well->data;
5485 u32 mask;
5486 u32 state;
5487 u32 ctrl;
5488
5489 mask = PUNIT_PWRGT_MASK(power_well_id);
5490 state = enable ? PUNIT_PWRGT_PWR_ON(power_well_id) :
5491 PUNIT_PWRGT_PWR_GATE(power_well_id);
5492
5493 mutex_lock(&dev_priv->rps.hw_lock);
5494
5495#define COND \
5496 ((vlv_punit_read(dev_priv, PUNIT_REG_PWRGT_STATUS) & mask) == state)
5497
5498 if (COND)
5499 goto out;
5500
5501 ctrl = vlv_punit_read(dev_priv, PUNIT_REG_PWRGT_CTRL);
5502 ctrl &= ~mask;
5503 ctrl |= state;
5504 vlv_punit_write(dev_priv, PUNIT_REG_PWRGT_CTRL, ctrl);
5505
5506 if (wait_for(COND, 100))
5507 DRM_ERROR("timout setting power well state %08x (%08x)\n",
5508 state,
5509 vlv_punit_read(dev_priv, PUNIT_REG_PWRGT_CTRL));
5510
5511#undef COND
5512
5513out:
5514 mutex_unlock(&dev_priv->rps.hw_lock);
5515}
5516
5517static void vlv_power_well_sync_hw(struct drm_i915_private *dev_priv,
5518 struct i915_power_well *power_well)
5519{
5520 vlv_set_power_well(dev_priv, power_well, power_well->count > 0);
5521}
5522
5523static void vlv_power_well_enable(struct drm_i915_private *dev_priv,
5524 struct i915_power_well *power_well)
5525{
5526 vlv_set_power_well(dev_priv, power_well, true);
5527}
5528
5529static void vlv_power_well_disable(struct drm_i915_private *dev_priv,
5530 struct i915_power_well *power_well)
5531{
5532 vlv_set_power_well(dev_priv, power_well, false);
5533}
5534
5535static bool vlv_power_well_enabled(struct drm_i915_private *dev_priv,
5536 struct i915_power_well *power_well)
5537{
5538 int power_well_id = power_well->data;
5539 bool enabled = false;
5540 u32 mask;
5541 u32 state;
5542 u32 ctrl;
5543
5544 mask = PUNIT_PWRGT_MASK(power_well_id);
5545 ctrl = PUNIT_PWRGT_PWR_ON(power_well_id);
5546
5547 mutex_lock(&dev_priv->rps.hw_lock);
5548
5549 state = vlv_punit_read(dev_priv, PUNIT_REG_PWRGT_STATUS) & mask;
5550 /*
5551 * We only ever set the power-on and power-gate states, anything
5552 * else is unexpected.
5553 */
5554 WARN_ON(state != PUNIT_PWRGT_PWR_ON(power_well_id) &&
5555 state != PUNIT_PWRGT_PWR_GATE(power_well_id));
5556 if (state == ctrl)
5557 enabled = true;
5558
5559 /*
5560 * A transient state at this point would mean some unexpected party
5561 * is poking at the power controls too.
5562 */
5563 ctrl = vlv_punit_read(dev_priv, PUNIT_REG_PWRGT_CTRL) & mask;
5564 WARN_ON(ctrl != state);
5565
5566 mutex_unlock(&dev_priv->rps.hw_lock);
5567
5568 return enabled;
5569}
5570
5571static void vlv_display_power_well_enable(struct drm_i915_private *dev_priv,
5572 struct i915_power_well *power_well)
5573{
5574 WARN_ON_ONCE(power_well->data != PUNIT_POWER_WELL_DISP2D);
5575
5576 vlv_set_power_well(dev_priv, power_well, true);
5577
5578 spin_lock_irq(&dev_priv->irq_lock);
5579 valleyview_enable_display_irqs(dev_priv);
5580 spin_unlock_irq(&dev_priv->irq_lock);
5581
5582 /*
5583 * During driver initialization we need to defer enabling hotplug
5584 * processing until fbdev is set up.
5585 */
5586 if (dev_priv->enable_hotplug_processing)
5587 intel_hpd_init(dev_priv->dev);
5588
5589 i915_redisable_vga_power_on(dev_priv->dev);
5590}
5591
5592static void vlv_display_power_well_disable(struct drm_i915_private *dev_priv,
5593 struct i915_power_well *power_well)
5594{
5595 struct drm_device *dev = dev_priv->dev;
5596 enum pipe pipe;
5597
5598 WARN_ON_ONCE(power_well->data != PUNIT_POWER_WELL_DISP2D);
5599
5600 spin_lock_irq(&dev_priv->irq_lock);
5601 for_each_pipe(pipe)
5602 __intel_set_cpu_fifo_underrun_reporting(dev, pipe, false);
5603
5604 valleyview_disable_display_irqs(dev_priv);
5605 spin_unlock_irq(&dev_priv->irq_lock);
5606
5607 spin_lock_irq(&dev->vbl_lock);
5608 for_each_pipe(pipe)
5609 reset_vblank_counter(dev, pipe);
5610 spin_unlock_irq(&dev->vbl_lock);
5611
5612 vlv_set_power_well(dev_priv, power_well, false);
5613}
5614
Imre Deak25eaa002014-03-04 19:23:06 +02005615static void check_power_well_state(struct drm_i915_private *dev_priv,
5616 struct i915_power_well *power_well)
5617{
5618 bool enabled = power_well->ops->is_enabled(dev_priv, power_well);
5619
5620 if (power_well->always_on || !i915.disable_power_well) {
5621 if (!enabled)
5622 goto mismatch;
5623
5624 return;
5625 }
5626
5627 if (enabled != (power_well->count > 0))
5628 goto mismatch;
5629
5630 return;
5631
5632mismatch:
5633 WARN(1, "state mismatch for '%s' (always_on %d hw state %d use-count %d disable_power_well %d\n",
5634 power_well->name, power_well->always_on, enabled,
5635 power_well->count, i915.disable_power_well);
5636}
5637
Imre Deakda7e29b2014-02-18 00:02:02 +02005638void intel_display_power_get(struct drm_i915_private *dev_priv,
Ville Syrjälä67656252013-09-16 17:38:28 +03005639 enum intel_display_power_domain domain)
5640{
Imre Deak83c00f52013-10-25 17:36:47 +03005641 struct i915_power_domains *power_domains;
Imre Deakc1ca7272013-11-25 17:15:29 +02005642 struct i915_power_well *power_well;
5643 int i;
Ville Syrjälä67656252013-09-16 17:38:28 +03005644
Paulo Zanoni9e6ea712014-03-07 20:08:06 -03005645 intel_runtime_pm_get(dev_priv);
5646
Imre Deak83c00f52013-10-25 17:36:47 +03005647 power_domains = &dev_priv->power_domains;
5648
5649 mutex_lock(&power_domains->lock);
Imre Deak1da51582013-11-25 17:15:35 +02005650
Imre Deak25eaa002014-03-04 19:23:06 +02005651 for_each_power_well(i, power_well, BIT(domain), power_domains) {
5652 if (!power_well->count++) {
5653 DRM_DEBUG_KMS("enabling %s\n", power_well->name);
Imre Deakc6cb5822014-03-04 19:22:55 +02005654 power_well->ops->enable(dev_priv, power_well);
Imre Deak25eaa002014-03-04 19:23:06 +02005655 }
5656
5657 check_power_well_state(dev_priv, power_well);
5658 }
Imre Deak1da51582013-11-25 17:15:35 +02005659
Imre Deakddf9c532013-11-27 22:02:02 +02005660 power_domains->domain_use_count[domain]++;
5661
Imre Deak83c00f52013-10-25 17:36:47 +03005662 mutex_unlock(&power_domains->lock);
Ville Syrjälä67656252013-09-16 17:38:28 +03005663}
5664
Imre Deakda7e29b2014-02-18 00:02:02 +02005665void intel_display_power_put(struct drm_i915_private *dev_priv,
Ville Syrjälä67656252013-09-16 17:38:28 +03005666 enum intel_display_power_domain domain)
5667{
Imre Deak83c00f52013-10-25 17:36:47 +03005668 struct i915_power_domains *power_domains;
Imre Deakc1ca7272013-11-25 17:15:29 +02005669 struct i915_power_well *power_well;
5670 int i;
Ville Syrjälä67656252013-09-16 17:38:28 +03005671
Imre Deak83c00f52013-10-25 17:36:47 +03005672 power_domains = &dev_priv->power_domains;
5673
5674 mutex_lock(&power_domains->lock);
Imre Deak1da51582013-11-25 17:15:35 +02005675
Imre Deak1da51582013-11-25 17:15:35 +02005676 WARN_ON(!power_domains->domain_use_count[domain]);
5677 power_domains->domain_use_count[domain]--;
Imre Deakddf9c532013-11-27 22:02:02 +02005678
Imre Deak70bf4072014-03-04 19:22:51 +02005679 for_each_power_well_rev(i, power_well, BIT(domain), power_domains) {
5680 WARN_ON(!power_well->count);
5681
Imre Deak25eaa002014-03-04 19:23:06 +02005682 if (!--power_well->count && i915.disable_power_well) {
5683 DRM_DEBUG_KMS("disabling %s\n", power_well->name);
Imre Deakc6cb5822014-03-04 19:22:55 +02005684 power_well->ops->disable(dev_priv, power_well);
Imre Deak25eaa002014-03-04 19:23:06 +02005685 }
5686
5687 check_power_well_state(dev_priv, power_well);
Imre Deak70bf4072014-03-04 19:22:51 +02005688 }
Imre Deak1da51582013-11-25 17:15:35 +02005689
Imre Deak83c00f52013-10-25 17:36:47 +03005690 mutex_unlock(&power_domains->lock);
Paulo Zanoni9e6ea712014-03-07 20:08:06 -03005691
5692 intel_runtime_pm_put(dev_priv);
Ville Syrjälä67656252013-09-16 17:38:28 +03005693}
5694
Imre Deak83c00f52013-10-25 17:36:47 +03005695static struct i915_power_domains *hsw_pwr;
Wang Xingchaoa38911a2013-05-30 22:07:11 +08005696
5697/* Display audio driver power well request */
5698void i915_request_power_well(void)
5699{
Imre Deakb4ed4482013-10-25 17:36:49 +03005700 struct drm_i915_private *dev_priv;
5701
Wang Xingchaoa38911a2013-05-30 22:07:11 +08005702 if (WARN_ON(!hsw_pwr))
5703 return;
5704
Imre Deakb4ed4482013-10-25 17:36:49 +03005705 dev_priv = container_of(hsw_pwr, struct drm_i915_private,
5706 power_domains);
Imre Deakda7e29b2014-02-18 00:02:02 +02005707 intel_display_power_get(dev_priv, POWER_DOMAIN_AUDIO);
Wang Xingchaoa38911a2013-05-30 22:07:11 +08005708}
5709EXPORT_SYMBOL_GPL(i915_request_power_well);
5710
5711/* Display audio driver power well release */
5712void i915_release_power_well(void)
5713{
Imre Deakb4ed4482013-10-25 17:36:49 +03005714 struct drm_i915_private *dev_priv;
5715
Wang Xingchaoa38911a2013-05-30 22:07:11 +08005716 if (WARN_ON(!hsw_pwr))
5717 return;
5718
Imre Deakb4ed4482013-10-25 17:36:49 +03005719 dev_priv = container_of(hsw_pwr, struct drm_i915_private,
5720 power_domains);
Imre Deakda7e29b2014-02-18 00:02:02 +02005721 intel_display_power_put(dev_priv, POWER_DOMAIN_AUDIO);
Wang Xingchaoa38911a2013-05-30 22:07:11 +08005722}
5723EXPORT_SYMBOL_GPL(i915_release_power_well);
5724
Imre Deakefcad912014-03-04 19:22:53 +02005725#define POWER_DOMAIN_MASK (BIT(POWER_DOMAIN_NUM) - 1)
5726
5727#define HSW_ALWAYS_ON_POWER_DOMAINS ( \
5728 BIT(POWER_DOMAIN_PIPE_A) | \
Imre Deakf5938f32014-03-04 19:22:54 +02005729 BIT(POWER_DOMAIN_TRANSCODER_EDP) | \
Imre Deak319be8a2014-03-04 19:22:57 +02005730 BIT(POWER_DOMAIN_PORT_DDI_A_2_LANES) | \
5731 BIT(POWER_DOMAIN_PORT_DDI_A_4_LANES) | \
5732 BIT(POWER_DOMAIN_PORT_DDI_B_2_LANES) | \
5733 BIT(POWER_DOMAIN_PORT_DDI_B_4_LANES) | \
5734 BIT(POWER_DOMAIN_PORT_DDI_C_2_LANES) | \
5735 BIT(POWER_DOMAIN_PORT_DDI_C_4_LANES) | \
5736 BIT(POWER_DOMAIN_PORT_DDI_D_2_LANES) | \
5737 BIT(POWER_DOMAIN_PORT_DDI_D_4_LANES) | \
5738 BIT(POWER_DOMAIN_PORT_CRT) | \
Imre Deakf5938f32014-03-04 19:22:54 +02005739 BIT(POWER_DOMAIN_INIT))
Imre Deakefcad912014-03-04 19:22:53 +02005740#define HSW_DISPLAY_POWER_DOMAINS ( \
5741 (POWER_DOMAIN_MASK & ~HSW_ALWAYS_ON_POWER_DOMAINS) | \
5742 BIT(POWER_DOMAIN_INIT))
5743
5744#define BDW_ALWAYS_ON_POWER_DOMAINS ( \
5745 HSW_ALWAYS_ON_POWER_DOMAINS | \
5746 BIT(POWER_DOMAIN_PIPE_A_PANEL_FITTER))
5747#define BDW_DISPLAY_POWER_DOMAINS ( \
5748 (POWER_DOMAIN_MASK & ~BDW_ALWAYS_ON_POWER_DOMAINS) | \
5749 BIT(POWER_DOMAIN_INIT))
5750
Imre Deak77961eb2014-03-05 16:20:56 +02005751#define VLV_ALWAYS_ON_POWER_DOMAINS BIT(POWER_DOMAIN_INIT)
5752#define VLV_DISPLAY_POWER_DOMAINS POWER_DOMAIN_MASK
5753
5754#define VLV_DPIO_CMN_BC_POWER_DOMAINS ( \
5755 BIT(POWER_DOMAIN_PORT_DDI_B_2_LANES) | \
5756 BIT(POWER_DOMAIN_PORT_DDI_B_4_LANES) | \
5757 BIT(POWER_DOMAIN_PORT_DDI_C_2_LANES) | \
5758 BIT(POWER_DOMAIN_PORT_DDI_C_4_LANES) | \
5759 BIT(POWER_DOMAIN_PORT_CRT) | \
5760 BIT(POWER_DOMAIN_INIT))
5761
5762#define VLV_DPIO_TX_B_LANES_01_POWER_DOMAINS ( \
5763 BIT(POWER_DOMAIN_PORT_DDI_B_2_LANES) | \
5764 BIT(POWER_DOMAIN_PORT_DDI_B_4_LANES) | \
5765 BIT(POWER_DOMAIN_INIT))
5766
5767#define VLV_DPIO_TX_B_LANES_23_POWER_DOMAINS ( \
5768 BIT(POWER_DOMAIN_PORT_DDI_B_4_LANES) | \
5769 BIT(POWER_DOMAIN_INIT))
5770
5771#define VLV_DPIO_TX_C_LANES_01_POWER_DOMAINS ( \
5772 BIT(POWER_DOMAIN_PORT_DDI_C_2_LANES) | \
5773 BIT(POWER_DOMAIN_PORT_DDI_C_4_LANES) | \
5774 BIT(POWER_DOMAIN_INIT))
5775
5776#define VLV_DPIO_TX_C_LANES_23_POWER_DOMAINS ( \
5777 BIT(POWER_DOMAIN_PORT_DDI_C_4_LANES) | \
5778 BIT(POWER_DOMAIN_INIT))
5779
Imre Deaka45f44662014-03-04 19:22:56 +02005780static const struct i915_power_well_ops i9xx_always_on_power_well_ops = {
5781 .sync_hw = i9xx_always_on_power_well_noop,
5782 .enable = i9xx_always_on_power_well_noop,
5783 .disable = i9xx_always_on_power_well_noop,
5784 .is_enabled = i9xx_always_on_power_well_enabled,
5785};
Imre Deakc6cb5822014-03-04 19:22:55 +02005786
Imre Deak1c2256d2013-11-25 17:15:34 +02005787static struct i915_power_well i9xx_always_on_power_well[] = {
5788 {
5789 .name = "always-on",
5790 .always_on = 1,
5791 .domains = POWER_DOMAIN_MASK,
Imre Deakc6cb5822014-03-04 19:22:55 +02005792 .ops = &i9xx_always_on_power_well_ops,
Imre Deak1c2256d2013-11-25 17:15:34 +02005793 },
5794};
5795
Imre Deakc6cb5822014-03-04 19:22:55 +02005796static const struct i915_power_well_ops hsw_power_well_ops = {
5797 .sync_hw = hsw_power_well_sync_hw,
5798 .enable = hsw_power_well_enable,
5799 .disable = hsw_power_well_disable,
5800 .is_enabled = hsw_power_well_enabled,
5801};
5802
Imre Deakc1ca7272013-11-25 17:15:29 +02005803static struct i915_power_well hsw_power_wells[] = {
5804 {
Imre Deak6f3ef5d2013-11-25 17:15:30 +02005805 .name = "always-on",
5806 .always_on = 1,
5807 .domains = HSW_ALWAYS_ON_POWER_DOMAINS,
Imre Deakc6cb5822014-03-04 19:22:55 +02005808 .ops = &i9xx_always_on_power_well_ops,
Imre Deak6f3ef5d2013-11-25 17:15:30 +02005809 },
5810 {
Imre Deakc1ca7272013-11-25 17:15:29 +02005811 .name = "display",
Imre Deakefcad912014-03-04 19:22:53 +02005812 .domains = HSW_DISPLAY_POWER_DOMAINS,
Imre Deakc6cb5822014-03-04 19:22:55 +02005813 .ops = &hsw_power_well_ops,
Imre Deakc1ca7272013-11-25 17:15:29 +02005814 },
5815};
5816
5817static struct i915_power_well bdw_power_wells[] = {
5818 {
Imre Deak6f3ef5d2013-11-25 17:15:30 +02005819 .name = "always-on",
5820 .always_on = 1,
5821 .domains = BDW_ALWAYS_ON_POWER_DOMAINS,
Imre Deakc6cb5822014-03-04 19:22:55 +02005822 .ops = &i9xx_always_on_power_well_ops,
Imre Deak6f3ef5d2013-11-25 17:15:30 +02005823 },
5824 {
Imre Deakc1ca7272013-11-25 17:15:29 +02005825 .name = "display",
Imre Deakefcad912014-03-04 19:22:53 +02005826 .domains = BDW_DISPLAY_POWER_DOMAINS,
Imre Deakc6cb5822014-03-04 19:22:55 +02005827 .ops = &hsw_power_well_ops,
Imre Deakc1ca7272013-11-25 17:15:29 +02005828 },
5829};
5830
Imre Deak77961eb2014-03-05 16:20:56 +02005831static const struct i915_power_well_ops vlv_display_power_well_ops = {
5832 .sync_hw = vlv_power_well_sync_hw,
5833 .enable = vlv_display_power_well_enable,
5834 .disable = vlv_display_power_well_disable,
5835 .is_enabled = vlv_power_well_enabled,
5836};
5837
5838static const struct i915_power_well_ops vlv_dpio_power_well_ops = {
5839 .sync_hw = vlv_power_well_sync_hw,
5840 .enable = vlv_power_well_enable,
5841 .disable = vlv_power_well_disable,
5842 .is_enabled = vlv_power_well_enabled,
5843};
5844
5845static struct i915_power_well vlv_power_wells[] = {
5846 {
5847 .name = "always-on",
5848 .always_on = 1,
5849 .domains = VLV_ALWAYS_ON_POWER_DOMAINS,
5850 .ops = &i9xx_always_on_power_well_ops,
5851 },
5852 {
5853 .name = "display",
5854 .domains = VLV_DISPLAY_POWER_DOMAINS,
5855 .data = PUNIT_POWER_WELL_DISP2D,
5856 .ops = &vlv_display_power_well_ops,
5857 },
5858 {
5859 .name = "dpio-common",
5860 .domains = VLV_DPIO_CMN_BC_POWER_DOMAINS,
5861 .data = PUNIT_POWER_WELL_DPIO_CMN_BC,
5862 .ops = &vlv_dpio_power_well_ops,
5863 },
5864 {
5865 .name = "dpio-tx-b-01",
5866 .domains = VLV_DPIO_TX_B_LANES_01_POWER_DOMAINS |
5867 VLV_DPIO_TX_B_LANES_23_POWER_DOMAINS |
5868 VLV_DPIO_TX_C_LANES_01_POWER_DOMAINS |
5869 VLV_DPIO_TX_C_LANES_23_POWER_DOMAINS,
5870 .ops = &vlv_dpio_power_well_ops,
5871 .data = PUNIT_POWER_WELL_DPIO_TX_B_LANES_01,
5872 },
5873 {
5874 .name = "dpio-tx-b-23",
5875 .domains = VLV_DPIO_TX_B_LANES_01_POWER_DOMAINS |
5876 VLV_DPIO_TX_B_LANES_23_POWER_DOMAINS |
5877 VLV_DPIO_TX_C_LANES_01_POWER_DOMAINS |
5878 VLV_DPIO_TX_C_LANES_23_POWER_DOMAINS,
5879 .ops = &vlv_dpio_power_well_ops,
5880 .data = PUNIT_POWER_WELL_DPIO_TX_B_LANES_23,
5881 },
5882 {
5883 .name = "dpio-tx-c-01",
5884 .domains = VLV_DPIO_TX_B_LANES_01_POWER_DOMAINS |
5885 VLV_DPIO_TX_B_LANES_23_POWER_DOMAINS |
5886 VLV_DPIO_TX_C_LANES_01_POWER_DOMAINS |
5887 VLV_DPIO_TX_C_LANES_23_POWER_DOMAINS,
5888 .ops = &vlv_dpio_power_well_ops,
5889 .data = PUNIT_POWER_WELL_DPIO_TX_C_LANES_01,
5890 },
5891 {
5892 .name = "dpio-tx-c-23",
5893 .domains = VLV_DPIO_TX_B_LANES_01_POWER_DOMAINS |
5894 VLV_DPIO_TX_B_LANES_23_POWER_DOMAINS |
5895 VLV_DPIO_TX_C_LANES_01_POWER_DOMAINS |
5896 VLV_DPIO_TX_C_LANES_23_POWER_DOMAINS,
5897 .ops = &vlv_dpio_power_well_ops,
5898 .data = PUNIT_POWER_WELL_DPIO_TX_C_LANES_23,
5899 },
5900};
5901
Imre Deakc1ca7272013-11-25 17:15:29 +02005902#define set_power_wells(power_domains, __power_wells) ({ \
5903 (power_domains)->power_wells = (__power_wells); \
5904 (power_domains)->power_well_count = ARRAY_SIZE(__power_wells); \
5905})
5906
Imre Deakda7e29b2014-02-18 00:02:02 +02005907int intel_power_domains_init(struct drm_i915_private *dev_priv)
Wang Xingchaoa38911a2013-05-30 22:07:11 +08005908{
Imre Deak83c00f52013-10-25 17:36:47 +03005909 struct i915_power_domains *power_domains = &dev_priv->power_domains;
Imre Deakc1ca7272013-11-25 17:15:29 +02005910
Imre Deak83c00f52013-10-25 17:36:47 +03005911 mutex_init(&power_domains->lock);
Wang Xingchaoa38911a2013-05-30 22:07:11 +08005912
Imre Deakc1ca7272013-11-25 17:15:29 +02005913 /*
5914 * The enabling order will be from lower to higher indexed wells,
5915 * the disabling order is reversed.
5916 */
Imre Deakda7e29b2014-02-18 00:02:02 +02005917 if (IS_HASWELL(dev_priv->dev)) {
Imre Deakc1ca7272013-11-25 17:15:29 +02005918 set_power_wells(power_domains, hsw_power_wells);
5919 hsw_pwr = power_domains;
Imre Deakda7e29b2014-02-18 00:02:02 +02005920 } else if (IS_BROADWELL(dev_priv->dev)) {
Imre Deakc1ca7272013-11-25 17:15:29 +02005921 set_power_wells(power_domains, bdw_power_wells);
5922 hsw_pwr = power_domains;
Imre Deak77961eb2014-03-05 16:20:56 +02005923 } else if (IS_VALLEYVIEW(dev_priv->dev)) {
5924 set_power_wells(power_domains, vlv_power_wells);
Imre Deakc1ca7272013-11-25 17:15:29 +02005925 } else {
Imre Deak1c2256d2013-11-25 17:15:34 +02005926 set_power_wells(power_domains, i9xx_always_on_power_well);
Imre Deakc1ca7272013-11-25 17:15:29 +02005927 }
Wang Xingchaoa38911a2013-05-30 22:07:11 +08005928
5929 return 0;
5930}
5931
Imre Deakda7e29b2014-02-18 00:02:02 +02005932void intel_power_domains_remove(struct drm_i915_private *dev_priv)
Wang Xingchaoa38911a2013-05-30 22:07:11 +08005933{
5934 hsw_pwr = NULL;
5935}
5936
Imre Deakda7e29b2014-02-18 00:02:02 +02005937static void intel_power_domains_resume(struct drm_i915_private *dev_priv)
Ville Syrjälä9cdb8262013-09-16 17:38:27 +03005938{
Imre Deak83c00f52013-10-25 17:36:47 +03005939 struct i915_power_domains *power_domains = &dev_priv->power_domains;
5940 struct i915_power_well *power_well;
Imre Deakc1ca7272013-11-25 17:15:29 +02005941 int i;
Ville Syrjälä9cdb8262013-09-16 17:38:27 +03005942
Imre Deak83c00f52013-10-25 17:36:47 +03005943 mutex_lock(&power_domains->lock);
Imre Deaka45f44662014-03-04 19:22:56 +02005944 for_each_power_well(i, power_well, POWER_DOMAIN_MASK, power_domains)
5945 power_well->ops->sync_hw(dev_priv, power_well);
Imre Deak83c00f52013-10-25 17:36:47 +03005946 mutex_unlock(&power_domains->lock);
Wang Xingchaoa38911a2013-05-30 22:07:11 +08005947}
5948
Imre Deakda7e29b2014-02-18 00:02:02 +02005949void intel_power_domains_init_hw(struct drm_i915_private *dev_priv)
Paulo Zanonifa42e232013-01-25 16:59:11 -02005950{
Paulo Zanonifa42e232013-01-25 16:59:11 -02005951 /* For now, we need the power well to be always enabled. */
Imre Deakda7e29b2014-02-18 00:02:02 +02005952 intel_display_set_init_power(dev_priv, true);
5953 intel_power_domains_resume(dev_priv);
Eugeni Dodonovd0d3e512012-05-09 15:37:16 -03005954}
5955
Paulo Zanonic67a4702013-08-19 13:18:09 -03005956void intel_aux_display_runtime_get(struct drm_i915_private *dev_priv)
5957{
Paulo Zanonid361ae22014-03-07 20:08:12 -03005958 intel_runtime_pm_get(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -03005959}
5960
5961void intel_aux_display_runtime_put(struct drm_i915_private *dev_priv)
5962{
Paulo Zanonid361ae22014-03-07 20:08:12 -03005963 intel_runtime_pm_put(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -03005964}
5965
Paulo Zanoni8a187452013-12-06 20:32:13 -02005966void intel_runtime_pm_get(struct drm_i915_private *dev_priv)
5967{
5968 struct drm_device *dev = dev_priv->dev;
5969 struct device *device = &dev->pdev->dev;
5970
5971 if (!HAS_RUNTIME_PM(dev))
5972 return;
5973
5974 pm_runtime_get_sync(device);
5975 WARN(dev_priv->pm.suspended, "Device still suspended.\n");
5976}
5977
5978void intel_runtime_pm_put(struct drm_i915_private *dev_priv)
5979{
5980 struct drm_device *dev = dev_priv->dev;
5981 struct device *device = &dev->pdev->dev;
5982
5983 if (!HAS_RUNTIME_PM(dev))
5984 return;
5985
5986 pm_runtime_mark_last_busy(device);
5987 pm_runtime_put_autosuspend(device);
5988}
5989
5990void intel_init_runtime_pm(struct drm_i915_private *dev_priv)
5991{
5992 struct drm_device *dev = dev_priv->dev;
5993 struct device *device = &dev->pdev->dev;
5994
Paulo Zanoni8a187452013-12-06 20:32:13 -02005995 if (!HAS_RUNTIME_PM(dev))
5996 return;
5997
5998 pm_runtime_set_active(device);
5999
6000 pm_runtime_set_autosuspend_delay(device, 10000); /* 10s */
6001 pm_runtime_mark_last_busy(device);
6002 pm_runtime_use_autosuspend(device);
Paulo Zanoniba0239e2014-03-07 20:08:07 -03006003
6004 pm_runtime_put_autosuspend(device);
Paulo Zanoni8a187452013-12-06 20:32:13 -02006005}
6006
6007void intel_fini_runtime_pm(struct drm_i915_private *dev_priv)
6008{
6009 struct drm_device *dev = dev_priv->dev;
6010 struct device *device = &dev->pdev->dev;
6011
6012 if (!HAS_RUNTIME_PM(dev))
6013 return;
6014
6015 /* Make sure we're not suspended first. */
6016 pm_runtime_get_sync(device);
6017 pm_runtime_disable(device);
6018}
6019
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006020/* Set up chip specific power management-related functions */
6021void intel_init_pm(struct drm_device *dev)
6022{
6023 struct drm_i915_private *dev_priv = dev->dev_private;
6024
Daniel Vetter3a77c4c2014-01-10 08:50:12 +01006025 if (HAS_FBC(dev)) {
Ville Syrjälä40045462013-11-28 17:29:59 +02006026 if (INTEL_INFO(dev)->gen >= 7) {
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006027 dev_priv->display.fbc_enabled = ironlake_fbc_enabled;
Ville Syrjälä40045462013-11-28 17:29:59 +02006028 dev_priv->display.enable_fbc = gen7_enable_fbc;
6029 dev_priv->display.disable_fbc = ironlake_disable_fbc;
6030 } else if (INTEL_INFO(dev)->gen >= 5) {
6031 dev_priv->display.fbc_enabled = ironlake_fbc_enabled;
6032 dev_priv->display.enable_fbc = ironlake_enable_fbc;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006033 dev_priv->display.disable_fbc = ironlake_disable_fbc;
6034 } else if (IS_GM45(dev)) {
6035 dev_priv->display.fbc_enabled = g4x_fbc_enabled;
6036 dev_priv->display.enable_fbc = g4x_enable_fbc;
6037 dev_priv->display.disable_fbc = g4x_disable_fbc;
Ville Syrjälä40045462013-11-28 17:29:59 +02006038 } else {
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006039 dev_priv->display.fbc_enabled = i8xx_fbc_enabled;
6040 dev_priv->display.enable_fbc = i8xx_enable_fbc;
6041 dev_priv->display.disable_fbc = i8xx_disable_fbc;
Ville Syrjälä993495a2013-12-12 17:27:40 +02006042
6043 /* This value was pulled out of someone's hat */
6044 I915_WRITE(FBC_CONTROL, 500 << FBC_CTL_INTERVAL_SHIFT);
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006045 }
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006046 }
6047
Daniel Vetterc921aba2012-04-26 23:28:17 +02006048 /* For cxsr */
6049 if (IS_PINEVIEW(dev))
6050 i915_pineview_get_mem_freq(dev);
6051 else if (IS_GEN5(dev))
6052 i915_ironlake_get_mem_freq(dev);
6053
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006054 /* For FIFO watermark updates */
6055 if (HAS_PCH_SPLIT(dev)) {
Damien Lespiaufa50ad62014-03-17 18:01:16 +00006056 ilk_setup_wm_latency(dev);
Ville Syrjälä53615a52013-08-01 16:18:50 +03006057
Ville Syrjäläbd602542014-01-07 16:14:10 +02006058 if ((IS_GEN5(dev) && dev_priv->wm.pri_latency[1] &&
6059 dev_priv->wm.spr_latency[1] && dev_priv->wm.cur_latency[1]) ||
6060 (!IS_GEN5(dev) && dev_priv->wm.pri_latency[0] &&
6061 dev_priv->wm.spr_latency[0] && dev_priv->wm.cur_latency[0])) {
6062 dev_priv->display.update_wm = ilk_update_wm;
6063 dev_priv->display.update_sprite_wm = ilk_update_sprite_wm;
6064 } else {
6065 DRM_DEBUG_KMS("Failed to read display plane latency. "
6066 "Disable CxSR\n");
6067 }
6068
6069 if (IS_GEN5(dev))
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006070 dev_priv->display.init_clock_gating = ironlake_init_clock_gating;
Ville Syrjäläbd602542014-01-07 16:14:10 +02006071 else if (IS_GEN6(dev))
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006072 dev_priv->display.init_clock_gating = gen6_init_clock_gating;
Ville Syrjäläbd602542014-01-07 16:14:10 +02006073 else if (IS_IVYBRIDGE(dev))
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006074 dev_priv->display.init_clock_gating = ivybridge_init_clock_gating;
Ville Syrjäläbd602542014-01-07 16:14:10 +02006075 else if (IS_HASWELL(dev))
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03006076 dev_priv->display.init_clock_gating = haswell_init_clock_gating;
Ville Syrjäläbd602542014-01-07 16:14:10 +02006077 else if (INTEL_INFO(dev)->gen == 8)
Ben Widawsky1020a5c2013-11-02 21:07:06 -07006078 dev_priv->display.init_clock_gating = gen8_init_clock_gating;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006079 } else if (IS_VALLEYVIEW(dev)) {
6080 dev_priv->display.update_wm = valleyview_update_wm;
6081 dev_priv->display.init_clock_gating =
6082 valleyview_init_clock_gating;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006083 } else if (IS_PINEVIEW(dev)) {
6084 if (!intel_get_cxsr_latency(IS_PINEVIEW_G(dev),
6085 dev_priv->is_ddr3,
6086 dev_priv->fsb_freq,
6087 dev_priv->mem_freq)) {
6088 DRM_INFO("failed to find known CxSR latency "
6089 "(found ddr%s fsb freq %d, mem freq %d), "
6090 "disabling CxSR\n",
6091 (dev_priv->is_ddr3 == 1) ? "3" : "2",
6092 dev_priv->fsb_freq, dev_priv->mem_freq);
6093 /* Disable CxSR and never update its watermark again */
6094 pineview_disable_cxsr(dev);
6095 dev_priv->display.update_wm = NULL;
6096 } else
6097 dev_priv->display.update_wm = pineview_update_wm;
6098 dev_priv->display.init_clock_gating = gen3_init_clock_gating;
6099 } else if (IS_G4X(dev)) {
6100 dev_priv->display.update_wm = g4x_update_wm;
6101 dev_priv->display.init_clock_gating = g4x_init_clock_gating;
6102 } else if (IS_GEN4(dev)) {
6103 dev_priv->display.update_wm = i965_update_wm;
6104 if (IS_CRESTLINE(dev))
6105 dev_priv->display.init_clock_gating = crestline_init_clock_gating;
6106 else if (IS_BROADWATER(dev))
6107 dev_priv->display.init_clock_gating = broadwater_init_clock_gating;
6108 } else if (IS_GEN3(dev)) {
6109 dev_priv->display.update_wm = i9xx_update_wm;
6110 dev_priv->display.get_fifo_size = i9xx_get_fifo_size;
6111 dev_priv->display.init_clock_gating = gen3_init_clock_gating;
Daniel Vetterfeb56b92013-12-14 20:38:30 -02006112 } else if (IS_GEN2(dev)) {
6113 if (INTEL_INFO(dev)->num_pipes == 1) {
6114 dev_priv->display.update_wm = i845_update_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006115 dev_priv->display.get_fifo_size = i845_get_fifo_size;
Daniel Vetterfeb56b92013-12-14 20:38:30 -02006116 } else {
6117 dev_priv->display.update_wm = i9xx_update_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006118 dev_priv->display.get_fifo_size = i830_get_fifo_size;
Daniel Vetterfeb56b92013-12-14 20:38:30 -02006119 }
6120
6121 if (IS_I85X(dev) || IS_I865G(dev))
6122 dev_priv->display.init_clock_gating = i85x_init_clock_gating;
6123 else
6124 dev_priv->display.init_clock_gating = i830_init_clock_gating;
6125 } else {
6126 DRM_ERROR("unexpected fall-through in intel_init_pm\n");
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006127 }
6128}
6129
Ben Widawsky42c05262012-09-26 10:34:00 -07006130int sandybridge_pcode_read(struct drm_i915_private *dev_priv, u8 mbox, u32 *val)
6131{
Jesse Barnes4fc688c2012-11-02 11:14:01 -07006132 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Ben Widawsky42c05262012-09-26 10:34:00 -07006133
6134 if (I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) {
6135 DRM_DEBUG_DRIVER("warning: pcode (read) mailbox access failed\n");
6136 return -EAGAIN;
6137 }
6138
6139 I915_WRITE(GEN6_PCODE_DATA, *val);
6140 I915_WRITE(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY | mbox);
6141
6142 if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
6143 500)) {
6144 DRM_ERROR("timeout waiting for pcode read (%d) to finish\n", mbox);
6145 return -ETIMEDOUT;
6146 }
6147
6148 *val = I915_READ(GEN6_PCODE_DATA);
6149 I915_WRITE(GEN6_PCODE_DATA, 0);
6150
6151 return 0;
6152}
6153
6154int sandybridge_pcode_write(struct drm_i915_private *dev_priv, u8 mbox, u32 val)
6155{
Jesse Barnes4fc688c2012-11-02 11:14:01 -07006156 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Ben Widawsky42c05262012-09-26 10:34:00 -07006157
6158 if (I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) {
6159 DRM_DEBUG_DRIVER("warning: pcode (write) mailbox access failed\n");
6160 return -EAGAIN;
6161 }
6162
6163 I915_WRITE(GEN6_PCODE_DATA, val);
6164 I915_WRITE(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY | mbox);
6165
6166 if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
6167 500)) {
6168 DRM_ERROR("timeout waiting for pcode write (%d) to finish\n", mbox);
6169 return -ETIMEDOUT;
6170 }
6171
6172 I915_WRITE(GEN6_PCODE_DATA, 0);
6173
6174 return 0;
6175}
Jesse Barnesa0e4e192013-04-02 11:23:05 -07006176
Ville Syrjälä2ec38152013-11-05 22:42:29 +02006177int vlv_gpu_freq(struct drm_i915_private *dev_priv, int val)
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006178{
Ville Syrjälä07ab1182013-11-05 22:42:28 +02006179 int div;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006180
Ville Syrjälä07ab1182013-11-05 22:42:28 +02006181 /* 4 x czclk */
Ville Syrjälä2ec38152013-11-05 22:42:29 +02006182 switch (dev_priv->mem_freq) {
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006183 case 800:
Ville Syrjälä07ab1182013-11-05 22:42:28 +02006184 div = 10;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006185 break;
6186 case 1066:
Ville Syrjälä07ab1182013-11-05 22:42:28 +02006187 div = 12;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006188 break;
6189 case 1333:
Ville Syrjälä07ab1182013-11-05 22:42:28 +02006190 div = 16;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006191 break;
6192 default:
6193 return -1;
6194 }
6195
Ville Syrjälä2ec38152013-11-05 22:42:29 +02006196 return DIV_ROUND_CLOSEST(dev_priv->mem_freq * (val + 6 - 0xbd), 4 * div);
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006197}
6198
Ville Syrjälä2ec38152013-11-05 22:42:29 +02006199int vlv_freq_opcode(struct drm_i915_private *dev_priv, int val)
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006200{
Ville Syrjälä07ab1182013-11-05 22:42:28 +02006201 int mul;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006202
Ville Syrjälä07ab1182013-11-05 22:42:28 +02006203 /* 4 x czclk */
Ville Syrjälä2ec38152013-11-05 22:42:29 +02006204 switch (dev_priv->mem_freq) {
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006205 case 800:
Ville Syrjälä07ab1182013-11-05 22:42:28 +02006206 mul = 10;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006207 break;
6208 case 1066:
Ville Syrjälä07ab1182013-11-05 22:42:28 +02006209 mul = 12;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006210 break;
6211 case 1333:
Ville Syrjälä07ab1182013-11-05 22:42:28 +02006212 mul = 16;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006213 break;
6214 default:
6215 return -1;
6216 }
6217
Ville Syrjälä2ec38152013-11-05 22:42:29 +02006218 return DIV_ROUND_CLOSEST(4 * mul * val, dev_priv->mem_freq) + 0xbd - 6;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006219}
6220
Daniel Vetterf742a552013-12-06 10:17:53 +01006221void intel_pm_setup(struct drm_device *dev)
Chris Wilson907b28c2013-07-19 20:36:52 +01006222{
6223 struct drm_i915_private *dev_priv = dev->dev_private;
6224
Daniel Vetterf742a552013-12-06 10:17:53 +01006225 mutex_init(&dev_priv->rps.hw_lock);
6226
Chris Wilson907b28c2013-07-19 20:36:52 +01006227 INIT_DELAYED_WORK(&dev_priv->rps.delayed_resume_work,
6228 intel_gen6_powersave_work);
Paulo Zanoni5d584b22014-03-07 20:08:15 -03006229
Paulo Zanoni33688d92014-03-07 20:08:19 -03006230 dev_priv->pm.suspended = false;
Paulo Zanoni5d584b22014-03-07 20:08:15 -03006231 dev_priv->pm.irqs_disabled = false;
Chris Wilson907b28c2013-07-19 20:36:52 +01006232}