blob: 9cf3aa52541421bd953c960b99e511fa59f789c0 [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>
Damien Lespiauf4db9322013-06-24 22:59:50 +010033#include <drm/i915_powerwell.h>
Eugeni Dodonov85208be2012-04-16 22:20:34 -030034
Eugeni Dodonovf6750b32012-04-18 11:51:14 -030035/* FBC, or Frame Buffer Compression, is a technique employed to compress the
36 * framebuffer contents in-memory, aiming at reducing the required bandwidth
37 * during in-memory transfers and, therefore, reduce the power packet.
Eugeni Dodonov85208be2012-04-16 22:20:34 -030038 *
Eugeni Dodonovf6750b32012-04-18 11:51:14 -030039 * The benefits of FBC are mostly visible with solid backgrounds and
40 * variation-less patterns.
Eugeni Dodonov85208be2012-04-16 22:20:34 -030041 *
Eugeni Dodonovf6750b32012-04-18 11:51:14 -030042 * FBC-related functionality can be enabled by the means of the
43 * i915.i915_enable_fbc parameter
Eugeni Dodonov85208be2012-04-16 22:20:34 -030044 */
45
Eugeni Dodonov1fa61102012-04-18 15:29:26 -030046static void i8xx_disable_fbc(struct drm_device *dev)
Eugeni Dodonov85208be2012-04-16 22:20:34 -030047{
48 struct drm_i915_private *dev_priv = dev->dev_private;
49 u32 fbc_ctl;
50
51 /* Disable compression */
52 fbc_ctl = I915_READ(FBC_CONTROL);
53 if ((fbc_ctl & FBC_CTL_EN) == 0)
54 return;
55
56 fbc_ctl &= ~FBC_CTL_EN;
57 I915_WRITE(FBC_CONTROL, fbc_ctl);
58
59 /* Wait for compressing bit to clear */
60 if (wait_for((I915_READ(FBC_STATUS) & FBC_STAT_COMPRESSING) == 0, 10)) {
61 DRM_DEBUG_KMS("FBC idle timed out\n");
62 return;
63 }
64
65 DRM_DEBUG_KMS("disabled FBC\n");
66}
67
Eugeni Dodonov1fa61102012-04-18 15:29:26 -030068static void i8xx_enable_fbc(struct drm_crtc *crtc, unsigned long interval)
Eugeni Dodonov85208be2012-04-16 22:20:34 -030069{
70 struct drm_device *dev = crtc->dev;
71 struct drm_i915_private *dev_priv = dev->dev_private;
72 struct drm_framebuffer *fb = crtc->fb;
73 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
74 struct drm_i915_gem_object *obj = intel_fb->obj;
75 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
76 int cfb_pitch;
77 int plane, i;
78 u32 fbc_ctl, fbc_ctl2;
79
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -070080 cfb_pitch = dev_priv->fbc.size / FBC_LL_SIZE;
Eugeni Dodonov85208be2012-04-16 22:20:34 -030081 if (fb->pitches[0] < cfb_pitch)
82 cfb_pitch = fb->pitches[0];
83
84 /* FBC_CTL wants 64B units */
85 cfb_pitch = (cfb_pitch / 64) - 1;
86 plane = intel_crtc->plane == 0 ? FBC_CTL_PLANEA : FBC_CTL_PLANEB;
87
88 /* Clear old tags */
89 for (i = 0; i < (FBC_LL_SIZE / 32) + 1; i++)
90 I915_WRITE(FBC_TAG + (i * 4), 0);
91
92 /* Set it up... */
93 fbc_ctl2 = FBC_CTL_FENCE_DBL | FBC_CTL_IDLE_IMM | FBC_CTL_CPU_FENCE;
94 fbc_ctl2 |= plane;
95 I915_WRITE(FBC_CONTROL2, fbc_ctl2);
96 I915_WRITE(FBC_FENCE_OFF, crtc->y);
97
98 /* enable it... */
99 fbc_ctl = FBC_CTL_EN | FBC_CTL_PERIODIC;
100 if (IS_I945GM(dev))
101 fbc_ctl |= FBC_CTL_C3_IDLE; /* 945 needs special SR handling */
102 fbc_ctl |= (cfb_pitch & 0xff) << FBC_CTL_STRIDE_SHIFT;
103 fbc_ctl |= (interval & 0x2fff) << FBC_CTL_INTERVAL_SHIFT;
104 fbc_ctl |= obj->fence_reg;
105 I915_WRITE(FBC_CONTROL, fbc_ctl);
106
Ville Syrjälä84f44ce2013-04-17 17:48:49 +0300107 DRM_DEBUG_KMS("enabled FBC, pitch %d, yoff %d, plane %c, ",
108 cfb_pitch, crtc->y, plane_name(intel_crtc->plane));
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300109}
110
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300111static bool i8xx_fbc_enabled(struct drm_device *dev)
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300112{
113 struct drm_i915_private *dev_priv = dev->dev_private;
114
115 return I915_READ(FBC_CONTROL) & FBC_CTL_EN;
116}
117
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300118static void g4x_enable_fbc(struct drm_crtc *crtc, unsigned long interval)
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300119{
120 struct drm_device *dev = crtc->dev;
121 struct drm_i915_private *dev_priv = dev->dev_private;
122 struct drm_framebuffer *fb = crtc->fb;
123 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
124 struct drm_i915_gem_object *obj = intel_fb->obj;
125 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
126 int plane = intel_crtc->plane == 0 ? DPFC_CTL_PLANEA : DPFC_CTL_PLANEB;
127 unsigned long stall_watermark = 200;
128 u32 dpfc_ctl;
129
130 dpfc_ctl = plane | DPFC_SR_EN | DPFC_CTL_LIMIT_1X;
131 dpfc_ctl |= DPFC_CTL_FENCE_EN | obj->fence_reg;
132 I915_WRITE(DPFC_CHICKEN, DPFC_HT_MODIFY);
133
134 I915_WRITE(DPFC_RECOMP_CTL, DPFC_RECOMP_STALL_EN |
135 (stall_watermark << DPFC_RECOMP_STALL_WM_SHIFT) |
136 (interval << DPFC_RECOMP_TIMER_COUNT_SHIFT));
137 I915_WRITE(DPFC_FENCE_YOFF, crtc->y);
138
139 /* enable it... */
140 I915_WRITE(DPFC_CONTROL, I915_READ(DPFC_CONTROL) | DPFC_CTL_EN);
141
Ville Syrjälä84f44ce2013-04-17 17:48:49 +0300142 DRM_DEBUG_KMS("enabled fbc on plane %c\n", plane_name(intel_crtc->plane));
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300143}
144
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300145static void g4x_disable_fbc(struct drm_device *dev)
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300146{
147 struct drm_i915_private *dev_priv = dev->dev_private;
148 u32 dpfc_ctl;
149
150 /* Disable compression */
151 dpfc_ctl = I915_READ(DPFC_CONTROL);
152 if (dpfc_ctl & DPFC_CTL_EN) {
153 dpfc_ctl &= ~DPFC_CTL_EN;
154 I915_WRITE(DPFC_CONTROL, dpfc_ctl);
155
156 DRM_DEBUG_KMS("disabled FBC\n");
157 }
158}
159
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300160static bool g4x_fbc_enabled(struct drm_device *dev)
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300161{
162 struct drm_i915_private *dev_priv = dev->dev_private;
163
164 return I915_READ(DPFC_CONTROL) & DPFC_CTL_EN;
165}
166
167static void sandybridge_blit_fbc_update(struct drm_device *dev)
168{
169 struct drm_i915_private *dev_priv = dev->dev_private;
170 u32 blt_ecoskpd;
171
172 /* Make sure blitter notifies FBC of writes */
173 gen6_gt_force_wake_get(dev_priv);
174 blt_ecoskpd = I915_READ(GEN6_BLITTER_ECOSKPD);
175 blt_ecoskpd |= GEN6_BLITTER_FBC_NOTIFY <<
176 GEN6_BLITTER_LOCK_SHIFT;
177 I915_WRITE(GEN6_BLITTER_ECOSKPD, blt_ecoskpd);
178 blt_ecoskpd |= GEN6_BLITTER_FBC_NOTIFY;
179 I915_WRITE(GEN6_BLITTER_ECOSKPD, blt_ecoskpd);
180 blt_ecoskpd &= ~(GEN6_BLITTER_FBC_NOTIFY <<
181 GEN6_BLITTER_LOCK_SHIFT);
182 I915_WRITE(GEN6_BLITTER_ECOSKPD, blt_ecoskpd);
183 POSTING_READ(GEN6_BLITTER_ECOSKPD);
184 gen6_gt_force_wake_put(dev_priv);
185}
186
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300187static void ironlake_enable_fbc(struct drm_crtc *crtc, unsigned long interval)
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300188{
189 struct drm_device *dev = crtc->dev;
190 struct drm_i915_private *dev_priv = dev->dev_private;
191 struct drm_framebuffer *fb = crtc->fb;
192 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
193 struct drm_i915_gem_object *obj = intel_fb->obj;
194 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
195 int plane = intel_crtc->plane == 0 ? DPFC_CTL_PLANEA : DPFC_CTL_PLANEB;
196 unsigned long stall_watermark = 200;
197 u32 dpfc_ctl;
198
199 dpfc_ctl = I915_READ(ILK_DPFC_CONTROL);
200 dpfc_ctl &= DPFC_RESERVED;
201 dpfc_ctl |= (plane | DPFC_CTL_LIMIT_1X);
202 /* Set persistent mode for front-buffer rendering, ala X. */
203 dpfc_ctl |= DPFC_CTL_PERSISTENT_MODE;
204 dpfc_ctl |= (DPFC_CTL_FENCE_EN | obj->fence_reg);
205 I915_WRITE(ILK_DPFC_CHICKEN, DPFC_HT_MODIFY);
206
207 I915_WRITE(ILK_DPFC_RECOMP_CTL, DPFC_RECOMP_STALL_EN |
208 (stall_watermark << DPFC_RECOMP_STALL_WM_SHIFT) |
209 (interval << DPFC_RECOMP_TIMER_COUNT_SHIFT));
210 I915_WRITE(ILK_DPFC_FENCE_YOFF, crtc->y);
Ben Widawskyf343c5f2013-07-05 14:41:04 -0700211 I915_WRITE(ILK_FBC_RT_BASE, i915_gem_obj_ggtt_offset(obj) | ILK_FBC_RT_VALID);
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300212 /* enable it... */
213 I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl | DPFC_CTL_EN);
214
215 if (IS_GEN6(dev)) {
216 I915_WRITE(SNB_DPFC_CTL_SA,
217 SNB_CPU_FENCE_ENABLE | obj->fence_reg);
218 I915_WRITE(DPFC_CPU_FENCE_OFFSET, crtc->y);
219 sandybridge_blit_fbc_update(dev);
220 }
221
Ville Syrjälä84f44ce2013-04-17 17:48:49 +0300222 DRM_DEBUG_KMS("enabled fbc on plane %c\n", plane_name(intel_crtc->plane));
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300223}
224
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300225static void ironlake_disable_fbc(struct drm_device *dev)
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300226{
227 struct drm_i915_private *dev_priv = dev->dev_private;
228 u32 dpfc_ctl;
229
230 /* Disable compression */
231 dpfc_ctl = I915_READ(ILK_DPFC_CONTROL);
232 if (dpfc_ctl & DPFC_CTL_EN) {
233 dpfc_ctl &= ~DPFC_CTL_EN;
234 I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl);
235
Rodrigo Vivib74ea102013-05-09 14:08:38 -0300236 if (IS_IVYBRIDGE(dev))
Damien Lespiau7dd23ba2013-05-10 14:33:17 +0100237 /* WaFbcDisableDpfcClockGating:ivb */
Rodrigo Vivib74ea102013-05-09 14:08:38 -0300238 I915_WRITE(ILK_DSPCLK_GATE_D,
239 I915_READ(ILK_DSPCLK_GATE_D) &
240 ~ILK_DPFCUNIT_CLOCK_GATE_DISABLE);
241
Rodrigo Vivid89f2072013-05-09 14:20:50 -0300242 if (IS_HASWELL(dev))
Damien Lespiau7dd23ba2013-05-10 14:33:17 +0100243 /* WaFbcDisableDpfcClockGating:hsw */
Rodrigo Vivid89f2072013-05-09 14:20:50 -0300244 I915_WRITE(HSW_CLKGATE_DISABLE_PART_1,
245 I915_READ(HSW_CLKGATE_DISABLE_PART_1) &
246 ~HSW_DPFC_GATING_DISABLE);
247
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300248 DRM_DEBUG_KMS("disabled FBC\n");
249 }
250}
251
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300252static bool ironlake_fbc_enabled(struct drm_device *dev)
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300253{
254 struct drm_i915_private *dev_priv = dev->dev_private;
255
256 return I915_READ(ILK_DPFC_CONTROL) & DPFC_CTL_EN;
257}
258
Rodrigo Viviabe959c2013-05-06 19:37:33 -0300259static void gen7_enable_fbc(struct drm_crtc *crtc, unsigned long interval)
260{
261 struct drm_device *dev = crtc->dev;
262 struct drm_i915_private *dev_priv = dev->dev_private;
263 struct drm_framebuffer *fb = crtc->fb;
264 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
265 struct drm_i915_gem_object *obj = intel_fb->obj;
266 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
267
Ben Widawskyf343c5f2013-07-05 14:41:04 -0700268 I915_WRITE(IVB_FBC_RT_BASE, i915_gem_obj_ggtt_offset(obj));
Rodrigo Viviabe959c2013-05-06 19:37:33 -0300269
270 I915_WRITE(ILK_DPFC_CONTROL, DPFC_CTL_EN | DPFC_CTL_LIMIT_1X |
271 IVB_DPFC_CTL_FENCE_EN |
272 intel_crtc->plane << IVB_DPFC_CTL_PLANE_SHIFT);
273
Rodrigo Vivi891348b2013-05-06 19:37:36 -0300274 if (IS_IVYBRIDGE(dev)) {
Damien Lespiau7dd23ba2013-05-10 14:33:17 +0100275 /* WaFbcAsynchFlipDisableFbcQueue:ivb */
Rodrigo Vivi891348b2013-05-06 19:37:36 -0300276 I915_WRITE(ILK_DISPLAY_CHICKEN1, ILK_FBCQ_DIS);
Damien Lespiau7dd23ba2013-05-10 14:33:17 +0100277 /* WaFbcDisableDpfcClockGating:ivb */
Rodrigo Vivi891348b2013-05-06 19:37:36 -0300278 I915_WRITE(ILK_DSPCLK_GATE_D,
279 I915_READ(ILK_DSPCLK_GATE_D) |
280 ILK_DPFCUNIT_CLOCK_GATE_DISABLE);
Rodrigo Vivi28554162013-05-06 19:37:37 -0300281 } else {
Damien Lespiau7dd23ba2013-05-10 14:33:17 +0100282 /* WaFbcAsynchFlipDisableFbcQueue:hsw */
Rodrigo Vivi28554162013-05-06 19:37:37 -0300283 I915_WRITE(HSW_PIPE_SLICE_CHICKEN_1(intel_crtc->pipe),
284 HSW_BYPASS_FBC_QUEUE);
Damien Lespiau7dd23ba2013-05-10 14:33:17 +0100285 /* WaFbcDisableDpfcClockGating:hsw */
Rodrigo Vivid89f2072013-05-09 14:20:50 -0300286 I915_WRITE(HSW_CLKGATE_DISABLE_PART_1,
287 I915_READ(HSW_CLKGATE_DISABLE_PART_1) |
288 HSW_DPFC_GATING_DISABLE);
Rodrigo Vivi891348b2013-05-06 19:37:36 -0300289 }
Rodrigo Vivib74ea102013-05-09 14:08:38 -0300290
Rodrigo Viviabe959c2013-05-06 19:37:33 -0300291 I915_WRITE(SNB_DPFC_CTL_SA,
292 SNB_CPU_FENCE_ENABLE | obj->fence_reg);
293 I915_WRITE(DPFC_CPU_FENCE_OFFSET, crtc->y);
294
295 sandybridge_blit_fbc_update(dev);
296
297 DRM_DEBUG_KMS("enabled fbc on plane %d\n", intel_crtc->plane);
298}
299
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300300bool intel_fbc_enabled(struct drm_device *dev)
301{
302 struct drm_i915_private *dev_priv = dev->dev_private;
303
304 if (!dev_priv->display.fbc_enabled)
305 return false;
306
307 return dev_priv->display.fbc_enabled(dev);
308}
309
310static void intel_fbc_work_fn(struct work_struct *__work)
311{
312 struct intel_fbc_work *work =
313 container_of(to_delayed_work(__work),
314 struct intel_fbc_work, work);
315 struct drm_device *dev = work->crtc->dev;
316 struct drm_i915_private *dev_priv = dev->dev_private;
317
318 mutex_lock(&dev->struct_mutex);
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -0700319 if (work == dev_priv->fbc.fbc_work) {
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300320 /* Double check that we haven't switched fb without cancelling
321 * the prior work.
322 */
323 if (work->crtc->fb == work->fb) {
324 dev_priv->display.enable_fbc(work->crtc,
325 work->interval);
326
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -0700327 dev_priv->fbc.plane = to_intel_crtc(work->crtc)->plane;
328 dev_priv->fbc.fb_id = work->crtc->fb->base.id;
329 dev_priv->fbc.y = work->crtc->y;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300330 }
331
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -0700332 dev_priv->fbc.fbc_work = NULL;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300333 }
334 mutex_unlock(&dev->struct_mutex);
335
336 kfree(work);
337}
338
339static void intel_cancel_fbc_work(struct drm_i915_private *dev_priv)
340{
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -0700341 if (dev_priv->fbc.fbc_work == NULL)
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300342 return;
343
344 DRM_DEBUG_KMS("cancelling pending FBC enable\n");
345
346 /* Synchronisation is provided by struct_mutex and checking of
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -0700347 * dev_priv->fbc.fbc_work, so we can perform the cancellation
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300348 * entirely asynchronously.
349 */
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -0700350 if (cancel_delayed_work(&dev_priv->fbc.fbc_work->work))
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300351 /* tasklet was killed before being run, clean up */
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -0700352 kfree(dev_priv->fbc.fbc_work);
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300353
354 /* Mark the work as no longer wanted so that if it does
355 * wake-up (because the work was already running and waiting
356 * for our mutex), it will discover that is no longer
357 * necessary to run.
358 */
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -0700359 dev_priv->fbc.fbc_work = NULL;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300360}
361
Damien Lespiaub63fb442013-06-24 16:22:01 +0100362static void intel_enable_fbc(struct drm_crtc *crtc, unsigned long interval)
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300363{
364 struct intel_fbc_work *work;
365 struct drm_device *dev = crtc->dev;
366 struct drm_i915_private *dev_priv = dev->dev_private;
367
368 if (!dev_priv->display.enable_fbc)
369 return;
370
371 intel_cancel_fbc_work(dev_priv);
372
373 work = kzalloc(sizeof *work, GFP_KERNEL);
374 if (work == NULL) {
Paulo Zanoni6cdcb5e2013-06-12 17:27:29 -0300375 DRM_ERROR("Failed to allocate FBC work structure\n");
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300376 dev_priv->display.enable_fbc(crtc, interval);
377 return;
378 }
379
380 work->crtc = crtc;
381 work->fb = crtc->fb;
382 work->interval = interval;
383 INIT_DELAYED_WORK(&work->work, intel_fbc_work_fn);
384
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -0700385 dev_priv->fbc.fbc_work = work;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300386
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300387 /* Delay the actual enabling to let pageflipping cease and the
388 * display to settle before starting the compression. Note that
389 * this delay also serves a second purpose: it allows for a
390 * vblank to pass after disabling the FBC before we attempt
391 * to modify the control registers.
392 *
393 * A more complicated solution would involve tracking vblanks
394 * following the termination of the page-flipping sequence
395 * and indeed performing the enable as a co-routine and not
396 * waiting synchronously upon the vblank.
Damien Lespiau7457d612013-06-07 17:41:07 +0100397 *
398 * WaFbcWaitForVBlankBeforeEnable:ilk,snb
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300399 */
400 schedule_delayed_work(&work->work, msecs_to_jiffies(50));
401}
402
403void intel_disable_fbc(struct drm_device *dev)
404{
405 struct drm_i915_private *dev_priv = dev->dev_private;
406
407 intel_cancel_fbc_work(dev_priv);
408
409 if (!dev_priv->display.disable_fbc)
410 return;
411
412 dev_priv->display.disable_fbc(dev);
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -0700413 dev_priv->fbc.plane = -1;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300414}
415
Chris Wilson29ebf902013-07-27 17:23:55 +0100416static bool set_no_fbc_reason(struct drm_i915_private *dev_priv,
417 enum no_fbc_reason reason)
418{
419 if (dev_priv->fbc.no_fbc_reason == reason)
420 return false;
421
422 dev_priv->fbc.no_fbc_reason = reason;
423 return true;
424}
425
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300426/**
427 * intel_update_fbc - enable/disable FBC as needed
428 * @dev: the drm_device
429 *
430 * Set up the framebuffer compression hardware at mode set time. We
431 * enable it if possible:
432 * - plane A only (on pre-965)
433 * - no pixel mulitply/line duplication
434 * - no alpha buffer discard
435 * - no dual wide
Paulo Zanonif85da862013-06-04 16:53:39 -0300436 * - framebuffer <= max_hdisplay in width, max_vdisplay in height
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300437 *
438 * We can't assume that any compression will take place (worst case),
439 * so the compressed buffer has to be the same size as the uncompressed
440 * one. It also must reside (along with the line length buffer) in
441 * stolen memory.
442 *
443 * We need to enable/disable FBC on a global basis.
444 */
445void intel_update_fbc(struct drm_device *dev)
446{
447 struct drm_i915_private *dev_priv = dev->dev_private;
448 struct drm_crtc *crtc = NULL, *tmp_crtc;
449 struct intel_crtc *intel_crtc;
450 struct drm_framebuffer *fb;
451 struct intel_framebuffer *intel_fb;
452 struct drm_i915_gem_object *obj;
Ville Syrjäläef644fd2013-09-04 18:25:21 +0300453 const struct drm_display_mode *mode;
454 const struct drm_display_mode *adjusted_mode;
Paulo Zanonif85da862013-06-04 16:53:39 -0300455 unsigned int max_hdisplay, max_vdisplay;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300456
Chris Wilson29ebf902013-07-27 17:23:55 +0100457 if (!I915_HAS_FBC(dev)) {
458 set_no_fbc_reason(dev_priv, FBC_UNSUPPORTED);
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300459 return;
Chris Wilson29ebf902013-07-27 17:23:55 +0100460 }
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300461
Chris Wilson29ebf902013-07-27 17:23:55 +0100462 if (!i915_powersave) {
463 if (set_no_fbc_reason(dev_priv, FBC_MODULE_PARAM))
464 DRM_DEBUG_KMS("fbc disabled per module param\n");
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300465 return;
Chris Wilson29ebf902013-07-27 17:23:55 +0100466 }
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300467
468 /*
469 * If FBC is already on, we just have to verify that we can
470 * keep it that way...
471 * Need to disable if:
472 * - more than one pipe is active
473 * - changing FBC params (stride, fence, mode)
474 * - new fb is too large to fit in compressed buffer
475 * - going to an unsupported config (interlace, pixel multiply, etc.)
476 */
477 list_for_each_entry(tmp_crtc, &dev->mode_config.crtc_list, head) {
Chris Wilson3490ea52013-01-07 10:11:40 +0000478 if (intel_crtc_active(tmp_crtc) &&
479 !to_intel_crtc(tmp_crtc)->primary_disabled) {
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300480 if (crtc) {
Chris Wilson29ebf902013-07-27 17:23:55 +0100481 if (set_no_fbc_reason(dev_priv, FBC_MULTIPLE_PIPES))
482 DRM_DEBUG_KMS("more than one pipe active, disabling compression\n");
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300483 goto out_disable;
484 }
485 crtc = tmp_crtc;
486 }
487 }
488
489 if (!crtc || crtc->fb == NULL) {
Chris Wilson29ebf902013-07-27 17:23:55 +0100490 if (set_no_fbc_reason(dev_priv, FBC_NO_OUTPUT))
491 DRM_DEBUG_KMS("no output, disabling\n");
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300492 goto out_disable;
493 }
494
495 intel_crtc = to_intel_crtc(crtc);
496 fb = crtc->fb;
497 intel_fb = to_intel_framebuffer(fb);
498 obj = intel_fb->obj;
Ville Syrjäläef644fd2013-09-04 18:25:21 +0300499 mode = &intel_crtc->config.requested_mode;
500 adjusted_mode = &intel_crtc->config.adjusted_mode;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300501
Damien Lespiau8a5729a2013-06-24 16:22:02 +0100502 if (i915_enable_fbc < 0 &&
503 INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev)) {
Chris Wilson29ebf902013-07-27 17:23:55 +0100504 if (set_no_fbc_reason(dev_priv, FBC_CHIP_DEFAULT))
505 DRM_DEBUG_KMS("disabled per chip default\n");
Damien Lespiau8a5729a2013-06-24 16:22:02 +0100506 goto out_disable;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300507 }
Damien Lespiau8a5729a2013-06-24 16:22:02 +0100508 if (!i915_enable_fbc) {
Chris Wilson29ebf902013-07-27 17:23:55 +0100509 if (set_no_fbc_reason(dev_priv, FBC_MODULE_PARAM))
510 DRM_DEBUG_KMS("fbc disabled per module param\n");
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300511 goto out_disable;
512 }
Ville Syrjäläef644fd2013-09-04 18:25:21 +0300513 if ((adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) ||
514 (adjusted_mode->flags & DRM_MODE_FLAG_DBLSCAN)) {
Chris Wilson29ebf902013-07-27 17:23:55 +0100515 if (set_no_fbc_reason(dev_priv, FBC_UNSUPPORTED_MODE))
516 DRM_DEBUG_KMS("mode incompatible with compression, "
517 "disabling\n");
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300518 goto out_disable;
519 }
Paulo Zanonif85da862013-06-04 16:53:39 -0300520
521 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
522 max_hdisplay = 4096;
523 max_vdisplay = 2048;
524 } else {
525 max_hdisplay = 2048;
526 max_vdisplay = 1536;
527 }
Ville Syrjäläef644fd2013-09-04 18:25:21 +0300528 if ((mode->hdisplay > max_hdisplay) ||
529 (mode->vdisplay > max_vdisplay)) {
Chris Wilson29ebf902013-07-27 17:23:55 +0100530 if (set_no_fbc_reason(dev_priv, FBC_MODE_TOO_LARGE))
531 DRM_DEBUG_KMS("mode too large for compression, disabling\n");
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300532 goto out_disable;
533 }
Rodrigo Vivi891348b2013-05-06 19:37:36 -0300534 if ((IS_I915GM(dev) || IS_I945GM(dev) || IS_HASWELL(dev)) &&
535 intel_crtc->plane != 0) {
Chris Wilson29ebf902013-07-27 17:23:55 +0100536 if (set_no_fbc_reason(dev_priv, FBC_BAD_PLANE))
537 DRM_DEBUG_KMS("plane not 0, disabling compression\n");
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300538 goto out_disable;
539 }
540
541 /* The use of a CPU fence is mandatory in order to detect writes
542 * by the CPU to the scanout and trigger updates to the FBC.
543 */
544 if (obj->tiling_mode != I915_TILING_X ||
545 obj->fence_reg == I915_FENCE_REG_NONE) {
Chris Wilson29ebf902013-07-27 17:23:55 +0100546 if (set_no_fbc_reason(dev_priv, FBC_NOT_TILED))
547 DRM_DEBUG_KMS("framebuffer not tiled or fenced, disabling compression\n");
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300548 goto out_disable;
549 }
550
551 /* If the kernel debugger is active, always disable compression */
552 if (in_dbg_master())
553 goto out_disable;
554
Chris Wilson11be49e2012-11-15 11:32:20 +0000555 if (i915_gem_stolen_setup_compression(dev, intel_fb->obj->base.size)) {
Chris Wilson29ebf902013-07-27 17:23:55 +0100556 if (set_no_fbc_reason(dev_priv, FBC_STOLEN_TOO_SMALL))
557 DRM_DEBUG_KMS("framebuffer too large, disabling compression\n");
Chris Wilson11be49e2012-11-15 11:32:20 +0000558 goto out_disable;
559 }
560
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300561 /* If the scanout has not changed, don't modify the FBC settings.
562 * Note that we make the fundamental assumption that the fb->obj
563 * cannot be unpinned (and have its GTT offset and fence revoked)
564 * without first being decoupled from the scanout and FBC disabled.
565 */
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -0700566 if (dev_priv->fbc.plane == intel_crtc->plane &&
567 dev_priv->fbc.fb_id == fb->base.id &&
568 dev_priv->fbc.y == crtc->y)
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300569 return;
570
571 if (intel_fbc_enabled(dev)) {
572 /* We update FBC along two paths, after changing fb/crtc
573 * configuration (modeswitching) and after page-flipping
574 * finishes. For the latter, we know that not only did
575 * we disable the FBC at the start of the page-flip
576 * sequence, but also more than one vblank has passed.
577 *
578 * For the former case of modeswitching, it is possible
579 * to switch between two FBC valid configurations
580 * instantaneously so we do need to disable the FBC
581 * before we can modify its control registers. We also
582 * have to wait for the next vblank for that to take
583 * effect. However, since we delay enabling FBC we can
584 * assume that a vblank has passed since disabling and
585 * that we can safely alter the registers in the deferred
586 * callback.
587 *
588 * In the scenario that we go from a valid to invalid
589 * and then back to valid FBC configuration we have
590 * no strict enforcement that a vblank occurred since
591 * disabling the FBC. However, along all current pipe
592 * disabling paths we do need to wait for a vblank at
593 * some point. And we wait before enabling FBC anyway.
594 */
595 DRM_DEBUG_KMS("disabling active FBC for update\n");
596 intel_disable_fbc(dev);
597 }
598
599 intel_enable_fbc(crtc, 500);
Chris Wilson29ebf902013-07-27 17:23:55 +0100600 dev_priv->fbc.no_fbc_reason = FBC_OK;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300601 return;
602
603out_disable:
604 /* Multiple disables should be harmless */
605 if (intel_fbc_enabled(dev)) {
606 DRM_DEBUG_KMS("unsupported config, disabling FBC\n");
607 intel_disable_fbc(dev);
608 }
Chris Wilson11be49e2012-11-15 11:32:20 +0000609 i915_gem_stolen_cleanup_compression(dev);
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300610}
611
Daniel Vetterc921aba2012-04-26 23:28:17 +0200612static void i915_pineview_get_mem_freq(struct drm_device *dev)
613{
614 drm_i915_private_t *dev_priv = dev->dev_private;
615 u32 tmp;
616
617 tmp = I915_READ(CLKCFG);
618
619 switch (tmp & CLKCFG_FSB_MASK) {
620 case CLKCFG_FSB_533:
621 dev_priv->fsb_freq = 533; /* 133*4 */
622 break;
623 case CLKCFG_FSB_800:
624 dev_priv->fsb_freq = 800; /* 200*4 */
625 break;
626 case CLKCFG_FSB_667:
627 dev_priv->fsb_freq = 667; /* 167*4 */
628 break;
629 case CLKCFG_FSB_400:
630 dev_priv->fsb_freq = 400; /* 100*4 */
631 break;
632 }
633
634 switch (tmp & CLKCFG_MEM_MASK) {
635 case CLKCFG_MEM_533:
636 dev_priv->mem_freq = 533;
637 break;
638 case CLKCFG_MEM_667:
639 dev_priv->mem_freq = 667;
640 break;
641 case CLKCFG_MEM_800:
642 dev_priv->mem_freq = 800;
643 break;
644 }
645
646 /* detect pineview DDR3 setting */
647 tmp = I915_READ(CSHRDDR3CTL);
648 dev_priv->is_ddr3 = (tmp & CSHRDDR3CTL_DDR3) ? 1 : 0;
649}
650
651static void i915_ironlake_get_mem_freq(struct drm_device *dev)
652{
653 drm_i915_private_t *dev_priv = dev->dev_private;
654 u16 ddrpll, csipll;
655
656 ddrpll = I915_READ16(DDRMPLL1);
657 csipll = I915_READ16(CSIPLL0);
658
659 switch (ddrpll & 0xff) {
660 case 0xc:
661 dev_priv->mem_freq = 800;
662 break;
663 case 0x10:
664 dev_priv->mem_freq = 1066;
665 break;
666 case 0x14:
667 dev_priv->mem_freq = 1333;
668 break;
669 case 0x18:
670 dev_priv->mem_freq = 1600;
671 break;
672 default:
673 DRM_DEBUG_DRIVER("unknown memory frequency 0x%02x\n",
674 ddrpll & 0xff);
675 dev_priv->mem_freq = 0;
676 break;
677 }
678
Daniel Vetter20e4d402012-08-08 23:35:39 +0200679 dev_priv->ips.r_t = dev_priv->mem_freq;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200680
681 switch (csipll & 0x3ff) {
682 case 0x00c:
683 dev_priv->fsb_freq = 3200;
684 break;
685 case 0x00e:
686 dev_priv->fsb_freq = 3733;
687 break;
688 case 0x010:
689 dev_priv->fsb_freq = 4266;
690 break;
691 case 0x012:
692 dev_priv->fsb_freq = 4800;
693 break;
694 case 0x014:
695 dev_priv->fsb_freq = 5333;
696 break;
697 case 0x016:
698 dev_priv->fsb_freq = 5866;
699 break;
700 case 0x018:
701 dev_priv->fsb_freq = 6400;
702 break;
703 default:
704 DRM_DEBUG_DRIVER("unknown fsb frequency 0x%04x\n",
705 csipll & 0x3ff);
706 dev_priv->fsb_freq = 0;
707 break;
708 }
709
710 if (dev_priv->fsb_freq == 3200) {
Daniel Vetter20e4d402012-08-08 23:35:39 +0200711 dev_priv->ips.c_m = 0;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200712 } else if (dev_priv->fsb_freq > 3200 && dev_priv->fsb_freq <= 4800) {
Daniel Vetter20e4d402012-08-08 23:35:39 +0200713 dev_priv->ips.c_m = 1;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200714 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +0200715 dev_priv->ips.c_m = 2;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200716 }
717}
718
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300719static const struct cxsr_latency cxsr_latency_table[] = {
720 {1, 0, 800, 400, 3382, 33382, 3983, 33983}, /* DDR2-400 SC */
721 {1, 0, 800, 667, 3354, 33354, 3807, 33807}, /* DDR2-667 SC */
722 {1, 0, 800, 800, 3347, 33347, 3763, 33763}, /* DDR2-800 SC */
723 {1, 1, 800, 667, 6420, 36420, 6873, 36873}, /* DDR3-667 SC */
724 {1, 1, 800, 800, 5902, 35902, 6318, 36318}, /* DDR3-800 SC */
725
726 {1, 0, 667, 400, 3400, 33400, 4021, 34021}, /* DDR2-400 SC */
727 {1, 0, 667, 667, 3372, 33372, 3845, 33845}, /* DDR2-667 SC */
728 {1, 0, 667, 800, 3386, 33386, 3822, 33822}, /* DDR2-800 SC */
729 {1, 1, 667, 667, 6438, 36438, 6911, 36911}, /* DDR3-667 SC */
730 {1, 1, 667, 800, 5941, 35941, 6377, 36377}, /* DDR3-800 SC */
731
732 {1, 0, 400, 400, 3472, 33472, 4173, 34173}, /* DDR2-400 SC */
733 {1, 0, 400, 667, 3443, 33443, 3996, 33996}, /* DDR2-667 SC */
734 {1, 0, 400, 800, 3430, 33430, 3946, 33946}, /* DDR2-800 SC */
735 {1, 1, 400, 667, 6509, 36509, 7062, 37062}, /* DDR3-667 SC */
736 {1, 1, 400, 800, 5985, 35985, 6501, 36501}, /* DDR3-800 SC */
737
738 {0, 0, 800, 400, 3438, 33438, 4065, 34065}, /* DDR2-400 SC */
739 {0, 0, 800, 667, 3410, 33410, 3889, 33889}, /* DDR2-667 SC */
740 {0, 0, 800, 800, 3403, 33403, 3845, 33845}, /* DDR2-800 SC */
741 {0, 1, 800, 667, 6476, 36476, 6955, 36955}, /* DDR3-667 SC */
742 {0, 1, 800, 800, 5958, 35958, 6400, 36400}, /* DDR3-800 SC */
743
744 {0, 0, 667, 400, 3456, 33456, 4103, 34106}, /* DDR2-400 SC */
745 {0, 0, 667, 667, 3428, 33428, 3927, 33927}, /* DDR2-667 SC */
746 {0, 0, 667, 800, 3443, 33443, 3905, 33905}, /* DDR2-800 SC */
747 {0, 1, 667, 667, 6494, 36494, 6993, 36993}, /* DDR3-667 SC */
748 {0, 1, 667, 800, 5998, 35998, 6460, 36460}, /* DDR3-800 SC */
749
750 {0, 0, 400, 400, 3528, 33528, 4255, 34255}, /* DDR2-400 SC */
751 {0, 0, 400, 667, 3500, 33500, 4079, 34079}, /* DDR2-667 SC */
752 {0, 0, 400, 800, 3487, 33487, 4029, 34029}, /* DDR2-800 SC */
753 {0, 1, 400, 667, 6566, 36566, 7145, 37145}, /* DDR3-667 SC */
754 {0, 1, 400, 800, 6042, 36042, 6584, 36584}, /* DDR3-800 SC */
755};
756
Daniel Vetter63c62272012-04-21 23:17:55 +0200757static const struct cxsr_latency *intel_get_cxsr_latency(int is_desktop,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300758 int is_ddr3,
759 int fsb,
760 int mem)
761{
762 const struct cxsr_latency *latency;
763 int i;
764
765 if (fsb == 0 || mem == 0)
766 return NULL;
767
768 for (i = 0; i < ARRAY_SIZE(cxsr_latency_table); i++) {
769 latency = &cxsr_latency_table[i];
770 if (is_desktop == latency->is_desktop &&
771 is_ddr3 == latency->is_ddr3 &&
772 fsb == latency->fsb_freq && mem == latency->mem_freq)
773 return latency;
774 }
775
776 DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
777
778 return NULL;
779}
780
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300781static void pineview_disable_cxsr(struct drm_device *dev)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300782{
783 struct drm_i915_private *dev_priv = dev->dev_private;
784
785 /* deactivate cxsr */
786 I915_WRITE(DSPFW3, I915_READ(DSPFW3) & ~PINEVIEW_SELF_REFRESH_EN);
787}
788
789/*
790 * Latency for FIFO fetches is dependent on several factors:
791 * - memory configuration (speed, channels)
792 * - chipset
793 * - current MCH state
794 * It can be fairly high in some situations, so here we assume a fairly
795 * pessimal value. It's a tradeoff between extra memory fetches (if we
796 * set this value too high, the FIFO will fetch frequently to stay full)
797 * and power consumption (set it too low to save power and we might see
798 * FIFO underruns and display "flicker").
799 *
800 * A value of 5us seems to be a good balance; safe for very low end
801 * platforms but not overly aggressive on lower latency configs.
802 */
803static const int latency_ns = 5000;
804
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300805static int i9xx_get_fifo_size(struct drm_device *dev, int plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300806{
807 struct drm_i915_private *dev_priv = dev->dev_private;
808 uint32_t dsparb = I915_READ(DSPARB);
809 int size;
810
811 size = dsparb & 0x7f;
812 if (plane)
813 size = ((dsparb >> DSPARB_CSTART_SHIFT) & 0x7f) - size;
814
815 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
816 plane ? "B" : "A", size);
817
818 return size;
819}
820
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300821static int i85x_get_fifo_size(struct drm_device *dev, int plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300822{
823 struct drm_i915_private *dev_priv = dev->dev_private;
824 uint32_t dsparb = I915_READ(DSPARB);
825 int size;
826
827 size = dsparb & 0x1ff;
828 if (plane)
829 size = ((dsparb >> DSPARB_BEND_SHIFT) & 0x1ff) - size;
830 size >>= 1; /* Convert to cachelines */
831
832 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
833 plane ? "B" : "A", size);
834
835 return size;
836}
837
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300838static int i845_get_fifo_size(struct drm_device *dev, int plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300839{
840 struct drm_i915_private *dev_priv = dev->dev_private;
841 uint32_t dsparb = I915_READ(DSPARB);
842 int size;
843
844 size = dsparb & 0x7f;
845 size >>= 2; /* Convert to cachelines */
846
847 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
848 plane ? "B" : "A",
849 size);
850
851 return size;
852}
853
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300854static int i830_get_fifo_size(struct drm_device *dev, int plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300855{
856 struct drm_i915_private *dev_priv = dev->dev_private;
857 uint32_t dsparb = I915_READ(DSPARB);
858 int size;
859
860 size = dsparb & 0x7f;
861 size >>= 1; /* Convert to cachelines */
862
863 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
864 plane ? "B" : "A", size);
865
866 return size;
867}
868
869/* Pineview has different values for various configs */
870static const struct intel_watermark_params pineview_display_wm = {
871 PINEVIEW_DISPLAY_FIFO,
872 PINEVIEW_MAX_WM,
873 PINEVIEW_DFT_WM,
874 PINEVIEW_GUARD_WM,
875 PINEVIEW_FIFO_LINE_SIZE
876};
877static const struct intel_watermark_params pineview_display_hplloff_wm = {
878 PINEVIEW_DISPLAY_FIFO,
879 PINEVIEW_MAX_WM,
880 PINEVIEW_DFT_HPLLOFF_WM,
881 PINEVIEW_GUARD_WM,
882 PINEVIEW_FIFO_LINE_SIZE
883};
884static const struct intel_watermark_params pineview_cursor_wm = {
885 PINEVIEW_CURSOR_FIFO,
886 PINEVIEW_CURSOR_MAX_WM,
887 PINEVIEW_CURSOR_DFT_WM,
888 PINEVIEW_CURSOR_GUARD_WM,
889 PINEVIEW_FIFO_LINE_SIZE,
890};
891static const struct intel_watermark_params pineview_cursor_hplloff_wm = {
892 PINEVIEW_CURSOR_FIFO,
893 PINEVIEW_CURSOR_MAX_WM,
894 PINEVIEW_CURSOR_DFT_WM,
895 PINEVIEW_CURSOR_GUARD_WM,
896 PINEVIEW_FIFO_LINE_SIZE
897};
898static const struct intel_watermark_params g4x_wm_info = {
899 G4X_FIFO_SIZE,
900 G4X_MAX_WM,
901 G4X_MAX_WM,
902 2,
903 G4X_FIFO_LINE_SIZE,
904};
905static const struct intel_watermark_params g4x_cursor_wm_info = {
906 I965_CURSOR_FIFO,
907 I965_CURSOR_MAX_WM,
908 I965_CURSOR_DFT_WM,
909 2,
910 G4X_FIFO_LINE_SIZE,
911};
912static const struct intel_watermark_params valleyview_wm_info = {
913 VALLEYVIEW_FIFO_SIZE,
914 VALLEYVIEW_MAX_WM,
915 VALLEYVIEW_MAX_WM,
916 2,
917 G4X_FIFO_LINE_SIZE,
918};
919static const struct intel_watermark_params valleyview_cursor_wm_info = {
920 I965_CURSOR_FIFO,
921 VALLEYVIEW_CURSOR_MAX_WM,
922 I965_CURSOR_DFT_WM,
923 2,
924 G4X_FIFO_LINE_SIZE,
925};
926static const struct intel_watermark_params i965_cursor_wm_info = {
927 I965_CURSOR_FIFO,
928 I965_CURSOR_MAX_WM,
929 I965_CURSOR_DFT_WM,
930 2,
931 I915_FIFO_LINE_SIZE,
932};
933static const struct intel_watermark_params i945_wm_info = {
934 I945_FIFO_SIZE,
935 I915_MAX_WM,
936 1,
937 2,
938 I915_FIFO_LINE_SIZE
939};
940static const struct intel_watermark_params i915_wm_info = {
941 I915_FIFO_SIZE,
942 I915_MAX_WM,
943 1,
944 2,
945 I915_FIFO_LINE_SIZE
946};
947static const struct intel_watermark_params i855_wm_info = {
948 I855GM_FIFO_SIZE,
949 I915_MAX_WM,
950 1,
951 2,
952 I830_FIFO_LINE_SIZE
953};
954static const struct intel_watermark_params i830_wm_info = {
955 I830_FIFO_SIZE,
956 I915_MAX_WM,
957 1,
958 2,
959 I830_FIFO_LINE_SIZE
960};
961
962static const struct intel_watermark_params ironlake_display_wm_info = {
963 ILK_DISPLAY_FIFO,
964 ILK_DISPLAY_MAXWM,
965 ILK_DISPLAY_DFTWM,
966 2,
967 ILK_FIFO_LINE_SIZE
968};
969static const struct intel_watermark_params ironlake_cursor_wm_info = {
970 ILK_CURSOR_FIFO,
971 ILK_CURSOR_MAXWM,
972 ILK_CURSOR_DFTWM,
973 2,
974 ILK_FIFO_LINE_SIZE
975};
976static const struct intel_watermark_params ironlake_display_srwm_info = {
977 ILK_DISPLAY_SR_FIFO,
978 ILK_DISPLAY_MAX_SRWM,
979 ILK_DISPLAY_DFT_SRWM,
980 2,
981 ILK_FIFO_LINE_SIZE
982};
983static const struct intel_watermark_params ironlake_cursor_srwm_info = {
984 ILK_CURSOR_SR_FIFO,
985 ILK_CURSOR_MAX_SRWM,
986 ILK_CURSOR_DFT_SRWM,
987 2,
988 ILK_FIFO_LINE_SIZE
989};
990
991static const struct intel_watermark_params sandybridge_display_wm_info = {
992 SNB_DISPLAY_FIFO,
993 SNB_DISPLAY_MAXWM,
994 SNB_DISPLAY_DFTWM,
995 2,
996 SNB_FIFO_LINE_SIZE
997};
998static const struct intel_watermark_params sandybridge_cursor_wm_info = {
999 SNB_CURSOR_FIFO,
1000 SNB_CURSOR_MAXWM,
1001 SNB_CURSOR_DFTWM,
1002 2,
1003 SNB_FIFO_LINE_SIZE
1004};
1005static const struct intel_watermark_params sandybridge_display_srwm_info = {
1006 SNB_DISPLAY_SR_FIFO,
1007 SNB_DISPLAY_MAX_SRWM,
1008 SNB_DISPLAY_DFT_SRWM,
1009 2,
1010 SNB_FIFO_LINE_SIZE
1011};
1012static const struct intel_watermark_params sandybridge_cursor_srwm_info = {
1013 SNB_CURSOR_SR_FIFO,
1014 SNB_CURSOR_MAX_SRWM,
1015 SNB_CURSOR_DFT_SRWM,
1016 2,
1017 SNB_FIFO_LINE_SIZE
1018};
1019
1020
1021/**
1022 * intel_calculate_wm - calculate watermark level
1023 * @clock_in_khz: pixel clock
1024 * @wm: chip FIFO params
1025 * @pixel_size: display pixel size
1026 * @latency_ns: memory latency for the platform
1027 *
1028 * Calculate the watermark level (the level at which the display plane will
1029 * start fetching from memory again). Each chip has a different display
1030 * FIFO size and allocation, so the caller needs to figure that out and pass
1031 * in the correct intel_watermark_params structure.
1032 *
1033 * As the pixel clock runs, the FIFO will be drained at a rate that depends
1034 * on the pixel size. When it reaches the watermark level, it'll start
1035 * fetching FIFO line sized based chunks from memory until the FIFO fills
1036 * past the watermark point. If the FIFO drains completely, a FIFO underrun
1037 * will occur, and a display engine hang could result.
1038 */
1039static unsigned long intel_calculate_wm(unsigned long clock_in_khz,
1040 const struct intel_watermark_params *wm,
1041 int fifo_size,
1042 int pixel_size,
1043 unsigned long latency_ns)
1044{
1045 long entries_required, wm_size;
1046
1047 /*
1048 * Note: we need to make sure we don't overflow for various clock &
1049 * latency values.
1050 * clocks go from a few thousand to several hundred thousand.
1051 * latency is usually a few thousand
1052 */
1053 entries_required = ((clock_in_khz / 1000) * pixel_size * latency_ns) /
1054 1000;
1055 entries_required = DIV_ROUND_UP(entries_required, wm->cacheline_size);
1056
1057 DRM_DEBUG_KMS("FIFO entries required for mode: %ld\n", entries_required);
1058
1059 wm_size = fifo_size - (entries_required + wm->guard_size);
1060
1061 DRM_DEBUG_KMS("FIFO watermark level: %ld\n", wm_size);
1062
1063 /* Don't promote wm_size to unsigned... */
1064 if (wm_size > (long)wm->max_wm)
1065 wm_size = wm->max_wm;
1066 if (wm_size <= 0)
1067 wm_size = wm->default_wm;
1068 return wm_size;
1069}
1070
1071static struct drm_crtc *single_enabled_crtc(struct drm_device *dev)
1072{
1073 struct drm_crtc *crtc, *enabled = NULL;
1074
1075 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Chris Wilson3490ea52013-01-07 10:11:40 +00001076 if (intel_crtc_active(crtc)) {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001077 if (enabled)
1078 return NULL;
1079 enabled = crtc;
1080 }
1081 }
1082
1083 return enabled;
1084}
1085
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001086static void pineview_update_wm(struct drm_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001087{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001088 struct drm_device *dev = unused_crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001089 struct drm_i915_private *dev_priv = dev->dev_private;
1090 struct drm_crtc *crtc;
1091 const struct cxsr_latency *latency;
1092 u32 reg;
1093 unsigned long wm;
1094
1095 latency = intel_get_cxsr_latency(IS_PINEVIEW_G(dev), dev_priv->is_ddr3,
1096 dev_priv->fsb_freq, dev_priv->mem_freq);
1097 if (!latency) {
1098 DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
1099 pineview_disable_cxsr(dev);
1100 return;
1101 }
1102
1103 crtc = single_enabled_crtc(dev);
1104 if (crtc) {
Ville Syrjälä4fe85902013-09-04 18:25:22 +03001105 int clock = to_intel_crtc(crtc)->config.adjusted_mode.clock;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001106 int pixel_size = crtc->fb->bits_per_pixel / 8;
1107
1108 /* Display SR */
1109 wm = intel_calculate_wm(clock, &pineview_display_wm,
1110 pineview_display_wm.fifo_size,
1111 pixel_size, latency->display_sr);
1112 reg = I915_READ(DSPFW1);
1113 reg &= ~DSPFW_SR_MASK;
1114 reg |= wm << DSPFW_SR_SHIFT;
1115 I915_WRITE(DSPFW1, reg);
1116 DRM_DEBUG_KMS("DSPFW1 register is %x\n", reg);
1117
1118 /* cursor SR */
1119 wm = intel_calculate_wm(clock, &pineview_cursor_wm,
1120 pineview_display_wm.fifo_size,
1121 pixel_size, latency->cursor_sr);
1122 reg = I915_READ(DSPFW3);
1123 reg &= ~DSPFW_CURSOR_SR_MASK;
1124 reg |= (wm & 0x3f) << DSPFW_CURSOR_SR_SHIFT;
1125 I915_WRITE(DSPFW3, reg);
1126
1127 /* Display HPLL off SR */
1128 wm = intel_calculate_wm(clock, &pineview_display_hplloff_wm,
1129 pineview_display_hplloff_wm.fifo_size,
1130 pixel_size, latency->display_hpll_disable);
1131 reg = I915_READ(DSPFW3);
1132 reg &= ~DSPFW_HPLL_SR_MASK;
1133 reg |= wm & DSPFW_HPLL_SR_MASK;
1134 I915_WRITE(DSPFW3, reg);
1135
1136 /* cursor HPLL off SR */
1137 wm = intel_calculate_wm(clock, &pineview_cursor_hplloff_wm,
1138 pineview_display_hplloff_wm.fifo_size,
1139 pixel_size, latency->cursor_hpll_disable);
1140 reg = I915_READ(DSPFW3);
1141 reg &= ~DSPFW_HPLL_CURSOR_MASK;
1142 reg |= (wm & 0x3f) << DSPFW_HPLL_CURSOR_SHIFT;
1143 I915_WRITE(DSPFW3, reg);
1144 DRM_DEBUG_KMS("DSPFW3 register is %x\n", reg);
1145
1146 /* activate cxsr */
1147 I915_WRITE(DSPFW3,
1148 I915_READ(DSPFW3) | PINEVIEW_SELF_REFRESH_EN);
1149 DRM_DEBUG_KMS("Self-refresh is enabled\n");
1150 } else {
1151 pineview_disable_cxsr(dev);
1152 DRM_DEBUG_KMS("Self-refresh is disabled\n");
1153 }
1154}
1155
1156static bool g4x_compute_wm0(struct drm_device *dev,
1157 int plane,
1158 const struct intel_watermark_params *display,
1159 int display_latency_ns,
1160 const struct intel_watermark_params *cursor,
1161 int cursor_latency_ns,
1162 int *plane_wm,
1163 int *cursor_wm)
1164{
1165 struct drm_crtc *crtc;
Ville Syrjälä4fe85902013-09-04 18:25:22 +03001166 const struct drm_display_mode *adjusted_mode;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001167 int htotal, hdisplay, clock, pixel_size;
1168 int line_time_us, line_count;
1169 int entries, tlb_miss;
1170
1171 crtc = intel_get_crtc_for_plane(dev, plane);
Chris Wilson3490ea52013-01-07 10:11:40 +00001172 if (!intel_crtc_active(crtc)) {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001173 *cursor_wm = cursor->guard_size;
1174 *plane_wm = display->guard_size;
1175 return false;
1176 }
1177
Ville Syrjälä4fe85902013-09-04 18:25:22 +03001178 adjusted_mode = &to_intel_crtc(crtc)->config.adjusted_mode;
1179 clock = adjusted_mode->clock;
1180 htotal = adjusted_mode->htotal;
1181 hdisplay = to_intel_crtc(crtc)->config.requested_mode.hdisplay;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001182 pixel_size = crtc->fb->bits_per_pixel / 8;
1183
1184 /* Use the small buffer method to calculate plane watermark */
1185 entries = ((clock * pixel_size / 1000) * display_latency_ns) / 1000;
1186 tlb_miss = display->fifo_size*display->cacheline_size - hdisplay * 8;
1187 if (tlb_miss > 0)
1188 entries += tlb_miss;
1189 entries = DIV_ROUND_UP(entries, display->cacheline_size);
1190 *plane_wm = entries + display->guard_size;
1191 if (*plane_wm > (int)display->max_wm)
1192 *plane_wm = display->max_wm;
1193
1194 /* Use the large buffer method to calculate cursor watermark */
1195 line_time_us = ((htotal * 1000) / clock);
1196 line_count = (cursor_latency_ns / line_time_us + 1000) / 1000;
1197 entries = line_count * 64 * pixel_size;
1198 tlb_miss = cursor->fifo_size*cursor->cacheline_size - hdisplay * 8;
1199 if (tlb_miss > 0)
1200 entries += tlb_miss;
1201 entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
1202 *cursor_wm = entries + cursor->guard_size;
1203 if (*cursor_wm > (int)cursor->max_wm)
1204 *cursor_wm = (int)cursor->max_wm;
1205
1206 return true;
1207}
1208
1209/*
1210 * Check the wm result.
1211 *
1212 * If any calculated watermark values is larger than the maximum value that
1213 * can be programmed into the associated watermark register, that watermark
1214 * must be disabled.
1215 */
1216static bool g4x_check_srwm(struct drm_device *dev,
1217 int display_wm, int cursor_wm,
1218 const struct intel_watermark_params *display,
1219 const struct intel_watermark_params *cursor)
1220{
1221 DRM_DEBUG_KMS("SR watermark: display plane %d, cursor %d\n",
1222 display_wm, cursor_wm);
1223
1224 if (display_wm > display->max_wm) {
1225 DRM_DEBUG_KMS("display watermark is too large(%d/%ld), disabling\n",
1226 display_wm, display->max_wm);
1227 return false;
1228 }
1229
1230 if (cursor_wm > cursor->max_wm) {
1231 DRM_DEBUG_KMS("cursor watermark is too large(%d/%ld), disabling\n",
1232 cursor_wm, cursor->max_wm);
1233 return false;
1234 }
1235
1236 if (!(display_wm || cursor_wm)) {
1237 DRM_DEBUG_KMS("SR latency is 0, disabling\n");
1238 return false;
1239 }
1240
1241 return true;
1242}
1243
1244static bool g4x_compute_srwm(struct drm_device *dev,
1245 int plane,
1246 int latency_ns,
1247 const struct intel_watermark_params *display,
1248 const struct intel_watermark_params *cursor,
1249 int *display_wm, int *cursor_wm)
1250{
1251 struct drm_crtc *crtc;
Ville Syrjälä4fe85902013-09-04 18:25:22 +03001252 const struct drm_display_mode *adjusted_mode;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001253 int hdisplay, htotal, pixel_size, clock;
1254 unsigned long line_time_us;
1255 int line_count, line_size;
1256 int small, large;
1257 int entries;
1258
1259 if (!latency_ns) {
1260 *display_wm = *cursor_wm = 0;
1261 return false;
1262 }
1263
1264 crtc = intel_get_crtc_for_plane(dev, plane);
Ville Syrjälä4fe85902013-09-04 18:25:22 +03001265 adjusted_mode = &to_intel_crtc(crtc)->config.adjusted_mode;
1266 clock = adjusted_mode->clock;
1267 htotal = adjusted_mode->htotal;
1268 hdisplay = to_intel_crtc(crtc)->config.requested_mode.hdisplay;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001269 pixel_size = crtc->fb->bits_per_pixel / 8;
1270
1271 line_time_us = (htotal * 1000) / clock;
1272 line_count = (latency_ns / line_time_us + 1000) / 1000;
1273 line_size = hdisplay * pixel_size;
1274
1275 /* Use the minimum of the small and large buffer method for primary */
1276 small = ((clock * pixel_size / 1000) * latency_ns) / 1000;
1277 large = line_count * line_size;
1278
1279 entries = DIV_ROUND_UP(min(small, large), display->cacheline_size);
1280 *display_wm = entries + display->guard_size;
1281
1282 /* calculate the self-refresh watermark for display cursor */
1283 entries = line_count * pixel_size * 64;
1284 entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
1285 *cursor_wm = entries + cursor->guard_size;
1286
1287 return g4x_check_srwm(dev,
1288 *display_wm, *cursor_wm,
1289 display, cursor);
1290}
1291
1292static bool vlv_compute_drain_latency(struct drm_device *dev,
1293 int plane,
1294 int *plane_prec_mult,
1295 int *plane_dl,
1296 int *cursor_prec_mult,
1297 int *cursor_dl)
1298{
1299 struct drm_crtc *crtc;
1300 int clock, pixel_size;
1301 int entries;
1302
1303 crtc = intel_get_crtc_for_plane(dev, plane);
Chris Wilson3490ea52013-01-07 10:11:40 +00001304 if (!intel_crtc_active(crtc))
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001305 return false;
1306
Ville Syrjälä4fe85902013-09-04 18:25:22 +03001307 clock = to_intel_crtc(crtc)->config.adjusted_mode.clock;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001308 pixel_size = crtc->fb->bits_per_pixel / 8; /* BPP */
1309
1310 entries = (clock / 1000) * pixel_size;
1311 *plane_prec_mult = (entries > 256) ?
1312 DRAIN_LATENCY_PRECISION_32 : DRAIN_LATENCY_PRECISION_16;
1313 *plane_dl = (64 * (*plane_prec_mult) * 4) / ((clock / 1000) *
1314 pixel_size);
1315
1316 entries = (clock / 1000) * 4; /* BPP is always 4 for cursor */
1317 *cursor_prec_mult = (entries > 256) ?
1318 DRAIN_LATENCY_PRECISION_32 : DRAIN_LATENCY_PRECISION_16;
1319 *cursor_dl = (64 * (*cursor_prec_mult) * 4) / ((clock / 1000) * 4);
1320
1321 return true;
1322}
1323
1324/*
1325 * Update drain latency registers of memory arbiter
1326 *
1327 * Valleyview SoC has a new memory arbiter and needs drain latency registers
1328 * to be programmed. Each plane has a drain latency multiplier and a drain
1329 * latency value.
1330 */
1331
1332static void vlv_update_drain_latency(struct drm_device *dev)
1333{
1334 struct drm_i915_private *dev_priv = dev->dev_private;
1335 int planea_prec, planea_dl, planeb_prec, planeb_dl;
1336 int cursora_prec, cursora_dl, cursorb_prec, cursorb_dl;
1337 int plane_prec_mult, cursor_prec_mult; /* Precision multiplier is
1338 either 16 or 32 */
1339
1340 /* For plane A, Cursor A */
1341 if (vlv_compute_drain_latency(dev, 0, &plane_prec_mult, &planea_dl,
1342 &cursor_prec_mult, &cursora_dl)) {
1343 cursora_prec = (cursor_prec_mult == DRAIN_LATENCY_PRECISION_32) ?
1344 DDL_CURSORA_PRECISION_32 : DDL_CURSORA_PRECISION_16;
1345 planea_prec = (plane_prec_mult == DRAIN_LATENCY_PRECISION_32) ?
1346 DDL_PLANEA_PRECISION_32 : DDL_PLANEA_PRECISION_16;
1347
1348 I915_WRITE(VLV_DDL1, cursora_prec |
1349 (cursora_dl << DDL_CURSORA_SHIFT) |
1350 planea_prec | planea_dl);
1351 }
1352
1353 /* For plane B, Cursor B */
1354 if (vlv_compute_drain_latency(dev, 1, &plane_prec_mult, &planeb_dl,
1355 &cursor_prec_mult, &cursorb_dl)) {
1356 cursorb_prec = (cursor_prec_mult == DRAIN_LATENCY_PRECISION_32) ?
1357 DDL_CURSORB_PRECISION_32 : DDL_CURSORB_PRECISION_16;
1358 planeb_prec = (plane_prec_mult == DRAIN_LATENCY_PRECISION_32) ?
1359 DDL_PLANEB_PRECISION_32 : DDL_PLANEB_PRECISION_16;
1360
1361 I915_WRITE(VLV_DDL2, cursorb_prec |
1362 (cursorb_dl << DDL_CURSORB_SHIFT) |
1363 planeb_prec | planeb_dl);
1364 }
1365}
1366
1367#define single_plane_enabled(mask) is_power_of_2(mask)
1368
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001369static void valleyview_update_wm(struct drm_crtc *crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001370{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001371 struct drm_device *dev = crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001372 static const int sr_latency_ns = 12000;
1373 struct drm_i915_private *dev_priv = dev->dev_private;
1374 int planea_wm, planeb_wm, cursora_wm, cursorb_wm;
1375 int plane_sr, cursor_sr;
Chris Wilsonaf6c4572012-12-11 12:01:43 +00001376 int ignore_plane_sr, ignore_cursor_sr;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001377 unsigned int enabled = 0;
1378
1379 vlv_update_drain_latency(dev);
1380
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001381 if (g4x_compute_wm0(dev, PIPE_A,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001382 &valleyview_wm_info, latency_ns,
1383 &valleyview_cursor_wm_info, latency_ns,
1384 &planea_wm, &cursora_wm))
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001385 enabled |= 1 << PIPE_A;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001386
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001387 if (g4x_compute_wm0(dev, PIPE_B,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001388 &valleyview_wm_info, latency_ns,
1389 &valleyview_cursor_wm_info, latency_ns,
1390 &planeb_wm, &cursorb_wm))
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001391 enabled |= 1 << PIPE_B;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001392
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001393 if (single_plane_enabled(enabled) &&
1394 g4x_compute_srwm(dev, ffs(enabled) - 1,
1395 sr_latency_ns,
1396 &valleyview_wm_info,
1397 &valleyview_cursor_wm_info,
Chris Wilsonaf6c4572012-12-11 12:01:43 +00001398 &plane_sr, &ignore_cursor_sr) &&
1399 g4x_compute_srwm(dev, ffs(enabled) - 1,
1400 2*sr_latency_ns,
1401 &valleyview_wm_info,
1402 &valleyview_cursor_wm_info,
Chris Wilson52bd02d2012-12-07 10:43:24 +00001403 &ignore_plane_sr, &cursor_sr)) {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001404 I915_WRITE(FW_BLC_SELF_VLV, FW_CSPWRDWNEN);
Chris Wilson52bd02d2012-12-07 10:43:24 +00001405 } else {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001406 I915_WRITE(FW_BLC_SELF_VLV,
1407 I915_READ(FW_BLC_SELF_VLV) & ~FW_CSPWRDWNEN);
Chris Wilson52bd02d2012-12-07 10:43:24 +00001408 plane_sr = cursor_sr = 0;
1409 }
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001410
1411 DRM_DEBUG_KMS("Setting FIFO watermarks - A: plane=%d, cursor=%d, B: plane=%d, cursor=%d, SR: plane=%d, cursor=%d\n",
1412 planea_wm, cursora_wm,
1413 planeb_wm, cursorb_wm,
1414 plane_sr, cursor_sr);
1415
1416 I915_WRITE(DSPFW1,
1417 (plane_sr << DSPFW_SR_SHIFT) |
1418 (cursorb_wm << DSPFW_CURSORB_SHIFT) |
1419 (planeb_wm << DSPFW_PLANEB_SHIFT) |
1420 planea_wm);
1421 I915_WRITE(DSPFW2,
Chris Wilson8c919b22012-12-04 16:33:19 +00001422 (I915_READ(DSPFW2) & ~DSPFW_CURSORA_MASK) |
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001423 (cursora_wm << DSPFW_CURSORA_SHIFT));
1424 I915_WRITE(DSPFW3,
Chris Wilson8c919b22012-12-04 16:33:19 +00001425 (I915_READ(DSPFW3) & ~DSPFW_CURSOR_SR_MASK) |
1426 (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001427}
1428
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001429static void g4x_update_wm(struct drm_crtc *crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001430{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001431 struct drm_device *dev = crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001432 static const int sr_latency_ns = 12000;
1433 struct drm_i915_private *dev_priv = dev->dev_private;
1434 int planea_wm, planeb_wm, cursora_wm, cursorb_wm;
1435 int plane_sr, cursor_sr;
1436 unsigned int enabled = 0;
1437
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001438 if (g4x_compute_wm0(dev, PIPE_A,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001439 &g4x_wm_info, latency_ns,
1440 &g4x_cursor_wm_info, latency_ns,
1441 &planea_wm, &cursora_wm))
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001442 enabled |= 1 << PIPE_A;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001443
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001444 if (g4x_compute_wm0(dev, PIPE_B,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001445 &g4x_wm_info, latency_ns,
1446 &g4x_cursor_wm_info, latency_ns,
1447 &planeb_wm, &cursorb_wm))
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001448 enabled |= 1 << PIPE_B;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001449
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001450 if (single_plane_enabled(enabled) &&
1451 g4x_compute_srwm(dev, ffs(enabled) - 1,
1452 sr_latency_ns,
1453 &g4x_wm_info,
1454 &g4x_cursor_wm_info,
Chris Wilson52bd02d2012-12-07 10:43:24 +00001455 &plane_sr, &cursor_sr)) {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001456 I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_EN);
Chris Wilson52bd02d2012-12-07 10:43:24 +00001457 } else {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001458 I915_WRITE(FW_BLC_SELF,
1459 I915_READ(FW_BLC_SELF) & ~FW_BLC_SELF_EN);
Chris Wilson52bd02d2012-12-07 10:43:24 +00001460 plane_sr = cursor_sr = 0;
1461 }
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001462
1463 DRM_DEBUG_KMS("Setting FIFO watermarks - A: plane=%d, cursor=%d, B: plane=%d, cursor=%d, SR: plane=%d, cursor=%d\n",
1464 planea_wm, cursora_wm,
1465 planeb_wm, cursorb_wm,
1466 plane_sr, cursor_sr);
1467
1468 I915_WRITE(DSPFW1,
1469 (plane_sr << DSPFW_SR_SHIFT) |
1470 (cursorb_wm << DSPFW_CURSORB_SHIFT) |
1471 (planeb_wm << DSPFW_PLANEB_SHIFT) |
1472 planea_wm);
1473 I915_WRITE(DSPFW2,
Chris Wilson8c919b22012-12-04 16:33:19 +00001474 (I915_READ(DSPFW2) & ~DSPFW_CURSORA_MASK) |
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001475 (cursora_wm << DSPFW_CURSORA_SHIFT));
1476 /* HPLL off in SR has some issues on G4x... disable it */
1477 I915_WRITE(DSPFW3,
Chris Wilson8c919b22012-12-04 16:33:19 +00001478 (I915_READ(DSPFW3) & ~(DSPFW_HPLL_SR_EN | DSPFW_CURSOR_SR_MASK)) |
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001479 (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
1480}
1481
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001482static void i965_update_wm(struct drm_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001483{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001484 struct drm_device *dev = unused_crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001485 struct drm_i915_private *dev_priv = dev->dev_private;
1486 struct drm_crtc *crtc;
1487 int srwm = 1;
1488 int cursor_sr = 16;
1489
1490 /* Calc sr entries for one plane configs */
1491 crtc = single_enabled_crtc(dev);
1492 if (crtc) {
1493 /* self-refresh has much higher latency */
1494 static const int sr_latency_ns = 12000;
Ville Syrjälä4fe85902013-09-04 18:25:22 +03001495 const struct drm_display_mode *adjusted_mode =
1496 &to_intel_crtc(crtc)->config.adjusted_mode;
1497 int clock = adjusted_mode->clock;
1498 int htotal = adjusted_mode->htotal;
1499 int hdisplay = to_intel_crtc(crtc)->config.requested_mode.hdisplay;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001500 int pixel_size = crtc->fb->bits_per_pixel / 8;
1501 unsigned long line_time_us;
1502 int entries;
1503
1504 line_time_us = ((htotal * 1000) / clock);
1505
1506 /* Use ns/us then divide to preserve precision */
1507 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
1508 pixel_size * hdisplay;
1509 entries = DIV_ROUND_UP(entries, I915_FIFO_LINE_SIZE);
1510 srwm = I965_FIFO_SIZE - entries;
1511 if (srwm < 0)
1512 srwm = 1;
1513 srwm &= 0x1ff;
1514 DRM_DEBUG_KMS("self-refresh entries: %d, wm: %d\n",
1515 entries, srwm);
1516
1517 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
1518 pixel_size * 64;
1519 entries = DIV_ROUND_UP(entries,
1520 i965_cursor_wm_info.cacheline_size);
1521 cursor_sr = i965_cursor_wm_info.fifo_size -
1522 (entries + i965_cursor_wm_info.guard_size);
1523
1524 if (cursor_sr > i965_cursor_wm_info.max_wm)
1525 cursor_sr = i965_cursor_wm_info.max_wm;
1526
1527 DRM_DEBUG_KMS("self-refresh watermark: display plane %d "
1528 "cursor %d\n", srwm, cursor_sr);
1529
1530 if (IS_CRESTLINE(dev))
1531 I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_EN);
1532 } else {
1533 /* Turn off self refresh if both pipes are enabled */
1534 if (IS_CRESTLINE(dev))
1535 I915_WRITE(FW_BLC_SELF, I915_READ(FW_BLC_SELF)
1536 & ~FW_BLC_SELF_EN);
1537 }
1538
1539 DRM_DEBUG_KMS("Setting FIFO watermarks - A: 8, B: 8, C: 8, SR %d\n",
1540 srwm);
1541
1542 /* 965 has limitations... */
1543 I915_WRITE(DSPFW1, (srwm << DSPFW_SR_SHIFT) |
1544 (8 << 16) | (8 << 8) | (8 << 0));
1545 I915_WRITE(DSPFW2, (8 << 8) | (8 << 0));
1546 /* update cursor SR watermark */
1547 I915_WRITE(DSPFW3, (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
1548}
1549
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001550static void i9xx_update_wm(struct drm_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001551{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001552 struct drm_device *dev = unused_crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001553 struct drm_i915_private *dev_priv = dev->dev_private;
1554 const struct intel_watermark_params *wm_info;
1555 uint32_t fwater_lo;
1556 uint32_t fwater_hi;
1557 int cwm, srwm = 1;
1558 int fifo_size;
1559 int planea_wm, planeb_wm;
1560 struct drm_crtc *crtc, *enabled = NULL;
1561
1562 if (IS_I945GM(dev))
1563 wm_info = &i945_wm_info;
1564 else if (!IS_GEN2(dev))
1565 wm_info = &i915_wm_info;
1566 else
1567 wm_info = &i855_wm_info;
1568
1569 fifo_size = dev_priv->display.get_fifo_size(dev, 0);
1570 crtc = intel_get_crtc_for_plane(dev, 0);
Chris Wilson3490ea52013-01-07 10:11:40 +00001571 if (intel_crtc_active(crtc)) {
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001572 int cpp = crtc->fb->bits_per_pixel / 8;
1573 if (IS_GEN2(dev))
1574 cpp = 4;
1575
Ville Syrjälä4fe85902013-09-04 18:25:22 +03001576 planea_wm = intel_calculate_wm(to_intel_crtc(crtc)->config.adjusted_mode.clock,
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001577 wm_info, fifo_size, cpp,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001578 latency_ns);
1579 enabled = crtc;
1580 } else
1581 planea_wm = fifo_size - wm_info->guard_size;
1582
1583 fifo_size = dev_priv->display.get_fifo_size(dev, 1);
1584 crtc = intel_get_crtc_for_plane(dev, 1);
Chris Wilson3490ea52013-01-07 10:11:40 +00001585 if (intel_crtc_active(crtc)) {
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001586 int cpp = crtc->fb->bits_per_pixel / 8;
1587 if (IS_GEN2(dev))
1588 cpp = 4;
1589
Ville Syrjälä4fe85902013-09-04 18:25:22 +03001590 planeb_wm = intel_calculate_wm(to_intel_crtc(crtc)->config.adjusted_mode.clock,
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001591 wm_info, fifo_size, cpp,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001592 latency_ns);
1593 if (enabled == NULL)
1594 enabled = crtc;
1595 else
1596 enabled = NULL;
1597 } else
1598 planeb_wm = fifo_size - wm_info->guard_size;
1599
1600 DRM_DEBUG_KMS("FIFO watermarks - A: %d, B: %d\n", planea_wm, planeb_wm);
1601
1602 /*
1603 * Overlay gets an aggressive default since video jitter is bad.
1604 */
1605 cwm = 2;
1606
1607 /* Play safe and disable self-refresh before adjusting watermarks. */
1608 if (IS_I945G(dev) || IS_I945GM(dev))
1609 I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_EN_MASK | 0);
1610 else if (IS_I915GM(dev))
1611 I915_WRITE(INSTPM, I915_READ(INSTPM) & ~INSTPM_SELF_EN);
1612
1613 /* Calc sr entries for one plane configs */
1614 if (HAS_FW_BLC(dev) && enabled) {
1615 /* self-refresh has much higher latency */
1616 static const int sr_latency_ns = 6000;
Ville Syrjälä4fe85902013-09-04 18:25:22 +03001617 const struct drm_display_mode *adjusted_mode =
1618 &to_intel_crtc(enabled)->config.adjusted_mode;
1619 int clock = adjusted_mode->clock;
1620 int htotal = adjusted_mode->htotal;
1621 int hdisplay = to_intel_crtc(crtc)->config.requested_mode.hdisplay;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001622 int pixel_size = enabled->fb->bits_per_pixel / 8;
1623 unsigned long line_time_us;
1624 int entries;
1625
1626 line_time_us = (htotal * 1000) / clock;
1627
1628 /* Use ns/us then divide to preserve precision */
1629 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
1630 pixel_size * hdisplay;
1631 entries = DIV_ROUND_UP(entries, wm_info->cacheline_size);
1632 DRM_DEBUG_KMS("self-refresh entries: %d\n", entries);
1633 srwm = wm_info->fifo_size - entries;
1634 if (srwm < 0)
1635 srwm = 1;
1636
1637 if (IS_I945G(dev) || IS_I945GM(dev))
1638 I915_WRITE(FW_BLC_SELF,
1639 FW_BLC_SELF_FIFO_MASK | (srwm & 0xff));
1640 else if (IS_I915GM(dev))
1641 I915_WRITE(FW_BLC_SELF, srwm & 0x3f);
1642 }
1643
1644 DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d, B: %d, C: %d, SR %d\n",
1645 planea_wm, planeb_wm, cwm, srwm);
1646
1647 fwater_lo = ((planeb_wm & 0x3f) << 16) | (planea_wm & 0x3f);
1648 fwater_hi = (cwm & 0x1f);
1649
1650 /* Set request length to 8 cachelines per fetch */
1651 fwater_lo = fwater_lo | (1 << 24) | (1 << 8);
1652 fwater_hi = fwater_hi | (1 << 8);
1653
1654 I915_WRITE(FW_BLC, fwater_lo);
1655 I915_WRITE(FW_BLC2, fwater_hi);
1656
1657 if (HAS_FW_BLC(dev)) {
1658 if (enabled) {
1659 if (IS_I945G(dev) || IS_I945GM(dev))
1660 I915_WRITE(FW_BLC_SELF,
1661 FW_BLC_SELF_EN_MASK | FW_BLC_SELF_EN);
1662 else if (IS_I915GM(dev))
1663 I915_WRITE(INSTPM, I915_READ(INSTPM) | INSTPM_SELF_EN);
1664 DRM_DEBUG_KMS("memory self refresh enabled\n");
1665 } else
1666 DRM_DEBUG_KMS("memory self refresh disabled\n");
1667 }
1668}
1669
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001670static void i830_update_wm(struct drm_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001671{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001672 struct drm_device *dev = unused_crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001673 struct drm_i915_private *dev_priv = dev->dev_private;
1674 struct drm_crtc *crtc;
1675 uint32_t fwater_lo;
1676 int planea_wm;
1677
1678 crtc = single_enabled_crtc(dev);
1679 if (crtc == NULL)
1680 return;
1681
Ville Syrjälä4fe85902013-09-04 18:25:22 +03001682 planea_wm = intel_calculate_wm(to_intel_crtc(crtc)->config.adjusted_mode.clock,
1683 &i830_wm_info,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001684 dev_priv->display.get_fifo_size(dev, 0),
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001685 4, latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001686 fwater_lo = I915_READ(FW_BLC) & ~0xfff;
1687 fwater_lo |= (3<<8) | planea_wm;
1688
1689 DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d\n", planea_wm);
1690
1691 I915_WRITE(FW_BLC, fwater_lo);
1692}
1693
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001694/*
1695 * Check the wm result.
1696 *
1697 * If any calculated watermark values is larger than the maximum value that
1698 * can be programmed into the associated watermark register, that watermark
1699 * must be disabled.
1700 */
1701static bool ironlake_check_srwm(struct drm_device *dev, int level,
1702 int fbc_wm, int display_wm, int cursor_wm,
1703 const struct intel_watermark_params *display,
1704 const struct intel_watermark_params *cursor)
1705{
1706 struct drm_i915_private *dev_priv = dev->dev_private;
1707
1708 DRM_DEBUG_KMS("watermark %d: display plane %d, fbc lines %d,"
1709 " cursor %d\n", level, display_wm, fbc_wm, cursor_wm);
1710
1711 if (fbc_wm > SNB_FBC_MAX_SRWM) {
1712 DRM_DEBUG_KMS("fbc watermark(%d) is too large(%d), disabling wm%d+\n",
1713 fbc_wm, SNB_FBC_MAX_SRWM, level);
1714
1715 /* fbc has it's own way to disable FBC WM */
1716 I915_WRITE(DISP_ARB_CTL,
1717 I915_READ(DISP_ARB_CTL) | DISP_FBC_WM_DIS);
1718 return false;
Ville Syrjälä615aaa52013-04-24 21:09:10 +03001719 } else if (INTEL_INFO(dev)->gen >= 6) {
1720 /* enable FBC WM (except on ILK, where it must remain off) */
1721 I915_WRITE(DISP_ARB_CTL,
1722 I915_READ(DISP_ARB_CTL) & ~DISP_FBC_WM_DIS);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001723 }
1724
1725 if (display_wm > display->max_wm) {
1726 DRM_DEBUG_KMS("display watermark(%d) is too large(%d), disabling wm%d+\n",
1727 display_wm, SNB_DISPLAY_MAX_SRWM, level);
1728 return false;
1729 }
1730
1731 if (cursor_wm > cursor->max_wm) {
1732 DRM_DEBUG_KMS("cursor watermark(%d) is too large(%d), disabling wm%d+\n",
1733 cursor_wm, SNB_CURSOR_MAX_SRWM, level);
1734 return false;
1735 }
1736
1737 if (!(fbc_wm || display_wm || cursor_wm)) {
1738 DRM_DEBUG_KMS("latency %d is 0, disabling wm%d+\n", level, level);
1739 return false;
1740 }
1741
1742 return true;
1743}
1744
1745/*
1746 * Compute watermark values of WM[1-3],
1747 */
1748static bool ironlake_compute_srwm(struct drm_device *dev, int level, int plane,
1749 int latency_ns,
1750 const struct intel_watermark_params *display,
1751 const struct intel_watermark_params *cursor,
1752 int *fbc_wm, int *display_wm, int *cursor_wm)
1753{
1754 struct drm_crtc *crtc;
Ville Syrjälä4fe85902013-09-04 18:25:22 +03001755 const struct drm_display_mode *adjusted_mode;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001756 unsigned long line_time_us;
1757 int hdisplay, htotal, pixel_size, clock;
1758 int line_count, line_size;
1759 int small, large;
1760 int entries;
1761
1762 if (!latency_ns) {
1763 *fbc_wm = *display_wm = *cursor_wm = 0;
1764 return false;
1765 }
1766
1767 crtc = intel_get_crtc_for_plane(dev, plane);
Ville Syrjälä4fe85902013-09-04 18:25:22 +03001768 adjusted_mode = &to_intel_crtc(crtc)->config.adjusted_mode;
1769 clock = adjusted_mode->clock;
1770 htotal = adjusted_mode->htotal;
1771 hdisplay = to_intel_crtc(crtc)->config.requested_mode.hdisplay;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001772 pixel_size = crtc->fb->bits_per_pixel / 8;
1773
1774 line_time_us = (htotal * 1000) / clock;
1775 line_count = (latency_ns / line_time_us + 1000) / 1000;
1776 line_size = hdisplay * pixel_size;
1777
1778 /* Use the minimum of the small and large buffer method for primary */
1779 small = ((clock * pixel_size / 1000) * latency_ns) / 1000;
1780 large = line_count * line_size;
1781
1782 entries = DIV_ROUND_UP(min(small, large), display->cacheline_size);
1783 *display_wm = entries + display->guard_size;
1784
1785 /*
1786 * Spec says:
1787 * FBC WM = ((Final Primary WM * 64) / number of bytes per line) + 2
1788 */
1789 *fbc_wm = DIV_ROUND_UP(*display_wm * 64, line_size) + 2;
1790
1791 /* calculate the self-refresh watermark for display cursor */
1792 entries = line_count * pixel_size * 64;
1793 entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
1794 *cursor_wm = entries + cursor->guard_size;
1795
1796 return ironlake_check_srwm(dev, level,
1797 *fbc_wm, *display_wm, *cursor_wm,
1798 display, cursor);
1799}
1800
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001801static void ironlake_update_wm(struct drm_crtc *crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001802{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001803 struct drm_device *dev = crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001804 struct drm_i915_private *dev_priv = dev->dev_private;
1805 int fbc_wm, plane_wm, cursor_wm;
1806 unsigned int enabled;
1807
1808 enabled = 0;
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001809 if (g4x_compute_wm0(dev, PIPE_A,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001810 &ironlake_display_wm_info,
Ville Syrjäläb0aea5d2013-08-01 16:18:54 +03001811 dev_priv->wm.pri_latency[0] * 100,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001812 &ironlake_cursor_wm_info,
Ville Syrjäläb0aea5d2013-08-01 16:18:54 +03001813 dev_priv->wm.cur_latency[0] * 100,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001814 &plane_wm, &cursor_wm)) {
1815 I915_WRITE(WM0_PIPEA_ILK,
1816 (plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm);
1817 DRM_DEBUG_KMS("FIFO watermarks For pipe A -"
1818 " plane %d, " "cursor: %d\n",
1819 plane_wm, cursor_wm);
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001820 enabled |= 1 << PIPE_A;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001821 }
1822
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001823 if (g4x_compute_wm0(dev, PIPE_B,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001824 &ironlake_display_wm_info,
Ville Syrjäläb0aea5d2013-08-01 16:18:54 +03001825 dev_priv->wm.pri_latency[0] * 100,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001826 &ironlake_cursor_wm_info,
Ville Syrjäläb0aea5d2013-08-01 16:18:54 +03001827 dev_priv->wm.cur_latency[0] * 100,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001828 &plane_wm, &cursor_wm)) {
1829 I915_WRITE(WM0_PIPEB_ILK,
1830 (plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm);
1831 DRM_DEBUG_KMS("FIFO watermarks For pipe B -"
1832 " plane %d, cursor: %d\n",
1833 plane_wm, cursor_wm);
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001834 enabled |= 1 << PIPE_B;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001835 }
1836
1837 /*
1838 * Calculate and update the self-refresh watermark only when one
1839 * display plane is used.
1840 */
1841 I915_WRITE(WM3_LP_ILK, 0);
1842 I915_WRITE(WM2_LP_ILK, 0);
1843 I915_WRITE(WM1_LP_ILK, 0);
1844
1845 if (!single_plane_enabled(enabled))
1846 return;
1847 enabled = ffs(enabled) - 1;
1848
1849 /* WM1 */
1850 if (!ironlake_compute_srwm(dev, 1, enabled,
Ville Syrjäläb0aea5d2013-08-01 16:18:54 +03001851 dev_priv->wm.pri_latency[1] * 500,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001852 &ironlake_display_srwm_info,
1853 &ironlake_cursor_srwm_info,
1854 &fbc_wm, &plane_wm, &cursor_wm))
1855 return;
1856
1857 I915_WRITE(WM1_LP_ILK,
1858 WM1_LP_SR_EN |
Ville Syrjäläb0aea5d2013-08-01 16:18:54 +03001859 (dev_priv->wm.pri_latency[1] << WM1_LP_LATENCY_SHIFT) |
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001860 (fbc_wm << WM1_LP_FBC_SHIFT) |
1861 (plane_wm << WM1_LP_SR_SHIFT) |
1862 cursor_wm);
1863
1864 /* WM2 */
1865 if (!ironlake_compute_srwm(dev, 2, enabled,
Ville Syrjäläb0aea5d2013-08-01 16:18:54 +03001866 dev_priv->wm.pri_latency[2] * 500,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001867 &ironlake_display_srwm_info,
1868 &ironlake_cursor_srwm_info,
1869 &fbc_wm, &plane_wm, &cursor_wm))
1870 return;
1871
1872 I915_WRITE(WM2_LP_ILK,
1873 WM2_LP_EN |
Ville Syrjäläb0aea5d2013-08-01 16:18:54 +03001874 (dev_priv->wm.pri_latency[2] << WM1_LP_LATENCY_SHIFT) |
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001875 (fbc_wm << WM1_LP_FBC_SHIFT) |
1876 (plane_wm << WM1_LP_SR_SHIFT) |
1877 cursor_wm);
1878
1879 /*
1880 * WM3 is unsupported on ILK, probably because we don't have latency
1881 * data for that power state
1882 */
1883}
1884
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001885static void sandybridge_update_wm(struct drm_crtc *crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001886{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001887 struct drm_device *dev = crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001888 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläb0aea5d2013-08-01 16:18:54 +03001889 int latency = dev_priv->wm.pri_latency[0] * 100; /* In unit 0.1us */
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001890 u32 val;
1891 int fbc_wm, plane_wm, cursor_wm;
1892 unsigned int enabled;
1893
1894 enabled = 0;
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001895 if (g4x_compute_wm0(dev, PIPE_A,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001896 &sandybridge_display_wm_info, latency,
1897 &sandybridge_cursor_wm_info, latency,
1898 &plane_wm, &cursor_wm)) {
1899 val = I915_READ(WM0_PIPEA_ILK);
1900 val &= ~(WM0_PIPE_PLANE_MASK | WM0_PIPE_CURSOR_MASK);
1901 I915_WRITE(WM0_PIPEA_ILK, val |
1902 ((plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm));
1903 DRM_DEBUG_KMS("FIFO watermarks For pipe A -"
1904 " plane %d, " "cursor: %d\n",
1905 plane_wm, cursor_wm);
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001906 enabled |= 1 << PIPE_A;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001907 }
1908
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001909 if (g4x_compute_wm0(dev, PIPE_B,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001910 &sandybridge_display_wm_info, latency,
1911 &sandybridge_cursor_wm_info, latency,
1912 &plane_wm, &cursor_wm)) {
1913 val = I915_READ(WM0_PIPEB_ILK);
1914 val &= ~(WM0_PIPE_PLANE_MASK | WM0_PIPE_CURSOR_MASK);
1915 I915_WRITE(WM0_PIPEB_ILK, val |
1916 ((plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm));
1917 DRM_DEBUG_KMS("FIFO watermarks For pipe B -"
1918 " plane %d, cursor: %d\n",
1919 plane_wm, cursor_wm);
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001920 enabled |= 1 << PIPE_B;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001921 }
1922
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001923 /*
1924 * Calculate and update the self-refresh watermark only when one
1925 * display plane is used.
1926 *
1927 * SNB support 3 levels of watermark.
1928 *
1929 * WM1/WM2/WM2 watermarks have to be enabled in the ascending order,
1930 * and disabled in the descending order
1931 *
1932 */
1933 I915_WRITE(WM3_LP_ILK, 0);
1934 I915_WRITE(WM2_LP_ILK, 0);
1935 I915_WRITE(WM1_LP_ILK, 0);
1936
1937 if (!single_plane_enabled(enabled) ||
1938 dev_priv->sprite_scaling_enabled)
1939 return;
1940 enabled = ffs(enabled) - 1;
1941
1942 /* WM1 */
1943 if (!ironlake_compute_srwm(dev, 1, enabled,
Ville Syrjäläb0aea5d2013-08-01 16:18:54 +03001944 dev_priv->wm.pri_latency[1] * 500,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001945 &sandybridge_display_srwm_info,
1946 &sandybridge_cursor_srwm_info,
1947 &fbc_wm, &plane_wm, &cursor_wm))
1948 return;
1949
1950 I915_WRITE(WM1_LP_ILK,
1951 WM1_LP_SR_EN |
Ville Syrjäläb0aea5d2013-08-01 16:18:54 +03001952 (dev_priv->wm.pri_latency[1] << WM1_LP_LATENCY_SHIFT) |
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001953 (fbc_wm << WM1_LP_FBC_SHIFT) |
1954 (plane_wm << WM1_LP_SR_SHIFT) |
1955 cursor_wm);
1956
1957 /* WM2 */
1958 if (!ironlake_compute_srwm(dev, 2, enabled,
Ville Syrjäläb0aea5d2013-08-01 16:18:54 +03001959 dev_priv->wm.pri_latency[2] * 500,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001960 &sandybridge_display_srwm_info,
1961 &sandybridge_cursor_srwm_info,
1962 &fbc_wm, &plane_wm, &cursor_wm))
1963 return;
1964
1965 I915_WRITE(WM2_LP_ILK,
1966 WM2_LP_EN |
Ville Syrjäläb0aea5d2013-08-01 16:18:54 +03001967 (dev_priv->wm.pri_latency[2] << WM1_LP_LATENCY_SHIFT) |
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001968 (fbc_wm << WM1_LP_FBC_SHIFT) |
1969 (plane_wm << WM1_LP_SR_SHIFT) |
1970 cursor_wm);
1971
1972 /* WM3 */
1973 if (!ironlake_compute_srwm(dev, 3, enabled,
Ville Syrjäläb0aea5d2013-08-01 16:18:54 +03001974 dev_priv->wm.pri_latency[3] * 500,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001975 &sandybridge_display_srwm_info,
1976 &sandybridge_cursor_srwm_info,
1977 &fbc_wm, &plane_wm, &cursor_wm))
1978 return;
1979
1980 I915_WRITE(WM3_LP_ILK,
1981 WM3_LP_EN |
Ville Syrjäläb0aea5d2013-08-01 16:18:54 +03001982 (dev_priv->wm.pri_latency[3] << WM1_LP_LATENCY_SHIFT) |
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001983 (fbc_wm << WM1_LP_FBC_SHIFT) |
1984 (plane_wm << WM1_LP_SR_SHIFT) |
1985 cursor_wm);
1986}
1987
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001988static void ivybridge_update_wm(struct drm_crtc *crtc)
Chris Wilsonc43d0182012-12-11 12:01:42 +00001989{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001990 struct drm_device *dev = crtc->dev;
Chris Wilsonc43d0182012-12-11 12:01:42 +00001991 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläb0aea5d2013-08-01 16:18:54 +03001992 int latency = dev_priv->wm.pri_latency[0] * 100; /* In unit 0.1us */
Chris Wilsonc43d0182012-12-11 12:01:42 +00001993 u32 val;
1994 int fbc_wm, plane_wm, cursor_wm;
1995 int ignore_fbc_wm, ignore_plane_wm, ignore_cursor_wm;
1996 unsigned int enabled;
1997
1998 enabled = 0;
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001999 if (g4x_compute_wm0(dev, PIPE_A,
Chris Wilsonc43d0182012-12-11 12:01:42 +00002000 &sandybridge_display_wm_info, latency,
2001 &sandybridge_cursor_wm_info, latency,
2002 &plane_wm, &cursor_wm)) {
2003 val = I915_READ(WM0_PIPEA_ILK);
2004 val &= ~(WM0_PIPE_PLANE_MASK | WM0_PIPE_CURSOR_MASK);
2005 I915_WRITE(WM0_PIPEA_ILK, val |
2006 ((plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm));
2007 DRM_DEBUG_KMS("FIFO watermarks For pipe A -"
2008 " plane %d, " "cursor: %d\n",
2009 plane_wm, cursor_wm);
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02002010 enabled |= 1 << PIPE_A;
Chris Wilsonc43d0182012-12-11 12:01:42 +00002011 }
2012
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02002013 if (g4x_compute_wm0(dev, PIPE_B,
Chris Wilsonc43d0182012-12-11 12:01:42 +00002014 &sandybridge_display_wm_info, latency,
2015 &sandybridge_cursor_wm_info, latency,
2016 &plane_wm, &cursor_wm)) {
2017 val = I915_READ(WM0_PIPEB_ILK);
2018 val &= ~(WM0_PIPE_PLANE_MASK | WM0_PIPE_CURSOR_MASK);
2019 I915_WRITE(WM0_PIPEB_ILK, val |
2020 ((plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm));
2021 DRM_DEBUG_KMS("FIFO watermarks For pipe B -"
2022 " plane %d, cursor: %d\n",
2023 plane_wm, cursor_wm);
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02002024 enabled |= 1 << PIPE_B;
Chris Wilsonc43d0182012-12-11 12:01:42 +00002025 }
2026
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02002027 if (g4x_compute_wm0(dev, PIPE_C,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002028 &sandybridge_display_wm_info, latency,
2029 &sandybridge_cursor_wm_info, latency,
2030 &plane_wm, &cursor_wm)) {
2031 val = I915_READ(WM0_PIPEC_IVB);
2032 val &= ~(WM0_PIPE_PLANE_MASK | WM0_PIPE_CURSOR_MASK);
2033 I915_WRITE(WM0_PIPEC_IVB, val |
2034 ((plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm));
2035 DRM_DEBUG_KMS("FIFO watermarks For pipe C -"
2036 " plane %d, cursor: %d\n",
2037 plane_wm, cursor_wm);
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02002038 enabled |= 1 << PIPE_C;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002039 }
2040
2041 /*
2042 * Calculate and update the self-refresh watermark only when one
2043 * display plane is used.
2044 *
2045 * SNB support 3 levels of watermark.
2046 *
2047 * WM1/WM2/WM2 watermarks have to be enabled in the ascending order,
2048 * and disabled in the descending order
2049 *
2050 */
2051 I915_WRITE(WM3_LP_ILK, 0);
2052 I915_WRITE(WM2_LP_ILK, 0);
2053 I915_WRITE(WM1_LP_ILK, 0);
2054
2055 if (!single_plane_enabled(enabled) ||
2056 dev_priv->sprite_scaling_enabled)
2057 return;
2058 enabled = ffs(enabled) - 1;
2059
2060 /* WM1 */
2061 if (!ironlake_compute_srwm(dev, 1, enabled,
Ville Syrjäläb0aea5d2013-08-01 16:18:54 +03002062 dev_priv->wm.pri_latency[1] * 500,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002063 &sandybridge_display_srwm_info,
2064 &sandybridge_cursor_srwm_info,
2065 &fbc_wm, &plane_wm, &cursor_wm))
2066 return;
2067
2068 I915_WRITE(WM1_LP_ILK,
2069 WM1_LP_SR_EN |
Ville Syrjäläb0aea5d2013-08-01 16:18:54 +03002070 (dev_priv->wm.pri_latency[1] << WM1_LP_LATENCY_SHIFT) |
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002071 (fbc_wm << WM1_LP_FBC_SHIFT) |
2072 (plane_wm << WM1_LP_SR_SHIFT) |
2073 cursor_wm);
2074
2075 /* WM2 */
2076 if (!ironlake_compute_srwm(dev, 2, enabled,
Ville Syrjäläb0aea5d2013-08-01 16:18:54 +03002077 dev_priv->wm.pri_latency[2] * 500,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002078 &sandybridge_display_srwm_info,
2079 &sandybridge_cursor_srwm_info,
2080 &fbc_wm, &plane_wm, &cursor_wm))
2081 return;
2082
2083 I915_WRITE(WM2_LP_ILK,
2084 WM2_LP_EN |
Ville Syrjäläb0aea5d2013-08-01 16:18:54 +03002085 (dev_priv->wm.pri_latency[2] << WM1_LP_LATENCY_SHIFT) |
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002086 (fbc_wm << WM1_LP_FBC_SHIFT) |
2087 (plane_wm << WM1_LP_SR_SHIFT) |
2088 cursor_wm);
2089
Chris Wilsonc43d0182012-12-11 12:01:42 +00002090 /* WM3, note we have to correct the cursor latency */
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002091 if (!ironlake_compute_srwm(dev, 3, enabled,
Ville Syrjäläb0aea5d2013-08-01 16:18:54 +03002092 dev_priv->wm.pri_latency[3] * 500,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002093 &sandybridge_display_srwm_info,
2094 &sandybridge_cursor_srwm_info,
Chris Wilsonc43d0182012-12-11 12:01:42 +00002095 &fbc_wm, &plane_wm, &ignore_cursor_wm) ||
2096 !ironlake_compute_srwm(dev, 3, enabled,
Ville Syrjäläb0aea5d2013-08-01 16:18:54 +03002097 dev_priv->wm.cur_latency[3] * 500,
Chris Wilsonc43d0182012-12-11 12:01:42 +00002098 &sandybridge_display_srwm_info,
2099 &sandybridge_cursor_srwm_info,
2100 &ignore_fbc_wm, &ignore_plane_wm, &cursor_wm))
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002101 return;
2102
2103 I915_WRITE(WM3_LP_ILK,
2104 WM3_LP_EN |
Ville Syrjäläb0aea5d2013-08-01 16:18:54 +03002105 (dev_priv->wm.pri_latency[3] << WM1_LP_LATENCY_SHIFT) |
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002106 (fbc_wm << WM1_LP_FBC_SHIFT) |
2107 (plane_wm << WM1_LP_SR_SHIFT) |
2108 cursor_wm);
2109}
2110
Ville Syrjälä36587292013-07-05 11:57:16 +03002111static uint32_t ilk_pipe_pixel_rate(struct drm_device *dev,
2112 struct drm_crtc *crtc)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002113{
2114 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2115 uint32_t pixel_rate, pfit_size;
2116
Daniel Vetterff9a6752013-06-01 17:16:21 +02002117 pixel_rate = intel_crtc->config.adjusted_mode.clock;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002118
2119 /* We only use IF-ID interlacing. If we ever use PF-ID we'll need to
2120 * adjust the pixel_rate here. */
2121
2122 pfit_size = intel_crtc->config.pch_pfit.size;
2123 if (pfit_size) {
2124 uint64_t pipe_w, pipe_h, pfit_w, pfit_h;
2125
2126 pipe_w = intel_crtc->config.requested_mode.hdisplay;
2127 pipe_h = intel_crtc->config.requested_mode.vdisplay;
2128 pfit_w = (pfit_size >> 16) & 0xFFFF;
2129 pfit_h = pfit_size & 0xFFFF;
2130 if (pipe_w < pfit_w)
2131 pipe_w = pfit_w;
2132 if (pipe_h < pfit_h)
2133 pipe_h = pfit_h;
2134
2135 pixel_rate = div_u64((uint64_t) pixel_rate * pipe_w * pipe_h,
2136 pfit_w * pfit_h);
2137 }
2138
2139 return pixel_rate;
2140}
2141
Ville Syrjälä37126462013-08-01 16:18:55 +03002142/* latency must be in 0.1us units. */
Ville Syrjälä23297042013-07-05 11:57:17 +03002143static uint32_t ilk_wm_method1(uint32_t pixel_rate, uint8_t bytes_per_pixel,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002144 uint32_t latency)
2145{
2146 uint64_t ret;
2147
Ville Syrjälä3312ba62013-08-01 16:18:53 +03002148 if (WARN(latency == 0, "Latency value missing\n"))
2149 return UINT_MAX;
2150
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002151 ret = (uint64_t) pixel_rate * bytes_per_pixel * latency;
2152 ret = DIV_ROUND_UP_ULL(ret, 64 * 10000) + 2;
2153
2154 return ret;
2155}
2156
Ville Syrjälä37126462013-08-01 16:18:55 +03002157/* latency must be in 0.1us units. */
Ville Syrjälä23297042013-07-05 11:57:17 +03002158static uint32_t ilk_wm_method2(uint32_t pixel_rate, uint32_t pipe_htotal,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002159 uint32_t horiz_pixels, uint8_t bytes_per_pixel,
2160 uint32_t latency)
2161{
2162 uint32_t ret;
2163
Ville Syrjälä3312ba62013-08-01 16:18:53 +03002164 if (WARN(latency == 0, "Latency value missing\n"))
2165 return UINT_MAX;
2166
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002167 ret = (latency * pixel_rate) / (pipe_htotal * 10000);
2168 ret = (ret + 1) * horiz_pixels * bytes_per_pixel;
2169 ret = DIV_ROUND_UP(ret, 64) + 2;
2170 return ret;
2171}
2172
Ville Syrjälä23297042013-07-05 11:57:17 +03002173static uint32_t ilk_wm_fbc(uint32_t pri_val, uint32_t horiz_pixels,
Paulo Zanonicca32e92013-05-31 11:45:06 -03002174 uint8_t bytes_per_pixel)
2175{
2176 return DIV_ROUND_UP(pri_val * 64, horiz_pixels * bytes_per_pixel) + 2;
2177}
2178
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002179struct hsw_pipe_wm_parameters {
2180 bool active;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002181 uint32_t pipe_htotal;
2182 uint32_t pixel_rate;
Ville Syrjäläc35426d2013-08-07 13:29:50 +03002183 struct intel_plane_wm_parameters pri;
2184 struct intel_plane_wm_parameters spr;
2185 struct intel_plane_wm_parameters cur;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002186};
2187
Paulo Zanonicca32e92013-05-31 11:45:06 -03002188struct hsw_wm_maximums {
2189 uint16_t pri;
2190 uint16_t spr;
2191 uint16_t cur;
2192 uint16_t fbc;
2193};
2194
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002195struct hsw_wm_values {
2196 uint32_t wm_pipe[3];
2197 uint32_t wm_lp[3];
2198 uint32_t wm_lp_spr[3];
2199 uint32_t wm_linetime[3];
Paulo Zanonicca32e92013-05-31 11:45:06 -03002200 bool enable_fbc_wm;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002201};
2202
Ville Syrjälä240264f2013-08-07 13:29:12 +03002203/* used in computing the new watermarks state */
2204struct intel_wm_config {
2205 unsigned int num_pipes_active;
2206 bool sprites_enabled;
2207 bool sprites_scaled;
2208 bool fbc_wm_enabled;
2209};
2210
Ville Syrjälä37126462013-08-01 16:18:55 +03002211/*
2212 * For both WM_PIPE and WM_LP.
2213 * mem_value must be in 0.1us units.
2214 */
Ville Syrjäläac830fe2013-08-30 14:30:23 +03002215static uint32_t ilk_compute_pri_wm(const struct hsw_pipe_wm_parameters *params,
Paulo Zanonicca32e92013-05-31 11:45:06 -03002216 uint32_t mem_value,
2217 bool is_lp)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002218{
Paulo Zanonicca32e92013-05-31 11:45:06 -03002219 uint32_t method1, method2;
2220
Ville Syrjäläc35426d2013-08-07 13:29:50 +03002221 if (!params->active || !params->pri.enabled)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002222 return 0;
2223
Ville Syrjälä23297042013-07-05 11:57:17 +03002224 method1 = ilk_wm_method1(params->pixel_rate,
Ville Syrjäläc35426d2013-08-07 13:29:50 +03002225 params->pri.bytes_per_pixel,
Paulo Zanonicca32e92013-05-31 11:45:06 -03002226 mem_value);
2227
2228 if (!is_lp)
2229 return method1;
2230
Ville Syrjälä23297042013-07-05 11:57:17 +03002231 method2 = ilk_wm_method2(params->pixel_rate,
Paulo Zanonicca32e92013-05-31 11:45:06 -03002232 params->pipe_htotal,
Ville Syrjäläc35426d2013-08-07 13:29:50 +03002233 params->pri.horiz_pixels,
2234 params->pri.bytes_per_pixel,
Paulo Zanonicca32e92013-05-31 11:45:06 -03002235 mem_value);
2236
2237 return min(method1, method2);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002238}
2239
Ville Syrjälä37126462013-08-01 16:18:55 +03002240/*
2241 * For both WM_PIPE and WM_LP.
2242 * mem_value must be in 0.1us units.
2243 */
Ville Syrjäläac830fe2013-08-30 14:30:23 +03002244static uint32_t ilk_compute_spr_wm(const struct hsw_pipe_wm_parameters *params,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002245 uint32_t mem_value)
2246{
2247 uint32_t method1, method2;
2248
Ville Syrjäläc35426d2013-08-07 13:29:50 +03002249 if (!params->active || !params->spr.enabled)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002250 return 0;
2251
Ville Syrjälä23297042013-07-05 11:57:17 +03002252 method1 = ilk_wm_method1(params->pixel_rate,
Ville Syrjäläc35426d2013-08-07 13:29:50 +03002253 params->spr.bytes_per_pixel,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002254 mem_value);
Ville Syrjälä23297042013-07-05 11:57:17 +03002255 method2 = ilk_wm_method2(params->pixel_rate,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002256 params->pipe_htotal,
Ville Syrjäläc35426d2013-08-07 13:29:50 +03002257 params->spr.horiz_pixels,
2258 params->spr.bytes_per_pixel,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002259 mem_value);
2260 return min(method1, method2);
2261}
2262
Ville Syrjälä37126462013-08-01 16:18:55 +03002263/*
2264 * For both WM_PIPE and WM_LP.
2265 * mem_value must be in 0.1us units.
2266 */
Ville Syrjäläac830fe2013-08-30 14:30:23 +03002267static uint32_t ilk_compute_cur_wm(const struct hsw_pipe_wm_parameters *params,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002268 uint32_t mem_value)
2269{
Ville Syrjäläc35426d2013-08-07 13:29:50 +03002270 if (!params->active || !params->cur.enabled)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002271 return 0;
2272
Ville Syrjälä23297042013-07-05 11:57:17 +03002273 return ilk_wm_method2(params->pixel_rate,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002274 params->pipe_htotal,
Ville Syrjäläc35426d2013-08-07 13:29:50 +03002275 params->cur.horiz_pixels,
2276 params->cur.bytes_per_pixel,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002277 mem_value);
2278}
2279
Paulo Zanonicca32e92013-05-31 11:45:06 -03002280/* Only for WM_LP. */
Ville Syrjäläac830fe2013-08-30 14:30:23 +03002281static uint32_t ilk_compute_fbc_wm(const struct hsw_pipe_wm_parameters *params,
Ville Syrjälä1fda9882013-07-05 11:57:19 +03002282 uint32_t pri_val)
Paulo Zanonicca32e92013-05-31 11:45:06 -03002283{
Ville Syrjäläc35426d2013-08-07 13:29:50 +03002284 if (!params->active || !params->pri.enabled)
Paulo Zanonicca32e92013-05-31 11:45:06 -03002285 return 0;
2286
Ville Syrjälä23297042013-07-05 11:57:17 +03002287 return ilk_wm_fbc(pri_val,
Ville Syrjäläc35426d2013-08-07 13:29:50 +03002288 params->pri.horiz_pixels,
2289 params->pri.bytes_per_pixel);
Paulo Zanonicca32e92013-05-31 11:45:06 -03002290}
2291
Ville Syrjälä158ae642013-08-07 13:28:19 +03002292static unsigned int ilk_display_fifo_size(const struct drm_device *dev)
2293{
2294 if (INTEL_INFO(dev)->gen >= 7)
2295 return 768;
2296 else
2297 return 512;
2298}
2299
2300/* Calculate the maximum primary/sprite plane watermark */
2301static unsigned int ilk_plane_wm_max(const struct drm_device *dev,
2302 int level,
Ville Syrjälä240264f2013-08-07 13:29:12 +03002303 const struct intel_wm_config *config,
Ville Syrjälä158ae642013-08-07 13:28:19 +03002304 enum intel_ddb_partitioning ddb_partitioning,
2305 bool is_sprite)
2306{
2307 unsigned int fifo_size = ilk_display_fifo_size(dev);
2308 unsigned int max;
2309
2310 /* if sprites aren't enabled, sprites get nothing */
Ville Syrjälä240264f2013-08-07 13:29:12 +03002311 if (is_sprite && !config->sprites_enabled)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002312 return 0;
2313
2314 /* HSW allows LP1+ watermarks even with multiple pipes */
Ville Syrjälä240264f2013-08-07 13:29:12 +03002315 if (level == 0 || config->num_pipes_active > 1) {
Ville Syrjälä158ae642013-08-07 13:28:19 +03002316 fifo_size /= INTEL_INFO(dev)->num_pipes;
2317
2318 /*
2319 * For some reason the non self refresh
2320 * FIFO size is only half of the self
2321 * refresh FIFO size on ILK/SNB.
2322 */
2323 if (INTEL_INFO(dev)->gen <= 6)
2324 fifo_size /= 2;
2325 }
2326
Ville Syrjälä240264f2013-08-07 13:29:12 +03002327 if (config->sprites_enabled) {
Ville Syrjälä158ae642013-08-07 13:28:19 +03002328 /* level 0 is always calculated with 1:1 split */
2329 if (level > 0 && ddb_partitioning == INTEL_DDB_PART_5_6) {
2330 if (is_sprite)
2331 fifo_size *= 5;
2332 fifo_size /= 6;
2333 } else {
2334 fifo_size /= 2;
2335 }
2336 }
2337
2338 /* clamp to max that the registers can hold */
2339 if (INTEL_INFO(dev)->gen >= 7)
2340 /* IVB/HSW primary/sprite plane watermarks */
2341 max = level == 0 ? 127 : 1023;
2342 else if (!is_sprite)
2343 /* ILK/SNB primary plane watermarks */
2344 max = level == 0 ? 127 : 511;
2345 else
2346 /* ILK/SNB sprite plane watermarks */
2347 max = level == 0 ? 63 : 255;
2348
2349 return min(fifo_size, max);
2350}
2351
2352/* Calculate the maximum cursor plane watermark */
2353static unsigned int ilk_cursor_wm_max(const struct drm_device *dev,
Ville Syrjälä240264f2013-08-07 13:29:12 +03002354 int level,
2355 const struct intel_wm_config *config)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002356{
2357 /* HSW LP1+ watermarks w/ multiple pipes */
Ville Syrjälä240264f2013-08-07 13:29:12 +03002358 if (level > 0 && config->num_pipes_active > 1)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002359 return 64;
2360
2361 /* otherwise just report max that registers can hold */
2362 if (INTEL_INFO(dev)->gen >= 7)
2363 return level == 0 ? 63 : 255;
2364 else
2365 return level == 0 ? 31 : 63;
2366}
2367
2368/* Calculate the maximum FBC watermark */
2369static unsigned int ilk_fbc_wm_max(void)
2370{
2371 /* max that registers can hold */
2372 return 15;
2373}
2374
2375static void ilk_wm_max(struct drm_device *dev,
2376 int level,
Ville Syrjälä240264f2013-08-07 13:29:12 +03002377 const struct intel_wm_config *config,
Ville Syrjälä158ae642013-08-07 13:28:19 +03002378 enum intel_ddb_partitioning ddb_partitioning,
2379 struct hsw_wm_maximums *max)
2380{
Ville Syrjälä240264f2013-08-07 13:29:12 +03002381 max->pri = ilk_plane_wm_max(dev, level, config, ddb_partitioning, false);
2382 max->spr = ilk_plane_wm_max(dev, level, config, ddb_partitioning, true);
2383 max->cur = ilk_cursor_wm_max(dev, level, config);
Ville Syrjälä158ae642013-08-07 13:28:19 +03002384 max->fbc = ilk_fbc_wm_max();
2385}
2386
Ville Syrjäläa9786a12013-08-07 13:24:47 +03002387static bool ilk_check_wm(int level,
2388 const struct hsw_wm_maximums *max,
Ville Syrjälä1fd527c2013-08-06 22:24:05 +03002389 struct intel_wm_level *result)
Ville Syrjäläa9786a12013-08-07 13:24:47 +03002390{
2391 bool ret;
2392
2393 /* already determined to be invalid? */
2394 if (!result->enable)
2395 return false;
2396
2397 result->enable = result->pri_val <= max->pri &&
2398 result->spr_val <= max->spr &&
2399 result->cur_val <= max->cur;
2400
2401 ret = result->enable;
2402
2403 /*
2404 * HACK until we can pre-compute everything,
2405 * and thus fail gracefully if LP0 watermarks
2406 * are exceeded...
2407 */
2408 if (level == 0 && !result->enable) {
2409 if (result->pri_val > max->pri)
2410 DRM_DEBUG_KMS("Primary WM%d too large %u (max %u)\n",
2411 level, result->pri_val, max->pri);
2412 if (result->spr_val > max->spr)
2413 DRM_DEBUG_KMS("Sprite WM%d too large %u (max %u)\n",
2414 level, result->spr_val, max->spr);
2415 if (result->cur_val > max->cur)
2416 DRM_DEBUG_KMS("Cursor WM%d too large %u (max %u)\n",
2417 level, result->cur_val, max->cur);
2418
2419 result->pri_val = min_t(uint32_t, result->pri_val, max->pri);
2420 result->spr_val = min_t(uint32_t, result->spr_val, max->spr);
2421 result->cur_val = min_t(uint32_t, result->cur_val, max->cur);
2422 result->enable = true;
2423 }
2424
2425 DRM_DEBUG_KMS("WM%d: %sabled\n", level, result->enable ? "en" : "dis");
2426
2427 return ret;
2428}
2429
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03002430static void ilk_compute_wm_level(struct drm_i915_private *dev_priv,
2431 int level,
Ville Syrjäläac830fe2013-08-30 14:30:23 +03002432 const struct hsw_pipe_wm_parameters *p,
Ville Syrjälä1fd527c2013-08-06 22:24:05 +03002433 struct intel_wm_level *result)
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03002434{
2435 uint16_t pri_latency = dev_priv->wm.pri_latency[level];
2436 uint16_t spr_latency = dev_priv->wm.spr_latency[level];
2437 uint16_t cur_latency = dev_priv->wm.cur_latency[level];
2438
2439 /* WM1+ latency values stored in 0.5us units */
2440 if (level > 0) {
2441 pri_latency *= 5;
2442 spr_latency *= 5;
2443 cur_latency *= 5;
2444 }
2445
2446 result->pri_val = ilk_compute_pri_wm(p, pri_latency, level);
2447 result->spr_val = ilk_compute_spr_wm(p, spr_latency);
2448 result->cur_val = ilk_compute_cur_wm(p, cur_latency);
2449 result->fbc_val = ilk_compute_fbc_wm(p, result->pri_val);
2450 result->enable = true;
2451}
2452
Ville Syrjälä5b77da32013-08-01 16:18:51 +03002453static bool hsw_compute_lp_wm(struct drm_i915_private *dev_priv,
Ville Syrjäläac830fe2013-08-30 14:30:23 +03002454 int level, const struct hsw_wm_maximums *max,
2455 const struct hsw_pipe_wm_parameters *params,
Ville Syrjälä1fd527c2013-08-06 22:24:05 +03002456 struct intel_wm_level *result)
Paulo Zanonicca32e92013-05-31 11:45:06 -03002457{
2458 enum pipe pipe;
Ville Syrjälä1fd527c2013-08-06 22:24:05 +03002459 struct intel_wm_level res[3];
Paulo Zanonicca32e92013-05-31 11:45:06 -03002460
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03002461 for (pipe = PIPE_A; pipe <= PIPE_C; pipe++)
2462 ilk_compute_wm_level(dev_priv, level, &params[pipe], &res[pipe]);
Paulo Zanonicca32e92013-05-31 11:45:06 -03002463
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03002464 result->pri_val = max3(res[0].pri_val, res[1].pri_val, res[2].pri_val);
2465 result->spr_val = max3(res[0].spr_val, res[1].spr_val, res[2].spr_val);
2466 result->cur_val = max3(res[0].cur_val, res[1].cur_val, res[2].cur_val);
2467 result->fbc_val = max3(res[0].fbc_val, res[1].fbc_val, res[2].fbc_val);
2468 result->enable = true;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002469
Ville Syrjäläa9786a12013-08-07 13:24:47 +03002470 return ilk_check_wm(level, max, result);
Paulo Zanonicca32e92013-05-31 11:45:06 -03002471}
2472
Ville Syrjälä8de123a2013-08-30 14:30:24 +03002473
2474static uint32_t hsw_compute_wm_pipe(struct drm_device *dev,
Ville Syrjäläac830fe2013-08-30 14:30:23 +03002475 const struct hsw_pipe_wm_parameters *params)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002476{
Ville Syrjälä8de123a2013-08-30 14:30:24 +03002477 struct drm_i915_private *dev_priv = dev->dev_private;
2478 struct intel_wm_config config = {
2479 .num_pipes_active = 1,
2480 .sprites_enabled = params->spr.enabled,
2481 .sprites_scaled = params->spr.scaled,
2482 };
2483 struct hsw_wm_maximums max;
2484 struct intel_wm_level res;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002485
Ville Syrjälä8de123a2013-08-30 14:30:24 +03002486 if (!params->active)
2487 return 0;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002488
Ville Syrjälä8de123a2013-08-30 14:30:24 +03002489 ilk_wm_max(dev, 0, &config, INTEL_DDB_PART_1_2, &max);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002490
Ville Syrjälä8de123a2013-08-30 14:30:24 +03002491 ilk_compute_wm_level(dev_priv, 0, params, &res);
2492
2493 ilk_check_wm(0, &max, &res);
2494
2495 return (res.pri_val << WM0_PIPE_PLANE_SHIFT) |
2496 (res.spr_val << WM0_PIPE_SPRITE_SHIFT) |
2497 res.cur_val;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002498}
2499
2500static uint32_t
2501hsw_compute_linetime_wm(struct drm_device *dev, struct drm_crtc *crtc)
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002502{
2503 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002504 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002505 struct drm_display_mode *mode = &intel_crtc->config.adjusted_mode;
Paulo Zanoni85a02de2013-05-03 17:23:43 -03002506 u32 linetime, ips_linetime;
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002507
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002508 if (!intel_crtc_active(crtc))
2509 return 0;
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002510
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002511 /* The WM are computed with base on how long it takes to fill a single
2512 * row at the given clock rate, multiplied by 8.
2513 * */
Paulo Zanoni85a02de2013-05-03 17:23:43 -03002514 linetime = DIV_ROUND_CLOSEST(mode->htotal * 1000 * 8, mode->clock);
2515 ips_linetime = DIV_ROUND_CLOSEST(mode->htotal * 1000 * 8,
2516 intel_ddi_get_cdclk_freq(dev_priv));
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002517
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002518 return PIPE_WM_LINETIME_IPS_LINETIME(ips_linetime) |
2519 PIPE_WM_LINETIME_TIME(linetime);
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002520}
2521
Ville Syrjälä12b134d2013-07-05 11:57:21 +03002522static void intel_read_wm_latency(struct drm_device *dev, uint16_t wm[5])
2523{
2524 struct drm_i915_private *dev_priv = dev->dev_private;
2525
2526 if (IS_HASWELL(dev)) {
2527 uint64_t sskpd = I915_READ64(MCH_SSKPD);
2528
2529 wm[0] = (sskpd >> 56) & 0xFF;
2530 if (wm[0] == 0)
2531 wm[0] = sskpd & 0xF;
Ville Syrjäläe5d50192013-07-05 11:57:22 +03002532 wm[1] = (sskpd >> 4) & 0xFF;
2533 wm[2] = (sskpd >> 12) & 0xFF;
2534 wm[3] = (sskpd >> 20) & 0x1FF;
2535 wm[4] = (sskpd >> 32) & 0x1FF;
Ville Syrjälä63cf9a12013-07-05 11:57:23 +03002536 } else if (INTEL_INFO(dev)->gen >= 6) {
2537 uint32_t sskpd = I915_READ(MCH_SSKPD);
2538
2539 wm[0] = (sskpd >> SSKPD_WM0_SHIFT) & SSKPD_WM_MASK;
2540 wm[1] = (sskpd >> SSKPD_WM1_SHIFT) & SSKPD_WM_MASK;
2541 wm[2] = (sskpd >> SSKPD_WM2_SHIFT) & SSKPD_WM_MASK;
2542 wm[3] = (sskpd >> SSKPD_WM3_SHIFT) & SSKPD_WM_MASK;
Ville Syrjälä3a88d0a2013-08-01 16:18:49 +03002543 } else if (INTEL_INFO(dev)->gen >= 5) {
2544 uint32_t mltr = I915_READ(MLTR_ILK);
2545
2546 /* ILK primary LP0 latency is 700 ns */
2547 wm[0] = 7;
2548 wm[1] = (mltr >> MLTR_WM1_SHIFT) & ILK_SRLT_MASK;
2549 wm[2] = (mltr >> MLTR_WM2_SHIFT) & ILK_SRLT_MASK;
Ville Syrjälä12b134d2013-07-05 11:57:21 +03002550 }
2551}
2552
Ville Syrjälä53615a52013-08-01 16:18:50 +03002553static void intel_fixup_spr_wm_latency(struct drm_device *dev, uint16_t wm[5])
2554{
2555 /* ILK sprite LP0 latency is 1300 ns */
2556 if (INTEL_INFO(dev)->gen == 5)
2557 wm[0] = 13;
2558}
2559
2560static void intel_fixup_cur_wm_latency(struct drm_device *dev, uint16_t wm[5])
2561{
2562 /* ILK cursor LP0 latency is 1300 ns */
2563 if (INTEL_INFO(dev)->gen == 5)
2564 wm[0] = 13;
2565
2566 /* WaDoubleCursorLP3Latency:ivb */
2567 if (IS_IVYBRIDGE(dev))
2568 wm[3] *= 2;
2569}
2570
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03002571static int ilk_wm_max_level(const struct drm_device *dev)
2572{
2573 /* how many WM levels are we expecting */
2574 if (IS_HASWELL(dev))
2575 return 4;
2576 else if (INTEL_INFO(dev)->gen >= 6)
2577 return 3;
2578 else
2579 return 2;
2580}
2581
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002582static void intel_print_wm_latency(struct drm_device *dev,
2583 const char *name,
2584 const uint16_t wm[5])
2585{
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03002586 int level, max_level = ilk_wm_max_level(dev);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002587
2588 for (level = 0; level <= max_level; level++) {
2589 unsigned int latency = wm[level];
2590
2591 if (latency == 0) {
2592 DRM_ERROR("%s WM%d latency not provided\n",
2593 name, level);
2594 continue;
2595 }
2596
2597 /* WM1+ latency values in 0.5us units */
2598 if (level > 0)
2599 latency *= 5;
2600
2601 DRM_DEBUG_KMS("%s WM%d latency %u (%u.%u usec)\n",
2602 name, level, wm[level],
2603 latency / 10, latency % 10);
2604 }
2605}
2606
Ville Syrjälä53615a52013-08-01 16:18:50 +03002607static void intel_setup_wm_latency(struct drm_device *dev)
2608{
2609 struct drm_i915_private *dev_priv = dev->dev_private;
2610
2611 intel_read_wm_latency(dev, dev_priv->wm.pri_latency);
2612
2613 memcpy(dev_priv->wm.spr_latency, dev_priv->wm.pri_latency,
2614 sizeof(dev_priv->wm.pri_latency));
2615 memcpy(dev_priv->wm.cur_latency, dev_priv->wm.pri_latency,
2616 sizeof(dev_priv->wm.pri_latency));
2617
2618 intel_fixup_spr_wm_latency(dev, dev_priv->wm.spr_latency);
2619 intel_fixup_cur_wm_latency(dev, dev_priv->wm.cur_latency);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002620
2621 intel_print_wm_latency(dev, "Primary", dev_priv->wm.pri_latency);
2622 intel_print_wm_latency(dev, "Sprite", dev_priv->wm.spr_latency);
2623 intel_print_wm_latency(dev, "Cursor", dev_priv->wm.cur_latency);
Ville Syrjälä53615a52013-08-01 16:18:50 +03002624}
2625
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002626static void hsw_compute_wm_parameters(struct drm_device *dev,
2627 struct hsw_pipe_wm_parameters *params,
Paulo Zanoni861f3382013-05-31 10:19:21 -03002628 struct hsw_wm_maximums *lp_max_1_2,
2629 struct hsw_wm_maximums *lp_max_5_6)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002630{
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002631 struct drm_crtc *crtc;
2632 struct drm_plane *plane;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002633 enum pipe pipe;
Ville Syrjälä240264f2013-08-07 13:29:12 +03002634 struct intel_wm_config config = {};
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002635
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002636 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2637 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2638 struct hsw_pipe_wm_parameters *p;
2639
2640 pipe = intel_crtc->pipe;
2641 p = &params[pipe];
2642
2643 p->active = intel_crtc_active(crtc);
2644 if (!p->active)
2645 continue;
2646
Ville Syrjälä240264f2013-08-07 13:29:12 +03002647 config.num_pipes_active++;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002648
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002649 p->pipe_htotal = intel_crtc->config.adjusted_mode.htotal;
Ville Syrjälä36587292013-07-05 11:57:16 +03002650 p->pixel_rate = ilk_pipe_pixel_rate(dev, crtc);
Ville Syrjäläc35426d2013-08-07 13:29:50 +03002651 p->pri.bytes_per_pixel = crtc->fb->bits_per_pixel / 8;
2652 p->cur.bytes_per_pixel = 4;
2653 p->pri.horiz_pixels =
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002654 intel_crtc->config.requested_mode.hdisplay;
Ville Syrjäläc35426d2013-08-07 13:29:50 +03002655 p->cur.horiz_pixels = 64;
2656 /* TODO: for now, assume primary and cursor planes are always enabled. */
2657 p->pri.enabled = true;
2658 p->cur.enabled = true;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002659 }
2660
2661 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
2662 struct intel_plane *intel_plane = to_intel_plane(plane);
2663 struct hsw_pipe_wm_parameters *p;
2664
2665 pipe = intel_plane->pipe;
2666 p = &params[pipe];
2667
Ville Syrjäläc35426d2013-08-07 13:29:50 +03002668 p->spr = intel_plane->wm;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002669
Ville Syrjäläc35426d2013-08-07 13:29:50 +03002670 config.sprites_enabled |= p->spr.enabled;
2671 config.sprites_scaled |= p->spr.scaled;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002672 }
Paulo Zanonicca32e92013-05-31 11:45:06 -03002673
Ville Syrjälä240264f2013-08-07 13:29:12 +03002674 ilk_wm_max(dev, 1, &config, INTEL_DDB_PART_1_2, lp_max_1_2);
Ville Syrjälä158ae642013-08-07 13:28:19 +03002675
2676 /* 5/6 split only in single pipe config on IVB+ */
Ville Syrjälä240264f2013-08-07 13:29:12 +03002677 if (INTEL_INFO(dev)->gen >= 7 && config.num_pipes_active <= 1)
2678 ilk_wm_max(dev, 1, &config, INTEL_DDB_PART_5_6, lp_max_5_6);
Ville Syrjälä158ae642013-08-07 13:28:19 +03002679 else
2680 *lp_max_5_6 = *lp_max_1_2;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002681}
2682
2683static void hsw_compute_wm_results(struct drm_device *dev,
Ville Syrjäläac830fe2013-08-30 14:30:23 +03002684 const struct hsw_pipe_wm_parameters *params,
2685 const struct hsw_wm_maximums *lp_maximums,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002686 struct hsw_wm_values *results)
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002687{
2688 struct drm_i915_private *dev_priv = dev->dev_private;
2689 struct drm_crtc *crtc;
Ville Syrjälä1fd527c2013-08-06 22:24:05 +03002690 struct intel_wm_level lp_results[4] = {};
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002691 enum pipe pipe;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002692 int level, max_level, wm_lp;
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002693
Paulo Zanonicca32e92013-05-31 11:45:06 -03002694 for (level = 1; level <= 4; level++)
Ville Syrjälä5b77da32013-08-01 16:18:51 +03002695 if (!hsw_compute_lp_wm(dev_priv, level,
2696 lp_maximums, params,
Paulo Zanonicca32e92013-05-31 11:45:06 -03002697 &lp_results[level - 1]))
2698 break;
2699 max_level = level - 1;
2700
Ville Syrjälä5c536612013-08-09 18:02:09 +03002701 memset(results, 0, sizeof(*results));
2702
Paulo Zanonicca32e92013-05-31 11:45:06 -03002703 /* The spec says it is preferred to disable FBC WMs instead of disabling
2704 * a WM level. */
2705 results->enable_fbc_wm = true;
2706 for (level = 1; level <= max_level; level++) {
Dan Carpenter16e54062013-08-09 13:07:31 +03002707 if (lp_results[level - 1].fbc_val > lp_maximums->fbc) {
Paulo Zanonicca32e92013-05-31 11:45:06 -03002708 results->enable_fbc_wm = false;
Ville Syrjälä71fff202013-08-06 22:24:03 +03002709 lp_results[level - 1].fbc_val = 0;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002710 }
2711 }
2712
Paulo Zanonicca32e92013-05-31 11:45:06 -03002713 for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
Ville Syrjälä1fd527c2013-08-06 22:24:05 +03002714 const struct intel_wm_level *r;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002715
2716 level = (max_level == 4 && wm_lp > 1) ? wm_lp + 1 : wm_lp;
2717 if (level > max_level)
2718 break;
2719
2720 r = &lp_results[level - 1];
2721 results->wm_lp[wm_lp - 1] = HSW_WM_LP_VAL(level * 2,
2722 r->fbc_val,
2723 r->pri_val,
2724 r->cur_val);
2725 results->wm_lp_spr[wm_lp - 1] = r->spr_val;
2726 }
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002727
2728 for_each_pipe(pipe)
Ville Syrjälä8de123a2013-08-30 14:30:24 +03002729 results->wm_pipe[pipe] = hsw_compute_wm_pipe(dev,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002730 &params[pipe]);
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002731
2732 for_each_pipe(pipe) {
2733 crtc = dev_priv->pipe_to_crtc_mapping[pipe];
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002734 results->wm_linetime[pipe] = hsw_compute_linetime_wm(dev, crtc);
2735 }
2736}
2737
Paulo Zanoni861f3382013-05-31 10:19:21 -03002738/* Find the result with the highest level enabled. Check for enable_fbc_wm in
2739 * case both are at the same level. Prefer r1 in case they're the same. */
Damien Lespiauf4db9322013-06-24 22:59:50 +01002740static struct hsw_wm_values *hsw_find_best_result(struct hsw_wm_values *r1,
2741 struct hsw_wm_values *r2)
Paulo Zanoni861f3382013-05-31 10:19:21 -03002742{
2743 int i, val_r1 = 0, val_r2 = 0;
2744
2745 for (i = 0; i < 3; i++) {
2746 if (r1->wm_lp[i] & WM3_LP_EN)
2747 val_r1 = r1->wm_lp[i] & WM1_LP_LATENCY_MASK;
2748 if (r2->wm_lp[i] & WM3_LP_EN)
2749 val_r2 = r2->wm_lp[i] & WM1_LP_LATENCY_MASK;
2750 }
2751
2752 if (val_r1 == val_r2) {
2753 if (r2->enable_fbc_wm && !r1->enable_fbc_wm)
2754 return r2;
2755 else
2756 return r1;
2757 } else if (val_r1 > val_r2) {
2758 return r1;
2759 } else {
2760 return r2;
2761 }
2762}
2763
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002764/*
2765 * The spec says we shouldn't write when we don't need, because every write
2766 * causes WMs to be re-evaluated, expending some power.
2767 */
2768static void hsw_write_wm_values(struct drm_i915_private *dev_priv,
2769 struct hsw_wm_values *results,
Ville Syrjälä77c122b2013-08-06 22:24:04 +03002770 enum intel_ddb_partitioning partitioning)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002771{
2772 struct hsw_wm_values previous;
2773 uint32_t val;
Ville Syrjälä77c122b2013-08-06 22:24:04 +03002774 enum intel_ddb_partitioning prev_partitioning;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002775 bool prev_enable_fbc_wm;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002776
2777 previous.wm_pipe[0] = I915_READ(WM0_PIPEA_ILK);
2778 previous.wm_pipe[1] = I915_READ(WM0_PIPEB_ILK);
2779 previous.wm_pipe[2] = I915_READ(WM0_PIPEC_IVB);
2780 previous.wm_lp[0] = I915_READ(WM1_LP_ILK);
2781 previous.wm_lp[1] = I915_READ(WM2_LP_ILK);
2782 previous.wm_lp[2] = I915_READ(WM3_LP_ILK);
2783 previous.wm_lp_spr[0] = I915_READ(WM1S_LP_ILK);
2784 previous.wm_lp_spr[1] = I915_READ(WM2S_LP_IVB);
2785 previous.wm_lp_spr[2] = I915_READ(WM3S_LP_IVB);
2786 previous.wm_linetime[0] = I915_READ(PIPE_WM_LINETIME(PIPE_A));
2787 previous.wm_linetime[1] = I915_READ(PIPE_WM_LINETIME(PIPE_B));
2788 previous.wm_linetime[2] = I915_READ(PIPE_WM_LINETIME(PIPE_C));
2789
2790 prev_partitioning = (I915_READ(WM_MISC) & WM_MISC_DATA_PARTITION_5_6) ?
Ville Syrjälä77c122b2013-08-06 22:24:04 +03002791 INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002792
Paulo Zanonicca32e92013-05-31 11:45:06 -03002793 prev_enable_fbc_wm = !(I915_READ(DISP_ARB_CTL) & DISP_FBC_WM_DIS);
2794
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002795 if (memcmp(results->wm_pipe, previous.wm_pipe,
2796 sizeof(results->wm_pipe)) == 0 &&
2797 memcmp(results->wm_lp, previous.wm_lp,
2798 sizeof(results->wm_lp)) == 0 &&
2799 memcmp(results->wm_lp_spr, previous.wm_lp_spr,
2800 sizeof(results->wm_lp_spr)) == 0 &&
2801 memcmp(results->wm_linetime, previous.wm_linetime,
2802 sizeof(results->wm_linetime)) == 0 &&
Paulo Zanonicca32e92013-05-31 11:45:06 -03002803 partitioning == prev_partitioning &&
2804 results->enable_fbc_wm == prev_enable_fbc_wm)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002805 return;
2806
2807 if (previous.wm_lp[2] != 0)
2808 I915_WRITE(WM3_LP_ILK, 0);
2809 if (previous.wm_lp[1] != 0)
2810 I915_WRITE(WM2_LP_ILK, 0);
2811 if (previous.wm_lp[0] != 0)
2812 I915_WRITE(WM1_LP_ILK, 0);
2813
2814 if (previous.wm_pipe[0] != results->wm_pipe[0])
2815 I915_WRITE(WM0_PIPEA_ILK, results->wm_pipe[0]);
2816 if (previous.wm_pipe[1] != results->wm_pipe[1])
2817 I915_WRITE(WM0_PIPEB_ILK, results->wm_pipe[1]);
2818 if (previous.wm_pipe[2] != results->wm_pipe[2])
2819 I915_WRITE(WM0_PIPEC_IVB, results->wm_pipe[2]);
2820
2821 if (previous.wm_linetime[0] != results->wm_linetime[0])
2822 I915_WRITE(PIPE_WM_LINETIME(PIPE_A), results->wm_linetime[0]);
2823 if (previous.wm_linetime[1] != results->wm_linetime[1])
2824 I915_WRITE(PIPE_WM_LINETIME(PIPE_B), results->wm_linetime[1]);
2825 if (previous.wm_linetime[2] != results->wm_linetime[2])
2826 I915_WRITE(PIPE_WM_LINETIME(PIPE_C), results->wm_linetime[2]);
2827
2828 if (prev_partitioning != partitioning) {
2829 val = I915_READ(WM_MISC);
Ville Syrjälä77c122b2013-08-06 22:24:04 +03002830 if (partitioning == INTEL_DDB_PART_1_2)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002831 val &= ~WM_MISC_DATA_PARTITION_5_6;
2832 else
2833 val |= WM_MISC_DATA_PARTITION_5_6;
2834 I915_WRITE(WM_MISC, val);
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002835 }
2836
Paulo Zanonicca32e92013-05-31 11:45:06 -03002837 if (prev_enable_fbc_wm != results->enable_fbc_wm) {
2838 val = I915_READ(DISP_ARB_CTL);
2839 if (results->enable_fbc_wm)
2840 val &= ~DISP_FBC_WM_DIS;
2841 else
2842 val |= DISP_FBC_WM_DIS;
2843 I915_WRITE(DISP_ARB_CTL, val);
2844 }
2845
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002846 if (previous.wm_lp_spr[0] != results->wm_lp_spr[0])
2847 I915_WRITE(WM1S_LP_ILK, results->wm_lp_spr[0]);
2848 if (previous.wm_lp_spr[1] != results->wm_lp_spr[1])
2849 I915_WRITE(WM2S_LP_IVB, results->wm_lp_spr[1]);
2850 if (previous.wm_lp_spr[2] != results->wm_lp_spr[2])
2851 I915_WRITE(WM3S_LP_IVB, results->wm_lp_spr[2]);
2852
2853 if (results->wm_lp[0] != 0)
2854 I915_WRITE(WM1_LP_ILK, results->wm_lp[0]);
2855 if (results->wm_lp[1] != 0)
2856 I915_WRITE(WM2_LP_ILK, results->wm_lp[1]);
2857 if (results->wm_lp[2] != 0)
2858 I915_WRITE(WM3_LP_ILK, results->wm_lp[2]);
2859}
2860
Ville Syrjälä46ba6142013-09-10 11:40:40 +03002861static void haswell_update_wm(struct drm_crtc *crtc)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002862{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03002863 struct drm_device *dev = crtc->dev;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002864 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni861f3382013-05-31 10:19:21 -03002865 struct hsw_wm_maximums lp_max_1_2, lp_max_5_6;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002866 struct hsw_pipe_wm_parameters params[3];
Paulo Zanoni861f3382013-05-31 10:19:21 -03002867 struct hsw_wm_values results_1_2, results_5_6, *best_results;
Ville Syrjälä77c122b2013-08-06 22:24:04 +03002868 enum intel_ddb_partitioning partitioning;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002869
Ville Syrjälä12b134d2013-07-05 11:57:21 +03002870 hsw_compute_wm_parameters(dev, params, &lp_max_1_2, &lp_max_5_6);
Paulo Zanoni861f3382013-05-31 10:19:21 -03002871
Ville Syrjälä53615a52013-08-01 16:18:50 +03002872 hsw_compute_wm_results(dev, params,
Ville Syrjälä53615a52013-08-01 16:18:50 +03002873 &lp_max_1_2, &results_1_2);
Paulo Zanoni861f3382013-05-31 10:19:21 -03002874 if (lp_max_1_2.pri != lp_max_5_6.pri) {
Ville Syrjälä53615a52013-08-01 16:18:50 +03002875 hsw_compute_wm_results(dev, params,
Ville Syrjälä53615a52013-08-01 16:18:50 +03002876 &lp_max_5_6, &results_5_6);
Paulo Zanoni861f3382013-05-31 10:19:21 -03002877 best_results = hsw_find_best_result(&results_1_2, &results_5_6);
2878 } else {
2879 best_results = &results_1_2;
2880 }
2881
2882 partitioning = (best_results == &results_1_2) ?
Ville Syrjälä77c122b2013-08-06 22:24:04 +03002883 INTEL_DDB_PART_1_2 : INTEL_DDB_PART_5_6;
Paulo Zanoni861f3382013-05-31 10:19:21 -03002884
2885 hsw_write_wm_values(dev_priv, best_results, partitioning);
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002886}
2887
Ville Syrjäläadf3d352013-08-06 22:24:11 +03002888static void haswell_update_sprite_wm(struct drm_plane *plane,
2889 struct drm_crtc *crtc,
Paulo Zanoni526682e2013-05-24 11:59:18 -03002890 uint32_t sprite_width, int pixel_size,
Ville Syrjäläbdd57d02013-07-05 11:57:13 +03002891 bool enabled, bool scaled)
Paulo Zanoni526682e2013-05-24 11:59:18 -03002892{
Ville Syrjäläadf3d352013-08-06 22:24:11 +03002893 struct intel_plane *intel_plane = to_intel_plane(plane);
Paulo Zanoni526682e2013-05-24 11:59:18 -03002894
Ville Syrjäläadf3d352013-08-06 22:24:11 +03002895 intel_plane->wm.enabled = enabled;
2896 intel_plane->wm.scaled = scaled;
2897 intel_plane->wm.horiz_pixels = sprite_width;
2898 intel_plane->wm.bytes_per_pixel = pixel_size;
Paulo Zanoni526682e2013-05-24 11:59:18 -03002899
Ville Syrjälä46ba6142013-09-10 11:40:40 +03002900 haswell_update_wm(crtc);
Paulo Zanoni526682e2013-05-24 11:59:18 -03002901}
2902
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002903static bool
2904sandybridge_compute_sprite_wm(struct drm_device *dev, int plane,
2905 uint32_t sprite_width, int pixel_size,
2906 const struct intel_watermark_params *display,
2907 int display_latency_ns, int *sprite_wm)
2908{
2909 struct drm_crtc *crtc;
2910 int clock;
2911 int entries, tlb_miss;
2912
2913 crtc = intel_get_crtc_for_plane(dev, plane);
Chris Wilson3490ea52013-01-07 10:11:40 +00002914 if (!intel_crtc_active(crtc)) {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002915 *sprite_wm = display->guard_size;
2916 return false;
2917 }
2918
Ville Syrjälä4fe85902013-09-04 18:25:22 +03002919 clock = to_intel_crtc(crtc)->config.adjusted_mode.clock;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002920
2921 /* Use the small buffer method to calculate the sprite watermark */
2922 entries = ((clock * pixel_size / 1000) * display_latency_ns) / 1000;
2923 tlb_miss = display->fifo_size*display->cacheline_size -
2924 sprite_width * 8;
2925 if (tlb_miss > 0)
2926 entries += tlb_miss;
2927 entries = DIV_ROUND_UP(entries, display->cacheline_size);
2928 *sprite_wm = entries + display->guard_size;
2929 if (*sprite_wm > (int)display->max_wm)
2930 *sprite_wm = display->max_wm;
2931
2932 return true;
2933}
2934
2935static bool
2936sandybridge_compute_sprite_srwm(struct drm_device *dev, int plane,
2937 uint32_t sprite_width, int pixel_size,
2938 const struct intel_watermark_params *display,
2939 int latency_ns, int *sprite_wm)
2940{
2941 struct drm_crtc *crtc;
2942 unsigned long line_time_us;
2943 int clock;
2944 int line_count, line_size;
2945 int small, large;
2946 int entries;
2947
2948 if (!latency_ns) {
2949 *sprite_wm = 0;
2950 return false;
2951 }
2952
2953 crtc = intel_get_crtc_for_plane(dev, plane);
Ville Syrjälä4fe85902013-09-04 18:25:22 +03002954 clock = to_intel_crtc(crtc)->config.adjusted_mode.clock;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002955 if (!clock) {
2956 *sprite_wm = 0;
2957 return false;
2958 }
2959
2960 line_time_us = (sprite_width * 1000) / clock;
2961 if (!line_time_us) {
2962 *sprite_wm = 0;
2963 return false;
2964 }
2965
2966 line_count = (latency_ns / line_time_us + 1000) / 1000;
2967 line_size = sprite_width * pixel_size;
2968
2969 /* Use the minimum of the small and large buffer method for primary */
2970 small = ((clock * pixel_size / 1000) * latency_ns) / 1000;
2971 large = line_count * line_size;
2972
2973 entries = DIV_ROUND_UP(min(small, large), display->cacheline_size);
2974 *sprite_wm = entries + display->guard_size;
2975
2976 return *sprite_wm > 0x3ff ? false : true;
2977}
2978
Ville Syrjäläadf3d352013-08-06 22:24:11 +03002979static void sandybridge_update_sprite_wm(struct drm_plane *plane,
2980 struct drm_crtc *crtc,
Paulo Zanoni4c4ff432013-05-24 11:59:17 -03002981 uint32_t sprite_width, int pixel_size,
Ville Syrjälä39db4a42013-08-06 22:24:00 +03002982 bool enabled, bool scaled)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002983{
Ville Syrjäläadf3d352013-08-06 22:24:11 +03002984 struct drm_device *dev = plane->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002985 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläadf3d352013-08-06 22:24:11 +03002986 int pipe = to_intel_plane(plane)->pipe;
Ville Syrjäläb0aea5d2013-08-01 16:18:54 +03002987 int latency = dev_priv->wm.spr_latency[0] * 100; /* In unit 0.1us */
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002988 u32 val;
2989 int sprite_wm, reg;
2990 int ret;
2991
Ville Syrjälä39db4a42013-08-06 22:24:00 +03002992 if (!enabled)
Paulo Zanoni4c4ff432013-05-24 11:59:17 -03002993 return;
2994
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002995 switch (pipe) {
2996 case 0:
2997 reg = WM0_PIPEA_ILK;
2998 break;
2999 case 1:
3000 reg = WM0_PIPEB_ILK;
3001 break;
3002 case 2:
3003 reg = WM0_PIPEC_IVB;
3004 break;
3005 default:
3006 return; /* bad pipe */
3007 }
3008
3009 ret = sandybridge_compute_sprite_wm(dev, pipe, sprite_width, pixel_size,
3010 &sandybridge_display_wm_info,
3011 latency, &sprite_wm);
3012 if (!ret) {
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03003013 DRM_DEBUG_KMS("failed to compute sprite wm for pipe %c\n",
3014 pipe_name(pipe));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03003015 return;
3016 }
3017
3018 val = I915_READ(reg);
3019 val &= ~WM0_PIPE_SPRITE_MASK;
3020 I915_WRITE(reg, val | (sprite_wm << WM0_PIPE_SPRITE_SHIFT));
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03003021 DRM_DEBUG_KMS("sprite watermarks For pipe %c - %d\n", pipe_name(pipe), sprite_wm);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03003022
3023
3024 ret = sandybridge_compute_sprite_srwm(dev, pipe, sprite_width,
3025 pixel_size,
3026 &sandybridge_display_srwm_info,
Ville Syrjäläb0aea5d2013-08-01 16:18:54 +03003027 dev_priv->wm.spr_latency[1] * 500,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03003028 &sprite_wm);
3029 if (!ret) {
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03003030 DRM_DEBUG_KMS("failed to compute sprite lp1 wm on pipe %c\n",
3031 pipe_name(pipe));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03003032 return;
3033 }
3034 I915_WRITE(WM1S_LP_ILK, sprite_wm);
3035
3036 /* Only IVB has two more LP watermarks for sprite */
3037 if (!IS_IVYBRIDGE(dev))
3038 return;
3039
3040 ret = sandybridge_compute_sprite_srwm(dev, pipe, sprite_width,
3041 pixel_size,
3042 &sandybridge_display_srwm_info,
Ville Syrjäläb0aea5d2013-08-01 16:18:54 +03003043 dev_priv->wm.spr_latency[2] * 500,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03003044 &sprite_wm);
3045 if (!ret) {
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03003046 DRM_DEBUG_KMS("failed to compute sprite lp2 wm on pipe %c\n",
3047 pipe_name(pipe));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03003048 return;
3049 }
3050 I915_WRITE(WM2S_LP_IVB, sprite_wm);
3051
3052 ret = sandybridge_compute_sprite_srwm(dev, pipe, sprite_width,
3053 pixel_size,
3054 &sandybridge_display_srwm_info,
Ville Syrjäläb0aea5d2013-08-01 16:18:54 +03003055 dev_priv->wm.spr_latency[3] * 500,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03003056 &sprite_wm);
3057 if (!ret) {
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03003058 DRM_DEBUG_KMS("failed to compute sprite lp3 wm on pipe %c\n",
3059 pipe_name(pipe));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03003060 return;
3061 }
3062 I915_WRITE(WM3S_LP_IVB, sprite_wm);
3063}
3064
3065/**
3066 * intel_update_watermarks - update FIFO watermark values based on current modes
3067 *
3068 * Calculate watermark values for the various WM regs based on current mode
3069 * and plane configuration.
3070 *
3071 * There are several cases to deal with here:
3072 * - normal (i.e. non-self-refresh)
3073 * - self-refresh (SR) mode
3074 * - lines are large relative to FIFO size (buffer can hold up to 2)
3075 * - lines are small relative to FIFO size (buffer can hold more than 2
3076 * lines), so need to account for TLB latency
3077 *
3078 * The normal calculation is:
3079 * watermark = dotclock * bytes per pixel * latency
3080 * where latency is platform & configuration dependent (we assume pessimal
3081 * values here).
3082 *
3083 * The SR calculation is:
3084 * watermark = (trunc(latency/line time)+1) * surface width *
3085 * bytes per pixel
3086 * where
3087 * line time = htotal / dotclock
3088 * surface width = hdisplay for normal plane and 64 for cursor
3089 * and latency is assumed to be high, as above.
3090 *
3091 * The final value programmed to the register should always be rounded up,
3092 * and include an extra 2 entries to account for clock crossings.
3093 *
3094 * We don't use the sprite, so we can ignore that. And on Crestline we have
3095 * to set the non-SR watermarks to 8.
3096 */
Ville Syrjälä46ba6142013-09-10 11:40:40 +03003097void intel_update_watermarks(struct drm_crtc *crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03003098{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03003099 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03003100
3101 if (dev_priv->display.update_wm)
Ville Syrjälä46ba6142013-09-10 11:40:40 +03003102 dev_priv->display.update_wm(crtc);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03003103}
3104
Ville Syrjäläadf3d352013-08-06 22:24:11 +03003105void intel_update_sprite_watermarks(struct drm_plane *plane,
3106 struct drm_crtc *crtc,
Paulo Zanoni4c4ff432013-05-24 11:59:17 -03003107 uint32_t sprite_width, int pixel_size,
Ville Syrjälä39db4a42013-08-06 22:24:00 +03003108 bool enabled, bool scaled)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03003109{
Ville Syrjäläadf3d352013-08-06 22:24:11 +03003110 struct drm_i915_private *dev_priv = plane->dev->dev_private;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03003111
3112 if (dev_priv->display.update_sprite_wm)
Ville Syrjäläadf3d352013-08-06 22:24:11 +03003113 dev_priv->display.update_sprite_wm(plane, crtc, sprite_width,
Ville Syrjälä39db4a42013-08-06 22:24:00 +03003114 pixel_size, enabled, scaled);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03003115}
3116
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003117static struct drm_i915_gem_object *
3118intel_alloc_context_page(struct drm_device *dev)
3119{
3120 struct drm_i915_gem_object *ctx;
3121 int ret;
3122
3123 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
3124
3125 ctx = i915_gem_alloc_object(dev, 4096);
3126 if (!ctx) {
3127 DRM_DEBUG("failed to alloc power context, RC6 disabled\n");
3128 return NULL;
3129 }
3130
Ben Widawskyc37e2202013-07-31 16:59:58 -07003131 ret = i915_gem_obj_ggtt_pin(ctx, 4096, true, false);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003132 if (ret) {
3133 DRM_ERROR("failed to pin power context: %d\n", ret);
3134 goto err_unref;
3135 }
3136
3137 ret = i915_gem_object_set_to_gtt_domain(ctx, 1);
3138 if (ret) {
3139 DRM_ERROR("failed to set-domain on power context: %d\n", ret);
3140 goto err_unpin;
3141 }
3142
3143 return ctx;
3144
3145err_unpin:
3146 i915_gem_object_unpin(ctx);
3147err_unref:
3148 drm_gem_object_unreference(&ctx->base);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003149 return NULL;
3150}
3151
Daniel Vetter92703882012-08-09 16:46:01 +02003152/**
3153 * Lock protecting IPS related data structures
Daniel Vetter92703882012-08-09 16:46:01 +02003154 */
3155DEFINE_SPINLOCK(mchdev_lock);
3156
3157/* Global for IPS driver to get at the current i915 device. Protected by
3158 * mchdev_lock. */
3159static struct drm_i915_private *i915_mch_dev;
3160
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003161bool ironlake_set_drps(struct drm_device *dev, u8 val)
3162{
3163 struct drm_i915_private *dev_priv = dev->dev_private;
3164 u16 rgvswctl;
3165
Daniel Vetter92703882012-08-09 16:46:01 +02003166 assert_spin_locked(&mchdev_lock);
3167
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003168 rgvswctl = I915_READ16(MEMSWCTL);
3169 if (rgvswctl & MEMCTL_CMD_STS) {
3170 DRM_DEBUG("gpu busy, RCS change rejected\n");
3171 return false; /* still busy with another command */
3172 }
3173
3174 rgvswctl = (MEMCTL_CMD_CHFREQ << MEMCTL_CMD_SHIFT) |
3175 (val << MEMCTL_FREQ_SHIFT) | MEMCTL_SFCAVM;
3176 I915_WRITE16(MEMSWCTL, rgvswctl);
3177 POSTING_READ16(MEMSWCTL);
3178
3179 rgvswctl |= MEMCTL_CMD_STS;
3180 I915_WRITE16(MEMSWCTL, rgvswctl);
3181
3182 return true;
3183}
3184
Daniel Vetter8090c6b2012-06-24 16:42:32 +02003185static void ironlake_enable_drps(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003186{
3187 struct drm_i915_private *dev_priv = dev->dev_private;
3188 u32 rgvmodectl = I915_READ(MEMMODECTL);
3189 u8 fmax, fmin, fstart, vstart;
3190
Daniel Vetter92703882012-08-09 16:46:01 +02003191 spin_lock_irq(&mchdev_lock);
3192
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003193 /* Enable temp reporting */
3194 I915_WRITE16(PMMISC, I915_READ(PMMISC) | MCPPCE_EN);
3195 I915_WRITE16(TSC1, I915_READ(TSC1) | TSE);
3196
3197 /* 100ms RC evaluation intervals */
3198 I915_WRITE(RCUPEI, 100000);
3199 I915_WRITE(RCDNEI, 100000);
3200
3201 /* Set max/min thresholds to 90ms and 80ms respectively */
3202 I915_WRITE(RCBMAXAVG, 90000);
3203 I915_WRITE(RCBMINAVG, 80000);
3204
3205 I915_WRITE(MEMIHYST, 1);
3206
3207 /* Set up min, max, and cur for interrupt handling */
3208 fmax = (rgvmodectl & MEMMODE_FMAX_MASK) >> MEMMODE_FMAX_SHIFT;
3209 fmin = (rgvmodectl & MEMMODE_FMIN_MASK);
3210 fstart = (rgvmodectl & MEMMODE_FSTART_MASK) >>
3211 MEMMODE_FSTART_SHIFT;
3212
3213 vstart = (I915_READ(PXVFREQ_BASE + (fstart * 4)) & PXVFREQ_PX_MASK) >>
3214 PXVFREQ_PX_SHIFT;
3215
Daniel Vetter20e4d402012-08-08 23:35:39 +02003216 dev_priv->ips.fmax = fmax; /* IPS callback will increase this */
3217 dev_priv->ips.fstart = fstart;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003218
Daniel Vetter20e4d402012-08-08 23:35:39 +02003219 dev_priv->ips.max_delay = fstart;
3220 dev_priv->ips.min_delay = fmin;
3221 dev_priv->ips.cur_delay = fstart;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003222
3223 DRM_DEBUG_DRIVER("fmax: %d, fmin: %d, fstart: %d\n",
3224 fmax, fmin, fstart);
3225
3226 I915_WRITE(MEMINTREN, MEMINT_CX_SUPR_EN | MEMINT_EVAL_CHG_EN);
3227
3228 /*
3229 * Interrupts will be enabled in ironlake_irq_postinstall
3230 */
3231
3232 I915_WRITE(VIDSTART, vstart);
3233 POSTING_READ(VIDSTART);
3234
3235 rgvmodectl |= MEMMODE_SWMODE_EN;
3236 I915_WRITE(MEMMODECTL, rgvmodectl);
3237
Daniel Vetter92703882012-08-09 16:46:01 +02003238 if (wait_for_atomic((I915_READ(MEMSWCTL) & MEMCTL_CMD_STS) == 0, 10))
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003239 DRM_ERROR("stuck trying to change perf mode\n");
Daniel Vetter92703882012-08-09 16:46:01 +02003240 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003241
3242 ironlake_set_drps(dev, fstart);
3243
Daniel Vetter20e4d402012-08-08 23:35:39 +02003244 dev_priv->ips.last_count1 = I915_READ(0x112e4) + I915_READ(0x112e8) +
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003245 I915_READ(0x112e0);
Daniel Vetter20e4d402012-08-08 23:35:39 +02003246 dev_priv->ips.last_time1 = jiffies_to_msecs(jiffies);
3247 dev_priv->ips.last_count2 = I915_READ(0x112f4);
3248 getrawmonotonic(&dev_priv->ips.last_time2);
Daniel Vetter92703882012-08-09 16:46:01 +02003249
3250 spin_unlock_irq(&mchdev_lock);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003251}
3252
Daniel Vetter8090c6b2012-06-24 16:42:32 +02003253static void ironlake_disable_drps(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003254{
3255 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter92703882012-08-09 16:46:01 +02003256 u16 rgvswctl;
3257
3258 spin_lock_irq(&mchdev_lock);
3259
3260 rgvswctl = I915_READ16(MEMSWCTL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003261
3262 /* Ack interrupts, disable EFC interrupt */
3263 I915_WRITE(MEMINTREN, I915_READ(MEMINTREN) & ~MEMINT_EVAL_CHG_EN);
3264 I915_WRITE(MEMINTRSTS, MEMINT_EVAL_CHG);
3265 I915_WRITE(DEIER, I915_READ(DEIER) & ~DE_PCU_EVENT);
3266 I915_WRITE(DEIIR, DE_PCU_EVENT);
3267 I915_WRITE(DEIMR, I915_READ(DEIMR) | DE_PCU_EVENT);
3268
3269 /* Go back to the starting frequency */
Daniel Vetter20e4d402012-08-08 23:35:39 +02003270 ironlake_set_drps(dev, dev_priv->ips.fstart);
Daniel Vetter92703882012-08-09 16:46:01 +02003271 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003272 rgvswctl |= MEMCTL_CMD_STS;
3273 I915_WRITE(MEMSWCTL, rgvswctl);
Daniel Vetter92703882012-08-09 16:46:01 +02003274 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003275
Daniel Vetter92703882012-08-09 16:46:01 +02003276 spin_unlock_irq(&mchdev_lock);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003277}
3278
Daniel Vetteracbe9472012-07-26 11:50:05 +02003279/* There's a funny hw issue where the hw returns all 0 when reading from
3280 * GEN6_RP_INTERRUPT_LIMITS. Hence we always need to compute the desired value
3281 * ourselves, instead of doing a rmw cycle (which might result in us clearing
3282 * all limits and the gpu stuck at whatever frequency it is at atm).
3283 */
Daniel Vetter65bccb52012-08-08 17:42:52 +02003284static u32 gen6_rps_limits(struct drm_i915_private *dev_priv, u8 *val)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003285{
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01003286 u32 limits;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003287
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01003288 limits = 0;
Daniel Vetterc6a828d2012-08-08 23:35:35 +02003289
3290 if (*val >= dev_priv->rps.max_delay)
3291 *val = dev_priv->rps.max_delay;
3292 limits |= dev_priv->rps.max_delay << 24;
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01003293
Daniel Vetter20b46e52012-07-26 11:16:14 +02003294 /* Only set the down limit when we've reached the lowest level to avoid
3295 * getting more interrupts, otherwise leave this clear. This prevents a
3296 * race in the hw when coming out of rc6: There's a tiny window where
3297 * the hw runs at the minimal clock before selecting the desired
3298 * frequency, if the down threshold expires in that window we will not
3299 * receive a down interrupt. */
Daniel Vetterc6a828d2012-08-08 23:35:35 +02003300 if (*val <= dev_priv->rps.min_delay) {
3301 *val = dev_priv->rps.min_delay;
3302 limits |= dev_priv->rps.min_delay << 16;
Daniel Vetter20b46e52012-07-26 11:16:14 +02003303 }
3304
3305 return limits;
3306}
3307
3308void gen6_set_rps(struct drm_device *dev, u8 val)
3309{
3310 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter65bccb52012-08-08 17:42:52 +02003311 u32 limits = gen6_rps_limits(dev_priv, &val);
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01003312
Jesse Barnes4fc688c2012-11-02 11:14:01 -07003313 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Ben Widawsky79249632012-09-07 19:43:42 -07003314 WARN_ON(val > dev_priv->rps.max_delay);
3315 WARN_ON(val < dev_priv->rps.min_delay);
Daniel Vetter004777c2012-08-09 15:07:01 +02003316
Daniel Vetterc6a828d2012-08-08 23:35:35 +02003317 if (val == dev_priv->rps.cur_delay)
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01003318 return;
3319
Rodrigo Vivi92bd1bf2013-03-25 17:55:49 -03003320 if (IS_HASWELL(dev))
3321 I915_WRITE(GEN6_RPNSWREQ,
3322 HSW_FREQUENCY(val));
3323 else
3324 I915_WRITE(GEN6_RPNSWREQ,
3325 GEN6_FREQUENCY(val) |
3326 GEN6_OFFSET(0) |
3327 GEN6_AGGRESSIVE_TURBO);
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01003328
3329 /* Make sure we continue to get interrupts
3330 * until we hit the minimum or maximum frequencies.
3331 */
3332 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS, limits);
3333
Ben Widawskyd5570a72012-09-07 19:43:41 -07003334 POSTING_READ(GEN6_RPNSWREQ);
3335
Daniel Vetterc6a828d2012-08-08 23:35:35 +02003336 dev_priv->rps.cur_delay = val;
Daniel Vetterbe2cde92012-08-30 13:26:48 +02003337
3338 trace_intel_gpu_freq_change(val * 50);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003339}
3340
Ville Syrjälä80814ae2013-06-25 19:21:02 +03003341/*
3342 * Wait until the previous freq change has completed,
3343 * or the timeout elapsed, and then update our notion
3344 * of the current GPU frequency.
3345 */
3346static void vlv_update_rps_cur_delay(struct drm_i915_private *dev_priv)
3347{
Ville Syrjälä80814ae2013-06-25 19:21:02 +03003348 u32 pval;
3349
3350 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
3351
Ville Syrjäläe8474402013-06-26 17:43:24 +03003352 if (wait_for(((pval = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS)) & GENFREQSTATUS) == 0, 10))
3353 DRM_DEBUG_DRIVER("timed out waiting for Punit\n");
Ville Syrjälä80814ae2013-06-25 19:21:02 +03003354
3355 pval >>= 8;
3356
3357 if (pval != dev_priv->rps.cur_delay)
3358 DRM_DEBUG_DRIVER("Punit overrode GPU freq: %d MHz (%u) requested, but got %d Mhz (%u)\n",
3359 vlv_gpu_freq(dev_priv->mem_freq, dev_priv->rps.cur_delay),
3360 dev_priv->rps.cur_delay,
3361 vlv_gpu_freq(dev_priv->mem_freq, pval), pval);
3362
3363 dev_priv->rps.cur_delay = pval;
3364}
3365
Jesse Barnes0a073b82013-04-17 15:54:58 -07003366void valleyview_set_rps(struct drm_device *dev, u8 val)
3367{
3368 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä7a670922013-06-25 19:21:06 +03003369
3370 gen6_rps_limits(dev_priv, &val);
Jesse Barnes0a073b82013-04-17 15:54:58 -07003371
3372 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
3373 WARN_ON(val > dev_priv->rps.max_delay);
3374 WARN_ON(val < dev_priv->rps.min_delay);
3375
Ville Syrjälä80814ae2013-06-25 19:21:02 +03003376 vlv_update_rps_cur_delay(dev_priv);
3377
Ville Syrjälä73008b92013-06-25 19:21:01 +03003378 DRM_DEBUG_DRIVER("GPU freq request from %d MHz (%u) to %d MHz (%u)\n",
Jesse Barnes0a073b82013-04-17 15:54:58 -07003379 vlv_gpu_freq(dev_priv->mem_freq,
3380 dev_priv->rps.cur_delay),
Ville Syrjälä73008b92013-06-25 19:21:01 +03003381 dev_priv->rps.cur_delay,
3382 vlv_gpu_freq(dev_priv->mem_freq, val), val);
Jesse Barnes0a073b82013-04-17 15:54:58 -07003383
3384 if (val == dev_priv->rps.cur_delay)
3385 return;
3386
Jani Nikulaae992582013-05-22 15:36:19 +03003387 vlv_punit_write(dev_priv, PUNIT_REG_GPU_FREQ_REQ, val);
Jesse Barnes0a073b82013-04-17 15:54:58 -07003388
Ville Syrjälä80814ae2013-06-25 19:21:02 +03003389 dev_priv->rps.cur_delay = val;
Jesse Barnes0a073b82013-04-17 15:54:58 -07003390
3391 trace_intel_gpu_freq_change(vlv_gpu_freq(dev_priv->mem_freq, val));
3392}
3393
Daniel Vetter44fc7d52013-07-12 22:43:27 +02003394static void gen6_disable_rps_interrupts(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003395{
3396 struct drm_i915_private *dev_priv = dev->dev_private;
3397
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003398 I915_WRITE(GEN6_PMINTRMSK, 0xffffffff);
Ben Widawsky48484052013-05-28 19:22:27 -07003399 I915_WRITE(GEN6_PMIER, I915_READ(GEN6_PMIER) & ~GEN6_PM_RPS_EVENTS);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003400 /* Complete PM interrupt masking here doesn't race with the rps work
3401 * item again unmasking PM interrupts because that is using a different
3402 * register (PMIMR) to mask PM interrupts. The only risk is in leaving
3403 * stale bits in PMIIR and PMIMR which gen6_enable_rps will clean up. */
3404
Daniel Vetter59cdb632013-07-04 23:35:28 +02003405 spin_lock_irq(&dev_priv->irq_lock);
Daniel Vetterc6a828d2012-08-08 23:35:35 +02003406 dev_priv->rps.pm_iir = 0;
Daniel Vetter59cdb632013-07-04 23:35:28 +02003407 spin_unlock_irq(&dev_priv->irq_lock);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003408
Ben Widawsky48484052013-05-28 19:22:27 -07003409 I915_WRITE(GEN6_PMIIR, GEN6_PM_RPS_EVENTS);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003410}
3411
Daniel Vetter44fc7d52013-07-12 22:43:27 +02003412static void gen6_disable_rps(struct drm_device *dev)
3413{
3414 struct drm_i915_private *dev_priv = dev->dev_private;
3415
3416 I915_WRITE(GEN6_RC_CONTROL, 0);
3417 I915_WRITE(GEN6_RPNSWREQ, 1 << 31);
3418
3419 gen6_disable_rps_interrupts(dev);
3420}
3421
Jesse Barnesd20d4f02013-04-23 10:09:28 -07003422static void valleyview_disable_rps(struct drm_device *dev)
3423{
3424 struct drm_i915_private *dev_priv = dev->dev_private;
3425
3426 I915_WRITE(GEN6_RC_CONTROL, 0);
Jesse Barnesd20d4f02013-04-23 10:09:28 -07003427
Daniel Vetter44fc7d52013-07-12 22:43:27 +02003428 gen6_disable_rps_interrupts(dev);
Jesse Barnesc9cddff2013-05-08 10:45:13 -07003429
3430 if (dev_priv->vlv_pctx) {
3431 drm_gem_object_unreference(&dev_priv->vlv_pctx->base);
3432 dev_priv->vlv_pctx = NULL;
3433 }
Jesse Barnesd20d4f02013-04-23 10:09:28 -07003434}
3435
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003436int intel_enable_rc6(const struct drm_device *dev)
3437{
Damien Lespiaueb4926e2013-06-07 17:41:14 +01003438 /* No RC6 before Ironlake */
3439 if (INTEL_INFO(dev)->gen < 5)
3440 return 0;
3441
Daniel Vetter456470e2012-08-08 23:35:40 +02003442 /* Respect the kernel parameter if it is set */
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003443 if (i915_enable_rc6 >= 0)
3444 return i915_enable_rc6;
3445
Chris Wilson6567d742012-11-10 10:00:06 +00003446 /* Disable RC6 on Ironlake */
3447 if (INTEL_INFO(dev)->gen == 5)
3448 return 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003449
Daniel Vetter456470e2012-08-08 23:35:40 +02003450 if (IS_HASWELL(dev)) {
3451 DRM_DEBUG_DRIVER("Haswell: only RC6 available\n");
3452 return INTEL_RC6_ENABLE;
3453 }
3454
3455 /* snb/ivb have more than one rc6 state. */
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003456 if (INTEL_INFO(dev)->gen == 6) {
3457 DRM_DEBUG_DRIVER("Sandybridge: deep RC6 disabled\n");
3458 return INTEL_RC6_ENABLE;
3459 }
Daniel Vetter456470e2012-08-08 23:35:40 +02003460
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003461 DRM_DEBUG_DRIVER("RC6 and deep RC6 enabled\n");
3462 return (INTEL_RC6_ENABLE | INTEL_RC6p_ENABLE);
3463}
3464
Daniel Vetter44fc7d52013-07-12 22:43:27 +02003465static void gen6_enable_rps_interrupts(struct drm_device *dev)
3466{
3467 struct drm_i915_private *dev_priv = dev->dev_private;
Mika Kuoppalaa9c1f902013-08-22 21:09:00 +03003468 u32 enabled_intrs;
Daniel Vetter44fc7d52013-07-12 22:43:27 +02003469
3470 spin_lock_irq(&dev_priv->irq_lock);
Daniel Vettera0b33352013-07-04 23:35:34 +02003471 WARN_ON(dev_priv->rps.pm_iir);
Paulo Zanoniedbfdb42013-08-06 18:57:13 -03003472 snb_enable_pm_irq(dev_priv, GEN6_PM_RPS_EVENTS);
Daniel Vetter44fc7d52013-07-12 22:43:27 +02003473 I915_WRITE(GEN6_PMIIR, GEN6_PM_RPS_EVENTS);
3474 spin_unlock_irq(&dev_priv->irq_lock);
Mika Kuoppalaa9c1f902013-08-22 21:09:00 +03003475
Vinit Azadfd547d22013-08-14 13:34:33 -07003476 /* only unmask PM interrupts we need. Mask all others. */
Mika Kuoppalaa9c1f902013-08-22 21:09:00 +03003477 enabled_intrs = GEN6_PM_RPS_EVENTS;
3478
3479 /* IVB and SNB hard hangs on looping batchbuffer
3480 * if GEN6_PM_UP_EI_EXPIRED is masked.
3481 */
3482 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
3483 enabled_intrs |= GEN6_PM_RP_UP_EI_EXPIRED;
3484
3485 I915_WRITE(GEN6_PMINTRMSK, ~enabled_intrs);
Daniel Vetter44fc7d52013-07-12 22:43:27 +02003486}
3487
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02003488static void gen6_enable_rps(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003489{
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02003490 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonb4519512012-05-11 14:29:30 +01003491 struct intel_ring_buffer *ring;
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01003492 u32 rp_state_cap;
3493 u32 gt_perf_status;
Ben Widawsky31643d52012-09-26 10:34:01 -07003494 u32 rc6vids, pcu_mbox, rc6_mask = 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003495 u32 gtfifodbg;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003496 int rc6_mode;
Ben Widawsky42c05262012-09-26 10:34:00 -07003497 int i, ret;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003498
Jesse Barnes4fc688c2012-11-02 11:14:01 -07003499 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02003500
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003501 /* Here begins a magic sequence of register writes to enable
3502 * auto-downclocking.
3503 *
3504 * Perhaps there might be some value in exposing these to
3505 * userspace...
3506 */
3507 I915_WRITE(GEN6_RC_STATE, 0);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003508
3509 /* Clear the DBG now so we don't confuse earlier errors */
3510 if ((gtfifodbg = I915_READ(GTFIFODBG))) {
3511 DRM_ERROR("GT fifo had a previous error %x\n", gtfifodbg);
3512 I915_WRITE(GTFIFODBG, gtfifodbg);
3513 }
3514
3515 gen6_gt_force_wake_get(dev_priv);
3516
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01003517 rp_state_cap = I915_READ(GEN6_RP_STATE_CAP);
3518 gt_perf_status = I915_READ(GEN6_GT_PERF_STATUS);
3519
Ben Widawsky31c77382013-04-05 14:29:22 -07003520 /* In units of 50MHz */
3521 dev_priv->rps.hw_max = dev_priv->rps.max_delay = rp_state_cap & 0xff;
Daniel Vetterc6a828d2012-08-08 23:35:35 +02003522 dev_priv->rps.min_delay = (rp_state_cap & 0xff0000) >> 16;
3523 dev_priv->rps.cur_delay = 0;
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01003524
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003525 /* disable the counters and set deterministic thresholds */
3526 I915_WRITE(GEN6_RC_CONTROL, 0);
3527
3528 I915_WRITE(GEN6_RC1_WAKE_RATE_LIMIT, 1000 << 16);
3529 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16 | 30);
3530 I915_WRITE(GEN6_RC6pp_WAKE_RATE_LIMIT, 30);
3531 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
3532 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
3533
Chris Wilsonb4519512012-05-11 14:29:30 +01003534 for_each_ring(ring, dev_priv, i)
3535 I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003536
3537 I915_WRITE(GEN6_RC_SLEEP, 0);
3538 I915_WRITE(GEN6_RC1e_THRESHOLD, 1000);
Stéphane Marchesin351aa562013-08-13 11:55:17 -07003539 if (INTEL_INFO(dev)->gen <= 6 || IS_IVYBRIDGE(dev))
3540 I915_WRITE(GEN6_RC6_THRESHOLD, 125000);
3541 else
3542 I915_WRITE(GEN6_RC6_THRESHOLD, 50000);
Stéphane Marchesin0920a482013-01-29 19:41:59 -08003543 I915_WRITE(GEN6_RC6p_THRESHOLD, 150000);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003544 I915_WRITE(GEN6_RC6pp_THRESHOLD, 64000); /* unused */
3545
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03003546 /* Check if we are enabling RC6 */
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003547 rc6_mode = intel_enable_rc6(dev_priv->dev);
3548 if (rc6_mode & INTEL_RC6_ENABLE)
3549 rc6_mask |= GEN6_RC_CTL_RC6_ENABLE;
3550
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03003551 /* We don't use those on Haswell */
3552 if (!IS_HASWELL(dev)) {
3553 if (rc6_mode & INTEL_RC6p_ENABLE)
3554 rc6_mask |= GEN6_RC_CTL_RC6p_ENABLE;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003555
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03003556 if (rc6_mode & INTEL_RC6pp_ENABLE)
3557 rc6_mask |= GEN6_RC_CTL_RC6pp_ENABLE;
3558 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003559
3560 DRM_INFO("Enabling RC6 states: RC6 %s, RC6p %s, RC6pp %s\n",
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03003561 (rc6_mask & GEN6_RC_CTL_RC6_ENABLE) ? "on" : "off",
3562 (rc6_mask & GEN6_RC_CTL_RC6p_ENABLE) ? "on" : "off",
3563 (rc6_mask & GEN6_RC_CTL_RC6pp_ENABLE) ? "on" : "off");
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003564
3565 I915_WRITE(GEN6_RC_CONTROL,
3566 rc6_mask |
3567 GEN6_RC_CTL_EI_MODE(1) |
3568 GEN6_RC_CTL_HW_ENABLE);
3569
Rodrigo Vivi92bd1bf2013-03-25 17:55:49 -03003570 if (IS_HASWELL(dev)) {
3571 I915_WRITE(GEN6_RPNSWREQ,
3572 HSW_FREQUENCY(10));
3573 I915_WRITE(GEN6_RC_VIDEO_FREQ,
3574 HSW_FREQUENCY(12));
3575 } else {
3576 I915_WRITE(GEN6_RPNSWREQ,
3577 GEN6_FREQUENCY(10) |
3578 GEN6_OFFSET(0) |
3579 GEN6_AGGRESSIVE_TURBO);
3580 I915_WRITE(GEN6_RC_VIDEO_FREQ,
3581 GEN6_FREQUENCY(12));
3582 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003583
3584 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 1000000);
3585 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS,
Daniel Vetterc6a828d2012-08-08 23:35:35 +02003586 dev_priv->rps.max_delay << 24 |
3587 dev_priv->rps.min_delay << 16);
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03003588
Daniel Vetter1ee9ae32012-08-15 10:41:45 +02003589 I915_WRITE(GEN6_RP_UP_THRESHOLD, 59400);
3590 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 245000);
3591 I915_WRITE(GEN6_RP_UP_EI, 66000);
3592 I915_WRITE(GEN6_RP_DOWN_EI, 350000);
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03003593
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003594 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
3595 I915_WRITE(GEN6_RP_CONTROL,
3596 GEN6_RP_MEDIA_TURBO |
Jesse Barnes89ba8292012-05-22 09:30:33 -07003597 GEN6_RP_MEDIA_HW_NORMAL_MODE |
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003598 GEN6_RP_MEDIA_IS_GFX |
3599 GEN6_RP_ENABLE |
3600 GEN6_RP_UP_BUSY_AVG |
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03003601 (IS_HASWELL(dev) ? GEN7_RP_DOWN_IDLE_AVG : GEN6_RP_DOWN_IDLE_CONT));
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003602
Ben Widawsky42c05262012-09-26 10:34:00 -07003603 ret = sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_MIN_FREQ_TABLE, 0);
Ben Widawsky988b36e2013-04-23 17:33:02 -07003604 if (!ret) {
Ben Widawsky42c05262012-09-26 10:34:00 -07003605 pcu_mbox = 0;
3606 ret = sandybridge_pcode_read(dev_priv, GEN6_READ_OC_PARAMS, &pcu_mbox);
Ben Widawskya2b3fc02013-03-19 20:19:56 -07003607 if (!ret && (pcu_mbox & (1<<31))) { /* OC supported */
Ben Widawsky10e08492013-04-05 14:29:23 -07003608 DRM_DEBUG_DRIVER("Overclocking supported. Max: %dMHz, Overclock max: %dMHz\n",
Ben Widawskya2b3fc02013-03-19 20:19:56 -07003609 (dev_priv->rps.max_delay & 0xff) * 50,
3610 (pcu_mbox & 0xff) * 50);
Ben Widawsky31c77382013-04-05 14:29:22 -07003611 dev_priv->rps.hw_max = pcu_mbox & 0xff;
Ben Widawsky42c05262012-09-26 10:34:00 -07003612 }
3613 } else {
3614 DRM_DEBUG_DRIVER("Failed to set the min frequency\n");
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003615 }
3616
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01003617 gen6_set_rps(dev_priv->dev, (gt_perf_status & 0xff00) >> 8);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003618
Daniel Vetter44fc7d52013-07-12 22:43:27 +02003619 gen6_enable_rps_interrupts(dev);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003620
Ben Widawsky31643d52012-09-26 10:34:01 -07003621 rc6vids = 0;
3622 ret = sandybridge_pcode_read(dev_priv, GEN6_PCODE_READ_RC6VIDS, &rc6vids);
3623 if (IS_GEN6(dev) && ret) {
3624 DRM_DEBUG_DRIVER("Couldn't check for BIOS workaround\n");
3625 } else if (IS_GEN6(dev) && (GEN6_DECODE_RC6_VID(rc6vids & 0xff) < 450)) {
3626 DRM_DEBUG_DRIVER("You should update your BIOS. Correcting minimum rc6 voltage (%dmV->%dmV)\n",
3627 GEN6_DECODE_RC6_VID(rc6vids & 0xff), 450);
3628 rc6vids &= 0xffff00;
3629 rc6vids |= GEN6_ENCODE_RC6_VID(450);
3630 ret = sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_RC6VIDS, rc6vids);
3631 if (ret)
3632 DRM_ERROR("Couldn't fix incorrect rc6 voltage\n");
3633 }
3634
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003635 gen6_gt_force_wake_put(dev_priv);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003636}
3637
Paulo Zanonic67a4702013-08-19 13:18:09 -03003638void gen6_update_ring_freq(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003639{
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02003640 struct drm_i915_private *dev_priv = dev->dev_private;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003641 int min_freq = 15;
Chris Wilson3ebecd02013-04-12 19:10:13 +01003642 unsigned int gpu_freq;
3643 unsigned int max_ia_freq, min_ring_freq;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003644 int scaling_factor = 180;
3645
Jesse Barnes4fc688c2012-11-02 11:14:01 -07003646 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02003647
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003648 max_ia_freq = cpufreq_quick_get_max(0);
3649 /*
3650 * Default to measured freq if none found, PCU will ensure we don't go
3651 * over
3652 */
3653 if (!max_ia_freq)
3654 max_ia_freq = tsc_khz;
3655
3656 /* Convert from kHz to MHz */
3657 max_ia_freq /= 1000;
3658
Chris Wilson3ebecd02013-04-12 19:10:13 +01003659 min_ring_freq = I915_READ(MCHBAR_MIRROR_BASE_SNB + DCLK);
3660 /* convert DDR frequency from units of 133.3MHz to bandwidth */
3661 min_ring_freq = (2 * 4 * min_ring_freq + 2) / 3;
3662
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003663 /*
3664 * For each potential GPU frequency, load a ring frequency we'd like
3665 * to use for memory access. We do this by specifying the IA frequency
3666 * the PCU should use as a reference to determine the ring frequency.
3667 */
Daniel Vetterc6a828d2012-08-08 23:35:35 +02003668 for (gpu_freq = dev_priv->rps.max_delay; gpu_freq >= dev_priv->rps.min_delay;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003669 gpu_freq--) {
Daniel Vetterc6a828d2012-08-08 23:35:35 +02003670 int diff = dev_priv->rps.max_delay - gpu_freq;
Chris Wilson3ebecd02013-04-12 19:10:13 +01003671 unsigned int ia_freq = 0, ring_freq = 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003672
Chris Wilson3ebecd02013-04-12 19:10:13 +01003673 if (IS_HASWELL(dev)) {
3674 ring_freq = (gpu_freq * 5 + 3) / 4;
3675 ring_freq = max(min_ring_freq, ring_freq);
3676 /* leave ia_freq as the default, chosen by cpufreq */
3677 } else {
3678 /* On older processors, there is no separate ring
3679 * clock domain, so in order to boost the bandwidth
3680 * of the ring, we need to upclock the CPU (ia_freq).
3681 *
3682 * For GPU frequencies less than 750MHz,
3683 * just use the lowest ring freq.
3684 */
3685 if (gpu_freq < min_freq)
3686 ia_freq = 800;
3687 else
3688 ia_freq = max_ia_freq - ((diff * scaling_factor) / 2);
3689 ia_freq = DIV_ROUND_CLOSEST(ia_freq, 100);
3690 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003691
Ben Widawsky42c05262012-09-26 10:34:00 -07003692 sandybridge_pcode_write(dev_priv,
3693 GEN6_PCODE_WRITE_MIN_FREQ_TABLE,
Chris Wilson3ebecd02013-04-12 19:10:13 +01003694 ia_freq << GEN6_PCODE_FREQ_IA_RATIO_SHIFT |
3695 ring_freq << GEN6_PCODE_FREQ_RING_RATIO_SHIFT |
3696 gpu_freq);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003697 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003698}
3699
Jesse Barnes0a073b82013-04-17 15:54:58 -07003700int valleyview_rps_max_freq(struct drm_i915_private *dev_priv)
3701{
3702 u32 val, rp0;
3703
Jani Nikula64936252013-05-22 15:36:20 +03003704 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FREQ_FUSE);
Jesse Barnes0a073b82013-04-17 15:54:58 -07003705
3706 rp0 = (val & FB_GFX_MAX_FREQ_FUSE_MASK) >> FB_GFX_MAX_FREQ_FUSE_SHIFT;
3707 /* Clamp to max */
3708 rp0 = min_t(u32, rp0, 0xea);
3709
3710 return rp0;
3711}
3712
3713static int valleyview_rps_rpe_freq(struct drm_i915_private *dev_priv)
3714{
3715 u32 val, rpe;
3716
Jani Nikula64936252013-05-22 15:36:20 +03003717 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FMAX_FUSE_LO);
Jesse Barnes0a073b82013-04-17 15:54:58 -07003718 rpe = (val & FB_FMAX_VMIN_FREQ_LO_MASK) >> FB_FMAX_VMIN_FREQ_LO_SHIFT;
Jani Nikula64936252013-05-22 15:36:20 +03003719 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FMAX_FUSE_HI);
Jesse Barnes0a073b82013-04-17 15:54:58 -07003720 rpe |= (val & FB_FMAX_VMIN_FREQ_HI_MASK) << 5;
3721
3722 return rpe;
3723}
3724
3725int valleyview_rps_min_freq(struct drm_i915_private *dev_priv)
3726{
Jani Nikula64936252013-05-22 15:36:20 +03003727 return vlv_punit_read(dev_priv, PUNIT_REG_GPU_LFM) & 0xff;
Jesse Barnes0a073b82013-04-17 15:54:58 -07003728}
3729
Jesse Barnes52ceb902013-04-23 10:09:26 -07003730static void vlv_rps_timer_work(struct work_struct *work)
3731{
3732 drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
3733 rps.vlv_work.work);
3734
3735 /*
3736 * Timer fired, we must be idle. Drop to min voltage state.
3737 * Note: we use RPe here since it should match the
3738 * Vmin we were shooting for. That should give us better
3739 * perf when we come back out of RC6 than if we used the
3740 * min freq available.
3741 */
3742 mutex_lock(&dev_priv->rps.hw_lock);
Ville Syrjälä6dc58482013-06-25 21:38:10 +03003743 if (dev_priv->rps.cur_delay > dev_priv->rps.rpe_delay)
3744 valleyview_set_rps(dev_priv->dev, dev_priv->rps.rpe_delay);
Jesse Barnes52ceb902013-04-23 10:09:26 -07003745 mutex_unlock(&dev_priv->rps.hw_lock);
3746}
3747
Jesse Barnesc9cddff2013-05-08 10:45:13 -07003748static void valleyview_setup_pctx(struct drm_device *dev)
3749{
3750 struct drm_i915_private *dev_priv = dev->dev_private;
3751 struct drm_i915_gem_object *pctx;
3752 unsigned long pctx_paddr;
3753 u32 pcbr;
3754 int pctx_size = 24*1024;
3755
3756 pcbr = I915_READ(VLV_PCBR);
3757 if (pcbr) {
3758 /* BIOS set it up already, grab the pre-alloc'd space */
3759 int pcbr_offset;
3760
3761 pcbr_offset = (pcbr & (~4095)) - dev_priv->mm.stolen_base;
3762 pctx = i915_gem_object_create_stolen_for_preallocated(dev_priv->dev,
3763 pcbr_offset,
Daniel Vetter190d6cd2013-07-04 13:06:28 +02003764 I915_GTT_OFFSET_NONE,
Jesse Barnesc9cddff2013-05-08 10:45:13 -07003765 pctx_size);
3766 goto out;
3767 }
3768
3769 /*
3770 * From the Gunit register HAS:
3771 * The Gfx driver is expected to program this register and ensure
3772 * proper allocation within Gfx stolen memory. For example, this
3773 * register should be programmed such than the PCBR range does not
3774 * overlap with other ranges, such as the frame buffer, protected
3775 * memory, or any other relevant ranges.
3776 */
3777 pctx = i915_gem_object_create_stolen(dev, pctx_size);
3778 if (!pctx) {
3779 DRM_DEBUG("not enough stolen space for PCTX, disabling\n");
3780 return;
3781 }
3782
3783 pctx_paddr = dev_priv->mm.stolen_base + pctx->stolen->start;
3784 I915_WRITE(VLV_PCBR, pctx_paddr);
3785
3786out:
3787 dev_priv->vlv_pctx = pctx;
3788}
3789
Jesse Barnes0a073b82013-04-17 15:54:58 -07003790static void valleyview_enable_rps(struct drm_device *dev)
3791{
3792 struct drm_i915_private *dev_priv = dev->dev_private;
3793 struct intel_ring_buffer *ring;
Ville Syrjälä73008b92013-06-25 19:21:01 +03003794 u32 gtfifodbg, val;
Jesse Barnes0a073b82013-04-17 15:54:58 -07003795 int i;
3796
3797 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
3798
3799 if ((gtfifodbg = I915_READ(GTFIFODBG))) {
3800 DRM_ERROR("GT fifo had a previous error %x\n", gtfifodbg);
3801 I915_WRITE(GTFIFODBG, gtfifodbg);
3802 }
3803
Jesse Barnesc9cddff2013-05-08 10:45:13 -07003804 valleyview_setup_pctx(dev);
3805
Jesse Barnes0a073b82013-04-17 15:54:58 -07003806 gen6_gt_force_wake_get(dev_priv);
3807
3808 I915_WRITE(GEN6_RP_UP_THRESHOLD, 59400);
3809 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 245000);
3810 I915_WRITE(GEN6_RP_UP_EI, 66000);
3811 I915_WRITE(GEN6_RP_DOWN_EI, 350000);
3812
3813 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
3814
3815 I915_WRITE(GEN6_RP_CONTROL,
3816 GEN6_RP_MEDIA_TURBO |
3817 GEN6_RP_MEDIA_HW_NORMAL_MODE |
3818 GEN6_RP_MEDIA_IS_GFX |
3819 GEN6_RP_ENABLE |
3820 GEN6_RP_UP_BUSY_AVG |
3821 GEN6_RP_DOWN_IDLE_CONT);
3822
3823 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 0x00280000);
3824 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
3825 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
3826
3827 for_each_ring(ring, dev_priv, i)
3828 I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
3829
3830 I915_WRITE(GEN6_RC6_THRESHOLD, 0xc350);
3831
3832 /* allows RC6 residency counter to work */
3833 I915_WRITE(0x138104, _MASKED_BIT_ENABLE(0x3));
3834 I915_WRITE(GEN6_RC_CONTROL,
3835 GEN7_RC_CTL_TO_MODE);
3836
Jani Nikula64936252013-05-22 15:36:20 +03003837 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
Jesse Barnes24459662013-05-02 10:48:08 -07003838 switch ((val >> 6) & 3) {
3839 case 0:
3840 case 1:
3841 dev_priv->mem_freq = 800;
3842 break;
3843 case 2:
3844 dev_priv->mem_freq = 1066;
3845 break;
3846 case 3:
3847 dev_priv->mem_freq = 1333;
3848 break;
3849 }
Jesse Barnes0a073b82013-04-17 15:54:58 -07003850 DRM_DEBUG_DRIVER("DDR speed: %d MHz", dev_priv->mem_freq);
3851
3852 DRM_DEBUG_DRIVER("GPLL enabled? %s\n", val & 0x10 ? "yes" : "no");
3853 DRM_DEBUG_DRIVER("GPU status: 0x%08x\n", val);
3854
Jesse Barnes0a073b82013-04-17 15:54:58 -07003855 dev_priv->rps.cur_delay = (val >> 8) & 0xff;
Ville Syrjälä73008b92013-06-25 19:21:01 +03003856 DRM_DEBUG_DRIVER("current GPU freq: %d MHz (%u)\n",
3857 vlv_gpu_freq(dev_priv->mem_freq,
3858 dev_priv->rps.cur_delay),
3859 dev_priv->rps.cur_delay);
Jesse Barnes0a073b82013-04-17 15:54:58 -07003860
3861 dev_priv->rps.max_delay = valleyview_rps_max_freq(dev_priv);
3862 dev_priv->rps.hw_max = dev_priv->rps.max_delay;
Ville Syrjälä73008b92013-06-25 19:21:01 +03003863 DRM_DEBUG_DRIVER("max GPU freq: %d MHz (%u)\n",
3864 vlv_gpu_freq(dev_priv->mem_freq,
3865 dev_priv->rps.max_delay),
3866 dev_priv->rps.max_delay);
Jesse Barnes0a073b82013-04-17 15:54:58 -07003867
Ville Syrjälä73008b92013-06-25 19:21:01 +03003868 dev_priv->rps.rpe_delay = valleyview_rps_rpe_freq(dev_priv);
3869 DRM_DEBUG_DRIVER("RPe GPU freq: %d MHz (%u)\n",
3870 vlv_gpu_freq(dev_priv->mem_freq,
3871 dev_priv->rps.rpe_delay),
3872 dev_priv->rps.rpe_delay);
Jesse Barnes0a073b82013-04-17 15:54:58 -07003873
Ville Syrjälä73008b92013-06-25 19:21:01 +03003874 dev_priv->rps.min_delay = valleyview_rps_min_freq(dev_priv);
3875 DRM_DEBUG_DRIVER("min GPU freq: %d MHz (%u)\n",
3876 vlv_gpu_freq(dev_priv->mem_freq,
3877 dev_priv->rps.min_delay),
3878 dev_priv->rps.min_delay);
Jesse Barnes0a073b82013-04-17 15:54:58 -07003879
Ville Syrjälä73008b92013-06-25 19:21:01 +03003880 DRM_DEBUG_DRIVER("setting GPU freq to %d MHz (%u)\n",
3881 vlv_gpu_freq(dev_priv->mem_freq,
3882 dev_priv->rps.rpe_delay),
3883 dev_priv->rps.rpe_delay);
Jesse Barnes0a073b82013-04-17 15:54:58 -07003884
Jesse Barnes52ceb902013-04-23 10:09:26 -07003885 INIT_DELAYED_WORK(&dev_priv->rps.vlv_work, vlv_rps_timer_work);
3886
Ville Syrjälä73008b92013-06-25 19:21:01 +03003887 valleyview_set_rps(dev_priv->dev, dev_priv->rps.rpe_delay);
Jesse Barnes0a073b82013-04-17 15:54:58 -07003888
Daniel Vetter44fc7d52013-07-12 22:43:27 +02003889 gen6_enable_rps_interrupts(dev);
Jesse Barnes0a073b82013-04-17 15:54:58 -07003890
3891 gen6_gt_force_wake_put(dev_priv);
3892}
3893
Daniel Vetter930ebb42012-06-29 23:32:16 +02003894void ironlake_teardown_rc6(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003895{
3896 struct drm_i915_private *dev_priv = dev->dev_private;
3897
Daniel Vetter3e373942012-11-02 19:55:04 +01003898 if (dev_priv->ips.renderctx) {
3899 i915_gem_object_unpin(dev_priv->ips.renderctx);
3900 drm_gem_object_unreference(&dev_priv->ips.renderctx->base);
3901 dev_priv->ips.renderctx = NULL;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003902 }
3903
Daniel Vetter3e373942012-11-02 19:55:04 +01003904 if (dev_priv->ips.pwrctx) {
3905 i915_gem_object_unpin(dev_priv->ips.pwrctx);
3906 drm_gem_object_unreference(&dev_priv->ips.pwrctx->base);
3907 dev_priv->ips.pwrctx = NULL;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003908 }
3909}
3910
Daniel Vetter930ebb42012-06-29 23:32:16 +02003911static void ironlake_disable_rc6(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003912{
3913 struct drm_i915_private *dev_priv = dev->dev_private;
3914
3915 if (I915_READ(PWRCTXA)) {
3916 /* Wake the GPU, prevent RC6, then restore RSTDBYCTL */
3917 I915_WRITE(RSTDBYCTL, I915_READ(RSTDBYCTL) | RCX_SW_EXIT);
3918 wait_for(((I915_READ(RSTDBYCTL) & RSX_STATUS_MASK) == RSX_STATUS_ON),
3919 50);
3920
3921 I915_WRITE(PWRCTXA, 0);
3922 POSTING_READ(PWRCTXA);
3923
3924 I915_WRITE(RSTDBYCTL, I915_READ(RSTDBYCTL) & ~RCX_SW_EXIT);
3925 POSTING_READ(RSTDBYCTL);
3926 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003927}
3928
3929static int ironlake_setup_rc6(struct drm_device *dev)
3930{
3931 struct drm_i915_private *dev_priv = dev->dev_private;
3932
Daniel Vetter3e373942012-11-02 19:55:04 +01003933 if (dev_priv->ips.renderctx == NULL)
3934 dev_priv->ips.renderctx = intel_alloc_context_page(dev);
3935 if (!dev_priv->ips.renderctx)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003936 return -ENOMEM;
3937
Daniel Vetter3e373942012-11-02 19:55:04 +01003938 if (dev_priv->ips.pwrctx == NULL)
3939 dev_priv->ips.pwrctx = intel_alloc_context_page(dev);
3940 if (!dev_priv->ips.pwrctx) {
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003941 ironlake_teardown_rc6(dev);
3942 return -ENOMEM;
3943 }
3944
3945 return 0;
3946}
3947
Daniel Vetter930ebb42012-06-29 23:32:16 +02003948static void ironlake_enable_rc6(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003949{
3950 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter6d90c952012-04-26 23:28:05 +02003951 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Chris Wilson3e960502012-11-27 16:22:54 +00003952 bool was_interruptible;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003953 int ret;
3954
3955 /* rc6 disabled by default due to repeated reports of hanging during
3956 * boot and resume.
3957 */
3958 if (!intel_enable_rc6(dev))
3959 return;
3960
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02003961 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
3962
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003963 ret = ironlake_setup_rc6(dev);
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02003964 if (ret)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003965 return;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003966
Chris Wilson3e960502012-11-27 16:22:54 +00003967 was_interruptible = dev_priv->mm.interruptible;
3968 dev_priv->mm.interruptible = false;
3969
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003970 /*
3971 * GPU can automatically power down the render unit if given a page
3972 * to save state.
3973 */
Daniel Vetter6d90c952012-04-26 23:28:05 +02003974 ret = intel_ring_begin(ring, 6);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003975 if (ret) {
3976 ironlake_teardown_rc6(dev);
Chris Wilson3e960502012-11-27 16:22:54 +00003977 dev_priv->mm.interruptible = was_interruptible;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003978 return;
3979 }
3980
Daniel Vetter6d90c952012-04-26 23:28:05 +02003981 intel_ring_emit(ring, MI_SUSPEND_FLUSH | MI_SUSPEND_FLUSH_EN);
3982 intel_ring_emit(ring, MI_SET_CONTEXT);
Ben Widawskyf343c5f2013-07-05 14:41:04 -07003983 intel_ring_emit(ring, i915_gem_obj_ggtt_offset(dev_priv->ips.renderctx) |
Daniel Vetter6d90c952012-04-26 23:28:05 +02003984 MI_MM_SPACE_GTT |
3985 MI_SAVE_EXT_STATE_EN |
3986 MI_RESTORE_EXT_STATE_EN |
3987 MI_RESTORE_INHIBIT);
3988 intel_ring_emit(ring, MI_SUSPEND_FLUSH);
3989 intel_ring_emit(ring, MI_NOOP);
3990 intel_ring_emit(ring, MI_FLUSH);
3991 intel_ring_advance(ring);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003992
3993 /*
3994 * Wait for the command parser to advance past MI_SET_CONTEXT. The HW
3995 * does an implicit flush, combined with MI_FLUSH above, it should be
3996 * safe to assume that renderctx is valid
3997 */
Chris Wilson3e960502012-11-27 16:22:54 +00003998 ret = intel_ring_idle(ring);
3999 dev_priv->mm.interruptible = was_interruptible;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004000 if (ret) {
Jani Nikuladef27a52013-03-12 10:49:19 +02004001 DRM_ERROR("failed to enable ironlake power savings\n");
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004002 ironlake_teardown_rc6(dev);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004003 return;
4004 }
4005
Ben Widawskyf343c5f2013-07-05 14:41:04 -07004006 I915_WRITE(PWRCTXA, i915_gem_obj_ggtt_offset(dev_priv->ips.pwrctx) | PWRCTX_EN);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004007 I915_WRITE(RSTDBYCTL, I915_READ(RSTDBYCTL) & ~RCX_SW_EXIT);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004008}
4009
Eugeni Dodonovdde18882012-04-18 15:29:24 -03004010static unsigned long intel_pxfreq(u32 vidfreq)
4011{
4012 unsigned long freq;
4013 int div = (vidfreq & 0x3f0000) >> 16;
4014 int post = (vidfreq & 0x3000) >> 12;
4015 int pre = (vidfreq & 0x7);
4016
4017 if (!pre)
4018 return 0;
4019
4020 freq = ((div * 133333) / ((1<<post) * pre));
4021
4022 return freq;
4023}
4024
Daniel Vettereb48eb02012-04-26 23:28:12 +02004025static const struct cparams {
4026 u16 i;
4027 u16 t;
4028 u16 m;
4029 u16 c;
4030} cparams[] = {
4031 { 1, 1333, 301, 28664 },
4032 { 1, 1066, 294, 24460 },
4033 { 1, 800, 294, 25192 },
4034 { 0, 1333, 276, 27605 },
4035 { 0, 1066, 276, 27605 },
4036 { 0, 800, 231, 23784 },
4037};
4038
Chris Wilsonf531dcb2012-09-25 10:16:12 +01004039static unsigned long __i915_chipset_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02004040{
4041 u64 total_count, diff, ret;
4042 u32 count1, count2, count3, m = 0, c = 0;
4043 unsigned long now = jiffies_to_msecs(jiffies), diff1;
4044 int i;
4045
Daniel Vetter02d71952012-08-09 16:44:54 +02004046 assert_spin_locked(&mchdev_lock);
4047
Daniel Vetter20e4d402012-08-08 23:35:39 +02004048 diff1 = now - dev_priv->ips.last_time1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02004049
4050 /* Prevent division-by-zero if we are asking too fast.
4051 * Also, we don't get interesting results if we are polling
4052 * faster than once in 10ms, so just return the saved value
4053 * in such cases.
4054 */
4055 if (diff1 <= 10)
Daniel Vetter20e4d402012-08-08 23:35:39 +02004056 return dev_priv->ips.chipset_power;
Daniel Vettereb48eb02012-04-26 23:28:12 +02004057
4058 count1 = I915_READ(DMIEC);
4059 count2 = I915_READ(DDREC);
4060 count3 = I915_READ(CSIEC);
4061
4062 total_count = count1 + count2 + count3;
4063
4064 /* FIXME: handle per-counter overflow */
Daniel Vetter20e4d402012-08-08 23:35:39 +02004065 if (total_count < dev_priv->ips.last_count1) {
4066 diff = ~0UL - dev_priv->ips.last_count1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02004067 diff += total_count;
4068 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +02004069 diff = total_count - dev_priv->ips.last_count1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02004070 }
4071
4072 for (i = 0; i < ARRAY_SIZE(cparams); i++) {
Daniel Vetter20e4d402012-08-08 23:35:39 +02004073 if (cparams[i].i == dev_priv->ips.c_m &&
4074 cparams[i].t == dev_priv->ips.r_t) {
Daniel Vettereb48eb02012-04-26 23:28:12 +02004075 m = cparams[i].m;
4076 c = cparams[i].c;
4077 break;
4078 }
4079 }
4080
4081 diff = div_u64(diff, diff1);
4082 ret = ((m * diff) + c);
4083 ret = div_u64(ret, 10);
4084
Daniel Vetter20e4d402012-08-08 23:35:39 +02004085 dev_priv->ips.last_count1 = total_count;
4086 dev_priv->ips.last_time1 = now;
Daniel Vettereb48eb02012-04-26 23:28:12 +02004087
Daniel Vetter20e4d402012-08-08 23:35:39 +02004088 dev_priv->ips.chipset_power = ret;
Daniel Vettereb48eb02012-04-26 23:28:12 +02004089
4090 return ret;
4091}
4092
Chris Wilsonf531dcb2012-09-25 10:16:12 +01004093unsigned long i915_chipset_val(struct drm_i915_private *dev_priv)
4094{
4095 unsigned long val;
4096
4097 if (dev_priv->info->gen != 5)
4098 return 0;
4099
4100 spin_lock_irq(&mchdev_lock);
4101
4102 val = __i915_chipset_val(dev_priv);
4103
4104 spin_unlock_irq(&mchdev_lock);
4105
4106 return val;
4107}
4108
Daniel Vettereb48eb02012-04-26 23:28:12 +02004109unsigned long i915_mch_val(struct drm_i915_private *dev_priv)
4110{
4111 unsigned long m, x, b;
4112 u32 tsfs;
4113
4114 tsfs = I915_READ(TSFS);
4115
4116 m = ((tsfs & TSFS_SLOPE_MASK) >> TSFS_SLOPE_SHIFT);
4117 x = I915_READ8(TR1);
4118
4119 b = tsfs & TSFS_INTR_MASK;
4120
4121 return ((m * x) / 127) - b;
4122}
4123
4124static u16 pvid_to_extvid(struct drm_i915_private *dev_priv, u8 pxvid)
4125{
4126 static const struct v_table {
4127 u16 vd; /* in .1 mil */
4128 u16 vm; /* in .1 mil */
4129 } v_table[] = {
4130 { 0, 0, },
4131 { 375, 0, },
4132 { 500, 0, },
4133 { 625, 0, },
4134 { 750, 0, },
4135 { 875, 0, },
4136 { 1000, 0, },
4137 { 1125, 0, },
4138 { 4125, 3000, },
4139 { 4125, 3000, },
4140 { 4125, 3000, },
4141 { 4125, 3000, },
4142 { 4125, 3000, },
4143 { 4125, 3000, },
4144 { 4125, 3000, },
4145 { 4125, 3000, },
4146 { 4125, 3000, },
4147 { 4125, 3000, },
4148 { 4125, 3000, },
4149 { 4125, 3000, },
4150 { 4125, 3000, },
4151 { 4125, 3000, },
4152 { 4125, 3000, },
4153 { 4125, 3000, },
4154 { 4125, 3000, },
4155 { 4125, 3000, },
4156 { 4125, 3000, },
4157 { 4125, 3000, },
4158 { 4125, 3000, },
4159 { 4125, 3000, },
4160 { 4125, 3000, },
4161 { 4125, 3000, },
4162 { 4250, 3125, },
4163 { 4375, 3250, },
4164 { 4500, 3375, },
4165 { 4625, 3500, },
4166 { 4750, 3625, },
4167 { 4875, 3750, },
4168 { 5000, 3875, },
4169 { 5125, 4000, },
4170 { 5250, 4125, },
4171 { 5375, 4250, },
4172 { 5500, 4375, },
4173 { 5625, 4500, },
4174 { 5750, 4625, },
4175 { 5875, 4750, },
4176 { 6000, 4875, },
4177 { 6125, 5000, },
4178 { 6250, 5125, },
4179 { 6375, 5250, },
4180 { 6500, 5375, },
4181 { 6625, 5500, },
4182 { 6750, 5625, },
4183 { 6875, 5750, },
4184 { 7000, 5875, },
4185 { 7125, 6000, },
4186 { 7250, 6125, },
4187 { 7375, 6250, },
4188 { 7500, 6375, },
4189 { 7625, 6500, },
4190 { 7750, 6625, },
4191 { 7875, 6750, },
4192 { 8000, 6875, },
4193 { 8125, 7000, },
4194 { 8250, 7125, },
4195 { 8375, 7250, },
4196 { 8500, 7375, },
4197 { 8625, 7500, },
4198 { 8750, 7625, },
4199 { 8875, 7750, },
4200 { 9000, 7875, },
4201 { 9125, 8000, },
4202 { 9250, 8125, },
4203 { 9375, 8250, },
4204 { 9500, 8375, },
4205 { 9625, 8500, },
4206 { 9750, 8625, },
4207 { 9875, 8750, },
4208 { 10000, 8875, },
4209 { 10125, 9000, },
4210 { 10250, 9125, },
4211 { 10375, 9250, },
4212 { 10500, 9375, },
4213 { 10625, 9500, },
4214 { 10750, 9625, },
4215 { 10875, 9750, },
4216 { 11000, 9875, },
4217 { 11125, 10000, },
4218 { 11250, 10125, },
4219 { 11375, 10250, },
4220 { 11500, 10375, },
4221 { 11625, 10500, },
4222 { 11750, 10625, },
4223 { 11875, 10750, },
4224 { 12000, 10875, },
4225 { 12125, 11000, },
4226 { 12250, 11125, },
4227 { 12375, 11250, },
4228 { 12500, 11375, },
4229 { 12625, 11500, },
4230 { 12750, 11625, },
4231 { 12875, 11750, },
4232 { 13000, 11875, },
4233 { 13125, 12000, },
4234 { 13250, 12125, },
4235 { 13375, 12250, },
4236 { 13500, 12375, },
4237 { 13625, 12500, },
4238 { 13750, 12625, },
4239 { 13875, 12750, },
4240 { 14000, 12875, },
4241 { 14125, 13000, },
4242 { 14250, 13125, },
4243 { 14375, 13250, },
4244 { 14500, 13375, },
4245 { 14625, 13500, },
4246 { 14750, 13625, },
4247 { 14875, 13750, },
4248 { 15000, 13875, },
4249 { 15125, 14000, },
4250 { 15250, 14125, },
4251 { 15375, 14250, },
4252 { 15500, 14375, },
4253 { 15625, 14500, },
4254 { 15750, 14625, },
4255 { 15875, 14750, },
4256 { 16000, 14875, },
4257 { 16125, 15000, },
4258 };
4259 if (dev_priv->info->is_mobile)
4260 return v_table[pxvid].vm;
4261 else
4262 return v_table[pxvid].vd;
4263}
4264
Daniel Vetter02d71952012-08-09 16:44:54 +02004265static void __i915_update_gfx_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02004266{
4267 struct timespec now, diff1;
4268 u64 diff;
4269 unsigned long diffms;
4270 u32 count;
4271
Daniel Vetter02d71952012-08-09 16:44:54 +02004272 assert_spin_locked(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004273
4274 getrawmonotonic(&now);
Daniel Vetter20e4d402012-08-08 23:35:39 +02004275 diff1 = timespec_sub(now, dev_priv->ips.last_time2);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004276
4277 /* Don't divide by 0 */
4278 diffms = diff1.tv_sec * 1000 + diff1.tv_nsec / 1000000;
4279 if (!diffms)
4280 return;
4281
4282 count = I915_READ(GFXEC);
4283
Daniel Vetter20e4d402012-08-08 23:35:39 +02004284 if (count < dev_priv->ips.last_count2) {
4285 diff = ~0UL - dev_priv->ips.last_count2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02004286 diff += count;
4287 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +02004288 diff = count - dev_priv->ips.last_count2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02004289 }
4290
Daniel Vetter20e4d402012-08-08 23:35:39 +02004291 dev_priv->ips.last_count2 = count;
4292 dev_priv->ips.last_time2 = now;
Daniel Vettereb48eb02012-04-26 23:28:12 +02004293
4294 /* More magic constants... */
4295 diff = diff * 1181;
4296 diff = div_u64(diff, diffms * 10);
Daniel Vetter20e4d402012-08-08 23:35:39 +02004297 dev_priv->ips.gfx_power = diff;
Daniel Vettereb48eb02012-04-26 23:28:12 +02004298}
4299
Daniel Vetter02d71952012-08-09 16:44:54 +02004300void i915_update_gfx_val(struct drm_i915_private *dev_priv)
4301{
4302 if (dev_priv->info->gen != 5)
4303 return;
4304
Daniel Vetter92703882012-08-09 16:46:01 +02004305 spin_lock_irq(&mchdev_lock);
Daniel Vetter02d71952012-08-09 16:44:54 +02004306
4307 __i915_update_gfx_val(dev_priv);
4308
Daniel Vetter92703882012-08-09 16:46:01 +02004309 spin_unlock_irq(&mchdev_lock);
Daniel Vetter02d71952012-08-09 16:44:54 +02004310}
4311
Chris Wilsonf531dcb2012-09-25 10:16:12 +01004312static unsigned long __i915_gfx_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02004313{
4314 unsigned long t, corr, state1, corr2, state2;
4315 u32 pxvid, ext_v;
4316
Daniel Vetter02d71952012-08-09 16:44:54 +02004317 assert_spin_locked(&mchdev_lock);
4318
Daniel Vetterc6a828d2012-08-08 23:35:35 +02004319 pxvid = I915_READ(PXVFREQ_BASE + (dev_priv->rps.cur_delay * 4));
Daniel Vettereb48eb02012-04-26 23:28:12 +02004320 pxvid = (pxvid >> 24) & 0x7f;
4321 ext_v = pvid_to_extvid(dev_priv, pxvid);
4322
4323 state1 = ext_v;
4324
4325 t = i915_mch_val(dev_priv);
4326
4327 /* Revel in the empirically derived constants */
4328
4329 /* Correction factor in 1/100000 units */
4330 if (t > 80)
4331 corr = ((t * 2349) + 135940);
4332 else if (t >= 50)
4333 corr = ((t * 964) + 29317);
4334 else /* < 50 */
4335 corr = ((t * 301) + 1004);
4336
4337 corr = corr * ((150142 * state1) / 10000 - 78642);
4338 corr /= 100000;
Daniel Vetter20e4d402012-08-08 23:35:39 +02004339 corr2 = (corr * dev_priv->ips.corr);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004340
4341 state2 = (corr2 * state1) / 10000;
4342 state2 /= 100; /* convert to mW */
4343
Daniel Vetter02d71952012-08-09 16:44:54 +02004344 __i915_update_gfx_val(dev_priv);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004345
Daniel Vetter20e4d402012-08-08 23:35:39 +02004346 return dev_priv->ips.gfx_power + state2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02004347}
4348
Chris Wilsonf531dcb2012-09-25 10:16:12 +01004349unsigned long i915_gfx_val(struct drm_i915_private *dev_priv)
4350{
4351 unsigned long val;
4352
4353 if (dev_priv->info->gen != 5)
4354 return 0;
4355
4356 spin_lock_irq(&mchdev_lock);
4357
4358 val = __i915_gfx_val(dev_priv);
4359
4360 spin_unlock_irq(&mchdev_lock);
4361
4362 return val;
4363}
4364
Daniel Vettereb48eb02012-04-26 23:28:12 +02004365/**
4366 * i915_read_mch_val - return value for IPS use
4367 *
4368 * Calculate and return a value for the IPS driver to use when deciding whether
4369 * we have thermal and power headroom to increase CPU or GPU power budget.
4370 */
4371unsigned long i915_read_mch_val(void)
4372{
4373 struct drm_i915_private *dev_priv;
4374 unsigned long chipset_val, graphics_val, ret = 0;
4375
Daniel Vetter92703882012-08-09 16:46:01 +02004376 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004377 if (!i915_mch_dev)
4378 goto out_unlock;
4379 dev_priv = i915_mch_dev;
4380
Chris Wilsonf531dcb2012-09-25 10:16:12 +01004381 chipset_val = __i915_chipset_val(dev_priv);
4382 graphics_val = __i915_gfx_val(dev_priv);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004383
4384 ret = chipset_val + graphics_val;
4385
4386out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02004387 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004388
4389 return ret;
4390}
4391EXPORT_SYMBOL_GPL(i915_read_mch_val);
4392
4393/**
4394 * i915_gpu_raise - raise GPU frequency limit
4395 *
4396 * Raise the limit; IPS indicates we have thermal headroom.
4397 */
4398bool i915_gpu_raise(void)
4399{
4400 struct drm_i915_private *dev_priv;
4401 bool ret = true;
4402
Daniel Vetter92703882012-08-09 16:46:01 +02004403 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004404 if (!i915_mch_dev) {
4405 ret = false;
4406 goto out_unlock;
4407 }
4408 dev_priv = i915_mch_dev;
4409
Daniel Vetter20e4d402012-08-08 23:35:39 +02004410 if (dev_priv->ips.max_delay > dev_priv->ips.fmax)
4411 dev_priv->ips.max_delay--;
Daniel Vettereb48eb02012-04-26 23:28:12 +02004412
4413out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02004414 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004415
4416 return ret;
4417}
4418EXPORT_SYMBOL_GPL(i915_gpu_raise);
4419
4420/**
4421 * i915_gpu_lower - lower GPU frequency limit
4422 *
4423 * IPS indicates we're close to a thermal limit, so throttle back the GPU
4424 * frequency maximum.
4425 */
4426bool i915_gpu_lower(void)
4427{
4428 struct drm_i915_private *dev_priv;
4429 bool ret = true;
4430
Daniel Vetter92703882012-08-09 16:46:01 +02004431 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004432 if (!i915_mch_dev) {
4433 ret = false;
4434 goto out_unlock;
4435 }
4436 dev_priv = i915_mch_dev;
4437
Daniel Vetter20e4d402012-08-08 23:35:39 +02004438 if (dev_priv->ips.max_delay < dev_priv->ips.min_delay)
4439 dev_priv->ips.max_delay++;
Daniel Vettereb48eb02012-04-26 23:28:12 +02004440
4441out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02004442 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004443
4444 return ret;
4445}
4446EXPORT_SYMBOL_GPL(i915_gpu_lower);
4447
4448/**
4449 * i915_gpu_busy - indicate GPU business to IPS
4450 *
4451 * Tell the IPS driver whether or not the GPU is busy.
4452 */
4453bool i915_gpu_busy(void)
4454{
4455 struct drm_i915_private *dev_priv;
Chris Wilsonf047e392012-07-21 12:31:41 +01004456 struct intel_ring_buffer *ring;
Daniel Vettereb48eb02012-04-26 23:28:12 +02004457 bool ret = false;
Chris Wilsonf047e392012-07-21 12:31:41 +01004458 int i;
Daniel Vettereb48eb02012-04-26 23:28:12 +02004459
Daniel Vetter92703882012-08-09 16:46:01 +02004460 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004461 if (!i915_mch_dev)
4462 goto out_unlock;
4463 dev_priv = i915_mch_dev;
4464
Chris Wilsonf047e392012-07-21 12:31:41 +01004465 for_each_ring(ring, dev_priv, i)
4466 ret |= !list_empty(&ring->request_list);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004467
4468out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02004469 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004470
4471 return ret;
4472}
4473EXPORT_SYMBOL_GPL(i915_gpu_busy);
4474
4475/**
4476 * i915_gpu_turbo_disable - disable graphics turbo
4477 *
4478 * Disable graphics turbo by resetting the max frequency and setting the
4479 * current frequency to the default.
4480 */
4481bool i915_gpu_turbo_disable(void)
4482{
4483 struct drm_i915_private *dev_priv;
4484 bool ret = true;
4485
Daniel Vetter92703882012-08-09 16:46:01 +02004486 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004487 if (!i915_mch_dev) {
4488 ret = false;
4489 goto out_unlock;
4490 }
4491 dev_priv = i915_mch_dev;
4492
Daniel Vetter20e4d402012-08-08 23:35:39 +02004493 dev_priv->ips.max_delay = dev_priv->ips.fstart;
Daniel Vettereb48eb02012-04-26 23:28:12 +02004494
Daniel Vetter20e4d402012-08-08 23:35:39 +02004495 if (!ironlake_set_drps(dev_priv->dev, dev_priv->ips.fstart))
Daniel Vettereb48eb02012-04-26 23:28:12 +02004496 ret = false;
4497
4498out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02004499 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004500
4501 return ret;
4502}
4503EXPORT_SYMBOL_GPL(i915_gpu_turbo_disable);
4504
4505/**
4506 * Tells the intel_ips driver that the i915 driver is now loaded, if
4507 * IPS got loaded first.
4508 *
4509 * This awkward dance is so that neither module has to depend on the
4510 * other in order for IPS to do the appropriate communication of
4511 * GPU turbo limits to i915.
4512 */
4513static void
4514ips_ping_for_i915_load(void)
4515{
4516 void (*link)(void);
4517
4518 link = symbol_get(ips_link_to_i915_driver);
4519 if (link) {
4520 link();
4521 symbol_put(ips_link_to_i915_driver);
4522 }
4523}
4524
4525void intel_gpu_ips_init(struct drm_i915_private *dev_priv)
4526{
Daniel Vetter02d71952012-08-09 16:44:54 +02004527 /* We only register the i915 ips part with intel-ips once everything is
4528 * set up, to avoid intel-ips sneaking in and reading bogus values. */
Daniel Vetter92703882012-08-09 16:46:01 +02004529 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004530 i915_mch_dev = dev_priv;
Daniel Vetter92703882012-08-09 16:46:01 +02004531 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004532
4533 ips_ping_for_i915_load();
4534}
4535
4536void intel_gpu_ips_teardown(void)
4537{
Daniel Vetter92703882012-08-09 16:46:01 +02004538 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004539 i915_mch_dev = NULL;
Daniel Vetter92703882012-08-09 16:46:01 +02004540 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004541}
Daniel Vetter8090c6b2012-06-24 16:42:32 +02004542static void intel_init_emon(struct drm_device *dev)
Eugeni Dodonovdde18882012-04-18 15:29:24 -03004543{
4544 struct drm_i915_private *dev_priv = dev->dev_private;
4545 u32 lcfuse;
4546 u8 pxw[16];
4547 int i;
4548
4549 /* Disable to program */
4550 I915_WRITE(ECR, 0);
4551 POSTING_READ(ECR);
4552
4553 /* Program energy weights for various events */
4554 I915_WRITE(SDEW, 0x15040d00);
4555 I915_WRITE(CSIEW0, 0x007f0000);
4556 I915_WRITE(CSIEW1, 0x1e220004);
4557 I915_WRITE(CSIEW2, 0x04000004);
4558
4559 for (i = 0; i < 5; i++)
4560 I915_WRITE(PEW + (i * 4), 0);
4561 for (i = 0; i < 3; i++)
4562 I915_WRITE(DEW + (i * 4), 0);
4563
4564 /* Program P-state weights to account for frequency power adjustment */
4565 for (i = 0; i < 16; i++) {
4566 u32 pxvidfreq = I915_READ(PXVFREQ_BASE + (i * 4));
4567 unsigned long freq = intel_pxfreq(pxvidfreq);
4568 unsigned long vid = (pxvidfreq & PXVFREQ_PX_MASK) >>
4569 PXVFREQ_PX_SHIFT;
4570 unsigned long val;
4571
4572 val = vid * vid;
4573 val *= (freq / 1000);
4574 val *= 255;
4575 val /= (127*127*900);
4576 if (val > 0xff)
4577 DRM_ERROR("bad pxval: %ld\n", val);
4578 pxw[i] = val;
4579 }
4580 /* Render standby states get 0 weight */
4581 pxw[14] = 0;
4582 pxw[15] = 0;
4583
4584 for (i = 0; i < 4; i++) {
4585 u32 val = (pxw[i*4] << 24) | (pxw[(i*4)+1] << 16) |
4586 (pxw[(i*4)+2] << 8) | (pxw[(i*4)+3]);
4587 I915_WRITE(PXW + (i * 4), val);
4588 }
4589
4590 /* Adjust magic regs to magic values (more experimental results) */
4591 I915_WRITE(OGW0, 0);
4592 I915_WRITE(OGW1, 0);
4593 I915_WRITE(EG0, 0x00007f00);
4594 I915_WRITE(EG1, 0x0000000e);
4595 I915_WRITE(EG2, 0x000e0000);
4596 I915_WRITE(EG3, 0x68000300);
4597 I915_WRITE(EG4, 0x42000000);
4598 I915_WRITE(EG5, 0x00140031);
4599 I915_WRITE(EG6, 0);
4600 I915_WRITE(EG7, 0);
4601
4602 for (i = 0; i < 8; i++)
4603 I915_WRITE(PXWL + (i * 4), 0);
4604
4605 /* Enable PMON + select events */
4606 I915_WRITE(ECR, 0x80000019);
4607
4608 lcfuse = I915_READ(LCFUSE02);
4609
Daniel Vetter20e4d402012-08-08 23:35:39 +02004610 dev_priv->ips.corr = (lcfuse & LCFUSE_HIV_MASK);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03004611}
4612
Daniel Vetter8090c6b2012-06-24 16:42:32 +02004613void intel_disable_gt_powersave(struct drm_device *dev)
4614{
Jesse Barnes1a01ab32012-11-02 11:14:00 -07004615 struct drm_i915_private *dev_priv = dev->dev_private;
4616
Daniel Vetterfd0c0642013-04-24 11:13:35 +02004617 /* Interrupts should be disabled already to avoid re-arming. */
4618 WARN_ON(dev->irq_enabled);
4619
Daniel Vetter930ebb42012-06-29 23:32:16 +02004620 if (IS_IRONLAKE_M(dev)) {
Daniel Vetter8090c6b2012-06-24 16:42:32 +02004621 ironlake_disable_drps(dev);
Daniel Vetter930ebb42012-06-29 23:32:16 +02004622 ironlake_disable_rc6(dev);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004623 } else if (INTEL_INFO(dev)->gen >= 6) {
Jesse Barnes1a01ab32012-11-02 11:14:00 -07004624 cancel_delayed_work_sync(&dev_priv->rps.delayed_resume_work);
Jesse Barnes250848c2013-04-23 10:09:27 -07004625 cancel_work_sync(&dev_priv->rps.work);
Jesse Barnes52ceb902013-04-23 10:09:26 -07004626 if (IS_VALLEYVIEW(dev))
4627 cancel_delayed_work_sync(&dev_priv->rps.vlv_work);
Jesse Barnes4fc688c2012-11-02 11:14:01 -07004628 mutex_lock(&dev_priv->rps.hw_lock);
Jesse Barnesd20d4f02013-04-23 10:09:28 -07004629 if (IS_VALLEYVIEW(dev))
4630 valleyview_disable_rps(dev);
4631 else
4632 gen6_disable_rps(dev);
Jesse Barnes4fc688c2012-11-02 11:14:01 -07004633 mutex_unlock(&dev_priv->rps.hw_lock);
Daniel Vetter930ebb42012-06-29 23:32:16 +02004634 }
Daniel Vetter8090c6b2012-06-24 16:42:32 +02004635}
4636
Jesse Barnes1a01ab32012-11-02 11:14:00 -07004637static void intel_gen6_powersave_work(struct work_struct *work)
4638{
4639 struct drm_i915_private *dev_priv =
4640 container_of(work, struct drm_i915_private,
4641 rps.delayed_resume_work.work);
4642 struct drm_device *dev = dev_priv->dev;
4643
Jesse Barnes4fc688c2012-11-02 11:14:01 -07004644 mutex_lock(&dev_priv->rps.hw_lock);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004645
4646 if (IS_VALLEYVIEW(dev)) {
4647 valleyview_enable_rps(dev);
4648 } else {
4649 gen6_enable_rps(dev);
4650 gen6_update_ring_freq(dev);
4651 }
Jesse Barnes4fc688c2012-11-02 11:14:01 -07004652 mutex_unlock(&dev_priv->rps.hw_lock);
Jesse Barnes1a01ab32012-11-02 11:14:00 -07004653}
4654
Daniel Vetter8090c6b2012-06-24 16:42:32 +02004655void intel_enable_gt_powersave(struct drm_device *dev)
4656{
Jesse Barnes1a01ab32012-11-02 11:14:00 -07004657 struct drm_i915_private *dev_priv = dev->dev_private;
4658
Daniel Vetter8090c6b2012-06-24 16:42:32 +02004659 if (IS_IRONLAKE_M(dev)) {
4660 ironlake_enable_drps(dev);
4661 ironlake_enable_rc6(dev);
4662 intel_init_emon(dev);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004663 } else if (IS_GEN6(dev) || IS_GEN7(dev)) {
Jesse Barnes1a01ab32012-11-02 11:14:00 -07004664 /*
4665 * PCU communication is slow and this doesn't need to be
4666 * done at any specific time, so do this out of our fast path
4667 * to make resume and init faster.
4668 */
4669 schedule_delayed_work(&dev_priv->rps.delayed_resume_work,
4670 round_jiffies_up_relative(HZ));
Daniel Vetter8090c6b2012-06-24 16:42:32 +02004671 }
4672}
4673
Daniel Vetter3107bd42012-10-31 22:52:31 +01004674static void ibx_init_clock_gating(struct drm_device *dev)
4675{
4676 struct drm_i915_private *dev_priv = dev->dev_private;
4677
4678 /*
4679 * On Ibex Peak and Cougar Point, we need to disable clock
4680 * gating for the panel power sequencer or it will fail to
4681 * start up when no ports are active.
4682 */
4683 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE);
4684}
4685
Ville Syrjälä0e088b82013-06-07 10:47:04 +03004686static void g4x_disable_trickle_feed(struct drm_device *dev)
4687{
4688 struct drm_i915_private *dev_priv = dev->dev_private;
4689 int pipe;
4690
4691 for_each_pipe(pipe) {
4692 I915_WRITE(DSPCNTR(pipe),
4693 I915_READ(DSPCNTR(pipe)) |
4694 DISPPLANE_TRICKLE_FEED_DISABLE);
4695 intel_flush_display_plane(dev_priv, pipe);
4696 }
4697}
4698
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03004699static void ironlake_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004700{
4701 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau231e54f2012-10-19 17:55:41 +01004702 uint32_t dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004703
Damien Lespiauf1e8fa52013-06-07 17:41:09 +01004704 /*
4705 * Required for FBC
4706 * WaFbcDisableDpfcClockGating:ilk
4707 */
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01004708 dspclk_gate |= ILK_DPFCRUNIT_CLOCK_GATE_DISABLE |
4709 ILK_DPFCUNIT_CLOCK_GATE_DISABLE |
4710 ILK_DPFDUNIT_CLOCK_GATE_ENABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004711
4712 I915_WRITE(PCH_3DCGDIS0,
4713 MARIUNIT_CLOCK_GATE_DISABLE |
4714 SVSMUNIT_CLOCK_GATE_DISABLE);
4715 I915_WRITE(PCH_3DCGDIS1,
4716 VFMUNIT_CLOCK_GATE_DISABLE);
4717
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004718 /*
4719 * According to the spec the following bits should be set in
4720 * order to enable memory self-refresh
4721 * The bit 22/21 of 0x42004
4722 * The bit 5 of 0x42020
4723 * The bit 15 of 0x45000
4724 */
4725 I915_WRITE(ILK_DISPLAY_CHICKEN2,
4726 (I915_READ(ILK_DISPLAY_CHICKEN2) |
4727 ILK_DPARB_GATE | ILK_VSDPFD_FULL));
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01004728 dspclk_gate |= ILK_DPARBUNIT_CLOCK_GATE_ENABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004729 I915_WRITE(DISP_ARB_CTL,
4730 (I915_READ(DISP_ARB_CTL) |
4731 DISP_FBC_WM_DIS));
4732 I915_WRITE(WM3_LP_ILK, 0);
4733 I915_WRITE(WM2_LP_ILK, 0);
4734 I915_WRITE(WM1_LP_ILK, 0);
4735
4736 /*
4737 * Based on the document from hardware guys the following bits
4738 * should be set unconditionally in order to enable FBC.
4739 * The bit 22 of 0x42000
4740 * The bit 22 of 0x42004
4741 * The bit 7,8,9 of 0x42020.
4742 */
4743 if (IS_IRONLAKE_M(dev)) {
Damien Lespiau4bb35332013-06-14 15:23:24 +01004744 /* WaFbcAsynchFlipDisableFbcQueue:ilk */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004745 I915_WRITE(ILK_DISPLAY_CHICKEN1,
4746 I915_READ(ILK_DISPLAY_CHICKEN1) |
4747 ILK_FBCQ_DIS);
4748 I915_WRITE(ILK_DISPLAY_CHICKEN2,
4749 I915_READ(ILK_DISPLAY_CHICKEN2) |
4750 ILK_DPARB_GATE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004751 }
4752
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01004753 I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
4754
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004755 I915_WRITE(ILK_DISPLAY_CHICKEN2,
4756 I915_READ(ILK_DISPLAY_CHICKEN2) |
4757 ILK_ELPIN_409_SELECT);
4758 I915_WRITE(_3D_CHICKEN2,
4759 _3D_CHICKEN2_WM_READ_PIPELINED << 16 |
4760 _3D_CHICKEN2_WM_READ_PIPELINED);
Daniel Vetter4358a372012-10-18 11:49:51 +02004761
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01004762 /* WaDisableRenderCachePipelinedFlush:ilk */
Daniel Vetter4358a372012-10-18 11:49:51 +02004763 I915_WRITE(CACHE_MODE_0,
4764 _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
Daniel Vetter3107bd42012-10-31 22:52:31 +01004765
Ville Syrjälä0e088b82013-06-07 10:47:04 +03004766 g4x_disable_trickle_feed(dev);
Ville Syrjäläbdad2b22013-06-07 10:47:03 +03004767
Daniel Vetter3107bd42012-10-31 22:52:31 +01004768 ibx_init_clock_gating(dev);
4769}
4770
4771static void cpt_init_clock_gating(struct drm_device *dev)
4772{
4773 struct drm_i915_private *dev_priv = dev->dev_private;
4774 int pipe;
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03004775 uint32_t val;
Daniel Vetter3107bd42012-10-31 22:52:31 +01004776
4777 /*
4778 * On Ibex Peak and Cougar Point, we need to disable clock
4779 * gating for the panel power sequencer or it will fail to
4780 * start up when no ports are active.
4781 */
4782 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE);
4783 I915_WRITE(SOUTH_CHICKEN2, I915_READ(SOUTH_CHICKEN2) |
4784 DPLS_EDP_PPS_FIX_DIS);
Takashi Iwai335c07b2012-12-11 11:46:29 +01004785 /* The below fixes the weird display corruption, a few pixels shifted
4786 * downward, on (only) LVDS of some HP laptops with IVY.
4787 */
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03004788 for_each_pipe(pipe) {
Paulo Zanonidc4bd2d2013-04-08 15:48:08 -03004789 val = I915_READ(TRANS_CHICKEN2(pipe));
4790 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
4791 val &= ~TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03004792 if (dev_priv->vbt.fdi_rx_polarity_inverted)
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03004793 val |= TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
Paulo Zanonidc4bd2d2013-04-08 15:48:08 -03004794 val &= ~TRANS_CHICKEN2_FRAME_START_DELAY_MASK;
4795 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_COUNTER;
4796 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_MODESWITCH;
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03004797 I915_WRITE(TRANS_CHICKEN2(pipe), val);
4798 }
Daniel Vetter3107bd42012-10-31 22:52:31 +01004799 /* WADP0ClockGatingDisable */
4800 for_each_pipe(pipe) {
4801 I915_WRITE(TRANS_CHICKEN1(pipe),
4802 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
4803 }
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004804}
4805
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01004806static void gen6_check_mch_setup(struct drm_device *dev)
4807{
4808 struct drm_i915_private *dev_priv = dev->dev_private;
4809 uint32_t tmp;
4810
4811 tmp = I915_READ(MCH_SSKPD);
4812 if ((tmp & MCH_SSKPD_WM0_MASK) != MCH_SSKPD_WM0_VAL) {
4813 DRM_INFO("Wrong MCH_SSKPD value: 0x%08x\n", tmp);
4814 DRM_INFO("This can cause pipe underruns and display issues.\n");
4815 DRM_INFO("Please upgrade your BIOS to fix this.\n");
4816 }
4817}
4818
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03004819static void gen6_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004820{
4821 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau231e54f2012-10-19 17:55:41 +01004822 uint32_t dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004823
Damien Lespiau231e54f2012-10-19 17:55:41 +01004824 I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004825
4826 I915_WRITE(ILK_DISPLAY_CHICKEN2,
4827 I915_READ(ILK_DISPLAY_CHICKEN2) |
4828 ILK_ELPIN_409_SELECT);
4829
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01004830 /* WaDisableHiZPlanesWhenMSAAEnabled:snb */
Daniel Vetter42839082012-12-14 23:38:28 +01004831 I915_WRITE(_3D_CHICKEN,
4832 _MASKED_BIT_ENABLE(_3D_CHICKEN_HIZ_PLANE_DISABLE_MSAA_4X_SNB));
4833
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01004834 /* WaSetupGtModeTdRowDispatch:snb */
Daniel Vetter6547fbd2012-12-14 23:38:29 +01004835 if (IS_SNB_GT1(dev))
4836 I915_WRITE(GEN6_GT_MODE,
4837 _MASKED_BIT_ENABLE(GEN6_TD_FOUR_ROW_DISPATCH_DISABLE));
4838
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004839 I915_WRITE(WM3_LP_ILK, 0);
4840 I915_WRITE(WM2_LP_ILK, 0);
4841 I915_WRITE(WM1_LP_ILK, 0);
4842
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004843 I915_WRITE(CACHE_MODE_0,
Daniel Vetter50743292012-04-26 22:02:54 +02004844 _MASKED_BIT_DISABLE(CM0_STC_EVICT_DISABLE_LRA_SNB));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004845
4846 I915_WRITE(GEN6_UCGCTL1,
4847 I915_READ(GEN6_UCGCTL1) |
4848 GEN6_BLBUNIT_CLOCK_GATE_DISABLE |
4849 GEN6_CSUNIT_CLOCK_GATE_DISABLE);
4850
4851 /* According to the BSpec vol1g, bit 12 (RCPBUNIT) clock
4852 * gating disable must be set. Failure to set it results in
4853 * flickering pixels due to Z write ordering failures after
4854 * some amount of runtime in the Mesa "fire" demo, and Unigine
4855 * Sanctuary and Tropics, and apparently anything else with
4856 * alpha test or pixel discard.
4857 *
4858 * According to the spec, bit 11 (RCCUNIT) must also be set,
4859 * but we didn't debug actual testcases to find it out.
Jesse Barnes0f846f82012-06-14 11:04:47 -07004860 *
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01004861 * Also apply WaDisableVDSUnitClockGating:snb and
4862 * WaDisableRCPBUnitClockGating:snb.
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004863 */
4864 I915_WRITE(GEN6_UCGCTL2,
Jesse Barnes0f846f82012-06-14 11:04:47 -07004865 GEN7_VDSUNIT_CLOCK_GATE_DISABLE |
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004866 GEN6_RCPBUNIT_CLOCK_GATE_DISABLE |
4867 GEN6_RCCUNIT_CLOCK_GATE_DISABLE);
4868
4869 /* Bspec says we need to always set all mask bits. */
Kenneth Graunke26b6e442012-10-07 08:51:07 -07004870 I915_WRITE(_3D_CHICKEN3, (0xFFFF << 16) |
4871 _3D_CHICKEN3_SF_DISABLE_FASTCLIP_CULL);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004872
4873 /*
4874 * According to the spec the following bits should be
4875 * set in order to enable memory self-refresh and fbc:
4876 * The bit21 and bit22 of 0x42000
4877 * The bit21 and bit22 of 0x42004
4878 * The bit5 and bit7 of 0x42020
4879 * The bit14 of 0x70180
4880 * The bit14 of 0x71180
Damien Lespiau4bb35332013-06-14 15:23:24 +01004881 *
4882 * WaFbcAsynchFlipDisableFbcQueue:snb
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004883 */
4884 I915_WRITE(ILK_DISPLAY_CHICKEN1,
4885 I915_READ(ILK_DISPLAY_CHICKEN1) |
4886 ILK_FBCQ_DIS | ILK_PABSTRETCH_DIS);
4887 I915_WRITE(ILK_DISPLAY_CHICKEN2,
4888 I915_READ(ILK_DISPLAY_CHICKEN2) |
4889 ILK_DPARB_GATE | ILK_VSDPFD_FULL);
Damien Lespiau231e54f2012-10-19 17:55:41 +01004890 I915_WRITE(ILK_DSPCLK_GATE_D,
4891 I915_READ(ILK_DSPCLK_GATE_D) |
4892 ILK_DPARBUNIT_CLOCK_GATE_ENABLE |
4893 ILK_DPFDUNIT_CLOCK_GATE_ENABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004894
Ville Syrjälä0e088b82013-06-07 10:47:04 +03004895 g4x_disable_trickle_feed(dev);
Ben Widawskyf8f2ac92012-10-03 19:34:24 -07004896
4897 /* The default value should be 0x200 according to docs, but the two
4898 * platforms I checked have a 0 for this. (Maybe BIOS overrides?) */
4899 I915_WRITE(GEN6_GT_MODE, _MASKED_BIT_DISABLE(0xffff));
4900 I915_WRITE(GEN6_GT_MODE, _MASKED_BIT_ENABLE(GEN6_GT_MODE_HI));
Daniel Vetter3107bd42012-10-31 22:52:31 +01004901
4902 cpt_init_clock_gating(dev);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01004903
4904 gen6_check_mch_setup(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004905}
4906
4907static void gen7_setup_fixed_func_scheduler(struct drm_i915_private *dev_priv)
4908{
4909 uint32_t reg = I915_READ(GEN7_FF_THREAD_MODE);
4910
4911 reg &= ~GEN7_FF_SCHED_MASK;
4912 reg |= GEN7_FF_TS_SCHED_HW;
4913 reg |= GEN7_FF_VS_SCHED_HW;
4914 reg |= GEN7_FF_DS_SCHED_HW;
4915
Ben Widawsky41c0b3a2013-01-26 11:52:00 -08004916 if (IS_HASWELL(dev_priv->dev))
4917 reg &= ~GEN7_FF_VS_REF_CNT_FFME;
4918
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004919 I915_WRITE(GEN7_FF_THREAD_MODE, reg);
4920}
4921
Paulo Zanoni17a303e2012-11-20 15:12:07 -02004922static void lpt_init_clock_gating(struct drm_device *dev)
4923{
4924 struct drm_i915_private *dev_priv = dev->dev_private;
4925
4926 /*
4927 * TODO: this bit should only be enabled when really needed, then
4928 * disabled when not needed anymore in order to save power.
4929 */
4930 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE)
4931 I915_WRITE(SOUTH_DSPCLK_GATE_D,
4932 I915_READ(SOUTH_DSPCLK_GATE_D) |
4933 PCH_LP_PARTITION_LEVEL_DISABLE);
Paulo Zanoni0a790cd2013-04-17 18:15:49 -03004934
4935 /* WADPOClockGatingDisable:hsw */
4936 I915_WRITE(_TRANSA_CHICKEN1,
4937 I915_READ(_TRANSA_CHICKEN1) |
4938 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
Paulo Zanoni17a303e2012-11-20 15:12:07 -02004939}
4940
Imre Deak7d708ee2013-04-17 14:04:50 +03004941static void lpt_suspend_hw(struct drm_device *dev)
4942{
4943 struct drm_i915_private *dev_priv = dev->dev_private;
4944
4945 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
4946 uint32_t val = I915_READ(SOUTH_DSPCLK_GATE_D);
4947
4948 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
4949 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
4950 }
4951}
4952
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03004953static void haswell_init_clock_gating(struct drm_device *dev)
4954{
4955 struct drm_i915_private *dev_priv = dev->dev_private;
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03004956
4957 I915_WRITE(WM3_LP_ILK, 0);
4958 I915_WRITE(WM2_LP_ILK, 0);
4959 I915_WRITE(WM1_LP_ILK, 0);
4960
4961 /* According to the spec, bit 13 (RCZUNIT) must be set on IVB.
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01004962 * This implements the WaDisableRCZUnitClockGating:hsw workaround.
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03004963 */
4964 I915_WRITE(GEN6_UCGCTL2, GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
4965
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01004966 /* Apply the WaDisableRHWOOptimizationForRenderHang:hsw workaround. */
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03004967 I915_WRITE(GEN7_COMMON_SLICE_CHICKEN1,
4968 GEN7_CSC1_RHWO_OPT_DISABLE_IN_RCC);
4969
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01004970 /* WaApplyL3ControlAndL3ChickenMode:hsw */
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03004971 I915_WRITE(GEN7_L3CNTLREG1,
4972 GEN7_WA_FOR_GEN7_L3_CONTROL);
4973 I915_WRITE(GEN7_L3_CHICKEN_MODE_REGISTER,
4974 GEN7_WA_L3_CHICKEN_MODE);
4975
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01004976 /* This is required by WaCatErrorRejectionIssue:hsw */
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03004977 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
4978 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
4979 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
4980
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01004981 /* WaVSRefCountFullforceMissDisable:hsw */
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03004982 gen7_setup_fixed_func_scheduler(dev_priv);
4983
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01004984 /* WaDisable4x2SubspanOptimization:hsw */
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03004985 I915_WRITE(CACHE_MODE_1,
4986 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Eugeni Dodonov1544d9d2012-07-02 11:51:10 -03004987
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01004988 /* WaSwitchSolVfFArbitrationPriority:hsw */
Ben Widawskye3dff582013-03-20 14:49:14 -07004989 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
4990
Paulo Zanoni90a88642013-05-03 17:23:45 -03004991 /* WaRsPkgCStateDisplayPMReq:hsw */
4992 I915_WRITE(CHICKEN_PAR1_1,
4993 I915_READ(CHICKEN_PAR1_1) | FORCE_ARB_IDLE_PLANES);
Eugeni Dodonov1544d9d2012-07-02 11:51:10 -03004994
Paulo Zanoni17a303e2012-11-20 15:12:07 -02004995 lpt_init_clock_gating(dev);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03004996}
4997
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03004998static void ivybridge_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004999{
5000 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawsky20848222012-05-04 18:58:59 -07005001 uint32_t snpcr;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005002
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005003 I915_WRITE(WM3_LP_ILK, 0);
5004 I915_WRITE(WM2_LP_ILK, 0);
5005 I915_WRITE(WM1_LP_ILK, 0);
5006
Damien Lespiau231e54f2012-10-19 17:55:41 +01005007 I915_WRITE(ILK_DSPCLK_GATE_D, ILK_VRHUNIT_CLOCK_GATE_DISABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005008
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005009 /* WaDisableEarlyCull:ivb */
Jesse Barnes87f80202012-10-02 17:43:41 -05005010 I915_WRITE(_3D_CHICKEN3,
5011 _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
5012
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005013 /* WaDisableBackToBackFlipFix:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005014 I915_WRITE(IVB_CHICKEN3,
5015 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
5016 CHICKEN3_DGMG_DONE_FIX_DISABLE);
5017
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005018 /* WaDisablePSDDualDispatchEnable:ivb */
Jesse Barnes12f33822012-10-25 12:15:45 -07005019 if (IS_IVB_GT1(dev))
5020 I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
5021 _MASKED_BIT_ENABLE(GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
5022 else
5023 I915_WRITE(GEN7_HALF_SLICE_CHICKEN1_GT2,
5024 _MASKED_BIT_ENABLE(GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
5025
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005026 /* Apply the WaDisableRHWOOptimizationForRenderHang:ivb workaround. */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005027 I915_WRITE(GEN7_COMMON_SLICE_CHICKEN1,
5028 GEN7_CSC1_RHWO_OPT_DISABLE_IN_RCC);
5029
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005030 /* WaApplyL3ControlAndL3ChickenMode:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005031 I915_WRITE(GEN7_L3CNTLREG1,
5032 GEN7_WA_FOR_GEN7_L3_CONTROL);
5033 I915_WRITE(GEN7_L3_CHICKEN_MODE_REGISTER,
Jesse Barnes8ab43972012-10-25 12:15:42 -07005034 GEN7_WA_L3_CHICKEN_MODE);
5035 if (IS_IVB_GT1(dev))
5036 I915_WRITE(GEN7_ROW_CHICKEN2,
5037 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
5038 else
5039 I915_WRITE(GEN7_ROW_CHICKEN2_GT2,
5040 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
5041
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005042
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005043 /* WaForceL3Serialization:ivb */
Jesse Barnes61939d92012-10-02 17:43:38 -05005044 I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
5045 ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
5046
Jesse Barnes0f846f82012-06-14 11:04:47 -07005047 /* According to the BSpec vol1g, bit 12 (RCPBUNIT) clock
5048 * gating disable must be set. Failure to set it results in
5049 * flickering pixels due to Z write ordering failures after
5050 * some amount of runtime in the Mesa "fire" demo, and Unigine
5051 * Sanctuary and Tropics, and apparently anything else with
5052 * alpha test or pixel discard.
5053 *
5054 * According to the spec, bit 11 (RCCUNIT) must also be set,
5055 * but we didn't debug actual testcases to find it out.
5056 *
5057 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005058 * This implements the WaDisableRCZUnitClockGating:ivb workaround.
Jesse Barnes0f846f82012-06-14 11:04:47 -07005059 */
5060 I915_WRITE(GEN6_UCGCTL2,
5061 GEN6_RCZUNIT_CLOCK_GATE_DISABLE |
5062 GEN6_RCCUNIT_CLOCK_GATE_DISABLE);
5063
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005064 /* This is required by WaCatErrorRejectionIssue:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005065 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
5066 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
5067 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
5068
Ville Syrjälä0e088b82013-06-07 10:47:04 +03005069 g4x_disable_trickle_feed(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005070
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005071 /* WaVSRefCountFullforceMissDisable:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005072 gen7_setup_fixed_func_scheduler(dev_priv);
Daniel Vetter97e19302012-04-24 16:00:21 +02005073
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005074 /* WaDisable4x2SubspanOptimization:ivb */
Daniel Vetter97e19302012-04-24 16:00:21 +02005075 I915_WRITE(CACHE_MODE_1,
5076 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Ben Widawsky20848222012-05-04 18:58:59 -07005077
5078 snpcr = I915_READ(GEN6_MBCUNIT_SNPCR);
5079 snpcr &= ~GEN6_MBC_SNPCR_MASK;
5080 snpcr |= GEN6_MBC_SNPCR_MED;
5081 I915_WRITE(GEN6_MBCUNIT_SNPCR, snpcr);
Daniel Vetter3107bd42012-10-31 22:52:31 +01005082
Ben Widawskyab5c6082013-04-05 13:12:41 -07005083 if (!HAS_PCH_NOP(dev))
5084 cpt_init_clock_gating(dev);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01005085
5086 gen6_check_mch_setup(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005087}
5088
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005089static void valleyview_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005090{
5091 struct drm_i915_private *dev_priv = dev->dev_private;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005092
Ville Syrjäläd7fe0cc2013-05-21 18:01:50 +03005093 I915_WRITE(DSPCLK_GATE_D, VRHUNIT_CLOCK_GATE_DISABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005094
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005095 /* WaDisableEarlyCull:vlv */
Jesse Barnes87f80202012-10-02 17:43:41 -05005096 I915_WRITE(_3D_CHICKEN3,
5097 _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
5098
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005099 /* WaDisableBackToBackFlipFix:vlv */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005100 I915_WRITE(IVB_CHICKEN3,
5101 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
5102 CHICKEN3_DGMG_DONE_FIX_DISABLE);
5103
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005104 /* WaDisablePSDDualDispatchEnable:vlv */
Jesse Barnes12f33822012-10-25 12:15:45 -07005105 I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
Jesse Barnesd3bc0302013-03-08 10:45:51 -08005106 _MASKED_BIT_ENABLE(GEN7_MAX_PS_THREAD_DEP |
5107 GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
Jesse Barnes12f33822012-10-25 12:15:45 -07005108
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005109 /* Apply the WaDisableRHWOOptimizationForRenderHang:vlv workaround. */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005110 I915_WRITE(GEN7_COMMON_SLICE_CHICKEN1,
5111 GEN7_CSC1_RHWO_OPT_DISABLE_IN_RCC);
5112
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005113 /* WaApplyL3ControlAndL3ChickenMode:vlv */
Jesse Barnesd0cf5ea2012-10-25 12:15:41 -07005114 I915_WRITE(GEN7_L3CNTLREG1, I915_READ(GEN7_L3CNTLREG1) | GEN7_L3AGDIS);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005115 I915_WRITE(GEN7_L3_CHICKEN_MODE_REGISTER, GEN7_WA_L3_CHICKEN_MODE);
5116
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005117 /* WaForceL3Serialization:vlv */
Jesse Barnes61939d92012-10-02 17:43:38 -05005118 I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
5119 ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
5120
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005121 /* WaDisableDopClockGating:vlv */
Jesse Barnes8ab43972012-10-25 12:15:42 -07005122 I915_WRITE(GEN7_ROW_CHICKEN2,
5123 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
5124
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005125 /* This is required by WaCatErrorRejectionIssue:vlv */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005126 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
5127 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
5128 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
5129
Jesse Barnes0f846f82012-06-14 11:04:47 -07005130 /* According to the BSpec vol1g, bit 12 (RCPBUNIT) clock
5131 * gating disable must be set. Failure to set it results in
5132 * flickering pixels due to Z write ordering failures after
5133 * some amount of runtime in the Mesa "fire" demo, and Unigine
5134 * Sanctuary and Tropics, and apparently anything else with
5135 * alpha test or pixel discard.
5136 *
5137 * According to the spec, bit 11 (RCCUNIT) must also be set,
5138 * but we didn't debug actual testcases to find it out.
5139 *
5140 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005141 * This implements the WaDisableRCZUnitClockGating:vlv workaround.
Jesse Barnes0f846f82012-06-14 11:04:47 -07005142 *
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005143 * Also apply WaDisableVDSUnitClockGating:vlv and
5144 * WaDisableRCPBUnitClockGating:vlv.
Jesse Barnes0f846f82012-06-14 11:04:47 -07005145 */
5146 I915_WRITE(GEN6_UCGCTL2,
5147 GEN7_VDSUNIT_CLOCK_GATE_DISABLE |
Jesse Barnes6edaa7f2012-06-14 11:04:49 -07005148 GEN7_TDLUNIT_CLOCK_GATE_DISABLE |
Jesse Barnes0f846f82012-06-14 11:04:47 -07005149 GEN6_RCZUNIT_CLOCK_GATE_DISABLE |
5150 GEN6_RCPBUNIT_CLOCK_GATE_DISABLE |
5151 GEN6_RCCUNIT_CLOCK_GATE_DISABLE);
5152
Jesse Barnese3f33d42012-06-14 11:04:50 -07005153 I915_WRITE(GEN7_UCGCTL4, GEN7_L3BANK2X_CLOCK_GATE_DISABLE);
5154
Ville Syrjäläe0d8d592013-06-12 22:11:18 +03005155 I915_WRITE(MI_ARB_VLV, MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005156
Daniel Vetter6b26c862012-04-24 14:04:12 +02005157 I915_WRITE(CACHE_MODE_1,
5158 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Jesse Barnes79831172012-06-20 10:53:12 -07005159
5160 /*
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005161 * WaDisableVLVClockGating_VBIIssue:vlv
Jesse Barnes2d809572012-10-25 12:15:44 -07005162 * Disable clock gating on th GCFG unit to prevent a delay
5163 * in the reporting of vblank events.
5164 */
Jesse Barnes4e8c84a2013-03-08 10:45:54 -08005165 I915_WRITE(VLV_GUNIT_CLOCK_GATE, 0xffffffff);
5166
5167 /* Conservative clock gating settings for now */
5168 I915_WRITE(0x9400, 0xffffffff);
5169 I915_WRITE(0x9404, 0xffffffff);
5170 I915_WRITE(0x9408, 0xffffffff);
5171 I915_WRITE(0x940c, 0xffffffff);
5172 I915_WRITE(0x9410, 0xffffffff);
5173 I915_WRITE(0x9414, 0xffffffff);
5174 I915_WRITE(0x9418, 0xffffffff);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005175}
5176
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005177static void g4x_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005178{
5179 struct drm_i915_private *dev_priv = dev->dev_private;
5180 uint32_t dspclk_gate;
5181
5182 I915_WRITE(RENCLK_GATE_D1, 0);
5183 I915_WRITE(RENCLK_GATE_D2, VF_UNIT_CLOCK_GATE_DISABLE |
5184 GS_UNIT_CLOCK_GATE_DISABLE |
5185 CL_UNIT_CLOCK_GATE_DISABLE);
5186 I915_WRITE(RAMCLK_GATE_D, 0);
5187 dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE |
5188 OVRUNIT_CLOCK_GATE_DISABLE |
5189 OVCUNIT_CLOCK_GATE_DISABLE;
5190 if (IS_GM45(dev))
5191 dspclk_gate |= DSSUNIT_CLOCK_GATE_DISABLE;
5192 I915_WRITE(DSPCLK_GATE_D, dspclk_gate);
Daniel Vetter4358a372012-10-18 11:49:51 +02005193
5194 /* WaDisableRenderCachePipelinedFlush */
5195 I915_WRITE(CACHE_MODE_0,
5196 _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
Ville Syrjäläde1aa622013-06-07 10:47:01 +03005197
Ville Syrjälä0e088b82013-06-07 10:47:04 +03005198 g4x_disable_trickle_feed(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005199}
5200
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005201static void crestline_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005202{
5203 struct drm_i915_private *dev_priv = dev->dev_private;
5204
5205 I915_WRITE(RENCLK_GATE_D1, I965_RCC_CLOCK_GATE_DISABLE);
5206 I915_WRITE(RENCLK_GATE_D2, 0);
5207 I915_WRITE(DSPCLK_GATE_D, 0);
5208 I915_WRITE(RAMCLK_GATE_D, 0);
5209 I915_WRITE16(DEUC, 0);
Ville Syrjälä20f94962013-06-07 10:47:02 +03005210 I915_WRITE(MI_ARB_STATE,
5211 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005212}
5213
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005214static void broadwater_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005215{
5216 struct drm_i915_private *dev_priv = dev->dev_private;
5217
5218 I915_WRITE(RENCLK_GATE_D1, I965_RCZ_CLOCK_GATE_DISABLE |
5219 I965_RCC_CLOCK_GATE_DISABLE |
5220 I965_RCPB_CLOCK_GATE_DISABLE |
5221 I965_ISC_CLOCK_GATE_DISABLE |
5222 I965_FBC_CLOCK_GATE_DISABLE);
5223 I915_WRITE(RENCLK_GATE_D2, 0);
Ville Syrjälä20f94962013-06-07 10:47:02 +03005224 I915_WRITE(MI_ARB_STATE,
5225 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005226}
5227
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005228static void gen3_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005229{
5230 struct drm_i915_private *dev_priv = dev->dev_private;
5231 u32 dstate = I915_READ(D_STATE);
5232
5233 dstate |= DSTATE_PLL_D3_OFF | DSTATE_GFX_CLOCK_GATING |
5234 DSTATE_DOT_CLOCK_GATING;
5235 I915_WRITE(D_STATE, dstate);
Chris Wilson13a86b82012-04-24 14:51:43 +01005236
5237 if (IS_PINEVIEW(dev))
5238 I915_WRITE(ECOSKPD, _MASKED_BIT_ENABLE(ECO_GATING_CX_ONLY));
Daniel Vetter974a3b02012-09-09 11:54:16 +02005239
5240 /* IIR "flip pending" means done if this bit is set */
5241 I915_WRITE(ECOSKPD, _MASKED_BIT_DISABLE(ECO_FLIP_DONE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005242}
5243
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005244static void i85x_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005245{
5246 struct drm_i915_private *dev_priv = dev->dev_private;
5247
5248 I915_WRITE(RENCLK_GATE_D1, SV_CLOCK_GATE_DISABLE);
5249}
5250
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005251static void i830_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005252{
5253 struct drm_i915_private *dev_priv = dev->dev_private;
5254
5255 I915_WRITE(DSPCLK_GATE_D, OVRUNIT_CLOCK_GATE_DISABLE);
5256}
5257
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005258void intel_init_clock_gating(struct drm_device *dev)
5259{
5260 struct drm_i915_private *dev_priv = dev->dev_private;
5261
5262 dev_priv->display.init_clock_gating(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005263}
5264
Imre Deak7d708ee2013-04-17 14:04:50 +03005265void intel_suspend_hw(struct drm_device *dev)
5266{
5267 if (HAS_PCH_LPT(dev))
5268 lpt_suspend_hw(dev);
5269}
5270
Paulo Zanoni15d199e2013-03-22 14:14:13 -03005271/**
5272 * We should only use the power well if we explicitly asked the hardware to
5273 * enable it, so check if it's enabled and also check if we've requested it to
5274 * be enabled.
5275 */
Paulo Zanonib97186f2013-05-03 12:15:36 -03005276bool intel_display_power_enabled(struct drm_device *dev,
5277 enum intel_display_power_domain domain)
Paulo Zanoni15d199e2013-03-22 14:14:13 -03005278{
5279 struct drm_i915_private *dev_priv = dev->dev_private;
5280
Paulo Zanonib97186f2013-05-03 12:15:36 -03005281 if (!HAS_POWER_WELL(dev))
5282 return true;
5283
5284 switch (domain) {
5285 case POWER_DOMAIN_PIPE_A:
5286 case POWER_DOMAIN_TRANSCODER_EDP:
5287 return true;
5288 case POWER_DOMAIN_PIPE_B:
5289 case POWER_DOMAIN_PIPE_C:
5290 case POWER_DOMAIN_PIPE_A_PANEL_FITTER:
5291 case POWER_DOMAIN_PIPE_B_PANEL_FITTER:
5292 case POWER_DOMAIN_PIPE_C_PANEL_FITTER:
5293 case POWER_DOMAIN_TRANSCODER_A:
5294 case POWER_DOMAIN_TRANSCODER_B:
5295 case POWER_DOMAIN_TRANSCODER_C:
Paulo Zanoni15d199e2013-03-22 14:14:13 -03005296 return I915_READ(HSW_PWR_WELL_DRIVER) ==
Paulo Zanoni6aedd1f2013-08-02 16:22:25 -03005297 (HSW_PWR_WELL_ENABLE_REQUEST | HSW_PWR_WELL_STATE_ENABLED);
Paulo Zanonib97186f2013-05-03 12:15:36 -03005298 default:
5299 BUG();
5300 }
Paulo Zanoni15d199e2013-03-22 14:14:13 -03005301}
5302
Wang Xingchaoa38911a2013-05-30 22:07:11 +08005303static void __intel_set_power_well(struct drm_device *dev, bool enable)
Eugeni Dodonovd0d3e512012-05-09 15:37:16 -03005304{
5305 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanonifa42e232013-01-25 16:59:11 -02005306 bool is_enabled, enable_requested;
5307 uint32_t tmp;
Eugeni Dodonovd0d3e512012-05-09 15:37:16 -03005308
Paulo Zanonifa42e232013-01-25 16:59:11 -02005309 tmp = I915_READ(HSW_PWR_WELL_DRIVER);
Paulo Zanoni6aedd1f2013-08-02 16:22:25 -03005310 is_enabled = tmp & HSW_PWR_WELL_STATE_ENABLED;
5311 enable_requested = tmp & HSW_PWR_WELL_ENABLE_REQUEST;
Eugeni Dodonovd0d3e512012-05-09 15:37:16 -03005312
Paulo Zanonifa42e232013-01-25 16:59:11 -02005313 if (enable) {
5314 if (!enable_requested)
Paulo Zanoni6aedd1f2013-08-02 16:22:25 -03005315 I915_WRITE(HSW_PWR_WELL_DRIVER,
5316 HSW_PWR_WELL_ENABLE_REQUEST);
Eugeni Dodonovd0d3e512012-05-09 15:37:16 -03005317
Paulo Zanonifa42e232013-01-25 16:59:11 -02005318 if (!is_enabled) {
5319 DRM_DEBUG_KMS("Enabling power well\n");
5320 if (wait_for((I915_READ(HSW_PWR_WELL_DRIVER) &
Paulo Zanoni6aedd1f2013-08-02 16:22:25 -03005321 HSW_PWR_WELL_STATE_ENABLED), 20))
Paulo Zanonifa42e232013-01-25 16:59:11 -02005322 DRM_ERROR("Timeout enabling power well\n");
5323 }
5324 } else {
5325 if (enable_requested) {
Paulo Zanoni9dbd8fe2013-07-23 10:48:11 -03005326 unsigned long irqflags;
5327 enum pipe p;
5328
Paulo Zanonifa42e232013-01-25 16:59:11 -02005329 I915_WRITE(HSW_PWR_WELL_DRIVER, 0);
Paulo Zanoni9dbd8fe2013-07-23 10:48:11 -03005330 POSTING_READ(HSW_PWR_WELL_DRIVER);
Paulo Zanonifa42e232013-01-25 16:59:11 -02005331 DRM_DEBUG_KMS("Requesting to disable the power well\n");
Paulo Zanoni9dbd8fe2013-07-23 10:48:11 -03005332
5333 /*
5334 * After this, the registers on the pipes that are part
5335 * of the power well will become zero, so we have to
5336 * adjust our counters according to that.
5337 *
5338 * FIXME: Should we do this in general in
5339 * drm_vblank_post_modeset?
5340 */
5341 spin_lock_irqsave(&dev->vbl_lock, irqflags);
5342 for_each_pipe(p)
5343 if (p != PIPE_A)
5344 dev->last_vblank[p] = 0;
5345 spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
Eugeni Dodonovd0d3e512012-05-09 15:37:16 -03005346 }
5347 }
Paulo Zanonifa42e232013-01-25 16:59:11 -02005348}
Eugeni Dodonovd0d3e512012-05-09 15:37:16 -03005349
Wang Xingchaoa38911a2013-05-30 22:07:11 +08005350static struct i915_power_well *hsw_pwr;
5351
5352/* Display audio driver power well request */
5353void i915_request_power_well(void)
5354{
5355 if (WARN_ON(!hsw_pwr))
5356 return;
5357
5358 spin_lock_irq(&hsw_pwr->lock);
5359 if (!hsw_pwr->count++ &&
5360 !hsw_pwr->i915_request)
5361 __intel_set_power_well(hsw_pwr->device, true);
5362 spin_unlock_irq(&hsw_pwr->lock);
5363}
5364EXPORT_SYMBOL_GPL(i915_request_power_well);
5365
5366/* Display audio driver power well release */
5367void i915_release_power_well(void)
5368{
5369 if (WARN_ON(!hsw_pwr))
5370 return;
5371
5372 spin_lock_irq(&hsw_pwr->lock);
5373 WARN_ON(!hsw_pwr->count);
5374 if (!--hsw_pwr->count &&
5375 !hsw_pwr->i915_request)
5376 __intel_set_power_well(hsw_pwr->device, false);
5377 spin_unlock_irq(&hsw_pwr->lock);
5378}
5379EXPORT_SYMBOL_GPL(i915_release_power_well);
5380
5381int i915_init_power_well(struct drm_device *dev)
5382{
5383 struct drm_i915_private *dev_priv = dev->dev_private;
5384
5385 hsw_pwr = &dev_priv->power_well;
5386
5387 hsw_pwr->device = dev;
5388 spin_lock_init(&hsw_pwr->lock);
5389 hsw_pwr->count = 0;
5390
5391 return 0;
5392}
5393
5394void i915_remove_power_well(struct drm_device *dev)
5395{
5396 hsw_pwr = NULL;
5397}
5398
5399void intel_set_power_well(struct drm_device *dev, bool enable)
5400{
5401 struct drm_i915_private *dev_priv = dev->dev_private;
5402 struct i915_power_well *power_well = &dev_priv->power_well;
5403
5404 if (!HAS_POWER_WELL(dev))
5405 return;
5406
5407 if (!i915_disable_power_well && !enable)
5408 return;
5409
5410 spin_lock_irq(&power_well->lock);
5411 power_well->i915_request = enable;
5412
5413 /* only reject "disable" power well request */
5414 if (power_well->count && !enable) {
5415 spin_unlock_irq(&power_well->lock);
5416 return;
5417 }
5418
5419 __intel_set_power_well(dev, enable);
5420 spin_unlock_irq(&power_well->lock);
5421}
5422
Paulo Zanonifa42e232013-01-25 16:59:11 -02005423/*
5424 * Starting with Haswell, we have a "Power Down Well" that can be turned off
5425 * when not needed anymore. We have 4 registers that can request the power well
5426 * to be enabled, and it will only be disabled if none of the registers is
5427 * requesting it to be enabled.
5428 */
5429void intel_init_power_well(struct drm_device *dev)
5430{
5431 struct drm_i915_private *dev_priv = dev->dev_private;
Eugeni Dodonovd0d3e512012-05-09 15:37:16 -03005432
Paulo Zanoni86d52df2013-03-06 20:03:18 -03005433 if (!HAS_POWER_WELL(dev))
Eugeni Dodonovd0d3e512012-05-09 15:37:16 -03005434 return;
5435
Paulo Zanonifa42e232013-01-25 16:59:11 -02005436 /* For now, we need the power well to be always enabled. */
5437 intel_set_power_well(dev, true);
Eugeni Dodonovd0d3e512012-05-09 15:37:16 -03005438
Paulo Zanonifa42e232013-01-25 16:59:11 -02005439 /* We're taking over the BIOS, so clear any requests made by it since
5440 * the driver is in charge now. */
Paulo Zanoni6aedd1f2013-08-02 16:22:25 -03005441 if (I915_READ(HSW_PWR_WELL_BIOS) & HSW_PWR_WELL_ENABLE_REQUEST)
Paulo Zanonifa42e232013-01-25 16:59:11 -02005442 I915_WRITE(HSW_PWR_WELL_BIOS, 0);
Eugeni Dodonovd0d3e512012-05-09 15:37:16 -03005443}
5444
Paulo Zanonic67a4702013-08-19 13:18:09 -03005445/* Disables PC8 so we can use the GMBUS and DP AUX interrupts. */
5446void intel_aux_display_runtime_get(struct drm_i915_private *dev_priv)
5447{
5448 hsw_disable_package_c8(dev_priv);
5449}
5450
5451void intel_aux_display_runtime_put(struct drm_i915_private *dev_priv)
5452{
5453 hsw_enable_package_c8(dev_priv);
5454}
5455
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005456/* Set up chip specific power management-related functions */
5457void intel_init_pm(struct drm_device *dev)
5458{
5459 struct drm_i915_private *dev_priv = dev->dev_private;
5460
5461 if (I915_HAS_FBC(dev)) {
5462 if (HAS_PCH_SPLIT(dev)) {
5463 dev_priv->display.fbc_enabled = ironlake_fbc_enabled;
Rodrigo Vivi891348b2013-05-06 19:37:36 -03005464 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
Rodrigo Viviabe959c2013-05-06 19:37:33 -03005465 dev_priv->display.enable_fbc =
5466 gen7_enable_fbc;
5467 else
5468 dev_priv->display.enable_fbc =
5469 ironlake_enable_fbc;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005470 dev_priv->display.disable_fbc = ironlake_disable_fbc;
5471 } else if (IS_GM45(dev)) {
5472 dev_priv->display.fbc_enabled = g4x_fbc_enabled;
5473 dev_priv->display.enable_fbc = g4x_enable_fbc;
5474 dev_priv->display.disable_fbc = g4x_disable_fbc;
5475 } else if (IS_CRESTLINE(dev)) {
5476 dev_priv->display.fbc_enabled = i8xx_fbc_enabled;
5477 dev_priv->display.enable_fbc = i8xx_enable_fbc;
5478 dev_priv->display.disable_fbc = i8xx_disable_fbc;
5479 }
5480 /* 855GM needs testing */
5481 }
5482
Daniel Vetterc921aba2012-04-26 23:28:17 +02005483 /* For cxsr */
5484 if (IS_PINEVIEW(dev))
5485 i915_pineview_get_mem_freq(dev);
5486 else if (IS_GEN5(dev))
5487 i915_ironlake_get_mem_freq(dev);
5488
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005489 /* For FIFO watermark updates */
5490 if (HAS_PCH_SPLIT(dev)) {
Ville Syrjälä53615a52013-08-01 16:18:50 +03005491 intel_setup_wm_latency(dev);
5492
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005493 if (IS_GEN5(dev)) {
Ville Syrjälä53615a52013-08-01 16:18:50 +03005494 if (dev_priv->wm.pri_latency[1] &&
5495 dev_priv->wm.spr_latency[1] &&
5496 dev_priv->wm.cur_latency[1])
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005497 dev_priv->display.update_wm = ironlake_update_wm;
5498 else {
5499 DRM_DEBUG_KMS("Failed to get proper latency. "
5500 "Disable CxSR\n");
5501 dev_priv->display.update_wm = NULL;
5502 }
5503 dev_priv->display.init_clock_gating = ironlake_init_clock_gating;
5504 } else if (IS_GEN6(dev)) {
Ville Syrjälä53615a52013-08-01 16:18:50 +03005505 if (dev_priv->wm.pri_latency[0] &&
5506 dev_priv->wm.spr_latency[0] &&
5507 dev_priv->wm.cur_latency[0]) {
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005508 dev_priv->display.update_wm = sandybridge_update_wm;
5509 dev_priv->display.update_sprite_wm = sandybridge_update_sprite_wm;
5510 } else {
5511 DRM_DEBUG_KMS("Failed to read display plane latency. "
5512 "Disable CxSR\n");
5513 dev_priv->display.update_wm = NULL;
5514 }
5515 dev_priv->display.init_clock_gating = gen6_init_clock_gating;
5516 } else if (IS_IVYBRIDGE(dev)) {
Ville Syrjälä53615a52013-08-01 16:18:50 +03005517 if (dev_priv->wm.pri_latency[0] &&
5518 dev_priv->wm.spr_latency[0] &&
5519 dev_priv->wm.cur_latency[0]) {
Chris Wilsonc43d0182012-12-11 12:01:42 +00005520 dev_priv->display.update_wm = ivybridge_update_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005521 dev_priv->display.update_sprite_wm = sandybridge_update_sprite_wm;
5522 } else {
5523 DRM_DEBUG_KMS("Failed to read display plane latency. "
5524 "Disable CxSR\n");
5525 dev_priv->display.update_wm = NULL;
5526 }
5527 dev_priv->display.init_clock_gating = ivybridge_init_clock_gating;
Eugeni Dodonov6b8a5ee2012-05-09 15:37:23 -03005528 } else if (IS_HASWELL(dev)) {
Ville Syrjälä53615a52013-08-01 16:18:50 +03005529 if (dev_priv->wm.pri_latency[0] &&
5530 dev_priv->wm.spr_latency[0] &&
5531 dev_priv->wm.cur_latency[0]) {
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03005532 dev_priv->display.update_wm = haswell_update_wm;
Paulo Zanoni526682e2013-05-24 11:59:18 -03005533 dev_priv->display.update_sprite_wm =
5534 haswell_update_sprite_wm;
Eugeni Dodonov6b8a5ee2012-05-09 15:37:23 -03005535 } else {
5536 DRM_DEBUG_KMS("Failed to read display plane latency. "
5537 "Disable CxSR\n");
5538 dev_priv->display.update_wm = NULL;
5539 }
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03005540 dev_priv->display.init_clock_gating = haswell_init_clock_gating;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005541 } else
5542 dev_priv->display.update_wm = NULL;
5543 } else if (IS_VALLEYVIEW(dev)) {
5544 dev_priv->display.update_wm = valleyview_update_wm;
5545 dev_priv->display.init_clock_gating =
5546 valleyview_init_clock_gating;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005547 } else if (IS_PINEVIEW(dev)) {
5548 if (!intel_get_cxsr_latency(IS_PINEVIEW_G(dev),
5549 dev_priv->is_ddr3,
5550 dev_priv->fsb_freq,
5551 dev_priv->mem_freq)) {
5552 DRM_INFO("failed to find known CxSR latency "
5553 "(found ddr%s fsb freq %d, mem freq %d), "
5554 "disabling CxSR\n",
5555 (dev_priv->is_ddr3 == 1) ? "3" : "2",
5556 dev_priv->fsb_freq, dev_priv->mem_freq);
5557 /* Disable CxSR and never update its watermark again */
5558 pineview_disable_cxsr(dev);
5559 dev_priv->display.update_wm = NULL;
5560 } else
5561 dev_priv->display.update_wm = pineview_update_wm;
5562 dev_priv->display.init_clock_gating = gen3_init_clock_gating;
5563 } else if (IS_G4X(dev)) {
5564 dev_priv->display.update_wm = g4x_update_wm;
5565 dev_priv->display.init_clock_gating = g4x_init_clock_gating;
5566 } else if (IS_GEN4(dev)) {
5567 dev_priv->display.update_wm = i965_update_wm;
5568 if (IS_CRESTLINE(dev))
5569 dev_priv->display.init_clock_gating = crestline_init_clock_gating;
5570 else if (IS_BROADWATER(dev))
5571 dev_priv->display.init_clock_gating = broadwater_init_clock_gating;
5572 } else if (IS_GEN3(dev)) {
5573 dev_priv->display.update_wm = i9xx_update_wm;
5574 dev_priv->display.get_fifo_size = i9xx_get_fifo_size;
5575 dev_priv->display.init_clock_gating = gen3_init_clock_gating;
5576 } else if (IS_I865G(dev)) {
5577 dev_priv->display.update_wm = i830_update_wm;
5578 dev_priv->display.init_clock_gating = i85x_init_clock_gating;
5579 dev_priv->display.get_fifo_size = i830_get_fifo_size;
5580 } else if (IS_I85X(dev)) {
5581 dev_priv->display.update_wm = i9xx_update_wm;
5582 dev_priv->display.get_fifo_size = i85x_get_fifo_size;
5583 dev_priv->display.init_clock_gating = i85x_init_clock_gating;
5584 } else {
5585 dev_priv->display.update_wm = i830_update_wm;
5586 dev_priv->display.init_clock_gating = i830_init_clock_gating;
5587 if (IS_845G(dev))
5588 dev_priv->display.get_fifo_size = i845_get_fifo_size;
5589 else
5590 dev_priv->display.get_fifo_size = i830_get_fifo_size;
5591 }
5592}
5593
Ben Widawsky42c05262012-09-26 10:34:00 -07005594int sandybridge_pcode_read(struct drm_i915_private *dev_priv, u8 mbox, u32 *val)
5595{
Jesse Barnes4fc688c2012-11-02 11:14:01 -07005596 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Ben Widawsky42c05262012-09-26 10:34:00 -07005597
5598 if (I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) {
5599 DRM_DEBUG_DRIVER("warning: pcode (read) mailbox access failed\n");
5600 return -EAGAIN;
5601 }
5602
5603 I915_WRITE(GEN6_PCODE_DATA, *val);
5604 I915_WRITE(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY | mbox);
5605
5606 if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
5607 500)) {
5608 DRM_ERROR("timeout waiting for pcode read (%d) to finish\n", mbox);
5609 return -ETIMEDOUT;
5610 }
5611
5612 *val = I915_READ(GEN6_PCODE_DATA);
5613 I915_WRITE(GEN6_PCODE_DATA, 0);
5614
5615 return 0;
5616}
5617
5618int sandybridge_pcode_write(struct drm_i915_private *dev_priv, u8 mbox, u32 val)
5619{
Jesse Barnes4fc688c2012-11-02 11:14:01 -07005620 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Ben Widawsky42c05262012-09-26 10:34:00 -07005621
5622 if (I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) {
5623 DRM_DEBUG_DRIVER("warning: pcode (write) mailbox access failed\n");
5624 return -EAGAIN;
5625 }
5626
5627 I915_WRITE(GEN6_PCODE_DATA, val);
5628 I915_WRITE(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY | mbox);
5629
5630 if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
5631 500)) {
5632 DRM_ERROR("timeout waiting for pcode write (%d) to finish\n", mbox);
5633 return -ETIMEDOUT;
5634 }
5635
5636 I915_WRITE(GEN6_PCODE_DATA, 0);
5637
5638 return 0;
5639}
Jesse Barnesa0e4e192013-04-02 11:23:05 -07005640
Jesse Barnes855ba3b2013-04-17 15:54:57 -07005641int vlv_gpu_freq(int ddr_freq, int val)
5642{
5643 int mult, base;
5644
5645 switch (ddr_freq) {
5646 case 800:
5647 mult = 20;
5648 base = 120;
5649 break;
5650 case 1066:
5651 mult = 22;
5652 base = 133;
5653 break;
5654 case 1333:
5655 mult = 21;
5656 base = 125;
5657 break;
5658 default:
5659 return -1;
5660 }
5661
5662 return ((val - 0xbd) * mult) + base;
5663}
5664
5665int vlv_freq_opcode(int ddr_freq, int val)
5666{
5667 int mult, base;
5668
5669 switch (ddr_freq) {
5670 case 800:
5671 mult = 20;
5672 base = 120;
5673 break;
5674 case 1066:
5675 mult = 22;
5676 base = 133;
5677 break;
5678 case 1333:
5679 mult = 21;
5680 base = 125;
5681 break;
5682 default:
5683 return -1;
5684 }
5685
5686 val /= mult;
5687 val -= base / mult;
5688 val += 0xbd;
5689
5690 if (val > 0xea)
5691 val = 0xea;
5692
5693 return val;
5694}
5695
Chris Wilson907b28c2013-07-19 20:36:52 +01005696void intel_pm_init(struct drm_device *dev)
5697{
5698 struct drm_i915_private *dev_priv = dev->dev_private;
5699
5700 INIT_DELAYED_WORK(&dev_priv->rps.delayed_resume_work,
5701 intel_gen6_powersave_work);
5702}
5703