blob: e0f016c24dcecf9470958928f42e9815eccfb3ff [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
Eugeni Dodonovf6750b32012-04-18 11:51:14 -030034/* FBC, or Frame Buffer Compression, is a technique employed to compress the
35 * framebuffer contents in-memory, aiming at reducing the required bandwidth
36 * during in-memory transfers and, therefore, reduce the power packet.
Eugeni Dodonov85208be2012-04-16 22:20:34 -030037 *
Eugeni Dodonovf6750b32012-04-18 11:51:14 -030038 * The benefits of FBC are mostly visible with solid backgrounds and
39 * variation-less patterns.
Eugeni Dodonov85208be2012-04-16 22:20:34 -030040 *
Eugeni Dodonovf6750b32012-04-18 11:51:14 -030041 * FBC-related functionality can be enabled by the means of the
42 * i915.i915_enable_fbc parameter
Eugeni Dodonov85208be2012-04-16 22:20:34 -030043 */
44
Eugeni Dodonov1fa61102012-04-18 15:29:26 -030045static void i8xx_disable_fbc(struct drm_device *dev)
Eugeni Dodonov85208be2012-04-16 22:20:34 -030046{
47 struct drm_i915_private *dev_priv = dev->dev_private;
48 u32 fbc_ctl;
49
50 /* Disable compression */
51 fbc_ctl = I915_READ(FBC_CONTROL);
52 if ((fbc_ctl & FBC_CTL_EN) == 0)
53 return;
54
55 fbc_ctl &= ~FBC_CTL_EN;
56 I915_WRITE(FBC_CONTROL, fbc_ctl);
57
58 /* Wait for compressing bit to clear */
59 if (wait_for((I915_READ(FBC_STATUS) & FBC_STAT_COMPRESSING) == 0, 10)) {
60 DRM_DEBUG_KMS("FBC idle timed out\n");
61 return;
62 }
63
64 DRM_DEBUG_KMS("disabled FBC\n");
65}
66
Eugeni Dodonov1fa61102012-04-18 15:29:26 -030067static void i8xx_enable_fbc(struct drm_crtc *crtc, unsigned long interval)
Eugeni Dodonov85208be2012-04-16 22:20:34 -030068{
69 struct drm_device *dev = crtc->dev;
70 struct drm_i915_private *dev_priv = dev->dev_private;
71 struct drm_framebuffer *fb = crtc->fb;
72 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
73 struct drm_i915_gem_object *obj = intel_fb->obj;
74 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
75 int cfb_pitch;
76 int plane, i;
77 u32 fbc_ctl, fbc_ctl2;
78
79 cfb_pitch = dev_priv->cfb_size / FBC_LL_SIZE;
80 if (fb->pitches[0] < cfb_pitch)
81 cfb_pitch = fb->pitches[0];
82
83 /* FBC_CTL wants 64B units */
84 cfb_pitch = (cfb_pitch / 64) - 1;
85 plane = intel_crtc->plane == 0 ? FBC_CTL_PLANEA : FBC_CTL_PLANEB;
86
87 /* Clear old tags */
88 for (i = 0; i < (FBC_LL_SIZE / 32) + 1; i++)
89 I915_WRITE(FBC_TAG + (i * 4), 0);
90
91 /* Set it up... */
92 fbc_ctl2 = FBC_CTL_FENCE_DBL | FBC_CTL_IDLE_IMM | FBC_CTL_CPU_FENCE;
93 fbc_ctl2 |= plane;
94 I915_WRITE(FBC_CONTROL2, fbc_ctl2);
95 I915_WRITE(FBC_FENCE_OFF, crtc->y);
96
97 /* enable it... */
98 fbc_ctl = FBC_CTL_EN | FBC_CTL_PERIODIC;
99 if (IS_I945GM(dev))
100 fbc_ctl |= FBC_CTL_C3_IDLE; /* 945 needs special SR handling */
101 fbc_ctl |= (cfb_pitch & 0xff) << FBC_CTL_STRIDE_SHIFT;
102 fbc_ctl |= (interval & 0x2fff) << FBC_CTL_INTERVAL_SHIFT;
103 fbc_ctl |= obj->fence_reg;
104 I915_WRITE(FBC_CONTROL, fbc_ctl);
105
106 DRM_DEBUG_KMS("enabled FBC, pitch %d, yoff %d, plane %d, ",
107 cfb_pitch, crtc->y, intel_crtc->plane);
108}
109
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300110static bool i8xx_fbc_enabled(struct drm_device *dev)
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300111{
112 struct drm_i915_private *dev_priv = dev->dev_private;
113
114 return I915_READ(FBC_CONTROL) & FBC_CTL_EN;
115}
116
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300117static void g4x_enable_fbc(struct drm_crtc *crtc, unsigned long interval)
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300118{
119 struct drm_device *dev = crtc->dev;
120 struct drm_i915_private *dev_priv = dev->dev_private;
121 struct drm_framebuffer *fb = crtc->fb;
122 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
123 struct drm_i915_gem_object *obj = intel_fb->obj;
124 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
125 int plane = intel_crtc->plane == 0 ? DPFC_CTL_PLANEA : DPFC_CTL_PLANEB;
126 unsigned long stall_watermark = 200;
127 u32 dpfc_ctl;
128
129 dpfc_ctl = plane | DPFC_SR_EN | DPFC_CTL_LIMIT_1X;
130 dpfc_ctl |= DPFC_CTL_FENCE_EN | obj->fence_reg;
131 I915_WRITE(DPFC_CHICKEN, DPFC_HT_MODIFY);
132
133 I915_WRITE(DPFC_RECOMP_CTL, DPFC_RECOMP_STALL_EN |
134 (stall_watermark << DPFC_RECOMP_STALL_WM_SHIFT) |
135 (interval << DPFC_RECOMP_TIMER_COUNT_SHIFT));
136 I915_WRITE(DPFC_FENCE_YOFF, crtc->y);
137
138 /* enable it... */
139 I915_WRITE(DPFC_CONTROL, I915_READ(DPFC_CONTROL) | DPFC_CTL_EN);
140
141 DRM_DEBUG_KMS("enabled fbc on plane %d\n", intel_crtc->plane);
142}
143
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300144static void g4x_disable_fbc(struct drm_device *dev)
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300145{
146 struct drm_i915_private *dev_priv = dev->dev_private;
147 u32 dpfc_ctl;
148
149 /* Disable compression */
150 dpfc_ctl = I915_READ(DPFC_CONTROL);
151 if (dpfc_ctl & DPFC_CTL_EN) {
152 dpfc_ctl &= ~DPFC_CTL_EN;
153 I915_WRITE(DPFC_CONTROL, dpfc_ctl);
154
155 DRM_DEBUG_KMS("disabled FBC\n");
156 }
157}
158
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300159static bool g4x_fbc_enabled(struct drm_device *dev)
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300160{
161 struct drm_i915_private *dev_priv = dev->dev_private;
162
163 return I915_READ(DPFC_CONTROL) & DPFC_CTL_EN;
164}
165
166static void sandybridge_blit_fbc_update(struct drm_device *dev)
167{
168 struct drm_i915_private *dev_priv = dev->dev_private;
169 u32 blt_ecoskpd;
170
171 /* Make sure blitter notifies FBC of writes */
172 gen6_gt_force_wake_get(dev_priv);
173 blt_ecoskpd = I915_READ(GEN6_BLITTER_ECOSKPD);
174 blt_ecoskpd |= GEN6_BLITTER_FBC_NOTIFY <<
175 GEN6_BLITTER_LOCK_SHIFT;
176 I915_WRITE(GEN6_BLITTER_ECOSKPD, blt_ecoskpd);
177 blt_ecoskpd |= GEN6_BLITTER_FBC_NOTIFY;
178 I915_WRITE(GEN6_BLITTER_ECOSKPD, blt_ecoskpd);
179 blt_ecoskpd &= ~(GEN6_BLITTER_FBC_NOTIFY <<
180 GEN6_BLITTER_LOCK_SHIFT);
181 I915_WRITE(GEN6_BLITTER_ECOSKPD, blt_ecoskpd);
182 POSTING_READ(GEN6_BLITTER_ECOSKPD);
183 gen6_gt_force_wake_put(dev_priv);
184}
185
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300186static void ironlake_enable_fbc(struct drm_crtc *crtc, unsigned long interval)
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300187{
188 struct drm_device *dev = crtc->dev;
189 struct drm_i915_private *dev_priv = dev->dev_private;
190 struct drm_framebuffer *fb = crtc->fb;
191 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
192 struct drm_i915_gem_object *obj = intel_fb->obj;
193 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
194 int plane = intel_crtc->plane == 0 ? DPFC_CTL_PLANEA : DPFC_CTL_PLANEB;
195 unsigned long stall_watermark = 200;
196 u32 dpfc_ctl;
197
198 dpfc_ctl = I915_READ(ILK_DPFC_CONTROL);
199 dpfc_ctl &= DPFC_RESERVED;
200 dpfc_ctl |= (plane | DPFC_CTL_LIMIT_1X);
201 /* Set persistent mode for front-buffer rendering, ala X. */
202 dpfc_ctl |= DPFC_CTL_PERSISTENT_MODE;
203 dpfc_ctl |= (DPFC_CTL_FENCE_EN | obj->fence_reg);
204 I915_WRITE(ILK_DPFC_CHICKEN, DPFC_HT_MODIFY);
205
206 I915_WRITE(ILK_DPFC_RECOMP_CTL, DPFC_RECOMP_STALL_EN |
207 (stall_watermark << DPFC_RECOMP_STALL_WM_SHIFT) |
208 (interval << DPFC_RECOMP_TIMER_COUNT_SHIFT));
209 I915_WRITE(ILK_DPFC_FENCE_YOFF, crtc->y);
210 I915_WRITE(ILK_FBC_RT_BASE, obj->gtt_offset | ILK_FBC_RT_VALID);
211 /* enable it... */
212 I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl | DPFC_CTL_EN);
213
214 if (IS_GEN6(dev)) {
215 I915_WRITE(SNB_DPFC_CTL_SA,
216 SNB_CPU_FENCE_ENABLE | obj->fence_reg);
217 I915_WRITE(DPFC_CPU_FENCE_OFFSET, crtc->y);
218 sandybridge_blit_fbc_update(dev);
219 }
220
221 DRM_DEBUG_KMS("enabled fbc on plane %d\n", intel_crtc->plane);
222}
223
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300224static void ironlake_disable_fbc(struct drm_device *dev)
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300225{
226 struct drm_i915_private *dev_priv = dev->dev_private;
227 u32 dpfc_ctl;
228
229 /* Disable compression */
230 dpfc_ctl = I915_READ(ILK_DPFC_CONTROL);
231 if (dpfc_ctl & DPFC_CTL_EN) {
232 dpfc_ctl &= ~DPFC_CTL_EN;
233 I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl);
234
235 DRM_DEBUG_KMS("disabled FBC\n");
236 }
237}
238
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300239static bool ironlake_fbc_enabled(struct drm_device *dev)
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300240{
241 struct drm_i915_private *dev_priv = dev->dev_private;
242
243 return I915_READ(ILK_DPFC_CONTROL) & DPFC_CTL_EN;
244}
245
246bool intel_fbc_enabled(struct drm_device *dev)
247{
248 struct drm_i915_private *dev_priv = dev->dev_private;
249
250 if (!dev_priv->display.fbc_enabled)
251 return false;
252
253 return dev_priv->display.fbc_enabled(dev);
254}
255
256static void intel_fbc_work_fn(struct work_struct *__work)
257{
258 struct intel_fbc_work *work =
259 container_of(to_delayed_work(__work),
260 struct intel_fbc_work, work);
261 struct drm_device *dev = work->crtc->dev;
262 struct drm_i915_private *dev_priv = dev->dev_private;
263
264 mutex_lock(&dev->struct_mutex);
265 if (work == dev_priv->fbc_work) {
266 /* Double check that we haven't switched fb without cancelling
267 * the prior work.
268 */
269 if (work->crtc->fb == work->fb) {
270 dev_priv->display.enable_fbc(work->crtc,
271 work->interval);
272
273 dev_priv->cfb_plane = to_intel_crtc(work->crtc)->plane;
274 dev_priv->cfb_fb = work->crtc->fb->base.id;
275 dev_priv->cfb_y = work->crtc->y;
276 }
277
278 dev_priv->fbc_work = NULL;
279 }
280 mutex_unlock(&dev->struct_mutex);
281
282 kfree(work);
283}
284
285static void intel_cancel_fbc_work(struct drm_i915_private *dev_priv)
286{
287 if (dev_priv->fbc_work == NULL)
288 return;
289
290 DRM_DEBUG_KMS("cancelling pending FBC enable\n");
291
292 /* Synchronisation is provided by struct_mutex and checking of
293 * dev_priv->fbc_work, so we can perform the cancellation
294 * entirely asynchronously.
295 */
296 if (cancel_delayed_work(&dev_priv->fbc_work->work))
297 /* tasklet was killed before being run, clean up */
298 kfree(dev_priv->fbc_work);
299
300 /* Mark the work as no longer wanted so that if it does
301 * wake-up (because the work was already running and waiting
302 * for our mutex), it will discover that is no longer
303 * necessary to run.
304 */
305 dev_priv->fbc_work = NULL;
306}
307
308void intel_enable_fbc(struct drm_crtc *crtc, unsigned long interval)
309{
310 struct intel_fbc_work *work;
311 struct drm_device *dev = crtc->dev;
312 struct drm_i915_private *dev_priv = dev->dev_private;
313
314 if (!dev_priv->display.enable_fbc)
315 return;
316
317 intel_cancel_fbc_work(dev_priv);
318
319 work = kzalloc(sizeof *work, GFP_KERNEL);
320 if (work == NULL) {
321 dev_priv->display.enable_fbc(crtc, interval);
322 return;
323 }
324
325 work->crtc = crtc;
326 work->fb = crtc->fb;
327 work->interval = interval;
328 INIT_DELAYED_WORK(&work->work, intel_fbc_work_fn);
329
330 dev_priv->fbc_work = work;
331
332 DRM_DEBUG_KMS("scheduling delayed FBC enable\n");
333
334 /* Delay the actual enabling to let pageflipping cease and the
335 * display to settle before starting the compression. Note that
336 * this delay also serves a second purpose: it allows for a
337 * vblank to pass after disabling the FBC before we attempt
338 * to modify the control registers.
339 *
340 * A more complicated solution would involve tracking vblanks
341 * following the termination of the page-flipping sequence
342 * and indeed performing the enable as a co-routine and not
343 * waiting synchronously upon the vblank.
344 */
345 schedule_delayed_work(&work->work, msecs_to_jiffies(50));
346}
347
348void intel_disable_fbc(struct drm_device *dev)
349{
350 struct drm_i915_private *dev_priv = dev->dev_private;
351
352 intel_cancel_fbc_work(dev_priv);
353
354 if (!dev_priv->display.disable_fbc)
355 return;
356
357 dev_priv->display.disable_fbc(dev);
358 dev_priv->cfb_plane = -1;
359}
360
361/**
362 * intel_update_fbc - enable/disable FBC as needed
363 * @dev: the drm_device
364 *
365 * Set up the framebuffer compression hardware at mode set time. We
366 * enable it if possible:
367 * - plane A only (on pre-965)
368 * - no pixel mulitply/line duplication
369 * - no alpha buffer discard
370 * - no dual wide
371 * - framebuffer <= 2048 in width, 1536 in height
372 *
373 * We can't assume that any compression will take place (worst case),
374 * so the compressed buffer has to be the same size as the uncompressed
375 * one. It also must reside (along with the line length buffer) in
376 * stolen memory.
377 *
378 * We need to enable/disable FBC on a global basis.
379 */
380void intel_update_fbc(struct drm_device *dev)
381{
382 struct drm_i915_private *dev_priv = dev->dev_private;
383 struct drm_crtc *crtc = NULL, *tmp_crtc;
384 struct intel_crtc *intel_crtc;
385 struct drm_framebuffer *fb;
386 struct intel_framebuffer *intel_fb;
387 struct drm_i915_gem_object *obj;
388 int enable_fbc;
389
390 DRM_DEBUG_KMS("\n");
391
392 if (!i915_powersave)
393 return;
394
395 if (!I915_HAS_FBC(dev))
396 return;
397
398 /*
399 * If FBC is already on, we just have to verify that we can
400 * keep it that way...
401 * Need to disable if:
402 * - more than one pipe is active
403 * - changing FBC params (stride, fence, mode)
404 * - new fb is too large to fit in compressed buffer
405 * - going to an unsupported config (interlace, pixel multiply, etc.)
406 */
407 list_for_each_entry(tmp_crtc, &dev->mode_config.crtc_list, head) {
408 if (tmp_crtc->enabled && tmp_crtc->fb) {
409 if (crtc) {
410 DRM_DEBUG_KMS("more than one pipe active, disabling compression\n");
411 dev_priv->no_fbc_reason = FBC_MULTIPLE_PIPES;
412 goto out_disable;
413 }
414 crtc = tmp_crtc;
415 }
416 }
417
418 if (!crtc || crtc->fb == NULL) {
419 DRM_DEBUG_KMS("no output, disabling\n");
420 dev_priv->no_fbc_reason = FBC_NO_OUTPUT;
421 goto out_disable;
422 }
423
424 intel_crtc = to_intel_crtc(crtc);
425 fb = crtc->fb;
426 intel_fb = to_intel_framebuffer(fb);
427 obj = intel_fb->obj;
428
429 enable_fbc = i915_enable_fbc;
430 if (enable_fbc < 0) {
431 DRM_DEBUG_KMS("fbc set to per-chip default\n");
432 enable_fbc = 1;
433 if (INTEL_INFO(dev)->gen <= 6)
434 enable_fbc = 0;
435 }
436 if (!enable_fbc) {
437 DRM_DEBUG_KMS("fbc disabled per module param\n");
438 dev_priv->no_fbc_reason = FBC_MODULE_PARAM;
439 goto out_disable;
440 }
441 if (intel_fb->obj->base.size > dev_priv->cfb_size) {
442 DRM_DEBUG_KMS("framebuffer too large, disabling "
443 "compression\n");
444 dev_priv->no_fbc_reason = FBC_STOLEN_TOO_SMALL;
445 goto out_disable;
446 }
447 if ((crtc->mode.flags & DRM_MODE_FLAG_INTERLACE) ||
448 (crtc->mode.flags & DRM_MODE_FLAG_DBLSCAN)) {
449 DRM_DEBUG_KMS("mode incompatible with compression, "
450 "disabling\n");
451 dev_priv->no_fbc_reason = FBC_UNSUPPORTED_MODE;
452 goto out_disable;
453 }
454 if ((crtc->mode.hdisplay > 2048) ||
455 (crtc->mode.vdisplay > 1536)) {
456 DRM_DEBUG_KMS("mode too large for compression, disabling\n");
457 dev_priv->no_fbc_reason = FBC_MODE_TOO_LARGE;
458 goto out_disable;
459 }
460 if ((IS_I915GM(dev) || IS_I945GM(dev)) && intel_crtc->plane != 0) {
461 DRM_DEBUG_KMS("plane not 0, disabling compression\n");
462 dev_priv->no_fbc_reason = FBC_BAD_PLANE;
463 goto out_disable;
464 }
465
466 /* The use of a CPU fence is mandatory in order to detect writes
467 * by the CPU to the scanout and trigger updates to the FBC.
468 */
469 if (obj->tiling_mode != I915_TILING_X ||
470 obj->fence_reg == I915_FENCE_REG_NONE) {
471 DRM_DEBUG_KMS("framebuffer not tiled or fenced, disabling compression\n");
472 dev_priv->no_fbc_reason = FBC_NOT_TILED;
473 goto out_disable;
474 }
475
476 /* If the kernel debugger is active, always disable compression */
477 if (in_dbg_master())
478 goto out_disable;
479
480 /* If the scanout has not changed, don't modify the FBC settings.
481 * Note that we make the fundamental assumption that the fb->obj
482 * cannot be unpinned (and have its GTT offset and fence revoked)
483 * without first being decoupled from the scanout and FBC disabled.
484 */
485 if (dev_priv->cfb_plane == intel_crtc->plane &&
486 dev_priv->cfb_fb == fb->base.id &&
487 dev_priv->cfb_y == crtc->y)
488 return;
489
490 if (intel_fbc_enabled(dev)) {
491 /* We update FBC along two paths, after changing fb/crtc
492 * configuration (modeswitching) and after page-flipping
493 * finishes. For the latter, we know that not only did
494 * we disable the FBC at the start of the page-flip
495 * sequence, but also more than one vblank has passed.
496 *
497 * For the former case of modeswitching, it is possible
498 * to switch between two FBC valid configurations
499 * instantaneously so we do need to disable the FBC
500 * before we can modify its control registers. We also
501 * have to wait for the next vblank for that to take
502 * effect. However, since we delay enabling FBC we can
503 * assume that a vblank has passed since disabling and
504 * that we can safely alter the registers in the deferred
505 * callback.
506 *
507 * In the scenario that we go from a valid to invalid
508 * and then back to valid FBC configuration we have
509 * no strict enforcement that a vblank occurred since
510 * disabling the FBC. However, along all current pipe
511 * disabling paths we do need to wait for a vblank at
512 * some point. And we wait before enabling FBC anyway.
513 */
514 DRM_DEBUG_KMS("disabling active FBC for update\n");
515 intel_disable_fbc(dev);
516 }
517
518 intel_enable_fbc(crtc, 500);
519 return;
520
521out_disable:
522 /* Multiple disables should be harmless */
523 if (intel_fbc_enabled(dev)) {
524 DRM_DEBUG_KMS("unsupported config, disabling FBC\n");
525 intel_disable_fbc(dev);
526 }
527}
528
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300529static const struct cxsr_latency cxsr_latency_table[] = {
530 {1, 0, 800, 400, 3382, 33382, 3983, 33983}, /* DDR2-400 SC */
531 {1, 0, 800, 667, 3354, 33354, 3807, 33807}, /* DDR2-667 SC */
532 {1, 0, 800, 800, 3347, 33347, 3763, 33763}, /* DDR2-800 SC */
533 {1, 1, 800, 667, 6420, 36420, 6873, 36873}, /* DDR3-667 SC */
534 {1, 1, 800, 800, 5902, 35902, 6318, 36318}, /* DDR3-800 SC */
535
536 {1, 0, 667, 400, 3400, 33400, 4021, 34021}, /* DDR2-400 SC */
537 {1, 0, 667, 667, 3372, 33372, 3845, 33845}, /* DDR2-667 SC */
538 {1, 0, 667, 800, 3386, 33386, 3822, 33822}, /* DDR2-800 SC */
539 {1, 1, 667, 667, 6438, 36438, 6911, 36911}, /* DDR3-667 SC */
540 {1, 1, 667, 800, 5941, 35941, 6377, 36377}, /* DDR3-800 SC */
541
542 {1, 0, 400, 400, 3472, 33472, 4173, 34173}, /* DDR2-400 SC */
543 {1, 0, 400, 667, 3443, 33443, 3996, 33996}, /* DDR2-667 SC */
544 {1, 0, 400, 800, 3430, 33430, 3946, 33946}, /* DDR2-800 SC */
545 {1, 1, 400, 667, 6509, 36509, 7062, 37062}, /* DDR3-667 SC */
546 {1, 1, 400, 800, 5985, 35985, 6501, 36501}, /* DDR3-800 SC */
547
548 {0, 0, 800, 400, 3438, 33438, 4065, 34065}, /* DDR2-400 SC */
549 {0, 0, 800, 667, 3410, 33410, 3889, 33889}, /* DDR2-667 SC */
550 {0, 0, 800, 800, 3403, 33403, 3845, 33845}, /* DDR2-800 SC */
551 {0, 1, 800, 667, 6476, 36476, 6955, 36955}, /* DDR3-667 SC */
552 {0, 1, 800, 800, 5958, 35958, 6400, 36400}, /* DDR3-800 SC */
553
554 {0, 0, 667, 400, 3456, 33456, 4103, 34106}, /* DDR2-400 SC */
555 {0, 0, 667, 667, 3428, 33428, 3927, 33927}, /* DDR2-667 SC */
556 {0, 0, 667, 800, 3443, 33443, 3905, 33905}, /* DDR2-800 SC */
557 {0, 1, 667, 667, 6494, 36494, 6993, 36993}, /* DDR3-667 SC */
558 {0, 1, 667, 800, 5998, 35998, 6460, 36460}, /* DDR3-800 SC */
559
560 {0, 0, 400, 400, 3528, 33528, 4255, 34255}, /* DDR2-400 SC */
561 {0, 0, 400, 667, 3500, 33500, 4079, 34079}, /* DDR2-667 SC */
562 {0, 0, 400, 800, 3487, 33487, 4029, 34029}, /* DDR2-800 SC */
563 {0, 1, 400, 667, 6566, 36566, 7145, 37145}, /* DDR3-667 SC */
564 {0, 1, 400, 800, 6042, 36042, 6584, 36584}, /* DDR3-800 SC */
565};
566
Daniel Vetter63c62272012-04-21 23:17:55 +0200567static const struct cxsr_latency *intel_get_cxsr_latency(int is_desktop,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300568 int is_ddr3,
569 int fsb,
570 int mem)
571{
572 const struct cxsr_latency *latency;
573 int i;
574
575 if (fsb == 0 || mem == 0)
576 return NULL;
577
578 for (i = 0; i < ARRAY_SIZE(cxsr_latency_table); i++) {
579 latency = &cxsr_latency_table[i];
580 if (is_desktop == latency->is_desktop &&
581 is_ddr3 == latency->is_ddr3 &&
582 fsb == latency->fsb_freq && mem == latency->mem_freq)
583 return latency;
584 }
585
586 DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
587
588 return NULL;
589}
590
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300591static void pineview_disable_cxsr(struct drm_device *dev)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300592{
593 struct drm_i915_private *dev_priv = dev->dev_private;
594
595 /* deactivate cxsr */
596 I915_WRITE(DSPFW3, I915_READ(DSPFW3) & ~PINEVIEW_SELF_REFRESH_EN);
597}
598
599/*
600 * Latency for FIFO fetches is dependent on several factors:
601 * - memory configuration (speed, channels)
602 * - chipset
603 * - current MCH state
604 * It can be fairly high in some situations, so here we assume a fairly
605 * pessimal value. It's a tradeoff between extra memory fetches (if we
606 * set this value too high, the FIFO will fetch frequently to stay full)
607 * and power consumption (set it too low to save power and we might see
608 * FIFO underruns and display "flicker").
609 *
610 * A value of 5us seems to be a good balance; safe for very low end
611 * platforms but not overly aggressive on lower latency configs.
612 */
613static const int latency_ns = 5000;
614
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300615static int i9xx_get_fifo_size(struct drm_device *dev, int plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300616{
617 struct drm_i915_private *dev_priv = dev->dev_private;
618 uint32_t dsparb = I915_READ(DSPARB);
619 int size;
620
621 size = dsparb & 0x7f;
622 if (plane)
623 size = ((dsparb >> DSPARB_CSTART_SHIFT) & 0x7f) - size;
624
625 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
626 plane ? "B" : "A", size);
627
628 return size;
629}
630
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300631static int i85x_get_fifo_size(struct drm_device *dev, int plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300632{
633 struct drm_i915_private *dev_priv = dev->dev_private;
634 uint32_t dsparb = I915_READ(DSPARB);
635 int size;
636
637 size = dsparb & 0x1ff;
638 if (plane)
639 size = ((dsparb >> DSPARB_BEND_SHIFT) & 0x1ff) - size;
640 size >>= 1; /* Convert to cachelines */
641
642 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
643 plane ? "B" : "A", size);
644
645 return size;
646}
647
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300648static int i845_get_fifo_size(struct drm_device *dev, int plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300649{
650 struct drm_i915_private *dev_priv = dev->dev_private;
651 uint32_t dsparb = I915_READ(DSPARB);
652 int size;
653
654 size = dsparb & 0x7f;
655 size >>= 2; /* Convert to cachelines */
656
657 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
658 plane ? "B" : "A",
659 size);
660
661 return size;
662}
663
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300664static int i830_get_fifo_size(struct drm_device *dev, int plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300665{
666 struct drm_i915_private *dev_priv = dev->dev_private;
667 uint32_t dsparb = I915_READ(DSPARB);
668 int size;
669
670 size = dsparb & 0x7f;
671 size >>= 1; /* Convert to cachelines */
672
673 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
674 plane ? "B" : "A", size);
675
676 return size;
677}
678
679/* Pineview has different values for various configs */
680static const struct intel_watermark_params pineview_display_wm = {
681 PINEVIEW_DISPLAY_FIFO,
682 PINEVIEW_MAX_WM,
683 PINEVIEW_DFT_WM,
684 PINEVIEW_GUARD_WM,
685 PINEVIEW_FIFO_LINE_SIZE
686};
687static const struct intel_watermark_params pineview_display_hplloff_wm = {
688 PINEVIEW_DISPLAY_FIFO,
689 PINEVIEW_MAX_WM,
690 PINEVIEW_DFT_HPLLOFF_WM,
691 PINEVIEW_GUARD_WM,
692 PINEVIEW_FIFO_LINE_SIZE
693};
694static const struct intel_watermark_params pineview_cursor_wm = {
695 PINEVIEW_CURSOR_FIFO,
696 PINEVIEW_CURSOR_MAX_WM,
697 PINEVIEW_CURSOR_DFT_WM,
698 PINEVIEW_CURSOR_GUARD_WM,
699 PINEVIEW_FIFO_LINE_SIZE,
700};
701static const struct intel_watermark_params pineview_cursor_hplloff_wm = {
702 PINEVIEW_CURSOR_FIFO,
703 PINEVIEW_CURSOR_MAX_WM,
704 PINEVIEW_CURSOR_DFT_WM,
705 PINEVIEW_CURSOR_GUARD_WM,
706 PINEVIEW_FIFO_LINE_SIZE
707};
708static const struct intel_watermark_params g4x_wm_info = {
709 G4X_FIFO_SIZE,
710 G4X_MAX_WM,
711 G4X_MAX_WM,
712 2,
713 G4X_FIFO_LINE_SIZE,
714};
715static const struct intel_watermark_params g4x_cursor_wm_info = {
716 I965_CURSOR_FIFO,
717 I965_CURSOR_MAX_WM,
718 I965_CURSOR_DFT_WM,
719 2,
720 G4X_FIFO_LINE_SIZE,
721};
722static const struct intel_watermark_params valleyview_wm_info = {
723 VALLEYVIEW_FIFO_SIZE,
724 VALLEYVIEW_MAX_WM,
725 VALLEYVIEW_MAX_WM,
726 2,
727 G4X_FIFO_LINE_SIZE,
728};
729static const struct intel_watermark_params valleyview_cursor_wm_info = {
730 I965_CURSOR_FIFO,
731 VALLEYVIEW_CURSOR_MAX_WM,
732 I965_CURSOR_DFT_WM,
733 2,
734 G4X_FIFO_LINE_SIZE,
735};
736static const struct intel_watermark_params i965_cursor_wm_info = {
737 I965_CURSOR_FIFO,
738 I965_CURSOR_MAX_WM,
739 I965_CURSOR_DFT_WM,
740 2,
741 I915_FIFO_LINE_SIZE,
742};
743static const struct intel_watermark_params i945_wm_info = {
744 I945_FIFO_SIZE,
745 I915_MAX_WM,
746 1,
747 2,
748 I915_FIFO_LINE_SIZE
749};
750static const struct intel_watermark_params i915_wm_info = {
751 I915_FIFO_SIZE,
752 I915_MAX_WM,
753 1,
754 2,
755 I915_FIFO_LINE_SIZE
756};
757static const struct intel_watermark_params i855_wm_info = {
758 I855GM_FIFO_SIZE,
759 I915_MAX_WM,
760 1,
761 2,
762 I830_FIFO_LINE_SIZE
763};
764static const struct intel_watermark_params i830_wm_info = {
765 I830_FIFO_SIZE,
766 I915_MAX_WM,
767 1,
768 2,
769 I830_FIFO_LINE_SIZE
770};
771
772static const struct intel_watermark_params ironlake_display_wm_info = {
773 ILK_DISPLAY_FIFO,
774 ILK_DISPLAY_MAXWM,
775 ILK_DISPLAY_DFTWM,
776 2,
777 ILK_FIFO_LINE_SIZE
778};
779static const struct intel_watermark_params ironlake_cursor_wm_info = {
780 ILK_CURSOR_FIFO,
781 ILK_CURSOR_MAXWM,
782 ILK_CURSOR_DFTWM,
783 2,
784 ILK_FIFO_LINE_SIZE
785};
786static const struct intel_watermark_params ironlake_display_srwm_info = {
787 ILK_DISPLAY_SR_FIFO,
788 ILK_DISPLAY_MAX_SRWM,
789 ILK_DISPLAY_DFT_SRWM,
790 2,
791 ILK_FIFO_LINE_SIZE
792};
793static const struct intel_watermark_params ironlake_cursor_srwm_info = {
794 ILK_CURSOR_SR_FIFO,
795 ILK_CURSOR_MAX_SRWM,
796 ILK_CURSOR_DFT_SRWM,
797 2,
798 ILK_FIFO_LINE_SIZE
799};
800
801static const struct intel_watermark_params sandybridge_display_wm_info = {
802 SNB_DISPLAY_FIFO,
803 SNB_DISPLAY_MAXWM,
804 SNB_DISPLAY_DFTWM,
805 2,
806 SNB_FIFO_LINE_SIZE
807};
808static const struct intel_watermark_params sandybridge_cursor_wm_info = {
809 SNB_CURSOR_FIFO,
810 SNB_CURSOR_MAXWM,
811 SNB_CURSOR_DFTWM,
812 2,
813 SNB_FIFO_LINE_SIZE
814};
815static const struct intel_watermark_params sandybridge_display_srwm_info = {
816 SNB_DISPLAY_SR_FIFO,
817 SNB_DISPLAY_MAX_SRWM,
818 SNB_DISPLAY_DFT_SRWM,
819 2,
820 SNB_FIFO_LINE_SIZE
821};
822static const struct intel_watermark_params sandybridge_cursor_srwm_info = {
823 SNB_CURSOR_SR_FIFO,
824 SNB_CURSOR_MAX_SRWM,
825 SNB_CURSOR_DFT_SRWM,
826 2,
827 SNB_FIFO_LINE_SIZE
828};
829
830
831/**
832 * intel_calculate_wm - calculate watermark level
833 * @clock_in_khz: pixel clock
834 * @wm: chip FIFO params
835 * @pixel_size: display pixel size
836 * @latency_ns: memory latency for the platform
837 *
838 * Calculate the watermark level (the level at which the display plane will
839 * start fetching from memory again). Each chip has a different display
840 * FIFO size and allocation, so the caller needs to figure that out and pass
841 * in the correct intel_watermark_params structure.
842 *
843 * As the pixel clock runs, the FIFO will be drained at a rate that depends
844 * on the pixel size. When it reaches the watermark level, it'll start
845 * fetching FIFO line sized based chunks from memory until the FIFO fills
846 * past the watermark point. If the FIFO drains completely, a FIFO underrun
847 * will occur, and a display engine hang could result.
848 */
849static unsigned long intel_calculate_wm(unsigned long clock_in_khz,
850 const struct intel_watermark_params *wm,
851 int fifo_size,
852 int pixel_size,
853 unsigned long latency_ns)
854{
855 long entries_required, wm_size;
856
857 /*
858 * Note: we need to make sure we don't overflow for various clock &
859 * latency values.
860 * clocks go from a few thousand to several hundred thousand.
861 * latency is usually a few thousand
862 */
863 entries_required = ((clock_in_khz / 1000) * pixel_size * latency_ns) /
864 1000;
865 entries_required = DIV_ROUND_UP(entries_required, wm->cacheline_size);
866
867 DRM_DEBUG_KMS("FIFO entries required for mode: %ld\n", entries_required);
868
869 wm_size = fifo_size - (entries_required + wm->guard_size);
870
871 DRM_DEBUG_KMS("FIFO watermark level: %ld\n", wm_size);
872
873 /* Don't promote wm_size to unsigned... */
874 if (wm_size > (long)wm->max_wm)
875 wm_size = wm->max_wm;
876 if (wm_size <= 0)
877 wm_size = wm->default_wm;
878 return wm_size;
879}
880
881static struct drm_crtc *single_enabled_crtc(struct drm_device *dev)
882{
883 struct drm_crtc *crtc, *enabled = NULL;
884
885 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
886 if (crtc->enabled && crtc->fb) {
887 if (enabled)
888 return NULL;
889 enabled = crtc;
890 }
891 }
892
893 return enabled;
894}
895
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300896static void pineview_update_wm(struct drm_device *dev)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300897{
898 struct drm_i915_private *dev_priv = dev->dev_private;
899 struct drm_crtc *crtc;
900 const struct cxsr_latency *latency;
901 u32 reg;
902 unsigned long wm;
903
904 latency = intel_get_cxsr_latency(IS_PINEVIEW_G(dev), dev_priv->is_ddr3,
905 dev_priv->fsb_freq, dev_priv->mem_freq);
906 if (!latency) {
907 DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
908 pineview_disable_cxsr(dev);
909 return;
910 }
911
912 crtc = single_enabled_crtc(dev);
913 if (crtc) {
914 int clock = crtc->mode.clock;
915 int pixel_size = crtc->fb->bits_per_pixel / 8;
916
917 /* Display SR */
918 wm = intel_calculate_wm(clock, &pineview_display_wm,
919 pineview_display_wm.fifo_size,
920 pixel_size, latency->display_sr);
921 reg = I915_READ(DSPFW1);
922 reg &= ~DSPFW_SR_MASK;
923 reg |= wm << DSPFW_SR_SHIFT;
924 I915_WRITE(DSPFW1, reg);
925 DRM_DEBUG_KMS("DSPFW1 register is %x\n", reg);
926
927 /* cursor SR */
928 wm = intel_calculate_wm(clock, &pineview_cursor_wm,
929 pineview_display_wm.fifo_size,
930 pixel_size, latency->cursor_sr);
931 reg = I915_READ(DSPFW3);
932 reg &= ~DSPFW_CURSOR_SR_MASK;
933 reg |= (wm & 0x3f) << DSPFW_CURSOR_SR_SHIFT;
934 I915_WRITE(DSPFW3, reg);
935
936 /* Display HPLL off SR */
937 wm = intel_calculate_wm(clock, &pineview_display_hplloff_wm,
938 pineview_display_hplloff_wm.fifo_size,
939 pixel_size, latency->display_hpll_disable);
940 reg = I915_READ(DSPFW3);
941 reg &= ~DSPFW_HPLL_SR_MASK;
942 reg |= wm & DSPFW_HPLL_SR_MASK;
943 I915_WRITE(DSPFW3, reg);
944
945 /* cursor HPLL off SR */
946 wm = intel_calculate_wm(clock, &pineview_cursor_hplloff_wm,
947 pineview_display_hplloff_wm.fifo_size,
948 pixel_size, latency->cursor_hpll_disable);
949 reg = I915_READ(DSPFW3);
950 reg &= ~DSPFW_HPLL_CURSOR_MASK;
951 reg |= (wm & 0x3f) << DSPFW_HPLL_CURSOR_SHIFT;
952 I915_WRITE(DSPFW3, reg);
953 DRM_DEBUG_KMS("DSPFW3 register is %x\n", reg);
954
955 /* activate cxsr */
956 I915_WRITE(DSPFW3,
957 I915_READ(DSPFW3) | PINEVIEW_SELF_REFRESH_EN);
958 DRM_DEBUG_KMS("Self-refresh is enabled\n");
959 } else {
960 pineview_disable_cxsr(dev);
961 DRM_DEBUG_KMS("Self-refresh is disabled\n");
962 }
963}
964
965static bool g4x_compute_wm0(struct drm_device *dev,
966 int plane,
967 const struct intel_watermark_params *display,
968 int display_latency_ns,
969 const struct intel_watermark_params *cursor,
970 int cursor_latency_ns,
971 int *plane_wm,
972 int *cursor_wm)
973{
974 struct drm_crtc *crtc;
975 int htotal, hdisplay, clock, pixel_size;
976 int line_time_us, line_count;
977 int entries, tlb_miss;
978
979 crtc = intel_get_crtc_for_plane(dev, plane);
980 if (crtc->fb == NULL || !crtc->enabled) {
981 *cursor_wm = cursor->guard_size;
982 *plane_wm = display->guard_size;
983 return false;
984 }
985
986 htotal = crtc->mode.htotal;
987 hdisplay = crtc->mode.hdisplay;
988 clock = crtc->mode.clock;
989 pixel_size = crtc->fb->bits_per_pixel / 8;
990
991 /* Use the small buffer method to calculate plane watermark */
992 entries = ((clock * pixel_size / 1000) * display_latency_ns) / 1000;
993 tlb_miss = display->fifo_size*display->cacheline_size - hdisplay * 8;
994 if (tlb_miss > 0)
995 entries += tlb_miss;
996 entries = DIV_ROUND_UP(entries, display->cacheline_size);
997 *plane_wm = entries + display->guard_size;
998 if (*plane_wm > (int)display->max_wm)
999 *plane_wm = display->max_wm;
1000
1001 /* Use the large buffer method to calculate cursor watermark */
1002 line_time_us = ((htotal * 1000) / clock);
1003 line_count = (cursor_latency_ns / line_time_us + 1000) / 1000;
1004 entries = line_count * 64 * pixel_size;
1005 tlb_miss = cursor->fifo_size*cursor->cacheline_size - hdisplay * 8;
1006 if (tlb_miss > 0)
1007 entries += tlb_miss;
1008 entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
1009 *cursor_wm = entries + cursor->guard_size;
1010 if (*cursor_wm > (int)cursor->max_wm)
1011 *cursor_wm = (int)cursor->max_wm;
1012
1013 return true;
1014}
1015
1016/*
1017 * Check the wm result.
1018 *
1019 * If any calculated watermark values is larger than the maximum value that
1020 * can be programmed into the associated watermark register, that watermark
1021 * must be disabled.
1022 */
1023static bool g4x_check_srwm(struct drm_device *dev,
1024 int display_wm, int cursor_wm,
1025 const struct intel_watermark_params *display,
1026 const struct intel_watermark_params *cursor)
1027{
1028 DRM_DEBUG_KMS("SR watermark: display plane %d, cursor %d\n",
1029 display_wm, cursor_wm);
1030
1031 if (display_wm > display->max_wm) {
1032 DRM_DEBUG_KMS("display watermark is too large(%d/%ld), disabling\n",
1033 display_wm, display->max_wm);
1034 return false;
1035 }
1036
1037 if (cursor_wm > cursor->max_wm) {
1038 DRM_DEBUG_KMS("cursor watermark is too large(%d/%ld), disabling\n",
1039 cursor_wm, cursor->max_wm);
1040 return false;
1041 }
1042
1043 if (!(display_wm || cursor_wm)) {
1044 DRM_DEBUG_KMS("SR latency is 0, disabling\n");
1045 return false;
1046 }
1047
1048 return true;
1049}
1050
1051static bool g4x_compute_srwm(struct drm_device *dev,
1052 int plane,
1053 int latency_ns,
1054 const struct intel_watermark_params *display,
1055 const struct intel_watermark_params *cursor,
1056 int *display_wm, int *cursor_wm)
1057{
1058 struct drm_crtc *crtc;
1059 int hdisplay, htotal, pixel_size, clock;
1060 unsigned long line_time_us;
1061 int line_count, line_size;
1062 int small, large;
1063 int entries;
1064
1065 if (!latency_ns) {
1066 *display_wm = *cursor_wm = 0;
1067 return false;
1068 }
1069
1070 crtc = intel_get_crtc_for_plane(dev, plane);
1071 hdisplay = crtc->mode.hdisplay;
1072 htotal = crtc->mode.htotal;
1073 clock = crtc->mode.clock;
1074 pixel_size = crtc->fb->bits_per_pixel / 8;
1075
1076 line_time_us = (htotal * 1000) / clock;
1077 line_count = (latency_ns / line_time_us + 1000) / 1000;
1078 line_size = hdisplay * pixel_size;
1079
1080 /* Use the minimum of the small and large buffer method for primary */
1081 small = ((clock * pixel_size / 1000) * latency_ns) / 1000;
1082 large = line_count * line_size;
1083
1084 entries = DIV_ROUND_UP(min(small, large), display->cacheline_size);
1085 *display_wm = entries + display->guard_size;
1086
1087 /* calculate the self-refresh watermark for display cursor */
1088 entries = line_count * pixel_size * 64;
1089 entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
1090 *cursor_wm = entries + cursor->guard_size;
1091
1092 return g4x_check_srwm(dev,
1093 *display_wm, *cursor_wm,
1094 display, cursor);
1095}
1096
1097static bool vlv_compute_drain_latency(struct drm_device *dev,
1098 int plane,
1099 int *plane_prec_mult,
1100 int *plane_dl,
1101 int *cursor_prec_mult,
1102 int *cursor_dl)
1103{
1104 struct drm_crtc *crtc;
1105 int clock, pixel_size;
1106 int entries;
1107
1108 crtc = intel_get_crtc_for_plane(dev, plane);
1109 if (crtc->fb == NULL || !crtc->enabled)
1110 return false;
1111
1112 clock = crtc->mode.clock; /* VESA DOT Clock */
1113 pixel_size = crtc->fb->bits_per_pixel / 8; /* BPP */
1114
1115 entries = (clock / 1000) * pixel_size;
1116 *plane_prec_mult = (entries > 256) ?
1117 DRAIN_LATENCY_PRECISION_32 : DRAIN_LATENCY_PRECISION_16;
1118 *plane_dl = (64 * (*plane_prec_mult) * 4) / ((clock / 1000) *
1119 pixel_size);
1120
1121 entries = (clock / 1000) * 4; /* BPP is always 4 for cursor */
1122 *cursor_prec_mult = (entries > 256) ?
1123 DRAIN_LATENCY_PRECISION_32 : DRAIN_LATENCY_PRECISION_16;
1124 *cursor_dl = (64 * (*cursor_prec_mult) * 4) / ((clock / 1000) * 4);
1125
1126 return true;
1127}
1128
1129/*
1130 * Update drain latency registers of memory arbiter
1131 *
1132 * Valleyview SoC has a new memory arbiter and needs drain latency registers
1133 * to be programmed. Each plane has a drain latency multiplier and a drain
1134 * latency value.
1135 */
1136
1137static void vlv_update_drain_latency(struct drm_device *dev)
1138{
1139 struct drm_i915_private *dev_priv = dev->dev_private;
1140 int planea_prec, planea_dl, planeb_prec, planeb_dl;
1141 int cursora_prec, cursora_dl, cursorb_prec, cursorb_dl;
1142 int plane_prec_mult, cursor_prec_mult; /* Precision multiplier is
1143 either 16 or 32 */
1144
1145 /* For plane A, Cursor A */
1146 if (vlv_compute_drain_latency(dev, 0, &plane_prec_mult, &planea_dl,
1147 &cursor_prec_mult, &cursora_dl)) {
1148 cursora_prec = (cursor_prec_mult == DRAIN_LATENCY_PRECISION_32) ?
1149 DDL_CURSORA_PRECISION_32 : DDL_CURSORA_PRECISION_16;
1150 planea_prec = (plane_prec_mult == DRAIN_LATENCY_PRECISION_32) ?
1151 DDL_PLANEA_PRECISION_32 : DDL_PLANEA_PRECISION_16;
1152
1153 I915_WRITE(VLV_DDL1, cursora_prec |
1154 (cursora_dl << DDL_CURSORA_SHIFT) |
1155 planea_prec | planea_dl);
1156 }
1157
1158 /* For plane B, Cursor B */
1159 if (vlv_compute_drain_latency(dev, 1, &plane_prec_mult, &planeb_dl,
1160 &cursor_prec_mult, &cursorb_dl)) {
1161 cursorb_prec = (cursor_prec_mult == DRAIN_LATENCY_PRECISION_32) ?
1162 DDL_CURSORB_PRECISION_32 : DDL_CURSORB_PRECISION_16;
1163 planeb_prec = (plane_prec_mult == DRAIN_LATENCY_PRECISION_32) ?
1164 DDL_PLANEB_PRECISION_32 : DDL_PLANEB_PRECISION_16;
1165
1166 I915_WRITE(VLV_DDL2, cursorb_prec |
1167 (cursorb_dl << DDL_CURSORB_SHIFT) |
1168 planeb_prec | planeb_dl);
1169 }
1170}
1171
1172#define single_plane_enabled(mask) is_power_of_2(mask)
1173
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03001174static void valleyview_update_wm(struct drm_device *dev)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001175{
1176 static const int sr_latency_ns = 12000;
1177 struct drm_i915_private *dev_priv = dev->dev_private;
1178 int planea_wm, planeb_wm, cursora_wm, cursorb_wm;
1179 int plane_sr, cursor_sr;
1180 unsigned int enabled = 0;
1181
1182 vlv_update_drain_latency(dev);
1183
1184 if (g4x_compute_wm0(dev, 0,
1185 &valleyview_wm_info, latency_ns,
1186 &valleyview_cursor_wm_info, latency_ns,
1187 &planea_wm, &cursora_wm))
1188 enabled |= 1;
1189
1190 if (g4x_compute_wm0(dev, 1,
1191 &valleyview_wm_info, latency_ns,
1192 &valleyview_cursor_wm_info, latency_ns,
1193 &planeb_wm, &cursorb_wm))
1194 enabled |= 2;
1195
1196 plane_sr = cursor_sr = 0;
1197 if (single_plane_enabled(enabled) &&
1198 g4x_compute_srwm(dev, ffs(enabled) - 1,
1199 sr_latency_ns,
1200 &valleyview_wm_info,
1201 &valleyview_cursor_wm_info,
1202 &plane_sr, &cursor_sr))
1203 I915_WRITE(FW_BLC_SELF_VLV, FW_CSPWRDWNEN);
1204 else
1205 I915_WRITE(FW_BLC_SELF_VLV,
1206 I915_READ(FW_BLC_SELF_VLV) & ~FW_CSPWRDWNEN);
1207
1208 DRM_DEBUG_KMS("Setting FIFO watermarks - A: plane=%d, cursor=%d, B: plane=%d, cursor=%d, SR: plane=%d, cursor=%d\n",
1209 planea_wm, cursora_wm,
1210 planeb_wm, cursorb_wm,
1211 plane_sr, cursor_sr);
1212
1213 I915_WRITE(DSPFW1,
1214 (plane_sr << DSPFW_SR_SHIFT) |
1215 (cursorb_wm << DSPFW_CURSORB_SHIFT) |
1216 (planeb_wm << DSPFW_PLANEB_SHIFT) |
1217 planea_wm);
1218 I915_WRITE(DSPFW2,
1219 (I915_READ(DSPFW2) & DSPFW_CURSORA_MASK) |
1220 (cursora_wm << DSPFW_CURSORA_SHIFT));
1221 I915_WRITE(DSPFW3,
1222 (I915_READ(DSPFW3) | (cursor_sr << DSPFW_CURSOR_SR_SHIFT)));
1223}
1224
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03001225static void g4x_update_wm(struct drm_device *dev)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001226{
1227 static const int sr_latency_ns = 12000;
1228 struct drm_i915_private *dev_priv = dev->dev_private;
1229 int planea_wm, planeb_wm, cursora_wm, cursorb_wm;
1230 int plane_sr, cursor_sr;
1231 unsigned int enabled = 0;
1232
1233 if (g4x_compute_wm0(dev, 0,
1234 &g4x_wm_info, latency_ns,
1235 &g4x_cursor_wm_info, latency_ns,
1236 &planea_wm, &cursora_wm))
1237 enabled |= 1;
1238
1239 if (g4x_compute_wm0(dev, 1,
1240 &g4x_wm_info, latency_ns,
1241 &g4x_cursor_wm_info, latency_ns,
1242 &planeb_wm, &cursorb_wm))
1243 enabled |= 2;
1244
1245 plane_sr = cursor_sr = 0;
1246 if (single_plane_enabled(enabled) &&
1247 g4x_compute_srwm(dev, ffs(enabled) - 1,
1248 sr_latency_ns,
1249 &g4x_wm_info,
1250 &g4x_cursor_wm_info,
1251 &plane_sr, &cursor_sr))
1252 I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_EN);
1253 else
1254 I915_WRITE(FW_BLC_SELF,
1255 I915_READ(FW_BLC_SELF) & ~FW_BLC_SELF_EN);
1256
1257 DRM_DEBUG_KMS("Setting FIFO watermarks - A: plane=%d, cursor=%d, B: plane=%d, cursor=%d, SR: plane=%d, cursor=%d\n",
1258 planea_wm, cursora_wm,
1259 planeb_wm, cursorb_wm,
1260 plane_sr, cursor_sr);
1261
1262 I915_WRITE(DSPFW1,
1263 (plane_sr << DSPFW_SR_SHIFT) |
1264 (cursorb_wm << DSPFW_CURSORB_SHIFT) |
1265 (planeb_wm << DSPFW_PLANEB_SHIFT) |
1266 planea_wm);
1267 I915_WRITE(DSPFW2,
1268 (I915_READ(DSPFW2) & DSPFW_CURSORA_MASK) |
1269 (cursora_wm << DSPFW_CURSORA_SHIFT));
1270 /* HPLL off in SR has some issues on G4x... disable it */
1271 I915_WRITE(DSPFW3,
1272 (I915_READ(DSPFW3) & ~DSPFW_HPLL_SR_EN) |
1273 (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
1274}
1275
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03001276static void i965_update_wm(struct drm_device *dev)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001277{
1278 struct drm_i915_private *dev_priv = dev->dev_private;
1279 struct drm_crtc *crtc;
1280 int srwm = 1;
1281 int cursor_sr = 16;
1282
1283 /* Calc sr entries for one plane configs */
1284 crtc = single_enabled_crtc(dev);
1285 if (crtc) {
1286 /* self-refresh has much higher latency */
1287 static const int sr_latency_ns = 12000;
1288 int clock = crtc->mode.clock;
1289 int htotal = crtc->mode.htotal;
1290 int hdisplay = crtc->mode.hdisplay;
1291 int pixel_size = crtc->fb->bits_per_pixel / 8;
1292 unsigned long line_time_us;
1293 int entries;
1294
1295 line_time_us = ((htotal * 1000) / clock);
1296
1297 /* Use ns/us then divide to preserve precision */
1298 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
1299 pixel_size * hdisplay;
1300 entries = DIV_ROUND_UP(entries, I915_FIFO_LINE_SIZE);
1301 srwm = I965_FIFO_SIZE - entries;
1302 if (srwm < 0)
1303 srwm = 1;
1304 srwm &= 0x1ff;
1305 DRM_DEBUG_KMS("self-refresh entries: %d, wm: %d\n",
1306 entries, srwm);
1307
1308 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
1309 pixel_size * 64;
1310 entries = DIV_ROUND_UP(entries,
1311 i965_cursor_wm_info.cacheline_size);
1312 cursor_sr = i965_cursor_wm_info.fifo_size -
1313 (entries + i965_cursor_wm_info.guard_size);
1314
1315 if (cursor_sr > i965_cursor_wm_info.max_wm)
1316 cursor_sr = i965_cursor_wm_info.max_wm;
1317
1318 DRM_DEBUG_KMS("self-refresh watermark: display plane %d "
1319 "cursor %d\n", srwm, cursor_sr);
1320
1321 if (IS_CRESTLINE(dev))
1322 I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_EN);
1323 } else {
1324 /* Turn off self refresh if both pipes are enabled */
1325 if (IS_CRESTLINE(dev))
1326 I915_WRITE(FW_BLC_SELF, I915_READ(FW_BLC_SELF)
1327 & ~FW_BLC_SELF_EN);
1328 }
1329
1330 DRM_DEBUG_KMS("Setting FIFO watermarks - A: 8, B: 8, C: 8, SR %d\n",
1331 srwm);
1332
1333 /* 965 has limitations... */
1334 I915_WRITE(DSPFW1, (srwm << DSPFW_SR_SHIFT) |
1335 (8 << 16) | (8 << 8) | (8 << 0));
1336 I915_WRITE(DSPFW2, (8 << 8) | (8 << 0));
1337 /* update cursor SR watermark */
1338 I915_WRITE(DSPFW3, (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
1339}
1340
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03001341static void i9xx_update_wm(struct drm_device *dev)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001342{
1343 struct drm_i915_private *dev_priv = dev->dev_private;
1344 const struct intel_watermark_params *wm_info;
1345 uint32_t fwater_lo;
1346 uint32_t fwater_hi;
1347 int cwm, srwm = 1;
1348 int fifo_size;
1349 int planea_wm, planeb_wm;
1350 struct drm_crtc *crtc, *enabled = NULL;
1351
1352 if (IS_I945GM(dev))
1353 wm_info = &i945_wm_info;
1354 else if (!IS_GEN2(dev))
1355 wm_info = &i915_wm_info;
1356 else
1357 wm_info = &i855_wm_info;
1358
1359 fifo_size = dev_priv->display.get_fifo_size(dev, 0);
1360 crtc = intel_get_crtc_for_plane(dev, 0);
1361 if (crtc->enabled && crtc->fb) {
1362 planea_wm = intel_calculate_wm(crtc->mode.clock,
1363 wm_info, fifo_size,
1364 crtc->fb->bits_per_pixel / 8,
1365 latency_ns);
1366 enabled = crtc;
1367 } else
1368 planea_wm = fifo_size - wm_info->guard_size;
1369
1370 fifo_size = dev_priv->display.get_fifo_size(dev, 1);
1371 crtc = intel_get_crtc_for_plane(dev, 1);
1372 if (crtc->enabled && crtc->fb) {
1373 planeb_wm = intel_calculate_wm(crtc->mode.clock,
1374 wm_info, fifo_size,
1375 crtc->fb->bits_per_pixel / 8,
1376 latency_ns);
1377 if (enabled == NULL)
1378 enabled = crtc;
1379 else
1380 enabled = NULL;
1381 } else
1382 planeb_wm = fifo_size - wm_info->guard_size;
1383
1384 DRM_DEBUG_KMS("FIFO watermarks - A: %d, B: %d\n", planea_wm, planeb_wm);
1385
1386 /*
1387 * Overlay gets an aggressive default since video jitter is bad.
1388 */
1389 cwm = 2;
1390
1391 /* Play safe and disable self-refresh before adjusting watermarks. */
1392 if (IS_I945G(dev) || IS_I945GM(dev))
1393 I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_EN_MASK | 0);
1394 else if (IS_I915GM(dev))
1395 I915_WRITE(INSTPM, I915_READ(INSTPM) & ~INSTPM_SELF_EN);
1396
1397 /* Calc sr entries for one plane configs */
1398 if (HAS_FW_BLC(dev) && enabled) {
1399 /* self-refresh has much higher latency */
1400 static const int sr_latency_ns = 6000;
1401 int clock = enabled->mode.clock;
1402 int htotal = enabled->mode.htotal;
1403 int hdisplay = enabled->mode.hdisplay;
1404 int pixel_size = enabled->fb->bits_per_pixel / 8;
1405 unsigned long line_time_us;
1406 int entries;
1407
1408 line_time_us = (htotal * 1000) / clock;
1409
1410 /* Use ns/us then divide to preserve precision */
1411 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
1412 pixel_size * hdisplay;
1413 entries = DIV_ROUND_UP(entries, wm_info->cacheline_size);
1414 DRM_DEBUG_KMS("self-refresh entries: %d\n", entries);
1415 srwm = wm_info->fifo_size - entries;
1416 if (srwm < 0)
1417 srwm = 1;
1418
1419 if (IS_I945G(dev) || IS_I945GM(dev))
1420 I915_WRITE(FW_BLC_SELF,
1421 FW_BLC_SELF_FIFO_MASK | (srwm & 0xff));
1422 else if (IS_I915GM(dev))
1423 I915_WRITE(FW_BLC_SELF, srwm & 0x3f);
1424 }
1425
1426 DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d, B: %d, C: %d, SR %d\n",
1427 planea_wm, planeb_wm, cwm, srwm);
1428
1429 fwater_lo = ((planeb_wm & 0x3f) << 16) | (planea_wm & 0x3f);
1430 fwater_hi = (cwm & 0x1f);
1431
1432 /* Set request length to 8 cachelines per fetch */
1433 fwater_lo = fwater_lo | (1 << 24) | (1 << 8);
1434 fwater_hi = fwater_hi | (1 << 8);
1435
1436 I915_WRITE(FW_BLC, fwater_lo);
1437 I915_WRITE(FW_BLC2, fwater_hi);
1438
1439 if (HAS_FW_BLC(dev)) {
1440 if (enabled) {
1441 if (IS_I945G(dev) || IS_I945GM(dev))
1442 I915_WRITE(FW_BLC_SELF,
1443 FW_BLC_SELF_EN_MASK | FW_BLC_SELF_EN);
1444 else if (IS_I915GM(dev))
1445 I915_WRITE(INSTPM, I915_READ(INSTPM) | INSTPM_SELF_EN);
1446 DRM_DEBUG_KMS("memory self refresh enabled\n");
1447 } else
1448 DRM_DEBUG_KMS("memory self refresh disabled\n");
1449 }
1450}
1451
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03001452static void i830_update_wm(struct drm_device *dev)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001453{
1454 struct drm_i915_private *dev_priv = dev->dev_private;
1455 struct drm_crtc *crtc;
1456 uint32_t fwater_lo;
1457 int planea_wm;
1458
1459 crtc = single_enabled_crtc(dev);
1460 if (crtc == NULL)
1461 return;
1462
1463 planea_wm = intel_calculate_wm(crtc->mode.clock, &i830_wm_info,
1464 dev_priv->display.get_fifo_size(dev, 0),
1465 crtc->fb->bits_per_pixel / 8,
1466 latency_ns);
1467 fwater_lo = I915_READ(FW_BLC) & ~0xfff;
1468 fwater_lo |= (3<<8) | planea_wm;
1469
1470 DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d\n", planea_wm);
1471
1472 I915_WRITE(FW_BLC, fwater_lo);
1473}
1474
1475#define ILK_LP0_PLANE_LATENCY 700
1476#define ILK_LP0_CURSOR_LATENCY 1300
1477
1478/*
1479 * Check the wm result.
1480 *
1481 * If any calculated watermark values is larger than the maximum value that
1482 * can be programmed into the associated watermark register, that watermark
1483 * must be disabled.
1484 */
1485static bool ironlake_check_srwm(struct drm_device *dev, int level,
1486 int fbc_wm, int display_wm, int cursor_wm,
1487 const struct intel_watermark_params *display,
1488 const struct intel_watermark_params *cursor)
1489{
1490 struct drm_i915_private *dev_priv = dev->dev_private;
1491
1492 DRM_DEBUG_KMS("watermark %d: display plane %d, fbc lines %d,"
1493 " cursor %d\n", level, display_wm, fbc_wm, cursor_wm);
1494
1495 if (fbc_wm > SNB_FBC_MAX_SRWM) {
1496 DRM_DEBUG_KMS("fbc watermark(%d) is too large(%d), disabling wm%d+\n",
1497 fbc_wm, SNB_FBC_MAX_SRWM, level);
1498
1499 /* fbc has it's own way to disable FBC WM */
1500 I915_WRITE(DISP_ARB_CTL,
1501 I915_READ(DISP_ARB_CTL) | DISP_FBC_WM_DIS);
1502 return false;
1503 }
1504
1505 if (display_wm > display->max_wm) {
1506 DRM_DEBUG_KMS("display watermark(%d) is too large(%d), disabling wm%d+\n",
1507 display_wm, SNB_DISPLAY_MAX_SRWM, level);
1508 return false;
1509 }
1510
1511 if (cursor_wm > cursor->max_wm) {
1512 DRM_DEBUG_KMS("cursor watermark(%d) is too large(%d), disabling wm%d+\n",
1513 cursor_wm, SNB_CURSOR_MAX_SRWM, level);
1514 return false;
1515 }
1516
1517 if (!(fbc_wm || display_wm || cursor_wm)) {
1518 DRM_DEBUG_KMS("latency %d is 0, disabling wm%d+\n", level, level);
1519 return false;
1520 }
1521
1522 return true;
1523}
1524
1525/*
1526 * Compute watermark values of WM[1-3],
1527 */
1528static bool ironlake_compute_srwm(struct drm_device *dev, int level, int plane,
1529 int latency_ns,
1530 const struct intel_watermark_params *display,
1531 const struct intel_watermark_params *cursor,
1532 int *fbc_wm, int *display_wm, int *cursor_wm)
1533{
1534 struct drm_crtc *crtc;
1535 unsigned long line_time_us;
1536 int hdisplay, htotal, pixel_size, clock;
1537 int line_count, line_size;
1538 int small, large;
1539 int entries;
1540
1541 if (!latency_ns) {
1542 *fbc_wm = *display_wm = *cursor_wm = 0;
1543 return false;
1544 }
1545
1546 crtc = intel_get_crtc_for_plane(dev, plane);
1547 hdisplay = crtc->mode.hdisplay;
1548 htotal = crtc->mode.htotal;
1549 clock = crtc->mode.clock;
1550 pixel_size = crtc->fb->bits_per_pixel / 8;
1551
1552 line_time_us = (htotal * 1000) / clock;
1553 line_count = (latency_ns / line_time_us + 1000) / 1000;
1554 line_size = hdisplay * pixel_size;
1555
1556 /* Use the minimum of the small and large buffer method for primary */
1557 small = ((clock * pixel_size / 1000) * latency_ns) / 1000;
1558 large = line_count * line_size;
1559
1560 entries = DIV_ROUND_UP(min(small, large), display->cacheline_size);
1561 *display_wm = entries + display->guard_size;
1562
1563 /*
1564 * Spec says:
1565 * FBC WM = ((Final Primary WM * 64) / number of bytes per line) + 2
1566 */
1567 *fbc_wm = DIV_ROUND_UP(*display_wm * 64, line_size) + 2;
1568
1569 /* calculate the self-refresh watermark for display cursor */
1570 entries = line_count * pixel_size * 64;
1571 entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
1572 *cursor_wm = entries + cursor->guard_size;
1573
1574 return ironlake_check_srwm(dev, level,
1575 *fbc_wm, *display_wm, *cursor_wm,
1576 display, cursor);
1577}
1578
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03001579static void ironlake_update_wm(struct drm_device *dev)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001580{
1581 struct drm_i915_private *dev_priv = dev->dev_private;
1582 int fbc_wm, plane_wm, cursor_wm;
1583 unsigned int enabled;
1584
1585 enabled = 0;
1586 if (g4x_compute_wm0(dev, 0,
1587 &ironlake_display_wm_info,
1588 ILK_LP0_PLANE_LATENCY,
1589 &ironlake_cursor_wm_info,
1590 ILK_LP0_CURSOR_LATENCY,
1591 &plane_wm, &cursor_wm)) {
1592 I915_WRITE(WM0_PIPEA_ILK,
1593 (plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm);
1594 DRM_DEBUG_KMS("FIFO watermarks For pipe A -"
1595 " plane %d, " "cursor: %d\n",
1596 plane_wm, cursor_wm);
1597 enabled |= 1;
1598 }
1599
1600 if (g4x_compute_wm0(dev, 1,
1601 &ironlake_display_wm_info,
1602 ILK_LP0_PLANE_LATENCY,
1603 &ironlake_cursor_wm_info,
1604 ILK_LP0_CURSOR_LATENCY,
1605 &plane_wm, &cursor_wm)) {
1606 I915_WRITE(WM0_PIPEB_ILK,
1607 (plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm);
1608 DRM_DEBUG_KMS("FIFO watermarks For pipe B -"
1609 " plane %d, cursor: %d\n",
1610 plane_wm, cursor_wm);
1611 enabled |= 2;
1612 }
1613
1614 /*
1615 * Calculate and update the self-refresh watermark only when one
1616 * display plane is used.
1617 */
1618 I915_WRITE(WM3_LP_ILK, 0);
1619 I915_WRITE(WM2_LP_ILK, 0);
1620 I915_WRITE(WM1_LP_ILK, 0);
1621
1622 if (!single_plane_enabled(enabled))
1623 return;
1624 enabled = ffs(enabled) - 1;
1625
1626 /* WM1 */
1627 if (!ironlake_compute_srwm(dev, 1, enabled,
1628 ILK_READ_WM1_LATENCY() * 500,
1629 &ironlake_display_srwm_info,
1630 &ironlake_cursor_srwm_info,
1631 &fbc_wm, &plane_wm, &cursor_wm))
1632 return;
1633
1634 I915_WRITE(WM1_LP_ILK,
1635 WM1_LP_SR_EN |
1636 (ILK_READ_WM1_LATENCY() << WM1_LP_LATENCY_SHIFT) |
1637 (fbc_wm << WM1_LP_FBC_SHIFT) |
1638 (plane_wm << WM1_LP_SR_SHIFT) |
1639 cursor_wm);
1640
1641 /* WM2 */
1642 if (!ironlake_compute_srwm(dev, 2, enabled,
1643 ILK_READ_WM2_LATENCY() * 500,
1644 &ironlake_display_srwm_info,
1645 &ironlake_cursor_srwm_info,
1646 &fbc_wm, &plane_wm, &cursor_wm))
1647 return;
1648
1649 I915_WRITE(WM2_LP_ILK,
1650 WM2_LP_EN |
1651 (ILK_READ_WM2_LATENCY() << WM1_LP_LATENCY_SHIFT) |
1652 (fbc_wm << WM1_LP_FBC_SHIFT) |
1653 (plane_wm << WM1_LP_SR_SHIFT) |
1654 cursor_wm);
1655
1656 /*
1657 * WM3 is unsupported on ILK, probably because we don't have latency
1658 * data for that power state
1659 */
1660}
1661
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03001662static void sandybridge_update_wm(struct drm_device *dev)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001663{
1664 struct drm_i915_private *dev_priv = dev->dev_private;
1665 int latency = SNB_READ_WM0_LATENCY() * 100; /* In unit 0.1us */
1666 u32 val;
1667 int fbc_wm, plane_wm, cursor_wm;
1668 unsigned int enabled;
1669
1670 enabled = 0;
1671 if (g4x_compute_wm0(dev, 0,
1672 &sandybridge_display_wm_info, latency,
1673 &sandybridge_cursor_wm_info, latency,
1674 &plane_wm, &cursor_wm)) {
1675 val = I915_READ(WM0_PIPEA_ILK);
1676 val &= ~(WM0_PIPE_PLANE_MASK | WM0_PIPE_CURSOR_MASK);
1677 I915_WRITE(WM0_PIPEA_ILK, val |
1678 ((plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm));
1679 DRM_DEBUG_KMS("FIFO watermarks For pipe A -"
1680 " plane %d, " "cursor: %d\n",
1681 plane_wm, cursor_wm);
1682 enabled |= 1;
1683 }
1684
1685 if (g4x_compute_wm0(dev, 1,
1686 &sandybridge_display_wm_info, latency,
1687 &sandybridge_cursor_wm_info, latency,
1688 &plane_wm, &cursor_wm)) {
1689 val = I915_READ(WM0_PIPEB_ILK);
1690 val &= ~(WM0_PIPE_PLANE_MASK | WM0_PIPE_CURSOR_MASK);
1691 I915_WRITE(WM0_PIPEB_ILK, val |
1692 ((plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm));
1693 DRM_DEBUG_KMS("FIFO watermarks For pipe B -"
1694 " plane %d, cursor: %d\n",
1695 plane_wm, cursor_wm);
1696 enabled |= 2;
1697 }
1698
1699 /* IVB has 3 pipes */
1700 if (IS_IVYBRIDGE(dev) &&
1701 g4x_compute_wm0(dev, 2,
1702 &sandybridge_display_wm_info, latency,
1703 &sandybridge_cursor_wm_info, latency,
1704 &plane_wm, &cursor_wm)) {
1705 val = I915_READ(WM0_PIPEC_IVB);
1706 val &= ~(WM0_PIPE_PLANE_MASK | WM0_PIPE_CURSOR_MASK);
1707 I915_WRITE(WM0_PIPEC_IVB, val |
1708 ((plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm));
1709 DRM_DEBUG_KMS("FIFO watermarks For pipe C -"
1710 " plane %d, cursor: %d\n",
1711 plane_wm, cursor_wm);
1712 enabled |= 3;
1713 }
1714
1715 /*
1716 * Calculate and update the self-refresh watermark only when one
1717 * display plane is used.
1718 *
1719 * SNB support 3 levels of watermark.
1720 *
1721 * WM1/WM2/WM2 watermarks have to be enabled in the ascending order,
1722 * and disabled in the descending order
1723 *
1724 */
1725 I915_WRITE(WM3_LP_ILK, 0);
1726 I915_WRITE(WM2_LP_ILK, 0);
1727 I915_WRITE(WM1_LP_ILK, 0);
1728
1729 if (!single_plane_enabled(enabled) ||
1730 dev_priv->sprite_scaling_enabled)
1731 return;
1732 enabled = ffs(enabled) - 1;
1733
1734 /* WM1 */
1735 if (!ironlake_compute_srwm(dev, 1, enabled,
1736 SNB_READ_WM1_LATENCY() * 500,
1737 &sandybridge_display_srwm_info,
1738 &sandybridge_cursor_srwm_info,
1739 &fbc_wm, &plane_wm, &cursor_wm))
1740 return;
1741
1742 I915_WRITE(WM1_LP_ILK,
1743 WM1_LP_SR_EN |
1744 (SNB_READ_WM1_LATENCY() << WM1_LP_LATENCY_SHIFT) |
1745 (fbc_wm << WM1_LP_FBC_SHIFT) |
1746 (plane_wm << WM1_LP_SR_SHIFT) |
1747 cursor_wm);
1748
1749 /* WM2 */
1750 if (!ironlake_compute_srwm(dev, 2, enabled,
1751 SNB_READ_WM2_LATENCY() * 500,
1752 &sandybridge_display_srwm_info,
1753 &sandybridge_cursor_srwm_info,
1754 &fbc_wm, &plane_wm, &cursor_wm))
1755 return;
1756
1757 I915_WRITE(WM2_LP_ILK,
1758 WM2_LP_EN |
1759 (SNB_READ_WM2_LATENCY() << WM1_LP_LATENCY_SHIFT) |
1760 (fbc_wm << WM1_LP_FBC_SHIFT) |
1761 (plane_wm << WM1_LP_SR_SHIFT) |
1762 cursor_wm);
1763
1764 /* WM3 */
1765 if (!ironlake_compute_srwm(dev, 3, enabled,
1766 SNB_READ_WM3_LATENCY() * 500,
1767 &sandybridge_display_srwm_info,
1768 &sandybridge_cursor_srwm_info,
1769 &fbc_wm, &plane_wm, &cursor_wm))
1770 return;
1771
1772 I915_WRITE(WM3_LP_ILK,
1773 WM3_LP_EN |
1774 (SNB_READ_WM3_LATENCY() << WM1_LP_LATENCY_SHIFT) |
1775 (fbc_wm << WM1_LP_FBC_SHIFT) |
1776 (plane_wm << WM1_LP_SR_SHIFT) |
1777 cursor_wm);
1778}
1779
1780static bool
1781sandybridge_compute_sprite_wm(struct drm_device *dev, int plane,
1782 uint32_t sprite_width, int pixel_size,
1783 const struct intel_watermark_params *display,
1784 int display_latency_ns, int *sprite_wm)
1785{
1786 struct drm_crtc *crtc;
1787 int clock;
1788 int entries, tlb_miss;
1789
1790 crtc = intel_get_crtc_for_plane(dev, plane);
1791 if (crtc->fb == NULL || !crtc->enabled) {
1792 *sprite_wm = display->guard_size;
1793 return false;
1794 }
1795
1796 clock = crtc->mode.clock;
1797
1798 /* Use the small buffer method to calculate the sprite watermark */
1799 entries = ((clock * pixel_size / 1000) * display_latency_ns) / 1000;
1800 tlb_miss = display->fifo_size*display->cacheline_size -
1801 sprite_width * 8;
1802 if (tlb_miss > 0)
1803 entries += tlb_miss;
1804 entries = DIV_ROUND_UP(entries, display->cacheline_size);
1805 *sprite_wm = entries + display->guard_size;
1806 if (*sprite_wm > (int)display->max_wm)
1807 *sprite_wm = display->max_wm;
1808
1809 return true;
1810}
1811
1812static bool
1813sandybridge_compute_sprite_srwm(struct drm_device *dev, int plane,
1814 uint32_t sprite_width, int pixel_size,
1815 const struct intel_watermark_params *display,
1816 int latency_ns, int *sprite_wm)
1817{
1818 struct drm_crtc *crtc;
1819 unsigned long line_time_us;
1820 int clock;
1821 int line_count, line_size;
1822 int small, large;
1823 int entries;
1824
1825 if (!latency_ns) {
1826 *sprite_wm = 0;
1827 return false;
1828 }
1829
1830 crtc = intel_get_crtc_for_plane(dev, plane);
1831 clock = crtc->mode.clock;
1832 if (!clock) {
1833 *sprite_wm = 0;
1834 return false;
1835 }
1836
1837 line_time_us = (sprite_width * 1000) / clock;
1838 if (!line_time_us) {
1839 *sprite_wm = 0;
1840 return false;
1841 }
1842
1843 line_count = (latency_ns / line_time_us + 1000) / 1000;
1844 line_size = sprite_width * pixel_size;
1845
1846 /* Use the minimum of the small and large buffer method for primary */
1847 small = ((clock * pixel_size / 1000) * latency_ns) / 1000;
1848 large = line_count * line_size;
1849
1850 entries = DIV_ROUND_UP(min(small, large), display->cacheline_size);
1851 *sprite_wm = entries + display->guard_size;
1852
1853 return *sprite_wm > 0x3ff ? false : true;
1854}
1855
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03001856static void sandybridge_update_sprite_wm(struct drm_device *dev, int pipe,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001857 uint32_t sprite_width, int pixel_size)
1858{
1859 struct drm_i915_private *dev_priv = dev->dev_private;
1860 int latency = SNB_READ_WM0_LATENCY() * 100; /* In unit 0.1us */
1861 u32 val;
1862 int sprite_wm, reg;
1863 int ret;
1864
1865 switch (pipe) {
1866 case 0:
1867 reg = WM0_PIPEA_ILK;
1868 break;
1869 case 1:
1870 reg = WM0_PIPEB_ILK;
1871 break;
1872 case 2:
1873 reg = WM0_PIPEC_IVB;
1874 break;
1875 default:
1876 return; /* bad pipe */
1877 }
1878
1879 ret = sandybridge_compute_sprite_wm(dev, pipe, sprite_width, pixel_size,
1880 &sandybridge_display_wm_info,
1881 latency, &sprite_wm);
1882 if (!ret) {
1883 DRM_DEBUG_KMS("failed to compute sprite wm for pipe %d\n",
1884 pipe);
1885 return;
1886 }
1887
1888 val = I915_READ(reg);
1889 val &= ~WM0_PIPE_SPRITE_MASK;
1890 I915_WRITE(reg, val | (sprite_wm << WM0_PIPE_SPRITE_SHIFT));
1891 DRM_DEBUG_KMS("sprite watermarks For pipe %d - %d\n", pipe, sprite_wm);
1892
1893
1894 ret = sandybridge_compute_sprite_srwm(dev, pipe, sprite_width,
1895 pixel_size,
1896 &sandybridge_display_srwm_info,
1897 SNB_READ_WM1_LATENCY() * 500,
1898 &sprite_wm);
1899 if (!ret) {
1900 DRM_DEBUG_KMS("failed to compute sprite lp1 wm on pipe %d\n",
1901 pipe);
1902 return;
1903 }
1904 I915_WRITE(WM1S_LP_ILK, sprite_wm);
1905
1906 /* Only IVB has two more LP watermarks for sprite */
1907 if (!IS_IVYBRIDGE(dev))
1908 return;
1909
1910 ret = sandybridge_compute_sprite_srwm(dev, pipe, sprite_width,
1911 pixel_size,
1912 &sandybridge_display_srwm_info,
1913 SNB_READ_WM2_LATENCY() * 500,
1914 &sprite_wm);
1915 if (!ret) {
1916 DRM_DEBUG_KMS("failed to compute sprite lp2 wm on pipe %d\n",
1917 pipe);
1918 return;
1919 }
1920 I915_WRITE(WM2S_LP_IVB, sprite_wm);
1921
1922 ret = sandybridge_compute_sprite_srwm(dev, pipe, sprite_width,
1923 pixel_size,
1924 &sandybridge_display_srwm_info,
1925 SNB_READ_WM3_LATENCY() * 500,
1926 &sprite_wm);
1927 if (!ret) {
1928 DRM_DEBUG_KMS("failed to compute sprite lp3 wm on pipe %d\n",
1929 pipe);
1930 return;
1931 }
1932 I915_WRITE(WM3S_LP_IVB, sprite_wm);
1933}
1934
1935/**
1936 * intel_update_watermarks - update FIFO watermark values based on current modes
1937 *
1938 * Calculate watermark values for the various WM regs based on current mode
1939 * and plane configuration.
1940 *
1941 * There are several cases to deal with here:
1942 * - normal (i.e. non-self-refresh)
1943 * - self-refresh (SR) mode
1944 * - lines are large relative to FIFO size (buffer can hold up to 2)
1945 * - lines are small relative to FIFO size (buffer can hold more than 2
1946 * lines), so need to account for TLB latency
1947 *
1948 * The normal calculation is:
1949 * watermark = dotclock * bytes per pixel * latency
1950 * where latency is platform & configuration dependent (we assume pessimal
1951 * values here).
1952 *
1953 * The SR calculation is:
1954 * watermark = (trunc(latency/line time)+1) * surface width *
1955 * bytes per pixel
1956 * where
1957 * line time = htotal / dotclock
1958 * surface width = hdisplay for normal plane and 64 for cursor
1959 * and latency is assumed to be high, as above.
1960 *
1961 * The final value programmed to the register should always be rounded up,
1962 * and include an extra 2 entries to account for clock crossings.
1963 *
1964 * We don't use the sprite, so we can ignore that. And on Crestline we have
1965 * to set the non-SR watermarks to 8.
1966 */
1967void intel_update_watermarks(struct drm_device *dev)
1968{
1969 struct drm_i915_private *dev_priv = dev->dev_private;
1970
1971 if (dev_priv->display.update_wm)
1972 dev_priv->display.update_wm(dev);
1973}
1974
1975void intel_update_sprite_watermarks(struct drm_device *dev, int pipe,
1976 uint32_t sprite_width, int pixel_size)
1977{
1978 struct drm_i915_private *dev_priv = dev->dev_private;
1979
1980 if (dev_priv->display.update_sprite_wm)
1981 dev_priv->display.update_sprite_wm(dev, pipe, sprite_width,
1982 pixel_size);
1983}
1984
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03001985static struct drm_i915_gem_object *
1986intel_alloc_context_page(struct drm_device *dev)
1987{
1988 struct drm_i915_gem_object *ctx;
1989 int ret;
1990
1991 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
1992
1993 ctx = i915_gem_alloc_object(dev, 4096);
1994 if (!ctx) {
1995 DRM_DEBUG("failed to alloc power context, RC6 disabled\n");
1996 return NULL;
1997 }
1998
1999 ret = i915_gem_object_pin(ctx, 4096, true);
2000 if (ret) {
2001 DRM_ERROR("failed to pin power context: %d\n", ret);
2002 goto err_unref;
2003 }
2004
2005 ret = i915_gem_object_set_to_gtt_domain(ctx, 1);
2006 if (ret) {
2007 DRM_ERROR("failed to set-domain on power context: %d\n", ret);
2008 goto err_unpin;
2009 }
2010
2011 return ctx;
2012
2013err_unpin:
2014 i915_gem_object_unpin(ctx);
2015err_unref:
2016 drm_gem_object_unreference(&ctx->base);
2017 mutex_unlock(&dev->struct_mutex);
2018 return NULL;
2019}
2020
2021bool ironlake_set_drps(struct drm_device *dev, u8 val)
2022{
2023 struct drm_i915_private *dev_priv = dev->dev_private;
2024 u16 rgvswctl;
2025
2026 rgvswctl = I915_READ16(MEMSWCTL);
2027 if (rgvswctl & MEMCTL_CMD_STS) {
2028 DRM_DEBUG("gpu busy, RCS change rejected\n");
2029 return false; /* still busy with another command */
2030 }
2031
2032 rgvswctl = (MEMCTL_CMD_CHFREQ << MEMCTL_CMD_SHIFT) |
2033 (val << MEMCTL_FREQ_SHIFT) | MEMCTL_SFCAVM;
2034 I915_WRITE16(MEMSWCTL, rgvswctl);
2035 POSTING_READ16(MEMSWCTL);
2036
2037 rgvswctl |= MEMCTL_CMD_STS;
2038 I915_WRITE16(MEMSWCTL, rgvswctl);
2039
2040 return true;
2041}
2042
2043void ironlake_enable_drps(struct drm_device *dev)
2044{
2045 struct drm_i915_private *dev_priv = dev->dev_private;
2046 u32 rgvmodectl = I915_READ(MEMMODECTL);
2047 u8 fmax, fmin, fstart, vstart;
2048
2049 /* Enable temp reporting */
2050 I915_WRITE16(PMMISC, I915_READ(PMMISC) | MCPPCE_EN);
2051 I915_WRITE16(TSC1, I915_READ(TSC1) | TSE);
2052
2053 /* 100ms RC evaluation intervals */
2054 I915_WRITE(RCUPEI, 100000);
2055 I915_WRITE(RCDNEI, 100000);
2056
2057 /* Set max/min thresholds to 90ms and 80ms respectively */
2058 I915_WRITE(RCBMAXAVG, 90000);
2059 I915_WRITE(RCBMINAVG, 80000);
2060
2061 I915_WRITE(MEMIHYST, 1);
2062
2063 /* Set up min, max, and cur for interrupt handling */
2064 fmax = (rgvmodectl & MEMMODE_FMAX_MASK) >> MEMMODE_FMAX_SHIFT;
2065 fmin = (rgvmodectl & MEMMODE_FMIN_MASK);
2066 fstart = (rgvmodectl & MEMMODE_FSTART_MASK) >>
2067 MEMMODE_FSTART_SHIFT;
2068
2069 vstart = (I915_READ(PXVFREQ_BASE + (fstart * 4)) & PXVFREQ_PX_MASK) >>
2070 PXVFREQ_PX_SHIFT;
2071
2072 dev_priv->fmax = fmax; /* IPS callback will increase this */
2073 dev_priv->fstart = fstart;
2074
2075 dev_priv->max_delay = fstart;
2076 dev_priv->min_delay = fmin;
2077 dev_priv->cur_delay = fstart;
2078
2079 DRM_DEBUG_DRIVER("fmax: %d, fmin: %d, fstart: %d\n",
2080 fmax, fmin, fstart);
2081
2082 I915_WRITE(MEMINTREN, MEMINT_CX_SUPR_EN | MEMINT_EVAL_CHG_EN);
2083
2084 /*
2085 * Interrupts will be enabled in ironlake_irq_postinstall
2086 */
2087
2088 I915_WRITE(VIDSTART, vstart);
2089 POSTING_READ(VIDSTART);
2090
2091 rgvmodectl |= MEMMODE_SWMODE_EN;
2092 I915_WRITE(MEMMODECTL, rgvmodectl);
2093
2094 if (wait_for((I915_READ(MEMSWCTL) & MEMCTL_CMD_STS) == 0, 10))
2095 DRM_ERROR("stuck trying to change perf mode\n");
2096 msleep(1);
2097
2098 ironlake_set_drps(dev, fstart);
2099
2100 dev_priv->last_count1 = I915_READ(0x112e4) + I915_READ(0x112e8) +
2101 I915_READ(0x112e0);
2102 dev_priv->last_time1 = jiffies_to_msecs(jiffies);
2103 dev_priv->last_count2 = I915_READ(0x112f4);
2104 getrawmonotonic(&dev_priv->last_time2);
2105}
2106
2107void ironlake_disable_drps(struct drm_device *dev)
2108{
2109 struct drm_i915_private *dev_priv = dev->dev_private;
2110 u16 rgvswctl = I915_READ16(MEMSWCTL);
2111
2112 /* Ack interrupts, disable EFC interrupt */
2113 I915_WRITE(MEMINTREN, I915_READ(MEMINTREN) & ~MEMINT_EVAL_CHG_EN);
2114 I915_WRITE(MEMINTRSTS, MEMINT_EVAL_CHG);
2115 I915_WRITE(DEIER, I915_READ(DEIER) & ~DE_PCU_EVENT);
2116 I915_WRITE(DEIIR, DE_PCU_EVENT);
2117 I915_WRITE(DEIMR, I915_READ(DEIMR) | DE_PCU_EVENT);
2118
2119 /* Go back to the starting frequency */
2120 ironlake_set_drps(dev, dev_priv->fstart);
2121 msleep(1);
2122 rgvswctl |= MEMCTL_CMD_STS;
2123 I915_WRITE(MEMSWCTL, rgvswctl);
2124 msleep(1);
2125
2126}
2127
2128void gen6_set_rps(struct drm_device *dev, u8 val)
2129{
2130 struct drm_i915_private *dev_priv = dev->dev_private;
2131 u32 swreq;
2132
2133 swreq = (val & 0x3ff) << 25;
2134 I915_WRITE(GEN6_RPNSWREQ, swreq);
2135}
2136
2137void gen6_disable_rps(struct drm_device *dev)
2138{
2139 struct drm_i915_private *dev_priv = dev->dev_private;
2140
2141 I915_WRITE(GEN6_RPNSWREQ, 1 << 31);
2142 I915_WRITE(GEN6_PMINTRMSK, 0xffffffff);
2143 I915_WRITE(GEN6_PMIER, 0);
2144 /* Complete PM interrupt masking here doesn't race with the rps work
2145 * item again unmasking PM interrupts because that is using a different
2146 * register (PMIMR) to mask PM interrupts. The only risk is in leaving
2147 * stale bits in PMIIR and PMIMR which gen6_enable_rps will clean up. */
2148
2149 spin_lock_irq(&dev_priv->rps_lock);
2150 dev_priv->pm_iir = 0;
2151 spin_unlock_irq(&dev_priv->rps_lock);
2152
2153 I915_WRITE(GEN6_PMIIR, I915_READ(GEN6_PMIIR));
2154}
2155
2156int intel_enable_rc6(const struct drm_device *dev)
2157{
2158 /*
2159 * Respect the kernel parameter if it is set
2160 */
2161 if (i915_enable_rc6 >= 0)
2162 return i915_enable_rc6;
2163
2164 /*
2165 * Disable RC6 on Ironlake
2166 */
2167 if (INTEL_INFO(dev)->gen == 5)
2168 return 0;
2169
2170 /* Sorry Haswell, no RC6 for you for now. */
2171 if (IS_HASWELL(dev))
2172 return 0;
2173
2174 /*
2175 * Disable rc6 on Sandybridge
2176 */
2177 if (INTEL_INFO(dev)->gen == 6) {
2178 DRM_DEBUG_DRIVER("Sandybridge: deep RC6 disabled\n");
2179 return INTEL_RC6_ENABLE;
2180 }
2181 DRM_DEBUG_DRIVER("RC6 and deep RC6 enabled\n");
2182 return (INTEL_RC6_ENABLE | INTEL_RC6p_ENABLE);
2183}
2184
2185void gen6_enable_rps(struct drm_i915_private *dev_priv)
2186{
2187 u32 rp_state_cap = I915_READ(GEN6_RP_STATE_CAP);
2188 u32 gt_perf_status = I915_READ(GEN6_GT_PERF_STATUS);
2189 u32 pcu_mbox, rc6_mask = 0;
2190 u32 gtfifodbg;
2191 int cur_freq, min_freq, max_freq;
2192 int rc6_mode;
2193 int i;
2194
2195 /* Here begins a magic sequence of register writes to enable
2196 * auto-downclocking.
2197 *
2198 * Perhaps there might be some value in exposing these to
2199 * userspace...
2200 */
2201 I915_WRITE(GEN6_RC_STATE, 0);
2202 mutex_lock(&dev_priv->dev->struct_mutex);
2203
2204 /* Clear the DBG now so we don't confuse earlier errors */
2205 if ((gtfifodbg = I915_READ(GTFIFODBG))) {
2206 DRM_ERROR("GT fifo had a previous error %x\n", gtfifodbg);
2207 I915_WRITE(GTFIFODBG, gtfifodbg);
2208 }
2209
2210 gen6_gt_force_wake_get(dev_priv);
2211
2212 /* disable the counters and set deterministic thresholds */
2213 I915_WRITE(GEN6_RC_CONTROL, 0);
2214
2215 I915_WRITE(GEN6_RC1_WAKE_RATE_LIMIT, 1000 << 16);
2216 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16 | 30);
2217 I915_WRITE(GEN6_RC6pp_WAKE_RATE_LIMIT, 30);
2218 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
2219 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
2220
2221 for (i = 0; i < I915_NUM_RINGS; i++)
2222 I915_WRITE(RING_MAX_IDLE(dev_priv->ring[i].mmio_base), 10);
2223
2224 I915_WRITE(GEN6_RC_SLEEP, 0);
2225 I915_WRITE(GEN6_RC1e_THRESHOLD, 1000);
2226 I915_WRITE(GEN6_RC6_THRESHOLD, 50000);
2227 I915_WRITE(GEN6_RC6p_THRESHOLD, 100000);
2228 I915_WRITE(GEN6_RC6pp_THRESHOLD, 64000); /* unused */
2229
2230 rc6_mode = intel_enable_rc6(dev_priv->dev);
2231 if (rc6_mode & INTEL_RC6_ENABLE)
2232 rc6_mask |= GEN6_RC_CTL_RC6_ENABLE;
2233
2234 if (rc6_mode & INTEL_RC6p_ENABLE)
2235 rc6_mask |= GEN6_RC_CTL_RC6p_ENABLE;
2236
2237 if (rc6_mode & INTEL_RC6pp_ENABLE)
2238 rc6_mask |= GEN6_RC_CTL_RC6pp_ENABLE;
2239
2240 DRM_INFO("Enabling RC6 states: RC6 %s, RC6p %s, RC6pp %s\n",
2241 (rc6_mode & INTEL_RC6_ENABLE) ? "on" : "off",
2242 (rc6_mode & INTEL_RC6p_ENABLE) ? "on" : "off",
2243 (rc6_mode & INTEL_RC6pp_ENABLE) ? "on" : "off");
2244
2245 I915_WRITE(GEN6_RC_CONTROL,
2246 rc6_mask |
2247 GEN6_RC_CTL_EI_MODE(1) |
2248 GEN6_RC_CTL_HW_ENABLE);
2249
2250 I915_WRITE(GEN6_RPNSWREQ,
2251 GEN6_FREQUENCY(10) |
2252 GEN6_OFFSET(0) |
2253 GEN6_AGGRESSIVE_TURBO);
2254 I915_WRITE(GEN6_RC_VIDEO_FREQ,
2255 GEN6_FREQUENCY(12));
2256
2257 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 1000000);
2258 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS,
2259 18 << 24 |
2260 6 << 16);
2261 I915_WRITE(GEN6_RP_UP_THRESHOLD, 10000);
2262 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 1000000);
2263 I915_WRITE(GEN6_RP_UP_EI, 100000);
2264 I915_WRITE(GEN6_RP_DOWN_EI, 5000000);
2265 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
2266 I915_WRITE(GEN6_RP_CONTROL,
2267 GEN6_RP_MEDIA_TURBO |
2268 GEN6_RP_MEDIA_HW_MODE |
2269 GEN6_RP_MEDIA_IS_GFX |
2270 GEN6_RP_ENABLE |
2271 GEN6_RP_UP_BUSY_AVG |
2272 GEN6_RP_DOWN_IDLE_CONT);
2273
2274 if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
2275 500))
2276 DRM_ERROR("timeout waiting for pcode mailbox to become idle\n");
2277
2278 I915_WRITE(GEN6_PCODE_DATA, 0);
2279 I915_WRITE(GEN6_PCODE_MAILBOX,
2280 GEN6_PCODE_READY |
2281 GEN6_PCODE_WRITE_MIN_FREQ_TABLE);
2282 if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
2283 500))
2284 DRM_ERROR("timeout waiting for pcode mailbox to finish\n");
2285
2286 min_freq = (rp_state_cap & 0xff0000) >> 16;
2287 max_freq = rp_state_cap & 0xff;
2288 cur_freq = (gt_perf_status & 0xff00) >> 8;
2289
2290 /* Check for overclock support */
2291 if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
2292 500))
2293 DRM_ERROR("timeout waiting for pcode mailbox to become idle\n");
2294 I915_WRITE(GEN6_PCODE_MAILBOX, GEN6_READ_OC_PARAMS);
2295 pcu_mbox = I915_READ(GEN6_PCODE_DATA);
2296 if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
2297 500))
2298 DRM_ERROR("timeout waiting for pcode mailbox to finish\n");
2299 if (pcu_mbox & (1<<31)) { /* OC supported */
2300 max_freq = pcu_mbox & 0xff;
2301 DRM_DEBUG_DRIVER("overclocking supported, adjusting frequency max to %dMHz\n", pcu_mbox * 50);
2302 }
2303
2304 /* In units of 100MHz */
2305 dev_priv->max_delay = max_freq;
2306 dev_priv->min_delay = min_freq;
2307 dev_priv->cur_delay = cur_freq;
2308
2309 /* requires MSI enabled */
2310 I915_WRITE(GEN6_PMIER,
2311 GEN6_PM_MBOX_EVENT |
2312 GEN6_PM_THERMAL_EVENT |
2313 GEN6_PM_RP_DOWN_TIMEOUT |
2314 GEN6_PM_RP_UP_THRESHOLD |
2315 GEN6_PM_RP_DOWN_THRESHOLD |
2316 GEN6_PM_RP_UP_EI_EXPIRED |
2317 GEN6_PM_RP_DOWN_EI_EXPIRED);
2318 spin_lock_irq(&dev_priv->rps_lock);
2319 WARN_ON(dev_priv->pm_iir != 0);
2320 I915_WRITE(GEN6_PMIMR, 0);
2321 spin_unlock_irq(&dev_priv->rps_lock);
2322 /* enable all PM interrupts */
2323 I915_WRITE(GEN6_PMINTRMSK, 0);
2324
2325 gen6_gt_force_wake_put(dev_priv);
2326 mutex_unlock(&dev_priv->dev->struct_mutex);
2327}
2328
2329void gen6_update_ring_freq(struct drm_i915_private *dev_priv)
2330{
2331 int min_freq = 15;
2332 int gpu_freq, ia_freq, max_ia_freq;
2333 int scaling_factor = 180;
2334
2335 max_ia_freq = cpufreq_quick_get_max(0);
2336 /*
2337 * Default to measured freq if none found, PCU will ensure we don't go
2338 * over
2339 */
2340 if (!max_ia_freq)
2341 max_ia_freq = tsc_khz;
2342
2343 /* Convert from kHz to MHz */
2344 max_ia_freq /= 1000;
2345
2346 mutex_lock(&dev_priv->dev->struct_mutex);
2347
2348 /*
2349 * For each potential GPU frequency, load a ring frequency we'd like
2350 * to use for memory access. We do this by specifying the IA frequency
2351 * the PCU should use as a reference to determine the ring frequency.
2352 */
2353 for (gpu_freq = dev_priv->max_delay; gpu_freq >= dev_priv->min_delay;
2354 gpu_freq--) {
2355 int diff = dev_priv->max_delay - gpu_freq;
2356
2357 /*
2358 * For GPU frequencies less than 750MHz, just use the lowest
2359 * ring freq.
2360 */
2361 if (gpu_freq < min_freq)
2362 ia_freq = 800;
2363 else
2364 ia_freq = max_ia_freq - ((diff * scaling_factor) / 2);
2365 ia_freq = DIV_ROUND_CLOSEST(ia_freq, 100);
2366
2367 I915_WRITE(GEN6_PCODE_DATA,
2368 (ia_freq << GEN6_PCODE_FREQ_IA_RATIO_SHIFT) |
2369 gpu_freq);
2370 I915_WRITE(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY |
2371 GEN6_PCODE_WRITE_MIN_FREQ_TABLE);
2372 if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) &
2373 GEN6_PCODE_READY) == 0, 10)) {
2374 DRM_ERROR("pcode write of freq table timed out\n");
2375 continue;
2376 }
2377 }
2378
2379 mutex_unlock(&dev_priv->dev->struct_mutex);
2380}
2381
2382static void ironlake_teardown_rc6(struct drm_device *dev)
2383{
2384 struct drm_i915_private *dev_priv = dev->dev_private;
2385
2386 if (dev_priv->renderctx) {
2387 i915_gem_object_unpin(dev_priv->renderctx);
2388 drm_gem_object_unreference(&dev_priv->renderctx->base);
2389 dev_priv->renderctx = NULL;
2390 }
2391
2392 if (dev_priv->pwrctx) {
2393 i915_gem_object_unpin(dev_priv->pwrctx);
2394 drm_gem_object_unreference(&dev_priv->pwrctx->base);
2395 dev_priv->pwrctx = NULL;
2396 }
2397}
2398
2399void ironlake_disable_rc6(struct drm_device *dev)
2400{
2401 struct drm_i915_private *dev_priv = dev->dev_private;
2402
2403 if (I915_READ(PWRCTXA)) {
2404 /* Wake the GPU, prevent RC6, then restore RSTDBYCTL */
2405 I915_WRITE(RSTDBYCTL, I915_READ(RSTDBYCTL) | RCX_SW_EXIT);
2406 wait_for(((I915_READ(RSTDBYCTL) & RSX_STATUS_MASK) == RSX_STATUS_ON),
2407 50);
2408
2409 I915_WRITE(PWRCTXA, 0);
2410 POSTING_READ(PWRCTXA);
2411
2412 I915_WRITE(RSTDBYCTL, I915_READ(RSTDBYCTL) & ~RCX_SW_EXIT);
2413 POSTING_READ(RSTDBYCTL);
2414 }
2415
2416 ironlake_teardown_rc6(dev);
2417}
2418
2419static int ironlake_setup_rc6(struct drm_device *dev)
2420{
2421 struct drm_i915_private *dev_priv = dev->dev_private;
2422
2423 if (dev_priv->renderctx == NULL)
2424 dev_priv->renderctx = intel_alloc_context_page(dev);
2425 if (!dev_priv->renderctx)
2426 return -ENOMEM;
2427
2428 if (dev_priv->pwrctx == NULL)
2429 dev_priv->pwrctx = intel_alloc_context_page(dev);
2430 if (!dev_priv->pwrctx) {
2431 ironlake_teardown_rc6(dev);
2432 return -ENOMEM;
2433 }
2434
2435 return 0;
2436}
2437
2438void ironlake_enable_rc6(struct drm_device *dev)
2439{
2440 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter6d90c952012-04-26 23:28:05 +02002441 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002442 int ret;
2443
2444 /* rc6 disabled by default due to repeated reports of hanging during
2445 * boot and resume.
2446 */
2447 if (!intel_enable_rc6(dev))
2448 return;
2449
2450 mutex_lock(&dev->struct_mutex);
2451 ret = ironlake_setup_rc6(dev);
2452 if (ret) {
2453 mutex_unlock(&dev->struct_mutex);
2454 return;
2455 }
2456
2457 /*
2458 * GPU can automatically power down the render unit if given a page
2459 * to save state.
2460 */
Daniel Vetter6d90c952012-04-26 23:28:05 +02002461 ret = intel_ring_begin(ring, 6);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002462 if (ret) {
2463 ironlake_teardown_rc6(dev);
2464 mutex_unlock(&dev->struct_mutex);
2465 return;
2466 }
2467
Daniel Vetter6d90c952012-04-26 23:28:05 +02002468 intel_ring_emit(ring, MI_SUSPEND_FLUSH | MI_SUSPEND_FLUSH_EN);
2469 intel_ring_emit(ring, MI_SET_CONTEXT);
2470 intel_ring_emit(ring, dev_priv->renderctx->gtt_offset |
2471 MI_MM_SPACE_GTT |
2472 MI_SAVE_EXT_STATE_EN |
2473 MI_RESTORE_EXT_STATE_EN |
2474 MI_RESTORE_INHIBIT);
2475 intel_ring_emit(ring, MI_SUSPEND_FLUSH);
2476 intel_ring_emit(ring, MI_NOOP);
2477 intel_ring_emit(ring, MI_FLUSH);
2478 intel_ring_advance(ring);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002479
2480 /*
2481 * Wait for the command parser to advance past MI_SET_CONTEXT. The HW
2482 * does an implicit flush, combined with MI_FLUSH above, it should be
2483 * safe to assume that renderctx is valid
2484 */
Daniel Vetter6d90c952012-04-26 23:28:05 +02002485 ret = intel_wait_ring_idle(ring);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002486 if (ret) {
2487 DRM_ERROR("failed to enable ironlake power power savings\n");
2488 ironlake_teardown_rc6(dev);
2489 mutex_unlock(&dev->struct_mutex);
2490 return;
2491 }
2492
2493 I915_WRITE(PWRCTXA, dev_priv->pwrctx->gtt_offset | PWRCTX_EN);
2494 I915_WRITE(RSTDBYCTL, I915_READ(RSTDBYCTL) & ~RCX_SW_EXIT);
2495 mutex_unlock(&dev->struct_mutex);
2496}
2497
Eugeni Dodonovdde18882012-04-18 15:29:24 -03002498static unsigned long intel_pxfreq(u32 vidfreq)
2499{
2500 unsigned long freq;
2501 int div = (vidfreq & 0x3f0000) >> 16;
2502 int post = (vidfreq & 0x3000) >> 12;
2503 int pre = (vidfreq & 0x7);
2504
2505 if (!pre)
2506 return 0;
2507
2508 freq = ((div * 133333) / ((1<<post) * pre));
2509
2510 return freq;
2511}
2512
Daniel Vettereb48eb02012-04-26 23:28:12 +02002513static const struct cparams {
2514 u16 i;
2515 u16 t;
2516 u16 m;
2517 u16 c;
2518} cparams[] = {
2519 { 1, 1333, 301, 28664 },
2520 { 1, 1066, 294, 24460 },
2521 { 1, 800, 294, 25192 },
2522 { 0, 1333, 276, 27605 },
2523 { 0, 1066, 276, 27605 },
2524 { 0, 800, 231, 23784 },
2525};
2526
2527unsigned long i915_chipset_val(struct drm_i915_private *dev_priv)
2528{
2529 u64 total_count, diff, ret;
2530 u32 count1, count2, count3, m = 0, c = 0;
2531 unsigned long now = jiffies_to_msecs(jiffies), diff1;
2532 int i;
2533
2534 diff1 = now - dev_priv->last_time1;
2535
2536 /* Prevent division-by-zero if we are asking too fast.
2537 * Also, we don't get interesting results if we are polling
2538 * faster than once in 10ms, so just return the saved value
2539 * in such cases.
2540 */
2541 if (diff1 <= 10)
2542 return dev_priv->chipset_power;
2543
2544 count1 = I915_READ(DMIEC);
2545 count2 = I915_READ(DDREC);
2546 count3 = I915_READ(CSIEC);
2547
2548 total_count = count1 + count2 + count3;
2549
2550 /* FIXME: handle per-counter overflow */
2551 if (total_count < dev_priv->last_count1) {
2552 diff = ~0UL - dev_priv->last_count1;
2553 diff += total_count;
2554 } else {
2555 diff = total_count - dev_priv->last_count1;
2556 }
2557
2558 for (i = 0; i < ARRAY_SIZE(cparams); i++) {
2559 if (cparams[i].i == dev_priv->c_m &&
2560 cparams[i].t == dev_priv->r_t) {
2561 m = cparams[i].m;
2562 c = cparams[i].c;
2563 break;
2564 }
2565 }
2566
2567 diff = div_u64(diff, diff1);
2568 ret = ((m * diff) + c);
2569 ret = div_u64(ret, 10);
2570
2571 dev_priv->last_count1 = total_count;
2572 dev_priv->last_time1 = now;
2573
2574 dev_priv->chipset_power = ret;
2575
2576 return ret;
2577}
2578
2579unsigned long i915_mch_val(struct drm_i915_private *dev_priv)
2580{
2581 unsigned long m, x, b;
2582 u32 tsfs;
2583
2584 tsfs = I915_READ(TSFS);
2585
2586 m = ((tsfs & TSFS_SLOPE_MASK) >> TSFS_SLOPE_SHIFT);
2587 x = I915_READ8(TR1);
2588
2589 b = tsfs & TSFS_INTR_MASK;
2590
2591 return ((m * x) / 127) - b;
2592}
2593
2594static u16 pvid_to_extvid(struct drm_i915_private *dev_priv, u8 pxvid)
2595{
2596 static const struct v_table {
2597 u16 vd; /* in .1 mil */
2598 u16 vm; /* in .1 mil */
2599 } v_table[] = {
2600 { 0, 0, },
2601 { 375, 0, },
2602 { 500, 0, },
2603 { 625, 0, },
2604 { 750, 0, },
2605 { 875, 0, },
2606 { 1000, 0, },
2607 { 1125, 0, },
2608 { 4125, 3000, },
2609 { 4125, 3000, },
2610 { 4125, 3000, },
2611 { 4125, 3000, },
2612 { 4125, 3000, },
2613 { 4125, 3000, },
2614 { 4125, 3000, },
2615 { 4125, 3000, },
2616 { 4125, 3000, },
2617 { 4125, 3000, },
2618 { 4125, 3000, },
2619 { 4125, 3000, },
2620 { 4125, 3000, },
2621 { 4125, 3000, },
2622 { 4125, 3000, },
2623 { 4125, 3000, },
2624 { 4125, 3000, },
2625 { 4125, 3000, },
2626 { 4125, 3000, },
2627 { 4125, 3000, },
2628 { 4125, 3000, },
2629 { 4125, 3000, },
2630 { 4125, 3000, },
2631 { 4125, 3000, },
2632 { 4250, 3125, },
2633 { 4375, 3250, },
2634 { 4500, 3375, },
2635 { 4625, 3500, },
2636 { 4750, 3625, },
2637 { 4875, 3750, },
2638 { 5000, 3875, },
2639 { 5125, 4000, },
2640 { 5250, 4125, },
2641 { 5375, 4250, },
2642 { 5500, 4375, },
2643 { 5625, 4500, },
2644 { 5750, 4625, },
2645 { 5875, 4750, },
2646 { 6000, 4875, },
2647 { 6125, 5000, },
2648 { 6250, 5125, },
2649 { 6375, 5250, },
2650 { 6500, 5375, },
2651 { 6625, 5500, },
2652 { 6750, 5625, },
2653 { 6875, 5750, },
2654 { 7000, 5875, },
2655 { 7125, 6000, },
2656 { 7250, 6125, },
2657 { 7375, 6250, },
2658 { 7500, 6375, },
2659 { 7625, 6500, },
2660 { 7750, 6625, },
2661 { 7875, 6750, },
2662 { 8000, 6875, },
2663 { 8125, 7000, },
2664 { 8250, 7125, },
2665 { 8375, 7250, },
2666 { 8500, 7375, },
2667 { 8625, 7500, },
2668 { 8750, 7625, },
2669 { 8875, 7750, },
2670 { 9000, 7875, },
2671 { 9125, 8000, },
2672 { 9250, 8125, },
2673 { 9375, 8250, },
2674 { 9500, 8375, },
2675 { 9625, 8500, },
2676 { 9750, 8625, },
2677 { 9875, 8750, },
2678 { 10000, 8875, },
2679 { 10125, 9000, },
2680 { 10250, 9125, },
2681 { 10375, 9250, },
2682 { 10500, 9375, },
2683 { 10625, 9500, },
2684 { 10750, 9625, },
2685 { 10875, 9750, },
2686 { 11000, 9875, },
2687 { 11125, 10000, },
2688 { 11250, 10125, },
2689 { 11375, 10250, },
2690 { 11500, 10375, },
2691 { 11625, 10500, },
2692 { 11750, 10625, },
2693 { 11875, 10750, },
2694 { 12000, 10875, },
2695 { 12125, 11000, },
2696 { 12250, 11125, },
2697 { 12375, 11250, },
2698 { 12500, 11375, },
2699 { 12625, 11500, },
2700 { 12750, 11625, },
2701 { 12875, 11750, },
2702 { 13000, 11875, },
2703 { 13125, 12000, },
2704 { 13250, 12125, },
2705 { 13375, 12250, },
2706 { 13500, 12375, },
2707 { 13625, 12500, },
2708 { 13750, 12625, },
2709 { 13875, 12750, },
2710 { 14000, 12875, },
2711 { 14125, 13000, },
2712 { 14250, 13125, },
2713 { 14375, 13250, },
2714 { 14500, 13375, },
2715 { 14625, 13500, },
2716 { 14750, 13625, },
2717 { 14875, 13750, },
2718 { 15000, 13875, },
2719 { 15125, 14000, },
2720 { 15250, 14125, },
2721 { 15375, 14250, },
2722 { 15500, 14375, },
2723 { 15625, 14500, },
2724 { 15750, 14625, },
2725 { 15875, 14750, },
2726 { 16000, 14875, },
2727 { 16125, 15000, },
2728 };
2729 if (dev_priv->info->is_mobile)
2730 return v_table[pxvid].vm;
2731 else
2732 return v_table[pxvid].vd;
2733}
2734
2735void i915_update_gfx_val(struct drm_i915_private *dev_priv)
2736{
2737 struct timespec now, diff1;
2738 u64 diff;
2739 unsigned long diffms;
2740 u32 count;
2741
2742 if (dev_priv->info->gen != 5)
2743 return;
2744
2745 getrawmonotonic(&now);
2746 diff1 = timespec_sub(now, dev_priv->last_time2);
2747
2748 /* Don't divide by 0 */
2749 diffms = diff1.tv_sec * 1000 + diff1.tv_nsec / 1000000;
2750 if (!diffms)
2751 return;
2752
2753 count = I915_READ(GFXEC);
2754
2755 if (count < dev_priv->last_count2) {
2756 diff = ~0UL - dev_priv->last_count2;
2757 diff += count;
2758 } else {
2759 diff = count - dev_priv->last_count2;
2760 }
2761
2762 dev_priv->last_count2 = count;
2763 dev_priv->last_time2 = now;
2764
2765 /* More magic constants... */
2766 diff = diff * 1181;
2767 diff = div_u64(diff, diffms * 10);
2768 dev_priv->gfx_power = diff;
2769}
2770
2771unsigned long i915_gfx_val(struct drm_i915_private *dev_priv)
2772{
2773 unsigned long t, corr, state1, corr2, state2;
2774 u32 pxvid, ext_v;
2775
2776 pxvid = I915_READ(PXVFREQ_BASE + (dev_priv->cur_delay * 4));
2777 pxvid = (pxvid >> 24) & 0x7f;
2778 ext_v = pvid_to_extvid(dev_priv, pxvid);
2779
2780 state1 = ext_v;
2781
2782 t = i915_mch_val(dev_priv);
2783
2784 /* Revel in the empirically derived constants */
2785
2786 /* Correction factor in 1/100000 units */
2787 if (t > 80)
2788 corr = ((t * 2349) + 135940);
2789 else if (t >= 50)
2790 corr = ((t * 964) + 29317);
2791 else /* < 50 */
2792 corr = ((t * 301) + 1004);
2793
2794 corr = corr * ((150142 * state1) / 10000 - 78642);
2795 corr /= 100000;
2796 corr2 = (corr * dev_priv->corr);
2797
2798 state2 = (corr2 * state1) / 10000;
2799 state2 /= 100; /* convert to mW */
2800
2801 i915_update_gfx_val(dev_priv);
2802
2803 return dev_priv->gfx_power + state2;
2804}
2805
2806/* Global for IPS driver to get at the current i915 device */
2807static struct drm_i915_private *i915_mch_dev;
2808/*
2809 * Lock protecting IPS related data structures
2810 * - i915_mch_dev
2811 * - dev_priv->max_delay
2812 * - dev_priv->min_delay
2813 * - dev_priv->fmax
2814 * - dev_priv->gpu_busy
2815 */
2816static DEFINE_SPINLOCK(mchdev_lock);
2817
2818/**
2819 * i915_read_mch_val - return value for IPS use
2820 *
2821 * Calculate and return a value for the IPS driver to use when deciding whether
2822 * we have thermal and power headroom to increase CPU or GPU power budget.
2823 */
2824unsigned long i915_read_mch_val(void)
2825{
2826 struct drm_i915_private *dev_priv;
2827 unsigned long chipset_val, graphics_val, ret = 0;
2828
2829 spin_lock(&mchdev_lock);
2830 if (!i915_mch_dev)
2831 goto out_unlock;
2832 dev_priv = i915_mch_dev;
2833
2834 chipset_val = i915_chipset_val(dev_priv);
2835 graphics_val = i915_gfx_val(dev_priv);
2836
2837 ret = chipset_val + graphics_val;
2838
2839out_unlock:
2840 spin_unlock(&mchdev_lock);
2841
2842 return ret;
2843}
2844EXPORT_SYMBOL_GPL(i915_read_mch_val);
2845
2846/**
2847 * i915_gpu_raise - raise GPU frequency limit
2848 *
2849 * Raise the limit; IPS indicates we have thermal headroom.
2850 */
2851bool i915_gpu_raise(void)
2852{
2853 struct drm_i915_private *dev_priv;
2854 bool ret = true;
2855
2856 spin_lock(&mchdev_lock);
2857 if (!i915_mch_dev) {
2858 ret = false;
2859 goto out_unlock;
2860 }
2861 dev_priv = i915_mch_dev;
2862
2863 if (dev_priv->max_delay > dev_priv->fmax)
2864 dev_priv->max_delay--;
2865
2866out_unlock:
2867 spin_unlock(&mchdev_lock);
2868
2869 return ret;
2870}
2871EXPORT_SYMBOL_GPL(i915_gpu_raise);
2872
2873/**
2874 * i915_gpu_lower - lower GPU frequency limit
2875 *
2876 * IPS indicates we're close to a thermal limit, so throttle back the GPU
2877 * frequency maximum.
2878 */
2879bool i915_gpu_lower(void)
2880{
2881 struct drm_i915_private *dev_priv;
2882 bool ret = true;
2883
2884 spin_lock(&mchdev_lock);
2885 if (!i915_mch_dev) {
2886 ret = false;
2887 goto out_unlock;
2888 }
2889 dev_priv = i915_mch_dev;
2890
2891 if (dev_priv->max_delay < dev_priv->min_delay)
2892 dev_priv->max_delay++;
2893
2894out_unlock:
2895 spin_unlock(&mchdev_lock);
2896
2897 return ret;
2898}
2899EXPORT_SYMBOL_GPL(i915_gpu_lower);
2900
2901/**
2902 * i915_gpu_busy - indicate GPU business to IPS
2903 *
2904 * Tell the IPS driver whether or not the GPU is busy.
2905 */
2906bool i915_gpu_busy(void)
2907{
2908 struct drm_i915_private *dev_priv;
2909 bool ret = false;
2910
2911 spin_lock(&mchdev_lock);
2912 if (!i915_mch_dev)
2913 goto out_unlock;
2914 dev_priv = i915_mch_dev;
2915
2916 ret = dev_priv->busy;
2917
2918out_unlock:
2919 spin_unlock(&mchdev_lock);
2920
2921 return ret;
2922}
2923EXPORT_SYMBOL_GPL(i915_gpu_busy);
2924
2925/**
2926 * i915_gpu_turbo_disable - disable graphics turbo
2927 *
2928 * Disable graphics turbo by resetting the max frequency and setting the
2929 * current frequency to the default.
2930 */
2931bool i915_gpu_turbo_disable(void)
2932{
2933 struct drm_i915_private *dev_priv;
2934 bool ret = true;
2935
2936 spin_lock(&mchdev_lock);
2937 if (!i915_mch_dev) {
2938 ret = false;
2939 goto out_unlock;
2940 }
2941 dev_priv = i915_mch_dev;
2942
2943 dev_priv->max_delay = dev_priv->fstart;
2944
2945 if (!ironlake_set_drps(dev_priv->dev, dev_priv->fstart))
2946 ret = false;
2947
2948out_unlock:
2949 spin_unlock(&mchdev_lock);
2950
2951 return ret;
2952}
2953EXPORT_SYMBOL_GPL(i915_gpu_turbo_disable);
2954
2955/**
2956 * Tells the intel_ips driver that the i915 driver is now loaded, if
2957 * IPS got loaded first.
2958 *
2959 * This awkward dance is so that neither module has to depend on the
2960 * other in order for IPS to do the appropriate communication of
2961 * GPU turbo limits to i915.
2962 */
2963static void
2964ips_ping_for_i915_load(void)
2965{
2966 void (*link)(void);
2967
2968 link = symbol_get(ips_link_to_i915_driver);
2969 if (link) {
2970 link();
2971 symbol_put(ips_link_to_i915_driver);
2972 }
2973}
2974
2975void intel_gpu_ips_init(struct drm_i915_private *dev_priv)
2976{
2977 spin_lock(&mchdev_lock);
2978 i915_mch_dev = dev_priv;
2979 dev_priv->mchdev_lock = &mchdev_lock;
2980 spin_unlock(&mchdev_lock);
2981
2982 ips_ping_for_i915_load();
2983}
2984
2985void intel_gpu_ips_teardown(void)
2986{
2987 spin_lock(&mchdev_lock);
2988 i915_mch_dev = NULL;
2989 spin_unlock(&mchdev_lock);
2990}
2991
Eugeni Dodonovdde18882012-04-18 15:29:24 -03002992void intel_init_emon(struct drm_device *dev)
2993{
2994 struct drm_i915_private *dev_priv = dev->dev_private;
2995 u32 lcfuse;
2996 u8 pxw[16];
2997 int i;
2998
2999 /* Disable to program */
3000 I915_WRITE(ECR, 0);
3001 POSTING_READ(ECR);
3002
3003 /* Program energy weights for various events */
3004 I915_WRITE(SDEW, 0x15040d00);
3005 I915_WRITE(CSIEW0, 0x007f0000);
3006 I915_WRITE(CSIEW1, 0x1e220004);
3007 I915_WRITE(CSIEW2, 0x04000004);
3008
3009 for (i = 0; i < 5; i++)
3010 I915_WRITE(PEW + (i * 4), 0);
3011 for (i = 0; i < 3; i++)
3012 I915_WRITE(DEW + (i * 4), 0);
3013
3014 /* Program P-state weights to account for frequency power adjustment */
3015 for (i = 0; i < 16; i++) {
3016 u32 pxvidfreq = I915_READ(PXVFREQ_BASE + (i * 4));
3017 unsigned long freq = intel_pxfreq(pxvidfreq);
3018 unsigned long vid = (pxvidfreq & PXVFREQ_PX_MASK) >>
3019 PXVFREQ_PX_SHIFT;
3020 unsigned long val;
3021
3022 val = vid * vid;
3023 val *= (freq / 1000);
3024 val *= 255;
3025 val /= (127*127*900);
3026 if (val > 0xff)
3027 DRM_ERROR("bad pxval: %ld\n", val);
3028 pxw[i] = val;
3029 }
3030 /* Render standby states get 0 weight */
3031 pxw[14] = 0;
3032 pxw[15] = 0;
3033
3034 for (i = 0; i < 4; i++) {
3035 u32 val = (pxw[i*4] << 24) | (pxw[(i*4)+1] << 16) |
3036 (pxw[(i*4)+2] << 8) | (pxw[(i*4)+3]);
3037 I915_WRITE(PXW + (i * 4), val);
3038 }
3039
3040 /* Adjust magic regs to magic values (more experimental results) */
3041 I915_WRITE(OGW0, 0);
3042 I915_WRITE(OGW1, 0);
3043 I915_WRITE(EG0, 0x00007f00);
3044 I915_WRITE(EG1, 0x0000000e);
3045 I915_WRITE(EG2, 0x000e0000);
3046 I915_WRITE(EG3, 0x68000300);
3047 I915_WRITE(EG4, 0x42000000);
3048 I915_WRITE(EG5, 0x00140031);
3049 I915_WRITE(EG6, 0);
3050 I915_WRITE(EG7, 0);
3051
3052 for (i = 0; i < 8; i++)
3053 I915_WRITE(PXWL + (i * 4), 0);
3054
3055 /* Enable PMON + select events */
3056 I915_WRITE(ECR, 0x80000019);
3057
3058 lcfuse = I915_READ(LCFUSE02);
3059
3060 dev_priv->corr = (lcfuse & LCFUSE_HIV_MASK);
3061}
3062
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03003063static void ironlake_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03003064{
3065 struct drm_i915_private *dev_priv = dev->dev_private;
3066 uint32_t dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE;
3067
3068 /* Required for FBC */
3069 dspclk_gate |= DPFCUNIT_CLOCK_GATE_DISABLE |
3070 DPFCRUNIT_CLOCK_GATE_DISABLE |
3071 DPFDUNIT_CLOCK_GATE_DISABLE;
3072 /* Required for CxSR */
3073 dspclk_gate |= DPARBUNIT_CLOCK_GATE_DISABLE;
3074
3075 I915_WRITE(PCH_3DCGDIS0,
3076 MARIUNIT_CLOCK_GATE_DISABLE |
3077 SVSMUNIT_CLOCK_GATE_DISABLE);
3078 I915_WRITE(PCH_3DCGDIS1,
3079 VFMUNIT_CLOCK_GATE_DISABLE);
3080
3081 I915_WRITE(PCH_DSPCLK_GATE_D, dspclk_gate);
3082
3083 /*
3084 * According to the spec the following bits should be set in
3085 * order to enable memory self-refresh
3086 * The bit 22/21 of 0x42004
3087 * The bit 5 of 0x42020
3088 * The bit 15 of 0x45000
3089 */
3090 I915_WRITE(ILK_DISPLAY_CHICKEN2,
3091 (I915_READ(ILK_DISPLAY_CHICKEN2) |
3092 ILK_DPARB_GATE | ILK_VSDPFD_FULL));
3093 I915_WRITE(ILK_DSPCLK_GATE,
3094 (I915_READ(ILK_DSPCLK_GATE) |
3095 ILK_DPARB_CLK_GATE));
3096 I915_WRITE(DISP_ARB_CTL,
3097 (I915_READ(DISP_ARB_CTL) |
3098 DISP_FBC_WM_DIS));
3099 I915_WRITE(WM3_LP_ILK, 0);
3100 I915_WRITE(WM2_LP_ILK, 0);
3101 I915_WRITE(WM1_LP_ILK, 0);
3102
3103 /*
3104 * Based on the document from hardware guys the following bits
3105 * should be set unconditionally in order to enable FBC.
3106 * The bit 22 of 0x42000
3107 * The bit 22 of 0x42004
3108 * The bit 7,8,9 of 0x42020.
3109 */
3110 if (IS_IRONLAKE_M(dev)) {
3111 I915_WRITE(ILK_DISPLAY_CHICKEN1,
3112 I915_READ(ILK_DISPLAY_CHICKEN1) |
3113 ILK_FBCQ_DIS);
3114 I915_WRITE(ILK_DISPLAY_CHICKEN2,
3115 I915_READ(ILK_DISPLAY_CHICKEN2) |
3116 ILK_DPARB_GATE);
3117 I915_WRITE(ILK_DSPCLK_GATE,
3118 I915_READ(ILK_DSPCLK_GATE) |
3119 ILK_DPFC_DIS1 |
3120 ILK_DPFC_DIS2 |
3121 ILK_CLK_FBC);
3122 }
3123
3124 I915_WRITE(ILK_DISPLAY_CHICKEN2,
3125 I915_READ(ILK_DISPLAY_CHICKEN2) |
3126 ILK_ELPIN_409_SELECT);
3127 I915_WRITE(_3D_CHICKEN2,
3128 _3D_CHICKEN2_WM_READ_PIPELINED << 16 |
3129 _3D_CHICKEN2_WM_READ_PIPELINED);
3130}
3131
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03003132static void gen6_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03003133{
3134 struct drm_i915_private *dev_priv = dev->dev_private;
3135 int pipe;
3136 uint32_t dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE;
3137
3138 I915_WRITE(PCH_DSPCLK_GATE_D, dspclk_gate);
3139
3140 I915_WRITE(ILK_DISPLAY_CHICKEN2,
3141 I915_READ(ILK_DISPLAY_CHICKEN2) |
3142 ILK_ELPIN_409_SELECT);
3143
3144 I915_WRITE(WM3_LP_ILK, 0);
3145 I915_WRITE(WM2_LP_ILK, 0);
3146 I915_WRITE(WM1_LP_ILK, 0);
3147
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03003148 I915_WRITE(CACHE_MODE_0,
Daniel Vetter50743292012-04-26 22:02:54 +02003149 _MASKED_BIT_DISABLE(CM0_STC_EVICT_DISABLE_LRA_SNB));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03003150
3151 I915_WRITE(GEN6_UCGCTL1,
3152 I915_READ(GEN6_UCGCTL1) |
3153 GEN6_BLBUNIT_CLOCK_GATE_DISABLE |
3154 GEN6_CSUNIT_CLOCK_GATE_DISABLE);
3155
3156 /* According to the BSpec vol1g, bit 12 (RCPBUNIT) clock
3157 * gating disable must be set. Failure to set it results in
3158 * flickering pixels due to Z write ordering failures after
3159 * some amount of runtime in the Mesa "fire" demo, and Unigine
3160 * Sanctuary and Tropics, and apparently anything else with
3161 * alpha test or pixel discard.
3162 *
3163 * According to the spec, bit 11 (RCCUNIT) must also be set,
3164 * but we didn't debug actual testcases to find it out.
3165 */
3166 I915_WRITE(GEN6_UCGCTL2,
3167 GEN6_RCPBUNIT_CLOCK_GATE_DISABLE |
3168 GEN6_RCCUNIT_CLOCK_GATE_DISABLE);
3169
3170 /* Bspec says we need to always set all mask bits. */
3171 I915_WRITE(_3D_CHICKEN, (0xFFFF << 16) |
3172 _3D_CHICKEN_SF_DISABLE_FASTCLIP_CULL);
3173
3174 /*
3175 * According to the spec the following bits should be
3176 * set in order to enable memory self-refresh and fbc:
3177 * The bit21 and bit22 of 0x42000
3178 * The bit21 and bit22 of 0x42004
3179 * The bit5 and bit7 of 0x42020
3180 * The bit14 of 0x70180
3181 * The bit14 of 0x71180
3182 */
3183 I915_WRITE(ILK_DISPLAY_CHICKEN1,
3184 I915_READ(ILK_DISPLAY_CHICKEN1) |
3185 ILK_FBCQ_DIS | ILK_PABSTRETCH_DIS);
3186 I915_WRITE(ILK_DISPLAY_CHICKEN2,
3187 I915_READ(ILK_DISPLAY_CHICKEN2) |
3188 ILK_DPARB_GATE | ILK_VSDPFD_FULL);
3189 I915_WRITE(ILK_DSPCLK_GATE,
3190 I915_READ(ILK_DSPCLK_GATE) |
3191 ILK_DPARB_CLK_GATE |
3192 ILK_DPFD_CLK_GATE);
3193
3194 for_each_pipe(pipe) {
3195 I915_WRITE(DSPCNTR(pipe),
3196 I915_READ(DSPCNTR(pipe)) |
3197 DISPPLANE_TRICKLE_FEED_DISABLE);
3198 intel_flush_display_plane(dev_priv, pipe);
3199 }
3200}
3201
3202static void gen7_setup_fixed_func_scheduler(struct drm_i915_private *dev_priv)
3203{
3204 uint32_t reg = I915_READ(GEN7_FF_THREAD_MODE);
3205
3206 reg &= ~GEN7_FF_SCHED_MASK;
3207 reg |= GEN7_FF_TS_SCHED_HW;
3208 reg |= GEN7_FF_VS_SCHED_HW;
3209 reg |= GEN7_FF_DS_SCHED_HW;
3210
3211 I915_WRITE(GEN7_FF_THREAD_MODE, reg);
3212}
3213
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03003214static void ivybridge_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03003215{
3216 struct drm_i915_private *dev_priv = dev->dev_private;
3217 int pipe;
3218 uint32_t dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE;
3219
3220 I915_WRITE(PCH_DSPCLK_GATE_D, dspclk_gate);
3221
3222 I915_WRITE(WM3_LP_ILK, 0);
3223 I915_WRITE(WM2_LP_ILK, 0);
3224 I915_WRITE(WM1_LP_ILK, 0);
3225
3226 /* According to the spec, bit 13 (RCZUNIT) must be set on IVB.
3227 * This implements the WaDisableRCZUnitClockGating workaround.
3228 */
3229 I915_WRITE(GEN6_UCGCTL2, GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
3230
3231 I915_WRITE(ILK_DSPCLK_GATE, IVB_VRHUNIT_CLK_GATE);
3232
3233 I915_WRITE(IVB_CHICKEN3,
3234 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
3235 CHICKEN3_DGMG_DONE_FIX_DISABLE);
3236
3237 /* Apply the WaDisableRHWOOptimizationForRenderHang workaround. */
3238 I915_WRITE(GEN7_COMMON_SLICE_CHICKEN1,
3239 GEN7_CSC1_RHWO_OPT_DISABLE_IN_RCC);
3240
3241 /* WaApplyL3ControlAndL3ChickenMode requires those two on Ivy Bridge */
3242 I915_WRITE(GEN7_L3CNTLREG1,
3243 GEN7_WA_FOR_GEN7_L3_CONTROL);
3244 I915_WRITE(GEN7_L3_CHICKEN_MODE_REGISTER,
3245 GEN7_WA_L3_CHICKEN_MODE);
3246
3247 /* This is required by WaCatErrorRejectionIssue */
3248 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
3249 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
3250 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
3251
3252 for_each_pipe(pipe) {
3253 I915_WRITE(DSPCNTR(pipe),
3254 I915_READ(DSPCNTR(pipe)) |
3255 DISPPLANE_TRICKLE_FEED_DISABLE);
3256 intel_flush_display_plane(dev_priv, pipe);
3257 }
3258
3259 gen7_setup_fixed_func_scheduler(dev_priv);
Daniel Vetter97e19302012-04-24 16:00:21 +02003260
3261 /* WaDisable4x2SubspanOptimization */
3262 I915_WRITE(CACHE_MODE_1,
3263 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03003264}
3265
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03003266static void valleyview_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03003267{
3268 struct drm_i915_private *dev_priv = dev->dev_private;
3269 int pipe;
3270 uint32_t dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE;
3271
3272 I915_WRITE(PCH_DSPCLK_GATE_D, dspclk_gate);
3273
3274 I915_WRITE(WM3_LP_ILK, 0);
3275 I915_WRITE(WM2_LP_ILK, 0);
3276 I915_WRITE(WM1_LP_ILK, 0);
3277
3278 /* According to the spec, bit 13 (RCZUNIT) must be set on IVB.
3279 * This implements the WaDisableRCZUnitClockGating workaround.
3280 */
3281 I915_WRITE(GEN6_UCGCTL2, GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
3282
3283 I915_WRITE(ILK_DSPCLK_GATE, IVB_VRHUNIT_CLK_GATE);
3284
3285 I915_WRITE(IVB_CHICKEN3,
3286 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
3287 CHICKEN3_DGMG_DONE_FIX_DISABLE);
3288
3289 /* Apply the WaDisableRHWOOptimizationForRenderHang workaround. */
3290 I915_WRITE(GEN7_COMMON_SLICE_CHICKEN1,
3291 GEN7_CSC1_RHWO_OPT_DISABLE_IN_RCC);
3292
3293 /* WaApplyL3ControlAndL3ChickenMode requires those two on Ivy Bridge */
3294 I915_WRITE(GEN7_L3CNTLREG1, GEN7_WA_FOR_GEN7_L3_CONTROL);
3295 I915_WRITE(GEN7_L3_CHICKEN_MODE_REGISTER, GEN7_WA_L3_CHICKEN_MODE);
3296
3297 /* This is required by WaCatErrorRejectionIssue */
3298 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
3299 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
3300 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
3301
3302 for_each_pipe(pipe) {
3303 I915_WRITE(DSPCNTR(pipe),
3304 I915_READ(DSPCNTR(pipe)) |
3305 DISPPLANE_TRICKLE_FEED_DISABLE);
3306 intel_flush_display_plane(dev_priv, pipe);
3307 }
3308
Daniel Vetter6b26c862012-04-24 14:04:12 +02003309 I915_WRITE(CACHE_MODE_1,
3310 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03003311}
3312
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03003313static void g4x_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03003314{
3315 struct drm_i915_private *dev_priv = dev->dev_private;
3316 uint32_t dspclk_gate;
3317
3318 I915_WRITE(RENCLK_GATE_D1, 0);
3319 I915_WRITE(RENCLK_GATE_D2, VF_UNIT_CLOCK_GATE_DISABLE |
3320 GS_UNIT_CLOCK_GATE_DISABLE |
3321 CL_UNIT_CLOCK_GATE_DISABLE);
3322 I915_WRITE(RAMCLK_GATE_D, 0);
3323 dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE |
3324 OVRUNIT_CLOCK_GATE_DISABLE |
3325 OVCUNIT_CLOCK_GATE_DISABLE;
3326 if (IS_GM45(dev))
3327 dspclk_gate |= DSSUNIT_CLOCK_GATE_DISABLE;
3328 I915_WRITE(DSPCLK_GATE_D, dspclk_gate);
3329}
3330
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03003331static void crestline_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03003332{
3333 struct drm_i915_private *dev_priv = dev->dev_private;
3334
3335 I915_WRITE(RENCLK_GATE_D1, I965_RCC_CLOCK_GATE_DISABLE);
3336 I915_WRITE(RENCLK_GATE_D2, 0);
3337 I915_WRITE(DSPCLK_GATE_D, 0);
3338 I915_WRITE(RAMCLK_GATE_D, 0);
3339 I915_WRITE16(DEUC, 0);
3340}
3341
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03003342static void broadwater_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03003343{
3344 struct drm_i915_private *dev_priv = dev->dev_private;
3345
3346 I915_WRITE(RENCLK_GATE_D1, I965_RCZ_CLOCK_GATE_DISABLE |
3347 I965_RCC_CLOCK_GATE_DISABLE |
3348 I965_RCPB_CLOCK_GATE_DISABLE |
3349 I965_ISC_CLOCK_GATE_DISABLE |
3350 I965_FBC_CLOCK_GATE_DISABLE);
3351 I915_WRITE(RENCLK_GATE_D2, 0);
3352}
3353
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03003354static void gen3_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03003355{
3356 struct drm_i915_private *dev_priv = dev->dev_private;
3357 u32 dstate = I915_READ(D_STATE);
3358
3359 dstate |= DSTATE_PLL_D3_OFF | DSTATE_GFX_CLOCK_GATING |
3360 DSTATE_DOT_CLOCK_GATING;
3361 I915_WRITE(D_STATE, dstate);
Chris Wilson13a86b82012-04-24 14:51:43 +01003362
3363 if (IS_PINEVIEW(dev))
3364 I915_WRITE(ECOSKPD, _MASKED_BIT_ENABLE(ECO_GATING_CX_ONLY));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03003365}
3366
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03003367static void i85x_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03003368{
3369 struct drm_i915_private *dev_priv = dev->dev_private;
3370
3371 I915_WRITE(RENCLK_GATE_D1, SV_CLOCK_GATE_DISABLE);
3372}
3373
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03003374static void i830_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03003375{
3376 struct drm_i915_private *dev_priv = dev->dev_private;
3377
3378 I915_WRITE(DSPCLK_GATE_D, OVRUNIT_CLOCK_GATE_DISABLE);
3379}
3380
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03003381static void ibx_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03003382{
3383 struct drm_i915_private *dev_priv = dev->dev_private;
3384
3385 /*
3386 * On Ibex Peak and Cougar Point, we need to disable clock
3387 * gating for the panel power sequencer or it will fail to
3388 * start up when no ports are active.
3389 */
3390 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE);
3391}
3392
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03003393static void cpt_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03003394{
3395 struct drm_i915_private *dev_priv = dev->dev_private;
3396 int pipe;
3397
3398 /*
3399 * On Ibex Peak and Cougar Point, we need to disable clock
3400 * gating for the panel power sequencer or it will fail to
3401 * start up when no ports are active.
3402 */
3403 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE);
3404 I915_WRITE(SOUTH_CHICKEN2, I915_READ(SOUTH_CHICKEN2) |
3405 DPLS_EDP_PPS_FIX_DIS);
3406 /* Without this, mode sets may fail silently on FDI */
3407 for_each_pipe(pipe)
3408 I915_WRITE(TRANS_CHICKEN2(pipe), TRANS_AUTOTRAIN_GEN_STALL_DIS);
3409}
3410
3411void intel_init_clock_gating(struct drm_device *dev)
3412{
3413 struct drm_i915_private *dev_priv = dev->dev_private;
3414
3415 dev_priv->display.init_clock_gating(dev);
3416
3417 if (dev_priv->display.init_pch_clock_gating)
3418 dev_priv->display.init_pch_clock_gating(dev);
3419}
3420
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03003421/* Set up chip specific power management-related functions */
3422void intel_init_pm(struct drm_device *dev)
3423{
3424 struct drm_i915_private *dev_priv = dev->dev_private;
3425
3426 if (I915_HAS_FBC(dev)) {
3427 if (HAS_PCH_SPLIT(dev)) {
3428 dev_priv->display.fbc_enabled = ironlake_fbc_enabled;
3429 dev_priv->display.enable_fbc = ironlake_enable_fbc;
3430 dev_priv->display.disable_fbc = ironlake_disable_fbc;
3431 } else if (IS_GM45(dev)) {
3432 dev_priv->display.fbc_enabled = g4x_fbc_enabled;
3433 dev_priv->display.enable_fbc = g4x_enable_fbc;
3434 dev_priv->display.disable_fbc = g4x_disable_fbc;
3435 } else if (IS_CRESTLINE(dev)) {
3436 dev_priv->display.fbc_enabled = i8xx_fbc_enabled;
3437 dev_priv->display.enable_fbc = i8xx_enable_fbc;
3438 dev_priv->display.disable_fbc = i8xx_disable_fbc;
3439 }
3440 /* 855GM needs testing */
3441 }
3442
3443 /* For FIFO watermark updates */
3444 if (HAS_PCH_SPLIT(dev)) {
3445 dev_priv->display.force_wake_get = __gen6_gt_force_wake_get;
3446 dev_priv->display.force_wake_put = __gen6_gt_force_wake_put;
3447
3448 /* IVB configs may use multi-threaded forcewake */
3449 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
3450 u32 ecobus;
3451
3452 /* A small trick here - if the bios hasn't configured MT forcewake,
3453 * and if the device is in RC6, then force_wake_mt_get will not wake
3454 * the device and the ECOBUS read will return zero. Which will be
3455 * (correctly) interpreted by the test below as MT forcewake being
3456 * disabled.
3457 */
3458 mutex_lock(&dev->struct_mutex);
3459 __gen6_gt_force_wake_mt_get(dev_priv);
3460 ecobus = I915_READ_NOTRACE(ECOBUS);
3461 __gen6_gt_force_wake_mt_put(dev_priv);
3462 mutex_unlock(&dev->struct_mutex);
3463
3464 if (ecobus & FORCEWAKE_MT_ENABLE) {
3465 DRM_DEBUG_KMS("Using MT version of forcewake\n");
3466 dev_priv->display.force_wake_get =
3467 __gen6_gt_force_wake_mt_get;
3468 dev_priv->display.force_wake_put =
3469 __gen6_gt_force_wake_mt_put;
3470 }
3471 }
3472
3473 if (HAS_PCH_IBX(dev))
3474 dev_priv->display.init_pch_clock_gating = ibx_init_clock_gating;
3475 else if (HAS_PCH_CPT(dev))
3476 dev_priv->display.init_pch_clock_gating = cpt_init_clock_gating;
3477
3478 if (IS_GEN5(dev)) {
3479 if (I915_READ(MLTR_ILK) & ILK_SRLT_MASK)
3480 dev_priv->display.update_wm = ironlake_update_wm;
3481 else {
3482 DRM_DEBUG_KMS("Failed to get proper latency. "
3483 "Disable CxSR\n");
3484 dev_priv->display.update_wm = NULL;
3485 }
3486 dev_priv->display.init_clock_gating = ironlake_init_clock_gating;
3487 } else if (IS_GEN6(dev)) {
3488 if (SNB_READ_WM0_LATENCY()) {
3489 dev_priv->display.update_wm = sandybridge_update_wm;
3490 dev_priv->display.update_sprite_wm = sandybridge_update_sprite_wm;
3491 } else {
3492 DRM_DEBUG_KMS("Failed to read display plane latency. "
3493 "Disable CxSR\n");
3494 dev_priv->display.update_wm = NULL;
3495 }
3496 dev_priv->display.init_clock_gating = gen6_init_clock_gating;
3497 } else if (IS_IVYBRIDGE(dev)) {
3498 /* FIXME: detect B0+ stepping and use auto training */
3499 if (SNB_READ_WM0_LATENCY()) {
3500 dev_priv->display.update_wm = sandybridge_update_wm;
3501 dev_priv->display.update_sprite_wm = sandybridge_update_sprite_wm;
3502 } else {
3503 DRM_DEBUG_KMS("Failed to read display plane latency. "
3504 "Disable CxSR\n");
3505 dev_priv->display.update_wm = NULL;
3506 }
3507 dev_priv->display.init_clock_gating = ivybridge_init_clock_gating;
3508 } else
3509 dev_priv->display.update_wm = NULL;
3510 } else if (IS_VALLEYVIEW(dev)) {
3511 dev_priv->display.update_wm = valleyview_update_wm;
3512 dev_priv->display.init_clock_gating =
3513 valleyview_init_clock_gating;
3514 dev_priv->display.force_wake_get = vlv_force_wake_get;
3515 dev_priv->display.force_wake_put = vlv_force_wake_put;
3516 } else if (IS_PINEVIEW(dev)) {
3517 if (!intel_get_cxsr_latency(IS_PINEVIEW_G(dev),
3518 dev_priv->is_ddr3,
3519 dev_priv->fsb_freq,
3520 dev_priv->mem_freq)) {
3521 DRM_INFO("failed to find known CxSR latency "
3522 "(found ddr%s fsb freq %d, mem freq %d), "
3523 "disabling CxSR\n",
3524 (dev_priv->is_ddr3 == 1) ? "3" : "2",
3525 dev_priv->fsb_freq, dev_priv->mem_freq);
3526 /* Disable CxSR and never update its watermark again */
3527 pineview_disable_cxsr(dev);
3528 dev_priv->display.update_wm = NULL;
3529 } else
3530 dev_priv->display.update_wm = pineview_update_wm;
3531 dev_priv->display.init_clock_gating = gen3_init_clock_gating;
3532 } else if (IS_G4X(dev)) {
3533 dev_priv->display.update_wm = g4x_update_wm;
3534 dev_priv->display.init_clock_gating = g4x_init_clock_gating;
3535 } else if (IS_GEN4(dev)) {
3536 dev_priv->display.update_wm = i965_update_wm;
3537 if (IS_CRESTLINE(dev))
3538 dev_priv->display.init_clock_gating = crestline_init_clock_gating;
3539 else if (IS_BROADWATER(dev))
3540 dev_priv->display.init_clock_gating = broadwater_init_clock_gating;
3541 } else if (IS_GEN3(dev)) {
3542 dev_priv->display.update_wm = i9xx_update_wm;
3543 dev_priv->display.get_fifo_size = i9xx_get_fifo_size;
3544 dev_priv->display.init_clock_gating = gen3_init_clock_gating;
3545 } else if (IS_I865G(dev)) {
3546 dev_priv->display.update_wm = i830_update_wm;
3547 dev_priv->display.init_clock_gating = i85x_init_clock_gating;
3548 dev_priv->display.get_fifo_size = i830_get_fifo_size;
3549 } else if (IS_I85X(dev)) {
3550 dev_priv->display.update_wm = i9xx_update_wm;
3551 dev_priv->display.get_fifo_size = i85x_get_fifo_size;
3552 dev_priv->display.init_clock_gating = i85x_init_clock_gating;
3553 } else {
3554 dev_priv->display.update_wm = i830_update_wm;
3555 dev_priv->display.init_clock_gating = i830_init_clock_gating;
3556 if (IS_845G(dev))
3557 dev_priv->display.get_fifo_size = i845_get_fifo_size;
3558 else
3559 dev_priv->display.get_fifo_size = i830_get_fifo_size;
3560 }
3561}
3562