blob: c5bc4c456baa5a2d84165efeb0be9b76aa9e2f06 [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
28#include "i915_drv.h"
29#include "intel_drv.h"
30
Eugeni Dodonovf6750b32012-04-18 11:51:14 -030031/* FBC, or Frame Buffer Compression, is a technique employed to compress the
32 * framebuffer contents in-memory, aiming at reducing the required bandwidth
33 * during in-memory transfers and, therefore, reduce the power packet.
Eugeni Dodonov85208be2012-04-16 22:20:34 -030034 *
Eugeni Dodonovf6750b32012-04-18 11:51:14 -030035 * The benefits of FBC are mostly visible with solid backgrounds and
36 * variation-less patterns.
Eugeni Dodonov85208be2012-04-16 22:20:34 -030037 *
Eugeni Dodonovf6750b32012-04-18 11:51:14 -030038 * FBC-related functionality can be enabled by the means of the
39 * i915.i915_enable_fbc parameter
Eugeni Dodonov85208be2012-04-16 22:20:34 -030040 */
41
42void i8xx_disable_fbc(struct drm_device *dev)
43{
44 struct drm_i915_private *dev_priv = dev->dev_private;
45 u32 fbc_ctl;
46
47 /* Disable compression */
48 fbc_ctl = I915_READ(FBC_CONTROL);
49 if ((fbc_ctl & FBC_CTL_EN) == 0)
50 return;
51
52 fbc_ctl &= ~FBC_CTL_EN;
53 I915_WRITE(FBC_CONTROL, fbc_ctl);
54
55 /* Wait for compressing bit to clear */
56 if (wait_for((I915_READ(FBC_STATUS) & FBC_STAT_COMPRESSING) == 0, 10)) {
57 DRM_DEBUG_KMS("FBC idle timed out\n");
58 return;
59 }
60
61 DRM_DEBUG_KMS("disabled FBC\n");
62}
63
64void i8xx_enable_fbc(struct drm_crtc *crtc, unsigned long interval)
65{
66 struct drm_device *dev = crtc->dev;
67 struct drm_i915_private *dev_priv = dev->dev_private;
68 struct drm_framebuffer *fb = crtc->fb;
69 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
70 struct drm_i915_gem_object *obj = intel_fb->obj;
71 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
72 int cfb_pitch;
73 int plane, i;
74 u32 fbc_ctl, fbc_ctl2;
75
76 cfb_pitch = dev_priv->cfb_size / FBC_LL_SIZE;
77 if (fb->pitches[0] < cfb_pitch)
78 cfb_pitch = fb->pitches[0];
79
80 /* FBC_CTL wants 64B units */
81 cfb_pitch = (cfb_pitch / 64) - 1;
82 plane = intel_crtc->plane == 0 ? FBC_CTL_PLANEA : FBC_CTL_PLANEB;
83
84 /* Clear old tags */
85 for (i = 0; i < (FBC_LL_SIZE / 32) + 1; i++)
86 I915_WRITE(FBC_TAG + (i * 4), 0);
87
88 /* Set it up... */
89 fbc_ctl2 = FBC_CTL_FENCE_DBL | FBC_CTL_IDLE_IMM | FBC_CTL_CPU_FENCE;
90 fbc_ctl2 |= plane;
91 I915_WRITE(FBC_CONTROL2, fbc_ctl2);
92 I915_WRITE(FBC_FENCE_OFF, crtc->y);
93
94 /* enable it... */
95 fbc_ctl = FBC_CTL_EN | FBC_CTL_PERIODIC;
96 if (IS_I945GM(dev))
97 fbc_ctl |= FBC_CTL_C3_IDLE; /* 945 needs special SR handling */
98 fbc_ctl |= (cfb_pitch & 0xff) << FBC_CTL_STRIDE_SHIFT;
99 fbc_ctl |= (interval & 0x2fff) << FBC_CTL_INTERVAL_SHIFT;
100 fbc_ctl |= obj->fence_reg;
101 I915_WRITE(FBC_CONTROL, fbc_ctl);
102
103 DRM_DEBUG_KMS("enabled FBC, pitch %d, yoff %d, plane %d, ",
104 cfb_pitch, crtc->y, intel_crtc->plane);
105}
106
107bool i8xx_fbc_enabled(struct drm_device *dev)
108{
109 struct drm_i915_private *dev_priv = dev->dev_private;
110
111 return I915_READ(FBC_CONTROL) & FBC_CTL_EN;
112}
113
114void g4x_enable_fbc(struct drm_crtc *crtc, unsigned long interval)
115{
116 struct drm_device *dev = crtc->dev;
117 struct drm_i915_private *dev_priv = dev->dev_private;
118 struct drm_framebuffer *fb = crtc->fb;
119 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
120 struct drm_i915_gem_object *obj = intel_fb->obj;
121 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
122 int plane = intel_crtc->plane == 0 ? DPFC_CTL_PLANEA : DPFC_CTL_PLANEB;
123 unsigned long stall_watermark = 200;
124 u32 dpfc_ctl;
125
126 dpfc_ctl = plane | DPFC_SR_EN | DPFC_CTL_LIMIT_1X;
127 dpfc_ctl |= DPFC_CTL_FENCE_EN | obj->fence_reg;
128 I915_WRITE(DPFC_CHICKEN, DPFC_HT_MODIFY);
129
130 I915_WRITE(DPFC_RECOMP_CTL, DPFC_RECOMP_STALL_EN |
131 (stall_watermark << DPFC_RECOMP_STALL_WM_SHIFT) |
132 (interval << DPFC_RECOMP_TIMER_COUNT_SHIFT));
133 I915_WRITE(DPFC_FENCE_YOFF, crtc->y);
134
135 /* enable it... */
136 I915_WRITE(DPFC_CONTROL, I915_READ(DPFC_CONTROL) | DPFC_CTL_EN);
137
138 DRM_DEBUG_KMS("enabled fbc on plane %d\n", intel_crtc->plane);
139}
140
141void g4x_disable_fbc(struct drm_device *dev)
142{
143 struct drm_i915_private *dev_priv = dev->dev_private;
144 u32 dpfc_ctl;
145
146 /* Disable compression */
147 dpfc_ctl = I915_READ(DPFC_CONTROL);
148 if (dpfc_ctl & DPFC_CTL_EN) {
149 dpfc_ctl &= ~DPFC_CTL_EN;
150 I915_WRITE(DPFC_CONTROL, dpfc_ctl);
151
152 DRM_DEBUG_KMS("disabled FBC\n");
153 }
154}
155
156bool g4x_fbc_enabled(struct drm_device *dev)
157{
158 struct drm_i915_private *dev_priv = dev->dev_private;
159
160 return I915_READ(DPFC_CONTROL) & DPFC_CTL_EN;
161}
162
163static void sandybridge_blit_fbc_update(struct drm_device *dev)
164{
165 struct drm_i915_private *dev_priv = dev->dev_private;
166 u32 blt_ecoskpd;
167
168 /* Make sure blitter notifies FBC of writes */
169 gen6_gt_force_wake_get(dev_priv);
170 blt_ecoskpd = I915_READ(GEN6_BLITTER_ECOSKPD);
171 blt_ecoskpd |= GEN6_BLITTER_FBC_NOTIFY <<
172 GEN6_BLITTER_LOCK_SHIFT;
173 I915_WRITE(GEN6_BLITTER_ECOSKPD, blt_ecoskpd);
174 blt_ecoskpd |= GEN6_BLITTER_FBC_NOTIFY;
175 I915_WRITE(GEN6_BLITTER_ECOSKPD, blt_ecoskpd);
176 blt_ecoskpd &= ~(GEN6_BLITTER_FBC_NOTIFY <<
177 GEN6_BLITTER_LOCK_SHIFT);
178 I915_WRITE(GEN6_BLITTER_ECOSKPD, blt_ecoskpd);
179 POSTING_READ(GEN6_BLITTER_ECOSKPD);
180 gen6_gt_force_wake_put(dev_priv);
181}
182
183void ironlake_enable_fbc(struct drm_crtc *crtc, unsigned long interval)
184{
185 struct drm_device *dev = crtc->dev;
186 struct drm_i915_private *dev_priv = dev->dev_private;
187 struct drm_framebuffer *fb = crtc->fb;
188 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
189 struct drm_i915_gem_object *obj = intel_fb->obj;
190 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
191 int plane = intel_crtc->plane == 0 ? DPFC_CTL_PLANEA : DPFC_CTL_PLANEB;
192 unsigned long stall_watermark = 200;
193 u32 dpfc_ctl;
194
195 dpfc_ctl = I915_READ(ILK_DPFC_CONTROL);
196 dpfc_ctl &= DPFC_RESERVED;
197 dpfc_ctl |= (plane | DPFC_CTL_LIMIT_1X);
198 /* Set persistent mode for front-buffer rendering, ala X. */
199 dpfc_ctl |= DPFC_CTL_PERSISTENT_MODE;
200 dpfc_ctl |= (DPFC_CTL_FENCE_EN | obj->fence_reg);
201 I915_WRITE(ILK_DPFC_CHICKEN, DPFC_HT_MODIFY);
202
203 I915_WRITE(ILK_DPFC_RECOMP_CTL, DPFC_RECOMP_STALL_EN |
204 (stall_watermark << DPFC_RECOMP_STALL_WM_SHIFT) |
205 (interval << DPFC_RECOMP_TIMER_COUNT_SHIFT));
206 I915_WRITE(ILK_DPFC_FENCE_YOFF, crtc->y);
207 I915_WRITE(ILK_FBC_RT_BASE, obj->gtt_offset | ILK_FBC_RT_VALID);
208 /* enable it... */
209 I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl | DPFC_CTL_EN);
210
211 if (IS_GEN6(dev)) {
212 I915_WRITE(SNB_DPFC_CTL_SA,
213 SNB_CPU_FENCE_ENABLE | obj->fence_reg);
214 I915_WRITE(DPFC_CPU_FENCE_OFFSET, crtc->y);
215 sandybridge_blit_fbc_update(dev);
216 }
217
218 DRM_DEBUG_KMS("enabled fbc on plane %d\n", intel_crtc->plane);
219}
220
221void ironlake_disable_fbc(struct drm_device *dev)
222{
223 struct drm_i915_private *dev_priv = dev->dev_private;
224 u32 dpfc_ctl;
225
226 /* Disable compression */
227 dpfc_ctl = I915_READ(ILK_DPFC_CONTROL);
228 if (dpfc_ctl & DPFC_CTL_EN) {
229 dpfc_ctl &= ~DPFC_CTL_EN;
230 I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl);
231
232 DRM_DEBUG_KMS("disabled FBC\n");
233 }
234}
235
236bool ironlake_fbc_enabled(struct drm_device *dev)
237{
238 struct drm_i915_private *dev_priv = dev->dev_private;
239
240 return I915_READ(ILK_DPFC_CONTROL) & DPFC_CTL_EN;
241}
242
243bool intel_fbc_enabled(struct drm_device *dev)
244{
245 struct drm_i915_private *dev_priv = dev->dev_private;
246
247 if (!dev_priv->display.fbc_enabled)
248 return false;
249
250 return dev_priv->display.fbc_enabled(dev);
251}
252
253static void intel_fbc_work_fn(struct work_struct *__work)
254{
255 struct intel_fbc_work *work =
256 container_of(to_delayed_work(__work),
257 struct intel_fbc_work, work);
258 struct drm_device *dev = work->crtc->dev;
259 struct drm_i915_private *dev_priv = dev->dev_private;
260
261 mutex_lock(&dev->struct_mutex);
262 if (work == dev_priv->fbc_work) {
263 /* Double check that we haven't switched fb without cancelling
264 * the prior work.
265 */
266 if (work->crtc->fb == work->fb) {
267 dev_priv->display.enable_fbc(work->crtc,
268 work->interval);
269
270 dev_priv->cfb_plane = to_intel_crtc(work->crtc)->plane;
271 dev_priv->cfb_fb = work->crtc->fb->base.id;
272 dev_priv->cfb_y = work->crtc->y;
273 }
274
275 dev_priv->fbc_work = NULL;
276 }
277 mutex_unlock(&dev->struct_mutex);
278
279 kfree(work);
280}
281
282static void intel_cancel_fbc_work(struct drm_i915_private *dev_priv)
283{
284 if (dev_priv->fbc_work == NULL)
285 return;
286
287 DRM_DEBUG_KMS("cancelling pending FBC enable\n");
288
289 /* Synchronisation is provided by struct_mutex and checking of
290 * dev_priv->fbc_work, so we can perform the cancellation
291 * entirely asynchronously.
292 */
293 if (cancel_delayed_work(&dev_priv->fbc_work->work))
294 /* tasklet was killed before being run, clean up */
295 kfree(dev_priv->fbc_work);
296
297 /* Mark the work as no longer wanted so that if it does
298 * wake-up (because the work was already running and waiting
299 * for our mutex), it will discover that is no longer
300 * necessary to run.
301 */
302 dev_priv->fbc_work = NULL;
303}
304
305void intel_enable_fbc(struct drm_crtc *crtc, unsigned long interval)
306{
307 struct intel_fbc_work *work;
308 struct drm_device *dev = crtc->dev;
309 struct drm_i915_private *dev_priv = dev->dev_private;
310
311 if (!dev_priv->display.enable_fbc)
312 return;
313
314 intel_cancel_fbc_work(dev_priv);
315
316 work = kzalloc(sizeof *work, GFP_KERNEL);
317 if (work == NULL) {
318 dev_priv->display.enable_fbc(crtc, interval);
319 return;
320 }
321
322 work->crtc = crtc;
323 work->fb = crtc->fb;
324 work->interval = interval;
325 INIT_DELAYED_WORK(&work->work, intel_fbc_work_fn);
326
327 dev_priv->fbc_work = work;
328
329 DRM_DEBUG_KMS("scheduling delayed FBC enable\n");
330
331 /* Delay the actual enabling to let pageflipping cease and the
332 * display to settle before starting the compression. Note that
333 * this delay also serves a second purpose: it allows for a
334 * vblank to pass after disabling the FBC before we attempt
335 * to modify the control registers.
336 *
337 * A more complicated solution would involve tracking vblanks
338 * following the termination of the page-flipping sequence
339 * and indeed performing the enable as a co-routine and not
340 * waiting synchronously upon the vblank.
341 */
342 schedule_delayed_work(&work->work, msecs_to_jiffies(50));
343}
344
345void intel_disable_fbc(struct drm_device *dev)
346{
347 struct drm_i915_private *dev_priv = dev->dev_private;
348
349 intel_cancel_fbc_work(dev_priv);
350
351 if (!dev_priv->display.disable_fbc)
352 return;
353
354 dev_priv->display.disable_fbc(dev);
355 dev_priv->cfb_plane = -1;
356}
357
358/**
359 * intel_update_fbc - enable/disable FBC as needed
360 * @dev: the drm_device
361 *
362 * Set up the framebuffer compression hardware at mode set time. We
363 * enable it if possible:
364 * - plane A only (on pre-965)
365 * - no pixel mulitply/line duplication
366 * - no alpha buffer discard
367 * - no dual wide
368 * - framebuffer <= 2048 in width, 1536 in height
369 *
370 * We can't assume that any compression will take place (worst case),
371 * so the compressed buffer has to be the same size as the uncompressed
372 * one. It also must reside (along with the line length buffer) in
373 * stolen memory.
374 *
375 * We need to enable/disable FBC on a global basis.
376 */
377void intel_update_fbc(struct drm_device *dev)
378{
379 struct drm_i915_private *dev_priv = dev->dev_private;
380 struct drm_crtc *crtc = NULL, *tmp_crtc;
381 struct intel_crtc *intel_crtc;
382 struct drm_framebuffer *fb;
383 struct intel_framebuffer *intel_fb;
384 struct drm_i915_gem_object *obj;
385 int enable_fbc;
386
387 DRM_DEBUG_KMS("\n");
388
389 if (!i915_powersave)
390 return;
391
392 if (!I915_HAS_FBC(dev))
393 return;
394
395 /*
396 * If FBC is already on, we just have to verify that we can
397 * keep it that way...
398 * Need to disable if:
399 * - more than one pipe is active
400 * - changing FBC params (stride, fence, mode)
401 * - new fb is too large to fit in compressed buffer
402 * - going to an unsupported config (interlace, pixel multiply, etc.)
403 */
404 list_for_each_entry(tmp_crtc, &dev->mode_config.crtc_list, head) {
405 if (tmp_crtc->enabled && tmp_crtc->fb) {
406 if (crtc) {
407 DRM_DEBUG_KMS("more than one pipe active, disabling compression\n");
408 dev_priv->no_fbc_reason = FBC_MULTIPLE_PIPES;
409 goto out_disable;
410 }
411 crtc = tmp_crtc;
412 }
413 }
414
415 if (!crtc || crtc->fb == NULL) {
416 DRM_DEBUG_KMS("no output, disabling\n");
417 dev_priv->no_fbc_reason = FBC_NO_OUTPUT;
418 goto out_disable;
419 }
420
421 intel_crtc = to_intel_crtc(crtc);
422 fb = crtc->fb;
423 intel_fb = to_intel_framebuffer(fb);
424 obj = intel_fb->obj;
425
426 enable_fbc = i915_enable_fbc;
427 if (enable_fbc < 0) {
428 DRM_DEBUG_KMS("fbc set to per-chip default\n");
429 enable_fbc = 1;
430 if (INTEL_INFO(dev)->gen <= 6)
431 enable_fbc = 0;
432 }
433 if (!enable_fbc) {
434 DRM_DEBUG_KMS("fbc disabled per module param\n");
435 dev_priv->no_fbc_reason = FBC_MODULE_PARAM;
436 goto out_disable;
437 }
438 if (intel_fb->obj->base.size > dev_priv->cfb_size) {
439 DRM_DEBUG_KMS("framebuffer too large, disabling "
440 "compression\n");
441 dev_priv->no_fbc_reason = FBC_STOLEN_TOO_SMALL;
442 goto out_disable;
443 }
444 if ((crtc->mode.flags & DRM_MODE_FLAG_INTERLACE) ||
445 (crtc->mode.flags & DRM_MODE_FLAG_DBLSCAN)) {
446 DRM_DEBUG_KMS("mode incompatible with compression, "
447 "disabling\n");
448 dev_priv->no_fbc_reason = FBC_UNSUPPORTED_MODE;
449 goto out_disable;
450 }
451 if ((crtc->mode.hdisplay > 2048) ||
452 (crtc->mode.vdisplay > 1536)) {
453 DRM_DEBUG_KMS("mode too large for compression, disabling\n");
454 dev_priv->no_fbc_reason = FBC_MODE_TOO_LARGE;
455 goto out_disable;
456 }
457 if ((IS_I915GM(dev) || IS_I945GM(dev)) && intel_crtc->plane != 0) {
458 DRM_DEBUG_KMS("plane not 0, disabling compression\n");
459 dev_priv->no_fbc_reason = FBC_BAD_PLANE;
460 goto out_disable;
461 }
462
463 /* The use of a CPU fence is mandatory in order to detect writes
464 * by the CPU to the scanout and trigger updates to the FBC.
465 */
466 if (obj->tiling_mode != I915_TILING_X ||
467 obj->fence_reg == I915_FENCE_REG_NONE) {
468 DRM_DEBUG_KMS("framebuffer not tiled or fenced, disabling compression\n");
469 dev_priv->no_fbc_reason = FBC_NOT_TILED;
470 goto out_disable;
471 }
472
473 /* If the kernel debugger is active, always disable compression */
474 if (in_dbg_master())
475 goto out_disable;
476
477 /* If the scanout has not changed, don't modify the FBC settings.
478 * Note that we make the fundamental assumption that the fb->obj
479 * cannot be unpinned (and have its GTT offset and fence revoked)
480 * without first being decoupled from the scanout and FBC disabled.
481 */
482 if (dev_priv->cfb_plane == intel_crtc->plane &&
483 dev_priv->cfb_fb == fb->base.id &&
484 dev_priv->cfb_y == crtc->y)
485 return;
486
487 if (intel_fbc_enabled(dev)) {
488 /* We update FBC along two paths, after changing fb/crtc
489 * configuration (modeswitching) and after page-flipping
490 * finishes. For the latter, we know that not only did
491 * we disable the FBC at the start of the page-flip
492 * sequence, but also more than one vblank has passed.
493 *
494 * For the former case of modeswitching, it is possible
495 * to switch between two FBC valid configurations
496 * instantaneously so we do need to disable the FBC
497 * before we can modify its control registers. We also
498 * have to wait for the next vblank for that to take
499 * effect. However, since we delay enabling FBC we can
500 * assume that a vblank has passed since disabling and
501 * that we can safely alter the registers in the deferred
502 * callback.
503 *
504 * In the scenario that we go from a valid to invalid
505 * and then back to valid FBC configuration we have
506 * no strict enforcement that a vblank occurred since
507 * disabling the FBC. However, along all current pipe
508 * disabling paths we do need to wait for a vblank at
509 * some point. And we wait before enabling FBC anyway.
510 */
511 DRM_DEBUG_KMS("disabling active FBC for update\n");
512 intel_disable_fbc(dev);
513 }
514
515 intel_enable_fbc(crtc, 500);
516 return;
517
518out_disable:
519 /* Multiple disables should be harmless */
520 if (intel_fbc_enabled(dev)) {
521 DRM_DEBUG_KMS("unsupported config, disabling FBC\n");
522 intel_disable_fbc(dev);
523 }
524}
525
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300526static const struct cxsr_latency cxsr_latency_table[] = {
527 {1, 0, 800, 400, 3382, 33382, 3983, 33983}, /* DDR2-400 SC */
528 {1, 0, 800, 667, 3354, 33354, 3807, 33807}, /* DDR2-667 SC */
529 {1, 0, 800, 800, 3347, 33347, 3763, 33763}, /* DDR2-800 SC */
530 {1, 1, 800, 667, 6420, 36420, 6873, 36873}, /* DDR3-667 SC */
531 {1, 1, 800, 800, 5902, 35902, 6318, 36318}, /* DDR3-800 SC */
532
533 {1, 0, 667, 400, 3400, 33400, 4021, 34021}, /* DDR2-400 SC */
534 {1, 0, 667, 667, 3372, 33372, 3845, 33845}, /* DDR2-667 SC */
535 {1, 0, 667, 800, 3386, 33386, 3822, 33822}, /* DDR2-800 SC */
536 {1, 1, 667, 667, 6438, 36438, 6911, 36911}, /* DDR3-667 SC */
537 {1, 1, 667, 800, 5941, 35941, 6377, 36377}, /* DDR3-800 SC */
538
539 {1, 0, 400, 400, 3472, 33472, 4173, 34173}, /* DDR2-400 SC */
540 {1, 0, 400, 667, 3443, 33443, 3996, 33996}, /* DDR2-667 SC */
541 {1, 0, 400, 800, 3430, 33430, 3946, 33946}, /* DDR2-800 SC */
542 {1, 1, 400, 667, 6509, 36509, 7062, 37062}, /* DDR3-667 SC */
543 {1, 1, 400, 800, 5985, 35985, 6501, 36501}, /* DDR3-800 SC */
544
545 {0, 0, 800, 400, 3438, 33438, 4065, 34065}, /* DDR2-400 SC */
546 {0, 0, 800, 667, 3410, 33410, 3889, 33889}, /* DDR2-667 SC */
547 {0, 0, 800, 800, 3403, 33403, 3845, 33845}, /* DDR2-800 SC */
548 {0, 1, 800, 667, 6476, 36476, 6955, 36955}, /* DDR3-667 SC */
549 {0, 1, 800, 800, 5958, 35958, 6400, 36400}, /* DDR3-800 SC */
550
551 {0, 0, 667, 400, 3456, 33456, 4103, 34106}, /* DDR2-400 SC */
552 {0, 0, 667, 667, 3428, 33428, 3927, 33927}, /* DDR2-667 SC */
553 {0, 0, 667, 800, 3443, 33443, 3905, 33905}, /* DDR2-800 SC */
554 {0, 1, 667, 667, 6494, 36494, 6993, 36993}, /* DDR3-667 SC */
555 {0, 1, 667, 800, 5998, 35998, 6460, 36460}, /* DDR3-800 SC */
556
557 {0, 0, 400, 400, 3528, 33528, 4255, 34255}, /* DDR2-400 SC */
558 {0, 0, 400, 667, 3500, 33500, 4079, 34079}, /* DDR2-667 SC */
559 {0, 0, 400, 800, 3487, 33487, 4029, 34029}, /* DDR2-800 SC */
560 {0, 1, 400, 667, 6566, 36566, 7145, 37145}, /* DDR3-667 SC */
561 {0, 1, 400, 800, 6042, 36042, 6584, 36584}, /* DDR3-800 SC */
562};
563
564const struct cxsr_latency *intel_get_cxsr_latency(int is_desktop,
565 int is_ddr3,
566 int fsb,
567 int mem)
568{
569 const struct cxsr_latency *latency;
570 int i;
571
572 if (fsb == 0 || mem == 0)
573 return NULL;
574
575 for (i = 0; i < ARRAY_SIZE(cxsr_latency_table); i++) {
576 latency = &cxsr_latency_table[i];
577 if (is_desktop == latency->is_desktop &&
578 is_ddr3 == latency->is_ddr3 &&
579 fsb == latency->fsb_freq && mem == latency->mem_freq)
580 return latency;
581 }
582
583 DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
584
585 return NULL;
586}
587
588void pineview_disable_cxsr(struct drm_device *dev)
589{
590 struct drm_i915_private *dev_priv = dev->dev_private;
591
592 /* deactivate cxsr */
593 I915_WRITE(DSPFW3, I915_READ(DSPFW3) & ~PINEVIEW_SELF_REFRESH_EN);
594}
595
596/*
597 * Latency for FIFO fetches is dependent on several factors:
598 * - memory configuration (speed, channels)
599 * - chipset
600 * - current MCH state
601 * It can be fairly high in some situations, so here we assume a fairly
602 * pessimal value. It's a tradeoff between extra memory fetches (if we
603 * set this value too high, the FIFO will fetch frequently to stay full)
604 * and power consumption (set it too low to save power and we might see
605 * FIFO underruns and display "flicker").
606 *
607 * A value of 5us seems to be a good balance; safe for very low end
608 * platforms but not overly aggressive on lower latency configs.
609 */
610static const int latency_ns = 5000;
611
612int i9xx_get_fifo_size(struct drm_device *dev, int plane)
613{
614 struct drm_i915_private *dev_priv = dev->dev_private;
615 uint32_t dsparb = I915_READ(DSPARB);
616 int size;
617
618 size = dsparb & 0x7f;
619 if (plane)
620 size = ((dsparb >> DSPARB_CSTART_SHIFT) & 0x7f) - size;
621
622 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
623 plane ? "B" : "A", size);
624
625 return size;
626}
627
628int i85x_get_fifo_size(struct drm_device *dev, int plane)
629{
630 struct drm_i915_private *dev_priv = dev->dev_private;
631 uint32_t dsparb = I915_READ(DSPARB);
632 int size;
633
634 size = dsparb & 0x1ff;
635 if (plane)
636 size = ((dsparb >> DSPARB_BEND_SHIFT) & 0x1ff) - size;
637 size >>= 1; /* Convert to cachelines */
638
639 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
640 plane ? "B" : "A", size);
641
642 return size;
643}
644
645int i845_get_fifo_size(struct drm_device *dev, int plane)
646{
647 struct drm_i915_private *dev_priv = dev->dev_private;
648 uint32_t dsparb = I915_READ(DSPARB);
649 int size;
650
651 size = dsparb & 0x7f;
652 size >>= 2; /* Convert to cachelines */
653
654 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
655 plane ? "B" : "A",
656 size);
657
658 return size;
659}
660
661int i830_get_fifo_size(struct drm_device *dev, int plane)
662{
663 struct drm_i915_private *dev_priv = dev->dev_private;
664 uint32_t dsparb = I915_READ(DSPARB);
665 int size;
666
667 size = dsparb & 0x7f;
668 size >>= 1; /* Convert to cachelines */
669
670 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
671 plane ? "B" : "A", size);
672
673 return size;
674}
675
676/* Pineview has different values for various configs */
677static const struct intel_watermark_params pineview_display_wm = {
678 PINEVIEW_DISPLAY_FIFO,
679 PINEVIEW_MAX_WM,
680 PINEVIEW_DFT_WM,
681 PINEVIEW_GUARD_WM,
682 PINEVIEW_FIFO_LINE_SIZE
683};
684static const struct intel_watermark_params pineview_display_hplloff_wm = {
685 PINEVIEW_DISPLAY_FIFO,
686 PINEVIEW_MAX_WM,
687 PINEVIEW_DFT_HPLLOFF_WM,
688 PINEVIEW_GUARD_WM,
689 PINEVIEW_FIFO_LINE_SIZE
690};
691static const struct intel_watermark_params pineview_cursor_wm = {
692 PINEVIEW_CURSOR_FIFO,
693 PINEVIEW_CURSOR_MAX_WM,
694 PINEVIEW_CURSOR_DFT_WM,
695 PINEVIEW_CURSOR_GUARD_WM,
696 PINEVIEW_FIFO_LINE_SIZE,
697};
698static const struct intel_watermark_params pineview_cursor_hplloff_wm = {
699 PINEVIEW_CURSOR_FIFO,
700 PINEVIEW_CURSOR_MAX_WM,
701 PINEVIEW_CURSOR_DFT_WM,
702 PINEVIEW_CURSOR_GUARD_WM,
703 PINEVIEW_FIFO_LINE_SIZE
704};
705static const struct intel_watermark_params g4x_wm_info = {
706 G4X_FIFO_SIZE,
707 G4X_MAX_WM,
708 G4X_MAX_WM,
709 2,
710 G4X_FIFO_LINE_SIZE,
711};
712static const struct intel_watermark_params g4x_cursor_wm_info = {
713 I965_CURSOR_FIFO,
714 I965_CURSOR_MAX_WM,
715 I965_CURSOR_DFT_WM,
716 2,
717 G4X_FIFO_LINE_SIZE,
718};
719static const struct intel_watermark_params valleyview_wm_info = {
720 VALLEYVIEW_FIFO_SIZE,
721 VALLEYVIEW_MAX_WM,
722 VALLEYVIEW_MAX_WM,
723 2,
724 G4X_FIFO_LINE_SIZE,
725};
726static const struct intel_watermark_params valleyview_cursor_wm_info = {
727 I965_CURSOR_FIFO,
728 VALLEYVIEW_CURSOR_MAX_WM,
729 I965_CURSOR_DFT_WM,
730 2,
731 G4X_FIFO_LINE_SIZE,
732};
733static const struct intel_watermark_params i965_cursor_wm_info = {
734 I965_CURSOR_FIFO,
735 I965_CURSOR_MAX_WM,
736 I965_CURSOR_DFT_WM,
737 2,
738 I915_FIFO_LINE_SIZE,
739};
740static const struct intel_watermark_params i945_wm_info = {
741 I945_FIFO_SIZE,
742 I915_MAX_WM,
743 1,
744 2,
745 I915_FIFO_LINE_SIZE
746};
747static const struct intel_watermark_params i915_wm_info = {
748 I915_FIFO_SIZE,
749 I915_MAX_WM,
750 1,
751 2,
752 I915_FIFO_LINE_SIZE
753};
754static const struct intel_watermark_params i855_wm_info = {
755 I855GM_FIFO_SIZE,
756 I915_MAX_WM,
757 1,
758 2,
759 I830_FIFO_LINE_SIZE
760};
761static const struct intel_watermark_params i830_wm_info = {
762 I830_FIFO_SIZE,
763 I915_MAX_WM,
764 1,
765 2,
766 I830_FIFO_LINE_SIZE
767};
768
769static const struct intel_watermark_params ironlake_display_wm_info = {
770 ILK_DISPLAY_FIFO,
771 ILK_DISPLAY_MAXWM,
772 ILK_DISPLAY_DFTWM,
773 2,
774 ILK_FIFO_LINE_SIZE
775};
776static const struct intel_watermark_params ironlake_cursor_wm_info = {
777 ILK_CURSOR_FIFO,
778 ILK_CURSOR_MAXWM,
779 ILK_CURSOR_DFTWM,
780 2,
781 ILK_FIFO_LINE_SIZE
782};
783static const struct intel_watermark_params ironlake_display_srwm_info = {
784 ILK_DISPLAY_SR_FIFO,
785 ILK_DISPLAY_MAX_SRWM,
786 ILK_DISPLAY_DFT_SRWM,
787 2,
788 ILK_FIFO_LINE_SIZE
789};
790static const struct intel_watermark_params ironlake_cursor_srwm_info = {
791 ILK_CURSOR_SR_FIFO,
792 ILK_CURSOR_MAX_SRWM,
793 ILK_CURSOR_DFT_SRWM,
794 2,
795 ILK_FIFO_LINE_SIZE
796};
797
798static const struct intel_watermark_params sandybridge_display_wm_info = {
799 SNB_DISPLAY_FIFO,
800 SNB_DISPLAY_MAXWM,
801 SNB_DISPLAY_DFTWM,
802 2,
803 SNB_FIFO_LINE_SIZE
804};
805static const struct intel_watermark_params sandybridge_cursor_wm_info = {
806 SNB_CURSOR_FIFO,
807 SNB_CURSOR_MAXWM,
808 SNB_CURSOR_DFTWM,
809 2,
810 SNB_FIFO_LINE_SIZE
811};
812static const struct intel_watermark_params sandybridge_display_srwm_info = {
813 SNB_DISPLAY_SR_FIFO,
814 SNB_DISPLAY_MAX_SRWM,
815 SNB_DISPLAY_DFT_SRWM,
816 2,
817 SNB_FIFO_LINE_SIZE
818};
819static const struct intel_watermark_params sandybridge_cursor_srwm_info = {
820 SNB_CURSOR_SR_FIFO,
821 SNB_CURSOR_MAX_SRWM,
822 SNB_CURSOR_DFT_SRWM,
823 2,
824 SNB_FIFO_LINE_SIZE
825};
826
827
828/**
829 * intel_calculate_wm - calculate watermark level
830 * @clock_in_khz: pixel clock
831 * @wm: chip FIFO params
832 * @pixel_size: display pixel size
833 * @latency_ns: memory latency for the platform
834 *
835 * Calculate the watermark level (the level at which the display plane will
836 * start fetching from memory again). Each chip has a different display
837 * FIFO size and allocation, so the caller needs to figure that out and pass
838 * in the correct intel_watermark_params structure.
839 *
840 * As the pixel clock runs, the FIFO will be drained at a rate that depends
841 * on the pixel size. When it reaches the watermark level, it'll start
842 * fetching FIFO line sized based chunks from memory until the FIFO fills
843 * past the watermark point. If the FIFO drains completely, a FIFO underrun
844 * will occur, and a display engine hang could result.
845 */
846static unsigned long intel_calculate_wm(unsigned long clock_in_khz,
847 const struct intel_watermark_params *wm,
848 int fifo_size,
849 int pixel_size,
850 unsigned long latency_ns)
851{
852 long entries_required, wm_size;
853
854 /*
855 * Note: we need to make sure we don't overflow for various clock &
856 * latency values.
857 * clocks go from a few thousand to several hundred thousand.
858 * latency is usually a few thousand
859 */
860 entries_required = ((clock_in_khz / 1000) * pixel_size * latency_ns) /
861 1000;
862 entries_required = DIV_ROUND_UP(entries_required, wm->cacheline_size);
863
864 DRM_DEBUG_KMS("FIFO entries required for mode: %ld\n", entries_required);
865
866 wm_size = fifo_size - (entries_required + wm->guard_size);
867
868 DRM_DEBUG_KMS("FIFO watermark level: %ld\n", wm_size);
869
870 /* Don't promote wm_size to unsigned... */
871 if (wm_size > (long)wm->max_wm)
872 wm_size = wm->max_wm;
873 if (wm_size <= 0)
874 wm_size = wm->default_wm;
875 return wm_size;
876}
877
878static struct drm_crtc *single_enabled_crtc(struct drm_device *dev)
879{
880 struct drm_crtc *crtc, *enabled = NULL;
881
882 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
883 if (crtc->enabled && crtc->fb) {
884 if (enabled)
885 return NULL;
886 enabled = crtc;
887 }
888 }
889
890 return enabled;
891}
892
893void pineview_update_wm(struct drm_device *dev)
894{
895 struct drm_i915_private *dev_priv = dev->dev_private;
896 struct drm_crtc *crtc;
897 const struct cxsr_latency *latency;
898 u32 reg;
899 unsigned long wm;
900
901 latency = intel_get_cxsr_latency(IS_PINEVIEW_G(dev), dev_priv->is_ddr3,
902 dev_priv->fsb_freq, dev_priv->mem_freq);
903 if (!latency) {
904 DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
905 pineview_disable_cxsr(dev);
906 return;
907 }
908
909 crtc = single_enabled_crtc(dev);
910 if (crtc) {
911 int clock = crtc->mode.clock;
912 int pixel_size = crtc->fb->bits_per_pixel / 8;
913
914 /* Display SR */
915 wm = intel_calculate_wm(clock, &pineview_display_wm,
916 pineview_display_wm.fifo_size,
917 pixel_size, latency->display_sr);
918 reg = I915_READ(DSPFW1);
919 reg &= ~DSPFW_SR_MASK;
920 reg |= wm << DSPFW_SR_SHIFT;
921 I915_WRITE(DSPFW1, reg);
922 DRM_DEBUG_KMS("DSPFW1 register is %x\n", reg);
923
924 /* cursor SR */
925 wm = intel_calculate_wm(clock, &pineview_cursor_wm,
926 pineview_display_wm.fifo_size,
927 pixel_size, latency->cursor_sr);
928 reg = I915_READ(DSPFW3);
929 reg &= ~DSPFW_CURSOR_SR_MASK;
930 reg |= (wm & 0x3f) << DSPFW_CURSOR_SR_SHIFT;
931 I915_WRITE(DSPFW3, reg);
932
933 /* Display HPLL off SR */
934 wm = intel_calculate_wm(clock, &pineview_display_hplloff_wm,
935 pineview_display_hplloff_wm.fifo_size,
936 pixel_size, latency->display_hpll_disable);
937 reg = I915_READ(DSPFW3);
938 reg &= ~DSPFW_HPLL_SR_MASK;
939 reg |= wm & DSPFW_HPLL_SR_MASK;
940 I915_WRITE(DSPFW3, reg);
941
942 /* cursor HPLL off SR */
943 wm = intel_calculate_wm(clock, &pineview_cursor_hplloff_wm,
944 pineview_display_hplloff_wm.fifo_size,
945 pixel_size, latency->cursor_hpll_disable);
946 reg = I915_READ(DSPFW3);
947 reg &= ~DSPFW_HPLL_CURSOR_MASK;
948 reg |= (wm & 0x3f) << DSPFW_HPLL_CURSOR_SHIFT;
949 I915_WRITE(DSPFW3, reg);
950 DRM_DEBUG_KMS("DSPFW3 register is %x\n", reg);
951
952 /* activate cxsr */
953 I915_WRITE(DSPFW3,
954 I915_READ(DSPFW3) | PINEVIEW_SELF_REFRESH_EN);
955 DRM_DEBUG_KMS("Self-refresh is enabled\n");
956 } else {
957 pineview_disable_cxsr(dev);
958 DRM_DEBUG_KMS("Self-refresh is disabled\n");
959 }
960}
961
962static bool g4x_compute_wm0(struct drm_device *dev,
963 int plane,
964 const struct intel_watermark_params *display,
965 int display_latency_ns,
966 const struct intel_watermark_params *cursor,
967 int cursor_latency_ns,
968 int *plane_wm,
969 int *cursor_wm)
970{
971 struct drm_crtc *crtc;
972 int htotal, hdisplay, clock, pixel_size;
973 int line_time_us, line_count;
974 int entries, tlb_miss;
975
976 crtc = intel_get_crtc_for_plane(dev, plane);
977 if (crtc->fb == NULL || !crtc->enabled) {
978 *cursor_wm = cursor->guard_size;
979 *plane_wm = display->guard_size;
980 return false;
981 }
982
983 htotal = crtc->mode.htotal;
984 hdisplay = crtc->mode.hdisplay;
985 clock = crtc->mode.clock;
986 pixel_size = crtc->fb->bits_per_pixel / 8;
987
988 /* Use the small buffer method to calculate plane watermark */
989 entries = ((clock * pixel_size / 1000) * display_latency_ns) / 1000;
990 tlb_miss = display->fifo_size*display->cacheline_size - hdisplay * 8;
991 if (tlb_miss > 0)
992 entries += tlb_miss;
993 entries = DIV_ROUND_UP(entries, display->cacheline_size);
994 *plane_wm = entries + display->guard_size;
995 if (*plane_wm > (int)display->max_wm)
996 *plane_wm = display->max_wm;
997
998 /* Use the large buffer method to calculate cursor watermark */
999 line_time_us = ((htotal * 1000) / clock);
1000 line_count = (cursor_latency_ns / line_time_us + 1000) / 1000;
1001 entries = line_count * 64 * pixel_size;
1002 tlb_miss = cursor->fifo_size*cursor->cacheline_size - hdisplay * 8;
1003 if (tlb_miss > 0)
1004 entries += tlb_miss;
1005 entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
1006 *cursor_wm = entries + cursor->guard_size;
1007 if (*cursor_wm > (int)cursor->max_wm)
1008 *cursor_wm = (int)cursor->max_wm;
1009
1010 return true;
1011}
1012
1013/*
1014 * Check the wm result.
1015 *
1016 * If any calculated watermark values is larger than the maximum value that
1017 * can be programmed into the associated watermark register, that watermark
1018 * must be disabled.
1019 */
1020static bool g4x_check_srwm(struct drm_device *dev,
1021 int display_wm, int cursor_wm,
1022 const struct intel_watermark_params *display,
1023 const struct intel_watermark_params *cursor)
1024{
1025 DRM_DEBUG_KMS("SR watermark: display plane %d, cursor %d\n",
1026 display_wm, cursor_wm);
1027
1028 if (display_wm > display->max_wm) {
1029 DRM_DEBUG_KMS("display watermark is too large(%d/%ld), disabling\n",
1030 display_wm, display->max_wm);
1031 return false;
1032 }
1033
1034 if (cursor_wm > cursor->max_wm) {
1035 DRM_DEBUG_KMS("cursor watermark is too large(%d/%ld), disabling\n",
1036 cursor_wm, cursor->max_wm);
1037 return false;
1038 }
1039
1040 if (!(display_wm || cursor_wm)) {
1041 DRM_DEBUG_KMS("SR latency is 0, disabling\n");
1042 return false;
1043 }
1044
1045 return true;
1046}
1047
1048static bool g4x_compute_srwm(struct drm_device *dev,
1049 int plane,
1050 int latency_ns,
1051 const struct intel_watermark_params *display,
1052 const struct intel_watermark_params *cursor,
1053 int *display_wm, int *cursor_wm)
1054{
1055 struct drm_crtc *crtc;
1056 int hdisplay, htotal, pixel_size, clock;
1057 unsigned long line_time_us;
1058 int line_count, line_size;
1059 int small, large;
1060 int entries;
1061
1062 if (!latency_ns) {
1063 *display_wm = *cursor_wm = 0;
1064 return false;
1065 }
1066
1067 crtc = intel_get_crtc_for_plane(dev, plane);
1068 hdisplay = crtc->mode.hdisplay;
1069 htotal = crtc->mode.htotal;
1070 clock = crtc->mode.clock;
1071 pixel_size = crtc->fb->bits_per_pixel / 8;
1072
1073 line_time_us = (htotal * 1000) / clock;
1074 line_count = (latency_ns / line_time_us + 1000) / 1000;
1075 line_size = hdisplay * pixel_size;
1076
1077 /* Use the minimum of the small and large buffer method for primary */
1078 small = ((clock * pixel_size / 1000) * latency_ns) / 1000;
1079 large = line_count * line_size;
1080
1081 entries = DIV_ROUND_UP(min(small, large), display->cacheline_size);
1082 *display_wm = entries + display->guard_size;
1083
1084 /* calculate the self-refresh watermark for display cursor */
1085 entries = line_count * pixel_size * 64;
1086 entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
1087 *cursor_wm = entries + cursor->guard_size;
1088
1089 return g4x_check_srwm(dev,
1090 *display_wm, *cursor_wm,
1091 display, cursor);
1092}
1093
1094static bool vlv_compute_drain_latency(struct drm_device *dev,
1095 int plane,
1096 int *plane_prec_mult,
1097 int *plane_dl,
1098 int *cursor_prec_mult,
1099 int *cursor_dl)
1100{
1101 struct drm_crtc *crtc;
1102 int clock, pixel_size;
1103 int entries;
1104
1105 crtc = intel_get_crtc_for_plane(dev, plane);
1106 if (crtc->fb == NULL || !crtc->enabled)
1107 return false;
1108
1109 clock = crtc->mode.clock; /* VESA DOT Clock */
1110 pixel_size = crtc->fb->bits_per_pixel / 8; /* BPP */
1111
1112 entries = (clock / 1000) * pixel_size;
1113 *plane_prec_mult = (entries > 256) ?
1114 DRAIN_LATENCY_PRECISION_32 : DRAIN_LATENCY_PRECISION_16;
1115 *plane_dl = (64 * (*plane_prec_mult) * 4) / ((clock / 1000) *
1116 pixel_size);
1117
1118 entries = (clock / 1000) * 4; /* BPP is always 4 for cursor */
1119 *cursor_prec_mult = (entries > 256) ?
1120 DRAIN_LATENCY_PRECISION_32 : DRAIN_LATENCY_PRECISION_16;
1121 *cursor_dl = (64 * (*cursor_prec_mult) * 4) / ((clock / 1000) * 4);
1122
1123 return true;
1124}
1125
1126/*
1127 * Update drain latency registers of memory arbiter
1128 *
1129 * Valleyview SoC has a new memory arbiter and needs drain latency registers
1130 * to be programmed. Each plane has a drain latency multiplier and a drain
1131 * latency value.
1132 */
1133
1134static void vlv_update_drain_latency(struct drm_device *dev)
1135{
1136 struct drm_i915_private *dev_priv = dev->dev_private;
1137 int planea_prec, planea_dl, planeb_prec, planeb_dl;
1138 int cursora_prec, cursora_dl, cursorb_prec, cursorb_dl;
1139 int plane_prec_mult, cursor_prec_mult; /* Precision multiplier is
1140 either 16 or 32 */
1141
1142 /* For plane A, Cursor A */
1143 if (vlv_compute_drain_latency(dev, 0, &plane_prec_mult, &planea_dl,
1144 &cursor_prec_mult, &cursora_dl)) {
1145 cursora_prec = (cursor_prec_mult == DRAIN_LATENCY_PRECISION_32) ?
1146 DDL_CURSORA_PRECISION_32 : DDL_CURSORA_PRECISION_16;
1147 planea_prec = (plane_prec_mult == DRAIN_LATENCY_PRECISION_32) ?
1148 DDL_PLANEA_PRECISION_32 : DDL_PLANEA_PRECISION_16;
1149
1150 I915_WRITE(VLV_DDL1, cursora_prec |
1151 (cursora_dl << DDL_CURSORA_SHIFT) |
1152 planea_prec | planea_dl);
1153 }
1154
1155 /* For plane B, Cursor B */
1156 if (vlv_compute_drain_latency(dev, 1, &plane_prec_mult, &planeb_dl,
1157 &cursor_prec_mult, &cursorb_dl)) {
1158 cursorb_prec = (cursor_prec_mult == DRAIN_LATENCY_PRECISION_32) ?
1159 DDL_CURSORB_PRECISION_32 : DDL_CURSORB_PRECISION_16;
1160 planeb_prec = (plane_prec_mult == DRAIN_LATENCY_PRECISION_32) ?
1161 DDL_PLANEB_PRECISION_32 : DDL_PLANEB_PRECISION_16;
1162
1163 I915_WRITE(VLV_DDL2, cursorb_prec |
1164 (cursorb_dl << DDL_CURSORB_SHIFT) |
1165 planeb_prec | planeb_dl);
1166 }
1167}
1168
1169#define single_plane_enabled(mask) is_power_of_2(mask)
1170
1171void valleyview_update_wm(struct drm_device *dev)
1172{
1173 static const int sr_latency_ns = 12000;
1174 struct drm_i915_private *dev_priv = dev->dev_private;
1175 int planea_wm, planeb_wm, cursora_wm, cursorb_wm;
1176 int plane_sr, cursor_sr;
1177 unsigned int enabled = 0;
1178
1179 vlv_update_drain_latency(dev);
1180
1181 if (g4x_compute_wm0(dev, 0,
1182 &valleyview_wm_info, latency_ns,
1183 &valleyview_cursor_wm_info, latency_ns,
1184 &planea_wm, &cursora_wm))
1185 enabled |= 1;
1186
1187 if (g4x_compute_wm0(dev, 1,
1188 &valleyview_wm_info, latency_ns,
1189 &valleyview_cursor_wm_info, latency_ns,
1190 &planeb_wm, &cursorb_wm))
1191 enabled |= 2;
1192
1193 plane_sr = cursor_sr = 0;
1194 if (single_plane_enabled(enabled) &&
1195 g4x_compute_srwm(dev, ffs(enabled) - 1,
1196 sr_latency_ns,
1197 &valleyview_wm_info,
1198 &valleyview_cursor_wm_info,
1199 &plane_sr, &cursor_sr))
1200 I915_WRITE(FW_BLC_SELF_VLV, FW_CSPWRDWNEN);
1201 else
1202 I915_WRITE(FW_BLC_SELF_VLV,
1203 I915_READ(FW_BLC_SELF_VLV) & ~FW_CSPWRDWNEN);
1204
1205 DRM_DEBUG_KMS("Setting FIFO watermarks - A: plane=%d, cursor=%d, B: plane=%d, cursor=%d, SR: plane=%d, cursor=%d\n",
1206 planea_wm, cursora_wm,
1207 planeb_wm, cursorb_wm,
1208 plane_sr, cursor_sr);
1209
1210 I915_WRITE(DSPFW1,
1211 (plane_sr << DSPFW_SR_SHIFT) |
1212 (cursorb_wm << DSPFW_CURSORB_SHIFT) |
1213 (planeb_wm << DSPFW_PLANEB_SHIFT) |
1214 planea_wm);
1215 I915_WRITE(DSPFW2,
1216 (I915_READ(DSPFW2) & DSPFW_CURSORA_MASK) |
1217 (cursora_wm << DSPFW_CURSORA_SHIFT));
1218 I915_WRITE(DSPFW3,
1219 (I915_READ(DSPFW3) | (cursor_sr << DSPFW_CURSOR_SR_SHIFT)));
1220}
1221
1222void g4x_update_wm(struct drm_device *dev)
1223{
1224 static const int sr_latency_ns = 12000;
1225 struct drm_i915_private *dev_priv = dev->dev_private;
1226 int planea_wm, planeb_wm, cursora_wm, cursorb_wm;
1227 int plane_sr, cursor_sr;
1228 unsigned int enabled = 0;
1229
1230 if (g4x_compute_wm0(dev, 0,
1231 &g4x_wm_info, latency_ns,
1232 &g4x_cursor_wm_info, latency_ns,
1233 &planea_wm, &cursora_wm))
1234 enabled |= 1;
1235
1236 if (g4x_compute_wm0(dev, 1,
1237 &g4x_wm_info, latency_ns,
1238 &g4x_cursor_wm_info, latency_ns,
1239 &planeb_wm, &cursorb_wm))
1240 enabled |= 2;
1241
1242 plane_sr = cursor_sr = 0;
1243 if (single_plane_enabled(enabled) &&
1244 g4x_compute_srwm(dev, ffs(enabled) - 1,
1245 sr_latency_ns,
1246 &g4x_wm_info,
1247 &g4x_cursor_wm_info,
1248 &plane_sr, &cursor_sr))
1249 I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_EN);
1250 else
1251 I915_WRITE(FW_BLC_SELF,
1252 I915_READ(FW_BLC_SELF) & ~FW_BLC_SELF_EN);
1253
1254 DRM_DEBUG_KMS("Setting FIFO watermarks - A: plane=%d, cursor=%d, B: plane=%d, cursor=%d, SR: plane=%d, cursor=%d\n",
1255 planea_wm, cursora_wm,
1256 planeb_wm, cursorb_wm,
1257 plane_sr, cursor_sr);
1258
1259 I915_WRITE(DSPFW1,
1260 (plane_sr << DSPFW_SR_SHIFT) |
1261 (cursorb_wm << DSPFW_CURSORB_SHIFT) |
1262 (planeb_wm << DSPFW_PLANEB_SHIFT) |
1263 planea_wm);
1264 I915_WRITE(DSPFW2,
1265 (I915_READ(DSPFW2) & DSPFW_CURSORA_MASK) |
1266 (cursora_wm << DSPFW_CURSORA_SHIFT));
1267 /* HPLL off in SR has some issues on G4x... disable it */
1268 I915_WRITE(DSPFW3,
1269 (I915_READ(DSPFW3) & ~DSPFW_HPLL_SR_EN) |
1270 (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
1271}
1272
1273void i965_update_wm(struct drm_device *dev)
1274{
1275 struct drm_i915_private *dev_priv = dev->dev_private;
1276 struct drm_crtc *crtc;
1277 int srwm = 1;
1278 int cursor_sr = 16;
1279
1280 /* Calc sr entries for one plane configs */
1281 crtc = single_enabled_crtc(dev);
1282 if (crtc) {
1283 /* self-refresh has much higher latency */
1284 static const int sr_latency_ns = 12000;
1285 int clock = crtc->mode.clock;
1286 int htotal = crtc->mode.htotal;
1287 int hdisplay = crtc->mode.hdisplay;
1288 int pixel_size = crtc->fb->bits_per_pixel / 8;
1289 unsigned long line_time_us;
1290 int entries;
1291
1292 line_time_us = ((htotal * 1000) / clock);
1293
1294 /* Use ns/us then divide to preserve precision */
1295 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
1296 pixel_size * hdisplay;
1297 entries = DIV_ROUND_UP(entries, I915_FIFO_LINE_SIZE);
1298 srwm = I965_FIFO_SIZE - entries;
1299 if (srwm < 0)
1300 srwm = 1;
1301 srwm &= 0x1ff;
1302 DRM_DEBUG_KMS("self-refresh entries: %d, wm: %d\n",
1303 entries, srwm);
1304
1305 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
1306 pixel_size * 64;
1307 entries = DIV_ROUND_UP(entries,
1308 i965_cursor_wm_info.cacheline_size);
1309 cursor_sr = i965_cursor_wm_info.fifo_size -
1310 (entries + i965_cursor_wm_info.guard_size);
1311
1312 if (cursor_sr > i965_cursor_wm_info.max_wm)
1313 cursor_sr = i965_cursor_wm_info.max_wm;
1314
1315 DRM_DEBUG_KMS("self-refresh watermark: display plane %d "
1316 "cursor %d\n", srwm, cursor_sr);
1317
1318 if (IS_CRESTLINE(dev))
1319 I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_EN);
1320 } else {
1321 /* Turn off self refresh if both pipes are enabled */
1322 if (IS_CRESTLINE(dev))
1323 I915_WRITE(FW_BLC_SELF, I915_READ(FW_BLC_SELF)
1324 & ~FW_BLC_SELF_EN);
1325 }
1326
1327 DRM_DEBUG_KMS("Setting FIFO watermarks - A: 8, B: 8, C: 8, SR %d\n",
1328 srwm);
1329
1330 /* 965 has limitations... */
1331 I915_WRITE(DSPFW1, (srwm << DSPFW_SR_SHIFT) |
1332 (8 << 16) | (8 << 8) | (8 << 0));
1333 I915_WRITE(DSPFW2, (8 << 8) | (8 << 0));
1334 /* update cursor SR watermark */
1335 I915_WRITE(DSPFW3, (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
1336}
1337
1338void i9xx_update_wm(struct drm_device *dev)
1339{
1340 struct drm_i915_private *dev_priv = dev->dev_private;
1341 const struct intel_watermark_params *wm_info;
1342 uint32_t fwater_lo;
1343 uint32_t fwater_hi;
1344 int cwm, srwm = 1;
1345 int fifo_size;
1346 int planea_wm, planeb_wm;
1347 struct drm_crtc *crtc, *enabled = NULL;
1348
1349 if (IS_I945GM(dev))
1350 wm_info = &i945_wm_info;
1351 else if (!IS_GEN2(dev))
1352 wm_info = &i915_wm_info;
1353 else
1354 wm_info = &i855_wm_info;
1355
1356 fifo_size = dev_priv->display.get_fifo_size(dev, 0);
1357 crtc = intel_get_crtc_for_plane(dev, 0);
1358 if (crtc->enabled && crtc->fb) {
1359 planea_wm = intel_calculate_wm(crtc->mode.clock,
1360 wm_info, fifo_size,
1361 crtc->fb->bits_per_pixel / 8,
1362 latency_ns);
1363 enabled = crtc;
1364 } else
1365 planea_wm = fifo_size - wm_info->guard_size;
1366
1367 fifo_size = dev_priv->display.get_fifo_size(dev, 1);
1368 crtc = intel_get_crtc_for_plane(dev, 1);
1369 if (crtc->enabled && crtc->fb) {
1370 planeb_wm = intel_calculate_wm(crtc->mode.clock,
1371 wm_info, fifo_size,
1372 crtc->fb->bits_per_pixel / 8,
1373 latency_ns);
1374 if (enabled == NULL)
1375 enabled = crtc;
1376 else
1377 enabled = NULL;
1378 } else
1379 planeb_wm = fifo_size - wm_info->guard_size;
1380
1381 DRM_DEBUG_KMS("FIFO watermarks - A: %d, B: %d\n", planea_wm, planeb_wm);
1382
1383 /*
1384 * Overlay gets an aggressive default since video jitter is bad.
1385 */
1386 cwm = 2;
1387
1388 /* Play safe and disable self-refresh before adjusting watermarks. */
1389 if (IS_I945G(dev) || IS_I945GM(dev))
1390 I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_EN_MASK | 0);
1391 else if (IS_I915GM(dev))
1392 I915_WRITE(INSTPM, I915_READ(INSTPM) & ~INSTPM_SELF_EN);
1393
1394 /* Calc sr entries for one plane configs */
1395 if (HAS_FW_BLC(dev) && enabled) {
1396 /* self-refresh has much higher latency */
1397 static const int sr_latency_ns = 6000;
1398 int clock = enabled->mode.clock;
1399 int htotal = enabled->mode.htotal;
1400 int hdisplay = enabled->mode.hdisplay;
1401 int pixel_size = enabled->fb->bits_per_pixel / 8;
1402 unsigned long line_time_us;
1403 int entries;
1404
1405 line_time_us = (htotal * 1000) / clock;
1406
1407 /* Use ns/us then divide to preserve precision */
1408 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
1409 pixel_size * hdisplay;
1410 entries = DIV_ROUND_UP(entries, wm_info->cacheline_size);
1411 DRM_DEBUG_KMS("self-refresh entries: %d\n", entries);
1412 srwm = wm_info->fifo_size - entries;
1413 if (srwm < 0)
1414 srwm = 1;
1415
1416 if (IS_I945G(dev) || IS_I945GM(dev))
1417 I915_WRITE(FW_BLC_SELF,
1418 FW_BLC_SELF_FIFO_MASK | (srwm & 0xff));
1419 else if (IS_I915GM(dev))
1420 I915_WRITE(FW_BLC_SELF, srwm & 0x3f);
1421 }
1422
1423 DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d, B: %d, C: %d, SR %d\n",
1424 planea_wm, planeb_wm, cwm, srwm);
1425
1426 fwater_lo = ((planeb_wm & 0x3f) << 16) | (planea_wm & 0x3f);
1427 fwater_hi = (cwm & 0x1f);
1428
1429 /* Set request length to 8 cachelines per fetch */
1430 fwater_lo = fwater_lo | (1 << 24) | (1 << 8);
1431 fwater_hi = fwater_hi | (1 << 8);
1432
1433 I915_WRITE(FW_BLC, fwater_lo);
1434 I915_WRITE(FW_BLC2, fwater_hi);
1435
1436 if (HAS_FW_BLC(dev)) {
1437 if (enabled) {
1438 if (IS_I945G(dev) || IS_I945GM(dev))
1439 I915_WRITE(FW_BLC_SELF,
1440 FW_BLC_SELF_EN_MASK | FW_BLC_SELF_EN);
1441 else if (IS_I915GM(dev))
1442 I915_WRITE(INSTPM, I915_READ(INSTPM) | INSTPM_SELF_EN);
1443 DRM_DEBUG_KMS("memory self refresh enabled\n");
1444 } else
1445 DRM_DEBUG_KMS("memory self refresh disabled\n");
1446 }
1447}
1448
1449void i830_update_wm(struct drm_device *dev)
1450{
1451 struct drm_i915_private *dev_priv = dev->dev_private;
1452 struct drm_crtc *crtc;
1453 uint32_t fwater_lo;
1454 int planea_wm;
1455
1456 crtc = single_enabled_crtc(dev);
1457 if (crtc == NULL)
1458 return;
1459
1460 planea_wm = intel_calculate_wm(crtc->mode.clock, &i830_wm_info,
1461 dev_priv->display.get_fifo_size(dev, 0),
1462 crtc->fb->bits_per_pixel / 8,
1463 latency_ns);
1464 fwater_lo = I915_READ(FW_BLC) & ~0xfff;
1465 fwater_lo |= (3<<8) | planea_wm;
1466
1467 DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d\n", planea_wm);
1468
1469 I915_WRITE(FW_BLC, fwater_lo);
1470}
1471
1472#define ILK_LP0_PLANE_LATENCY 700
1473#define ILK_LP0_CURSOR_LATENCY 1300
1474
1475/*
1476 * Check the wm result.
1477 *
1478 * If any calculated watermark values is larger than the maximum value that
1479 * can be programmed into the associated watermark register, that watermark
1480 * must be disabled.
1481 */
1482static bool ironlake_check_srwm(struct drm_device *dev, int level,
1483 int fbc_wm, int display_wm, int cursor_wm,
1484 const struct intel_watermark_params *display,
1485 const struct intel_watermark_params *cursor)
1486{
1487 struct drm_i915_private *dev_priv = dev->dev_private;
1488
1489 DRM_DEBUG_KMS("watermark %d: display plane %d, fbc lines %d,"
1490 " cursor %d\n", level, display_wm, fbc_wm, cursor_wm);
1491
1492 if (fbc_wm > SNB_FBC_MAX_SRWM) {
1493 DRM_DEBUG_KMS("fbc watermark(%d) is too large(%d), disabling wm%d+\n",
1494 fbc_wm, SNB_FBC_MAX_SRWM, level);
1495
1496 /* fbc has it's own way to disable FBC WM */
1497 I915_WRITE(DISP_ARB_CTL,
1498 I915_READ(DISP_ARB_CTL) | DISP_FBC_WM_DIS);
1499 return false;
1500 }
1501
1502 if (display_wm > display->max_wm) {
1503 DRM_DEBUG_KMS("display watermark(%d) is too large(%d), disabling wm%d+\n",
1504 display_wm, SNB_DISPLAY_MAX_SRWM, level);
1505 return false;
1506 }
1507
1508 if (cursor_wm > cursor->max_wm) {
1509 DRM_DEBUG_KMS("cursor watermark(%d) is too large(%d), disabling wm%d+\n",
1510 cursor_wm, SNB_CURSOR_MAX_SRWM, level);
1511 return false;
1512 }
1513
1514 if (!(fbc_wm || display_wm || cursor_wm)) {
1515 DRM_DEBUG_KMS("latency %d is 0, disabling wm%d+\n", level, level);
1516 return false;
1517 }
1518
1519 return true;
1520}
1521
1522/*
1523 * Compute watermark values of WM[1-3],
1524 */
1525static bool ironlake_compute_srwm(struct drm_device *dev, int level, int plane,
1526 int latency_ns,
1527 const struct intel_watermark_params *display,
1528 const struct intel_watermark_params *cursor,
1529 int *fbc_wm, int *display_wm, int *cursor_wm)
1530{
1531 struct drm_crtc *crtc;
1532 unsigned long line_time_us;
1533 int hdisplay, htotal, pixel_size, clock;
1534 int line_count, line_size;
1535 int small, large;
1536 int entries;
1537
1538 if (!latency_ns) {
1539 *fbc_wm = *display_wm = *cursor_wm = 0;
1540 return false;
1541 }
1542
1543 crtc = intel_get_crtc_for_plane(dev, plane);
1544 hdisplay = crtc->mode.hdisplay;
1545 htotal = crtc->mode.htotal;
1546 clock = crtc->mode.clock;
1547 pixel_size = crtc->fb->bits_per_pixel / 8;
1548
1549 line_time_us = (htotal * 1000) / clock;
1550 line_count = (latency_ns / line_time_us + 1000) / 1000;
1551 line_size = hdisplay * pixel_size;
1552
1553 /* Use the minimum of the small and large buffer method for primary */
1554 small = ((clock * pixel_size / 1000) * latency_ns) / 1000;
1555 large = line_count * line_size;
1556
1557 entries = DIV_ROUND_UP(min(small, large), display->cacheline_size);
1558 *display_wm = entries + display->guard_size;
1559
1560 /*
1561 * Spec says:
1562 * FBC WM = ((Final Primary WM * 64) / number of bytes per line) + 2
1563 */
1564 *fbc_wm = DIV_ROUND_UP(*display_wm * 64, line_size) + 2;
1565
1566 /* calculate the self-refresh watermark for display cursor */
1567 entries = line_count * pixel_size * 64;
1568 entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
1569 *cursor_wm = entries + cursor->guard_size;
1570
1571 return ironlake_check_srwm(dev, level,
1572 *fbc_wm, *display_wm, *cursor_wm,
1573 display, cursor);
1574}
1575
1576void ironlake_update_wm(struct drm_device *dev)
1577{
1578 struct drm_i915_private *dev_priv = dev->dev_private;
1579 int fbc_wm, plane_wm, cursor_wm;
1580 unsigned int enabled;
1581
1582 enabled = 0;
1583 if (g4x_compute_wm0(dev, 0,
1584 &ironlake_display_wm_info,
1585 ILK_LP0_PLANE_LATENCY,
1586 &ironlake_cursor_wm_info,
1587 ILK_LP0_CURSOR_LATENCY,
1588 &plane_wm, &cursor_wm)) {
1589 I915_WRITE(WM0_PIPEA_ILK,
1590 (plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm);
1591 DRM_DEBUG_KMS("FIFO watermarks For pipe A -"
1592 " plane %d, " "cursor: %d\n",
1593 plane_wm, cursor_wm);
1594 enabled |= 1;
1595 }
1596
1597 if (g4x_compute_wm0(dev, 1,
1598 &ironlake_display_wm_info,
1599 ILK_LP0_PLANE_LATENCY,
1600 &ironlake_cursor_wm_info,
1601 ILK_LP0_CURSOR_LATENCY,
1602 &plane_wm, &cursor_wm)) {
1603 I915_WRITE(WM0_PIPEB_ILK,
1604 (plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm);
1605 DRM_DEBUG_KMS("FIFO watermarks For pipe B -"
1606 " plane %d, cursor: %d\n",
1607 plane_wm, cursor_wm);
1608 enabled |= 2;
1609 }
1610
1611 /*
1612 * Calculate and update the self-refresh watermark only when one
1613 * display plane is used.
1614 */
1615 I915_WRITE(WM3_LP_ILK, 0);
1616 I915_WRITE(WM2_LP_ILK, 0);
1617 I915_WRITE(WM1_LP_ILK, 0);
1618
1619 if (!single_plane_enabled(enabled))
1620 return;
1621 enabled = ffs(enabled) - 1;
1622
1623 /* WM1 */
1624 if (!ironlake_compute_srwm(dev, 1, enabled,
1625 ILK_READ_WM1_LATENCY() * 500,
1626 &ironlake_display_srwm_info,
1627 &ironlake_cursor_srwm_info,
1628 &fbc_wm, &plane_wm, &cursor_wm))
1629 return;
1630
1631 I915_WRITE(WM1_LP_ILK,
1632 WM1_LP_SR_EN |
1633 (ILK_READ_WM1_LATENCY() << WM1_LP_LATENCY_SHIFT) |
1634 (fbc_wm << WM1_LP_FBC_SHIFT) |
1635 (plane_wm << WM1_LP_SR_SHIFT) |
1636 cursor_wm);
1637
1638 /* WM2 */
1639 if (!ironlake_compute_srwm(dev, 2, enabled,
1640 ILK_READ_WM2_LATENCY() * 500,
1641 &ironlake_display_srwm_info,
1642 &ironlake_cursor_srwm_info,
1643 &fbc_wm, &plane_wm, &cursor_wm))
1644 return;
1645
1646 I915_WRITE(WM2_LP_ILK,
1647 WM2_LP_EN |
1648 (ILK_READ_WM2_LATENCY() << WM1_LP_LATENCY_SHIFT) |
1649 (fbc_wm << WM1_LP_FBC_SHIFT) |
1650 (plane_wm << WM1_LP_SR_SHIFT) |
1651 cursor_wm);
1652
1653 /*
1654 * WM3 is unsupported on ILK, probably because we don't have latency
1655 * data for that power state
1656 */
1657}
1658
1659void sandybridge_update_wm(struct drm_device *dev)
1660{
1661 struct drm_i915_private *dev_priv = dev->dev_private;
1662 int latency = SNB_READ_WM0_LATENCY() * 100; /* In unit 0.1us */
1663 u32 val;
1664 int fbc_wm, plane_wm, cursor_wm;
1665 unsigned int enabled;
1666
1667 enabled = 0;
1668 if (g4x_compute_wm0(dev, 0,
1669 &sandybridge_display_wm_info, latency,
1670 &sandybridge_cursor_wm_info, latency,
1671 &plane_wm, &cursor_wm)) {
1672 val = I915_READ(WM0_PIPEA_ILK);
1673 val &= ~(WM0_PIPE_PLANE_MASK | WM0_PIPE_CURSOR_MASK);
1674 I915_WRITE(WM0_PIPEA_ILK, val |
1675 ((plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm));
1676 DRM_DEBUG_KMS("FIFO watermarks For pipe A -"
1677 " plane %d, " "cursor: %d\n",
1678 plane_wm, cursor_wm);
1679 enabled |= 1;
1680 }
1681
1682 if (g4x_compute_wm0(dev, 1,
1683 &sandybridge_display_wm_info, latency,
1684 &sandybridge_cursor_wm_info, latency,
1685 &plane_wm, &cursor_wm)) {
1686 val = I915_READ(WM0_PIPEB_ILK);
1687 val &= ~(WM0_PIPE_PLANE_MASK | WM0_PIPE_CURSOR_MASK);
1688 I915_WRITE(WM0_PIPEB_ILK, val |
1689 ((plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm));
1690 DRM_DEBUG_KMS("FIFO watermarks For pipe B -"
1691 " plane %d, cursor: %d\n",
1692 plane_wm, cursor_wm);
1693 enabled |= 2;
1694 }
1695
1696 /* IVB has 3 pipes */
1697 if (IS_IVYBRIDGE(dev) &&
1698 g4x_compute_wm0(dev, 2,
1699 &sandybridge_display_wm_info, latency,
1700 &sandybridge_cursor_wm_info, latency,
1701 &plane_wm, &cursor_wm)) {
1702 val = I915_READ(WM0_PIPEC_IVB);
1703 val &= ~(WM0_PIPE_PLANE_MASK | WM0_PIPE_CURSOR_MASK);
1704 I915_WRITE(WM0_PIPEC_IVB, val |
1705 ((plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm));
1706 DRM_DEBUG_KMS("FIFO watermarks For pipe C -"
1707 " plane %d, cursor: %d\n",
1708 plane_wm, cursor_wm);
1709 enabled |= 3;
1710 }
1711
1712 /*
1713 * Calculate and update the self-refresh watermark only when one
1714 * display plane is used.
1715 *
1716 * SNB support 3 levels of watermark.
1717 *
1718 * WM1/WM2/WM2 watermarks have to be enabled in the ascending order,
1719 * and disabled in the descending order
1720 *
1721 */
1722 I915_WRITE(WM3_LP_ILK, 0);
1723 I915_WRITE(WM2_LP_ILK, 0);
1724 I915_WRITE(WM1_LP_ILK, 0);
1725
1726 if (!single_plane_enabled(enabled) ||
1727 dev_priv->sprite_scaling_enabled)
1728 return;
1729 enabled = ffs(enabled) - 1;
1730
1731 /* WM1 */
1732 if (!ironlake_compute_srwm(dev, 1, enabled,
1733 SNB_READ_WM1_LATENCY() * 500,
1734 &sandybridge_display_srwm_info,
1735 &sandybridge_cursor_srwm_info,
1736 &fbc_wm, &plane_wm, &cursor_wm))
1737 return;
1738
1739 I915_WRITE(WM1_LP_ILK,
1740 WM1_LP_SR_EN |
1741 (SNB_READ_WM1_LATENCY() << WM1_LP_LATENCY_SHIFT) |
1742 (fbc_wm << WM1_LP_FBC_SHIFT) |
1743 (plane_wm << WM1_LP_SR_SHIFT) |
1744 cursor_wm);
1745
1746 /* WM2 */
1747 if (!ironlake_compute_srwm(dev, 2, enabled,
1748 SNB_READ_WM2_LATENCY() * 500,
1749 &sandybridge_display_srwm_info,
1750 &sandybridge_cursor_srwm_info,
1751 &fbc_wm, &plane_wm, &cursor_wm))
1752 return;
1753
1754 I915_WRITE(WM2_LP_ILK,
1755 WM2_LP_EN |
1756 (SNB_READ_WM2_LATENCY() << WM1_LP_LATENCY_SHIFT) |
1757 (fbc_wm << WM1_LP_FBC_SHIFT) |
1758 (plane_wm << WM1_LP_SR_SHIFT) |
1759 cursor_wm);
1760
1761 /* WM3 */
1762 if (!ironlake_compute_srwm(dev, 3, enabled,
1763 SNB_READ_WM3_LATENCY() * 500,
1764 &sandybridge_display_srwm_info,
1765 &sandybridge_cursor_srwm_info,
1766 &fbc_wm, &plane_wm, &cursor_wm))
1767 return;
1768
1769 I915_WRITE(WM3_LP_ILK,
1770 WM3_LP_EN |
1771 (SNB_READ_WM3_LATENCY() << WM1_LP_LATENCY_SHIFT) |
1772 (fbc_wm << WM1_LP_FBC_SHIFT) |
1773 (plane_wm << WM1_LP_SR_SHIFT) |
1774 cursor_wm);
1775}
1776
1777static bool
1778sandybridge_compute_sprite_wm(struct drm_device *dev, int plane,
1779 uint32_t sprite_width, int pixel_size,
1780 const struct intel_watermark_params *display,
1781 int display_latency_ns, int *sprite_wm)
1782{
1783 struct drm_crtc *crtc;
1784 int clock;
1785 int entries, tlb_miss;
1786
1787 crtc = intel_get_crtc_for_plane(dev, plane);
1788 if (crtc->fb == NULL || !crtc->enabled) {
1789 *sprite_wm = display->guard_size;
1790 return false;
1791 }
1792
1793 clock = crtc->mode.clock;
1794
1795 /* Use the small buffer method to calculate the sprite watermark */
1796 entries = ((clock * pixel_size / 1000) * display_latency_ns) / 1000;
1797 tlb_miss = display->fifo_size*display->cacheline_size -
1798 sprite_width * 8;
1799 if (tlb_miss > 0)
1800 entries += tlb_miss;
1801 entries = DIV_ROUND_UP(entries, display->cacheline_size);
1802 *sprite_wm = entries + display->guard_size;
1803 if (*sprite_wm > (int)display->max_wm)
1804 *sprite_wm = display->max_wm;
1805
1806 return true;
1807}
1808
1809static bool
1810sandybridge_compute_sprite_srwm(struct drm_device *dev, int plane,
1811 uint32_t sprite_width, int pixel_size,
1812 const struct intel_watermark_params *display,
1813 int latency_ns, int *sprite_wm)
1814{
1815 struct drm_crtc *crtc;
1816 unsigned long line_time_us;
1817 int clock;
1818 int line_count, line_size;
1819 int small, large;
1820 int entries;
1821
1822 if (!latency_ns) {
1823 *sprite_wm = 0;
1824 return false;
1825 }
1826
1827 crtc = intel_get_crtc_for_plane(dev, plane);
1828 clock = crtc->mode.clock;
1829 if (!clock) {
1830 *sprite_wm = 0;
1831 return false;
1832 }
1833
1834 line_time_us = (sprite_width * 1000) / clock;
1835 if (!line_time_us) {
1836 *sprite_wm = 0;
1837 return false;
1838 }
1839
1840 line_count = (latency_ns / line_time_us + 1000) / 1000;
1841 line_size = sprite_width * pixel_size;
1842
1843 /* Use the minimum of the small and large buffer method for primary */
1844 small = ((clock * pixel_size / 1000) * latency_ns) / 1000;
1845 large = line_count * line_size;
1846
1847 entries = DIV_ROUND_UP(min(small, large), display->cacheline_size);
1848 *sprite_wm = entries + display->guard_size;
1849
1850 return *sprite_wm > 0x3ff ? false : true;
1851}
1852
1853void sandybridge_update_sprite_wm(struct drm_device *dev, int pipe,
1854 uint32_t sprite_width, int pixel_size)
1855{
1856 struct drm_i915_private *dev_priv = dev->dev_private;
1857 int latency = SNB_READ_WM0_LATENCY() * 100; /* In unit 0.1us */
1858 u32 val;
1859 int sprite_wm, reg;
1860 int ret;
1861
1862 switch (pipe) {
1863 case 0:
1864 reg = WM0_PIPEA_ILK;
1865 break;
1866 case 1:
1867 reg = WM0_PIPEB_ILK;
1868 break;
1869 case 2:
1870 reg = WM0_PIPEC_IVB;
1871 break;
1872 default:
1873 return; /* bad pipe */
1874 }
1875
1876 ret = sandybridge_compute_sprite_wm(dev, pipe, sprite_width, pixel_size,
1877 &sandybridge_display_wm_info,
1878 latency, &sprite_wm);
1879 if (!ret) {
1880 DRM_DEBUG_KMS("failed to compute sprite wm for pipe %d\n",
1881 pipe);
1882 return;
1883 }
1884
1885 val = I915_READ(reg);
1886 val &= ~WM0_PIPE_SPRITE_MASK;
1887 I915_WRITE(reg, val | (sprite_wm << WM0_PIPE_SPRITE_SHIFT));
1888 DRM_DEBUG_KMS("sprite watermarks For pipe %d - %d\n", pipe, sprite_wm);
1889
1890
1891 ret = sandybridge_compute_sprite_srwm(dev, pipe, sprite_width,
1892 pixel_size,
1893 &sandybridge_display_srwm_info,
1894 SNB_READ_WM1_LATENCY() * 500,
1895 &sprite_wm);
1896 if (!ret) {
1897 DRM_DEBUG_KMS("failed to compute sprite lp1 wm on pipe %d\n",
1898 pipe);
1899 return;
1900 }
1901 I915_WRITE(WM1S_LP_ILK, sprite_wm);
1902
1903 /* Only IVB has two more LP watermarks for sprite */
1904 if (!IS_IVYBRIDGE(dev))
1905 return;
1906
1907 ret = sandybridge_compute_sprite_srwm(dev, pipe, sprite_width,
1908 pixel_size,
1909 &sandybridge_display_srwm_info,
1910 SNB_READ_WM2_LATENCY() * 500,
1911 &sprite_wm);
1912 if (!ret) {
1913 DRM_DEBUG_KMS("failed to compute sprite lp2 wm on pipe %d\n",
1914 pipe);
1915 return;
1916 }
1917 I915_WRITE(WM2S_LP_IVB, sprite_wm);
1918
1919 ret = sandybridge_compute_sprite_srwm(dev, pipe, sprite_width,
1920 pixel_size,
1921 &sandybridge_display_srwm_info,
1922 SNB_READ_WM3_LATENCY() * 500,
1923 &sprite_wm);
1924 if (!ret) {
1925 DRM_DEBUG_KMS("failed to compute sprite lp3 wm on pipe %d\n",
1926 pipe);
1927 return;
1928 }
1929 I915_WRITE(WM3S_LP_IVB, sprite_wm);
1930}
1931
1932/**
1933 * intel_update_watermarks - update FIFO watermark values based on current modes
1934 *
1935 * Calculate watermark values for the various WM regs based on current mode
1936 * and plane configuration.
1937 *
1938 * There are several cases to deal with here:
1939 * - normal (i.e. non-self-refresh)
1940 * - self-refresh (SR) mode
1941 * - lines are large relative to FIFO size (buffer can hold up to 2)
1942 * - lines are small relative to FIFO size (buffer can hold more than 2
1943 * lines), so need to account for TLB latency
1944 *
1945 * The normal calculation is:
1946 * watermark = dotclock * bytes per pixel * latency
1947 * where latency is platform & configuration dependent (we assume pessimal
1948 * values here).
1949 *
1950 * The SR calculation is:
1951 * watermark = (trunc(latency/line time)+1) * surface width *
1952 * bytes per pixel
1953 * where
1954 * line time = htotal / dotclock
1955 * surface width = hdisplay for normal plane and 64 for cursor
1956 * and latency is assumed to be high, as above.
1957 *
1958 * The final value programmed to the register should always be rounded up,
1959 * and include an extra 2 entries to account for clock crossings.
1960 *
1961 * We don't use the sprite, so we can ignore that. And on Crestline we have
1962 * to set the non-SR watermarks to 8.
1963 */
1964void intel_update_watermarks(struct drm_device *dev)
1965{
1966 struct drm_i915_private *dev_priv = dev->dev_private;
1967
1968 if (dev_priv->display.update_wm)
1969 dev_priv->display.update_wm(dev);
1970}
1971
1972void intel_update_sprite_watermarks(struct drm_device *dev, int pipe,
1973 uint32_t sprite_width, int pixel_size)
1974{
1975 struct drm_i915_private *dev_priv = dev->dev_private;
1976
1977 if (dev_priv->display.update_sprite_wm)
1978 dev_priv->display.update_sprite_wm(dev, pipe, sprite_width,
1979 pixel_size);
1980}
1981