blob: be2d5bf7f539104b2b0807bb720f52df584bb70a [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;
Matt Roper2ff8fde2014-07-08 07:50:07 -070096 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Eugeni Dodonov85208be2012-04-16 22:20:34 -030097 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
98 int cfb_pitch;
Ville Syrjälä7f2cf222014-01-23 16:49:11 +020099 int i;
Ville Syrjälä159f9872013-11-28 17:29:57 +0200100 u32 fbc_ctl;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300101
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -0700102 cfb_pitch = dev_priv->fbc.size / FBC_LL_SIZE;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300103 if (fb->pitches[0] < cfb_pitch)
104 cfb_pitch = fb->pitches[0];
105
Ville Syrjälä42a430f2013-11-28 17:29:56 +0200106 /* FBC_CTL wants 32B or 64B units */
107 if (IS_GEN2(dev))
108 cfb_pitch = (cfb_pitch / 32) - 1;
109 else
110 cfb_pitch = (cfb_pitch / 64) - 1;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300111
112 /* Clear old tags */
113 for (i = 0; i < (FBC_LL_SIZE / 32) + 1; i++)
114 I915_WRITE(FBC_TAG + (i * 4), 0);
115
Ville Syrjälä159f9872013-11-28 17:29:57 +0200116 if (IS_GEN4(dev)) {
117 u32 fbc_ctl2;
118
119 /* Set it up... */
120 fbc_ctl2 = FBC_CTL_FENCE_DBL | FBC_CTL_IDLE_IMM | FBC_CTL_CPU_FENCE;
Ville Syrjälä7f2cf222014-01-23 16:49:11 +0200121 fbc_ctl2 |= FBC_CTL_PLANE(intel_crtc->plane);
Ville Syrjälä159f9872013-11-28 17:29:57 +0200122 I915_WRITE(FBC_CONTROL2, fbc_ctl2);
123 I915_WRITE(FBC_FENCE_OFF, crtc->y);
124 }
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300125
126 /* enable it... */
Ville Syrjälä993495a2013-12-12 17:27:40 +0200127 fbc_ctl = I915_READ(FBC_CONTROL);
128 fbc_ctl &= 0x3fff << FBC_CTL_INTERVAL_SHIFT;
129 fbc_ctl |= FBC_CTL_EN | FBC_CTL_PERIODIC;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300130 if (IS_I945GM(dev))
131 fbc_ctl |= FBC_CTL_C3_IDLE; /* 945 needs special SR handling */
132 fbc_ctl |= (cfb_pitch & 0xff) << FBC_CTL_STRIDE_SHIFT;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300133 fbc_ctl |= obj->fence_reg;
134 I915_WRITE(FBC_CONTROL, fbc_ctl);
135
Ville Syrjälä5cd54102014-01-23 16:49:16 +0200136 DRM_DEBUG_KMS("enabled FBC, pitch %d, yoff %d, plane %c\n",
Ville Syrjälä84f44ce2013-04-17 17:48:49 +0300137 cfb_pitch, crtc->y, plane_name(intel_crtc->plane));
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300138}
139
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300140static bool i8xx_fbc_enabled(struct drm_device *dev)
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300141{
142 struct drm_i915_private *dev_priv = dev->dev_private;
143
144 return I915_READ(FBC_CONTROL) & FBC_CTL_EN;
145}
146
Ville Syrjälä993495a2013-12-12 17:27:40 +0200147static void g4x_enable_fbc(struct drm_crtc *crtc)
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300148{
149 struct drm_device *dev = crtc->dev;
150 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roperf4510a22014-04-01 15:22:40 -0700151 struct drm_framebuffer *fb = crtc->primary->fb;
Matt Roper2ff8fde2014-07-08 07:50:07 -0700152 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300153 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300154 u32 dpfc_ctl;
155
Ville Syrjälä3fa2e0e2014-01-23 16:49:12 +0200156 dpfc_ctl = DPFC_CTL_PLANE(intel_crtc->plane) | DPFC_SR_EN;
157 if (drm_format_plane_cpp(fb->pixel_format, 0) == 2)
158 dpfc_ctl |= DPFC_CTL_LIMIT_2X;
159 else
160 dpfc_ctl |= DPFC_CTL_LIMIT_1X;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300161 dpfc_ctl |= DPFC_CTL_FENCE_EN | obj->fence_reg;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300162
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300163 I915_WRITE(DPFC_FENCE_YOFF, crtc->y);
164
165 /* enable it... */
Ville Syrjäläfe74c1a2014-01-23 16:49:13 +0200166 I915_WRITE(DPFC_CONTROL, dpfc_ctl | DPFC_CTL_EN);
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300167
Ville Syrjälä84f44ce2013-04-17 17:48:49 +0300168 DRM_DEBUG_KMS("enabled fbc on plane %c\n", plane_name(intel_crtc->plane));
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300169}
170
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300171static void g4x_disable_fbc(struct drm_device *dev)
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300172{
173 struct drm_i915_private *dev_priv = dev->dev_private;
174 u32 dpfc_ctl;
175
176 /* Disable compression */
177 dpfc_ctl = I915_READ(DPFC_CONTROL);
178 if (dpfc_ctl & DPFC_CTL_EN) {
179 dpfc_ctl &= ~DPFC_CTL_EN;
180 I915_WRITE(DPFC_CONTROL, dpfc_ctl);
181
182 DRM_DEBUG_KMS("disabled FBC\n");
183 }
184}
185
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300186static bool g4x_fbc_enabled(struct drm_device *dev)
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300187{
188 struct drm_i915_private *dev_priv = dev->dev_private;
189
190 return I915_READ(DPFC_CONTROL) & DPFC_CTL_EN;
191}
192
193static void sandybridge_blit_fbc_update(struct drm_device *dev)
194{
195 struct drm_i915_private *dev_priv = dev->dev_private;
196 u32 blt_ecoskpd;
197
198 /* Make sure blitter notifies FBC of writes */
Deepak S940aece2013-11-23 14:55:43 +0530199
200 /* Blitter is part of Media powerwell on VLV. No impact of
201 * his param in other platforms for now */
202 gen6_gt_force_wake_get(dev_priv, FORCEWAKE_MEDIA);
Deepak Sc8d9a592013-11-23 14:55:42 +0530203
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300204 blt_ecoskpd = I915_READ(GEN6_BLITTER_ECOSKPD);
205 blt_ecoskpd |= GEN6_BLITTER_FBC_NOTIFY <<
206 GEN6_BLITTER_LOCK_SHIFT;
207 I915_WRITE(GEN6_BLITTER_ECOSKPD, blt_ecoskpd);
208 blt_ecoskpd |= GEN6_BLITTER_FBC_NOTIFY;
209 I915_WRITE(GEN6_BLITTER_ECOSKPD, blt_ecoskpd);
210 blt_ecoskpd &= ~(GEN6_BLITTER_FBC_NOTIFY <<
211 GEN6_BLITTER_LOCK_SHIFT);
212 I915_WRITE(GEN6_BLITTER_ECOSKPD, blt_ecoskpd);
213 POSTING_READ(GEN6_BLITTER_ECOSKPD);
Deepak Sc8d9a592013-11-23 14:55:42 +0530214
Deepak S940aece2013-11-23 14:55:43 +0530215 gen6_gt_force_wake_put(dev_priv, FORCEWAKE_MEDIA);
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300216}
217
Ville Syrjälä993495a2013-12-12 17:27:40 +0200218static void ironlake_enable_fbc(struct drm_crtc *crtc)
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300219{
220 struct drm_device *dev = crtc->dev;
221 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roperf4510a22014-04-01 15:22:40 -0700222 struct drm_framebuffer *fb = crtc->primary->fb;
Matt Roper2ff8fde2014-07-08 07:50:07 -0700223 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300224 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300225 u32 dpfc_ctl;
226
Ville Syrjälä46f3dab2014-01-23 16:49:14 +0200227 dpfc_ctl = DPFC_CTL_PLANE(intel_crtc->plane);
Ville Syrjälä3fa2e0e2014-01-23 16:49:12 +0200228 if (drm_format_plane_cpp(fb->pixel_format, 0) == 2)
Ben Widawsky5e59f712014-06-30 10:41:24 -0700229 dev_priv->fbc.threshold++;
230
231 switch (dev_priv->fbc.threshold) {
232 case 4:
233 case 3:
234 dpfc_ctl |= DPFC_CTL_LIMIT_4X;
235 break;
236 case 2:
Ville Syrjälä3fa2e0e2014-01-23 16:49:12 +0200237 dpfc_ctl |= DPFC_CTL_LIMIT_2X;
Ben Widawsky5e59f712014-06-30 10:41:24 -0700238 break;
239 case 1:
Ville Syrjälä3fa2e0e2014-01-23 16:49:12 +0200240 dpfc_ctl |= DPFC_CTL_LIMIT_1X;
Ben Widawsky5e59f712014-06-30 10:41:24 -0700241 break;
242 }
Ville Syrjäläd6293362013-11-21 21:29:45 +0200243 dpfc_ctl |= DPFC_CTL_FENCE_EN;
244 if (IS_GEN5(dev))
245 dpfc_ctl |= obj->fence_reg;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300246
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300247 I915_WRITE(ILK_DPFC_FENCE_YOFF, crtc->y);
Ben Widawskyf343c5f2013-07-05 14:41:04 -0700248 I915_WRITE(ILK_FBC_RT_BASE, i915_gem_obj_ggtt_offset(obj) | ILK_FBC_RT_VALID);
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300249 /* enable it... */
250 I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl | DPFC_CTL_EN);
251
252 if (IS_GEN6(dev)) {
253 I915_WRITE(SNB_DPFC_CTL_SA,
254 SNB_CPU_FENCE_ENABLE | obj->fence_reg);
255 I915_WRITE(DPFC_CPU_FENCE_OFFSET, crtc->y);
256 sandybridge_blit_fbc_update(dev);
257 }
258
Ville Syrjälä84f44ce2013-04-17 17:48:49 +0300259 DRM_DEBUG_KMS("enabled fbc on plane %c\n", plane_name(intel_crtc->plane));
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300260}
261
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300262static void ironlake_disable_fbc(struct drm_device *dev)
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300263{
264 struct drm_i915_private *dev_priv = dev->dev_private;
265 u32 dpfc_ctl;
266
267 /* Disable compression */
268 dpfc_ctl = I915_READ(ILK_DPFC_CONTROL);
269 if (dpfc_ctl & DPFC_CTL_EN) {
270 dpfc_ctl &= ~DPFC_CTL_EN;
271 I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl);
272
273 DRM_DEBUG_KMS("disabled FBC\n");
274 }
275}
276
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300277static bool ironlake_fbc_enabled(struct drm_device *dev)
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300278{
279 struct drm_i915_private *dev_priv = dev->dev_private;
280
281 return I915_READ(ILK_DPFC_CONTROL) & DPFC_CTL_EN;
282}
283
Ville Syrjälä993495a2013-12-12 17:27:40 +0200284static void gen7_enable_fbc(struct drm_crtc *crtc)
Rodrigo Viviabe959c2013-05-06 19:37:33 -0300285{
286 struct drm_device *dev = crtc->dev;
287 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roperf4510a22014-04-01 15:22:40 -0700288 struct drm_framebuffer *fb = crtc->primary->fb;
Matt Roper2ff8fde2014-07-08 07:50:07 -0700289 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Rodrigo Viviabe959c2013-05-06 19:37:33 -0300290 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjälä3fa2e0e2014-01-23 16:49:12 +0200291 u32 dpfc_ctl;
Rodrigo Viviabe959c2013-05-06 19:37:33 -0300292
Ville Syrjälä3fa2e0e2014-01-23 16:49:12 +0200293 dpfc_ctl = IVB_DPFC_CTL_PLANE(intel_crtc->plane);
294 if (drm_format_plane_cpp(fb->pixel_format, 0) == 2)
Ben Widawsky5e59f712014-06-30 10:41:24 -0700295 dev_priv->fbc.threshold++;
296
297 switch (dev_priv->fbc.threshold) {
298 case 4:
299 case 3:
300 dpfc_ctl |= DPFC_CTL_LIMIT_4X;
301 break;
302 case 2:
Ville Syrjälä3fa2e0e2014-01-23 16:49:12 +0200303 dpfc_ctl |= DPFC_CTL_LIMIT_2X;
Ben Widawsky5e59f712014-06-30 10:41:24 -0700304 break;
305 case 1:
Ville Syrjälä3fa2e0e2014-01-23 16:49:12 +0200306 dpfc_ctl |= DPFC_CTL_LIMIT_1X;
Ben Widawsky5e59f712014-06-30 10:41:24 -0700307 break;
308 }
309
Ville Syrjälä3fa2e0e2014-01-23 16:49:12 +0200310 dpfc_ctl |= IVB_DPFC_CTL_FENCE_EN;
311
312 I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl | DPFC_CTL_EN);
Rodrigo Viviabe959c2013-05-06 19:37:33 -0300313
Rodrigo Vivi891348b2013-05-06 19:37:36 -0300314 if (IS_IVYBRIDGE(dev)) {
Damien Lespiau7dd23ba2013-05-10 14:33:17 +0100315 /* WaFbcAsynchFlipDisableFbcQueue:ivb */
Ville Syrjälä2adb6db2014-03-05 13:05:46 +0200316 I915_WRITE(ILK_DISPLAY_CHICKEN1,
317 I915_READ(ILK_DISPLAY_CHICKEN1) |
318 ILK_FBCQ_DIS);
Rodrigo Vivi28554162013-05-06 19:37:37 -0300319 } else {
Ville Syrjälä2adb6db2014-03-05 13:05:46 +0200320 /* WaFbcAsynchFlipDisableFbcQueue:hsw,bdw */
Ville Syrjälä8f670bb2014-03-05 13:05:47 +0200321 I915_WRITE(CHICKEN_PIPESL_1(intel_crtc->pipe),
322 I915_READ(CHICKEN_PIPESL_1(intel_crtc->pipe)) |
323 HSW_FBCQ_DIS);
Rodrigo Vivi891348b2013-05-06 19:37:36 -0300324 }
Rodrigo Vivib74ea102013-05-09 14:08:38 -0300325
Rodrigo Viviabe959c2013-05-06 19:37:33 -0300326 I915_WRITE(SNB_DPFC_CTL_SA,
327 SNB_CPU_FENCE_ENABLE | obj->fence_reg);
328 I915_WRITE(DPFC_CPU_FENCE_OFFSET, crtc->y);
329
330 sandybridge_blit_fbc_update(dev);
331
Ville Syrjäläb19870e2013-11-06 23:02:25 +0200332 DRM_DEBUG_KMS("enabled fbc on plane %c\n", plane_name(intel_crtc->plane));
Rodrigo Viviabe959c2013-05-06 19:37:33 -0300333}
334
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300335bool intel_fbc_enabled(struct drm_device *dev)
336{
337 struct drm_i915_private *dev_priv = dev->dev_private;
338
339 if (!dev_priv->display.fbc_enabled)
340 return false;
341
342 return dev_priv->display.fbc_enabled(dev);
343}
344
345static void intel_fbc_work_fn(struct work_struct *__work)
346{
347 struct intel_fbc_work *work =
348 container_of(to_delayed_work(__work),
349 struct intel_fbc_work, work);
350 struct drm_device *dev = work->crtc->dev;
351 struct drm_i915_private *dev_priv = dev->dev_private;
352
353 mutex_lock(&dev->struct_mutex);
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -0700354 if (work == dev_priv->fbc.fbc_work) {
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300355 /* Double check that we haven't switched fb without cancelling
356 * the prior work.
357 */
Matt Roperf4510a22014-04-01 15:22:40 -0700358 if (work->crtc->primary->fb == work->fb) {
Ville Syrjälä993495a2013-12-12 17:27:40 +0200359 dev_priv->display.enable_fbc(work->crtc);
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300360
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -0700361 dev_priv->fbc.plane = to_intel_crtc(work->crtc)->plane;
Matt Roperf4510a22014-04-01 15:22:40 -0700362 dev_priv->fbc.fb_id = work->crtc->primary->fb->base.id;
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -0700363 dev_priv->fbc.y = work->crtc->y;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300364 }
365
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -0700366 dev_priv->fbc.fbc_work = NULL;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300367 }
368 mutex_unlock(&dev->struct_mutex);
369
370 kfree(work);
371}
372
373static void intel_cancel_fbc_work(struct drm_i915_private *dev_priv)
374{
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -0700375 if (dev_priv->fbc.fbc_work == NULL)
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300376 return;
377
378 DRM_DEBUG_KMS("cancelling pending FBC enable\n");
379
380 /* Synchronisation is provided by struct_mutex and checking of
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -0700381 * dev_priv->fbc.fbc_work, so we can perform the cancellation
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300382 * entirely asynchronously.
383 */
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -0700384 if (cancel_delayed_work(&dev_priv->fbc.fbc_work->work))
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300385 /* tasklet was killed before being run, clean up */
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -0700386 kfree(dev_priv->fbc.fbc_work);
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300387
388 /* Mark the work as no longer wanted so that if it does
389 * wake-up (because the work was already running and waiting
390 * for our mutex), it will discover that is no longer
391 * necessary to run.
392 */
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -0700393 dev_priv->fbc.fbc_work = NULL;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300394}
395
Ville Syrjälä993495a2013-12-12 17:27:40 +0200396static void intel_enable_fbc(struct drm_crtc *crtc)
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300397{
398 struct intel_fbc_work *work;
399 struct drm_device *dev = crtc->dev;
400 struct drm_i915_private *dev_priv = dev->dev_private;
401
402 if (!dev_priv->display.enable_fbc)
403 return;
404
405 intel_cancel_fbc_work(dev_priv);
406
Daniel Vetterb14c5672013-09-19 12:18:32 +0200407 work = kzalloc(sizeof(*work), GFP_KERNEL);
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300408 if (work == NULL) {
Paulo Zanoni6cdcb5e2013-06-12 17:27:29 -0300409 DRM_ERROR("Failed to allocate FBC work structure\n");
Ville Syrjälä993495a2013-12-12 17:27:40 +0200410 dev_priv->display.enable_fbc(crtc);
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300411 return;
412 }
413
414 work->crtc = crtc;
Matt Roperf4510a22014-04-01 15:22:40 -0700415 work->fb = crtc->primary->fb;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300416 INIT_DELAYED_WORK(&work->work, intel_fbc_work_fn);
417
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -0700418 dev_priv->fbc.fbc_work = work;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300419
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300420 /* Delay the actual enabling to let pageflipping cease and the
421 * display to settle before starting the compression. Note that
422 * this delay also serves a second purpose: it allows for a
423 * vblank to pass after disabling the FBC before we attempt
424 * to modify the control registers.
425 *
426 * A more complicated solution would involve tracking vblanks
427 * following the termination of the page-flipping sequence
428 * and indeed performing the enable as a co-routine and not
429 * waiting synchronously upon the vblank.
Damien Lespiau7457d612013-06-07 17:41:07 +0100430 *
431 * WaFbcWaitForVBlankBeforeEnable:ilk,snb
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300432 */
433 schedule_delayed_work(&work->work, msecs_to_jiffies(50));
434}
435
436void intel_disable_fbc(struct drm_device *dev)
437{
438 struct drm_i915_private *dev_priv = dev->dev_private;
439
440 intel_cancel_fbc_work(dev_priv);
441
442 if (!dev_priv->display.disable_fbc)
443 return;
444
445 dev_priv->display.disable_fbc(dev);
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -0700446 dev_priv->fbc.plane = -1;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300447}
448
Chris Wilson29ebf902013-07-27 17:23:55 +0100449static bool set_no_fbc_reason(struct drm_i915_private *dev_priv,
450 enum no_fbc_reason reason)
451{
452 if (dev_priv->fbc.no_fbc_reason == reason)
453 return false;
454
455 dev_priv->fbc.no_fbc_reason = reason;
456 return true;
457}
458
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300459/**
460 * intel_update_fbc - enable/disable FBC as needed
461 * @dev: the drm_device
462 *
463 * Set up the framebuffer compression hardware at mode set time. We
464 * enable it if possible:
465 * - plane A only (on pre-965)
466 * - no pixel mulitply/line duplication
467 * - no alpha buffer discard
468 * - no dual wide
Paulo Zanonif85da862013-06-04 16:53:39 -0300469 * - framebuffer <= max_hdisplay in width, max_vdisplay in height
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300470 *
471 * We can't assume that any compression will take place (worst case),
472 * so the compressed buffer has to be the same size as the uncompressed
473 * one. It also must reside (along with the line length buffer) in
474 * stolen memory.
475 *
476 * We need to enable/disable FBC on a global basis.
477 */
478void intel_update_fbc(struct drm_device *dev)
479{
480 struct drm_i915_private *dev_priv = dev->dev_private;
481 struct drm_crtc *crtc = NULL, *tmp_crtc;
482 struct intel_crtc *intel_crtc;
483 struct drm_framebuffer *fb;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300484 struct drm_i915_gem_object *obj;
Ville Syrjäläef644fd2013-09-04 18:25:21 +0300485 const struct drm_display_mode *adjusted_mode;
Ville Syrjälä37327ab2013-09-04 18:25:28 +0300486 unsigned int max_width, max_height;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300487
Daniel Vetter3a77c4c2014-01-10 08:50:12 +0100488 if (!HAS_FBC(dev)) {
Chris Wilson29ebf902013-07-27 17:23:55 +0100489 set_no_fbc_reason(dev_priv, FBC_UNSUPPORTED);
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300490 return;
Chris Wilson29ebf902013-07-27 17:23:55 +0100491 }
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300492
Jani Nikulad330a952014-01-21 11:24:25 +0200493 if (!i915.powersave) {
Chris Wilson29ebf902013-07-27 17:23:55 +0100494 if (set_no_fbc_reason(dev_priv, FBC_MODULE_PARAM))
495 DRM_DEBUG_KMS("fbc disabled per module param\n");
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300496 return;
Chris Wilson29ebf902013-07-27 17:23:55 +0100497 }
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300498
499 /*
500 * If FBC is already on, we just have to verify that we can
501 * keep it that way...
502 * Need to disable if:
503 * - more than one pipe is active
504 * - changing FBC params (stride, fence, mode)
505 * - new fb is too large to fit in compressed buffer
506 * - going to an unsupported config (interlace, pixel multiply, etc.)
507 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +0100508 for_each_crtc(dev, tmp_crtc) {
Chris Wilson3490ea52013-01-07 10:11:40 +0000509 if (intel_crtc_active(tmp_crtc) &&
Ville Syrjälä4c445e02013-10-09 17:24:58 +0300510 to_intel_crtc(tmp_crtc)->primary_enabled) {
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300511 if (crtc) {
Chris Wilson29ebf902013-07-27 17:23:55 +0100512 if (set_no_fbc_reason(dev_priv, FBC_MULTIPLE_PIPES))
513 DRM_DEBUG_KMS("more than one pipe active, disabling compression\n");
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300514 goto out_disable;
515 }
516 crtc = tmp_crtc;
517 }
518 }
519
Matt Roperf4510a22014-04-01 15:22:40 -0700520 if (!crtc || crtc->primary->fb == NULL) {
Chris Wilson29ebf902013-07-27 17:23:55 +0100521 if (set_no_fbc_reason(dev_priv, FBC_NO_OUTPUT))
522 DRM_DEBUG_KMS("no output, disabling\n");
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300523 goto out_disable;
524 }
525
526 intel_crtc = to_intel_crtc(crtc);
Matt Roperf4510a22014-04-01 15:22:40 -0700527 fb = crtc->primary->fb;
Matt Roper2ff8fde2014-07-08 07:50:07 -0700528 obj = intel_fb_obj(fb);
Ville Syrjäläef644fd2013-09-04 18:25:21 +0300529 adjusted_mode = &intel_crtc->config.adjusted_mode;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300530
Chris Wilson03689202014-06-06 10:37:11 +0100531 if (i915.enable_fbc < 0) {
Chris Wilson29ebf902013-07-27 17:23:55 +0100532 if (set_no_fbc_reason(dev_priv, FBC_CHIP_DEFAULT))
533 DRM_DEBUG_KMS("disabled per chip default\n");
Damien Lespiau8a5729a2013-06-24 16:22:02 +0100534 goto out_disable;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300535 }
Jani Nikulad330a952014-01-21 11:24:25 +0200536 if (!i915.enable_fbc) {
Chris Wilson29ebf902013-07-27 17:23:55 +0100537 if (set_no_fbc_reason(dev_priv, FBC_MODULE_PARAM))
538 DRM_DEBUG_KMS("fbc disabled per module param\n");
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300539 goto out_disable;
540 }
Ville Syrjäläef644fd2013-09-04 18:25:21 +0300541 if ((adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) ||
542 (adjusted_mode->flags & DRM_MODE_FLAG_DBLSCAN)) {
Chris Wilson29ebf902013-07-27 17:23:55 +0100543 if (set_no_fbc_reason(dev_priv, FBC_UNSUPPORTED_MODE))
544 DRM_DEBUG_KMS("mode incompatible with compression, "
545 "disabling\n");
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300546 goto out_disable;
547 }
Paulo Zanonif85da862013-06-04 16:53:39 -0300548
Daisy Sun032843a2014-06-16 15:48:18 -0700549 if (INTEL_INFO(dev)->gen >= 8 || IS_HASWELL(dev)) {
550 max_width = 4096;
551 max_height = 4096;
552 } else if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
Ville Syrjälä37327ab2013-09-04 18:25:28 +0300553 max_width = 4096;
554 max_height = 2048;
Paulo Zanonif85da862013-06-04 16:53:39 -0300555 } else {
Ville Syrjälä37327ab2013-09-04 18:25:28 +0300556 max_width = 2048;
557 max_height = 1536;
Paulo Zanonif85da862013-06-04 16:53:39 -0300558 }
Ville Syrjälä37327ab2013-09-04 18:25:28 +0300559 if (intel_crtc->config.pipe_src_w > max_width ||
560 intel_crtc->config.pipe_src_h > max_height) {
Chris Wilson29ebf902013-07-27 17:23:55 +0100561 if (set_no_fbc_reason(dev_priv, FBC_MODE_TOO_LARGE))
562 DRM_DEBUG_KMS("mode too large for compression, disabling\n");
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300563 goto out_disable;
564 }
Ben Widawsky8f94d242014-02-20 16:01:20 -0800565 if ((INTEL_INFO(dev)->gen < 4 || HAS_DDI(dev)) &&
Ville Syrjäläc5a44aa2013-11-28 17:29:58 +0200566 intel_crtc->plane != PLANE_A) {
Chris Wilson29ebf902013-07-27 17:23:55 +0100567 if (set_no_fbc_reason(dev_priv, FBC_BAD_PLANE))
Ville Syrjäläc5a44aa2013-11-28 17:29:58 +0200568 DRM_DEBUG_KMS("plane not A, disabling compression\n");
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300569 goto out_disable;
570 }
571
572 /* The use of a CPU fence is mandatory in order to detect writes
573 * by the CPU to the scanout and trigger updates to the FBC.
574 */
575 if (obj->tiling_mode != I915_TILING_X ||
576 obj->fence_reg == I915_FENCE_REG_NONE) {
Chris Wilson29ebf902013-07-27 17:23:55 +0100577 if (set_no_fbc_reason(dev_priv, FBC_NOT_TILED))
578 DRM_DEBUG_KMS("framebuffer not tiled or fenced, disabling compression\n");
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300579 goto out_disable;
580 }
581
582 /* If the kernel debugger is active, always disable compression */
583 if (in_dbg_master())
584 goto out_disable;
585
Matt Roper2ff8fde2014-07-08 07:50:07 -0700586 if (i915_gem_stolen_setup_compression(dev, obj->base.size,
Ben Widawsky5e59f712014-06-30 10:41:24 -0700587 drm_format_plane_cpp(fb->pixel_format, 0))) {
Chris Wilson29ebf902013-07-27 17:23:55 +0100588 if (set_no_fbc_reason(dev_priv, FBC_STOLEN_TOO_SMALL))
589 DRM_DEBUG_KMS("framebuffer too large, disabling compression\n");
Chris Wilson11be49e2012-11-15 11:32:20 +0000590 goto out_disable;
591 }
592
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300593 /* If the scanout has not changed, don't modify the FBC settings.
594 * Note that we make the fundamental assumption that the fb->obj
595 * cannot be unpinned (and have its GTT offset and fence revoked)
596 * without first being decoupled from the scanout and FBC disabled.
597 */
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -0700598 if (dev_priv->fbc.plane == intel_crtc->plane &&
599 dev_priv->fbc.fb_id == fb->base.id &&
600 dev_priv->fbc.y == crtc->y)
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300601 return;
602
603 if (intel_fbc_enabled(dev)) {
604 /* We update FBC along two paths, after changing fb/crtc
605 * configuration (modeswitching) and after page-flipping
606 * finishes. For the latter, we know that not only did
607 * we disable the FBC at the start of the page-flip
608 * sequence, but also more than one vblank has passed.
609 *
610 * For the former case of modeswitching, it is possible
611 * to switch between two FBC valid configurations
612 * instantaneously so we do need to disable the FBC
613 * before we can modify its control registers. We also
614 * have to wait for the next vblank for that to take
615 * effect. However, since we delay enabling FBC we can
616 * assume that a vblank has passed since disabling and
617 * that we can safely alter the registers in the deferred
618 * callback.
619 *
620 * In the scenario that we go from a valid to invalid
621 * and then back to valid FBC configuration we have
622 * no strict enforcement that a vblank occurred since
623 * disabling the FBC. However, along all current pipe
624 * disabling paths we do need to wait for a vblank at
625 * some point. And we wait before enabling FBC anyway.
626 */
627 DRM_DEBUG_KMS("disabling active FBC for update\n");
628 intel_disable_fbc(dev);
629 }
630
Ville Syrjälä993495a2013-12-12 17:27:40 +0200631 intel_enable_fbc(crtc);
Chris Wilson29ebf902013-07-27 17:23:55 +0100632 dev_priv->fbc.no_fbc_reason = FBC_OK;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300633 return;
634
635out_disable:
636 /* Multiple disables should be harmless */
637 if (intel_fbc_enabled(dev)) {
638 DRM_DEBUG_KMS("unsupported config, disabling FBC\n");
639 intel_disable_fbc(dev);
640 }
Chris Wilson11be49e2012-11-15 11:32:20 +0000641 i915_gem_stolen_cleanup_compression(dev);
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300642}
643
Daniel Vetterc921aba2012-04-26 23:28:17 +0200644static void i915_pineview_get_mem_freq(struct drm_device *dev)
645{
Jani Nikula50227e12014-03-31 14:27:21 +0300646 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200647 u32 tmp;
648
649 tmp = I915_READ(CLKCFG);
650
651 switch (tmp & CLKCFG_FSB_MASK) {
652 case CLKCFG_FSB_533:
653 dev_priv->fsb_freq = 533; /* 133*4 */
654 break;
655 case CLKCFG_FSB_800:
656 dev_priv->fsb_freq = 800; /* 200*4 */
657 break;
658 case CLKCFG_FSB_667:
659 dev_priv->fsb_freq = 667; /* 167*4 */
660 break;
661 case CLKCFG_FSB_400:
662 dev_priv->fsb_freq = 400; /* 100*4 */
663 break;
664 }
665
666 switch (tmp & CLKCFG_MEM_MASK) {
667 case CLKCFG_MEM_533:
668 dev_priv->mem_freq = 533;
669 break;
670 case CLKCFG_MEM_667:
671 dev_priv->mem_freq = 667;
672 break;
673 case CLKCFG_MEM_800:
674 dev_priv->mem_freq = 800;
675 break;
676 }
677
678 /* detect pineview DDR3 setting */
679 tmp = I915_READ(CSHRDDR3CTL);
680 dev_priv->is_ddr3 = (tmp & CSHRDDR3CTL_DDR3) ? 1 : 0;
681}
682
683static void i915_ironlake_get_mem_freq(struct drm_device *dev)
684{
Jani Nikula50227e12014-03-31 14:27:21 +0300685 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200686 u16 ddrpll, csipll;
687
688 ddrpll = I915_READ16(DDRMPLL1);
689 csipll = I915_READ16(CSIPLL0);
690
691 switch (ddrpll & 0xff) {
692 case 0xc:
693 dev_priv->mem_freq = 800;
694 break;
695 case 0x10:
696 dev_priv->mem_freq = 1066;
697 break;
698 case 0x14:
699 dev_priv->mem_freq = 1333;
700 break;
701 case 0x18:
702 dev_priv->mem_freq = 1600;
703 break;
704 default:
705 DRM_DEBUG_DRIVER("unknown memory frequency 0x%02x\n",
706 ddrpll & 0xff);
707 dev_priv->mem_freq = 0;
708 break;
709 }
710
Daniel Vetter20e4d402012-08-08 23:35:39 +0200711 dev_priv->ips.r_t = dev_priv->mem_freq;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200712
713 switch (csipll & 0x3ff) {
714 case 0x00c:
715 dev_priv->fsb_freq = 3200;
716 break;
717 case 0x00e:
718 dev_priv->fsb_freq = 3733;
719 break;
720 case 0x010:
721 dev_priv->fsb_freq = 4266;
722 break;
723 case 0x012:
724 dev_priv->fsb_freq = 4800;
725 break;
726 case 0x014:
727 dev_priv->fsb_freq = 5333;
728 break;
729 case 0x016:
730 dev_priv->fsb_freq = 5866;
731 break;
732 case 0x018:
733 dev_priv->fsb_freq = 6400;
734 break;
735 default:
736 DRM_DEBUG_DRIVER("unknown fsb frequency 0x%04x\n",
737 csipll & 0x3ff);
738 dev_priv->fsb_freq = 0;
739 break;
740 }
741
742 if (dev_priv->fsb_freq == 3200) {
Daniel Vetter20e4d402012-08-08 23:35:39 +0200743 dev_priv->ips.c_m = 0;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200744 } else if (dev_priv->fsb_freq > 3200 && dev_priv->fsb_freq <= 4800) {
Daniel Vetter20e4d402012-08-08 23:35:39 +0200745 dev_priv->ips.c_m = 1;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200746 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +0200747 dev_priv->ips.c_m = 2;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200748 }
749}
750
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300751static const struct cxsr_latency cxsr_latency_table[] = {
752 {1, 0, 800, 400, 3382, 33382, 3983, 33983}, /* DDR2-400 SC */
753 {1, 0, 800, 667, 3354, 33354, 3807, 33807}, /* DDR2-667 SC */
754 {1, 0, 800, 800, 3347, 33347, 3763, 33763}, /* DDR2-800 SC */
755 {1, 1, 800, 667, 6420, 36420, 6873, 36873}, /* DDR3-667 SC */
756 {1, 1, 800, 800, 5902, 35902, 6318, 36318}, /* DDR3-800 SC */
757
758 {1, 0, 667, 400, 3400, 33400, 4021, 34021}, /* DDR2-400 SC */
759 {1, 0, 667, 667, 3372, 33372, 3845, 33845}, /* DDR2-667 SC */
760 {1, 0, 667, 800, 3386, 33386, 3822, 33822}, /* DDR2-800 SC */
761 {1, 1, 667, 667, 6438, 36438, 6911, 36911}, /* DDR3-667 SC */
762 {1, 1, 667, 800, 5941, 35941, 6377, 36377}, /* DDR3-800 SC */
763
764 {1, 0, 400, 400, 3472, 33472, 4173, 34173}, /* DDR2-400 SC */
765 {1, 0, 400, 667, 3443, 33443, 3996, 33996}, /* DDR2-667 SC */
766 {1, 0, 400, 800, 3430, 33430, 3946, 33946}, /* DDR2-800 SC */
767 {1, 1, 400, 667, 6509, 36509, 7062, 37062}, /* DDR3-667 SC */
768 {1, 1, 400, 800, 5985, 35985, 6501, 36501}, /* DDR3-800 SC */
769
770 {0, 0, 800, 400, 3438, 33438, 4065, 34065}, /* DDR2-400 SC */
771 {0, 0, 800, 667, 3410, 33410, 3889, 33889}, /* DDR2-667 SC */
772 {0, 0, 800, 800, 3403, 33403, 3845, 33845}, /* DDR2-800 SC */
773 {0, 1, 800, 667, 6476, 36476, 6955, 36955}, /* DDR3-667 SC */
774 {0, 1, 800, 800, 5958, 35958, 6400, 36400}, /* DDR3-800 SC */
775
776 {0, 0, 667, 400, 3456, 33456, 4103, 34106}, /* DDR2-400 SC */
777 {0, 0, 667, 667, 3428, 33428, 3927, 33927}, /* DDR2-667 SC */
778 {0, 0, 667, 800, 3443, 33443, 3905, 33905}, /* DDR2-800 SC */
779 {0, 1, 667, 667, 6494, 36494, 6993, 36993}, /* DDR3-667 SC */
780 {0, 1, 667, 800, 5998, 35998, 6460, 36460}, /* DDR3-800 SC */
781
782 {0, 0, 400, 400, 3528, 33528, 4255, 34255}, /* DDR2-400 SC */
783 {0, 0, 400, 667, 3500, 33500, 4079, 34079}, /* DDR2-667 SC */
784 {0, 0, 400, 800, 3487, 33487, 4029, 34029}, /* DDR2-800 SC */
785 {0, 1, 400, 667, 6566, 36566, 7145, 37145}, /* DDR3-667 SC */
786 {0, 1, 400, 800, 6042, 36042, 6584, 36584}, /* DDR3-800 SC */
787};
788
Daniel Vetter63c62272012-04-21 23:17:55 +0200789static const struct cxsr_latency *intel_get_cxsr_latency(int is_desktop,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300790 int is_ddr3,
791 int fsb,
792 int mem)
793{
794 const struct cxsr_latency *latency;
795 int i;
796
797 if (fsb == 0 || mem == 0)
798 return NULL;
799
800 for (i = 0; i < ARRAY_SIZE(cxsr_latency_table); i++) {
801 latency = &cxsr_latency_table[i];
802 if (is_desktop == latency->is_desktop &&
803 is_ddr3 == latency->is_ddr3 &&
804 fsb == latency->fsb_freq && mem == latency->mem_freq)
805 return latency;
806 }
807
808 DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
809
810 return NULL;
811}
812
Imre Deak5209b1f2014-07-01 12:36:17 +0300813void intel_set_memory_cxsr(struct drm_i915_private *dev_priv, bool enable)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300814{
Imre Deak5209b1f2014-07-01 12:36:17 +0300815 struct drm_device *dev = dev_priv->dev;
816 u32 val;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300817
Imre Deak5209b1f2014-07-01 12:36:17 +0300818 if (IS_VALLEYVIEW(dev)) {
819 I915_WRITE(FW_BLC_SELF_VLV, enable ? FW_CSPWRDWNEN : 0);
820 } else if (IS_G4X(dev) || IS_CRESTLINE(dev)) {
821 I915_WRITE(FW_BLC_SELF, enable ? FW_BLC_SELF_EN : 0);
822 } else if (IS_PINEVIEW(dev)) {
823 val = I915_READ(DSPFW3) & ~PINEVIEW_SELF_REFRESH_EN;
824 val |= enable ? PINEVIEW_SELF_REFRESH_EN : 0;
825 I915_WRITE(DSPFW3, val);
826 } else if (IS_I945G(dev) || IS_I945GM(dev)) {
827 val = enable ? _MASKED_BIT_ENABLE(FW_BLC_SELF_EN) :
828 _MASKED_BIT_DISABLE(FW_BLC_SELF_EN);
829 I915_WRITE(FW_BLC_SELF, val);
830 } else if (IS_I915GM(dev)) {
831 val = enable ? _MASKED_BIT_ENABLE(INSTPM_SELF_EN) :
832 _MASKED_BIT_DISABLE(INSTPM_SELF_EN);
833 I915_WRITE(INSTPM, val);
834 } else {
835 return;
836 }
837
838 DRM_DEBUG_KMS("memory self-refresh is %s\n",
839 enable ? "enabled" : "disabled");
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300840}
841
842/*
843 * Latency for FIFO fetches is dependent on several factors:
844 * - memory configuration (speed, channels)
845 * - chipset
846 * - current MCH state
847 * It can be fairly high in some situations, so here we assume a fairly
848 * pessimal value. It's a tradeoff between extra memory fetches (if we
849 * set this value too high, the FIFO will fetch frequently to stay full)
850 * and power consumption (set it too low to save power and we might see
851 * FIFO underruns and display "flicker").
852 *
853 * A value of 5us seems to be a good balance; safe for very low end
854 * platforms but not overly aggressive on lower latency configs.
855 */
856static const int latency_ns = 5000;
857
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300858static int i9xx_get_fifo_size(struct drm_device *dev, int plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300859{
860 struct drm_i915_private *dev_priv = dev->dev_private;
861 uint32_t dsparb = I915_READ(DSPARB);
862 int size;
863
864 size = dsparb & 0x7f;
865 if (plane)
866 size = ((dsparb >> DSPARB_CSTART_SHIFT) & 0x7f) - size;
867
868 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
869 plane ? "B" : "A", size);
870
871 return size;
872}
873
Daniel Vetterfeb56b92013-12-14 20:38:30 -0200874static int i830_get_fifo_size(struct drm_device *dev, int plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300875{
876 struct drm_i915_private *dev_priv = dev->dev_private;
877 uint32_t dsparb = I915_READ(DSPARB);
878 int size;
879
880 size = dsparb & 0x1ff;
881 if (plane)
882 size = ((dsparb >> DSPARB_BEND_SHIFT) & 0x1ff) - size;
883 size >>= 1; /* Convert to cachelines */
884
885 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
886 plane ? "B" : "A", size);
887
888 return size;
889}
890
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300891static int i845_get_fifo_size(struct drm_device *dev, int plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300892{
893 struct drm_i915_private *dev_priv = dev->dev_private;
894 uint32_t dsparb = I915_READ(DSPARB);
895 int size;
896
897 size = dsparb & 0x7f;
898 size >>= 2; /* Convert to cachelines */
899
900 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
901 plane ? "B" : "A",
902 size);
903
904 return size;
905}
906
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300907/* Pineview has different values for various configs */
908static const struct intel_watermark_params pineview_display_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300909 .fifo_size = PINEVIEW_DISPLAY_FIFO,
910 .max_wm = PINEVIEW_MAX_WM,
911 .default_wm = PINEVIEW_DFT_WM,
912 .guard_size = PINEVIEW_GUARD_WM,
913 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300914};
915static const struct intel_watermark_params pineview_display_hplloff_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300916 .fifo_size = PINEVIEW_DISPLAY_FIFO,
917 .max_wm = PINEVIEW_MAX_WM,
918 .default_wm = PINEVIEW_DFT_HPLLOFF_WM,
919 .guard_size = PINEVIEW_GUARD_WM,
920 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300921};
922static const struct intel_watermark_params pineview_cursor_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300923 .fifo_size = PINEVIEW_CURSOR_FIFO,
924 .max_wm = PINEVIEW_CURSOR_MAX_WM,
925 .default_wm = PINEVIEW_CURSOR_DFT_WM,
926 .guard_size = PINEVIEW_CURSOR_GUARD_WM,
927 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300928};
929static const struct intel_watermark_params pineview_cursor_hplloff_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300930 .fifo_size = PINEVIEW_CURSOR_FIFO,
931 .max_wm = PINEVIEW_CURSOR_MAX_WM,
932 .default_wm = PINEVIEW_CURSOR_DFT_WM,
933 .guard_size = PINEVIEW_CURSOR_GUARD_WM,
934 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300935};
936static const struct intel_watermark_params g4x_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300937 .fifo_size = G4X_FIFO_SIZE,
938 .max_wm = G4X_MAX_WM,
939 .default_wm = G4X_MAX_WM,
940 .guard_size = 2,
941 .cacheline_size = G4X_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300942};
943static const struct intel_watermark_params g4x_cursor_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300944 .fifo_size = I965_CURSOR_FIFO,
945 .max_wm = I965_CURSOR_MAX_WM,
946 .default_wm = I965_CURSOR_DFT_WM,
947 .guard_size = 2,
948 .cacheline_size = G4X_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300949};
950static const struct intel_watermark_params valleyview_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300951 .fifo_size = VALLEYVIEW_FIFO_SIZE,
952 .max_wm = VALLEYVIEW_MAX_WM,
953 .default_wm = VALLEYVIEW_MAX_WM,
954 .guard_size = 2,
955 .cacheline_size = G4X_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300956};
957static const struct intel_watermark_params valleyview_cursor_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300958 .fifo_size = I965_CURSOR_FIFO,
959 .max_wm = VALLEYVIEW_CURSOR_MAX_WM,
960 .default_wm = I965_CURSOR_DFT_WM,
961 .guard_size = 2,
962 .cacheline_size = G4X_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300963};
964static const struct intel_watermark_params i965_cursor_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300965 .fifo_size = I965_CURSOR_FIFO,
966 .max_wm = I965_CURSOR_MAX_WM,
967 .default_wm = I965_CURSOR_DFT_WM,
968 .guard_size = 2,
969 .cacheline_size = I915_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300970};
971static const struct intel_watermark_params i945_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300972 .fifo_size = I945_FIFO_SIZE,
973 .max_wm = I915_MAX_WM,
974 .default_wm = 1,
975 .guard_size = 2,
976 .cacheline_size = I915_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300977};
978static const struct intel_watermark_params i915_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300979 .fifo_size = I915_FIFO_SIZE,
980 .max_wm = I915_MAX_WM,
981 .default_wm = 1,
982 .guard_size = 2,
983 .cacheline_size = I915_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300984};
Daniel Vetterfeb56b92013-12-14 20:38:30 -0200985static const struct intel_watermark_params i830_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300986 .fifo_size = I855GM_FIFO_SIZE,
987 .max_wm = I915_MAX_WM,
988 .default_wm = 1,
989 .guard_size = 2,
990 .cacheline_size = I830_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300991};
Daniel Vetterfeb56b92013-12-14 20:38:30 -0200992static const struct intel_watermark_params i845_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300993 .fifo_size = I830_FIFO_SIZE,
994 .max_wm = I915_MAX_WM,
995 .default_wm = 1,
996 .guard_size = 2,
997 .cacheline_size = I830_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300998};
999
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001000/**
1001 * intel_calculate_wm - calculate watermark level
1002 * @clock_in_khz: pixel clock
1003 * @wm: chip FIFO params
1004 * @pixel_size: display pixel size
1005 * @latency_ns: memory latency for the platform
1006 *
1007 * Calculate the watermark level (the level at which the display plane will
1008 * start fetching from memory again). Each chip has a different display
1009 * FIFO size and allocation, so the caller needs to figure that out and pass
1010 * in the correct intel_watermark_params structure.
1011 *
1012 * As the pixel clock runs, the FIFO will be drained at a rate that depends
1013 * on the pixel size. When it reaches the watermark level, it'll start
1014 * fetching FIFO line sized based chunks from memory until the FIFO fills
1015 * past the watermark point. If the FIFO drains completely, a FIFO underrun
1016 * will occur, and a display engine hang could result.
1017 */
1018static unsigned long intel_calculate_wm(unsigned long clock_in_khz,
1019 const struct intel_watermark_params *wm,
1020 int fifo_size,
1021 int pixel_size,
1022 unsigned long latency_ns)
1023{
1024 long entries_required, wm_size;
1025
1026 /*
1027 * Note: we need to make sure we don't overflow for various clock &
1028 * latency values.
1029 * clocks go from a few thousand to several hundred thousand.
1030 * latency is usually a few thousand
1031 */
1032 entries_required = ((clock_in_khz / 1000) * pixel_size * latency_ns) /
1033 1000;
1034 entries_required = DIV_ROUND_UP(entries_required, wm->cacheline_size);
1035
1036 DRM_DEBUG_KMS("FIFO entries required for mode: %ld\n", entries_required);
1037
1038 wm_size = fifo_size - (entries_required + wm->guard_size);
1039
1040 DRM_DEBUG_KMS("FIFO watermark level: %ld\n", wm_size);
1041
1042 /* Don't promote wm_size to unsigned... */
1043 if (wm_size > (long)wm->max_wm)
1044 wm_size = wm->max_wm;
1045 if (wm_size <= 0)
1046 wm_size = wm->default_wm;
1047 return wm_size;
1048}
1049
1050static struct drm_crtc *single_enabled_crtc(struct drm_device *dev)
1051{
1052 struct drm_crtc *crtc, *enabled = NULL;
1053
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01001054 for_each_crtc(dev, crtc) {
Chris Wilson3490ea52013-01-07 10:11:40 +00001055 if (intel_crtc_active(crtc)) {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001056 if (enabled)
1057 return NULL;
1058 enabled = crtc;
1059 }
1060 }
1061
1062 return enabled;
1063}
1064
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001065static void pineview_update_wm(struct drm_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001066{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001067 struct drm_device *dev = unused_crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001068 struct drm_i915_private *dev_priv = dev->dev_private;
1069 struct drm_crtc *crtc;
1070 const struct cxsr_latency *latency;
1071 u32 reg;
1072 unsigned long wm;
1073
1074 latency = intel_get_cxsr_latency(IS_PINEVIEW_G(dev), dev_priv->is_ddr3,
1075 dev_priv->fsb_freq, dev_priv->mem_freq);
1076 if (!latency) {
1077 DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
Imre Deak5209b1f2014-07-01 12:36:17 +03001078 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001079 return;
1080 }
1081
1082 crtc = single_enabled_crtc(dev);
1083 if (crtc) {
Damien Lespiau241bfc32013-09-25 16:45:37 +01001084 const struct drm_display_mode *adjusted_mode;
Matt Roperf4510a22014-04-01 15:22:40 -07001085 int pixel_size = crtc->primary->fb->bits_per_pixel / 8;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001086 int clock;
1087
1088 adjusted_mode = &to_intel_crtc(crtc)->config.adjusted_mode;
1089 clock = adjusted_mode->crtc_clock;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001090
1091 /* Display SR */
1092 wm = intel_calculate_wm(clock, &pineview_display_wm,
1093 pineview_display_wm.fifo_size,
1094 pixel_size, latency->display_sr);
1095 reg = I915_READ(DSPFW1);
1096 reg &= ~DSPFW_SR_MASK;
1097 reg |= wm << DSPFW_SR_SHIFT;
1098 I915_WRITE(DSPFW1, reg);
1099 DRM_DEBUG_KMS("DSPFW1 register is %x\n", reg);
1100
1101 /* cursor SR */
1102 wm = intel_calculate_wm(clock, &pineview_cursor_wm,
1103 pineview_display_wm.fifo_size,
1104 pixel_size, latency->cursor_sr);
1105 reg = I915_READ(DSPFW3);
1106 reg &= ~DSPFW_CURSOR_SR_MASK;
1107 reg |= (wm & 0x3f) << DSPFW_CURSOR_SR_SHIFT;
1108 I915_WRITE(DSPFW3, reg);
1109
1110 /* Display HPLL off SR */
1111 wm = intel_calculate_wm(clock, &pineview_display_hplloff_wm,
1112 pineview_display_hplloff_wm.fifo_size,
1113 pixel_size, latency->display_hpll_disable);
1114 reg = I915_READ(DSPFW3);
1115 reg &= ~DSPFW_HPLL_SR_MASK;
1116 reg |= wm & DSPFW_HPLL_SR_MASK;
1117 I915_WRITE(DSPFW3, reg);
1118
1119 /* cursor HPLL off SR */
1120 wm = intel_calculate_wm(clock, &pineview_cursor_hplloff_wm,
1121 pineview_display_hplloff_wm.fifo_size,
1122 pixel_size, latency->cursor_hpll_disable);
1123 reg = I915_READ(DSPFW3);
1124 reg &= ~DSPFW_HPLL_CURSOR_MASK;
1125 reg |= (wm & 0x3f) << DSPFW_HPLL_CURSOR_SHIFT;
1126 I915_WRITE(DSPFW3, reg);
1127 DRM_DEBUG_KMS("DSPFW3 register is %x\n", reg);
1128
Imre Deak5209b1f2014-07-01 12:36:17 +03001129 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001130 } else {
Imre Deak5209b1f2014-07-01 12:36:17 +03001131 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001132 }
1133}
1134
1135static bool g4x_compute_wm0(struct drm_device *dev,
1136 int plane,
1137 const struct intel_watermark_params *display,
1138 int display_latency_ns,
1139 const struct intel_watermark_params *cursor,
1140 int cursor_latency_ns,
1141 int *plane_wm,
1142 int *cursor_wm)
1143{
1144 struct drm_crtc *crtc;
Ville Syrjälä4fe85902013-09-04 18:25:22 +03001145 const struct drm_display_mode *adjusted_mode;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001146 int htotal, hdisplay, clock, pixel_size;
1147 int line_time_us, line_count;
1148 int entries, tlb_miss;
1149
1150 crtc = intel_get_crtc_for_plane(dev, plane);
Chris Wilson3490ea52013-01-07 10:11:40 +00001151 if (!intel_crtc_active(crtc)) {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001152 *cursor_wm = cursor->guard_size;
1153 *plane_wm = display->guard_size;
1154 return false;
1155 }
1156
Ville Syrjälä4fe85902013-09-04 18:25:22 +03001157 adjusted_mode = &to_intel_crtc(crtc)->config.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001158 clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -08001159 htotal = adjusted_mode->crtc_htotal;
Ville Syrjälä37327ab2013-09-04 18:25:28 +03001160 hdisplay = to_intel_crtc(crtc)->config.pipe_src_w;
Matt Roperf4510a22014-04-01 15:22:40 -07001161 pixel_size = crtc->primary->fb->bits_per_pixel / 8;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001162
1163 /* Use the small buffer method to calculate plane watermark */
1164 entries = ((clock * pixel_size / 1000) * display_latency_ns) / 1000;
1165 tlb_miss = display->fifo_size*display->cacheline_size - hdisplay * 8;
1166 if (tlb_miss > 0)
1167 entries += tlb_miss;
1168 entries = DIV_ROUND_UP(entries, display->cacheline_size);
1169 *plane_wm = entries + display->guard_size;
1170 if (*plane_wm > (int)display->max_wm)
1171 *plane_wm = display->max_wm;
1172
1173 /* Use the large buffer method to calculate cursor watermark */
Ville Syrjälä922044c2014-02-14 14:18:57 +02001174 line_time_us = max(htotal * 1000 / clock, 1);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001175 line_count = (cursor_latency_ns / line_time_us + 1000) / 1000;
Chris Wilson7bb836d2014-03-26 12:38:14 +00001176 entries = line_count * to_intel_crtc(crtc)->cursor_width * pixel_size;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001177 tlb_miss = cursor->fifo_size*cursor->cacheline_size - hdisplay * 8;
1178 if (tlb_miss > 0)
1179 entries += tlb_miss;
1180 entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
1181 *cursor_wm = entries + cursor->guard_size;
1182 if (*cursor_wm > (int)cursor->max_wm)
1183 *cursor_wm = (int)cursor->max_wm;
1184
1185 return true;
1186}
1187
1188/*
1189 * Check the wm result.
1190 *
1191 * If any calculated watermark values is larger than the maximum value that
1192 * can be programmed into the associated watermark register, that watermark
1193 * must be disabled.
1194 */
1195static bool g4x_check_srwm(struct drm_device *dev,
1196 int display_wm, int cursor_wm,
1197 const struct intel_watermark_params *display,
1198 const struct intel_watermark_params *cursor)
1199{
1200 DRM_DEBUG_KMS("SR watermark: display plane %d, cursor %d\n",
1201 display_wm, cursor_wm);
1202
1203 if (display_wm > display->max_wm) {
1204 DRM_DEBUG_KMS("display watermark is too large(%d/%ld), disabling\n",
1205 display_wm, display->max_wm);
1206 return false;
1207 }
1208
1209 if (cursor_wm > cursor->max_wm) {
1210 DRM_DEBUG_KMS("cursor watermark is too large(%d/%ld), disabling\n",
1211 cursor_wm, cursor->max_wm);
1212 return false;
1213 }
1214
1215 if (!(display_wm || cursor_wm)) {
1216 DRM_DEBUG_KMS("SR latency is 0, disabling\n");
1217 return false;
1218 }
1219
1220 return true;
1221}
1222
1223static bool g4x_compute_srwm(struct drm_device *dev,
1224 int plane,
1225 int latency_ns,
1226 const struct intel_watermark_params *display,
1227 const struct intel_watermark_params *cursor,
1228 int *display_wm, int *cursor_wm)
1229{
1230 struct drm_crtc *crtc;
Ville Syrjälä4fe85902013-09-04 18:25:22 +03001231 const struct drm_display_mode *adjusted_mode;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001232 int hdisplay, htotal, pixel_size, clock;
1233 unsigned long line_time_us;
1234 int line_count, line_size;
1235 int small, large;
1236 int entries;
1237
1238 if (!latency_ns) {
1239 *display_wm = *cursor_wm = 0;
1240 return false;
1241 }
1242
1243 crtc = intel_get_crtc_for_plane(dev, plane);
Ville Syrjälä4fe85902013-09-04 18:25:22 +03001244 adjusted_mode = &to_intel_crtc(crtc)->config.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001245 clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -08001246 htotal = adjusted_mode->crtc_htotal;
Ville Syrjälä37327ab2013-09-04 18:25:28 +03001247 hdisplay = to_intel_crtc(crtc)->config.pipe_src_w;
Matt Roperf4510a22014-04-01 15:22:40 -07001248 pixel_size = crtc->primary->fb->bits_per_pixel / 8;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001249
Ville Syrjälä922044c2014-02-14 14:18:57 +02001250 line_time_us = max(htotal * 1000 / clock, 1);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001251 line_count = (latency_ns / line_time_us + 1000) / 1000;
1252 line_size = hdisplay * pixel_size;
1253
1254 /* Use the minimum of the small and large buffer method for primary */
1255 small = ((clock * pixel_size / 1000) * latency_ns) / 1000;
1256 large = line_count * line_size;
1257
1258 entries = DIV_ROUND_UP(min(small, large), display->cacheline_size);
1259 *display_wm = entries + display->guard_size;
1260
1261 /* calculate the self-refresh watermark for display cursor */
Chris Wilson7bb836d2014-03-26 12:38:14 +00001262 entries = line_count * pixel_size * to_intel_crtc(crtc)->cursor_width;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001263 entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
1264 *cursor_wm = entries + cursor->guard_size;
1265
1266 return g4x_check_srwm(dev,
1267 *display_wm, *cursor_wm,
1268 display, cursor);
1269}
1270
1271static bool vlv_compute_drain_latency(struct drm_device *dev,
1272 int plane,
1273 int *plane_prec_mult,
1274 int *plane_dl,
1275 int *cursor_prec_mult,
1276 int *cursor_dl)
1277{
1278 struct drm_crtc *crtc;
1279 int clock, pixel_size;
1280 int entries;
1281
1282 crtc = intel_get_crtc_for_plane(dev, plane);
Chris Wilson3490ea52013-01-07 10:11:40 +00001283 if (!intel_crtc_active(crtc))
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001284 return false;
1285
Damien Lespiau241bfc32013-09-25 16:45:37 +01001286 clock = to_intel_crtc(crtc)->config.adjusted_mode.crtc_clock;
Matt Roperf4510a22014-04-01 15:22:40 -07001287 pixel_size = crtc->primary->fb->bits_per_pixel / 8; /* BPP */
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001288
1289 entries = (clock / 1000) * pixel_size;
1290 *plane_prec_mult = (entries > 256) ?
1291 DRAIN_LATENCY_PRECISION_32 : DRAIN_LATENCY_PRECISION_16;
1292 *plane_dl = (64 * (*plane_prec_mult) * 4) / ((clock / 1000) *
1293 pixel_size);
1294
1295 entries = (clock / 1000) * 4; /* BPP is always 4 for cursor */
1296 *cursor_prec_mult = (entries > 256) ?
1297 DRAIN_LATENCY_PRECISION_32 : DRAIN_LATENCY_PRECISION_16;
1298 *cursor_dl = (64 * (*cursor_prec_mult) * 4) / ((clock / 1000) * 4);
1299
1300 return true;
1301}
1302
1303/*
1304 * Update drain latency registers of memory arbiter
1305 *
1306 * Valleyview SoC has a new memory arbiter and needs drain latency registers
1307 * to be programmed. Each plane has a drain latency multiplier and a drain
1308 * latency value.
1309 */
1310
1311static void vlv_update_drain_latency(struct drm_device *dev)
1312{
1313 struct drm_i915_private *dev_priv = dev->dev_private;
1314 int planea_prec, planea_dl, planeb_prec, planeb_dl;
1315 int cursora_prec, cursora_dl, cursorb_prec, cursorb_dl;
1316 int plane_prec_mult, cursor_prec_mult; /* Precision multiplier is
1317 either 16 or 32 */
1318
1319 /* For plane A, Cursor A */
1320 if (vlv_compute_drain_latency(dev, 0, &plane_prec_mult, &planea_dl,
1321 &cursor_prec_mult, &cursora_dl)) {
1322 cursora_prec = (cursor_prec_mult == DRAIN_LATENCY_PRECISION_32) ?
1323 DDL_CURSORA_PRECISION_32 : DDL_CURSORA_PRECISION_16;
1324 planea_prec = (plane_prec_mult == DRAIN_LATENCY_PRECISION_32) ?
1325 DDL_PLANEA_PRECISION_32 : DDL_PLANEA_PRECISION_16;
1326
1327 I915_WRITE(VLV_DDL1, cursora_prec |
1328 (cursora_dl << DDL_CURSORA_SHIFT) |
1329 planea_prec | planea_dl);
1330 }
1331
1332 /* For plane B, Cursor B */
1333 if (vlv_compute_drain_latency(dev, 1, &plane_prec_mult, &planeb_dl,
1334 &cursor_prec_mult, &cursorb_dl)) {
1335 cursorb_prec = (cursor_prec_mult == DRAIN_LATENCY_PRECISION_32) ?
1336 DDL_CURSORB_PRECISION_32 : DDL_CURSORB_PRECISION_16;
1337 planeb_prec = (plane_prec_mult == DRAIN_LATENCY_PRECISION_32) ?
1338 DDL_PLANEB_PRECISION_32 : DDL_PLANEB_PRECISION_16;
1339
1340 I915_WRITE(VLV_DDL2, cursorb_prec |
1341 (cursorb_dl << DDL_CURSORB_SHIFT) |
1342 planeb_prec | planeb_dl);
1343 }
1344}
1345
1346#define single_plane_enabled(mask) is_power_of_2(mask)
1347
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001348static void valleyview_update_wm(struct drm_crtc *crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001349{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001350 struct drm_device *dev = crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001351 static const int sr_latency_ns = 12000;
1352 struct drm_i915_private *dev_priv = dev->dev_private;
1353 int planea_wm, planeb_wm, cursora_wm, cursorb_wm;
1354 int plane_sr, cursor_sr;
Chris Wilsonaf6c4572012-12-11 12:01:43 +00001355 int ignore_plane_sr, ignore_cursor_sr;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001356 unsigned int enabled = 0;
Imre Deak98584252014-06-13 14:54:20 +03001357 bool cxsr_enabled;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001358
1359 vlv_update_drain_latency(dev);
1360
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001361 if (g4x_compute_wm0(dev, PIPE_A,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001362 &valleyview_wm_info, latency_ns,
1363 &valleyview_cursor_wm_info, latency_ns,
1364 &planea_wm, &cursora_wm))
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001365 enabled |= 1 << PIPE_A;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001366
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001367 if (g4x_compute_wm0(dev, PIPE_B,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001368 &valleyview_wm_info, latency_ns,
1369 &valleyview_cursor_wm_info, latency_ns,
1370 &planeb_wm, &cursorb_wm))
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001371 enabled |= 1 << PIPE_B;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001372
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001373 if (single_plane_enabled(enabled) &&
1374 g4x_compute_srwm(dev, ffs(enabled) - 1,
1375 sr_latency_ns,
1376 &valleyview_wm_info,
1377 &valleyview_cursor_wm_info,
Chris Wilsonaf6c4572012-12-11 12:01:43 +00001378 &plane_sr, &ignore_cursor_sr) &&
1379 g4x_compute_srwm(dev, ffs(enabled) - 1,
1380 2*sr_latency_ns,
1381 &valleyview_wm_info,
1382 &valleyview_cursor_wm_info,
Chris Wilson52bd02d2012-12-07 10:43:24 +00001383 &ignore_plane_sr, &cursor_sr)) {
Imre Deak98584252014-06-13 14:54:20 +03001384 cxsr_enabled = true;
Chris Wilson52bd02d2012-12-07 10:43:24 +00001385 } else {
Imre Deak98584252014-06-13 14:54:20 +03001386 cxsr_enabled = false;
Imre Deak5209b1f2014-07-01 12:36:17 +03001387 intel_set_memory_cxsr(dev_priv, false);
Chris Wilson52bd02d2012-12-07 10:43:24 +00001388 plane_sr = cursor_sr = 0;
1389 }
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001390
1391 DRM_DEBUG_KMS("Setting FIFO watermarks - A: plane=%d, cursor=%d, B: plane=%d, cursor=%d, SR: plane=%d, cursor=%d\n",
1392 planea_wm, cursora_wm,
1393 planeb_wm, cursorb_wm,
1394 plane_sr, cursor_sr);
1395
1396 I915_WRITE(DSPFW1,
1397 (plane_sr << DSPFW_SR_SHIFT) |
1398 (cursorb_wm << DSPFW_CURSORB_SHIFT) |
1399 (planeb_wm << DSPFW_PLANEB_SHIFT) |
1400 planea_wm);
1401 I915_WRITE(DSPFW2,
Chris Wilson8c919b22012-12-04 16:33:19 +00001402 (I915_READ(DSPFW2) & ~DSPFW_CURSORA_MASK) |
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001403 (cursora_wm << DSPFW_CURSORA_SHIFT));
1404 I915_WRITE(DSPFW3,
Chris Wilson8c919b22012-12-04 16:33:19 +00001405 (I915_READ(DSPFW3) & ~DSPFW_CURSOR_SR_MASK) |
1406 (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
Imre Deak98584252014-06-13 14:54:20 +03001407
1408 if (cxsr_enabled)
1409 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001410}
1411
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001412static void g4x_update_wm(struct drm_crtc *crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001413{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001414 struct drm_device *dev = crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001415 static const int sr_latency_ns = 12000;
1416 struct drm_i915_private *dev_priv = dev->dev_private;
1417 int planea_wm, planeb_wm, cursora_wm, cursorb_wm;
1418 int plane_sr, cursor_sr;
1419 unsigned int enabled = 0;
Imre Deak98584252014-06-13 14:54:20 +03001420 bool cxsr_enabled;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001421
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001422 if (g4x_compute_wm0(dev, PIPE_A,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001423 &g4x_wm_info, latency_ns,
1424 &g4x_cursor_wm_info, latency_ns,
1425 &planea_wm, &cursora_wm))
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001426 enabled |= 1 << PIPE_A;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001427
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001428 if (g4x_compute_wm0(dev, PIPE_B,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001429 &g4x_wm_info, latency_ns,
1430 &g4x_cursor_wm_info, latency_ns,
1431 &planeb_wm, &cursorb_wm))
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001432 enabled |= 1 << PIPE_B;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001433
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001434 if (single_plane_enabled(enabled) &&
1435 g4x_compute_srwm(dev, ffs(enabled) - 1,
1436 sr_latency_ns,
1437 &g4x_wm_info,
1438 &g4x_cursor_wm_info,
Chris Wilson52bd02d2012-12-07 10:43:24 +00001439 &plane_sr, &cursor_sr)) {
Imre Deak98584252014-06-13 14:54:20 +03001440 cxsr_enabled = true;
Chris Wilson52bd02d2012-12-07 10:43:24 +00001441 } else {
Imre Deak98584252014-06-13 14:54:20 +03001442 cxsr_enabled = false;
Imre Deak5209b1f2014-07-01 12:36:17 +03001443 intel_set_memory_cxsr(dev_priv, false);
Chris Wilson52bd02d2012-12-07 10:43:24 +00001444 plane_sr = cursor_sr = 0;
1445 }
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001446
1447 DRM_DEBUG_KMS("Setting FIFO watermarks - A: plane=%d, cursor=%d, B: plane=%d, cursor=%d, SR: plane=%d, cursor=%d\n",
1448 planea_wm, cursora_wm,
1449 planeb_wm, cursorb_wm,
1450 plane_sr, cursor_sr);
1451
1452 I915_WRITE(DSPFW1,
1453 (plane_sr << DSPFW_SR_SHIFT) |
1454 (cursorb_wm << DSPFW_CURSORB_SHIFT) |
1455 (planeb_wm << DSPFW_PLANEB_SHIFT) |
1456 planea_wm);
1457 I915_WRITE(DSPFW2,
Chris Wilson8c919b22012-12-04 16:33:19 +00001458 (I915_READ(DSPFW2) & ~DSPFW_CURSORA_MASK) |
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001459 (cursora_wm << DSPFW_CURSORA_SHIFT));
1460 /* HPLL off in SR has some issues on G4x... disable it */
1461 I915_WRITE(DSPFW3,
Chris Wilson8c919b22012-12-04 16:33:19 +00001462 (I915_READ(DSPFW3) & ~(DSPFW_HPLL_SR_EN | DSPFW_CURSOR_SR_MASK)) |
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001463 (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
Imre Deak98584252014-06-13 14:54:20 +03001464
1465 if (cxsr_enabled)
1466 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001467}
1468
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001469static void i965_update_wm(struct drm_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001470{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001471 struct drm_device *dev = unused_crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001472 struct drm_i915_private *dev_priv = dev->dev_private;
1473 struct drm_crtc *crtc;
1474 int srwm = 1;
1475 int cursor_sr = 16;
Imre Deak98584252014-06-13 14:54:20 +03001476 bool cxsr_enabled;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001477
1478 /* Calc sr entries for one plane configs */
1479 crtc = single_enabled_crtc(dev);
1480 if (crtc) {
1481 /* self-refresh has much higher latency */
1482 static const int sr_latency_ns = 12000;
Ville Syrjälä4fe85902013-09-04 18:25:22 +03001483 const struct drm_display_mode *adjusted_mode =
1484 &to_intel_crtc(crtc)->config.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001485 int clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -08001486 int htotal = adjusted_mode->crtc_htotal;
Ville Syrjälä37327ab2013-09-04 18:25:28 +03001487 int hdisplay = to_intel_crtc(crtc)->config.pipe_src_w;
Matt Roperf4510a22014-04-01 15:22:40 -07001488 int pixel_size = crtc->primary->fb->bits_per_pixel / 8;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001489 unsigned long line_time_us;
1490 int entries;
1491
Ville Syrjälä922044c2014-02-14 14:18:57 +02001492 line_time_us = max(htotal * 1000 / clock, 1);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001493
1494 /* Use ns/us then divide to preserve precision */
1495 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
1496 pixel_size * hdisplay;
1497 entries = DIV_ROUND_UP(entries, I915_FIFO_LINE_SIZE);
1498 srwm = I965_FIFO_SIZE - entries;
1499 if (srwm < 0)
1500 srwm = 1;
1501 srwm &= 0x1ff;
1502 DRM_DEBUG_KMS("self-refresh entries: %d, wm: %d\n",
1503 entries, srwm);
1504
1505 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
Chris Wilson7bb836d2014-03-26 12:38:14 +00001506 pixel_size * to_intel_crtc(crtc)->cursor_width;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001507 entries = DIV_ROUND_UP(entries,
1508 i965_cursor_wm_info.cacheline_size);
1509 cursor_sr = i965_cursor_wm_info.fifo_size -
1510 (entries + i965_cursor_wm_info.guard_size);
1511
1512 if (cursor_sr > i965_cursor_wm_info.max_wm)
1513 cursor_sr = i965_cursor_wm_info.max_wm;
1514
1515 DRM_DEBUG_KMS("self-refresh watermark: display plane %d "
1516 "cursor %d\n", srwm, cursor_sr);
1517
Imre Deak98584252014-06-13 14:54:20 +03001518 cxsr_enabled = true;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001519 } else {
Imre Deak98584252014-06-13 14:54:20 +03001520 cxsr_enabled = false;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001521 /* Turn off self refresh if both pipes are enabled */
Imre Deak5209b1f2014-07-01 12:36:17 +03001522 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001523 }
1524
1525 DRM_DEBUG_KMS("Setting FIFO watermarks - A: 8, B: 8, C: 8, SR %d\n",
1526 srwm);
1527
1528 /* 965 has limitations... */
1529 I915_WRITE(DSPFW1, (srwm << DSPFW_SR_SHIFT) |
1530 (8 << 16) | (8 << 8) | (8 << 0));
1531 I915_WRITE(DSPFW2, (8 << 8) | (8 << 0));
1532 /* update cursor SR watermark */
1533 I915_WRITE(DSPFW3, (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
Imre Deak98584252014-06-13 14:54:20 +03001534
1535 if (cxsr_enabled)
1536 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001537}
1538
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001539static void i9xx_update_wm(struct drm_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001540{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001541 struct drm_device *dev = unused_crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001542 struct drm_i915_private *dev_priv = dev->dev_private;
1543 const struct intel_watermark_params *wm_info;
1544 uint32_t fwater_lo;
1545 uint32_t fwater_hi;
1546 int cwm, srwm = 1;
1547 int fifo_size;
1548 int planea_wm, planeb_wm;
1549 struct drm_crtc *crtc, *enabled = NULL;
1550
1551 if (IS_I945GM(dev))
1552 wm_info = &i945_wm_info;
1553 else if (!IS_GEN2(dev))
1554 wm_info = &i915_wm_info;
1555 else
Daniel Vetterfeb56b92013-12-14 20:38:30 -02001556 wm_info = &i830_wm_info;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001557
1558 fifo_size = dev_priv->display.get_fifo_size(dev, 0);
1559 crtc = intel_get_crtc_for_plane(dev, 0);
Chris Wilson3490ea52013-01-07 10:11:40 +00001560 if (intel_crtc_active(crtc)) {
Damien Lespiau241bfc32013-09-25 16:45:37 +01001561 const struct drm_display_mode *adjusted_mode;
Matt Roperf4510a22014-04-01 15:22:40 -07001562 int cpp = crtc->primary->fb->bits_per_pixel / 8;
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001563 if (IS_GEN2(dev))
1564 cpp = 4;
1565
Damien Lespiau241bfc32013-09-25 16:45:37 +01001566 adjusted_mode = &to_intel_crtc(crtc)->config.adjusted_mode;
1567 planea_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001568 wm_info, fifo_size, cpp,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001569 latency_ns);
1570 enabled = crtc;
1571 } else
1572 planea_wm = fifo_size - wm_info->guard_size;
1573
1574 fifo_size = dev_priv->display.get_fifo_size(dev, 1);
1575 crtc = intel_get_crtc_for_plane(dev, 1);
Chris Wilson3490ea52013-01-07 10:11:40 +00001576 if (intel_crtc_active(crtc)) {
Damien Lespiau241bfc32013-09-25 16:45:37 +01001577 const struct drm_display_mode *adjusted_mode;
Matt Roperf4510a22014-04-01 15:22:40 -07001578 int cpp = crtc->primary->fb->bits_per_pixel / 8;
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001579 if (IS_GEN2(dev))
1580 cpp = 4;
1581
Damien Lespiau241bfc32013-09-25 16:45:37 +01001582 adjusted_mode = &to_intel_crtc(crtc)->config.adjusted_mode;
1583 planeb_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001584 wm_info, fifo_size, cpp,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001585 latency_ns);
1586 if (enabled == NULL)
1587 enabled = crtc;
1588 else
1589 enabled = NULL;
1590 } else
1591 planeb_wm = fifo_size - wm_info->guard_size;
1592
1593 DRM_DEBUG_KMS("FIFO watermarks - A: %d, B: %d\n", planea_wm, planeb_wm);
1594
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02001595 if (IS_I915GM(dev) && enabled) {
Matt Roper2ff8fde2014-07-08 07:50:07 -07001596 struct drm_i915_gem_object *obj;
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02001597
Matt Roper2ff8fde2014-07-08 07:50:07 -07001598 obj = intel_fb_obj(enabled->primary->fb);
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02001599
1600 /* self-refresh seems busted with untiled */
Matt Roper2ff8fde2014-07-08 07:50:07 -07001601 if (obj->tiling_mode == I915_TILING_NONE)
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02001602 enabled = NULL;
1603 }
1604
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001605 /*
1606 * Overlay gets an aggressive default since video jitter is bad.
1607 */
1608 cwm = 2;
1609
1610 /* Play safe and disable self-refresh before adjusting watermarks. */
Imre Deak5209b1f2014-07-01 12:36:17 +03001611 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001612
1613 /* Calc sr entries for one plane configs */
1614 if (HAS_FW_BLC(dev) && enabled) {
1615 /* self-refresh has much higher latency */
1616 static const int sr_latency_ns = 6000;
Ville Syrjälä4fe85902013-09-04 18:25:22 +03001617 const struct drm_display_mode *adjusted_mode =
1618 &to_intel_crtc(enabled)->config.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001619 int clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -08001620 int htotal = adjusted_mode->crtc_htotal;
Daniel Vetterf727b492013-11-20 15:02:10 +01001621 int hdisplay = to_intel_crtc(enabled)->config.pipe_src_w;
Matt Roperf4510a22014-04-01 15:22:40 -07001622 int pixel_size = enabled->primary->fb->bits_per_pixel / 8;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001623 unsigned long line_time_us;
1624 int entries;
1625
Ville Syrjälä922044c2014-02-14 14:18:57 +02001626 line_time_us = max(htotal * 1000 / clock, 1);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001627
1628 /* Use ns/us then divide to preserve precision */
1629 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
1630 pixel_size * hdisplay;
1631 entries = DIV_ROUND_UP(entries, wm_info->cacheline_size);
1632 DRM_DEBUG_KMS("self-refresh entries: %d\n", entries);
1633 srwm = wm_info->fifo_size - entries;
1634 if (srwm < 0)
1635 srwm = 1;
1636
1637 if (IS_I945G(dev) || IS_I945GM(dev))
1638 I915_WRITE(FW_BLC_SELF,
1639 FW_BLC_SELF_FIFO_MASK | (srwm & 0xff));
1640 else if (IS_I915GM(dev))
1641 I915_WRITE(FW_BLC_SELF, srwm & 0x3f);
1642 }
1643
1644 DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d, B: %d, C: %d, SR %d\n",
1645 planea_wm, planeb_wm, cwm, srwm);
1646
1647 fwater_lo = ((planeb_wm & 0x3f) << 16) | (planea_wm & 0x3f);
1648 fwater_hi = (cwm & 0x1f);
1649
1650 /* Set request length to 8 cachelines per fetch */
1651 fwater_lo = fwater_lo | (1 << 24) | (1 << 8);
1652 fwater_hi = fwater_hi | (1 << 8);
1653
1654 I915_WRITE(FW_BLC, fwater_lo);
1655 I915_WRITE(FW_BLC2, fwater_hi);
1656
Imre Deak5209b1f2014-07-01 12:36:17 +03001657 if (enabled)
1658 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001659}
1660
Daniel Vetterfeb56b92013-12-14 20:38:30 -02001661static void i845_update_wm(struct drm_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001662{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001663 struct drm_device *dev = unused_crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001664 struct drm_i915_private *dev_priv = dev->dev_private;
1665 struct drm_crtc *crtc;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001666 const struct drm_display_mode *adjusted_mode;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001667 uint32_t fwater_lo;
1668 int planea_wm;
1669
1670 crtc = single_enabled_crtc(dev);
1671 if (crtc == NULL)
1672 return;
1673
Damien Lespiau241bfc32013-09-25 16:45:37 +01001674 adjusted_mode = &to_intel_crtc(crtc)->config.adjusted_mode;
1675 planea_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Daniel Vetterfeb56b92013-12-14 20:38:30 -02001676 &i845_wm_info,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001677 dev_priv->display.get_fifo_size(dev, 0),
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001678 4, latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001679 fwater_lo = I915_READ(FW_BLC) & ~0xfff;
1680 fwater_lo |= (3<<8) | planea_wm;
1681
1682 DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d\n", planea_wm);
1683
1684 I915_WRITE(FW_BLC, fwater_lo);
1685}
1686
Ville Syrjälä36587292013-07-05 11:57:16 +03001687static uint32_t ilk_pipe_pixel_rate(struct drm_device *dev,
1688 struct drm_crtc *crtc)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001689{
1690 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilsonfd4daa92013-08-27 17:04:17 +01001691 uint32_t pixel_rate;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001692
Damien Lespiau241bfc32013-09-25 16:45:37 +01001693 pixel_rate = intel_crtc->config.adjusted_mode.crtc_clock;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001694
1695 /* We only use IF-ID interlacing. If we ever use PF-ID we'll need to
1696 * adjust the pixel_rate here. */
1697
Chris Wilsonfd4daa92013-08-27 17:04:17 +01001698 if (intel_crtc->config.pch_pfit.enabled) {
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001699 uint64_t pipe_w, pipe_h, pfit_w, pfit_h;
Chris Wilsonfd4daa92013-08-27 17:04:17 +01001700 uint32_t pfit_size = intel_crtc->config.pch_pfit.size;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001701
Ville Syrjälä37327ab2013-09-04 18:25:28 +03001702 pipe_w = intel_crtc->config.pipe_src_w;
1703 pipe_h = intel_crtc->config.pipe_src_h;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001704 pfit_w = (pfit_size >> 16) & 0xFFFF;
1705 pfit_h = pfit_size & 0xFFFF;
1706 if (pipe_w < pfit_w)
1707 pipe_w = pfit_w;
1708 if (pipe_h < pfit_h)
1709 pipe_h = pfit_h;
1710
1711 pixel_rate = div_u64((uint64_t) pixel_rate * pipe_w * pipe_h,
1712 pfit_w * pfit_h);
1713 }
1714
1715 return pixel_rate;
1716}
1717
Ville Syrjälä37126462013-08-01 16:18:55 +03001718/* latency must be in 0.1us units. */
Ville Syrjälä23297042013-07-05 11:57:17 +03001719static uint32_t ilk_wm_method1(uint32_t pixel_rate, uint8_t bytes_per_pixel,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001720 uint32_t latency)
1721{
1722 uint64_t ret;
1723
Ville Syrjälä3312ba62013-08-01 16:18:53 +03001724 if (WARN(latency == 0, "Latency value missing\n"))
1725 return UINT_MAX;
1726
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001727 ret = (uint64_t) pixel_rate * bytes_per_pixel * latency;
1728 ret = DIV_ROUND_UP_ULL(ret, 64 * 10000) + 2;
1729
1730 return ret;
1731}
1732
Ville Syrjälä37126462013-08-01 16:18:55 +03001733/* latency must be in 0.1us units. */
Ville Syrjälä23297042013-07-05 11:57:17 +03001734static uint32_t ilk_wm_method2(uint32_t pixel_rate, uint32_t pipe_htotal,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001735 uint32_t horiz_pixels, uint8_t bytes_per_pixel,
1736 uint32_t latency)
1737{
1738 uint32_t ret;
1739
Ville Syrjälä3312ba62013-08-01 16:18:53 +03001740 if (WARN(latency == 0, "Latency value missing\n"))
1741 return UINT_MAX;
1742
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001743 ret = (latency * pixel_rate) / (pipe_htotal * 10000);
1744 ret = (ret + 1) * horiz_pixels * bytes_per_pixel;
1745 ret = DIV_ROUND_UP(ret, 64) + 2;
1746 return ret;
1747}
1748
Ville Syrjälä23297042013-07-05 11:57:17 +03001749static uint32_t ilk_wm_fbc(uint32_t pri_val, uint32_t horiz_pixels,
Paulo Zanonicca32e92013-05-31 11:45:06 -03001750 uint8_t bytes_per_pixel)
1751{
1752 return DIV_ROUND_UP(pri_val * 64, horiz_pixels * bytes_per_pixel) + 2;
1753}
1754
Imre Deak820c1982013-12-17 14:46:36 +02001755struct ilk_pipe_wm_parameters {
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001756 bool active;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001757 uint32_t pipe_htotal;
1758 uint32_t pixel_rate;
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001759 struct intel_plane_wm_parameters pri;
1760 struct intel_plane_wm_parameters spr;
1761 struct intel_plane_wm_parameters cur;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001762};
1763
Imre Deak820c1982013-12-17 14:46:36 +02001764struct ilk_wm_maximums {
Paulo Zanonicca32e92013-05-31 11:45:06 -03001765 uint16_t pri;
1766 uint16_t spr;
1767 uint16_t cur;
1768 uint16_t fbc;
1769};
1770
Ville Syrjälä240264f2013-08-07 13:29:12 +03001771/* used in computing the new watermarks state */
1772struct intel_wm_config {
1773 unsigned int num_pipes_active;
1774 bool sprites_enabled;
1775 bool sprites_scaled;
Ville Syrjälä240264f2013-08-07 13:29:12 +03001776};
1777
Ville Syrjälä37126462013-08-01 16:18:55 +03001778/*
1779 * For both WM_PIPE and WM_LP.
1780 * mem_value must be in 0.1us units.
1781 */
Imre Deak820c1982013-12-17 14:46:36 +02001782static uint32_t ilk_compute_pri_wm(const struct ilk_pipe_wm_parameters *params,
Paulo Zanonicca32e92013-05-31 11:45:06 -03001783 uint32_t mem_value,
1784 bool is_lp)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001785{
Paulo Zanonicca32e92013-05-31 11:45:06 -03001786 uint32_t method1, method2;
1787
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001788 if (!params->active || !params->pri.enabled)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001789 return 0;
1790
Ville Syrjälä23297042013-07-05 11:57:17 +03001791 method1 = ilk_wm_method1(params->pixel_rate,
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001792 params->pri.bytes_per_pixel,
Paulo Zanonicca32e92013-05-31 11:45:06 -03001793 mem_value);
1794
1795 if (!is_lp)
1796 return method1;
1797
Ville Syrjälä23297042013-07-05 11:57:17 +03001798 method2 = ilk_wm_method2(params->pixel_rate,
Paulo Zanonicca32e92013-05-31 11:45:06 -03001799 params->pipe_htotal,
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001800 params->pri.horiz_pixels,
1801 params->pri.bytes_per_pixel,
Paulo Zanonicca32e92013-05-31 11:45:06 -03001802 mem_value);
1803
1804 return min(method1, method2);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001805}
1806
Ville Syrjälä37126462013-08-01 16:18:55 +03001807/*
1808 * For both WM_PIPE and WM_LP.
1809 * mem_value must be in 0.1us units.
1810 */
Imre Deak820c1982013-12-17 14:46:36 +02001811static uint32_t ilk_compute_spr_wm(const struct ilk_pipe_wm_parameters *params,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001812 uint32_t mem_value)
1813{
1814 uint32_t method1, method2;
1815
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001816 if (!params->active || !params->spr.enabled)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001817 return 0;
1818
Ville Syrjälä23297042013-07-05 11:57:17 +03001819 method1 = ilk_wm_method1(params->pixel_rate,
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001820 params->spr.bytes_per_pixel,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001821 mem_value);
Ville Syrjälä23297042013-07-05 11:57:17 +03001822 method2 = ilk_wm_method2(params->pixel_rate,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001823 params->pipe_htotal,
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001824 params->spr.horiz_pixels,
1825 params->spr.bytes_per_pixel,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001826 mem_value);
1827 return min(method1, method2);
1828}
1829
Ville Syrjälä37126462013-08-01 16:18:55 +03001830/*
1831 * For both WM_PIPE and WM_LP.
1832 * mem_value must be in 0.1us units.
1833 */
Imre Deak820c1982013-12-17 14:46:36 +02001834static uint32_t ilk_compute_cur_wm(const struct ilk_pipe_wm_parameters *params,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001835 uint32_t mem_value)
1836{
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001837 if (!params->active || !params->cur.enabled)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001838 return 0;
1839
Ville Syrjälä23297042013-07-05 11:57:17 +03001840 return ilk_wm_method2(params->pixel_rate,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001841 params->pipe_htotal,
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001842 params->cur.horiz_pixels,
1843 params->cur.bytes_per_pixel,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001844 mem_value);
1845}
1846
Paulo Zanonicca32e92013-05-31 11:45:06 -03001847/* Only for WM_LP. */
Imre Deak820c1982013-12-17 14:46:36 +02001848static uint32_t ilk_compute_fbc_wm(const struct ilk_pipe_wm_parameters *params,
Ville Syrjälä1fda9882013-07-05 11:57:19 +03001849 uint32_t pri_val)
Paulo Zanonicca32e92013-05-31 11:45:06 -03001850{
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001851 if (!params->active || !params->pri.enabled)
Paulo Zanonicca32e92013-05-31 11:45:06 -03001852 return 0;
1853
Ville Syrjälä23297042013-07-05 11:57:17 +03001854 return ilk_wm_fbc(pri_val,
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001855 params->pri.horiz_pixels,
1856 params->pri.bytes_per_pixel);
Paulo Zanonicca32e92013-05-31 11:45:06 -03001857}
1858
Ville Syrjälä158ae642013-08-07 13:28:19 +03001859static unsigned int ilk_display_fifo_size(const struct drm_device *dev)
1860{
Ville Syrjälä416f4722013-11-02 21:07:46 -07001861 if (INTEL_INFO(dev)->gen >= 8)
1862 return 3072;
1863 else if (INTEL_INFO(dev)->gen >= 7)
Ville Syrjälä158ae642013-08-07 13:28:19 +03001864 return 768;
1865 else
1866 return 512;
1867}
1868
Ville Syrjälä4e975082014-03-07 18:32:11 +02001869static unsigned int ilk_plane_wm_reg_max(const struct drm_device *dev,
1870 int level, bool is_sprite)
1871{
1872 if (INTEL_INFO(dev)->gen >= 8)
1873 /* BDW primary/sprite plane watermarks */
1874 return level == 0 ? 255 : 2047;
1875 else if (INTEL_INFO(dev)->gen >= 7)
1876 /* IVB/HSW primary/sprite plane watermarks */
1877 return level == 0 ? 127 : 1023;
1878 else if (!is_sprite)
1879 /* ILK/SNB primary plane watermarks */
1880 return level == 0 ? 127 : 511;
1881 else
1882 /* ILK/SNB sprite plane watermarks */
1883 return level == 0 ? 63 : 255;
1884}
1885
1886static unsigned int ilk_cursor_wm_reg_max(const struct drm_device *dev,
1887 int level)
1888{
1889 if (INTEL_INFO(dev)->gen >= 7)
1890 return level == 0 ? 63 : 255;
1891 else
1892 return level == 0 ? 31 : 63;
1893}
1894
1895static unsigned int ilk_fbc_wm_reg_max(const struct drm_device *dev)
1896{
1897 if (INTEL_INFO(dev)->gen >= 8)
1898 return 31;
1899 else
1900 return 15;
1901}
1902
Ville Syrjälä158ae642013-08-07 13:28:19 +03001903/* Calculate the maximum primary/sprite plane watermark */
1904static unsigned int ilk_plane_wm_max(const struct drm_device *dev,
1905 int level,
Ville Syrjälä240264f2013-08-07 13:29:12 +03001906 const struct intel_wm_config *config,
Ville Syrjälä158ae642013-08-07 13:28:19 +03001907 enum intel_ddb_partitioning ddb_partitioning,
1908 bool is_sprite)
1909{
1910 unsigned int fifo_size = ilk_display_fifo_size(dev);
Ville Syrjälä158ae642013-08-07 13:28:19 +03001911
1912 /* if sprites aren't enabled, sprites get nothing */
Ville Syrjälä240264f2013-08-07 13:29:12 +03001913 if (is_sprite && !config->sprites_enabled)
Ville Syrjälä158ae642013-08-07 13:28:19 +03001914 return 0;
1915
1916 /* HSW allows LP1+ watermarks even with multiple pipes */
Ville Syrjälä240264f2013-08-07 13:29:12 +03001917 if (level == 0 || config->num_pipes_active > 1) {
Ville Syrjälä158ae642013-08-07 13:28:19 +03001918 fifo_size /= INTEL_INFO(dev)->num_pipes;
1919
1920 /*
1921 * For some reason the non self refresh
1922 * FIFO size is only half of the self
1923 * refresh FIFO size on ILK/SNB.
1924 */
1925 if (INTEL_INFO(dev)->gen <= 6)
1926 fifo_size /= 2;
1927 }
1928
Ville Syrjälä240264f2013-08-07 13:29:12 +03001929 if (config->sprites_enabled) {
Ville Syrjälä158ae642013-08-07 13:28:19 +03001930 /* level 0 is always calculated with 1:1 split */
1931 if (level > 0 && ddb_partitioning == INTEL_DDB_PART_5_6) {
1932 if (is_sprite)
1933 fifo_size *= 5;
1934 fifo_size /= 6;
1935 } else {
1936 fifo_size /= 2;
1937 }
1938 }
1939
1940 /* clamp to max that the registers can hold */
Ville Syrjälä4e975082014-03-07 18:32:11 +02001941 return min(fifo_size, ilk_plane_wm_reg_max(dev, level, is_sprite));
Ville Syrjälä158ae642013-08-07 13:28:19 +03001942}
1943
1944/* Calculate the maximum cursor plane watermark */
1945static unsigned int ilk_cursor_wm_max(const struct drm_device *dev,
Ville Syrjälä240264f2013-08-07 13:29:12 +03001946 int level,
1947 const struct intel_wm_config *config)
Ville Syrjälä158ae642013-08-07 13:28:19 +03001948{
1949 /* HSW LP1+ watermarks w/ multiple pipes */
Ville Syrjälä240264f2013-08-07 13:29:12 +03001950 if (level > 0 && config->num_pipes_active > 1)
Ville Syrjälä158ae642013-08-07 13:28:19 +03001951 return 64;
1952
1953 /* otherwise just report max that registers can hold */
Ville Syrjälä4e975082014-03-07 18:32:11 +02001954 return ilk_cursor_wm_reg_max(dev, level);
Ville Syrjälä158ae642013-08-07 13:28:19 +03001955}
1956
Damien Lespiaud34ff9c2014-01-06 19:17:23 +00001957static void ilk_compute_wm_maximums(const struct drm_device *dev,
Ville Syrjälä34982fe2013-10-09 19:18:09 +03001958 int level,
1959 const struct intel_wm_config *config,
1960 enum intel_ddb_partitioning ddb_partitioning,
Imre Deak820c1982013-12-17 14:46:36 +02001961 struct ilk_wm_maximums *max)
Ville Syrjälä158ae642013-08-07 13:28:19 +03001962{
Ville Syrjälä240264f2013-08-07 13:29:12 +03001963 max->pri = ilk_plane_wm_max(dev, level, config, ddb_partitioning, false);
1964 max->spr = ilk_plane_wm_max(dev, level, config, ddb_partitioning, true);
1965 max->cur = ilk_cursor_wm_max(dev, level, config);
Ville Syrjälä4e975082014-03-07 18:32:11 +02001966 max->fbc = ilk_fbc_wm_reg_max(dev);
Ville Syrjälä158ae642013-08-07 13:28:19 +03001967}
1968
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03001969static void ilk_compute_wm_reg_maximums(struct drm_device *dev,
1970 int level,
1971 struct ilk_wm_maximums *max)
1972{
1973 max->pri = ilk_plane_wm_reg_max(dev, level, false);
1974 max->spr = ilk_plane_wm_reg_max(dev, level, true);
1975 max->cur = ilk_cursor_wm_reg_max(dev, level);
1976 max->fbc = ilk_fbc_wm_reg_max(dev);
1977}
1978
Ville Syrjäläd9395652013-10-09 19:18:10 +03001979static bool ilk_validate_wm_level(int level,
Imre Deak820c1982013-12-17 14:46:36 +02001980 const struct ilk_wm_maximums *max,
Ville Syrjäläd9395652013-10-09 19:18:10 +03001981 struct intel_wm_level *result)
Ville Syrjäläa9786a12013-08-07 13:24:47 +03001982{
1983 bool ret;
1984
1985 /* already determined to be invalid? */
1986 if (!result->enable)
1987 return false;
1988
1989 result->enable = result->pri_val <= max->pri &&
1990 result->spr_val <= max->spr &&
1991 result->cur_val <= max->cur;
1992
1993 ret = result->enable;
1994
1995 /*
1996 * HACK until we can pre-compute everything,
1997 * and thus fail gracefully if LP0 watermarks
1998 * are exceeded...
1999 */
2000 if (level == 0 && !result->enable) {
2001 if (result->pri_val > max->pri)
2002 DRM_DEBUG_KMS("Primary WM%d too large %u (max %u)\n",
2003 level, result->pri_val, max->pri);
2004 if (result->spr_val > max->spr)
2005 DRM_DEBUG_KMS("Sprite WM%d too large %u (max %u)\n",
2006 level, result->spr_val, max->spr);
2007 if (result->cur_val > max->cur)
2008 DRM_DEBUG_KMS("Cursor WM%d too large %u (max %u)\n",
2009 level, result->cur_val, max->cur);
2010
2011 result->pri_val = min_t(uint32_t, result->pri_val, max->pri);
2012 result->spr_val = min_t(uint32_t, result->spr_val, max->spr);
2013 result->cur_val = min_t(uint32_t, result->cur_val, max->cur);
2014 result->enable = true;
2015 }
2016
Ville Syrjäläa9786a12013-08-07 13:24:47 +03002017 return ret;
2018}
2019
Damien Lespiaud34ff9c2014-01-06 19:17:23 +00002020static void ilk_compute_wm_level(const struct drm_i915_private *dev_priv,
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03002021 int level,
Imre Deak820c1982013-12-17 14:46:36 +02002022 const struct ilk_pipe_wm_parameters *p,
Ville Syrjälä1fd527c2013-08-06 22:24:05 +03002023 struct intel_wm_level *result)
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03002024{
2025 uint16_t pri_latency = dev_priv->wm.pri_latency[level];
2026 uint16_t spr_latency = dev_priv->wm.spr_latency[level];
2027 uint16_t cur_latency = dev_priv->wm.cur_latency[level];
2028
2029 /* WM1+ latency values stored in 0.5us units */
2030 if (level > 0) {
2031 pri_latency *= 5;
2032 spr_latency *= 5;
2033 cur_latency *= 5;
2034 }
2035
2036 result->pri_val = ilk_compute_pri_wm(p, pri_latency, level);
2037 result->spr_val = ilk_compute_spr_wm(p, spr_latency);
2038 result->cur_val = ilk_compute_cur_wm(p, cur_latency);
2039 result->fbc_val = ilk_compute_fbc_wm(p, result->pri_val);
2040 result->enable = true;
2041}
2042
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002043static uint32_t
2044hsw_compute_linetime_wm(struct drm_device *dev, struct drm_crtc *crtc)
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002045{
2046 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002047 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002048 struct drm_display_mode *mode = &intel_crtc->config.adjusted_mode;
Paulo Zanoni85a02de2013-05-03 17:23:43 -03002049 u32 linetime, ips_linetime;
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002050
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002051 if (!intel_crtc_active(crtc))
2052 return 0;
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002053
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002054 /* The WM are computed with base on how long it takes to fill a single
2055 * row at the given clock rate, multiplied by 8.
2056 * */
Jesse Barnesfec8cba2013-11-27 11:10:26 -08002057 linetime = DIV_ROUND_CLOSEST(mode->crtc_htotal * 1000 * 8,
2058 mode->crtc_clock);
2059 ips_linetime = DIV_ROUND_CLOSEST(mode->crtc_htotal * 1000 * 8,
Paulo Zanoni85a02de2013-05-03 17:23:43 -03002060 intel_ddi_get_cdclk_freq(dev_priv));
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002061
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002062 return PIPE_WM_LINETIME_IPS_LINETIME(ips_linetime) |
2063 PIPE_WM_LINETIME_TIME(linetime);
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002064}
2065
Ville Syrjälä12b134d2013-07-05 11:57:21 +03002066static void intel_read_wm_latency(struct drm_device *dev, uint16_t wm[5])
2067{
2068 struct drm_i915_private *dev_priv = dev->dev_private;
2069
Ville Syrjäläa42a5712014-01-07 16:14:08 +02002070 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Ville Syrjälä12b134d2013-07-05 11:57:21 +03002071 uint64_t sskpd = I915_READ64(MCH_SSKPD);
2072
2073 wm[0] = (sskpd >> 56) & 0xFF;
2074 if (wm[0] == 0)
2075 wm[0] = sskpd & 0xF;
Ville Syrjäläe5d50192013-07-05 11:57:22 +03002076 wm[1] = (sskpd >> 4) & 0xFF;
2077 wm[2] = (sskpd >> 12) & 0xFF;
2078 wm[3] = (sskpd >> 20) & 0x1FF;
2079 wm[4] = (sskpd >> 32) & 0x1FF;
Ville Syrjälä63cf9a12013-07-05 11:57:23 +03002080 } else if (INTEL_INFO(dev)->gen >= 6) {
2081 uint32_t sskpd = I915_READ(MCH_SSKPD);
2082
2083 wm[0] = (sskpd >> SSKPD_WM0_SHIFT) & SSKPD_WM_MASK;
2084 wm[1] = (sskpd >> SSKPD_WM1_SHIFT) & SSKPD_WM_MASK;
2085 wm[2] = (sskpd >> SSKPD_WM2_SHIFT) & SSKPD_WM_MASK;
2086 wm[3] = (sskpd >> SSKPD_WM3_SHIFT) & SSKPD_WM_MASK;
Ville Syrjälä3a88d0a2013-08-01 16:18:49 +03002087 } else if (INTEL_INFO(dev)->gen >= 5) {
2088 uint32_t mltr = I915_READ(MLTR_ILK);
2089
2090 /* ILK primary LP0 latency is 700 ns */
2091 wm[0] = 7;
2092 wm[1] = (mltr >> MLTR_WM1_SHIFT) & ILK_SRLT_MASK;
2093 wm[2] = (mltr >> MLTR_WM2_SHIFT) & ILK_SRLT_MASK;
Ville Syrjälä12b134d2013-07-05 11:57:21 +03002094 }
2095}
2096
Ville Syrjälä53615a52013-08-01 16:18:50 +03002097static void intel_fixup_spr_wm_latency(struct drm_device *dev, uint16_t wm[5])
2098{
2099 /* ILK sprite LP0 latency is 1300 ns */
2100 if (INTEL_INFO(dev)->gen == 5)
2101 wm[0] = 13;
2102}
2103
2104static void intel_fixup_cur_wm_latency(struct drm_device *dev, uint16_t wm[5])
2105{
2106 /* ILK cursor LP0 latency is 1300 ns */
2107 if (INTEL_INFO(dev)->gen == 5)
2108 wm[0] = 13;
2109
2110 /* WaDoubleCursorLP3Latency:ivb */
2111 if (IS_IVYBRIDGE(dev))
2112 wm[3] *= 2;
2113}
2114
Damien Lespiau546c81f2014-05-13 15:30:26 +01002115int ilk_wm_max_level(const struct drm_device *dev)
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03002116{
2117 /* how many WM levels are we expecting */
Ville Syrjäläa42a5712014-01-07 16:14:08 +02002118 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03002119 return 4;
2120 else if (INTEL_INFO(dev)->gen >= 6)
2121 return 3;
2122 else
2123 return 2;
2124}
2125
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002126static void intel_print_wm_latency(struct drm_device *dev,
2127 const char *name,
2128 const uint16_t wm[5])
2129{
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03002130 int level, max_level = ilk_wm_max_level(dev);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002131
2132 for (level = 0; level <= max_level; level++) {
2133 unsigned int latency = wm[level];
2134
2135 if (latency == 0) {
2136 DRM_ERROR("%s WM%d latency not provided\n",
2137 name, level);
2138 continue;
2139 }
2140
2141 /* WM1+ latency values in 0.5us units */
2142 if (level > 0)
2143 latency *= 5;
2144
2145 DRM_DEBUG_KMS("%s WM%d latency %u (%u.%u usec)\n",
2146 name, level, wm[level],
2147 latency / 10, latency % 10);
2148 }
2149}
2150
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002151static bool ilk_increase_wm_latency(struct drm_i915_private *dev_priv,
2152 uint16_t wm[5], uint16_t min)
2153{
2154 int level, max_level = ilk_wm_max_level(dev_priv->dev);
2155
2156 if (wm[0] >= min)
2157 return false;
2158
2159 wm[0] = max(wm[0], min);
2160 for (level = 1; level <= max_level; level++)
2161 wm[level] = max_t(uint16_t, wm[level], DIV_ROUND_UP(min, 5));
2162
2163 return true;
2164}
2165
2166static void snb_wm_latency_quirk(struct drm_device *dev)
2167{
2168 struct drm_i915_private *dev_priv = dev->dev_private;
2169 bool changed;
2170
2171 /*
2172 * The BIOS provided WM memory latency values are often
2173 * inadequate for high resolution displays. Adjust them.
2174 */
2175 changed = ilk_increase_wm_latency(dev_priv, dev_priv->wm.pri_latency, 12) |
2176 ilk_increase_wm_latency(dev_priv, dev_priv->wm.spr_latency, 12) |
2177 ilk_increase_wm_latency(dev_priv, dev_priv->wm.cur_latency, 12);
2178
2179 if (!changed)
2180 return;
2181
2182 DRM_DEBUG_KMS("WM latency values increased to avoid potential underruns\n");
2183 intel_print_wm_latency(dev, "Primary", dev_priv->wm.pri_latency);
2184 intel_print_wm_latency(dev, "Sprite", dev_priv->wm.spr_latency);
2185 intel_print_wm_latency(dev, "Cursor", dev_priv->wm.cur_latency);
2186}
2187
Damien Lespiaufa50ad62014-03-17 18:01:16 +00002188static void ilk_setup_wm_latency(struct drm_device *dev)
Ville Syrjälä53615a52013-08-01 16:18:50 +03002189{
2190 struct drm_i915_private *dev_priv = dev->dev_private;
2191
2192 intel_read_wm_latency(dev, dev_priv->wm.pri_latency);
2193
2194 memcpy(dev_priv->wm.spr_latency, dev_priv->wm.pri_latency,
2195 sizeof(dev_priv->wm.pri_latency));
2196 memcpy(dev_priv->wm.cur_latency, dev_priv->wm.pri_latency,
2197 sizeof(dev_priv->wm.pri_latency));
2198
2199 intel_fixup_spr_wm_latency(dev, dev_priv->wm.spr_latency);
2200 intel_fixup_cur_wm_latency(dev, dev_priv->wm.cur_latency);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002201
2202 intel_print_wm_latency(dev, "Primary", dev_priv->wm.pri_latency);
2203 intel_print_wm_latency(dev, "Sprite", dev_priv->wm.spr_latency);
2204 intel_print_wm_latency(dev, "Cursor", dev_priv->wm.cur_latency);
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002205
2206 if (IS_GEN6(dev))
2207 snb_wm_latency_quirk(dev);
Ville Syrjälä53615a52013-08-01 16:18:50 +03002208}
2209
Imre Deak820c1982013-12-17 14:46:36 +02002210static void ilk_compute_wm_parameters(struct drm_crtc *crtc,
Ville Syrjälä2a44b762014-03-07 18:32:09 +02002211 struct ilk_pipe_wm_parameters *p)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002212{
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03002213 struct drm_device *dev = crtc->dev;
2214 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2215 enum pipe pipe = intel_crtc->pipe;
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03002216 struct drm_plane *plane;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002217
Ville Syrjälä2a44b762014-03-07 18:32:09 +02002218 if (!intel_crtc_active(crtc))
2219 return;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002220
Ville Syrjälä2a44b762014-03-07 18:32:09 +02002221 p->active = true;
2222 p->pipe_htotal = intel_crtc->config.adjusted_mode.crtc_htotal;
2223 p->pixel_rate = ilk_pipe_pixel_rate(dev, crtc);
2224 p->pri.bytes_per_pixel = crtc->primary->fb->bits_per_pixel / 8;
2225 p->cur.bytes_per_pixel = 4;
2226 p->pri.horiz_pixels = intel_crtc->config.pipe_src_w;
2227 p->cur.horiz_pixels = intel_crtc->cursor_width;
2228 /* TODO: for now, assume primary and cursor planes are always enabled. */
2229 p->pri.enabled = true;
2230 p->cur.enabled = true;
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03002231
Matt Roperaf2b6532014-04-01 15:22:32 -07002232 drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002233 struct intel_plane *intel_plane = to_intel_plane(plane);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002234
Ville Syrjälä2a44b762014-03-07 18:32:09 +02002235 if (intel_plane->pipe == pipe) {
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03002236 p->spr = intel_plane->wm;
Ville Syrjälä2a44b762014-03-07 18:32:09 +02002237 break;
2238 }
2239 }
2240}
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002241
Ville Syrjälä2a44b762014-03-07 18:32:09 +02002242static void ilk_compute_wm_config(struct drm_device *dev,
2243 struct intel_wm_config *config)
2244{
2245 struct intel_crtc *intel_crtc;
2246
2247 /* Compute the currently _active_ config */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01002248 for_each_intel_crtc(dev, intel_crtc) {
Ville Syrjälä2a44b762014-03-07 18:32:09 +02002249 const struct intel_pipe_wm *wm = &intel_crtc->wm.active;
2250
2251 if (!wm->pipe_enabled)
2252 continue;
2253
2254 config->sprites_enabled |= wm->sprites_enabled;
2255 config->sprites_scaled |= wm->sprites_scaled;
2256 config->num_pipes_active++;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002257 }
2258}
2259
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002260/* Compute new watermarks for the pipe */
2261static bool intel_compute_pipe_wm(struct drm_crtc *crtc,
Imre Deak820c1982013-12-17 14:46:36 +02002262 const struct ilk_pipe_wm_parameters *params,
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002263 struct intel_pipe_wm *pipe_wm)
2264{
2265 struct drm_device *dev = crtc->dev;
Damien Lespiaud34ff9c2014-01-06 19:17:23 +00002266 const struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002267 int level, max_level = ilk_wm_max_level(dev);
2268 /* LP0 watermark maximums depend on this pipe alone */
2269 struct intel_wm_config config = {
2270 .num_pipes_active = 1,
2271 .sprites_enabled = params->spr.enabled,
2272 .sprites_scaled = params->spr.scaled,
2273 };
Imre Deak820c1982013-12-17 14:46:36 +02002274 struct ilk_wm_maximums max;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002275
Ville Syrjälä2a44b762014-03-07 18:32:09 +02002276 pipe_wm->pipe_enabled = params->active;
2277 pipe_wm->sprites_enabled = params->spr.enabled;
2278 pipe_wm->sprites_scaled = params->spr.scaled;
2279
Ville Syrjälä7b39a0b2013-12-05 15:51:30 +02002280 /* ILK/SNB: LP2+ watermarks only w/o sprites */
2281 if (INTEL_INFO(dev)->gen <= 6 && params->spr.enabled)
2282 max_level = 1;
2283
2284 /* ILK/SNB/IVB: LP1+ watermarks only w/o scaling */
2285 if (params->spr.scaled)
2286 max_level = 0;
2287
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002288 ilk_compute_wm_level(dev_priv, 0, params, &pipe_wm->wm[0]);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002289
Ville Syrjäläa42a5712014-01-07 16:14:08 +02002290 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Ville Syrjäläce0e0712013-12-05 15:51:36 +02002291 pipe_wm->linetime = hsw_compute_linetime_wm(dev, crtc);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002292
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002293 /* LP0 watermarks always use 1/2 DDB partitioning */
2294 ilk_compute_wm_maximums(dev, 0, &config, INTEL_DDB_PART_1_2, &max);
2295
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002296 /* At least LP0 must be valid */
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002297 if (!ilk_validate_wm_level(0, &max, &pipe_wm->wm[0]))
2298 return false;
2299
2300 ilk_compute_wm_reg_maximums(dev, 1, &max);
2301
2302 for (level = 1; level <= max_level; level++) {
2303 struct intel_wm_level wm = {};
2304
2305 ilk_compute_wm_level(dev_priv, level, params, &wm);
2306
2307 /*
2308 * Disable any watermark level that exceeds the
2309 * register maximums since such watermarks are
2310 * always invalid.
2311 */
2312 if (!ilk_validate_wm_level(level, &max, &wm))
2313 break;
2314
2315 pipe_wm->wm[level] = wm;
2316 }
2317
2318 return true;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002319}
2320
2321/*
2322 * Merge the watermarks from all active pipes for a specific level.
2323 */
2324static void ilk_merge_wm_level(struct drm_device *dev,
2325 int level,
2326 struct intel_wm_level *ret_wm)
2327{
2328 const struct intel_crtc *intel_crtc;
2329
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002330 ret_wm->enable = true;
2331
Damien Lespiaud3fcc802014-05-13 23:32:22 +01002332 for_each_intel_crtc(dev, intel_crtc) {
Ville Syrjäläfe392ef2014-03-07 18:32:10 +02002333 const struct intel_pipe_wm *active = &intel_crtc->wm.active;
2334 const struct intel_wm_level *wm = &active->wm[level];
2335
2336 if (!active->pipe_enabled)
2337 continue;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002338
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002339 /*
2340 * The watermark values may have been used in the past,
2341 * so we must maintain them in the registers for some
2342 * time even if the level is now disabled.
2343 */
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002344 if (!wm->enable)
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002345 ret_wm->enable = false;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002346
2347 ret_wm->pri_val = max(ret_wm->pri_val, wm->pri_val);
2348 ret_wm->spr_val = max(ret_wm->spr_val, wm->spr_val);
2349 ret_wm->cur_val = max(ret_wm->cur_val, wm->cur_val);
2350 ret_wm->fbc_val = max(ret_wm->fbc_val, wm->fbc_val);
2351 }
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002352}
2353
2354/*
2355 * Merge all low power watermarks for all active pipes.
2356 */
2357static void ilk_wm_merge(struct drm_device *dev,
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02002358 const struct intel_wm_config *config,
Imre Deak820c1982013-12-17 14:46:36 +02002359 const struct ilk_wm_maximums *max,
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002360 struct intel_pipe_wm *merged)
2361{
2362 int level, max_level = ilk_wm_max_level(dev);
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002363 int last_enabled_level = max_level;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002364
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02002365 /* ILK/SNB/IVB: LP1+ watermarks only w/ single pipe */
2366 if ((INTEL_INFO(dev)->gen <= 6 || IS_IVYBRIDGE(dev)) &&
2367 config->num_pipes_active > 1)
2368 return;
2369
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02002370 /* ILK: FBC WM must be disabled always */
2371 merged->fbc_wm_enabled = INTEL_INFO(dev)->gen >= 6;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002372
2373 /* merge each WM1+ level */
2374 for (level = 1; level <= max_level; level++) {
2375 struct intel_wm_level *wm = &merged->wm[level];
2376
2377 ilk_merge_wm_level(dev, level, wm);
2378
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002379 if (level > last_enabled_level)
2380 wm->enable = false;
2381 else if (!ilk_validate_wm_level(level, max, wm))
2382 /* make sure all following levels get disabled */
2383 last_enabled_level = level - 1;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002384
2385 /*
2386 * The spec says it is preferred to disable
2387 * FBC WMs instead of disabling a WM level.
2388 */
2389 if (wm->fbc_val > max->fbc) {
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002390 if (wm->enable)
2391 merged->fbc_wm_enabled = false;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002392 wm->fbc_val = 0;
2393 }
2394 }
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02002395
2396 /* ILK: LP2+ must be disabled when FBC WM is disabled but FBC enabled */
2397 /*
2398 * FIXME this is racy. FBC might get enabled later.
2399 * What we should check here is whether FBC can be
2400 * enabled sometime later.
2401 */
2402 if (IS_GEN5(dev) && !merged->fbc_wm_enabled && intel_fbc_enabled(dev)) {
2403 for (level = 2; level <= max_level; level++) {
2404 struct intel_wm_level *wm = &merged->wm[level];
2405
2406 wm->enable = false;
2407 }
2408 }
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002409}
2410
Ville Syrjäläb380ca32013-10-09 19:18:01 +03002411static int ilk_wm_lp_to_level(int wm_lp, const struct intel_pipe_wm *pipe_wm)
2412{
2413 /* LP1,LP2,LP3 levels are either 1,2,3 or 1,3,4 */
2414 return wm_lp + (wm_lp >= 2 && pipe_wm->wm[4].enable);
2415}
2416
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02002417/* The value we need to program into the WM_LPx latency field */
2418static unsigned int ilk_wm_lp_latency(struct drm_device *dev, int level)
2419{
2420 struct drm_i915_private *dev_priv = dev->dev_private;
2421
Ville Syrjäläa42a5712014-01-07 16:14:08 +02002422 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02002423 return 2 * level;
2424 else
2425 return dev_priv->wm.pri_latency[level];
2426}
2427
Imre Deak820c1982013-12-17 14:46:36 +02002428static void ilk_compute_wm_results(struct drm_device *dev,
Ville Syrjälä0362c782013-10-09 19:17:57 +03002429 const struct intel_pipe_wm *merged,
Ville Syrjälä609cede2013-10-09 19:18:03 +03002430 enum intel_ddb_partitioning partitioning,
Imre Deak820c1982013-12-17 14:46:36 +02002431 struct ilk_wm_values *results)
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002432{
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002433 struct intel_crtc *intel_crtc;
2434 int level, wm_lp;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002435
Ville Syrjälä0362c782013-10-09 19:17:57 +03002436 results->enable_fbc_wm = merged->fbc_wm_enabled;
Ville Syrjälä609cede2013-10-09 19:18:03 +03002437 results->partitioning = partitioning;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002438
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002439 /* LP1+ register values */
Paulo Zanonicca32e92013-05-31 11:45:06 -03002440 for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
Ville Syrjälä1fd527c2013-08-06 22:24:05 +03002441 const struct intel_wm_level *r;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002442
Ville Syrjäläb380ca32013-10-09 19:18:01 +03002443 level = ilk_wm_lp_to_level(wm_lp, merged);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002444
Ville Syrjälä0362c782013-10-09 19:17:57 +03002445 r = &merged->wm[level];
Paulo Zanonicca32e92013-05-31 11:45:06 -03002446
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002447 /*
2448 * Maintain the watermark values even if the level is
2449 * disabled. Doing otherwise could cause underruns.
2450 */
2451 results->wm_lp[wm_lp - 1] =
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02002452 (ilk_wm_lp_latency(dev, level) << WM1_LP_LATENCY_SHIFT) |
Ville Syrjälä416f4722013-11-02 21:07:46 -07002453 (r->pri_val << WM1_LP_SR_SHIFT) |
2454 r->cur_val;
2455
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002456 if (r->enable)
2457 results->wm_lp[wm_lp - 1] |= WM1_LP_SR_EN;
2458
Ville Syrjälä416f4722013-11-02 21:07:46 -07002459 if (INTEL_INFO(dev)->gen >= 8)
2460 results->wm_lp[wm_lp - 1] |=
2461 r->fbc_val << WM1_LP_FBC_SHIFT_BDW;
2462 else
2463 results->wm_lp[wm_lp - 1] |=
2464 r->fbc_val << WM1_LP_FBC_SHIFT;
2465
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002466 /*
2467 * Always set WM1S_LP_EN when spr_val != 0, even if the
2468 * level is disabled. Doing otherwise could cause underruns.
2469 */
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02002470 if (INTEL_INFO(dev)->gen <= 6 && r->spr_val) {
2471 WARN_ON(wm_lp != 1);
2472 results->wm_lp_spr[wm_lp - 1] = WM1S_LP_EN | r->spr_val;
2473 } else
2474 results->wm_lp_spr[wm_lp - 1] = r->spr_val;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002475 }
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002476
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002477 /* LP0 register values */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01002478 for_each_intel_crtc(dev, intel_crtc) {
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002479 enum pipe pipe = intel_crtc->pipe;
2480 const struct intel_wm_level *r =
2481 &intel_crtc->wm.active.wm[0];
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002482
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002483 if (WARN_ON(!r->enable))
2484 continue;
2485
2486 results->wm_linetime[pipe] = intel_crtc->wm.active.linetime;
2487
2488 results->wm_pipe[pipe] =
2489 (r->pri_val << WM0_PIPE_PLANE_SHIFT) |
2490 (r->spr_val << WM0_PIPE_SPRITE_SHIFT) |
2491 r->cur_val;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002492 }
2493}
2494
Paulo Zanoni861f3382013-05-31 10:19:21 -03002495/* Find the result with the highest level enabled. Check for enable_fbc_wm in
2496 * case both are at the same level. Prefer r1 in case they're the same. */
Imre Deak820c1982013-12-17 14:46:36 +02002497static struct intel_pipe_wm *ilk_find_best_result(struct drm_device *dev,
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002498 struct intel_pipe_wm *r1,
2499 struct intel_pipe_wm *r2)
Paulo Zanoni861f3382013-05-31 10:19:21 -03002500{
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002501 int level, max_level = ilk_wm_max_level(dev);
2502 int level1 = 0, level2 = 0;
Paulo Zanoni861f3382013-05-31 10:19:21 -03002503
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002504 for (level = 1; level <= max_level; level++) {
2505 if (r1->wm[level].enable)
2506 level1 = level;
2507 if (r2->wm[level].enable)
2508 level2 = level;
Paulo Zanoni861f3382013-05-31 10:19:21 -03002509 }
2510
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002511 if (level1 == level2) {
2512 if (r2->fbc_wm_enabled && !r1->fbc_wm_enabled)
Paulo Zanoni861f3382013-05-31 10:19:21 -03002513 return r2;
2514 else
2515 return r1;
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002516 } else if (level1 > level2) {
Paulo Zanoni861f3382013-05-31 10:19:21 -03002517 return r1;
2518 } else {
2519 return r2;
2520 }
2521}
2522
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002523/* dirty bits used to track which watermarks need changes */
2524#define WM_DIRTY_PIPE(pipe) (1 << (pipe))
2525#define WM_DIRTY_LINETIME(pipe) (1 << (8 + (pipe)))
2526#define WM_DIRTY_LP(wm_lp) (1 << (15 + (wm_lp)))
2527#define WM_DIRTY_LP_ALL (WM_DIRTY_LP(1) | WM_DIRTY_LP(2) | WM_DIRTY_LP(3))
2528#define WM_DIRTY_FBC (1 << 24)
2529#define WM_DIRTY_DDB (1 << 25)
2530
2531static unsigned int ilk_compute_wm_dirty(struct drm_device *dev,
Imre Deak820c1982013-12-17 14:46:36 +02002532 const struct ilk_wm_values *old,
2533 const struct ilk_wm_values *new)
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002534{
2535 unsigned int dirty = 0;
2536 enum pipe pipe;
2537 int wm_lp;
2538
2539 for_each_pipe(pipe) {
2540 if (old->wm_linetime[pipe] != new->wm_linetime[pipe]) {
2541 dirty |= WM_DIRTY_LINETIME(pipe);
2542 /* Must disable LP1+ watermarks too */
2543 dirty |= WM_DIRTY_LP_ALL;
2544 }
2545
2546 if (old->wm_pipe[pipe] != new->wm_pipe[pipe]) {
2547 dirty |= WM_DIRTY_PIPE(pipe);
2548 /* Must disable LP1+ watermarks too */
2549 dirty |= WM_DIRTY_LP_ALL;
2550 }
2551 }
2552
2553 if (old->enable_fbc_wm != new->enable_fbc_wm) {
2554 dirty |= WM_DIRTY_FBC;
2555 /* Must disable LP1+ watermarks too */
2556 dirty |= WM_DIRTY_LP_ALL;
2557 }
2558
2559 if (old->partitioning != new->partitioning) {
2560 dirty |= WM_DIRTY_DDB;
2561 /* Must disable LP1+ watermarks too */
2562 dirty |= WM_DIRTY_LP_ALL;
2563 }
2564
2565 /* LP1+ watermarks already deemed dirty, no need to continue */
2566 if (dirty & WM_DIRTY_LP_ALL)
2567 return dirty;
2568
2569 /* Find the lowest numbered LP1+ watermark in need of an update... */
2570 for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
2571 if (old->wm_lp[wm_lp - 1] != new->wm_lp[wm_lp - 1] ||
2572 old->wm_lp_spr[wm_lp - 1] != new->wm_lp_spr[wm_lp - 1])
2573 break;
2574 }
2575
2576 /* ...and mark it and all higher numbered LP1+ watermarks as dirty */
2577 for (; wm_lp <= 3; wm_lp++)
2578 dirty |= WM_DIRTY_LP(wm_lp);
2579
2580 return dirty;
2581}
2582
Ville Syrjälä8553c182013-12-05 15:51:39 +02002583static bool _ilk_disable_lp_wm(struct drm_i915_private *dev_priv,
2584 unsigned int dirty)
2585{
Imre Deak820c1982013-12-17 14:46:36 +02002586 struct ilk_wm_values *previous = &dev_priv->wm.hw;
Ville Syrjälä8553c182013-12-05 15:51:39 +02002587 bool changed = false;
2588
2589 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] & WM1_LP_SR_EN) {
2590 previous->wm_lp[2] &= ~WM1_LP_SR_EN;
2591 I915_WRITE(WM3_LP_ILK, previous->wm_lp[2]);
2592 changed = true;
2593 }
2594 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] & WM1_LP_SR_EN) {
2595 previous->wm_lp[1] &= ~WM1_LP_SR_EN;
2596 I915_WRITE(WM2_LP_ILK, previous->wm_lp[1]);
2597 changed = true;
2598 }
2599 if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] & WM1_LP_SR_EN) {
2600 previous->wm_lp[0] &= ~WM1_LP_SR_EN;
2601 I915_WRITE(WM1_LP_ILK, previous->wm_lp[0]);
2602 changed = true;
2603 }
2604
2605 /*
2606 * Don't touch WM1S_LP_EN here.
2607 * Doing so could cause underruns.
2608 */
2609
2610 return changed;
2611}
2612
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002613/*
2614 * The spec says we shouldn't write when we don't need, because every write
2615 * causes WMs to be re-evaluated, expending some power.
2616 */
Imre Deak820c1982013-12-17 14:46:36 +02002617static void ilk_write_wm_values(struct drm_i915_private *dev_priv,
2618 struct ilk_wm_values *results)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002619{
Ville Syrjäläac9545f2013-12-05 15:51:28 +02002620 struct drm_device *dev = dev_priv->dev;
Imre Deak820c1982013-12-17 14:46:36 +02002621 struct ilk_wm_values *previous = &dev_priv->wm.hw;
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002622 unsigned int dirty;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002623 uint32_t val;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002624
Ville Syrjälä8553c182013-12-05 15:51:39 +02002625 dirty = ilk_compute_wm_dirty(dev, previous, results);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002626 if (!dirty)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002627 return;
2628
Ville Syrjälä8553c182013-12-05 15:51:39 +02002629 _ilk_disable_lp_wm(dev_priv, dirty);
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02002630
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002631 if (dirty & WM_DIRTY_PIPE(PIPE_A))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002632 I915_WRITE(WM0_PIPEA_ILK, results->wm_pipe[0]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002633 if (dirty & WM_DIRTY_PIPE(PIPE_B))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002634 I915_WRITE(WM0_PIPEB_ILK, results->wm_pipe[1]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002635 if (dirty & WM_DIRTY_PIPE(PIPE_C))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002636 I915_WRITE(WM0_PIPEC_IVB, results->wm_pipe[2]);
2637
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002638 if (dirty & WM_DIRTY_LINETIME(PIPE_A))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002639 I915_WRITE(PIPE_WM_LINETIME(PIPE_A), results->wm_linetime[0]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002640 if (dirty & WM_DIRTY_LINETIME(PIPE_B))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002641 I915_WRITE(PIPE_WM_LINETIME(PIPE_B), results->wm_linetime[1]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002642 if (dirty & WM_DIRTY_LINETIME(PIPE_C))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002643 I915_WRITE(PIPE_WM_LINETIME(PIPE_C), results->wm_linetime[2]);
2644
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002645 if (dirty & WM_DIRTY_DDB) {
Ville Syrjäläa42a5712014-01-07 16:14:08 +02002646 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Ville Syrjäläac9545f2013-12-05 15:51:28 +02002647 val = I915_READ(WM_MISC);
2648 if (results->partitioning == INTEL_DDB_PART_1_2)
2649 val &= ~WM_MISC_DATA_PARTITION_5_6;
2650 else
2651 val |= WM_MISC_DATA_PARTITION_5_6;
2652 I915_WRITE(WM_MISC, val);
2653 } else {
2654 val = I915_READ(DISP_ARB_CTL2);
2655 if (results->partitioning == INTEL_DDB_PART_1_2)
2656 val &= ~DISP_DATA_PARTITION_5_6;
2657 else
2658 val |= DISP_DATA_PARTITION_5_6;
2659 I915_WRITE(DISP_ARB_CTL2, val);
2660 }
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002661 }
2662
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002663 if (dirty & WM_DIRTY_FBC) {
Paulo Zanonicca32e92013-05-31 11:45:06 -03002664 val = I915_READ(DISP_ARB_CTL);
2665 if (results->enable_fbc_wm)
2666 val &= ~DISP_FBC_WM_DIS;
2667 else
2668 val |= DISP_FBC_WM_DIS;
2669 I915_WRITE(DISP_ARB_CTL, val);
2670 }
2671
Imre Deak954911e2013-12-17 14:46:34 +02002672 if (dirty & WM_DIRTY_LP(1) &&
2673 previous->wm_lp_spr[0] != results->wm_lp_spr[0])
2674 I915_WRITE(WM1S_LP_ILK, results->wm_lp_spr[0]);
2675
2676 if (INTEL_INFO(dev)->gen >= 7) {
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02002677 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp_spr[1] != results->wm_lp_spr[1])
2678 I915_WRITE(WM2S_LP_IVB, results->wm_lp_spr[1]);
2679 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp_spr[2] != results->wm_lp_spr[2])
2680 I915_WRITE(WM3S_LP_IVB, results->wm_lp_spr[2]);
2681 }
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002682
Ville Syrjäläfacd6192013-12-05 15:51:33 +02002683 if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] != results->wm_lp[0])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002684 I915_WRITE(WM1_LP_ILK, results->wm_lp[0]);
Ville Syrjäläfacd6192013-12-05 15:51:33 +02002685 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] != results->wm_lp[1])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002686 I915_WRITE(WM2_LP_ILK, results->wm_lp[1]);
Ville Syrjäläfacd6192013-12-05 15:51:33 +02002687 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] != results->wm_lp[2])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002688 I915_WRITE(WM3_LP_ILK, results->wm_lp[2]);
Ville Syrjälä609cede2013-10-09 19:18:03 +03002689
2690 dev_priv->wm.hw = *results;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002691}
2692
Ville Syrjälä8553c182013-12-05 15:51:39 +02002693static bool ilk_disable_lp_wm(struct drm_device *dev)
2694{
2695 struct drm_i915_private *dev_priv = dev->dev_private;
2696
2697 return _ilk_disable_lp_wm(dev_priv, WM_DIRTY_LP_ALL);
2698}
2699
Imre Deak820c1982013-12-17 14:46:36 +02002700static void ilk_update_wm(struct drm_crtc *crtc)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002701{
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03002702 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjälä46ba6142013-09-10 11:40:40 +03002703 struct drm_device *dev = crtc->dev;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002704 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak820c1982013-12-17 14:46:36 +02002705 struct ilk_wm_maximums max;
2706 struct ilk_pipe_wm_parameters params = {};
2707 struct ilk_wm_values results = {};
Ville Syrjälä77c122b2013-08-06 22:24:04 +03002708 enum intel_ddb_partitioning partitioning;
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03002709 struct intel_pipe_wm pipe_wm = {};
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002710 struct intel_pipe_wm lp_wm_1_2 = {}, lp_wm_5_6 = {}, *best_lp_wm;
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03002711 struct intel_wm_config config = {};
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002712
Ville Syrjälä2a44b762014-03-07 18:32:09 +02002713 ilk_compute_wm_parameters(crtc, &params);
Paulo Zanoni861f3382013-05-31 10:19:21 -03002714
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03002715 intel_compute_pipe_wm(crtc, &params, &pipe_wm);
2716
2717 if (!memcmp(&intel_crtc->wm.active, &pipe_wm, sizeof(pipe_wm)))
2718 return;
2719
2720 intel_crtc->wm.active = pipe_wm;
2721
Ville Syrjälä2a44b762014-03-07 18:32:09 +02002722 ilk_compute_wm_config(dev, &config);
2723
Ville Syrjälä34982fe2013-10-09 19:18:09 +03002724 ilk_compute_wm_maximums(dev, 1, &config, INTEL_DDB_PART_1_2, &max);
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02002725 ilk_wm_merge(dev, &config, &max, &lp_wm_1_2);
Ville Syrjälä0362c782013-10-09 19:17:57 +03002726
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03002727 /* 5/6 split only in single pipe config on IVB+ */
Ville Syrjäläec98c8d2013-10-11 15:26:26 +03002728 if (INTEL_INFO(dev)->gen >= 7 &&
2729 config.num_pipes_active == 1 && config.sprites_enabled) {
Ville Syrjälä34982fe2013-10-09 19:18:09 +03002730 ilk_compute_wm_maximums(dev, 1, &config, INTEL_DDB_PART_5_6, &max);
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02002731 ilk_wm_merge(dev, &config, &max, &lp_wm_5_6);
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03002732
Imre Deak820c1982013-12-17 14:46:36 +02002733 best_lp_wm = ilk_find_best_result(dev, &lp_wm_1_2, &lp_wm_5_6);
Paulo Zanoni861f3382013-05-31 10:19:21 -03002734 } else {
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002735 best_lp_wm = &lp_wm_1_2;
Paulo Zanoni861f3382013-05-31 10:19:21 -03002736 }
2737
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002738 partitioning = (best_lp_wm == &lp_wm_1_2) ?
Ville Syrjälä77c122b2013-08-06 22:24:04 +03002739 INTEL_DDB_PART_1_2 : INTEL_DDB_PART_5_6;
Paulo Zanoni861f3382013-05-31 10:19:21 -03002740
Imre Deak820c1982013-12-17 14:46:36 +02002741 ilk_compute_wm_results(dev, best_lp_wm, partitioning, &results);
Ville Syrjälä609cede2013-10-09 19:18:03 +03002742
Imre Deak820c1982013-12-17 14:46:36 +02002743 ilk_write_wm_values(dev_priv, &results);
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002744}
2745
Imre Deak820c1982013-12-17 14:46:36 +02002746static void ilk_update_sprite_wm(struct drm_plane *plane,
Ville Syrjäläadf3d352013-08-06 22:24:11 +03002747 struct drm_crtc *crtc,
Paulo Zanoni526682e2013-05-24 11:59:18 -03002748 uint32_t sprite_width, int pixel_size,
Ville Syrjäläbdd57d02013-07-05 11:57:13 +03002749 bool enabled, bool scaled)
Paulo Zanoni526682e2013-05-24 11:59:18 -03002750{
Ville Syrjälä8553c182013-12-05 15:51:39 +02002751 struct drm_device *dev = plane->dev;
Ville Syrjäläadf3d352013-08-06 22:24:11 +03002752 struct intel_plane *intel_plane = to_intel_plane(plane);
Paulo Zanoni526682e2013-05-24 11:59:18 -03002753
Ville Syrjäläadf3d352013-08-06 22:24:11 +03002754 intel_plane->wm.enabled = enabled;
2755 intel_plane->wm.scaled = scaled;
2756 intel_plane->wm.horiz_pixels = sprite_width;
2757 intel_plane->wm.bytes_per_pixel = pixel_size;
Paulo Zanoni526682e2013-05-24 11:59:18 -03002758
Ville Syrjälä8553c182013-12-05 15:51:39 +02002759 /*
2760 * IVB workaround: must disable low power watermarks for at least
2761 * one frame before enabling scaling. LP watermarks can be re-enabled
2762 * when scaling is disabled.
2763 *
2764 * WaCxSRDisabledForSpriteScaling:ivb
2765 */
2766 if (IS_IVYBRIDGE(dev) && scaled && ilk_disable_lp_wm(dev))
2767 intel_wait_for_vblank(dev, intel_plane->pipe);
2768
Imre Deak820c1982013-12-17 14:46:36 +02002769 ilk_update_wm(crtc);
Paulo Zanoni526682e2013-05-24 11:59:18 -03002770}
2771
Ville Syrjälä243e6a42013-10-14 14:55:24 +03002772static void ilk_pipe_wm_get_hw_state(struct drm_crtc *crtc)
2773{
2774 struct drm_device *dev = crtc->dev;
2775 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak820c1982013-12-17 14:46:36 +02002776 struct ilk_wm_values *hw = &dev_priv->wm.hw;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03002777 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2778 struct intel_pipe_wm *active = &intel_crtc->wm.active;
2779 enum pipe pipe = intel_crtc->pipe;
2780 static const unsigned int wm0_pipe_reg[] = {
2781 [PIPE_A] = WM0_PIPEA_ILK,
2782 [PIPE_B] = WM0_PIPEB_ILK,
2783 [PIPE_C] = WM0_PIPEC_IVB,
2784 };
2785
2786 hw->wm_pipe[pipe] = I915_READ(wm0_pipe_reg[pipe]);
Ville Syrjäläa42a5712014-01-07 16:14:08 +02002787 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Ville Syrjäläce0e0712013-12-05 15:51:36 +02002788 hw->wm_linetime[pipe] = I915_READ(PIPE_WM_LINETIME(pipe));
Ville Syrjälä243e6a42013-10-14 14:55:24 +03002789
Ville Syrjälä2a44b762014-03-07 18:32:09 +02002790 active->pipe_enabled = intel_crtc_active(crtc);
2791
2792 if (active->pipe_enabled) {
Ville Syrjälä243e6a42013-10-14 14:55:24 +03002793 u32 tmp = hw->wm_pipe[pipe];
2794
2795 /*
2796 * For active pipes LP0 watermark is marked as
2797 * enabled, and LP1+ watermaks as disabled since
2798 * we can't really reverse compute them in case
2799 * multiple pipes are active.
2800 */
2801 active->wm[0].enable = true;
2802 active->wm[0].pri_val = (tmp & WM0_PIPE_PLANE_MASK) >> WM0_PIPE_PLANE_SHIFT;
2803 active->wm[0].spr_val = (tmp & WM0_PIPE_SPRITE_MASK) >> WM0_PIPE_SPRITE_SHIFT;
2804 active->wm[0].cur_val = tmp & WM0_PIPE_CURSOR_MASK;
2805 active->linetime = hw->wm_linetime[pipe];
2806 } else {
2807 int level, max_level = ilk_wm_max_level(dev);
2808
2809 /*
2810 * For inactive pipes, all watermark levels
2811 * should be marked as enabled but zeroed,
2812 * which is what we'd compute them to.
2813 */
2814 for (level = 0; level <= max_level; level++)
2815 active->wm[level].enable = true;
2816 }
2817}
2818
2819void ilk_wm_get_hw_state(struct drm_device *dev)
2820{
2821 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak820c1982013-12-17 14:46:36 +02002822 struct ilk_wm_values *hw = &dev_priv->wm.hw;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03002823 struct drm_crtc *crtc;
2824
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01002825 for_each_crtc(dev, crtc)
Ville Syrjälä243e6a42013-10-14 14:55:24 +03002826 ilk_pipe_wm_get_hw_state(crtc);
2827
2828 hw->wm_lp[0] = I915_READ(WM1_LP_ILK);
2829 hw->wm_lp[1] = I915_READ(WM2_LP_ILK);
2830 hw->wm_lp[2] = I915_READ(WM3_LP_ILK);
2831
2832 hw->wm_lp_spr[0] = I915_READ(WM1S_LP_ILK);
Ville Syrjäläcfa76982014-03-07 18:32:08 +02002833 if (INTEL_INFO(dev)->gen >= 7) {
2834 hw->wm_lp_spr[1] = I915_READ(WM2S_LP_IVB);
2835 hw->wm_lp_spr[2] = I915_READ(WM3S_LP_IVB);
2836 }
Ville Syrjälä243e6a42013-10-14 14:55:24 +03002837
Ville Syrjäläa42a5712014-01-07 16:14:08 +02002838 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Ville Syrjäläac9545f2013-12-05 15:51:28 +02002839 hw->partitioning = (I915_READ(WM_MISC) & WM_MISC_DATA_PARTITION_5_6) ?
2840 INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
2841 else if (IS_IVYBRIDGE(dev))
2842 hw->partitioning = (I915_READ(DISP_ARB_CTL2) & DISP_DATA_PARTITION_5_6) ?
2843 INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03002844
2845 hw->enable_fbc_wm =
2846 !(I915_READ(DISP_ARB_CTL) & DISP_FBC_WM_DIS);
2847}
2848
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002849/**
2850 * intel_update_watermarks - update FIFO watermark values based on current modes
2851 *
2852 * Calculate watermark values for the various WM regs based on current mode
2853 * and plane configuration.
2854 *
2855 * There are several cases to deal with here:
2856 * - normal (i.e. non-self-refresh)
2857 * - self-refresh (SR) mode
2858 * - lines are large relative to FIFO size (buffer can hold up to 2)
2859 * - lines are small relative to FIFO size (buffer can hold more than 2
2860 * lines), so need to account for TLB latency
2861 *
2862 * The normal calculation is:
2863 * watermark = dotclock * bytes per pixel * latency
2864 * where latency is platform & configuration dependent (we assume pessimal
2865 * values here).
2866 *
2867 * The SR calculation is:
2868 * watermark = (trunc(latency/line time)+1) * surface width *
2869 * bytes per pixel
2870 * where
2871 * line time = htotal / dotclock
2872 * surface width = hdisplay for normal plane and 64 for cursor
2873 * and latency is assumed to be high, as above.
2874 *
2875 * The final value programmed to the register should always be rounded up,
2876 * and include an extra 2 entries to account for clock crossings.
2877 *
2878 * We don't use the sprite, so we can ignore that. And on Crestline we have
2879 * to set the non-SR watermarks to 8.
2880 */
Ville Syrjälä46ba6142013-09-10 11:40:40 +03002881void intel_update_watermarks(struct drm_crtc *crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002882{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03002883 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002884
2885 if (dev_priv->display.update_wm)
Ville Syrjälä46ba6142013-09-10 11:40:40 +03002886 dev_priv->display.update_wm(crtc);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002887}
2888
Ville Syrjäläadf3d352013-08-06 22:24:11 +03002889void intel_update_sprite_watermarks(struct drm_plane *plane,
2890 struct drm_crtc *crtc,
Paulo Zanoni4c4ff432013-05-24 11:59:17 -03002891 uint32_t sprite_width, int pixel_size,
Ville Syrjälä39db4a42013-08-06 22:24:00 +03002892 bool enabled, bool scaled)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002893{
Ville Syrjäläadf3d352013-08-06 22:24:11 +03002894 struct drm_i915_private *dev_priv = plane->dev->dev_private;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002895
2896 if (dev_priv->display.update_sprite_wm)
Ville Syrjäläadf3d352013-08-06 22:24:11 +03002897 dev_priv->display.update_sprite_wm(plane, crtc, sprite_width,
Ville Syrjälä39db4a42013-08-06 22:24:00 +03002898 pixel_size, enabled, scaled);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002899}
2900
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002901static struct drm_i915_gem_object *
2902intel_alloc_context_page(struct drm_device *dev)
2903{
2904 struct drm_i915_gem_object *ctx;
2905 int ret;
2906
2907 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2908
2909 ctx = i915_gem_alloc_object(dev, 4096);
2910 if (!ctx) {
2911 DRM_DEBUG("failed to alloc power context, RC6 disabled\n");
2912 return NULL;
2913 }
2914
Daniel Vetterc69766f2014-02-14 14:01:17 +01002915 ret = i915_gem_obj_ggtt_pin(ctx, 4096, 0);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002916 if (ret) {
2917 DRM_ERROR("failed to pin power context: %d\n", ret);
2918 goto err_unref;
2919 }
2920
2921 ret = i915_gem_object_set_to_gtt_domain(ctx, 1);
2922 if (ret) {
2923 DRM_ERROR("failed to set-domain on power context: %d\n", ret);
2924 goto err_unpin;
2925 }
2926
2927 return ctx;
2928
2929err_unpin:
Ben Widawskyd7f46fc2013-12-06 14:10:55 -08002930 i915_gem_object_ggtt_unpin(ctx);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002931err_unref:
2932 drm_gem_object_unreference(&ctx->base);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002933 return NULL;
2934}
2935
Daniel Vetter92703882012-08-09 16:46:01 +02002936/**
2937 * Lock protecting IPS related data structures
Daniel Vetter92703882012-08-09 16:46:01 +02002938 */
2939DEFINE_SPINLOCK(mchdev_lock);
2940
2941/* Global for IPS driver to get at the current i915 device. Protected by
2942 * mchdev_lock. */
2943static struct drm_i915_private *i915_mch_dev;
2944
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002945bool ironlake_set_drps(struct drm_device *dev, u8 val)
2946{
2947 struct drm_i915_private *dev_priv = dev->dev_private;
2948 u16 rgvswctl;
2949
Daniel Vetter92703882012-08-09 16:46:01 +02002950 assert_spin_locked(&mchdev_lock);
2951
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002952 rgvswctl = I915_READ16(MEMSWCTL);
2953 if (rgvswctl & MEMCTL_CMD_STS) {
2954 DRM_DEBUG("gpu busy, RCS change rejected\n");
2955 return false; /* still busy with another command */
2956 }
2957
2958 rgvswctl = (MEMCTL_CMD_CHFREQ << MEMCTL_CMD_SHIFT) |
2959 (val << MEMCTL_FREQ_SHIFT) | MEMCTL_SFCAVM;
2960 I915_WRITE16(MEMSWCTL, rgvswctl);
2961 POSTING_READ16(MEMSWCTL);
2962
2963 rgvswctl |= MEMCTL_CMD_STS;
2964 I915_WRITE16(MEMSWCTL, rgvswctl);
2965
2966 return true;
2967}
2968
Daniel Vetter8090c6b2012-06-24 16:42:32 +02002969static void ironlake_enable_drps(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002970{
2971 struct drm_i915_private *dev_priv = dev->dev_private;
2972 u32 rgvmodectl = I915_READ(MEMMODECTL);
2973 u8 fmax, fmin, fstart, vstart;
2974
Daniel Vetter92703882012-08-09 16:46:01 +02002975 spin_lock_irq(&mchdev_lock);
2976
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002977 /* Enable temp reporting */
2978 I915_WRITE16(PMMISC, I915_READ(PMMISC) | MCPPCE_EN);
2979 I915_WRITE16(TSC1, I915_READ(TSC1) | TSE);
2980
2981 /* 100ms RC evaluation intervals */
2982 I915_WRITE(RCUPEI, 100000);
2983 I915_WRITE(RCDNEI, 100000);
2984
2985 /* Set max/min thresholds to 90ms and 80ms respectively */
2986 I915_WRITE(RCBMAXAVG, 90000);
2987 I915_WRITE(RCBMINAVG, 80000);
2988
2989 I915_WRITE(MEMIHYST, 1);
2990
2991 /* Set up min, max, and cur for interrupt handling */
2992 fmax = (rgvmodectl & MEMMODE_FMAX_MASK) >> MEMMODE_FMAX_SHIFT;
2993 fmin = (rgvmodectl & MEMMODE_FMIN_MASK);
2994 fstart = (rgvmodectl & MEMMODE_FSTART_MASK) >>
2995 MEMMODE_FSTART_SHIFT;
2996
2997 vstart = (I915_READ(PXVFREQ_BASE + (fstart * 4)) & PXVFREQ_PX_MASK) >>
2998 PXVFREQ_PX_SHIFT;
2999
Daniel Vetter20e4d402012-08-08 23:35:39 +02003000 dev_priv->ips.fmax = fmax; /* IPS callback will increase this */
3001 dev_priv->ips.fstart = fstart;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003002
Daniel Vetter20e4d402012-08-08 23:35:39 +02003003 dev_priv->ips.max_delay = fstart;
3004 dev_priv->ips.min_delay = fmin;
3005 dev_priv->ips.cur_delay = fstart;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003006
3007 DRM_DEBUG_DRIVER("fmax: %d, fmin: %d, fstart: %d\n",
3008 fmax, fmin, fstart);
3009
3010 I915_WRITE(MEMINTREN, MEMINT_CX_SUPR_EN | MEMINT_EVAL_CHG_EN);
3011
3012 /*
3013 * Interrupts will be enabled in ironlake_irq_postinstall
3014 */
3015
3016 I915_WRITE(VIDSTART, vstart);
3017 POSTING_READ(VIDSTART);
3018
3019 rgvmodectl |= MEMMODE_SWMODE_EN;
3020 I915_WRITE(MEMMODECTL, rgvmodectl);
3021
Daniel Vetter92703882012-08-09 16:46:01 +02003022 if (wait_for_atomic((I915_READ(MEMSWCTL) & MEMCTL_CMD_STS) == 0, 10))
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003023 DRM_ERROR("stuck trying to change perf mode\n");
Daniel Vetter92703882012-08-09 16:46:01 +02003024 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003025
3026 ironlake_set_drps(dev, fstart);
3027
Daniel Vetter20e4d402012-08-08 23:35:39 +02003028 dev_priv->ips.last_count1 = I915_READ(0x112e4) + I915_READ(0x112e8) +
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003029 I915_READ(0x112e0);
Daniel Vetter20e4d402012-08-08 23:35:39 +02003030 dev_priv->ips.last_time1 = jiffies_to_msecs(jiffies);
3031 dev_priv->ips.last_count2 = I915_READ(0x112f4);
3032 getrawmonotonic(&dev_priv->ips.last_time2);
Daniel Vetter92703882012-08-09 16:46:01 +02003033
3034 spin_unlock_irq(&mchdev_lock);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003035}
3036
Daniel Vetter8090c6b2012-06-24 16:42:32 +02003037static void ironlake_disable_drps(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003038{
3039 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter92703882012-08-09 16:46:01 +02003040 u16 rgvswctl;
3041
3042 spin_lock_irq(&mchdev_lock);
3043
3044 rgvswctl = I915_READ16(MEMSWCTL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003045
3046 /* Ack interrupts, disable EFC interrupt */
3047 I915_WRITE(MEMINTREN, I915_READ(MEMINTREN) & ~MEMINT_EVAL_CHG_EN);
3048 I915_WRITE(MEMINTRSTS, MEMINT_EVAL_CHG);
3049 I915_WRITE(DEIER, I915_READ(DEIER) & ~DE_PCU_EVENT);
3050 I915_WRITE(DEIIR, DE_PCU_EVENT);
3051 I915_WRITE(DEIMR, I915_READ(DEIMR) | DE_PCU_EVENT);
3052
3053 /* Go back to the starting frequency */
Daniel Vetter20e4d402012-08-08 23:35:39 +02003054 ironlake_set_drps(dev, dev_priv->ips.fstart);
Daniel Vetter92703882012-08-09 16:46:01 +02003055 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003056 rgvswctl |= MEMCTL_CMD_STS;
3057 I915_WRITE(MEMSWCTL, rgvswctl);
Daniel Vetter92703882012-08-09 16:46:01 +02003058 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003059
Daniel Vetter92703882012-08-09 16:46:01 +02003060 spin_unlock_irq(&mchdev_lock);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003061}
3062
Daniel Vetteracbe9472012-07-26 11:50:05 +02003063/* There's a funny hw issue where the hw returns all 0 when reading from
3064 * GEN6_RP_INTERRUPT_LIMITS. Hence we always need to compute the desired value
3065 * ourselves, instead of doing a rmw cycle (which might result in us clearing
3066 * all limits and the gpu stuck at whatever frequency it is at atm).
3067 */
Chris Wilson6917c7b2013-11-06 13:56:26 -02003068static u32 gen6_rps_limits(struct drm_i915_private *dev_priv, u8 val)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003069{
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01003070 u32 limits;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003071
Daniel Vetter20b46e52012-07-26 11:16:14 +02003072 /* Only set the down limit when we've reached the lowest level to avoid
3073 * getting more interrupts, otherwise leave this clear. This prevents a
3074 * race in the hw when coming out of rc6: There's a tiny window where
3075 * the hw runs at the minimal clock before selecting the desired
3076 * frequency, if the down threshold expires in that window we will not
3077 * receive a down interrupt. */
Ben Widawskyb39fb292014-03-19 18:31:11 -07003078 limits = dev_priv->rps.max_freq_softlimit << 24;
3079 if (val <= dev_priv->rps.min_freq_softlimit)
3080 limits |= dev_priv->rps.min_freq_softlimit << 16;
Daniel Vetter20b46e52012-07-26 11:16:14 +02003081
3082 return limits;
3083}
3084
Chris Wilsondd75fdc2013-09-25 17:34:57 +01003085static void gen6_set_rps_thresholds(struct drm_i915_private *dev_priv, u8 val)
3086{
3087 int new_power;
3088
3089 new_power = dev_priv->rps.power;
3090 switch (dev_priv->rps.power) {
3091 case LOW_POWER:
Ben Widawskyb39fb292014-03-19 18:31:11 -07003092 if (val > dev_priv->rps.efficient_freq + 1 && val > dev_priv->rps.cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01003093 new_power = BETWEEN;
3094 break;
3095
3096 case BETWEEN:
Ben Widawskyb39fb292014-03-19 18:31:11 -07003097 if (val <= dev_priv->rps.efficient_freq && val < dev_priv->rps.cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01003098 new_power = LOW_POWER;
Ben Widawskyb39fb292014-03-19 18:31:11 -07003099 else if (val >= dev_priv->rps.rp0_freq && val > dev_priv->rps.cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01003100 new_power = HIGH_POWER;
3101 break;
3102
3103 case HIGH_POWER:
Ben Widawskyb39fb292014-03-19 18:31:11 -07003104 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 +01003105 new_power = BETWEEN;
3106 break;
3107 }
3108 /* Max/min bins are special */
Ben Widawskyb39fb292014-03-19 18:31:11 -07003109 if (val == dev_priv->rps.min_freq_softlimit)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01003110 new_power = LOW_POWER;
Ben Widawskyb39fb292014-03-19 18:31:11 -07003111 if (val == dev_priv->rps.max_freq_softlimit)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01003112 new_power = HIGH_POWER;
3113 if (new_power == dev_priv->rps.power)
3114 return;
3115
3116 /* Note the units here are not exactly 1us, but 1280ns. */
3117 switch (new_power) {
3118 case LOW_POWER:
3119 /* Upclock if more than 95% busy over 16ms */
3120 I915_WRITE(GEN6_RP_UP_EI, 12500);
3121 I915_WRITE(GEN6_RP_UP_THRESHOLD, 11800);
3122
3123 /* Downclock if less than 85% busy over 32ms */
3124 I915_WRITE(GEN6_RP_DOWN_EI, 25000);
3125 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 21250);
3126
3127 I915_WRITE(GEN6_RP_CONTROL,
3128 GEN6_RP_MEDIA_TURBO |
3129 GEN6_RP_MEDIA_HW_NORMAL_MODE |
3130 GEN6_RP_MEDIA_IS_GFX |
3131 GEN6_RP_ENABLE |
3132 GEN6_RP_UP_BUSY_AVG |
3133 GEN6_RP_DOWN_IDLE_AVG);
3134 break;
3135
3136 case BETWEEN:
3137 /* Upclock if more than 90% busy over 13ms */
3138 I915_WRITE(GEN6_RP_UP_EI, 10250);
3139 I915_WRITE(GEN6_RP_UP_THRESHOLD, 9225);
3140
3141 /* Downclock if less than 75% busy over 32ms */
3142 I915_WRITE(GEN6_RP_DOWN_EI, 25000);
3143 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 18750);
3144
3145 I915_WRITE(GEN6_RP_CONTROL,
3146 GEN6_RP_MEDIA_TURBO |
3147 GEN6_RP_MEDIA_HW_NORMAL_MODE |
3148 GEN6_RP_MEDIA_IS_GFX |
3149 GEN6_RP_ENABLE |
3150 GEN6_RP_UP_BUSY_AVG |
3151 GEN6_RP_DOWN_IDLE_AVG);
3152 break;
3153
3154 case HIGH_POWER:
3155 /* Upclock if more than 85% busy over 10ms */
3156 I915_WRITE(GEN6_RP_UP_EI, 8000);
3157 I915_WRITE(GEN6_RP_UP_THRESHOLD, 6800);
3158
3159 /* Downclock if less than 60% busy over 32ms */
3160 I915_WRITE(GEN6_RP_DOWN_EI, 25000);
3161 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 15000);
3162
3163 I915_WRITE(GEN6_RP_CONTROL,
3164 GEN6_RP_MEDIA_TURBO |
3165 GEN6_RP_MEDIA_HW_NORMAL_MODE |
3166 GEN6_RP_MEDIA_IS_GFX |
3167 GEN6_RP_ENABLE |
3168 GEN6_RP_UP_BUSY_AVG |
3169 GEN6_RP_DOWN_IDLE_AVG);
3170 break;
3171 }
3172
3173 dev_priv->rps.power = new_power;
3174 dev_priv->rps.last_adj = 0;
3175}
3176
Chris Wilson2876ce72014-03-28 08:03:34 +00003177static u32 gen6_rps_pm_mask(struct drm_i915_private *dev_priv, u8 val)
3178{
3179 u32 mask = 0;
3180
3181 if (val > dev_priv->rps.min_freq_softlimit)
3182 mask |= GEN6_PM_RP_DOWN_THRESHOLD | GEN6_PM_RP_DOWN_TIMEOUT;
3183 if (val < dev_priv->rps.max_freq_softlimit)
3184 mask |= GEN6_PM_RP_UP_THRESHOLD;
3185
Chris Wilson7b3c29f2014-07-10 20:31:19 +01003186 mask |= dev_priv->pm_rps_events & (GEN6_PM_RP_DOWN_EI_EXPIRED | GEN6_PM_RP_UP_EI_EXPIRED);
3187 mask &= dev_priv->pm_rps_events;
3188
Chris Wilson2876ce72014-03-28 08:03:34 +00003189 /* IVB and SNB hard hangs on looping batchbuffer
3190 * if GEN6_PM_UP_EI_EXPIRED is masked.
3191 */
3192 if (INTEL_INFO(dev_priv->dev)->gen <= 7 && !IS_HASWELL(dev_priv->dev))
3193 mask |= GEN6_PM_RP_UP_EI_EXPIRED;
3194
Deepak Sbaccd452014-05-15 20:58:09 +03003195 if (IS_GEN8(dev_priv->dev))
3196 mask |= GEN8_PMINTR_REDIRECT_TO_NON_DISP;
3197
Chris Wilson2876ce72014-03-28 08:03:34 +00003198 return ~mask;
3199}
3200
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06003201/* gen6_set_rps is called to update the frequency request, but should also be
3202 * called when the range (min_delay and max_delay) is modified so that we can
3203 * update the GEN6_RP_INTERRUPT_LIMITS register accordingly. */
Daniel Vetter20b46e52012-07-26 11:16:14 +02003204void gen6_set_rps(struct drm_device *dev, u8 val)
3205{
3206 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01003207
Jesse Barnes4fc688c2012-11-02 11:14:01 -07003208 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Ben Widawskyb39fb292014-03-19 18:31:11 -07003209 WARN_ON(val > dev_priv->rps.max_freq_softlimit);
3210 WARN_ON(val < dev_priv->rps.min_freq_softlimit);
Daniel Vetter004777c2012-08-09 15:07:01 +02003211
Chris Wilsoneb64cad2014-03-27 08:24:20 +00003212 /* min/max delay may still have been modified so be sure to
3213 * write the limits value.
3214 */
3215 if (val != dev_priv->rps.cur_freq) {
3216 gen6_set_rps_thresholds(dev_priv, val);
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06003217
Ben Widawsky50e6a2a2014-03-31 17:16:43 -07003218 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Chris Wilsoneb64cad2014-03-27 08:24:20 +00003219 I915_WRITE(GEN6_RPNSWREQ,
3220 HSW_FREQUENCY(val));
3221 else
3222 I915_WRITE(GEN6_RPNSWREQ,
3223 GEN6_FREQUENCY(val) |
3224 GEN6_OFFSET(0) |
3225 GEN6_AGGRESSIVE_TURBO);
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06003226 }
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01003227
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01003228 /* Make sure we continue to get interrupts
3229 * until we hit the minimum or maximum frequencies.
3230 */
Chris Wilsoneb64cad2014-03-27 08:24:20 +00003231 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS, gen6_rps_limits(dev_priv, val));
Chris Wilson2876ce72014-03-28 08:03:34 +00003232 I915_WRITE(GEN6_PMINTRMSK, gen6_rps_pm_mask(dev_priv, val));
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01003233
Ben Widawskyd5570a72012-09-07 19:43:41 -07003234 POSTING_READ(GEN6_RPNSWREQ);
3235
Ben Widawskyb39fb292014-03-19 18:31:11 -07003236 dev_priv->rps.cur_freq = val;
Daniel Vetterbe2cde92012-08-30 13:26:48 +02003237 trace_intel_gpu_freq_change(val * 50);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003238}
3239
Deepak S76c3552f2014-01-30 23:08:16 +05303240/* vlv_set_rps_idle: Set the frequency to Rpn if Gfx clocks are down
3241 *
3242 * * If Gfx is Idle, then
3243 * 1. Mask Turbo interrupts
3244 * 2. Bring up Gfx clock
3245 * 3. Change the freq to Rpn and wait till P-Unit updates freq
3246 * 4. Clear the Force GFX CLK ON bit so that Gfx can down
3247 * 5. Unmask Turbo interrupts
3248*/
3249static void vlv_set_rps_idle(struct drm_i915_private *dev_priv)
3250{
Deepak S5549d252014-06-28 11:26:11 +05303251 struct drm_device *dev = dev_priv->dev;
3252
3253 /* Latest VLV doesn't need to force the gfx clock */
3254 if (dev->pdev->revision >= 0xd) {
3255 valleyview_set_rps(dev_priv->dev, dev_priv->rps.min_freq_softlimit);
3256 return;
3257 }
3258
Deepak S76c3552f2014-01-30 23:08:16 +05303259 /*
3260 * When we are idle. Drop to min voltage state.
3261 */
3262
Ben Widawskyb39fb292014-03-19 18:31:11 -07003263 if (dev_priv->rps.cur_freq <= dev_priv->rps.min_freq_softlimit)
Deepak S76c3552f2014-01-30 23:08:16 +05303264 return;
3265
3266 /* Mask turbo interrupt so that they will not come in between */
3267 I915_WRITE(GEN6_PMINTRMSK, 0xffffffff);
3268
Imre Deak650ad972014-04-18 16:35:02 +03003269 vlv_force_gfx_clock(dev_priv, true);
Deepak S76c3552f2014-01-30 23:08:16 +05303270
Ben Widawskyb39fb292014-03-19 18:31:11 -07003271 dev_priv->rps.cur_freq = dev_priv->rps.min_freq_softlimit;
Deepak S76c3552f2014-01-30 23:08:16 +05303272
3273 vlv_punit_write(dev_priv, PUNIT_REG_GPU_FREQ_REQ,
Ben Widawskyb39fb292014-03-19 18:31:11 -07003274 dev_priv->rps.min_freq_softlimit);
Deepak S76c3552f2014-01-30 23:08:16 +05303275
3276 if (wait_for(((vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS))
3277 & GENFREQSTATUS) == 0, 5))
3278 DRM_ERROR("timed out waiting for Punit\n");
3279
Imre Deak650ad972014-04-18 16:35:02 +03003280 vlv_force_gfx_clock(dev_priv, false);
Deepak S76c3552f2014-01-30 23:08:16 +05303281
Chris Wilson7b3c29f2014-07-10 20:31:19 +01003282 I915_WRITE(GEN6_PMINTRMSK,
3283 gen6_rps_pm_mask(dev_priv, dev_priv->rps.cur_freq));
Deepak S76c3552f2014-01-30 23:08:16 +05303284}
3285
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003286void gen6_rps_idle(struct drm_i915_private *dev_priv)
3287{
Damien Lespiau691bb712013-12-12 14:36:36 +00003288 struct drm_device *dev = dev_priv->dev;
3289
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003290 mutex_lock(&dev_priv->rps.hw_lock);
Chris Wilsonc0951f02013-10-10 21:58:50 +01003291 if (dev_priv->rps.enabled) {
Deepak S34638112014-06-28 11:26:26 +05303292 if (IS_CHERRYVIEW(dev))
3293 valleyview_set_rps(dev_priv->dev, dev_priv->rps.min_freq_softlimit);
3294 else if (IS_VALLEYVIEW(dev))
Deepak S76c3552f2014-01-30 23:08:16 +05303295 vlv_set_rps_idle(dev_priv);
Chris Wilsonc0951f02013-10-10 21:58:50 +01003296 else
Ben Widawskyb39fb292014-03-19 18:31:11 -07003297 gen6_set_rps(dev_priv->dev, dev_priv->rps.min_freq_softlimit);
Chris Wilsonc0951f02013-10-10 21:58:50 +01003298 dev_priv->rps.last_adj = 0;
3299 }
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003300 mutex_unlock(&dev_priv->rps.hw_lock);
3301}
3302
3303void gen6_rps_boost(struct drm_i915_private *dev_priv)
3304{
Damien Lespiau691bb712013-12-12 14:36:36 +00003305 struct drm_device *dev = dev_priv->dev;
3306
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003307 mutex_lock(&dev_priv->rps.hw_lock);
Chris Wilsonc0951f02013-10-10 21:58:50 +01003308 if (dev_priv->rps.enabled) {
Damien Lespiau691bb712013-12-12 14:36:36 +00003309 if (IS_VALLEYVIEW(dev))
Ben Widawskyb39fb292014-03-19 18:31:11 -07003310 valleyview_set_rps(dev_priv->dev, dev_priv->rps.max_freq_softlimit);
Chris Wilsonc0951f02013-10-10 21:58:50 +01003311 else
Ben Widawskyb39fb292014-03-19 18:31:11 -07003312 gen6_set_rps(dev_priv->dev, dev_priv->rps.max_freq_softlimit);
Chris Wilsonc0951f02013-10-10 21:58:50 +01003313 dev_priv->rps.last_adj = 0;
3314 }
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003315 mutex_unlock(&dev_priv->rps.hw_lock);
3316}
3317
Jesse Barnes0a073b82013-04-17 15:54:58 -07003318void valleyview_set_rps(struct drm_device *dev, u8 val)
3319{
3320 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä7a670922013-06-25 19:21:06 +03003321
Jesse Barnes0a073b82013-04-17 15:54:58 -07003322 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Ben Widawskyb39fb292014-03-19 18:31:11 -07003323 WARN_ON(val > dev_priv->rps.max_freq_softlimit);
3324 WARN_ON(val < dev_priv->rps.min_freq_softlimit);
Jesse Barnes0a073b82013-04-17 15:54:58 -07003325
Ville Syrjälä73008b92013-06-25 19:21:01 +03003326 DRM_DEBUG_DRIVER("GPU freq request from %d MHz (%u) to %d MHz (%u)\n",
Ben Widawskyb39fb292014-03-19 18:31:11 -07003327 vlv_gpu_freq(dev_priv, dev_priv->rps.cur_freq),
3328 dev_priv->rps.cur_freq,
Ville Syrjälä2ec38152013-11-05 22:42:29 +02003329 vlv_gpu_freq(dev_priv, val), val);
Jesse Barnes0a073b82013-04-17 15:54:58 -07003330
Chris Wilson2876ce72014-03-28 08:03:34 +00003331 if (val != dev_priv->rps.cur_freq)
3332 vlv_punit_write(dev_priv, PUNIT_REG_GPU_FREQ_REQ, val);
Jesse Barnes0a073b82013-04-17 15:54:58 -07003333
Imre Deak09c87db2014-04-03 20:02:42 +03003334 I915_WRITE(GEN6_PMINTRMSK, gen6_rps_pm_mask(dev_priv, val));
Jesse Barnes0a073b82013-04-17 15:54:58 -07003335
Ben Widawskyb39fb292014-03-19 18:31:11 -07003336 dev_priv->rps.cur_freq = val;
Ville Syrjälä2ec38152013-11-05 22:42:29 +02003337 trace_intel_gpu_freq_change(vlv_gpu_freq(dev_priv, val));
Jesse Barnes0a073b82013-04-17 15:54:58 -07003338}
3339
Ben Widawsky09610212014-05-15 20:58:08 +03003340static void gen8_disable_rps_interrupts(struct drm_device *dev)
3341{
3342 struct drm_i915_private *dev_priv = dev->dev_private;
3343
Mika Kuoppala992f1912014-05-16 13:44:12 +03003344 I915_WRITE(GEN6_PMINTRMSK, ~GEN8_PMINTR_REDIRECT_TO_NON_DISP);
Ben Widawsky09610212014-05-15 20:58:08 +03003345 I915_WRITE(GEN8_GT_IER(2), I915_READ(GEN8_GT_IER(2)) &
3346 ~dev_priv->pm_rps_events);
3347 /* Complete PM interrupt masking here doesn't race with the rps work
3348 * item again unmasking PM interrupts because that is using a different
3349 * register (GEN8_GT_IMR(2)) to mask PM interrupts. The only risk is in
3350 * leaving stale bits in GEN8_GT_IIR(2) and GEN8_GT_IMR(2) which
3351 * gen8_enable_rps will clean up. */
3352
3353 spin_lock_irq(&dev_priv->irq_lock);
3354 dev_priv->rps.pm_iir = 0;
3355 spin_unlock_irq(&dev_priv->irq_lock);
3356
3357 I915_WRITE(GEN8_GT_IIR(2), dev_priv->pm_rps_events);
3358}
3359
Daniel Vetter44fc7d52013-07-12 22:43:27 +02003360static void gen6_disable_rps_interrupts(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003361{
3362 struct drm_i915_private *dev_priv = dev->dev_private;
3363
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003364 I915_WRITE(GEN6_PMINTRMSK, 0xffffffff);
Deepak Sa6706b42014-03-15 20:23:22 +05303365 I915_WRITE(GEN6_PMIER, I915_READ(GEN6_PMIER) &
3366 ~dev_priv->pm_rps_events);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003367 /* Complete PM interrupt masking here doesn't race with the rps work
3368 * item again unmasking PM interrupts because that is using a different
3369 * register (PMIMR) to mask PM interrupts. The only risk is in leaving
3370 * stale bits in PMIIR and PMIMR which gen6_enable_rps will clean up. */
3371
Daniel Vetter59cdb632013-07-04 23:35:28 +02003372 spin_lock_irq(&dev_priv->irq_lock);
Daniel Vetterc6a828d2012-08-08 23:35:35 +02003373 dev_priv->rps.pm_iir = 0;
Daniel Vetter59cdb632013-07-04 23:35:28 +02003374 spin_unlock_irq(&dev_priv->irq_lock);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003375
Deepak Sa6706b42014-03-15 20:23:22 +05303376 I915_WRITE(GEN6_PMIIR, dev_priv->pm_rps_events);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003377}
3378
Daniel Vetter44fc7d52013-07-12 22:43:27 +02003379static void gen6_disable_rps(struct drm_device *dev)
3380{
3381 struct drm_i915_private *dev_priv = dev->dev_private;
3382
3383 I915_WRITE(GEN6_RC_CONTROL, 0);
3384 I915_WRITE(GEN6_RPNSWREQ, 1 << 31);
3385
Ben Widawsky09610212014-05-15 20:58:08 +03003386 if (IS_BROADWELL(dev))
3387 gen8_disable_rps_interrupts(dev);
3388 else
3389 gen6_disable_rps_interrupts(dev);
Daniel Vetter44fc7d52013-07-12 22:43:27 +02003390}
3391
Deepak S38807742014-05-23 21:00:15 +05303392static void cherryview_disable_rps(struct drm_device *dev)
3393{
3394 struct drm_i915_private *dev_priv = dev->dev_private;
3395
3396 I915_WRITE(GEN6_RC_CONTROL, 0);
Deepak S3497a562014-07-10 13:16:26 +05303397
3398 gen8_disable_rps_interrupts(dev);
Deepak S38807742014-05-23 21:00:15 +05303399}
3400
Jesse Barnesd20d4f02013-04-23 10:09:28 -07003401static void valleyview_disable_rps(struct drm_device *dev)
3402{
3403 struct drm_i915_private *dev_priv = dev->dev_private;
3404
3405 I915_WRITE(GEN6_RC_CONTROL, 0);
Jesse Barnesd20d4f02013-04-23 10:09:28 -07003406
Daniel Vetter44fc7d52013-07-12 22:43:27 +02003407 gen6_disable_rps_interrupts(dev);
Jesse Barnesd20d4f02013-04-23 10:09:28 -07003408}
3409
Ben Widawskydc39fff2013-10-18 12:32:07 -07003410static void intel_print_rc6_info(struct drm_device *dev, u32 mode)
3411{
Imre Deak91ca6892014-04-14 20:24:25 +03003412 if (IS_VALLEYVIEW(dev)) {
3413 if (mode & (GEN7_RC_CTL_TO_MODE | GEN6_RC_CTL_EI_MODE(1)))
3414 mode = GEN6_RC_CTL_RC6_ENABLE;
3415 else
3416 mode = 0;
3417 }
Ben Widawskydc39fff2013-10-18 12:32:07 -07003418 DRM_INFO("Enabling RC6 states: RC6 %s, RC6p %s, RC6pp %s\n",
Ben Widawsky1c79b422014-01-28 20:25:40 -08003419 (mode & GEN6_RC_CTL_RC6_ENABLE) ? "on" : "off",
3420 (mode & GEN6_RC_CTL_RC6p_ENABLE) ? "on" : "off",
3421 (mode & GEN6_RC_CTL_RC6pp_ENABLE) ? "on" : "off");
Ben Widawskydc39fff2013-10-18 12:32:07 -07003422}
3423
Imre Deake6069ca2014-04-18 16:01:02 +03003424static int sanitize_rc6_option(const struct drm_device *dev, int enable_rc6)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003425{
Damien Lespiaueb4926e2013-06-07 17:41:14 +01003426 /* No RC6 before Ironlake */
3427 if (INTEL_INFO(dev)->gen < 5)
3428 return 0;
3429
Imre Deake6069ca2014-04-18 16:01:02 +03003430 /* RC6 is only on Ironlake mobile not on desktop */
3431 if (INTEL_INFO(dev)->gen == 5 && !IS_IRONLAKE_M(dev))
3432 return 0;
3433
Daniel Vetter456470e2012-08-08 23:35:40 +02003434 /* Respect the kernel parameter if it is set */
Imre Deake6069ca2014-04-18 16:01:02 +03003435 if (enable_rc6 >= 0) {
3436 int mask;
3437
3438 if (INTEL_INFO(dev)->gen == 6 || IS_IVYBRIDGE(dev))
3439 mask = INTEL_RC6_ENABLE | INTEL_RC6p_ENABLE |
3440 INTEL_RC6pp_ENABLE;
3441 else
3442 mask = INTEL_RC6_ENABLE;
3443
3444 if ((enable_rc6 & mask) != enable_rc6)
3445 DRM_INFO("Adjusting RC6 mask to %d (requested %d, valid %d)\n",
Mika Kuoppala8fd9c1a92014-05-15 20:58:10 +03003446 enable_rc6 & mask, enable_rc6, mask);
Imre Deake6069ca2014-04-18 16:01:02 +03003447
3448 return enable_rc6 & mask;
3449 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003450
Chris Wilson6567d742012-11-10 10:00:06 +00003451 /* Disable RC6 on Ironlake */
3452 if (INTEL_INFO(dev)->gen == 5)
3453 return 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003454
Ben Widawsky8bade1a2014-01-28 20:25:39 -08003455 if (IS_IVYBRIDGE(dev))
Ben Widawskycca84a12014-01-28 20:25:38 -08003456 return (INTEL_RC6_ENABLE | INTEL_RC6p_ENABLE);
Ben Widawsky8bade1a2014-01-28 20:25:39 -08003457
3458 return INTEL_RC6_ENABLE;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003459}
3460
Imre Deake6069ca2014-04-18 16:01:02 +03003461int intel_enable_rc6(const struct drm_device *dev)
3462{
3463 return i915.enable_rc6;
3464}
3465
Ben Widawsky09610212014-05-15 20:58:08 +03003466static void gen8_enable_rps_interrupts(struct drm_device *dev)
3467{
3468 struct drm_i915_private *dev_priv = dev->dev_private;
3469
3470 spin_lock_irq(&dev_priv->irq_lock);
3471 WARN_ON(dev_priv->rps.pm_iir);
3472 bdw_enable_pm_irq(dev_priv, dev_priv->pm_rps_events);
3473 I915_WRITE(GEN8_GT_IIR(2), dev_priv->pm_rps_events);
3474 spin_unlock_irq(&dev_priv->irq_lock);
3475}
3476
Daniel Vetter44fc7d52013-07-12 22:43:27 +02003477static void gen6_enable_rps_interrupts(struct drm_device *dev)
3478{
3479 struct drm_i915_private *dev_priv = dev->dev_private;
3480
3481 spin_lock_irq(&dev_priv->irq_lock);
Daniel Vettera0b33352013-07-04 23:35:34 +02003482 WARN_ON(dev_priv->rps.pm_iir);
Deepak Sa6706b42014-03-15 20:23:22 +05303483 snb_enable_pm_irq(dev_priv, dev_priv->pm_rps_events);
3484 I915_WRITE(GEN6_PMIIR, dev_priv->pm_rps_events);
Daniel Vetter44fc7d52013-07-12 22:43:27 +02003485 spin_unlock_irq(&dev_priv->irq_lock);
Daniel Vetter44fc7d52013-07-12 22:43:27 +02003486}
3487
Ben Widawsky3280e8b2014-03-31 17:16:42 -07003488static void parse_rp_state_cap(struct drm_i915_private *dev_priv, u32 rp_state_cap)
3489{
3490 /* All of these values are in units of 50MHz */
3491 dev_priv->rps.cur_freq = 0;
3492 /* static values from HW: RP0 < RPe < RP1 < RPn (min_freq) */
3493 dev_priv->rps.rp1_freq = (rp_state_cap >> 8) & 0xff;
3494 dev_priv->rps.rp0_freq = (rp_state_cap >> 0) & 0xff;
3495 dev_priv->rps.min_freq = (rp_state_cap >> 16) & 0xff;
3496 /* XXX: only BYT has a special efficient freq */
3497 dev_priv->rps.efficient_freq = dev_priv->rps.rp1_freq;
3498 /* hw_max = RP0 until we check for overclocking */
3499 dev_priv->rps.max_freq = dev_priv->rps.rp0_freq;
3500
3501 /* Preserve min/max settings in case of re-init */
3502 if (dev_priv->rps.max_freq_softlimit == 0)
3503 dev_priv->rps.max_freq_softlimit = dev_priv->rps.max_freq;
3504
3505 if (dev_priv->rps.min_freq_softlimit == 0)
3506 dev_priv->rps.min_freq_softlimit = dev_priv->rps.min_freq;
3507}
3508
Ben Widawsky6edee7f2013-11-02 21:07:52 -07003509static void gen8_enable_rps(struct drm_device *dev)
3510{
3511 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01003512 struct intel_engine_cs *ring;
Ben Widawsky6edee7f2013-11-02 21:07:52 -07003513 uint32_t rc6_mask = 0, rp_state_cap;
3514 int unused;
3515
3516 /* 1a: Software RC state - RC0 */
3517 I915_WRITE(GEN6_RC_STATE, 0);
3518
3519 /* 1c & 1d: Get forcewake during program sequence. Although the driver
3520 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
Deepak Sc8d9a592013-11-23 14:55:42 +05303521 gen6_gt_force_wake_get(dev_priv, FORCEWAKE_ALL);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07003522
3523 /* 2a: Disable RC states. */
3524 I915_WRITE(GEN6_RC_CONTROL, 0);
3525
3526 rp_state_cap = I915_READ(GEN6_RP_STATE_CAP);
Ben Widawsky3280e8b2014-03-31 17:16:42 -07003527 parse_rp_state_cap(dev_priv, rp_state_cap);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07003528
3529 /* 2b: Program RC6 thresholds.*/
3530 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16);
3531 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
3532 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
3533 for_each_ring(ring, dev_priv, unused)
3534 I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
3535 I915_WRITE(GEN6_RC_SLEEP, 0);
Tom O'Rourke0d68b252014-04-09 11:44:06 -07003536 if (IS_BROADWELL(dev))
3537 I915_WRITE(GEN6_RC6_THRESHOLD, 625); /* 800us/1.28 for TO */
3538 else
3539 I915_WRITE(GEN6_RC6_THRESHOLD, 50000); /* 50/125ms per EI */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07003540
3541 /* 3: Enable RC6 */
3542 if (intel_enable_rc6(dev) & INTEL_RC6_ENABLE)
3543 rc6_mask = GEN6_RC_CTL_RC6_ENABLE;
Ben Widawskyabbf9d22014-01-28 20:25:41 -08003544 intel_print_rc6_info(dev, rc6_mask);
Tom O'Rourke0d68b252014-04-09 11:44:06 -07003545 if (IS_BROADWELL(dev))
3546 I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
3547 GEN7_RC_CTL_TO_MODE |
3548 rc6_mask);
3549 else
3550 I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
3551 GEN6_RC_CTL_EI_MODE(1) |
3552 rc6_mask);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07003553
3554 /* 4 Program defaults and thresholds for RPS*/
Ben Widawskyf9bdc582014-03-31 17:16:41 -07003555 I915_WRITE(GEN6_RPNSWREQ,
3556 HSW_FREQUENCY(dev_priv->rps.rp1_freq));
3557 I915_WRITE(GEN6_RC_VIDEO_FREQ,
3558 HSW_FREQUENCY(dev_priv->rps.rp1_freq));
Ben Widawsky6edee7f2013-11-02 21:07:52 -07003559 /* NB: Docs say 1s, and 1000000 - which aren't equivalent */
3560 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 100000000 / 128); /* 1 second timeout */
3561
3562 /* Docs recommend 900MHz, and 300 MHz respectively */
3563 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS,
Ben Widawskyb39fb292014-03-19 18:31:11 -07003564 dev_priv->rps.max_freq_softlimit << 24 |
3565 dev_priv->rps.min_freq_softlimit << 16);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07003566
3567 I915_WRITE(GEN6_RP_UP_THRESHOLD, 7600000 / 128); /* 76ms busyness per EI, 90% */
3568 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 31300000 / 128); /* 313ms busyness per EI, 70%*/
3569 I915_WRITE(GEN6_RP_UP_EI, 66000); /* 84.48ms, XXX: random? */
3570 I915_WRITE(GEN6_RP_DOWN_EI, 350000); /* 448ms, XXX: random? */
3571
3572 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
3573
3574 /* 5: Enable RPS */
3575 I915_WRITE(GEN6_RP_CONTROL,
3576 GEN6_RP_MEDIA_TURBO |
3577 GEN6_RP_MEDIA_HW_NORMAL_MODE |
Tom O'Rourke7405f422014-06-10 16:26:34 -07003578 GEN6_RP_MEDIA_IS_GFX |
Ben Widawsky6edee7f2013-11-02 21:07:52 -07003579 GEN6_RP_ENABLE |
3580 GEN6_RP_UP_BUSY_AVG |
3581 GEN6_RP_DOWN_IDLE_AVG);
3582
3583 /* 6: Ring frequency + overclocking (our driver does this later */
3584
3585 gen6_set_rps(dev, (I915_READ(GEN6_GT_PERF_STATUS) & 0xff00) >> 8);
3586
Ben Widawsky09610212014-05-15 20:58:08 +03003587 gen8_enable_rps_interrupts(dev);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07003588
Deepak Sc8d9a592013-11-23 14:55:42 +05303589 gen6_gt_force_wake_put(dev_priv, FORCEWAKE_ALL);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07003590}
3591
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02003592static void gen6_enable_rps(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003593{
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02003594 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01003595 struct intel_engine_cs *ring;
Ben Widawsky2a5913a2014-03-19 18:31:13 -07003596 u32 rp_state_cap;
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01003597 u32 gt_perf_status;
Ben Widawskyd060c162014-03-19 18:31:08 -07003598 u32 rc6vids, pcu_mbox = 0, rc6_mask = 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003599 u32 gtfifodbg;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003600 int rc6_mode;
Ben Widawsky42c05262012-09-26 10:34:00 -07003601 int i, ret;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003602
Jesse Barnes4fc688c2012-11-02 11:14:01 -07003603 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02003604
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003605 /* Here begins a magic sequence of register writes to enable
3606 * auto-downclocking.
3607 *
3608 * Perhaps there might be some value in exposing these to
3609 * userspace...
3610 */
3611 I915_WRITE(GEN6_RC_STATE, 0);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003612
3613 /* Clear the DBG now so we don't confuse earlier errors */
3614 if ((gtfifodbg = I915_READ(GTFIFODBG))) {
3615 DRM_ERROR("GT fifo had a previous error %x\n", gtfifodbg);
3616 I915_WRITE(GTFIFODBG, gtfifodbg);
3617 }
3618
Deepak Sc8d9a592013-11-23 14:55:42 +05303619 gen6_gt_force_wake_get(dev_priv, FORCEWAKE_ALL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003620
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01003621 rp_state_cap = I915_READ(GEN6_RP_STATE_CAP);
3622 gt_perf_status = I915_READ(GEN6_GT_PERF_STATUS);
3623
Ben Widawsky3280e8b2014-03-31 17:16:42 -07003624 parse_rp_state_cap(dev_priv, rp_state_cap);
Jeff McGeedd0a1aa2014-02-04 11:32:31 -06003625
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003626 /* disable the counters and set deterministic thresholds */
3627 I915_WRITE(GEN6_RC_CONTROL, 0);
3628
3629 I915_WRITE(GEN6_RC1_WAKE_RATE_LIMIT, 1000 << 16);
3630 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16 | 30);
3631 I915_WRITE(GEN6_RC6pp_WAKE_RATE_LIMIT, 30);
3632 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
3633 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
3634
Chris Wilsonb4519512012-05-11 14:29:30 +01003635 for_each_ring(ring, dev_priv, i)
3636 I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003637
3638 I915_WRITE(GEN6_RC_SLEEP, 0);
3639 I915_WRITE(GEN6_RC1e_THRESHOLD, 1000);
Daniel Vetter29c78f62013-11-16 16:04:26 +01003640 if (IS_IVYBRIDGE(dev))
Stéphane Marchesin351aa562013-08-13 11:55:17 -07003641 I915_WRITE(GEN6_RC6_THRESHOLD, 125000);
3642 else
3643 I915_WRITE(GEN6_RC6_THRESHOLD, 50000);
Stéphane Marchesin0920a482013-01-29 19:41:59 -08003644 I915_WRITE(GEN6_RC6p_THRESHOLD, 150000);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003645 I915_WRITE(GEN6_RC6pp_THRESHOLD, 64000); /* unused */
3646
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03003647 /* Check if we are enabling RC6 */
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003648 rc6_mode = intel_enable_rc6(dev_priv->dev);
3649 if (rc6_mode & INTEL_RC6_ENABLE)
3650 rc6_mask |= GEN6_RC_CTL_RC6_ENABLE;
3651
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03003652 /* We don't use those on Haswell */
3653 if (!IS_HASWELL(dev)) {
3654 if (rc6_mode & INTEL_RC6p_ENABLE)
3655 rc6_mask |= GEN6_RC_CTL_RC6p_ENABLE;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003656
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03003657 if (rc6_mode & INTEL_RC6pp_ENABLE)
3658 rc6_mask |= GEN6_RC_CTL_RC6pp_ENABLE;
3659 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003660
Ben Widawskydc39fff2013-10-18 12:32:07 -07003661 intel_print_rc6_info(dev, rc6_mask);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003662
3663 I915_WRITE(GEN6_RC_CONTROL,
3664 rc6_mask |
3665 GEN6_RC_CTL_EI_MODE(1) |
3666 GEN6_RC_CTL_HW_ENABLE);
3667
Chris Wilsondd75fdc2013-09-25 17:34:57 +01003668 /* Power down if completely idle for over 50ms */
3669 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 50000);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003670 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003671
Ben Widawsky42c05262012-09-26 10:34:00 -07003672 ret = sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_MIN_FREQ_TABLE, 0);
Ben Widawskyd060c162014-03-19 18:31:08 -07003673 if (ret)
Ben Widawsky42c05262012-09-26 10:34:00 -07003674 DRM_DEBUG_DRIVER("Failed to set the min frequency\n");
Ben Widawskyd060c162014-03-19 18:31:08 -07003675
3676 ret = sandybridge_pcode_read(dev_priv, GEN6_READ_OC_PARAMS, &pcu_mbox);
3677 if (!ret && (pcu_mbox & (1<<31))) { /* OC supported */
3678 DRM_DEBUG_DRIVER("Overclocking supported. Max: %dMHz, Overclock max: %dMHz\n",
Ben Widawskyb39fb292014-03-19 18:31:11 -07003679 (dev_priv->rps.max_freq_softlimit & 0xff) * 50,
Ben Widawskyd060c162014-03-19 18:31:08 -07003680 (pcu_mbox & 0xff) * 50);
Ben Widawskyb39fb292014-03-19 18:31:11 -07003681 dev_priv->rps.max_freq = pcu_mbox & 0xff;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003682 }
3683
Chris Wilsondd75fdc2013-09-25 17:34:57 +01003684 dev_priv->rps.power = HIGH_POWER; /* force a reset */
Ben Widawskyb39fb292014-03-19 18:31:11 -07003685 gen6_set_rps(dev_priv->dev, dev_priv->rps.min_freq_softlimit);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003686
Daniel Vetter44fc7d52013-07-12 22:43:27 +02003687 gen6_enable_rps_interrupts(dev);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003688
Ben Widawsky31643d52012-09-26 10:34:01 -07003689 rc6vids = 0;
3690 ret = sandybridge_pcode_read(dev_priv, GEN6_PCODE_READ_RC6VIDS, &rc6vids);
3691 if (IS_GEN6(dev) && ret) {
3692 DRM_DEBUG_DRIVER("Couldn't check for BIOS workaround\n");
3693 } else if (IS_GEN6(dev) && (GEN6_DECODE_RC6_VID(rc6vids & 0xff) < 450)) {
3694 DRM_DEBUG_DRIVER("You should update your BIOS. Correcting minimum rc6 voltage (%dmV->%dmV)\n",
3695 GEN6_DECODE_RC6_VID(rc6vids & 0xff), 450);
3696 rc6vids &= 0xffff00;
3697 rc6vids |= GEN6_ENCODE_RC6_VID(450);
3698 ret = sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_RC6VIDS, rc6vids);
3699 if (ret)
3700 DRM_ERROR("Couldn't fix incorrect rc6 voltage\n");
3701 }
3702
Deepak Sc8d9a592013-11-23 14:55:42 +05303703 gen6_gt_force_wake_put(dev_priv, FORCEWAKE_ALL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003704}
3705
Imre Deakc2bc2fc2014-04-18 16:16:23 +03003706static void __gen6_update_ring_freq(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003707{
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02003708 struct drm_i915_private *dev_priv = dev->dev_private;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003709 int min_freq = 15;
Chris Wilson3ebecd02013-04-12 19:10:13 +01003710 unsigned int gpu_freq;
3711 unsigned int max_ia_freq, min_ring_freq;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003712 int scaling_factor = 180;
Ben Widawskyeda79642013-10-07 17:15:48 -03003713 struct cpufreq_policy *policy;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003714
Jesse Barnes4fc688c2012-11-02 11:14:01 -07003715 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02003716
Ben Widawskyeda79642013-10-07 17:15:48 -03003717 policy = cpufreq_cpu_get(0);
3718 if (policy) {
3719 max_ia_freq = policy->cpuinfo.max_freq;
3720 cpufreq_cpu_put(policy);
3721 } else {
3722 /*
3723 * Default to measured freq if none found, PCU will ensure we
3724 * don't go over
3725 */
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003726 max_ia_freq = tsc_khz;
Ben Widawskyeda79642013-10-07 17:15:48 -03003727 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003728
3729 /* Convert from kHz to MHz */
3730 max_ia_freq /= 1000;
3731
Ben Widawsky153b4b952013-10-22 22:05:09 -07003732 min_ring_freq = I915_READ(DCLK) & 0xf;
Ben Widawskyf6aca452013-10-02 09:25:02 -07003733 /* convert DDR frequency from units of 266.6MHz to bandwidth */
3734 min_ring_freq = mult_frac(min_ring_freq, 8, 3);
Chris Wilson3ebecd02013-04-12 19:10:13 +01003735
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003736 /*
3737 * For each potential GPU frequency, load a ring frequency we'd like
3738 * to use for memory access. We do this by specifying the IA frequency
3739 * the PCU should use as a reference to determine the ring frequency.
3740 */
Ben Widawskyb39fb292014-03-19 18:31:11 -07003741 for (gpu_freq = dev_priv->rps.max_freq_softlimit; gpu_freq >= dev_priv->rps.min_freq_softlimit;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003742 gpu_freq--) {
Ben Widawskyb39fb292014-03-19 18:31:11 -07003743 int diff = dev_priv->rps.max_freq_softlimit - gpu_freq;
Chris Wilson3ebecd02013-04-12 19:10:13 +01003744 unsigned int ia_freq = 0, ring_freq = 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003745
Ben Widawsky46c764d2013-11-02 21:07:49 -07003746 if (INTEL_INFO(dev)->gen >= 8) {
3747 /* max(2 * GT, DDR). NB: GT is 50MHz units */
3748 ring_freq = max(min_ring_freq, gpu_freq);
3749 } else if (IS_HASWELL(dev)) {
Ben Widawskyf6aca452013-10-02 09:25:02 -07003750 ring_freq = mult_frac(gpu_freq, 5, 4);
Chris Wilson3ebecd02013-04-12 19:10:13 +01003751 ring_freq = max(min_ring_freq, ring_freq);
3752 /* leave ia_freq as the default, chosen by cpufreq */
3753 } else {
3754 /* On older processors, there is no separate ring
3755 * clock domain, so in order to boost the bandwidth
3756 * of the ring, we need to upclock the CPU (ia_freq).
3757 *
3758 * For GPU frequencies less than 750MHz,
3759 * just use the lowest ring freq.
3760 */
3761 if (gpu_freq < min_freq)
3762 ia_freq = 800;
3763 else
3764 ia_freq = max_ia_freq - ((diff * scaling_factor) / 2);
3765 ia_freq = DIV_ROUND_CLOSEST(ia_freq, 100);
3766 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003767
Ben Widawsky42c05262012-09-26 10:34:00 -07003768 sandybridge_pcode_write(dev_priv,
3769 GEN6_PCODE_WRITE_MIN_FREQ_TABLE,
Chris Wilson3ebecd02013-04-12 19:10:13 +01003770 ia_freq << GEN6_PCODE_FREQ_IA_RATIO_SHIFT |
3771 ring_freq << GEN6_PCODE_FREQ_RING_RATIO_SHIFT |
3772 gpu_freq);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003773 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003774}
3775
Imre Deakc2bc2fc2014-04-18 16:16:23 +03003776void gen6_update_ring_freq(struct drm_device *dev)
3777{
3778 struct drm_i915_private *dev_priv = dev->dev_private;
3779
3780 if (INTEL_INFO(dev)->gen < 6 || IS_VALLEYVIEW(dev))
3781 return;
3782
3783 mutex_lock(&dev_priv->rps.hw_lock);
3784 __gen6_update_ring_freq(dev);
3785 mutex_unlock(&dev_priv->rps.hw_lock);
3786}
3787
Ville Syrjälä03af2042014-06-28 02:03:53 +03003788static int cherryview_rps_max_freq(struct drm_i915_private *dev_priv)
Deepak S2b6b3a02014-05-27 15:59:30 +05303789{
3790 u32 val, rp0;
3791
3792 val = vlv_punit_read(dev_priv, PUNIT_GPU_STATUS_REG);
3793 rp0 = (val >> PUNIT_GPU_STATUS_MAX_FREQ_SHIFT) & PUNIT_GPU_STATUS_MAX_FREQ_MASK;
3794
3795 return rp0;
3796}
3797
3798static int cherryview_rps_rpe_freq(struct drm_i915_private *dev_priv)
3799{
3800 u32 val, rpe;
3801
3802 val = vlv_punit_read(dev_priv, PUNIT_GPU_DUTYCYCLE_REG);
3803 rpe = (val >> PUNIT_GPU_DUTYCYCLE_RPE_FREQ_SHIFT) & PUNIT_GPU_DUTYCYCLE_RPE_FREQ_MASK;
3804
3805 return rpe;
3806}
3807
Deepak S7707df42014-07-12 18:46:14 +05303808static int cherryview_rps_guar_freq(struct drm_i915_private *dev_priv)
3809{
3810 u32 val, rp1;
3811
3812 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
3813 rp1 = (val >> PUNIT_GPU_STATUS_MAX_FREQ_SHIFT) & PUNIT_GPU_STATUS_MAX_FREQ_MASK;
3814
3815 return rp1;
3816}
3817
Ville Syrjälä03af2042014-06-28 02:03:53 +03003818static int cherryview_rps_min_freq(struct drm_i915_private *dev_priv)
Deepak S2b6b3a02014-05-27 15:59:30 +05303819{
3820 u32 val, rpn;
3821
3822 val = vlv_punit_read(dev_priv, PUNIT_GPU_STATUS_REG);
3823 rpn = (val >> PUNIT_GPU_STATIS_GFX_MIN_FREQ_SHIFT) & PUNIT_GPU_STATUS_GFX_MIN_FREQ_MASK;
3824 return rpn;
3825}
3826
Deepak Sf8f2b002014-07-10 13:16:21 +05303827static int valleyview_rps_guar_freq(struct drm_i915_private *dev_priv)
3828{
3829 u32 val, rp1;
3830
3831 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FREQ_FUSE);
3832
3833 rp1 = (val & FB_GFX_FGUARANTEED_FREQ_FUSE_MASK) >> FB_GFX_FGUARANTEED_FREQ_FUSE_SHIFT;
3834
3835 return rp1;
3836}
3837
Ville Syrjälä03af2042014-06-28 02:03:53 +03003838static int valleyview_rps_max_freq(struct drm_i915_private *dev_priv)
Jesse Barnes0a073b82013-04-17 15:54:58 -07003839{
3840 u32 val, rp0;
3841
Jani Nikula64936252013-05-22 15:36:20 +03003842 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FREQ_FUSE);
Jesse Barnes0a073b82013-04-17 15:54:58 -07003843
3844 rp0 = (val & FB_GFX_MAX_FREQ_FUSE_MASK) >> FB_GFX_MAX_FREQ_FUSE_SHIFT;
3845 /* Clamp to max */
3846 rp0 = min_t(u32, rp0, 0xea);
3847
3848 return rp0;
3849}
3850
3851static int valleyview_rps_rpe_freq(struct drm_i915_private *dev_priv)
3852{
3853 u32 val, rpe;
3854
Jani Nikula64936252013-05-22 15:36:20 +03003855 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FMAX_FUSE_LO);
Jesse Barnes0a073b82013-04-17 15:54:58 -07003856 rpe = (val & FB_FMAX_VMIN_FREQ_LO_MASK) >> FB_FMAX_VMIN_FREQ_LO_SHIFT;
Jani Nikula64936252013-05-22 15:36:20 +03003857 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FMAX_FUSE_HI);
Jesse Barnes0a073b82013-04-17 15:54:58 -07003858 rpe |= (val & FB_FMAX_VMIN_FREQ_HI_MASK) << 5;
3859
3860 return rpe;
3861}
3862
Ville Syrjälä03af2042014-06-28 02:03:53 +03003863static int valleyview_rps_min_freq(struct drm_i915_private *dev_priv)
Jesse Barnes0a073b82013-04-17 15:54:58 -07003864{
Jani Nikula64936252013-05-22 15:36:20 +03003865 return vlv_punit_read(dev_priv, PUNIT_REG_GPU_LFM) & 0xff;
Jesse Barnes0a073b82013-04-17 15:54:58 -07003866}
3867
Imre Deakae484342014-03-31 15:10:44 +03003868/* Check that the pctx buffer wasn't move under us. */
3869static void valleyview_check_pctx(struct drm_i915_private *dev_priv)
3870{
3871 unsigned long pctx_addr = I915_READ(VLV_PCBR) & ~4095;
3872
3873 WARN_ON(pctx_addr != dev_priv->mm.stolen_base +
3874 dev_priv->vlv_pctx->stolen->start);
3875}
3876
Deepak S38807742014-05-23 21:00:15 +05303877
3878/* Check that the pcbr address is not empty. */
3879static void cherryview_check_pctx(struct drm_i915_private *dev_priv)
3880{
3881 unsigned long pctx_addr = I915_READ(VLV_PCBR) & ~4095;
3882
3883 WARN_ON((pctx_addr >> VLV_PCBR_ADDR_SHIFT) == 0);
3884}
3885
3886static void cherryview_setup_pctx(struct drm_device *dev)
3887{
3888 struct drm_i915_private *dev_priv = dev->dev_private;
3889 unsigned long pctx_paddr, paddr;
3890 struct i915_gtt *gtt = &dev_priv->gtt;
3891 u32 pcbr;
3892 int pctx_size = 32*1024;
3893
3894 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
3895
3896 pcbr = I915_READ(VLV_PCBR);
3897 if ((pcbr >> VLV_PCBR_ADDR_SHIFT) == 0) {
3898 paddr = (dev_priv->mm.stolen_base +
3899 (gtt->stolen_size - pctx_size));
3900
3901 pctx_paddr = (paddr & (~4095));
3902 I915_WRITE(VLV_PCBR, pctx_paddr);
3903 }
3904}
3905
Jesse Barnesc9cddff2013-05-08 10:45:13 -07003906static void valleyview_setup_pctx(struct drm_device *dev)
3907{
3908 struct drm_i915_private *dev_priv = dev->dev_private;
3909 struct drm_i915_gem_object *pctx;
3910 unsigned long pctx_paddr;
3911 u32 pcbr;
3912 int pctx_size = 24*1024;
3913
Imre Deak17b0c1f2014-02-11 21:39:06 +02003914 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
3915
Jesse Barnesc9cddff2013-05-08 10:45:13 -07003916 pcbr = I915_READ(VLV_PCBR);
3917 if (pcbr) {
3918 /* BIOS set it up already, grab the pre-alloc'd space */
3919 int pcbr_offset;
3920
3921 pcbr_offset = (pcbr & (~4095)) - dev_priv->mm.stolen_base;
3922 pctx = i915_gem_object_create_stolen_for_preallocated(dev_priv->dev,
3923 pcbr_offset,
Daniel Vetter190d6cd2013-07-04 13:06:28 +02003924 I915_GTT_OFFSET_NONE,
Jesse Barnesc9cddff2013-05-08 10:45:13 -07003925 pctx_size);
3926 goto out;
3927 }
3928
3929 /*
3930 * From the Gunit register HAS:
3931 * The Gfx driver is expected to program this register and ensure
3932 * proper allocation within Gfx stolen memory. For example, this
3933 * register should be programmed such than the PCBR range does not
3934 * overlap with other ranges, such as the frame buffer, protected
3935 * memory, or any other relevant ranges.
3936 */
3937 pctx = i915_gem_object_create_stolen(dev, pctx_size);
3938 if (!pctx) {
3939 DRM_DEBUG("not enough stolen space for PCTX, disabling\n");
3940 return;
3941 }
3942
3943 pctx_paddr = dev_priv->mm.stolen_base + pctx->stolen->start;
3944 I915_WRITE(VLV_PCBR, pctx_paddr);
3945
3946out:
3947 dev_priv->vlv_pctx = pctx;
3948}
3949
Imre Deakae484342014-03-31 15:10:44 +03003950static void valleyview_cleanup_pctx(struct drm_device *dev)
3951{
3952 struct drm_i915_private *dev_priv = dev->dev_private;
3953
3954 if (WARN_ON(!dev_priv->vlv_pctx))
3955 return;
3956
3957 drm_gem_object_unreference(&dev_priv->vlv_pctx->base);
3958 dev_priv->vlv_pctx = NULL;
3959}
3960
Imre Deak4e805192014-04-14 20:24:41 +03003961static void valleyview_init_gt_powersave(struct drm_device *dev)
3962{
3963 struct drm_i915_private *dev_priv = dev->dev_private;
3964
3965 valleyview_setup_pctx(dev);
3966
3967 mutex_lock(&dev_priv->rps.hw_lock);
3968
3969 dev_priv->rps.max_freq = valleyview_rps_max_freq(dev_priv);
3970 dev_priv->rps.rp0_freq = dev_priv->rps.max_freq;
3971 DRM_DEBUG_DRIVER("max GPU freq: %d MHz (%u)\n",
3972 vlv_gpu_freq(dev_priv, dev_priv->rps.max_freq),
3973 dev_priv->rps.max_freq);
3974
3975 dev_priv->rps.efficient_freq = valleyview_rps_rpe_freq(dev_priv);
3976 DRM_DEBUG_DRIVER("RPe GPU freq: %d MHz (%u)\n",
3977 vlv_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
3978 dev_priv->rps.efficient_freq);
3979
Deepak Sf8f2b002014-07-10 13:16:21 +05303980 dev_priv->rps.rp1_freq = valleyview_rps_guar_freq(dev_priv);
3981 DRM_DEBUG_DRIVER("RP1(Guar Freq) GPU freq: %d MHz (%u)\n",
3982 vlv_gpu_freq(dev_priv, dev_priv->rps.rp1_freq),
3983 dev_priv->rps.rp1_freq);
3984
Imre Deak4e805192014-04-14 20:24:41 +03003985 dev_priv->rps.min_freq = valleyview_rps_min_freq(dev_priv);
3986 DRM_DEBUG_DRIVER("min GPU freq: %d MHz (%u)\n",
3987 vlv_gpu_freq(dev_priv, dev_priv->rps.min_freq),
3988 dev_priv->rps.min_freq);
3989
3990 /* Preserve min/max settings in case of re-init */
3991 if (dev_priv->rps.max_freq_softlimit == 0)
3992 dev_priv->rps.max_freq_softlimit = dev_priv->rps.max_freq;
3993
3994 if (dev_priv->rps.min_freq_softlimit == 0)
3995 dev_priv->rps.min_freq_softlimit = dev_priv->rps.min_freq;
3996
3997 mutex_unlock(&dev_priv->rps.hw_lock);
3998}
3999
Deepak S38807742014-05-23 21:00:15 +05304000static void cherryview_init_gt_powersave(struct drm_device *dev)
4001{
Deepak S2b6b3a02014-05-27 15:59:30 +05304002 struct drm_i915_private *dev_priv = dev->dev_private;
4003
Deepak S38807742014-05-23 21:00:15 +05304004 cherryview_setup_pctx(dev);
Deepak S2b6b3a02014-05-27 15:59:30 +05304005
4006 mutex_lock(&dev_priv->rps.hw_lock);
4007
4008 dev_priv->rps.max_freq = cherryview_rps_max_freq(dev_priv);
4009 dev_priv->rps.rp0_freq = dev_priv->rps.max_freq;
4010 DRM_DEBUG_DRIVER("max GPU freq: %d MHz (%u)\n",
4011 vlv_gpu_freq(dev_priv, dev_priv->rps.max_freq),
4012 dev_priv->rps.max_freq);
4013
4014 dev_priv->rps.efficient_freq = cherryview_rps_rpe_freq(dev_priv);
4015 DRM_DEBUG_DRIVER("RPe GPU freq: %d MHz (%u)\n",
4016 vlv_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
4017 dev_priv->rps.efficient_freq);
4018
Deepak S7707df42014-07-12 18:46:14 +05304019 dev_priv->rps.rp1_freq = cherryview_rps_guar_freq(dev_priv);
4020 DRM_DEBUG_DRIVER("RP1(Guar) GPU freq: %d MHz (%u)\n",
4021 vlv_gpu_freq(dev_priv, dev_priv->rps.rp1_freq),
4022 dev_priv->rps.rp1_freq);
4023
Deepak S2b6b3a02014-05-27 15:59:30 +05304024 dev_priv->rps.min_freq = cherryview_rps_min_freq(dev_priv);
4025 DRM_DEBUG_DRIVER("min GPU freq: %d MHz (%u)\n",
4026 vlv_gpu_freq(dev_priv, dev_priv->rps.min_freq),
4027 dev_priv->rps.min_freq);
4028
4029 /* Preserve min/max settings in case of re-init */
4030 if (dev_priv->rps.max_freq_softlimit == 0)
4031 dev_priv->rps.max_freq_softlimit = dev_priv->rps.max_freq;
4032
4033 if (dev_priv->rps.min_freq_softlimit == 0)
4034 dev_priv->rps.min_freq_softlimit = dev_priv->rps.min_freq;
4035
4036 mutex_unlock(&dev_priv->rps.hw_lock);
Deepak S38807742014-05-23 21:00:15 +05304037}
4038
Imre Deak4e805192014-04-14 20:24:41 +03004039static void valleyview_cleanup_gt_powersave(struct drm_device *dev)
4040{
4041 valleyview_cleanup_pctx(dev);
4042}
4043
Deepak S38807742014-05-23 21:00:15 +05304044static void cherryview_enable_rps(struct drm_device *dev)
4045{
4046 struct drm_i915_private *dev_priv = dev->dev_private;
4047 struct intel_engine_cs *ring;
Deepak S2b6b3a02014-05-27 15:59:30 +05304048 u32 gtfifodbg, val, rc6_mode = 0, pcbr;
Deepak S38807742014-05-23 21:00:15 +05304049 int i;
4050
4051 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
4052
4053 gtfifodbg = I915_READ(GTFIFODBG);
4054 if (gtfifodbg) {
4055 DRM_DEBUG_DRIVER("GT fifo had a previous error %x\n",
4056 gtfifodbg);
4057 I915_WRITE(GTFIFODBG, gtfifodbg);
4058 }
4059
4060 cherryview_check_pctx(dev_priv);
4061
4062 /* 1a & 1b: Get forcewake during program sequence. Although the driver
4063 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
4064 gen6_gt_force_wake_get(dev_priv, FORCEWAKE_ALL);
4065
4066 /* 2a: Program RC6 thresholds.*/
4067 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16);
4068 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
4069 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
4070
4071 for_each_ring(ring, dev_priv, i)
4072 I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
4073 I915_WRITE(GEN6_RC_SLEEP, 0);
4074
4075 I915_WRITE(GEN6_RC6_THRESHOLD, 50000); /* 50/125ms per EI */
4076
4077 /* allows RC6 residency counter to work */
4078 I915_WRITE(VLV_COUNTER_CONTROL,
4079 _MASKED_BIT_ENABLE(VLV_COUNT_RANGE_HIGH |
4080 VLV_MEDIA_RC6_COUNT_EN |
4081 VLV_RENDER_RC6_COUNT_EN));
4082
4083 /* For now we assume BIOS is allocating and populating the PCBR */
4084 pcbr = I915_READ(VLV_PCBR);
4085
4086 DRM_DEBUG_DRIVER("PCBR offset : 0x%x\n", pcbr);
4087
4088 /* 3: Enable RC6 */
4089 if ((intel_enable_rc6(dev) & INTEL_RC6_ENABLE) &&
4090 (pcbr >> VLV_PCBR_ADDR_SHIFT))
4091 rc6_mode = GEN6_RC_CTL_EI_MODE(1);
4092
4093 I915_WRITE(GEN6_RC_CONTROL, rc6_mode);
4094
Deepak S2b6b3a02014-05-27 15:59:30 +05304095 /* 4 Program defaults and thresholds for RPS*/
4096 I915_WRITE(GEN6_RP_UP_THRESHOLD, 59400);
4097 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 245000);
4098 I915_WRITE(GEN6_RP_UP_EI, 66000);
4099 I915_WRITE(GEN6_RP_DOWN_EI, 350000);
4100
4101 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
4102
Tom O'Rourke7405f422014-06-10 16:26:34 -07004103 /* WaDisablePwrmtrEvent:chv (pre-production hw) */
4104 I915_WRITE(0xA80C, I915_READ(0xA80C) & 0x00ffffff);
4105 I915_WRITE(0xA810, I915_READ(0xA810) & 0xffffff00);
4106
Deepak S2b6b3a02014-05-27 15:59:30 +05304107 /* 5: Enable RPS */
4108 I915_WRITE(GEN6_RP_CONTROL,
4109 GEN6_RP_MEDIA_HW_NORMAL_MODE |
Tom O'Rourke7405f422014-06-10 16:26:34 -07004110 GEN6_RP_MEDIA_IS_GFX | /* WaSetMaskForGfxBusyness:chv (pre-production hw ?) */
Deepak S2b6b3a02014-05-27 15:59:30 +05304111 GEN6_RP_ENABLE |
4112 GEN6_RP_UP_BUSY_AVG |
4113 GEN6_RP_DOWN_IDLE_AVG);
4114
4115 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
4116
4117 DRM_DEBUG_DRIVER("GPLL enabled? %s\n", val & 0x10 ? "yes" : "no");
4118 DRM_DEBUG_DRIVER("GPU status: 0x%08x\n", val);
4119
4120 dev_priv->rps.cur_freq = (val >> 8) & 0xff;
4121 DRM_DEBUG_DRIVER("current GPU freq: %d MHz (%u)\n",
4122 vlv_gpu_freq(dev_priv, dev_priv->rps.cur_freq),
4123 dev_priv->rps.cur_freq);
4124
4125 DRM_DEBUG_DRIVER("setting GPU freq to %d MHz (%u)\n",
4126 vlv_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
4127 dev_priv->rps.efficient_freq);
4128
4129 valleyview_set_rps(dev_priv->dev, dev_priv->rps.efficient_freq);
4130
Deepak S3497a562014-07-10 13:16:26 +05304131 gen8_enable_rps_interrupts(dev);
4132
Deepak S38807742014-05-23 21:00:15 +05304133 gen6_gt_force_wake_put(dev_priv, FORCEWAKE_ALL);
4134}
4135
Jesse Barnes0a073b82013-04-17 15:54:58 -07004136static void valleyview_enable_rps(struct drm_device *dev)
4137{
4138 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01004139 struct intel_engine_cs *ring;
Ben Widawsky2a5913a2014-03-19 18:31:13 -07004140 u32 gtfifodbg, val, rc6_mode = 0;
Jesse Barnes0a073b82013-04-17 15:54:58 -07004141 int i;
4142
4143 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
4144
Imre Deakae484342014-03-31 15:10:44 +03004145 valleyview_check_pctx(dev_priv);
4146
Jesse Barnes0a073b82013-04-17 15:54:58 -07004147 if ((gtfifodbg = I915_READ(GTFIFODBG))) {
Jesse Barnesf7d85c12013-09-27 10:40:54 -07004148 DRM_DEBUG_DRIVER("GT fifo had a previous error %x\n",
4149 gtfifodbg);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004150 I915_WRITE(GTFIFODBG, gtfifodbg);
4151 }
4152
Deepak Sc8d9a592013-11-23 14:55:42 +05304153 /* If VLV, Forcewake all wells, else re-direct to regular path */
4154 gen6_gt_force_wake_get(dev_priv, FORCEWAKE_ALL);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004155
4156 I915_WRITE(GEN6_RP_UP_THRESHOLD, 59400);
4157 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 245000);
4158 I915_WRITE(GEN6_RP_UP_EI, 66000);
4159 I915_WRITE(GEN6_RP_DOWN_EI, 350000);
4160
4161 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
Deepak S31685c22014-07-03 17:33:01 -04004162 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 0xf4240);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004163
4164 I915_WRITE(GEN6_RP_CONTROL,
4165 GEN6_RP_MEDIA_TURBO |
4166 GEN6_RP_MEDIA_HW_NORMAL_MODE |
4167 GEN6_RP_MEDIA_IS_GFX |
4168 GEN6_RP_ENABLE |
4169 GEN6_RP_UP_BUSY_AVG |
4170 GEN6_RP_DOWN_IDLE_CONT);
4171
4172 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 0x00280000);
4173 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
4174 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
4175
4176 for_each_ring(ring, dev_priv, i)
4177 I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
4178
Jesse Barnes2f0aa302013-11-15 09:32:11 -08004179 I915_WRITE(GEN6_RC6_THRESHOLD, 0x557);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004180
4181 /* allows RC6 residency counter to work */
Jesse Barnes49798eb2013-09-26 17:55:57 -07004182 I915_WRITE(VLV_COUNTER_CONTROL,
Deepak S31685c22014-07-03 17:33:01 -04004183 _MASKED_BIT_ENABLE(VLV_MEDIA_RC0_COUNT_EN |
4184 VLV_RENDER_RC0_COUNT_EN |
Jesse Barnes49798eb2013-09-26 17:55:57 -07004185 VLV_MEDIA_RC6_COUNT_EN |
4186 VLV_RENDER_RC6_COUNT_EN));
Deepak S31685c22014-07-03 17:33:01 -04004187
Jesse Barnesa2b23fe2013-09-19 09:33:13 -07004188 if (intel_enable_rc6(dev) & INTEL_RC6_ENABLE)
Jesse Barnes6b88f292013-11-15 09:32:12 -08004189 rc6_mode = GEN7_RC_CTL_TO_MODE | VLV_RC_CTL_CTX_RST_PARALLEL;
Ben Widawskydc39fff2013-10-18 12:32:07 -07004190
4191 intel_print_rc6_info(dev, rc6_mode);
4192
Jesse Barnesa2b23fe2013-09-19 09:33:13 -07004193 I915_WRITE(GEN6_RC_CONTROL, rc6_mode);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004194
Jani Nikula64936252013-05-22 15:36:20 +03004195 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004196
4197 DRM_DEBUG_DRIVER("GPLL enabled? %s\n", val & 0x10 ? "yes" : "no");
4198 DRM_DEBUG_DRIVER("GPU status: 0x%08x\n", val);
4199
Ben Widawskyb39fb292014-03-19 18:31:11 -07004200 dev_priv->rps.cur_freq = (val >> 8) & 0xff;
Ville Syrjälä73008b92013-06-25 19:21:01 +03004201 DRM_DEBUG_DRIVER("current GPU freq: %d MHz (%u)\n",
Ben Widawskyb39fb292014-03-19 18:31:11 -07004202 vlv_gpu_freq(dev_priv, dev_priv->rps.cur_freq),
4203 dev_priv->rps.cur_freq);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004204
Ville Syrjälä73008b92013-06-25 19:21:01 +03004205 DRM_DEBUG_DRIVER("setting GPU freq to %d MHz (%u)\n",
Ben Widawskyb39fb292014-03-19 18:31:11 -07004206 vlv_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
4207 dev_priv->rps.efficient_freq);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004208
Ben Widawskyb39fb292014-03-19 18:31:11 -07004209 valleyview_set_rps(dev_priv->dev, dev_priv->rps.efficient_freq);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004210
Daniel Vetter44fc7d52013-07-12 22:43:27 +02004211 gen6_enable_rps_interrupts(dev);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004212
Deepak Sc8d9a592013-11-23 14:55:42 +05304213 gen6_gt_force_wake_put(dev_priv, FORCEWAKE_ALL);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004214}
4215
Daniel Vetter930ebb42012-06-29 23:32:16 +02004216void ironlake_teardown_rc6(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004217{
4218 struct drm_i915_private *dev_priv = dev->dev_private;
4219
Daniel Vetter3e373942012-11-02 19:55:04 +01004220 if (dev_priv->ips.renderctx) {
Ben Widawskyd7f46fc2013-12-06 14:10:55 -08004221 i915_gem_object_ggtt_unpin(dev_priv->ips.renderctx);
Daniel Vetter3e373942012-11-02 19:55:04 +01004222 drm_gem_object_unreference(&dev_priv->ips.renderctx->base);
4223 dev_priv->ips.renderctx = NULL;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004224 }
4225
Daniel Vetter3e373942012-11-02 19:55:04 +01004226 if (dev_priv->ips.pwrctx) {
Ben Widawskyd7f46fc2013-12-06 14:10:55 -08004227 i915_gem_object_ggtt_unpin(dev_priv->ips.pwrctx);
Daniel Vetter3e373942012-11-02 19:55:04 +01004228 drm_gem_object_unreference(&dev_priv->ips.pwrctx->base);
4229 dev_priv->ips.pwrctx = NULL;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004230 }
4231}
4232
Daniel Vetter930ebb42012-06-29 23:32:16 +02004233static void ironlake_disable_rc6(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004234{
4235 struct drm_i915_private *dev_priv = dev->dev_private;
4236
4237 if (I915_READ(PWRCTXA)) {
4238 /* Wake the GPU, prevent RC6, then restore RSTDBYCTL */
4239 I915_WRITE(RSTDBYCTL, I915_READ(RSTDBYCTL) | RCX_SW_EXIT);
4240 wait_for(((I915_READ(RSTDBYCTL) & RSX_STATUS_MASK) == RSX_STATUS_ON),
4241 50);
4242
4243 I915_WRITE(PWRCTXA, 0);
4244 POSTING_READ(PWRCTXA);
4245
4246 I915_WRITE(RSTDBYCTL, I915_READ(RSTDBYCTL) & ~RCX_SW_EXIT);
4247 POSTING_READ(RSTDBYCTL);
4248 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004249}
4250
4251static int ironlake_setup_rc6(struct drm_device *dev)
4252{
4253 struct drm_i915_private *dev_priv = dev->dev_private;
4254
Daniel Vetter3e373942012-11-02 19:55:04 +01004255 if (dev_priv->ips.renderctx == NULL)
4256 dev_priv->ips.renderctx = intel_alloc_context_page(dev);
4257 if (!dev_priv->ips.renderctx)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004258 return -ENOMEM;
4259
Daniel Vetter3e373942012-11-02 19:55:04 +01004260 if (dev_priv->ips.pwrctx == NULL)
4261 dev_priv->ips.pwrctx = intel_alloc_context_page(dev);
4262 if (!dev_priv->ips.pwrctx) {
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004263 ironlake_teardown_rc6(dev);
4264 return -ENOMEM;
4265 }
4266
4267 return 0;
4268}
4269
Daniel Vetter930ebb42012-06-29 23:32:16 +02004270static void ironlake_enable_rc6(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004271{
4272 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01004273 struct intel_engine_cs *ring = &dev_priv->ring[RCS];
Chris Wilson3e960502012-11-27 16:22:54 +00004274 bool was_interruptible;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004275 int ret;
4276
4277 /* rc6 disabled by default due to repeated reports of hanging during
4278 * boot and resume.
4279 */
4280 if (!intel_enable_rc6(dev))
4281 return;
4282
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02004283 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
4284
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004285 ret = ironlake_setup_rc6(dev);
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02004286 if (ret)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004287 return;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004288
Chris Wilson3e960502012-11-27 16:22:54 +00004289 was_interruptible = dev_priv->mm.interruptible;
4290 dev_priv->mm.interruptible = false;
4291
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004292 /*
4293 * GPU can automatically power down the render unit if given a page
4294 * to save state.
4295 */
Daniel Vetter6d90c952012-04-26 23:28:05 +02004296 ret = intel_ring_begin(ring, 6);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004297 if (ret) {
4298 ironlake_teardown_rc6(dev);
Chris Wilson3e960502012-11-27 16:22:54 +00004299 dev_priv->mm.interruptible = was_interruptible;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004300 return;
4301 }
4302
Daniel Vetter6d90c952012-04-26 23:28:05 +02004303 intel_ring_emit(ring, MI_SUSPEND_FLUSH | MI_SUSPEND_FLUSH_EN);
4304 intel_ring_emit(ring, MI_SET_CONTEXT);
Ben Widawskyf343c5f2013-07-05 14:41:04 -07004305 intel_ring_emit(ring, i915_gem_obj_ggtt_offset(dev_priv->ips.renderctx) |
Daniel Vetter6d90c952012-04-26 23:28:05 +02004306 MI_MM_SPACE_GTT |
4307 MI_SAVE_EXT_STATE_EN |
4308 MI_RESTORE_EXT_STATE_EN |
4309 MI_RESTORE_INHIBIT);
4310 intel_ring_emit(ring, MI_SUSPEND_FLUSH);
4311 intel_ring_emit(ring, MI_NOOP);
4312 intel_ring_emit(ring, MI_FLUSH);
4313 intel_ring_advance(ring);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004314
4315 /*
4316 * Wait for the command parser to advance past MI_SET_CONTEXT. The HW
4317 * does an implicit flush, combined with MI_FLUSH above, it should be
4318 * safe to assume that renderctx is valid
4319 */
Chris Wilson3e960502012-11-27 16:22:54 +00004320 ret = intel_ring_idle(ring);
4321 dev_priv->mm.interruptible = was_interruptible;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004322 if (ret) {
Jani Nikuladef27a52013-03-12 10:49:19 +02004323 DRM_ERROR("failed to enable ironlake power savings\n");
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004324 ironlake_teardown_rc6(dev);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004325 return;
4326 }
4327
Ben Widawskyf343c5f2013-07-05 14:41:04 -07004328 I915_WRITE(PWRCTXA, i915_gem_obj_ggtt_offset(dev_priv->ips.pwrctx) | PWRCTX_EN);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004329 I915_WRITE(RSTDBYCTL, I915_READ(RSTDBYCTL) & ~RCX_SW_EXIT);
Ben Widawskydc39fff2013-10-18 12:32:07 -07004330
Imre Deak91ca6892014-04-14 20:24:25 +03004331 intel_print_rc6_info(dev, GEN6_RC_CTL_RC6_ENABLE);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004332}
4333
Eugeni Dodonovdde18882012-04-18 15:29:24 -03004334static unsigned long intel_pxfreq(u32 vidfreq)
4335{
4336 unsigned long freq;
4337 int div = (vidfreq & 0x3f0000) >> 16;
4338 int post = (vidfreq & 0x3000) >> 12;
4339 int pre = (vidfreq & 0x7);
4340
4341 if (!pre)
4342 return 0;
4343
4344 freq = ((div * 133333) / ((1<<post) * pre));
4345
4346 return freq;
4347}
4348
Daniel Vettereb48eb02012-04-26 23:28:12 +02004349static const struct cparams {
4350 u16 i;
4351 u16 t;
4352 u16 m;
4353 u16 c;
4354} cparams[] = {
4355 { 1, 1333, 301, 28664 },
4356 { 1, 1066, 294, 24460 },
4357 { 1, 800, 294, 25192 },
4358 { 0, 1333, 276, 27605 },
4359 { 0, 1066, 276, 27605 },
4360 { 0, 800, 231, 23784 },
4361};
4362
Chris Wilsonf531dcb2012-09-25 10:16:12 +01004363static unsigned long __i915_chipset_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02004364{
4365 u64 total_count, diff, ret;
4366 u32 count1, count2, count3, m = 0, c = 0;
4367 unsigned long now = jiffies_to_msecs(jiffies), diff1;
4368 int i;
4369
Daniel Vetter02d71952012-08-09 16:44:54 +02004370 assert_spin_locked(&mchdev_lock);
4371
Daniel Vetter20e4d402012-08-08 23:35:39 +02004372 diff1 = now - dev_priv->ips.last_time1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02004373
4374 /* Prevent division-by-zero if we are asking too fast.
4375 * Also, we don't get interesting results if we are polling
4376 * faster than once in 10ms, so just return the saved value
4377 * in such cases.
4378 */
4379 if (diff1 <= 10)
Daniel Vetter20e4d402012-08-08 23:35:39 +02004380 return dev_priv->ips.chipset_power;
Daniel Vettereb48eb02012-04-26 23:28:12 +02004381
4382 count1 = I915_READ(DMIEC);
4383 count2 = I915_READ(DDREC);
4384 count3 = I915_READ(CSIEC);
4385
4386 total_count = count1 + count2 + count3;
4387
4388 /* FIXME: handle per-counter overflow */
Daniel Vetter20e4d402012-08-08 23:35:39 +02004389 if (total_count < dev_priv->ips.last_count1) {
4390 diff = ~0UL - dev_priv->ips.last_count1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02004391 diff += total_count;
4392 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +02004393 diff = total_count - dev_priv->ips.last_count1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02004394 }
4395
4396 for (i = 0; i < ARRAY_SIZE(cparams); i++) {
Daniel Vetter20e4d402012-08-08 23:35:39 +02004397 if (cparams[i].i == dev_priv->ips.c_m &&
4398 cparams[i].t == dev_priv->ips.r_t) {
Daniel Vettereb48eb02012-04-26 23:28:12 +02004399 m = cparams[i].m;
4400 c = cparams[i].c;
4401 break;
4402 }
4403 }
4404
4405 diff = div_u64(diff, diff1);
4406 ret = ((m * diff) + c);
4407 ret = div_u64(ret, 10);
4408
Daniel Vetter20e4d402012-08-08 23:35:39 +02004409 dev_priv->ips.last_count1 = total_count;
4410 dev_priv->ips.last_time1 = now;
Daniel Vettereb48eb02012-04-26 23:28:12 +02004411
Daniel Vetter20e4d402012-08-08 23:35:39 +02004412 dev_priv->ips.chipset_power = ret;
Daniel Vettereb48eb02012-04-26 23:28:12 +02004413
4414 return ret;
4415}
4416
Chris Wilsonf531dcb2012-09-25 10:16:12 +01004417unsigned long i915_chipset_val(struct drm_i915_private *dev_priv)
4418{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00004419 struct drm_device *dev = dev_priv->dev;
Chris Wilsonf531dcb2012-09-25 10:16:12 +01004420 unsigned long val;
4421
Damien Lespiau3d13ef22014-02-07 19:12:47 +00004422 if (INTEL_INFO(dev)->gen != 5)
Chris Wilsonf531dcb2012-09-25 10:16:12 +01004423 return 0;
4424
4425 spin_lock_irq(&mchdev_lock);
4426
4427 val = __i915_chipset_val(dev_priv);
4428
4429 spin_unlock_irq(&mchdev_lock);
4430
4431 return val;
4432}
4433
Daniel Vettereb48eb02012-04-26 23:28:12 +02004434unsigned long i915_mch_val(struct drm_i915_private *dev_priv)
4435{
4436 unsigned long m, x, b;
4437 u32 tsfs;
4438
4439 tsfs = I915_READ(TSFS);
4440
4441 m = ((tsfs & TSFS_SLOPE_MASK) >> TSFS_SLOPE_SHIFT);
4442 x = I915_READ8(TR1);
4443
4444 b = tsfs & TSFS_INTR_MASK;
4445
4446 return ((m * x) / 127) - b;
4447}
4448
4449static u16 pvid_to_extvid(struct drm_i915_private *dev_priv, u8 pxvid)
4450{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00004451 struct drm_device *dev = dev_priv->dev;
Daniel Vettereb48eb02012-04-26 23:28:12 +02004452 static const struct v_table {
4453 u16 vd; /* in .1 mil */
4454 u16 vm; /* in .1 mil */
4455 } v_table[] = {
4456 { 0, 0, },
4457 { 375, 0, },
4458 { 500, 0, },
4459 { 625, 0, },
4460 { 750, 0, },
4461 { 875, 0, },
4462 { 1000, 0, },
4463 { 1125, 0, },
4464 { 4125, 3000, },
4465 { 4125, 3000, },
4466 { 4125, 3000, },
4467 { 4125, 3000, },
4468 { 4125, 3000, },
4469 { 4125, 3000, },
4470 { 4125, 3000, },
4471 { 4125, 3000, },
4472 { 4125, 3000, },
4473 { 4125, 3000, },
4474 { 4125, 3000, },
4475 { 4125, 3000, },
4476 { 4125, 3000, },
4477 { 4125, 3000, },
4478 { 4125, 3000, },
4479 { 4125, 3000, },
4480 { 4125, 3000, },
4481 { 4125, 3000, },
4482 { 4125, 3000, },
4483 { 4125, 3000, },
4484 { 4125, 3000, },
4485 { 4125, 3000, },
4486 { 4125, 3000, },
4487 { 4125, 3000, },
4488 { 4250, 3125, },
4489 { 4375, 3250, },
4490 { 4500, 3375, },
4491 { 4625, 3500, },
4492 { 4750, 3625, },
4493 { 4875, 3750, },
4494 { 5000, 3875, },
4495 { 5125, 4000, },
4496 { 5250, 4125, },
4497 { 5375, 4250, },
4498 { 5500, 4375, },
4499 { 5625, 4500, },
4500 { 5750, 4625, },
4501 { 5875, 4750, },
4502 { 6000, 4875, },
4503 { 6125, 5000, },
4504 { 6250, 5125, },
4505 { 6375, 5250, },
4506 { 6500, 5375, },
4507 { 6625, 5500, },
4508 { 6750, 5625, },
4509 { 6875, 5750, },
4510 { 7000, 5875, },
4511 { 7125, 6000, },
4512 { 7250, 6125, },
4513 { 7375, 6250, },
4514 { 7500, 6375, },
4515 { 7625, 6500, },
4516 { 7750, 6625, },
4517 { 7875, 6750, },
4518 { 8000, 6875, },
4519 { 8125, 7000, },
4520 { 8250, 7125, },
4521 { 8375, 7250, },
4522 { 8500, 7375, },
4523 { 8625, 7500, },
4524 { 8750, 7625, },
4525 { 8875, 7750, },
4526 { 9000, 7875, },
4527 { 9125, 8000, },
4528 { 9250, 8125, },
4529 { 9375, 8250, },
4530 { 9500, 8375, },
4531 { 9625, 8500, },
4532 { 9750, 8625, },
4533 { 9875, 8750, },
4534 { 10000, 8875, },
4535 { 10125, 9000, },
4536 { 10250, 9125, },
4537 { 10375, 9250, },
4538 { 10500, 9375, },
4539 { 10625, 9500, },
4540 { 10750, 9625, },
4541 { 10875, 9750, },
4542 { 11000, 9875, },
4543 { 11125, 10000, },
4544 { 11250, 10125, },
4545 { 11375, 10250, },
4546 { 11500, 10375, },
4547 { 11625, 10500, },
4548 { 11750, 10625, },
4549 { 11875, 10750, },
4550 { 12000, 10875, },
4551 { 12125, 11000, },
4552 { 12250, 11125, },
4553 { 12375, 11250, },
4554 { 12500, 11375, },
4555 { 12625, 11500, },
4556 { 12750, 11625, },
4557 { 12875, 11750, },
4558 { 13000, 11875, },
4559 { 13125, 12000, },
4560 { 13250, 12125, },
4561 { 13375, 12250, },
4562 { 13500, 12375, },
4563 { 13625, 12500, },
4564 { 13750, 12625, },
4565 { 13875, 12750, },
4566 { 14000, 12875, },
4567 { 14125, 13000, },
4568 { 14250, 13125, },
4569 { 14375, 13250, },
4570 { 14500, 13375, },
4571 { 14625, 13500, },
4572 { 14750, 13625, },
4573 { 14875, 13750, },
4574 { 15000, 13875, },
4575 { 15125, 14000, },
4576 { 15250, 14125, },
4577 { 15375, 14250, },
4578 { 15500, 14375, },
4579 { 15625, 14500, },
4580 { 15750, 14625, },
4581 { 15875, 14750, },
4582 { 16000, 14875, },
4583 { 16125, 15000, },
4584 };
Damien Lespiau3d13ef22014-02-07 19:12:47 +00004585 if (INTEL_INFO(dev)->is_mobile)
Daniel Vettereb48eb02012-04-26 23:28:12 +02004586 return v_table[pxvid].vm;
4587 else
4588 return v_table[pxvid].vd;
4589}
4590
Daniel Vetter02d71952012-08-09 16:44:54 +02004591static void __i915_update_gfx_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02004592{
4593 struct timespec now, diff1;
4594 u64 diff;
4595 unsigned long diffms;
4596 u32 count;
4597
Daniel Vetter02d71952012-08-09 16:44:54 +02004598 assert_spin_locked(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004599
4600 getrawmonotonic(&now);
Daniel Vetter20e4d402012-08-08 23:35:39 +02004601 diff1 = timespec_sub(now, dev_priv->ips.last_time2);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004602
4603 /* Don't divide by 0 */
4604 diffms = diff1.tv_sec * 1000 + diff1.tv_nsec / 1000000;
4605 if (!diffms)
4606 return;
4607
4608 count = I915_READ(GFXEC);
4609
Daniel Vetter20e4d402012-08-08 23:35:39 +02004610 if (count < dev_priv->ips.last_count2) {
4611 diff = ~0UL - dev_priv->ips.last_count2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02004612 diff += count;
4613 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +02004614 diff = count - dev_priv->ips.last_count2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02004615 }
4616
Daniel Vetter20e4d402012-08-08 23:35:39 +02004617 dev_priv->ips.last_count2 = count;
4618 dev_priv->ips.last_time2 = now;
Daniel Vettereb48eb02012-04-26 23:28:12 +02004619
4620 /* More magic constants... */
4621 diff = diff * 1181;
4622 diff = div_u64(diff, diffms * 10);
Daniel Vetter20e4d402012-08-08 23:35:39 +02004623 dev_priv->ips.gfx_power = diff;
Daniel Vettereb48eb02012-04-26 23:28:12 +02004624}
4625
Daniel Vetter02d71952012-08-09 16:44:54 +02004626void i915_update_gfx_val(struct drm_i915_private *dev_priv)
4627{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00004628 struct drm_device *dev = dev_priv->dev;
4629
4630 if (INTEL_INFO(dev)->gen != 5)
Daniel Vetter02d71952012-08-09 16:44:54 +02004631 return;
4632
Daniel Vetter92703882012-08-09 16:46:01 +02004633 spin_lock_irq(&mchdev_lock);
Daniel Vetter02d71952012-08-09 16:44:54 +02004634
4635 __i915_update_gfx_val(dev_priv);
4636
Daniel Vetter92703882012-08-09 16:46:01 +02004637 spin_unlock_irq(&mchdev_lock);
Daniel Vetter02d71952012-08-09 16:44:54 +02004638}
4639
Chris Wilsonf531dcb2012-09-25 10:16:12 +01004640static unsigned long __i915_gfx_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02004641{
4642 unsigned long t, corr, state1, corr2, state2;
4643 u32 pxvid, ext_v;
4644
Daniel Vetter02d71952012-08-09 16:44:54 +02004645 assert_spin_locked(&mchdev_lock);
4646
Ben Widawskyb39fb292014-03-19 18:31:11 -07004647 pxvid = I915_READ(PXVFREQ_BASE + (dev_priv->rps.cur_freq * 4));
Daniel Vettereb48eb02012-04-26 23:28:12 +02004648 pxvid = (pxvid >> 24) & 0x7f;
4649 ext_v = pvid_to_extvid(dev_priv, pxvid);
4650
4651 state1 = ext_v;
4652
4653 t = i915_mch_val(dev_priv);
4654
4655 /* Revel in the empirically derived constants */
4656
4657 /* Correction factor in 1/100000 units */
4658 if (t > 80)
4659 corr = ((t * 2349) + 135940);
4660 else if (t >= 50)
4661 corr = ((t * 964) + 29317);
4662 else /* < 50 */
4663 corr = ((t * 301) + 1004);
4664
4665 corr = corr * ((150142 * state1) / 10000 - 78642);
4666 corr /= 100000;
Daniel Vetter20e4d402012-08-08 23:35:39 +02004667 corr2 = (corr * dev_priv->ips.corr);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004668
4669 state2 = (corr2 * state1) / 10000;
4670 state2 /= 100; /* convert to mW */
4671
Daniel Vetter02d71952012-08-09 16:44:54 +02004672 __i915_update_gfx_val(dev_priv);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004673
Daniel Vetter20e4d402012-08-08 23:35:39 +02004674 return dev_priv->ips.gfx_power + state2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02004675}
4676
Chris Wilsonf531dcb2012-09-25 10:16:12 +01004677unsigned long i915_gfx_val(struct drm_i915_private *dev_priv)
4678{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00004679 struct drm_device *dev = dev_priv->dev;
Chris Wilsonf531dcb2012-09-25 10:16:12 +01004680 unsigned long val;
4681
Damien Lespiau3d13ef22014-02-07 19:12:47 +00004682 if (INTEL_INFO(dev)->gen != 5)
Chris Wilsonf531dcb2012-09-25 10:16:12 +01004683 return 0;
4684
4685 spin_lock_irq(&mchdev_lock);
4686
4687 val = __i915_gfx_val(dev_priv);
4688
4689 spin_unlock_irq(&mchdev_lock);
4690
4691 return val;
4692}
4693
Daniel Vettereb48eb02012-04-26 23:28:12 +02004694/**
4695 * i915_read_mch_val - return value for IPS use
4696 *
4697 * Calculate and return a value for the IPS driver to use when deciding whether
4698 * we have thermal and power headroom to increase CPU or GPU power budget.
4699 */
4700unsigned long i915_read_mch_val(void)
4701{
4702 struct drm_i915_private *dev_priv;
4703 unsigned long chipset_val, graphics_val, ret = 0;
4704
Daniel Vetter92703882012-08-09 16:46:01 +02004705 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004706 if (!i915_mch_dev)
4707 goto out_unlock;
4708 dev_priv = i915_mch_dev;
4709
Chris Wilsonf531dcb2012-09-25 10:16:12 +01004710 chipset_val = __i915_chipset_val(dev_priv);
4711 graphics_val = __i915_gfx_val(dev_priv);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004712
4713 ret = chipset_val + graphics_val;
4714
4715out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02004716 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004717
4718 return ret;
4719}
4720EXPORT_SYMBOL_GPL(i915_read_mch_val);
4721
4722/**
4723 * i915_gpu_raise - raise GPU frequency limit
4724 *
4725 * Raise the limit; IPS indicates we have thermal headroom.
4726 */
4727bool i915_gpu_raise(void)
4728{
4729 struct drm_i915_private *dev_priv;
4730 bool ret = true;
4731
Daniel Vetter92703882012-08-09 16:46:01 +02004732 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004733 if (!i915_mch_dev) {
4734 ret = false;
4735 goto out_unlock;
4736 }
4737 dev_priv = i915_mch_dev;
4738
Daniel Vetter20e4d402012-08-08 23:35:39 +02004739 if (dev_priv->ips.max_delay > dev_priv->ips.fmax)
4740 dev_priv->ips.max_delay--;
Daniel Vettereb48eb02012-04-26 23:28:12 +02004741
4742out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02004743 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004744
4745 return ret;
4746}
4747EXPORT_SYMBOL_GPL(i915_gpu_raise);
4748
4749/**
4750 * i915_gpu_lower - lower GPU frequency limit
4751 *
4752 * IPS indicates we're close to a thermal limit, so throttle back the GPU
4753 * frequency maximum.
4754 */
4755bool i915_gpu_lower(void)
4756{
4757 struct drm_i915_private *dev_priv;
4758 bool ret = true;
4759
Daniel Vetter92703882012-08-09 16:46:01 +02004760 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004761 if (!i915_mch_dev) {
4762 ret = false;
4763 goto out_unlock;
4764 }
4765 dev_priv = i915_mch_dev;
4766
Daniel Vetter20e4d402012-08-08 23:35:39 +02004767 if (dev_priv->ips.max_delay < dev_priv->ips.min_delay)
4768 dev_priv->ips.max_delay++;
Daniel Vettereb48eb02012-04-26 23:28:12 +02004769
4770out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02004771 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004772
4773 return ret;
4774}
4775EXPORT_SYMBOL_GPL(i915_gpu_lower);
4776
4777/**
4778 * i915_gpu_busy - indicate GPU business to IPS
4779 *
4780 * Tell the IPS driver whether or not the GPU is busy.
4781 */
4782bool i915_gpu_busy(void)
4783{
4784 struct drm_i915_private *dev_priv;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01004785 struct intel_engine_cs *ring;
Daniel Vettereb48eb02012-04-26 23:28:12 +02004786 bool ret = false;
Chris Wilsonf047e392012-07-21 12:31:41 +01004787 int i;
Daniel Vettereb48eb02012-04-26 23:28:12 +02004788
Daniel Vetter92703882012-08-09 16:46:01 +02004789 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004790 if (!i915_mch_dev)
4791 goto out_unlock;
4792 dev_priv = i915_mch_dev;
4793
Chris Wilsonf047e392012-07-21 12:31:41 +01004794 for_each_ring(ring, dev_priv, i)
4795 ret |= !list_empty(&ring->request_list);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004796
4797out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02004798 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004799
4800 return ret;
4801}
4802EXPORT_SYMBOL_GPL(i915_gpu_busy);
4803
4804/**
4805 * i915_gpu_turbo_disable - disable graphics turbo
4806 *
4807 * Disable graphics turbo by resetting the max frequency and setting the
4808 * current frequency to the default.
4809 */
4810bool i915_gpu_turbo_disable(void)
4811{
4812 struct drm_i915_private *dev_priv;
4813 bool ret = true;
4814
Daniel Vetter92703882012-08-09 16:46:01 +02004815 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004816 if (!i915_mch_dev) {
4817 ret = false;
4818 goto out_unlock;
4819 }
4820 dev_priv = i915_mch_dev;
4821
Daniel Vetter20e4d402012-08-08 23:35:39 +02004822 dev_priv->ips.max_delay = dev_priv->ips.fstart;
Daniel Vettereb48eb02012-04-26 23:28:12 +02004823
Daniel Vetter20e4d402012-08-08 23:35:39 +02004824 if (!ironlake_set_drps(dev_priv->dev, dev_priv->ips.fstart))
Daniel Vettereb48eb02012-04-26 23:28:12 +02004825 ret = false;
4826
4827out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02004828 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004829
4830 return ret;
4831}
4832EXPORT_SYMBOL_GPL(i915_gpu_turbo_disable);
4833
4834/**
4835 * Tells the intel_ips driver that the i915 driver is now loaded, if
4836 * IPS got loaded first.
4837 *
4838 * This awkward dance is so that neither module has to depend on the
4839 * other in order for IPS to do the appropriate communication of
4840 * GPU turbo limits to i915.
4841 */
4842static void
4843ips_ping_for_i915_load(void)
4844{
4845 void (*link)(void);
4846
4847 link = symbol_get(ips_link_to_i915_driver);
4848 if (link) {
4849 link();
4850 symbol_put(ips_link_to_i915_driver);
4851 }
4852}
4853
4854void intel_gpu_ips_init(struct drm_i915_private *dev_priv)
4855{
Daniel Vetter02d71952012-08-09 16:44:54 +02004856 /* We only register the i915 ips part with intel-ips once everything is
4857 * set up, to avoid intel-ips sneaking in and reading bogus values. */
Daniel Vetter92703882012-08-09 16:46:01 +02004858 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004859 i915_mch_dev = dev_priv;
Daniel Vetter92703882012-08-09 16:46:01 +02004860 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004861
4862 ips_ping_for_i915_load();
4863}
4864
4865void intel_gpu_ips_teardown(void)
4866{
Daniel Vetter92703882012-08-09 16:46:01 +02004867 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004868 i915_mch_dev = NULL;
Daniel Vetter92703882012-08-09 16:46:01 +02004869 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004870}
Deepak S76c3552f2014-01-30 23:08:16 +05304871
Daniel Vetter8090c6b2012-06-24 16:42:32 +02004872static void intel_init_emon(struct drm_device *dev)
Eugeni Dodonovdde18882012-04-18 15:29:24 -03004873{
4874 struct drm_i915_private *dev_priv = dev->dev_private;
4875 u32 lcfuse;
4876 u8 pxw[16];
4877 int i;
4878
4879 /* Disable to program */
4880 I915_WRITE(ECR, 0);
4881 POSTING_READ(ECR);
4882
4883 /* Program energy weights for various events */
4884 I915_WRITE(SDEW, 0x15040d00);
4885 I915_WRITE(CSIEW0, 0x007f0000);
4886 I915_WRITE(CSIEW1, 0x1e220004);
4887 I915_WRITE(CSIEW2, 0x04000004);
4888
4889 for (i = 0; i < 5; i++)
4890 I915_WRITE(PEW + (i * 4), 0);
4891 for (i = 0; i < 3; i++)
4892 I915_WRITE(DEW + (i * 4), 0);
4893
4894 /* Program P-state weights to account for frequency power adjustment */
4895 for (i = 0; i < 16; i++) {
4896 u32 pxvidfreq = I915_READ(PXVFREQ_BASE + (i * 4));
4897 unsigned long freq = intel_pxfreq(pxvidfreq);
4898 unsigned long vid = (pxvidfreq & PXVFREQ_PX_MASK) >>
4899 PXVFREQ_PX_SHIFT;
4900 unsigned long val;
4901
4902 val = vid * vid;
4903 val *= (freq / 1000);
4904 val *= 255;
4905 val /= (127*127*900);
4906 if (val > 0xff)
4907 DRM_ERROR("bad pxval: %ld\n", val);
4908 pxw[i] = val;
4909 }
4910 /* Render standby states get 0 weight */
4911 pxw[14] = 0;
4912 pxw[15] = 0;
4913
4914 for (i = 0; i < 4; i++) {
4915 u32 val = (pxw[i*4] << 24) | (pxw[(i*4)+1] << 16) |
4916 (pxw[(i*4)+2] << 8) | (pxw[(i*4)+3]);
4917 I915_WRITE(PXW + (i * 4), val);
4918 }
4919
4920 /* Adjust magic regs to magic values (more experimental results) */
4921 I915_WRITE(OGW0, 0);
4922 I915_WRITE(OGW1, 0);
4923 I915_WRITE(EG0, 0x00007f00);
4924 I915_WRITE(EG1, 0x0000000e);
4925 I915_WRITE(EG2, 0x000e0000);
4926 I915_WRITE(EG3, 0x68000300);
4927 I915_WRITE(EG4, 0x42000000);
4928 I915_WRITE(EG5, 0x00140031);
4929 I915_WRITE(EG6, 0);
4930 I915_WRITE(EG7, 0);
4931
4932 for (i = 0; i < 8; i++)
4933 I915_WRITE(PXWL + (i * 4), 0);
4934
4935 /* Enable PMON + select events */
4936 I915_WRITE(ECR, 0x80000019);
4937
4938 lcfuse = I915_READ(LCFUSE02);
4939
Daniel Vetter20e4d402012-08-08 23:35:39 +02004940 dev_priv->ips.corr = (lcfuse & LCFUSE_HIV_MASK);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03004941}
4942
Imre Deakae484342014-03-31 15:10:44 +03004943void intel_init_gt_powersave(struct drm_device *dev)
4944{
Imre Deake6069ca2014-04-18 16:01:02 +03004945 i915.enable_rc6 = sanitize_rc6_option(dev, i915.enable_rc6);
4946
Deepak S38807742014-05-23 21:00:15 +05304947 if (IS_CHERRYVIEW(dev))
4948 cherryview_init_gt_powersave(dev);
4949 else if (IS_VALLEYVIEW(dev))
Imre Deak4e805192014-04-14 20:24:41 +03004950 valleyview_init_gt_powersave(dev);
Imre Deakae484342014-03-31 15:10:44 +03004951}
4952
4953void intel_cleanup_gt_powersave(struct drm_device *dev)
4954{
Deepak S38807742014-05-23 21:00:15 +05304955 if (IS_CHERRYVIEW(dev))
4956 return;
4957 else if (IS_VALLEYVIEW(dev))
Imre Deak4e805192014-04-14 20:24:41 +03004958 valleyview_cleanup_gt_powersave(dev);
Imre Deakae484342014-03-31 15:10:44 +03004959}
4960
Jesse Barnes156c7ca2014-06-12 08:35:45 -07004961/**
4962 * intel_suspend_gt_powersave - suspend PM work and helper threads
4963 * @dev: drm device
4964 *
4965 * We don't want to disable RC6 or other features here, we just want
4966 * to make sure any work we've queued has finished and won't bother
4967 * us while we're suspended.
4968 */
4969void intel_suspend_gt_powersave(struct drm_device *dev)
4970{
4971 struct drm_i915_private *dev_priv = dev->dev_private;
4972
4973 /* Interrupts should be disabled already to avoid re-arming. */
Jesse Barnese11aa362014-06-18 09:52:55 -07004974 WARN_ON(dev->irq_enabled && !dev_priv->pm.irqs_disabled);
Jesse Barnes156c7ca2014-06-12 08:35:45 -07004975
4976 flush_delayed_work(&dev_priv->rps.delayed_resume_work);
4977
4978 cancel_work_sync(&dev_priv->rps.work);
Deepak Sb47adc12014-06-20 20:03:02 +05304979
4980 /* Force GPU to min freq during suspend */
4981 gen6_rps_idle(dev_priv);
Jesse Barnes156c7ca2014-06-12 08:35:45 -07004982}
4983
Daniel Vetter8090c6b2012-06-24 16:42:32 +02004984void intel_disable_gt_powersave(struct drm_device *dev)
4985{
Jesse Barnes1a01ab32012-11-02 11:14:00 -07004986 struct drm_i915_private *dev_priv = dev->dev_private;
4987
Daniel Vetterfd0c0642013-04-24 11:13:35 +02004988 /* Interrupts should be disabled already to avoid re-arming. */
Jesse Barnese11aa362014-06-18 09:52:55 -07004989 WARN_ON(dev->irq_enabled && !dev_priv->pm.irqs_disabled);
Daniel Vetterfd0c0642013-04-24 11:13:35 +02004990
Daniel Vetter930ebb42012-06-29 23:32:16 +02004991 if (IS_IRONLAKE_M(dev)) {
Daniel Vetter8090c6b2012-06-24 16:42:32 +02004992 ironlake_disable_drps(dev);
Daniel Vetter930ebb42012-06-29 23:32:16 +02004993 ironlake_disable_rc6(dev);
Deepak S38807742014-05-23 21:00:15 +05304994 } else if (INTEL_INFO(dev)->gen >= 6) {
Daniel Vetter10d8d362014-06-12 17:48:52 +02004995 intel_suspend_gt_powersave(dev);
Imre Deake4948372014-05-12 18:35:04 +03004996
Jesse Barnes4fc688c2012-11-02 11:14:01 -07004997 mutex_lock(&dev_priv->rps.hw_lock);
Deepak S38807742014-05-23 21:00:15 +05304998 if (IS_CHERRYVIEW(dev))
4999 cherryview_disable_rps(dev);
5000 else if (IS_VALLEYVIEW(dev))
Jesse Barnesd20d4f02013-04-23 10:09:28 -07005001 valleyview_disable_rps(dev);
5002 else
5003 gen6_disable_rps(dev);
Chris Wilsonc0951f02013-10-10 21:58:50 +01005004 dev_priv->rps.enabled = false;
Jesse Barnes4fc688c2012-11-02 11:14:01 -07005005 mutex_unlock(&dev_priv->rps.hw_lock);
Daniel Vetter930ebb42012-06-29 23:32:16 +02005006 }
Daniel Vetter8090c6b2012-06-24 16:42:32 +02005007}
5008
Jesse Barnes1a01ab32012-11-02 11:14:00 -07005009static void intel_gen6_powersave_work(struct work_struct *work)
5010{
5011 struct drm_i915_private *dev_priv =
5012 container_of(work, struct drm_i915_private,
5013 rps.delayed_resume_work.work);
5014 struct drm_device *dev = dev_priv->dev;
5015
Jesse Barnes4fc688c2012-11-02 11:14:01 -07005016 mutex_lock(&dev_priv->rps.hw_lock);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005017
Deepak S38807742014-05-23 21:00:15 +05305018 if (IS_CHERRYVIEW(dev)) {
5019 cherryview_enable_rps(dev);
5020 } else if (IS_VALLEYVIEW(dev)) {
Jesse Barnes0a073b82013-04-17 15:54:58 -07005021 valleyview_enable_rps(dev);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005022 } else if (IS_BROADWELL(dev)) {
5023 gen8_enable_rps(dev);
Imre Deakc2bc2fc2014-04-18 16:16:23 +03005024 __gen6_update_ring_freq(dev);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005025 } else {
5026 gen6_enable_rps(dev);
Imre Deakc2bc2fc2014-04-18 16:16:23 +03005027 __gen6_update_ring_freq(dev);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005028 }
Chris Wilsonc0951f02013-10-10 21:58:50 +01005029 dev_priv->rps.enabled = true;
Jesse Barnes4fc688c2012-11-02 11:14:01 -07005030 mutex_unlock(&dev_priv->rps.hw_lock);
Imre Deakc6df39b2014-04-14 20:24:29 +03005031
5032 intel_runtime_pm_put(dev_priv);
Jesse Barnes1a01ab32012-11-02 11:14:00 -07005033}
5034
Daniel Vetter8090c6b2012-06-24 16:42:32 +02005035void intel_enable_gt_powersave(struct drm_device *dev)
5036{
Jesse Barnes1a01ab32012-11-02 11:14:00 -07005037 struct drm_i915_private *dev_priv = dev->dev_private;
5038
Daniel Vetter8090c6b2012-06-24 16:42:32 +02005039 if (IS_IRONLAKE_M(dev)) {
Imre Deakdc1d0132014-04-14 20:24:28 +03005040 mutex_lock(&dev->struct_mutex);
Daniel Vetter8090c6b2012-06-24 16:42:32 +02005041 ironlake_enable_drps(dev);
5042 ironlake_enable_rc6(dev);
5043 intel_init_emon(dev);
Imre Deakdc1d0132014-04-14 20:24:28 +03005044 mutex_unlock(&dev->struct_mutex);
Deepak S38807742014-05-23 21:00:15 +05305045 } else if (INTEL_INFO(dev)->gen >= 6) {
Jesse Barnes1a01ab32012-11-02 11:14:00 -07005046 /*
5047 * PCU communication is slow and this doesn't need to be
5048 * done at any specific time, so do this out of our fast path
5049 * to make resume and init faster.
Imre Deakc6df39b2014-04-14 20:24:29 +03005050 *
5051 * We depend on the HW RC6 power context save/restore
5052 * mechanism when entering D3 through runtime PM suspend. So
5053 * disable RPM until RPS/RC6 is properly setup. We can only
5054 * get here via the driver load/system resume/runtime resume
5055 * paths, so the _noresume version is enough (and in case of
5056 * runtime resume it's necessary).
Jesse Barnes1a01ab32012-11-02 11:14:00 -07005057 */
Imre Deakc6df39b2014-04-14 20:24:29 +03005058 if (schedule_delayed_work(&dev_priv->rps.delayed_resume_work,
5059 round_jiffies_up_relative(HZ)))
5060 intel_runtime_pm_get_noresume(dev_priv);
Daniel Vetter8090c6b2012-06-24 16:42:32 +02005061 }
5062}
5063
Imre Deakc6df39b2014-04-14 20:24:29 +03005064void intel_reset_gt_powersave(struct drm_device *dev)
5065{
5066 struct drm_i915_private *dev_priv = dev->dev_private;
5067
5068 dev_priv->rps.enabled = false;
5069 intel_enable_gt_powersave(dev);
5070}
5071
Daniel Vetter3107bd42012-10-31 22:52:31 +01005072static void ibx_init_clock_gating(struct drm_device *dev)
5073{
5074 struct drm_i915_private *dev_priv = dev->dev_private;
5075
5076 /*
5077 * On Ibex Peak and Cougar Point, we need to disable clock
5078 * gating for the panel power sequencer or it will fail to
5079 * start up when no ports are active.
5080 */
5081 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE);
5082}
5083
Ville Syrjälä0e088b82013-06-07 10:47:04 +03005084static void g4x_disable_trickle_feed(struct drm_device *dev)
5085{
5086 struct drm_i915_private *dev_priv = dev->dev_private;
5087 int pipe;
5088
5089 for_each_pipe(pipe) {
5090 I915_WRITE(DSPCNTR(pipe),
5091 I915_READ(DSPCNTR(pipe)) |
5092 DISPPLANE_TRICKLE_FEED_DISABLE);
Ville Syrjälä1dba99f2013-10-01 18:02:18 +03005093 intel_flush_primary_plane(dev_priv, pipe);
Ville Syrjälä0e088b82013-06-07 10:47:04 +03005094 }
5095}
5096
Ville Syrjälä017636c2013-12-05 15:51:37 +02005097static void ilk_init_lp_watermarks(struct drm_device *dev)
5098{
5099 struct drm_i915_private *dev_priv = dev->dev_private;
5100
5101 I915_WRITE(WM3_LP_ILK, I915_READ(WM3_LP_ILK) & ~WM1_LP_SR_EN);
5102 I915_WRITE(WM2_LP_ILK, I915_READ(WM2_LP_ILK) & ~WM1_LP_SR_EN);
5103 I915_WRITE(WM1_LP_ILK, I915_READ(WM1_LP_ILK) & ~WM1_LP_SR_EN);
5104
5105 /*
5106 * Don't touch WM1S_LP_EN here.
5107 * Doing so could cause underruns.
5108 */
5109}
5110
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005111static void ironlake_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005112{
5113 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau231e54f2012-10-19 17:55:41 +01005114 uint32_t dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005115
Damien Lespiauf1e8fa52013-06-07 17:41:09 +01005116 /*
5117 * Required for FBC
5118 * WaFbcDisableDpfcClockGating:ilk
5119 */
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01005120 dspclk_gate |= ILK_DPFCRUNIT_CLOCK_GATE_DISABLE |
5121 ILK_DPFCUNIT_CLOCK_GATE_DISABLE |
5122 ILK_DPFDUNIT_CLOCK_GATE_ENABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005123
5124 I915_WRITE(PCH_3DCGDIS0,
5125 MARIUNIT_CLOCK_GATE_DISABLE |
5126 SVSMUNIT_CLOCK_GATE_DISABLE);
5127 I915_WRITE(PCH_3DCGDIS1,
5128 VFMUNIT_CLOCK_GATE_DISABLE);
5129
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005130 /*
5131 * According to the spec the following bits should be set in
5132 * order to enable memory self-refresh
5133 * The bit 22/21 of 0x42004
5134 * The bit 5 of 0x42020
5135 * The bit 15 of 0x45000
5136 */
5137 I915_WRITE(ILK_DISPLAY_CHICKEN2,
5138 (I915_READ(ILK_DISPLAY_CHICKEN2) |
5139 ILK_DPARB_GATE | ILK_VSDPFD_FULL));
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01005140 dspclk_gate |= ILK_DPARBUNIT_CLOCK_GATE_ENABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005141 I915_WRITE(DISP_ARB_CTL,
5142 (I915_READ(DISP_ARB_CTL) |
5143 DISP_FBC_WM_DIS));
Ville Syrjälä017636c2013-12-05 15:51:37 +02005144
5145 ilk_init_lp_watermarks(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005146
5147 /*
5148 * Based on the document from hardware guys the following bits
5149 * should be set unconditionally in order to enable FBC.
5150 * The bit 22 of 0x42000
5151 * The bit 22 of 0x42004
5152 * The bit 7,8,9 of 0x42020.
5153 */
5154 if (IS_IRONLAKE_M(dev)) {
Damien Lespiau4bb35332013-06-14 15:23:24 +01005155 /* WaFbcAsynchFlipDisableFbcQueue:ilk */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005156 I915_WRITE(ILK_DISPLAY_CHICKEN1,
5157 I915_READ(ILK_DISPLAY_CHICKEN1) |
5158 ILK_FBCQ_DIS);
5159 I915_WRITE(ILK_DISPLAY_CHICKEN2,
5160 I915_READ(ILK_DISPLAY_CHICKEN2) |
5161 ILK_DPARB_GATE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005162 }
5163
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01005164 I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
5165
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005166 I915_WRITE(ILK_DISPLAY_CHICKEN2,
5167 I915_READ(ILK_DISPLAY_CHICKEN2) |
5168 ILK_ELPIN_409_SELECT);
5169 I915_WRITE(_3D_CHICKEN2,
5170 _3D_CHICKEN2_WM_READ_PIPELINED << 16 |
5171 _3D_CHICKEN2_WM_READ_PIPELINED);
Daniel Vetter4358a372012-10-18 11:49:51 +02005172
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005173 /* WaDisableRenderCachePipelinedFlush:ilk */
Daniel Vetter4358a372012-10-18 11:49:51 +02005174 I915_WRITE(CACHE_MODE_0,
5175 _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
Daniel Vetter3107bd42012-10-31 22:52:31 +01005176
Akash Goel4e046322014-04-04 17:14:38 +05305177 /* WaDisable_RenderCache_OperationalFlush:ilk */
5178 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
5179
Ville Syrjälä0e088b82013-06-07 10:47:04 +03005180 g4x_disable_trickle_feed(dev);
Ville Syrjäläbdad2b22013-06-07 10:47:03 +03005181
Daniel Vetter3107bd42012-10-31 22:52:31 +01005182 ibx_init_clock_gating(dev);
5183}
5184
5185static void cpt_init_clock_gating(struct drm_device *dev)
5186{
5187 struct drm_i915_private *dev_priv = dev->dev_private;
5188 int pipe;
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03005189 uint32_t val;
Daniel Vetter3107bd42012-10-31 22:52:31 +01005190
5191 /*
5192 * On Ibex Peak and Cougar Point, we need to disable clock
5193 * gating for the panel power sequencer or it will fail to
5194 * start up when no ports are active.
5195 */
Jesse Barnescd664072013-10-02 10:34:19 -07005196 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE |
5197 PCH_DPLUNIT_CLOCK_GATE_DISABLE |
5198 PCH_CPUNIT_CLOCK_GATE_DISABLE);
Daniel Vetter3107bd42012-10-31 22:52:31 +01005199 I915_WRITE(SOUTH_CHICKEN2, I915_READ(SOUTH_CHICKEN2) |
5200 DPLS_EDP_PPS_FIX_DIS);
Takashi Iwai335c07b2012-12-11 11:46:29 +01005201 /* The below fixes the weird display corruption, a few pixels shifted
5202 * downward, on (only) LVDS of some HP laptops with IVY.
5203 */
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03005204 for_each_pipe(pipe) {
Paulo Zanonidc4bd2d2013-04-08 15:48:08 -03005205 val = I915_READ(TRANS_CHICKEN2(pipe));
5206 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
5207 val &= ~TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03005208 if (dev_priv->vbt.fdi_rx_polarity_inverted)
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03005209 val |= TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
Paulo Zanonidc4bd2d2013-04-08 15:48:08 -03005210 val &= ~TRANS_CHICKEN2_FRAME_START_DELAY_MASK;
5211 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_COUNTER;
5212 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_MODESWITCH;
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03005213 I915_WRITE(TRANS_CHICKEN2(pipe), val);
5214 }
Daniel Vetter3107bd42012-10-31 22:52:31 +01005215 /* WADP0ClockGatingDisable */
5216 for_each_pipe(pipe) {
5217 I915_WRITE(TRANS_CHICKEN1(pipe),
5218 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
5219 }
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005220}
5221
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01005222static void gen6_check_mch_setup(struct drm_device *dev)
5223{
5224 struct drm_i915_private *dev_priv = dev->dev_private;
5225 uint32_t tmp;
5226
5227 tmp = I915_READ(MCH_SSKPD);
5228 if ((tmp & MCH_SSKPD_WM0_MASK) != MCH_SSKPD_WM0_VAL) {
5229 DRM_INFO("Wrong MCH_SSKPD value: 0x%08x\n", tmp);
5230 DRM_INFO("This can cause pipe underruns and display issues.\n");
5231 DRM_INFO("Please upgrade your BIOS to fix this.\n");
5232 }
5233}
5234
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005235static void gen6_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005236{
5237 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau231e54f2012-10-19 17:55:41 +01005238 uint32_t dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005239
Damien Lespiau231e54f2012-10-19 17:55:41 +01005240 I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005241
5242 I915_WRITE(ILK_DISPLAY_CHICKEN2,
5243 I915_READ(ILK_DISPLAY_CHICKEN2) |
5244 ILK_ELPIN_409_SELECT);
5245
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005246 /* WaDisableHiZPlanesWhenMSAAEnabled:snb */
Daniel Vetter42839082012-12-14 23:38:28 +01005247 I915_WRITE(_3D_CHICKEN,
5248 _MASKED_BIT_ENABLE(_3D_CHICKEN_HIZ_PLANE_DISABLE_MSAA_4X_SNB));
5249
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005250 /* WaSetupGtModeTdRowDispatch:snb */
Daniel Vetter6547fbd2012-12-14 23:38:29 +01005251 if (IS_SNB_GT1(dev))
5252 I915_WRITE(GEN6_GT_MODE,
5253 _MASKED_BIT_ENABLE(GEN6_TD_FOUR_ROW_DISPATCH_DISABLE));
5254
Akash Goel4e046322014-04-04 17:14:38 +05305255 /* WaDisable_RenderCache_OperationalFlush:snb */
5256 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
5257
Ville Syrjälä8d85d272014-02-04 21:59:15 +02005258 /*
5259 * BSpec recoomends 8x4 when MSAA is used,
5260 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02005261 *
5262 * Note that PS/WM thread counts depend on the WIZ hashing
5263 * disable bit, which we don't touch here, but it's good
5264 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjälä8d85d272014-02-04 21:59:15 +02005265 */
5266 I915_WRITE(GEN6_GT_MODE,
5267 GEN6_WIZ_HASHING_MASK | GEN6_WIZ_HASHING_16x4);
5268
Ville Syrjälä017636c2013-12-05 15:51:37 +02005269 ilk_init_lp_watermarks(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005270
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005271 I915_WRITE(CACHE_MODE_0,
Daniel Vetter50743292012-04-26 22:02:54 +02005272 _MASKED_BIT_DISABLE(CM0_STC_EVICT_DISABLE_LRA_SNB));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005273
5274 I915_WRITE(GEN6_UCGCTL1,
5275 I915_READ(GEN6_UCGCTL1) |
5276 GEN6_BLBUNIT_CLOCK_GATE_DISABLE |
5277 GEN6_CSUNIT_CLOCK_GATE_DISABLE);
5278
5279 /* According to the BSpec vol1g, bit 12 (RCPBUNIT) clock
5280 * gating disable must be set. Failure to set it results in
5281 * flickering pixels due to Z write ordering failures after
5282 * some amount of runtime in the Mesa "fire" demo, and Unigine
5283 * Sanctuary and Tropics, and apparently anything else with
5284 * alpha test or pixel discard.
5285 *
5286 * According to the spec, bit 11 (RCCUNIT) must also be set,
5287 * but we didn't debug actual testcases to find it out.
Jesse Barnes0f846f82012-06-14 11:04:47 -07005288 *
Ville Syrjäläef593182014-01-22 21:32:47 +02005289 * WaDisableRCCUnitClockGating:snb
5290 * WaDisableRCPBUnitClockGating:snb
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005291 */
5292 I915_WRITE(GEN6_UCGCTL2,
5293 GEN6_RCPBUNIT_CLOCK_GATE_DISABLE |
5294 GEN6_RCCUNIT_CLOCK_GATE_DISABLE);
5295
Ville Syrjälä5eb146d2014-02-04 21:59:16 +02005296 /* WaStripsFansDisableFastClipPerformanceFix:snb */
Ville Syrjälä743b57d2014-02-04 21:59:17 +02005297 I915_WRITE(_3D_CHICKEN3,
5298 _MASKED_BIT_ENABLE(_3D_CHICKEN3_SF_DISABLE_FASTCLIP_CULL));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005299
5300 /*
Ville Syrjäläe927ecd2014-02-04 21:59:18 +02005301 * Bspec says:
5302 * "This bit must be set if 3DSTATE_CLIP clip mode is set to normal and
5303 * 3DSTATE_SF number of SF output attributes is more than 16."
5304 */
5305 I915_WRITE(_3D_CHICKEN3,
5306 _MASKED_BIT_ENABLE(_3D_CHICKEN3_SF_DISABLE_PIPELINED_ATTR_FETCH));
5307
5308 /*
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005309 * According to the spec the following bits should be
5310 * set in order to enable memory self-refresh and fbc:
5311 * The bit21 and bit22 of 0x42000
5312 * The bit21 and bit22 of 0x42004
5313 * The bit5 and bit7 of 0x42020
5314 * The bit14 of 0x70180
5315 * The bit14 of 0x71180
Damien Lespiau4bb35332013-06-14 15:23:24 +01005316 *
5317 * WaFbcAsynchFlipDisableFbcQueue:snb
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005318 */
5319 I915_WRITE(ILK_DISPLAY_CHICKEN1,
5320 I915_READ(ILK_DISPLAY_CHICKEN1) |
5321 ILK_FBCQ_DIS | ILK_PABSTRETCH_DIS);
5322 I915_WRITE(ILK_DISPLAY_CHICKEN2,
5323 I915_READ(ILK_DISPLAY_CHICKEN2) |
5324 ILK_DPARB_GATE | ILK_VSDPFD_FULL);
Damien Lespiau231e54f2012-10-19 17:55:41 +01005325 I915_WRITE(ILK_DSPCLK_GATE_D,
5326 I915_READ(ILK_DSPCLK_GATE_D) |
5327 ILK_DPARBUNIT_CLOCK_GATE_ENABLE |
5328 ILK_DPFDUNIT_CLOCK_GATE_ENABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005329
Ville Syrjälä0e088b82013-06-07 10:47:04 +03005330 g4x_disable_trickle_feed(dev);
Ben Widawskyf8f2ac92012-10-03 19:34:24 -07005331
Daniel Vetter3107bd42012-10-31 22:52:31 +01005332 cpt_init_clock_gating(dev);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01005333
5334 gen6_check_mch_setup(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005335}
5336
5337static void gen7_setup_fixed_func_scheduler(struct drm_i915_private *dev_priv)
5338{
5339 uint32_t reg = I915_READ(GEN7_FF_THREAD_MODE);
5340
Ville Syrjälä3aad9052014-01-22 21:32:59 +02005341 /*
Ville Syrjälä46680e02014-01-22 21:33:01 +02005342 * WaVSThreadDispatchOverride:ivb,vlv
Ville Syrjälä3aad9052014-01-22 21:32:59 +02005343 *
5344 * This actually overrides the dispatch
5345 * mode for all thread types.
5346 */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005347 reg &= ~GEN7_FF_SCHED_MASK;
5348 reg |= GEN7_FF_TS_SCHED_HW;
5349 reg |= GEN7_FF_VS_SCHED_HW;
5350 reg |= GEN7_FF_DS_SCHED_HW;
5351
5352 I915_WRITE(GEN7_FF_THREAD_MODE, reg);
5353}
5354
Paulo Zanoni17a303e2012-11-20 15:12:07 -02005355static void lpt_init_clock_gating(struct drm_device *dev)
5356{
5357 struct drm_i915_private *dev_priv = dev->dev_private;
5358
5359 /*
5360 * TODO: this bit should only be enabled when really needed, then
5361 * disabled when not needed anymore in order to save power.
5362 */
5363 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE)
5364 I915_WRITE(SOUTH_DSPCLK_GATE_D,
5365 I915_READ(SOUTH_DSPCLK_GATE_D) |
5366 PCH_LP_PARTITION_LEVEL_DISABLE);
Paulo Zanoni0a790cd2013-04-17 18:15:49 -03005367
5368 /* WADPOClockGatingDisable:hsw */
5369 I915_WRITE(_TRANSA_CHICKEN1,
5370 I915_READ(_TRANSA_CHICKEN1) |
5371 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
Paulo Zanoni17a303e2012-11-20 15:12:07 -02005372}
5373
Imre Deak7d708ee2013-04-17 14:04:50 +03005374static void lpt_suspend_hw(struct drm_device *dev)
5375{
5376 struct drm_i915_private *dev_priv = dev->dev_private;
5377
5378 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
5379 uint32_t val = I915_READ(SOUTH_DSPCLK_GATE_D);
5380
5381 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
5382 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
5383 }
5384}
5385
Ben Widawsky1020a5c2013-11-02 21:07:06 -07005386static void gen8_init_clock_gating(struct drm_device *dev)
5387{
5388 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau07d27e22014-03-03 17:31:46 +00005389 enum pipe pipe;
Ben Widawsky1020a5c2013-11-02 21:07:06 -07005390
5391 I915_WRITE(WM3_LP_ILK, 0);
5392 I915_WRITE(WM2_LP_ILK, 0);
5393 I915_WRITE(WM1_LP_ILK, 0);
Ben Widawsky50ed5fb2013-11-02 21:07:40 -07005394
5395 /* FIXME(BDW): Check all the w/a, some might only apply to
5396 * pre-production hw. */
5397
Kenneth Graunkec8966e12014-02-26 23:59:30 -08005398 /* WaDisablePartialInstShootdown:bdw */
5399 I915_WRITE(GEN8_ROW_CHICKEN,
5400 _MASKED_BIT_ENABLE(PARTIAL_INSTRUCTION_SHOOTDOWN_DISABLE));
5401
Kenneth Graunke1411e6a2014-02-26 23:59:31 -08005402 /* WaDisableThreadStallDopClockGating:bdw */
5403 /* FIXME: Unclear whether we really need this on production bdw. */
5404 I915_WRITE(GEN8_ROW_CHICKEN,
5405 _MASKED_BIT_ENABLE(STALL_DOP_GATING_DISABLE));
5406
Damien Lespiau4167e322014-01-16 16:51:35 +00005407 /*
5408 * This GEN8_CENTROID_PIXEL_OPT_DIS W/A is only needed for
5409 * pre-production hardware
5410 */
Ben Widawskyfd392b62013-11-04 22:52:39 -08005411 I915_WRITE(HALF_SLICE_CHICKEN3,
5412 _MASKED_BIT_ENABLE(GEN8_CENTROID_PIXEL_OPT_DIS));
Ben Widawskybf663472013-11-02 21:07:57 -07005413 I915_WRITE(HALF_SLICE_CHICKEN3,
5414 _MASKED_BIT_ENABLE(GEN8_SAMPLER_POWER_BYPASS_DIS));
Ben Widawsky4afe8d32013-11-02 21:07:55 -07005415 I915_WRITE(GAMTARBMODE, _MASKED_BIT_ENABLE(ARB_MODE_BWGTLB_DISABLE));
5416
Ben Widawsky7f88da02013-11-02 21:07:58 -07005417 I915_WRITE(_3D_CHICKEN3,
Michel Thierryb3f9ad92014-07-07 12:40:17 +01005418 _MASKED_BIT_ENABLE(_3D_CHICKEN_SDE_LIMIT_FIFO_POLY_DEPTH(2)));
Ben Widawsky7f88da02013-11-02 21:07:58 -07005419
Ben Widawskya75f3622013-11-02 21:07:59 -07005420 I915_WRITE(COMMON_SLICE_CHICKEN2,
5421 _MASKED_BIT_ENABLE(GEN8_CSC2_SBE_VUE_CACHE_CONSERVATIVE));
5422
Ben Widawsky4c2e7a52013-11-02 21:08:00 -07005423 I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
5424 _MASKED_BIT_ENABLE(GEN7_SINGLE_SUBSCAN_DISPATCH_ENABLE));
5425
Ben Widawsky242a4012014-04-18 18:04:29 -03005426 /* WaDisableDopClockGating:bdw May not be needed for production */
5427 I915_WRITE(GEN7_ROW_CHICKEN2,
5428 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
5429
Ben Widawskyab57fff2013-12-12 15:28:04 -08005430 /* WaSwitchSolVfFArbitrationPriority:bdw */
Ben Widawsky50ed5fb2013-11-02 21:07:40 -07005431 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07005432
Ben Widawskyab57fff2013-12-12 15:28:04 -08005433 /* WaPsrDPAMaskVBlankInSRD:bdw */
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07005434 I915_WRITE(CHICKEN_PAR1_1,
5435 I915_READ(CHICKEN_PAR1_1) | DPA_MASK_VBLANK_SRD);
5436
Ben Widawskyab57fff2013-12-12 15:28:04 -08005437 /* WaPsrDPRSUnmaskVBlankInSRD:bdw */
Damien Lespiau07d27e22014-03-03 17:31:46 +00005438 for_each_pipe(pipe) {
5439 I915_WRITE(CHICKEN_PIPESL_1(pipe),
Ville Syrjäläc7c65622014-03-05 13:05:45 +02005440 I915_READ(CHICKEN_PIPESL_1(pipe)) |
Ville Syrjälä8f670bb2014-03-05 13:05:47 +02005441 BDW_DPRS_MASK_VBLANK_SRD);
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07005442 }
Ben Widawsky63801f22013-12-12 17:26:03 -08005443
5444 /* Use Force Non-Coherent whenever executing a 3D context. This is a
5445 * workaround for for a possible hang in the unlikely event a TLB
5446 * invalidation occurs during a PSD flush.
5447 */
5448 I915_WRITE(HDC_CHICKEN0,
5449 I915_READ(HDC_CHICKEN0) |
5450 _MASKED_BIT_ENABLE(HDC_FORCE_NON_COHERENT));
Ben Widawskyab57fff2013-12-12 15:28:04 -08005451
5452 /* WaVSRefCountFullforceMissDisable:bdw */
5453 /* WaDSRefCountFullforceMissDisable:bdw */
5454 I915_WRITE(GEN7_FF_THREAD_MODE,
5455 I915_READ(GEN7_FF_THREAD_MODE) &
5456 ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
Ville Syrjälä36075a42014-02-04 21:59:21 +02005457
5458 /*
5459 * BSpec recommends 8x4 when MSAA is used,
5460 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02005461 *
5462 * Note that PS/WM thread counts depend on the WIZ hashing
5463 * disable bit, which we don't touch here, but it's good
5464 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjälä36075a42014-02-04 21:59:21 +02005465 */
5466 I915_WRITE(GEN7_GT_MODE,
5467 GEN6_WIZ_HASHING_MASK | GEN6_WIZ_HASHING_16x4);
Ville Syrjälä295e8bb2014-02-27 21:59:01 +02005468
5469 I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
5470 _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
Ville Syrjälä4f1ca9e2014-02-27 21:59:02 +02005471
5472 /* WaDisableSDEUnitClockGating:bdw */
5473 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
5474 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Damien Lespiau5d708682014-03-26 18:41:51 +00005475
5476 /* Wa4x4STCOptimizationDisable:bdw */
5477 I915_WRITE(CACHE_MODE_1,
5478 _MASKED_BIT_ENABLE(GEN8_4x4_STC_OPTIMIZATION_DISABLE));
Ben Widawsky1020a5c2013-11-02 21:07:06 -07005479}
5480
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03005481static void haswell_init_clock_gating(struct drm_device *dev)
5482{
5483 struct drm_i915_private *dev_priv = dev->dev_private;
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03005484
Ville Syrjälä017636c2013-12-05 15:51:37 +02005485 ilk_init_lp_watermarks(dev);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03005486
Francisco Jerezf3fc4882013-10-02 15:53:16 -07005487 /* L3 caching of data atomics doesn't work -- disable it. */
5488 I915_WRITE(HSW_SCRATCH1, HSW_SCRATCH1_L3_DATA_ATOMICS_DISABLE);
5489 I915_WRITE(HSW_ROW_CHICKEN3,
5490 _MASKED_BIT_ENABLE(HSW_ROW_CHICKEN3_L3_GLOBAL_ATOMICS_DISABLE));
5491
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005492 /* This is required by WaCatErrorRejectionIssue:hsw */
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03005493 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
5494 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
5495 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
5496
Ville Syrjäläe36ea7f2014-01-22 21:33:00 +02005497 /* WaVSRefCountFullforceMissDisable:hsw */
5498 I915_WRITE(GEN7_FF_THREAD_MODE,
5499 I915_READ(GEN7_FF_THREAD_MODE) & ~GEN7_FF_VS_REF_CNT_FFME);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03005500
Akash Goel4e046322014-04-04 17:14:38 +05305501 /* WaDisable_RenderCache_OperationalFlush:hsw */
5502 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
5503
Chia-I Wufe27c602014-01-28 13:29:33 +08005504 /* enable HiZ Raw Stall Optimization */
5505 I915_WRITE(CACHE_MODE_0_GEN7,
5506 _MASKED_BIT_DISABLE(HIZ_RAW_STALL_OPT_DISABLE));
5507
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005508 /* WaDisable4x2SubspanOptimization:hsw */
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03005509 I915_WRITE(CACHE_MODE_1,
5510 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Eugeni Dodonov1544d9d2012-07-02 11:51:10 -03005511
Ville Syrjäläa12c4962014-02-04 21:59:20 +02005512 /*
5513 * BSpec recommends 8x4 when MSAA is used,
5514 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02005515 *
5516 * Note that PS/WM thread counts depend on the WIZ hashing
5517 * disable bit, which we don't touch here, but it's good
5518 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjäläa12c4962014-02-04 21:59:20 +02005519 */
5520 I915_WRITE(GEN7_GT_MODE,
5521 GEN6_WIZ_HASHING_MASK | GEN6_WIZ_HASHING_16x4);
5522
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005523 /* WaSwitchSolVfFArbitrationPriority:hsw */
Ben Widawskye3dff582013-03-20 14:49:14 -07005524 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
5525
Paulo Zanoni90a88642013-05-03 17:23:45 -03005526 /* WaRsPkgCStateDisplayPMReq:hsw */
5527 I915_WRITE(CHICKEN_PAR1_1,
5528 I915_READ(CHICKEN_PAR1_1) | FORCE_ARB_IDLE_PLANES);
Eugeni Dodonov1544d9d2012-07-02 11:51:10 -03005529
Paulo Zanoni17a303e2012-11-20 15:12:07 -02005530 lpt_init_clock_gating(dev);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03005531}
5532
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005533static void ivybridge_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005534{
5535 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawsky20848222012-05-04 18:58:59 -07005536 uint32_t snpcr;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005537
Ville Syrjälä017636c2013-12-05 15:51:37 +02005538 ilk_init_lp_watermarks(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005539
Damien Lespiau231e54f2012-10-19 17:55:41 +01005540 I915_WRITE(ILK_DSPCLK_GATE_D, ILK_VRHUNIT_CLOCK_GATE_DISABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005541
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005542 /* WaDisableEarlyCull:ivb */
Jesse Barnes87f80202012-10-02 17:43:41 -05005543 I915_WRITE(_3D_CHICKEN3,
5544 _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
5545
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005546 /* WaDisableBackToBackFlipFix:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005547 I915_WRITE(IVB_CHICKEN3,
5548 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
5549 CHICKEN3_DGMG_DONE_FIX_DISABLE);
5550
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005551 /* WaDisablePSDDualDispatchEnable:ivb */
Jesse Barnes12f33822012-10-25 12:15:45 -07005552 if (IS_IVB_GT1(dev))
5553 I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
5554 _MASKED_BIT_ENABLE(GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
Jesse Barnes12f33822012-10-25 12:15:45 -07005555
Akash Goel4e046322014-04-04 17:14:38 +05305556 /* WaDisable_RenderCache_OperationalFlush:ivb */
5557 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
5558
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005559 /* Apply the WaDisableRHWOOptimizationForRenderHang:ivb workaround. */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005560 I915_WRITE(GEN7_COMMON_SLICE_CHICKEN1,
5561 GEN7_CSC1_RHWO_OPT_DISABLE_IN_RCC);
5562
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005563 /* WaApplyL3ControlAndL3ChickenMode:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005564 I915_WRITE(GEN7_L3CNTLREG1,
5565 GEN7_WA_FOR_GEN7_L3_CONTROL);
5566 I915_WRITE(GEN7_L3_CHICKEN_MODE_REGISTER,
Jesse Barnes8ab43972012-10-25 12:15:42 -07005567 GEN7_WA_L3_CHICKEN_MODE);
5568 if (IS_IVB_GT1(dev))
5569 I915_WRITE(GEN7_ROW_CHICKEN2,
5570 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Ville Syrjälä412236c2014-01-22 21:32:44 +02005571 else {
5572 /* must write both registers */
5573 I915_WRITE(GEN7_ROW_CHICKEN2,
5574 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Jesse Barnes8ab43972012-10-25 12:15:42 -07005575 I915_WRITE(GEN7_ROW_CHICKEN2_GT2,
5576 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Ville Syrjälä412236c2014-01-22 21:32:44 +02005577 }
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005578
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005579 /* WaForceL3Serialization:ivb */
Jesse Barnes61939d92012-10-02 17:43:38 -05005580 I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
5581 ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
5582
Ville Syrjälä1b80a19a2014-01-22 21:32:53 +02005583 /*
Jesse Barnes0f846f82012-06-14 11:04:47 -07005584 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005585 * This implements the WaDisableRCZUnitClockGating:ivb workaround.
Jesse Barnes0f846f82012-06-14 11:04:47 -07005586 */
5587 I915_WRITE(GEN6_UCGCTL2,
Ville Syrjälä28acf3b2014-01-22 21:32:48 +02005588 GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
Jesse Barnes0f846f82012-06-14 11:04:47 -07005589
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005590 /* This is required by WaCatErrorRejectionIssue:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005591 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
5592 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
5593 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
5594
Ville Syrjälä0e088b82013-06-07 10:47:04 +03005595 g4x_disable_trickle_feed(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005596
5597 gen7_setup_fixed_func_scheduler(dev_priv);
Daniel Vetter97e19302012-04-24 16:00:21 +02005598
Chris Wilson22721342014-03-04 09:41:43 +00005599 if (0) { /* causes HiZ corruption on ivb:gt1 */
5600 /* enable HiZ Raw Stall Optimization */
5601 I915_WRITE(CACHE_MODE_0_GEN7,
5602 _MASKED_BIT_DISABLE(HIZ_RAW_STALL_OPT_DISABLE));
5603 }
Chia-I Wu116f2b62014-01-28 13:29:34 +08005604
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005605 /* WaDisable4x2SubspanOptimization:ivb */
Daniel Vetter97e19302012-04-24 16:00:21 +02005606 I915_WRITE(CACHE_MODE_1,
5607 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Ben Widawsky20848222012-05-04 18:58:59 -07005608
Ville Syrjäläa607c1a2014-02-04 21:59:19 +02005609 /*
5610 * BSpec recommends 8x4 when MSAA is used,
5611 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02005612 *
5613 * Note that PS/WM thread counts depend on the WIZ hashing
5614 * disable bit, which we don't touch here, but it's good
5615 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjäläa607c1a2014-02-04 21:59:19 +02005616 */
5617 I915_WRITE(GEN7_GT_MODE,
5618 GEN6_WIZ_HASHING_MASK | GEN6_WIZ_HASHING_16x4);
5619
Ben Widawsky20848222012-05-04 18:58:59 -07005620 snpcr = I915_READ(GEN6_MBCUNIT_SNPCR);
5621 snpcr &= ~GEN6_MBC_SNPCR_MASK;
5622 snpcr |= GEN6_MBC_SNPCR_MED;
5623 I915_WRITE(GEN6_MBCUNIT_SNPCR, snpcr);
Daniel Vetter3107bd42012-10-31 22:52:31 +01005624
Ben Widawskyab5c6082013-04-05 13:12:41 -07005625 if (!HAS_PCH_NOP(dev))
5626 cpt_init_clock_gating(dev);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01005627
5628 gen6_check_mch_setup(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005629}
5630
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005631static void valleyview_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005632{
5633 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes85b1d7b2013-11-04 11:52:45 -08005634 u32 val;
5635
5636 mutex_lock(&dev_priv->rps.hw_lock);
5637 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
5638 mutex_unlock(&dev_priv->rps.hw_lock);
5639 switch ((val >> 6) & 3) {
5640 case 0:
Deepak Sf6d51942014-04-03 21:01:28 +05305641 case 1:
Jesse Barnes85b1d7b2013-11-04 11:52:45 -08005642 dev_priv->mem_freq = 800;
5643 break;
Jesse Barnesf64a28a2013-11-04 16:07:00 -08005644 case 2:
Deepak Sf6d51942014-04-03 21:01:28 +05305645 dev_priv->mem_freq = 1066;
Jesse Barnes85b1d7b2013-11-04 11:52:45 -08005646 break;
Jesse Barnesf64a28a2013-11-04 16:07:00 -08005647 case 3:
Chon Ming Lee23259912013-11-07 15:23:26 +08005648 dev_priv->mem_freq = 1333;
Jesse Barnesf64a28a2013-11-04 16:07:00 -08005649 break;
Jesse Barnes85b1d7b2013-11-04 11:52:45 -08005650 }
5651 DRM_DEBUG_DRIVER("DDR speed: %d MHz", dev_priv->mem_freq);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005652
Ville Syrjäläd7fe0cc2013-05-21 18:01:50 +03005653 I915_WRITE(DSPCLK_GATE_D, VRHUNIT_CLOCK_GATE_DISABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005654
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005655 /* WaDisableEarlyCull:vlv */
Jesse Barnes87f80202012-10-02 17:43:41 -05005656 I915_WRITE(_3D_CHICKEN3,
5657 _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
5658
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005659 /* WaDisableBackToBackFlipFix:vlv */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005660 I915_WRITE(IVB_CHICKEN3,
5661 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
5662 CHICKEN3_DGMG_DONE_FIX_DISABLE);
5663
Ville Syrjäläfad7d362014-01-22 21:32:39 +02005664 /* WaPsdDispatchEnable:vlv */
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005665 /* WaDisablePSDDualDispatchEnable:vlv */
Jesse Barnes12f33822012-10-25 12:15:45 -07005666 I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
Jesse Barnesd3bc0302013-03-08 10:45:51 -08005667 _MASKED_BIT_ENABLE(GEN7_MAX_PS_THREAD_DEP |
5668 GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
Jesse Barnes12f33822012-10-25 12:15:45 -07005669
Akash Goel4e046322014-04-04 17:14:38 +05305670 /* WaDisable_RenderCache_OperationalFlush:vlv */
5671 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
5672
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005673 /* WaForceL3Serialization:vlv */
Jesse Barnes61939d92012-10-02 17:43:38 -05005674 I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
5675 ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
5676
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005677 /* WaDisableDopClockGating:vlv */
Jesse Barnes8ab43972012-10-25 12:15:42 -07005678 I915_WRITE(GEN7_ROW_CHICKEN2,
5679 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
5680
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005681 /* This is required by WaCatErrorRejectionIssue:vlv */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005682 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
5683 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
5684 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
5685
Ville Syrjälä46680e02014-01-22 21:33:01 +02005686 gen7_setup_fixed_func_scheduler(dev_priv);
5687
Ville Syrjälä3c0edae2014-01-22 21:32:56 +02005688 /*
Jesse Barnes0f846f82012-06-14 11:04:47 -07005689 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005690 * This implements the WaDisableRCZUnitClockGating:vlv workaround.
Jesse Barnes0f846f82012-06-14 11:04:47 -07005691 */
5692 I915_WRITE(GEN6_UCGCTL2,
Ville Syrjälä3c0edae2014-01-22 21:32:56 +02005693 GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
Jesse Barnes0f846f82012-06-14 11:04:47 -07005694
Akash Goelc98f5062014-03-24 23:00:07 +05305695 /* WaDisableL3Bank2xClockGate:vlv
5696 * Disabling L3 clock gating- MMIO 940c[25] = 1
5697 * Set bit 25, to disable L3_BANK_2x_CLK_GATING */
5698 I915_WRITE(GEN7_UCGCTL4,
5699 I915_READ(GEN7_UCGCTL4) | GEN7_L3BANK2X_CLOCK_GATE_DISABLE);
Jesse Barnese3f33d42012-06-14 11:04:50 -07005700
Ville Syrjäläe0d8d592013-06-12 22:11:18 +03005701 I915_WRITE(MI_ARB_VLV, MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005702
Ville Syrjäläafd58e72014-01-22 21:33:03 +02005703 /*
5704 * BSpec says this must be set, even though
5705 * WaDisable4x2SubspanOptimization isn't listed for VLV.
5706 */
Daniel Vetter6b26c862012-04-24 14:04:12 +02005707 I915_WRITE(CACHE_MODE_1,
5708 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Jesse Barnes79831172012-06-20 10:53:12 -07005709
5710 /*
Ville Syrjälä031994e2014-01-22 21:32:46 +02005711 * WaIncreaseL3CreditsForVLVB0:vlv
5712 * This is the hardware default actually.
5713 */
5714 I915_WRITE(GEN7_L3SQCREG1, VLV_B0_WA_L3SQCREG1_VALUE);
5715
5716 /*
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005717 * WaDisableVLVClockGating_VBIIssue:vlv
Jesse Barnes2d809572012-10-25 12:15:44 -07005718 * Disable clock gating on th GCFG unit to prevent a delay
5719 * in the reporting of vblank events.
5720 */
Ville Syrjälä7a0d1ee2014-01-22 21:33:04 +02005721 I915_WRITE(VLV_GUNIT_CLOCK_GATE, GCFG_DIS);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005722}
5723
Ville Syrjäläa4565da2014-04-09 13:28:10 +03005724static void cherryview_init_clock_gating(struct drm_device *dev)
5725{
5726 struct drm_i915_private *dev_priv = dev->dev_private;
Deepak S67c3bf62014-07-10 13:16:24 +05305727 u32 val;
5728
5729 mutex_lock(&dev_priv->rps.hw_lock);
5730 val = vlv_punit_read(dev_priv, CCK_FUSE_REG);
5731 mutex_unlock(&dev_priv->rps.hw_lock);
5732 switch ((val >> 2) & 0x7) {
5733 case 0:
5734 case 1:
5735 dev_priv->rps.cz_freq = CHV_CZ_CLOCK_FREQ_MODE_200;
5736 dev_priv->mem_freq = 1600;
5737 break;
5738 case 2:
5739 dev_priv->rps.cz_freq = CHV_CZ_CLOCK_FREQ_MODE_267;
5740 dev_priv->mem_freq = 1600;
5741 break;
5742 case 3:
5743 dev_priv->rps.cz_freq = CHV_CZ_CLOCK_FREQ_MODE_333;
5744 dev_priv->mem_freq = 2000;
5745 break;
5746 case 4:
5747 dev_priv->rps.cz_freq = CHV_CZ_CLOCK_FREQ_MODE_320;
5748 dev_priv->mem_freq = 1600;
5749 break;
5750 case 5:
5751 dev_priv->rps.cz_freq = CHV_CZ_CLOCK_FREQ_MODE_400;
5752 dev_priv->mem_freq = 1600;
5753 break;
5754 }
5755 DRM_DEBUG_DRIVER("DDR speed: %d MHz", dev_priv->mem_freq);
Ville Syrjäläa4565da2014-04-09 13:28:10 +03005756
5757 I915_WRITE(DSPCLK_GATE_D, VRHUNIT_CLOCK_GATE_DISABLE);
5758
5759 I915_WRITE(MI_ARB_VLV, MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE);
Ville Syrjälädd811e72014-04-09 13:28:33 +03005760
5761 /* WaDisablePartialInstShootdown:chv */
5762 I915_WRITE(GEN8_ROW_CHICKEN,
5763 _MASKED_BIT_ENABLE(PARTIAL_INSTRUCTION_SHOOTDOWN_DISABLE));
Ville Syrjäläa7068022014-04-09 13:28:34 +03005764
5765 /* WaDisableThreadStallDopClockGating:chv */
5766 I915_WRITE(GEN8_ROW_CHICKEN,
5767 _MASKED_BIT_ENABLE(STALL_DOP_GATING_DISABLE));
Ville Syrjälä232ce332014-04-09 13:28:35 +03005768
5769 /* WaVSRefCountFullforceMissDisable:chv */
5770 /* WaDSRefCountFullforceMissDisable:chv */
5771 I915_WRITE(GEN7_FF_THREAD_MODE,
5772 I915_READ(GEN7_FF_THREAD_MODE) &
5773 ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
Ville Syrjäläacea6f92014-04-09 13:28:36 +03005774
5775 /* WaDisableSemaphoreAndSyncFlipWait:chv */
5776 I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
5777 _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
Ville Syrjälä08466972014-04-09 13:28:37 +03005778
5779 /* WaDisableCSUnitClockGating:chv */
5780 I915_WRITE(GEN6_UCGCTL1, I915_READ(GEN6_UCGCTL1) |
5781 GEN6_CSUNIT_CLOCK_GATE_DISABLE);
Ville Syrjäläc6317802014-04-09 13:28:38 +03005782
5783 /* WaDisableSDEUnitClockGating:chv */
5784 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
5785 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Rafael Barbalhoe0d34ce2014-04-09 13:28:40 +03005786
5787 /* WaDisableSamplerPowerBypass:chv (pre-production hw) */
5788 I915_WRITE(HALF_SLICE_CHICKEN3,
5789 _MASKED_BIT_ENABLE(GEN8_SAMPLER_POWER_BYPASS_DIS));
Ville Syrjäläe4443e42014-04-09 13:28:41 +03005790
5791 /* WaDisableGunitClockGating:chv (pre-production hw) */
5792 I915_WRITE(VLV_GUNIT_CLOCK_GATE, I915_READ(VLV_GUNIT_CLOCK_GATE) |
5793 GINT_DIS);
5794
5795 /* WaDisableFfDopClockGating:chv (pre-production hw) */
5796 I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
5797 _MASKED_BIT_ENABLE(GEN8_FF_DOP_CLOCK_GATE_DISABLE));
5798
5799 /* WaDisableDopClockGating:chv (pre-production hw) */
5800 I915_WRITE(GEN7_ROW_CHICKEN2,
5801 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
5802 I915_WRITE(GEN6_UCGCTL1, I915_READ(GEN6_UCGCTL1) |
5803 GEN6_EU_TCUNIT_CLOCK_GATE_DISABLE);
Ville Syrjäläa4565da2014-04-09 13:28:10 +03005804}
5805
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005806static void g4x_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005807{
5808 struct drm_i915_private *dev_priv = dev->dev_private;
5809 uint32_t dspclk_gate;
5810
5811 I915_WRITE(RENCLK_GATE_D1, 0);
5812 I915_WRITE(RENCLK_GATE_D2, VF_UNIT_CLOCK_GATE_DISABLE |
5813 GS_UNIT_CLOCK_GATE_DISABLE |
5814 CL_UNIT_CLOCK_GATE_DISABLE);
5815 I915_WRITE(RAMCLK_GATE_D, 0);
5816 dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE |
5817 OVRUNIT_CLOCK_GATE_DISABLE |
5818 OVCUNIT_CLOCK_GATE_DISABLE;
5819 if (IS_GM45(dev))
5820 dspclk_gate |= DSSUNIT_CLOCK_GATE_DISABLE;
5821 I915_WRITE(DSPCLK_GATE_D, dspclk_gate);
Daniel Vetter4358a372012-10-18 11:49:51 +02005822
5823 /* WaDisableRenderCachePipelinedFlush */
5824 I915_WRITE(CACHE_MODE_0,
5825 _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
Ville Syrjäläde1aa622013-06-07 10:47:01 +03005826
Akash Goel4e046322014-04-04 17:14:38 +05305827 /* WaDisable_RenderCache_OperationalFlush:g4x */
5828 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
5829
Ville Syrjälä0e088b82013-06-07 10:47:04 +03005830 g4x_disable_trickle_feed(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005831}
5832
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005833static void crestline_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005834{
5835 struct drm_i915_private *dev_priv = dev->dev_private;
5836
5837 I915_WRITE(RENCLK_GATE_D1, I965_RCC_CLOCK_GATE_DISABLE);
5838 I915_WRITE(RENCLK_GATE_D2, 0);
5839 I915_WRITE(DSPCLK_GATE_D, 0);
5840 I915_WRITE(RAMCLK_GATE_D, 0);
5841 I915_WRITE16(DEUC, 0);
Ville Syrjälä20f94962013-06-07 10:47:02 +03005842 I915_WRITE(MI_ARB_STATE,
5843 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Akash Goel4e046322014-04-04 17:14:38 +05305844
5845 /* WaDisable_RenderCache_OperationalFlush:gen4 */
5846 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005847}
5848
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005849static void broadwater_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005850{
5851 struct drm_i915_private *dev_priv = dev->dev_private;
5852
5853 I915_WRITE(RENCLK_GATE_D1, I965_RCZ_CLOCK_GATE_DISABLE |
5854 I965_RCC_CLOCK_GATE_DISABLE |
5855 I965_RCPB_CLOCK_GATE_DISABLE |
5856 I965_ISC_CLOCK_GATE_DISABLE |
5857 I965_FBC_CLOCK_GATE_DISABLE);
5858 I915_WRITE(RENCLK_GATE_D2, 0);
Ville Syrjälä20f94962013-06-07 10:47:02 +03005859 I915_WRITE(MI_ARB_STATE,
5860 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Akash Goel4e046322014-04-04 17:14:38 +05305861
5862 /* WaDisable_RenderCache_OperationalFlush:gen4 */
5863 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005864}
5865
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005866static void gen3_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005867{
5868 struct drm_i915_private *dev_priv = dev->dev_private;
5869 u32 dstate = I915_READ(D_STATE);
5870
5871 dstate |= DSTATE_PLL_D3_OFF | DSTATE_GFX_CLOCK_GATING |
5872 DSTATE_DOT_CLOCK_GATING;
5873 I915_WRITE(D_STATE, dstate);
Chris Wilson13a86b82012-04-24 14:51:43 +01005874
5875 if (IS_PINEVIEW(dev))
5876 I915_WRITE(ECOSKPD, _MASKED_BIT_ENABLE(ECO_GATING_CX_ONLY));
Daniel Vetter974a3b02012-09-09 11:54:16 +02005877
5878 /* IIR "flip pending" means done if this bit is set */
5879 I915_WRITE(ECOSKPD, _MASKED_BIT_DISABLE(ECO_FLIP_DONE));
Ville Syrjälä12fabbcb92014-02-25 15:13:38 +02005880
5881 /* interrupts should cause a wake up from C3 */
Ville Syrjälä32992542014-02-25 15:13:39 +02005882 I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_AGPBUSY_INT_EN));
Ville Syrjälädbb42742014-02-25 15:13:41 +02005883
5884 /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
5885 I915_WRITE(MI_ARB_STATE, _MASKED_BIT_ENABLE(MI_ARB_C3_LP_WRITE_ENABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005886}
5887
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005888static void i85x_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005889{
5890 struct drm_i915_private *dev_priv = dev->dev_private;
5891
5892 I915_WRITE(RENCLK_GATE_D1, SV_CLOCK_GATE_DISABLE);
Ville Syrjälä54e472a2014-02-25 15:13:40 +02005893
5894 /* interrupts should cause a wake up from C3 */
5895 I915_WRITE(MI_STATE, _MASKED_BIT_ENABLE(MI_AGPBUSY_INT_EN) |
5896 _MASKED_BIT_DISABLE(MI_AGPBUSY_830_MODE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005897}
5898
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005899static void i830_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005900{
5901 struct drm_i915_private *dev_priv = dev->dev_private;
5902
5903 I915_WRITE(DSPCLK_GATE_D, OVRUNIT_CLOCK_GATE_DISABLE);
5904}
5905
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005906void intel_init_clock_gating(struct drm_device *dev)
5907{
5908 struct drm_i915_private *dev_priv = dev->dev_private;
5909
5910 dev_priv->display.init_clock_gating(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005911}
5912
Imre Deak7d708ee2013-04-17 14:04:50 +03005913void intel_suspend_hw(struct drm_device *dev)
5914{
5915 if (HAS_PCH_LPT(dev))
5916 lpt_suspend_hw(dev);
5917}
5918
Imre Deakc1ca7272013-11-25 17:15:29 +02005919#define for_each_power_well(i, power_well, domain_mask, power_domains) \
5920 for (i = 0; \
5921 i < (power_domains)->power_well_count && \
5922 ((power_well) = &(power_domains)->power_wells[i]); \
5923 i++) \
5924 if ((power_well)->domains & (domain_mask))
5925
5926#define for_each_power_well_rev(i, power_well, domain_mask, power_domains) \
5927 for (i = (power_domains)->power_well_count - 1; \
5928 i >= 0 && ((power_well) = &(power_domains)->power_wells[i]);\
5929 i--) \
5930 if ((power_well)->domains & (domain_mask))
5931
Paulo Zanoni15d199e2013-03-22 14:14:13 -03005932/**
5933 * We should only use the power well if we explicitly asked the hardware to
5934 * enable it, so check if it's enabled and also check if we've requested it to
5935 * be enabled.
5936 */
Imre Deakda7e29b2014-02-18 00:02:02 +02005937static bool hsw_power_well_enabled(struct drm_i915_private *dev_priv,
Imre Deakc1ca7272013-11-25 17:15:29 +02005938 struct i915_power_well *power_well)
5939{
Imre Deakc1ca7272013-11-25 17:15:29 +02005940 return I915_READ(HSW_PWR_WELL_DRIVER) ==
5941 (HSW_PWR_WELL_ENABLE_REQUEST | HSW_PWR_WELL_STATE_ENABLED);
5942}
5943
Imre Deakbfafe932014-06-05 20:31:47 +03005944bool intel_display_power_enabled_unlocked(struct drm_i915_private *dev_priv,
5945 enum intel_display_power_domain domain)
Imre Deakddf9c532013-11-27 22:02:02 +02005946{
Imre Deakddf9c532013-11-27 22:02:02 +02005947 struct i915_power_domains *power_domains;
Imre Deakb8c000d2014-06-02 14:21:10 +03005948 struct i915_power_well *power_well;
5949 bool is_enabled;
5950 int i;
5951
5952 if (dev_priv->pm.suspended)
5953 return false;
Imre Deakddf9c532013-11-27 22:02:02 +02005954
5955 power_domains = &dev_priv->power_domains;
Imre Deakbfafe932014-06-05 20:31:47 +03005956
Imre Deakb8c000d2014-06-02 14:21:10 +03005957 is_enabled = true;
Imre Deakbfafe932014-06-05 20:31:47 +03005958
Imre Deakb8c000d2014-06-02 14:21:10 +03005959 for_each_power_well_rev(i, power_well, BIT(domain), power_domains) {
5960 if (power_well->always_on)
5961 continue;
Imre Deakddf9c532013-11-27 22:02:02 +02005962
Imre Deakbfafe932014-06-05 20:31:47 +03005963 if (!power_well->hw_enabled) {
Imre Deakb8c000d2014-06-02 14:21:10 +03005964 is_enabled = false;
5965 break;
5966 }
5967 }
Imre Deakbfafe932014-06-05 20:31:47 +03005968
Imre Deakb8c000d2014-06-02 14:21:10 +03005969 return is_enabled;
Imre Deakddf9c532013-11-27 22:02:02 +02005970}
5971
Imre Deakda7e29b2014-02-18 00:02:02 +02005972bool intel_display_power_enabled(struct drm_i915_private *dev_priv,
Paulo Zanonib97186f2013-05-03 12:15:36 -03005973 enum intel_display_power_domain domain)
Paulo Zanoni15d199e2013-03-22 14:14:13 -03005974{
Imre Deakc1ca7272013-11-25 17:15:29 +02005975 struct i915_power_domains *power_domains;
Imre Deakbfafe932014-06-05 20:31:47 +03005976 bool ret;
Paulo Zanoni882244a2014-04-01 14:55:12 -03005977
Imre Deakc1ca7272013-11-25 17:15:29 +02005978 power_domains = &dev_priv->power_domains;
5979
Imre Deakc1ca7272013-11-25 17:15:29 +02005980 mutex_lock(&power_domains->lock);
Imre Deakbfafe932014-06-05 20:31:47 +03005981 ret = intel_display_power_enabled_unlocked(dev_priv, domain);
Imre Deakc1ca7272013-11-25 17:15:29 +02005982 mutex_unlock(&power_domains->lock);
5983
Imre Deakbfafe932014-06-05 20:31:47 +03005984 return ret;
Paulo Zanoni15d199e2013-03-22 14:14:13 -03005985}
5986
Imre Deak93c73e82014-02-18 00:02:19 +02005987/*
5988 * Starting with Haswell, we have a "Power Down Well" that can be turned off
5989 * when not needed anymore. We have 4 registers that can request the power well
5990 * to be enabled, and it will only be disabled if none of the registers is
5991 * requesting it to be enabled.
5992 */
Paulo Zanonid5e8fdc2013-12-11 18:50:09 -02005993static void hsw_power_well_post_enable(struct drm_i915_private *dev_priv)
5994{
5995 struct drm_device *dev = dev_priv->dev;
5996 unsigned long irqflags;
5997
Paulo Zanonif9dcb0d2013-12-11 18:50:10 -02005998 /*
5999 * After we re-enable the power well, if we touch VGA register 0x3d5
6000 * we'll get unclaimed register interrupts. This stops after we write
6001 * anything to the VGA MSR register. The vgacon module uses this
6002 * register all the time, so if we unbind our driver and, as a
6003 * consequence, bind vgacon, we'll get stuck in an infinite loop at
6004 * console_unlock(). So make here we touch the VGA MSR register, making
6005 * sure vgacon can keep working normally without triggering interrupts
6006 * and error messages.
6007 */
6008 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
6009 outb(inb(VGA_MSR_READ), VGA_MSR_WRITE);
6010 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
6011
Paulo Zanonid5e8fdc2013-12-11 18:50:09 -02006012 if (IS_BROADWELL(dev)) {
6013 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
6014 I915_WRITE(GEN8_DE_PIPE_IMR(PIPE_B),
6015 dev_priv->de_irq_mask[PIPE_B]);
6016 I915_WRITE(GEN8_DE_PIPE_IER(PIPE_B),
6017 ~dev_priv->de_irq_mask[PIPE_B] |
6018 GEN8_PIPE_VBLANK);
6019 I915_WRITE(GEN8_DE_PIPE_IMR(PIPE_C),
6020 dev_priv->de_irq_mask[PIPE_C]);
6021 I915_WRITE(GEN8_DE_PIPE_IER(PIPE_C),
6022 ~dev_priv->de_irq_mask[PIPE_C] |
6023 GEN8_PIPE_VBLANK);
6024 POSTING_READ(GEN8_DE_PIPE_IER(PIPE_C));
6025 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
6026 }
6027}
6028
Imre Deakda7e29b2014-02-18 00:02:02 +02006029static void hsw_set_power_well(struct drm_i915_private *dev_priv,
Imre Deakc1ca7272013-11-25 17:15:29 +02006030 struct i915_power_well *power_well, bool enable)
Eugeni Dodonovd0d3e512012-05-09 15:37:16 -03006031{
Paulo Zanonifa42e232013-01-25 16:59:11 -02006032 bool is_enabled, enable_requested;
6033 uint32_t tmp;
Eugeni Dodonovd0d3e512012-05-09 15:37:16 -03006034
Paulo Zanonifa42e232013-01-25 16:59:11 -02006035 tmp = I915_READ(HSW_PWR_WELL_DRIVER);
Paulo Zanoni6aedd1f2013-08-02 16:22:25 -03006036 is_enabled = tmp & HSW_PWR_WELL_STATE_ENABLED;
6037 enable_requested = tmp & HSW_PWR_WELL_ENABLE_REQUEST;
Eugeni Dodonovd0d3e512012-05-09 15:37:16 -03006038
Paulo Zanonifa42e232013-01-25 16:59:11 -02006039 if (enable) {
6040 if (!enable_requested)
Paulo Zanoni6aedd1f2013-08-02 16:22:25 -03006041 I915_WRITE(HSW_PWR_WELL_DRIVER,
6042 HSW_PWR_WELL_ENABLE_REQUEST);
Eugeni Dodonovd0d3e512012-05-09 15:37:16 -03006043
Paulo Zanonifa42e232013-01-25 16:59:11 -02006044 if (!is_enabled) {
6045 DRM_DEBUG_KMS("Enabling power well\n");
6046 if (wait_for((I915_READ(HSW_PWR_WELL_DRIVER) &
Paulo Zanoni6aedd1f2013-08-02 16:22:25 -03006047 HSW_PWR_WELL_STATE_ENABLED), 20))
Paulo Zanonifa42e232013-01-25 16:59:11 -02006048 DRM_ERROR("Timeout enabling power well\n");
6049 }
Ben Widawsky596cc112013-11-11 14:46:28 -08006050
Paulo Zanonid5e8fdc2013-12-11 18:50:09 -02006051 hsw_power_well_post_enable(dev_priv);
Paulo Zanonifa42e232013-01-25 16:59:11 -02006052 } else {
6053 if (enable_requested) {
6054 I915_WRITE(HSW_PWR_WELL_DRIVER, 0);
Paulo Zanoni9dbd8fe2013-07-23 10:48:11 -03006055 POSTING_READ(HSW_PWR_WELL_DRIVER);
Paulo Zanonifa42e232013-01-25 16:59:11 -02006056 DRM_DEBUG_KMS("Requesting to disable the power well\n");
Eugeni Dodonovd0d3e512012-05-09 15:37:16 -03006057 }
6058 }
Paulo Zanonifa42e232013-01-25 16:59:11 -02006059}
Eugeni Dodonovd0d3e512012-05-09 15:37:16 -03006060
Imre Deakc6cb5822014-03-04 19:22:55 +02006061static void hsw_power_well_sync_hw(struct drm_i915_private *dev_priv,
6062 struct i915_power_well *power_well)
6063{
6064 hsw_set_power_well(dev_priv, power_well, power_well->count > 0);
6065
6066 /*
6067 * We're taking over the BIOS, so clear any requests made by it since
6068 * the driver is in charge now.
6069 */
6070 if (I915_READ(HSW_PWR_WELL_BIOS) & HSW_PWR_WELL_ENABLE_REQUEST)
6071 I915_WRITE(HSW_PWR_WELL_BIOS, 0);
6072}
6073
6074static void hsw_power_well_enable(struct drm_i915_private *dev_priv,
6075 struct i915_power_well *power_well)
6076{
Imre Deakc6cb5822014-03-04 19:22:55 +02006077 hsw_set_power_well(dev_priv, power_well, true);
6078}
6079
6080static void hsw_power_well_disable(struct drm_i915_private *dev_priv,
6081 struct i915_power_well *power_well)
6082{
6083 hsw_set_power_well(dev_priv, power_well, false);
Imre Deakc6cb5822014-03-04 19:22:55 +02006084}
6085
Imre Deaka45f44662014-03-04 19:22:56 +02006086static void i9xx_always_on_power_well_noop(struct drm_i915_private *dev_priv,
6087 struct i915_power_well *power_well)
6088{
6089}
6090
6091static bool i9xx_always_on_power_well_enabled(struct drm_i915_private *dev_priv,
6092 struct i915_power_well *power_well)
6093{
6094 return true;
6095}
6096
Ville Syrjäläd2011dc2014-06-13 13:37:56 +03006097static void vlv_set_power_well(struct drm_i915_private *dev_priv,
6098 struct i915_power_well *power_well, bool enable)
Imre Deak77961eb2014-03-05 16:20:56 +02006099{
Ville Syrjäläd2011dc2014-06-13 13:37:56 +03006100 enum punit_power_well power_well_id = power_well->data;
Imre Deak77961eb2014-03-05 16:20:56 +02006101 u32 mask;
6102 u32 state;
6103 u32 ctrl;
6104
6105 mask = PUNIT_PWRGT_MASK(power_well_id);
6106 state = enable ? PUNIT_PWRGT_PWR_ON(power_well_id) :
6107 PUNIT_PWRGT_PWR_GATE(power_well_id);
6108
6109 mutex_lock(&dev_priv->rps.hw_lock);
6110
6111#define COND \
6112 ((vlv_punit_read(dev_priv, PUNIT_REG_PWRGT_STATUS) & mask) == state)
6113
6114 if (COND)
6115 goto out;
6116
6117 ctrl = vlv_punit_read(dev_priv, PUNIT_REG_PWRGT_CTRL);
6118 ctrl &= ~mask;
6119 ctrl |= state;
6120 vlv_punit_write(dev_priv, PUNIT_REG_PWRGT_CTRL, ctrl);
6121
6122 if (wait_for(COND, 100))
6123 DRM_ERROR("timout setting power well state %08x (%08x)\n",
6124 state,
6125 vlv_punit_read(dev_priv, PUNIT_REG_PWRGT_CTRL));
6126
6127#undef COND
6128
6129out:
6130 mutex_unlock(&dev_priv->rps.hw_lock);
6131}
6132
6133static void vlv_power_well_sync_hw(struct drm_i915_private *dev_priv,
6134 struct i915_power_well *power_well)
6135{
6136 vlv_set_power_well(dev_priv, power_well, power_well->count > 0);
6137}
6138
6139static void vlv_power_well_enable(struct drm_i915_private *dev_priv,
6140 struct i915_power_well *power_well)
6141{
6142 vlv_set_power_well(dev_priv, power_well, true);
6143}
6144
6145static void vlv_power_well_disable(struct drm_i915_private *dev_priv,
6146 struct i915_power_well *power_well)
6147{
6148 vlv_set_power_well(dev_priv, power_well, false);
6149}
6150
6151static bool vlv_power_well_enabled(struct drm_i915_private *dev_priv,
6152 struct i915_power_well *power_well)
6153{
6154 int power_well_id = power_well->data;
6155 bool enabled = false;
6156 u32 mask;
6157 u32 state;
6158 u32 ctrl;
6159
6160 mask = PUNIT_PWRGT_MASK(power_well_id);
6161 ctrl = PUNIT_PWRGT_PWR_ON(power_well_id);
6162
6163 mutex_lock(&dev_priv->rps.hw_lock);
6164
6165 state = vlv_punit_read(dev_priv, PUNIT_REG_PWRGT_STATUS) & mask;
6166 /*
6167 * We only ever set the power-on and power-gate states, anything
6168 * else is unexpected.
6169 */
6170 WARN_ON(state != PUNIT_PWRGT_PWR_ON(power_well_id) &&
6171 state != PUNIT_PWRGT_PWR_GATE(power_well_id));
6172 if (state == ctrl)
6173 enabled = true;
6174
6175 /*
6176 * A transient state at this point would mean some unexpected party
6177 * is poking at the power controls too.
6178 */
6179 ctrl = vlv_punit_read(dev_priv, PUNIT_REG_PWRGT_CTRL) & mask;
6180 WARN_ON(ctrl != state);
6181
6182 mutex_unlock(&dev_priv->rps.hw_lock);
6183
6184 return enabled;
6185}
6186
6187static void vlv_display_power_well_enable(struct drm_i915_private *dev_priv,
6188 struct i915_power_well *power_well)
6189{
6190 WARN_ON_ONCE(power_well->data != PUNIT_POWER_WELL_DISP2D);
6191
6192 vlv_set_power_well(dev_priv, power_well, true);
6193
6194 spin_lock_irq(&dev_priv->irq_lock);
6195 valleyview_enable_display_irqs(dev_priv);
6196 spin_unlock_irq(&dev_priv->irq_lock);
6197
6198 /*
Imre Deak0d116a22014-04-25 13:19:05 +03006199 * During driver initialization/resume we can avoid restoring the
6200 * part of the HW/SW state that will be inited anyway explicitly.
Imre Deak77961eb2014-03-05 16:20:56 +02006201 */
Imre Deak0d116a22014-04-25 13:19:05 +03006202 if (dev_priv->power_domains.initializing)
6203 return;
6204
6205 intel_hpd_init(dev_priv->dev);
Imre Deak77961eb2014-03-05 16:20:56 +02006206
6207 i915_redisable_vga_power_on(dev_priv->dev);
6208}
6209
6210static void vlv_display_power_well_disable(struct drm_i915_private *dev_priv,
6211 struct i915_power_well *power_well)
6212{
Imre Deak77961eb2014-03-05 16:20:56 +02006213 WARN_ON_ONCE(power_well->data != PUNIT_POWER_WELL_DISP2D);
6214
6215 spin_lock_irq(&dev_priv->irq_lock);
Imre Deak77961eb2014-03-05 16:20:56 +02006216 valleyview_disable_display_irqs(dev_priv);
6217 spin_unlock_irq(&dev_priv->irq_lock);
6218
Imre Deak77961eb2014-03-05 16:20:56 +02006219 vlv_set_power_well(dev_priv, power_well, false);
6220}
6221
Ville Syrjäläaa519f22014-06-13 13:37:55 +03006222static void vlv_dpio_cmn_power_well_enable(struct drm_i915_private *dev_priv,
6223 struct i915_power_well *power_well)
6224{
6225 WARN_ON_ONCE(power_well->data != PUNIT_POWER_WELL_DPIO_CMN_BC);
6226
6227 /*
6228 * Enable the CRI clock source so we can get at the
6229 * display and the reference clock for VGA
6230 * hotplug / manual detection.
6231 */
6232 I915_WRITE(DPLL(PIPE_B), I915_READ(DPLL(PIPE_B)) |
6233 DPLL_REFA_CLK_ENABLE_VLV | DPLL_INTEGRATED_CRI_CLK_VLV);
6234 udelay(1); /* >10ns for cmnreset, >0ns for sidereset */
6235
6236 vlv_set_power_well(dev_priv, power_well, true);
6237
6238 /*
6239 * From VLV2A0_DP_eDP_DPIO_driver_vbios_notes_10.docx -
6240 * 6. De-assert cmn_reset/side_reset. Same as VLV X0.
6241 * a. GUnit 0x2110 bit[0] set to 1 (def 0)
6242 * b. The other bits such as sfr settings / modesel may all
6243 * be set to 0.
6244 *
6245 * This should only be done on init and resume from S3 with
6246 * both PLLs disabled, or we risk losing DPIO and PLL
6247 * synchronization.
6248 */
6249 I915_WRITE(DPIO_CTL, I915_READ(DPIO_CTL) | DPIO_CMNRST);
6250}
6251
6252static void vlv_dpio_cmn_power_well_disable(struct drm_i915_private *dev_priv,
6253 struct i915_power_well *power_well)
6254{
6255 struct drm_device *dev = dev_priv->dev;
6256 enum pipe pipe;
6257
6258 WARN_ON_ONCE(power_well->data != PUNIT_POWER_WELL_DPIO_CMN_BC);
6259
6260 for_each_pipe(pipe)
6261 assert_pll_disabled(dev_priv, pipe);
6262
6263 /* Assert common reset */
6264 I915_WRITE(DPIO_CTL, I915_READ(DPIO_CTL) & ~DPIO_CMNRST);
6265
6266 vlv_set_power_well(dev_priv, power_well, false);
6267}
6268
Imre Deak25eaa002014-03-04 19:23:06 +02006269static void check_power_well_state(struct drm_i915_private *dev_priv,
6270 struct i915_power_well *power_well)
6271{
6272 bool enabled = power_well->ops->is_enabled(dev_priv, power_well);
6273
6274 if (power_well->always_on || !i915.disable_power_well) {
6275 if (!enabled)
6276 goto mismatch;
6277
6278 return;
6279 }
6280
6281 if (enabled != (power_well->count > 0))
6282 goto mismatch;
6283
6284 return;
6285
6286mismatch:
6287 WARN(1, "state mismatch for '%s' (always_on %d hw state %d use-count %d disable_power_well %d\n",
6288 power_well->name, power_well->always_on, enabled,
6289 power_well->count, i915.disable_power_well);
6290}
6291
Imre Deakda7e29b2014-02-18 00:02:02 +02006292void intel_display_power_get(struct drm_i915_private *dev_priv,
Ville Syrjälä67656252013-09-16 17:38:28 +03006293 enum intel_display_power_domain domain)
6294{
Imre Deak83c00f52013-10-25 17:36:47 +03006295 struct i915_power_domains *power_domains;
Imre Deakc1ca7272013-11-25 17:15:29 +02006296 struct i915_power_well *power_well;
6297 int i;
Ville Syrjälä67656252013-09-16 17:38:28 +03006298
Paulo Zanoni9e6ea712014-03-07 20:08:06 -03006299 intel_runtime_pm_get(dev_priv);
6300
Imre Deak83c00f52013-10-25 17:36:47 +03006301 power_domains = &dev_priv->power_domains;
6302
6303 mutex_lock(&power_domains->lock);
Imre Deak1da51582013-11-25 17:15:35 +02006304
Imre Deak25eaa002014-03-04 19:23:06 +02006305 for_each_power_well(i, power_well, BIT(domain), power_domains) {
6306 if (!power_well->count++) {
6307 DRM_DEBUG_KMS("enabling %s\n", power_well->name);
Imre Deakc6cb5822014-03-04 19:22:55 +02006308 power_well->ops->enable(dev_priv, power_well);
Imre Deakbfafe932014-06-05 20:31:47 +03006309 power_well->hw_enabled = true;
Imre Deak25eaa002014-03-04 19:23:06 +02006310 }
6311
6312 check_power_well_state(dev_priv, power_well);
6313 }
Imre Deak1da51582013-11-25 17:15:35 +02006314
Imre Deakddf9c532013-11-27 22:02:02 +02006315 power_domains->domain_use_count[domain]++;
6316
Imre Deak83c00f52013-10-25 17:36:47 +03006317 mutex_unlock(&power_domains->lock);
Ville Syrjälä67656252013-09-16 17:38:28 +03006318}
6319
Imre Deakda7e29b2014-02-18 00:02:02 +02006320void intel_display_power_put(struct drm_i915_private *dev_priv,
Ville Syrjälä67656252013-09-16 17:38:28 +03006321 enum intel_display_power_domain domain)
6322{
Imre Deak83c00f52013-10-25 17:36:47 +03006323 struct i915_power_domains *power_domains;
Imre Deakc1ca7272013-11-25 17:15:29 +02006324 struct i915_power_well *power_well;
6325 int i;
Ville Syrjälä67656252013-09-16 17:38:28 +03006326
Imre Deak83c00f52013-10-25 17:36:47 +03006327 power_domains = &dev_priv->power_domains;
6328
6329 mutex_lock(&power_domains->lock);
Imre Deak1da51582013-11-25 17:15:35 +02006330
Imre Deak1da51582013-11-25 17:15:35 +02006331 WARN_ON(!power_domains->domain_use_count[domain]);
6332 power_domains->domain_use_count[domain]--;
Imre Deakddf9c532013-11-27 22:02:02 +02006333
Imre Deak70bf4072014-03-04 19:22:51 +02006334 for_each_power_well_rev(i, power_well, BIT(domain), power_domains) {
6335 WARN_ON(!power_well->count);
6336
Imre Deak25eaa002014-03-04 19:23:06 +02006337 if (!--power_well->count && i915.disable_power_well) {
6338 DRM_DEBUG_KMS("disabling %s\n", power_well->name);
Imre Deakbfafe932014-06-05 20:31:47 +03006339 power_well->hw_enabled = false;
Imre Deakc6cb5822014-03-04 19:22:55 +02006340 power_well->ops->disable(dev_priv, power_well);
Imre Deak25eaa002014-03-04 19:23:06 +02006341 }
6342
6343 check_power_well_state(dev_priv, power_well);
Imre Deak70bf4072014-03-04 19:22:51 +02006344 }
Imre Deak1da51582013-11-25 17:15:35 +02006345
Imre Deak83c00f52013-10-25 17:36:47 +03006346 mutex_unlock(&power_domains->lock);
Paulo Zanoni9e6ea712014-03-07 20:08:06 -03006347
6348 intel_runtime_pm_put(dev_priv);
Ville Syrjälä67656252013-09-16 17:38:28 +03006349}
6350
Imre Deak83c00f52013-10-25 17:36:47 +03006351static struct i915_power_domains *hsw_pwr;
Wang Xingchaoa38911a2013-05-30 22:07:11 +08006352
6353/* Display audio driver power well request */
Takashi Iwai74b0c2d2014-06-13 15:14:34 +02006354int i915_request_power_well(void)
Wang Xingchaoa38911a2013-05-30 22:07:11 +08006355{
Imre Deakb4ed4482013-10-25 17:36:49 +03006356 struct drm_i915_private *dev_priv;
6357
Takashi Iwai74b0c2d2014-06-13 15:14:34 +02006358 if (!hsw_pwr)
6359 return -ENODEV;
Wang Xingchaoa38911a2013-05-30 22:07:11 +08006360
Imre Deakb4ed4482013-10-25 17:36:49 +03006361 dev_priv = container_of(hsw_pwr, struct drm_i915_private,
6362 power_domains);
Imre Deakda7e29b2014-02-18 00:02:02 +02006363 intel_display_power_get(dev_priv, POWER_DOMAIN_AUDIO);
Takashi Iwai74b0c2d2014-06-13 15:14:34 +02006364 return 0;
Wang Xingchaoa38911a2013-05-30 22:07:11 +08006365}
6366EXPORT_SYMBOL_GPL(i915_request_power_well);
6367
6368/* Display audio driver power well release */
Takashi Iwai74b0c2d2014-06-13 15:14:34 +02006369int i915_release_power_well(void)
Wang Xingchaoa38911a2013-05-30 22:07:11 +08006370{
Imre Deakb4ed4482013-10-25 17:36:49 +03006371 struct drm_i915_private *dev_priv;
6372
Takashi Iwai74b0c2d2014-06-13 15:14:34 +02006373 if (!hsw_pwr)
6374 return -ENODEV;
Wang Xingchaoa38911a2013-05-30 22:07:11 +08006375
Imre Deakb4ed4482013-10-25 17:36:49 +03006376 dev_priv = container_of(hsw_pwr, struct drm_i915_private,
6377 power_domains);
Imre Deakda7e29b2014-02-18 00:02:02 +02006378 intel_display_power_put(dev_priv, POWER_DOMAIN_AUDIO);
Takashi Iwai74b0c2d2014-06-13 15:14:34 +02006379 return 0;
Wang Xingchaoa38911a2013-05-30 22:07:11 +08006380}
6381EXPORT_SYMBOL_GPL(i915_release_power_well);
6382
Jani Nikulac149dcb2014-07-04 10:00:37 +08006383/*
6384 * Private interface for the audio driver to get CDCLK in kHz.
6385 *
6386 * Caller must request power well using i915_request_power_well() prior to
6387 * making the call.
6388 */
6389int i915_get_cdclk_freq(void)
6390{
6391 struct drm_i915_private *dev_priv;
6392
6393 if (!hsw_pwr)
6394 return -ENODEV;
6395
6396 dev_priv = container_of(hsw_pwr, struct drm_i915_private,
6397 power_domains);
6398
6399 return intel_ddi_get_cdclk_freq(dev_priv);
6400}
6401EXPORT_SYMBOL_GPL(i915_get_cdclk_freq);
6402
6403
Imre Deakefcad912014-03-04 19:22:53 +02006404#define POWER_DOMAIN_MASK (BIT(POWER_DOMAIN_NUM) - 1)
6405
6406#define HSW_ALWAYS_ON_POWER_DOMAINS ( \
6407 BIT(POWER_DOMAIN_PIPE_A) | \
Imre Deakf5938f32014-03-04 19:22:54 +02006408 BIT(POWER_DOMAIN_TRANSCODER_EDP) | \
Imre Deak319be8a2014-03-04 19:22:57 +02006409 BIT(POWER_DOMAIN_PORT_DDI_A_2_LANES) | \
6410 BIT(POWER_DOMAIN_PORT_DDI_A_4_LANES) | \
6411 BIT(POWER_DOMAIN_PORT_DDI_B_2_LANES) | \
6412 BIT(POWER_DOMAIN_PORT_DDI_B_4_LANES) | \
6413 BIT(POWER_DOMAIN_PORT_DDI_C_2_LANES) | \
6414 BIT(POWER_DOMAIN_PORT_DDI_C_4_LANES) | \
6415 BIT(POWER_DOMAIN_PORT_DDI_D_2_LANES) | \
6416 BIT(POWER_DOMAIN_PORT_DDI_D_4_LANES) | \
6417 BIT(POWER_DOMAIN_PORT_CRT) | \
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -03006418 BIT(POWER_DOMAIN_PLLS) | \
Imre Deakf5938f32014-03-04 19:22:54 +02006419 BIT(POWER_DOMAIN_INIT))
Imre Deakefcad912014-03-04 19:22:53 +02006420#define HSW_DISPLAY_POWER_DOMAINS ( \
6421 (POWER_DOMAIN_MASK & ~HSW_ALWAYS_ON_POWER_DOMAINS) | \
6422 BIT(POWER_DOMAIN_INIT))
6423
6424#define BDW_ALWAYS_ON_POWER_DOMAINS ( \
6425 HSW_ALWAYS_ON_POWER_DOMAINS | \
6426 BIT(POWER_DOMAIN_PIPE_A_PANEL_FITTER))
6427#define BDW_DISPLAY_POWER_DOMAINS ( \
6428 (POWER_DOMAIN_MASK & ~BDW_ALWAYS_ON_POWER_DOMAINS) | \
6429 BIT(POWER_DOMAIN_INIT))
6430
Imre Deak77961eb2014-03-05 16:20:56 +02006431#define VLV_ALWAYS_ON_POWER_DOMAINS BIT(POWER_DOMAIN_INIT)
6432#define VLV_DISPLAY_POWER_DOMAINS POWER_DOMAIN_MASK
6433
6434#define VLV_DPIO_CMN_BC_POWER_DOMAINS ( \
6435 BIT(POWER_DOMAIN_PORT_DDI_B_2_LANES) | \
6436 BIT(POWER_DOMAIN_PORT_DDI_B_4_LANES) | \
6437 BIT(POWER_DOMAIN_PORT_DDI_C_2_LANES) | \
6438 BIT(POWER_DOMAIN_PORT_DDI_C_4_LANES) | \
6439 BIT(POWER_DOMAIN_PORT_CRT) | \
6440 BIT(POWER_DOMAIN_INIT))
6441
6442#define VLV_DPIO_TX_B_LANES_01_POWER_DOMAINS ( \
6443 BIT(POWER_DOMAIN_PORT_DDI_B_2_LANES) | \
6444 BIT(POWER_DOMAIN_PORT_DDI_B_4_LANES) | \
6445 BIT(POWER_DOMAIN_INIT))
6446
6447#define VLV_DPIO_TX_B_LANES_23_POWER_DOMAINS ( \
6448 BIT(POWER_DOMAIN_PORT_DDI_B_4_LANES) | \
6449 BIT(POWER_DOMAIN_INIT))
6450
6451#define VLV_DPIO_TX_C_LANES_01_POWER_DOMAINS ( \
6452 BIT(POWER_DOMAIN_PORT_DDI_C_2_LANES) | \
6453 BIT(POWER_DOMAIN_PORT_DDI_C_4_LANES) | \
6454 BIT(POWER_DOMAIN_INIT))
6455
6456#define VLV_DPIO_TX_C_LANES_23_POWER_DOMAINS ( \
6457 BIT(POWER_DOMAIN_PORT_DDI_C_4_LANES) | \
6458 BIT(POWER_DOMAIN_INIT))
6459
Imre Deaka45f44662014-03-04 19:22:56 +02006460static const struct i915_power_well_ops i9xx_always_on_power_well_ops = {
6461 .sync_hw = i9xx_always_on_power_well_noop,
6462 .enable = i9xx_always_on_power_well_noop,
6463 .disable = i9xx_always_on_power_well_noop,
6464 .is_enabled = i9xx_always_on_power_well_enabled,
6465};
Imre Deakc6cb5822014-03-04 19:22:55 +02006466
Imre Deak1c2256d2013-11-25 17:15:34 +02006467static struct i915_power_well i9xx_always_on_power_well[] = {
6468 {
6469 .name = "always-on",
6470 .always_on = 1,
6471 .domains = POWER_DOMAIN_MASK,
Imre Deakc6cb5822014-03-04 19:22:55 +02006472 .ops = &i9xx_always_on_power_well_ops,
Imre Deak1c2256d2013-11-25 17:15:34 +02006473 },
6474};
6475
Imre Deakc6cb5822014-03-04 19:22:55 +02006476static const struct i915_power_well_ops hsw_power_well_ops = {
6477 .sync_hw = hsw_power_well_sync_hw,
6478 .enable = hsw_power_well_enable,
6479 .disable = hsw_power_well_disable,
6480 .is_enabled = hsw_power_well_enabled,
6481};
6482
Imre Deakc1ca7272013-11-25 17:15:29 +02006483static struct i915_power_well hsw_power_wells[] = {
6484 {
Imre Deak6f3ef5d2013-11-25 17:15:30 +02006485 .name = "always-on",
6486 .always_on = 1,
6487 .domains = HSW_ALWAYS_ON_POWER_DOMAINS,
Imre Deakc6cb5822014-03-04 19:22:55 +02006488 .ops = &i9xx_always_on_power_well_ops,
Imre Deak6f3ef5d2013-11-25 17:15:30 +02006489 },
6490 {
Imre Deakc1ca7272013-11-25 17:15:29 +02006491 .name = "display",
Imre Deakefcad912014-03-04 19:22:53 +02006492 .domains = HSW_DISPLAY_POWER_DOMAINS,
Imre Deakc6cb5822014-03-04 19:22:55 +02006493 .ops = &hsw_power_well_ops,
Imre Deakc1ca7272013-11-25 17:15:29 +02006494 },
6495};
6496
6497static struct i915_power_well bdw_power_wells[] = {
6498 {
Imre Deak6f3ef5d2013-11-25 17:15:30 +02006499 .name = "always-on",
6500 .always_on = 1,
6501 .domains = BDW_ALWAYS_ON_POWER_DOMAINS,
Imre Deakc6cb5822014-03-04 19:22:55 +02006502 .ops = &i9xx_always_on_power_well_ops,
Imre Deak6f3ef5d2013-11-25 17:15:30 +02006503 },
6504 {
Imre Deakc1ca7272013-11-25 17:15:29 +02006505 .name = "display",
Imre Deakefcad912014-03-04 19:22:53 +02006506 .domains = BDW_DISPLAY_POWER_DOMAINS,
Imre Deakc6cb5822014-03-04 19:22:55 +02006507 .ops = &hsw_power_well_ops,
Imre Deakc1ca7272013-11-25 17:15:29 +02006508 },
6509};
6510
Imre Deak77961eb2014-03-05 16:20:56 +02006511static const struct i915_power_well_ops vlv_display_power_well_ops = {
6512 .sync_hw = vlv_power_well_sync_hw,
6513 .enable = vlv_display_power_well_enable,
6514 .disable = vlv_display_power_well_disable,
6515 .is_enabled = vlv_power_well_enabled,
6516};
6517
Ville Syrjäläaa519f22014-06-13 13:37:55 +03006518static const struct i915_power_well_ops vlv_dpio_cmn_power_well_ops = {
6519 .sync_hw = vlv_power_well_sync_hw,
6520 .enable = vlv_dpio_cmn_power_well_enable,
6521 .disable = vlv_dpio_cmn_power_well_disable,
6522 .is_enabled = vlv_power_well_enabled,
6523};
6524
Imre Deak77961eb2014-03-05 16:20:56 +02006525static const struct i915_power_well_ops vlv_dpio_power_well_ops = {
6526 .sync_hw = vlv_power_well_sync_hw,
6527 .enable = vlv_power_well_enable,
6528 .disable = vlv_power_well_disable,
6529 .is_enabled = vlv_power_well_enabled,
6530};
6531
6532static struct i915_power_well vlv_power_wells[] = {
6533 {
6534 .name = "always-on",
6535 .always_on = 1,
6536 .domains = VLV_ALWAYS_ON_POWER_DOMAINS,
6537 .ops = &i9xx_always_on_power_well_ops,
6538 },
6539 {
6540 .name = "display",
6541 .domains = VLV_DISPLAY_POWER_DOMAINS,
6542 .data = PUNIT_POWER_WELL_DISP2D,
6543 .ops = &vlv_display_power_well_ops,
6544 },
6545 {
Imre Deak77961eb2014-03-05 16:20:56 +02006546 .name = "dpio-tx-b-01",
6547 .domains = VLV_DPIO_TX_B_LANES_01_POWER_DOMAINS |
6548 VLV_DPIO_TX_B_LANES_23_POWER_DOMAINS |
6549 VLV_DPIO_TX_C_LANES_01_POWER_DOMAINS |
6550 VLV_DPIO_TX_C_LANES_23_POWER_DOMAINS,
6551 .ops = &vlv_dpio_power_well_ops,
6552 .data = PUNIT_POWER_WELL_DPIO_TX_B_LANES_01,
6553 },
6554 {
6555 .name = "dpio-tx-b-23",
6556 .domains = VLV_DPIO_TX_B_LANES_01_POWER_DOMAINS |
6557 VLV_DPIO_TX_B_LANES_23_POWER_DOMAINS |
6558 VLV_DPIO_TX_C_LANES_01_POWER_DOMAINS |
6559 VLV_DPIO_TX_C_LANES_23_POWER_DOMAINS,
6560 .ops = &vlv_dpio_power_well_ops,
6561 .data = PUNIT_POWER_WELL_DPIO_TX_B_LANES_23,
6562 },
6563 {
6564 .name = "dpio-tx-c-01",
6565 .domains = VLV_DPIO_TX_B_LANES_01_POWER_DOMAINS |
6566 VLV_DPIO_TX_B_LANES_23_POWER_DOMAINS |
6567 VLV_DPIO_TX_C_LANES_01_POWER_DOMAINS |
6568 VLV_DPIO_TX_C_LANES_23_POWER_DOMAINS,
6569 .ops = &vlv_dpio_power_well_ops,
6570 .data = PUNIT_POWER_WELL_DPIO_TX_C_LANES_01,
6571 },
6572 {
6573 .name = "dpio-tx-c-23",
6574 .domains = VLV_DPIO_TX_B_LANES_01_POWER_DOMAINS |
6575 VLV_DPIO_TX_B_LANES_23_POWER_DOMAINS |
6576 VLV_DPIO_TX_C_LANES_01_POWER_DOMAINS |
6577 VLV_DPIO_TX_C_LANES_23_POWER_DOMAINS,
6578 .ops = &vlv_dpio_power_well_ops,
6579 .data = PUNIT_POWER_WELL_DPIO_TX_C_LANES_23,
6580 },
Jesse Barnesf099a3c2014-05-23 13:16:43 -07006581 {
6582 .name = "dpio-common",
6583 .domains = VLV_DPIO_CMN_BC_POWER_DOMAINS,
6584 .data = PUNIT_POWER_WELL_DPIO_CMN_BC,
Ville Syrjäläaa519f22014-06-13 13:37:55 +03006585 .ops = &vlv_dpio_cmn_power_well_ops,
Jesse Barnesf099a3c2014-05-23 13:16:43 -07006586 },
Imre Deak77961eb2014-03-05 16:20:56 +02006587};
6588
Ville Syrjäläd2011dc2014-06-13 13:37:56 +03006589static struct i915_power_well *lookup_power_well(struct drm_i915_private *dev_priv,
6590 enum punit_power_well power_well_id)
6591{
6592 struct i915_power_domains *power_domains = &dev_priv->power_domains;
6593 struct i915_power_well *power_well;
6594 int i;
6595
6596 for_each_power_well(i, power_well, POWER_DOMAIN_MASK, power_domains) {
6597 if (power_well->data == power_well_id)
6598 return power_well;
6599 }
6600
6601 return NULL;
6602}
6603
Imre Deakc1ca7272013-11-25 17:15:29 +02006604#define set_power_wells(power_domains, __power_wells) ({ \
6605 (power_domains)->power_wells = (__power_wells); \
6606 (power_domains)->power_well_count = ARRAY_SIZE(__power_wells); \
6607})
6608
Imre Deakda7e29b2014-02-18 00:02:02 +02006609int intel_power_domains_init(struct drm_i915_private *dev_priv)
Wang Xingchaoa38911a2013-05-30 22:07:11 +08006610{
Imre Deak83c00f52013-10-25 17:36:47 +03006611 struct i915_power_domains *power_domains = &dev_priv->power_domains;
Imre Deakc1ca7272013-11-25 17:15:29 +02006612
Imre Deak83c00f52013-10-25 17:36:47 +03006613 mutex_init(&power_domains->lock);
Wang Xingchaoa38911a2013-05-30 22:07:11 +08006614
Imre Deakc1ca7272013-11-25 17:15:29 +02006615 /*
6616 * The enabling order will be from lower to higher indexed wells,
6617 * the disabling order is reversed.
6618 */
Imre Deakda7e29b2014-02-18 00:02:02 +02006619 if (IS_HASWELL(dev_priv->dev)) {
Imre Deakc1ca7272013-11-25 17:15:29 +02006620 set_power_wells(power_domains, hsw_power_wells);
6621 hsw_pwr = power_domains;
Imre Deakda7e29b2014-02-18 00:02:02 +02006622 } else if (IS_BROADWELL(dev_priv->dev)) {
Imre Deakc1ca7272013-11-25 17:15:29 +02006623 set_power_wells(power_domains, bdw_power_wells);
6624 hsw_pwr = power_domains;
Imre Deak77961eb2014-03-05 16:20:56 +02006625 } else if (IS_VALLEYVIEW(dev_priv->dev)) {
6626 set_power_wells(power_domains, vlv_power_wells);
Imre Deakc1ca7272013-11-25 17:15:29 +02006627 } else {
Imre Deak1c2256d2013-11-25 17:15:34 +02006628 set_power_wells(power_domains, i9xx_always_on_power_well);
Imre Deakc1ca7272013-11-25 17:15:29 +02006629 }
Wang Xingchaoa38911a2013-05-30 22:07:11 +08006630
6631 return 0;
6632}
6633
Imre Deakda7e29b2014-02-18 00:02:02 +02006634void intel_power_domains_remove(struct drm_i915_private *dev_priv)
Wang Xingchaoa38911a2013-05-30 22:07:11 +08006635{
6636 hsw_pwr = NULL;
6637}
6638
Imre Deakda7e29b2014-02-18 00:02:02 +02006639static void intel_power_domains_resume(struct drm_i915_private *dev_priv)
Ville Syrjälä9cdb8262013-09-16 17:38:27 +03006640{
Imre Deak83c00f52013-10-25 17:36:47 +03006641 struct i915_power_domains *power_domains = &dev_priv->power_domains;
6642 struct i915_power_well *power_well;
Imre Deakc1ca7272013-11-25 17:15:29 +02006643 int i;
Ville Syrjälä9cdb8262013-09-16 17:38:27 +03006644
Imre Deak83c00f52013-10-25 17:36:47 +03006645 mutex_lock(&power_domains->lock);
Imre Deakbfafe932014-06-05 20:31:47 +03006646 for_each_power_well(i, power_well, POWER_DOMAIN_MASK, power_domains) {
Imre Deaka45f44662014-03-04 19:22:56 +02006647 power_well->ops->sync_hw(dev_priv, power_well);
Imre Deakbfafe932014-06-05 20:31:47 +03006648 power_well->hw_enabled = power_well->ops->is_enabled(dev_priv,
6649 power_well);
6650 }
Imre Deak83c00f52013-10-25 17:36:47 +03006651 mutex_unlock(&power_domains->lock);
Wang Xingchaoa38911a2013-05-30 22:07:11 +08006652}
6653
Ville Syrjäläd2011dc2014-06-13 13:37:56 +03006654static void vlv_cmnlane_wa(struct drm_i915_private *dev_priv)
6655{
6656 struct i915_power_well *cmn =
6657 lookup_power_well(dev_priv, PUNIT_POWER_WELL_DPIO_CMN_BC);
6658 struct i915_power_well *disp2d =
6659 lookup_power_well(dev_priv, PUNIT_POWER_WELL_DISP2D);
6660
6661 /* nothing to do if common lane is already off */
6662 if (!cmn->ops->is_enabled(dev_priv, cmn))
6663 return;
6664
6665 /* If the display might be already active skip this */
6666 if (disp2d->ops->is_enabled(dev_priv, disp2d) &&
6667 I915_READ(DPIO_CTL) & DPIO_CMNRST)
6668 return;
6669
6670 DRM_DEBUG_KMS("toggling display PHY side reset\n");
6671
6672 /* cmnlane needs DPLL registers */
6673 disp2d->ops->enable(dev_priv, disp2d);
6674
6675 /*
6676 * From VLV2A0_DP_eDP_HDMI_DPIO_driver_vbios_notes_11.docx:
6677 * Need to assert and de-assert PHY SB reset by gating the
6678 * common lane power, then un-gating it.
6679 * Simply ungating isn't enough to reset the PHY enough to get
6680 * ports and lanes running.
6681 */
6682 cmn->ops->disable(dev_priv, cmn);
6683}
6684
Imre Deakda7e29b2014-02-18 00:02:02 +02006685void intel_power_domains_init_hw(struct drm_i915_private *dev_priv)
Paulo Zanonifa42e232013-01-25 16:59:11 -02006686{
Ville Syrjäläd2011dc2014-06-13 13:37:56 +03006687 struct drm_device *dev = dev_priv->dev;
Imre Deak0d116a22014-04-25 13:19:05 +03006688 struct i915_power_domains *power_domains = &dev_priv->power_domains;
6689
6690 power_domains->initializing = true;
Ville Syrjäläd2011dc2014-06-13 13:37:56 +03006691
6692 if (IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
6693 mutex_lock(&power_domains->lock);
6694 vlv_cmnlane_wa(dev_priv);
6695 mutex_unlock(&power_domains->lock);
6696 }
6697
Paulo Zanonifa42e232013-01-25 16:59:11 -02006698 /* For now, we need the power well to be always enabled. */
Imre Deakda7e29b2014-02-18 00:02:02 +02006699 intel_display_set_init_power(dev_priv, true);
6700 intel_power_domains_resume(dev_priv);
Imre Deak0d116a22014-04-25 13:19:05 +03006701 power_domains->initializing = false;
Eugeni Dodonovd0d3e512012-05-09 15:37:16 -03006702}
6703
Paulo Zanonic67a4702013-08-19 13:18:09 -03006704void intel_aux_display_runtime_get(struct drm_i915_private *dev_priv)
6705{
Paulo Zanonid361ae22014-03-07 20:08:12 -03006706 intel_runtime_pm_get(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -03006707}
6708
6709void intel_aux_display_runtime_put(struct drm_i915_private *dev_priv)
6710{
Paulo Zanonid361ae22014-03-07 20:08:12 -03006711 intel_runtime_pm_put(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -03006712}
6713
Paulo Zanoni8a187452013-12-06 20:32:13 -02006714void intel_runtime_pm_get(struct drm_i915_private *dev_priv)
6715{
6716 struct drm_device *dev = dev_priv->dev;
6717 struct device *device = &dev->pdev->dev;
6718
6719 if (!HAS_RUNTIME_PM(dev))
6720 return;
6721
6722 pm_runtime_get_sync(device);
6723 WARN(dev_priv->pm.suspended, "Device still suspended.\n");
6724}
6725
Imre Deakc6df39b2014-04-14 20:24:29 +03006726void intel_runtime_pm_get_noresume(struct drm_i915_private *dev_priv)
6727{
6728 struct drm_device *dev = dev_priv->dev;
6729 struct device *device = &dev->pdev->dev;
6730
6731 if (!HAS_RUNTIME_PM(dev))
6732 return;
6733
6734 WARN(dev_priv->pm.suspended, "Getting nosync-ref while suspended.\n");
6735 pm_runtime_get_noresume(device);
6736}
6737
Paulo Zanoni8a187452013-12-06 20:32:13 -02006738void intel_runtime_pm_put(struct drm_i915_private *dev_priv)
6739{
6740 struct drm_device *dev = dev_priv->dev;
6741 struct device *device = &dev->pdev->dev;
6742
6743 if (!HAS_RUNTIME_PM(dev))
6744 return;
6745
6746 pm_runtime_mark_last_busy(device);
6747 pm_runtime_put_autosuspend(device);
6748}
6749
6750void intel_init_runtime_pm(struct drm_i915_private *dev_priv)
6751{
6752 struct drm_device *dev = dev_priv->dev;
6753 struct device *device = &dev->pdev->dev;
6754
Paulo Zanoni8a187452013-12-06 20:32:13 -02006755 if (!HAS_RUNTIME_PM(dev))
6756 return;
6757
6758 pm_runtime_set_active(device);
6759
Imre Deakaeab0b52014-04-14 20:24:36 +03006760 /*
6761 * RPM depends on RC6 to save restore the GT HW context, so make RC6 a
6762 * requirement.
6763 */
6764 if (!intel_enable_rc6(dev)) {
6765 DRM_INFO("RC6 disabled, disabling runtime PM support\n");
6766 return;
6767 }
6768
Paulo Zanoni8a187452013-12-06 20:32:13 -02006769 pm_runtime_set_autosuspend_delay(device, 10000); /* 10s */
6770 pm_runtime_mark_last_busy(device);
6771 pm_runtime_use_autosuspend(device);
Paulo Zanoniba0239e2014-03-07 20:08:07 -03006772
6773 pm_runtime_put_autosuspend(device);
Paulo Zanoni8a187452013-12-06 20:32:13 -02006774}
6775
6776void intel_fini_runtime_pm(struct drm_i915_private *dev_priv)
6777{
6778 struct drm_device *dev = dev_priv->dev;
6779 struct device *device = &dev->pdev->dev;
6780
6781 if (!HAS_RUNTIME_PM(dev))
6782 return;
6783
Imre Deakaeab0b52014-04-14 20:24:36 +03006784 if (!intel_enable_rc6(dev))
6785 return;
6786
Paulo Zanoni8a187452013-12-06 20:32:13 -02006787 /* Make sure we're not suspended first. */
6788 pm_runtime_get_sync(device);
6789 pm_runtime_disable(device);
6790}
6791
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006792/* Set up chip specific power management-related functions */
6793void intel_init_pm(struct drm_device *dev)
6794{
6795 struct drm_i915_private *dev_priv = dev->dev_private;
6796
Daniel Vetter3a77c4c2014-01-10 08:50:12 +01006797 if (HAS_FBC(dev)) {
Ville Syrjälä40045462013-11-28 17:29:59 +02006798 if (INTEL_INFO(dev)->gen >= 7) {
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006799 dev_priv->display.fbc_enabled = ironlake_fbc_enabled;
Ville Syrjälä40045462013-11-28 17:29:59 +02006800 dev_priv->display.enable_fbc = gen7_enable_fbc;
6801 dev_priv->display.disable_fbc = ironlake_disable_fbc;
6802 } else if (INTEL_INFO(dev)->gen >= 5) {
6803 dev_priv->display.fbc_enabled = ironlake_fbc_enabled;
6804 dev_priv->display.enable_fbc = ironlake_enable_fbc;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006805 dev_priv->display.disable_fbc = ironlake_disable_fbc;
6806 } else if (IS_GM45(dev)) {
6807 dev_priv->display.fbc_enabled = g4x_fbc_enabled;
6808 dev_priv->display.enable_fbc = g4x_enable_fbc;
6809 dev_priv->display.disable_fbc = g4x_disable_fbc;
Ville Syrjälä40045462013-11-28 17:29:59 +02006810 } else {
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006811 dev_priv->display.fbc_enabled = i8xx_fbc_enabled;
6812 dev_priv->display.enable_fbc = i8xx_enable_fbc;
6813 dev_priv->display.disable_fbc = i8xx_disable_fbc;
Ville Syrjälä993495a2013-12-12 17:27:40 +02006814
6815 /* This value was pulled out of someone's hat */
6816 I915_WRITE(FBC_CONTROL, 500 << FBC_CTL_INTERVAL_SHIFT);
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006817 }
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006818 }
6819
Daniel Vetterc921aba2012-04-26 23:28:17 +02006820 /* For cxsr */
6821 if (IS_PINEVIEW(dev))
6822 i915_pineview_get_mem_freq(dev);
6823 else if (IS_GEN5(dev))
6824 i915_ironlake_get_mem_freq(dev);
6825
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006826 /* For FIFO watermark updates */
6827 if (HAS_PCH_SPLIT(dev)) {
Damien Lespiaufa50ad62014-03-17 18:01:16 +00006828 ilk_setup_wm_latency(dev);
Ville Syrjälä53615a52013-08-01 16:18:50 +03006829
Ville Syrjäläbd6025442014-01-07 16:14:10 +02006830 if ((IS_GEN5(dev) && dev_priv->wm.pri_latency[1] &&
6831 dev_priv->wm.spr_latency[1] && dev_priv->wm.cur_latency[1]) ||
6832 (!IS_GEN5(dev) && dev_priv->wm.pri_latency[0] &&
6833 dev_priv->wm.spr_latency[0] && dev_priv->wm.cur_latency[0])) {
6834 dev_priv->display.update_wm = ilk_update_wm;
6835 dev_priv->display.update_sprite_wm = ilk_update_sprite_wm;
6836 } else {
6837 DRM_DEBUG_KMS("Failed to read display plane latency. "
6838 "Disable CxSR\n");
6839 }
6840
6841 if (IS_GEN5(dev))
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006842 dev_priv->display.init_clock_gating = ironlake_init_clock_gating;
Ville Syrjäläbd6025442014-01-07 16:14:10 +02006843 else if (IS_GEN6(dev))
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006844 dev_priv->display.init_clock_gating = gen6_init_clock_gating;
Ville Syrjäläbd6025442014-01-07 16:14:10 +02006845 else if (IS_IVYBRIDGE(dev))
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006846 dev_priv->display.init_clock_gating = ivybridge_init_clock_gating;
Ville Syrjäläbd6025442014-01-07 16:14:10 +02006847 else if (IS_HASWELL(dev))
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03006848 dev_priv->display.init_clock_gating = haswell_init_clock_gating;
Ville Syrjäläbd6025442014-01-07 16:14:10 +02006849 else if (INTEL_INFO(dev)->gen == 8)
Ben Widawsky1020a5c2013-11-02 21:07:06 -07006850 dev_priv->display.init_clock_gating = gen8_init_clock_gating;
Ville Syrjäläa4565da2014-04-09 13:28:10 +03006851 } else if (IS_CHERRYVIEW(dev)) {
6852 dev_priv->display.update_wm = valleyview_update_wm;
6853 dev_priv->display.init_clock_gating =
6854 cherryview_init_clock_gating;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006855 } else if (IS_VALLEYVIEW(dev)) {
6856 dev_priv->display.update_wm = valleyview_update_wm;
6857 dev_priv->display.init_clock_gating =
6858 valleyview_init_clock_gating;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006859 } else if (IS_PINEVIEW(dev)) {
6860 if (!intel_get_cxsr_latency(IS_PINEVIEW_G(dev),
6861 dev_priv->is_ddr3,
6862 dev_priv->fsb_freq,
6863 dev_priv->mem_freq)) {
6864 DRM_INFO("failed to find known CxSR latency "
6865 "(found ddr%s fsb freq %d, mem freq %d), "
6866 "disabling CxSR\n",
6867 (dev_priv->is_ddr3 == 1) ? "3" : "2",
6868 dev_priv->fsb_freq, dev_priv->mem_freq);
6869 /* Disable CxSR and never update its watermark again */
Imre Deak5209b1f2014-07-01 12:36:17 +03006870 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006871 dev_priv->display.update_wm = NULL;
6872 } else
6873 dev_priv->display.update_wm = pineview_update_wm;
6874 dev_priv->display.init_clock_gating = gen3_init_clock_gating;
6875 } else if (IS_G4X(dev)) {
6876 dev_priv->display.update_wm = g4x_update_wm;
6877 dev_priv->display.init_clock_gating = g4x_init_clock_gating;
6878 } else if (IS_GEN4(dev)) {
6879 dev_priv->display.update_wm = i965_update_wm;
6880 if (IS_CRESTLINE(dev))
6881 dev_priv->display.init_clock_gating = crestline_init_clock_gating;
6882 else if (IS_BROADWATER(dev))
6883 dev_priv->display.init_clock_gating = broadwater_init_clock_gating;
6884 } else if (IS_GEN3(dev)) {
6885 dev_priv->display.update_wm = i9xx_update_wm;
6886 dev_priv->display.get_fifo_size = i9xx_get_fifo_size;
6887 dev_priv->display.init_clock_gating = gen3_init_clock_gating;
Daniel Vetterfeb56b92013-12-14 20:38:30 -02006888 } else if (IS_GEN2(dev)) {
6889 if (INTEL_INFO(dev)->num_pipes == 1) {
6890 dev_priv->display.update_wm = i845_update_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006891 dev_priv->display.get_fifo_size = i845_get_fifo_size;
Daniel Vetterfeb56b92013-12-14 20:38:30 -02006892 } else {
6893 dev_priv->display.update_wm = i9xx_update_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006894 dev_priv->display.get_fifo_size = i830_get_fifo_size;
Daniel Vetterfeb56b92013-12-14 20:38:30 -02006895 }
6896
6897 if (IS_I85X(dev) || IS_I865G(dev))
6898 dev_priv->display.init_clock_gating = i85x_init_clock_gating;
6899 else
6900 dev_priv->display.init_clock_gating = i830_init_clock_gating;
6901 } else {
6902 DRM_ERROR("unexpected fall-through in intel_init_pm\n");
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006903 }
6904}
6905
Ben Widawsky42c05262012-09-26 10:34:00 -07006906int sandybridge_pcode_read(struct drm_i915_private *dev_priv, u8 mbox, u32 *val)
6907{
Jesse Barnes4fc688c2012-11-02 11:14:01 -07006908 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Ben Widawsky42c05262012-09-26 10:34:00 -07006909
6910 if (I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) {
6911 DRM_DEBUG_DRIVER("warning: pcode (read) mailbox access failed\n");
6912 return -EAGAIN;
6913 }
6914
6915 I915_WRITE(GEN6_PCODE_DATA, *val);
6916 I915_WRITE(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY | mbox);
6917
6918 if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
6919 500)) {
6920 DRM_ERROR("timeout waiting for pcode read (%d) to finish\n", mbox);
6921 return -ETIMEDOUT;
6922 }
6923
6924 *val = I915_READ(GEN6_PCODE_DATA);
6925 I915_WRITE(GEN6_PCODE_DATA, 0);
6926
6927 return 0;
6928}
6929
6930int sandybridge_pcode_write(struct drm_i915_private *dev_priv, u8 mbox, u32 val)
6931{
Jesse Barnes4fc688c2012-11-02 11:14:01 -07006932 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Ben Widawsky42c05262012-09-26 10:34:00 -07006933
6934 if (I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) {
6935 DRM_DEBUG_DRIVER("warning: pcode (write) mailbox access failed\n");
6936 return -EAGAIN;
6937 }
6938
6939 I915_WRITE(GEN6_PCODE_DATA, val);
6940 I915_WRITE(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY | mbox);
6941
6942 if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
6943 500)) {
6944 DRM_ERROR("timeout waiting for pcode write (%d) to finish\n", mbox);
6945 return -ETIMEDOUT;
6946 }
6947
6948 I915_WRITE(GEN6_PCODE_DATA, 0);
6949
6950 return 0;
6951}
Jesse Barnesa0e4e192013-04-02 11:23:05 -07006952
Fengguang Wub55dd642014-07-12 11:21:39 +02006953static int byt_gpu_freq(struct drm_i915_private *dev_priv, int val)
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006954{
Ville Syrjälä07ab1182013-11-05 22:42:28 +02006955 int div;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006956
Ville Syrjälä07ab1182013-11-05 22:42:28 +02006957 /* 4 x czclk */
Ville Syrjälä2ec38152013-11-05 22:42:29 +02006958 switch (dev_priv->mem_freq) {
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006959 case 800:
Ville Syrjälä07ab1182013-11-05 22:42:28 +02006960 div = 10;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006961 break;
6962 case 1066:
Ville Syrjälä07ab1182013-11-05 22:42:28 +02006963 div = 12;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006964 break;
6965 case 1333:
Ville Syrjälä07ab1182013-11-05 22:42:28 +02006966 div = 16;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006967 break;
6968 default:
6969 return -1;
6970 }
6971
Ville Syrjälä2ec38152013-11-05 22:42:29 +02006972 return DIV_ROUND_CLOSEST(dev_priv->mem_freq * (val + 6 - 0xbd), 4 * div);
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006973}
6974
Fengguang Wub55dd642014-07-12 11:21:39 +02006975static int byt_freq_opcode(struct drm_i915_private *dev_priv, int val)
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006976{
Ville Syrjälä07ab1182013-11-05 22:42:28 +02006977 int mul;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006978
Ville Syrjälä07ab1182013-11-05 22:42:28 +02006979 /* 4 x czclk */
Ville Syrjälä2ec38152013-11-05 22:42:29 +02006980 switch (dev_priv->mem_freq) {
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006981 case 800:
Ville Syrjälä07ab1182013-11-05 22:42:28 +02006982 mul = 10;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006983 break;
6984 case 1066:
Ville Syrjälä07ab1182013-11-05 22:42:28 +02006985 mul = 12;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006986 break;
6987 case 1333:
Ville Syrjälä07ab1182013-11-05 22:42:28 +02006988 mul = 16;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006989 break;
6990 default:
6991 return -1;
6992 }
6993
Ville Syrjälä2ec38152013-11-05 22:42:29 +02006994 return DIV_ROUND_CLOSEST(4 * mul * val, dev_priv->mem_freq) + 0xbd - 6;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006995}
6996
Fengguang Wub55dd642014-07-12 11:21:39 +02006997static int chv_gpu_freq(struct drm_i915_private *dev_priv, int val)
Deepak S22b1b2f2014-07-12 14:54:33 +05306998{
6999 int div, freq;
7000
7001 switch (dev_priv->rps.cz_freq) {
7002 case 200:
7003 div = 5;
7004 break;
7005 case 267:
7006 div = 6;
7007 break;
7008 case 320:
7009 case 333:
7010 case 400:
7011 div = 8;
7012 break;
7013 default:
7014 return -1;
7015 }
7016
7017 freq = (DIV_ROUND_CLOSEST((dev_priv->rps.cz_freq * val), 2 * div) / 2);
7018
7019 return freq;
7020}
7021
Fengguang Wub55dd642014-07-12 11:21:39 +02007022static int chv_freq_opcode(struct drm_i915_private *dev_priv, int val)
Deepak S22b1b2f2014-07-12 14:54:33 +05307023{
7024 int mul, opcode;
7025
7026 switch (dev_priv->rps.cz_freq) {
7027 case 200:
7028 mul = 5;
7029 break;
7030 case 267:
7031 mul = 6;
7032 break;
7033 case 320:
7034 case 333:
7035 case 400:
7036 mul = 8;
7037 break;
7038 default:
7039 return -1;
7040 }
7041
7042 opcode = (DIV_ROUND_CLOSEST((val * 2 * mul), dev_priv->rps.cz_freq) * 2);
7043
7044 return opcode;
7045}
7046
7047int vlv_gpu_freq(struct drm_i915_private *dev_priv, int val)
7048{
7049 int ret = -1;
7050
7051 if (IS_CHERRYVIEW(dev_priv->dev))
7052 ret = chv_gpu_freq(dev_priv, val);
7053 else if (IS_VALLEYVIEW(dev_priv->dev))
7054 ret = byt_gpu_freq(dev_priv, val);
7055
7056 return ret;
7057}
7058
7059int vlv_freq_opcode(struct drm_i915_private *dev_priv, int val)
7060{
7061 int ret = -1;
7062
7063 if (IS_CHERRYVIEW(dev_priv->dev))
7064 ret = chv_freq_opcode(dev_priv, val);
7065 else if (IS_VALLEYVIEW(dev_priv->dev))
7066 ret = byt_freq_opcode(dev_priv, val);
7067
7068 return ret;
7069}
7070
Daniel Vetterf742a552013-12-06 10:17:53 +01007071void intel_pm_setup(struct drm_device *dev)
Chris Wilson907b28c2013-07-19 20:36:52 +01007072{
7073 struct drm_i915_private *dev_priv = dev->dev_private;
7074
Daniel Vetterf742a552013-12-06 10:17:53 +01007075 mutex_init(&dev_priv->rps.hw_lock);
7076
Chris Wilson907b28c2013-07-19 20:36:52 +01007077 INIT_DELAYED_WORK(&dev_priv->rps.delayed_resume_work,
7078 intel_gen6_powersave_work);
Paulo Zanoni5d584b22014-03-07 20:08:15 -03007079
Paulo Zanoni33688d92014-03-07 20:08:19 -03007080 dev_priv->pm.suspended = false;
Paulo Zanoni5d584b22014-03-07 20:08:15 -03007081 dev_priv->pm.irqs_disabled = false;
Chris Wilson907b28c2013-07-19 20:36:52 +01007082}