blob: 7e028341b2f518d09bd0782f1c072784abb77b97 [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;
95 struct drm_framebuffer *fb = crtc->fb;
96 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;
100 int plane, 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 plane = intel_crtc->plane == 0 ? FBC_CTL_PLANEA : FBC_CTL_PLANEB;
113
114 /* Clear old tags */
115 for (i = 0; i < (FBC_LL_SIZE / 32) + 1; i++)
116 I915_WRITE(FBC_TAG + (i * 4), 0);
117
Ville Syrjälä159f9872013-11-28 17:29:57 +0200118 if (IS_GEN4(dev)) {
119 u32 fbc_ctl2;
120
121 /* Set it up... */
122 fbc_ctl2 = FBC_CTL_FENCE_DBL | FBC_CTL_IDLE_IMM | FBC_CTL_CPU_FENCE;
123 fbc_ctl2 |= plane;
124 I915_WRITE(FBC_CONTROL2, fbc_ctl2);
125 I915_WRITE(FBC_FENCE_OFF, crtc->y);
126 }
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300127
128 /* enable it... */
Ville Syrjälä993495a2013-12-12 17:27:40 +0200129 fbc_ctl = I915_READ(FBC_CONTROL);
130 fbc_ctl &= 0x3fff << FBC_CTL_INTERVAL_SHIFT;
131 fbc_ctl |= FBC_CTL_EN | FBC_CTL_PERIODIC;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300132 if (IS_I945GM(dev))
133 fbc_ctl |= FBC_CTL_C3_IDLE; /* 945 needs special SR handling */
134 fbc_ctl |= (cfb_pitch & 0xff) << FBC_CTL_STRIDE_SHIFT;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300135 fbc_ctl |= obj->fence_reg;
136 I915_WRITE(FBC_CONTROL, fbc_ctl);
137
Ville Syrjälä84f44ce2013-04-17 17:48:49 +0300138 DRM_DEBUG_KMS("enabled FBC, pitch %d, yoff %d, plane %c, ",
139 cfb_pitch, crtc->y, plane_name(intel_crtc->plane));
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300140}
141
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300142static bool i8xx_fbc_enabled(struct drm_device *dev)
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300143{
144 struct drm_i915_private *dev_priv = dev->dev_private;
145
146 return I915_READ(FBC_CONTROL) & FBC_CTL_EN;
147}
148
Ville Syrjälä993495a2013-12-12 17:27:40 +0200149static void g4x_enable_fbc(struct drm_crtc *crtc)
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300150{
151 struct drm_device *dev = crtc->dev;
152 struct drm_i915_private *dev_priv = dev->dev_private;
153 struct drm_framebuffer *fb = crtc->fb;
154 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
155 struct drm_i915_gem_object *obj = intel_fb->obj;
156 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
157 int plane = intel_crtc->plane == 0 ? DPFC_CTL_PLANEA : DPFC_CTL_PLANEB;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300158 u32 dpfc_ctl;
159
160 dpfc_ctl = plane | DPFC_SR_EN | DPFC_CTL_LIMIT_1X;
161 dpfc_ctl |= DPFC_CTL_FENCE_EN | obj->fence_reg;
162 I915_WRITE(DPFC_CHICKEN, DPFC_HT_MODIFY);
163
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300164 I915_WRITE(DPFC_FENCE_YOFF, crtc->y);
165
166 /* enable it... */
167 I915_WRITE(DPFC_CONTROL, I915_READ(DPFC_CONTROL) | DPFC_CTL_EN);
168
Ville Syrjälä84f44ce2013-04-17 17:48:49 +0300169 DRM_DEBUG_KMS("enabled fbc on plane %c\n", plane_name(intel_crtc->plane));
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300170}
171
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300172static void g4x_disable_fbc(struct drm_device *dev)
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300173{
174 struct drm_i915_private *dev_priv = dev->dev_private;
175 u32 dpfc_ctl;
176
177 /* Disable compression */
178 dpfc_ctl = I915_READ(DPFC_CONTROL);
179 if (dpfc_ctl & DPFC_CTL_EN) {
180 dpfc_ctl &= ~DPFC_CTL_EN;
181 I915_WRITE(DPFC_CONTROL, dpfc_ctl);
182
183 DRM_DEBUG_KMS("disabled FBC\n");
184 }
185}
186
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300187static bool g4x_fbc_enabled(struct drm_device *dev)
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300188{
189 struct drm_i915_private *dev_priv = dev->dev_private;
190
191 return I915_READ(DPFC_CONTROL) & DPFC_CTL_EN;
192}
193
194static void sandybridge_blit_fbc_update(struct drm_device *dev)
195{
196 struct drm_i915_private *dev_priv = dev->dev_private;
197 u32 blt_ecoskpd;
198
199 /* Make sure blitter notifies FBC of writes */
Deepak S940aece2013-11-23 14:55:43 +0530200
201 /* Blitter is part of Media powerwell on VLV. No impact of
202 * his param in other platforms for now */
203 gen6_gt_force_wake_get(dev_priv, FORCEWAKE_MEDIA);
Deepak Sc8d9a592013-11-23 14:55:42 +0530204
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300205 blt_ecoskpd = I915_READ(GEN6_BLITTER_ECOSKPD);
206 blt_ecoskpd |= GEN6_BLITTER_FBC_NOTIFY <<
207 GEN6_BLITTER_LOCK_SHIFT;
208 I915_WRITE(GEN6_BLITTER_ECOSKPD, blt_ecoskpd);
209 blt_ecoskpd |= GEN6_BLITTER_FBC_NOTIFY;
210 I915_WRITE(GEN6_BLITTER_ECOSKPD, blt_ecoskpd);
211 blt_ecoskpd &= ~(GEN6_BLITTER_FBC_NOTIFY <<
212 GEN6_BLITTER_LOCK_SHIFT);
213 I915_WRITE(GEN6_BLITTER_ECOSKPD, blt_ecoskpd);
214 POSTING_READ(GEN6_BLITTER_ECOSKPD);
Deepak Sc8d9a592013-11-23 14:55:42 +0530215
Deepak S940aece2013-11-23 14:55:43 +0530216 gen6_gt_force_wake_put(dev_priv, FORCEWAKE_MEDIA);
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300217}
218
Ville Syrjälä993495a2013-12-12 17:27:40 +0200219static void ironlake_enable_fbc(struct drm_crtc *crtc)
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300220{
221 struct drm_device *dev = crtc->dev;
222 struct drm_i915_private *dev_priv = dev->dev_private;
223 struct drm_framebuffer *fb = crtc->fb;
224 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
225 struct drm_i915_gem_object *obj = intel_fb->obj;
226 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
227 int plane = intel_crtc->plane == 0 ? DPFC_CTL_PLANEA : DPFC_CTL_PLANEB;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300228 u32 dpfc_ctl;
229
230 dpfc_ctl = I915_READ(ILK_DPFC_CONTROL);
231 dpfc_ctl &= DPFC_RESERVED;
232 dpfc_ctl |= (plane | DPFC_CTL_LIMIT_1X);
233 /* Set persistent mode for front-buffer rendering, ala X. */
234 dpfc_ctl |= DPFC_CTL_PERSISTENT_MODE;
Ville Syrjäläd6293362013-11-21 21:29:45 +0200235 dpfc_ctl |= DPFC_CTL_FENCE_EN;
236 if (IS_GEN5(dev))
237 dpfc_ctl |= obj->fence_reg;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300238 I915_WRITE(ILK_DPFC_CHICKEN, DPFC_HT_MODIFY);
239
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300240 I915_WRITE(ILK_DPFC_FENCE_YOFF, crtc->y);
Ben Widawskyf343c5f2013-07-05 14:41:04 -0700241 I915_WRITE(ILK_FBC_RT_BASE, i915_gem_obj_ggtt_offset(obj) | ILK_FBC_RT_VALID);
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300242 /* enable it... */
243 I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl | DPFC_CTL_EN);
244
245 if (IS_GEN6(dev)) {
246 I915_WRITE(SNB_DPFC_CTL_SA,
247 SNB_CPU_FENCE_ENABLE | obj->fence_reg);
248 I915_WRITE(DPFC_CPU_FENCE_OFFSET, crtc->y);
249 sandybridge_blit_fbc_update(dev);
250 }
251
Ville Syrjälä84f44ce2013-04-17 17:48:49 +0300252 DRM_DEBUG_KMS("enabled fbc on plane %c\n", plane_name(intel_crtc->plane));
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300253}
254
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300255static void ironlake_disable_fbc(struct drm_device *dev)
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300256{
257 struct drm_i915_private *dev_priv = dev->dev_private;
258 u32 dpfc_ctl;
259
260 /* Disable compression */
261 dpfc_ctl = I915_READ(ILK_DPFC_CONTROL);
262 if (dpfc_ctl & DPFC_CTL_EN) {
263 dpfc_ctl &= ~DPFC_CTL_EN;
264 I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl);
265
266 DRM_DEBUG_KMS("disabled FBC\n");
267 }
268}
269
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300270static bool ironlake_fbc_enabled(struct drm_device *dev)
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300271{
272 struct drm_i915_private *dev_priv = dev->dev_private;
273
274 return I915_READ(ILK_DPFC_CONTROL) & DPFC_CTL_EN;
275}
276
Ville Syrjälä993495a2013-12-12 17:27:40 +0200277static void gen7_enable_fbc(struct drm_crtc *crtc)
Rodrigo Viviabe959c2013-05-06 19:37:33 -0300278{
279 struct drm_device *dev = crtc->dev;
280 struct drm_i915_private *dev_priv = dev->dev_private;
281 struct drm_framebuffer *fb = crtc->fb;
282 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
283 struct drm_i915_gem_object *obj = intel_fb->obj;
284 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
285
Ben Widawskyf343c5f2013-07-05 14:41:04 -0700286 I915_WRITE(IVB_FBC_RT_BASE, i915_gem_obj_ggtt_offset(obj));
Rodrigo Viviabe959c2013-05-06 19:37:33 -0300287
288 I915_WRITE(ILK_DPFC_CONTROL, DPFC_CTL_EN | DPFC_CTL_LIMIT_1X |
289 IVB_DPFC_CTL_FENCE_EN |
290 intel_crtc->plane << IVB_DPFC_CTL_PLANE_SHIFT);
291
Rodrigo Vivi891348b2013-05-06 19:37:36 -0300292 if (IS_IVYBRIDGE(dev)) {
Damien Lespiau7dd23ba2013-05-10 14:33:17 +0100293 /* WaFbcAsynchFlipDisableFbcQueue:ivb */
Rodrigo Vivi891348b2013-05-06 19:37:36 -0300294 I915_WRITE(ILK_DISPLAY_CHICKEN1, ILK_FBCQ_DIS);
Rodrigo Vivi28554162013-05-06 19:37:37 -0300295 } else {
Damien Lespiau7dd23ba2013-05-10 14:33:17 +0100296 /* WaFbcAsynchFlipDisableFbcQueue:hsw */
Rodrigo Vivi28554162013-05-06 19:37:37 -0300297 I915_WRITE(HSW_PIPE_SLICE_CHICKEN_1(intel_crtc->pipe),
298 HSW_BYPASS_FBC_QUEUE);
Rodrigo Vivi891348b2013-05-06 19:37:36 -0300299 }
Rodrigo Vivib74ea102013-05-09 14:08:38 -0300300
Rodrigo Viviabe959c2013-05-06 19:37:33 -0300301 I915_WRITE(SNB_DPFC_CTL_SA,
302 SNB_CPU_FENCE_ENABLE | obj->fence_reg);
303 I915_WRITE(DPFC_CPU_FENCE_OFFSET, crtc->y);
304
305 sandybridge_blit_fbc_update(dev);
306
Ville Syrjäläb19870e2013-11-06 23:02:25 +0200307 DRM_DEBUG_KMS("enabled fbc on plane %c\n", plane_name(intel_crtc->plane));
Rodrigo Viviabe959c2013-05-06 19:37:33 -0300308}
309
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300310bool intel_fbc_enabled(struct drm_device *dev)
311{
312 struct drm_i915_private *dev_priv = dev->dev_private;
313
314 if (!dev_priv->display.fbc_enabled)
315 return false;
316
317 return dev_priv->display.fbc_enabled(dev);
318}
319
320static void intel_fbc_work_fn(struct work_struct *__work)
321{
322 struct intel_fbc_work *work =
323 container_of(to_delayed_work(__work),
324 struct intel_fbc_work, work);
325 struct drm_device *dev = work->crtc->dev;
326 struct drm_i915_private *dev_priv = dev->dev_private;
327
328 mutex_lock(&dev->struct_mutex);
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -0700329 if (work == dev_priv->fbc.fbc_work) {
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300330 /* Double check that we haven't switched fb without cancelling
331 * the prior work.
332 */
333 if (work->crtc->fb == work->fb) {
Ville Syrjälä993495a2013-12-12 17:27:40 +0200334 dev_priv->display.enable_fbc(work->crtc);
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300335
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -0700336 dev_priv->fbc.plane = to_intel_crtc(work->crtc)->plane;
337 dev_priv->fbc.fb_id = work->crtc->fb->base.id;
338 dev_priv->fbc.y = work->crtc->y;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300339 }
340
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -0700341 dev_priv->fbc.fbc_work = NULL;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300342 }
343 mutex_unlock(&dev->struct_mutex);
344
345 kfree(work);
346}
347
348static void intel_cancel_fbc_work(struct drm_i915_private *dev_priv)
349{
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -0700350 if (dev_priv->fbc.fbc_work == NULL)
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300351 return;
352
353 DRM_DEBUG_KMS("cancelling pending FBC enable\n");
354
355 /* Synchronisation is provided by struct_mutex and checking of
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -0700356 * dev_priv->fbc.fbc_work, so we can perform the cancellation
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300357 * entirely asynchronously.
358 */
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -0700359 if (cancel_delayed_work(&dev_priv->fbc.fbc_work->work))
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300360 /* tasklet was killed before being run, clean up */
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -0700361 kfree(dev_priv->fbc.fbc_work);
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300362
363 /* Mark the work as no longer wanted so that if it does
364 * wake-up (because the work was already running and waiting
365 * for our mutex), it will discover that is no longer
366 * necessary to run.
367 */
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -0700368 dev_priv->fbc.fbc_work = NULL;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300369}
370
Ville Syrjälä993495a2013-12-12 17:27:40 +0200371static void intel_enable_fbc(struct drm_crtc *crtc)
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300372{
373 struct intel_fbc_work *work;
374 struct drm_device *dev = crtc->dev;
375 struct drm_i915_private *dev_priv = dev->dev_private;
376
377 if (!dev_priv->display.enable_fbc)
378 return;
379
380 intel_cancel_fbc_work(dev_priv);
381
Daniel Vetterb14c5672013-09-19 12:18:32 +0200382 work = kzalloc(sizeof(*work), GFP_KERNEL);
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300383 if (work == NULL) {
Paulo Zanoni6cdcb5e2013-06-12 17:27:29 -0300384 DRM_ERROR("Failed to allocate FBC work structure\n");
Ville Syrjälä993495a2013-12-12 17:27:40 +0200385 dev_priv->display.enable_fbc(crtc);
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300386 return;
387 }
388
389 work->crtc = crtc;
390 work->fb = crtc->fb;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300391 INIT_DELAYED_WORK(&work->work, intel_fbc_work_fn);
392
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -0700393 dev_priv->fbc.fbc_work = work;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300394
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300395 /* Delay the actual enabling to let pageflipping cease and the
396 * display to settle before starting the compression. Note that
397 * this delay also serves a second purpose: it allows for a
398 * vblank to pass after disabling the FBC before we attempt
399 * to modify the control registers.
400 *
401 * A more complicated solution would involve tracking vblanks
402 * following the termination of the page-flipping sequence
403 * and indeed performing the enable as a co-routine and not
404 * waiting synchronously upon the vblank.
Damien Lespiau7457d612013-06-07 17:41:07 +0100405 *
406 * WaFbcWaitForVBlankBeforeEnable:ilk,snb
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300407 */
408 schedule_delayed_work(&work->work, msecs_to_jiffies(50));
409}
410
411void intel_disable_fbc(struct drm_device *dev)
412{
413 struct drm_i915_private *dev_priv = dev->dev_private;
414
415 intel_cancel_fbc_work(dev_priv);
416
417 if (!dev_priv->display.disable_fbc)
418 return;
419
420 dev_priv->display.disable_fbc(dev);
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -0700421 dev_priv->fbc.plane = -1;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300422}
423
Chris Wilson29ebf902013-07-27 17:23:55 +0100424static bool set_no_fbc_reason(struct drm_i915_private *dev_priv,
425 enum no_fbc_reason reason)
426{
427 if (dev_priv->fbc.no_fbc_reason == reason)
428 return false;
429
430 dev_priv->fbc.no_fbc_reason = reason;
431 return true;
432}
433
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300434/**
435 * intel_update_fbc - enable/disable FBC as needed
436 * @dev: the drm_device
437 *
438 * Set up the framebuffer compression hardware at mode set time. We
439 * enable it if possible:
440 * - plane A only (on pre-965)
441 * - no pixel mulitply/line duplication
442 * - no alpha buffer discard
443 * - no dual wide
Paulo Zanonif85da862013-06-04 16:53:39 -0300444 * - framebuffer <= max_hdisplay in width, max_vdisplay in height
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300445 *
446 * We can't assume that any compression will take place (worst case),
447 * so the compressed buffer has to be the same size as the uncompressed
448 * one. It also must reside (along with the line length buffer) in
449 * stolen memory.
450 *
451 * We need to enable/disable FBC on a global basis.
452 */
453void intel_update_fbc(struct drm_device *dev)
454{
455 struct drm_i915_private *dev_priv = dev->dev_private;
456 struct drm_crtc *crtc = NULL, *tmp_crtc;
457 struct intel_crtc *intel_crtc;
458 struct drm_framebuffer *fb;
459 struct intel_framebuffer *intel_fb;
460 struct drm_i915_gem_object *obj;
Ville Syrjäläef644fd2013-09-04 18:25:21 +0300461 const struct drm_display_mode *adjusted_mode;
Ville Syrjälä37327ab2013-09-04 18:25:28 +0300462 unsigned int max_width, max_height;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300463
Chris Wilson29ebf902013-07-27 17:23:55 +0100464 if (!I915_HAS_FBC(dev)) {
465 set_no_fbc_reason(dev_priv, FBC_UNSUPPORTED);
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300466 return;
Chris Wilson29ebf902013-07-27 17:23:55 +0100467 }
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300468
Chris Wilson29ebf902013-07-27 17:23:55 +0100469 if (!i915_powersave) {
470 if (set_no_fbc_reason(dev_priv, FBC_MODULE_PARAM))
471 DRM_DEBUG_KMS("fbc disabled per module param\n");
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300472 return;
Chris Wilson29ebf902013-07-27 17:23:55 +0100473 }
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300474
475 /*
476 * If FBC is already on, we just have to verify that we can
477 * keep it that way...
478 * Need to disable if:
479 * - more than one pipe is active
480 * - changing FBC params (stride, fence, mode)
481 * - new fb is too large to fit in compressed buffer
482 * - going to an unsupported config (interlace, pixel multiply, etc.)
483 */
484 list_for_each_entry(tmp_crtc, &dev->mode_config.crtc_list, head) {
Chris Wilson3490ea52013-01-07 10:11:40 +0000485 if (intel_crtc_active(tmp_crtc) &&
Ville Syrjälä4c445e02013-10-09 17:24:58 +0300486 to_intel_crtc(tmp_crtc)->primary_enabled) {
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300487 if (crtc) {
Chris Wilson29ebf902013-07-27 17:23:55 +0100488 if (set_no_fbc_reason(dev_priv, FBC_MULTIPLE_PIPES))
489 DRM_DEBUG_KMS("more than one pipe active, disabling compression\n");
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300490 goto out_disable;
491 }
492 crtc = tmp_crtc;
493 }
494 }
495
496 if (!crtc || crtc->fb == NULL) {
Chris Wilson29ebf902013-07-27 17:23:55 +0100497 if (set_no_fbc_reason(dev_priv, FBC_NO_OUTPUT))
498 DRM_DEBUG_KMS("no output, disabling\n");
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300499 goto out_disable;
500 }
501
502 intel_crtc = to_intel_crtc(crtc);
503 fb = crtc->fb;
504 intel_fb = to_intel_framebuffer(fb);
505 obj = intel_fb->obj;
Ville Syrjäläef644fd2013-09-04 18:25:21 +0300506 adjusted_mode = &intel_crtc->config.adjusted_mode;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300507
Damien Lespiau8a5729a2013-06-24 16:22:02 +0100508 if (i915_enable_fbc < 0 &&
509 INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev)) {
Chris Wilson29ebf902013-07-27 17:23:55 +0100510 if (set_no_fbc_reason(dev_priv, FBC_CHIP_DEFAULT))
511 DRM_DEBUG_KMS("disabled per chip default\n");
Damien Lespiau8a5729a2013-06-24 16:22:02 +0100512 goto out_disable;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300513 }
Damien Lespiau8a5729a2013-06-24 16:22:02 +0100514 if (!i915_enable_fbc) {
Chris Wilson29ebf902013-07-27 17:23:55 +0100515 if (set_no_fbc_reason(dev_priv, FBC_MODULE_PARAM))
516 DRM_DEBUG_KMS("fbc disabled per module param\n");
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300517 goto out_disable;
518 }
Ville Syrjäläef644fd2013-09-04 18:25:21 +0300519 if ((adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) ||
520 (adjusted_mode->flags & DRM_MODE_FLAG_DBLSCAN)) {
Chris Wilson29ebf902013-07-27 17:23:55 +0100521 if (set_no_fbc_reason(dev_priv, FBC_UNSUPPORTED_MODE))
522 DRM_DEBUG_KMS("mode incompatible with compression, "
523 "disabling\n");
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300524 goto out_disable;
525 }
Paulo Zanonif85da862013-06-04 16:53:39 -0300526
527 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
Ville Syrjälä37327ab2013-09-04 18:25:28 +0300528 max_width = 4096;
529 max_height = 2048;
Paulo Zanonif85da862013-06-04 16:53:39 -0300530 } else {
Ville Syrjälä37327ab2013-09-04 18:25:28 +0300531 max_width = 2048;
532 max_height = 1536;
Paulo Zanonif85da862013-06-04 16:53:39 -0300533 }
Ville Syrjälä37327ab2013-09-04 18:25:28 +0300534 if (intel_crtc->config.pipe_src_w > max_width ||
535 intel_crtc->config.pipe_src_h > max_height) {
Chris Wilson29ebf902013-07-27 17:23:55 +0100536 if (set_no_fbc_reason(dev_priv, FBC_MODE_TOO_LARGE))
537 DRM_DEBUG_KMS("mode too large for compression, disabling\n");
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300538 goto out_disable;
539 }
Ville Syrjäläc5a44aa2013-11-28 17:29:58 +0200540 if ((INTEL_INFO(dev)->gen < 4 || IS_HASWELL(dev)) &&
541 intel_crtc->plane != PLANE_A) {
Chris Wilson29ebf902013-07-27 17:23:55 +0100542 if (set_no_fbc_reason(dev_priv, FBC_BAD_PLANE))
Ville Syrjäläc5a44aa2013-11-28 17:29:58 +0200543 DRM_DEBUG_KMS("plane not A, disabling compression\n");
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300544 goto out_disable;
545 }
546
547 /* The use of a CPU fence is mandatory in order to detect writes
548 * by the CPU to the scanout and trigger updates to the FBC.
549 */
550 if (obj->tiling_mode != I915_TILING_X ||
551 obj->fence_reg == I915_FENCE_REG_NONE) {
Chris Wilson29ebf902013-07-27 17:23:55 +0100552 if (set_no_fbc_reason(dev_priv, FBC_NOT_TILED))
553 DRM_DEBUG_KMS("framebuffer not tiled or fenced, disabling compression\n");
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300554 goto out_disable;
555 }
556
557 /* If the kernel debugger is active, always disable compression */
558 if (in_dbg_master())
559 goto out_disable;
560
Chris Wilson11be49e2012-11-15 11:32:20 +0000561 if (i915_gem_stolen_setup_compression(dev, intel_fb->obj->base.size)) {
Chris Wilson29ebf902013-07-27 17:23:55 +0100562 if (set_no_fbc_reason(dev_priv, FBC_STOLEN_TOO_SMALL))
563 DRM_DEBUG_KMS("framebuffer too large, disabling compression\n");
Chris Wilson11be49e2012-11-15 11:32:20 +0000564 goto out_disable;
565 }
566
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300567 /* If the scanout has not changed, don't modify the FBC settings.
568 * Note that we make the fundamental assumption that the fb->obj
569 * cannot be unpinned (and have its GTT offset and fence revoked)
570 * without first being decoupled from the scanout and FBC disabled.
571 */
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -0700572 if (dev_priv->fbc.plane == intel_crtc->plane &&
573 dev_priv->fbc.fb_id == fb->base.id &&
574 dev_priv->fbc.y == crtc->y)
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300575 return;
576
577 if (intel_fbc_enabled(dev)) {
578 /* We update FBC along two paths, after changing fb/crtc
579 * configuration (modeswitching) and after page-flipping
580 * finishes. For the latter, we know that not only did
581 * we disable the FBC at the start of the page-flip
582 * sequence, but also more than one vblank has passed.
583 *
584 * For the former case of modeswitching, it is possible
585 * to switch between two FBC valid configurations
586 * instantaneously so we do need to disable the FBC
587 * before we can modify its control registers. We also
588 * have to wait for the next vblank for that to take
589 * effect. However, since we delay enabling FBC we can
590 * assume that a vblank has passed since disabling and
591 * that we can safely alter the registers in the deferred
592 * callback.
593 *
594 * In the scenario that we go from a valid to invalid
595 * and then back to valid FBC configuration we have
596 * no strict enforcement that a vblank occurred since
597 * disabling the FBC. However, along all current pipe
598 * disabling paths we do need to wait for a vblank at
599 * some point. And we wait before enabling FBC anyway.
600 */
601 DRM_DEBUG_KMS("disabling active FBC for update\n");
602 intel_disable_fbc(dev);
603 }
604
Ville Syrjälä993495a2013-12-12 17:27:40 +0200605 intel_enable_fbc(crtc);
Chris Wilson29ebf902013-07-27 17:23:55 +0100606 dev_priv->fbc.no_fbc_reason = FBC_OK;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300607 return;
608
609out_disable:
610 /* Multiple disables should be harmless */
611 if (intel_fbc_enabled(dev)) {
612 DRM_DEBUG_KMS("unsupported config, disabling FBC\n");
613 intel_disable_fbc(dev);
614 }
Chris Wilson11be49e2012-11-15 11:32:20 +0000615 i915_gem_stolen_cleanup_compression(dev);
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300616}
617
Daniel Vetterc921aba2012-04-26 23:28:17 +0200618static void i915_pineview_get_mem_freq(struct drm_device *dev)
619{
620 drm_i915_private_t *dev_priv = dev->dev_private;
621 u32 tmp;
622
623 tmp = I915_READ(CLKCFG);
624
625 switch (tmp & CLKCFG_FSB_MASK) {
626 case CLKCFG_FSB_533:
627 dev_priv->fsb_freq = 533; /* 133*4 */
628 break;
629 case CLKCFG_FSB_800:
630 dev_priv->fsb_freq = 800; /* 200*4 */
631 break;
632 case CLKCFG_FSB_667:
633 dev_priv->fsb_freq = 667; /* 167*4 */
634 break;
635 case CLKCFG_FSB_400:
636 dev_priv->fsb_freq = 400; /* 100*4 */
637 break;
638 }
639
640 switch (tmp & CLKCFG_MEM_MASK) {
641 case CLKCFG_MEM_533:
642 dev_priv->mem_freq = 533;
643 break;
644 case CLKCFG_MEM_667:
645 dev_priv->mem_freq = 667;
646 break;
647 case CLKCFG_MEM_800:
648 dev_priv->mem_freq = 800;
649 break;
650 }
651
652 /* detect pineview DDR3 setting */
653 tmp = I915_READ(CSHRDDR3CTL);
654 dev_priv->is_ddr3 = (tmp & CSHRDDR3CTL_DDR3) ? 1 : 0;
655}
656
657static void i915_ironlake_get_mem_freq(struct drm_device *dev)
658{
659 drm_i915_private_t *dev_priv = dev->dev_private;
660 u16 ddrpll, csipll;
661
662 ddrpll = I915_READ16(DDRMPLL1);
663 csipll = I915_READ16(CSIPLL0);
664
665 switch (ddrpll & 0xff) {
666 case 0xc:
667 dev_priv->mem_freq = 800;
668 break;
669 case 0x10:
670 dev_priv->mem_freq = 1066;
671 break;
672 case 0x14:
673 dev_priv->mem_freq = 1333;
674 break;
675 case 0x18:
676 dev_priv->mem_freq = 1600;
677 break;
678 default:
679 DRM_DEBUG_DRIVER("unknown memory frequency 0x%02x\n",
680 ddrpll & 0xff);
681 dev_priv->mem_freq = 0;
682 break;
683 }
684
Daniel Vetter20e4d402012-08-08 23:35:39 +0200685 dev_priv->ips.r_t = dev_priv->mem_freq;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200686
687 switch (csipll & 0x3ff) {
688 case 0x00c:
689 dev_priv->fsb_freq = 3200;
690 break;
691 case 0x00e:
692 dev_priv->fsb_freq = 3733;
693 break;
694 case 0x010:
695 dev_priv->fsb_freq = 4266;
696 break;
697 case 0x012:
698 dev_priv->fsb_freq = 4800;
699 break;
700 case 0x014:
701 dev_priv->fsb_freq = 5333;
702 break;
703 case 0x016:
704 dev_priv->fsb_freq = 5866;
705 break;
706 case 0x018:
707 dev_priv->fsb_freq = 6400;
708 break;
709 default:
710 DRM_DEBUG_DRIVER("unknown fsb frequency 0x%04x\n",
711 csipll & 0x3ff);
712 dev_priv->fsb_freq = 0;
713 break;
714 }
715
716 if (dev_priv->fsb_freq == 3200) {
Daniel Vetter20e4d402012-08-08 23:35:39 +0200717 dev_priv->ips.c_m = 0;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200718 } else if (dev_priv->fsb_freq > 3200 && dev_priv->fsb_freq <= 4800) {
Daniel Vetter20e4d402012-08-08 23:35:39 +0200719 dev_priv->ips.c_m = 1;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200720 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +0200721 dev_priv->ips.c_m = 2;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200722 }
723}
724
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300725static const struct cxsr_latency cxsr_latency_table[] = {
726 {1, 0, 800, 400, 3382, 33382, 3983, 33983}, /* DDR2-400 SC */
727 {1, 0, 800, 667, 3354, 33354, 3807, 33807}, /* DDR2-667 SC */
728 {1, 0, 800, 800, 3347, 33347, 3763, 33763}, /* DDR2-800 SC */
729 {1, 1, 800, 667, 6420, 36420, 6873, 36873}, /* DDR3-667 SC */
730 {1, 1, 800, 800, 5902, 35902, 6318, 36318}, /* DDR3-800 SC */
731
732 {1, 0, 667, 400, 3400, 33400, 4021, 34021}, /* DDR2-400 SC */
733 {1, 0, 667, 667, 3372, 33372, 3845, 33845}, /* DDR2-667 SC */
734 {1, 0, 667, 800, 3386, 33386, 3822, 33822}, /* DDR2-800 SC */
735 {1, 1, 667, 667, 6438, 36438, 6911, 36911}, /* DDR3-667 SC */
736 {1, 1, 667, 800, 5941, 35941, 6377, 36377}, /* DDR3-800 SC */
737
738 {1, 0, 400, 400, 3472, 33472, 4173, 34173}, /* DDR2-400 SC */
739 {1, 0, 400, 667, 3443, 33443, 3996, 33996}, /* DDR2-667 SC */
740 {1, 0, 400, 800, 3430, 33430, 3946, 33946}, /* DDR2-800 SC */
741 {1, 1, 400, 667, 6509, 36509, 7062, 37062}, /* DDR3-667 SC */
742 {1, 1, 400, 800, 5985, 35985, 6501, 36501}, /* DDR3-800 SC */
743
744 {0, 0, 800, 400, 3438, 33438, 4065, 34065}, /* DDR2-400 SC */
745 {0, 0, 800, 667, 3410, 33410, 3889, 33889}, /* DDR2-667 SC */
746 {0, 0, 800, 800, 3403, 33403, 3845, 33845}, /* DDR2-800 SC */
747 {0, 1, 800, 667, 6476, 36476, 6955, 36955}, /* DDR3-667 SC */
748 {0, 1, 800, 800, 5958, 35958, 6400, 36400}, /* DDR3-800 SC */
749
750 {0, 0, 667, 400, 3456, 33456, 4103, 34106}, /* DDR2-400 SC */
751 {0, 0, 667, 667, 3428, 33428, 3927, 33927}, /* DDR2-667 SC */
752 {0, 0, 667, 800, 3443, 33443, 3905, 33905}, /* DDR2-800 SC */
753 {0, 1, 667, 667, 6494, 36494, 6993, 36993}, /* DDR3-667 SC */
754 {0, 1, 667, 800, 5998, 35998, 6460, 36460}, /* DDR3-800 SC */
755
756 {0, 0, 400, 400, 3528, 33528, 4255, 34255}, /* DDR2-400 SC */
757 {0, 0, 400, 667, 3500, 33500, 4079, 34079}, /* DDR2-667 SC */
758 {0, 0, 400, 800, 3487, 33487, 4029, 34029}, /* DDR2-800 SC */
759 {0, 1, 400, 667, 6566, 36566, 7145, 37145}, /* DDR3-667 SC */
760 {0, 1, 400, 800, 6042, 36042, 6584, 36584}, /* DDR3-800 SC */
761};
762
Daniel Vetter63c62272012-04-21 23:17:55 +0200763static const struct cxsr_latency *intel_get_cxsr_latency(int is_desktop,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300764 int is_ddr3,
765 int fsb,
766 int mem)
767{
768 const struct cxsr_latency *latency;
769 int i;
770
771 if (fsb == 0 || mem == 0)
772 return NULL;
773
774 for (i = 0; i < ARRAY_SIZE(cxsr_latency_table); i++) {
775 latency = &cxsr_latency_table[i];
776 if (is_desktop == latency->is_desktop &&
777 is_ddr3 == latency->is_ddr3 &&
778 fsb == latency->fsb_freq && mem == latency->mem_freq)
779 return latency;
780 }
781
782 DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
783
784 return NULL;
785}
786
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300787static void pineview_disable_cxsr(struct drm_device *dev)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300788{
789 struct drm_i915_private *dev_priv = dev->dev_private;
790
791 /* deactivate cxsr */
792 I915_WRITE(DSPFW3, I915_READ(DSPFW3) & ~PINEVIEW_SELF_REFRESH_EN);
793}
794
795/*
796 * Latency for FIFO fetches is dependent on several factors:
797 * - memory configuration (speed, channels)
798 * - chipset
799 * - current MCH state
800 * It can be fairly high in some situations, so here we assume a fairly
801 * pessimal value. It's a tradeoff between extra memory fetches (if we
802 * set this value too high, the FIFO will fetch frequently to stay full)
803 * and power consumption (set it too low to save power and we might see
804 * FIFO underruns and display "flicker").
805 *
806 * A value of 5us seems to be a good balance; safe for very low end
807 * platforms but not overly aggressive on lower latency configs.
808 */
809static const int latency_ns = 5000;
810
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300811static int i9xx_get_fifo_size(struct drm_device *dev, int plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300812{
813 struct drm_i915_private *dev_priv = dev->dev_private;
814 uint32_t dsparb = I915_READ(DSPARB);
815 int size;
816
817 size = dsparb & 0x7f;
818 if (plane)
819 size = ((dsparb >> DSPARB_CSTART_SHIFT) & 0x7f) - size;
820
821 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
822 plane ? "B" : "A", size);
823
824 return size;
825}
826
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300827static int i85x_get_fifo_size(struct drm_device *dev, int plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300828{
829 struct drm_i915_private *dev_priv = dev->dev_private;
830 uint32_t dsparb = I915_READ(DSPARB);
831 int size;
832
833 size = dsparb & 0x1ff;
834 if (plane)
835 size = ((dsparb >> DSPARB_BEND_SHIFT) & 0x1ff) - size;
836 size >>= 1; /* Convert to cachelines */
837
838 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
839 plane ? "B" : "A", size);
840
841 return size;
842}
843
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300844static int i845_get_fifo_size(struct drm_device *dev, int plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300845{
846 struct drm_i915_private *dev_priv = dev->dev_private;
847 uint32_t dsparb = I915_READ(DSPARB);
848 int size;
849
850 size = dsparb & 0x7f;
851 size >>= 2; /* Convert to cachelines */
852
853 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
854 plane ? "B" : "A",
855 size);
856
857 return size;
858}
859
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300860static int i830_get_fifo_size(struct drm_device *dev, int plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300861{
862 struct drm_i915_private *dev_priv = dev->dev_private;
863 uint32_t dsparb = I915_READ(DSPARB);
864 int size;
865
866 size = dsparb & 0x7f;
867 size >>= 1; /* Convert to cachelines */
868
869 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
870 plane ? "B" : "A", size);
871
872 return size;
873}
874
875/* Pineview has different values for various configs */
876static const struct intel_watermark_params pineview_display_wm = {
877 PINEVIEW_DISPLAY_FIFO,
878 PINEVIEW_MAX_WM,
879 PINEVIEW_DFT_WM,
880 PINEVIEW_GUARD_WM,
881 PINEVIEW_FIFO_LINE_SIZE
882};
883static const struct intel_watermark_params pineview_display_hplloff_wm = {
884 PINEVIEW_DISPLAY_FIFO,
885 PINEVIEW_MAX_WM,
886 PINEVIEW_DFT_HPLLOFF_WM,
887 PINEVIEW_GUARD_WM,
888 PINEVIEW_FIFO_LINE_SIZE
889};
890static const struct intel_watermark_params pineview_cursor_wm = {
891 PINEVIEW_CURSOR_FIFO,
892 PINEVIEW_CURSOR_MAX_WM,
893 PINEVIEW_CURSOR_DFT_WM,
894 PINEVIEW_CURSOR_GUARD_WM,
895 PINEVIEW_FIFO_LINE_SIZE,
896};
897static const struct intel_watermark_params pineview_cursor_hplloff_wm = {
898 PINEVIEW_CURSOR_FIFO,
899 PINEVIEW_CURSOR_MAX_WM,
900 PINEVIEW_CURSOR_DFT_WM,
901 PINEVIEW_CURSOR_GUARD_WM,
902 PINEVIEW_FIFO_LINE_SIZE
903};
904static const struct intel_watermark_params g4x_wm_info = {
905 G4X_FIFO_SIZE,
906 G4X_MAX_WM,
907 G4X_MAX_WM,
908 2,
909 G4X_FIFO_LINE_SIZE,
910};
911static const struct intel_watermark_params g4x_cursor_wm_info = {
912 I965_CURSOR_FIFO,
913 I965_CURSOR_MAX_WM,
914 I965_CURSOR_DFT_WM,
915 2,
916 G4X_FIFO_LINE_SIZE,
917};
918static const struct intel_watermark_params valleyview_wm_info = {
919 VALLEYVIEW_FIFO_SIZE,
920 VALLEYVIEW_MAX_WM,
921 VALLEYVIEW_MAX_WM,
922 2,
923 G4X_FIFO_LINE_SIZE,
924};
925static const struct intel_watermark_params valleyview_cursor_wm_info = {
926 I965_CURSOR_FIFO,
927 VALLEYVIEW_CURSOR_MAX_WM,
928 I965_CURSOR_DFT_WM,
929 2,
930 G4X_FIFO_LINE_SIZE,
931};
932static const struct intel_watermark_params i965_cursor_wm_info = {
933 I965_CURSOR_FIFO,
934 I965_CURSOR_MAX_WM,
935 I965_CURSOR_DFT_WM,
936 2,
937 I915_FIFO_LINE_SIZE,
938};
939static const struct intel_watermark_params i945_wm_info = {
940 I945_FIFO_SIZE,
941 I915_MAX_WM,
942 1,
943 2,
944 I915_FIFO_LINE_SIZE
945};
946static const struct intel_watermark_params i915_wm_info = {
947 I915_FIFO_SIZE,
948 I915_MAX_WM,
949 1,
950 2,
951 I915_FIFO_LINE_SIZE
952};
953static const struct intel_watermark_params i855_wm_info = {
954 I855GM_FIFO_SIZE,
955 I915_MAX_WM,
956 1,
957 2,
958 I830_FIFO_LINE_SIZE
959};
960static const struct intel_watermark_params i830_wm_info = {
961 I830_FIFO_SIZE,
962 I915_MAX_WM,
963 1,
964 2,
965 I830_FIFO_LINE_SIZE
966};
967
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300968/**
969 * intel_calculate_wm - calculate watermark level
970 * @clock_in_khz: pixel clock
971 * @wm: chip FIFO params
972 * @pixel_size: display pixel size
973 * @latency_ns: memory latency for the platform
974 *
975 * Calculate the watermark level (the level at which the display plane will
976 * start fetching from memory again). Each chip has a different display
977 * FIFO size and allocation, so the caller needs to figure that out and pass
978 * in the correct intel_watermark_params structure.
979 *
980 * As the pixel clock runs, the FIFO will be drained at a rate that depends
981 * on the pixel size. When it reaches the watermark level, it'll start
982 * fetching FIFO line sized based chunks from memory until the FIFO fills
983 * past the watermark point. If the FIFO drains completely, a FIFO underrun
984 * will occur, and a display engine hang could result.
985 */
986static unsigned long intel_calculate_wm(unsigned long clock_in_khz,
987 const struct intel_watermark_params *wm,
988 int fifo_size,
989 int pixel_size,
990 unsigned long latency_ns)
991{
992 long entries_required, wm_size;
993
994 /*
995 * Note: we need to make sure we don't overflow for various clock &
996 * latency values.
997 * clocks go from a few thousand to several hundred thousand.
998 * latency is usually a few thousand
999 */
1000 entries_required = ((clock_in_khz / 1000) * pixel_size * latency_ns) /
1001 1000;
1002 entries_required = DIV_ROUND_UP(entries_required, wm->cacheline_size);
1003
1004 DRM_DEBUG_KMS("FIFO entries required for mode: %ld\n", entries_required);
1005
1006 wm_size = fifo_size - (entries_required + wm->guard_size);
1007
1008 DRM_DEBUG_KMS("FIFO watermark level: %ld\n", wm_size);
1009
1010 /* Don't promote wm_size to unsigned... */
1011 if (wm_size > (long)wm->max_wm)
1012 wm_size = wm->max_wm;
1013 if (wm_size <= 0)
1014 wm_size = wm->default_wm;
1015 return wm_size;
1016}
1017
1018static struct drm_crtc *single_enabled_crtc(struct drm_device *dev)
1019{
1020 struct drm_crtc *crtc, *enabled = NULL;
1021
1022 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Chris Wilson3490ea52013-01-07 10:11:40 +00001023 if (intel_crtc_active(crtc)) {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001024 if (enabled)
1025 return NULL;
1026 enabled = crtc;
1027 }
1028 }
1029
1030 return enabled;
1031}
1032
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001033static void pineview_update_wm(struct drm_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001034{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001035 struct drm_device *dev = unused_crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001036 struct drm_i915_private *dev_priv = dev->dev_private;
1037 struct drm_crtc *crtc;
1038 const struct cxsr_latency *latency;
1039 u32 reg;
1040 unsigned long wm;
1041
1042 latency = intel_get_cxsr_latency(IS_PINEVIEW_G(dev), dev_priv->is_ddr3,
1043 dev_priv->fsb_freq, dev_priv->mem_freq);
1044 if (!latency) {
1045 DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
1046 pineview_disable_cxsr(dev);
1047 return;
1048 }
1049
1050 crtc = single_enabled_crtc(dev);
1051 if (crtc) {
Damien Lespiau241bfc32013-09-25 16:45:37 +01001052 const struct drm_display_mode *adjusted_mode;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001053 int pixel_size = crtc->fb->bits_per_pixel / 8;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001054 int clock;
1055
1056 adjusted_mode = &to_intel_crtc(crtc)->config.adjusted_mode;
1057 clock = adjusted_mode->crtc_clock;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001058
1059 /* Display SR */
1060 wm = intel_calculate_wm(clock, &pineview_display_wm,
1061 pineview_display_wm.fifo_size,
1062 pixel_size, latency->display_sr);
1063 reg = I915_READ(DSPFW1);
1064 reg &= ~DSPFW_SR_MASK;
1065 reg |= wm << DSPFW_SR_SHIFT;
1066 I915_WRITE(DSPFW1, reg);
1067 DRM_DEBUG_KMS("DSPFW1 register is %x\n", reg);
1068
1069 /* cursor SR */
1070 wm = intel_calculate_wm(clock, &pineview_cursor_wm,
1071 pineview_display_wm.fifo_size,
1072 pixel_size, latency->cursor_sr);
1073 reg = I915_READ(DSPFW3);
1074 reg &= ~DSPFW_CURSOR_SR_MASK;
1075 reg |= (wm & 0x3f) << DSPFW_CURSOR_SR_SHIFT;
1076 I915_WRITE(DSPFW3, reg);
1077
1078 /* Display HPLL off SR */
1079 wm = intel_calculate_wm(clock, &pineview_display_hplloff_wm,
1080 pineview_display_hplloff_wm.fifo_size,
1081 pixel_size, latency->display_hpll_disable);
1082 reg = I915_READ(DSPFW3);
1083 reg &= ~DSPFW_HPLL_SR_MASK;
1084 reg |= wm & DSPFW_HPLL_SR_MASK;
1085 I915_WRITE(DSPFW3, reg);
1086
1087 /* cursor HPLL off SR */
1088 wm = intel_calculate_wm(clock, &pineview_cursor_hplloff_wm,
1089 pineview_display_hplloff_wm.fifo_size,
1090 pixel_size, latency->cursor_hpll_disable);
1091 reg = I915_READ(DSPFW3);
1092 reg &= ~DSPFW_HPLL_CURSOR_MASK;
1093 reg |= (wm & 0x3f) << DSPFW_HPLL_CURSOR_SHIFT;
1094 I915_WRITE(DSPFW3, reg);
1095 DRM_DEBUG_KMS("DSPFW3 register is %x\n", reg);
1096
1097 /* activate cxsr */
1098 I915_WRITE(DSPFW3,
1099 I915_READ(DSPFW3) | PINEVIEW_SELF_REFRESH_EN);
1100 DRM_DEBUG_KMS("Self-refresh is enabled\n");
1101 } else {
1102 pineview_disable_cxsr(dev);
1103 DRM_DEBUG_KMS("Self-refresh is disabled\n");
1104 }
1105}
1106
1107static bool g4x_compute_wm0(struct drm_device *dev,
1108 int plane,
1109 const struct intel_watermark_params *display,
1110 int display_latency_ns,
1111 const struct intel_watermark_params *cursor,
1112 int cursor_latency_ns,
1113 int *plane_wm,
1114 int *cursor_wm)
1115{
1116 struct drm_crtc *crtc;
Ville Syrjälä4fe85902013-09-04 18:25:22 +03001117 const struct drm_display_mode *adjusted_mode;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001118 int htotal, hdisplay, clock, pixel_size;
1119 int line_time_us, line_count;
1120 int entries, tlb_miss;
1121
1122 crtc = intel_get_crtc_for_plane(dev, plane);
Chris Wilson3490ea52013-01-07 10:11:40 +00001123 if (!intel_crtc_active(crtc)) {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001124 *cursor_wm = cursor->guard_size;
1125 *plane_wm = display->guard_size;
1126 return false;
1127 }
1128
Ville Syrjälä4fe85902013-09-04 18:25:22 +03001129 adjusted_mode = &to_intel_crtc(crtc)->config.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001130 clock = adjusted_mode->crtc_clock;
Ville Syrjälä4fe85902013-09-04 18:25:22 +03001131 htotal = adjusted_mode->htotal;
Ville Syrjälä37327ab2013-09-04 18:25:28 +03001132 hdisplay = to_intel_crtc(crtc)->config.pipe_src_w;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001133 pixel_size = crtc->fb->bits_per_pixel / 8;
1134
1135 /* Use the small buffer method to calculate plane watermark */
1136 entries = ((clock * pixel_size / 1000) * display_latency_ns) / 1000;
1137 tlb_miss = display->fifo_size*display->cacheline_size - hdisplay * 8;
1138 if (tlb_miss > 0)
1139 entries += tlb_miss;
1140 entries = DIV_ROUND_UP(entries, display->cacheline_size);
1141 *plane_wm = entries + display->guard_size;
1142 if (*plane_wm > (int)display->max_wm)
1143 *plane_wm = display->max_wm;
1144
1145 /* Use the large buffer method to calculate cursor watermark */
1146 line_time_us = ((htotal * 1000) / clock);
1147 line_count = (cursor_latency_ns / line_time_us + 1000) / 1000;
1148 entries = line_count * 64 * pixel_size;
1149 tlb_miss = cursor->fifo_size*cursor->cacheline_size - hdisplay * 8;
1150 if (tlb_miss > 0)
1151 entries += tlb_miss;
1152 entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
1153 *cursor_wm = entries + cursor->guard_size;
1154 if (*cursor_wm > (int)cursor->max_wm)
1155 *cursor_wm = (int)cursor->max_wm;
1156
1157 return true;
1158}
1159
1160/*
1161 * Check the wm result.
1162 *
1163 * If any calculated watermark values is larger than the maximum value that
1164 * can be programmed into the associated watermark register, that watermark
1165 * must be disabled.
1166 */
1167static bool g4x_check_srwm(struct drm_device *dev,
1168 int display_wm, int cursor_wm,
1169 const struct intel_watermark_params *display,
1170 const struct intel_watermark_params *cursor)
1171{
1172 DRM_DEBUG_KMS("SR watermark: display plane %d, cursor %d\n",
1173 display_wm, cursor_wm);
1174
1175 if (display_wm > display->max_wm) {
1176 DRM_DEBUG_KMS("display watermark is too large(%d/%ld), disabling\n",
1177 display_wm, display->max_wm);
1178 return false;
1179 }
1180
1181 if (cursor_wm > cursor->max_wm) {
1182 DRM_DEBUG_KMS("cursor watermark is too large(%d/%ld), disabling\n",
1183 cursor_wm, cursor->max_wm);
1184 return false;
1185 }
1186
1187 if (!(display_wm || cursor_wm)) {
1188 DRM_DEBUG_KMS("SR latency is 0, disabling\n");
1189 return false;
1190 }
1191
1192 return true;
1193}
1194
1195static bool g4x_compute_srwm(struct drm_device *dev,
1196 int plane,
1197 int latency_ns,
1198 const struct intel_watermark_params *display,
1199 const struct intel_watermark_params *cursor,
1200 int *display_wm, int *cursor_wm)
1201{
1202 struct drm_crtc *crtc;
Ville Syrjälä4fe85902013-09-04 18:25:22 +03001203 const struct drm_display_mode *adjusted_mode;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001204 int hdisplay, htotal, pixel_size, clock;
1205 unsigned long line_time_us;
1206 int line_count, line_size;
1207 int small, large;
1208 int entries;
1209
1210 if (!latency_ns) {
1211 *display_wm = *cursor_wm = 0;
1212 return false;
1213 }
1214
1215 crtc = intel_get_crtc_for_plane(dev, plane);
Ville Syrjälä4fe85902013-09-04 18:25:22 +03001216 adjusted_mode = &to_intel_crtc(crtc)->config.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001217 clock = adjusted_mode->crtc_clock;
Ville Syrjälä4fe85902013-09-04 18:25:22 +03001218 htotal = adjusted_mode->htotal;
Ville Syrjälä37327ab2013-09-04 18:25:28 +03001219 hdisplay = to_intel_crtc(crtc)->config.pipe_src_w;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001220 pixel_size = crtc->fb->bits_per_pixel / 8;
1221
1222 line_time_us = (htotal * 1000) / clock;
1223 line_count = (latency_ns / line_time_us + 1000) / 1000;
1224 line_size = hdisplay * pixel_size;
1225
1226 /* Use the minimum of the small and large buffer method for primary */
1227 small = ((clock * pixel_size / 1000) * latency_ns) / 1000;
1228 large = line_count * line_size;
1229
1230 entries = DIV_ROUND_UP(min(small, large), display->cacheline_size);
1231 *display_wm = entries + display->guard_size;
1232
1233 /* calculate the self-refresh watermark for display cursor */
1234 entries = line_count * pixel_size * 64;
1235 entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
1236 *cursor_wm = entries + cursor->guard_size;
1237
1238 return g4x_check_srwm(dev,
1239 *display_wm, *cursor_wm,
1240 display, cursor);
1241}
1242
1243static bool vlv_compute_drain_latency(struct drm_device *dev,
1244 int plane,
1245 int *plane_prec_mult,
1246 int *plane_dl,
1247 int *cursor_prec_mult,
1248 int *cursor_dl)
1249{
1250 struct drm_crtc *crtc;
1251 int clock, pixel_size;
1252 int entries;
1253
1254 crtc = intel_get_crtc_for_plane(dev, plane);
Chris Wilson3490ea52013-01-07 10:11:40 +00001255 if (!intel_crtc_active(crtc))
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001256 return false;
1257
Damien Lespiau241bfc32013-09-25 16:45:37 +01001258 clock = to_intel_crtc(crtc)->config.adjusted_mode.crtc_clock;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001259 pixel_size = crtc->fb->bits_per_pixel / 8; /* BPP */
1260
1261 entries = (clock / 1000) * pixel_size;
1262 *plane_prec_mult = (entries > 256) ?
1263 DRAIN_LATENCY_PRECISION_32 : DRAIN_LATENCY_PRECISION_16;
1264 *plane_dl = (64 * (*plane_prec_mult) * 4) / ((clock / 1000) *
1265 pixel_size);
1266
1267 entries = (clock / 1000) * 4; /* BPP is always 4 for cursor */
1268 *cursor_prec_mult = (entries > 256) ?
1269 DRAIN_LATENCY_PRECISION_32 : DRAIN_LATENCY_PRECISION_16;
1270 *cursor_dl = (64 * (*cursor_prec_mult) * 4) / ((clock / 1000) * 4);
1271
1272 return true;
1273}
1274
1275/*
1276 * Update drain latency registers of memory arbiter
1277 *
1278 * Valleyview SoC has a new memory arbiter and needs drain latency registers
1279 * to be programmed. Each plane has a drain latency multiplier and a drain
1280 * latency value.
1281 */
1282
1283static void vlv_update_drain_latency(struct drm_device *dev)
1284{
1285 struct drm_i915_private *dev_priv = dev->dev_private;
1286 int planea_prec, planea_dl, planeb_prec, planeb_dl;
1287 int cursora_prec, cursora_dl, cursorb_prec, cursorb_dl;
1288 int plane_prec_mult, cursor_prec_mult; /* Precision multiplier is
1289 either 16 or 32 */
1290
1291 /* For plane A, Cursor A */
1292 if (vlv_compute_drain_latency(dev, 0, &plane_prec_mult, &planea_dl,
1293 &cursor_prec_mult, &cursora_dl)) {
1294 cursora_prec = (cursor_prec_mult == DRAIN_LATENCY_PRECISION_32) ?
1295 DDL_CURSORA_PRECISION_32 : DDL_CURSORA_PRECISION_16;
1296 planea_prec = (plane_prec_mult == DRAIN_LATENCY_PRECISION_32) ?
1297 DDL_PLANEA_PRECISION_32 : DDL_PLANEA_PRECISION_16;
1298
1299 I915_WRITE(VLV_DDL1, cursora_prec |
1300 (cursora_dl << DDL_CURSORA_SHIFT) |
1301 planea_prec | planea_dl);
1302 }
1303
1304 /* For plane B, Cursor B */
1305 if (vlv_compute_drain_latency(dev, 1, &plane_prec_mult, &planeb_dl,
1306 &cursor_prec_mult, &cursorb_dl)) {
1307 cursorb_prec = (cursor_prec_mult == DRAIN_LATENCY_PRECISION_32) ?
1308 DDL_CURSORB_PRECISION_32 : DDL_CURSORB_PRECISION_16;
1309 planeb_prec = (plane_prec_mult == DRAIN_LATENCY_PRECISION_32) ?
1310 DDL_PLANEB_PRECISION_32 : DDL_PLANEB_PRECISION_16;
1311
1312 I915_WRITE(VLV_DDL2, cursorb_prec |
1313 (cursorb_dl << DDL_CURSORB_SHIFT) |
1314 planeb_prec | planeb_dl);
1315 }
1316}
1317
1318#define single_plane_enabled(mask) is_power_of_2(mask)
1319
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001320static void valleyview_update_wm(struct drm_crtc *crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001321{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001322 struct drm_device *dev = crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001323 static const int sr_latency_ns = 12000;
1324 struct drm_i915_private *dev_priv = dev->dev_private;
1325 int planea_wm, planeb_wm, cursora_wm, cursorb_wm;
1326 int plane_sr, cursor_sr;
Chris Wilsonaf6c4572012-12-11 12:01:43 +00001327 int ignore_plane_sr, ignore_cursor_sr;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001328 unsigned int enabled = 0;
1329
1330 vlv_update_drain_latency(dev);
1331
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001332 if (g4x_compute_wm0(dev, PIPE_A,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001333 &valleyview_wm_info, latency_ns,
1334 &valleyview_cursor_wm_info, latency_ns,
1335 &planea_wm, &cursora_wm))
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001336 enabled |= 1 << PIPE_A;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001337
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001338 if (g4x_compute_wm0(dev, PIPE_B,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001339 &valleyview_wm_info, latency_ns,
1340 &valleyview_cursor_wm_info, latency_ns,
1341 &planeb_wm, &cursorb_wm))
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001342 enabled |= 1 << PIPE_B;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001343
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001344 if (single_plane_enabled(enabled) &&
1345 g4x_compute_srwm(dev, ffs(enabled) - 1,
1346 sr_latency_ns,
1347 &valleyview_wm_info,
1348 &valleyview_cursor_wm_info,
Chris Wilsonaf6c4572012-12-11 12:01:43 +00001349 &plane_sr, &ignore_cursor_sr) &&
1350 g4x_compute_srwm(dev, ffs(enabled) - 1,
1351 2*sr_latency_ns,
1352 &valleyview_wm_info,
1353 &valleyview_cursor_wm_info,
Chris Wilson52bd02d2012-12-07 10:43:24 +00001354 &ignore_plane_sr, &cursor_sr)) {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001355 I915_WRITE(FW_BLC_SELF_VLV, FW_CSPWRDWNEN);
Chris Wilson52bd02d2012-12-07 10:43:24 +00001356 } else {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001357 I915_WRITE(FW_BLC_SELF_VLV,
1358 I915_READ(FW_BLC_SELF_VLV) & ~FW_CSPWRDWNEN);
Chris Wilson52bd02d2012-12-07 10:43:24 +00001359 plane_sr = cursor_sr = 0;
1360 }
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001361
1362 DRM_DEBUG_KMS("Setting FIFO watermarks - A: plane=%d, cursor=%d, B: plane=%d, cursor=%d, SR: plane=%d, cursor=%d\n",
1363 planea_wm, cursora_wm,
1364 planeb_wm, cursorb_wm,
1365 plane_sr, cursor_sr);
1366
1367 I915_WRITE(DSPFW1,
1368 (plane_sr << DSPFW_SR_SHIFT) |
1369 (cursorb_wm << DSPFW_CURSORB_SHIFT) |
1370 (planeb_wm << DSPFW_PLANEB_SHIFT) |
1371 planea_wm);
1372 I915_WRITE(DSPFW2,
Chris Wilson8c919b22012-12-04 16:33:19 +00001373 (I915_READ(DSPFW2) & ~DSPFW_CURSORA_MASK) |
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001374 (cursora_wm << DSPFW_CURSORA_SHIFT));
1375 I915_WRITE(DSPFW3,
Chris Wilson8c919b22012-12-04 16:33:19 +00001376 (I915_READ(DSPFW3) & ~DSPFW_CURSOR_SR_MASK) |
1377 (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001378}
1379
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001380static void g4x_update_wm(struct drm_crtc *crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001381{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001382 struct drm_device *dev = crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001383 static const int sr_latency_ns = 12000;
1384 struct drm_i915_private *dev_priv = dev->dev_private;
1385 int planea_wm, planeb_wm, cursora_wm, cursorb_wm;
1386 int plane_sr, cursor_sr;
1387 unsigned int enabled = 0;
1388
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001389 if (g4x_compute_wm0(dev, PIPE_A,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001390 &g4x_wm_info, latency_ns,
1391 &g4x_cursor_wm_info, latency_ns,
1392 &planea_wm, &cursora_wm))
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001393 enabled |= 1 << PIPE_A;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001394
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001395 if (g4x_compute_wm0(dev, PIPE_B,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001396 &g4x_wm_info, latency_ns,
1397 &g4x_cursor_wm_info, latency_ns,
1398 &planeb_wm, &cursorb_wm))
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001399 enabled |= 1 << PIPE_B;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001400
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001401 if (single_plane_enabled(enabled) &&
1402 g4x_compute_srwm(dev, ffs(enabled) - 1,
1403 sr_latency_ns,
1404 &g4x_wm_info,
1405 &g4x_cursor_wm_info,
Chris Wilson52bd02d2012-12-07 10:43:24 +00001406 &plane_sr, &cursor_sr)) {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001407 I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_EN);
Chris Wilson52bd02d2012-12-07 10:43:24 +00001408 } else {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001409 I915_WRITE(FW_BLC_SELF,
1410 I915_READ(FW_BLC_SELF) & ~FW_BLC_SELF_EN);
Chris Wilson52bd02d2012-12-07 10:43:24 +00001411 plane_sr = cursor_sr = 0;
1412 }
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001413
1414 DRM_DEBUG_KMS("Setting FIFO watermarks - A: plane=%d, cursor=%d, B: plane=%d, cursor=%d, SR: plane=%d, cursor=%d\n",
1415 planea_wm, cursora_wm,
1416 planeb_wm, cursorb_wm,
1417 plane_sr, cursor_sr);
1418
1419 I915_WRITE(DSPFW1,
1420 (plane_sr << DSPFW_SR_SHIFT) |
1421 (cursorb_wm << DSPFW_CURSORB_SHIFT) |
1422 (planeb_wm << DSPFW_PLANEB_SHIFT) |
1423 planea_wm);
1424 I915_WRITE(DSPFW2,
Chris Wilson8c919b22012-12-04 16:33:19 +00001425 (I915_READ(DSPFW2) & ~DSPFW_CURSORA_MASK) |
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001426 (cursora_wm << DSPFW_CURSORA_SHIFT));
1427 /* HPLL off in SR has some issues on G4x... disable it */
1428 I915_WRITE(DSPFW3,
Chris Wilson8c919b22012-12-04 16:33:19 +00001429 (I915_READ(DSPFW3) & ~(DSPFW_HPLL_SR_EN | DSPFW_CURSOR_SR_MASK)) |
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001430 (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
1431}
1432
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001433static void i965_update_wm(struct drm_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001434{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001435 struct drm_device *dev = unused_crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001436 struct drm_i915_private *dev_priv = dev->dev_private;
1437 struct drm_crtc *crtc;
1438 int srwm = 1;
1439 int cursor_sr = 16;
1440
1441 /* Calc sr entries for one plane configs */
1442 crtc = single_enabled_crtc(dev);
1443 if (crtc) {
1444 /* self-refresh has much higher latency */
1445 static const int sr_latency_ns = 12000;
Ville Syrjälä4fe85902013-09-04 18:25:22 +03001446 const struct drm_display_mode *adjusted_mode =
1447 &to_intel_crtc(crtc)->config.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001448 int clock = adjusted_mode->crtc_clock;
Ville Syrjälä4fe85902013-09-04 18:25:22 +03001449 int htotal = adjusted_mode->htotal;
Ville Syrjälä37327ab2013-09-04 18:25:28 +03001450 int hdisplay = to_intel_crtc(crtc)->config.pipe_src_w;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001451 int pixel_size = crtc->fb->bits_per_pixel / 8;
1452 unsigned long line_time_us;
1453 int entries;
1454
1455 line_time_us = ((htotal * 1000) / clock);
1456
1457 /* Use ns/us then divide to preserve precision */
1458 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
1459 pixel_size * hdisplay;
1460 entries = DIV_ROUND_UP(entries, I915_FIFO_LINE_SIZE);
1461 srwm = I965_FIFO_SIZE - entries;
1462 if (srwm < 0)
1463 srwm = 1;
1464 srwm &= 0x1ff;
1465 DRM_DEBUG_KMS("self-refresh entries: %d, wm: %d\n",
1466 entries, srwm);
1467
1468 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
1469 pixel_size * 64;
1470 entries = DIV_ROUND_UP(entries,
1471 i965_cursor_wm_info.cacheline_size);
1472 cursor_sr = i965_cursor_wm_info.fifo_size -
1473 (entries + i965_cursor_wm_info.guard_size);
1474
1475 if (cursor_sr > i965_cursor_wm_info.max_wm)
1476 cursor_sr = i965_cursor_wm_info.max_wm;
1477
1478 DRM_DEBUG_KMS("self-refresh watermark: display plane %d "
1479 "cursor %d\n", srwm, cursor_sr);
1480
1481 if (IS_CRESTLINE(dev))
1482 I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_EN);
1483 } else {
1484 /* Turn off self refresh if both pipes are enabled */
1485 if (IS_CRESTLINE(dev))
1486 I915_WRITE(FW_BLC_SELF, I915_READ(FW_BLC_SELF)
1487 & ~FW_BLC_SELF_EN);
1488 }
1489
1490 DRM_DEBUG_KMS("Setting FIFO watermarks - A: 8, B: 8, C: 8, SR %d\n",
1491 srwm);
1492
1493 /* 965 has limitations... */
1494 I915_WRITE(DSPFW1, (srwm << DSPFW_SR_SHIFT) |
1495 (8 << 16) | (8 << 8) | (8 << 0));
1496 I915_WRITE(DSPFW2, (8 << 8) | (8 << 0));
1497 /* update cursor SR watermark */
1498 I915_WRITE(DSPFW3, (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
1499}
1500
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001501static void i9xx_update_wm(struct drm_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001502{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001503 struct drm_device *dev = unused_crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001504 struct drm_i915_private *dev_priv = dev->dev_private;
1505 const struct intel_watermark_params *wm_info;
1506 uint32_t fwater_lo;
1507 uint32_t fwater_hi;
1508 int cwm, srwm = 1;
1509 int fifo_size;
1510 int planea_wm, planeb_wm;
1511 struct drm_crtc *crtc, *enabled = NULL;
1512
1513 if (IS_I945GM(dev))
1514 wm_info = &i945_wm_info;
1515 else if (!IS_GEN2(dev))
1516 wm_info = &i915_wm_info;
1517 else
1518 wm_info = &i855_wm_info;
1519
1520 fifo_size = dev_priv->display.get_fifo_size(dev, 0);
1521 crtc = intel_get_crtc_for_plane(dev, 0);
Chris Wilson3490ea52013-01-07 10:11:40 +00001522 if (intel_crtc_active(crtc)) {
Damien Lespiau241bfc32013-09-25 16:45:37 +01001523 const struct drm_display_mode *adjusted_mode;
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001524 int cpp = crtc->fb->bits_per_pixel / 8;
1525 if (IS_GEN2(dev))
1526 cpp = 4;
1527
Damien Lespiau241bfc32013-09-25 16:45:37 +01001528 adjusted_mode = &to_intel_crtc(crtc)->config.adjusted_mode;
1529 planea_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001530 wm_info, fifo_size, cpp,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001531 latency_ns);
1532 enabled = crtc;
1533 } else
1534 planea_wm = fifo_size - wm_info->guard_size;
1535
1536 fifo_size = dev_priv->display.get_fifo_size(dev, 1);
1537 crtc = intel_get_crtc_for_plane(dev, 1);
Chris Wilson3490ea52013-01-07 10:11:40 +00001538 if (intel_crtc_active(crtc)) {
Damien Lespiau241bfc32013-09-25 16:45:37 +01001539 const struct drm_display_mode *adjusted_mode;
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001540 int cpp = crtc->fb->bits_per_pixel / 8;
1541 if (IS_GEN2(dev))
1542 cpp = 4;
1543
Damien Lespiau241bfc32013-09-25 16:45:37 +01001544 adjusted_mode = &to_intel_crtc(crtc)->config.adjusted_mode;
1545 planeb_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001546 wm_info, fifo_size, cpp,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001547 latency_ns);
1548 if (enabled == NULL)
1549 enabled = crtc;
1550 else
1551 enabled = NULL;
1552 } else
1553 planeb_wm = fifo_size - wm_info->guard_size;
1554
1555 DRM_DEBUG_KMS("FIFO watermarks - A: %d, B: %d\n", planea_wm, planeb_wm);
1556
1557 /*
1558 * Overlay gets an aggressive default since video jitter is bad.
1559 */
1560 cwm = 2;
1561
1562 /* Play safe and disable self-refresh before adjusting watermarks. */
1563 if (IS_I945G(dev) || IS_I945GM(dev))
1564 I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_EN_MASK | 0);
1565 else if (IS_I915GM(dev))
1566 I915_WRITE(INSTPM, I915_READ(INSTPM) & ~INSTPM_SELF_EN);
1567
1568 /* Calc sr entries for one plane configs */
1569 if (HAS_FW_BLC(dev) && enabled) {
1570 /* self-refresh has much higher latency */
1571 static const int sr_latency_ns = 6000;
Ville Syrjälä4fe85902013-09-04 18:25:22 +03001572 const struct drm_display_mode *adjusted_mode =
1573 &to_intel_crtc(enabled)->config.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001574 int clock = adjusted_mode->crtc_clock;
Ville Syrjälä4fe85902013-09-04 18:25:22 +03001575 int htotal = adjusted_mode->htotal;
Daniel Vetterf727b492013-11-20 15:02:10 +01001576 int hdisplay = to_intel_crtc(enabled)->config.pipe_src_w;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001577 int pixel_size = enabled->fb->bits_per_pixel / 8;
1578 unsigned long line_time_us;
1579 int entries;
1580
1581 line_time_us = (htotal * 1000) / clock;
1582
1583 /* Use ns/us then divide to preserve precision */
1584 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
1585 pixel_size * hdisplay;
1586 entries = DIV_ROUND_UP(entries, wm_info->cacheline_size);
1587 DRM_DEBUG_KMS("self-refresh entries: %d\n", entries);
1588 srwm = wm_info->fifo_size - entries;
1589 if (srwm < 0)
1590 srwm = 1;
1591
1592 if (IS_I945G(dev) || IS_I945GM(dev))
1593 I915_WRITE(FW_BLC_SELF,
1594 FW_BLC_SELF_FIFO_MASK | (srwm & 0xff));
1595 else if (IS_I915GM(dev))
1596 I915_WRITE(FW_BLC_SELF, srwm & 0x3f);
1597 }
1598
1599 DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d, B: %d, C: %d, SR %d\n",
1600 planea_wm, planeb_wm, cwm, srwm);
1601
1602 fwater_lo = ((planeb_wm & 0x3f) << 16) | (planea_wm & 0x3f);
1603 fwater_hi = (cwm & 0x1f);
1604
1605 /* Set request length to 8 cachelines per fetch */
1606 fwater_lo = fwater_lo | (1 << 24) | (1 << 8);
1607 fwater_hi = fwater_hi | (1 << 8);
1608
1609 I915_WRITE(FW_BLC, fwater_lo);
1610 I915_WRITE(FW_BLC2, fwater_hi);
1611
1612 if (HAS_FW_BLC(dev)) {
1613 if (enabled) {
1614 if (IS_I945G(dev) || IS_I945GM(dev))
1615 I915_WRITE(FW_BLC_SELF,
1616 FW_BLC_SELF_EN_MASK | FW_BLC_SELF_EN);
1617 else if (IS_I915GM(dev))
1618 I915_WRITE(INSTPM, I915_READ(INSTPM) | INSTPM_SELF_EN);
1619 DRM_DEBUG_KMS("memory self refresh enabled\n");
1620 } else
1621 DRM_DEBUG_KMS("memory self refresh disabled\n");
1622 }
1623}
1624
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001625static void i830_update_wm(struct drm_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001626{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001627 struct drm_device *dev = unused_crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001628 struct drm_i915_private *dev_priv = dev->dev_private;
1629 struct drm_crtc *crtc;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001630 const struct drm_display_mode *adjusted_mode;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001631 uint32_t fwater_lo;
1632 int planea_wm;
1633
1634 crtc = single_enabled_crtc(dev);
1635 if (crtc == NULL)
1636 return;
1637
Damien Lespiau241bfc32013-09-25 16:45:37 +01001638 adjusted_mode = &to_intel_crtc(crtc)->config.adjusted_mode;
1639 planea_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Ville Syrjälä4fe85902013-09-04 18:25:22 +03001640 &i830_wm_info,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001641 dev_priv->display.get_fifo_size(dev, 0),
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001642 4, latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001643 fwater_lo = I915_READ(FW_BLC) & ~0xfff;
1644 fwater_lo |= (3<<8) | planea_wm;
1645
1646 DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d\n", planea_wm);
1647
1648 I915_WRITE(FW_BLC, fwater_lo);
1649}
1650
Ville Syrjälä36587292013-07-05 11:57:16 +03001651static uint32_t ilk_pipe_pixel_rate(struct drm_device *dev,
1652 struct drm_crtc *crtc)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001653{
1654 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilsonfd4daa92013-08-27 17:04:17 +01001655 uint32_t pixel_rate;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001656
Damien Lespiau241bfc32013-09-25 16:45:37 +01001657 pixel_rate = intel_crtc->config.adjusted_mode.crtc_clock;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001658
1659 /* We only use IF-ID interlacing. If we ever use PF-ID we'll need to
1660 * adjust the pixel_rate here. */
1661
Chris Wilsonfd4daa92013-08-27 17:04:17 +01001662 if (intel_crtc->config.pch_pfit.enabled) {
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001663 uint64_t pipe_w, pipe_h, pfit_w, pfit_h;
Chris Wilsonfd4daa92013-08-27 17:04:17 +01001664 uint32_t pfit_size = intel_crtc->config.pch_pfit.size;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001665
Ville Syrjälä37327ab2013-09-04 18:25:28 +03001666 pipe_w = intel_crtc->config.pipe_src_w;
1667 pipe_h = intel_crtc->config.pipe_src_h;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001668 pfit_w = (pfit_size >> 16) & 0xFFFF;
1669 pfit_h = pfit_size & 0xFFFF;
1670 if (pipe_w < pfit_w)
1671 pipe_w = pfit_w;
1672 if (pipe_h < pfit_h)
1673 pipe_h = pfit_h;
1674
1675 pixel_rate = div_u64((uint64_t) pixel_rate * pipe_w * pipe_h,
1676 pfit_w * pfit_h);
1677 }
1678
1679 return pixel_rate;
1680}
1681
Ville Syrjälä37126462013-08-01 16:18:55 +03001682/* latency must be in 0.1us units. */
Ville Syrjälä23297042013-07-05 11:57:17 +03001683static uint32_t ilk_wm_method1(uint32_t pixel_rate, uint8_t bytes_per_pixel,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001684 uint32_t latency)
1685{
1686 uint64_t ret;
1687
Ville Syrjälä3312ba62013-08-01 16:18:53 +03001688 if (WARN(latency == 0, "Latency value missing\n"))
1689 return UINT_MAX;
1690
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001691 ret = (uint64_t) pixel_rate * bytes_per_pixel * latency;
1692 ret = DIV_ROUND_UP_ULL(ret, 64 * 10000) + 2;
1693
1694 return ret;
1695}
1696
Ville Syrjälä37126462013-08-01 16:18:55 +03001697/* latency must be in 0.1us units. */
Ville Syrjälä23297042013-07-05 11:57:17 +03001698static uint32_t ilk_wm_method2(uint32_t pixel_rate, uint32_t pipe_htotal,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001699 uint32_t horiz_pixels, uint8_t bytes_per_pixel,
1700 uint32_t latency)
1701{
1702 uint32_t ret;
1703
Ville Syrjälä3312ba62013-08-01 16:18:53 +03001704 if (WARN(latency == 0, "Latency value missing\n"))
1705 return UINT_MAX;
1706
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001707 ret = (latency * pixel_rate) / (pipe_htotal * 10000);
1708 ret = (ret + 1) * horiz_pixels * bytes_per_pixel;
1709 ret = DIV_ROUND_UP(ret, 64) + 2;
1710 return ret;
1711}
1712
Ville Syrjälä23297042013-07-05 11:57:17 +03001713static uint32_t ilk_wm_fbc(uint32_t pri_val, uint32_t horiz_pixels,
Paulo Zanonicca32e92013-05-31 11:45:06 -03001714 uint8_t bytes_per_pixel)
1715{
1716 return DIV_ROUND_UP(pri_val * 64, horiz_pixels * bytes_per_pixel) + 2;
1717}
1718
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001719struct hsw_pipe_wm_parameters {
1720 bool active;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001721 uint32_t pipe_htotal;
1722 uint32_t pixel_rate;
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001723 struct intel_plane_wm_parameters pri;
1724 struct intel_plane_wm_parameters spr;
1725 struct intel_plane_wm_parameters cur;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001726};
1727
Paulo Zanonicca32e92013-05-31 11:45:06 -03001728struct hsw_wm_maximums {
1729 uint16_t pri;
1730 uint16_t spr;
1731 uint16_t cur;
1732 uint16_t fbc;
1733};
1734
Ville Syrjälä240264f2013-08-07 13:29:12 +03001735/* used in computing the new watermarks state */
1736struct intel_wm_config {
1737 unsigned int num_pipes_active;
1738 bool sprites_enabled;
1739 bool sprites_scaled;
Ville Syrjälä240264f2013-08-07 13:29:12 +03001740};
1741
Ville Syrjälä37126462013-08-01 16:18:55 +03001742/*
1743 * For both WM_PIPE and WM_LP.
1744 * mem_value must be in 0.1us units.
1745 */
Ville Syrjäläac830fe2013-08-30 14:30:23 +03001746static uint32_t ilk_compute_pri_wm(const struct hsw_pipe_wm_parameters *params,
Paulo Zanonicca32e92013-05-31 11:45:06 -03001747 uint32_t mem_value,
1748 bool is_lp)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001749{
Paulo Zanonicca32e92013-05-31 11:45:06 -03001750 uint32_t method1, method2;
1751
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001752 if (!params->active || !params->pri.enabled)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001753 return 0;
1754
Ville Syrjälä23297042013-07-05 11:57:17 +03001755 method1 = ilk_wm_method1(params->pixel_rate,
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001756 params->pri.bytes_per_pixel,
Paulo Zanonicca32e92013-05-31 11:45:06 -03001757 mem_value);
1758
1759 if (!is_lp)
1760 return method1;
1761
Ville Syrjälä23297042013-07-05 11:57:17 +03001762 method2 = ilk_wm_method2(params->pixel_rate,
Paulo Zanonicca32e92013-05-31 11:45:06 -03001763 params->pipe_htotal,
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001764 params->pri.horiz_pixels,
1765 params->pri.bytes_per_pixel,
Paulo Zanonicca32e92013-05-31 11:45:06 -03001766 mem_value);
1767
1768 return min(method1, method2);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001769}
1770
Ville Syrjälä37126462013-08-01 16:18:55 +03001771/*
1772 * For both WM_PIPE and WM_LP.
1773 * mem_value must be in 0.1us units.
1774 */
Ville Syrjäläac830fe2013-08-30 14:30:23 +03001775static uint32_t ilk_compute_spr_wm(const struct hsw_pipe_wm_parameters *params,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001776 uint32_t mem_value)
1777{
1778 uint32_t method1, method2;
1779
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001780 if (!params->active || !params->spr.enabled)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001781 return 0;
1782
Ville Syrjälä23297042013-07-05 11:57:17 +03001783 method1 = ilk_wm_method1(params->pixel_rate,
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001784 params->spr.bytes_per_pixel,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001785 mem_value);
Ville Syrjälä23297042013-07-05 11:57:17 +03001786 method2 = ilk_wm_method2(params->pixel_rate,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001787 params->pipe_htotal,
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001788 params->spr.horiz_pixels,
1789 params->spr.bytes_per_pixel,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001790 mem_value);
1791 return min(method1, method2);
1792}
1793
Ville Syrjälä37126462013-08-01 16:18:55 +03001794/*
1795 * For both WM_PIPE and WM_LP.
1796 * mem_value must be in 0.1us units.
1797 */
Ville Syrjäläac830fe2013-08-30 14:30:23 +03001798static uint32_t ilk_compute_cur_wm(const struct hsw_pipe_wm_parameters *params,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001799 uint32_t mem_value)
1800{
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001801 if (!params->active || !params->cur.enabled)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001802 return 0;
1803
Ville Syrjälä23297042013-07-05 11:57:17 +03001804 return ilk_wm_method2(params->pixel_rate,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001805 params->pipe_htotal,
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001806 params->cur.horiz_pixels,
1807 params->cur.bytes_per_pixel,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001808 mem_value);
1809}
1810
Paulo Zanonicca32e92013-05-31 11:45:06 -03001811/* Only for WM_LP. */
Ville Syrjäläac830fe2013-08-30 14:30:23 +03001812static uint32_t ilk_compute_fbc_wm(const struct hsw_pipe_wm_parameters *params,
Ville Syrjälä1fda9882013-07-05 11:57:19 +03001813 uint32_t pri_val)
Paulo Zanonicca32e92013-05-31 11:45:06 -03001814{
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001815 if (!params->active || !params->pri.enabled)
Paulo Zanonicca32e92013-05-31 11:45:06 -03001816 return 0;
1817
Ville Syrjälä23297042013-07-05 11:57:17 +03001818 return ilk_wm_fbc(pri_val,
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001819 params->pri.horiz_pixels,
1820 params->pri.bytes_per_pixel);
Paulo Zanonicca32e92013-05-31 11:45:06 -03001821}
1822
Ville Syrjälä158ae642013-08-07 13:28:19 +03001823static unsigned int ilk_display_fifo_size(const struct drm_device *dev)
1824{
Ville Syrjälä416f4722013-11-02 21:07:46 -07001825 if (INTEL_INFO(dev)->gen >= 8)
1826 return 3072;
1827 else if (INTEL_INFO(dev)->gen >= 7)
Ville Syrjälä158ae642013-08-07 13:28:19 +03001828 return 768;
1829 else
1830 return 512;
1831}
1832
1833/* Calculate the maximum primary/sprite plane watermark */
1834static unsigned int ilk_plane_wm_max(const struct drm_device *dev,
1835 int level,
Ville Syrjälä240264f2013-08-07 13:29:12 +03001836 const struct intel_wm_config *config,
Ville Syrjälä158ae642013-08-07 13:28:19 +03001837 enum intel_ddb_partitioning ddb_partitioning,
1838 bool is_sprite)
1839{
1840 unsigned int fifo_size = ilk_display_fifo_size(dev);
1841 unsigned int max;
1842
1843 /* if sprites aren't enabled, sprites get nothing */
Ville Syrjälä240264f2013-08-07 13:29:12 +03001844 if (is_sprite && !config->sprites_enabled)
Ville Syrjälä158ae642013-08-07 13:28:19 +03001845 return 0;
1846
1847 /* HSW allows LP1+ watermarks even with multiple pipes */
Ville Syrjälä240264f2013-08-07 13:29:12 +03001848 if (level == 0 || config->num_pipes_active > 1) {
Ville Syrjälä158ae642013-08-07 13:28:19 +03001849 fifo_size /= INTEL_INFO(dev)->num_pipes;
1850
1851 /*
1852 * For some reason the non self refresh
1853 * FIFO size is only half of the self
1854 * refresh FIFO size on ILK/SNB.
1855 */
1856 if (INTEL_INFO(dev)->gen <= 6)
1857 fifo_size /= 2;
1858 }
1859
Ville Syrjälä240264f2013-08-07 13:29:12 +03001860 if (config->sprites_enabled) {
Ville Syrjälä158ae642013-08-07 13:28:19 +03001861 /* level 0 is always calculated with 1:1 split */
1862 if (level > 0 && ddb_partitioning == INTEL_DDB_PART_5_6) {
1863 if (is_sprite)
1864 fifo_size *= 5;
1865 fifo_size /= 6;
1866 } else {
1867 fifo_size /= 2;
1868 }
1869 }
1870
1871 /* clamp to max that the registers can hold */
Ville Syrjälä416f4722013-11-02 21:07:46 -07001872 if (INTEL_INFO(dev)->gen >= 8)
1873 max = level == 0 ? 255 : 2047;
1874 else if (INTEL_INFO(dev)->gen >= 7)
Ville Syrjälä158ae642013-08-07 13:28:19 +03001875 /* IVB/HSW primary/sprite plane watermarks */
1876 max = level == 0 ? 127 : 1023;
1877 else if (!is_sprite)
1878 /* ILK/SNB primary plane watermarks */
1879 max = level == 0 ? 127 : 511;
1880 else
1881 /* ILK/SNB sprite plane watermarks */
1882 max = level == 0 ? 63 : 255;
1883
1884 return min(fifo_size, max);
1885}
1886
1887/* Calculate the maximum cursor plane watermark */
1888static unsigned int ilk_cursor_wm_max(const struct drm_device *dev,
Ville Syrjälä240264f2013-08-07 13:29:12 +03001889 int level,
1890 const struct intel_wm_config *config)
Ville Syrjälä158ae642013-08-07 13:28:19 +03001891{
1892 /* HSW LP1+ watermarks w/ multiple pipes */
Ville Syrjälä240264f2013-08-07 13:29:12 +03001893 if (level > 0 && config->num_pipes_active > 1)
Ville Syrjälä158ae642013-08-07 13:28:19 +03001894 return 64;
1895
1896 /* otherwise just report max that registers can hold */
1897 if (INTEL_INFO(dev)->gen >= 7)
1898 return level == 0 ? 63 : 255;
1899 else
1900 return level == 0 ? 31 : 63;
1901}
1902
1903/* Calculate the maximum FBC watermark */
Ville Syrjälä416f4722013-11-02 21:07:46 -07001904static unsigned int ilk_fbc_wm_max(struct drm_device *dev)
Ville Syrjälä158ae642013-08-07 13:28:19 +03001905{
1906 /* max that registers can hold */
Ville Syrjälä416f4722013-11-02 21:07:46 -07001907 if (INTEL_INFO(dev)->gen >= 8)
1908 return 31;
1909 else
1910 return 15;
Ville Syrjälä158ae642013-08-07 13:28:19 +03001911}
1912
Ville Syrjälä34982fe2013-10-09 19:18:09 +03001913static void ilk_compute_wm_maximums(struct drm_device *dev,
1914 int level,
1915 const struct intel_wm_config *config,
1916 enum intel_ddb_partitioning ddb_partitioning,
1917 struct hsw_wm_maximums *max)
Ville Syrjälä158ae642013-08-07 13:28:19 +03001918{
Ville Syrjälä240264f2013-08-07 13:29:12 +03001919 max->pri = ilk_plane_wm_max(dev, level, config, ddb_partitioning, false);
1920 max->spr = ilk_plane_wm_max(dev, level, config, ddb_partitioning, true);
1921 max->cur = ilk_cursor_wm_max(dev, level, config);
Ville Syrjälä416f4722013-11-02 21:07:46 -07001922 max->fbc = ilk_fbc_wm_max(dev);
Ville Syrjälä158ae642013-08-07 13:28:19 +03001923}
1924
Ville Syrjäläd9395652013-10-09 19:18:10 +03001925static bool ilk_validate_wm_level(int level,
1926 const struct hsw_wm_maximums *max,
1927 struct intel_wm_level *result)
Ville Syrjäläa9786a12013-08-07 13:24:47 +03001928{
1929 bool ret;
1930
1931 /* already determined to be invalid? */
1932 if (!result->enable)
1933 return false;
1934
1935 result->enable = result->pri_val <= max->pri &&
1936 result->spr_val <= max->spr &&
1937 result->cur_val <= max->cur;
1938
1939 ret = result->enable;
1940
1941 /*
1942 * HACK until we can pre-compute everything,
1943 * and thus fail gracefully if LP0 watermarks
1944 * are exceeded...
1945 */
1946 if (level == 0 && !result->enable) {
1947 if (result->pri_val > max->pri)
1948 DRM_DEBUG_KMS("Primary WM%d too large %u (max %u)\n",
1949 level, result->pri_val, max->pri);
1950 if (result->spr_val > max->spr)
1951 DRM_DEBUG_KMS("Sprite WM%d too large %u (max %u)\n",
1952 level, result->spr_val, max->spr);
1953 if (result->cur_val > max->cur)
1954 DRM_DEBUG_KMS("Cursor WM%d too large %u (max %u)\n",
1955 level, result->cur_val, max->cur);
1956
1957 result->pri_val = min_t(uint32_t, result->pri_val, max->pri);
1958 result->spr_val = min_t(uint32_t, result->spr_val, max->spr);
1959 result->cur_val = min_t(uint32_t, result->cur_val, max->cur);
1960 result->enable = true;
1961 }
1962
Ville Syrjäläa9786a12013-08-07 13:24:47 +03001963 return ret;
1964}
1965
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03001966static void ilk_compute_wm_level(struct drm_i915_private *dev_priv,
1967 int level,
Ville Syrjäläac830fe2013-08-30 14:30:23 +03001968 const struct hsw_pipe_wm_parameters *p,
Ville Syrjälä1fd527c2013-08-06 22:24:05 +03001969 struct intel_wm_level *result)
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03001970{
1971 uint16_t pri_latency = dev_priv->wm.pri_latency[level];
1972 uint16_t spr_latency = dev_priv->wm.spr_latency[level];
1973 uint16_t cur_latency = dev_priv->wm.cur_latency[level];
1974
1975 /* WM1+ latency values stored in 0.5us units */
1976 if (level > 0) {
1977 pri_latency *= 5;
1978 spr_latency *= 5;
1979 cur_latency *= 5;
1980 }
1981
1982 result->pri_val = ilk_compute_pri_wm(p, pri_latency, level);
1983 result->spr_val = ilk_compute_spr_wm(p, spr_latency);
1984 result->cur_val = ilk_compute_cur_wm(p, cur_latency);
1985 result->fbc_val = ilk_compute_fbc_wm(p, result->pri_val);
1986 result->enable = true;
1987}
1988
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001989static uint32_t
1990hsw_compute_linetime_wm(struct drm_device *dev, struct drm_crtc *crtc)
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03001991{
1992 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03001993 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03001994 struct drm_display_mode *mode = &intel_crtc->config.adjusted_mode;
Paulo Zanoni85a02de2013-05-03 17:23:43 -03001995 u32 linetime, ips_linetime;
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03001996
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001997 if (!intel_crtc_active(crtc))
1998 return 0;
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03001999
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002000 /* The WM are computed with base on how long it takes to fill a single
2001 * row at the given clock rate, multiplied by 8.
2002 * */
Paulo Zanoni85a02de2013-05-03 17:23:43 -03002003 linetime = DIV_ROUND_CLOSEST(mode->htotal * 1000 * 8, mode->clock);
2004 ips_linetime = DIV_ROUND_CLOSEST(mode->htotal * 1000 * 8,
2005 intel_ddi_get_cdclk_freq(dev_priv));
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002006
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002007 return PIPE_WM_LINETIME_IPS_LINETIME(ips_linetime) |
2008 PIPE_WM_LINETIME_TIME(linetime);
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002009}
2010
Ville Syrjälä12b134d2013-07-05 11:57:21 +03002011static void intel_read_wm_latency(struct drm_device *dev, uint16_t wm[5])
2012{
2013 struct drm_i915_private *dev_priv = dev->dev_private;
2014
2015 if (IS_HASWELL(dev)) {
2016 uint64_t sskpd = I915_READ64(MCH_SSKPD);
2017
2018 wm[0] = (sskpd >> 56) & 0xFF;
2019 if (wm[0] == 0)
2020 wm[0] = sskpd & 0xF;
Ville Syrjäläe5d50192013-07-05 11:57:22 +03002021 wm[1] = (sskpd >> 4) & 0xFF;
2022 wm[2] = (sskpd >> 12) & 0xFF;
2023 wm[3] = (sskpd >> 20) & 0x1FF;
2024 wm[4] = (sskpd >> 32) & 0x1FF;
Ville Syrjälä63cf9a12013-07-05 11:57:23 +03002025 } else if (INTEL_INFO(dev)->gen >= 6) {
2026 uint32_t sskpd = I915_READ(MCH_SSKPD);
2027
2028 wm[0] = (sskpd >> SSKPD_WM0_SHIFT) & SSKPD_WM_MASK;
2029 wm[1] = (sskpd >> SSKPD_WM1_SHIFT) & SSKPD_WM_MASK;
2030 wm[2] = (sskpd >> SSKPD_WM2_SHIFT) & SSKPD_WM_MASK;
2031 wm[3] = (sskpd >> SSKPD_WM3_SHIFT) & SSKPD_WM_MASK;
Ville Syrjälä3a88d0a2013-08-01 16:18:49 +03002032 } else if (INTEL_INFO(dev)->gen >= 5) {
2033 uint32_t mltr = I915_READ(MLTR_ILK);
2034
2035 /* ILK primary LP0 latency is 700 ns */
2036 wm[0] = 7;
2037 wm[1] = (mltr >> MLTR_WM1_SHIFT) & ILK_SRLT_MASK;
2038 wm[2] = (mltr >> MLTR_WM2_SHIFT) & ILK_SRLT_MASK;
Ville Syrjälä12b134d2013-07-05 11:57:21 +03002039 }
2040}
2041
Ville Syrjälä53615a52013-08-01 16:18:50 +03002042static void intel_fixup_spr_wm_latency(struct drm_device *dev, uint16_t wm[5])
2043{
2044 /* ILK sprite LP0 latency is 1300 ns */
2045 if (INTEL_INFO(dev)->gen == 5)
2046 wm[0] = 13;
2047}
2048
2049static void intel_fixup_cur_wm_latency(struct drm_device *dev, uint16_t wm[5])
2050{
2051 /* ILK cursor LP0 latency is 1300 ns */
2052 if (INTEL_INFO(dev)->gen == 5)
2053 wm[0] = 13;
2054
2055 /* WaDoubleCursorLP3Latency:ivb */
2056 if (IS_IVYBRIDGE(dev))
2057 wm[3] *= 2;
2058}
2059
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03002060static int ilk_wm_max_level(const struct drm_device *dev)
2061{
2062 /* how many WM levels are we expecting */
2063 if (IS_HASWELL(dev))
2064 return 4;
2065 else if (INTEL_INFO(dev)->gen >= 6)
2066 return 3;
2067 else
2068 return 2;
2069}
2070
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002071static void intel_print_wm_latency(struct drm_device *dev,
2072 const char *name,
2073 const uint16_t wm[5])
2074{
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03002075 int level, max_level = ilk_wm_max_level(dev);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002076
2077 for (level = 0; level <= max_level; level++) {
2078 unsigned int latency = wm[level];
2079
2080 if (latency == 0) {
2081 DRM_ERROR("%s WM%d latency not provided\n",
2082 name, level);
2083 continue;
2084 }
2085
2086 /* WM1+ latency values in 0.5us units */
2087 if (level > 0)
2088 latency *= 5;
2089
2090 DRM_DEBUG_KMS("%s WM%d latency %u (%u.%u usec)\n",
2091 name, level, wm[level],
2092 latency / 10, latency % 10);
2093 }
2094}
2095
Ville Syrjälä53615a52013-08-01 16:18:50 +03002096static void intel_setup_wm_latency(struct drm_device *dev)
2097{
2098 struct drm_i915_private *dev_priv = dev->dev_private;
2099
2100 intel_read_wm_latency(dev, dev_priv->wm.pri_latency);
2101
2102 memcpy(dev_priv->wm.spr_latency, dev_priv->wm.pri_latency,
2103 sizeof(dev_priv->wm.pri_latency));
2104 memcpy(dev_priv->wm.cur_latency, dev_priv->wm.pri_latency,
2105 sizeof(dev_priv->wm.pri_latency));
2106
2107 intel_fixup_spr_wm_latency(dev, dev_priv->wm.spr_latency);
2108 intel_fixup_cur_wm_latency(dev, dev_priv->wm.cur_latency);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002109
2110 intel_print_wm_latency(dev, "Primary", dev_priv->wm.pri_latency);
2111 intel_print_wm_latency(dev, "Sprite", dev_priv->wm.spr_latency);
2112 intel_print_wm_latency(dev, "Cursor", dev_priv->wm.cur_latency);
Ville Syrjälä53615a52013-08-01 16:18:50 +03002113}
2114
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03002115static void hsw_compute_wm_parameters(struct drm_crtc *crtc,
2116 struct hsw_pipe_wm_parameters *p,
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03002117 struct intel_wm_config *config)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002118{
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03002119 struct drm_device *dev = crtc->dev;
2120 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2121 enum pipe pipe = intel_crtc->pipe;
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03002122 struct drm_plane *plane;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002123
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03002124 p->active = intel_crtc_active(crtc);
2125 if (p->active) {
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002126 p->pipe_htotal = intel_crtc->config.adjusted_mode.htotal;
Ville Syrjälä36587292013-07-05 11:57:16 +03002127 p->pixel_rate = ilk_pipe_pixel_rate(dev, crtc);
Ville Syrjäläc35426d2013-08-07 13:29:50 +03002128 p->pri.bytes_per_pixel = crtc->fb->bits_per_pixel / 8;
2129 p->cur.bytes_per_pixel = 4;
Ville Syrjälä37327ab2013-09-04 18:25:28 +03002130 p->pri.horiz_pixels = intel_crtc->config.pipe_src_w;
Ville Syrjäläc35426d2013-08-07 13:29:50 +03002131 p->cur.horiz_pixels = 64;
2132 /* TODO: for now, assume primary and cursor planes are always enabled. */
2133 p->pri.enabled = true;
2134 p->cur.enabled = true;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002135 }
2136
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03002137 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03002138 config->num_pipes_active += intel_crtc_active(crtc);
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03002139
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002140 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
2141 struct intel_plane *intel_plane = to_intel_plane(plane);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002142
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03002143 if (intel_plane->pipe == pipe)
2144 p->spr = intel_plane->wm;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002145
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03002146 config->sprites_enabled |= intel_plane->wm.enabled;
2147 config->sprites_scaled |= intel_plane->wm.scaled;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002148 }
2149}
2150
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002151/* Compute new watermarks for the pipe */
2152static bool intel_compute_pipe_wm(struct drm_crtc *crtc,
2153 const struct hsw_pipe_wm_parameters *params,
2154 struct intel_pipe_wm *pipe_wm)
2155{
2156 struct drm_device *dev = crtc->dev;
2157 struct drm_i915_private *dev_priv = dev->dev_private;
2158 int level, max_level = ilk_wm_max_level(dev);
2159 /* LP0 watermark maximums depend on this pipe alone */
2160 struct intel_wm_config config = {
2161 .num_pipes_active = 1,
2162 .sprites_enabled = params->spr.enabled,
2163 .sprites_scaled = params->spr.scaled,
2164 };
2165 struct hsw_wm_maximums max;
2166
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002167 /* LP0 watermarks always use 1/2 DDB partitioning */
Ville Syrjälä34982fe2013-10-09 19:18:09 +03002168 ilk_compute_wm_maximums(dev, 0, &config, INTEL_DDB_PART_1_2, &max);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002169
Ville Syrjälä7b39a0b2013-12-05 15:51:30 +02002170 /* ILK/SNB: LP2+ watermarks only w/o sprites */
2171 if (INTEL_INFO(dev)->gen <= 6 && params->spr.enabled)
2172 max_level = 1;
2173
2174 /* ILK/SNB/IVB: LP1+ watermarks only w/o scaling */
2175 if (params->spr.scaled)
2176 max_level = 0;
2177
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002178 for (level = 0; level <= max_level; level++)
2179 ilk_compute_wm_level(dev_priv, level, params,
2180 &pipe_wm->wm[level]);
2181
Ville Syrjäläce0e0712013-12-05 15:51:36 +02002182 if (IS_HASWELL(dev))
2183 pipe_wm->linetime = hsw_compute_linetime_wm(dev, crtc);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002184
2185 /* At least LP0 must be valid */
Ville Syrjäläd9395652013-10-09 19:18:10 +03002186 return ilk_validate_wm_level(0, &max, &pipe_wm->wm[0]);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002187}
2188
2189/*
2190 * Merge the watermarks from all active pipes for a specific level.
2191 */
2192static void ilk_merge_wm_level(struct drm_device *dev,
2193 int level,
2194 struct intel_wm_level *ret_wm)
2195{
2196 const struct intel_crtc *intel_crtc;
2197
2198 list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list, base.head) {
2199 const struct intel_wm_level *wm =
2200 &intel_crtc->wm.active.wm[level];
2201
2202 if (!wm->enable)
2203 return;
2204
2205 ret_wm->pri_val = max(ret_wm->pri_val, wm->pri_val);
2206 ret_wm->spr_val = max(ret_wm->spr_val, wm->spr_val);
2207 ret_wm->cur_val = max(ret_wm->cur_val, wm->cur_val);
2208 ret_wm->fbc_val = max(ret_wm->fbc_val, wm->fbc_val);
2209 }
2210
2211 ret_wm->enable = true;
2212}
2213
2214/*
2215 * Merge all low power watermarks for all active pipes.
2216 */
2217static void ilk_wm_merge(struct drm_device *dev,
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02002218 const struct intel_wm_config *config,
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002219 const struct hsw_wm_maximums *max,
2220 struct intel_pipe_wm *merged)
2221{
2222 int level, max_level = ilk_wm_max_level(dev);
2223
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02002224 /* ILK/SNB/IVB: LP1+ watermarks only w/ single pipe */
2225 if ((INTEL_INFO(dev)->gen <= 6 || IS_IVYBRIDGE(dev)) &&
2226 config->num_pipes_active > 1)
2227 return;
2228
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02002229 /* ILK: FBC WM must be disabled always */
2230 merged->fbc_wm_enabled = INTEL_INFO(dev)->gen >= 6;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002231
2232 /* merge each WM1+ level */
2233 for (level = 1; level <= max_level; level++) {
2234 struct intel_wm_level *wm = &merged->wm[level];
2235
2236 ilk_merge_wm_level(dev, level, wm);
2237
Ville Syrjäläd9395652013-10-09 19:18:10 +03002238 if (!ilk_validate_wm_level(level, max, wm))
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002239 break;
2240
2241 /*
2242 * The spec says it is preferred to disable
2243 * FBC WMs instead of disabling a WM level.
2244 */
2245 if (wm->fbc_val > max->fbc) {
2246 merged->fbc_wm_enabled = false;
2247 wm->fbc_val = 0;
2248 }
2249 }
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02002250
2251 /* ILK: LP2+ must be disabled when FBC WM is disabled but FBC enabled */
2252 /*
2253 * FIXME this is racy. FBC might get enabled later.
2254 * What we should check here is whether FBC can be
2255 * enabled sometime later.
2256 */
2257 if (IS_GEN5(dev) && !merged->fbc_wm_enabled && intel_fbc_enabled(dev)) {
2258 for (level = 2; level <= max_level; level++) {
2259 struct intel_wm_level *wm = &merged->wm[level];
2260
2261 wm->enable = false;
2262 }
2263 }
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002264}
2265
Ville Syrjäläb380ca32013-10-09 19:18:01 +03002266static int ilk_wm_lp_to_level(int wm_lp, const struct intel_pipe_wm *pipe_wm)
2267{
2268 /* LP1,LP2,LP3 levels are either 1,2,3 or 1,3,4 */
2269 return wm_lp + (wm_lp >= 2 && pipe_wm->wm[4].enable);
2270}
2271
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02002272/* The value we need to program into the WM_LPx latency field */
2273static unsigned int ilk_wm_lp_latency(struct drm_device *dev, int level)
2274{
2275 struct drm_i915_private *dev_priv = dev->dev_private;
2276
2277 if (IS_HASWELL(dev))
2278 return 2 * level;
2279 else
2280 return dev_priv->wm.pri_latency[level];
2281}
2282
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002283static void hsw_compute_wm_results(struct drm_device *dev,
Ville Syrjälä0362c782013-10-09 19:17:57 +03002284 const struct intel_pipe_wm *merged,
Ville Syrjälä609cede2013-10-09 19:18:03 +03002285 enum intel_ddb_partitioning partitioning,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002286 struct hsw_wm_values *results)
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002287{
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002288 struct intel_crtc *intel_crtc;
2289 int level, wm_lp;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002290
Ville Syrjälä0362c782013-10-09 19:17:57 +03002291 results->enable_fbc_wm = merged->fbc_wm_enabled;
Ville Syrjälä609cede2013-10-09 19:18:03 +03002292 results->partitioning = partitioning;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002293
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002294 /* LP1+ register values */
Paulo Zanonicca32e92013-05-31 11:45:06 -03002295 for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
Ville Syrjälä1fd527c2013-08-06 22:24:05 +03002296 const struct intel_wm_level *r;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002297
Ville Syrjäläb380ca32013-10-09 19:18:01 +03002298 level = ilk_wm_lp_to_level(wm_lp, merged);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002299
Ville Syrjälä0362c782013-10-09 19:17:57 +03002300 r = &merged->wm[level];
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002301 if (!r->enable)
Paulo Zanonicca32e92013-05-31 11:45:06 -03002302 break;
2303
Ville Syrjälä416f4722013-11-02 21:07:46 -07002304 results->wm_lp[wm_lp - 1] = WM3_LP_EN |
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02002305 (ilk_wm_lp_latency(dev, level) << WM1_LP_LATENCY_SHIFT) |
Ville Syrjälä416f4722013-11-02 21:07:46 -07002306 (r->pri_val << WM1_LP_SR_SHIFT) |
2307 r->cur_val;
2308
2309 if (INTEL_INFO(dev)->gen >= 8)
2310 results->wm_lp[wm_lp - 1] |=
2311 r->fbc_val << WM1_LP_FBC_SHIFT_BDW;
2312 else
2313 results->wm_lp[wm_lp - 1] |=
2314 r->fbc_val << WM1_LP_FBC_SHIFT;
2315
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02002316 if (INTEL_INFO(dev)->gen <= 6 && r->spr_val) {
2317 WARN_ON(wm_lp != 1);
2318 results->wm_lp_spr[wm_lp - 1] = WM1S_LP_EN | r->spr_val;
2319 } else
2320 results->wm_lp_spr[wm_lp - 1] = r->spr_val;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002321 }
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002322
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002323 /* LP0 register values */
2324 list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list, base.head) {
2325 enum pipe pipe = intel_crtc->pipe;
2326 const struct intel_wm_level *r =
2327 &intel_crtc->wm.active.wm[0];
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002328
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002329 if (WARN_ON(!r->enable))
2330 continue;
2331
2332 results->wm_linetime[pipe] = intel_crtc->wm.active.linetime;
2333
2334 results->wm_pipe[pipe] =
2335 (r->pri_val << WM0_PIPE_PLANE_SHIFT) |
2336 (r->spr_val << WM0_PIPE_SPRITE_SHIFT) |
2337 r->cur_val;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002338 }
2339}
2340
Paulo Zanoni861f3382013-05-31 10:19:21 -03002341/* Find the result with the highest level enabled. Check for enable_fbc_wm in
2342 * case both are at the same level. Prefer r1 in case they're the same. */
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002343static struct intel_pipe_wm *hsw_find_best_result(struct drm_device *dev,
2344 struct intel_pipe_wm *r1,
2345 struct intel_pipe_wm *r2)
Paulo Zanoni861f3382013-05-31 10:19:21 -03002346{
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002347 int level, max_level = ilk_wm_max_level(dev);
2348 int level1 = 0, level2 = 0;
Paulo Zanoni861f3382013-05-31 10:19:21 -03002349
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002350 for (level = 1; level <= max_level; level++) {
2351 if (r1->wm[level].enable)
2352 level1 = level;
2353 if (r2->wm[level].enable)
2354 level2 = level;
Paulo Zanoni861f3382013-05-31 10:19:21 -03002355 }
2356
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002357 if (level1 == level2) {
2358 if (r2->fbc_wm_enabled && !r1->fbc_wm_enabled)
Paulo Zanoni861f3382013-05-31 10:19:21 -03002359 return r2;
2360 else
2361 return r1;
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002362 } else if (level1 > level2) {
Paulo Zanoni861f3382013-05-31 10:19:21 -03002363 return r1;
2364 } else {
2365 return r2;
2366 }
2367}
2368
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002369/* dirty bits used to track which watermarks need changes */
2370#define WM_DIRTY_PIPE(pipe) (1 << (pipe))
2371#define WM_DIRTY_LINETIME(pipe) (1 << (8 + (pipe)))
2372#define WM_DIRTY_LP(wm_lp) (1 << (15 + (wm_lp)))
2373#define WM_DIRTY_LP_ALL (WM_DIRTY_LP(1) | WM_DIRTY_LP(2) | WM_DIRTY_LP(3))
2374#define WM_DIRTY_FBC (1 << 24)
2375#define WM_DIRTY_DDB (1 << 25)
2376
2377static unsigned int ilk_compute_wm_dirty(struct drm_device *dev,
2378 const struct hsw_wm_values *old,
2379 const struct hsw_wm_values *new)
2380{
2381 unsigned int dirty = 0;
2382 enum pipe pipe;
2383 int wm_lp;
2384
2385 for_each_pipe(pipe) {
2386 if (old->wm_linetime[pipe] != new->wm_linetime[pipe]) {
2387 dirty |= WM_DIRTY_LINETIME(pipe);
2388 /* Must disable LP1+ watermarks too */
2389 dirty |= WM_DIRTY_LP_ALL;
2390 }
2391
2392 if (old->wm_pipe[pipe] != new->wm_pipe[pipe]) {
2393 dirty |= WM_DIRTY_PIPE(pipe);
2394 /* Must disable LP1+ watermarks too */
2395 dirty |= WM_DIRTY_LP_ALL;
2396 }
2397 }
2398
2399 if (old->enable_fbc_wm != new->enable_fbc_wm) {
2400 dirty |= WM_DIRTY_FBC;
2401 /* Must disable LP1+ watermarks too */
2402 dirty |= WM_DIRTY_LP_ALL;
2403 }
2404
2405 if (old->partitioning != new->partitioning) {
2406 dirty |= WM_DIRTY_DDB;
2407 /* Must disable LP1+ watermarks too */
2408 dirty |= WM_DIRTY_LP_ALL;
2409 }
2410
2411 /* LP1+ watermarks already deemed dirty, no need to continue */
2412 if (dirty & WM_DIRTY_LP_ALL)
2413 return dirty;
2414
2415 /* Find the lowest numbered LP1+ watermark in need of an update... */
2416 for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
2417 if (old->wm_lp[wm_lp - 1] != new->wm_lp[wm_lp - 1] ||
2418 old->wm_lp_spr[wm_lp - 1] != new->wm_lp_spr[wm_lp - 1])
2419 break;
2420 }
2421
2422 /* ...and mark it and all higher numbered LP1+ watermarks as dirty */
2423 for (; wm_lp <= 3; wm_lp++)
2424 dirty |= WM_DIRTY_LP(wm_lp);
2425
2426 return dirty;
2427}
2428
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002429/*
2430 * The spec says we shouldn't write when we don't need, because every write
2431 * causes WMs to be re-evaluated, expending some power.
2432 */
2433static void hsw_write_wm_values(struct drm_i915_private *dev_priv,
Ville Syrjälä609cede2013-10-09 19:18:03 +03002434 struct hsw_wm_values *results)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002435{
Ville Syrjäläac9545f2013-12-05 15:51:28 +02002436 struct drm_device *dev = dev_priv->dev;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03002437 struct hsw_wm_values *previous = &dev_priv->wm.hw;
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002438 unsigned int dirty;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002439 uint32_t val;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002440
Ville Syrjälä243e6a42013-10-14 14:55:24 +03002441 dirty = ilk_compute_wm_dirty(dev_priv->dev, previous, results);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002442 if (!dirty)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002443 return;
2444
Ville Syrjäläfacd6192013-12-05 15:51:33 +02002445 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] & WM1_LP_SR_EN) {
2446 previous->wm_lp[2] &= ~WM1_LP_SR_EN;
2447 I915_WRITE(WM3_LP_ILK, previous->wm_lp[2]);
2448 }
2449 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] & WM1_LP_SR_EN) {
2450 previous->wm_lp[1] &= ~WM1_LP_SR_EN;
2451 I915_WRITE(WM2_LP_ILK, previous->wm_lp[1]);
2452 }
2453 if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] & WM1_LP_SR_EN) {
2454 previous->wm_lp[0] &= ~WM1_LP_SR_EN;
2455 I915_WRITE(WM1_LP_ILK, previous->wm_lp[0]);
2456 }
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002457
Ville Syrjäläfacd6192013-12-05 15:51:33 +02002458 /*
2459 * Don't touch WM1S_LP_EN here.
2460 * Doing so could cause underruns.
2461 */
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02002462
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002463 if (dirty & WM_DIRTY_PIPE(PIPE_A))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002464 I915_WRITE(WM0_PIPEA_ILK, results->wm_pipe[0]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002465 if (dirty & WM_DIRTY_PIPE(PIPE_B))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002466 I915_WRITE(WM0_PIPEB_ILK, results->wm_pipe[1]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002467 if (dirty & WM_DIRTY_PIPE(PIPE_C))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002468 I915_WRITE(WM0_PIPEC_IVB, results->wm_pipe[2]);
2469
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002470 if (dirty & WM_DIRTY_LINETIME(PIPE_A))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002471 I915_WRITE(PIPE_WM_LINETIME(PIPE_A), results->wm_linetime[0]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002472 if (dirty & WM_DIRTY_LINETIME(PIPE_B))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002473 I915_WRITE(PIPE_WM_LINETIME(PIPE_B), results->wm_linetime[1]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002474 if (dirty & WM_DIRTY_LINETIME(PIPE_C))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002475 I915_WRITE(PIPE_WM_LINETIME(PIPE_C), results->wm_linetime[2]);
2476
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002477 if (dirty & WM_DIRTY_DDB) {
Ville Syrjäläac9545f2013-12-05 15:51:28 +02002478 if (IS_HASWELL(dev)) {
2479 val = I915_READ(WM_MISC);
2480 if (results->partitioning == INTEL_DDB_PART_1_2)
2481 val &= ~WM_MISC_DATA_PARTITION_5_6;
2482 else
2483 val |= WM_MISC_DATA_PARTITION_5_6;
2484 I915_WRITE(WM_MISC, val);
2485 } else {
2486 val = I915_READ(DISP_ARB_CTL2);
2487 if (results->partitioning == INTEL_DDB_PART_1_2)
2488 val &= ~DISP_DATA_PARTITION_5_6;
2489 else
2490 val |= DISP_DATA_PARTITION_5_6;
2491 I915_WRITE(DISP_ARB_CTL2, val);
2492 }
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002493 }
2494
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002495 if (dirty & WM_DIRTY_FBC) {
Paulo Zanonicca32e92013-05-31 11:45:06 -03002496 val = I915_READ(DISP_ARB_CTL);
2497 if (results->enable_fbc_wm)
2498 val &= ~DISP_FBC_WM_DIS;
2499 else
2500 val |= DISP_FBC_WM_DIS;
2501 I915_WRITE(DISP_ARB_CTL, val);
2502 }
2503
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02002504 if (INTEL_INFO(dev)->gen <= 6) {
Ville Syrjäläfacd6192013-12-05 15:51:33 +02002505 if (dirty & WM_DIRTY_LP(1) && previous->wm_lp_spr[0] != results->wm_lp_spr[0])
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02002506 I915_WRITE(WM1S_LP_ILK, results->wm_lp_spr[0]);
2507 } else {
2508 if (dirty & WM_DIRTY_LP(1) && previous->wm_lp_spr[0] != results->wm_lp_spr[0])
2509 I915_WRITE(WM1S_LP_ILK, results->wm_lp_spr[0]);
2510 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp_spr[1] != results->wm_lp_spr[1])
2511 I915_WRITE(WM2S_LP_IVB, results->wm_lp_spr[1]);
2512 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp_spr[2] != results->wm_lp_spr[2])
2513 I915_WRITE(WM3S_LP_IVB, results->wm_lp_spr[2]);
2514 }
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002515
Ville Syrjäläfacd6192013-12-05 15:51:33 +02002516 if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] != results->wm_lp[0])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002517 I915_WRITE(WM1_LP_ILK, results->wm_lp[0]);
Ville Syrjäläfacd6192013-12-05 15:51:33 +02002518 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] != results->wm_lp[1])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002519 I915_WRITE(WM2_LP_ILK, results->wm_lp[1]);
Ville Syrjäläfacd6192013-12-05 15:51:33 +02002520 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] != results->wm_lp[2])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002521 I915_WRITE(WM3_LP_ILK, results->wm_lp[2]);
Ville Syrjälä609cede2013-10-09 19:18:03 +03002522
2523 dev_priv->wm.hw = *results;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002524}
2525
Ville Syrjälä46ba6142013-09-10 11:40:40 +03002526static void haswell_update_wm(struct drm_crtc *crtc)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002527{
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03002528 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjälä46ba6142013-09-10 11:40:40 +03002529 struct drm_device *dev = crtc->dev;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002530 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03002531 struct hsw_wm_maximums max;
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03002532 struct hsw_pipe_wm_parameters params = {};
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002533 struct hsw_wm_values results = {};
Ville Syrjälä77c122b2013-08-06 22:24:04 +03002534 enum intel_ddb_partitioning partitioning;
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03002535 struct intel_pipe_wm pipe_wm = {};
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002536 struct intel_pipe_wm lp_wm_1_2 = {}, lp_wm_5_6 = {}, *best_lp_wm;
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03002537 struct intel_wm_config config = {};
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002538
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03002539 hsw_compute_wm_parameters(crtc, &params, &config);
Paulo Zanoni861f3382013-05-31 10:19:21 -03002540
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03002541 intel_compute_pipe_wm(crtc, &params, &pipe_wm);
2542
2543 if (!memcmp(&intel_crtc->wm.active, &pipe_wm, sizeof(pipe_wm)))
2544 return;
2545
2546 intel_crtc->wm.active = pipe_wm;
2547
Ville Syrjälä34982fe2013-10-09 19:18:09 +03002548 ilk_compute_wm_maximums(dev, 1, &config, INTEL_DDB_PART_1_2, &max);
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02002549 ilk_wm_merge(dev, &config, &max, &lp_wm_1_2);
Ville Syrjälä0362c782013-10-09 19:17:57 +03002550
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03002551 /* 5/6 split only in single pipe config on IVB+ */
Ville Syrjäläec98c8d2013-10-11 15:26:26 +03002552 if (INTEL_INFO(dev)->gen >= 7 &&
2553 config.num_pipes_active == 1 && config.sprites_enabled) {
Ville Syrjälä34982fe2013-10-09 19:18:09 +03002554 ilk_compute_wm_maximums(dev, 1, &config, INTEL_DDB_PART_5_6, &max);
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02002555 ilk_wm_merge(dev, &config, &max, &lp_wm_5_6);
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03002556
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002557 best_lp_wm = hsw_find_best_result(dev, &lp_wm_1_2, &lp_wm_5_6);
Paulo Zanoni861f3382013-05-31 10:19:21 -03002558 } else {
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002559 best_lp_wm = &lp_wm_1_2;
Paulo Zanoni861f3382013-05-31 10:19:21 -03002560 }
2561
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002562 partitioning = (best_lp_wm == &lp_wm_1_2) ?
Ville Syrjälä77c122b2013-08-06 22:24:04 +03002563 INTEL_DDB_PART_1_2 : INTEL_DDB_PART_5_6;
Paulo Zanoni861f3382013-05-31 10:19:21 -03002564
Ville Syrjälä609cede2013-10-09 19:18:03 +03002565 hsw_compute_wm_results(dev, best_lp_wm, partitioning, &results);
2566
2567 hsw_write_wm_values(dev_priv, &results);
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002568}
2569
Ville Syrjäläadf3d352013-08-06 22:24:11 +03002570static void haswell_update_sprite_wm(struct drm_plane *plane,
2571 struct drm_crtc *crtc,
Paulo Zanoni526682e2013-05-24 11:59:18 -03002572 uint32_t sprite_width, int pixel_size,
Ville Syrjäläbdd57d02013-07-05 11:57:13 +03002573 bool enabled, bool scaled)
Paulo Zanoni526682e2013-05-24 11:59:18 -03002574{
Ville Syrjäläadf3d352013-08-06 22:24:11 +03002575 struct intel_plane *intel_plane = to_intel_plane(plane);
Paulo Zanoni526682e2013-05-24 11:59:18 -03002576
Ville Syrjäläadf3d352013-08-06 22:24:11 +03002577 intel_plane->wm.enabled = enabled;
2578 intel_plane->wm.scaled = scaled;
2579 intel_plane->wm.horiz_pixels = sprite_width;
2580 intel_plane->wm.bytes_per_pixel = pixel_size;
Paulo Zanoni526682e2013-05-24 11:59:18 -03002581
Ville Syrjälä46ba6142013-09-10 11:40:40 +03002582 haswell_update_wm(crtc);
Paulo Zanoni526682e2013-05-24 11:59:18 -03002583}
2584
Ville Syrjälä243e6a42013-10-14 14:55:24 +03002585static void ilk_pipe_wm_get_hw_state(struct drm_crtc *crtc)
2586{
2587 struct drm_device *dev = crtc->dev;
2588 struct drm_i915_private *dev_priv = dev->dev_private;
2589 struct hsw_wm_values *hw = &dev_priv->wm.hw;
2590 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2591 struct intel_pipe_wm *active = &intel_crtc->wm.active;
2592 enum pipe pipe = intel_crtc->pipe;
2593 static const unsigned int wm0_pipe_reg[] = {
2594 [PIPE_A] = WM0_PIPEA_ILK,
2595 [PIPE_B] = WM0_PIPEB_ILK,
2596 [PIPE_C] = WM0_PIPEC_IVB,
2597 };
2598
2599 hw->wm_pipe[pipe] = I915_READ(wm0_pipe_reg[pipe]);
Ville Syrjäläce0e0712013-12-05 15:51:36 +02002600 if (IS_HASWELL(dev))
2601 hw->wm_linetime[pipe] = I915_READ(PIPE_WM_LINETIME(pipe));
Ville Syrjälä243e6a42013-10-14 14:55:24 +03002602
2603 if (intel_crtc_active(crtc)) {
2604 u32 tmp = hw->wm_pipe[pipe];
2605
2606 /*
2607 * For active pipes LP0 watermark is marked as
2608 * enabled, and LP1+ watermaks as disabled since
2609 * we can't really reverse compute them in case
2610 * multiple pipes are active.
2611 */
2612 active->wm[0].enable = true;
2613 active->wm[0].pri_val = (tmp & WM0_PIPE_PLANE_MASK) >> WM0_PIPE_PLANE_SHIFT;
2614 active->wm[0].spr_val = (tmp & WM0_PIPE_SPRITE_MASK) >> WM0_PIPE_SPRITE_SHIFT;
2615 active->wm[0].cur_val = tmp & WM0_PIPE_CURSOR_MASK;
2616 active->linetime = hw->wm_linetime[pipe];
2617 } else {
2618 int level, max_level = ilk_wm_max_level(dev);
2619
2620 /*
2621 * For inactive pipes, all watermark levels
2622 * should be marked as enabled but zeroed,
2623 * which is what we'd compute them to.
2624 */
2625 for (level = 0; level <= max_level; level++)
2626 active->wm[level].enable = true;
2627 }
2628}
2629
2630void ilk_wm_get_hw_state(struct drm_device *dev)
2631{
2632 struct drm_i915_private *dev_priv = dev->dev_private;
2633 struct hsw_wm_values *hw = &dev_priv->wm.hw;
2634 struct drm_crtc *crtc;
2635
2636 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
2637 ilk_pipe_wm_get_hw_state(crtc);
2638
2639 hw->wm_lp[0] = I915_READ(WM1_LP_ILK);
2640 hw->wm_lp[1] = I915_READ(WM2_LP_ILK);
2641 hw->wm_lp[2] = I915_READ(WM3_LP_ILK);
2642
2643 hw->wm_lp_spr[0] = I915_READ(WM1S_LP_ILK);
2644 hw->wm_lp_spr[1] = I915_READ(WM2S_LP_IVB);
2645 hw->wm_lp_spr[2] = I915_READ(WM3S_LP_IVB);
2646
Ville Syrjäläac9545f2013-12-05 15:51:28 +02002647 if (IS_HASWELL(dev))
2648 hw->partitioning = (I915_READ(WM_MISC) & WM_MISC_DATA_PARTITION_5_6) ?
2649 INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
2650 else if (IS_IVYBRIDGE(dev))
2651 hw->partitioning = (I915_READ(DISP_ARB_CTL2) & DISP_DATA_PARTITION_5_6) ?
2652 INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03002653
2654 hw->enable_fbc_wm =
2655 !(I915_READ(DISP_ARB_CTL) & DISP_FBC_WM_DIS);
2656}
2657
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002658/**
2659 * intel_update_watermarks - update FIFO watermark values based on current modes
2660 *
2661 * Calculate watermark values for the various WM regs based on current mode
2662 * and plane configuration.
2663 *
2664 * There are several cases to deal with here:
2665 * - normal (i.e. non-self-refresh)
2666 * - self-refresh (SR) mode
2667 * - lines are large relative to FIFO size (buffer can hold up to 2)
2668 * - lines are small relative to FIFO size (buffer can hold more than 2
2669 * lines), so need to account for TLB latency
2670 *
2671 * The normal calculation is:
2672 * watermark = dotclock * bytes per pixel * latency
2673 * where latency is platform & configuration dependent (we assume pessimal
2674 * values here).
2675 *
2676 * The SR calculation is:
2677 * watermark = (trunc(latency/line time)+1) * surface width *
2678 * bytes per pixel
2679 * where
2680 * line time = htotal / dotclock
2681 * surface width = hdisplay for normal plane and 64 for cursor
2682 * and latency is assumed to be high, as above.
2683 *
2684 * The final value programmed to the register should always be rounded up,
2685 * and include an extra 2 entries to account for clock crossings.
2686 *
2687 * We don't use the sprite, so we can ignore that. And on Crestline we have
2688 * to set the non-SR watermarks to 8.
2689 */
Ville Syrjälä46ba6142013-09-10 11:40:40 +03002690void intel_update_watermarks(struct drm_crtc *crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002691{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03002692 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002693
2694 if (dev_priv->display.update_wm)
Ville Syrjälä46ba6142013-09-10 11:40:40 +03002695 dev_priv->display.update_wm(crtc);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002696}
2697
Ville Syrjäläadf3d352013-08-06 22:24:11 +03002698void intel_update_sprite_watermarks(struct drm_plane *plane,
2699 struct drm_crtc *crtc,
Paulo Zanoni4c4ff432013-05-24 11:59:17 -03002700 uint32_t sprite_width, int pixel_size,
Ville Syrjälä39db4a42013-08-06 22:24:00 +03002701 bool enabled, bool scaled)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002702{
Ville Syrjäläadf3d352013-08-06 22:24:11 +03002703 struct drm_i915_private *dev_priv = plane->dev->dev_private;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002704
2705 if (dev_priv->display.update_sprite_wm)
Ville Syrjäläadf3d352013-08-06 22:24:11 +03002706 dev_priv->display.update_sprite_wm(plane, crtc, sprite_width,
Ville Syrjälä39db4a42013-08-06 22:24:00 +03002707 pixel_size, enabled, scaled);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002708}
2709
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002710static struct drm_i915_gem_object *
2711intel_alloc_context_page(struct drm_device *dev)
2712{
2713 struct drm_i915_gem_object *ctx;
2714 int ret;
2715
2716 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2717
2718 ctx = i915_gem_alloc_object(dev, 4096);
2719 if (!ctx) {
2720 DRM_DEBUG("failed to alloc power context, RC6 disabled\n");
2721 return NULL;
2722 }
2723
Ben Widawskyc37e2202013-07-31 16:59:58 -07002724 ret = i915_gem_obj_ggtt_pin(ctx, 4096, true, false);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002725 if (ret) {
2726 DRM_ERROR("failed to pin power context: %d\n", ret);
2727 goto err_unref;
2728 }
2729
2730 ret = i915_gem_object_set_to_gtt_domain(ctx, 1);
2731 if (ret) {
2732 DRM_ERROR("failed to set-domain on power context: %d\n", ret);
2733 goto err_unpin;
2734 }
2735
2736 return ctx;
2737
2738err_unpin:
2739 i915_gem_object_unpin(ctx);
2740err_unref:
2741 drm_gem_object_unreference(&ctx->base);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002742 return NULL;
2743}
2744
Daniel Vetter92703882012-08-09 16:46:01 +02002745/**
2746 * Lock protecting IPS related data structures
Daniel Vetter92703882012-08-09 16:46:01 +02002747 */
2748DEFINE_SPINLOCK(mchdev_lock);
2749
2750/* Global for IPS driver to get at the current i915 device. Protected by
2751 * mchdev_lock. */
2752static struct drm_i915_private *i915_mch_dev;
2753
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002754bool ironlake_set_drps(struct drm_device *dev, u8 val)
2755{
2756 struct drm_i915_private *dev_priv = dev->dev_private;
2757 u16 rgvswctl;
2758
Daniel Vetter92703882012-08-09 16:46:01 +02002759 assert_spin_locked(&mchdev_lock);
2760
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002761 rgvswctl = I915_READ16(MEMSWCTL);
2762 if (rgvswctl & MEMCTL_CMD_STS) {
2763 DRM_DEBUG("gpu busy, RCS change rejected\n");
2764 return false; /* still busy with another command */
2765 }
2766
2767 rgvswctl = (MEMCTL_CMD_CHFREQ << MEMCTL_CMD_SHIFT) |
2768 (val << MEMCTL_FREQ_SHIFT) | MEMCTL_SFCAVM;
2769 I915_WRITE16(MEMSWCTL, rgvswctl);
2770 POSTING_READ16(MEMSWCTL);
2771
2772 rgvswctl |= MEMCTL_CMD_STS;
2773 I915_WRITE16(MEMSWCTL, rgvswctl);
2774
2775 return true;
2776}
2777
Daniel Vetter8090c6b2012-06-24 16:42:32 +02002778static void ironlake_enable_drps(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002779{
2780 struct drm_i915_private *dev_priv = dev->dev_private;
2781 u32 rgvmodectl = I915_READ(MEMMODECTL);
2782 u8 fmax, fmin, fstart, vstart;
2783
Daniel Vetter92703882012-08-09 16:46:01 +02002784 spin_lock_irq(&mchdev_lock);
2785
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002786 /* Enable temp reporting */
2787 I915_WRITE16(PMMISC, I915_READ(PMMISC) | MCPPCE_EN);
2788 I915_WRITE16(TSC1, I915_READ(TSC1) | TSE);
2789
2790 /* 100ms RC evaluation intervals */
2791 I915_WRITE(RCUPEI, 100000);
2792 I915_WRITE(RCDNEI, 100000);
2793
2794 /* Set max/min thresholds to 90ms and 80ms respectively */
2795 I915_WRITE(RCBMAXAVG, 90000);
2796 I915_WRITE(RCBMINAVG, 80000);
2797
2798 I915_WRITE(MEMIHYST, 1);
2799
2800 /* Set up min, max, and cur for interrupt handling */
2801 fmax = (rgvmodectl & MEMMODE_FMAX_MASK) >> MEMMODE_FMAX_SHIFT;
2802 fmin = (rgvmodectl & MEMMODE_FMIN_MASK);
2803 fstart = (rgvmodectl & MEMMODE_FSTART_MASK) >>
2804 MEMMODE_FSTART_SHIFT;
2805
2806 vstart = (I915_READ(PXVFREQ_BASE + (fstart * 4)) & PXVFREQ_PX_MASK) >>
2807 PXVFREQ_PX_SHIFT;
2808
Daniel Vetter20e4d402012-08-08 23:35:39 +02002809 dev_priv->ips.fmax = fmax; /* IPS callback will increase this */
2810 dev_priv->ips.fstart = fstart;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002811
Daniel Vetter20e4d402012-08-08 23:35:39 +02002812 dev_priv->ips.max_delay = fstart;
2813 dev_priv->ips.min_delay = fmin;
2814 dev_priv->ips.cur_delay = fstart;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002815
2816 DRM_DEBUG_DRIVER("fmax: %d, fmin: %d, fstart: %d\n",
2817 fmax, fmin, fstart);
2818
2819 I915_WRITE(MEMINTREN, MEMINT_CX_SUPR_EN | MEMINT_EVAL_CHG_EN);
2820
2821 /*
2822 * Interrupts will be enabled in ironlake_irq_postinstall
2823 */
2824
2825 I915_WRITE(VIDSTART, vstart);
2826 POSTING_READ(VIDSTART);
2827
2828 rgvmodectl |= MEMMODE_SWMODE_EN;
2829 I915_WRITE(MEMMODECTL, rgvmodectl);
2830
Daniel Vetter92703882012-08-09 16:46:01 +02002831 if (wait_for_atomic((I915_READ(MEMSWCTL) & MEMCTL_CMD_STS) == 0, 10))
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002832 DRM_ERROR("stuck trying to change perf mode\n");
Daniel Vetter92703882012-08-09 16:46:01 +02002833 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002834
2835 ironlake_set_drps(dev, fstart);
2836
Daniel Vetter20e4d402012-08-08 23:35:39 +02002837 dev_priv->ips.last_count1 = I915_READ(0x112e4) + I915_READ(0x112e8) +
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002838 I915_READ(0x112e0);
Daniel Vetter20e4d402012-08-08 23:35:39 +02002839 dev_priv->ips.last_time1 = jiffies_to_msecs(jiffies);
2840 dev_priv->ips.last_count2 = I915_READ(0x112f4);
2841 getrawmonotonic(&dev_priv->ips.last_time2);
Daniel Vetter92703882012-08-09 16:46:01 +02002842
2843 spin_unlock_irq(&mchdev_lock);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002844}
2845
Daniel Vetter8090c6b2012-06-24 16:42:32 +02002846static void ironlake_disable_drps(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002847{
2848 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter92703882012-08-09 16:46:01 +02002849 u16 rgvswctl;
2850
2851 spin_lock_irq(&mchdev_lock);
2852
2853 rgvswctl = I915_READ16(MEMSWCTL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002854
2855 /* Ack interrupts, disable EFC interrupt */
2856 I915_WRITE(MEMINTREN, I915_READ(MEMINTREN) & ~MEMINT_EVAL_CHG_EN);
2857 I915_WRITE(MEMINTRSTS, MEMINT_EVAL_CHG);
2858 I915_WRITE(DEIER, I915_READ(DEIER) & ~DE_PCU_EVENT);
2859 I915_WRITE(DEIIR, DE_PCU_EVENT);
2860 I915_WRITE(DEIMR, I915_READ(DEIMR) | DE_PCU_EVENT);
2861
2862 /* Go back to the starting frequency */
Daniel Vetter20e4d402012-08-08 23:35:39 +02002863 ironlake_set_drps(dev, dev_priv->ips.fstart);
Daniel Vetter92703882012-08-09 16:46:01 +02002864 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002865 rgvswctl |= MEMCTL_CMD_STS;
2866 I915_WRITE(MEMSWCTL, rgvswctl);
Daniel Vetter92703882012-08-09 16:46:01 +02002867 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002868
Daniel Vetter92703882012-08-09 16:46:01 +02002869 spin_unlock_irq(&mchdev_lock);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002870}
2871
Daniel Vetteracbe9472012-07-26 11:50:05 +02002872/* There's a funny hw issue where the hw returns all 0 when reading from
2873 * GEN6_RP_INTERRUPT_LIMITS. Hence we always need to compute the desired value
2874 * ourselves, instead of doing a rmw cycle (which might result in us clearing
2875 * all limits and the gpu stuck at whatever frequency it is at atm).
2876 */
Chris Wilson6917c7b2013-11-06 13:56:26 -02002877static u32 gen6_rps_limits(struct drm_i915_private *dev_priv, u8 val)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002878{
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01002879 u32 limits;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002880
Daniel Vetter20b46e52012-07-26 11:16:14 +02002881 /* Only set the down limit when we've reached the lowest level to avoid
2882 * getting more interrupts, otherwise leave this clear. This prevents a
2883 * race in the hw when coming out of rc6: There's a tiny window where
2884 * the hw runs at the minimal clock before selecting the desired
2885 * frequency, if the down threshold expires in that window we will not
2886 * receive a down interrupt. */
Chris Wilson6917c7b2013-11-06 13:56:26 -02002887 limits = dev_priv->rps.max_delay << 24;
2888 if (val <= dev_priv->rps.min_delay)
Daniel Vetterc6a828d2012-08-08 23:35:35 +02002889 limits |= dev_priv->rps.min_delay << 16;
Daniel Vetter20b46e52012-07-26 11:16:14 +02002890
2891 return limits;
2892}
2893
Chris Wilsondd75fdc2013-09-25 17:34:57 +01002894static void gen6_set_rps_thresholds(struct drm_i915_private *dev_priv, u8 val)
2895{
2896 int new_power;
2897
2898 new_power = dev_priv->rps.power;
2899 switch (dev_priv->rps.power) {
2900 case LOW_POWER:
2901 if (val > dev_priv->rps.rpe_delay + 1 && val > dev_priv->rps.cur_delay)
2902 new_power = BETWEEN;
2903 break;
2904
2905 case BETWEEN:
2906 if (val <= dev_priv->rps.rpe_delay && val < dev_priv->rps.cur_delay)
2907 new_power = LOW_POWER;
2908 else if (val >= dev_priv->rps.rp0_delay && val > dev_priv->rps.cur_delay)
2909 new_power = HIGH_POWER;
2910 break;
2911
2912 case HIGH_POWER:
2913 if (val < (dev_priv->rps.rp1_delay + dev_priv->rps.rp0_delay) >> 1 && val < dev_priv->rps.cur_delay)
2914 new_power = BETWEEN;
2915 break;
2916 }
2917 /* Max/min bins are special */
2918 if (val == dev_priv->rps.min_delay)
2919 new_power = LOW_POWER;
2920 if (val == dev_priv->rps.max_delay)
2921 new_power = HIGH_POWER;
2922 if (new_power == dev_priv->rps.power)
2923 return;
2924
2925 /* Note the units here are not exactly 1us, but 1280ns. */
2926 switch (new_power) {
2927 case LOW_POWER:
2928 /* Upclock if more than 95% busy over 16ms */
2929 I915_WRITE(GEN6_RP_UP_EI, 12500);
2930 I915_WRITE(GEN6_RP_UP_THRESHOLD, 11800);
2931
2932 /* Downclock if less than 85% busy over 32ms */
2933 I915_WRITE(GEN6_RP_DOWN_EI, 25000);
2934 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 21250);
2935
2936 I915_WRITE(GEN6_RP_CONTROL,
2937 GEN6_RP_MEDIA_TURBO |
2938 GEN6_RP_MEDIA_HW_NORMAL_MODE |
2939 GEN6_RP_MEDIA_IS_GFX |
2940 GEN6_RP_ENABLE |
2941 GEN6_RP_UP_BUSY_AVG |
2942 GEN6_RP_DOWN_IDLE_AVG);
2943 break;
2944
2945 case BETWEEN:
2946 /* Upclock if more than 90% busy over 13ms */
2947 I915_WRITE(GEN6_RP_UP_EI, 10250);
2948 I915_WRITE(GEN6_RP_UP_THRESHOLD, 9225);
2949
2950 /* Downclock if less than 75% busy over 32ms */
2951 I915_WRITE(GEN6_RP_DOWN_EI, 25000);
2952 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 18750);
2953
2954 I915_WRITE(GEN6_RP_CONTROL,
2955 GEN6_RP_MEDIA_TURBO |
2956 GEN6_RP_MEDIA_HW_NORMAL_MODE |
2957 GEN6_RP_MEDIA_IS_GFX |
2958 GEN6_RP_ENABLE |
2959 GEN6_RP_UP_BUSY_AVG |
2960 GEN6_RP_DOWN_IDLE_AVG);
2961 break;
2962
2963 case HIGH_POWER:
2964 /* Upclock if more than 85% busy over 10ms */
2965 I915_WRITE(GEN6_RP_UP_EI, 8000);
2966 I915_WRITE(GEN6_RP_UP_THRESHOLD, 6800);
2967
2968 /* Downclock if less than 60% busy over 32ms */
2969 I915_WRITE(GEN6_RP_DOWN_EI, 25000);
2970 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 15000);
2971
2972 I915_WRITE(GEN6_RP_CONTROL,
2973 GEN6_RP_MEDIA_TURBO |
2974 GEN6_RP_MEDIA_HW_NORMAL_MODE |
2975 GEN6_RP_MEDIA_IS_GFX |
2976 GEN6_RP_ENABLE |
2977 GEN6_RP_UP_BUSY_AVG |
2978 GEN6_RP_DOWN_IDLE_AVG);
2979 break;
2980 }
2981
2982 dev_priv->rps.power = new_power;
2983 dev_priv->rps.last_adj = 0;
2984}
2985
Daniel Vetter20b46e52012-07-26 11:16:14 +02002986void gen6_set_rps(struct drm_device *dev, u8 val)
2987{
2988 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01002989
Jesse Barnes4fc688c2012-11-02 11:14:01 -07002990 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Ben Widawsky79249632012-09-07 19:43:42 -07002991 WARN_ON(val > dev_priv->rps.max_delay);
2992 WARN_ON(val < dev_priv->rps.min_delay);
Daniel Vetter004777c2012-08-09 15:07:01 +02002993
Daniel Vetterc6a828d2012-08-08 23:35:35 +02002994 if (val == dev_priv->rps.cur_delay)
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01002995 return;
2996
Chris Wilsondd75fdc2013-09-25 17:34:57 +01002997 gen6_set_rps_thresholds(dev_priv, val);
2998
Rodrigo Vivi92bd1bf2013-03-25 17:55:49 -03002999 if (IS_HASWELL(dev))
3000 I915_WRITE(GEN6_RPNSWREQ,
3001 HSW_FREQUENCY(val));
3002 else
3003 I915_WRITE(GEN6_RPNSWREQ,
3004 GEN6_FREQUENCY(val) |
3005 GEN6_OFFSET(0) |
3006 GEN6_AGGRESSIVE_TURBO);
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01003007
3008 /* Make sure we continue to get interrupts
3009 * until we hit the minimum or maximum frequencies.
3010 */
Chris Wilson6917c7b2013-11-06 13:56:26 -02003011 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS,
3012 gen6_rps_limits(dev_priv, val));
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01003013
Ben Widawskyd5570a72012-09-07 19:43:41 -07003014 POSTING_READ(GEN6_RPNSWREQ);
3015
Daniel Vetterc6a828d2012-08-08 23:35:35 +02003016 dev_priv->rps.cur_delay = val;
Daniel Vetterbe2cde92012-08-30 13:26:48 +02003017
3018 trace_intel_gpu_freq_change(val * 50);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003019}
3020
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003021void gen6_rps_idle(struct drm_i915_private *dev_priv)
3022{
Damien Lespiau691bb712013-12-12 14:36:36 +00003023 struct drm_device *dev = dev_priv->dev;
3024
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003025 mutex_lock(&dev_priv->rps.hw_lock);
Chris Wilsonc0951f02013-10-10 21:58:50 +01003026 if (dev_priv->rps.enabled) {
Damien Lespiau691bb712013-12-12 14:36:36 +00003027 if (IS_VALLEYVIEW(dev))
Chris Wilsonc0951f02013-10-10 21:58:50 +01003028 valleyview_set_rps(dev_priv->dev, dev_priv->rps.min_delay);
3029 else
3030 gen6_set_rps(dev_priv->dev, dev_priv->rps.min_delay);
3031 dev_priv->rps.last_adj = 0;
3032 }
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003033 mutex_unlock(&dev_priv->rps.hw_lock);
3034}
3035
3036void gen6_rps_boost(struct drm_i915_private *dev_priv)
3037{
Damien Lespiau691bb712013-12-12 14:36:36 +00003038 struct drm_device *dev = dev_priv->dev;
3039
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003040 mutex_lock(&dev_priv->rps.hw_lock);
Chris Wilsonc0951f02013-10-10 21:58:50 +01003041 if (dev_priv->rps.enabled) {
Damien Lespiau691bb712013-12-12 14:36:36 +00003042 if (IS_VALLEYVIEW(dev))
Chris Wilsonc0951f02013-10-10 21:58:50 +01003043 valleyview_set_rps(dev_priv->dev, dev_priv->rps.max_delay);
3044 else
3045 gen6_set_rps(dev_priv->dev, dev_priv->rps.max_delay);
3046 dev_priv->rps.last_adj = 0;
3047 }
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003048 mutex_unlock(&dev_priv->rps.hw_lock);
3049}
3050
Jesse Barnes0a073b82013-04-17 15:54:58 -07003051void valleyview_set_rps(struct drm_device *dev, u8 val)
3052{
3053 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä7a670922013-06-25 19:21:06 +03003054
Jesse Barnes0a073b82013-04-17 15:54:58 -07003055 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
3056 WARN_ON(val > dev_priv->rps.max_delay);
3057 WARN_ON(val < dev_priv->rps.min_delay);
3058
Ville Syrjälä73008b92013-06-25 19:21:01 +03003059 DRM_DEBUG_DRIVER("GPU freq request from %d MHz (%u) to %d MHz (%u)\n",
Ville Syrjälä2ec38152013-11-05 22:42:29 +02003060 vlv_gpu_freq(dev_priv, dev_priv->rps.cur_delay),
Ville Syrjälä73008b92013-06-25 19:21:01 +03003061 dev_priv->rps.cur_delay,
Ville Syrjälä2ec38152013-11-05 22:42:29 +02003062 vlv_gpu_freq(dev_priv, val), val);
Jesse Barnes0a073b82013-04-17 15:54:58 -07003063
3064 if (val == dev_priv->rps.cur_delay)
3065 return;
3066
Jani Nikulaae992582013-05-22 15:36:19 +03003067 vlv_punit_write(dev_priv, PUNIT_REG_GPU_FREQ_REQ, val);
Jesse Barnes0a073b82013-04-17 15:54:58 -07003068
Ville Syrjälä80814ae2013-06-25 19:21:02 +03003069 dev_priv->rps.cur_delay = val;
Jesse Barnes0a073b82013-04-17 15:54:58 -07003070
Ville Syrjälä2ec38152013-11-05 22:42:29 +02003071 trace_intel_gpu_freq_change(vlv_gpu_freq(dev_priv, val));
Jesse Barnes0a073b82013-04-17 15:54:58 -07003072}
3073
Daniel Vetter44fc7d52013-07-12 22:43:27 +02003074static void gen6_disable_rps_interrupts(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003075{
3076 struct drm_i915_private *dev_priv = dev->dev_private;
3077
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003078 I915_WRITE(GEN6_PMINTRMSK, 0xffffffff);
Ben Widawsky48484052013-05-28 19:22:27 -07003079 I915_WRITE(GEN6_PMIER, I915_READ(GEN6_PMIER) & ~GEN6_PM_RPS_EVENTS);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003080 /* Complete PM interrupt masking here doesn't race with the rps work
3081 * item again unmasking PM interrupts because that is using a different
3082 * register (PMIMR) to mask PM interrupts. The only risk is in leaving
3083 * stale bits in PMIIR and PMIMR which gen6_enable_rps will clean up. */
3084
Daniel Vetter59cdb632013-07-04 23:35:28 +02003085 spin_lock_irq(&dev_priv->irq_lock);
Daniel Vetterc6a828d2012-08-08 23:35:35 +02003086 dev_priv->rps.pm_iir = 0;
Daniel Vetter59cdb632013-07-04 23:35:28 +02003087 spin_unlock_irq(&dev_priv->irq_lock);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003088
Ben Widawsky48484052013-05-28 19:22:27 -07003089 I915_WRITE(GEN6_PMIIR, GEN6_PM_RPS_EVENTS);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003090}
3091
Daniel Vetter44fc7d52013-07-12 22:43:27 +02003092static void gen6_disable_rps(struct drm_device *dev)
3093{
3094 struct drm_i915_private *dev_priv = dev->dev_private;
3095
3096 I915_WRITE(GEN6_RC_CONTROL, 0);
3097 I915_WRITE(GEN6_RPNSWREQ, 1 << 31);
3098
3099 gen6_disable_rps_interrupts(dev);
3100}
3101
Jesse Barnesd20d4f02013-04-23 10:09:28 -07003102static void valleyview_disable_rps(struct drm_device *dev)
3103{
3104 struct drm_i915_private *dev_priv = dev->dev_private;
3105
3106 I915_WRITE(GEN6_RC_CONTROL, 0);
Jesse Barnesd20d4f02013-04-23 10:09:28 -07003107
Daniel Vetter44fc7d52013-07-12 22:43:27 +02003108 gen6_disable_rps_interrupts(dev);
Jesse Barnesc9cddff2013-05-08 10:45:13 -07003109
3110 if (dev_priv->vlv_pctx) {
3111 drm_gem_object_unreference(&dev_priv->vlv_pctx->base);
3112 dev_priv->vlv_pctx = NULL;
3113 }
Jesse Barnesd20d4f02013-04-23 10:09:28 -07003114}
3115
Ben Widawskydc39fff2013-10-18 12:32:07 -07003116static void intel_print_rc6_info(struct drm_device *dev, u32 mode)
3117{
3118 if (IS_GEN6(dev))
3119 DRM_DEBUG_DRIVER("Sandybridge: deep RC6 disabled\n");
3120
3121 if (IS_HASWELL(dev))
3122 DRM_DEBUG_DRIVER("Haswell: only RC6 available\n");
3123
3124 DRM_INFO("Enabling RC6 states: RC6 %s, RC6p %s, RC6pp %s\n",
3125 (mode & GEN6_RC_CTL_RC6_ENABLE) ? "on" : "off",
3126 (mode & GEN6_RC_CTL_RC6p_ENABLE) ? "on" : "off",
3127 (mode & GEN6_RC_CTL_RC6pp_ENABLE) ? "on" : "off");
3128}
3129
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003130int intel_enable_rc6(const struct drm_device *dev)
3131{
Damien Lespiaueb4926e2013-06-07 17:41:14 +01003132 /* No RC6 before Ironlake */
3133 if (INTEL_INFO(dev)->gen < 5)
3134 return 0;
3135
Daniel Vetter456470e2012-08-08 23:35:40 +02003136 /* Respect the kernel parameter if it is set */
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003137 if (i915_enable_rc6 >= 0)
3138 return i915_enable_rc6;
3139
Chris Wilson6567d742012-11-10 10:00:06 +00003140 /* Disable RC6 on Ironlake */
3141 if (INTEL_INFO(dev)->gen == 5)
3142 return 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003143
Ben Widawskydc39fff2013-10-18 12:32:07 -07003144 if (IS_HASWELL(dev))
Daniel Vetter456470e2012-08-08 23:35:40 +02003145 return INTEL_RC6_ENABLE;
Daniel Vetter456470e2012-08-08 23:35:40 +02003146
3147 /* snb/ivb have more than one rc6 state. */
Ben Widawskydc39fff2013-10-18 12:32:07 -07003148 if (INTEL_INFO(dev)->gen == 6)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003149 return INTEL_RC6_ENABLE;
Daniel Vetter456470e2012-08-08 23:35:40 +02003150
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003151 return (INTEL_RC6_ENABLE | INTEL_RC6p_ENABLE);
3152}
3153
Daniel Vetter44fc7d52013-07-12 22:43:27 +02003154static void gen6_enable_rps_interrupts(struct drm_device *dev)
3155{
3156 struct drm_i915_private *dev_priv = dev->dev_private;
Mika Kuoppalaa9c1f902013-08-22 21:09:00 +03003157 u32 enabled_intrs;
Daniel Vetter44fc7d52013-07-12 22:43:27 +02003158
3159 spin_lock_irq(&dev_priv->irq_lock);
Daniel Vettera0b33352013-07-04 23:35:34 +02003160 WARN_ON(dev_priv->rps.pm_iir);
Paulo Zanoniedbfdb42013-08-06 18:57:13 -03003161 snb_enable_pm_irq(dev_priv, GEN6_PM_RPS_EVENTS);
Daniel Vetter44fc7d52013-07-12 22:43:27 +02003162 I915_WRITE(GEN6_PMIIR, GEN6_PM_RPS_EVENTS);
3163 spin_unlock_irq(&dev_priv->irq_lock);
Mika Kuoppalaa9c1f902013-08-22 21:09:00 +03003164
Vinit Azadfd547d22013-08-14 13:34:33 -07003165 /* only unmask PM interrupts we need. Mask all others. */
Mika Kuoppalaa9c1f902013-08-22 21:09:00 +03003166 enabled_intrs = GEN6_PM_RPS_EVENTS;
3167
3168 /* IVB and SNB hard hangs on looping batchbuffer
3169 * if GEN6_PM_UP_EI_EXPIRED is masked.
3170 */
3171 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
3172 enabled_intrs |= GEN6_PM_RP_UP_EI_EXPIRED;
3173
3174 I915_WRITE(GEN6_PMINTRMSK, ~enabled_intrs);
Daniel Vetter44fc7d52013-07-12 22:43:27 +02003175}
3176
Ben Widawsky6edee7f2013-11-02 21:07:52 -07003177static void gen8_enable_rps(struct drm_device *dev)
3178{
3179 struct drm_i915_private *dev_priv = dev->dev_private;
3180 struct intel_ring_buffer *ring;
3181 uint32_t rc6_mask = 0, rp_state_cap;
3182 int unused;
3183
3184 /* 1a: Software RC state - RC0 */
3185 I915_WRITE(GEN6_RC_STATE, 0);
3186
3187 /* 1c & 1d: Get forcewake during program sequence. Although the driver
3188 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
Deepak Sc8d9a592013-11-23 14:55:42 +05303189 gen6_gt_force_wake_get(dev_priv, FORCEWAKE_ALL);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07003190
3191 /* 2a: Disable RC states. */
3192 I915_WRITE(GEN6_RC_CONTROL, 0);
3193
3194 rp_state_cap = I915_READ(GEN6_RP_STATE_CAP);
3195
3196 /* 2b: Program RC6 thresholds.*/
3197 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16);
3198 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
3199 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
3200 for_each_ring(ring, dev_priv, unused)
3201 I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
3202 I915_WRITE(GEN6_RC_SLEEP, 0);
3203 I915_WRITE(GEN6_RC6_THRESHOLD, 50000); /* 50/125ms per EI */
3204
3205 /* 3: Enable RC6 */
3206 if (intel_enable_rc6(dev) & INTEL_RC6_ENABLE)
3207 rc6_mask = GEN6_RC_CTL_RC6_ENABLE;
3208 DRM_INFO("RC6 %s\n", (rc6_mask & GEN6_RC_CTL_RC6_ENABLE) ? "on" : "off");
3209 I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
3210 GEN6_RC_CTL_EI_MODE(1) |
3211 rc6_mask);
3212
3213 /* 4 Program defaults and thresholds for RPS*/
3214 I915_WRITE(GEN6_RPNSWREQ, HSW_FREQUENCY(10)); /* Request 500 MHz */
3215 I915_WRITE(GEN6_RC_VIDEO_FREQ, HSW_FREQUENCY(12)); /* Request 600 MHz */
3216 /* NB: Docs say 1s, and 1000000 - which aren't equivalent */
3217 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 100000000 / 128); /* 1 second timeout */
3218
3219 /* Docs recommend 900MHz, and 300 MHz respectively */
3220 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS,
3221 dev_priv->rps.max_delay << 24 |
3222 dev_priv->rps.min_delay << 16);
3223
3224 I915_WRITE(GEN6_RP_UP_THRESHOLD, 7600000 / 128); /* 76ms busyness per EI, 90% */
3225 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 31300000 / 128); /* 313ms busyness per EI, 70%*/
3226 I915_WRITE(GEN6_RP_UP_EI, 66000); /* 84.48ms, XXX: random? */
3227 I915_WRITE(GEN6_RP_DOWN_EI, 350000); /* 448ms, XXX: random? */
3228
3229 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
3230
3231 /* 5: Enable RPS */
3232 I915_WRITE(GEN6_RP_CONTROL,
3233 GEN6_RP_MEDIA_TURBO |
3234 GEN6_RP_MEDIA_HW_NORMAL_MODE |
3235 GEN6_RP_MEDIA_IS_GFX |
3236 GEN6_RP_ENABLE |
3237 GEN6_RP_UP_BUSY_AVG |
3238 GEN6_RP_DOWN_IDLE_AVG);
3239
3240 /* 6: Ring frequency + overclocking (our driver does this later */
3241
3242 gen6_set_rps(dev, (I915_READ(GEN6_GT_PERF_STATUS) & 0xff00) >> 8);
3243
3244 gen6_enable_rps_interrupts(dev);
3245
Deepak Sc8d9a592013-11-23 14:55:42 +05303246 gen6_gt_force_wake_put(dev_priv, FORCEWAKE_ALL);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07003247}
3248
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02003249static void gen6_enable_rps(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003250{
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02003251 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonb4519512012-05-11 14:29:30 +01003252 struct intel_ring_buffer *ring;
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01003253 u32 rp_state_cap;
3254 u32 gt_perf_status;
Ben Widawsky31643d52012-09-26 10:34:01 -07003255 u32 rc6vids, pcu_mbox, rc6_mask = 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003256 u32 gtfifodbg;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003257 int rc6_mode;
Ben Widawsky42c05262012-09-26 10:34:00 -07003258 int i, ret;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003259
Jesse Barnes4fc688c2012-11-02 11:14:01 -07003260 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02003261
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003262 /* Here begins a magic sequence of register writes to enable
3263 * auto-downclocking.
3264 *
3265 * Perhaps there might be some value in exposing these to
3266 * userspace...
3267 */
3268 I915_WRITE(GEN6_RC_STATE, 0);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003269
3270 /* Clear the DBG now so we don't confuse earlier errors */
3271 if ((gtfifodbg = I915_READ(GTFIFODBG))) {
3272 DRM_ERROR("GT fifo had a previous error %x\n", gtfifodbg);
3273 I915_WRITE(GTFIFODBG, gtfifodbg);
3274 }
3275
Deepak Sc8d9a592013-11-23 14:55:42 +05303276 gen6_gt_force_wake_get(dev_priv, FORCEWAKE_ALL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003277
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01003278 rp_state_cap = I915_READ(GEN6_RP_STATE_CAP);
3279 gt_perf_status = I915_READ(GEN6_GT_PERF_STATUS);
3280
Ben Widawsky31c77382013-04-05 14:29:22 -07003281 /* In units of 50MHz */
3282 dev_priv->rps.hw_max = dev_priv->rps.max_delay = rp_state_cap & 0xff;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01003283 dev_priv->rps.min_delay = (rp_state_cap >> 16) & 0xff;
3284 dev_priv->rps.rp1_delay = (rp_state_cap >> 8) & 0xff;
3285 dev_priv->rps.rp0_delay = (rp_state_cap >> 0) & 0xff;
3286 dev_priv->rps.rpe_delay = dev_priv->rps.rp1_delay;
Daniel Vetterc6a828d2012-08-08 23:35:35 +02003287 dev_priv->rps.cur_delay = 0;
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01003288
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003289 /* disable the counters and set deterministic thresholds */
3290 I915_WRITE(GEN6_RC_CONTROL, 0);
3291
3292 I915_WRITE(GEN6_RC1_WAKE_RATE_LIMIT, 1000 << 16);
3293 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16 | 30);
3294 I915_WRITE(GEN6_RC6pp_WAKE_RATE_LIMIT, 30);
3295 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
3296 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
3297
Chris Wilsonb4519512012-05-11 14:29:30 +01003298 for_each_ring(ring, dev_priv, i)
3299 I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003300
3301 I915_WRITE(GEN6_RC_SLEEP, 0);
3302 I915_WRITE(GEN6_RC1e_THRESHOLD, 1000);
Daniel Vetter29c78f62013-11-16 16:04:26 +01003303 if (IS_IVYBRIDGE(dev))
Stéphane Marchesin351aa562013-08-13 11:55:17 -07003304 I915_WRITE(GEN6_RC6_THRESHOLD, 125000);
3305 else
3306 I915_WRITE(GEN6_RC6_THRESHOLD, 50000);
Stéphane Marchesin0920a482013-01-29 19:41:59 -08003307 I915_WRITE(GEN6_RC6p_THRESHOLD, 150000);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003308 I915_WRITE(GEN6_RC6pp_THRESHOLD, 64000); /* unused */
3309
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03003310 /* Check if we are enabling RC6 */
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003311 rc6_mode = intel_enable_rc6(dev_priv->dev);
3312 if (rc6_mode & INTEL_RC6_ENABLE)
3313 rc6_mask |= GEN6_RC_CTL_RC6_ENABLE;
3314
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03003315 /* We don't use those on Haswell */
3316 if (!IS_HASWELL(dev)) {
3317 if (rc6_mode & INTEL_RC6p_ENABLE)
3318 rc6_mask |= GEN6_RC_CTL_RC6p_ENABLE;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003319
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03003320 if (rc6_mode & INTEL_RC6pp_ENABLE)
3321 rc6_mask |= GEN6_RC_CTL_RC6pp_ENABLE;
3322 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003323
Ben Widawskydc39fff2013-10-18 12:32:07 -07003324 intel_print_rc6_info(dev, rc6_mask);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003325
3326 I915_WRITE(GEN6_RC_CONTROL,
3327 rc6_mask |
3328 GEN6_RC_CTL_EI_MODE(1) |
3329 GEN6_RC_CTL_HW_ENABLE);
3330
Chris Wilsondd75fdc2013-09-25 17:34:57 +01003331 /* Power down if completely idle for over 50ms */
3332 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 50000);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003333 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003334
Ben Widawsky42c05262012-09-26 10:34:00 -07003335 ret = sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_MIN_FREQ_TABLE, 0);
Ben Widawsky988b36e2013-04-23 17:33:02 -07003336 if (!ret) {
Ben Widawsky42c05262012-09-26 10:34:00 -07003337 pcu_mbox = 0;
3338 ret = sandybridge_pcode_read(dev_priv, GEN6_READ_OC_PARAMS, &pcu_mbox);
Ben Widawskya2b3fc02013-03-19 20:19:56 -07003339 if (!ret && (pcu_mbox & (1<<31))) { /* OC supported */
Ben Widawsky10e08492013-04-05 14:29:23 -07003340 DRM_DEBUG_DRIVER("Overclocking supported. Max: %dMHz, Overclock max: %dMHz\n",
Ben Widawskya2b3fc02013-03-19 20:19:56 -07003341 (dev_priv->rps.max_delay & 0xff) * 50,
3342 (pcu_mbox & 0xff) * 50);
Ben Widawsky31c77382013-04-05 14:29:22 -07003343 dev_priv->rps.hw_max = pcu_mbox & 0xff;
Ben Widawsky42c05262012-09-26 10:34:00 -07003344 }
3345 } else {
3346 DRM_DEBUG_DRIVER("Failed to set the min frequency\n");
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003347 }
3348
Chris Wilsondd75fdc2013-09-25 17:34:57 +01003349 dev_priv->rps.power = HIGH_POWER; /* force a reset */
3350 gen6_set_rps(dev_priv->dev, dev_priv->rps.min_delay);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003351
Daniel Vetter44fc7d52013-07-12 22:43:27 +02003352 gen6_enable_rps_interrupts(dev);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003353
Ben Widawsky31643d52012-09-26 10:34:01 -07003354 rc6vids = 0;
3355 ret = sandybridge_pcode_read(dev_priv, GEN6_PCODE_READ_RC6VIDS, &rc6vids);
3356 if (IS_GEN6(dev) && ret) {
3357 DRM_DEBUG_DRIVER("Couldn't check for BIOS workaround\n");
3358 } else if (IS_GEN6(dev) && (GEN6_DECODE_RC6_VID(rc6vids & 0xff) < 450)) {
3359 DRM_DEBUG_DRIVER("You should update your BIOS. Correcting minimum rc6 voltage (%dmV->%dmV)\n",
3360 GEN6_DECODE_RC6_VID(rc6vids & 0xff), 450);
3361 rc6vids &= 0xffff00;
3362 rc6vids |= GEN6_ENCODE_RC6_VID(450);
3363 ret = sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_RC6VIDS, rc6vids);
3364 if (ret)
3365 DRM_ERROR("Couldn't fix incorrect rc6 voltage\n");
3366 }
3367
Deepak Sc8d9a592013-11-23 14:55:42 +05303368 gen6_gt_force_wake_put(dev_priv, FORCEWAKE_ALL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003369}
3370
Paulo Zanonic67a4702013-08-19 13:18:09 -03003371void gen6_update_ring_freq(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003372{
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02003373 struct drm_i915_private *dev_priv = dev->dev_private;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003374 int min_freq = 15;
Chris Wilson3ebecd02013-04-12 19:10:13 +01003375 unsigned int gpu_freq;
3376 unsigned int max_ia_freq, min_ring_freq;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003377 int scaling_factor = 180;
Ben Widawskyeda79642013-10-07 17:15:48 -03003378 struct cpufreq_policy *policy;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003379
Jesse Barnes4fc688c2012-11-02 11:14:01 -07003380 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02003381
Ben Widawskyeda79642013-10-07 17:15:48 -03003382 policy = cpufreq_cpu_get(0);
3383 if (policy) {
3384 max_ia_freq = policy->cpuinfo.max_freq;
3385 cpufreq_cpu_put(policy);
3386 } else {
3387 /*
3388 * Default to measured freq if none found, PCU will ensure we
3389 * don't go over
3390 */
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003391 max_ia_freq = tsc_khz;
Ben Widawskyeda79642013-10-07 17:15:48 -03003392 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003393
3394 /* Convert from kHz to MHz */
3395 max_ia_freq /= 1000;
3396
Ben Widawsky153b4b952013-10-22 22:05:09 -07003397 min_ring_freq = I915_READ(DCLK) & 0xf;
Ben Widawskyf6aca452013-10-02 09:25:02 -07003398 /* convert DDR frequency from units of 266.6MHz to bandwidth */
3399 min_ring_freq = mult_frac(min_ring_freq, 8, 3);
Chris Wilson3ebecd02013-04-12 19:10:13 +01003400
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003401 /*
3402 * For each potential GPU frequency, load a ring frequency we'd like
3403 * to use for memory access. We do this by specifying the IA frequency
3404 * the PCU should use as a reference to determine the ring frequency.
3405 */
Daniel Vetterc6a828d2012-08-08 23:35:35 +02003406 for (gpu_freq = dev_priv->rps.max_delay; gpu_freq >= dev_priv->rps.min_delay;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003407 gpu_freq--) {
Daniel Vetterc6a828d2012-08-08 23:35:35 +02003408 int diff = dev_priv->rps.max_delay - gpu_freq;
Chris Wilson3ebecd02013-04-12 19:10:13 +01003409 unsigned int ia_freq = 0, ring_freq = 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003410
Ben Widawsky46c764d2013-11-02 21:07:49 -07003411 if (INTEL_INFO(dev)->gen >= 8) {
3412 /* max(2 * GT, DDR). NB: GT is 50MHz units */
3413 ring_freq = max(min_ring_freq, gpu_freq);
3414 } else if (IS_HASWELL(dev)) {
Ben Widawskyf6aca452013-10-02 09:25:02 -07003415 ring_freq = mult_frac(gpu_freq, 5, 4);
Chris Wilson3ebecd02013-04-12 19:10:13 +01003416 ring_freq = max(min_ring_freq, ring_freq);
3417 /* leave ia_freq as the default, chosen by cpufreq */
3418 } else {
3419 /* On older processors, there is no separate ring
3420 * clock domain, so in order to boost the bandwidth
3421 * of the ring, we need to upclock the CPU (ia_freq).
3422 *
3423 * For GPU frequencies less than 750MHz,
3424 * just use the lowest ring freq.
3425 */
3426 if (gpu_freq < min_freq)
3427 ia_freq = 800;
3428 else
3429 ia_freq = max_ia_freq - ((diff * scaling_factor) / 2);
3430 ia_freq = DIV_ROUND_CLOSEST(ia_freq, 100);
3431 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003432
Ben Widawsky42c05262012-09-26 10:34:00 -07003433 sandybridge_pcode_write(dev_priv,
3434 GEN6_PCODE_WRITE_MIN_FREQ_TABLE,
Chris Wilson3ebecd02013-04-12 19:10:13 +01003435 ia_freq << GEN6_PCODE_FREQ_IA_RATIO_SHIFT |
3436 ring_freq << GEN6_PCODE_FREQ_RING_RATIO_SHIFT |
3437 gpu_freq);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003438 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003439}
3440
Jesse Barnes0a073b82013-04-17 15:54:58 -07003441int valleyview_rps_max_freq(struct drm_i915_private *dev_priv)
3442{
3443 u32 val, rp0;
3444
Jani Nikula64936252013-05-22 15:36:20 +03003445 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FREQ_FUSE);
Jesse Barnes0a073b82013-04-17 15:54:58 -07003446
3447 rp0 = (val & FB_GFX_MAX_FREQ_FUSE_MASK) >> FB_GFX_MAX_FREQ_FUSE_SHIFT;
3448 /* Clamp to max */
3449 rp0 = min_t(u32, rp0, 0xea);
3450
3451 return rp0;
3452}
3453
3454static int valleyview_rps_rpe_freq(struct drm_i915_private *dev_priv)
3455{
3456 u32 val, rpe;
3457
Jani Nikula64936252013-05-22 15:36:20 +03003458 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FMAX_FUSE_LO);
Jesse Barnes0a073b82013-04-17 15:54:58 -07003459 rpe = (val & FB_FMAX_VMIN_FREQ_LO_MASK) >> FB_FMAX_VMIN_FREQ_LO_SHIFT;
Jani Nikula64936252013-05-22 15:36:20 +03003460 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FMAX_FUSE_HI);
Jesse Barnes0a073b82013-04-17 15:54:58 -07003461 rpe |= (val & FB_FMAX_VMIN_FREQ_HI_MASK) << 5;
3462
3463 return rpe;
3464}
3465
3466int valleyview_rps_min_freq(struct drm_i915_private *dev_priv)
3467{
Jani Nikula64936252013-05-22 15:36:20 +03003468 return vlv_punit_read(dev_priv, PUNIT_REG_GPU_LFM) & 0xff;
Jesse Barnes0a073b82013-04-17 15:54:58 -07003469}
3470
Jesse Barnesc9cddff2013-05-08 10:45:13 -07003471static void valleyview_setup_pctx(struct drm_device *dev)
3472{
3473 struct drm_i915_private *dev_priv = dev->dev_private;
3474 struct drm_i915_gem_object *pctx;
3475 unsigned long pctx_paddr;
3476 u32 pcbr;
3477 int pctx_size = 24*1024;
3478
3479 pcbr = I915_READ(VLV_PCBR);
3480 if (pcbr) {
3481 /* BIOS set it up already, grab the pre-alloc'd space */
3482 int pcbr_offset;
3483
3484 pcbr_offset = (pcbr & (~4095)) - dev_priv->mm.stolen_base;
3485 pctx = i915_gem_object_create_stolen_for_preallocated(dev_priv->dev,
3486 pcbr_offset,
Daniel Vetter190d6cd2013-07-04 13:06:28 +02003487 I915_GTT_OFFSET_NONE,
Jesse Barnesc9cddff2013-05-08 10:45:13 -07003488 pctx_size);
3489 goto out;
3490 }
3491
3492 /*
3493 * From the Gunit register HAS:
3494 * The Gfx driver is expected to program this register and ensure
3495 * proper allocation within Gfx stolen memory. For example, this
3496 * register should be programmed such than the PCBR range does not
3497 * overlap with other ranges, such as the frame buffer, protected
3498 * memory, or any other relevant ranges.
3499 */
3500 pctx = i915_gem_object_create_stolen(dev, pctx_size);
3501 if (!pctx) {
3502 DRM_DEBUG("not enough stolen space for PCTX, disabling\n");
3503 return;
3504 }
3505
3506 pctx_paddr = dev_priv->mm.stolen_base + pctx->stolen->start;
3507 I915_WRITE(VLV_PCBR, pctx_paddr);
3508
3509out:
3510 dev_priv->vlv_pctx = pctx;
3511}
3512
Jesse Barnes0a073b82013-04-17 15:54:58 -07003513static void valleyview_enable_rps(struct drm_device *dev)
3514{
3515 struct drm_i915_private *dev_priv = dev->dev_private;
3516 struct intel_ring_buffer *ring;
Jesse Barnesa2b23fe2013-09-19 09:33:13 -07003517 u32 gtfifodbg, val, rc6_mode = 0;
Jesse Barnes0a073b82013-04-17 15:54:58 -07003518 int i;
3519
3520 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
3521
3522 if ((gtfifodbg = I915_READ(GTFIFODBG))) {
Jesse Barnesf7d85c12013-09-27 10:40:54 -07003523 DRM_DEBUG_DRIVER("GT fifo had a previous error %x\n",
3524 gtfifodbg);
Jesse Barnes0a073b82013-04-17 15:54:58 -07003525 I915_WRITE(GTFIFODBG, gtfifodbg);
3526 }
3527
Jesse Barnesc9cddff2013-05-08 10:45:13 -07003528 valleyview_setup_pctx(dev);
3529
Deepak Sc8d9a592013-11-23 14:55:42 +05303530 /* If VLV, Forcewake all wells, else re-direct to regular path */
3531 gen6_gt_force_wake_get(dev_priv, FORCEWAKE_ALL);
Jesse Barnes0a073b82013-04-17 15:54:58 -07003532
3533 I915_WRITE(GEN6_RP_UP_THRESHOLD, 59400);
3534 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 245000);
3535 I915_WRITE(GEN6_RP_UP_EI, 66000);
3536 I915_WRITE(GEN6_RP_DOWN_EI, 350000);
3537
3538 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
3539
3540 I915_WRITE(GEN6_RP_CONTROL,
3541 GEN6_RP_MEDIA_TURBO |
3542 GEN6_RP_MEDIA_HW_NORMAL_MODE |
3543 GEN6_RP_MEDIA_IS_GFX |
3544 GEN6_RP_ENABLE |
3545 GEN6_RP_UP_BUSY_AVG |
3546 GEN6_RP_DOWN_IDLE_CONT);
3547
3548 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 0x00280000);
3549 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
3550 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
3551
3552 for_each_ring(ring, dev_priv, i)
3553 I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
3554
Jesse Barnes2f0aa302013-11-15 09:32:11 -08003555 I915_WRITE(GEN6_RC6_THRESHOLD, 0x557);
Jesse Barnes0a073b82013-04-17 15:54:58 -07003556
3557 /* allows RC6 residency counter to work */
Jesse Barnes49798eb2013-09-26 17:55:57 -07003558 I915_WRITE(VLV_COUNTER_CONTROL,
3559 _MASKED_BIT_ENABLE(VLV_COUNT_RANGE_HIGH |
3560 VLV_MEDIA_RC6_COUNT_EN |
3561 VLV_RENDER_RC6_COUNT_EN));
Jesse Barnesa2b23fe2013-09-19 09:33:13 -07003562 if (intel_enable_rc6(dev) & INTEL_RC6_ENABLE)
Jesse Barnes6b88f292013-11-15 09:32:12 -08003563 rc6_mode = GEN7_RC_CTL_TO_MODE | VLV_RC_CTL_CTX_RST_PARALLEL;
Ben Widawskydc39fff2013-10-18 12:32:07 -07003564
3565 intel_print_rc6_info(dev, rc6_mode);
3566
Jesse Barnesa2b23fe2013-09-19 09:33:13 -07003567 I915_WRITE(GEN6_RC_CONTROL, rc6_mode);
Jesse Barnes0a073b82013-04-17 15:54:58 -07003568
Jani Nikula64936252013-05-22 15:36:20 +03003569 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
Jesse Barnes0a073b82013-04-17 15:54:58 -07003570
3571 DRM_DEBUG_DRIVER("GPLL enabled? %s\n", val & 0x10 ? "yes" : "no");
3572 DRM_DEBUG_DRIVER("GPU status: 0x%08x\n", val);
3573
Jesse Barnes0a073b82013-04-17 15:54:58 -07003574 dev_priv->rps.cur_delay = (val >> 8) & 0xff;
Ville Syrjälä73008b92013-06-25 19:21:01 +03003575 DRM_DEBUG_DRIVER("current GPU freq: %d MHz (%u)\n",
Ville Syrjälä2ec38152013-11-05 22:42:29 +02003576 vlv_gpu_freq(dev_priv, dev_priv->rps.cur_delay),
Ville Syrjälä73008b92013-06-25 19:21:01 +03003577 dev_priv->rps.cur_delay);
Jesse Barnes0a073b82013-04-17 15:54:58 -07003578
3579 dev_priv->rps.max_delay = valleyview_rps_max_freq(dev_priv);
3580 dev_priv->rps.hw_max = dev_priv->rps.max_delay;
Ville Syrjälä73008b92013-06-25 19:21:01 +03003581 DRM_DEBUG_DRIVER("max GPU freq: %d MHz (%u)\n",
Ville Syrjälä2ec38152013-11-05 22:42:29 +02003582 vlv_gpu_freq(dev_priv, dev_priv->rps.max_delay),
Ville Syrjälä73008b92013-06-25 19:21:01 +03003583 dev_priv->rps.max_delay);
Jesse Barnes0a073b82013-04-17 15:54:58 -07003584
Ville Syrjälä73008b92013-06-25 19:21:01 +03003585 dev_priv->rps.rpe_delay = valleyview_rps_rpe_freq(dev_priv);
3586 DRM_DEBUG_DRIVER("RPe GPU freq: %d MHz (%u)\n",
Ville Syrjälä2ec38152013-11-05 22:42:29 +02003587 vlv_gpu_freq(dev_priv, dev_priv->rps.rpe_delay),
Ville Syrjälä73008b92013-06-25 19:21:01 +03003588 dev_priv->rps.rpe_delay);
Jesse Barnes0a073b82013-04-17 15:54:58 -07003589
Ville Syrjälä73008b92013-06-25 19:21:01 +03003590 dev_priv->rps.min_delay = valleyview_rps_min_freq(dev_priv);
3591 DRM_DEBUG_DRIVER("min GPU freq: %d MHz (%u)\n",
Ville Syrjälä2ec38152013-11-05 22:42:29 +02003592 vlv_gpu_freq(dev_priv, dev_priv->rps.min_delay),
Ville Syrjälä73008b92013-06-25 19:21:01 +03003593 dev_priv->rps.min_delay);
Jesse Barnes0a073b82013-04-17 15:54:58 -07003594
Ville Syrjälä73008b92013-06-25 19:21:01 +03003595 DRM_DEBUG_DRIVER("setting GPU freq to %d MHz (%u)\n",
Ville Syrjälä2ec38152013-11-05 22:42:29 +02003596 vlv_gpu_freq(dev_priv, dev_priv->rps.rpe_delay),
Ville Syrjälä73008b92013-06-25 19:21:01 +03003597 dev_priv->rps.rpe_delay);
Jesse Barnes0a073b82013-04-17 15:54:58 -07003598
Ville Syrjälä73008b92013-06-25 19:21:01 +03003599 valleyview_set_rps(dev_priv->dev, dev_priv->rps.rpe_delay);
Jesse Barnes0a073b82013-04-17 15:54:58 -07003600
Daniel Vetter44fc7d52013-07-12 22:43:27 +02003601 gen6_enable_rps_interrupts(dev);
Jesse Barnes0a073b82013-04-17 15:54:58 -07003602
Deepak Sc8d9a592013-11-23 14:55:42 +05303603 gen6_gt_force_wake_put(dev_priv, FORCEWAKE_ALL);
Jesse Barnes0a073b82013-04-17 15:54:58 -07003604}
3605
Daniel Vetter930ebb42012-06-29 23:32:16 +02003606void ironlake_teardown_rc6(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003607{
3608 struct drm_i915_private *dev_priv = dev->dev_private;
3609
Daniel Vetter3e373942012-11-02 19:55:04 +01003610 if (dev_priv->ips.renderctx) {
3611 i915_gem_object_unpin(dev_priv->ips.renderctx);
3612 drm_gem_object_unreference(&dev_priv->ips.renderctx->base);
3613 dev_priv->ips.renderctx = NULL;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003614 }
3615
Daniel Vetter3e373942012-11-02 19:55:04 +01003616 if (dev_priv->ips.pwrctx) {
3617 i915_gem_object_unpin(dev_priv->ips.pwrctx);
3618 drm_gem_object_unreference(&dev_priv->ips.pwrctx->base);
3619 dev_priv->ips.pwrctx = NULL;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003620 }
3621}
3622
Daniel Vetter930ebb42012-06-29 23:32:16 +02003623static void ironlake_disable_rc6(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003624{
3625 struct drm_i915_private *dev_priv = dev->dev_private;
3626
3627 if (I915_READ(PWRCTXA)) {
3628 /* Wake the GPU, prevent RC6, then restore RSTDBYCTL */
3629 I915_WRITE(RSTDBYCTL, I915_READ(RSTDBYCTL) | RCX_SW_EXIT);
3630 wait_for(((I915_READ(RSTDBYCTL) & RSX_STATUS_MASK) == RSX_STATUS_ON),
3631 50);
3632
3633 I915_WRITE(PWRCTXA, 0);
3634 POSTING_READ(PWRCTXA);
3635
3636 I915_WRITE(RSTDBYCTL, I915_READ(RSTDBYCTL) & ~RCX_SW_EXIT);
3637 POSTING_READ(RSTDBYCTL);
3638 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003639}
3640
3641static int ironlake_setup_rc6(struct drm_device *dev)
3642{
3643 struct drm_i915_private *dev_priv = dev->dev_private;
3644
Daniel Vetter3e373942012-11-02 19:55:04 +01003645 if (dev_priv->ips.renderctx == NULL)
3646 dev_priv->ips.renderctx = intel_alloc_context_page(dev);
3647 if (!dev_priv->ips.renderctx)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003648 return -ENOMEM;
3649
Daniel Vetter3e373942012-11-02 19:55:04 +01003650 if (dev_priv->ips.pwrctx == NULL)
3651 dev_priv->ips.pwrctx = intel_alloc_context_page(dev);
3652 if (!dev_priv->ips.pwrctx) {
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003653 ironlake_teardown_rc6(dev);
3654 return -ENOMEM;
3655 }
3656
3657 return 0;
3658}
3659
Daniel Vetter930ebb42012-06-29 23:32:16 +02003660static void ironlake_enable_rc6(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003661{
3662 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter6d90c952012-04-26 23:28:05 +02003663 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Chris Wilson3e960502012-11-27 16:22:54 +00003664 bool was_interruptible;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003665 int ret;
3666
3667 /* rc6 disabled by default due to repeated reports of hanging during
3668 * boot and resume.
3669 */
3670 if (!intel_enable_rc6(dev))
3671 return;
3672
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02003673 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
3674
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003675 ret = ironlake_setup_rc6(dev);
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02003676 if (ret)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003677 return;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003678
Chris Wilson3e960502012-11-27 16:22:54 +00003679 was_interruptible = dev_priv->mm.interruptible;
3680 dev_priv->mm.interruptible = false;
3681
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003682 /*
3683 * GPU can automatically power down the render unit if given a page
3684 * to save state.
3685 */
Daniel Vetter6d90c952012-04-26 23:28:05 +02003686 ret = intel_ring_begin(ring, 6);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003687 if (ret) {
3688 ironlake_teardown_rc6(dev);
Chris Wilson3e960502012-11-27 16:22:54 +00003689 dev_priv->mm.interruptible = was_interruptible;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003690 return;
3691 }
3692
Daniel Vetter6d90c952012-04-26 23:28:05 +02003693 intel_ring_emit(ring, MI_SUSPEND_FLUSH | MI_SUSPEND_FLUSH_EN);
3694 intel_ring_emit(ring, MI_SET_CONTEXT);
Ben Widawskyf343c5f2013-07-05 14:41:04 -07003695 intel_ring_emit(ring, i915_gem_obj_ggtt_offset(dev_priv->ips.renderctx) |
Daniel Vetter6d90c952012-04-26 23:28:05 +02003696 MI_MM_SPACE_GTT |
3697 MI_SAVE_EXT_STATE_EN |
3698 MI_RESTORE_EXT_STATE_EN |
3699 MI_RESTORE_INHIBIT);
3700 intel_ring_emit(ring, MI_SUSPEND_FLUSH);
3701 intel_ring_emit(ring, MI_NOOP);
3702 intel_ring_emit(ring, MI_FLUSH);
3703 intel_ring_advance(ring);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003704
3705 /*
3706 * Wait for the command parser to advance past MI_SET_CONTEXT. The HW
3707 * does an implicit flush, combined with MI_FLUSH above, it should be
3708 * safe to assume that renderctx is valid
3709 */
Chris Wilson3e960502012-11-27 16:22:54 +00003710 ret = intel_ring_idle(ring);
3711 dev_priv->mm.interruptible = was_interruptible;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003712 if (ret) {
Jani Nikuladef27a52013-03-12 10:49:19 +02003713 DRM_ERROR("failed to enable ironlake power savings\n");
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003714 ironlake_teardown_rc6(dev);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003715 return;
3716 }
3717
Ben Widawskyf343c5f2013-07-05 14:41:04 -07003718 I915_WRITE(PWRCTXA, i915_gem_obj_ggtt_offset(dev_priv->ips.pwrctx) | PWRCTX_EN);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003719 I915_WRITE(RSTDBYCTL, I915_READ(RSTDBYCTL) & ~RCX_SW_EXIT);
Ben Widawskydc39fff2013-10-18 12:32:07 -07003720
3721 intel_print_rc6_info(dev, INTEL_RC6_ENABLE);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003722}
3723
Eugeni Dodonovdde18882012-04-18 15:29:24 -03003724static unsigned long intel_pxfreq(u32 vidfreq)
3725{
3726 unsigned long freq;
3727 int div = (vidfreq & 0x3f0000) >> 16;
3728 int post = (vidfreq & 0x3000) >> 12;
3729 int pre = (vidfreq & 0x7);
3730
3731 if (!pre)
3732 return 0;
3733
3734 freq = ((div * 133333) / ((1<<post) * pre));
3735
3736 return freq;
3737}
3738
Daniel Vettereb48eb02012-04-26 23:28:12 +02003739static const struct cparams {
3740 u16 i;
3741 u16 t;
3742 u16 m;
3743 u16 c;
3744} cparams[] = {
3745 { 1, 1333, 301, 28664 },
3746 { 1, 1066, 294, 24460 },
3747 { 1, 800, 294, 25192 },
3748 { 0, 1333, 276, 27605 },
3749 { 0, 1066, 276, 27605 },
3750 { 0, 800, 231, 23784 },
3751};
3752
Chris Wilsonf531dcb2012-09-25 10:16:12 +01003753static unsigned long __i915_chipset_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02003754{
3755 u64 total_count, diff, ret;
3756 u32 count1, count2, count3, m = 0, c = 0;
3757 unsigned long now = jiffies_to_msecs(jiffies), diff1;
3758 int i;
3759
Daniel Vetter02d71952012-08-09 16:44:54 +02003760 assert_spin_locked(&mchdev_lock);
3761
Daniel Vetter20e4d402012-08-08 23:35:39 +02003762 diff1 = now - dev_priv->ips.last_time1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02003763
3764 /* Prevent division-by-zero if we are asking too fast.
3765 * Also, we don't get interesting results if we are polling
3766 * faster than once in 10ms, so just return the saved value
3767 * in such cases.
3768 */
3769 if (diff1 <= 10)
Daniel Vetter20e4d402012-08-08 23:35:39 +02003770 return dev_priv->ips.chipset_power;
Daniel Vettereb48eb02012-04-26 23:28:12 +02003771
3772 count1 = I915_READ(DMIEC);
3773 count2 = I915_READ(DDREC);
3774 count3 = I915_READ(CSIEC);
3775
3776 total_count = count1 + count2 + count3;
3777
3778 /* FIXME: handle per-counter overflow */
Daniel Vetter20e4d402012-08-08 23:35:39 +02003779 if (total_count < dev_priv->ips.last_count1) {
3780 diff = ~0UL - dev_priv->ips.last_count1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02003781 diff += total_count;
3782 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +02003783 diff = total_count - dev_priv->ips.last_count1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02003784 }
3785
3786 for (i = 0; i < ARRAY_SIZE(cparams); i++) {
Daniel Vetter20e4d402012-08-08 23:35:39 +02003787 if (cparams[i].i == dev_priv->ips.c_m &&
3788 cparams[i].t == dev_priv->ips.r_t) {
Daniel Vettereb48eb02012-04-26 23:28:12 +02003789 m = cparams[i].m;
3790 c = cparams[i].c;
3791 break;
3792 }
3793 }
3794
3795 diff = div_u64(diff, diff1);
3796 ret = ((m * diff) + c);
3797 ret = div_u64(ret, 10);
3798
Daniel Vetter20e4d402012-08-08 23:35:39 +02003799 dev_priv->ips.last_count1 = total_count;
3800 dev_priv->ips.last_time1 = now;
Daniel Vettereb48eb02012-04-26 23:28:12 +02003801
Daniel Vetter20e4d402012-08-08 23:35:39 +02003802 dev_priv->ips.chipset_power = ret;
Daniel Vettereb48eb02012-04-26 23:28:12 +02003803
3804 return ret;
3805}
3806
Chris Wilsonf531dcb2012-09-25 10:16:12 +01003807unsigned long i915_chipset_val(struct drm_i915_private *dev_priv)
3808{
3809 unsigned long val;
3810
3811 if (dev_priv->info->gen != 5)
3812 return 0;
3813
3814 spin_lock_irq(&mchdev_lock);
3815
3816 val = __i915_chipset_val(dev_priv);
3817
3818 spin_unlock_irq(&mchdev_lock);
3819
3820 return val;
3821}
3822
Daniel Vettereb48eb02012-04-26 23:28:12 +02003823unsigned long i915_mch_val(struct drm_i915_private *dev_priv)
3824{
3825 unsigned long m, x, b;
3826 u32 tsfs;
3827
3828 tsfs = I915_READ(TSFS);
3829
3830 m = ((tsfs & TSFS_SLOPE_MASK) >> TSFS_SLOPE_SHIFT);
3831 x = I915_READ8(TR1);
3832
3833 b = tsfs & TSFS_INTR_MASK;
3834
3835 return ((m * x) / 127) - b;
3836}
3837
3838static u16 pvid_to_extvid(struct drm_i915_private *dev_priv, u8 pxvid)
3839{
3840 static const struct v_table {
3841 u16 vd; /* in .1 mil */
3842 u16 vm; /* in .1 mil */
3843 } v_table[] = {
3844 { 0, 0, },
3845 { 375, 0, },
3846 { 500, 0, },
3847 { 625, 0, },
3848 { 750, 0, },
3849 { 875, 0, },
3850 { 1000, 0, },
3851 { 1125, 0, },
3852 { 4125, 3000, },
3853 { 4125, 3000, },
3854 { 4125, 3000, },
3855 { 4125, 3000, },
3856 { 4125, 3000, },
3857 { 4125, 3000, },
3858 { 4125, 3000, },
3859 { 4125, 3000, },
3860 { 4125, 3000, },
3861 { 4125, 3000, },
3862 { 4125, 3000, },
3863 { 4125, 3000, },
3864 { 4125, 3000, },
3865 { 4125, 3000, },
3866 { 4125, 3000, },
3867 { 4125, 3000, },
3868 { 4125, 3000, },
3869 { 4125, 3000, },
3870 { 4125, 3000, },
3871 { 4125, 3000, },
3872 { 4125, 3000, },
3873 { 4125, 3000, },
3874 { 4125, 3000, },
3875 { 4125, 3000, },
3876 { 4250, 3125, },
3877 { 4375, 3250, },
3878 { 4500, 3375, },
3879 { 4625, 3500, },
3880 { 4750, 3625, },
3881 { 4875, 3750, },
3882 { 5000, 3875, },
3883 { 5125, 4000, },
3884 { 5250, 4125, },
3885 { 5375, 4250, },
3886 { 5500, 4375, },
3887 { 5625, 4500, },
3888 { 5750, 4625, },
3889 { 5875, 4750, },
3890 { 6000, 4875, },
3891 { 6125, 5000, },
3892 { 6250, 5125, },
3893 { 6375, 5250, },
3894 { 6500, 5375, },
3895 { 6625, 5500, },
3896 { 6750, 5625, },
3897 { 6875, 5750, },
3898 { 7000, 5875, },
3899 { 7125, 6000, },
3900 { 7250, 6125, },
3901 { 7375, 6250, },
3902 { 7500, 6375, },
3903 { 7625, 6500, },
3904 { 7750, 6625, },
3905 { 7875, 6750, },
3906 { 8000, 6875, },
3907 { 8125, 7000, },
3908 { 8250, 7125, },
3909 { 8375, 7250, },
3910 { 8500, 7375, },
3911 { 8625, 7500, },
3912 { 8750, 7625, },
3913 { 8875, 7750, },
3914 { 9000, 7875, },
3915 { 9125, 8000, },
3916 { 9250, 8125, },
3917 { 9375, 8250, },
3918 { 9500, 8375, },
3919 { 9625, 8500, },
3920 { 9750, 8625, },
3921 { 9875, 8750, },
3922 { 10000, 8875, },
3923 { 10125, 9000, },
3924 { 10250, 9125, },
3925 { 10375, 9250, },
3926 { 10500, 9375, },
3927 { 10625, 9500, },
3928 { 10750, 9625, },
3929 { 10875, 9750, },
3930 { 11000, 9875, },
3931 { 11125, 10000, },
3932 { 11250, 10125, },
3933 { 11375, 10250, },
3934 { 11500, 10375, },
3935 { 11625, 10500, },
3936 { 11750, 10625, },
3937 { 11875, 10750, },
3938 { 12000, 10875, },
3939 { 12125, 11000, },
3940 { 12250, 11125, },
3941 { 12375, 11250, },
3942 { 12500, 11375, },
3943 { 12625, 11500, },
3944 { 12750, 11625, },
3945 { 12875, 11750, },
3946 { 13000, 11875, },
3947 { 13125, 12000, },
3948 { 13250, 12125, },
3949 { 13375, 12250, },
3950 { 13500, 12375, },
3951 { 13625, 12500, },
3952 { 13750, 12625, },
3953 { 13875, 12750, },
3954 { 14000, 12875, },
3955 { 14125, 13000, },
3956 { 14250, 13125, },
3957 { 14375, 13250, },
3958 { 14500, 13375, },
3959 { 14625, 13500, },
3960 { 14750, 13625, },
3961 { 14875, 13750, },
3962 { 15000, 13875, },
3963 { 15125, 14000, },
3964 { 15250, 14125, },
3965 { 15375, 14250, },
3966 { 15500, 14375, },
3967 { 15625, 14500, },
3968 { 15750, 14625, },
3969 { 15875, 14750, },
3970 { 16000, 14875, },
3971 { 16125, 15000, },
3972 };
3973 if (dev_priv->info->is_mobile)
3974 return v_table[pxvid].vm;
3975 else
3976 return v_table[pxvid].vd;
3977}
3978
Daniel Vetter02d71952012-08-09 16:44:54 +02003979static void __i915_update_gfx_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02003980{
3981 struct timespec now, diff1;
3982 u64 diff;
3983 unsigned long diffms;
3984 u32 count;
3985
Daniel Vetter02d71952012-08-09 16:44:54 +02003986 assert_spin_locked(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02003987
3988 getrawmonotonic(&now);
Daniel Vetter20e4d402012-08-08 23:35:39 +02003989 diff1 = timespec_sub(now, dev_priv->ips.last_time2);
Daniel Vettereb48eb02012-04-26 23:28:12 +02003990
3991 /* Don't divide by 0 */
3992 diffms = diff1.tv_sec * 1000 + diff1.tv_nsec / 1000000;
3993 if (!diffms)
3994 return;
3995
3996 count = I915_READ(GFXEC);
3997
Daniel Vetter20e4d402012-08-08 23:35:39 +02003998 if (count < dev_priv->ips.last_count2) {
3999 diff = ~0UL - dev_priv->ips.last_count2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02004000 diff += count;
4001 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +02004002 diff = count - dev_priv->ips.last_count2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02004003 }
4004
Daniel Vetter20e4d402012-08-08 23:35:39 +02004005 dev_priv->ips.last_count2 = count;
4006 dev_priv->ips.last_time2 = now;
Daniel Vettereb48eb02012-04-26 23:28:12 +02004007
4008 /* More magic constants... */
4009 diff = diff * 1181;
4010 diff = div_u64(diff, diffms * 10);
Daniel Vetter20e4d402012-08-08 23:35:39 +02004011 dev_priv->ips.gfx_power = diff;
Daniel Vettereb48eb02012-04-26 23:28:12 +02004012}
4013
Daniel Vetter02d71952012-08-09 16:44:54 +02004014void i915_update_gfx_val(struct drm_i915_private *dev_priv)
4015{
4016 if (dev_priv->info->gen != 5)
4017 return;
4018
Daniel Vetter92703882012-08-09 16:46:01 +02004019 spin_lock_irq(&mchdev_lock);
Daniel Vetter02d71952012-08-09 16:44:54 +02004020
4021 __i915_update_gfx_val(dev_priv);
4022
Daniel Vetter92703882012-08-09 16:46:01 +02004023 spin_unlock_irq(&mchdev_lock);
Daniel Vetter02d71952012-08-09 16:44:54 +02004024}
4025
Chris Wilsonf531dcb2012-09-25 10:16:12 +01004026static unsigned long __i915_gfx_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02004027{
4028 unsigned long t, corr, state1, corr2, state2;
4029 u32 pxvid, ext_v;
4030
Daniel Vetter02d71952012-08-09 16:44:54 +02004031 assert_spin_locked(&mchdev_lock);
4032
Daniel Vetterc6a828d2012-08-08 23:35:35 +02004033 pxvid = I915_READ(PXVFREQ_BASE + (dev_priv->rps.cur_delay * 4));
Daniel Vettereb48eb02012-04-26 23:28:12 +02004034 pxvid = (pxvid >> 24) & 0x7f;
4035 ext_v = pvid_to_extvid(dev_priv, pxvid);
4036
4037 state1 = ext_v;
4038
4039 t = i915_mch_val(dev_priv);
4040
4041 /* Revel in the empirically derived constants */
4042
4043 /* Correction factor in 1/100000 units */
4044 if (t > 80)
4045 corr = ((t * 2349) + 135940);
4046 else if (t >= 50)
4047 corr = ((t * 964) + 29317);
4048 else /* < 50 */
4049 corr = ((t * 301) + 1004);
4050
4051 corr = corr * ((150142 * state1) / 10000 - 78642);
4052 corr /= 100000;
Daniel Vetter20e4d402012-08-08 23:35:39 +02004053 corr2 = (corr * dev_priv->ips.corr);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004054
4055 state2 = (corr2 * state1) / 10000;
4056 state2 /= 100; /* convert to mW */
4057
Daniel Vetter02d71952012-08-09 16:44:54 +02004058 __i915_update_gfx_val(dev_priv);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004059
Daniel Vetter20e4d402012-08-08 23:35:39 +02004060 return dev_priv->ips.gfx_power + state2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02004061}
4062
Chris Wilsonf531dcb2012-09-25 10:16:12 +01004063unsigned long i915_gfx_val(struct drm_i915_private *dev_priv)
4064{
4065 unsigned long val;
4066
4067 if (dev_priv->info->gen != 5)
4068 return 0;
4069
4070 spin_lock_irq(&mchdev_lock);
4071
4072 val = __i915_gfx_val(dev_priv);
4073
4074 spin_unlock_irq(&mchdev_lock);
4075
4076 return val;
4077}
4078
Daniel Vettereb48eb02012-04-26 23:28:12 +02004079/**
4080 * i915_read_mch_val - return value for IPS use
4081 *
4082 * Calculate and return a value for the IPS driver to use when deciding whether
4083 * we have thermal and power headroom to increase CPU or GPU power budget.
4084 */
4085unsigned long i915_read_mch_val(void)
4086{
4087 struct drm_i915_private *dev_priv;
4088 unsigned long chipset_val, graphics_val, ret = 0;
4089
Daniel Vetter92703882012-08-09 16:46:01 +02004090 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004091 if (!i915_mch_dev)
4092 goto out_unlock;
4093 dev_priv = i915_mch_dev;
4094
Chris Wilsonf531dcb2012-09-25 10:16:12 +01004095 chipset_val = __i915_chipset_val(dev_priv);
4096 graphics_val = __i915_gfx_val(dev_priv);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004097
4098 ret = chipset_val + graphics_val;
4099
4100out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02004101 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004102
4103 return ret;
4104}
4105EXPORT_SYMBOL_GPL(i915_read_mch_val);
4106
4107/**
4108 * i915_gpu_raise - raise GPU frequency limit
4109 *
4110 * Raise the limit; IPS indicates we have thermal headroom.
4111 */
4112bool i915_gpu_raise(void)
4113{
4114 struct drm_i915_private *dev_priv;
4115 bool ret = true;
4116
Daniel Vetter92703882012-08-09 16:46:01 +02004117 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004118 if (!i915_mch_dev) {
4119 ret = false;
4120 goto out_unlock;
4121 }
4122 dev_priv = i915_mch_dev;
4123
Daniel Vetter20e4d402012-08-08 23:35:39 +02004124 if (dev_priv->ips.max_delay > dev_priv->ips.fmax)
4125 dev_priv->ips.max_delay--;
Daniel Vettereb48eb02012-04-26 23:28:12 +02004126
4127out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02004128 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004129
4130 return ret;
4131}
4132EXPORT_SYMBOL_GPL(i915_gpu_raise);
4133
4134/**
4135 * i915_gpu_lower - lower GPU frequency limit
4136 *
4137 * IPS indicates we're close to a thermal limit, so throttle back the GPU
4138 * frequency maximum.
4139 */
4140bool i915_gpu_lower(void)
4141{
4142 struct drm_i915_private *dev_priv;
4143 bool ret = true;
4144
Daniel Vetter92703882012-08-09 16:46:01 +02004145 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004146 if (!i915_mch_dev) {
4147 ret = false;
4148 goto out_unlock;
4149 }
4150 dev_priv = i915_mch_dev;
4151
Daniel Vetter20e4d402012-08-08 23:35:39 +02004152 if (dev_priv->ips.max_delay < dev_priv->ips.min_delay)
4153 dev_priv->ips.max_delay++;
Daniel Vettereb48eb02012-04-26 23:28:12 +02004154
4155out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02004156 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004157
4158 return ret;
4159}
4160EXPORT_SYMBOL_GPL(i915_gpu_lower);
4161
4162/**
4163 * i915_gpu_busy - indicate GPU business to IPS
4164 *
4165 * Tell the IPS driver whether or not the GPU is busy.
4166 */
4167bool i915_gpu_busy(void)
4168{
4169 struct drm_i915_private *dev_priv;
Chris Wilsonf047e392012-07-21 12:31:41 +01004170 struct intel_ring_buffer *ring;
Daniel Vettereb48eb02012-04-26 23:28:12 +02004171 bool ret = false;
Chris Wilsonf047e392012-07-21 12:31:41 +01004172 int i;
Daniel Vettereb48eb02012-04-26 23:28:12 +02004173
Daniel Vetter92703882012-08-09 16:46:01 +02004174 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004175 if (!i915_mch_dev)
4176 goto out_unlock;
4177 dev_priv = i915_mch_dev;
4178
Chris Wilsonf047e392012-07-21 12:31:41 +01004179 for_each_ring(ring, dev_priv, i)
4180 ret |= !list_empty(&ring->request_list);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004181
4182out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02004183 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004184
4185 return ret;
4186}
4187EXPORT_SYMBOL_GPL(i915_gpu_busy);
4188
4189/**
4190 * i915_gpu_turbo_disable - disable graphics turbo
4191 *
4192 * Disable graphics turbo by resetting the max frequency and setting the
4193 * current frequency to the default.
4194 */
4195bool i915_gpu_turbo_disable(void)
4196{
4197 struct drm_i915_private *dev_priv;
4198 bool ret = true;
4199
Daniel Vetter92703882012-08-09 16:46:01 +02004200 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004201 if (!i915_mch_dev) {
4202 ret = false;
4203 goto out_unlock;
4204 }
4205 dev_priv = i915_mch_dev;
4206
Daniel Vetter20e4d402012-08-08 23:35:39 +02004207 dev_priv->ips.max_delay = dev_priv->ips.fstart;
Daniel Vettereb48eb02012-04-26 23:28:12 +02004208
Daniel Vetter20e4d402012-08-08 23:35:39 +02004209 if (!ironlake_set_drps(dev_priv->dev, dev_priv->ips.fstart))
Daniel Vettereb48eb02012-04-26 23:28:12 +02004210 ret = false;
4211
4212out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02004213 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004214
4215 return ret;
4216}
4217EXPORT_SYMBOL_GPL(i915_gpu_turbo_disable);
4218
4219/**
4220 * Tells the intel_ips driver that the i915 driver is now loaded, if
4221 * IPS got loaded first.
4222 *
4223 * This awkward dance is so that neither module has to depend on the
4224 * other in order for IPS to do the appropriate communication of
4225 * GPU turbo limits to i915.
4226 */
4227static void
4228ips_ping_for_i915_load(void)
4229{
4230 void (*link)(void);
4231
4232 link = symbol_get(ips_link_to_i915_driver);
4233 if (link) {
4234 link();
4235 symbol_put(ips_link_to_i915_driver);
4236 }
4237}
4238
4239void intel_gpu_ips_init(struct drm_i915_private *dev_priv)
4240{
Daniel Vetter02d71952012-08-09 16:44:54 +02004241 /* We only register the i915 ips part with intel-ips once everything is
4242 * set up, to avoid intel-ips sneaking in and reading bogus values. */
Daniel Vetter92703882012-08-09 16:46:01 +02004243 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004244 i915_mch_dev = dev_priv;
Daniel Vetter92703882012-08-09 16:46:01 +02004245 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004246
4247 ips_ping_for_i915_load();
4248}
4249
4250void intel_gpu_ips_teardown(void)
4251{
Daniel Vetter92703882012-08-09 16:46:01 +02004252 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004253 i915_mch_dev = NULL;
Daniel Vetter92703882012-08-09 16:46:01 +02004254 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004255}
Daniel Vetter8090c6b2012-06-24 16:42:32 +02004256static void intel_init_emon(struct drm_device *dev)
Eugeni Dodonovdde18882012-04-18 15:29:24 -03004257{
4258 struct drm_i915_private *dev_priv = dev->dev_private;
4259 u32 lcfuse;
4260 u8 pxw[16];
4261 int i;
4262
4263 /* Disable to program */
4264 I915_WRITE(ECR, 0);
4265 POSTING_READ(ECR);
4266
4267 /* Program energy weights for various events */
4268 I915_WRITE(SDEW, 0x15040d00);
4269 I915_WRITE(CSIEW0, 0x007f0000);
4270 I915_WRITE(CSIEW1, 0x1e220004);
4271 I915_WRITE(CSIEW2, 0x04000004);
4272
4273 for (i = 0; i < 5; i++)
4274 I915_WRITE(PEW + (i * 4), 0);
4275 for (i = 0; i < 3; i++)
4276 I915_WRITE(DEW + (i * 4), 0);
4277
4278 /* Program P-state weights to account for frequency power adjustment */
4279 for (i = 0; i < 16; i++) {
4280 u32 pxvidfreq = I915_READ(PXVFREQ_BASE + (i * 4));
4281 unsigned long freq = intel_pxfreq(pxvidfreq);
4282 unsigned long vid = (pxvidfreq & PXVFREQ_PX_MASK) >>
4283 PXVFREQ_PX_SHIFT;
4284 unsigned long val;
4285
4286 val = vid * vid;
4287 val *= (freq / 1000);
4288 val *= 255;
4289 val /= (127*127*900);
4290 if (val > 0xff)
4291 DRM_ERROR("bad pxval: %ld\n", val);
4292 pxw[i] = val;
4293 }
4294 /* Render standby states get 0 weight */
4295 pxw[14] = 0;
4296 pxw[15] = 0;
4297
4298 for (i = 0; i < 4; i++) {
4299 u32 val = (pxw[i*4] << 24) | (pxw[(i*4)+1] << 16) |
4300 (pxw[(i*4)+2] << 8) | (pxw[(i*4)+3]);
4301 I915_WRITE(PXW + (i * 4), val);
4302 }
4303
4304 /* Adjust magic regs to magic values (more experimental results) */
4305 I915_WRITE(OGW0, 0);
4306 I915_WRITE(OGW1, 0);
4307 I915_WRITE(EG0, 0x00007f00);
4308 I915_WRITE(EG1, 0x0000000e);
4309 I915_WRITE(EG2, 0x000e0000);
4310 I915_WRITE(EG3, 0x68000300);
4311 I915_WRITE(EG4, 0x42000000);
4312 I915_WRITE(EG5, 0x00140031);
4313 I915_WRITE(EG6, 0);
4314 I915_WRITE(EG7, 0);
4315
4316 for (i = 0; i < 8; i++)
4317 I915_WRITE(PXWL + (i * 4), 0);
4318
4319 /* Enable PMON + select events */
4320 I915_WRITE(ECR, 0x80000019);
4321
4322 lcfuse = I915_READ(LCFUSE02);
4323
Daniel Vetter20e4d402012-08-08 23:35:39 +02004324 dev_priv->ips.corr = (lcfuse & LCFUSE_HIV_MASK);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03004325}
4326
Daniel Vetter8090c6b2012-06-24 16:42:32 +02004327void intel_disable_gt_powersave(struct drm_device *dev)
4328{
Jesse Barnes1a01ab32012-11-02 11:14:00 -07004329 struct drm_i915_private *dev_priv = dev->dev_private;
4330
Daniel Vetterfd0c0642013-04-24 11:13:35 +02004331 /* Interrupts should be disabled already to avoid re-arming. */
4332 WARN_ON(dev->irq_enabled);
4333
Daniel Vetter930ebb42012-06-29 23:32:16 +02004334 if (IS_IRONLAKE_M(dev)) {
Daniel Vetter8090c6b2012-06-24 16:42:32 +02004335 ironlake_disable_drps(dev);
Daniel Vetter930ebb42012-06-29 23:32:16 +02004336 ironlake_disable_rc6(dev);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004337 } else if (INTEL_INFO(dev)->gen >= 6) {
Jesse Barnes1a01ab32012-11-02 11:14:00 -07004338 cancel_delayed_work_sync(&dev_priv->rps.delayed_resume_work);
Jesse Barnes250848c2013-04-23 10:09:27 -07004339 cancel_work_sync(&dev_priv->rps.work);
Jesse Barnes4fc688c2012-11-02 11:14:01 -07004340 mutex_lock(&dev_priv->rps.hw_lock);
Jesse Barnesd20d4f02013-04-23 10:09:28 -07004341 if (IS_VALLEYVIEW(dev))
4342 valleyview_disable_rps(dev);
4343 else
4344 gen6_disable_rps(dev);
Chris Wilsonc0951f02013-10-10 21:58:50 +01004345 dev_priv->rps.enabled = false;
Jesse Barnes4fc688c2012-11-02 11:14:01 -07004346 mutex_unlock(&dev_priv->rps.hw_lock);
Daniel Vetter930ebb42012-06-29 23:32:16 +02004347 }
Daniel Vetter8090c6b2012-06-24 16:42:32 +02004348}
4349
Jesse Barnes1a01ab32012-11-02 11:14:00 -07004350static void intel_gen6_powersave_work(struct work_struct *work)
4351{
4352 struct drm_i915_private *dev_priv =
4353 container_of(work, struct drm_i915_private,
4354 rps.delayed_resume_work.work);
4355 struct drm_device *dev = dev_priv->dev;
4356
Jesse Barnes4fc688c2012-11-02 11:14:01 -07004357 mutex_lock(&dev_priv->rps.hw_lock);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004358
4359 if (IS_VALLEYVIEW(dev)) {
4360 valleyview_enable_rps(dev);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004361 } else if (IS_BROADWELL(dev)) {
4362 gen8_enable_rps(dev);
4363 gen6_update_ring_freq(dev);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004364 } else {
4365 gen6_enable_rps(dev);
4366 gen6_update_ring_freq(dev);
4367 }
Chris Wilsonc0951f02013-10-10 21:58:50 +01004368 dev_priv->rps.enabled = true;
Jesse Barnes4fc688c2012-11-02 11:14:01 -07004369 mutex_unlock(&dev_priv->rps.hw_lock);
Jesse Barnes1a01ab32012-11-02 11:14:00 -07004370}
4371
Daniel Vetter8090c6b2012-06-24 16:42:32 +02004372void intel_enable_gt_powersave(struct drm_device *dev)
4373{
Jesse Barnes1a01ab32012-11-02 11:14:00 -07004374 struct drm_i915_private *dev_priv = dev->dev_private;
4375
Daniel Vetter8090c6b2012-06-24 16:42:32 +02004376 if (IS_IRONLAKE_M(dev)) {
4377 ironlake_enable_drps(dev);
4378 ironlake_enable_rc6(dev);
4379 intel_init_emon(dev);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004380 } else if (IS_GEN6(dev) || IS_GEN7(dev)) {
Jesse Barnes1a01ab32012-11-02 11:14:00 -07004381 /*
4382 * PCU communication is slow and this doesn't need to be
4383 * done at any specific time, so do this out of our fast path
4384 * to make resume and init faster.
4385 */
4386 schedule_delayed_work(&dev_priv->rps.delayed_resume_work,
4387 round_jiffies_up_relative(HZ));
Daniel Vetter8090c6b2012-06-24 16:42:32 +02004388 }
4389}
4390
Daniel Vetter3107bd42012-10-31 22:52:31 +01004391static void ibx_init_clock_gating(struct drm_device *dev)
4392{
4393 struct drm_i915_private *dev_priv = dev->dev_private;
4394
4395 /*
4396 * On Ibex Peak and Cougar Point, we need to disable clock
4397 * gating for the panel power sequencer or it will fail to
4398 * start up when no ports are active.
4399 */
4400 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE);
4401}
4402
Ville Syrjälä0e088b82013-06-07 10:47:04 +03004403static void g4x_disable_trickle_feed(struct drm_device *dev)
4404{
4405 struct drm_i915_private *dev_priv = dev->dev_private;
4406 int pipe;
4407
4408 for_each_pipe(pipe) {
4409 I915_WRITE(DSPCNTR(pipe),
4410 I915_READ(DSPCNTR(pipe)) |
4411 DISPPLANE_TRICKLE_FEED_DISABLE);
Ville Syrjälä1dba99f2013-10-01 18:02:18 +03004412 intel_flush_primary_plane(dev_priv, pipe);
Ville Syrjälä0e088b82013-06-07 10:47:04 +03004413 }
4414}
4415
Ville Syrjälä017636c2013-12-05 15:51:37 +02004416static void ilk_init_lp_watermarks(struct drm_device *dev)
4417{
4418 struct drm_i915_private *dev_priv = dev->dev_private;
4419
4420 I915_WRITE(WM3_LP_ILK, I915_READ(WM3_LP_ILK) & ~WM1_LP_SR_EN);
4421 I915_WRITE(WM2_LP_ILK, I915_READ(WM2_LP_ILK) & ~WM1_LP_SR_EN);
4422 I915_WRITE(WM1_LP_ILK, I915_READ(WM1_LP_ILK) & ~WM1_LP_SR_EN);
4423
4424 /*
4425 * Don't touch WM1S_LP_EN here.
4426 * Doing so could cause underruns.
4427 */
4428}
4429
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03004430static void ironlake_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004431{
4432 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau231e54f2012-10-19 17:55:41 +01004433 uint32_t dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004434
Damien Lespiauf1e8fa52013-06-07 17:41:09 +01004435 /*
4436 * Required for FBC
4437 * WaFbcDisableDpfcClockGating:ilk
4438 */
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01004439 dspclk_gate |= ILK_DPFCRUNIT_CLOCK_GATE_DISABLE |
4440 ILK_DPFCUNIT_CLOCK_GATE_DISABLE |
4441 ILK_DPFDUNIT_CLOCK_GATE_ENABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004442
4443 I915_WRITE(PCH_3DCGDIS0,
4444 MARIUNIT_CLOCK_GATE_DISABLE |
4445 SVSMUNIT_CLOCK_GATE_DISABLE);
4446 I915_WRITE(PCH_3DCGDIS1,
4447 VFMUNIT_CLOCK_GATE_DISABLE);
4448
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004449 /*
4450 * According to the spec the following bits should be set in
4451 * order to enable memory self-refresh
4452 * The bit 22/21 of 0x42004
4453 * The bit 5 of 0x42020
4454 * The bit 15 of 0x45000
4455 */
4456 I915_WRITE(ILK_DISPLAY_CHICKEN2,
4457 (I915_READ(ILK_DISPLAY_CHICKEN2) |
4458 ILK_DPARB_GATE | ILK_VSDPFD_FULL));
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01004459 dspclk_gate |= ILK_DPARBUNIT_CLOCK_GATE_ENABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004460 I915_WRITE(DISP_ARB_CTL,
4461 (I915_READ(DISP_ARB_CTL) |
4462 DISP_FBC_WM_DIS));
Ville Syrjälä017636c2013-12-05 15:51:37 +02004463
4464 ilk_init_lp_watermarks(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004465
4466 /*
4467 * Based on the document from hardware guys the following bits
4468 * should be set unconditionally in order to enable FBC.
4469 * The bit 22 of 0x42000
4470 * The bit 22 of 0x42004
4471 * The bit 7,8,9 of 0x42020.
4472 */
4473 if (IS_IRONLAKE_M(dev)) {
Damien Lespiau4bb35332013-06-14 15:23:24 +01004474 /* WaFbcAsynchFlipDisableFbcQueue:ilk */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004475 I915_WRITE(ILK_DISPLAY_CHICKEN1,
4476 I915_READ(ILK_DISPLAY_CHICKEN1) |
4477 ILK_FBCQ_DIS);
4478 I915_WRITE(ILK_DISPLAY_CHICKEN2,
4479 I915_READ(ILK_DISPLAY_CHICKEN2) |
4480 ILK_DPARB_GATE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004481 }
4482
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01004483 I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
4484
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004485 I915_WRITE(ILK_DISPLAY_CHICKEN2,
4486 I915_READ(ILK_DISPLAY_CHICKEN2) |
4487 ILK_ELPIN_409_SELECT);
4488 I915_WRITE(_3D_CHICKEN2,
4489 _3D_CHICKEN2_WM_READ_PIPELINED << 16 |
4490 _3D_CHICKEN2_WM_READ_PIPELINED);
Daniel Vetter4358a372012-10-18 11:49:51 +02004491
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01004492 /* WaDisableRenderCachePipelinedFlush:ilk */
Daniel Vetter4358a372012-10-18 11:49:51 +02004493 I915_WRITE(CACHE_MODE_0,
4494 _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
Daniel Vetter3107bd42012-10-31 22:52:31 +01004495
Ville Syrjälä0e088b82013-06-07 10:47:04 +03004496 g4x_disable_trickle_feed(dev);
Ville Syrjäläbdad2b22013-06-07 10:47:03 +03004497
Daniel Vetter3107bd42012-10-31 22:52:31 +01004498 ibx_init_clock_gating(dev);
4499}
4500
4501static void cpt_init_clock_gating(struct drm_device *dev)
4502{
4503 struct drm_i915_private *dev_priv = dev->dev_private;
4504 int pipe;
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03004505 uint32_t val;
Daniel Vetter3107bd42012-10-31 22:52:31 +01004506
4507 /*
4508 * On Ibex Peak and Cougar Point, we need to disable clock
4509 * gating for the panel power sequencer or it will fail to
4510 * start up when no ports are active.
4511 */
Jesse Barnescd664072013-10-02 10:34:19 -07004512 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE |
4513 PCH_DPLUNIT_CLOCK_GATE_DISABLE |
4514 PCH_CPUNIT_CLOCK_GATE_DISABLE);
Daniel Vetter3107bd42012-10-31 22:52:31 +01004515 I915_WRITE(SOUTH_CHICKEN2, I915_READ(SOUTH_CHICKEN2) |
4516 DPLS_EDP_PPS_FIX_DIS);
Takashi Iwai335c07b2012-12-11 11:46:29 +01004517 /* The below fixes the weird display corruption, a few pixels shifted
4518 * downward, on (only) LVDS of some HP laptops with IVY.
4519 */
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03004520 for_each_pipe(pipe) {
Paulo Zanonidc4bd2d2013-04-08 15:48:08 -03004521 val = I915_READ(TRANS_CHICKEN2(pipe));
4522 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
4523 val &= ~TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03004524 if (dev_priv->vbt.fdi_rx_polarity_inverted)
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03004525 val |= TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
Paulo Zanonidc4bd2d2013-04-08 15:48:08 -03004526 val &= ~TRANS_CHICKEN2_FRAME_START_DELAY_MASK;
4527 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_COUNTER;
4528 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_MODESWITCH;
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03004529 I915_WRITE(TRANS_CHICKEN2(pipe), val);
4530 }
Daniel Vetter3107bd42012-10-31 22:52:31 +01004531 /* WADP0ClockGatingDisable */
4532 for_each_pipe(pipe) {
4533 I915_WRITE(TRANS_CHICKEN1(pipe),
4534 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
4535 }
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004536}
4537
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01004538static void gen6_check_mch_setup(struct drm_device *dev)
4539{
4540 struct drm_i915_private *dev_priv = dev->dev_private;
4541 uint32_t tmp;
4542
4543 tmp = I915_READ(MCH_SSKPD);
4544 if ((tmp & MCH_SSKPD_WM0_MASK) != MCH_SSKPD_WM0_VAL) {
4545 DRM_INFO("Wrong MCH_SSKPD value: 0x%08x\n", tmp);
4546 DRM_INFO("This can cause pipe underruns and display issues.\n");
4547 DRM_INFO("Please upgrade your BIOS to fix this.\n");
4548 }
4549}
4550
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03004551static void gen6_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004552{
4553 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau231e54f2012-10-19 17:55:41 +01004554 uint32_t dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004555
Damien Lespiau231e54f2012-10-19 17:55:41 +01004556 I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004557
4558 I915_WRITE(ILK_DISPLAY_CHICKEN2,
4559 I915_READ(ILK_DISPLAY_CHICKEN2) |
4560 ILK_ELPIN_409_SELECT);
4561
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01004562 /* WaDisableHiZPlanesWhenMSAAEnabled:snb */
Daniel Vetter42839082012-12-14 23:38:28 +01004563 I915_WRITE(_3D_CHICKEN,
4564 _MASKED_BIT_ENABLE(_3D_CHICKEN_HIZ_PLANE_DISABLE_MSAA_4X_SNB));
4565
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01004566 /* WaSetupGtModeTdRowDispatch:snb */
Daniel Vetter6547fbd2012-12-14 23:38:29 +01004567 if (IS_SNB_GT1(dev))
4568 I915_WRITE(GEN6_GT_MODE,
4569 _MASKED_BIT_ENABLE(GEN6_TD_FOUR_ROW_DISPATCH_DISABLE));
4570
Ville Syrjälä017636c2013-12-05 15:51:37 +02004571 ilk_init_lp_watermarks(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004572
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004573 I915_WRITE(CACHE_MODE_0,
Daniel Vetter50743292012-04-26 22:02:54 +02004574 _MASKED_BIT_DISABLE(CM0_STC_EVICT_DISABLE_LRA_SNB));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004575
4576 I915_WRITE(GEN6_UCGCTL1,
4577 I915_READ(GEN6_UCGCTL1) |
4578 GEN6_BLBUNIT_CLOCK_GATE_DISABLE |
4579 GEN6_CSUNIT_CLOCK_GATE_DISABLE);
4580
4581 /* According to the BSpec vol1g, bit 12 (RCPBUNIT) clock
4582 * gating disable must be set. Failure to set it results in
4583 * flickering pixels due to Z write ordering failures after
4584 * some amount of runtime in the Mesa "fire" demo, and Unigine
4585 * Sanctuary and Tropics, and apparently anything else with
4586 * alpha test or pixel discard.
4587 *
4588 * According to the spec, bit 11 (RCCUNIT) must also be set,
4589 * but we didn't debug actual testcases to find it out.
Jesse Barnes0f846f82012-06-14 11:04:47 -07004590 *
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01004591 * Also apply WaDisableVDSUnitClockGating:snb and
4592 * WaDisableRCPBUnitClockGating:snb.
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004593 */
4594 I915_WRITE(GEN6_UCGCTL2,
Jesse Barnes0f846f82012-06-14 11:04:47 -07004595 GEN7_VDSUNIT_CLOCK_GATE_DISABLE |
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004596 GEN6_RCPBUNIT_CLOCK_GATE_DISABLE |
4597 GEN6_RCCUNIT_CLOCK_GATE_DISABLE);
4598
4599 /* Bspec says we need to always set all mask bits. */
Kenneth Graunke26b6e442012-10-07 08:51:07 -07004600 I915_WRITE(_3D_CHICKEN3, (0xFFFF << 16) |
4601 _3D_CHICKEN3_SF_DISABLE_FASTCLIP_CULL);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004602
4603 /*
4604 * According to the spec the following bits should be
4605 * set in order to enable memory self-refresh and fbc:
4606 * The bit21 and bit22 of 0x42000
4607 * The bit21 and bit22 of 0x42004
4608 * The bit5 and bit7 of 0x42020
4609 * The bit14 of 0x70180
4610 * The bit14 of 0x71180
Damien Lespiau4bb35332013-06-14 15:23:24 +01004611 *
4612 * WaFbcAsynchFlipDisableFbcQueue:snb
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004613 */
4614 I915_WRITE(ILK_DISPLAY_CHICKEN1,
4615 I915_READ(ILK_DISPLAY_CHICKEN1) |
4616 ILK_FBCQ_DIS | ILK_PABSTRETCH_DIS);
4617 I915_WRITE(ILK_DISPLAY_CHICKEN2,
4618 I915_READ(ILK_DISPLAY_CHICKEN2) |
4619 ILK_DPARB_GATE | ILK_VSDPFD_FULL);
Damien Lespiau231e54f2012-10-19 17:55:41 +01004620 I915_WRITE(ILK_DSPCLK_GATE_D,
4621 I915_READ(ILK_DSPCLK_GATE_D) |
4622 ILK_DPARBUNIT_CLOCK_GATE_ENABLE |
4623 ILK_DPFDUNIT_CLOCK_GATE_ENABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004624
Ville Syrjälä0e088b82013-06-07 10:47:04 +03004625 g4x_disable_trickle_feed(dev);
Ben Widawskyf8f2ac92012-10-03 19:34:24 -07004626
4627 /* The default value should be 0x200 according to docs, but the two
4628 * platforms I checked have a 0 for this. (Maybe BIOS overrides?) */
4629 I915_WRITE(GEN6_GT_MODE, _MASKED_BIT_DISABLE(0xffff));
4630 I915_WRITE(GEN6_GT_MODE, _MASKED_BIT_ENABLE(GEN6_GT_MODE_HI));
Daniel Vetter3107bd42012-10-31 22:52:31 +01004631
4632 cpt_init_clock_gating(dev);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01004633
4634 gen6_check_mch_setup(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004635}
4636
4637static void gen7_setup_fixed_func_scheduler(struct drm_i915_private *dev_priv)
4638{
4639 uint32_t reg = I915_READ(GEN7_FF_THREAD_MODE);
4640
4641 reg &= ~GEN7_FF_SCHED_MASK;
4642 reg |= GEN7_FF_TS_SCHED_HW;
4643 reg |= GEN7_FF_VS_SCHED_HW;
4644 reg |= GEN7_FF_DS_SCHED_HW;
4645
Ben Widawsky41c0b3a2013-01-26 11:52:00 -08004646 if (IS_HASWELL(dev_priv->dev))
4647 reg &= ~GEN7_FF_VS_REF_CNT_FFME;
4648
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004649 I915_WRITE(GEN7_FF_THREAD_MODE, reg);
4650}
4651
Paulo Zanoni17a303e2012-11-20 15:12:07 -02004652static void lpt_init_clock_gating(struct drm_device *dev)
4653{
4654 struct drm_i915_private *dev_priv = dev->dev_private;
4655
4656 /*
4657 * TODO: this bit should only be enabled when really needed, then
4658 * disabled when not needed anymore in order to save power.
4659 */
4660 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE)
4661 I915_WRITE(SOUTH_DSPCLK_GATE_D,
4662 I915_READ(SOUTH_DSPCLK_GATE_D) |
4663 PCH_LP_PARTITION_LEVEL_DISABLE);
Paulo Zanoni0a790cd2013-04-17 18:15:49 -03004664
4665 /* WADPOClockGatingDisable:hsw */
4666 I915_WRITE(_TRANSA_CHICKEN1,
4667 I915_READ(_TRANSA_CHICKEN1) |
4668 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
Paulo Zanoni17a303e2012-11-20 15:12:07 -02004669}
4670
Imre Deak7d708ee2013-04-17 14:04:50 +03004671static void lpt_suspend_hw(struct drm_device *dev)
4672{
4673 struct drm_i915_private *dev_priv = dev->dev_private;
4674
4675 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
4676 uint32_t val = I915_READ(SOUTH_DSPCLK_GATE_D);
4677
4678 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
4679 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
4680 }
4681}
4682
Ben Widawsky1020a5c2013-11-02 21:07:06 -07004683static void gen8_init_clock_gating(struct drm_device *dev)
4684{
4685 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07004686 enum pipe i;
Ben Widawsky1020a5c2013-11-02 21:07:06 -07004687
4688 I915_WRITE(WM3_LP_ILK, 0);
4689 I915_WRITE(WM2_LP_ILK, 0);
4690 I915_WRITE(WM1_LP_ILK, 0);
Ben Widawsky50ed5fb2013-11-02 21:07:40 -07004691
4692 /* FIXME(BDW): Check all the w/a, some might only apply to
4693 * pre-production hw. */
4694
Ben Widawskyfd392b62013-11-04 22:52:39 -08004695 WARN(!i915_preliminary_hw_support,
4696 "GEN8_CENTROID_PIXEL_OPT_DIS not be needed for production\n");
4697 I915_WRITE(HALF_SLICE_CHICKEN3,
4698 _MASKED_BIT_ENABLE(GEN8_CENTROID_PIXEL_OPT_DIS));
Ben Widawskybf663472013-11-02 21:07:57 -07004699 I915_WRITE(HALF_SLICE_CHICKEN3,
4700 _MASKED_BIT_ENABLE(GEN8_SAMPLER_POWER_BYPASS_DIS));
Ben Widawsky4afe8d32013-11-02 21:07:55 -07004701 I915_WRITE(GAMTARBMODE, _MASKED_BIT_ENABLE(ARB_MODE_BWGTLB_DISABLE));
4702
Ben Widawsky7f88da02013-11-02 21:07:58 -07004703 I915_WRITE(_3D_CHICKEN3,
4704 _3D_CHICKEN_SDE_LIMIT_FIFO_POLY_DEPTH(2));
4705
Ben Widawskya75f3622013-11-02 21:07:59 -07004706 I915_WRITE(COMMON_SLICE_CHICKEN2,
4707 _MASKED_BIT_ENABLE(GEN8_CSC2_SBE_VUE_CACHE_CONSERVATIVE));
4708
Ben Widawsky4c2e7a52013-11-02 21:08:00 -07004709 I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
4710 _MASKED_BIT_ENABLE(GEN7_SINGLE_SUBSCAN_DISPATCH_ENABLE));
4711
Ben Widawskyab57fff2013-12-12 15:28:04 -08004712 /* WaSwitchSolVfFArbitrationPriority:bdw */
Ben Widawsky50ed5fb2013-11-02 21:07:40 -07004713 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07004714
Ben Widawskyab57fff2013-12-12 15:28:04 -08004715 /* WaPsrDPAMaskVBlankInSRD:bdw */
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07004716 I915_WRITE(CHICKEN_PAR1_1,
4717 I915_READ(CHICKEN_PAR1_1) | DPA_MASK_VBLANK_SRD);
4718
Ben Widawskyab57fff2013-12-12 15:28:04 -08004719 /* WaPsrDPRSUnmaskVBlankInSRD:bdw */
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07004720 for_each_pipe(i) {
4721 I915_WRITE(CHICKEN_PIPESL_1(i),
4722 I915_READ(CHICKEN_PIPESL_1(i) |
4723 DPRS_MASK_VBLANK_SRD));
4724 }
Ben Widawsky63801f22013-12-12 17:26:03 -08004725
4726 /* Use Force Non-Coherent whenever executing a 3D context. This is a
4727 * workaround for for a possible hang in the unlikely event a TLB
4728 * invalidation occurs during a PSD flush.
4729 */
4730 I915_WRITE(HDC_CHICKEN0,
4731 I915_READ(HDC_CHICKEN0) |
4732 _MASKED_BIT_ENABLE(HDC_FORCE_NON_COHERENT));
Ben Widawskyab57fff2013-12-12 15:28:04 -08004733
4734 /* WaVSRefCountFullforceMissDisable:bdw */
4735 /* WaDSRefCountFullforceMissDisable:bdw */
4736 I915_WRITE(GEN7_FF_THREAD_MODE,
4737 I915_READ(GEN7_FF_THREAD_MODE) &
4738 ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
Ben Widawsky1020a5c2013-11-02 21:07:06 -07004739}
4740
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03004741static void haswell_init_clock_gating(struct drm_device *dev)
4742{
4743 struct drm_i915_private *dev_priv = dev->dev_private;
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03004744
Ville Syrjälä017636c2013-12-05 15:51:37 +02004745 ilk_init_lp_watermarks(dev);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03004746
4747 /* According to the spec, bit 13 (RCZUNIT) must be set on IVB.
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01004748 * This implements the WaDisableRCZUnitClockGating:hsw workaround.
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03004749 */
4750 I915_WRITE(GEN6_UCGCTL2, GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
4751
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01004752 /* Apply the WaDisableRHWOOptimizationForRenderHang:hsw workaround. */
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03004753 I915_WRITE(GEN7_COMMON_SLICE_CHICKEN1,
4754 GEN7_CSC1_RHWO_OPT_DISABLE_IN_RCC);
4755
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01004756 /* WaApplyL3ControlAndL3ChickenMode:hsw */
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03004757 I915_WRITE(GEN7_L3CNTLREG1,
4758 GEN7_WA_FOR_GEN7_L3_CONTROL);
4759 I915_WRITE(GEN7_L3_CHICKEN_MODE_REGISTER,
4760 GEN7_WA_L3_CHICKEN_MODE);
4761
Francisco Jerezf3fc4882013-10-02 15:53:16 -07004762 /* L3 caching of data atomics doesn't work -- disable it. */
4763 I915_WRITE(HSW_SCRATCH1, HSW_SCRATCH1_L3_DATA_ATOMICS_DISABLE);
4764 I915_WRITE(HSW_ROW_CHICKEN3,
4765 _MASKED_BIT_ENABLE(HSW_ROW_CHICKEN3_L3_GLOBAL_ATOMICS_DISABLE));
4766
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01004767 /* This is required by WaCatErrorRejectionIssue:hsw */
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03004768 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
4769 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
4770 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
4771
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01004772 /* WaVSRefCountFullforceMissDisable:hsw */
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03004773 gen7_setup_fixed_func_scheduler(dev_priv);
4774
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01004775 /* WaDisable4x2SubspanOptimization:hsw */
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03004776 I915_WRITE(CACHE_MODE_1,
4777 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Eugeni Dodonov1544d9d2012-07-02 11:51:10 -03004778
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01004779 /* WaSwitchSolVfFArbitrationPriority:hsw */
Ben Widawskye3dff582013-03-20 14:49:14 -07004780 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
4781
Paulo Zanoni90a88642013-05-03 17:23:45 -03004782 /* WaRsPkgCStateDisplayPMReq:hsw */
4783 I915_WRITE(CHICKEN_PAR1_1,
4784 I915_READ(CHICKEN_PAR1_1) | FORCE_ARB_IDLE_PLANES);
Eugeni Dodonov1544d9d2012-07-02 11:51:10 -03004785
Paulo Zanoni17a303e2012-11-20 15:12:07 -02004786 lpt_init_clock_gating(dev);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03004787}
4788
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03004789static void ivybridge_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004790{
4791 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawsky20848222012-05-04 18:58:59 -07004792 uint32_t snpcr;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004793
Ville Syrjälä017636c2013-12-05 15:51:37 +02004794 ilk_init_lp_watermarks(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004795
Damien Lespiau231e54f2012-10-19 17:55:41 +01004796 I915_WRITE(ILK_DSPCLK_GATE_D, ILK_VRHUNIT_CLOCK_GATE_DISABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004797
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01004798 /* WaDisableEarlyCull:ivb */
Jesse Barnes87f80202012-10-02 17:43:41 -05004799 I915_WRITE(_3D_CHICKEN3,
4800 _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
4801
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01004802 /* WaDisableBackToBackFlipFix:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004803 I915_WRITE(IVB_CHICKEN3,
4804 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
4805 CHICKEN3_DGMG_DONE_FIX_DISABLE);
4806
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01004807 /* WaDisablePSDDualDispatchEnable:ivb */
Jesse Barnes12f33822012-10-25 12:15:45 -07004808 if (IS_IVB_GT1(dev))
4809 I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
4810 _MASKED_BIT_ENABLE(GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
4811 else
4812 I915_WRITE(GEN7_HALF_SLICE_CHICKEN1_GT2,
4813 _MASKED_BIT_ENABLE(GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
4814
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01004815 /* Apply the WaDisableRHWOOptimizationForRenderHang:ivb workaround. */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004816 I915_WRITE(GEN7_COMMON_SLICE_CHICKEN1,
4817 GEN7_CSC1_RHWO_OPT_DISABLE_IN_RCC);
4818
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01004819 /* WaApplyL3ControlAndL3ChickenMode:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004820 I915_WRITE(GEN7_L3CNTLREG1,
4821 GEN7_WA_FOR_GEN7_L3_CONTROL);
4822 I915_WRITE(GEN7_L3_CHICKEN_MODE_REGISTER,
Jesse Barnes8ab43972012-10-25 12:15:42 -07004823 GEN7_WA_L3_CHICKEN_MODE);
4824 if (IS_IVB_GT1(dev))
4825 I915_WRITE(GEN7_ROW_CHICKEN2,
4826 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
4827 else
4828 I915_WRITE(GEN7_ROW_CHICKEN2_GT2,
4829 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
4830
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004831
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01004832 /* WaForceL3Serialization:ivb */
Jesse Barnes61939d92012-10-02 17:43:38 -05004833 I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
4834 ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
4835
Jesse Barnes0f846f82012-06-14 11:04:47 -07004836 /* According to the BSpec vol1g, bit 12 (RCPBUNIT) clock
4837 * gating disable must be set. Failure to set it results in
4838 * flickering pixels due to Z write ordering failures after
4839 * some amount of runtime in the Mesa "fire" demo, and Unigine
4840 * Sanctuary and Tropics, and apparently anything else with
4841 * alpha test or pixel discard.
4842 *
4843 * According to the spec, bit 11 (RCCUNIT) must also be set,
4844 * but we didn't debug actual testcases to find it out.
4845 *
4846 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01004847 * This implements the WaDisableRCZUnitClockGating:ivb workaround.
Jesse Barnes0f846f82012-06-14 11:04:47 -07004848 */
4849 I915_WRITE(GEN6_UCGCTL2,
4850 GEN6_RCZUNIT_CLOCK_GATE_DISABLE |
4851 GEN6_RCCUNIT_CLOCK_GATE_DISABLE);
4852
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01004853 /* This is required by WaCatErrorRejectionIssue:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004854 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
4855 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
4856 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
4857
Ville Syrjälä0e088b82013-06-07 10:47:04 +03004858 g4x_disable_trickle_feed(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004859
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01004860 /* WaVSRefCountFullforceMissDisable:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004861 gen7_setup_fixed_func_scheduler(dev_priv);
Daniel Vetter97e19302012-04-24 16:00:21 +02004862
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01004863 /* WaDisable4x2SubspanOptimization:ivb */
Daniel Vetter97e19302012-04-24 16:00:21 +02004864 I915_WRITE(CACHE_MODE_1,
4865 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Ben Widawsky20848222012-05-04 18:58:59 -07004866
4867 snpcr = I915_READ(GEN6_MBCUNIT_SNPCR);
4868 snpcr &= ~GEN6_MBC_SNPCR_MASK;
4869 snpcr |= GEN6_MBC_SNPCR_MED;
4870 I915_WRITE(GEN6_MBCUNIT_SNPCR, snpcr);
Daniel Vetter3107bd42012-10-31 22:52:31 +01004871
Ben Widawskyab5c6082013-04-05 13:12:41 -07004872 if (!HAS_PCH_NOP(dev))
4873 cpt_init_clock_gating(dev);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01004874
4875 gen6_check_mch_setup(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004876}
4877
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03004878static void valleyview_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004879{
4880 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes85b1d7b2013-11-04 11:52:45 -08004881 u32 val;
4882
4883 mutex_lock(&dev_priv->rps.hw_lock);
4884 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
4885 mutex_unlock(&dev_priv->rps.hw_lock);
4886 switch ((val >> 6) & 3) {
4887 case 0:
Jesse Barnes85b1d7b2013-11-04 11:52:45 -08004888 dev_priv->mem_freq = 800;
4889 break;
Jesse Barnesf64a28a2013-11-04 16:07:00 -08004890 case 1:
Jesse Barnes85b1d7b2013-11-04 11:52:45 -08004891 dev_priv->mem_freq = 1066;
4892 break;
Jesse Barnesf64a28a2013-11-04 16:07:00 -08004893 case 2:
Jesse Barnes85b1d7b2013-11-04 11:52:45 -08004894 dev_priv->mem_freq = 1333;
4895 break;
Jesse Barnesf64a28a2013-11-04 16:07:00 -08004896 case 3:
Chon Ming Lee23259912013-11-07 15:23:26 +08004897 dev_priv->mem_freq = 1333;
Jesse Barnesf64a28a2013-11-04 16:07:00 -08004898 break;
Jesse Barnes85b1d7b2013-11-04 11:52:45 -08004899 }
4900 DRM_DEBUG_DRIVER("DDR speed: %d MHz", dev_priv->mem_freq);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004901
Ville Syrjäläd7fe0cc2013-05-21 18:01:50 +03004902 I915_WRITE(DSPCLK_GATE_D, VRHUNIT_CLOCK_GATE_DISABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004903
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01004904 /* WaDisableEarlyCull:vlv */
Jesse Barnes87f80202012-10-02 17:43:41 -05004905 I915_WRITE(_3D_CHICKEN3,
4906 _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
4907
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01004908 /* WaDisableBackToBackFlipFix:vlv */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004909 I915_WRITE(IVB_CHICKEN3,
4910 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
4911 CHICKEN3_DGMG_DONE_FIX_DISABLE);
4912
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01004913 /* WaDisablePSDDualDispatchEnable:vlv */
Jesse Barnes12f33822012-10-25 12:15:45 -07004914 I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
Jesse Barnesd3bc0302013-03-08 10:45:51 -08004915 _MASKED_BIT_ENABLE(GEN7_MAX_PS_THREAD_DEP |
4916 GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
Jesse Barnes12f33822012-10-25 12:15:45 -07004917
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01004918 /* Apply the WaDisableRHWOOptimizationForRenderHang:vlv workaround. */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004919 I915_WRITE(GEN7_COMMON_SLICE_CHICKEN1,
4920 GEN7_CSC1_RHWO_OPT_DISABLE_IN_RCC);
4921
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01004922 /* WaApplyL3ControlAndL3ChickenMode:vlv */
Jesse Barnesd0cf5ea2012-10-25 12:15:41 -07004923 I915_WRITE(GEN7_L3CNTLREG1, I915_READ(GEN7_L3CNTLREG1) | GEN7_L3AGDIS);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004924 I915_WRITE(GEN7_L3_CHICKEN_MODE_REGISTER, GEN7_WA_L3_CHICKEN_MODE);
4925
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01004926 /* WaForceL3Serialization:vlv */
Jesse Barnes61939d92012-10-02 17:43:38 -05004927 I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
4928 ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
4929
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01004930 /* WaDisableDopClockGating:vlv */
Jesse Barnes8ab43972012-10-25 12:15:42 -07004931 I915_WRITE(GEN7_ROW_CHICKEN2,
4932 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
4933
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01004934 /* This is required by WaCatErrorRejectionIssue:vlv */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004935 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
4936 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
4937 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
4938
Jesse Barnes0f846f82012-06-14 11:04:47 -07004939 /* According to the BSpec vol1g, bit 12 (RCPBUNIT) clock
4940 * gating disable must be set. Failure to set it results in
4941 * flickering pixels due to Z write ordering failures after
4942 * some amount of runtime in the Mesa "fire" demo, and Unigine
4943 * Sanctuary and Tropics, and apparently anything else with
4944 * alpha test or pixel discard.
4945 *
4946 * According to the spec, bit 11 (RCCUNIT) must also be set,
4947 * but we didn't debug actual testcases to find it out.
4948 *
4949 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01004950 * This implements the WaDisableRCZUnitClockGating:vlv workaround.
Jesse Barnes0f846f82012-06-14 11:04:47 -07004951 *
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01004952 * Also apply WaDisableVDSUnitClockGating:vlv and
4953 * WaDisableRCPBUnitClockGating:vlv.
Jesse Barnes0f846f82012-06-14 11:04:47 -07004954 */
4955 I915_WRITE(GEN6_UCGCTL2,
4956 GEN7_VDSUNIT_CLOCK_GATE_DISABLE |
Jesse Barnes6edaa7f2012-06-14 11:04:49 -07004957 GEN7_TDLUNIT_CLOCK_GATE_DISABLE |
Jesse Barnes0f846f82012-06-14 11:04:47 -07004958 GEN6_RCZUNIT_CLOCK_GATE_DISABLE |
4959 GEN6_RCPBUNIT_CLOCK_GATE_DISABLE |
4960 GEN6_RCCUNIT_CLOCK_GATE_DISABLE);
4961
Jesse Barnese3f33d42012-06-14 11:04:50 -07004962 I915_WRITE(GEN7_UCGCTL4, GEN7_L3BANK2X_CLOCK_GATE_DISABLE);
4963
Ville Syrjäläe0d8d592013-06-12 22:11:18 +03004964 I915_WRITE(MI_ARB_VLV, MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004965
Daniel Vetter6b26c862012-04-24 14:04:12 +02004966 I915_WRITE(CACHE_MODE_1,
4967 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Jesse Barnes79831172012-06-20 10:53:12 -07004968
4969 /*
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01004970 * WaDisableVLVClockGating_VBIIssue:vlv
Jesse Barnes2d809572012-10-25 12:15:44 -07004971 * Disable clock gating on th GCFG unit to prevent a delay
4972 * in the reporting of vblank events.
4973 */
Jesse Barnes4e8c84a2013-03-08 10:45:54 -08004974 I915_WRITE(VLV_GUNIT_CLOCK_GATE, 0xffffffff);
4975
4976 /* Conservative clock gating settings for now */
4977 I915_WRITE(0x9400, 0xffffffff);
4978 I915_WRITE(0x9404, 0xffffffff);
4979 I915_WRITE(0x9408, 0xffffffff);
4980 I915_WRITE(0x940c, 0xffffffff);
4981 I915_WRITE(0x9410, 0xffffffff);
4982 I915_WRITE(0x9414, 0xffffffff);
4983 I915_WRITE(0x9418, 0xffffffff);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004984}
4985
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03004986static void g4x_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004987{
4988 struct drm_i915_private *dev_priv = dev->dev_private;
4989 uint32_t dspclk_gate;
4990
4991 I915_WRITE(RENCLK_GATE_D1, 0);
4992 I915_WRITE(RENCLK_GATE_D2, VF_UNIT_CLOCK_GATE_DISABLE |
4993 GS_UNIT_CLOCK_GATE_DISABLE |
4994 CL_UNIT_CLOCK_GATE_DISABLE);
4995 I915_WRITE(RAMCLK_GATE_D, 0);
4996 dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE |
4997 OVRUNIT_CLOCK_GATE_DISABLE |
4998 OVCUNIT_CLOCK_GATE_DISABLE;
4999 if (IS_GM45(dev))
5000 dspclk_gate |= DSSUNIT_CLOCK_GATE_DISABLE;
5001 I915_WRITE(DSPCLK_GATE_D, dspclk_gate);
Daniel Vetter4358a372012-10-18 11:49:51 +02005002
5003 /* WaDisableRenderCachePipelinedFlush */
5004 I915_WRITE(CACHE_MODE_0,
5005 _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
Ville Syrjäläde1aa622013-06-07 10:47:01 +03005006
Ville Syrjälä0e088b82013-06-07 10:47:04 +03005007 g4x_disable_trickle_feed(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005008}
5009
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005010static void crestline_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005011{
5012 struct drm_i915_private *dev_priv = dev->dev_private;
5013
5014 I915_WRITE(RENCLK_GATE_D1, I965_RCC_CLOCK_GATE_DISABLE);
5015 I915_WRITE(RENCLK_GATE_D2, 0);
5016 I915_WRITE(DSPCLK_GATE_D, 0);
5017 I915_WRITE(RAMCLK_GATE_D, 0);
5018 I915_WRITE16(DEUC, 0);
Ville Syrjälä20f94962013-06-07 10:47:02 +03005019 I915_WRITE(MI_ARB_STATE,
5020 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005021}
5022
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005023static void broadwater_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005024{
5025 struct drm_i915_private *dev_priv = dev->dev_private;
5026
5027 I915_WRITE(RENCLK_GATE_D1, I965_RCZ_CLOCK_GATE_DISABLE |
5028 I965_RCC_CLOCK_GATE_DISABLE |
5029 I965_RCPB_CLOCK_GATE_DISABLE |
5030 I965_ISC_CLOCK_GATE_DISABLE |
5031 I965_FBC_CLOCK_GATE_DISABLE);
5032 I915_WRITE(RENCLK_GATE_D2, 0);
Ville Syrjälä20f94962013-06-07 10:47:02 +03005033 I915_WRITE(MI_ARB_STATE,
5034 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005035}
5036
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005037static void gen3_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005038{
5039 struct drm_i915_private *dev_priv = dev->dev_private;
5040 u32 dstate = I915_READ(D_STATE);
5041
5042 dstate |= DSTATE_PLL_D3_OFF | DSTATE_GFX_CLOCK_GATING |
5043 DSTATE_DOT_CLOCK_GATING;
5044 I915_WRITE(D_STATE, dstate);
Chris Wilson13a86b82012-04-24 14:51:43 +01005045
5046 if (IS_PINEVIEW(dev))
5047 I915_WRITE(ECOSKPD, _MASKED_BIT_ENABLE(ECO_GATING_CX_ONLY));
Daniel Vetter974a3b02012-09-09 11:54:16 +02005048
5049 /* IIR "flip pending" means done if this bit is set */
5050 I915_WRITE(ECOSKPD, _MASKED_BIT_DISABLE(ECO_FLIP_DONE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005051}
5052
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005053static void i85x_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005054{
5055 struct drm_i915_private *dev_priv = dev->dev_private;
5056
5057 I915_WRITE(RENCLK_GATE_D1, SV_CLOCK_GATE_DISABLE);
5058}
5059
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005060static void i830_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005061{
5062 struct drm_i915_private *dev_priv = dev->dev_private;
5063
5064 I915_WRITE(DSPCLK_GATE_D, OVRUNIT_CLOCK_GATE_DISABLE);
5065}
5066
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005067void intel_init_clock_gating(struct drm_device *dev)
5068{
5069 struct drm_i915_private *dev_priv = dev->dev_private;
5070
5071 dev_priv->display.init_clock_gating(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005072}
5073
Imre Deak7d708ee2013-04-17 14:04:50 +03005074void intel_suspend_hw(struct drm_device *dev)
5075{
5076 if (HAS_PCH_LPT(dev))
5077 lpt_suspend_hw(dev);
5078}
5079
Imre Deakc1ca7272013-11-25 17:15:29 +02005080#define for_each_power_well(i, power_well, domain_mask, power_domains) \
5081 for (i = 0; \
5082 i < (power_domains)->power_well_count && \
5083 ((power_well) = &(power_domains)->power_wells[i]); \
5084 i++) \
5085 if ((power_well)->domains & (domain_mask))
5086
5087#define for_each_power_well_rev(i, power_well, domain_mask, power_domains) \
5088 for (i = (power_domains)->power_well_count - 1; \
5089 i >= 0 && ((power_well) = &(power_domains)->power_wells[i]);\
5090 i--) \
5091 if ((power_well)->domains & (domain_mask))
5092
Paulo Zanoni15d199e2013-03-22 14:14:13 -03005093/**
5094 * We should only use the power well if we explicitly asked the hardware to
5095 * enable it, so check if it's enabled and also check if we've requested it to
5096 * be enabled.
5097 */
Imre Deakc1ca7272013-11-25 17:15:29 +02005098static bool hsw_power_well_enabled(struct drm_device *dev,
5099 struct i915_power_well *power_well)
5100{
5101 struct drm_i915_private *dev_priv = dev->dev_private;
5102
5103 return I915_READ(HSW_PWR_WELL_DRIVER) ==
5104 (HSW_PWR_WELL_ENABLE_REQUEST | HSW_PWR_WELL_STATE_ENABLED);
5105}
5106
Imre Deakddf9c532013-11-27 22:02:02 +02005107bool intel_display_power_enabled_sw(struct drm_device *dev,
5108 enum intel_display_power_domain domain)
5109{
5110 struct drm_i915_private *dev_priv = dev->dev_private;
5111 struct i915_power_domains *power_domains;
5112
5113 power_domains = &dev_priv->power_domains;
5114
5115 return power_domains->domain_use_count[domain];
5116}
5117
Paulo Zanonib97186f2013-05-03 12:15:36 -03005118bool intel_display_power_enabled(struct drm_device *dev,
5119 enum intel_display_power_domain domain)
Paulo Zanoni15d199e2013-03-22 14:14:13 -03005120{
5121 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deakc1ca7272013-11-25 17:15:29 +02005122 struct i915_power_domains *power_domains;
5123 struct i915_power_well *power_well;
5124 bool is_enabled;
5125 int i;
Paulo Zanoni15d199e2013-03-22 14:14:13 -03005126
Imre Deakc1ca7272013-11-25 17:15:29 +02005127 power_domains = &dev_priv->power_domains;
5128
5129 is_enabled = true;
5130
5131 mutex_lock(&power_domains->lock);
5132 for_each_power_well_rev(i, power_well, BIT(domain), power_domains) {
Imre Deak6f3ef5d2013-11-25 17:15:30 +02005133 if (power_well->always_on)
5134 continue;
5135
Imre Deakc1ca7272013-11-25 17:15:29 +02005136 if (!power_well->is_enabled(dev, power_well)) {
5137 is_enabled = false;
5138 break;
5139 }
5140 }
5141 mutex_unlock(&power_domains->lock);
5142
5143 return is_enabled;
Paulo Zanoni15d199e2013-03-22 14:14:13 -03005144}
5145
Paulo Zanonid5e8fdc2013-12-11 18:50:09 -02005146static void hsw_power_well_post_enable(struct drm_i915_private *dev_priv)
5147{
5148 struct drm_device *dev = dev_priv->dev;
5149 unsigned long irqflags;
5150
Paulo Zanonif9dcb0d2013-12-11 18:50:10 -02005151 /*
5152 * After we re-enable the power well, if we touch VGA register 0x3d5
5153 * we'll get unclaimed register interrupts. This stops after we write
5154 * anything to the VGA MSR register. The vgacon module uses this
5155 * register all the time, so if we unbind our driver and, as a
5156 * consequence, bind vgacon, we'll get stuck in an infinite loop at
5157 * console_unlock(). So make here we touch the VGA MSR register, making
5158 * sure vgacon can keep working normally without triggering interrupts
5159 * and error messages.
5160 */
5161 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
5162 outb(inb(VGA_MSR_READ), VGA_MSR_WRITE);
5163 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
5164
Paulo Zanonid5e8fdc2013-12-11 18:50:09 -02005165 if (IS_BROADWELL(dev)) {
5166 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
5167 I915_WRITE(GEN8_DE_PIPE_IMR(PIPE_B),
5168 dev_priv->de_irq_mask[PIPE_B]);
5169 I915_WRITE(GEN8_DE_PIPE_IER(PIPE_B),
5170 ~dev_priv->de_irq_mask[PIPE_B] |
5171 GEN8_PIPE_VBLANK);
5172 I915_WRITE(GEN8_DE_PIPE_IMR(PIPE_C),
5173 dev_priv->de_irq_mask[PIPE_C]);
5174 I915_WRITE(GEN8_DE_PIPE_IER(PIPE_C),
5175 ~dev_priv->de_irq_mask[PIPE_C] |
5176 GEN8_PIPE_VBLANK);
5177 POSTING_READ(GEN8_DE_PIPE_IER(PIPE_C));
5178 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
5179 }
5180}
5181
5182static void hsw_power_well_post_disable(struct drm_i915_private *dev_priv)
5183{
5184 struct drm_device *dev = dev_priv->dev;
5185 enum pipe p;
5186 unsigned long irqflags;
5187
5188 /*
5189 * After this, the registers on the pipes that are part of the power
5190 * well will become zero, so we have to adjust our counters according to
5191 * that.
5192 *
5193 * FIXME: Should we do this in general in drm_vblank_post_modeset?
5194 */
5195 spin_lock_irqsave(&dev->vbl_lock, irqflags);
5196 for_each_pipe(p)
5197 if (p != PIPE_A)
5198 dev->vblank[p].last = 0;
5199 spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
5200}
5201
Imre Deakc1ca7272013-11-25 17:15:29 +02005202static void hsw_set_power_well(struct drm_device *dev,
5203 struct i915_power_well *power_well, bool enable)
Eugeni Dodonovd0d3e512012-05-09 15:37:16 -03005204{
5205 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanonifa42e232013-01-25 16:59:11 -02005206 bool is_enabled, enable_requested;
5207 uint32_t tmp;
Eugeni Dodonovd0d3e512012-05-09 15:37:16 -03005208
Paulo Zanonid62292c2013-11-27 17:59:22 -02005209 WARN_ON(dev_priv->pc8.enabled);
5210
Paulo Zanonifa42e232013-01-25 16:59:11 -02005211 tmp = I915_READ(HSW_PWR_WELL_DRIVER);
Paulo Zanoni6aedd1f2013-08-02 16:22:25 -03005212 is_enabled = tmp & HSW_PWR_WELL_STATE_ENABLED;
5213 enable_requested = tmp & HSW_PWR_WELL_ENABLE_REQUEST;
Eugeni Dodonovd0d3e512012-05-09 15:37:16 -03005214
Paulo Zanonifa42e232013-01-25 16:59:11 -02005215 if (enable) {
5216 if (!enable_requested)
Paulo Zanoni6aedd1f2013-08-02 16:22:25 -03005217 I915_WRITE(HSW_PWR_WELL_DRIVER,
5218 HSW_PWR_WELL_ENABLE_REQUEST);
Eugeni Dodonovd0d3e512012-05-09 15:37:16 -03005219
Paulo Zanonifa42e232013-01-25 16:59:11 -02005220 if (!is_enabled) {
5221 DRM_DEBUG_KMS("Enabling power well\n");
5222 if (wait_for((I915_READ(HSW_PWR_WELL_DRIVER) &
Paulo Zanoni6aedd1f2013-08-02 16:22:25 -03005223 HSW_PWR_WELL_STATE_ENABLED), 20))
Paulo Zanonifa42e232013-01-25 16:59:11 -02005224 DRM_ERROR("Timeout enabling power well\n");
5225 }
Ben Widawsky596cc112013-11-11 14:46:28 -08005226
Paulo Zanonid5e8fdc2013-12-11 18:50:09 -02005227 hsw_power_well_post_enable(dev_priv);
Paulo Zanonifa42e232013-01-25 16:59:11 -02005228 } else {
5229 if (enable_requested) {
5230 I915_WRITE(HSW_PWR_WELL_DRIVER, 0);
Paulo Zanoni9dbd8fe2013-07-23 10:48:11 -03005231 POSTING_READ(HSW_PWR_WELL_DRIVER);
Paulo Zanonifa42e232013-01-25 16:59:11 -02005232 DRM_DEBUG_KMS("Requesting to disable the power well\n");
Paulo Zanoni9dbd8fe2013-07-23 10:48:11 -03005233
Paulo Zanonid5e8fdc2013-12-11 18:50:09 -02005234 hsw_power_well_post_disable(dev_priv);
Eugeni Dodonovd0d3e512012-05-09 15:37:16 -03005235 }
5236 }
Paulo Zanonifa42e232013-01-25 16:59:11 -02005237}
Eugeni Dodonovd0d3e512012-05-09 15:37:16 -03005238
Imre Deakb4ed4482013-10-25 17:36:49 +03005239static void __intel_power_well_get(struct drm_device *dev,
5240 struct i915_power_well *power_well)
Ville Syrjälä2d66aef2013-09-16 17:38:29 +03005241{
Paulo Zanonid62292c2013-11-27 17:59:22 -02005242 struct drm_i915_private *dev_priv = dev->dev_private;
5243
5244 if (!power_well->count++ && power_well->set) {
5245 hsw_disable_package_c8(dev_priv);
Imre Deakc1ca7272013-11-25 17:15:29 +02005246 power_well->set(dev, power_well, true);
Paulo Zanonid62292c2013-11-27 17:59:22 -02005247 }
Ville Syrjälä2d66aef2013-09-16 17:38:29 +03005248}
5249
Imre Deakb4ed4482013-10-25 17:36:49 +03005250static void __intel_power_well_put(struct drm_device *dev,
5251 struct i915_power_well *power_well)
Ville Syrjälä2d66aef2013-09-16 17:38:29 +03005252{
Paulo Zanonid62292c2013-11-27 17:59:22 -02005253 struct drm_i915_private *dev_priv = dev->dev_private;
5254
Ville Syrjälä2d66aef2013-09-16 17:38:29 +03005255 WARN_ON(!power_well->count);
Imre Deakc1ca7272013-11-25 17:15:29 +02005256
Paulo Zanonid62292c2013-11-27 17:59:22 -02005257 if (!--power_well->count && power_well->set &&
5258 i915_disable_power_well) {
Imre Deakc1ca7272013-11-25 17:15:29 +02005259 power_well->set(dev, power_well, false);
Paulo Zanonid62292c2013-11-27 17:59:22 -02005260 hsw_enable_package_c8(dev_priv);
5261 }
Ville Syrjälä2d66aef2013-09-16 17:38:29 +03005262}
5263
Ville Syrjälä67656252013-09-16 17:38:28 +03005264void intel_display_power_get(struct drm_device *dev,
5265 enum intel_display_power_domain domain)
5266{
5267 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak83c00f52013-10-25 17:36:47 +03005268 struct i915_power_domains *power_domains;
Imre Deakc1ca7272013-11-25 17:15:29 +02005269 struct i915_power_well *power_well;
5270 int i;
Ville Syrjälä67656252013-09-16 17:38:28 +03005271
Imre Deak83c00f52013-10-25 17:36:47 +03005272 power_domains = &dev_priv->power_domains;
5273
5274 mutex_lock(&power_domains->lock);
Imre Deak1da51582013-11-25 17:15:35 +02005275
Imre Deakc1ca7272013-11-25 17:15:29 +02005276 for_each_power_well(i, power_well, BIT(domain), power_domains)
5277 __intel_power_well_get(dev, power_well);
Imre Deak1da51582013-11-25 17:15:35 +02005278
Imre Deakddf9c532013-11-27 22:02:02 +02005279 power_domains->domain_use_count[domain]++;
5280
Imre Deak83c00f52013-10-25 17:36:47 +03005281 mutex_unlock(&power_domains->lock);
Ville Syrjälä67656252013-09-16 17:38:28 +03005282}
5283
5284void intel_display_power_put(struct drm_device *dev,
5285 enum intel_display_power_domain domain)
5286{
5287 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak83c00f52013-10-25 17:36:47 +03005288 struct i915_power_domains *power_domains;
Imre Deakc1ca7272013-11-25 17:15:29 +02005289 struct i915_power_well *power_well;
5290 int i;
Ville Syrjälä67656252013-09-16 17:38:28 +03005291
Imre Deak83c00f52013-10-25 17:36:47 +03005292 power_domains = &dev_priv->power_domains;
5293
5294 mutex_lock(&power_domains->lock);
Imre Deak1da51582013-11-25 17:15:35 +02005295
Imre Deak1da51582013-11-25 17:15:35 +02005296 WARN_ON(!power_domains->domain_use_count[domain]);
5297 power_domains->domain_use_count[domain]--;
Imre Deakddf9c532013-11-27 22:02:02 +02005298
5299 for_each_power_well_rev(i, power_well, BIT(domain), power_domains)
5300 __intel_power_well_put(dev, power_well);
Imre Deak1da51582013-11-25 17:15:35 +02005301
Imre Deak83c00f52013-10-25 17:36:47 +03005302 mutex_unlock(&power_domains->lock);
Ville Syrjälä67656252013-09-16 17:38:28 +03005303}
5304
Imre Deak83c00f52013-10-25 17:36:47 +03005305static struct i915_power_domains *hsw_pwr;
Wang Xingchaoa38911a2013-05-30 22:07:11 +08005306
5307/* Display audio driver power well request */
5308void i915_request_power_well(void)
5309{
Imre Deakb4ed4482013-10-25 17:36:49 +03005310 struct drm_i915_private *dev_priv;
5311
Wang Xingchaoa38911a2013-05-30 22:07:11 +08005312 if (WARN_ON(!hsw_pwr))
5313 return;
5314
Imre Deakb4ed4482013-10-25 17:36:49 +03005315 dev_priv = container_of(hsw_pwr, struct drm_i915_private,
5316 power_domains);
Imre Deakfbeeaa22013-11-25 17:15:28 +02005317 intel_display_power_get(dev_priv->dev, POWER_DOMAIN_AUDIO);
Wang Xingchaoa38911a2013-05-30 22:07:11 +08005318}
5319EXPORT_SYMBOL_GPL(i915_request_power_well);
5320
5321/* Display audio driver power well release */
5322void i915_release_power_well(void)
5323{
Imre Deakb4ed4482013-10-25 17:36:49 +03005324 struct drm_i915_private *dev_priv;
5325
Wang Xingchaoa38911a2013-05-30 22:07:11 +08005326 if (WARN_ON(!hsw_pwr))
5327 return;
5328
Imre Deakb4ed4482013-10-25 17:36:49 +03005329 dev_priv = container_of(hsw_pwr, struct drm_i915_private,
5330 power_domains);
Imre Deakfbeeaa22013-11-25 17:15:28 +02005331 intel_display_power_put(dev_priv->dev, POWER_DOMAIN_AUDIO);
Wang Xingchaoa38911a2013-05-30 22:07:11 +08005332}
5333EXPORT_SYMBOL_GPL(i915_release_power_well);
5334
Imre Deak1c2256d2013-11-25 17:15:34 +02005335static struct i915_power_well i9xx_always_on_power_well[] = {
5336 {
5337 .name = "always-on",
5338 .always_on = 1,
5339 .domains = POWER_DOMAIN_MASK,
5340 },
5341};
5342
Imre Deakc1ca7272013-11-25 17:15:29 +02005343static struct i915_power_well hsw_power_wells[] = {
5344 {
Imre Deak6f3ef5d2013-11-25 17:15:30 +02005345 .name = "always-on",
5346 .always_on = 1,
5347 .domains = HSW_ALWAYS_ON_POWER_DOMAINS,
5348 },
5349 {
Imre Deakc1ca7272013-11-25 17:15:29 +02005350 .name = "display",
5351 .domains = POWER_DOMAIN_MASK & ~HSW_ALWAYS_ON_POWER_DOMAINS,
5352 .is_enabled = hsw_power_well_enabled,
5353 .set = hsw_set_power_well,
5354 },
5355};
5356
5357static struct i915_power_well bdw_power_wells[] = {
5358 {
Imre Deak6f3ef5d2013-11-25 17:15:30 +02005359 .name = "always-on",
5360 .always_on = 1,
5361 .domains = BDW_ALWAYS_ON_POWER_DOMAINS,
5362 },
5363 {
Imre Deakc1ca7272013-11-25 17:15:29 +02005364 .name = "display",
5365 .domains = POWER_DOMAIN_MASK & ~BDW_ALWAYS_ON_POWER_DOMAINS,
5366 .is_enabled = hsw_power_well_enabled,
5367 .set = hsw_set_power_well,
5368 },
5369};
5370
5371#define set_power_wells(power_domains, __power_wells) ({ \
5372 (power_domains)->power_wells = (__power_wells); \
5373 (power_domains)->power_well_count = ARRAY_SIZE(__power_wells); \
5374})
5375
Imre Deakddb642f2013-10-28 17:20:35 +02005376int intel_power_domains_init(struct drm_device *dev)
Wang Xingchaoa38911a2013-05-30 22:07:11 +08005377{
5378 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak83c00f52013-10-25 17:36:47 +03005379 struct i915_power_domains *power_domains = &dev_priv->power_domains;
Imre Deakc1ca7272013-11-25 17:15:29 +02005380
Imre Deak83c00f52013-10-25 17:36:47 +03005381 mutex_init(&power_domains->lock);
Wang Xingchaoa38911a2013-05-30 22:07:11 +08005382
Imre Deakc1ca7272013-11-25 17:15:29 +02005383 /*
5384 * The enabling order will be from lower to higher indexed wells,
5385 * the disabling order is reversed.
5386 */
5387 if (IS_HASWELL(dev)) {
5388 set_power_wells(power_domains, hsw_power_wells);
5389 hsw_pwr = power_domains;
5390 } else if (IS_BROADWELL(dev)) {
5391 set_power_wells(power_domains, bdw_power_wells);
5392 hsw_pwr = power_domains;
5393 } else {
Imre Deak1c2256d2013-11-25 17:15:34 +02005394 set_power_wells(power_domains, i9xx_always_on_power_well);
Imre Deakc1ca7272013-11-25 17:15:29 +02005395 }
Wang Xingchaoa38911a2013-05-30 22:07:11 +08005396
5397 return 0;
5398}
5399
Imre Deakddb642f2013-10-28 17:20:35 +02005400void intel_power_domains_remove(struct drm_device *dev)
Wang Xingchaoa38911a2013-05-30 22:07:11 +08005401{
5402 hsw_pwr = NULL;
5403}
5404
Imre Deakddb642f2013-10-28 17:20:35 +02005405static void intel_power_domains_resume(struct drm_device *dev)
Ville Syrjälä9cdb8262013-09-16 17:38:27 +03005406{
5407 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak83c00f52013-10-25 17:36:47 +03005408 struct i915_power_domains *power_domains = &dev_priv->power_domains;
5409 struct i915_power_well *power_well;
Imre Deakc1ca7272013-11-25 17:15:29 +02005410 int i;
Ville Syrjälä9cdb8262013-09-16 17:38:27 +03005411
Imre Deak83c00f52013-10-25 17:36:47 +03005412 mutex_lock(&power_domains->lock);
Imre Deakc1ca7272013-11-25 17:15:29 +02005413 for_each_power_well(i, power_well, POWER_DOMAIN_MASK, power_domains) {
5414 if (power_well->set)
5415 power_well->set(dev, power_well, power_well->count > 0);
5416 }
Imre Deak83c00f52013-10-25 17:36:47 +03005417 mutex_unlock(&power_domains->lock);
Wang Xingchaoa38911a2013-05-30 22:07:11 +08005418}
5419
Paulo Zanonifa42e232013-01-25 16:59:11 -02005420/*
5421 * Starting with Haswell, we have a "Power Down Well" that can be turned off
5422 * when not needed anymore. We have 4 registers that can request the power well
5423 * to be enabled, and it will only be disabled if none of the registers is
5424 * requesting it to be enabled.
5425 */
Imre Deakddb642f2013-10-28 17:20:35 +02005426void intel_power_domains_init_hw(struct drm_device *dev)
Paulo Zanonifa42e232013-01-25 16:59:11 -02005427{
5428 struct drm_i915_private *dev_priv = dev->dev_private;
Eugeni Dodonovd0d3e512012-05-09 15:37:16 -03005429
Paulo Zanonifa42e232013-01-25 16:59:11 -02005430 /* For now, we need the power well to be always enabled. */
Imre Deakbaa70702013-10-25 17:36:48 +03005431 intel_display_set_init_power(dev, true);
Imre Deakddb642f2013-10-28 17:20:35 +02005432 intel_power_domains_resume(dev);
Eugeni Dodonovd0d3e512012-05-09 15:37:16 -03005433
Imre Deakf7243ac2013-11-25 17:15:33 +02005434 if (!(IS_HASWELL(dev) || IS_BROADWELL(dev)))
5435 return;
5436
Paulo Zanonifa42e232013-01-25 16:59:11 -02005437 /* We're taking over the BIOS, so clear any requests made by it since
5438 * the driver is in charge now. */
Paulo Zanoni6aedd1f2013-08-02 16:22:25 -03005439 if (I915_READ(HSW_PWR_WELL_BIOS) & HSW_PWR_WELL_ENABLE_REQUEST)
Paulo Zanonifa42e232013-01-25 16:59:11 -02005440 I915_WRITE(HSW_PWR_WELL_BIOS, 0);
Eugeni Dodonovd0d3e512012-05-09 15:37:16 -03005441}
5442
Paulo Zanonic67a4702013-08-19 13:18:09 -03005443/* Disables PC8 so we can use the GMBUS and DP AUX interrupts. */
5444void intel_aux_display_runtime_get(struct drm_i915_private *dev_priv)
5445{
5446 hsw_disable_package_c8(dev_priv);
5447}
5448
5449void intel_aux_display_runtime_put(struct drm_i915_private *dev_priv)
5450{
5451 hsw_enable_package_c8(dev_priv);
5452}
5453
Paulo Zanoni8a187452013-12-06 20:32:13 -02005454void intel_runtime_pm_get(struct drm_i915_private *dev_priv)
5455{
5456 struct drm_device *dev = dev_priv->dev;
5457 struct device *device = &dev->pdev->dev;
5458
5459 if (!HAS_RUNTIME_PM(dev))
5460 return;
5461
5462 pm_runtime_get_sync(device);
5463 WARN(dev_priv->pm.suspended, "Device still suspended.\n");
5464}
5465
5466void intel_runtime_pm_put(struct drm_i915_private *dev_priv)
5467{
5468 struct drm_device *dev = dev_priv->dev;
5469 struct device *device = &dev->pdev->dev;
5470
5471 if (!HAS_RUNTIME_PM(dev))
5472 return;
5473
5474 pm_runtime_mark_last_busy(device);
5475 pm_runtime_put_autosuspend(device);
5476}
5477
5478void intel_init_runtime_pm(struct drm_i915_private *dev_priv)
5479{
5480 struct drm_device *dev = dev_priv->dev;
5481 struct device *device = &dev->pdev->dev;
5482
5483 dev_priv->pm.suspended = false;
5484
5485 if (!HAS_RUNTIME_PM(dev))
5486 return;
5487
5488 pm_runtime_set_active(device);
5489
5490 pm_runtime_set_autosuspend_delay(device, 10000); /* 10s */
5491 pm_runtime_mark_last_busy(device);
5492 pm_runtime_use_autosuspend(device);
5493}
5494
5495void intel_fini_runtime_pm(struct drm_i915_private *dev_priv)
5496{
5497 struct drm_device *dev = dev_priv->dev;
5498 struct device *device = &dev->pdev->dev;
5499
5500 if (!HAS_RUNTIME_PM(dev))
5501 return;
5502
5503 /* Make sure we're not suspended first. */
5504 pm_runtime_get_sync(device);
5505 pm_runtime_disable(device);
5506}
5507
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005508/* Set up chip specific power management-related functions */
5509void intel_init_pm(struct drm_device *dev)
5510{
5511 struct drm_i915_private *dev_priv = dev->dev_private;
5512
5513 if (I915_HAS_FBC(dev)) {
Ville Syrjälä40045462013-11-28 17:29:59 +02005514 if (INTEL_INFO(dev)->gen >= 7) {
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005515 dev_priv->display.fbc_enabled = ironlake_fbc_enabled;
Ville Syrjälä40045462013-11-28 17:29:59 +02005516 dev_priv->display.enable_fbc = gen7_enable_fbc;
5517 dev_priv->display.disable_fbc = ironlake_disable_fbc;
5518 } else if (INTEL_INFO(dev)->gen >= 5) {
5519 dev_priv->display.fbc_enabled = ironlake_fbc_enabled;
5520 dev_priv->display.enable_fbc = ironlake_enable_fbc;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005521 dev_priv->display.disable_fbc = ironlake_disable_fbc;
5522 } else if (IS_GM45(dev)) {
5523 dev_priv->display.fbc_enabled = g4x_fbc_enabled;
5524 dev_priv->display.enable_fbc = g4x_enable_fbc;
5525 dev_priv->display.disable_fbc = g4x_disable_fbc;
Ville Syrjälä40045462013-11-28 17:29:59 +02005526 } else {
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005527 dev_priv->display.fbc_enabled = i8xx_fbc_enabled;
5528 dev_priv->display.enable_fbc = i8xx_enable_fbc;
5529 dev_priv->display.disable_fbc = i8xx_disable_fbc;
Ville Syrjälä993495a2013-12-12 17:27:40 +02005530
5531 /* This value was pulled out of someone's hat */
5532 I915_WRITE(FBC_CONTROL, 500 << FBC_CTL_INTERVAL_SHIFT);
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005533 }
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005534 }
5535
Daniel Vetterc921aba2012-04-26 23:28:17 +02005536 /* For cxsr */
5537 if (IS_PINEVIEW(dev))
5538 i915_pineview_get_mem_freq(dev);
5539 else if (IS_GEN5(dev))
5540 i915_ironlake_get_mem_freq(dev);
5541
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005542 /* For FIFO watermark updates */
5543 if (HAS_PCH_SPLIT(dev)) {
Ville Syrjälä53615a52013-08-01 16:18:50 +03005544 intel_setup_wm_latency(dev);
5545
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005546 if (IS_GEN5(dev)) {
Ville Syrjälä53615a52013-08-01 16:18:50 +03005547 if (dev_priv->wm.pri_latency[1] &&
5548 dev_priv->wm.spr_latency[1] &&
Ville Syrjälä96f90c52013-12-05 15:51:38 +02005549 dev_priv->wm.cur_latency[1]) {
5550 dev_priv->display.update_wm = haswell_update_wm;
5551 dev_priv->display.update_sprite_wm =
5552 haswell_update_sprite_wm;
5553 } else {
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005554 DRM_DEBUG_KMS("Failed to get proper latency. "
5555 "Disable CxSR\n");
5556 dev_priv->display.update_wm = NULL;
5557 }
5558 dev_priv->display.init_clock_gating = ironlake_init_clock_gating;
5559 } else if (IS_GEN6(dev)) {
Ville Syrjälä53615a52013-08-01 16:18:50 +03005560 if (dev_priv->wm.pri_latency[0] &&
5561 dev_priv->wm.spr_latency[0] &&
5562 dev_priv->wm.cur_latency[0]) {
Ville Syrjälä96f90c52013-12-05 15:51:38 +02005563 dev_priv->display.update_wm = haswell_update_wm;
5564 dev_priv->display.update_sprite_wm =
5565 haswell_update_sprite_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005566 } else {
5567 DRM_DEBUG_KMS("Failed to read display plane latency. "
5568 "Disable CxSR\n");
5569 dev_priv->display.update_wm = NULL;
5570 }
5571 dev_priv->display.init_clock_gating = gen6_init_clock_gating;
5572 } else if (IS_IVYBRIDGE(dev)) {
Ville Syrjälä53615a52013-08-01 16:18:50 +03005573 if (dev_priv->wm.pri_latency[0] &&
5574 dev_priv->wm.spr_latency[0] &&
5575 dev_priv->wm.cur_latency[0]) {
Ville Syrjälä96f90c52013-12-05 15:51:38 +02005576 dev_priv->display.update_wm = haswell_update_wm;
5577 dev_priv->display.update_sprite_wm =
5578 haswell_update_sprite_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005579 } else {
5580 DRM_DEBUG_KMS("Failed to read display plane latency. "
5581 "Disable CxSR\n");
5582 dev_priv->display.update_wm = NULL;
5583 }
5584 dev_priv->display.init_clock_gating = ivybridge_init_clock_gating;
Eugeni Dodonov6b8a5ee2012-05-09 15:37:23 -03005585 } else if (IS_HASWELL(dev)) {
Ville Syrjälä53615a52013-08-01 16:18:50 +03005586 if (dev_priv->wm.pri_latency[0] &&
5587 dev_priv->wm.spr_latency[0] &&
5588 dev_priv->wm.cur_latency[0]) {
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03005589 dev_priv->display.update_wm = haswell_update_wm;
Paulo Zanoni526682e2013-05-24 11:59:18 -03005590 dev_priv->display.update_sprite_wm =
5591 haswell_update_sprite_wm;
Eugeni Dodonov6b8a5ee2012-05-09 15:37:23 -03005592 } else {
5593 DRM_DEBUG_KMS("Failed to read display plane latency. "
5594 "Disable CxSR\n");
5595 dev_priv->display.update_wm = NULL;
5596 }
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03005597 dev_priv->display.init_clock_gating = haswell_init_clock_gating;
Ben Widawsky1020a5c2013-11-02 21:07:06 -07005598 } else if (INTEL_INFO(dev)->gen == 8) {
5599 dev_priv->display.init_clock_gating = gen8_init_clock_gating;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005600 } else
5601 dev_priv->display.update_wm = NULL;
5602 } else if (IS_VALLEYVIEW(dev)) {
5603 dev_priv->display.update_wm = valleyview_update_wm;
5604 dev_priv->display.init_clock_gating =
5605 valleyview_init_clock_gating;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005606 } else if (IS_PINEVIEW(dev)) {
5607 if (!intel_get_cxsr_latency(IS_PINEVIEW_G(dev),
5608 dev_priv->is_ddr3,
5609 dev_priv->fsb_freq,
5610 dev_priv->mem_freq)) {
5611 DRM_INFO("failed to find known CxSR latency "
5612 "(found ddr%s fsb freq %d, mem freq %d), "
5613 "disabling CxSR\n",
5614 (dev_priv->is_ddr3 == 1) ? "3" : "2",
5615 dev_priv->fsb_freq, dev_priv->mem_freq);
5616 /* Disable CxSR and never update its watermark again */
5617 pineview_disable_cxsr(dev);
5618 dev_priv->display.update_wm = NULL;
5619 } else
5620 dev_priv->display.update_wm = pineview_update_wm;
5621 dev_priv->display.init_clock_gating = gen3_init_clock_gating;
5622 } else if (IS_G4X(dev)) {
5623 dev_priv->display.update_wm = g4x_update_wm;
5624 dev_priv->display.init_clock_gating = g4x_init_clock_gating;
5625 } else if (IS_GEN4(dev)) {
5626 dev_priv->display.update_wm = i965_update_wm;
5627 if (IS_CRESTLINE(dev))
5628 dev_priv->display.init_clock_gating = crestline_init_clock_gating;
5629 else if (IS_BROADWATER(dev))
5630 dev_priv->display.init_clock_gating = broadwater_init_clock_gating;
5631 } else if (IS_GEN3(dev)) {
5632 dev_priv->display.update_wm = i9xx_update_wm;
5633 dev_priv->display.get_fifo_size = i9xx_get_fifo_size;
5634 dev_priv->display.init_clock_gating = gen3_init_clock_gating;
5635 } else if (IS_I865G(dev)) {
5636 dev_priv->display.update_wm = i830_update_wm;
5637 dev_priv->display.init_clock_gating = i85x_init_clock_gating;
5638 dev_priv->display.get_fifo_size = i830_get_fifo_size;
5639 } else if (IS_I85X(dev)) {
5640 dev_priv->display.update_wm = i9xx_update_wm;
5641 dev_priv->display.get_fifo_size = i85x_get_fifo_size;
5642 dev_priv->display.init_clock_gating = i85x_init_clock_gating;
5643 } else {
5644 dev_priv->display.update_wm = i830_update_wm;
5645 dev_priv->display.init_clock_gating = i830_init_clock_gating;
5646 if (IS_845G(dev))
5647 dev_priv->display.get_fifo_size = i845_get_fifo_size;
5648 else
5649 dev_priv->display.get_fifo_size = i830_get_fifo_size;
5650 }
5651}
5652
Ben Widawsky42c05262012-09-26 10:34:00 -07005653int sandybridge_pcode_read(struct drm_i915_private *dev_priv, u8 mbox, u32 *val)
5654{
Jesse Barnes4fc688c2012-11-02 11:14:01 -07005655 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Ben Widawsky42c05262012-09-26 10:34:00 -07005656
5657 if (I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) {
5658 DRM_DEBUG_DRIVER("warning: pcode (read) mailbox access failed\n");
5659 return -EAGAIN;
5660 }
5661
5662 I915_WRITE(GEN6_PCODE_DATA, *val);
5663 I915_WRITE(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY | mbox);
5664
5665 if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
5666 500)) {
5667 DRM_ERROR("timeout waiting for pcode read (%d) to finish\n", mbox);
5668 return -ETIMEDOUT;
5669 }
5670
5671 *val = I915_READ(GEN6_PCODE_DATA);
5672 I915_WRITE(GEN6_PCODE_DATA, 0);
5673
5674 return 0;
5675}
5676
5677int sandybridge_pcode_write(struct drm_i915_private *dev_priv, u8 mbox, u32 val)
5678{
Jesse Barnes4fc688c2012-11-02 11:14:01 -07005679 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Ben Widawsky42c05262012-09-26 10:34:00 -07005680
5681 if (I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) {
5682 DRM_DEBUG_DRIVER("warning: pcode (write) mailbox access failed\n");
5683 return -EAGAIN;
5684 }
5685
5686 I915_WRITE(GEN6_PCODE_DATA, val);
5687 I915_WRITE(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY | mbox);
5688
5689 if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
5690 500)) {
5691 DRM_ERROR("timeout waiting for pcode write (%d) to finish\n", mbox);
5692 return -ETIMEDOUT;
5693 }
5694
5695 I915_WRITE(GEN6_PCODE_DATA, 0);
5696
5697 return 0;
5698}
Jesse Barnesa0e4e192013-04-02 11:23:05 -07005699
Ville Syrjälä2ec38152013-11-05 22:42:29 +02005700int vlv_gpu_freq(struct drm_i915_private *dev_priv, int val)
Jesse Barnes855ba3b2013-04-17 15:54:57 -07005701{
Ville Syrjälä07ab1182013-11-05 22:42:28 +02005702 int div;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07005703
Ville Syrjälä07ab1182013-11-05 22:42:28 +02005704 /* 4 x czclk */
Ville Syrjälä2ec38152013-11-05 22:42:29 +02005705 switch (dev_priv->mem_freq) {
Jesse Barnes855ba3b2013-04-17 15:54:57 -07005706 case 800:
Ville Syrjälä07ab1182013-11-05 22:42:28 +02005707 div = 10;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07005708 break;
5709 case 1066:
Ville Syrjälä07ab1182013-11-05 22:42:28 +02005710 div = 12;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07005711 break;
5712 case 1333:
Ville Syrjälä07ab1182013-11-05 22:42:28 +02005713 div = 16;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07005714 break;
5715 default:
5716 return -1;
5717 }
5718
Ville Syrjälä2ec38152013-11-05 22:42:29 +02005719 return DIV_ROUND_CLOSEST(dev_priv->mem_freq * (val + 6 - 0xbd), 4 * div);
Jesse Barnes855ba3b2013-04-17 15:54:57 -07005720}
5721
Ville Syrjälä2ec38152013-11-05 22:42:29 +02005722int vlv_freq_opcode(struct drm_i915_private *dev_priv, int val)
Jesse Barnes855ba3b2013-04-17 15:54:57 -07005723{
Ville Syrjälä07ab1182013-11-05 22:42:28 +02005724 int mul;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07005725
Ville Syrjälä07ab1182013-11-05 22:42:28 +02005726 /* 4 x czclk */
Ville Syrjälä2ec38152013-11-05 22:42:29 +02005727 switch (dev_priv->mem_freq) {
Jesse Barnes855ba3b2013-04-17 15:54:57 -07005728 case 800:
Ville Syrjälä07ab1182013-11-05 22:42:28 +02005729 mul = 10;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07005730 break;
5731 case 1066:
Ville Syrjälä07ab1182013-11-05 22:42:28 +02005732 mul = 12;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07005733 break;
5734 case 1333:
Ville Syrjälä07ab1182013-11-05 22:42:28 +02005735 mul = 16;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07005736 break;
5737 default:
5738 return -1;
5739 }
5740
Ville Syrjälä2ec38152013-11-05 22:42:29 +02005741 return DIV_ROUND_CLOSEST(4 * mul * val, dev_priv->mem_freq) + 0xbd - 6;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07005742}
5743
Chris Wilson907b28c2013-07-19 20:36:52 +01005744void intel_pm_init(struct drm_device *dev)
5745{
5746 struct drm_i915_private *dev_priv = dev->dev_private;
5747
5748 INIT_DELAYED_WORK(&dev_priv->rps.delayed_resume_work,
5749 intel_gen6_powersave_work);
5750}