blob: ded429459342a18a028561e9d8699cc2124fb99a [file] [log] [blame]
Eugeni Dodonov85208be2012-04-16 22:20:34 -03001/*
2 * Copyright © 2012 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eugeni Dodonov <eugeni.dodonov@intel.com>
25 *
26 */
27
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -030028#include <linux/cpufreq.h>
Eugeni Dodonov85208be2012-04-16 22:20:34 -030029#include "i915_drv.h"
30#include "intel_drv.h"
Daniel Vettereb48eb02012-04-26 23:28:12 +020031#include "../../../platform/x86/intel_ips.h"
32#include <linux/module.h>
Paulo Zanonif9dcb0d2013-12-11 18:50:10 -020033#include <linux/vgaarb.h>
Damien Lespiauf4db9322013-06-24 22:59:50 +010034#include <drm/i915_powerwell.h>
Paulo Zanoni8a187452013-12-06 20:32:13 -020035#include <linux/pm_runtime.h>
Eugeni Dodonov85208be2012-04-16 22:20:34 -030036
Ben Widawskydc39fff2013-10-18 12:32:07 -070037/**
38 * RC6 is a special power stage which allows the GPU to enter an very
39 * low-voltage mode when idle, using down to 0V while at this stage. This
40 * stage is entered automatically when the GPU is idle when RC6 support is
41 * enabled, and as soon as new workload arises GPU wakes up automatically as well.
42 *
43 * There are different RC6 modes available in Intel GPU, which differentiate
44 * among each other with the latency required to enter and leave RC6 and
45 * voltage consumed by the GPU in different states.
46 *
47 * The combination of the following flags define which states GPU is allowed
48 * to enter, while RC6 is the normal RC6 state, RC6p is the deep RC6, and
49 * RC6pp is deepest RC6. Their support by hardware varies according to the
50 * GPU, BIOS, chipset and platform. RC6 is usually the safest one and the one
51 * which brings the most power savings; deeper states save more power, but
52 * require higher latency to switch to and wake up.
53 */
54#define INTEL_RC6_ENABLE (1<<0)
55#define INTEL_RC6p_ENABLE (1<<1)
56#define INTEL_RC6pp_ENABLE (1<<2)
57
Eugeni Dodonovf6750b32012-04-18 11:51:14 -030058/* FBC, or Frame Buffer Compression, is a technique employed to compress the
59 * framebuffer contents in-memory, aiming at reducing the required bandwidth
60 * during in-memory transfers and, therefore, reduce the power packet.
Eugeni Dodonov85208be2012-04-16 22:20:34 -030061 *
Eugeni Dodonovf6750b32012-04-18 11:51:14 -030062 * The benefits of FBC are mostly visible with solid backgrounds and
63 * variation-less patterns.
Eugeni Dodonov85208be2012-04-16 22:20:34 -030064 *
Eugeni Dodonovf6750b32012-04-18 11:51:14 -030065 * FBC-related functionality can be enabled by the means of the
66 * i915.i915_enable_fbc parameter
Eugeni Dodonov85208be2012-04-16 22:20:34 -030067 */
68
Eugeni Dodonov1fa61102012-04-18 15:29:26 -030069static void i8xx_disable_fbc(struct drm_device *dev)
Eugeni Dodonov85208be2012-04-16 22:20:34 -030070{
71 struct drm_i915_private *dev_priv = dev->dev_private;
72 u32 fbc_ctl;
73
74 /* Disable compression */
75 fbc_ctl = I915_READ(FBC_CONTROL);
76 if ((fbc_ctl & FBC_CTL_EN) == 0)
77 return;
78
79 fbc_ctl &= ~FBC_CTL_EN;
80 I915_WRITE(FBC_CONTROL, fbc_ctl);
81
82 /* Wait for compressing bit to clear */
83 if (wait_for((I915_READ(FBC_STATUS) & FBC_STAT_COMPRESSING) == 0, 10)) {
84 DRM_DEBUG_KMS("FBC idle timed out\n");
85 return;
86 }
87
88 DRM_DEBUG_KMS("disabled FBC\n");
89}
90
Ville Syrjälä993495a2013-12-12 17:27:40 +020091static void i8xx_enable_fbc(struct drm_crtc *crtc)
Eugeni Dodonov85208be2012-04-16 22:20:34 -030092{
93 struct drm_device *dev = crtc->dev;
94 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roperf4510a22014-04-01 15:22:40 -070095 struct drm_framebuffer *fb = crtc->primary->fb;
Eugeni Dodonov85208be2012-04-16 22:20:34 -030096 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
97 struct drm_i915_gem_object *obj = intel_fb->obj;
98 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
99 int cfb_pitch;
Ville Syrjälä7f2cf222014-01-23 16:49:11 +0200100 int i;
Ville Syrjälä159f9872013-11-28 17:29:57 +0200101 u32 fbc_ctl;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300102
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -0700103 cfb_pitch = dev_priv->fbc.size / FBC_LL_SIZE;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300104 if (fb->pitches[0] < cfb_pitch)
105 cfb_pitch = fb->pitches[0];
106
Ville Syrjälä42a430f2013-11-28 17:29:56 +0200107 /* FBC_CTL wants 32B or 64B units */
108 if (IS_GEN2(dev))
109 cfb_pitch = (cfb_pitch / 32) - 1;
110 else
111 cfb_pitch = (cfb_pitch / 64) - 1;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300112
113 /* Clear old tags */
114 for (i = 0; i < (FBC_LL_SIZE / 32) + 1; i++)
115 I915_WRITE(FBC_TAG + (i * 4), 0);
116
Ville Syrjälä159f9872013-11-28 17:29:57 +0200117 if (IS_GEN4(dev)) {
118 u32 fbc_ctl2;
119
120 /* Set it up... */
121 fbc_ctl2 = FBC_CTL_FENCE_DBL | FBC_CTL_IDLE_IMM | FBC_CTL_CPU_FENCE;
Ville Syrjälä7f2cf222014-01-23 16:49:11 +0200122 fbc_ctl2 |= FBC_CTL_PLANE(intel_crtc->plane);
Ville Syrjälä159f9872013-11-28 17:29:57 +0200123 I915_WRITE(FBC_CONTROL2, fbc_ctl2);
124 I915_WRITE(FBC_FENCE_OFF, crtc->y);
125 }
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300126
127 /* enable it... */
Ville Syrjälä993495a2013-12-12 17:27:40 +0200128 fbc_ctl = I915_READ(FBC_CONTROL);
129 fbc_ctl &= 0x3fff << FBC_CTL_INTERVAL_SHIFT;
130 fbc_ctl |= FBC_CTL_EN | FBC_CTL_PERIODIC;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300131 if (IS_I945GM(dev))
132 fbc_ctl |= FBC_CTL_C3_IDLE; /* 945 needs special SR handling */
133 fbc_ctl |= (cfb_pitch & 0xff) << FBC_CTL_STRIDE_SHIFT;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300134 fbc_ctl |= obj->fence_reg;
135 I915_WRITE(FBC_CONTROL, fbc_ctl);
136
Ville Syrjälä5cd54102014-01-23 16:49:16 +0200137 DRM_DEBUG_KMS("enabled FBC, pitch %d, yoff %d, plane %c\n",
Ville Syrjälä84f44ce2013-04-17 17:48:49 +0300138 cfb_pitch, crtc->y, plane_name(intel_crtc->plane));
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300139}
140
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300141static bool i8xx_fbc_enabled(struct drm_device *dev)
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300142{
143 struct drm_i915_private *dev_priv = dev->dev_private;
144
145 return I915_READ(FBC_CONTROL) & FBC_CTL_EN;
146}
147
Ville Syrjälä993495a2013-12-12 17:27:40 +0200148static void g4x_enable_fbc(struct drm_crtc *crtc)
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300149{
150 struct drm_device *dev = crtc->dev;
151 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roperf4510a22014-04-01 15:22:40 -0700152 struct drm_framebuffer *fb = crtc->primary->fb;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300153 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
154 struct drm_i915_gem_object *obj = intel_fb->obj;
155 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300156 u32 dpfc_ctl;
157
Ville Syrjälä3fa2e0e2014-01-23 16:49:12 +0200158 dpfc_ctl = DPFC_CTL_PLANE(intel_crtc->plane) | DPFC_SR_EN;
159 if (drm_format_plane_cpp(fb->pixel_format, 0) == 2)
160 dpfc_ctl |= DPFC_CTL_LIMIT_2X;
161 else
162 dpfc_ctl |= DPFC_CTL_LIMIT_1X;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300163 dpfc_ctl |= DPFC_CTL_FENCE_EN | obj->fence_reg;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300164
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300165 I915_WRITE(DPFC_FENCE_YOFF, crtc->y);
166
167 /* enable it... */
Ville Syrjäläfe74c1a2014-01-23 16:49:13 +0200168 I915_WRITE(DPFC_CONTROL, dpfc_ctl | DPFC_CTL_EN);
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300169
Ville Syrjälä84f44ce2013-04-17 17:48:49 +0300170 DRM_DEBUG_KMS("enabled fbc on plane %c\n", plane_name(intel_crtc->plane));
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300171}
172
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300173static void g4x_disable_fbc(struct drm_device *dev)
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300174{
175 struct drm_i915_private *dev_priv = dev->dev_private;
176 u32 dpfc_ctl;
177
178 /* Disable compression */
179 dpfc_ctl = I915_READ(DPFC_CONTROL);
180 if (dpfc_ctl & DPFC_CTL_EN) {
181 dpfc_ctl &= ~DPFC_CTL_EN;
182 I915_WRITE(DPFC_CONTROL, dpfc_ctl);
183
184 DRM_DEBUG_KMS("disabled FBC\n");
185 }
186}
187
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300188static bool g4x_fbc_enabled(struct drm_device *dev)
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300189{
190 struct drm_i915_private *dev_priv = dev->dev_private;
191
192 return I915_READ(DPFC_CONTROL) & DPFC_CTL_EN;
193}
194
195static void sandybridge_blit_fbc_update(struct drm_device *dev)
196{
197 struct drm_i915_private *dev_priv = dev->dev_private;
198 u32 blt_ecoskpd;
199
200 /* Make sure blitter notifies FBC of writes */
Deepak S940aece2013-11-23 14:55:43 +0530201
202 /* Blitter is part of Media powerwell on VLV. No impact of
203 * his param in other platforms for now */
204 gen6_gt_force_wake_get(dev_priv, FORCEWAKE_MEDIA);
Deepak Sc8d9a592013-11-23 14:55:42 +0530205
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300206 blt_ecoskpd = I915_READ(GEN6_BLITTER_ECOSKPD);
207 blt_ecoskpd |= GEN6_BLITTER_FBC_NOTIFY <<
208 GEN6_BLITTER_LOCK_SHIFT;
209 I915_WRITE(GEN6_BLITTER_ECOSKPD, blt_ecoskpd);
210 blt_ecoskpd |= GEN6_BLITTER_FBC_NOTIFY;
211 I915_WRITE(GEN6_BLITTER_ECOSKPD, blt_ecoskpd);
212 blt_ecoskpd &= ~(GEN6_BLITTER_FBC_NOTIFY <<
213 GEN6_BLITTER_LOCK_SHIFT);
214 I915_WRITE(GEN6_BLITTER_ECOSKPD, blt_ecoskpd);
215 POSTING_READ(GEN6_BLITTER_ECOSKPD);
Deepak Sc8d9a592013-11-23 14:55:42 +0530216
Deepak S940aece2013-11-23 14:55:43 +0530217 gen6_gt_force_wake_put(dev_priv, FORCEWAKE_MEDIA);
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300218}
219
Ville Syrjälä993495a2013-12-12 17:27:40 +0200220static void ironlake_enable_fbc(struct drm_crtc *crtc)
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300221{
222 struct drm_device *dev = crtc->dev;
223 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roperf4510a22014-04-01 15:22:40 -0700224 struct drm_framebuffer *fb = crtc->primary->fb;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300225 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
226 struct drm_i915_gem_object *obj = intel_fb->obj;
227 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300228 u32 dpfc_ctl;
229
Ville Syrjälä46f3dab2014-01-23 16:49:14 +0200230 dpfc_ctl = DPFC_CTL_PLANE(intel_crtc->plane);
Ville Syrjälä3fa2e0e2014-01-23 16:49:12 +0200231 if (drm_format_plane_cpp(fb->pixel_format, 0) == 2)
Ben Widawsky5e59f712014-06-30 10:41:24 -0700232 dev_priv->fbc.threshold++;
233
234 switch (dev_priv->fbc.threshold) {
235 case 4:
236 case 3:
237 dpfc_ctl |= DPFC_CTL_LIMIT_4X;
238 break;
239 case 2:
Ville Syrjälä3fa2e0e2014-01-23 16:49:12 +0200240 dpfc_ctl |= DPFC_CTL_LIMIT_2X;
Ben Widawsky5e59f712014-06-30 10:41:24 -0700241 break;
242 case 1:
Ville Syrjälä3fa2e0e2014-01-23 16:49:12 +0200243 dpfc_ctl |= DPFC_CTL_LIMIT_1X;
Ben Widawsky5e59f712014-06-30 10:41:24 -0700244 break;
245 }
Ville Syrjäläd6293362013-11-21 21:29:45 +0200246 dpfc_ctl |= DPFC_CTL_FENCE_EN;
247 if (IS_GEN5(dev))
248 dpfc_ctl |= obj->fence_reg;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300249
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300250 I915_WRITE(ILK_DPFC_FENCE_YOFF, crtc->y);
Ben Widawskyf343c5f2013-07-05 14:41:04 -0700251 I915_WRITE(ILK_FBC_RT_BASE, i915_gem_obj_ggtt_offset(obj) | ILK_FBC_RT_VALID);
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300252 /* enable it... */
253 I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl | DPFC_CTL_EN);
254
255 if (IS_GEN6(dev)) {
256 I915_WRITE(SNB_DPFC_CTL_SA,
257 SNB_CPU_FENCE_ENABLE | obj->fence_reg);
258 I915_WRITE(DPFC_CPU_FENCE_OFFSET, crtc->y);
259 sandybridge_blit_fbc_update(dev);
260 }
261
Ville Syrjälä84f44ce2013-04-17 17:48:49 +0300262 DRM_DEBUG_KMS("enabled fbc on plane %c\n", plane_name(intel_crtc->plane));
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300263}
264
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300265static void ironlake_disable_fbc(struct drm_device *dev)
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300266{
267 struct drm_i915_private *dev_priv = dev->dev_private;
268 u32 dpfc_ctl;
269
270 /* Disable compression */
271 dpfc_ctl = I915_READ(ILK_DPFC_CONTROL);
272 if (dpfc_ctl & DPFC_CTL_EN) {
273 dpfc_ctl &= ~DPFC_CTL_EN;
274 I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl);
275
276 DRM_DEBUG_KMS("disabled FBC\n");
277 }
278}
279
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300280static bool ironlake_fbc_enabled(struct drm_device *dev)
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300281{
282 struct drm_i915_private *dev_priv = dev->dev_private;
283
284 return I915_READ(ILK_DPFC_CONTROL) & DPFC_CTL_EN;
285}
286
Ville Syrjälä993495a2013-12-12 17:27:40 +0200287static void gen7_enable_fbc(struct drm_crtc *crtc)
Rodrigo Viviabe959c2013-05-06 19:37:33 -0300288{
289 struct drm_device *dev = crtc->dev;
290 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roperf4510a22014-04-01 15:22:40 -0700291 struct drm_framebuffer *fb = crtc->primary->fb;
Rodrigo Viviabe959c2013-05-06 19:37:33 -0300292 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
293 struct drm_i915_gem_object *obj = intel_fb->obj;
294 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjälä3fa2e0e2014-01-23 16:49:12 +0200295 u32 dpfc_ctl;
Rodrigo Viviabe959c2013-05-06 19:37:33 -0300296
Ville Syrjälä3fa2e0e2014-01-23 16:49:12 +0200297 dpfc_ctl = IVB_DPFC_CTL_PLANE(intel_crtc->plane);
298 if (drm_format_plane_cpp(fb->pixel_format, 0) == 2)
Ben Widawsky5e59f712014-06-30 10:41:24 -0700299 dev_priv->fbc.threshold++;
300
301 switch (dev_priv->fbc.threshold) {
302 case 4:
303 case 3:
304 dpfc_ctl |= DPFC_CTL_LIMIT_4X;
305 break;
306 case 2:
Ville Syrjälä3fa2e0e2014-01-23 16:49:12 +0200307 dpfc_ctl |= DPFC_CTL_LIMIT_2X;
Ben Widawsky5e59f712014-06-30 10:41:24 -0700308 break;
309 case 1:
Ville Syrjälä3fa2e0e2014-01-23 16:49:12 +0200310 dpfc_ctl |= DPFC_CTL_LIMIT_1X;
Ben Widawsky5e59f712014-06-30 10:41:24 -0700311 break;
312 }
313
Ville Syrjälä3fa2e0e2014-01-23 16:49:12 +0200314 dpfc_ctl |= IVB_DPFC_CTL_FENCE_EN;
315
316 I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl | DPFC_CTL_EN);
Rodrigo Viviabe959c2013-05-06 19:37:33 -0300317
Rodrigo Vivi891348b2013-05-06 19:37:36 -0300318 if (IS_IVYBRIDGE(dev)) {
Damien Lespiau7dd23ba2013-05-10 14:33:17 +0100319 /* WaFbcAsynchFlipDisableFbcQueue:ivb */
Ville Syrjälä2adb6db2014-03-05 13:05:46 +0200320 I915_WRITE(ILK_DISPLAY_CHICKEN1,
321 I915_READ(ILK_DISPLAY_CHICKEN1) |
322 ILK_FBCQ_DIS);
Rodrigo Vivi28554162013-05-06 19:37:37 -0300323 } else {
Ville Syrjälä2adb6db2014-03-05 13:05:46 +0200324 /* WaFbcAsynchFlipDisableFbcQueue:hsw,bdw */
Ville Syrjälä8f670bb2014-03-05 13:05:47 +0200325 I915_WRITE(CHICKEN_PIPESL_1(intel_crtc->pipe),
326 I915_READ(CHICKEN_PIPESL_1(intel_crtc->pipe)) |
327 HSW_FBCQ_DIS);
Rodrigo Vivi891348b2013-05-06 19:37:36 -0300328 }
Rodrigo Vivib74ea102013-05-09 14:08:38 -0300329
Rodrigo Viviabe959c2013-05-06 19:37:33 -0300330 I915_WRITE(SNB_DPFC_CTL_SA,
331 SNB_CPU_FENCE_ENABLE | obj->fence_reg);
332 I915_WRITE(DPFC_CPU_FENCE_OFFSET, crtc->y);
333
334 sandybridge_blit_fbc_update(dev);
335
Ville Syrjäläb19870e2013-11-06 23:02:25 +0200336 DRM_DEBUG_KMS("enabled fbc on plane %c\n", plane_name(intel_crtc->plane));
Rodrigo Viviabe959c2013-05-06 19:37:33 -0300337}
338
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300339bool intel_fbc_enabled(struct drm_device *dev)
340{
341 struct drm_i915_private *dev_priv = dev->dev_private;
342
343 if (!dev_priv->display.fbc_enabled)
344 return false;
345
346 return dev_priv->display.fbc_enabled(dev);
347}
348
349static void intel_fbc_work_fn(struct work_struct *__work)
350{
351 struct intel_fbc_work *work =
352 container_of(to_delayed_work(__work),
353 struct intel_fbc_work, work);
354 struct drm_device *dev = work->crtc->dev;
355 struct drm_i915_private *dev_priv = dev->dev_private;
356
357 mutex_lock(&dev->struct_mutex);
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -0700358 if (work == dev_priv->fbc.fbc_work) {
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300359 /* Double check that we haven't switched fb without cancelling
360 * the prior work.
361 */
Matt Roperf4510a22014-04-01 15:22:40 -0700362 if (work->crtc->primary->fb == work->fb) {
Ville Syrjälä993495a2013-12-12 17:27:40 +0200363 dev_priv->display.enable_fbc(work->crtc);
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300364
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -0700365 dev_priv->fbc.plane = to_intel_crtc(work->crtc)->plane;
Matt Roperf4510a22014-04-01 15:22:40 -0700366 dev_priv->fbc.fb_id = work->crtc->primary->fb->base.id;
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -0700367 dev_priv->fbc.y = work->crtc->y;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300368 }
369
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -0700370 dev_priv->fbc.fbc_work = NULL;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300371 }
372 mutex_unlock(&dev->struct_mutex);
373
374 kfree(work);
375}
376
377static void intel_cancel_fbc_work(struct drm_i915_private *dev_priv)
378{
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -0700379 if (dev_priv->fbc.fbc_work == NULL)
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300380 return;
381
382 DRM_DEBUG_KMS("cancelling pending FBC enable\n");
383
384 /* Synchronisation is provided by struct_mutex and checking of
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -0700385 * dev_priv->fbc.fbc_work, so we can perform the cancellation
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300386 * entirely asynchronously.
387 */
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -0700388 if (cancel_delayed_work(&dev_priv->fbc.fbc_work->work))
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300389 /* tasklet was killed before being run, clean up */
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -0700390 kfree(dev_priv->fbc.fbc_work);
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300391
392 /* Mark the work as no longer wanted so that if it does
393 * wake-up (because the work was already running and waiting
394 * for our mutex), it will discover that is no longer
395 * necessary to run.
396 */
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -0700397 dev_priv->fbc.fbc_work = NULL;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300398}
399
Ville Syrjälä993495a2013-12-12 17:27:40 +0200400static void intel_enable_fbc(struct drm_crtc *crtc)
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300401{
402 struct intel_fbc_work *work;
403 struct drm_device *dev = crtc->dev;
404 struct drm_i915_private *dev_priv = dev->dev_private;
405
406 if (!dev_priv->display.enable_fbc)
407 return;
408
409 intel_cancel_fbc_work(dev_priv);
410
Daniel Vetterb14c5672013-09-19 12:18:32 +0200411 work = kzalloc(sizeof(*work), GFP_KERNEL);
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300412 if (work == NULL) {
Paulo Zanoni6cdcb5e2013-06-12 17:27:29 -0300413 DRM_ERROR("Failed to allocate FBC work structure\n");
Ville Syrjälä993495a2013-12-12 17:27:40 +0200414 dev_priv->display.enable_fbc(crtc);
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300415 return;
416 }
417
418 work->crtc = crtc;
Matt Roperf4510a22014-04-01 15:22:40 -0700419 work->fb = crtc->primary->fb;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300420 INIT_DELAYED_WORK(&work->work, intel_fbc_work_fn);
421
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -0700422 dev_priv->fbc.fbc_work = work;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300423
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300424 /* Delay the actual enabling to let pageflipping cease and the
425 * display to settle before starting the compression. Note that
426 * this delay also serves a second purpose: it allows for a
427 * vblank to pass after disabling the FBC before we attempt
428 * to modify the control registers.
429 *
430 * A more complicated solution would involve tracking vblanks
431 * following the termination of the page-flipping sequence
432 * and indeed performing the enable as a co-routine and not
433 * waiting synchronously upon the vblank.
Damien Lespiau7457d612013-06-07 17:41:07 +0100434 *
435 * WaFbcWaitForVBlankBeforeEnable:ilk,snb
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300436 */
437 schedule_delayed_work(&work->work, msecs_to_jiffies(50));
438}
439
440void intel_disable_fbc(struct drm_device *dev)
441{
442 struct drm_i915_private *dev_priv = dev->dev_private;
443
444 intel_cancel_fbc_work(dev_priv);
445
446 if (!dev_priv->display.disable_fbc)
447 return;
448
449 dev_priv->display.disable_fbc(dev);
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -0700450 dev_priv->fbc.plane = -1;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300451}
452
Chris Wilson29ebf902013-07-27 17:23:55 +0100453static bool set_no_fbc_reason(struct drm_i915_private *dev_priv,
454 enum no_fbc_reason reason)
455{
456 if (dev_priv->fbc.no_fbc_reason == reason)
457 return false;
458
459 dev_priv->fbc.no_fbc_reason = reason;
460 return true;
461}
462
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300463/**
464 * intel_update_fbc - enable/disable FBC as needed
465 * @dev: the drm_device
466 *
467 * Set up the framebuffer compression hardware at mode set time. We
468 * enable it if possible:
469 * - plane A only (on pre-965)
470 * - no pixel mulitply/line duplication
471 * - no alpha buffer discard
472 * - no dual wide
Paulo Zanonif85da862013-06-04 16:53:39 -0300473 * - framebuffer <= max_hdisplay in width, max_vdisplay in height
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300474 *
475 * We can't assume that any compression will take place (worst case),
476 * so the compressed buffer has to be the same size as the uncompressed
477 * one. It also must reside (along with the line length buffer) in
478 * stolen memory.
479 *
480 * We need to enable/disable FBC on a global basis.
481 */
482void intel_update_fbc(struct drm_device *dev)
483{
484 struct drm_i915_private *dev_priv = dev->dev_private;
485 struct drm_crtc *crtc = NULL, *tmp_crtc;
486 struct intel_crtc *intel_crtc;
487 struct drm_framebuffer *fb;
488 struct intel_framebuffer *intel_fb;
489 struct drm_i915_gem_object *obj;
Ville Syrjäläef644fd2013-09-04 18:25:21 +0300490 const struct drm_display_mode *adjusted_mode;
Ville Syrjälä37327ab2013-09-04 18:25:28 +0300491 unsigned int max_width, max_height;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300492
Daniel Vetter3a77c4c2014-01-10 08:50:12 +0100493 if (!HAS_FBC(dev)) {
Chris Wilson29ebf902013-07-27 17:23:55 +0100494 set_no_fbc_reason(dev_priv, FBC_UNSUPPORTED);
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300495 return;
Chris Wilson29ebf902013-07-27 17:23:55 +0100496 }
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300497
Jani Nikulad330a952014-01-21 11:24:25 +0200498 if (!i915.powersave) {
Chris Wilson29ebf902013-07-27 17:23:55 +0100499 if (set_no_fbc_reason(dev_priv, FBC_MODULE_PARAM))
500 DRM_DEBUG_KMS("fbc disabled per module param\n");
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300501 return;
Chris Wilson29ebf902013-07-27 17:23:55 +0100502 }
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300503
504 /*
505 * If FBC is already on, we just have to verify that we can
506 * keep it that way...
507 * Need to disable if:
508 * - more than one pipe is active
509 * - changing FBC params (stride, fence, mode)
510 * - new fb is too large to fit in compressed buffer
511 * - going to an unsupported config (interlace, pixel multiply, etc.)
512 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +0100513 for_each_crtc(dev, tmp_crtc) {
Chris Wilson3490ea52013-01-07 10:11:40 +0000514 if (intel_crtc_active(tmp_crtc) &&
Ville Syrjälä4c445e02013-10-09 17:24:58 +0300515 to_intel_crtc(tmp_crtc)->primary_enabled) {
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300516 if (crtc) {
Chris Wilson29ebf902013-07-27 17:23:55 +0100517 if (set_no_fbc_reason(dev_priv, FBC_MULTIPLE_PIPES))
518 DRM_DEBUG_KMS("more than one pipe active, disabling compression\n");
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300519 goto out_disable;
520 }
521 crtc = tmp_crtc;
522 }
523 }
524
Matt Roperf4510a22014-04-01 15:22:40 -0700525 if (!crtc || crtc->primary->fb == NULL) {
Chris Wilson29ebf902013-07-27 17:23:55 +0100526 if (set_no_fbc_reason(dev_priv, FBC_NO_OUTPUT))
527 DRM_DEBUG_KMS("no output, disabling\n");
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300528 goto out_disable;
529 }
530
531 intel_crtc = to_intel_crtc(crtc);
Matt Roperf4510a22014-04-01 15:22:40 -0700532 fb = crtc->primary->fb;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300533 intel_fb = to_intel_framebuffer(fb);
534 obj = intel_fb->obj;
Ville Syrjäläef644fd2013-09-04 18:25:21 +0300535 adjusted_mode = &intel_crtc->config.adjusted_mode;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300536
Chris Wilson03689202014-06-06 10:37:11 +0100537 if (i915.enable_fbc < 0) {
Chris Wilson29ebf902013-07-27 17:23:55 +0100538 if (set_no_fbc_reason(dev_priv, FBC_CHIP_DEFAULT))
539 DRM_DEBUG_KMS("disabled per chip default\n");
Damien Lespiau8a5729a2013-06-24 16:22:02 +0100540 goto out_disable;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300541 }
Jani Nikulad330a952014-01-21 11:24:25 +0200542 if (!i915.enable_fbc) {
Chris Wilson29ebf902013-07-27 17:23:55 +0100543 if (set_no_fbc_reason(dev_priv, FBC_MODULE_PARAM))
544 DRM_DEBUG_KMS("fbc disabled per module param\n");
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300545 goto out_disable;
546 }
Ville Syrjäläef644fd2013-09-04 18:25:21 +0300547 if ((adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) ||
548 (adjusted_mode->flags & DRM_MODE_FLAG_DBLSCAN)) {
Chris Wilson29ebf902013-07-27 17:23:55 +0100549 if (set_no_fbc_reason(dev_priv, FBC_UNSUPPORTED_MODE))
550 DRM_DEBUG_KMS("mode incompatible with compression, "
551 "disabling\n");
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300552 goto out_disable;
553 }
Paulo Zanonif85da862013-06-04 16:53:39 -0300554
Daisy Sun032843a2014-06-16 15:48:18 -0700555 if (INTEL_INFO(dev)->gen >= 8 || IS_HASWELL(dev)) {
556 max_width = 4096;
557 max_height = 4096;
558 } else if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
Ville Syrjälä37327ab2013-09-04 18:25:28 +0300559 max_width = 4096;
560 max_height = 2048;
Paulo Zanonif85da862013-06-04 16:53:39 -0300561 } else {
Ville Syrjälä37327ab2013-09-04 18:25:28 +0300562 max_width = 2048;
563 max_height = 1536;
Paulo Zanonif85da862013-06-04 16:53:39 -0300564 }
Ville Syrjälä37327ab2013-09-04 18:25:28 +0300565 if (intel_crtc->config.pipe_src_w > max_width ||
566 intel_crtc->config.pipe_src_h > max_height) {
Chris Wilson29ebf902013-07-27 17:23:55 +0100567 if (set_no_fbc_reason(dev_priv, FBC_MODE_TOO_LARGE))
568 DRM_DEBUG_KMS("mode too large for compression, disabling\n");
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300569 goto out_disable;
570 }
Ben Widawsky8f94d242014-02-20 16:01:20 -0800571 if ((INTEL_INFO(dev)->gen < 4 || HAS_DDI(dev)) &&
Ville Syrjäläc5a44aa2013-11-28 17:29:58 +0200572 intel_crtc->plane != PLANE_A) {
Chris Wilson29ebf902013-07-27 17:23:55 +0100573 if (set_no_fbc_reason(dev_priv, FBC_BAD_PLANE))
Ville Syrjäläc5a44aa2013-11-28 17:29:58 +0200574 DRM_DEBUG_KMS("plane not A, disabling compression\n");
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300575 goto out_disable;
576 }
577
578 /* The use of a CPU fence is mandatory in order to detect writes
579 * by the CPU to the scanout and trigger updates to the FBC.
580 */
581 if (obj->tiling_mode != I915_TILING_X ||
582 obj->fence_reg == I915_FENCE_REG_NONE) {
Chris Wilson29ebf902013-07-27 17:23:55 +0100583 if (set_no_fbc_reason(dev_priv, FBC_NOT_TILED))
584 DRM_DEBUG_KMS("framebuffer not tiled or fenced, disabling compression\n");
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300585 goto out_disable;
586 }
587
588 /* If the kernel debugger is active, always disable compression */
589 if (in_dbg_master())
590 goto out_disable;
591
Ben Widawsky5e59f712014-06-30 10:41:24 -0700592 if (i915_gem_stolen_setup_compression(dev, intel_fb->obj->base.size,
593 drm_format_plane_cpp(fb->pixel_format, 0))) {
Chris Wilson29ebf902013-07-27 17:23:55 +0100594 if (set_no_fbc_reason(dev_priv, FBC_STOLEN_TOO_SMALL))
595 DRM_DEBUG_KMS("framebuffer too large, disabling compression\n");
Chris Wilson11be49e2012-11-15 11:32:20 +0000596 goto out_disable;
597 }
598
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300599 /* If the scanout has not changed, don't modify the FBC settings.
600 * Note that we make the fundamental assumption that the fb->obj
601 * cannot be unpinned (and have its GTT offset and fence revoked)
602 * without first being decoupled from the scanout and FBC disabled.
603 */
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -0700604 if (dev_priv->fbc.plane == intel_crtc->plane &&
605 dev_priv->fbc.fb_id == fb->base.id &&
606 dev_priv->fbc.y == crtc->y)
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300607 return;
608
609 if (intel_fbc_enabled(dev)) {
610 /* We update FBC along two paths, after changing fb/crtc
611 * configuration (modeswitching) and after page-flipping
612 * finishes. For the latter, we know that not only did
613 * we disable the FBC at the start of the page-flip
614 * sequence, but also more than one vblank has passed.
615 *
616 * For the former case of modeswitching, it is possible
617 * to switch between two FBC valid configurations
618 * instantaneously so we do need to disable the FBC
619 * before we can modify its control registers. We also
620 * have to wait for the next vblank for that to take
621 * effect. However, since we delay enabling FBC we can
622 * assume that a vblank has passed since disabling and
623 * that we can safely alter the registers in the deferred
624 * callback.
625 *
626 * In the scenario that we go from a valid to invalid
627 * and then back to valid FBC configuration we have
628 * no strict enforcement that a vblank occurred since
629 * disabling the FBC. However, along all current pipe
630 * disabling paths we do need to wait for a vblank at
631 * some point. And we wait before enabling FBC anyway.
632 */
633 DRM_DEBUG_KMS("disabling active FBC for update\n");
634 intel_disable_fbc(dev);
635 }
636
Ville Syrjälä993495a2013-12-12 17:27:40 +0200637 intel_enable_fbc(crtc);
Chris Wilson29ebf902013-07-27 17:23:55 +0100638 dev_priv->fbc.no_fbc_reason = FBC_OK;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300639 return;
640
641out_disable:
642 /* Multiple disables should be harmless */
643 if (intel_fbc_enabled(dev)) {
644 DRM_DEBUG_KMS("unsupported config, disabling FBC\n");
645 intel_disable_fbc(dev);
646 }
Chris Wilson11be49e2012-11-15 11:32:20 +0000647 i915_gem_stolen_cleanup_compression(dev);
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300648}
649
Daniel Vetterc921aba2012-04-26 23:28:17 +0200650static void i915_pineview_get_mem_freq(struct drm_device *dev)
651{
Jani Nikula50227e12014-03-31 14:27:21 +0300652 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200653 u32 tmp;
654
655 tmp = I915_READ(CLKCFG);
656
657 switch (tmp & CLKCFG_FSB_MASK) {
658 case CLKCFG_FSB_533:
659 dev_priv->fsb_freq = 533; /* 133*4 */
660 break;
661 case CLKCFG_FSB_800:
662 dev_priv->fsb_freq = 800; /* 200*4 */
663 break;
664 case CLKCFG_FSB_667:
665 dev_priv->fsb_freq = 667; /* 167*4 */
666 break;
667 case CLKCFG_FSB_400:
668 dev_priv->fsb_freq = 400; /* 100*4 */
669 break;
670 }
671
672 switch (tmp & CLKCFG_MEM_MASK) {
673 case CLKCFG_MEM_533:
674 dev_priv->mem_freq = 533;
675 break;
676 case CLKCFG_MEM_667:
677 dev_priv->mem_freq = 667;
678 break;
679 case CLKCFG_MEM_800:
680 dev_priv->mem_freq = 800;
681 break;
682 }
683
684 /* detect pineview DDR3 setting */
685 tmp = I915_READ(CSHRDDR3CTL);
686 dev_priv->is_ddr3 = (tmp & CSHRDDR3CTL_DDR3) ? 1 : 0;
687}
688
689static void i915_ironlake_get_mem_freq(struct drm_device *dev)
690{
Jani Nikula50227e12014-03-31 14:27:21 +0300691 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200692 u16 ddrpll, csipll;
693
694 ddrpll = I915_READ16(DDRMPLL1);
695 csipll = I915_READ16(CSIPLL0);
696
697 switch (ddrpll & 0xff) {
698 case 0xc:
699 dev_priv->mem_freq = 800;
700 break;
701 case 0x10:
702 dev_priv->mem_freq = 1066;
703 break;
704 case 0x14:
705 dev_priv->mem_freq = 1333;
706 break;
707 case 0x18:
708 dev_priv->mem_freq = 1600;
709 break;
710 default:
711 DRM_DEBUG_DRIVER("unknown memory frequency 0x%02x\n",
712 ddrpll & 0xff);
713 dev_priv->mem_freq = 0;
714 break;
715 }
716
Daniel Vetter20e4d402012-08-08 23:35:39 +0200717 dev_priv->ips.r_t = dev_priv->mem_freq;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200718
719 switch (csipll & 0x3ff) {
720 case 0x00c:
721 dev_priv->fsb_freq = 3200;
722 break;
723 case 0x00e:
724 dev_priv->fsb_freq = 3733;
725 break;
726 case 0x010:
727 dev_priv->fsb_freq = 4266;
728 break;
729 case 0x012:
730 dev_priv->fsb_freq = 4800;
731 break;
732 case 0x014:
733 dev_priv->fsb_freq = 5333;
734 break;
735 case 0x016:
736 dev_priv->fsb_freq = 5866;
737 break;
738 case 0x018:
739 dev_priv->fsb_freq = 6400;
740 break;
741 default:
742 DRM_DEBUG_DRIVER("unknown fsb frequency 0x%04x\n",
743 csipll & 0x3ff);
744 dev_priv->fsb_freq = 0;
745 break;
746 }
747
748 if (dev_priv->fsb_freq == 3200) {
Daniel Vetter20e4d402012-08-08 23:35:39 +0200749 dev_priv->ips.c_m = 0;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200750 } else if (dev_priv->fsb_freq > 3200 && dev_priv->fsb_freq <= 4800) {
Daniel Vetter20e4d402012-08-08 23:35:39 +0200751 dev_priv->ips.c_m = 1;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200752 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +0200753 dev_priv->ips.c_m = 2;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200754 }
755}
756
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300757static const struct cxsr_latency cxsr_latency_table[] = {
758 {1, 0, 800, 400, 3382, 33382, 3983, 33983}, /* DDR2-400 SC */
759 {1, 0, 800, 667, 3354, 33354, 3807, 33807}, /* DDR2-667 SC */
760 {1, 0, 800, 800, 3347, 33347, 3763, 33763}, /* DDR2-800 SC */
761 {1, 1, 800, 667, 6420, 36420, 6873, 36873}, /* DDR3-667 SC */
762 {1, 1, 800, 800, 5902, 35902, 6318, 36318}, /* DDR3-800 SC */
763
764 {1, 0, 667, 400, 3400, 33400, 4021, 34021}, /* DDR2-400 SC */
765 {1, 0, 667, 667, 3372, 33372, 3845, 33845}, /* DDR2-667 SC */
766 {1, 0, 667, 800, 3386, 33386, 3822, 33822}, /* DDR2-800 SC */
767 {1, 1, 667, 667, 6438, 36438, 6911, 36911}, /* DDR3-667 SC */
768 {1, 1, 667, 800, 5941, 35941, 6377, 36377}, /* DDR3-800 SC */
769
770 {1, 0, 400, 400, 3472, 33472, 4173, 34173}, /* DDR2-400 SC */
771 {1, 0, 400, 667, 3443, 33443, 3996, 33996}, /* DDR2-667 SC */
772 {1, 0, 400, 800, 3430, 33430, 3946, 33946}, /* DDR2-800 SC */
773 {1, 1, 400, 667, 6509, 36509, 7062, 37062}, /* DDR3-667 SC */
774 {1, 1, 400, 800, 5985, 35985, 6501, 36501}, /* DDR3-800 SC */
775
776 {0, 0, 800, 400, 3438, 33438, 4065, 34065}, /* DDR2-400 SC */
777 {0, 0, 800, 667, 3410, 33410, 3889, 33889}, /* DDR2-667 SC */
778 {0, 0, 800, 800, 3403, 33403, 3845, 33845}, /* DDR2-800 SC */
779 {0, 1, 800, 667, 6476, 36476, 6955, 36955}, /* DDR3-667 SC */
780 {0, 1, 800, 800, 5958, 35958, 6400, 36400}, /* DDR3-800 SC */
781
782 {0, 0, 667, 400, 3456, 33456, 4103, 34106}, /* DDR2-400 SC */
783 {0, 0, 667, 667, 3428, 33428, 3927, 33927}, /* DDR2-667 SC */
784 {0, 0, 667, 800, 3443, 33443, 3905, 33905}, /* DDR2-800 SC */
785 {0, 1, 667, 667, 6494, 36494, 6993, 36993}, /* DDR3-667 SC */
786 {0, 1, 667, 800, 5998, 35998, 6460, 36460}, /* DDR3-800 SC */
787
788 {0, 0, 400, 400, 3528, 33528, 4255, 34255}, /* DDR2-400 SC */
789 {0, 0, 400, 667, 3500, 33500, 4079, 34079}, /* DDR2-667 SC */
790 {0, 0, 400, 800, 3487, 33487, 4029, 34029}, /* DDR2-800 SC */
791 {0, 1, 400, 667, 6566, 36566, 7145, 37145}, /* DDR3-667 SC */
792 {0, 1, 400, 800, 6042, 36042, 6584, 36584}, /* DDR3-800 SC */
793};
794
Daniel Vetter63c62272012-04-21 23:17:55 +0200795static const struct cxsr_latency *intel_get_cxsr_latency(int is_desktop,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300796 int is_ddr3,
797 int fsb,
798 int mem)
799{
800 const struct cxsr_latency *latency;
801 int i;
802
803 if (fsb == 0 || mem == 0)
804 return NULL;
805
806 for (i = 0; i < ARRAY_SIZE(cxsr_latency_table); i++) {
807 latency = &cxsr_latency_table[i];
808 if (is_desktop == latency->is_desktop &&
809 is_ddr3 == latency->is_ddr3 &&
810 fsb == latency->fsb_freq && mem == latency->mem_freq)
811 return latency;
812 }
813
814 DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
815
816 return NULL;
817}
818
Imre Deak5209b1f2014-07-01 12:36:17 +0300819void intel_set_memory_cxsr(struct drm_i915_private *dev_priv, bool enable)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300820{
Imre Deak5209b1f2014-07-01 12:36:17 +0300821 struct drm_device *dev = dev_priv->dev;
822 u32 val;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300823
Imre Deak5209b1f2014-07-01 12:36:17 +0300824 if (IS_VALLEYVIEW(dev)) {
825 I915_WRITE(FW_BLC_SELF_VLV, enable ? FW_CSPWRDWNEN : 0);
826 } else if (IS_G4X(dev) || IS_CRESTLINE(dev)) {
827 I915_WRITE(FW_BLC_SELF, enable ? FW_BLC_SELF_EN : 0);
828 } else if (IS_PINEVIEW(dev)) {
829 val = I915_READ(DSPFW3) & ~PINEVIEW_SELF_REFRESH_EN;
830 val |= enable ? PINEVIEW_SELF_REFRESH_EN : 0;
831 I915_WRITE(DSPFW3, val);
832 } else if (IS_I945G(dev) || IS_I945GM(dev)) {
833 val = enable ? _MASKED_BIT_ENABLE(FW_BLC_SELF_EN) :
834 _MASKED_BIT_DISABLE(FW_BLC_SELF_EN);
835 I915_WRITE(FW_BLC_SELF, val);
836 } else if (IS_I915GM(dev)) {
837 val = enable ? _MASKED_BIT_ENABLE(INSTPM_SELF_EN) :
838 _MASKED_BIT_DISABLE(INSTPM_SELF_EN);
839 I915_WRITE(INSTPM, val);
840 } else {
841 return;
842 }
843
844 DRM_DEBUG_KMS("memory self-refresh is %s\n",
845 enable ? "enabled" : "disabled");
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300846}
847
848/*
849 * Latency for FIFO fetches is dependent on several factors:
850 * - memory configuration (speed, channels)
851 * - chipset
852 * - current MCH state
853 * It can be fairly high in some situations, so here we assume a fairly
854 * pessimal value. It's a tradeoff between extra memory fetches (if we
855 * set this value too high, the FIFO will fetch frequently to stay full)
856 * and power consumption (set it too low to save power and we might see
857 * FIFO underruns and display "flicker").
858 *
859 * A value of 5us seems to be a good balance; safe for very low end
860 * platforms but not overly aggressive on lower latency configs.
861 */
862static const int latency_ns = 5000;
863
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300864static int i9xx_get_fifo_size(struct drm_device *dev, int plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300865{
866 struct drm_i915_private *dev_priv = dev->dev_private;
867 uint32_t dsparb = I915_READ(DSPARB);
868 int size;
869
870 size = dsparb & 0x7f;
871 if (plane)
872 size = ((dsparb >> DSPARB_CSTART_SHIFT) & 0x7f) - size;
873
874 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
875 plane ? "B" : "A", size);
876
877 return size;
878}
879
Daniel Vetterfeb56b92013-12-14 20:38:30 -0200880static int i830_get_fifo_size(struct drm_device *dev, int plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300881{
882 struct drm_i915_private *dev_priv = dev->dev_private;
883 uint32_t dsparb = I915_READ(DSPARB);
884 int size;
885
886 size = dsparb & 0x1ff;
887 if (plane)
888 size = ((dsparb >> DSPARB_BEND_SHIFT) & 0x1ff) - size;
889 size >>= 1; /* Convert to cachelines */
890
891 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
892 plane ? "B" : "A", size);
893
894 return size;
895}
896
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300897static int i845_get_fifo_size(struct drm_device *dev, int plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300898{
899 struct drm_i915_private *dev_priv = dev->dev_private;
900 uint32_t dsparb = I915_READ(DSPARB);
901 int size;
902
903 size = dsparb & 0x7f;
904 size >>= 2; /* Convert to cachelines */
905
906 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
907 plane ? "B" : "A",
908 size);
909
910 return size;
911}
912
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300913/* Pineview has different values for various configs */
914static const struct intel_watermark_params pineview_display_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300915 .fifo_size = PINEVIEW_DISPLAY_FIFO,
916 .max_wm = PINEVIEW_MAX_WM,
917 .default_wm = PINEVIEW_DFT_WM,
918 .guard_size = PINEVIEW_GUARD_WM,
919 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300920};
921static const struct intel_watermark_params pineview_display_hplloff_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300922 .fifo_size = PINEVIEW_DISPLAY_FIFO,
923 .max_wm = PINEVIEW_MAX_WM,
924 .default_wm = PINEVIEW_DFT_HPLLOFF_WM,
925 .guard_size = PINEVIEW_GUARD_WM,
926 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300927};
928static const struct intel_watermark_params pineview_cursor_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300929 .fifo_size = PINEVIEW_CURSOR_FIFO,
930 .max_wm = PINEVIEW_CURSOR_MAX_WM,
931 .default_wm = PINEVIEW_CURSOR_DFT_WM,
932 .guard_size = PINEVIEW_CURSOR_GUARD_WM,
933 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300934};
935static const struct intel_watermark_params pineview_cursor_hplloff_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300936 .fifo_size = PINEVIEW_CURSOR_FIFO,
937 .max_wm = PINEVIEW_CURSOR_MAX_WM,
938 .default_wm = PINEVIEW_CURSOR_DFT_WM,
939 .guard_size = PINEVIEW_CURSOR_GUARD_WM,
940 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300941};
942static const struct intel_watermark_params g4x_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300943 .fifo_size = G4X_FIFO_SIZE,
944 .max_wm = G4X_MAX_WM,
945 .default_wm = G4X_MAX_WM,
946 .guard_size = 2,
947 .cacheline_size = G4X_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300948};
949static const struct intel_watermark_params g4x_cursor_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300950 .fifo_size = I965_CURSOR_FIFO,
951 .max_wm = I965_CURSOR_MAX_WM,
952 .default_wm = I965_CURSOR_DFT_WM,
953 .guard_size = 2,
954 .cacheline_size = G4X_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300955};
956static const struct intel_watermark_params valleyview_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300957 .fifo_size = VALLEYVIEW_FIFO_SIZE,
958 .max_wm = VALLEYVIEW_MAX_WM,
959 .default_wm = VALLEYVIEW_MAX_WM,
960 .guard_size = 2,
961 .cacheline_size = G4X_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300962};
963static const struct intel_watermark_params valleyview_cursor_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300964 .fifo_size = I965_CURSOR_FIFO,
965 .max_wm = VALLEYVIEW_CURSOR_MAX_WM,
966 .default_wm = I965_CURSOR_DFT_WM,
967 .guard_size = 2,
968 .cacheline_size = G4X_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300969};
970static const struct intel_watermark_params i965_cursor_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300971 .fifo_size = I965_CURSOR_FIFO,
972 .max_wm = I965_CURSOR_MAX_WM,
973 .default_wm = I965_CURSOR_DFT_WM,
974 .guard_size = 2,
975 .cacheline_size = I915_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300976};
977static const struct intel_watermark_params i945_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300978 .fifo_size = I945_FIFO_SIZE,
979 .max_wm = I915_MAX_WM,
980 .default_wm = 1,
981 .guard_size = 2,
982 .cacheline_size = I915_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300983};
984static const struct intel_watermark_params i915_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300985 .fifo_size = I915_FIFO_SIZE,
986 .max_wm = I915_MAX_WM,
987 .default_wm = 1,
988 .guard_size = 2,
989 .cacheline_size = I915_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300990};
Daniel Vetterfeb56b92013-12-14 20:38:30 -0200991static const struct intel_watermark_params i830_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300992 .fifo_size = I855GM_FIFO_SIZE,
993 .max_wm = I915_MAX_WM,
994 .default_wm = 1,
995 .guard_size = 2,
996 .cacheline_size = I830_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300997};
Daniel Vetterfeb56b92013-12-14 20:38:30 -0200998static const struct intel_watermark_params i845_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300999 .fifo_size = I830_FIFO_SIZE,
1000 .max_wm = I915_MAX_WM,
1001 .default_wm = 1,
1002 .guard_size = 2,
1003 .cacheline_size = I830_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001004};
1005
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001006/**
1007 * intel_calculate_wm - calculate watermark level
1008 * @clock_in_khz: pixel clock
1009 * @wm: chip FIFO params
1010 * @pixel_size: display pixel size
1011 * @latency_ns: memory latency for the platform
1012 *
1013 * Calculate the watermark level (the level at which the display plane will
1014 * start fetching from memory again). Each chip has a different display
1015 * FIFO size and allocation, so the caller needs to figure that out and pass
1016 * in the correct intel_watermark_params structure.
1017 *
1018 * As the pixel clock runs, the FIFO will be drained at a rate that depends
1019 * on the pixel size. When it reaches the watermark level, it'll start
1020 * fetching FIFO line sized based chunks from memory until the FIFO fills
1021 * past the watermark point. If the FIFO drains completely, a FIFO underrun
1022 * will occur, and a display engine hang could result.
1023 */
1024static unsigned long intel_calculate_wm(unsigned long clock_in_khz,
1025 const struct intel_watermark_params *wm,
1026 int fifo_size,
1027 int pixel_size,
1028 unsigned long latency_ns)
1029{
1030 long entries_required, wm_size;
1031
1032 /*
1033 * Note: we need to make sure we don't overflow for various clock &
1034 * latency values.
1035 * clocks go from a few thousand to several hundred thousand.
1036 * latency is usually a few thousand
1037 */
1038 entries_required = ((clock_in_khz / 1000) * pixel_size * latency_ns) /
1039 1000;
1040 entries_required = DIV_ROUND_UP(entries_required, wm->cacheline_size);
1041
1042 DRM_DEBUG_KMS("FIFO entries required for mode: %ld\n", entries_required);
1043
1044 wm_size = fifo_size - (entries_required + wm->guard_size);
1045
1046 DRM_DEBUG_KMS("FIFO watermark level: %ld\n", wm_size);
1047
1048 /* Don't promote wm_size to unsigned... */
1049 if (wm_size > (long)wm->max_wm)
1050 wm_size = wm->max_wm;
1051 if (wm_size <= 0)
1052 wm_size = wm->default_wm;
1053 return wm_size;
1054}
1055
1056static struct drm_crtc *single_enabled_crtc(struct drm_device *dev)
1057{
1058 struct drm_crtc *crtc, *enabled = NULL;
1059
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01001060 for_each_crtc(dev, crtc) {
Chris Wilson3490ea52013-01-07 10:11:40 +00001061 if (intel_crtc_active(crtc)) {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001062 if (enabled)
1063 return NULL;
1064 enabled = crtc;
1065 }
1066 }
1067
1068 return enabled;
1069}
1070
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001071static void pineview_update_wm(struct drm_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001072{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001073 struct drm_device *dev = unused_crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001074 struct drm_i915_private *dev_priv = dev->dev_private;
1075 struct drm_crtc *crtc;
1076 const struct cxsr_latency *latency;
1077 u32 reg;
1078 unsigned long wm;
1079
1080 latency = intel_get_cxsr_latency(IS_PINEVIEW_G(dev), dev_priv->is_ddr3,
1081 dev_priv->fsb_freq, dev_priv->mem_freq);
1082 if (!latency) {
1083 DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
Imre Deak5209b1f2014-07-01 12:36:17 +03001084 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001085 return;
1086 }
1087
1088 crtc = single_enabled_crtc(dev);
1089 if (crtc) {
Damien Lespiau241bfc32013-09-25 16:45:37 +01001090 const struct drm_display_mode *adjusted_mode;
Matt Roperf4510a22014-04-01 15:22:40 -07001091 int pixel_size = crtc->primary->fb->bits_per_pixel / 8;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001092 int clock;
1093
1094 adjusted_mode = &to_intel_crtc(crtc)->config.adjusted_mode;
1095 clock = adjusted_mode->crtc_clock;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001096
1097 /* Display SR */
1098 wm = intel_calculate_wm(clock, &pineview_display_wm,
1099 pineview_display_wm.fifo_size,
1100 pixel_size, latency->display_sr);
1101 reg = I915_READ(DSPFW1);
1102 reg &= ~DSPFW_SR_MASK;
1103 reg |= wm << DSPFW_SR_SHIFT;
1104 I915_WRITE(DSPFW1, reg);
1105 DRM_DEBUG_KMS("DSPFW1 register is %x\n", reg);
1106
1107 /* cursor SR */
1108 wm = intel_calculate_wm(clock, &pineview_cursor_wm,
1109 pineview_display_wm.fifo_size,
1110 pixel_size, latency->cursor_sr);
1111 reg = I915_READ(DSPFW3);
1112 reg &= ~DSPFW_CURSOR_SR_MASK;
1113 reg |= (wm & 0x3f) << DSPFW_CURSOR_SR_SHIFT;
1114 I915_WRITE(DSPFW3, reg);
1115
1116 /* Display HPLL off SR */
1117 wm = intel_calculate_wm(clock, &pineview_display_hplloff_wm,
1118 pineview_display_hplloff_wm.fifo_size,
1119 pixel_size, latency->display_hpll_disable);
1120 reg = I915_READ(DSPFW3);
1121 reg &= ~DSPFW_HPLL_SR_MASK;
1122 reg |= wm & DSPFW_HPLL_SR_MASK;
1123 I915_WRITE(DSPFW3, reg);
1124
1125 /* cursor HPLL off SR */
1126 wm = intel_calculate_wm(clock, &pineview_cursor_hplloff_wm,
1127 pineview_display_hplloff_wm.fifo_size,
1128 pixel_size, latency->cursor_hpll_disable);
1129 reg = I915_READ(DSPFW3);
1130 reg &= ~DSPFW_HPLL_CURSOR_MASK;
1131 reg |= (wm & 0x3f) << DSPFW_HPLL_CURSOR_SHIFT;
1132 I915_WRITE(DSPFW3, reg);
1133 DRM_DEBUG_KMS("DSPFW3 register is %x\n", reg);
1134
Imre Deak5209b1f2014-07-01 12:36:17 +03001135 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001136 } else {
Imre Deak5209b1f2014-07-01 12:36:17 +03001137 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001138 }
1139}
1140
1141static bool g4x_compute_wm0(struct drm_device *dev,
1142 int plane,
1143 const struct intel_watermark_params *display,
1144 int display_latency_ns,
1145 const struct intel_watermark_params *cursor,
1146 int cursor_latency_ns,
1147 int *plane_wm,
1148 int *cursor_wm)
1149{
1150 struct drm_crtc *crtc;
Ville Syrjälä4fe85902013-09-04 18:25:22 +03001151 const struct drm_display_mode *adjusted_mode;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001152 int htotal, hdisplay, clock, pixel_size;
1153 int line_time_us, line_count;
1154 int entries, tlb_miss;
1155
1156 crtc = intel_get_crtc_for_plane(dev, plane);
Chris Wilson3490ea52013-01-07 10:11:40 +00001157 if (!intel_crtc_active(crtc)) {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001158 *cursor_wm = cursor->guard_size;
1159 *plane_wm = display->guard_size;
1160 return false;
1161 }
1162
Ville Syrjälä4fe85902013-09-04 18:25:22 +03001163 adjusted_mode = &to_intel_crtc(crtc)->config.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001164 clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -08001165 htotal = adjusted_mode->crtc_htotal;
Ville Syrjälä37327ab2013-09-04 18:25:28 +03001166 hdisplay = to_intel_crtc(crtc)->config.pipe_src_w;
Matt Roperf4510a22014-04-01 15:22:40 -07001167 pixel_size = crtc->primary->fb->bits_per_pixel / 8;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001168
1169 /* Use the small buffer method to calculate plane watermark */
1170 entries = ((clock * pixel_size / 1000) * display_latency_ns) / 1000;
1171 tlb_miss = display->fifo_size*display->cacheline_size - hdisplay * 8;
1172 if (tlb_miss > 0)
1173 entries += tlb_miss;
1174 entries = DIV_ROUND_UP(entries, display->cacheline_size);
1175 *plane_wm = entries + display->guard_size;
1176 if (*plane_wm > (int)display->max_wm)
1177 *plane_wm = display->max_wm;
1178
1179 /* Use the large buffer method to calculate cursor watermark */
Ville Syrjälä922044c2014-02-14 14:18:57 +02001180 line_time_us = max(htotal * 1000 / clock, 1);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001181 line_count = (cursor_latency_ns / line_time_us + 1000) / 1000;
Chris Wilson7bb836d2014-03-26 12:38:14 +00001182 entries = line_count * to_intel_crtc(crtc)->cursor_width * pixel_size;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001183 tlb_miss = cursor->fifo_size*cursor->cacheline_size - hdisplay * 8;
1184 if (tlb_miss > 0)
1185 entries += tlb_miss;
1186 entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
1187 *cursor_wm = entries + cursor->guard_size;
1188 if (*cursor_wm > (int)cursor->max_wm)
1189 *cursor_wm = (int)cursor->max_wm;
1190
1191 return true;
1192}
1193
1194/*
1195 * Check the wm result.
1196 *
1197 * If any calculated watermark values is larger than the maximum value that
1198 * can be programmed into the associated watermark register, that watermark
1199 * must be disabled.
1200 */
1201static bool g4x_check_srwm(struct drm_device *dev,
1202 int display_wm, int cursor_wm,
1203 const struct intel_watermark_params *display,
1204 const struct intel_watermark_params *cursor)
1205{
1206 DRM_DEBUG_KMS("SR watermark: display plane %d, cursor %d\n",
1207 display_wm, cursor_wm);
1208
1209 if (display_wm > display->max_wm) {
1210 DRM_DEBUG_KMS("display watermark is too large(%d/%ld), disabling\n",
1211 display_wm, display->max_wm);
1212 return false;
1213 }
1214
1215 if (cursor_wm > cursor->max_wm) {
1216 DRM_DEBUG_KMS("cursor watermark is too large(%d/%ld), disabling\n",
1217 cursor_wm, cursor->max_wm);
1218 return false;
1219 }
1220
1221 if (!(display_wm || cursor_wm)) {
1222 DRM_DEBUG_KMS("SR latency is 0, disabling\n");
1223 return false;
1224 }
1225
1226 return true;
1227}
1228
1229static bool g4x_compute_srwm(struct drm_device *dev,
1230 int plane,
1231 int latency_ns,
1232 const struct intel_watermark_params *display,
1233 const struct intel_watermark_params *cursor,
1234 int *display_wm, int *cursor_wm)
1235{
1236 struct drm_crtc *crtc;
Ville Syrjälä4fe85902013-09-04 18:25:22 +03001237 const struct drm_display_mode *adjusted_mode;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001238 int hdisplay, htotal, pixel_size, clock;
1239 unsigned long line_time_us;
1240 int line_count, line_size;
1241 int small, large;
1242 int entries;
1243
1244 if (!latency_ns) {
1245 *display_wm = *cursor_wm = 0;
1246 return false;
1247 }
1248
1249 crtc = intel_get_crtc_for_plane(dev, plane);
Ville Syrjälä4fe85902013-09-04 18:25:22 +03001250 adjusted_mode = &to_intel_crtc(crtc)->config.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001251 clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -08001252 htotal = adjusted_mode->crtc_htotal;
Ville Syrjälä37327ab2013-09-04 18:25:28 +03001253 hdisplay = to_intel_crtc(crtc)->config.pipe_src_w;
Matt Roperf4510a22014-04-01 15:22:40 -07001254 pixel_size = crtc->primary->fb->bits_per_pixel / 8;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001255
Ville Syrjälä922044c2014-02-14 14:18:57 +02001256 line_time_us = max(htotal * 1000 / clock, 1);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001257 line_count = (latency_ns / line_time_us + 1000) / 1000;
1258 line_size = hdisplay * pixel_size;
1259
1260 /* Use the minimum of the small and large buffer method for primary */
1261 small = ((clock * pixel_size / 1000) * latency_ns) / 1000;
1262 large = line_count * line_size;
1263
1264 entries = DIV_ROUND_UP(min(small, large), display->cacheline_size);
1265 *display_wm = entries + display->guard_size;
1266
1267 /* calculate the self-refresh watermark for display cursor */
Chris Wilson7bb836d2014-03-26 12:38:14 +00001268 entries = line_count * pixel_size * to_intel_crtc(crtc)->cursor_width;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001269 entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
1270 *cursor_wm = entries + cursor->guard_size;
1271
1272 return g4x_check_srwm(dev,
1273 *display_wm, *cursor_wm,
1274 display, cursor);
1275}
1276
1277static bool vlv_compute_drain_latency(struct drm_device *dev,
1278 int plane,
1279 int *plane_prec_mult,
1280 int *plane_dl,
1281 int *cursor_prec_mult,
1282 int *cursor_dl)
1283{
1284 struct drm_crtc *crtc;
1285 int clock, pixel_size;
1286 int entries;
1287
1288 crtc = intel_get_crtc_for_plane(dev, plane);
Chris Wilson3490ea52013-01-07 10:11:40 +00001289 if (!intel_crtc_active(crtc))
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001290 return false;
1291
Damien Lespiau241bfc32013-09-25 16:45:37 +01001292 clock = to_intel_crtc(crtc)->config.adjusted_mode.crtc_clock;
Matt Roperf4510a22014-04-01 15:22:40 -07001293 pixel_size = crtc->primary->fb->bits_per_pixel / 8; /* BPP */
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001294
1295 entries = (clock / 1000) * pixel_size;
1296 *plane_prec_mult = (entries > 256) ?
1297 DRAIN_LATENCY_PRECISION_32 : DRAIN_LATENCY_PRECISION_16;
1298 *plane_dl = (64 * (*plane_prec_mult) * 4) / ((clock / 1000) *
1299 pixel_size);
1300
1301 entries = (clock / 1000) * 4; /* BPP is always 4 for cursor */
1302 *cursor_prec_mult = (entries > 256) ?
1303 DRAIN_LATENCY_PRECISION_32 : DRAIN_LATENCY_PRECISION_16;
1304 *cursor_dl = (64 * (*cursor_prec_mult) * 4) / ((clock / 1000) * 4);
1305
1306 return true;
1307}
1308
1309/*
1310 * Update drain latency registers of memory arbiter
1311 *
1312 * Valleyview SoC has a new memory arbiter and needs drain latency registers
1313 * to be programmed. Each plane has a drain latency multiplier and a drain
1314 * latency value.
1315 */
1316
1317static void vlv_update_drain_latency(struct drm_device *dev)
1318{
1319 struct drm_i915_private *dev_priv = dev->dev_private;
1320 int planea_prec, planea_dl, planeb_prec, planeb_dl;
1321 int cursora_prec, cursora_dl, cursorb_prec, cursorb_dl;
1322 int plane_prec_mult, cursor_prec_mult; /* Precision multiplier is
1323 either 16 or 32 */
1324
1325 /* For plane A, Cursor A */
1326 if (vlv_compute_drain_latency(dev, 0, &plane_prec_mult, &planea_dl,
1327 &cursor_prec_mult, &cursora_dl)) {
1328 cursora_prec = (cursor_prec_mult == DRAIN_LATENCY_PRECISION_32) ?
1329 DDL_CURSORA_PRECISION_32 : DDL_CURSORA_PRECISION_16;
1330 planea_prec = (plane_prec_mult == DRAIN_LATENCY_PRECISION_32) ?
1331 DDL_PLANEA_PRECISION_32 : DDL_PLANEA_PRECISION_16;
1332
1333 I915_WRITE(VLV_DDL1, cursora_prec |
1334 (cursora_dl << DDL_CURSORA_SHIFT) |
1335 planea_prec | planea_dl);
1336 }
1337
1338 /* For plane B, Cursor B */
1339 if (vlv_compute_drain_latency(dev, 1, &plane_prec_mult, &planeb_dl,
1340 &cursor_prec_mult, &cursorb_dl)) {
1341 cursorb_prec = (cursor_prec_mult == DRAIN_LATENCY_PRECISION_32) ?
1342 DDL_CURSORB_PRECISION_32 : DDL_CURSORB_PRECISION_16;
1343 planeb_prec = (plane_prec_mult == DRAIN_LATENCY_PRECISION_32) ?
1344 DDL_PLANEB_PRECISION_32 : DDL_PLANEB_PRECISION_16;
1345
1346 I915_WRITE(VLV_DDL2, cursorb_prec |
1347 (cursorb_dl << DDL_CURSORB_SHIFT) |
1348 planeb_prec | planeb_dl);
1349 }
1350}
1351
1352#define single_plane_enabled(mask) is_power_of_2(mask)
1353
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001354static void valleyview_update_wm(struct drm_crtc *crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001355{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001356 struct drm_device *dev = crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001357 static const int sr_latency_ns = 12000;
1358 struct drm_i915_private *dev_priv = dev->dev_private;
1359 int planea_wm, planeb_wm, cursora_wm, cursorb_wm;
1360 int plane_sr, cursor_sr;
Chris Wilsonaf6c4572012-12-11 12:01:43 +00001361 int ignore_plane_sr, ignore_cursor_sr;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001362 unsigned int enabled = 0;
1363
1364 vlv_update_drain_latency(dev);
1365
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001366 if (g4x_compute_wm0(dev, PIPE_A,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001367 &valleyview_wm_info, latency_ns,
1368 &valleyview_cursor_wm_info, latency_ns,
1369 &planea_wm, &cursora_wm))
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001370 enabled |= 1 << PIPE_A;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001371
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001372 if (g4x_compute_wm0(dev, PIPE_B,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001373 &valleyview_wm_info, latency_ns,
1374 &valleyview_cursor_wm_info, latency_ns,
1375 &planeb_wm, &cursorb_wm))
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001376 enabled |= 1 << PIPE_B;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001377
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001378 if (single_plane_enabled(enabled) &&
1379 g4x_compute_srwm(dev, ffs(enabled) - 1,
1380 sr_latency_ns,
1381 &valleyview_wm_info,
1382 &valleyview_cursor_wm_info,
Chris Wilsonaf6c4572012-12-11 12:01:43 +00001383 &plane_sr, &ignore_cursor_sr) &&
1384 g4x_compute_srwm(dev, ffs(enabled) - 1,
1385 2*sr_latency_ns,
1386 &valleyview_wm_info,
1387 &valleyview_cursor_wm_info,
Chris Wilson52bd02d2012-12-07 10:43:24 +00001388 &ignore_plane_sr, &cursor_sr)) {
Imre Deak5209b1f2014-07-01 12:36:17 +03001389 intel_set_memory_cxsr(dev_priv, true);
Chris Wilson52bd02d2012-12-07 10:43:24 +00001390 } else {
Imre Deak5209b1f2014-07-01 12:36:17 +03001391 intel_set_memory_cxsr(dev_priv, false);
Chris Wilson52bd02d2012-12-07 10:43:24 +00001392 plane_sr = cursor_sr = 0;
1393 }
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001394
1395 DRM_DEBUG_KMS("Setting FIFO watermarks - A: plane=%d, cursor=%d, B: plane=%d, cursor=%d, SR: plane=%d, cursor=%d\n",
1396 planea_wm, cursora_wm,
1397 planeb_wm, cursorb_wm,
1398 plane_sr, cursor_sr);
1399
1400 I915_WRITE(DSPFW1,
1401 (plane_sr << DSPFW_SR_SHIFT) |
1402 (cursorb_wm << DSPFW_CURSORB_SHIFT) |
1403 (planeb_wm << DSPFW_PLANEB_SHIFT) |
1404 planea_wm);
1405 I915_WRITE(DSPFW2,
Chris Wilson8c919b22012-12-04 16:33:19 +00001406 (I915_READ(DSPFW2) & ~DSPFW_CURSORA_MASK) |
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001407 (cursora_wm << DSPFW_CURSORA_SHIFT));
1408 I915_WRITE(DSPFW3,
Chris Wilson8c919b22012-12-04 16:33:19 +00001409 (I915_READ(DSPFW3) & ~DSPFW_CURSOR_SR_MASK) |
1410 (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001411}
1412
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001413static void g4x_update_wm(struct drm_crtc *crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001414{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001415 struct drm_device *dev = crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001416 static const int sr_latency_ns = 12000;
1417 struct drm_i915_private *dev_priv = dev->dev_private;
1418 int planea_wm, planeb_wm, cursora_wm, cursorb_wm;
1419 int plane_sr, cursor_sr;
1420 unsigned int enabled = 0;
1421
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001422 if (g4x_compute_wm0(dev, PIPE_A,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001423 &g4x_wm_info, latency_ns,
1424 &g4x_cursor_wm_info, latency_ns,
1425 &planea_wm, &cursora_wm))
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001426 enabled |= 1 << PIPE_A;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001427
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001428 if (g4x_compute_wm0(dev, PIPE_B,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001429 &g4x_wm_info, latency_ns,
1430 &g4x_cursor_wm_info, latency_ns,
1431 &planeb_wm, &cursorb_wm))
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001432 enabled |= 1 << PIPE_B;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001433
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001434 if (single_plane_enabled(enabled) &&
1435 g4x_compute_srwm(dev, ffs(enabled) - 1,
1436 sr_latency_ns,
1437 &g4x_wm_info,
1438 &g4x_cursor_wm_info,
Chris Wilson52bd02d2012-12-07 10:43:24 +00001439 &plane_sr, &cursor_sr)) {
Imre Deak5209b1f2014-07-01 12:36:17 +03001440 intel_set_memory_cxsr(dev_priv, true);
Chris Wilson52bd02d2012-12-07 10:43:24 +00001441 } else {
Imre Deak5209b1f2014-07-01 12:36:17 +03001442 intel_set_memory_cxsr(dev_priv, false);
Chris Wilson52bd02d2012-12-07 10:43:24 +00001443 plane_sr = cursor_sr = 0;
1444 }
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001445
1446 DRM_DEBUG_KMS("Setting FIFO watermarks - A: plane=%d, cursor=%d, B: plane=%d, cursor=%d, SR: plane=%d, cursor=%d\n",
1447 planea_wm, cursora_wm,
1448 planeb_wm, cursorb_wm,
1449 plane_sr, cursor_sr);
1450
1451 I915_WRITE(DSPFW1,
1452 (plane_sr << DSPFW_SR_SHIFT) |
1453 (cursorb_wm << DSPFW_CURSORB_SHIFT) |
1454 (planeb_wm << DSPFW_PLANEB_SHIFT) |
1455 planea_wm);
1456 I915_WRITE(DSPFW2,
Chris Wilson8c919b22012-12-04 16:33:19 +00001457 (I915_READ(DSPFW2) & ~DSPFW_CURSORA_MASK) |
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001458 (cursora_wm << DSPFW_CURSORA_SHIFT));
1459 /* HPLL off in SR has some issues on G4x... disable it */
1460 I915_WRITE(DSPFW3,
Chris Wilson8c919b22012-12-04 16:33:19 +00001461 (I915_READ(DSPFW3) & ~(DSPFW_HPLL_SR_EN | DSPFW_CURSOR_SR_MASK)) |
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001462 (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
1463}
1464
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001465static void i965_update_wm(struct drm_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001466{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001467 struct drm_device *dev = unused_crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001468 struct drm_i915_private *dev_priv = dev->dev_private;
1469 struct drm_crtc *crtc;
1470 int srwm = 1;
1471 int cursor_sr = 16;
1472
1473 /* Calc sr entries for one plane configs */
1474 crtc = single_enabled_crtc(dev);
1475 if (crtc) {
1476 /* self-refresh has much higher latency */
1477 static const int sr_latency_ns = 12000;
Ville Syrjälä4fe85902013-09-04 18:25:22 +03001478 const struct drm_display_mode *adjusted_mode =
1479 &to_intel_crtc(crtc)->config.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001480 int clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -08001481 int htotal = adjusted_mode->crtc_htotal;
Ville Syrjälä37327ab2013-09-04 18:25:28 +03001482 int hdisplay = to_intel_crtc(crtc)->config.pipe_src_w;
Matt Roperf4510a22014-04-01 15:22:40 -07001483 int pixel_size = crtc->primary->fb->bits_per_pixel / 8;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001484 unsigned long line_time_us;
1485 int entries;
1486
Ville Syrjälä922044c2014-02-14 14:18:57 +02001487 line_time_us = max(htotal * 1000 / clock, 1);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001488
1489 /* Use ns/us then divide to preserve precision */
1490 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
1491 pixel_size * hdisplay;
1492 entries = DIV_ROUND_UP(entries, I915_FIFO_LINE_SIZE);
1493 srwm = I965_FIFO_SIZE - entries;
1494 if (srwm < 0)
1495 srwm = 1;
1496 srwm &= 0x1ff;
1497 DRM_DEBUG_KMS("self-refresh entries: %d, wm: %d\n",
1498 entries, srwm);
1499
1500 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
Chris Wilson7bb836d2014-03-26 12:38:14 +00001501 pixel_size * to_intel_crtc(crtc)->cursor_width;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001502 entries = DIV_ROUND_UP(entries,
1503 i965_cursor_wm_info.cacheline_size);
1504 cursor_sr = i965_cursor_wm_info.fifo_size -
1505 (entries + i965_cursor_wm_info.guard_size);
1506
1507 if (cursor_sr > i965_cursor_wm_info.max_wm)
1508 cursor_sr = i965_cursor_wm_info.max_wm;
1509
1510 DRM_DEBUG_KMS("self-refresh watermark: display plane %d "
1511 "cursor %d\n", srwm, cursor_sr);
1512
Imre Deak5209b1f2014-07-01 12:36:17 +03001513 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001514 } else {
1515 /* Turn off self refresh if both pipes are enabled */
Imre Deak5209b1f2014-07-01 12:36:17 +03001516 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001517 }
1518
1519 DRM_DEBUG_KMS("Setting FIFO watermarks - A: 8, B: 8, C: 8, SR %d\n",
1520 srwm);
1521
1522 /* 965 has limitations... */
1523 I915_WRITE(DSPFW1, (srwm << DSPFW_SR_SHIFT) |
1524 (8 << 16) | (8 << 8) | (8 << 0));
1525 I915_WRITE(DSPFW2, (8 << 8) | (8 << 0));
1526 /* update cursor SR watermark */
1527 I915_WRITE(DSPFW3, (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
1528}
1529
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001530static void i9xx_update_wm(struct drm_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001531{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001532 struct drm_device *dev = unused_crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001533 struct drm_i915_private *dev_priv = dev->dev_private;
1534 const struct intel_watermark_params *wm_info;
1535 uint32_t fwater_lo;
1536 uint32_t fwater_hi;
1537 int cwm, srwm = 1;
1538 int fifo_size;
1539 int planea_wm, planeb_wm;
1540 struct drm_crtc *crtc, *enabled = NULL;
1541
1542 if (IS_I945GM(dev))
1543 wm_info = &i945_wm_info;
1544 else if (!IS_GEN2(dev))
1545 wm_info = &i915_wm_info;
1546 else
Daniel Vetterfeb56b92013-12-14 20:38:30 -02001547 wm_info = &i830_wm_info;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001548
1549 fifo_size = dev_priv->display.get_fifo_size(dev, 0);
1550 crtc = intel_get_crtc_for_plane(dev, 0);
Chris Wilson3490ea52013-01-07 10:11:40 +00001551 if (intel_crtc_active(crtc)) {
Damien Lespiau241bfc32013-09-25 16:45:37 +01001552 const struct drm_display_mode *adjusted_mode;
Matt Roperf4510a22014-04-01 15:22:40 -07001553 int cpp = crtc->primary->fb->bits_per_pixel / 8;
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001554 if (IS_GEN2(dev))
1555 cpp = 4;
1556
Damien Lespiau241bfc32013-09-25 16:45:37 +01001557 adjusted_mode = &to_intel_crtc(crtc)->config.adjusted_mode;
1558 planea_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001559 wm_info, fifo_size, cpp,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001560 latency_ns);
1561 enabled = crtc;
1562 } else
1563 planea_wm = fifo_size - wm_info->guard_size;
1564
1565 fifo_size = dev_priv->display.get_fifo_size(dev, 1);
1566 crtc = intel_get_crtc_for_plane(dev, 1);
Chris Wilson3490ea52013-01-07 10:11:40 +00001567 if (intel_crtc_active(crtc)) {
Damien Lespiau241bfc32013-09-25 16:45:37 +01001568 const struct drm_display_mode *adjusted_mode;
Matt Roperf4510a22014-04-01 15:22:40 -07001569 int cpp = crtc->primary->fb->bits_per_pixel / 8;
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001570 if (IS_GEN2(dev))
1571 cpp = 4;
1572
Damien Lespiau241bfc32013-09-25 16:45:37 +01001573 adjusted_mode = &to_intel_crtc(crtc)->config.adjusted_mode;
1574 planeb_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001575 wm_info, fifo_size, cpp,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001576 latency_ns);
1577 if (enabled == NULL)
1578 enabled = crtc;
1579 else
1580 enabled = NULL;
1581 } else
1582 planeb_wm = fifo_size - wm_info->guard_size;
1583
1584 DRM_DEBUG_KMS("FIFO watermarks - A: %d, B: %d\n", planea_wm, planeb_wm);
1585
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02001586 if (IS_I915GM(dev) && enabled) {
1587 struct intel_framebuffer *fb;
1588
1589 fb = to_intel_framebuffer(enabled->primary->fb);
1590
1591 /* self-refresh seems busted with untiled */
1592 if (fb->obj->tiling_mode == I915_TILING_NONE)
1593 enabled = NULL;
1594 }
1595
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001596 /*
1597 * Overlay gets an aggressive default since video jitter is bad.
1598 */
1599 cwm = 2;
1600
1601 /* Play safe and disable self-refresh before adjusting watermarks. */
Imre Deak5209b1f2014-07-01 12:36:17 +03001602 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001603
1604 /* Calc sr entries for one plane configs */
1605 if (HAS_FW_BLC(dev) && enabled) {
1606 /* self-refresh has much higher latency */
1607 static const int sr_latency_ns = 6000;
Ville Syrjälä4fe85902013-09-04 18:25:22 +03001608 const struct drm_display_mode *adjusted_mode =
1609 &to_intel_crtc(enabled)->config.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001610 int clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -08001611 int htotal = adjusted_mode->crtc_htotal;
Daniel Vetterf727b492013-11-20 15:02:10 +01001612 int hdisplay = to_intel_crtc(enabled)->config.pipe_src_w;
Matt Roperf4510a22014-04-01 15:22:40 -07001613 int pixel_size = enabled->primary->fb->bits_per_pixel / 8;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001614 unsigned long line_time_us;
1615 int entries;
1616
Ville Syrjälä922044c2014-02-14 14:18:57 +02001617 line_time_us = max(htotal * 1000 / clock, 1);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001618
1619 /* Use ns/us then divide to preserve precision */
1620 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
1621 pixel_size * hdisplay;
1622 entries = DIV_ROUND_UP(entries, wm_info->cacheline_size);
1623 DRM_DEBUG_KMS("self-refresh entries: %d\n", entries);
1624 srwm = wm_info->fifo_size - entries;
1625 if (srwm < 0)
1626 srwm = 1;
1627
1628 if (IS_I945G(dev) || IS_I945GM(dev))
1629 I915_WRITE(FW_BLC_SELF,
1630 FW_BLC_SELF_FIFO_MASK | (srwm & 0xff));
1631 else if (IS_I915GM(dev))
1632 I915_WRITE(FW_BLC_SELF, srwm & 0x3f);
1633 }
1634
1635 DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d, B: %d, C: %d, SR %d\n",
1636 planea_wm, planeb_wm, cwm, srwm);
1637
1638 fwater_lo = ((planeb_wm & 0x3f) << 16) | (planea_wm & 0x3f);
1639 fwater_hi = (cwm & 0x1f);
1640
1641 /* Set request length to 8 cachelines per fetch */
1642 fwater_lo = fwater_lo | (1 << 24) | (1 << 8);
1643 fwater_hi = fwater_hi | (1 << 8);
1644
1645 I915_WRITE(FW_BLC, fwater_lo);
1646 I915_WRITE(FW_BLC2, fwater_hi);
1647
Imre Deak5209b1f2014-07-01 12:36:17 +03001648 if (enabled)
1649 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001650}
1651
Daniel Vetterfeb56b92013-12-14 20:38:30 -02001652static void i845_update_wm(struct drm_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001653{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001654 struct drm_device *dev = unused_crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001655 struct drm_i915_private *dev_priv = dev->dev_private;
1656 struct drm_crtc *crtc;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001657 const struct drm_display_mode *adjusted_mode;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001658 uint32_t fwater_lo;
1659 int planea_wm;
1660
1661 crtc = single_enabled_crtc(dev);
1662 if (crtc == NULL)
1663 return;
1664
Damien Lespiau241bfc32013-09-25 16:45:37 +01001665 adjusted_mode = &to_intel_crtc(crtc)->config.adjusted_mode;
1666 planea_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Daniel Vetterfeb56b92013-12-14 20:38:30 -02001667 &i845_wm_info,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001668 dev_priv->display.get_fifo_size(dev, 0),
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001669 4, latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001670 fwater_lo = I915_READ(FW_BLC) & ~0xfff;
1671 fwater_lo |= (3<<8) | planea_wm;
1672
1673 DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d\n", planea_wm);
1674
1675 I915_WRITE(FW_BLC, fwater_lo);
1676}
1677
Ville Syrjälä36587292013-07-05 11:57:16 +03001678static uint32_t ilk_pipe_pixel_rate(struct drm_device *dev,
1679 struct drm_crtc *crtc)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001680{
1681 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilsonfd4daa92013-08-27 17:04:17 +01001682 uint32_t pixel_rate;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001683
Damien Lespiau241bfc32013-09-25 16:45:37 +01001684 pixel_rate = intel_crtc->config.adjusted_mode.crtc_clock;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001685
1686 /* We only use IF-ID interlacing. If we ever use PF-ID we'll need to
1687 * adjust the pixel_rate here. */
1688
Chris Wilsonfd4daa92013-08-27 17:04:17 +01001689 if (intel_crtc->config.pch_pfit.enabled) {
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001690 uint64_t pipe_w, pipe_h, pfit_w, pfit_h;
Chris Wilsonfd4daa92013-08-27 17:04:17 +01001691 uint32_t pfit_size = intel_crtc->config.pch_pfit.size;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001692
Ville Syrjälä37327ab2013-09-04 18:25:28 +03001693 pipe_w = intel_crtc->config.pipe_src_w;
1694 pipe_h = intel_crtc->config.pipe_src_h;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001695 pfit_w = (pfit_size >> 16) & 0xFFFF;
1696 pfit_h = pfit_size & 0xFFFF;
1697 if (pipe_w < pfit_w)
1698 pipe_w = pfit_w;
1699 if (pipe_h < pfit_h)
1700 pipe_h = pfit_h;
1701
1702 pixel_rate = div_u64((uint64_t) pixel_rate * pipe_w * pipe_h,
1703 pfit_w * pfit_h);
1704 }
1705
1706 return pixel_rate;
1707}
1708
Ville Syrjälä37126462013-08-01 16:18:55 +03001709/* latency must be in 0.1us units. */
Ville Syrjälä23297042013-07-05 11:57:17 +03001710static uint32_t ilk_wm_method1(uint32_t pixel_rate, uint8_t bytes_per_pixel,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001711 uint32_t latency)
1712{
1713 uint64_t ret;
1714
Ville Syrjälä3312ba62013-08-01 16:18:53 +03001715 if (WARN(latency == 0, "Latency value missing\n"))
1716 return UINT_MAX;
1717
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001718 ret = (uint64_t) pixel_rate * bytes_per_pixel * latency;
1719 ret = DIV_ROUND_UP_ULL(ret, 64 * 10000) + 2;
1720
1721 return ret;
1722}
1723
Ville Syrjälä37126462013-08-01 16:18:55 +03001724/* latency must be in 0.1us units. */
Ville Syrjälä23297042013-07-05 11:57:17 +03001725static uint32_t ilk_wm_method2(uint32_t pixel_rate, uint32_t pipe_htotal,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001726 uint32_t horiz_pixels, uint8_t bytes_per_pixel,
1727 uint32_t latency)
1728{
1729 uint32_t ret;
1730
Ville Syrjälä3312ba62013-08-01 16:18:53 +03001731 if (WARN(latency == 0, "Latency value missing\n"))
1732 return UINT_MAX;
1733
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001734 ret = (latency * pixel_rate) / (pipe_htotal * 10000);
1735 ret = (ret + 1) * horiz_pixels * bytes_per_pixel;
1736 ret = DIV_ROUND_UP(ret, 64) + 2;
1737 return ret;
1738}
1739
Ville Syrjälä23297042013-07-05 11:57:17 +03001740static uint32_t ilk_wm_fbc(uint32_t pri_val, uint32_t horiz_pixels,
Paulo Zanonicca32e92013-05-31 11:45:06 -03001741 uint8_t bytes_per_pixel)
1742{
1743 return DIV_ROUND_UP(pri_val * 64, horiz_pixels * bytes_per_pixel) + 2;
1744}
1745
Imre Deak820c1982013-12-17 14:46:36 +02001746struct ilk_pipe_wm_parameters {
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001747 bool active;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001748 uint32_t pipe_htotal;
1749 uint32_t pixel_rate;
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001750 struct intel_plane_wm_parameters pri;
1751 struct intel_plane_wm_parameters spr;
1752 struct intel_plane_wm_parameters cur;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001753};
1754
Imre Deak820c1982013-12-17 14:46:36 +02001755struct ilk_wm_maximums {
Paulo Zanonicca32e92013-05-31 11:45:06 -03001756 uint16_t pri;
1757 uint16_t spr;
1758 uint16_t cur;
1759 uint16_t fbc;
1760};
1761
Ville Syrjälä240264f2013-08-07 13:29:12 +03001762/* used in computing the new watermarks state */
1763struct intel_wm_config {
1764 unsigned int num_pipes_active;
1765 bool sprites_enabled;
1766 bool sprites_scaled;
Ville Syrjälä240264f2013-08-07 13:29:12 +03001767};
1768
Ville Syrjälä37126462013-08-01 16:18:55 +03001769/*
1770 * For both WM_PIPE and WM_LP.
1771 * mem_value must be in 0.1us units.
1772 */
Imre Deak820c1982013-12-17 14:46:36 +02001773static uint32_t ilk_compute_pri_wm(const struct ilk_pipe_wm_parameters *params,
Paulo Zanonicca32e92013-05-31 11:45:06 -03001774 uint32_t mem_value,
1775 bool is_lp)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001776{
Paulo Zanonicca32e92013-05-31 11:45:06 -03001777 uint32_t method1, method2;
1778
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001779 if (!params->active || !params->pri.enabled)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001780 return 0;
1781
Ville Syrjälä23297042013-07-05 11:57:17 +03001782 method1 = ilk_wm_method1(params->pixel_rate,
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001783 params->pri.bytes_per_pixel,
Paulo Zanonicca32e92013-05-31 11:45:06 -03001784 mem_value);
1785
1786 if (!is_lp)
1787 return method1;
1788
Ville Syrjälä23297042013-07-05 11:57:17 +03001789 method2 = ilk_wm_method2(params->pixel_rate,
Paulo Zanonicca32e92013-05-31 11:45:06 -03001790 params->pipe_htotal,
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001791 params->pri.horiz_pixels,
1792 params->pri.bytes_per_pixel,
Paulo Zanonicca32e92013-05-31 11:45:06 -03001793 mem_value);
1794
1795 return min(method1, method2);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001796}
1797
Ville Syrjälä37126462013-08-01 16:18:55 +03001798/*
1799 * For both WM_PIPE and WM_LP.
1800 * mem_value must be in 0.1us units.
1801 */
Imre Deak820c1982013-12-17 14:46:36 +02001802static uint32_t ilk_compute_spr_wm(const struct ilk_pipe_wm_parameters *params,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001803 uint32_t mem_value)
1804{
1805 uint32_t method1, method2;
1806
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001807 if (!params->active || !params->spr.enabled)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001808 return 0;
1809
Ville Syrjälä23297042013-07-05 11:57:17 +03001810 method1 = ilk_wm_method1(params->pixel_rate,
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001811 params->spr.bytes_per_pixel,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001812 mem_value);
Ville Syrjälä23297042013-07-05 11:57:17 +03001813 method2 = ilk_wm_method2(params->pixel_rate,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001814 params->pipe_htotal,
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001815 params->spr.horiz_pixels,
1816 params->spr.bytes_per_pixel,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001817 mem_value);
1818 return min(method1, method2);
1819}
1820
Ville Syrjälä37126462013-08-01 16:18:55 +03001821/*
1822 * For both WM_PIPE and WM_LP.
1823 * mem_value must be in 0.1us units.
1824 */
Imre Deak820c1982013-12-17 14:46:36 +02001825static uint32_t ilk_compute_cur_wm(const struct ilk_pipe_wm_parameters *params,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001826 uint32_t mem_value)
1827{
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001828 if (!params->active || !params->cur.enabled)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001829 return 0;
1830
Ville Syrjälä23297042013-07-05 11:57:17 +03001831 return ilk_wm_method2(params->pixel_rate,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001832 params->pipe_htotal,
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001833 params->cur.horiz_pixels,
1834 params->cur.bytes_per_pixel,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001835 mem_value);
1836}
1837
Paulo Zanonicca32e92013-05-31 11:45:06 -03001838/* Only for WM_LP. */
Imre Deak820c1982013-12-17 14:46:36 +02001839static uint32_t ilk_compute_fbc_wm(const struct ilk_pipe_wm_parameters *params,
Ville Syrjälä1fda9882013-07-05 11:57:19 +03001840 uint32_t pri_val)
Paulo Zanonicca32e92013-05-31 11:45:06 -03001841{
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001842 if (!params->active || !params->pri.enabled)
Paulo Zanonicca32e92013-05-31 11:45:06 -03001843 return 0;
1844
Ville Syrjälä23297042013-07-05 11:57:17 +03001845 return ilk_wm_fbc(pri_val,
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001846 params->pri.horiz_pixels,
1847 params->pri.bytes_per_pixel);
Paulo Zanonicca32e92013-05-31 11:45:06 -03001848}
1849
Ville Syrjälä158ae642013-08-07 13:28:19 +03001850static unsigned int ilk_display_fifo_size(const struct drm_device *dev)
1851{
Ville Syrjälä416f4722013-11-02 21:07:46 -07001852 if (INTEL_INFO(dev)->gen >= 8)
1853 return 3072;
1854 else if (INTEL_INFO(dev)->gen >= 7)
Ville Syrjälä158ae642013-08-07 13:28:19 +03001855 return 768;
1856 else
1857 return 512;
1858}
1859
Ville Syrjälä4e975082014-03-07 18:32:11 +02001860static unsigned int ilk_plane_wm_reg_max(const struct drm_device *dev,
1861 int level, bool is_sprite)
1862{
1863 if (INTEL_INFO(dev)->gen >= 8)
1864 /* BDW primary/sprite plane watermarks */
1865 return level == 0 ? 255 : 2047;
1866 else if (INTEL_INFO(dev)->gen >= 7)
1867 /* IVB/HSW primary/sprite plane watermarks */
1868 return level == 0 ? 127 : 1023;
1869 else if (!is_sprite)
1870 /* ILK/SNB primary plane watermarks */
1871 return level == 0 ? 127 : 511;
1872 else
1873 /* ILK/SNB sprite plane watermarks */
1874 return level == 0 ? 63 : 255;
1875}
1876
1877static unsigned int ilk_cursor_wm_reg_max(const struct drm_device *dev,
1878 int level)
1879{
1880 if (INTEL_INFO(dev)->gen >= 7)
1881 return level == 0 ? 63 : 255;
1882 else
1883 return level == 0 ? 31 : 63;
1884}
1885
1886static unsigned int ilk_fbc_wm_reg_max(const struct drm_device *dev)
1887{
1888 if (INTEL_INFO(dev)->gen >= 8)
1889 return 31;
1890 else
1891 return 15;
1892}
1893
Ville Syrjälä158ae642013-08-07 13:28:19 +03001894/* Calculate the maximum primary/sprite plane watermark */
1895static unsigned int ilk_plane_wm_max(const struct drm_device *dev,
1896 int level,
Ville Syrjälä240264f2013-08-07 13:29:12 +03001897 const struct intel_wm_config *config,
Ville Syrjälä158ae642013-08-07 13:28:19 +03001898 enum intel_ddb_partitioning ddb_partitioning,
1899 bool is_sprite)
1900{
1901 unsigned int fifo_size = ilk_display_fifo_size(dev);
Ville Syrjälä158ae642013-08-07 13:28:19 +03001902
1903 /* if sprites aren't enabled, sprites get nothing */
Ville Syrjälä240264f2013-08-07 13:29:12 +03001904 if (is_sprite && !config->sprites_enabled)
Ville Syrjälä158ae642013-08-07 13:28:19 +03001905 return 0;
1906
1907 /* HSW allows LP1+ watermarks even with multiple pipes */
Ville Syrjälä240264f2013-08-07 13:29:12 +03001908 if (level == 0 || config->num_pipes_active > 1) {
Ville Syrjälä158ae642013-08-07 13:28:19 +03001909 fifo_size /= INTEL_INFO(dev)->num_pipes;
1910
1911 /*
1912 * For some reason the non self refresh
1913 * FIFO size is only half of the self
1914 * refresh FIFO size on ILK/SNB.
1915 */
1916 if (INTEL_INFO(dev)->gen <= 6)
1917 fifo_size /= 2;
1918 }
1919
Ville Syrjälä240264f2013-08-07 13:29:12 +03001920 if (config->sprites_enabled) {
Ville Syrjälä158ae642013-08-07 13:28:19 +03001921 /* level 0 is always calculated with 1:1 split */
1922 if (level > 0 && ddb_partitioning == INTEL_DDB_PART_5_6) {
1923 if (is_sprite)
1924 fifo_size *= 5;
1925 fifo_size /= 6;
1926 } else {
1927 fifo_size /= 2;
1928 }
1929 }
1930
1931 /* clamp to max that the registers can hold */
Ville Syrjälä4e975082014-03-07 18:32:11 +02001932 return min(fifo_size, ilk_plane_wm_reg_max(dev, level, is_sprite));
Ville Syrjälä158ae642013-08-07 13:28:19 +03001933}
1934
1935/* Calculate the maximum cursor plane watermark */
1936static unsigned int ilk_cursor_wm_max(const struct drm_device *dev,
Ville Syrjälä240264f2013-08-07 13:29:12 +03001937 int level,
1938 const struct intel_wm_config *config)
Ville Syrjälä158ae642013-08-07 13:28:19 +03001939{
1940 /* HSW LP1+ watermarks w/ multiple pipes */
Ville Syrjälä240264f2013-08-07 13:29:12 +03001941 if (level > 0 && config->num_pipes_active > 1)
Ville Syrjälä158ae642013-08-07 13:28:19 +03001942 return 64;
1943
1944 /* otherwise just report max that registers can hold */
Ville Syrjälä4e975082014-03-07 18:32:11 +02001945 return ilk_cursor_wm_reg_max(dev, level);
Ville Syrjälä158ae642013-08-07 13:28:19 +03001946}
1947
Damien Lespiaud34ff9c2014-01-06 19:17:23 +00001948static void ilk_compute_wm_maximums(const struct drm_device *dev,
Ville Syrjälä34982fe2013-10-09 19:18:09 +03001949 int level,
1950 const struct intel_wm_config *config,
1951 enum intel_ddb_partitioning ddb_partitioning,
Imre Deak820c1982013-12-17 14:46:36 +02001952 struct ilk_wm_maximums *max)
Ville Syrjälä158ae642013-08-07 13:28:19 +03001953{
Ville Syrjälä240264f2013-08-07 13:29:12 +03001954 max->pri = ilk_plane_wm_max(dev, level, config, ddb_partitioning, false);
1955 max->spr = ilk_plane_wm_max(dev, level, config, ddb_partitioning, true);
1956 max->cur = ilk_cursor_wm_max(dev, level, config);
Ville Syrjälä4e975082014-03-07 18:32:11 +02001957 max->fbc = ilk_fbc_wm_reg_max(dev);
Ville Syrjälä158ae642013-08-07 13:28:19 +03001958}
1959
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03001960static void ilk_compute_wm_reg_maximums(struct drm_device *dev,
1961 int level,
1962 struct ilk_wm_maximums *max)
1963{
1964 max->pri = ilk_plane_wm_reg_max(dev, level, false);
1965 max->spr = ilk_plane_wm_reg_max(dev, level, true);
1966 max->cur = ilk_cursor_wm_reg_max(dev, level);
1967 max->fbc = ilk_fbc_wm_reg_max(dev);
1968}
1969
Ville Syrjäläd9395652013-10-09 19:18:10 +03001970static bool ilk_validate_wm_level(int level,
Imre Deak820c1982013-12-17 14:46:36 +02001971 const struct ilk_wm_maximums *max,
Ville Syrjäläd9395652013-10-09 19:18:10 +03001972 struct intel_wm_level *result)
Ville Syrjäläa9786a12013-08-07 13:24:47 +03001973{
1974 bool ret;
1975
1976 /* already determined to be invalid? */
1977 if (!result->enable)
1978 return false;
1979
1980 result->enable = result->pri_val <= max->pri &&
1981 result->spr_val <= max->spr &&
1982 result->cur_val <= max->cur;
1983
1984 ret = result->enable;
1985
1986 /*
1987 * HACK until we can pre-compute everything,
1988 * and thus fail gracefully if LP0 watermarks
1989 * are exceeded...
1990 */
1991 if (level == 0 && !result->enable) {
1992 if (result->pri_val > max->pri)
1993 DRM_DEBUG_KMS("Primary WM%d too large %u (max %u)\n",
1994 level, result->pri_val, max->pri);
1995 if (result->spr_val > max->spr)
1996 DRM_DEBUG_KMS("Sprite WM%d too large %u (max %u)\n",
1997 level, result->spr_val, max->spr);
1998 if (result->cur_val > max->cur)
1999 DRM_DEBUG_KMS("Cursor WM%d too large %u (max %u)\n",
2000 level, result->cur_val, max->cur);
2001
2002 result->pri_val = min_t(uint32_t, result->pri_val, max->pri);
2003 result->spr_val = min_t(uint32_t, result->spr_val, max->spr);
2004 result->cur_val = min_t(uint32_t, result->cur_val, max->cur);
2005 result->enable = true;
2006 }
2007
Ville Syrjäläa9786a12013-08-07 13:24:47 +03002008 return ret;
2009}
2010
Damien Lespiaud34ff9c2014-01-06 19:17:23 +00002011static void ilk_compute_wm_level(const struct drm_i915_private *dev_priv,
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03002012 int level,
Imre Deak820c1982013-12-17 14:46:36 +02002013 const struct ilk_pipe_wm_parameters *p,
Ville Syrjälä1fd527c2013-08-06 22:24:05 +03002014 struct intel_wm_level *result)
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03002015{
2016 uint16_t pri_latency = dev_priv->wm.pri_latency[level];
2017 uint16_t spr_latency = dev_priv->wm.spr_latency[level];
2018 uint16_t cur_latency = dev_priv->wm.cur_latency[level];
2019
2020 /* WM1+ latency values stored in 0.5us units */
2021 if (level > 0) {
2022 pri_latency *= 5;
2023 spr_latency *= 5;
2024 cur_latency *= 5;
2025 }
2026
2027 result->pri_val = ilk_compute_pri_wm(p, pri_latency, level);
2028 result->spr_val = ilk_compute_spr_wm(p, spr_latency);
2029 result->cur_val = ilk_compute_cur_wm(p, cur_latency);
2030 result->fbc_val = ilk_compute_fbc_wm(p, result->pri_val);
2031 result->enable = true;
2032}
2033
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002034static uint32_t
2035hsw_compute_linetime_wm(struct drm_device *dev, struct drm_crtc *crtc)
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002036{
2037 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002038 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002039 struct drm_display_mode *mode = &intel_crtc->config.adjusted_mode;
Paulo Zanoni85a02de2013-05-03 17:23:43 -03002040 u32 linetime, ips_linetime;
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002041
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002042 if (!intel_crtc_active(crtc))
2043 return 0;
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002044
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002045 /* The WM are computed with base on how long it takes to fill a single
2046 * row at the given clock rate, multiplied by 8.
2047 * */
Jesse Barnesfec8cba2013-11-27 11:10:26 -08002048 linetime = DIV_ROUND_CLOSEST(mode->crtc_htotal * 1000 * 8,
2049 mode->crtc_clock);
2050 ips_linetime = DIV_ROUND_CLOSEST(mode->crtc_htotal * 1000 * 8,
Paulo Zanoni85a02de2013-05-03 17:23:43 -03002051 intel_ddi_get_cdclk_freq(dev_priv));
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002052
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002053 return PIPE_WM_LINETIME_IPS_LINETIME(ips_linetime) |
2054 PIPE_WM_LINETIME_TIME(linetime);
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002055}
2056
Ville Syrjälä12b134d2013-07-05 11:57:21 +03002057static void intel_read_wm_latency(struct drm_device *dev, uint16_t wm[5])
2058{
2059 struct drm_i915_private *dev_priv = dev->dev_private;
2060
Ville Syrjäläa42a5712014-01-07 16:14:08 +02002061 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Ville Syrjälä12b134d2013-07-05 11:57:21 +03002062 uint64_t sskpd = I915_READ64(MCH_SSKPD);
2063
2064 wm[0] = (sskpd >> 56) & 0xFF;
2065 if (wm[0] == 0)
2066 wm[0] = sskpd & 0xF;
Ville Syrjäläe5d50192013-07-05 11:57:22 +03002067 wm[1] = (sskpd >> 4) & 0xFF;
2068 wm[2] = (sskpd >> 12) & 0xFF;
2069 wm[3] = (sskpd >> 20) & 0x1FF;
2070 wm[4] = (sskpd >> 32) & 0x1FF;
Ville Syrjälä63cf9a12013-07-05 11:57:23 +03002071 } else if (INTEL_INFO(dev)->gen >= 6) {
2072 uint32_t sskpd = I915_READ(MCH_SSKPD);
2073
2074 wm[0] = (sskpd >> SSKPD_WM0_SHIFT) & SSKPD_WM_MASK;
2075 wm[1] = (sskpd >> SSKPD_WM1_SHIFT) & SSKPD_WM_MASK;
2076 wm[2] = (sskpd >> SSKPD_WM2_SHIFT) & SSKPD_WM_MASK;
2077 wm[3] = (sskpd >> SSKPD_WM3_SHIFT) & SSKPD_WM_MASK;
Ville Syrjälä3a88d0a2013-08-01 16:18:49 +03002078 } else if (INTEL_INFO(dev)->gen >= 5) {
2079 uint32_t mltr = I915_READ(MLTR_ILK);
2080
2081 /* ILK primary LP0 latency is 700 ns */
2082 wm[0] = 7;
2083 wm[1] = (mltr >> MLTR_WM1_SHIFT) & ILK_SRLT_MASK;
2084 wm[2] = (mltr >> MLTR_WM2_SHIFT) & ILK_SRLT_MASK;
Ville Syrjälä12b134d2013-07-05 11:57:21 +03002085 }
2086}
2087
Ville Syrjälä53615a52013-08-01 16:18:50 +03002088static void intel_fixup_spr_wm_latency(struct drm_device *dev, uint16_t wm[5])
2089{
2090 /* ILK sprite LP0 latency is 1300 ns */
2091 if (INTEL_INFO(dev)->gen == 5)
2092 wm[0] = 13;
2093}
2094
2095static void intel_fixup_cur_wm_latency(struct drm_device *dev, uint16_t wm[5])
2096{
2097 /* ILK cursor LP0 latency is 1300 ns */
2098 if (INTEL_INFO(dev)->gen == 5)
2099 wm[0] = 13;
2100
2101 /* WaDoubleCursorLP3Latency:ivb */
2102 if (IS_IVYBRIDGE(dev))
2103 wm[3] *= 2;
2104}
2105
Damien Lespiau546c81f2014-05-13 15:30:26 +01002106int ilk_wm_max_level(const struct drm_device *dev)
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03002107{
2108 /* how many WM levels are we expecting */
Ville Syrjäläa42a5712014-01-07 16:14:08 +02002109 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03002110 return 4;
2111 else if (INTEL_INFO(dev)->gen >= 6)
2112 return 3;
2113 else
2114 return 2;
2115}
2116
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002117static void intel_print_wm_latency(struct drm_device *dev,
2118 const char *name,
2119 const uint16_t wm[5])
2120{
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03002121 int level, max_level = ilk_wm_max_level(dev);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002122
2123 for (level = 0; level <= max_level; level++) {
2124 unsigned int latency = wm[level];
2125
2126 if (latency == 0) {
2127 DRM_ERROR("%s WM%d latency not provided\n",
2128 name, level);
2129 continue;
2130 }
2131
2132 /* WM1+ latency values in 0.5us units */
2133 if (level > 0)
2134 latency *= 5;
2135
2136 DRM_DEBUG_KMS("%s WM%d latency %u (%u.%u usec)\n",
2137 name, level, wm[level],
2138 latency / 10, latency % 10);
2139 }
2140}
2141
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002142static bool ilk_increase_wm_latency(struct drm_i915_private *dev_priv,
2143 uint16_t wm[5], uint16_t min)
2144{
2145 int level, max_level = ilk_wm_max_level(dev_priv->dev);
2146
2147 if (wm[0] >= min)
2148 return false;
2149
2150 wm[0] = max(wm[0], min);
2151 for (level = 1; level <= max_level; level++)
2152 wm[level] = max_t(uint16_t, wm[level], DIV_ROUND_UP(min, 5));
2153
2154 return true;
2155}
2156
2157static void snb_wm_latency_quirk(struct drm_device *dev)
2158{
2159 struct drm_i915_private *dev_priv = dev->dev_private;
2160 bool changed;
2161
2162 /*
2163 * The BIOS provided WM memory latency values are often
2164 * inadequate for high resolution displays. Adjust them.
2165 */
2166 changed = ilk_increase_wm_latency(dev_priv, dev_priv->wm.pri_latency, 12) |
2167 ilk_increase_wm_latency(dev_priv, dev_priv->wm.spr_latency, 12) |
2168 ilk_increase_wm_latency(dev_priv, dev_priv->wm.cur_latency, 12);
2169
2170 if (!changed)
2171 return;
2172
2173 DRM_DEBUG_KMS("WM latency values increased to avoid potential underruns\n");
2174 intel_print_wm_latency(dev, "Primary", dev_priv->wm.pri_latency);
2175 intel_print_wm_latency(dev, "Sprite", dev_priv->wm.spr_latency);
2176 intel_print_wm_latency(dev, "Cursor", dev_priv->wm.cur_latency);
2177}
2178
Damien Lespiaufa50ad62014-03-17 18:01:16 +00002179static void ilk_setup_wm_latency(struct drm_device *dev)
Ville Syrjälä53615a52013-08-01 16:18:50 +03002180{
2181 struct drm_i915_private *dev_priv = dev->dev_private;
2182
2183 intel_read_wm_latency(dev, dev_priv->wm.pri_latency);
2184
2185 memcpy(dev_priv->wm.spr_latency, dev_priv->wm.pri_latency,
2186 sizeof(dev_priv->wm.pri_latency));
2187 memcpy(dev_priv->wm.cur_latency, dev_priv->wm.pri_latency,
2188 sizeof(dev_priv->wm.pri_latency));
2189
2190 intel_fixup_spr_wm_latency(dev, dev_priv->wm.spr_latency);
2191 intel_fixup_cur_wm_latency(dev, dev_priv->wm.cur_latency);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002192
2193 intel_print_wm_latency(dev, "Primary", dev_priv->wm.pri_latency);
2194 intel_print_wm_latency(dev, "Sprite", dev_priv->wm.spr_latency);
2195 intel_print_wm_latency(dev, "Cursor", dev_priv->wm.cur_latency);
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002196
2197 if (IS_GEN6(dev))
2198 snb_wm_latency_quirk(dev);
Ville Syrjälä53615a52013-08-01 16:18:50 +03002199}
2200
Imre Deak820c1982013-12-17 14:46:36 +02002201static void ilk_compute_wm_parameters(struct drm_crtc *crtc,
Ville Syrjälä2a44b762014-03-07 18:32:09 +02002202 struct ilk_pipe_wm_parameters *p)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002203{
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03002204 struct drm_device *dev = crtc->dev;
2205 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2206 enum pipe pipe = intel_crtc->pipe;
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03002207 struct drm_plane *plane;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002208
Ville Syrjälä2a44b762014-03-07 18:32:09 +02002209 if (!intel_crtc_active(crtc))
2210 return;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002211
Ville Syrjälä2a44b762014-03-07 18:32:09 +02002212 p->active = true;
2213 p->pipe_htotal = intel_crtc->config.adjusted_mode.crtc_htotal;
2214 p->pixel_rate = ilk_pipe_pixel_rate(dev, crtc);
2215 p->pri.bytes_per_pixel = crtc->primary->fb->bits_per_pixel / 8;
2216 p->cur.bytes_per_pixel = 4;
2217 p->pri.horiz_pixels = intel_crtc->config.pipe_src_w;
2218 p->cur.horiz_pixels = intel_crtc->cursor_width;
2219 /* TODO: for now, assume primary and cursor planes are always enabled. */
2220 p->pri.enabled = true;
2221 p->cur.enabled = true;
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03002222
Matt Roperaf2b6532014-04-01 15:22:32 -07002223 drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002224 struct intel_plane *intel_plane = to_intel_plane(plane);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002225
Ville Syrjälä2a44b762014-03-07 18:32:09 +02002226 if (intel_plane->pipe == pipe) {
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03002227 p->spr = intel_plane->wm;
Ville Syrjälä2a44b762014-03-07 18:32:09 +02002228 break;
2229 }
2230 }
2231}
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002232
Ville Syrjälä2a44b762014-03-07 18:32:09 +02002233static void ilk_compute_wm_config(struct drm_device *dev,
2234 struct intel_wm_config *config)
2235{
2236 struct intel_crtc *intel_crtc;
2237
2238 /* Compute the currently _active_ config */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01002239 for_each_intel_crtc(dev, intel_crtc) {
Ville Syrjälä2a44b762014-03-07 18:32:09 +02002240 const struct intel_pipe_wm *wm = &intel_crtc->wm.active;
2241
2242 if (!wm->pipe_enabled)
2243 continue;
2244
2245 config->sprites_enabled |= wm->sprites_enabled;
2246 config->sprites_scaled |= wm->sprites_scaled;
2247 config->num_pipes_active++;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002248 }
2249}
2250
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002251/* Compute new watermarks for the pipe */
2252static bool intel_compute_pipe_wm(struct drm_crtc *crtc,
Imre Deak820c1982013-12-17 14:46:36 +02002253 const struct ilk_pipe_wm_parameters *params,
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002254 struct intel_pipe_wm *pipe_wm)
2255{
2256 struct drm_device *dev = crtc->dev;
Damien Lespiaud34ff9c2014-01-06 19:17:23 +00002257 const struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002258 int level, max_level = ilk_wm_max_level(dev);
2259 /* LP0 watermark maximums depend on this pipe alone */
2260 struct intel_wm_config config = {
2261 .num_pipes_active = 1,
2262 .sprites_enabled = params->spr.enabled,
2263 .sprites_scaled = params->spr.scaled,
2264 };
Imre Deak820c1982013-12-17 14:46:36 +02002265 struct ilk_wm_maximums max;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002266
Ville Syrjälä2a44b762014-03-07 18:32:09 +02002267 pipe_wm->pipe_enabled = params->active;
2268 pipe_wm->sprites_enabled = params->spr.enabled;
2269 pipe_wm->sprites_scaled = params->spr.scaled;
2270
Ville Syrjälä7b39a0b2013-12-05 15:51:30 +02002271 /* ILK/SNB: LP2+ watermarks only w/o sprites */
2272 if (INTEL_INFO(dev)->gen <= 6 && params->spr.enabled)
2273 max_level = 1;
2274
2275 /* ILK/SNB/IVB: LP1+ watermarks only w/o scaling */
2276 if (params->spr.scaled)
2277 max_level = 0;
2278
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002279 ilk_compute_wm_level(dev_priv, 0, params, &pipe_wm->wm[0]);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002280
Ville Syrjäläa42a5712014-01-07 16:14:08 +02002281 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Ville Syrjäläce0e0712013-12-05 15:51:36 +02002282 pipe_wm->linetime = hsw_compute_linetime_wm(dev, crtc);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002283
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002284 /* LP0 watermarks always use 1/2 DDB partitioning */
2285 ilk_compute_wm_maximums(dev, 0, &config, INTEL_DDB_PART_1_2, &max);
2286
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002287 /* At least LP0 must be valid */
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002288 if (!ilk_validate_wm_level(0, &max, &pipe_wm->wm[0]))
2289 return false;
2290
2291 ilk_compute_wm_reg_maximums(dev, 1, &max);
2292
2293 for (level = 1; level <= max_level; level++) {
2294 struct intel_wm_level wm = {};
2295
2296 ilk_compute_wm_level(dev_priv, level, params, &wm);
2297
2298 /*
2299 * Disable any watermark level that exceeds the
2300 * register maximums since such watermarks are
2301 * always invalid.
2302 */
2303 if (!ilk_validate_wm_level(level, &max, &wm))
2304 break;
2305
2306 pipe_wm->wm[level] = wm;
2307 }
2308
2309 return true;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002310}
2311
2312/*
2313 * Merge the watermarks from all active pipes for a specific level.
2314 */
2315static void ilk_merge_wm_level(struct drm_device *dev,
2316 int level,
2317 struct intel_wm_level *ret_wm)
2318{
2319 const struct intel_crtc *intel_crtc;
2320
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002321 ret_wm->enable = true;
2322
Damien Lespiaud3fcc802014-05-13 23:32:22 +01002323 for_each_intel_crtc(dev, intel_crtc) {
Ville Syrjäläfe392ef2014-03-07 18:32:10 +02002324 const struct intel_pipe_wm *active = &intel_crtc->wm.active;
2325 const struct intel_wm_level *wm = &active->wm[level];
2326
2327 if (!active->pipe_enabled)
2328 continue;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002329
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002330 /*
2331 * The watermark values may have been used in the past,
2332 * so we must maintain them in the registers for some
2333 * time even if the level is now disabled.
2334 */
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002335 if (!wm->enable)
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002336 ret_wm->enable = false;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002337
2338 ret_wm->pri_val = max(ret_wm->pri_val, wm->pri_val);
2339 ret_wm->spr_val = max(ret_wm->spr_val, wm->spr_val);
2340 ret_wm->cur_val = max(ret_wm->cur_val, wm->cur_val);
2341 ret_wm->fbc_val = max(ret_wm->fbc_val, wm->fbc_val);
2342 }
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002343}
2344
2345/*
2346 * Merge all low power watermarks for all active pipes.
2347 */
2348static void ilk_wm_merge(struct drm_device *dev,
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02002349 const struct intel_wm_config *config,
Imre Deak820c1982013-12-17 14:46:36 +02002350 const struct ilk_wm_maximums *max,
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002351 struct intel_pipe_wm *merged)
2352{
2353 int level, max_level = ilk_wm_max_level(dev);
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002354 int last_enabled_level = max_level;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002355
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02002356 /* ILK/SNB/IVB: LP1+ watermarks only w/ single pipe */
2357 if ((INTEL_INFO(dev)->gen <= 6 || IS_IVYBRIDGE(dev)) &&
2358 config->num_pipes_active > 1)
2359 return;
2360
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02002361 /* ILK: FBC WM must be disabled always */
2362 merged->fbc_wm_enabled = INTEL_INFO(dev)->gen >= 6;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002363
2364 /* merge each WM1+ level */
2365 for (level = 1; level <= max_level; level++) {
2366 struct intel_wm_level *wm = &merged->wm[level];
2367
2368 ilk_merge_wm_level(dev, level, wm);
2369
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002370 if (level > last_enabled_level)
2371 wm->enable = false;
2372 else if (!ilk_validate_wm_level(level, max, wm))
2373 /* make sure all following levels get disabled */
2374 last_enabled_level = level - 1;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002375
2376 /*
2377 * The spec says it is preferred to disable
2378 * FBC WMs instead of disabling a WM level.
2379 */
2380 if (wm->fbc_val > max->fbc) {
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002381 if (wm->enable)
2382 merged->fbc_wm_enabled = false;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002383 wm->fbc_val = 0;
2384 }
2385 }
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02002386
2387 /* ILK: LP2+ must be disabled when FBC WM is disabled but FBC enabled */
2388 /*
2389 * FIXME this is racy. FBC might get enabled later.
2390 * What we should check here is whether FBC can be
2391 * enabled sometime later.
2392 */
2393 if (IS_GEN5(dev) && !merged->fbc_wm_enabled && intel_fbc_enabled(dev)) {
2394 for (level = 2; level <= max_level; level++) {
2395 struct intel_wm_level *wm = &merged->wm[level];
2396
2397 wm->enable = false;
2398 }
2399 }
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002400}
2401
Ville Syrjäläb380ca32013-10-09 19:18:01 +03002402static int ilk_wm_lp_to_level(int wm_lp, const struct intel_pipe_wm *pipe_wm)
2403{
2404 /* LP1,LP2,LP3 levels are either 1,2,3 or 1,3,4 */
2405 return wm_lp + (wm_lp >= 2 && pipe_wm->wm[4].enable);
2406}
2407
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02002408/* The value we need to program into the WM_LPx latency field */
2409static unsigned int ilk_wm_lp_latency(struct drm_device *dev, int level)
2410{
2411 struct drm_i915_private *dev_priv = dev->dev_private;
2412
Ville Syrjäläa42a5712014-01-07 16:14:08 +02002413 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02002414 return 2 * level;
2415 else
2416 return dev_priv->wm.pri_latency[level];
2417}
2418
Imre Deak820c1982013-12-17 14:46:36 +02002419static void ilk_compute_wm_results(struct drm_device *dev,
Ville Syrjälä0362c782013-10-09 19:17:57 +03002420 const struct intel_pipe_wm *merged,
Ville Syrjälä609cede2013-10-09 19:18:03 +03002421 enum intel_ddb_partitioning partitioning,
Imre Deak820c1982013-12-17 14:46:36 +02002422 struct ilk_wm_values *results)
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002423{
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002424 struct intel_crtc *intel_crtc;
2425 int level, wm_lp;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002426
Ville Syrjälä0362c782013-10-09 19:17:57 +03002427 results->enable_fbc_wm = merged->fbc_wm_enabled;
Ville Syrjälä609cede2013-10-09 19:18:03 +03002428 results->partitioning = partitioning;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002429
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002430 /* LP1+ register values */
Paulo Zanonicca32e92013-05-31 11:45:06 -03002431 for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
Ville Syrjälä1fd527c2013-08-06 22:24:05 +03002432 const struct intel_wm_level *r;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002433
Ville Syrjäläb380ca32013-10-09 19:18:01 +03002434 level = ilk_wm_lp_to_level(wm_lp, merged);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002435
Ville Syrjälä0362c782013-10-09 19:17:57 +03002436 r = &merged->wm[level];
Paulo Zanonicca32e92013-05-31 11:45:06 -03002437
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002438 /*
2439 * Maintain the watermark values even if the level is
2440 * disabled. Doing otherwise could cause underruns.
2441 */
2442 results->wm_lp[wm_lp - 1] =
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02002443 (ilk_wm_lp_latency(dev, level) << WM1_LP_LATENCY_SHIFT) |
Ville Syrjälä416f4722013-11-02 21:07:46 -07002444 (r->pri_val << WM1_LP_SR_SHIFT) |
2445 r->cur_val;
2446
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002447 if (r->enable)
2448 results->wm_lp[wm_lp - 1] |= WM1_LP_SR_EN;
2449
Ville Syrjälä416f4722013-11-02 21:07:46 -07002450 if (INTEL_INFO(dev)->gen >= 8)
2451 results->wm_lp[wm_lp - 1] |=
2452 r->fbc_val << WM1_LP_FBC_SHIFT_BDW;
2453 else
2454 results->wm_lp[wm_lp - 1] |=
2455 r->fbc_val << WM1_LP_FBC_SHIFT;
2456
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002457 /*
2458 * Always set WM1S_LP_EN when spr_val != 0, even if the
2459 * level is disabled. Doing otherwise could cause underruns.
2460 */
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02002461 if (INTEL_INFO(dev)->gen <= 6 && r->spr_val) {
2462 WARN_ON(wm_lp != 1);
2463 results->wm_lp_spr[wm_lp - 1] = WM1S_LP_EN | r->spr_val;
2464 } else
2465 results->wm_lp_spr[wm_lp - 1] = r->spr_val;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002466 }
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002467
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002468 /* LP0 register values */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01002469 for_each_intel_crtc(dev, intel_crtc) {
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002470 enum pipe pipe = intel_crtc->pipe;
2471 const struct intel_wm_level *r =
2472 &intel_crtc->wm.active.wm[0];
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002473
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002474 if (WARN_ON(!r->enable))
2475 continue;
2476
2477 results->wm_linetime[pipe] = intel_crtc->wm.active.linetime;
2478
2479 results->wm_pipe[pipe] =
2480 (r->pri_val << WM0_PIPE_PLANE_SHIFT) |
2481 (r->spr_val << WM0_PIPE_SPRITE_SHIFT) |
2482 r->cur_val;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002483 }
2484}
2485
Paulo Zanoni861f3382013-05-31 10:19:21 -03002486/* Find the result with the highest level enabled. Check for enable_fbc_wm in
2487 * case both are at the same level. Prefer r1 in case they're the same. */
Imre Deak820c1982013-12-17 14:46:36 +02002488static struct intel_pipe_wm *ilk_find_best_result(struct drm_device *dev,
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002489 struct intel_pipe_wm *r1,
2490 struct intel_pipe_wm *r2)
Paulo Zanoni861f3382013-05-31 10:19:21 -03002491{
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002492 int level, max_level = ilk_wm_max_level(dev);
2493 int level1 = 0, level2 = 0;
Paulo Zanoni861f3382013-05-31 10:19:21 -03002494
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002495 for (level = 1; level <= max_level; level++) {
2496 if (r1->wm[level].enable)
2497 level1 = level;
2498 if (r2->wm[level].enable)
2499 level2 = level;
Paulo Zanoni861f3382013-05-31 10:19:21 -03002500 }
2501
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002502 if (level1 == level2) {
2503 if (r2->fbc_wm_enabled && !r1->fbc_wm_enabled)
Paulo Zanoni861f3382013-05-31 10:19:21 -03002504 return r2;
2505 else
2506 return r1;
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002507 } else if (level1 > level2) {
Paulo Zanoni861f3382013-05-31 10:19:21 -03002508 return r1;
2509 } else {
2510 return r2;
2511 }
2512}
2513
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002514/* dirty bits used to track which watermarks need changes */
2515#define WM_DIRTY_PIPE(pipe) (1 << (pipe))
2516#define WM_DIRTY_LINETIME(pipe) (1 << (8 + (pipe)))
2517#define WM_DIRTY_LP(wm_lp) (1 << (15 + (wm_lp)))
2518#define WM_DIRTY_LP_ALL (WM_DIRTY_LP(1) | WM_DIRTY_LP(2) | WM_DIRTY_LP(3))
2519#define WM_DIRTY_FBC (1 << 24)
2520#define WM_DIRTY_DDB (1 << 25)
2521
2522static unsigned int ilk_compute_wm_dirty(struct drm_device *dev,
Imre Deak820c1982013-12-17 14:46:36 +02002523 const struct ilk_wm_values *old,
2524 const struct ilk_wm_values *new)
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002525{
2526 unsigned int dirty = 0;
2527 enum pipe pipe;
2528 int wm_lp;
2529
2530 for_each_pipe(pipe) {
2531 if (old->wm_linetime[pipe] != new->wm_linetime[pipe]) {
2532 dirty |= WM_DIRTY_LINETIME(pipe);
2533 /* Must disable LP1+ watermarks too */
2534 dirty |= WM_DIRTY_LP_ALL;
2535 }
2536
2537 if (old->wm_pipe[pipe] != new->wm_pipe[pipe]) {
2538 dirty |= WM_DIRTY_PIPE(pipe);
2539 /* Must disable LP1+ watermarks too */
2540 dirty |= WM_DIRTY_LP_ALL;
2541 }
2542 }
2543
2544 if (old->enable_fbc_wm != new->enable_fbc_wm) {
2545 dirty |= WM_DIRTY_FBC;
2546 /* Must disable LP1+ watermarks too */
2547 dirty |= WM_DIRTY_LP_ALL;
2548 }
2549
2550 if (old->partitioning != new->partitioning) {
2551 dirty |= WM_DIRTY_DDB;
2552 /* Must disable LP1+ watermarks too */
2553 dirty |= WM_DIRTY_LP_ALL;
2554 }
2555
2556 /* LP1+ watermarks already deemed dirty, no need to continue */
2557 if (dirty & WM_DIRTY_LP_ALL)
2558 return dirty;
2559
2560 /* Find the lowest numbered LP1+ watermark in need of an update... */
2561 for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
2562 if (old->wm_lp[wm_lp - 1] != new->wm_lp[wm_lp - 1] ||
2563 old->wm_lp_spr[wm_lp - 1] != new->wm_lp_spr[wm_lp - 1])
2564 break;
2565 }
2566
2567 /* ...and mark it and all higher numbered LP1+ watermarks as dirty */
2568 for (; wm_lp <= 3; wm_lp++)
2569 dirty |= WM_DIRTY_LP(wm_lp);
2570
2571 return dirty;
2572}
2573
Ville Syrjälä8553c182013-12-05 15:51:39 +02002574static bool _ilk_disable_lp_wm(struct drm_i915_private *dev_priv,
2575 unsigned int dirty)
2576{
Imre Deak820c1982013-12-17 14:46:36 +02002577 struct ilk_wm_values *previous = &dev_priv->wm.hw;
Ville Syrjälä8553c182013-12-05 15:51:39 +02002578 bool changed = false;
2579
2580 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] & WM1_LP_SR_EN) {
2581 previous->wm_lp[2] &= ~WM1_LP_SR_EN;
2582 I915_WRITE(WM3_LP_ILK, previous->wm_lp[2]);
2583 changed = true;
2584 }
2585 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] & WM1_LP_SR_EN) {
2586 previous->wm_lp[1] &= ~WM1_LP_SR_EN;
2587 I915_WRITE(WM2_LP_ILK, previous->wm_lp[1]);
2588 changed = true;
2589 }
2590 if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] & WM1_LP_SR_EN) {
2591 previous->wm_lp[0] &= ~WM1_LP_SR_EN;
2592 I915_WRITE(WM1_LP_ILK, previous->wm_lp[0]);
2593 changed = true;
2594 }
2595
2596 /*
2597 * Don't touch WM1S_LP_EN here.
2598 * Doing so could cause underruns.
2599 */
2600
2601 return changed;
2602}
2603
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002604/*
2605 * The spec says we shouldn't write when we don't need, because every write
2606 * causes WMs to be re-evaluated, expending some power.
2607 */
Imre Deak820c1982013-12-17 14:46:36 +02002608static void ilk_write_wm_values(struct drm_i915_private *dev_priv,
2609 struct ilk_wm_values *results)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002610{
Ville Syrjäläac9545f2013-12-05 15:51:28 +02002611 struct drm_device *dev = dev_priv->dev;
Imre Deak820c1982013-12-17 14:46:36 +02002612 struct ilk_wm_values *previous = &dev_priv->wm.hw;
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002613 unsigned int dirty;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002614 uint32_t val;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002615
Ville Syrjälä8553c182013-12-05 15:51:39 +02002616 dirty = ilk_compute_wm_dirty(dev, previous, results);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002617 if (!dirty)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002618 return;
2619
Ville Syrjälä8553c182013-12-05 15:51:39 +02002620 _ilk_disable_lp_wm(dev_priv, dirty);
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02002621
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002622 if (dirty & WM_DIRTY_PIPE(PIPE_A))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002623 I915_WRITE(WM0_PIPEA_ILK, results->wm_pipe[0]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002624 if (dirty & WM_DIRTY_PIPE(PIPE_B))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002625 I915_WRITE(WM0_PIPEB_ILK, results->wm_pipe[1]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002626 if (dirty & WM_DIRTY_PIPE(PIPE_C))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002627 I915_WRITE(WM0_PIPEC_IVB, results->wm_pipe[2]);
2628
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002629 if (dirty & WM_DIRTY_LINETIME(PIPE_A))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002630 I915_WRITE(PIPE_WM_LINETIME(PIPE_A), results->wm_linetime[0]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002631 if (dirty & WM_DIRTY_LINETIME(PIPE_B))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002632 I915_WRITE(PIPE_WM_LINETIME(PIPE_B), results->wm_linetime[1]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002633 if (dirty & WM_DIRTY_LINETIME(PIPE_C))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002634 I915_WRITE(PIPE_WM_LINETIME(PIPE_C), results->wm_linetime[2]);
2635
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002636 if (dirty & WM_DIRTY_DDB) {
Ville Syrjäläa42a5712014-01-07 16:14:08 +02002637 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Ville Syrjäläac9545f2013-12-05 15:51:28 +02002638 val = I915_READ(WM_MISC);
2639 if (results->partitioning == INTEL_DDB_PART_1_2)
2640 val &= ~WM_MISC_DATA_PARTITION_5_6;
2641 else
2642 val |= WM_MISC_DATA_PARTITION_5_6;
2643 I915_WRITE(WM_MISC, val);
2644 } else {
2645 val = I915_READ(DISP_ARB_CTL2);
2646 if (results->partitioning == INTEL_DDB_PART_1_2)
2647 val &= ~DISP_DATA_PARTITION_5_6;
2648 else
2649 val |= DISP_DATA_PARTITION_5_6;
2650 I915_WRITE(DISP_ARB_CTL2, val);
2651 }
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002652 }
2653
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002654 if (dirty & WM_DIRTY_FBC) {
Paulo Zanonicca32e92013-05-31 11:45:06 -03002655 val = I915_READ(DISP_ARB_CTL);
2656 if (results->enable_fbc_wm)
2657 val &= ~DISP_FBC_WM_DIS;
2658 else
2659 val |= DISP_FBC_WM_DIS;
2660 I915_WRITE(DISP_ARB_CTL, val);
2661 }
2662
Imre Deak954911e2013-12-17 14:46:34 +02002663 if (dirty & WM_DIRTY_LP(1) &&
2664 previous->wm_lp_spr[0] != results->wm_lp_spr[0])
2665 I915_WRITE(WM1S_LP_ILK, results->wm_lp_spr[0]);
2666
2667 if (INTEL_INFO(dev)->gen >= 7) {
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02002668 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp_spr[1] != results->wm_lp_spr[1])
2669 I915_WRITE(WM2S_LP_IVB, results->wm_lp_spr[1]);
2670 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp_spr[2] != results->wm_lp_spr[2])
2671 I915_WRITE(WM3S_LP_IVB, results->wm_lp_spr[2]);
2672 }
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002673
Ville Syrjäläfacd619b2013-12-05 15:51:33 +02002674 if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] != results->wm_lp[0])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002675 I915_WRITE(WM1_LP_ILK, results->wm_lp[0]);
Ville Syrjäläfacd619b2013-12-05 15:51:33 +02002676 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] != results->wm_lp[1])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002677 I915_WRITE(WM2_LP_ILK, results->wm_lp[1]);
Ville Syrjäläfacd619b2013-12-05 15:51:33 +02002678 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] != results->wm_lp[2])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002679 I915_WRITE(WM3_LP_ILK, results->wm_lp[2]);
Ville Syrjälä609cede2013-10-09 19:18:03 +03002680
2681 dev_priv->wm.hw = *results;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002682}
2683
Ville Syrjälä8553c182013-12-05 15:51:39 +02002684static bool ilk_disable_lp_wm(struct drm_device *dev)
2685{
2686 struct drm_i915_private *dev_priv = dev->dev_private;
2687
2688 return _ilk_disable_lp_wm(dev_priv, WM_DIRTY_LP_ALL);
2689}
2690
Imre Deak820c1982013-12-17 14:46:36 +02002691static void ilk_update_wm(struct drm_crtc *crtc)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002692{
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03002693 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjälä46ba6142013-09-10 11:40:40 +03002694 struct drm_device *dev = crtc->dev;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002695 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak820c1982013-12-17 14:46:36 +02002696 struct ilk_wm_maximums max;
2697 struct ilk_pipe_wm_parameters params = {};
2698 struct ilk_wm_values results = {};
Ville Syrjälä77c122b2013-08-06 22:24:04 +03002699 enum intel_ddb_partitioning partitioning;
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03002700 struct intel_pipe_wm pipe_wm = {};
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002701 struct intel_pipe_wm lp_wm_1_2 = {}, lp_wm_5_6 = {}, *best_lp_wm;
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03002702 struct intel_wm_config config = {};
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002703
Ville Syrjälä2a44b762014-03-07 18:32:09 +02002704 ilk_compute_wm_parameters(crtc, &params);
Paulo Zanoni861f3382013-05-31 10:19:21 -03002705
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03002706 intel_compute_pipe_wm(crtc, &params, &pipe_wm);
2707
2708 if (!memcmp(&intel_crtc->wm.active, &pipe_wm, sizeof(pipe_wm)))
2709 return;
2710
2711 intel_crtc->wm.active = pipe_wm;
2712
Ville Syrjälä2a44b762014-03-07 18:32:09 +02002713 ilk_compute_wm_config(dev, &config);
2714
Ville Syrjälä34982fe2013-10-09 19:18:09 +03002715 ilk_compute_wm_maximums(dev, 1, &config, INTEL_DDB_PART_1_2, &max);
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02002716 ilk_wm_merge(dev, &config, &max, &lp_wm_1_2);
Ville Syrjälä0362c782013-10-09 19:17:57 +03002717
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03002718 /* 5/6 split only in single pipe config on IVB+ */
Ville Syrjäläec98c8d2013-10-11 15:26:26 +03002719 if (INTEL_INFO(dev)->gen >= 7 &&
2720 config.num_pipes_active == 1 && config.sprites_enabled) {
Ville Syrjälä34982fe2013-10-09 19:18:09 +03002721 ilk_compute_wm_maximums(dev, 1, &config, INTEL_DDB_PART_5_6, &max);
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02002722 ilk_wm_merge(dev, &config, &max, &lp_wm_5_6);
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03002723
Imre Deak820c1982013-12-17 14:46:36 +02002724 best_lp_wm = ilk_find_best_result(dev, &lp_wm_1_2, &lp_wm_5_6);
Paulo Zanoni861f3382013-05-31 10:19:21 -03002725 } else {
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002726 best_lp_wm = &lp_wm_1_2;
Paulo Zanoni861f3382013-05-31 10:19:21 -03002727 }
2728
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002729 partitioning = (best_lp_wm == &lp_wm_1_2) ?
Ville Syrjälä77c122b2013-08-06 22:24:04 +03002730 INTEL_DDB_PART_1_2 : INTEL_DDB_PART_5_6;
Paulo Zanoni861f3382013-05-31 10:19:21 -03002731
Imre Deak820c1982013-12-17 14:46:36 +02002732 ilk_compute_wm_results(dev, best_lp_wm, partitioning, &results);
Ville Syrjälä609cede2013-10-09 19:18:03 +03002733
Imre Deak820c1982013-12-17 14:46:36 +02002734 ilk_write_wm_values(dev_priv, &results);
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002735}
2736
Imre Deak820c1982013-12-17 14:46:36 +02002737static void ilk_update_sprite_wm(struct drm_plane *plane,
Ville Syrjäläadf3d352013-08-06 22:24:11 +03002738 struct drm_crtc *crtc,
Paulo Zanoni526682e2013-05-24 11:59:18 -03002739 uint32_t sprite_width, int pixel_size,
Ville Syrjäläbdd57d02013-07-05 11:57:13 +03002740 bool enabled, bool scaled)
Paulo Zanoni526682e2013-05-24 11:59:18 -03002741{
Ville Syrjälä8553c182013-12-05 15:51:39 +02002742 struct drm_device *dev = plane->dev;
Ville Syrjäläadf3d352013-08-06 22:24:11 +03002743 struct intel_plane *intel_plane = to_intel_plane(plane);
Paulo Zanoni526682e2013-05-24 11:59:18 -03002744
Ville Syrjäläadf3d352013-08-06 22:24:11 +03002745 intel_plane->wm.enabled = enabled;
2746 intel_plane->wm.scaled = scaled;
2747 intel_plane->wm.horiz_pixels = sprite_width;
2748 intel_plane->wm.bytes_per_pixel = pixel_size;
Paulo Zanoni526682e2013-05-24 11:59:18 -03002749
Ville Syrjälä8553c182013-12-05 15:51:39 +02002750 /*
2751 * IVB workaround: must disable low power watermarks for at least
2752 * one frame before enabling scaling. LP watermarks can be re-enabled
2753 * when scaling is disabled.
2754 *
2755 * WaCxSRDisabledForSpriteScaling:ivb
2756 */
2757 if (IS_IVYBRIDGE(dev) && scaled && ilk_disable_lp_wm(dev))
2758 intel_wait_for_vblank(dev, intel_plane->pipe);
2759
Imre Deak820c1982013-12-17 14:46:36 +02002760 ilk_update_wm(crtc);
Paulo Zanoni526682e2013-05-24 11:59:18 -03002761}
2762
Ville Syrjälä243e6a42013-10-14 14:55:24 +03002763static void ilk_pipe_wm_get_hw_state(struct drm_crtc *crtc)
2764{
2765 struct drm_device *dev = crtc->dev;
2766 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak820c1982013-12-17 14:46:36 +02002767 struct ilk_wm_values *hw = &dev_priv->wm.hw;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03002768 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2769 struct intel_pipe_wm *active = &intel_crtc->wm.active;
2770 enum pipe pipe = intel_crtc->pipe;
2771 static const unsigned int wm0_pipe_reg[] = {
2772 [PIPE_A] = WM0_PIPEA_ILK,
2773 [PIPE_B] = WM0_PIPEB_ILK,
2774 [PIPE_C] = WM0_PIPEC_IVB,
2775 };
2776
2777 hw->wm_pipe[pipe] = I915_READ(wm0_pipe_reg[pipe]);
Ville Syrjäläa42a5712014-01-07 16:14:08 +02002778 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Ville Syrjäläce0e0712013-12-05 15:51:36 +02002779 hw->wm_linetime[pipe] = I915_READ(PIPE_WM_LINETIME(pipe));
Ville Syrjälä243e6a42013-10-14 14:55:24 +03002780
Ville Syrjälä2a44b762014-03-07 18:32:09 +02002781 active->pipe_enabled = intel_crtc_active(crtc);
2782
2783 if (active->pipe_enabled) {
Ville Syrjälä243e6a42013-10-14 14:55:24 +03002784 u32 tmp = hw->wm_pipe[pipe];
2785
2786 /*
2787 * For active pipes LP0 watermark is marked as
2788 * enabled, and LP1+ watermaks as disabled since
2789 * we can't really reverse compute them in case
2790 * multiple pipes are active.
2791 */
2792 active->wm[0].enable = true;
2793 active->wm[0].pri_val = (tmp & WM0_PIPE_PLANE_MASK) >> WM0_PIPE_PLANE_SHIFT;
2794 active->wm[0].spr_val = (tmp & WM0_PIPE_SPRITE_MASK) >> WM0_PIPE_SPRITE_SHIFT;
2795 active->wm[0].cur_val = tmp & WM0_PIPE_CURSOR_MASK;
2796 active->linetime = hw->wm_linetime[pipe];
2797 } else {
2798 int level, max_level = ilk_wm_max_level(dev);
2799
2800 /*
2801 * For inactive pipes, all watermark levels
2802 * should be marked as enabled but zeroed,
2803 * which is what we'd compute them to.
2804 */
2805 for (level = 0; level <= max_level; level++)
2806 active->wm[level].enable = true;
2807 }
2808}
2809
2810void ilk_wm_get_hw_state(struct drm_device *dev)
2811{
2812 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak820c1982013-12-17 14:46:36 +02002813 struct ilk_wm_values *hw = &dev_priv->wm.hw;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03002814 struct drm_crtc *crtc;
2815
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01002816 for_each_crtc(dev, crtc)
Ville Syrjälä243e6a42013-10-14 14:55:24 +03002817 ilk_pipe_wm_get_hw_state(crtc);
2818
2819 hw->wm_lp[0] = I915_READ(WM1_LP_ILK);
2820 hw->wm_lp[1] = I915_READ(WM2_LP_ILK);
2821 hw->wm_lp[2] = I915_READ(WM3_LP_ILK);
2822
2823 hw->wm_lp_spr[0] = I915_READ(WM1S_LP_ILK);
Ville Syrjäläcfa76982014-03-07 18:32:08 +02002824 if (INTEL_INFO(dev)->gen >= 7) {
2825 hw->wm_lp_spr[1] = I915_READ(WM2S_LP_IVB);
2826 hw->wm_lp_spr[2] = I915_READ(WM3S_LP_IVB);
2827 }
Ville Syrjälä243e6a42013-10-14 14:55:24 +03002828
Ville Syrjäläa42a5712014-01-07 16:14:08 +02002829 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Ville Syrjäläac9545f2013-12-05 15:51:28 +02002830 hw->partitioning = (I915_READ(WM_MISC) & WM_MISC_DATA_PARTITION_5_6) ?
2831 INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
2832 else if (IS_IVYBRIDGE(dev))
2833 hw->partitioning = (I915_READ(DISP_ARB_CTL2) & DISP_DATA_PARTITION_5_6) ?
2834 INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03002835
2836 hw->enable_fbc_wm =
2837 !(I915_READ(DISP_ARB_CTL) & DISP_FBC_WM_DIS);
2838}
2839
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002840/**
2841 * intel_update_watermarks - update FIFO watermark values based on current modes
2842 *
2843 * Calculate watermark values for the various WM regs based on current mode
2844 * and plane configuration.
2845 *
2846 * There are several cases to deal with here:
2847 * - normal (i.e. non-self-refresh)
2848 * - self-refresh (SR) mode
2849 * - lines are large relative to FIFO size (buffer can hold up to 2)
2850 * - lines are small relative to FIFO size (buffer can hold more than 2
2851 * lines), so need to account for TLB latency
2852 *
2853 * The normal calculation is:
2854 * watermark = dotclock * bytes per pixel * latency
2855 * where latency is platform & configuration dependent (we assume pessimal
2856 * values here).
2857 *
2858 * The SR calculation is:
2859 * watermark = (trunc(latency/line time)+1) * surface width *
2860 * bytes per pixel
2861 * where
2862 * line time = htotal / dotclock
2863 * surface width = hdisplay for normal plane and 64 for cursor
2864 * and latency is assumed to be high, as above.
2865 *
2866 * The final value programmed to the register should always be rounded up,
2867 * and include an extra 2 entries to account for clock crossings.
2868 *
2869 * We don't use the sprite, so we can ignore that. And on Crestline we have
2870 * to set the non-SR watermarks to 8.
2871 */
Ville Syrjälä46ba6142013-09-10 11:40:40 +03002872void intel_update_watermarks(struct drm_crtc *crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002873{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03002874 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002875
2876 if (dev_priv->display.update_wm)
Ville Syrjälä46ba6142013-09-10 11:40:40 +03002877 dev_priv->display.update_wm(crtc);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002878}
2879
Ville Syrjäläadf3d352013-08-06 22:24:11 +03002880void intel_update_sprite_watermarks(struct drm_plane *plane,
2881 struct drm_crtc *crtc,
Paulo Zanoni4c4ff432013-05-24 11:59:17 -03002882 uint32_t sprite_width, int pixel_size,
Ville Syrjälä39db4a42013-08-06 22:24:00 +03002883 bool enabled, bool scaled)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002884{
Ville Syrjäläadf3d352013-08-06 22:24:11 +03002885 struct drm_i915_private *dev_priv = plane->dev->dev_private;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002886
2887 if (dev_priv->display.update_sprite_wm)
Ville Syrjäläadf3d352013-08-06 22:24:11 +03002888 dev_priv->display.update_sprite_wm(plane, crtc, sprite_width,
Ville Syrjälä39db4a42013-08-06 22:24:00 +03002889 pixel_size, enabled, scaled);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002890}
2891
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002892static struct drm_i915_gem_object *
2893intel_alloc_context_page(struct drm_device *dev)
2894{
2895 struct drm_i915_gem_object *ctx;
2896 int ret;
2897
2898 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2899
2900 ctx = i915_gem_alloc_object(dev, 4096);
2901 if (!ctx) {
2902 DRM_DEBUG("failed to alloc power context, RC6 disabled\n");
2903 return NULL;
2904 }
2905
Daniel Vetterc69766f2014-02-14 14:01:17 +01002906 ret = i915_gem_obj_ggtt_pin(ctx, 4096, 0);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002907 if (ret) {
2908 DRM_ERROR("failed to pin power context: %d\n", ret);
2909 goto err_unref;
2910 }
2911
2912 ret = i915_gem_object_set_to_gtt_domain(ctx, 1);
2913 if (ret) {
2914 DRM_ERROR("failed to set-domain on power context: %d\n", ret);
2915 goto err_unpin;
2916 }
2917
2918 return ctx;
2919
2920err_unpin:
Ben Widawskyd7f46fc2013-12-06 14:10:55 -08002921 i915_gem_object_ggtt_unpin(ctx);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002922err_unref:
2923 drm_gem_object_unreference(&ctx->base);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002924 return NULL;
2925}
2926
Daniel Vetter92703882012-08-09 16:46:01 +02002927/**
2928 * Lock protecting IPS related data structures
Daniel Vetter92703882012-08-09 16:46:01 +02002929 */
2930DEFINE_SPINLOCK(mchdev_lock);
2931
2932/* Global for IPS driver to get at the current i915 device. Protected by
2933 * mchdev_lock. */
2934static struct drm_i915_private *i915_mch_dev;
2935
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002936bool ironlake_set_drps(struct drm_device *dev, u8 val)
2937{
2938 struct drm_i915_private *dev_priv = dev->dev_private;
2939 u16 rgvswctl;
2940
Daniel Vetter92703882012-08-09 16:46:01 +02002941 assert_spin_locked(&mchdev_lock);
2942
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002943 rgvswctl = I915_READ16(MEMSWCTL);
2944 if (rgvswctl & MEMCTL_CMD_STS) {
2945 DRM_DEBUG("gpu busy, RCS change rejected\n");
2946 return false; /* still busy with another command */
2947 }
2948
2949 rgvswctl = (MEMCTL_CMD_CHFREQ << MEMCTL_CMD_SHIFT) |
2950 (val << MEMCTL_FREQ_SHIFT) | MEMCTL_SFCAVM;
2951 I915_WRITE16(MEMSWCTL, rgvswctl);
2952 POSTING_READ16(MEMSWCTL);
2953
2954 rgvswctl |= MEMCTL_CMD_STS;
2955 I915_WRITE16(MEMSWCTL, rgvswctl);
2956
2957 return true;
2958}
2959
Daniel Vetter8090c6b2012-06-24 16:42:32 +02002960static void ironlake_enable_drps(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002961{
2962 struct drm_i915_private *dev_priv = dev->dev_private;
2963 u32 rgvmodectl = I915_READ(MEMMODECTL);
2964 u8 fmax, fmin, fstart, vstart;
2965
Daniel Vetter92703882012-08-09 16:46:01 +02002966 spin_lock_irq(&mchdev_lock);
2967
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002968 /* Enable temp reporting */
2969 I915_WRITE16(PMMISC, I915_READ(PMMISC) | MCPPCE_EN);
2970 I915_WRITE16(TSC1, I915_READ(TSC1) | TSE);
2971
2972 /* 100ms RC evaluation intervals */
2973 I915_WRITE(RCUPEI, 100000);
2974 I915_WRITE(RCDNEI, 100000);
2975
2976 /* Set max/min thresholds to 90ms and 80ms respectively */
2977 I915_WRITE(RCBMAXAVG, 90000);
2978 I915_WRITE(RCBMINAVG, 80000);
2979
2980 I915_WRITE(MEMIHYST, 1);
2981
2982 /* Set up min, max, and cur for interrupt handling */
2983 fmax = (rgvmodectl & MEMMODE_FMAX_MASK) >> MEMMODE_FMAX_SHIFT;
2984 fmin = (rgvmodectl & MEMMODE_FMIN_MASK);
2985 fstart = (rgvmodectl & MEMMODE_FSTART_MASK) >>
2986 MEMMODE_FSTART_SHIFT;
2987
2988 vstart = (I915_READ(PXVFREQ_BASE + (fstart * 4)) & PXVFREQ_PX_MASK) >>
2989 PXVFREQ_PX_SHIFT;
2990
Daniel Vetter20e4d402012-08-08 23:35:39 +02002991 dev_priv->ips.fmax = fmax; /* IPS callback will increase this */
2992 dev_priv->ips.fstart = fstart;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002993
Daniel Vetter20e4d402012-08-08 23:35:39 +02002994 dev_priv->ips.max_delay = fstart;
2995 dev_priv->ips.min_delay = fmin;
2996 dev_priv->ips.cur_delay = fstart;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002997
2998 DRM_DEBUG_DRIVER("fmax: %d, fmin: %d, fstart: %d\n",
2999 fmax, fmin, fstart);
3000
3001 I915_WRITE(MEMINTREN, MEMINT_CX_SUPR_EN | MEMINT_EVAL_CHG_EN);
3002
3003 /*
3004 * Interrupts will be enabled in ironlake_irq_postinstall
3005 */
3006
3007 I915_WRITE(VIDSTART, vstart);
3008 POSTING_READ(VIDSTART);
3009
3010 rgvmodectl |= MEMMODE_SWMODE_EN;
3011 I915_WRITE(MEMMODECTL, rgvmodectl);
3012
Daniel Vetter92703882012-08-09 16:46:01 +02003013 if (wait_for_atomic((I915_READ(MEMSWCTL) & MEMCTL_CMD_STS) == 0, 10))
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003014 DRM_ERROR("stuck trying to change perf mode\n");
Daniel Vetter92703882012-08-09 16:46:01 +02003015 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003016
3017 ironlake_set_drps(dev, fstart);
3018
Daniel Vetter20e4d402012-08-08 23:35:39 +02003019 dev_priv->ips.last_count1 = I915_READ(0x112e4) + I915_READ(0x112e8) +
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003020 I915_READ(0x112e0);
Daniel Vetter20e4d402012-08-08 23:35:39 +02003021 dev_priv->ips.last_time1 = jiffies_to_msecs(jiffies);
3022 dev_priv->ips.last_count2 = I915_READ(0x112f4);
3023 getrawmonotonic(&dev_priv->ips.last_time2);
Daniel Vetter92703882012-08-09 16:46:01 +02003024
3025 spin_unlock_irq(&mchdev_lock);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003026}
3027
Daniel Vetter8090c6b2012-06-24 16:42:32 +02003028static void ironlake_disable_drps(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003029{
3030 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter92703882012-08-09 16:46:01 +02003031 u16 rgvswctl;
3032
3033 spin_lock_irq(&mchdev_lock);
3034
3035 rgvswctl = I915_READ16(MEMSWCTL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003036
3037 /* Ack interrupts, disable EFC interrupt */
3038 I915_WRITE(MEMINTREN, I915_READ(MEMINTREN) & ~MEMINT_EVAL_CHG_EN);
3039 I915_WRITE(MEMINTRSTS, MEMINT_EVAL_CHG);
3040 I915_WRITE(DEIER, I915_READ(DEIER) & ~DE_PCU_EVENT);
3041 I915_WRITE(DEIIR, DE_PCU_EVENT);
3042 I915_WRITE(DEIMR, I915_READ(DEIMR) | DE_PCU_EVENT);
3043
3044 /* Go back to the starting frequency */
Daniel Vetter20e4d402012-08-08 23:35:39 +02003045 ironlake_set_drps(dev, dev_priv->ips.fstart);
Daniel Vetter92703882012-08-09 16:46:01 +02003046 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003047 rgvswctl |= MEMCTL_CMD_STS;
3048 I915_WRITE(MEMSWCTL, rgvswctl);
Daniel Vetter92703882012-08-09 16:46:01 +02003049 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003050
Daniel Vetter92703882012-08-09 16:46:01 +02003051 spin_unlock_irq(&mchdev_lock);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003052}
3053
Daniel Vetteracbe9472012-07-26 11:50:05 +02003054/* There's a funny hw issue where the hw returns all 0 when reading from
3055 * GEN6_RP_INTERRUPT_LIMITS. Hence we always need to compute the desired value
3056 * ourselves, instead of doing a rmw cycle (which might result in us clearing
3057 * all limits and the gpu stuck at whatever frequency it is at atm).
3058 */
Chris Wilson6917c7b2013-11-06 13:56:26 -02003059static u32 gen6_rps_limits(struct drm_i915_private *dev_priv, u8 val)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003060{
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01003061 u32 limits;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003062
Daniel Vetter20b46e52012-07-26 11:16:14 +02003063 /* Only set the down limit when we've reached the lowest level to avoid
3064 * getting more interrupts, otherwise leave this clear. This prevents a
3065 * race in the hw when coming out of rc6: There's a tiny window where
3066 * the hw runs at the minimal clock before selecting the desired
3067 * frequency, if the down threshold expires in that window we will not
3068 * receive a down interrupt. */
Ben Widawskyb39fb292014-03-19 18:31:11 -07003069 limits = dev_priv->rps.max_freq_softlimit << 24;
3070 if (val <= dev_priv->rps.min_freq_softlimit)
3071 limits |= dev_priv->rps.min_freq_softlimit << 16;
Daniel Vetter20b46e52012-07-26 11:16:14 +02003072
3073 return limits;
3074}
3075
Chris Wilsondd75fdc2013-09-25 17:34:57 +01003076static void gen6_set_rps_thresholds(struct drm_i915_private *dev_priv, u8 val)
3077{
3078 int new_power;
3079
3080 new_power = dev_priv->rps.power;
3081 switch (dev_priv->rps.power) {
3082 case LOW_POWER:
Ben Widawskyb39fb292014-03-19 18:31:11 -07003083 if (val > dev_priv->rps.efficient_freq + 1 && val > dev_priv->rps.cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01003084 new_power = BETWEEN;
3085 break;
3086
3087 case BETWEEN:
Ben Widawskyb39fb292014-03-19 18:31:11 -07003088 if (val <= dev_priv->rps.efficient_freq && val < dev_priv->rps.cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01003089 new_power = LOW_POWER;
Ben Widawskyb39fb292014-03-19 18:31:11 -07003090 else if (val >= dev_priv->rps.rp0_freq && val > dev_priv->rps.cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01003091 new_power = HIGH_POWER;
3092 break;
3093
3094 case HIGH_POWER:
Ben Widawskyb39fb292014-03-19 18:31:11 -07003095 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 +01003096 new_power = BETWEEN;
3097 break;
3098 }
3099 /* Max/min bins are special */
Ben Widawskyb39fb292014-03-19 18:31:11 -07003100 if (val == dev_priv->rps.min_freq_softlimit)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01003101 new_power = LOW_POWER;
Ben Widawskyb39fb292014-03-19 18:31:11 -07003102 if (val == dev_priv->rps.max_freq_softlimit)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01003103 new_power = HIGH_POWER;
3104 if (new_power == dev_priv->rps.power)
3105 return;
3106
3107 /* Note the units here are not exactly 1us, but 1280ns. */
3108 switch (new_power) {
3109 case LOW_POWER:
3110 /* Upclock if more than 95% busy over 16ms */
3111 I915_WRITE(GEN6_RP_UP_EI, 12500);
3112 I915_WRITE(GEN6_RP_UP_THRESHOLD, 11800);
3113
3114 /* Downclock if less than 85% busy over 32ms */
3115 I915_WRITE(GEN6_RP_DOWN_EI, 25000);
3116 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 21250);
3117
3118 I915_WRITE(GEN6_RP_CONTROL,
3119 GEN6_RP_MEDIA_TURBO |
3120 GEN6_RP_MEDIA_HW_NORMAL_MODE |
3121 GEN6_RP_MEDIA_IS_GFX |
3122 GEN6_RP_ENABLE |
3123 GEN6_RP_UP_BUSY_AVG |
3124 GEN6_RP_DOWN_IDLE_AVG);
3125 break;
3126
3127 case BETWEEN:
3128 /* Upclock if more than 90% busy over 13ms */
3129 I915_WRITE(GEN6_RP_UP_EI, 10250);
3130 I915_WRITE(GEN6_RP_UP_THRESHOLD, 9225);
3131
3132 /* Downclock if less than 75% busy over 32ms */
3133 I915_WRITE(GEN6_RP_DOWN_EI, 25000);
3134 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 18750);
3135
3136 I915_WRITE(GEN6_RP_CONTROL,
3137 GEN6_RP_MEDIA_TURBO |
3138 GEN6_RP_MEDIA_HW_NORMAL_MODE |
3139 GEN6_RP_MEDIA_IS_GFX |
3140 GEN6_RP_ENABLE |
3141 GEN6_RP_UP_BUSY_AVG |
3142 GEN6_RP_DOWN_IDLE_AVG);
3143 break;
3144
3145 case HIGH_POWER:
3146 /* Upclock if more than 85% busy over 10ms */
3147 I915_WRITE(GEN6_RP_UP_EI, 8000);
3148 I915_WRITE(GEN6_RP_UP_THRESHOLD, 6800);
3149
3150 /* Downclock if less than 60% busy over 32ms */
3151 I915_WRITE(GEN6_RP_DOWN_EI, 25000);
3152 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 15000);
3153
3154 I915_WRITE(GEN6_RP_CONTROL,
3155 GEN6_RP_MEDIA_TURBO |
3156 GEN6_RP_MEDIA_HW_NORMAL_MODE |
3157 GEN6_RP_MEDIA_IS_GFX |
3158 GEN6_RP_ENABLE |
3159 GEN6_RP_UP_BUSY_AVG |
3160 GEN6_RP_DOWN_IDLE_AVG);
3161 break;
3162 }
3163
3164 dev_priv->rps.power = new_power;
3165 dev_priv->rps.last_adj = 0;
3166}
3167
Chris Wilson2876ce72014-03-28 08:03:34 +00003168static u32 gen6_rps_pm_mask(struct drm_i915_private *dev_priv, u8 val)
3169{
3170 u32 mask = 0;
3171
3172 if (val > dev_priv->rps.min_freq_softlimit)
3173 mask |= GEN6_PM_RP_DOWN_THRESHOLD | GEN6_PM_RP_DOWN_TIMEOUT;
3174 if (val < dev_priv->rps.max_freq_softlimit)
3175 mask |= GEN6_PM_RP_UP_THRESHOLD;
3176
3177 /* IVB and SNB hard hangs on looping batchbuffer
3178 * if GEN6_PM_UP_EI_EXPIRED is masked.
3179 */
3180 if (INTEL_INFO(dev_priv->dev)->gen <= 7 && !IS_HASWELL(dev_priv->dev))
3181 mask |= GEN6_PM_RP_UP_EI_EXPIRED;
3182
Deepak Sbaccd452014-05-15 20:58:09 +03003183 if (IS_GEN8(dev_priv->dev))
3184 mask |= GEN8_PMINTR_REDIRECT_TO_NON_DISP;
3185
Chris Wilson2876ce72014-03-28 08:03:34 +00003186 return ~mask;
3187}
3188
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06003189/* gen6_set_rps is called to update the frequency request, but should also be
3190 * called when the range (min_delay and max_delay) is modified so that we can
3191 * update the GEN6_RP_INTERRUPT_LIMITS register accordingly. */
Daniel Vetter20b46e52012-07-26 11:16:14 +02003192void gen6_set_rps(struct drm_device *dev, u8 val)
3193{
3194 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01003195
Jesse Barnes4fc688c2012-11-02 11:14:01 -07003196 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Ben Widawskyb39fb292014-03-19 18:31:11 -07003197 WARN_ON(val > dev_priv->rps.max_freq_softlimit);
3198 WARN_ON(val < dev_priv->rps.min_freq_softlimit);
Daniel Vetter004777c2012-08-09 15:07:01 +02003199
Chris Wilsoneb64cad2014-03-27 08:24:20 +00003200 /* min/max delay may still have been modified so be sure to
3201 * write the limits value.
3202 */
3203 if (val != dev_priv->rps.cur_freq) {
3204 gen6_set_rps_thresholds(dev_priv, val);
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06003205
Ben Widawsky50e6a2a2014-03-31 17:16:43 -07003206 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Chris Wilsoneb64cad2014-03-27 08:24:20 +00003207 I915_WRITE(GEN6_RPNSWREQ,
3208 HSW_FREQUENCY(val));
3209 else
3210 I915_WRITE(GEN6_RPNSWREQ,
3211 GEN6_FREQUENCY(val) |
3212 GEN6_OFFSET(0) |
3213 GEN6_AGGRESSIVE_TURBO);
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06003214 }
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01003215
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01003216 /* Make sure we continue to get interrupts
3217 * until we hit the minimum or maximum frequencies.
3218 */
Chris Wilsoneb64cad2014-03-27 08:24:20 +00003219 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS, gen6_rps_limits(dev_priv, val));
Chris Wilson2876ce72014-03-28 08:03:34 +00003220 I915_WRITE(GEN6_PMINTRMSK, gen6_rps_pm_mask(dev_priv, val));
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01003221
Ben Widawskyd5570a72012-09-07 19:43:41 -07003222 POSTING_READ(GEN6_RPNSWREQ);
3223
Ben Widawskyb39fb292014-03-19 18:31:11 -07003224 dev_priv->rps.cur_freq = val;
Daniel Vetterbe2cde9a2012-08-30 13:26:48 +02003225 trace_intel_gpu_freq_change(val * 50);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003226}
3227
Deepak S76c3552f2014-01-30 23:08:16 +05303228/* vlv_set_rps_idle: Set the frequency to Rpn if Gfx clocks are down
3229 *
3230 * * If Gfx is Idle, then
3231 * 1. Mask Turbo interrupts
3232 * 2. Bring up Gfx clock
3233 * 3. Change the freq to Rpn and wait till P-Unit updates freq
3234 * 4. Clear the Force GFX CLK ON bit so that Gfx can down
3235 * 5. Unmask Turbo interrupts
3236*/
3237static void vlv_set_rps_idle(struct drm_i915_private *dev_priv)
3238{
Deepak S5549d252014-06-28 11:26:11 +05303239 struct drm_device *dev = dev_priv->dev;
3240
3241 /* Latest VLV doesn't need to force the gfx clock */
3242 if (dev->pdev->revision >= 0xd) {
3243 valleyview_set_rps(dev_priv->dev, dev_priv->rps.min_freq_softlimit);
3244 return;
3245 }
3246
Deepak S76c3552f2014-01-30 23:08:16 +05303247 /*
3248 * When we are idle. Drop to min voltage state.
3249 */
3250
Ben Widawskyb39fb292014-03-19 18:31:11 -07003251 if (dev_priv->rps.cur_freq <= dev_priv->rps.min_freq_softlimit)
Deepak S76c3552f2014-01-30 23:08:16 +05303252 return;
3253
3254 /* Mask turbo interrupt so that they will not come in between */
3255 I915_WRITE(GEN6_PMINTRMSK, 0xffffffff);
3256
Imre Deak650ad972014-04-18 16:35:02 +03003257 vlv_force_gfx_clock(dev_priv, true);
Deepak S76c3552f2014-01-30 23:08:16 +05303258
Ben Widawskyb39fb292014-03-19 18:31:11 -07003259 dev_priv->rps.cur_freq = dev_priv->rps.min_freq_softlimit;
Deepak S76c3552f2014-01-30 23:08:16 +05303260
3261 vlv_punit_write(dev_priv, PUNIT_REG_GPU_FREQ_REQ,
Ben Widawskyb39fb292014-03-19 18:31:11 -07003262 dev_priv->rps.min_freq_softlimit);
Deepak S76c3552f2014-01-30 23:08:16 +05303263
3264 if (wait_for(((vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS))
3265 & GENFREQSTATUS) == 0, 5))
3266 DRM_ERROR("timed out waiting for Punit\n");
3267
Imre Deak650ad972014-04-18 16:35:02 +03003268 vlv_force_gfx_clock(dev_priv, false);
Deepak S76c3552f2014-01-30 23:08:16 +05303269
Chris Wilson2876ce72014-03-28 08:03:34 +00003270 I915_WRITE(GEN6_PMINTRMSK,
3271 gen6_rps_pm_mask(dev_priv, dev_priv->rps.cur_freq));
Deepak S76c3552f2014-01-30 23:08:16 +05303272}
3273
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003274void gen6_rps_idle(struct drm_i915_private *dev_priv)
3275{
Damien Lespiau691bb712013-12-12 14:36:36 +00003276 struct drm_device *dev = dev_priv->dev;
3277
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003278 mutex_lock(&dev_priv->rps.hw_lock);
Chris Wilsonc0951f02013-10-10 21:58:50 +01003279 if (dev_priv->rps.enabled) {
Damien Lespiau691bb712013-12-12 14:36:36 +00003280 if (IS_VALLEYVIEW(dev))
Deepak S76c3552f2014-01-30 23:08:16 +05303281 vlv_set_rps_idle(dev_priv);
Chris Wilsonc0951f02013-10-10 21:58:50 +01003282 else
Ben Widawskyb39fb292014-03-19 18:31:11 -07003283 gen6_set_rps(dev_priv->dev, dev_priv->rps.min_freq_softlimit);
Chris Wilsonc0951f02013-10-10 21:58:50 +01003284 dev_priv->rps.last_adj = 0;
3285 }
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003286 mutex_unlock(&dev_priv->rps.hw_lock);
3287}
3288
3289void gen6_rps_boost(struct drm_i915_private *dev_priv)
3290{
Damien Lespiau691bb712013-12-12 14:36:36 +00003291 struct drm_device *dev = dev_priv->dev;
3292
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003293 mutex_lock(&dev_priv->rps.hw_lock);
Chris Wilsonc0951f02013-10-10 21:58:50 +01003294 if (dev_priv->rps.enabled) {
Damien Lespiau691bb712013-12-12 14:36:36 +00003295 if (IS_VALLEYVIEW(dev))
Ben Widawskyb39fb292014-03-19 18:31:11 -07003296 valleyview_set_rps(dev_priv->dev, dev_priv->rps.max_freq_softlimit);
Chris Wilsonc0951f02013-10-10 21:58:50 +01003297 else
Ben Widawskyb39fb292014-03-19 18:31:11 -07003298 gen6_set_rps(dev_priv->dev, dev_priv->rps.max_freq_softlimit);
Chris Wilsonc0951f02013-10-10 21:58:50 +01003299 dev_priv->rps.last_adj = 0;
3300 }
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003301 mutex_unlock(&dev_priv->rps.hw_lock);
3302}
3303
Jesse Barnes0a073b82013-04-17 15:54:58 -07003304void valleyview_set_rps(struct drm_device *dev, u8 val)
3305{
3306 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä7a670922013-06-25 19:21:06 +03003307
Jesse Barnes0a073b82013-04-17 15:54:58 -07003308 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Ben Widawskyb39fb292014-03-19 18:31:11 -07003309 WARN_ON(val > dev_priv->rps.max_freq_softlimit);
3310 WARN_ON(val < dev_priv->rps.min_freq_softlimit);
Jesse Barnes0a073b82013-04-17 15:54:58 -07003311
Ville Syrjälä73008b92013-06-25 19:21:01 +03003312 DRM_DEBUG_DRIVER("GPU freq request from %d MHz (%u) to %d MHz (%u)\n",
Ben Widawskyb39fb292014-03-19 18:31:11 -07003313 vlv_gpu_freq(dev_priv, dev_priv->rps.cur_freq),
3314 dev_priv->rps.cur_freq,
Ville Syrjälä2ec38152013-11-05 22:42:29 +02003315 vlv_gpu_freq(dev_priv, val), val);
Jesse Barnes0a073b82013-04-17 15:54:58 -07003316
Chris Wilson2876ce72014-03-28 08:03:34 +00003317 if (val != dev_priv->rps.cur_freq)
3318 vlv_punit_write(dev_priv, PUNIT_REG_GPU_FREQ_REQ, val);
Jesse Barnes0a073b82013-04-17 15:54:58 -07003319
Imre Deak09c87db2014-04-03 20:02:42 +03003320 I915_WRITE(GEN6_PMINTRMSK, gen6_rps_pm_mask(dev_priv, val));
Jesse Barnes0a073b82013-04-17 15:54:58 -07003321
Ben Widawskyb39fb292014-03-19 18:31:11 -07003322 dev_priv->rps.cur_freq = val;
Ville Syrjälä2ec38152013-11-05 22:42:29 +02003323 trace_intel_gpu_freq_change(vlv_gpu_freq(dev_priv, val));
Jesse Barnes0a073b82013-04-17 15:54:58 -07003324}
3325
Ben Widawsky09610212014-05-15 20:58:08 +03003326static void gen8_disable_rps_interrupts(struct drm_device *dev)
3327{
3328 struct drm_i915_private *dev_priv = dev->dev_private;
3329
Mika Kuoppala992f1912014-05-16 13:44:12 +03003330 I915_WRITE(GEN6_PMINTRMSK, ~GEN8_PMINTR_REDIRECT_TO_NON_DISP);
Ben Widawsky09610212014-05-15 20:58:08 +03003331 I915_WRITE(GEN8_GT_IER(2), I915_READ(GEN8_GT_IER(2)) &
3332 ~dev_priv->pm_rps_events);
3333 /* Complete PM interrupt masking here doesn't race with the rps work
3334 * item again unmasking PM interrupts because that is using a different
3335 * register (GEN8_GT_IMR(2)) to mask PM interrupts. The only risk is in
3336 * leaving stale bits in GEN8_GT_IIR(2) and GEN8_GT_IMR(2) which
3337 * gen8_enable_rps will clean up. */
3338
3339 spin_lock_irq(&dev_priv->irq_lock);
3340 dev_priv->rps.pm_iir = 0;
3341 spin_unlock_irq(&dev_priv->irq_lock);
3342
3343 I915_WRITE(GEN8_GT_IIR(2), dev_priv->pm_rps_events);
3344}
3345
Daniel Vetter44fc7d52013-07-12 22:43:27 +02003346static void gen6_disable_rps_interrupts(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003347{
3348 struct drm_i915_private *dev_priv = dev->dev_private;
3349
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003350 I915_WRITE(GEN6_PMINTRMSK, 0xffffffff);
Deepak Sa6706b42014-03-15 20:23:22 +05303351 I915_WRITE(GEN6_PMIER, I915_READ(GEN6_PMIER) &
3352 ~dev_priv->pm_rps_events);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003353 /* Complete PM interrupt masking here doesn't race with the rps work
3354 * item again unmasking PM interrupts because that is using a different
3355 * register (PMIMR) to mask PM interrupts. The only risk is in leaving
3356 * stale bits in PMIIR and PMIMR which gen6_enable_rps will clean up. */
3357
Daniel Vetter59cdb632013-07-04 23:35:28 +02003358 spin_lock_irq(&dev_priv->irq_lock);
Daniel Vetterc6a828d2012-08-08 23:35:35 +02003359 dev_priv->rps.pm_iir = 0;
Daniel Vetter59cdb632013-07-04 23:35:28 +02003360 spin_unlock_irq(&dev_priv->irq_lock);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003361
Deepak Sa6706b42014-03-15 20:23:22 +05303362 I915_WRITE(GEN6_PMIIR, dev_priv->pm_rps_events);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003363}
3364
Daniel Vetter44fc7d52013-07-12 22:43:27 +02003365static void gen6_disable_rps(struct drm_device *dev)
3366{
3367 struct drm_i915_private *dev_priv = dev->dev_private;
3368
3369 I915_WRITE(GEN6_RC_CONTROL, 0);
3370 I915_WRITE(GEN6_RPNSWREQ, 1 << 31);
3371
Ben Widawsky09610212014-05-15 20:58:08 +03003372 if (IS_BROADWELL(dev))
3373 gen8_disable_rps_interrupts(dev);
3374 else
3375 gen6_disable_rps_interrupts(dev);
Daniel Vetter44fc7d52013-07-12 22:43:27 +02003376}
3377
Deepak S38807742014-05-23 21:00:15 +05303378static void cherryview_disable_rps(struct drm_device *dev)
3379{
3380 struct drm_i915_private *dev_priv = dev->dev_private;
3381
3382 I915_WRITE(GEN6_RC_CONTROL, 0);
3383}
3384
Jesse Barnesd20d4f02013-04-23 10:09:28 -07003385static void valleyview_disable_rps(struct drm_device *dev)
3386{
3387 struct drm_i915_private *dev_priv = dev->dev_private;
3388
3389 I915_WRITE(GEN6_RC_CONTROL, 0);
Jesse Barnesd20d4f02013-04-23 10:09:28 -07003390
Daniel Vetter44fc7d52013-07-12 22:43:27 +02003391 gen6_disable_rps_interrupts(dev);
Jesse Barnesd20d4f02013-04-23 10:09:28 -07003392}
3393
Ben Widawskydc39fff2013-10-18 12:32:07 -07003394static void intel_print_rc6_info(struct drm_device *dev, u32 mode)
3395{
Imre Deak91ca6892014-04-14 20:24:25 +03003396 if (IS_VALLEYVIEW(dev)) {
3397 if (mode & (GEN7_RC_CTL_TO_MODE | GEN6_RC_CTL_EI_MODE(1)))
3398 mode = GEN6_RC_CTL_RC6_ENABLE;
3399 else
3400 mode = 0;
3401 }
Ben Widawskydc39fff2013-10-18 12:32:07 -07003402 DRM_INFO("Enabling RC6 states: RC6 %s, RC6p %s, RC6pp %s\n",
Ben Widawsky1c79b422014-01-28 20:25:40 -08003403 (mode & GEN6_RC_CTL_RC6_ENABLE) ? "on" : "off",
3404 (mode & GEN6_RC_CTL_RC6p_ENABLE) ? "on" : "off",
3405 (mode & GEN6_RC_CTL_RC6pp_ENABLE) ? "on" : "off");
Ben Widawskydc39fff2013-10-18 12:32:07 -07003406}
3407
Imre Deake6069ca2014-04-18 16:01:02 +03003408static int sanitize_rc6_option(const struct drm_device *dev, int enable_rc6)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003409{
Damien Lespiaueb4926e2013-06-07 17:41:14 +01003410 /* No RC6 before Ironlake */
3411 if (INTEL_INFO(dev)->gen < 5)
3412 return 0;
3413
Imre Deake6069ca2014-04-18 16:01:02 +03003414 /* RC6 is only on Ironlake mobile not on desktop */
3415 if (INTEL_INFO(dev)->gen == 5 && !IS_IRONLAKE_M(dev))
3416 return 0;
3417
Daniel Vetter456470e2012-08-08 23:35:40 +02003418 /* Respect the kernel parameter if it is set */
Imre Deake6069ca2014-04-18 16:01:02 +03003419 if (enable_rc6 >= 0) {
3420 int mask;
3421
3422 if (INTEL_INFO(dev)->gen == 6 || IS_IVYBRIDGE(dev))
3423 mask = INTEL_RC6_ENABLE | INTEL_RC6p_ENABLE |
3424 INTEL_RC6pp_ENABLE;
3425 else
3426 mask = INTEL_RC6_ENABLE;
3427
3428 if ((enable_rc6 & mask) != enable_rc6)
3429 DRM_INFO("Adjusting RC6 mask to %d (requested %d, valid %d)\n",
Mika Kuoppala8fd9c1a92014-05-15 20:58:10 +03003430 enable_rc6 & mask, enable_rc6, mask);
Imre Deake6069ca2014-04-18 16:01:02 +03003431
3432 return enable_rc6 & mask;
3433 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003434
Chris Wilson6567d742012-11-10 10:00:06 +00003435 /* Disable RC6 on Ironlake */
3436 if (INTEL_INFO(dev)->gen == 5)
3437 return 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003438
Ben Widawsky8bade1a2014-01-28 20:25:39 -08003439 if (IS_IVYBRIDGE(dev))
Ben Widawskycca84a12014-01-28 20:25:38 -08003440 return (INTEL_RC6_ENABLE | INTEL_RC6p_ENABLE);
Ben Widawsky8bade1a2014-01-28 20:25:39 -08003441
3442 return INTEL_RC6_ENABLE;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003443}
3444
Imre Deake6069ca2014-04-18 16:01:02 +03003445int intel_enable_rc6(const struct drm_device *dev)
3446{
3447 return i915.enable_rc6;
3448}
3449
Ben Widawsky09610212014-05-15 20:58:08 +03003450static void gen8_enable_rps_interrupts(struct drm_device *dev)
3451{
3452 struct drm_i915_private *dev_priv = dev->dev_private;
3453
3454 spin_lock_irq(&dev_priv->irq_lock);
3455 WARN_ON(dev_priv->rps.pm_iir);
3456 bdw_enable_pm_irq(dev_priv, dev_priv->pm_rps_events);
3457 I915_WRITE(GEN8_GT_IIR(2), dev_priv->pm_rps_events);
3458 spin_unlock_irq(&dev_priv->irq_lock);
3459}
3460
Daniel Vetter44fc7d52013-07-12 22:43:27 +02003461static void gen6_enable_rps_interrupts(struct drm_device *dev)
3462{
3463 struct drm_i915_private *dev_priv = dev->dev_private;
3464
3465 spin_lock_irq(&dev_priv->irq_lock);
Daniel Vettera0b33352013-07-04 23:35:34 +02003466 WARN_ON(dev_priv->rps.pm_iir);
Deepak Sa6706b42014-03-15 20:23:22 +05303467 snb_enable_pm_irq(dev_priv, dev_priv->pm_rps_events);
3468 I915_WRITE(GEN6_PMIIR, dev_priv->pm_rps_events);
Daniel Vetter44fc7d52013-07-12 22:43:27 +02003469 spin_unlock_irq(&dev_priv->irq_lock);
Daniel Vetter44fc7d52013-07-12 22:43:27 +02003470}
3471
Ben Widawsky3280e8b2014-03-31 17:16:42 -07003472static void parse_rp_state_cap(struct drm_i915_private *dev_priv, u32 rp_state_cap)
3473{
3474 /* All of these values are in units of 50MHz */
3475 dev_priv->rps.cur_freq = 0;
3476 /* static values from HW: RP0 < RPe < RP1 < RPn (min_freq) */
3477 dev_priv->rps.rp1_freq = (rp_state_cap >> 8) & 0xff;
3478 dev_priv->rps.rp0_freq = (rp_state_cap >> 0) & 0xff;
3479 dev_priv->rps.min_freq = (rp_state_cap >> 16) & 0xff;
3480 /* XXX: only BYT has a special efficient freq */
3481 dev_priv->rps.efficient_freq = dev_priv->rps.rp1_freq;
3482 /* hw_max = RP0 until we check for overclocking */
3483 dev_priv->rps.max_freq = dev_priv->rps.rp0_freq;
3484
3485 /* Preserve min/max settings in case of re-init */
3486 if (dev_priv->rps.max_freq_softlimit == 0)
3487 dev_priv->rps.max_freq_softlimit = dev_priv->rps.max_freq;
3488
3489 if (dev_priv->rps.min_freq_softlimit == 0)
3490 dev_priv->rps.min_freq_softlimit = dev_priv->rps.min_freq;
3491}
3492
Ben Widawsky6edee7f2013-11-02 21:07:52 -07003493static void gen8_enable_rps(struct drm_device *dev)
3494{
3495 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01003496 struct intel_engine_cs *ring;
Ben Widawsky6edee7f2013-11-02 21:07:52 -07003497 uint32_t rc6_mask = 0, rp_state_cap;
3498 int unused;
3499
3500 /* 1a: Software RC state - RC0 */
3501 I915_WRITE(GEN6_RC_STATE, 0);
3502
3503 /* 1c & 1d: Get forcewake during program sequence. Although the driver
3504 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
Deepak Sc8d9a592013-11-23 14:55:42 +05303505 gen6_gt_force_wake_get(dev_priv, FORCEWAKE_ALL);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07003506
3507 /* 2a: Disable RC states. */
3508 I915_WRITE(GEN6_RC_CONTROL, 0);
3509
3510 rp_state_cap = I915_READ(GEN6_RP_STATE_CAP);
Ben Widawsky3280e8b2014-03-31 17:16:42 -07003511 parse_rp_state_cap(dev_priv, rp_state_cap);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07003512
3513 /* 2b: Program RC6 thresholds.*/
3514 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16);
3515 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
3516 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
3517 for_each_ring(ring, dev_priv, unused)
3518 I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
3519 I915_WRITE(GEN6_RC_SLEEP, 0);
Tom O'Rourke0d68b252014-04-09 11:44:06 -07003520 if (IS_BROADWELL(dev))
3521 I915_WRITE(GEN6_RC6_THRESHOLD, 625); /* 800us/1.28 for TO */
3522 else
3523 I915_WRITE(GEN6_RC6_THRESHOLD, 50000); /* 50/125ms per EI */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07003524
3525 /* 3: Enable RC6 */
3526 if (intel_enable_rc6(dev) & INTEL_RC6_ENABLE)
3527 rc6_mask = GEN6_RC_CTL_RC6_ENABLE;
Ben Widawskyabbf9d22014-01-28 20:25:41 -08003528 intel_print_rc6_info(dev, rc6_mask);
Tom O'Rourke0d68b252014-04-09 11:44:06 -07003529 if (IS_BROADWELL(dev))
3530 I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
3531 GEN7_RC_CTL_TO_MODE |
3532 rc6_mask);
3533 else
3534 I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
3535 GEN6_RC_CTL_EI_MODE(1) |
3536 rc6_mask);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07003537
3538 /* 4 Program defaults and thresholds for RPS*/
Ben Widawskyf9bdc582014-03-31 17:16:41 -07003539 I915_WRITE(GEN6_RPNSWREQ,
3540 HSW_FREQUENCY(dev_priv->rps.rp1_freq));
3541 I915_WRITE(GEN6_RC_VIDEO_FREQ,
3542 HSW_FREQUENCY(dev_priv->rps.rp1_freq));
Ben Widawsky6edee7f2013-11-02 21:07:52 -07003543 /* NB: Docs say 1s, and 1000000 - which aren't equivalent */
3544 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 100000000 / 128); /* 1 second timeout */
3545
3546 /* Docs recommend 900MHz, and 300 MHz respectively */
3547 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS,
Ben Widawskyb39fb292014-03-19 18:31:11 -07003548 dev_priv->rps.max_freq_softlimit << 24 |
3549 dev_priv->rps.min_freq_softlimit << 16);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07003550
3551 I915_WRITE(GEN6_RP_UP_THRESHOLD, 7600000 / 128); /* 76ms busyness per EI, 90% */
3552 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 31300000 / 128); /* 313ms busyness per EI, 70%*/
3553 I915_WRITE(GEN6_RP_UP_EI, 66000); /* 84.48ms, XXX: random? */
3554 I915_WRITE(GEN6_RP_DOWN_EI, 350000); /* 448ms, XXX: random? */
3555
3556 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
3557
3558 /* 5: Enable RPS */
3559 I915_WRITE(GEN6_RP_CONTROL,
3560 GEN6_RP_MEDIA_TURBO |
3561 GEN6_RP_MEDIA_HW_NORMAL_MODE |
Tom O'Rourke7405f422014-06-10 16:26:34 -07003562 GEN6_RP_MEDIA_IS_GFX |
Ben Widawsky6edee7f2013-11-02 21:07:52 -07003563 GEN6_RP_ENABLE |
3564 GEN6_RP_UP_BUSY_AVG |
3565 GEN6_RP_DOWN_IDLE_AVG);
3566
3567 /* 6: Ring frequency + overclocking (our driver does this later */
3568
3569 gen6_set_rps(dev, (I915_READ(GEN6_GT_PERF_STATUS) & 0xff00) >> 8);
3570
Ben Widawsky09610212014-05-15 20:58:08 +03003571 gen8_enable_rps_interrupts(dev);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07003572
Deepak Sc8d9a592013-11-23 14:55:42 +05303573 gen6_gt_force_wake_put(dev_priv, FORCEWAKE_ALL);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07003574}
3575
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02003576static void gen6_enable_rps(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003577{
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02003578 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01003579 struct intel_engine_cs *ring;
Ben Widawsky2a5913a2014-03-19 18:31:13 -07003580 u32 rp_state_cap;
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01003581 u32 gt_perf_status;
Ben Widawskyd060c162014-03-19 18:31:08 -07003582 u32 rc6vids, pcu_mbox = 0, rc6_mask = 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003583 u32 gtfifodbg;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003584 int rc6_mode;
Ben Widawsky42c05262012-09-26 10:34:00 -07003585 int i, ret;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003586
Jesse Barnes4fc688c2012-11-02 11:14:01 -07003587 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02003588
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003589 /* Here begins a magic sequence of register writes to enable
3590 * auto-downclocking.
3591 *
3592 * Perhaps there might be some value in exposing these to
3593 * userspace...
3594 */
3595 I915_WRITE(GEN6_RC_STATE, 0);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003596
3597 /* Clear the DBG now so we don't confuse earlier errors */
3598 if ((gtfifodbg = I915_READ(GTFIFODBG))) {
3599 DRM_ERROR("GT fifo had a previous error %x\n", gtfifodbg);
3600 I915_WRITE(GTFIFODBG, gtfifodbg);
3601 }
3602
Deepak Sc8d9a592013-11-23 14:55:42 +05303603 gen6_gt_force_wake_get(dev_priv, FORCEWAKE_ALL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003604
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01003605 rp_state_cap = I915_READ(GEN6_RP_STATE_CAP);
3606 gt_perf_status = I915_READ(GEN6_GT_PERF_STATUS);
3607
Ben Widawsky3280e8b2014-03-31 17:16:42 -07003608 parse_rp_state_cap(dev_priv, rp_state_cap);
Jeff McGeedd0a1aa2014-02-04 11:32:31 -06003609
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003610 /* disable the counters and set deterministic thresholds */
3611 I915_WRITE(GEN6_RC_CONTROL, 0);
3612
3613 I915_WRITE(GEN6_RC1_WAKE_RATE_LIMIT, 1000 << 16);
3614 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16 | 30);
3615 I915_WRITE(GEN6_RC6pp_WAKE_RATE_LIMIT, 30);
3616 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
3617 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
3618
Chris Wilsonb4519512012-05-11 14:29:30 +01003619 for_each_ring(ring, dev_priv, i)
3620 I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003621
3622 I915_WRITE(GEN6_RC_SLEEP, 0);
3623 I915_WRITE(GEN6_RC1e_THRESHOLD, 1000);
Daniel Vetter29c78f62013-11-16 16:04:26 +01003624 if (IS_IVYBRIDGE(dev))
Stéphane Marchesin351aa562013-08-13 11:55:17 -07003625 I915_WRITE(GEN6_RC6_THRESHOLD, 125000);
3626 else
3627 I915_WRITE(GEN6_RC6_THRESHOLD, 50000);
Stéphane Marchesin0920a482013-01-29 19:41:59 -08003628 I915_WRITE(GEN6_RC6p_THRESHOLD, 150000);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003629 I915_WRITE(GEN6_RC6pp_THRESHOLD, 64000); /* unused */
3630
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03003631 /* Check if we are enabling RC6 */
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003632 rc6_mode = intel_enable_rc6(dev_priv->dev);
3633 if (rc6_mode & INTEL_RC6_ENABLE)
3634 rc6_mask |= GEN6_RC_CTL_RC6_ENABLE;
3635
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03003636 /* We don't use those on Haswell */
3637 if (!IS_HASWELL(dev)) {
3638 if (rc6_mode & INTEL_RC6p_ENABLE)
3639 rc6_mask |= GEN6_RC_CTL_RC6p_ENABLE;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003640
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03003641 if (rc6_mode & INTEL_RC6pp_ENABLE)
3642 rc6_mask |= GEN6_RC_CTL_RC6pp_ENABLE;
3643 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003644
Ben Widawskydc39fff2013-10-18 12:32:07 -07003645 intel_print_rc6_info(dev, rc6_mask);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003646
3647 I915_WRITE(GEN6_RC_CONTROL,
3648 rc6_mask |
3649 GEN6_RC_CTL_EI_MODE(1) |
3650 GEN6_RC_CTL_HW_ENABLE);
3651
Chris Wilsondd75fdc2013-09-25 17:34:57 +01003652 /* Power down if completely idle for over 50ms */
3653 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 50000);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003654 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003655
Ben Widawsky42c05262012-09-26 10:34:00 -07003656 ret = sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_MIN_FREQ_TABLE, 0);
Ben Widawskyd060c162014-03-19 18:31:08 -07003657 if (ret)
Ben Widawsky42c05262012-09-26 10:34:00 -07003658 DRM_DEBUG_DRIVER("Failed to set the min frequency\n");
Ben Widawskyd060c162014-03-19 18:31:08 -07003659
3660 ret = sandybridge_pcode_read(dev_priv, GEN6_READ_OC_PARAMS, &pcu_mbox);
3661 if (!ret && (pcu_mbox & (1<<31))) { /* OC supported */
3662 DRM_DEBUG_DRIVER("Overclocking supported. Max: %dMHz, Overclock max: %dMHz\n",
Ben Widawskyb39fb292014-03-19 18:31:11 -07003663 (dev_priv->rps.max_freq_softlimit & 0xff) * 50,
Ben Widawskyd060c162014-03-19 18:31:08 -07003664 (pcu_mbox & 0xff) * 50);
Ben Widawskyb39fb292014-03-19 18:31:11 -07003665 dev_priv->rps.max_freq = pcu_mbox & 0xff;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003666 }
3667
Chris Wilsondd75fdc2013-09-25 17:34:57 +01003668 dev_priv->rps.power = HIGH_POWER; /* force a reset */
Ben Widawskyb39fb292014-03-19 18:31:11 -07003669 gen6_set_rps(dev_priv->dev, dev_priv->rps.min_freq_softlimit);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003670
Daniel Vetter44fc7d52013-07-12 22:43:27 +02003671 gen6_enable_rps_interrupts(dev);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003672
Ben Widawsky31643d52012-09-26 10:34:01 -07003673 rc6vids = 0;
3674 ret = sandybridge_pcode_read(dev_priv, GEN6_PCODE_READ_RC6VIDS, &rc6vids);
3675 if (IS_GEN6(dev) && ret) {
3676 DRM_DEBUG_DRIVER("Couldn't check for BIOS workaround\n");
3677 } else if (IS_GEN6(dev) && (GEN6_DECODE_RC6_VID(rc6vids & 0xff) < 450)) {
3678 DRM_DEBUG_DRIVER("You should update your BIOS. Correcting minimum rc6 voltage (%dmV->%dmV)\n",
3679 GEN6_DECODE_RC6_VID(rc6vids & 0xff), 450);
3680 rc6vids &= 0xffff00;
3681 rc6vids |= GEN6_ENCODE_RC6_VID(450);
3682 ret = sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_RC6VIDS, rc6vids);
3683 if (ret)
3684 DRM_ERROR("Couldn't fix incorrect rc6 voltage\n");
3685 }
3686
Deepak Sc8d9a592013-11-23 14:55:42 +05303687 gen6_gt_force_wake_put(dev_priv, FORCEWAKE_ALL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003688}
3689
Imre Deakc2bc2fc2014-04-18 16:16:23 +03003690static void __gen6_update_ring_freq(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003691{
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02003692 struct drm_i915_private *dev_priv = dev->dev_private;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003693 int min_freq = 15;
Chris Wilson3ebecd02013-04-12 19:10:13 +01003694 unsigned int gpu_freq;
3695 unsigned int max_ia_freq, min_ring_freq;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003696 int scaling_factor = 180;
Ben Widawskyeda79642013-10-07 17:15:48 -03003697 struct cpufreq_policy *policy;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003698
Jesse Barnes4fc688c2012-11-02 11:14:01 -07003699 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02003700
Ben Widawskyeda79642013-10-07 17:15:48 -03003701 policy = cpufreq_cpu_get(0);
3702 if (policy) {
3703 max_ia_freq = policy->cpuinfo.max_freq;
3704 cpufreq_cpu_put(policy);
3705 } else {
3706 /*
3707 * Default to measured freq if none found, PCU will ensure we
3708 * don't go over
3709 */
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003710 max_ia_freq = tsc_khz;
Ben Widawskyeda79642013-10-07 17:15:48 -03003711 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003712
3713 /* Convert from kHz to MHz */
3714 max_ia_freq /= 1000;
3715
Ben Widawsky153b4b952013-10-22 22:05:09 -07003716 min_ring_freq = I915_READ(DCLK) & 0xf;
Ben Widawskyf6aca452013-10-02 09:25:02 -07003717 /* convert DDR frequency from units of 266.6MHz to bandwidth */
3718 min_ring_freq = mult_frac(min_ring_freq, 8, 3);
Chris Wilson3ebecd02013-04-12 19:10:13 +01003719
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003720 /*
3721 * For each potential GPU frequency, load a ring frequency we'd like
3722 * to use for memory access. We do this by specifying the IA frequency
3723 * the PCU should use as a reference to determine the ring frequency.
3724 */
Ben Widawskyb39fb292014-03-19 18:31:11 -07003725 for (gpu_freq = dev_priv->rps.max_freq_softlimit; gpu_freq >= dev_priv->rps.min_freq_softlimit;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003726 gpu_freq--) {
Ben Widawskyb39fb292014-03-19 18:31:11 -07003727 int diff = dev_priv->rps.max_freq_softlimit - gpu_freq;
Chris Wilson3ebecd02013-04-12 19:10:13 +01003728 unsigned int ia_freq = 0, ring_freq = 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003729
Ben Widawsky46c764d2013-11-02 21:07:49 -07003730 if (INTEL_INFO(dev)->gen >= 8) {
3731 /* max(2 * GT, DDR). NB: GT is 50MHz units */
3732 ring_freq = max(min_ring_freq, gpu_freq);
3733 } else if (IS_HASWELL(dev)) {
Ben Widawskyf6aca452013-10-02 09:25:02 -07003734 ring_freq = mult_frac(gpu_freq, 5, 4);
Chris Wilson3ebecd02013-04-12 19:10:13 +01003735 ring_freq = max(min_ring_freq, ring_freq);
3736 /* leave ia_freq as the default, chosen by cpufreq */
3737 } else {
3738 /* On older processors, there is no separate ring
3739 * clock domain, so in order to boost the bandwidth
3740 * of the ring, we need to upclock the CPU (ia_freq).
3741 *
3742 * For GPU frequencies less than 750MHz,
3743 * just use the lowest ring freq.
3744 */
3745 if (gpu_freq < min_freq)
3746 ia_freq = 800;
3747 else
3748 ia_freq = max_ia_freq - ((diff * scaling_factor) / 2);
3749 ia_freq = DIV_ROUND_CLOSEST(ia_freq, 100);
3750 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003751
Ben Widawsky42c05262012-09-26 10:34:00 -07003752 sandybridge_pcode_write(dev_priv,
3753 GEN6_PCODE_WRITE_MIN_FREQ_TABLE,
Chris Wilson3ebecd02013-04-12 19:10:13 +01003754 ia_freq << GEN6_PCODE_FREQ_IA_RATIO_SHIFT |
3755 ring_freq << GEN6_PCODE_FREQ_RING_RATIO_SHIFT |
3756 gpu_freq);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003757 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003758}
3759
Imre Deakc2bc2fc2014-04-18 16:16:23 +03003760void gen6_update_ring_freq(struct drm_device *dev)
3761{
3762 struct drm_i915_private *dev_priv = dev->dev_private;
3763
3764 if (INTEL_INFO(dev)->gen < 6 || IS_VALLEYVIEW(dev))
3765 return;
3766
3767 mutex_lock(&dev_priv->rps.hw_lock);
3768 __gen6_update_ring_freq(dev);
3769 mutex_unlock(&dev_priv->rps.hw_lock);
3770}
3771
Deepak S2b6b3a02014-05-27 15:59:30 +05303772int cherryview_rps_max_freq(struct drm_i915_private *dev_priv)
3773{
3774 u32 val, rp0;
3775
3776 val = vlv_punit_read(dev_priv, PUNIT_GPU_STATUS_REG);
3777 rp0 = (val >> PUNIT_GPU_STATUS_MAX_FREQ_SHIFT) & PUNIT_GPU_STATUS_MAX_FREQ_MASK;
3778
3779 return rp0;
3780}
3781
3782static int cherryview_rps_rpe_freq(struct drm_i915_private *dev_priv)
3783{
3784 u32 val, rpe;
3785
3786 val = vlv_punit_read(dev_priv, PUNIT_GPU_DUTYCYCLE_REG);
3787 rpe = (val >> PUNIT_GPU_DUTYCYCLE_RPE_FREQ_SHIFT) & PUNIT_GPU_DUTYCYCLE_RPE_FREQ_MASK;
3788
3789 return rpe;
3790}
3791
3792int cherryview_rps_min_freq(struct drm_i915_private *dev_priv)
3793{
3794 u32 val, rpn;
3795
3796 val = vlv_punit_read(dev_priv, PUNIT_GPU_STATUS_REG);
3797 rpn = (val >> PUNIT_GPU_STATIS_GFX_MIN_FREQ_SHIFT) & PUNIT_GPU_STATUS_GFX_MIN_FREQ_MASK;
3798 return rpn;
3799}
3800
Jesse Barnes0a073b82013-04-17 15:54:58 -07003801int valleyview_rps_max_freq(struct drm_i915_private *dev_priv)
3802{
3803 u32 val, rp0;
3804
Jani Nikula64936252013-05-22 15:36:20 +03003805 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FREQ_FUSE);
Jesse Barnes0a073b82013-04-17 15:54:58 -07003806
3807 rp0 = (val & FB_GFX_MAX_FREQ_FUSE_MASK) >> FB_GFX_MAX_FREQ_FUSE_SHIFT;
3808 /* Clamp to max */
3809 rp0 = min_t(u32, rp0, 0xea);
3810
3811 return rp0;
3812}
3813
3814static int valleyview_rps_rpe_freq(struct drm_i915_private *dev_priv)
3815{
3816 u32 val, rpe;
3817
Jani Nikula64936252013-05-22 15:36:20 +03003818 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FMAX_FUSE_LO);
Jesse Barnes0a073b82013-04-17 15:54:58 -07003819 rpe = (val & FB_FMAX_VMIN_FREQ_LO_MASK) >> FB_FMAX_VMIN_FREQ_LO_SHIFT;
Jani Nikula64936252013-05-22 15:36:20 +03003820 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FMAX_FUSE_HI);
Jesse Barnes0a073b82013-04-17 15:54:58 -07003821 rpe |= (val & FB_FMAX_VMIN_FREQ_HI_MASK) << 5;
3822
3823 return rpe;
3824}
3825
3826int valleyview_rps_min_freq(struct drm_i915_private *dev_priv)
3827{
Jani Nikula64936252013-05-22 15:36:20 +03003828 return vlv_punit_read(dev_priv, PUNIT_REG_GPU_LFM) & 0xff;
Jesse Barnes0a073b82013-04-17 15:54:58 -07003829}
3830
Imre Deakae484342014-03-31 15:10:44 +03003831/* Check that the pctx buffer wasn't move under us. */
3832static void valleyview_check_pctx(struct drm_i915_private *dev_priv)
3833{
3834 unsigned long pctx_addr = I915_READ(VLV_PCBR) & ~4095;
3835
3836 WARN_ON(pctx_addr != dev_priv->mm.stolen_base +
3837 dev_priv->vlv_pctx->stolen->start);
3838}
3839
Deepak S38807742014-05-23 21:00:15 +05303840
3841/* Check that the pcbr address is not empty. */
3842static void cherryview_check_pctx(struct drm_i915_private *dev_priv)
3843{
3844 unsigned long pctx_addr = I915_READ(VLV_PCBR) & ~4095;
3845
3846 WARN_ON((pctx_addr >> VLV_PCBR_ADDR_SHIFT) == 0);
3847}
3848
3849static void cherryview_setup_pctx(struct drm_device *dev)
3850{
3851 struct drm_i915_private *dev_priv = dev->dev_private;
3852 unsigned long pctx_paddr, paddr;
3853 struct i915_gtt *gtt = &dev_priv->gtt;
3854 u32 pcbr;
3855 int pctx_size = 32*1024;
3856
3857 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
3858
3859 pcbr = I915_READ(VLV_PCBR);
3860 if ((pcbr >> VLV_PCBR_ADDR_SHIFT) == 0) {
3861 paddr = (dev_priv->mm.stolen_base +
3862 (gtt->stolen_size - pctx_size));
3863
3864 pctx_paddr = (paddr & (~4095));
3865 I915_WRITE(VLV_PCBR, pctx_paddr);
3866 }
3867}
3868
Jesse Barnesc9cddff2013-05-08 10:45:13 -07003869static void valleyview_setup_pctx(struct drm_device *dev)
3870{
3871 struct drm_i915_private *dev_priv = dev->dev_private;
3872 struct drm_i915_gem_object *pctx;
3873 unsigned long pctx_paddr;
3874 u32 pcbr;
3875 int pctx_size = 24*1024;
3876
Imre Deak17b0c1f2014-02-11 21:39:06 +02003877 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
3878
Jesse Barnesc9cddff2013-05-08 10:45:13 -07003879 pcbr = I915_READ(VLV_PCBR);
3880 if (pcbr) {
3881 /* BIOS set it up already, grab the pre-alloc'd space */
3882 int pcbr_offset;
3883
3884 pcbr_offset = (pcbr & (~4095)) - dev_priv->mm.stolen_base;
3885 pctx = i915_gem_object_create_stolen_for_preallocated(dev_priv->dev,
3886 pcbr_offset,
Daniel Vetter190d6cd2013-07-04 13:06:28 +02003887 I915_GTT_OFFSET_NONE,
Jesse Barnesc9cddff2013-05-08 10:45:13 -07003888 pctx_size);
3889 goto out;
3890 }
3891
3892 /*
3893 * From the Gunit register HAS:
3894 * The Gfx driver is expected to program this register and ensure
3895 * proper allocation within Gfx stolen memory. For example, this
3896 * register should be programmed such than the PCBR range does not
3897 * overlap with other ranges, such as the frame buffer, protected
3898 * memory, or any other relevant ranges.
3899 */
3900 pctx = i915_gem_object_create_stolen(dev, pctx_size);
3901 if (!pctx) {
3902 DRM_DEBUG("not enough stolen space for PCTX, disabling\n");
3903 return;
3904 }
3905
3906 pctx_paddr = dev_priv->mm.stolen_base + pctx->stolen->start;
3907 I915_WRITE(VLV_PCBR, pctx_paddr);
3908
3909out:
3910 dev_priv->vlv_pctx = pctx;
3911}
3912
Imre Deakae484342014-03-31 15:10:44 +03003913static void valleyview_cleanup_pctx(struct drm_device *dev)
3914{
3915 struct drm_i915_private *dev_priv = dev->dev_private;
3916
3917 if (WARN_ON(!dev_priv->vlv_pctx))
3918 return;
3919
3920 drm_gem_object_unreference(&dev_priv->vlv_pctx->base);
3921 dev_priv->vlv_pctx = NULL;
3922}
3923
Imre Deak4e805192014-04-14 20:24:41 +03003924static void valleyview_init_gt_powersave(struct drm_device *dev)
3925{
3926 struct drm_i915_private *dev_priv = dev->dev_private;
3927
3928 valleyview_setup_pctx(dev);
3929
3930 mutex_lock(&dev_priv->rps.hw_lock);
3931
3932 dev_priv->rps.max_freq = valleyview_rps_max_freq(dev_priv);
3933 dev_priv->rps.rp0_freq = dev_priv->rps.max_freq;
3934 DRM_DEBUG_DRIVER("max GPU freq: %d MHz (%u)\n",
3935 vlv_gpu_freq(dev_priv, dev_priv->rps.max_freq),
3936 dev_priv->rps.max_freq);
3937
3938 dev_priv->rps.efficient_freq = valleyview_rps_rpe_freq(dev_priv);
3939 DRM_DEBUG_DRIVER("RPe GPU freq: %d MHz (%u)\n",
3940 vlv_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
3941 dev_priv->rps.efficient_freq);
3942
3943 dev_priv->rps.min_freq = valleyview_rps_min_freq(dev_priv);
3944 DRM_DEBUG_DRIVER("min GPU freq: %d MHz (%u)\n",
3945 vlv_gpu_freq(dev_priv, dev_priv->rps.min_freq),
3946 dev_priv->rps.min_freq);
3947
3948 /* Preserve min/max settings in case of re-init */
3949 if (dev_priv->rps.max_freq_softlimit == 0)
3950 dev_priv->rps.max_freq_softlimit = dev_priv->rps.max_freq;
3951
3952 if (dev_priv->rps.min_freq_softlimit == 0)
3953 dev_priv->rps.min_freq_softlimit = dev_priv->rps.min_freq;
3954
3955 mutex_unlock(&dev_priv->rps.hw_lock);
3956}
3957
Deepak S38807742014-05-23 21:00:15 +05303958static void cherryview_init_gt_powersave(struct drm_device *dev)
3959{
Deepak S2b6b3a02014-05-27 15:59:30 +05303960 struct drm_i915_private *dev_priv = dev->dev_private;
3961
Deepak S38807742014-05-23 21:00:15 +05303962 cherryview_setup_pctx(dev);
Deepak S2b6b3a02014-05-27 15:59:30 +05303963
3964 mutex_lock(&dev_priv->rps.hw_lock);
3965
3966 dev_priv->rps.max_freq = cherryview_rps_max_freq(dev_priv);
3967 dev_priv->rps.rp0_freq = dev_priv->rps.max_freq;
3968 DRM_DEBUG_DRIVER("max GPU freq: %d MHz (%u)\n",
3969 vlv_gpu_freq(dev_priv, dev_priv->rps.max_freq),
3970 dev_priv->rps.max_freq);
3971
3972 dev_priv->rps.efficient_freq = cherryview_rps_rpe_freq(dev_priv);
3973 DRM_DEBUG_DRIVER("RPe GPU freq: %d MHz (%u)\n",
3974 vlv_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
3975 dev_priv->rps.efficient_freq);
3976
3977 dev_priv->rps.min_freq = cherryview_rps_min_freq(dev_priv);
3978 DRM_DEBUG_DRIVER("min GPU freq: %d MHz (%u)\n",
3979 vlv_gpu_freq(dev_priv, dev_priv->rps.min_freq),
3980 dev_priv->rps.min_freq);
3981
3982 /* Preserve min/max settings in case of re-init */
3983 if (dev_priv->rps.max_freq_softlimit == 0)
3984 dev_priv->rps.max_freq_softlimit = dev_priv->rps.max_freq;
3985
3986 if (dev_priv->rps.min_freq_softlimit == 0)
3987 dev_priv->rps.min_freq_softlimit = dev_priv->rps.min_freq;
3988
3989 mutex_unlock(&dev_priv->rps.hw_lock);
Deepak S38807742014-05-23 21:00:15 +05303990}
3991
Imre Deak4e805192014-04-14 20:24:41 +03003992static void valleyview_cleanup_gt_powersave(struct drm_device *dev)
3993{
3994 valleyview_cleanup_pctx(dev);
3995}
3996
Deepak S38807742014-05-23 21:00:15 +05303997static void cherryview_enable_rps(struct drm_device *dev)
3998{
3999 struct drm_i915_private *dev_priv = dev->dev_private;
4000 struct intel_engine_cs *ring;
Deepak S2b6b3a02014-05-27 15:59:30 +05304001 u32 gtfifodbg, val, rc6_mode = 0, pcbr;
Deepak S38807742014-05-23 21:00:15 +05304002 int i;
4003
4004 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
4005
4006 gtfifodbg = I915_READ(GTFIFODBG);
4007 if (gtfifodbg) {
4008 DRM_DEBUG_DRIVER("GT fifo had a previous error %x\n",
4009 gtfifodbg);
4010 I915_WRITE(GTFIFODBG, gtfifodbg);
4011 }
4012
4013 cherryview_check_pctx(dev_priv);
4014
4015 /* 1a & 1b: Get forcewake during program sequence. Although the driver
4016 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
4017 gen6_gt_force_wake_get(dev_priv, FORCEWAKE_ALL);
4018
4019 /* 2a: Program RC6 thresholds.*/
4020 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16);
4021 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
4022 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
4023
4024 for_each_ring(ring, dev_priv, i)
4025 I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
4026 I915_WRITE(GEN6_RC_SLEEP, 0);
4027
4028 I915_WRITE(GEN6_RC6_THRESHOLD, 50000); /* 50/125ms per EI */
4029
4030 /* allows RC6 residency counter to work */
4031 I915_WRITE(VLV_COUNTER_CONTROL,
4032 _MASKED_BIT_ENABLE(VLV_COUNT_RANGE_HIGH |
4033 VLV_MEDIA_RC6_COUNT_EN |
4034 VLV_RENDER_RC6_COUNT_EN));
4035
4036 /* For now we assume BIOS is allocating and populating the PCBR */
4037 pcbr = I915_READ(VLV_PCBR);
4038
4039 DRM_DEBUG_DRIVER("PCBR offset : 0x%x\n", pcbr);
4040
4041 /* 3: Enable RC6 */
4042 if ((intel_enable_rc6(dev) & INTEL_RC6_ENABLE) &&
4043 (pcbr >> VLV_PCBR_ADDR_SHIFT))
4044 rc6_mode = GEN6_RC_CTL_EI_MODE(1);
4045
4046 I915_WRITE(GEN6_RC_CONTROL, rc6_mode);
4047
Deepak S2b6b3a02014-05-27 15:59:30 +05304048 /* 4 Program defaults and thresholds for RPS*/
4049 I915_WRITE(GEN6_RP_UP_THRESHOLD, 59400);
4050 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 245000);
4051 I915_WRITE(GEN6_RP_UP_EI, 66000);
4052 I915_WRITE(GEN6_RP_DOWN_EI, 350000);
4053
4054 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
4055
Tom O'Rourke7405f422014-06-10 16:26:34 -07004056 /* WaDisablePwrmtrEvent:chv (pre-production hw) */
4057 I915_WRITE(0xA80C, I915_READ(0xA80C) & 0x00ffffff);
4058 I915_WRITE(0xA810, I915_READ(0xA810) & 0xffffff00);
4059
Deepak S2b6b3a02014-05-27 15:59:30 +05304060 /* 5: Enable RPS */
4061 I915_WRITE(GEN6_RP_CONTROL,
4062 GEN6_RP_MEDIA_HW_NORMAL_MODE |
Tom O'Rourke7405f422014-06-10 16:26:34 -07004063 GEN6_RP_MEDIA_IS_GFX | /* WaSetMaskForGfxBusyness:chv (pre-production hw ?) */
Deepak S2b6b3a02014-05-27 15:59:30 +05304064 GEN6_RP_ENABLE |
4065 GEN6_RP_UP_BUSY_AVG |
4066 GEN6_RP_DOWN_IDLE_AVG);
4067
4068 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
4069
4070 DRM_DEBUG_DRIVER("GPLL enabled? %s\n", val & 0x10 ? "yes" : "no");
4071 DRM_DEBUG_DRIVER("GPU status: 0x%08x\n", val);
4072
4073 dev_priv->rps.cur_freq = (val >> 8) & 0xff;
4074 DRM_DEBUG_DRIVER("current GPU freq: %d MHz (%u)\n",
4075 vlv_gpu_freq(dev_priv, dev_priv->rps.cur_freq),
4076 dev_priv->rps.cur_freq);
4077
4078 DRM_DEBUG_DRIVER("setting GPU freq to %d MHz (%u)\n",
4079 vlv_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
4080 dev_priv->rps.efficient_freq);
4081
4082 valleyview_set_rps(dev_priv->dev, dev_priv->rps.efficient_freq);
4083
Deepak S38807742014-05-23 21:00:15 +05304084 gen6_gt_force_wake_put(dev_priv, FORCEWAKE_ALL);
4085}
4086
Jesse Barnes0a073b82013-04-17 15:54:58 -07004087static void valleyview_enable_rps(struct drm_device *dev)
4088{
4089 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01004090 struct intel_engine_cs *ring;
Ben Widawsky2a5913a2014-03-19 18:31:13 -07004091 u32 gtfifodbg, val, rc6_mode = 0;
Jesse Barnes0a073b82013-04-17 15:54:58 -07004092 int i;
4093
4094 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
4095
Imre Deakae484342014-03-31 15:10:44 +03004096 valleyview_check_pctx(dev_priv);
4097
Jesse Barnes0a073b82013-04-17 15:54:58 -07004098 if ((gtfifodbg = I915_READ(GTFIFODBG))) {
Jesse Barnesf7d85c12013-09-27 10:40:54 -07004099 DRM_DEBUG_DRIVER("GT fifo had a previous error %x\n",
4100 gtfifodbg);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004101 I915_WRITE(GTFIFODBG, gtfifodbg);
4102 }
4103
Deepak Sc8d9a592013-11-23 14:55:42 +05304104 /* If VLV, Forcewake all wells, else re-direct to regular path */
4105 gen6_gt_force_wake_get(dev_priv, FORCEWAKE_ALL);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004106
4107 I915_WRITE(GEN6_RP_UP_THRESHOLD, 59400);
4108 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 245000);
4109 I915_WRITE(GEN6_RP_UP_EI, 66000);
4110 I915_WRITE(GEN6_RP_DOWN_EI, 350000);
4111
4112 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
4113
4114 I915_WRITE(GEN6_RP_CONTROL,
4115 GEN6_RP_MEDIA_TURBO |
4116 GEN6_RP_MEDIA_HW_NORMAL_MODE |
4117 GEN6_RP_MEDIA_IS_GFX |
4118 GEN6_RP_ENABLE |
4119 GEN6_RP_UP_BUSY_AVG |
4120 GEN6_RP_DOWN_IDLE_CONT);
4121
4122 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 0x00280000);
4123 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
4124 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
4125
4126 for_each_ring(ring, dev_priv, i)
4127 I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
4128
Jesse Barnes2f0aa3042013-11-15 09:32:11 -08004129 I915_WRITE(GEN6_RC6_THRESHOLD, 0x557);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004130
4131 /* allows RC6 residency counter to work */
Jesse Barnes49798eb2013-09-26 17:55:57 -07004132 I915_WRITE(VLV_COUNTER_CONTROL,
4133 _MASKED_BIT_ENABLE(VLV_COUNT_RANGE_HIGH |
4134 VLV_MEDIA_RC6_COUNT_EN |
4135 VLV_RENDER_RC6_COUNT_EN));
Jesse Barnesa2b23fe2013-09-19 09:33:13 -07004136 if (intel_enable_rc6(dev) & INTEL_RC6_ENABLE)
Jesse Barnes6b88f292013-11-15 09:32:12 -08004137 rc6_mode = GEN7_RC_CTL_TO_MODE | VLV_RC_CTL_CTX_RST_PARALLEL;
Ben Widawskydc39fff2013-10-18 12:32:07 -07004138
4139 intel_print_rc6_info(dev, rc6_mode);
4140
Jesse Barnesa2b23fe2013-09-19 09:33:13 -07004141 I915_WRITE(GEN6_RC_CONTROL, rc6_mode);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004142
Jani Nikula64936252013-05-22 15:36:20 +03004143 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004144
4145 DRM_DEBUG_DRIVER("GPLL enabled? %s\n", val & 0x10 ? "yes" : "no");
4146 DRM_DEBUG_DRIVER("GPU status: 0x%08x\n", val);
4147
Ben Widawskyb39fb292014-03-19 18:31:11 -07004148 dev_priv->rps.cur_freq = (val >> 8) & 0xff;
Ville Syrjälä73008b92013-06-25 19:21:01 +03004149 DRM_DEBUG_DRIVER("current GPU freq: %d MHz (%u)\n",
Ben Widawskyb39fb292014-03-19 18:31:11 -07004150 vlv_gpu_freq(dev_priv, dev_priv->rps.cur_freq),
4151 dev_priv->rps.cur_freq);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004152
Ville Syrjälä73008b92013-06-25 19:21:01 +03004153 DRM_DEBUG_DRIVER("setting GPU freq to %d MHz (%u)\n",
Ben Widawskyb39fb292014-03-19 18:31:11 -07004154 vlv_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
4155 dev_priv->rps.efficient_freq);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004156
Ben Widawskyb39fb292014-03-19 18:31:11 -07004157 valleyview_set_rps(dev_priv->dev, dev_priv->rps.efficient_freq);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004158
Daniel Vetter44fc7d52013-07-12 22:43:27 +02004159 gen6_enable_rps_interrupts(dev);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004160
Deepak Sc8d9a592013-11-23 14:55:42 +05304161 gen6_gt_force_wake_put(dev_priv, FORCEWAKE_ALL);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004162}
4163
Daniel Vetter930ebb42012-06-29 23:32:16 +02004164void ironlake_teardown_rc6(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004165{
4166 struct drm_i915_private *dev_priv = dev->dev_private;
4167
Daniel Vetter3e373942012-11-02 19:55:04 +01004168 if (dev_priv->ips.renderctx) {
Ben Widawskyd7f46fc2013-12-06 14:10:55 -08004169 i915_gem_object_ggtt_unpin(dev_priv->ips.renderctx);
Daniel Vetter3e373942012-11-02 19:55:04 +01004170 drm_gem_object_unreference(&dev_priv->ips.renderctx->base);
4171 dev_priv->ips.renderctx = NULL;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004172 }
4173
Daniel Vetter3e373942012-11-02 19:55:04 +01004174 if (dev_priv->ips.pwrctx) {
Ben Widawskyd7f46fc2013-12-06 14:10:55 -08004175 i915_gem_object_ggtt_unpin(dev_priv->ips.pwrctx);
Daniel Vetter3e373942012-11-02 19:55:04 +01004176 drm_gem_object_unreference(&dev_priv->ips.pwrctx->base);
4177 dev_priv->ips.pwrctx = NULL;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004178 }
4179}
4180
Daniel Vetter930ebb42012-06-29 23:32:16 +02004181static void ironlake_disable_rc6(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004182{
4183 struct drm_i915_private *dev_priv = dev->dev_private;
4184
4185 if (I915_READ(PWRCTXA)) {
4186 /* Wake the GPU, prevent RC6, then restore RSTDBYCTL */
4187 I915_WRITE(RSTDBYCTL, I915_READ(RSTDBYCTL) | RCX_SW_EXIT);
4188 wait_for(((I915_READ(RSTDBYCTL) & RSX_STATUS_MASK) == RSX_STATUS_ON),
4189 50);
4190
4191 I915_WRITE(PWRCTXA, 0);
4192 POSTING_READ(PWRCTXA);
4193
4194 I915_WRITE(RSTDBYCTL, I915_READ(RSTDBYCTL) & ~RCX_SW_EXIT);
4195 POSTING_READ(RSTDBYCTL);
4196 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004197}
4198
4199static int ironlake_setup_rc6(struct drm_device *dev)
4200{
4201 struct drm_i915_private *dev_priv = dev->dev_private;
4202
Daniel Vetter3e373942012-11-02 19:55:04 +01004203 if (dev_priv->ips.renderctx == NULL)
4204 dev_priv->ips.renderctx = intel_alloc_context_page(dev);
4205 if (!dev_priv->ips.renderctx)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004206 return -ENOMEM;
4207
Daniel Vetter3e373942012-11-02 19:55:04 +01004208 if (dev_priv->ips.pwrctx == NULL)
4209 dev_priv->ips.pwrctx = intel_alloc_context_page(dev);
4210 if (!dev_priv->ips.pwrctx) {
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004211 ironlake_teardown_rc6(dev);
4212 return -ENOMEM;
4213 }
4214
4215 return 0;
4216}
4217
Daniel Vetter930ebb42012-06-29 23:32:16 +02004218static void ironlake_enable_rc6(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004219{
4220 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01004221 struct intel_engine_cs *ring = &dev_priv->ring[RCS];
Chris Wilson3e960502012-11-27 16:22:54 +00004222 bool was_interruptible;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004223 int ret;
4224
4225 /* rc6 disabled by default due to repeated reports of hanging during
4226 * boot and resume.
4227 */
4228 if (!intel_enable_rc6(dev))
4229 return;
4230
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02004231 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
4232
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004233 ret = ironlake_setup_rc6(dev);
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02004234 if (ret)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004235 return;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004236
Chris Wilson3e960502012-11-27 16:22:54 +00004237 was_interruptible = dev_priv->mm.interruptible;
4238 dev_priv->mm.interruptible = false;
4239
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004240 /*
4241 * GPU can automatically power down the render unit if given a page
4242 * to save state.
4243 */
Daniel Vetter6d90c952012-04-26 23:28:05 +02004244 ret = intel_ring_begin(ring, 6);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004245 if (ret) {
4246 ironlake_teardown_rc6(dev);
Chris Wilson3e960502012-11-27 16:22:54 +00004247 dev_priv->mm.interruptible = was_interruptible;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004248 return;
4249 }
4250
Daniel Vetter6d90c952012-04-26 23:28:05 +02004251 intel_ring_emit(ring, MI_SUSPEND_FLUSH | MI_SUSPEND_FLUSH_EN);
4252 intel_ring_emit(ring, MI_SET_CONTEXT);
Ben Widawskyf343c5f2013-07-05 14:41:04 -07004253 intel_ring_emit(ring, i915_gem_obj_ggtt_offset(dev_priv->ips.renderctx) |
Daniel Vetter6d90c952012-04-26 23:28:05 +02004254 MI_MM_SPACE_GTT |
4255 MI_SAVE_EXT_STATE_EN |
4256 MI_RESTORE_EXT_STATE_EN |
4257 MI_RESTORE_INHIBIT);
4258 intel_ring_emit(ring, MI_SUSPEND_FLUSH);
4259 intel_ring_emit(ring, MI_NOOP);
4260 intel_ring_emit(ring, MI_FLUSH);
4261 intel_ring_advance(ring);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004262
4263 /*
4264 * Wait for the command parser to advance past MI_SET_CONTEXT. The HW
4265 * does an implicit flush, combined with MI_FLUSH above, it should be
4266 * safe to assume that renderctx is valid
4267 */
Chris Wilson3e960502012-11-27 16:22:54 +00004268 ret = intel_ring_idle(ring);
4269 dev_priv->mm.interruptible = was_interruptible;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004270 if (ret) {
Jani Nikuladef27a52013-03-12 10:49:19 +02004271 DRM_ERROR("failed to enable ironlake power savings\n");
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004272 ironlake_teardown_rc6(dev);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004273 return;
4274 }
4275
Ben Widawskyf343c5f2013-07-05 14:41:04 -07004276 I915_WRITE(PWRCTXA, i915_gem_obj_ggtt_offset(dev_priv->ips.pwrctx) | PWRCTX_EN);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004277 I915_WRITE(RSTDBYCTL, I915_READ(RSTDBYCTL) & ~RCX_SW_EXIT);
Ben Widawskydc39fff2013-10-18 12:32:07 -07004278
Imre Deak91ca6892014-04-14 20:24:25 +03004279 intel_print_rc6_info(dev, GEN6_RC_CTL_RC6_ENABLE);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004280}
4281
Eugeni Dodonovdde18882012-04-18 15:29:24 -03004282static unsigned long intel_pxfreq(u32 vidfreq)
4283{
4284 unsigned long freq;
4285 int div = (vidfreq & 0x3f0000) >> 16;
4286 int post = (vidfreq & 0x3000) >> 12;
4287 int pre = (vidfreq & 0x7);
4288
4289 if (!pre)
4290 return 0;
4291
4292 freq = ((div * 133333) / ((1<<post) * pre));
4293
4294 return freq;
4295}
4296
Daniel Vettereb48eb02012-04-26 23:28:12 +02004297static const struct cparams {
4298 u16 i;
4299 u16 t;
4300 u16 m;
4301 u16 c;
4302} cparams[] = {
4303 { 1, 1333, 301, 28664 },
4304 { 1, 1066, 294, 24460 },
4305 { 1, 800, 294, 25192 },
4306 { 0, 1333, 276, 27605 },
4307 { 0, 1066, 276, 27605 },
4308 { 0, 800, 231, 23784 },
4309};
4310
Chris Wilsonf531dcb22012-09-25 10:16:12 +01004311static unsigned long __i915_chipset_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02004312{
4313 u64 total_count, diff, ret;
4314 u32 count1, count2, count3, m = 0, c = 0;
4315 unsigned long now = jiffies_to_msecs(jiffies), diff1;
4316 int i;
4317
Daniel Vetter02d71952012-08-09 16:44:54 +02004318 assert_spin_locked(&mchdev_lock);
4319
Daniel Vetter20e4d402012-08-08 23:35:39 +02004320 diff1 = now - dev_priv->ips.last_time1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02004321
4322 /* Prevent division-by-zero if we are asking too fast.
4323 * Also, we don't get interesting results if we are polling
4324 * faster than once in 10ms, so just return the saved value
4325 * in such cases.
4326 */
4327 if (diff1 <= 10)
Daniel Vetter20e4d402012-08-08 23:35:39 +02004328 return dev_priv->ips.chipset_power;
Daniel Vettereb48eb02012-04-26 23:28:12 +02004329
4330 count1 = I915_READ(DMIEC);
4331 count2 = I915_READ(DDREC);
4332 count3 = I915_READ(CSIEC);
4333
4334 total_count = count1 + count2 + count3;
4335
4336 /* FIXME: handle per-counter overflow */
Daniel Vetter20e4d402012-08-08 23:35:39 +02004337 if (total_count < dev_priv->ips.last_count1) {
4338 diff = ~0UL - dev_priv->ips.last_count1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02004339 diff += total_count;
4340 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +02004341 diff = total_count - dev_priv->ips.last_count1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02004342 }
4343
4344 for (i = 0; i < ARRAY_SIZE(cparams); i++) {
Daniel Vetter20e4d402012-08-08 23:35:39 +02004345 if (cparams[i].i == dev_priv->ips.c_m &&
4346 cparams[i].t == dev_priv->ips.r_t) {
Daniel Vettereb48eb02012-04-26 23:28:12 +02004347 m = cparams[i].m;
4348 c = cparams[i].c;
4349 break;
4350 }
4351 }
4352
4353 diff = div_u64(diff, diff1);
4354 ret = ((m * diff) + c);
4355 ret = div_u64(ret, 10);
4356
Daniel Vetter20e4d402012-08-08 23:35:39 +02004357 dev_priv->ips.last_count1 = total_count;
4358 dev_priv->ips.last_time1 = now;
Daniel Vettereb48eb02012-04-26 23:28:12 +02004359
Daniel Vetter20e4d402012-08-08 23:35:39 +02004360 dev_priv->ips.chipset_power = ret;
Daniel Vettereb48eb02012-04-26 23:28:12 +02004361
4362 return ret;
4363}
4364
Chris Wilsonf531dcb22012-09-25 10:16:12 +01004365unsigned long i915_chipset_val(struct drm_i915_private *dev_priv)
4366{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00004367 struct drm_device *dev = dev_priv->dev;
Chris Wilsonf531dcb22012-09-25 10:16:12 +01004368 unsigned long val;
4369
Damien Lespiau3d13ef22014-02-07 19:12:47 +00004370 if (INTEL_INFO(dev)->gen != 5)
Chris Wilsonf531dcb22012-09-25 10:16:12 +01004371 return 0;
4372
4373 spin_lock_irq(&mchdev_lock);
4374
4375 val = __i915_chipset_val(dev_priv);
4376
4377 spin_unlock_irq(&mchdev_lock);
4378
4379 return val;
4380}
4381
Daniel Vettereb48eb02012-04-26 23:28:12 +02004382unsigned long i915_mch_val(struct drm_i915_private *dev_priv)
4383{
4384 unsigned long m, x, b;
4385 u32 tsfs;
4386
4387 tsfs = I915_READ(TSFS);
4388
4389 m = ((tsfs & TSFS_SLOPE_MASK) >> TSFS_SLOPE_SHIFT);
4390 x = I915_READ8(TR1);
4391
4392 b = tsfs & TSFS_INTR_MASK;
4393
4394 return ((m * x) / 127) - b;
4395}
4396
4397static u16 pvid_to_extvid(struct drm_i915_private *dev_priv, u8 pxvid)
4398{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00004399 struct drm_device *dev = dev_priv->dev;
Daniel Vettereb48eb02012-04-26 23:28:12 +02004400 static const struct v_table {
4401 u16 vd; /* in .1 mil */
4402 u16 vm; /* in .1 mil */
4403 } v_table[] = {
4404 { 0, 0, },
4405 { 375, 0, },
4406 { 500, 0, },
4407 { 625, 0, },
4408 { 750, 0, },
4409 { 875, 0, },
4410 { 1000, 0, },
4411 { 1125, 0, },
4412 { 4125, 3000, },
4413 { 4125, 3000, },
4414 { 4125, 3000, },
4415 { 4125, 3000, },
4416 { 4125, 3000, },
4417 { 4125, 3000, },
4418 { 4125, 3000, },
4419 { 4125, 3000, },
4420 { 4125, 3000, },
4421 { 4125, 3000, },
4422 { 4125, 3000, },
4423 { 4125, 3000, },
4424 { 4125, 3000, },
4425 { 4125, 3000, },
4426 { 4125, 3000, },
4427 { 4125, 3000, },
4428 { 4125, 3000, },
4429 { 4125, 3000, },
4430 { 4125, 3000, },
4431 { 4125, 3000, },
4432 { 4125, 3000, },
4433 { 4125, 3000, },
4434 { 4125, 3000, },
4435 { 4125, 3000, },
4436 { 4250, 3125, },
4437 { 4375, 3250, },
4438 { 4500, 3375, },
4439 { 4625, 3500, },
4440 { 4750, 3625, },
4441 { 4875, 3750, },
4442 { 5000, 3875, },
4443 { 5125, 4000, },
4444 { 5250, 4125, },
4445 { 5375, 4250, },
4446 { 5500, 4375, },
4447 { 5625, 4500, },
4448 { 5750, 4625, },
4449 { 5875, 4750, },
4450 { 6000, 4875, },
4451 { 6125, 5000, },
4452 { 6250, 5125, },
4453 { 6375, 5250, },
4454 { 6500, 5375, },
4455 { 6625, 5500, },
4456 { 6750, 5625, },
4457 { 6875, 5750, },
4458 { 7000, 5875, },
4459 { 7125, 6000, },
4460 { 7250, 6125, },
4461 { 7375, 6250, },
4462 { 7500, 6375, },
4463 { 7625, 6500, },
4464 { 7750, 6625, },
4465 { 7875, 6750, },
4466 { 8000, 6875, },
4467 { 8125, 7000, },
4468 { 8250, 7125, },
4469 { 8375, 7250, },
4470 { 8500, 7375, },
4471 { 8625, 7500, },
4472 { 8750, 7625, },
4473 { 8875, 7750, },
4474 { 9000, 7875, },
4475 { 9125, 8000, },
4476 { 9250, 8125, },
4477 { 9375, 8250, },
4478 { 9500, 8375, },
4479 { 9625, 8500, },
4480 { 9750, 8625, },
4481 { 9875, 8750, },
4482 { 10000, 8875, },
4483 { 10125, 9000, },
4484 { 10250, 9125, },
4485 { 10375, 9250, },
4486 { 10500, 9375, },
4487 { 10625, 9500, },
4488 { 10750, 9625, },
4489 { 10875, 9750, },
4490 { 11000, 9875, },
4491 { 11125, 10000, },
4492 { 11250, 10125, },
4493 { 11375, 10250, },
4494 { 11500, 10375, },
4495 { 11625, 10500, },
4496 { 11750, 10625, },
4497 { 11875, 10750, },
4498 { 12000, 10875, },
4499 { 12125, 11000, },
4500 { 12250, 11125, },
4501 { 12375, 11250, },
4502 { 12500, 11375, },
4503 { 12625, 11500, },
4504 { 12750, 11625, },
4505 { 12875, 11750, },
4506 { 13000, 11875, },
4507 { 13125, 12000, },
4508 { 13250, 12125, },
4509 { 13375, 12250, },
4510 { 13500, 12375, },
4511 { 13625, 12500, },
4512 { 13750, 12625, },
4513 { 13875, 12750, },
4514 { 14000, 12875, },
4515 { 14125, 13000, },
4516 { 14250, 13125, },
4517 { 14375, 13250, },
4518 { 14500, 13375, },
4519 { 14625, 13500, },
4520 { 14750, 13625, },
4521 { 14875, 13750, },
4522 { 15000, 13875, },
4523 { 15125, 14000, },
4524 { 15250, 14125, },
4525 { 15375, 14250, },
4526 { 15500, 14375, },
4527 { 15625, 14500, },
4528 { 15750, 14625, },
4529 { 15875, 14750, },
4530 { 16000, 14875, },
4531 { 16125, 15000, },
4532 };
Damien Lespiau3d13ef22014-02-07 19:12:47 +00004533 if (INTEL_INFO(dev)->is_mobile)
Daniel Vettereb48eb02012-04-26 23:28:12 +02004534 return v_table[pxvid].vm;
4535 else
4536 return v_table[pxvid].vd;
4537}
4538
Daniel Vetter02d71952012-08-09 16:44:54 +02004539static void __i915_update_gfx_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02004540{
4541 struct timespec now, diff1;
4542 u64 diff;
4543 unsigned long diffms;
4544 u32 count;
4545
Daniel Vetter02d71952012-08-09 16:44:54 +02004546 assert_spin_locked(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004547
4548 getrawmonotonic(&now);
Daniel Vetter20e4d402012-08-08 23:35:39 +02004549 diff1 = timespec_sub(now, dev_priv->ips.last_time2);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004550
4551 /* Don't divide by 0 */
4552 diffms = diff1.tv_sec * 1000 + diff1.tv_nsec / 1000000;
4553 if (!diffms)
4554 return;
4555
4556 count = I915_READ(GFXEC);
4557
Daniel Vetter20e4d402012-08-08 23:35:39 +02004558 if (count < dev_priv->ips.last_count2) {
4559 diff = ~0UL - dev_priv->ips.last_count2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02004560 diff += count;
4561 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +02004562 diff = count - dev_priv->ips.last_count2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02004563 }
4564
Daniel Vetter20e4d402012-08-08 23:35:39 +02004565 dev_priv->ips.last_count2 = count;
4566 dev_priv->ips.last_time2 = now;
Daniel Vettereb48eb02012-04-26 23:28:12 +02004567
4568 /* More magic constants... */
4569 diff = diff * 1181;
4570 diff = div_u64(diff, diffms * 10);
Daniel Vetter20e4d402012-08-08 23:35:39 +02004571 dev_priv->ips.gfx_power = diff;
Daniel Vettereb48eb02012-04-26 23:28:12 +02004572}
4573
Daniel Vetter02d71952012-08-09 16:44:54 +02004574void i915_update_gfx_val(struct drm_i915_private *dev_priv)
4575{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00004576 struct drm_device *dev = dev_priv->dev;
4577
4578 if (INTEL_INFO(dev)->gen != 5)
Daniel Vetter02d71952012-08-09 16:44:54 +02004579 return;
4580
Daniel Vetter92703882012-08-09 16:46:01 +02004581 spin_lock_irq(&mchdev_lock);
Daniel Vetter02d71952012-08-09 16:44:54 +02004582
4583 __i915_update_gfx_val(dev_priv);
4584
Daniel Vetter92703882012-08-09 16:46:01 +02004585 spin_unlock_irq(&mchdev_lock);
Daniel Vetter02d71952012-08-09 16:44:54 +02004586}
4587
Chris Wilsonf531dcb22012-09-25 10:16:12 +01004588static unsigned long __i915_gfx_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02004589{
4590 unsigned long t, corr, state1, corr2, state2;
4591 u32 pxvid, ext_v;
4592
Daniel Vetter02d71952012-08-09 16:44:54 +02004593 assert_spin_locked(&mchdev_lock);
4594
Ben Widawskyb39fb292014-03-19 18:31:11 -07004595 pxvid = I915_READ(PXVFREQ_BASE + (dev_priv->rps.cur_freq * 4));
Daniel Vettereb48eb02012-04-26 23:28:12 +02004596 pxvid = (pxvid >> 24) & 0x7f;
4597 ext_v = pvid_to_extvid(dev_priv, pxvid);
4598
4599 state1 = ext_v;
4600
4601 t = i915_mch_val(dev_priv);
4602
4603 /* Revel in the empirically derived constants */
4604
4605 /* Correction factor in 1/100000 units */
4606 if (t > 80)
4607 corr = ((t * 2349) + 135940);
4608 else if (t >= 50)
4609 corr = ((t * 964) + 29317);
4610 else /* < 50 */
4611 corr = ((t * 301) + 1004);
4612
4613 corr = corr * ((150142 * state1) / 10000 - 78642);
4614 corr /= 100000;
Daniel Vetter20e4d402012-08-08 23:35:39 +02004615 corr2 = (corr * dev_priv->ips.corr);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004616
4617 state2 = (corr2 * state1) / 10000;
4618 state2 /= 100; /* convert to mW */
4619
Daniel Vetter02d71952012-08-09 16:44:54 +02004620 __i915_update_gfx_val(dev_priv);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004621
Daniel Vetter20e4d402012-08-08 23:35:39 +02004622 return dev_priv->ips.gfx_power + state2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02004623}
4624
Chris Wilsonf531dcb22012-09-25 10:16:12 +01004625unsigned long i915_gfx_val(struct drm_i915_private *dev_priv)
4626{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00004627 struct drm_device *dev = dev_priv->dev;
Chris Wilsonf531dcb22012-09-25 10:16:12 +01004628 unsigned long val;
4629
Damien Lespiau3d13ef22014-02-07 19:12:47 +00004630 if (INTEL_INFO(dev)->gen != 5)
Chris Wilsonf531dcb22012-09-25 10:16:12 +01004631 return 0;
4632
4633 spin_lock_irq(&mchdev_lock);
4634
4635 val = __i915_gfx_val(dev_priv);
4636
4637 spin_unlock_irq(&mchdev_lock);
4638
4639 return val;
4640}
4641
Daniel Vettereb48eb02012-04-26 23:28:12 +02004642/**
4643 * i915_read_mch_val - return value for IPS use
4644 *
4645 * Calculate and return a value for the IPS driver to use when deciding whether
4646 * we have thermal and power headroom to increase CPU or GPU power budget.
4647 */
4648unsigned long i915_read_mch_val(void)
4649{
4650 struct drm_i915_private *dev_priv;
4651 unsigned long chipset_val, graphics_val, ret = 0;
4652
Daniel Vetter92703882012-08-09 16:46:01 +02004653 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004654 if (!i915_mch_dev)
4655 goto out_unlock;
4656 dev_priv = i915_mch_dev;
4657
Chris Wilsonf531dcb22012-09-25 10:16:12 +01004658 chipset_val = __i915_chipset_val(dev_priv);
4659 graphics_val = __i915_gfx_val(dev_priv);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004660
4661 ret = chipset_val + graphics_val;
4662
4663out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02004664 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004665
4666 return ret;
4667}
4668EXPORT_SYMBOL_GPL(i915_read_mch_val);
4669
4670/**
4671 * i915_gpu_raise - raise GPU frequency limit
4672 *
4673 * Raise the limit; IPS indicates we have thermal headroom.
4674 */
4675bool i915_gpu_raise(void)
4676{
4677 struct drm_i915_private *dev_priv;
4678 bool ret = true;
4679
Daniel Vetter92703882012-08-09 16:46:01 +02004680 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004681 if (!i915_mch_dev) {
4682 ret = false;
4683 goto out_unlock;
4684 }
4685 dev_priv = i915_mch_dev;
4686
Daniel Vetter20e4d402012-08-08 23:35:39 +02004687 if (dev_priv->ips.max_delay > dev_priv->ips.fmax)
4688 dev_priv->ips.max_delay--;
Daniel Vettereb48eb02012-04-26 23:28:12 +02004689
4690out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02004691 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004692
4693 return ret;
4694}
4695EXPORT_SYMBOL_GPL(i915_gpu_raise);
4696
4697/**
4698 * i915_gpu_lower - lower GPU frequency limit
4699 *
4700 * IPS indicates we're close to a thermal limit, so throttle back the GPU
4701 * frequency maximum.
4702 */
4703bool i915_gpu_lower(void)
4704{
4705 struct drm_i915_private *dev_priv;
4706 bool ret = true;
4707
Daniel Vetter92703882012-08-09 16:46:01 +02004708 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004709 if (!i915_mch_dev) {
4710 ret = false;
4711 goto out_unlock;
4712 }
4713 dev_priv = i915_mch_dev;
4714
Daniel Vetter20e4d402012-08-08 23:35:39 +02004715 if (dev_priv->ips.max_delay < dev_priv->ips.min_delay)
4716 dev_priv->ips.max_delay++;
Daniel Vettereb48eb02012-04-26 23:28:12 +02004717
4718out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02004719 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004720
4721 return ret;
4722}
4723EXPORT_SYMBOL_GPL(i915_gpu_lower);
4724
4725/**
4726 * i915_gpu_busy - indicate GPU business to IPS
4727 *
4728 * Tell the IPS driver whether or not the GPU is busy.
4729 */
4730bool i915_gpu_busy(void)
4731{
4732 struct drm_i915_private *dev_priv;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01004733 struct intel_engine_cs *ring;
Daniel Vettereb48eb02012-04-26 23:28:12 +02004734 bool ret = false;
Chris Wilsonf047e392012-07-21 12:31:41 +01004735 int i;
Daniel Vettereb48eb02012-04-26 23:28:12 +02004736
Daniel Vetter92703882012-08-09 16:46:01 +02004737 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004738 if (!i915_mch_dev)
4739 goto out_unlock;
4740 dev_priv = i915_mch_dev;
4741
Chris Wilsonf047e392012-07-21 12:31:41 +01004742 for_each_ring(ring, dev_priv, i)
4743 ret |= !list_empty(&ring->request_list);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004744
4745out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02004746 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004747
4748 return ret;
4749}
4750EXPORT_SYMBOL_GPL(i915_gpu_busy);
4751
4752/**
4753 * i915_gpu_turbo_disable - disable graphics turbo
4754 *
4755 * Disable graphics turbo by resetting the max frequency and setting the
4756 * current frequency to the default.
4757 */
4758bool i915_gpu_turbo_disable(void)
4759{
4760 struct drm_i915_private *dev_priv;
4761 bool ret = true;
4762
Daniel Vetter92703882012-08-09 16:46:01 +02004763 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004764 if (!i915_mch_dev) {
4765 ret = false;
4766 goto out_unlock;
4767 }
4768 dev_priv = i915_mch_dev;
4769
Daniel Vetter20e4d402012-08-08 23:35:39 +02004770 dev_priv->ips.max_delay = dev_priv->ips.fstart;
Daniel Vettereb48eb02012-04-26 23:28:12 +02004771
Daniel Vetter20e4d402012-08-08 23:35:39 +02004772 if (!ironlake_set_drps(dev_priv->dev, dev_priv->ips.fstart))
Daniel Vettereb48eb02012-04-26 23:28:12 +02004773 ret = false;
4774
4775out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02004776 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004777
4778 return ret;
4779}
4780EXPORT_SYMBOL_GPL(i915_gpu_turbo_disable);
4781
4782/**
4783 * Tells the intel_ips driver that the i915 driver is now loaded, if
4784 * IPS got loaded first.
4785 *
4786 * This awkward dance is so that neither module has to depend on the
4787 * other in order for IPS to do the appropriate communication of
4788 * GPU turbo limits to i915.
4789 */
4790static void
4791ips_ping_for_i915_load(void)
4792{
4793 void (*link)(void);
4794
4795 link = symbol_get(ips_link_to_i915_driver);
4796 if (link) {
4797 link();
4798 symbol_put(ips_link_to_i915_driver);
4799 }
4800}
4801
4802void intel_gpu_ips_init(struct drm_i915_private *dev_priv)
4803{
Daniel Vetter02d71952012-08-09 16:44:54 +02004804 /* We only register the i915 ips part with intel-ips once everything is
4805 * set up, to avoid intel-ips sneaking in and reading bogus values. */
Daniel Vetter92703882012-08-09 16:46:01 +02004806 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004807 i915_mch_dev = dev_priv;
Daniel Vetter92703882012-08-09 16:46:01 +02004808 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004809
4810 ips_ping_for_i915_load();
4811}
4812
4813void intel_gpu_ips_teardown(void)
4814{
Daniel Vetter92703882012-08-09 16:46:01 +02004815 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004816 i915_mch_dev = NULL;
Daniel Vetter92703882012-08-09 16:46:01 +02004817 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004818}
Deepak S76c3552f2014-01-30 23:08:16 +05304819
Daniel Vetter8090c6b2012-06-24 16:42:32 +02004820static void intel_init_emon(struct drm_device *dev)
Eugeni Dodonovdde18882012-04-18 15:29:24 -03004821{
4822 struct drm_i915_private *dev_priv = dev->dev_private;
4823 u32 lcfuse;
4824 u8 pxw[16];
4825 int i;
4826
4827 /* Disable to program */
4828 I915_WRITE(ECR, 0);
4829 POSTING_READ(ECR);
4830
4831 /* Program energy weights for various events */
4832 I915_WRITE(SDEW, 0x15040d00);
4833 I915_WRITE(CSIEW0, 0x007f0000);
4834 I915_WRITE(CSIEW1, 0x1e220004);
4835 I915_WRITE(CSIEW2, 0x04000004);
4836
4837 for (i = 0; i < 5; i++)
4838 I915_WRITE(PEW + (i * 4), 0);
4839 for (i = 0; i < 3; i++)
4840 I915_WRITE(DEW + (i * 4), 0);
4841
4842 /* Program P-state weights to account for frequency power adjustment */
4843 for (i = 0; i < 16; i++) {
4844 u32 pxvidfreq = I915_READ(PXVFREQ_BASE + (i * 4));
4845 unsigned long freq = intel_pxfreq(pxvidfreq);
4846 unsigned long vid = (pxvidfreq & PXVFREQ_PX_MASK) >>
4847 PXVFREQ_PX_SHIFT;
4848 unsigned long val;
4849
4850 val = vid * vid;
4851 val *= (freq / 1000);
4852 val *= 255;
4853 val /= (127*127*900);
4854 if (val > 0xff)
4855 DRM_ERROR("bad pxval: %ld\n", val);
4856 pxw[i] = val;
4857 }
4858 /* Render standby states get 0 weight */
4859 pxw[14] = 0;
4860 pxw[15] = 0;
4861
4862 for (i = 0; i < 4; i++) {
4863 u32 val = (pxw[i*4] << 24) | (pxw[(i*4)+1] << 16) |
4864 (pxw[(i*4)+2] << 8) | (pxw[(i*4)+3]);
4865 I915_WRITE(PXW + (i * 4), val);
4866 }
4867
4868 /* Adjust magic regs to magic values (more experimental results) */
4869 I915_WRITE(OGW0, 0);
4870 I915_WRITE(OGW1, 0);
4871 I915_WRITE(EG0, 0x00007f00);
4872 I915_WRITE(EG1, 0x0000000e);
4873 I915_WRITE(EG2, 0x000e0000);
4874 I915_WRITE(EG3, 0x68000300);
4875 I915_WRITE(EG4, 0x42000000);
4876 I915_WRITE(EG5, 0x00140031);
4877 I915_WRITE(EG6, 0);
4878 I915_WRITE(EG7, 0);
4879
4880 for (i = 0; i < 8; i++)
4881 I915_WRITE(PXWL + (i * 4), 0);
4882
4883 /* Enable PMON + select events */
4884 I915_WRITE(ECR, 0x80000019);
4885
4886 lcfuse = I915_READ(LCFUSE02);
4887
Daniel Vetter20e4d402012-08-08 23:35:39 +02004888 dev_priv->ips.corr = (lcfuse & LCFUSE_HIV_MASK);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03004889}
4890
Imre Deakae484342014-03-31 15:10:44 +03004891void intel_init_gt_powersave(struct drm_device *dev)
4892{
Imre Deake6069ca2014-04-18 16:01:02 +03004893 i915.enable_rc6 = sanitize_rc6_option(dev, i915.enable_rc6);
4894
Deepak S38807742014-05-23 21:00:15 +05304895 if (IS_CHERRYVIEW(dev))
4896 cherryview_init_gt_powersave(dev);
4897 else if (IS_VALLEYVIEW(dev))
Imre Deak4e805192014-04-14 20:24:41 +03004898 valleyview_init_gt_powersave(dev);
Imre Deakae484342014-03-31 15:10:44 +03004899}
4900
4901void intel_cleanup_gt_powersave(struct drm_device *dev)
4902{
Deepak S38807742014-05-23 21:00:15 +05304903 if (IS_CHERRYVIEW(dev))
4904 return;
4905 else if (IS_VALLEYVIEW(dev))
Imre Deak4e805192014-04-14 20:24:41 +03004906 valleyview_cleanup_gt_powersave(dev);
Imre Deakae484342014-03-31 15:10:44 +03004907}
4908
Jesse Barnes156c7ca2014-06-12 08:35:45 -07004909/**
4910 * intel_suspend_gt_powersave - suspend PM work and helper threads
4911 * @dev: drm device
4912 *
4913 * We don't want to disable RC6 or other features here, we just want
4914 * to make sure any work we've queued has finished and won't bother
4915 * us while we're suspended.
4916 */
4917void intel_suspend_gt_powersave(struct drm_device *dev)
4918{
4919 struct drm_i915_private *dev_priv = dev->dev_private;
4920
4921 /* Interrupts should be disabled already to avoid re-arming. */
Jesse Barnese11aa362014-06-18 09:52:55 -07004922 WARN_ON(dev->irq_enabled && !dev_priv->pm.irqs_disabled);
Jesse Barnes156c7ca2014-06-12 08:35:45 -07004923
4924 flush_delayed_work(&dev_priv->rps.delayed_resume_work);
4925
4926 cancel_work_sync(&dev_priv->rps.work);
4927}
4928
Daniel Vetter8090c6b2012-06-24 16:42:32 +02004929void intel_disable_gt_powersave(struct drm_device *dev)
4930{
Jesse Barnes1a01ab32012-11-02 11:14:00 -07004931 struct drm_i915_private *dev_priv = dev->dev_private;
4932
Daniel Vetterfd0c0642013-04-24 11:13:35 +02004933 /* Interrupts should be disabled already to avoid re-arming. */
Jesse Barnese11aa362014-06-18 09:52:55 -07004934 WARN_ON(dev->irq_enabled && !dev_priv->pm.irqs_disabled);
Daniel Vetterfd0c0642013-04-24 11:13:35 +02004935
Daniel Vetter930ebb42012-06-29 23:32:16 +02004936 if (IS_IRONLAKE_M(dev)) {
Daniel Vetter8090c6b2012-06-24 16:42:32 +02004937 ironlake_disable_drps(dev);
Daniel Vetter930ebb42012-06-29 23:32:16 +02004938 ironlake_disable_rc6(dev);
Deepak S38807742014-05-23 21:00:15 +05304939 } else if (INTEL_INFO(dev)->gen >= 6) {
Daniel Vetter10d8d362014-06-12 17:48:52 +02004940 intel_suspend_gt_powersave(dev);
Imre Deake4948372014-05-12 18:35:04 +03004941
Jesse Barnes4fc688c2012-11-02 11:14:01 -07004942 mutex_lock(&dev_priv->rps.hw_lock);
Deepak S38807742014-05-23 21:00:15 +05304943 if (IS_CHERRYVIEW(dev))
4944 cherryview_disable_rps(dev);
4945 else if (IS_VALLEYVIEW(dev))
Jesse Barnesd20d4f02013-04-23 10:09:28 -07004946 valleyview_disable_rps(dev);
4947 else
4948 gen6_disable_rps(dev);
Chris Wilsonc0951f02013-10-10 21:58:50 +01004949 dev_priv->rps.enabled = false;
Jesse Barnes4fc688c2012-11-02 11:14:01 -07004950 mutex_unlock(&dev_priv->rps.hw_lock);
Daniel Vetter930ebb42012-06-29 23:32:16 +02004951 }
Daniel Vetter8090c6b2012-06-24 16:42:32 +02004952}
4953
Jesse Barnes1a01ab32012-11-02 11:14:00 -07004954static void intel_gen6_powersave_work(struct work_struct *work)
4955{
4956 struct drm_i915_private *dev_priv =
4957 container_of(work, struct drm_i915_private,
4958 rps.delayed_resume_work.work);
4959 struct drm_device *dev = dev_priv->dev;
4960
Jesse Barnes4fc688c2012-11-02 11:14:01 -07004961 mutex_lock(&dev_priv->rps.hw_lock);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004962
Deepak S38807742014-05-23 21:00:15 +05304963 if (IS_CHERRYVIEW(dev)) {
4964 cherryview_enable_rps(dev);
4965 } else if (IS_VALLEYVIEW(dev)) {
Jesse Barnes0a073b82013-04-17 15:54:58 -07004966 valleyview_enable_rps(dev);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004967 } else if (IS_BROADWELL(dev)) {
4968 gen8_enable_rps(dev);
Imre Deakc2bc2fc2014-04-18 16:16:23 +03004969 __gen6_update_ring_freq(dev);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004970 } else {
4971 gen6_enable_rps(dev);
Imre Deakc2bc2fc2014-04-18 16:16:23 +03004972 __gen6_update_ring_freq(dev);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004973 }
Chris Wilsonc0951f02013-10-10 21:58:50 +01004974 dev_priv->rps.enabled = true;
Jesse Barnes4fc688c2012-11-02 11:14:01 -07004975 mutex_unlock(&dev_priv->rps.hw_lock);
Imre Deakc6df39b2014-04-14 20:24:29 +03004976
4977 intel_runtime_pm_put(dev_priv);
Jesse Barnes1a01ab32012-11-02 11:14:00 -07004978}
4979
Daniel Vetter8090c6b2012-06-24 16:42:32 +02004980void intel_enable_gt_powersave(struct drm_device *dev)
4981{
Jesse Barnes1a01ab32012-11-02 11:14:00 -07004982 struct drm_i915_private *dev_priv = dev->dev_private;
4983
Daniel Vetter8090c6b2012-06-24 16:42:32 +02004984 if (IS_IRONLAKE_M(dev)) {
Imre Deakdc1d0132014-04-14 20:24:28 +03004985 mutex_lock(&dev->struct_mutex);
Daniel Vetter8090c6b2012-06-24 16:42:32 +02004986 ironlake_enable_drps(dev);
4987 ironlake_enable_rc6(dev);
4988 intel_init_emon(dev);
Imre Deakdc1d0132014-04-14 20:24:28 +03004989 mutex_unlock(&dev->struct_mutex);
Deepak S38807742014-05-23 21:00:15 +05304990 } else if (INTEL_INFO(dev)->gen >= 6) {
Jesse Barnes1a01ab32012-11-02 11:14:00 -07004991 /*
4992 * PCU communication is slow and this doesn't need to be
4993 * done at any specific time, so do this out of our fast path
4994 * to make resume and init faster.
Imre Deakc6df39b2014-04-14 20:24:29 +03004995 *
4996 * We depend on the HW RC6 power context save/restore
4997 * mechanism when entering D3 through runtime PM suspend. So
4998 * disable RPM until RPS/RC6 is properly setup. We can only
4999 * get here via the driver load/system resume/runtime resume
5000 * paths, so the _noresume version is enough (and in case of
5001 * runtime resume it's necessary).
Jesse Barnes1a01ab32012-11-02 11:14:00 -07005002 */
Imre Deakc6df39b2014-04-14 20:24:29 +03005003 if (schedule_delayed_work(&dev_priv->rps.delayed_resume_work,
5004 round_jiffies_up_relative(HZ)))
5005 intel_runtime_pm_get_noresume(dev_priv);
Daniel Vetter8090c6b2012-06-24 16:42:32 +02005006 }
5007}
5008
Imre Deakc6df39b2014-04-14 20:24:29 +03005009void intel_reset_gt_powersave(struct drm_device *dev)
5010{
5011 struct drm_i915_private *dev_priv = dev->dev_private;
5012
5013 dev_priv->rps.enabled = false;
5014 intel_enable_gt_powersave(dev);
5015}
5016
Daniel Vetter3107bd42012-10-31 22:52:31 +01005017static void ibx_init_clock_gating(struct drm_device *dev)
5018{
5019 struct drm_i915_private *dev_priv = dev->dev_private;
5020
5021 /*
5022 * On Ibex Peak and Cougar Point, we need to disable clock
5023 * gating for the panel power sequencer or it will fail to
5024 * start up when no ports are active.
5025 */
5026 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE);
5027}
5028
Ville Syrjälä0e088b82013-06-07 10:47:04 +03005029static void g4x_disable_trickle_feed(struct drm_device *dev)
5030{
5031 struct drm_i915_private *dev_priv = dev->dev_private;
5032 int pipe;
5033
5034 for_each_pipe(pipe) {
5035 I915_WRITE(DSPCNTR(pipe),
5036 I915_READ(DSPCNTR(pipe)) |
5037 DISPPLANE_TRICKLE_FEED_DISABLE);
Ville Syrjälä1dba99f2013-10-01 18:02:18 +03005038 intel_flush_primary_plane(dev_priv, pipe);
Ville Syrjälä0e088b82013-06-07 10:47:04 +03005039 }
5040}
5041
Ville Syrjälä017636c2013-12-05 15:51:37 +02005042static void ilk_init_lp_watermarks(struct drm_device *dev)
5043{
5044 struct drm_i915_private *dev_priv = dev->dev_private;
5045
5046 I915_WRITE(WM3_LP_ILK, I915_READ(WM3_LP_ILK) & ~WM1_LP_SR_EN);
5047 I915_WRITE(WM2_LP_ILK, I915_READ(WM2_LP_ILK) & ~WM1_LP_SR_EN);
5048 I915_WRITE(WM1_LP_ILK, I915_READ(WM1_LP_ILK) & ~WM1_LP_SR_EN);
5049
5050 /*
5051 * Don't touch WM1S_LP_EN here.
5052 * Doing so could cause underruns.
5053 */
5054}
5055
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005056static void ironlake_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005057{
5058 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau231e54f2012-10-19 17:55:41 +01005059 uint32_t dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005060
Damien Lespiauf1e8fa52013-06-07 17:41:09 +01005061 /*
5062 * Required for FBC
5063 * WaFbcDisableDpfcClockGating:ilk
5064 */
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01005065 dspclk_gate |= ILK_DPFCRUNIT_CLOCK_GATE_DISABLE |
5066 ILK_DPFCUNIT_CLOCK_GATE_DISABLE |
5067 ILK_DPFDUNIT_CLOCK_GATE_ENABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005068
5069 I915_WRITE(PCH_3DCGDIS0,
5070 MARIUNIT_CLOCK_GATE_DISABLE |
5071 SVSMUNIT_CLOCK_GATE_DISABLE);
5072 I915_WRITE(PCH_3DCGDIS1,
5073 VFMUNIT_CLOCK_GATE_DISABLE);
5074
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005075 /*
5076 * According to the spec the following bits should be set in
5077 * order to enable memory self-refresh
5078 * The bit 22/21 of 0x42004
5079 * The bit 5 of 0x42020
5080 * The bit 15 of 0x45000
5081 */
5082 I915_WRITE(ILK_DISPLAY_CHICKEN2,
5083 (I915_READ(ILK_DISPLAY_CHICKEN2) |
5084 ILK_DPARB_GATE | ILK_VSDPFD_FULL));
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01005085 dspclk_gate |= ILK_DPARBUNIT_CLOCK_GATE_ENABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005086 I915_WRITE(DISP_ARB_CTL,
5087 (I915_READ(DISP_ARB_CTL) |
5088 DISP_FBC_WM_DIS));
Ville Syrjälä017636c2013-12-05 15:51:37 +02005089
5090 ilk_init_lp_watermarks(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005091
5092 /*
5093 * Based on the document from hardware guys the following bits
5094 * should be set unconditionally in order to enable FBC.
5095 * The bit 22 of 0x42000
5096 * The bit 22 of 0x42004
5097 * The bit 7,8,9 of 0x42020.
5098 */
5099 if (IS_IRONLAKE_M(dev)) {
Damien Lespiau4bb35332013-06-14 15:23:24 +01005100 /* WaFbcAsynchFlipDisableFbcQueue:ilk */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005101 I915_WRITE(ILK_DISPLAY_CHICKEN1,
5102 I915_READ(ILK_DISPLAY_CHICKEN1) |
5103 ILK_FBCQ_DIS);
5104 I915_WRITE(ILK_DISPLAY_CHICKEN2,
5105 I915_READ(ILK_DISPLAY_CHICKEN2) |
5106 ILK_DPARB_GATE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005107 }
5108
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01005109 I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
5110
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005111 I915_WRITE(ILK_DISPLAY_CHICKEN2,
5112 I915_READ(ILK_DISPLAY_CHICKEN2) |
5113 ILK_ELPIN_409_SELECT);
5114 I915_WRITE(_3D_CHICKEN2,
5115 _3D_CHICKEN2_WM_READ_PIPELINED << 16 |
5116 _3D_CHICKEN2_WM_READ_PIPELINED);
Daniel Vetter4358a372012-10-18 11:49:51 +02005117
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005118 /* WaDisableRenderCachePipelinedFlush:ilk */
Daniel Vetter4358a372012-10-18 11:49:51 +02005119 I915_WRITE(CACHE_MODE_0,
5120 _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
Daniel Vetter3107bd42012-10-31 22:52:31 +01005121
Akash Goel4e046322014-04-04 17:14:38 +05305122 /* WaDisable_RenderCache_OperationalFlush:ilk */
5123 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
5124
Ville Syrjälä0e088b82013-06-07 10:47:04 +03005125 g4x_disable_trickle_feed(dev);
Ville Syrjäläbdad2b22013-06-07 10:47:03 +03005126
Daniel Vetter3107bd42012-10-31 22:52:31 +01005127 ibx_init_clock_gating(dev);
5128}
5129
5130static void cpt_init_clock_gating(struct drm_device *dev)
5131{
5132 struct drm_i915_private *dev_priv = dev->dev_private;
5133 int pipe;
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03005134 uint32_t val;
Daniel Vetter3107bd42012-10-31 22:52:31 +01005135
5136 /*
5137 * On Ibex Peak and Cougar Point, we need to disable clock
5138 * gating for the panel power sequencer or it will fail to
5139 * start up when no ports are active.
5140 */
Jesse Barnescd664072013-10-02 10:34:19 -07005141 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE |
5142 PCH_DPLUNIT_CLOCK_GATE_DISABLE |
5143 PCH_CPUNIT_CLOCK_GATE_DISABLE);
Daniel Vetter3107bd42012-10-31 22:52:31 +01005144 I915_WRITE(SOUTH_CHICKEN2, I915_READ(SOUTH_CHICKEN2) |
5145 DPLS_EDP_PPS_FIX_DIS);
Takashi Iwai335c07b2012-12-11 11:46:29 +01005146 /* The below fixes the weird display corruption, a few pixels shifted
5147 * downward, on (only) LVDS of some HP laptops with IVY.
5148 */
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03005149 for_each_pipe(pipe) {
Paulo Zanonidc4bd2d2013-04-08 15:48:08 -03005150 val = I915_READ(TRANS_CHICKEN2(pipe));
5151 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
5152 val &= ~TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03005153 if (dev_priv->vbt.fdi_rx_polarity_inverted)
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03005154 val |= TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
Paulo Zanonidc4bd2d2013-04-08 15:48:08 -03005155 val &= ~TRANS_CHICKEN2_FRAME_START_DELAY_MASK;
5156 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_COUNTER;
5157 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_MODESWITCH;
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03005158 I915_WRITE(TRANS_CHICKEN2(pipe), val);
5159 }
Daniel Vetter3107bd42012-10-31 22:52:31 +01005160 /* WADP0ClockGatingDisable */
5161 for_each_pipe(pipe) {
5162 I915_WRITE(TRANS_CHICKEN1(pipe),
5163 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
5164 }
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005165}
5166
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01005167static void gen6_check_mch_setup(struct drm_device *dev)
5168{
5169 struct drm_i915_private *dev_priv = dev->dev_private;
5170 uint32_t tmp;
5171
5172 tmp = I915_READ(MCH_SSKPD);
5173 if ((tmp & MCH_SSKPD_WM0_MASK) != MCH_SSKPD_WM0_VAL) {
5174 DRM_INFO("Wrong MCH_SSKPD value: 0x%08x\n", tmp);
5175 DRM_INFO("This can cause pipe underruns and display issues.\n");
5176 DRM_INFO("Please upgrade your BIOS to fix this.\n");
5177 }
5178}
5179
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005180static void gen6_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005181{
5182 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau231e54f2012-10-19 17:55:41 +01005183 uint32_t dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005184
Damien Lespiau231e54f2012-10-19 17:55:41 +01005185 I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005186
5187 I915_WRITE(ILK_DISPLAY_CHICKEN2,
5188 I915_READ(ILK_DISPLAY_CHICKEN2) |
5189 ILK_ELPIN_409_SELECT);
5190
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005191 /* WaDisableHiZPlanesWhenMSAAEnabled:snb */
Daniel Vetter42839082012-12-14 23:38:28 +01005192 I915_WRITE(_3D_CHICKEN,
5193 _MASKED_BIT_ENABLE(_3D_CHICKEN_HIZ_PLANE_DISABLE_MSAA_4X_SNB));
5194
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005195 /* WaSetupGtModeTdRowDispatch:snb */
Daniel Vetter6547fbd2012-12-14 23:38:29 +01005196 if (IS_SNB_GT1(dev))
5197 I915_WRITE(GEN6_GT_MODE,
5198 _MASKED_BIT_ENABLE(GEN6_TD_FOUR_ROW_DISPATCH_DISABLE));
5199
Akash Goel4e046322014-04-04 17:14:38 +05305200 /* WaDisable_RenderCache_OperationalFlush:snb */
5201 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
5202
Ville Syrjälä8d85d272014-02-04 21:59:15 +02005203 /*
5204 * BSpec recoomends 8x4 when MSAA is used,
5205 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02005206 *
5207 * Note that PS/WM thread counts depend on the WIZ hashing
5208 * disable bit, which we don't touch here, but it's good
5209 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjälä8d85d272014-02-04 21:59:15 +02005210 */
5211 I915_WRITE(GEN6_GT_MODE,
5212 GEN6_WIZ_HASHING_MASK | GEN6_WIZ_HASHING_16x4);
5213
Ville Syrjälä017636c2013-12-05 15:51:37 +02005214 ilk_init_lp_watermarks(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005215
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005216 I915_WRITE(CACHE_MODE_0,
Daniel Vetter50743292012-04-26 22:02:54 +02005217 _MASKED_BIT_DISABLE(CM0_STC_EVICT_DISABLE_LRA_SNB));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005218
5219 I915_WRITE(GEN6_UCGCTL1,
5220 I915_READ(GEN6_UCGCTL1) |
5221 GEN6_BLBUNIT_CLOCK_GATE_DISABLE |
5222 GEN6_CSUNIT_CLOCK_GATE_DISABLE);
5223
5224 /* According to the BSpec vol1g, bit 12 (RCPBUNIT) clock
5225 * gating disable must be set. Failure to set it results in
5226 * flickering pixels due to Z write ordering failures after
5227 * some amount of runtime in the Mesa "fire" demo, and Unigine
5228 * Sanctuary and Tropics, and apparently anything else with
5229 * alpha test or pixel discard.
5230 *
5231 * According to the spec, bit 11 (RCCUNIT) must also be set,
5232 * but we didn't debug actual testcases to find it out.
Jesse Barnes0f846f82012-06-14 11:04:47 -07005233 *
Ville Syrjäläef593182014-01-22 21:32:47 +02005234 * WaDisableRCCUnitClockGating:snb
5235 * WaDisableRCPBUnitClockGating:snb
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005236 */
5237 I915_WRITE(GEN6_UCGCTL2,
5238 GEN6_RCPBUNIT_CLOCK_GATE_DISABLE |
5239 GEN6_RCCUNIT_CLOCK_GATE_DISABLE);
5240
Ville Syrjälä5eb146d2014-02-04 21:59:16 +02005241 /* WaStripsFansDisableFastClipPerformanceFix:snb */
Ville Syrjälä743b57d2014-02-04 21:59:17 +02005242 I915_WRITE(_3D_CHICKEN3,
5243 _MASKED_BIT_ENABLE(_3D_CHICKEN3_SF_DISABLE_FASTCLIP_CULL));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005244
5245 /*
Ville Syrjäläe927ecd2014-02-04 21:59:18 +02005246 * Bspec says:
5247 * "This bit must be set if 3DSTATE_CLIP clip mode is set to normal and
5248 * 3DSTATE_SF number of SF output attributes is more than 16."
5249 */
5250 I915_WRITE(_3D_CHICKEN3,
5251 _MASKED_BIT_ENABLE(_3D_CHICKEN3_SF_DISABLE_PIPELINED_ATTR_FETCH));
5252
5253 /*
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005254 * According to the spec the following bits should be
5255 * set in order to enable memory self-refresh and fbc:
5256 * The bit21 and bit22 of 0x42000
5257 * The bit21 and bit22 of 0x42004
5258 * The bit5 and bit7 of 0x42020
5259 * The bit14 of 0x70180
5260 * The bit14 of 0x71180
Damien Lespiau4bb35332013-06-14 15:23:24 +01005261 *
5262 * WaFbcAsynchFlipDisableFbcQueue:snb
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005263 */
5264 I915_WRITE(ILK_DISPLAY_CHICKEN1,
5265 I915_READ(ILK_DISPLAY_CHICKEN1) |
5266 ILK_FBCQ_DIS | ILK_PABSTRETCH_DIS);
5267 I915_WRITE(ILK_DISPLAY_CHICKEN2,
5268 I915_READ(ILK_DISPLAY_CHICKEN2) |
5269 ILK_DPARB_GATE | ILK_VSDPFD_FULL);
Damien Lespiau231e54f2012-10-19 17:55:41 +01005270 I915_WRITE(ILK_DSPCLK_GATE_D,
5271 I915_READ(ILK_DSPCLK_GATE_D) |
5272 ILK_DPARBUNIT_CLOCK_GATE_ENABLE |
5273 ILK_DPFDUNIT_CLOCK_GATE_ENABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005274
Ville Syrjälä0e088b82013-06-07 10:47:04 +03005275 g4x_disable_trickle_feed(dev);
Ben Widawskyf8f2ac92012-10-03 19:34:24 -07005276
Daniel Vetter3107bd42012-10-31 22:52:31 +01005277 cpt_init_clock_gating(dev);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01005278
5279 gen6_check_mch_setup(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005280}
5281
5282static void gen7_setup_fixed_func_scheduler(struct drm_i915_private *dev_priv)
5283{
5284 uint32_t reg = I915_READ(GEN7_FF_THREAD_MODE);
5285
Ville Syrjälä3aad9052014-01-22 21:32:59 +02005286 /*
Ville Syrjälä46680e02014-01-22 21:33:01 +02005287 * WaVSThreadDispatchOverride:ivb,vlv
Ville Syrjälä3aad9052014-01-22 21:32:59 +02005288 *
5289 * This actually overrides the dispatch
5290 * mode for all thread types.
5291 */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005292 reg &= ~GEN7_FF_SCHED_MASK;
5293 reg |= GEN7_FF_TS_SCHED_HW;
5294 reg |= GEN7_FF_VS_SCHED_HW;
5295 reg |= GEN7_FF_DS_SCHED_HW;
5296
5297 I915_WRITE(GEN7_FF_THREAD_MODE, reg);
5298}
5299
Paulo Zanoni17a303e2012-11-20 15:12:07 -02005300static void lpt_init_clock_gating(struct drm_device *dev)
5301{
5302 struct drm_i915_private *dev_priv = dev->dev_private;
5303
5304 /*
5305 * TODO: this bit should only be enabled when really needed, then
5306 * disabled when not needed anymore in order to save power.
5307 */
5308 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE)
5309 I915_WRITE(SOUTH_DSPCLK_GATE_D,
5310 I915_READ(SOUTH_DSPCLK_GATE_D) |
5311 PCH_LP_PARTITION_LEVEL_DISABLE);
Paulo Zanoni0a790cd2013-04-17 18:15:49 -03005312
5313 /* WADPOClockGatingDisable:hsw */
5314 I915_WRITE(_TRANSA_CHICKEN1,
5315 I915_READ(_TRANSA_CHICKEN1) |
5316 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
Paulo Zanoni17a303e2012-11-20 15:12:07 -02005317}
5318
Imre Deak7d708ee2013-04-17 14:04:50 +03005319static void lpt_suspend_hw(struct drm_device *dev)
5320{
5321 struct drm_i915_private *dev_priv = dev->dev_private;
5322
5323 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
5324 uint32_t val = I915_READ(SOUTH_DSPCLK_GATE_D);
5325
5326 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
5327 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
5328 }
5329}
5330
Ben Widawsky1020a5c2013-11-02 21:07:06 -07005331static void gen8_init_clock_gating(struct drm_device *dev)
5332{
5333 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau07d27e22014-03-03 17:31:46 +00005334 enum pipe pipe;
Ben Widawsky1020a5c2013-11-02 21:07:06 -07005335
5336 I915_WRITE(WM3_LP_ILK, 0);
5337 I915_WRITE(WM2_LP_ILK, 0);
5338 I915_WRITE(WM1_LP_ILK, 0);
Ben Widawsky50ed5fb2013-11-02 21:07:40 -07005339
5340 /* FIXME(BDW): Check all the w/a, some might only apply to
5341 * pre-production hw. */
5342
Kenneth Graunkec8966e12014-02-26 23:59:30 -08005343 /* WaDisablePartialInstShootdown:bdw */
5344 I915_WRITE(GEN8_ROW_CHICKEN,
5345 _MASKED_BIT_ENABLE(PARTIAL_INSTRUCTION_SHOOTDOWN_DISABLE));
5346
Kenneth Graunke1411e6a2014-02-26 23:59:31 -08005347 /* WaDisableThreadStallDopClockGating:bdw */
5348 /* FIXME: Unclear whether we really need this on production bdw. */
5349 I915_WRITE(GEN8_ROW_CHICKEN,
5350 _MASKED_BIT_ENABLE(STALL_DOP_GATING_DISABLE));
5351
Damien Lespiau4167e322014-01-16 16:51:35 +00005352 /*
5353 * This GEN8_CENTROID_PIXEL_OPT_DIS W/A is only needed for
5354 * pre-production hardware
5355 */
Ben Widawskyfd392b62013-11-04 22:52:39 -08005356 I915_WRITE(HALF_SLICE_CHICKEN3,
5357 _MASKED_BIT_ENABLE(GEN8_CENTROID_PIXEL_OPT_DIS));
Ben Widawskybf663472013-11-02 21:07:57 -07005358 I915_WRITE(HALF_SLICE_CHICKEN3,
5359 _MASKED_BIT_ENABLE(GEN8_SAMPLER_POWER_BYPASS_DIS));
Ben Widawsky4afe8d32013-11-02 21:07:55 -07005360 I915_WRITE(GAMTARBMODE, _MASKED_BIT_ENABLE(ARB_MODE_BWGTLB_DISABLE));
5361
Ben Widawsky7f88da02013-11-02 21:07:58 -07005362 I915_WRITE(_3D_CHICKEN3,
5363 _3D_CHICKEN_SDE_LIMIT_FIFO_POLY_DEPTH(2));
5364
Ben Widawskya75f3622013-11-02 21:07:59 -07005365 I915_WRITE(COMMON_SLICE_CHICKEN2,
5366 _MASKED_BIT_ENABLE(GEN8_CSC2_SBE_VUE_CACHE_CONSERVATIVE));
5367
Ben Widawsky4c2e7a52013-11-02 21:08:00 -07005368 I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
5369 _MASKED_BIT_ENABLE(GEN7_SINGLE_SUBSCAN_DISPATCH_ENABLE));
5370
Ben Widawsky242a4012014-04-18 18:04:29 -03005371 /* WaDisableDopClockGating:bdw May not be needed for production */
5372 I915_WRITE(GEN7_ROW_CHICKEN2,
5373 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
5374
Ben Widawskyab57fff2013-12-12 15:28:04 -08005375 /* WaSwitchSolVfFArbitrationPriority:bdw */
Ben Widawsky50ed5fb2013-11-02 21:07:40 -07005376 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07005377
Ben Widawskyab57fff2013-12-12 15:28:04 -08005378 /* WaPsrDPAMaskVBlankInSRD:bdw */
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07005379 I915_WRITE(CHICKEN_PAR1_1,
5380 I915_READ(CHICKEN_PAR1_1) | DPA_MASK_VBLANK_SRD);
5381
Ben Widawskyab57fff2013-12-12 15:28:04 -08005382 /* WaPsrDPRSUnmaskVBlankInSRD:bdw */
Damien Lespiau07d27e22014-03-03 17:31:46 +00005383 for_each_pipe(pipe) {
5384 I915_WRITE(CHICKEN_PIPESL_1(pipe),
Ville Syrjäläc7c65622014-03-05 13:05:45 +02005385 I915_READ(CHICKEN_PIPESL_1(pipe)) |
Ville Syrjälä8f670bb2014-03-05 13:05:47 +02005386 BDW_DPRS_MASK_VBLANK_SRD);
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07005387 }
Ben Widawsky63801f22013-12-12 17:26:03 -08005388
5389 /* Use Force Non-Coherent whenever executing a 3D context. This is a
5390 * workaround for for a possible hang in the unlikely event a TLB
5391 * invalidation occurs during a PSD flush.
5392 */
5393 I915_WRITE(HDC_CHICKEN0,
5394 I915_READ(HDC_CHICKEN0) |
5395 _MASKED_BIT_ENABLE(HDC_FORCE_NON_COHERENT));
Ben Widawskyab57fff2013-12-12 15:28:04 -08005396
5397 /* WaVSRefCountFullforceMissDisable:bdw */
5398 /* WaDSRefCountFullforceMissDisable:bdw */
5399 I915_WRITE(GEN7_FF_THREAD_MODE,
5400 I915_READ(GEN7_FF_THREAD_MODE) &
5401 ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
Ville Syrjälä36075a42014-02-04 21:59:21 +02005402
5403 /*
5404 * BSpec recommends 8x4 when MSAA is used,
5405 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02005406 *
5407 * Note that PS/WM thread counts depend on the WIZ hashing
5408 * disable bit, which we don't touch here, but it's good
5409 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjälä36075a42014-02-04 21:59:21 +02005410 */
5411 I915_WRITE(GEN7_GT_MODE,
5412 GEN6_WIZ_HASHING_MASK | GEN6_WIZ_HASHING_16x4);
Ville Syrjälä295e8bb2014-02-27 21:59:01 +02005413
5414 I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
5415 _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
Ville Syrjälä4f1ca9e2014-02-27 21:59:02 +02005416
5417 /* WaDisableSDEUnitClockGating:bdw */
5418 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
5419 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Damien Lespiau5d708682014-03-26 18:41:51 +00005420
5421 /* Wa4x4STCOptimizationDisable:bdw */
5422 I915_WRITE(CACHE_MODE_1,
5423 _MASKED_BIT_ENABLE(GEN8_4x4_STC_OPTIMIZATION_DISABLE));
Ben Widawsky1020a5c2013-11-02 21:07:06 -07005424}
5425
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03005426static void haswell_init_clock_gating(struct drm_device *dev)
5427{
5428 struct drm_i915_private *dev_priv = dev->dev_private;
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03005429
Ville Syrjälä017636c2013-12-05 15:51:37 +02005430 ilk_init_lp_watermarks(dev);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03005431
Francisco Jerezf3fc4882013-10-02 15:53:16 -07005432 /* L3 caching of data atomics doesn't work -- disable it. */
5433 I915_WRITE(HSW_SCRATCH1, HSW_SCRATCH1_L3_DATA_ATOMICS_DISABLE);
5434 I915_WRITE(HSW_ROW_CHICKEN3,
5435 _MASKED_BIT_ENABLE(HSW_ROW_CHICKEN3_L3_GLOBAL_ATOMICS_DISABLE));
5436
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005437 /* This is required by WaCatErrorRejectionIssue:hsw */
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03005438 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
5439 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
5440 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
5441
Ville Syrjäläe36ea7f2014-01-22 21:33:00 +02005442 /* WaVSRefCountFullforceMissDisable:hsw */
5443 I915_WRITE(GEN7_FF_THREAD_MODE,
5444 I915_READ(GEN7_FF_THREAD_MODE) & ~GEN7_FF_VS_REF_CNT_FFME);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03005445
Akash Goel4e046322014-04-04 17:14:38 +05305446 /* WaDisable_RenderCache_OperationalFlush:hsw */
5447 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
5448
Chia-I Wufe27c602014-01-28 13:29:33 +08005449 /* enable HiZ Raw Stall Optimization */
5450 I915_WRITE(CACHE_MODE_0_GEN7,
5451 _MASKED_BIT_DISABLE(HIZ_RAW_STALL_OPT_DISABLE));
5452
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005453 /* WaDisable4x2SubspanOptimization:hsw */
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03005454 I915_WRITE(CACHE_MODE_1,
5455 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Eugeni Dodonov1544d9d2012-07-02 11:51:10 -03005456
Ville Syrjäläa12c4962014-02-04 21:59:20 +02005457 /*
5458 * BSpec recommends 8x4 when MSAA is used,
5459 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02005460 *
5461 * Note that PS/WM thread counts depend on the WIZ hashing
5462 * disable bit, which we don't touch here, but it's good
5463 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjäläa12c4962014-02-04 21:59:20 +02005464 */
5465 I915_WRITE(GEN7_GT_MODE,
5466 GEN6_WIZ_HASHING_MASK | GEN6_WIZ_HASHING_16x4);
5467
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005468 /* WaSwitchSolVfFArbitrationPriority:hsw */
Ben Widawskye3dff582013-03-20 14:49:14 -07005469 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
5470
Paulo Zanoni90a88642013-05-03 17:23:45 -03005471 /* WaRsPkgCStateDisplayPMReq:hsw */
5472 I915_WRITE(CHICKEN_PAR1_1,
5473 I915_READ(CHICKEN_PAR1_1) | FORCE_ARB_IDLE_PLANES);
Eugeni Dodonov1544d9d2012-07-02 11:51:10 -03005474
Paulo Zanoni17a303e2012-11-20 15:12:07 -02005475 lpt_init_clock_gating(dev);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03005476}
5477
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005478static void ivybridge_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005479{
5480 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawsky20848222012-05-04 18:58:59 -07005481 uint32_t snpcr;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005482
Ville Syrjälä017636c2013-12-05 15:51:37 +02005483 ilk_init_lp_watermarks(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005484
Damien Lespiau231e54f2012-10-19 17:55:41 +01005485 I915_WRITE(ILK_DSPCLK_GATE_D, ILK_VRHUNIT_CLOCK_GATE_DISABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005486
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005487 /* WaDisableEarlyCull:ivb */
Jesse Barnes87f80202012-10-02 17:43:41 -05005488 I915_WRITE(_3D_CHICKEN3,
5489 _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
5490
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005491 /* WaDisableBackToBackFlipFix:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005492 I915_WRITE(IVB_CHICKEN3,
5493 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
5494 CHICKEN3_DGMG_DONE_FIX_DISABLE);
5495
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005496 /* WaDisablePSDDualDispatchEnable:ivb */
Jesse Barnes12f33822012-10-25 12:15:45 -07005497 if (IS_IVB_GT1(dev))
5498 I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
5499 _MASKED_BIT_ENABLE(GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
Jesse Barnes12f33822012-10-25 12:15:45 -07005500
Akash Goel4e046322014-04-04 17:14:38 +05305501 /* WaDisable_RenderCache_OperationalFlush:ivb */
5502 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
5503
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005504 /* Apply the WaDisableRHWOOptimizationForRenderHang:ivb workaround. */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005505 I915_WRITE(GEN7_COMMON_SLICE_CHICKEN1,
5506 GEN7_CSC1_RHWO_OPT_DISABLE_IN_RCC);
5507
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005508 /* WaApplyL3ControlAndL3ChickenMode:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005509 I915_WRITE(GEN7_L3CNTLREG1,
5510 GEN7_WA_FOR_GEN7_L3_CONTROL);
5511 I915_WRITE(GEN7_L3_CHICKEN_MODE_REGISTER,
Jesse Barnes8ab43972012-10-25 12:15:42 -07005512 GEN7_WA_L3_CHICKEN_MODE);
5513 if (IS_IVB_GT1(dev))
5514 I915_WRITE(GEN7_ROW_CHICKEN2,
5515 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Ville Syrjälä412236c2014-01-22 21:32:44 +02005516 else {
5517 /* must write both registers */
5518 I915_WRITE(GEN7_ROW_CHICKEN2,
5519 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Jesse Barnes8ab43972012-10-25 12:15:42 -07005520 I915_WRITE(GEN7_ROW_CHICKEN2_GT2,
5521 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Ville Syrjälä412236c2014-01-22 21:32:44 +02005522 }
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005523
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005524 /* WaForceL3Serialization:ivb */
Jesse Barnes61939d92012-10-02 17:43:38 -05005525 I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
5526 ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
5527
Ville Syrjälä1b80a19a2014-01-22 21:32:53 +02005528 /*
Jesse Barnes0f846f82012-06-14 11:04:47 -07005529 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005530 * This implements the WaDisableRCZUnitClockGating:ivb workaround.
Jesse Barnes0f846f82012-06-14 11:04:47 -07005531 */
5532 I915_WRITE(GEN6_UCGCTL2,
Ville Syrjälä28acf3b2014-01-22 21:32:48 +02005533 GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
Jesse Barnes0f846f82012-06-14 11:04:47 -07005534
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005535 /* This is required by WaCatErrorRejectionIssue:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005536 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
5537 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
5538 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
5539
Ville Syrjälä0e088b82013-06-07 10:47:04 +03005540 g4x_disable_trickle_feed(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005541
5542 gen7_setup_fixed_func_scheduler(dev_priv);
Daniel Vetter97e19302012-04-24 16:00:21 +02005543
Chris Wilson22721342014-03-04 09:41:43 +00005544 if (0) { /* causes HiZ corruption on ivb:gt1 */
5545 /* enable HiZ Raw Stall Optimization */
5546 I915_WRITE(CACHE_MODE_0_GEN7,
5547 _MASKED_BIT_DISABLE(HIZ_RAW_STALL_OPT_DISABLE));
5548 }
Chia-I Wu116f2b62014-01-28 13:29:34 +08005549
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005550 /* WaDisable4x2SubspanOptimization:ivb */
Daniel Vetter97e19302012-04-24 16:00:21 +02005551 I915_WRITE(CACHE_MODE_1,
5552 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Ben Widawsky20848222012-05-04 18:58:59 -07005553
Ville Syrjäläa607c1a2014-02-04 21:59:19 +02005554 /*
5555 * BSpec recommends 8x4 when MSAA is used,
5556 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02005557 *
5558 * Note that PS/WM thread counts depend on the WIZ hashing
5559 * disable bit, which we don't touch here, but it's good
5560 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjäläa607c1a2014-02-04 21:59:19 +02005561 */
5562 I915_WRITE(GEN7_GT_MODE,
5563 GEN6_WIZ_HASHING_MASK | GEN6_WIZ_HASHING_16x4);
5564
Ben Widawsky20848222012-05-04 18:58:59 -07005565 snpcr = I915_READ(GEN6_MBCUNIT_SNPCR);
5566 snpcr &= ~GEN6_MBC_SNPCR_MASK;
5567 snpcr |= GEN6_MBC_SNPCR_MED;
5568 I915_WRITE(GEN6_MBCUNIT_SNPCR, snpcr);
Daniel Vetter3107bd42012-10-31 22:52:31 +01005569
Ben Widawskyab5c6082013-04-05 13:12:41 -07005570 if (!HAS_PCH_NOP(dev))
5571 cpt_init_clock_gating(dev);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01005572
5573 gen6_check_mch_setup(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005574}
5575
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005576static void valleyview_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005577{
5578 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes85b1d7b2013-11-04 11:52:45 -08005579 u32 val;
5580
5581 mutex_lock(&dev_priv->rps.hw_lock);
5582 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
5583 mutex_unlock(&dev_priv->rps.hw_lock);
5584 switch ((val >> 6) & 3) {
5585 case 0:
Deepak Sf6d51942014-04-03 21:01:28 +05305586 case 1:
Jesse Barnes85b1d7b2013-11-04 11:52:45 -08005587 dev_priv->mem_freq = 800;
5588 break;
Jesse Barnesf64a28a2013-11-04 16:07:00 -08005589 case 2:
Deepak Sf6d51942014-04-03 21:01:28 +05305590 dev_priv->mem_freq = 1066;
Jesse Barnes85b1d7b2013-11-04 11:52:45 -08005591 break;
Jesse Barnesf64a28a2013-11-04 16:07:00 -08005592 case 3:
Chon Ming Lee23259912013-11-07 15:23:26 +08005593 dev_priv->mem_freq = 1333;
Jesse Barnesf64a28a2013-11-04 16:07:00 -08005594 break;
Jesse Barnes85b1d7b2013-11-04 11:52:45 -08005595 }
5596 DRM_DEBUG_DRIVER("DDR speed: %d MHz", dev_priv->mem_freq);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005597
Ville Syrjäläd7fe0cc2013-05-21 18:01:50 +03005598 I915_WRITE(DSPCLK_GATE_D, VRHUNIT_CLOCK_GATE_DISABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005599
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005600 /* WaDisableEarlyCull:vlv */
Jesse Barnes87f80202012-10-02 17:43:41 -05005601 I915_WRITE(_3D_CHICKEN3,
5602 _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
5603
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005604 /* WaDisableBackToBackFlipFix:vlv */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005605 I915_WRITE(IVB_CHICKEN3,
5606 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
5607 CHICKEN3_DGMG_DONE_FIX_DISABLE);
5608
Ville Syrjäläfad7d362014-01-22 21:32:39 +02005609 /* WaPsdDispatchEnable:vlv */
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005610 /* WaDisablePSDDualDispatchEnable:vlv */
Jesse Barnes12f33822012-10-25 12:15:45 -07005611 I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
Jesse Barnesd3bc0302013-03-08 10:45:51 -08005612 _MASKED_BIT_ENABLE(GEN7_MAX_PS_THREAD_DEP |
5613 GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
Jesse Barnes12f33822012-10-25 12:15:45 -07005614
Akash Goel4e046322014-04-04 17:14:38 +05305615 /* WaDisable_RenderCache_OperationalFlush:vlv */
5616 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
5617
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005618 /* WaForceL3Serialization:vlv */
Jesse Barnes61939d92012-10-02 17:43:38 -05005619 I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
5620 ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
5621
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005622 /* WaDisableDopClockGating:vlv */
Jesse Barnes8ab43972012-10-25 12:15:42 -07005623 I915_WRITE(GEN7_ROW_CHICKEN2,
5624 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
5625
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005626 /* This is required by WaCatErrorRejectionIssue:vlv */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005627 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
5628 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
5629 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
5630
Ville Syrjälä46680e02014-01-22 21:33:01 +02005631 gen7_setup_fixed_func_scheduler(dev_priv);
5632
Ville Syrjälä3c0edae2014-01-22 21:32:56 +02005633 /*
Jesse Barnes0f846f82012-06-14 11:04:47 -07005634 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005635 * This implements the WaDisableRCZUnitClockGating:vlv workaround.
Jesse Barnes0f846f82012-06-14 11:04:47 -07005636 */
5637 I915_WRITE(GEN6_UCGCTL2,
Ville Syrjälä3c0edae2014-01-22 21:32:56 +02005638 GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
Jesse Barnes0f846f82012-06-14 11:04:47 -07005639
Akash Goelc98f5062014-03-24 23:00:07 +05305640 /* WaDisableL3Bank2xClockGate:vlv
5641 * Disabling L3 clock gating- MMIO 940c[25] = 1
5642 * Set bit 25, to disable L3_BANK_2x_CLK_GATING */
5643 I915_WRITE(GEN7_UCGCTL4,
5644 I915_READ(GEN7_UCGCTL4) | GEN7_L3BANK2X_CLOCK_GATE_DISABLE);
Jesse Barnese3f33d42012-06-14 11:04:50 -07005645
Ville Syrjäläe0d8d592013-06-12 22:11:18 +03005646 I915_WRITE(MI_ARB_VLV, MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005647
Ville Syrjäläafd58e72014-01-22 21:33:03 +02005648 /*
5649 * BSpec says this must be set, even though
5650 * WaDisable4x2SubspanOptimization isn't listed for VLV.
5651 */
Daniel Vetter6b26c862012-04-24 14:04:12 +02005652 I915_WRITE(CACHE_MODE_1,
5653 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Jesse Barnes79831172012-06-20 10:53:12 -07005654
5655 /*
Ville Syrjälä031994e2014-01-22 21:32:46 +02005656 * WaIncreaseL3CreditsForVLVB0:vlv
5657 * This is the hardware default actually.
5658 */
5659 I915_WRITE(GEN7_L3SQCREG1, VLV_B0_WA_L3SQCREG1_VALUE);
5660
5661 /*
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005662 * WaDisableVLVClockGating_VBIIssue:vlv
Jesse Barnes2d809572012-10-25 12:15:44 -07005663 * Disable clock gating on th GCFG unit to prevent a delay
5664 * in the reporting of vblank events.
5665 */
Ville Syrjälä7a0d1ee2014-01-22 21:33:04 +02005666 I915_WRITE(VLV_GUNIT_CLOCK_GATE, GCFG_DIS);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005667}
5668
Ville Syrjäläa4565da2014-04-09 13:28:10 +03005669static void cherryview_init_clock_gating(struct drm_device *dev)
5670{
5671 struct drm_i915_private *dev_priv = dev->dev_private;
5672
5673 I915_WRITE(DSPCLK_GATE_D, VRHUNIT_CLOCK_GATE_DISABLE);
5674
5675 I915_WRITE(MI_ARB_VLV, MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE);
Ville Syrjälädd811e72014-04-09 13:28:33 +03005676
5677 /* WaDisablePartialInstShootdown:chv */
5678 I915_WRITE(GEN8_ROW_CHICKEN,
5679 _MASKED_BIT_ENABLE(PARTIAL_INSTRUCTION_SHOOTDOWN_DISABLE));
Ville Syrjäläa7068022014-04-09 13:28:34 +03005680
5681 /* WaDisableThreadStallDopClockGating:chv */
5682 I915_WRITE(GEN8_ROW_CHICKEN,
5683 _MASKED_BIT_ENABLE(STALL_DOP_GATING_DISABLE));
Ville Syrjälä232ce332014-04-09 13:28:35 +03005684
5685 /* WaVSRefCountFullforceMissDisable:chv */
5686 /* WaDSRefCountFullforceMissDisable:chv */
5687 I915_WRITE(GEN7_FF_THREAD_MODE,
5688 I915_READ(GEN7_FF_THREAD_MODE) &
5689 ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
Ville Syrjäläacea6f92014-04-09 13:28:36 +03005690
5691 /* WaDisableSemaphoreAndSyncFlipWait:chv */
5692 I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
5693 _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
Ville Syrjälä08466972014-04-09 13:28:37 +03005694
5695 /* WaDisableCSUnitClockGating:chv */
5696 I915_WRITE(GEN6_UCGCTL1, I915_READ(GEN6_UCGCTL1) |
5697 GEN6_CSUNIT_CLOCK_GATE_DISABLE);
Ville Syrjäläc6317802014-04-09 13:28:38 +03005698
5699 /* WaDisableSDEUnitClockGating:chv */
5700 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
5701 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Rafael Barbalhoe0d34ce2014-04-09 13:28:40 +03005702
5703 /* WaDisableSamplerPowerBypass:chv (pre-production hw) */
5704 I915_WRITE(HALF_SLICE_CHICKEN3,
5705 _MASKED_BIT_ENABLE(GEN8_SAMPLER_POWER_BYPASS_DIS));
Ville Syrjäläe4443e42014-04-09 13:28:41 +03005706
5707 /* WaDisableGunitClockGating:chv (pre-production hw) */
5708 I915_WRITE(VLV_GUNIT_CLOCK_GATE, I915_READ(VLV_GUNIT_CLOCK_GATE) |
5709 GINT_DIS);
5710
5711 /* WaDisableFfDopClockGating:chv (pre-production hw) */
5712 I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
5713 _MASKED_BIT_ENABLE(GEN8_FF_DOP_CLOCK_GATE_DISABLE));
5714
5715 /* WaDisableDopClockGating:chv (pre-production hw) */
5716 I915_WRITE(GEN7_ROW_CHICKEN2,
5717 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
5718 I915_WRITE(GEN6_UCGCTL1, I915_READ(GEN6_UCGCTL1) |
5719 GEN6_EU_TCUNIT_CLOCK_GATE_DISABLE);
Ville Syrjäläa4565da2014-04-09 13:28:10 +03005720}
5721
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005722static void g4x_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005723{
5724 struct drm_i915_private *dev_priv = dev->dev_private;
5725 uint32_t dspclk_gate;
5726
5727 I915_WRITE(RENCLK_GATE_D1, 0);
5728 I915_WRITE(RENCLK_GATE_D2, VF_UNIT_CLOCK_GATE_DISABLE |
5729 GS_UNIT_CLOCK_GATE_DISABLE |
5730 CL_UNIT_CLOCK_GATE_DISABLE);
5731 I915_WRITE(RAMCLK_GATE_D, 0);
5732 dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE |
5733 OVRUNIT_CLOCK_GATE_DISABLE |
5734 OVCUNIT_CLOCK_GATE_DISABLE;
5735 if (IS_GM45(dev))
5736 dspclk_gate |= DSSUNIT_CLOCK_GATE_DISABLE;
5737 I915_WRITE(DSPCLK_GATE_D, dspclk_gate);
Daniel Vetter4358a372012-10-18 11:49:51 +02005738
5739 /* WaDisableRenderCachePipelinedFlush */
5740 I915_WRITE(CACHE_MODE_0,
5741 _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
Ville Syrjäläde1aa622013-06-07 10:47:01 +03005742
Akash Goel4e046322014-04-04 17:14:38 +05305743 /* WaDisable_RenderCache_OperationalFlush:g4x */
5744 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
5745
Ville Syrjälä0e088b82013-06-07 10:47:04 +03005746 g4x_disable_trickle_feed(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005747}
5748
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005749static void crestline_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005750{
5751 struct drm_i915_private *dev_priv = dev->dev_private;
5752
5753 I915_WRITE(RENCLK_GATE_D1, I965_RCC_CLOCK_GATE_DISABLE);
5754 I915_WRITE(RENCLK_GATE_D2, 0);
5755 I915_WRITE(DSPCLK_GATE_D, 0);
5756 I915_WRITE(RAMCLK_GATE_D, 0);
5757 I915_WRITE16(DEUC, 0);
Ville Syrjälä20f94962013-06-07 10:47:02 +03005758 I915_WRITE(MI_ARB_STATE,
5759 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Akash Goel4e046322014-04-04 17:14:38 +05305760
5761 /* WaDisable_RenderCache_OperationalFlush:gen4 */
5762 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005763}
5764
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005765static void broadwater_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005766{
5767 struct drm_i915_private *dev_priv = dev->dev_private;
5768
5769 I915_WRITE(RENCLK_GATE_D1, I965_RCZ_CLOCK_GATE_DISABLE |
5770 I965_RCC_CLOCK_GATE_DISABLE |
5771 I965_RCPB_CLOCK_GATE_DISABLE |
5772 I965_ISC_CLOCK_GATE_DISABLE |
5773 I965_FBC_CLOCK_GATE_DISABLE);
5774 I915_WRITE(RENCLK_GATE_D2, 0);
Ville Syrjälä20f94962013-06-07 10:47:02 +03005775 I915_WRITE(MI_ARB_STATE,
5776 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Akash Goel4e046322014-04-04 17:14:38 +05305777
5778 /* WaDisable_RenderCache_OperationalFlush:gen4 */
5779 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005780}
5781
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005782static void gen3_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005783{
5784 struct drm_i915_private *dev_priv = dev->dev_private;
5785 u32 dstate = I915_READ(D_STATE);
5786
5787 dstate |= DSTATE_PLL_D3_OFF | DSTATE_GFX_CLOCK_GATING |
5788 DSTATE_DOT_CLOCK_GATING;
5789 I915_WRITE(D_STATE, dstate);
Chris Wilson13a86b82012-04-24 14:51:43 +01005790
5791 if (IS_PINEVIEW(dev))
5792 I915_WRITE(ECOSKPD, _MASKED_BIT_ENABLE(ECO_GATING_CX_ONLY));
Daniel Vetter974a3b02012-09-09 11:54:16 +02005793
5794 /* IIR "flip pending" means done if this bit is set */
5795 I915_WRITE(ECOSKPD, _MASKED_BIT_DISABLE(ECO_FLIP_DONE));
Ville Syrjälä12fabbcb92014-02-25 15:13:38 +02005796
5797 /* interrupts should cause a wake up from C3 */
Ville Syrjälä32992542014-02-25 15:13:39 +02005798 I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_AGPBUSY_INT_EN));
Ville Syrjälädbb42742014-02-25 15:13:41 +02005799
5800 /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
5801 I915_WRITE(MI_ARB_STATE, _MASKED_BIT_ENABLE(MI_ARB_C3_LP_WRITE_ENABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005802}
5803
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005804static void i85x_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005805{
5806 struct drm_i915_private *dev_priv = dev->dev_private;
5807
5808 I915_WRITE(RENCLK_GATE_D1, SV_CLOCK_GATE_DISABLE);
Ville Syrjälä54e472a2014-02-25 15:13:40 +02005809
5810 /* interrupts should cause a wake up from C3 */
5811 I915_WRITE(MI_STATE, _MASKED_BIT_ENABLE(MI_AGPBUSY_INT_EN) |
5812 _MASKED_BIT_DISABLE(MI_AGPBUSY_830_MODE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005813}
5814
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005815static void i830_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005816{
5817 struct drm_i915_private *dev_priv = dev->dev_private;
5818
5819 I915_WRITE(DSPCLK_GATE_D, OVRUNIT_CLOCK_GATE_DISABLE);
5820}
5821
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005822void intel_init_clock_gating(struct drm_device *dev)
5823{
5824 struct drm_i915_private *dev_priv = dev->dev_private;
5825
5826 dev_priv->display.init_clock_gating(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005827}
5828
Imre Deak7d708ee2013-04-17 14:04:50 +03005829void intel_suspend_hw(struct drm_device *dev)
5830{
5831 if (HAS_PCH_LPT(dev))
5832 lpt_suspend_hw(dev);
5833}
5834
Imre Deakc1ca7272013-11-25 17:15:29 +02005835#define for_each_power_well(i, power_well, domain_mask, power_domains) \
5836 for (i = 0; \
5837 i < (power_domains)->power_well_count && \
5838 ((power_well) = &(power_domains)->power_wells[i]); \
5839 i++) \
5840 if ((power_well)->domains & (domain_mask))
5841
5842#define for_each_power_well_rev(i, power_well, domain_mask, power_domains) \
5843 for (i = (power_domains)->power_well_count - 1; \
5844 i >= 0 && ((power_well) = &(power_domains)->power_wells[i]);\
5845 i--) \
5846 if ((power_well)->domains & (domain_mask))
5847
Paulo Zanoni15d199e2013-03-22 14:14:13 -03005848/**
5849 * We should only use the power well if we explicitly asked the hardware to
5850 * enable it, so check if it's enabled and also check if we've requested it to
5851 * be enabled.
5852 */
Imre Deakda7e29b2014-02-18 00:02:02 +02005853static bool hsw_power_well_enabled(struct drm_i915_private *dev_priv,
Imre Deakc1ca7272013-11-25 17:15:29 +02005854 struct i915_power_well *power_well)
5855{
Imre Deakc1ca7272013-11-25 17:15:29 +02005856 return I915_READ(HSW_PWR_WELL_DRIVER) ==
5857 (HSW_PWR_WELL_ENABLE_REQUEST | HSW_PWR_WELL_STATE_ENABLED);
5858}
5859
Imre Deakbfafe932014-06-05 20:31:47 +03005860bool intel_display_power_enabled_unlocked(struct drm_i915_private *dev_priv,
5861 enum intel_display_power_domain domain)
Imre Deakddf9c532013-11-27 22:02:02 +02005862{
Imre Deakddf9c532013-11-27 22:02:02 +02005863 struct i915_power_domains *power_domains;
Imre Deakb8c000d2014-06-02 14:21:10 +03005864 struct i915_power_well *power_well;
5865 bool is_enabled;
5866 int i;
5867
5868 if (dev_priv->pm.suspended)
5869 return false;
Imre Deakddf9c532013-11-27 22:02:02 +02005870
5871 power_domains = &dev_priv->power_domains;
Imre Deakbfafe932014-06-05 20:31:47 +03005872
Imre Deakb8c000d2014-06-02 14:21:10 +03005873 is_enabled = true;
Imre Deakbfafe932014-06-05 20:31:47 +03005874
Imre Deakb8c000d2014-06-02 14:21:10 +03005875 for_each_power_well_rev(i, power_well, BIT(domain), power_domains) {
5876 if (power_well->always_on)
5877 continue;
Imre Deakddf9c532013-11-27 22:02:02 +02005878
Imre Deakbfafe932014-06-05 20:31:47 +03005879 if (!power_well->hw_enabled) {
Imre Deakb8c000d2014-06-02 14:21:10 +03005880 is_enabled = false;
5881 break;
5882 }
5883 }
Imre Deakbfafe932014-06-05 20:31:47 +03005884
Imre Deakb8c000d2014-06-02 14:21:10 +03005885 return is_enabled;
Imre Deakddf9c532013-11-27 22:02:02 +02005886}
5887
Imre Deakda7e29b2014-02-18 00:02:02 +02005888bool intel_display_power_enabled(struct drm_i915_private *dev_priv,
Paulo Zanonib97186f2013-05-03 12:15:36 -03005889 enum intel_display_power_domain domain)
Paulo Zanoni15d199e2013-03-22 14:14:13 -03005890{
Imre Deakc1ca7272013-11-25 17:15:29 +02005891 struct i915_power_domains *power_domains;
Imre Deakbfafe932014-06-05 20:31:47 +03005892 bool ret;
Paulo Zanoni882244a2014-04-01 14:55:12 -03005893
Imre Deakc1ca7272013-11-25 17:15:29 +02005894 power_domains = &dev_priv->power_domains;
5895
Imre Deakc1ca7272013-11-25 17:15:29 +02005896 mutex_lock(&power_domains->lock);
Imre Deakbfafe932014-06-05 20:31:47 +03005897 ret = intel_display_power_enabled_unlocked(dev_priv, domain);
Imre Deakc1ca7272013-11-25 17:15:29 +02005898 mutex_unlock(&power_domains->lock);
5899
Imre Deakbfafe932014-06-05 20:31:47 +03005900 return ret;
Paulo Zanoni15d199e2013-03-22 14:14:13 -03005901}
5902
Imre Deak93c73e82014-02-18 00:02:19 +02005903/*
5904 * Starting with Haswell, we have a "Power Down Well" that can be turned off
5905 * when not needed anymore. We have 4 registers that can request the power well
5906 * to be enabled, and it will only be disabled if none of the registers is
5907 * requesting it to be enabled.
5908 */
Paulo Zanonid5e8fdc2013-12-11 18:50:09 -02005909static void hsw_power_well_post_enable(struct drm_i915_private *dev_priv)
5910{
5911 struct drm_device *dev = dev_priv->dev;
5912 unsigned long irqflags;
5913
Paulo Zanonif9dcb0d2013-12-11 18:50:10 -02005914 /*
5915 * After we re-enable the power well, if we touch VGA register 0x3d5
5916 * we'll get unclaimed register interrupts. This stops after we write
5917 * anything to the VGA MSR register. The vgacon module uses this
5918 * register all the time, so if we unbind our driver and, as a
5919 * consequence, bind vgacon, we'll get stuck in an infinite loop at
5920 * console_unlock(). So make here we touch the VGA MSR register, making
5921 * sure vgacon can keep working normally without triggering interrupts
5922 * and error messages.
5923 */
5924 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
5925 outb(inb(VGA_MSR_READ), VGA_MSR_WRITE);
5926 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
5927
Paulo Zanonid5e8fdc2013-12-11 18:50:09 -02005928 if (IS_BROADWELL(dev)) {
5929 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
5930 I915_WRITE(GEN8_DE_PIPE_IMR(PIPE_B),
5931 dev_priv->de_irq_mask[PIPE_B]);
5932 I915_WRITE(GEN8_DE_PIPE_IER(PIPE_B),
5933 ~dev_priv->de_irq_mask[PIPE_B] |
5934 GEN8_PIPE_VBLANK);
5935 I915_WRITE(GEN8_DE_PIPE_IMR(PIPE_C),
5936 dev_priv->de_irq_mask[PIPE_C]);
5937 I915_WRITE(GEN8_DE_PIPE_IER(PIPE_C),
5938 ~dev_priv->de_irq_mask[PIPE_C] |
5939 GEN8_PIPE_VBLANK);
5940 POSTING_READ(GEN8_DE_PIPE_IER(PIPE_C));
5941 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
5942 }
5943}
5944
Imre Deakda7e29b2014-02-18 00:02:02 +02005945static void hsw_set_power_well(struct drm_i915_private *dev_priv,
Imre Deakc1ca7272013-11-25 17:15:29 +02005946 struct i915_power_well *power_well, bool enable)
Eugeni Dodonovd0d3e512012-05-09 15:37:16 -03005947{
Paulo Zanonifa42e232013-01-25 16:59:11 -02005948 bool is_enabled, enable_requested;
5949 uint32_t tmp;
Eugeni Dodonovd0d3e512012-05-09 15:37:16 -03005950
Paulo Zanonifa42e232013-01-25 16:59:11 -02005951 tmp = I915_READ(HSW_PWR_WELL_DRIVER);
Paulo Zanoni6aedd1f2013-08-02 16:22:25 -03005952 is_enabled = tmp & HSW_PWR_WELL_STATE_ENABLED;
5953 enable_requested = tmp & HSW_PWR_WELL_ENABLE_REQUEST;
Eugeni Dodonovd0d3e512012-05-09 15:37:16 -03005954
Paulo Zanonifa42e232013-01-25 16:59:11 -02005955 if (enable) {
5956 if (!enable_requested)
Paulo Zanoni6aedd1f2013-08-02 16:22:25 -03005957 I915_WRITE(HSW_PWR_WELL_DRIVER,
5958 HSW_PWR_WELL_ENABLE_REQUEST);
Eugeni Dodonovd0d3e512012-05-09 15:37:16 -03005959
Paulo Zanonifa42e232013-01-25 16:59:11 -02005960 if (!is_enabled) {
5961 DRM_DEBUG_KMS("Enabling power well\n");
5962 if (wait_for((I915_READ(HSW_PWR_WELL_DRIVER) &
Paulo Zanoni6aedd1f2013-08-02 16:22:25 -03005963 HSW_PWR_WELL_STATE_ENABLED), 20))
Paulo Zanonifa42e232013-01-25 16:59:11 -02005964 DRM_ERROR("Timeout enabling power well\n");
5965 }
Ben Widawsky596cc112013-11-11 14:46:28 -08005966
Paulo Zanonid5e8fdc2013-12-11 18:50:09 -02005967 hsw_power_well_post_enable(dev_priv);
Paulo Zanonifa42e232013-01-25 16:59:11 -02005968 } else {
5969 if (enable_requested) {
5970 I915_WRITE(HSW_PWR_WELL_DRIVER, 0);
Paulo Zanoni9dbd8fe2013-07-23 10:48:11 -03005971 POSTING_READ(HSW_PWR_WELL_DRIVER);
Paulo Zanonifa42e232013-01-25 16:59:11 -02005972 DRM_DEBUG_KMS("Requesting to disable the power well\n");
Eugeni Dodonovd0d3e512012-05-09 15:37:16 -03005973 }
5974 }
Paulo Zanonifa42e232013-01-25 16:59:11 -02005975}
Eugeni Dodonovd0d3e512012-05-09 15:37:16 -03005976
Imre Deakc6cb5822014-03-04 19:22:55 +02005977static void hsw_power_well_sync_hw(struct drm_i915_private *dev_priv,
5978 struct i915_power_well *power_well)
5979{
5980 hsw_set_power_well(dev_priv, power_well, power_well->count > 0);
5981
5982 /*
5983 * We're taking over the BIOS, so clear any requests made by it since
5984 * the driver is in charge now.
5985 */
5986 if (I915_READ(HSW_PWR_WELL_BIOS) & HSW_PWR_WELL_ENABLE_REQUEST)
5987 I915_WRITE(HSW_PWR_WELL_BIOS, 0);
5988}
5989
5990static void hsw_power_well_enable(struct drm_i915_private *dev_priv,
5991 struct i915_power_well *power_well)
5992{
Imre Deakc6cb5822014-03-04 19:22:55 +02005993 hsw_set_power_well(dev_priv, power_well, true);
5994}
5995
5996static void hsw_power_well_disable(struct drm_i915_private *dev_priv,
5997 struct i915_power_well *power_well)
5998{
5999 hsw_set_power_well(dev_priv, power_well, false);
Imre Deakc6cb5822014-03-04 19:22:55 +02006000}
6001
Imre Deaka45f44662014-03-04 19:22:56 +02006002static void i9xx_always_on_power_well_noop(struct drm_i915_private *dev_priv,
6003 struct i915_power_well *power_well)
6004{
6005}
6006
6007static bool i9xx_always_on_power_well_enabled(struct drm_i915_private *dev_priv,
6008 struct i915_power_well *power_well)
6009{
6010 return true;
6011}
6012
Ville Syrjäläd2011dc2014-06-13 13:37:56 +03006013static void vlv_set_power_well(struct drm_i915_private *dev_priv,
6014 struct i915_power_well *power_well, bool enable)
Imre Deak77961eb2014-03-05 16:20:56 +02006015{
Ville Syrjäläd2011dc2014-06-13 13:37:56 +03006016 enum punit_power_well power_well_id = power_well->data;
Imre Deak77961eb2014-03-05 16:20:56 +02006017 u32 mask;
6018 u32 state;
6019 u32 ctrl;
6020
6021 mask = PUNIT_PWRGT_MASK(power_well_id);
6022 state = enable ? PUNIT_PWRGT_PWR_ON(power_well_id) :
6023 PUNIT_PWRGT_PWR_GATE(power_well_id);
6024
6025 mutex_lock(&dev_priv->rps.hw_lock);
6026
6027#define COND \
6028 ((vlv_punit_read(dev_priv, PUNIT_REG_PWRGT_STATUS) & mask) == state)
6029
6030 if (COND)
6031 goto out;
6032
6033 ctrl = vlv_punit_read(dev_priv, PUNIT_REG_PWRGT_CTRL);
6034 ctrl &= ~mask;
6035 ctrl |= state;
6036 vlv_punit_write(dev_priv, PUNIT_REG_PWRGT_CTRL, ctrl);
6037
6038 if (wait_for(COND, 100))
6039 DRM_ERROR("timout setting power well state %08x (%08x)\n",
6040 state,
6041 vlv_punit_read(dev_priv, PUNIT_REG_PWRGT_CTRL));
6042
6043#undef COND
6044
6045out:
6046 mutex_unlock(&dev_priv->rps.hw_lock);
6047}
6048
6049static void vlv_power_well_sync_hw(struct drm_i915_private *dev_priv,
6050 struct i915_power_well *power_well)
6051{
6052 vlv_set_power_well(dev_priv, power_well, power_well->count > 0);
6053}
6054
6055static void vlv_power_well_enable(struct drm_i915_private *dev_priv,
6056 struct i915_power_well *power_well)
6057{
6058 vlv_set_power_well(dev_priv, power_well, true);
6059}
6060
6061static void vlv_power_well_disable(struct drm_i915_private *dev_priv,
6062 struct i915_power_well *power_well)
6063{
6064 vlv_set_power_well(dev_priv, power_well, false);
6065}
6066
6067static bool vlv_power_well_enabled(struct drm_i915_private *dev_priv,
6068 struct i915_power_well *power_well)
6069{
6070 int power_well_id = power_well->data;
6071 bool enabled = false;
6072 u32 mask;
6073 u32 state;
6074 u32 ctrl;
6075
6076 mask = PUNIT_PWRGT_MASK(power_well_id);
6077 ctrl = PUNIT_PWRGT_PWR_ON(power_well_id);
6078
6079 mutex_lock(&dev_priv->rps.hw_lock);
6080
6081 state = vlv_punit_read(dev_priv, PUNIT_REG_PWRGT_STATUS) & mask;
6082 /*
6083 * We only ever set the power-on and power-gate states, anything
6084 * else is unexpected.
6085 */
6086 WARN_ON(state != PUNIT_PWRGT_PWR_ON(power_well_id) &&
6087 state != PUNIT_PWRGT_PWR_GATE(power_well_id));
6088 if (state == ctrl)
6089 enabled = true;
6090
6091 /*
6092 * A transient state at this point would mean some unexpected party
6093 * is poking at the power controls too.
6094 */
6095 ctrl = vlv_punit_read(dev_priv, PUNIT_REG_PWRGT_CTRL) & mask;
6096 WARN_ON(ctrl != state);
6097
6098 mutex_unlock(&dev_priv->rps.hw_lock);
6099
6100 return enabled;
6101}
6102
6103static void vlv_display_power_well_enable(struct drm_i915_private *dev_priv,
6104 struct i915_power_well *power_well)
6105{
6106 WARN_ON_ONCE(power_well->data != PUNIT_POWER_WELL_DISP2D);
6107
6108 vlv_set_power_well(dev_priv, power_well, true);
6109
6110 spin_lock_irq(&dev_priv->irq_lock);
6111 valleyview_enable_display_irqs(dev_priv);
6112 spin_unlock_irq(&dev_priv->irq_lock);
6113
6114 /*
Imre Deak0d116a22014-04-25 13:19:05 +03006115 * During driver initialization/resume we can avoid restoring the
6116 * part of the HW/SW state that will be inited anyway explicitly.
Imre Deak77961eb2014-03-05 16:20:56 +02006117 */
Imre Deak0d116a22014-04-25 13:19:05 +03006118 if (dev_priv->power_domains.initializing)
6119 return;
6120
6121 intel_hpd_init(dev_priv->dev);
Imre Deak77961eb2014-03-05 16:20:56 +02006122
6123 i915_redisable_vga_power_on(dev_priv->dev);
6124}
6125
6126static void vlv_display_power_well_disable(struct drm_i915_private *dev_priv,
6127 struct i915_power_well *power_well)
6128{
Imre Deak77961eb2014-03-05 16:20:56 +02006129 WARN_ON_ONCE(power_well->data != PUNIT_POWER_WELL_DISP2D);
6130
6131 spin_lock_irq(&dev_priv->irq_lock);
Imre Deak77961eb2014-03-05 16:20:56 +02006132 valleyview_disable_display_irqs(dev_priv);
6133 spin_unlock_irq(&dev_priv->irq_lock);
6134
Imre Deak77961eb2014-03-05 16:20:56 +02006135 vlv_set_power_well(dev_priv, power_well, false);
6136}
6137
Ville Syrjäläaa519f22014-06-13 13:37:55 +03006138static void vlv_dpio_cmn_power_well_enable(struct drm_i915_private *dev_priv,
6139 struct i915_power_well *power_well)
6140{
6141 WARN_ON_ONCE(power_well->data != PUNIT_POWER_WELL_DPIO_CMN_BC);
6142
6143 /*
6144 * Enable the CRI clock source so we can get at the
6145 * display and the reference clock for VGA
6146 * hotplug / manual detection.
6147 */
6148 I915_WRITE(DPLL(PIPE_B), I915_READ(DPLL(PIPE_B)) |
6149 DPLL_REFA_CLK_ENABLE_VLV | DPLL_INTEGRATED_CRI_CLK_VLV);
6150 udelay(1); /* >10ns for cmnreset, >0ns for sidereset */
6151
6152 vlv_set_power_well(dev_priv, power_well, true);
6153
6154 /*
6155 * From VLV2A0_DP_eDP_DPIO_driver_vbios_notes_10.docx -
6156 * 6. De-assert cmn_reset/side_reset. Same as VLV X0.
6157 * a. GUnit 0x2110 bit[0] set to 1 (def 0)
6158 * b. The other bits such as sfr settings / modesel may all
6159 * be set to 0.
6160 *
6161 * This should only be done on init and resume from S3 with
6162 * both PLLs disabled, or we risk losing DPIO and PLL
6163 * synchronization.
6164 */
6165 I915_WRITE(DPIO_CTL, I915_READ(DPIO_CTL) | DPIO_CMNRST);
6166}
6167
6168static void vlv_dpio_cmn_power_well_disable(struct drm_i915_private *dev_priv,
6169 struct i915_power_well *power_well)
6170{
6171 struct drm_device *dev = dev_priv->dev;
6172 enum pipe pipe;
6173
6174 WARN_ON_ONCE(power_well->data != PUNIT_POWER_WELL_DPIO_CMN_BC);
6175
6176 for_each_pipe(pipe)
6177 assert_pll_disabled(dev_priv, pipe);
6178
6179 /* Assert common reset */
6180 I915_WRITE(DPIO_CTL, I915_READ(DPIO_CTL) & ~DPIO_CMNRST);
6181
6182 vlv_set_power_well(dev_priv, power_well, false);
6183}
6184
Imre Deak25eaa002014-03-04 19:23:06 +02006185static void check_power_well_state(struct drm_i915_private *dev_priv,
6186 struct i915_power_well *power_well)
6187{
6188 bool enabled = power_well->ops->is_enabled(dev_priv, power_well);
6189
6190 if (power_well->always_on || !i915.disable_power_well) {
6191 if (!enabled)
6192 goto mismatch;
6193
6194 return;
6195 }
6196
6197 if (enabled != (power_well->count > 0))
6198 goto mismatch;
6199
6200 return;
6201
6202mismatch:
6203 WARN(1, "state mismatch for '%s' (always_on %d hw state %d use-count %d disable_power_well %d\n",
6204 power_well->name, power_well->always_on, enabled,
6205 power_well->count, i915.disable_power_well);
6206}
6207
Imre Deakda7e29b2014-02-18 00:02:02 +02006208void intel_display_power_get(struct drm_i915_private *dev_priv,
Ville Syrjälä67656252013-09-16 17:38:28 +03006209 enum intel_display_power_domain domain)
6210{
Imre Deak83c00f52013-10-25 17:36:47 +03006211 struct i915_power_domains *power_domains;
Imre Deakc1ca7272013-11-25 17:15:29 +02006212 struct i915_power_well *power_well;
6213 int i;
Ville Syrjälä67656252013-09-16 17:38:28 +03006214
Paulo Zanoni9e6ea712014-03-07 20:08:06 -03006215 intel_runtime_pm_get(dev_priv);
6216
Imre Deak83c00f52013-10-25 17:36:47 +03006217 power_domains = &dev_priv->power_domains;
6218
6219 mutex_lock(&power_domains->lock);
Imre Deak1da51582013-11-25 17:15:35 +02006220
Imre Deak25eaa002014-03-04 19:23:06 +02006221 for_each_power_well(i, power_well, BIT(domain), power_domains) {
6222 if (!power_well->count++) {
6223 DRM_DEBUG_KMS("enabling %s\n", power_well->name);
Imre Deakc6cb5822014-03-04 19:22:55 +02006224 power_well->ops->enable(dev_priv, power_well);
Imre Deakbfafe932014-06-05 20:31:47 +03006225 power_well->hw_enabled = true;
Imre Deak25eaa002014-03-04 19:23:06 +02006226 }
6227
6228 check_power_well_state(dev_priv, power_well);
6229 }
Imre Deak1da51582013-11-25 17:15:35 +02006230
Imre Deakddf9c532013-11-27 22:02:02 +02006231 power_domains->domain_use_count[domain]++;
6232
Imre Deak83c00f52013-10-25 17:36:47 +03006233 mutex_unlock(&power_domains->lock);
Ville Syrjälä67656252013-09-16 17:38:28 +03006234}
6235
Imre Deakda7e29b2014-02-18 00:02:02 +02006236void intel_display_power_put(struct drm_i915_private *dev_priv,
Ville Syrjälä67656252013-09-16 17:38:28 +03006237 enum intel_display_power_domain domain)
6238{
Imre Deak83c00f52013-10-25 17:36:47 +03006239 struct i915_power_domains *power_domains;
Imre Deakc1ca7272013-11-25 17:15:29 +02006240 struct i915_power_well *power_well;
6241 int i;
Ville Syrjälä67656252013-09-16 17:38:28 +03006242
Imre Deak83c00f52013-10-25 17:36:47 +03006243 power_domains = &dev_priv->power_domains;
6244
6245 mutex_lock(&power_domains->lock);
Imre Deak1da51582013-11-25 17:15:35 +02006246
Imre Deak1da51582013-11-25 17:15:35 +02006247 WARN_ON(!power_domains->domain_use_count[domain]);
6248 power_domains->domain_use_count[domain]--;
Imre Deakddf9c532013-11-27 22:02:02 +02006249
Imre Deak70bf4072014-03-04 19:22:51 +02006250 for_each_power_well_rev(i, power_well, BIT(domain), power_domains) {
6251 WARN_ON(!power_well->count);
6252
Imre Deak25eaa002014-03-04 19:23:06 +02006253 if (!--power_well->count && i915.disable_power_well) {
6254 DRM_DEBUG_KMS("disabling %s\n", power_well->name);
Imre Deakbfafe932014-06-05 20:31:47 +03006255 power_well->hw_enabled = false;
Imre Deakc6cb5822014-03-04 19:22:55 +02006256 power_well->ops->disable(dev_priv, power_well);
Imre Deak25eaa002014-03-04 19:23:06 +02006257 }
6258
6259 check_power_well_state(dev_priv, power_well);
Imre Deak70bf4072014-03-04 19:22:51 +02006260 }
Imre Deak1da51582013-11-25 17:15:35 +02006261
Imre Deak83c00f52013-10-25 17:36:47 +03006262 mutex_unlock(&power_domains->lock);
Paulo Zanoni9e6ea712014-03-07 20:08:06 -03006263
6264 intel_runtime_pm_put(dev_priv);
Ville Syrjälä67656252013-09-16 17:38:28 +03006265}
6266
Imre Deak83c00f52013-10-25 17:36:47 +03006267static struct i915_power_domains *hsw_pwr;
Wang Xingchaoa38911a2013-05-30 22:07:11 +08006268
6269/* Display audio driver power well request */
Takashi Iwai74b0c2d2014-06-13 15:14:34 +02006270int i915_request_power_well(void)
Wang Xingchaoa38911a2013-05-30 22:07:11 +08006271{
Imre Deakb4ed4482013-10-25 17:36:49 +03006272 struct drm_i915_private *dev_priv;
6273
Takashi Iwai74b0c2d2014-06-13 15:14:34 +02006274 if (!hsw_pwr)
6275 return -ENODEV;
Wang Xingchaoa38911a2013-05-30 22:07:11 +08006276
Imre Deakb4ed4482013-10-25 17:36:49 +03006277 dev_priv = container_of(hsw_pwr, struct drm_i915_private,
6278 power_domains);
Imre Deakda7e29b2014-02-18 00:02:02 +02006279 intel_display_power_get(dev_priv, POWER_DOMAIN_AUDIO);
Takashi Iwai74b0c2d2014-06-13 15:14:34 +02006280 return 0;
Wang Xingchaoa38911a2013-05-30 22:07:11 +08006281}
6282EXPORT_SYMBOL_GPL(i915_request_power_well);
6283
6284/* Display audio driver power well release */
Takashi Iwai74b0c2d2014-06-13 15:14:34 +02006285int i915_release_power_well(void)
Wang Xingchaoa38911a2013-05-30 22:07:11 +08006286{
Imre Deakb4ed4482013-10-25 17:36:49 +03006287 struct drm_i915_private *dev_priv;
6288
Takashi Iwai74b0c2d2014-06-13 15:14:34 +02006289 if (!hsw_pwr)
6290 return -ENODEV;
Wang Xingchaoa38911a2013-05-30 22:07:11 +08006291
Imre Deakb4ed4482013-10-25 17:36:49 +03006292 dev_priv = container_of(hsw_pwr, struct drm_i915_private,
6293 power_domains);
Imre Deakda7e29b2014-02-18 00:02:02 +02006294 intel_display_power_put(dev_priv, POWER_DOMAIN_AUDIO);
Takashi Iwai74b0c2d2014-06-13 15:14:34 +02006295 return 0;
Wang Xingchaoa38911a2013-05-30 22:07:11 +08006296}
6297EXPORT_SYMBOL_GPL(i915_release_power_well);
6298
Jani Nikulac149dcb2014-07-04 10:00:37 +08006299/*
6300 * Private interface for the audio driver to get CDCLK in kHz.
6301 *
6302 * Caller must request power well using i915_request_power_well() prior to
6303 * making the call.
6304 */
6305int i915_get_cdclk_freq(void)
6306{
6307 struct drm_i915_private *dev_priv;
6308
6309 if (!hsw_pwr)
6310 return -ENODEV;
6311
6312 dev_priv = container_of(hsw_pwr, struct drm_i915_private,
6313 power_domains);
6314
6315 return intel_ddi_get_cdclk_freq(dev_priv);
6316}
6317EXPORT_SYMBOL_GPL(i915_get_cdclk_freq);
6318
6319
Imre Deakefcad912014-03-04 19:22:53 +02006320#define POWER_DOMAIN_MASK (BIT(POWER_DOMAIN_NUM) - 1)
6321
6322#define HSW_ALWAYS_ON_POWER_DOMAINS ( \
6323 BIT(POWER_DOMAIN_PIPE_A) | \
Imre Deakf5938f32014-03-04 19:22:54 +02006324 BIT(POWER_DOMAIN_TRANSCODER_EDP) | \
Imre Deak319be8a2014-03-04 19:22:57 +02006325 BIT(POWER_DOMAIN_PORT_DDI_A_2_LANES) | \
6326 BIT(POWER_DOMAIN_PORT_DDI_A_4_LANES) | \
6327 BIT(POWER_DOMAIN_PORT_DDI_B_2_LANES) | \
6328 BIT(POWER_DOMAIN_PORT_DDI_B_4_LANES) | \
6329 BIT(POWER_DOMAIN_PORT_DDI_C_2_LANES) | \
6330 BIT(POWER_DOMAIN_PORT_DDI_C_4_LANES) | \
6331 BIT(POWER_DOMAIN_PORT_DDI_D_2_LANES) | \
6332 BIT(POWER_DOMAIN_PORT_DDI_D_4_LANES) | \
6333 BIT(POWER_DOMAIN_PORT_CRT) | \
Imre Deakf5938f32014-03-04 19:22:54 +02006334 BIT(POWER_DOMAIN_INIT))
Imre Deakefcad912014-03-04 19:22:53 +02006335#define HSW_DISPLAY_POWER_DOMAINS ( \
6336 (POWER_DOMAIN_MASK & ~HSW_ALWAYS_ON_POWER_DOMAINS) | \
6337 BIT(POWER_DOMAIN_INIT))
6338
6339#define BDW_ALWAYS_ON_POWER_DOMAINS ( \
6340 HSW_ALWAYS_ON_POWER_DOMAINS | \
6341 BIT(POWER_DOMAIN_PIPE_A_PANEL_FITTER))
6342#define BDW_DISPLAY_POWER_DOMAINS ( \
6343 (POWER_DOMAIN_MASK & ~BDW_ALWAYS_ON_POWER_DOMAINS) | \
6344 BIT(POWER_DOMAIN_INIT))
6345
Imre Deak77961eb2014-03-05 16:20:56 +02006346#define VLV_ALWAYS_ON_POWER_DOMAINS BIT(POWER_DOMAIN_INIT)
6347#define VLV_DISPLAY_POWER_DOMAINS POWER_DOMAIN_MASK
6348
6349#define VLV_DPIO_CMN_BC_POWER_DOMAINS ( \
6350 BIT(POWER_DOMAIN_PORT_DDI_B_2_LANES) | \
6351 BIT(POWER_DOMAIN_PORT_DDI_B_4_LANES) | \
6352 BIT(POWER_DOMAIN_PORT_DDI_C_2_LANES) | \
6353 BIT(POWER_DOMAIN_PORT_DDI_C_4_LANES) | \
6354 BIT(POWER_DOMAIN_PORT_CRT) | \
6355 BIT(POWER_DOMAIN_INIT))
6356
6357#define VLV_DPIO_TX_B_LANES_01_POWER_DOMAINS ( \
6358 BIT(POWER_DOMAIN_PORT_DDI_B_2_LANES) | \
6359 BIT(POWER_DOMAIN_PORT_DDI_B_4_LANES) | \
6360 BIT(POWER_DOMAIN_INIT))
6361
6362#define VLV_DPIO_TX_B_LANES_23_POWER_DOMAINS ( \
6363 BIT(POWER_DOMAIN_PORT_DDI_B_4_LANES) | \
6364 BIT(POWER_DOMAIN_INIT))
6365
6366#define VLV_DPIO_TX_C_LANES_01_POWER_DOMAINS ( \
6367 BIT(POWER_DOMAIN_PORT_DDI_C_2_LANES) | \
6368 BIT(POWER_DOMAIN_PORT_DDI_C_4_LANES) | \
6369 BIT(POWER_DOMAIN_INIT))
6370
6371#define VLV_DPIO_TX_C_LANES_23_POWER_DOMAINS ( \
6372 BIT(POWER_DOMAIN_PORT_DDI_C_4_LANES) | \
6373 BIT(POWER_DOMAIN_INIT))
6374
Imre Deaka45f44662014-03-04 19:22:56 +02006375static const struct i915_power_well_ops i9xx_always_on_power_well_ops = {
6376 .sync_hw = i9xx_always_on_power_well_noop,
6377 .enable = i9xx_always_on_power_well_noop,
6378 .disable = i9xx_always_on_power_well_noop,
6379 .is_enabled = i9xx_always_on_power_well_enabled,
6380};
Imre Deakc6cb5822014-03-04 19:22:55 +02006381
Imre Deak1c2256d2013-11-25 17:15:34 +02006382static struct i915_power_well i9xx_always_on_power_well[] = {
6383 {
6384 .name = "always-on",
6385 .always_on = 1,
6386 .domains = POWER_DOMAIN_MASK,
Imre Deakc6cb5822014-03-04 19:22:55 +02006387 .ops = &i9xx_always_on_power_well_ops,
Imre Deak1c2256d2013-11-25 17:15:34 +02006388 },
6389};
6390
Imre Deakc6cb5822014-03-04 19:22:55 +02006391static const struct i915_power_well_ops hsw_power_well_ops = {
6392 .sync_hw = hsw_power_well_sync_hw,
6393 .enable = hsw_power_well_enable,
6394 .disable = hsw_power_well_disable,
6395 .is_enabled = hsw_power_well_enabled,
6396};
6397
Imre Deakc1ca7272013-11-25 17:15:29 +02006398static struct i915_power_well hsw_power_wells[] = {
6399 {
Imre Deak6f3ef5d2013-11-25 17:15:30 +02006400 .name = "always-on",
6401 .always_on = 1,
6402 .domains = HSW_ALWAYS_ON_POWER_DOMAINS,
Imre Deakc6cb5822014-03-04 19:22:55 +02006403 .ops = &i9xx_always_on_power_well_ops,
Imre Deak6f3ef5d2013-11-25 17:15:30 +02006404 },
6405 {
Imre Deakc1ca7272013-11-25 17:15:29 +02006406 .name = "display",
Imre Deakefcad912014-03-04 19:22:53 +02006407 .domains = HSW_DISPLAY_POWER_DOMAINS,
Imre Deakc6cb5822014-03-04 19:22:55 +02006408 .ops = &hsw_power_well_ops,
Imre Deakc1ca7272013-11-25 17:15:29 +02006409 },
6410};
6411
6412static struct i915_power_well bdw_power_wells[] = {
6413 {
Imre Deak6f3ef5d2013-11-25 17:15:30 +02006414 .name = "always-on",
6415 .always_on = 1,
6416 .domains = BDW_ALWAYS_ON_POWER_DOMAINS,
Imre Deakc6cb5822014-03-04 19:22:55 +02006417 .ops = &i9xx_always_on_power_well_ops,
Imre Deak6f3ef5d2013-11-25 17:15:30 +02006418 },
6419 {
Imre Deakc1ca7272013-11-25 17:15:29 +02006420 .name = "display",
Imre Deakefcad912014-03-04 19:22:53 +02006421 .domains = BDW_DISPLAY_POWER_DOMAINS,
Imre Deakc6cb5822014-03-04 19:22:55 +02006422 .ops = &hsw_power_well_ops,
Imre Deakc1ca7272013-11-25 17:15:29 +02006423 },
6424};
6425
Imre Deak77961eb2014-03-05 16:20:56 +02006426static const struct i915_power_well_ops vlv_display_power_well_ops = {
6427 .sync_hw = vlv_power_well_sync_hw,
6428 .enable = vlv_display_power_well_enable,
6429 .disable = vlv_display_power_well_disable,
6430 .is_enabled = vlv_power_well_enabled,
6431};
6432
Ville Syrjäläaa519f22014-06-13 13:37:55 +03006433static const struct i915_power_well_ops vlv_dpio_cmn_power_well_ops = {
6434 .sync_hw = vlv_power_well_sync_hw,
6435 .enable = vlv_dpio_cmn_power_well_enable,
6436 .disable = vlv_dpio_cmn_power_well_disable,
6437 .is_enabled = vlv_power_well_enabled,
6438};
6439
Imre Deak77961eb2014-03-05 16:20:56 +02006440static const struct i915_power_well_ops vlv_dpio_power_well_ops = {
6441 .sync_hw = vlv_power_well_sync_hw,
6442 .enable = vlv_power_well_enable,
6443 .disable = vlv_power_well_disable,
6444 .is_enabled = vlv_power_well_enabled,
6445};
6446
6447static struct i915_power_well vlv_power_wells[] = {
6448 {
6449 .name = "always-on",
6450 .always_on = 1,
6451 .domains = VLV_ALWAYS_ON_POWER_DOMAINS,
6452 .ops = &i9xx_always_on_power_well_ops,
6453 },
6454 {
6455 .name = "display",
6456 .domains = VLV_DISPLAY_POWER_DOMAINS,
6457 .data = PUNIT_POWER_WELL_DISP2D,
6458 .ops = &vlv_display_power_well_ops,
6459 },
6460 {
Imre Deak77961eb2014-03-05 16:20:56 +02006461 .name = "dpio-tx-b-01",
6462 .domains = VLV_DPIO_TX_B_LANES_01_POWER_DOMAINS |
6463 VLV_DPIO_TX_B_LANES_23_POWER_DOMAINS |
6464 VLV_DPIO_TX_C_LANES_01_POWER_DOMAINS |
6465 VLV_DPIO_TX_C_LANES_23_POWER_DOMAINS,
6466 .ops = &vlv_dpio_power_well_ops,
6467 .data = PUNIT_POWER_WELL_DPIO_TX_B_LANES_01,
6468 },
6469 {
6470 .name = "dpio-tx-b-23",
6471 .domains = VLV_DPIO_TX_B_LANES_01_POWER_DOMAINS |
6472 VLV_DPIO_TX_B_LANES_23_POWER_DOMAINS |
6473 VLV_DPIO_TX_C_LANES_01_POWER_DOMAINS |
6474 VLV_DPIO_TX_C_LANES_23_POWER_DOMAINS,
6475 .ops = &vlv_dpio_power_well_ops,
6476 .data = PUNIT_POWER_WELL_DPIO_TX_B_LANES_23,
6477 },
6478 {
6479 .name = "dpio-tx-c-01",
6480 .domains = VLV_DPIO_TX_B_LANES_01_POWER_DOMAINS |
6481 VLV_DPIO_TX_B_LANES_23_POWER_DOMAINS |
6482 VLV_DPIO_TX_C_LANES_01_POWER_DOMAINS |
6483 VLV_DPIO_TX_C_LANES_23_POWER_DOMAINS,
6484 .ops = &vlv_dpio_power_well_ops,
6485 .data = PUNIT_POWER_WELL_DPIO_TX_C_LANES_01,
6486 },
6487 {
6488 .name = "dpio-tx-c-23",
6489 .domains = VLV_DPIO_TX_B_LANES_01_POWER_DOMAINS |
6490 VLV_DPIO_TX_B_LANES_23_POWER_DOMAINS |
6491 VLV_DPIO_TX_C_LANES_01_POWER_DOMAINS |
6492 VLV_DPIO_TX_C_LANES_23_POWER_DOMAINS,
6493 .ops = &vlv_dpio_power_well_ops,
6494 .data = PUNIT_POWER_WELL_DPIO_TX_C_LANES_23,
6495 },
Jesse Barnesf099a3c2014-05-23 13:16:43 -07006496 {
6497 .name = "dpio-common",
6498 .domains = VLV_DPIO_CMN_BC_POWER_DOMAINS,
6499 .data = PUNIT_POWER_WELL_DPIO_CMN_BC,
Ville Syrjäläaa519f22014-06-13 13:37:55 +03006500 .ops = &vlv_dpio_cmn_power_well_ops,
Jesse Barnesf099a3c2014-05-23 13:16:43 -07006501 },
Imre Deak77961eb2014-03-05 16:20:56 +02006502};
6503
Ville Syrjäläd2011dc2014-06-13 13:37:56 +03006504static struct i915_power_well *lookup_power_well(struct drm_i915_private *dev_priv,
6505 enum punit_power_well power_well_id)
6506{
6507 struct i915_power_domains *power_domains = &dev_priv->power_domains;
6508 struct i915_power_well *power_well;
6509 int i;
6510
6511 for_each_power_well(i, power_well, POWER_DOMAIN_MASK, power_domains) {
6512 if (power_well->data == power_well_id)
6513 return power_well;
6514 }
6515
6516 return NULL;
6517}
6518
Imre Deakc1ca7272013-11-25 17:15:29 +02006519#define set_power_wells(power_domains, __power_wells) ({ \
6520 (power_domains)->power_wells = (__power_wells); \
6521 (power_domains)->power_well_count = ARRAY_SIZE(__power_wells); \
6522})
6523
Imre Deakda7e29b2014-02-18 00:02:02 +02006524int intel_power_domains_init(struct drm_i915_private *dev_priv)
Wang Xingchaoa38911a2013-05-30 22:07:11 +08006525{
Imre Deak83c00f52013-10-25 17:36:47 +03006526 struct i915_power_domains *power_domains = &dev_priv->power_domains;
Imre Deakc1ca7272013-11-25 17:15:29 +02006527
Imre Deak83c00f52013-10-25 17:36:47 +03006528 mutex_init(&power_domains->lock);
Wang Xingchaoa38911a2013-05-30 22:07:11 +08006529
Imre Deakc1ca7272013-11-25 17:15:29 +02006530 /*
6531 * The enabling order will be from lower to higher indexed wells,
6532 * the disabling order is reversed.
6533 */
Imre Deakda7e29b2014-02-18 00:02:02 +02006534 if (IS_HASWELL(dev_priv->dev)) {
Imre Deakc1ca7272013-11-25 17:15:29 +02006535 set_power_wells(power_domains, hsw_power_wells);
6536 hsw_pwr = power_domains;
Imre Deakda7e29b2014-02-18 00:02:02 +02006537 } else if (IS_BROADWELL(dev_priv->dev)) {
Imre Deakc1ca7272013-11-25 17:15:29 +02006538 set_power_wells(power_domains, bdw_power_wells);
6539 hsw_pwr = power_domains;
Imre Deak77961eb2014-03-05 16:20:56 +02006540 } else if (IS_VALLEYVIEW(dev_priv->dev)) {
6541 set_power_wells(power_domains, vlv_power_wells);
Imre Deakc1ca7272013-11-25 17:15:29 +02006542 } else {
Imre Deak1c2256d2013-11-25 17:15:34 +02006543 set_power_wells(power_domains, i9xx_always_on_power_well);
Imre Deakc1ca7272013-11-25 17:15:29 +02006544 }
Wang Xingchaoa38911a2013-05-30 22:07:11 +08006545
6546 return 0;
6547}
6548
Imre Deakda7e29b2014-02-18 00:02:02 +02006549void intel_power_domains_remove(struct drm_i915_private *dev_priv)
Wang Xingchaoa38911a2013-05-30 22:07:11 +08006550{
6551 hsw_pwr = NULL;
6552}
6553
Imre Deakda7e29b2014-02-18 00:02:02 +02006554static void intel_power_domains_resume(struct drm_i915_private *dev_priv)
Ville Syrjälä9cdb8262013-09-16 17:38:27 +03006555{
Imre Deak83c00f52013-10-25 17:36:47 +03006556 struct i915_power_domains *power_domains = &dev_priv->power_domains;
6557 struct i915_power_well *power_well;
Imre Deakc1ca7272013-11-25 17:15:29 +02006558 int i;
Ville Syrjälä9cdb8262013-09-16 17:38:27 +03006559
Imre Deak83c00f52013-10-25 17:36:47 +03006560 mutex_lock(&power_domains->lock);
Imre Deakbfafe932014-06-05 20:31:47 +03006561 for_each_power_well(i, power_well, POWER_DOMAIN_MASK, power_domains) {
Imre Deaka45f44662014-03-04 19:22:56 +02006562 power_well->ops->sync_hw(dev_priv, power_well);
Imre Deakbfafe932014-06-05 20:31:47 +03006563 power_well->hw_enabled = power_well->ops->is_enabled(dev_priv,
6564 power_well);
6565 }
Imre Deak83c00f52013-10-25 17:36:47 +03006566 mutex_unlock(&power_domains->lock);
Wang Xingchaoa38911a2013-05-30 22:07:11 +08006567}
6568
Ville Syrjäläd2011dc2014-06-13 13:37:56 +03006569static void vlv_cmnlane_wa(struct drm_i915_private *dev_priv)
6570{
6571 struct i915_power_well *cmn =
6572 lookup_power_well(dev_priv, PUNIT_POWER_WELL_DPIO_CMN_BC);
6573 struct i915_power_well *disp2d =
6574 lookup_power_well(dev_priv, PUNIT_POWER_WELL_DISP2D);
6575
6576 /* nothing to do if common lane is already off */
6577 if (!cmn->ops->is_enabled(dev_priv, cmn))
6578 return;
6579
6580 /* If the display might be already active skip this */
6581 if (disp2d->ops->is_enabled(dev_priv, disp2d) &&
6582 I915_READ(DPIO_CTL) & DPIO_CMNRST)
6583 return;
6584
6585 DRM_DEBUG_KMS("toggling display PHY side reset\n");
6586
6587 /* cmnlane needs DPLL registers */
6588 disp2d->ops->enable(dev_priv, disp2d);
6589
6590 /*
6591 * From VLV2A0_DP_eDP_HDMI_DPIO_driver_vbios_notes_11.docx:
6592 * Need to assert and de-assert PHY SB reset by gating the
6593 * common lane power, then un-gating it.
6594 * Simply ungating isn't enough to reset the PHY enough to get
6595 * ports and lanes running.
6596 */
6597 cmn->ops->disable(dev_priv, cmn);
6598}
6599
Imre Deakda7e29b2014-02-18 00:02:02 +02006600void intel_power_domains_init_hw(struct drm_i915_private *dev_priv)
Paulo Zanonifa42e232013-01-25 16:59:11 -02006601{
Ville Syrjäläd2011dc2014-06-13 13:37:56 +03006602 struct drm_device *dev = dev_priv->dev;
Imre Deak0d116a22014-04-25 13:19:05 +03006603 struct i915_power_domains *power_domains = &dev_priv->power_domains;
6604
6605 power_domains->initializing = true;
Ville Syrjäläd2011dc2014-06-13 13:37:56 +03006606
6607 if (IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
6608 mutex_lock(&power_domains->lock);
6609 vlv_cmnlane_wa(dev_priv);
6610 mutex_unlock(&power_domains->lock);
6611 }
6612
Paulo Zanonifa42e232013-01-25 16:59:11 -02006613 /* For now, we need the power well to be always enabled. */
Imre Deakda7e29b2014-02-18 00:02:02 +02006614 intel_display_set_init_power(dev_priv, true);
6615 intel_power_domains_resume(dev_priv);
Imre Deak0d116a22014-04-25 13:19:05 +03006616 power_domains->initializing = false;
Eugeni Dodonovd0d3e512012-05-09 15:37:16 -03006617}
6618
Paulo Zanonic67a4702013-08-19 13:18:09 -03006619void intel_aux_display_runtime_get(struct drm_i915_private *dev_priv)
6620{
Paulo Zanonid361ae22014-03-07 20:08:12 -03006621 intel_runtime_pm_get(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -03006622}
6623
6624void intel_aux_display_runtime_put(struct drm_i915_private *dev_priv)
6625{
Paulo Zanonid361ae22014-03-07 20:08:12 -03006626 intel_runtime_pm_put(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -03006627}
6628
Paulo Zanoni8a187452013-12-06 20:32:13 -02006629void intel_runtime_pm_get(struct drm_i915_private *dev_priv)
6630{
6631 struct drm_device *dev = dev_priv->dev;
6632 struct device *device = &dev->pdev->dev;
6633
6634 if (!HAS_RUNTIME_PM(dev))
6635 return;
6636
6637 pm_runtime_get_sync(device);
6638 WARN(dev_priv->pm.suspended, "Device still suspended.\n");
6639}
6640
Imre Deakc6df39b2014-04-14 20:24:29 +03006641void intel_runtime_pm_get_noresume(struct drm_i915_private *dev_priv)
6642{
6643 struct drm_device *dev = dev_priv->dev;
6644 struct device *device = &dev->pdev->dev;
6645
6646 if (!HAS_RUNTIME_PM(dev))
6647 return;
6648
6649 WARN(dev_priv->pm.suspended, "Getting nosync-ref while suspended.\n");
6650 pm_runtime_get_noresume(device);
6651}
6652
Paulo Zanoni8a187452013-12-06 20:32:13 -02006653void intel_runtime_pm_put(struct drm_i915_private *dev_priv)
6654{
6655 struct drm_device *dev = dev_priv->dev;
6656 struct device *device = &dev->pdev->dev;
6657
6658 if (!HAS_RUNTIME_PM(dev))
6659 return;
6660
6661 pm_runtime_mark_last_busy(device);
6662 pm_runtime_put_autosuspend(device);
6663}
6664
6665void intel_init_runtime_pm(struct drm_i915_private *dev_priv)
6666{
6667 struct drm_device *dev = dev_priv->dev;
6668 struct device *device = &dev->pdev->dev;
6669
Paulo Zanoni8a187452013-12-06 20:32:13 -02006670 if (!HAS_RUNTIME_PM(dev))
6671 return;
6672
6673 pm_runtime_set_active(device);
6674
Imre Deakaeab0b52014-04-14 20:24:36 +03006675 /*
6676 * RPM depends on RC6 to save restore the GT HW context, so make RC6 a
6677 * requirement.
6678 */
6679 if (!intel_enable_rc6(dev)) {
6680 DRM_INFO("RC6 disabled, disabling runtime PM support\n");
6681 return;
6682 }
6683
Paulo Zanoni8a187452013-12-06 20:32:13 -02006684 pm_runtime_set_autosuspend_delay(device, 10000); /* 10s */
6685 pm_runtime_mark_last_busy(device);
6686 pm_runtime_use_autosuspend(device);
Paulo Zanoniba0239e2014-03-07 20:08:07 -03006687
6688 pm_runtime_put_autosuspend(device);
Paulo Zanoni8a187452013-12-06 20:32:13 -02006689}
6690
6691void intel_fini_runtime_pm(struct drm_i915_private *dev_priv)
6692{
6693 struct drm_device *dev = dev_priv->dev;
6694 struct device *device = &dev->pdev->dev;
6695
6696 if (!HAS_RUNTIME_PM(dev))
6697 return;
6698
Imre Deakaeab0b52014-04-14 20:24:36 +03006699 if (!intel_enable_rc6(dev))
6700 return;
6701
Paulo Zanoni8a187452013-12-06 20:32:13 -02006702 /* Make sure we're not suspended first. */
6703 pm_runtime_get_sync(device);
6704 pm_runtime_disable(device);
6705}
6706
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006707/* Set up chip specific power management-related functions */
6708void intel_init_pm(struct drm_device *dev)
6709{
6710 struct drm_i915_private *dev_priv = dev->dev_private;
6711
Daniel Vetter3a77c4c2014-01-10 08:50:12 +01006712 if (HAS_FBC(dev)) {
Ville Syrjälä40045462013-11-28 17:29:59 +02006713 if (INTEL_INFO(dev)->gen >= 7) {
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006714 dev_priv->display.fbc_enabled = ironlake_fbc_enabled;
Ville Syrjälä40045462013-11-28 17:29:59 +02006715 dev_priv->display.enable_fbc = gen7_enable_fbc;
6716 dev_priv->display.disable_fbc = ironlake_disable_fbc;
6717 } else if (INTEL_INFO(dev)->gen >= 5) {
6718 dev_priv->display.fbc_enabled = ironlake_fbc_enabled;
6719 dev_priv->display.enable_fbc = ironlake_enable_fbc;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006720 dev_priv->display.disable_fbc = ironlake_disable_fbc;
6721 } else if (IS_GM45(dev)) {
6722 dev_priv->display.fbc_enabled = g4x_fbc_enabled;
6723 dev_priv->display.enable_fbc = g4x_enable_fbc;
6724 dev_priv->display.disable_fbc = g4x_disable_fbc;
Ville Syrjälä40045462013-11-28 17:29:59 +02006725 } else {
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006726 dev_priv->display.fbc_enabled = i8xx_fbc_enabled;
6727 dev_priv->display.enable_fbc = i8xx_enable_fbc;
6728 dev_priv->display.disable_fbc = i8xx_disable_fbc;
Ville Syrjälä993495a2013-12-12 17:27:40 +02006729
6730 /* This value was pulled out of someone's hat */
6731 I915_WRITE(FBC_CONTROL, 500 << FBC_CTL_INTERVAL_SHIFT);
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006732 }
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006733 }
6734
Daniel Vetterc921aba2012-04-26 23:28:17 +02006735 /* For cxsr */
6736 if (IS_PINEVIEW(dev))
6737 i915_pineview_get_mem_freq(dev);
6738 else if (IS_GEN5(dev))
6739 i915_ironlake_get_mem_freq(dev);
6740
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006741 /* For FIFO watermark updates */
6742 if (HAS_PCH_SPLIT(dev)) {
Damien Lespiaufa50ad62014-03-17 18:01:16 +00006743 ilk_setup_wm_latency(dev);
Ville Syrjälä53615a52013-08-01 16:18:50 +03006744
Ville Syrjäläbd602542014-01-07 16:14:10 +02006745 if ((IS_GEN5(dev) && dev_priv->wm.pri_latency[1] &&
6746 dev_priv->wm.spr_latency[1] && dev_priv->wm.cur_latency[1]) ||
6747 (!IS_GEN5(dev) && dev_priv->wm.pri_latency[0] &&
6748 dev_priv->wm.spr_latency[0] && dev_priv->wm.cur_latency[0])) {
6749 dev_priv->display.update_wm = ilk_update_wm;
6750 dev_priv->display.update_sprite_wm = ilk_update_sprite_wm;
6751 } else {
6752 DRM_DEBUG_KMS("Failed to read display plane latency. "
6753 "Disable CxSR\n");
6754 }
6755
6756 if (IS_GEN5(dev))
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006757 dev_priv->display.init_clock_gating = ironlake_init_clock_gating;
Ville Syrjäläbd602542014-01-07 16:14:10 +02006758 else if (IS_GEN6(dev))
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006759 dev_priv->display.init_clock_gating = gen6_init_clock_gating;
Ville Syrjäläbd602542014-01-07 16:14:10 +02006760 else if (IS_IVYBRIDGE(dev))
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006761 dev_priv->display.init_clock_gating = ivybridge_init_clock_gating;
Ville Syrjäläbd602542014-01-07 16:14:10 +02006762 else if (IS_HASWELL(dev))
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03006763 dev_priv->display.init_clock_gating = haswell_init_clock_gating;
Ville Syrjäläbd602542014-01-07 16:14:10 +02006764 else if (INTEL_INFO(dev)->gen == 8)
Ben Widawsky1020a5c2013-11-02 21:07:06 -07006765 dev_priv->display.init_clock_gating = gen8_init_clock_gating;
Ville Syrjäläa4565da2014-04-09 13:28:10 +03006766 } else if (IS_CHERRYVIEW(dev)) {
6767 dev_priv->display.update_wm = valleyview_update_wm;
6768 dev_priv->display.init_clock_gating =
6769 cherryview_init_clock_gating;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006770 } else if (IS_VALLEYVIEW(dev)) {
6771 dev_priv->display.update_wm = valleyview_update_wm;
6772 dev_priv->display.init_clock_gating =
6773 valleyview_init_clock_gating;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006774 } else if (IS_PINEVIEW(dev)) {
6775 if (!intel_get_cxsr_latency(IS_PINEVIEW_G(dev),
6776 dev_priv->is_ddr3,
6777 dev_priv->fsb_freq,
6778 dev_priv->mem_freq)) {
6779 DRM_INFO("failed to find known CxSR latency "
6780 "(found ddr%s fsb freq %d, mem freq %d), "
6781 "disabling CxSR\n",
6782 (dev_priv->is_ddr3 == 1) ? "3" : "2",
6783 dev_priv->fsb_freq, dev_priv->mem_freq);
6784 /* Disable CxSR and never update its watermark again */
Imre Deak5209b1f2014-07-01 12:36:17 +03006785 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006786 dev_priv->display.update_wm = NULL;
6787 } else
6788 dev_priv->display.update_wm = pineview_update_wm;
6789 dev_priv->display.init_clock_gating = gen3_init_clock_gating;
6790 } else if (IS_G4X(dev)) {
6791 dev_priv->display.update_wm = g4x_update_wm;
6792 dev_priv->display.init_clock_gating = g4x_init_clock_gating;
6793 } else if (IS_GEN4(dev)) {
6794 dev_priv->display.update_wm = i965_update_wm;
6795 if (IS_CRESTLINE(dev))
6796 dev_priv->display.init_clock_gating = crestline_init_clock_gating;
6797 else if (IS_BROADWATER(dev))
6798 dev_priv->display.init_clock_gating = broadwater_init_clock_gating;
6799 } else if (IS_GEN3(dev)) {
6800 dev_priv->display.update_wm = i9xx_update_wm;
6801 dev_priv->display.get_fifo_size = i9xx_get_fifo_size;
6802 dev_priv->display.init_clock_gating = gen3_init_clock_gating;
Daniel Vetterfeb56b92013-12-14 20:38:30 -02006803 } else if (IS_GEN2(dev)) {
6804 if (INTEL_INFO(dev)->num_pipes == 1) {
6805 dev_priv->display.update_wm = i845_update_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006806 dev_priv->display.get_fifo_size = i845_get_fifo_size;
Daniel Vetterfeb56b92013-12-14 20:38:30 -02006807 } else {
6808 dev_priv->display.update_wm = i9xx_update_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006809 dev_priv->display.get_fifo_size = i830_get_fifo_size;
Daniel Vetterfeb56b92013-12-14 20:38:30 -02006810 }
6811
6812 if (IS_I85X(dev) || IS_I865G(dev))
6813 dev_priv->display.init_clock_gating = i85x_init_clock_gating;
6814 else
6815 dev_priv->display.init_clock_gating = i830_init_clock_gating;
6816 } else {
6817 DRM_ERROR("unexpected fall-through in intel_init_pm\n");
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006818 }
6819}
6820
Ben Widawsky42c05262012-09-26 10:34:00 -07006821int sandybridge_pcode_read(struct drm_i915_private *dev_priv, u8 mbox, u32 *val)
6822{
Jesse Barnes4fc688c2012-11-02 11:14:01 -07006823 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Ben Widawsky42c05262012-09-26 10:34:00 -07006824
6825 if (I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) {
6826 DRM_DEBUG_DRIVER("warning: pcode (read) mailbox access failed\n");
6827 return -EAGAIN;
6828 }
6829
6830 I915_WRITE(GEN6_PCODE_DATA, *val);
6831 I915_WRITE(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY | mbox);
6832
6833 if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
6834 500)) {
6835 DRM_ERROR("timeout waiting for pcode read (%d) to finish\n", mbox);
6836 return -ETIMEDOUT;
6837 }
6838
6839 *val = I915_READ(GEN6_PCODE_DATA);
6840 I915_WRITE(GEN6_PCODE_DATA, 0);
6841
6842 return 0;
6843}
6844
6845int sandybridge_pcode_write(struct drm_i915_private *dev_priv, u8 mbox, u32 val)
6846{
Jesse Barnes4fc688c2012-11-02 11:14:01 -07006847 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Ben Widawsky42c05262012-09-26 10:34:00 -07006848
6849 if (I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) {
6850 DRM_DEBUG_DRIVER("warning: pcode (write) mailbox access failed\n");
6851 return -EAGAIN;
6852 }
6853
6854 I915_WRITE(GEN6_PCODE_DATA, val);
6855 I915_WRITE(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY | mbox);
6856
6857 if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
6858 500)) {
6859 DRM_ERROR("timeout waiting for pcode write (%d) to finish\n", mbox);
6860 return -ETIMEDOUT;
6861 }
6862
6863 I915_WRITE(GEN6_PCODE_DATA, 0);
6864
6865 return 0;
6866}
Jesse Barnesa0e4e192013-04-02 11:23:05 -07006867
Ville Syrjälä2ec38152013-11-05 22:42:29 +02006868int vlv_gpu_freq(struct drm_i915_private *dev_priv, int val)
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006869{
Ville Syrjälä07ab1182013-11-05 22:42:28 +02006870 int div;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006871
Ville Syrjälä07ab1182013-11-05 22:42:28 +02006872 /* 4 x czclk */
Ville Syrjälä2ec38152013-11-05 22:42:29 +02006873 switch (dev_priv->mem_freq) {
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006874 case 800:
Ville Syrjälä07ab1182013-11-05 22:42:28 +02006875 div = 10;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006876 break;
6877 case 1066:
Ville Syrjälä07ab1182013-11-05 22:42:28 +02006878 div = 12;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006879 break;
6880 case 1333:
Ville Syrjälä07ab1182013-11-05 22:42:28 +02006881 div = 16;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006882 break;
6883 default:
6884 return -1;
6885 }
6886
Ville Syrjälä2ec38152013-11-05 22:42:29 +02006887 return DIV_ROUND_CLOSEST(dev_priv->mem_freq * (val + 6 - 0xbd), 4 * div);
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006888}
6889
Ville Syrjälä2ec38152013-11-05 22:42:29 +02006890int vlv_freq_opcode(struct drm_i915_private *dev_priv, int val)
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006891{
Ville Syrjälä07ab1182013-11-05 22:42:28 +02006892 int mul;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006893
Ville Syrjälä07ab1182013-11-05 22:42:28 +02006894 /* 4 x czclk */
Ville Syrjälä2ec38152013-11-05 22:42:29 +02006895 switch (dev_priv->mem_freq) {
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006896 case 800:
Ville Syrjälä07ab1182013-11-05 22:42:28 +02006897 mul = 10;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006898 break;
6899 case 1066:
Ville Syrjälä07ab1182013-11-05 22:42:28 +02006900 mul = 12;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006901 break;
6902 case 1333:
Ville Syrjälä07ab1182013-11-05 22:42:28 +02006903 mul = 16;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006904 break;
6905 default:
6906 return -1;
6907 }
6908
Ville Syrjälä2ec38152013-11-05 22:42:29 +02006909 return DIV_ROUND_CLOSEST(4 * mul * val, dev_priv->mem_freq) + 0xbd - 6;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006910}
6911
Daniel Vetterf742a552013-12-06 10:17:53 +01006912void intel_pm_setup(struct drm_device *dev)
Chris Wilson907b28c2013-07-19 20:36:52 +01006913{
6914 struct drm_i915_private *dev_priv = dev->dev_private;
6915
Daniel Vetterf742a552013-12-06 10:17:53 +01006916 mutex_init(&dev_priv->rps.hw_lock);
6917
Chris Wilson907b28c2013-07-19 20:36:52 +01006918 INIT_DELAYED_WORK(&dev_priv->rps.delayed_resume_work,
6919 intel_gen6_powersave_work);
Paulo Zanoni5d584b22014-03-07 20:08:15 -03006920
Paulo Zanoni33688d92014-03-07 20:08:19 -03006921 dev_priv->pm.suspended = false;
Paulo Zanoni5d584b22014-03-07 20:08:15 -03006922 dev_priv->pm.irqs_disabled = false;
Chris Wilson907b28c2013-07-19 20:36:52 +01006923}