blob: 743449a51be807fa24d307840ed5f14440c86101 [file] [log] [blame]
Eugeni Dodonov85208be2012-04-16 22:20:34 -03001/*
2 * Copyright © 2012 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eugeni Dodonov <eugeni.dodonov@intel.com>
25 *
26 */
27
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -030028#include <linux/cpufreq.h>
Eugeni Dodonov85208be2012-04-16 22:20:34 -030029#include "i915_drv.h"
30#include "intel_drv.h"
Daniel Vettereb48eb02012-04-26 23:28:12 +020031#include "../../../platform/x86/intel_ips.h"
32#include <linux/module.h>
Paulo Zanonif9dcb0d2013-12-11 18:50:10 -020033#include <linux/vgaarb.h>
Damien Lespiauf4db9322013-06-24 22:59:50 +010034#include <drm/i915_powerwell.h>
Paulo Zanoni8a187452013-12-06 20:32:13 -020035#include <linux/pm_runtime.h>
Eugeni Dodonov85208be2012-04-16 22:20:34 -030036
Ben Widawskydc39fff2013-10-18 12:32:07 -070037/**
38 * RC6 is a special power stage which allows the GPU to enter an very
39 * low-voltage mode when idle, using down to 0V while at this stage. This
40 * stage is entered automatically when the GPU is idle when RC6 support is
41 * enabled, and as soon as new workload arises GPU wakes up automatically as well.
42 *
43 * There are different RC6 modes available in Intel GPU, which differentiate
44 * among each other with the latency required to enter and leave RC6 and
45 * voltage consumed by the GPU in different states.
46 *
47 * The combination of the following flags define which states GPU is allowed
48 * to enter, while RC6 is the normal RC6 state, RC6p is the deep RC6, and
49 * RC6pp is deepest RC6. Their support by hardware varies according to the
50 * GPU, BIOS, chipset and platform. RC6 is usually the safest one and the one
51 * which brings the most power savings; deeper states save more power, but
52 * require higher latency to switch to and wake up.
53 */
54#define INTEL_RC6_ENABLE (1<<0)
55#define INTEL_RC6p_ENABLE (1<<1)
56#define INTEL_RC6pp_ENABLE (1<<2)
57
Eugeni Dodonovf6750b32012-04-18 11:51:14 -030058/* FBC, or Frame Buffer Compression, is a technique employed to compress the
59 * framebuffer contents in-memory, aiming at reducing the required bandwidth
60 * during in-memory transfers and, therefore, reduce the power packet.
Eugeni Dodonov85208be2012-04-16 22:20:34 -030061 *
Eugeni Dodonovf6750b32012-04-18 11:51:14 -030062 * The benefits of FBC are mostly visible with solid backgrounds and
63 * variation-less patterns.
Eugeni Dodonov85208be2012-04-16 22:20:34 -030064 *
Eugeni Dodonovf6750b32012-04-18 11:51:14 -030065 * FBC-related functionality can be enabled by the means of the
66 * i915.i915_enable_fbc parameter
Eugeni Dodonov85208be2012-04-16 22:20:34 -030067 */
68
Eugeni Dodonov1fa61102012-04-18 15:29:26 -030069static void i8xx_disable_fbc(struct drm_device *dev)
Eugeni Dodonov85208be2012-04-16 22:20:34 -030070{
71 struct drm_i915_private *dev_priv = dev->dev_private;
72 u32 fbc_ctl;
73
74 /* Disable compression */
75 fbc_ctl = I915_READ(FBC_CONTROL);
76 if ((fbc_ctl & FBC_CTL_EN) == 0)
77 return;
78
79 fbc_ctl &= ~FBC_CTL_EN;
80 I915_WRITE(FBC_CONTROL, fbc_ctl);
81
82 /* Wait for compressing bit to clear */
83 if (wait_for((I915_READ(FBC_STATUS) & FBC_STAT_COMPRESSING) == 0, 10)) {
84 DRM_DEBUG_KMS("FBC idle timed out\n");
85 return;
86 }
87
88 DRM_DEBUG_KMS("disabled FBC\n");
89}
90
Ville Syrjälä993495a2013-12-12 17:27:40 +020091static void i8xx_enable_fbc(struct drm_crtc *crtc)
Eugeni Dodonov85208be2012-04-16 22:20:34 -030092{
93 struct drm_device *dev = crtc->dev;
94 struct drm_i915_private *dev_priv = dev->dev_private;
95 struct drm_framebuffer *fb = crtc->fb;
96 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
97 struct drm_i915_gem_object *obj = intel_fb->obj;
98 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
99 int cfb_pitch;
100 int plane, i;
Ville Syrjälä159f9872013-11-28 17:29:57 +0200101 u32 fbc_ctl;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300102
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -0700103 cfb_pitch = dev_priv->fbc.size / FBC_LL_SIZE;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300104 if (fb->pitches[0] < cfb_pitch)
105 cfb_pitch = fb->pitches[0];
106
Ville Syrjälä42a430f2013-11-28 17:29:56 +0200107 /* FBC_CTL wants 32B or 64B units */
108 if (IS_GEN2(dev))
109 cfb_pitch = (cfb_pitch / 32) - 1;
110 else
111 cfb_pitch = (cfb_pitch / 64) - 1;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300112 plane = intel_crtc->plane == 0 ? FBC_CTL_PLANEA : FBC_CTL_PLANEB;
113
114 /* Clear old tags */
115 for (i = 0; i < (FBC_LL_SIZE / 32) + 1; i++)
116 I915_WRITE(FBC_TAG + (i * 4), 0);
117
Ville Syrjälä159f9872013-11-28 17:29:57 +0200118 if (IS_GEN4(dev)) {
119 u32 fbc_ctl2;
120
121 /* Set it up... */
122 fbc_ctl2 = FBC_CTL_FENCE_DBL | FBC_CTL_IDLE_IMM | FBC_CTL_CPU_FENCE;
123 fbc_ctl2 |= plane;
124 I915_WRITE(FBC_CONTROL2, fbc_ctl2);
125 I915_WRITE(FBC_FENCE_OFF, crtc->y);
126 }
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300127
128 /* enable it... */
Ville Syrjälä993495a2013-12-12 17:27:40 +0200129 fbc_ctl = I915_READ(FBC_CONTROL);
130 fbc_ctl &= 0x3fff << FBC_CTL_INTERVAL_SHIFT;
131 fbc_ctl |= FBC_CTL_EN | FBC_CTL_PERIODIC;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300132 if (IS_I945GM(dev))
133 fbc_ctl |= FBC_CTL_C3_IDLE; /* 945 needs special SR handling */
134 fbc_ctl |= (cfb_pitch & 0xff) << FBC_CTL_STRIDE_SHIFT;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300135 fbc_ctl |= obj->fence_reg;
136 I915_WRITE(FBC_CONTROL, fbc_ctl);
137
Ville Syrjälä84f44ce2013-04-17 17:48:49 +0300138 DRM_DEBUG_KMS("enabled FBC, pitch %d, yoff %d, plane %c, ",
139 cfb_pitch, crtc->y, plane_name(intel_crtc->plane));
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300140}
141
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300142static bool i8xx_fbc_enabled(struct drm_device *dev)
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300143{
144 struct drm_i915_private *dev_priv = dev->dev_private;
145
146 return I915_READ(FBC_CONTROL) & FBC_CTL_EN;
147}
148
Ville Syrjälä993495a2013-12-12 17:27:40 +0200149static void g4x_enable_fbc(struct drm_crtc *crtc)
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300150{
151 struct drm_device *dev = crtc->dev;
152 struct drm_i915_private *dev_priv = dev->dev_private;
153 struct drm_framebuffer *fb = crtc->fb;
154 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
155 struct drm_i915_gem_object *obj = intel_fb->obj;
156 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
157 int plane = intel_crtc->plane == 0 ? DPFC_CTL_PLANEA : DPFC_CTL_PLANEB;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300158 u32 dpfc_ctl;
159
160 dpfc_ctl = plane | DPFC_SR_EN | DPFC_CTL_LIMIT_1X;
161 dpfc_ctl |= DPFC_CTL_FENCE_EN | obj->fence_reg;
162 I915_WRITE(DPFC_CHICKEN, DPFC_HT_MODIFY);
163
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300164 I915_WRITE(DPFC_FENCE_YOFF, crtc->y);
165
166 /* enable it... */
167 I915_WRITE(DPFC_CONTROL, I915_READ(DPFC_CONTROL) | DPFC_CTL_EN);
168
Ville Syrjälä84f44ce2013-04-17 17:48:49 +0300169 DRM_DEBUG_KMS("enabled fbc on plane %c\n", plane_name(intel_crtc->plane));
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300170}
171
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300172static void g4x_disable_fbc(struct drm_device *dev)
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300173{
174 struct drm_i915_private *dev_priv = dev->dev_private;
175 u32 dpfc_ctl;
176
177 /* Disable compression */
178 dpfc_ctl = I915_READ(DPFC_CONTROL);
179 if (dpfc_ctl & DPFC_CTL_EN) {
180 dpfc_ctl &= ~DPFC_CTL_EN;
181 I915_WRITE(DPFC_CONTROL, dpfc_ctl);
182
183 DRM_DEBUG_KMS("disabled FBC\n");
184 }
185}
186
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300187static bool g4x_fbc_enabled(struct drm_device *dev)
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300188{
189 struct drm_i915_private *dev_priv = dev->dev_private;
190
191 return I915_READ(DPFC_CONTROL) & DPFC_CTL_EN;
192}
193
194static void sandybridge_blit_fbc_update(struct drm_device *dev)
195{
196 struct drm_i915_private *dev_priv = dev->dev_private;
197 u32 blt_ecoskpd;
198
199 /* Make sure blitter notifies FBC of writes */
Deepak S940aece2013-11-23 14:55:43 +0530200
201 /* Blitter is part of Media powerwell on VLV. No impact of
202 * his param in other platforms for now */
203 gen6_gt_force_wake_get(dev_priv, FORCEWAKE_MEDIA);
Deepak Sc8d9a592013-11-23 14:55:42 +0530204
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300205 blt_ecoskpd = I915_READ(GEN6_BLITTER_ECOSKPD);
206 blt_ecoskpd |= GEN6_BLITTER_FBC_NOTIFY <<
207 GEN6_BLITTER_LOCK_SHIFT;
208 I915_WRITE(GEN6_BLITTER_ECOSKPD, blt_ecoskpd);
209 blt_ecoskpd |= GEN6_BLITTER_FBC_NOTIFY;
210 I915_WRITE(GEN6_BLITTER_ECOSKPD, blt_ecoskpd);
211 blt_ecoskpd &= ~(GEN6_BLITTER_FBC_NOTIFY <<
212 GEN6_BLITTER_LOCK_SHIFT);
213 I915_WRITE(GEN6_BLITTER_ECOSKPD, blt_ecoskpd);
214 POSTING_READ(GEN6_BLITTER_ECOSKPD);
Deepak Sc8d9a592013-11-23 14:55:42 +0530215
Deepak S940aece2013-11-23 14:55:43 +0530216 gen6_gt_force_wake_put(dev_priv, FORCEWAKE_MEDIA);
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300217}
218
Ville Syrjälä993495a2013-12-12 17:27:40 +0200219static void ironlake_enable_fbc(struct drm_crtc *crtc)
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300220{
221 struct drm_device *dev = crtc->dev;
222 struct drm_i915_private *dev_priv = dev->dev_private;
223 struct drm_framebuffer *fb = crtc->fb;
224 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
225 struct drm_i915_gem_object *obj = intel_fb->obj;
226 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
227 int plane = intel_crtc->plane == 0 ? DPFC_CTL_PLANEA : DPFC_CTL_PLANEB;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300228 u32 dpfc_ctl;
229
230 dpfc_ctl = I915_READ(ILK_DPFC_CONTROL);
231 dpfc_ctl &= DPFC_RESERVED;
232 dpfc_ctl |= (plane | DPFC_CTL_LIMIT_1X);
233 /* Set persistent mode for front-buffer rendering, ala X. */
234 dpfc_ctl |= DPFC_CTL_PERSISTENT_MODE;
Ville Syrjäläd6293362013-11-21 21:29:45 +0200235 dpfc_ctl |= DPFC_CTL_FENCE_EN;
236 if (IS_GEN5(dev))
237 dpfc_ctl |= obj->fence_reg;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300238 I915_WRITE(ILK_DPFC_CHICKEN, DPFC_HT_MODIFY);
239
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300240 I915_WRITE(ILK_DPFC_FENCE_YOFF, crtc->y);
Ben Widawskyf343c5f2013-07-05 14:41:04 -0700241 I915_WRITE(ILK_FBC_RT_BASE, i915_gem_obj_ggtt_offset(obj) | ILK_FBC_RT_VALID);
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300242 /* enable it... */
243 I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl | DPFC_CTL_EN);
244
245 if (IS_GEN6(dev)) {
246 I915_WRITE(SNB_DPFC_CTL_SA,
247 SNB_CPU_FENCE_ENABLE | obj->fence_reg);
248 I915_WRITE(DPFC_CPU_FENCE_OFFSET, crtc->y);
249 sandybridge_blit_fbc_update(dev);
250 }
251
Ville Syrjälä84f44ce2013-04-17 17:48:49 +0300252 DRM_DEBUG_KMS("enabled fbc on plane %c\n", plane_name(intel_crtc->plane));
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300253}
254
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300255static void ironlake_disable_fbc(struct drm_device *dev)
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300256{
257 struct drm_i915_private *dev_priv = dev->dev_private;
258 u32 dpfc_ctl;
259
260 /* Disable compression */
261 dpfc_ctl = I915_READ(ILK_DPFC_CONTROL);
262 if (dpfc_ctl & DPFC_CTL_EN) {
263 dpfc_ctl &= ~DPFC_CTL_EN;
264 I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl);
265
266 DRM_DEBUG_KMS("disabled FBC\n");
267 }
268}
269
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300270static bool ironlake_fbc_enabled(struct drm_device *dev)
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300271{
272 struct drm_i915_private *dev_priv = dev->dev_private;
273
274 return I915_READ(ILK_DPFC_CONTROL) & DPFC_CTL_EN;
275}
276
Ville Syrjälä993495a2013-12-12 17:27:40 +0200277static void gen7_enable_fbc(struct drm_crtc *crtc)
Rodrigo Viviabe959c2013-05-06 19:37:33 -0300278{
279 struct drm_device *dev = crtc->dev;
280 struct drm_i915_private *dev_priv = dev->dev_private;
281 struct drm_framebuffer *fb = crtc->fb;
282 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
283 struct drm_i915_gem_object *obj = intel_fb->obj;
284 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
285
Ben Widawskyf343c5f2013-07-05 14:41:04 -0700286 I915_WRITE(IVB_FBC_RT_BASE, i915_gem_obj_ggtt_offset(obj));
Rodrigo Viviabe959c2013-05-06 19:37:33 -0300287
288 I915_WRITE(ILK_DPFC_CONTROL, DPFC_CTL_EN | DPFC_CTL_LIMIT_1X |
289 IVB_DPFC_CTL_FENCE_EN |
290 intel_crtc->plane << IVB_DPFC_CTL_PLANE_SHIFT);
291
Rodrigo Vivi891348b2013-05-06 19:37:36 -0300292 if (IS_IVYBRIDGE(dev)) {
Damien Lespiau7dd23ba2013-05-10 14:33:17 +0100293 /* WaFbcAsynchFlipDisableFbcQueue:ivb */
Rodrigo Vivi891348b2013-05-06 19:37:36 -0300294 I915_WRITE(ILK_DISPLAY_CHICKEN1, ILK_FBCQ_DIS);
Rodrigo Vivi28554162013-05-06 19:37:37 -0300295 } else {
Damien Lespiau7dd23ba2013-05-10 14:33:17 +0100296 /* WaFbcAsynchFlipDisableFbcQueue:hsw */
Rodrigo Vivi28554162013-05-06 19:37:37 -0300297 I915_WRITE(HSW_PIPE_SLICE_CHICKEN_1(intel_crtc->pipe),
298 HSW_BYPASS_FBC_QUEUE);
Rodrigo Vivi891348b2013-05-06 19:37:36 -0300299 }
Rodrigo Vivib74ea102013-05-09 14:08:38 -0300300
Rodrigo Viviabe959c2013-05-06 19:37:33 -0300301 I915_WRITE(SNB_DPFC_CTL_SA,
302 SNB_CPU_FENCE_ENABLE | obj->fence_reg);
303 I915_WRITE(DPFC_CPU_FENCE_OFFSET, crtc->y);
304
305 sandybridge_blit_fbc_update(dev);
306
Ville Syrjäläb19870e2013-11-06 23:02:25 +0200307 DRM_DEBUG_KMS("enabled fbc on plane %c\n", plane_name(intel_crtc->plane));
Rodrigo Viviabe959c2013-05-06 19:37:33 -0300308}
309
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300310bool intel_fbc_enabled(struct drm_device *dev)
311{
312 struct drm_i915_private *dev_priv = dev->dev_private;
313
314 if (!dev_priv->display.fbc_enabled)
315 return false;
316
317 return dev_priv->display.fbc_enabled(dev);
318}
319
320static void intel_fbc_work_fn(struct work_struct *__work)
321{
322 struct intel_fbc_work *work =
323 container_of(to_delayed_work(__work),
324 struct intel_fbc_work, work);
325 struct drm_device *dev = work->crtc->dev;
326 struct drm_i915_private *dev_priv = dev->dev_private;
327
328 mutex_lock(&dev->struct_mutex);
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -0700329 if (work == dev_priv->fbc.fbc_work) {
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300330 /* Double check that we haven't switched fb without cancelling
331 * the prior work.
332 */
333 if (work->crtc->fb == work->fb) {
Ville Syrjälä993495a2013-12-12 17:27:40 +0200334 dev_priv->display.enable_fbc(work->crtc);
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300335
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -0700336 dev_priv->fbc.plane = to_intel_crtc(work->crtc)->plane;
337 dev_priv->fbc.fb_id = work->crtc->fb->base.id;
338 dev_priv->fbc.y = work->crtc->y;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300339 }
340
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -0700341 dev_priv->fbc.fbc_work = NULL;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300342 }
343 mutex_unlock(&dev->struct_mutex);
344
345 kfree(work);
346}
347
348static void intel_cancel_fbc_work(struct drm_i915_private *dev_priv)
349{
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -0700350 if (dev_priv->fbc.fbc_work == NULL)
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300351 return;
352
353 DRM_DEBUG_KMS("cancelling pending FBC enable\n");
354
355 /* Synchronisation is provided by struct_mutex and checking of
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -0700356 * dev_priv->fbc.fbc_work, so we can perform the cancellation
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300357 * entirely asynchronously.
358 */
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -0700359 if (cancel_delayed_work(&dev_priv->fbc.fbc_work->work))
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300360 /* tasklet was killed before being run, clean up */
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -0700361 kfree(dev_priv->fbc.fbc_work);
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300362
363 /* Mark the work as no longer wanted so that if it does
364 * wake-up (because the work was already running and waiting
365 * for our mutex), it will discover that is no longer
366 * necessary to run.
367 */
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -0700368 dev_priv->fbc.fbc_work = NULL;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300369}
370
Ville Syrjälä993495a2013-12-12 17:27:40 +0200371static void intel_enable_fbc(struct drm_crtc *crtc)
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300372{
373 struct intel_fbc_work *work;
374 struct drm_device *dev = crtc->dev;
375 struct drm_i915_private *dev_priv = dev->dev_private;
376
377 if (!dev_priv->display.enable_fbc)
378 return;
379
380 intel_cancel_fbc_work(dev_priv);
381
Daniel Vetterb14c5672013-09-19 12:18:32 +0200382 work = kzalloc(sizeof(*work), GFP_KERNEL);
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300383 if (work == NULL) {
Paulo Zanoni6cdcb5e2013-06-12 17:27:29 -0300384 DRM_ERROR("Failed to allocate FBC work structure\n");
Ville Syrjälä993495a2013-12-12 17:27:40 +0200385 dev_priv->display.enable_fbc(crtc);
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300386 return;
387 }
388
389 work->crtc = crtc;
390 work->fb = crtc->fb;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300391 INIT_DELAYED_WORK(&work->work, intel_fbc_work_fn);
392
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -0700393 dev_priv->fbc.fbc_work = work;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300394
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300395 /* Delay the actual enabling to let pageflipping cease and the
396 * display to settle before starting the compression. Note that
397 * this delay also serves a second purpose: it allows for a
398 * vblank to pass after disabling the FBC before we attempt
399 * to modify the control registers.
400 *
401 * A more complicated solution would involve tracking vblanks
402 * following the termination of the page-flipping sequence
403 * and indeed performing the enable as a co-routine and not
404 * waiting synchronously upon the vblank.
Damien Lespiau7457d612013-06-07 17:41:07 +0100405 *
406 * WaFbcWaitForVBlankBeforeEnable:ilk,snb
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300407 */
408 schedule_delayed_work(&work->work, msecs_to_jiffies(50));
409}
410
411void intel_disable_fbc(struct drm_device *dev)
412{
413 struct drm_i915_private *dev_priv = dev->dev_private;
414
415 intel_cancel_fbc_work(dev_priv);
416
417 if (!dev_priv->display.disable_fbc)
418 return;
419
420 dev_priv->display.disable_fbc(dev);
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -0700421 dev_priv->fbc.plane = -1;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300422}
423
Chris Wilson29ebf902013-07-27 17:23:55 +0100424static bool set_no_fbc_reason(struct drm_i915_private *dev_priv,
425 enum no_fbc_reason reason)
426{
427 if (dev_priv->fbc.no_fbc_reason == reason)
428 return false;
429
430 dev_priv->fbc.no_fbc_reason = reason;
431 return true;
432}
433
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300434/**
435 * intel_update_fbc - enable/disable FBC as needed
436 * @dev: the drm_device
437 *
438 * Set up the framebuffer compression hardware at mode set time. We
439 * enable it if possible:
440 * - plane A only (on pre-965)
441 * - no pixel mulitply/line duplication
442 * - no alpha buffer discard
443 * - no dual wide
Paulo Zanonif85da862013-06-04 16:53:39 -0300444 * - framebuffer <= max_hdisplay in width, max_vdisplay in height
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300445 *
446 * We can't assume that any compression will take place (worst case),
447 * so the compressed buffer has to be the same size as the uncompressed
448 * one. It also must reside (along with the line length buffer) in
449 * stolen memory.
450 *
451 * We need to enable/disable FBC on a global basis.
452 */
453void intel_update_fbc(struct drm_device *dev)
454{
455 struct drm_i915_private *dev_priv = dev->dev_private;
456 struct drm_crtc *crtc = NULL, *tmp_crtc;
457 struct intel_crtc *intel_crtc;
458 struct drm_framebuffer *fb;
459 struct intel_framebuffer *intel_fb;
460 struct drm_i915_gem_object *obj;
Ville Syrjäläef644fd2013-09-04 18:25:21 +0300461 const struct drm_display_mode *adjusted_mode;
Ville Syrjälä37327ab2013-09-04 18:25:28 +0300462 unsigned int max_width, max_height;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300463
Chris Wilson29ebf902013-07-27 17:23:55 +0100464 if (!I915_HAS_FBC(dev)) {
465 set_no_fbc_reason(dev_priv, FBC_UNSUPPORTED);
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300466 return;
Chris Wilson29ebf902013-07-27 17:23:55 +0100467 }
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300468
Chris Wilson29ebf902013-07-27 17:23:55 +0100469 if (!i915_powersave) {
470 if (set_no_fbc_reason(dev_priv, FBC_MODULE_PARAM))
471 DRM_DEBUG_KMS("fbc disabled per module param\n");
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300472 return;
Chris Wilson29ebf902013-07-27 17:23:55 +0100473 }
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300474
475 /*
476 * If FBC is already on, we just have to verify that we can
477 * keep it that way...
478 * Need to disable if:
479 * - more than one pipe is active
480 * - changing FBC params (stride, fence, mode)
481 * - new fb is too large to fit in compressed buffer
482 * - going to an unsupported config (interlace, pixel multiply, etc.)
483 */
484 list_for_each_entry(tmp_crtc, &dev->mode_config.crtc_list, head) {
Chris Wilson3490ea52013-01-07 10:11:40 +0000485 if (intel_crtc_active(tmp_crtc) &&
Ville Syrjälä4c445e02013-10-09 17:24:58 +0300486 to_intel_crtc(tmp_crtc)->primary_enabled) {
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300487 if (crtc) {
Chris Wilson29ebf902013-07-27 17:23:55 +0100488 if (set_no_fbc_reason(dev_priv, FBC_MULTIPLE_PIPES))
489 DRM_DEBUG_KMS("more than one pipe active, disabling compression\n");
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300490 goto out_disable;
491 }
492 crtc = tmp_crtc;
493 }
494 }
495
496 if (!crtc || crtc->fb == NULL) {
Chris Wilson29ebf902013-07-27 17:23:55 +0100497 if (set_no_fbc_reason(dev_priv, FBC_NO_OUTPUT))
498 DRM_DEBUG_KMS("no output, disabling\n");
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300499 goto out_disable;
500 }
501
502 intel_crtc = to_intel_crtc(crtc);
503 fb = crtc->fb;
504 intel_fb = to_intel_framebuffer(fb);
505 obj = intel_fb->obj;
Ville Syrjäläef644fd2013-09-04 18:25:21 +0300506 adjusted_mode = &intel_crtc->config.adjusted_mode;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300507
Damien Lespiau8a5729a2013-06-24 16:22:02 +0100508 if (i915_enable_fbc < 0 &&
509 INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev)) {
Chris Wilson29ebf902013-07-27 17:23:55 +0100510 if (set_no_fbc_reason(dev_priv, FBC_CHIP_DEFAULT))
511 DRM_DEBUG_KMS("disabled per chip default\n");
Damien Lespiau8a5729a2013-06-24 16:22:02 +0100512 goto out_disable;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300513 }
Damien Lespiau8a5729a2013-06-24 16:22:02 +0100514 if (!i915_enable_fbc) {
Chris Wilson29ebf902013-07-27 17:23:55 +0100515 if (set_no_fbc_reason(dev_priv, FBC_MODULE_PARAM))
516 DRM_DEBUG_KMS("fbc disabled per module param\n");
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300517 goto out_disable;
518 }
Ville Syrjäläef644fd2013-09-04 18:25:21 +0300519 if ((adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) ||
520 (adjusted_mode->flags & DRM_MODE_FLAG_DBLSCAN)) {
Chris Wilson29ebf902013-07-27 17:23:55 +0100521 if (set_no_fbc_reason(dev_priv, FBC_UNSUPPORTED_MODE))
522 DRM_DEBUG_KMS("mode incompatible with compression, "
523 "disabling\n");
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300524 goto out_disable;
525 }
Paulo Zanonif85da862013-06-04 16:53:39 -0300526
527 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
Ville Syrjälä37327ab2013-09-04 18:25:28 +0300528 max_width = 4096;
529 max_height = 2048;
Paulo Zanonif85da862013-06-04 16:53:39 -0300530 } else {
Ville Syrjälä37327ab2013-09-04 18:25:28 +0300531 max_width = 2048;
532 max_height = 1536;
Paulo Zanonif85da862013-06-04 16:53:39 -0300533 }
Ville Syrjälä37327ab2013-09-04 18:25:28 +0300534 if (intel_crtc->config.pipe_src_w > max_width ||
535 intel_crtc->config.pipe_src_h > max_height) {
Chris Wilson29ebf902013-07-27 17:23:55 +0100536 if (set_no_fbc_reason(dev_priv, FBC_MODE_TOO_LARGE))
537 DRM_DEBUG_KMS("mode too large for compression, disabling\n");
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300538 goto out_disable;
539 }
Ville Syrjäläc5a44aa2013-11-28 17:29:58 +0200540 if ((INTEL_INFO(dev)->gen < 4 || IS_HASWELL(dev)) &&
541 intel_crtc->plane != PLANE_A) {
Chris Wilson29ebf902013-07-27 17:23:55 +0100542 if (set_no_fbc_reason(dev_priv, FBC_BAD_PLANE))
Ville Syrjäläc5a44aa2013-11-28 17:29:58 +0200543 DRM_DEBUG_KMS("plane not A, disabling compression\n");
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300544 goto out_disable;
545 }
546
547 /* The use of a CPU fence is mandatory in order to detect writes
548 * by the CPU to the scanout and trigger updates to the FBC.
549 */
550 if (obj->tiling_mode != I915_TILING_X ||
551 obj->fence_reg == I915_FENCE_REG_NONE) {
Chris Wilson29ebf902013-07-27 17:23:55 +0100552 if (set_no_fbc_reason(dev_priv, FBC_NOT_TILED))
553 DRM_DEBUG_KMS("framebuffer not tiled or fenced, disabling compression\n");
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300554 goto out_disable;
555 }
556
557 /* If the kernel debugger is active, always disable compression */
558 if (in_dbg_master())
559 goto out_disable;
560
Chris Wilson11be49e2012-11-15 11:32:20 +0000561 if (i915_gem_stolen_setup_compression(dev, intel_fb->obj->base.size)) {
Chris Wilson29ebf902013-07-27 17:23:55 +0100562 if (set_no_fbc_reason(dev_priv, FBC_STOLEN_TOO_SMALL))
563 DRM_DEBUG_KMS("framebuffer too large, disabling compression\n");
Chris Wilson11be49e2012-11-15 11:32:20 +0000564 goto out_disable;
565 }
566
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300567 /* If the scanout has not changed, don't modify the FBC settings.
568 * Note that we make the fundamental assumption that the fb->obj
569 * cannot be unpinned (and have its GTT offset and fence revoked)
570 * without first being decoupled from the scanout and FBC disabled.
571 */
Ben Widawsky5c3fe8b2013-06-27 16:30:21 -0700572 if (dev_priv->fbc.plane == intel_crtc->plane &&
573 dev_priv->fbc.fb_id == fb->base.id &&
574 dev_priv->fbc.y == crtc->y)
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300575 return;
576
577 if (intel_fbc_enabled(dev)) {
578 /* We update FBC along two paths, after changing fb/crtc
579 * configuration (modeswitching) and after page-flipping
580 * finishes. For the latter, we know that not only did
581 * we disable the FBC at the start of the page-flip
582 * sequence, but also more than one vblank has passed.
583 *
584 * For the former case of modeswitching, it is possible
585 * to switch between two FBC valid configurations
586 * instantaneously so we do need to disable the FBC
587 * before we can modify its control registers. We also
588 * have to wait for the next vblank for that to take
589 * effect. However, since we delay enabling FBC we can
590 * assume that a vblank has passed since disabling and
591 * that we can safely alter the registers in the deferred
592 * callback.
593 *
594 * In the scenario that we go from a valid to invalid
595 * and then back to valid FBC configuration we have
596 * no strict enforcement that a vblank occurred since
597 * disabling the FBC. However, along all current pipe
598 * disabling paths we do need to wait for a vblank at
599 * some point. And we wait before enabling FBC anyway.
600 */
601 DRM_DEBUG_KMS("disabling active FBC for update\n");
602 intel_disable_fbc(dev);
603 }
604
Ville Syrjälä993495a2013-12-12 17:27:40 +0200605 intel_enable_fbc(crtc);
Chris Wilson29ebf902013-07-27 17:23:55 +0100606 dev_priv->fbc.no_fbc_reason = FBC_OK;
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300607 return;
608
609out_disable:
610 /* Multiple disables should be harmless */
611 if (intel_fbc_enabled(dev)) {
612 DRM_DEBUG_KMS("unsupported config, disabling FBC\n");
613 intel_disable_fbc(dev);
614 }
Chris Wilson11be49e2012-11-15 11:32:20 +0000615 i915_gem_stolen_cleanup_compression(dev);
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300616}
617
Daniel Vetterc921aba2012-04-26 23:28:17 +0200618static void i915_pineview_get_mem_freq(struct drm_device *dev)
619{
620 drm_i915_private_t *dev_priv = dev->dev_private;
621 u32 tmp;
622
623 tmp = I915_READ(CLKCFG);
624
625 switch (tmp & CLKCFG_FSB_MASK) {
626 case CLKCFG_FSB_533:
627 dev_priv->fsb_freq = 533; /* 133*4 */
628 break;
629 case CLKCFG_FSB_800:
630 dev_priv->fsb_freq = 800; /* 200*4 */
631 break;
632 case CLKCFG_FSB_667:
633 dev_priv->fsb_freq = 667; /* 167*4 */
634 break;
635 case CLKCFG_FSB_400:
636 dev_priv->fsb_freq = 400; /* 100*4 */
637 break;
638 }
639
640 switch (tmp & CLKCFG_MEM_MASK) {
641 case CLKCFG_MEM_533:
642 dev_priv->mem_freq = 533;
643 break;
644 case CLKCFG_MEM_667:
645 dev_priv->mem_freq = 667;
646 break;
647 case CLKCFG_MEM_800:
648 dev_priv->mem_freq = 800;
649 break;
650 }
651
652 /* detect pineview DDR3 setting */
653 tmp = I915_READ(CSHRDDR3CTL);
654 dev_priv->is_ddr3 = (tmp & CSHRDDR3CTL_DDR3) ? 1 : 0;
655}
656
657static void i915_ironlake_get_mem_freq(struct drm_device *dev)
658{
659 drm_i915_private_t *dev_priv = dev->dev_private;
660 u16 ddrpll, csipll;
661
662 ddrpll = I915_READ16(DDRMPLL1);
663 csipll = I915_READ16(CSIPLL0);
664
665 switch (ddrpll & 0xff) {
666 case 0xc:
667 dev_priv->mem_freq = 800;
668 break;
669 case 0x10:
670 dev_priv->mem_freq = 1066;
671 break;
672 case 0x14:
673 dev_priv->mem_freq = 1333;
674 break;
675 case 0x18:
676 dev_priv->mem_freq = 1600;
677 break;
678 default:
679 DRM_DEBUG_DRIVER("unknown memory frequency 0x%02x\n",
680 ddrpll & 0xff);
681 dev_priv->mem_freq = 0;
682 break;
683 }
684
Daniel Vetter20e4d402012-08-08 23:35:39 +0200685 dev_priv->ips.r_t = dev_priv->mem_freq;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200686
687 switch (csipll & 0x3ff) {
688 case 0x00c:
689 dev_priv->fsb_freq = 3200;
690 break;
691 case 0x00e:
692 dev_priv->fsb_freq = 3733;
693 break;
694 case 0x010:
695 dev_priv->fsb_freq = 4266;
696 break;
697 case 0x012:
698 dev_priv->fsb_freq = 4800;
699 break;
700 case 0x014:
701 dev_priv->fsb_freq = 5333;
702 break;
703 case 0x016:
704 dev_priv->fsb_freq = 5866;
705 break;
706 case 0x018:
707 dev_priv->fsb_freq = 6400;
708 break;
709 default:
710 DRM_DEBUG_DRIVER("unknown fsb frequency 0x%04x\n",
711 csipll & 0x3ff);
712 dev_priv->fsb_freq = 0;
713 break;
714 }
715
716 if (dev_priv->fsb_freq == 3200) {
Daniel Vetter20e4d402012-08-08 23:35:39 +0200717 dev_priv->ips.c_m = 0;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200718 } else if (dev_priv->fsb_freq > 3200 && dev_priv->fsb_freq <= 4800) {
Daniel Vetter20e4d402012-08-08 23:35:39 +0200719 dev_priv->ips.c_m = 1;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200720 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +0200721 dev_priv->ips.c_m = 2;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200722 }
723}
724
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300725static const struct cxsr_latency cxsr_latency_table[] = {
726 {1, 0, 800, 400, 3382, 33382, 3983, 33983}, /* DDR2-400 SC */
727 {1, 0, 800, 667, 3354, 33354, 3807, 33807}, /* DDR2-667 SC */
728 {1, 0, 800, 800, 3347, 33347, 3763, 33763}, /* DDR2-800 SC */
729 {1, 1, 800, 667, 6420, 36420, 6873, 36873}, /* DDR3-667 SC */
730 {1, 1, 800, 800, 5902, 35902, 6318, 36318}, /* DDR3-800 SC */
731
732 {1, 0, 667, 400, 3400, 33400, 4021, 34021}, /* DDR2-400 SC */
733 {1, 0, 667, 667, 3372, 33372, 3845, 33845}, /* DDR2-667 SC */
734 {1, 0, 667, 800, 3386, 33386, 3822, 33822}, /* DDR2-800 SC */
735 {1, 1, 667, 667, 6438, 36438, 6911, 36911}, /* DDR3-667 SC */
736 {1, 1, 667, 800, 5941, 35941, 6377, 36377}, /* DDR3-800 SC */
737
738 {1, 0, 400, 400, 3472, 33472, 4173, 34173}, /* DDR2-400 SC */
739 {1, 0, 400, 667, 3443, 33443, 3996, 33996}, /* DDR2-667 SC */
740 {1, 0, 400, 800, 3430, 33430, 3946, 33946}, /* DDR2-800 SC */
741 {1, 1, 400, 667, 6509, 36509, 7062, 37062}, /* DDR3-667 SC */
742 {1, 1, 400, 800, 5985, 35985, 6501, 36501}, /* DDR3-800 SC */
743
744 {0, 0, 800, 400, 3438, 33438, 4065, 34065}, /* DDR2-400 SC */
745 {0, 0, 800, 667, 3410, 33410, 3889, 33889}, /* DDR2-667 SC */
746 {0, 0, 800, 800, 3403, 33403, 3845, 33845}, /* DDR2-800 SC */
747 {0, 1, 800, 667, 6476, 36476, 6955, 36955}, /* DDR3-667 SC */
748 {0, 1, 800, 800, 5958, 35958, 6400, 36400}, /* DDR3-800 SC */
749
750 {0, 0, 667, 400, 3456, 33456, 4103, 34106}, /* DDR2-400 SC */
751 {0, 0, 667, 667, 3428, 33428, 3927, 33927}, /* DDR2-667 SC */
752 {0, 0, 667, 800, 3443, 33443, 3905, 33905}, /* DDR2-800 SC */
753 {0, 1, 667, 667, 6494, 36494, 6993, 36993}, /* DDR3-667 SC */
754 {0, 1, 667, 800, 5998, 35998, 6460, 36460}, /* DDR3-800 SC */
755
756 {0, 0, 400, 400, 3528, 33528, 4255, 34255}, /* DDR2-400 SC */
757 {0, 0, 400, 667, 3500, 33500, 4079, 34079}, /* DDR2-667 SC */
758 {0, 0, 400, 800, 3487, 33487, 4029, 34029}, /* DDR2-800 SC */
759 {0, 1, 400, 667, 6566, 36566, 7145, 37145}, /* DDR3-667 SC */
760 {0, 1, 400, 800, 6042, 36042, 6584, 36584}, /* DDR3-800 SC */
761};
762
Daniel Vetter63c62272012-04-21 23:17:55 +0200763static const struct cxsr_latency *intel_get_cxsr_latency(int is_desktop,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300764 int is_ddr3,
765 int fsb,
766 int mem)
767{
768 const struct cxsr_latency *latency;
769 int i;
770
771 if (fsb == 0 || mem == 0)
772 return NULL;
773
774 for (i = 0; i < ARRAY_SIZE(cxsr_latency_table); i++) {
775 latency = &cxsr_latency_table[i];
776 if (is_desktop == latency->is_desktop &&
777 is_ddr3 == latency->is_ddr3 &&
778 fsb == latency->fsb_freq && mem == latency->mem_freq)
779 return latency;
780 }
781
782 DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
783
784 return NULL;
785}
786
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300787static void pineview_disable_cxsr(struct drm_device *dev)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300788{
789 struct drm_i915_private *dev_priv = dev->dev_private;
790
791 /* deactivate cxsr */
792 I915_WRITE(DSPFW3, I915_READ(DSPFW3) & ~PINEVIEW_SELF_REFRESH_EN);
793}
794
795/*
796 * Latency for FIFO fetches is dependent on several factors:
797 * - memory configuration (speed, channels)
798 * - chipset
799 * - current MCH state
800 * It can be fairly high in some situations, so here we assume a fairly
801 * pessimal value. It's a tradeoff between extra memory fetches (if we
802 * set this value too high, the FIFO will fetch frequently to stay full)
803 * and power consumption (set it too low to save power and we might see
804 * FIFO underruns and display "flicker").
805 *
806 * A value of 5us seems to be a good balance; safe for very low end
807 * platforms but not overly aggressive on lower latency configs.
808 */
809static const int latency_ns = 5000;
810
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300811static int i9xx_get_fifo_size(struct drm_device *dev, int plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300812{
813 struct drm_i915_private *dev_priv = dev->dev_private;
814 uint32_t dsparb = I915_READ(DSPARB);
815 int size;
816
817 size = dsparb & 0x7f;
818 if (plane)
819 size = ((dsparb >> DSPARB_CSTART_SHIFT) & 0x7f) - size;
820
821 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
822 plane ? "B" : "A", size);
823
824 return size;
825}
826
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300827static int i85x_get_fifo_size(struct drm_device *dev, int plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300828{
829 struct drm_i915_private *dev_priv = dev->dev_private;
830 uint32_t dsparb = I915_READ(DSPARB);
831 int size;
832
833 size = dsparb & 0x1ff;
834 if (plane)
835 size = ((dsparb >> DSPARB_BEND_SHIFT) & 0x1ff) - size;
836 size >>= 1; /* Convert to cachelines */
837
838 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
839 plane ? "B" : "A", size);
840
841 return size;
842}
843
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300844static int i845_get_fifo_size(struct drm_device *dev, int plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300845{
846 struct drm_i915_private *dev_priv = dev->dev_private;
847 uint32_t dsparb = I915_READ(DSPARB);
848 int size;
849
850 size = dsparb & 0x7f;
851 size >>= 2; /* Convert to cachelines */
852
853 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
854 plane ? "B" : "A",
855 size);
856
857 return size;
858}
859
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300860static int i830_get_fifo_size(struct drm_device *dev, int plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300861{
862 struct drm_i915_private *dev_priv = dev->dev_private;
863 uint32_t dsparb = I915_READ(DSPARB);
864 int size;
865
866 size = dsparb & 0x7f;
867 size >>= 1; /* Convert to cachelines */
868
869 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
870 plane ? "B" : "A", size);
871
872 return size;
873}
874
875/* Pineview has different values for various configs */
876static const struct intel_watermark_params pineview_display_wm = {
877 PINEVIEW_DISPLAY_FIFO,
878 PINEVIEW_MAX_WM,
879 PINEVIEW_DFT_WM,
880 PINEVIEW_GUARD_WM,
881 PINEVIEW_FIFO_LINE_SIZE
882};
883static const struct intel_watermark_params pineview_display_hplloff_wm = {
884 PINEVIEW_DISPLAY_FIFO,
885 PINEVIEW_MAX_WM,
886 PINEVIEW_DFT_HPLLOFF_WM,
887 PINEVIEW_GUARD_WM,
888 PINEVIEW_FIFO_LINE_SIZE
889};
890static const struct intel_watermark_params pineview_cursor_wm = {
891 PINEVIEW_CURSOR_FIFO,
892 PINEVIEW_CURSOR_MAX_WM,
893 PINEVIEW_CURSOR_DFT_WM,
894 PINEVIEW_CURSOR_GUARD_WM,
895 PINEVIEW_FIFO_LINE_SIZE,
896};
897static const struct intel_watermark_params pineview_cursor_hplloff_wm = {
898 PINEVIEW_CURSOR_FIFO,
899 PINEVIEW_CURSOR_MAX_WM,
900 PINEVIEW_CURSOR_DFT_WM,
901 PINEVIEW_CURSOR_GUARD_WM,
902 PINEVIEW_FIFO_LINE_SIZE
903};
904static const struct intel_watermark_params g4x_wm_info = {
905 G4X_FIFO_SIZE,
906 G4X_MAX_WM,
907 G4X_MAX_WM,
908 2,
909 G4X_FIFO_LINE_SIZE,
910};
911static const struct intel_watermark_params g4x_cursor_wm_info = {
912 I965_CURSOR_FIFO,
913 I965_CURSOR_MAX_WM,
914 I965_CURSOR_DFT_WM,
915 2,
916 G4X_FIFO_LINE_SIZE,
917};
918static const struct intel_watermark_params valleyview_wm_info = {
919 VALLEYVIEW_FIFO_SIZE,
920 VALLEYVIEW_MAX_WM,
921 VALLEYVIEW_MAX_WM,
922 2,
923 G4X_FIFO_LINE_SIZE,
924};
925static const struct intel_watermark_params valleyview_cursor_wm_info = {
926 I965_CURSOR_FIFO,
927 VALLEYVIEW_CURSOR_MAX_WM,
928 I965_CURSOR_DFT_WM,
929 2,
930 G4X_FIFO_LINE_SIZE,
931};
932static const struct intel_watermark_params i965_cursor_wm_info = {
933 I965_CURSOR_FIFO,
934 I965_CURSOR_MAX_WM,
935 I965_CURSOR_DFT_WM,
936 2,
937 I915_FIFO_LINE_SIZE,
938};
939static const struct intel_watermark_params i945_wm_info = {
940 I945_FIFO_SIZE,
941 I915_MAX_WM,
942 1,
943 2,
944 I915_FIFO_LINE_SIZE
945};
946static const struct intel_watermark_params i915_wm_info = {
947 I915_FIFO_SIZE,
948 I915_MAX_WM,
949 1,
950 2,
951 I915_FIFO_LINE_SIZE
952};
953static const struct intel_watermark_params i855_wm_info = {
954 I855GM_FIFO_SIZE,
955 I915_MAX_WM,
956 1,
957 2,
958 I830_FIFO_LINE_SIZE
959};
960static const struct intel_watermark_params i830_wm_info = {
961 I830_FIFO_SIZE,
962 I915_MAX_WM,
963 1,
964 2,
965 I830_FIFO_LINE_SIZE
966};
967
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300968/**
969 * intel_calculate_wm - calculate watermark level
970 * @clock_in_khz: pixel clock
971 * @wm: chip FIFO params
972 * @pixel_size: display pixel size
973 * @latency_ns: memory latency for the platform
974 *
975 * Calculate the watermark level (the level at which the display plane will
976 * start fetching from memory again). Each chip has a different display
977 * FIFO size and allocation, so the caller needs to figure that out and pass
978 * in the correct intel_watermark_params structure.
979 *
980 * As the pixel clock runs, the FIFO will be drained at a rate that depends
981 * on the pixel size. When it reaches the watermark level, it'll start
982 * fetching FIFO line sized based chunks from memory until the FIFO fills
983 * past the watermark point. If the FIFO drains completely, a FIFO underrun
984 * will occur, and a display engine hang could result.
985 */
986static unsigned long intel_calculate_wm(unsigned long clock_in_khz,
987 const struct intel_watermark_params *wm,
988 int fifo_size,
989 int pixel_size,
990 unsigned long latency_ns)
991{
992 long entries_required, wm_size;
993
994 /*
995 * Note: we need to make sure we don't overflow for various clock &
996 * latency values.
997 * clocks go from a few thousand to several hundred thousand.
998 * latency is usually a few thousand
999 */
1000 entries_required = ((clock_in_khz / 1000) * pixel_size * latency_ns) /
1001 1000;
1002 entries_required = DIV_ROUND_UP(entries_required, wm->cacheline_size);
1003
1004 DRM_DEBUG_KMS("FIFO entries required for mode: %ld\n", entries_required);
1005
1006 wm_size = fifo_size - (entries_required + wm->guard_size);
1007
1008 DRM_DEBUG_KMS("FIFO watermark level: %ld\n", wm_size);
1009
1010 /* Don't promote wm_size to unsigned... */
1011 if (wm_size > (long)wm->max_wm)
1012 wm_size = wm->max_wm;
1013 if (wm_size <= 0)
1014 wm_size = wm->default_wm;
1015 return wm_size;
1016}
1017
1018static struct drm_crtc *single_enabled_crtc(struct drm_device *dev)
1019{
1020 struct drm_crtc *crtc, *enabled = NULL;
1021
1022 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Chris Wilson3490ea52013-01-07 10:11:40 +00001023 if (intel_crtc_active(crtc)) {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001024 if (enabled)
1025 return NULL;
1026 enabled = crtc;
1027 }
1028 }
1029
1030 return enabled;
1031}
1032
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001033static void pineview_update_wm(struct drm_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001034{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001035 struct drm_device *dev = unused_crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001036 struct drm_i915_private *dev_priv = dev->dev_private;
1037 struct drm_crtc *crtc;
1038 const struct cxsr_latency *latency;
1039 u32 reg;
1040 unsigned long wm;
1041
1042 latency = intel_get_cxsr_latency(IS_PINEVIEW_G(dev), dev_priv->is_ddr3,
1043 dev_priv->fsb_freq, dev_priv->mem_freq);
1044 if (!latency) {
1045 DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
1046 pineview_disable_cxsr(dev);
1047 return;
1048 }
1049
1050 crtc = single_enabled_crtc(dev);
1051 if (crtc) {
Damien Lespiau241bfc32013-09-25 16:45:37 +01001052 const struct drm_display_mode *adjusted_mode;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001053 int pixel_size = crtc->fb->bits_per_pixel / 8;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001054 int clock;
1055
1056 adjusted_mode = &to_intel_crtc(crtc)->config.adjusted_mode;
1057 clock = adjusted_mode->crtc_clock;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001058
1059 /* Display SR */
1060 wm = intel_calculate_wm(clock, &pineview_display_wm,
1061 pineview_display_wm.fifo_size,
1062 pixel_size, latency->display_sr);
1063 reg = I915_READ(DSPFW1);
1064 reg &= ~DSPFW_SR_MASK;
1065 reg |= wm << DSPFW_SR_SHIFT;
1066 I915_WRITE(DSPFW1, reg);
1067 DRM_DEBUG_KMS("DSPFW1 register is %x\n", reg);
1068
1069 /* cursor SR */
1070 wm = intel_calculate_wm(clock, &pineview_cursor_wm,
1071 pineview_display_wm.fifo_size,
1072 pixel_size, latency->cursor_sr);
1073 reg = I915_READ(DSPFW3);
1074 reg &= ~DSPFW_CURSOR_SR_MASK;
1075 reg |= (wm & 0x3f) << DSPFW_CURSOR_SR_SHIFT;
1076 I915_WRITE(DSPFW3, reg);
1077
1078 /* Display HPLL off SR */
1079 wm = intel_calculate_wm(clock, &pineview_display_hplloff_wm,
1080 pineview_display_hplloff_wm.fifo_size,
1081 pixel_size, latency->display_hpll_disable);
1082 reg = I915_READ(DSPFW3);
1083 reg &= ~DSPFW_HPLL_SR_MASK;
1084 reg |= wm & DSPFW_HPLL_SR_MASK;
1085 I915_WRITE(DSPFW3, reg);
1086
1087 /* cursor HPLL off SR */
1088 wm = intel_calculate_wm(clock, &pineview_cursor_hplloff_wm,
1089 pineview_display_hplloff_wm.fifo_size,
1090 pixel_size, latency->cursor_hpll_disable);
1091 reg = I915_READ(DSPFW3);
1092 reg &= ~DSPFW_HPLL_CURSOR_MASK;
1093 reg |= (wm & 0x3f) << DSPFW_HPLL_CURSOR_SHIFT;
1094 I915_WRITE(DSPFW3, reg);
1095 DRM_DEBUG_KMS("DSPFW3 register is %x\n", reg);
1096
1097 /* activate cxsr */
1098 I915_WRITE(DSPFW3,
1099 I915_READ(DSPFW3) | PINEVIEW_SELF_REFRESH_EN);
1100 DRM_DEBUG_KMS("Self-refresh is enabled\n");
1101 } else {
1102 pineview_disable_cxsr(dev);
1103 DRM_DEBUG_KMS("Self-refresh is disabled\n");
1104 }
1105}
1106
1107static bool g4x_compute_wm0(struct drm_device *dev,
1108 int plane,
1109 const struct intel_watermark_params *display,
1110 int display_latency_ns,
1111 const struct intel_watermark_params *cursor,
1112 int cursor_latency_ns,
1113 int *plane_wm,
1114 int *cursor_wm)
1115{
1116 struct drm_crtc *crtc;
Ville Syrjälä4fe85902013-09-04 18:25:22 +03001117 const struct drm_display_mode *adjusted_mode;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001118 int htotal, hdisplay, clock, pixel_size;
1119 int line_time_us, line_count;
1120 int entries, tlb_miss;
1121
1122 crtc = intel_get_crtc_for_plane(dev, plane);
Chris Wilson3490ea52013-01-07 10:11:40 +00001123 if (!intel_crtc_active(crtc)) {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001124 *cursor_wm = cursor->guard_size;
1125 *plane_wm = display->guard_size;
1126 return false;
1127 }
1128
Ville Syrjälä4fe85902013-09-04 18:25:22 +03001129 adjusted_mode = &to_intel_crtc(crtc)->config.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001130 clock = adjusted_mode->crtc_clock;
Ville Syrjälä4fe85902013-09-04 18:25:22 +03001131 htotal = adjusted_mode->htotal;
Ville Syrjälä37327ab2013-09-04 18:25:28 +03001132 hdisplay = to_intel_crtc(crtc)->config.pipe_src_w;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001133 pixel_size = crtc->fb->bits_per_pixel / 8;
1134
1135 /* Use the small buffer method to calculate plane watermark */
1136 entries = ((clock * pixel_size / 1000) * display_latency_ns) / 1000;
1137 tlb_miss = display->fifo_size*display->cacheline_size - hdisplay * 8;
1138 if (tlb_miss > 0)
1139 entries += tlb_miss;
1140 entries = DIV_ROUND_UP(entries, display->cacheline_size);
1141 *plane_wm = entries + display->guard_size;
1142 if (*plane_wm > (int)display->max_wm)
1143 *plane_wm = display->max_wm;
1144
1145 /* Use the large buffer method to calculate cursor watermark */
1146 line_time_us = ((htotal * 1000) / clock);
1147 line_count = (cursor_latency_ns / line_time_us + 1000) / 1000;
1148 entries = line_count * 64 * pixel_size;
1149 tlb_miss = cursor->fifo_size*cursor->cacheline_size - hdisplay * 8;
1150 if (tlb_miss > 0)
1151 entries += tlb_miss;
1152 entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
1153 *cursor_wm = entries + cursor->guard_size;
1154 if (*cursor_wm > (int)cursor->max_wm)
1155 *cursor_wm = (int)cursor->max_wm;
1156
1157 return true;
1158}
1159
1160/*
1161 * Check the wm result.
1162 *
1163 * If any calculated watermark values is larger than the maximum value that
1164 * can be programmed into the associated watermark register, that watermark
1165 * must be disabled.
1166 */
1167static bool g4x_check_srwm(struct drm_device *dev,
1168 int display_wm, int cursor_wm,
1169 const struct intel_watermark_params *display,
1170 const struct intel_watermark_params *cursor)
1171{
1172 DRM_DEBUG_KMS("SR watermark: display plane %d, cursor %d\n",
1173 display_wm, cursor_wm);
1174
1175 if (display_wm > display->max_wm) {
1176 DRM_DEBUG_KMS("display watermark is too large(%d/%ld), disabling\n",
1177 display_wm, display->max_wm);
1178 return false;
1179 }
1180
1181 if (cursor_wm > cursor->max_wm) {
1182 DRM_DEBUG_KMS("cursor watermark is too large(%d/%ld), disabling\n",
1183 cursor_wm, cursor->max_wm);
1184 return false;
1185 }
1186
1187 if (!(display_wm || cursor_wm)) {
1188 DRM_DEBUG_KMS("SR latency is 0, disabling\n");
1189 return false;
1190 }
1191
1192 return true;
1193}
1194
1195static bool g4x_compute_srwm(struct drm_device *dev,
1196 int plane,
1197 int latency_ns,
1198 const struct intel_watermark_params *display,
1199 const struct intel_watermark_params *cursor,
1200 int *display_wm, int *cursor_wm)
1201{
1202 struct drm_crtc *crtc;
Ville Syrjälä4fe85902013-09-04 18:25:22 +03001203 const struct drm_display_mode *adjusted_mode;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001204 int hdisplay, htotal, pixel_size, clock;
1205 unsigned long line_time_us;
1206 int line_count, line_size;
1207 int small, large;
1208 int entries;
1209
1210 if (!latency_ns) {
1211 *display_wm = *cursor_wm = 0;
1212 return false;
1213 }
1214
1215 crtc = intel_get_crtc_for_plane(dev, plane);
Ville Syrjälä4fe85902013-09-04 18:25:22 +03001216 adjusted_mode = &to_intel_crtc(crtc)->config.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001217 clock = adjusted_mode->crtc_clock;
Ville Syrjälä4fe85902013-09-04 18:25:22 +03001218 htotal = adjusted_mode->htotal;
Ville Syrjälä37327ab2013-09-04 18:25:28 +03001219 hdisplay = to_intel_crtc(crtc)->config.pipe_src_w;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001220 pixel_size = crtc->fb->bits_per_pixel / 8;
1221
1222 line_time_us = (htotal * 1000) / clock;
1223 line_count = (latency_ns / line_time_us + 1000) / 1000;
1224 line_size = hdisplay * pixel_size;
1225
1226 /* Use the minimum of the small and large buffer method for primary */
1227 small = ((clock * pixel_size / 1000) * latency_ns) / 1000;
1228 large = line_count * line_size;
1229
1230 entries = DIV_ROUND_UP(min(small, large), display->cacheline_size);
1231 *display_wm = entries + display->guard_size;
1232
1233 /* calculate the self-refresh watermark for display cursor */
1234 entries = line_count * pixel_size * 64;
1235 entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
1236 *cursor_wm = entries + cursor->guard_size;
1237
1238 return g4x_check_srwm(dev,
1239 *display_wm, *cursor_wm,
1240 display, cursor);
1241}
1242
1243static bool vlv_compute_drain_latency(struct drm_device *dev,
1244 int plane,
1245 int *plane_prec_mult,
1246 int *plane_dl,
1247 int *cursor_prec_mult,
1248 int *cursor_dl)
1249{
1250 struct drm_crtc *crtc;
1251 int clock, pixel_size;
1252 int entries;
1253
1254 crtc = intel_get_crtc_for_plane(dev, plane);
Chris Wilson3490ea52013-01-07 10:11:40 +00001255 if (!intel_crtc_active(crtc))
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001256 return false;
1257
Damien Lespiau241bfc32013-09-25 16:45:37 +01001258 clock = to_intel_crtc(crtc)->config.adjusted_mode.crtc_clock;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001259 pixel_size = crtc->fb->bits_per_pixel / 8; /* BPP */
1260
1261 entries = (clock / 1000) * pixel_size;
1262 *plane_prec_mult = (entries > 256) ?
1263 DRAIN_LATENCY_PRECISION_32 : DRAIN_LATENCY_PRECISION_16;
1264 *plane_dl = (64 * (*plane_prec_mult) * 4) / ((clock / 1000) *
1265 pixel_size);
1266
1267 entries = (clock / 1000) * 4; /* BPP is always 4 for cursor */
1268 *cursor_prec_mult = (entries > 256) ?
1269 DRAIN_LATENCY_PRECISION_32 : DRAIN_LATENCY_PRECISION_16;
1270 *cursor_dl = (64 * (*cursor_prec_mult) * 4) / ((clock / 1000) * 4);
1271
1272 return true;
1273}
1274
1275/*
1276 * Update drain latency registers of memory arbiter
1277 *
1278 * Valleyview SoC has a new memory arbiter and needs drain latency registers
1279 * to be programmed. Each plane has a drain latency multiplier and a drain
1280 * latency value.
1281 */
1282
1283static void vlv_update_drain_latency(struct drm_device *dev)
1284{
1285 struct drm_i915_private *dev_priv = dev->dev_private;
1286 int planea_prec, planea_dl, planeb_prec, planeb_dl;
1287 int cursora_prec, cursora_dl, cursorb_prec, cursorb_dl;
1288 int plane_prec_mult, cursor_prec_mult; /* Precision multiplier is
1289 either 16 or 32 */
1290
1291 /* For plane A, Cursor A */
1292 if (vlv_compute_drain_latency(dev, 0, &plane_prec_mult, &planea_dl,
1293 &cursor_prec_mult, &cursora_dl)) {
1294 cursora_prec = (cursor_prec_mult == DRAIN_LATENCY_PRECISION_32) ?
1295 DDL_CURSORA_PRECISION_32 : DDL_CURSORA_PRECISION_16;
1296 planea_prec = (plane_prec_mult == DRAIN_LATENCY_PRECISION_32) ?
1297 DDL_PLANEA_PRECISION_32 : DDL_PLANEA_PRECISION_16;
1298
1299 I915_WRITE(VLV_DDL1, cursora_prec |
1300 (cursora_dl << DDL_CURSORA_SHIFT) |
1301 planea_prec | planea_dl);
1302 }
1303
1304 /* For plane B, Cursor B */
1305 if (vlv_compute_drain_latency(dev, 1, &plane_prec_mult, &planeb_dl,
1306 &cursor_prec_mult, &cursorb_dl)) {
1307 cursorb_prec = (cursor_prec_mult == DRAIN_LATENCY_PRECISION_32) ?
1308 DDL_CURSORB_PRECISION_32 : DDL_CURSORB_PRECISION_16;
1309 planeb_prec = (plane_prec_mult == DRAIN_LATENCY_PRECISION_32) ?
1310 DDL_PLANEB_PRECISION_32 : DDL_PLANEB_PRECISION_16;
1311
1312 I915_WRITE(VLV_DDL2, cursorb_prec |
1313 (cursorb_dl << DDL_CURSORB_SHIFT) |
1314 planeb_prec | planeb_dl);
1315 }
1316}
1317
1318#define single_plane_enabled(mask) is_power_of_2(mask)
1319
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001320static void valleyview_update_wm(struct drm_crtc *crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001321{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001322 struct drm_device *dev = crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001323 static const int sr_latency_ns = 12000;
1324 struct drm_i915_private *dev_priv = dev->dev_private;
1325 int planea_wm, planeb_wm, cursora_wm, cursorb_wm;
1326 int plane_sr, cursor_sr;
Chris Wilsonaf6c4572012-12-11 12:01:43 +00001327 int ignore_plane_sr, ignore_cursor_sr;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001328 unsigned int enabled = 0;
1329
1330 vlv_update_drain_latency(dev);
1331
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001332 if (g4x_compute_wm0(dev, PIPE_A,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001333 &valleyview_wm_info, latency_ns,
1334 &valleyview_cursor_wm_info, latency_ns,
1335 &planea_wm, &cursora_wm))
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001336 enabled |= 1 << PIPE_A;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001337
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001338 if (g4x_compute_wm0(dev, PIPE_B,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001339 &valleyview_wm_info, latency_ns,
1340 &valleyview_cursor_wm_info, latency_ns,
1341 &planeb_wm, &cursorb_wm))
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001342 enabled |= 1 << PIPE_B;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001343
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001344 if (single_plane_enabled(enabled) &&
1345 g4x_compute_srwm(dev, ffs(enabled) - 1,
1346 sr_latency_ns,
1347 &valleyview_wm_info,
1348 &valleyview_cursor_wm_info,
Chris Wilsonaf6c4572012-12-11 12:01:43 +00001349 &plane_sr, &ignore_cursor_sr) &&
1350 g4x_compute_srwm(dev, ffs(enabled) - 1,
1351 2*sr_latency_ns,
1352 &valleyview_wm_info,
1353 &valleyview_cursor_wm_info,
Chris Wilson52bd02d2012-12-07 10:43:24 +00001354 &ignore_plane_sr, &cursor_sr)) {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001355 I915_WRITE(FW_BLC_SELF_VLV, FW_CSPWRDWNEN);
Chris Wilson52bd02d2012-12-07 10:43:24 +00001356 } else {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001357 I915_WRITE(FW_BLC_SELF_VLV,
1358 I915_READ(FW_BLC_SELF_VLV) & ~FW_CSPWRDWNEN);
Chris Wilson52bd02d2012-12-07 10:43:24 +00001359 plane_sr = cursor_sr = 0;
1360 }
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001361
1362 DRM_DEBUG_KMS("Setting FIFO watermarks - A: plane=%d, cursor=%d, B: plane=%d, cursor=%d, SR: plane=%d, cursor=%d\n",
1363 planea_wm, cursora_wm,
1364 planeb_wm, cursorb_wm,
1365 plane_sr, cursor_sr);
1366
1367 I915_WRITE(DSPFW1,
1368 (plane_sr << DSPFW_SR_SHIFT) |
1369 (cursorb_wm << DSPFW_CURSORB_SHIFT) |
1370 (planeb_wm << DSPFW_PLANEB_SHIFT) |
1371 planea_wm);
1372 I915_WRITE(DSPFW2,
Chris Wilson8c919b22012-12-04 16:33:19 +00001373 (I915_READ(DSPFW2) & ~DSPFW_CURSORA_MASK) |
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001374 (cursora_wm << DSPFW_CURSORA_SHIFT));
1375 I915_WRITE(DSPFW3,
Chris Wilson8c919b22012-12-04 16:33:19 +00001376 (I915_READ(DSPFW3) & ~DSPFW_CURSOR_SR_MASK) |
1377 (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001378}
1379
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001380static void g4x_update_wm(struct drm_crtc *crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001381{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001382 struct drm_device *dev = crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001383 static const int sr_latency_ns = 12000;
1384 struct drm_i915_private *dev_priv = dev->dev_private;
1385 int planea_wm, planeb_wm, cursora_wm, cursorb_wm;
1386 int plane_sr, cursor_sr;
1387 unsigned int enabled = 0;
1388
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001389 if (g4x_compute_wm0(dev, PIPE_A,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001390 &g4x_wm_info, latency_ns,
1391 &g4x_cursor_wm_info, latency_ns,
1392 &planea_wm, &cursora_wm))
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001393 enabled |= 1 << PIPE_A;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001394
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001395 if (g4x_compute_wm0(dev, PIPE_B,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001396 &g4x_wm_info, latency_ns,
1397 &g4x_cursor_wm_info, latency_ns,
1398 &planeb_wm, &cursorb_wm))
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001399 enabled |= 1 << PIPE_B;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001400
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001401 if (single_plane_enabled(enabled) &&
1402 g4x_compute_srwm(dev, ffs(enabled) - 1,
1403 sr_latency_ns,
1404 &g4x_wm_info,
1405 &g4x_cursor_wm_info,
Chris Wilson52bd02d2012-12-07 10:43:24 +00001406 &plane_sr, &cursor_sr)) {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001407 I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_EN);
Chris Wilson52bd02d2012-12-07 10:43:24 +00001408 } else {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001409 I915_WRITE(FW_BLC_SELF,
1410 I915_READ(FW_BLC_SELF) & ~FW_BLC_SELF_EN);
Chris Wilson52bd02d2012-12-07 10:43:24 +00001411 plane_sr = cursor_sr = 0;
1412 }
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001413
1414 DRM_DEBUG_KMS("Setting FIFO watermarks - A: plane=%d, cursor=%d, B: plane=%d, cursor=%d, SR: plane=%d, cursor=%d\n",
1415 planea_wm, cursora_wm,
1416 planeb_wm, cursorb_wm,
1417 plane_sr, cursor_sr);
1418
1419 I915_WRITE(DSPFW1,
1420 (plane_sr << DSPFW_SR_SHIFT) |
1421 (cursorb_wm << DSPFW_CURSORB_SHIFT) |
1422 (planeb_wm << DSPFW_PLANEB_SHIFT) |
1423 planea_wm);
1424 I915_WRITE(DSPFW2,
Chris Wilson8c919b22012-12-04 16:33:19 +00001425 (I915_READ(DSPFW2) & ~DSPFW_CURSORA_MASK) |
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001426 (cursora_wm << DSPFW_CURSORA_SHIFT));
1427 /* HPLL off in SR has some issues on G4x... disable it */
1428 I915_WRITE(DSPFW3,
Chris Wilson8c919b22012-12-04 16:33:19 +00001429 (I915_READ(DSPFW3) & ~(DSPFW_HPLL_SR_EN | DSPFW_CURSOR_SR_MASK)) |
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001430 (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
1431}
1432
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001433static void i965_update_wm(struct drm_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001434{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001435 struct drm_device *dev = unused_crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001436 struct drm_i915_private *dev_priv = dev->dev_private;
1437 struct drm_crtc *crtc;
1438 int srwm = 1;
1439 int cursor_sr = 16;
1440
1441 /* Calc sr entries for one plane configs */
1442 crtc = single_enabled_crtc(dev);
1443 if (crtc) {
1444 /* self-refresh has much higher latency */
1445 static const int sr_latency_ns = 12000;
Ville Syrjälä4fe85902013-09-04 18:25:22 +03001446 const struct drm_display_mode *adjusted_mode =
1447 &to_intel_crtc(crtc)->config.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001448 int clock = adjusted_mode->crtc_clock;
Ville Syrjälä4fe85902013-09-04 18:25:22 +03001449 int htotal = adjusted_mode->htotal;
Ville Syrjälä37327ab2013-09-04 18:25:28 +03001450 int hdisplay = to_intel_crtc(crtc)->config.pipe_src_w;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001451 int pixel_size = crtc->fb->bits_per_pixel / 8;
1452 unsigned long line_time_us;
1453 int entries;
1454
1455 line_time_us = ((htotal * 1000) / clock);
1456
1457 /* Use ns/us then divide to preserve precision */
1458 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
1459 pixel_size * hdisplay;
1460 entries = DIV_ROUND_UP(entries, I915_FIFO_LINE_SIZE);
1461 srwm = I965_FIFO_SIZE - entries;
1462 if (srwm < 0)
1463 srwm = 1;
1464 srwm &= 0x1ff;
1465 DRM_DEBUG_KMS("self-refresh entries: %d, wm: %d\n",
1466 entries, srwm);
1467
1468 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
1469 pixel_size * 64;
1470 entries = DIV_ROUND_UP(entries,
1471 i965_cursor_wm_info.cacheline_size);
1472 cursor_sr = i965_cursor_wm_info.fifo_size -
1473 (entries + i965_cursor_wm_info.guard_size);
1474
1475 if (cursor_sr > i965_cursor_wm_info.max_wm)
1476 cursor_sr = i965_cursor_wm_info.max_wm;
1477
1478 DRM_DEBUG_KMS("self-refresh watermark: display plane %d "
1479 "cursor %d\n", srwm, cursor_sr);
1480
1481 if (IS_CRESTLINE(dev))
1482 I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_EN);
1483 } else {
1484 /* Turn off self refresh if both pipes are enabled */
1485 if (IS_CRESTLINE(dev))
1486 I915_WRITE(FW_BLC_SELF, I915_READ(FW_BLC_SELF)
1487 & ~FW_BLC_SELF_EN);
1488 }
1489
1490 DRM_DEBUG_KMS("Setting FIFO watermarks - A: 8, B: 8, C: 8, SR %d\n",
1491 srwm);
1492
1493 /* 965 has limitations... */
1494 I915_WRITE(DSPFW1, (srwm << DSPFW_SR_SHIFT) |
1495 (8 << 16) | (8 << 8) | (8 << 0));
1496 I915_WRITE(DSPFW2, (8 << 8) | (8 << 0));
1497 /* update cursor SR watermark */
1498 I915_WRITE(DSPFW3, (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
1499}
1500
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001501static void i9xx_update_wm(struct drm_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001502{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001503 struct drm_device *dev = unused_crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001504 struct drm_i915_private *dev_priv = dev->dev_private;
1505 const struct intel_watermark_params *wm_info;
1506 uint32_t fwater_lo;
1507 uint32_t fwater_hi;
1508 int cwm, srwm = 1;
1509 int fifo_size;
1510 int planea_wm, planeb_wm;
1511 struct drm_crtc *crtc, *enabled = NULL;
1512
1513 if (IS_I945GM(dev))
1514 wm_info = &i945_wm_info;
1515 else if (!IS_GEN2(dev))
1516 wm_info = &i915_wm_info;
1517 else
1518 wm_info = &i855_wm_info;
1519
1520 fifo_size = dev_priv->display.get_fifo_size(dev, 0);
1521 crtc = intel_get_crtc_for_plane(dev, 0);
Chris Wilson3490ea52013-01-07 10:11:40 +00001522 if (intel_crtc_active(crtc)) {
Damien Lespiau241bfc32013-09-25 16:45:37 +01001523 const struct drm_display_mode *adjusted_mode;
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001524 int cpp = crtc->fb->bits_per_pixel / 8;
1525 if (IS_GEN2(dev))
1526 cpp = 4;
1527
Damien Lespiau241bfc32013-09-25 16:45:37 +01001528 adjusted_mode = &to_intel_crtc(crtc)->config.adjusted_mode;
1529 planea_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001530 wm_info, fifo_size, cpp,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001531 latency_ns);
1532 enabled = crtc;
1533 } else
1534 planea_wm = fifo_size - wm_info->guard_size;
1535
1536 fifo_size = dev_priv->display.get_fifo_size(dev, 1);
1537 crtc = intel_get_crtc_for_plane(dev, 1);
Chris Wilson3490ea52013-01-07 10:11:40 +00001538 if (intel_crtc_active(crtc)) {
Damien Lespiau241bfc32013-09-25 16:45:37 +01001539 const struct drm_display_mode *adjusted_mode;
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001540 int cpp = crtc->fb->bits_per_pixel / 8;
1541 if (IS_GEN2(dev))
1542 cpp = 4;
1543
Damien Lespiau241bfc32013-09-25 16:45:37 +01001544 adjusted_mode = &to_intel_crtc(crtc)->config.adjusted_mode;
1545 planeb_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001546 wm_info, fifo_size, cpp,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001547 latency_ns);
1548 if (enabled == NULL)
1549 enabled = crtc;
1550 else
1551 enabled = NULL;
1552 } else
1553 planeb_wm = fifo_size - wm_info->guard_size;
1554
1555 DRM_DEBUG_KMS("FIFO watermarks - A: %d, B: %d\n", planea_wm, planeb_wm);
1556
1557 /*
1558 * Overlay gets an aggressive default since video jitter is bad.
1559 */
1560 cwm = 2;
1561
1562 /* Play safe and disable self-refresh before adjusting watermarks. */
1563 if (IS_I945G(dev) || IS_I945GM(dev))
1564 I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_EN_MASK | 0);
1565 else if (IS_I915GM(dev))
1566 I915_WRITE(INSTPM, I915_READ(INSTPM) & ~INSTPM_SELF_EN);
1567
1568 /* Calc sr entries for one plane configs */
1569 if (HAS_FW_BLC(dev) && enabled) {
1570 /* self-refresh has much higher latency */
1571 static const int sr_latency_ns = 6000;
Ville Syrjälä4fe85902013-09-04 18:25:22 +03001572 const struct drm_display_mode *adjusted_mode =
1573 &to_intel_crtc(enabled)->config.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001574 int clock = adjusted_mode->crtc_clock;
Ville Syrjälä4fe85902013-09-04 18:25:22 +03001575 int htotal = adjusted_mode->htotal;
Daniel Vetterf727b492013-11-20 15:02:10 +01001576 int hdisplay = to_intel_crtc(enabled)->config.pipe_src_w;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001577 int pixel_size = enabled->fb->bits_per_pixel / 8;
1578 unsigned long line_time_us;
1579 int entries;
1580
1581 line_time_us = (htotal * 1000) / clock;
1582
1583 /* Use ns/us then divide to preserve precision */
1584 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
1585 pixel_size * hdisplay;
1586 entries = DIV_ROUND_UP(entries, wm_info->cacheline_size);
1587 DRM_DEBUG_KMS("self-refresh entries: %d\n", entries);
1588 srwm = wm_info->fifo_size - entries;
1589 if (srwm < 0)
1590 srwm = 1;
1591
1592 if (IS_I945G(dev) || IS_I945GM(dev))
1593 I915_WRITE(FW_BLC_SELF,
1594 FW_BLC_SELF_FIFO_MASK | (srwm & 0xff));
1595 else if (IS_I915GM(dev))
1596 I915_WRITE(FW_BLC_SELF, srwm & 0x3f);
1597 }
1598
1599 DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d, B: %d, C: %d, SR %d\n",
1600 planea_wm, planeb_wm, cwm, srwm);
1601
1602 fwater_lo = ((planeb_wm & 0x3f) << 16) | (planea_wm & 0x3f);
1603 fwater_hi = (cwm & 0x1f);
1604
1605 /* Set request length to 8 cachelines per fetch */
1606 fwater_lo = fwater_lo | (1 << 24) | (1 << 8);
1607 fwater_hi = fwater_hi | (1 << 8);
1608
1609 I915_WRITE(FW_BLC, fwater_lo);
1610 I915_WRITE(FW_BLC2, fwater_hi);
1611
1612 if (HAS_FW_BLC(dev)) {
1613 if (enabled) {
1614 if (IS_I945G(dev) || IS_I945GM(dev))
1615 I915_WRITE(FW_BLC_SELF,
1616 FW_BLC_SELF_EN_MASK | FW_BLC_SELF_EN);
1617 else if (IS_I915GM(dev))
1618 I915_WRITE(INSTPM, I915_READ(INSTPM) | INSTPM_SELF_EN);
1619 DRM_DEBUG_KMS("memory self refresh enabled\n");
1620 } else
1621 DRM_DEBUG_KMS("memory self refresh disabled\n");
1622 }
1623}
1624
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001625static void i830_update_wm(struct drm_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001626{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001627 struct drm_device *dev = unused_crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001628 struct drm_i915_private *dev_priv = dev->dev_private;
1629 struct drm_crtc *crtc;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001630 const struct drm_display_mode *adjusted_mode;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001631 uint32_t fwater_lo;
1632 int planea_wm;
1633
1634 crtc = single_enabled_crtc(dev);
1635 if (crtc == NULL)
1636 return;
1637
Damien Lespiau241bfc32013-09-25 16:45:37 +01001638 adjusted_mode = &to_intel_crtc(crtc)->config.adjusted_mode;
1639 planea_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Ville Syrjälä4fe85902013-09-04 18:25:22 +03001640 &i830_wm_info,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001641 dev_priv->display.get_fifo_size(dev, 0),
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001642 4, latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001643 fwater_lo = I915_READ(FW_BLC) & ~0xfff;
1644 fwater_lo |= (3<<8) | planea_wm;
1645
1646 DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d\n", planea_wm);
1647
1648 I915_WRITE(FW_BLC, fwater_lo);
1649}
1650
Ville Syrjälä36587292013-07-05 11:57:16 +03001651static uint32_t ilk_pipe_pixel_rate(struct drm_device *dev,
1652 struct drm_crtc *crtc)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001653{
1654 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilsonfd4daa92013-08-27 17:04:17 +01001655 uint32_t pixel_rate;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001656
Damien Lespiau241bfc32013-09-25 16:45:37 +01001657 pixel_rate = intel_crtc->config.adjusted_mode.crtc_clock;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001658
1659 /* We only use IF-ID interlacing. If we ever use PF-ID we'll need to
1660 * adjust the pixel_rate here. */
1661
Chris Wilsonfd4daa92013-08-27 17:04:17 +01001662 if (intel_crtc->config.pch_pfit.enabled) {
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001663 uint64_t pipe_w, pipe_h, pfit_w, pfit_h;
Chris Wilsonfd4daa92013-08-27 17:04:17 +01001664 uint32_t pfit_size = intel_crtc->config.pch_pfit.size;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001665
Ville Syrjälä37327ab2013-09-04 18:25:28 +03001666 pipe_w = intel_crtc->config.pipe_src_w;
1667 pipe_h = intel_crtc->config.pipe_src_h;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001668 pfit_w = (pfit_size >> 16) & 0xFFFF;
1669 pfit_h = pfit_size & 0xFFFF;
1670 if (pipe_w < pfit_w)
1671 pipe_w = pfit_w;
1672 if (pipe_h < pfit_h)
1673 pipe_h = pfit_h;
1674
1675 pixel_rate = div_u64((uint64_t) pixel_rate * pipe_w * pipe_h,
1676 pfit_w * pfit_h);
1677 }
1678
1679 return pixel_rate;
1680}
1681
Ville Syrjälä37126462013-08-01 16:18:55 +03001682/* latency must be in 0.1us units. */
Ville Syrjälä23297042013-07-05 11:57:17 +03001683static uint32_t ilk_wm_method1(uint32_t pixel_rate, uint8_t bytes_per_pixel,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001684 uint32_t latency)
1685{
1686 uint64_t ret;
1687
Ville Syrjälä3312ba62013-08-01 16:18:53 +03001688 if (WARN(latency == 0, "Latency value missing\n"))
1689 return UINT_MAX;
1690
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001691 ret = (uint64_t) pixel_rate * bytes_per_pixel * latency;
1692 ret = DIV_ROUND_UP_ULL(ret, 64 * 10000) + 2;
1693
1694 return ret;
1695}
1696
Ville Syrjälä37126462013-08-01 16:18:55 +03001697/* latency must be in 0.1us units. */
Ville Syrjälä23297042013-07-05 11:57:17 +03001698static uint32_t ilk_wm_method2(uint32_t pixel_rate, uint32_t pipe_htotal,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001699 uint32_t horiz_pixels, uint8_t bytes_per_pixel,
1700 uint32_t latency)
1701{
1702 uint32_t ret;
1703
Ville Syrjälä3312ba62013-08-01 16:18:53 +03001704 if (WARN(latency == 0, "Latency value missing\n"))
1705 return UINT_MAX;
1706
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001707 ret = (latency * pixel_rate) / (pipe_htotal * 10000);
1708 ret = (ret + 1) * horiz_pixels * bytes_per_pixel;
1709 ret = DIV_ROUND_UP(ret, 64) + 2;
1710 return ret;
1711}
1712
Ville Syrjälä23297042013-07-05 11:57:17 +03001713static uint32_t ilk_wm_fbc(uint32_t pri_val, uint32_t horiz_pixels,
Paulo Zanonicca32e92013-05-31 11:45:06 -03001714 uint8_t bytes_per_pixel)
1715{
1716 return DIV_ROUND_UP(pri_val * 64, horiz_pixels * bytes_per_pixel) + 2;
1717}
1718
Imre Deak820c1982013-12-17 14:46:36 +02001719struct ilk_pipe_wm_parameters {
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001720 bool active;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001721 uint32_t pipe_htotal;
1722 uint32_t pixel_rate;
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001723 struct intel_plane_wm_parameters pri;
1724 struct intel_plane_wm_parameters spr;
1725 struct intel_plane_wm_parameters cur;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001726};
1727
Imre Deak820c1982013-12-17 14:46:36 +02001728struct ilk_wm_maximums {
Paulo Zanonicca32e92013-05-31 11:45:06 -03001729 uint16_t pri;
1730 uint16_t spr;
1731 uint16_t cur;
1732 uint16_t fbc;
1733};
1734
Ville Syrjälä240264f2013-08-07 13:29:12 +03001735/* used in computing the new watermarks state */
1736struct intel_wm_config {
1737 unsigned int num_pipes_active;
1738 bool sprites_enabled;
1739 bool sprites_scaled;
Ville Syrjälä240264f2013-08-07 13:29:12 +03001740};
1741
Ville Syrjälä37126462013-08-01 16:18:55 +03001742/*
1743 * For both WM_PIPE and WM_LP.
1744 * mem_value must be in 0.1us units.
1745 */
Imre Deak820c1982013-12-17 14:46:36 +02001746static uint32_t ilk_compute_pri_wm(const struct ilk_pipe_wm_parameters *params,
Paulo Zanonicca32e92013-05-31 11:45:06 -03001747 uint32_t mem_value,
1748 bool is_lp)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001749{
Paulo Zanonicca32e92013-05-31 11:45:06 -03001750 uint32_t method1, method2;
1751
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001752 if (!params->active || !params->pri.enabled)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001753 return 0;
1754
Ville Syrjälä23297042013-07-05 11:57:17 +03001755 method1 = ilk_wm_method1(params->pixel_rate,
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001756 params->pri.bytes_per_pixel,
Paulo Zanonicca32e92013-05-31 11:45:06 -03001757 mem_value);
1758
1759 if (!is_lp)
1760 return method1;
1761
Ville Syrjälä23297042013-07-05 11:57:17 +03001762 method2 = ilk_wm_method2(params->pixel_rate,
Paulo Zanonicca32e92013-05-31 11:45:06 -03001763 params->pipe_htotal,
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001764 params->pri.horiz_pixels,
1765 params->pri.bytes_per_pixel,
Paulo Zanonicca32e92013-05-31 11:45:06 -03001766 mem_value);
1767
1768 return min(method1, method2);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001769}
1770
Ville Syrjälä37126462013-08-01 16:18:55 +03001771/*
1772 * For both WM_PIPE and WM_LP.
1773 * mem_value must be in 0.1us units.
1774 */
Imre Deak820c1982013-12-17 14:46:36 +02001775static uint32_t ilk_compute_spr_wm(const struct ilk_pipe_wm_parameters *params,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001776 uint32_t mem_value)
1777{
1778 uint32_t method1, method2;
1779
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001780 if (!params->active || !params->spr.enabled)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001781 return 0;
1782
Ville Syrjälä23297042013-07-05 11:57:17 +03001783 method1 = ilk_wm_method1(params->pixel_rate,
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001784 params->spr.bytes_per_pixel,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001785 mem_value);
Ville Syrjälä23297042013-07-05 11:57:17 +03001786 method2 = ilk_wm_method2(params->pixel_rate,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001787 params->pipe_htotal,
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001788 params->spr.horiz_pixels,
1789 params->spr.bytes_per_pixel,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001790 mem_value);
1791 return min(method1, method2);
1792}
1793
Ville Syrjälä37126462013-08-01 16:18:55 +03001794/*
1795 * For both WM_PIPE and WM_LP.
1796 * mem_value must be in 0.1us units.
1797 */
Imre Deak820c1982013-12-17 14:46:36 +02001798static uint32_t ilk_compute_cur_wm(const struct ilk_pipe_wm_parameters *params,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001799 uint32_t mem_value)
1800{
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001801 if (!params->active || !params->cur.enabled)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001802 return 0;
1803
Ville Syrjälä23297042013-07-05 11:57:17 +03001804 return ilk_wm_method2(params->pixel_rate,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001805 params->pipe_htotal,
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001806 params->cur.horiz_pixels,
1807 params->cur.bytes_per_pixel,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001808 mem_value);
1809}
1810
Paulo Zanonicca32e92013-05-31 11:45:06 -03001811/* Only for WM_LP. */
Imre Deak820c1982013-12-17 14:46:36 +02001812static uint32_t ilk_compute_fbc_wm(const struct ilk_pipe_wm_parameters *params,
Ville Syrjälä1fda9882013-07-05 11:57:19 +03001813 uint32_t pri_val)
Paulo Zanonicca32e92013-05-31 11:45:06 -03001814{
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001815 if (!params->active || !params->pri.enabled)
Paulo Zanonicca32e92013-05-31 11:45:06 -03001816 return 0;
1817
Ville Syrjälä23297042013-07-05 11:57:17 +03001818 return ilk_wm_fbc(pri_val,
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001819 params->pri.horiz_pixels,
1820 params->pri.bytes_per_pixel);
Paulo Zanonicca32e92013-05-31 11:45:06 -03001821}
1822
Ville Syrjälä158ae642013-08-07 13:28:19 +03001823static unsigned int ilk_display_fifo_size(const struct drm_device *dev)
1824{
Ville Syrjälä416f4722013-11-02 21:07:46 -07001825 if (INTEL_INFO(dev)->gen >= 8)
1826 return 3072;
1827 else if (INTEL_INFO(dev)->gen >= 7)
Ville Syrjälä158ae642013-08-07 13:28:19 +03001828 return 768;
1829 else
1830 return 512;
1831}
1832
1833/* Calculate the maximum primary/sprite plane watermark */
1834static unsigned int ilk_plane_wm_max(const struct drm_device *dev,
1835 int level,
Ville Syrjälä240264f2013-08-07 13:29:12 +03001836 const struct intel_wm_config *config,
Ville Syrjälä158ae642013-08-07 13:28:19 +03001837 enum intel_ddb_partitioning ddb_partitioning,
1838 bool is_sprite)
1839{
1840 unsigned int fifo_size = ilk_display_fifo_size(dev);
1841 unsigned int max;
1842
1843 /* if sprites aren't enabled, sprites get nothing */
Ville Syrjälä240264f2013-08-07 13:29:12 +03001844 if (is_sprite && !config->sprites_enabled)
Ville Syrjälä158ae642013-08-07 13:28:19 +03001845 return 0;
1846
1847 /* HSW allows LP1+ watermarks even with multiple pipes */
Ville Syrjälä240264f2013-08-07 13:29:12 +03001848 if (level == 0 || config->num_pipes_active > 1) {
Ville Syrjälä158ae642013-08-07 13:28:19 +03001849 fifo_size /= INTEL_INFO(dev)->num_pipes;
1850
1851 /*
1852 * For some reason the non self refresh
1853 * FIFO size is only half of the self
1854 * refresh FIFO size on ILK/SNB.
1855 */
1856 if (INTEL_INFO(dev)->gen <= 6)
1857 fifo_size /= 2;
1858 }
1859
Ville Syrjälä240264f2013-08-07 13:29:12 +03001860 if (config->sprites_enabled) {
Ville Syrjälä158ae642013-08-07 13:28:19 +03001861 /* level 0 is always calculated with 1:1 split */
1862 if (level > 0 && ddb_partitioning == INTEL_DDB_PART_5_6) {
1863 if (is_sprite)
1864 fifo_size *= 5;
1865 fifo_size /= 6;
1866 } else {
1867 fifo_size /= 2;
1868 }
1869 }
1870
1871 /* clamp to max that the registers can hold */
Ville Syrjälä416f4722013-11-02 21:07:46 -07001872 if (INTEL_INFO(dev)->gen >= 8)
1873 max = level == 0 ? 255 : 2047;
1874 else if (INTEL_INFO(dev)->gen >= 7)
Ville Syrjälä158ae642013-08-07 13:28:19 +03001875 /* IVB/HSW primary/sprite plane watermarks */
1876 max = level == 0 ? 127 : 1023;
1877 else if (!is_sprite)
1878 /* ILK/SNB primary plane watermarks */
1879 max = level == 0 ? 127 : 511;
1880 else
1881 /* ILK/SNB sprite plane watermarks */
1882 max = level == 0 ? 63 : 255;
1883
1884 return min(fifo_size, max);
1885}
1886
1887/* Calculate the maximum cursor plane watermark */
1888static unsigned int ilk_cursor_wm_max(const struct drm_device *dev,
Ville Syrjälä240264f2013-08-07 13:29:12 +03001889 int level,
1890 const struct intel_wm_config *config)
Ville Syrjälä158ae642013-08-07 13:28:19 +03001891{
1892 /* HSW LP1+ watermarks w/ multiple pipes */
Ville Syrjälä240264f2013-08-07 13:29:12 +03001893 if (level > 0 && config->num_pipes_active > 1)
Ville Syrjälä158ae642013-08-07 13:28:19 +03001894 return 64;
1895
1896 /* otherwise just report max that registers can hold */
1897 if (INTEL_INFO(dev)->gen >= 7)
1898 return level == 0 ? 63 : 255;
1899 else
1900 return level == 0 ? 31 : 63;
1901}
1902
1903/* Calculate the maximum FBC watermark */
Ville Syrjälä416f4722013-11-02 21:07:46 -07001904static unsigned int ilk_fbc_wm_max(struct drm_device *dev)
Ville Syrjälä158ae642013-08-07 13:28:19 +03001905{
1906 /* max that registers can hold */
Ville Syrjälä416f4722013-11-02 21:07:46 -07001907 if (INTEL_INFO(dev)->gen >= 8)
1908 return 31;
1909 else
1910 return 15;
Ville Syrjälä158ae642013-08-07 13:28:19 +03001911}
1912
Ville Syrjälä34982fe2013-10-09 19:18:09 +03001913static void ilk_compute_wm_maximums(struct drm_device *dev,
1914 int level,
1915 const struct intel_wm_config *config,
1916 enum intel_ddb_partitioning ddb_partitioning,
Imre Deak820c1982013-12-17 14:46:36 +02001917 struct ilk_wm_maximums *max)
Ville Syrjälä158ae642013-08-07 13:28:19 +03001918{
Ville Syrjälä240264f2013-08-07 13:29:12 +03001919 max->pri = ilk_plane_wm_max(dev, level, config, ddb_partitioning, false);
1920 max->spr = ilk_plane_wm_max(dev, level, config, ddb_partitioning, true);
1921 max->cur = ilk_cursor_wm_max(dev, level, config);
Ville Syrjälä416f4722013-11-02 21:07:46 -07001922 max->fbc = ilk_fbc_wm_max(dev);
Ville Syrjälä158ae642013-08-07 13:28:19 +03001923}
1924
Ville Syrjäläd9395652013-10-09 19:18:10 +03001925static bool ilk_validate_wm_level(int level,
Imre Deak820c1982013-12-17 14:46:36 +02001926 const struct ilk_wm_maximums *max,
Ville Syrjäläd9395652013-10-09 19:18:10 +03001927 struct intel_wm_level *result)
Ville Syrjäläa9786a12013-08-07 13:24:47 +03001928{
1929 bool ret;
1930
1931 /* already determined to be invalid? */
1932 if (!result->enable)
1933 return false;
1934
1935 result->enable = result->pri_val <= max->pri &&
1936 result->spr_val <= max->spr &&
1937 result->cur_val <= max->cur;
1938
1939 ret = result->enable;
1940
1941 /*
1942 * HACK until we can pre-compute everything,
1943 * and thus fail gracefully if LP0 watermarks
1944 * are exceeded...
1945 */
1946 if (level == 0 && !result->enable) {
1947 if (result->pri_val > max->pri)
1948 DRM_DEBUG_KMS("Primary WM%d too large %u (max %u)\n",
1949 level, result->pri_val, max->pri);
1950 if (result->spr_val > max->spr)
1951 DRM_DEBUG_KMS("Sprite WM%d too large %u (max %u)\n",
1952 level, result->spr_val, max->spr);
1953 if (result->cur_val > max->cur)
1954 DRM_DEBUG_KMS("Cursor WM%d too large %u (max %u)\n",
1955 level, result->cur_val, max->cur);
1956
1957 result->pri_val = min_t(uint32_t, result->pri_val, max->pri);
1958 result->spr_val = min_t(uint32_t, result->spr_val, max->spr);
1959 result->cur_val = min_t(uint32_t, result->cur_val, max->cur);
1960 result->enable = true;
1961 }
1962
Ville Syrjäläa9786a12013-08-07 13:24:47 +03001963 return ret;
1964}
1965
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03001966static void ilk_compute_wm_level(struct drm_i915_private *dev_priv,
1967 int level,
Imre Deak820c1982013-12-17 14:46:36 +02001968 const struct ilk_pipe_wm_parameters *p,
Ville Syrjälä1fd527c2013-08-06 22:24:05 +03001969 struct intel_wm_level *result)
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03001970{
1971 uint16_t pri_latency = dev_priv->wm.pri_latency[level];
1972 uint16_t spr_latency = dev_priv->wm.spr_latency[level];
1973 uint16_t cur_latency = dev_priv->wm.cur_latency[level];
1974
1975 /* WM1+ latency values stored in 0.5us units */
1976 if (level > 0) {
1977 pri_latency *= 5;
1978 spr_latency *= 5;
1979 cur_latency *= 5;
1980 }
1981
1982 result->pri_val = ilk_compute_pri_wm(p, pri_latency, level);
1983 result->spr_val = ilk_compute_spr_wm(p, spr_latency);
1984 result->cur_val = ilk_compute_cur_wm(p, cur_latency);
1985 result->fbc_val = ilk_compute_fbc_wm(p, result->pri_val);
1986 result->enable = true;
1987}
1988
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001989static uint32_t
1990hsw_compute_linetime_wm(struct drm_device *dev, struct drm_crtc *crtc)
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03001991{
1992 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03001993 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03001994 struct drm_display_mode *mode = &intel_crtc->config.adjusted_mode;
Paulo Zanoni85a02de2013-05-03 17:23:43 -03001995 u32 linetime, ips_linetime;
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03001996
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001997 if (!intel_crtc_active(crtc))
1998 return 0;
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03001999
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002000 /* The WM are computed with base on how long it takes to fill a single
2001 * row at the given clock rate, multiplied by 8.
2002 * */
Paulo Zanoni85a02de2013-05-03 17:23:43 -03002003 linetime = DIV_ROUND_CLOSEST(mode->htotal * 1000 * 8, mode->clock);
2004 ips_linetime = DIV_ROUND_CLOSEST(mode->htotal * 1000 * 8,
2005 intel_ddi_get_cdclk_freq(dev_priv));
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002006
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002007 return PIPE_WM_LINETIME_IPS_LINETIME(ips_linetime) |
2008 PIPE_WM_LINETIME_TIME(linetime);
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002009}
2010
Ville Syrjälä12b134d2013-07-05 11:57:21 +03002011static void intel_read_wm_latency(struct drm_device *dev, uint16_t wm[5])
2012{
2013 struct drm_i915_private *dev_priv = dev->dev_private;
2014
Ville Syrjäläa42a5712014-01-07 16:14:08 +02002015 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Ville Syrjälä12b134d2013-07-05 11:57:21 +03002016 uint64_t sskpd = I915_READ64(MCH_SSKPD);
2017
2018 wm[0] = (sskpd >> 56) & 0xFF;
2019 if (wm[0] == 0)
2020 wm[0] = sskpd & 0xF;
Ville Syrjäläe5d50192013-07-05 11:57:22 +03002021 wm[1] = (sskpd >> 4) & 0xFF;
2022 wm[2] = (sskpd >> 12) & 0xFF;
2023 wm[3] = (sskpd >> 20) & 0x1FF;
2024 wm[4] = (sskpd >> 32) & 0x1FF;
Ville Syrjälä63cf9a12013-07-05 11:57:23 +03002025 } else if (INTEL_INFO(dev)->gen >= 6) {
2026 uint32_t sskpd = I915_READ(MCH_SSKPD);
2027
2028 wm[0] = (sskpd >> SSKPD_WM0_SHIFT) & SSKPD_WM_MASK;
2029 wm[1] = (sskpd >> SSKPD_WM1_SHIFT) & SSKPD_WM_MASK;
2030 wm[2] = (sskpd >> SSKPD_WM2_SHIFT) & SSKPD_WM_MASK;
2031 wm[3] = (sskpd >> SSKPD_WM3_SHIFT) & SSKPD_WM_MASK;
Ville Syrjälä3a88d0a2013-08-01 16:18:49 +03002032 } else if (INTEL_INFO(dev)->gen >= 5) {
2033 uint32_t mltr = I915_READ(MLTR_ILK);
2034
2035 /* ILK primary LP0 latency is 700 ns */
2036 wm[0] = 7;
2037 wm[1] = (mltr >> MLTR_WM1_SHIFT) & ILK_SRLT_MASK;
2038 wm[2] = (mltr >> MLTR_WM2_SHIFT) & ILK_SRLT_MASK;
Ville Syrjälä12b134d2013-07-05 11:57:21 +03002039 }
2040}
2041
Ville Syrjälä53615a52013-08-01 16:18:50 +03002042static void intel_fixup_spr_wm_latency(struct drm_device *dev, uint16_t wm[5])
2043{
2044 /* ILK sprite LP0 latency is 1300 ns */
2045 if (INTEL_INFO(dev)->gen == 5)
2046 wm[0] = 13;
2047}
2048
2049static void intel_fixup_cur_wm_latency(struct drm_device *dev, uint16_t wm[5])
2050{
2051 /* ILK cursor LP0 latency is 1300 ns */
2052 if (INTEL_INFO(dev)->gen == 5)
2053 wm[0] = 13;
2054
2055 /* WaDoubleCursorLP3Latency:ivb */
2056 if (IS_IVYBRIDGE(dev))
2057 wm[3] *= 2;
2058}
2059
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03002060static int ilk_wm_max_level(const struct drm_device *dev)
2061{
2062 /* how many WM levels are we expecting */
Ville Syrjäläa42a5712014-01-07 16:14:08 +02002063 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03002064 return 4;
2065 else if (INTEL_INFO(dev)->gen >= 6)
2066 return 3;
2067 else
2068 return 2;
2069}
2070
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002071static void intel_print_wm_latency(struct drm_device *dev,
2072 const char *name,
2073 const uint16_t wm[5])
2074{
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03002075 int level, max_level = ilk_wm_max_level(dev);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002076
2077 for (level = 0; level <= max_level; level++) {
2078 unsigned int latency = wm[level];
2079
2080 if (latency == 0) {
2081 DRM_ERROR("%s WM%d latency not provided\n",
2082 name, level);
2083 continue;
2084 }
2085
2086 /* WM1+ latency values in 0.5us units */
2087 if (level > 0)
2088 latency *= 5;
2089
2090 DRM_DEBUG_KMS("%s WM%d latency %u (%u.%u usec)\n",
2091 name, level, wm[level],
2092 latency / 10, latency % 10);
2093 }
2094}
2095
Ville Syrjälä53615a52013-08-01 16:18:50 +03002096static void intel_setup_wm_latency(struct drm_device *dev)
2097{
2098 struct drm_i915_private *dev_priv = dev->dev_private;
2099
2100 intel_read_wm_latency(dev, dev_priv->wm.pri_latency);
2101
2102 memcpy(dev_priv->wm.spr_latency, dev_priv->wm.pri_latency,
2103 sizeof(dev_priv->wm.pri_latency));
2104 memcpy(dev_priv->wm.cur_latency, dev_priv->wm.pri_latency,
2105 sizeof(dev_priv->wm.pri_latency));
2106
2107 intel_fixup_spr_wm_latency(dev, dev_priv->wm.spr_latency);
2108 intel_fixup_cur_wm_latency(dev, dev_priv->wm.cur_latency);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002109
2110 intel_print_wm_latency(dev, "Primary", dev_priv->wm.pri_latency);
2111 intel_print_wm_latency(dev, "Sprite", dev_priv->wm.spr_latency);
2112 intel_print_wm_latency(dev, "Cursor", dev_priv->wm.cur_latency);
Ville Syrjälä53615a52013-08-01 16:18:50 +03002113}
2114
Imre Deak820c1982013-12-17 14:46:36 +02002115static void ilk_compute_wm_parameters(struct drm_crtc *crtc,
2116 struct ilk_pipe_wm_parameters *p,
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03002117 struct intel_wm_config *config)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002118{
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03002119 struct drm_device *dev = crtc->dev;
2120 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2121 enum pipe pipe = intel_crtc->pipe;
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03002122 struct drm_plane *plane;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002123
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03002124 p->active = intel_crtc_active(crtc);
2125 if (p->active) {
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002126 p->pipe_htotal = intel_crtc->config.adjusted_mode.htotal;
Ville Syrjälä36587292013-07-05 11:57:16 +03002127 p->pixel_rate = ilk_pipe_pixel_rate(dev, crtc);
Ville Syrjäläc35426d2013-08-07 13:29:50 +03002128 p->pri.bytes_per_pixel = crtc->fb->bits_per_pixel / 8;
2129 p->cur.bytes_per_pixel = 4;
Ville Syrjälä37327ab2013-09-04 18:25:28 +03002130 p->pri.horiz_pixels = intel_crtc->config.pipe_src_w;
Ville Syrjäläc35426d2013-08-07 13:29:50 +03002131 p->cur.horiz_pixels = 64;
2132 /* TODO: for now, assume primary and cursor planes are always enabled. */
2133 p->pri.enabled = true;
2134 p->cur.enabled = true;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002135 }
2136
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03002137 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03002138 config->num_pipes_active += intel_crtc_active(crtc);
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03002139
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002140 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
2141 struct intel_plane *intel_plane = to_intel_plane(plane);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002142
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03002143 if (intel_plane->pipe == pipe)
2144 p->spr = intel_plane->wm;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002145
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03002146 config->sprites_enabled |= intel_plane->wm.enabled;
2147 config->sprites_scaled |= intel_plane->wm.scaled;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002148 }
2149}
2150
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002151/* Compute new watermarks for the pipe */
2152static bool intel_compute_pipe_wm(struct drm_crtc *crtc,
Imre Deak820c1982013-12-17 14:46:36 +02002153 const struct ilk_pipe_wm_parameters *params,
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002154 struct intel_pipe_wm *pipe_wm)
2155{
2156 struct drm_device *dev = crtc->dev;
2157 struct drm_i915_private *dev_priv = dev->dev_private;
2158 int level, max_level = ilk_wm_max_level(dev);
2159 /* LP0 watermark maximums depend on this pipe alone */
2160 struct intel_wm_config config = {
2161 .num_pipes_active = 1,
2162 .sprites_enabled = params->spr.enabled,
2163 .sprites_scaled = params->spr.scaled,
2164 };
Imre Deak820c1982013-12-17 14:46:36 +02002165 struct ilk_wm_maximums max;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002166
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002167 /* LP0 watermarks always use 1/2 DDB partitioning */
Ville Syrjälä34982fe2013-10-09 19:18:09 +03002168 ilk_compute_wm_maximums(dev, 0, &config, INTEL_DDB_PART_1_2, &max);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002169
Ville Syrjälä7b39a0b2013-12-05 15:51:30 +02002170 /* ILK/SNB: LP2+ watermarks only w/o sprites */
2171 if (INTEL_INFO(dev)->gen <= 6 && params->spr.enabled)
2172 max_level = 1;
2173
2174 /* ILK/SNB/IVB: LP1+ watermarks only w/o scaling */
2175 if (params->spr.scaled)
2176 max_level = 0;
2177
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002178 for (level = 0; level <= max_level; level++)
2179 ilk_compute_wm_level(dev_priv, level, params,
2180 &pipe_wm->wm[level]);
2181
Ville Syrjäläa42a5712014-01-07 16:14:08 +02002182 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Ville Syrjäläce0e0712013-12-05 15:51:36 +02002183 pipe_wm->linetime = hsw_compute_linetime_wm(dev, crtc);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002184
2185 /* At least LP0 must be valid */
Ville Syrjäläd9395652013-10-09 19:18:10 +03002186 return ilk_validate_wm_level(0, &max, &pipe_wm->wm[0]);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002187}
2188
2189/*
2190 * Merge the watermarks from all active pipes for a specific level.
2191 */
2192static void ilk_merge_wm_level(struct drm_device *dev,
2193 int level,
2194 struct intel_wm_level *ret_wm)
2195{
2196 const struct intel_crtc *intel_crtc;
2197
2198 list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list, base.head) {
2199 const struct intel_wm_level *wm =
2200 &intel_crtc->wm.active.wm[level];
2201
2202 if (!wm->enable)
2203 return;
2204
2205 ret_wm->pri_val = max(ret_wm->pri_val, wm->pri_val);
2206 ret_wm->spr_val = max(ret_wm->spr_val, wm->spr_val);
2207 ret_wm->cur_val = max(ret_wm->cur_val, wm->cur_val);
2208 ret_wm->fbc_val = max(ret_wm->fbc_val, wm->fbc_val);
2209 }
2210
2211 ret_wm->enable = true;
2212}
2213
2214/*
2215 * Merge all low power watermarks for all active pipes.
2216 */
2217static void ilk_wm_merge(struct drm_device *dev,
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02002218 const struct intel_wm_config *config,
Imre Deak820c1982013-12-17 14:46:36 +02002219 const struct ilk_wm_maximums *max,
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002220 struct intel_pipe_wm *merged)
2221{
2222 int level, max_level = ilk_wm_max_level(dev);
2223
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02002224 /* ILK/SNB/IVB: LP1+ watermarks only w/ single pipe */
2225 if ((INTEL_INFO(dev)->gen <= 6 || IS_IVYBRIDGE(dev)) &&
2226 config->num_pipes_active > 1)
2227 return;
2228
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02002229 /* ILK: FBC WM must be disabled always */
2230 merged->fbc_wm_enabled = INTEL_INFO(dev)->gen >= 6;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002231
2232 /* merge each WM1+ level */
2233 for (level = 1; level <= max_level; level++) {
2234 struct intel_wm_level *wm = &merged->wm[level];
2235
2236 ilk_merge_wm_level(dev, level, wm);
2237
Ville Syrjäläd9395652013-10-09 19:18:10 +03002238 if (!ilk_validate_wm_level(level, max, wm))
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002239 break;
2240
2241 /*
2242 * The spec says it is preferred to disable
2243 * FBC WMs instead of disabling a WM level.
2244 */
2245 if (wm->fbc_val > max->fbc) {
2246 merged->fbc_wm_enabled = false;
2247 wm->fbc_val = 0;
2248 }
2249 }
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02002250
2251 /* ILK: LP2+ must be disabled when FBC WM is disabled but FBC enabled */
2252 /*
2253 * FIXME this is racy. FBC might get enabled later.
2254 * What we should check here is whether FBC can be
2255 * enabled sometime later.
2256 */
2257 if (IS_GEN5(dev) && !merged->fbc_wm_enabled && intel_fbc_enabled(dev)) {
2258 for (level = 2; level <= max_level; level++) {
2259 struct intel_wm_level *wm = &merged->wm[level];
2260
2261 wm->enable = false;
2262 }
2263 }
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002264}
2265
Ville Syrjäläb380ca32013-10-09 19:18:01 +03002266static int ilk_wm_lp_to_level(int wm_lp, const struct intel_pipe_wm *pipe_wm)
2267{
2268 /* LP1,LP2,LP3 levels are either 1,2,3 or 1,3,4 */
2269 return wm_lp + (wm_lp >= 2 && pipe_wm->wm[4].enable);
2270}
2271
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02002272/* The value we need to program into the WM_LPx latency field */
2273static unsigned int ilk_wm_lp_latency(struct drm_device *dev, int level)
2274{
2275 struct drm_i915_private *dev_priv = dev->dev_private;
2276
Ville Syrjäläa42a5712014-01-07 16:14:08 +02002277 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02002278 return 2 * level;
2279 else
2280 return dev_priv->wm.pri_latency[level];
2281}
2282
Imre Deak820c1982013-12-17 14:46:36 +02002283static void ilk_compute_wm_results(struct drm_device *dev,
Ville Syrjälä0362c782013-10-09 19:17:57 +03002284 const struct intel_pipe_wm *merged,
Ville Syrjälä609cede2013-10-09 19:18:03 +03002285 enum intel_ddb_partitioning partitioning,
Imre Deak820c1982013-12-17 14:46:36 +02002286 struct ilk_wm_values *results)
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002287{
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002288 struct intel_crtc *intel_crtc;
2289 int level, wm_lp;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002290
Ville Syrjälä0362c782013-10-09 19:17:57 +03002291 results->enable_fbc_wm = merged->fbc_wm_enabled;
Ville Syrjälä609cede2013-10-09 19:18:03 +03002292 results->partitioning = partitioning;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002293
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002294 /* LP1+ register values */
Paulo Zanonicca32e92013-05-31 11:45:06 -03002295 for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
Ville Syrjälä1fd527c2013-08-06 22:24:05 +03002296 const struct intel_wm_level *r;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002297
Ville Syrjäläb380ca32013-10-09 19:18:01 +03002298 level = ilk_wm_lp_to_level(wm_lp, merged);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002299
Ville Syrjälä0362c782013-10-09 19:17:57 +03002300 r = &merged->wm[level];
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002301 if (!r->enable)
Paulo Zanonicca32e92013-05-31 11:45:06 -03002302 break;
2303
Ville Syrjälä416f4722013-11-02 21:07:46 -07002304 results->wm_lp[wm_lp - 1] = WM3_LP_EN |
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02002305 (ilk_wm_lp_latency(dev, level) << WM1_LP_LATENCY_SHIFT) |
Ville Syrjälä416f4722013-11-02 21:07:46 -07002306 (r->pri_val << WM1_LP_SR_SHIFT) |
2307 r->cur_val;
2308
2309 if (INTEL_INFO(dev)->gen >= 8)
2310 results->wm_lp[wm_lp - 1] |=
2311 r->fbc_val << WM1_LP_FBC_SHIFT_BDW;
2312 else
2313 results->wm_lp[wm_lp - 1] |=
2314 r->fbc_val << WM1_LP_FBC_SHIFT;
2315
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02002316 if (INTEL_INFO(dev)->gen <= 6 && r->spr_val) {
2317 WARN_ON(wm_lp != 1);
2318 results->wm_lp_spr[wm_lp - 1] = WM1S_LP_EN | r->spr_val;
2319 } else
2320 results->wm_lp_spr[wm_lp - 1] = r->spr_val;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002321 }
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002322
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002323 /* LP0 register values */
2324 list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list, base.head) {
2325 enum pipe pipe = intel_crtc->pipe;
2326 const struct intel_wm_level *r =
2327 &intel_crtc->wm.active.wm[0];
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002328
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002329 if (WARN_ON(!r->enable))
2330 continue;
2331
2332 results->wm_linetime[pipe] = intel_crtc->wm.active.linetime;
2333
2334 results->wm_pipe[pipe] =
2335 (r->pri_val << WM0_PIPE_PLANE_SHIFT) |
2336 (r->spr_val << WM0_PIPE_SPRITE_SHIFT) |
2337 r->cur_val;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002338 }
2339}
2340
Paulo Zanoni861f3382013-05-31 10:19:21 -03002341/* Find the result with the highest level enabled. Check for enable_fbc_wm in
2342 * case both are at the same level. Prefer r1 in case they're the same. */
Imre Deak820c1982013-12-17 14:46:36 +02002343static struct intel_pipe_wm *ilk_find_best_result(struct drm_device *dev,
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002344 struct intel_pipe_wm *r1,
2345 struct intel_pipe_wm *r2)
Paulo Zanoni861f3382013-05-31 10:19:21 -03002346{
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002347 int level, max_level = ilk_wm_max_level(dev);
2348 int level1 = 0, level2 = 0;
Paulo Zanoni861f3382013-05-31 10:19:21 -03002349
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002350 for (level = 1; level <= max_level; level++) {
2351 if (r1->wm[level].enable)
2352 level1 = level;
2353 if (r2->wm[level].enable)
2354 level2 = level;
Paulo Zanoni861f3382013-05-31 10:19:21 -03002355 }
2356
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002357 if (level1 == level2) {
2358 if (r2->fbc_wm_enabled && !r1->fbc_wm_enabled)
Paulo Zanoni861f3382013-05-31 10:19:21 -03002359 return r2;
2360 else
2361 return r1;
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002362 } else if (level1 > level2) {
Paulo Zanoni861f3382013-05-31 10:19:21 -03002363 return r1;
2364 } else {
2365 return r2;
2366 }
2367}
2368
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002369/* dirty bits used to track which watermarks need changes */
2370#define WM_DIRTY_PIPE(pipe) (1 << (pipe))
2371#define WM_DIRTY_LINETIME(pipe) (1 << (8 + (pipe)))
2372#define WM_DIRTY_LP(wm_lp) (1 << (15 + (wm_lp)))
2373#define WM_DIRTY_LP_ALL (WM_DIRTY_LP(1) | WM_DIRTY_LP(2) | WM_DIRTY_LP(3))
2374#define WM_DIRTY_FBC (1 << 24)
2375#define WM_DIRTY_DDB (1 << 25)
2376
2377static unsigned int ilk_compute_wm_dirty(struct drm_device *dev,
Imre Deak820c1982013-12-17 14:46:36 +02002378 const struct ilk_wm_values *old,
2379 const struct ilk_wm_values *new)
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002380{
2381 unsigned int dirty = 0;
2382 enum pipe pipe;
2383 int wm_lp;
2384
2385 for_each_pipe(pipe) {
2386 if (old->wm_linetime[pipe] != new->wm_linetime[pipe]) {
2387 dirty |= WM_DIRTY_LINETIME(pipe);
2388 /* Must disable LP1+ watermarks too */
2389 dirty |= WM_DIRTY_LP_ALL;
2390 }
2391
2392 if (old->wm_pipe[pipe] != new->wm_pipe[pipe]) {
2393 dirty |= WM_DIRTY_PIPE(pipe);
2394 /* Must disable LP1+ watermarks too */
2395 dirty |= WM_DIRTY_LP_ALL;
2396 }
2397 }
2398
2399 if (old->enable_fbc_wm != new->enable_fbc_wm) {
2400 dirty |= WM_DIRTY_FBC;
2401 /* Must disable LP1+ watermarks too */
2402 dirty |= WM_DIRTY_LP_ALL;
2403 }
2404
2405 if (old->partitioning != new->partitioning) {
2406 dirty |= WM_DIRTY_DDB;
2407 /* Must disable LP1+ watermarks too */
2408 dirty |= WM_DIRTY_LP_ALL;
2409 }
2410
2411 /* LP1+ watermarks already deemed dirty, no need to continue */
2412 if (dirty & WM_DIRTY_LP_ALL)
2413 return dirty;
2414
2415 /* Find the lowest numbered LP1+ watermark in need of an update... */
2416 for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
2417 if (old->wm_lp[wm_lp - 1] != new->wm_lp[wm_lp - 1] ||
2418 old->wm_lp_spr[wm_lp - 1] != new->wm_lp_spr[wm_lp - 1])
2419 break;
2420 }
2421
2422 /* ...and mark it and all higher numbered LP1+ watermarks as dirty */
2423 for (; wm_lp <= 3; wm_lp++)
2424 dirty |= WM_DIRTY_LP(wm_lp);
2425
2426 return dirty;
2427}
2428
Ville Syrjälä8553c182013-12-05 15:51:39 +02002429static bool _ilk_disable_lp_wm(struct drm_i915_private *dev_priv,
2430 unsigned int dirty)
2431{
Imre Deak820c1982013-12-17 14:46:36 +02002432 struct ilk_wm_values *previous = &dev_priv->wm.hw;
Ville Syrjälä8553c182013-12-05 15:51:39 +02002433 bool changed = false;
2434
2435 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] & WM1_LP_SR_EN) {
2436 previous->wm_lp[2] &= ~WM1_LP_SR_EN;
2437 I915_WRITE(WM3_LP_ILK, previous->wm_lp[2]);
2438 changed = true;
2439 }
2440 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] & WM1_LP_SR_EN) {
2441 previous->wm_lp[1] &= ~WM1_LP_SR_EN;
2442 I915_WRITE(WM2_LP_ILK, previous->wm_lp[1]);
2443 changed = true;
2444 }
2445 if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] & WM1_LP_SR_EN) {
2446 previous->wm_lp[0] &= ~WM1_LP_SR_EN;
2447 I915_WRITE(WM1_LP_ILK, previous->wm_lp[0]);
2448 changed = true;
2449 }
2450
2451 /*
2452 * Don't touch WM1S_LP_EN here.
2453 * Doing so could cause underruns.
2454 */
2455
2456 return changed;
2457}
2458
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002459/*
2460 * The spec says we shouldn't write when we don't need, because every write
2461 * causes WMs to be re-evaluated, expending some power.
2462 */
Imre Deak820c1982013-12-17 14:46:36 +02002463static void ilk_write_wm_values(struct drm_i915_private *dev_priv,
2464 struct ilk_wm_values *results)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002465{
Ville Syrjäläac9545f2013-12-05 15:51:28 +02002466 struct drm_device *dev = dev_priv->dev;
Imre Deak820c1982013-12-17 14:46:36 +02002467 struct ilk_wm_values *previous = &dev_priv->wm.hw;
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002468 unsigned int dirty;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002469 uint32_t val;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002470
Ville Syrjälä8553c182013-12-05 15:51:39 +02002471 dirty = ilk_compute_wm_dirty(dev, previous, results);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002472 if (!dirty)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002473 return;
2474
Ville Syrjälä8553c182013-12-05 15:51:39 +02002475 _ilk_disable_lp_wm(dev_priv, dirty);
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02002476
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002477 if (dirty & WM_DIRTY_PIPE(PIPE_A))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002478 I915_WRITE(WM0_PIPEA_ILK, results->wm_pipe[0]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002479 if (dirty & WM_DIRTY_PIPE(PIPE_B))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002480 I915_WRITE(WM0_PIPEB_ILK, results->wm_pipe[1]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002481 if (dirty & WM_DIRTY_PIPE(PIPE_C))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002482 I915_WRITE(WM0_PIPEC_IVB, results->wm_pipe[2]);
2483
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002484 if (dirty & WM_DIRTY_LINETIME(PIPE_A))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002485 I915_WRITE(PIPE_WM_LINETIME(PIPE_A), results->wm_linetime[0]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002486 if (dirty & WM_DIRTY_LINETIME(PIPE_B))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002487 I915_WRITE(PIPE_WM_LINETIME(PIPE_B), results->wm_linetime[1]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002488 if (dirty & WM_DIRTY_LINETIME(PIPE_C))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002489 I915_WRITE(PIPE_WM_LINETIME(PIPE_C), results->wm_linetime[2]);
2490
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002491 if (dirty & WM_DIRTY_DDB) {
Ville Syrjäläa42a5712014-01-07 16:14:08 +02002492 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Ville Syrjäläac9545f2013-12-05 15:51:28 +02002493 val = I915_READ(WM_MISC);
2494 if (results->partitioning == INTEL_DDB_PART_1_2)
2495 val &= ~WM_MISC_DATA_PARTITION_5_6;
2496 else
2497 val |= WM_MISC_DATA_PARTITION_5_6;
2498 I915_WRITE(WM_MISC, val);
2499 } else {
2500 val = I915_READ(DISP_ARB_CTL2);
2501 if (results->partitioning == INTEL_DDB_PART_1_2)
2502 val &= ~DISP_DATA_PARTITION_5_6;
2503 else
2504 val |= DISP_DATA_PARTITION_5_6;
2505 I915_WRITE(DISP_ARB_CTL2, val);
2506 }
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002507 }
2508
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002509 if (dirty & WM_DIRTY_FBC) {
Paulo Zanonicca32e92013-05-31 11:45:06 -03002510 val = I915_READ(DISP_ARB_CTL);
2511 if (results->enable_fbc_wm)
2512 val &= ~DISP_FBC_WM_DIS;
2513 else
2514 val |= DISP_FBC_WM_DIS;
2515 I915_WRITE(DISP_ARB_CTL, val);
2516 }
2517
Imre Deak954911e2013-12-17 14:46:34 +02002518 if (dirty & WM_DIRTY_LP(1) &&
2519 previous->wm_lp_spr[0] != results->wm_lp_spr[0])
2520 I915_WRITE(WM1S_LP_ILK, results->wm_lp_spr[0]);
2521
2522 if (INTEL_INFO(dev)->gen >= 7) {
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02002523 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp_spr[1] != results->wm_lp_spr[1])
2524 I915_WRITE(WM2S_LP_IVB, results->wm_lp_spr[1]);
2525 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp_spr[2] != results->wm_lp_spr[2])
2526 I915_WRITE(WM3S_LP_IVB, results->wm_lp_spr[2]);
2527 }
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002528
Ville Syrjäläfacd619b2013-12-05 15:51:33 +02002529 if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] != results->wm_lp[0])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002530 I915_WRITE(WM1_LP_ILK, results->wm_lp[0]);
Ville Syrjäläfacd619b2013-12-05 15:51:33 +02002531 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] != results->wm_lp[1])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002532 I915_WRITE(WM2_LP_ILK, results->wm_lp[1]);
Ville Syrjäläfacd619b2013-12-05 15:51:33 +02002533 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] != results->wm_lp[2])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002534 I915_WRITE(WM3_LP_ILK, results->wm_lp[2]);
Ville Syrjälä609cede2013-10-09 19:18:03 +03002535
2536 dev_priv->wm.hw = *results;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002537}
2538
Ville Syrjälä8553c182013-12-05 15:51:39 +02002539static bool ilk_disable_lp_wm(struct drm_device *dev)
2540{
2541 struct drm_i915_private *dev_priv = dev->dev_private;
2542
2543 return _ilk_disable_lp_wm(dev_priv, WM_DIRTY_LP_ALL);
2544}
2545
Imre Deak820c1982013-12-17 14:46:36 +02002546static void ilk_update_wm(struct drm_crtc *crtc)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002547{
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03002548 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjälä46ba6142013-09-10 11:40:40 +03002549 struct drm_device *dev = crtc->dev;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002550 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak820c1982013-12-17 14:46:36 +02002551 struct ilk_wm_maximums max;
2552 struct ilk_pipe_wm_parameters params = {};
2553 struct ilk_wm_values results = {};
Ville Syrjälä77c122b2013-08-06 22:24:04 +03002554 enum intel_ddb_partitioning partitioning;
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03002555 struct intel_pipe_wm pipe_wm = {};
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002556 struct intel_pipe_wm lp_wm_1_2 = {}, lp_wm_5_6 = {}, *best_lp_wm;
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03002557 struct intel_wm_config config = {};
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002558
Imre Deak820c1982013-12-17 14:46:36 +02002559 ilk_compute_wm_parameters(crtc, &params, &config);
Paulo Zanoni861f3382013-05-31 10:19:21 -03002560
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03002561 intel_compute_pipe_wm(crtc, &params, &pipe_wm);
2562
2563 if (!memcmp(&intel_crtc->wm.active, &pipe_wm, sizeof(pipe_wm)))
2564 return;
2565
2566 intel_crtc->wm.active = pipe_wm;
2567
Ville Syrjälä34982fe2013-10-09 19:18:09 +03002568 ilk_compute_wm_maximums(dev, 1, &config, INTEL_DDB_PART_1_2, &max);
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02002569 ilk_wm_merge(dev, &config, &max, &lp_wm_1_2);
Ville Syrjälä0362c782013-10-09 19:17:57 +03002570
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03002571 /* 5/6 split only in single pipe config on IVB+ */
Ville Syrjäläec98c8d2013-10-11 15:26:26 +03002572 if (INTEL_INFO(dev)->gen >= 7 &&
2573 config.num_pipes_active == 1 && config.sprites_enabled) {
Ville Syrjälä34982fe2013-10-09 19:18:09 +03002574 ilk_compute_wm_maximums(dev, 1, &config, INTEL_DDB_PART_5_6, &max);
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02002575 ilk_wm_merge(dev, &config, &max, &lp_wm_5_6);
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03002576
Imre Deak820c1982013-12-17 14:46:36 +02002577 best_lp_wm = ilk_find_best_result(dev, &lp_wm_1_2, &lp_wm_5_6);
Paulo Zanoni861f3382013-05-31 10:19:21 -03002578 } else {
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002579 best_lp_wm = &lp_wm_1_2;
Paulo Zanoni861f3382013-05-31 10:19:21 -03002580 }
2581
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002582 partitioning = (best_lp_wm == &lp_wm_1_2) ?
Ville Syrjälä77c122b2013-08-06 22:24:04 +03002583 INTEL_DDB_PART_1_2 : INTEL_DDB_PART_5_6;
Paulo Zanoni861f3382013-05-31 10:19:21 -03002584
Imre Deak820c1982013-12-17 14:46:36 +02002585 ilk_compute_wm_results(dev, best_lp_wm, partitioning, &results);
Ville Syrjälä609cede2013-10-09 19:18:03 +03002586
Imre Deak820c1982013-12-17 14:46:36 +02002587 ilk_write_wm_values(dev_priv, &results);
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002588}
2589
Imre Deak820c1982013-12-17 14:46:36 +02002590static void ilk_update_sprite_wm(struct drm_plane *plane,
Ville Syrjäläadf3d352013-08-06 22:24:11 +03002591 struct drm_crtc *crtc,
Paulo Zanoni526682e2013-05-24 11:59:18 -03002592 uint32_t sprite_width, int pixel_size,
Ville Syrjäläbdd57d02013-07-05 11:57:13 +03002593 bool enabled, bool scaled)
Paulo Zanoni526682e2013-05-24 11:59:18 -03002594{
Ville Syrjälä8553c182013-12-05 15:51:39 +02002595 struct drm_device *dev = plane->dev;
Ville Syrjäläadf3d352013-08-06 22:24:11 +03002596 struct intel_plane *intel_plane = to_intel_plane(plane);
Paulo Zanoni526682e2013-05-24 11:59:18 -03002597
Ville Syrjäläadf3d352013-08-06 22:24:11 +03002598 intel_plane->wm.enabled = enabled;
2599 intel_plane->wm.scaled = scaled;
2600 intel_plane->wm.horiz_pixels = sprite_width;
2601 intel_plane->wm.bytes_per_pixel = pixel_size;
Paulo Zanoni526682e2013-05-24 11:59:18 -03002602
Ville Syrjälä8553c182013-12-05 15:51:39 +02002603 /*
2604 * IVB workaround: must disable low power watermarks for at least
2605 * one frame before enabling scaling. LP watermarks can be re-enabled
2606 * when scaling is disabled.
2607 *
2608 * WaCxSRDisabledForSpriteScaling:ivb
2609 */
2610 if (IS_IVYBRIDGE(dev) && scaled && ilk_disable_lp_wm(dev))
2611 intel_wait_for_vblank(dev, intel_plane->pipe);
2612
Imre Deak820c1982013-12-17 14:46:36 +02002613 ilk_update_wm(crtc);
Paulo Zanoni526682e2013-05-24 11:59:18 -03002614}
2615
Ville Syrjälä243e6a42013-10-14 14:55:24 +03002616static void ilk_pipe_wm_get_hw_state(struct drm_crtc *crtc)
2617{
2618 struct drm_device *dev = crtc->dev;
2619 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak820c1982013-12-17 14:46:36 +02002620 struct ilk_wm_values *hw = &dev_priv->wm.hw;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03002621 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2622 struct intel_pipe_wm *active = &intel_crtc->wm.active;
2623 enum pipe pipe = intel_crtc->pipe;
2624 static const unsigned int wm0_pipe_reg[] = {
2625 [PIPE_A] = WM0_PIPEA_ILK,
2626 [PIPE_B] = WM0_PIPEB_ILK,
2627 [PIPE_C] = WM0_PIPEC_IVB,
2628 };
2629
2630 hw->wm_pipe[pipe] = I915_READ(wm0_pipe_reg[pipe]);
Ville Syrjäläa42a5712014-01-07 16:14:08 +02002631 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Ville Syrjäläce0e0712013-12-05 15:51:36 +02002632 hw->wm_linetime[pipe] = I915_READ(PIPE_WM_LINETIME(pipe));
Ville Syrjälä243e6a42013-10-14 14:55:24 +03002633
2634 if (intel_crtc_active(crtc)) {
2635 u32 tmp = hw->wm_pipe[pipe];
2636
2637 /*
2638 * For active pipes LP0 watermark is marked as
2639 * enabled, and LP1+ watermaks as disabled since
2640 * we can't really reverse compute them in case
2641 * multiple pipes are active.
2642 */
2643 active->wm[0].enable = true;
2644 active->wm[0].pri_val = (tmp & WM0_PIPE_PLANE_MASK) >> WM0_PIPE_PLANE_SHIFT;
2645 active->wm[0].spr_val = (tmp & WM0_PIPE_SPRITE_MASK) >> WM0_PIPE_SPRITE_SHIFT;
2646 active->wm[0].cur_val = tmp & WM0_PIPE_CURSOR_MASK;
2647 active->linetime = hw->wm_linetime[pipe];
2648 } else {
2649 int level, max_level = ilk_wm_max_level(dev);
2650
2651 /*
2652 * For inactive pipes, all watermark levels
2653 * should be marked as enabled but zeroed,
2654 * which is what we'd compute them to.
2655 */
2656 for (level = 0; level <= max_level; level++)
2657 active->wm[level].enable = true;
2658 }
2659}
2660
2661void ilk_wm_get_hw_state(struct drm_device *dev)
2662{
2663 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak820c1982013-12-17 14:46:36 +02002664 struct ilk_wm_values *hw = &dev_priv->wm.hw;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03002665 struct drm_crtc *crtc;
2666
2667 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
2668 ilk_pipe_wm_get_hw_state(crtc);
2669
2670 hw->wm_lp[0] = I915_READ(WM1_LP_ILK);
2671 hw->wm_lp[1] = I915_READ(WM2_LP_ILK);
2672 hw->wm_lp[2] = I915_READ(WM3_LP_ILK);
2673
2674 hw->wm_lp_spr[0] = I915_READ(WM1S_LP_ILK);
2675 hw->wm_lp_spr[1] = I915_READ(WM2S_LP_IVB);
2676 hw->wm_lp_spr[2] = I915_READ(WM3S_LP_IVB);
2677
Ville Syrjäläa42a5712014-01-07 16:14:08 +02002678 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Ville Syrjäläac9545f2013-12-05 15:51:28 +02002679 hw->partitioning = (I915_READ(WM_MISC) & WM_MISC_DATA_PARTITION_5_6) ?
2680 INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
2681 else if (IS_IVYBRIDGE(dev))
2682 hw->partitioning = (I915_READ(DISP_ARB_CTL2) & DISP_DATA_PARTITION_5_6) ?
2683 INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03002684
2685 hw->enable_fbc_wm =
2686 !(I915_READ(DISP_ARB_CTL) & DISP_FBC_WM_DIS);
2687}
2688
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002689/**
2690 * intel_update_watermarks - update FIFO watermark values based on current modes
2691 *
2692 * Calculate watermark values for the various WM regs based on current mode
2693 * and plane configuration.
2694 *
2695 * There are several cases to deal with here:
2696 * - normal (i.e. non-self-refresh)
2697 * - self-refresh (SR) mode
2698 * - lines are large relative to FIFO size (buffer can hold up to 2)
2699 * - lines are small relative to FIFO size (buffer can hold more than 2
2700 * lines), so need to account for TLB latency
2701 *
2702 * The normal calculation is:
2703 * watermark = dotclock * bytes per pixel * latency
2704 * where latency is platform & configuration dependent (we assume pessimal
2705 * values here).
2706 *
2707 * The SR calculation is:
2708 * watermark = (trunc(latency/line time)+1) * surface width *
2709 * bytes per pixel
2710 * where
2711 * line time = htotal / dotclock
2712 * surface width = hdisplay for normal plane and 64 for cursor
2713 * and latency is assumed to be high, as above.
2714 *
2715 * The final value programmed to the register should always be rounded up,
2716 * and include an extra 2 entries to account for clock crossings.
2717 *
2718 * We don't use the sprite, so we can ignore that. And on Crestline we have
2719 * to set the non-SR watermarks to 8.
2720 */
Ville Syrjälä46ba6142013-09-10 11:40:40 +03002721void intel_update_watermarks(struct drm_crtc *crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002722{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03002723 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002724
2725 if (dev_priv->display.update_wm)
Ville Syrjälä46ba6142013-09-10 11:40:40 +03002726 dev_priv->display.update_wm(crtc);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002727}
2728
Ville Syrjäläadf3d352013-08-06 22:24:11 +03002729void intel_update_sprite_watermarks(struct drm_plane *plane,
2730 struct drm_crtc *crtc,
Paulo Zanoni4c4ff432013-05-24 11:59:17 -03002731 uint32_t sprite_width, int pixel_size,
Ville Syrjälä39db4a42013-08-06 22:24:00 +03002732 bool enabled, bool scaled)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002733{
Ville Syrjäläadf3d352013-08-06 22:24:11 +03002734 struct drm_i915_private *dev_priv = plane->dev->dev_private;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002735
2736 if (dev_priv->display.update_sprite_wm)
Ville Syrjäläadf3d352013-08-06 22:24:11 +03002737 dev_priv->display.update_sprite_wm(plane, crtc, sprite_width,
Ville Syrjälä39db4a42013-08-06 22:24:00 +03002738 pixel_size, enabled, scaled);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002739}
2740
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002741static struct drm_i915_gem_object *
2742intel_alloc_context_page(struct drm_device *dev)
2743{
2744 struct drm_i915_gem_object *ctx;
2745 int ret;
2746
2747 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2748
2749 ctx = i915_gem_alloc_object(dev, 4096);
2750 if (!ctx) {
2751 DRM_DEBUG("failed to alloc power context, RC6 disabled\n");
2752 return NULL;
2753 }
2754
Ben Widawskyc37e2202013-07-31 16:59:58 -07002755 ret = i915_gem_obj_ggtt_pin(ctx, 4096, true, false);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002756 if (ret) {
2757 DRM_ERROR("failed to pin power context: %d\n", ret);
2758 goto err_unref;
2759 }
2760
2761 ret = i915_gem_object_set_to_gtt_domain(ctx, 1);
2762 if (ret) {
2763 DRM_ERROR("failed to set-domain on power context: %d\n", ret);
2764 goto err_unpin;
2765 }
2766
2767 return ctx;
2768
2769err_unpin:
2770 i915_gem_object_unpin(ctx);
2771err_unref:
2772 drm_gem_object_unreference(&ctx->base);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002773 return NULL;
2774}
2775
Daniel Vetter92703882012-08-09 16:46:01 +02002776/**
2777 * Lock protecting IPS related data structures
Daniel Vetter92703882012-08-09 16:46:01 +02002778 */
2779DEFINE_SPINLOCK(mchdev_lock);
2780
2781/* Global for IPS driver to get at the current i915 device. Protected by
2782 * mchdev_lock. */
2783static struct drm_i915_private *i915_mch_dev;
2784
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002785bool ironlake_set_drps(struct drm_device *dev, u8 val)
2786{
2787 struct drm_i915_private *dev_priv = dev->dev_private;
2788 u16 rgvswctl;
2789
Daniel Vetter92703882012-08-09 16:46:01 +02002790 assert_spin_locked(&mchdev_lock);
2791
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002792 rgvswctl = I915_READ16(MEMSWCTL);
2793 if (rgvswctl & MEMCTL_CMD_STS) {
2794 DRM_DEBUG("gpu busy, RCS change rejected\n");
2795 return false; /* still busy with another command */
2796 }
2797
2798 rgvswctl = (MEMCTL_CMD_CHFREQ << MEMCTL_CMD_SHIFT) |
2799 (val << MEMCTL_FREQ_SHIFT) | MEMCTL_SFCAVM;
2800 I915_WRITE16(MEMSWCTL, rgvswctl);
2801 POSTING_READ16(MEMSWCTL);
2802
2803 rgvswctl |= MEMCTL_CMD_STS;
2804 I915_WRITE16(MEMSWCTL, rgvswctl);
2805
2806 return true;
2807}
2808
Daniel Vetter8090c6b2012-06-24 16:42:32 +02002809static void ironlake_enable_drps(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002810{
2811 struct drm_i915_private *dev_priv = dev->dev_private;
2812 u32 rgvmodectl = I915_READ(MEMMODECTL);
2813 u8 fmax, fmin, fstart, vstart;
2814
Daniel Vetter92703882012-08-09 16:46:01 +02002815 spin_lock_irq(&mchdev_lock);
2816
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002817 /* Enable temp reporting */
2818 I915_WRITE16(PMMISC, I915_READ(PMMISC) | MCPPCE_EN);
2819 I915_WRITE16(TSC1, I915_READ(TSC1) | TSE);
2820
2821 /* 100ms RC evaluation intervals */
2822 I915_WRITE(RCUPEI, 100000);
2823 I915_WRITE(RCDNEI, 100000);
2824
2825 /* Set max/min thresholds to 90ms and 80ms respectively */
2826 I915_WRITE(RCBMAXAVG, 90000);
2827 I915_WRITE(RCBMINAVG, 80000);
2828
2829 I915_WRITE(MEMIHYST, 1);
2830
2831 /* Set up min, max, and cur for interrupt handling */
2832 fmax = (rgvmodectl & MEMMODE_FMAX_MASK) >> MEMMODE_FMAX_SHIFT;
2833 fmin = (rgvmodectl & MEMMODE_FMIN_MASK);
2834 fstart = (rgvmodectl & MEMMODE_FSTART_MASK) >>
2835 MEMMODE_FSTART_SHIFT;
2836
2837 vstart = (I915_READ(PXVFREQ_BASE + (fstart * 4)) & PXVFREQ_PX_MASK) >>
2838 PXVFREQ_PX_SHIFT;
2839
Daniel Vetter20e4d402012-08-08 23:35:39 +02002840 dev_priv->ips.fmax = fmax; /* IPS callback will increase this */
2841 dev_priv->ips.fstart = fstart;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002842
Daniel Vetter20e4d402012-08-08 23:35:39 +02002843 dev_priv->ips.max_delay = fstart;
2844 dev_priv->ips.min_delay = fmin;
2845 dev_priv->ips.cur_delay = fstart;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002846
2847 DRM_DEBUG_DRIVER("fmax: %d, fmin: %d, fstart: %d\n",
2848 fmax, fmin, fstart);
2849
2850 I915_WRITE(MEMINTREN, MEMINT_CX_SUPR_EN | MEMINT_EVAL_CHG_EN);
2851
2852 /*
2853 * Interrupts will be enabled in ironlake_irq_postinstall
2854 */
2855
2856 I915_WRITE(VIDSTART, vstart);
2857 POSTING_READ(VIDSTART);
2858
2859 rgvmodectl |= MEMMODE_SWMODE_EN;
2860 I915_WRITE(MEMMODECTL, rgvmodectl);
2861
Daniel Vetter92703882012-08-09 16:46:01 +02002862 if (wait_for_atomic((I915_READ(MEMSWCTL) & MEMCTL_CMD_STS) == 0, 10))
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002863 DRM_ERROR("stuck trying to change perf mode\n");
Daniel Vetter92703882012-08-09 16:46:01 +02002864 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002865
2866 ironlake_set_drps(dev, fstart);
2867
Daniel Vetter20e4d402012-08-08 23:35:39 +02002868 dev_priv->ips.last_count1 = I915_READ(0x112e4) + I915_READ(0x112e8) +
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002869 I915_READ(0x112e0);
Daniel Vetter20e4d402012-08-08 23:35:39 +02002870 dev_priv->ips.last_time1 = jiffies_to_msecs(jiffies);
2871 dev_priv->ips.last_count2 = I915_READ(0x112f4);
2872 getrawmonotonic(&dev_priv->ips.last_time2);
Daniel Vetter92703882012-08-09 16:46:01 +02002873
2874 spin_unlock_irq(&mchdev_lock);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002875}
2876
Daniel Vetter8090c6b2012-06-24 16:42:32 +02002877static void ironlake_disable_drps(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002878{
2879 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter92703882012-08-09 16:46:01 +02002880 u16 rgvswctl;
2881
2882 spin_lock_irq(&mchdev_lock);
2883
2884 rgvswctl = I915_READ16(MEMSWCTL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002885
2886 /* Ack interrupts, disable EFC interrupt */
2887 I915_WRITE(MEMINTREN, I915_READ(MEMINTREN) & ~MEMINT_EVAL_CHG_EN);
2888 I915_WRITE(MEMINTRSTS, MEMINT_EVAL_CHG);
2889 I915_WRITE(DEIER, I915_READ(DEIER) & ~DE_PCU_EVENT);
2890 I915_WRITE(DEIIR, DE_PCU_EVENT);
2891 I915_WRITE(DEIMR, I915_READ(DEIMR) | DE_PCU_EVENT);
2892
2893 /* Go back to the starting frequency */
Daniel Vetter20e4d402012-08-08 23:35:39 +02002894 ironlake_set_drps(dev, dev_priv->ips.fstart);
Daniel Vetter92703882012-08-09 16:46:01 +02002895 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002896 rgvswctl |= MEMCTL_CMD_STS;
2897 I915_WRITE(MEMSWCTL, rgvswctl);
Daniel Vetter92703882012-08-09 16:46:01 +02002898 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002899
Daniel Vetter92703882012-08-09 16:46:01 +02002900 spin_unlock_irq(&mchdev_lock);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002901}
2902
Daniel Vetteracbe9472012-07-26 11:50:05 +02002903/* There's a funny hw issue where the hw returns all 0 when reading from
2904 * GEN6_RP_INTERRUPT_LIMITS. Hence we always need to compute the desired value
2905 * ourselves, instead of doing a rmw cycle (which might result in us clearing
2906 * all limits and the gpu stuck at whatever frequency it is at atm).
2907 */
Chris Wilson6917c7b2013-11-06 13:56:26 -02002908static u32 gen6_rps_limits(struct drm_i915_private *dev_priv, u8 val)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002909{
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01002910 u32 limits;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002911
Daniel Vetter20b46e52012-07-26 11:16:14 +02002912 /* Only set the down limit when we've reached the lowest level to avoid
2913 * getting more interrupts, otherwise leave this clear. This prevents a
2914 * race in the hw when coming out of rc6: There's a tiny window where
2915 * the hw runs at the minimal clock before selecting the desired
2916 * frequency, if the down threshold expires in that window we will not
2917 * receive a down interrupt. */
Chris Wilson6917c7b2013-11-06 13:56:26 -02002918 limits = dev_priv->rps.max_delay << 24;
2919 if (val <= dev_priv->rps.min_delay)
Daniel Vetterc6a828d2012-08-08 23:35:35 +02002920 limits |= dev_priv->rps.min_delay << 16;
Daniel Vetter20b46e52012-07-26 11:16:14 +02002921
2922 return limits;
2923}
2924
Chris Wilsondd75fdc2013-09-25 17:34:57 +01002925static void gen6_set_rps_thresholds(struct drm_i915_private *dev_priv, u8 val)
2926{
2927 int new_power;
2928
2929 new_power = dev_priv->rps.power;
2930 switch (dev_priv->rps.power) {
2931 case LOW_POWER:
2932 if (val > dev_priv->rps.rpe_delay + 1 && val > dev_priv->rps.cur_delay)
2933 new_power = BETWEEN;
2934 break;
2935
2936 case BETWEEN:
2937 if (val <= dev_priv->rps.rpe_delay && val < dev_priv->rps.cur_delay)
2938 new_power = LOW_POWER;
2939 else if (val >= dev_priv->rps.rp0_delay && val > dev_priv->rps.cur_delay)
2940 new_power = HIGH_POWER;
2941 break;
2942
2943 case HIGH_POWER:
2944 if (val < (dev_priv->rps.rp1_delay + dev_priv->rps.rp0_delay) >> 1 && val < dev_priv->rps.cur_delay)
2945 new_power = BETWEEN;
2946 break;
2947 }
2948 /* Max/min bins are special */
2949 if (val == dev_priv->rps.min_delay)
2950 new_power = LOW_POWER;
2951 if (val == dev_priv->rps.max_delay)
2952 new_power = HIGH_POWER;
2953 if (new_power == dev_priv->rps.power)
2954 return;
2955
2956 /* Note the units here are not exactly 1us, but 1280ns. */
2957 switch (new_power) {
2958 case LOW_POWER:
2959 /* Upclock if more than 95% busy over 16ms */
2960 I915_WRITE(GEN6_RP_UP_EI, 12500);
2961 I915_WRITE(GEN6_RP_UP_THRESHOLD, 11800);
2962
2963 /* Downclock if less than 85% busy over 32ms */
2964 I915_WRITE(GEN6_RP_DOWN_EI, 25000);
2965 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 21250);
2966
2967 I915_WRITE(GEN6_RP_CONTROL,
2968 GEN6_RP_MEDIA_TURBO |
2969 GEN6_RP_MEDIA_HW_NORMAL_MODE |
2970 GEN6_RP_MEDIA_IS_GFX |
2971 GEN6_RP_ENABLE |
2972 GEN6_RP_UP_BUSY_AVG |
2973 GEN6_RP_DOWN_IDLE_AVG);
2974 break;
2975
2976 case BETWEEN:
2977 /* Upclock if more than 90% busy over 13ms */
2978 I915_WRITE(GEN6_RP_UP_EI, 10250);
2979 I915_WRITE(GEN6_RP_UP_THRESHOLD, 9225);
2980
2981 /* Downclock if less than 75% busy over 32ms */
2982 I915_WRITE(GEN6_RP_DOWN_EI, 25000);
2983 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 18750);
2984
2985 I915_WRITE(GEN6_RP_CONTROL,
2986 GEN6_RP_MEDIA_TURBO |
2987 GEN6_RP_MEDIA_HW_NORMAL_MODE |
2988 GEN6_RP_MEDIA_IS_GFX |
2989 GEN6_RP_ENABLE |
2990 GEN6_RP_UP_BUSY_AVG |
2991 GEN6_RP_DOWN_IDLE_AVG);
2992 break;
2993
2994 case HIGH_POWER:
2995 /* Upclock if more than 85% busy over 10ms */
2996 I915_WRITE(GEN6_RP_UP_EI, 8000);
2997 I915_WRITE(GEN6_RP_UP_THRESHOLD, 6800);
2998
2999 /* Downclock if less than 60% busy over 32ms */
3000 I915_WRITE(GEN6_RP_DOWN_EI, 25000);
3001 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 15000);
3002
3003 I915_WRITE(GEN6_RP_CONTROL,
3004 GEN6_RP_MEDIA_TURBO |
3005 GEN6_RP_MEDIA_HW_NORMAL_MODE |
3006 GEN6_RP_MEDIA_IS_GFX |
3007 GEN6_RP_ENABLE |
3008 GEN6_RP_UP_BUSY_AVG |
3009 GEN6_RP_DOWN_IDLE_AVG);
3010 break;
3011 }
3012
3013 dev_priv->rps.power = new_power;
3014 dev_priv->rps.last_adj = 0;
3015}
3016
Daniel Vetter20b46e52012-07-26 11:16:14 +02003017void gen6_set_rps(struct drm_device *dev, u8 val)
3018{
3019 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01003020
Jesse Barnes4fc688c2012-11-02 11:14:01 -07003021 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Ben Widawsky79249632012-09-07 19:43:42 -07003022 WARN_ON(val > dev_priv->rps.max_delay);
3023 WARN_ON(val < dev_priv->rps.min_delay);
Daniel Vetter004777c2012-08-09 15:07:01 +02003024
Daniel Vetterc6a828d2012-08-08 23:35:35 +02003025 if (val == dev_priv->rps.cur_delay)
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01003026 return;
3027
Chris Wilsondd75fdc2013-09-25 17:34:57 +01003028 gen6_set_rps_thresholds(dev_priv, val);
3029
Rodrigo Vivi92bd1bf2013-03-25 17:55:49 -03003030 if (IS_HASWELL(dev))
3031 I915_WRITE(GEN6_RPNSWREQ,
3032 HSW_FREQUENCY(val));
3033 else
3034 I915_WRITE(GEN6_RPNSWREQ,
3035 GEN6_FREQUENCY(val) |
3036 GEN6_OFFSET(0) |
3037 GEN6_AGGRESSIVE_TURBO);
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01003038
3039 /* Make sure we continue to get interrupts
3040 * until we hit the minimum or maximum frequencies.
3041 */
Chris Wilson6917c7b2013-11-06 13:56:26 -02003042 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS,
3043 gen6_rps_limits(dev_priv, val));
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01003044
Ben Widawskyd5570a72012-09-07 19:43:41 -07003045 POSTING_READ(GEN6_RPNSWREQ);
3046
Daniel Vetterc6a828d2012-08-08 23:35:35 +02003047 dev_priv->rps.cur_delay = val;
Daniel Vetterbe2cde9a2012-08-30 13:26:48 +02003048
3049 trace_intel_gpu_freq_change(val * 50);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003050}
3051
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003052void gen6_rps_idle(struct drm_i915_private *dev_priv)
3053{
Damien Lespiau691bb712013-12-12 14:36:36 +00003054 struct drm_device *dev = dev_priv->dev;
3055
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003056 mutex_lock(&dev_priv->rps.hw_lock);
Chris Wilsonc0951f02013-10-10 21:58:50 +01003057 if (dev_priv->rps.enabled) {
Damien Lespiau691bb712013-12-12 14:36:36 +00003058 if (IS_VALLEYVIEW(dev))
Chris Wilsonc0951f02013-10-10 21:58:50 +01003059 valleyview_set_rps(dev_priv->dev, dev_priv->rps.min_delay);
3060 else
3061 gen6_set_rps(dev_priv->dev, dev_priv->rps.min_delay);
3062 dev_priv->rps.last_adj = 0;
3063 }
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003064 mutex_unlock(&dev_priv->rps.hw_lock);
3065}
3066
3067void gen6_rps_boost(struct drm_i915_private *dev_priv)
3068{
Damien Lespiau691bb712013-12-12 14:36:36 +00003069 struct drm_device *dev = dev_priv->dev;
3070
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003071 mutex_lock(&dev_priv->rps.hw_lock);
Chris Wilsonc0951f02013-10-10 21:58:50 +01003072 if (dev_priv->rps.enabled) {
Damien Lespiau691bb712013-12-12 14:36:36 +00003073 if (IS_VALLEYVIEW(dev))
Chris Wilsonc0951f02013-10-10 21:58:50 +01003074 valleyview_set_rps(dev_priv->dev, dev_priv->rps.max_delay);
3075 else
3076 gen6_set_rps(dev_priv->dev, dev_priv->rps.max_delay);
3077 dev_priv->rps.last_adj = 0;
3078 }
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003079 mutex_unlock(&dev_priv->rps.hw_lock);
3080}
3081
Jesse Barnes0a073b82013-04-17 15:54:58 -07003082void valleyview_set_rps(struct drm_device *dev, u8 val)
3083{
3084 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä7a670922013-06-25 19:21:06 +03003085
Jesse Barnes0a073b82013-04-17 15:54:58 -07003086 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
3087 WARN_ON(val > dev_priv->rps.max_delay);
3088 WARN_ON(val < dev_priv->rps.min_delay);
3089
Ville Syrjälä73008b92013-06-25 19:21:01 +03003090 DRM_DEBUG_DRIVER("GPU freq request from %d MHz (%u) to %d MHz (%u)\n",
Ville Syrjälä2ec38152013-11-05 22:42:29 +02003091 vlv_gpu_freq(dev_priv, dev_priv->rps.cur_delay),
Ville Syrjälä73008b92013-06-25 19:21:01 +03003092 dev_priv->rps.cur_delay,
Ville Syrjälä2ec38152013-11-05 22:42:29 +02003093 vlv_gpu_freq(dev_priv, val), val);
Jesse Barnes0a073b82013-04-17 15:54:58 -07003094
3095 if (val == dev_priv->rps.cur_delay)
3096 return;
3097
Jani Nikulaae992582013-05-22 15:36:19 +03003098 vlv_punit_write(dev_priv, PUNIT_REG_GPU_FREQ_REQ, val);
Jesse Barnes0a073b82013-04-17 15:54:58 -07003099
Ville Syrjälä80814ae2013-06-25 19:21:02 +03003100 dev_priv->rps.cur_delay = val;
Jesse Barnes0a073b82013-04-17 15:54:58 -07003101
Ville Syrjälä2ec38152013-11-05 22:42:29 +02003102 trace_intel_gpu_freq_change(vlv_gpu_freq(dev_priv, val));
Jesse Barnes0a073b82013-04-17 15:54:58 -07003103}
3104
Daniel Vetter44fc7d52013-07-12 22:43:27 +02003105static void gen6_disable_rps_interrupts(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003106{
3107 struct drm_i915_private *dev_priv = dev->dev_private;
3108
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003109 I915_WRITE(GEN6_PMINTRMSK, 0xffffffff);
Ben Widawsky48484052013-05-28 19:22:27 -07003110 I915_WRITE(GEN6_PMIER, I915_READ(GEN6_PMIER) & ~GEN6_PM_RPS_EVENTS);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003111 /* Complete PM interrupt masking here doesn't race with the rps work
3112 * item again unmasking PM interrupts because that is using a different
3113 * register (PMIMR) to mask PM interrupts. The only risk is in leaving
3114 * stale bits in PMIIR and PMIMR which gen6_enable_rps will clean up. */
3115
Daniel Vetter59cdb632013-07-04 23:35:28 +02003116 spin_lock_irq(&dev_priv->irq_lock);
Daniel Vetterc6a828d2012-08-08 23:35:35 +02003117 dev_priv->rps.pm_iir = 0;
Daniel Vetter59cdb632013-07-04 23:35:28 +02003118 spin_unlock_irq(&dev_priv->irq_lock);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003119
Ben Widawsky48484052013-05-28 19:22:27 -07003120 I915_WRITE(GEN6_PMIIR, GEN6_PM_RPS_EVENTS);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003121}
3122
Daniel Vetter44fc7d52013-07-12 22:43:27 +02003123static void gen6_disable_rps(struct drm_device *dev)
3124{
3125 struct drm_i915_private *dev_priv = dev->dev_private;
3126
3127 I915_WRITE(GEN6_RC_CONTROL, 0);
3128 I915_WRITE(GEN6_RPNSWREQ, 1 << 31);
3129
3130 gen6_disable_rps_interrupts(dev);
3131}
3132
Jesse Barnesd20d4f02013-04-23 10:09:28 -07003133static void valleyview_disable_rps(struct drm_device *dev)
3134{
3135 struct drm_i915_private *dev_priv = dev->dev_private;
3136
3137 I915_WRITE(GEN6_RC_CONTROL, 0);
Jesse Barnesd20d4f02013-04-23 10:09:28 -07003138
Daniel Vetter44fc7d52013-07-12 22:43:27 +02003139 gen6_disable_rps_interrupts(dev);
Jesse Barnesc9cddff2013-05-08 10:45:13 -07003140
3141 if (dev_priv->vlv_pctx) {
3142 drm_gem_object_unreference(&dev_priv->vlv_pctx->base);
3143 dev_priv->vlv_pctx = NULL;
3144 }
Jesse Barnesd20d4f02013-04-23 10:09:28 -07003145}
3146
Ben Widawskydc39fff2013-10-18 12:32:07 -07003147static void intel_print_rc6_info(struct drm_device *dev, u32 mode)
3148{
3149 if (IS_GEN6(dev))
3150 DRM_DEBUG_DRIVER("Sandybridge: deep RC6 disabled\n");
3151
3152 if (IS_HASWELL(dev))
3153 DRM_DEBUG_DRIVER("Haswell: only RC6 available\n");
3154
3155 DRM_INFO("Enabling RC6 states: RC6 %s, RC6p %s, RC6pp %s\n",
3156 (mode & GEN6_RC_CTL_RC6_ENABLE) ? "on" : "off",
3157 (mode & GEN6_RC_CTL_RC6p_ENABLE) ? "on" : "off",
3158 (mode & GEN6_RC_CTL_RC6pp_ENABLE) ? "on" : "off");
3159}
3160
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003161int intel_enable_rc6(const struct drm_device *dev)
3162{
Damien Lespiaueb4926e2013-06-07 17:41:14 +01003163 /* No RC6 before Ironlake */
3164 if (INTEL_INFO(dev)->gen < 5)
3165 return 0;
3166
Daniel Vetter456470e2012-08-08 23:35:40 +02003167 /* Respect the kernel parameter if it is set */
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003168 if (i915_enable_rc6 >= 0)
3169 return i915_enable_rc6;
3170
Chris Wilson6567d742012-11-10 10:00:06 +00003171 /* Disable RC6 on Ironlake */
3172 if (INTEL_INFO(dev)->gen == 5)
3173 return 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003174
Ben Widawskydc39fff2013-10-18 12:32:07 -07003175 if (IS_HASWELL(dev))
Daniel Vetter456470e2012-08-08 23:35:40 +02003176 return INTEL_RC6_ENABLE;
Daniel Vetter456470e2012-08-08 23:35:40 +02003177
3178 /* snb/ivb have more than one rc6 state. */
Ben Widawskydc39fff2013-10-18 12:32:07 -07003179 if (INTEL_INFO(dev)->gen == 6)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003180 return INTEL_RC6_ENABLE;
Daniel Vetter456470e2012-08-08 23:35:40 +02003181
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003182 return (INTEL_RC6_ENABLE | INTEL_RC6p_ENABLE);
3183}
3184
Daniel Vetter44fc7d52013-07-12 22:43:27 +02003185static void gen6_enable_rps_interrupts(struct drm_device *dev)
3186{
3187 struct drm_i915_private *dev_priv = dev->dev_private;
Mika Kuoppalaa9c1f902013-08-22 21:09:00 +03003188 u32 enabled_intrs;
Daniel Vetter44fc7d52013-07-12 22:43:27 +02003189
3190 spin_lock_irq(&dev_priv->irq_lock);
Daniel Vettera0b33352013-07-04 23:35:34 +02003191 WARN_ON(dev_priv->rps.pm_iir);
Paulo Zanoniedbfdb42013-08-06 18:57:13 -03003192 snb_enable_pm_irq(dev_priv, GEN6_PM_RPS_EVENTS);
Daniel Vetter44fc7d52013-07-12 22:43:27 +02003193 I915_WRITE(GEN6_PMIIR, GEN6_PM_RPS_EVENTS);
3194 spin_unlock_irq(&dev_priv->irq_lock);
Mika Kuoppalaa9c1f902013-08-22 21:09:00 +03003195
Vinit Azadfd547d22013-08-14 13:34:33 -07003196 /* only unmask PM interrupts we need. Mask all others. */
Mika Kuoppalaa9c1f902013-08-22 21:09:00 +03003197 enabled_intrs = GEN6_PM_RPS_EVENTS;
3198
3199 /* IVB and SNB hard hangs on looping batchbuffer
3200 * if GEN6_PM_UP_EI_EXPIRED is masked.
3201 */
3202 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
3203 enabled_intrs |= GEN6_PM_RP_UP_EI_EXPIRED;
3204
3205 I915_WRITE(GEN6_PMINTRMSK, ~enabled_intrs);
Daniel Vetter44fc7d52013-07-12 22:43:27 +02003206}
3207
Ben Widawsky6edee7f2013-11-02 21:07:52 -07003208static void gen8_enable_rps(struct drm_device *dev)
3209{
3210 struct drm_i915_private *dev_priv = dev->dev_private;
3211 struct intel_ring_buffer *ring;
3212 uint32_t rc6_mask = 0, rp_state_cap;
3213 int unused;
3214
3215 /* 1a: Software RC state - RC0 */
3216 I915_WRITE(GEN6_RC_STATE, 0);
3217
3218 /* 1c & 1d: Get forcewake during program sequence. Although the driver
3219 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
Deepak Sc8d9a592013-11-23 14:55:42 +05303220 gen6_gt_force_wake_get(dev_priv, FORCEWAKE_ALL);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07003221
3222 /* 2a: Disable RC states. */
3223 I915_WRITE(GEN6_RC_CONTROL, 0);
3224
3225 rp_state_cap = I915_READ(GEN6_RP_STATE_CAP);
3226
3227 /* 2b: Program RC6 thresholds.*/
3228 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16);
3229 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
3230 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
3231 for_each_ring(ring, dev_priv, unused)
3232 I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
3233 I915_WRITE(GEN6_RC_SLEEP, 0);
3234 I915_WRITE(GEN6_RC6_THRESHOLD, 50000); /* 50/125ms per EI */
3235
3236 /* 3: Enable RC6 */
3237 if (intel_enable_rc6(dev) & INTEL_RC6_ENABLE)
3238 rc6_mask = GEN6_RC_CTL_RC6_ENABLE;
3239 DRM_INFO("RC6 %s\n", (rc6_mask & GEN6_RC_CTL_RC6_ENABLE) ? "on" : "off");
3240 I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
3241 GEN6_RC_CTL_EI_MODE(1) |
3242 rc6_mask);
3243
3244 /* 4 Program defaults and thresholds for RPS*/
3245 I915_WRITE(GEN6_RPNSWREQ, HSW_FREQUENCY(10)); /* Request 500 MHz */
3246 I915_WRITE(GEN6_RC_VIDEO_FREQ, HSW_FREQUENCY(12)); /* Request 600 MHz */
3247 /* NB: Docs say 1s, and 1000000 - which aren't equivalent */
3248 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 100000000 / 128); /* 1 second timeout */
3249
3250 /* Docs recommend 900MHz, and 300 MHz respectively */
3251 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS,
3252 dev_priv->rps.max_delay << 24 |
3253 dev_priv->rps.min_delay << 16);
3254
3255 I915_WRITE(GEN6_RP_UP_THRESHOLD, 7600000 / 128); /* 76ms busyness per EI, 90% */
3256 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 31300000 / 128); /* 313ms busyness per EI, 70%*/
3257 I915_WRITE(GEN6_RP_UP_EI, 66000); /* 84.48ms, XXX: random? */
3258 I915_WRITE(GEN6_RP_DOWN_EI, 350000); /* 448ms, XXX: random? */
3259
3260 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
3261
3262 /* 5: Enable RPS */
3263 I915_WRITE(GEN6_RP_CONTROL,
3264 GEN6_RP_MEDIA_TURBO |
3265 GEN6_RP_MEDIA_HW_NORMAL_MODE |
3266 GEN6_RP_MEDIA_IS_GFX |
3267 GEN6_RP_ENABLE |
3268 GEN6_RP_UP_BUSY_AVG |
3269 GEN6_RP_DOWN_IDLE_AVG);
3270
3271 /* 6: Ring frequency + overclocking (our driver does this later */
3272
3273 gen6_set_rps(dev, (I915_READ(GEN6_GT_PERF_STATUS) & 0xff00) >> 8);
3274
3275 gen6_enable_rps_interrupts(dev);
3276
Deepak Sc8d9a592013-11-23 14:55:42 +05303277 gen6_gt_force_wake_put(dev_priv, FORCEWAKE_ALL);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07003278}
3279
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02003280static void gen6_enable_rps(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003281{
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02003282 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonb4519512012-05-11 14:29:30 +01003283 struct intel_ring_buffer *ring;
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01003284 u32 rp_state_cap;
3285 u32 gt_perf_status;
Ben Widawsky31643d52012-09-26 10:34:01 -07003286 u32 rc6vids, pcu_mbox, rc6_mask = 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003287 u32 gtfifodbg;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003288 int rc6_mode;
Ben Widawsky42c05262012-09-26 10:34:00 -07003289 int i, ret;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003290
Jesse Barnes4fc688c2012-11-02 11:14:01 -07003291 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02003292
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003293 /* Here begins a magic sequence of register writes to enable
3294 * auto-downclocking.
3295 *
3296 * Perhaps there might be some value in exposing these to
3297 * userspace...
3298 */
3299 I915_WRITE(GEN6_RC_STATE, 0);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003300
3301 /* Clear the DBG now so we don't confuse earlier errors */
3302 if ((gtfifodbg = I915_READ(GTFIFODBG))) {
3303 DRM_ERROR("GT fifo had a previous error %x\n", gtfifodbg);
3304 I915_WRITE(GTFIFODBG, gtfifodbg);
3305 }
3306
Deepak Sc8d9a592013-11-23 14:55:42 +05303307 gen6_gt_force_wake_get(dev_priv, FORCEWAKE_ALL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003308
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01003309 rp_state_cap = I915_READ(GEN6_RP_STATE_CAP);
3310 gt_perf_status = I915_READ(GEN6_GT_PERF_STATUS);
3311
Ben Widawsky31c77382013-04-05 14:29:22 -07003312 /* In units of 50MHz */
3313 dev_priv->rps.hw_max = dev_priv->rps.max_delay = rp_state_cap & 0xff;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01003314 dev_priv->rps.min_delay = (rp_state_cap >> 16) & 0xff;
3315 dev_priv->rps.rp1_delay = (rp_state_cap >> 8) & 0xff;
3316 dev_priv->rps.rp0_delay = (rp_state_cap >> 0) & 0xff;
3317 dev_priv->rps.rpe_delay = dev_priv->rps.rp1_delay;
Daniel Vetterc6a828d2012-08-08 23:35:35 +02003318 dev_priv->rps.cur_delay = 0;
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01003319
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003320 /* disable the counters and set deterministic thresholds */
3321 I915_WRITE(GEN6_RC_CONTROL, 0);
3322
3323 I915_WRITE(GEN6_RC1_WAKE_RATE_LIMIT, 1000 << 16);
3324 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16 | 30);
3325 I915_WRITE(GEN6_RC6pp_WAKE_RATE_LIMIT, 30);
3326 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
3327 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
3328
Chris Wilsonb4519512012-05-11 14:29:30 +01003329 for_each_ring(ring, dev_priv, i)
3330 I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003331
3332 I915_WRITE(GEN6_RC_SLEEP, 0);
3333 I915_WRITE(GEN6_RC1e_THRESHOLD, 1000);
Daniel Vetter29c78f62013-11-16 16:04:26 +01003334 if (IS_IVYBRIDGE(dev))
Stéphane Marchesin351aa562013-08-13 11:55:17 -07003335 I915_WRITE(GEN6_RC6_THRESHOLD, 125000);
3336 else
3337 I915_WRITE(GEN6_RC6_THRESHOLD, 50000);
Stéphane Marchesin0920a482013-01-29 19:41:59 -08003338 I915_WRITE(GEN6_RC6p_THRESHOLD, 150000);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003339 I915_WRITE(GEN6_RC6pp_THRESHOLD, 64000); /* unused */
3340
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03003341 /* Check if we are enabling RC6 */
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003342 rc6_mode = intel_enable_rc6(dev_priv->dev);
3343 if (rc6_mode & INTEL_RC6_ENABLE)
3344 rc6_mask |= GEN6_RC_CTL_RC6_ENABLE;
3345
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03003346 /* We don't use those on Haswell */
3347 if (!IS_HASWELL(dev)) {
3348 if (rc6_mode & INTEL_RC6p_ENABLE)
3349 rc6_mask |= GEN6_RC_CTL_RC6p_ENABLE;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003350
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03003351 if (rc6_mode & INTEL_RC6pp_ENABLE)
3352 rc6_mask |= GEN6_RC_CTL_RC6pp_ENABLE;
3353 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003354
Ben Widawskydc39fff2013-10-18 12:32:07 -07003355 intel_print_rc6_info(dev, rc6_mask);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003356
3357 I915_WRITE(GEN6_RC_CONTROL,
3358 rc6_mask |
3359 GEN6_RC_CTL_EI_MODE(1) |
3360 GEN6_RC_CTL_HW_ENABLE);
3361
Chris Wilsondd75fdc2013-09-25 17:34:57 +01003362 /* Power down if completely idle for over 50ms */
3363 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 50000);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003364 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003365
Ben Widawsky42c05262012-09-26 10:34:00 -07003366 ret = sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_MIN_FREQ_TABLE, 0);
Ben Widawsky988b36e2013-04-23 17:33:02 -07003367 if (!ret) {
Ben Widawsky42c05262012-09-26 10:34:00 -07003368 pcu_mbox = 0;
3369 ret = sandybridge_pcode_read(dev_priv, GEN6_READ_OC_PARAMS, &pcu_mbox);
Ben Widawskya2b3fc02013-03-19 20:19:56 -07003370 if (!ret && (pcu_mbox & (1<<31))) { /* OC supported */
Ben Widawsky10e08492013-04-05 14:29:23 -07003371 DRM_DEBUG_DRIVER("Overclocking supported. Max: %dMHz, Overclock max: %dMHz\n",
Ben Widawskya2b3fc02013-03-19 20:19:56 -07003372 (dev_priv->rps.max_delay & 0xff) * 50,
3373 (pcu_mbox & 0xff) * 50);
Ben Widawsky31c77382013-04-05 14:29:22 -07003374 dev_priv->rps.hw_max = pcu_mbox & 0xff;
Ben Widawsky42c05262012-09-26 10:34:00 -07003375 }
3376 } else {
3377 DRM_DEBUG_DRIVER("Failed to set the min frequency\n");
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003378 }
3379
Chris Wilsondd75fdc2013-09-25 17:34:57 +01003380 dev_priv->rps.power = HIGH_POWER; /* force a reset */
3381 gen6_set_rps(dev_priv->dev, dev_priv->rps.min_delay);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003382
Daniel Vetter44fc7d52013-07-12 22:43:27 +02003383 gen6_enable_rps_interrupts(dev);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003384
Ben Widawsky31643d52012-09-26 10:34:01 -07003385 rc6vids = 0;
3386 ret = sandybridge_pcode_read(dev_priv, GEN6_PCODE_READ_RC6VIDS, &rc6vids);
3387 if (IS_GEN6(dev) && ret) {
3388 DRM_DEBUG_DRIVER("Couldn't check for BIOS workaround\n");
3389 } else if (IS_GEN6(dev) && (GEN6_DECODE_RC6_VID(rc6vids & 0xff) < 450)) {
3390 DRM_DEBUG_DRIVER("You should update your BIOS. Correcting minimum rc6 voltage (%dmV->%dmV)\n",
3391 GEN6_DECODE_RC6_VID(rc6vids & 0xff), 450);
3392 rc6vids &= 0xffff00;
3393 rc6vids |= GEN6_ENCODE_RC6_VID(450);
3394 ret = sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_RC6VIDS, rc6vids);
3395 if (ret)
3396 DRM_ERROR("Couldn't fix incorrect rc6 voltage\n");
3397 }
3398
Deepak Sc8d9a592013-11-23 14:55:42 +05303399 gen6_gt_force_wake_put(dev_priv, FORCEWAKE_ALL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003400}
3401
Paulo Zanonic67a4702013-08-19 13:18:09 -03003402void gen6_update_ring_freq(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003403{
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02003404 struct drm_i915_private *dev_priv = dev->dev_private;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003405 int min_freq = 15;
Chris Wilson3ebecd02013-04-12 19:10:13 +01003406 unsigned int gpu_freq;
3407 unsigned int max_ia_freq, min_ring_freq;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003408 int scaling_factor = 180;
Ben Widawskyeda79642013-10-07 17:15:48 -03003409 struct cpufreq_policy *policy;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003410
Jesse Barnes4fc688c2012-11-02 11:14:01 -07003411 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02003412
Ben Widawskyeda79642013-10-07 17:15:48 -03003413 policy = cpufreq_cpu_get(0);
3414 if (policy) {
3415 max_ia_freq = policy->cpuinfo.max_freq;
3416 cpufreq_cpu_put(policy);
3417 } else {
3418 /*
3419 * Default to measured freq if none found, PCU will ensure we
3420 * don't go over
3421 */
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003422 max_ia_freq = tsc_khz;
Ben Widawskyeda79642013-10-07 17:15:48 -03003423 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003424
3425 /* Convert from kHz to MHz */
3426 max_ia_freq /= 1000;
3427
Ben Widawsky153b4b952013-10-22 22:05:09 -07003428 min_ring_freq = I915_READ(DCLK) & 0xf;
Ben Widawskyf6aca452013-10-02 09:25:02 -07003429 /* convert DDR frequency from units of 266.6MHz to bandwidth */
3430 min_ring_freq = mult_frac(min_ring_freq, 8, 3);
Chris Wilson3ebecd02013-04-12 19:10:13 +01003431
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003432 /*
3433 * For each potential GPU frequency, load a ring frequency we'd like
3434 * to use for memory access. We do this by specifying the IA frequency
3435 * the PCU should use as a reference to determine the ring frequency.
3436 */
Daniel Vetterc6a828d2012-08-08 23:35:35 +02003437 for (gpu_freq = dev_priv->rps.max_delay; gpu_freq >= dev_priv->rps.min_delay;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003438 gpu_freq--) {
Daniel Vetterc6a828d2012-08-08 23:35:35 +02003439 int diff = dev_priv->rps.max_delay - gpu_freq;
Chris Wilson3ebecd02013-04-12 19:10:13 +01003440 unsigned int ia_freq = 0, ring_freq = 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003441
Ben Widawsky46c764d2013-11-02 21:07:49 -07003442 if (INTEL_INFO(dev)->gen >= 8) {
3443 /* max(2 * GT, DDR). NB: GT is 50MHz units */
3444 ring_freq = max(min_ring_freq, gpu_freq);
3445 } else if (IS_HASWELL(dev)) {
Ben Widawskyf6aca452013-10-02 09:25:02 -07003446 ring_freq = mult_frac(gpu_freq, 5, 4);
Chris Wilson3ebecd02013-04-12 19:10:13 +01003447 ring_freq = max(min_ring_freq, ring_freq);
3448 /* leave ia_freq as the default, chosen by cpufreq */
3449 } else {
3450 /* On older processors, there is no separate ring
3451 * clock domain, so in order to boost the bandwidth
3452 * of the ring, we need to upclock the CPU (ia_freq).
3453 *
3454 * For GPU frequencies less than 750MHz,
3455 * just use the lowest ring freq.
3456 */
3457 if (gpu_freq < min_freq)
3458 ia_freq = 800;
3459 else
3460 ia_freq = max_ia_freq - ((diff * scaling_factor) / 2);
3461 ia_freq = DIV_ROUND_CLOSEST(ia_freq, 100);
3462 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003463
Ben Widawsky42c05262012-09-26 10:34:00 -07003464 sandybridge_pcode_write(dev_priv,
3465 GEN6_PCODE_WRITE_MIN_FREQ_TABLE,
Chris Wilson3ebecd02013-04-12 19:10:13 +01003466 ia_freq << GEN6_PCODE_FREQ_IA_RATIO_SHIFT |
3467 ring_freq << GEN6_PCODE_FREQ_RING_RATIO_SHIFT |
3468 gpu_freq);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003469 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003470}
3471
Jesse Barnes0a073b82013-04-17 15:54:58 -07003472int valleyview_rps_max_freq(struct drm_i915_private *dev_priv)
3473{
3474 u32 val, rp0;
3475
Jani Nikula64936252013-05-22 15:36:20 +03003476 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FREQ_FUSE);
Jesse Barnes0a073b82013-04-17 15:54:58 -07003477
3478 rp0 = (val & FB_GFX_MAX_FREQ_FUSE_MASK) >> FB_GFX_MAX_FREQ_FUSE_SHIFT;
3479 /* Clamp to max */
3480 rp0 = min_t(u32, rp0, 0xea);
3481
3482 return rp0;
3483}
3484
3485static int valleyview_rps_rpe_freq(struct drm_i915_private *dev_priv)
3486{
3487 u32 val, rpe;
3488
Jani Nikula64936252013-05-22 15:36:20 +03003489 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FMAX_FUSE_LO);
Jesse Barnes0a073b82013-04-17 15:54:58 -07003490 rpe = (val & FB_FMAX_VMIN_FREQ_LO_MASK) >> FB_FMAX_VMIN_FREQ_LO_SHIFT;
Jani Nikula64936252013-05-22 15:36:20 +03003491 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FMAX_FUSE_HI);
Jesse Barnes0a073b82013-04-17 15:54:58 -07003492 rpe |= (val & FB_FMAX_VMIN_FREQ_HI_MASK) << 5;
3493
3494 return rpe;
3495}
3496
3497int valleyview_rps_min_freq(struct drm_i915_private *dev_priv)
3498{
Jani Nikula64936252013-05-22 15:36:20 +03003499 return vlv_punit_read(dev_priv, PUNIT_REG_GPU_LFM) & 0xff;
Jesse Barnes0a073b82013-04-17 15:54:58 -07003500}
3501
Jesse Barnesc9cddff2013-05-08 10:45:13 -07003502static void valleyview_setup_pctx(struct drm_device *dev)
3503{
3504 struct drm_i915_private *dev_priv = dev->dev_private;
3505 struct drm_i915_gem_object *pctx;
3506 unsigned long pctx_paddr;
3507 u32 pcbr;
3508 int pctx_size = 24*1024;
3509
3510 pcbr = I915_READ(VLV_PCBR);
3511 if (pcbr) {
3512 /* BIOS set it up already, grab the pre-alloc'd space */
3513 int pcbr_offset;
3514
3515 pcbr_offset = (pcbr & (~4095)) - dev_priv->mm.stolen_base;
3516 pctx = i915_gem_object_create_stolen_for_preallocated(dev_priv->dev,
3517 pcbr_offset,
Daniel Vetter190d6cd2013-07-04 13:06:28 +02003518 I915_GTT_OFFSET_NONE,
Jesse Barnesc9cddff2013-05-08 10:45:13 -07003519 pctx_size);
3520 goto out;
3521 }
3522
3523 /*
3524 * From the Gunit register HAS:
3525 * The Gfx driver is expected to program this register and ensure
3526 * proper allocation within Gfx stolen memory. For example, this
3527 * register should be programmed such than the PCBR range does not
3528 * overlap with other ranges, such as the frame buffer, protected
3529 * memory, or any other relevant ranges.
3530 */
3531 pctx = i915_gem_object_create_stolen(dev, pctx_size);
3532 if (!pctx) {
3533 DRM_DEBUG("not enough stolen space for PCTX, disabling\n");
3534 return;
3535 }
3536
3537 pctx_paddr = dev_priv->mm.stolen_base + pctx->stolen->start;
3538 I915_WRITE(VLV_PCBR, pctx_paddr);
3539
3540out:
3541 dev_priv->vlv_pctx = pctx;
3542}
3543
Jesse Barnes0a073b82013-04-17 15:54:58 -07003544static void valleyview_enable_rps(struct drm_device *dev)
3545{
3546 struct drm_i915_private *dev_priv = dev->dev_private;
3547 struct intel_ring_buffer *ring;
Jesse Barnesa2b23fe2013-09-19 09:33:13 -07003548 u32 gtfifodbg, val, rc6_mode = 0;
Jesse Barnes0a073b82013-04-17 15:54:58 -07003549 int i;
3550
3551 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
3552
3553 if ((gtfifodbg = I915_READ(GTFIFODBG))) {
Jesse Barnesf7d85c12013-09-27 10:40:54 -07003554 DRM_DEBUG_DRIVER("GT fifo had a previous error %x\n",
3555 gtfifodbg);
Jesse Barnes0a073b82013-04-17 15:54:58 -07003556 I915_WRITE(GTFIFODBG, gtfifodbg);
3557 }
3558
Jesse Barnesc9cddff2013-05-08 10:45:13 -07003559 valleyview_setup_pctx(dev);
3560
Deepak Sc8d9a592013-11-23 14:55:42 +05303561 /* If VLV, Forcewake all wells, else re-direct to regular path */
3562 gen6_gt_force_wake_get(dev_priv, FORCEWAKE_ALL);
Jesse Barnes0a073b82013-04-17 15:54:58 -07003563
3564 I915_WRITE(GEN6_RP_UP_THRESHOLD, 59400);
3565 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 245000);
3566 I915_WRITE(GEN6_RP_UP_EI, 66000);
3567 I915_WRITE(GEN6_RP_DOWN_EI, 350000);
3568
3569 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
3570
3571 I915_WRITE(GEN6_RP_CONTROL,
3572 GEN6_RP_MEDIA_TURBO |
3573 GEN6_RP_MEDIA_HW_NORMAL_MODE |
3574 GEN6_RP_MEDIA_IS_GFX |
3575 GEN6_RP_ENABLE |
3576 GEN6_RP_UP_BUSY_AVG |
3577 GEN6_RP_DOWN_IDLE_CONT);
3578
3579 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 0x00280000);
3580 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
3581 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
3582
3583 for_each_ring(ring, dev_priv, i)
3584 I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
3585
Jesse Barnes2f0aa3042013-11-15 09:32:11 -08003586 I915_WRITE(GEN6_RC6_THRESHOLD, 0x557);
Jesse Barnes0a073b82013-04-17 15:54:58 -07003587
3588 /* allows RC6 residency counter to work */
Jesse Barnes49798eb2013-09-26 17:55:57 -07003589 I915_WRITE(VLV_COUNTER_CONTROL,
3590 _MASKED_BIT_ENABLE(VLV_COUNT_RANGE_HIGH |
3591 VLV_MEDIA_RC6_COUNT_EN |
3592 VLV_RENDER_RC6_COUNT_EN));
Jesse Barnesa2b23fe2013-09-19 09:33:13 -07003593 if (intel_enable_rc6(dev) & INTEL_RC6_ENABLE)
Jesse Barnes6b88f292013-11-15 09:32:12 -08003594 rc6_mode = GEN7_RC_CTL_TO_MODE | VLV_RC_CTL_CTX_RST_PARALLEL;
Ben Widawskydc39fff2013-10-18 12:32:07 -07003595
3596 intel_print_rc6_info(dev, rc6_mode);
3597
Jesse Barnesa2b23fe2013-09-19 09:33:13 -07003598 I915_WRITE(GEN6_RC_CONTROL, rc6_mode);
Jesse Barnes0a073b82013-04-17 15:54:58 -07003599
Jani Nikula64936252013-05-22 15:36:20 +03003600 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
Jesse Barnes0a073b82013-04-17 15:54:58 -07003601
3602 DRM_DEBUG_DRIVER("GPLL enabled? %s\n", val & 0x10 ? "yes" : "no");
3603 DRM_DEBUG_DRIVER("GPU status: 0x%08x\n", val);
3604
Jesse Barnes0a073b82013-04-17 15:54:58 -07003605 dev_priv->rps.cur_delay = (val >> 8) & 0xff;
Ville Syrjälä73008b92013-06-25 19:21:01 +03003606 DRM_DEBUG_DRIVER("current GPU freq: %d MHz (%u)\n",
Ville Syrjälä2ec38152013-11-05 22:42:29 +02003607 vlv_gpu_freq(dev_priv, dev_priv->rps.cur_delay),
Ville Syrjälä73008b92013-06-25 19:21:01 +03003608 dev_priv->rps.cur_delay);
Jesse Barnes0a073b82013-04-17 15:54:58 -07003609
3610 dev_priv->rps.max_delay = valleyview_rps_max_freq(dev_priv);
3611 dev_priv->rps.hw_max = dev_priv->rps.max_delay;
Ville Syrjälä73008b92013-06-25 19:21:01 +03003612 DRM_DEBUG_DRIVER("max GPU freq: %d MHz (%u)\n",
Ville Syrjälä2ec38152013-11-05 22:42:29 +02003613 vlv_gpu_freq(dev_priv, dev_priv->rps.max_delay),
Ville Syrjälä73008b92013-06-25 19:21:01 +03003614 dev_priv->rps.max_delay);
Jesse Barnes0a073b82013-04-17 15:54:58 -07003615
Ville Syrjälä73008b92013-06-25 19:21:01 +03003616 dev_priv->rps.rpe_delay = valleyview_rps_rpe_freq(dev_priv);
3617 DRM_DEBUG_DRIVER("RPe GPU freq: %d MHz (%u)\n",
Ville Syrjälä2ec38152013-11-05 22:42:29 +02003618 vlv_gpu_freq(dev_priv, dev_priv->rps.rpe_delay),
Ville Syrjälä73008b92013-06-25 19:21:01 +03003619 dev_priv->rps.rpe_delay);
Jesse Barnes0a073b82013-04-17 15:54:58 -07003620
Ville Syrjälä73008b92013-06-25 19:21:01 +03003621 dev_priv->rps.min_delay = valleyview_rps_min_freq(dev_priv);
3622 DRM_DEBUG_DRIVER("min GPU freq: %d MHz (%u)\n",
Ville Syrjälä2ec38152013-11-05 22:42:29 +02003623 vlv_gpu_freq(dev_priv, dev_priv->rps.min_delay),
Ville Syrjälä73008b92013-06-25 19:21:01 +03003624 dev_priv->rps.min_delay);
Jesse Barnes0a073b82013-04-17 15:54:58 -07003625
Ville Syrjälä73008b92013-06-25 19:21:01 +03003626 DRM_DEBUG_DRIVER("setting GPU freq to %d MHz (%u)\n",
Ville Syrjälä2ec38152013-11-05 22:42:29 +02003627 vlv_gpu_freq(dev_priv, dev_priv->rps.rpe_delay),
Ville Syrjälä73008b92013-06-25 19:21:01 +03003628 dev_priv->rps.rpe_delay);
Jesse Barnes0a073b82013-04-17 15:54:58 -07003629
Ville Syrjälä73008b92013-06-25 19:21:01 +03003630 valleyview_set_rps(dev_priv->dev, dev_priv->rps.rpe_delay);
Jesse Barnes0a073b82013-04-17 15:54:58 -07003631
Daniel Vetter44fc7d52013-07-12 22:43:27 +02003632 gen6_enable_rps_interrupts(dev);
Jesse Barnes0a073b82013-04-17 15:54:58 -07003633
Deepak Sc8d9a592013-11-23 14:55:42 +05303634 gen6_gt_force_wake_put(dev_priv, FORCEWAKE_ALL);
Jesse Barnes0a073b82013-04-17 15:54:58 -07003635}
3636
Daniel Vetter930ebb42012-06-29 23:32:16 +02003637void ironlake_teardown_rc6(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003638{
3639 struct drm_i915_private *dev_priv = dev->dev_private;
3640
Daniel Vetter3e373942012-11-02 19:55:04 +01003641 if (dev_priv->ips.renderctx) {
3642 i915_gem_object_unpin(dev_priv->ips.renderctx);
3643 drm_gem_object_unreference(&dev_priv->ips.renderctx->base);
3644 dev_priv->ips.renderctx = NULL;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003645 }
3646
Daniel Vetter3e373942012-11-02 19:55:04 +01003647 if (dev_priv->ips.pwrctx) {
3648 i915_gem_object_unpin(dev_priv->ips.pwrctx);
3649 drm_gem_object_unreference(&dev_priv->ips.pwrctx->base);
3650 dev_priv->ips.pwrctx = NULL;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003651 }
3652}
3653
Daniel Vetter930ebb42012-06-29 23:32:16 +02003654static void ironlake_disable_rc6(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003655{
3656 struct drm_i915_private *dev_priv = dev->dev_private;
3657
3658 if (I915_READ(PWRCTXA)) {
3659 /* Wake the GPU, prevent RC6, then restore RSTDBYCTL */
3660 I915_WRITE(RSTDBYCTL, I915_READ(RSTDBYCTL) | RCX_SW_EXIT);
3661 wait_for(((I915_READ(RSTDBYCTL) & RSX_STATUS_MASK) == RSX_STATUS_ON),
3662 50);
3663
3664 I915_WRITE(PWRCTXA, 0);
3665 POSTING_READ(PWRCTXA);
3666
3667 I915_WRITE(RSTDBYCTL, I915_READ(RSTDBYCTL) & ~RCX_SW_EXIT);
3668 POSTING_READ(RSTDBYCTL);
3669 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003670}
3671
3672static int ironlake_setup_rc6(struct drm_device *dev)
3673{
3674 struct drm_i915_private *dev_priv = dev->dev_private;
3675
Daniel Vetter3e373942012-11-02 19:55:04 +01003676 if (dev_priv->ips.renderctx == NULL)
3677 dev_priv->ips.renderctx = intel_alloc_context_page(dev);
3678 if (!dev_priv->ips.renderctx)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003679 return -ENOMEM;
3680
Daniel Vetter3e373942012-11-02 19:55:04 +01003681 if (dev_priv->ips.pwrctx == NULL)
3682 dev_priv->ips.pwrctx = intel_alloc_context_page(dev);
3683 if (!dev_priv->ips.pwrctx) {
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003684 ironlake_teardown_rc6(dev);
3685 return -ENOMEM;
3686 }
3687
3688 return 0;
3689}
3690
Daniel Vetter930ebb42012-06-29 23:32:16 +02003691static void ironlake_enable_rc6(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003692{
3693 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter6d90c952012-04-26 23:28:05 +02003694 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Chris Wilson3e960502012-11-27 16:22:54 +00003695 bool was_interruptible;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003696 int ret;
3697
3698 /* rc6 disabled by default due to repeated reports of hanging during
3699 * boot and resume.
3700 */
3701 if (!intel_enable_rc6(dev))
3702 return;
3703
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02003704 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
3705
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003706 ret = ironlake_setup_rc6(dev);
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02003707 if (ret)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003708 return;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003709
Chris Wilson3e960502012-11-27 16:22:54 +00003710 was_interruptible = dev_priv->mm.interruptible;
3711 dev_priv->mm.interruptible = false;
3712
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003713 /*
3714 * GPU can automatically power down the render unit if given a page
3715 * to save state.
3716 */
Daniel Vetter6d90c952012-04-26 23:28:05 +02003717 ret = intel_ring_begin(ring, 6);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003718 if (ret) {
3719 ironlake_teardown_rc6(dev);
Chris Wilson3e960502012-11-27 16:22:54 +00003720 dev_priv->mm.interruptible = was_interruptible;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003721 return;
3722 }
3723
Daniel Vetter6d90c952012-04-26 23:28:05 +02003724 intel_ring_emit(ring, MI_SUSPEND_FLUSH | MI_SUSPEND_FLUSH_EN);
3725 intel_ring_emit(ring, MI_SET_CONTEXT);
Ben Widawskyf343c5f2013-07-05 14:41:04 -07003726 intel_ring_emit(ring, i915_gem_obj_ggtt_offset(dev_priv->ips.renderctx) |
Daniel Vetter6d90c952012-04-26 23:28:05 +02003727 MI_MM_SPACE_GTT |
3728 MI_SAVE_EXT_STATE_EN |
3729 MI_RESTORE_EXT_STATE_EN |
3730 MI_RESTORE_INHIBIT);
3731 intel_ring_emit(ring, MI_SUSPEND_FLUSH);
3732 intel_ring_emit(ring, MI_NOOP);
3733 intel_ring_emit(ring, MI_FLUSH);
3734 intel_ring_advance(ring);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003735
3736 /*
3737 * Wait for the command parser to advance past MI_SET_CONTEXT. The HW
3738 * does an implicit flush, combined with MI_FLUSH above, it should be
3739 * safe to assume that renderctx is valid
3740 */
Chris Wilson3e960502012-11-27 16:22:54 +00003741 ret = intel_ring_idle(ring);
3742 dev_priv->mm.interruptible = was_interruptible;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003743 if (ret) {
Jani Nikuladef27a52013-03-12 10:49:19 +02003744 DRM_ERROR("failed to enable ironlake power savings\n");
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003745 ironlake_teardown_rc6(dev);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003746 return;
3747 }
3748
Ben Widawskyf343c5f2013-07-05 14:41:04 -07003749 I915_WRITE(PWRCTXA, i915_gem_obj_ggtt_offset(dev_priv->ips.pwrctx) | PWRCTX_EN);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003750 I915_WRITE(RSTDBYCTL, I915_READ(RSTDBYCTL) & ~RCX_SW_EXIT);
Ben Widawskydc39fff2013-10-18 12:32:07 -07003751
3752 intel_print_rc6_info(dev, INTEL_RC6_ENABLE);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003753}
3754
Eugeni Dodonovdde18882012-04-18 15:29:24 -03003755static unsigned long intel_pxfreq(u32 vidfreq)
3756{
3757 unsigned long freq;
3758 int div = (vidfreq & 0x3f0000) >> 16;
3759 int post = (vidfreq & 0x3000) >> 12;
3760 int pre = (vidfreq & 0x7);
3761
3762 if (!pre)
3763 return 0;
3764
3765 freq = ((div * 133333) / ((1<<post) * pre));
3766
3767 return freq;
3768}
3769
Daniel Vettereb48eb02012-04-26 23:28:12 +02003770static const struct cparams {
3771 u16 i;
3772 u16 t;
3773 u16 m;
3774 u16 c;
3775} cparams[] = {
3776 { 1, 1333, 301, 28664 },
3777 { 1, 1066, 294, 24460 },
3778 { 1, 800, 294, 25192 },
3779 { 0, 1333, 276, 27605 },
3780 { 0, 1066, 276, 27605 },
3781 { 0, 800, 231, 23784 },
3782};
3783
Chris Wilsonf531dcb22012-09-25 10:16:12 +01003784static unsigned long __i915_chipset_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02003785{
3786 u64 total_count, diff, ret;
3787 u32 count1, count2, count3, m = 0, c = 0;
3788 unsigned long now = jiffies_to_msecs(jiffies), diff1;
3789 int i;
3790
Daniel Vetter02d71952012-08-09 16:44:54 +02003791 assert_spin_locked(&mchdev_lock);
3792
Daniel Vetter20e4d402012-08-08 23:35:39 +02003793 diff1 = now - dev_priv->ips.last_time1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02003794
3795 /* Prevent division-by-zero if we are asking too fast.
3796 * Also, we don't get interesting results if we are polling
3797 * faster than once in 10ms, so just return the saved value
3798 * in such cases.
3799 */
3800 if (diff1 <= 10)
Daniel Vetter20e4d402012-08-08 23:35:39 +02003801 return dev_priv->ips.chipset_power;
Daniel Vettereb48eb02012-04-26 23:28:12 +02003802
3803 count1 = I915_READ(DMIEC);
3804 count2 = I915_READ(DDREC);
3805 count3 = I915_READ(CSIEC);
3806
3807 total_count = count1 + count2 + count3;
3808
3809 /* FIXME: handle per-counter overflow */
Daniel Vetter20e4d402012-08-08 23:35:39 +02003810 if (total_count < dev_priv->ips.last_count1) {
3811 diff = ~0UL - dev_priv->ips.last_count1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02003812 diff += total_count;
3813 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +02003814 diff = total_count - dev_priv->ips.last_count1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02003815 }
3816
3817 for (i = 0; i < ARRAY_SIZE(cparams); i++) {
Daniel Vetter20e4d402012-08-08 23:35:39 +02003818 if (cparams[i].i == dev_priv->ips.c_m &&
3819 cparams[i].t == dev_priv->ips.r_t) {
Daniel Vettereb48eb02012-04-26 23:28:12 +02003820 m = cparams[i].m;
3821 c = cparams[i].c;
3822 break;
3823 }
3824 }
3825
3826 diff = div_u64(diff, diff1);
3827 ret = ((m * diff) + c);
3828 ret = div_u64(ret, 10);
3829
Daniel Vetter20e4d402012-08-08 23:35:39 +02003830 dev_priv->ips.last_count1 = total_count;
3831 dev_priv->ips.last_time1 = now;
Daniel Vettereb48eb02012-04-26 23:28:12 +02003832
Daniel Vetter20e4d402012-08-08 23:35:39 +02003833 dev_priv->ips.chipset_power = ret;
Daniel Vettereb48eb02012-04-26 23:28:12 +02003834
3835 return ret;
3836}
3837
Chris Wilsonf531dcb22012-09-25 10:16:12 +01003838unsigned long i915_chipset_val(struct drm_i915_private *dev_priv)
3839{
3840 unsigned long val;
3841
3842 if (dev_priv->info->gen != 5)
3843 return 0;
3844
3845 spin_lock_irq(&mchdev_lock);
3846
3847 val = __i915_chipset_val(dev_priv);
3848
3849 spin_unlock_irq(&mchdev_lock);
3850
3851 return val;
3852}
3853
Daniel Vettereb48eb02012-04-26 23:28:12 +02003854unsigned long i915_mch_val(struct drm_i915_private *dev_priv)
3855{
3856 unsigned long m, x, b;
3857 u32 tsfs;
3858
3859 tsfs = I915_READ(TSFS);
3860
3861 m = ((tsfs & TSFS_SLOPE_MASK) >> TSFS_SLOPE_SHIFT);
3862 x = I915_READ8(TR1);
3863
3864 b = tsfs & TSFS_INTR_MASK;
3865
3866 return ((m * x) / 127) - b;
3867}
3868
3869static u16 pvid_to_extvid(struct drm_i915_private *dev_priv, u8 pxvid)
3870{
3871 static const struct v_table {
3872 u16 vd; /* in .1 mil */
3873 u16 vm; /* in .1 mil */
3874 } v_table[] = {
3875 { 0, 0, },
3876 { 375, 0, },
3877 { 500, 0, },
3878 { 625, 0, },
3879 { 750, 0, },
3880 { 875, 0, },
3881 { 1000, 0, },
3882 { 1125, 0, },
3883 { 4125, 3000, },
3884 { 4125, 3000, },
3885 { 4125, 3000, },
3886 { 4125, 3000, },
3887 { 4125, 3000, },
3888 { 4125, 3000, },
3889 { 4125, 3000, },
3890 { 4125, 3000, },
3891 { 4125, 3000, },
3892 { 4125, 3000, },
3893 { 4125, 3000, },
3894 { 4125, 3000, },
3895 { 4125, 3000, },
3896 { 4125, 3000, },
3897 { 4125, 3000, },
3898 { 4125, 3000, },
3899 { 4125, 3000, },
3900 { 4125, 3000, },
3901 { 4125, 3000, },
3902 { 4125, 3000, },
3903 { 4125, 3000, },
3904 { 4125, 3000, },
3905 { 4125, 3000, },
3906 { 4125, 3000, },
3907 { 4250, 3125, },
3908 { 4375, 3250, },
3909 { 4500, 3375, },
3910 { 4625, 3500, },
3911 { 4750, 3625, },
3912 { 4875, 3750, },
3913 { 5000, 3875, },
3914 { 5125, 4000, },
3915 { 5250, 4125, },
3916 { 5375, 4250, },
3917 { 5500, 4375, },
3918 { 5625, 4500, },
3919 { 5750, 4625, },
3920 { 5875, 4750, },
3921 { 6000, 4875, },
3922 { 6125, 5000, },
3923 { 6250, 5125, },
3924 { 6375, 5250, },
3925 { 6500, 5375, },
3926 { 6625, 5500, },
3927 { 6750, 5625, },
3928 { 6875, 5750, },
3929 { 7000, 5875, },
3930 { 7125, 6000, },
3931 { 7250, 6125, },
3932 { 7375, 6250, },
3933 { 7500, 6375, },
3934 { 7625, 6500, },
3935 { 7750, 6625, },
3936 { 7875, 6750, },
3937 { 8000, 6875, },
3938 { 8125, 7000, },
3939 { 8250, 7125, },
3940 { 8375, 7250, },
3941 { 8500, 7375, },
3942 { 8625, 7500, },
3943 { 8750, 7625, },
3944 { 8875, 7750, },
3945 { 9000, 7875, },
3946 { 9125, 8000, },
3947 { 9250, 8125, },
3948 { 9375, 8250, },
3949 { 9500, 8375, },
3950 { 9625, 8500, },
3951 { 9750, 8625, },
3952 { 9875, 8750, },
3953 { 10000, 8875, },
3954 { 10125, 9000, },
3955 { 10250, 9125, },
3956 { 10375, 9250, },
3957 { 10500, 9375, },
3958 { 10625, 9500, },
3959 { 10750, 9625, },
3960 { 10875, 9750, },
3961 { 11000, 9875, },
3962 { 11125, 10000, },
3963 { 11250, 10125, },
3964 { 11375, 10250, },
3965 { 11500, 10375, },
3966 { 11625, 10500, },
3967 { 11750, 10625, },
3968 { 11875, 10750, },
3969 { 12000, 10875, },
3970 { 12125, 11000, },
3971 { 12250, 11125, },
3972 { 12375, 11250, },
3973 { 12500, 11375, },
3974 { 12625, 11500, },
3975 { 12750, 11625, },
3976 { 12875, 11750, },
3977 { 13000, 11875, },
3978 { 13125, 12000, },
3979 { 13250, 12125, },
3980 { 13375, 12250, },
3981 { 13500, 12375, },
3982 { 13625, 12500, },
3983 { 13750, 12625, },
3984 { 13875, 12750, },
3985 { 14000, 12875, },
3986 { 14125, 13000, },
3987 { 14250, 13125, },
3988 { 14375, 13250, },
3989 { 14500, 13375, },
3990 { 14625, 13500, },
3991 { 14750, 13625, },
3992 { 14875, 13750, },
3993 { 15000, 13875, },
3994 { 15125, 14000, },
3995 { 15250, 14125, },
3996 { 15375, 14250, },
3997 { 15500, 14375, },
3998 { 15625, 14500, },
3999 { 15750, 14625, },
4000 { 15875, 14750, },
4001 { 16000, 14875, },
4002 { 16125, 15000, },
4003 };
4004 if (dev_priv->info->is_mobile)
4005 return v_table[pxvid].vm;
4006 else
4007 return v_table[pxvid].vd;
4008}
4009
Daniel Vetter02d71952012-08-09 16:44:54 +02004010static void __i915_update_gfx_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02004011{
4012 struct timespec now, diff1;
4013 u64 diff;
4014 unsigned long diffms;
4015 u32 count;
4016
Daniel Vetter02d71952012-08-09 16:44:54 +02004017 assert_spin_locked(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004018
4019 getrawmonotonic(&now);
Daniel Vetter20e4d402012-08-08 23:35:39 +02004020 diff1 = timespec_sub(now, dev_priv->ips.last_time2);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004021
4022 /* Don't divide by 0 */
4023 diffms = diff1.tv_sec * 1000 + diff1.tv_nsec / 1000000;
4024 if (!diffms)
4025 return;
4026
4027 count = I915_READ(GFXEC);
4028
Daniel Vetter20e4d402012-08-08 23:35:39 +02004029 if (count < dev_priv->ips.last_count2) {
4030 diff = ~0UL - dev_priv->ips.last_count2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02004031 diff += count;
4032 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +02004033 diff = count - dev_priv->ips.last_count2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02004034 }
4035
Daniel Vetter20e4d402012-08-08 23:35:39 +02004036 dev_priv->ips.last_count2 = count;
4037 dev_priv->ips.last_time2 = now;
Daniel Vettereb48eb02012-04-26 23:28:12 +02004038
4039 /* More magic constants... */
4040 diff = diff * 1181;
4041 diff = div_u64(diff, diffms * 10);
Daniel Vetter20e4d402012-08-08 23:35:39 +02004042 dev_priv->ips.gfx_power = diff;
Daniel Vettereb48eb02012-04-26 23:28:12 +02004043}
4044
Daniel Vetter02d71952012-08-09 16:44:54 +02004045void i915_update_gfx_val(struct drm_i915_private *dev_priv)
4046{
4047 if (dev_priv->info->gen != 5)
4048 return;
4049
Daniel Vetter92703882012-08-09 16:46:01 +02004050 spin_lock_irq(&mchdev_lock);
Daniel Vetter02d71952012-08-09 16:44:54 +02004051
4052 __i915_update_gfx_val(dev_priv);
4053
Daniel Vetter92703882012-08-09 16:46:01 +02004054 spin_unlock_irq(&mchdev_lock);
Daniel Vetter02d71952012-08-09 16:44:54 +02004055}
4056
Chris Wilsonf531dcb22012-09-25 10:16:12 +01004057static unsigned long __i915_gfx_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02004058{
4059 unsigned long t, corr, state1, corr2, state2;
4060 u32 pxvid, ext_v;
4061
Daniel Vetter02d71952012-08-09 16:44:54 +02004062 assert_spin_locked(&mchdev_lock);
4063
Daniel Vetterc6a828d2012-08-08 23:35:35 +02004064 pxvid = I915_READ(PXVFREQ_BASE + (dev_priv->rps.cur_delay * 4));
Daniel Vettereb48eb02012-04-26 23:28:12 +02004065 pxvid = (pxvid >> 24) & 0x7f;
4066 ext_v = pvid_to_extvid(dev_priv, pxvid);
4067
4068 state1 = ext_v;
4069
4070 t = i915_mch_val(dev_priv);
4071
4072 /* Revel in the empirically derived constants */
4073
4074 /* Correction factor in 1/100000 units */
4075 if (t > 80)
4076 corr = ((t * 2349) + 135940);
4077 else if (t >= 50)
4078 corr = ((t * 964) + 29317);
4079 else /* < 50 */
4080 corr = ((t * 301) + 1004);
4081
4082 corr = corr * ((150142 * state1) / 10000 - 78642);
4083 corr /= 100000;
Daniel Vetter20e4d402012-08-08 23:35:39 +02004084 corr2 = (corr * dev_priv->ips.corr);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004085
4086 state2 = (corr2 * state1) / 10000;
4087 state2 /= 100; /* convert to mW */
4088
Daniel Vetter02d71952012-08-09 16:44:54 +02004089 __i915_update_gfx_val(dev_priv);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004090
Daniel Vetter20e4d402012-08-08 23:35:39 +02004091 return dev_priv->ips.gfx_power + state2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02004092}
4093
Chris Wilsonf531dcb22012-09-25 10:16:12 +01004094unsigned long i915_gfx_val(struct drm_i915_private *dev_priv)
4095{
4096 unsigned long val;
4097
4098 if (dev_priv->info->gen != 5)
4099 return 0;
4100
4101 spin_lock_irq(&mchdev_lock);
4102
4103 val = __i915_gfx_val(dev_priv);
4104
4105 spin_unlock_irq(&mchdev_lock);
4106
4107 return val;
4108}
4109
Daniel Vettereb48eb02012-04-26 23:28:12 +02004110/**
4111 * i915_read_mch_val - return value for IPS use
4112 *
4113 * Calculate and return a value for the IPS driver to use when deciding whether
4114 * we have thermal and power headroom to increase CPU or GPU power budget.
4115 */
4116unsigned long i915_read_mch_val(void)
4117{
4118 struct drm_i915_private *dev_priv;
4119 unsigned long chipset_val, graphics_val, ret = 0;
4120
Daniel Vetter92703882012-08-09 16:46:01 +02004121 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004122 if (!i915_mch_dev)
4123 goto out_unlock;
4124 dev_priv = i915_mch_dev;
4125
Chris Wilsonf531dcb22012-09-25 10:16:12 +01004126 chipset_val = __i915_chipset_val(dev_priv);
4127 graphics_val = __i915_gfx_val(dev_priv);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004128
4129 ret = chipset_val + graphics_val;
4130
4131out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02004132 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004133
4134 return ret;
4135}
4136EXPORT_SYMBOL_GPL(i915_read_mch_val);
4137
4138/**
4139 * i915_gpu_raise - raise GPU frequency limit
4140 *
4141 * Raise the limit; IPS indicates we have thermal headroom.
4142 */
4143bool i915_gpu_raise(void)
4144{
4145 struct drm_i915_private *dev_priv;
4146 bool ret = true;
4147
Daniel Vetter92703882012-08-09 16:46:01 +02004148 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004149 if (!i915_mch_dev) {
4150 ret = false;
4151 goto out_unlock;
4152 }
4153 dev_priv = i915_mch_dev;
4154
Daniel Vetter20e4d402012-08-08 23:35:39 +02004155 if (dev_priv->ips.max_delay > dev_priv->ips.fmax)
4156 dev_priv->ips.max_delay--;
Daniel Vettereb48eb02012-04-26 23:28:12 +02004157
4158out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02004159 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004160
4161 return ret;
4162}
4163EXPORT_SYMBOL_GPL(i915_gpu_raise);
4164
4165/**
4166 * i915_gpu_lower - lower GPU frequency limit
4167 *
4168 * IPS indicates we're close to a thermal limit, so throttle back the GPU
4169 * frequency maximum.
4170 */
4171bool i915_gpu_lower(void)
4172{
4173 struct drm_i915_private *dev_priv;
4174 bool ret = true;
4175
Daniel Vetter92703882012-08-09 16:46:01 +02004176 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004177 if (!i915_mch_dev) {
4178 ret = false;
4179 goto out_unlock;
4180 }
4181 dev_priv = i915_mch_dev;
4182
Daniel Vetter20e4d402012-08-08 23:35:39 +02004183 if (dev_priv->ips.max_delay < dev_priv->ips.min_delay)
4184 dev_priv->ips.max_delay++;
Daniel Vettereb48eb02012-04-26 23:28:12 +02004185
4186out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02004187 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004188
4189 return ret;
4190}
4191EXPORT_SYMBOL_GPL(i915_gpu_lower);
4192
4193/**
4194 * i915_gpu_busy - indicate GPU business to IPS
4195 *
4196 * Tell the IPS driver whether or not the GPU is busy.
4197 */
4198bool i915_gpu_busy(void)
4199{
4200 struct drm_i915_private *dev_priv;
Chris Wilsonf047e392012-07-21 12:31:41 +01004201 struct intel_ring_buffer *ring;
Daniel Vettereb48eb02012-04-26 23:28:12 +02004202 bool ret = false;
Chris Wilsonf047e392012-07-21 12:31:41 +01004203 int i;
Daniel Vettereb48eb02012-04-26 23:28:12 +02004204
Daniel Vetter92703882012-08-09 16:46:01 +02004205 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004206 if (!i915_mch_dev)
4207 goto out_unlock;
4208 dev_priv = i915_mch_dev;
4209
Chris Wilsonf047e392012-07-21 12:31:41 +01004210 for_each_ring(ring, dev_priv, i)
4211 ret |= !list_empty(&ring->request_list);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004212
4213out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02004214 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004215
4216 return ret;
4217}
4218EXPORT_SYMBOL_GPL(i915_gpu_busy);
4219
4220/**
4221 * i915_gpu_turbo_disable - disable graphics turbo
4222 *
4223 * Disable graphics turbo by resetting the max frequency and setting the
4224 * current frequency to the default.
4225 */
4226bool i915_gpu_turbo_disable(void)
4227{
4228 struct drm_i915_private *dev_priv;
4229 bool ret = true;
4230
Daniel Vetter92703882012-08-09 16:46:01 +02004231 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004232 if (!i915_mch_dev) {
4233 ret = false;
4234 goto out_unlock;
4235 }
4236 dev_priv = i915_mch_dev;
4237
Daniel Vetter20e4d402012-08-08 23:35:39 +02004238 dev_priv->ips.max_delay = dev_priv->ips.fstart;
Daniel Vettereb48eb02012-04-26 23:28:12 +02004239
Daniel Vetter20e4d402012-08-08 23:35:39 +02004240 if (!ironlake_set_drps(dev_priv->dev, dev_priv->ips.fstart))
Daniel Vettereb48eb02012-04-26 23:28:12 +02004241 ret = false;
4242
4243out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02004244 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004245
4246 return ret;
4247}
4248EXPORT_SYMBOL_GPL(i915_gpu_turbo_disable);
4249
4250/**
4251 * Tells the intel_ips driver that the i915 driver is now loaded, if
4252 * IPS got loaded first.
4253 *
4254 * This awkward dance is so that neither module has to depend on the
4255 * other in order for IPS to do the appropriate communication of
4256 * GPU turbo limits to i915.
4257 */
4258static void
4259ips_ping_for_i915_load(void)
4260{
4261 void (*link)(void);
4262
4263 link = symbol_get(ips_link_to_i915_driver);
4264 if (link) {
4265 link();
4266 symbol_put(ips_link_to_i915_driver);
4267 }
4268}
4269
4270void intel_gpu_ips_init(struct drm_i915_private *dev_priv)
4271{
Daniel Vetter02d71952012-08-09 16:44:54 +02004272 /* We only register the i915 ips part with intel-ips once everything is
4273 * set up, to avoid intel-ips sneaking in and reading bogus values. */
Daniel Vetter92703882012-08-09 16:46:01 +02004274 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004275 i915_mch_dev = dev_priv;
Daniel Vetter92703882012-08-09 16:46:01 +02004276 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004277
4278 ips_ping_for_i915_load();
4279}
4280
4281void intel_gpu_ips_teardown(void)
4282{
Daniel Vetter92703882012-08-09 16:46:01 +02004283 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004284 i915_mch_dev = NULL;
Daniel Vetter92703882012-08-09 16:46:01 +02004285 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02004286}
Daniel Vetter8090c6b2012-06-24 16:42:32 +02004287static void intel_init_emon(struct drm_device *dev)
Eugeni Dodonovdde18882012-04-18 15:29:24 -03004288{
4289 struct drm_i915_private *dev_priv = dev->dev_private;
4290 u32 lcfuse;
4291 u8 pxw[16];
4292 int i;
4293
4294 /* Disable to program */
4295 I915_WRITE(ECR, 0);
4296 POSTING_READ(ECR);
4297
4298 /* Program energy weights for various events */
4299 I915_WRITE(SDEW, 0x15040d00);
4300 I915_WRITE(CSIEW0, 0x007f0000);
4301 I915_WRITE(CSIEW1, 0x1e220004);
4302 I915_WRITE(CSIEW2, 0x04000004);
4303
4304 for (i = 0; i < 5; i++)
4305 I915_WRITE(PEW + (i * 4), 0);
4306 for (i = 0; i < 3; i++)
4307 I915_WRITE(DEW + (i * 4), 0);
4308
4309 /* Program P-state weights to account for frequency power adjustment */
4310 for (i = 0; i < 16; i++) {
4311 u32 pxvidfreq = I915_READ(PXVFREQ_BASE + (i * 4));
4312 unsigned long freq = intel_pxfreq(pxvidfreq);
4313 unsigned long vid = (pxvidfreq & PXVFREQ_PX_MASK) >>
4314 PXVFREQ_PX_SHIFT;
4315 unsigned long val;
4316
4317 val = vid * vid;
4318 val *= (freq / 1000);
4319 val *= 255;
4320 val /= (127*127*900);
4321 if (val > 0xff)
4322 DRM_ERROR("bad pxval: %ld\n", val);
4323 pxw[i] = val;
4324 }
4325 /* Render standby states get 0 weight */
4326 pxw[14] = 0;
4327 pxw[15] = 0;
4328
4329 for (i = 0; i < 4; i++) {
4330 u32 val = (pxw[i*4] << 24) | (pxw[(i*4)+1] << 16) |
4331 (pxw[(i*4)+2] << 8) | (pxw[(i*4)+3]);
4332 I915_WRITE(PXW + (i * 4), val);
4333 }
4334
4335 /* Adjust magic regs to magic values (more experimental results) */
4336 I915_WRITE(OGW0, 0);
4337 I915_WRITE(OGW1, 0);
4338 I915_WRITE(EG0, 0x00007f00);
4339 I915_WRITE(EG1, 0x0000000e);
4340 I915_WRITE(EG2, 0x000e0000);
4341 I915_WRITE(EG3, 0x68000300);
4342 I915_WRITE(EG4, 0x42000000);
4343 I915_WRITE(EG5, 0x00140031);
4344 I915_WRITE(EG6, 0);
4345 I915_WRITE(EG7, 0);
4346
4347 for (i = 0; i < 8; i++)
4348 I915_WRITE(PXWL + (i * 4), 0);
4349
4350 /* Enable PMON + select events */
4351 I915_WRITE(ECR, 0x80000019);
4352
4353 lcfuse = I915_READ(LCFUSE02);
4354
Daniel Vetter20e4d402012-08-08 23:35:39 +02004355 dev_priv->ips.corr = (lcfuse & LCFUSE_HIV_MASK);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03004356}
4357
Daniel Vetter8090c6b2012-06-24 16:42:32 +02004358void intel_disable_gt_powersave(struct drm_device *dev)
4359{
Jesse Barnes1a01ab32012-11-02 11:14:00 -07004360 struct drm_i915_private *dev_priv = dev->dev_private;
4361
Daniel Vetterfd0c0642013-04-24 11:13:35 +02004362 /* Interrupts should be disabled already to avoid re-arming. */
4363 WARN_ON(dev->irq_enabled);
4364
Daniel Vetter930ebb42012-06-29 23:32:16 +02004365 if (IS_IRONLAKE_M(dev)) {
Daniel Vetter8090c6b2012-06-24 16:42:32 +02004366 ironlake_disable_drps(dev);
Daniel Vetter930ebb42012-06-29 23:32:16 +02004367 ironlake_disable_rc6(dev);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004368 } else if (INTEL_INFO(dev)->gen >= 6) {
Jesse Barnes1a01ab32012-11-02 11:14:00 -07004369 cancel_delayed_work_sync(&dev_priv->rps.delayed_resume_work);
Jesse Barnes250848c2013-04-23 10:09:27 -07004370 cancel_work_sync(&dev_priv->rps.work);
Jesse Barnes4fc688c2012-11-02 11:14:01 -07004371 mutex_lock(&dev_priv->rps.hw_lock);
Jesse Barnesd20d4f02013-04-23 10:09:28 -07004372 if (IS_VALLEYVIEW(dev))
4373 valleyview_disable_rps(dev);
4374 else
4375 gen6_disable_rps(dev);
Chris Wilsonc0951f02013-10-10 21:58:50 +01004376 dev_priv->rps.enabled = false;
Jesse Barnes4fc688c2012-11-02 11:14:01 -07004377 mutex_unlock(&dev_priv->rps.hw_lock);
Daniel Vetter930ebb42012-06-29 23:32:16 +02004378 }
Daniel Vetter8090c6b2012-06-24 16:42:32 +02004379}
4380
Jesse Barnes1a01ab32012-11-02 11:14:00 -07004381static void intel_gen6_powersave_work(struct work_struct *work)
4382{
4383 struct drm_i915_private *dev_priv =
4384 container_of(work, struct drm_i915_private,
4385 rps.delayed_resume_work.work);
4386 struct drm_device *dev = dev_priv->dev;
4387
Jesse Barnes4fc688c2012-11-02 11:14:01 -07004388 mutex_lock(&dev_priv->rps.hw_lock);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004389
4390 if (IS_VALLEYVIEW(dev)) {
4391 valleyview_enable_rps(dev);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004392 } else if (IS_BROADWELL(dev)) {
4393 gen8_enable_rps(dev);
4394 gen6_update_ring_freq(dev);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004395 } else {
4396 gen6_enable_rps(dev);
4397 gen6_update_ring_freq(dev);
4398 }
Chris Wilsonc0951f02013-10-10 21:58:50 +01004399 dev_priv->rps.enabled = true;
Jesse Barnes4fc688c2012-11-02 11:14:01 -07004400 mutex_unlock(&dev_priv->rps.hw_lock);
Jesse Barnes1a01ab32012-11-02 11:14:00 -07004401}
4402
Daniel Vetter8090c6b2012-06-24 16:42:32 +02004403void intel_enable_gt_powersave(struct drm_device *dev)
4404{
Jesse Barnes1a01ab32012-11-02 11:14:00 -07004405 struct drm_i915_private *dev_priv = dev->dev_private;
4406
Daniel Vetter8090c6b2012-06-24 16:42:32 +02004407 if (IS_IRONLAKE_M(dev)) {
4408 ironlake_enable_drps(dev);
4409 ironlake_enable_rc6(dev);
4410 intel_init_emon(dev);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004411 } else if (IS_GEN6(dev) || IS_GEN7(dev)) {
Jesse Barnes1a01ab32012-11-02 11:14:00 -07004412 /*
4413 * PCU communication is slow and this doesn't need to be
4414 * done at any specific time, so do this out of our fast path
4415 * to make resume and init faster.
4416 */
4417 schedule_delayed_work(&dev_priv->rps.delayed_resume_work,
4418 round_jiffies_up_relative(HZ));
Daniel Vetter8090c6b2012-06-24 16:42:32 +02004419 }
4420}
4421
Daniel Vetter3107bd42012-10-31 22:52:31 +01004422static void ibx_init_clock_gating(struct drm_device *dev)
4423{
4424 struct drm_i915_private *dev_priv = dev->dev_private;
4425
4426 /*
4427 * On Ibex Peak and Cougar Point, we need to disable clock
4428 * gating for the panel power sequencer or it will fail to
4429 * start up when no ports are active.
4430 */
4431 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE);
4432}
4433
Ville Syrjälä0e088b82013-06-07 10:47:04 +03004434static void g4x_disable_trickle_feed(struct drm_device *dev)
4435{
4436 struct drm_i915_private *dev_priv = dev->dev_private;
4437 int pipe;
4438
4439 for_each_pipe(pipe) {
4440 I915_WRITE(DSPCNTR(pipe),
4441 I915_READ(DSPCNTR(pipe)) |
4442 DISPPLANE_TRICKLE_FEED_DISABLE);
Ville Syrjälä1dba99f2013-10-01 18:02:18 +03004443 intel_flush_primary_plane(dev_priv, pipe);
Ville Syrjälä0e088b82013-06-07 10:47:04 +03004444 }
4445}
4446
Ville Syrjälä017636c2013-12-05 15:51:37 +02004447static void ilk_init_lp_watermarks(struct drm_device *dev)
4448{
4449 struct drm_i915_private *dev_priv = dev->dev_private;
4450
4451 I915_WRITE(WM3_LP_ILK, I915_READ(WM3_LP_ILK) & ~WM1_LP_SR_EN);
4452 I915_WRITE(WM2_LP_ILK, I915_READ(WM2_LP_ILK) & ~WM1_LP_SR_EN);
4453 I915_WRITE(WM1_LP_ILK, I915_READ(WM1_LP_ILK) & ~WM1_LP_SR_EN);
4454
4455 /*
4456 * Don't touch WM1S_LP_EN here.
4457 * Doing so could cause underruns.
4458 */
4459}
4460
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03004461static void ironlake_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004462{
4463 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau231e54f2012-10-19 17:55:41 +01004464 uint32_t dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004465
Damien Lespiauf1e8fa52013-06-07 17:41:09 +01004466 /*
4467 * Required for FBC
4468 * WaFbcDisableDpfcClockGating:ilk
4469 */
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01004470 dspclk_gate |= ILK_DPFCRUNIT_CLOCK_GATE_DISABLE |
4471 ILK_DPFCUNIT_CLOCK_GATE_DISABLE |
4472 ILK_DPFDUNIT_CLOCK_GATE_ENABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004473
4474 I915_WRITE(PCH_3DCGDIS0,
4475 MARIUNIT_CLOCK_GATE_DISABLE |
4476 SVSMUNIT_CLOCK_GATE_DISABLE);
4477 I915_WRITE(PCH_3DCGDIS1,
4478 VFMUNIT_CLOCK_GATE_DISABLE);
4479
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004480 /*
4481 * According to the spec the following bits should be set in
4482 * order to enable memory self-refresh
4483 * The bit 22/21 of 0x42004
4484 * The bit 5 of 0x42020
4485 * The bit 15 of 0x45000
4486 */
4487 I915_WRITE(ILK_DISPLAY_CHICKEN2,
4488 (I915_READ(ILK_DISPLAY_CHICKEN2) |
4489 ILK_DPARB_GATE | ILK_VSDPFD_FULL));
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01004490 dspclk_gate |= ILK_DPARBUNIT_CLOCK_GATE_ENABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004491 I915_WRITE(DISP_ARB_CTL,
4492 (I915_READ(DISP_ARB_CTL) |
4493 DISP_FBC_WM_DIS));
Ville Syrjälä017636c2013-12-05 15:51:37 +02004494
4495 ilk_init_lp_watermarks(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004496
4497 /*
4498 * Based on the document from hardware guys the following bits
4499 * should be set unconditionally in order to enable FBC.
4500 * The bit 22 of 0x42000
4501 * The bit 22 of 0x42004
4502 * The bit 7,8,9 of 0x42020.
4503 */
4504 if (IS_IRONLAKE_M(dev)) {
Damien Lespiau4bb35332013-06-14 15:23:24 +01004505 /* WaFbcAsynchFlipDisableFbcQueue:ilk */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004506 I915_WRITE(ILK_DISPLAY_CHICKEN1,
4507 I915_READ(ILK_DISPLAY_CHICKEN1) |
4508 ILK_FBCQ_DIS);
4509 I915_WRITE(ILK_DISPLAY_CHICKEN2,
4510 I915_READ(ILK_DISPLAY_CHICKEN2) |
4511 ILK_DPARB_GATE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004512 }
4513
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01004514 I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
4515
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004516 I915_WRITE(ILK_DISPLAY_CHICKEN2,
4517 I915_READ(ILK_DISPLAY_CHICKEN2) |
4518 ILK_ELPIN_409_SELECT);
4519 I915_WRITE(_3D_CHICKEN2,
4520 _3D_CHICKEN2_WM_READ_PIPELINED << 16 |
4521 _3D_CHICKEN2_WM_READ_PIPELINED);
Daniel Vetter4358a372012-10-18 11:49:51 +02004522
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01004523 /* WaDisableRenderCachePipelinedFlush:ilk */
Daniel Vetter4358a372012-10-18 11:49:51 +02004524 I915_WRITE(CACHE_MODE_0,
4525 _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
Daniel Vetter3107bd42012-10-31 22:52:31 +01004526
Ville Syrjälä0e088b82013-06-07 10:47:04 +03004527 g4x_disable_trickle_feed(dev);
Ville Syrjäläbdad2b22013-06-07 10:47:03 +03004528
Daniel Vetter3107bd42012-10-31 22:52:31 +01004529 ibx_init_clock_gating(dev);
4530}
4531
4532static void cpt_init_clock_gating(struct drm_device *dev)
4533{
4534 struct drm_i915_private *dev_priv = dev->dev_private;
4535 int pipe;
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03004536 uint32_t val;
Daniel Vetter3107bd42012-10-31 22:52:31 +01004537
4538 /*
4539 * On Ibex Peak and Cougar Point, we need to disable clock
4540 * gating for the panel power sequencer or it will fail to
4541 * start up when no ports are active.
4542 */
Jesse Barnescd664072013-10-02 10:34:19 -07004543 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE |
4544 PCH_DPLUNIT_CLOCK_GATE_DISABLE |
4545 PCH_CPUNIT_CLOCK_GATE_DISABLE);
Daniel Vetter3107bd42012-10-31 22:52:31 +01004546 I915_WRITE(SOUTH_CHICKEN2, I915_READ(SOUTH_CHICKEN2) |
4547 DPLS_EDP_PPS_FIX_DIS);
Takashi Iwai335c07b2012-12-11 11:46:29 +01004548 /* The below fixes the weird display corruption, a few pixels shifted
4549 * downward, on (only) LVDS of some HP laptops with IVY.
4550 */
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03004551 for_each_pipe(pipe) {
Paulo Zanonidc4bd2d2013-04-08 15:48:08 -03004552 val = I915_READ(TRANS_CHICKEN2(pipe));
4553 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
4554 val &= ~TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03004555 if (dev_priv->vbt.fdi_rx_polarity_inverted)
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03004556 val |= TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
Paulo Zanonidc4bd2d2013-04-08 15:48:08 -03004557 val &= ~TRANS_CHICKEN2_FRAME_START_DELAY_MASK;
4558 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_COUNTER;
4559 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_MODESWITCH;
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03004560 I915_WRITE(TRANS_CHICKEN2(pipe), val);
4561 }
Daniel Vetter3107bd42012-10-31 22:52:31 +01004562 /* WADP0ClockGatingDisable */
4563 for_each_pipe(pipe) {
4564 I915_WRITE(TRANS_CHICKEN1(pipe),
4565 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
4566 }
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004567}
4568
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01004569static void gen6_check_mch_setup(struct drm_device *dev)
4570{
4571 struct drm_i915_private *dev_priv = dev->dev_private;
4572 uint32_t tmp;
4573
4574 tmp = I915_READ(MCH_SSKPD);
4575 if ((tmp & MCH_SSKPD_WM0_MASK) != MCH_SSKPD_WM0_VAL) {
4576 DRM_INFO("Wrong MCH_SSKPD value: 0x%08x\n", tmp);
4577 DRM_INFO("This can cause pipe underruns and display issues.\n");
4578 DRM_INFO("Please upgrade your BIOS to fix this.\n");
4579 }
4580}
4581
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03004582static void gen6_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004583{
4584 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau231e54f2012-10-19 17:55:41 +01004585 uint32_t dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004586
Damien Lespiau231e54f2012-10-19 17:55:41 +01004587 I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004588
4589 I915_WRITE(ILK_DISPLAY_CHICKEN2,
4590 I915_READ(ILK_DISPLAY_CHICKEN2) |
4591 ILK_ELPIN_409_SELECT);
4592
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01004593 /* WaDisableHiZPlanesWhenMSAAEnabled:snb */
Daniel Vetter42839082012-12-14 23:38:28 +01004594 I915_WRITE(_3D_CHICKEN,
4595 _MASKED_BIT_ENABLE(_3D_CHICKEN_HIZ_PLANE_DISABLE_MSAA_4X_SNB));
4596
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01004597 /* WaSetupGtModeTdRowDispatch:snb */
Daniel Vetter6547fbd2012-12-14 23:38:29 +01004598 if (IS_SNB_GT1(dev))
4599 I915_WRITE(GEN6_GT_MODE,
4600 _MASKED_BIT_ENABLE(GEN6_TD_FOUR_ROW_DISPATCH_DISABLE));
4601
Ville Syrjälä017636c2013-12-05 15:51:37 +02004602 ilk_init_lp_watermarks(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004603
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004604 I915_WRITE(CACHE_MODE_0,
Daniel Vetter50743292012-04-26 22:02:54 +02004605 _MASKED_BIT_DISABLE(CM0_STC_EVICT_DISABLE_LRA_SNB));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004606
4607 I915_WRITE(GEN6_UCGCTL1,
4608 I915_READ(GEN6_UCGCTL1) |
4609 GEN6_BLBUNIT_CLOCK_GATE_DISABLE |
4610 GEN6_CSUNIT_CLOCK_GATE_DISABLE);
4611
4612 /* According to the BSpec vol1g, bit 12 (RCPBUNIT) clock
4613 * gating disable must be set. Failure to set it results in
4614 * flickering pixels due to Z write ordering failures after
4615 * some amount of runtime in the Mesa "fire" demo, and Unigine
4616 * Sanctuary and Tropics, and apparently anything else with
4617 * alpha test or pixel discard.
4618 *
4619 * According to the spec, bit 11 (RCCUNIT) must also be set,
4620 * but we didn't debug actual testcases to find it out.
Jesse Barnes0f846f82012-06-14 11:04:47 -07004621 *
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01004622 * Also apply WaDisableVDSUnitClockGating:snb and
4623 * WaDisableRCPBUnitClockGating:snb.
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004624 */
4625 I915_WRITE(GEN6_UCGCTL2,
Jesse Barnes0f846f82012-06-14 11:04:47 -07004626 GEN7_VDSUNIT_CLOCK_GATE_DISABLE |
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004627 GEN6_RCPBUNIT_CLOCK_GATE_DISABLE |
4628 GEN6_RCCUNIT_CLOCK_GATE_DISABLE);
4629
4630 /* Bspec says we need to always set all mask bits. */
Kenneth Graunke26b6e442012-10-07 08:51:07 -07004631 I915_WRITE(_3D_CHICKEN3, (0xFFFF << 16) |
4632 _3D_CHICKEN3_SF_DISABLE_FASTCLIP_CULL);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004633
4634 /*
4635 * According to the spec the following bits should be
4636 * set in order to enable memory self-refresh and fbc:
4637 * The bit21 and bit22 of 0x42000
4638 * The bit21 and bit22 of 0x42004
4639 * The bit5 and bit7 of 0x42020
4640 * The bit14 of 0x70180
4641 * The bit14 of 0x71180
Damien Lespiau4bb35332013-06-14 15:23:24 +01004642 *
4643 * WaFbcAsynchFlipDisableFbcQueue:snb
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004644 */
4645 I915_WRITE(ILK_DISPLAY_CHICKEN1,
4646 I915_READ(ILK_DISPLAY_CHICKEN1) |
4647 ILK_FBCQ_DIS | ILK_PABSTRETCH_DIS);
4648 I915_WRITE(ILK_DISPLAY_CHICKEN2,
4649 I915_READ(ILK_DISPLAY_CHICKEN2) |
4650 ILK_DPARB_GATE | ILK_VSDPFD_FULL);
Damien Lespiau231e54f2012-10-19 17:55:41 +01004651 I915_WRITE(ILK_DSPCLK_GATE_D,
4652 I915_READ(ILK_DSPCLK_GATE_D) |
4653 ILK_DPARBUNIT_CLOCK_GATE_ENABLE |
4654 ILK_DPFDUNIT_CLOCK_GATE_ENABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004655
Ville Syrjälä0e088b82013-06-07 10:47:04 +03004656 g4x_disable_trickle_feed(dev);
Ben Widawskyf8f2ac92012-10-03 19:34:24 -07004657
4658 /* The default value should be 0x200 according to docs, but the two
4659 * platforms I checked have a 0 for this. (Maybe BIOS overrides?) */
4660 I915_WRITE(GEN6_GT_MODE, _MASKED_BIT_DISABLE(0xffff));
4661 I915_WRITE(GEN6_GT_MODE, _MASKED_BIT_ENABLE(GEN6_GT_MODE_HI));
Daniel Vetter3107bd42012-10-31 22:52:31 +01004662
4663 cpt_init_clock_gating(dev);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01004664
4665 gen6_check_mch_setup(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004666}
4667
4668static void gen7_setup_fixed_func_scheduler(struct drm_i915_private *dev_priv)
4669{
4670 uint32_t reg = I915_READ(GEN7_FF_THREAD_MODE);
4671
4672 reg &= ~GEN7_FF_SCHED_MASK;
4673 reg |= GEN7_FF_TS_SCHED_HW;
4674 reg |= GEN7_FF_VS_SCHED_HW;
4675 reg |= GEN7_FF_DS_SCHED_HW;
4676
Ben Widawsky41c0b3a2013-01-26 11:52:00 -08004677 if (IS_HASWELL(dev_priv->dev))
4678 reg &= ~GEN7_FF_VS_REF_CNT_FFME;
4679
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004680 I915_WRITE(GEN7_FF_THREAD_MODE, reg);
4681}
4682
Paulo Zanoni17a303e2012-11-20 15:12:07 -02004683static void lpt_init_clock_gating(struct drm_device *dev)
4684{
4685 struct drm_i915_private *dev_priv = dev->dev_private;
4686
4687 /*
4688 * TODO: this bit should only be enabled when really needed, then
4689 * disabled when not needed anymore in order to save power.
4690 */
4691 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE)
4692 I915_WRITE(SOUTH_DSPCLK_GATE_D,
4693 I915_READ(SOUTH_DSPCLK_GATE_D) |
4694 PCH_LP_PARTITION_LEVEL_DISABLE);
Paulo Zanoni0a790cd2013-04-17 18:15:49 -03004695
4696 /* WADPOClockGatingDisable:hsw */
4697 I915_WRITE(_TRANSA_CHICKEN1,
4698 I915_READ(_TRANSA_CHICKEN1) |
4699 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
Paulo Zanoni17a303e2012-11-20 15:12:07 -02004700}
4701
Imre Deak7d708ee2013-04-17 14:04:50 +03004702static void lpt_suspend_hw(struct drm_device *dev)
4703{
4704 struct drm_i915_private *dev_priv = dev->dev_private;
4705
4706 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
4707 uint32_t val = I915_READ(SOUTH_DSPCLK_GATE_D);
4708
4709 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
4710 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
4711 }
4712}
4713
Ben Widawsky1020a5c2013-11-02 21:07:06 -07004714static void gen8_init_clock_gating(struct drm_device *dev)
4715{
4716 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07004717 enum pipe i;
Ben Widawsky1020a5c2013-11-02 21:07:06 -07004718
4719 I915_WRITE(WM3_LP_ILK, 0);
4720 I915_WRITE(WM2_LP_ILK, 0);
4721 I915_WRITE(WM1_LP_ILK, 0);
Ben Widawsky50ed5fb2013-11-02 21:07:40 -07004722
4723 /* FIXME(BDW): Check all the w/a, some might only apply to
4724 * pre-production hw. */
4725
Ben Widawskyfd392b62013-11-04 22:52:39 -08004726 WARN(!i915_preliminary_hw_support,
4727 "GEN8_CENTROID_PIXEL_OPT_DIS not be needed for production\n");
4728 I915_WRITE(HALF_SLICE_CHICKEN3,
4729 _MASKED_BIT_ENABLE(GEN8_CENTROID_PIXEL_OPT_DIS));
Ben Widawskybf663472013-11-02 21:07:57 -07004730 I915_WRITE(HALF_SLICE_CHICKEN3,
4731 _MASKED_BIT_ENABLE(GEN8_SAMPLER_POWER_BYPASS_DIS));
Ben Widawsky4afe8d32013-11-02 21:07:55 -07004732 I915_WRITE(GAMTARBMODE, _MASKED_BIT_ENABLE(ARB_MODE_BWGTLB_DISABLE));
4733
Ben Widawsky7f88da02013-11-02 21:07:58 -07004734 I915_WRITE(_3D_CHICKEN3,
4735 _3D_CHICKEN_SDE_LIMIT_FIFO_POLY_DEPTH(2));
4736
Ben Widawskya75f3622013-11-02 21:07:59 -07004737 I915_WRITE(COMMON_SLICE_CHICKEN2,
4738 _MASKED_BIT_ENABLE(GEN8_CSC2_SBE_VUE_CACHE_CONSERVATIVE));
4739
Ben Widawsky4c2e7a52013-11-02 21:08:00 -07004740 I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
4741 _MASKED_BIT_ENABLE(GEN7_SINGLE_SUBSCAN_DISPATCH_ENABLE));
4742
Ben Widawskyab57fff2013-12-12 15:28:04 -08004743 /* WaSwitchSolVfFArbitrationPriority:bdw */
Ben Widawsky50ed5fb2013-11-02 21:07:40 -07004744 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07004745
Ben Widawskyab57fff2013-12-12 15:28:04 -08004746 /* WaPsrDPAMaskVBlankInSRD:bdw */
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07004747 I915_WRITE(CHICKEN_PAR1_1,
4748 I915_READ(CHICKEN_PAR1_1) | DPA_MASK_VBLANK_SRD);
4749
Ben Widawskyab57fff2013-12-12 15:28:04 -08004750 /* WaPsrDPRSUnmaskVBlankInSRD:bdw */
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07004751 for_each_pipe(i) {
4752 I915_WRITE(CHICKEN_PIPESL_1(i),
4753 I915_READ(CHICKEN_PIPESL_1(i) |
4754 DPRS_MASK_VBLANK_SRD));
4755 }
Ben Widawsky63801f22013-12-12 17:26:03 -08004756
4757 /* Use Force Non-Coherent whenever executing a 3D context. This is a
4758 * workaround for for a possible hang in the unlikely event a TLB
4759 * invalidation occurs during a PSD flush.
4760 */
4761 I915_WRITE(HDC_CHICKEN0,
4762 I915_READ(HDC_CHICKEN0) |
4763 _MASKED_BIT_ENABLE(HDC_FORCE_NON_COHERENT));
Ben Widawskyab57fff2013-12-12 15:28:04 -08004764
4765 /* WaVSRefCountFullforceMissDisable:bdw */
4766 /* WaDSRefCountFullforceMissDisable:bdw */
4767 I915_WRITE(GEN7_FF_THREAD_MODE,
4768 I915_READ(GEN7_FF_THREAD_MODE) &
4769 ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
Ben Widawsky1020a5c2013-11-02 21:07:06 -07004770}
4771
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03004772static void haswell_init_clock_gating(struct drm_device *dev)
4773{
4774 struct drm_i915_private *dev_priv = dev->dev_private;
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03004775
Ville Syrjälä017636c2013-12-05 15:51:37 +02004776 ilk_init_lp_watermarks(dev);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03004777
4778 /* According to the spec, bit 13 (RCZUNIT) must be set on IVB.
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01004779 * This implements the WaDisableRCZUnitClockGating:hsw workaround.
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03004780 */
4781 I915_WRITE(GEN6_UCGCTL2, GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
4782
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01004783 /* Apply the WaDisableRHWOOptimizationForRenderHang:hsw workaround. */
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03004784 I915_WRITE(GEN7_COMMON_SLICE_CHICKEN1,
4785 GEN7_CSC1_RHWO_OPT_DISABLE_IN_RCC);
4786
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01004787 /* WaApplyL3ControlAndL3ChickenMode:hsw */
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03004788 I915_WRITE(GEN7_L3CNTLREG1,
4789 GEN7_WA_FOR_GEN7_L3_CONTROL);
4790 I915_WRITE(GEN7_L3_CHICKEN_MODE_REGISTER,
4791 GEN7_WA_L3_CHICKEN_MODE);
4792
Francisco Jerezf3fc4882013-10-02 15:53:16 -07004793 /* L3 caching of data atomics doesn't work -- disable it. */
4794 I915_WRITE(HSW_SCRATCH1, HSW_SCRATCH1_L3_DATA_ATOMICS_DISABLE);
4795 I915_WRITE(HSW_ROW_CHICKEN3,
4796 _MASKED_BIT_ENABLE(HSW_ROW_CHICKEN3_L3_GLOBAL_ATOMICS_DISABLE));
4797
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01004798 /* This is required by WaCatErrorRejectionIssue:hsw */
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03004799 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
4800 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
4801 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
4802
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01004803 /* WaVSRefCountFullforceMissDisable:hsw */
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03004804 gen7_setup_fixed_func_scheduler(dev_priv);
4805
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01004806 /* WaDisable4x2SubspanOptimization:hsw */
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03004807 I915_WRITE(CACHE_MODE_1,
4808 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Eugeni Dodonov1544d9d2012-07-02 11:51:10 -03004809
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01004810 /* WaSwitchSolVfFArbitrationPriority:hsw */
Ben Widawskye3dff582013-03-20 14:49:14 -07004811 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
4812
Paulo Zanoni90a88642013-05-03 17:23:45 -03004813 /* WaRsPkgCStateDisplayPMReq:hsw */
4814 I915_WRITE(CHICKEN_PAR1_1,
4815 I915_READ(CHICKEN_PAR1_1) | FORCE_ARB_IDLE_PLANES);
Eugeni Dodonov1544d9d2012-07-02 11:51:10 -03004816
Paulo Zanoni17a303e2012-11-20 15:12:07 -02004817 lpt_init_clock_gating(dev);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03004818}
4819
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03004820static void ivybridge_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004821{
4822 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawsky20848222012-05-04 18:58:59 -07004823 uint32_t snpcr;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004824
Ville Syrjälä017636c2013-12-05 15:51:37 +02004825 ilk_init_lp_watermarks(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004826
Damien Lespiau231e54f2012-10-19 17:55:41 +01004827 I915_WRITE(ILK_DSPCLK_GATE_D, ILK_VRHUNIT_CLOCK_GATE_DISABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004828
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01004829 /* WaDisableEarlyCull:ivb */
Jesse Barnes87f80202012-10-02 17:43:41 -05004830 I915_WRITE(_3D_CHICKEN3,
4831 _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
4832
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01004833 /* WaDisableBackToBackFlipFix:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004834 I915_WRITE(IVB_CHICKEN3,
4835 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
4836 CHICKEN3_DGMG_DONE_FIX_DISABLE);
4837
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01004838 /* WaDisablePSDDualDispatchEnable:ivb */
Jesse Barnes12f33822012-10-25 12:15:45 -07004839 if (IS_IVB_GT1(dev))
4840 I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
4841 _MASKED_BIT_ENABLE(GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
4842 else
4843 I915_WRITE(GEN7_HALF_SLICE_CHICKEN1_GT2,
4844 _MASKED_BIT_ENABLE(GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
4845
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01004846 /* Apply the WaDisableRHWOOptimizationForRenderHang:ivb workaround. */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004847 I915_WRITE(GEN7_COMMON_SLICE_CHICKEN1,
4848 GEN7_CSC1_RHWO_OPT_DISABLE_IN_RCC);
4849
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01004850 /* WaApplyL3ControlAndL3ChickenMode:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004851 I915_WRITE(GEN7_L3CNTLREG1,
4852 GEN7_WA_FOR_GEN7_L3_CONTROL);
4853 I915_WRITE(GEN7_L3_CHICKEN_MODE_REGISTER,
Jesse Barnes8ab43972012-10-25 12:15:42 -07004854 GEN7_WA_L3_CHICKEN_MODE);
4855 if (IS_IVB_GT1(dev))
4856 I915_WRITE(GEN7_ROW_CHICKEN2,
4857 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
4858 else
4859 I915_WRITE(GEN7_ROW_CHICKEN2_GT2,
4860 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
4861
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004862
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01004863 /* WaForceL3Serialization:ivb */
Jesse Barnes61939d92012-10-02 17:43:38 -05004864 I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
4865 ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
4866
Jesse Barnes0f846f82012-06-14 11:04:47 -07004867 /* According to the BSpec vol1g, bit 12 (RCPBUNIT) clock
4868 * gating disable must be set. Failure to set it results in
4869 * flickering pixels due to Z write ordering failures after
4870 * some amount of runtime in the Mesa "fire" demo, and Unigine
4871 * Sanctuary and Tropics, and apparently anything else with
4872 * alpha test or pixel discard.
4873 *
4874 * According to the spec, bit 11 (RCCUNIT) must also be set,
4875 * but we didn't debug actual testcases to find it out.
4876 *
4877 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01004878 * This implements the WaDisableRCZUnitClockGating:ivb workaround.
Jesse Barnes0f846f82012-06-14 11:04:47 -07004879 */
4880 I915_WRITE(GEN6_UCGCTL2,
4881 GEN6_RCZUNIT_CLOCK_GATE_DISABLE |
4882 GEN6_RCCUNIT_CLOCK_GATE_DISABLE);
4883
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01004884 /* This is required by WaCatErrorRejectionIssue:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004885 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
4886 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
4887 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
4888
Ville Syrjälä0e088b82013-06-07 10:47:04 +03004889 g4x_disable_trickle_feed(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004890
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01004891 /* WaVSRefCountFullforceMissDisable:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004892 gen7_setup_fixed_func_scheduler(dev_priv);
Daniel Vetter97e19302012-04-24 16:00:21 +02004893
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01004894 /* WaDisable4x2SubspanOptimization:ivb */
Daniel Vetter97e19302012-04-24 16:00:21 +02004895 I915_WRITE(CACHE_MODE_1,
4896 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Ben Widawsky20848222012-05-04 18:58:59 -07004897
4898 snpcr = I915_READ(GEN6_MBCUNIT_SNPCR);
4899 snpcr &= ~GEN6_MBC_SNPCR_MASK;
4900 snpcr |= GEN6_MBC_SNPCR_MED;
4901 I915_WRITE(GEN6_MBCUNIT_SNPCR, snpcr);
Daniel Vetter3107bd42012-10-31 22:52:31 +01004902
Ben Widawskyab5c6082013-04-05 13:12:41 -07004903 if (!HAS_PCH_NOP(dev))
4904 cpt_init_clock_gating(dev);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01004905
4906 gen6_check_mch_setup(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004907}
4908
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03004909static void valleyview_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004910{
4911 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes85b1d7b2013-11-04 11:52:45 -08004912 u32 val;
4913
4914 mutex_lock(&dev_priv->rps.hw_lock);
4915 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
4916 mutex_unlock(&dev_priv->rps.hw_lock);
4917 switch ((val >> 6) & 3) {
4918 case 0:
Jesse Barnes85b1d7b2013-11-04 11:52:45 -08004919 dev_priv->mem_freq = 800;
4920 break;
Jesse Barnesf64a28a2013-11-04 16:07:00 -08004921 case 1:
Jesse Barnes85b1d7b2013-11-04 11:52:45 -08004922 dev_priv->mem_freq = 1066;
4923 break;
Jesse Barnesf64a28a2013-11-04 16:07:00 -08004924 case 2:
Jesse Barnes85b1d7b2013-11-04 11:52:45 -08004925 dev_priv->mem_freq = 1333;
4926 break;
Jesse Barnesf64a28a2013-11-04 16:07:00 -08004927 case 3:
Chon Ming Lee23259912013-11-07 15:23:26 +08004928 dev_priv->mem_freq = 1333;
Jesse Barnesf64a28a2013-11-04 16:07:00 -08004929 break;
Jesse Barnes85b1d7b2013-11-04 11:52:45 -08004930 }
4931 DRM_DEBUG_DRIVER("DDR speed: %d MHz", dev_priv->mem_freq);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004932
Ville Syrjäläd7fe0cc2013-05-21 18:01:50 +03004933 I915_WRITE(DSPCLK_GATE_D, VRHUNIT_CLOCK_GATE_DISABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004934
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01004935 /* WaDisableEarlyCull:vlv */
Jesse Barnes87f80202012-10-02 17:43:41 -05004936 I915_WRITE(_3D_CHICKEN3,
4937 _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
4938
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01004939 /* WaDisableBackToBackFlipFix:vlv */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004940 I915_WRITE(IVB_CHICKEN3,
4941 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
4942 CHICKEN3_DGMG_DONE_FIX_DISABLE);
4943
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01004944 /* WaDisablePSDDualDispatchEnable:vlv */
Jesse Barnes12f33822012-10-25 12:15:45 -07004945 I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
Jesse Barnesd3bc0302013-03-08 10:45:51 -08004946 _MASKED_BIT_ENABLE(GEN7_MAX_PS_THREAD_DEP |
4947 GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
Jesse Barnes12f33822012-10-25 12:15:45 -07004948
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01004949 /* Apply the WaDisableRHWOOptimizationForRenderHang:vlv workaround. */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004950 I915_WRITE(GEN7_COMMON_SLICE_CHICKEN1,
4951 GEN7_CSC1_RHWO_OPT_DISABLE_IN_RCC);
4952
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01004953 /* WaApplyL3ControlAndL3ChickenMode:vlv */
Jesse Barnesd0cf5ea2012-10-25 12:15:41 -07004954 I915_WRITE(GEN7_L3CNTLREG1, I915_READ(GEN7_L3CNTLREG1) | GEN7_L3AGDIS);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004955 I915_WRITE(GEN7_L3_CHICKEN_MODE_REGISTER, GEN7_WA_L3_CHICKEN_MODE);
4956
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01004957 /* WaForceL3Serialization:vlv */
Jesse Barnes61939d92012-10-02 17:43:38 -05004958 I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
4959 ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
4960
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01004961 /* WaDisableDopClockGating:vlv */
Jesse Barnes8ab43972012-10-25 12:15:42 -07004962 I915_WRITE(GEN7_ROW_CHICKEN2,
4963 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
4964
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01004965 /* This is required by WaCatErrorRejectionIssue:vlv */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004966 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
4967 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
4968 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
4969
Jesse Barnes0f846f82012-06-14 11:04:47 -07004970 /* According to the BSpec vol1g, bit 12 (RCPBUNIT) clock
4971 * gating disable must be set. Failure to set it results in
4972 * flickering pixels due to Z write ordering failures after
4973 * some amount of runtime in the Mesa "fire" demo, and Unigine
4974 * Sanctuary and Tropics, and apparently anything else with
4975 * alpha test or pixel discard.
4976 *
4977 * According to the spec, bit 11 (RCCUNIT) must also be set,
4978 * but we didn't debug actual testcases to find it out.
4979 *
4980 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01004981 * This implements the WaDisableRCZUnitClockGating:vlv workaround.
Jesse Barnes0f846f82012-06-14 11:04:47 -07004982 *
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01004983 * Also apply WaDisableVDSUnitClockGating:vlv and
4984 * WaDisableRCPBUnitClockGating:vlv.
Jesse Barnes0f846f82012-06-14 11:04:47 -07004985 */
4986 I915_WRITE(GEN6_UCGCTL2,
4987 GEN7_VDSUNIT_CLOCK_GATE_DISABLE |
Jesse Barnes6edaa7f2012-06-14 11:04:49 -07004988 GEN7_TDLUNIT_CLOCK_GATE_DISABLE |
Jesse Barnes0f846f82012-06-14 11:04:47 -07004989 GEN6_RCZUNIT_CLOCK_GATE_DISABLE |
4990 GEN6_RCPBUNIT_CLOCK_GATE_DISABLE |
4991 GEN6_RCCUNIT_CLOCK_GATE_DISABLE);
4992
Jesse Barnese3f33d42012-06-14 11:04:50 -07004993 I915_WRITE(GEN7_UCGCTL4, GEN7_L3BANK2X_CLOCK_GATE_DISABLE);
4994
Ville Syrjäläe0d8d592013-06-12 22:11:18 +03004995 I915_WRITE(MI_ARB_VLV, MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004996
Daniel Vetter6b26c862012-04-24 14:04:12 +02004997 I915_WRITE(CACHE_MODE_1,
4998 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Jesse Barnes79831172012-06-20 10:53:12 -07004999
5000 /*
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005001 * WaDisableVLVClockGating_VBIIssue:vlv
Jesse Barnes2d809572012-10-25 12:15:44 -07005002 * Disable clock gating on th GCFG unit to prevent a delay
5003 * in the reporting of vblank events.
5004 */
Jesse Barnes4e8c84a2013-03-08 10:45:54 -08005005 I915_WRITE(VLV_GUNIT_CLOCK_GATE, 0xffffffff);
5006
5007 /* Conservative clock gating settings for now */
5008 I915_WRITE(0x9400, 0xffffffff);
5009 I915_WRITE(0x9404, 0xffffffff);
5010 I915_WRITE(0x9408, 0xffffffff);
5011 I915_WRITE(0x940c, 0xffffffff);
5012 I915_WRITE(0x9410, 0xffffffff);
5013 I915_WRITE(0x9414, 0xffffffff);
5014 I915_WRITE(0x9418, 0xffffffff);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005015}
5016
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005017static void g4x_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005018{
5019 struct drm_i915_private *dev_priv = dev->dev_private;
5020 uint32_t dspclk_gate;
5021
5022 I915_WRITE(RENCLK_GATE_D1, 0);
5023 I915_WRITE(RENCLK_GATE_D2, VF_UNIT_CLOCK_GATE_DISABLE |
5024 GS_UNIT_CLOCK_GATE_DISABLE |
5025 CL_UNIT_CLOCK_GATE_DISABLE);
5026 I915_WRITE(RAMCLK_GATE_D, 0);
5027 dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE |
5028 OVRUNIT_CLOCK_GATE_DISABLE |
5029 OVCUNIT_CLOCK_GATE_DISABLE;
5030 if (IS_GM45(dev))
5031 dspclk_gate |= DSSUNIT_CLOCK_GATE_DISABLE;
5032 I915_WRITE(DSPCLK_GATE_D, dspclk_gate);
Daniel Vetter4358a372012-10-18 11:49:51 +02005033
5034 /* WaDisableRenderCachePipelinedFlush */
5035 I915_WRITE(CACHE_MODE_0,
5036 _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
Ville Syrjäläde1aa622013-06-07 10:47:01 +03005037
Ville Syrjälä0e088b82013-06-07 10:47:04 +03005038 g4x_disable_trickle_feed(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005039}
5040
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005041static void crestline_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005042{
5043 struct drm_i915_private *dev_priv = dev->dev_private;
5044
5045 I915_WRITE(RENCLK_GATE_D1, I965_RCC_CLOCK_GATE_DISABLE);
5046 I915_WRITE(RENCLK_GATE_D2, 0);
5047 I915_WRITE(DSPCLK_GATE_D, 0);
5048 I915_WRITE(RAMCLK_GATE_D, 0);
5049 I915_WRITE16(DEUC, 0);
Ville Syrjälä20f94962013-06-07 10:47:02 +03005050 I915_WRITE(MI_ARB_STATE,
5051 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005052}
5053
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005054static void broadwater_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005055{
5056 struct drm_i915_private *dev_priv = dev->dev_private;
5057
5058 I915_WRITE(RENCLK_GATE_D1, I965_RCZ_CLOCK_GATE_DISABLE |
5059 I965_RCC_CLOCK_GATE_DISABLE |
5060 I965_RCPB_CLOCK_GATE_DISABLE |
5061 I965_ISC_CLOCK_GATE_DISABLE |
5062 I965_FBC_CLOCK_GATE_DISABLE);
5063 I915_WRITE(RENCLK_GATE_D2, 0);
Ville Syrjälä20f94962013-06-07 10:47:02 +03005064 I915_WRITE(MI_ARB_STATE,
5065 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005066}
5067
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005068static void gen3_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005069{
5070 struct drm_i915_private *dev_priv = dev->dev_private;
5071 u32 dstate = I915_READ(D_STATE);
5072
5073 dstate |= DSTATE_PLL_D3_OFF | DSTATE_GFX_CLOCK_GATING |
5074 DSTATE_DOT_CLOCK_GATING;
5075 I915_WRITE(D_STATE, dstate);
Chris Wilson13a86b82012-04-24 14:51:43 +01005076
5077 if (IS_PINEVIEW(dev))
5078 I915_WRITE(ECOSKPD, _MASKED_BIT_ENABLE(ECO_GATING_CX_ONLY));
Daniel Vetter974a3b02012-09-09 11:54:16 +02005079
5080 /* IIR "flip pending" means done if this bit is set */
5081 I915_WRITE(ECOSKPD, _MASKED_BIT_DISABLE(ECO_FLIP_DONE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005082}
5083
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005084static void i85x_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005085{
5086 struct drm_i915_private *dev_priv = dev->dev_private;
5087
5088 I915_WRITE(RENCLK_GATE_D1, SV_CLOCK_GATE_DISABLE);
5089}
5090
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005091static void i830_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005092{
5093 struct drm_i915_private *dev_priv = dev->dev_private;
5094
5095 I915_WRITE(DSPCLK_GATE_D, OVRUNIT_CLOCK_GATE_DISABLE);
5096}
5097
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005098void intel_init_clock_gating(struct drm_device *dev)
5099{
5100 struct drm_i915_private *dev_priv = dev->dev_private;
5101
5102 dev_priv->display.init_clock_gating(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005103}
5104
Imre Deak7d708ee2013-04-17 14:04:50 +03005105void intel_suspend_hw(struct drm_device *dev)
5106{
5107 if (HAS_PCH_LPT(dev))
5108 lpt_suspend_hw(dev);
5109}
5110
Imre Deakc1ca7272013-11-25 17:15:29 +02005111#define for_each_power_well(i, power_well, domain_mask, power_domains) \
5112 for (i = 0; \
5113 i < (power_domains)->power_well_count && \
5114 ((power_well) = &(power_domains)->power_wells[i]); \
5115 i++) \
5116 if ((power_well)->domains & (domain_mask))
5117
5118#define for_each_power_well_rev(i, power_well, domain_mask, power_domains) \
5119 for (i = (power_domains)->power_well_count - 1; \
5120 i >= 0 && ((power_well) = &(power_domains)->power_wells[i]);\
5121 i--) \
5122 if ((power_well)->domains & (domain_mask))
5123
Paulo Zanoni15d199e2013-03-22 14:14:13 -03005124/**
5125 * We should only use the power well if we explicitly asked the hardware to
5126 * enable it, so check if it's enabled and also check if we've requested it to
5127 * be enabled.
5128 */
Imre Deakc1ca7272013-11-25 17:15:29 +02005129static bool hsw_power_well_enabled(struct drm_device *dev,
5130 struct i915_power_well *power_well)
5131{
5132 struct drm_i915_private *dev_priv = dev->dev_private;
5133
5134 return I915_READ(HSW_PWR_WELL_DRIVER) ==
5135 (HSW_PWR_WELL_ENABLE_REQUEST | HSW_PWR_WELL_STATE_ENABLED);
5136}
5137
Imre Deakddf9c532013-11-27 22:02:02 +02005138bool intel_display_power_enabled_sw(struct drm_device *dev,
5139 enum intel_display_power_domain domain)
5140{
5141 struct drm_i915_private *dev_priv = dev->dev_private;
5142 struct i915_power_domains *power_domains;
5143
5144 power_domains = &dev_priv->power_domains;
5145
5146 return power_domains->domain_use_count[domain];
5147}
5148
Paulo Zanonib97186f2013-05-03 12:15:36 -03005149bool intel_display_power_enabled(struct drm_device *dev,
5150 enum intel_display_power_domain domain)
Paulo Zanoni15d199e2013-03-22 14:14:13 -03005151{
5152 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deakc1ca7272013-11-25 17:15:29 +02005153 struct i915_power_domains *power_domains;
5154 struct i915_power_well *power_well;
5155 bool is_enabled;
5156 int i;
Paulo Zanoni15d199e2013-03-22 14:14:13 -03005157
Imre Deakc1ca7272013-11-25 17:15:29 +02005158 power_domains = &dev_priv->power_domains;
5159
5160 is_enabled = true;
5161
5162 mutex_lock(&power_domains->lock);
5163 for_each_power_well_rev(i, power_well, BIT(domain), power_domains) {
Imre Deak6f3ef5d2013-11-25 17:15:30 +02005164 if (power_well->always_on)
5165 continue;
5166
Imre Deakc1ca7272013-11-25 17:15:29 +02005167 if (!power_well->is_enabled(dev, power_well)) {
5168 is_enabled = false;
5169 break;
5170 }
5171 }
5172 mutex_unlock(&power_domains->lock);
5173
5174 return is_enabled;
Paulo Zanoni15d199e2013-03-22 14:14:13 -03005175}
5176
Paulo Zanonid5e8fdc2013-12-11 18:50:09 -02005177static void hsw_power_well_post_enable(struct drm_i915_private *dev_priv)
5178{
5179 struct drm_device *dev = dev_priv->dev;
5180 unsigned long irqflags;
5181
Paulo Zanonif9dcb0d2013-12-11 18:50:10 -02005182 /*
5183 * After we re-enable the power well, if we touch VGA register 0x3d5
5184 * we'll get unclaimed register interrupts. This stops after we write
5185 * anything to the VGA MSR register. The vgacon module uses this
5186 * register all the time, so if we unbind our driver and, as a
5187 * consequence, bind vgacon, we'll get stuck in an infinite loop at
5188 * console_unlock(). So make here we touch the VGA MSR register, making
5189 * sure vgacon can keep working normally without triggering interrupts
5190 * and error messages.
5191 */
5192 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
5193 outb(inb(VGA_MSR_READ), VGA_MSR_WRITE);
5194 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
5195
Paulo Zanonid5e8fdc2013-12-11 18:50:09 -02005196 if (IS_BROADWELL(dev)) {
5197 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
5198 I915_WRITE(GEN8_DE_PIPE_IMR(PIPE_B),
5199 dev_priv->de_irq_mask[PIPE_B]);
5200 I915_WRITE(GEN8_DE_PIPE_IER(PIPE_B),
5201 ~dev_priv->de_irq_mask[PIPE_B] |
5202 GEN8_PIPE_VBLANK);
5203 I915_WRITE(GEN8_DE_PIPE_IMR(PIPE_C),
5204 dev_priv->de_irq_mask[PIPE_C]);
5205 I915_WRITE(GEN8_DE_PIPE_IER(PIPE_C),
5206 ~dev_priv->de_irq_mask[PIPE_C] |
5207 GEN8_PIPE_VBLANK);
5208 POSTING_READ(GEN8_DE_PIPE_IER(PIPE_C));
5209 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
5210 }
5211}
5212
5213static void hsw_power_well_post_disable(struct drm_i915_private *dev_priv)
5214{
5215 struct drm_device *dev = dev_priv->dev;
5216 enum pipe p;
5217 unsigned long irqflags;
5218
5219 /*
5220 * After this, the registers on the pipes that are part of the power
5221 * well will become zero, so we have to adjust our counters according to
5222 * that.
5223 *
5224 * FIXME: Should we do this in general in drm_vblank_post_modeset?
5225 */
5226 spin_lock_irqsave(&dev->vbl_lock, irqflags);
5227 for_each_pipe(p)
5228 if (p != PIPE_A)
5229 dev->vblank[p].last = 0;
5230 spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
5231}
5232
Imre Deakc1ca7272013-11-25 17:15:29 +02005233static void hsw_set_power_well(struct drm_device *dev,
5234 struct i915_power_well *power_well, bool enable)
Eugeni Dodonovd0d3e512012-05-09 15:37:16 -03005235{
5236 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanonifa42e232013-01-25 16:59:11 -02005237 bool is_enabled, enable_requested;
5238 uint32_t tmp;
Eugeni Dodonovd0d3e512012-05-09 15:37:16 -03005239
Paulo Zanonid62292c2013-11-27 17:59:22 -02005240 WARN_ON(dev_priv->pc8.enabled);
5241
Paulo Zanonifa42e232013-01-25 16:59:11 -02005242 tmp = I915_READ(HSW_PWR_WELL_DRIVER);
Paulo Zanoni6aedd1f2013-08-02 16:22:25 -03005243 is_enabled = tmp & HSW_PWR_WELL_STATE_ENABLED;
5244 enable_requested = tmp & HSW_PWR_WELL_ENABLE_REQUEST;
Eugeni Dodonovd0d3e512012-05-09 15:37:16 -03005245
Paulo Zanonifa42e232013-01-25 16:59:11 -02005246 if (enable) {
5247 if (!enable_requested)
Paulo Zanoni6aedd1f2013-08-02 16:22:25 -03005248 I915_WRITE(HSW_PWR_WELL_DRIVER,
5249 HSW_PWR_WELL_ENABLE_REQUEST);
Eugeni Dodonovd0d3e512012-05-09 15:37:16 -03005250
Paulo Zanonifa42e232013-01-25 16:59:11 -02005251 if (!is_enabled) {
5252 DRM_DEBUG_KMS("Enabling power well\n");
5253 if (wait_for((I915_READ(HSW_PWR_WELL_DRIVER) &
Paulo Zanoni6aedd1f2013-08-02 16:22:25 -03005254 HSW_PWR_WELL_STATE_ENABLED), 20))
Paulo Zanonifa42e232013-01-25 16:59:11 -02005255 DRM_ERROR("Timeout enabling power well\n");
5256 }
Ben Widawsky596cc112013-11-11 14:46:28 -08005257
Paulo Zanonid5e8fdc2013-12-11 18:50:09 -02005258 hsw_power_well_post_enable(dev_priv);
Paulo Zanonifa42e232013-01-25 16:59:11 -02005259 } else {
5260 if (enable_requested) {
5261 I915_WRITE(HSW_PWR_WELL_DRIVER, 0);
Paulo Zanoni9dbd8fe2013-07-23 10:48:11 -03005262 POSTING_READ(HSW_PWR_WELL_DRIVER);
Paulo Zanonifa42e232013-01-25 16:59:11 -02005263 DRM_DEBUG_KMS("Requesting to disable the power well\n");
Paulo Zanoni9dbd8fe2013-07-23 10:48:11 -03005264
Paulo Zanonid5e8fdc2013-12-11 18:50:09 -02005265 hsw_power_well_post_disable(dev_priv);
Eugeni Dodonovd0d3e512012-05-09 15:37:16 -03005266 }
5267 }
Paulo Zanonifa42e232013-01-25 16:59:11 -02005268}
Eugeni Dodonovd0d3e512012-05-09 15:37:16 -03005269
Imre Deakb4ed4482013-10-25 17:36:49 +03005270static void __intel_power_well_get(struct drm_device *dev,
5271 struct i915_power_well *power_well)
Ville Syrjälä2d66aef2013-09-16 17:38:29 +03005272{
Paulo Zanonid62292c2013-11-27 17:59:22 -02005273 struct drm_i915_private *dev_priv = dev->dev_private;
5274
5275 if (!power_well->count++ && power_well->set) {
5276 hsw_disable_package_c8(dev_priv);
Imre Deakc1ca7272013-11-25 17:15:29 +02005277 power_well->set(dev, power_well, true);
Paulo Zanonid62292c2013-11-27 17:59:22 -02005278 }
Ville Syrjälä2d66aef2013-09-16 17:38:29 +03005279}
5280
Imre Deakb4ed4482013-10-25 17:36:49 +03005281static void __intel_power_well_put(struct drm_device *dev,
5282 struct i915_power_well *power_well)
Ville Syrjälä2d66aef2013-09-16 17:38:29 +03005283{
Paulo Zanonid62292c2013-11-27 17:59:22 -02005284 struct drm_i915_private *dev_priv = dev->dev_private;
5285
Ville Syrjälä2d66aef2013-09-16 17:38:29 +03005286 WARN_ON(!power_well->count);
Imre Deakc1ca7272013-11-25 17:15:29 +02005287
Paulo Zanonid62292c2013-11-27 17:59:22 -02005288 if (!--power_well->count && power_well->set &&
5289 i915_disable_power_well) {
Imre Deakc1ca7272013-11-25 17:15:29 +02005290 power_well->set(dev, power_well, false);
Paulo Zanonid62292c2013-11-27 17:59:22 -02005291 hsw_enable_package_c8(dev_priv);
5292 }
Ville Syrjälä2d66aef2013-09-16 17:38:29 +03005293}
5294
Ville Syrjälä67656252013-09-16 17:38:28 +03005295void intel_display_power_get(struct drm_device *dev,
5296 enum intel_display_power_domain domain)
5297{
5298 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak83c00f52013-10-25 17:36:47 +03005299 struct i915_power_domains *power_domains;
Imre Deakc1ca7272013-11-25 17:15:29 +02005300 struct i915_power_well *power_well;
5301 int i;
Ville Syrjälä67656252013-09-16 17:38:28 +03005302
Imre Deak83c00f52013-10-25 17:36:47 +03005303 power_domains = &dev_priv->power_domains;
5304
5305 mutex_lock(&power_domains->lock);
Imre Deak1da51582013-11-25 17:15:35 +02005306
Imre Deakc1ca7272013-11-25 17:15:29 +02005307 for_each_power_well(i, power_well, BIT(domain), power_domains)
5308 __intel_power_well_get(dev, power_well);
Imre Deak1da51582013-11-25 17:15:35 +02005309
Imre Deakddf9c532013-11-27 22:02:02 +02005310 power_domains->domain_use_count[domain]++;
5311
Imre Deak83c00f52013-10-25 17:36:47 +03005312 mutex_unlock(&power_domains->lock);
Ville Syrjälä67656252013-09-16 17:38:28 +03005313}
5314
5315void intel_display_power_put(struct drm_device *dev,
5316 enum intel_display_power_domain domain)
5317{
5318 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak83c00f52013-10-25 17:36:47 +03005319 struct i915_power_domains *power_domains;
Imre Deakc1ca7272013-11-25 17:15:29 +02005320 struct i915_power_well *power_well;
5321 int i;
Ville Syrjälä67656252013-09-16 17:38:28 +03005322
Imre Deak83c00f52013-10-25 17:36:47 +03005323 power_domains = &dev_priv->power_domains;
5324
5325 mutex_lock(&power_domains->lock);
Imre Deak1da51582013-11-25 17:15:35 +02005326
Imre Deak1da51582013-11-25 17:15:35 +02005327 WARN_ON(!power_domains->domain_use_count[domain]);
5328 power_domains->domain_use_count[domain]--;
Imre Deakddf9c532013-11-27 22:02:02 +02005329
5330 for_each_power_well_rev(i, power_well, BIT(domain), power_domains)
5331 __intel_power_well_put(dev, power_well);
Imre Deak1da51582013-11-25 17:15:35 +02005332
Imre Deak83c00f52013-10-25 17:36:47 +03005333 mutex_unlock(&power_domains->lock);
Ville Syrjälä67656252013-09-16 17:38:28 +03005334}
5335
Imre Deak83c00f52013-10-25 17:36:47 +03005336static struct i915_power_domains *hsw_pwr;
Wang Xingchaoa38911a2013-05-30 22:07:11 +08005337
5338/* Display audio driver power well request */
5339void i915_request_power_well(void)
5340{
Imre Deakb4ed4482013-10-25 17:36:49 +03005341 struct drm_i915_private *dev_priv;
5342
Wang Xingchaoa38911a2013-05-30 22:07:11 +08005343 if (WARN_ON(!hsw_pwr))
5344 return;
5345
Imre Deakb4ed4482013-10-25 17:36:49 +03005346 dev_priv = container_of(hsw_pwr, struct drm_i915_private,
5347 power_domains);
Imre Deakfbeeaa22013-11-25 17:15:28 +02005348 intel_display_power_get(dev_priv->dev, POWER_DOMAIN_AUDIO);
Wang Xingchaoa38911a2013-05-30 22:07:11 +08005349}
5350EXPORT_SYMBOL_GPL(i915_request_power_well);
5351
5352/* Display audio driver power well release */
5353void i915_release_power_well(void)
5354{
Imre Deakb4ed4482013-10-25 17:36:49 +03005355 struct drm_i915_private *dev_priv;
5356
Wang Xingchaoa38911a2013-05-30 22:07:11 +08005357 if (WARN_ON(!hsw_pwr))
5358 return;
5359
Imre Deakb4ed4482013-10-25 17:36:49 +03005360 dev_priv = container_of(hsw_pwr, struct drm_i915_private,
5361 power_domains);
Imre Deakfbeeaa22013-11-25 17:15:28 +02005362 intel_display_power_put(dev_priv->dev, POWER_DOMAIN_AUDIO);
Wang Xingchaoa38911a2013-05-30 22:07:11 +08005363}
5364EXPORT_SYMBOL_GPL(i915_release_power_well);
5365
Imre Deak1c2256d2013-11-25 17:15:34 +02005366static struct i915_power_well i9xx_always_on_power_well[] = {
5367 {
5368 .name = "always-on",
5369 .always_on = 1,
5370 .domains = POWER_DOMAIN_MASK,
5371 },
5372};
5373
Imre Deakc1ca7272013-11-25 17:15:29 +02005374static struct i915_power_well hsw_power_wells[] = {
5375 {
Imre Deak6f3ef5d2013-11-25 17:15:30 +02005376 .name = "always-on",
5377 .always_on = 1,
5378 .domains = HSW_ALWAYS_ON_POWER_DOMAINS,
5379 },
5380 {
Imre Deakc1ca7272013-11-25 17:15:29 +02005381 .name = "display",
5382 .domains = POWER_DOMAIN_MASK & ~HSW_ALWAYS_ON_POWER_DOMAINS,
5383 .is_enabled = hsw_power_well_enabled,
5384 .set = hsw_set_power_well,
5385 },
5386};
5387
5388static struct i915_power_well bdw_power_wells[] = {
5389 {
Imre Deak6f3ef5d2013-11-25 17:15:30 +02005390 .name = "always-on",
5391 .always_on = 1,
5392 .domains = BDW_ALWAYS_ON_POWER_DOMAINS,
5393 },
5394 {
Imre Deakc1ca7272013-11-25 17:15:29 +02005395 .name = "display",
5396 .domains = POWER_DOMAIN_MASK & ~BDW_ALWAYS_ON_POWER_DOMAINS,
5397 .is_enabled = hsw_power_well_enabled,
5398 .set = hsw_set_power_well,
5399 },
5400};
5401
5402#define set_power_wells(power_domains, __power_wells) ({ \
5403 (power_domains)->power_wells = (__power_wells); \
5404 (power_domains)->power_well_count = ARRAY_SIZE(__power_wells); \
5405})
5406
Imre Deakddb642f2013-10-28 17:20:35 +02005407int intel_power_domains_init(struct drm_device *dev)
Wang Xingchaoa38911a2013-05-30 22:07:11 +08005408{
5409 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak83c00f52013-10-25 17:36:47 +03005410 struct i915_power_domains *power_domains = &dev_priv->power_domains;
Imre Deakc1ca7272013-11-25 17:15:29 +02005411
Imre Deak83c00f52013-10-25 17:36:47 +03005412 mutex_init(&power_domains->lock);
Wang Xingchaoa38911a2013-05-30 22:07:11 +08005413
Imre Deakc1ca7272013-11-25 17:15:29 +02005414 /*
5415 * The enabling order will be from lower to higher indexed wells,
5416 * the disabling order is reversed.
5417 */
5418 if (IS_HASWELL(dev)) {
5419 set_power_wells(power_domains, hsw_power_wells);
5420 hsw_pwr = power_domains;
5421 } else if (IS_BROADWELL(dev)) {
5422 set_power_wells(power_domains, bdw_power_wells);
5423 hsw_pwr = power_domains;
5424 } else {
Imre Deak1c2256d2013-11-25 17:15:34 +02005425 set_power_wells(power_domains, i9xx_always_on_power_well);
Imre Deakc1ca7272013-11-25 17:15:29 +02005426 }
Wang Xingchaoa38911a2013-05-30 22:07:11 +08005427
5428 return 0;
5429}
5430
Imre Deakddb642f2013-10-28 17:20:35 +02005431void intel_power_domains_remove(struct drm_device *dev)
Wang Xingchaoa38911a2013-05-30 22:07:11 +08005432{
5433 hsw_pwr = NULL;
5434}
5435
Imre Deakddb642f2013-10-28 17:20:35 +02005436static void intel_power_domains_resume(struct drm_device *dev)
Ville Syrjälä9cdb8262013-09-16 17:38:27 +03005437{
5438 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak83c00f52013-10-25 17:36:47 +03005439 struct i915_power_domains *power_domains = &dev_priv->power_domains;
5440 struct i915_power_well *power_well;
Imre Deakc1ca7272013-11-25 17:15:29 +02005441 int i;
Ville Syrjälä9cdb8262013-09-16 17:38:27 +03005442
Imre Deak83c00f52013-10-25 17:36:47 +03005443 mutex_lock(&power_domains->lock);
Imre Deakc1ca7272013-11-25 17:15:29 +02005444 for_each_power_well(i, power_well, POWER_DOMAIN_MASK, power_domains) {
5445 if (power_well->set)
5446 power_well->set(dev, power_well, power_well->count > 0);
5447 }
Imre Deak83c00f52013-10-25 17:36:47 +03005448 mutex_unlock(&power_domains->lock);
Wang Xingchaoa38911a2013-05-30 22:07:11 +08005449}
5450
Paulo Zanonifa42e232013-01-25 16:59:11 -02005451/*
5452 * Starting with Haswell, we have a "Power Down Well" that can be turned off
5453 * when not needed anymore. We have 4 registers that can request the power well
5454 * to be enabled, and it will only be disabled if none of the registers is
5455 * requesting it to be enabled.
5456 */
Imre Deakddb642f2013-10-28 17:20:35 +02005457void intel_power_domains_init_hw(struct drm_device *dev)
Paulo Zanonifa42e232013-01-25 16:59:11 -02005458{
5459 struct drm_i915_private *dev_priv = dev->dev_private;
Eugeni Dodonovd0d3e512012-05-09 15:37:16 -03005460
Paulo Zanonifa42e232013-01-25 16:59:11 -02005461 /* For now, we need the power well to be always enabled. */
Imre Deakbaa70702013-10-25 17:36:48 +03005462 intel_display_set_init_power(dev, true);
Imre Deakddb642f2013-10-28 17:20:35 +02005463 intel_power_domains_resume(dev);
Eugeni Dodonovd0d3e512012-05-09 15:37:16 -03005464
Imre Deakf7243ac2013-11-25 17:15:33 +02005465 if (!(IS_HASWELL(dev) || IS_BROADWELL(dev)))
5466 return;
5467
Paulo Zanonifa42e232013-01-25 16:59:11 -02005468 /* We're taking over the BIOS, so clear any requests made by it since
5469 * the driver is in charge now. */
Paulo Zanoni6aedd1f2013-08-02 16:22:25 -03005470 if (I915_READ(HSW_PWR_WELL_BIOS) & HSW_PWR_WELL_ENABLE_REQUEST)
Paulo Zanonifa42e232013-01-25 16:59:11 -02005471 I915_WRITE(HSW_PWR_WELL_BIOS, 0);
Eugeni Dodonovd0d3e512012-05-09 15:37:16 -03005472}
5473
Paulo Zanonic67a4702013-08-19 13:18:09 -03005474/* Disables PC8 so we can use the GMBUS and DP AUX interrupts. */
5475void intel_aux_display_runtime_get(struct drm_i915_private *dev_priv)
5476{
5477 hsw_disable_package_c8(dev_priv);
5478}
5479
5480void intel_aux_display_runtime_put(struct drm_i915_private *dev_priv)
5481{
5482 hsw_enable_package_c8(dev_priv);
5483}
5484
Paulo Zanoni8a187452013-12-06 20:32:13 -02005485void intel_runtime_pm_get(struct drm_i915_private *dev_priv)
5486{
5487 struct drm_device *dev = dev_priv->dev;
5488 struct device *device = &dev->pdev->dev;
5489
5490 if (!HAS_RUNTIME_PM(dev))
5491 return;
5492
5493 pm_runtime_get_sync(device);
5494 WARN(dev_priv->pm.suspended, "Device still suspended.\n");
5495}
5496
5497void intel_runtime_pm_put(struct drm_i915_private *dev_priv)
5498{
5499 struct drm_device *dev = dev_priv->dev;
5500 struct device *device = &dev->pdev->dev;
5501
5502 if (!HAS_RUNTIME_PM(dev))
5503 return;
5504
5505 pm_runtime_mark_last_busy(device);
5506 pm_runtime_put_autosuspend(device);
5507}
5508
5509void intel_init_runtime_pm(struct drm_i915_private *dev_priv)
5510{
5511 struct drm_device *dev = dev_priv->dev;
5512 struct device *device = &dev->pdev->dev;
5513
5514 dev_priv->pm.suspended = false;
5515
5516 if (!HAS_RUNTIME_PM(dev))
5517 return;
5518
5519 pm_runtime_set_active(device);
5520
5521 pm_runtime_set_autosuspend_delay(device, 10000); /* 10s */
5522 pm_runtime_mark_last_busy(device);
5523 pm_runtime_use_autosuspend(device);
5524}
5525
5526void intel_fini_runtime_pm(struct drm_i915_private *dev_priv)
5527{
5528 struct drm_device *dev = dev_priv->dev;
5529 struct device *device = &dev->pdev->dev;
5530
5531 if (!HAS_RUNTIME_PM(dev))
5532 return;
5533
5534 /* Make sure we're not suspended first. */
5535 pm_runtime_get_sync(device);
5536 pm_runtime_disable(device);
5537}
5538
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005539/* Set up chip specific power management-related functions */
5540void intel_init_pm(struct drm_device *dev)
5541{
5542 struct drm_i915_private *dev_priv = dev->dev_private;
5543
5544 if (I915_HAS_FBC(dev)) {
Ville Syrjälä40045462013-11-28 17:29:59 +02005545 if (INTEL_INFO(dev)->gen >= 7) {
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005546 dev_priv->display.fbc_enabled = ironlake_fbc_enabled;
Ville Syrjälä40045462013-11-28 17:29:59 +02005547 dev_priv->display.enable_fbc = gen7_enable_fbc;
5548 dev_priv->display.disable_fbc = ironlake_disable_fbc;
5549 } else if (INTEL_INFO(dev)->gen >= 5) {
5550 dev_priv->display.fbc_enabled = ironlake_fbc_enabled;
5551 dev_priv->display.enable_fbc = ironlake_enable_fbc;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005552 dev_priv->display.disable_fbc = ironlake_disable_fbc;
5553 } else if (IS_GM45(dev)) {
5554 dev_priv->display.fbc_enabled = g4x_fbc_enabled;
5555 dev_priv->display.enable_fbc = g4x_enable_fbc;
5556 dev_priv->display.disable_fbc = g4x_disable_fbc;
Ville Syrjälä40045462013-11-28 17:29:59 +02005557 } else {
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005558 dev_priv->display.fbc_enabled = i8xx_fbc_enabled;
5559 dev_priv->display.enable_fbc = i8xx_enable_fbc;
5560 dev_priv->display.disable_fbc = i8xx_disable_fbc;
Ville Syrjälä993495a2013-12-12 17:27:40 +02005561
5562 /* This value was pulled out of someone's hat */
5563 I915_WRITE(FBC_CONTROL, 500 << FBC_CTL_INTERVAL_SHIFT);
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005564 }
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005565 }
5566
Daniel Vetterc921aba2012-04-26 23:28:17 +02005567 /* For cxsr */
5568 if (IS_PINEVIEW(dev))
5569 i915_pineview_get_mem_freq(dev);
5570 else if (IS_GEN5(dev))
5571 i915_ironlake_get_mem_freq(dev);
5572
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005573 /* For FIFO watermark updates */
5574 if (HAS_PCH_SPLIT(dev)) {
Ville Syrjälä53615a52013-08-01 16:18:50 +03005575 intel_setup_wm_latency(dev);
5576
Ville Syrjäläbd602542014-01-07 16:14:10 +02005577 if ((IS_GEN5(dev) && dev_priv->wm.pri_latency[1] &&
5578 dev_priv->wm.spr_latency[1] && dev_priv->wm.cur_latency[1]) ||
5579 (!IS_GEN5(dev) && dev_priv->wm.pri_latency[0] &&
5580 dev_priv->wm.spr_latency[0] && dev_priv->wm.cur_latency[0])) {
5581 dev_priv->display.update_wm = ilk_update_wm;
5582 dev_priv->display.update_sprite_wm = ilk_update_sprite_wm;
5583 } else {
5584 DRM_DEBUG_KMS("Failed to read display plane latency. "
5585 "Disable CxSR\n");
5586 }
5587
5588 if (IS_GEN5(dev))
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005589 dev_priv->display.init_clock_gating = ironlake_init_clock_gating;
Ville Syrjäläbd602542014-01-07 16:14:10 +02005590 else if (IS_GEN6(dev))
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005591 dev_priv->display.init_clock_gating = gen6_init_clock_gating;
Ville Syrjäläbd602542014-01-07 16:14:10 +02005592 else if (IS_IVYBRIDGE(dev))
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005593 dev_priv->display.init_clock_gating = ivybridge_init_clock_gating;
Ville Syrjäläbd602542014-01-07 16:14:10 +02005594 else if (IS_HASWELL(dev))
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03005595 dev_priv->display.init_clock_gating = haswell_init_clock_gating;
Ville Syrjäläbd602542014-01-07 16:14:10 +02005596 else if (INTEL_INFO(dev)->gen == 8)
Ben Widawsky1020a5c2013-11-02 21:07:06 -07005597 dev_priv->display.init_clock_gating = gen8_init_clock_gating;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005598 } else if (IS_VALLEYVIEW(dev)) {
5599 dev_priv->display.update_wm = valleyview_update_wm;
5600 dev_priv->display.init_clock_gating =
5601 valleyview_init_clock_gating;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005602 } else if (IS_PINEVIEW(dev)) {
5603 if (!intel_get_cxsr_latency(IS_PINEVIEW_G(dev),
5604 dev_priv->is_ddr3,
5605 dev_priv->fsb_freq,
5606 dev_priv->mem_freq)) {
5607 DRM_INFO("failed to find known CxSR latency "
5608 "(found ddr%s fsb freq %d, mem freq %d), "
5609 "disabling CxSR\n",
5610 (dev_priv->is_ddr3 == 1) ? "3" : "2",
5611 dev_priv->fsb_freq, dev_priv->mem_freq);
5612 /* Disable CxSR and never update its watermark again */
5613 pineview_disable_cxsr(dev);
5614 dev_priv->display.update_wm = NULL;
5615 } else
5616 dev_priv->display.update_wm = pineview_update_wm;
5617 dev_priv->display.init_clock_gating = gen3_init_clock_gating;
5618 } else if (IS_G4X(dev)) {
5619 dev_priv->display.update_wm = g4x_update_wm;
5620 dev_priv->display.init_clock_gating = g4x_init_clock_gating;
5621 } else if (IS_GEN4(dev)) {
5622 dev_priv->display.update_wm = i965_update_wm;
5623 if (IS_CRESTLINE(dev))
5624 dev_priv->display.init_clock_gating = crestline_init_clock_gating;
5625 else if (IS_BROADWATER(dev))
5626 dev_priv->display.init_clock_gating = broadwater_init_clock_gating;
5627 } else if (IS_GEN3(dev)) {
5628 dev_priv->display.update_wm = i9xx_update_wm;
5629 dev_priv->display.get_fifo_size = i9xx_get_fifo_size;
5630 dev_priv->display.init_clock_gating = gen3_init_clock_gating;
5631 } else if (IS_I865G(dev)) {
5632 dev_priv->display.update_wm = i830_update_wm;
5633 dev_priv->display.init_clock_gating = i85x_init_clock_gating;
5634 dev_priv->display.get_fifo_size = i830_get_fifo_size;
5635 } else if (IS_I85X(dev)) {
5636 dev_priv->display.update_wm = i9xx_update_wm;
5637 dev_priv->display.get_fifo_size = i85x_get_fifo_size;
5638 dev_priv->display.init_clock_gating = i85x_init_clock_gating;
5639 } else {
5640 dev_priv->display.update_wm = i830_update_wm;
5641 dev_priv->display.init_clock_gating = i830_init_clock_gating;
5642 if (IS_845G(dev))
5643 dev_priv->display.get_fifo_size = i845_get_fifo_size;
5644 else
5645 dev_priv->display.get_fifo_size = i830_get_fifo_size;
5646 }
5647}
5648
Ben Widawsky42c05262012-09-26 10:34:00 -07005649int sandybridge_pcode_read(struct drm_i915_private *dev_priv, u8 mbox, u32 *val)
5650{
Jesse Barnes4fc688c2012-11-02 11:14:01 -07005651 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Ben Widawsky42c05262012-09-26 10:34:00 -07005652
5653 if (I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) {
5654 DRM_DEBUG_DRIVER("warning: pcode (read) mailbox access failed\n");
5655 return -EAGAIN;
5656 }
5657
5658 I915_WRITE(GEN6_PCODE_DATA, *val);
5659 I915_WRITE(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY | mbox);
5660
5661 if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
5662 500)) {
5663 DRM_ERROR("timeout waiting for pcode read (%d) to finish\n", mbox);
5664 return -ETIMEDOUT;
5665 }
5666
5667 *val = I915_READ(GEN6_PCODE_DATA);
5668 I915_WRITE(GEN6_PCODE_DATA, 0);
5669
5670 return 0;
5671}
5672
5673int sandybridge_pcode_write(struct drm_i915_private *dev_priv, u8 mbox, u32 val)
5674{
Jesse Barnes4fc688c2012-11-02 11:14:01 -07005675 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Ben Widawsky42c05262012-09-26 10:34:00 -07005676
5677 if (I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) {
5678 DRM_DEBUG_DRIVER("warning: pcode (write) mailbox access failed\n");
5679 return -EAGAIN;
5680 }
5681
5682 I915_WRITE(GEN6_PCODE_DATA, val);
5683 I915_WRITE(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY | mbox);
5684
5685 if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
5686 500)) {
5687 DRM_ERROR("timeout waiting for pcode write (%d) to finish\n", mbox);
5688 return -ETIMEDOUT;
5689 }
5690
5691 I915_WRITE(GEN6_PCODE_DATA, 0);
5692
5693 return 0;
5694}
Jesse Barnesa0e4e192013-04-02 11:23:05 -07005695
Ville Syrjälä2ec38152013-11-05 22:42:29 +02005696int vlv_gpu_freq(struct drm_i915_private *dev_priv, int val)
Jesse Barnes855ba3b2013-04-17 15:54:57 -07005697{
Ville Syrjälä07ab1182013-11-05 22:42:28 +02005698 int div;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07005699
Ville Syrjälä07ab1182013-11-05 22:42:28 +02005700 /* 4 x czclk */
Ville Syrjälä2ec38152013-11-05 22:42:29 +02005701 switch (dev_priv->mem_freq) {
Jesse Barnes855ba3b2013-04-17 15:54:57 -07005702 case 800:
Ville Syrjälä07ab1182013-11-05 22:42:28 +02005703 div = 10;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07005704 break;
5705 case 1066:
Ville Syrjälä07ab1182013-11-05 22:42:28 +02005706 div = 12;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07005707 break;
5708 case 1333:
Ville Syrjälä07ab1182013-11-05 22:42:28 +02005709 div = 16;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07005710 break;
5711 default:
5712 return -1;
5713 }
5714
Ville Syrjälä2ec38152013-11-05 22:42:29 +02005715 return DIV_ROUND_CLOSEST(dev_priv->mem_freq * (val + 6 - 0xbd), 4 * div);
Jesse Barnes855ba3b2013-04-17 15:54:57 -07005716}
5717
Ville Syrjälä2ec38152013-11-05 22:42:29 +02005718int vlv_freq_opcode(struct drm_i915_private *dev_priv, int val)
Jesse Barnes855ba3b2013-04-17 15:54:57 -07005719{
Ville Syrjälä07ab1182013-11-05 22:42:28 +02005720 int mul;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07005721
Ville Syrjälä07ab1182013-11-05 22:42:28 +02005722 /* 4 x czclk */
Ville Syrjälä2ec38152013-11-05 22:42:29 +02005723 switch (dev_priv->mem_freq) {
Jesse Barnes855ba3b2013-04-17 15:54:57 -07005724 case 800:
Ville Syrjälä07ab1182013-11-05 22:42:28 +02005725 mul = 10;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07005726 break;
5727 case 1066:
Ville Syrjälä07ab1182013-11-05 22:42:28 +02005728 mul = 12;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07005729 break;
5730 case 1333:
Ville Syrjälä07ab1182013-11-05 22:42:28 +02005731 mul = 16;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07005732 break;
5733 default:
5734 return -1;
5735 }
5736
Ville Syrjälä2ec38152013-11-05 22:42:29 +02005737 return DIV_ROUND_CLOSEST(4 * mul * val, dev_priv->mem_freq) + 0xbd - 6;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07005738}
5739
Chris Wilson907b28c2013-07-19 20:36:52 +01005740void intel_pm_init(struct drm_device *dev)
5741{
5742 struct drm_i915_private *dev_priv = dev->dev_private;
5743
5744 INIT_DELAYED_WORK(&dev_priv->rps.delayed_resume_work,
5745 intel_gen6_powersave_work);
5746}