blob: b1b3a6b892c6bf572851fe2118fda3fa610201c0 [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
Rodrigo Vivida46f932014-08-01 02:04:45 -0700312 if (dev_priv->fbc.false_color)
313 dpfc_ctl |= FBC_CTL_FALSE_COLOR;
314
Ville Syrjälä3fa2e0e2014-01-23 16:49:12 +0200315 I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl | DPFC_CTL_EN);
Rodrigo Viviabe959c2013-05-06 19:37:33 -0300316
Rodrigo Vivi891348b2013-05-06 19:37:36 -0300317 if (IS_IVYBRIDGE(dev)) {
Damien Lespiau7dd23ba2013-05-10 14:33:17 +0100318 /* WaFbcAsynchFlipDisableFbcQueue:ivb */
Ville Syrjälä2adb6db2014-03-05 13:05:46 +0200319 I915_WRITE(ILK_DISPLAY_CHICKEN1,
320 I915_READ(ILK_DISPLAY_CHICKEN1) |
321 ILK_FBCQ_DIS);
Rodrigo Vivi28554162013-05-06 19:37:37 -0300322 } else {
Ville Syrjälä2adb6db2014-03-05 13:05:46 +0200323 /* WaFbcAsynchFlipDisableFbcQueue:hsw,bdw */
Ville Syrjälä8f670bb2014-03-05 13:05:47 +0200324 I915_WRITE(CHICKEN_PIPESL_1(intel_crtc->pipe),
325 I915_READ(CHICKEN_PIPESL_1(intel_crtc->pipe)) |
326 HSW_FBCQ_DIS);
Rodrigo Vivi891348b2013-05-06 19:37:36 -0300327 }
Rodrigo Vivib74ea102013-05-09 14:08:38 -0300328
Rodrigo Viviabe959c2013-05-06 19:37:33 -0300329 I915_WRITE(SNB_DPFC_CTL_SA,
330 SNB_CPU_FENCE_ENABLE | obj->fence_reg);
331 I915_WRITE(DPFC_CPU_FENCE_OFFSET, crtc->y);
332
333 sandybridge_blit_fbc_update(dev);
334
Ville Syrjäläb19870e2013-11-06 23:02:25 +0200335 DRM_DEBUG_KMS("enabled fbc on plane %c\n", plane_name(intel_crtc->plane));
Rodrigo Viviabe959c2013-05-06 19:37:33 -0300336}
337
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300338bool intel_fbc_enabled(struct drm_device *dev)
339{
340 struct drm_i915_private *dev_priv = dev->dev_private;
341
342 if (!dev_priv->display.fbc_enabled)
343 return false;
344
345 return dev_priv->display.fbc_enabled(dev);
346}
347
348static void intel_fbc_work_fn(struct work_struct *__work)
349{
350 struct intel_fbc_work *work =
351 container_of(to_delayed_work(__work),
352 struct intel_fbc_work, work);
353 struct drm_device *dev = work->crtc->dev;
354 struct drm_i915_private *dev_priv = dev->dev_private;
355
356 mutex_lock(&dev->struct_mutex);
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -0700357 if (work == dev_priv->fbc.fbc_work) {
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300358 /* Double check that we haven't switched fb without cancelling
359 * the prior work.
360 */
Matt Roperf4510a22014-04-01 15:22:40 -0700361 if (work->crtc->primary->fb == work->fb) {
Ville Syrjälä993495a2013-12-12 17:27:40 +0200362 dev_priv->display.enable_fbc(work->crtc);
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300363
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -0700364 dev_priv->fbc.plane = to_intel_crtc(work->crtc)->plane;
Matt Roperf4510a22014-04-01 15:22:40 -0700365 dev_priv->fbc.fb_id = work->crtc->primary->fb->base.id;
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -0700366 dev_priv->fbc.y = work->crtc->y;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300367 }
368
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -0700369 dev_priv->fbc.fbc_work = NULL;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300370 }
371 mutex_unlock(&dev->struct_mutex);
372
373 kfree(work);
374}
375
376static void intel_cancel_fbc_work(struct drm_i915_private *dev_priv)
377{
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -0700378 if (dev_priv->fbc.fbc_work == NULL)
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300379 return;
380
381 DRM_DEBUG_KMS("cancelling pending FBC enable\n");
382
383 /* Synchronisation is provided by struct_mutex and checking of
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -0700384 * dev_priv->fbc.fbc_work, so we can perform the cancellation
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300385 * entirely asynchronously.
386 */
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -0700387 if (cancel_delayed_work(&dev_priv->fbc.fbc_work->work))
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300388 /* tasklet was killed before being run, clean up */
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -0700389 kfree(dev_priv->fbc.fbc_work);
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300390
391 /* Mark the work as no longer wanted so that if it does
392 * wake-up (because the work was already running and waiting
393 * for our mutex), it will discover that is no longer
394 * necessary to run.
395 */
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -0700396 dev_priv->fbc.fbc_work = NULL;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300397}
398
Ville Syrjälä993495a2013-12-12 17:27:40 +0200399static void intel_enable_fbc(struct drm_crtc *crtc)
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300400{
401 struct intel_fbc_work *work;
402 struct drm_device *dev = crtc->dev;
403 struct drm_i915_private *dev_priv = dev->dev_private;
404
405 if (!dev_priv->display.enable_fbc)
406 return;
407
408 intel_cancel_fbc_work(dev_priv);
409
Daniel Vetterb14c5672013-09-19 12:18:32 +0200410 work = kzalloc(sizeof(*work), GFP_KERNEL);
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300411 if (work == NULL) {
Paulo Zanoni6cdcb5e2013-06-12 17:27:29 -0300412 DRM_ERROR("Failed to allocate FBC work structure\n");
Ville Syrjälä993495a2013-12-12 17:27:40 +0200413 dev_priv->display.enable_fbc(crtc);
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300414 return;
415 }
416
417 work->crtc = crtc;
Matt Roperf4510a22014-04-01 15:22:40 -0700418 work->fb = crtc->primary->fb;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300419 INIT_DELAYED_WORK(&work->work, intel_fbc_work_fn);
420
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -0700421 dev_priv->fbc.fbc_work = work;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300422
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300423 /* Delay the actual enabling to let pageflipping cease and the
424 * display to settle before starting the compression. Note that
425 * this delay also serves a second purpose: it allows for a
426 * vblank to pass after disabling the FBC before we attempt
427 * to modify the control registers.
428 *
429 * A more complicated solution would involve tracking vblanks
430 * following the termination of the page-flipping sequence
431 * and indeed performing the enable as a co-routine and not
432 * waiting synchronously upon the vblank.
Damien Lespiau7457d612013-06-07 17:41:07 +0100433 *
434 * WaFbcWaitForVBlankBeforeEnable:ilk,snb
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300435 */
436 schedule_delayed_work(&work->work, msecs_to_jiffies(50));
437}
438
439void intel_disable_fbc(struct drm_device *dev)
440{
441 struct drm_i915_private *dev_priv = dev->dev_private;
442
443 intel_cancel_fbc_work(dev_priv);
444
445 if (!dev_priv->display.disable_fbc)
446 return;
447
448 dev_priv->display.disable_fbc(dev);
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -0700449 dev_priv->fbc.plane = -1;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300450}
451
Chris Wilson29ebf902013-07-27 17:23:55 +0100452static bool set_no_fbc_reason(struct drm_i915_private *dev_priv,
453 enum no_fbc_reason reason)
454{
455 if (dev_priv->fbc.no_fbc_reason == reason)
456 return false;
457
458 dev_priv->fbc.no_fbc_reason = reason;
459 return true;
460}
461
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300462/**
463 * intel_update_fbc - enable/disable FBC as needed
464 * @dev: the drm_device
465 *
466 * Set up the framebuffer compression hardware at mode set time. We
467 * enable it if possible:
468 * - plane A only (on pre-965)
469 * - no pixel mulitply/line duplication
470 * - no alpha buffer discard
471 * - no dual wide
Paulo Zanonif85da862013-06-04 16:53:39 -0300472 * - framebuffer <= max_hdisplay in width, max_vdisplay in height
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300473 *
474 * We can't assume that any compression will take place (worst case),
475 * so the compressed buffer has to be the same size as the uncompressed
476 * one. It also must reside (along with the line length buffer) in
477 * stolen memory.
478 *
479 * We need to enable/disable FBC on a global basis.
480 */
481void intel_update_fbc(struct drm_device *dev)
482{
483 struct drm_i915_private *dev_priv = dev->dev_private;
484 struct drm_crtc *crtc = NULL, *tmp_crtc;
485 struct intel_crtc *intel_crtc;
486 struct drm_framebuffer *fb;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300487 struct drm_i915_gem_object *obj;
Ville Syrjäläef644fd2013-09-04 18:25:21 +0300488 const struct drm_display_mode *adjusted_mode;
Ville Syrjälä37327ab2013-09-04 18:25:28 +0300489 unsigned int max_width, max_height;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300490
Daniel Vetter3a77c4c2014-01-10 08:50:12 +0100491 if (!HAS_FBC(dev)) {
Chris Wilson29ebf902013-07-27 17:23:55 +0100492 set_no_fbc_reason(dev_priv, FBC_UNSUPPORTED);
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300493 return;
Chris Wilson29ebf902013-07-27 17:23:55 +0100494 }
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300495
Jani Nikulad330a952014-01-21 11:24:25 +0200496 if (!i915.powersave) {
Chris Wilson29ebf902013-07-27 17:23:55 +0100497 if (set_no_fbc_reason(dev_priv, FBC_MODULE_PARAM))
498 DRM_DEBUG_KMS("fbc disabled per module param\n");
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300499 return;
Chris Wilson29ebf902013-07-27 17:23:55 +0100500 }
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300501
502 /*
503 * If FBC is already on, we just have to verify that we can
504 * keep it that way...
505 * Need to disable if:
506 * - more than one pipe is active
507 * - changing FBC params (stride, fence, mode)
508 * - new fb is too large to fit in compressed buffer
509 * - going to an unsupported config (interlace, pixel multiply, etc.)
510 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +0100511 for_each_crtc(dev, tmp_crtc) {
Chris Wilson3490ea52013-01-07 10:11:40 +0000512 if (intel_crtc_active(tmp_crtc) &&
Ville Syrjälä4c445e02013-10-09 17:24:58 +0300513 to_intel_crtc(tmp_crtc)->primary_enabled) {
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300514 if (crtc) {
Chris Wilson29ebf902013-07-27 17:23:55 +0100515 if (set_no_fbc_reason(dev_priv, FBC_MULTIPLE_PIPES))
516 DRM_DEBUG_KMS("more than one pipe active, disabling compression\n");
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300517 goto out_disable;
518 }
519 crtc = tmp_crtc;
520 }
521 }
522
Matt Roperf4510a22014-04-01 15:22:40 -0700523 if (!crtc || crtc->primary->fb == NULL) {
Chris Wilson29ebf902013-07-27 17:23:55 +0100524 if (set_no_fbc_reason(dev_priv, FBC_NO_OUTPUT))
525 DRM_DEBUG_KMS("no output, disabling\n");
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300526 goto out_disable;
527 }
528
529 intel_crtc = to_intel_crtc(crtc);
Matt Roperf4510a22014-04-01 15:22:40 -0700530 fb = crtc->primary->fb;
Matt Roper2ff8fde2014-07-08 07:50:07 -0700531 obj = intel_fb_obj(fb);
Ville Syrjäläef644fd2013-09-04 18:25:21 +0300532 adjusted_mode = &intel_crtc->config.adjusted_mode;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300533
Chris Wilson03689202014-06-06 10:37:11 +0100534 if (i915.enable_fbc < 0) {
Chris Wilson29ebf902013-07-27 17:23:55 +0100535 if (set_no_fbc_reason(dev_priv, FBC_CHIP_DEFAULT))
536 DRM_DEBUG_KMS("disabled per chip default\n");
Damien Lespiau8a5729a2013-06-24 16:22:02 +0100537 goto out_disable;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300538 }
Jani Nikulad330a952014-01-21 11:24:25 +0200539 if (!i915.enable_fbc) {
Chris Wilson29ebf902013-07-27 17:23:55 +0100540 if (set_no_fbc_reason(dev_priv, FBC_MODULE_PARAM))
541 DRM_DEBUG_KMS("fbc disabled per module param\n");
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300542 goto out_disable;
543 }
Ville Syrjäläef644fd2013-09-04 18:25:21 +0300544 if ((adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) ||
545 (adjusted_mode->flags & DRM_MODE_FLAG_DBLSCAN)) {
Chris Wilson29ebf902013-07-27 17:23:55 +0100546 if (set_no_fbc_reason(dev_priv, FBC_UNSUPPORTED_MODE))
547 DRM_DEBUG_KMS("mode incompatible with compression, "
548 "disabling\n");
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300549 goto out_disable;
550 }
Paulo Zanonif85da862013-06-04 16:53:39 -0300551
Daisy Sun032843a2014-06-16 15:48:18 -0700552 if (INTEL_INFO(dev)->gen >= 8 || IS_HASWELL(dev)) {
553 max_width = 4096;
554 max_height = 4096;
555 } else if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
Ville Syrjälä37327ab2013-09-04 18:25:28 +0300556 max_width = 4096;
557 max_height = 2048;
Paulo Zanonif85da862013-06-04 16:53:39 -0300558 } else {
Ville Syrjälä37327ab2013-09-04 18:25:28 +0300559 max_width = 2048;
560 max_height = 1536;
Paulo Zanonif85da862013-06-04 16:53:39 -0300561 }
Ville Syrjälä37327ab2013-09-04 18:25:28 +0300562 if (intel_crtc->config.pipe_src_w > max_width ||
563 intel_crtc->config.pipe_src_h > max_height) {
Chris Wilson29ebf902013-07-27 17:23:55 +0100564 if (set_no_fbc_reason(dev_priv, FBC_MODE_TOO_LARGE))
565 DRM_DEBUG_KMS("mode too large for compression, disabling\n");
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300566 goto out_disable;
567 }
Ben Widawsky8f94d242014-02-20 16:01:20 -0800568 if ((INTEL_INFO(dev)->gen < 4 || HAS_DDI(dev)) &&
Ville Syrjäläc5a44aa2013-11-28 17:29:58 +0200569 intel_crtc->plane != PLANE_A) {
Chris Wilson29ebf902013-07-27 17:23:55 +0100570 if (set_no_fbc_reason(dev_priv, FBC_BAD_PLANE))
Ville Syrjäläc5a44aa2013-11-28 17:29:58 +0200571 DRM_DEBUG_KMS("plane not A, disabling compression\n");
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300572 goto out_disable;
573 }
574
575 /* The use of a CPU fence is mandatory in order to detect writes
576 * by the CPU to the scanout and trigger updates to the FBC.
577 */
578 if (obj->tiling_mode != I915_TILING_X ||
579 obj->fence_reg == I915_FENCE_REG_NONE) {
Chris Wilson29ebf902013-07-27 17:23:55 +0100580 if (set_no_fbc_reason(dev_priv, FBC_NOT_TILED))
581 DRM_DEBUG_KMS("framebuffer not tiled or fenced, disabling compression\n");
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300582 goto out_disable;
583 }
Sonika Jindal48404c12014-08-22 14:06:04 +0530584 if (INTEL_INFO(dev)->gen <= 4 && !IS_G4X(dev) &&
585 to_intel_plane(crtc->primary)->rotation != BIT(DRM_ROTATE_0)) {
586 if (set_no_fbc_reason(dev_priv, FBC_UNSUPPORTED_MODE))
587 DRM_DEBUG_KMS("Rotation unsupported, disabling\n");
588 goto out_disable;
589 }
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300590
591 /* If the kernel debugger is active, always disable compression */
592 if (in_dbg_master())
593 goto out_disable;
594
Matt Roper2ff8fde2014-07-08 07:50:07 -0700595 if (i915_gem_stolen_setup_compression(dev, obj->base.size,
Ben Widawsky5e59f712014-06-30 10:41:24 -0700596 drm_format_plane_cpp(fb->pixel_format, 0))) {
Chris Wilson29ebf902013-07-27 17:23:55 +0100597 if (set_no_fbc_reason(dev_priv, FBC_STOLEN_TOO_SMALL))
598 DRM_DEBUG_KMS("framebuffer too large, disabling compression\n");
Chris Wilson11be49e2012-11-15 11:32:20 +0000599 goto out_disable;
600 }
601
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300602 /* If the scanout has not changed, don't modify the FBC settings.
603 * Note that we make the fundamental assumption that the fb->obj
604 * cannot be unpinned (and have its GTT offset and fence revoked)
605 * without first being decoupled from the scanout and FBC disabled.
606 */
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -0700607 if (dev_priv->fbc.plane == intel_crtc->plane &&
608 dev_priv->fbc.fb_id == fb->base.id &&
609 dev_priv->fbc.y == crtc->y)
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300610 return;
611
612 if (intel_fbc_enabled(dev)) {
613 /* We update FBC along two paths, after changing fb/crtc
614 * configuration (modeswitching) and after page-flipping
615 * finishes. For the latter, we know that not only did
616 * we disable the FBC at the start of the page-flip
617 * sequence, but also more than one vblank has passed.
618 *
619 * For the former case of modeswitching, it is possible
620 * to switch between two FBC valid configurations
621 * instantaneously so we do need to disable the FBC
622 * before we can modify its control registers. We also
623 * have to wait for the next vblank for that to take
624 * effect. However, since we delay enabling FBC we can
625 * assume that a vblank has passed since disabling and
626 * that we can safely alter the registers in the deferred
627 * callback.
628 *
629 * In the scenario that we go from a valid to invalid
630 * and then back to valid FBC configuration we have
631 * no strict enforcement that a vblank occurred since
632 * disabling the FBC. However, along all current pipe
633 * disabling paths we do need to wait for a vblank at
634 * some point. And we wait before enabling FBC anyway.
635 */
636 DRM_DEBUG_KMS("disabling active FBC for update\n");
637 intel_disable_fbc(dev);
638 }
639
Ville Syrjälä993495a2013-12-12 17:27:40 +0200640 intel_enable_fbc(crtc);
Chris Wilson29ebf902013-07-27 17:23:55 +0100641 dev_priv->fbc.no_fbc_reason = FBC_OK;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300642 return;
643
644out_disable:
645 /* Multiple disables should be harmless */
646 if (intel_fbc_enabled(dev)) {
647 DRM_DEBUG_KMS("unsupported config, disabling FBC\n");
648 intel_disable_fbc(dev);
649 }
Chris Wilson11be49e2012-11-15 11:32:20 +0000650 i915_gem_stolen_cleanup_compression(dev);
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300651}
652
Daniel Vetterc921aba2012-04-26 23:28:17 +0200653static void i915_pineview_get_mem_freq(struct drm_device *dev)
654{
Jani Nikula50227e12014-03-31 14:27:21 +0300655 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200656 u32 tmp;
657
658 tmp = I915_READ(CLKCFG);
659
660 switch (tmp & CLKCFG_FSB_MASK) {
661 case CLKCFG_FSB_533:
662 dev_priv->fsb_freq = 533; /* 133*4 */
663 break;
664 case CLKCFG_FSB_800:
665 dev_priv->fsb_freq = 800; /* 200*4 */
666 break;
667 case CLKCFG_FSB_667:
668 dev_priv->fsb_freq = 667; /* 167*4 */
669 break;
670 case CLKCFG_FSB_400:
671 dev_priv->fsb_freq = 400; /* 100*4 */
672 break;
673 }
674
675 switch (tmp & CLKCFG_MEM_MASK) {
676 case CLKCFG_MEM_533:
677 dev_priv->mem_freq = 533;
678 break;
679 case CLKCFG_MEM_667:
680 dev_priv->mem_freq = 667;
681 break;
682 case CLKCFG_MEM_800:
683 dev_priv->mem_freq = 800;
684 break;
685 }
686
687 /* detect pineview DDR3 setting */
688 tmp = I915_READ(CSHRDDR3CTL);
689 dev_priv->is_ddr3 = (tmp & CSHRDDR3CTL_DDR3) ? 1 : 0;
690}
691
692static void i915_ironlake_get_mem_freq(struct drm_device *dev)
693{
Jani Nikula50227e12014-03-31 14:27:21 +0300694 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200695 u16 ddrpll, csipll;
696
697 ddrpll = I915_READ16(DDRMPLL1);
698 csipll = I915_READ16(CSIPLL0);
699
700 switch (ddrpll & 0xff) {
701 case 0xc:
702 dev_priv->mem_freq = 800;
703 break;
704 case 0x10:
705 dev_priv->mem_freq = 1066;
706 break;
707 case 0x14:
708 dev_priv->mem_freq = 1333;
709 break;
710 case 0x18:
711 dev_priv->mem_freq = 1600;
712 break;
713 default:
714 DRM_DEBUG_DRIVER("unknown memory frequency 0x%02x\n",
715 ddrpll & 0xff);
716 dev_priv->mem_freq = 0;
717 break;
718 }
719
Daniel Vetter20e4d402012-08-08 23:35:39 +0200720 dev_priv->ips.r_t = dev_priv->mem_freq;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200721
722 switch (csipll & 0x3ff) {
723 case 0x00c:
724 dev_priv->fsb_freq = 3200;
725 break;
726 case 0x00e:
727 dev_priv->fsb_freq = 3733;
728 break;
729 case 0x010:
730 dev_priv->fsb_freq = 4266;
731 break;
732 case 0x012:
733 dev_priv->fsb_freq = 4800;
734 break;
735 case 0x014:
736 dev_priv->fsb_freq = 5333;
737 break;
738 case 0x016:
739 dev_priv->fsb_freq = 5866;
740 break;
741 case 0x018:
742 dev_priv->fsb_freq = 6400;
743 break;
744 default:
745 DRM_DEBUG_DRIVER("unknown fsb frequency 0x%04x\n",
746 csipll & 0x3ff);
747 dev_priv->fsb_freq = 0;
748 break;
749 }
750
751 if (dev_priv->fsb_freq == 3200) {
Daniel Vetter20e4d402012-08-08 23:35:39 +0200752 dev_priv->ips.c_m = 0;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200753 } else if (dev_priv->fsb_freq > 3200 && dev_priv->fsb_freq <= 4800) {
Daniel Vetter20e4d402012-08-08 23:35:39 +0200754 dev_priv->ips.c_m = 1;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200755 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +0200756 dev_priv->ips.c_m = 2;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200757 }
758}
759
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300760static const struct cxsr_latency cxsr_latency_table[] = {
761 {1, 0, 800, 400, 3382, 33382, 3983, 33983}, /* DDR2-400 SC */
762 {1, 0, 800, 667, 3354, 33354, 3807, 33807}, /* DDR2-667 SC */
763 {1, 0, 800, 800, 3347, 33347, 3763, 33763}, /* DDR2-800 SC */
764 {1, 1, 800, 667, 6420, 36420, 6873, 36873}, /* DDR3-667 SC */
765 {1, 1, 800, 800, 5902, 35902, 6318, 36318}, /* DDR3-800 SC */
766
767 {1, 0, 667, 400, 3400, 33400, 4021, 34021}, /* DDR2-400 SC */
768 {1, 0, 667, 667, 3372, 33372, 3845, 33845}, /* DDR2-667 SC */
769 {1, 0, 667, 800, 3386, 33386, 3822, 33822}, /* DDR2-800 SC */
770 {1, 1, 667, 667, 6438, 36438, 6911, 36911}, /* DDR3-667 SC */
771 {1, 1, 667, 800, 5941, 35941, 6377, 36377}, /* DDR3-800 SC */
772
773 {1, 0, 400, 400, 3472, 33472, 4173, 34173}, /* DDR2-400 SC */
774 {1, 0, 400, 667, 3443, 33443, 3996, 33996}, /* DDR2-667 SC */
775 {1, 0, 400, 800, 3430, 33430, 3946, 33946}, /* DDR2-800 SC */
776 {1, 1, 400, 667, 6509, 36509, 7062, 37062}, /* DDR3-667 SC */
777 {1, 1, 400, 800, 5985, 35985, 6501, 36501}, /* DDR3-800 SC */
778
779 {0, 0, 800, 400, 3438, 33438, 4065, 34065}, /* DDR2-400 SC */
780 {0, 0, 800, 667, 3410, 33410, 3889, 33889}, /* DDR2-667 SC */
781 {0, 0, 800, 800, 3403, 33403, 3845, 33845}, /* DDR2-800 SC */
782 {0, 1, 800, 667, 6476, 36476, 6955, 36955}, /* DDR3-667 SC */
783 {0, 1, 800, 800, 5958, 35958, 6400, 36400}, /* DDR3-800 SC */
784
785 {0, 0, 667, 400, 3456, 33456, 4103, 34106}, /* DDR2-400 SC */
786 {0, 0, 667, 667, 3428, 33428, 3927, 33927}, /* DDR2-667 SC */
787 {0, 0, 667, 800, 3443, 33443, 3905, 33905}, /* DDR2-800 SC */
788 {0, 1, 667, 667, 6494, 36494, 6993, 36993}, /* DDR3-667 SC */
789 {0, 1, 667, 800, 5998, 35998, 6460, 36460}, /* DDR3-800 SC */
790
791 {0, 0, 400, 400, 3528, 33528, 4255, 34255}, /* DDR2-400 SC */
792 {0, 0, 400, 667, 3500, 33500, 4079, 34079}, /* DDR2-667 SC */
793 {0, 0, 400, 800, 3487, 33487, 4029, 34029}, /* DDR2-800 SC */
794 {0, 1, 400, 667, 6566, 36566, 7145, 37145}, /* DDR3-667 SC */
795 {0, 1, 400, 800, 6042, 36042, 6584, 36584}, /* DDR3-800 SC */
796};
797
Daniel Vetter63c62272012-04-21 23:17:55 +0200798static const struct cxsr_latency *intel_get_cxsr_latency(int is_desktop,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300799 int is_ddr3,
800 int fsb,
801 int mem)
802{
803 const struct cxsr_latency *latency;
804 int i;
805
806 if (fsb == 0 || mem == 0)
807 return NULL;
808
809 for (i = 0; i < ARRAY_SIZE(cxsr_latency_table); i++) {
810 latency = &cxsr_latency_table[i];
811 if (is_desktop == latency->is_desktop &&
812 is_ddr3 == latency->is_ddr3 &&
813 fsb == latency->fsb_freq && mem == latency->mem_freq)
814 return latency;
815 }
816
817 DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
818
819 return NULL;
820}
821
Imre Deak5209b1f2014-07-01 12:36:17 +0300822void intel_set_memory_cxsr(struct drm_i915_private *dev_priv, bool enable)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300823{
Imre Deak5209b1f2014-07-01 12:36:17 +0300824 struct drm_device *dev = dev_priv->dev;
825 u32 val;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300826
Imre Deak5209b1f2014-07-01 12:36:17 +0300827 if (IS_VALLEYVIEW(dev)) {
828 I915_WRITE(FW_BLC_SELF_VLV, enable ? FW_CSPWRDWNEN : 0);
829 } else if (IS_G4X(dev) || IS_CRESTLINE(dev)) {
830 I915_WRITE(FW_BLC_SELF, enable ? FW_BLC_SELF_EN : 0);
831 } else if (IS_PINEVIEW(dev)) {
832 val = I915_READ(DSPFW3) & ~PINEVIEW_SELF_REFRESH_EN;
833 val |= enable ? PINEVIEW_SELF_REFRESH_EN : 0;
834 I915_WRITE(DSPFW3, val);
835 } else if (IS_I945G(dev) || IS_I945GM(dev)) {
836 val = enable ? _MASKED_BIT_ENABLE(FW_BLC_SELF_EN) :
837 _MASKED_BIT_DISABLE(FW_BLC_SELF_EN);
838 I915_WRITE(FW_BLC_SELF, val);
839 } else if (IS_I915GM(dev)) {
840 val = enable ? _MASKED_BIT_ENABLE(INSTPM_SELF_EN) :
841 _MASKED_BIT_DISABLE(INSTPM_SELF_EN);
842 I915_WRITE(INSTPM, val);
843 } else {
844 return;
845 }
846
847 DRM_DEBUG_KMS("memory self-refresh is %s\n",
848 enable ? "enabled" : "disabled");
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300849}
850
851/*
852 * Latency for FIFO fetches is dependent on several factors:
853 * - memory configuration (speed, channels)
854 * - chipset
855 * - current MCH state
856 * It can be fairly high in some situations, so here we assume a fairly
857 * pessimal value. It's a tradeoff between extra memory fetches (if we
858 * set this value too high, the FIFO will fetch frequently to stay full)
859 * and power consumption (set it too low to save power and we might see
860 * FIFO underruns and display "flicker").
861 *
862 * A value of 5us seems to be a good balance; safe for very low end
863 * platforms but not overly aggressive on lower latency configs.
864 */
865static const int latency_ns = 5000;
866
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300867static int i9xx_get_fifo_size(struct drm_device *dev, int plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300868{
869 struct drm_i915_private *dev_priv = dev->dev_private;
870 uint32_t dsparb = I915_READ(DSPARB);
871 int size;
872
873 size = dsparb & 0x7f;
874 if (plane)
875 size = ((dsparb >> DSPARB_CSTART_SHIFT) & 0x7f) - size;
876
877 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
878 plane ? "B" : "A", size);
879
880 return size;
881}
882
Daniel Vetterfeb56b92013-12-14 20:38:30 -0200883static int i830_get_fifo_size(struct drm_device *dev, int plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300884{
885 struct drm_i915_private *dev_priv = dev->dev_private;
886 uint32_t dsparb = I915_READ(DSPARB);
887 int size;
888
889 size = dsparb & 0x1ff;
890 if (plane)
891 size = ((dsparb >> DSPARB_BEND_SHIFT) & 0x1ff) - size;
892 size >>= 1; /* Convert to cachelines */
893
894 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
895 plane ? "B" : "A", size);
896
897 return size;
898}
899
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300900static int i845_get_fifo_size(struct drm_device *dev, int plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300901{
902 struct drm_i915_private *dev_priv = dev->dev_private;
903 uint32_t dsparb = I915_READ(DSPARB);
904 int size;
905
906 size = dsparb & 0x7f;
907 size >>= 2; /* Convert to cachelines */
908
909 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
910 plane ? "B" : "A",
911 size);
912
913 return size;
914}
915
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300916/* Pineview has different values for various configs */
917static const struct intel_watermark_params pineview_display_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300918 .fifo_size = PINEVIEW_DISPLAY_FIFO,
919 .max_wm = PINEVIEW_MAX_WM,
920 .default_wm = PINEVIEW_DFT_WM,
921 .guard_size = PINEVIEW_GUARD_WM,
922 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300923};
924static const struct intel_watermark_params pineview_display_hplloff_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300925 .fifo_size = PINEVIEW_DISPLAY_FIFO,
926 .max_wm = PINEVIEW_MAX_WM,
927 .default_wm = PINEVIEW_DFT_HPLLOFF_WM,
928 .guard_size = PINEVIEW_GUARD_WM,
929 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300930};
931static const struct intel_watermark_params pineview_cursor_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300932 .fifo_size = PINEVIEW_CURSOR_FIFO,
933 .max_wm = PINEVIEW_CURSOR_MAX_WM,
934 .default_wm = PINEVIEW_CURSOR_DFT_WM,
935 .guard_size = PINEVIEW_CURSOR_GUARD_WM,
936 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300937};
938static const struct intel_watermark_params pineview_cursor_hplloff_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300939 .fifo_size = PINEVIEW_CURSOR_FIFO,
940 .max_wm = PINEVIEW_CURSOR_MAX_WM,
941 .default_wm = PINEVIEW_CURSOR_DFT_WM,
942 .guard_size = PINEVIEW_CURSOR_GUARD_WM,
943 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300944};
945static const struct intel_watermark_params g4x_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300946 .fifo_size = G4X_FIFO_SIZE,
947 .max_wm = G4X_MAX_WM,
948 .default_wm = G4X_MAX_WM,
949 .guard_size = 2,
950 .cacheline_size = G4X_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300951};
952static const struct intel_watermark_params g4x_cursor_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300953 .fifo_size = I965_CURSOR_FIFO,
954 .max_wm = I965_CURSOR_MAX_WM,
955 .default_wm = I965_CURSOR_DFT_WM,
956 .guard_size = 2,
957 .cacheline_size = G4X_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300958};
959static const struct intel_watermark_params valleyview_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300960 .fifo_size = VALLEYVIEW_FIFO_SIZE,
961 .max_wm = VALLEYVIEW_MAX_WM,
962 .default_wm = VALLEYVIEW_MAX_WM,
963 .guard_size = 2,
964 .cacheline_size = G4X_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300965};
966static const struct intel_watermark_params valleyview_cursor_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300967 .fifo_size = I965_CURSOR_FIFO,
968 .max_wm = VALLEYVIEW_CURSOR_MAX_WM,
969 .default_wm = I965_CURSOR_DFT_WM,
970 .guard_size = 2,
971 .cacheline_size = G4X_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300972};
973static const struct intel_watermark_params i965_cursor_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300974 .fifo_size = I965_CURSOR_FIFO,
975 .max_wm = I965_CURSOR_MAX_WM,
976 .default_wm = I965_CURSOR_DFT_WM,
977 .guard_size = 2,
978 .cacheline_size = I915_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300979};
980static const struct intel_watermark_params i945_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300981 .fifo_size = I945_FIFO_SIZE,
982 .max_wm = I915_MAX_WM,
983 .default_wm = 1,
984 .guard_size = 2,
985 .cacheline_size = I915_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300986};
987static const struct intel_watermark_params i915_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300988 .fifo_size = I915_FIFO_SIZE,
989 .max_wm = I915_MAX_WM,
990 .default_wm = 1,
991 .guard_size = 2,
992 .cacheline_size = I915_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300993};
Daniel Vetterfeb56b92013-12-14 20:38:30 -0200994static const struct intel_watermark_params i830_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300995 .fifo_size = I855GM_FIFO_SIZE,
996 .max_wm = I915_MAX_WM,
997 .default_wm = 1,
998 .guard_size = 2,
999 .cacheline_size = I830_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001000};
Daniel Vetterfeb56b92013-12-14 20:38:30 -02001001static const struct intel_watermark_params i845_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +03001002 .fifo_size = I830_FIFO_SIZE,
1003 .max_wm = I915_MAX_WM,
1004 .default_wm = 1,
1005 .guard_size = 2,
1006 .cacheline_size = I830_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001007};
1008
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001009/**
1010 * intel_calculate_wm - calculate watermark level
1011 * @clock_in_khz: pixel clock
1012 * @wm: chip FIFO params
1013 * @pixel_size: display pixel size
1014 * @latency_ns: memory latency for the platform
1015 *
1016 * Calculate the watermark level (the level at which the display plane will
1017 * start fetching from memory again). Each chip has a different display
1018 * FIFO size and allocation, so the caller needs to figure that out and pass
1019 * in the correct intel_watermark_params structure.
1020 *
1021 * As the pixel clock runs, the FIFO will be drained at a rate that depends
1022 * on the pixel size. When it reaches the watermark level, it'll start
1023 * fetching FIFO line sized based chunks from memory until the FIFO fills
1024 * past the watermark point. If the FIFO drains completely, a FIFO underrun
1025 * will occur, and a display engine hang could result.
1026 */
1027static unsigned long intel_calculate_wm(unsigned long clock_in_khz,
1028 const struct intel_watermark_params *wm,
1029 int fifo_size,
1030 int pixel_size,
1031 unsigned long latency_ns)
1032{
1033 long entries_required, wm_size;
1034
1035 /*
1036 * Note: we need to make sure we don't overflow for various clock &
1037 * latency values.
1038 * clocks go from a few thousand to several hundred thousand.
1039 * latency is usually a few thousand
1040 */
1041 entries_required = ((clock_in_khz / 1000) * pixel_size * latency_ns) /
1042 1000;
1043 entries_required = DIV_ROUND_UP(entries_required, wm->cacheline_size);
1044
1045 DRM_DEBUG_KMS("FIFO entries required for mode: %ld\n", entries_required);
1046
1047 wm_size = fifo_size - (entries_required + wm->guard_size);
1048
1049 DRM_DEBUG_KMS("FIFO watermark level: %ld\n", wm_size);
1050
1051 /* Don't promote wm_size to unsigned... */
1052 if (wm_size > (long)wm->max_wm)
1053 wm_size = wm->max_wm;
1054 if (wm_size <= 0)
1055 wm_size = wm->default_wm;
1056 return wm_size;
1057}
1058
1059static struct drm_crtc *single_enabled_crtc(struct drm_device *dev)
1060{
1061 struct drm_crtc *crtc, *enabled = NULL;
1062
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01001063 for_each_crtc(dev, crtc) {
Chris Wilson3490ea52013-01-07 10:11:40 +00001064 if (intel_crtc_active(crtc)) {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001065 if (enabled)
1066 return NULL;
1067 enabled = crtc;
1068 }
1069 }
1070
1071 return enabled;
1072}
1073
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001074static void pineview_update_wm(struct drm_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001075{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001076 struct drm_device *dev = unused_crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001077 struct drm_i915_private *dev_priv = dev->dev_private;
1078 struct drm_crtc *crtc;
1079 const struct cxsr_latency *latency;
1080 u32 reg;
1081 unsigned long wm;
1082
1083 latency = intel_get_cxsr_latency(IS_PINEVIEW_G(dev), dev_priv->is_ddr3,
1084 dev_priv->fsb_freq, dev_priv->mem_freq);
1085 if (!latency) {
1086 DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
Imre Deak5209b1f2014-07-01 12:36:17 +03001087 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001088 return;
1089 }
1090
1091 crtc = single_enabled_crtc(dev);
1092 if (crtc) {
Damien Lespiau241bfc32013-09-25 16:45:37 +01001093 const struct drm_display_mode *adjusted_mode;
Matt Roperf4510a22014-04-01 15:22:40 -07001094 int pixel_size = crtc->primary->fb->bits_per_pixel / 8;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001095 int clock;
1096
1097 adjusted_mode = &to_intel_crtc(crtc)->config.adjusted_mode;
1098 clock = adjusted_mode->crtc_clock;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001099
1100 /* Display SR */
1101 wm = intel_calculate_wm(clock, &pineview_display_wm,
1102 pineview_display_wm.fifo_size,
1103 pixel_size, latency->display_sr);
1104 reg = I915_READ(DSPFW1);
1105 reg &= ~DSPFW_SR_MASK;
1106 reg |= wm << DSPFW_SR_SHIFT;
1107 I915_WRITE(DSPFW1, reg);
1108 DRM_DEBUG_KMS("DSPFW1 register is %x\n", reg);
1109
1110 /* cursor SR */
1111 wm = intel_calculate_wm(clock, &pineview_cursor_wm,
1112 pineview_display_wm.fifo_size,
1113 pixel_size, latency->cursor_sr);
1114 reg = I915_READ(DSPFW3);
1115 reg &= ~DSPFW_CURSOR_SR_MASK;
1116 reg |= (wm & 0x3f) << DSPFW_CURSOR_SR_SHIFT;
1117 I915_WRITE(DSPFW3, reg);
1118
1119 /* Display HPLL off SR */
1120 wm = intel_calculate_wm(clock, &pineview_display_hplloff_wm,
1121 pineview_display_hplloff_wm.fifo_size,
1122 pixel_size, latency->display_hpll_disable);
1123 reg = I915_READ(DSPFW3);
1124 reg &= ~DSPFW_HPLL_SR_MASK;
1125 reg |= wm & DSPFW_HPLL_SR_MASK;
1126 I915_WRITE(DSPFW3, reg);
1127
1128 /* cursor HPLL off SR */
1129 wm = intel_calculate_wm(clock, &pineview_cursor_hplloff_wm,
1130 pineview_display_hplloff_wm.fifo_size,
1131 pixel_size, latency->cursor_hpll_disable);
1132 reg = I915_READ(DSPFW3);
1133 reg &= ~DSPFW_HPLL_CURSOR_MASK;
1134 reg |= (wm & 0x3f) << DSPFW_HPLL_CURSOR_SHIFT;
1135 I915_WRITE(DSPFW3, reg);
1136 DRM_DEBUG_KMS("DSPFW3 register is %x\n", reg);
1137
Imre Deak5209b1f2014-07-01 12:36:17 +03001138 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001139 } else {
Imre Deak5209b1f2014-07-01 12:36:17 +03001140 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001141 }
1142}
1143
1144static bool g4x_compute_wm0(struct drm_device *dev,
1145 int plane,
1146 const struct intel_watermark_params *display,
1147 int display_latency_ns,
1148 const struct intel_watermark_params *cursor,
1149 int cursor_latency_ns,
1150 int *plane_wm,
1151 int *cursor_wm)
1152{
1153 struct drm_crtc *crtc;
Ville Syrjälä4fe85902013-09-04 18:25:22 +03001154 const struct drm_display_mode *adjusted_mode;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001155 int htotal, hdisplay, clock, pixel_size;
1156 int line_time_us, line_count;
1157 int entries, tlb_miss;
1158
1159 crtc = intel_get_crtc_for_plane(dev, plane);
Chris Wilson3490ea52013-01-07 10:11:40 +00001160 if (!intel_crtc_active(crtc)) {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001161 *cursor_wm = cursor->guard_size;
1162 *plane_wm = display->guard_size;
1163 return false;
1164 }
1165
Ville Syrjälä4fe85902013-09-04 18:25:22 +03001166 adjusted_mode = &to_intel_crtc(crtc)->config.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001167 clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -08001168 htotal = adjusted_mode->crtc_htotal;
Ville Syrjälä37327ab2013-09-04 18:25:28 +03001169 hdisplay = to_intel_crtc(crtc)->config.pipe_src_w;
Matt Roperf4510a22014-04-01 15:22:40 -07001170 pixel_size = crtc->primary->fb->bits_per_pixel / 8;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001171
1172 /* Use the small buffer method to calculate plane watermark */
1173 entries = ((clock * pixel_size / 1000) * display_latency_ns) / 1000;
1174 tlb_miss = display->fifo_size*display->cacheline_size - hdisplay * 8;
1175 if (tlb_miss > 0)
1176 entries += tlb_miss;
1177 entries = DIV_ROUND_UP(entries, display->cacheline_size);
1178 *plane_wm = entries + display->guard_size;
1179 if (*plane_wm > (int)display->max_wm)
1180 *plane_wm = display->max_wm;
1181
1182 /* Use the large buffer method to calculate cursor watermark */
Ville Syrjälä922044c2014-02-14 14:18:57 +02001183 line_time_us = max(htotal * 1000 / clock, 1);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001184 line_count = (cursor_latency_ns / line_time_us + 1000) / 1000;
Chris Wilson7bb836d2014-03-26 12:38:14 +00001185 entries = line_count * to_intel_crtc(crtc)->cursor_width * pixel_size;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001186 tlb_miss = cursor->fifo_size*cursor->cacheline_size - hdisplay * 8;
1187 if (tlb_miss > 0)
1188 entries += tlb_miss;
1189 entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
1190 *cursor_wm = entries + cursor->guard_size;
1191 if (*cursor_wm > (int)cursor->max_wm)
1192 *cursor_wm = (int)cursor->max_wm;
1193
1194 return true;
1195}
1196
1197/*
1198 * Check the wm result.
1199 *
1200 * If any calculated watermark values is larger than the maximum value that
1201 * can be programmed into the associated watermark register, that watermark
1202 * must be disabled.
1203 */
1204static bool g4x_check_srwm(struct drm_device *dev,
1205 int display_wm, int cursor_wm,
1206 const struct intel_watermark_params *display,
1207 const struct intel_watermark_params *cursor)
1208{
1209 DRM_DEBUG_KMS("SR watermark: display plane %d, cursor %d\n",
1210 display_wm, cursor_wm);
1211
1212 if (display_wm > display->max_wm) {
1213 DRM_DEBUG_KMS("display watermark is too large(%d/%ld), disabling\n",
1214 display_wm, display->max_wm);
1215 return false;
1216 }
1217
1218 if (cursor_wm > cursor->max_wm) {
1219 DRM_DEBUG_KMS("cursor watermark is too large(%d/%ld), disabling\n",
1220 cursor_wm, cursor->max_wm);
1221 return false;
1222 }
1223
1224 if (!(display_wm || cursor_wm)) {
1225 DRM_DEBUG_KMS("SR latency is 0, disabling\n");
1226 return false;
1227 }
1228
1229 return true;
1230}
1231
1232static bool g4x_compute_srwm(struct drm_device *dev,
1233 int plane,
1234 int latency_ns,
1235 const struct intel_watermark_params *display,
1236 const struct intel_watermark_params *cursor,
1237 int *display_wm, int *cursor_wm)
1238{
1239 struct drm_crtc *crtc;
Ville Syrjälä4fe85902013-09-04 18:25:22 +03001240 const struct drm_display_mode *adjusted_mode;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001241 int hdisplay, htotal, pixel_size, clock;
1242 unsigned long line_time_us;
1243 int line_count, line_size;
1244 int small, large;
1245 int entries;
1246
1247 if (!latency_ns) {
1248 *display_wm = *cursor_wm = 0;
1249 return false;
1250 }
1251
1252 crtc = intel_get_crtc_for_plane(dev, plane);
Ville Syrjälä4fe85902013-09-04 18:25:22 +03001253 adjusted_mode = &to_intel_crtc(crtc)->config.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001254 clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -08001255 htotal = adjusted_mode->crtc_htotal;
Ville Syrjälä37327ab2013-09-04 18:25:28 +03001256 hdisplay = to_intel_crtc(crtc)->config.pipe_src_w;
Matt Roperf4510a22014-04-01 15:22:40 -07001257 pixel_size = crtc->primary->fb->bits_per_pixel / 8;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001258
Ville Syrjälä922044c2014-02-14 14:18:57 +02001259 line_time_us = max(htotal * 1000 / clock, 1);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001260 line_count = (latency_ns / line_time_us + 1000) / 1000;
1261 line_size = hdisplay * pixel_size;
1262
1263 /* Use the minimum of the small and large buffer method for primary */
1264 small = ((clock * pixel_size / 1000) * latency_ns) / 1000;
1265 large = line_count * line_size;
1266
1267 entries = DIV_ROUND_UP(min(small, large), display->cacheline_size);
1268 *display_wm = entries + display->guard_size;
1269
1270 /* calculate the self-refresh watermark for display cursor */
Chris Wilson7bb836d2014-03-26 12:38:14 +00001271 entries = line_count * pixel_size * to_intel_crtc(crtc)->cursor_width;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001272 entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
1273 *cursor_wm = entries + cursor->guard_size;
1274
1275 return g4x_check_srwm(dev,
1276 *display_wm, *cursor_wm,
1277 display, cursor);
1278}
1279
Gajanan Bhat0948c262014-08-07 01:58:24 +05301280static bool vlv_compute_drain_latency(struct drm_crtc *crtc,
1281 int pixel_size,
1282 int *prec_mult,
1283 int *drain_latency)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001284{
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001285 int entries;
Gajanan Bhat0948c262014-08-07 01:58:24 +05301286 int clock = to_intel_crtc(crtc)->config.adjusted_mode.crtc_clock;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001287
Gajanan Bhat0948c262014-08-07 01:58:24 +05301288 if (WARN(clock == 0, "Pixel clock is zero!\n"))
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001289 return false;
1290
Gajanan Bhat0948c262014-08-07 01:58:24 +05301291 if (WARN(pixel_size == 0, "Pixel size is zero!\n"))
1292 return false;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001293
Gajanan Bhata398e9c2014-08-05 23:15:54 +05301294 entries = DIV_ROUND_UP(clock, 1000) * pixel_size;
Gajanan Bhat0948c262014-08-07 01:58:24 +05301295 *prec_mult = (entries > 128) ? DRAIN_LATENCY_PRECISION_64 :
1296 DRAIN_LATENCY_PRECISION_32;
1297 *drain_latency = (64 * (*prec_mult) * 4) / entries;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001298
Gajanan Bhata398e9c2014-08-05 23:15:54 +05301299 if (*drain_latency > DRAIN_LATENCY_MASK)
1300 *drain_latency = DRAIN_LATENCY_MASK;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001301
1302 return true;
1303}
1304
1305/*
1306 * Update drain latency registers of memory arbiter
1307 *
1308 * Valleyview SoC has a new memory arbiter and needs drain latency registers
1309 * to be programmed. Each plane has a drain latency multiplier and a drain
1310 * latency value.
1311 */
1312
Gajanan Bhat41aad812014-07-16 18:24:03 +05301313static void vlv_update_drain_latency(struct drm_crtc *crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001314{
Gajanan Bhat0948c262014-08-07 01:58:24 +05301315 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
1316 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1317 int pixel_size;
1318 int drain_latency;
1319 enum pipe pipe = intel_crtc->pipe;
1320 int plane_prec, prec_mult, plane_dl;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001321
Gajanan Bhat0948c262014-08-07 01:58:24 +05301322 plane_dl = I915_READ(VLV_DDL(pipe)) & ~(DDL_PLANE_PRECISION_64 |
1323 DRAIN_LATENCY_MASK | DDL_CURSOR_PRECISION_64 |
1324 (DRAIN_LATENCY_MASK << DDL_CURSOR_SHIFT));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001325
Gajanan Bhat0948c262014-08-07 01:58:24 +05301326 if (!intel_crtc_active(crtc)) {
1327 I915_WRITE(VLV_DDL(pipe), plane_dl);
1328 return;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001329 }
1330
Gajanan Bhat0948c262014-08-07 01:58:24 +05301331 /* Primary plane Drain Latency */
1332 pixel_size = crtc->primary->fb->bits_per_pixel / 8; /* BPP */
1333 if (vlv_compute_drain_latency(crtc, pixel_size, &prec_mult, &drain_latency)) {
1334 plane_prec = (prec_mult == DRAIN_LATENCY_PRECISION_64) ?
1335 DDL_PLANE_PRECISION_64 :
1336 DDL_PLANE_PRECISION_32;
1337 plane_dl |= plane_prec | drain_latency;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001338 }
Gajanan Bhat0948c262014-08-07 01:58:24 +05301339
1340 /* Cursor Drain Latency
1341 * BPP is always 4 for cursor
1342 */
1343 pixel_size = 4;
1344
1345 /* Program cursor DL only if it is enabled */
1346 if (intel_crtc->cursor_base &&
1347 vlv_compute_drain_latency(crtc, pixel_size, &prec_mult, &drain_latency)) {
1348 plane_prec = (prec_mult == DRAIN_LATENCY_PRECISION_64) ?
1349 DDL_CURSOR_PRECISION_64 :
1350 DDL_CURSOR_PRECISION_32;
1351 plane_dl |= plane_prec | (drain_latency << DDL_CURSOR_SHIFT);
1352 }
1353
1354 I915_WRITE(VLV_DDL(pipe), plane_dl);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001355}
1356
1357#define single_plane_enabled(mask) is_power_of_2(mask)
1358
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001359static void valleyview_update_wm(struct drm_crtc *crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001360{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001361 struct drm_device *dev = crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001362 static const int sr_latency_ns = 12000;
1363 struct drm_i915_private *dev_priv = dev->dev_private;
1364 int planea_wm, planeb_wm, cursora_wm, cursorb_wm;
1365 int plane_sr, cursor_sr;
Chris Wilsonaf6c4572012-12-11 12:01:43 +00001366 int ignore_plane_sr, ignore_cursor_sr;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001367 unsigned int enabled = 0;
Imre Deak98584252014-06-13 14:54:20 +03001368 bool cxsr_enabled;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001369
Gajanan Bhat41aad812014-07-16 18:24:03 +05301370 vlv_update_drain_latency(crtc);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001371
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001372 if (g4x_compute_wm0(dev, PIPE_A,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001373 &valleyview_wm_info, latency_ns,
1374 &valleyview_cursor_wm_info, latency_ns,
1375 &planea_wm, &cursora_wm))
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001376 enabled |= 1 << PIPE_A;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001377
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001378 if (g4x_compute_wm0(dev, PIPE_B,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001379 &valleyview_wm_info, latency_ns,
1380 &valleyview_cursor_wm_info, latency_ns,
1381 &planeb_wm, &cursorb_wm))
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001382 enabled |= 1 << PIPE_B;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001383
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001384 if (single_plane_enabled(enabled) &&
1385 g4x_compute_srwm(dev, ffs(enabled) - 1,
1386 sr_latency_ns,
1387 &valleyview_wm_info,
1388 &valleyview_cursor_wm_info,
Chris Wilsonaf6c4572012-12-11 12:01:43 +00001389 &plane_sr, &ignore_cursor_sr) &&
1390 g4x_compute_srwm(dev, ffs(enabled) - 1,
1391 2*sr_latency_ns,
1392 &valleyview_wm_info,
1393 &valleyview_cursor_wm_info,
Chris Wilson52bd02d2012-12-07 10:43:24 +00001394 &ignore_plane_sr, &cursor_sr)) {
Imre Deak98584252014-06-13 14:54:20 +03001395 cxsr_enabled = true;
Chris Wilson52bd02d2012-12-07 10:43:24 +00001396 } else {
Imre Deak98584252014-06-13 14:54:20 +03001397 cxsr_enabled = false;
Imre Deak5209b1f2014-07-01 12:36:17 +03001398 intel_set_memory_cxsr(dev_priv, false);
Chris Wilson52bd02d2012-12-07 10:43:24 +00001399 plane_sr = cursor_sr = 0;
1400 }
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001401
Ville Syrjäläa5043452014-06-28 02:04:18 +03001402 DRM_DEBUG_KMS("Setting FIFO watermarks - A: plane=%d, cursor=%d, "
1403 "B: plane=%d, cursor=%d, SR: plane=%d, cursor=%d\n",
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001404 planea_wm, cursora_wm,
1405 planeb_wm, cursorb_wm,
1406 plane_sr, cursor_sr);
1407
1408 I915_WRITE(DSPFW1,
1409 (plane_sr << DSPFW_SR_SHIFT) |
1410 (cursorb_wm << DSPFW_CURSORB_SHIFT) |
1411 (planeb_wm << DSPFW_PLANEB_SHIFT) |
Ville Syrjälä0a560672014-06-11 16:51:18 +03001412 (planea_wm << DSPFW_PLANEA_SHIFT));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001413 I915_WRITE(DSPFW2,
Chris Wilson8c919b22012-12-04 16:33:19 +00001414 (I915_READ(DSPFW2) & ~DSPFW_CURSORA_MASK) |
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001415 (cursora_wm << DSPFW_CURSORA_SHIFT));
1416 I915_WRITE(DSPFW3,
Chris Wilson8c919b22012-12-04 16:33:19 +00001417 (I915_READ(DSPFW3) & ~DSPFW_CURSOR_SR_MASK) |
1418 (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
Imre Deak98584252014-06-13 14:54:20 +03001419
1420 if (cxsr_enabled)
1421 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001422}
1423
Ville Syrjälä3c2777f2014-06-26 17:03:06 +03001424static void cherryview_update_wm(struct drm_crtc *crtc)
1425{
1426 struct drm_device *dev = crtc->dev;
1427 static const int sr_latency_ns = 12000;
1428 struct drm_i915_private *dev_priv = dev->dev_private;
1429 int planea_wm, planeb_wm, planec_wm;
1430 int cursora_wm, cursorb_wm, cursorc_wm;
1431 int plane_sr, cursor_sr;
1432 int ignore_plane_sr, ignore_cursor_sr;
1433 unsigned int enabled = 0;
1434 bool cxsr_enabled;
1435
1436 vlv_update_drain_latency(crtc);
1437
1438 if (g4x_compute_wm0(dev, PIPE_A,
1439 &valleyview_wm_info, latency_ns,
1440 &valleyview_cursor_wm_info, latency_ns,
1441 &planea_wm, &cursora_wm))
1442 enabled |= 1 << PIPE_A;
1443
1444 if (g4x_compute_wm0(dev, PIPE_B,
1445 &valleyview_wm_info, latency_ns,
1446 &valleyview_cursor_wm_info, latency_ns,
1447 &planeb_wm, &cursorb_wm))
1448 enabled |= 1 << PIPE_B;
1449
1450 if (g4x_compute_wm0(dev, PIPE_C,
1451 &valleyview_wm_info, latency_ns,
1452 &valleyview_cursor_wm_info, latency_ns,
1453 &planec_wm, &cursorc_wm))
1454 enabled |= 1 << PIPE_C;
1455
1456 if (single_plane_enabled(enabled) &&
1457 g4x_compute_srwm(dev, ffs(enabled) - 1,
1458 sr_latency_ns,
1459 &valleyview_wm_info,
1460 &valleyview_cursor_wm_info,
1461 &plane_sr, &ignore_cursor_sr) &&
1462 g4x_compute_srwm(dev, ffs(enabled) - 1,
1463 2*sr_latency_ns,
1464 &valleyview_wm_info,
1465 &valleyview_cursor_wm_info,
1466 &ignore_plane_sr, &cursor_sr)) {
1467 cxsr_enabled = true;
1468 } else {
1469 cxsr_enabled = false;
1470 intel_set_memory_cxsr(dev_priv, false);
1471 plane_sr = cursor_sr = 0;
1472 }
1473
1474 DRM_DEBUG_KMS("Setting FIFO watermarks - A: plane=%d, cursor=%d, "
1475 "B: plane=%d, cursor=%d, C: plane=%d, cursor=%d, "
1476 "SR: plane=%d, cursor=%d\n",
1477 planea_wm, cursora_wm,
1478 planeb_wm, cursorb_wm,
1479 planec_wm, cursorc_wm,
1480 plane_sr, cursor_sr);
1481
1482 I915_WRITE(DSPFW1,
1483 (plane_sr << DSPFW_SR_SHIFT) |
1484 (cursorb_wm << DSPFW_CURSORB_SHIFT) |
1485 (planeb_wm << DSPFW_PLANEB_SHIFT) |
1486 (planea_wm << DSPFW_PLANEA_SHIFT));
1487 I915_WRITE(DSPFW2,
1488 (I915_READ(DSPFW2) & ~DSPFW_CURSORA_MASK) |
1489 (cursora_wm << DSPFW_CURSORA_SHIFT));
1490 I915_WRITE(DSPFW3,
1491 (I915_READ(DSPFW3) & ~DSPFW_CURSOR_SR_MASK) |
1492 (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
1493 I915_WRITE(DSPFW9_CHV,
1494 (I915_READ(DSPFW9_CHV) & ~(DSPFW_PLANEC_MASK |
1495 DSPFW_CURSORC_MASK)) |
1496 (planec_wm << DSPFW_PLANEC_SHIFT) |
1497 (cursorc_wm << DSPFW_CURSORC_SHIFT));
1498
1499 if (cxsr_enabled)
1500 intel_set_memory_cxsr(dev_priv, true);
1501}
1502
Gajanan Bhat01e184c2014-08-07 17:03:30 +05301503static void valleyview_update_sprite_wm(struct drm_plane *plane,
1504 struct drm_crtc *crtc,
1505 uint32_t sprite_width,
1506 uint32_t sprite_height,
1507 int pixel_size,
1508 bool enabled, bool scaled)
1509{
1510 struct drm_device *dev = crtc->dev;
1511 struct drm_i915_private *dev_priv = dev->dev_private;
1512 int pipe = to_intel_plane(plane)->pipe;
1513 int sprite = to_intel_plane(plane)->plane;
1514 int drain_latency;
1515 int plane_prec;
1516 int sprite_dl;
1517 int prec_mult;
1518
1519 sprite_dl = I915_READ(VLV_DDL(pipe)) & ~(DDL_SPRITE_PRECISION_64(sprite) |
1520 (DRAIN_LATENCY_MASK << DDL_SPRITE_SHIFT(sprite)));
1521
1522 if (enabled && vlv_compute_drain_latency(crtc, pixel_size, &prec_mult,
1523 &drain_latency)) {
1524 plane_prec = (prec_mult == DRAIN_LATENCY_PRECISION_64) ?
1525 DDL_SPRITE_PRECISION_64(sprite) :
1526 DDL_SPRITE_PRECISION_32(sprite);
1527 sprite_dl |= plane_prec |
1528 (drain_latency << DDL_SPRITE_SHIFT(sprite));
1529 }
1530
1531 I915_WRITE(VLV_DDL(pipe), sprite_dl);
1532}
1533
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001534static void g4x_update_wm(struct drm_crtc *crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001535{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001536 struct drm_device *dev = crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001537 static const int sr_latency_ns = 12000;
1538 struct drm_i915_private *dev_priv = dev->dev_private;
1539 int planea_wm, planeb_wm, cursora_wm, cursorb_wm;
1540 int plane_sr, cursor_sr;
1541 unsigned int enabled = 0;
Imre Deak98584252014-06-13 14:54:20 +03001542 bool cxsr_enabled;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001543
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001544 if (g4x_compute_wm0(dev, PIPE_A,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001545 &g4x_wm_info, latency_ns,
1546 &g4x_cursor_wm_info, latency_ns,
1547 &planea_wm, &cursora_wm))
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001548 enabled |= 1 << PIPE_A;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001549
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001550 if (g4x_compute_wm0(dev, PIPE_B,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001551 &g4x_wm_info, latency_ns,
1552 &g4x_cursor_wm_info, latency_ns,
1553 &planeb_wm, &cursorb_wm))
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001554 enabled |= 1 << PIPE_B;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001555
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001556 if (single_plane_enabled(enabled) &&
1557 g4x_compute_srwm(dev, ffs(enabled) - 1,
1558 sr_latency_ns,
1559 &g4x_wm_info,
1560 &g4x_cursor_wm_info,
Chris Wilson52bd02d2012-12-07 10:43:24 +00001561 &plane_sr, &cursor_sr)) {
Imre Deak98584252014-06-13 14:54:20 +03001562 cxsr_enabled = true;
Chris Wilson52bd02d2012-12-07 10:43:24 +00001563 } else {
Imre Deak98584252014-06-13 14:54:20 +03001564 cxsr_enabled = false;
Imre Deak5209b1f2014-07-01 12:36:17 +03001565 intel_set_memory_cxsr(dev_priv, false);
Chris Wilson52bd02d2012-12-07 10:43:24 +00001566 plane_sr = cursor_sr = 0;
1567 }
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001568
Ville Syrjäläa5043452014-06-28 02:04:18 +03001569 DRM_DEBUG_KMS("Setting FIFO watermarks - A: plane=%d, cursor=%d, "
1570 "B: plane=%d, cursor=%d, SR: plane=%d, cursor=%d\n",
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001571 planea_wm, cursora_wm,
1572 planeb_wm, cursorb_wm,
1573 plane_sr, cursor_sr);
1574
1575 I915_WRITE(DSPFW1,
1576 (plane_sr << DSPFW_SR_SHIFT) |
1577 (cursorb_wm << DSPFW_CURSORB_SHIFT) |
1578 (planeb_wm << DSPFW_PLANEB_SHIFT) |
Ville Syrjälä0a560672014-06-11 16:51:18 +03001579 (planea_wm << DSPFW_PLANEA_SHIFT));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001580 I915_WRITE(DSPFW2,
Chris Wilson8c919b22012-12-04 16:33:19 +00001581 (I915_READ(DSPFW2) & ~DSPFW_CURSORA_MASK) |
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001582 (cursora_wm << DSPFW_CURSORA_SHIFT));
1583 /* HPLL off in SR has some issues on G4x... disable it */
1584 I915_WRITE(DSPFW3,
Chris Wilson8c919b22012-12-04 16:33:19 +00001585 (I915_READ(DSPFW3) & ~(DSPFW_HPLL_SR_EN | DSPFW_CURSOR_SR_MASK)) |
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001586 (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
Imre Deak98584252014-06-13 14:54:20 +03001587
1588 if (cxsr_enabled)
1589 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001590}
1591
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001592static void i965_update_wm(struct drm_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001593{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001594 struct drm_device *dev = unused_crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001595 struct drm_i915_private *dev_priv = dev->dev_private;
1596 struct drm_crtc *crtc;
1597 int srwm = 1;
1598 int cursor_sr = 16;
Imre Deak98584252014-06-13 14:54:20 +03001599 bool cxsr_enabled;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001600
1601 /* Calc sr entries for one plane configs */
1602 crtc = single_enabled_crtc(dev);
1603 if (crtc) {
1604 /* self-refresh has much higher latency */
1605 static const int sr_latency_ns = 12000;
Ville Syrjälä4fe85902013-09-04 18:25:22 +03001606 const struct drm_display_mode *adjusted_mode =
1607 &to_intel_crtc(crtc)->config.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001608 int clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -08001609 int htotal = adjusted_mode->crtc_htotal;
Ville Syrjälä37327ab2013-09-04 18:25:28 +03001610 int hdisplay = to_intel_crtc(crtc)->config.pipe_src_w;
Matt Roperf4510a22014-04-01 15:22:40 -07001611 int pixel_size = crtc->primary->fb->bits_per_pixel / 8;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001612 unsigned long line_time_us;
1613 int entries;
1614
Ville Syrjälä922044c2014-02-14 14:18:57 +02001615 line_time_us = max(htotal * 1000 / clock, 1);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001616
1617 /* Use ns/us then divide to preserve precision */
1618 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
1619 pixel_size * hdisplay;
1620 entries = DIV_ROUND_UP(entries, I915_FIFO_LINE_SIZE);
1621 srwm = I965_FIFO_SIZE - entries;
1622 if (srwm < 0)
1623 srwm = 1;
1624 srwm &= 0x1ff;
1625 DRM_DEBUG_KMS("self-refresh entries: %d, wm: %d\n",
1626 entries, srwm);
1627
1628 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
Chris Wilson7bb836d2014-03-26 12:38:14 +00001629 pixel_size * to_intel_crtc(crtc)->cursor_width;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001630 entries = DIV_ROUND_UP(entries,
1631 i965_cursor_wm_info.cacheline_size);
1632 cursor_sr = i965_cursor_wm_info.fifo_size -
1633 (entries + i965_cursor_wm_info.guard_size);
1634
1635 if (cursor_sr > i965_cursor_wm_info.max_wm)
1636 cursor_sr = i965_cursor_wm_info.max_wm;
1637
1638 DRM_DEBUG_KMS("self-refresh watermark: display plane %d "
1639 "cursor %d\n", srwm, cursor_sr);
1640
Imre Deak98584252014-06-13 14:54:20 +03001641 cxsr_enabled = true;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001642 } else {
Imre Deak98584252014-06-13 14:54:20 +03001643 cxsr_enabled = false;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001644 /* Turn off self refresh if both pipes are enabled */
Imre Deak5209b1f2014-07-01 12:36:17 +03001645 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001646 }
1647
1648 DRM_DEBUG_KMS("Setting FIFO watermarks - A: 8, B: 8, C: 8, SR %d\n",
1649 srwm);
1650
1651 /* 965 has limitations... */
1652 I915_WRITE(DSPFW1, (srwm << DSPFW_SR_SHIFT) |
Ville Syrjälä0a560672014-06-11 16:51:18 +03001653 (8 << DSPFW_CURSORB_SHIFT) |
1654 (8 << DSPFW_PLANEB_SHIFT) |
1655 (8 << DSPFW_PLANEA_SHIFT));
1656 I915_WRITE(DSPFW2, (8 << DSPFW_CURSORA_SHIFT) |
1657 (8 << DSPFW_PLANEC_SHIFT_OLD));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001658 /* update cursor SR watermark */
1659 I915_WRITE(DSPFW3, (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
Imre Deak98584252014-06-13 14:54:20 +03001660
1661 if (cxsr_enabled)
1662 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001663}
1664
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001665static void i9xx_update_wm(struct drm_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001666{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001667 struct drm_device *dev = unused_crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001668 struct drm_i915_private *dev_priv = dev->dev_private;
1669 const struct intel_watermark_params *wm_info;
1670 uint32_t fwater_lo;
1671 uint32_t fwater_hi;
1672 int cwm, srwm = 1;
1673 int fifo_size;
1674 int planea_wm, planeb_wm;
1675 struct drm_crtc *crtc, *enabled = NULL;
1676
1677 if (IS_I945GM(dev))
1678 wm_info = &i945_wm_info;
1679 else if (!IS_GEN2(dev))
1680 wm_info = &i915_wm_info;
1681 else
Daniel Vetterfeb56b92013-12-14 20:38:30 -02001682 wm_info = &i830_wm_info;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001683
1684 fifo_size = dev_priv->display.get_fifo_size(dev, 0);
1685 crtc = intel_get_crtc_for_plane(dev, 0);
Chris Wilson3490ea52013-01-07 10:11:40 +00001686 if (intel_crtc_active(crtc)) {
Damien Lespiau241bfc32013-09-25 16:45:37 +01001687 const struct drm_display_mode *adjusted_mode;
Matt Roperf4510a22014-04-01 15:22:40 -07001688 int cpp = crtc->primary->fb->bits_per_pixel / 8;
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001689 if (IS_GEN2(dev))
1690 cpp = 4;
1691
Damien Lespiau241bfc32013-09-25 16:45:37 +01001692 adjusted_mode = &to_intel_crtc(crtc)->config.adjusted_mode;
1693 planea_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001694 wm_info, fifo_size, cpp,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001695 latency_ns);
1696 enabled = crtc;
1697 } else
1698 planea_wm = fifo_size - wm_info->guard_size;
1699
1700 fifo_size = dev_priv->display.get_fifo_size(dev, 1);
1701 crtc = intel_get_crtc_for_plane(dev, 1);
Chris Wilson3490ea52013-01-07 10:11:40 +00001702 if (intel_crtc_active(crtc)) {
Damien Lespiau241bfc32013-09-25 16:45:37 +01001703 const struct drm_display_mode *adjusted_mode;
Matt Roperf4510a22014-04-01 15:22:40 -07001704 int cpp = crtc->primary->fb->bits_per_pixel / 8;
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001705 if (IS_GEN2(dev))
1706 cpp = 4;
1707
Damien Lespiau241bfc32013-09-25 16:45:37 +01001708 adjusted_mode = &to_intel_crtc(crtc)->config.adjusted_mode;
1709 planeb_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001710 wm_info, fifo_size, cpp,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001711 latency_ns);
1712 if (enabled == NULL)
1713 enabled = crtc;
1714 else
1715 enabled = NULL;
1716 } else
1717 planeb_wm = fifo_size - wm_info->guard_size;
1718
1719 DRM_DEBUG_KMS("FIFO watermarks - A: %d, B: %d\n", planea_wm, planeb_wm);
1720
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02001721 if (IS_I915GM(dev) && enabled) {
Matt Roper2ff8fde2014-07-08 07:50:07 -07001722 struct drm_i915_gem_object *obj;
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02001723
Matt Roper2ff8fde2014-07-08 07:50:07 -07001724 obj = intel_fb_obj(enabled->primary->fb);
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02001725
1726 /* self-refresh seems busted with untiled */
Matt Roper2ff8fde2014-07-08 07:50:07 -07001727 if (obj->tiling_mode == I915_TILING_NONE)
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02001728 enabled = NULL;
1729 }
1730
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001731 /*
1732 * Overlay gets an aggressive default since video jitter is bad.
1733 */
1734 cwm = 2;
1735
1736 /* Play safe and disable self-refresh before adjusting watermarks. */
Imre Deak5209b1f2014-07-01 12:36:17 +03001737 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001738
1739 /* Calc sr entries for one plane configs */
1740 if (HAS_FW_BLC(dev) && enabled) {
1741 /* self-refresh has much higher latency */
1742 static const int sr_latency_ns = 6000;
Ville Syrjälä4fe85902013-09-04 18:25:22 +03001743 const struct drm_display_mode *adjusted_mode =
1744 &to_intel_crtc(enabled)->config.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001745 int clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -08001746 int htotal = adjusted_mode->crtc_htotal;
Daniel Vetterf727b492013-11-20 15:02:10 +01001747 int hdisplay = to_intel_crtc(enabled)->config.pipe_src_w;
Matt Roperf4510a22014-04-01 15:22:40 -07001748 int pixel_size = enabled->primary->fb->bits_per_pixel / 8;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001749 unsigned long line_time_us;
1750 int entries;
1751
Ville Syrjälä922044c2014-02-14 14:18:57 +02001752 line_time_us = max(htotal * 1000 / clock, 1);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001753
1754 /* Use ns/us then divide to preserve precision */
1755 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
1756 pixel_size * hdisplay;
1757 entries = DIV_ROUND_UP(entries, wm_info->cacheline_size);
1758 DRM_DEBUG_KMS("self-refresh entries: %d\n", entries);
1759 srwm = wm_info->fifo_size - entries;
1760 if (srwm < 0)
1761 srwm = 1;
1762
1763 if (IS_I945G(dev) || IS_I945GM(dev))
1764 I915_WRITE(FW_BLC_SELF,
1765 FW_BLC_SELF_FIFO_MASK | (srwm & 0xff));
1766 else if (IS_I915GM(dev))
1767 I915_WRITE(FW_BLC_SELF, srwm & 0x3f);
1768 }
1769
1770 DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d, B: %d, C: %d, SR %d\n",
1771 planea_wm, planeb_wm, cwm, srwm);
1772
1773 fwater_lo = ((planeb_wm & 0x3f) << 16) | (planea_wm & 0x3f);
1774 fwater_hi = (cwm & 0x1f);
1775
1776 /* Set request length to 8 cachelines per fetch */
1777 fwater_lo = fwater_lo | (1 << 24) | (1 << 8);
1778 fwater_hi = fwater_hi | (1 << 8);
1779
1780 I915_WRITE(FW_BLC, fwater_lo);
1781 I915_WRITE(FW_BLC2, fwater_hi);
1782
Imre Deak5209b1f2014-07-01 12:36:17 +03001783 if (enabled)
1784 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001785}
1786
Daniel Vetterfeb56b92013-12-14 20:38:30 -02001787static void i845_update_wm(struct drm_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001788{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001789 struct drm_device *dev = unused_crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001790 struct drm_i915_private *dev_priv = dev->dev_private;
1791 struct drm_crtc *crtc;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001792 const struct drm_display_mode *adjusted_mode;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001793 uint32_t fwater_lo;
1794 int planea_wm;
1795
1796 crtc = single_enabled_crtc(dev);
1797 if (crtc == NULL)
1798 return;
1799
Damien Lespiau241bfc32013-09-25 16:45:37 +01001800 adjusted_mode = &to_intel_crtc(crtc)->config.adjusted_mode;
1801 planea_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Daniel Vetterfeb56b92013-12-14 20:38:30 -02001802 &i845_wm_info,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001803 dev_priv->display.get_fifo_size(dev, 0),
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001804 4, latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001805 fwater_lo = I915_READ(FW_BLC) & ~0xfff;
1806 fwater_lo |= (3<<8) | planea_wm;
1807
1808 DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d\n", planea_wm);
1809
1810 I915_WRITE(FW_BLC, fwater_lo);
1811}
1812
Ville Syrjälä36587292013-07-05 11:57:16 +03001813static uint32_t ilk_pipe_pixel_rate(struct drm_device *dev,
1814 struct drm_crtc *crtc)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001815{
1816 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilsonfd4daa92013-08-27 17:04:17 +01001817 uint32_t pixel_rate;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001818
Damien Lespiau241bfc32013-09-25 16:45:37 +01001819 pixel_rate = intel_crtc->config.adjusted_mode.crtc_clock;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001820
1821 /* We only use IF-ID interlacing. If we ever use PF-ID we'll need to
1822 * adjust the pixel_rate here. */
1823
Chris Wilsonfd4daa92013-08-27 17:04:17 +01001824 if (intel_crtc->config.pch_pfit.enabled) {
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001825 uint64_t pipe_w, pipe_h, pfit_w, pfit_h;
Chris Wilsonfd4daa92013-08-27 17:04:17 +01001826 uint32_t pfit_size = intel_crtc->config.pch_pfit.size;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001827
Ville Syrjälä37327ab2013-09-04 18:25:28 +03001828 pipe_w = intel_crtc->config.pipe_src_w;
1829 pipe_h = intel_crtc->config.pipe_src_h;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001830 pfit_w = (pfit_size >> 16) & 0xFFFF;
1831 pfit_h = pfit_size & 0xFFFF;
1832 if (pipe_w < pfit_w)
1833 pipe_w = pfit_w;
1834 if (pipe_h < pfit_h)
1835 pipe_h = pfit_h;
1836
1837 pixel_rate = div_u64((uint64_t) pixel_rate * pipe_w * pipe_h,
1838 pfit_w * pfit_h);
1839 }
1840
1841 return pixel_rate;
1842}
1843
Ville Syrjälä37126462013-08-01 16:18:55 +03001844/* latency must be in 0.1us units. */
Ville Syrjälä23297042013-07-05 11:57:17 +03001845static uint32_t ilk_wm_method1(uint32_t pixel_rate, uint8_t bytes_per_pixel,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001846 uint32_t latency)
1847{
1848 uint64_t ret;
1849
Ville Syrjälä3312ba62013-08-01 16:18:53 +03001850 if (WARN(latency == 0, "Latency value missing\n"))
1851 return UINT_MAX;
1852
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001853 ret = (uint64_t) pixel_rate * bytes_per_pixel * latency;
1854 ret = DIV_ROUND_UP_ULL(ret, 64 * 10000) + 2;
1855
1856 return ret;
1857}
1858
Ville Syrjälä37126462013-08-01 16:18:55 +03001859/* latency must be in 0.1us units. */
Ville Syrjälä23297042013-07-05 11:57:17 +03001860static uint32_t ilk_wm_method2(uint32_t pixel_rate, uint32_t pipe_htotal,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001861 uint32_t horiz_pixels, uint8_t bytes_per_pixel,
1862 uint32_t latency)
1863{
1864 uint32_t ret;
1865
Ville Syrjälä3312ba62013-08-01 16:18:53 +03001866 if (WARN(latency == 0, "Latency value missing\n"))
1867 return UINT_MAX;
1868
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001869 ret = (latency * pixel_rate) / (pipe_htotal * 10000);
1870 ret = (ret + 1) * horiz_pixels * bytes_per_pixel;
1871 ret = DIV_ROUND_UP(ret, 64) + 2;
1872 return ret;
1873}
1874
Ville Syrjälä23297042013-07-05 11:57:17 +03001875static uint32_t ilk_wm_fbc(uint32_t pri_val, uint32_t horiz_pixels,
Paulo Zanonicca32e92013-05-31 11:45:06 -03001876 uint8_t bytes_per_pixel)
1877{
1878 return DIV_ROUND_UP(pri_val * 64, horiz_pixels * bytes_per_pixel) + 2;
1879}
1880
Imre Deak820c1982013-12-17 14:46:36 +02001881struct ilk_pipe_wm_parameters {
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001882 bool active;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001883 uint32_t pipe_htotal;
1884 uint32_t pixel_rate;
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001885 struct intel_plane_wm_parameters pri;
1886 struct intel_plane_wm_parameters spr;
1887 struct intel_plane_wm_parameters cur;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001888};
1889
Imre Deak820c1982013-12-17 14:46:36 +02001890struct ilk_wm_maximums {
Paulo Zanonicca32e92013-05-31 11:45:06 -03001891 uint16_t pri;
1892 uint16_t spr;
1893 uint16_t cur;
1894 uint16_t fbc;
1895};
1896
Ville Syrjälä240264f2013-08-07 13:29:12 +03001897/* used in computing the new watermarks state */
1898struct intel_wm_config {
1899 unsigned int num_pipes_active;
1900 bool sprites_enabled;
1901 bool sprites_scaled;
Ville Syrjälä240264f2013-08-07 13:29:12 +03001902};
1903
Ville Syrjälä37126462013-08-01 16:18:55 +03001904/*
1905 * For both WM_PIPE and WM_LP.
1906 * mem_value must be in 0.1us units.
1907 */
Imre Deak820c1982013-12-17 14:46:36 +02001908static uint32_t ilk_compute_pri_wm(const struct ilk_pipe_wm_parameters *params,
Paulo Zanonicca32e92013-05-31 11:45:06 -03001909 uint32_t mem_value,
1910 bool is_lp)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001911{
Paulo Zanonicca32e92013-05-31 11:45:06 -03001912 uint32_t method1, method2;
1913
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001914 if (!params->active || !params->pri.enabled)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001915 return 0;
1916
Ville Syrjälä23297042013-07-05 11:57:17 +03001917 method1 = ilk_wm_method1(params->pixel_rate,
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001918 params->pri.bytes_per_pixel,
Paulo Zanonicca32e92013-05-31 11:45:06 -03001919 mem_value);
1920
1921 if (!is_lp)
1922 return method1;
1923
Ville Syrjälä23297042013-07-05 11:57:17 +03001924 method2 = ilk_wm_method2(params->pixel_rate,
Paulo Zanonicca32e92013-05-31 11:45:06 -03001925 params->pipe_htotal,
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001926 params->pri.horiz_pixels,
1927 params->pri.bytes_per_pixel,
Paulo Zanonicca32e92013-05-31 11:45:06 -03001928 mem_value);
1929
1930 return min(method1, method2);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001931}
1932
Ville Syrjälä37126462013-08-01 16:18:55 +03001933/*
1934 * For both WM_PIPE and WM_LP.
1935 * mem_value must be in 0.1us units.
1936 */
Imre Deak820c1982013-12-17 14:46:36 +02001937static uint32_t ilk_compute_spr_wm(const struct ilk_pipe_wm_parameters *params,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001938 uint32_t mem_value)
1939{
1940 uint32_t method1, method2;
1941
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001942 if (!params->active || !params->spr.enabled)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001943 return 0;
1944
Ville Syrjälä23297042013-07-05 11:57:17 +03001945 method1 = ilk_wm_method1(params->pixel_rate,
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001946 params->spr.bytes_per_pixel,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001947 mem_value);
Ville Syrjälä23297042013-07-05 11:57:17 +03001948 method2 = ilk_wm_method2(params->pixel_rate,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001949 params->pipe_htotal,
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001950 params->spr.horiz_pixels,
1951 params->spr.bytes_per_pixel,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001952 mem_value);
1953 return min(method1, method2);
1954}
1955
Ville Syrjälä37126462013-08-01 16:18:55 +03001956/*
1957 * For both WM_PIPE and WM_LP.
1958 * mem_value must be in 0.1us units.
1959 */
Imre Deak820c1982013-12-17 14:46:36 +02001960static uint32_t ilk_compute_cur_wm(const struct ilk_pipe_wm_parameters *params,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001961 uint32_t mem_value)
1962{
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001963 if (!params->active || !params->cur.enabled)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001964 return 0;
1965
Ville Syrjälä23297042013-07-05 11:57:17 +03001966 return ilk_wm_method2(params->pixel_rate,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001967 params->pipe_htotal,
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001968 params->cur.horiz_pixels,
1969 params->cur.bytes_per_pixel,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001970 mem_value);
1971}
1972
Paulo Zanonicca32e92013-05-31 11:45:06 -03001973/* Only for WM_LP. */
Imre Deak820c1982013-12-17 14:46:36 +02001974static uint32_t ilk_compute_fbc_wm(const struct ilk_pipe_wm_parameters *params,
Ville Syrjälä1fda9882013-07-05 11:57:19 +03001975 uint32_t pri_val)
Paulo Zanonicca32e92013-05-31 11:45:06 -03001976{
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001977 if (!params->active || !params->pri.enabled)
Paulo Zanonicca32e92013-05-31 11:45:06 -03001978 return 0;
1979
Ville Syrjälä23297042013-07-05 11:57:17 +03001980 return ilk_wm_fbc(pri_val,
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001981 params->pri.horiz_pixels,
1982 params->pri.bytes_per_pixel);
Paulo Zanonicca32e92013-05-31 11:45:06 -03001983}
1984
Ville Syrjälä158ae642013-08-07 13:28:19 +03001985static unsigned int ilk_display_fifo_size(const struct drm_device *dev)
1986{
Ville Syrjälä416f4722013-11-02 21:07:46 -07001987 if (INTEL_INFO(dev)->gen >= 8)
1988 return 3072;
1989 else if (INTEL_INFO(dev)->gen >= 7)
Ville Syrjälä158ae642013-08-07 13:28:19 +03001990 return 768;
1991 else
1992 return 512;
1993}
1994
Ville Syrjälä4e975082014-03-07 18:32:11 +02001995static unsigned int ilk_plane_wm_reg_max(const struct drm_device *dev,
1996 int level, bool is_sprite)
1997{
1998 if (INTEL_INFO(dev)->gen >= 8)
1999 /* BDW primary/sprite plane watermarks */
2000 return level == 0 ? 255 : 2047;
2001 else if (INTEL_INFO(dev)->gen >= 7)
2002 /* IVB/HSW primary/sprite plane watermarks */
2003 return level == 0 ? 127 : 1023;
2004 else if (!is_sprite)
2005 /* ILK/SNB primary plane watermarks */
2006 return level == 0 ? 127 : 511;
2007 else
2008 /* ILK/SNB sprite plane watermarks */
2009 return level == 0 ? 63 : 255;
2010}
2011
2012static unsigned int ilk_cursor_wm_reg_max(const struct drm_device *dev,
2013 int level)
2014{
2015 if (INTEL_INFO(dev)->gen >= 7)
2016 return level == 0 ? 63 : 255;
2017 else
2018 return level == 0 ? 31 : 63;
2019}
2020
2021static unsigned int ilk_fbc_wm_reg_max(const struct drm_device *dev)
2022{
2023 if (INTEL_INFO(dev)->gen >= 8)
2024 return 31;
2025 else
2026 return 15;
2027}
2028
Ville Syrjälä158ae642013-08-07 13:28:19 +03002029/* Calculate the maximum primary/sprite plane watermark */
2030static unsigned int ilk_plane_wm_max(const struct drm_device *dev,
2031 int level,
Ville Syrjälä240264f2013-08-07 13:29:12 +03002032 const struct intel_wm_config *config,
Ville Syrjälä158ae642013-08-07 13:28:19 +03002033 enum intel_ddb_partitioning ddb_partitioning,
2034 bool is_sprite)
2035{
2036 unsigned int fifo_size = ilk_display_fifo_size(dev);
Ville Syrjälä158ae642013-08-07 13:28:19 +03002037
2038 /* if sprites aren't enabled, sprites get nothing */
Ville Syrjälä240264f2013-08-07 13:29:12 +03002039 if (is_sprite && !config->sprites_enabled)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002040 return 0;
2041
2042 /* HSW allows LP1+ watermarks even with multiple pipes */
Ville Syrjälä240264f2013-08-07 13:29:12 +03002043 if (level == 0 || config->num_pipes_active > 1) {
Ville Syrjälä158ae642013-08-07 13:28:19 +03002044 fifo_size /= INTEL_INFO(dev)->num_pipes;
2045
2046 /*
2047 * For some reason the non self refresh
2048 * FIFO size is only half of the self
2049 * refresh FIFO size on ILK/SNB.
2050 */
2051 if (INTEL_INFO(dev)->gen <= 6)
2052 fifo_size /= 2;
2053 }
2054
Ville Syrjälä240264f2013-08-07 13:29:12 +03002055 if (config->sprites_enabled) {
Ville Syrjälä158ae642013-08-07 13:28:19 +03002056 /* level 0 is always calculated with 1:1 split */
2057 if (level > 0 && ddb_partitioning == INTEL_DDB_PART_5_6) {
2058 if (is_sprite)
2059 fifo_size *= 5;
2060 fifo_size /= 6;
2061 } else {
2062 fifo_size /= 2;
2063 }
2064 }
2065
2066 /* clamp to max that the registers can hold */
Ville Syrjälä4e975082014-03-07 18:32:11 +02002067 return min(fifo_size, ilk_plane_wm_reg_max(dev, level, is_sprite));
Ville Syrjälä158ae642013-08-07 13:28:19 +03002068}
2069
2070/* Calculate the maximum cursor plane watermark */
2071static unsigned int ilk_cursor_wm_max(const struct drm_device *dev,
Ville Syrjälä240264f2013-08-07 13:29:12 +03002072 int level,
2073 const struct intel_wm_config *config)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002074{
2075 /* HSW LP1+ watermarks w/ multiple pipes */
Ville Syrjälä240264f2013-08-07 13:29:12 +03002076 if (level > 0 && config->num_pipes_active > 1)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002077 return 64;
2078
2079 /* otherwise just report max that registers can hold */
Ville Syrjälä4e975082014-03-07 18:32:11 +02002080 return ilk_cursor_wm_reg_max(dev, level);
Ville Syrjälä158ae642013-08-07 13:28:19 +03002081}
2082
Damien Lespiaud34ff9c2014-01-06 19:17:23 +00002083static void ilk_compute_wm_maximums(const struct drm_device *dev,
Ville Syrjälä34982fe2013-10-09 19:18:09 +03002084 int level,
2085 const struct intel_wm_config *config,
2086 enum intel_ddb_partitioning ddb_partitioning,
Imre Deak820c1982013-12-17 14:46:36 +02002087 struct ilk_wm_maximums *max)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002088{
Ville Syrjälä240264f2013-08-07 13:29:12 +03002089 max->pri = ilk_plane_wm_max(dev, level, config, ddb_partitioning, false);
2090 max->spr = ilk_plane_wm_max(dev, level, config, ddb_partitioning, true);
2091 max->cur = ilk_cursor_wm_max(dev, level, config);
Ville Syrjälä4e975082014-03-07 18:32:11 +02002092 max->fbc = ilk_fbc_wm_reg_max(dev);
Ville Syrjälä158ae642013-08-07 13:28:19 +03002093}
2094
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002095static void ilk_compute_wm_reg_maximums(struct drm_device *dev,
2096 int level,
2097 struct ilk_wm_maximums *max)
2098{
2099 max->pri = ilk_plane_wm_reg_max(dev, level, false);
2100 max->spr = ilk_plane_wm_reg_max(dev, level, true);
2101 max->cur = ilk_cursor_wm_reg_max(dev, level);
2102 max->fbc = ilk_fbc_wm_reg_max(dev);
2103}
2104
Ville Syrjäläd9395652013-10-09 19:18:10 +03002105static bool ilk_validate_wm_level(int level,
Imre Deak820c1982013-12-17 14:46:36 +02002106 const struct ilk_wm_maximums *max,
Ville Syrjäläd9395652013-10-09 19:18:10 +03002107 struct intel_wm_level *result)
Ville Syrjäläa9786a12013-08-07 13:24:47 +03002108{
2109 bool ret;
2110
2111 /* already determined to be invalid? */
2112 if (!result->enable)
2113 return false;
2114
2115 result->enable = result->pri_val <= max->pri &&
2116 result->spr_val <= max->spr &&
2117 result->cur_val <= max->cur;
2118
2119 ret = result->enable;
2120
2121 /*
2122 * HACK until we can pre-compute everything,
2123 * and thus fail gracefully if LP0 watermarks
2124 * are exceeded...
2125 */
2126 if (level == 0 && !result->enable) {
2127 if (result->pri_val > max->pri)
2128 DRM_DEBUG_KMS("Primary WM%d too large %u (max %u)\n",
2129 level, result->pri_val, max->pri);
2130 if (result->spr_val > max->spr)
2131 DRM_DEBUG_KMS("Sprite WM%d too large %u (max %u)\n",
2132 level, result->spr_val, max->spr);
2133 if (result->cur_val > max->cur)
2134 DRM_DEBUG_KMS("Cursor WM%d too large %u (max %u)\n",
2135 level, result->cur_val, max->cur);
2136
2137 result->pri_val = min_t(uint32_t, result->pri_val, max->pri);
2138 result->spr_val = min_t(uint32_t, result->spr_val, max->spr);
2139 result->cur_val = min_t(uint32_t, result->cur_val, max->cur);
2140 result->enable = true;
2141 }
2142
Ville Syrjäläa9786a12013-08-07 13:24:47 +03002143 return ret;
2144}
2145
Damien Lespiaud34ff9c2014-01-06 19:17:23 +00002146static void ilk_compute_wm_level(const struct drm_i915_private *dev_priv,
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03002147 int level,
Imre Deak820c1982013-12-17 14:46:36 +02002148 const struct ilk_pipe_wm_parameters *p,
Ville Syrjälä1fd527c2013-08-06 22:24:05 +03002149 struct intel_wm_level *result)
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03002150{
2151 uint16_t pri_latency = dev_priv->wm.pri_latency[level];
2152 uint16_t spr_latency = dev_priv->wm.spr_latency[level];
2153 uint16_t cur_latency = dev_priv->wm.cur_latency[level];
2154
2155 /* WM1+ latency values stored in 0.5us units */
2156 if (level > 0) {
2157 pri_latency *= 5;
2158 spr_latency *= 5;
2159 cur_latency *= 5;
2160 }
2161
2162 result->pri_val = ilk_compute_pri_wm(p, pri_latency, level);
2163 result->spr_val = ilk_compute_spr_wm(p, spr_latency);
2164 result->cur_val = ilk_compute_cur_wm(p, cur_latency);
2165 result->fbc_val = ilk_compute_fbc_wm(p, result->pri_val);
2166 result->enable = true;
2167}
2168
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002169static uint32_t
2170hsw_compute_linetime_wm(struct drm_device *dev, struct drm_crtc *crtc)
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002171{
2172 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002173 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002174 struct drm_display_mode *mode = &intel_crtc->config.adjusted_mode;
Paulo Zanoni85a02de2013-05-03 17:23:43 -03002175 u32 linetime, ips_linetime;
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002176
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002177 if (!intel_crtc_active(crtc))
2178 return 0;
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002179
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002180 /* The WM are computed with base on how long it takes to fill a single
2181 * row at the given clock rate, multiplied by 8.
2182 * */
Jesse Barnesfec8cba2013-11-27 11:10:26 -08002183 linetime = DIV_ROUND_CLOSEST(mode->crtc_htotal * 1000 * 8,
2184 mode->crtc_clock);
2185 ips_linetime = DIV_ROUND_CLOSEST(mode->crtc_htotal * 1000 * 8,
Paulo Zanoni85a02de2013-05-03 17:23:43 -03002186 intel_ddi_get_cdclk_freq(dev_priv));
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002187
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002188 return PIPE_WM_LINETIME_IPS_LINETIME(ips_linetime) |
2189 PIPE_WM_LINETIME_TIME(linetime);
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002190}
2191
Ville Syrjälä12b134d2013-07-05 11:57:21 +03002192static void intel_read_wm_latency(struct drm_device *dev, uint16_t wm[5])
2193{
2194 struct drm_i915_private *dev_priv = dev->dev_private;
2195
Ville Syrjäläa42a5712014-01-07 16:14:08 +02002196 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Ville Syrjälä12b134d2013-07-05 11:57:21 +03002197 uint64_t sskpd = I915_READ64(MCH_SSKPD);
2198
2199 wm[0] = (sskpd >> 56) & 0xFF;
2200 if (wm[0] == 0)
2201 wm[0] = sskpd & 0xF;
Ville Syrjäläe5d50192013-07-05 11:57:22 +03002202 wm[1] = (sskpd >> 4) & 0xFF;
2203 wm[2] = (sskpd >> 12) & 0xFF;
2204 wm[3] = (sskpd >> 20) & 0x1FF;
2205 wm[4] = (sskpd >> 32) & 0x1FF;
Ville Syrjälä63cf9a12013-07-05 11:57:23 +03002206 } else if (INTEL_INFO(dev)->gen >= 6) {
2207 uint32_t sskpd = I915_READ(MCH_SSKPD);
2208
2209 wm[0] = (sskpd >> SSKPD_WM0_SHIFT) & SSKPD_WM_MASK;
2210 wm[1] = (sskpd >> SSKPD_WM1_SHIFT) & SSKPD_WM_MASK;
2211 wm[2] = (sskpd >> SSKPD_WM2_SHIFT) & SSKPD_WM_MASK;
2212 wm[3] = (sskpd >> SSKPD_WM3_SHIFT) & SSKPD_WM_MASK;
Ville Syrjälä3a88d0a2013-08-01 16:18:49 +03002213 } else if (INTEL_INFO(dev)->gen >= 5) {
2214 uint32_t mltr = I915_READ(MLTR_ILK);
2215
2216 /* ILK primary LP0 latency is 700 ns */
2217 wm[0] = 7;
2218 wm[1] = (mltr >> MLTR_WM1_SHIFT) & ILK_SRLT_MASK;
2219 wm[2] = (mltr >> MLTR_WM2_SHIFT) & ILK_SRLT_MASK;
Ville Syrjälä12b134d2013-07-05 11:57:21 +03002220 }
2221}
2222
Ville Syrjälä53615a52013-08-01 16:18:50 +03002223static void intel_fixup_spr_wm_latency(struct drm_device *dev, uint16_t wm[5])
2224{
2225 /* ILK sprite LP0 latency is 1300 ns */
2226 if (INTEL_INFO(dev)->gen == 5)
2227 wm[0] = 13;
2228}
2229
2230static void intel_fixup_cur_wm_latency(struct drm_device *dev, uint16_t wm[5])
2231{
2232 /* ILK cursor LP0 latency is 1300 ns */
2233 if (INTEL_INFO(dev)->gen == 5)
2234 wm[0] = 13;
2235
2236 /* WaDoubleCursorLP3Latency:ivb */
2237 if (IS_IVYBRIDGE(dev))
2238 wm[3] *= 2;
2239}
2240
Damien Lespiau546c81f2014-05-13 15:30:26 +01002241int ilk_wm_max_level(const struct drm_device *dev)
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03002242{
2243 /* how many WM levels are we expecting */
Ville Syrjäläa42a5712014-01-07 16:14:08 +02002244 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03002245 return 4;
2246 else if (INTEL_INFO(dev)->gen >= 6)
2247 return 3;
2248 else
2249 return 2;
2250}
2251
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002252static void intel_print_wm_latency(struct drm_device *dev,
2253 const char *name,
2254 const uint16_t wm[5])
2255{
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03002256 int level, max_level = ilk_wm_max_level(dev);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002257
2258 for (level = 0; level <= max_level; level++) {
2259 unsigned int latency = wm[level];
2260
2261 if (latency == 0) {
2262 DRM_ERROR("%s WM%d latency not provided\n",
2263 name, level);
2264 continue;
2265 }
2266
2267 /* WM1+ latency values in 0.5us units */
2268 if (level > 0)
2269 latency *= 5;
2270
2271 DRM_DEBUG_KMS("%s WM%d latency %u (%u.%u usec)\n",
2272 name, level, wm[level],
2273 latency / 10, latency % 10);
2274 }
2275}
2276
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002277static bool ilk_increase_wm_latency(struct drm_i915_private *dev_priv,
2278 uint16_t wm[5], uint16_t min)
2279{
2280 int level, max_level = ilk_wm_max_level(dev_priv->dev);
2281
2282 if (wm[0] >= min)
2283 return false;
2284
2285 wm[0] = max(wm[0], min);
2286 for (level = 1; level <= max_level; level++)
2287 wm[level] = max_t(uint16_t, wm[level], DIV_ROUND_UP(min, 5));
2288
2289 return true;
2290}
2291
2292static void snb_wm_latency_quirk(struct drm_device *dev)
2293{
2294 struct drm_i915_private *dev_priv = dev->dev_private;
2295 bool changed;
2296
2297 /*
2298 * The BIOS provided WM memory latency values are often
2299 * inadequate for high resolution displays. Adjust them.
2300 */
2301 changed = ilk_increase_wm_latency(dev_priv, dev_priv->wm.pri_latency, 12) |
2302 ilk_increase_wm_latency(dev_priv, dev_priv->wm.spr_latency, 12) |
2303 ilk_increase_wm_latency(dev_priv, dev_priv->wm.cur_latency, 12);
2304
2305 if (!changed)
2306 return;
2307
2308 DRM_DEBUG_KMS("WM latency values increased to avoid potential underruns\n");
2309 intel_print_wm_latency(dev, "Primary", dev_priv->wm.pri_latency);
2310 intel_print_wm_latency(dev, "Sprite", dev_priv->wm.spr_latency);
2311 intel_print_wm_latency(dev, "Cursor", dev_priv->wm.cur_latency);
2312}
2313
Damien Lespiaufa50ad62014-03-17 18:01:16 +00002314static void ilk_setup_wm_latency(struct drm_device *dev)
Ville Syrjälä53615a52013-08-01 16:18:50 +03002315{
2316 struct drm_i915_private *dev_priv = dev->dev_private;
2317
2318 intel_read_wm_latency(dev, dev_priv->wm.pri_latency);
2319
2320 memcpy(dev_priv->wm.spr_latency, dev_priv->wm.pri_latency,
2321 sizeof(dev_priv->wm.pri_latency));
2322 memcpy(dev_priv->wm.cur_latency, dev_priv->wm.pri_latency,
2323 sizeof(dev_priv->wm.pri_latency));
2324
2325 intel_fixup_spr_wm_latency(dev, dev_priv->wm.spr_latency);
2326 intel_fixup_cur_wm_latency(dev, dev_priv->wm.cur_latency);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002327
2328 intel_print_wm_latency(dev, "Primary", dev_priv->wm.pri_latency);
2329 intel_print_wm_latency(dev, "Sprite", dev_priv->wm.spr_latency);
2330 intel_print_wm_latency(dev, "Cursor", dev_priv->wm.cur_latency);
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002331
2332 if (IS_GEN6(dev))
2333 snb_wm_latency_quirk(dev);
Ville Syrjälä53615a52013-08-01 16:18:50 +03002334}
2335
Imre Deak820c1982013-12-17 14:46:36 +02002336static void ilk_compute_wm_parameters(struct drm_crtc *crtc,
Ville Syrjälä2a44b762014-03-07 18:32:09 +02002337 struct ilk_pipe_wm_parameters *p)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002338{
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03002339 struct drm_device *dev = crtc->dev;
2340 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2341 enum pipe pipe = intel_crtc->pipe;
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03002342 struct drm_plane *plane;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002343
Ville Syrjälä2a44b762014-03-07 18:32:09 +02002344 if (!intel_crtc_active(crtc))
2345 return;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002346
Ville Syrjälä2a44b762014-03-07 18:32:09 +02002347 p->active = true;
2348 p->pipe_htotal = intel_crtc->config.adjusted_mode.crtc_htotal;
2349 p->pixel_rate = ilk_pipe_pixel_rate(dev, crtc);
2350 p->pri.bytes_per_pixel = crtc->primary->fb->bits_per_pixel / 8;
2351 p->cur.bytes_per_pixel = 4;
2352 p->pri.horiz_pixels = intel_crtc->config.pipe_src_w;
2353 p->cur.horiz_pixels = intel_crtc->cursor_width;
2354 /* TODO: for now, assume primary and cursor planes are always enabled. */
2355 p->pri.enabled = true;
2356 p->cur.enabled = true;
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03002357
Matt Roperaf2b6532014-04-01 15:22:32 -07002358 drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002359 struct intel_plane *intel_plane = to_intel_plane(plane);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002360
Ville Syrjälä2a44b762014-03-07 18:32:09 +02002361 if (intel_plane->pipe == pipe) {
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03002362 p->spr = intel_plane->wm;
Ville Syrjälä2a44b762014-03-07 18:32:09 +02002363 break;
2364 }
2365 }
2366}
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002367
Ville Syrjälä2a44b762014-03-07 18:32:09 +02002368static void ilk_compute_wm_config(struct drm_device *dev,
2369 struct intel_wm_config *config)
2370{
2371 struct intel_crtc *intel_crtc;
2372
2373 /* Compute the currently _active_ config */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01002374 for_each_intel_crtc(dev, intel_crtc) {
Ville Syrjälä2a44b762014-03-07 18:32:09 +02002375 const struct intel_pipe_wm *wm = &intel_crtc->wm.active;
2376
2377 if (!wm->pipe_enabled)
2378 continue;
2379
2380 config->sprites_enabled |= wm->sprites_enabled;
2381 config->sprites_scaled |= wm->sprites_scaled;
2382 config->num_pipes_active++;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002383 }
2384}
2385
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002386/* Compute new watermarks for the pipe */
2387static bool intel_compute_pipe_wm(struct drm_crtc *crtc,
Imre Deak820c1982013-12-17 14:46:36 +02002388 const struct ilk_pipe_wm_parameters *params,
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002389 struct intel_pipe_wm *pipe_wm)
2390{
2391 struct drm_device *dev = crtc->dev;
Damien Lespiaud34ff9c2014-01-06 19:17:23 +00002392 const struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002393 int level, max_level = ilk_wm_max_level(dev);
2394 /* LP0 watermark maximums depend on this pipe alone */
2395 struct intel_wm_config config = {
2396 .num_pipes_active = 1,
2397 .sprites_enabled = params->spr.enabled,
2398 .sprites_scaled = params->spr.scaled,
2399 };
Imre Deak820c1982013-12-17 14:46:36 +02002400 struct ilk_wm_maximums max;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002401
Ville Syrjälä2a44b762014-03-07 18:32:09 +02002402 pipe_wm->pipe_enabled = params->active;
2403 pipe_wm->sprites_enabled = params->spr.enabled;
2404 pipe_wm->sprites_scaled = params->spr.scaled;
2405
Ville Syrjälä7b39a0b2013-12-05 15:51:30 +02002406 /* ILK/SNB: LP2+ watermarks only w/o sprites */
2407 if (INTEL_INFO(dev)->gen <= 6 && params->spr.enabled)
2408 max_level = 1;
2409
2410 /* ILK/SNB/IVB: LP1+ watermarks only w/o scaling */
2411 if (params->spr.scaled)
2412 max_level = 0;
2413
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002414 ilk_compute_wm_level(dev_priv, 0, params, &pipe_wm->wm[0]);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002415
Ville Syrjäläa42a5712014-01-07 16:14:08 +02002416 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Ville Syrjäläce0e0712013-12-05 15:51:36 +02002417 pipe_wm->linetime = hsw_compute_linetime_wm(dev, crtc);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002418
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002419 /* LP0 watermarks always use 1/2 DDB partitioning */
2420 ilk_compute_wm_maximums(dev, 0, &config, INTEL_DDB_PART_1_2, &max);
2421
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002422 /* At least LP0 must be valid */
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002423 if (!ilk_validate_wm_level(0, &max, &pipe_wm->wm[0]))
2424 return false;
2425
2426 ilk_compute_wm_reg_maximums(dev, 1, &max);
2427
2428 for (level = 1; level <= max_level; level++) {
2429 struct intel_wm_level wm = {};
2430
2431 ilk_compute_wm_level(dev_priv, level, params, &wm);
2432
2433 /*
2434 * Disable any watermark level that exceeds the
2435 * register maximums since such watermarks are
2436 * always invalid.
2437 */
2438 if (!ilk_validate_wm_level(level, &max, &wm))
2439 break;
2440
2441 pipe_wm->wm[level] = wm;
2442 }
2443
2444 return true;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002445}
2446
2447/*
2448 * Merge the watermarks from all active pipes for a specific level.
2449 */
2450static void ilk_merge_wm_level(struct drm_device *dev,
2451 int level,
2452 struct intel_wm_level *ret_wm)
2453{
2454 const struct intel_crtc *intel_crtc;
2455
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002456 ret_wm->enable = true;
2457
Damien Lespiaud3fcc802014-05-13 23:32:22 +01002458 for_each_intel_crtc(dev, intel_crtc) {
Ville Syrjäläfe392ef2014-03-07 18:32:10 +02002459 const struct intel_pipe_wm *active = &intel_crtc->wm.active;
2460 const struct intel_wm_level *wm = &active->wm[level];
2461
2462 if (!active->pipe_enabled)
2463 continue;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002464
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002465 /*
2466 * The watermark values may have been used in the past,
2467 * so we must maintain them in the registers for some
2468 * time even if the level is now disabled.
2469 */
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002470 if (!wm->enable)
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002471 ret_wm->enable = false;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002472
2473 ret_wm->pri_val = max(ret_wm->pri_val, wm->pri_val);
2474 ret_wm->spr_val = max(ret_wm->spr_val, wm->spr_val);
2475 ret_wm->cur_val = max(ret_wm->cur_val, wm->cur_val);
2476 ret_wm->fbc_val = max(ret_wm->fbc_val, wm->fbc_val);
2477 }
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002478}
2479
2480/*
2481 * Merge all low power watermarks for all active pipes.
2482 */
2483static void ilk_wm_merge(struct drm_device *dev,
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02002484 const struct intel_wm_config *config,
Imre Deak820c1982013-12-17 14:46:36 +02002485 const struct ilk_wm_maximums *max,
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002486 struct intel_pipe_wm *merged)
2487{
2488 int level, max_level = ilk_wm_max_level(dev);
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002489 int last_enabled_level = max_level;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002490
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02002491 /* ILK/SNB/IVB: LP1+ watermarks only w/ single pipe */
2492 if ((INTEL_INFO(dev)->gen <= 6 || IS_IVYBRIDGE(dev)) &&
2493 config->num_pipes_active > 1)
2494 return;
2495
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02002496 /* ILK: FBC WM must be disabled always */
2497 merged->fbc_wm_enabled = INTEL_INFO(dev)->gen >= 6;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002498
2499 /* merge each WM1+ level */
2500 for (level = 1; level <= max_level; level++) {
2501 struct intel_wm_level *wm = &merged->wm[level];
2502
2503 ilk_merge_wm_level(dev, level, wm);
2504
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002505 if (level > last_enabled_level)
2506 wm->enable = false;
2507 else if (!ilk_validate_wm_level(level, max, wm))
2508 /* make sure all following levels get disabled */
2509 last_enabled_level = level - 1;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002510
2511 /*
2512 * The spec says it is preferred to disable
2513 * FBC WMs instead of disabling a WM level.
2514 */
2515 if (wm->fbc_val > max->fbc) {
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002516 if (wm->enable)
2517 merged->fbc_wm_enabled = false;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002518 wm->fbc_val = 0;
2519 }
2520 }
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02002521
2522 /* ILK: LP2+ must be disabled when FBC WM is disabled but FBC enabled */
2523 /*
2524 * FIXME this is racy. FBC might get enabled later.
2525 * What we should check here is whether FBC can be
2526 * enabled sometime later.
2527 */
2528 if (IS_GEN5(dev) && !merged->fbc_wm_enabled && intel_fbc_enabled(dev)) {
2529 for (level = 2; level <= max_level; level++) {
2530 struct intel_wm_level *wm = &merged->wm[level];
2531
2532 wm->enable = false;
2533 }
2534 }
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002535}
2536
Ville Syrjäläb380ca32013-10-09 19:18:01 +03002537static int ilk_wm_lp_to_level(int wm_lp, const struct intel_pipe_wm *pipe_wm)
2538{
2539 /* LP1,LP2,LP3 levels are either 1,2,3 or 1,3,4 */
2540 return wm_lp + (wm_lp >= 2 && pipe_wm->wm[4].enable);
2541}
2542
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02002543/* The value we need to program into the WM_LPx latency field */
2544static unsigned int ilk_wm_lp_latency(struct drm_device *dev, int level)
2545{
2546 struct drm_i915_private *dev_priv = dev->dev_private;
2547
Ville Syrjäläa42a5712014-01-07 16:14:08 +02002548 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02002549 return 2 * level;
2550 else
2551 return dev_priv->wm.pri_latency[level];
2552}
2553
Imre Deak820c1982013-12-17 14:46:36 +02002554static void ilk_compute_wm_results(struct drm_device *dev,
Ville Syrjälä0362c782013-10-09 19:17:57 +03002555 const struct intel_pipe_wm *merged,
Ville Syrjälä609cede2013-10-09 19:18:03 +03002556 enum intel_ddb_partitioning partitioning,
Imre Deak820c1982013-12-17 14:46:36 +02002557 struct ilk_wm_values *results)
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002558{
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002559 struct intel_crtc *intel_crtc;
2560 int level, wm_lp;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002561
Ville Syrjälä0362c782013-10-09 19:17:57 +03002562 results->enable_fbc_wm = merged->fbc_wm_enabled;
Ville Syrjälä609cede2013-10-09 19:18:03 +03002563 results->partitioning = partitioning;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002564
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002565 /* LP1+ register values */
Paulo Zanonicca32e92013-05-31 11:45:06 -03002566 for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
Ville Syrjälä1fd527c2013-08-06 22:24:05 +03002567 const struct intel_wm_level *r;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002568
Ville Syrjäläb380ca32013-10-09 19:18:01 +03002569 level = ilk_wm_lp_to_level(wm_lp, merged);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002570
Ville Syrjälä0362c782013-10-09 19:17:57 +03002571 r = &merged->wm[level];
Paulo Zanonicca32e92013-05-31 11:45:06 -03002572
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002573 /*
2574 * Maintain the watermark values even if the level is
2575 * disabled. Doing otherwise could cause underruns.
2576 */
2577 results->wm_lp[wm_lp - 1] =
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02002578 (ilk_wm_lp_latency(dev, level) << WM1_LP_LATENCY_SHIFT) |
Ville Syrjälä416f4722013-11-02 21:07:46 -07002579 (r->pri_val << WM1_LP_SR_SHIFT) |
2580 r->cur_val;
2581
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002582 if (r->enable)
2583 results->wm_lp[wm_lp - 1] |= WM1_LP_SR_EN;
2584
Ville Syrjälä416f4722013-11-02 21:07:46 -07002585 if (INTEL_INFO(dev)->gen >= 8)
2586 results->wm_lp[wm_lp - 1] |=
2587 r->fbc_val << WM1_LP_FBC_SHIFT_BDW;
2588 else
2589 results->wm_lp[wm_lp - 1] |=
2590 r->fbc_val << WM1_LP_FBC_SHIFT;
2591
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002592 /*
2593 * Always set WM1S_LP_EN when spr_val != 0, even if the
2594 * level is disabled. Doing otherwise could cause underruns.
2595 */
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02002596 if (INTEL_INFO(dev)->gen <= 6 && r->spr_val) {
2597 WARN_ON(wm_lp != 1);
2598 results->wm_lp_spr[wm_lp - 1] = WM1S_LP_EN | r->spr_val;
2599 } else
2600 results->wm_lp_spr[wm_lp - 1] = r->spr_val;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002601 }
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002602
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002603 /* LP0 register values */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01002604 for_each_intel_crtc(dev, intel_crtc) {
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002605 enum pipe pipe = intel_crtc->pipe;
2606 const struct intel_wm_level *r =
2607 &intel_crtc->wm.active.wm[0];
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002608
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002609 if (WARN_ON(!r->enable))
2610 continue;
2611
2612 results->wm_linetime[pipe] = intel_crtc->wm.active.linetime;
2613
2614 results->wm_pipe[pipe] =
2615 (r->pri_val << WM0_PIPE_PLANE_SHIFT) |
2616 (r->spr_val << WM0_PIPE_SPRITE_SHIFT) |
2617 r->cur_val;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002618 }
2619}
2620
Paulo Zanoni861f3382013-05-31 10:19:21 -03002621/* Find the result with the highest level enabled. Check for enable_fbc_wm in
2622 * case both are at the same level. Prefer r1 in case they're the same. */
Imre Deak820c1982013-12-17 14:46:36 +02002623static struct intel_pipe_wm *ilk_find_best_result(struct drm_device *dev,
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002624 struct intel_pipe_wm *r1,
2625 struct intel_pipe_wm *r2)
Paulo Zanoni861f3382013-05-31 10:19:21 -03002626{
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002627 int level, max_level = ilk_wm_max_level(dev);
2628 int level1 = 0, level2 = 0;
Paulo Zanoni861f3382013-05-31 10:19:21 -03002629
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002630 for (level = 1; level <= max_level; level++) {
2631 if (r1->wm[level].enable)
2632 level1 = level;
2633 if (r2->wm[level].enable)
2634 level2 = level;
Paulo Zanoni861f3382013-05-31 10:19:21 -03002635 }
2636
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002637 if (level1 == level2) {
2638 if (r2->fbc_wm_enabled && !r1->fbc_wm_enabled)
Paulo Zanoni861f3382013-05-31 10:19:21 -03002639 return r2;
2640 else
2641 return r1;
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002642 } else if (level1 > level2) {
Paulo Zanoni861f3382013-05-31 10:19:21 -03002643 return r1;
2644 } else {
2645 return r2;
2646 }
2647}
2648
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002649/* dirty bits used to track which watermarks need changes */
2650#define WM_DIRTY_PIPE(pipe) (1 << (pipe))
2651#define WM_DIRTY_LINETIME(pipe) (1 << (8 + (pipe)))
2652#define WM_DIRTY_LP(wm_lp) (1 << (15 + (wm_lp)))
2653#define WM_DIRTY_LP_ALL (WM_DIRTY_LP(1) | WM_DIRTY_LP(2) | WM_DIRTY_LP(3))
2654#define WM_DIRTY_FBC (1 << 24)
2655#define WM_DIRTY_DDB (1 << 25)
2656
Damien Lespiau055e3932014-08-18 13:49:10 +01002657static unsigned int ilk_compute_wm_dirty(struct drm_i915_private *dev_priv,
Imre Deak820c1982013-12-17 14:46:36 +02002658 const struct ilk_wm_values *old,
2659 const struct ilk_wm_values *new)
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002660{
2661 unsigned int dirty = 0;
2662 enum pipe pipe;
2663 int wm_lp;
2664
Damien Lespiau055e3932014-08-18 13:49:10 +01002665 for_each_pipe(dev_priv, pipe) {
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002666 if (old->wm_linetime[pipe] != new->wm_linetime[pipe]) {
2667 dirty |= WM_DIRTY_LINETIME(pipe);
2668 /* Must disable LP1+ watermarks too */
2669 dirty |= WM_DIRTY_LP_ALL;
2670 }
2671
2672 if (old->wm_pipe[pipe] != new->wm_pipe[pipe]) {
2673 dirty |= WM_DIRTY_PIPE(pipe);
2674 /* Must disable LP1+ watermarks too */
2675 dirty |= WM_DIRTY_LP_ALL;
2676 }
2677 }
2678
2679 if (old->enable_fbc_wm != new->enable_fbc_wm) {
2680 dirty |= WM_DIRTY_FBC;
2681 /* Must disable LP1+ watermarks too */
2682 dirty |= WM_DIRTY_LP_ALL;
2683 }
2684
2685 if (old->partitioning != new->partitioning) {
2686 dirty |= WM_DIRTY_DDB;
2687 /* Must disable LP1+ watermarks too */
2688 dirty |= WM_DIRTY_LP_ALL;
2689 }
2690
2691 /* LP1+ watermarks already deemed dirty, no need to continue */
2692 if (dirty & WM_DIRTY_LP_ALL)
2693 return dirty;
2694
2695 /* Find the lowest numbered LP1+ watermark in need of an update... */
2696 for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
2697 if (old->wm_lp[wm_lp - 1] != new->wm_lp[wm_lp - 1] ||
2698 old->wm_lp_spr[wm_lp - 1] != new->wm_lp_spr[wm_lp - 1])
2699 break;
2700 }
2701
2702 /* ...and mark it and all higher numbered LP1+ watermarks as dirty */
2703 for (; wm_lp <= 3; wm_lp++)
2704 dirty |= WM_DIRTY_LP(wm_lp);
2705
2706 return dirty;
2707}
2708
Ville Syrjälä8553c182013-12-05 15:51:39 +02002709static bool _ilk_disable_lp_wm(struct drm_i915_private *dev_priv,
2710 unsigned int dirty)
2711{
Imre Deak820c1982013-12-17 14:46:36 +02002712 struct ilk_wm_values *previous = &dev_priv->wm.hw;
Ville Syrjälä8553c182013-12-05 15:51:39 +02002713 bool changed = false;
2714
2715 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] & WM1_LP_SR_EN) {
2716 previous->wm_lp[2] &= ~WM1_LP_SR_EN;
2717 I915_WRITE(WM3_LP_ILK, previous->wm_lp[2]);
2718 changed = true;
2719 }
2720 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] & WM1_LP_SR_EN) {
2721 previous->wm_lp[1] &= ~WM1_LP_SR_EN;
2722 I915_WRITE(WM2_LP_ILK, previous->wm_lp[1]);
2723 changed = true;
2724 }
2725 if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] & WM1_LP_SR_EN) {
2726 previous->wm_lp[0] &= ~WM1_LP_SR_EN;
2727 I915_WRITE(WM1_LP_ILK, previous->wm_lp[0]);
2728 changed = true;
2729 }
2730
2731 /*
2732 * Don't touch WM1S_LP_EN here.
2733 * Doing so could cause underruns.
2734 */
2735
2736 return changed;
2737}
2738
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002739/*
2740 * The spec says we shouldn't write when we don't need, because every write
2741 * causes WMs to be re-evaluated, expending some power.
2742 */
Imre Deak820c1982013-12-17 14:46:36 +02002743static void ilk_write_wm_values(struct drm_i915_private *dev_priv,
2744 struct ilk_wm_values *results)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002745{
Ville Syrjäläac9545f2013-12-05 15:51:28 +02002746 struct drm_device *dev = dev_priv->dev;
Imre Deak820c1982013-12-17 14:46:36 +02002747 struct ilk_wm_values *previous = &dev_priv->wm.hw;
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002748 unsigned int dirty;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002749 uint32_t val;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002750
Damien Lespiau055e3932014-08-18 13:49:10 +01002751 dirty = ilk_compute_wm_dirty(dev_priv, previous, results);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002752 if (!dirty)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002753 return;
2754
Ville Syrjälä8553c182013-12-05 15:51:39 +02002755 _ilk_disable_lp_wm(dev_priv, dirty);
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02002756
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002757 if (dirty & WM_DIRTY_PIPE(PIPE_A))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002758 I915_WRITE(WM0_PIPEA_ILK, results->wm_pipe[0]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002759 if (dirty & WM_DIRTY_PIPE(PIPE_B))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002760 I915_WRITE(WM0_PIPEB_ILK, results->wm_pipe[1]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002761 if (dirty & WM_DIRTY_PIPE(PIPE_C))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002762 I915_WRITE(WM0_PIPEC_IVB, results->wm_pipe[2]);
2763
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002764 if (dirty & WM_DIRTY_LINETIME(PIPE_A))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002765 I915_WRITE(PIPE_WM_LINETIME(PIPE_A), results->wm_linetime[0]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002766 if (dirty & WM_DIRTY_LINETIME(PIPE_B))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002767 I915_WRITE(PIPE_WM_LINETIME(PIPE_B), results->wm_linetime[1]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002768 if (dirty & WM_DIRTY_LINETIME(PIPE_C))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002769 I915_WRITE(PIPE_WM_LINETIME(PIPE_C), results->wm_linetime[2]);
2770
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002771 if (dirty & WM_DIRTY_DDB) {
Ville Syrjäläa42a5712014-01-07 16:14:08 +02002772 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Ville Syrjäläac9545f2013-12-05 15:51:28 +02002773 val = I915_READ(WM_MISC);
2774 if (results->partitioning == INTEL_DDB_PART_1_2)
2775 val &= ~WM_MISC_DATA_PARTITION_5_6;
2776 else
2777 val |= WM_MISC_DATA_PARTITION_5_6;
2778 I915_WRITE(WM_MISC, val);
2779 } else {
2780 val = I915_READ(DISP_ARB_CTL2);
2781 if (results->partitioning == INTEL_DDB_PART_1_2)
2782 val &= ~DISP_DATA_PARTITION_5_6;
2783 else
2784 val |= DISP_DATA_PARTITION_5_6;
2785 I915_WRITE(DISP_ARB_CTL2, val);
2786 }
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002787 }
2788
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002789 if (dirty & WM_DIRTY_FBC) {
Paulo Zanonicca32e92013-05-31 11:45:06 -03002790 val = I915_READ(DISP_ARB_CTL);
2791 if (results->enable_fbc_wm)
2792 val &= ~DISP_FBC_WM_DIS;
2793 else
2794 val |= DISP_FBC_WM_DIS;
2795 I915_WRITE(DISP_ARB_CTL, val);
2796 }
2797
Imre Deak954911e2013-12-17 14:46:34 +02002798 if (dirty & WM_DIRTY_LP(1) &&
2799 previous->wm_lp_spr[0] != results->wm_lp_spr[0])
2800 I915_WRITE(WM1S_LP_ILK, results->wm_lp_spr[0]);
2801
2802 if (INTEL_INFO(dev)->gen >= 7) {
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02002803 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp_spr[1] != results->wm_lp_spr[1])
2804 I915_WRITE(WM2S_LP_IVB, results->wm_lp_spr[1]);
2805 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp_spr[2] != results->wm_lp_spr[2])
2806 I915_WRITE(WM3S_LP_IVB, results->wm_lp_spr[2]);
2807 }
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002808
Ville Syrjäläfacd619b2013-12-05 15:51:33 +02002809 if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] != results->wm_lp[0])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002810 I915_WRITE(WM1_LP_ILK, results->wm_lp[0]);
Ville Syrjäläfacd619b2013-12-05 15:51:33 +02002811 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] != results->wm_lp[1])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002812 I915_WRITE(WM2_LP_ILK, results->wm_lp[1]);
Ville Syrjäläfacd619b2013-12-05 15:51:33 +02002813 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] != results->wm_lp[2])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002814 I915_WRITE(WM3_LP_ILK, results->wm_lp[2]);
Ville Syrjälä609cede2013-10-09 19:18:03 +03002815
2816 dev_priv->wm.hw = *results;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002817}
2818
Ville Syrjälä8553c182013-12-05 15:51:39 +02002819static bool ilk_disable_lp_wm(struct drm_device *dev)
2820{
2821 struct drm_i915_private *dev_priv = dev->dev_private;
2822
2823 return _ilk_disable_lp_wm(dev_priv, WM_DIRTY_LP_ALL);
2824}
2825
Imre Deak820c1982013-12-17 14:46:36 +02002826static void ilk_update_wm(struct drm_crtc *crtc)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002827{
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03002828 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjälä46ba6142013-09-10 11:40:40 +03002829 struct drm_device *dev = crtc->dev;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002830 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak820c1982013-12-17 14:46:36 +02002831 struct ilk_wm_maximums max;
2832 struct ilk_pipe_wm_parameters params = {};
2833 struct ilk_wm_values results = {};
Ville Syrjälä77c122b2013-08-06 22:24:04 +03002834 enum intel_ddb_partitioning partitioning;
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03002835 struct intel_pipe_wm pipe_wm = {};
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002836 struct intel_pipe_wm lp_wm_1_2 = {}, lp_wm_5_6 = {}, *best_lp_wm;
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03002837 struct intel_wm_config config = {};
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002838
Ville Syrjälä2a44b762014-03-07 18:32:09 +02002839 ilk_compute_wm_parameters(crtc, &params);
Paulo Zanoni861f3382013-05-31 10:19:21 -03002840
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03002841 intel_compute_pipe_wm(crtc, &params, &pipe_wm);
2842
2843 if (!memcmp(&intel_crtc->wm.active, &pipe_wm, sizeof(pipe_wm)))
2844 return;
2845
2846 intel_crtc->wm.active = pipe_wm;
2847
Ville Syrjälä2a44b762014-03-07 18:32:09 +02002848 ilk_compute_wm_config(dev, &config);
2849
Ville Syrjälä34982fe2013-10-09 19:18:09 +03002850 ilk_compute_wm_maximums(dev, 1, &config, INTEL_DDB_PART_1_2, &max);
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02002851 ilk_wm_merge(dev, &config, &max, &lp_wm_1_2);
Ville Syrjälä0362c782013-10-09 19:17:57 +03002852
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03002853 /* 5/6 split only in single pipe config on IVB+ */
Ville Syrjäläec98c8d2013-10-11 15:26:26 +03002854 if (INTEL_INFO(dev)->gen >= 7 &&
2855 config.num_pipes_active == 1 && config.sprites_enabled) {
Ville Syrjälä34982fe2013-10-09 19:18:09 +03002856 ilk_compute_wm_maximums(dev, 1, &config, INTEL_DDB_PART_5_6, &max);
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02002857 ilk_wm_merge(dev, &config, &max, &lp_wm_5_6);
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03002858
Imre Deak820c1982013-12-17 14:46:36 +02002859 best_lp_wm = ilk_find_best_result(dev, &lp_wm_1_2, &lp_wm_5_6);
Paulo Zanoni861f3382013-05-31 10:19:21 -03002860 } else {
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002861 best_lp_wm = &lp_wm_1_2;
Paulo Zanoni861f3382013-05-31 10:19:21 -03002862 }
2863
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002864 partitioning = (best_lp_wm == &lp_wm_1_2) ?
Ville Syrjälä77c122b2013-08-06 22:24:04 +03002865 INTEL_DDB_PART_1_2 : INTEL_DDB_PART_5_6;
Paulo Zanoni861f3382013-05-31 10:19:21 -03002866
Imre Deak820c1982013-12-17 14:46:36 +02002867 ilk_compute_wm_results(dev, best_lp_wm, partitioning, &results);
Ville Syrjälä609cede2013-10-09 19:18:03 +03002868
Imre Deak820c1982013-12-17 14:46:36 +02002869 ilk_write_wm_values(dev_priv, &results);
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002870}
2871
Damien Lespiaued57cb82014-07-15 09:21:24 +02002872static void
2873ilk_update_sprite_wm(struct drm_plane *plane,
2874 struct drm_crtc *crtc,
2875 uint32_t sprite_width, uint32_t sprite_height,
2876 int pixel_size, bool enabled, bool scaled)
Paulo Zanoni526682e2013-05-24 11:59:18 -03002877{
Ville Syrjälä8553c182013-12-05 15:51:39 +02002878 struct drm_device *dev = plane->dev;
Ville Syrjäläadf3d352013-08-06 22:24:11 +03002879 struct intel_plane *intel_plane = to_intel_plane(plane);
Paulo Zanoni526682e2013-05-24 11:59:18 -03002880
Ville Syrjäläadf3d352013-08-06 22:24:11 +03002881 intel_plane->wm.enabled = enabled;
2882 intel_plane->wm.scaled = scaled;
2883 intel_plane->wm.horiz_pixels = sprite_width;
Damien Lespiaued57cb82014-07-15 09:21:24 +02002884 intel_plane->wm.vert_pixels = sprite_width;
Ville Syrjäläadf3d352013-08-06 22:24:11 +03002885 intel_plane->wm.bytes_per_pixel = pixel_size;
Paulo Zanoni526682e2013-05-24 11:59:18 -03002886
Ville Syrjälä8553c182013-12-05 15:51:39 +02002887 /*
2888 * IVB workaround: must disable low power watermarks for at least
2889 * one frame before enabling scaling. LP watermarks can be re-enabled
2890 * when scaling is disabled.
2891 *
2892 * WaCxSRDisabledForSpriteScaling:ivb
2893 */
2894 if (IS_IVYBRIDGE(dev) && scaled && ilk_disable_lp_wm(dev))
2895 intel_wait_for_vblank(dev, intel_plane->pipe);
2896
Imre Deak820c1982013-12-17 14:46:36 +02002897 ilk_update_wm(crtc);
Paulo Zanoni526682e2013-05-24 11:59:18 -03002898}
2899
Ville Syrjälä243e6a42013-10-14 14:55:24 +03002900static void ilk_pipe_wm_get_hw_state(struct drm_crtc *crtc)
2901{
2902 struct drm_device *dev = crtc->dev;
2903 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak820c1982013-12-17 14:46:36 +02002904 struct ilk_wm_values *hw = &dev_priv->wm.hw;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03002905 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2906 struct intel_pipe_wm *active = &intel_crtc->wm.active;
2907 enum pipe pipe = intel_crtc->pipe;
2908 static const unsigned int wm0_pipe_reg[] = {
2909 [PIPE_A] = WM0_PIPEA_ILK,
2910 [PIPE_B] = WM0_PIPEB_ILK,
2911 [PIPE_C] = WM0_PIPEC_IVB,
2912 };
2913
2914 hw->wm_pipe[pipe] = I915_READ(wm0_pipe_reg[pipe]);
Ville Syrjäläa42a5712014-01-07 16:14:08 +02002915 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Ville Syrjäläce0e0712013-12-05 15:51:36 +02002916 hw->wm_linetime[pipe] = I915_READ(PIPE_WM_LINETIME(pipe));
Ville Syrjälä243e6a42013-10-14 14:55:24 +03002917
Ville Syrjälä2a44b762014-03-07 18:32:09 +02002918 active->pipe_enabled = intel_crtc_active(crtc);
2919
2920 if (active->pipe_enabled) {
Ville Syrjälä243e6a42013-10-14 14:55:24 +03002921 u32 tmp = hw->wm_pipe[pipe];
2922
2923 /*
2924 * For active pipes LP0 watermark is marked as
2925 * enabled, and LP1+ watermaks as disabled since
2926 * we can't really reverse compute them in case
2927 * multiple pipes are active.
2928 */
2929 active->wm[0].enable = true;
2930 active->wm[0].pri_val = (tmp & WM0_PIPE_PLANE_MASK) >> WM0_PIPE_PLANE_SHIFT;
2931 active->wm[0].spr_val = (tmp & WM0_PIPE_SPRITE_MASK) >> WM0_PIPE_SPRITE_SHIFT;
2932 active->wm[0].cur_val = tmp & WM0_PIPE_CURSOR_MASK;
2933 active->linetime = hw->wm_linetime[pipe];
2934 } else {
2935 int level, max_level = ilk_wm_max_level(dev);
2936
2937 /*
2938 * For inactive pipes, all watermark levels
2939 * should be marked as enabled but zeroed,
2940 * which is what we'd compute them to.
2941 */
2942 for (level = 0; level <= max_level; level++)
2943 active->wm[level].enable = true;
2944 }
2945}
2946
2947void ilk_wm_get_hw_state(struct drm_device *dev)
2948{
2949 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak820c1982013-12-17 14:46:36 +02002950 struct ilk_wm_values *hw = &dev_priv->wm.hw;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03002951 struct drm_crtc *crtc;
2952
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01002953 for_each_crtc(dev, crtc)
Ville Syrjälä243e6a42013-10-14 14:55:24 +03002954 ilk_pipe_wm_get_hw_state(crtc);
2955
2956 hw->wm_lp[0] = I915_READ(WM1_LP_ILK);
2957 hw->wm_lp[1] = I915_READ(WM2_LP_ILK);
2958 hw->wm_lp[2] = I915_READ(WM3_LP_ILK);
2959
2960 hw->wm_lp_spr[0] = I915_READ(WM1S_LP_ILK);
Ville Syrjäläcfa76982014-03-07 18:32:08 +02002961 if (INTEL_INFO(dev)->gen >= 7) {
2962 hw->wm_lp_spr[1] = I915_READ(WM2S_LP_IVB);
2963 hw->wm_lp_spr[2] = I915_READ(WM3S_LP_IVB);
2964 }
Ville Syrjälä243e6a42013-10-14 14:55:24 +03002965
Ville Syrjäläa42a5712014-01-07 16:14:08 +02002966 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Ville Syrjäläac9545f2013-12-05 15:51:28 +02002967 hw->partitioning = (I915_READ(WM_MISC) & WM_MISC_DATA_PARTITION_5_6) ?
2968 INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
2969 else if (IS_IVYBRIDGE(dev))
2970 hw->partitioning = (I915_READ(DISP_ARB_CTL2) & DISP_DATA_PARTITION_5_6) ?
2971 INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03002972
2973 hw->enable_fbc_wm =
2974 !(I915_READ(DISP_ARB_CTL) & DISP_FBC_WM_DIS);
2975}
2976
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002977/**
2978 * intel_update_watermarks - update FIFO watermark values based on current modes
2979 *
2980 * Calculate watermark values for the various WM regs based on current mode
2981 * and plane configuration.
2982 *
2983 * There are several cases to deal with here:
2984 * - normal (i.e. non-self-refresh)
2985 * - self-refresh (SR) mode
2986 * - lines are large relative to FIFO size (buffer can hold up to 2)
2987 * - lines are small relative to FIFO size (buffer can hold more than 2
2988 * lines), so need to account for TLB latency
2989 *
2990 * The normal calculation is:
2991 * watermark = dotclock * bytes per pixel * latency
2992 * where latency is platform & configuration dependent (we assume pessimal
2993 * values here).
2994 *
2995 * The SR calculation is:
2996 * watermark = (trunc(latency/line time)+1) * surface width *
2997 * bytes per pixel
2998 * where
2999 * line time = htotal / dotclock
3000 * surface width = hdisplay for normal plane and 64 for cursor
3001 * and latency is assumed to be high, as above.
3002 *
3003 * The final value programmed to the register should always be rounded up,
3004 * and include an extra 2 entries to account for clock crossings.
3005 *
3006 * We don't use the sprite, so we can ignore that. And on Crestline we have
3007 * to set the non-SR watermarks to 8.
3008 */
Ville Syrjälä46ba6142013-09-10 11:40:40 +03003009void intel_update_watermarks(struct drm_crtc *crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03003010{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03003011 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03003012
3013 if (dev_priv->display.update_wm)
Ville Syrjälä46ba6142013-09-10 11:40:40 +03003014 dev_priv->display.update_wm(crtc);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03003015}
3016
Ville Syrjäläadf3d352013-08-06 22:24:11 +03003017void intel_update_sprite_watermarks(struct drm_plane *plane,
3018 struct drm_crtc *crtc,
Damien Lespiaued57cb82014-07-15 09:21:24 +02003019 uint32_t sprite_width,
3020 uint32_t sprite_height,
3021 int pixel_size,
Ville Syrjälä39db4a42013-08-06 22:24:00 +03003022 bool enabled, bool scaled)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03003023{
Ville Syrjäläadf3d352013-08-06 22:24:11 +03003024 struct drm_i915_private *dev_priv = plane->dev->dev_private;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03003025
3026 if (dev_priv->display.update_sprite_wm)
Damien Lespiaued57cb82014-07-15 09:21:24 +02003027 dev_priv->display.update_sprite_wm(plane, crtc,
3028 sprite_width, sprite_height,
Ville Syrjälä39db4a42013-08-06 22:24:00 +03003029 pixel_size, enabled, scaled);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03003030}
3031
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003032static struct drm_i915_gem_object *
3033intel_alloc_context_page(struct drm_device *dev)
3034{
3035 struct drm_i915_gem_object *ctx;
3036 int ret;
3037
3038 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
3039
3040 ctx = i915_gem_alloc_object(dev, 4096);
3041 if (!ctx) {
3042 DRM_DEBUG("failed to alloc power context, RC6 disabled\n");
3043 return NULL;
3044 }
3045
Daniel Vetterc69766f2014-02-14 14:01:17 +01003046 ret = i915_gem_obj_ggtt_pin(ctx, 4096, 0);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003047 if (ret) {
3048 DRM_ERROR("failed to pin power context: %d\n", ret);
3049 goto err_unref;
3050 }
3051
3052 ret = i915_gem_object_set_to_gtt_domain(ctx, 1);
3053 if (ret) {
3054 DRM_ERROR("failed to set-domain on power context: %d\n", ret);
3055 goto err_unpin;
3056 }
3057
3058 return ctx;
3059
3060err_unpin:
Ben Widawskyd7f46fc2013-12-06 14:10:55 -08003061 i915_gem_object_ggtt_unpin(ctx);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003062err_unref:
3063 drm_gem_object_unreference(&ctx->base);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003064 return NULL;
3065}
3066
Daniel Vetter92703882012-08-09 16:46:01 +02003067/**
3068 * Lock protecting IPS related data structures
Daniel Vetter92703882012-08-09 16:46:01 +02003069 */
3070DEFINE_SPINLOCK(mchdev_lock);
3071
3072/* Global for IPS driver to get at the current i915 device. Protected by
3073 * mchdev_lock. */
3074static struct drm_i915_private *i915_mch_dev;
3075
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003076bool ironlake_set_drps(struct drm_device *dev, u8 val)
3077{
3078 struct drm_i915_private *dev_priv = dev->dev_private;
3079 u16 rgvswctl;
3080
Daniel Vetter92703882012-08-09 16:46:01 +02003081 assert_spin_locked(&mchdev_lock);
3082
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003083 rgvswctl = I915_READ16(MEMSWCTL);
3084 if (rgvswctl & MEMCTL_CMD_STS) {
3085 DRM_DEBUG("gpu busy, RCS change rejected\n");
3086 return false; /* still busy with another command */
3087 }
3088
3089 rgvswctl = (MEMCTL_CMD_CHFREQ << MEMCTL_CMD_SHIFT) |
3090 (val << MEMCTL_FREQ_SHIFT) | MEMCTL_SFCAVM;
3091 I915_WRITE16(MEMSWCTL, rgvswctl);
3092 POSTING_READ16(MEMSWCTL);
3093
3094 rgvswctl |= MEMCTL_CMD_STS;
3095 I915_WRITE16(MEMSWCTL, rgvswctl);
3096
3097 return true;
3098}
3099
Daniel Vetter8090c6b2012-06-24 16:42:32 +02003100static void ironlake_enable_drps(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003101{
3102 struct drm_i915_private *dev_priv = dev->dev_private;
3103 u32 rgvmodectl = I915_READ(MEMMODECTL);
3104 u8 fmax, fmin, fstart, vstart;
3105
Daniel Vetter92703882012-08-09 16:46:01 +02003106 spin_lock_irq(&mchdev_lock);
3107
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003108 /* Enable temp reporting */
3109 I915_WRITE16(PMMISC, I915_READ(PMMISC) | MCPPCE_EN);
3110 I915_WRITE16(TSC1, I915_READ(TSC1) | TSE);
3111
3112 /* 100ms RC evaluation intervals */
3113 I915_WRITE(RCUPEI, 100000);
3114 I915_WRITE(RCDNEI, 100000);
3115
3116 /* Set max/min thresholds to 90ms and 80ms respectively */
3117 I915_WRITE(RCBMAXAVG, 90000);
3118 I915_WRITE(RCBMINAVG, 80000);
3119
3120 I915_WRITE(MEMIHYST, 1);
3121
3122 /* Set up min, max, and cur for interrupt handling */
3123 fmax = (rgvmodectl & MEMMODE_FMAX_MASK) >> MEMMODE_FMAX_SHIFT;
3124 fmin = (rgvmodectl & MEMMODE_FMIN_MASK);
3125 fstart = (rgvmodectl & MEMMODE_FSTART_MASK) >>
3126 MEMMODE_FSTART_SHIFT;
3127
3128 vstart = (I915_READ(PXVFREQ_BASE + (fstart * 4)) & PXVFREQ_PX_MASK) >>
3129 PXVFREQ_PX_SHIFT;
3130
Daniel Vetter20e4d402012-08-08 23:35:39 +02003131 dev_priv->ips.fmax = fmax; /* IPS callback will increase this */
3132 dev_priv->ips.fstart = fstart;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003133
Daniel Vetter20e4d402012-08-08 23:35:39 +02003134 dev_priv->ips.max_delay = fstart;
3135 dev_priv->ips.min_delay = fmin;
3136 dev_priv->ips.cur_delay = fstart;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003137
3138 DRM_DEBUG_DRIVER("fmax: %d, fmin: %d, fstart: %d\n",
3139 fmax, fmin, fstart);
3140
3141 I915_WRITE(MEMINTREN, MEMINT_CX_SUPR_EN | MEMINT_EVAL_CHG_EN);
3142
3143 /*
3144 * Interrupts will be enabled in ironlake_irq_postinstall
3145 */
3146
3147 I915_WRITE(VIDSTART, vstart);
3148 POSTING_READ(VIDSTART);
3149
3150 rgvmodectl |= MEMMODE_SWMODE_EN;
3151 I915_WRITE(MEMMODECTL, rgvmodectl);
3152
Daniel Vetter92703882012-08-09 16:46:01 +02003153 if (wait_for_atomic((I915_READ(MEMSWCTL) & MEMCTL_CMD_STS) == 0, 10))
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003154 DRM_ERROR("stuck trying to change perf mode\n");
Daniel Vetter92703882012-08-09 16:46:01 +02003155 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003156
3157 ironlake_set_drps(dev, fstart);
3158
Daniel Vetter20e4d402012-08-08 23:35:39 +02003159 dev_priv->ips.last_count1 = I915_READ(0x112e4) + I915_READ(0x112e8) +
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003160 I915_READ(0x112e0);
Daniel Vetter20e4d402012-08-08 23:35:39 +02003161 dev_priv->ips.last_time1 = jiffies_to_msecs(jiffies);
3162 dev_priv->ips.last_count2 = I915_READ(0x112f4);
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00003163 dev_priv->ips.last_time2 = ktime_get_raw_ns();
Daniel Vetter92703882012-08-09 16:46:01 +02003164
3165 spin_unlock_irq(&mchdev_lock);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003166}
3167
Daniel Vetter8090c6b2012-06-24 16:42:32 +02003168static void ironlake_disable_drps(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003169{
3170 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter92703882012-08-09 16:46:01 +02003171 u16 rgvswctl;
3172
3173 spin_lock_irq(&mchdev_lock);
3174
3175 rgvswctl = I915_READ16(MEMSWCTL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003176
3177 /* Ack interrupts, disable EFC interrupt */
3178 I915_WRITE(MEMINTREN, I915_READ(MEMINTREN) & ~MEMINT_EVAL_CHG_EN);
3179 I915_WRITE(MEMINTRSTS, MEMINT_EVAL_CHG);
3180 I915_WRITE(DEIER, I915_READ(DEIER) & ~DE_PCU_EVENT);
3181 I915_WRITE(DEIIR, DE_PCU_EVENT);
3182 I915_WRITE(DEIMR, I915_READ(DEIMR) | DE_PCU_EVENT);
3183
3184 /* Go back to the starting frequency */
Daniel Vetter20e4d402012-08-08 23:35:39 +02003185 ironlake_set_drps(dev, dev_priv->ips.fstart);
Daniel Vetter92703882012-08-09 16:46:01 +02003186 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003187 rgvswctl |= MEMCTL_CMD_STS;
3188 I915_WRITE(MEMSWCTL, rgvswctl);
Daniel Vetter92703882012-08-09 16:46:01 +02003189 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003190
Daniel Vetter92703882012-08-09 16:46:01 +02003191 spin_unlock_irq(&mchdev_lock);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003192}
3193
Daniel Vetteracbe9472012-07-26 11:50:05 +02003194/* There's a funny hw issue where the hw returns all 0 when reading from
3195 * GEN6_RP_INTERRUPT_LIMITS. Hence we always need to compute the desired value
3196 * ourselves, instead of doing a rmw cycle (which might result in us clearing
3197 * all limits and the gpu stuck at whatever frequency it is at atm).
3198 */
Chris Wilson6917c7b2013-11-06 13:56:26 -02003199static u32 gen6_rps_limits(struct drm_i915_private *dev_priv, u8 val)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003200{
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01003201 u32 limits;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003202
Daniel Vetter20b46e52012-07-26 11:16:14 +02003203 /* Only set the down limit when we've reached the lowest level to avoid
3204 * getting more interrupts, otherwise leave this clear. This prevents a
3205 * race in the hw when coming out of rc6: There's a tiny window where
3206 * the hw runs at the minimal clock before selecting the desired
3207 * frequency, if the down threshold expires in that window we will not
3208 * receive a down interrupt. */
Ben Widawskyb39fb292014-03-19 18:31:11 -07003209 limits = dev_priv->rps.max_freq_softlimit << 24;
3210 if (val <= dev_priv->rps.min_freq_softlimit)
3211 limits |= dev_priv->rps.min_freq_softlimit << 16;
Daniel Vetter20b46e52012-07-26 11:16:14 +02003212
3213 return limits;
3214}
3215
Chris Wilsondd75fdc2013-09-25 17:34:57 +01003216static void gen6_set_rps_thresholds(struct drm_i915_private *dev_priv, u8 val)
3217{
3218 int new_power;
3219
3220 new_power = dev_priv->rps.power;
3221 switch (dev_priv->rps.power) {
3222 case LOW_POWER:
Ben Widawskyb39fb292014-03-19 18:31:11 -07003223 if (val > dev_priv->rps.efficient_freq + 1 && val > dev_priv->rps.cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01003224 new_power = BETWEEN;
3225 break;
3226
3227 case BETWEEN:
Ben Widawskyb39fb292014-03-19 18:31:11 -07003228 if (val <= dev_priv->rps.efficient_freq && val < dev_priv->rps.cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01003229 new_power = LOW_POWER;
Ben Widawskyb39fb292014-03-19 18:31:11 -07003230 else if (val >= dev_priv->rps.rp0_freq && val > dev_priv->rps.cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01003231 new_power = HIGH_POWER;
3232 break;
3233
3234 case HIGH_POWER:
Ben Widawskyb39fb292014-03-19 18:31:11 -07003235 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 +01003236 new_power = BETWEEN;
3237 break;
3238 }
3239 /* Max/min bins are special */
Ben Widawskyb39fb292014-03-19 18:31:11 -07003240 if (val == dev_priv->rps.min_freq_softlimit)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01003241 new_power = LOW_POWER;
Ben Widawskyb39fb292014-03-19 18:31:11 -07003242 if (val == dev_priv->rps.max_freq_softlimit)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01003243 new_power = HIGH_POWER;
3244 if (new_power == dev_priv->rps.power)
3245 return;
3246
3247 /* Note the units here are not exactly 1us, but 1280ns. */
3248 switch (new_power) {
3249 case LOW_POWER:
3250 /* Upclock if more than 95% busy over 16ms */
3251 I915_WRITE(GEN6_RP_UP_EI, 12500);
3252 I915_WRITE(GEN6_RP_UP_THRESHOLD, 11800);
3253
3254 /* Downclock if less than 85% busy over 32ms */
3255 I915_WRITE(GEN6_RP_DOWN_EI, 25000);
3256 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 21250);
3257
3258 I915_WRITE(GEN6_RP_CONTROL,
3259 GEN6_RP_MEDIA_TURBO |
3260 GEN6_RP_MEDIA_HW_NORMAL_MODE |
3261 GEN6_RP_MEDIA_IS_GFX |
3262 GEN6_RP_ENABLE |
3263 GEN6_RP_UP_BUSY_AVG |
3264 GEN6_RP_DOWN_IDLE_AVG);
3265 break;
3266
3267 case BETWEEN:
3268 /* Upclock if more than 90% busy over 13ms */
3269 I915_WRITE(GEN6_RP_UP_EI, 10250);
3270 I915_WRITE(GEN6_RP_UP_THRESHOLD, 9225);
3271
3272 /* Downclock if less than 75% busy over 32ms */
3273 I915_WRITE(GEN6_RP_DOWN_EI, 25000);
3274 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 18750);
3275
3276 I915_WRITE(GEN6_RP_CONTROL,
3277 GEN6_RP_MEDIA_TURBO |
3278 GEN6_RP_MEDIA_HW_NORMAL_MODE |
3279 GEN6_RP_MEDIA_IS_GFX |
3280 GEN6_RP_ENABLE |
3281 GEN6_RP_UP_BUSY_AVG |
3282 GEN6_RP_DOWN_IDLE_AVG);
3283 break;
3284
3285 case HIGH_POWER:
3286 /* Upclock if more than 85% busy over 10ms */
3287 I915_WRITE(GEN6_RP_UP_EI, 8000);
3288 I915_WRITE(GEN6_RP_UP_THRESHOLD, 6800);
3289
3290 /* Downclock if less than 60% busy over 32ms */
3291 I915_WRITE(GEN6_RP_DOWN_EI, 25000);
3292 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 15000);
3293
3294 I915_WRITE(GEN6_RP_CONTROL,
3295 GEN6_RP_MEDIA_TURBO |
3296 GEN6_RP_MEDIA_HW_NORMAL_MODE |
3297 GEN6_RP_MEDIA_IS_GFX |
3298 GEN6_RP_ENABLE |
3299 GEN6_RP_UP_BUSY_AVG |
3300 GEN6_RP_DOWN_IDLE_AVG);
3301 break;
3302 }
3303
3304 dev_priv->rps.power = new_power;
3305 dev_priv->rps.last_adj = 0;
3306}
3307
Chris Wilson2876ce72014-03-28 08:03:34 +00003308static u32 gen6_rps_pm_mask(struct drm_i915_private *dev_priv, u8 val)
3309{
3310 u32 mask = 0;
3311
3312 if (val > dev_priv->rps.min_freq_softlimit)
3313 mask |= GEN6_PM_RP_DOWN_THRESHOLD | GEN6_PM_RP_DOWN_TIMEOUT;
3314 if (val < dev_priv->rps.max_freq_softlimit)
3315 mask |= GEN6_PM_RP_UP_THRESHOLD;
3316
Chris Wilson7b3c29f2014-07-10 20:31:19 +01003317 mask |= dev_priv->pm_rps_events & (GEN6_PM_RP_DOWN_EI_EXPIRED | GEN6_PM_RP_UP_EI_EXPIRED);
3318 mask &= dev_priv->pm_rps_events;
3319
Chris Wilson2876ce72014-03-28 08:03:34 +00003320 /* IVB and SNB hard hangs on looping batchbuffer
3321 * if GEN6_PM_UP_EI_EXPIRED is masked.
3322 */
3323 if (INTEL_INFO(dev_priv->dev)->gen <= 7 && !IS_HASWELL(dev_priv->dev))
3324 mask |= GEN6_PM_RP_UP_EI_EXPIRED;
3325
Deepak Sbaccd452014-05-15 20:58:09 +03003326 if (IS_GEN8(dev_priv->dev))
3327 mask |= GEN8_PMINTR_REDIRECT_TO_NON_DISP;
3328
Chris Wilson2876ce72014-03-28 08:03:34 +00003329 return ~mask;
3330}
3331
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06003332/* gen6_set_rps is called to update the frequency request, but should also be
3333 * called when the range (min_delay and max_delay) is modified so that we can
3334 * update the GEN6_RP_INTERRUPT_LIMITS register accordingly. */
Daniel Vetter20b46e52012-07-26 11:16:14 +02003335void gen6_set_rps(struct drm_device *dev, u8 val)
3336{
3337 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01003338
Jesse Barnes4fc688c2012-11-02 11:14:01 -07003339 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Ben Widawskyb39fb292014-03-19 18:31:11 -07003340 WARN_ON(val > dev_priv->rps.max_freq_softlimit);
3341 WARN_ON(val < dev_priv->rps.min_freq_softlimit);
Daniel Vetter004777c2012-08-09 15:07:01 +02003342
Chris Wilsoneb64cad2014-03-27 08:24:20 +00003343 /* min/max delay may still have been modified so be sure to
3344 * write the limits value.
3345 */
3346 if (val != dev_priv->rps.cur_freq) {
3347 gen6_set_rps_thresholds(dev_priv, val);
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06003348
Ben Widawsky50e6a2a2014-03-31 17:16:43 -07003349 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Chris Wilsoneb64cad2014-03-27 08:24:20 +00003350 I915_WRITE(GEN6_RPNSWREQ,
3351 HSW_FREQUENCY(val));
3352 else
3353 I915_WRITE(GEN6_RPNSWREQ,
3354 GEN6_FREQUENCY(val) |
3355 GEN6_OFFSET(0) |
3356 GEN6_AGGRESSIVE_TURBO);
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06003357 }
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01003358
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01003359 /* Make sure we continue to get interrupts
3360 * until we hit the minimum or maximum frequencies.
3361 */
Chris Wilsoneb64cad2014-03-27 08:24:20 +00003362 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS, gen6_rps_limits(dev_priv, val));
Chris Wilson2876ce72014-03-28 08:03:34 +00003363 I915_WRITE(GEN6_PMINTRMSK, gen6_rps_pm_mask(dev_priv, val));
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01003364
Ben Widawskyd5570a72012-09-07 19:43:41 -07003365 POSTING_READ(GEN6_RPNSWREQ);
3366
Ben Widawskyb39fb292014-03-19 18:31:11 -07003367 dev_priv->rps.cur_freq = val;
Daniel Vetterbe2cde9a2012-08-30 13:26:48 +02003368 trace_intel_gpu_freq_change(val * 50);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003369}
3370
Deepak S76c3552f2014-01-30 23:08:16 +05303371/* vlv_set_rps_idle: Set the frequency to Rpn if Gfx clocks are down
3372 *
3373 * * If Gfx is Idle, then
3374 * 1. Mask Turbo interrupts
3375 * 2. Bring up Gfx clock
3376 * 3. Change the freq to Rpn and wait till P-Unit updates freq
3377 * 4. Clear the Force GFX CLK ON bit so that Gfx can down
3378 * 5. Unmask Turbo interrupts
3379*/
3380static void vlv_set_rps_idle(struct drm_i915_private *dev_priv)
3381{
Deepak S5549d252014-06-28 11:26:11 +05303382 struct drm_device *dev = dev_priv->dev;
3383
3384 /* Latest VLV doesn't need to force the gfx clock */
3385 if (dev->pdev->revision >= 0xd) {
3386 valleyview_set_rps(dev_priv->dev, dev_priv->rps.min_freq_softlimit);
3387 return;
3388 }
3389
Deepak S76c3552f2014-01-30 23:08:16 +05303390 /*
3391 * When we are idle. Drop to min voltage state.
3392 */
3393
Ben Widawskyb39fb292014-03-19 18:31:11 -07003394 if (dev_priv->rps.cur_freq <= dev_priv->rps.min_freq_softlimit)
Deepak S76c3552f2014-01-30 23:08:16 +05303395 return;
3396
3397 /* Mask turbo interrupt so that they will not come in between */
3398 I915_WRITE(GEN6_PMINTRMSK, 0xffffffff);
3399
Imre Deak650ad972014-04-18 16:35:02 +03003400 vlv_force_gfx_clock(dev_priv, true);
Deepak S76c3552f2014-01-30 23:08:16 +05303401
Ben Widawskyb39fb292014-03-19 18:31:11 -07003402 dev_priv->rps.cur_freq = dev_priv->rps.min_freq_softlimit;
Deepak S76c3552f2014-01-30 23:08:16 +05303403
3404 vlv_punit_write(dev_priv, PUNIT_REG_GPU_FREQ_REQ,
Ben Widawskyb39fb292014-03-19 18:31:11 -07003405 dev_priv->rps.min_freq_softlimit);
Deepak S76c3552f2014-01-30 23:08:16 +05303406
3407 if (wait_for(((vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS))
3408 & GENFREQSTATUS) == 0, 5))
3409 DRM_ERROR("timed out waiting for Punit\n");
3410
Imre Deak650ad972014-04-18 16:35:02 +03003411 vlv_force_gfx_clock(dev_priv, false);
Deepak S76c3552f2014-01-30 23:08:16 +05303412
Chris Wilson2876ce72014-03-28 08:03:34 +00003413 I915_WRITE(GEN6_PMINTRMSK,
3414 gen6_rps_pm_mask(dev_priv, dev_priv->rps.cur_freq));
Deepak S76c3552f2014-01-30 23:08:16 +05303415}
3416
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003417void gen6_rps_idle(struct drm_i915_private *dev_priv)
3418{
Damien Lespiau691bb712013-12-12 14:36:36 +00003419 struct drm_device *dev = dev_priv->dev;
3420
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003421 mutex_lock(&dev_priv->rps.hw_lock);
Chris Wilsonc0951f02013-10-10 21:58:50 +01003422 if (dev_priv->rps.enabled) {
Deepak S34638112014-06-28 11:26:26 +05303423 if (IS_CHERRYVIEW(dev))
3424 valleyview_set_rps(dev_priv->dev, dev_priv->rps.min_freq_softlimit);
3425 else if (IS_VALLEYVIEW(dev))
Deepak S76c3552f2014-01-30 23:08:16 +05303426 vlv_set_rps_idle(dev_priv);
Chris Wilsonc0951f02013-10-10 21:58:50 +01003427 else
Ben Widawskyb39fb292014-03-19 18:31:11 -07003428 gen6_set_rps(dev_priv->dev, dev_priv->rps.min_freq_softlimit);
Chris Wilsonc0951f02013-10-10 21:58:50 +01003429 dev_priv->rps.last_adj = 0;
3430 }
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003431 mutex_unlock(&dev_priv->rps.hw_lock);
3432}
3433
3434void gen6_rps_boost(struct drm_i915_private *dev_priv)
3435{
Damien Lespiau691bb712013-12-12 14:36:36 +00003436 struct drm_device *dev = dev_priv->dev;
3437
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003438 mutex_lock(&dev_priv->rps.hw_lock);
Chris Wilsonc0951f02013-10-10 21:58:50 +01003439 if (dev_priv->rps.enabled) {
Damien Lespiau691bb712013-12-12 14:36:36 +00003440 if (IS_VALLEYVIEW(dev))
Ben Widawskyb39fb292014-03-19 18:31:11 -07003441 valleyview_set_rps(dev_priv->dev, dev_priv->rps.max_freq_softlimit);
Chris Wilsonc0951f02013-10-10 21:58:50 +01003442 else
Ben Widawskyb39fb292014-03-19 18:31:11 -07003443 gen6_set_rps(dev_priv->dev, dev_priv->rps.max_freq_softlimit);
Chris Wilsonc0951f02013-10-10 21:58:50 +01003444 dev_priv->rps.last_adj = 0;
3445 }
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003446 mutex_unlock(&dev_priv->rps.hw_lock);
3447}
3448
Jesse Barnes0a073b82013-04-17 15:54:58 -07003449void valleyview_set_rps(struct drm_device *dev, u8 val)
3450{
3451 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä7a670922013-06-25 19:21:06 +03003452
Jesse Barnes0a073b82013-04-17 15:54:58 -07003453 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Ben Widawskyb39fb292014-03-19 18:31:11 -07003454 WARN_ON(val > dev_priv->rps.max_freq_softlimit);
3455 WARN_ON(val < dev_priv->rps.min_freq_softlimit);
Jesse Barnes0a073b82013-04-17 15:54:58 -07003456
Ville Syrjälä73008b92013-06-25 19:21:01 +03003457 DRM_DEBUG_DRIVER("GPU freq request from %d MHz (%u) to %d MHz (%u)\n",
Ben Widawskyb39fb292014-03-19 18:31:11 -07003458 vlv_gpu_freq(dev_priv, dev_priv->rps.cur_freq),
3459 dev_priv->rps.cur_freq,
Ville Syrjälä2ec38152013-11-05 22:42:29 +02003460 vlv_gpu_freq(dev_priv, val), val);
Jesse Barnes0a073b82013-04-17 15:54:58 -07003461
Chris Wilson2876ce72014-03-28 08:03:34 +00003462 if (val != dev_priv->rps.cur_freq)
3463 vlv_punit_write(dev_priv, PUNIT_REG_GPU_FREQ_REQ, val);
Jesse Barnes0a073b82013-04-17 15:54:58 -07003464
Imre Deak09c87db2014-04-03 20:02:42 +03003465 I915_WRITE(GEN6_PMINTRMSK, gen6_rps_pm_mask(dev_priv, val));
Jesse Barnes0a073b82013-04-17 15:54:58 -07003466
Ben Widawskyb39fb292014-03-19 18:31:11 -07003467 dev_priv->rps.cur_freq = val;
Ville Syrjälä2ec38152013-11-05 22:42:29 +02003468 trace_intel_gpu_freq_change(vlv_gpu_freq(dev_priv, val));
Jesse Barnes0a073b82013-04-17 15:54:58 -07003469}
3470
Ben Widawsky09610212014-05-15 20:58:08 +03003471static void gen8_disable_rps_interrupts(struct drm_device *dev)
3472{
3473 struct drm_i915_private *dev_priv = dev->dev_private;
3474
Mika Kuoppala992f1912014-05-16 13:44:12 +03003475 I915_WRITE(GEN6_PMINTRMSK, ~GEN8_PMINTR_REDIRECT_TO_NON_DISP);
Ben Widawsky09610212014-05-15 20:58:08 +03003476 I915_WRITE(GEN8_GT_IER(2), I915_READ(GEN8_GT_IER(2)) &
3477 ~dev_priv->pm_rps_events);
3478 /* Complete PM interrupt masking here doesn't race with the rps work
3479 * item again unmasking PM interrupts because that is using a different
3480 * register (GEN8_GT_IMR(2)) to mask PM interrupts. The only risk is in
3481 * leaving stale bits in GEN8_GT_IIR(2) and GEN8_GT_IMR(2) which
3482 * gen8_enable_rps will clean up. */
3483
3484 spin_lock_irq(&dev_priv->irq_lock);
3485 dev_priv->rps.pm_iir = 0;
3486 spin_unlock_irq(&dev_priv->irq_lock);
3487
3488 I915_WRITE(GEN8_GT_IIR(2), dev_priv->pm_rps_events);
3489}
3490
Daniel Vetter44fc7d52013-07-12 22:43:27 +02003491static void gen6_disable_rps_interrupts(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003492{
3493 struct drm_i915_private *dev_priv = dev->dev_private;
3494
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003495 I915_WRITE(GEN6_PMINTRMSK, 0xffffffff);
Deepak Sa6706b42014-03-15 20:23:22 +05303496 I915_WRITE(GEN6_PMIER, I915_READ(GEN6_PMIER) &
3497 ~dev_priv->pm_rps_events);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003498 /* Complete PM interrupt masking here doesn't race with the rps work
3499 * item again unmasking PM interrupts because that is using a different
3500 * register (PMIMR) to mask PM interrupts. The only risk is in leaving
3501 * stale bits in PMIIR and PMIMR which gen6_enable_rps will clean up. */
3502
Daniel Vetter59cdb632013-07-04 23:35:28 +02003503 spin_lock_irq(&dev_priv->irq_lock);
Daniel Vetterc6a828d2012-08-08 23:35:35 +02003504 dev_priv->rps.pm_iir = 0;
Daniel Vetter59cdb632013-07-04 23:35:28 +02003505 spin_unlock_irq(&dev_priv->irq_lock);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003506
Deepak Sa6706b42014-03-15 20:23:22 +05303507 I915_WRITE(GEN6_PMIIR, dev_priv->pm_rps_events);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003508}
3509
Daniel Vetter44fc7d52013-07-12 22:43:27 +02003510static void gen6_disable_rps(struct drm_device *dev)
3511{
3512 struct drm_i915_private *dev_priv = dev->dev_private;
3513
3514 I915_WRITE(GEN6_RC_CONTROL, 0);
3515 I915_WRITE(GEN6_RPNSWREQ, 1 << 31);
3516
Ben Widawsky09610212014-05-15 20:58:08 +03003517 if (IS_BROADWELL(dev))
3518 gen8_disable_rps_interrupts(dev);
3519 else
3520 gen6_disable_rps_interrupts(dev);
Daniel Vetter44fc7d52013-07-12 22:43:27 +02003521}
3522
Deepak S38807742014-05-23 21:00:15 +05303523static void cherryview_disable_rps(struct drm_device *dev)
3524{
3525 struct drm_i915_private *dev_priv = dev->dev_private;
3526
3527 I915_WRITE(GEN6_RC_CONTROL, 0);
Deepak S3497a562014-07-10 13:16:26 +05303528
3529 gen8_disable_rps_interrupts(dev);
Deepak S38807742014-05-23 21:00:15 +05303530}
3531
Jesse Barnesd20d4f02013-04-23 10:09:28 -07003532static void valleyview_disable_rps(struct drm_device *dev)
3533{
3534 struct drm_i915_private *dev_priv = dev->dev_private;
3535
3536 I915_WRITE(GEN6_RC_CONTROL, 0);
Jesse Barnesd20d4f02013-04-23 10:09:28 -07003537
Daniel Vetter44fc7d52013-07-12 22:43:27 +02003538 gen6_disable_rps_interrupts(dev);
Jesse Barnesd20d4f02013-04-23 10:09:28 -07003539}
3540
Ben Widawskydc39fff2013-10-18 12:32:07 -07003541static void intel_print_rc6_info(struct drm_device *dev, u32 mode)
3542{
Imre Deak91ca6892014-04-14 20:24:25 +03003543 if (IS_VALLEYVIEW(dev)) {
3544 if (mode & (GEN7_RC_CTL_TO_MODE | GEN6_RC_CTL_EI_MODE(1)))
3545 mode = GEN6_RC_CTL_RC6_ENABLE;
3546 else
3547 mode = 0;
3548 }
Daniel Vetter8dfd1f02014-08-04 11:15:56 +02003549 DRM_DEBUG_KMS("Enabling RC6 states: RC6 %s, RC6p %s, RC6pp %s\n",
3550 (mode & GEN6_RC_CTL_RC6_ENABLE) ? "on" : "off",
3551 (mode & GEN6_RC_CTL_RC6p_ENABLE) ? "on" : "off",
3552 (mode & GEN6_RC_CTL_RC6pp_ENABLE) ? "on" : "off");
Ben Widawskydc39fff2013-10-18 12:32:07 -07003553}
3554
Imre Deake6069ca2014-04-18 16:01:02 +03003555static int sanitize_rc6_option(const struct drm_device *dev, int enable_rc6)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003556{
Damien Lespiaueb4926e2013-06-07 17:41:14 +01003557 /* No RC6 before Ironlake */
3558 if (INTEL_INFO(dev)->gen < 5)
3559 return 0;
3560
Imre Deake6069ca2014-04-18 16:01:02 +03003561 /* RC6 is only on Ironlake mobile not on desktop */
3562 if (INTEL_INFO(dev)->gen == 5 && !IS_IRONLAKE_M(dev))
3563 return 0;
3564
Daniel Vetter456470e2012-08-08 23:35:40 +02003565 /* Respect the kernel parameter if it is set */
Imre Deake6069ca2014-04-18 16:01:02 +03003566 if (enable_rc6 >= 0) {
3567 int mask;
3568
3569 if (INTEL_INFO(dev)->gen == 6 || IS_IVYBRIDGE(dev))
3570 mask = INTEL_RC6_ENABLE | INTEL_RC6p_ENABLE |
3571 INTEL_RC6pp_ENABLE;
3572 else
3573 mask = INTEL_RC6_ENABLE;
3574
3575 if ((enable_rc6 & mask) != enable_rc6)
Daniel Vetter8dfd1f02014-08-04 11:15:56 +02003576 DRM_DEBUG_KMS("Adjusting RC6 mask to %d (requested %d, valid %d)\n",
3577 enable_rc6 & mask, enable_rc6, mask);
Imre Deake6069ca2014-04-18 16:01:02 +03003578
3579 return enable_rc6 & mask;
3580 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003581
Chris Wilson6567d742012-11-10 10:00:06 +00003582 /* Disable RC6 on Ironlake */
3583 if (INTEL_INFO(dev)->gen == 5)
3584 return 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003585
Ben Widawsky8bade1a2014-01-28 20:25:39 -08003586 if (IS_IVYBRIDGE(dev))
Ben Widawskycca84a12014-01-28 20:25:38 -08003587 return (INTEL_RC6_ENABLE | INTEL_RC6p_ENABLE);
Ben Widawsky8bade1a2014-01-28 20:25:39 -08003588
3589 return INTEL_RC6_ENABLE;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003590}
3591
Imre Deake6069ca2014-04-18 16:01:02 +03003592int intel_enable_rc6(const struct drm_device *dev)
3593{
3594 return i915.enable_rc6;
3595}
3596
Ben Widawsky09610212014-05-15 20:58:08 +03003597static void gen8_enable_rps_interrupts(struct drm_device *dev)
3598{
3599 struct drm_i915_private *dev_priv = dev->dev_private;
3600
3601 spin_lock_irq(&dev_priv->irq_lock);
3602 WARN_ON(dev_priv->rps.pm_iir);
Daniel Vetter480c8032014-07-16 09:49:40 +02003603 gen8_enable_pm_irq(dev_priv, dev_priv->pm_rps_events);
Ben Widawsky09610212014-05-15 20:58:08 +03003604 I915_WRITE(GEN8_GT_IIR(2), dev_priv->pm_rps_events);
3605 spin_unlock_irq(&dev_priv->irq_lock);
3606}
3607
Daniel Vetter44fc7d52013-07-12 22:43:27 +02003608static void gen6_enable_rps_interrupts(struct drm_device *dev)
3609{
3610 struct drm_i915_private *dev_priv = dev->dev_private;
3611
3612 spin_lock_irq(&dev_priv->irq_lock);
Daniel Vettera0b33352013-07-04 23:35:34 +02003613 WARN_ON(dev_priv->rps.pm_iir);
Daniel Vetter480c8032014-07-16 09:49:40 +02003614 gen6_enable_pm_irq(dev_priv, dev_priv->pm_rps_events);
Deepak Sa6706b42014-03-15 20:23:22 +05303615 I915_WRITE(GEN6_PMIIR, dev_priv->pm_rps_events);
Daniel Vetter44fc7d52013-07-12 22:43:27 +02003616 spin_unlock_irq(&dev_priv->irq_lock);
Daniel Vetter44fc7d52013-07-12 22:43:27 +02003617}
3618
Ben Widawsky3280e8b2014-03-31 17:16:42 -07003619static void parse_rp_state_cap(struct drm_i915_private *dev_priv, u32 rp_state_cap)
3620{
3621 /* All of these values are in units of 50MHz */
3622 dev_priv->rps.cur_freq = 0;
3623 /* static values from HW: RP0 < RPe < RP1 < RPn (min_freq) */
3624 dev_priv->rps.rp1_freq = (rp_state_cap >> 8) & 0xff;
3625 dev_priv->rps.rp0_freq = (rp_state_cap >> 0) & 0xff;
3626 dev_priv->rps.min_freq = (rp_state_cap >> 16) & 0xff;
3627 /* XXX: only BYT has a special efficient freq */
3628 dev_priv->rps.efficient_freq = dev_priv->rps.rp1_freq;
3629 /* hw_max = RP0 until we check for overclocking */
3630 dev_priv->rps.max_freq = dev_priv->rps.rp0_freq;
3631
3632 /* Preserve min/max settings in case of re-init */
3633 if (dev_priv->rps.max_freq_softlimit == 0)
3634 dev_priv->rps.max_freq_softlimit = dev_priv->rps.max_freq;
3635
3636 if (dev_priv->rps.min_freq_softlimit == 0)
3637 dev_priv->rps.min_freq_softlimit = dev_priv->rps.min_freq;
3638}
3639
Ben Widawsky6edee7f2013-11-02 21:07:52 -07003640static void gen8_enable_rps(struct drm_device *dev)
3641{
3642 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01003643 struct intel_engine_cs *ring;
Ben Widawsky6edee7f2013-11-02 21:07:52 -07003644 uint32_t rc6_mask = 0, rp_state_cap;
3645 int unused;
3646
3647 /* 1a: Software RC state - RC0 */
3648 I915_WRITE(GEN6_RC_STATE, 0);
3649
3650 /* 1c & 1d: Get forcewake during program sequence. Although the driver
3651 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
Deepak Sc8d9a592013-11-23 14:55:42 +05303652 gen6_gt_force_wake_get(dev_priv, FORCEWAKE_ALL);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07003653
3654 /* 2a: Disable RC states. */
3655 I915_WRITE(GEN6_RC_CONTROL, 0);
3656
3657 rp_state_cap = I915_READ(GEN6_RP_STATE_CAP);
Ben Widawsky3280e8b2014-03-31 17:16:42 -07003658 parse_rp_state_cap(dev_priv, rp_state_cap);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07003659
3660 /* 2b: Program RC6 thresholds.*/
3661 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16);
3662 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
3663 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
3664 for_each_ring(ring, dev_priv, unused)
3665 I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
3666 I915_WRITE(GEN6_RC_SLEEP, 0);
Tom O'Rourke0d68b252014-04-09 11:44:06 -07003667 if (IS_BROADWELL(dev))
3668 I915_WRITE(GEN6_RC6_THRESHOLD, 625); /* 800us/1.28 for TO */
3669 else
3670 I915_WRITE(GEN6_RC6_THRESHOLD, 50000); /* 50/125ms per EI */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07003671
3672 /* 3: Enable RC6 */
3673 if (intel_enable_rc6(dev) & INTEL_RC6_ENABLE)
3674 rc6_mask = GEN6_RC_CTL_RC6_ENABLE;
Ben Widawskyabbf9d22014-01-28 20:25:41 -08003675 intel_print_rc6_info(dev, rc6_mask);
Tom O'Rourke0d68b252014-04-09 11:44:06 -07003676 if (IS_BROADWELL(dev))
3677 I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
3678 GEN7_RC_CTL_TO_MODE |
3679 rc6_mask);
3680 else
3681 I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
3682 GEN6_RC_CTL_EI_MODE(1) |
3683 rc6_mask);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07003684
3685 /* 4 Program defaults and thresholds for RPS*/
Ben Widawskyf9bdc582014-03-31 17:16:41 -07003686 I915_WRITE(GEN6_RPNSWREQ,
3687 HSW_FREQUENCY(dev_priv->rps.rp1_freq));
3688 I915_WRITE(GEN6_RC_VIDEO_FREQ,
3689 HSW_FREQUENCY(dev_priv->rps.rp1_freq));
Ben Widawsky6edee7f2013-11-02 21:07:52 -07003690 /* NB: Docs say 1s, and 1000000 - which aren't equivalent */
3691 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 100000000 / 128); /* 1 second timeout */
3692
3693 /* Docs recommend 900MHz, and 300 MHz respectively */
3694 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS,
Ben Widawskyb39fb292014-03-19 18:31:11 -07003695 dev_priv->rps.max_freq_softlimit << 24 |
3696 dev_priv->rps.min_freq_softlimit << 16);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07003697
3698 I915_WRITE(GEN6_RP_UP_THRESHOLD, 7600000 / 128); /* 76ms busyness per EI, 90% */
3699 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 31300000 / 128); /* 313ms busyness per EI, 70%*/
3700 I915_WRITE(GEN6_RP_UP_EI, 66000); /* 84.48ms, XXX: random? */
3701 I915_WRITE(GEN6_RP_DOWN_EI, 350000); /* 448ms, XXX: random? */
3702
3703 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
3704
3705 /* 5: Enable RPS */
3706 I915_WRITE(GEN6_RP_CONTROL,
3707 GEN6_RP_MEDIA_TURBO |
3708 GEN6_RP_MEDIA_HW_NORMAL_MODE |
Tom O'Rourke223a6f22014-06-10 16:26:34 -07003709 GEN6_RP_MEDIA_IS_GFX |
Ben Widawsky6edee7f2013-11-02 21:07:52 -07003710 GEN6_RP_ENABLE |
3711 GEN6_RP_UP_BUSY_AVG |
3712 GEN6_RP_DOWN_IDLE_AVG);
3713
3714 /* 6: Ring frequency + overclocking (our driver does this later */
3715
3716 gen6_set_rps(dev, (I915_READ(GEN6_GT_PERF_STATUS) & 0xff00) >> 8);
3717
Ben Widawsky09610212014-05-15 20:58:08 +03003718 gen8_enable_rps_interrupts(dev);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07003719
Deepak Sc8d9a592013-11-23 14:55:42 +05303720 gen6_gt_force_wake_put(dev_priv, FORCEWAKE_ALL);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07003721}
3722
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02003723static void gen6_enable_rps(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003724{
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02003725 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01003726 struct intel_engine_cs *ring;
Ben Widawsky2a5913a2014-03-19 18:31:13 -07003727 u32 rp_state_cap;
Ben Widawskyd060c162014-03-19 18:31:08 -07003728 u32 rc6vids, pcu_mbox = 0, rc6_mask = 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003729 u32 gtfifodbg;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003730 int rc6_mode;
Ben Widawsky42c05262012-09-26 10:34:00 -07003731 int i, ret;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003732
Jesse Barnes4fc688c2012-11-02 11:14:01 -07003733 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02003734
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003735 /* Here begins a magic sequence of register writes to enable
3736 * auto-downclocking.
3737 *
3738 * Perhaps there might be some value in exposing these to
3739 * userspace...
3740 */
3741 I915_WRITE(GEN6_RC_STATE, 0);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003742
3743 /* Clear the DBG now so we don't confuse earlier errors */
3744 if ((gtfifodbg = I915_READ(GTFIFODBG))) {
3745 DRM_ERROR("GT fifo had a previous error %x\n", gtfifodbg);
3746 I915_WRITE(GTFIFODBG, gtfifodbg);
3747 }
3748
Deepak Sc8d9a592013-11-23 14:55:42 +05303749 gen6_gt_force_wake_get(dev_priv, FORCEWAKE_ALL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003750
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01003751 rp_state_cap = I915_READ(GEN6_RP_STATE_CAP);
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01003752
Ben Widawsky3280e8b2014-03-31 17:16:42 -07003753 parse_rp_state_cap(dev_priv, rp_state_cap);
Jeff McGeedd0a1aa2014-02-04 11:32:31 -06003754
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003755 /* disable the counters and set deterministic thresholds */
3756 I915_WRITE(GEN6_RC_CONTROL, 0);
3757
3758 I915_WRITE(GEN6_RC1_WAKE_RATE_LIMIT, 1000 << 16);
3759 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16 | 30);
3760 I915_WRITE(GEN6_RC6pp_WAKE_RATE_LIMIT, 30);
3761 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
3762 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
3763
Chris Wilsonb4519512012-05-11 14:29:30 +01003764 for_each_ring(ring, dev_priv, i)
3765 I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003766
3767 I915_WRITE(GEN6_RC_SLEEP, 0);
3768 I915_WRITE(GEN6_RC1e_THRESHOLD, 1000);
Daniel Vetter29c78f62013-11-16 16:04:26 +01003769 if (IS_IVYBRIDGE(dev))
Stéphane Marchesin351aa562013-08-13 11:55:17 -07003770 I915_WRITE(GEN6_RC6_THRESHOLD, 125000);
3771 else
3772 I915_WRITE(GEN6_RC6_THRESHOLD, 50000);
Stéphane Marchesin0920a482013-01-29 19:41:59 -08003773 I915_WRITE(GEN6_RC6p_THRESHOLD, 150000);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003774 I915_WRITE(GEN6_RC6pp_THRESHOLD, 64000); /* unused */
3775
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03003776 /* Check if we are enabling RC6 */
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003777 rc6_mode = intel_enable_rc6(dev_priv->dev);
3778 if (rc6_mode & INTEL_RC6_ENABLE)
3779 rc6_mask |= GEN6_RC_CTL_RC6_ENABLE;
3780
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03003781 /* We don't use those on Haswell */
3782 if (!IS_HASWELL(dev)) {
3783 if (rc6_mode & INTEL_RC6p_ENABLE)
3784 rc6_mask |= GEN6_RC_CTL_RC6p_ENABLE;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003785
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03003786 if (rc6_mode & INTEL_RC6pp_ENABLE)
3787 rc6_mask |= GEN6_RC_CTL_RC6pp_ENABLE;
3788 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003789
Ben Widawskydc39fff2013-10-18 12:32:07 -07003790 intel_print_rc6_info(dev, rc6_mask);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003791
3792 I915_WRITE(GEN6_RC_CONTROL,
3793 rc6_mask |
3794 GEN6_RC_CTL_EI_MODE(1) |
3795 GEN6_RC_CTL_HW_ENABLE);
3796
Chris Wilsondd75fdc2013-09-25 17:34:57 +01003797 /* Power down if completely idle for over 50ms */
3798 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 50000);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003799 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003800
Ben Widawsky42c05262012-09-26 10:34:00 -07003801 ret = sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_MIN_FREQ_TABLE, 0);
Ben Widawskyd060c162014-03-19 18:31:08 -07003802 if (ret)
Ben Widawsky42c05262012-09-26 10:34:00 -07003803 DRM_DEBUG_DRIVER("Failed to set the min frequency\n");
Ben Widawskyd060c162014-03-19 18:31:08 -07003804
3805 ret = sandybridge_pcode_read(dev_priv, GEN6_READ_OC_PARAMS, &pcu_mbox);
3806 if (!ret && (pcu_mbox & (1<<31))) { /* OC supported */
3807 DRM_DEBUG_DRIVER("Overclocking supported. Max: %dMHz, Overclock max: %dMHz\n",
Ben Widawskyb39fb292014-03-19 18:31:11 -07003808 (dev_priv->rps.max_freq_softlimit & 0xff) * 50,
Ben Widawskyd060c162014-03-19 18:31:08 -07003809 (pcu_mbox & 0xff) * 50);
Ben Widawskyb39fb292014-03-19 18:31:11 -07003810 dev_priv->rps.max_freq = pcu_mbox & 0xff;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003811 }
3812
Chris Wilsondd75fdc2013-09-25 17:34:57 +01003813 dev_priv->rps.power = HIGH_POWER; /* force a reset */
Ben Widawskyb39fb292014-03-19 18:31:11 -07003814 gen6_set_rps(dev_priv->dev, dev_priv->rps.min_freq_softlimit);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003815
Daniel Vetter44fc7d52013-07-12 22:43:27 +02003816 gen6_enable_rps_interrupts(dev);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003817
Ben Widawsky31643d52012-09-26 10:34:01 -07003818 rc6vids = 0;
3819 ret = sandybridge_pcode_read(dev_priv, GEN6_PCODE_READ_RC6VIDS, &rc6vids);
3820 if (IS_GEN6(dev) && ret) {
3821 DRM_DEBUG_DRIVER("Couldn't check for BIOS workaround\n");
3822 } else if (IS_GEN6(dev) && (GEN6_DECODE_RC6_VID(rc6vids & 0xff) < 450)) {
3823 DRM_DEBUG_DRIVER("You should update your BIOS. Correcting minimum rc6 voltage (%dmV->%dmV)\n",
3824 GEN6_DECODE_RC6_VID(rc6vids & 0xff), 450);
3825 rc6vids &= 0xffff00;
3826 rc6vids |= GEN6_ENCODE_RC6_VID(450);
3827 ret = sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_RC6VIDS, rc6vids);
3828 if (ret)
3829 DRM_ERROR("Couldn't fix incorrect rc6 voltage\n");
3830 }
3831
Deepak Sc8d9a592013-11-23 14:55:42 +05303832 gen6_gt_force_wake_put(dev_priv, FORCEWAKE_ALL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003833}
3834
Imre Deakc2bc2fc2014-04-18 16:16:23 +03003835static void __gen6_update_ring_freq(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003836{
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02003837 struct drm_i915_private *dev_priv = dev->dev_private;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003838 int min_freq = 15;
Chris Wilson3ebecd02013-04-12 19:10:13 +01003839 unsigned int gpu_freq;
3840 unsigned int max_ia_freq, min_ring_freq;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003841 int scaling_factor = 180;
Ben Widawskyeda79642013-10-07 17:15:48 -03003842 struct cpufreq_policy *policy;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003843
Jesse Barnes4fc688c2012-11-02 11:14:01 -07003844 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02003845
Ben Widawskyeda79642013-10-07 17:15:48 -03003846 policy = cpufreq_cpu_get(0);
3847 if (policy) {
3848 max_ia_freq = policy->cpuinfo.max_freq;
3849 cpufreq_cpu_put(policy);
3850 } else {
3851 /*
3852 * Default to measured freq if none found, PCU will ensure we
3853 * don't go over
3854 */
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003855 max_ia_freq = tsc_khz;
Ben Widawskyeda79642013-10-07 17:15:48 -03003856 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003857
3858 /* Convert from kHz to MHz */
3859 max_ia_freq /= 1000;
3860
Ben Widawsky153b4b952013-10-22 22:05:09 -07003861 min_ring_freq = I915_READ(DCLK) & 0xf;
Ben Widawskyf6aca452013-10-02 09:25:02 -07003862 /* convert DDR frequency from units of 266.6MHz to bandwidth */
3863 min_ring_freq = mult_frac(min_ring_freq, 8, 3);
Chris Wilson3ebecd02013-04-12 19:10:13 +01003864
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003865 /*
3866 * For each potential GPU frequency, load a ring frequency we'd like
3867 * to use for memory access. We do this by specifying the IA frequency
3868 * the PCU should use as a reference to determine the ring frequency.
3869 */
Ben Widawskyb39fb292014-03-19 18:31:11 -07003870 for (gpu_freq = dev_priv->rps.max_freq_softlimit; gpu_freq >= dev_priv->rps.min_freq_softlimit;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003871 gpu_freq--) {
Ben Widawskyb39fb292014-03-19 18:31:11 -07003872 int diff = dev_priv->rps.max_freq_softlimit - gpu_freq;
Chris Wilson3ebecd02013-04-12 19:10:13 +01003873 unsigned int ia_freq = 0, ring_freq = 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003874
Ben Widawsky46c764d2013-11-02 21:07:49 -07003875 if (INTEL_INFO(dev)->gen >= 8) {
3876 /* max(2 * GT, DDR). NB: GT is 50MHz units */
3877 ring_freq = max(min_ring_freq, gpu_freq);
3878 } else if (IS_HASWELL(dev)) {
Ben Widawskyf6aca452013-10-02 09:25:02 -07003879 ring_freq = mult_frac(gpu_freq, 5, 4);
Chris Wilson3ebecd02013-04-12 19:10:13 +01003880 ring_freq = max(min_ring_freq, ring_freq);
3881 /* leave ia_freq as the default, chosen by cpufreq */
3882 } else {
3883 /* On older processors, there is no separate ring
3884 * clock domain, so in order to boost the bandwidth
3885 * of the ring, we need to upclock the CPU (ia_freq).
3886 *
3887 * For GPU frequencies less than 750MHz,
3888 * just use the lowest ring freq.
3889 */
3890 if (gpu_freq < min_freq)
3891 ia_freq = 800;
3892 else
3893 ia_freq = max_ia_freq - ((diff * scaling_factor) / 2);
3894 ia_freq = DIV_ROUND_CLOSEST(ia_freq, 100);
3895 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003896
Ben Widawsky42c05262012-09-26 10:34:00 -07003897 sandybridge_pcode_write(dev_priv,
3898 GEN6_PCODE_WRITE_MIN_FREQ_TABLE,
Chris Wilson3ebecd02013-04-12 19:10:13 +01003899 ia_freq << GEN6_PCODE_FREQ_IA_RATIO_SHIFT |
3900 ring_freq << GEN6_PCODE_FREQ_RING_RATIO_SHIFT |
3901 gpu_freq);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003902 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003903}
3904
Imre Deakc2bc2fc2014-04-18 16:16:23 +03003905void gen6_update_ring_freq(struct drm_device *dev)
3906{
3907 struct drm_i915_private *dev_priv = dev->dev_private;
3908
3909 if (INTEL_INFO(dev)->gen < 6 || IS_VALLEYVIEW(dev))
3910 return;
3911
3912 mutex_lock(&dev_priv->rps.hw_lock);
3913 __gen6_update_ring_freq(dev);
3914 mutex_unlock(&dev_priv->rps.hw_lock);
3915}
3916
Ville Syrjälä03af2042014-06-28 02:03:53 +03003917static int cherryview_rps_max_freq(struct drm_i915_private *dev_priv)
Deepak S2b6b3a02014-05-27 15:59:30 +05303918{
3919 u32 val, rp0;
3920
3921 val = vlv_punit_read(dev_priv, PUNIT_GPU_STATUS_REG);
3922 rp0 = (val >> PUNIT_GPU_STATUS_MAX_FREQ_SHIFT) & PUNIT_GPU_STATUS_MAX_FREQ_MASK;
3923
3924 return rp0;
3925}
3926
3927static int cherryview_rps_rpe_freq(struct drm_i915_private *dev_priv)
3928{
3929 u32 val, rpe;
3930
3931 val = vlv_punit_read(dev_priv, PUNIT_GPU_DUTYCYCLE_REG);
3932 rpe = (val >> PUNIT_GPU_DUTYCYCLE_RPE_FREQ_SHIFT) & PUNIT_GPU_DUTYCYCLE_RPE_FREQ_MASK;
3933
3934 return rpe;
3935}
3936
Deepak S7707df42014-07-12 18:46:14 +05303937static int cherryview_rps_guar_freq(struct drm_i915_private *dev_priv)
3938{
3939 u32 val, rp1;
3940
3941 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
3942 rp1 = (val >> PUNIT_GPU_STATUS_MAX_FREQ_SHIFT) & PUNIT_GPU_STATUS_MAX_FREQ_MASK;
3943
3944 return rp1;
3945}
3946
Ville Syrjälä03af2042014-06-28 02:03:53 +03003947static int cherryview_rps_min_freq(struct drm_i915_private *dev_priv)
Deepak S2b6b3a02014-05-27 15:59:30 +05303948{
3949 u32 val, rpn;
3950
3951 val = vlv_punit_read(dev_priv, PUNIT_GPU_STATUS_REG);
3952 rpn = (val >> PUNIT_GPU_STATIS_GFX_MIN_FREQ_SHIFT) & PUNIT_GPU_STATUS_GFX_MIN_FREQ_MASK;
3953 return rpn;
3954}
3955
Deepak Sf8f2b002014-07-10 13:16:21 +05303956static int valleyview_rps_guar_freq(struct drm_i915_private *dev_priv)
3957{
3958 u32 val, rp1;
3959
3960 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FREQ_FUSE);
3961
3962 rp1 = (val & FB_GFX_FGUARANTEED_FREQ_FUSE_MASK) >> FB_GFX_FGUARANTEED_FREQ_FUSE_SHIFT;
3963
3964 return rp1;
3965}
3966
Ville Syrjälä03af2042014-06-28 02:03:53 +03003967static int valleyview_rps_max_freq(struct drm_i915_private *dev_priv)
Jesse Barnes0a073b82013-04-17 15:54:58 -07003968{
3969 u32 val, rp0;
3970
Jani Nikula64936252013-05-22 15:36:20 +03003971 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FREQ_FUSE);
Jesse Barnes0a073b82013-04-17 15:54:58 -07003972
3973 rp0 = (val & FB_GFX_MAX_FREQ_FUSE_MASK) >> FB_GFX_MAX_FREQ_FUSE_SHIFT;
3974 /* Clamp to max */
3975 rp0 = min_t(u32, rp0, 0xea);
3976
3977 return rp0;
3978}
3979
3980static int valleyview_rps_rpe_freq(struct drm_i915_private *dev_priv)
3981{
3982 u32 val, rpe;
3983
Jani Nikula64936252013-05-22 15:36:20 +03003984 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FMAX_FUSE_LO);
Jesse Barnes0a073b82013-04-17 15:54:58 -07003985 rpe = (val & FB_FMAX_VMIN_FREQ_LO_MASK) >> FB_FMAX_VMIN_FREQ_LO_SHIFT;
Jani Nikula64936252013-05-22 15:36:20 +03003986 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FMAX_FUSE_HI);
Jesse Barnes0a073b82013-04-17 15:54:58 -07003987 rpe |= (val & FB_FMAX_VMIN_FREQ_HI_MASK) << 5;
3988
3989 return rpe;
3990}
3991
Ville Syrjälä03af2042014-06-28 02:03:53 +03003992static int valleyview_rps_min_freq(struct drm_i915_private *dev_priv)
Jesse Barnes0a073b82013-04-17 15:54:58 -07003993{
Jani Nikula64936252013-05-22 15:36:20 +03003994 return vlv_punit_read(dev_priv, PUNIT_REG_GPU_LFM) & 0xff;
Jesse Barnes0a073b82013-04-17 15:54:58 -07003995}
3996
Imre Deakae484342014-03-31 15:10:44 +03003997/* Check that the pctx buffer wasn't move under us. */
3998static void valleyview_check_pctx(struct drm_i915_private *dev_priv)
3999{
4000 unsigned long pctx_addr = I915_READ(VLV_PCBR) & ~4095;
4001
4002 WARN_ON(pctx_addr != dev_priv->mm.stolen_base +
4003 dev_priv->vlv_pctx->stolen->start);
4004}
4005
Deepak S38807742014-05-23 21:00:15 +05304006
4007/* Check that the pcbr address is not empty. */
4008static void cherryview_check_pctx(struct drm_i915_private *dev_priv)
4009{
4010 unsigned long pctx_addr = I915_READ(VLV_PCBR) & ~4095;
4011
4012 WARN_ON((pctx_addr >> VLV_PCBR_ADDR_SHIFT) == 0);
4013}
4014
4015static void cherryview_setup_pctx(struct drm_device *dev)
4016{
4017 struct drm_i915_private *dev_priv = dev->dev_private;
4018 unsigned long pctx_paddr, paddr;
4019 struct i915_gtt *gtt = &dev_priv->gtt;
4020 u32 pcbr;
4021 int pctx_size = 32*1024;
4022
4023 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
4024
4025 pcbr = I915_READ(VLV_PCBR);
4026 if ((pcbr >> VLV_PCBR_ADDR_SHIFT) == 0) {
4027 paddr = (dev_priv->mm.stolen_base +
4028 (gtt->stolen_size - pctx_size));
4029
4030 pctx_paddr = (paddr & (~4095));
4031 I915_WRITE(VLV_PCBR, pctx_paddr);
4032 }
4033}
4034
Jesse Barnesc9cddff2013-05-08 10:45:13 -07004035static void valleyview_setup_pctx(struct drm_device *dev)
4036{
4037 struct drm_i915_private *dev_priv = dev->dev_private;
4038 struct drm_i915_gem_object *pctx;
4039 unsigned long pctx_paddr;
4040 u32 pcbr;
4041 int pctx_size = 24*1024;
4042
Imre Deak17b0c1f2014-02-11 21:39:06 +02004043 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
4044
Jesse Barnesc9cddff2013-05-08 10:45:13 -07004045 pcbr = I915_READ(VLV_PCBR);
4046 if (pcbr) {
4047 /* BIOS set it up already, grab the pre-alloc'd space */
4048 int pcbr_offset;
4049
4050 pcbr_offset = (pcbr & (~4095)) - dev_priv->mm.stolen_base;
4051 pctx = i915_gem_object_create_stolen_for_preallocated(dev_priv->dev,
4052 pcbr_offset,
Daniel Vetter190d6cd2013-07-04 13:06:28 +02004053 I915_GTT_OFFSET_NONE,
Jesse Barnesc9cddff2013-05-08 10:45:13 -07004054 pctx_size);
4055 goto out;
4056 }
4057
4058 /*
4059 * From the Gunit register HAS:
4060 * The Gfx driver is expected to program this register and ensure
4061 * proper allocation within Gfx stolen memory. For example, this
4062 * register should be programmed such than the PCBR range does not
4063 * overlap with other ranges, such as the frame buffer, protected
4064 * memory, or any other relevant ranges.
4065 */
4066 pctx = i915_gem_object_create_stolen(dev, pctx_size);
4067 if (!pctx) {
4068 DRM_DEBUG("not enough stolen space for PCTX, disabling\n");
4069 return;
4070 }
4071
4072 pctx_paddr = dev_priv->mm.stolen_base + pctx->stolen->start;
4073 I915_WRITE(VLV_PCBR, pctx_paddr);
4074
4075out:
4076 dev_priv->vlv_pctx = pctx;
4077}
4078
Imre Deakae484342014-03-31 15:10:44 +03004079static void valleyview_cleanup_pctx(struct drm_device *dev)
4080{
4081 struct drm_i915_private *dev_priv = dev->dev_private;
4082
4083 if (WARN_ON(!dev_priv->vlv_pctx))
4084 return;
4085
4086 drm_gem_object_unreference(&dev_priv->vlv_pctx->base);
4087 dev_priv->vlv_pctx = NULL;
4088}
4089
Imre Deak4e805192014-04-14 20:24:41 +03004090static void valleyview_init_gt_powersave(struct drm_device *dev)
4091{
4092 struct drm_i915_private *dev_priv = dev->dev_private;
4093
4094 valleyview_setup_pctx(dev);
4095
4096 mutex_lock(&dev_priv->rps.hw_lock);
4097
4098 dev_priv->rps.max_freq = valleyview_rps_max_freq(dev_priv);
4099 dev_priv->rps.rp0_freq = dev_priv->rps.max_freq;
4100 DRM_DEBUG_DRIVER("max GPU freq: %d MHz (%u)\n",
4101 vlv_gpu_freq(dev_priv, dev_priv->rps.max_freq),
4102 dev_priv->rps.max_freq);
4103
4104 dev_priv->rps.efficient_freq = valleyview_rps_rpe_freq(dev_priv);
4105 DRM_DEBUG_DRIVER("RPe GPU freq: %d MHz (%u)\n",
4106 vlv_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
4107 dev_priv->rps.efficient_freq);
4108
Deepak Sf8f2b002014-07-10 13:16:21 +05304109 dev_priv->rps.rp1_freq = valleyview_rps_guar_freq(dev_priv);
4110 DRM_DEBUG_DRIVER("RP1(Guar Freq) GPU freq: %d MHz (%u)\n",
4111 vlv_gpu_freq(dev_priv, dev_priv->rps.rp1_freq),
4112 dev_priv->rps.rp1_freq);
4113
Imre Deak4e805192014-04-14 20:24:41 +03004114 dev_priv->rps.min_freq = valleyview_rps_min_freq(dev_priv);
4115 DRM_DEBUG_DRIVER("min GPU freq: %d MHz (%u)\n",
4116 vlv_gpu_freq(dev_priv, dev_priv->rps.min_freq),
4117 dev_priv->rps.min_freq);
4118
4119 /* Preserve min/max settings in case of re-init */
4120 if (dev_priv->rps.max_freq_softlimit == 0)
4121 dev_priv->rps.max_freq_softlimit = dev_priv->rps.max_freq;
4122
4123 if (dev_priv->rps.min_freq_softlimit == 0)
4124 dev_priv->rps.min_freq_softlimit = dev_priv->rps.min_freq;
4125
4126 mutex_unlock(&dev_priv->rps.hw_lock);
4127}
4128
Deepak S38807742014-05-23 21:00:15 +05304129static void cherryview_init_gt_powersave(struct drm_device *dev)
4130{
Deepak S2b6b3a02014-05-27 15:59:30 +05304131 struct drm_i915_private *dev_priv = dev->dev_private;
4132
Deepak S38807742014-05-23 21:00:15 +05304133 cherryview_setup_pctx(dev);
Deepak S2b6b3a02014-05-27 15:59:30 +05304134
4135 mutex_lock(&dev_priv->rps.hw_lock);
4136
4137 dev_priv->rps.max_freq = cherryview_rps_max_freq(dev_priv);
4138 dev_priv->rps.rp0_freq = dev_priv->rps.max_freq;
4139 DRM_DEBUG_DRIVER("max GPU freq: %d MHz (%u)\n",
4140 vlv_gpu_freq(dev_priv, dev_priv->rps.max_freq),
4141 dev_priv->rps.max_freq);
4142
4143 dev_priv->rps.efficient_freq = cherryview_rps_rpe_freq(dev_priv);
4144 DRM_DEBUG_DRIVER("RPe GPU freq: %d MHz (%u)\n",
4145 vlv_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
4146 dev_priv->rps.efficient_freq);
4147
Deepak S7707df42014-07-12 18:46:14 +05304148 dev_priv->rps.rp1_freq = cherryview_rps_guar_freq(dev_priv);
4149 DRM_DEBUG_DRIVER("RP1(Guar) GPU freq: %d MHz (%u)\n",
4150 vlv_gpu_freq(dev_priv, dev_priv->rps.rp1_freq),
4151 dev_priv->rps.rp1_freq);
4152
Deepak S2b6b3a02014-05-27 15:59:30 +05304153 dev_priv->rps.min_freq = cherryview_rps_min_freq(dev_priv);
4154 DRM_DEBUG_DRIVER("min GPU freq: %d MHz (%u)\n",
4155 vlv_gpu_freq(dev_priv, dev_priv->rps.min_freq),
4156 dev_priv->rps.min_freq);
4157
4158 /* Preserve min/max settings in case of re-init */
4159 if (dev_priv->rps.max_freq_softlimit == 0)
4160 dev_priv->rps.max_freq_softlimit = dev_priv->rps.max_freq;
4161
4162 if (dev_priv->rps.min_freq_softlimit == 0)
4163 dev_priv->rps.min_freq_softlimit = dev_priv->rps.min_freq;
4164
4165 mutex_unlock(&dev_priv->rps.hw_lock);
Deepak S38807742014-05-23 21:00:15 +05304166}
4167
Imre Deak4e805192014-04-14 20:24:41 +03004168static void valleyview_cleanup_gt_powersave(struct drm_device *dev)
4169{
4170 valleyview_cleanup_pctx(dev);
4171}
4172
Deepak S38807742014-05-23 21:00:15 +05304173static void cherryview_enable_rps(struct drm_device *dev)
4174{
4175 struct drm_i915_private *dev_priv = dev->dev_private;
4176 struct intel_engine_cs *ring;
Deepak S2b6b3a02014-05-27 15:59:30 +05304177 u32 gtfifodbg, val, rc6_mode = 0, pcbr;
Deepak S38807742014-05-23 21:00:15 +05304178 int i;
4179
4180 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
4181
4182 gtfifodbg = I915_READ(GTFIFODBG);
4183 if (gtfifodbg) {
4184 DRM_DEBUG_DRIVER("GT fifo had a previous error %x\n",
4185 gtfifodbg);
4186 I915_WRITE(GTFIFODBG, gtfifodbg);
4187 }
4188
4189 cherryview_check_pctx(dev_priv);
4190
4191 /* 1a & 1b: Get forcewake during program sequence. Although the driver
4192 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
4193 gen6_gt_force_wake_get(dev_priv, FORCEWAKE_ALL);
4194
4195 /* 2a: Program RC6 thresholds.*/
4196 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16);
4197 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
4198 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
4199
4200 for_each_ring(ring, dev_priv, i)
4201 I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
4202 I915_WRITE(GEN6_RC_SLEEP, 0);
4203
4204 I915_WRITE(GEN6_RC6_THRESHOLD, 50000); /* 50/125ms per EI */
4205
4206 /* allows RC6 residency counter to work */
4207 I915_WRITE(VLV_COUNTER_CONTROL,
4208 _MASKED_BIT_ENABLE(VLV_COUNT_RANGE_HIGH |
4209 VLV_MEDIA_RC6_COUNT_EN |
4210 VLV_RENDER_RC6_COUNT_EN));
4211
4212 /* For now we assume BIOS is allocating and populating the PCBR */
4213 pcbr = I915_READ(VLV_PCBR);
4214
4215 DRM_DEBUG_DRIVER("PCBR offset : 0x%x\n", pcbr);
4216
4217 /* 3: Enable RC6 */
4218 if ((intel_enable_rc6(dev) & INTEL_RC6_ENABLE) &&
4219 (pcbr >> VLV_PCBR_ADDR_SHIFT))
4220 rc6_mode = GEN6_RC_CTL_EI_MODE(1);
4221
4222 I915_WRITE(GEN6_RC_CONTROL, rc6_mode);
4223
Deepak S2b6b3a02014-05-27 15:59:30 +05304224 /* 4 Program defaults and thresholds for RPS*/
4225 I915_WRITE(GEN6_RP_UP_THRESHOLD, 59400);
4226 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 245000);
4227 I915_WRITE(GEN6_RP_UP_EI, 66000);
4228 I915_WRITE(GEN6_RP_DOWN_EI, 350000);
4229
4230 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
4231
Tom O'Rourke7405f422014-06-10 16:26:34 -07004232 /* WaDisablePwrmtrEvent:chv (pre-production hw) */
4233 I915_WRITE(0xA80C, I915_READ(0xA80C) & 0x00ffffff);
4234 I915_WRITE(0xA810, I915_READ(0xA810) & 0xffffff00);
4235
Deepak S2b6b3a02014-05-27 15:59:30 +05304236 /* 5: Enable RPS */
4237 I915_WRITE(GEN6_RP_CONTROL,
4238 GEN6_RP_MEDIA_HW_NORMAL_MODE |
Tom O'Rourke7405f422014-06-10 16:26:34 -07004239 GEN6_RP_MEDIA_IS_GFX | /* WaSetMaskForGfxBusyness:chv (pre-production hw ?) */
Deepak S2b6b3a02014-05-27 15:59:30 +05304240 GEN6_RP_ENABLE |
4241 GEN6_RP_UP_BUSY_AVG |
4242 GEN6_RP_DOWN_IDLE_AVG);
4243
4244 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
4245
4246 DRM_DEBUG_DRIVER("GPLL enabled? %s\n", val & 0x10 ? "yes" : "no");
4247 DRM_DEBUG_DRIVER("GPU status: 0x%08x\n", val);
4248
4249 dev_priv->rps.cur_freq = (val >> 8) & 0xff;
4250 DRM_DEBUG_DRIVER("current GPU freq: %d MHz (%u)\n",
4251 vlv_gpu_freq(dev_priv, dev_priv->rps.cur_freq),
4252 dev_priv->rps.cur_freq);
4253
4254 DRM_DEBUG_DRIVER("setting GPU freq to %d MHz (%u)\n",
4255 vlv_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
4256 dev_priv->rps.efficient_freq);
4257
4258 valleyview_set_rps(dev_priv->dev, dev_priv->rps.efficient_freq);
4259
Deepak S3497a562014-07-10 13:16:26 +05304260 gen8_enable_rps_interrupts(dev);
4261
Deepak S38807742014-05-23 21:00:15 +05304262 gen6_gt_force_wake_put(dev_priv, FORCEWAKE_ALL);
4263}
4264
Jesse Barnes0a073b82013-04-17 15:54:58 -07004265static void valleyview_enable_rps(struct drm_device *dev)
4266{
4267 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01004268 struct intel_engine_cs *ring;
Ben Widawsky2a5913a2014-03-19 18:31:13 -07004269 u32 gtfifodbg, val, rc6_mode = 0;
Jesse Barnes0a073b82013-04-17 15:54:58 -07004270 int i;
4271
4272 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
4273
Imre Deakae484342014-03-31 15:10:44 +03004274 valleyview_check_pctx(dev_priv);
4275
Jesse Barnes0a073b82013-04-17 15:54:58 -07004276 if ((gtfifodbg = I915_READ(GTFIFODBG))) {
Jesse Barnesf7d85c12013-09-27 10:40:54 -07004277 DRM_DEBUG_DRIVER("GT fifo had a previous error %x\n",
4278 gtfifodbg);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004279 I915_WRITE(GTFIFODBG, gtfifodbg);
4280 }
4281
Deepak Sc8d9a592013-11-23 14:55:42 +05304282 /* If VLV, Forcewake all wells, else re-direct to regular path */
4283 gen6_gt_force_wake_get(dev_priv, FORCEWAKE_ALL);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004284
4285 I915_WRITE(GEN6_RP_UP_THRESHOLD, 59400);
4286 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 245000);
4287 I915_WRITE(GEN6_RP_UP_EI, 66000);
4288 I915_WRITE(GEN6_RP_DOWN_EI, 350000);
4289
4290 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
Deepak S31685c22014-07-03 17:33:01 -04004291 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 0xf4240);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004292
4293 I915_WRITE(GEN6_RP_CONTROL,
4294 GEN6_RP_MEDIA_TURBO |
4295 GEN6_RP_MEDIA_HW_NORMAL_MODE |
4296 GEN6_RP_MEDIA_IS_GFX |
4297 GEN6_RP_ENABLE |
4298 GEN6_RP_UP_BUSY_AVG |
4299 GEN6_RP_DOWN_IDLE_CONT);
4300
4301 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 0x00280000);
4302 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
4303 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
4304
4305 for_each_ring(ring, dev_priv, i)
4306 I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
4307
Jesse Barnes2f0aa3042013-11-15 09:32:11 -08004308 I915_WRITE(GEN6_RC6_THRESHOLD, 0x557);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004309
4310 /* allows RC6 residency counter to work */
Jesse Barnes49798eb2013-09-26 17:55:57 -07004311 I915_WRITE(VLV_COUNTER_CONTROL,
Deepak S31685c22014-07-03 17:33:01 -04004312 _MASKED_BIT_ENABLE(VLV_MEDIA_RC0_COUNT_EN |
4313 VLV_RENDER_RC0_COUNT_EN |
Jesse Barnes49798eb2013-09-26 17:55:57 -07004314 VLV_MEDIA_RC6_COUNT_EN |
4315 VLV_RENDER_RC6_COUNT_EN));
Deepak S31685c22014-07-03 17:33:01 -04004316
Jesse Barnesa2b23fe2013-09-19 09:33:13 -07004317 if (intel_enable_rc6(dev) & INTEL_RC6_ENABLE)
Jesse Barnes6b88f292013-11-15 09:32:12 -08004318 rc6_mode = GEN7_RC_CTL_TO_MODE | VLV_RC_CTL_CTX_RST_PARALLEL;
Ben Widawskydc39fff2013-10-18 12:32:07 -07004319
4320 intel_print_rc6_info(dev, rc6_mode);
4321
Jesse Barnesa2b23fe2013-09-19 09:33:13 -07004322 I915_WRITE(GEN6_RC_CONTROL, rc6_mode);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004323
Jani Nikula64936252013-05-22 15:36:20 +03004324 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004325
4326 DRM_DEBUG_DRIVER("GPLL enabled? %s\n", val & 0x10 ? "yes" : "no");
4327 DRM_DEBUG_DRIVER("GPU status: 0x%08x\n", val);
4328
Ben Widawskyb39fb292014-03-19 18:31:11 -07004329 dev_priv->rps.cur_freq = (val >> 8) & 0xff;
Ville Syrjälä73008b92013-06-25 19:21:01 +03004330 DRM_DEBUG_DRIVER("current GPU freq: %d MHz (%u)\n",
Ben Widawskyb39fb292014-03-19 18:31:11 -07004331 vlv_gpu_freq(dev_priv, dev_priv->rps.cur_freq),
4332 dev_priv->rps.cur_freq);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004333
Ville Syrjälä73008b92013-06-25 19:21:01 +03004334 DRM_DEBUG_DRIVER("setting GPU freq to %d MHz (%u)\n",
Ben Widawskyb39fb292014-03-19 18:31:11 -07004335 vlv_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
4336 dev_priv->rps.efficient_freq);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004337
Ben Widawskyb39fb292014-03-19 18:31:11 -07004338 valleyview_set_rps(dev_priv->dev, dev_priv->rps.efficient_freq);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004339
Daniel Vetter44fc7d52013-07-12 22:43:27 +02004340 gen6_enable_rps_interrupts(dev);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004341
Deepak Sc8d9a592013-11-23 14:55:42 +05304342 gen6_gt_force_wake_put(dev_priv, FORCEWAKE_ALL);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004343}
4344
Daniel Vetter930ebb42012-06-29 23:32:16 +02004345void ironlake_teardown_rc6(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004346{
4347 struct drm_i915_private *dev_priv = dev->dev_private;
4348
Daniel Vetter3e373942012-11-02 19:55:04 +01004349 if (dev_priv->ips.renderctx) {
Ben Widawskyd7f46fc2013-12-06 14:10:55 -08004350 i915_gem_object_ggtt_unpin(dev_priv->ips.renderctx);
Daniel Vetter3e373942012-11-02 19:55:04 +01004351 drm_gem_object_unreference(&dev_priv->ips.renderctx->base);
4352 dev_priv->ips.renderctx = NULL;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004353 }
4354
Daniel Vetter3e373942012-11-02 19:55:04 +01004355 if (dev_priv->ips.pwrctx) {
Ben Widawskyd7f46fc2013-12-06 14:10:55 -08004356 i915_gem_object_ggtt_unpin(dev_priv->ips.pwrctx);
Daniel Vetter3e373942012-11-02 19:55:04 +01004357 drm_gem_object_unreference(&dev_priv->ips.pwrctx->base);
4358 dev_priv->ips.pwrctx = NULL;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004359 }
4360}
4361
Daniel Vetter930ebb42012-06-29 23:32:16 +02004362static void ironlake_disable_rc6(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004363{
4364 struct drm_i915_private *dev_priv = dev->dev_private;
4365
4366 if (I915_READ(PWRCTXA)) {
4367 /* Wake the GPU, prevent RC6, then restore RSTDBYCTL */
4368 I915_WRITE(RSTDBYCTL, I915_READ(RSTDBYCTL) | RCX_SW_EXIT);
4369 wait_for(((I915_READ(RSTDBYCTL) & RSX_STATUS_MASK) == RSX_STATUS_ON),
4370 50);
4371
4372 I915_WRITE(PWRCTXA, 0);
4373 POSTING_READ(PWRCTXA);
4374
4375 I915_WRITE(RSTDBYCTL, I915_READ(RSTDBYCTL) & ~RCX_SW_EXIT);
4376 POSTING_READ(RSTDBYCTL);
4377 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004378}
4379
4380static int ironlake_setup_rc6(struct drm_device *dev)
4381{
4382 struct drm_i915_private *dev_priv = dev->dev_private;
4383
Daniel Vetter3e373942012-11-02 19:55:04 +01004384 if (dev_priv->ips.renderctx == NULL)
4385 dev_priv->ips.renderctx = intel_alloc_context_page(dev);
4386 if (!dev_priv->ips.renderctx)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004387 return -ENOMEM;
4388
Daniel Vetter3e373942012-11-02 19:55:04 +01004389 if (dev_priv->ips.pwrctx == NULL)
4390 dev_priv->ips.pwrctx = intel_alloc_context_page(dev);
4391 if (!dev_priv->ips.pwrctx) {
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004392 ironlake_teardown_rc6(dev);
4393 return -ENOMEM;
4394 }
4395
4396 return 0;
4397}
4398
Daniel Vetter930ebb42012-06-29 23:32:16 +02004399static void ironlake_enable_rc6(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004400{
4401 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01004402 struct intel_engine_cs *ring = &dev_priv->ring[RCS];
Chris Wilson3e960502012-11-27 16:22:54 +00004403 bool was_interruptible;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004404 int ret;
4405
4406 /* rc6 disabled by default due to repeated reports of hanging during
4407 * boot and resume.
4408 */
4409 if (!intel_enable_rc6(dev))
4410 return;
4411
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02004412 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
4413
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004414 ret = ironlake_setup_rc6(dev);
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02004415 if (ret)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004416 return;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004417
Chris Wilson3e960502012-11-27 16:22:54 +00004418 was_interruptible = dev_priv->mm.interruptible;
4419 dev_priv->mm.interruptible = false;
4420
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004421 /*
4422 * GPU can automatically power down the render unit if given a page
4423 * to save state.
4424 */
Daniel Vetter6d90c952012-04-26 23:28:05 +02004425 ret = intel_ring_begin(ring, 6);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004426 if (ret) {
4427 ironlake_teardown_rc6(dev);
Chris Wilson3e960502012-11-27 16:22:54 +00004428 dev_priv->mm.interruptible = was_interruptible;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004429 return;
4430 }
4431
Daniel Vetter6d90c952012-04-26 23:28:05 +02004432 intel_ring_emit(ring, MI_SUSPEND_FLUSH | MI_SUSPEND_FLUSH_EN);
4433 intel_ring_emit(ring, MI_SET_CONTEXT);
Ben Widawskyf343c5f2013-07-05 14:41:04 -07004434 intel_ring_emit(ring, i915_gem_obj_ggtt_offset(dev_priv->ips.renderctx) |
Daniel Vetter6d90c952012-04-26 23:28:05 +02004435 MI_MM_SPACE_GTT |
4436 MI_SAVE_EXT_STATE_EN |
4437 MI_RESTORE_EXT_STATE_EN |
4438 MI_RESTORE_INHIBIT);
4439 intel_ring_emit(ring, MI_SUSPEND_FLUSH);
4440 intel_ring_emit(ring, MI_NOOP);
4441 intel_ring_emit(ring, MI_FLUSH);
4442 intel_ring_advance(ring);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004443
4444 /*
4445 * Wait for the command parser to advance past MI_SET_CONTEXT. The HW
4446 * does an implicit flush, combined with MI_FLUSH above, it should be
4447 * safe to assume that renderctx is valid
4448 */
Chris Wilson3e960502012-11-27 16:22:54 +00004449 ret = intel_ring_idle(ring);
4450 dev_priv->mm.interruptible = was_interruptible;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004451 if (ret) {
Jani Nikuladef27a52013-03-12 10:49:19 +02004452 DRM_ERROR("failed to enable ironlake power savings\n");
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004453 ironlake_teardown_rc6(dev);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004454 return;
4455 }
4456
Ben Widawskyf343c5f2013-07-05 14:41:04 -07004457 I915_WRITE(PWRCTXA, i915_gem_obj_ggtt_offset(dev_priv->ips.pwrctx) | PWRCTX_EN);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004458 I915_WRITE(RSTDBYCTL, I915_READ(RSTDBYCTL) & ~RCX_SW_EXIT);
Ben Widawskydc39fff2013-10-18 12:32:07 -07004459
Imre Deak91ca6892014-04-14 20:24:25 +03004460 intel_print_rc6_info(dev, GEN6_RC_CTL_RC6_ENABLE);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004461}
4462
Eugeni Dodonovdde18882012-04-18 15:29:24 -03004463static unsigned long intel_pxfreq(u32 vidfreq)
4464{
4465 unsigned long freq;
4466 int div = (vidfreq & 0x3f0000) >> 16;
4467 int post = (vidfreq & 0x3000) >> 12;
4468 int pre = (vidfreq & 0x7);
4469
4470 if (!pre)
4471 return 0;
4472
4473 freq = ((div * 133333) / ((1<<post) * pre));
4474
4475 return freq;
4476}
4477
Daniel Vettereb48eb02012-04-26 23:28:12 +02004478static const struct cparams {
4479 u16 i;
4480 u16 t;
4481 u16 m;
4482 u16 c;
4483} cparams[] = {
4484 { 1, 1333, 301, 28664 },
4485 { 1, 1066, 294, 24460 },
4486 { 1, 800, 294, 25192 },
4487 { 0, 1333, 276, 27605 },
4488 { 0, 1066, 276, 27605 },
4489 { 0, 800, 231, 23784 },
4490};
4491
Chris Wilsonf531dcb22012-09-25 10:16:12 +01004492static unsigned long __i915_chipset_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02004493{
4494 u64 total_count, diff, ret;
4495 u32 count1, count2, count3, m = 0, c = 0;
4496 unsigned long now = jiffies_to_msecs(jiffies), diff1;
4497 int i;
4498
Daniel Vetter02d71952012-08-09 16:44:54 +02004499 assert_spin_locked(&mchdev_lock);
4500
Daniel Vetter20e4d402012-08-08 23:35:39 +02004501 diff1 = now - dev_priv->ips.last_time1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02004502
4503 /* Prevent division-by-zero if we are asking too fast.
4504 * Also, we don't get interesting results if we are polling
4505 * faster than once in 10ms, so just return the saved value
4506 * in such cases.
4507 */
4508 if (diff1 <= 10)
Daniel Vetter20e4d402012-08-08 23:35:39 +02004509 return dev_priv->ips.chipset_power;
Daniel Vettereb48eb02012-04-26 23:28:12 +02004510
4511 count1 = I915_READ(DMIEC);
4512 count2 = I915_READ(DDREC);
4513 count3 = I915_READ(CSIEC);
4514
4515 total_count = count1 + count2 + count3;
4516
4517 /* FIXME: handle per-counter overflow */
Daniel Vetter20e4d402012-08-08 23:35:39 +02004518 if (total_count < dev_priv->ips.last_count1) {
4519 diff = ~0UL - dev_priv->ips.last_count1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02004520 diff += total_count;
4521 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +02004522 diff = total_count - dev_priv->ips.last_count1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02004523 }
4524
4525 for (i = 0; i < ARRAY_SIZE(cparams); i++) {
Daniel Vetter20e4d402012-08-08 23:35:39 +02004526 if (cparams[i].i == dev_priv->ips.c_m &&
4527 cparams[i].t == dev_priv->ips.r_t) {
Daniel Vettereb48eb02012-04-26 23:28:12 +02004528 m = cparams[i].m;
4529 c = cparams[i].c;
4530 break;
4531 }
4532 }
4533
4534 diff = div_u64(diff, diff1);
4535 ret = ((m * diff) + c);
4536 ret = div_u64(ret, 10);
4537
Daniel Vetter20e4d402012-08-08 23:35:39 +02004538 dev_priv->ips.last_count1 = total_count;
4539 dev_priv->ips.last_time1 = now;
Daniel Vettereb48eb02012-04-26 23:28:12 +02004540
Daniel Vetter20e4d402012-08-08 23:35:39 +02004541 dev_priv->ips.chipset_power = ret;
Daniel Vettereb48eb02012-04-26 23:28:12 +02004542
4543 return ret;
4544}
4545
Chris Wilsonf531dcb22012-09-25 10:16:12 +01004546unsigned long i915_chipset_val(struct drm_i915_private *dev_priv)
4547{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00004548 struct drm_device *dev = dev_priv->dev;
Chris Wilsonf531dcb22012-09-25 10:16:12 +01004549 unsigned long val;
4550
Damien Lespiau3d13ef22014-02-07 19:12:47 +00004551 if (INTEL_INFO(dev)->gen != 5)
Chris Wilsonf531dcb22012-09-25 10:16:12 +01004552 return 0;
4553
4554 spin_lock_irq(&mchdev_lock);
4555
4556 val = __i915_chipset_val(dev_priv);
4557
4558 spin_unlock_irq(&mchdev_lock);
4559
4560 return val;
4561}
4562
Daniel Vettereb48eb02012-04-26 23:28:12 +02004563unsigned long i915_mch_val(struct drm_i915_private *dev_priv)
4564{
4565 unsigned long m, x, b;
4566 u32 tsfs;
4567
4568 tsfs = I915_READ(TSFS);
4569
4570 m = ((tsfs & TSFS_SLOPE_MASK) >> TSFS_SLOPE_SHIFT);
4571 x = I915_READ8(TR1);
4572
4573 b = tsfs & TSFS_INTR_MASK;
4574
4575 return ((m * x) / 127) - b;
4576}
4577
4578static u16 pvid_to_extvid(struct drm_i915_private *dev_priv, u8 pxvid)
4579{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00004580 struct drm_device *dev = dev_priv->dev;
Daniel Vettereb48eb02012-04-26 23:28:12 +02004581 static const struct v_table {
4582 u16 vd; /* in .1 mil */
4583 u16 vm; /* in .1 mil */
4584 } v_table[] = {
4585 { 0, 0, },
4586 { 375, 0, },
4587 { 500, 0, },
4588 { 625, 0, },
4589 { 750, 0, },
4590 { 875, 0, },
4591 { 1000, 0, },
4592 { 1125, 0, },
4593 { 4125, 3000, },
4594 { 4125, 3000, },
4595 { 4125, 3000, },
4596 { 4125, 3000, },
4597 { 4125, 3000, },
4598 { 4125, 3000, },
4599 { 4125, 3000, },
4600 { 4125, 3000, },
4601 { 4125, 3000, },
4602 { 4125, 3000, },
4603 { 4125, 3000, },
4604 { 4125, 3000, },
4605 { 4125, 3000, },
4606 { 4125, 3000, },
4607 { 4125, 3000, },
4608 { 4125, 3000, },
4609 { 4125, 3000, },
4610 { 4125, 3000, },
4611 { 4125, 3000, },
4612 { 4125, 3000, },
4613 { 4125, 3000, },
4614 { 4125, 3000, },
4615 { 4125, 3000, },
4616 { 4125, 3000, },
4617 { 4250, 3125, },
4618 { 4375, 3250, },
4619 { 4500, 3375, },
4620 { 4625, 3500, },
4621 { 4750, 3625, },
4622 { 4875, 3750, },
4623 { 5000, 3875, },
4624 { 5125, 4000, },
4625 { 5250, 4125, },
4626 { 5375, 4250, },
4627 { 5500, 4375, },
4628 { 5625, 4500, },
4629 { 5750, 4625, },
4630 { 5875, 4750, },
4631 { 6000, 4875, },
4632 { 6125, 5000, },
4633 { 6250, 5125, },
4634 { 6375, 5250, },
4635 { 6500, 5375, },
4636 { 6625, 5500, },
4637 { 6750, 5625, },
4638 { 6875, 5750, },
4639 { 7000, 5875, },
4640 { 7125, 6000, },
4641 { 7250, 6125, },
4642 { 7375, 6250, },
4643 { 7500, 6375, },
4644 { 7625, 6500, },
4645 { 7750, 6625, },
4646 { 7875, 6750, },
4647 { 8000, 6875, },
4648 { 8125, 7000, },
4649 { 8250, 7125, },
4650 { 8375, 7250, },
4651 { 8500, 7375, },
4652 { 8625, 7500, },
4653 { 8750, 7625, },
4654 { 8875, 7750, },
4655 { 9000, 7875, },
4656 { 9125, 8000, },
4657 { 9250, 8125, },
4658 { 9375, 8250, },
4659 { 9500, 8375, },
4660 { 9625, 8500, },
4661 { 9750, 8625, },
4662 { 9875, 8750, },
4663 { 10000, 8875, },
4664 { 10125, 9000, },
4665 { 10250, 9125, },
4666 { 10375, 9250, },
4667 { 10500, 9375, },
4668 { 10625, 9500, },
4669 { 10750, 9625, },
4670 { 10875, 9750, },
4671 { 11000, 9875, },
4672 { 11125, 10000, },
4673 { 11250, 10125, },
4674 { 11375, 10250, },
4675 { 11500, 10375, },
4676 { 11625, 10500, },
4677 { 11750, 10625, },
4678 { 11875, 10750, },
4679 { 12000, 10875, },
4680 { 12125, 11000, },
4681 { 12250, 11125, },
4682 { 12375, 11250, },
4683 { 12500, 11375, },
4684 { 12625, 11500, },
4685 { 12750, 11625, },
4686 { 12875, 11750, },
4687 { 13000, 11875, },
4688 { 13125, 12000, },
4689 { 13250, 12125, },
4690 { 13375, 12250, },
4691 { 13500, 12375, },
4692 { 13625, 12500, },
4693 { 13750, 12625, },
4694 { 13875, 12750, },
4695 { 14000, 12875, },
4696 { 14125, 13000, },
4697 { 14250, 13125, },
4698 { 14375, 13250, },
4699 { 14500, 13375, },
4700 { 14625, 13500, },
4701 { 14750, 13625, },
4702 { 14875, 13750, },
4703 { 15000, 13875, },
4704 { 15125, 14000, },
4705 { 15250, 14125, },
4706 { 15375, 14250, },
4707 { 15500, 14375, },
4708 { 15625, 14500, },
4709 { 15750, 14625, },
4710 { 15875, 14750, },
4711 { 16000, 14875, },
4712 { 16125, 15000, },
4713 };
Damien Lespiau3d13ef22014-02-07 19:12:47 +00004714 if (INTEL_INFO(dev)->is_mobile)
Daniel Vettereb48eb02012-04-26 23:28:12 +02004715 return v_table[pxvid].vm;
4716 else
4717 return v_table[pxvid].vd;
4718}
4719
Daniel Vetter02d71952012-08-09 16:44:54 +02004720static void __i915_update_gfx_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02004721{
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00004722 u64 now, diff, diffms;
Daniel Vettereb48eb02012-04-26 23:28:12 +02004723 u32 count;
4724
Daniel Vetter02d71952012-08-09 16:44:54 +02004725 assert_spin_locked(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004726
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00004727 now = ktime_get_raw_ns();
4728 diffms = now - dev_priv->ips.last_time2;
4729 do_div(diffms, NSEC_PER_MSEC);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004730
4731 /* Don't divide by 0 */
Daniel Vettereb48eb02012-04-26 23:28:12 +02004732 if (!diffms)
4733 return;
4734
4735 count = I915_READ(GFXEC);
4736
Daniel Vetter20e4d402012-08-08 23:35:39 +02004737 if (count < dev_priv->ips.last_count2) {
4738 diff = ~0UL - dev_priv->ips.last_count2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02004739 diff += count;
4740 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +02004741 diff = count - dev_priv->ips.last_count2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02004742 }
4743
Daniel Vetter20e4d402012-08-08 23:35:39 +02004744 dev_priv->ips.last_count2 = count;
4745 dev_priv->ips.last_time2 = now;
Daniel Vettereb48eb02012-04-26 23:28:12 +02004746
4747 /* More magic constants... */
4748 diff = diff * 1181;
4749 diff = div_u64(diff, diffms * 10);
Daniel Vetter20e4d402012-08-08 23:35:39 +02004750 dev_priv->ips.gfx_power = diff;
Daniel Vettereb48eb02012-04-26 23:28:12 +02004751}
4752
Daniel Vetter02d71952012-08-09 16:44:54 +02004753void i915_update_gfx_val(struct drm_i915_private *dev_priv)
4754{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00004755 struct drm_device *dev = dev_priv->dev;
4756
4757 if (INTEL_INFO(dev)->gen != 5)
Daniel Vetter02d71952012-08-09 16:44:54 +02004758 return;
4759
Daniel Vetter92703882012-08-09 16:46:01 +02004760 spin_lock_irq(&mchdev_lock);
Daniel Vetter02d71952012-08-09 16:44:54 +02004761
4762 __i915_update_gfx_val(dev_priv);
4763
Daniel Vetter92703882012-08-09 16:46:01 +02004764 spin_unlock_irq(&mchdev_lock);
Daniel Vetter02d71952012-08-09 16:44:54 +02004765}
4766
Chris Wilsonf531dcb22012-09-25 10:16:12 +01004767static unsigned long __i915_gfx_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02004768{
4769 unsigned long t, corr, state1, corr2, state2;
4770 u32 pxvid, ext_v;
4771
Daniel Vetter02d71952012-08-09 16:44:54 +02004772 assert_spin_locked(&mchdev_lock);
4773
Ben Widawskyb39fb292014-03-19 18:31:11 -07004774 pxvid = I915_READ(PXVFREQ_BASE + (dev_priv->rps.cur_freq * 4));
Daniel Vettereb48eb02012-04-26 23:28:12 +02004775 pxvid = (pxvid >> 24) & 0x7f;
4776 ext_v = pvid_to_extvid(dev_priv, pxvid);
4777
4778 state1 = ext_v;
4779
4780 t = i915_mch_val(dev_priv);
4781
4782 /* Revel in the empirically derived constants */
4783
4784 /* Correction factor in 1/100000 units */
4785 if (t > 80)
4786 corr = ((t * 2349) + 135940);
4787 else if (t >= 50)
4788 corr = ((t * 964) + 29317);
4789 else /* < 50 */
4790 corr = ((t * 301) + 1004);
4791
4792 corr = corr * ((150142 * state1) / 10000 - 78642);
4793 corr /= 100000;
Daniel Vetter20e4d402012-08-08 23:35:39 +02004794 corr2 = (corr * dev_priv->ips.corr);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004795
4796 state2 = (corr2 * state1) / 10000;
4797 state2 /= 100; /* convert to mW */
4798
Daniel Vetter02d71952012-08-09 16:44:54 +02004799 __i915_update_gfx_val(dev_priv);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004800
Daniel Vetter20e4d402012-08-08 23:35:39 +02004801 return dev_priv->ips.gfx_power + state2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02004802}
4803
Chris Wilsonf531dcb22012-09-25 10:16:12 +01004804unsigned long i915_gfx_val(struct drm_i915_private *dev_priv)
4805{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00004806 struct drm_device *dev = dev_priv->dev;
Chris Wilsonf531dcb22012-09-25 10:16:12 +01004807 unsigned long val;
4808
Damien Lespiau3d13ef22014-02-07 19:12:47 +00004809 if (INTEL_INFO(dev)->gen != 5)
Chris Wilsonf531dcb22012-09-25 10:16:12 +01004810 return 0;
4811
4812 spin_lock_irq(&mchdev_lock);
4813
4814 val = __i915_gfx_val(dev_priv);
4815
4816 spin_unlock_irq(&mchdev_lock);
4817
4818 return val;
4819}
4820
Daniel Vettereb48eb02012-04-26 23:28:12 +02004821/**
4822 * i915_read_mch_val - return value for IPS use
4823 *
4824 * Calculate and return a value for the IPS driver to use when deciding whether
4825 * we have thermal and power headroom to increase CPU or GPU power budget.
4826 */
4827unsigned long i915_read_mch_val(void)
4828{
4829 struct drm_i915_private *dev_priv;
4830 unsigned long chipset_val, graphics_val, ret = 0;
4831
Daniel Vetter92703882012-08-09 16:46:01 +02004832 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004833 if (!i915_mch_dev)
4834 goto out_unlock;
4835 dev_priv = i915_mch_dev;
4836
Chris Wilsonf531dcb22012-09-25 10:16:12 +01004837 chipset_val = __i915_chipset_val(dev_priv);
4838 graphics_val = __i915_gfx_val(dev_priv);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004839
4840 ret = chipset_val + graphics_val;
4841
4842out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02004843 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004844
4845 return ret;
4846}
4847EXPORT_SYMBOL_GPL(i915_read_mch_val);
4848
4849/**
4850 * i915_gpu_raise - raise GPU frequency limit
4851 *
4852 * Raise the limit; IPS indicates we have thermal headroom.
4853 */
4854bool i915_gpu_raise(void)
4855{
4856 struct drm_i915_private *dev_priv;
4857 bool ret = true;
4858
Daniel Vetter92703882012-08-09 16:46:01 +02004859 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004860 if (!i915_mch_dev) {
4861 ret = false;
4862 goto out_unlock;
4863 }
4864 dev_priv = i915_mch_dev;
4865
Daniel Vetter20e4d402012-08-08 23:35:39 +02004866 if (dev_priv->ips.max_delay > dev_priv->ips.fmax)
4867 dev_priv->ips.max_delay--;
Daniel Vettereb48eb02012-04-26 23:28:12 +02004868
4869out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02004870 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004871
4872 return ret;
4873}
4874EXPORT_SYMBOL_GPL(i915_gpu_raise);
4875
4876/**
4877 * i915_gpu_lower - lower GPU frequency limit
4878 *
4879 * IPS indicates we're close to a thermal limit, so throttle back the GPU
4880 * frequency maximum.
4881 */
4882bool i915_gpu_lower(void)
4883{
4884 struct drm_i915_private *dev_priv;
4885 bool ret = true;
4886
Daniel Vetter92703882012-08-09 16:46:01 +02004887 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004888 if (!i915_mch_dev) {
4889 ret = false;
4890 goto out_unlock;
4891 }
4892 dev_priv = i915_mch_dev;
4893
Daniel Vetter20e4d402012-08-08 23:35:39 +02004894 if (dev_priv->ips.max_delay < dev_priv->ips.min_delay)
4895 dev_priv->ips.max_delay++;
Daniel Vettereb48eb02012-04-26 23:28:12 +02004896
4897out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02004898 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004899
4900 return ret;
4901}
4902EXPORT_SYMBOL_GPL(i915_gpu_lower);
4903
4904/**
4905 * i915_gpu_busy - indicate GPU business to IPS
4906 *
4907 * Tell the IPS driver whether or not the GPU is busy.
4908 */
4909bool i915_gpu_busy(void)
4910{
4911 struct drm_i915_private *dev_priv;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01004912 struct intel_engine_cs *ring;
Daniel Vettereb48eb02012-04-26 23:28:12 +02004913 bool ret = false;
Chris Wilsonf047e392012-07-21 12:31:41 +01004914 int i;
Daniel Vettereb48eb02012-04-26 23:28:12 +02004915
Daniel Vetter92703882012-08-09 16:46:01 +02004916 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004917 if (!i915_mch_dev)
4918 goto out_unlock;
4919 dev_priv = i915_mch_dev;
4920
Chris Wilsonf047e392012-07-21 12:31:41 +01004921 for_each_ring(ring, dev_priv, i)
4922 ret |= !list_empty(&ring->request_list);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004923
4924out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02004925 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004926
4927 return ret;
4928}
4929EXPORT_SYMBOL_GPL(i915_gpu_busy);
4930
4931/**
4932 * i915_gpu_turbo_disable - disable graphics turbo
4933 *
4934 * Disable graphics turbo by resetting the max frequency and setting the
4935 * current frequency to the default.
4936 */
4937bool i915_gpu_turbo_disable(void)
4938{
4939 struct drm_i915_private *dev_priv;
4940 bool ret = true;
4941
Daniel Vetter92703882012-08-09 16:46:01 +02004942 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004943 if (!i915_mch_dev) {
4944 ret = false;
4945 goto out_unlock;
4946 }
4947 dev_priv = i915_mch_dev;
4948
Daniel Vetter20e4d402012-08-08 23:35:39 +02004949 dev_priv->ips.max_delay = dev_priv->ips.fstart;
Daniel Vettereb48eb02012-04-26 23:28:12 +02004950
Daniel Vetter20e4d402012-08-08 23:35:39 +02004951 if (!ironlake_set_drps(dev_priv->dev, dev_priv->ips.fstart))
Daniel Vettereb48eb02012-04-26 23:28:12 +02004952 ret = false;
4953
4954out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02004955 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004956
4957 return ret;
4958}
4959EXPORT_SYMBOL_GPL(i915_gpu_turbo_disable);
4960
4961/**
4962 * Tells the intel_ips driver that the i915 driver is now loaded, if
4963 * IPS got loaded first.
4964 *
4965 * This awkward dance is so that neither module has to depend on the
4966 * other in order for IPS to do the appropriate communication of
4967 * GPU turbo limits to i915.
4968 */
4969static void
4970ips_ping_for_i915_load(void)
4971{
4972 void (*link)(void);
4973
4974 link = symbol_get(ips_link_to_i915_driver);
4975 if (link) {
4976 link();
4977 symbol_put(ips_link_to_i915_driver);
4978 }
4979}
4980
4981void intel_gpu_ips_init(struct drm_i915_private *dev_priv)
4982{
Daniel Vetter02d71952012-08-09 16:44:54 +02004983 /* We only register the i915 ips part with intel-ips once everything is
4984 * set up, to avoid intel-ips sneaking in and reading bogus values. */
Daniel Vetter92703882012-08-09 16:46:01 +02004985 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004986 i915_mch_dev = dev_priv;
Daniel Vetter92703882012-08-09 16:46:01 +02004987 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004988
4989 ips_ping_for_i915_load();
4990}
4991
4992void intel_gpu_ips_teardown(void)
4993{
Daniel Vetter92703882012-08-09 16:46:01 +02004994 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004995 i915_mch_dev = NULL;
Daniel Vetter92703882012-08-09 16:46:01 +02004996 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004997}
Deepak S76c3552f2014-01-30 23:08:16 +05304998
Daniel Vetter8090c6b2012-06-24 16:42:32 +02004999static void intel_init_emon(struct drm_device *dev)
Eugeni Dodonovdde18882012-04-18 15:29:24 -03005000{
5001 struct drm_i915_private *dev_priv = dev->dev_private;
5002 u32 lcfuse;
5003 u8 pxw[16];
5004 int i;
5005
5006 /* Disable to program */
5007 I915_WRITE(ECR, 0);
5008 POSTING_READ(ECR);
5009
5010 /* Program energy weights for various events */
5011 I915_WRITE(SDEW, 0x15040d00);
5012 I915_WRITE(CSIEW0, 0x007f0000);
5013 I915_WRITE(CSIEW1, 0x1e220004);
5014 I915_WRITE(CSIEW2, 0x04000004);
5015
5016 for (i = 0; i < 5; i++)
5017 I915_WRITE(PEW + (i * 4), 0);
5018 for (i = 0; i < 3; i++)
5019 I915_WRITE(DEW + (i * 4), 0);
5020
5021 /* Program P-state weights to account for frequency power adjustment */
5022 for (i = 0; i < 16; i++) {
5023 u32 pxvidfreq = I915_READ(PXVFREQ_BASE + (i * 4));
5024 unsigned long freq = intel_pxfreq(pxvidfreq);
5025 unsigned long vid = (pxvidfreq & PXVFREQ_PX_MASK) >>
5026 PXVFREQ_PX_SHIFT;
5027 unsigned long val;
5028
5029 val = vid * vid;
5030 val *= (freq / 1000);
5031 val *= 255;
5032 val /= (127*127*900);
5033 if (val > 0xff)
5034 DRM_ERROR("bad pxval: %ld\n", val);
5035 pxw[i] = val;
5036 }
5037 /* Render standby states get 0 weight */
5038 pxw[14] = 0;
5039 pxw[15] = 0;
5040
5041 for (i = 0; i < 4; i++) {
5042 u32 val = (pxw[i*4] << 24) | (pxw[(i*4)+1] << 16) |
5043 (pxw[(i*4)+2] << 8) | (pxw[(i*4)+3]);
5044 I915_WRITE(PXW + (i * 4), val);
5045 }
5046
5047 /* Adjust magic regs to magic values (more experimental results) */
5048 I915_WRITE(OGW0, 0);
5049 I915_WRITE(OGW1, 0);
5050 I915_WRITE(EG0, 0x00007f00);
5051 I915_WRITE(EG1, 0x0000000e);
5052 I915_WRITE(EG2, 0x000e0000);
5053 I915_WRITE(EG3, 0x68000300);
5054 I915_WRITE(EG4, 0x42000000);
5055 I915_WRITE(EG5, 0x00140031);
5056 I915_WRITE(EG6, 0);
5057 I915_WRITE(EG7, 0);
5058
5059 for (i = 0; i < 8; i++)
5060 I915_WRITE(PXWL + (i * 4), 0);
5061
5062 /* Enable PMON + select events */
5063 I915_WRITE(ECR, 0x80000019);
5064
5065 lcfuse = I915_READ(LCFUSE02);
5066
Daniel Vetter20e4d402012-08-08 23:35:39 +02005067 dev_priv->ips.corr = (lcfuse & LCFUSE_HIV_MASK);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03005068}
5069
Imre Deakae484342014-03-31 15:10:44 +03005070void intel_init_gt_powersave(struct drm_device *dev)
5071{
Imre Deake6069ca2014-04-18 16:01:02 +03005072 i915.enable_rc6 = sanitize_rc6_option(dev, i915.enable_rc6);
5073
Deepak S38807742014-05-23 21:00:15 +05305074 if (IS_CHERRYVIEW(dev))
5075 cherryview_init_gt_powersave(dev);
5076 else if (IS_VALLEYVIEW(dev))
Imre Deak4e805192014-04-14 20:24:41 +03005077 valleyview_init_gt_powersave(dev);
Imre Deakae484342014-03-31 15:10:44 +03005078}
5079
5080void intel_cleanup_gt_powersave(struct drm_device *dev)
5081{
Deepak S38807742014-05-23 21:00:15 +05305082 if (IS_CHERRYVIEW(dev))
5083 return;
5084 else if (IS_VALLEYVIEW(dev))
Imre Deak4e805192014-04-14 20:24:41 +03005085 valleyview_cleanup_gt_powersave(dev);
Imre Deakae484342014-03-31 15:10:44 +03005086}
5087
Jesse Barnes156c7ca2014-06-12 08:35:45 -07005088/**
5089 * intel_suspend_gt_powersave - suspend PM work and helper threads
5090 * @dev: drm device
5091 *
5092 * We don't want to disable RC6 or other features here, we just want
5093 * to make sure any work we've queued has finished and won't bother
5094 * us while we're suspended.
5095 */
5096void intel_suspend_gt_powersave(struct drm_device *dev)
5097{
5098 struct drm_i915_private *dev_priv = dev->dev_private;
5099
5100 /* Interrupts should be disabled already to avoid re-arming. */
Jesse Barnes9df7575f2014-06-20 09:29:20 -07005101 WARN_ON(intel_irqs_enabled(dev_priv));
Jesse Barnes156c7ca2014-06-12 08:35:45 -07005102
5103 flush_delayed_work(&dev_priv->rps.delayed_resume_work);
5104
5105 cancel_work_sync(&dev_priv->rps.work);
Deepak Sb47adc12014-06-20 20:03:02 +05305106
5107 /* Force GPU to min freq during suspend */
5108 gen6_rps_idle(dev_priv);
Jesse Barnes156c7ca2014-06-12 08:35:45 -07005109}
5110
Daniel Vetter8090c6b2012-06-24 16:42:32 +02005111void intel_disable_gt_powersave(struct drm_device *dev)
5112{
Jesse Barnes1a01ab32012-11-02 11:14:00 -07005113 struct drm_i915_private *dev_priv = dev->dev_private;
5114
Daniel Vetterfd0c0642013-04-24 11:13:35 +02005115 /* Interrupts should be disabled already to avoid re-arming. */
Jesse Barnes9df7575f2014-06-20 09:29:20 -07005116 WARN_ON(intel_irqs_enabled(dev_priv));
Daniel Vetterfd0c0642013-04-24 11:13:35 +02005117
Daniel Vetter930ebb42012-06-29 23:32:16 +02005118 if (IS_IRONLAKE_M(dev)) {
Daniel Vetter8090c6b2012-06-24 16:42:32 +02005119 ironlake_disable_drps(dev);
Daniel Vetter930ebb42012-06-29 23:32:16 +02005120 ironlake_disable_rc6(dev);
Deepak S38807742014-05-23 21:00:15 +05305121 } else if (INTEL_INFO(dev)->gen >= 6) {
Daniel Vetter10d8d362014-06-12 17:48:52 +02005122 intel_suspend_gt_powersave(dev);
Imre Deake4948372014-05-12 18:35:04 +03005123
Jesse Barnes4fc688c2012-11-02 11:14:01 -07005124 mutex_lock(&dev_priv->rps.hw_lock);
Deepak S38807742014-05-23 21:00:15 +05305125 if (IS_CHERRYVIEW(dev))
5126 cherryview_disable_rps(dev);
5127 else if (IS_VALLEYVIEW(dev))
Jesse Barnesd20d4f02013-04-23 10:09:28 -07005128 valleyview_disable_rps(dev);
5129 else
5130 gen6_disable_rps(dev);
Chris Wilsonc0951f02013-10-10 21:58:50 +01005131 dev_priv->rps.enabled = false;
Jesse Barnes4fc688c2012-11-02 11:14:01 -07005132 mutex_unlock(&dev_priv->rps.hw_lock);
Daniel Vetter930ebb42012-06-29 23:32:16 +02005133 }
Daniel Vetter8090c6b2012-06-24 16:42:32 +02005134}
5135
Jesse Barnes1a01ab32012-11-02 11:14:00 -07005136static void intel_gen6_powersave_work(struct work_struct *work)
5137{
5138 struct drm_i915_private *dev_priv =
5139 container_of(work, struct drm_i915_private,
5140 rps.delayed_resume_work.work);
5141 struct drm_device *dev = dev_priv->dev;
5142
Jesse Barnes4fc688c2012-11-02 11:14:01 -07005143 mutex_lock(&dev_priv->rps.hw_lock);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005144
Deepak S38807742014-05-23 21:00:15 +05305145 if (IS_CHERRYVIEW(dev)) {
5146 cherryview_enable_rps(dev);
5147 } else if (IS_VALLEYVIEW(dev)) {
Jesse Barnes0a073b82013-04-17 15:54:58 -07005148 valleyview_enable_rps(dev);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005149 } else if (IS_BROADWELL(dev)) {
5150 gen8_enable_rps(dev);
Imre Deakc2bc2fc2014-04-18 16:16:23 +03005151 __gen6_update_ring_freq(dev);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005152 } else {
5153 gen6_enable_rps(dev);
Imre Deakc2bc2fc2014-04-18 16:16:23 +03005154 __gen6_update_ring_freq(dev);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005155 }
Chris Wilsonc0951f02013-10-10 21:58:50 +01005156 dev_priv->rps.enabled = true;
Jesse Barnes4fc688c2012-11-02 11:14:01 -07005157 mutex_unlock(&dev_priv->rps.hw_lock);
Imre Deakc6df39b2014-04-14 20:24:29 +03005158
5159 intel_runtime_pm_put(dev_priv);
Jesse Barnes1a01ab32012-11-02 11:14:00 -07005160}
5161
Daniel Vetter8090c6b2012-06-24 16:42:32 +02005162void intel_enable_gt_powersave(struct drm_device *dev)
5163{
Jesse Barnes1a01ab32012-11-02 11:14:00 -07005164 struct drm_i915_private *dev_priv = dev->dev_private;
5165
Daniel Vetter8090c6b2012-06-24 16:42:32 +02005166 if (IS_IRONLAKE_M(dev)) {
Imre Deakdc1d0132014-04-14 20:24:28 +03005167 mutex_lock(&dev->struct_mutex);
Daniel Vetter8090c6b2012-06-24 16:42:32 +02005168 ironlake_enable_drps(dev);
5169 ironlake_enable_rc6(dev);
5170 intel_init_emon(dev);
Imre Deakdc1d0132014-04-14 20:24:28 +03005171 mutex_unlock(&dev->struct_mutex);
Deepak S38807742014-05-23 21:00:15 +05305172 } else if (INTEL_INFO(dev)->gen >= 6) {
Jesse Barnes1a01ab32012-11-02 11:14:00 -07005173 /*
5174 * PCU communication is slow and this doesn't need to be
5175 * done at any specific time, so do this out of our fast path
5176 * to make resume and init faster.
Imre Deakc6df39b2014-04-14 20:24:29 +03005177 *
5178 * We depend on the HW RC6 power context save/restore
5179 * mechanism when entering D3 through runtime PM suspend. So
5180 * disable RPM until RPS/RC6 is properly setup. We can only
5181 * get here via the driver load/system resume/runtime resume
5182 * paths, so the _noresume version is enough (and in case of
5183 * runtime resume it's necessary).
Jesse Barnes1a01ab32012-11-02 11:14:00 -07005184 */
Imre Deakc6df39b2014-04-14 20:24:29 +03005185 if (schedule_delayed_work(&dev_priv->rps.delayed_resume_work,
5186 round_jiffies_up_relative(HZ)))
5187 intel_runtime_pm_get_noresume(dev_priv);
Daniel Vetter8090c6b2012-06-24 16:42:32 +02005188 }
5189}
5190
Imre Deakc6df39b2014-04-14 20:24:29 +03005191void intel_reset_gt_powersave(struct drm_device *dev)
5192{
5193 struct drm_i915_private *dev_priv = dev->dev_private;
5194
5195 dev_priv->rps.enabled = false;
5196 intel_enable_gt_powersave(dev);
5197}
5198
Daniel Vetter3107bd42012-10-31 22:52:31 +01005199static void ibx_init_clock_gating(struct drm_device *dev)
5200{
5201 struct drm_i915_private *dev_priv = dev->dev_private;
5202
5203 /*
5204 * On Ibex Peak and Cougar Point, we need to disable clock
5205 * gating for the panel power sequencer or it will fail to
5206 * start up when no ports are active.
5207 */
5208 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE);
5209}
5210
Ville Syrjälä0e088b82013-06-07 10:47:04 +03005211static void g4x_disable_trickle_feed(struct drm_device *dev)
5212{
5213 struct drm_i915_private *dev_priv = dev->dev_private;
5214 int pipe;
5215
Damien Lespiau055e3932014-08-18 13:49:10 +01005216 for_each_pipe(dev_priv, pipe) {
Ville Syrjälä0e088b82013-06-07 10:47:04 +03005217 I915_WRITE(DSPCNTR(pipe),
5218 I915_READ(DSPCNTR(pipe)) |
5219 DISPPLANE_TRICKLE_FEED_DISABLE);
Ville Syrjälä1dba99f2013-10-01 18:02:18 +03005220 intel_flush_primary_plane(dev_priv, pipe);
Ville Syrjälä0e088b82013-06-07 10:47:04 +03005221 }
5222}
5223
Ville Syrjälä017636c2013-12-05 15:51:37 +02005224static void ilk_init_lp_watermarks(struct drm_device *dev)
5225{
5226 struct drm_i915_private *dev_priv = dev->dev_private;
5227
5228 I915_WRITE(WM3_LP_ILK, I915_READ(WM3_LP_ILK) & ~WM1_LP_SR_EN);
5229 I915_WRITE(WM2_LP_ILK, I915_READ(WM2_LP_ILK) & ~WM1_LP_SR_EN);
5230 I915_WRITE(WM1_LP_ILK, I915_READ(WM1_LP_ILK) & ~WM1_LP_SR_EN);
5231
5232 /*
5233 * Don't touch WM1S_LP_EN here.
5234 * Doing so could cause underruns.
5235 */
5236}
5237
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005238static void ironlake_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005239{
5240 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau231e54f2012-10-19 17:55:41 +01005241 uint32_t dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005242
Damien Lespiauf1e8fa52013-06-07 17:41:09 +01005243 /*
5244 * Required for FBC
5245 * WaFbcDisableDpfcClockGating:ilk
5246 */
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01005247 dspclk_gate |= ILK_DPFCRUNIT_CLOCK_GATE_DISABLE |
5248 ILK_DPFCUNIT_CLOCK_GATE_DISABLE |
5249 ILK_DPFDUNIT_CLOCK_GATE_ENABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005250
5251 I915_WRITE(PCH_3DCGDIS0,
5252 MARIUNIT_CLOCK_GATE_DISABLE |
5253 SVSMUNIT_CLOCK_GATE_DISABLE);
5254 I915_WRITE(PCH_3DCGDIS1,
5255 VFMUNIT_CLOCK_GATE_DISABLE);
5256
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005257 /*
5258 * According to the spec the following bits should be set in
5259 * order to enable memory self-refresh
5260 * The bit 22/21 of 0x42004
5261 * The bit 5 of 0x42020
5262 * The bit 15 of 0x45000
5263 */
5264 I915_WRITE(ILK_DISPLAY_CHICKEN2,
5265 (I915_READ(ILK_DISPLAY_CHICKEN2) |
5266 ILK_DPARB_GATE | ILK_VSDPFD_FULL));
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01005267 dspclk_gate |= ILK_DPARBUNIT_CLOCK_GATE_ENABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005268 I915_WRITE(DISP_ARB_CTL,
5269 (I915_READ(DISP_ARB_CTL) |
5270 DISP_FBC_WM_DIS));
Ville Syrjälä017636c2013-12-05 15:51:37 +02005271
5272 ilk_init_lp_watermarks(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005273
5274 /*
5275 * Based on the document from hardware guys the following bits
5276 * should be set unconditionally in order to enable FBC.
5277 * The bit 22 of 0x42000
5278 * The bit 22 of 0x42004
5279 * The bit 7,8,9 of 0x42020.
5280 */
5281 if (IS_IRONLAKE_M(dev)) {
Damien Lespiau4bb35332013-06-14 15:23:24 +01005282 /* WaFbcAsynchFlipDisableFbcQueue:ilk */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005283 I915_WRITE(ILK_DISPLAY_CHICKEN1,
5284 I915_READ(ILK_DISPLAY_CHICKEN1) |
5285 ILK_FBCQ_DIS);
5286 I915_WRITE(ILK_DISPLAY_CHICKEN2,
5287 I915_READ(ILK_DISPLAY_CHICKEN2) |
5288 ILK_DPARB_GATE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005289 }
5290
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01005291 I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
5292
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005293 I915_WRITE(ILK_DISPLAY_CHICKEN2,
5294 I915_READ(ILK_DISPLAY_CHICKEN2) |
5295 ILK_ELPIN_409_SELECT);
5296 I915_WRITE(_3D_CHICKEN2,
5297 _3D_CHICKEN2_WM_READ_PIPELINED << 16 |
5298 _3D_CHICKEN2_WM_READ_PIPELINED);
Daniel Vetter4358a372012-10-18 11:49:51 +02005299
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005300 /* WaDisableRenderCachePipelinedFlush:ilk */
Daniel Vetter4358a372012-10-18 11:49:51 +02005301 I915_WRITE(CACHE_MODE_0,
5302 _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
Daniel Vetter3107bd42012-10-31 22:52:31 +01005303
Akash Goel4e046322014-04-04 17:14:38 +05305304 /* WaDisable_RenderCache_OperationalFlush:ilk */
5305 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
5306
Ville Syrjälä0e088b82013-06-07 10:47:04 +03005307 g4x_disable_trickle_feed(dev);
Ville Syrjäläbdad2b22013-06-07 10:47:03 +03005308
Daniel Vetter3107bd42012-10-31 22:52:31 +01005309 ibx_init_clock_gating(dev);
5310}
5311
5312static void cpt_init_clock_gating(struct drm_device *dev)
5313{
5314 struct drm_i915_private *dev_priv = dev->dev_private;
5315 int pipe;
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03005316 uint32_t val;
Daniel Vetter3107bd42012-10-31 22:52:31 +01005317
5318 /*
5319 * On Ibex Peak and Cougar Point, we need to disable clock
5320 * gating for the panel power sequencer or it will fail to
5321 * start up when no ports are active.
5322 */
Jesse Barnescd664072013-10-02 10:34:19 -07005323 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE |
5324 PCH_DPLUNIT_CLOCK_GATE_DISABLE |
5325 PCH_CPUNIT_CLOCK_GATE_DISABLE);
Daniel Vetter3107bd42012-10-31 22:52:31 +01005326 I915_WRITE(SOUTH_CHICKEN2, I915_READ(SOUTH_CHICKEN2) |
5327 DPLS_EDP_PPS_FIX_DIS);
Takashi Iwai335c07b2012-12-11 11:46:29 +01005328 /* The below fixes the weird display corruption, a few pixels shifted
5329 * downward, on (only) LVDS of some HP laptops with IVY.
5330 */
Damien Lespiau055e3932014-08-18 13:49:10 +01005331 for_each_pipe(dev_priv, pipe) {
Paulo Zanonidc4bd2d2013-04-08 15:48:08 -03005332 val = I915_READ(TRANS_CHICKEN2(pipe));
5333 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
5334 val &= ~TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03005335 if (dev_priv->vbt.fdi_rx_polarity_inverted)
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03005336 val |= TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
Paulo Zanonidc4bd2d2013-04-08 15:48:08 -03005337 val &= ~TRANS_CHICKEN2_FRAME_START_DELAY_MASK;
5338 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_COUNTER;
5339 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_MODESWITCH;
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03005340 I915_WRITE(TRANS_CHICKEN2(pipe), val);
5341 }
Daniel Vetter3107bd42012-10-31 22:52:31 +01005342 /* WADP0ClockGatingDisable */
Damien Lespiau055e3932014-08-18 13:49:10 +01005343 for_each_pipe(dev_priv, pipe) {
Daniel Vetter3107bd42012-10-31 22:52:31 +01005344 I915_WRITE(TRANS_CHICKEN1(pipe),
5345 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
5346 }
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005347}
5348
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01005349static void gen6_check_mch_setup(struct drm_device *dev)
5350{
5351 struct drm_i915_private *dev_priv = dev->dev_private;
5352 uint32_t tmp;
5353
5354 tmp = I915_READ(MCH_SSKPD);
Daniel Vetterdf662a22014-08-04 11:17:25 +02005355 if ((tmp & MCH_SSKPD_WM0_MASK) != MCH_SSKPD_WM0_VAL)
5356 DRM_DEBUG_KMS("Wrong MCH_SSKPD value: 0x%08x This can cause underruns.\n",
5357 tmp);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01005358}
5359
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005360static void gen6_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005361{
5362 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau231e54f2012-10-19 17:55:41 +01005363 uint32_t dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005364
Damien Lespiau231e54f2012-10-19 17:55:41 +01005365 I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005366
5367 I915_WRITE(ILK_DISPLAY_CHICKEN2,
5368 I915_READ(ILK_DISPLAY_CHICKEN2) |
5369 ILK_ELPIN_409_SELECT);
5370
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005371 /* WaDisableHiZPlanesWhenMSAAEnabled:snb */
Daniel Vetter42839082012-12-14 23:38:28 +01005372 I915_WRITE(_3D_CHICKEN,
5373 _MASKED_BIT_ENABLE(_3D_CHICKEN_HIZ_PLANE_DISABLE_MSAA_4X_SNB));
5374
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005375 /* WaSetupGtModeTdRowDispatch:snb */
Daniel Vetter6547fbd2012-12-14 23:38:29 +01005376 if (IS_SNB_GT1(dev))
5377 I915_WRITE(GEN6_GT_MODE,
5378 _MASKED_BIT_ENABLE(GEN6_TD_FOUR_ROW_DISPATCH_DISABLE));
5379
Akash Goel4e046322014-04-04 17:14:38 +05305380 /* WaDisable_RenderCache_OperationalFlush:snb */
5381 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
5382
Ville Syrjälä8d85d272014-02-04 21:59:15 +02005383 /*
5384 * BSpec recoomends 8x4 when MSAA is used,
5385 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02005386 *
5387 * Note that PS/WM thread counts depend on the WIZ hashing
5388 * disable bit, which we don't touch here, but it's good
5389 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjälä8d85d272014-02-04 21:59:15 +02005390 */
5391 I915_WRITE(GEN6_GT_MODE,
5392 GEN6_WIZ_HASHING_MASK | GEN6_WIZ_HASHING_16x4);
5393
Ville Syrjälä017636c2013-12-05 15:51:37 +02005394 ilk_init_lp_watermarks(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005395
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005396 I915_WRITE(CACHE_MODE_0,
Daniel Vetter50743292012-04-26 22:02:54 +02005397 _MASKED_BIT_DISABLE(CM0_STC_EVICT_DISABLE_LRA_SNB));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005398
5399 I915_WRITE(GEN6_UCGCTL1,
5400 I915_READ(GEN6_UCGCTL1) |
5401 GEN6_BLBUNIT_CLOCK_GATE_DISABLE |
5402 GEN6_CSUNIT_CLOCK_GATE_DISABLE);
5403
5404 /* According to the BSpec vol1g, bit 12 (RCPBUNIT) clock
5405 * gating disable must be set. Failure to set it results in
5406 * flickering pixels due to Z write ordering failures after
5407 * some amount of runtime in the Mesa "fire" demo, and Unigine
5408 * Sanctuary and Tropics, and apparently anything else with
5409 * alpha test or pixel discard.
5410 *
5411 * According to the spec, bit 11 (RCCUNIT) must also be set,
5412 * but we didn't debug actual testcases to find it out.
Jesse Barnes0f846f82012-06-14 11:04:47 -07005413 *
Ville Syrjäläef593182014-01-22 21:32:47 +02005414 * WaDisableRCCUnitClockGating:snb
5415 * WaDisableRCPBUnitClockGating:snb
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005416 */
5417 I915_WRITE(GEN6_UCGCTL2,
5418 GEN6_RCPBUNIT_CLOCK_GATE_DISABLE |
5419 GEN6_RCCUNIT_CLOCK_GATE_DISABLE);
5420
Ville Syrjälä5eb146d2014-02-04 21:59:16 +02005421 /* WaStripsFansDisableFastClipPerformanceFix:snb */
Ville Syrjälä743b57d2014-02-04 21:59:17 +02005422 I915_WRITE(_3D_CHICKEN3,
5423 _MASKED_BIT_ENABLE(_3D_CHICKEN3_SF_DISABLE_FASTCLIP_CULL));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005424
5425 /*
Ville Syrjäläe927ecd2014-02-04 21:59:18 +02005426 * Bspec says:
5427 * "This bit must be set if 3DSTATE_CLIP clip mode is set to normal and
5428 * 3DSTATE_SF number of SF output attributes is more than 16."
5429 */
5430 I915_WRITE(_3D_CHICKEN3,
5431 _MASKED_BIT_ENABLE(_3D_CHICKEN3_SF_DISABLE_PIPELINED_ATTR_FETCH));
5432
5433 /*
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005434 * According to the spec the following bits should be
5435 * set in order to enable memory self-refresh and fbc:
5436 * The bit21 and bit22 of 0x42000
5437 * The bit21 and bit22 of 0x42004
5438 * The bit5 and bit7 of 0x42020
5439 * The bit14 of 0x70180
5440 * The bit14 of 0x71180
Damien Lespiau4bb35332013-06-14 15:23:24 +01005441 *
5442 * WaFbcAsynchFlipDisableFbcQueue:snb
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005443 */
5444 I915_WRITE(ILK_DISPLAY_CHICKEN1,
5445 I915_READ(ILK_DISPLAY_CHICKEN1) |
5446 ILK_FBCQ_DIS | ILK_PABSTRETCH_DIS);
5447 I915_WRITE(ILK_DISPLAY_CHICKEN2,
5448 I915_READ(ILK_DISPLAY_CHICKEN2) |
5449 ILK_DPARB_GATE | ILK_VSDPFD_FULL);
Damien Lespiau231e54f2012-10-19 17:55:41 +01005450 I915_WRITE(ILK_DSPCLK_GATE_D,
5451 I915_READ(ILK_DSPCLK_GATE_D) |
5452 ILK_DPARBUNIT_CLOCK_GATE_ENABLE |
5453 ILK_DPFDUNIT_CLOCK_GATE_ENABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005454
Ville Syrjälä0e088b82013-06-07 10:47:04 +03005455 g4x_disable_trickle_feed(dev);
Ben Widawskyf8f2ac92012-10-03 19:34:24 -07005456
Daniel Vetter3107bd42012-10-31 22:52:31 +01005457 cpt_init_clock_gating(dev);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01005458
5459 gen6_check_mch_setup(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005460}
5461
5462static void gen7_setup_fixed_func_scheduler(struct drm_i915_private *dev_priv)
5463{
5464 uint32_t reg = I915_READ(GEN7_FF_THREAD_MODE);
5465
Ville Syrjälä3aad9052014-01-22 21:32:59 +02005466 /*
Ville Syrjälä46680e02014-01-22 21:33:01 +02005467 * WaVSThreadDispatchOverride:ivb,vlv
Ville Syrjälä3aad9052014-01-22 21:32:59 +02005468 *
5469 * This actually overrides the dispatch
5470 * mode for all thread types.
5471 */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005472 reg &= ~GEN7_FF_SCHED_MASK;
5473 reg |= GEN7_FF_TS_SCHED_HW;
5474 reg |= GEN7_FF_VS_SCHED_HW;
5475 reg |= GEN7_FF_DS_SCHED_HW;
5476
5477 I915_WRITE(GEN7_FF_THREAD_MODE, reg);
5478}
5479
Paulo Zanoni17a303e2012-11-20 15:12:07 -02005480static void lpt_init_clock_gating(struct drm_device *dev)
5481{
5482 struct drm_i915_private *dev_priv = dev->dev_private;
5483
5484 /*
5485 * TODO: this bit should only be enabled when really needed, then
5486 * disabled when not needed anymore in order to save power.
5487 */
5488 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE)
5489 I915_WRITE(SOUTH_DSPCLK_GATE_D,
5490 I915_READ(SOUTH_DSPCLK_GATE_D) |
5491 PCH_LP_PARTITION_LEVEL_DISABLE);
Paulo Zanoni0a790cd2013-04-17 18:15:49 -03005492
5493 /* WADPOClockGatingDisable:hsw */
5494 I915_WRITE(_TRANSA_CHICKEN1,
5495 I915_READ(_TRANSA_CHICKEN1) |
5496 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
Paulo Zanoni17a303e2012-11-20 15:12:07 -02005497}
5498
Imre Deak7d708ee2013-04-17 14:04:50 +03005499static void lpt_suspend_hw(struct drm_device *dev)
5500{
5501 struct drm_i915_private *dev_priv = dev->dev_private;
5502
5503 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
5504 uint32_t val = I915_READ(SOUTH_DSPCLK_GATE_D);
5505
5506 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
5507 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
5508 }
5509}
5510
Ben Widawsky1020a5c2013-11-02 21:07:06 -07005511static void gen8_init_clock_gating(struct drm_device *dev)
5512{
5513 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau07d27e22014-03-03 17:31:46 +00005514 enum pipe pipe;
Ben Widawsky1020a5c2013-11-02 21:07:06 -07005515
5516 I915_WRITE(WM3_LP_ILK, 0);
5517 I915_WRITE(WM2_LP_ILK, 0);
5518 I915_WRITE(WM1_LP_ILK, 0);
Ben Widawsky50ed5fb2013-11-02 21:07:40 -07005519
5520 /* FIXME(BDW): Check all the w/a, some might only apply to
5521 * pre-production hw. */
5522
Kenneth Graunkec8966e12014-02-26 23:59:30 -08005523 /* WaDisablePartialInstShootdown:bdw */
5524 I915_WRITE(GEN8_ROW_CHICKEN,
5525 _MASKED_BIT_ENABLE(PARTIAL_INSTRUCTION_SHOOTDOWN_DISABLE));
5526
Kenneth Graunke1411e6a2014-02-26 23:59:31 -08005527 /* WaDisableThreadStallDopClockGating:bdw */
5528 /* FIXME: Unclear whether we really need this on production bdw. */
5529 I915_WRITE(GEN8_ROW_CHICKEN,
5530 _MASKED_BIT_ENABLE(STALL_DOP_GATING_DISABLE));
5531
Damien Lespiau4167e322014-01-16 16:51:35 +00005532 /*
5533 * This GEN8_CENTROID_PIXEL_OPT_DIS W/A is only needed for
5534 * pre-production hardware
5535 */
Ben Widawskyfd392b62013-11-04 22:52:39 -08005536 I915_WRITE(HALF_SLICE_CHICKEN3,
5537 _MASKED_BIT_ENABLE(GEN8_CENTROID_PIXEL_OPT_DIS));
Ben Widawskybf663472013-11-02 21:07:57 -07005538 I915_WRITE(HALF_SLICE_CHICKEN3,
5539 _MASKED_BIT_ENABLE(GEN8_SAMPLER_POWER_BYPASS_DIS));
Ben Widawsky4afe8d32013-11-02 21:07:55 -07005540 I915_WRITE(GAMTARBMODE, _MASKED_BIT_ENABLE(ARB_MODE_BWGTLB_DISABLE));
5541
Ben Widawsky7f88da02013-11-02 21:07:58 -07005542 I915_WRITE(_3D_CHICKEN3,
Michel Thierryb3f9ad92014-07-07 12:40:17 +01005543 _MASKED_BIT_ENABLE(_3D_CHICKEN_SDE_LIMIT_FIFO_POLY_DEPTH(2)));
Ben Widawsky7f88da02013-11-02 21:07:58 -07005544
Ben Widawskya75f3622013-11-02 21:07:59 -07005545 I915_WRITE(COMMON_SLICE_CHICKEN2,
5546 _MASKED_BIT_ENABLE(GEN8_CSC2_SBE_VUE_CACHE_CONSERVATIVE));
5547
Ben Widawsky4c2e7a52013-11-02 21:08:00 -07005548 I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
5549 _MASKED_BIT_ENABLE(GEN7_SINGLE_SUBSCAN_DISPATCH_ENABLE));
5550
Ben Widawsky242a4012014-04-18 18:04:29 -03005551 /* WaDisableDopClockGating:bdw May not be needed for production */
5552 I915_WRITE(GEN7_ROW_CHICKEN2,
5553 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
5554
Ben Widawskyab57fff2013-12-12 15:28:04 -08005555 /* WaSwitchSolVfFArbitrationPriority:bdw */
Ben Widawsky50ed5fb2013-11-02 21:07:40 -07005556 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07005557
Ben Widawskyab57fff2013-12-12 15:28:04 -08005558 /* WaPsrDPAMaskVBlankInSRD:bdw */
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07005559 I915_WRITE(CHICKEN_PAR1_1,
5560 I915_READ(CHICKEN_PAR1_1) | DPA_MASK_VBLANK_SRD);
5561
Ben Widawskyab57fff2013-12-12 15:28:04 -08005562 /* WaPsrDPRSUnmaskVBlankInSRD:bdw */
Damien Lespiau055e3932014-08-18 13:49:10 +01005563 for_each_pipe(dev_priv, pipe) {
Damien Lespiau07d27e22014-03-03 17:31:46 +00005564 I915_WRITE(CHICKEN_PIPESL_1(pipe),
Ville Syrjäläc7c65622014-03-05 13:05:45 +02005565 I915_READ(CHICKEN_PIPESL_1(pipe)) |
Ville Syrjälä8f670bb2014-03-05 13:05:47 +02005566 BDW_DPRS_MASK_VBLANK_SRD);
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07005567 }
Ben Widawsky63801f22013-12-12 17:26:03 -08005568
5569 /* Use Force Non-Coherent whenever executing a 3D context. This is a
5570 * workaround for for a possible hang in the unlikely event a TLB
5571 * invalidation occurs during a PSD flush.
5572 */
5573 I915_WRITE(HDC_CHICKEN0,
5574 I915_READ(HDC_CHICKEN0) |
5575 _MASKED_BIT_ENABLE(HDC_FORCE_NON_COHERENT));
Ben Widawskyab57fff2013-12-12 15:28:04 -08005576
5577 /* WaVSRefCountFullforceMissDisable:bdw */
5578 /* WaDSRefCountFullforceMissDisable:bdw */
5579 I915_WRITE(GEN7_FF_THREAD_MODE,
5580 I915_READ(GEN7_FF_THREAD_MODE) &
5581 ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
Ville Syrjälä36075a42014-02-04 21:59:21 +02005582
5583 /*
5584 * BSpec recommends 8x4 when MSAA is used,
5585 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02005586 *
5587 * Note that PS/WM thread counts depend on the WIZ hashing
5588 * disable bit, which we don't touch here, but it's good
5589 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjälä36075a42014-02-04 21:59:21 +02005590 */
5591 I915_WRITE(GEN7_GT_MODE,
5592 GEN6_WIZ_HASHING_MASK | GEN6_WIZ_HASHING_16x4);
Ville Syrjälä295e8bb2014-02-27 21:59:01 +02005593
5594 I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
5595 _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
Ville Syrjälä4f1ca9e2014-02-27 21:59:02 +02005596
5597 /* WaDisableSDEUnitClockGating:bdw */
5598 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
5599 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Damien Lespiau5d708682014-03-26 18:41:51 +00005600
5601 /* Wa4x4STCOptimizationDisable:bdw */
5602 I915_WRITE(CACHE_MODE_1,
5603 _MASKED_BIT_ENABLE(GEN8_4x4_STC_OPTIMIZATION_DISABLE));
Ben Widawsky1020a5c2013-11-02 21:07:06 -07005604}
5605
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03005606static void haswell_init_clock_gating(struct drm_device *dev)
5607{
5608 struct drm_i915_private *dev_priv = dev->dev_private;
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03005609
Ville Syrjälä017636c2013-12-05 15:51:37 +02005610 ilk_init_lp_watermarks(dev);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03005611
Francisco Jerezf3fc4882013-10-02 15:53:16 -07005612 /* L3 caching of data atomics doesn't work -- disable it. */
5613 I915_WRITE(HSW_SCRATCH1, HSW_SCRATCH1_L3_DATA_ATOMICS_DISABLE);
5614 I915_WRITE(HSW_ROW_CHICKEN3,
5615 _MASKED_BIT_ENABLE(HSW_ROW_CHICKEN3_L3_GLOBAL_ATOMICS_DISABLE));
5616
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005617 /* This is required by WaCatErrorRejectionIssue:hsw */
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03005618 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
5619 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
5620 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
5621
Ville Syrjäläe36ea7f2014-01-22 21:33:00 +02005622 /* WaVSRefCountFullforceMissDisable:hsw */
5623 I915_WRITE(GEN7_FF_THREAD_MODE,
5624 I915_READ(GEN7_FF_THREAD_MODE) & ~GEN7_FF_VS_REF_CNT_FFME);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03005625
Akash Goel4e046322014-04-04 17:14:38 +05305626 /* WaDisable_RenderCache_OperationalFlush:hsw */
5627 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
5628
Chia-I Wufe27c602014-01-28 13:29:33 +08005629 /* enable HiZ Raw Stall Optimization */
5630 I915_WRITE(CACHE_MODE_0_GEN7,
5631 _MASKED_BIT_DISABLE(HIZ_RAW_STALL_OPT_DISABLE));
5632
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005633 /* WaDisable4x2SubspanOptimization:hsw */
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03005634 I915_WRITE(CACHE_MODE_1,
5635 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Eugeni Dodonov1544d9d2012-07-02 11:51:10 -03005636
Ville Syrjäläa12c4962014-02-04 21:59:20 +02005637 /*
5638 * BSpec recommends 8x4 when MSAA is used,
5639 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02005640 *
5641 * Note that PS/WM thread counts depend on the WIZ hashing
5642 * disable bit, which we don't touch here, but it's good
5643 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjäläa12c4962014-02-04 21:59:20 +02005644 */
5645 I915_WRITE(GEN7_GT_MODE,
5646 GEN6_WIZ_HASHING_MASK | GEN6_WIZ_HASHING_16x4);
5647
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005648 /* WaSwitchSolVfFArbitrationPriority:hsw */
Ben Widawskye3dff582013-03-20 14:49:14 -07005649 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
5650
Paulo Zanoni90a88642013-05-03 17:23:45 -03005651 /* WaRsPkgCStateDisplayPMReq:hsw */
5652 I915_WRITE(CHICKEN_PAR1_1,
5653 I915_READ(CHICKEN_PAR1_1) | FORCE_ARB_IDLE_PLANES);
Eugeni Dodonov1544d9d2012-07-02 11:51:10 -03005654
Paulo Zanoni17a303e2012-11-20 15:12:07 -02005655 lpt_init_clock_gating(dev);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03005656}
5657
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005658static void ivybridge_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005659{
5660 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawsky20848222012-05-04 18:58:59 -07005661 uint32_t snpcr;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005662
Ville Syrjälä017636c2013-12-05 15:51:37 +02005663 ilk_init_lp_watermarks(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005664
Damien Lespiau231e54f2012-10-19 17:55:41 +01005665 I915_WRITE(ILK_DSPCLK_GATE_D, ILK_VRHUNIT_CLOCK_GATE_DISABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005666
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005667 /* WaDisableEarlyCull:ivb */
Jesse Barnes87f80202012-10-02 17:43:41 -05005668 I915_WRITE(_3D_CHICKEN3,
5669 _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
5670
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005671 /* WaDisableBackToBackFlipFix:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005672 I915_WRITE(IVB_CHICKEN3,
5673 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
5674 CHICKEN3_DGMG_DONE_FIX_DISABLE);
5675
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005676 /* WaDisablePSDDualDispatchEnable:ivb */
Jesse Barnes12f33822012-10-25 12:15:45 -07005677 if (IS_IVB_GT1(dev))
5678 I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
5679 _MASKED_BIT_ENABLE(GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
Jesse Barnes12f33822012-10-25 12:15:45 -07005680
Akash Goel4e046322014-04-04 17:14:38 +05305681 /* WaDisable_RenderCache_OperationalFlush:ivb */
5682 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
5683
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005684 /* Apply the WaDisableRHWOOptimizationForRenderHang:ivb workaround. */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005685 I915_WRITE(GEN7_COMMON_SLICE_CHICKEN1,
5686 GEN7_CSC1_RHWO_OPT_DISABLE_IN_RCC);
5687
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005688 /* WaApplyL3ControlAndL3ChickenMode:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005689 I915_WRITE(GEN7_L3CNTLREG1,
5690 GEN7_WA_FOR_GEN7_L3_CONTROL);
5691 I915_WRITE(GEN7_L3_CHICKEN_MODE_REGISTER,
Jesse Barnes8ab43972012-10-25 12:15:42 -07005692 GEN7_WA_L3_CHICKEN_MODE);
5693 if (IS_IVB_GT1(dev))
5694 I915_WRITE(GEN7_ROW_CHICKEN2,
5695 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Ville Syrjälä412236c2014-01-22 21:32:44 +02005696 else {
5697 /* must write both registers */
5698 I915_WRITE(GEN7_ROW_CHICKEN2,
5699 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Jesse Barnes8ab43972012-10-25 12:15:42 -07005700 I915_WRITE(GEN7_ROW_CHICKEN2_GT2,
5701 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Ville Syrjälä412236c2014-01-22 21:32:44 +02005702 }
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005703
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005704 /* WaForceL3Serialization:ivb */
Jesse Barnes61939d92012-10-02 17:43:38 -05005705 I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
5706 ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
5707
Ville Syrjälä1b80a19a2014-01-22 21:32:53 +02005708 /*
Jesse Barnes0f846f82012-06-14 11:04:47 -07005709 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005710 * This implements the WaDisableRCZUnitClockGating:ivb workaround.
Jesse Barnes0f846f82012-06-14 11:04:47 -07005711 */
5712 I915_WRITE(GEN6_UCGCTL2,
Ville Syrjälä28acf3b2014-01-22 21:32:48 +02005713 GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
Jesse Barnes0f846f82012-06-14 11:04:47 -07005714
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005715 /* This is required by WaCatErrorRejectionIssue:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005716 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
5717 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
5718 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
5719
Ville Syrjälä0e088b82013-06-07 10:47:04 +03005720 g4x_disable_trickle_feed(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005721
5722 gen7_setup_fixed_func_scheduler(dev_priv);
Daniel Vetter97e19302012-04-24 16:00:21 +02005723
Chris Wilson22721342014-03-04 09:41:43 +00005724 if (0) { /* causes HiZ corruption on ivb:gt1 */
5725 /* enable HiZ Raw Stall Optimization */
5726 I915_WRITE(CACHE_MODE_0_GEN7,
5727 _MASKED_BIT_DISABLE(HIZ_RAW_STALL_OPT_DISABLE));
5728 }
Chia-I Wu116f2b62014-01-28 13:29:34 +08005729
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005730 /* WaDisable4x2SubspanOptimization:ivb */
Daniel Vetter97e19302012-04-24 16:00:21 +02005731 I915_WRITE(CACHE_MODE_1,
5732 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Ben Widawsky20848222012-05-04 18:58:59 -07005733
Ville Syrjäläa607c1a2014-02-04 21:59:19 +02005734 /*
5735 * BSpec recommends 8x4 when MSAA is used,
5736 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02005737 *
5738 * Note that PS/WM thread counts depend on the WIZ hashing
5739 * disable bit, which we don't touch here, but it's good
5740 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjäläa607c1a2014-02-04 21:59:19 +02005741 */
5742 I915_WRITE(GEN7_GT_MODE,
5743 GEN6_WIZ_HASHING_MASK | GEN6_WIZ_HASHING_16x4);
5744
Ben Widawsky20848222012-05-04 18:58:59 -07005745 snpcr = I915_READ(GEN6_MBCUNIT_SNPCR);
5746 snpcr &= ~GEN6_MBC_SNPCR_MASK;
5747 snpcr |= GEN6_MBC_SNPCR_MED;
5748 I915_WRITE(GEN6_MBCUNIT_SNPCR, snpcr);
Daniel Vetter3107bd42012-10-31 22:52:31 +01005749
Ben Widawskyab5c6082013-04-05 13:12:41 -07005750 if (!HAS_PCH_NOP(dev))
5751 cpt_init_clock_gating(dev);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01005752
5753 gen6_check_mch_setup(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005754}
5755
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005756static void valleyview_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005757{
5758 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes85b1d7b2013-11-04 11:52:45 -08005759 u32 val;
5760
5761 mutex_lock(&dev_priv->rps.hw_lock);
5762 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
5763 mutex_unlock(&dev_priv->rps.hw_lock);
5764 switch ((val >> 6) & 3) {
5765 case 0:
Deepak Sf6d51942014-04-03 21:01:28 +05305766 case 1:
Jesse Barnes85b1d7b2013-11-04 11:52:45 -08005767 dev_priv->mem_freq = 800;
5768 break;
Jesse Barnesf64a28a2013-11-04 16:07:00 -08005769 case 2:
Deepak Sf6d51942014-04-03 21:01:28 +05305770 dev_priv->mem_freq = 1066;
Jesse Barnes85b1d7b2013-11-04 11:52:45 -08005771 break;
Jesse Barnesf64a28a2013-11-04 16:07:00 -08005772 case 3:
Chon Ming Lee23259912013-11-07 15:23:26 +08005773 dev_priv->mem_freq = 1333;
Jesse Barnesf64a28a2013-11-04 16:07:00 -08005774 break;
Jesse Barnes85b1d7b2013-11-04 11:52:45 -08005775 }
5776 DRM_DEBUG_DRIVER("DDR speed: %d MHz", dev_priv->mem_freq);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005777
Ville Syrjäläd7fe0cc2013-05-21 18:01:50 +03005778 I915_WRITE(DSPCLK_GATE_D, VRHUNIT_CLOCK_GATE_DISABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005779
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005780 /* WaDisableEarlyCull:vlv */
Jesse Barnes87f80202012-10-02 17:43:41 -05005781 I915_WRITE(_3D_CHICKEN3,
5782 _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
5783
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005784 /* WaDisableBackToBackFlipFix:vlv */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005785 I915_WRITE(IVB_CHICKEN3,
5786 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
5787 CHICKEN3_DGMG_DONE_FIX_DISABLE);
5788
Ville Syrjäläfad7d362014-01-22 21:32:39 +02005789 /* WaPsdDispatchEnable:vlv */
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005790 /* WaDisablePSDDualDispatchEnable:vlv */
Jesse Barnes12f33822012-10-25 12:15:45 -07005791 I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
Jesse Barnesd3bc0302013-03-08 10:45:51 -08005792 _MASKED_BIT_ENABLE(GEN7_MAX_PS_THREAD_DEP |
5793 GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
Jesse Barnes12f33822012-10-25 12:15:45 -07005794
Akash Goel4e046322014-04-04 17:14:38 +05305795 /* WaDisable_RenderCache_OperationalFlush:vlv */
5796 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
5797
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005798 /* WaForceL3Serialization:vlv */
Jesse Barnes61939d92012-10-02 17:43:38 -05005799 I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
5800 ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
5801
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005802 /* WaDisableDopClockGating:vlv */
Jesse Barnes8ab43972012-10-25 12:15:42 -07005803 I915_WRITE(GEN7_ROW_CHICKEN2,
5804 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
5805
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005806 /* This is required by WaCatErrorRejectionIssue:vlv */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005807 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
5808 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
5809 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
5810
Ville Syrjälä46680e02014-01-22 21:33:01 +02005811 gen7_setup_fixed_func_scheduler(dev_priv);
5812
Ville Syrjälä3c0edae2014-01-22 21:32:56 +02005813 /*
Jesse Barnes0f846f82012-06-14 11:04:47 -07005814 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005815 * This implements the WaDisableRCZUnitClockGating:vlv workaround.
Jesse Barnes0f846f82012-06-14 11:04:47 -07005816 */
5817 I915_WRITE(GEN6_UCGCTL2,
Ville Syrjälä3c0edae2014-01-22 21:32:56 +02005818 GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
Jesse Barnes0f846f82012-06-14 11:04:47 -07005819
Akash Goelc98f5062014-03-24 23:00:07 +05305820 /* WaDisableL3Bank2xClockGate:vlv
5821 * Disabling L3 clock gating- MMIO 940c[25] = 1
5822 * Set bit 25, to disable L3_BANK_2x_CLK_GATING */
5823 I915_WRITE(GEN7_UCGCTL4,
5824 I915_READ(GEN7_UCGCTL4) | GEN7_L3BANK2X_CLOCK_GATE_DISABLE);
Jesse Barnese3f33d42012-06-14 11:04:50 -07005825
Ville Syrjäläe0d8d592013-06-12 22:11:18 +03005826 I915_WRITE(MI_ARB_VLV, MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005827
Ville Syrjäläafd58e72014-01-22 21:33:03 +02005828 /*
5829 * BSpec says this must be set, even though
5830 * WaDisable4x2SubspanOptimization isn't listed for VLV.
5831 */
Daniel Vetter6b26c862012-04-24 14:04:12 +02005832 I915_WRITE(CACHE_MODE_1,
5833 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Jesse Barnes79831172012-06-20 10:53:12 -07005834
5835 /*
Ville Syrjälä031994e2014-01-22 21:32:46 +02005836 * WaIncreaseL3CreditsForVLVB0:vlv
5837 * This is the hardware default actually.
5838 */
5839 I915_WRITE(GEN7_L3SQCREG1, VLV_B0_WA_L3SQCREG1_VALUE);
5840
5841 /*
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005842 * WaDisableVLVClockGating_VBIIssue:vlv
Jesse Barnes2d809572012-10-25 12:15:44 -07005843 * Disable clock gating on th GCFG unit to prevent a delay
5844 * in the reporting of vblank events.
5845 */
Ville Syrjälä7a0d1ee2014-01-22 21:33:04 +02005846 I915_WRITE(VLV_GUNIT_CLOCK_GATE, GCFG_DIS);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005847}
5848
Ville Syrjäläa4565da2014-04-09 13:28:10 +03005849static void cherryview_init_clock_gating(struct drm_device *dev)
5850{
5851 struct drm_i915_private *dev_priv = dev->dev_private;
Deepak S67c3bf62014-07-10 13:16:24 +05305852 u32 val;
5853
5854 mutex_lock(&dev_priv->rps.hw_lock);
5855 val = vlv_punit_read(dev_priv, CCK_FUSE_REG);
5856 mutex_unlock(&dev_priv->rps.hw_lock);
5857 switch ((val >> 2) & 0x7) {
5858 case 0:
5859 case 1:
5860 dev_priv->rps.cz_freq = CHV_CZ_CLOCK_FREQ_MODE_200;
5861 dev_priv->mem_freq = 1600;
5862 break;
5863 case 2:
5864 dev_priv->rps.cz_freq = CHV_CZ_CLOCK_FREQ_MODE_267;
5865 dev_priv->mem_freq = 1600;
5866 break;
5867 case 3:
5868 dev_priv->rps.cz_freq = CHV_CZ_CLOCK_FREQ_MODE_333;
5869 dev_priv->mem_freq = 2000;
5870 break;
5871 case 4:
5872 dev_priv->rps.cz_freq = CHV_CZ_CLOCK_FREQ_MODE_320;
5873 dev_priv->mem_freq = 1600;
5874 break;
5875 case 5:
5876 dev_priv->rps.cz_freq = CHV_CZ_CLOCK_FREQ_MODE_400;
5877 dev_priv->mem_freq = 1600;
5878 break;
5879 }
5880 DRM_DEBUG_DRIVER("DDR speed: %d MHz", dev_priv->mem_freq);
Ville Syrjäläa4565da2014-04-09 13:28:10 +03005881
5882 I915_WRITE(DSPCLK_GATE_D, VRHUNIT_CLOCK_GATE_DISABLE);
5883
5884 I915_WRITE(MI_ARB_VLV, MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE);
Ville Syrjälädd811e72014-04-09 13:28:33 +03005885
5886 /* WaDisablePartialInstShootdown:chv */
5887 I915_WRITE(GEN8_ROW_CHICKEN,
5888 _MASKED_BIT_ENABLE(PARTIAL_INSTRUCTION_SHOOTDOWN_DISABLE));
Ville Syrjäläa7068022014-04-09 13:28:34 +03005889
5890 /* WaDisableThreadStallDopClockGating:chv */
5891 I915_WRITE(GEN8_ROW_CHICKEN,
5892 _MASKED_BIT_ENABLE(STALL_DOP_GATING_DISABLE));
Ville Syrjälä232ce332014-04-09 13:28:35 +03005893
5894 /* WaVSRefCountFullforceMissDisable:chv */
5895 /* WaDSRefCountFullforceMissDisable:chv */
5896 I915_WRITE(GEN7_FF_THREAD_MODE,
5897 I915_READ(GEN7_FF_THREAD_MODE) &
5898 ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
Ville Syrjäläacea6f92014-04-09 13:28:36 +03005899
5900 /* WaDisableSemaphoreAndSyncFlipWait:chv */
5901 I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
5902 _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
Ville Syrjälä08466972014-04-09 13:28:37 +03005903
5904 /* WaDisableCSUnitClockGating:chv */
5905 I915_WRITE(GEN6_UCGCTL1, I915_READ(GEN6_UCGCTL1) |
5906 GEN6_CSUNIT_CLOCK_GATE_DISABLE);
Ville Syrjäläc6317802014-04-09 13:28:38 +03005907
5908 /* WaDisableSDEUnitClockGating:chv */
5909 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
5910 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Rafael Barbalhoe0d34ce2014-04-09 13:28:40 +03005911
5912 /* WaDisableSamplerPowerBypass:chv (pre-production hw) */
5913 I915_WRITE(HALF_SLICE_CHICKEN3,
5914 _MASKED_BIT_ENABLE(GEN8_SAMPLER_POWER_BYPASS_DIS));
Ville Syrjäläe4443e42014-04-09 13:28:41 +03005915
5916 /* WaDisableGunitClockGating:chv (pre-production hw) */
5917 I915_WRITE(VLV_GUNIT_CLOCK_GATE, I915_READ(VLV_GUNIT_CLOCK_GATE) |
5918 GINT_DIS);
5919
5920 /* WaDisableFfDopClockGating:chv (pre-production hw) */
5921 I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
5922 _MASKED_BIT_ENABLE(GEN8_FF_DOP_CLOCK_GATE_DISABLE));
5923
5924 /* WaDisableDopClockGating:chv (pre-production hw) */
5925 I915_WRITE(GEN7_ROW_CHICKEN2,
5926 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
5927 I915_WRITE(GEN6_UCGCTL1, I915_READ(GEN6_UCGCTL1) |
5928 GEN6_EU_TCUNIT_CLOCK_GATE_DISABLE);
Ville Syrjäläa4565da2014-04-09 13:28:10 +03005929}
5930
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005931static void g4x_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005932{
5933 struct drm_i915_private *dev_priv = dev->dev_private;
5934 uint32_t dspclk_gate;
5935
5936 I915_WRITE(RENCLK_GATE_D1, 0);
5937 I915_WRITE(RENCLK_GATE_D2, VF_UNIT_CLOCK_GATE_DISABLE |
5938 GS_UNIT_CLOCK_GATE_DISABLE |
5939 CL_UNIT_CLOCK_GATE_DISABLE);
5940 I915_WRITE(RAMCLK_GATE_D, 0);
5941 dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE |
5942 OVRUNIT_CLOCK_GATE_DISABLE |
5943 OVCUNIT_CLOCK_GATE_DISABLE;
5944 if (IS_GM45(dev))
5945 dspclk_gate |= DSSUNIT_CLOCK_GATE_DISABLE;
5946 I915_WRITE(DSPCLK_GATE_D, dspclk_gate);
Daniel Vetter4358a372012-10-18 11:49:51 +02005947
5948 /* WaDisableRenderCachePipelinedFlush */
5949 I915_WRITE(CACHE_MODE_0,
5950 _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
Ville Syrjäläde1aa622013-06-07 10:47:01 +03005951
Akash Goel4e046322014-04-04 17:14:38 +05305952 /* WaDisable_RenderCache_OperationalFlush:g4x */
5953 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
5954
Ville Syrjälä0e088b82013-06-07 10:47:04 +03005955 g4x_disable_trickle_feed(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005956}
5957
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005958static void crestline_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005959{
5960 struct drm_i915_private *dev_priv = dev->dev_private;
5961
5962 I915_WRITE(RENCLK_GATE_D1, I965_RCC_CLOCK_GATE_DISABLE);
5963 I915_WRITE(RENCLK_GATE_D2, 0);
5964 I915_WRITE(DSPCLK_GATE_D, 0);
5965 I915_WRITE(RAMCLK_GATE_D, 0);
5966 I915_WRITE16(DEUC, 0);
Ville Syrjälä20f94962013-06-07 10:47:02 +03005967 I915_WRITE(MI_ARB_STATE,
5968 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Akash Goel4e046322014-04-04 17:14:38 +05305969
5970 /* WaDisable_RenderCache_OperationalFlush:gen4 */
5971 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005972}
5973
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005974static void broadwater_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005975{
5976 struct drm_i915_private *dev_priv = dev->dev_private;
5977
5978 I915_WRITE(RENCLK_GATE_D1, I965_RCZ_CLOCK_GATE_DISABLE |
5979 I965_RCC_CLOCK_GATE_DISABLE |
5980 I965_RCPB_CLOCK_GATE_DISABLE |
5981 I965_ISC_CLOCK_GATE_DISABLE |
5982 I965_FBC_CLOCK_GATE_DISABLE);
5983 I915_WRITE(RENCLK_GATE_D2, 0);
Ville Syrjälä20f94962013-06-07 10:47:02 +03005984 I915_WRITE(MI_ARB_STATE,
5985 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Akash Goel4e046322014-04-04 17:14:38 +05305986
5987 /* WaDisable_RenderCache_OperationalFlush:gen4 */
5988 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005989}
5990
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005991static void gen3_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005992{
5993 struct drm_i915_private *dev_priv = dev->dev_private;
5994 u32 dstate = I915_READ(D_STATE);
5995
5996 dstate |= DSTATE_PLL_D3_OFF | DSTATE_GFX_CLOCK_GATING |
5997 DSTATE_DOT_CLOCK_GATING;
5998 I915_WRITE(D_STATE, dstate);
Chris Wilson13a86b82012-04-24 14:51:43 +01005999
6000 if (IS_PINEVIEW(dev))
6001 I915_WRITE(ECOSKPD, _MASKED_BIT_ENABLE(ECO_GATING_CX_ONLY));
Daniel Vetter974a3b02012-09-09 11:54:16 +02006002
6003 /* IIR "flip pending" means done if this bit is set */
6004 I915_WRITE(ECOSKPD, _MASKED_BIT_DISABLE(ECO_FLIP_DONE));
Ville Syrjälä12fabbcb92014-02-25 15:13:38 +02006005
6006 /* interrupts should cause a wake up from C3 */
Ville Syrjälä32992542014-02-25 15:13:39 +02006007 I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_AGPBUSY_INT_EN));
Ville Syrjälädbb42742014-02-25 15:13:41 +02006008
6009 /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
6010 I915_WRITE(MI_ARB_STATE, _MASKED_BIT_ENABLE(MI_ARB_C3_LP_WRITE_ENABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006011}
6012
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006013static void i85x_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006014{
6015 struct drm_i915_private *dev_priv = dev->dev_private;
6016
6017 I915_WRITE(RENCLK_GATE_D1, SV_CLOCK_GATE_DISABLE);
Ville Syrjälä54e472a2014-02-25 15:13:40 +02006018
6019 /* interrupts should cause a wake up from C3 */
6020 I915_WRITE(MI_STATE, _MASKED_BIT_ENABLE(MI_AGPBUSY_INT_EN) |
6021 _MASKED_BIT_DISABLE(MI_AGPBUSY_830_MODE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006022}
6023
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006024static void i830_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006025{
6026 struct drm_i915_private *dev_priv = dev->dev_private;
6027
6028 I915_WRITE(DSPCLK_GATE_D, OVRUNIT_CLOCK_GATE_DISABLE);
6029}
6030
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006031void intel_init_clock_gating(struct drm_device *dev)
6032{
6033 struct drm_i915_private *dev_priv = dev->dev_private;
6034
6035 dev_priv->display.init_clock_gating(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006036}
6037
Imre Deak7d708ee2013-04-17 14:04:50 +03006038void intel_suspend_hw(struct drm_device *dev)
6039{
6040 if (HAS_PCH_LPT(dev))
6041 lpt_suspend_hw(dev);
6042}
6043
Imre Deakc1ca7272013-11-25 17:15:29 +02006044#define for_each_power_well(i, power_well, domain_mask, power_domains) \
6045 for (i = 0; \
6046 i < (power_domains)->power_well_count && \
6047 ((power_well) = &(power_domains)->power_wells[i]); \
6048 i++) \
6049 if ((power_well)->domains & (domain_mask))
6050
6051#define for_each_power_well_rev(i, power_well, domain_mask, power_domains) \
6052 for (i = (power_domains)->power_well_count - 1; \
6053 i >= 0 && ((power_well) = &(power_domains)->power_wells[i]);\
6054 i--) \
6055 if ((power_well)->domains & (domain_mask))
6056
Paulo Zanoni15d199e2013-03-22 14:14:13 -03006057/**
6058 * We should only use the power well if we explicitly asked the hardware to
6059 * enable it, so check if it's enabled and also check if we've requested it to
6060 * be enabled.
6061 */
Imre Deakda7e29b2014-02-18 00:02:02 +02006062static bool hsw_power_well_enabled(struct drm_i915_private *dev_priv,
Imre Deakc1ca7272013-11-25 17:15:29 +02006063 struct i915_power_well *power_well)
6064{
Imre Deakc1ca7272013-11-25 17:15:29 +02006065 return I915_READ(HSW_PWR_WELL_DRIVER) ==
6066 (HSW_PWR_WELL_ENABLE_REQUEST | HSW_PWR_WELL_STATE_ENABLED);
6067}
6068
Imre Deakbfafe932014-06-05 20:31:47 +03006069bool intel_display_power_enabled_unlocked(struct drm_i915_private *dev_priv,
6070 enum intel_display_power_domain domain)
Imre Deakddf9c532013-11-27 22:02:02 +02006071{
Imre Deakddf9c532013-11-27 22:02:02 +02006072 struct i915_power_domains *power_domains;
Imre Deakb8c000d2014-06-02 14:21:10 +03006073 struct i915_power_well *power_well;
6074 bool is_enabled;
6075 int i;
6076
6077 if (dev_priv->pm.suspended)
6078 return false;
Imre Deakddf9c532013-11-27 22:02:02 +02006079
6080 power_domains = &dev_priv->power_domains;
Imre Deakbfafe932014-06-05 20:31:47 +03006081
Imre Deakb8c000d2014-06-02 14:21:10 +03006082 is_enabled = true;
Imre Deakbfafe932014-06-05 20:31:47 +03006083
Imre Deakb8c000d2014-06-02 14:21:10 +03006084 for_each_power_well_rev(i, power_well, BIT(domain), power_domains) {
6085 if (power_well->always_on)
6086 continue;
Imre Deakddf9c532013-11-27 22:02:02 +02006087
Imre Deakbfafe932014-06-05 20:31:47 +03006088 if (!power_well->hw_enabled) {
Imre Deakb8c000d2014-06-02 14:21:10 +03006089 is_enabled = false;
6090 break;
6091 }
6092 }
Imre Deakbfafe932014-06-05 20:31:47 +03006093
Imre Deakb8c000d2014-06-02 14:21:10 +03006094 return is_enabled;
Imre Deakddf9c532013-11-27 22:02:02 +02006095}
6096
Imre Deakda7e29b2014-02-18 00:02:02 +02006097bool intel_display_power_enabled(struct drm_i915_private *dev_priv,
Paulo Zanonib97186f2013-05-03 12:15:36 -03006098 enum intel_display_power_domain domain)
Paulo Zanoni15d199e2013-03-22 14:14:13 -03006099{
Imre Deakc1ca7272013-11-25 17:15:29 +02006100 struct i915_power_domains *power_domains;
Imre Deakbfafe932014-06-05 20:31:47 +03006101 bool ret;
Paulo Zanoni882244a2014-04-01 14:55:12 -03006102
Imre Deakc1ca7272013-11-25 17:15:29 +02006103 power_domains = &dev_priv->power_domains;
6104
Imre Deakc1ca7272013-11-25 17:15:29 +02006105 mutex_lock(&power_domains->lock);
Imre Deakbfafe932014-06-05 20:31:47 +03006106 ret = intel_display_power_enabled_unlocked(dev_priv, domain);
Imre Deakc1ca7272013-11-25 17:15:29 +02006107 mutex_unlock(&power_domains->lock);
6108
Imre Deakbfafe932014-06-05 20:31:47 +03006109 return ret;
Paulo Zanoni15d199e2013-03-22 14:14:13 -03006110}
6111
Imre Deak93c73e82014-02-18 00:02:19 +02006112/*
6113 * Starting with Haswell, we have a "Power Down Well" that can be turned off
6114 * when not needed anymore. We have 4 registers that can request the power well
6115 * to be enabled, and it will only be disabled if none of the registers is
6116 * requesting it to be enabled.
6117 */
Paulo Zanonid5e8fdc2013-12-11 18:50:09 -02006118static void hsw_power_well_post_enable(struct drm_i915_private *dev_priv)
6119{
6120 struct drm_device *dev = dev_priv->dev;
Paulo Zanonid5e8fdc2013-12-11 18:50:09 -02006121
Paulo Zanonif9dcb0d2013-12-11 18:50:10 -02006122 /*
6123 * After we re-enable the power well, if we touch VGA register 0x3d5
6124 * we'll get unclaimed register interrupts. This stops after we write
6125 * anything to the VGA MSR register. The vgacon module uses this
6126 * register all the time, so if we unbind our driver and, as a
6127 * consequence, bind vgacon, we'll get stuck in an infinite loop at
6128 * console_unlock(). So make here we touch the VGA MSR register, making
6129 * sure vgacon can keep working normally without triggering interrupts
6130 * and error messages.
6131 */
6132 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
6133 outb(inb(VGA_MSR_READ), VGA_MSR_WRITE);
6134 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
6135
Paulo Zanonid49bdb02014-07-04 11:50:31 -03006136 if (IS_BROADWELL(dev))
6137 gen8_irq_power_well_post_enable(dev_priv);
Paulo Zanonid5e8fdc2013-12-11 18:50:09 -02006138}
6139
Imre Deakda7e29b2014-02-18 00:02:02 +02006140static void hsw_set_power_well(struct drm_i915_private *dev_priv,
Imre Deakc1ca7272013-11-25 17:15:29 +02006141 struct i915_power_well *power_well, bool enable)
Eugeni Dodonovd0d3e512012-05-09 15:37:16 -03006142{
Paulo Zanonifa42e232013-01-25 16:59:11 -02006143 bool is_enabled, enable_requested;
6144 uint32_t tmp;
Eugeni Dodonovd0d3e512012-05-09 15:37:16 -03006145
Paulo Zanonifa42e232013-01-25 16:59:11 -02006146 tmp = I915_READ(HSW_PWR_WELL_DRIVER);
Paulo Zanoni6aedd1f2013-08-02 16:22:25 -03006147 is_enabled = tmp & HSW_PWR_WELL_STATE_ENABLED;
6148 enable_requested = tmp & HSW_PWR_WELL_ENABLE_REQUEST;
Eugeni Dodonovd0d3e512012-05-09 15:37:16 -03006149
Paulo Zanonifa42e232013-01-25 16:59:11 -02006150 if (enable) {
6151 if (!enable_requested)
Paulo Zanoni6aedd1f2013-08-02 16:22:25 -03006152 I915_WRITE(HSW_PWR_WELL_DRIVER,
6153 HSW_PWR_WELL_ENABLE_REQUEST);
Eugeni Dodonovd0d3e512012-05-09 15:37:16 -03006154
Paulo Zanonifa42e232013-01-25 16:59:11 -02006155 if (!is_enabled) {
6156 DRM_DEBUG_KMS("Enabling power well\n");
6157 if (wait_for((I915_READ(HSW_PWR_WELL_DRIVER) &
Paulo Zanoni6aedd1f2013-08-02 16:22:25 -03006158 HSW_PWR_WELL_STATE_ENABLED), 20))
Paulo Zanonifa42e232013-01-25 16:59:11 -02006159 DRM_ERROR("Timeout enabling power well\n");
6160 }
Ben Widawsky596cc112013-11-11 14:46:28 -08006161
Paulo Zanonid5e8fdc2013-12-11 18:50:09 -02006162 hsw_power_well_post_enable(dev_priv);
Paulo Zanonifa42e232013-01-25 16:59:11 -02006163 } else {
6164 if (enable_requested) {
6165 I915_WRITE(HSW_PWR_WELL_DRIVER, 0);
Paulo Zanoni9dbd8fe2013-07-23 10:48:11 -03006166 POSTING_READ(HSW_PWR_WELL_DRIVER);
Paulo Zanonifa42e232013-01-25 16:59:11 -02006167 DRM_DEBUG_KMS("Requesting to disable the power well\n");
Eugeni Dodonovd0d3e512012-05-09 15:37:16 -03006168 }
6169 }
Paulo Zanonifa42e232013-01-25 16:59:11 -02006170}
Eugeni Dodonovd0d3e512012-05-09 15:37:16 -03006171
Imre Deakc6cb5822014-03-04 19:22:55 +02006172static void hsw_power_well_sync_hw(struct drm_i915_private *dev_priv,
6173 struct i915_power_well *power_well)
6174{
6175 hsw_set_power_well(dev_priv, power_well, power_well->count > 0);
6176
6177 /*
6178 * We're taking over the BIOS, so clear any requests made by it since
6179 * the driver is in charge now.
6180 */
6181 if (I915_READ(HSW_PWR_WELL_BIOS) & HSW_PWR_WELL_ENABLE_REQUEST)
6182 I915_WRITE(HSW_PWR_WELL_BIOS, 0);
6183}
6184
6185static void hsw_power_well_enable(struct drm_i915_private *dev_priv,
6186 struct i915_power_well *power_well)
6187{
Imre Deakc6cb5822014-03-04 19:22:55 +02006188 hsw_set_power_well(dev_priv, power_well, true);
6189}
6190
6191static void hsw_power_well_disable(struct drm_i915_private *dev_priv,
6192 struct i915_power_well *power_well)
6193{
6194 hsw_set_power_well(dev_priv, power_well, false);
Imre Deakc6cb5822014-03-04 19:22:55 +02006195}
6196
Imre Deaka45f44662014-03-04 19:22:56 +02006197static void i9xx_always_on_power_well_noop(struct drm_i915_private *dev_priv,
6198 struct i915_power_well *power_well)
6199{
6200}
6201
6202static bool i9xx_always_on_power_well_enabled(struct drm_i915_private *dev_priv,
6203 struct i915_power_well *power_well)
6204{
6205 return true;
6206}
6207
Ville Syrjäläd2011dc2014-06-13 13:37:56 +03006208static void vlv_set_power_well(struct drm_i915_private *dev_priv,
6209 struct i915_power_well *power_well, bool enable)
Imre Deak77961eb2014-03-05 16:20:56 +02006210{
Ville Syrjäläd2011dc2014-06-13 13:37:56 +03006211 enum punit_power_well power_well_id = power_well->data;
Imre Deak77961eb2014-03-05 16:20:56 +02006212 u32 mask;
6213 u32 state;
6214 u32 ctrl;
6215
6216 mask = PUNIT_PWRGT_MASK(power_well_id);
6217 state = enable ? PUNIT_PWRGT_PWR_ON(power_well_id) :
6218 PUNIT_PWRGT_PWR_GATE(power_well_id);
6219
6220 mutex_lock(&dev_priv->rps.hw_lock);
6221
6222#define COND \
6223 ((vlv_punit_read(dev_priv, PUNIT_REG_PWRGT_STATUS) & mask) == state)
6224
6225 if (COND)
6226 goto out;
6227
6228 ctrl = vlv_punit_read(dev_priv, PUNIT_REG_PWRGT_CTRL);
6229 ctrl &= ~mask;
6230 ctrl |= state;
6231 vlv_punit_write(dev_priv, PUNIT_REG_PWRGT_CTRL, ctrl);
6232
6233 if (wait_for(COND, 100))
6234 DRM_ERROR("timout setting power well state %08x (%08x)\n",
6235 state,
6236 vlv_punit_read(dev_priv, PUNIT_REG_PWRGT_CTRL));
6237
6238#undef COND
6239
6240out:
6241 mutex_unlock(&dev_priv->rps.hw_lock);
6242}
6243
6244static void vlv_power_well_sync_hw(struct drm_i915_private *dev_priv,
6245 struct i915_power_well *power_well)
6246{
6247 vlv_set_power_well(dev_priv, power_well, power_well->count > 0);
6248}
6249
6250static void vlv_power_well_enable(struct drm_i915_private *dev_priv,
6251 struct i915_power_well *power_well)
6252{
6253 vlv_set_power_well(dev_priv, power_well, true);
6254}
6255
6256static void vlv_power_well_disable(struct drm_i915_private *dev_priv,
6257 struct i915_power_well *power_well)
6258{
6259 vlv_set_power_well(dev_priv, power_well, false);
6260}
6261
6262static bool vlv_power_well_enabled(struct drm_i915_private *dev_priv,
6263 struct i915_power_well *power_well)
6264{
6265 int power_well_id = power_well->data;
6266 bool enabled = false;
6267 u32 mask;
6268 u32 state;
6269 u32 ctrl;
6270
6271 mask = PUNIT_PWRGT_MASK(power_well_id);
6272 ctrl = PUNIT_PWRGT_PWR_ON(power_well_id);
6273
6274 mutex_lock(&dev_priv->rps.hw_lock);
6275
6276 state = vlv_punit_read(dev_priv, PUNIT_REG_PWRGT_STATUS) & mask;
6277 /*
6278 * We only ever set the power-on and power-gate states, anything
6279 * else is unexpected.
6280 */
6281 WARN_ON(state != PUNIT_PWRGT_PWR_ON(power_well_id) &&
6282 state != PUNIT_PWRGT_PWR_GATE(power_well_id));
6283 if (state == ctrl)
6284 enabled = true;
6285
6286 /*
6287 * A transient state at this point would mean some unexpected party
6288 * is poking at the power controls too.
6289 */
6290 ctrl = vlv_punit_read(dev_priv, PUNIT_REG_PWRGT_CTRL) & mask;
6291 WARN_ON(ctrl != state);
6292
6293 mutex_unlock(&dev_priv->rps.hw_lock);
6294
6295 return enabled;
6296}
6297
6298static void vlv_display_power_well_enable(struct drm_i915_private *dev_priv,
6299 struct i915_power_well *power_well)
6300{
6301 WARN_ON_ONCE(power_well->data != PUNIT_POWER_WELL_DISP2D);
6302
6303 vlv_set_power_well(dev_priv, power_well, true);
6304
6305 spin_lock_irq(&dev_priv->irq_lock);
6306 valleyview_enable_display_irqs(dev_priv);
6307 spin_unlock_irq(&dev_priv->irq_lock);
6308
6309 /*
Imre Deak0d116a22014-04-25 13:19:05 +03006310 * During driver initialization/resume we can avoid restoring the
6311 * part of the HW/SW state that will be inited anyway explicitly.
Imre Deak77961eb2014-03-05 16:20:56 +02006312 */
Imre Deak0d116a22014-04-25 13:19:05 +03006313 if (dev_priv->power_domains.initializing)
6314 return;
6315
6316 intel_hpd_init(dev_priv->dev);
Imre Deak77961eb2014-03-05 16:20:56 +02006317
6318 i915_redisable_vga_power_on(dev_priv->dev);
6319}
6320
6321static void vlv_display_power_well_disable(struct drm_i915_private *dev_priv,
6322 struct i915_power_well *power_well)
6323{
Imre Deak77961eb2014-03-05 16:20:56 +02006324 WARN_ON_ONCE(power_well->data != PUNIT_POWER_WELL_DISP2D);
6325
6326 spin_lock_irq(&dev_priv->irq_lock);
Imre Deak77961eb2014-03-05 16:20:56 +02006327 valleyview_disable_display_irqs(dev_priv);
6328 spin_unlock_irq(&dev_priv->irq_lock);
6329
Imre Deak77961eb2014-03-05 16:20:56 +02006330 vlv_set_power_well(dev_priv, power_well, false);
6331}
6332
Ville Syrjäläaa519f22014-06-13 13:37:55 +03006333static void vlv_dpio_cmn_power_well_enable(struct drm_i915_private *dev_priv,
6334 struct i915_power_well *power_well)
6335{
6336 WARN_ON_ONCE(power_well->data != PUNIT_POWER_WELL_DPIO_CMN_BC);
6337
6338 /*
6339 * Enable the CRI clock source so we can get at the
6340 * display and the reference clock for VGA
6341 * hotplug / manual detection.
6342 */
6343 I915_WRITE(DPLL(PIPE_B), I915_READ(DPLL(PIPE_B)) |
6344 DPLL_REFA_CLK_ENABLE_VLV | DPLL_INTEGRATED_CRI_CLK_VLV);
6345 udelay(1); /* >10ns for cmnreset, >0ns for sidereset */
6346
6347 vlv_set_power_well(dev_priv, power_well, true);
6348
6349 /*
6350 * From VLV2A0_DP_eDP_DPIO_driver_vbios_notes_10.docx -
6351 * 6. De-assert cmn_reset/side_reset. Same as VLV X0.
6352 * a. GUnit 0x2110 bit[0] set to 1 (def 0)
6353 * b. The other bits such as sfr settings / modesel may all
6354 * be set to 0.
6355 *
6356 * This should only be done on init and resume from S3 with
6357 * both PLLs disabled, or we risk losing DPIO and PLL
6358 * synchronization.
6359 */
6360 I915_WRITE(DPIO_CTL, I915_READ(DPIO_CTL) | DPIO_CMNRST);
6361}
6362
6363static void vlv_dpio_cmn_power_well_disable(struct drm_i915_private *dev_priv,
6364 struct i915_power_well *power_well)
6365{
Ville Syrjäläaa519f22014-06-13 13:37:55 +03006366 enum pipe pipe;
6367
6368 WARN_ON_ONCE(power_well->data != PUNIT_POWER_WELL_DPIO_CMN_BC);
6369
Damien Lespiau055e3932014-08-18 13:49:10 +01006370 for_each_pipe(dev_priv, pipe)
Ville Syrjäläaa519f22014-06-13 13:37:55 +03006371 assert_pll_disabled(dev_priv, pipe);
6372
6373 /* Assert common reset */
6374 I915_WRITE(DPIO_CTL, I915_READ(DPIO_CTL) & ~DPIO_CMNRST);
6375
6376 vlv_set_power_well(dev_priv, power_well, false);
6377}
6378
Ville Syrjälä5d6f7ea2014-06-28 02:04:08 +03006379static void chv_dpio_cmn_power_well_enable(struct drm_i915_private *dev_priv,
6380 struct i915_power_well *power_well)
6381{
6382 enum dpio_phy phy;
6383
6384 WARN_ON_ONCE(power_well->data != PUNIT_POWER_WELL_DPIO_CMN_BC &&
6385 power_well->data != PUNIT_POWER_WELL_DPIO_CMN_D);
6386
6387 /*
6388 * Enable the CRI clock source so we can get at the
6389 * display and the reference clock for VGA
6390 * hotplug / manual detection.
6391 */
6392 if (power_well->data == PUNIT_POWER_WELL_DPIO_CMN_BC) {
6393 phy = DPIO_PHY0;
6394 I915_WRITE(DPLL(PIPE_B), I915_READ(DPLL(PIPE_B)) |
6395 DPLL_REFA_CLK_ENABLE_VLV);
6396 I915_WRITE(DPLL(PIPE_B), I915_READ(DPLL(PIPE_B)) |
6397 DPLL_REFA_CLK_ENABLE_VLV | DPLL_INTEGRATED_CRI_CLK_VLV);
6398 } else {
6399 phy = DPIO_PHY1;
6400 I915_WRITE(DPLL(PIPE_C), I915_READ(DPLL(PIPE_C)) |
6401 DPLL_REFA_CLK_ENABLE_VLV | DPLL_INTEGRATED_CRI_CLK_VLV);
6402 }
6403 udelay(1); /* >10ns for cmnreset, >0ns for sidereset */
6404 vlv_set_power_well(dev_priv, power_well, true);
6405
6406 /* Poll for phypwrgood signal */
6407 if (wait_for(I915_READ(DISPLAY_PHY_STATUS) & PHY_POWERGOOD(phy), 1))
6408 DRM_ERROR("Display PHY %d is not power up\n", phy);
6409
Ville Syrjäläefd814b2014-06-27 19:52:13 +03006410 I915_WRITE(DISPLAY_PHY_CONTROL, I915_READ(DISPLAY_PHY_CONTROL) |
6411 PHY_COM_LANE_RESET_DEASSERT(phy));
Ville Syrjälä5d6f7ea2014-06-28 02:04:08 +03006412}
6413
6414static void chv_dpio_cmn_power_well_disable(struct drm_i915_private *dev_priv,
6415 struct i915_power_well *power_well)
6416{
6417 enum dpio_phy phy;
6418
6419 WARN_ON_ONCE(power_well->data != PUNIT_POWER_WELL_DPIO_CMN_BC &&
6420 power_well->data != PUNIT_POWER_WELL_DPIO_CMN_D);
6421
6422 if (power_well->data == PUNIT_POWER_WELL_DPIO_CMN_BC) {
6423 phy = DPIO_PHY0;
6424 assert_pll_disabled(dev_priv, PIPE_A);
6425 assert_pll_disabled(dev_priv, PIPE_B);
6426 } else {
6427 phy = DPIO_PHY1;
6428 assert_pll_disabled(dev_priv, PIPE_C);
6429 }
6430
Ville Syrjäläefd814b2014-06-27 19:52:13 +03006431 I915_WRITE(DISPLAY_PHY_CONTROL, I915_READ(DISPLAY_PHY_CONTROL) &
6432 ~PHY_COM_LANE_RESET_DEASSERT(phy));
Ville Syrjälä5d6f7ea2014-06-28 02:04:08 +03006433
6434 vlv_set_power_well(dev_priv, power_well, false);
6435}
6436
Ville Syrjälä26972b02014-06-28 02:04:11 +03006437static bool chv_pipe_power_well_enabled(struct drm_i915_private *dev_priv,
6438 struct i915_power_well *power_well)
6439{
6440 enum pipe pipe = power_well->data;
6441 bool enabled;
6442 u32 state, ctrl;
6443
6444 mutex_lock(&dev_priv->rps.hw_lock);
6445
6446 state = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) & DP_SSS_MASK(pipe);
6447 /*
6448 * We only ever set the power-on and power-gate states, anything
6449 * else is unexpected.
6450 */
6451 WARN_ON(state != DP_SSS_PWR_ON(pipe) && state != DP_SSS_PWR_GATE(pipe));
6452 enabled = state == DP_SSS_PWR_ON(pipe);
6453
6454 /*
6455 * A transient state at this point would mean some unexpected party
6456 * is poking at the power controls too.
6457 */
6458 ctrl = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) & DP_SSC_MASK(pipe);
6459 WARN_ON(ctrl << 16 != state);
6460
6461 mutex_unlock(&dev_priv->rps.hw_lock);
6462
6463 return enabled;
6464}
6465
6466static void chv_set_pipe_power_well(struct drm_i915_private *dev_priv,
6467 struct i915_power_well *power_well,
6468 bool enable)
6469{
6470 enum pipe pipe = power_well->data;
6471 u32 state;
6472 u32 ctrl;
6473
6474 state = enable ? DP_SSS_PWR_ON(pipe) : DP_SSS_PWR_GATE(pipe);
6475
6476 mutex_lock(&dev_priv->rps.hw_lock);
6477
6478#define COND \
6479 ((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) & DP_SSS_MASK(pipe)) == state)
6480
6481 if (COND)
6482 goto out;
6483
6484 ctrl = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
6485 ctrl &= ~DP_SSC_MASK(pipe);
6486 ctrl |= enable ? DP_SSC_PWR_ON(pipe) : DP_SSC_PWR_GATE(pipe);
6487 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, ctrl);
6488
6489 if (wait_for(COND, 100))
6490 DRM_ERROR("timout setting power well state %08x (%08x)\n",
6491 state,
6492 vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ));
6493
6494#undef COND
6495
6496out:
6497 mutex_unlock(&dev_priv->rps.hw_lock);
6498}
6499
6500static void chv_pipe_power_well_sync_hw(struct drm_i915_private *dev_priv,
6501 struct i915_power_well *power_well)
6502{
6503 chv_set_pipe_power_well(dev_priv, power_well, power_well->count > 0);
6504}
6505
6506static void chv_pipe_power_well_enable(struct drm_i915_private *dev_priv,
6507 struct i915_power_well *power_well)
6508{
6509 WARN_ON_ONCE(power_well->data != PIPE_A &&
6510 power_well->data != PIPE_B &&
6511 power_well->data != PIPE_C);
6512
6513 chv_set_pipe_power_well(dev_priv, power_well, true);
6514}
6515
6516static void chv_pipe_power_well_disable(struct drm_i915_private *dev_priv,
6517 struct i915_power_well *power_well)
6518{
6519 WARN_ON_ONCE(power_well->data != PIPE_A &&
6520 power_well->data != PIPE_B &&
6521 power_well->data != PIPE_C);
6522
6523 chv_set_pipe_power_well(dev_priv, power_well, false);
6524}
6525
Imre Deak25eaa002014-03-04 19:23:06 +02006526static void check_power_well_state(struct drm_i915_private *dev_priv,
6527 struct i915_power_well *power_well)
6528{
6529 bool enabled = power_well->ops->is_enabled(dev_priv, power_well);
6530
6531 if (power_well->always_on || !i915.disable_power_well) {
6532 if (!enabled)
6533 goto mismatch;
6534
6535 return;
6536 }
6537
6538 if (enabled != (power_well->count > 0))
6539 goto mismatch;
6540
6541 return;
6542
6543mismatch:
6544 WARN(1, "state mismatch for '%s' (always_on %d hw state %d use-count %d disable_power_well %d\n",
6545 power_well->name, power_well->always_on, enabled,
6546 power_well->count, i915.disable_power_well);
6547}
6548
Imre Deakda7e29b2014-02-18 00:02:02 +02006549void intel_display_power_get(struct drm_i915_private *dev_priv,
Ville Syrjälä67656252013-09-16 17:38:28 +03006550 enum intel_display_power_domain domain)
6551{
Imre Deak83c00f52013-10-25 17:36:47 +03006552 struct i915_power_domains *power_domains;
Imre Deakc1ca7272013-11-25 17:15:29 +02006553 struct i915_power_well *power_well;
6554 int i;
Ville Syrjälä67656252013-09-16 17:38:28 +03006555
Paulo Zanoni9e6ea712014-03-07 20:08:06 -03006556 intel_runtime_pm_get(dev_priv);
6557
Imre Deak83c00f52013-10-25 17:36:47 +03006558 power_domains = &dev_priv->power_domains;
6559
6560 mutex_lock(&power_domains->lock);
Imre Deak1da51582013-11-25 17:15:35 +02006561
Imre Deak25eaa002014-03-04 19:23:06 +02006562 for_each_power_well(i, power_well, BIT(domain), power_domains) {
6563 if (!power_well->count++) {
6564 DRM_DEBUG_KMS("enabling %s\n", power_well->name);
Imre Deakc6cb5822014-03-04 19:22:55 +02006565 power_well->ops->enable(dev_priv, power_well);
Imre Deakbfafe932014-06-05 20:31:47 +03006566 power_well->hw_enabled = true;
Imre Deak25eaa002014-03-04 19:23:06 +02006567 }
6568
6569 check_power_well_state(dev_priv, power_well);
6570 }
Imre Deak1da51582013-11-25 17:15:35 +02006571
Imre Deakddf9c532013-11-27 22:02:02 +02006572 power_domains->domain_use_count[domain]++;
6573
Imre Deak83c00f52013-10-25 17:36:47 +03006574 mutex_unlock(&power_domains->lock);
Ville Syrjälä67656252013-09-16 17:38:28 +03006575}
6576
Imre Deakda7e29b2014-02-18 00:02:02 +02006577void intel_display_power_put(struct drm_i915_private *dev_priv,
Ville Syrjälä67656252013-09-16 17:38:28 +03006578 enum intel_display_power_domain domain)
6579{
Imre Deak83c00f52013-10-25 17:36:47 +03006580 struct i915_power_domains *power_domains;
Imre Deakc1ca7272013-11-25 17:15:29 +02006581 struct i915_power_well *power_well;
6582 int i;
Ville Syrjälä67656252013-09-16 17:38:28 +03006583
Imre Deak83c00f52013-10-25 17:36:47 +03006584 power_domains = &dev_priv->power_domains;
6585
6586 mutex_lock(&power_domains->lock);
Imre Deak1da51582013-11-25 17:15:35 +02006587
Imre Deak1da51582013-11-25 17:15:35 +02006588 WARN_ON(!power_domains->domain_use_count[domain]);
6589 power_domains->domain_use_count[domain]--;
Imre Deakddf9c532013-11-27 22:02:02 +02006590
Imre Deak70bf4072014-03-04 19:22:51 +02006591 for_each_power_well_rev(i, power_well, BIT(domain), power_domains) {
6592 WARN_ON(!power_well->count);
6593
Imre Deak25eaa002014-03-04 19:23:06 +02006594 if (!--power_well->count && i915.disable_power_well) {
6595 DRM_DEBUG_KMS("disabling %s\n", power_well->name);
Imre Deakbfafe932014-06-05 20:31:47 +03006596 power_well->hw_enabled = false;
Imre Deakc6cb5822014-03-04 19:22:55 +02006597 power_well->ops->disable(dev_priv, power_well);
Imre Deak25eaa002014-03-04 19:23:06 +02006598 }
6599
6600 check_power_well_state(dev_priv, power_well);
Imre Deak70bf4072014-03-04 19:22:51 +02006601 }
Imre Deak1da51582013-11-25 17:15:35 +02006602
Imre Deak83c00f52013-10-25 17:36:47 +03006603 mutex_unlock(&power_domains->lock);
Paulo Zanoni9e6ea712014-03-07 20:08:06 -03006604
6605 intel_runtime_pm_put(dev_priv);
Ville Syrjälä67656252013-09-16 17:38:28 +03006606}
6607
Imre Deak83c00f52013-10-25 17:36:47 +03006608static struct i915_power_domains *hsw_pwr;
Wang Xingchaoa38911a2013-05-30 22:07:11 +08006609
6610/* Display audio driver power well request */
Takashi Iwai74b0c2d2014-06-13 15:14:34 +02006611int i915_request_power_well(void)
Wang Xingchaoa38911a2013-05-30 22:07:11 +08006612{
Imre Deakb4ed4482013-10-25 17:36:49 +03006613 struct drm_i915_private *dev_priv;
6614
Takashi Iwai74b0c2d2014-06-13 15:14:34 +02006615 if (!hsw_pwr)
6616 return -ENODEV;
Wang Xingchaoa38911a2013-05-30 22:07:11 +08006617
Imre Deakb4ed4482013-10-25 17:36:49 +03006618 dev_priv = container_of(hsw_pwr, struct drm_i915_private,
6619 power_domains);
Imre Deakda7e29b2014-02-18 00:02:02 +02006620 intel_display_power_get(dev_priv, POWER_DOMAIN_AUDIO);
Takashi Iwai74b0c2d2014-06-13 15:14:34 +02006621 return 0;
Wang Xingchaoa38911a2013-05-30 22:07:11 +08006622}
6623EXPORT_SYMBOL_GPL(i915_request_power_well);
6624
6625/* Display audio driver power well release */
Takashi Iwai74b0c2d2014-06-13 15:14:34 +02006626int i915_release_power_well(void)
Wang Xingchaoa38911a2013-05-30 22:07:11 +08006627{
Imre Deakb4ed4482013-10-25 17:36:49 +03006628 struct drm_i915_private *dev_priv;
6629
Takashi Iwai74b0c2d2014-06-13 15:14:34 +02006630 if (!hsw_pwr)
6631 return -ENODEV;
Wang Xingchaoa38911a2013-05-30 22:07:11 +08006632
Imre Deakb4ed4482013-10-25 17:36:49 +03006633 dev_priv = container_of(hsw_pwr, struct drm_i915_private,
6634 power_domains);
Imre Deakda7e29b2014-02-18 00:02:02 +02006635 intel_display_power_put(dev_priv, POWER_DOMAIN_AUDIO);
Takashi Iwai74b0c2d2014-06-13 15:14:34 +02006636 return 0;
Wang Xingchaoa38911a2013-05-30 22:07:11 +08006637}
6638EXPORT_SYMBOL_GPL(i915_release_power_well);
6639
Jani Nikulac149dcb2014-07-04 10:00:37 +08006640/*
6641 * Private interface for the audio driver to get CDCLK in kHz.
6642 *
6643 * Caller must request power well using i915_request_power_well() prior to
6644 * making the call.
6645 */
6646int i915_get_cdclk_freq(void)
6647{
6648 struct drm_i915_private *dev_priv;
6649
6650 if (!hsw_pwr)
6651 return -ENODEV;
6652
6653 dev_priv = container_of(hsw_pwr, struct drm_i915_private,
6654 power_domains);
6655
6656 return intel_ddi_get_cdclk_freq(dev_priv);
6657}
6658EXPORT_SYMBOL_GPL(i915_get_cdclk_freq);
6659
6660
Imre Deakefcad912014-03-04 19:22:53 +02006661#define POWER_DOMAIN_MASK (BIT(POWER_DOMAIN_NUM) - 1)
6662
6663#define HSW_ALWAYS_ON_POWER_DOMAINS ( \
6664 BIT(POWER_DOMAIN_PIPE_A) | \
Imre Deakf5938f32014-03-04 19:22:54 +02006665 BIT(POWER_DOMAIN_TRANSCODER_EDP) | \
Imre Deak319be8a2014-03-04 19:22:57 +02006666 BIT(POWER_DOMAIN_PORT_DDI_A_2_LANES) | \
6667 BIT(POWER_DOMAIN_PORT_DDI_A_4_LANES) | \
6668 BIT(POWER_DOMAIN_PORT_DDI_B_2_LANES) | \
6669 BIT(POWER_DOMAIN_PORT_DDI_B_4_LANES) | \
6670 BIT(POWER_DOMAIN_PORT_DDI_C_2_LANES) | \
6671 BIT(POWER_DOMAIN_PORT_DDI_C_4_LANES) | \
6672 BIT(POWER_DOMAIN_PORT_DDI_D_2_LANES) | \
6673 BIT(POWER_DOMAIN_PORT_DDI_D_4_LANES) | \
6674 BIT(POWER_DOMAIN_PORT_CRT) | \
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -03006675 BIT(POWER_DOMAIN_PLLS) | \
Imre Deakf5938f32014-03-04 19:22:54 +02006676 BIT(POWER_DOMAIN_INIT))
Imre Deakefcad912014-03-04 19:22:53 +02006677#define HSW_DISPLAY_POWER_DOMAINS ( \
6678 (POWER_DOMAIN_MASK & ~HSW_ALWAYS_ON_POWER_DOMAINS) | \
6679 BIT(POWER_DOMAIN_INIT))
6680
6681#define BDW_ALWAYS_ON_POWER_DOMAINS ( \
6682 HSW_ALWAYS_ON_POWER_DOMAINS | \
6683 BIT(POWER_DOMAIN_PIPE_A_PANEL_FITTER))
6684#define BDW_DISPLAY_POWER_DOMAINS ( \
6685 (POWER_DOMAIN_MASK & ~BDW_ALWAYS_ON_POWER_DOMAINS) | \
6686 BIT(POWER_DOMAIN_INIT))
6687
Imre Deak77961eb2014-03-05 16:20:56 +02006688#define VLV_ALWAYS_ON_POWER_DOMAINS BIT(POWER_DOMAIN_INIT)
6689#define VLV_DISPLAY_POWER_DOMAINS POWER_DOMAIN_MASK
6690
6691#define VLV_DPIO_CMN_BC_POWER_DOMAINS ( \
6692 BIT(POWER_DOMAIN_PORT_DDI_B_2_LANES) | \
6693 BIT(POWER_DOMAIN_PORT_DDI_B_4_LANES) | \
6694 BIT(POWER_DOMAIN_PORT_DDI_C_2_LANES) | \
6695 BIT(POWER_DOMAIN_PORT_DDI_C_4_LANES) | \
6696 BIT(POWER_DOMAIN_PORT_CRT) | \
6697 BIT(POWER_DOMAIN_INIT))
6698
6699#define VLV_DPIO_TX_B_LANES_01_POWER_DOMAINS ( \
6700 BIT(POWER_DOMAIN_PORT_DDI_B_2_LANES) | \
6701 BIT(POWER_DOMAIN_PORT_DDI_B_4_LANES) | \
6702 BIT(POWER_DOMAIN_INIT))
6703
6704#define VLV_DPIO_TX_B_LANES_23_POWER_DOMAINS ( \
6705 BIT(POWER_DOMAIN_PORT_DDI_B_4_LANES) | \
6706 BIT(POWER_DOMAIN_INIT))
6707
6708#define VLV_DPIO_TX_C_LANES_01_POWER_DOMAINS ( \
6709 BIT(POWER_DOMAIN_PORT_DDI_C_2_LANES) | \
6710 BIT(POWER_DOMAIN_PORT_DDI_C_4_LANES) | \
6711 BIT(POWER_DOMAIN_INIT))
6712
6713#define VLV_DPIO_TX_C_LANES_23_POWER_DOMAINS ( \
6714 BIT(POWER_DOMAIN_PORT_DDI_C_4_LANES) | \
6715 BIT(POWER_DOMAIN_INIT))
6716
Ville Syrjälä26972b02014-06-28 02:04:11 +03006717#define CHV_PIPE_A_POWER_DOMAINS ( \
6718 BIT(POWER_DOMAIN_PIPE_A) | \
6719 BIT(POWER_DOMAIN_INIT))
6720
6721#define CHV_PIPE_B_POWER_DOMAINS ( \
6722 BIT(POWER_DOMAIN_PIPE_B) | \
6723 BIT(POWER_DOMAIN_INIT))
6724
6725#define CHV_PIPE_C_POWER_DOMAINS ( \
6726 BIT(POWER_DOMAIN_PIPE_C) | \
6727 BIT(POWER_DOMAIN_INIT))
6728
Ville Syrjälä5d6f7ea2014-06-28 02:04:08 +03006729#define CHV_DPIO_CMN_BC_POWER_DOMAINS ( \
6730 BIT(POWER_DOMAIN_PORT_DDI_B_2_LANES) | \
6731 BIT(POWER_DOMAIN_PORT_DDI_B_4_LANES) | \
6732 BIT(POWER_DOMAIN_PORT_DDI_C_2_LANES) | \
6733 BIT(POWER_DOMAIN_PORT_DDI_C_4_LANES) | \
6734 BIT(POWER_DOMAIN_INIT))
6735
6736#define CHV_DPIO_CMN_D_POWER_DOMAINS ( \
6737 BIT(POWER_DOMAIN_PORT_DDI_D_2_LANES) | \
6738 BIT(POWER_DOMAIN_PORT_DDI_D_4_LANES) | \
6739 BIT(POWER_DOMAIN_INIT))
6740
Ville Syrjälä2ce147f2014-06-28 02:04:13 +03006741#define CHV_DPIO_TX_D_LANES_01_POWER_DOMAINS ( \
6742 BIT(POWER_DOMAIN_PORT_DDI_D_2_LANES) | \
6743 BIT(POWER_DOMAIN_PORT_DDI_D_4_LANES) | \
6744 BIT(POWER_DOMAIN_INIT))
6745
6746#define CHV_DPIO_TX_D_LANES_23_POWER_DOMAINS ( \
6747 BIT(POWER_DOMAIN_PORT_DDI_D_4_LANES) | \
6748 BIT(POWER_DOMAIN_INIT))
6749
Imre Deaka45f44662014-03-04 19:22:56 +02006750static const struct i915_power_well_ops i9xx_always_on_power_well_ops = {
6751 .sync_hw = i9xx_always_on_power_well_noop,
6752 .enable = i9xx_always_on_power_well_noop,
6753 .disable = i9xx_always_on_power_well_noop,
6754 .is_enabled = i9xx_always_on_power_well_enabled,
6755};
Imre Deakc6cb5822014-03-04 19:22:55 +02006756
Ville Syrjälä26972b02014-06-28 02:04:11 +03006757static const struct i915_power_well_ops chv_pipe_power_well_ops = {
6758 .sync_hw = chv_pipe_power_well_sync_hw,
6759 .enable = chv_pipe_power_well_enable,
6760 .disable = chv_pipe_power_well_disable,
6761 .is_enabled = chv_pipe_power_well_enabled,
6762};
6763
Ville Syrjälä5d6f7ea2014-06-28 02:04:08 +03006764static const struct i915_power_well_ops chv_dpio_cmn_power_well_ops = {
6765 .sync_hw = vlv_power_well_sync_hw,
6766 .enable = chv_dpio_cmn_power_well_enable,
6767 .disable = chv_dpio_cmn_power_well_disable,
6768 .is_enabled = vlv_power_well_enabled,
6769};
6770
Imre Deak1c2256d2013-11-25 17:15:34 +02006771static struct i915_power_well i9xx_always_on_power_well[] = {
6772 {
6773 .name = "always-on",
6774 .always_on = 1,
6775 .domains = POWER_DOMAIN_MASK,
Imre Deakc6cb5822014-03-04 19:22:55 +02006776 .ops = &i9xx_always_on_power_well_ops,
Imre Deak1c2256d2013-11-25 17:15:34 +02006777 },
6778};
6779
Imre Deakc6cb5822014-03-04 19:22:55 +02006780static const struct i915_power_well_ops hsw_power_well_ops = {
6781 .sync_hw = hsw_power_well_sync_hw,
6782 .enable = hsw_power_well_enable,
6783 .disable = hsw_power_well_disable,
6784 .is_enabled = hsw_power_well_enabled,
6785};
6786
Imre Deakc1ca7272013-11-25 17:15:29 +02006787static struct i915_power_well hsw_power_wells[] = {
6788 {
Imre Deak6f3ef5d2013-11-25 17:15:30 +02006789 .name = "always-on",
6790 .always_on = 1,
6791 .domains = HSW_ALWAYS_ON_POWER_DOMAINS,
Imre Deakc6cb5822014-03-04 19:22:55 +02006792 .ops = &i9xx_always_on_power_well_ops,
Imre Deak6f3ef5d2013-11-25 17:15:30 +02006793 },
6794 {
Imre Deakc1ca7272013-11-25 17:15:29 +02006795 .name = "display",
Imre Deakefcad912014-03-04 19:22:53 +02006796 .domains = HSW_DISPLAY_POWER_DOMAINS,
Imre Deakc6cb5822014-03-04 19:22:55 +02006797 .ops = &hsw_power_well_ops,
Imre Deakc1ca7272013-11-25 17:15:29 +02006798 },
6799};
6800
6801static struct i915_power_well bdw_power_wells[] = {
6802 {
Imre Deak6f3ef5d2013-11-25 17:15:30 +02006803 .name = "always-on",
6804 .always_on = 1,
6805 .domains = BDW_ALWAYS_ON_POWER_DOMAINS,
Imre Deakc6cb5822014-03-04 19:22:55 +02006806 .ops = &i9xx_always_on_power_well_ops,
Imre Deak6f3ef5d2013-11-25 17:15:30 +02006807 },
6808 {
Imre Deakc1ca7272013-11-25 17:15:29 +02006809 .name = "display",
Imre Deakefcad912014-03-04 19:22:53 +02006810 .domains = BDW_DISPLAY_POWER_DOMAINS,
Imre Deakc6cb5822014-03-04 19:22:55 +02006811 .ops = &hsw_power_well_ops,
Imre Deakc1ca7272013-11-25 17:15:29 +02006812 },
6813};
6814
Imre Deak77961eb2014-03-05 16:20:56 +02006815static const struct i915_power_well_ops vlv_display_power_well_ops = {
6816 .sync_hw = vlv_power_well_sync_hw,
6817 .enable = vlv_display_power_well_enable,
6818 .disable = vlv_display_power_well_disable,
6819 .is_enabled = vlv_power_well_enabled,
6820};
6821
Ville Syrjäläaa519f22014-06-13 13:37:55 +03006822static const struct i915_power_well_ops vlv_dpio_cmn_power_well_ops = {
6823 .sync_hw = vlv_power_well_sync_hw,
6824 .enable = vlv_dpio_cmn_power_well_enable,
6825 .disable = vlv_dpio_cmn_power_well_disable,
6826 .is_enabled = vlv_power_well_enabled,
6827};
6828
Imre Deak77961eb2014-03-05 16:20:56 +02006829static const struct i915_power_well_ops vlv_dpio_power_well_ops = {
6830 .sync_hw = vlv_power_well_sync_hw,
6831 .enable = vlv_power_well_enable,
6832 .disable = vlv_power_well_disable,
6833 .is_enabled = vlv_power_well_enabled,
6834};
6835
6836static struct i915_power_well vlv_power_wells[] = {
6837 {
6838 .name = "always-on",
6839 .always_on = 1,
6840 .domains = VLV_ALWAYS_ON_POWER_DOMAINS,
6841 .ops = &i9xx_always_on_power_well_ops,
6842 },
6843 {
6844 .name = "display",
6845 .domains = VLV_DISPLAY_POWER_DOMAINS,
6846 .data = PUNIT_POWER_WELL_DISP2D,
6847 .ops = &vlv_display_power_well_ops,
6848 },
6849 {
Imre Deak77961eb2014-03-05 16:20:56 +02006850 .name = "dpio-tx-b-01",
6851 .domains = VLV_DPIO_TX_B_LANES_01_POWER_DOMAINS |
6852 VLV_DPIO_TX_B_LANES_23_POWER_DOMAINS |
6853 VLV_DPIO_TX_C_LANES_01_POWER_DOMAINS |
6854 VLV_DPIO_TX_C_LANES_23_POWER_DOMAINS,
6855 .ops = &vlv_dpio_power_well_ops,
6856 .data = PUNIT_POWER_WELL_DPIO_TX_B_LANES_01,
6857 },
6858 {
6859 .name = "dpio-tx-b-23",
6860 .domains = VLV_DPIO_TX_B_LANES_01_POWER_DOMAINS |
6861 VLV_DPIO_TX_B_LANES_23_POWER_DOMAINS |
6862 VLV_DPIO_TX_C_LANES_01_POWER_DOMAINS |
6863 VLV_DPIO_TX_C_LANES_23_POWER_DOMAINS,
6864 .ops = &vlv_dpio_power_well_ops,
6865 .data = PUNIT_POWER_WELL_DPIO_TX_B_LANES_23,
6866 },
6867 {
6868 .name = "dpio-tx-c-01",
6869 .domains = VLV_DPIO_TX_B_LANES_01_POWER_DOMAINS |
6870 VLV_DPIO_TX_B_LANES_23_POWER_DOMAINS |
6871 VLV_DPIO_TX_C_LANES_01_POWER_DOMAINS |
6872 VLV_DPIO_TX_C_LANES_23_POWER_DOMAINS,
6873 .ops = &vlv_dpio_power_well_ops,
6874 .data = PUNIT_POWER_WELL_DPIO_TX_C_LANES_01,
6875 },
6876 {
6877 .name = "dpio-tx-c-23",
6878 .domains = VLV_DPIO_TX_B_LANES_01_POWER_DOMAINS |
6879 VLV_DPIO_TX_B_LANES_23_POWER_DOMAINS |
6880 VLV_DPIO_TX_C_LANES_01_POWER_DOMAINS |
6881 VLV_DPIO_TX_C_LANES_23_POWER_DOMAINS,
6882 .ops = &vlv_dpio_power_well_ops,
6883 .data = PUNIT_POWER_WELL_DPIO_TX_C_LANES_23,
6884 },
Jesse Barnesf099a3c2014-05-23 13:16:43 -07006885 {
6886 .name = "dpio-common",
6887 .domains = VLV_DPIO_CMN_BC_POWER_DOMAINS,
6888 .data = PUNIT_POWER_WELL_DPIO_CMN_BC,
Ville Syrjäläaa519f22014-06-13 13:37:55 +03006889 .ops = &vlv_dpio_cmn_power_well_ops,
Jesse Barnesf099a3c2014-05-23 13:16:43 -07006890 },
Imre Deak77961eb2014-03-05 16:20:56 +02006891};
6892
Ville Syrjälä4811ff42014-06-28 02:04:07 +03006893static struct i915_power_well chv_power_wells[] = {
6894 {
6895 .name = "always-on",
6896 .always_on = 1,
6897 .domains = VLV_ALWAYS_ON_POWER_DOMAINS,
6898 .ops = &i9xx_always_on_power_well_ops,
6899 },
Ville Syrjäläf07057d2014-06-28 02:04:10 +03006900#if 0
6901 {
6902 .name = "display",
6903 .domains = VLV_DISPLAY_POWER_DOMAINS,
6904 .data = PUNIT_POWER_WELL_DISP2D,
6905 .ops = &vlv_display_power_well_ops,
6906 },
Ville Syrjälä26972b02014-06-28 02:04:11 +03006907 {
6908 .name = "pipe-a",
6909 .domains = CHV_PIPE_A_POWER_DOMAINS,
6910 .data = PIPE_A,
6911 .ops = &chv_pipe_power_well_ops,
6912 },
6913 {
6914 .name = "pipe-b",
6915 .domains = CHV_PIPE_B_POWER_DOMAINS,
6916 .data = PIPE_B,
6917 .ops = &chv_pipe_power_well_ops,
6918 },
6919 {
6920 .name = "pipe-c",
6921 .domains = CHV_PIPE_C_POWER_DOMAINS,
6922 .data = PIPE_C,
6923 .ops = &chv_pipe_power_well_ops,
6924 },
Ville Syrjäläf07057d2014-06-28 02:04:10 +03006925#endif
Ville Syrjälä5d6f7ea2014-06-28 02:04:08 +03006926 {
6927 .name = "dpio-common-bc",
Ville Syrjälä3dd7b9742014-06-27 19:49:57 +03006928 /*
6929 * XXX: cmnreset for one PHY seems to disturb the other.
6930 * As a workaround keep both powered on at the same
6931 * time for now.
6932 */
6933 .domains = CHV_DPIO_CMN_BC_POWER_DOMAINS | CHV_DPIO_CMN_D_POWER_DOMAINS,
Ville Syrjälä5d6f7ea2014-06-28 02:04:08 +03006934 .data = PUNIT_POWER_WELL_DPIO_CMN_BC,
6935 .ops = &chv_dpio_cmn_power_well_ops,
6936 },
6937 {
6938 .name = "dpio-common-d",
Ville Syrjälä3dd7b9742014-06-27 19:49:57 +03006939 /*
6940 * XXX: cmnreset for one PHY seems to disturb the other.
6941 * As a workaround keep both powered on at the same
6942 * time for now.
6943 */
6944 .domains = CHV_DPIO_CMN_BC_POWER_DOMAINS | CHV_DPIO_CMN_D_POWER_DOMAINS,
Ville Syrjälä5d6f7ea2014-06-28 02:04:08 +03006945 .data = PUNIT_POWER_WELL_DPIO_CMN_D,
6946 .ops = &chv_dpio_cmn_power_well_ops,
6947 },
Ville Syrjälä82583562014-06-28 02:04:12 +03006948#if 0
6949 {
6950 .name = "dpio-tx-b-01",
6951 .domains = VLV_DPIO_TX_B_LANES_01_POWER_DOMAINS |
6952 VLV_DPIO_TX_B_LANES_23_POWER_DOMAINS,
6953 .ops = &vlv_dpio_power_well_ops,
6954 .data = PUNIT_POWER_WELL_DPIO_TX_B_LANES_01,
6955 },
6956 {
6957 .name = "dpio-tx-b-23",
6958 .domains = VLV_DPIO_TX_B_LANES_01_POWER_DOMAINS |
6959 VLV_DPIO_TX_B_LANES_23_POWER_DOMAINS,
6960 .ops = &vlv_dpio_power_well_ops,
6961 .data = PUNIT_POWER_WELL_DPIO_TX_B_LANES_23,
6962 },
6963 {
6964 .name = "dpio-tx-c-01",
6965 .domains = VLV_DPIO_TX_C_LANES_01_POWER_DOMAINS |
6966 VLV_DPIO_TX_C_LANES_23_POWER_DOMAINS,
6967 .ops = &vlv_dpio_power_well_ops,
6968 .data = PUNIT_POWER_WELL_DPIO_TX_C_LANES_01,
6969 },
6970 {
6971 .name = "dpio-tx-c-23",
6972 .domains = VLV_DPIO_TX_C_LANES_01_POWER_DOMAINS |
6973 VLV_DPIO_TX_C_LANES_23_POWER_DOMAINS,
6974 .ops = &vlv_dpio_power_well_ops,
6975 .data = PUNIT_POWER_WELL_DPIO_TX_C_LANES_23,
6976 },
Ville Syrjälä2ce147f2014-06-28 02:04:13 +03006977 {
6978 .name = "dpio-tx-d-01",
6979 .domains = CHV_DPIO_TX_D_LANES_01_POWER_DOMAINS |
6980 CHV_DPIO_TX_D_LANES_23_POWER_DOMAINS,
6981 .ops = &vlv_dpio_power_well_ops,
6982 .data = PUNIT_POWER_WELL_DPIO_TX_D_LANES_01,
6983 },
6984 {
6985 .name = "dpio-tx-d-23",
6986 .domains = CHV_DPIO_TX_D_LANES_01_POWER_DOMAINS |
6987 CHV_DPIO_TX_D_LANES_23_POWER_DOMAINS,
6988 .ops = &vlv_dpio_power_well_ops,
6989 .data = PUNIT_POWER_WELL_DPIO_TX_D_LANES_23,
6990 },
Ville Syrjälä82583562014-06-28 02:04:12 +03006991#endif
Ville Syrjälä4811ff42014-06-28 02:04:07 +03006992};
6993
Ville Syrjäläd2011dc2014-06-13 13:37:56 +03006994static struct i915_power_well *lookup_power_well(struct drm_i915_private *dev_priv,
6995 enum punit_power_well power_well_id)
6996{
6997 struct i915_power_domains *power_domains = &dev_priv->power_domains;
6998 struct i915_power_well *power_well;
6999 int i;
7000
7001 for_each_power_well(i, power_well, POWER_DOMAIN_MASK, power_domains) {
7002 if (power_well->data == power_well_id)
7003 return power_well;
7004 }
7005
7006 return NULL;
7007}
7008
Imre Deakc1ca7272013-11-25 17:15:29 +02007009#define set_power_wells(power_domains, __power_wells) ({ \
7010 (power_domains)->power_wells = (__power_wells); \
7011 (power_domains)->power_well_count = ARRAY_SIZE(__power_wells); \
7012})
7013
Imre Deakda7e29b2014-02-18 00:02:02 +02007014int intel_power_domains_init(struct drm_i915_private *dev_priv)
Wang Xingchaoa38911a2013-05-30 22:07:11 +08007015{
Imre Deak83c00f52013-10-25 17:36:47 +03007016 struct i915_power_domains *power_domains = &dev_priv->power_domains;
Imre Deakc1ca7272013-11-25 17:15:29 +02007017
Imre Deak83c00f52013-10-25 17:36:47 +03007018 mutex_init(&power_domains->lock);
Wang Xingchaoa38911a2013-05-30 22:07:11 +08007019
Imre Deakc1ca7272013-11-25 17:15:29 +02007020 /*
7021 * The enabling order will be from lower to higher indexed wells,
7022 * the disabling order is reversed.
7023 */
Imre Deakda7e29b2014-02-18 00:02:02 +02007024 if (IS_HASWELL(dev_priv->dev)) {
Imre Deakc1ca7272013-11-25 17:15:29 +02007025 set_power_wells(power_domains, hsw_power_wells);
7026 hsw_pwr = power_domains;
Imre Deakda7e29b2014-02-18 00:02:02 +02007027 } else if (IS_BROADWELL(dev_priv->dev)) {
Imre Deakc1ca7272013-11-25 17:15:29 +02007028 set_power_wells(power_domains, bdw_power_wells);
7029 hsw_pwr = power_domains;
Ville Syrjälä4811ff42014-06-28 02:04:07 +03007030 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
7031 set_power_wells(power_domains, chv_power_wells);
Imre Deak77961eb2014-03-05 16:20:56 +02007032 } else if (IS_VALLEYVIEW(dev_priv->dev)) {
7033 set_power_wells(power_domains, vlv_power_wells);
Imre Deakc1ca7272013-11-25 17:15:29 +02007034 } else {
Imre Deak1c2256d2013-11-25 17:15:34 +02007035 set_power_wells(power_domains, i9xx_always_on_power_well);
Imre Deakc1ca7272013-11-25 17:15:29 +02007036 }
Wang Xingchaoa38911a2013-05-30 22:07:11 +08007037
7038 return 0;
7039}
7040
Imre Deakda7e29b2014-02-18 00:02:02 +02007041void intel_power_domains_remove(struct drm_i915_private *dev_priv)
Wang Xingchaoa38911a2013-05-30 22:07:11 +08007042{
7043 hsw_pwr = NULL;
7044}
7045
Imre Deakda7e29b2014-02-18 00:02:02 +02007046static void intel_power_domains_resume(struct drm_i915_private *dev_priv)
Ville Syrjälä9cdb8262013-09-16 17:38:27 +03007047{
Imre Deak83c00f52013-10-25 17:36:47 +03007048 struct i915_power_domains *power_domains = &dev_priv->power_domains;
7049 struct i915_power_well *power_well;
Imre Deakc1ca7272013-11-25 17:15:29 +02007050 int i;
Ville Syrjälä9cdb8262013-09-16 17:38:27 +03007051
Imre Deak83c00f52013-10-25 17:36:47 +03007052 mutex_lock(&power_domains->lock);
Imre Deakbfafe932014-06-05 20:31:47 +03007053 for_each_power_well(i, power_well, POWER_DOMAIN_MASK, power_domains) {
Imre Deaka45f44662014-03-04 19:22:56 +02007054 power_well->ops->sync_hw(dev_priv, power_well);
Imre Deakbfafe932014-06-05 20:31:47 +03007055 power_well->hw_enabled = power_well->ops->is_enabled(dev_priv,
7056 power_well);
7057 }
Imre Deak83c00f52013-10-25 17:36:47 +03007058 mutex_unlock(&power_domains->lock);
Wang Xingchaoa38911a2013-05-30 22:07:11 +08007059}
7060
Ville Syrjäläd2011dc2014-06-13 13:37:56 +03007061static void vlv_cmnlane_wa(struct drm_i915_private *dev_priv)
7062{
7063 struct i915_power_well *cmn =
7064 lookup_power_well(dev_priv, PUNIT_POWER_WELL_DPIO_CMN_BC);
7065 struct i915_power_well *disp2d =
7066 lookup_power_well(dev_priv, PUNIT_POWER_WELL_DISP2D);
7067
7068 /* nothing to do if common lane is already off */
7069 if (!cmn->ops->is_enabled(dev_priv, cmn))
7070 return;
7071
7072 /* If the display might be already active skip this */
7073 if (disp2d->ops->is_enabled(dev_priv, disp2d) &&
7074 I915_READ(DPIO_CTL) & DPIO_CMNRST)
7075 return;
7076
7077 DRM_DEBUG_KMS("toggling display PHY side reset\n");
7078
7079 /* cmnlane needs DPLL registers */
7080 disp2d->ops->enable(dev_priv, disp2d);
7081
7082 /*
7083 * From VLV2A0_DP_eDP_HDMI_DPIO_driver_vbios_notes_11.docx:
7084 * Need to assert and de-assert PHY SB reset by gating the
7085 * common lane power, then un-gating it.
7086 * Simply ungating isn't enough to reset the PHY enough to get
7087 * ports and lanes running.
7088 */
7089 cmn->ops->disable(dev_priv, cmn);
7090}
7091
Imre Deakda7e29b2014-02-18 00:02:02 +02007092void intel_power_domains_init_hw(struct drm_i915_private *dev_priv)
Paulo Zanonifa42e232013-01-25 16:59:11 -02007093{
Ville Syrjäläd2011dc2014-06-13 13:37:56 +03007094 struct drm_device *dev = dev_priv->dev;
Imre Deak0d116a22014-04-25 13:19:05 +03007095 struct i915_power_domains *power_domains = &dev_priv->power_domains;
7096
7097 power_domains->initializing = true;
Ville Syrjäläd2011dc2014-06-13 13:37:56 +03007098
7099 if (IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
7100 mutex_lock(&power_domains->lock);
7101 vlv_cmnlane_wa(dev_priv);
7102 mutex_unlock(&power_domains->lock);
7103 }
7104
Paulo Zanonifa42e232013-01-25 16:59:11 -02007105 /* For now, we need the power well to be always enabled. */
Imre Deakda7e29b2014-02-18 00:02:02 +02007106 intel_display_set_init_power(dev_priv, true);
7107 intel_power_domains_resume(dev_priv);
Imre Deak0d116a22014-04-25 13:19:05 +03007108 power_domains->initializing = false;
Eugeni Dodonovd0d3e512012-05-09 15:37:16 -03007109}
7110
Paulo Zanonic67a4702013-08-19 13:18:09 -03007111void intel_aux_display_runtime_get(struct drm_i915_private *dev_priv)
7112{
Paulo Zanonid361ae22014-03-07 20:08:12 -03007113 intel_runtime_pm_get(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -03007114}
7115
7116void intel_aux_display_runtime_put(struct drm_i915_private *dev_priv)
7117{
Paulo Zanonid361ae22014-03-07 20:08:12 -03007118 intel_runtime_pm_put(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -03007119}
7120
Paulo Zanoni8a187452013-12-06 20:32:13 -02007121void intel_runtime_pm_get(struct drm_i915_private *dev_priv)
7122{
7123 struct drm_device *dev = dev_priv->dev;
7124 struct device *device = &dev->pdev->dev;
7125
7126 if (!HAS_RUNTIME_PM(dev))
7127 return;
7128
7129 pm_runtime_get_sync(device);
7130 WARN(dev_priv->pm.suspended, "Device still suspended.\n");
7131}
7132
Imre Deakc6df39b2014-04-14 20:24:29 +03007133void intel_runtime_pm_get_noresume(struct drm_i915_private *dev_priv)
7134{
7135 struct drm_device *dev = dev_priv->dev;
7136 struct device *device = &dev->pdev->dev;
7137
7138 if (!HAS_RUNTIME_PM(dev))
7139 return;
7140
7141 WARN(dev_priv->pm.suspended, "Getting nosync-ref while suspended.\n");
7142 pm_runtime_get_noresume(device);
7143}
7144
Paulo Zanoni8a187452013-12-06 20:32:13 -02007145void intel_runtime_pm_put(struct drm_i915_private *dev_priv)
7146{
7147 struct drm_device *dev = dev_priv->dev;
7148 struct device *device = &dev->pdev->dev;
7149
7150 if (!HAS_RUNTIME_PM(dev))
7151 return;
7152
7153 pm_runtime_mark_last_busy(device);
7154 pm_runtime_put_autosuspend(device);
7155}
7156
7157void intel_init_runtime_pm(struct drm_i915_private *dev_priv)
7158{
7159 struct drm_device *dev = dev_priv->dev;
7160 struct device *device = &dev->pdev->dev;
7161
Paulo Zanoni8a187452013-12-06 20:32:13 -02007162 if (!HAS_RUNTIME_PM(dev))
7163 return;
7164
7165 pm_runtime_set_active(device);
7166
Imre Deakaeab0b52014-04-14 20:24:36 +03007167 /*
7168 * RPM depends on RC6 to save restore the GT HW context, so make RC6 a
7169 * requirement.
7170 */
7171 if (!intel_enable_rc6(dev)) {
7172 DRM_INFO("RC6 disabled, disabling runtime PM support\n");
7173 return;
7174 }
7175
Paulo Zanoni8a187452013-12-06 20:32:13 -02007176 pm_runtime_set_autosuspend_delay(device, 10000); /* 10s */
7177 pm_runtime_mark_last_busy(device);
7178 pm_runtime_use_autosuspend(device);
Paulo Zanoniba0239e2014-03-07 20:08:07 -03007179
7180 pm_runtime_put_autosuspend(device);
Paulo Zanoni8a187452013-12-06 20:32:13 -02007181}
7182
7183void intel_fini_runtime_pm(struct drm_i915_private *dev_priv)
7184{
7185 struct drm_device *dev = dev_priv->dev;
7186 struct device *device = &dev->pdev->dev;
7187
7188 if (!HAS_RUNTIME_PM(dev))
7189 return;
7190
Imre Deakaeab0b52014-04-14 20:24:36 +03007191 if (!intel_enable_rc6(dev))
7192 return;
7193
Paulo Zanoni8a187452013-12-06 20:32:13 -02007194 /* Make sure we're not suspended first. */
7195 pm_runtime_get_sync(device);
7196 pm_runtime_disable(device);
7197}
7198
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007199/* Set up chip specific power management-related functions */
7200void intel_init_pm(struct drm_device *dev)
7201{
7202 struct drm_i915_private *dev_priv = dev->dev_private;
7203
Daniel Vetter3a77c4c2014-01-10 08:50:12 +01007204 if (HAS_FBC(dev)) {
Ville Syrjälä40045462013-11-28 17:29:59 +02007205 if (INTEL_INFO(dev)->gen >= 7) {
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007206 dev_priv->display.fbc_enabled = ironlake_fbc_enabled;
Ville Syrjälä40045462013-11-28 17:29:59 +02007207 dev_priv->display.enable_fbc = gen7_enable_fbc;
7208 dev_priv->display.disable_fbc = ironlake_disable_fbc;
7209 } else if (INTEL_INFO(dev)->gen >= 5) {
7210 dev_priv->display.fbc_enabled = ironlake_fbc_enabled;
7211 dev_priv->display.enable_fbc = ironlake_enable_fbc;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007212 dev_priv->display.disable_fbc = ironlake_disable_fbc;
7213 } else if (IS_GM45(dev)) {
7214 dev_priv->display.fbc_enabled = g4x_fbc_enabled;
7215 dev_priv->display.enable_fbc = g4x_enable_fbc;
7216 dev_priv->display.disable_fbc = g4x_disable_fbc;
Ville Syrjälä40045462013-11-28 17:29:59 +02007217 } else {
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007218 dev_priv->display.fbc_enabled = i8xx_fbc_enabled;
7219 dev_priv->display.enable_fbc = i8xx_enable_fbc;
7220 dev_priv->display.disable_fbc = i8xx_disable_fbc;
Ville Syrjälä993495a2013-12-12 17:27:40 +02007221
7222 /* This value was pulled out of someone's hat */
7223 I915_WRITE(FBC_CONTROL, 500 << FBC_CTL_INTERVAL_SHIFT);
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007224 }
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007225 }
7226
Daniel Vetterc921aba2012-04-26 23:28:17 +02007227 /* For cxsr */
7228 if (IS_PINEVIEW(dev))
7229 i915_pineview_get_mem_freq(dev);
7230 else if (IS_GEN5(dev))
7231 i915_ironlake_get_mem_freq(dev);
7232
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007233 /* For FIFO watermark updates */
7234 if (HAS_PCH_SPLIT(dev)) {
Damien Lespiaufa50ad62014-03-17 18:01:16 +00007235 ilk_setup_wm_latency(dev);
Ville Syrjälä53615a52013-08-01 16:18:50 +03007236
Ville Syrjäläbd602542014-01-07 16:14:10 +02007237 if ((IS_GEN5(dev) && dev_priv->wm.pri_latency[1] &&
7238 dev_priv->wm.spr_latency[1] && dev_priv->wm.cur_latency[1]) ||
7239 (!IS_GEN5(dev) && dev_priv->wm.pri_latency[0] &&
7240 dev_priv->wm.spr_latency[0] && dev_priv->wm.cur_latency[0])) {
7241 dev_priv->display.update_wm = ilk_update_wm;
7242 dev_priv->display.update_sprite_wm = ilk_update_sprite_wm;
7243 } else {
7244 DRM_DEBUG_KMS("Failed to read display plane latency. "
7245 "Disable CxSR\n");
7246 }
7247
7248 if (IS_GEN5(dev))
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007249 dev_priv->display.init_clock_gating = ironlake_init_clock_gating;
Ville Syrjäläbd602542014-01-07 16:14:10 +02007250 else if (IS_GEN6(dev))
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007251 dev_priv->display.init_clock_gating = gen6_init_clock_gating;
Ville Syrjäläbd602542014-01-07 16:14:10 +02007252 else if (IS_IVYBRIDGE(dev))
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007253 dev_priv->display.init_clock_gating = ivybridge_init_clock_gating;
Ville Syrjäläbd602542014-01-07 16:14:10 +02007254 else if (IS_HASWELL(dev))
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03007255 dev_priv->display.init_clock_gating = haswell_init_clock_gating;
Ville Syrjäläbd602542014-01-07 16:14:10 +02007256 else if (INTEL_INFO(dev)->gen == 8)
Ben Widawsky1020a5c2013-11-02 21:07:06 -07007257 dev_priv->display.init_clock_gating = gen8_init_clock_gating;
Ville Syrjäläa4565da2014-04-09 13:28:10 +03007258 } else if (IS_CHERRYVIEW(dev)) {
Ville Syrjälä3c2777f2014-06-26 17:03:06 +03007259 dev_priv->display.update_wm = cherryview_update_wm;
Gajanan Bhat01e184c2014-08-07 17:03:30 +05307260 dev_priv->display.update_sprite_wm = valleyview_update_sprite_wm;
Ville Syrjäläa4565da2014-04-09 13:28:10 +03007261 dev_priv->display.init_clock_gating =
7262 cherryview_init_clock_gating;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007263 } else if (IS_VALLEYVIEW(dev)) {
7264 dev_priv->display.update_wm = valleyview_update_wm;
Gajanan Bhat01e184c2014-08-07 17:03:30 +05307265 dev_priv->display.update_sprite_wm = valleyview_update_sprite_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007266 dev_priv->display.init_clock_gating =
7267 valleyview_init_clock_gating;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007268 } else if (IS_PINEVIEW(dev)) {
7269 if (!intel_get_cxsr_latency(IS_PINEVIEW_G(dev),
7270 dev_priv->is_ddr3,
7271 dev_priv->fsb_freq,
7272 dev_priv->mem_freq)) {
7273 DRM_INFO("failed to find known CxSR latency "
7274 "(found ddr%s fsb freq %d, mem freq %d), "
7275 "disabling CxSR\n",
7276 (dev_priv->is_ddr3 == 1) ? "3" : "2",
7277 dev_priv->fsb_freq, dev_priv->mem_freq);
7278 /* Disable CxSR and never update its watermark again */
Imre Deak5209b1f2014-07-01 12:36:17 +03007279 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007280 dev_priv->display.update_wm = NULL;
7281 } else
7282 dev_priv->display.update_wm = pineview_update_wm;
7283 dev_priv->display.init_clock_gating = gen3_init_clock_gating;
7284 } else if (IS_G4X(dev)) {
7285 dev_priv->display.update_wm = g4x_update_wm;
7286 dev_priv->display.init_clock_gating = g4x_init_clock_gating;
7287 } else if (IS_GEN4(dev)) {
7288 dev_priv->display.update_wm = i965_update_wm;
7289 if (IS_CRESTLINE(dev))
7290 dev_priv->display.init_clock_gating = crestline_init_clock_gating;
7291 else if (IS_BROADWATER(dev))
7292 dev_priv->display.init_clock_gating = broadwater_init_clock_gating;
7293 } else if (IS_GEN3(dev)) {
7294 dev_priv->display.update_wm = i9xx_update_wm;
7295 dev_priv->display.get_fifo_size = i9xx_get_fifo_size;
7296 dev_priv->display.init_clock_gating = gen3_init_clock_gating;
Daniel Vetterfeb56b92013-12-14 20:38:30 -02007297 } else if (IS_GEN2(dev)) {
7298 if (INTEL_INFO(dev)->num_pipes == 1) {
7299 dev_priv->display.update_wm = i845_update_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007300 dev_priv->display.get_fifo_size = i845_get_fifo_size;
Daniel Vetterfeb56b92013-12-14 20:38:30 -02007301 } else {
7302 dev_priv->display.update_wm = i9xx_update_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007303 dev_priv->display.get_fifo_size = i830_get_fifo_size;
Daniel Vetterfeb56b92013-12-14 20:38:30 -02007304 }
7305
7306 if (IS_I85X(dev) || IS_I865G(dev))
7307 dev_priv->display.init_clock_gating = i85x_init_clock_gating;
7308 else
7309 dev_priv->display.init_clock_gating = i830_init_clock_gating;
7310 } else {
7311 DRM_ERROR("unexpected fall-through in intel_init_pm\n");
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007312 }
7313}
7314
Ben Widawsky42c05262012-09-26 10:34:00 -07007315int sandybridge_pcode_read(struct drm_i915_private *dev_priv, u8 mbox, u32 *val)
7316{
Jesse Barnes4fc688c2012-11-02 11:14:01 -07007317 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Ben Widawsky42c05262012-09-26 10:34:00 -07007318
7319 if (I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) {
7320 DRM_DEBUG_DRIVER("warning: pcode (read) mailbox access failed\n");
7321 return -EAGAIN;
7322 }
7323
7324 I915_WRITE(GEN6_PCODE_DATA, *val);
7325 I915_WRITE(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY | mbox);
7326
7327 if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
7328 500)) {
7329 DRM_ERROR("timeout waiting for pcode read (%d) to finish\n", mbox);
7330 return -ETIMEDOUT;
7331 }
7332
7333 *val = I915_READ(GEN6_PCODE_DATA);
7334 I915_WRITE(GEN6_PCODE_DATA, 0);
7335
7336 return 0;
7337}
7338
7339int sandybridge_pcode_write(struct drm_i915_private *dev_priv, u8 mbox, u32 val)
7340{
Jesse Barnes4fc688c2012-11-02 11:14:01 -07007341 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Ben Widawsky42c05262012-09-26 10:34:00 -07007342
7343 if (I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) {
7344 DRM_DEBUG_DRIVER("warning: pcode (write) mailbox access failed\n");
7345 return -EAGAIN;
7346 }
7347
7348 I915_WRITE(GEN6_PCODE_DATA, val);
7349 I915_WRITE(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY | mbox);
7350
7351 if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
7352 500)) {
7353 DRM_ERROR("timeout waiting for pcode write (%d) to finish\n", mbox);
7354 return -ETIMEDOUT;
7355 }
7356
7357 I915_WRITE(GEN6_PCODE_DATA, 0);
7358
7359 return 0;
7360}
Jesse Barnesa0e4e192013-04-02 11:23:05 -07007361
Fengguang Wub55dd642014-07-12 11:21:39 +02007362static int byt_gpu_freq(struct drm_i915_private *dev_priv, int val)
Jesse Barnes855ba3b2013-04-17 15:54:57 -07007363{
Ville Syrjälä07ab1182013-11-05 22:42:28 +02007364 int div;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07007365
Ville Syrjälä07ab1182013-11-05 22:42:28 +02007366 /* 4 x czclk */
Ville Syrjälä2ec38152013-11-05 22:42:29 +02007367 switch (dev_priv->mem_freq) {
Jesse Barnes855ba3b2013-04-17 15:54:57 -07007368 case 800:
Ville Syrjälä07ab1182013-11-05 22:42:28 +02007369 div = 10;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07007370 break;
7371 case 1066:
Ville Syrjälä07ab1182013-11-05 22:42:28 +02007372 div = 12;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07007373 break;
7374 case 1333:
Ville Syrjälä07ab1182013-11-05 22:42:28 +02007375 div = 16;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07007376 break;
7377 default:
7378 return -1;
7379 }
7380
Ville Syrjälä2ec38152013-11-05 22:42:29 +02007381 return DIV_ROUND_CLOSEST(dev_priv->mem_freq * (val + 6 - 0xbd), 4 * div);
Jesse Barnes855ba3b2013-04-17 15:54:57 -07007382}
7383
Fengguang Wub55dd642014-07-12 11:21:39 +02007384static int byt_freq_opcode(struct drm_i915_private *dev_priv, int val)
Jesse Barnes855ba3b2013-04-17 15:54:57 -07007385{
Ville Syrjälä07ab1182013-11-05 22:42:28 +02007386 int mul;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07007387
Ville Syrjälä07ab1182013-11-05 22:42:28 +02007388 /* 4 x czclk */
Ville Syrjälä2ec38152013-11-05 22:42:29 +02007389 switch (dev_priv->mem_freq) {
Jesse Barnes855ba3b2013-04-17 15:54:57 -07007390 case 800:
Ville Syrjälä07ab1182013-11-05 22:42:28 +02007391 mul = 10;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07007392 break;
7393 case 1066:
Ville Syrjälä07ab1182013-11-05 22:42:28 +02007394 mul = 12;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07007395 break;
7396 case 1333:
Ville Syrjälä07ab1182013-11-05 22:42:28 +02007397 mul = 16;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07007398 break;
7399 default:
7400 return -1;
7401 }
7402
Ville Syrjälä2ec38152013-11-05 22:42:29 +02007403 return DIV_ROUND_CLOSEST(4 * mul * val, dev_priv->mem_freq) + 0xbd - 6;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07007404}
7405
Fengguang Wub55dd642014-07-12 11:21:39 +02007406static int chv_gpu_freq(struct drm_i915_private *dev_priv, int val)
Deepak S22b1b2f2014-07-12 14:54:33 +05307407{
7408 int div, freq;
7409
7410 switch (dev_priv->rps.cz_freq) {
7411 case 200:
7412 div = 5;
7413 break;
7414 case 267:
7415 div = 6;
7416 break;
7417 case 320:
7418 case 333:
7419 case 400:
7420 div = 8;
7421 break;
7422 default:
7423 return -1;
7424 }
7425
7426 freq = (DIV_ROUND_CLOSEST((dev_priv->rps.cz_freq * val), 2 * div) / 2);
7427
7428 return freq;
7429}
7430
Fengguang Wub55dd642014-07-12 11:21:39 +02007431static int chv_freq_opcode(struct drm_i915_private *dev_priv, int val)
Deepak S22b1b2f2014-07-12 14:54:33 +05307432{
7433 int mul, opcode;
7434
7435 switch (dev_priv->rps.cz_freq) {
7436 case 200:
7437 mul = 5;
7438 break;
7439 case 267:
7440 mul = 6;
7441 break;
7442 case 320:
7443 case 333:
7444 case 400:
7445 mul = 8;
7446 break;
7447 default:
7448 return -1;
7449 }
7450
7451 opcode = (DIV_ROUND_CLOSEST((val * 2 * mul), dev_priv->rps.cz_freq) * 2);
7452
7453 return opcode;
7454}
7455
7456int vlv_gpu_freq(struct drm_i915_private *dev_priv, int val)
7457{
7458 int ret = -1;
7459
7460 if (IS_CHERRYVIEW(dev_priv->dev))
7461 ret = chv_gpu_freq(dev_priv, val);
7462 else if (IS_VALLEYVIEW(dev_priv->dev))
7463 ret = byt_gpu_freq(dev_priv, val);
7464
7465 return ret;
7466}
7467
7468int vlv_freq_opcode(struct drm_i915_private *dev_priv, int val)
7469{
7470 int ret = -1;
7471
7472 if (IS_CHERRYVIEW(dev_priv->dev))
7473 ret = chv_freq_opcode(dev_priv, val);
7474 else if (IS_VALLEYVIEW(dev_priv->dev))
7475 ret = byt_freq_opcode(dev_priv, val);
7476
7477 return ret;
7478}
7479
Daniel Vetterf742a552013-12-06 10:17:53 +01007480void intel_pm_setup(struct drm_device *dev)
Chris Wilson907b28c2013-07-19 20:36:52 +01007481{
7482 struct drm_i915_private *dev_priv = dev->dev_private;
7483
Daniel Vetterf742a552013-12-06 10:17:53 +01007484 mutex_init(&dev_priv->rps.hw_lock);
7485
Chris Wilson907b28c2013-07-19 20:36:52 +01007486 INIT_DELAYED_WORK(&dev_priv->rps.delayed_resume_work,
7487 intel_gen6_powersave_work);
Paulo Zanoni5d584b22014-03-07 20:08:15 -03007488
Paulo Zanoni33688d92014-03-07 20:08:19 -03007489 dev_priv->pm.suspended = false;
Jesse Barnes9df7575f2014-06-20 09:29:20 -07007490 dev_priv->pm._irqs_disabled = false;
Chris Wilson907b28c2013-07-19 20:36:52 +01007491}