blob: 5fc1cc9cd1d3d7501bcca9b04128f826af558c8d [file] [log] [blame]
Eugeni Dodonov85208be2012-04-16 22:20:34 -03001/*
2 * Copyright © 2012 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eugeni Dodonov <eugeni.dodonov@intel.com>
25 *
26 */
27
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -030028#include <linux/cpufreq.h>
Eugeni Dodonov85208be2012-04-16 22:20:34 -030029#include "i915_drv.h"
30#include "intel_drv.h"
Daniel Vettereb48eb02012-04-26 23:28:12 +020031#include "../../../platform/x86/intel_ips.h"
32#include <linux/module.h>
Paulo Zanonif9dcb0d2013-12-11 18:50:10 -020033#include <linux/vgaarb.h>
Damien Lespiauf4db9322013-06-24 22:59:50 +010034#include <drm/i915_powerwell.h>
Paulo Zanoni8a187452013-12-06 20:32:13 -020035#include <linux/pm_runtime.h>
Eugeni Dodonov85208be2012-04-16 22:20:34 -030036
Ben Widawskydc39fff2013-10-18 12:32:07 -070037/**
38 * RC6 is a special power stage which allows the GPU to enter an very
39 * low-voltage mode when idle, using down to 0V while at this stage. This
40 * stage is entered automatically when the GPU is idle when RC6 support is
41 * enabled, and as soon as new workload arises GPU wakes up automatically as well.
42 *
43 * There are different RC6 modes available in Intel GPU, which differentiate
44 * among each other with the latency required to enter and leave RC6 and
45 * voltage consumed by the GPU in different states.
46 *
47 * The combination of the following flags define which states GPU is allowed
48 * to enter, while RC6 is the normal RC6 state, RC6p is the deep RC6, and
49 * RC6pp is deepest RC6. Their support by hardware varies according to the
50 * GPU, BIOS, chipset and platform. RC6 is usually the safest one and the one
51 * which brings the most power savings; deeper states save more power, but
52 * require higher latency to switch to and wake up.
53 */
54#define INTEL_RC6_ENABLE (1<<0)
55#define INTEL_RC6p_ENABLE (1<<1)
56#define INTEL_RC6pp_ENABLE (1<<2)
57
Eugeni Dodonovf6750b32012-04-18 11:51:14 -030058/* FBC, or Frame Buffer Compression, is a technique employed to compress the
59 * framebuffer contents in-memory, aiming at reducing the required bandwidth
60 * during in-memory transfers and, therefore, reduce the power packet.
Eugeni Dodonov85208be2012-04-16 22:20:34 -030061 *
Eugeni Dodonovf6750b32012-04-18 11:51:14 -030062 * The benefits of FBC are mostly visible with solid backgrounds and
63 * variation-less patterns.
Eugeni Dodonov85208be2012-04-16 22:20:34 -030064 *
Eugeni Dodonovf6750b32012-04-18 11:51:14 -030065 * FBC-related functionality can be enabled by the means of the
66 * i915.i915_enable_fbc parameter
Eugeni Dodonov85208be2012-04-16 22:20:34 -030067 */
68
Eugeni Dodonov1fa61102012-04-18 15:29:26 -030069static void i8xx_disable_fbc(struct drm_device *dev)
Eugeni Dodonov85208be2012-04-16 22:20:34 -030070{
71 struct drm_i915_private *dev_priv = dev->dev_private;
72 u32 fbc_ctl;
73
74 /* Disable compression */
75 fbc_ctl = I915_READ(FBC_CONTROL);
76 if ((fbc_ctl & FBC_CTL_EN) == 0)
77 return;
78
79 fbc_ctl &= ~FBC_CTL_EN;
80 I915_WRITE(FBC_CONTROL, fbc_ctl);
81
82 /* Wait for compressing bit to clear */
83 if (wait_for((I915_READ(FBC_STATUS) & FBC_STAT_COMPRESSING) == 0, 10)) {
84 DRM_DEBUG_KMS("FBC idle timed out\n");
85 return;
86 }
87
88 DRM_DEBUG_KMS("disabled FBC\n");
89}
90
Ville Syrjälä993495a2013-12-12 17:27:40 +020091static void i8xx_enable_fbc(struct drm_crtc *crtc)
Eugeni Dodonov85208be2012-04-16 22:20:34 -030092{
93 struct drm_device *dev = crtc->dev;
94 struct drm_i915_private *dev_priv = dev->dev_private;
95 struct drm_framebuffer *fb = crtc->fb;
96 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
97 struct drm_i915_gem_object *obj = intel_fb->obj;
98 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
99 int cfb_pitch;
100 int plane, i;
Ville Syrjälä159f9872013-11-28 17:29:57 +0200101 u32 fbc_ctl;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300102
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -0700103 cfb_pitch = dev_priv->fbc.size / FBC_LL_SIZE;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300104 if (fb->pitches[0] < cfb_pitch)
105 cfb_pitch = fb->pitches[0];
106
Ville Syrjälä42a430f2013-11-28 17:29:56 +0200107 /* FBC_CTL wants 32B or 64B units */
108 if (IS_GEN2(dev))
109 cfb_pitch = (cfb_pitch / 32) - 1;
110 else
111 cfb_pitch = (cfb_pitch / 64) - 1;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300112 plane = intel_crtc->plane == 0 ? FBC_CTL_PLANEA : FBC_CTL_PLANEB;
113
114 /* Clear old tags */
115 for (i = 0; i < (FBC_LL_SIZE / 32) + 1; i++)
116 I915_WRITE(FBC_TAG + (i * 4), 0);
117
Ville Syrjälä159f9872013-11-28 17:29:57 +0200118 if (IS_GEN4(dev)) {
119 u32 fbc_ctl2;
120
121 /* Set it up... */
122 fbc_ctl2 = FBC_CTL_FENCE_DBL | FBC_CTL_IDLE_IMM | FBC_CTL_CPU_FENCE;
123 fbc_ctl2 |= plane;
124 I915_WRITE(FBC_CONTROL2, fbc_ctl2);
125 I915_WRITE(FBC_FENCE_OFF, crtc->y);
126 }
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300127
128 /* enable it... */
Ville Syrjälä993495a2013-12-12 17:27:40 +0200129 fbc_ctl = I915_READ(FBC_CONTROL);
130 fbc_ctl &= 0x3fff << FBC_CTL_INTERVAL_SHIFT;
131 fbc_ctl |= FBC_CTL_EN | FBC_CTL_PERIODIC;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300132 if (IS_I945GM(dev))
133 fbc_ctl |= FBC_CTL_C3_IDLE; /* 945 needs special SR handling */
134 fbc_ctl |= (cfb_pitch & 0xff) << FBC_CTL_STRIDE_SHIFT;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300135 fbc_ctl |= obj->fence_reg;
136 I915_WRITE(FBC_CONTROL, fbc_ctl);
137
Ville Syrjälä84f44ce2013-04-17 17:48:49 +0300138 DRM_DEBUG_KMS("enabled FBC, pitch %d, yoff %d, plane %c, ",
139 cfb_pitch, crtc->y, plane_name(intel_crtc->plane));
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300140}
141
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300142static bool i8xx_fbc_enabled(struct drm_device *dev)
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300143{
144 struct drm_i915_private *dev_priv = dev->dev_private;
145
146 return I915_READ(FBC_CONTROL) & FBC_CTL_EN;
147}
148
Ville Syrjälä993495a2013-12-12 17:27:40 +0200149static void g4x_enable_fbc(struct drm_crtc *crtc)
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300150{
151 struct drm_device *dev = crtc->dev;
152 struct drm_i915_private *dev_priv = dev->dev_private;
153 struct drm_framebuffer *fb = crtc->fb;
154 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
155 struct drm_i915_gem_object *obj = intel_fb->obj;
156 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
157 int plane = intel_crtc->plane == 0 ? DPFC_CTL_PLANEA : DPFC_CTL_PLANEB;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300158 u32 dpfc_ctl;
159
160 dpfc_ctl = plane | DPFC_SR_EN | DPFC_CTL_LIMIT_1X;
161 dpfc_ctl |= DPFC_CTL_FENCE_EN | obj->fence_reg;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300162
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300163 I915_WRITE(DPFC_FENCE_YOFF, crtc->y);
164
165 /* enable it... */
166 I915_WRITE(DPFC_CONTROL, I915_READ(DPFC_CONTROL) | DPFC_CTL_EN);
167
Ville Syrjälä84f44ce2013-04-17 17:48:49 +0300168 DRM_DEBUG_KMS("enabled fbc on plane %c\n", plane_name(intel_crtc->plane));
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300169}
170
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300171static void g4x_disable_fbc(struct drm_device *dev)
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300172{
173 struct drm_i915_private *dev_priv = dev->dev_private;
174 u32 dpfc_ctl;
175
176 /* Disable compression */
177 dpfc_ctl = I915_READ(DPFC_CONTROL);
178 if (dpfc_ctl & DPFC_CTL_EN) {
179 dpfc_ctl &= ~DPFC_CTL_EN;
180 I915_WRITE(DPFC_CONTROL, dpfc_ctl);
181
182 DRM_DEBUG_KMS("disabled FBC\n");
183 }
184}
185
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300186static bool g4x_fbc_enabled(struct drm_device *dev)
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300187{
188 struct drm_i915_private *dev_priv = dev->dev_private;
189
190 return I915_READ(DPFC_CONTROL) & DPFC_CTL_EN;
191}
192
193static void sandybridge_blit_fbc_update(struct drm_device *dev)
194{
195 struct drm_i915_private *dev_priv = dev->dev_private;
196 u32 blt_ecoskpd;
197
198 /* Make sure blitter notifies FBC of writes */
Deepak S940aece2013-11-23 14:55:43 +0530199
200 /* Blitter is part of Media powerwell on VLV. No impact of
201 * his param in other platforms for now */
202 gen6_gt_force_wake_get(dev_priv, FORCEWAKE_MEDIA);
Deepak Sc8d9a592013-11-23 14:55:42 +0530203
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300204 blt_ecoskpd = I915_READ(GEN6_BLITTER_ECOSKPD);
205 blt_ecoskpd |= GEN6_BLITTER_FBC_NOTIFY <<
206 GEN6_BLITTER_LOCK_SHIFT;
207 I915_WRITE(GEN6_BLITTER_ECOSKPD, blt_ecoskpd);
208 blt_ecoskpd |= GEN6_BLITTER_FBC_NOTIFY;
209 I915_WRITE(GEN6_BLITTER_ECOSKPD, blt_ecoskpd);
210 blt_ecoskpd &= ~(GEN6_BLITTER_FBC_NOTIFY <<
211 GEN6_BLITTER_LOCK_SHIFT);
212 I915_WRITE(GEN6_BLITTER_ECOSKPD, blt_ecoskpd);
213 POSTING_READ(GEN6_BLITTER_ECOSKPD);
Deepak Sc8d9a592013-11-23 14:55:42 +0530214
Deepak S940aece2013-11-23 14:55:43 +0530215 gen6_gt_force_wake_put(dev_priv, FORCEWAKE_MEDIA);
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300216}
217
Ville Syrjälä993495a2013-12-12 17:27:40 +0200218static void ironlake_enable_fbc(struct drm_crtc *crtc)
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300219{
220 struct drm_device *dev = crtc->dev;
221 struct drm_i915_private *dev_priv = dev->dev_private;
222 struct drm_framebuffer *fb = crtc->fb;
223 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
224 struct drm_i915_gem_object *obj = intel_fb->obj;
225 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
226 int plane = intel_crtc->plane == 0 ? DPFC_CTL_PLANEA : DPFC_CTL_PLANEB;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300227 u32 dpfc_ctl;
228
229 dpfc_ctl = I915_READ(ILK_DPFC_CONTROL);
230 dpfc_ctl &= DPFC_RESERVED;
231 dpfc_ctl |= (plane | DPFC_CTL_LIMIT_1X);
Ville Syrjäläd6293362013-11-21 21:29:45 +0200232 dpfc_ctl |= DPFC_CTL_FENCE_EN;
233 if (IS_GEN5(dev))
234 dpfc_ctl |= obj->fence_reg;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300235
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300236 I915_WRITE(ILK_DPFC_FENCE_YOFF, crtc->y);
Ben Widawskyf343c5f2013-07-05 14:41:04 -0700237 I915_WRITE(ILK_FBC_RT_BASE, i915_gem_obj_ggtt_offset(obj) | ILK_FBC_RT_VALID);
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300238 /* enable it... */
239 I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl | DPFC_CTL_EN);
240
241 if (IS_GEN6(dev)) {
242 I915_WRITE(SNB_DPFC_CTL_SA,
243 SNB_CPU_FENCE_ENABLE | obj->fence_reg);
244 I915_WRITE(DPFC_CPU_FENCE_OFFSET, crtc->y);
245 sandybridge_blit_fbc_update(dev);
246 }
247
Ville Syrjälä84f44ce2013-04-17 17:48:49 +0300248 DRM_DEBUG_KMS("enabled fbc on plane %c\n", plane_name(intel_crtc->plane));
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300249}
250
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300251static void ironlake_disable_fbc(struct drm_device *dev)
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300252{
253 struct drm_i915_private *dev_priv = dev->dev_private;
254 u32 dpfc_ctl;
255
256 /* Disable compression */
257 dpfc_ctl = I915_READ(ILK_DPFC_CONTROL);
258 if (dpfc_ctl & DPFC_CTL_EN) {
259 dpfc_ctl &= ~DPFC_CTL_EN;
260 I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl);
261
262 DRM_DEBUG_KMS("disabled FBC\n");
263 }
264}
265
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300266static bool ironlake_fbc_enabled(struct drm_device *dev)
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300267{
268 struct drm_i915_private *dev_priv = dev->dev_private;
269
270 return I915_READ(ILK_DPFC_CONTROL) & DPFC_CTL_EN;
271}
272
Ville Syrjälä993495a2013-12-12 17:27:40 +0200273static void gen7_enable_fbc(struct drm_crtc *crtc)
Rodrigo Viviabe959c2013-05-06 19:37:33 -0300274{
275 struct drm_device *dev = crtc->dev;
276 struct drm_i915_private *dev_priv = dev->dev_private;
277 struct drm_framebuffer *fb = crtc->fb;
278 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
279 struct drm_i915_gem_object *obj = intel_fb->obj;
280 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
281
Rodrigo Viviabe959c2013-05-06 19:37:33 -0300282 I915_WRITE(ILK_DPFC_CONTROL, DPFC_CTL_EN | DPFC_CTL_LIMIT_1X |
283 IVB_DPFC_CTL_FENCE_EN |
284 intel_crtc->plane << IVB_DPFC_CTL_PLANE_SHIFT);
285
Rodrigo Vivi891348b2013-05-06 19:37:36 -0300286 if (IS_IVYBRIDGE(dev)) {
Damien Lespiau7dd23ba2013-05-10 14:33:17 +0100287 /* WaFbcAsynchFlipDisableFbcQueue:ivb */
Rodrigo Vivi891348b2013-05-06 19:37:36 -0300288 I915_WRITE(ILK_DISPLAY_CHICKEN1, ILK_FBCQ_DIS);
Rodrigo Vivi28554162013-05-06 19:37:37 -0300289 } else {
Damien Lespiau7dd23ba2013-05-10 14:33:17 +0100290 /* WaFbcAsynchFlipDisableFbcQueue:hsw */
Rodrigo Vivi28554162013-05-06 19:37:37 -0300291 I915_WRITE(HSW_PIPE_SLICE_CHICKEN_1(intel_crtc->pipe),
292 HSW_BYPASS_FBC_QUEUE);
Rodrigo Vivi891348b2013-05-06 19:37:36 -0300293 }
Rodrigo Vivib74ea102013-05-09 14:08:38 -0300294
Rodrigo Viviabe959c2013-05-06 19:37:33 -0300295 I915_WRITE(SNB_DPFC_CTL_SA,
296 SNB_CPU_FENCE_ENABLE | obj->fence_reg);
297 I915_WRITE(DPFC_CPU_FENCE_OFFSET, crtc->y);
298
299 sandybridge_blit_fbc_update(dev);
300
Ville Syrjäläb19870e2013-11-06 23:02:25 +0200301 DRM_DEBUG_KMS("enabled fbc on plane %c\n", plane_name(intel_crtc->plane));
Rodrigo Viviabe959c2013-05-06 19:37:33 -0300302}
303
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300304bool intel_fbc_enabled(struct drm_device *dev)
305{
306 struct drm_i915_private *dev_priv = dev->dev_private;
307
308 if (!dev_priv->display.fbc_enabled)
309 return false;
310
311 return dev_priv->display.fbc_enabled(dev);
312}
313
314static void intel_fbc_work_fn(struct work_struct *__work)
315{
316 struct intel_fbc_work *work =
317 container_of(to_delayed_work(__work),
318 struct intel_fbc_work, work);
319 struct drm_device *dev = work->crtc->dev;
320 struct drm_i915_private *dev_priv = dev->dev_private;
321
322 mutex_lock(&dev->struct_mutex);
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -0700323 if (work == dev_priv->fbc.fbc_work) {
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300324 /* Double check that we haven't switched fb without cancelling
325 * the prior work.
326 */
327 if (work->crtc->fb == work->fb) {
Ville Syrjälä993495a2013-12-12 17:27:40 +0200328 dev_priv->display.enable_fbc(work->crtc);
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300329
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -0700330 dev_priv->fbc.plane = to_intel_crtc(work->crtc)->plane;
331 dev_priv->fbc.fb_id = work->crtc->fb->base.id;
332 dev_priv->fbc.y = work->crtc->y;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300333 }
334
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -0700335 dev_priv->fbc.fbc_work = NULL;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300336 }
337 mutex_unlock(&dev->struct_mutex);
338
339 kfree(work);
340}
341
342static void intel_cancel_fbc_work(struct drm_i915_private *dev_priv)
343{
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -0700344 if (dev_priv->fbc.fbc_work == NULL)
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300345 return;
346
347 DRM_DEBUG_KMS("cancelling pending FBC enable\n");
348
349 /* Synchronisation is provided by struct_mutex and checking of
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -0700350 * dev_priv->fbc.fbc_work, so we can perform the cancellation
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300351 * entirely asynchronously.
352 */
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -0700353 if (cancel_delayed_work(&dev_priv->fbc.fbc_work->work))
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300354 /* tasklet was killed before being run, clean up */
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -0700355 kfree(dev_priv->fbc.fbc_work);
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300356
357 /* Mark the work as no longer wanted so that if it does
358 * wake-up (because the work was already running and waiting
359 * for our mutex), it will discover that is no longer
360 * necessary to run.
361 */
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -0700362 dev_priv->fbc.fbc_work = NULL;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300363}
364
Ville Syrjälä993495a2013-12-12 17:27:40 +0200365static void intel_enable_fbc(struct drm_crtc *crtc)
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300366{
367 struct intel_fbc_work *work;
368 struct drm_device *dev = crtc->dev;
369 struct drm_i915_private *dev_priv = dev->dev_private;
370
371 if (!dev_priv->display.enable_fbc)
372 return;
373
374 intel_cancel_fbc_work(dev_priv);
375
Daniel Vetterb14c5672013-09-19 12:18:32 +0200376 work = kzalloc(sizeof(*work), GFP_KERNEL);
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300377 if (work == NULL) {
Paulo Zanoni6cdcb5e2013-06-12 17:27:29 -0300378 DRM_ERROR("Failed to allocate FBC work structure\n");
Ville Syrjälä993495a2013-12-12 17:27:40 +0200379 dev_priv->display.enable_fbc(crtc);
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300380 return;
381 }
382
383 work->crtc = crtc;
384 work->fb = crtc->fb;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300385 INIT_DELAYED_WORK(&work->work, intel_fbc_work_fn);
386
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -0700387 dev_priv->fbc.fbc_work = work;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300388
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300389 /* Delay the actual enabling to let pageflipping cease and the
390 * display to settle before starting the compression. Note that
391 * this delay also serves a second purpose: it allows for a
392 * vblank to pass after disabling the FBC before we attempt
393 * to modify the control registers.
394 *
395 * A more complicated solution would involve tracking vblanks
396 * following the termination of the page-flipping sequence
397 * and indeed performing the enable as a co-routine and not
398 * waiting synchronously upon the vblank.
Damien Lespiau7457d612013-06-07 17:41:07 +0100399 *
400 * WaFbcWaitForVBlankBeforeEnable:ilk,snb
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300401 */
402 schedule_delayed_work(&work->work, msecs_to_jiffies(50));
403}
404
405void intel_disable_fbc(struct drm_device *dev)
406{
407 struct drm_i915_private *dev_priv = dev->dev_private;
408
409 intel_cancel_fbc_work(dev_priv);
410
411 if (!dev_priv->display.disable_fbc)
412 return;
413
414 dev_priv->display.disable_fbc(dev);
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -0700415 dev_priv->fbc.plane = -1;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300416}
417
Chris Wilson29ebf902013-07-27 17:23:55 +0100418static bool set_no_fbc_reason(struct drm_i915_private *dev_priv,
419 enum no_fbc_reason reason)
420{
421 if (dev_priv->fbc.no_fbc_reason == reason)
422 return false;
423
424 dev_priv->fbc.no_fbc_reason = reason;
425 return true;
426}
427
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300428/**
429 * intel_update_fbc - enable/disable FBC as needed
430 * @dev: the drm_device
431 *
432 * Set up the framebuffer compression hardware at mode set time. We
433 * enable it if possible:
434 * - plane A only (on pre-965)
435 * - no pixel mulitply/line duplication
436 * - no alpha buffer discard
437 * - no dual wide
Paulo Zanonif85da862013-06-04 16:53:39 -0300438 * - framebuffer <= max_hdisplay in width, max_vdisplay in height
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300439 *
440 * We can't assume that any compression will take place (worst case),
441 * so the compressed buffer has to be the same size as the uncompressed
442 * one. It also must reside (along with the line length buffer) in
443 * stolen memory.
444 *
445 * We need to enable/disable FBC on a global basis.
446 */
447void intel_update_fbc(struct drm_device *dev)
448{
449 struct drm_i915_private *dev_priv = dev->dev_private;
450 struct drm_crtc *crtc = NULL, *tmp_crtc;
451 struct intel_crtc *intel_crtc;
452 struct drm_framebuffer *fb;
453 struct intel_framebuffer *intel_fb;
454 struct drm_i915_gem_object *obj;
Ville Syrjäläef644fd2013-09-04 18:25:21 +0300455 const struct drm_display_mode *adjusted_mode;
Ville Syrjälä37327ab2013-09-04 18:25:28 +0300456 unsigned int max_width, max_height;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300457
Daniel Vetter3a77c4c2014-01-10 08:50:12 +0100458 if (!HAS_FBC(dev)) {
Chris Wilson29ebf902013-07-27 17:23:55 +0100459 set_no_fbc_reason(dev_priv, FBC_UNSUPPORTED);
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300460 return;
Chris Wilson29ebf902013-07-27 17:23:55 +0100461 }
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300462
Chris Wilson29ebf902013-07-27 17:23:55 +0100463 if (!i915_powersave) {
464 if (set_no_fbc_reason(dev_priv, FBC_MODULE_PARAM))
465 DRM_DEBUG_KMS("fbc disabled per module param\n");
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300466 return;
Chris Wilson29ebf902013-07-27 17:23:55 +0100467 }
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300468
469 /*
470 * If FBC is already on, we just have to verify that we can
471 * keep it that way...
472 * Need to disable if:
473 * - more than one pipe is active
474 * - changing FBC params (stride, fence, mode)
475 * - new fb is too large to fit in compressed buffer
476 * - going to an unsupported config (interlace, pixel multiply, etc.)
477 */
478 list_for_each_entry(tmp_crtc, &dev->mode_config.crtc_list, head) {
Chris Wilson3490ea52013-01-07 10:11:40 +0000479 if (intel_crtc_active(tmp_crtc) &&
Ville Syrjälä4c445e02013-10-09 17:24:58 +0300480 to_intel_crtc(tmp_crtc)->primary_enabled) {
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300481 if (crtc) {
Chris Wilson29ebf902013-07-27 17:23:55 +0100482 if (set_no_fbc_reason(dev_priv, FBC_MULTIPLE_PIPES))
483 DRM_DEBUG_KMS("more than one pipe active, disabling compression\n");
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300484 goto out_disable;
485 }
486 crtc = tmp_crtc;
487 }
488 }
489
490 if (!crtc || crtc->fb == NULL) {
Chris Wilson29ebf902013-07-27 17:23:55 +0100491 if (set_no_fbc_reason(dev_priv, FBC_NO_OUTPUT))
492 DRM_DEBUG_KMS("no output, disabling\n");
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300493 goto out_disable;
494 }
495
496 intel_crtc = to_intel_crtc(crtc);
497 fb = crtc->fb;
498 intel_fb = to_intel_framebuffer(fb);
499 obj = intel_fb->obj;
Ville Syrjäläef644fd2013-09-04 18:25:21 +0300500 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) {
Ville Syrjälä37327ab2013-09-04 18:25:28 +0300522 max_width = 4096;
523 max_height = 2048;
Paulo Zanonif85da862013-06-04 16:53:39 -0300524 } else {
Ville Syrjälä37327ab2013-09-04 18:25:28 +0300525 max_width = 2048;
526 max_height = 1536;
Paulo Zanonif85da862013-06-04 16:53:39 -0300527 }
Ville Syrjälä37327ab2013-09-04 18:25:28 +0300528 if (intel_crtc->config.pipe_src_w > max_width ||
529 intel_crtc->config.pipe_src_h > max_height) {
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 }
Ville Syrjäläc5a44aa2013-11-28 17:29:58 +0200534 if ((INTEL_INFO(dev)->gen < 4 || IS_HASWELL(dev)) &&
535 intel_crtc->plane != PLANE_A) {
Chris Wilson29ebf902013-07-27 17:23:55 +0100536 if (set_no_fbc_reason(dev_priv, FBC_BAD_PLANE))
Ville Syrjäläc5a44aa2013-11-28 17:29:58 +0200537 DRM_DEBUG_KMS("plane not A, 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
Ville Syrjälä993495a2013-12-12 17:27:40 +0200599 intel_enable_fbc(crtc);
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
Daniel Vetterfeb56b92013-12-14 20:38:30 -0200821static int i830_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 Dodonovb445e3b2012-04-16 22:20:35 -0300854/* Pineview has different values for various configs */
855static const struct intel_watermark_params pineview_display_wm = {
856 PINEVIEW_DISPLAY_FIFO,
857 PINEVIEW_MAX_WM,
858 PINEVIEW_DFT_WM,
859 PINEVIEW_GUARD_WM,
860 PINEVIEW_FIFO_LINE_SIZE
861};
862static const struct intel_watermark_params pineview_display_hplloff_wm = {
863 PINEVIEW_DISPLAY_FIFO,
864 PINEVIEW_MAX_WM,
865 PINEVIEW_DFT_HPLLOFF_WM,
866 PINEVIEW_GUARD_WM,
867 PINEVIEW_FIFO_LINE_SIZE
868};
869static const struct intel_watermark_params pineview_cursor_wm = {
870 PINEVIEW_CURSOR_FIFO,
871 PINEVIEW_CURSOR_MAX_WM,
872 PINEVIEW_CURSOR_DFT_WM,
873 PINEVIEW_CURSOR_GUARD_WM,
874 PINEVIEW_FIFO_LINE_SIZE,
875};
876static const struct intel_watermark_params pineview_cursor_hplloff_wm = {
877 PINEVIEW_CURSOR_FIFO,
878 PINEVIEW_CURSOR_MAX_WM,
879 PINEVIEW_CURSOR_DFT_WM,
880 PINEVIEW_CURSOR_GUARD_WM,
881 PINEVIEW_FIFO_LINE_SIZE
882};
883static const struct intel_watermark_params g4x_wm_info = {
884 G4X_FIFO_SIZE,
885 G4X_MAX_WM,
886 G4X_MAX_WM,
887 2,
888 G4X_FIFO_LINE_SIZE,
889};
890static const struct intel_watermark_params g4x_cursor_wm_info = {
891 I965_CURSOR_FIFO,
892 I965_CURSOR_MAX_WM,
893 I965_CURSOR_DFT_WM,
894 2,
895 G4X_FIFO_LINE_SIZE,
896};
897static const struct intel_watermark_params valleyview_wm_info = {
898 VALLEYVIEW_FIFO_SIZE,
899 VALLEYVIEW_MAX_WM,
900 VALLEYVIEW_MAX_WM,
901 2,
902 G4X_FIFO_LINE_SIZE,
903};
904static const struct intel_watermark_params valleyview_cursor_wm_info = {
905 I965_CURSOR_FIFO,
906 VALLEYVIEW_CURSOR_MAX_WM,
907 I965_CURSOR_DFT_WM,
908 2,
909 G4X_FIFO_LINE_SIZE,
910};
911static const struct intel_watermark_params i965_cursor_wm_info = {
912 I965_CURSOR_FIFO,
913 I965_CURSOR_MAX_WM,
914 I965_CURSOR_DFT_WM,
915 2,
916 I915_FIFO_LINE_SIZE,
917};
918static const struct intel_watermark_params i945_wm_info = {
919 I945_FIFO_SIZE,
920 I915_MAX_WM,
921 1,
922 2,
923 I915_FIFO_LINE_SIZE
924};
925static const struct intel_watermark_params i915_wm_info = {
926 I915_FIFO_SIZE,
927 I915_MAX_WM,
928 1,
929 2,
930 I915_FIFO_LINE_SIZE
931};
Daniel Vetterfeb56b92013-12-14 20:38:30 -0200932static const struct intel_watermark_params i830_wm_info = {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300933 I855GM_FIFO_SIZE,
934 I915_MAX_WM,
935 1,
936 2,
937 I830_FIFO_LINE_SIZE
938};
Daniel Vetterfeb56b92013-12-14 20:38:30 -0200939static const struct intel_watermark_params i845_wm_info = {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300940 I830_FIFO_SIZE,
941 I915_MAX_WM,
942 1,
943 2,
944 I830_FIFO_LINE_SIZE
945};
946
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300947/**
948 * intel_calculate_wm - calculate watermark level
949 * @clock_in_khz: pixel clock
950 * @wm: chip FIFO params
951 * @pixel_size: display pixel size
952 * @latency_ns: memory latency for the platform
953 *
954 * Calculate the watermark level (the level at which the display plane will
955 * start fetching from memory again). Each chip has a different display
956 * FIFO size and allocation, so the caller needs to figure that out and pass
957 * in the correct intel_watermark_params structure.
958 *
959 * As the pixel clock runs, the FIFO will be drained at a rate that depends
960 * on the pixel size. When it reaches the watermark level, it'll start
961 * fetching FIFO line sized based chunks from memory until the FIFO fills
962 * past the watermark point. If the FIFO drains completely, a FIFO underrun
963 * will occur, and a display engine hang could result.
964 */
965static unsigned long intel_calculate_wm(unsigned long clock_in_khz,
966 const struct intel_watermark_params *wm,
967 int fifo_size,
968 int pixel_size,
969 unsigned long latency_ns)
970{
971 long entries_required, wm_size;
972
973 /*
974 * Note: we need to make sure we don't overflow for various clock &
975 * latency values.
976 * clocks go from a few thousand to several hundred thousand.
977 * latency is usually a few thousand
978 */
979 entries_required = ((clock_in_khz / 1000) * pixel_size * latency_ns) /
980 1000;
981 entries_required = DIV_ROUND_UP(entries_required, wm->cacheline_size);
982
983 DRM_DEBUG_KMS("FIFO entries required for mode: %ld\n", entries_required);
984
985 wm_size = fifo_size - (entries_required + wm->guard_size);
986
987 DRM_DEBUG_KMS("FIFO watermark level: %ld\n", wm_size);
988
989 /* Don't promote wm_size to unsigned... */
990 if (wm_size > (long)wm->max_wm)
991 wm_size = wm->max_wm;
992 if (wm_size <= 0)
993 wm_size = wm->default_wm;
994 return wm_size;
995}
996
997static struct drm_crtc *single_enabled_crtc(struct drm_device *dev)
998{
999 struct drm_crtc *crtc, *enabled = NULL;
1000
1001 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Chris Wilson3490ea52013-01-07 10:11:40 +00001002 if (intel_crtc_active(crtc)) {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001003 if (enabled)
1004 return NULL;
1005 enabled = crtc;
1006 }
1007 }
1008
1009 return enabled;
1010}
1011
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001012static void pineview_update_wm(struct drm_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001013{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001014 struct drm_device *dev = unused_crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001015 struct drm_i915_private *dev_priv = dev->dev_private;
1016 struct drm_crtc *crtc;
1017 const struct cxsr_latency *latency;
1018 u32 reg;
1019 unsigned long wm;
1020
1021 latency = intel_get_cxsr_latency(IS_PINEVIEW_G(dev), dev_priv->is_ddr3,
1022 dev_priv->fsb_freq, dev_priv->mem_freq);
1023 if (!latency) {
1024 DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
1025 pineview_disable_cxsr(dev);
1026 return;
1027 }
1028
1029 crtc = single_enabled_crtc(dev);
1030 if (crtc) {
Damien Lespiau241bfc32013-09-25 16:45:37 +01001031 const struct drm_display_mode *adjusted_mode;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001032 int pixel_size = crtc->fb->bits_per_pixel / 8;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001033 int clock;
1034
1035 adjusted_mode = &to_intel_crtc(crtc)->config.adjusted_mode;
1036 clock = adjusted_mode->crtc_clock;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001037
1038 /* Display SR */
1039 wm = intel_calculate_wm(clock, &pineview_display_wm,
1040 pineview_display_wm.fifo_size,
1041 pixel_size, latency->display_sr);
1042 reg = I915_READ(DSPFW1);
1043 reg &= ~DSPFW_SR_MASK;
1044 reg |= wm << DSPFW_SR_SHIFT;
1045 I915_WRITE(DSPFW1, reg);
1046 DRM_DEBUG_KMS("DSPFW1 register is %x\n", reg);
1047
1048 /* cursor SR */
1049 wm = intel_calculate_wm(clock, &pineview_cursor_wm,
1050 pineview_display_wm.fifo_size,
1051 pixel_size, latency->cursor_sr);
1052 reg = I915_READ(DSPFW3);
1053 reg &= ~DSPFW_CURSOR_SR_MASK;
1054 reg |= (wm & 0x3f) << DSPFW_CURSOR_SR_SHIFT;
1055 I915_WRITE(DSPFW3, reg);
1056
1057 /* Display HPLL off SR */
1058 wm = intel_calculate_wm(clock, &pineview_display_hplloff_wm,
1059 pineview_display_hplloff_wm.fifo_size,
1060 pixel_size, latency->display_hpll_disable);
1061 reg = I915_READ(DSPFW3);
1062 reg &= ~DSPFW_HPLL_SR_MASK;
1063 reg |= wm & DSPFW_HPLL_SR_MASK;
1064 I915_WRITE(DSPFW3, reg);
1065
1066 /* cursor HPLL off SR */
1067 wm = intel_calculate_wm(clock, &pineview_cursor_hplloff_wm,
1068 pineview_display_hplloff_wm.fifo_size,
1069 pixel_size, latency->cursor_hpll_disable);
1070 reg = I915_READ(DSPFW3);
1071 reg &= ~DSPFW_HPLL_CURSOR_MASK;
1072 reg |= (wm & 0x3f) << DSPFW_HPLL_CURSOR_SHIFT;
1073 I915_WRITE(DSPFW3, reg);
1074 DRM_DEBUG_KMS("DSPFW3 register is %x\n", reg);
1075
1076 /* activate cxsr */
1077 I915_WRITE(DSPFW3,
1078 I915_READ(DSPFW3) | PINEVIEW_SELF_REFRESH_EN);
1079 DRM_DEBUG_KMS("Self-refresh is enabled\n");
1080 } else {
1081 pineview_disable_cxsr(dev);
1082 DRM_DEBUG_KMS("Self-refresh is disabled\n");
1083 }
1084}
1085
1086static bool g4x_compute_wm0(struct drm_device *dev,
1087 int plane,
1088 const struct intel_watermark_params *display,
1089 int display_latency_ns,
1090 const struct intel_watermark_params *cursor,
1091 int cursor_latency_ns,
1092 int *plane_wm,
1093 int *cursor_wm)
1094{
1095 struct drm_crtc *crtc;
Ville Syrjälä4fe85902013-09-04 18:25:22 +03001096 const struct drm_display_mode *adjusted_mode;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001097 int htotal, hdisplay, clock, pixel_size;
1098 int line_time_us, line_count;
1099 int entries, tlb_miss;
1100
1101 crtc = intel_get_crtc_for_plane(dev, plane);
Chris Wilson3490ea52013-01-07 10:11:40 +00001102 if (!intel_crtc_active(crtc)) {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001103 *cursor_wm = cursor->guard_size;
1104 *plane_wm = display->guard_size;
1105 return false;
1106 }
1107
Ville Syrjälä4fe85902013-09-04 18:25:22 +03001108 adjusted_mode = &to_intel_crtc(crtc)->config.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001109 clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -08001110 htotal = adjusted_mode->crtc_htotal;
Ville Syrjälä37327ab2013-09-04 18:25:28 +03001111 hdisplay = to_intel_crtc(crtc)->config.pipe_src_w;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001112 pixel_size = crtc->fb->bits_per_pixel / 8;
1113
1114 /* Use the small buffer method to calculate plane watermark */
1115 entries = ((clock * pixel_size / 1000) * display_latency_ns) / 1000;
1116 tlb_miss = display->fifo_size*display->cacheline_size - hdisplay * 8;
1117 if (tlb_miss > 0)
1118 entries += tlb_miss;
1119 entries = DIV_ROUND_UP(entries, display->cacheline_size);
1120 *plane_wm = entries + display->guard_size;
1121 if (*plane_wm > (int)display->max_wm)
1122 *plane_wm = display->max_wm;
1123
1124 /* Use the large buffer method to calculate cursor watermark */
1125 line_time_us = ((htotal * 1000) / clock);
1126 line_count = (cursor_latency_ns / line_time_us + 1000) / 1000;
1127 entries = line_count * 64 * pixel_size;
1128 tlb_miss = cursor->fifo_size*cursor->cacheline_size - hdisplay * 8;
1129 if (tlb_miss > 0)
1130 entries += tlb_miss;
1131 entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
1132 *cursor_wm = entries + cursor->guard_size;
1133 if (*cursor_wm > (int)cursor->max_wm)
1134 *cursor_wm = (int)cursor->max_wm;
1135
1136 return true;
1137}
1138
1139/*
1140 * Check the wm result.
1141 *
1142 * If any calculated watermark values is larger than the maximum value that
1143 * can be programmed into the associated watermark register, that watermark
1144 * must be disabled.
1145 */
1146static bool g4x_check_srwm(struct drm_device *dev,
1147 int display_wm, int cursor_wm,
1148 const struct intel_watermark_params *display,
1149 const struct intel_watermark_params *cursor)
1150{
1151 DRM_DEBUG_KMS("SR watermark: display plane %d, cursor %d\n",
1152 display_wm, cursor_wm);
1153
1154 if (display_wm > display->max_wm) {
1155 DRM_DEBUG_KMS("display watermark is too large(%d/%ld), disabling\n",
1156 display_wm, display->max_wm);
1157 return false;
1158 }
1159
1160 if (cursor_wm > cursor->max_wm) {
1161 DRM_DEBUG_KMS("cursor watermark is too large(%d/%ld), disabling\n",
1162 cursor_wm, cursor->max_wm);
1163 return false;
1164 }
1165
1166 if (!(display_wm || cursor_wm)) {
1167 DRM_DEBUG_KMS("SR latency is 0, disabling\n");
1168 return false;
1169 }
1170
1171 return true;
1172}
1173
1174static bool g4x_compute_srwm(struct drm_device *dev,
1175 int plane,
1176 int latency_ns,
1177 const struct intel_watermark_params *display,
1178 const struct intel_watermark_params *cursor,
1179 int *display_wm, int *cursor_wm)
1180{
1181 struct drm_crtc *crtc;
Ville Syrjälä4fe85902013-09-04 18:25:22 +03001182 const struct drm_display_mode *adjusted_mode;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001183 int hdisplay, htotal, pixel_size, clock;
1184 unsigned long line_time_us;
1185 int line_count, line_size;
1186 int small, large;
1187 int entries;
1188
1189 if (!latency_ns) {
1190 *display_wm = *cursor_wm = 0;
1191 return false;
1192 }
1193
1194 crtc = intel_get_crtc_for_plane(dev, plane);
Ville Syrjälä4fe85902013-09-04 18:25:22 +03001195 adjusted_mode = &to_intel_crtc(crtc)->config.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001196 clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -08001197 htotal = adjusted_mode->crtc_htotal;
Ville Syrjälä37327ab2013-09-04 18:25:28 +03001198 hdisplay = to_intel_crtc(crtc)->config.pipe_src_w;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001199 pixel_size = crtc->fb->bits_per_pixel / 8;
1200
1201 line_time_us = (htotal * 1000) / clock;
1202 line_count = (latency_ns / line_time_us + 1000) / 1000;
1203 line_size = hdisplay * pixel_size;
1204
1205 /* Use the minimum of the small and large buffer method for primary */
1206 small = ((clock * pixel_size / 1000) * latency_ns) / 1000;
1207 large = line_count * line_size;
1208
1209 entries = DIV_ROUND_UP(min(small, large), display->cacheline_size);
1210 *display_wm = entries + display->guard_size;
1211
1212 /* calculate the self-refresh watermark for display cursor */
1213 entries = line_count * pixel_size * 64;
1214 entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
1215 *cursor_wm = entries + cursor->guard_size;
1216
1217 return g4x_check_srwm(dev,
1218 *display_wm, *cursor_wm,
1219 display, cursor);
1220}
1221
1222static bool vlv_compute_drain_latency(struct drm_device *dev,
1223 int plane,
1224 int *plane_prec_mult,
1225 int *plane_dl,
1226 int *cursor_prec_mult,
1227 int *cursor_dl)
1228{
1229 struct drm_crtc *crtc;
1230 int clock, pixel_size;
1231 int entries;
1232
1233 crtc = intel_get_crtc_for_plane(dev, plane);
Chris Wilson3490ea52013-01-07 10:11:40 +00001234 if (!intel_crtc_active(crtc))
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001235 return false;
1236
Damien Lespiau241bfc32013-09-25 16:45:37 +01001237 clock = to_intel_crtc(crtc)->config.adjusted_mode.crtc_clock;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001238 pixel_size = crtc->fb->bits_per_pixel / 8; /* BPP */
1239
1240 entries = (clock / 1000) * pixel_size;
1241 *plane_prec_mult = (entries > 256) ?
1242 DRAIN_LATENCY_PRECISION_32 : DRAIN_LATENCY_PRECISION_16;
1243 *plane_dl = (64 * (*plane_prec_mult) * 4) / ((clock / 1000) *
1244 pixel_size);
1245
1246 entries = (clock / 1000) * 4; /* BPP is always 4 for cursor */
1247 *cursor_prec_mult = (entries > 256) ?
1248 DRAIN_LATENCY_PRECISION_32 : DRAIN_LATENCY_PRECISION_16;
1249 *cursor_dl = (64 * (*cursor_prec_mult) * 4) / ((clock / 1000) * 4);
1250
1251 return true;
1252}
1253
1254/*
1255 * Update drain latency registers of memory arbiter
1256 *
1257 * Valleyview SoC has a new memory arbiter and needs drain latency registers
1258 * to be programmed. Each plane has a drain latency multiplier and a drain
1259 * latency value.
1260 */
1261
1262static void vlv_update_drain_latency(struct drm_device *dev)
1263{
1264 struct drm_i915_private *dev_priv = dev->dev_private;
1265 int planea_prec, planea_dl, planeb_prec, planeb_dl;
1266 int cursora_prec, cursora_dl, cursorb_prec, cursorb_dl;
1267 int plane_prec_mult, cursor_prec_mult; /* Precision multiplier is
1268 either 16 or 32 */
1269
1270 /* For plane A, Cursor A */
1271 if (vlv_compute_drain_latency(dev, 0, &plane_prec_mult, &planea_dl,
1272 &cursor_prec_mult, &cursora_dl)) {
1273 cursora_prec = (cursor_prec_mult == DRAIN_LATENCY_PRECISION_32) ?
1274 DDL_CURSORA_PRECISION_32 : DDL_CURSORA_PRECISION_16;
1275 planea_prec = (plane_prec_mult == DRAIN_LATENCY_PRECISION_32) ?
1276 DDL_PLANEA_PRECISION_32 : DDL_PLANEA_PRECISION_16;
1277
1278 I915_WRITE(VLV_DDL1, cursora_prec |
1279 (cursora_dl << DDL_CURSORA_SHIFT) |
1280 planea_prec | planea_dl);
1281 }
1282
1283 /* For plane B, Cursor B */
1284 if (vlv_compute_drain_latency(dev, 1, &plane_prec_mult, &planeb_dl,
1285 &cursor_prec_mult, &cursorb_dl)) {
1286 cursorb_prec = (cursor_prec_mult == DRAIN_LATENCY_PRECISION_32) ?
1287 DDL_CURSORB_PRECISION_32 : DDL_CURSORB_PRECISION_16;
1288 planeb_prec = (plane_prec_mult == DRAIN_LATENCY_PRECISION_32) ?
1289 DDL_PLANEB_PRECISION_32 : DDL_PLANEB_PRECISION_16;
1290
1291 I915_WRITE(VLV_DDL2, cursorb_prec |
1292 (cursorb_dl << DDL_CURSORB_SHIFT) |
1293 planeb_prec | planeb_dl);
1294 }
1295}
1296
1297#define single_plane_enabled(mask) is_power_of_2(mask)
1298
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001299static void valleyview_update_wm(struct drm_crtc *crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001300{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001301 struct drm_device *dev = crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001302 static const int sr_latency_ns = 12000;
1303 struct drm_i915_private *dev_priv = dev->dev_private;
1304 int planea_wm, planeb_wm, cursora_wm, cursorb_wm;
1305 int plane_sr, cursor_sr;
Chris Wilsonaf6c4572012-12-11 12:01:43 +00001306 int ignore_plane_sr, ignore_cursor_sr;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001307 unsigned int enabled = 0;
1308
1309 vlv_update_drain_latency(dev);
1310
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001311 if (g4x_compute_wm0(dev, PIPE_A,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001312 &valleyview_wm_info, latency_ns,
1313 &valleyview_cursor_wm_info, latency_ns,
1314 &planea_wm, &cursora_wm))
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001315 enabled |= 1 << PIPE_A;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001316
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001317 if (g4x_compute_wm0(dev, PIPE_B,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001318 &valleyview_wm_info, latency_ns,
1319 &valleyview_cursor_wm_info, latency_ns,
1320 &planeb_wm, &cursorb_wm))
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001321 enabled |= 1 << PIPE_B;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001322
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001323 if (single_plane_enabled(enabled) &&
1324 g4x_compute_srwm(dev, ffs(enabled) - 1,
1325 sr_latency_ns,
1326 &valleyview_wm_info,
1327 &valleyview_cursor_wm_info,
Chris Wilsonaf6c4572012-12-11 12:01:43 +00001328 &plane_sr, &ignore_cursor_sr) &&
1329 g4x_compute_srwm(dev, ffs(enabled) - 1,
1330 2*sr_latency_ns,
1331 &valleyview_wm_info,
1332 &valleyview_cursor_wm_info,
Chris Wilson52bd02d2012-12-07 10:43:24 +00001333 &ignore_plane_sr, &cursor_sr)) {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001334 I915_WRITE(FW_BLC_SELF_VLV, FW_CSPWRDWNEN);
Chris Wilson52bd02d2012-12-07 10:43:24 +00001335 } else {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001336 I915_WRITE(FW_BLC_SELF_VLV,
1337 I915_READ(FW_BLC_SELF_VLV) & ~FW_CSPWRDWNEN);
Chris Wilson52bd02d2012-12-07 10:43:24 +00001338 plane_sr = cursor_sr = 0;
1339 }
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001340
1341 DRM_DEBUG_KMS("Setting FIFO watermarks - A: plane=%d, cursor=%d, B: plane=%d, cursor=%d, SR: plane=%d, cursor=%d\n",
1342 planea_wm, cursora_wm,
1343 planeb_wm, cursorb_wm,
1344 plane_sr, cursor_sr);
1345
1346 I915_WRITE(DSPFW1,
1347 (plane_sr << DSPFW_SR_SHIFT) |
1348 (cursorb_wm << DSPFW_CURSORB_SHIFT) |
1349 (planeb_wm << DSPFW_PLANEB_SHIFT) |
1350 planea_wm);
1351 I915_WRITE(DSPFW2,
Chris Wilson8c919b22012-12-04 16:33:19 +00001352 (I915_READ(DSPFW2) & ~DSPFW_CURSORA_MASK) |
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001353 (cursora_wm << DSPFW_CURSORA_SHIFT));
1354 I915_WRITE(DSPFW3,
Chris Wilson8c919b22012-12-04 16:33:19 +00001355 (I915_READ(DSPFW3) & ~DSPFW_CURSOR_SR_MASK) |
1356 (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001357}
1358
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001359static void g4x_update_wm(struct drm_crtc *crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001360{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001361 struct drm_device *dev = crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001362 static const int sr_latency_ns = 12000;
1363 struct drm_i915_private *dev_priv = dev->dev_private;
1364 int planea_wm, planeb_wm, cursora_wm, cursorb_wm;
1365 int plane_sr, cursor_sr;
1366 unsigned int enabled = 0;
1367
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001368 if (g4x_compute_wm0(dev, PIPE_A,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001369 &g4x_wm_info, latency_ns,
1370 &g4x_cursor_wm_info, latency_ns,
1371 &planea_wm, &cursora_wm))
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001372 enabled |= 1 << PIPE_A;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001373
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001374 if (g4x_compute_wm0(dev, PIPE_B,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001375 &g4x_wm_info, latency_ns,
1376 &g4x_cursor_wm_info, latency_ns,
1377 &planeb_wm, &cursorb_wm))
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001378 enabled |= 1 << PIPE_B;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001379
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001380 if (single_plane_enabled(enabled) &&
1381 g4x_compute_srwm(dev, ffs(enabled) - 1,
1382 sr_latency_ns,
1383 &g4x_wm_info,
1384 &g4x_cursor_wm_info,
Chris Wilson52bd02d2012-12-07 10:43:24 +00001385 &plane_sr, &cursor_sr)) {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001386 I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_EN);
Chris Wilson52bd02d2012-12-07 10:43:24 +00001387 } else {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001388 I915_WRITE(FW_BLC_SELF,
1389 I915_READ(FW_BLC_SELF) & ~FW_BLC_SELF_EN);
Chris Wilson52bd02d2012-12-07 10:43:24 +00001390 plane_sr = cursor_sr = 0;
1391 }
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001392
1393 DRM_DEBUG_KMS("Setting FIFO watermarks - A: plane=%d, cursor=%d, B: plane=%d, cursor=%d, SR: plane=%d, cursor=%d\n",
1394 planea_wm, cursora_wm,
1395 planeb_wm, cursorb_wm,
1396 plane_sr, cursor_sr);
1397
1398 I915_WRITE(DSPFW1,
1399 (plane_sr << DSPFW_SR_SHIFT) |
1400 (cursorb_wm << DSPFW_CURSORB_SHIFT) |
1401 (planeb_wm << DSPFW_PLANEB_SHIFT) |
1402 planea_wm);
1403 I915_WRITE(DSPFW2,
Chris Wilson8c919b22012-12-04 16:33:19 +00001404 (I915_READ(DSPFW2) & ~DSPFW_CURSORA_MASK) |
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001405 (cursora_wm << DSPFW_CURSORA_SHIFT));
1406 /* HPLL off in SR has some issues on G4x... disable it */
1407 I915_WRITE(DSPFW3,
Chris Wilson8c919b22012-12-04 16:33:19 +00001408 (I915_READ(DSPFW3) & ~(DSPFW_HPLL_SR_EN | DSPFW_CURSOR_SR_MASK)) |
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001409 (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
1410}
1411
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001412static void i965_update_wm(struct drm_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001413{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001414 struct drm_device *dev = unused_crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001415 struct drm_i915_private *dev_priv = dev->dev_private;
1416 struct drm_crtc *crtc;
1417 int srwm = 1;
1418 int cursor_sr = 16;
1419
1420 /* Calc sr entries for one plane configs */
1421 crtc = single_enabled_crtc(dev);
1422 if (crtc) {
1423 /* self-refresh has much higher latency */
1424 static const int sr_latency_ns = 12000;
Ville Syrjälä4fe85902013-09-04 18:25:22 +03001425 const struct drm_display_mode *adjusted_mode =
1426 &to_intel_crtc(crtc)->config.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001427 int clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -08001428 int htotal = adjusted_mode->crtc_htotal;
Ville Syrjälä37327ab2013-09-04 18:25:28 +03001429 int hdisplay = to_intel_crtc(crtc)->config.pipe_src_w;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001430 int pixel_size = crtc->fb->bits_per_pixel / 8;
1431 unsigned long line_time_us;
1432 int entries;
1433
1434 line_time_us = ((htotal * 1000) / clock);
1435
1436 /* Use ns/us then divide to preserve precision */
1437 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
1438 pixel_size * hdisplay;
1439 entries = DIV_ROUND_UP(entries, I915_FIFO_LINE_SIZE);
1440 srwm = I965_FIFO_SIZE - entries;
1441 if (srwm < 0)
1442 srwm = 1;
1443 srwm &= 0x1ff;
1444 DRM_DEBUG_KMS("self-refresh entries: %d, wm: %d\n",
1445 entries, srwm);
1446
1447 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
1448 pixel_size * 64;
1449 entries = DIV_ROUND_UP(entries,
1450 i965_cursor_wm_info.cacheline_size);
1451 cursor_sr = i965_cursor_wm_info.fifo_size -
1452 (entries + i965_cursor_wm_info.guard_size);
1453
1454 if (cursor_sr > i965_cursor_wm_info.max_wm)
1455 cursor_sr = i965_cursor_wm_info.max_wm;
1456
1457 DRM_DEBUG_KMS("self-refresh watermark: display plane %d "
1458 "cursor %d\n", srwm, cursor_sr);
1459
1460 if (IS_CRESTLINE(dev))
1461 I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_EN);
1462 } else {
1463 /* Turn off self refresh if both pipes are enabled */
1464 if (IS_CRESTLINE(dev))
1465 I915_WRITE(FW_BLC_SELF, I915_READ(FW_BLC_SELF)
1466 & ~FW_BLC_SELF_EN);
1467 }
1468
1469 DRM_DEBUG_KMS("Setting FIFO watermarks - A: 8, B: 8, C: 8, SR %d\n",
1470 srwm);
1471
1472 /* 965 has limitations... */
1473 I915_WRITE(DSPFW1, (srwm << DSPFW_SR_SHIFT) |
1474 (8 << 16) | (8 << 8) | (8 << 0));
1475 I915_WRITE(DSPFW2, (8 << 8) | (8 << 0));
1476 /* update cursor SR watermark */
1477 I915_WRITE(DSPFW3, (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
1478}
1479
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001480static void i9xx_update_wm(struct drm_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001481{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001482 struct drm_device *dev = unused_crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001483 struct drm_i915_private *dev_priv = dev->dev_private;
1484 const struct intel_watermark_params *wm_info;
1485 uint32_t fwater_lo;
1486 uint32_t fwater_hi;
1487 int cwm, srwm = 1;
1488 int fifo_size;
1489 int planea_wm, planeb_wm;
1490 struct drm_crtc *crtc, *enabled = NULL;
1491
1492 if (IS_I945GM(dev))
1493 wm_info = &i945_wm_info;
1494 else if (!IS_GEN2(dev))
1495 wm_info = &i915_wm_info;
1496 else
Daniel Vetterfeb56b92013-12-14 20:38:30 -02001497 wm_info = &i830_wm_info;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001498
1499 fifo_size = dev_priv->display.get_fifo_size(dev, 0);
1500 crtc = intel_get_crtc_for_plane(dev, 0);
Chris Wilson3490ea52013-01-07 10:11:40 +00001501 if (intel_crtc_active(crtc)) {
Damien Lespiau241bfc32013-09-25 16:45:37 +01001502 const struct drm_display_mode *adjusted_mode;
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001503 int cpp = crtc->fb->bits_per_pixel / 8;
1504 if (IS_GEN2(dev))
1505 cpp = 4;
1506
Damien Lespiau241bfc32013-09-25 16:45:37 +01001507 adjusted_mode = &to_intel_crtc(crtc)->config.adjusted_mode;
1508 planea_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001509 wm_info, fifo_size, cpp,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001510 latency_ns);
1511 enabled = crtc;
1512 } else
1513 planea_wm = fifo_size - wm_info->guard_size;
1514
1515 fifo_size = dev_priv->display.get_fifo_size(dev, 1);
1516 crtc = intel_get_crtc_for_plane(dev, 1);
Chris Wilson3490ea52013-01-07 10:11:40 +00001517 if (intel_crtc_active(crtc)) {
Damien Lespiau241bfc32013-09-25 16:45:37 +01001518 const struct drm_display_mode *adjusted_mode;
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001519 int cpp = crtc->fb->bits_per_pixel / 8;
1520 if (IS_GEN2(dev))
1521 cpp = 4;
1522
Damien Lespiau241bfc32013-09-25 16:45:37 +01001523 adjusted_mode = &to_intel_crtc(crtc)->config.adjusted_mode;
1524 planeb_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001525 wm_info, fifo_size, cpp,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001526 latency_ns);
1527 if (enabled == NULL)
1528 enabled = crtc;
1529 else
1530 enabled = NULL;
1531 } else
1532 planeb_wm = fifo_size - wm_info->guard_size;
1533
1534 DRM_DEBUG_KMS("FIFO watermarks - A: %d, B: %d\n", planea_wm, planeb_wm);
1535
1536 /*
1537 * Overlay gets an aggressive default since video jitter is bad.
1538 */
1539 cwm = 2;
1540
1541 /* Play safe and disable self-refresh before adjusting watermarks. */
1542 if (IS_I945G(dev) || IS_I945GM(dev))
1543 I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_EN_MASK | 0);
1544 else if (IS_I915GM(dev))
Ville Syrjälä3f2dc5a2014-01-10 14:06:47 +02001545 I915_WRITE(INSTPM, _MASKED_BIT_DISABLE(INSTPM_SELF_EN));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001546
1547 /* Calc sr entries for one plane configs */
1548 if (HAS_FW_BLC(dev) && enabled) {
1549 /* self-refresh has much higher latency */
1550 static const int sr_latency_ns = 6000;
Ville Syrjälä4fe85902013-09-04 18:25:22 +03001551 const struct drm_display_mode *adjusted_mode =
1552 &to_intel_crtc(enabled)->config.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001553 int clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -08001554 int htotal = adjusted_mode->crtc_htotal;
Daniel Vetterf727b492013-11-20 15:02:10 +01001555 int hdisplay = to_intel_crtc(enabled)->config.pipe_src_w;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001556 int pixel_size = enabled->fb->bits_per_pixel / 8;
1557 unsigned long line_time_us;
1558 int entries;
1559
1560 line_time_us = (htotal * 1000) / clock;
1561
1562 /* Use ns/us then divide to preserve precision */
1563 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
1564 pixel_size * hdisplay;
1565 entries = DIV_ROUND_UP(entries, wm_info->cacheline_size);
1566 DRM_DEBUG_KMS("self-refresh entries: %d\n", entries);
1567 srwm = wm_info->fifo_size - entries;
1568 if (srwm < 0)
1569 srwm = 1;
1570
1571 if (IS_I945G(dev) || IS_I945GM(dev))
1572 I915_WRITE(FW_BLC_SELF,
1573 FW_BLC_SELF_FIFO_MASK | (srwm & 0xff));
1574 else if (IS_I915GM(dev))
1575 I915_WRITE(FW_BLC_SELF, srwm & 0x3f);
1576 }
1577
1578 DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d, B: %d, C: %d, SR %d\n",
1579 planea_wm, planeb_wm, cwm, srwm);
1580
1581 fwater_lo = ((planeb_wm & 0x3f) << 16) | (planea_wm & 0x3f);
1582 fwater_hi = (cwm & 0x1f);
1583
1584 /* Set request length to 8 cachelines per fetch */
1585 fwater_lo = fwater_lo | (1 << 24) | (1 << 8);
1586 fwater_hi = fwater_hi | (1 << 8);
1587
1588 I915_WRITE(FW_BLC, fwater_lo);
1589 I915_WRITE(FW_BLC2, fwater_hi);
1590
1591 if (HAS_FW_BLC(dev)) {
1592 if (enabled) {
1593 if (IS_I945G(dev) || IS_I945GM(dev))
1594 I915_WRITE(FW_BLC_SELF,
1595 FW_BLC_SELF_EN_MASK | FW_BLC_SELF_EN);
1596 else if (IS_I915GM(dev))
Ville Syrjälä3f2dc5a2014-01-10 14:06:47 +02001597 I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_SELF_EN));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001598 DRM_DEBUG_KMS("memory self refresh enabled\n");
1599 } else
1600 DRM_DEBUG_KMS("memory self refresh disabled\n");
1601 }
1602}
1603
Daniel Vetterfeb56b92013-12-14 20:38:30 -02001604static void i845_update_wm(struct drm_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001605{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001606 struct drm_device *dev = unused_crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001607 struct drm_i915_private *dev_priv = dev->dev_private;
1608 struct drm_crtc *crtc;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001609 const struct drm_display_mode *adjusted_mode;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001610 uint32_t fwater_lo;
1611 int planea_wm;
1612
1613 crtc = single_enabled_crtc(dev);
1614 if (crtc == NULL)
1615 return;
1616
Damien Lespiau241bfc32013-09-25 16:45:37 +01001617 adjusted_mode = &to_intel_crtc(crtc)->config.adjusted_mode;
1618 planea_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Daniel Vetterfeb56b92013-12-14 20:38:30 -02001619 &i845_wm_info,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001620 dev_priv->display.get_fifo_size(dev, 0),
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001621 4, latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001622 fwater_lo = I915_READ(FW_BLC) & ~0xfff;
1623 fwater_lo |= (3<<8) | planea_wm;
1624
1625 DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d\n", planea_wm);
1626
1627 I915_WRITE(FW_BLC, fwater_lo);
1628}
1629
Ville Syrjälä36587292013-07-05 11:57:16 +03001630static uint32_t ilk_pipe_pixel_rate(struct drm_device *dev,
1631 struct drm_crtc *crtc)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001632{
1633 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilsonfd4daa92013-08-27 17:04:17 +01001634 uint32_t pixel_rate;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001635
Damien Lespiau241bfc32013-09-25 16:45:37 +01001636 pixel_rate = intel_crtc->config.adjusted_mode.crtc_clock;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001637
1638 /* We only use IF-ID interlacing. If we ever use PF-ID we'll need to
1639 * adjust the pixel_rate here. */
1640
Chris Wilsonfd4daa92013-08-27 17:04:17 +01001641 if (intel_crtc->config.pch_pfit.enabled) {
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001642 uint64_t pipe_w, pipe_h, pfit_w, pfit_h;
Chris Wilsonfd4daa92013-08-27 17:04:17 +01001643 uint32_t pfit_size = intel_crtc->config.pch_pfit.size;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001644
Ville Syrjälä37327ab2013-09-04 18:25:28 +03001645 pipe_w = intel_crtc->config.pipe_src_w;
1646 pipe_h = intel_crtc->config.pipe_src_h;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001647 pfit_w = (pfit_size >> 16) & 0xFFFF;
1648 pfit_h = pfit_size & 0xFFFF;
1649 if (pipe_w < pfit_w)
1650 pipe_w = pfit_w;
1651 if (pipe_h < pfit_h)
1652 pipe_h = pfit_h;
1653
1654 pixel_rate = div_u64((uint64_t) pixel_rate * pipe_w * pipe_h,
1655 pfit_w * pfit_h);
1656 }
1657
1658 return pixel_rate;
1659}
1660
Ville Syrjälä37126462013-08-01 16:18:55 +03001661/* latency must be in 0.1us units. */
Ville Syrjälä23297042013-07-05 11:57:17 +03001662static uint32_t ilk_wm_method1(uint32_t pixel_rate, uint8_t bytes_per_pixel,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001663 uint32_t latency)
1664{
1665 uint64_t ret;
1666
Ville Syrjälä3312ba62013-08-01 16:18:53 +03001667 if (WARN(latency == 0, "Latency value missing\n"))
1668 return UINT_MAX;
1669
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001670 ret = (uint64_t) pixel_rate * bytes_per_pixel * latency;
1671 ret = DIV_ROUND_UP_ULL(ret, 64 * 10000) + 2;
1672
1673 return ret;
1674}
1675
Ville Syrjälä37126462013-08-01 16:18:55 +03001676/* latency must be in 0.1us units. */
Ville Syrjälä23297042013-07-05 11:57:17 +03001677static uint32_t ilk_wm_method2(uint32_t pixel_rate, uint32_t pipe_htotal,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001678 uint32_t horiz_pixels, uint8_t bytes_per_pixel,
1679 uint32_t latency)
1680{
1681 uint32_t ret;
1682
Ville Syrjälä3312ba62013-08-01 16:18:53 +03001683 if (WARN(latency == 0, "Latency value missing\n"))
1684 return UINT_MAX;
1685
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001686 ret = (latency * pixel_rate) / (pipe_htotal * 10000);
1687 ret = (ret + 1) * horiz_pixels * bytes_per_pixel;
1688 ret = DIV_ROUND_UP(ret, 64) + 2;
1689 return ret;
1690}
1691
Ville Syrjälä23297042013-07-05 11:57:17 +03001692static uint32_t ilk_wm_fbc(uint32_t pri_val, uint32_t horiz_pixels,
Paulo Zanonicca32e92013-05-31 11:45:06 -03001693 uint8_t bytes_per_pixel)
1694{
1695 return DIV_ROUND_UP(pri_val * 64, horiz_pixels * bytes_per_pixel) + 2;
1696}
1697
Imre Deak820c1982013-12-17 14:46:36 +02001698struct ilk_pipe_wm_parameters {
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001699 bool active;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001700 uint32_t pipe_htotal;
1701 uint32_t pixel_rate;
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001702 struct intel_plane_wm_parameters pri;
1703 struct intel_plane_wm_parameters spr;
1704 struct intel_plane_wm_parameters cur;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001705};
1706
Imre Deak820c1982013-12-17 14:46:36 +02001707struct ilk_wm_maximums {
Paulo Zanonicca32e92013-05-31 11:45:06 -03001708 uint16_t pri;
1709 uint16_t spr;
1710 uint16_t cur;
1711 uint16_t fbc;
1712};
1713
Ville Syrjälä240264f2013-08-07 13:29:12 +03001714/* used in computing the new watermarks state */
1715struct intel_wm_config {
1716 unsigned int num_pipes_active;
1717 bool sprites_enabled;
1718 bool sprites_scaled;
Ville Syrjälä240264f2013-08-07 13:29:12 +03001719};
1720
Ville Syrjälä37126462013-08-01 16:18:55 +03001721/*
1722 * For both WM_PIPE and WM_LP.
1723 * mem_value must be in 0.1us units.
1724 */
Imre Deak820c1982013-12-17 14:46:36 +02001725static uint32_t ilk_compute_pri_wm(const struct ilk_pipe_wm_parameters *params,
Paulo Zanonicca32e92013-05-31 11:45:06 -03001726 uint32_t mem_value,
1727 bool is_lp)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001728{
Paulo Zanonicca32e92013-05-31 11:45:06 -03001729 uint32_t method1, method2;
1730
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001731 if (!params->active || !params->pri.enabled)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001732 return 0;
1733
Ville Syrjälä23297042013-07-05 11:57:17 +03001734 method1 = ilk_wm_method1(params->pixel_rate,
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001735 params->pri.bytes_per_pixel,
Paulo Zanonicca32e92013-05-31 11:45:06 -03001736 mem_value);
1737
1738 if (!is_lp)
1739 return method1;
1740
Ville Syrjälä23297042013-07-05 11:57:17 +03001741 method2 = ilk_wm_method2(params->pixel_rate,
Paulo Zanonicca32e92013-05-31 11:45:06 -03001742 params->pipe_htotal,
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001743 params->pri.horiz_pixels,
1744 params->pri.bytes_per_pixel,
Paulo Zanonicca32e92013-05-31 11:45:06 -03001745 mem_value);
1746
1747 return min(method1, method2);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001748}
1749
Ville Syrjälä37126462013-08-01 16:18:55 +03001750/*
1751 * For both WM_PIPE and WM_LP.
1752 * mem_value must be in 0.1us units.
1753 */
Imre Deak820c1982013-12-17 14:46:36 +02001754static uint32_t ilk_compute_spr_wm(const struct ilk_pipe_wm_parameters *params,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001755 uint32_t mem_value)
1756{
1757 uint32_t method1, method2;
1758
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001759 if (!params->active || !params->spr.enabled)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001760 return 0;
1761
Ville Syrjälä23297042013-07-05 11:57:17 +03001762 method1 = ilk_wm_method1(params->pixel_rate,
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001763 params->spr.bytes_per_pixel,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001764 mem_value);
Ville Syrjälä23297042013-07-05 11:57:17 +03001765 method2 = ilk_wm_method2(params->pixel_rate,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001766 params->pipe_htotal,
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001767 params->spr.horiz_pixels,
1768 params->spr.bytes_per_pixel,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001769 mem_value);
1770 return min(method1, method2);
1771}
1772
Ville Syrjälä37126462013-08-01 16:18:55 +03001773/*
1774 * For both WM_PIPE and WM_LP.
1775 * mem_value must be in 0.1us units.
1776 */
Imre Deak820c1982013-12-17 14:46:36 +02001777static uint32_t ilk_compute_cur_wm(const struct ilk_pipe_wm_parameters *params,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001778 uint32_t mem_value)
1779{
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001780 if (!params->active || !params->cur.enabled)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001781 return 0;
1782
Ville Syrjälä23297042013-07-05 11:57:17 +03001783 return ilk_wm_method2(params->pixel_rate,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001784 params->pipe_htotal,
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001785 params->cur.horiz_pixels,
1786 params->cur.bytes_per_pixel,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001787 mem_value);
1788}
1789
Paulo Zanonicca32e92013-05-31 11:45:06 -03001790/* Only for WM_LP. */
Imre Deak820c1982013-12-17 14:46:36 +02001791static uint32_t ilk_compute_fbc_wm(const struct ilk_pipe_wm_parameters *params,
Ville Syrjälä1fda9882013-07-05 11:57:19 +03001792 uint32_t pri_val)
Paulo Zanonicca32e92013-05-31 11:45:06 -03001793{
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001794 if (!params->active || !params->pri.enabled)
Paulo Zanonicca32e92013-05-31 11:45:06 -03001795 return 0;
1796
Ville Syrjälä23297042013-07-05 11:57:17 +03001797 return ilk_wm_fbc(pri_val,
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001798 params->pri.horiz_pixels,
1799 params->pri.bytes_per_pixel);
Paulo Zanonicca32e92013-05-31 11:45:06 -03001800}
1801
Ville Syrjälä158ae642013-08-07 13:28:19 +03001802static unsigned int ilk_display_fifo_size(const struct drm_device *dev)
1803{
Ville Syrjälä416f4722013-11-02 21:07:46 -07001804 if (INTEL_INFO(dev)->gen >= 8)
1805 return 3072;
1806 else if (INTEL_INFO(dev)->gen >= 7)
Ville Syrjälä158ae642013-08-07 13:28:19 +03001807 return 768;
1808 else
1809 return 512;
1810}
1811
1812/* Calculate the maximum primary/sprite plane watermark */
1813static unsigned int ilk_plane_wm_max(const struct drm_device *dev,
1814 int level,
Ville Syrjälä240264f2013-08-07 13:29:12 +03001815 const struct intel_wm_config *config,
Ville Syrjälä158ae642013-08-07 13:28:19 +03001816 enum intel_ddb_partitioning ddb_partitioning,
1817 bool is_sprite)
1818{
1819 unsigned int fifo_size = ilk_display_fifo_size(dev);
1820 unsigned int max;
1821
1822 /* if sprites aren't enabled, sprites get nothing */
Ville Syrjälä240264f2013-08-07 13:29:12 +03001823 if (is_sprite && !config->sprites_enabled)
Ville Syrjälä158ae642013-08-07 13:28:19 +03001824 return 0;
1825
1826 /* HSW allows LP1+ watermarks even with multiple pipes */
Ville Syrjälä240264f2013-08-07 13:29:12 +03001827 if (level == 0 || config->num_pipes_active > 1) {
Ville Syrjälä158ae642013-08-07 13:28:19 +03001828 fifo_size /= INTEL_INFO(dev)->num_pipes;
1829
1830 /*
1831 * For some reason the non self refresh
1832 * FIFO size is only half of the self
1833 * refresh FIFO size on ILK/SNB.
1834 */
1835 if (INTEL_INFO(dev)->gen <= 6)
1836 fifo_size /= 2;
1837 }
1838
Ville Syrjälä240264f2013-08-07 13:29:12 +03001839 if (config->sprites_enabled) {
Ville Syrjälä158ae642013-08-07 13:28:19 +03001840 /* level 0 is always calculated with 1:1 split */
1841 if (level > 0 && ddb_partitioning == INTEL_DDB_PART_5_6) {
1842 if (is_sprite)
1843 fifo_size *= 5;
1844 fifo_size /= 6;
1845 } else {
1846 fifo_size /= 2;
1847 }
1848 }
1849
1850 /* clamp to max that the registers can hold */
Ville Syrjälä416f4722013-11-02 21:07:46 -07001851 if (INTEL_INFO(dev)->gen >= 8)
1852 max = level == 0 ? 255 : 2047;
1853 else if (INTEL_INFO(dev)->gen >= 7)
Ville Syrjälä158ae642013-08-07 13:28:19 +03001854 /* IVB/HSW primary/sprite plane watermarks */
1855 max = level == 0 ? 127 : 1023;
1856 else if (!is_sprite)
1857 /* ILK/SNB primary plane watermarks */
1858 max = level == 0 ? 127 : 511;
1859 else
1860 /* ILK/SNB sprite plane watermarks */
1861 max = level == 0 ? 63 : 255;
1862
1863 return min(fifo_size, max);
1864}
1865
1866/* Calculate the maximum cursor plane watermark */
1867static unsigned int ilk_cursor_wm_max(const struct drm_device *dev,
Ville Syrjälä240264f2013-08-07 13:29:12 +03001868 int level,
1869 const struct intel_wm_config *config)
Ville Syrjälä158ae642013-08-07 13:28:19 +03001870{
1871 /* HSW LP1+ watermarks w/ multiple pipes */
Ville Syrjälä240264f2013-08-07 13:29:12 +03001872 if (level > 0 && config->num_pipes_active > 1)
Ville Syrjälä158ae642013-08-07 13:28:19 +03001873 return 64;
1874
1875 /* otherwise just report max that registers can hold */
1876 if (INTEL_INFO(dev)->gen >= 7)
1877 return level == 0 ? 63 : 255;
1878 else
1879 return level == 0 ? 31 : 63;
1880}
1881
1882/* Calculate the maximum FBC watermark */
Ville Syrjälä416f4722013-11-02 21:07:46 -07001883static unsigned int ilk_fbc_wm_max(struct drm_device *dev)
Ville Syrjälä158ae642013-08-07 13:28:19 +03001884{
1885 /* max that registers can hold */
Ville Syrjälä416f4722013-11-02 21:07:46 -07001886 if (INTEL_INFO(dev)->gen >= 8)
1887 return 31;
1888 else
1889 return 15;
Ville Syrjälä158ae642013-08-07 13:28:19 +03001890}
1891
Ville Syrjälä34982fe2013-10-09 19:18:09 +03001892static void ilk_compute_wm_maximums(struct drm_device *dev,
1893 int level,
1894 const struct intel_wm_config *config,
1895 enum intel_ddb_partitioning ddb_partitioning,
Imre Deak820c1982013-12-17 14:46:36 +02001896 struct ilk_wm_maximums *max)
Ville Syrjälä158ae642013-08-07 13:28:19 +03001897{
Ville Syrjälä240264f2013-08-07 13:29:12 +03001898 max->pri = ilk_plane_wm_max(dev, level, config, ddb_partitioning, false);
1899 max->spr = ilk_plane_wm_max(dev, level, config, ddb_partitioning, true);
1900 max->cur = ilk_cursor_wm_max(dev, level, config);
Ville Syrjälä416f4722013-11-02 21:07:46 -07001901 max->fbc = ilk_fbc_wm_max(dev);
Ville Syrjälä158ae642013-08-07 13:28:19 +03001902}
1903
Ville Syrjäläd9395652013-10-09 19:18:10 +03001904static bool ilk_validate_wm_level(int level,
Imre Deak820c1982013-12-17 14:46:36 +02001905 const struct ilk_wm_maximums *max,
Ville Syrjäläd9395652013-10-09 19:18:10 +03001906 struct intel_wm_level *result)
Ville Syrjäläa9786a12013-08-07 13:24:47 +03001907{
1908 bool ret;
1909
1910 /* already determined to be invalid? */
1911 if (!result->enable)
1912 return false;
1913
1914 result->enable = result->pri_val <= max->pri &&
1915 result->spr_val <= max->spr &&
1916 result->cur_val <= max->cur;
1917
1918 ret = result->enable;
1919
1920 /*
1921 * HACK until we can pre-compute everything,
1922 * and thus fail gracefully if LP0 watermarks
1923 * are exceeded...
1924 */
1925 if (level == 0 && !result->enable) {
1926 if (result->pri_val > max->pri)
1927 DRM_DEBUG_KMS("Primary WM%d too large %u (max %u)\n",
1928 level, result->pri_val, max->pri);
1929 if (result->spr_val > max->spr)
1930 DRM_DEBUG_KMS("Sprite WM%d too large %u (max %u)\n",
1931 level, result->spr_val, max->spr);
1932 if (result->cur_val > max->cur)
1933 DRM_DEBUG_KMS("Cursor WM%d too large %u (max %u)\n",
1934 level, result->cur_val, max->cur);
1935
1936 result->pri_val = min_t(uint32_t, result->pri_val, max->pri);
1937 result->spr_val = min_t(uint32_t, result->spr_val, max->spr);
1938 result->cur_val = min_t(uint32_t, result->cur_val, max->cur);
1939 result->enable = true;
1940 }
1941
Ville Syrjäläa9786a12013-08-07 13:24:47 +03001942 return ret;
1943}
1944
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03001945static void ilk_compute_wm_level(struct drm_i915_private *dev_priv,
1946 int level,
Imre Deak820c1982013-12-17 14:46:36 +02001947 const struct ilk_pipe_wm_parameters *p,
Ville Syrjälä1fd527c2013-08-06 22:24:05 +03001948 struct intel_wm_level *result)
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03001949{
1950 uint16_t pri_latency = dev_priv->wm.pri_latency[level];
1951 uint16_t spr_latency = dev_priv->wm.spr_latency[level];
1952 uint16_t cur_latency = dev_priv->wm.cur_latency[level];
1953
1954 /* WM1+ latency values stored in 0.5us units */
1955 if (level > 0) {
1956 pri_latency *= 5;
1957 spr_latency *= 5;
1958 cur_latency *= 5;
1959 }
1960
1961 result->pri_val = ilk_compute_pri_wm(p, pri_latency, level);
1962 result->spr_val = ilk_compute_spr_wm(p, spr_latency);
1963 result->cur_val = ilk_compute_cur_wm(p, cur_latency);
1964 result->fbc_val = ilk_compute_fbc_wm(p, result->pri_val);
1965 result->enable = true;
1966}
1967
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001968static uint32_t
1969hsw_compute_linetime_wm(struct drm_device *dev, struct drm_crtc *crtc)
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03001970{
1971 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03001972 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03001973 struct drm_display_mode *mode = &intel_crtc->config.adjusted_mode;
Paulo Zanoni85a02de2013-05-03 17:23:43 -03001974 u32 linetime, ips_linetime;
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03001975
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001976 if (!intel_crtc_active(crtc))
1977 return 0;
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03001978
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03001979 /* The WM are computed with base on how long it takes to fill a single
1980 * row at the given clock rate, multiplied by 8.
1981 * */
Jesse Barnesfec8cba2013-11-27 11:10:26 -08001982 linetime = DIV_ROUND_CLOSEST(mode->crtc_htotal * 1000 * 8,
1983 mode->crtc_clock);
1984 ips_linetime = DIV_ROUND_CLOSEST(mode->crtc_htotal * 1000 * 8,
Paulo Zanoni85a02de2013-05-03 17:23:43 -03001985 intel_ddi_get_cdclk_freq(dev_priv));
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03001986
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001987 return PIPE_WM_LINETIME_IPS_LINETIME(ips_linetime) |
1988 PIPE_WM_LINETIME_TIME(linetime);
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03001989}
1990
Ville Syrjälä12b134d2013-07-05 11:57:21 +03001991static void intel_read_wm_latency(struct drm_device *dev, uint16_t wm[5])
1992{
1993 struct drm_i915_private *dev_priv = dev->dev_private;
1994
Ville Syrjäläa42a5712014-01-07 16:14:08 +02001995 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Ville Syrjälä12b134d2013-07-05 11:57:21 +03001996 uint64_t sskpd = I915_READ64(MCH_SSKPD);
1997
1998 wm[0] = (sskpd >> 56) & 0xFF;
1999 if (wm[0] == 0)
2000 wm[0] = sskpd & 0xF;
Ville Syrjäläe5d50192013-07-05 11:57:22 +03002001 wm[1] = (sskpd >> 4) & 0xFF;
2002 wm[2] = (sskpd >> 12) & 0xFF;
2003 wm[3] = (sskpd >> 20) & 0x1FF;
2004 wm[4] = (sskpd >> 32) & 0x1FF;
Ville Syrjälä63cf9a12013-07-05 11:57:23 +03002005 } else if (INTEL_INFO(dev)->gen >= 6) {
2006 uint32_t sskpd = I915_READ(MCH_SSKPD);
2007
2008 wm[0] = (sskpd >> SSKPD_WM0_SHIFT) & SSKPD_WM_MASK;
2009 wm[1] = (sskpd >> SSKPD_WM1_SHIFT) & SSKPD_WM_MASK;
2010 wm[2] = (sskpd >> SSKPD_WM2_SHIFT) & SSKPD_WM_MASK;
2011 wm[3] = (sskpd >> SSKPD_WM3_SHIFT) & SSKPD_WM_MASK;
Ville Syrjälä3a88d0a2013-08-01 16:18:49 +03002012 } else if (INTEL_INFO(dev)->gen >= 5) {
2013 uint32_t mltr = I915_READ(MLTR_ILK);
2014
2015 /* ILK primary LP0 latency is 700 ns */
2016 wm[0] = 7;
2017 wm[1] = (mltr >> MLTR_WM1_SHIFT) & ILK_SRLT_MASK;
2018 wm[2] = (mltr >> MLTR_WM2_SHIFT) & ILK_SRLT_MASK;
Ville Syrjälä12b134d2013-07-05 11:57:21 +03002019 }
2020}
2021
Ville Syrjälä53615a52013-08-01 16:18:50 +03002022static void intel_fixup_spr_wm_latency(struct drm_device *dev, uint16_t wm[5])
2023{
2024 /* ILK sprite LP0 latency is 1300 ns */
2025 if (INTEL_INFO(dev)->gen == 5)
2026 wm[0] = 13;
2027}
2028
2029static void intel_fixup_cur_wm_latency(struct drm_device *dev, uint16_t wm[5])
2030{
2031 /* ILK cursor LP0 latency is 1300 ns */
2032 if (INTEL_INFO(dev)->gen == 5)
2033 wm[0] = 13;
2034
2035 /* WaDoubleCursorLP3Latency:ivb */
2036 if (IS_IVYBRIDGE(dev))
2037 wm[3] *= 2;
2038}
2039
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03002040static int ilk_wm_max_level(const struct drm_device *dev)
2041{
2042 /* how many WM levels are we expecting */
Ville Syrjäläa42a5712014-01-07 16:14:08 +02002043 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03002044 return 4;
2045 else if (INTEL_INFO(dev)->gen >= 6)
2046 return 3;
2047 else
2048 return 2;
2049}
2050
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002051static void intel_print_wm_latency(struct drm_device *dev,
2052 const char *name,
2053 const uint16_t wm[5])
2054{
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03002055 int level, max_level = ilk_wm_max_level(dev);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002056
2057 for (level = 0; level <= max_level; level++) {
2058 unsigned int latency = wm[level];
2059
2060 if (latency == 0) {
2061 DRM_ERROR("%s WM%d latency not provided\n",
2062 name, level);
2063 continue;
2064 }
2065
2066 /* WM1+ latency values in 0.5us units */
2067 if (level > 0)
2068 latency *= 5;
2069
2070 DRM_DEBUG_KMS("%s WM%d latency %u (%u.%u usec)\n",
2071 name, level, wm[level],
2072 latency / 10, latency % 10);
2073 }
2074}
2075
Ville Syrjälä53615a52013-08-01 16:18:50 +03002076static void intel_setup_wm_latency(struct drm_device *dev)
2077{
2078 struct drm_i915_private *dev_priv = dev->dev_private;
2079
2080 intel_read_wm_latency(dev, dev_priv->wm.pri_latency);
2081
2082 memcpy(dev_priv->wm.spr_latency, dev_priv->wm.pri_latency,
2083 sizeof(dev_priv->wm.pri_latency));
2084 memcpy(dev_priv->wm.cur_latency, dev_priv->wm.pri_latency,
2085 sizeof(dev_priv->wm.pri_latency));
2086
2087 intel_fixup_spr_wm_latency(dev, dev_priv->wm.spr_latency);
2088 intel_fixup_cur_wm_latency(dev, dev_priv->wm.cur_latency);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002089
2090 intel_print_wm_latency(dev, "Primary", dev_priv->wm.pri_latency);
2091 intel_print_wm_latency(dev, "Sprite", dev_priv->wm.spr_latency);
2092 intel_print_wm_latency(dev, "Cursor", dev_priv->wm.cur_latency);
Ville Syrjälä53615a52013-08-01 16:18:50 +03002093}
2094
Imre Deak820c1982013-12-17 14:46:36 +02002095static void ilk_compute_wm_parameters(struct drm_crtc *crtc,
2096 struct ilk_pipe_wm_parameters *p,
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03002097 struct intel_wm_config *config)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002098{
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03002099 struct drm_device *dev = crtc->dev;
2100 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2101 enum pipe pipe = intel_crtc->pipe;
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03002102 struct drm_plane *plane;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002103
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03002104 p->active = intel_crtc_active(crtc);
2105 if (p->active) {
Jesse Barnes576b2592013-12-20 13:08:00 -08002106 p->pipe_htotal = intel_crtc->config.adjusted_mode.crtc_htotal;
Ville Syrjälä36587292013-07-05 11:57:16 +03002107 p->pixel_rate = ilk_pipe_pixel_rate(dev, crtc);
Ville Syrjäläc35426d2013-08-07 13:29:50 +03002108 p->pri.bytes_per_pixel = crtc->fb->bits_per_pixel / 8;
2109 p->cur.bytes_per_pixel = 4;
Ville Syrjälä37327ab2013-09-04 18:25:28 +03002110 p->pri.horiz_pixels = intel_crtc->config.pipe_src_w;
Ville Syrjäläc35426d2013-08-07 13:29:50 +03002111 p->cur.horiz_pixels = 64;
2112 /* TODO: for now, assume primary and cursor planes are always enabled. */
2113 p->pri.enabled = true;
2114 p->cur.enabled = true;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002115 }
2116
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03002117 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03002118 config->num_pipes_active += intel_crtc_active(crtc);
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03002119
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002120 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
2121 struct intel_plane *intel_plane = to_intel_plane(plane);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002122
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03002123 if (intel_plane->pipe == pipe)
2124 p->spr = intel_plane->wm;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002125
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03002126 config->sprites_enabled |= intel_plane->wm.enabled;
2127 config->sprites_scaled |= intel_plane->wm.scaled;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002128 }
2129}
2130
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002131/* Compute new watermarks for the pipe */
2132static bool intel_compute_pipe_wm(struct drm_crtc *crtc,
Imre Deak820c1982013-12-17 14:46:36 +02002133 const struct ilk_pipe_wm_parameters *params,
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002134 struct intel_pipe_wm *pipe_wm)
2135{
2136 struct drm_device *dev = crtc->dev;
2137 struct drm_i915_private *dev_priv = dev->dev_private;
2138 int level, max_level = ilk_wm_max_level(dev);
2139 /* LP0 watermark maximums depend on this pipe alone */
2140 struct intel_wm_config config = {
2141 .num_pipes_active = 1,
2142 .sprites_enabled = params->spr.enabled,
2143 .sprites_scaled = params->spr.scaled,
2144 };
Imre Deak820c1982013-12-17 14:46:36 +02002145 struct ilk_wm_maximums max;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002146
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002147 /* LP0 watermarks always use 1/2 DDB partitioning */
Ville Syrjälä34982fe2013-10-09 19:18:09 +03002148 ilk_compute_wm_maximums(dev, 0, &config, INTEL_DDB_PART_1_2, &max);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002149
Ville Syrjälä7b39a0b2013-12-05 15:51:30 +02002150 /* ILK/SNB: LP2+ watermarks only w/o sprites */
2151 if (INTEL_INFO(dev)->gen <= 6 && params->spr.enabled)
2152 max_level = 1;
2153
2154 /* ILK/SNB/IVB: LP1+ watermarks only w/o scaling */
2155 if (params->spr.scaled)
2156 max_level = 0;
2157
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002158 for (level = 0; level <= max_level; level++)
2159 ilk_compute_wm_level(dev_priv, level, params,
2160 &pipe_wm->wm[level]);
2161
Ville Syrjäläa42a5712014-01-07 16:14:08 +02002162 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Ville Syrjäläce0e0712013-12-05 15:51:36 +02002163 pipe_wm->linetime = hsw_compute_linetime_wm(dev, crtc);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002164
2165 /* At least LP0 must be valid */
Ville Syrjäläd9395652013-10-09 19:18:10 +03002166 return ilk_validate_wm_level(0, &max, &pipe_wm->wm[0]);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002167}
2168
2169/*
2170 * Merge the watermarks from all active pipes for a specific level.
2171 */
2172static void ilk_merge_wm_level(struct drm_device *dev,
2173 int level,
2174 struct intel_wm_level *ret_wm)
2175{
2176 const struct intel_crtc *intel_crtc;
2177
2178 list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list, base.head) {
2179 const struct intel_wm_level *wm =
2180 &intel_crtc->wm.active.wm[level];
2181
2182 if (!wm->enable)
2183 return;
2184
2185 ret_wm->pri_val = max(ret_wm->pri_val, wm->pri_val);
2186 ret_wm->spr_val = max(ret_wm->spr_val, wm->spr_val);
2187 ret_wm->cur_val = max(ret_wm->cur_val, wm->cur_val);
2188 ret_wm->fbc_val = max(ret_wm->fbc_val, wm->fbc_val);
2189 }
2190
2191 ret_wm->enable = true;
2192}
2193
2194/*
2195 * Merge all low power watermarks for all active pipes.
2196 */
2197static void ilk_wm_merge(struct drm_device *dev,
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02002198 const struct intel_wm_config *config,
Imre Deak820c1982013-12-17 14:46:36 +02002199 const struct ilk_wm_maximums *max,
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002200 struct intel_pipe_wm *merged)
2201{
2202 int level, max_level = ilk_wm_max_level(dev);
2203
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02002204 /* ILK/SNB/IVB: LP1+ watermarks only w/ single pipe */
2205 if ((INTEL_INFO(dev)->gen <= 6 || IS_IVYBRIDGE(dev)) &&
2206 config->num_pipes_active > 1)
2207 return;
2208
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02002209 /* ILK: FBC WM must be disabled always */
2210 merged->fbc_wm_enabled = INTEL_INFO(dev)->gen >= 6;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002211
2212 /* merge each WM1+ level */
2213 for (level = 1; level <= max_level; level++) {
2214 struct intel_wm_level *wm = &merged->wm[level];
2215
2216 ilk_merge_wm_level(dev, level, wm);
2217
Ville Syrjäläd9395652013-10-09 19:18:10 +03002218 if (!ilk_validate_wm_level(level, max, wm))
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002219 break;
2220
2221 /*
2222 * The spec says it is preferred to disable
2223 * FBC WMs instead of disabling a WM level.
2224 */
2225 if (wm->fbc_val > max->fbc) {
2226 merged->fbc_wm_enabled = false;
2227 wm->fbc_val = 0;
2228 }
2229 }
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02002230
2231 /* ILK: LP2+ must be disabled when FBC WM is disabled but FBC enabled */
2232 /*
2233 * FIXME this is racy. FBC might get enabled later.
2234 * What we should check here is whether FBC can be
2235 * enabled sometime later.
2236 */
2237 if (IS_GEN5(dev) && !merged->fbc_wm_enabled && intel_fbc_enabled(dev)) {
2238 for (level = 2; level <= max_level; level++) {
2239 struct intel_wm_level *wm = &merged->wm[level];
2240
2241 wm->enable = false;
2242 }
2243 }
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002244}
2245
Ville Syrjäläb380ca32013-10-09 19:18:01 +03002246static int ilk_wm_lp_to_level(int wm_lp, const struct intel_pipe_wm *pipe_wm)
2247{
2248 /* LP1,LP2,LP3 levels are either 1,2,3 or 1,3,4 */
2249 return wm_lp + (wm_lp >= 2 && pipe_wm->wm[4].enable);
2250}
2251
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02002252/* The value we need to program into the WM_LPx latency field */
2253static unsigned int ilk_wm_lp_latency(struct drm_device *dev, int level)
2254{
2255 struct drm_i915_private *dev_priv = dev->dev_private;
2256
Ville Syrjäläa42a5712014-01-07 16:14:08 +02002257 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02002258 return 2 * level;
2259 else
2260 return dev_priv->wm.pri_latency[level];
2261}
2262
Imre Deak820c1982013-12-17 14:46:36 +02002263static void ilk_compute_wm_results(struct drm_device *dev,
Ville Syrjälä0362c782013-10-09 19:17:57 +03002264 const struct intel_pipe_wm *merged,
Ville Syrjälä609cede2013-10-09 19:18:03 +03002265 enum intel_ddb_partitioning partitioning,
Imre Deak820c1982013-12-17 14:46:36 +02002266 struct ilk_wm_values *results)
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002267{
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002268 struct intel_crtc *intel_crtc;
2269 int level, wm_lp;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002270
Ville Syrjälä0362c782013-10-09 19:17:57 +03002271 results->enable_fbc_wm = merged->fbc_wm_enabled;
Ville Syrjälä609cede2013-10-09 19:18:03 +03002272 results->partitioning = partitioning;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002273
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002274 /* LP1+ register values */
Paulo Zanonicca32e92013-05-31 11:45:06 -03002275 for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
Ville Syrjälä1fd527c2013-08-06 22:24:05 +03002276 const struct intel_wm_level *r;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002277
Ville Syrjäläb380ca32013-10-09 19:18:01 +03002278 level = ilk_wm_lp_to_level(wm_lp, merged);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002279
Ville Syrjälä0362c782013-10-09 19:17:57 +03002280 r = &merged->wm[level];
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002281 if (!r->enable)
Paulo Zanonicca32e92013-05-31 11:45:06 -03002282 break;
2283
Ville Syrjälä416f4722013-11-02 21:07:46 -07002284 results->wm_lp[wm_lp - 1] = WM3_LP_EN |
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02002285 (ilk_wm_lp_latency(dev, level) << WM1_LP_LATENCY_SHIFT) |
Ville Syrjälä416f4722013-11-02 21:07:46 -07002286 (r->pri_val << WM1_LP_SR_SHIFT) |
2287 r->cur_val;
2288
2289 if (INTEL_INFO(dev)->gen >= 8)
2290 results->wm_lp[wm_lp - 1] |=
2291 r->fbc_val << WM1_LP_FBC_SHIFT_BDW;
2292 else
2293 results->wm_lp[wm_lp - 1] |=
2294 r->fbc_val << WM1_LP_FBC_SHIFT;
2295
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02002296 if (INTEL_INFO(dev)->gen <= 6 && r->spr_val) {
2297 WARN_ON(wm_lp != 1);
2298 results->wm_lp_spr[wm_lp - 1] = WM1S_LP_EN | r->spr_val;
2299 } else
2300 results->wm_lp_spr[wm_lp - 1] = r->spr_val;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002301 }
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002302
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002303 /* LP0 register values */
2304 list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list, base.head) {
2305 enum pipe pipe = intel_crtc->pipe;
2306 const struct intel_wm_level *r =
2307 &intel_crtc->wm.active.wm[0];
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002308
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002309 if (WARN_ON(!r->enable))
2310 continue;
2311
2312 results->wm_linetime[pipe] = intel_crtc->wm.active.linetime;
2313
2314 results->wm_pipe[pipe] =
2315 (r->pri_val << WM0_PIPE_PLANE_SHIFT) |
2316 (r->spr_val << WM0_PIPE_SPRITE_SHIFT) |
2317 r->cur_val;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002318 }
2319}
2320
Paulo Zanoni861f3382013-05-31 10:19:21 -03002321/* Find the result with the highest level enabled. Check for enable_fbc_wm in
2322 * case both are at the same level. Prefer r1 in case they're the same. */
Imre Deak820c1982013-12-17 14:46:36 +02002323static struct intel_pipe_wm *ilk_find_best_result(struct drm_device *dev,
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002324 struct intel_pipe_wm *r1,
2325 struct intel_pipe_wm *r2)
Paulo Zanoni861f3382013-05-31 10:19:21 -03002326{
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002327 int level, max_level = ilk_wm_max_level(dev);
2328 int level1 = 0, level2 = 0;
Paulo Zanoni861f3382013-05-31 10:19:21 -03002329
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002330 for (level = 1; level <= max_level; level++) {
2331 if (r1->wm[level].enable)
2332 level1 = level;
2333 if (r2->wm[level].enable)
2334 level2 = level;
Paulo Zanoni861f3382013-05-31 10:19:21 -03002335 }
2336
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002337 if (level1 == level2) {
2338 if (r2->fbc_wm_enabled && !r1->fbc_wm_enabled)
Paulo Zanoni861f3382013-05-31 10:19:21 -03002339 return r2;
2340 else
2341 return r1;
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002342 } else if (level1 > level2) {
Paulo Zanoni861f3382013-05-31 10:19:21 -03002343 return r1;
2344 } else {
2345 return r2;
2346 }
2347}
2348
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002349/* dirty bits used to track which watermarks need changes */
2350#define WM_DIRTY_PIPE(pipe) (1 << (pipe))
2351#define WM_DIRTY_LINETIME(pipe) (1 << (8 + (pipe)))
2352#define WM_DIRTY_LP(wm_lp) (1 << (15 + (wm_lp)))
2353#define WM_DIRTY_LP_ALL (WM_DIRTY_LP(1) | WM_DIRTY_LP(2) | WM_DIRTY_LP(3))
2354#define WM_DIRTY_FBC (1 << 24)
2355#define WM_DIRTY_DDB (1 << 25)
2356
2357static unsigned int ilk_compute_wm_dirty(struct drm_device *dev,
Imre Deak820c1982013-12-17 14:46:36 +02002358 const struct ilk_wm_values *old,
2359 const struct ilk_wm_values *new)
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002360{
2361 unsigned int dirty = 0;
2362 enum pipe pipe;
2363 int wm_lp;
2364
2365 for_each_pipe(pipe) {
2366 if (old->wm_linetime[pipe] != new->wm_linetime[pipe]) {
2367 dirty |= WM_DIRTY_LINETIME(pipe);
2368 /* Must disable LP1+ watermarks too */
2369 dirty |= WM_DIRTY_LP_ALL;
2370 }
2371
2372 if (old->wm_pipe[pipe] != new->wm_pipe[pipe]) {
2373 dirty |= WM_DIRTY_PIPE(pipe);
2374 /* Must disable LP1+ watermarks too */
2375 dirty |= WM_DIRTY_LP_ALL;
2376 }
2377 }
2378
2379 if (old->enable_fbc_wm != new->enable_fbc_wm) {
2380 dirty |= WM_DIRTY_FBC;
2381 /* Must disable LP1+ watermarks too */
2382 dirty |= WM_DIRTY_LP_ALL;
2383 }
2384
2385 if (old->partitioning != new->partitioning) {
2386 dirty |= WM_DIRTY_DDB;
2387 /* Must disable LP1+ watermarks too */
2388 dirty |= WM_DIRTY_LP_ALL;
2389 }
2390
2391 /* LP1+ watermarks already deemed dirty, no need to continue */
2392 if (dirty & WM_DIRTY_LP_ALL)
2393 return dirty;
2394
2395 /* Find the lowest numbered LP1+ watermark in need of an update... */
2396 for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
2397 if (old->wm_lp[wm_lp - 1] != new->wm_lp[wm_lp - 1] ||
2398 old->wm_lp_spr[wm_lp - 1] != new->wm_lp_spr[wm_lp - 1])
2399 break;
2400 }
2401
2402 /* ...and mark it and all higher numbered LP1+ watermarks as dirty */
2403 for (; wm_lp <= 3; wm_lp++)
2404 dirty |= WM_DIRTY_LP(wm_lp);
2405
2406 return dirty;
2407}
2408
Ville Syrjälä8553c182013-12-05 15:51:39 +02002409static bool _ilk_disable_lp_wm(struct drm_i915_private *dev_priv,
2410 unsigned int dirty)
2411{
Imre Deak820c1982013-12-17 14:46:36 +02002412 struct ilk_wm_values *previous = &dev_priv->wm.hw;
Ville Syrjälä8553c182013-12-05 15:51:39 +02002413 bool changed = false;
2414
2415 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] & WM1_LP_SR_EN) {
2416 previous->wm_lp[2] &= ~WM1_LP_SR_EN;
2417 I915_WRITE(WM3_LP_ILK, previous->wm_lp[2]);
2418 changed = true;
2419 }
2420 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] & WM1_LP_SR_EN) {
2421 previous->wm_lp[1] &= ~WM1_LP_SR_EN;
2422 I915_WRITE(WM2_LP_ILK, previous->wm_lp[1]);
2423 changed = true;
2424 }
2425 if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] & WM1_LP_SR_EN) {
2426 previous->wm_lp[0] &= ~WM1_LP_SR_EN;
2427 I915_WRITE(WM1_LP_ILK, previous->wm_lp[0]);
2428 changed = true;
2429 }
2430
2431 /*
2432 * Don't touch WM1S_LP_EN here.
2433 * Doing so could cause underruns.
2434 */
2435
2436 return changed;
2437}
2438
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002439/*
2440 * The spec says we shouldn't write when we don't need, because every write
2441 * causes WMs to be re-evaluated, expending some power.
2442 */
Imre Deak820c1982013-12-17 14:46:36 +02002443static void ilk_write_wm_values(struct drm_i915_private *dev_priv,
2444 struct ilk_wm_values *results)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002445{
Ville Syrjäläac9545f2013-12-05 15:51:28 +02002446 struct drm_device *dev = dev_priv->dev;
Imre Deak820c1982013-12-17 14:46:36 +02002447 struct ilk_wm_values *previous = &dev_priv->wm.hw;
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002448 unsigned int dirty;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002449 uint32_t val;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002450
Ville Syrjälä8553c182013-12-05 15:51:39 +02002451 dirty = ilk_compute_wm_dirty(dev, previous, results);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002452 if (!dirty)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002453 return;
2454
Ville Syrjälä8553c182013-12-05 15:51:39 +02002455 _ilk_disable_lp_wm(dev_priv, dirty);
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02002456
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002457 if (dirty & WM_DIRTY_PIPE(PIPE_A))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002458 I915_WRITE(WM0_PIPEA_ILK, results->wm_pipe[0]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002459 if (dirty & WM_DIRTY_PIPE(PIPE_B))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002460 I915_WRITE(WM0_PIPEB_ILK, results->wm_pipe[1]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002461 if (dirty & WM_DIRTY_PIPE(PIPE_C))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002462 I915_WRITE(WM0_PIPEC_IVB, results->wm_pipe[2]);
2463
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002464 if (dirty & WM_DIRTY_LINETIME(PIPE_A))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002465 I915_WRITE(PIPE_WM_LINETIME(PIPE_A), results->wm_linetime[0]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002466 if (dirty & WM_DIRTY_LINETIME(PIPE_B))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002467 I915_WRITE(PIPE_WM_LINETIME(PIPE_B), results->wm_linetime[1]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002468 if (dirty & WM_DIRTY_LINETIME(PIPE_C))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002469 I915_WRITE(PIPE_WM_LINETIME(PIPE_C), results->wm_linetime[2]);
2470
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002471 if (dirty & WM_DIRTY_DDB) {
Ville Syrjäläa42a5712014-01-07 16:14:08 +02002472 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Ville Syrjäläac9545f2013-12-05 15:51:28 +02002473 val = I915_READ(WM_MISC);
2474 if (results->partitioning == INTEL_DDB_PART_1_2)
2475 val &= ~WM_MISC_DATA_PARTITION_5_6;
2476 else
2477 val |= WM_MISC_DATA_PARTITION_5_6;
2478 I915_WRITE(WM_MISC, val);
2479 } else {
2480 val = I915_READ(DISP_ARB_CTL2);
2481 if (results->partitioning == INTEL_DDB_PART_1_2)
2482 val &= ~DISP_DATA_PARTITION_5_6;
2483 else
2484 val |= DISP_DATA_PARTITION_5_6;
2485 I915_WRITE(DISP_ARB_CTL2, val);
2486 }
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002487 }
2488
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002489 if (dirty & WM_DIRTY_FBC) {
Paulo Zanonicca32e92013-05-31 11:45:06 -03002490 val = I915_READ(DISP_ARB_CTL);
2491 if (results->enable_fbc_wm)
2492 val &= ~DISP_FBC_WM_DIS;
2493 else
2494 val |= DISP_FBC_WM_DIS;
2495 I915_WRITE(DISP_ARB_CTL, val);
2496 }
2497
Imre Deak954911e2013-12-17 14:46:34 +02002498 if (dirty & WM_DIRTY_LP(1) &&
2499 previous->wm_lp_spr[0] != results->wm_lp_spr[0])
2500 I915_WRITE(WM1S_LP_ILK, results->wm_lp_spr[0]);
2501
2502 if (INTEL_INFO(dev)->gen >= 7) {
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02002503 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp_spr[1] != results->wm_lp_spr[1])
2504 I915_WRITE(WM2S_LP_IVB, results->wm_lp_spr[1]);
2505 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp_spr[2] != results->wm_lp_spr[2])
2506 I915_WRITE(WM3S_LP_IVB, results->wm_lp_spr[2]);
2507 }
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002508
Ville Syrjäläfacd6192013-12-05 15:51:33 +02002509 if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] != results->wm_lp[0])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002510 I915_WRITE(WM1_LP_ILK, results->wm_lp[0]);
Ville Syrjäläfacd6192013-12-05 15:51:33 +02002511 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] != results->wm_lp[1])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002512 I915_WRITE(WM2_LP_ILK, results->wm_lp[1]);
Ville Syrjäläfacd6192013-12-05 15:51:33 +02002513 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] != results->wm_lp[2])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002514 I915_WRITE(WM3_LP_ILK, results->wm_lp[2]);
Ville Syrjälä609cede2013-10-09 19:18:03 +03002515
2516 dev_priv->wm.hw = *results;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002517}
2518
Ville Syrjälä8553c182013-12-05 15:51:39 +02002519static bool ilk_disable_lp_wm(struct drm_device *dev)
2520{
2521 struct drm_i915_private *dev_priv = dev->dev_private;
2522
2523 return _ilk_disable_lp_wm(dev_priv, WM_DIRTY_LP_ALL);
2524}
2525
Imre Deak820c1982013-12-17 14:46:36 +02002526static void ilk_update_wm(struct drm_crtc *crtc)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002527{
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03002528 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjälä46ba6142013-09-10 11:40:40 +03002529 struct drm_device *dev = crtc->dev;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002530 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak820c1982013-12-17 14:46:36 +02002531 struct ilk_wm_maximums max;
2532 struct ilk_pipe_wm_parameters params = {};
2533 struct ilk_wm_values results = {};
Ville Syrjälä77c122b2013-08-06 22:24:04 +03002534 enum intel_ddb_partitioning partitioning;
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03002535 struct intel_pipe_wm pipe_wm = {};
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002536 struct intel_pipe_wm lp_wm_1_2 = {}, lp_wm_5_6 = {}, *best_lp_wm;
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03002537 struct intel_wm_config config = {};
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002538
Imre Deak820c1982013-12-17 14:46:36 +02002539 ilk_compute_wm_parameters(crtc, &params, &config);
Paulo Zanoni861f3382013-05-31 10:19:21 -03002540
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03002541 intel_compute_pipe_wm(crtc, &params, &pipe_wm);
2542
2543 if (!memcmp(&intel_crtc->wm.active, &pipe_wm, sizeof(pipe_wm)))
2544 return;
2545
2546 intel_crtc->wm.active = pipe_wm;
2547
Ville Syrjälä34982fe2013-10-09 19:18:09 +03002548 ilk_compute_wm_maximums(dev, 1, &config, INTEL_DDB_PART_1_2, &max);
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02002549 ilk_wm_merge(dev, &config, &max, &lp_wm_1_2);
Ville Syrjälä0362c782013-10-09 19:17:57 +03002550
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03002551 /* 5/6 split only in single pipe config on IVB+ */
Ville Syrjäläec98c8d2013-10-11 15:26:26 +03002552 if (INTEL_INFO(dev)->gen >= 7 &&
2553 config.num_pipes_active == 1 && config.sprites_enabled) {
Ville Syrjälä34982fe2013-10-09 19:18:09 +03002554 ilk_compute_wm_maximums(dev, 1, &config, INTEL_DDB_PART_5_6, &max);
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02002555 ilk_wm_merge(dev, &config, &max, &lp_wm_5_6);
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03002556
Imre Deak820c1982013-12-17 14:46:36 +02002557 best_lp_wm = ilk_find_best_result(dev, &lp_wm_1_2, &lp_wm_5_6);
Paulo Zanoni861f3382013-05-31 10:19:21 -03002558 } else {
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002559 best_lp_wm = &lp_wm_1_2;
Paulo Zanoni861f3382013-05-31 10:19:21 -03002560 }
2561
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002562 partitioning = (best_lp_wm == &lp_wm_1_2) ?
Ville Syrjälä77c122b2013-08-06 22:24:04 +03002563 INTEL_DDB_PART_1_2 : INTEL_DDB_PART_5_6;
Paulo Zanoni861f3382013-05-31 10:19:21 -03002564
Imre Deak820c1982013-12-17 14:46:36 +02002565 ilk_compute_wm_results(dev, best_lp_wm, partitioning, &results);
Ville Syrjälä609cede2013-10-09 19:18:03 +03002566
Imre Deak820c1982013-12-17 14:46:36 +02002567 ilk_write_wm_values(dev_priv, &results);
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002568}
2569
Imre Deak820c1982013-12-17 14:46:36 +02002570static void ilk_update_sprite_wm(struct drm_plane *plane,
Ville Syrjäläadf3d352013-08-06 22:24:11 +03002571 struct drm_crtc *crtc,
Paulo Zanoni526682e2013-05-24 11:59:18 -03002572 uint32_t sprite_width, int pixel_size,
Ville Syrjäläbdd57d02013-07-05 11:57:13 +03002573 bool enabled, bool scaled)
Paulo Zanoni526682e2013-05-24 11:59:18 -03002574{
Ville Syrjälä8553c182013-12-05 15:51:39 +02002575 struct drm_device *dev = plane->dev;
Ville Syrjäläadf3d352013-08-06 22:24:11 +03002576 struct intel_plane *intel_plane = to_intel_plane(plane);
Paulo Zanoni526682e2013-05-24 11:59:18 -03002577
Ville Syrjäläadf3d352013-08-06 22:24:11 +03002578 intel_plane->wm.enabled = enabled;
2579 intel_plane->wm.scaled = scaled;
2580 intel_plane->wm.horiz_pixels = sprite_width;
2581 intel_plane->wm.bytes_per_pixel = pixel_size;
Paulo Zanoni526682e2013-05-24 11:59:18 -03002582
Ville Syrjälä8553c182013-12-05 15:51:39 +02002583 /*
2584 * IVB workaround: must disable low power watermarks for at least
2585 * one frame before enabling scaling. LP watermarks can be re-enabled
2586 * when scaling is disabled.
2587 *
2588 * WaCxSRDisabledForSpriteScaling:ivb
2589 */
2590 if (IS_IVYBRIDGE(dev) && scaled && ilk_disable_lp_wm(dev))
2591 intel_wait_for_vblank(dev, intel_plane->pipe);
2592
Imre Deak820c1982013-12-17 14:46:36 +02002593 ilk_update_wm(crtc);
Paulo Zanoni526682e2013-05-24 11:59:18 -03002594}
2595
Ville Syrjälä243e6a42013-10-14 14:55:24 +03002596static void ilk_pipe_wm_get_hw_state(struct drm_crtc *crtc)
2597{
2598 struct drm_device *dev = crtc->dev;
2599 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak820c1982013-12-17 14:46:36 +02002600 struct ilk_wm_values *hw = &dev_priv->wm.hw;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03002601 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2602 struct intel_pipe_wm *active = &intel_crtc->wm.active;
2603 enum pipe pipe = intel_crtc->pipe;
2604 static const unsigned int wm0_pipe_reg[] = {
2605 [PIPE_A] = WM0_PIPEA_ILK,
2606 [PIPE_B] = WM0_PIPEB_ILK,
2607 [PIPE_C] = WM0_PIPEC_IVB,
2608 };
2609
2610 hw->wm_pipe[pipe] = I915_READ(wm0_pipe_reg[pipe]);
Ville Syrjäläa42a5712014-01-07 16:14:08 +02002611 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Ville Syrjäläce0e0712013-12-05 15:51:36 +02002612 hw->wm_linetime[pipe] = I915_READ(PIPE_WM_LINETIME(pipe));
Ville Syrjälä243e6a42013-10-14 14:55:24 +03002613
2614 if (intel_crtc_active(crtc)) {
2615 u32 tmp = hw->wm_pipe[pipe];
2616
2617 /*
2618 * For active pipes LP0 watermark is marked as
2619 * enabled, and LP1+ watermaks as disabled since
2620 * we can't really reverse compute them in case
2621 * multiple pipes are active.
2622 */
2623 active->wm[0].enable = true;
2624 active->wm[0].pri_val = (tmp & WM0_PIPE_PLANE_MASK) >> WM0_PIPE_PLANE_SHIFT;
2625 active->wm[0].spr_val = (tmp & WM0_PIPE_SPRITE_MASK) >> WM0_PIPE_SPRITE_SHIFT;
2626 active->wm[0].cur_val = tmp & WM0_PIPE_CURSOR_MASK;
2627 active->linetime = hw->wm_linetime[pipe];
2628 } else {
2629 int level, max_level = ilk_wm_max_level(dev);
2630
2631 /*
2632 * For inactive pipes, all watermark levels
2633 * should be marked as enabled but zeroed,
2634 * which is what we'd compute them to.
2635 */
2636 for (level = 0; level <= max_level; level++)
2637 active->wm[level].enable = true;
2638 }
2639}
2640
2641void ilk_wm_get_hw_state(struct drm_device *dev)
2642{
2643 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak820c1982013-12-17 14:46:36 +02002644 struct ilk_wm_values *hw = &dev_priv->wm.hw;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03002645 struct drm_crtc *crtc;
2646
2647 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
2648 ilk_pipe_wm_get_hw_state(crtc);
2649
2650 hw->wm_lp[0] = I915_READ(WM1_LP_ILK);
2651 hw->wm_lp[1] = I915_READ(WM2_LP_ILK);
2652 hw->wm_lp[2] = I915_READ(WM3_LP_ILK);
2653
2654 hw->wm_lp_spr[0] = I915_READ(WM1S_LP_ILK);
2655 hw->wm_lp_spr[1] = I915_READ(WM2S_LP_IVB);
2656 hw->wm_lp_spr[2] = I915_READ(WM3S_LP_IVB);
2657
Ville Syrjäläa42a5712014-01-07 16:14:08 +02002658 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Ville Syrjäläac9545f2013-12-05 15:51:28 +02002659 hw->partitioning = (I915_READ(WM_MISC) & WM_MISC_DATA_PARTITION_5_6) ?
2660 INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
2661 else if (IS_IVYBRIDGE(dev))
2662 hw->partitioning = (I915_READ(DISP_ARB_CTL2) & DISP_DATA_PARTITION_5_6) ?
2663 INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03002664
2665 hw->enable_fbc_wm =
2666 !(I915_READ(DISP_ARB_CTL) & DISP_FBC_WM_DIS);
2667}
2668
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002669/**
2670 * intel_update_watermarks - update FIFO watermark values based on current modes
2671 *
2672 * Calculate watermark values for the various WM regs based on current mode
2673 * and plane configuration.
2674 *
2675 * There are several cases to deal with here:
2676 * - normal (i.e. non-self-refresh)
2677 * - self-refresh (SR) mode
2678 * - lines are large relative to FIFO size (buffer can hold up to 2)
2679 * - lines are small relative to FIFO size (buffer can hold more than 2
2680 * lines), so need to account for TLB latency
2681 *
2682 * The normal calculation is:
2683 * watermark = dotclock * bytes per pixel * latency
2684 * where latency is platform & configuration dependent (we assume pessimal
2685 * values here).
2686 *
2687 * The SR calculation is:
2688 * watermark = (trunc(latency/line time)+1) * surface width *
2689 * bytes per pixel
2690 * where
2691 * line time = htotal / dotclock
2692 * surface width = hdisplay for normal plane and 64 for cursor
2693 * and latency is assumed to be high, as above.
2694 *
2695 * The final value programmed to the register should always be rounded up,
2696 * and include an extra 2 entries to account for clock crossings.
2697 *
2698 * We don't use the sprite, so we can ignore that. And on Crestline we have
2699 * to set the non-SR watermarks to 8.
2700 */
Ville Syrjälä46ba6142013-09-10 11:40:40 +03002701void intel_update_watermarks(struct drm_crtc *crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002702{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03002703 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002704
2705 if (dev_priv->display.update_wm)
Ville Syrjälä46ba6142013-09-10 11:40:40 +03002706 dev_priv->display.update_wm(crtc);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002707}
2708
Ville Syrjäläadf3d352013-08-06 22:24:11 +03002709void intel_update_sprite_watermarks(struct drm_plane *plane,
2710 struct drm_crtc *crtc,
Paulo Zanoni4c4ff432013-05-24 11:59:17 -03002711 uint32_t sprite_width, int pixel_size,
Ville Syrjälä39db4a42013-08-06 22:24:00 +03002712 bool enabled, bool scaled)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002713{
Ville Syrjäläadf3d352013-08-06 22:24:11 +03002714 struct drm_i915_private *dev_priv = plane->dev->dev_private;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002715
2716 if (dev_priv->display.update_sprite_wm)
Ville Syrjäläadf3d352013-08-06 22:24:11 +03002717 dev_priv->display.update_sprite_wm(plane, crtc, sprite_width,
Ville Syrjälä39db4a42013-08-06 22:24:00 +03002718 pixel_size, enabled, scaled);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002719}
2720
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002721static struct drm_i915_gem_object *
2722intel_alloc_context_page(struct drm_device *dev)
2723{
2724 struct drm_i915_gem_object *ctx;
2725 int ret;
2726
2727 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2728
2729 ctx = i915_gem_alloc_object(dev, 4096);
2730 if (!ctx) {
2731 DRM_DEBUG("failed to alloc power context, RC6 disabled\n");
2732 return NULL;
2733 }
2734
Ben Widawskyc37e2202013-07-31 16:59:58 -07002735 ret = i915_gem_obj_ggtt_pin(ctx, 4096, true, false);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002736 if (ret) {
2737 DRM_ERROR("failed to pin power context: %d\n", ret);
2738 goto err_unref;
2739 }
2740
2741 ret = i915_gem_object_set_to_gtt_domain(ctx, 1);
2742 if (ret) {
2743 DRM_ERROR("failed to set-domain on power context: %d\n", ret);
2744 goto err_unpin;
2745 }
2746
2747 return ctx;
2748
2749err_unpin:
Ben Widawskyd7f46fc2013-12-06 14:10:55 -08002750 i915_gem_object_ggtt_unpin(ctx);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002751err_unref:
2752 drm_gem_object_unreference(&ctx->base);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002753 return NULL;
2754}
2755
Daniel Vetter92703882012-08-09 16:46:01 +02002756/**
2757 * Lock protecting IPS related data structures
Daniel Vetter92703882012-08-09 16:46:01 +02002758 */
2759DEFINE_SPINLOCK(mchdev_lock);
2760
2761/* Global for IPS driver to get at the current i915 device. Protected by
2762 * mchdev_lock. */
2763static struct drm_i915_private *i915_mch_dev;
2764
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002765bool ironlake_set_drps(struct drm_device *dev, u8 val)
2766{
2767 struct drm_i915_private *dev_priv = dev->dev_private;
2768 u16 rgvswctl;
2769
Daniel Vetter92703882012-08-09 16:46:01 +02002770 assert_spin_locked(&mchdev_lock);
2771
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002772 rgvswctl = I915_READ16(MEMSWCTL);
2773 if (rgvswctl & MEMCTL_CMD_STS) {
2774 DRM_DEBUG("gpu busy, RCS change rejected\n");
2775 return false; /* still busy with another command */
2776 }
2777
2778 rgvswctl = (MEMCTL_CMD_CHFREQ << MEMCTL_CMD_SHIFT) |
2779 (val << MEMCTL_FREQ_SHIFT) | MEMCTL_SFCAVM;
2780 I915_WRITE16(MEMSWCTL, rgvswctl);
2781 POSTING_READ16(MEMSWCTL);
2782
2783 rgvswctl |= MEMCTL_CMD_STS;
2784 I915_WRITE16(MEMSWCTL, rgvswctl);
2785
2786 return true;
2787}
2788
Daniel Vetter8090c6b2012-06-24 16:42:32 +02002789static void ironlake_enable_drps(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002790{
2791 struct drm_i915_private *dev_priv = dev->dev_private;
2792 u32 rgvmodectl = I915_READ(MEMMODECTL);
2793 u8 fmax, fmin, fstart, vstart;
2794
Daniel Vetter92703882012-08-09 16:46:01 +02002795 spin_lock_irq(&mchdev_lock);
2796
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002797 /* Enable temp reporting */
2798 I915_WRITE16(PMMISC, I915_READ(PMMISC) | MCPPCE_EN);
2799 I915_WRITE16(TSC1, I915_READ(TSC1) | TSE);
2800
2801 /* 100ms RC evaluation intervals */
2802 I915_WRITE(RCUPEI, 100000);
2803 I915_WRITE(RCDNEI, 100000);
2804
2805 /* Set max/min thresholds to 90ms and 80ms respectively */
2806 I915_WRITE(RCBMAXAVG, 90000);
2807 I915_WRITE(RCBMINAVG, 80000);
2808
2809 I915_WRITE(MEMIHYST, 1);
2810
2811 /* Set up min, max, and cur for interrupt handling */
2812 fmax = (rgvmodectl & MEMMODE_FMAX_MASK) >> MEMMODE_FMAX_SHIFT;
2813 fmin = (rgvmodectl & MEMMODE_FMIN_MASK);
2814 fstart = (rgvmodectl & MEMMODE_FSTART_MASK) >>
2815 MEMMODE_FSTART_SHIFT;
2816
2817 vstart = (I915_READ(PXVFREQ_BASE + (fstart * 4)) & PXVFREQ_PX_MASK) >>
2818 PXVFREQ_PX_SHIFT;
2819
Daniel Vetter20e4d402012-08-08 23:35:39 +02002820 dev_priv->ips.fmax = fmax; /* IPS callback will increase this */
2821 dev_priv->ips.fstart = fstart;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002822
Daniel Vetter20e4d402012-08-08 23:35:39 +02002823 dev_priv->ips.max_delay = fstart;
2824 dev_priv->ips.min_delay = fmin;
2825 dev_priv->ips.cur_delay = fstart;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002826
2827 DRM_DEBUG_DRIVER("fmax: %d, fmin: %d, fstart: %d\n",
2828 fmax, fmin, fstart);
2829
2830 I915_WRITE(MEMINTREN, MEMINT_CX_SUPR_EN | MEMINT_EVAL_CHG_EN);
2831
2832 /*
2833 * Interrupts will be enabled in ironlake_irq_postinstall
2834 */
2835
2836 I915_WRITE(VIDSTART, vstart);
2837 POSTING_READ(VIDSTART);
2838
2839 rgvmodectl |= MEMMODE_SWMODE_EN;
2840 I915_WRITE(MEMMODECTL, rgvmodectl);
2841
Daniel Vetter92703882012-08-09 16:46:01 +02002842 if (wait_for_atomic((I915_READ(MEMSWCTL) & MEMCTL_CMD_STS) == 0, 10))
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002843 DRM_ERROR("stuck trying to change perf mode\n");
Daniel Vetter92703882012-08-09 16:46:01 +02002844 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002845
2846 ironlake_set_drps(dev, fstart);
2847
Daniel Vetter20e4d402012-08-08 23:35:39 +02002848 dev_priv->ips.last_count1 = I915_READ(0x112e4) + I915_READ(0x112e8) +
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002849 I915_READ(0x112e0);
Daniel Vetter20e4d402012-08-08 23:35:39 +02002850 dev_priv->ips.last_time1 = jiffies_to_msecs(jiffies);
2851 dev_priv->ips.last_count2 = I915_READ(0x112f4);
2852 getrawmonotonic(&dev_priv->ips.last_time2);
Daniel Vetter92703882012-08-09 16:46:01 +02002853
2854 spin_unlock_irq(&mchdev_lock);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002855}
2856
Daniel Vetter8090c6b2012-06-24 16:42:32 +02002857static void ironlake_disable_drps(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002858{
2859 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter92703882012-08-09 16:46:01 +02002860 u16 rgvswctl;
2861
2862 spin_lock_irq(&mchdev_lock);
2863
2864 rgvswctl = I915_READ16(MEMSWCTL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002865
2866 /* Ack interrupts, disable EFC interrupt */
2867 I915_WRITE(MEMINTREN, I915_READ(MEMINTREN) & ~MEMINT_EVAL_CHG_EN);
2868 I915_WRITE(MEMINTRSTS, MEMINT_EVAL_CHG);
2869 I915_WRITE(DEIER, I915_READ(DEIER) & ~DE_PCU_EVENT);
2870 I915_WRITE(DEIIR, DE_PCU_EVENT);
2871 I915_WRITE(DEIMR, I915_READ(DEIMR) | DE_PCU_EVENT);
2872
2873 /* Go back to the starting frequency */
Daniel Vetter20e4d402012-08-08 23:35:39 +02002874 ironlake_set_drps(dev, dev_priv->ips.fstart);
Daniel Vetter92703882012-08-09 16:46:01 +02002875 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002876 rgvswctl |= MEMCTL_CMD_STS;
2877 I915_WRITE(MEMSWCTL, rgvswctl);
Daniel Vetter92703882012-08-09 16:46:01 +02002878 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002879
Daniel Vetter92703882012-08-09 16:46:01 +02002880 spin_unlock_irq(&mchdev_lock);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002881}
2882
Daniel Vetteracbe9472012-07-26 11:50:05 +02002883/* There's a funny hw issue where the hw returns all 0 when reading from
2884 * GEN6_RP_INTERRUPT_LIMITS. Hence we always need to compute the desired value
2885 * ourselves, instead of doing a rmw cycle (which might result in us clearing
2886 * all limits and the gpu stuck at whatever frequency it is at atm).
2887 */
Chris Wilson6917c7b2013-11-06 13:56:26 -02002888static u32 gen6_rps_limits(struct drm_i915_private *dev_priv, u8 val)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002889{
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01002890 u32 limits;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002891
Daniel Vetter20b46e52012-07-26 11:16:14 +02002892 /* Only set the down limit when we've reached the lowest level to avoid
2893 * getting more interrupts, otherwise leave this clear. This prevents a
2894 * race in the hw when coming out of rc6: There's a tiny window where
2895 * the hw runs at the minimal clock before selecting the desired
2896 * frequency, if the down threshold expires in that window we will not
2897 * receive a down interrupt. */
Chris Wilson6917c7b2013-11-06 13:56:26 -02002898 limits = dev_priv->rps.max_delay << 24;
2899 if (val <= dev_priv->rps.min_delay)
Daniel Vetterc6a828d2012-08-08 23:35:35 +02002900 limits |= dev_priv->rps.min_delay << 16;
Daniel Vetter20b46e52012-07-26 11:16:14 +02002901
2902 return limits;
2903}
2904
Chris Wilsondd75fdc2013-09-25 17:34:57 +01002905static void gen6_set_rps_thresholds(struct drm_i915_private *dev_priv, u8 val)
2906{
2907 int new_power;
2908
2909 new_power = dev_priv->rps.power;
2910 switch (dev_priv->rps.power) {
2911 case LOW_POWER:
2912 if (val > dev_priv->rps.rpe_delay + 1 && val > dev_priv->rps.cur_delay)
2913 new_power = BETWEEN;
2914 break;
2915
2916 case BETWEEN:
2917 if (val <= dev_priv->rps.rpe_delay && val < dev_priv->rps.cur_delay)
2918 new_power = LOW_POWER;
2919 else if (val >= dev_priv->rps.rp0_delay && val > dev_priv->rps.cur_delay)
2920 new_power = HIGH_POWER;
2921 break;
2922
2923 case HIGH_POWER:
2924 if (val < (dev_priv->rps.rp1_delay + dev_priv->rps.rp0_delay) >> 1 && val < dev_priv->rps.cur_delay)
2925 new_power = BETWEEN;
2926 break;
2927 }
2928 /* Max/min bins are special */
2929 if (val == dev_priv->rps.min_delay)
2930 new_power = LOW_POWER;
2931 if (val == dev_priv->rps.max_delay)
2932 new_power = HIGH_POWER;
2933 if (new_power == dev_priv->rps.power)
2934 return;
2935
2936 /* Note the units here are not exactly 1us, but 1280ns. */
2937 switch (new_power) {
2938 case LOW_POWER:
2939 /* Upclock if more than 95% busy over 16ms */
2940 I915_WRITE(GEN6_RP_UP_EI, 12500);
2941 I915_WRITE(GEN6_RP_UP_THRESHOLD, 11800);
2942
2943 /* Downclock if less than 85% busy over 32ms */
2944 I915_WRITE(GEN6_RP_DOWN_EI, 25000);
2945 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 21250);
2946
2947 I915_WRITE(GEN6_RP_CONTROL,
2948 GEN6_RP_MEDIA_TURBO |
2949 GEN6_RP_MEDIA_HW_NORMAL_MODE |
2950 GEN6_RP_MEDIA_IS_GFX |
2951 GEN6_RP_ENABLE |
2952 GEN6_RP_UP_BUSY_AVG |
2953 GEN6_RP_DOWN_IDLE_AVG);
2954 break;
2955
2956 case BETWEEN:
2957 /* Upclock if more than 90% busy over 13ms */
2958 I915_WRITE(GEN6_RP_UP_EI, 10250);
2959 I915_WRITE(GEN6_RP_UP_THRESHOLD, 9225);
2960
2961 /* Downclock if less than 75% busy over 32ms */
2962 I915_WRITE(GEN6_RP_DOWN_EI, 25000);
2963 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 18750);
2964
2965 I915_WRITE(GEN6_RP_CONTROL,
2966 GEN6_RP_MEDIA_TURBO |
2967 GEN6_RP_MEDIA_HW_NORMAL_MODE |
2968 GEN6_RP_MEDIA_IS_GFX |
2969 GEN6_RP_ENABLE |
2970 GEN6_RP_UP_BUSY_AVG |
2971 GEN6_RP_DOWN_IDLE_AVG);
2972 break;
2973
2974 case HIGH_POWER:
2975 /* Upclock if more than 85% busy over 10ms */
2976 I915_WRITE(GEN6_RP_UP_EI, 8000);
2977 I915_WRITE(GEN6_RP_UP_THRESHOLD, 6800);
2978
2979 /* Downclock if less than 60% busy over 32ms */
2980 I915_WRITE(GEN6_RP_DOWN_EI, 25000);
2981 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 15000);
2982
2983 I915_WRITE(GEN6_RP_CONTROL,
2984 GEN6_RP_MEDIA_TURBO |
2985 GEN6_RP_MEDIA_HW_NORMAL_MODE |
2986 GEN6_RP_MEDIA_IS_GFX |
2987 GEN6_RP_ENABLE |
2988 GEN6_RP_UP_BUSY_AVG |
2989 GEN6_RP_DOWN_IDLE_AVG);
2990 break;
2991 }
2992
2993 dev_priv->rps.power = new_power;
2994 dev_priv->rps.last_adj = 0;
2995}
2996
Daniel Vetter20b46e52012-07-26 11:16:14 +02002997void gen6_set_rps(struct drm_device *dev, u8 val)
2998{
2999 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01003000
Jesse Barnes4fc688c2012-11-02 11:14:01 -07003001 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Ben Widawsky79249632012-09-07 19:43:42 -07003002 WARN_ON(val > dev_priv->rps.max_delay);
3003 WARN_ON(val < dev_priv->rps.min_delay);
Daniel Vetter004777c2012-08-09 15:07:01 +02003004
Daniel Vetterc6a828d2012-08-08 23:35:35 +02003005 if (val == dev_priv->rps.cur_delay)
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01003006 return;
3007
Chris Wilsondd75fdc2013-09-25 17:34:57 +01003008 gen6_set_rps_thresholds(dev_priv, val);
3009
Rodrigo Vivi92bd1bf2013-03-25 17:55:49 -03003010 if (IS_HASWELL(dev))
3011 I915_WRITE(GEN6_RPNSWREQ,
3012 HSW_FREQUENCY(val));
3013 else
3014 I915_WRITE(GEN6_RPNSWREQ,
3015 GEN6_FREQUENCY(val) |
3016 GEN6_OFFSET(0) |
3017 GEN6_AGGRESSIVE_TURBO);
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01003018
3019 /* Make sure we continue to get interrupts
3020 * until we hit the minimum or maximum frequencies.
3021 */
Chris Wilson6917c7b2013-11-06 13:56:26 -02003022 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS,
3023 gen6_rps_limits(dev_priv, val));
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01003024
Ben Widawskyd5570a72012-09-07 19:43:41 -07003025 POSTING_READ(GEN6_RPNSWREQ);
3026
Daniel Vetterc6a828d2012-08-08 23:35:35 +02003027 dev_priv->rps.cur_delay = val;
Daniel Vetterbe2cde92012-08-30 13:26:48 +02003028
3029 trace_intel_gpu_freq_change(val * 50);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003030}
3031
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003032void gen6_rps_idle(struct drm_i915_private *dev_priv)
3033{
Damien Lespiau691bb712013-12-12 14:36:36 +00003034 struct drm_device *dev = dev_priv->dev;
3035
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003036 mutex_lock(&dev_priv->rps.hw_lock);
Chris Wilsonc0951f02013-10-10 21:58:50 +01003037 if (dev_priv->rps.enabled) {
Damien Lespiau691bb712013-12-12 14:36:36 +00003038 if (IS_VALLEYVIEW(dev))
Chris Wilsonc0951f02013-10-10 21:58:50 +01003039 valleyview_set_rps(dev_priv->dev, dev_priv->rps.min_delay);
3040 else
3041 gen6_set_rps(dev_priv->dev, dev_priv->rps.min_delay);
3042 dev_priv->rps.last_adj = 0;
3043 }
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003044 mutex_unlock(&dev_priv->rps.hw_lock);
3045}
3046
3047void gen6_rps_boost(struct drm_i915_private *dev_priv)
3048{
Damien Lespiau691bb712013-12-12 14:36:36 +00003049 struct drm_device *dev = dev_priv->dev;
3050
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003051 mutex_lock(&dev_priv->rps.hw_lock);
Chris Wilsonc0951f02013-10-10 21:58:50 +01003052 if (dev_priv->rps.enabled) {
Damien Lespiau691bb712013-12-12 14:36:36 +00003053 if (IS_VALLEYVIEW(dev))
Chris Wilsonc0951f02013-10-10 21:58:50 +01003054 valleyview_set_rps(dev_priv->dev, dev_priv->rps.max_delay);
3055 else
3056 gen6_set_rps(dev_priv->dev, dev_priv->rps.max_delay);
3057 dev_priv->rps.last_adj = 0;
3058 }
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003059 mutex_unlock(&dev_priv->rps.hw_lock);
3060}
3061
Jesse Barnes0a073b82013-04-17 15:54:58 -07003062void valleyview_set_rps(struct drm_device *dev, u8 val)
3063{
3064 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä7a670922013-06-25 19:21:06 +03003065
Jesse Barnes0a073b82013-04-17 15:54:58 -07003066 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
3067 WARN_ON(val > dev_priv->rps.max_delay);
3068 WARN_ON(val < dev_priv->rps.min_delay);
3069
Ville Syrjälä73008b92013-06-25 19:21:01 +03003070 DRM_DEBUG_DRIVER("GPU freq request from %d MHz (%u) to %d MHz (%u)\n",
Ville Syrjälä2ec38152013-11-05 22:42:29 +02003071 vlv_gpu_freq(dev_priv, dev_priv->rps.cur_delay),
Ville Syrjälä73008b92013-06-25 19:21:01 +03003072 dev_priv->rps.cur_delay,
Ville Syrjälä2ec38152013-11-05 22:42:29 +02003073 vlv_gpu_freq(dev_priv, val), val);
Jesse Barnes0a073b82013-04-17 15:54:58 -07003074
3075 if (val == dev_priv->rps.cur_delay)
3076 return;
3077
Jani Nikulaae992582013-05-22 15:36:19 +03003078 vlv_punit_write(dev_priv, PUNIT_REG_GPU_FREQ_REQ, val);
Jesse Barnes0a073b82013-04-17 15:54:58 -07003079
Ville Syrjälä80814ae2013-06-25 19:21:02 +03003080 dev_priv->rps.cur_delay = val;
Jesse Barnes0a073b82013-04-17 15:54:58 -07003081
Ville Syrjälä2ec38152013-11-05 22:42:29 +02003082 trace_intel_gpu_freq_change(vlv_gpu_freq(dev_priv, val));
Jesse Barnes0a073b82013-04-17 15:54:58 -07003083}
3084
Daniel Vetter44fc7d52013-07-12 22:43:27 +02003085static void gen6_disable_rps_interrupts(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003086{
3087 struct drm_i915_private *dev_priv = dev->dev_private;
3088
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003089 I915_WRITE(GEN6_PMINTRMSK, 0xffffffff);
Ben Widawsky48484052013-05-28 19:22:27 -07003090 I915_WRITE(GEN6_PMIER, I915_READ(GEN6_PMIER) & ~GEN6_PM_RPS_EVENTS);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003091 /* Complete PM interrupt masking here doesn't race with the rps work
3092 * item again unmasking PM interrupts because that is using a different
3093 * register (PMIMR) to mask PM interrupts. The only risk is in leaving
3094 * stale bits in PMIIR and PMIMR which gen6_enable_rps will clean up. */
3095
Daniel Vetter59cdb632013-07-04 23:35:28 +02003096 spin_lock_irq(&dev_priv->irq_lock);
Daniel Vetterc6a828d2012-08-08 23:35:35 +02003097 dev_priv->rps.pm_iir = 0;
Daniel Vetter59cdb632013-07-04 23:35:28 +02003098 spin_unlock_irq(&dev_priv->irq_lock);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003099
Ben Widawsky48484052013-05-28 19:22:27 -07003100 I915_WRITE(GEN6_PMIIR, GEN6_PM_RPS_EVENTS);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003101}
3102
Daniel Vetter44fc7d52013-07-12 22:43:27 +02003103static void gen6_disable_rps(struct drm_device *dev)
3104{
3105 struct drm_i915_private *dev_priv = dev->dev_private;
3106
3107 I915_WRITE(GEN6_RC_CONTROL, 0);
3108 I915_WRITE(GEN6_RPNSWREQ, 1 << 31);
3109
3110 gen6_disable_rps_interrupts(dev);
3111}
3112
Jesse Barnesd20d4f02013-04-23 10:09:28 -07003113static void valleyview_disable_rps(struct drm_device *dev)
3114{
3115 struct drm_i915_private *dev_priv = dev->dev_private;
3116
3117 I915_WRITE(GEN6_RC_CONTROL, 0);
Jesse Barnesd20d4f02013-04-23 10:09:28 -07003118
Daniel Vetter44fc7d52013-07-12 22:43:27 +02003119 gen6_disable_rps_interrupts(dev);
Jesse Barnesc9cddff2013-05-08 10:45:13 -07003120
3121 if (dev_priv->vlv_pctx) {
3122 drm_gem_object_unreference(&dev_priv->vlv_pctx->base);
3123 dev_priv->vlv_pctx = NULL;
3124 }
Jesse Barnesd20d4f02013-04-23 10:09:28 -07003125}
3126
Ben Widawskydc39fff2013-10-18 12:32:07 -07003127static void intel_print_rc6_info(struct drm_device *dev, u32 mode)
3128{
3129 if (IS_GEN6(dev))
3130 DRM_DEBUG_DRIVER("Sandybridge: deep RC6 disabled\n");
3131
3132 if (IS_HASWELL(dev))
3133 DRM_DEBUG_DRIVER("Haswell: only RC6 available\n");
3134
3135 DRM_INFO("Enabling RC6 states: RC6 %s, RC6p %s, RC6pp %s\n",
3136 (mode & GEN6_RC_CTL_RC6_ENABLE) ? "on" : "off",
3137 (mode & GEN6_RC_CTL_RC6p_ENABLE) ? "on" : "off",
3138 (mode & GEN6_RC_CTL_RC6pp_ENABLE) ? "on" : "off");
3139}
3140
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003141int intel_enable_rc6(const struct drm_device *dev)
3142{
Damien Lespiaueb4926e2013-06-07 17:41:14 +01003143 /* No RC6 before Ironlake */
3144 if (INTEL_INFO(dev)->gen < 5)
3145 return 0;
3146
Daniel Vetter456470e2012-08-08 23:35:40 +02003147 /* Respect the kernel parameter if it is set */
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003148 if (i915_enable_rc6 >= 0)
3149 return i915_enable_rc6;
3150
Chris Wilson6567d742012-11-10 10:00:06 +00003151 /* Disable RC6 on Ironlake */
3152 if (INTEL_INFO(dev)->gen == 5)
3153 return 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003154
Ben Widawskydc39fff2013-10-18 12:32:07 -07003155 if (IS_HASWELL(dev))
Daniel Vetter456470e2012-08-08 23:35:40 +02003156 return INTEL_RC6_ENABLE;
Daniel Vetter456470e2012-08-08 23:35:40 +02003157
3158 /* snb/ivb have more than one rc6 state. */
Ben Widawskydc39fff2013-10-18 12:32:07 -07003159 if (INTEL_INFO(dev)->gen == 6)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003160 return INTEL_RC6_ENABLE;
Daniel Vetter456470e2012-08-08 23:35:40 +02003161
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003162 return (INTEL_RC6_ENABLE | INTEL_RC6p_ENABLE);
3163}
3164
Daniel Vetter44fc7d52013-07-12 22:43:27 +02003165static void gen6_enable_rps_interrupts(struct drm_device *dev)
3166{
3167 struct drm_i915_private *dev_priv = dev->dev_private;
Mika Kuoppalaa9c1f902013-08-22 21:09:00 +03003168 u32 enabled_intrs;
Daniel Vetter44fc7d52013-07-12 22:43:27 +02003169
3170 spin_lock_irq(&dev_priv->irq_lock);
Daniel Vettera0b33352013-07-04 23:35:34 +02003171 WARN_ON(dev_priv->rps.pm_iir);
Paulo Zanoniedbfdb42013-08-06 18:57:13 -03003172 snb_enable_pm_irq(dev_priv, GEN6_PM_RPS_EVENTS);
Daniel Vetter44fc7d52013-07-12 22:43:27 +02003173 I915_WRITE(GEN6_PMIIR, GEN6_PM_RPS_EVENTS);
3174 spin_unlock_irq(&dev_priv->irq_lock);
Mika Kuoppalaa9c1f902013-08-22 21:09:00 +03003175
Vinit Azadfd547d22013-08-14 13:34:33 -07003176 /* only unmask PM interrupts we need. Mask all others. */
Mika Kuoppalaa9c1f902013-08-22 21:09:00 +03003177 enabled_intrs = GEN6_PM_RPS_EVENTS;
3178
3179 /* IVB and SNB hard hangs on looping batchbuffer
3180 * if GEN6_PM_UP_EI_EXPIRED is masked.
3181 */
3182 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
3183 enabled_intrs |= GEN6_PM_RP_UP_EI_EXPIRED;
3184
3185 I915_WRITE(GEN6_PMINTRMSK, ~enabled_intrs);
Daniel Vetter44fc7d52013-07-12 22:43:27 +02003186}
3187
Ben Widawsky6edee7f2013-11-02 21:07:52 -07003188static void gen8_enable_rps(struct drm_device *dev)
3189{
3190 struct drm_i915_private *dev_priv = dev->dev_private;
3191 struct intel_ring_buffer *ring;
3192 uint32_t rc6_mask = 0, rp_state_cap;
3193 int unused;
3194
3195 /* 1a: Software RC state - RC0 */
3196 I915_WRITE(GEN6_RC_STATE, 0);
3197
3198 /* 1c & 1d: Get forcewake during program sequence. Although the driver
3199 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
Deepak Sc8d9a592013-11-23 14:55:42 +05303200 gen6_gt_force_wake_get(dev_priv, FORCEWAKE_ALL);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07003201
3202 /* 2a: Disable RC states. */
3203 I915_WRITE(GEN6_RC_CONTROL, 0);
3204
3205 rp_state_cap = I915_READ(GEN6_RP_STATE_CAP);
3206
3207 /* 2b: Program RC6 thresholds.*/
3208 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16);
3209 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
3210 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
3211 for_each_ring(ring, dev_priv, unused)
3212 I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
3213 I915_WRITE(GEN6_RC_SLEEP, 0);
3214 I915_WRITE(GEN6_RC6_THRESHOLD, 50000); /* 50/125ms per EI */
3215
3216 /* 3: Enable RC6 */
3217 if (intel_enable_rc6(dev) & INTEL_RC6_ENABLE)
3218 rc6_mask = GEN6_RC_CTL_RC6_ENABLE;
3219 DRM_INFO("RC6 %s\n", (rc6_mask & GEN6_RC_CTL_RC6_ENABLE) ? "on" : "off");
3220 I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
3221 GEN6_RC_CTL_EI_MODE(1) |
3222 rc6_mask);
3223
3224 /* 4 Program defaults and thresholds for RPS*/
3225 I915_WRITE(GEN6_RPNSWREQ, HSW_FREQUENCY(10)); /* Request 500 MHz */
3226 I915_WRITE(GEN6_RC_VIDEO_FREQ, HSW_FREQUENCY(12)); /* Request 600 MHz */
3227 /* NB: Docs say 1s, and 1000000 - which aren't equivalent */
3228 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 100000000 / 128); /* 1 second timeout */
3229
3230 /* Docs recommend 900MHz, and 300 MHz respectively */
3231 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS,
3232 dev_priv->rps.max_delay << 24 |
3233 dev_priv->rps.min_delay << 16);
3234
3235 I915_WRITE(GEN6_RP_UP_THRESHOLD, 7600000 / 128); /* 76ms busyness per EI, 90% */
3236 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 31300000 / 128); /* 313ms busyness per EI, 70%*/
3237 I915_WRITE(GEN6_RP_UP_EI, 66000); /* 84.48ms, XXX: random? */
3238 I915_WRITE(GEN6_RP_DOWN_EI, 350000); /* 448ms, XXX: random? */
3239
3240 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
3241
3242 /* 5: Enable RPS */
3243 I915_WRITE(GEN6_RP_CONTROL,
3244 GEN6_RP_MEDIA_TURBO |
3245 GEN6_RP_MEDIA_HW_NORMAL_MODE |
3246 GEN6_RP_MEDIA_IS_GFX |
3247 GEN6_RP_ENABLE |
3248 GEN6_RP_UP_BUSY_AVG |
3249 GEN6_RP_DOWN_IDLE_AVG);
3250
3251 /* 6: Ring frequency + overclocking (our driver does this later */
3252
3253 gen6_set_rps(dev, (I915_READ(GEN6_GT_PERF_STATUS) & 0xff00) >> 8);
3254
3255 gen6_enable_rps_interrupts(dev);
3256
Deepak Sc8d9a592013-11-23 14:55:42 +05303257 gen6_gt_force_wake_put(dev_priv, FORCEWAKE_ALL);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07003258}
3259
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02003260static void gen6_enable_rps(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003261{
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02003262 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonb4519512012-05-11 14:29:30 +01003263 struct intel_ring_buffer *ring;
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01003264 u32 rp_state_cap;
3265 u32 gt_perf_status;
Ben Widawsky31643d52012-09-26 10:34:01 -07003266 u32 rc6vids, pcu_mbox, rc6_mask = 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003267 u32 gtfifodbg;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003268 int rc6_mode;
Ben Widawsky42c05262012-09-26 10:34:00 -07003269 int i, ret;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003270
Jesse Barnes4fc688c2012-11-02 11:14:01 -07003271 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02003272
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003273 /* Here begins a magic sequence of register writes to enable
3274 * auto-downclocking.
3275 *
3276 * Perhaps there might be some value in exposing these to
3277 * userspace...
3278 */
3279 I915_WRITE(GEN6_RC_STATE, 0);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003280
3281 /* Clear the DBG now so we don't confuse earlier errors */
3282 if ((gtfifodbg = I915_READ(GTFIFODBG))) {
3283 DRM_ERROR("GT fifo had a previous error %x\n", gtfifodbg);
3284 I915_WRITE(GTFIFODBG, gtfifodbg);
3285 }
3286
Deepak Sc8d9a592013-11-23 14:55:42 +05303287 gen6_gt_force_wake_get(dev_priv, FORCEWAKE_ALL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003288
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01003289 rp_state_cap = I915_READ(GEN6_RP_STATE_CAP);
3290 gt_perf_status = I915_READ(GEN6_GT_PERF_STATUS);
3291
Ben Widawsky31c77382013-04-05 14:29:22 -07003292 /* In units of 50MHz */
3293 dev_priv->rps.hw_max = dev_priv->rps.max_delay = rp_state_cap & 0xff;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01003294 dev_priv->rps.min_delay = (rp_state_cap >> 16) & 0xff;
3295 dev_priv->rps.rp1_delay = (rp_state_cap >> 8) & 0xff;
3296 dev_priv->rps.rp0_delay = (rp_state_cap >> 0) & 0xff;
3297 dev_priv->rps.rpe_delay = dev_priv->rps.rp1_delay;
Daniel Vetterc6a828d2012-08-08 23:35:35 +02003298 dev_priv->rps.cur_delay = 0;
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01003299
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003300 /* disable the counters and set deterministic thresholds */
3301 I915_WRITE(GEN6_RC_CONTROL, 0);
3302
3303 I915_WRITE(GEN6_RC1_WAKE_RATE_LIMIT, 1000 << 16);
3304 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16 | 30);
3305 I915_WRITE(GEN6_RC6pp_WAKE_RATE_LIMIT, 30);
3306 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
3307 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
3308
Chris Wilsonb4519512012-05-11 14:29:30 +01003309 for_each_ring(ring, dev_priv, i)
3310 I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003311
3312 I915_WRITE(GEN6_RC_SLEEP, 0);
3313 I915_WRITE(GEN6_RC1e_THRESHOLD, 1000);
Daniel Vetter29c78f62013-11-16 16:04:26 +01003314 if (IS_IVYBRIDGE(dev))
Stéphane Marchesin351aa562013-08-13 11:55:17 -07003315 I915_WRITE(GEN6_RC6_THRESHOLD, 125000);
3316 else
3317 I915_WRITE(GEN6_RC6_THRESHOLD, 50000);
Stéphane Marchesin0920a482013-01-29 19:41:59 -08003318 I915_WRITE(GEN6_RC6p_THRESHOLD, 150000);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003319 I915_WRITE(GEN6_RC6pp_THRESHOLD, 64000); /* unused */
3320
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03003321 /* Check if we are enabling RC6 */
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003322 rc6_mode = intel_enable_rc6(dev_priv->dev);
3323 if (rc6_mode & INTEL_RC6_ENABLE)
3324 rc6_mask |= GEN6_RC_CTL_RC6_ENABLE;
3325
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03003326 /* We don't use those on Haswell */
3327 if (!IS_HASWELL(dev)) {
3328 if (rc6_mode & INTEL_RC6p_ENABLE)
3329 rc6_mask |= GEN6_RC_CTL_RC6p_ENABLE;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003330
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03003331 if (rc6_mode & INTEL_RC6pp_ENABLE)
3332 rc6_mask |= GEN6_RC_CTL_RC6pp_ENABLE;
3333 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003334
Ben Widawskydc39fff2013-10-18 12:32:07 -07003335 intel_print_rc6_info(dev, rc6_mask);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003336
3337 I915_WRITE(GEN6_RC_CONTROL,
3338 rc6_mask |
3339 GEN6_RC_CTL_EI_MODE(1) |
3340 GEN6_RC_CTL_HW_ENABLE);
3341
Chris Wilsondd75fdc2013-09-25 17:34:57 +01003342 /* Power down if completely idle for over 50ms */
3343 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 50000);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003344 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003345
Ben Widawsky42c05262012-09-26 10:34:00 -07003346 ret = sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_MIN_FREQ_TABLE, 0);
Ben Widawsky988b36e2013-04-23 17:33:02 -07003347 if (!ret) {
Ben Widawsky42c05262012-09-26 10:34:00 -07003348 pcu_mbox = 0;
3349 ret = sandybridge_pcode_read(dev_priv, GEN6_READ_OC_PARAMS, &pcu_mbox);
Ben Widawskya2b3fc02013-03-19 20:19:56 -07003350 if (!ret && (pcu_mbox & (1<<31))) { /* OC supported */
Ben Widawsky10e08492013-04-05 14:29:23 -07003351 DRM_DEBUG_DRIVER("Overclocking supported. Max: %dMHz, Overclock max: %dMHz\n",
Ben Widawskya2b3fc02013-03-19 20:19:56 -07003352 (dev_priv->rps.max_delay & 0xff) * 50,
3353 (pcu_mbox & 0xff) * 50);
Ben Widawsky31c77382013-04-05 14:29:22 -07003354 dev_priv->rps.hw_max = pcu_mbox & 0xff;
Ben Widawsky42c05262012-09-26 10:34:00 -07003355 }
3356 } else {
3357 DRM_DEBUG_DRIVER("Failed to set the min frequency\n");
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003358 }
3359
Chris Wilsondd75fdc2013-09-25 17:34:57 +01003360 dev_priv->rps.power = HIGH_POWER; /* force a reset */
3361 gen6_set_rps(dev_priv->dev, dev_priv->rps.min_delay);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003362
Daniel Vetter44fc7d52013-07-12 22:43:27 +02003363 gen6_enable_rps_interrupts(dev);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003364
Ben Widawsky31643d52012-09-26 10:34:01 -07003365 rc6vids = 0;
3366 ret = sandybridge_pcode_read(dev_priv, GEN6_PCODE_READ_RC6VIDS, &rc6vids);
3367 if (IS_GEN6(dev) && ret) {
3368 DRM_DEBUG_DRIVER("Couldn't check for BIOS workaround\n");
3369 } else if (IS_GEN6(dev) && (GEN6_DECODE_RC6_VID(rc6vids & 0xff) < 450)) {
3370 DRM_DEBUG_DRIVER("You should update your BIOS. Correcting minimum rc6 voltage (%dmV->%dmV)\n",
3371 GEN6_DECODE_RC6_VID(rc6vids & 0xff), 450);
3372 rc6vids &= 0xffff00;
3373 rc6vids |= GEN6_ENCODE_RC6_VID(450);
3374 ret = sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_RC6VIDS, rc6vids);
3375 if (ret)
3376 DRM_ERROR("Couldn't fix incorrect rc6 voltage\n");
3377 }
3378
Deepak Sc8d9a592013-11-23 14:55:42 +05303379 gen6_gt_force_wake_put(dev_priv, FORCEWAKE_ALL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003380}
3381
Paulo Zanonic67a4702013-08-19 13:18:09 -03003382void gen6_update_ring_freq(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003383{
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02003384 struct drm_i915_private *dev_priv = dev->dev_private;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003385 int min_freq = 15;
Chris Wilson3ebecd02013-04-12 19:10:13 +01003386 unsigned int gpu_freq;
3387 unsigned int max_ia_freq, min_ring_freq;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003388 int scaling_factor = 180;
Ben Widawskyeda79642013-10-07 17:15:48 -03003389 struct cpufreq_policy *policy;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003390
Jesse Barnes4fc688c2012-11-02 11:14:01 -07003391 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02003392
Ben Widawskyeda79642013-10-07 17:15:48 -03003393 policy = cpufreq_cpu_get(0);
3394 if (policy) {
3395 max_ia_freq = policy->cpuinfo.max_freq;
3396 cpufreq_cpu_put(policy);
3397 } else {
3398 /*
3399 * Default to measured freq if none found, PCU will ensure we
3400 * don't go over
3401 */
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003402 max_ia_freq = tsc_khz;
Ben Widawskyeda79642013-10-07 17:15:48 -03003403 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003404
3405 /* Convert from kHz to MHz */
3406 max_ia_freq /= 1000;
3407
Ben Widawsky153b4b952013-10-22 22:05:09 -07003408 min_ring_freq = I915_READ(DCLK) & 0xf;
Ben Widawskyf6aca452013-10-02 09:25:02 -07003409 /* convert DDR frequency from units of 266.6MHz to bandwidth */
3410 min_ring_freq = mult_frac(min_ring_freq, 8, 3);
Chris Wilson3ebecd02013-04-12 19:10:13 +01003411
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003412 /*
3413 * For each potential GPU frequency, load a ring frequency we'd like
3414 * to use for memory access. We do this by specifying the IA frequency
3415 * the PCU should use as a reference to determine the ring frequency.
3416 */
Daniel Vetterc6a828d2012-08-08 23:35:35 +02003417 for (gpu_freq = dev_priv->rps.max_delay; gpu_freq >= dev_priv->rps.min_delay;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003418 gpu_freq--) {
Daniel Vetterc6a828d2012-08-08 23:35:35 +02003419 int diff = dev_priv->rps.max_delay - gpu_freq;
Chris Wilson3ebecd02013-04-12 19:10:13 +01003420 unsigned int ia_freq = 0, ring_freq = 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003421
Ben Widawsky46c764d2013-11-02 21:07:49 -07003422 if (INTEL_INFO(dev)->gen >= 8) {
3423 /* max(2 * GT, DDR). NB: GT is 50MHz units */
3424 ring_freq = max(min_ring_freq, gpu_freq);
3425 } else if (IS_HASWELL(dev)) {
Ben Widawskyf6aca452013-10-02 09:25:02 -07003426 ring_freq = mult_frac(gpu_freq, 5, 4);
Chris Wilson3ebecd02013-04-12 19:10:13 +01003427 ring_freq = max(min_ring_freq, ring_freq);
3428 /* leave ia_freq as the default, chosen by cpufreq */
3429 } else {
3430 /* On older processors, there is no separate ring
3431 * clock domain, so in order to boost the bandwidth
3432 * of the ring, we need to upclock the CPU (ia_freq).
3433 *
3434 * For GPU frequencies less than 750MHz,
3435 * just use the lowest ring freq.
3436 */
3437 if (gpu_freq < min_freq)
3438 ia_freq = 800;
3439 else
3440 ia_freq = max_ia_freq - ((diff * scaling_factor) / 2);
3441 ia_freq = DIV_ROUND_CLOSEST(ia_freq, 100);
3442 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003443
Ben Widawsky42c05262012-09-26 10:34:00 -07003444 sandybridge_pcode_write(dev_priv,
3445 GEN6_PCODE_WRITE_MIN_FREQ_TABLE,
Chris Wilson3ebecd02013-04-12 19:10:13 +01003446 ia_freq << GEN6_PCODE_FREQ_IA_RATIO_SHIFT |
3447 ring_freq << GEN6_PCODE_FREQ_RING_RATIO_SHIFT |
3448 gpu_freq);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003449 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003450}
3451
Jesse Barnes0a073b82013-04-17 15:54:58 -07003452int valleyview_rps_max_freq(struct drm_i915_private *dev_priv)
3453{
3454 u32 val, rp0;
3455
Jani Nikula64936252013-05-22 15:36:20 +03003456 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FREQ_FUSE);
Jesse Barnes0a073b82013-04-17 15:54:58 -07003457
3458 rp0 = (val & FB_GFX_MAX_FREQ_FUSE_MASK) >> FB_GFX_MAX_FREQ_FUSE_SHIFT;
3459 /* Clamp to max */
3460 rp0 = min_t(u32, rp0, 0xea);
3461
3462 return rp0;
3463}
3464
3465static int valleyview_rps_rpe_freq(struct drm_i915_private *dev_priv)
3466{
3467 u32 val, rpe;
3468
Jani Nikula64936252013-05-22 15:36:20 +03003469 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FMAX_FUSE_LO);
Jesse Barnes0a073b82013-04-17 15:54:58 -07003470 rpe = (val & FB_FMAX_VMIN_FREQ_LO_MASK) >> FB_FMAX_VMIN_FREQ_LO_SHIFT;
Jani Nikula64936252013-05-22 15:36:20 +03003471 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FMAX_FUSE_HI);
Jesse Barnes0a073b82013-04-17 15:54:58 -07003472 rpe |= (val & FB_FMAX_VMIN_FREQ_HI_MASK) << 5;
3473
3474 return rpe;
3475}
3476
3477int valleyview_rps_min_freq(struct drm_i915_private *dev_priv)
3478{
Jani Nikula64936252013-05-22 15:36:20 +03003479 return vlv_punit_read(dev_priv, PUNIT_REG_GPU_LFM) & 0xff;
Jesse Barnes0a073b82013-04-17 15:54:58 -07003480}
3481
Jesse Barnesc9cddff2013-05-08 10:45:13 -07003482static void valleyview_setup_pctx(struct drm_device *dev)
3483{
3484 struct drm_i915_private *dev_priv = dev->dev_private;
3485 struct drm_i915_gem_object *pctx;
3486 unsigned long pctx_paddr;
3487 u32 pcbr;
3488 int pctx_size = 24*1024;
3489
3490 pcbr = I915_READ(VLV_PCBR);
3491 if (pcbr) {
3492 /* BIOS set it up already, grab the pre-alloc'd space */
3493 int pcbr_offset;
3494
3495 pcbr_offset = (pcbr & (~4095)) - dev_priv->mm.stolen_base;
3496 pctx = i915_gem_object_create_stolen_for_preallocated(dev_priv->dev,
3497 pcbr_offset,
Daniel Vetter190d6cd2013-07-04 13:06:28 +02003498 I915_GTT_OFFSET_NONE,
Jesse Barnesc9cddff2013-05-08 10:45:13 -07003499 pctx_size);
3500 goto out;
3501 }
3502
3503 /*
3504 * From the Gunit register HAS:
3505 * The Gfx driver is expected to program this register and ensure
3506 * proper allocation within Gfx stolen memory. For example, this
3507 * register should be programmed such than the PCBR range does not
3508 * overlap with other ranges, such as the frame buffer, protected
3509 * memory, or any other relevant ranges.
3510 */
3511 pctx = i915_gem_object_create_stolen(dev, pctx_size);
3512 if (!pctx) {
3513 DRM_DEBUG("not enough stolen space for PCTX, disabling\n");
3514 return;
3515 }
3516
3517 pctx_paddr = dev_priv->mm.stolen_base + pctx->stolen->start;
3518 I915_WRITE(VLV_PCBR, pctx_paddr);
3519
3520out:
3521 dev_priv->vlv_pctx = pctx;
3522}
3523
Jesse Barnes0a073b82013-04-17 15:54:58 -07003524static void valleyview_enable_rps(struct drm_device *dev)
3525{
3526 struct drm_i915_private *dev_priv = dev->dev_private;
3527 struct intel_ring_buffer *ring;
Jesse Barnesa2b23fe2013-09-19 09:33:13 -07003528 u32 gtfifodbg, val, rc6_mode = 0;
Jesse Barnes0a073b82013-04-17 15:54:58 -07003529 int i;
3530
3531 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
3532
3533 if ((gtfifodbg = I915_READ(GTFIFODBG))) {
Jesse Barnesf7d85c12013-09-27 10:40:54 -07003534 DRM_DEBUG_DRIVER("GT fifo had a previous error %x\n",
3535 gtfifodbg);
Jesse Barnes0a073b82013-04-17 15:54:58 -07003536 I915_WRITE(GTFIFODBG, gtfifodbg);
3537 }
3538
Jesse Barnesc9cddff2013-05-08 10:45:13 -07003539 valleyview_setup_pctx(dev);
3540
Deepak Sc8d9a592013-11-23 14:55:42 +05303541 /* If VLV, Forcewake all wells, else re-direct to regular path */
3542 gen6_gt_force_wake_get(dev_priv, FORCEWAKE_ALL);
Jesse Barnes0a073b82013-04-17 15:54:58 -07003543
3544 I915_WRITE(GEN6_RP_UP_THRESHOLD, 59400);
3545 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 245000);
3546 I915_WRITE(GEN6_RP_UP_EI, 66000);
3547 I915_WRITE(GEN6_RP_DOWN_EI, 350000);
3548
3549 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
3550
3551 I915_WRITE(GEN6_RP_CONTROL,
3552 GEN6_RP_MEDIA_TURBO |
3553 GEN6_RP_MEDIA_HW_NORMAL_MODE |
3554 GEN6_RP_MEDIA_IS_GFX |
3555 GEN6_RP_ENABLE |
3556 GEN6_RP_UP_BUSY_AVG |
3557 GEN6_RP_DOWN_IDLE_CONT);
3558
3559 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 0x00280000);
3560 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
3561 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
3562
3563 for_each_ring(ring, dev_priv, i)
3564 I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
3565
Jesse Barnes2f0aa302013-11-15 09:32:11 -08003566 I915_WRITE(GEN6_RC6_THRESHOLD, 0x557);
Jesse Barnes0a073b82013-04-17 15:54:58 -07003567
3568 /* allows RC6 residency counter to work */
Jesse Barnes49798eb2013-09-26 17:55:57 -07003569 I915_WRITE(VLV_COUNTER_CONTROL,
3570 _MASKED_BIT_ENABLE(VLV_COUNT_RANGE_HIGH |
3571 VLV_MEDIA_RC6_COUNT_EN |
3572 VLV_RENDER_RC6_COUNT_EN));
Jesse Barnesa2b23fe2013-09-19 09:33:13 -07003573 if (intel_enable_rc6(dev) & INTEL_RC6_ENABLE)
Jesse Barnes6b88f292013-11-15 09:32:12 -08003574 rc6_mode = GEN7_RC_CTL_TO_MODE | VLV_RC_CTL_CTX_RST_PARALLEL;
Ben Widawskydc39fff2013-10-18 12:32:07 -07003575
3576 intel_print_rc6_info(dev, rc6_mode);
3577
Jesse Barnesa2b23fe2013-09-19 09:33:13 -07003578 I915_WRITE(GEN6_RC_CONTROL, rc6_mode);
Jesse Barnes0a073b82013-04-17 15:54:58 -07003579
Jani Nikula64936252013-05-22 15:36:20 +03003580 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
Jesse Barnes0a073b82013-04-17 15:54:58 -07003581
3582 DRM_DEBUG_DRIVER("GPLL enabled? %s\n", val & 0x10 ? "yes" : "no");
3583 DRM_DEBUG_DRIVER("GPU status: 0x%08x\n", val);
3584
Jesse Barnes0a073b82013-04-17 15:54:58 -07003585 dev_priv->rps.cur_delay = (val >> 8) & 0xff;
Ville Syrjälä73008b92013-06-25 19:21:01 +03003586 DRM_DEBUG_DRIVER("current GPU freq: %d MHz (%u)\n",
Ville Syrjälä2ec38152013-11-05 22:42:29 +02003587 vlv_gpu_freq(dev_priv, dev_priv->rps.cur_delay),
Ville Syrjälä73008b92013-06-25 19:21:01 +03003588 dev_priv->rps.cur_delay);
Jesse Barnes0a073b82013-04-17 15:54:58 -07003589
3590 dev_priv->rps.max_delay = valleyview_rps_max_freq(dev_priv);
3591 dev_priv->rps.hw_max = dev_priv->rps.max_delay;
Ville Syrjälä73008b92013-06-25 19:21:01 +03003592 DRM_DEBUG_DRIVER("max GPU freq: %d MHz (%u)\n",
Ville Syrjälä2ec38152013-11-05 22:42:29 +02003593 vlv_gpu_freq(dev_priv, dev_priv->rps.max_delay),
Ville Syrjälä73008b92013-06-25 19:21:01 +03003594 dev_priv->rps.max_delay);
Jesse Barnes0a073b82013-04-17 15:54:58 -07003595
Ville Syrjälä73008b92013-06-25 19:21:01 +03003596 dev_priv->rps.rpe_delay = valleyview_rps_rpe_freq(dev_priv);
3597 DRM_DEBUG_DRIVER("RPe GPU freq: %d MHz (%u)\n",
Ville Syrjälä2ec38152013-11-05 22:42:29 +02003598 vlv_gpu_freq(dev_priv, dev_priv->rps.rpe_delay),
Ville Syrjälä73008b92013-06-25 19:21:01 +03003599 dev_priv->rps.rpe_delay);
Jesse Barnes0a073b82013-04-17 15:54:58 -07003600
Ville Syrjälä73008b92013-06-25 19:21:01 +03003601 dev_priv->rps.min_delay = valleyview_rps_min_freq(dev_priv);
3602 DRM_DEBUG_DRIVER("min GPU freq: %d MHz (%u)\n",
Ville Syrjälä2ec38152013-11-05 22:42:29 +02003603 vlv_gpu_freq(dev_priv, dev_priv->rps.min_delay),
Ville Syrjälä73008b92013-06-25 19:21:01 +03003604 dev_priv->rps.min_delay);
Jesse Barnes0a073b82013-04-17 15:54:58 -07003605
Ville Syrjälä73008b92013-06-25 19:21:01 +03003606 DRM_DEBUG_DRIVER("setting GPU freq to %d MHz (%u)\n",
Ville Syrjälä2ec38152013-11-05 22:42:29 +02003607 vlv_gpu_freq(dev_priv, dev_priv->rps.rpe_delay),
Ville Syrjälä73008b92013-06-25 19:21:01 +03003608 dev_priv->rps.rpe_delay);
Jesse Barnes0a073b82013-04-17 15:54:58 -07003609
Ville Syrjälä73008b92013-06-25 19:21:01 +03003610 valleyview_set_rps(dev_priv->dev, dev_priv->rps.rpe_delay);
Jesse Barnes0a073b82013-04-17 15:54:58 -07003611
Daniel Vetter44fc7d52013-07-12 22:43:27 +02003612 gen6_enable_rps_interrupts(dev);
Jesse Barnes0a073b82013-04-17 15:54:58 -07003613
Deepak Sc8d9a592013-11-23 14:55:42 +05303614 gen6_gt_force_wake_put(dev_priv, FORCEWAKE_ALL);
Jesse Barnes0a073b82013-04-17 15:54:58 -07003615}
3616
Daniel Vetter930ebb42012-06-29 23:32:16 +02003617void ironlake_teardown_rc6(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003618{
3619 struct drm_i915_private *dev_priv = dev->dev_private;
3620
Daniel Vetter3e373942012-11-02 19:55:04 +01003621 if (dev_priv->ips.renderctx) {
Ben Widawskyd7f46fc2013-12-06 14:10:55 -08003622 i915_gem_object_ggtt_unpin(dev_priv->ips.renderctx);
Daniel Vetter3e373942012-11-02 19:55:04 +01003623 drm_gem_object_unreference(&dev_priv->ips.renderctx->base);
3624 dev_priv->ips.renderctx = NULL;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003625 }
3626
Daniel Vetter3e373942012-11-02 19:55:04 +01003627 if (dev_priv->ips.pwrctx) {
Ben Widawskyd7f46fc2013-12-06 14:10:55 -08003628 i915_gem_object_ggtt_unpin(dev_priv->ips.pwrctx);
Daniel Vetter3e373942012-11-02 19:55:04 +01003629 drm_gem_object_unreference(&dev_priv->ips.pwrctx->base);
3630 dev_priv->ips.pwrctx = NULL;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003631 }
3632}
3633
Daniel Vetter930ebb42012-06-29 23:32:16 +02003634static void ironlake_disable_rc6(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003635{
3636 struct drm_i915_private *dev_priv = dev->dev_private;
3637
3638 if (I915_READ(PWRCTXA)) {
3639 /* Wake the GPU, prevent RC6, then restore RSTDBYCTL */
3640 I915_WRITE(RSTDBYCTL, I915_READ(RSTDBYCTL) | RCX_SW_EXIT);
3641 wait_for(((I915_READ(RSTDBYCTL) & RSX_STATUS_MASK) == RSX_STATUS_ON),
3642 50);
3643
3644 I915_WRITE(PWRCTXA, 0);
3645 POSTING_READ(PWRCTXA);
3646
3647 I915_WRITE(RSTDBYCTL, I915_READ(RSTDBYCTL) & ~RCX_SW_EXIT);
3648 POSTING_READ(RSTDBYCTL);
3649 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003650}
3651
3652static int ironlake_setup_rc6(struct drm_device *dev)
3653{
3654 struct drm_i915_private *dev_priv = dev->dev_private;
3655
Daniel Vetter3e373942012-11-02 19:55:04 +01003656 if (dev_priv->ips.renderctx == NULL)
3657 dev_priv->ips.renderctx = intel_alloc_context_page(dev);
3658 if (!dev_priv->ips.renderctx)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003659 return -ENOMEM;
3660
Daniel Vetter3e373942012-11-02 19:55:04 +01003661 if (dev_priv->ips.pwrctx == NULL)
3662 dev_priv->ips.pwrctx = intel_alloc_context_page(dev);
3663 if (!dev_priv->ips.pwrctx) {
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003664 ironlake_teardown_rc6(dev);
3665 return -ENOMEM;
3666 }
3667
3668 return 0;
3669}
3670
Daniel Vetter930ebb42012-06-29 23:32:16 +02003671static void ironlake_enable_rc6(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003672{
3673 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter6d90c952012-04-26 23:28:05 +02003674 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Chris Wilson3e960502012-11-27 16:22:54 +00003675 bool was_interruptible;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003676 int ret;
3677
3678 /* rc6 disabled by default due to repeated reports of hanging during
3679 * boot and resume.
3680 */
3681 if (!intel_enable_rc6(dev))
3682 return;
3683
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02003684 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
3685
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003686 ret = ironlake_setup_rc6(dev);
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02003687 if (ret)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003688 return;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003689
Chris Wilson3e960502012-11-27 16:22:54 +00003690 was_interruptible = dev_priv->mm.interruptible;
3691 dev_priv->mm.interruptible = false;
3692
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003693 /*
3694 * GPU can automatically power down the render unit if given a page
3695 * to save state.
3696 */
Daniel Vetter6d90c952012-04-26 23:28:05 +02003697 ret = intel_ring_begin(ring, 6);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003698 if (ret) {
3699 ironlake_teardown_rc6(dev);
Chris Wilson3e960502012-11-27 16:22:54 +00003700 dev_priv->mm.interruptible = was_interruptible;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003701 return;
3702 }
3703
Daniel Vetter6d90c952012-04-26 23:28:05 +02003704 intel_ring_emit(ring, MI_SUSPEND_FLUSH | MI_SUSPEND_FLUSH_EN);
3705 intel_ring_emit(ring, MI_SET_CONTEXT);
Ben Widawskyf343c5f2013-07-05 14:41:04 -07003706 intel_ring_emit(ring, i915_gem_obj_ggtt_offset(dev_priv->ips.renderctx) |
Daniel Vetter6d90c952012-04-26 23:28:05 +02003707 MI_MM_SPACE_GTT |
3708 MI_SAVE_EXT_STATE_EN |
3709 MI_RESTORE_EXT_STATE_EN |
3710 MI_RESTORE_INHIBIT);
3711 intel_ring_emit(ring, MI_SUSPEND_FLUSH);
3712 intel_ring_emit(ring, MI_NOOP);
3713 intel_ring_emit(ring, MI_FLUSH);
3714 intel_ring_advance(ring);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003715
3716 /*
3717 * Wait for the command parser to advance past MI_SET_CONTEXT. The HW
3718 * does an implicit flush, combined with MI_FLUSH above, it should be
3719 * safe to assume that renderctx is valid
3720 */
Chris Wilson3e960502012-11-27 16:22:54 +00003721 ret = intel_ring_idle(ring);
3722 dev_priv->mm.interruptible = was_interruptible;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003723 if (ret) {
Jani Nikuladef27a52013-03-12 10:49:19 +02003724 DRM_ERROR("failed to enable ironlake power savings\n");
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003725 ironlake_teardown_rc6(dev);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003726 return;
3727 }
3728
Ben Widawskyf343c5f2013-07-05 14:41:04 -07003729 I915_WRITE(PWRCTXA, i915_gem_obj_ggtt_offset(dev_priv->ips.pwrctx) | PWRCTX_EN);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003730 I915_WRITE(RSTDBYCTL, I915_READ(RSTDBYCTL) & ~RCX_SW_EXIT);
Ben Widawskydc39fff2013-10-18 12:32:07 -07003731
3732 intel_print_rc6_info(dev, INTEL_RC6_ENABLE);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003733}
3734
Eugeni Dodonovdde18882012-04-18 15:29:24 -03003735static unsigned long intel_pxfreq(u32 vidfreq)
3736{
3737 unsigned long freq;
3738 int div = (vidfreq & 0x3f0000) >> 16;
3739 int post = (vidfreq & 0x3000) >> 12;
3740 int pre = (vidfreq & 0x7);
3741
3742 if (!pre)
3743 return 0;
3744
3745 freq = ((div * 133333) / ((1<<post) * pre));
3746
3747 return freq;
3748}
3749
Daniel Vettereb48eb02012-04-26 23:28:12 +02003750static const struct cparams {
3751 u16 i;
3752 u16 t;
3753 u16 m;
3754 u16 c;
3755} cparams[] = {
3756 { 1, 1333, 301, 28664 },
3757 { 1, 1066, 294, 24460 },
3758 { 1, 800, 294, 25192 },
3759 { 0, 1333, 276, 27605 },
3760 { 0, 1066, 276, 27605 },
3761 { 0, 800, 231, 23784 },
3762};
3763
Chris Wilsonf531dcb2012-09-25 10:16:12 +01003764static unsigned long __i915_chipset_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02003765{
3766 u64 total_count, diff, ret;
3767 u32 count1, count2, count3, m = 0, c = 0;
3768 unsigned long now = jiffies_to_msecs(jiffies), diff1;
3769 int i;
3770
Daniel Vetter02d71952012-08-09 16:44:54 +02003771 assert_spin_locked(&mchdev_lock);
3772
Daniel Vetter20e4d402012-08-08 23:35:39 +02003773 diff1 = now - dev_priv->ips.last_time1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02003774
3775 /* Prevent division-by-zero if we are asking too fast.
3776 * Also, we don't get interesting results if we are polling
3777 * faster than once in 10ms, so just return the saved value
3778 * in such cases.
3779 */
3780 if (diff1 <= 10)
Daniel Vetter20e4d402012-08-08 23:35:39 +02003781 return dev_priv->ips.chipset_power;
Daniel Vettereb48eb02012-04-26 23:28:12 +02003782
3783 count1 = I915_READ(DMIEC);
3784 count2 = I915_READ(DDREC);
3785 count3 = I915_READ(CSIEC);
3786
3787 total_count = count1 + count2 + count3;
3788
3789 /* FIXME: handle per-counter overflow */
Daniel Vetter20e4d402012-08-08 23:35:39 +02003790 if (total_count < dev_priv->ips.last_count1) {
3791 diff = ~0UL - dev_priv->ips.last_count1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02003792 diff += total_count;
3793 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +02003794 diff = total_count - dev_priv->ips.last_count1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02003795 }
3796
3797 for (i = 0; i < ARRAY_SIZE(cparams); i++) {
Daniel Vetter20e4d402012-08-08 23:35:39 +02003798 if (cparams[i].i == dev_priv->ips.c_m &&
3799 cparams[i].t == dev_priv->ips.r_t) {
Daniel Vettereb48eb02012-04-26 23:28:12 +02003800 m = cparams[i].m;
3801 c = cparams[i].c;
3802 break;
3803 }
3804 }
3805
3806 diff = div_u64(diff, diff1);
3807 ret = ((m * diff) + c);
3808 ret = div_u64(ret, 10);
3809
Daniel Vetter20e4d402012-08-08 23:35:39 +02003810 dev_priv->ips.last_count1 = total_count;
3811 dev_priv->ips.last_time1 = now;
Daniel Vettereb48eb02012-04-26 23:28:12 +02003812
Daniel Vetter20e4d402012-08-08 23:35:39 +02003813 dev_priv->ips.chipset_power = ret;
Daniel Vettereb48eb02012-04-26 23:28:12 +02003814
3815 return ret;
3816}
3817
Chris Wilsonf531dcb2012-09-25 10:16:12 +01003818unsigned long i915_chipset_val(struct drm_i915_private *dev_priv)
3819{
3820 unsigned long val;
3821
3822 if (dev_priv->info->gen != 5)
3823 return 0;
3824
3825 spin_lock_irq(&mchdev_lock);
3826
3827 val = __i915_chipset_val(dev_priv);
3828
3829 spin_unlock_irq(&mchdev_lock);
3830
3831 return val;
3832}
3833
Daniel Vettereb48eb02012-04-26 23:28:12 +02003834unsigned long i915_mch_val(struct drm_i915_private *dev_priv)
3835{
3836 unsigned long m, x, b;
3837 u32 tsfs;
3838
3839 tsfs = I915_READ(TSFS);
3840
3841 m = ((tsfs & TSFS_SLOPE_MASK) >> TSFS_SLOPE_SHIFT);
3842 x = I915_READ8(TR1);
3843
3844 b = tsfs & TSFS_INTR_MASK;
3845
3846 return ((m * x) / 127) - b;
3847}
3848
3849static u16 pvid_to_extvid(struct drm_i915_private *dev_priv, u8 pxvid)
3850{
3851 static const struct v_table {
3852 u16 vd; /* in .1 mil */
3853 u16 vm; /* in .1 mil */
3854 } v_table[] = {
3855 { 0, 0, },
3856 { 375, 0, },
3857 { 500, 0, },
3858 { 625, 0, },
3859 { 750, 0, },
3860 { 875, 0, },
3861 { 1000, 0, },
3862 { 1125, 0, },
3863 { 4125, 3000, },
3864 { 4125, 3000, },
3865 { 4125, 3000, },
3866 { 4125, 3000, },
3867 { 4125, 3000, },
3868 { 4125, 3000, },
3869 { 4125, 3000, },
3870 { 4125, 3000, },
3871 { 4125, 3000, },
3872 { 4125, 3000, },
3873 { 4125, 3000, },
3874 { 4125, 3000, },
3875 { 4125, 3000, },
3876 { 4125, 3000, },
3877 { 4125, 3000, },
3878 { 4125, 3000, },
3879 { 4125, 3000, },
3880 { 4125, 3000, },
3881 { 4125, 3000, },
3882 { 4125, 3000, },
3883 { 4125, 3000, },
3884 { 4125, 3000, },
3885 { 4125, 3000, },
3886 { 4125, 3000, },
3887 { 4250, 3125, },
3888 { 4375, 3250, },
3889 { 4500, 3375, },
3890 { 4625, 3500, },
3891 { 4750, 3625, },
3892 { 4875, 3750, },
3893 { 5000, 3875, },
3894 { 5125, 4000, },
3895 { 5250, 4125, },
3896 { 5375, 4250, },
3897 { 5500, 4375, },
3898 { 5625, 4500, },
3899 { 5750, 4625, },
3900 { 5875, 4750, },
3901 { 6000, 4875, },
3902 { 6125, 5000, },
3903 { 6250, 5125, },
3904 { 6375, 5250, },
3905 { 6500, 5375, },
3906 { 6625, 5500, },
3907 { 6750, 5625, },
3908 { 6875, 5750, },
3909 { 7000, 5875, },
3910 { 7125, 6000, },
3911 { 7250, 6125, },
3912 { 7375, 6250, },
3913 { 7500, 6375, },
3914 { 7625, 6500, },
3915 { 7750, 6625, },
3916 { 7875, 6750, },
3917 { 8000, 6875, },
3918 { 8125, 7000, },
3919 { 8250, 7125, },
3920 { 8375, 7250, },
3921 { 8500, 7375, },
3922 { 8625, 7500, },
3923 { 8750, 7625, },
3924 { 8875, 7750, },
3925 { 9000, 7875, },
3926 { 9125, 8000, },
3927 { 9250, 8125, },
3928 { 9375, 8250, },
3929 { 9500, 8375, },
3930 { 9625, 8500, },
3931 { 9750, 8625, },
3932 { 9875, 8750, },
3933 { 10000, 8875, },
3934 { 10125, 9000, },
3935 { 10250, 9125, },
3936 { 10375, 9250, },
3937 { 10500, 9375, },
3938 { 10625, 9500, },
3939 { 10750, 9625, },
3940 { 10875, 9750, },
3941 { 11000, 9875, },
3942 { 11125, 10000, },
3943 { 11250, 10125, },
3944 { 11375, 10250, },
3945 { 11500, 10375, },
3946 { 11625, 10500, },
3947 { 11750, 10625, },
3948 { 11875, 10750, },
3949 { 12000, 10875, },
3950 { 12125, 11000, },
3951 { 12250, 11125, },
3952 { 12375, 11250, },
3953 { 12500, 11375, },
3954 { 12625, 11500, },
3955 { 12750, 11625, },
3956 { 12875, 11750, },
3957 { 13000, 11875, },
3958 { 13125, 12000, },
3959 { 13250, 12125, },
3960 { 13375, 12250, },
3961 { 13500, 12375, },
3962 { 13625, 12500, },
3963 { 13750, 12625, },
3964 { 13875, 12750, },
3965 { 14000, 12875, },
3966 { 14125, 13000, },
3967 { 14250, 13125, },
3968 { 14375, 13250, },
3969 { 14500, 13375, },
3970 { 14625, 13500, },
3971 { 14750, 13625, },
3972 { 14875, 13750, },
3973 { 15000, 13875, },
3974 { 15125, 14000, },
3975 { 15250, 14125, },
3976 { 15375, 14250, },
3977 { 15500, 14375, },
3978 { 15625, 14500, },
3979 { 15750, 14625, },
3980 { 15875, 14750, },
3981 { 16000, 14875, },
3982 { 16125, 15000, },
3983 };
3984 if (dev_priv->info->is_mobile)
3985 return v_table[pxvid].vm;
3986 else
3987 return v_table[pxvid].vd;
3988}
3989
Daniel Vetter02d71952012-08-09 16:44:54 +02003990static void __i915_update_gfx_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02003991{
3992 struct timespec now, diff1;
3993 u64 diff;
3994 unsigned long diffms;
3995 u32 count;
3996
Daniel Vetter02d71952012-08-09 16:44:54 +02003997 assert_spin_locked(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02003998
3999 getrawmonotonic(&now);
Daniel Vetter20e4d402012-08-08 23:35:39 +02004000 diff1 = timespec_sub(now, dev_priv->ips.last_time2);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004001
4002 /* Don't divide by 0 */
4003 diffms = diff1.tv_sec * 1000 + diff1.tv_nsec / 1000000;
4004 if (!diffms)
4005 return;
4006
4007 count = I915_READ(GFXEC);
4008
Daniel Vetter20e4d402012-08-08 23:35:39 +02004009 if (count < dev_priv->ips.last_count2) {
4010 diff = ~0UL - dev_priv->ips.last_count2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02004011 diff += count;
4012 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +02004013 diff = count - dev_priv->ips.last_count2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02004014 }
4015
Daniel Vetter20e4d402012-08-08 23:35:39 +02004016 dev_priv->ips.last_count2 = count;
4017 dev_priv->ips.last_time2 = now;
Daniel Vettereb48eb02012-04-26 23:28:12 +02004018
4019 /* More magic constants... */
4020 diff = diff * 1181;
4021 diff = div_u64(diff, diffms * 10);
Daniel Vetter20e4d402012-08-08 23:35:39 +02004022 dev_priv->ips.gfx_power = diff;
Daniel Vettereb48eb02012-04-26 23:28:12 +02004023}
4024
Daniel Vetter02d71952012-08-09 16:44:54 +02004025void i915_update_gfx_val(struct drm_i915_private *dev_priv)
4026{
4027 if (dev_priv->info->gen != 5)
4028 return;
4029
Daniel Vetter92703882012-08-09 16:46:01 +02004030 spin_lock_irq(&mchdev_lock);
Daniel Vetter02d71952012-08-09 16:44:54 +02004031
4032 __i915_update_gfx_val(dev_priv);
4033
Daniel Vetter92703882012-08-09 16:46:01 +02004034 spin_unlock_irq(&mchdev_lock);
Daniel Vetter02d71952012-08-09 16:44:54 +02004035}
4036
Chris Wilsonf531dcb2012-09-25 10:16:12 +01004037static unsigned long __i915_gfx_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02004038{
4039 unsigned long t, corr, state1, corr2, state2;
4040 u32 pxvid, ext_v;
4041
Daniel Vetter02d71952012-08-09 16:44:54 +02004042 assert_spin_locked(&mchdev_lock);
4043
Daniel Vetterc6a828d2012-08-08 23:35:35 +02004044 pxvid = I915_READ(PXVFREQ_BASE + (dev_priv->rps.cur_delay * 4));
Daniel Vettereb48eb02012-04-26 23:28:12 +02004045 pxvid = (pxvid >> 24) & 0x7f;
4046 ext_v = pvid_to_extvid(dev_priv, pxvid);
4047
4048 state1 = ext_v;
4049
4050 t = i915_mch_val(dev_priv);
4051
4052 /* Revel in the empirically derived constants */
4053
4054 /* Correction factor in 1/100000 units */
4055 if (t > 80)
4056 corr = ((t * 2349) + 135940);
4057 else if (t >= 50)
4058 corr = ((t * 964) + 29317);
4059 else /* < 50 */
4060 corr = ((t * 301) + 1004);
4061
4062 corr = corr * ((150142 * state1) / 10000 - 78642);
4063 corr /= 100000;
Daniel Vetter20e4d402012-08-08 23:35:39 +02004064 corr2 = (corr * dev_priv->ips.corr);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004065
4066 state2 = (corr2 * state1) / 10000;
4067 state2 /= 100; /* convert to mW */
4068
Daniel Vetter02d71952012-08-09 16:44:54 +02004069 __i915_update_gfx_val(dev_priv);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004070
Daniel Vetter20e4d402012-08-08 23:35:39 +02004071 return dev_priv->ips.gfx_power + state2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02004072}
4073
Chris Wilsonf531dcb2012-09-25 10:16:12 +01004074unsigned long i915_gfx_val(struct drm_i915_private *dev_priv)
4075{
4076 unsigned long val;
4077
4078 if (dev_priv->info->gen != 5)
4079 return 0;
4080
4081 spin_lock_irq(&mchdev_lock);
4082
4083 val = __i915_gfx_val(dev_priv);
4084
4085 spin_unlock_irq(&mchdev_lock);
4086
4087 return val;
4088}
4089
Daniel Vettereb48eb02012-04-26 23:28:12 +02004090/**
4091 * i915_read_mch_val - return value for IPS use
4092 *
4093 * Calculate and return a value for the IPS driver to use when deciding whether
4094 * we have thermal and power headroom to increase CPU or GPU power budget.
4095 */
4096unsigned long i915_read_mch_val(void)
4097{
4098 struct drm_i915_private *dev_priv;
4099 unsigned long chipset_val, graphics_val, ret = 0;
4100
Daniel Vetter92703882012-08-09 16:46:01 +02004101 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004102 if (!i915_mch_dev)
4103 goto out_unlock;
4104 dev_priv = i915_mch_dev;
4105
Chris Wilsonf531dcb2012-09-25 10:16:12 +01004106 chipset_val = __i915_chipset_val(dev_priv);
4107 graphics_val = __i915_gfx_val(dev_priv);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004108
4109 ret = chipset_val + graphics_val;
4110
4111out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02004112 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004113
4114 return ret;
4115}
4116EXPORT_SYMBOL_GPL(i915_read_mch_val);
4117
4118/**
4119 * i915_gpu_raise - raise GPU frequency limit
4120 *
4121 * Raise the limit; IPS indicates we have thermal headroom.
4122 */
4123bool i915_gpu_raise(void)
4124{
4125 struct drm_i915_private *dev_priv;
4126 bool ret = true;
4127
Daniel Vetter92703882012-08-09 16:46:01 +02004128 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004129 if (!i915_mch_dev) {
4130 ret = false;
4131 goto out_unlock;
4132 }
4133 dev_priv = i915_mch_dev;
4134
Daniel Vetter20e4d402012-08-08 23:35:39 +02004135 if (dev_priv->ips.max_delay > dev_priv->ips.fmax)
4136 dev_priv->ips.max_delay--;
Daniel Vettereb48eb02012-04-26 23:28:12 +02004137
4138out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02004139 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004140
4141 return ret;
4142}
4143EXPORT_SYMBOL_GPL(i915_gpu_raise);
4144
4145/**
4146 * i915_gpu_lower - lower GPU frequency limit
4147 *
4148 * IPS indicates we're close to a thermal limit, so throttle back the GPU
4149 * frequency maximum.
4150 */
4151bool i915_gpu_lower(void)
4152{
4153 struct drm_i915_private *dev_priv;
4154 bool ret = true;
4155
Daniel Vetter92703882012-08-09 16:46:01 +02004156 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004157 if (!i915_mch_dev) {
4158 ret = false;
4159 goto out_unlock;
4160 }
4161 dev_priv = i915_mch_dev;
4162
Daniel Vetter20e4d402012-08-08 23:35:39 +02004163 if (dev_priv->ips.max_delay < dev_priv->ips.min_delay)
4164 dev_priv->ips.max_delay++;
Daniel Vettereb48eb02012-04-26 23:28:12 +02004165
4166out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02004167 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004168
4169 return ret;
4170}
4171EXPORT_SYMBOL_GPL(i915_gpu_lower);
4172
4173/**
4174 * i915_gpu_busy - indicate GPU business to IPS
4175 *
4176 * Tell the IPS driver whether or not the GPU is busy.
4177 */
4178bool i915_gpu_busy(void)
4179{
4180 struct drm_i915_private *dev_priv;
Chris Wilsonf047e392012-07-21 12:31:41 +01004181 struct intel_ring_buffer *ring;
Daniel Vettereb48eb02012-04-26 23:28:12 +02004182 bool ret = false;
Chris Wilsonf047e392012-07-21 12:31:41 +01004183 int i;
Daniel Vettereb48eb02012-04-26 23:28:12 +02004184
Daniel Vetter92703882012-08-09 16:46:01 +02004185 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004186 if (!i915_mch_dev)
4187 goto out_unlock;
4188 dev_priv = i915_mch_dev;
4189
Chris Wilsonf047e392012-07-21 12:31:41 +01004190 for_each_ring(ring, dev_priv, i)
4191 ret |= !list_empty(&ring->request_list);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004192
4193out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02004194 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004195
4196 return ret;
4197}
4198EXPORT_SYMBOL_GPL(i915_gpu_busy);
4199
4200/**
4201 * i915_gpu_turbo_disable - disable graphics turbo
4202 *
4203 * Disable graphics turbo by resetting the max frequency and setting the
4204 * current frequency to the default.
4205 */
4206bool i915_gpu_turbo_disable(void)
4207{
4208 struct drm_i915_private *dev_priv;
4209 bool ret = true;
4210
Daniel Vetter92703882012-08-09 16:46:01 +02004211 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004212 if (!i915_mch_dev) {
4213 ret = false;
4214 goto out_unlock;
4215 }
4216 dev_priv = i915_mch_dev;
4217
Daniel Vetter20e4d402012-08-08 23:35:39 +02004218 dev_priv->ips.max_delay = dev_priv->ips.fstart;
Daniel Vettereb48eb02012-04-26 23:28:12 +02004219
Daniel Vetter20e4d402012-08-08 23:35:39 +02004220 if (!ironlake_set_drps(dev_priv->dev, dev_priv->ips.fstart))
Daniel Vettereb48eb02012-04-26 23:28:12 +02004221 ret = false;
4222
4223out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02004224 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004225
4226 return ret;
4227}
4228EXPORT_SYMBOL_GPL(i915_gpu_turbo_disable);
4229
4230/**
4231 * Tells the intel_ips driver that the i915 driver is now loaded, if
4232 * IPS got loaded first.
4233 *
4234 * This awkward dance is so that neither module has to depend on the
4235 * other in order for IPS to do the appropriate communication of
4236 * GPU turbo limits to i915.
4237 */
4238static void
4239ips_ping_for_i915_load(void)
4240{
4241 void (*link)(void);
4242
4243 link = symbol_get(ips_link_to_i915_driver);
4244 if (link) {
4245 link();
4246 symbol_put(ips_link_to_i915_driver);
4247 }
4248}
4249
4250void intel_gpu_ips_init(struct drm_i915_private *dev_priv)
4251{
Daniel Vetter02d71952012-08-09 16:44:54 +02004252 /* We only register the i915 ips part with intel-ips once everything is
4253 * set up, to avoid intel-ips sneaking in and reading bogus values. */
Daniel Vetter92703882012-08-09 16:46:01 +02004254 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004255 i915_mch_dev = dev_priv;
Daniel Vetter92703882012-08-09 16:46:01 +02004256 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004257
4258 ips_ping_for_i915_load();
4259}
4260
4261void intel_gpu_ips_teardown(void)
4262{
Daniel Vetter92703882012-08-09 16:46:01 +02004263 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004264 i915_mch_dev = NULL;
Daniel Vetter92703882012-08-09 16:46:01 +02004265 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004266}
Daniel Vetter8090c6b2012-06-24 16:42:32 +02004267static void intel_init_emon(struct drm_device *dev)
Eugeni Dodonovdde18882012-04-18 15:29:24 -03004268{
4269 struct drm_i915_private *dev_priv = dev->dev_private;
4270 u32 lcfuse;
4271 u8 pxw[16];
4272 int i;
4273
4274 /* Disable to program */
4275 I915_WRITE(ECR, 0);
4276 POSTING_READ(ECR);
4277
4278 /* Program energy weights for various events */
4279 I915_WRITE(SDEW, 0x15040d00);
4280 I915_WRITE(CSIEW0, 0x007f0000);
4281 I915_WRITE(CSIEW1, 0x1e220004);
4282 I915_WRITE(CSIEW2, 0x04000004);
4283
4284 for (i = 0; i < 5; i++)
4285 I915_WRITE(PEW + (i * 4), 0);
4286 for (i = 0; i < 3; i++)
4287 I915_WRITE(DEW + (i * 4), 0);
4288
4289 /* Program P-state weights to account for frequency power adjustment */
4290 for (i = 0; i < 16; i++) {
4291 u32 pxvidfreq = I915_READ(PXVFREQ_BASE + (i * 4));
4292 unsigned long freq = intel_pxfreq(pxvidfreq);
4293 unsigned long vid = (pxvidfreq & PXVFREQ_PX_MASK) >>
4294 PXVFREQ_PX_SHIFT;
4295 unsigned long val;
4296
4297 val = vid * vid;
4298 val *= (freq / 1000);
4299 val *= 255;
4300 val /= (127*127*900);
4301 if (val > 0xff)
4302 DRM_ERROR("bad pxval: %ld\n", val);
4303 pxw[i] = val;
4304 }
4305 /* Render standby states get 0 weight */
4306 pxw[14] = 0;
4307 pxw[15] = 0;
4308
4309 for (i = 0; i < 4; i++) {
4310 u32 val = (pxw[i*4] << 24) | (pxw[(i*4)+1] << 16) |
4311 (pxw[(i*4)+2] << 8) | (pxw[(i*4)+3]);
4312 I915_WRITE(PXW + (i * 4), val);
4313 }
4314
4315 /* Adjust magic regs to magic values (more experimental results) */
4316 I915_WRITE(OGW0, 0);
4317 I915_WRITE(OGW1, 0);
4318 I915_WRITE(EG0, 0x00007f00);
4319 I915_WRITE(EG1, 0x0000000e);
4320 I915_WRITE(EG2, 0x000e0000);
4321 I915_WRITE(EG3, 0x68000300);
4322 I915_WRITE(EG4, 0x42000000);
4323 I915_WRITE(EG5, 0x00140031);
4324 I915_WRITE(EG6, 0);
4325 I915_WRITE(EG7, 0);
4326
4327 for (i = 0; i < 8; i++)
4328 I915_WRITE(PXWL + (i * 4), 0);
4329
4330 /* Enable PMON + select events */
4331 I915_WRITE(ECR, 0x80000019);
4332
4333 lcfuse = I915_READ(LCFUSE02);
4334
Daniel Vetter20e4d402012-08-08 23:35:39 +02004335 dev_priv->ips.corr = (lcfuse & LCFUSE_HIV_MASK);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03004336}
4337
Daniel Vetter8090c6b2012-06-24 16:42:32 +02004338void intel_disable_gt_powersave(struct drm_device *dev)
4339{
Jesse Barnes1a01ab32012-11-02 11:14:00 -07004340 struct drm_i915_private *dev_priv = dev->dev_private;
4341
Daniel Vetterfd0c0642013-04-24 11:13:35 +02004342 /* Interrupts should be disabled already to avoid re-arming. */
4343 WARN_ON(dev->irq_enabled);
4344
Daniel Vetter930ebb42012-06-29 23:32:16 +02004345 if (IS_IRONLAKE_M(dev)) {
Daniel Vetter8090c6b2012-06-24 16:42:32 +02004346 ironlake_disable_drps(dev);
Daniel Vetter930ebb42012-06-29 23:32:16 +02004347 ironlake_disable_rc6(dev);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004348 } else if (INTEL_INFO(dev)->gen >= 6) {
Jesse Barnes1a01ab32012-11-02 11:14:00 -07004349 cancel_delayed_work_sync(&dev_priv->rps.delayed_resume_work);
Jesse Barnes250848c2013-04-23 10:09:27 -07004350 cancel_work_sync(&dev_priv->rps.work);
Jesse Barnes4fc688c2012-11-02 11:14:01 -07004351 mutex_lock(&dev_priv->rps.hw_lock);
Jesse Barnesd20d4f02013-04-23 10:09:28 -07004352 if (IS_VALLEYVIEW(dev))
4353 valleyview_disable_rps(dev);
4354 else
4355 gen6_disable_rps(dev);
Chris Wilsonc0951f02013-10-10 21:58:50 +01004356 dev_priv->rps.enabled = false;
Jesse Barnes4fc688c2012-11-02 11:14:01 -07004357 mutex_unlock(&dev_priv->rps.hw_lock);
Daniel Vetter930ebb42012-06-29 23:32:16 +02004358 }
Daniel Vetter8090c6b2012-06-24 16:42:32 +02004359}
4360
Jesse Barnes1a01ab32012-11-02 11:14:00 -07004361static void intel_gen6_powersave_work(struct work_struct *work)
4362{
4363 struct drm_i915_private *dev_priv =
4364 container_of(work, struct drm_i915_private,
4365 rps.delayed_resume_work.work);
4366 struct drm_device *dev = dev_priv->dev;
4367
Jesse Barnes4fc688c2012-11-02 11:14:01 -07004368 mutex_lock(&dev_priv->rps.hw_lock);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004369
4370 if (IS_VALLEYVIEW(dev)) {
4371 valleyview_enable_rps(dev);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004372 } else if (IS_BROADWELL(dev)) {
4373 gen8_enable_rps(dev);
4374 gen6_update_ring_freq(dev);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004375 } else {
4376 gen6_enable_rps(dev);
4377 gen6_update_ring_freq(dev);
4378 }
Chris Wilsonc0951f02013-10-10 21:58:50 +01004379 dev_priv->rps.enabled = true;
Jesse Barnes4fc688c2012-11-02 11:14:01 -07004380 mutex_unlock(&dev_priv->rps.hw_lock);
Jesse Barnes1a01ab32012-11-02 11:14:00 -07004381}
4382
Daniel Vetter8090c6b2012-06-24 16:42:32 +02004383void intel_enable_gt_powersave(struct drm_device *dev)
4384{
Jesse Barnes1a01ab32012-11-02 11:14:00 -07004385 struct drm_i915_private *dev_priv = dev->dev_private;
4386
Daniel Vetter8090c6b2012-06-24 16:42:32 +02004387 if (IS_IRONLAKE_M(dev)) {
4388 ironlake_enable_drps(dev);
4389 ironlake_enable_rc6(dev);
4390 intel_init_emon(dev);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004391 } else if (IS_GEN6(dev) || IS_GEN7(dev)) {
Jesse Barnes1a01ab32012-11-02 11:14:00 -07004392 /*
4393 * PCU communication is slow and this doesn't need to be
4394 * done at any specific time, so do this out of our fast path
4395 * to make resume and init faster.
4396 */
4397 schedule_delayed_work(&dev_priv->rps.delayed_resume_work,
4398 round_jiffies_up_relative(HZ));
Daniel Vetter8090c6b2012-06-24 16:42:32 +02004399 }
4400}
4401
Daniel Vetter3107bd42012-10-31 22:52:31 +01004402static void ibx_init_clock_gating(struct drm_device *dev)
4403{
4404 struct drm_i915_private *dev_priv = dev->dev_private;
4405
4406 /*
4407 * On Ibex Peak and Cougar Point, we need to disable clock
4408 * gating for the panel power sequencer or it will fail to
4409 * start up when no ports are active.
4410 */
4411 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE);
4412}
4413
Ville Syrjälä0e088b82013-06-07 10:47:04 +03004414static void g4x_disable_trickle_feed(struct drm_device *dev)
4415{
4416 struct drm_i915_private *dev_priv = dev->dev_private;
4417 int pipe;
4418
4419 for_each_pipe(pipe) {
4420 I915_WRITE(DSPCNTR(pipe),
4421 I915_READ(DSPCNTR(pipe)) |
4422 DISPPLANE_TRICKLE_FEED_DISABLE);
Ville Syrjälä1dba99f2013-10-01 18:02:18 +03004423 intel_flush_primary_plane(dev_priv, pipe);
Ville Syrjälä0e088b82013-06-07 10:47:04 +03004424 }
4425}
4426
Ville Syrjälä017636c2013-12-05 15:51:37 +02004427static void ilk_init_lp_watermarks(struct drm_device *dev)
4428{
4429 struct drm_i915_private *dev_priv = dev->dev_private;
4430
4431 I915_WRITE(WM3_LP_ILK, I915_READ(WM3_LP_ILK) & ~WM1_LP_SR_EN);
4432 I915_WRITE(WM2_LP_ILK, I915_READ(WM2_LP_ILK) & ~WM1_LP_SR_EN);
4433 I915_WRITE(WM1_LP_ILK, I915_READ(WM1_LP_ILK) & ~WM1_LP_SR_EN);
4434
4435 /*
4436 * Don't touch WM1S_LP_EN here.
4437 * Doing so could cause underruns.
4438 */
4439}
4440
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03004441static void ironlake_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004442{
4443 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau231e54f2012-10-19 17:55:41 +01004444 uint32_t dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004445
Damien Lespiauf1e8fa52013-06-07 17:41:09 +01004446 /*
4447 * Required for FBC
4448 * WaFbcDisableDpfcClockGating:ilk
4449 */
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01004450 dspclk_gate |= ILK_DPFCRUNIT_CLOCK_GATE_DISABLE |
4451 ILK_DPFCUNIT_CLOCK_GATE_DISABLE |
4452 ILK_DPFDUNIT_CLOCK_GATE_ENABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004453
4454 I915_WRITE(PCH_3DCGDIS0,
4455 MARIUNIT_CLOCK_GATE_DISABLE |
4456 SVSMUNIT_CLOCK_GATE_DISABLE);
4457 I915_WRITE(PCH_3DCGDIS1,
4458 VFMUNIT_CLOCK_GATE_DISABLE);
4459
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004460 /*
4461 * According to the spec the following bits should be set in
4462 * order to enable memory self-refresh
4463 * The bit 22/21 of 0x42004
4464 * The bit 5 of 0x42020
4465 * The bit 15 of 0x45000
4466 */
4467 I915_WRITE(ILK_DISPLAY_CHICKEN2,
4468 (I915_READ(ILK_DISPLAY_CHICKEN2) |
4469 ILK_DPARB_GATE | ILK_VSDPFD_FULL));
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01004470 dspclk_gate |= ILK_DPARBUNIT_CLOCK_GATE_ENABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004471 I915_WRITE(DISP_ARB_CTL,
4472 (I915_READ(DISP_ARB_CTL) |
4473 DISP_FBC_WM_DIS));
Ville Syrjälä017636c2013-12-05 15:51:37 +02004474
4475 ilk_init_lp_watermarks(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004476
4477 /*
4478 * Based on the document from hardware guys the following bits
4479 * should be set unconditionally in order to enable FBC.
4480 * The bit 22 of 0x42000
4481 * The bit 22 of 0x42004
4482 * The bit 7,8,9 of 0x42020.
4483 */
4484 if (IS_IRONLAKE_M(dev)) {
Damien Lespiau4bb35332013-06-14 15:23:24 +01004485 /* WaFbcAsynchFlipDisableFbcQueue:ilk */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004486 I915_WRITE(ILK_DISPLAY_CHICKEN1,
4487 I915_READ(ILK_DISPLAY_CHICKEN1) |
4488 ILK_FBCQ_DIS);
4489 I915_WRITE(ILK_DISPLAY_CHICKEN2,
4490 I915_READ(ILK_DISPLAY_CHICKEN2) |
4491 ILK_DPARB_GATE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004492 }
4493
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01004494 I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
4495
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004496 I915_WRITE(ILK_DISPLAY_CHICKEN2,
4497 I915_READ(ILK_DISPLAY_CHICKEN2) |
4498 ILK_ELPIN_409_SELECT);
4499 I915_WRITE(_3D_CHICKEN2,
4500 _3D_CHICKEN2_WM_READ_PIPELINED << 16 |
4501 _3D_CHICKEN2_WM_READ_PIPELINED);
Daniel Vetter4358a372012-10-18 11:49:51 +02004502
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01004503 /* WaDisableRenderCachePipelinedFlush:ilk */
Daniel Vetter4358a372012-10-18 11:49:51 +02004504 I915_WRITE(CACHE_MODE_0,
4505 _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
Daniel Vetter3107bd42012-10-31 22:52:31 +01004506
Ville Syrjälä0e088b82013-06-07 10:47:04 +03004507 g4x_disable_trickle_feed(dev);
Ville Syrjäläbdad2b22013-06-07 10:47:03 +03004508
Daniel Vetter3107bd42012-10-31 22:52:31 +01004509 ibx_init_clock_gating(dev);
4510}
4511
4512static void cpt_init_clock_gating(struct drm_device *dev)
4513{
4514 struct drm_i915_private *dev_priv = dev->dev_private;
4515 int pipe;
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03004516 uint32_t val;
Daniel Vetter3107bd42012-10-31 22:52:31 +01004517
4518 /*
4519 * On Ibex Peak and Cougar Point, we need to disable clock
4520 * gating for the panel power sequencer or it will fail to
4521 * start up when no ports are active.
4522 */
Jesse Barnescd664072013-10-02 10:34:19 -07004523 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE |
4524 PCH_DPLUNIT_CLOCK_GATE_DISABLE |
4525 PCH_CPUNIT_CLOCK_GATE_DISABLE);
Daniel Vetter3107bd42012-10-31 22:52:31 +01004526 I915_WRITE(SOUTH_CHICKEN2, I915_READ(SOUTH_CHICKEN2) |
4527 DPLS_EDP_PPS_FIX_DIS);
Takashi Iwai335c07b2012-12-11 11:46:29 +01004528 /* The below fixes the weird display corruption, a few pixels shifted
4529 * downward, on (only) LVDS of some HP laptops with IVY.
4530 */
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03004531 for_each_pipe(pipe) {
Paulo Zanonidc4bd2d2013-04-08 15:48:08 -03004532 val = I915_READ(TRANS_CHICKEN2(pipe));
4533 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
4534 val &= ~TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03004535 if (dev_priv->vbt.fdi_rx_polarity_inverted)
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03004536 val |= TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
Paulo Zanonidc4bd2d2013-04-08 15:48:08 -03004537 val &= ~TRANS_CHICKEN2_FRAME_START_DELAY_MASK;
4538 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_COUNTER;
4539 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_MODESWITCH;
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03004540 I915_WRITE(TRANS_CHICKEN2(pipe), val);
4541 }
Daniel Vetter3107bd42012-10-31 22:52:31 +01004542 /* WADP0ClockGatingDisable */
4543 for_each_pipe(pipe) {
4544 I915_WRITE(TRANS_CHICKEN1(pipe),
4545 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
4546 }
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004547}
4548
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01004549static void gen6_check_mch_setup(struct drm_device *dev)
4550{
4551 struct drm_i915_private *dev_priv = dev->dev_private;
4552 uint32_t tmp;
4553
4554 tmp = I915_READ(MCH_SSKPD);
4555 if ((tmp & MCH_SSKPD_WM0_MASK) != MCH_SSKPD_WM0_VAL) {
4556 DRM_INFO("Wrong MCH_SSKPD value: 0x%08x\n", tmp);
4557 DRM_INFO("This can cause pipe underruns and display issues.\n");
4558 DRM_INFO("Please upgrade your BIOS to fix this.\n");
4559 }
4560}
4561
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03004562static void gen6_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004563{
4564 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau231e54f2012-10-19 17:55:41 +01004565 uint32_t dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004566
Damien Lespiau231e54f2012-10-19 17:55:41 +01004567 I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004568
4569 I915_WRITE(ILK_DISPLAY_CHICKEN2,
4570 I915_READ(ILK_DISPLAY_CHICKEN2) |
4571 ILK_ELPIN_409_SELECT);
4572
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01004573 /* WaDisableHiZPlanesWhenMSAAEnabled:snb */
Daniel Vetter42839082012-12-14 23:38:28 +01004574 I915_WRITE(_3D_CHICKEN,
4575 _MASKED_BIT_ENABLE(_3D_CHICKEN_HIZ_PLANE_DISABLE_MSAA_4X_SNB));
4576
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01004577 /* WaSetupGtModeTdRowDispatch:snb */
Daniel Vetter6547fbd2012-12-14 23:38:29 +01004578 if (IS_SNB_GT1(dev))
4579 I915_WRITE(GEN6_GT_MODE,
4580 _MASKED_BIT_ENABLE(GEN6_TD_FOUR_ROW_DISPATCH_DISABLE));
4581
Ville Syrjälä017636c2013-12-05 15:51:37 +02004582 ilk_init_lp_watermarks(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004583
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004584 I915_WRITE(CACHE_MODE_0,
Daniel Vetter50743292012-04-26 22:02:54 +02004585 _MASKED_BIT_DISABLE(CM0_STC_EVICT_DISABLE_LRA_SNB));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004586
4587 I915_WRITE(GEN6_UCGCTL1,
4588 I915_READ(GEN6_UCGCTL1) |
4589 GEN6_BLBUNIT_CLOCK_GATE_DISABLE |
4590 GEN6_CSUNIT_CLOCK_GATE_DISABLE);
4591
4592 /* According to the BSpec vol1g, bit 12 (RCPBUNIT) clock
4593 * gating disable must be set. Failure to set it results in
4594 * flickering pixels due to Z write ordering failures after
4595 * some amount of runtime in the Mesa "fire" demo, and Unigine
4596 * Sanctuary and Tropics, and apparently anything else with
4597 * alpha test or pixel discard.
4598 *
4599 * According to the spec, bit 11 (RCCUNIT) must also be set,
4600 * but we didn't debug actual testcases to find it out.
Jesse Barnes0f846f82012-06-14 11:04:47 -07004601 *
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01004602 * Also apply WaDisableVDSUnitClockGating:snb and
4603 * WaDisableRCPBUnitClockGating:snb.
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004604 */
4605 I915_WRITE(GEN6_UCGCTL2,
Jesse Barnes0f846f82012-06-14 11:04:47 -07004606 GEN7_VDSUNIT_CLOCK_GATE_DISABLE |
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004607 GEN6_RCPBUNIT_CLOCK_GATE_DISABLE |
4608 GEN6_RCCUNIT_CLOCK_GATE_DISABLE);
4609
4610 /* Bspec says we need to always set all mask bits. */
Kenneth Graunke26b6e442012-10-07 08:51:07 -07004611 I915_WRITE(_3D_CHICKEN3, (0xFFFF << 16) |
4612 _3D_CHICKEN3_SF_DISABLE_FASTCLIP_CULL);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004613
4614 /*
4615 * According to the spec the following bits should be
4616 * set in order to enable memory self-refresh and fbc:
4617 * The bit21 and bit22 of 0x42000
4618 * The bit21 and bit22 of 0x42004
4619 * The bit5 and bit7 of 0x42020
4620 * The bit14 of 0x70180
4621 * The bit14 of 0x71180
Damien Lespiau4bb35332013-06-14 15:23:24 +01004622 *
4623 * WaFbcAsynchFlipDisableFbcQueue:snb
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004624 */
4625 I915_WRITE(ILK_DISPLAY_CHICKEN1,
4626 I915_READ(ILK_DISPLAY_CHICKEN1) |
4627 ILK_FBCQ_DIS | ILK_PABSTRETCH_DIS);
4628 I915_WRITE(ILK_DISPLAY_CHICKEN2,
4629 I915_READ(ILK_DISPLAY_CHICKEN2) |
4630 ILK_DPARB_GATE | ILK_VSDPFD_FULL);
Damien Lespiau231e54f2012-10-19 17:55:41 +01004631 I915_WRITE(ILK_DSPCLK_GATE_D,
4632 I915_READ(ILK_DSPCLK_GATE_D) |
4633 ILK_DPARBUNIT_CLOCK_GATE_ENABLE |
4634 ILK_DPFDUNIT_CLOCK_GATE_ENABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004635
Ville Syrjälä0e088b82013-06-07 10:47:04 +03004636 g4x_disable_trickle_feed(dev);
Ben Widawskyf8f2ac92012-10-03 19:34:24 -07004637
4638 /* The default value should be 0x200 according to docs, but the two
4639 * platforms I checked have a 0 for this. (Maybe BIOS overrides?) */
4640 I915_WRITE(GEN6_GT_MODE, _MASKED_BIT_DISABLE(0xffff));
4641 I915_WRITE(GEN6_GT_MODE, _MASKED_BIT_ENABLE(GEN6_GT_MODE_HI));
Daniel Vetter3107bd42012-10-31 22:52:31 +01004642
4643 cpt_init_clock_gating(dev);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01004644
4645 gen6_check_mch_setup(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004646}
4647
4648static void gen7_setup_fixed_func_scheduler(struct drm_i915_private *dev_priv)
4649{
4650 uint32_t reg = I915_READ(GEN7_FF_THREAD_MODE);
4651
4652 reg &= ~GEN7_FF_SCHED_MASK;
4653 reg |= GEN7_FF_TS_SCHED_HW;
4654 reg |= GEN7_FF_VS_SCHED_HW;
4655 reg |= GEN7_FF_DS_SCHED_HW;
4656
Ben Widawsky41c0b3a2013-01-26 11:52:00 -08004657 if (IS_HASWELL(dev_priv->dev))
4658 reg &= ~GEN7_FF_VS_REF_CNT_FFME;
4659
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004660 I915_WRITE(GEN7_FF_THREAD_MODE, reg);
4661}
4662
Paulo Zanoni17a303e2012-11-20 15:12:07 -02004663static void lpt_init_clock_gating(struct drm_device *dev)
4664{
4665 struct drm_i915_private *dev_priv = dev->dev_private;
4666
4667 /*
4668 * TODO: this bit should only be enabled when really needed, then
4669 * disabled when not needed anymore in order to save power.
4670 */
4671 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE)
4672 I915_WRITE(SOUTH_DSPCLK_GATE_D,
4673 I915_READ(SOUTH_DSPCLK_GATE_D) |
4674 PCH_LP_PARTITION_LEVEL_DISABLE);
Paulo Zanoni0a790cd2013-04-17 18:15:49 -03004675
4676 /* WADPOClockGatingDisable:hsw */
4677 I915_WRITE(_TRANSA_CHICKEN1,
4678 I915_READ(_TRANSA_CHICKEN1) |
4679 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
Paulo Zanoni17a303e2012-11-20 15:12:07 -02004680}
4681
Imre Deak7d708ee2013-04-17 14:04:50 +03004682static void lpt_suspend_hw(struct drm_device *dev)
4683{
4684 struct drm_i915_private *dev_priv = dev->dev_private;
4685
4686 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
4687 uint32_t val = I915_READ(SOUTH_DSPCLK_GATE_D);
4688
4689 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
4690 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
4691 }
4692}
4693
Ben Widawsky1020a5c2013-11-02 21:07:06 -07004694static void gen8_init_clock_gating(struct drm_device *dev)
4695{
4696 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07004697 enum pipe i;
Ben Widawsky1020a5c2013-11-02 21:07:06 -07004698
4699 I915_WRITE(WM3_LP_ILK, 0);
4700 I915_WRITE(WM2_LP_ILK, 0);
4701 I915_WRITE(WM1_LP_ILK, 0);
Ben Widawsky50ed5fb2013-11-02 21:07:40 -07004702
4703 /* FIXME(BDW): Check all the w/a, some might only apply to
4704 * pre-production hw. */
4705
Damien Lespiau4167e322014-01-16 16:51:35 +00004706 /*
4707 * This GEN8_CENTROID_PIXEL_OPT_DIS W/A is only needed for
4708 * pre-production hardware
4709 */
Ben Widawskyfd392b62013-11-04 22:52:39 -08004710 I915_WRITE(HALF_SLICE_CHICKEN3,
4711 _MASKED_BIT_ENABLE(GEN8_CENTROID_PIXEL_OPT_DIS));
Ben Widawskybf663472013-11-02 21:07:57 -07004712 I915_WRITE(HALF_SLICE_CHICKEN3,
4713 _MASKED_BIT_ENABLE(GEN8_SAMPLER_POWER_BYPASS_DIS));
Ben Widawsky4afe8d32013-11-02 21:07:55 -07004714 I915_WRITE(GAMTARBMODE, _MASKED_BIT_ENABLE(ARB_MODE_BWGTLB_DISABLE));
4715
Ben Widawsky7f88da02013-11-02 21:07:58 -07004716 I915_WRITE(_3D_CHICKEN3,
4717 _3D_CHICKEN_SDE_LIMIT_FIFO_POLY_DEPTH(2));
4718
Ben Widawskya75f3622013-11-02 21:07:59 -07004719 I915_WRITE(COMMON_SLICE_CHICKEN2,
4720 _MASKED_BIT_ENABLE(GEN8_CSC2_SBE_VUE_CACHE_CONSERVATIVE));
4721
Ben Widawsky4c2e7a52013-11-02 21:08:00 -07004722 I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
4723 _MASKED_BIT_ENABLE(GEN7_SINGLE_SUBSCAN_DISPATCH_ENABLE));
4724
Ben Widawskyab57fff2013-12-12 15:28:04 -08004725 /* WaSwitchSolVfFArbitrationPriority:bdw */
Ben Widawsky50ed5fb2013-11-02 21:07:40 -07004726 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07004727
Ben Widawskyab57fff2013-12-12 15:28:04 -08004728 /* WaPsrDPAMaskVBlankInSRD:bdw */
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07004729 I915_WRITE(CHICKEN_PAR1_1,
4730 I915_READ(CHICKEN_PAR1_1) | DPA_MASK_VBLANK_SRD);
4731
Ben Widawskyab57fff2013-12-12 15:28:04 -08004732 /* WaPsrDPRSUnmaskVBlankInSRD:bdw */
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07004733 for_each_pipe(i) {
4734 I915_WRITE(CHICKEN_PIPESL_1(i),
4735 I915_READ(CHICKEN_PIPESL_1(i) |
4736 DPRS_MASK_VBLANK_SRD));
4737 }
Ben Widawsky63801f22013-12-12 17:26:03 -08004738
4739 /* Use Force Non-Coherent whenever executing a 3D context. This is a
4740 * workaround for for a possible hang in the unlikely event a TLB
4741 * invalidation occurs during a PSD flush.
4742 */
4743 I915_WRITE(HDC_CHICKEN0,
4744 I915_READ(HDC_CHICKEN0) |
4745 _MASKED_BIT_ENABLE(HDC_FORCE_NON_COHERENT));
Ben Widawskyab57fff2013-12-12 15:28:04 -08004746
4747 /* WaVSRefCountFullforceMissDisable:bdw */
4748 /* WaDSRefCountFullforceMissDisable:bdw */
4749 I915_WRITE(GEN7_FF_THREAD_MODE,
4750 I915_READ(GEN7_FF_THREAD_MODE) &
4751 ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
Ben Widawsky1020a5c2013-11-02 21:07:06 -07004752}
4753
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03004754static void haswell_init_clock_gating(struct drm_device *dev)
4755{
4756 struct drm_i915_private *dev_priv = dev->dev_private;
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03004757
Ville Syrjälä017636c2013-12-05 15:51:37 +02004758 ilk_init_lp_watermarks(dev);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03004759
4760 /* According to the spec, bit 13 (RCZUNIT) must be set on IVB.
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01004761 * This implements the WaDisableRCZUnitClockGating:hsw workaround.
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03004762 */
4763 I915_WRITE(GEN6_UCGCTL2, GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
4764
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01004765 /* Apply the WaDisableRHWOOptimizationForRenderHang:hsw workaround. */
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03004766 I915_WRITE(GEN7_COMMON_SLICE_CHICKEN1,
4767 GEN7_CSC1_RHWO_OPT_DISABLE_IN_RCC);
4768
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01004769 /* WaApplyL3ControlAndL3ChickenMode:hsw */
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03004770 I915_WRITE(GEN7_L3CNTLREG1,
4771 GEN7_WA_FOR_GEN7_L3_CONTROL);
4772 I915_WRITE(GEN7_L3_CHICKEN_MODE_REGISTER,
4773 GEN7_WA_L3_CHICKEN_MODE);
4774
Francisco Jerezf3fc4882013-10-02 15:53:16 -07004775 /* L3 caching of data atomics doesn't work -- disable it. */
4776 I915_WRITE(HSW_SCRATCH1, HSW_SCRATCH1_L3_DATA_ATOMICS_DISABLE);
4777 I915_WRITE(HSW_ROW_CHICKEN3,
4778 _MASKED_BIT_ENABLE(HSW_ROW_CHICKEN3_L3_GLOBAL_ATOMICS_DISABLE));
4779
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01004780 /* This is required by WaCatErrorRejectionIssue:hsw */
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03004781 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
4782 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
4783 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
4784
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01004785 /* WaVSRefCountFullforceMissDisable:hsw */
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03004786 gen7_setup_fixed_func_scheduler(dev_priv);
4787
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01004788 /* WaDisable4x2SubspanOptimization:hsw */
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03004789 I915_WRITE(CACHE_MODE_1,
4790 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Eugeni Dodonov1544d9d2012-07-02 11:51:10 -03004791
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01004792 /* WaSwitchSolVfFArbitrationPriority:hsw */
Ben Widawskye3dff582013-03-20 14:49:14 -07004793 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
4794
Paulo Zanoni90a88642013-05-03 17:23:45 -03004795 /* WaRsPkgCStateDisplayPMReq:hsw */
4796 I915_WRITE(CHICKEN_PAR1_1,
4797 I915_READ(CHICKEN_PAR1_1) | FORCE_ARB_IDLE_PLANES);
Eugeni Dodonov1544d9d2012-07-02 11:51:10 -03004798
Paulo Zanoni17a303e2012-11-20 15:12:07 -02004799 lpt_init_clock_gating(dev);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03004800}
4801
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03004802static void ivybridge_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004803{
4804 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawsky20848222012-05-04 18:58:59 -07004805 uint32_t snpcr;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004806
Ville Syrjälä017636c2013-12-05 15:51:37 +02004807 ilk_init_lp_watermarks(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004808
Damien Lespiau231e54f2012-10-19 17:55:41 +01004809 I915_WRITE(ILK_DSPCLK_GATE_D, ILK_VRHUNIT_CLOCK_GATE_DISABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004810
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01004811 /* WaDisableEarlyCull:ivb */
Jesse Barnes87f80202012-10-02 17:43:41 -05004812 I915_WRITE(_3D_CHICKEN3,
4813 _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
4814
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01004815 /* WaDisableBackToBackFlipFix:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004816 I915_WRITE(IVB_CHICKEN3,
4817 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
4818 CHICKEN3_DGMG_DONE_FIX_DISABLE);
4819
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01004820 /* WaDisablePSDDualDispatchEnable:ivb */
Jesse Barnes12f33822012-10-25 12:15:45 -07004821 if (IS_IVB_GT1(dev))
4822 I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
4823 _MASKED_BIT_ENABLE(GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
4824 else
4825 I915_WRITE(GEN7_HALF_SLICE_CHICKEN1_GT2,
4826 _MASKED_BIT_ENABLE(GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
4827
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01004828 /* Apply the WaDisableRHWOOptimizationForRenderHang:ivb workaround. */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004829 I915_WRITE(GEN7_COMMON_SLICE_CHICKEN1,
4830 GEN7_CSC1_RHWO_OPT_DISABLE_IN_RCC);
4831
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01004832 /* WaApplyL3ControlAndL3ChickenMode:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004833 I915_WRITE(GEN7_L3CNTLREG1,
4834 GEN7_WA_FOR_GEN7_L3_CONTROL);
4835 I915_WRITE(GEN7_L3_CHICKEN_MODE_REGISTER,
Jesse Barnes8ab43972012-10-25 12:15:42 -07004836 GEN7_WA_L3_CHICKEN_MODE);
4837 if (IS_IVB_GT1(dev))
4838 I915_WRITE(GEN7_ROW_CHICKEN2,
4839 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
4840 else
4841 I915_WRITE(GEN7_ROW_CHICKEN2_GT2,
4842 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
4843
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004844
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01004845 /* WaForceL3Serialization:ivb */
Jesse Barnes61939d92012-10-02 17:43:38 -05004846 I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
4847 ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
4848
Jesse Barnes0f846f82012-06-14 11:04:47 -07004849 /* According to the BSpec vol1g, bit 12 (RCPBUNIT) clock
4850 * gating disable must be set. Failure to set it results in
4851 * flickering pixels due to Z write ordering failures after
4852 * some amount of runtime in the Mesa "fire" demo, and Unigine
4853 * Sanctuary and Tropics, and apparently anything else with
4854 * alpha test or pixel discard.
4855 *
4856 * According to the spec, bit 11 (RCCUNIT) must also be set,
4857 * but we didn't debug actual testcases to find it out.
4858 *
4859 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01004860 * This implements the WaDisableRCZUnitClockGating:ivb workaround.
Jesse Barnes0f846f82012-06-14 11:04:47 -07004861 */
4862 I915_WRITE(GEN6_UCGCTL2,
4863 GEN6_RCZUNIT_CLOCK_GATE_DISABLE |
4864 GEN6_RCCUNIT_CLOCK_GATE_DISABLE);
4865
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01004866 /* This is required by WaCatErrorRejectionIssue:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004867 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
4868 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
4869 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
4870
Ville Syrjälä0e088b82013-06-07 10:47:04 +03004871 g4x_disable_trickle_feed(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004872
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01004873 /* WaVSRefCountFullforceMissDisable:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004874 gen7_setup_fixed_func_scheduler(dev_priv);
Daniel Vetter97e19302012-04-24 16:00:21 +02004875
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01004876 /* WaDisable4x2SubspanOptimization:ivb */
Daniel Vetter97e19302012-04-24 16:00:21 +02004877 I915_WRITE(CACHE_MODE_1,
4878 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Ben Widawsky20848222012-05-04 18:58:59 -07004879
4880 snpcr = I915_READ(GEN6_MBCUNIT_SNPCR);
4881 snpcr &= ~GEN6_MBC_SNPCR_MASK;
4882 snpcr |= GEN6_MBC_SNPCR_MED;
4883 I915_WRITE(GEN6_MBCUNIT_SNPCR, snpcr);
Daniel Vetter3107bd42012-10-31 22:52:31 +01004884
Ben Widawskyab5c6082013-04-05 13:12:41 -07004885 if (!HAS_PCH_NOP(dev))
4886 cpt_init_clock_gating(dev);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01004887
4888 gen6_check_mch_setup(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004889}
4890
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03004891static void valleyview_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004892{
4893 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes85b1d7b2013-11-04 11:52:45 -08004894 u32 val;
4895
4896 mutex_lock(&dev_priv->rps.hw_lock);
4897 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
4898 mutex_unlock(&dev_priv->rps.hw_lock);
4899 switch ((val >> 6) & 3) {
4900 case 0:
Jesse Barnes85b1d7b2013-11-04 11:52:45 -08004901 dev_priv->mem_freq = 800;
4902 break;
Jesse Barnesf64a28a2013-11-04 16:07:00 -08004903 case 1:
Jesse Barnes85b1d7b2013-11-04 11:52:45 -08004904 dev_priv->mem_freq = 1066;
4905 break;
Jesse Barnesf64a28a2013-11-04 16:07:00 -08004906 case 2:
Jesse Barnes85b1d7b2013-11-04 11:52:45 -08004907 dev_priv->mem_freq = 1333;
4908 break;
Jesse Barnesf64a28a2013-11-04 16:07:00 -08004909 case 3:
Chon Ming Lee23259912013-11-07 15:23:26 +08004910 dev_priv->mem_freq = 1333;
Jesse Barnesf64a28a2013-11-04 16:07:00 -08004911 break;
Jesse Barnes85b1d7b2013-11-04 11:52:45 -08004912 }
4913 DRM_DEBUG_DRIVER("DDR speed: %d MHz", dev_priv->mem_freq);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004914
Ville Syrjäläd7fe0cc2013-05-21 18:01:50 +03004915 I915_WRITE(DSPCLK_GATE_D, VRHUNIT_CLOCK_GATE_DISABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004916
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01004917 /* WaDisableEarlyCull:vlv */
Jesse Barnes87f80202012-10-02 17:43:41 -05004918 I915_WRITE(_3D_CHICKEN3,
4919 _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
4920
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01004921 /* WaDisableBackToBackFlipFix:vlv */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004922 I915_WRITE(IVB_CHICKEN3,
4923 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
4924 CHICKEN3_DGMG_DONE_FIX_DISABLE);
4925
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01004926 /* WaDisablePSDDualDispatchEnable:vlv */
Jesse Barnes12f33822012-10-25 12:15:45 -07004927 I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
Jesse Barnesd3bc0302013-03-08 10:45:51 -08004928 _MASKED_BIT_ENABLE(GEN7_MAX_PS_THREAD_DEP |
4929 GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
Jesse Barnes12f33822012-10-25 12:15:45 -07004930
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01004931 /* Apply the WaDisableRHWOOptimizationForRenderHang:vlv workaround. */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004932 I915_WRITE(GEN7_COMMON_SLICE_CHICKEN1,
4933 GEN7_CSC1_RHWO_OPT_DISABLE_IN_RCC);
4934
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01004935 /* WaApplyL3ControlAndL3ChickenMode:vlv */
Jesse Barnesd0cf5ea2012-10-25 12:15:41 -07004936 I915_WRITE(GEN7_L3CNTLREG1, I915_READ(GEN7_L3CNTLREG1) | GEN7_L3AGDIS);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004937 I915_WRITE(GEN7_L3_CHICKEN_MODE_REGISTER, GEN7_WA_L3_CHICKEN_MODE);
4938
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01004939 /* WaForceL3Serialization:vlv */
Jesse Barnes61939d92012-10-02 17:43:38 -05004940 I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
4941 ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
4942
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01004943 /* WaDisableDopClockGating:vlv */
Jesse Barnes8ab43972012-10-25 12:15:42 -07004944 I915_WRITE(GEN7_ROW_CHICKEN2,
4945 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
4946
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01004947 /* This is required by WaCatErrorRejectionIssue:vlv */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004948 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
4949 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
4950 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
4951
Jesse Barnes0f846f82012-06-14 11:04:47 -07004952 /* According to the BSpec vol1g, bit 12 (RCPBUNIT) clock
4953 * gating disable must be set. Failure to set it results in
4954 * flickering pixels due to Z write ordering failures after
4955 * some amount of runtime in the Mesa "fire" demo, and Unigine
4956 * Sanctuary and Tropics, and apparently anything else with
4957 * alpha test or pixel discard.
4958 *
4959 * According to the spec, bit 11 (RCCUNIT) must also be set,
4960 * but we didn't debug actual testcases to find it out.
4961 *
4962 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01004963 * This implements the WaDisableRCZUnitClockGating:vlv workaround.
Jesse Barnes0f846f82012-06-14 11:04:47 -07004964 *
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01004965 * Also apply WaDisableVDSUnitClockGating:vlv and
4966 * WaDisableRCPBUnitClockGating:vlv.
Jesse Barnes0f846f82012-06-14 11:04:47 -07004967 */
4968 I915_WRITE(GEN6_UCGCTL2,
4969 GEN7_VDSUNIT_CLOCK_GATE_DISABLE |
Jesse Barnes6edaa7f2012-06-14 11:04:49 -07004970 GEN7_TDLUNIT_CLOCK_GATE_DISABLE |
Jesse Barnes0f846f82012-06-14 11:04:47 -07004971 GEN6_RCZUNIT_CLOCK_GATE_DISABLE |
4972 GEN6_RCPBUNIT_CLOCK_GATE_DISABLE |
4973 GEN6_RCCUNIT_CLOCK_GATE_DISABLE);
4974
Jesse Barnese3f33d42012-06-14 11:04:50 -07004975 I915_WRITE(GEN7_UCGCTL4, GEN7_L3BANK2X_CLOCK_GATE_DISABLE);
4976
Ville Syrjäläe0d8d592013-06-12 22:11:18 +03004977 I915_WRITE(MI_ARB_VLV, MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004978
Daniel Vetter6b26c862012-04-24 14:04:12 +02004979 I915_WRITE(CACHE_MODE_1,
4980 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Jesse Barnes79831172012-06-20 10:53:12 -07004981
4982 /*
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01004983 * WaDisableVLVClockGating_VBIIssue:vlv
Jesse Barnes2d809572012-10-25 12:15:44 -07004984 * Disable clock gating on th GCFG unit to prevent a delay
4985 * in the reporting of vblank events.
4986 */
Jesse Barnes4e8c84a2013-03-08 10:45:54 -08004987 I915_WRITE(VLV_GUNIT_CLOCK_GATE, 0xffffffff);
4988
4989 /* Conservative clock gating settings for now */
4990 I915_WRITE(0x9400, 0xffffffff);
4991 I915_WRITE(0x9404, 0xffffffff);
4992 I915_WRITE(0x9408, 0xffffffff);
4993 I915_WRITE(0x940c, 0xffffffff);
4994 I915_WRITE(0x9410, 0xffffffff);
4995 I915_WRITE(0x9414, 0xffffffff);
4996 I915_WRITE(0x9418, 0xffffffff);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004997}
4998
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03004999static void g4x_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005000{
5001 struct drm_i915_private *dev_priv = dev->dev_private;
5002 uint32_t dspclk_gate;
5003
5004 I915_WRITE(RENCLK_GATE_D1, 0);
5005 I915_WRITE(RENCLK_GATE_D2, VF_UNIT_CLOCK_GATE_DISABLE |
5006 GS_UNIT_CLOCK_GATE_DISABLE |
5007 CL_UNIT_CLOCK_GATE_DISABLE);
5008 I915_WRITE(RAMCLK_GATE_D, 0);
5009 dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE |
5010 OVRUNIT_CLOCK_GATE_DISABLE |
5011 OVCUNIT_CLOCK_GATE_DISABLE;
5012 if (IS_GM45(dev))
5013 dspclk_gate |= DSSUNIT_CLOCK_GATE_DISABLE;
5014 I915_WRITE(DSPCLK_GATE_D, dspclk_gate);
Daniel Vetter4358a372012-10-18 11:49:51 +02005015
5016 /* WaDisableRenderCachePipelinedFlush */
5017 I915_WRITE(CACHE_MODE_0,
5018 _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
Ville Syrjäläde1aa622013-06-07 10:47:01 +03005019
Ville Syrjälä0e088b82013-06-07 10:47:04 +03005020 g4x_disable_trickle_feed(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005021}
5022
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005023static void crestline_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005024{
5025 struct drm_i915_private *dev_priv = dev->dev_private;
5026
5027 I915_WRITE(RENCLK_GATE_D1, I965_RCC_CLOCK_GATE_DISABLE);
5028 I915_WRITE(RENCLK_GATE_D2, 0);
5029 I915_WRITE(DSPCLK_GATE_D, 0);
5030 I915_WRITE(RAMCLK_GATE_D, 0);
5031 I915_WRITE16(DEUC, 0);
Ville Syrjälä20f94962013-06-07 10:47:02 +03005032 I915_WRITE(MI_ARB_STATE,
5033 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005034}
5035
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005036static void broadwater_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005037{
5038 struct drm_i915_private *dev_priv = dev->dev_private;
5039
5040 I915_WRITE(RENCLK_GATE_D1, I965_RCZ_CLOCK_GATE_DISABLE |
5041 I965_RCC_CLOCK_GATE_DISABLE |
5042 I965_RCPB_CLOCK_GATE_DISABLE |
5043 I965_ISC_CLOCK_GATE_DISABLE |
5044 I965_FBC_CLOCK_GATE_DISABLE);
5045 I915_WRITE(RENCLK_GATE_D2, 0);
Ville Syrjälä20f94962013-06-07 10:47:02 +03005046 I915_WRITE(MI_ARB_STATE,
5047 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005048}
5049
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005050static void gen3_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005051{
5052 struct drm_i915_private *dev_priv = dev->dev_private;
5053 u32 dstate = I915_READ(D_STATE);
5054
5055 dstate |= DSTATE_PLL_D3_OFF | DSTATE_GFX_CLOCK_GATING |
5056 DSTATE_DOT_CLOCK_GATING;
5057 I915_WRITE(D_STATE, dstate);
Chris Wilson13a86b82012-04-24 14:51:43 +01005058
5059 if (IS_PINEVIEW(dev))
5060 I915_WRITE(ECOSKPD, _MASKED_BIT_ENABLE(ECO_GATING_CX_ONLY));
Daniel Vetter974a3b02012-09-09 11:54:16 +02005061
5062 /* IIR "flip pending" means done if this bit is set */
5063 I915_WRITE(ECOSKPD, _MASKED_BIT_DISABLE(ECO_FLIP_DONE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005064}
5065
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005066static void i85x_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005067{
5068 struct drm_i915_private *dev_priv = dev->dev_private;
5069
5070 I915_WRITE(RENCLK_GATE_D1, SV_CLOCK_GATE_DISABLE);
5071}
5072
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005073static void i830_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005074{
5075 struct drm_i915_private *dev_priv = dev->dev_private;
5076
5077 I915_WRITE(DSPCLK_GATE_D, OVRUNIT_CLOCK_GATE_DISABLE);
5078}
5079
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005080void intel_init_clock_gating(struct drm_device *dev)
5081{
5082 struct drm_i915_private *dev_priv = dev->dev_private;
5083
5084 dev_priv->display.init_clock_gating(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005085}
5086
Imre Deak7d708ee2013-04-17 14:04:50 +03005087void intel_suspend_hw(struct drm_device *dev)
5088{
5089 if (HAS_PCH_LPT(dev))
5090 lpt_suspend_hw(dev);
5091}
5092
Imre Deakc1ca7272013-11-25 17:15:29 +02005093#define for_each_power_well(i, power_well, domain_mask, power_domains) \
5094 for (i = 0; \
5095 i < (power_domains)->power_well_count && \
5096 ((power_well) = &(power_domains)->power_wells[i]); \
5097 i++) \
5098 if ((power_well)->domains & (domain_mask))
5099
5100#define for_each_power_well_rev(i, power_well, domain_mask, power_domains) \
5101 for (i = (power_domains)->power_well_count - 1; \
5102 i >= 0 && ((power_well) = &(power_domains)->power_wells[i]);\
5103 i--) \
5104 if ((power_well)->domains & (domain_mask))
5105
Paulo Zanoni15d199e2013-03-22 14:14:13 -03005106/**
5107 * We should only use the power well if we explicitly asked the hardware to
5108 * enable it, so check if it's enabled and also check if we've requested it to
5109 * be enabled.
5110 */
Imre Deakc1ca7272013-11-25 17:15:29 +02005111static bool hsw_power_well_enabled(struct drm_device *dev,
5112 struct i915_power_well *power_well)
5113{
5114 struct drm_i915_private *dev_priv = dev->dev_private;
5115
5116 return I915_READ(HSW_PWR_WELL_DRIVER) ==
5117 (HSW_PWR_WELL_ENABLE_REQUEST | HSW_PWR_WELL_STATE_ENABLED);
5118}
5119
Imre Deakddf9c532013-11-27 22:02:02 +02005120bool intel_display_power_enabled_sw(struct drm_device *dev,
5121 enum intel_display_power_domain domain)
5122{
5123 struct drm_i915_private *dev_priv = dev->dev_private;
5124 struct i915_power_domains *power_domains;
5125
5126 power_domains = &dev_priv->power_domains;
5127
5128 return power_domains->domain_use_count[domain];
5129}
5130
Paulo Zanonib97186f2013-05-03 12:15:36 -03005131bool intel_display_power_enabled(struct drm_device *dev,
5132 enum intel_display_power_domain domain)
Paulo Zanoni15d199e2013-03-22 14:14:13 -03005133{
5134 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deakc1ca7272013-11-25 17:15:29 +02005135 struct i915_power_domains *power_domains;
5136 struct i915_power_well *power_well;
5137 bool is_enabled;
5138 int i;
Paulo Zanoni15d199e2013-03-22 14:14:13 -03005139
Imre Deakc1ca7272013-11-25 17:15:29 +02005140 power_domains = &dev_priv->power_domains;
5141
5142 is_enabled = true;
5143
5144 mutex_lock(&power_domains->lock);
5145 for_each_power_well_rev(i, power_well, BIT(domain), power_domains) {
Imre Deak6f3ef5d2013-11-25 17:15:30 +02005146 if (power_well->always_on)
5147 continue;
5148
Imre Deakc1ca7272013-11-25 17:15:29 +02005149 if (!power_well->is_enabled(dev, power_well)) {
5150 is_enabled = false;
5151 break;
5152 }
5153 }
5154 mutex_unlock(&power_domains->lock);
5155
5156 return is_enabled;
Paulo Zanoni15d199e2013-03-22 14:14:13 -03005157}
5158
Paulo Zanonid5e8fdc2013-12-11 18:50:09 -02005159static void hsw_power_well_post_enable(struct drm_i915_private *dev_priv)
5160{
5161 struct drm_device *dev = dev_priv->dev;
5162 unsigned long irqflags;
5163
Paulo Zanonif9dcb0d2013-12-11 18:50:10 -02005164 /*
5165 * After we re-enable the power well, if we touch VGA register 0x3d5
5166 * we'll get unclaimed register interrupts. This stops after we write
5167 * anything to the VGA MSR register. The vgacon module uses this
5168 * register all the time, so if we unbind our driver and, as a
5169 * consequence, bind vgacon, we'll get stuck in an infinite loop at
5170 * console_unlock(). So make here we touch the VGA MSR register, making
5171 * sure vgacon can keep working normally without triggering interrupts
5172 * and error messages.
5173 */
5174 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
5175 outb(inb(VGA_MSR_READ), VGA_MSR_WRITE);
5176 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
5177
Paulo Zanonid5e8fdc2013-12-11 18:50:09 -02005178 if (IS_BROADWELL(dev)) {
5179 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
5180 I915_WRITE(GEN8_DE_PIPE_IMR(PIPE_B),
5181 dev_priv->de_irq_mask[PIPE_B]);
5182 I915_WRITE(GEN8_DE_PIPE_IER(PIPE_B),
5183 ~dev_priv->de_irq_mask[PIPE_B] |
5184 GEN8_PIPE_VBLANK);
5185 I915_WRITE(GEN8_DE_PIPE_IMR(PIPE_C),
5186 dev_priv->de_irq_mask[PIPE_C]);
5187 I915_WRITE(GEN8_DE_PIPE_IER(PIPE_C),
5188 ~dev_priv->de_irq_mask[PIPE_C] |
5189 GEN8_PIPE_VBLANK);
5190 POSTING_READ(GEN8_DE_PIPE_IER(PIPE_C));
5191 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
5192 }
5193}
5194
5195static void hsw_power_well_post_disable(struct drm_i915_private *dev_priv)
5196{
5197 struct drm_device *dev = dev_priv->dev;
5198 enum pipe p;
5199 unsigned long irqflags;
5200
5201 /*
5202 * After this, the registers on the pipes that are part of the power
5203 * well will become zero, so we have to adjust our counters according to
5204 * that.
5205 *
5206 * FIXME: Should we do this in general in drm_vblank_post_modeset?
5207 */
5208 spin_lock_irqsave(&dev->vbl_lock, irqflags);
5209 for_each_pipe(p)
5210 if (p != PIPE_A)
5211 dev->vblank[p].last = 0;
5212 spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
5213}
5214
Imre Deakc1ca7272013-11-25 17:15:29 +02005215static void hsw_set_power_well(struct drm_device *dev,
5216 struct i915_power_well *power_well, bool enable)
Eugeni Dodonovd0d3e512012-05-09 15:37:16 -03005217{
5218 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanonifa42e232013-01-25 16:59:11 -02005219 bool is_enabled, enable_requested;
5220 uint32_t tmp;
Eugeni Dodonovd0d3e512012-05-09 15:37:16 -03005221
Paulo Zanonid62292c2013-11-27 17:59:22 -02005222 WARN_ON(dev_priv->pc8.enabled);
5223
Paulo Zanonifa42e232013-01-25 16:59:11 -02005224 tmp = I915_READ(HSW_PWR_WELL_DRIVER);
Paulo Zanoni6aedd1f2013-08-02 16:22:25 -03005225 is_enabled = tmp & HSW_PWR_WELL_STATE_ENABLED;
5226 enable_requested = tmp & HSW_PWR_WELL_ENABLE_REQUEST;
Eugeni Dodonovd0d3e512012-05-09 15:37:16 -03005227
Paulo Zanonifa42e232013-01-25 16:59:11 -02005228 if (enable) {
5229 if (!enable_requested)
Paulo Zanoni6aedd1f2013-08-02 16:22:25 -03005230 I915_WRITE(HSW_PWR_WELL_DRIVER,
5231 HSW_PWR_WELL_ENABLE_REQUEST);
Eugeni Dodonovd0d3e512012-05-09 15:37:16 -03005232
Paulo Zanonifa42e232013-01-25 16:59:11 -02005233 if (!is_enabled) {
5234 DRM_DEBUG_KMS("Enabling power well\n");
5235 if (wait_for((I915_READ(HSW_PWR_WELL_DRIVER) &
Paulo Zanoni6aedd1f2013-08-02 16:22:25 -03005236 HSW_PWR_WELL_STATE_ENABLED), 20))
Paulo Zanonifa42e232013-01-25 16:59:11 -02005237 DRM_ERROR("Timeout enabling power well\n");
5238 }
Ben Widawsky596cc112013-11-11 14:46:28 -08005239
Paulo Zanonid5e8fdc2013-12-11 18:50:09 -02005240 hsw_power_well_post_enable(dev_priv);
Paulo Zanonifa42e232013-01-25 16:59:11 -02005241 } else {
5242 if (enable_requested) {
5243 I915_WRITE(HSW_PWR_WELL_DRIVER, 0);
Paulo Zanoni9dbd8fe2013-07-23 10:48:11 -03005244 POSTING_READ(HSW_PWR_WELL_DRIVER);
Paulo Zanonifa42e232013-01-25 16:59:11 -02005245 DRM_DEBUG_KMS("Requesting to disable the power well\n");
Paulo Zanoni9dbd8fe2013-07-23 10:48:11 -03005246
Paulo Zanonid5e8fdc2013-12-11 18:50:09 -02005247 hsw_power_well_post_disable(dev_priv);
Eugeni Dodonovd0d3e512012-05-09 15:37:16 -03005248 }
5249 }
Paulo Zanonifa42e232013-01-25 16:59:11 -02005250}
Eugeni Dodonovd0d3e512012-05-09 15:37:16 -03005251
Imre Deakb4ed4482013-10-25 17:36:49 +03005252static void __intel_power_well_get(struct drm_device *dev,
5253 struct i915_power_well *power_well)
Ville Syrjälä2d66aef2013-09-16 17:38:29 +03005254{
Paulo Zanonid62292c2013-11-27 17:59:22 -02005255 struct drm_i915_private *dev_priv = dev->dev_private;
5256
5257 if (!power_well->count++ && power_well->set) {
5258 hsw_disable_package_c8(dev_priv);
Imre Deakc1ca7272013-11-25 17:15:29 +02005259 power_well->set(dev, power_well, true);
Paulo Zanonid62292c2013-11-27 17:59:22 -02005260 }
Ville Syrjälä2d66aef2013-09-16 17:38:29 +03005261}
5262
Imre Deakb4ed4482013-10-25 17:36:49 +03005263static void __intel_power_well_put(struct drm_device *dev,
5264 struct i915_power_well *power_well)
Ville Syrjälä2d66aef2013-09-16 17:38:29 +03005265{
Paulo Zanonid62292c2013-11-27 17:59:22 -02005266 struct drm_i915_private *dev_priv = dev->dev_private;
5267
Ville Syrjälä2d66aef2013-09-16 17:38:29 +03005268 WARN_ON(!power_well->count);
Imre Deakc1ca7272013-11-25 17:15:29 +02005269
Paulo Zanonid62292c2013-11-27 17:59:22 -02005270 if (!--power_well->count && power_well->set &&
5271 i915_disable_power_well) {
Imre Deakc1ca7272013-11-25 17:15:29 +02005272 power_well->set(dev, power_well, false);
Paulo Zanonid62292c2013-11-27 17:59:22 -02005273 hsw_enable_package_c8(dev_priv);
5274 }
Ville Syrjälä2d66aef2013-09-16 17:38:29 +03005275}
5276
Ville Syrjälä67656252013-09-16 17:38:28 +03005277void intel_display_power_get(struct drm_device *dev,
5278 enum intel_display_power_domain domain)
5279{
5280 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak83c00f52013-10-25 17:36:47 +03005281 struct i915_power_domains *power_domains;
Imre Deakc1ca7272013-11-25 17:15:29 +02005282 struct i915_power_well *power_well;
5283 int i;
Ville Syrjälä67656252013-09-16 17:38:28 +03005284
Imre Deak83c00f52013-10-25 17:36:47 +03005285 power_domains = &dev_priv->power_domains;
5286
5287 mutex_lock(&power_domains->lock);
Imre Deak1da51582013-11-25 17:15:35 +02005288
Imre Deakc1ca7272013-11-25 17:15:29 +02005289 for_each_power_well(i, power_well, BIT(domain), power_domains)
5290 __intel_power_well_get(dev, power_well);
Imre Deak1da51582013-11-25 17:15:35 +02005291
Imre Deakddf9c532013-11-27 22:02:02 +02005292 power_domains->domain_use_count[domain]++;
5293
Imre Deak83c00f52013-10-25 17:36:47 +03005294 mutex_unlock(&power_domains->lock);
Ville Syrjälä67656252013-09-16 17:38:28 +03005295}
5296
5297void intel_display_power_put(struct drm_device *dev,
5298 enum intel_display_power_domain domain)
5299{
5300 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak83c00f52013-10-25 17:36:47 +03005301 struct i915_power_domains *power_domains;
Imre Deakc1ca7272013-11-25 17:15:29 +02005302 struct i915_power_well *power_well;
5303 int i;
Ville Syrjälä67656252013-09-16 17:38:28 +03005304
Imre Deak83c00f52013-10-25 17:36:47 +03005305 power_domains = &dev_priv->power_domains;
5306
5307 mutex_lock(&power_domains->lock);
Imre Deak1da51582013-11-25 17:15:35 +02005308
Imre Deak1da51582013-11-25 17:15:35 +02005309 WARN_ON(!power_domains->domain_use_count[domain]);
5310 power_domains->domain_use_count[domain]--;
Imre Deakddf9c532013-11-27 22:02:02 +02005311
5312 for_each_power_well_rev(i, power_well, BIT(domain), power_domains)
5313 __intel_power_well_put(dev, power_well);
Imre Deak1da51582013-11-25 17:15:35 +02005314
Imre Deak83c00f52013-10-25 17:36:47 +03005315 mutex_unlock(&power_domains->lock);
Ville Syrjälä67656252013-09-16 17:38:28 +03005316}
5317
Imre Deak83c00f52013-10-25 17:36:47 +03005318static struct i915_power_domains *hsw_pwr;
Wang Xingchaoa38911a2013-05-30 22:07:11 +08005319
5320/* Display audio driver power well request */
5321void i915_request_power_well(void)
5322{
Imre Deakb4ed4482013-10-25 17:36:49 +03005323 struct drm_i915_private *dev_priv;
5324
Wang Xingchaoa38911a2013-05-30 22:07:11 +08005325 if (WARN_ON(!hsw_pwr))
5326 return;
5327
Imre Deakb4ed4482013-10-25 17:36:49 +03005328 dev_priv = container_of(hsw_pwr, struct drm_i915_private,
5329 power_domains);
Imre Deakfbeeaa22013-11-25 17:15:28 +02005330 intel_display_power_get(dev_priv->dev, POWER_DOMAIN_AUDIO);
Wang Xingchaoa38911a2013-05-30 22:07:11 +08005331}
5332EXPORT_SYMBOL_GPL(i915_request_power_well);
5333
5334/* Display audio driver power well release */
5335void i915_release_power_well(void)
5336{
Imre Deakb4ed4482013-10-25 17:36:49 +03005337 struct drm_i915_private *dev_priv;
5338
Wang Xingchaoa38911a2013-05-30 22:07:11 +08005339 if (WARN_ON(!hsw_pwr))
5340 return;
5341
Imre Deakb4ed4482013-10-25 17:36:49 +03005342 dev_priv = container_of(hsw_pwr, struct drm_i915_private,
5343 power_domains);
Imre Deakfbeeaa22013-11-25 17:15:28 +02005344 intel_display_power_put(dev_priv->dev, POWER_DOMAIN_AUDIO);
Wang Xingchaoa38911a2013-05-30 22:07:11 +08005345}
5346EXPORT_SYMBOL_GPL(i915_release_power_well);
5347
Imre Deak1c2256d2013-11-25 17:15:34 +02005348static struct i915_power_well i9xx_always_on_power_well[] = {
5349 {
5350 .name = "always-on",
5351 .always_on = 1,
5352 .domains = POWER_DOMAIN_MASK,
5353 },
5354};
5355
Imre Deakc1ca7272013-11-25 17:15:29 +02005356static struct i915_power_well hsw_power_wells[] = {
5357 {
Imre Deak6f3ef5d2013-11-25 17:15:30 +02005358 .name = "always-on",
5359 .always_on = 1,
5360 .domains = HSW_ALWAYS_ON_POWER_DOMAINS,
5361 },
5362 {
Imre Deakc1ca7272013-11-25 17:15:29 +02005363 .name = "display",
5364 .domains = POWER_DOMAIN_MASK & ~HSW_ALWAYS_ON_POWER_DOMAINS,
5365 .is_enabled = hsw_power_well_enabled,
5366 .set = hsw_set_power_well,
5367 },
5368};
5369
5370static struct i915_power_well bdw_power_wells[] = {
5371 {
Imre Deak6f3ef5d2013-11-25 17:15:30 +02005372 .name = "always-on",
5373 .always_on = 1,
5374 .domains = BDW_ALWAYS_ON_POWER_DOMAINS,
5375 },
5376 {
Imre Deakc1ca7272013-11-25 17:15:29 +02005377 .name = "display",
5378 .domains = POWER_DOMAIN_MASK & ~BDW_ALWAYS_ON_POWER_DOMAINS,
5379 .is_enabled = hsw_power_well_enabled,
5380 .set = hsw_set_power_well,
5381 },
5382};
5383
5384#define set_power_wells(power_domains, __power_wells) ({ \
5385 (power_domains)->power_wells = (__power_wells); \
5386 (power_domains)->power_well_count = ARRAY_SIZE(__power_wells); \
5387})
5388
Imre Deakddb642f2013-10-28 17:20:35 +02005389int intel_power_domains_init(struct drm_device *dev)
Wang Xingchaoa38911a2013-05-30 22:07:11 +08005390{
5391 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak83c00f52013-10-25 17:36:47 +03005392 struct i915_power_domains *power_domains = &dev_priv->power_domains;
Imre Deakc1ca7272013-11-25 17:15:29 +02005393
Imre Deak83c00f52013-10-25 17:36:47 +03005394 mutex_init(&power_domains->lock);
Wang Xingchaoa38911a2013-05-30 22:07:11 +08005395
Imre Deakc1ca7272013-11-25 17:15:29 +02005396 /*
5397 * The enabling order will be from lower to higher indexed wells,
5398 * the disabling order is reversed.
5399 */
5400 if (IS_HASWELL(dev)) {
5401 set_power_wells(power_domains, hsw_power_wells);
5402 hsw_pwr = power_domains;
5403 } else if (IS_BROADWELL(dev)) {
5404 set_power_wells(power_domains, bdw_power_wells);
5405 hsw_pwr = power_domains;
5406 } else {
Imre Deak1c2256d2013-11-25 17:15:34 +02005407 set_power_wells(power_domains, i9xx_always_on_power_well);
Imre Deakc1ca7272013-11-25 17:15:29 +02005408 }
Wang Xingchaoa38911a2013-05-30 22:07:11 +08005409
5410 return 0;
5411}
5412
Imre Deakddb642f2013-10-28 17:20:35 +02005413void intel_power_domains_remove(struct drm_device *dev)
Wang Xingchaoa38911a2013-05-30 22:07:11 +08005414{
5415 hsw_pwr = NULL;
5416}
5417
Imre Deakddb642f2013-10-28 17:20:35 +02005418static void intel_power_domains_resume(struct drm_device *dev)
Ville Syrjälä9cdb8262013-09-16 17:38:27 +03005419{
5420 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak83c00f52013-10-25 17:36:47 +03005421 struct i915_power_domains *power_domains = &dev_priv->power_domains;
5422 struct i915_power_well *power_well;
Imre Deakc1ca7272013-11-25 17:15:29 +02005423 int i;
Ville Syrjälä9cdb8262013-09-16 17:38:27 +03005424
Imre Deak83c00f52013-10-25 17:36:47 +03005425 mutex_lock(&power_domains->lock);
Imre Deakc1ca7272013-11-25 17:15:29 +02005426 for_each_power_well(i, power_well, POWER_DOMAIN_MASK, power_domains) {
5427 if (power_well->set)
5428 power_well->set(dev, power_well, power_well->count > 0);
5429 }
Imre Deak83c00f52013-10-25 17:36:47 +03005430 mutex_unlock(&power_domains->lock);
Wang Xingchaoa38911a2013-05-30 22:07:11 +08005431}
5432
Paulo Zanonifa42e232013-01-25 16:59:11 -02005433/*
5434 * Starting with Haswell, we have a "Power Down Well" that can be turned off
5435 * when not needed anymore. We have 4 registers that can request the power well
5436 * to be enabled, and it will only be disabled if none of the registers is
5437 * requesting it to be enabled.
5438 */
Imre Deakddb642f2013-10-28 17:20:35 +02005439void intel_power_domains_init_hw(struct drm_device *dev)
Paulo Zanonifa42e232013-01-25 16:59:11 -02005440{
5441 struct drm_i915_private *dev_priv = dev->dev_private;
Eugeni Dodonovd0d3e512012-05-09 15:37:16 -03005442
Paulo Zanonifa42e232013-01-25 16:59:11 -02005443 /* For now, we need the power well to be always enabled. */
Imre Deakbaa70702013-10-25 17:36:48 +03005444 intel_display_set_init_power(dev, true);
Imre Deakddb642f2013-10-28 17:20:35 +02005445 intel_power_domains_resume(dev);
Eugeni Dodonovd0d3e512012-05-09 15:37:16 -03005446
Imre Deakf7243ac2013-11-25 17:15:33 +02005447 if (!(IS_HASWELL(dev) || IS_BROADWELL(dev)))
5448 return;
5449
Paulo Zanonifa42e232013-01-25 16:59:11 -02005450 /* We're taking over the BIOS, so clear any requests made by it since
5451 * the driver is in charge now. */
Paulo Zanoni6aedd1f2013-08-02 16:22:25 -03005452 if (I915_READ(HSW_PWR_WELL_BIOS) & HSW_PWR_WELL_ENABLE_REQUEST)
Paulo Zanonifa42e232013-01-25 16:59:11 -02005453 I915_WRITE(HSW_PWR_WELL_BIOS, 0);
Eugeni Dodonovd0d3e512012-05-09 15:37:16 -03005454}
5455
Paulo Zanonic67a4702013-08-19 13:18:09 -03005456/* Disables PC8 so we can use the GMBUS and DP AUX interrupts. */
5457void intel_aux_display_runtime_get(struct drm_i915_private *dev_priv)
5458{
5459 hsw_disable_package_c8(dev_priv);
5460}
5461
5462void intel_aux_display_runtime_put(struct drm_i915_private *dev_priv)
5463{
5464 hsw_enable_package_c8(dev_priv);
5465}
5466
Paulo Zanoni8a187452013-12-06 20:32:13 -02005467void intel_runtime_pm_get(struct drm_i915_private *dev_priv)
5468{
5469 struct drm_device *dev = dev_priv->dev;
5470 struct device *device = &dev->pdev->dev;
5471
5472 if (!HAS_RUNTIME_PM(dev))
5473 return;
5474
5475 pm_runtime_get_sync(device);
5476 WARN(dev_priv->pm.suspended, "Device still suspended.\n");
5477}
5478
5479void intel_runtime_pm_put(struct drm_i915_private *dev_priv)
5480{
5481 struct drm_device *dev = dev_priv->dev;
5482 struct device *device = &dev->pdev->dev;
5483
5484 if (!HAS_RUNTIME_PM(dev))
5485 return;
5486
5487 pm_runtime_mark_last_busy(device);
5488 pm_runtime_put_autosuspend(device);
5489}
5490
5491void intel_init_runtime_pm(struct drm_i915_private *dev_priv)
5492{
5493 struct drm_device *dev = dev_priv->dev;
5494 struct device *device = &dev->pdev->dev;
5495
5496 dev_priv->pm.suspended = false;
5497
5498 if (!HAS_RUNTIME_PM(dev))
5499 return;
5500
5501 pm_runtime_set_active(device);
5502
5503 pm_runtime_set_autosuspend_delay(device, 10000); /* 10s */
5504 pm_runtime_mark_last_busy(device);
5505 pm_runtime_use_autosuspend(device);
5506}
5507
5508void intel_fini_runtime_pm(struct drm_i915_private *dev_priv)
5509{
5510 struct drm_device *dev = dev_priv->dev;
5511 struct device *device = &dev->pdev->dev;
5512
5513 if (!HAS_RUNTIME_PM(dev))
5514 return;
5515
5516 /* Make sure we're not suspended first. */
5517 pm_runtime_get_sync(device);
5518 pm_runtime_disable(device);
5519}
5520
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005521/* Set up chip specific power management-related functions */
5522void intel_init_pm(struct drm_device *dev)
5523{
5524 struct drm_i915_private *dev_priv = dev->dev_private;
5525
Daniel Vetter3a77c4c2014-01-10 08:50:12 +01005526 if (HAS_FBC(dev)) {
Ville Syrjälä40045462013-11-28 17:29:59 +02005527 if (INTEL_INFO(dev)->gen >= 7) {
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005528 dev_priv->display.fbc_enabled = ironlake_fbc_enabled;
Ville Syrjälä40045462013-11-28 17:29:59 +02005529 dev_priv->display.enable_fbc = gen7_enable_fbc;
5530 dev_priv->display.disable_fbc = ironlake_disable_fbc;
5531 } else if (INTEL_INFO(dev)->gen >= 5) {
5532 dev_priv->display.fbc_enabled = ironlake_fbc_enabled;
5533 dev_priv->display.enable_fbc = ironlake_enable_fbc;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005534 dev_priv->display.disable_fbc = ironlake_disable_fbc;
5535 } else if (IS_GM45(dev)) {
5536 dev_priv->display.fbc_enabled = g4x_fbc_enabled;
5537 dev_priv->display.enable_fbc = g4x_enable_fbc;
5538 dev_priv->display.disable_fbc = g4x_disable_fbc;
Ville Syrjälä40045462013-11-28 17:29:59 +02005539 } else {
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005540 dev_priv->display.fbc_enabled = i8xx_fbc_enabled;
5541 dev_priv->display.enable_fbc = i8xx_enable_fbc;
5542 dev_priv->display.disable_fbc = i8xx_disable_fbc;
Ville Syrjälä993495a2013-12-12 17:27:40 +02005543
5544 /* This value was pulled out of someone's hat */
5545 I915_WRITE(FBC_CONTROL, 500 << FBC_CTL_INTERVAL_SHIFT);
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005546 }
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005547 }
5548
Daniel Vetterc921aba2012-04-26 23:28:17 +02005549 /* For cxsr */
5550 if (IS_PINEVIEW(dev))
5551 i915_pineview_get_mem_freq(dev);
5552 else if (IS_GEN5(dev))
5553 i915_ironlake_get_mem_freq(dev);
5554
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005555 /* For FIFO watermark updates */
5556 if (HAS_PCH_SPLIT(dev)) {
Ville Syrjälä53615a52013-08-01 16:18:50 +03005557 intel_setup_wm_latency(dev);
5558
Ville Syrjäläbd6025442014-01-07 16:14:10 +02005559 if ((IS_GEN5(dev) && dev_priv->wm.pri_latency[1] &&
5560 dev_priv->wm.spr_latency[1] && dev_priv->wm.cur_latency[1]) ||
5561 (!IS_GEN5(dev) && dev_priv->wm.pri_latency[0] &&
5562 dev_priv->wm.spr_latency[0] && dev_priv->wm.cur_latency[0])) {
5563 dev_priv->display.update_wm = ilk_update_wm;
5564 dev_priv->display.update_sprite_wm = ilk_update_sprite_wm;
5565 } else {
5566 DRM_DEBUG_KMS("Failed to read display plane latency. "
5567 "Disable CxSR\n");
5568 }
5569
5570 if (IS_GEN5(dev))
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005571 dev_priv->display.init_clock_gating = ironlake_init_clock_gating;
Ville Syrjäläbd6025442014-01-07 16:14:10 +02005572 else if (IS_GEN6(dev))
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005573 dev_priv->display.init_clock_gating = gen6_init_clock_gating;
Ville Syrjäläbd6025442014-01-07 16:14:10 +02005574 else if (IS_IVYBRIDGE(dev))
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005575 dev_priv->display.init_clock_gating = ivybridge_init_clock_gating;
Ville Syrjäläbd6025442014-01-07 16:14:10 +02005576 else if (IS_HASWELL(dev))
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03005577 dev_priv->display.init_clock_gating = haswell_init_clock_gating;
Ville Syrjäläbd6025442014-01-07 16:14:10 +02005578 else if (INTEL_INFO(dev)->gen == 8)
Ben Widawsky1020a5c2013-11-02 21:07:06 -07005579 dev_priv->display.init_clock_gating = gen8_init_clock_gating;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005580 } else if (IS_VALLEYVIEW(dev)) {
5581 dev_priv->display.update_wm = valleyview_update_wm;
5582 dev_priv->display.init_clock_gating =
5583 valleyview_init_clock_gating;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005584 } else if (IS_PINEVIEW(dev)) {
5585 if (!intel_get_cxsr_latency(IS_PINEVIEW_G(dev),
5586 dev_priv->is_ddr3,
5587 dev_priv->fsb_freq,
5588 dev_priv->mem_freq)) {
5589 DRM_INFO("failed to find known CxSR latency "
5590 "(found ddr%s fsb freq %d, mem freq %d), "
5591 "disabling CxSR\n",
5592 (dev_priv->is_ddr3 == 1) ? "3" : "2",
5593 dev_priv->fsb_freq, dev_priv->mem_freq);
5594 /* Disable CxSR and never update its watermark again */
5595 pineview_disable_cxsr(dev);
5596 dev_priv->display.update_wm = NULL;
5597 } else
5598 dev_priv->display.update_wm = pineview_update_wm;
5599 dev_priv->display.init_clock_gating = gen3_init_clock_gating;
5600 } else if (IS_G4X(dev)) {
5601 dev_priv->display.update_wm = g4x_update_wm;
5602 dev_priv->display.init_clock_gating = g4x_init_clock_gating;
5603 } else if (IS_GEN4(dev)) {
5604 dev_priv->display.update_wm = i965_update_wm;
5605 if (IS_CRESTLINE(dev))
5606 dev_priv->display.init_clock_gating = crestline_init_clock_gating;
5607 else if (IS_BROADWATER(dev))
5608 dev_priv->display.init_clock_gating = broadwater_init_clock_gating;
5609 } else if (IS_GEN3(dev)) {
5610 dev_priv->display.update_wm = i9xx_update_wm;
5611 dev_priv->display.get_fifo_size = i9xx_get_fifo_size;
5612 dev_priv->display.init_clock_gating = gen3_init_clock_gating;
Daniel Vetterfeb56b92013-12-14 20:38:30 -02005613 } else if (IS_GEN2(dev)) {
5614 if (INTEL_INFO(dev)->num_pipes == 1) {
5615 dev_priv->display.update_wm = i845_update_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005616 dev_priv->display.get_fifo_size = i845_get_fifo_size;
Daniel Vetterfeb56b92013-12-14 20:38:30 -02005617 } else {
5618 dev_priv->display.update_wm = i9xx_update_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005619 dev_priv->display.get_fifo_size = i830_get_fifo_size;
Daniel Vetterfeb56b92013-12-14 20:38:30 -02005620 }
5621
5622 if (IS_I85X(dev) || IS_I865G(dev))
5623 dev_priv->display.init_clock_gating = i85x_init_clock_gating;
5624 else
5625 dev_priv->display.init_clock_gating = i830_init_clock_gating;
5626 } else {
5627 DRM_ERROR("unexpected fall-through in intel_init_pm\n");
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005628 }
5629}
5630
Ben Widawsky42c05262012-09-26 10:34:00 -07005631int sandybridge_pcode_read(struct drm_i915_private *dev_priv, u8 mbox, u32 *val)
5632{
Jesse Barnes4fc688c2012-11-02 11:14:01 -07005633 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Ben Widawsky42c05262012-09-26 10:34:00 -07005634
5635 if (I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) {
5636 DRM_DEBUG_DRIVER("warning: pcode (read) mailbox access failed\n");
5637 return -EAGAIN;
5638 }
5639
5640 I915_WRITE(GEN6_PCODE_DATA, *val);
5641 I915_WRITE(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY | mbox);
5642
5643 if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
5644 500)) {
5645 DRM_ERROR("timeout waiting for pcode read (%d) to finish\n", mbox);
5646 return -ETIMEDOUT;
5647 }
5648
5649 *val = I915_READ(GEN6_PCODE_DATA);
5650 I915_WRITE(GEN6_PCODE_DATA, 0);
5651
5652 return 0;
5653}
5654
5655int sandybridge_pcode_write(struct drm_i915_private *dev_priv, u8 mbox, u32 val)
5656{
Jesse Barnes4fc688c2012-11-02 11:14:01 -07005657 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Ben Widawsky42c05262012-09-26 10:34:00 -07005658
5659 if (I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) {
5660 DRM_DEBUG_DRIVER("warning: pcode (write) mailbox access failed\n");
5661 return -EAGAIN;
5662 }
5663
5664 I915_WRITE(GEN6_PCODE_DATA, val);
5665 I915_WRITE(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY | mbox);
5666
5667 if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
5668 500)) {
5669 DRM_ERROR("timeout waiting for pcode write (%d) to finish\n", mbox);
5670 return -ETIMEDOUT;
5671 }
5672
5673 I915_WRITE(GEN6_PCODE_DATA, 0);
5674
5675 return 0;
5676}
Jesse Barnesa0e4e192013-04-02 11:23:05 -07005677
Ville Syrjälä2ec38152013-11-05 22:42:29 +02005678int vlv_gpu_freq(struct drm_i915_private *dev_priv, int val)
Jesse Barnes855ba3b2013-04-17 15:54:57 -07005679{
Ville Syrjälä07ab1182013-11-05 22:42:28 +02005680 int div;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07005681
Ville Syrjälä07ab1182013-11-05 22:42:28 +02005682 /* 4 x czclk */
Ville Syrjälä2ec38152013-11-05 22:42:29 +02005683 switch (dev_priv->mem_freq) {
Jesse Barnes855ba3b2013-04-17 15:54:57 -07005684 case 800:
Ville Syrjälä07ab1182013-11-05 22:42:28 +02005685 div = 10;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07005686 break;
5687 case 1066:
Ville Syrjälä07ab1182013-11-05 22:42:28 +02005688 div = 12;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07005689 break;
5690 case 1333:
Ville Syrjälä07ab1182013-11-05 22:42:28 +02005691 div = 16;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07005692 break;
5693 default:
5694 return -1;
5695 }
5696
Ville Syrjälä2ec38152013-11-05 22:42:29 +02005697 return DIV_ROUND_CLOSEST(dev_priv->mem_freq * (val + 6 - 0xbd), 4 * div);
Jesse Barnes855ba3b2013-04-17 15:54:57 -07005698}
5699
Ville Syrjälä2ec38152013-11-05 22:42:29 +02005700int vlv_freq_opcode(struct drm_i915_private *dev_priv, int val)
Jesse Barnes855ba3b2013-04-17 15:54:57 -07005701{
Ville Syrjälä07ab1182013-11-05 22:42:28 +02005702 int mul;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07005703
Ville Syrjälä07ab1182013-11-05 22:42:28 +02005704 /* 4 x czclk */
Ville Syrjälä2ec38152013-11-05 22:42:29 +02005705 switch (dev_priv->mem_freq) {
Jesse Barnes855ba3b2013-04-17 15:54:57 -07005706 case 800:
Ville Syrjälä07ab1182013-11-05 22:42:28 +02005707 mul = 10;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07005708 break;
5709 case 1066:
Ville Syrjälä07ab1182013-11-05 22:42:28 +02005710 mul = 12;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07005711 break;
5712 case 1333:
Ville Syrjälä07ab1182013-11-05 22:42:28 +02005713 mul = 16;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07005714 break;
5715 default:
5716 return -1;
5717 }
5718
Ville Syrjälä2ec38152013-11-05 22:42:29 +02005719 return DIV_ROUND_CLOSEST(4 * mul * val, dev_priv->mem_freq) + 0xbd - 6;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07005720}
5721
Daniel Vetterf742a552013-12-06 10:17:53 +01005722void intel_pm_setup(struct drm_device *dev)
Chris Wilson907b28c2013-07-19 20:36:52 +01005723{
5724 struct drm_i915_private *dev_priv = dev->dev_private;
5725
Daniel Vetterf742a552013-12-06 10:17:53 +01005726 mutex_init(&dev_priv->rps.hw_lock);
5727
5728 mutex_init(&dev_priv->pc8.lock);
5729 dev_priv->pc8.requirements_met = false;
5730 dev_priv->pc8.gpu_idle = false;
5731 dev_priv->pc8.irqs_disabled = false;
5732 dev_priv->pc8.enabled = false;
5733 dev_priv->pc8.disable_count = 2; /* requirements_met + gpu_idle */
5734 INIT_DELAYED_WORK(&dev_priv->pc8.enable_work, hsw_enable_pc8_work);
Chris Wilson907b28c2013-07-19 20:36:52 +01005735 INIT_DELAYED_WORK(&dev_priv->rps.delayed_resume_work,
5736 intel_gen6_powersave_work);
5737}