blob: adca00783e61b304723d3d05197b53f47b6b33db [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>
Eugeni Dodonov85208be2012-04-16 22:20:34 -030033
Ben Widawsky057d3862012-09-01 22:59:49 -070034#define FORCEWAKE_ACK_TIMEOUT_MS 2
Ben Widawskyb67a4372012-09-01 22:59:47 -070035
Eugeni Dodonovf6750b32012-04-18 11:51:14 -030036/* FBC, or Frame Buffer Compression, is a technique employed to compress the
37 * framebuffer contents in-memory, aiming at reducing the required bandwidth
38 * during in-memory transfers and, therefore, reduce the power packet.
Eugeni Dodonov85208be2012-04-16 22:20:34 -030039 *
Eugeni Dodonovf6750b32012-04-18 11:51:14 -030040 * The benefits of FBC are mostly visible with solid backgrounds and
41 * variation-less patterns.
Eugeni Dodonov85208be2012-04-16 22:20:34 -030042 *
Eugeni Dodonovf6750b32012-04-18 11:51:14 -030043 * FBC-related functionality can be enabled by the means of the
44 * i915.i915_enable_fbc parameter
Eugeni Dodonov85208be2012-04-16 22:20:34 -030045 */
46
Chris Wilson3490ea52013-01-07 10:11:40 +000047static bool intel_crtc_active(struct drm_crtc *crtc)
48{
49 /* Be paranoid as we can arrive here with only partial
50 * state retrieved from the hardware during setup.
51 */
52 return to_intel_crtc(crtc)->active && crtc->fb && crtc->mode.clock;
53}
54
Eugeni Dodonov1fa61102012-04-18 15:29:26 -030055static void i8xx_disable_fbc(struct drm_device *dev)
Eugeni Dodonov85208be2012-04-16 22:20:34 -030056{
57 struct drm_i915_private *dev_priv = dev->dev_private;
58 u32 fbc_ctl;
59
60 /* Disable compression */
61 fbc_ctl = I915_READ(FBC_CONTROL);
62 if ((fbc_ctl & FBC_CTL_EN) == 0)
63 return;
64
65 fbc_ctl &= ~FBC_CTL_EN;
66 I915_WRITE(FBC_CONTROL, fbc_ctl);
67
68 /* Wait for compressing bit to clear */
69 if (wait_for((I915_READ(FBC_STATUS) & FBC_STAT_COMPRESSING) == 0, 10)) {
70 DRM_DEBUG_KMS("FBC idle timed out\n");
71 return;
72 }
73
74 DRM_DEBUG_KMS("disabled FBC\n");
75}
76
Eugeni Dodonov1fa61102012-04-18 15:29:26 -030077static void i8xx_enable_fbc(struct drm_crtc *crtc, unsigned long interval)
Eugeni Dodonov85208be2012-04-16 22:20:34 -030078{
79 struct drm_device *dev = crtc->dev;
80 struct drm_i915_private *dev_priv = dev->dev_private;
81 struct drm_framebuffer *fb = crtc->fb;
82 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
83 struct drm_i915_gem_object *obj = intel_fb->obj;
84 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
85 int cfb_pitch;
86 int plane, i;
87 u32 fbc_ctl, fbc_ctl2;
88
89 cfb_pitch = dev_priv->cfb_size / FBC_LL_SIZE;
90 if (fb->pitches[0] < cfb_pitch)
91 cfb_pitch = fb->pitches[0];
92
93 /* FBC_CTL wants 64B units */
94 cfb_pitch = (cfb_pitch / 64) - 1;
95 plane = intel_crtc->plane == 0 ? FBC_CTL_PLANEA : FBC_CTL_PLANEB;
96
97 /* Clear old tags */
98 for (i = 0; i < (FBC_LL_SIZE / 32) + 1; i++)
99 I915_WRITE(FBC_TAG + (i * 4), 0);
100
101 /* Set it up... */
102 fbc_ctl2 = FBC_CTL_FENCE_DBL | FBC_CTL_IDLE_IMM | FBC_CTL_CPU_FENCE;
103 fbc_ctl2 |= plane;
104 I915_WRITE(FBC_CONTROL2, fbc_ctl2);
105 I915_WRITE(FBC_FENCE_OFF, crtc->y);
106
107 /* enable it... */
108 fbc_ctl = FBC_CTL_EN | FBC_CTL_PERIODIC;
109 if (IS_I945GM(dev))
110 fbc_ctl |= FBC_CTL_C3_IDLE; /* 945 needs special SR handling */
111 fbc_ctl |= (cfb_pitch & 0xff) << FBC_CTL_STRIDE_SHIFT;
112 fbc_ctl |= (interval & 0x2fff) << FBC_CTL_INTERVAL_SHIFT;
113 fbc_ctl |= obj->fence_reg;
114 I915_WRITE(FBC_CONTROL, fbc_ctl);
115
116 DRM_DEBUG_KMS("enabled FBC, pitch %d, yoff %d, plane %d, ",
117 cfb_pitch, crtc->y, intel_crtc->plane);
118}
119
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300120static bool i8xx_fbc_enabled(struct drm_device *dev)
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300121{
122 struct drm_i915_private *dev_priv = dev->dev_private;
123
124 return I915_READ(FBC_CONTROL) & FBC_CTL_EN;
125}
126
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300127static void g4x_enable_fbc(struct drm_crtc *crtc, unsigned long interval)
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300128{
129 struct drm_device *dev = crtc->dev;
130 struct drm_i915_private *dev_priv = dev->dev_private;
131 struct drm_framebuffer *fb = crtc->fb;
132 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
133 struct drm_i915_gem_object *obj = intel_fb->obj;
134 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
135 int plane = intel_crtc->plane == 0 ? DPFC_CTL_PLANEA : DPFC_CTL_PLANEB;
136 unsigned long stall_watermark = 200;
137 u32 dpfc_ctl;
138
139 dpfc_ctl = plane | DPFC_SR_EN | DPFC_CTL_LIMIT_1X;
140 dpfc_ctl |= DPFC_CTL_FENCE_EN | obj->fence_reg;
141 I915_WRITE(DPFC_CHICKEN, DPFC_HT_MODIFY);
142
143 I915_WRITE(DPFC_RECOMP_CTL, DPFC_RECOMP_STALL_EN |
144 (stall_watermark << DPFC_RECOMP_STALL_WM_SHIFT) |
145 (interval << DPFC_RECOMP_TIMER_COUNT_SHIFT));
146 I915_WRITE(DPFC_FENCE_YOFF, crtc->y);
147
148 /* enable it... */
149 I915_WRITE(DPFC_CONTROL, I915_READ(DPFC_CONTROL) | DPFC_CTL_EN);
150
151 DRM_DEBUG_KMS("enabled fbc on plane %d\n", intel_crtc->plane);
152}
153
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300154static void g4x_disable_fbc(struct drm_device *dev)
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300155{
156 struct drm_i915_private *dev_priv = dev->dev_private;
157 u32 dpfc_ctl;
158
159 /* Disable compression */
160 dpfc_ctl = I915_READ(DPFC_CONTROL);
161 if (dpfc_ctl & DPFC_CTL_EN) {
162 dpfc_ctl &= ~DPFC_CTL_EN;
163 I915_WRITE(DPFC_CONTROL, dpfc_ctl);
164
165 DRM_DEBUG_KMS("disabled FBC\n");
166 }
167}
168
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300169static bool g4x_fbc_enabled(struct drm_device *dev)
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300170{
171 struct drm_i915_private *dev_priv = dev->dev_private;
172
173 return I915_READ(DPFC_CONTROL) & DPFC_CTL_EN;
174}
175
176static void sandybridge_blit_fbc_update(struct drm_device *dev)
177{
178 struct drm_i915_private *dev_priv = dev->dev_private;
179 u32 blt_ecoskpd;
180
181 /* Make sure blitter notifies FBC of writes */
182 gen6_gt_force_wake_get(dev_priv);
183 blt_ecoskpd = I915_READ(GEN6_BLITTER_ECOSKPD);
184 blt_ecoskpd |= GEN6_BLITTER_FBC_NOTIFY <<
185 GEN6_BLITTER_LOCK_SHIFT;
186 I915_WRITE(GEN6_BLITTER_ECOSKPD, blt_ecoskpd);
187 blt_ecoskpd |= GEN6_BLITTER_FBC_NOTIFY;
188 I915_WRITE(GEN6_BLITTER_ECOSKPD, blt_ecoskpd);
189 blt_ecoskpd &= ~(GEN6_BLITTER_FBC_NOTIFY <<
190 GEN6_BLITTER_LOCK_SHIFT);
191 I915_WRITE(GEN6_BLITTER_ECOSKPD, blt_ecoskpd);
192 POSTING_READ(GEN6_BLITTER_ECOSKPD);
193 gen6_gt_force_wake_put(dev_priv);
194}
195
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300196static void ironlake_enable_fbc(struct drm_crtc *crtc, unsigned long interval)
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300197{
198 struct drm_device *dev = crtc->dev;
199 struct drm_i915_private *dev_priv = dev->dev_private;
200 struct drm_framebuffer *fb = crtc->fb;
201 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
202 struct drm_i915_gem_object *obj = intel_fb->obj;
203 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
204 int plane = intel_crtc->plane == 0 ? DPFC_CTL_PLANEA : DPFC_CTL_PLANEB;
205 unsigned long stall_watermark = 200;
206 u32 dpfc_ctl;
207
208 dpfc_ctl = I915_READ(ILK_DPFC_CONTROL);
209 dpfc_ctl &= DPFC_RESERVED;
210 dpfc_ctl |= (plane | DPFC_CTL_LIMIT_1X);
211 /* Set persistent mode for front-buffer rendering, ala X. */
212 dpfc_ctl |= DPFC_CTL_PERSISTENT_MODE;
213 dpfc_ctl |= (DPFC_CTL_FENCE_EN | obj->fence_reg);
214 I915_WRITE(ILK_DPFC_CHICKEN, DPFC_HT_MODIFY);
215
216 I915_WRITE(ILK_DPFC_RECOMP_CTL, DPFC_RECOMP_STALL_EN |
217 (stall_watermark << DPFC_RECOMP_STALL_WM_SHIFT) |
218 (interval << DPFC_RECOMP_TIMER_COUNT_SHIFT));
219 I915_WRITE(ILK_DPFC_FENCE_YOFF, crtc->y);
220 I915_WRITE(ILK_FBC_RT_BASE, obj->gtt_offset | ILK_FBC_RT_VALID);
221 /* enable it... */
222 I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl | DPFC_CTL_EN);
223
224 if (IS_GEN6(dev)) {
225 I915_WRITE(SNB_DPFC_CTL_SA,
226 SNB_CPU_FENCE_ENABLE | obj->fence_reg);
227 I915_WRITE(DPFC_CPU_FENCE_OFFSET, crtc->y);
228 sandybridge_blit_fbc_update(dev);
229 }
230
231 DRM_DEBUG_KMS("enabled fbc on plane %d\n", intel_crtc->plane);
232}
233
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300234static void ironlake_disable_fbc(struct drm_device *dev)
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300235{
236 struct drm_i915_private *dev_priv = dev->dev_private;
237 u32 dpfc_ctl;
238
239 /* Disable compression */
240 dpfc_ctl = I915_READ(ILK_DPFC_CONTROL);
241 if (dpfc_ctl & DPFC_CTL_EN) {
242 dpfc_ctl &= ~DPFC_CTL_EN;
243 I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl);
244
245 DRM_DEBUG_KMS("disabled FBC\n");
246 }
247}
248
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300249static bool ironlake_fbc_enabled(struct drm_device *dev)
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300250{
251 struct drm_i915_private *dev_priv = dev->dev_private;
252
253 return I915_READ(ILK_DPFC_CONTROL) & DPFC_CTL_EN;
254}
255
256bool intel_fbc_enabled(struct drm_device *dev)
257{
258 struct drm_i915_private *dev_priv = dev->dev_private;
259
260 if (!dev_priv->display.fbc_enabled)
261 return false;
262
263 return dev_priv->display.fbc_enabled(dev);
264}
265
266static void intel_fbc_work_fn(struct work_struct *__work)
267{
268 struct intel_fbc_work *work =
269 container_of(to_delayed_work(__work),
270 struct intel_fbc_work, work);
271 struct drm_device *dev = work->crtc->dev;
272 struct drm_i915_private *dev_priv = dev->dev_private;
273
274 mutex_lock(&dev->struct_mutex);
275 if (work == dev_priv->fbc_work) {
276 /* Double check that we haven't switched fb without cancelling
277 * the prior work.
278 */
279 if (work->crtc->fb == work->fb) {
280 dev_priv->display.enable_fbc(work->crtc,
281 work->interval);
282
283 dev_priv->cfb_plane = to_intel_crtc(work->crtc)->plane;
284 dev_priv->cfb_fb = work->crtc->fb->base.id;
285 dev_priv->cfb_y = work->crtc->y;
286 }
287
288 dev_priv->fbc_work = NULL;
289 }
290 mutex_unlock(&dev->struct_mutex);
291
292 kfree(work);
293}
294
295static void intel_cancel_fbc_work(struct drm_i915_private *dev_priv)
296{
297 if (dev_priv->fbc_work == NULL)
298 return;
299
300 DRM_DEBUG_KMS("cancelling pending FBC enable\n");
301
302 /* Synchronisation is provided by struct_mutex and checking of
303 * dev_priv->fbc_work, so we can perform the cancellation
304 * entirely asynchronously.
305 */
306 if (cancel_delayed_work(&dev_priv->fbc_work->work))
307 /* tasklet was killed before being run, clean up */
308 kfree(dev_priv->fbc_work);
309
310 /* Mark the work as no longer wanted so that if it does
311 * wake-up (because the work was already running and waiting
312 * for our mutex), it will discover that is no longer
313 * necessary to run.
314 */
315 dev_priv->fbc_work = NULL;
316}
317
318void intel_enable_fbc(struct drm_crtc *crtc, unsigned long interval)
319{
320 struct intel_fbc_work *work;
321 struct drm_device *dev = crtc->dev;
322 struct drm_i915_private *dev_priv = dev->dev_private;
323
324 if (!dev_priv->display.enable_fbc)
325 return;
326
327 intel_cancel_fbc_work(dev_priv);
328
329 work = kzalloc(sizeof *work, GFP_KERNEL);
330 if (work == NULL) {
331 dev_priv->display.enable_fbc(crtc, interval);
332 return;
333 }
334
335 work->crtc = crtc;
336 work->fb = crtc->fb;
337 work->interval = interval;
338 INIT_DELAYED_WORK(&work->work, intel_fbc_work_fn);
339
340 dev_priv->fbc_work = work;
341
342 DRM_DEBUG_KMS("scheduling delayed FBC enable\n");
343
344 /* Delay the actual enabling to let pageflipping cease and the
345 * display to settle before starting the compression. Note that
346 * this delay also serves a second purpose: it allows for a
347 * vblank to pass after disabling the FBC before we attempt
348 * to modify the control registers.
349 *
350 * A more complicated solution would involve tracking vblanks
351 * following the termination of the page-flipping sequence
352 * and indeed performing the enable as a co-routine and not
353 * waiting synchronously upon the vblank.
354 */
355 schedule_delayed_work(&work->work, msecs_to_jiffies(50));
356}
357
358void intel_disable_fbc(struct drm_device *dev)
359{
360 struct drm_i915_private *dev_priv = dev->dev_private;
361
362 intel_cancel_fbc_work(dev_priv);
363
364 if (!dev_priv->display.disable_fbc)
365 return;
366
367 dev_priv->display.disable_fbc(dev);
368 dev_priv->cfb_plane = -1;
369}
370
371/**
372 * intel_update_fbc - enable/disable FBC as needed
373 * @dev: the drm_device
374 *
375 * Set up the framebuffer compression hardware at mode set time. We
376 * enable it if possible:
377 * - plane A only (on pre-965)
378 * - no pixel mulitply/line duplication
379 * - no alpha buffer discard
380 * - no dual wide
381 * - framebuffer <= 2048 in width, 1536 in height
382 *
383 * We can't assume that any compression will take place (worst case),
384 * so the compressed buffer has to be the same size as the uncompressed
385 * one. It also must reside (along with the line length buffer) in
386 * stolen memory.
387 *
388 * We need to enable/disable FBC on a global basis.
389 */
390void intel_update_fbc(struct drm_device *dev)
391{
392 struct drm_i915_private *dev_priv = dev->dev_private;
393 struct drm_crtc *crtc = NULL, *tmp_crtc;
394 struct intel_crtc *intel_crtc;
395 struct drm_framebuffer *fb;
396 struct intel_framebuffer *intel_fb;
397 struct drm_i915_gem_object *obj;
398 int enable_fbc;
399
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300400 if (!i915_powersave)
401 return;
402
403 if (!I915_HAS_FBC(dev))
404 return;
405
406 /*
407 * If FBC is already on, we just have to verify that we can
408 * keep it that way...
409 * Need to disable if:
410 * - more than one pipe is active
411 * - changing FBC params (stride, fence, mode)
412 * - new fb is too large to fit in compressed buffer
413 * - going to an unsupported config (interlace, pixel multiply, etc.)
414 */
415 list_for_each_entry(tmp_crtc, &dev->mode_config.crtc_list, head) {
Chris Wilson3490ea52013-01-07 10:11:40 +0000416 if (intel_crtc_active(tmp_crtc) &&
417 !to_intel_crtc(tmp_crtc)->primary_disabled) {
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300418 if (crtc) {
419 DRM_DEBUG_KMS("more than one pipe active, disabling compression\n");
420 dev_priv->no_fbc_reason = FBC_MULTIPLE_PIPES;
421 goto out_disable;
422 }
423 crtc = tmp_crtc;
424 }
425 }
426
427 if (!crtc || crtc->fb == NULL) {
428 DRM_DEBUG_KMS("no output, disabling\n");
429 dev_priv->no_fbc_reason = FBC_NO_OUTPUT;
430 goto out_disable;
431 }
432
433 intel_crtc = to_intel_crtc(crtc);
434 fb = crtc->fb;
435 intel_fb = to_intel_framebuffer(fb);
436 obj = intel_fb->obj;
437
438 enable_fbc = i915_enable_fbc;
439 if (enable_fbc < 0) {
440 DRM_DEBUG_KMS("fbc set to per-chip default\n");
441 enable_fbc = 1;
442 if (INTEL_INFO(dev)->gen <= 6)
443 enable_fbc = 0;
444 }
445 if (!enable_fbc) {
446 DRM_DEBUG_KMS("fbc disabled per module param\n");
447 dev_priv->no_fbc_reason = FBC_MODULE_PARAM;
448 goto out_disable;
449 }
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300450 if ((crtc->mode.flags & DRM_MODE_FLAG_INTERLACE) ||
451 (crtc->mode.flags & DRM_MODE_FLAG_DBLSCAN)) {
452 DRM_DEBUG_KMS("mode incompatible with compression, "
453 "disabling\n");
454 dev_priv->no_fbc_reason = FBC_UNSUPPORTED_MODE;
455 goto out_disable;
456 }
457 if ((crtc->mode.hdisplay > 2048) ||
458 (crtc->mode.vdisplay > 1536)) {
459 DRM_DEBUG_KMS("mode too large for compression, disabling\n");
460 dev_priv->no_fbc_reason = FBC_MODE_TOO_LARGE;
461 goto out_disable;
462 }
463 if ((IS_I915GM(dev) || IS_I945GM(dev)) && intel_crtc->plane != 0) {
464 DRM_DEBUG_KMS("plane not 0, disabling compression\n");
465 dev_priv->no_fbc_reason = FBC_BAD_PLANE;
466 goto out_disable;
467 }
468
469 /* The use of a CPU fence is mandatory in order to detect writes
470 * by the CPU to the scanout and trigger updates to the FBC.
471 */
472 if (obj->tiling_mode != I915_TILING_X ||
473 obj->fence_reg == I915_FENCE_REG_NONE) {
474 DRM_DEBUG_KMS("framebuffer not tiled or fenced, disabling compression\n");
475 dev_priv->no_fbc_reason = FBC_NOT_TILED;
476 goto out_disable;
477 }
478
479 /* If the kernel debugger is active, always disable compression */
480 if (in_dbg_master())
481 goto out_disable;
482
Chris Wilson11be49e2012-11-15 11:32:20 +0000483 if (i915_gem_stolen_setup_compression(dev, intel_fb->obj->base.size)) {
484 DRM_INFO("not enough stolen space for compressed buffer (need %zd bytes), disabling\n", intel_fb->obj->base.size);
485 DRM_INFO("hint: you may be able to increase stolen memory size in the BIOS to avoid this\n");
486 DRM_DEBUG_KMS("framebuffer too large, disabling compression\n");
487 dev_priv->no_fbc_reason = FBC_STOLEN_TOO_SMALL;
488 goto out_disable;
489 }
490
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300491 /* If the scanout has not changed, don't modify the FBC settings.
492 * Note that we make the fundamental assumption that the fb->obj
493 * cannot be unpinned (and have its GTT offset and fence revoked)
494 * without first being decoupled from the scanout and FBC disabled.
495 */
496 if (dev_priv->cfb_plane == intel_crtc->plane &&
497 dev_priv->cfb_fb == fb->base.id &&
498 dev_priv->cfb_y == crtc->y)
499 return;
500
501 if (intel_fbc_enabled(dev)) {
502 /* We update FBC along two paths, after changing fb/crtc
503 * configuration (modeswitching) and after page-flipping
504 * finishes. For the latter, we know that not only did
505 * we disable the FBC at the start of the page-flip
506 * sequence, but also more than one vblank has passed.
507 *
508 * For the former case of modeswitching, it is possible
509 * to switch between two FBC valid configurations
510 * instantaneously so we do need to disable the FBC
511 * before we can modify its control registers. We also
512 * have to wait for the next vblank for that to take
513 * effect. However, since we delay enabling FBC we can
514 * assume that a vblank has passed since disabling and
515 * that we can safely alter the registers in the deferred
516 * callback.
517 *
518 * In the scenario that we go from a valid to invalid
519 * and then back to valid FBC configuration we have
520 * no strict enforcement that a vblank occurred since
521 * disabling the FBC. However, along all current pipe
522 * disabling paths we do need to wait for a vblank at
523 * some point. And we wait before enabling FBC anyway.
524 */
525 DRM_DEBUG_KMS("disabling active FBC for update\n");
526 intel_disable_fbc(dev);
527 }
528
529 intel_enable_fbc(crtc, 500);
530 return;
531
532out_disable:
533 /* Multiple disables should be harmless */
534 if (intel_fbc_enabled(dev)) {
535 DRM_DEBUG_KMS("unsupported config, disabling FBC\n");
536 intel_disable_fbc(dev);
537 }
Chris Wilson11be49e2012-11-15 11:32:20 +0000538 i915_gem_stolen_cleanup_compression(dev);
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300539}
540
Daniel Vetterc921aba2012-04-26 23:28:17 +0200541static void i915_pineview_get_mem_freq(struct drm_device *dev)
542{
543 drm_i915_private_t *dev_priv = dev->dev_private;
544 u32 tmp;
545
546 tmp = I915_READ(CLKCFG);
547
548 switch (tmp & CLKCFG_FSB_MASK) {
549 case CLKCFG_FSB_533:
550 dev_priv->fsb_freq = 533; /* 133*4 */
551 break;
552 case CLKCFG_FSB_800:
553 dev_priv->fsb_freq = 800; /* 200*4 */
554 break;
555 case CLKCFG_FSB_667:
556 dev_priv->fsb_freq = 667; /* 167*4 */
557 break;
558 case CLKCFG_FSB_400:
559 dev_priv->fsb_freq = 400; /* 100*4 */
560 break;
561 }
562
563 switch (tmp & CLKCFG_MEM_MASK) {
564 case CLKCFG_MEM_533:
565 dev_priv->mem_freq = 533;
566 break;
567 case CLKCFG_MEM_667:
568 dev_priv->mem_freq = 667;
569 break;
570 case CLKCFG_MEM_800:
571 dev_priv->mem_freq = 800;
572 break;
573 }
574
575 /* detect pineview DDR3 setting */
576 tmp = I915_READ(CSHRDDR3CTL);
577 dev_priv->is_ddr3 = (tmp & CSHRDDR3CTL_DDR3) ? 1 : 0;
578}
579
580static void i915_ironlake_get_mem_freq(struct drm_device *dev)
581{
582 drm_i915_private_t *dev_priv = dev->dev_private;
583 u16 ddrpll, csipll;
584
585 ddrpll = I915_READ16(DDRMPLL1);
586 csipll = I915_READ16(CSIPLL0);
587
588 switch (ddrpll & 0xff) {
589 case 0xc:
590 dev_priv->mem_freq = 800;
591 break;
592 case 0x10:
593 dev_priv->mem_freq = 1066;
594 break;
595 case 0x14:
596 dev_priv->mem_freq = 1333;
597 break;
598 case 0x18:
599 dev_priv->mem_freq = 1600;
600 break;
601 default:
602 DRM_DEBUG_DRIVER("unknown memory frequency 0x%02x\n",
603 ddrpll & 0xff);
604 dev_priv->mem_freq = 0;
605 break;
606 }
607
Daniel Vetter20e4d402012-08-08 23:35:39 +0200608 dev_priv->ips.r_t = dev_priv->mem_freq;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200609
610 switch (csipll & 0x3ff) {
611 case 0x00c:
612 dev_priv->fsb_freq = 3200;
613 break;
614 case 0x00e:
615 dev_priv->fsb_freq = 3733;
616 break;
617 case 0x010:
618 dev_priv->fsb_freq = 4266;
619 break;
620 case 0x012:
621 dev_priv->fsb_freq = 4800;
622 break;
623 case 0x014:
624 dev_priv->fsb_freq = 5333;
625 break;
626 case 0x016:
627 dev_priv->fsb_freq = 5866;
628 break;
629 case 0x018:
630 dev_priv->fsb_freq = 6400;
631 break;
632 default:
633 DRM_DEBUG_DRIVER("unknown fsb frequency 0x%04x\n",
634 csipll & 0x3ff);
635 dev_priv->fsb_freq = 0;
636 break;
637 }
638
639 if (dev_priv->fsb_freq == 3200) {
Daniel Vetter20e4d402012-08-08 23:35:39 +0200640 dev_priv->ips.c_m = 0;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200641 } else if (dev_priv->fsb_freq > 3200 && dev_priv->fsb_freq <= 4800) {
Daniel Vetter20e4d402012-08-08 23:35:39 +0200642 dev_priv->ips.c_m = 1;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200643 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +0200644 dev_priv->ips.c_m = 2;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200645 }
646}
647
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300648static const struct cxsr_latency cxsr_latency_table[] = {
649 {1, 0, 800, 400, 3382, 33382, 3983, 33983}, /* DDR2-400 SC */
650 {1, 0, 800, 667, 3354, 33354, 3807, 33807}, /* DDR2-667 SC */
651 {1, 0, 800, 800, 3347, 33347, 3763, 33763}, /* DDR2-800 SC */
652 {1, 1, 800, 667, 6420, 36420, 6873, 36873}, /* DDR3-667 SC */
653 {1, 1, 800, 800, 5902, 35902, 6318, 36318}, /* DDR3-800 SC */
654
655 {1, 0, 667, 400, 3400, 33400, 4021, 34021}, /* DDR2-400 SC */
656 {1, 0, 667, 667, 3372, 33372, 3845, 33845}, /* DDR2-667 SC */
657 {1, 0, 667, 800, 3386, 33386, 3822, 33822}, /* DDR2-800 SC */
658 {1, 1, 667, 667, 6438, 36438, 6911, 36911}, /* DDR3-667 SC */
659 {1, 1, 667, 800, 5941, 35941, 6377, 36377}, /* DDR3-800 SC */
660
661 {1, 0, 400, 400, 3472, 33472, 4173, 34173}, /* DDR2-400 SC */
662 {1, 0, 400, 667, 3443, 33443, 3996, 33996}, /* DDR2-667 SC */
663 {1, 0, 400, 800, 3430, 33430, 3946, 33946}, /* DDR2-800 SC */
664 {1, 1, 400, 667, 6509, 36509, 7062, 37062}, /* DDR3-667 SC */
665 {1, 1, 400, 800, 5985, 35985, 6501, 36501}, /* DDR3-800 SC */
666
667 {0, 0, 800, 400, 3438, 33438, 4065, 34065}, /* DDR2-400 SC */
668 {0, 0, 800, 667, 3410, 33410, 3889, 33889}, /* DDR2-667 SC */
669 {0, 0, 800, 800, 3403, 33403, 3845, 33845}, /* DDR2-800 SC */
670 {0, 1, 800, 667, 6476, 36476, 6955, 36955}, /* DDR3-667 SC */
671 {0, 1, 800, 800, 5958, 35958, 6400, 36400}, /* DDR3-800 SC */
672
673 {0, 0, 667, 400, 3456, 33456, 4103, 34106}, /* DDR2-400 SC */
674 {0, 0, 667, 667, 3428, 33428, 3927, 33927}, /* DDR2-667 SC */
675 {0, 0, 667, 800, 3443, 33443, 3905, 33905}, /* DDR2-800 SC */
676 {0, 1, 667, 667, 6494, 36494, 6993, 36993}, /* DDR3-667 SC */
677 {0, 1, 667, 800, 5998, 35998, 6460, 36460}, /* DDR3-800 SC */
678
679 {0, 0, 400, 400, 3528, 33528, 4255, 34255}, /* DDR2-400 SC */
680 {0, 0, 400, 667, 3500, 33500, 4079, 34079}, /* DDR2-667 SC */
681 {0, 0, 400, 800, 3487, 33487, 4029, 34029}, /* DDR2-800 SC */
682 {0, 1, 400, 667, 6566, 36566, 7145, 37145}, /* DDR3-667 SC */
683 {0, 1, 400, 800, 6042, 36042, 6584, 36584}, /* DDR3-800 SC */
684};
685
Daniel Vetter63c62272012-04-21 23:17:55 +0200686static const struct cxsr_latency *intel_get_cxsr_latency(int is_desktop,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300687 int is_ddr3,
688 int fsb,
689 int mem)
690{
691 const struct cxsr_latency *latency;
692 int i;
693
694 if (fsb == 0 || mem == 0)
695 return NULL;
696
697 for (i = 0; i < ARRAY_SIZE(cxsr_latency_table); i++) {
698 latency = &cxsr_latency_table[i];
699 if (is_desktop == latency->is_desktop &&
700 is_ddr3 == latency->is_ddr3 &&
701 fsb == latency->fsb_freq && mem == latency->mem_freq)
702 return latency;
703 }
704
705 DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
706
707 return NULL;
708}
709
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300710static void pineview_disable_cxsr(struct drm_device *dev)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300711{
712 struct drm_i915_private *dev_priv = dev->dev_private;
713
714 /* deactivate cxsr */
715 I915_WRITE(DSPFW3, I915_READ(DSPFW3) & ~PINEVIEW_SELF_REFRESH_EN);
716}
717
718/*
719 * Latency for FIFO fetches is dependent on several factors:
720 * - memory configuration (speed, channels)
721 * - chipset
722 * - current MCH state
723 * It can be fairly high in some situations, so here we assume a fairly
724 * pessimal value. It's a tradeoff between extra memory fetches (if we
725 * set this value too high, the FIFO will fetch frequently to stay full)
726 * and power consumption (set it too low to save power and we might see
727 * FIFO underruns and display "flicker").
728 *
729 * A value of 5us seems to be a good balance; safe for very low end
730 * platforms but not overly aggressive on lower latency configs.
731 */
732static const int latency_ns = 5000;
733
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300734static int i9xx_get_fifo_size(struct drm_device *dev, int plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300735{
736 struct drm_i915_private *dev_priv = dev->dev_private;
737 uint32_t dsparb = I915_READ(DSPARB);
738 int size;
739
740 size = dsparb & 0x7f;
741 if (plane)
742 size = ((dsparb >> DSPARB_CSTART_SHIFT) & 0x7f) - size;
743
744 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
745 plane ? "B" : "A", size);
746
747 return size;
748}
749
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300750static int i85x_get_fifo_size(struct drm_device *dev, int plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300751{
752 struct drm_i915_private *dev_priv = dev->dev_private;
753 uint32_t dsparb = I915_READ(DSPARB);
754 int size;
755
756 size = dsparb & 0x1ff;
757 if (plane)
758 size = ((dsparb >> DSPARB_BEND_SHIFT) & 0x1ff) - size;
759 size >>= 1; /* Convert to cachelines */
760
761 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
762 plane ? "B" : "A", size);
763
764 return size;
765}
766
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300767static int i845_get_fifo_size(struct drm_device *dev, int plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300768{
769 struct drm_i915_private *dev_priv = dev->dev_private;
770 uint32_t dsparb = I915_READ(DSPARB);
771 int size;
772
773 size = dsparb & 0x7f;
774 size >>= 2; /* Convert to cachelines */
775
776 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
777 plane ? "B" : "A",
778 size);
779
780 return size;
781}
782
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300783static int i830_get_fifo_size(struct drm_device *dev, int plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300784{
785 struct drm_i915_private *dev_priv = dev->dev_private;
786 uint32_t dsparb = I915_READ(DSPARB);
787 int size;
788
789 size = dsparb & 0x7f;
790 size >>= 1; /* Convert to cachelines */
791
792 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
793 plane ? "B" : "A", size);
794
795 return size;
796}
797
798/* Pineview has different values for various configs */
799static const struct intel_watermark_params pineview_display_wm = {
800 PINEVIEW_DISPLAY_FIFO,
801 PINEVIEW_MAX_WM,
802 PINEVIEW_DFT_WM,
803 PINEVIEW_GUARD_WM,
804 PINEVIEW_FIFO_LINE_SIZE
805};
806static const struct intel_watermark_params pineview_display_hplloff_wm = {
807 PINEVIEW_DISPLAY_FIFO,
808 PINEVIEW_MAX_WM,
809 PINEVIEW_DFT_HPLLOFF_WM,
810 PINEVIEW_GUARD_WM,
811 PINEVIEW_FIFO_LINE_SIZE
812};
813static const struct intel_watermark_params pineview_cursor_wm = {
814 PINEVIEW_CURSOR_FIFO,
815 PINEVIEW_CURSOR_MAX_WM,
816 PINEVIEW_CURSOR_DFT_WM,
817 PINEVIEW_CURSOR_GUARD_WM,
818 PINEVIEW_FIFO_LINE_SIZE,
819};
820static const struct intel_watermark_params pineview_cursor_hplloff_wm = {
821 PINEVIEW_CURSOR_FIFO,
822 PINEVIEW_CURSOR_MAX_WM,
823 PINEVIEW_CURSOR_DFT_WM,
824 PINEVIEW_CURSOR_GUARD_WM,
825 PINEVIEW_FIFO_LINE_SIZE
826};
827static const struct intel_watermark_params g4x_wm_info = {
828 G4X_FIFO_SIZE,
829 G4X_MAX_WM,
830 G4X_MAX_WM,
831 2,
832 G4X_FIFO_LINE_SIZE,
833};
834static const struct intel_watermark_params g4x_cursor_wm_info = {
835 I965_CURSOR_FIFO,
836 I965_CURSOR_MAX_WM,
837 I965_CURSOR_DFT_WM,
838 2,
839 G4X_FIFO_LINE_SIZE,
840};
841static const struct intel_watermark_params valleyview_wm_info = {
842 VALLEYVIEW_FIFO_SIZE,
843 VALLEYVIEW_MAX_WM,
844 VALLEYVIEW_MAX_WM,
845 2,
846 G4X_FIFO_LINE_SIZE,
847};
848static const struct intel_watermark_params valleyview_cursor_wm_info = {
849 I965_CURSOR_FIFO,
850 VALLEYVIEW_CURSOR_MAX_WM,
851 I965_CURSOR_DFT_WM,
852 2,
853 G4X_FIFO_LINE_SIZE,
854};
855static const struct intel_watermark_params i965_cursor_wm_info = {
856 I965_CURSOR_FIFO,
857 I965_CURSOR_MAX_WM,
858 I965_CURSOR_DFT_WM,
859 2,
860 I915_FIFO_LINE_SIZE,
861};
862static const struct intel_watermark_params i945_wm_info = {
863 I945_FIFO_SIZE,
864 I915_MAX_WM,
865 1,
866 2,
867 I915_FIFO_LINE_SIZE
868};
869static const struct intel_watermark_params i915_wm_info = {
870 I915_FIFO_SIZE,
871 I915_MAX_WM,
872 1,
873 2,
874 I915_FIFO_LINE_SIZE
875};
876static const struct intel_watermark_params i855_wm_info = {
877 I855GM_FIFO_SIZE,
878 I915_MAX_WM,
879 1,
880 2,
881 I830_FIFO_LINE_SIZE
882};
883static const struct intel_watermark_params i830_wm_info = {
884 I830_FIFO_SIZE,
885 I915_MAX_WM,
886 1,
887 2,
888 I830_FIFO_LINE_SIZE
889};
890
891static const struct intel_watermark_params ironlake_display_wm_info = {
892 ILK_DISPLAY_FIFO,
893 ILK_DISPLAY_MAXWM,
894 ILK_DISPLAY_DFTWM,
895 2,
896 ILK_FIFO_LINE_SIZE
897};
898static const struct intel_watermark_params ironlake_cursor_wm_info = {
899 ILK_CURSOR_FIFO,
900 ILK_CURSOR_MAXWM,
901 ILK_CURSOR_DFTWM,
902 2,
903 ILK_FIFO_LINE_SIZE
904};
905static const struct intel_watermark_params ironlake_display_srwm_info = {
906 ILK_DISPLAY_SR_FIFO,
907 ILK_DISPLAY_MAX_SRWM,
908 ILK_DISPLAY_DFT_SRWM,
909 2,
910 ILK_FIFO_LINE_SIZE
911};
912static const struct intel_watermark_params ironlake_cursor_srwm_info = {
913 ILK_CURSOR_SR_FIFO,
914 ILK_CURSOR_MAX_SRWM,
915 ILK_CURSOR_DFT_SRWM,
916 2,
917 ILK_FIFO_LINE_SIZE
918};
919
920static const struct intel_watermark_params sandybridge_display_wm_info = {
921 SNB_DISPLAY_FIFO,
922 SNB_DISPLAY_MAXWM,
923 SNB_DISPLAY_DFTWM,
924 2,
925 SNB_FIFO_LINE_SIZE
926};
927static const struct intel_watermark_params sandybridge_cursor_wm_info = {
928 SNB_CURSOR_FIFO,
929 SNB_CURSOR_MAXWM,
930 SNB_CURSOR_DFTWM,
931 2,
932 SNB_FIFO_LINE_SIZE
933};
934static const struct intel_watermark_params sandybridge_display_srwm_info = {
935 SNB_DISPLAY_SR_FIFO,
936 SNB_DISPLAY_MAX_SRWM,
937 SNB_DISPLAY_DFT_SRWM,
938 2,
939 SNB_FIFO_LINE_SIZE
940};
941static const struct intel_watermark_params sandybridge_cursor_srwm_info = {
942 SNB_CURSOR_SR_FIFO,
943 SNB_CURSOR_MAX_SRWM,
944 SNB_CURSOR_DFT_SRWM,
945 2,
946 SNB_FIFO_LINE_SIZE
947};
948
949
950/**
951 * intel_calculate_wm - calculate watermark level
952 * @clock_in_khz: pixel clock
953 * @wm: chip FIFO params
954 * @pixel_size: display pixel size
955 * @latency_ns: memory latency for the platform
956 *
957 * Calculate the watermark level (the level at which the display plane will
958 * start fetching from memory again). Each chip has a different display
959 * FIFO size and allocation, so the caller needs to figure that out and pass
960 * in the correct intel_watermark_params structure.
961 *
962 * As the pixel clock runs, the FIFO will be drained at a rate that depends
963 * on the pixel size. When it reaches the watermark level, it'll start
964 * fetching FIFO line sized based chunks from memory until the FIFO fills
965 * past the watermark point. If the FIFO drains completely, a FIFO underrun
966 * will occur, and a display engine hang could result.
967 */
968static unsigned long intel_calculate_wm(unsigned long clock_in_khz,
969 const struct intel_watermark_params *wm,
970 int fifo_size,
971 int pixel_size,
972 unsigned long latency_ns)
973{
974 long entries_required, wm_size;
975
976 /*
977 * Note: we need to make sure we don't overflow for various clock &
978 * latency values.
979 * clocks go from a few thousand to several hundred thousand.
980 * latency is usually a few thousand
981 */
982 entries_required = ((clock_in_khz / 1000) * pixel_size * latency_ns) /
983 1000;
984 entries_required = DIV_ROUND_UP(entries_required, wm->cacheline_size);
985
986 DRM_DEBUG_KMS("FIFO entries required for mode: %ld\n", entries_required);
987
988 wm_size = fifo_size - (entries_required + wm->guard_size);
989
990 DRM_DEBUG_KMS("FIFO watermark level: %ld\n", wm_size);
991
992 /* Don't promote wm_size to unsigned... */
993 if (wm_size > (long)wm->max_wm)
994 wm_size = wm->max_wm;
995 if (wm_size <= 0)
996 wm_size = wm->default_wm;
997 return wm_size;
998}
999
1000static struct drm_crtc *single_enabled_crtc(struct drm_device *dev)
1001{
1002 struct drm_crtc *crtc, *enabled = NULL;
1003
1004 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Chris Wilson3490ea52013-01-07 10:11:40 +00001005 if (intel_crtc_active(crtc)) {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001006 if (enabled)
1007 return NULL;
1008 enabled = crtc;
1009 }
1010 }
1011
1012 return enabled;
1013}
1014
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03001015static void pineview_update_wm(struct drm_device *dev)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001016{
1017 struct drm_i915_private *dev_priv = dev->dev_private;
1018 struct drm_crtc *crtc;
1019 const struct cxsr_latency *latency;
1020 u32 reg;
1021 unsigned long wm;
1022
1023 latency = intel_get_cxsr_latency(IS_PINEVIEW_G(dev), dev_priv->is_ddr3,
1024 dev_priv->fsb_freq, dev_priv->mem_freq);
1025 if (!latency) {
1026 DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
1027 pineview_disable_cxsr(dev);
1028 return;
1029 }
1030
1031 crtc = single_enabled_crtc(dev);
1032 if (crtc) {
1033 int clock = crtc->mode.clock;
1034 int pixel_size = crtc->fb->bits_per_pixel / 8;
1035
1036 /* Display SR */
1037 wm = intel_calculate_wm(clock, &pineview_display_wm,
1038 pineview_display_wm.fifo_size,
1039 pixel_size, latency->display_sr);
1040 reg = I915_READ(DSPFW1);
1041 reg &= ~DSPFW_SR_MASK;
1042 reg |= wm << DSPFW_SR_SHIFT;
1043 I915_WRITE(DSPFW1, reg);
1044 DRM_DEBUG_KMS("DSPFW1 register is %x\n", reg);
1045
1046 /* cursor SR */
1047 wm = intel_calculate_wm(clock, &pineview_cursor_wm,
1048 pineview_display_wm.fifo_size,
1049 pixel_size, latency->cursor_sr);
1050 reg = I915_READ(DSPFW3);
1051 reg &= ~DSPFW_CURSOR_SR_MASK;
1052 reg |= (wm & 0x3f) << DSPFW_CURSOR_SR_SHIFT;
1053 I915_WRITE(DSPFW3, reg);
1054
1055 /* Display HPLL off SR */
1056 wm = intel_calculate_wm(clock, &pineview_display_hplloff_wm,
1057 pineview_display_hplloff_wm.fifo_size,
1058 pixel_size, latency->display_hpll_disable);
1059 reg = I915_READ(DSPFW3);
1060 reg &= ~DSPFW_HPLL_SR_MASK;
1061 reg |= wm & DSPFW_HPLL_SR_MASK;
1062 I915_WRITE(DSPFW3, reg);
1063
1064 /* cursor HPLL off SR */
1065 wm = intel_calculate_wm(clock, &pineview_cursor_hplloff_wm,
1066 pineview_display_hplloff_wm.fifo_size,
1067 pixel_size, latency->cursor_hpll_disable);
1068 reg = I915_READ(DSPFW3);
1069 reg &= ~DSPFW_HPLL_CURSOR_MASK;
1070 reg |= (wm & 0x3f) << DSPFW_HPLL_CURSOR_SHIFT;
1071 I915_WRITE(DSPFW3, reg);
1072 DRM_DEBUG_KMS("DSPFW3 register is %x\n", reg);
1073
1074 /* activate cxsr */
1075 I915_WRITE(DSPFW3,
1076 I915_READ(DSPFW3) | PINEVIEW_SELF_REFRESH_EN);
1077 DRM_DEBUG_KMS("Self-refresh is enabled\n");
1078 } else {
1079 pineview_disable_cxsr(dev);
1080 DRM_DEBUG_KMS("Self-refresh is disabled\n");
1081 }
1082}
1083
1084static bool g4x_compute_wm0(struct drm_device *dev,
1085 int plane,
1086 const struct intel_watermark_params *display,
1087 int display_latency_ns,
1088 const struct intel_watermark_params *cursor,
1089 int cursor_latency_ns,
1090 int *plane_wm,
1091 int *cursor_wm)
1092{
1093 struct drm_crtc *crtc;
1094 int htotal, hdisplay, clock, pixel_size;
1095 int line_time_us, line_count;
1096 int entries, tlb_miss;
1097
1098 crtc = intel_get_crtc_for_plane(dev, plane);
Chris Wilson3490ea52013-01-07 10:11:40 +00001099 if (!intel_crtc_active(crtc)) {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001100 *cursor_wm = cursor->guard_size;
1101 *plane_wm = display->guard_size;
1102 return false;
1103 }
1104
1105 htotal = crtc->mode.htotal;
1106 hdisplay = crtc->mode.hdisplay;
1107 clock = crtc->mode.clock;
1108 pixel_size = crtc->fb->bits_per_pixel / 8;
1109
1110 /* Use the small buffer method to calculate plane watermark */
1111 entries = ((clock * pixel_size / 1000) * display_latency_ns) / 1000;
1112 tlb_miss = display->fifo_size*display->cacheline_size - hdisplay * 8;
1113 if (tlb_miss > 0)
1114 entries += tlb_miss;
1115 entries = DIV_ROUND_UP(entries, display->cacheline_size);
1116 *plane_wm = entries + display->guard_size;
1117 if (*plane_wm > (int)display->max_wm)
1118 *plane_wm = display->max_wm;
1119
1120 /* Use the large buffer method to calculate cursor watermark */
1121 line_time_us = ((htotal * 1000) / clock);
1122 line_count = (cursor_latency_ns / line_time_us + 1000) / 1000;
1123 entries = line_count * 64 * pixel_size;
1124 tlb_miss = cursor->fifo_size*cursor->cacheline_size - hdisplay * 8;
1125 if (tlb_miss > 0)
1126 entries += tlb_miss;
1127 entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
1128 *cursor_wm = entries + cursor->guard_size;
1129 if (*cursor_wm > (int)cursor->max_wm)
1130 *cursor_wm = (int)cursor->max_wm;
1131
1132 return true;
1133}
1134
1135/*
1136 * Check the wm result.
1137 *
1138 * If any calculated watermark values is larger than the maximum value that
1139 * can be programmed into the associated watermark register, that watermark
1140 * must be disabled.
1141 */
1142static bool g4x_check_srwm(struct drm_device *dev,
1143 int display_wm, int cursor_wm,
1144 const struct intel_watermark_params *display,
1145 const struct intel_watermark_params *cursor)
1146{
1147 DRM_DEBUG_KMS("SR watermark: display plane %d, cursor %d\n",
1148 display_wm, cursor_wm);
1149
1150 if (display_wm > display->max_wm) {
1151 DRM_DEBUG_KMS("display watermark is too large(%d/%ld), disabling\n",
1152 display_wm, display->max_wm);
1153 return false;
1154 }
1155
1156 if (cursor_wm > cursor->max_wm) {
1157 DRM_DEBUG_KMS("cursor watermark is too large(%d/%ld), disabling\n",
1158 cursor_wm, cursor->max_wm);
1159 return false;
1160 }
1161
1162 if (!(display_wm || cursor_wm)) {
1163 DRM_DEBUG_KMS("SR latency is 0, disabling\n");
1164 return false;
1165 }
1166
1167 return true;
1168}
1169
1170static bool g4x_compute_srwm(struct drm_device *dev,
1171 int plane,
1172 int latency_ns,
1173 const struct intel_watermark_params *display,
1174 const struct intel_watermark_params *cursor,
1175 int *display_wm, int *cursor_wm)
1176{
1177 struct drm_crtc *crtc;
1178 int hdisplay, htotal, pixel_size, clock;
1179 unsigned long line_time_us;
1180 int line_count, line_size;
1181 int small, large;
1182 int entries;
1183
1184 if (!latency_ns) {
1185 *display_wm = *cursor_wm = 0;
1186 return false;
1187 }
1188
1189 crtc = intel_get_crtc_for_plane(dev, plane);
1190 hdisplay = crtc->mode.hdisplay;
1191 htotal = crtc->mode.htotal;
1192 clock = crtc->mode.clock;
1193 pixel_size = crtc->fb->bits_per_pixel / 8;
1194
1195 line_time_us = (htotal * 1000) / clock;
1196 line_count = (latency_ns / line_time_us + 1000) / 1000;
1197 line_size = hdisplay * pixel_size;
1198
1199 /* Use the minimum of the small and large buffer method for primary */
1200 small = ((clock * pixel_size / 1000) * latency_ns) / 1000;
1201 large = line_count * line_size;
1202
1203 entries = DIV_ROUND_UP(min(small, large), display->cacheline_size);
1204 *display_wm = entries + display->guard_size;
1205
1206 /* calculate the self-refresh watermark for display cursor */
1207 entries = line_count * pixel_size * 64;
1208 entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
1209 *cursor_wm = entries + cursor->guard_size;
1210
1211 return g4x_check_srwm(dev,
1212 *display_wm, *cursor_wm,
1213 display, cursor);
1214}
1215
1216static bool vlv_compute_drain_latency(struct drm_device *dev,
1217 int plane,
1218 int *plane_prec_mult,
1219 int *plane_dl,
1220 int *cursor_prec_mult,
1221 int *cursor_dl)
1222{
1223 struct drm_crtc *crtc;
1224 int clock, pixel_size;
1225 int entries;
1226
1227 crtc = intel_get_crtc_for_plane(dev, plane);
Chris Wilson3490ea52013-01-07 10:11:40 +00001228 if (!intel_crtc_active(crtc))
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001229 return false;
1230
1231 clock = crtc->mode.clock; /* VESA DOT Clock */
1232 pixel_size = crtc->fb->bits_per_pixel / 8; /* BPP */
1233
1234 entries = (clock / 1000) * pixel_size;
1235 *plane_prec_mult = (entries > 256) ?
1236 DRAIN_LATENCY_PRECISION_32 : DRAIN_LATENCY_PRECISION_16;
1237 *plane_dl = (64 * (*plane_prec_mult) * 4) / ((clock / 1000) *
1238 pixel_size);
1239
1240 entries = (clock / 1000) * 4; /* BPP is always 4 for cursor */
1241 *cursor_prec_mult = (entries > 256) ?
1242 DRAIN_LATENCY_PRECISION_32 : DRAIN_LATENCY_PRECISION_16;
1243 *cursor_dl = (64 * (*cursor_prec_mult) * 4) / ((clock / 1000) * 4);
1244
1245 return true;
1246}
1247
1248/*
1249 * Update drain latency registers of memory arbiter
1250 *
1251 * Valleyview SoC has a new memory arbiter and needs drain latency registers
1252 * to be programmed. Each plane has a drain latency multiplier and a drain
1253 * latency value.
1254 */
1255
1256static void vlv_update_drain_latency(struct drm_device *dev)
1257{
1258 struct drm_i915_private *dev_priv = dev->dev_private;
1259 int planea_prec, planea_dl, planeb_prec, planeb_dl;
1260 int cursora_prec, cursora_dl, cursorb_prec, cursorb_dl;
1261 int plane_prec_mult, cursor_prec_mult; /* Precision multiplier is
1262 either 16 or 32 */
1263
1264 /* For plane A, Cursor A */
1265 if (vlv_compute_drain_latency(dev, 0, &plane_prec_mult, &planea_dl,
1266 &cursor_prec_mult, &cursora_dl)) {
1267 cursora_prec = (cursor_prec_mult == DRAIN_LATENCY_PRECISION_32) ?
1268 DDL_CURSORA_PRECISION_32 : DDL_CURSORA_PRECISION_16;
1269 planea_prec = (plane_prec_mult == DRAIN_LATENCY_PRECISION_32) ?
1270 DDL_PLANEA_PRECISION_32 : DDL_PLANEA_PRECISION_16;
1271
1272 I915_WRITE(VLV_DDL1, cursora_prec |
1273 (cursora_dl << DDL_CURSORA_SHIFT) |
1274 planea_prec | planea_dl);
1275 }
1276
1277 /* For plane B, Cursor B */
1278 if (vlv_compute_drain_latency(dev, 1, &plane_prec_mult, &planeb_dl,
1279 &cursor_prec_mult, &cursorb_dl)) {
1280 cursorb_prec = (cursor_prec_mult == DRAIN_LATENCY_PRECISION_32) ?
1281 DDL_CURSORB_PRECISION_32 : DDL_CURSORB_PRECISION_16;
1282 planeb_prec = (plane_prec_mult == DRAIN_LATENCY_PRECISION_32) ?
1283 DDL_PLANEB_PRECISION_32 : DDL_PLANEB_PRECISION_16;
1284
1285 I915_WRITE(VLV_DDL2, cursorb_prec |
1286 (cursorb_dl << DDL_CURSORB_SHIFT) |
1287 planeb_prec | planeb_dl);
1288 }
1289}
1290
1291#define single_plane_enabled(mask) is_power_of_2(mask)
1292
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03001293static void valleyview_update_wm(struct drm_device *dev)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001294{
1295 static const int sr_latency_ns = 12000;
1296 struct drm_i915_private *dev_priv = dev->dev_private;
1297 int planea_wm, planeb_wm, cursora_wm, cursorb_wm;
1298 int plane_sr, cursor_sr;
Chris Wilsonaf6c4572012-12-11 12:01:43 +00001299 int ignore_plane_sr, ignore_cursor_sr;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001300 unsigned int enabled = 0;
1301
1302 vlv_update_drain_latency(dev);
1303
1304 if (g4x_compute_wm0(dev, 0,
1305 &valleyview_wm_info, latency_ns,
1306 &valleyview_cursor_wm_info, latency_ns,
1307 &planea_wm, &cursora_wm))
1308 enabled |= 1;
1309
1310 if (g4x_compute_wm0(dev, 1,
1311 &valleyview_wm_info, latency_ns,
1312 &valleyview_cursor_wm_info, latency_ns,
1313 &planeb_wm, &cursorb_wm))
1314 enabled |= 2;
1315
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001316 if (single_plane_enabled(enabled) &&
1317 g4x_compute_srwm(dev, ffs(enabled) - 1,
1318 sr_latency_ns,
1319 &valleyview_wm_info,
1320 &valleyview_cursor_wm_info,
Chris Wilsonaf6c4572012-12-11 12:01:43 +00001321 &plane_sr, &ignore_cursor_sr) &&
1322 g4x_compute_srwm(dev, ffs(enabled) - 1,
1323 2*sr_latency_ns,
1324 &valleyview_wm_info,
1325 &valleyview_cursor_wm_info,
Chris Wilson52bd02d2012-12-07 10:43:24 +00001326 &ignore_plane_sr, &cursor_sr)) {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001327 I915_WRITE(FW_BLC_SELF_VLV, FW_CSPWRDWNEN);
Chris Wilson52bd02d2012-12-07 10:43:24 +00001328 } else {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001329 I915_WRITE(FW_BLC_SELF_VLV,
1330 I915_READ(FW_BLC_SELF_VLV) & ~FW_CSPWRDWNEN);
Chris Wilson52bd02d2012-12-07 10:43:24 +00001331 plane_sr = cursor_sr = 0;
1332 }
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001333
1334 DRM_DEBUG_KMS("Setting FIFO watermarks - A: plane=%d, cursor=%d, B: plane=%d, cursor=%d, SR: plane=%d, cursor=%d\n",
1335 planea_wm, cursora_wm,
1336 planeb_wm, cursorb_wm,
1337 plane_sr, cursor_sr);
1338
1339 I915_WRITE(DSPFW1,
1340 (plane_sr << DSPFW_SR_SHIFT) |
1341 (cursorb_wm << DSPFW_CURSORB_SHIFT) |
1342 (planeb_wm << DSPFW_PLANEB_SHIFT) |
1343 planea_wm);
1344 I915_WRITE(DSPFW2,
Chris Wilson8c919b22012-12-04 16:33:19 +00001345 (I915_READ(DSPFW2) & ~DSPFW_CURSORA_MASK) |
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001346 (cursora_wm << DSPFW_CURSORA_SHIFT));
1347 I915_WRITE(DSPFW3,
Chris Wilson8c919b22012-12-04 16:33:19 +00001348 (I915_READ(DSPFW3) & ~DSPFW_CURSOR_SR_MASK) |
1349 (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001350}
1351
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03001352static void g4x_update_wm(struct drm_device *dev)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001353{
1354 static const int sr_latency_ns = 12000;
1355 struct drm_i915_private *dev_priv = dev->dev_private;
1356 int planea_wm, planeb_wm, cursora_wm, cursorb_wm;
1357 int plane_sr, cursor_sr;
1358 unsigned int enabled = 0;
1359
1360 if (g4x_compute_wm0(dev, 0,
1361 &g4x_wm_info, latency_ns,
1362 &g4x_cursor_wm_info, latency_ns,
1363 &planea_wm, &cursora_wm))
1364 enabled |= 1;
1365
1366 if (g4x_compute_wm0(dev, 1,
1367 &g4x_wm_info, latency_ns,
1368 &g4x_cursor_wm_info, latency_ns,
1369 &planeb_wm, &cursorb_wm))
1370 enabled |= 2;
1371
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001372 if (single_plane_enabled(enabled) &&
1373 g4x_compute_srwm(dev, ffs(enabled) - 1,
1374 sr_latency_ns,
1375 &g4x_wm_info,
1376 &g4x_cursor_wm_info,
Chris Wilson52bd02d2012-12-07 10:43:24 +00001377 &plane_sr, &cursor_sr)) {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001378 I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_EN);
Chris Wilson52bd02d2012-12-07 10:43:24 +00001379 } else {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001380 I915_WRITE(FW_BLC_SELF,
1381 I915_READ(FW_BLC_SELF) & ~FW_BLC_SELF_EN);
Chris Wilson52bd02d2012-12-07 10:43:24 +00001382 plane_sr = cursor_sr = 0;
1383 }
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001384
1385 DRM_DEBUG_KMS("Setting FIFO watermarks - A: plane=%d, cursor=%d, B: plane=%d, cursor=%d, SR: plane=%d, cursor=%d\n",
1386 planea_wm, cursora_wm,
1387 planeb_wm, cursorb_wm,
1388 plane_sr, cursor_sr);
1389
1390 I915_WRITE(DSPFW1,
1391 (plane_sr << DSPFW_SR_SHIFT) |
1392 (cursorb_wm << DSPFW_CURSORB_SHIFT) |
1393 (planeb_wm << DSPFW_PLANEB_SHIFT) |
1394 planea_wm);
1395 I915_WRITE(DSPFW2,
Chris Wilson8c919b22012-12-04 16:33:19 +00001396 (I915_READ(DSPFW2) & ~DSPFW_CURSORA_MASK) |
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001397 (cursora_wm << DSPFW_CURSORA_SHIFT));
1398 /* HPLL off in SR has some issues on G4x... disable it */
1399 I915_WRITE(DSPFW3,
Chris Wilson8c919b22012-12-04 16:33:19 +00001400 (I915_READ(DSPFW3) & ~(DSPFW_HPLL_SR_EN | DSPFW_CURSOR_SR_MASK)) |
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001401 (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
1402}
1403
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03001404static void i965_update_wm(struct drm_device *dev)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001405{
1406 struct drm_i915_private *dev_priv = dev->dev_private;
1407 struct drm_crtc *crtc;
1408 int srwm = 1;
1409 int cursor_sr = 16;
1410
1411 /* Calc sr entries for one plane configs */
1412 crtc = single_enabled_crtc(dev);
1413 if (crtc) {
1414 /* self-refresh has much higher latency */
1415 static const int sr_latency_ns = 12000;
1416 int clock = crtc->mode.clock;
1417 int htotal = crtc->mode.htotal;
1418 int hdisplay = crtc->mode.hdisplay;
1419 int pixel_size = crtc->fb->bits_per_pixel / 8;
1420 unsigned long line_time_us;
1421 int entries;
1422
1423 line_time_us = ((htotal * 1000) / clock);
1424
1425 /* Use ns/us then divide to preserve precision */
1426 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
1427 pixel_size * hdisplay;
1428 entries = DIV_ROUND_UP(entries, I915_FIFO_LINE_SIZE);
1429 srwm = I965_FIFO_SIZE - entries;
1430 if (srwm < 0)
1431 srwm = 1;
1432 srwm &= 0x1ff;
1433 DRM_DEBUG_KMS("self-refresh entries: %d, wm: %d\n",
1434 entries, srwm);
1435
1436 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
1437 pixel_size * 64;
1438 entries = DIV_ROUND_UP(entries,
1439 i965_cursor_wm_info.cacheline_size);
1440 cursor_sr = i965_cursor_wm_info.fifo_size -
1441 (entries + i965_cursor_wm_info.guard_size);
1442
1443 if (cursor_sr > i965_cursor_wm_info.max_wm)
1444 cursor_sr = i965_cursor_wm_info.max_wm;
1445
1446 DRM_DEBUG_KMS("self-refresh watermark: display plane %d "
1447 "cursor %d\n", srwm, cursor_sr);
1448
1449 if (IS_CRESTLINE(dev))
1450 I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_EN);
1451 } else {
1452 /* Turn off self refresh if both pipes are enabled */
1453 if (IS_CRESTLINE(dev))
1454 I915_WRITE(FW_BLC_SELF, I915_READ(FW_BLC_SELF)
1455 & ~FW_BLC_SELF_EN);
1456 }
1457
1458 DRM_DEBUG_KMS("Setting FIFO watermarks - A: 8, B: 8, C: 8, SR %d\n",
1459 srwm);
1460
1461 /* 965 has limitations... */
1462 I915_WRITE(DSPFW1, (srwm << DSPFW_SR_SHIFT) |
1463 (8 << 16) | (8 << 8) | (8 << 0));
1464 I915_WRITE(DSPFW2, (8 << 8) | (8 << 0));
1465 /* update cursor SR watermark */
1466 I915_WRITE(DSPFW3, (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
1467}
1468
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03001469static void i9xx_update_wm(struct drm_device *dev)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001470{
1471 struct drm_i915_private *dev_priv = dev->dev_private;
1472 const struct intel_watermark_params *wm_info;
1473 uint32_t fwater_lo;
1474 uint32_t fwater_hi;
1475 int cwm, srwm = 1;
1476 int fifo_size;
1477 int planea_wm, planeb_wm;
1478 struct drm_crtc *crtc, *enabled = NULL;
1479
1480 if (IS_I945GM(dev))
1481 wm_info = &i945_wm_info;
1482 else if (!IS_GEN2(dev))
1483 wm_info = &i915_wm_info;
1484 else
1485 wm_info = &i855_wm_info;
1486
1487 fifo_size = dev_priv->display.get_fifo_size(dev, 0);
1488 crtc = intel_get_crtc_for_plane(dev, 0);
Chris Wilson3490ea52013-01-07 10:11:40 +00001489 if (intel_crtc_active(crtc)) {
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001490 int cpp = crtc->fb->bits_per_pixel / 8;
1491 if (IS_GEN2(dev))
1492 cpp = 4;
1493
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001494 planea_wm = intel_calculate_wm(crtc->mode.clock,
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001495 wm_info, fifo_size, cpp,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001496 latency_ns);
1497 enabled = crtc;
1498 } else
1499 planea_wm = fifo_size - wm_info->guard_size;
1500
1501 fifo_size = dev_priv->display.get_fifo_size(dev, 1);
1502 crtc = intel_get_crtc_for_plane(dev, 1);
Chris Wilson3490ea52013-01-07 10:11:40 +00001503 if (intel_crtc_active(crtc)) {
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001504 int cpp = crtc->fb->bits_per_pixel / 8;
1505 if (IS_GEN2(dev))
1506 cpp = 4;
1507
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001508 planeb_wm = intel_calculate_wm(crtc->mode.clock,
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001509 wm_info, fifo_size, cpp,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001510 latency_ns);
1511 if (enabled == NULL)
1512 enabled = crtc;
1513 else
1514 enabled = NULL;
1515 } else
1516 planeb_wm = fifo_size - wm_info->guard_size;
1517
1518 DRM_DEBUG_KMS("FIFO watermarks - A: %d, B: %d\n", planea_wm, planeb_wm);
1519
1520 /*
1521 * Overlay gets an aggressive default since video jitter is bad.
1522 */
1523 cwm = 2;
1524
1525 /* Play safe and disable self-refresh before adjusting watermarks. */
1526 if (IS_I945G(dev) || IS_I945GM(dev))
1527 I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_EN_MASK | 0);
1528 else if (IS_I915GM(dev))
1529 I915_WRITE(INSTPM, I915_READ(INSTPM) & ~INSTPM_SELF_EN);
1530
1531 /* Calc sr entries for one plane configs */
1532 if (HAS_FW_BLC(dev) && enabled) {
1533 /* self-refresh has much higher latency */
1534 static const int sr_latency_ns = 6000;
1535 int clock = enabled->mode.clock;
1536 int htotal = enabled->mode.htotal;
1537 int hdisplay = enabled->mode.hdisplay;
1538 int pixel_size = enabled->fb->bits_per_pixel / 8;
1539 unsigned long line_time_us;
1540 int entries;
1541
1542 line_time_us = (htotal * 1000) / clock;
1543
1544 /* Use ns/us then divide to preserve precision */
1545 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
1546 pixel_size * hdisplay;
1547 entries = DIV_ROUND_UP(entries, wm_info->cacheline_size);
1548 DRM_DEBUG_KMS("self-refresh entries: %d\n", entries);
1549 srwm = wm_info->fifo_size - entries;
1550 if (srwm < 0)
1551 srwm = 1;
1552
1553 if (IS_I945G(dev) || IS_I945GM(dev))
1554 I915_WRITE(FW_BLC_SELF,
1555 FW_BLC_SELF_FIFO_MASK | (srwm & 0xff));
1556 else if (IS_I915GM(dev))
1557 I915_WRITE(FW_BLC_SELF, srwm & 0x3f);
1558 }
1559
1560 DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d, B: %d, C: %d, SR %d\n",
1561 planea_wm, planeb_wm, cwm, srwm);
1562
1563 fwater_lo = ((planeb_wm & 0x3f) << 16) | (planea_wm & 0x3f);
1564 fwater_hi = (cwm & 0x1f);
1565
1566 /* Set request length to 8 cachelines per fetch */
1567 fwater_lo = fwater_lo | (1 << 24) | (1 << 8);
1568 fwater_hi = fwater_hi | (1 << 8);
1569
1570 I915_WRITE(FW_BLC, fwater_lo);
1571 I915_WRITE(FW_BLC2, fwater_hi);
1572
1573 if (HAS_FW_BLC(dev)) {
1574 if (enabled) {
1575 if (IS_I945G(dev) || IS_I945GM(dev))
1576 I915_WRITE(FW_BLC_SELF,
1577 FW_BLC_SELF_EN_MASK | FW_BLC_SELF_EN);
1578 else if (IS_I915GM(dev))
1579 I915_WRITE(INSTPM, I915_READ(INSTPM) | INSTPM_SELF_EN);
1580 DRM_DEBUG_KMS("memory self refresh enabled\n");
1581 } else
1582 DRM_DEBUG_KMS("memory self refresh disabled\n");
1583 }
1584}
1585
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03001586static void i830_update_wm(struct drm_device *dev)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001587{
1588 struct drm_i915_private *dev_priv = dev->dev_private;
1589 struct drm_crtc *crtc;
1590 uint32_t fwater_lo;
1591 int planea_wm;
1592
1593 crtc = single_enabled_crtc(dev);
1594 if (crtc == NULL)
1595 return;
1596
1597 planea_wm = intel_calculate_wm(crtc->mode.clock, &i830_wm_info,
1598 dev_priv->display.get_fifo_size(dev, 0),
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001599 4, latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001600 fwater_lo = I915_READ(FW_BLC) & ~0xfff;
1601 fwater_lo |= (3<<8) | planea_wm;
1602
1603 DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d\n", planea_wm);
1604
1605 I915_WRITE(FW_BLC, fwater_lo);
1606}
1607
1608#define ILK_LP0_PLANE_LATENCY 700
1609#define ILK_LP0_CURSOR_LATENCY 1300
1610
1611/*
1612 * Check the wm result.
1613 *
1614 * If any calculated watermark values is larger than the maximum value that
1615 * can be programmed into the associated watermark register, that watermark
1616 * must be disabled.
1617 */
1618static bool ironlake_check_srwm(struct drm_device *dev, int level,
1619 int fbc_wm, int display_wm, int cursor_wm,
1620 const struct intel_watermark_params *display,
1621 const struct intel_watermark_params *cursor)
1622{
1623 struct drm_i915_private *dev_priv = dev->dev_private;
1624
1625 DRM_DEBUG_KMS("watermark %d: display plane %d, fbc lines %d,"
1626 " cursor %d\n", level, display_wm, fbc_wm, cursor_wm);
1627
1628 if (fbc_wm > SNB_FBC_MAX_SRWM) {
1629 DRM_DEBUG_KMS("fbc watermark(%d) is too large(%d), disabling wm%d+\n",
1630 fbc_wm, SNB_FBC_MAX_SRWM, level);
1631
1632 /* fbc has it's own way to disable FBC WM */
1633 I915_WRITE(DISP_ARB_CTL,
1634 I915_READ(DISP_ARB_CTL) | DISP_FBC_WM_DIS);
1635 return false;
1636 }
1637
1638 if (display_wm > display->max_wm) {
1639 DRM_DEBUG_KMS("display watermark(%d) is too large(%d), disabling wm%d+\n",
1640 display_wm, SNB_DISPLAY_MAX_SRWM, level);
1641 return false;
1642 }
1643
1644 if (cursor_wm > cursor->max_wm) {
1645 DRM_DEBUG_KMS("cursor watermark(%d) is too large(%d), disabling wm%d+\n",
1646 cursor_wm, SNB_CURSOR_MAX_SRWM, level);
1647 return false;
1648 }
1649
1650 if (!(fbc_wm || display_wm || cursor_wm)) {
1651 DRM_DEBUG_KMS("latency %d is 0, disabling wm%d+\n", level, level);
1652 return false;
1653 }
1654
1655 return true;
1656}
1657
1658/*
1659 * Compute watermark values of WM[1-3],
1660 */
1661static bool ironlake_compute_srwm(struct drm_device *dev, int level, int plane,
1662 int latency_ns,
1663 const struct intel_watermark_params *display,
1664 const struct intel_watermark_params *cursor,
1665 int *fbc_wm, int *display_wm, int *cursor_wm)
1666{
1667 struct drm_crtc *crtc;
1668 unsigned long line_time_us;
1669 int hdisplay, htotal, pixel_size, clock;
1670 int line_count, line_size;
1671 int small, large;
1672 int entries;
1673
1674 if (!latency_ns) {
1675 *fbc_wm = *display_wm = *cursor_wm = 0;
1676 return false;
1677 }
1678
1679 crtc = intel_get_crtc_for_plane(dev, plane);
1680 hdisplay = crtc->mode.hdisplay;
1681 htotal = crtc->mode.htotal;
1682 clock = crtc->mode.clock;
1683 pixel_size = crtc->fb->bits_per_pixel / 8;
1684
1685 line_time_us = (htotal * 1000) / clock;
1686 line_count = (latency_ns / line_time_us + 1000) / 1000;
1687 line_size = hdisplay * pixel_size;
1688
1689 /* Use the minimum of the small and large buffer method for primary */
1690 small = ((clock * pixel_size / 1000) * latency_ns) / 1000;
1691 large = line_count * line_size;
1692
1693 entries = DIV_ROUND_UP(min(small, large), display->cacheline_size);
1694 *display_wm = entries + display->guard_size;
1695
1696 /*
1697 * Spec says:
1698 * FBC WM = ((Final Primary WM * 64) / number of bytes per line) + 2
1699 */
1700 *fbc_wm = DIV_ROUND_UP(*display_wm * 64, line_size) + 2;
1701
1702 /* calculate the self-refresh watermark for display cursor */
1703 entries = line_count * pixel_size * 64;
1704 entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
1705 *cursor_wm = entries + cursor->guard_size;
1706
1707 return ironlake_check_srwm(dev, level,
1708 *fbc_wm, *display_wm, *cursor_wm,
1709 display, cursor);
1710}
1711
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03001712static void ironlake_update_wm(struct drm_device *dev)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001713{
1714 struct drm_i915_private *dev_priv = dev->dev_private;
1715 int fbc_wm, plane_wm, cursor_wm;
1716 unsigned int enabled;
1717
1718 enabled = 0;
1719 if (g4x_compute_wm0(dev, 0,
1720 &ironlake_display_wm_info,
1721 ILK_LP0_PLANE_LATENCY,
1722 &ironlake_cursor_wm_info,
1723 ILK_LP0_CURSOR_LATENCY,
1724 &plane_wm, &cursor_wm)) {
1725 I915_WRITE(WM0_PIPEA_ILK,
1726 (plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm);
1727 DRM_DEBUG_KMS("FIFO watermarks For pipe A -"
1728 " plane %d, " "cursor: %d\n",
1729 plane_wm, cursor_wm);
1730 enabled |= 1;
1731 }
1732
1733 if (g4x_compute_wm0(dev, 1,
1734 &ironlake_display_wm_info,
1735 ILK_LP0_PLANE_LATENCY,
1736 &ironlake_cursor_wm_info,
1737 ILK_LP0_CURSOR_LATENCY,
1738 &plane_wm, &cursor_wm)) {
1739 I915_WRITE(WM0_PIPEB_ILK,
1740 (plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm);
1741 DRM_DEBUG_KMS("FIFO watermarks For pipe B -"
1742 " plane %d, cursor: %d\n",
1743 plane_wm, cursor_wm);
1744 enabled |= 2;
1745 }
1746
1747 /*
1748 * Calculate and update the self-refresh watermark only when one
1749 * display plane is used.
1750 */
1751 I915_WRITE(WM3_LP_ILK, 0);
1752 I915_WRITE(WM2_LP_ILK, 0);
1753 I915_WRITE(WM1_LP_ILK, 0);
1754
1755 if (!single_plane_enabled(enabled))
1756 return;
1757 enabled = ffs(enabled) - 1;
1758
1759 /* WM1 */
1760 if (!ironlake_compute_srwm(dev, 1, enabled,
1761 ILK_READ_WM1_LATENCY() * 500,
1762 &ironlake_display_srwm_info,
1763 &ironlake_cursor_srwm_info,
1764 &fbc_wm, &plane_wm, &cursor_wm))
1765 return;
1766
1767 I915_WRITE(WM1_LP_ILK,
1768 WM1_LP_SR_EN |
1769 (ILK_READ_WM1_LATENCY() << WM1_LP_LATENCY_SHIFT) |
1770 (fbc_wm << WM1_LP_FBC_SHIFT) |
1771 (plane_wm << WM1_LP_SR_SHIFT) |
1772 cursor_wm);
1773
1774 /* WM2 */
1775 if (!ironlake_compute_srwm(dev, 2, enabled,
1776 ILK_READ_WM2_LATENCY() * 500,
1777 &ironlake_display_srwm_info,
1778 &ironlake_cursor_srwm_info,
1779 &fbc_wm, &plane_wm, &cursor_wm))
1780 return;
1781
1782 I915_WRITE(WM2_LP_ILK,
1783 WM2_LP_EN |
1784 (ILK_READ_WM2_LATENCY() << WM1_LP_LATENCY_SHIFT) |
1785 (fbc_wm << WM1_LP_FBC_SHIFT) |
1786 (plane_wm << WM1_LP_SR_SHIFT) |
1787 cursor_wm);
1788
1789 /*
1790 * WM3 is unsupported on ILK, probably because we don't have latency
1791 * data for that power state
1792 */
1793}
1794
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03001795static void sandybridge_update_wm(struct drm_device *dev)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001796{
1797 struct drm_i915_private *dev_priv = dev->dev_private;
1798 int latency = SNB_READ_WM0_LATENCY() * 100; /* In unit 0.1us */
1799 u32 val;
1800 int fbc_wm, plane_wm, cursor_wm;
1801 unsigned int enabled;
1802
1803 enabled = 0;
1804 if (g4x_compute_wm0(dev, 0,
1805 &sandybridge_display_wm_info, latency,
1806 &sandybridge_cursor_wm_info, latency,
1807 &plane_wm, &cursor_wm)) {
1808 val = I915_READ(WM0_PIPEA_ILK);
1809 val &= ~(WM0_PIPE_PLANE_MASK | WM0_PIPE_CURSOR_MASK);
1810 I915_WRITE(WM0_PIPEA_ILK, val |
1811 ((plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm));
1812 DRM_DEBUG_KMS("FIFO watermarks For pipe A -"
1813 " plane %d, " "cursor: %d\n",
1814 plane_wm, cursor_wm);
1815 enabled |= 1;
1816 }
1817
1818 if (g4x_compute_wm0(dev, 1,
1819 &sandybridge_display_wm_info, latency,
1820 &sandybridge_cursor_wm_info, latency,
1821 &plane_wm, &cursor_wm)) {
1822 val = I915_READ(WM0_PIPEB_ILK);
1823 val &= ~(WM0_PIPE_PLANE_MASK | WM0_PIPE_CURSOR_MASK);
1824 I915_WRITE(WM0_PIPEB_ILK, val |
1825 ((plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm));
1826 DRM_DEBUG_KMS("FIFO watermarks For pipe B -"
1827 " plane %d, cursor: %d\n",
1828 plane_wm, cursor_wm);
1829 enabled |= 2;
1830 }
1831
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001832 /*
1833 * Calculate and update the self-refresh watermark only when one
1834 * display plane is used.
1835 *
1836 * SNB support 3 levels of watermark.
1837 *
1838 * WM1/WM2/WM2 watermarks have to be enabled in the ascending order,
1839 * and disabled in the descending order
1840 *
1841 */
1842 I915_WRITE(WM3_LP_ILK, 0);
1843 I915_WRITE(WM2_LP_ILK, 0);
1844 I915_WRITE(WM1_LP_ILK, 0);
1845
1846 if (!single_plane_enabled(enabled) ||
1847 dev_priv->sprite_scaling_enabled)
1848 return;
1849 enabled = ffs(enabled) - 1;
1850
1851 /* WM1 */
1852 if (!ironlake_compute_srwm(dev, 1, enabled,
1853 SNB_READ_WM1_LATENCY() * 500,
1854 &sandybridge_display_srwm_info,
1855 &sandybridge_cursor_srwm_info,
1856 &fbc_wm, &plane_wm, &cursor_wm))
1857 return;
1858
1859 I915_WRITE(WM1_LP_ILK,
1860 WM1_LP_SR_EN |
1861 (SNB_READ_WM1_LATENCY() << WM1_LP_LATENCY_SHIFT) |
1862 (fbc_wm << WM1_LP_FBC_SHIFT) |
1863 (plane_wm << WM1_LP_SR_SHIFT) |
1864 cursor_wm);
1865
1866 /* WM2 */
1867 if (!ironlake_compute_srwm(dev, 2, enabled,
1868 SNB_READ_WM2_LATENCY() * 500,
1869 &sandybridge_display_srwm_info,
1870 &sandybridge_cursor_srwm_info,
1871 &fbc_wm, &plane_wm, &cursor_wm))
1872 return;
1873
1874 I915_WRITE(WM2_LP_ILK,
1875 WM2_LP_EN |
1876 (SNB_READ_WM2_LATENCY() << WM1_LP_LATENCY_SHIFT) |
1877 (fbc_wm << WM1_LP_FBC_SHIFT) |
1878 (plane_wm << WM1_LP_SR_SHIFT) |
1879 cursor_wm);
1880
1881 /* WM3 */
1882 if (!ironlake_compute_srwm(dev, 3, enabled,
1883 SNB_READ_WM3_LATENCY() * 500,
1884 &sandybridge_display_srwm_info,
1885 &sandybridge_cursor_srwm_info,
1886 &fbc_wm, &plane_wm, &cursor_wm))
1887 return;
1888
1889 I915_WRITE(WM3_LP_ILK,
1890 WM3_LP_EN |
1891 (SNB_READ_WM3_LATENCY() << WM1_LP_LATENCY_SHIFT) |
1892 (fbc_wm << WM1_LP_FBC_SHIFT) |
1893 (plane_wm << WM1_LP_SR_SHIFT) |
1894 cursor_wm);
1895}
1896
Chris Wilsonc43d0182012-12-11 12:01:42 +00001897static void ivybridge_update_wm(struct drm_device *dev)
1898{
1899 struct drm_i915_private *dev_priv = dev->dev_private;
1900 int latency = SNB_READ_WM0_LATENCY() * 100; /* In unit 0.1us */
1901 u32 val;
1902 int fbc_wm, plane_wm, cursor_wm;
1903 int ignore_fbc_wm, ignore_plane_wm, ignore_cursor_wm;
1904 unsigned int enabled;
1905
1906 enabled = 0;
1907 if (g4x_compute_wm0(dev, 0,
1908 &sandybridge_display_wm_info, latency,
1909 &sandybridge_cursor_wm_info, latency,
1910 &plane_wm, &cursor_wm)) {
1911 val = I915_READ(WM0_PIPEA_ILK);
1912 val &= ~(WM0_PIPE_PLANE_MASK | WM0_PIPE_CURSOR_MASK);
1913 I915_WRITE(WM0_PIPEA_ILK, val |
1914 ((plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm));
1915 DRM_DEBUG_KMS("FIFO watermarks For pipe A -"
1916 " plane %d, " "cursor: %d\n",
1917 plane_wm, cursor_wm);
1918 enabled |= 1;
1919 }
1920
1921 if (g4x_compute_wm0(dev, 1,
1922 &sandybridge_display_wm_info, latency,
1923 &sandybridge_cursor_wm_info, latency,
1924 &plane_wm, &cursor_wm)) {
1925 val = I915_READ(WM0_PIPEB_ILK);
1926 val &= ~(WM0_PIPE_PLANE_MASK | WM0_PIPE_CURSOR_MASK);
1927 I915_WRITE(WM0_PIPEB_ILK, val |
1928 ((plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm));
1929 DRM_DEBUG_KMS("FIFO watermarks For pipe B -"
1930 " plane %d, cursor: %d\n",
1931 plane_wm, cursor_wm);
1932 enabled |= 2;
1933 }
1934
1935 if (g4x_compute_wm0(dev, 2,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001936 &sandybridge_display_wm_info, latency,
1937 &sandybridge_cursor_wm_info, latency,
1938 &plane_wm, &cursor_wm)) {
1939 val = I915_READ(WM0_PIPEC_IVB);
1940 val &= ~(WM0_PIPE_PLANE_MASK | WM0_PIPE_CURSOR_MASK);
1941 I915_WRITE(WM0_PIPEC_IVB, val |
1942 ((plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm));
1943 DRM_DEBUG_KMS("FIFO watermarks For pipe C -"
1944 " plane %d, cursor: %d\n",
1945 plane_wm, cursor_wm);
1946 enabled |= 3;
1947 }
1948
1949 /*
1950 * Calculate and update the self-refresh watermark only when one
1951 * display plane is used.
1952 *
1953 * SNB support 3 levels of watermark.
1954 *
1955 * WM1/WM2/WM2 watermarks have to be enabled in the ascending order,
1956 * and disabled in the descending order
1957 *
1958 */
1959 I915_WRITE(WM3_LP_ILK, 0);
1960 I915_WRITE(WM2_LP_ILK, 0);
1961 I915_WRITE(WM1_LP_ILK, 0);
1962
1963 if (!single_plane_enabled(enabled) ||
1964 dev_priv->sprite_scaling_enabled)
1965 return;
1966 enabled = ffs(enabled) - 1;
1967
1968 /* WM1 */
1969 if (!ironlake_compute_srwm(dev, 1, enabled,
1970 SNB_READ_WM1_LATENCY() * 500,
1971 &sandybridge_display_srwm_info,
1972 &sandybridge_cursor_srwm_info,
1973 &fbc_wm, &plane_wm, &cursor_wm))
1974 return;
1975
1976 I915_WRITE(WM1_LP_ILK,
1977 WM1_LP_SR_EN |
1978 (SNB_READ_WM1_LATENCY() << WM1_LP_LATENCY_SHIFT) |
1979 (fbc_wm << WM1_LP_FBC_SHIFT) |
1980 (plane_wm << WM1_LP_SR_SHIFT) |
1981 cursor_wm);
1982
1983 /* WM2 */
1984 if (!ironlake_compute_srwm(dev, 2, enabled,
1985 SNB_READ_WM2_LATENCY() * 500,
1986 &sandybridge_display_srwm_info,
1987 &sandybridge_cursor_srwm_info,
1988 &fbc_wm, &plane_wm, &cursor_wm))
1989 return;
1990
1991 I915_WRITE(WM2_LP_ILK,
1992 WM2_LP_EN |
1993 (SNB_READ_WM2_LATENCY() << WM1_LP_LATENCY_SHIFT) |
1994 (fbc_wm << WM1_LP_FBC_SHIFT) |
1995 (plane_wm << WM1_LP_SR_SHIFT) |
1996 cursor_wm);
1997
Chris Wilsonc43d0182012-12-11 12:01:42 +00001998 /* WM3, note we have to correct the cursor latency */
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001999 if (!ironlake_compute_srwm(dev, 3, enabled,
2000 SNB_READ_WM3_LATENCY() * 500,
2001 &sandybridge_display_srwm_info,
2002 &sandybridge_cursor_srwm_info,
Chris Wilsonc43d0182012-12-11 12:01:42 +00002003 &fbc_wm, &plane_wm, &ignore_cursor_wm) ||
2004 !ironlake_compute_srwm(dev, 3, enabled,
2005 2 * SNB_READ_WM3_LATENCY() * 500,
2006 &sandybridge_display_srwm_info,
2007 &sandybridge_cursor_srwm_info,
2008 &ignore_fbc_wm, &ignore_plane_wm, &cursor_wm))
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002009 return;
2010
2011 I915_WRITE(WM3_LP_ILK,
2012 WM3_LP_EN |
2013 (SNB_READ_WM3_LATENCY() << WM1_LP_LATENCY_SHIFT) |
2014 (fbc_wm << WM1_LP_FBC_SHIFT) |
2015 (plane_wm << WM1_LP_SR_SHIFT) |
2016 cursor_wm);
2017}
2018
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002019static void
2020haswell_update_linetime_wm(struct drm_device *dev, int pipe,
2021 struct drm_display_mode *mode)
2022{
2023 struct drm_i915_private *dev_priv = dev->dev_private;
2024 u32 temp;
2025
2026 temp = I915_READ(PIPE_WM_LINETIME(pipe));
2027 temp &= ~PIPE_WM_LINETIME_MASK;
2028
2029 /* The WM are computed with base on how long it takes to fill a single
2030 * row at the given clock rate, multiplied by 8.
2031 * */
2032 temp |= PIPE_WM_LINETIME_TIME(
2033 ((mode->crtc_hdisplay * 1000) / mode->clock) * 8);
2034
2035 /* IPS watermarks are only used by pipe A, and are ignored by
2036 * pipes B and C. They are calculated similarly to the common
2037 * linetime values, except that we are using CD clock frequency
2038 * in MHz instead of pixel rate for the division.
2039 *
2040 * This is a placeholder for the IPS watermark calculation code.
2041 */
2042
2043 I915_WRITE(PIPE_WM_LINETIME(pipe), temp);
2044}
2045
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002046static bool
2047sandybridge_compute_sprite_wm(struct drm_device *dev, int plane,
2048 uint32_t sprite_width, int pixel_size,
2049 const struct intel_watermark_params *display,
2050 int display_latency_ns, int *sprite_wm)
2051{
2052 struct drm_crtc *crtc;
2053 int clock;
2054 int entries, tlb_miss;
2055
2056 crtc = intel_get_crtc_for_plane(dev, plane);
Chris Wilson3490ea52013-01-07 10:11:40 +00002057 if (!intel_crtc_active(crtc)) {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002058 *sprite_wm = display->guard_size;
2059 return false;
2060 }
2061
2062 clock = crtc->mode.clock;
2063
2064 /* Use the small buffer method to calculate the sprite watermark */
2065 entries = ((clock * pixel_size / 1000) * display_latency_ns) / 1000;
2066 tlb_miss = display->fifo_size*display->cacheline_size -
2067 sprite_width * 8;
2068 if (tlb_miss > 0)
2069 entries += tlb_miss;
2070 entries = DIV_ROUND_UP(entries, display->cacheline_size);
2071 *sprite_wm = entries + display->guard_size;
2072 if (*sprite_wm > (int)display->max_wm)
2073 *sprite_wm = display->max_wm;
2074
2075 return true;
2076}
2077
2078static bool
2079sandybridge_compute_sprite_srwm(struct drm_device *dev, int plane,
2080 uint32_t sprite_width, int pixel_size,
2081 const struct intel_watermark_params *display,
2082 int latency_ns, int *sprite_wm)
2083{
2084 struct drm_crtc *crtc;
2085 unsigned long line_time_us;
2086 int clock;
2087 int line_count, line_size;
2088 int small, large;
2089 int entries;
2090
2091 if (!latency_ns) {
2092 *sprite_wm = 0;
2093 return false;
2094 }
2095
2096 crtc = intel_get_crtc_for_plane(dev, plane);
2097 clock = crtc->mode.clock;
2098 if (!clock) {
2099 *sprite_wm = 0;
2100 return false;
2101 }
2102
2103 line_time_us = (sprite_width * 1000) / clock;
2104 if (!line_time_us) {
2105 *sprite_wm = 0;
2106 return false;
2107 }
2108
2109 line_count = (latency_ns / line_time_us + 1000) / 1000;
2110 line_size = sprite_width * pixel_size;
2111
2112 /* Use the minimum of the small and large buffer method for primary */
2113 small = ((clock * pixel_size / 1000) * latency_ns) / 1000;
2114 large = line_count * line_size;
2115
2116 entries = DIV_ROUND_UP(min(small, large), display->cacheline_size);
2117 *sprite_wm = entries + display->guard_size;
2118
2119 return *sprite_wm > 0x3ff ? false : true;
2120}
2121
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03002122static void sandybridge_update_sprite_wm(struct drm_device *dev, int pipe,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002123 uint32_t sprite_width, int pixel_size)
2124{
2125 struct drm_i915_private *dev_priv = dev->dev_private;
2126 int latency = SNB_READ_WM0_LATENCY() * 100; /* In unit 0.1us */
2127 u32 val;
2128 int sprite_wm, reg;
2129 int ret;
2130
2131 switch (pipe) {
2132 case 0:
2133 reg = WM0_PIPEA_ILK;
2134 break;
2135 case 1:
2136 reg = WM0_PIPEB_ILK;
2137 break;
2138 case 2:
2139 reg = WM0_PIPEC_IVB;
2140 break;
2141 default:
2142 return; /* bad pipe */
2143 }
2144
2145 ret = sandybridge_compute_sprite_wm(dev, pipe, sprite_width, pixel_size,
2146 &sandybridge_display_wm_info,
2147 latency, &sprite_wm);
2148 if (!ret) {
2149 DRM_DEBUG_KMS("failed to compute sprite wm for pipe %d\n",
2150 pipe);
2151 return;
2152 }
2153
2154 val = I915_READ(reg);
2155 val &= ~WM0_PIPE_SPRITE_MASK;
2156 I915_WRITE(reg, val | (sprite_wm << WM0_PIPE_SPRITE_SHIFT));
2157 DRM_DEBUG_KMS("sprite watermarks For pipe %d - %d\n", pipe, sprite_wm);
2158
2159
2160 ret = sandybridge_compute_sprite_srwm(dev, pipe, sprite_width,
2161 pixel_size,
2162 &sandybridge_display_srwm_info,
2163 SNB_READ_WM1_LATENCY() * 500,
2164 &sprite_wm);
2165 if (!ret) {
2166 DRM_DEBUG_KMS("failed to compute sprite lp1 wm on pipe %d\n",
2167 pipe);
2168 return;
2169 }
2170 I915_WRITE(WM1S_LP_ILK, sprite_wm);
2171
2172 /* Only IVB has two more LP watermarks for sprite */
2173 if (!IS_IVYBRIDGE(dev))
2174 return;
2175
2176 ret = sandybridge_compute_sprite_srwm(dev, pipe, sprite_width,
2177 pixel_size,
2178 &sandybridge_display_srwm_info,
2179 SNB_READ_WM2_LATENCY() * 500,
2180 &sprite_wm);
2181 if (!ret) {
2182 DRM_DEBUG_KMS("failed to compute sprite lp2 wm on pipe %d\n",
2183 pipe);
2184 return;
2185 }
2186 I915_WRITE(WM2S_LP_IVB, sprite_wm);
2187
2188 ret = sandybridge_compute_sprite_srwm(dev, pipe, sprite_width,
2189 pixel_size,
2190 &sandybridge_display_srwm_info,
2191 SNB_READ_WM3_LATENCY() * 500,
2192 &sprite_wm);
2193 if (!ret) {
2194 DRM_DEBUG_KMS("failed to compute sprite lp3 wm on pipe %d\n",
2195 pipe);
2196 return;
2197 }
2198 I915_WRITE(WM3S_LP_IVB, sprite_wm);
2199}
2200
2201/**
2202 * intel_update_watermarks - update FIFO watermark values based on current modes
2203 *
2204 * Calculate watermark values for the various WM regs based on current mode
2205 * and plane configuration.
2206 *
2207 * There are several cases to deal with here:
2208 * - normal (i.e. non-self-refresh)
2209 * - self-refresh (SR) mode
2210 * - lines are large relative to FIFO size (buffer can hold up to 2)
2211 * - lines are small relative to FIFO size (buffer can hold more than 2
2212 * lines), so need to account for TLB latency
2213 *
2214 * The normal calculation is:
2215 * watermark = dotclock * bytes per pixel * latency
2216 * where latency is platform & configuration dependent (we assume pessimal
2217 * values here).
2218 *
2219 * The SR calculation is:
2220 * watermark = (trunc(latency/line time)+1) * surface width *
2221 * bytes per pixel
2222 * where
2223 * line time = htotal / dotclock
2224 * surface width = hdisplay for normal plane and 64 for cursor
2225 * and latency is assumed to be high, as above.
2226 *
2227 * The final value programmed to the register should always be rounded up,
2228 * and include an extra 2 entries to account for clock crossings.
2229 *
2230 * We don't use the sprite, so we can ignore that. And on Crestline we have
2231 * to set the non-SR watermarks to 8.
2232 */
2233void intel_update_watermarks(struct drm_device *dev)
2234{
2235 struct drm_i915_private *dev_priv = dev->dev_private;
2236
2237 if (dev_priv->display.update_wm)
2238 dev_priv->display.update_wm(dev);
2239}
2240
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002241void intel_update_linetime_watermarks(struct drm_device *dev,
2242 int pipe, struct drm_display_mode *mode)
2243{
2244 struct drm_i915_private *dev_priv = dev->dev_private;
2245
2246 if (dev_priv->display.update_linetime_wm)
2247 dev_priv->display.update_linetime_wm(dev, pipe, mode);
2248}
2249
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002250void intel_update_sprite_watermarks(struct drm_device *dev, int pipe,
2251 uint32_t sprite_width, int pixel_size)
2252{
2253 struct drm_i915_private *dev_priv = dev->dev_private;
2254
2255 if (dev_priv->display.update_sprite_wm)
2256 dev_priv->display.update_sprite_wm(dev, pipe, sprite_width,
2257 pixel_size);
2258}
2259
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002260static struct drm_i915_gem_object *
2261intel_alloc_context_page(struct drm_device *dev)
2262{
2263 struct drm_i915_gem_object *ctx;
2264 int ret;
2265
2266 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2267
2268 ctx = i915_gem_alloc_object(dev, 4096);
2269 if (!ctx) {
2270 DRM_DEBUG("failed to alloc power context, RC6 disabled\n");
2271 return NULL;
2272 }
2273
Chris Wilson86a1ee22012-08-11 15:41:04 +01002274 ret = i915_gem_object_pin(ctx, 4096, true, false);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002275 if (ret) {
2276 DRM_ERROR("failed to pin power context: %d\n", ret);
2277 goto err_unref;
2278 }
2279
2280 ret = i915_gem_object_set_to_gtt_domain(ctx, 1);
2281 if (ret) {
2282 DRM_ERROR("failed to set-domain on power context: %d\n", ret);
2283 goto err_unpin;
2284 }
2285
2286 return ctx;
2287
2288err_unpin:
2289 i915_gem_object_unpin(ctx);
2290err_unref:
2291 drm_gem_object_unreference(&ctx->base);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002292 return NULL;
2293}
2294
Daniel Vetter92703882012-08-09 16:46:01 +02002295/**
2296 * Lock protecting IPS related data structures
Daniel Vetter92703882012-08-09 16:46:01 +02002297 */
2298DEFINE_SPINLOCK(mchdev_lock);
2299
2300/* Global for IPS driver to get at the current i915 device. Protected by
2301 * mchdev_lock. */
2302static struct drm_i915_private *i915_mch_dev;
2303
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002304bool ironlake_set_drps(struct drm_device *dev, u8 val)
2305{
2306 struct drm_i915_private *dev_priv = dev->dev_private;
2307 u16 rgvswctl;
2308
Daniel Vetter92703882012-08-09 16:46:01 +02002309 assert_spin_locked(&mchdev_lock);
2310
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002311 rgvswctl = I915_READ16(MEMSWCTL);
2312 if (rgvswctl & MEMCTL_CMD_STS) {
2313 DRM_DEBUG("gpu busy, RCS change rejected\n");
2314 return false; /* still busy with another command */
2315 }
2316
2317 rgvswctl = (MEMCTL_CMD_CHFREQ << MEMCTL_CMD_SHIFT) |
2318 (val << MEMCTL_FREQ_SHIFT) | MEMCTL_SFCAVM;
2319 I915_WRITE16(MEMSWCTL, rgvswctl);
2320 POSTING_READ16(MEMSWCTL);
2321
2322 rgvswctl |= MEMCTL_CMD_STS;
2323 I915_WRITE16(MEMSWCTL, rgvswctl);
2324
2325 return true;
2326}
2327
Daniel Vetter8090c6b2012-06-24 16:42:32 +02002328static void ironlake_enable_drps(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002329{
2330 struct drm_i915_private *dev_priv = dev->dev_private;
2331 u32 rgvmodectl = I915_READ(MEMMODECTL);
2332 u8 fmax, fmin, fstart, vstart;
2333
Daniel Vetter92703882012-08-09 16:46:01 +02002334 spin_lock_irq(&mchdev_lock);
2335
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002336 /* Enable temp reporting */
2337 I915_WRITE16(PMMISC, I915_READ(PMMISC) | MCPPCE_EN);
2338 I915_WRITE16(TSC1, I915_READ(TSC1) | TSE);
2339
2340 /* 100ms RC evaluation intervals */
2341 I915_WRITE(RCUPEI, 100000);
2342 I915_WRITE(RCDNEI, 100000);
2343
2344 /* Set max/min thresholds to 90ms and 80ms respectively */
2345 I915_WRITE(RCBMAXAVG, 90000);
2346 I915_WRITE(RCBMINAVG, 80000);
2347
2348 I915_WRITE(MEMIHYST, 1);
2349
2350 /* Set up min, max, and cur for interrupt handling */
2351 fmax = (rgvmodectl & MEMMODE_FMAX_MASK) >> MEMMODE_FMAX_SHIFT;
2352 fmin = (rgvmodectl & MEMMODE_FMIN_MASK);
2353 fstart = (rgvmodectl & MEMMODE_FSTART_MASK) >>
2354 MEMMODE_FSTART_SHIFT;
2355
2356 vstart = (I915_READ(PXVFREQ_BASE + (fstart * 4)) & PXVFREQ_PX_MASK) >>
2357 PXVFREQ_PX_SHIFT;
2358
Daniel Vetter20e4d402012-08-08 23:35:39 +02002359 dev_priv->ips.fmax = fmax; /* IPS callback will increase this */
2360 dev_priv->ips.fstart = fstart;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002361
Daniel Vetter20e4d402012-08-08 23:35:39 +02002362 dev_priv->ips.max_delay = fstart;
2363 dev_priv->ips.min_delay = fmin;
2364 dev_priv->ips.cur_delay = fstart;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002365
2366 DRM_DEBUG_DRIVER("fmax: %d, fmin: %d, fstart: %d\n",
2367 fmax, fmin, fstart);
2368
2369 I915_WRITE(MEMINTREN, MEMINT_CX_SUPR_EN | MEMINT_EVAL_CHG_EN);
2370
2371 /*
2372 * Interrupts will be enabled in ironlake_irq_postinstall
2373 */
2374
2375 I915_WRITE(VIDSTART, vstart);
2376 POSTING_READ(VIDSTART);
2377
2378 rgvmodectl |= MEMMODE_SWMODE_EN;
2379 I915_WRITE(MEMMODECTL, rgvmodectl);
2380
Daniel Vetter92703882012-08-09 16:46:01 +02002381 if (wait_for_atomic((I915_READ(MEMSWCTL) & MEMCTL_CMD_STS) == 0, 10))
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002382 DRM_ERROR("stuck trying to change perf mode\n");
Daniel Vetter92703882012-08-09 16:46:01 +02002383 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002384
2385 ironlake_set_drps(dev, fstart);
2386
Daniel Vetter20e4d402012-08-08 23:35:39 +02002387 dev_priv->ips.last_count1 = I915_READ(0x112e4) + I915_READ(0x112e8) +
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002388 I915_READ(0x112e0);
Daniel Vetter20e4d402012-08-08 23:35:39 +02002389 dev_priv->ips.last_time1 = jiffies_to_msecs(jiffies);
2390 dev_priv->ips.last_count2 = I915_READ(0x112f4);
2391 getrawmonotonic(&dev_priv->ips.last_time2);
Daniel Vetter92703882012-08-09 16:46:01 +02002392
2393 spin_unlock_irq(&mchdev_lock);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002394}
2395
Daniel Vetter8090c6b2012-06-24 16:42:32 +02002396static void ironlake_disable_drps(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002397{
2398 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter92703882012-08-09 16:46:01 +02002399 u16 rgvswctl;
2400
2401 spin_lock_irq(&mchdev_lock);
2402
2403 rgvswctl = I915_READ16(MEMSWCTL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002404
2405 /* Ack interrupts, disable EFC interrupt */
2406 I915_WRITE(MEMINTREN, I915_READ(MEMINTREN) & ~MEMINT_EVAL_CHG_EN);
2407 I915_WRITE(MEMINTRSTS, MEMINT_EVAL_CHG);
2408 I915_WRITE(DEIER, I915_READ(DEIER) & ~DE_PCU_EVENT);
2409 I915_WRITE(DEIIR, DE_PCU_EVENT);
2410 I915_WRITE(DEIMR, I915_READ(DEIMR) | DE_PCU_EVENT);
2411
2412 /* Go back to the starting frequency */
Daniel Vetter20e4d402012-08-08 23:35:39 +02002413 ironlake_set_drps(dev, dev_priv->ips.fstart);
Daniel Vetter92703882012-08-09 16:46:01 +02002414 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002415 rgvswctl |= MEMCTL_CMD_STS;
2416 I915_WRITE(MEMSWCTL, rgvswctl);
Daniel Vetter92703882012-08-09 16:46:01 +02002417 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002418
Daniel Vetter92703882012-08-09 16:46:01 +02002419 spin_unlock_irq(&mchdev_lock);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002420}
2421
Daniel Vetteracbe9472012-07-26 11:50:05 +02002422/* There's a funny hw issue where the hw returns all 0 when reading from
2423 * GEN6_RP_INTERRUPT_LIMITS. Hence we always need to compute the desired value
2424 * ourselves, instead of doing a rmw cycle (which might result in us clearing
2425 * all limits and the gpu stuck at whatever frequency it is at atm).
2426 */
Daniel Vetter65bccb52012-08-08 17:42:52 +02002427static u32 gen6_rps_limits(struct drm_i915_private *dev_priv, u8 *val)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002428{
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01002429 u32 limits;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002430
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01002431 limits = 0;
Daniel Vetterc6a828d2012-08-08 23:35:35 +02002432
2433 if (*val >= dev_priv->rps.max_delay)
2434 *val = dev_priv->rps.max_delay;
2435 limits |= dev_priv->rps.max_delay << 24;
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01002436
Daniel Vetter20b46e52012-07-26 11:16:14 +02002437 /* Only set the down limit when we've reached the lowest level to avoid
2438 * getting more interrupts, otherwise leave this clear. This prevents a
2439 * race in the hw when coming out of rc6: There's a tiny window where
2440 * the hw runs at the minimal clock before selecting the desired
2441 * frequency, if the down threshold expires in that window we will not
2442 * receive a down interrupt. */
Daniel Vetterc6a828d2012-08-08 23:35:35 +02002443 if (*val <= dev_priv->rps.min_delay) {
2444 *val = dev_priv->rps.min_delay;
2445 limits |= dev_priv->rps.min_delay << 16;
Daniel Vetter20b46e52012-07-26 11:16:14 +02002446 }
2447
2448 return limits;
2449}
2450
2451void gen6_set_rps(struct drm_device *dev, u8 val)
2452{
2453 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter65bccb52012-08-08 17:42:52 +02002454 u32 limits = gen6_rps_limits(dev_priv, &val);
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01002455
Jesse Barnes4fc688c2012-11-02 11:14:01 -07002456 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Ben Widawsky79249632012-09-07 19:43:42 -07002457 WARN_ON(val > dev_priv->rps.max_delay);
2458 WARN_ON(val < dev_priv->rps.min_delay);
Daniel Vetter004777c2012-08-09 15:07:01 +02002459
Daniel Vetterc6a828d2012-08-08 23:35:35 +02002460 if (val == dev_priv->rps.cur_delay)
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01002461 return;
2462
2463 I915_WRITE(GEN6_RPNSWREQ,
2464 GEN6_FREQUENCY(val) |
2465 GEN6_OFFSET(0) |
2466 GEN6_AGGRESSIVE_TURBO);
2467
2468 /* Make sure we continue to get interrupts
2469 * until we hit the minimum or maximum frequencies.
2470 */
2471 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS, limits);
2472
Ben Widawskyd5570a72012-09-07 19:43:41 -07002473 POSTING_READ(GEN6_RPNSWREQ);
2474
Daniel Vetterc6a828d2012-08-08 23:35:35 +02002475 dev_priv->rps.cur_delay = val;
Daniel Vetterbe2cde92012-08-30 13:26:48 +02002476
2477 trace_intel_gpu_freq_change(val * 50);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002478}
2479
Daniel Vetter8090c6b2012-06-24 16:42:32 +02002480static void gen6_disable_rps(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002481{
2482 struct drm_i915_private *dev_priv = dev->dev_private;
2483
Eugeni Dodonov88509482012-07-02 11:51:08 -03002484 I915_WRITE(GEN6_RC_CONTROL, 0);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002485 I915_WRITE(GEN6_RPNSWREQ, 1 << 31);
2486 I915_WRITE(GEN6_PMINTRMSK, 0xffffffff);
2487 I915_WRITE(GEN6_PMIER, 0);
2488 /* Complete PM interrupt masking here doesn't race with the rps work
2489 * item again unmasking PM interrupts because that is using a different
2490 * register (PMIMR) to mask PM interrupts. The only risk is in leaving
2491 * stale bits in PMIIR and PMIMR which gen6_enable_rps will clean up. */
2492
Daniel Vetterc6a828d2012-08-08 23:35:35 +02002493 spin_lock_irq(&dev_priv->rps.lock);
2494 dev_priv->rps.pm_iir = 0;
2495 spin_unlock_irq(&dev_priv->rps.lock);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002496
2497 I915_WRITE(GEN6_PMIIR, I915_READ(GEN6_PMIIR));
2498}
2499
2500int intel_enable_rc6(const struct drm_device *dev)
2501{
Daniel Vetter456470e2012-08-08 23:35:40 +02002502 /* Respect the kernel parameter if it is set */
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002503 if (i915_enable_rc6 >= 0)
2504 return i915_enable_rc6;
2505
Chris Wilson6567d742012-11-10 10:00:06 +00002506 /* Disable RC6 on Ironlake */
2507 if (INTEL_INFO(dev)->gen == 5)
2508 return 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002509
Daniel Vetter456470e2012-08-08 23:35:40 +02002510 if (IS_HASWELL(dev)) {
2511 DRM_DEBUG_DRIVER("Haswell: only RC6 available\n");
2512 return INTEL_RC6_ENABLE;
2513 }
2514
2515 /* snb/ivb have more than one rc6 state. */
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002516 if (INTEL_INFO(dev)->gen == 6) {
2517 DRM_DEBUG_DRIVER("Sandybridge: deep RC6 disabled\n");
2518 return INTEL_RC6_ENABLE;
2519 }
Daniel Vetter456470e2012-08-08 23:35:40 +02002520
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002521 DRM_DEBUG_DRIVER("RC6 and deep RC6 enabled\n");
2522 return (INTEL_RC6_ENABLE | INTEL_RC6p_ENABLE);
2523}
2524
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02002525static void gen6_enable_rps(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002526{
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02002527 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonb4519512012-05-11 14:29:30 +01002528 struct intel_ring_buffer *ring;
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01002529 u32 rp_state_cap;
2530 u32 gt_perf_status;
Ben Widawsky31643d52012-09-26 10:34:01 -07002531 u32 rc6vids, pcu_mbox, rc6_mask = 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002532 u32 gtfifodbg;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002533 int rc6_mode;
Ben Widawsky42c05262012-09-26 10:34:00 -07002534 int i, ret;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002535
Jesse Barnes4fc688c2012-11-02 11:14:01 -07002536 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02002537
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002538 /* Here begins a magic sequence of register writes to enable
2539 * auto-downclocking.
2540 *
2541 * Perhaps there might be some value in exposing these to
2542 * userspace...
2543 */
2544 I915_WRITE(GEN6_RC_STATE, 0);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002545
2546 /* Clear the DBG now so we don't confuse earlier errors */
2547 if ((gtfifodbg = I915_READ(GTFIFODBG))) {
2548 DRM_ERROR("GT fifo had a previous error %x\n", gtfifodbg);
2549 I915_WRITE(GTFIFODBG, gtfifodbg);
2550 }
2551
2552 gen6_gt_force_wake_get(dev_priv);
2553
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01002554 rp_state_cap = I915_READ(GEN6_RP_STATE_CAP);
2555 gt_perf_status = I915_READ(GEN6_GT_PERF_STATUS);
2556
2557 /* In units of 100MHz */
Daniel Vetterc6a828d2012-08-08 23:35:35 +02002558 dev_priv->rps.max_delay = rp_state_cap & 0xff;
2559 dev_priv->rps.min_delay = (rp_state_cap & 0xff0000) >> 16;
2560 dev_priv->rps.cur_delay = 0;
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01002561
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002562 /* disable the counters and set deterministic thresholds */
2563 I915_WRITE(GEN6_RC_CONTROL, 0);
2564
2565 I915_WRITE(GEN6_RC1_WAKE_RATE_LIMIT, 1000 << 16);
2566 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16 | 30);
2567 I915_WRITE(GEN6_RC6pp_WAKE_RATE_LIMIT, 30);
2568 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
2569 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
2570
Chris Wilsonb4519512012-05-11 14:29:30 +01002571 for_each_ring(ring, dev_priv, i)
2572 I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002573
2574 I915_WRITE(GEN6_RC_SLEEP, 0);
2575 I915_WRITE(GEN6_RC1e_THRESHOLD, 1000);
2576 I915_WRITE(GEN6_RC6_THRESHOLD, 50000);
Stéphane Marchesin0920a482013-01-29 19:41:59 -08002577 I915_WRITE(GEN6_RC6p_THRESHOLD, 150000);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002578 I915_WRITE(GEN6_RC6pp_THRESHOLD, 64000); /* unused */
2579
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03002580 /* Check if we are enabling RC6 */
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002581 rc6_mode = intel_enable_rc6(dev_priv->dev);
2582 if (rc6_mode & INTEL_RC6_ENABLE)
2583 rc6_mask |= GEN6_RC_CTL_RC6_ENABLE;
2584
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03002585 /* We don't use those on Haswell */
2586 if (!IS_HASWELL(dev)) {
2587 if (rc6_mode & INTEL_RC6p_ENABLE)
2588 rc6_mask |= GEN6_RC_CTL_RC6p_ENABLE;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002589
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03002590 if (rc6_mode & INTEL_RC6pp_ENABLE)
2591 rc6_mask |= GEN6_RC_CTL_RC6pp_ENABLE;
2592 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002593
2594 DRM_INFO("Enabling RC6 states: RC6 %s, RC6p %s, RC6pp %s\n",
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03002595 (rc6_mask & GEN6_RC_CTL_RC6_ENABLE) ? "on" : "off",
2596 (rc6_mask & GEN6_RC_CTL_RC6p_ENABLE) ? "on" : "off",
2597 (rc6_mask & GEN6_RC_CTL_RC6pp_ENABLE) ? "on" : "off");
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002598
2599 I915_WRITE(GEN6_RC_CONTROL,
2600 rc6_mask |
2601 GEN6_RC_CTL_EI_MODE(1) |
2602 GEN6_RC_CTL_HW_ENABLE);
2603
2604 I915_WRITE(GEN6_RPNSWREQ,
2605 GEN6_FREQUENCY(10) |
2606 GEN6_OFFSET(0) |
2607 GEN6_AGGRESSIVE_TURBO);
2608 I915_WRITE(GEN6_RC_VIDEO_FREQ,
2609 GEN6_FREQUENCY(12));
2610
2611 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 1000000);
2612 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS,
Daniel Vetterc6a828d2012-08-08 23:35:35 +02002613 dev_priv->rps.max_delay << 24 |
2614 dev_priv->rps.min_delay << 16);
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03002615
Daniel Vetter1ee9ae32012-08-15 10:41:45 +02002616 I915_WRITE(GEN6_RP_UP_THRESHOLD, 59400);
2617 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 245000);
2618 I915_WRITE(GEN6_RP_UP_EI, 66000);
2619 I915_WRITE(GEN6_RP_DOWN_EI, 350000);
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03002620
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002621 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
2622 I915_WRITE(GEN6_RP_CONTROL,
2623 GEN6_RP_MEDIA_TURBO |
Jesse Barnes89ba8292012-05-22 09:30:33 -07002624 GEN6_RP_MEDIA_HW_NORMAL_MODE |
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002625 GEN6_RP_MEDIA_IS_GFX |
2626 GEN6_RP_ENABLE |
2627 GEN6_RP_UP_BUSY_AVG |
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03002628 (IS_HASWELL(dev) ? GEN7_RP_DOWN_IDLE_AVG : GEN6_RP_DOWN_IDLE_CONT));
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002629
Ben Widawsky42c05262012-09-26 10:34:00 -07002630 ret = sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_MIN_FREQ_TABLE, 0);
2631 if (!ret) {
2632 pcu_mbox = 0;
2633 ret = sandybridge_pcode_read(dev_priv, GEN6_READ_OC_PARAMS, &pcu_mbox);
2634 if (ret && pcu_mbox & (1<<31)) { /* OC supported */
2635 dev_priv->rps.max_delay = pcu_mbox & 0xff;
2636 DRM_DEBUG_DRIVER("overclocking supported, adjusting frequency max to %dMHz\n", pcu_mbox * 50);
2637 }
2638 } else {
2639 DRM_DEBUG_DRIVER("Failed to set the min frequency\n");
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002640 }
2641
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01002642 gen6_set_rps(dev_priv->dev, (gt_perf_status & 0xff00) >> 8);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002643
2644 /* requires MSI enabled */
Chris Wilsonff928262012-07-05 15:02:17 +01002645 I915_WRITE(GEN6_PMIER, GEN6_PM_DEFERRED_EVENTS);
Daniel Vetterc6a828d2012-08-08 23:35:35 +02002646 spin_lock_irq(&dev_priv->rps.lock);
2647 WARN_ON(dev_priv->rps.pm_iir != 0);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002648 I915_WRITE(GEN6_PMIMR, 0);
Daniel Vetterc6a828d2012-08-08 23:35:35 +02002649 spin_unlock_irq(&dev_priv->rps.lock);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002650 /* enable all PM interrupts */
2651 I915_WRITE(GEN6_PMINTRMSK, 0);
2652
Ben Widawsky31643d52012-09-26 10:34:01 -07002653 rc6vids = 0;
2654 ret = sandybridge_pcode_read(dev_priv, GEN6_PCODE_READ_RC6VIDS, &rc6vids);
2655 if (IS_GEN6(dev) && ret) {
2656 DRM_DEBUG_DRIVER("Couldn't check for BIOS workaround\n");
2657 } else if (IS_GEN6(dev) && (GEN6_DECODE_RC6_VID(rc6vids & 0xff) < 450)) {
2658 DRM_DEBUG_DRIVER("You should update your BIOS. Correcting minimum rc6 voltage (%dmV->%dmV)\n",
2659 GEN6_DECODE_RC6_VID(rc6vids & 0xff), 450);
2660 rc6vids &= 0xffff00;
2661 rc6vids |= GEN6_ENCODE_RC6_VID(450);
2662 ret = sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_RC6VIDS, rc6vids);
2663 if (ret)
2664 DRM_ERROR("Couldn't fix incorrect rc6 voltage\n");
2665 }
2666
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002667 gen6_gt_force_wake_put(dev_priv);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002668}
2669
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02002670static void gen6_update_ring_freq(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002671{
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02002672 struct drm_i915_private *dev_priv = dev->dev_private;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002673 int min_freq = 15;
Jean Delvaree3fef092012-11-12 14:18:02 +01002674 int gpu_freq;
2675 unsigned int ia_freq, max_ia_freq;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002676 int scaling_factor = 180;
2677
Jesse Barnes4fc688c2012-11-02 11:14:01 -07002678 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02002679
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002680 max_ia_freq = cpufreq_quick_get_max(0);
2681 /*
2682 * Default to measured freq if none found, PCU will ensure we don't go
2683 * over
2684 */
2685 if (!max_ia_freq)
2686 max_ia_freq = tsc_khz;
2687
2688 /* Convert from kHz to MHz */
2689 max_ia_freq /= 1000;
2690
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002691 /*
2692 * For each potential GPU frequency, load a ring frequency we'd like
2693 * to use for memory access. We do this by specifying the IA frequency
2694 * the PCU should use as a reference to determine the ring frequency.
2695 */
Daniel Vetterc6a828d2012-08-08 23:35:35 +02002696 for (gpu_freq = dev_priv->rps.max_delay; gpu_freq >= dev_priv->rps.min_delay;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002697 gpu_freq--) {
Daniel Vetterc6a828d2012-08-08 23:35:35 +02002698 int diff = dev_priv->rps.max_delay - gpu_freq;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002699
2700 /*
2701 * For GPU frequencies less than 750MHz, just use the lowest
2702 * ring freq.
2703 */
2704 if (gpu_freq < min_freq)
2705 ia_freq = 800;
2706 else
2707 ia_freq = max_ia_freq - ((diff * scaling_factor) / 2);
2708 ia_freq = DIV_ROUND_CLOSEST(ia_freq, 100);
Ben Widawsky42c05262012-09-26 10:34:00 -07002709 ia_freq <<= GEN6_PCODE_FREQ_IA_RATIO_SHIFT;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002710
Ben Widawsky42c05262012-09-26 10:34:00 -07002711 sandybridge_pcode_write(dev_priv,
2712 GEN6_PCODE_WRITE_MIN_FREQ_TABLE,
2713 ia_freq | gpu_freq);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002714 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002715}
2716
Daniel Vetter930ebb42012-06-29 23:32:16 +02002717void ironlake_teardown_rc6(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002718{
2719 struct drm_i915_private *dev_priv = dev->dev_private;
2720
Daniel Vetter3e373942012-11-02 19:55:04 +01002721 if (dev_priv->ips.renderctx) {
2722 i915_gem_object_unpin(dev_priv->ips.renderctx);
2723 drm_gem_object_unreference(&dev_priv->ips.renderctx->base);
2724 dev_priv->ips.renderctx = NULL;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002725 }
2726
Daniel Vetter3e373942012-11-02 19:55:04 +01002727 if (dev_priv->ips.pwrctx) {
2728 i915_gem_object_unpin(dev_priv->ips.pwrctx);
2729 drm_gem_object_unreference(&dev_priv->ips.pwrctx->base);
2730 dev_priv->ips.pwrctx = NULL;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002731 }
2732}
2733
Daniel Vetter930ebb42012-06-29 23:32:16 +02002734static void ironlake_disable_rc6(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002735{
2736 struct drm_i915_private *dev_priv = dev->dev_private;
2737
2738 if (I915_READ(PWRCTXA)) {
2739 /* Wake the GPU, prevent RC6, then restore RSTDBYCTL */
2740 I915_WRITE(RSTDBYCTL, I915_READ(RSTDBYCTL) | RCX_SW_EXIT);
2741 wait_for(((I915_READ(RSTDBYCTL) & RSX_STATUS_MASK) == RSX_STATUS_ON),
2742 50);
2743
2744 I915_WRITE(PWRCTXA, 0);
2745 POSTING_READ(PWRCTXA);
2746
2747 I915_WRITE(RSTDBYCTL, I915_READ(RSTDBYCTL) & ~RCX_SW_EXIT);
2748 POSTING_READ(RSTDBYCTL);
2749 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002750}
2751
2752static int ironlake_setup_rc6(struct drm_device *dev)
2753{
2754 struct drm_i915_private *dev_priv = dev->dev_private;
2755
Daniel Vetter3e373942012-11-02 19:55:04 +01002756 if (dev_priv->ips.renderctx == NULL)
2757 dev_priv->ips.renderctx = intel_alloc_context_page(dev);
2758 if (!dev_priv->ips.renderctx)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002759 return -ENOMEM;
2760
Daniel Vetter3e373942012-11-02 19:55:04 +01002761 if (dev_priv->ips.pwrctx == NULL)
2762 dev_priv->ips.pwrctx = intel_alloc_context_page(dev);
2763 if (!dev_priv->ips.pwrctx) {
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002764 ironlake_teardown_rc6(dev);
2765 return -ENOMEM;
2766 }
2767
2768 return 0;
2769}
2770
Daniel Vetter930ebb42012-06-29 23:32:16 +02002771static void ironlake_enable_rc6(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002772{
2773 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter6d90c952012-04-26 23:28:05 +02002774 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Chris Wilson3e960502012-11-27 16:22:54 +00002775 bool was_interruptible;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002776 int ret;
2777
2778 /* rc6 disabled by default due to repeated reports of hanging during
2779 * boot and resume.
2780 */
2781 if (!intel_enable_rc6(dev))
2782 return;
2783
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02002784 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2785
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002786 ret = ironlake_setup_rc6(dev);
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02002787 if (ret)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002788 return;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002789
Chris Wilson3e960502012-11-27 16:22:54 +00002790 was_interruptible = dev_priv->mm.interruptible;
2791 dev_priv->mm.interruptible = false;
2792
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002793 /*
2794 * GPU can automatically power down the render unit if given a page
2795 * to save state.
2796 */
Daniel Vetter6d90c952012-04-26 23:28:05 +02002797 ret = intel_ring_begin(ring, 6);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002798 if (ret) {
2799 ironlake_teardown_rc6(dev);
Chris Wilson3e960502012-11-27 16:22:54 +00002800 dev_priv->mm.interruptible = was_interruptible;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002801 return;
2802 }
2803
Daniel Vetter6d90c952012-04-26 23:28:05 +02002804 intel_ring_emit(ring, MI_SUSPEND_FLUSH | MI_SUSPEND_FLUSH_EN);
2805 intel_ring_emit(ring, MI_SET_CONTEXT);
Daniel Vetter3e373942012-11-02 19:55:04 +01002806 intel_ring_emit(ring, dev_priv->ips.renderctx->gtt_offset |
Daniel Vetter6d90c952012-04-26 23:28:05 +02002807 MI_MM_SPACE_GTT |
2808 MI_SAVE_EXT_STATE_EN |
2809 MI_RESTORE_EXT_STATE_EN |
2810 MI_RESTORE_INHIBIT);
2811 intel_ring_emit(ring, MI_SUSPEND_FLUSH);
2812 intel_ring_emit(ring, MI_NOOP);
2813 intel_ring_emit(ring, MI_FLUSH);
2814 intel_ring_advance(ring);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002815
2816 /*
2817 * Wait for the command parser to advance past MI_SET_CONTEXT. The HW
2818 * does an implicit flush, combined with MI_FLUSH above, it should be
2819 * safe to assume that renderctx is valid
2820 */
Chris Wilson3e960502012-11-27 16:22:54 +00002821 ret = intel_ring_idle(ring);
2822 dev_priv->mm.interruptible = was_interruptible;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002823 if (ret) {
2824 DRM_ERROR("failed to enable ironlake power power savings\n");
2825 ironlake_teardown_rc6(dev);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002826 return;
2827 }
2828
Daniel Vetter3e373942012-11-02 19:55:04 +01002829 I915_WRITE(PWRCTXA, dev_priv->ips.pwrctx->gtt_offset | PWRCTX_EN);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002830 I915_WRITE(RSTDBYCTL, I915_READ(RSTDBYCTL) & ~RCX_SW_EXIT);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002831}
2832
Eugeni Dodonovdde18882012-04-18 15:29:24 -03002833static unsigned long intel_pxfreq(u32 vidfreq)
2834{
2835 unsigned long freq;
2836 int div = (vidfreq & 0x3f0000) >> 16;
2837 int post = (vidfreq & 0x3000) >> 12;
2838 int pre = (vidfreq & 0x7);
2839
2840 if (!pre)
2841 return 0;
2842
2843 freq = ((div * 133333) / ((1<<post) * pre));
2844
2845 return freq;
2846}
2847
Daniel Vettereb48eb02012-04-26 23:28:12 +02002848static const struct cparams {
2849 u16 i;
2850 u16 t;
2851 u16 m;
2852 u16 c;
2853} cparams[] = {
2854 { 1, 1333, 301, 28664 },
2855 { 1, 1066, 294, 24460 },
2856 { 1, 800, 294, 25192 },
2857 { 0, 1333, 276, 27605 },
2858 { 0, 1066, 276, 27605 },
2859 { 0, 800, 231, 23784 },
2860};
2861
Chris Wilsonf531dcb2012-09-25 10:16:12 +01002862static unsigned long __i915_chipset_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02002863{
2864 u64 total_count, diff, ret;
2865 u32 count1, count2, count3, m = 0, c = 0;
2866 unsigned long now = jiffies_to_msecs(jiffies), diff1;
2867 int i;
2868
Daniel Vetter02d71952012-08-09 16:44:54 +02002869 assert_spin_locked(&mchdev_lock);
2870
Daniel Vetter20e4d402012-08-08 23:35:39 +02002871 diff1 = now - dev_priv->ips.last_time1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02002872
2873 /* Prevent division-by-zero if we are asking too fast.
2874 * Also, we don't get interesting results if we are polling
2875 * faster than once in 10ms, so just return the saved value
2876 * in such cases.
2877 */
2878 if (diff1 <= 10)
Daniel Vetter20e4d402012-08-08 23:35:39 +02002879 return dev_priv->ips.chipset_power;
Daniel Vettereb48eb02012-04-26 23:28:12 +02002880
2881 count1 = I915_READ(DMIEC);
2882 count2 = I915_READ(DDREC);
2883 count3 = I915_READ(CSIEC);
2884
2885 total_count = count1 + count2 + count3;
2886
2887 /* FIXME: handle per-counter overflow */
Daniel Vetter20e4d402012-08-08 23:35:39 +02002888 if (total_count < dev_priv->ips.last_count1) {
2889 diff = ~0UL - dev_priv->ips.last_count1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02002890 diff += total_count;
2891 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +02002892 diff = total_count - dev_priv->ips.last_count1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02002893 }
2894
2895 for (i = 0; i < ARRAY_SIZE(cparams); i++) {
Daniel Vetter20e4d402012-08-08 23:35:39 +02002896 if (cparams[i].i == dev_priv->ips.c_m &&
2897 cparams[i].t == dev_priv->ips.r_t) {
Daniel Vettereb48eb02012-04-26 23:28:12 +02002898 m = cparams[i].m;
2899 c = cparams[i].c;
2900 break;
2901 }
2902 }
2903
2904 diff = div_u64(diff, diff1);
2905 ret = ((m * diff) + c);
2906 ret = div_u64(ret, 10);
2907
Daniel Vetter20e4d402012-08-08 23:35:39 +02002908 dev_priv->ips.last_count1 = total_count;
2909 dev_priv->ips.last_time1 = now;
Daniel Vettereb48eb02012-04-26 23:28:12 +02002910
Daniel Vetter20e4d402012-08-08 23:35:39 +02002911 dev_priv->ips.chipset_power = ret;
Daniel Vettereb48eb02012-04-26 23:28:12 +02002912
2913 return ret;
2914}
2915
Chris Wilsonf531dcb2012-09-25 10:16:12 +01002916unsigned long i915_chipset_val(struct drm_i915_private *dev_priv)
2917{
2918 unsigned long val;
2919
2920 if (dev_priv->info->gen != 5)
2921 return 0;
2922
2923 spin_lock_irq(&mchdev_lock);
2924
2925 val = __i915_chipset_val(dev_priv);
2926
2927 spin_unlock_irq(&mchdev_lock);
2928
2929 return val;
2930}
2931
Daniel Vettereb48eb02012-04-26 23:28:12 +02002932unsigned long i915_mch_val(struct drm_i915_private *dev_priv)
2933{
2934 unsigned long m, x, b;
2935 u32 tsfs;
2936
2937 tsfs = I915_READ(TSFS);
2938
2939 m = ((tsfs & TSFS_SLOPE_MASK) >> TSFS_SLOPE_SHIFT);
2940 x = I915_READ8(TR1);
2941
2942 b = tsfs & TSFS_INTR_MASK;
2943
2944 return ((m * x) / 127) - b;
2945}
2946
2947static u16 pvid_to_extvid(struct drm_i915_private *dev_priv, u8 pxvid)
2948{
2949 static const struct v_table {
2950 u16 vd; /* in .1 mil */
2951 u16 vm; /* in .1 mil */
2952 } v_table[] = {
2953 { 0, 0, },
2954 { 375, 0, },
2955 { 500, 0, },
2956 { 625, 0, },
2957 { 750, 0, },
2958 { 875, 0, },
2959 { 1000, 0, },
2960 { 1125, 0, },
2961 { 4125, 3000, },
2962 { 4125, 3000, },
2963 { 4125, 3000, },
2964 { 4125, 3000, },
2965 { 4125, 3000, },
2966 { 4125, 3000, },
2967 { 4125, 3000, },
2968 { 4125, 3000, },
2969 { 4125, 3000, },
2970 { 4125, 3000, },
2971 { 4125, 3000, },
2972 { 4125, 3000, },
2973 { 4125, 3000, },
2974 { 4125, 3000, },
2975 { 4125, 3000, },
2976 { 4125, 3000, },
2977 { 4125, 3000, },
2978 { 4125, 3000, },
2979 { 4125, 3000, },
2980 { 4125, 3000, },
2981 { 4125, 3000, },
2982 { 4125, 3000, },
2983 { 4125, 3000, },
2984 { 4125, 3000, },
2985 { 4250, 3125, },
2986 { 4375, 3250, },
2987 { 4500, 3375, },
2988 { 4625, 3500, },
2989 { 4750, 3625, },
2990 { 4875, 3750, },
2991 { 5000, 3875, },
2992 { 5125, 4000, },
2993 { 5250, 4125, },
2994 { 5375, 4250, },
2995 { 5500, 4375, },
2996 { 5625, 4500, },
2997 { 5750, 4625, },
2998 { 5875, 4750, },
2999 { 6000, 4875, },
3000 { 6125, 5000, },
3001 { 6250, 5125, },
3002 { 6375, 5250, },
3003 { 6500, 5375, },
3004 { 6625, 5500, },
3005 { 6750, 5625, },
3006 { 6875, 5750, },
3007 { 7000, 5875, },
3008 { 7125, 6000, },
3009 { 7250, 6125, },
3010 { 7375, 6250, },
3011 { 7500, 6375, },
3012 { 7625, 6500, },
3013 { 7750, 6625, },
3014 { 7875, 6750, },
3015 { 8000, 6875, },
3016 { 8125, 7000, },
3017 { 8250, 7125, },
3018 { 8375, 7250, },
3019 { 8500, 7375, },
3020 { 8625, 7500, },
3021 { 8750, 7625, },
3022 { 8875, 7750, },
3023 { 9000, 7875, },
3024 { 9125, 8000, },
3025 { 9250, 8125, },
3026 { 9375, 8250, },
3027 { 9500, 8375, },
3028 { 9625, 8500, },
3029 { 9750, 8625, },
3030 { 9875, 8750, },
3031 { 10000, 8875, },
3032 { 10125, 9000, },
3033 { 10250, 9125, },
3034 { 10375, 9250, },
3035 { 10500, 9375, },
3036 { 10625, 9500, },
3037 { 10750, 9625, },
3038 { 10875, 9750, },
3039 { 11000, 9875, },
3040 { 11125, 10000, },
3041 { 11250, 10125, },
3042 { 11375, 10250, },
3043 { 11500, 10375, },
3044 { 11625, 10500, },
3045 { 11750, 10625, },
3046 { 11875, 10750, },
3047 { 12000, 10875, },
3048 { 12125, 11000, },
3049 { 12250, 11125, },
3050 { 12375, 11250, },
3051 { 12500, 11375, },
3052 { 12625, 11500, },
3053 { 12750, 11625, },
3054 { 12875, 11750, },
3055 { 13000, 11875, },
3056 { 13125, 12000, },
3057 { 13250, 12125, },
3058 { 13375, 12250, },
3059 { 13500, 12375, },
3060 { 13625, 12500, },
3061 { 13750, 12625, },
3062 { 13875, 12750, },
3063 { 14000, 12875, },
3064 { 14125, 13000, },
3065 { 14250, 13125, },
3066 { 14375, 13250, },
3067 { 14500, 13375, },
3068 { 14625, 13500, },
3069 { 14750, 13625, },
3070 { 14875, 13750, },
3071 { 15000, 13875, },
3072 { 15125, 14000, },
3073 { 15250, 14125, },
3074 { 15375, 14250, },
3075 { 15500, 14375, },
3076 { 15625, 14500, },
3077 { 15750, 14625, },
3078 { 15875, 14750, },
3079 { 16000, 14875, },
3080 { 16125, 15000, },
3081 };
3082 if (dev_priv->info->is_mobile)
3083 return v_table[pxvid].vm;
3084 else
3085 return v_table[pxvid].vd;
3086}
3087
Daniel Vetter02d71952012-08-09 16:44:54 +02003088static void __i915_update_gfx_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02003089{
3090 struct timespec now, diff1;
3091 u64 diff;
3092 unsigned long diffms;
3093 u32 count;
3094
Daniel Vetter02d71952012-08-09 16:44:54 +02003095 assert_spin_locked(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02003096
3097 getrawmonotonic(&now);
Daniel Vetter20e4d402012-08-08 23:35:39 +02003098 diff1 = timespec_sub(now, dev_priv->ips.last_time2);
Daniel Vettereb48eb02012-04-26 23:28:12 +02003099
3100 /* Don't divide by 0 */
3101 diffms = diff1.tv_sec * 1000 + diff1.tv_nsec / 1000000;
3102 if (!diffms)
3103 return;
3104
3105 count = I915_READ(GFXEC);
3106
Daniel Vetter20e4d402012-08-08 23:35:39 +02003107 if (count < dev_priv->ips.last_count2) {
3108 diff = ~0UL - dev_priv->ips.last_count2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02003109 diff += count;
3110 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +02003111 diff = count - dev_priv->ips.last_count2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02003112 }
3113
Daniel Vetter20e4d402012-08-08 23:35:39 +02003114 dev_priv->ips.last_count2 = count;
3115 dev_priv->ips.last_time2 = now;
Daniel Vettereb48eb02012-04-26 23:28:12 +02003116
3117 /* More magic constants... */
3118 diff = diff * 1181;
3119 diff = div_u64(diff, diffms * 10);
Daniel Vetter20e4d402012-08-08 23:35:39 +02003120 dev_priv->ips.gfx_power = diff;
Daniel Vettereb48eb02012-04-26 23:28:12 +02003121}
3122
Daniel Vetter02d71952012-08-09 16:44:54 +02003123void i915_update_gfx_val(struct drm_i915_private *dev_priv)
3124{
3125 if (dev_priv->info->gen != 5)
3126 return;
3127
Daniel Vetter92703882012-08-09 16:46:01 +02003128 spin_lock_irq(&mchdev_lock);
Daniel Vetter02d71952012-08-09 16:44:54 +02003129
3130 __i915_update_gfx_val(dev_priv);
3131
Daniel Vetter92703882012-08-09 16:46:01 +02003132 spin_unlock_irq(&mchdev_lock);
Daniel Vetter02d71952012-08-09 16:44:54 +02003133}
3134
Chris Wilsonf531dcb2012-09-25 10:16:12 +01003135static unsigned long __i915_gfx_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02003136{
3137 unsigned long t, corr, state1, corr2, state2;
3138 u32 pxvid, ext_v;
3139
Daniel Vetter02d71952012-08-09 16:44:54 +02003140 assert_spin_locked(&mchdev_lock);
3141
Daniel Vetterc6a828d2012-08-08 23:35:35 +02003142 pxvid = I915_READ(PXVFREQ_BASE + (dev_priv->rps.cur_delay * 4));
Daniel Vettereb48eb02012-04-26 23:28:12 +02003143 pxvid = (pxvid >> 24) & 0x7f;
3144 ext_v = pvid_to_extvid(dev_priv, pxvid);
3145
3146 state1 = ext_v;
3147
3148 t = i915_mch_val(dev_priv);
3149
3150 /* Revel in the empirically derived constants */
3151
3152 /* Correction factor in 1/100000 units */
3153 if (t > 80)
3154 corr = ((t * 2349) + 135940);
3155 else if (t >= 50)
3156 corr = ((t * 964) + 29317);
3157 else /* < 50 */
3158 corr = ((t * 301) + 1004);
3159
3160 corr = corr * ((150142 * state1) / 10000 - 78642);
3161 corr /= 100000;
Daniel Vetter20e4d402012-08-08 23:35:39 +02003162 corr2 = (corr * dev_priv->ips.corr);
Daniel Vettereb48eb02012-04-26 23:28:12 +02003163
3164 state2 = (corr2 * state1) / 10000;
3165 state2 /= 100; /* convert to mW */
3166
Daniel Vetter02d71952012-08-09 16:44:54 +02003167 __i915_update_gfx_val(dev_priv);
Daniel Vettereb48eb02012-04-26 23:28:12 +02003168
Daniel Vetter20e4d402012-08-08 23:35:39 +02003169 return dev_priv->ips.gfx_power + state2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02003170}
3171
Chris Wilsonf531dcb2012-09-25 10:16:12 +01003172unsigned long i915_gfx_val(struct drm_i915_private *dev_priv)
3173{
3174 unsigned long val;
3175
3176 if (dev_priv->info->gen != 5)
3177 return 0;
3178
3179 spin_lock_irq(&mchdev_lock);
3180
3181 val = __i915_gfx_val(dev_priv);
3182
3183 spin_unlock_irq(&mchdev_lock);
3184
3185 return val;
3186}
3187
Daniel Vettereb48eb02012-04-26 23:28:12 +02003188/**
3189 * i915_read_mch_val - return value for IPS use
3190 *
3191 * Calculate and return a value for the IPS driver to use when deciding whether
3192 * we have thermal and power headroom to increase CPU or GPU power budget.
3193 */
3194unsigned long i915_read_mch_val(void)
3195{
3196 struct drm_i915_private *dev_priv;
3197 unsigned long chipset_val, graphics_val, ret = 0;
3198
Daniel Vetter92703882012-08-09 16:46:01 +02003199 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02003200 if (!i915_mch_dev)
3201 goto out_unlock;
3202 dev_priv = i915_mch_dev;
3203
Chris Wilsonf531dcb2012-09-25 10:16:12 +01003204 chipset_val = __i915_chipset_val(dev_priv);
3205 graphics_val = __i915_gfx_val(dev_priv);
Daniel Vettereb48eb02012-04-26 23:28:12 +02003206
3207 ret = chipset_val + graphics_val;
3208
3209out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02003210 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02003211
3212 return ret;
3213}
3214EXPORT_SYMBOL_GPL(i915_read_mch_val);
3215
3216/**
3217 * i915_gpu_raise - raise GPU frequency limit
3218 *
3219 * Raise the limit; IPS indicates we have thermal headroom.
3220 */
3221bool i915_gpu_raise(void)
3222{
3223 struct drm_i915_private *dev_priv;
3224 bool ret = true;
3225
Daniel Vetter92703882012-08-09 16:46:01 +02003226 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02003227 if (!i915_mch_dev) {
3228 ret = false;
3229 goto out_unlock;
3230 }
3231 dev_priv = i915_mch_dev;
3232
Daniel Vetter20e4d402012-08-08 23:35:39 +02003233 if (dev_priv->ips.max_delay > dev_priv->ips.fmax)
3234 dev_priv->ips.max_delay--;
Daniel Vettereb48eb02012-04-26 23:28:12 +02003235
3236out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02003237 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02003238
3239 return ret;
3240}
3241EXPORT_SYMBOL_GPL(i915_gpu_raise);
3242
3243/**
3244 * i915_gpu_lower - lower GPU frequency limit
3245 *
3246 * IPS indicates we're close to a thermal limit, so throttle back the GPU
3247 * frequency maximum.
3248 */
3249bool i915_gpu_lower(void)
3250{
3251 struct drm_i915_private *dev_priv;
3252 bool ret = true;
3253
Daniel Vetter92703882012-08-09 16:46:01 +02003254 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02003255 if (!i915_mch_dev) {
3256 ret = false;
3257 goto out_unlock;
3258 }
3259 dev_priv = i915_mch_dev;
3260
Daniel Vetter20e4d402012-08-08 23:35:39 +02003261 if (dev_priv->ips.max_delay < dev_priv->ips.min_delay)
3262 dev_priv->ips.max_delay++;
Daniel Vettereb48eb02012-04-26 23:28:12 +02003263
3264out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02003265 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02003266
3267 return ret;
3268}
3269EXPORT_SYMBOL_GPL(i915_gpu_lower);
3270
3271/**
3272 * i915_gpu_busy - indicate GPU business to IPS
3273 *
3274 * Tell the IPS driver whether or not the GPU is busy.
3275 */
3276bool i915_gpu_busy(void)
3277{
3278 struct drm_i915_private *dev_priv;
Chris Wilsonf047e392012-07-21 12:31:41 +01003279 struct intel_ring_buffer *ring;
Daniel Vettereb48eb02012-04-26 23:28:12 +02003280 bool ret = false;
Chris Wilsonf047e392012-07-21 12:31:41 +01003281 int i;
Daniel Vettereb48eb02012-04-26 23:28:12 +02003282
Daniel Vetter92703882012-08-09 16:46:01 +02003283 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02003284 if (!i915_mch_dev)
3285 goto out_unlock;
3286 dev_priv = i915_mch_dev;
3287
Chris Wilsonf047e392012-07-21 12:31:41 +01003288 for_each_ring(ring, dev_priv, i)
3289 ret |= !list_empty(&ring->request_list);
Daniel Vettereb48eb02012-04-26 23:28:12 +02003290
3291out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02003292 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02003293
3294 return ret;
3295}
3296EXPORT_SYMBOL_GPL(i915_gpu_busy);
3297
3298/**
3299 * i915_gpu_turbo_disable - disable graphics turbo
3300 *
3301 * Disable graphics turbo by resetting the max frequency and setting the
3302 * current frequency to the default.
3303 */
3304bool i915_gpu_turbo_disable(void)
3305{
3306 struct drm_i915_private *dev_priv;
3307 bool ret = true;
3308
Daniel Vetter92703882012-08-09 16:46:01 +02003309 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02003310 if (!i915_mch_dev) {
3311 ret = false;
3312 goto out_unlock;
3313 }
3314 dev_priv = i915_mch_dev;
3315
Daniel Vetter20e4d402012-08-08 23:35:39 +02003316 dev_priv->ips.max_delay = dev_priv->ips.fstart;
Daniel Vettereb48eb02012-04-26 23:28:12 +02003317
Daniel Vetter20e4d402012-08-08 23:35:39 +02003318 if (!ironlake_set_drps(dev_priv->dev, dev_priv->ips.fstart))
Daniel Vettereb48eb02012-04-26 23:28:12 +02003319 ret = false;
3320
3321out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02003322 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02003323
3324 return ret;
3325}
3326EXPORT_SYMBOL_GPL(i915_gpu_turbo_disable);
3327
3328/**
3329 * Tells the intel_ips driver that the i915 driver is now loaded, if
3330 * IPS got loaded first.
3331 *
3332 * This awkward dance is so that neither module has to depend on the
3333 * other in order for IPS to do the appropriate communication of
3334 * GPU turbo limits to i915.
3335 */
3336static void
3337ips_ping_for_i915_load(void)
3338{
3339 void (*link)(void);
3340
3341 link = symbol_get(ips_link_to_i915_driver);
3342 if (link) {
3343 link();
3344 symbol_put(ips_link_to_i915_driver);
3345 }
3346}
3347
3348void intel_gpu_ips_init(struct drm_i915_private *dev_priv)
3349{
Daniel Vetter02d71952012-08-09 16:44:54 +02003350 /* We only register the i915 ips part with intel-ips once everything is
3351 * set up, to avoid intel-ips sneaking in and reading bogus values. */
Daniel Vetter92703882012-08-09 16:46:01 +02003352 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02003353 i915_mch_dev = dev_priv;
Daniel Vetter92703882012-08-09 16:46:01 +02003354 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02003355
3356 ips_ping_for_i915_load();
3357}
3358
3359void intel_gpu_ips_teardown(void)
3360{
Daniel Vetter92703882012-08-09 16:46:01 +02003361 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02003362 i915_mch_dev = NULL;
Daniel Vetter92703882012-08-09 16:46:01 +02003363 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02003364}
Daniel Vetter8090c6b2012-06-24 16:42:32 +02003365static void intel_init_emon(struct drm_device *dev)
Eugeni Dodonovdde18882012-04-18 15:29:24 -03003366{
3367 struct drm_i915_private *dev_priv = dev->dev_private;
3368 u32 lcfuse;
3369 u8 pxw[16];
3370 int i;
3371
3372 /* Disable to program */
3373 I915_WRITE(ECR, 0);
3374 POSTING_READ(ECR);
3375
3376 /* Program energy weights for various events */
3377 I915_WRITE(SDEW, 0x15040d00);
3378 I915_WRITE(CSIEW0, 0x007f0000);
3379 I915_WRITE(CSIEW1, 0x1e220004);
3380 I915_WRITE(CSIEW2, 0x04000004);
3381
3382 for (i = 0; i < 5; i++)
3383 I915_WRITE(PEW + (i * 4), 0);
3384 for (i = 0; i < 3; i++)
3385 I915_WRITE(DEW + (i * 4), 0);
3386
3387 /* Program P-state weights to account for frequency power adjustment */
3388 for (i = 0; i < 16; i++) {
3389 u32 pxvidfreq = I915_READ(PXVFREQ_BASE + (i * 4));
3390 unsigned long freq = intel_pxfreq(pxvidfreq);
3391 unsigned long vid = (pxvidfreq & PXVFREQ_PX_MASK) >>
3392 PXVFREQ_PX_SHIFT;
3393 unsigned long val;
3394
3395 val = vid * vid;
3396 val *= (freq / 1000);
3397 val *= 255;
3398 val /= (127*127*900);
3399 if (val > 0xff)
3400 DRM_ERROR("bad pxval: %ld\n", val);
3401 pxw[i] = val;
3402 }
3403 /* Render standby states get 0 weight */
3404 pxw[14] = 0;
3405 pxw[15] = 0;
3406
3407 for (i = 0; i < 4; i++) {
3408 u32 val = (pxw[i*4] << 24) | (pxw[(i*4)+1] << 16) |
3409 (pxw[(i*4)+2] << 8) | (pxw[(i*4)+3]);
3410 I915_WRITE(PXW + (i * 4), val);
3411 }
3412
3413 /* Adjust magic regs to magic values (more experimental results) */
3414 I915_WRITE(OGW0, 0);
3415 I915_WRITE(OGW1, 0);
3416 I915_WRITE(EG0, 0x00007f00);
3417 I915_WRITE(EG1, 0x0000000e);
3418 I915_WRITE(EG2, 0x000e0000);
3419 I915_WRITE(EG3, 0x68000300);
3420 I915_WRITE(EG4, 0x42000000);
3421 I915_WRITE(EG5, 0x00140031);
3422 I915_WRITE(EG6, 0);
3423 I915_WRITE(EG7, 0);
3424
3425 for (i = 0; i < 8; i++)
3426 I915_WRITE(PXWL + (i * 4), 0);
3427
3428 /* Enable PMON + select events */
3429 I915_WRITE(ECR, 0x80000019);
3430
3431 lcfuse = I915_READ(LCFUSE02);
3432
Daniel Vetter20e4d402012-08-08 23:35:39 +02003433 dev_priv->ips.corr = (lcfuse & LCFUSE_HIV_MASK);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03003434}
3435
Daniel Vetter8090c6b2012-06-24 16:42:32 +02003436void intel_disable_gt_powersave(struct drm_device *dev)
3437{
Jesse Barnes1a01ab32012-11-02 11:14:00 -07003438 struct drm_i915_private *dev_priv = dev->dev_private;
3439
Daniel Vetter930ebb42012-06-29 23:32:16 +02003440 if (IS_IRONLAKE_M(dev)) {
Daniel Vetter8090c6b2012-06-24 16:42:32 +02003441 ironlake_disable_drps(dev);
Daniel Vetter930ebb42012-06-29 23:32:16 +02003442 ironlake_disable_rc6(dev);
3443 } else if (INTEL_INFO(dev)->gen >= 6 && !IS_VALLEYVIEW(dev)) {
Jesse Barnes1a01ab32012-11-02 11:14:00 -07003444 cancel_delayed_work_sync(&dev_priv->rps.delayed_resume_work);
Jesse Barnes4fc688c2012-11-02 11:14:01 -07003445 mutex_lock(&dev_priv->rps.hw_lock);
Daniel Vetter8090c6b2012-06-24 16:42:32 +02003446 gen6_disable_rps(dev);
Jesse Barnes4fc688c2012-11-02 11:14:01 -07003447 mutex_unlock(&dev_priv->rps.hw_lock);
Daniel Vetter930ebb42012-06-29 23:32:16 +02003448 }
Daniel Vetter8090c6b2012-06-24 16:42:32 +02003449}
3450
Jesse Barnes1a01ab32012-11-02 11:14:00 -07003451static void intel_gen6_powersave_work(struct work_struct *work)
3452{
3453 struct drm_i915_private *dev_priv =
3454 container_of(work, struct drm_i915_private,
3455 rps.delayed_resume_work.work);
3456 struct drm_device *dev = dev_priv->dev;
3457
Jesse Barnes4fc688c2012-11-02 11:14:01 -07003458 mutex_lock(&dev_priv->rps.hw_lock);
Jesse Barnes1a01ab32012-11-02 11:14:00 -07003459 gen6_enable_rps(dev);
3460 gen6_update_ring_freq(dev);
Jesse Barnes4fc688c2012-11-02 11:14:01 -07003461 mutex_unlock(&dev_priv->rps.hw_lock);
Jesse Barnes1a01ab32012-11-02 11:14:00 -07003462}
3463
Daniel Vetter8090c6b2012-06-24 16:42:32 +02003464void intel_enable_gt_powersave(struct drm_device *dev)
3465{
Jesse Barnes1a01ab32012-11-02 11:14:00 -07003466 struct drm_i915_private *dev_priv = dev->dev_private;
3467
Daniel Vetter8090c6b2012-06-24 16:42:32 +02003468 if (IS_IRONLAKE_M(dev)) {
3469 ironlake_enable_drps(dev);
3470 ironlake_enable_rc6(dev);
3471 intel_init_emon(dev);
Eugeni Dodonov7cf50fc2012-07-02 11:51:06 -03003472 } else if ((IS_GEN6(dev) || IS_GEN7(dev)) && !IS_VALLEYVIEW(dev)) {
Jesse Barnes1a01ab32012-11-02 11:14:00 -07003473 /*
3474 * PCU communication is slow and this doesn't need to be
3475 * done at any specific time, so do this out of our fast path
3476 * to make resume and init faster.
3477 */
3478 schedule_delayed_work(&dev_priv->rps.delayed_resume_work,
3479 round_jiffies_up_relative(HZ));
Daniel Vetter8090c6b2012-06-24 16:42:32 +02003480 }
3481}
3482
Daniel Vetter3107bd42012-10-31 22:52:31 +01003483static void ibx_init_clock_gating(struct drm_device *dev)
3484{
3485 struct drm_i915_private *dev_priv = dev->dev_private;
3486
3487 /*
3488 * On Ibex Peak and Cougar Point, we need to disable clock
3489 * gating for the panel power sequencer or it will fail to
3490 * start up when no ports are active.
3491 */
3492 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE);
3493}
3494
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03003495static void ironlake_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03003496{
3497 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau231e54f2012-10-19 17:55:41 +01003498 uint32_t dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03003499
3500 /* Required for FBC */
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01003501 dspclk_gate |= ILK_DPFCRUNIT_CLOCK_GATE_DISABLE |
3502 ILK_DPFCUNIT_CLOCK_GATE_DISABLE |
3503 ILK_DPFDUNIT_CLOCK_GATE_ENABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03003504
3505 I915_WRITE(PCH_3DCGDIS0,
3506 MARIUNIT_CLOCK_GATE_DISABLE |
3507 SVSMUNIT_CLOCK_GATE_DISABLE);
3508 I915_WRITE(PCH_3DCGDIS1,
3509 VFMUNIT_CLOCK_GATE_DISABLE);
3510
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03003511 /*
3512 * According to the spec the following bits should be set in
3513 * order to enable memory self-refresh
3514 * The bit 22/21 of 0x42004
3515 * The bit 5 of 0x42020
3516 * The bit 15 of 0x45000
3517 */
3518 I915_WRITE(ILK_DISPLAY_CHICKEN2,
3519 (I915_READ(ILK_DISPLAY_CHICKEN2) |
3520 ILK_DPARB_GATE | ILK_VSDPFD_FULL));
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01003521 dspclk_gate |= ILK_DPARBUNIT_CLOCK_GATE_ENABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03003522 I915_WRITE(DISP_ARB_CTL,
3523 (I915_READ(DISP_ARB_CTL) |
3524 DISP_FBC_WM_DIS));
3525 I915_WRITE(WM3_LP_ILK, 0);
3526 I915_WRITE(WM2_LP_ILK, 0);
3527 I915_WRITE(WM1_LP_ILK, 0);
3528
3529 /*
3530 * Based on the document from hardware guys the following bits
3531 * should be set unconditionally in order to enable FBC.
3532 * The bit 22 of 0x42000
3533 * The bit 22 of 0x42004
3534 * The bit 7,8,9 of 0x42020.
3535 */
3536 if (IS_IRONLAKE_M(dev)) {
3537 I915_WRITE(ILK_DISPLAY_CHICKEN1,
3538 I915_READ(ILK_DISPLAY_CHICKEN1) |
3539 ILK_FBCQ_DIS);
3540 I915_WRITE(ILK_DISPLAY_CHICKEN2,
3541 I915_READ(ILK_DISPLAY_CHICKEN2) |
3542 ILK_DPARB_GATE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03003543 }
3544
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01003545 I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
3546
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03003547 I915_WRITE(ILK_DISPLAY_CHICKEN2,
3548 I915_READ(ILK_DISPLAY_CHICKEN2) |
3549 ILK_ELPIN_409_SELECT);
3550 I915_WRITE(_3D_CHICKEN2,
3551 _3D_CHICKEN2_WM_READ_PIPELINED << 16 |
3552 _3D_CHICKEN2_WM_READ_PIPELINED);
Daniel Vetter4358a372012-10-18 11:49:51 +02003553
3554 /* WaDisableRenderCachePipelinedFlush */
3555 I915_WRITE(CACHE_MODE_0,
3556 _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
Daniel Vetter3107bd42012-10-31 22:52:31 +01003557
3558 ibx_init_clock_gating(dev);
3559}
3560
3561static void cpt_init_clock_gating(struct drm_device *dev)
3562{
3563 struct drm_i915_private *dev_priv = dev->dev_private;
3564 int pipe;
3565
3566 /*
3567 * On Ibex Peak and Cougar Point, we need to disable clock
3568 * gating for the panel power sequencer or it will fail to
3569 * start up when no ports are active.
3570 */
3571 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE);
3572 I915_WRITE(SOUTH_CHICKEN2, I915_READ(SOUTH_CHICKEN2) |
3573 DPLS_EDP_PPS_FIX_DIS);
Takashi Iwai335c07b2012-12-11 11:46:29 +01003574 /* The below fixes the weird display corruption, a few pixels shifted
3575 * downward, on (only) LVDS of some HP laptops with IVY.
3576 */
3577 for_each_pipe(pipe)
3578 I915_WRITE(TRANS_CHICKEN2(pipe), TRANS_CHICKEN2_TIMING_OVERRIDE);
Daniel Vetter3107bd42012-10-31 22:52:31 +01003579 /* WADP0ClockGatingDisable */
3580 for_each_pipe(pipe) {
3581 I915_WRITE(TRANS_CHICKEN1(pipe),
3582 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
3583 }
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03003584}
3585
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01003586static void gen6_check_mch_setup(struct drm_device *dev)
3587{
3588 struct drm_i915_private *dev_priv = dev->dev_private;
3589 uint32_t tmp;
3590
3591 tmp = I915_READ(MCH_SSKPD);
3592 if ((tmp & MCH_SSKPD_WM0_MASK) != MCH_SSKPD_WM0_VAL) {
3593 DRM_INFO("Wrong MCH_SSKPD value: 0x%08x\n", tmp);
3594 DRM_INFO("This can cause pipe underruns and display issues.\n");
3595 DRM_INFO("Please upgrade your BIOS to fix this.\n");
3596 }
3597}
3598
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03003599static void gen6_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03003600{
3601 struct drm_i915_private *dev_priv = dev->dev_private;
3602 int pipe;
Damien Lespiau231e54f2012-10-19 17:55:41 +01003603 uint32_t dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03003604
Damien Lespiau231e54f2012-10-19 17:55:41 +01003605 I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03003606
3607 I915_WRITE(ILK_DISPLAY_CHICKEN2,
3608 I915_READ(ILK_DISPLAY_CHICKEN2) |
3609 ILK_ELPIN_409_SELECT);
3610
Daniel Vetter42839082012-12-14 23:38:28 +01003611 /* WaDisableHiZPlanesWhenMSAAEnabled */
3612 I915_WRITE(_3D_CHICKEN,
3613 _MASKED_BIT_ENABLE(_3D_CHICKEN_HIZ_PLANE_DISABLE_MSAA_4X_SNB));
3614
Daniel Vetter6547fbd2012-12-14 23:38:29 +01003615 /* WaSetupGtModeTdRowDispatch */
3616 if (IS_SNB_GT1(dev))
3617 I915_WRITE(GEN6_GT_MODE,
3618 _MASKED_BIT_ENABLE(GEN6_TD_FOUR_ROW_DISPATCH_DISABLE));
3619
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03003620 I915_WRITE(WM3_LP_ILK, 0);
3621 I915_WRITE(WM2_LP_ILK, 0);
3622 I915_WRITE(WM1_LP_ILK, 0);
3623
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03003624 I915_WRITE(CACHE_MODE_0,
Daniel Vetter50743292012-04-26 22:02:54 +02003625 _MASKED_BIT_DISABLE(CM0_STC_EVICT_DISABLE_LRA_SNB));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03003626
3627 I915_WRITE(GEN6_UCGCTL1,
3628 I915_READ(GEN6_UCGCTL1) |
3629 GEN6_BLBUNIT_CLOCK_GATE_DISABLE |
3630 GEN6_CSUNIT_CLOCK_GATE_DISABLE);
3631
3632 /* According to the BSpec vol1g, bit 12 (RCPBUNIT) clock
3633 * gating disable must be set. Failure to set it results in
3634 * flickering pixels due to Z write ordering failures after
3635 * some amount of runtime in the Mesa "fire" demo, and Unigine
3636 * Sanctuary and Tropics, and apparently anything else with
3637 * alpha test or pixel discard.
3638 *
3639 * According to the spec, bit 11 (RCCUNIT) must also be set,
3640 * but we didn't debug actual testcases to find it out.
Jesse Barnes0f846f82012-06-14 11:04:47 -07003641 *
3642 * Also apply WaDisableVDSUnitClockGating and
3643 * WaDisableRCPBUnitClockGating.
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03003644 */
3645 I915_WRITE(GEN6_UCGCTL2,
Jesse Barnes0f846f82012-06-14 11:04:47 -07003646 GEN7_VDSUNIT_CLOCK_GATE_DISABLE |
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03003647 GEN6_RCPBUNIT_CLOCK_GATE_DISABLE |
3648 GEN6_RCCUNIT_CLOCK_GATE_DISABLE);
3649
3650 /* Bspec says we need to always set all mask bits. */
Kenneth Graunke26b6e442012-10-07 08:51:07 -07003651 I915_WRITE(_3D_CHICKEN3, (0xFFFF << 16) |
3652 _3D_CHICKEN3_SF_DISABLE_FASTCLIP_CULL);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03003653
3654 /*
3655 * According to the spec the following bits should be
3656 * set in order to enable memory self-refresh and fbc:
3657 * The bit21 and bit22 of 0x42000
3658 * The bit21 and bit22 of 0x42004
3659 * The bit5 and bit7 of 0x42020
3660 * The bit14 of 0x70180
3661 * The bit14 of 0x71180
3662 */
3663 I915_WRITE(ILK_DISPLAY_CHICKEN1,
3664 I915_READ(ILK_DISPLAY_CHICKEN1) |
3665 ILK_FBCQ_DIS | ILK_PABSTRETCH_DIS);
3666 I915_WRITE(ILK_DISPLAY_CHICKEN2,
3667 I915_READ(ILK_DISPLAY_CHICKEN2) |
3668 ILK_DPARB_GATE | ILK_VSDPFD_FULL);
Damien Lespiau231e54f2012-10-19 17:55:41 +01003669 I915_WRITE(ILK_DSPCLK_GATE_D,
3670 I915_READ(ILK_DSPCLK_GATE_D) |
3671 ILK_DPARBUNIT_CLOCK_GATE_ENABLE |
3672 ILK_DPFDUNIT_CLOCK_GATE_ENABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03003673
Paulo Zanonib3bf0762012-11-20 13:27:44 -02003674 /* WaMbcDriverBootEnable */
Jesse Barnesb4ae3f22012-06-14 11:04:48 -07003675 I915_WRITE(GEN6_MBCTL, I915_READ(GEN6_MBCTL) |
3676 GEN6_MBCTL_ENABLE_BOOT_FETCH);
3677
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03003678 for_each_pipe(pipe) {
3679 I915_WRITE(DSPCNTR(pipe),
3680 I915_READ(DSPCNTR(pipe)) |
3681 DISPPLANE_TRICKLE_FEED_DISABLE);
3682 intel_flush_display_plane(dev_priv, pipe);
3683 }
Ben Widawskyf8f2ac92012-10-03 19:34:24 -07003684
3685 /* The default value should be 0x200 according to docs, but the two
3686 * platforms I checked have a 0 for this. (Maybe BIOS overrides?) */
3687 I915_WRITE(GEN6_GT_MODE, _MASKED_BIT_DISABLE(0xffff));
3688 I915_WRITE(GEN6_GT_MODE, _MASKED_BIT_ENABLE(GEN6_GT_MODE_HI));
Daniel Vetter3107bd42012-10-31 22:52:31 +01003689
3690 cpt_init_clock_gating(dev);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01003691
3692 gen6_check_mch_setup(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03003693}
3694
3695static void gen7_setup_fixed_func_scheduler(struct drm_i915_private *dev_priv)
3696{
3697 uint32_t reg = I915_READ(GEN7_FF_THREAD_MODE);
3698
3699 reg &= ~GEN7_FF_SCHED_MASK;
3700 reg |= GEN7_FF_TS_SCHED_HW;
3701 reg |= GEN7_FF_VS_SCHED_HW;
3702 reg |= GEN7_FF_DS_SCHED_HW;
3703
Ben Widawsky41c0b3a2013-01-26 11:52:00 -08003704 /* WaVSRefCountFullforceMissDisable */
3705 if (IS_HASWELL(dev_priv->dev))
3706 reg &= ~GEN7_FF_VS_REF_CNT_FFME;
3707
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03003708 I915_WRITE(GEN7_FF_THREAD_MODE, reg);
3709}
3710
Paulo Zanoni17a303e2012-11-20 15:12:07 -02003711static void lpt_init_clock_gating(struct drm_device *dev)
3712{
3713 struct drm_i915_private *dev_priv = dev->dev_private;
3714
3715 /*
3716 * TODO: this bit should only be enabled when really needed, then
3717 * disabled when not needed anymore in order to save power.
3718 */
3719 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE)
3720 I915_WRITE(SOUTH_DSPCLK_GATE_D,
3721 I915_READ(SOUTH_DSPCLK_GATE_D) |
3722 PCH_LP_PARTITION_LEVEL_DISABLE);
3723}
3724
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03003725static void haswell_init_clock_gating(struct drm_device *dev)
3726{
3727 struct drm_i915_private *dev_priv = dev->dev_private;
3728 int pipe;
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03003729
3730 I915_WRITE(WM3_LP_ILK, 0);
3731 I915_WRITE(WM2_LP_ILK, 0);
3732 I915_WRITE(WM1_LP_ILK, 0);
3733
3734 /* According to the spec, bit 13 (RCZUNIT) must be set on IVB.
3735 * This implements the WaDisableRCZUnitClockGating workaround.
3736 */
3737 I915_WRITE(GEN6_UCGCTL2, GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
3738
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03003739 /* Apply the WaDisableRHWOOptimizationForRenderHang workaround. */
3740 I915_WRITE(GEN7_COMMON_SLICE_CHICKEN1,
3741 GEN7_CSC1_RHWO_OPT_DISABLE_IN_RCC);
3742
3743 /* WaApplyL3ControlAndL3ChickenMode requires those two on Ivy Bridge */
3744 I915_WRITE(GEN7_L3CNTLREG1,
3745 GEN7_WA_FOR_GEN7_L3_CONTROL);
3746 I915_WRITE(GEN7_L3_CHICKEN_MODE_REGISTER,
3747 GEN7_WA_L3_CHICKEN_MODE);
3748
3749 /* This is required by WaCatErrorRejectionIssue */
3750 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
3751 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
3752 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
3753
3754 for_each_pipe(pipe) {
3755 I915_WRITE(DSPCNTR(pipe),
3756 I915_READ(DSPCNTR(pipe)) |
3757 DISPPLANE_TRICKLE_FEED_DISABLE);
3758 intel_flush_display_plane(dev_priv, pipe);
3759 }
3760
3761 gen7_setup_fixed_func_scheduler(dev_priv);
3762
3763 /* WaDisable4x2SubspanOptimization */
3764 I915_WRITE(CACHE_MODE_1,
3765 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Eugeni Dodonov1544d9d2012-07-02 11:51:10 -03003766
Paulo Zanonib3bf0762012-11-20 13:27:44 -02003767 /* WaMbcDriverBootEnable */
3768 I915_WRITE(GEN6_MBCTL, I915_READ(GEN6_MBCTL) |
3769 GEN6_MBCTL_ENABLE_BOOT_FETCH);
3770
Eugeni Dodonov1544d9d2012-07-02 11:51:10 -03003771 /* XXX: This is a workaround for early silicon revisions and should be
3772 * removed later.
3773 */
3774 I915_WRITE(WM_DBG,
3775 I915_READ(WM_DBG) |
3776 WM_DBG_DISALLOW_MULTIPLE_LP |
3777 WM_DBG_DISALLOW_SPRITE |
3778 WM_DBG_DISALLOW_MAXFIFO);
3779
Paulo Zanoni17a303e2012-11-20 15:12:07 -02003780 lpt_init_clock_gating(dev);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03003781}
3782
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03003783static void ivybridge_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03003784{
3785 struct drm_i915_private *dev_priv = dev->dev_private;
3786 int pipe;
Ben Widawsky20848222012-05-04 18:58:59 -07003787 uint32_t snpcr;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03003788
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03003789 I915_WRITE(WM3_LP_ILK, 0);
3790 I915_WRITE(WM2_LP_ILK, 0);
3791 I915_WRITE(WM1_LP_ILK, 0);
3792
Damien Lespiau231e54f2012-10-19 17:55:41 +01003793 I915_WRITE(ILK_DSPCLK_GATE_D, ILK_VRHUNIT_CLOCK_GATE_DISABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03003794
Jesse Barnes87f80202012-10-02 17:43:41 -05003795 /* WaDisableEarlyCull */
3796 I915_WRITE(_3D_CHICKEN3,
3797 _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
3798
Damien Lespiau62cb9442012-10-04 18:49:23 +01003799 /* WaDisableBackToBackFlipFix */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03003800 I915_WRITE(IVB_CHICKEN3,
3801 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
3802 CHICKEN3_DGMG_DONE_FIX_DISABLE);
3803
Jesse Barnes12f33822012-10-25 12:15:45 -07003804 /* WaDisablePSDDualDispatchEnable */
3805 if (IS_IVB_GT1(dev))
3806 I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
3807 _MASKED_BIT_ENABLE(GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
3808 else
3809 I915_WRITE(GEN7_HALF_SLICE_CHICKEN1_GT2,
3810 _MASKED_BIT_ENABLE(GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
3811
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03003812 /* Apply the WaDisableRHWOOptimizationForRenderHang workaround. */
3813 I915_WRITE(GEN7_COMMON_SLICE_CHICKEN1,
3814 GEN7_CSC1_RHWO_OPT_DISABLE_IN_RCC);
3815
3816 /* WaApplyL3ControlAndL3ChickenMode requires those two on Ivy Bridge */
3817 I915_WRITE(GEN7_L3CNTLREG1,
3818 GEN7_WA_FOR_GEN7_L3_CONTROL);
3819 I915_WRITE(GEN7_L3_CHICKEN_MODE_REGISTER,
Jesse Barnes8ab43972012-10-25 12:15:42 -07003820 GEN7_WA_L3_CHICKEN_MODE);
3821 if (IS_IVB_GT1(dev))
3822 I915_WRITE(GEN7_ROW_CHICKEN2,
3823 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
3824 else
3825 I915_WRITE(GEN7_ROW_CHICKEN2_GT2,
3826 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
3827
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03003828
Jesse Barnes61939d92012-10-02 17:43:38 -05003829 /* WaForceL3Serialization */
3830 I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
3831 ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
3832
Jesse Barnes0f846f82012-06-14 11:04:47 -07003833 /* According to the BSpec vol1g, bit 12 (RCPBUNIT) clock
3834 * gating disable must be set. Failure to set it results in
3835 * flickering pixels due to Z write ordering failures after
3836 * some amount of runtime in the Mesa "fire" demo, and Unigine
3837 * Sanctuary and Tropics, and apparently anything else with
3838 * alpha test or pixel discard.
3839 *
3840 * According to the spec, bit 11 (RCCUNIT) must also be set,
3841 * but we didn't debug actual testcases to find it out.
3842 *
3843 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
3844 * This implements the WaDisableRCZUnitClockGating workaround.
3845 */
3846 I915_WRITE(GEN6_UCGCTL2,
3847 GEN6_RCZUNIT_CLOCK_GATE_DISABLE |
3848 GEN6_RCCUNIT_CLOCK_GATE_DISABLE);
3849
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03003850 /* This is required by WaCatErrorRejectionIssue */
3851 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
3852 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
3853 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
3854
3855 for_each_pipe(pipe) {
3856 I915_WRITE(DSPCNTR(pipe),
3857 I915_READ(DSPCNTR(pipe)) |
3858 DISPPLANE_TRICKLE_FEED_DISABLE);
3859 intel_flush_display_plane(dev_priv, pipe);
3860 }
3861
Paulo Zanonib3bf0762012-11-20 13:27:44 -02003862 /* WaMbcDriverBootEnable */
Jesse Barnesb4ae3f22012-06-14 11:04:48 -07003863 I915_WRITE(GEN6_MBCTL, I915_READ(GEN6_MBCTL) |
3864 GEN6_MBCTL_ENABLE_BOOT_FETCH);
3865
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03003866 gen7_setup_fixed_func_scheduler(dev_priv);
Daniel Vetter97e19302012-04-24 16:00:21 +02003867
3868 /* WaDisable4x2SubspanOptimization */
3869 I915_WRITE(CACHE_MODE_1,
3870 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Ben Widawsky20848222012-05-04 18:58:59 -07003871
3872 snpcr = I915_READ(GEN6_MBCUNIT_SNPCR);
3873 snpcr &= ~GEN6_MBC_SNPCR_MASK;
3874 snpcr |= GEN6_MBC_SNPCR_MED;
3875 I915_WRITE(GEN6_MBCUNIT_SNPCR, snpcr);
Daniel Vetter3107bd42012-10-31 22:52:31 +01003876
3877 cpt_init_clock_gating(dev);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01003878
3879 gen6_check_mch_setup(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03003880}
3881
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03003882static void valleyview_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03003883{
3884 struct drm_i915_private *dev_priv = dev->dev_private;
3885 int pipe;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03003886
3887 I915_WRITE(WM3_LP_ILK, 0);
3888 I915_WRITE(WM2_LP_ILK, 0);
3889 I915_WRITE(WM1_LP_ILK, 0);
3890
Damien Lespiau231e54f2012-10-19 17:55:41 +01003891 I915_WRITE(ILK_DSPCLK_GATE_D, ILK_VRHUNIT_CLOCK_GATE_DISABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03003892
Jesse Barnes87f80202012-10-02 17:43:41 -05003893 /* WaDisableEarlyCull */
3894 I915_WRITE(_3D_CHICKEN3,
3895 _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
3896
Damien Lespiau62cb9442012-10-04 18:49:23 +01003897 /* WaDisableBackToBackFlipFix */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03003898 I915_WRITE(IVB_CHICKEN3,
3899 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
3900 CHICKEN3_DGMG_DONE_FIX_DISABLE);
3901
Jesse Barnes12f33822012-10-25 12:15:45 -07003902 I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
3903 _MASKED_BIT_ENABLE(GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
3904
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03003905 /* Apply the WaDisableRHWOOptimizationForRenderHang workaround. */
3906 I915_WRITE(GEN7_COMMON_SLICE_CHICKEN1,
3907 GEN7_CSC1_RHWO_OPT_DISABLE_IN_RCC);
3908
3909 /* WaApplyL3ControlAndL3ChickenMode requires those two on Ivy Bridge */
Jesse Barnesd0cf5ea2012-10-25 12:15:41 -07003910 I915_WRITE(GEN7_L3CNTLREG1, I915_READ(GEN7_L3CNTLREG1) | GEN7_L3AGDIS);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03003911 I915_WRITE(GEN7_L3_CHICKEN_MODE_REGISTER, GEN7_WA_L3_CHICKEN_MODE);
3912
Jesse Barnes61939d92012-10-02 17:43:38 -05003913 /* WaForceL3Serialization */
3914 I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
3915 ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
3916
Jesse Barnes8ab43972012-10-25 12:15:42 -07003917 /* WaDisableDopClockGating */
3918 I915_WRITE(GEN7_ROW_CHICKEN2,
3919 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
3920
Jesse Barnes5c9664d2012-10-25 12:15:43 -07003921 /* WaForceL3Serialization */
3922 I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
3923 ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
3924
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03003925 /* This is required by WaCatErrorRejectionIssue */
3926 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
3927 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
3928 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
3929
Paulo Zanonib3bf0762012-11-20 13:27:44 -02003930 /* WaMbcDriverBootEnable */
Jesse Barnesb4ae3f22012-06-14 11:04:48 -07003931 I915_WRITE(GEN6_MBCTL, I915_READ(GEN6_MBCTL) |
3932 GEN6_MBCTL_ENABLE_BOOT_FETCH);
3933
Jesse Barnes0f846f82012-06-14 11:04:47 -07003934
3935 /* According to the BSpec vol1g, bit 12 (RCPBUNIT) clock
3936 * gating disable must be set. Failure to set it results in
3937 * flickering pixels due to Z write ordering failures after
3938 * some amount of runtime in the Mesa "fire" demo, and Unigine
3939 * Sanctuary and Tropics, and apparently anything else with
3940 * alpha test or pixel discard.
3941 *
3942 * According to the spec, bit 11 (RCCUNIT) must also be set,
3943 * but we didn't debug actual testcases to find it out.
3944 *
3945 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
3946 * This implements the WaDisableRCZUnitClockGating workaround.
3947 *
3948 * Also apply WaDisableVDSUnitClockGating and
3949 * WaDisableRCPBUnitClockGating.
3950 */
3951 I915_WRITE(GEN6_UCGCTL2,
3952 GEN7_VDSUNIT_CLOCK_GATE_DISABLE |
Jesse Barnes6edaa7f2012-06-14 11:04:49 -07003953 GEN7_TDLUNIT_CLOCK_GATE_DISABLE |
Jesse Barnes0f846f82012-06-14 11:04:47 -07003954 GEN6_RCZUNIT_CLOCK_GATE_DISABLE |
3955 GEN6_RCPBUNIT_CLOCK_GATE_DISABLE |
3956 GEN6_RCCUNIT_CLOCK_GATE_DISABLE);
3957
Jesse Barnese3f33d42012-06-14 11:04:50 -07003958 I915_WRITE(GEN7_UCGCTL4, GEN7_L3BANK2X_CLOCK_GATE_DISABLE);
3959
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03003960 for_each_pipe(pipe) {
3961 I915_WRITE(DSPCNTR(pipe),
3962 I915_READ(DSPCNTR(pipe)) |
3963 DISPPLANE_TRICKLE_FEED_DISABLE);
3964 intel_flush_display_plane(dev_priv, pipe);
3965 }
3966
Daniel Vetter6b26c862012-04-24 14:04:12 +02003967 I915_WRITE(CACHE_MODE_1,
3968 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Jesse Barnes79831172012-06-20 10:53:12 -07003969
3970 /*
3971 * On ValleyView, the GUnit needs to signal the GT
3972 * when flip and other events complete. So enable
3973 * all the GUnit->GT interrupts here
3974 */
3975 I915_WRITE(VLV_DPFLIPSTAT, PIPEB_LINE_COMPARE_INT_EN |
3976 PIPEB_HLINE_INT_EN | PIPEB_VBLANK_INT_EN |
3977 SPRITED_FLIPDONE_INT_EN | SPRITEC_FLIPDONE_INT_EN |
3978 PLANEB_FLIPDONE_INT_EN | PIPEA_LINE_COMPARE_INT_EN |
3979 PIPEA_HLINE_INT_EN | PIPEA_VBLANK_INT_EN |
3980 SPRITEB_FLIPDONE_INT_EN | SPRITEA_FLIPDONE_INT_EN |
3981 PLANEA_FLIPDONE_INT_EN);
Jesse Barnes2d809572012-10-25 12:15:44 -07003982
3983 /*
3984 * WaDisableVLVClockGating_VBIIssue
3985 * Disable clock gating on th GCFG unit to prevent a delay
3986 * in the reporting of vblank events.
3987 */
3988 I915_WRITE(VLV_GUNIT_CLOCK_GATE, GCFG_DIS);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03003989}
3990
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03003991static void g4x_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03003992{
3993 struct drm_i915_private *dev_priv = dev->dev_private;
3994 uint32_t dspclk_gate;
3995
3996 I915_WRITE(RENCLK_GATE_D1, 0);
3997 I915_WRITE(RENCLK_GATE_D2, VF_UNIT_CLOCK_GATE_DISABLE |
3998 GS_UNIT_CLOCK_GATE_DISABLE |
3999 CL_UNIT_CLOCK_GATE_DISABLE);
4000 I915_WRITE(RAMCLK_GATE_D, 0);
4001 dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE |
4002 OVRUNIT_CLOCK_GATE_DISABLE |
4003 OVCUNIT_CLOCK_GATE_DISABLE;
4004 if (IS_GM45(dev))
4005 dspclk_gate |= DSSUNIT_CLOCK_GATE_DISABLE;
4006 I915_WRITE(DSPCLK_GATE_D, dspclk_gate);
Daniel Vetter4358a372012-10-18 11:49:51 +02004007
4008 /* WaDisableRenderCachePipelinedFlush */
4009 I915_WRITE(CACHE_MODE_0,
4010 _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004011}
4012
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03004013static void crestline_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004014{
4015 struct drm_i915_private *dev_priv = dev->dev_private;
4016
4017 I915_WRITE(RENCLK_GATE_D1, I965_RCC_CLOCK_GATE_DISABLE);
4018 I915_WRITE(RENCLK_GATE_D2, 0);
4019 I915_WRITE(DSPCLK_GATE_D, 0);
4020 I915_WRITE(RAMCLK_GATE_D, 0);
4021 I915_WRITE16(DEUC, 0);
4022}
4023
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03004024static void broadwater_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004025{
4026 struct drm_i915_private *dev_priv = dev->dev_private;
4027
4028 I915_WRITE(RENCLK_GATE_D1, I965_RCZ_CLOCK_GATE_DISABLE |
4029 I965_RCC_CLOCK_GATE_DISABLE |
4030 I965_RCPB_CLOCK_GATE_DISABLE |
4031 I965_ISC_CLOCK_GATE_DISABLE |
4032 I965_FBC_CLOCK_GATE_DISABLE);
4033 I915_WRITE(RENCLK_GATE_D2, 0);
4034}
4035
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03004036static void gen3_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004037{
4038 struct drm_i915_private *dev_priv = dev->dev_private;
4039 u32 dstate = I915_READ(D_STATE);
4040
4041 dstate |= DSTATE_PLL_D3_OFF | DSTATE_GFX_CLOCK_GATING |
4042 DSTATE_DOT_CLOCK_GATING;
4043 I915_WRITE(D_STATE, dstate);
Chris Wilson13a86b82012-04-24 14:51:43 +01004044
4045 if (IS_PINEVIEW(dev))
4046 I915_WRITE(ECOSKPD, _MASKED_BIT_ENABLE(ECO_GATING_CX_ONLY));
Daniel Vetter974a3b02012-09-09 11:54:16 +02004047
4048 /* IIR "flip pending" means done if this bit is set */
4049 I915_WRITE(ECOSKPD, _MASKED_BIT_DISABLE(ECO_FLIP_DONE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004050}
4051
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03004052static void i85x_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004053{
4054 struct drm_i915_private *dev_priv = dev->dev_private;
4055
4056 I915_WRITE(RENCLK_GATE_D1, SV_CLOCK_GATE_DISABLE);
4057}
4058
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03004059static void i830_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004060{
4061 struct drm_i915_private *dev_priv = dev->dev_private;
4062
4063 I915_WRITE(DSPCLK_GATE_D, OVRUNIT_CLOCK_GATE_DISABLE);
4064}
4065
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004066void intel_init_clock_gating(struct drm_device *dev)
4067{
4068 struct drm_i915_private *dev_priv = dev->dev_private;
4069
4070 dev_priv->display.init_clock_gating(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004071}
4072
Paulo Zanonicb107992013-01-25 16:59:15 -02004073void intel_set_power_well(struct drm_device *dev, bool enable)
Eugeni Dodonovd0d3e512012-05-09 15:37:16 -03004074{
4075 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanonifa42e232013-01-25 16:59:11 -02004076 bool is_enabled, enable_requested;
4077 uint32_t tmp;
Eugeni Dodonovd0d3e512012-05-09 15:37:16 -03004078
4079 if (!IS_HASWELL(dev))
4080 return;
4081
Paulo Zanoni2124b722013-03-22 14:07:23 -03004082 if (!i915_disable_power_well && !enable)
4083 return;
4084
Paulo Zanonifa42e232013-01-25 16:59:11 -02004085 tmp = I915_READ(HSW_PWR_WELL_DRIVER);
4086 is_enabled = tmp & HSW_PWR_WELL_STATE;
4087 enable_requested = tmp & HSW_PWR_WELL_ENABLE;
Eugeni Dodonovd0d3e512012-05-09 15:37:16 -03004088
Paulo Zanonifa42e232013-01-25 16:59:11 -02004089 if (enable) {
4090 if (!enable_requested)
4091 I915_WRITE(HSW_PWR_WELL_DRIVER, HSW_PWR_WELL_ENABLE);
Eugeni Dodonovd0d3e512012-05-09 15:37:16 -03004092
Paulo Zanonifa42e232013-01-25 16:59:11 -02004093 if (!is_enabled) {
4094 DRM_DEBUG_KMS("Enabling power well\n");
4095 if (wait_for((I915_READ(HSW_PWR_WELL_DRIVER) &
4096 HSW_PWR_WELL_STATE), 20))
4097 DRM_ERROR("Timeout enabling power well\n");
4098 }
4099 } else {
4100 if (enable_requested) {
4101 I915_WRITE(HSW_PWR_WELL_DRIVER, 0);
4102 DRM_DEBUG_KMS("Requesting to disable the power well\n");
Eugeni Dodonovd0d3e512012-05-09 15:37:16 -03004103 }
4104 }
Paulo Zanonifa42e232013-01-25 16:59:11 -02004105}
Eugeni Dodonovd0d3e512012-05-09 15:37:16 -03004106
Paulo Zanonifa42e232013-01-25 16:59:11 -02004107/*
4108 * Starting with Haswell, we have a "Power Down Well" that can be turned off
4109 * when not needed anymore. We have 4 registers that can request the power well
4110 * to be enabled, and it will only be disabled if none of the registers is
4111 * requesting it to be enabled.
4112 */
4113void intel_init_power_well(struct drm_device *dev)
4114{
4115 struct drm_i915_private *dev_priv = dev->dev_private;
Eugeni Dodonovd0d3e512012-05-09 15:37:16 -03004116
4117 if (!IS_HASWELL(dev))
4118 return;
4119
Paulo Zanonifa42e232013-01-25 16:59:11 -02004120 /* For now, we need the power well to be always enabled. */
4121 intel_set_power_well(dev, true);
Eugeni Dodonovd0d3e512012-05-09 15:37:16 -03004122
Paulo Zanonifa42e232013-01-25 16:59:11 -02004123 /* We're taking over the BIOS, so clear any requests made by it since
4124 * the driver is in charge now. */
4125 if (I915_READ(HSW_PWR_WELL_BIOS) & HSW_PWR_WELL_ENABLE)
4126 I915_WRITE(HSW_PWR_WELL_BIOS, 0);
Eugeni Dodonovd0d3e512012-05-09 15:37:16 -03004127}
4128
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03004129/* Set up chip specific power management-related functions */
4130void intel_init_pm(struct drm_device *dev)
4131{
4132 struct drm_i915_private *dev_priv = dev->dev_private;
4133
4134 if (I915_HAS_FBC(dev)) {
4135 if (HAS_PCH_SPLIT(dev)) {
4136 dev_priv->display.fbc_enabled = ironlake_fbc_enabled;
4137 dev_priv->display.enable_fbc = ironlake_enable_fbc;
4138 dev_priv->display.disable_fbc = ironlake_disable_fbc;
4139 } else if (IS_GM45(dev)) {
4140 dev_priv->display.fbc_enabled = g4x_fbc_enabled;
4141 dev_priv->display.enable_fbc = g4x_enable_fbc;
4142 dev_priv->display.disable_fbc = g4x_disable_fbc;
4143 } else if (IS_CRESTLINE(dev)) {
4144 dev_priv->display.fbc_enabled = i8xx_fbc_enabled;
4145 dev_priv->display.enable_fbc = i8xx_enable_fbc;
4146 dev_priv->display.disable_fbc = i8xx_disable_fbc;
4147 }
4148 /* 855GM needs testing */
4149 }
4150
Daniel Vetterc921aba2012-04-26 23:28:17 +02004151 /* For cxsr */
4152 if (IS_PINEVIEW(dev))
4153 i915_pineview_get_mem_freq(dev);
4154 else if (IS_GEN5(dev))
4155 i915_ironlake_get_mem_freq(dev);
4156
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03004157 /* For FIFO watermark updates */
4158 if (HAS_PCH_SPLIT(dev)) {
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03004159 if (IS_GEN5(dev)) {
4160 if (I915_READ(MLTR_ILK) & ILK_SRLT_MASK)
4161 dev_priv->display.update_wm = ironlake_update_wm;
4162 else {
4163 DRM_DEBUG_KMS("Failed to get proper latency. "
4164 "Disable CxSR\n");
4165 dev_priv->display.update_wm = NULL;
4166 }
4167 dev_priv->display.init_clock_gating = ironlake_init_clock_gating;
4168 } else if (IS_GEN6(dev)) {
4169 if (SNB_READ_WM0_LATENCY()) {
4170 dev_priv->display.update_wm = sandybridge_update_wm;
4171 dev_priv->display.update_sprite_wm = sandybridge_update_sprite_wm;
4172 } else {
4173 DRM_DEBUG_KMS("Failed to read display plane latency. "
4174 "Disable CxSR\n");
4175 dev_priv->display.update_wm = NULL;
4176 }
4177 dev_priv->display.init_clock_gating = gen6_init_clock_gating;
4178 } else if (IS_IVYBRIDGE(dev)) {
4179 /* FIXME: detect B0+ stepping and use auto training */
4180 if (SNB_READ_WM0_LATENCY()) {
Chris Wilsonc43d0182012-12-11 12:01:42 +00004181 dev_priv->display.update_wm = ivybridge_update_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03004182 dev_priv->display.update_sprite_wm = sandybridge_update_sprite_wm;
4183 } else {
4184 DRM_DEBUG_KMS("Failed to read display plane latency. "
4185 "Disable CxSR\n");
4186 dev_priv->display.update_wm = NULL;
4187 }
4188 dev_priv->display.init_clock_gating = ivybridge_init_clock_gating;
Eugeni Dodonov6b8a5ee2012-05-09 15:37:23 -03004189 } else if (IS_HASWELL(dev)) {
4190 if (SNB_READ_WM0_LATENCY()) {
4191 dev_priv->display.update_wm = sandybridge_update_wm;
4192 dev_priv->display.update_sprite_wm = sandybridge_update_sprite_wm;
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03004193 dev_priv->display.update_linetime_wm = haswell_update_linetime_wm;
Eugeni Dodonov6b8a5ee2012-05-09 15:37:23 -03004194 } else {
4195 DRM_DEBUG_KMS("Failed to read display plane latency. "
4196 "Disable CxSR\n");
4197 dev_priv->display.update_wm = NULL;
4198 }
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03004199 dev_priv->display.init_clock_gating = haswell_init_clock_gating;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03004200 } else
4201 dev_priv->display.update_wm = NULL;
4202 } else if (IS_VALLEYVIEW(dev)) {
4203 dev_priv->display.update_wm = valleyview_update_wm;
4204 dev_priv->display.init_clock_gating =
4205 valleyview_init_clock_gating;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03004206 } else if (IS_PINEVIEW(dev)) {
4207 if (!intel_get_cxsr_latency(IS_PINEVIEW_G(dev),
4208 dev_priv->is_ddr3,
4209 dev_priv->fsb_freq,
4210 dev_priv->mem_freq)) {
4211 DRM_INFO("failed to find known CxSR latency "
4212 "(found ddr%s fsb freq %d, mem freq %d), "
4213 "disabling CxSR\n",
4214 (dev_priv->is_ddr3 == 1) ? "3" : "2",
4215 dev_priv->fsb_freq, dev_priv->mem_freq);
4216 /* Disable CxSR and never update its watermark again */
4217 pineview_disable_cxsr(dev);
4218 dev_priv->display.update_wm = NULL;
4219 } else
4220 dev_priv->display.update_wm = pineview_update_wm;
4221 dev_priv->display.init_clock_gating = gen3_init_clock_gating;
4222 } else if (IS_G4X(dev)) {
4223 dev_priv->display.update_wm = g4x_update_wm;
4224 dev_priv->display.init_clock_gating = g4x_init_clock_gating;
4225 } else if (IS_GEN4(dev)) {
4226 dev_priv->display.update_wm = i965_update_wm;
4227 if (IS_CRESTLINE(dev))
4228 dev_priv->display.init_clock_gating = crestline_init_clock_gating;
4229 else if (IS_BROADWATER(dev))
4230 dev_priv->display.init_clock_gating = broadwater_init_clock_gating;
4231 } else if (IS_GEN3(dev)) {
4232 dev_priv->display.update_wm = i9xx_update_wm;
4233 dev_priv->display.get_fifo_size = i9xx_get_fifo_size;
4234 dev_priv->display.init_clock_gating = gen3_init_clock_gating;
4235 } else if (IS_I865G(dev)) {
4236 dev_priv->display.update_wm = i830_update_wm;
4237 dev_priv->display.init_clock_gating = i85x_init_clock_gating;
4238 dev_priv->display.get_fifo_size = i830_get_fifo_size;
4239 } else if (IS_I85X(dev)) {
4240 dev_priv->display.update_wm = i9xx_update_wm;
4241 dev_priv->display.get_fifo_size = i85x_get_fifo_size;
4242 dev_priv->display.init_clock_gating = i85x_init_clock_gating;
4243 } else {
4244 dev_priv->display.update_wm = i830_update_wm;
4245 dev_priv->display.init_clock_gating = i830_init_clock_gating;
4246 if (IS_845G(dev))
4247 dev_priv->display.get_fifo_size = i845_get_fifo_size;
4248 else
4249 dev_priv->display.get_fifo_size = i830_get_fifo_size;
4250 }
4251}
4252
Eugeni Dodonov65901902012-07-02 11:51:11 -03004253static void __gen6_gt_wait_for_thread_c0(struct drm_i915_private *dev_priv)
4254{
4255 u32 gt_thread_status_mask;
4256
4257 if (IS_HASWELL(dev_priv->dev))
4258 gt_thread_status_mask = GEN6_GT_THREAD_STATUS_CORE_MASK_HSW;
4259 else
4260 gt_thread_status_mask = GEN6_GT_THREAD_STATUS_CORE_MASK;
4261
4262 /* w/a for a sporadic read returning 0 by waiting for the GT
4263 * thread to wake up.
4264 */
4265 if (wait_for_atomic_us((I915_READ_NOTRACE(GEN6_GT_THREAD_STATUS_REG) & gt_thread_status_mask) == 0, 500))
4266 DRM_ERROR("GT thread status wait timed out\n");
4267}
4268
Chris Wilson16995a92012-10-18 11:46:10 +01004269static void __gen6_gt_force_wake_reset(struct drm_i915_private *dev_priv)
4270{
4271 I915_WRITE_NOTRACE(FORCEWAKE, 0);
4272 POSTING_READ(ECOBUS); /* something from same cacheline, but !FORCEWAKE */
4273}
4274
Eugeni Dodonov65901902012-07-02 11:51:11 -03004275static void __gen6_gt_force_wake_get(struct drm_i915_private *dev_priv)
4276{
4277 u32 forcewake_ack;
4278
4279 if (IS_HASWELL(dev_priv->dev))
4280 forcewake_ack = FORCEWAKE_ACK_HSW;
4281 else
4282 forcewake_ack = FORCEWAKE_ACK;
4283
Ben Widawsky057d3862012-09-01 22:59:49 -07004284 if (wait_for_atomic((I915_READ_NOTRACE(forcewake_ack) & 1) == 0,
4285 FORCEWAKE_ACK_TIMEOUT_MS))
Daniel Vetter8a038fd2012-08-24 17:26:21 +02004286 DRM_ERROR("Timed out waiting for forcewake old ack to clear.\n");
Eugeni Dodonov65901902012-07-02 11:51:11 -03004287
Chris Wilsonc5836c22012-10-17 12:09:55 +01004288 I915_WRITE_NOTRACE(FORCEWAKE, FORCEWAKE_KERNEL);
Ben Widawsky8dee3ee2012-09-01 22:59:50 -07004289 POSTING_READ(ECOBUS); /* something from same cacheline, but !FORCEWAKE */
Eugeni Dodonov65901902012-07-02 11:51:11 -03004290
Ben Widawsky057d3862012-09-01 22:59:49 -07004291 if (wait_for_atomic((I915_READ_NOTRACE(forcewake_ack) & 1),
4292 FORCEWAKE_ACK_TIMEOUT_MS))
Daniel Vetter8a038fd2012-08-24 17:26:21 +02004293 DRM_ERROR("Timed out waiting for forcewake to ack request.\n");
Eugeni Dodonov65901902012-07-02 11:51:11 -03004294
4295 __gen6_gt_wait_for_thread_c0(dev_priv);
4296}
4297
Chris Wilson16995a92012-10-18 11:46:10 +01004298static void __gen6_gt_force_wake_mt_reset(struct drm_i915_private *dev_priv)
4299{
4300 I915_WRITE_NOTRACE(FORCEWAKE_MT, _MASKED_BIT_DISABLE(0xffff));
Jani Nikulab5144072013-01-17 10:24:09 +02004301 /* something from same cacheline, but !FORCEWAKE_MT */
4302 POSTING_READ(ECOBUS);
Chris Wilson16995a92012-10-18 11:46:10 +01004303}
4304
Eugeni Dodonov65901902012-07-02 11:51:11 -03004305static void __gen6_gt_force_wake_mt_get(struct drm_i915_private *dev_priv)
4306{
4307 u32 forcewake_ack;
4308
4309 if (IS_HASWELL(dev_priv->dev))
4310 forcewake_ack = FORCEWAKE_ACK_HSW;
4311 else
4312 forcewake_ack = FORCEWAKE_MT_ACK;
4313
Ben Widawsky057d3862012-09-01 22:59:49 -07004314 if (wait_for_atomic((I915_READ_NOTRACE(forcewake_ack) & 1) == 0,
4315 FORCEWAKE_ACK_TIMEOUT_MS))
Daniel Vetter8a038fd2012-08-24 17:26:21 +02004316 DRM_ERROR("Timed out waiting for forcewake old ack to clear.\n");
Eugeni Dodonov65901902012-07-02 11:51:11 -03004317
Chris Wilsonc5836c22012-10-17 12:09:55 +01004318 I915_WRITE_NOTRACE(FORCEWAKE_MT, _MASKED_BIT_ENABLE(FORCEWAKE_KERNEL));
Jani Nikulab5144072013-01-17 10:24:09 +02004319 /* something from same cacheline, but !FORCEWAKE_MT */
4320 POSTING_READ(ECOBUS);
Eugeni Dodonov65901902012-07-02 11:51:11 -03004321
Ben Widawsky057d3862012-09-01 22:59:49 -07004322 if (wait_for_atomic((I915_READ_NOTRACE(forcewake_ack) & 1),
4323 FORCEWAKE_ACK_TIMEOUT_MS))
Daniel Vetter8a038fd2012-08-24 17:26:21 +02004324 DRM_ERROR("Timed out waiting for forcewake to ack request.\n");
Eugeni Dodonov65901902012-07-02 11:51:11 -03004325
4326 __gen6_gt_wait_for_thread_c0(dev_priv);
4327}
4328
4329/*
4330 * Generally this is called implicitly by the register read function. However,
4331 * if some sequence requires the GT to not power down then this function should
4332 * be called at the beginning of the sequence followed by a call to
4333 * gen6_gt_force_wake_put() at the end of the sequence.
4334 */
4335void gen6_gt_force_wake_get(struct drm_i915_private *dev_priv)
4336{
4337 unsigned long irqflags;
4338
4339 spin_lock_irqsave(&dev_priv->gt_lock, irqflags);
4340 if (dev_priv->forcewake_count++ == 0)
4341 dev_priv->gt.force_wake_get(dev_priv);
4342 spin_unlock_irqrestore(&dev_priv->gt_lock, irqflags);
4343}
4344
4345void gen6_gt_check_fifodbg(struct drm_i915_private *dev_priv)
4346{
4347 u32 gtfifodbg;
4348 gtfifodbg = I915_READ_NOTRACE(GTFIFODBG);
4349 if (WARN(gtfifodbg & GT_FIFO_CPU_ERROR_MASK,
4350 "MMIO read or write has been dropped %x\n", gtfifodbg))
4351 I915_WRITE_NOTRACE(GTFIFODBG, GT_FIFO_CPU_ERROR_MASK);
4352}
4353
4354static void __gen6_gt_force_wake_put(struct drm_i915_private *dev_priv)
4355{
4356 I915_WRITE_NOTRACE(FORCEWAKE, 0);
Jani Nikulab5144072013-01-17 10:24:09 +02004357 /* something from same cacheline, but !FORCEWAKE */
4358 POSTING_READ(ECOBUS);
Eugeni Dodonov65901902012-07-02 11:51:11 -03004359 gen6_gt_check_fifodbg(dev_priv);
4360}
4361
4362static void __gen6_gt_force_wake_mt_put(struct drm_i915_private *dev_priv)
4363{
Chris Wilsonc5836c22012-10-17 12:09:55 +01004364 I915_WRITE_NOTRACE(FORCEWAKE_MT, _MASKED_BIT_DISABLE(FORCEWAKE_KERNEL));
Jani Nikulab5144072013-01-17 10:24:09 +02004365 /* something from same cacheline, but !FORCEWAKE_MT */
4366 POSTING_READ(ECOBUS);
Eugeni Dodonov65901902012-07-02 11:51:11 -03004367 gen6_gt_check_fifodbg(dev_priv);
4368}
4369
4370/*
4371 * see gen6_gt_force_wake_get()
4372 */
4373void gen6_gt_force_wake_put(struct drm_i915_private *dev_priv)
4374{
4375 unsigned long irqflags;
4376
4377 spin_lock_irqsave(&dev_priv->gt_lock, irqflags);
4378 if (--dev_priv->forcewake_count == 0)
4379 dev_priv->gt.force_wake_put(dev_priv);
4380 spin_unlock_irqrestore(&dev_priv->gt_lock, irqflags);
4381}
4382
4383int __gen6_gt_wait_for_fifo(struct drm_i915_private *dev_priv)
4384{
4385 int ret = 0;
4386
4387 if (dev_priv->gt_fifo_count < GT_FIFO_NUM_RESERVED_ENTRIES) {
4388 int loop = 500;
4389 u32 fifo = I915_READ_NOTRACE(GT_FIFO_FREE_ENTRIES);
4390 while (fifo <= GT_FIFO_NUM_RESERVED_ENTRIES && loop--) {
4391 udelay(10);
4392 fifo = I915_READ_NOTRACE(GT_FIFO_FREE_ENTRIES);
4393 }
4394 if (WARN_ON(loop < 0 && fifo <= GT_FIFO_NUM_RESERVED_ENTRIES))
4395 ++ret;
4396 dev_priv->gt_fifo_count = fifo;
4397 }
4398 dev_priv->gt_fifo_count--;
4399
4400 return ret;
4401}
4402
Chris Wilson16995a92012-10-18 11:46:10 +01004403static void vlv_force_wake_reset(struct drm_i915_private *dev_priv)
4404{
4405 I915_WRITE_NOTRACE(FORCEWAKE_VLV, _MASKED_BIT_DISABLE(0xffff));
Jani Nikulab5144072013-01-17 10:24:09 +02004406 /* something from same cacheline, but !FORCEWAKE_VLV */
4407 POSTING_READ(FORCEWAKE_ACK_VLV);
Chris Wilson16995a92012-10-18 11:46:10 +01004408}
4409
Eugeni Dodonov65901902012-07-02 11:51:11 -03004410static void vlv_force_wake_get(struct drm_i915_private *dev_priv)
4411{
Ben Widawsky057d3862012-09-01 22:59:49 -07004412 if (wait_for_atomic((I915_READ_NOTRACE(FORCEWAKE_ACK_VLV) & 1) == 0,
4413 FORCEWAKE_ACK_TIMEOUT_MS))
Daniel Vetter8a038fd2012-08-24 17:26:21 +02004414 DRM_ERROR("Timed out waiting for forcewake old ack to clear.\n");
Eugeni Dodonov65901902012-07-02 11:51:11 -03004415
Chris Wilsonc5836c22012-10-17 12:09:55 +01004416 I915_WRITE_NOTRACE(FORCEWAKE_VLV, _MASKED_BIT_ENABLE(FORCEWAKE_KERNEL));
Eugeni Dodonov65901902012-07-02 11:51:11 -03004417
Ben Widawsky057d3862012-09-01 22:59:49 -07004418 if (wait_for_atomic((I915_READ_NOTRACE(FORCEWAKE_ACK_VLV) & 1),
4419 FORCEWAKE_ACK_TIMEOUT_MS))
Daniel Vetter8a038fd2012-08-24 17:26:21 +02004420 DRM_ERROR("Timed out waiting for forcewake to ack request.\n");
Eugeni Dodonov65901902012-07-02 11:51:11 -03004421
4422 __gen6_gt_wait_for_thread_c0(dev_priv);
4423}
4424
4425static void vlv_force_wake_put(struct drm_i915_private *dev_priv)
4426{
Chris Wilsonc5836c22012-10-17 12:09:55 +01004427 I915_WRITE_NOTRACE(FORCEWAKE_VLV, _MASKED_BIT_DISABLE(FORCEWAKE_KERNEL));
Jani Nikulab5144072013-01-17 10:24:09 +02004428 /* something from same cacheline, but !FORCEWAKE_VLV */
4429 POSTING_READ(FORCEWAKE_ACK_VLV);
Daniel Vetter5ab140a2012-08-24 17:26:20 +02004430 gen6_gt_check_fifodbg(dev_priv);
Eugeni Dodonov65901902012-07-02 11:51:11 -03004431}
4432
Chris Wilson16995a92012-10-18 11:46:10 +01004433void intel_gt_reset(struct drm_device *dev)
4434{
4435 struct drm_i915_private *dev_priv = dev->dev_private;
4436
4437 if (IS_VALLEYVIEW(dev)) {
4438 vlv_force_wake_reset(dev_priv);
4439 } else if (INTEL_INFO(dev)->gen >= 6) {
4440 __gen6_gt_force_wake_reset(dev_priv);
4441 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
4442 __gen6_gt_force_wake_mt_reset(dev_priv);
4443 }
4444}
4445
Eugeni Dodonov65901902012-07-02 11:51:11 -03004446void intel_gt_init(struct drm_device *dev)
4447{
4448 struct drm_i915_private *dev_priv = dev->dev_private;
4449
4450 spin_lock_init(&dev_priv->gt_lock);
4451
Chris Wilson16995a92012-10-18 11:46:10 +01004452 intel_gt_reset(dev);
4453
Eugeni Dodonov65901902012-07-02 11:51:11 -03004454 if (IS_VALLEYVIEW(dev)) {
4455 dev_priv->gt.force_wake_get = vlv_force_wake_get;
4456 dev_priv->gt.force_wake_put = vlv_force_wake_put;
Daniel Vetter36ec8f82012-10-18 14:44:35 +02004457 } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
4458 dev_priv->gt.force_wake_get = __gen6_gt_force_wake_mt_get;
4459 dev_priv->gt.force_wake_put = __gen6_gt_force_wake_mt_put;
4460 } else if (IS_GEN6(dev)) {
Eugeni Dodonov65901902012-07-02 11:51:11 -03004461 dev_priv->gt.force_wake_get = __gen6_gt_force_wake_get;
4462 dev_priv->gt.force_wake_put = __gen6_gt_force_wake_put;
Eugeni Dodonov65901902012-07-02 11:51:11 -03004463 }
Jesse Barnes1a01ab32012-11-02 11:14:00 -07004464 INIT_DELAYED_WORK(&dev_priv->rps.delayed_resume_work,
4465 intel_gen6_powersave_work);
Eugeni Dodonov65901902012-07-02 11:51:11 -03004466}
4467
Ben Widawsky42c05262012-09-26 10:34:00 -07004468int sandybridge_pcode_read(struct drm_i915_private *dev_priv, u8 mbox, u32 *val)
4469{
Jesse Barnes4fc688c2012-11-02 11:14:01 -07004470 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Ben Widawsky42c05262012-09-26 10:34:00 -07004471
4472 if (I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) {
4473 DRM_DEBUG_DRIVER("warning: pcode (read) mailbox access failed\n");
4474 return -EAGAIN;
4475 }
4476
4477 I915_WRITE(GEN6_PCODE_DATA, *val);
4478 I915_WRITE(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY | mbox);
4479
4480 if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
4481 500)) {
4482 DRM_ERROR("timeout waiting for pcode read (%d) to finish\n", mbox);
4483 return -ETIMEDOUT;
4484 }
4485
4486 *val = I915_READ(GEN6_PCODE_DATA);
4487 I915_WRITE(GEN6_PCODE_DATA, 0);
4488
4489 return 0;
4490}
4491
4492int sandybridge_pcode_write(struct drm_i915_private *dev_priv, u8 mbox, u32 val)
4493{
Jesse Barnes4fc688c2012-11-02 11:14:01 -07004494 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Ben Widawsky42c05262012-09-26 10:34:00 -07004495
4496 if (I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) {
4497 DRM_DEBUG_DRIVER("warning: pcode (write) mailbox access failed\n");
4498 return -EAGAIN;
4499 }
4500
4501 I915_WRITE(GEN6_PCODE_DATA, val);
4502 I915_WRITE(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY | mbox);
4503
4504 if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
4505 500)) {
4506 DRM_ERROR("timeout waiting for pcode write (%d) to finish\n", mbox);
4507 return -ETIMEDOUT;
4508 }
4509
4510 I915_WRITE(GEN6_PCODE_DATA, 0);
4511
4512 return 0;
4513}