blob: 3b85660ce4e4b5b96a3d638a4bf299c4a96adf31 [file] [log] [blame]
Eugeni Dodonov85208be2012-04-16 22:20:34 -03001/*
2 * Copyright © 2012 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eugeni Dodonov <eugeni.dodonov@intel.com>
25 *
26 */
27
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -030028#include <linux/cpufreq.h>
Eugeni Dodonov85208be2012-04-16 22:20:34 -030029#include "i915_drv.h"
30#include "intel_drv.h"
Daniel Vettereb48eb02012-04-26 23:28:12 +020031#include "../../../platform/x86/intel_ips.h"
32#include <linux/module.h>
Eugeni Dodonov85208be2012-04-16 22:20:34 -030033
Ben Widawsky057d3862012-09-01 22:59:49 -070034#define FORCEWAKE_ACK_TIMEOUT_MS 2
Ben Widawskyb67a4372012-09-01 22:59:47 -070035
Eugeni Dodonovf6750b32012-04-18 11:51:14 -030036/* FBC, or Frame Buffer Compression, is a technique employed to compress the
37 * framebuffer contents in-memory, aiming at reducing the required bandwidth
38 * during in-memory transfers and, therefore, reduce the power packet.
Eugeni Dodonov85208be2012-04-16 22:20:34 -030039 *
Eugeni Dodonovf6750b32012-04-18 11:51:14 -030040 * The benefits of FBC are mostly visible with solid backgrounds and
41 * variation-less patterns.
Eugeni Dodonov85208be2012-04-16 22:20:34 -030042 *
Eugeni Dodonovf6750b32012-04-18 11:51:14 -030043 * FBC-related functionality can be enabled by the means of the
44 * i915.i915_enable_fbc parameter
Eugeni Dodonov85208be2012-04-16 22:20:34 -030045 */
46
Eugeni Dodonov1fa61102012-04-18 15:29:26 -030047static void i8xx_disable_fbc(struct drm_device *dev)
Eugeni Dodonov85208be2012-04-16 22:20:34 -030048{
49 struct drm_i915_private *dev_priv = dev->dev_private;
50 u32 fbc_ctl;
51
52 /* Disable compression */
53 fbc_ctl = I915_READ(FBC_CONTROL);
54 if ((fbc_ctl & FBC_CTL_EN) == 0)
55 return;
56
57 fbc_ctl &= ~FBC_CTL_EN;
58 I915_WRITE(FBC_CONTROL, fbc_ctl);
59
60 /* Wait for compressing bit to clear */
61 if (wait_for((I915_READ(FBC_STATUS) & FBC_STAT_COMPRESSING) == 0, 10)) {
62 DRM_DEBUG_KMS("FBC idle timed out\n");
63 return;
64 }
65
66 DRM_DEBUG_KMS("disabled FBC\n");
67}
68
Eugeni Dodonov1fa61102012-04-18 15:29:26 -030069static void i8xx_enable_fbc(struct drm_crtc *crtc, unsigned long interval)
Eugeni Dodonov85208be2012-04-16 22:20:34 -030070{
71 struct drm_device *dev = crtc->dev;
72 struct drm_i915_private *dev_priv = dev->dev_private;
73 struct drm_framebuffer *fb = crtc->fb;
74 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
75 struct drm_i915_gem_object *obj = intel_fb->obj;
76 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
77 int cfb_pitch;
78 int plane, i;
79 u32 fbc_ctl, fbc_ctl2;
80
81 cfb_pitch = dev_priv->cfb_size / FBC_LL_SIZE;
82 if (fb->pitches[0] < cfb_pitch)
83 cfb_pitch = fb->pitches[0];
84
85 /* FBC_CTL wants 64B units */
86 cfb_pitch = (cfb_pitch / 64) - 1;
87 plane = intel_crtc->plane == 0 ? FBC_CTL_PLANEA : FBC_CTL_PLANEB;
88
89 /* Clear old tags */
90 for (i = 0; i < (FBC_LL_SIZE / 32) + 1; i++)
91 I915_WRITE(FBC_TAG + (i * 4), 0);
92
93 /* Set it up... */
94 fbc_ctl2 = FBC_CTL_FENCE_DBL | FBC_CTL_IDLE_IMM | FBC_CTL_CPU_FENCE;
95 fbc_ctl2 |= plane;
96 I915_WRITE(FBC_CONTROL2, fbc_ctl2);
97 I915_WRITE(FBC_FENCE_OFF, crtc->y);
98
99 /* enable it... */
100 fbc_ctl = FBC_CTL_EN | FBC_CTL_PERIODIC;
101 if (IS_I945GM(dev))
102 fbc_ctl |= FBC_CTL_C3_IDLE; /* 945 needs special SR handling */
103 fbc_ctl |= (cfb_pitch & 0xff) << FBC_CTL_STRIDE_SHIFT;
104 fbc_ctl |= (interval & 0x2fff) << FBC_CTL_INTERVAL_SHIFT;
105 fbc_ctl |= obj->fence_reg;
106 I915_WRITE(FBC_CONTROL, fbc_ctl);
107
108 DRM_DEBUG_KMS("enabled FBC, pitch %d, yoff %d, plane %d, ",
109 cfb_pitch, crtc->y, intel_crtc->plane);
110}
111
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300112static bool i8xx_fbc_enabled(struct drm_device *dev)
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300113{
114 struct drm_i915_private *dev_priv = dev->dev_private;
115
116 return I915_READ(FBC_CONTROL) & FBC_CTL_EN;
117}
118
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300119static void g4x_enable_fbc(struct drm_crtc *crtc, unsigned long interval)
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300120{
121 struct drm_device *dev = crtc->dev;
122 struct drm_i915_private *dev_priv = dev->dev_private;
123 struct drm_framebuffer *fb = crtc->fb;
124 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
125 struct drm_i915_gem_object *obj = intel_fb->obj;
126 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
127 int plane = intel_crtc->plane == 0 ? DPFC_CTL_PLANEA : DPFC_CTL_PLANEB;
128 unsigned long stall_watermark = 200;
129 u32 dpfc_ctl;
130
131 dpfc_ctl = plane | DPFC_SR_EN | DPFC_CTL_LIMIT_1X;
132 dpfc_ctl |= DPFC_CTL_FENCE_EN | obj->fence_reg;
133 I915_WRITE(DPFC_CHICKEN, DPFC_HT_MODIFY);
134
135 I915_WRITE(DPFC_RECOMP_CTL, DPFC_RECOMP_STALL_EN |
136 (stall_watermark << DPFC_RECOMP_STALL_WM_SHIFT) |
137 (interval << DPFC_RECOMP_TIMER_COUNT_SHIFT));
138 I915_WRITE(DPFC_FENCE_YOFF, crtc->y);
139
140 /* enable it... */
141 I915_WRITE(DPFC_CONTROL, I915_READ(DPFC_CONTROL) | DPFC_CTL_EN);
142
143 DRM_DEBUG_KMS("enabled fbc on plane %d\n", intel_crtc->plane);
144}
145
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300146static void g4x_disable_fbc(struct drm_device *dev)
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300147{
148 struct drm_i915_private *dev_priv = dev->dev_private;
149 u32 dpfc_ctl;
150
151 /* Disable compression */
152 dpfc_ctl = I915_READ(DPFC_CONTROL);
153 if (dpfc_ctl & DPFC_CTL_EN) {
154 dpfc_ctl &= ~DPFC_CTL_EN;
155 I915_WRITE(DPFC_CONTROL, dpfc_ctl);
156
157 DRM_DEBUG_KMS("disabled FBC\n");
158 }
159}
160
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300161static bool g4x_fbc_enabled(struct drm_device *dev)
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300162{
163 struct drm_i915_private *dev_priv = dev->dev_private;
164
165 return I915_READ(DPFC_CONTROL) & DPFC_CTL_EN;
166}
167
168static void sandybridge_blit_fbc_update(struct drm_device *dev)
169{
170 struct drm_i915_private *dev_priv = dev->dev_private;
171 u32 blt_ecoskpd;
172
173 /* Make sure blitter notifies FBC of writes */
174 gen6_gt_force_wake_get(dev_priv);
175 blt_ecoskpd = I915_READ(GEN6_BLITTER_ECOSKPD);
176 blt_ecoskpd |= GEN6_BLITTER_FBC_NOTIFY <<
177 GEN6_BLITTER_LOCK_SHIFT;
178 I915_WRITE(GEN6_BLITTER_ECOSKPD, blt_ecoskpd);
179 blt_ecoskpd |= GEN6_BLITTER_FBC_NOTIFY;
180 I915_WRITE(GEN6_BLITTER_ECOSKPD, blt_ecoskpd);
181 blt_ecoskpd &= ~(GEN6_BLITTER_FBC_NOTIFY <<
182 GEN6_BLITTER_LOCK_SHIFT);
183 I915_WRITE(GEN6_BLITTER_ECOSKPD, blt_ecoskpd);
184 POSTING_READ(GEN6_BLITTER_ECOSKPD);
185 gen6_gt_force_wake_put(dev_priv);
186}
187
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300188static void ironlake_enable_fbc(struct drm_crtc *crtc, unsigned long interval)
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300189{
190 struct drm_device *dev = crtc->dev;
191 struct drm_i915_private *dev_priv = dev->dev_private;
192 struct drm_framebuffer *fb = crtc->fb;
193 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
194 struct drm_i915_gem_object *obj = intel_fb->obj;
195 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
196 int plane = intel_crtc->plane == 0 ? DPFC_CTL_PLANEA : DPFC_CTL_PLANEB;
197 unsigned long stall_watermark = 200;
198 u32 dpfc_ctl;
199
200 dpfc_ctl = I915_READ(ILK_DPFC_CONTROL);
201 dpfc_ctl &= DPFC_RESERVED;
202 dpfc_ctl |= (plane | DPFC_CTL_LIMIT_1X);
203 /* Set persistent mode for front-buffer rendering, ala X. */
204 dpfc_ctl |= DPFC_CTL_PERSISTENT_MODE;
205 dpfc_ctl |= (DPFC_CTL_FENCE_EN | obj->fence_reg);
206 I915_WRITE(ILK_DPFC_CHICKEN, DPFC_HT_MODIFY);
207
208 I915_WRITE(ILK_DPFC_RECOMP_CTL, DPFC_RECOMP_STALL_EN |
209 (stall_watermark << DPFC_RECOMP_STALL_WM_SHIFT) |
210 (interval << DPFC_RECOMP_TIMER_COUNT_SHIFT));
211 I915_WRITE(ILK_DPFC_FENCE_YOFF, crtc->y);
212 I915_WRITE(ILK_FBC_RT_BASE, obj->gtt_offset | ILK_FBC_RT_VALID);
213 /* enable it... */
214 I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl | DPFC_CTL_EN);
215
216 if (IS_GEN6(dev)) {
217 I915_WRITE(SNB_DPFC_CTL_SA,
218 SNB_CPU_FENCE_ENABLE | obj->fence_reg);
219 I915_WRITE(DPFC_CPU_FENCE_OFFSET, crtc->y);
220 sandybridge_blit_fbc_update(dev);
221 }
222
223 DRM_DEBUG_KMS("enabled fbc on plane %d\n", intel_crtc->plane);
224}
225
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300226static void ironlake_disable_fbc(struct drm_device *dev)
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300227{
228 struct drm_i915_private *dev_priv = dev->dev_private;
229 u32 dpfc_ctl;
230
231 /* Disable compression */
232 dpfc_ctl = I915_READ(ILK_DPFC_CONTROL);
233 if (dpfc_ctl & DPFC_CTL_EN) {
234 dpfc_ctl &= ~DPFC_CTL_EN;
235 I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl);
236
237 DRM_DEBUG_KMS("disabled FBC\n");
238 }
239}
240
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300241static bool ironlake_fbc_enabled(struct drm_device *dev)
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300242{
243 struct drm_i915_private *dev_priv = dev->dev_private;
244
245 return I915_READ(ILK_DPFC_CONTROL) & DPFC_CTL_EN;
246}
247
248bool intel_fbc_enabled(struct drm_device *dev)
249{
250 struct drm_i915_private *dev_priv = dev->dev_private;
251
252 if (!dev_priv->display.fbc_enabled)
253 return false;
254
255 return dev_priv->display.fbc_enabled(dev);
256}
257
258static void intel_fbc_work_fn(struct work_struct *__work)
259{
260 struct intel_fbc_work *work =
261 container_of(to_delayed_work(__work),
262 struct intel_fbc_work, work);
263 struct drm_device *dev = work->crtc->dev;
264 struct drm_i915_private *dev_priv = dev->dev_private;
265
266 mutex_lock(&dev->struct_mutex);
267 if (work == dev_priv->fbc_work) {
268 /* Double check that we haven't switched fb without cancelling
269 * the prior work.
270 */
271 if (work->crtc->fb == work->fb) {
272 dev_priv->display.enable_fbc(work->crtc,
273 work->interval);
274
275 dev_priv->cfb_plane = to_intel_crtc(work->crtc)->plane;
276 dev_priv->cfb_fb = work->crtc->fb->base.id;
277 dev_priv->cfb_y = work->crtc->y;
278 }
279
280 dev_priv->fbc_work = NULL;
281 }
282 mutex_unlock(&dev->struct_mutex);
283
284 kfree(work);
285}
286
287static void intel_cancel_fbc_work(struct drm_i915_private *dev_priv)
288{
289 if (dev_priv->fbc_work == NULL)
290 return;
291
292 DRM_DEBUG_KMS("cancelling pending FBC enable\n");
293
294 /* Synchronisation is provided by struct_mutex and checking of
295 * dev_priv->fbc_work, so we can perform the cancellation
296 * entirely asynchronously.
297 */
298 if (cancel_delayed_work(&dev_priv->fbc_work->work))
299 /* tasklet was killed before being run, clean up */
300 kfree(dev_priv->fbc_work);
301
302 /* Mark the work as no longer wanted so that if it does
303 * wake-up (because the work was already running and waiting
304 * for our mutex), it will discover that is no longer
305 * necessary to run.
306 */
307 dev_priv->fbc_work = NULL;
308}
309
310void intel_enable_fbc(struct drm_crtc *crtc, unsigned long interval)
311{
312 struct intel_fbc_work *work;
313 struct drm_device *dev = crtc->dev;
314 struct drm_i915_private *dev_priv = dev->dev_private;
315
316 if (!dev_priv->display.enable_fbc)
317 return;
318
319 intel_cancel_fbc_work(dev_priv);
320
321 work = kzalloc(sizeof *work, GFP_KERNEL);
322 if (work == NULL) {
323 dev_priv->display.enable_fbc(crtc, interval);
324 return;
325 }
326
327 work->crtc = crtc;
328 work->fb = crtc->fb;
329 work->interval = interval;
330 INIT_DELAYED_WORK(&work->work, intel_fbc_work_fn);
331
332 dev_priv->fbc_work = work;
333
334 DRM_DEBUG_KMS("scheduling delayed FBC enable\n");
335
336 /* Delay the actual enabling to let pageflipping cease and the
337 * display to settle before starting the compression. Note that
338 * this delay also serves a second purpose: it allows for a
339 * vblank to pass after disabling the FBC before we attempt
340 * to modify the control registers.
341 *
342 * A more complicated solution would involve tracking vblanks
343 * following the termination of the page-flipping sequence
344 * and indeed performing the enable as a co-routine and not
345 * waiting synchronously upon the vblank.
346 */
347 schedule_delayed_work(&work->work, msecs_to_jiffies(50));
348}
349
350void intel_disable_fbc(struct drm_device *dev)
351{
352 struct drm_i915_private *dev_priv = dev->dev_private;
353
354 intel_cancel_fbc_work(dev_priv);
355
356 if (!dev_priv->display.disable_fbc)
357 return;
358
359 dev_priv->display.disable_fbc(dev);
360 dev_priv->cfb_plane = -1;
361}
362
363/**
364 * intel_update_fbc - enable/disable FBC as needed
365 * @dev: the drm_device
366 *
367 * Set up the framebuffer compression hardware at mode set time. We
368 * enable it if possible:
369 * - plane A only (on pre-965)
370 * - no pixel mulitply/line duplication
371 * - no alpha buffer discard
372 * - no dual wide
373 * - framebuffer <= 2048 in width, 1536 in height
374 *
375 * We can't assume that any compression will take place (worst case),
376 * so the compressed buffer has to be the same size as the uncompressed
377 * one. It also must reside (along with the line length buffer) in
378 * stolen memory.
379 *
380 * We need to enable/disable FBC on a global basis.
381 */
382void intel_update_fbc(struct drm_device *dev)
383{
384 struct drm_i915_private *dev_priv = dev->dev_private;
385 struct drm_crtc *crtc = NULL, *tmp_crtc;
386 struct intel_crtc *intel_crtc;
387 struct drm_framebuffer *fb;
388 struct intel_framebuffer *intel_fb;
389 struct drm_i915_gem_object *obj;
390 int enable_fbc;
391
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300392 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) {
Chris Wilsonf20e0b02012-12-07 10:43:25 +0000408 if (to_intel_crtc(tmp_crtc)->active &&
Chris Wilson93314b52012-06-13 17:36:55 +0100409 !to_intel_crtc(tmp_crtc)->primary_disabled &&
410 tmp_crtc->fb) {
Eugeni Dodonov85208be2012-04-16 22:20:34 -0300411 if (crtc) {
412 DRM_DEBUG_KMS("more than one pipe active, disabling compression\n");
413 dev_priv->no_fbc_reason = FBC_MULTIPLE_PIPES;
414 goto out_disable;
415 }
416 crtc = tmp_crtc;
417 }
418 }
419
420 if (!crtc || crtc->fb == NULL) {
421 DRM_DEBUG_KMS("no output, disabling\n");
422 dev_priv->no_fbc_reason = FBC_NO_OUTPUT;
423 goto out_disable;
424 }
425
426 intel_crtc = to_intel_crtc(crtc);
427 fb = crtc->fb;
428 intel_fb = to_intel_framebuffer(fb);
429 obj = intel_fb->obj;
430
431 enable_fbc = i915_enable_fbc;
432 if (enable_fbc < 0) {
433 DRM_DEBUG_KMS("fbc set to per-chip default\n");
434 enable_fbc = 1;
435 if (INTEL_INFO(dev)->gen <= 6)
436 enable_fbc = 0;
437 }
438 if (!enable_fbc) {
439 DRM_DEBUG_KMS("fbc disabled per module param\n");
440 dev_priv->no_fbc_reason = FBC_MODULE_PARAM;
441 goto out_disable;
442 }
443 if (intel_fb->obj->base.size > dev_priv->cfb_size) {
444 DRM_DEBUG_KMS("framebuffer too large, disabling "
445 "compression\n");
446 dev_priv->no_fbc_reason = FBC_STOLEN_TOO_SMALL;
447 goto out_disable;
448 }
449 if ((crtc->mode.flags & DRM_MODE_FLAG_INTERLACE) ||
450 (crtc->mode.flags & DRM_MODE_FLAG_DBLSCAN)) {
451 DRM_DEBUG_KMS("mode incompatible with compression, "
452 "disabling\n");
453 dev_priv->no_fbc_reason = FBC_UNSUPPORTED_MODE;
454 goto out_disable;
455 }
456 if ((crtc->mode.hdisplay > 2048) ||
457 (crtc->mode.vdisplay > 1536)) {
458 DRM_DEBUG_KMS("mode too large for compression, disabling\n");
459 dev_priv->no_fbc_reason = FBC_MODE_TOO_LARGE;
460 goto out_disable;
461 }
462 if ((IS_I915GM(dev) || IS_I945GM(dev)) && intel_crtc->plane != 0) {
463 DRM_DEBUG_KMS("plane not 0, disabling compression\n");
464 dev_priv->no_fbc_reason = FBC_BAD_PLANE;
465 goto out_disable;
466 }
467
468 /* The use of a CPU fence is mandatory in order to detect writes
469 * by the CPU to the scanout and trigger updates to the FBC.
470 */
471 if (obj->tiling_mode != I915_TILING_X ||
472 obj->fence_reg == I915_FENCE_REG_NONE) {
473 DRM_DEBUG_KMS("framebuffer not tiled or fenced, disabling compression\n");
474 dev_priv->no_fbc_reason = FBC_NOT_TILED;
475 goto out_disable;
476 }
477
478 /* If the kernel debugger is active, always disable compression */
479 if (in_dbg_master())
480 goto out_disable;
481
482 /* If the scanout has not changed, don't modify the FBC settings.
483 * Note that we make the fundamental assumption that the fb->obj
484 * cannot be unpinned (and have its GTT offset and fence revoked)
485 * without first being decoupled from the scanout and FBC disabled.
486 */
487 if (dev_priv->cfb_plane == intel_crtc->plane &&
488 dev_priv->cfb_fb == fb->base.id &&
489 dev_priv->cfb_y == crtc->y)
490 return;
491
492 if (intel_fbc_enabled(dev)) {
493 /* We update FBC along two paths, after changing fb/crtc
494 * configuration (modeswitching) and after page-flipping
495 * finishes. For the latter, we know that not only did
496 * we disable the FBC at the start of the page-flip
497 * sequence, but also more than one vblank has passed.
498 *
499 * For the former case of modeswitching, it is possible
500 * to switch between two FBC valid configurations
501 * instantaneously so we do need to disable the FBC
502 * before we can modify its control registers. We also
503 * have to wait for the next vblank for that to take
504 * effect. However, since we delay enabling FBC we can
505 * assume that a vblank has passed since disabling and
506 * that we can safely alter the registers in the deferred
507 * callback.
508 *
509 * In the scenario that we go from a valid to invalid
510 * and then back to valid FBC configuration we have
511 * no strict enforcement that a vblank occurred since
512 * disabling the FBC. However, along all current pipe
513 * disabling paths we do need to wait for a vblank at
514 * some point. And we wait before enabling FBC anyway.
515 */
516 DRM_DEBUG_KMS("disabling active FBC for update\n");
517 intel_disable_fbc(dev);
518 }
519
520 intel_enable_fbc(crtc, 500);
521 return;
522
523out_disable:
524 /* Multiple disables should be harmless */
525 if (intel_fbc_enabled(dev)) {
526 DRM_DEBUG_KMS("unsupported config, disabling FBC\n");
527 intel_disable_fbc(dev);
528 }
529}
530
Daniel Vetterc921aba2012-04-26 23:28:17 +0200531static void i915_pineview_get_mem_freq(struct drm_device *dev)
532{
533 drm_i915_private_t *dev_priv = dev->dev_private;
534 u32 tmp;
535
536 tmp = I915_READ(CLKCFG);
537
538 switch (tmp & CLKCFG_FSB_MASK) {
539 case CLKCFG_FSB_533:
540 dev_priv->fsb_freq = 533; /* 133*4 */
541 break;
542 case CLKCFG_FSB_800:
543 dev_priv->fsb_freq = 800; /* 200*4 */
544 break;
545 case CLKCFG_FSB_667:
546 dev_priv->fsb_freq = 667; /* 167*4 */
547 break;
548 case CLKCFG_FSB_400:
549 dev_priv->fsb_freq = 400; /* 100*4 */
550 break;
551 }
552
553 switch (tmp & CLKCFG_MEM_MASK) {
554 case CLKCFG_MEM_533:
555 dev_priv->mem_freq = 533;
556 break;
557 case CLKCFG_MEM_667:
558 dev_priv->mem_freq = 667;
559 break;
560 case CLKCFG_MEM_800:
561 dev_priv->mem_freq = 800;
562 break;
563 }
564
565 /* detect pineview DDR3 setting */
566 tmp = I915_READ(CSHRDDR3CTL);
567 dev_priv->is_ddr3 = (tmp & CSHRDDR3CTL_DDR3) ? 1 : 0;
568}
569
570static void i915_ironlake_get_mem_freq(struct drm_device *dev)
571{
572 drm_i915_private_t *dev_priv = dev->dev_private;
573 u16 ddrpll, csipll;
574
575 ddrpll = I915_READ16(DDRMPLL1);
576 csipll = I915_READ16(CSIPLL0);
577
578 switch (ddrpll & 0xff) {
579 case 0xc:
580 dev_priv->mem_freq = 800;
581 break;
582 case 0x10:
583 dev_priv->mem_freq = 1066;
584 break;
585 case 0x14:
586 dev_priv->mem_freq = 1333;
587 break;
588 case 0x18:
589 dev_priv->mem_freq = 1600;
590 break;
591 default:
592 DRM_DEBUG_DRIVER("unknown memory frequency 0x%02x\n",
593 ddrpll & 0xff);
594 dev_priv->mem_freq = 0;
595 break;
596 }
597
Daniel Vetter20e4d402012-08-08 23:35:39 +0200598 dev_priv->ips.r_t = dev_priv->mem_freq;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200599
600 switch (csipll & 0x3ff) {
601 case 0x00c:
602 dev_priv->fsb_freq = 3200;
603 break;
604 case 0x00e:
605 dev_priv->fsb_freq = 3733;
606 break;
607 case 0x010:
608 dev_priv->fsb_freq = 4266;
609 break;
610 case 0x012:
611 dev_priv->fsb_freq = 4800;
612 break;
613 case 0x014:
614 dev_priv->fsb_freq = 5333;
615 break;
616 case 0x016:
617 dev_priv->fsb_freq = 5866;
618 break;
619 case 0x018:
620 dev_priv->fsb_freq = 6400;
621 break;
622 default:
623 DRM_DEBUG_DRIVER("unknown fsb frequency 0x%04x\n",
624 csipll & 0x3ff);
625 dev_priv->fsb_freq = 0;
626 break;
627 }
628
629 if (dev_priv->fsb_freq == 3200) {
Daniel Vetter20e4d402012-08-08 23:35:39 +0200630 dev_priv->ips.c_m = 0;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200631 } else if (dev_priv->fsb_freq > 3200 && dev_priv->fsb_freq <= 4800) {
Daniel Vetter20e4d402012-08-08 23:35:39 +0200632 dev_priv->ips.c_m = 1;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200633 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +0200634 dev_priv->ips.c_m = 2;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200635 }
636}
637
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300638static const struct cxsr_latency cxsr_latency_table[] = {
639 {1, 0, 800, 400, 3382, 33382, 3983, 33983}, /* DDR2-400 SC */
640 {1, 0, 800, 667, 3354, 33354, 3807, 33807}, /* DDR2-667 SC */
641 {1, 0, 800, 800, 3347, 33347, 3763, 33763}, /* DDR2-800 SC */
642 {1, 1, 800, 667, 6420, 36420, 6873, 36873}, /* DDR3-667 SC */
643 {1, 1, 800, 800, 5902, 35902, 6318, 36318}, /* DDR3-800 SC */
644
645 {1, 0, 667, 400, 3400, 33400, 4021, 34021}, /* DDR2-400 SC */
646 {1, 0, 667, 667, 3372, 33372, 3845, 33845}, /* DDR2-667 SC */
647 {1, 0, 667, 800, 3386, 33386, 3822, 33822}, /* DDR2-800 SC */
648 {1, 1, 667, 667, 6438, 36438, 6911, 36911}, /* DDR3-667 SC */
649 {1, 1, 667, 800, 5941, 35941, 6377, 36377}, /* DDR3-800 SC */
650
651 {1, 0, 400, 400, 3472, 33472, 4173, 34173}, /* DDR2-400 SC */
652 {1, 0, 400, 667, 3443, 33443, 3996, 33996}, /* DDR2-667 SC */
653 {1, 0, 400, 800, 3430, 33430, 3946, 33946}, /* DDR2-800 SC */
654 {1, 1, 400, 667, 6509, 36509, 7062, 37062}, /* DDR3-667 SC */
655 {1, 1, 400, 800, 5985, 35985, 6501, 36501}, /* DDR3-800 SC */
656
657 {0, 0, 800, 400, 3438, 33438, 4065, 34065}, /* DDR2-400 SC */
658 {0, 0, 800, 667, 3410, 33410, 3889, 33889}, /* DDR2-667 SC */
659 {0, 0, 800, 800, 3403, 33403, 3845, 33845}, /* DDR2-800 SC */
660 {0, 1, 800, 667, 6476, 36476, 6955, 36955}, /* DDR3-667 SC */
661 {0, 1, 800, 800, 5958, 35958, 6400, 36400}, /* DDR3-800 SC */
662
663 {0, 0, 667, 400, 3456, 33456, 4103, 34106}, /* DDR2-400 SC */
664 {0, 0, 667, 667, 3428, 33428, 3927, 33927}, /* DDR2-667 SC */
665 {0, 0, 667, 800, 3443, 33443, 3905, 33905}, /* DDR2-800 SC */
666 {0, 1, 667, 667, 6494, 36494, 6993, 36993}, /* DDR3-667 SC */
667 {0, 1, 667, 800, 5998, 35998, 6460, 36460}, /* DDR3-800 SC */
668
669 {0, 0, 400, 400, 3528, 33528, 4255, 34255}, /* DDR2-400 SC */
670 {0, 0, 400, 667, 3500, 33500, 4079, 34079}, /* DDR2-667 SC */
671 {0, 0, 400, 800, 3487, 33487, 4029, 34029}, /* DDR2-800 SC */
672 {0, 1, 400, 667, 6566, 36566, 7145, 37145}, /* DDR3-667 SC */
673 {0, 1, 400, 800, 6042, 36042, 6584, 36584}, /* DDR3-800 SC */
674};
675
Daniel Vetter63c62272012-04-21 23:17:55 +0200676static const struct cxsr_latency *intel_get_cxsr_latency(int is_desktop,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300677 int is_ddr3,
678 int fsb,
679 int mem)
680{
681 const struct cxsr_latency *latency;
682 int i;
683
684 if (fsb == 0 || mem == 0)
685 return NULL;
686
687 for (i = 0; i < ARRAY_SIZE(cxsr_latency_table); i++) {
688 latency = &cxsr_latency_table[i];
689 if (is_desktop == latency->is_desktop &&
690 is_ddr3 == latency->is_ddr3 &&
691 fsb == latency->fsb_freq && mem == latency->mem_freq)
692 return latency;
693 }
694
695 DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
696
697 return NULL;
698}
699
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300700static void pineview_disable_cxsr(struct drm_device *dev)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300701{
702 struct drm_i915_private *dev_priv = dev->dev_private;
703
704 /* deactivate cxsr */
705 I915_WRITE(DSPFW3, I915_READ(DSPFW3) & ~PINEVIEW_SELF_REFRESH_EN);
706}
707
708/*
709 * Latency for FIFO fetches is dependent on several factors:
710 * - memory configuration (speed, channels)
711 * - chipset
712 * - current MCH state
713 * It can be fairly high in some situations, so here we assume a fairly
714 * pessimal value. It's a tradeoff between extra memory fetches (if we
715 * set this value too high, the FIFO will fetch frequently to stay full)
716 * and power consumption (set it too low to save power and we might see
717 * FIFO underruns and display "flicker").
718 *
719 * A value of 5us seems to be a good balance; safe for very low end
720 * platforms but not overly aggressive on lower latency configs.
721 */
722static const int latency_ns = 5000;
723
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300724static int i9xx_get_fifo_size(struct drm_device *dev, int plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300725{
726 struct drm_i915_private *dev_priv = dev->dev_private;
727 uint32_t dsparb = I915_READ(DSPARB);
728 int size;
729
730 size = dsparb & 0x7f;
731 if (plane)
732 size = ((dsparb >> DSPARB_CSTART_SHIFT) & 0x7f) - size;
733
734 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
735 plane ? "B" : "A", size);
736
737 return size;
738}
739
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300740static int i85x_get_fifo_size(struct drm_device *dev, int plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300741{
742 struct drm_i915_private *dev_priv = dev->dev_private;
743 uint32_t dsparb = I915_READ(DSPARB);
744 int size;
745
746 size = dsparb & 0x1ff;
747 if (plane)
748 size = ((dsparb >> DSPARB_BEND_SHIFT) & 0x1ff) - size;
749 size >>= 1; /* Convert to cachelines */
750
751 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
752 plane ? "B" : "A", size);
753
754 return size;
755}
756
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300757static int i845_get_fifo_size(struct drm_device *dev, int plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300758{
759 struct drm_i915_private *dev_priv = dev->dev_private;
760 uint32_t dsparb = I915_READ(DSPARB);
761 int size;
762
763 size = dsparb & 0x7f;
764 size >>= 2; /* Convert to cachelines */
765
766 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
767 plane ? "B" : "A",
768 size);
769
770 return size;
771}
772
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300773static int i830_get_fifo_size(struct drm_device *dev, int plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300774{
775 struct drm_i915_private *dev_priv = dev->dev_private;
776 uint32_t dsparb = I915_READ(DSPARB);
777 int size;
778
779 size = dsparb & 0x7f;
780 size >>= 1; /* Convert to cachelines */
781
782 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
783 plane ? "B" : "A", size);
784
785 return size;
786}
787
788/* Pineview has different values for various configs */
789static const struct intel_watermark_params pineview_display_wm = {
790 PINEVIEW_DISPLAY_FIFO,
791 PINEVIEW_MAX_WM,
792 PINEVIEW_DFT_WM,
793 PINEVIEW_GUARD_WM,
794 PINEVIEW_FIFO_LINE_SIZE
795};
796static const struct intel_watermark_params pineview_display_hplloff_wm = {
797 PINEVIEW_DISPLAY_FIFO,
798 PINEVIEW_MAX_WM,
799 PINEVIEW_DFT_HPLLOFF_WM,
800 PINEVIEW_GUARD_WM,
801 PINEVIEW_FIFO_LINE_SIZE
802};
803static const struct intel_watermark_params pineview_cursor_wm = {
804 PINEVIEW_CURSOR_FIFO,
805 PINEVIEW_CURSOR_MAX_WM,
806 PINEVIEW_CURSOR_DFT_WM,
807 PINEVIEW_CURSOR_GUARD_WM,
808 PINEVIEW_FIFO_LINE_SIZE,
809};
810static const struct intel_watermark_params pineview_cursor_hplloff_wm = {
811 PINEVIEW_CURSOR_FIFO,
812 PINEVIEW_CURSOR_MAX_WM,
813 PINEVIEW_CURSOR_DFT_WM,
814 PINEVIEW_CURSOR_GUARD_WM,
815 PINEVIEW_FIFO_LINE_SIZE
816};
817static const struct intel_watermark_params g4x_wm_info = {
818 G4X_FIFO_SIZE,
819 G4X_MAX_WM,
820 G4X_MAX_WM,
821 2,
822 G4X_FIFO_LINE_SIZE,
823};
824static const struct intel_watermark_params g4x_cursor_wm_info = {
825 I965_CURSOR_FIFO,
826 I965_CURSOR_MAX_WM,
827 I965_CURSOR_DFT_WM,
828 2,
829 G4X_FIFO_LINE_SIZE,
830};
831static const struct intel_watermark_params valleyview_wm_info = {
832 VALLEYVIEW_FIFO_SIZE,
833 VALLEYVIEW_MAX_WM,
834 VALLEYVIEW_MAX_WM,
835 2,
836 G4X_FIFO_LINE_SIZE,
837};
838static const struct intel_watermark_params valleyview_cursor_wm_info = {
839 I965_CURSOR_FIFO,
840 VALLEYVIEW_CURSOR_MAX_WM,
841 I965_CURSOR_DFT_WM,
842 2,
843 G4X_FIFO_LINE_SIZE,
844};
845static const struct intel_watermark_params i965_cursor_wm_info = {
846 I965_CURSOR_FIFO,
847 I965_CURSOR_MAX_WM,
848 I965_CURSOR_DFT_WM,
849 2,
850 I915_FIFO_LINE_SIZE,
851};
852static const struct intel_watermark_params i945_wm_info = {
853 I945_FIFO_SIZE,
854 I915_MAX_WM,
855 1,
856 2,
857 I915_FIFO_LINE_SIZE
858};
859static const struct intel_watermark_params i915_wm_info = {
860 I915_FIFO_SIZE,
861 I915_MAX_WM,
862 1,
863 2,
864 I915_FIFO_LINE_SIZE
865};
866static const struct intel_watermark_params i855_wm_info = {
867 I855GM_FIFO_SIZE,
868 I915_MAX_WM,
869 1,
870 2,
871 I830_FIFO_LINE_SIZE
872};
873static const struct intel_watermark_params i830_wm_info = {
874 I830_FIFO_SIZE,
875 I915_MAX_WM,
876 1,
877 2,
878 I830_FIFO_LINE_SIZE
879};
880
881static const struct intel_watermark_params ironlake_display_wm_info = {
882 ILK_DISPLAY_FIFO,
883 ILK_DISPLAY_MAXWM,
884 ILK_DISPLAY_DFTWM,
885 2,
886 ILK_FIFO_LINE_SIZE
887};
888static const struct intel_watermark_params ironlake_cursor_wm_info = {
889 ILK_CURSOR_FIFO,
890 ILK_CURSOR_MAXWM,
891 ILK_CURSOR_DFTWM,
892 2,
893 ILK_FIFO_LINE_SIZE
894};
895static const struct intel_watermark_params ironlake_display_srwm_info = {
896 ILK_DISPLAY_SR_FIFO,
897 ILK_DISPLAY_MAX_SRWM,
898 ILK_DISPLAY_DFT_SRWM,
899 2,
900 ILK_FIFO_LINE_SIZE
901};
902static const struct intel_watermark_params ironlake_cursor_srwm_info = {
903 ILK_CURSOR_SR_FIFO,
904 ILK_CURSOR_MAX_SRWM,
905 ILK_CURSOR_DFT_SRWM,
906 2,
907 ILK_FIFO_LINE_SIZE
908};
909
910static const struct intel_watermark_params sandybridge_display_wm_info = {
911 SNB_DISPLAY_FIFO,
912 SNB_DISPLAY_MAXWM,
913 SNB_DISPLAY_DFTWM,
914 2,
915 SNB_FIFO_LINE_SIZE
916};
917static const struct intel_watermark_params sandybridge_cursor_wm_info = {
918 SNB_CURSOR_FIFO,
919 SNB_CURSOR_MAXWM,
920 SNB_CURSOR_DFTWM,
921 2,
922 SNB_FIFO_LINE_SIZE
923};
924static const struct intel_watermark_params sandybridge_display_srwm_info = {
925 SNB_DISPLAY_SR_FIFO,
926 SNB_DISPLAY_MAX_SRWM,
927 SNB_DISPLAY_DFT_SRWM,
928 2,
929 SNB_FIFO_LINE_SIZE
930};
931static const struct intel_watermark_params sandybridge_cursor_srwm_info = {
932 SNB_CURSOR_SR_FIFO,
933 SNB_CURSOR_MAX_SRWM,
934 SNB_CURSOR_DFT_SRWM,
935 2,
936 SNB_FIFO_LINE_SIZE
937};
938
939
940/**
941 * intel_calculate_wm - calculate watermark level
942 * @clock_in_khz: pixel clock
943 * @wm: chip FIFO params
944 * @pixel_size: display pixel size
945 * @latency_ns: memory latency for the platform
946 *
947 * Calculate the watermark level (the level at which the display plane will
948 * start fetching from memory again). Each chip has a different display
949 * FIFO size and allocation, so the caller needs to figure that out and pass
950 * in the correct intel_watermark_params structure.
951 *
952 * As the pixel clock runs, the FIFO will be drained at a rate that depends
953 * on the pixel size. When it reaches the watermark level, it'll start
954 * fetching FIFO line sized based chunks from memory until the FIFO fills
955 * past the watermark point. If the FIFO drains completely, a FIFO underrun
956 * will occur, and a display engine hang could result.
957 */
958static unsigned long intel_calculate_wm(unsigned long clock_in_khz,
959 const struct intel_watermark_params *wm,
960 int fifo_size,
961 int pixel_size,
962 unsigned long latency_ns)
963{
964 long entries_required, wm_size;
965
966 /*
967 * Note: we need to make sure we don't overflow for various clock &
968 * latency values.
969 * clocks go from a few thousand to several hundred thousand.
970 * latency is usually a few thousand
971 */
972 entries_required = ((clock_in_khz / 1000) * pixel_size * latency_ns) /
973 1000;
974 entries_required = DIV_ROUND_UP(entries_required, wm->cacheline_size);
975
976 DRM_DEBUG_KMS("FIFO entries required for mode: %ld\n", entries_required);
977
978 wm_size = fifo_size - (entries_required + wm->guard_size);
979
980 DRM_DEBUG_KMS("FIFO watermark level: %ld\n", wm_size);
981
982 /* Don't promote wm_size to unsigned... */
983 if (wm_size > (long)wm->max_wm)
984 wm_size = wm->max_wm;
985 if (wm_size <= 0)
986 wm_size = wm->default_wm;
987 return wm_size;
988}
989
990static struct drm_crtc *single_enabled_crtc(struct drm_device *dev)
991{
992 struct drm_crtc *crtc, *enabled = NULL;
993
994 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Chris Wilsonf20e0b02012-12-07 10:43:25 +0000995 if (to_intel_crtc(crtc)->active && crtc->fb) {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300996 if (enabled)
997 return NULL;
998 enabled = crtc;
999 }
1000 }
1001
1002 return enabled;
1003}
1004
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03001005static void pineview_update_wm(struct drm_device *dev)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001006{
1007 struct drm_i915_private *dev_priv = dev->dev_private;
1008 struct drm_crtc *crtc;
1009 const struct cxsr_latency *latency;
1010 u32 reg;
1011 unsigned long wm;
1012
1013 latency = intel_get_cxsr_latency(IS_PINEVIEW_G(dev), dev_priv->is_ddr3,
1014 dev_priv->fsb_freq, dev_priv->mem_freq);
1015 if (!latency) {
1016 DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
1017 pineview_disable_cxsr(dev);
1018 return;
1019 }
1020
1021 crtc = single_enabled_crtc(dev);
1022 if (crtc) {
1023 int clock = crtc->mode.clock;
1024 int pixel_size = crtc->fb->bits_per_pixel / 8;
1025
1026 /* Display SR */
1027 wm = intel_calculate_wm(clock, &pineview_display_wm,
1028 pineview_display_wm.fifo_size,
1029 pixel_size, latency->display_sr);
1030 reg = I915_READ(DSPFW1);
1031 reg &= ~DSPFW_SR_MASK;
1032 reg |= wm << DSPFW_SR_SHIFT;
1033 I915_WRITE(DSPFW1, reg);
1034 DRM_DEBUG_KMS("DSPFW1 register is %x\n", reg);
1035
1036 /* cursor SR */
1037 wm = intel_calculate_wm(clock, &pineview_cursor_wm,
1038 pineview_display_wm.fifo_size,
1039 pixel_size, latency->cursor_sr);
1040 reg = I915_READ(DSPFW3);
1041 reg &= ~DSPFW_CURSOR_SR_MASK;
1042 reg |= (wm & 0x3f) << DSPFW_CURSOR_SR_SHIFT;
1043 I915_WRITE(DSPFW3, reg);
1044
1045 /* Display HPLL off SR */
1046 wm = intel_calculate_wm(clock, &pineview_display_hplloff_wm,
1047 pineview_display_hplloff_wm.fifo_size,
1048 pixel_size, latency->display_hpll_disable);
1049 reg = I915_READ(DSPFW3);
1050 reg &= ~DSPFW_HPLL_SR_MASK;
1051 reg |= wm & DSPFW_HPLL_SR_MASK;
1052 I915_WRITE(DSPFW3, reg);
1053
1054 /* cursor HPLL off SR */
1055 wm = intel_calculate_wm(clock, &pineview_cursor_hplloff_wm,
1056 pineview_display_hplloff_wm.fifo_size,
1057 pixel_size, latency->cursor_hpll_disable);
1058 reg = I915_READ(DSPFW3);
1059 reg &= ~DSPFW_HPLL_CURSOR_MASK;
1060 reg |= (wm & 0x3f) << DSPFW_HPLL_CURSOR_SHIFT;
1061 I915_WRITE(DSPFW3, reg);
1062 DRM_DEBUG_KMS("DSPFW3 register is %x\n", reg);
1063
1064 /* activate cxsr */
1065 I915_WRITE(DSPFW3,
1066 I915_READ(DSPFW3) | PINEVIEW_SELF_REFRESH_EN);
1067 DRM_DEBUG_KMS("Self-refresh is enabled\n");
1068 } else {
1069 pineview_disable_cxsr(dev);
1070 DRM_DEBUG_KMS("Self-refresh is disabled\n");
1071 }
1072}
1073
1074static bool g4x_compute_wm0(struct drm_device *dev,
1075 int plane,
1076 const struct intel_watermark_params *display,
1077 int display_latency_ns,
1078 const struct intel_watermark_params *cursor,
1079 int cursor_latency_ns,
1080 int *plane_wm,
1081 int *cursor_wm)
1082{
1083 struct drm_crtc *crtc;
1084 int htotal, hdisplay, clock, pixel_size;
1085 int line_time_us, line_count;
1086 int entries, tlb_miss;
1087
1088 crtc = intel_get_crtc_for_plane(dev, plane);
Chris Wilsonf20e0b02012-12-07 10:43:25 +00001089 if (crtc->fb == NULL || !to_intel_crtc(crtc)->active) {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001090 *cursor_wm = cursor->guard_size;
1091 *plane_wm = display->guard_size;
1092 return false;
1093 }
1094
1095 htotal = crtc->mode.htotal;
1096 hdisplay = crtc->mode.hdisplay;
1097 clock = crtc->mode.clock;
1098 pixel_size = crtc->fb->bits_per_pixel / 8;
1099
1100 /* Use the small buffer method to calculate plane watermark */
1101 entries = ((clock * pixel_size / 1000) * display_latency_ns) / 1000;
1102 tlb_miss = display->fifo_size*display->cacheline_size - hdisplay * 8;
1103 if (tlb_miss > 0)
1104 entries += tlb_miss;
1105 entries = DIV_ROUND_UP(entries, display->cacheline_size);
1106 *plane_wm = entries + display->guard_size;
1107 if (*plane_wm > (int)display->max_wm)
1108 *plane_wm = display->max_wm;
1109
1110 /* Use the large buffer method to calculate cursor watermark */
1111 line_time_us = ((htotal * 1000) / clock);
1112 line_count = (cursor_latency_ns / line_time_us + 1000) / 1000;
1113 entries = line_count * 64 * pixel_size;
1114 tlb_miss = cursor->fifo_size*cursor->cacheline_size - hdisplay * 8;
1115 if (tlb_miss > 0)
1116 entries += tlb_miss;
1117 entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
1118 *cursor_wm = entries + cursor->guard_size;
1119 if (*cursor_wm > (int)cursor->max_wm)
1120 *cursor_wm = (int)cursor->max_wm;
1121
1122 return true;
1123}
1124
1125/*
1126 * Check the wm result.
1127 *
1128 * If any calculated watermark values is larger than the maximum value that
1129 * can be programmed into the associated watermark register, that watermark
1130 * must be disabled.
1131 */
1132static bool g4x_check_srwm(struct drm_device *dev,
1133 int display_wm, int cursor_wm,
1134 const struct intel_watermark_params *display,
1135 const struct intel_watermark_params *cursor)
1136{
1137 DRM_DEBUG_KMS("SR watermark: display plane %d, cursor %d\n",
1138 display_wm, cursor_wm);
1139
1140 if (display_wm > display->max_wm) {
1141 DRM_DEBUG_KMS("display watermark is too large(%d/%ld), disabling\n",
1142 display_wm, display->max_wm);
1143 return false;
1144 }
1145
1146 if (cursor_wm > cursor->max_wm) {
1147 DRM_DEBUG_KMS("cursor watermark is too large(%d/%ld), disabling\n",
1148 cursor_wm, cursor->max_wm);
1149 return false;
1150 }
1151
1152 if (!(display_wm || cursor_wm)) {
1153 DRM_DEBUG_KMS("SR latency is 0, disabling\n");
1154 return false;
1155 }
1156
1157 return true;
1158}
1159
1160static bool g4x_compute_srwm(struct drm_device *dev,
1161 int plane,
1162 int latency_ns,
1163 const struct intel_watermark_params *display,
1164 const struct intel_watermark_params *cursor,
1165 int *display_wm, int *cursor_wm)
1166{
1167 struct drm_crtc *crtc;
1168 int hdisplay, htotal, pixel_size, clock;
1169 unsigned long line_time_us;
1170 int line_count, line_size;
1171 int small, large;
1172 int entries;
1173
1174 if (!latency_ns) {
1175 *display_wm = *cursor_wm = 0;
1176 return false;
1177 }
1178
1179 crtc = intel_get_crtc_for_plane(dev, plane);
1180 hdisplay = crtc->mode.hdisplay;
1181 htotal = crtc->mode.htotal;
1182 clock = crtc->mode.clock;
1183 pixel_size = crtc->fb->bits_per_pixel / 8;
1184
1185 line_time_us = (htotal * 1000) / clock;
1186 line_count = (latency_ns / line_time_us + 1000) / 1000;
1187 line_size = hdisplay * pixel_size;
1188
1189 /* Use the minimum of the small and large buffer method for primary */
1190 small = ((clock * pixel_size / 1000) * latency_ns) / 1000;
1191 large = line_count * line_size;
1192
1193 entries = DIV_ROUND_UP(min(small, large), display->cacheline_size);
1194 *display_wm = entries + display->guard_size;
1195
1196 /* calculate the self-refresh watermark for display cursor */
1197 entries = line_count * pixel_size * 64;
1198 entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
1199 *cursor_wm = entries + cursor->guard_size;
1200
1201 return g4x_check_srwm(dev,
1202 *display_wm, *cursor_wm,
1203 display, cursor);
1204}
1205
1206static bool vlv_compute_drain_latency(struct drm_device *dev,
1207 int plane,
1208 int *plane_prec_mult,
1209 int *plane_dl,
1210 int *cursor_prec_mult,
1211 int *cursor_dl)
1212{
1213 struct drm_crtc *crtc;
1214 int clock, pixel_size;
1215 int entries;
1216
1217 crtc = intel_get_crtc_for_plane(dev, plane);
Chris Wilsonf20e0b02012-12-07 10:43:25 +00001218 if (crtc->fb == NULL || !to_intel_crtc(crtc)->active)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001219 return false;
1220
1221 clock = crtc->mode.clock; /* VESA DOT Clock */
1222 pixel_size = crtc->fb->bits_per_pixel / 8; /* BPP */
1223
1224 entries = (clock / 1000) * pixel_size;
1225 *plane_prec_mult = (entries > 256) ?
1226 DRAIN_LATENCY_PRECISION_32 : DRAIN_LATENCY_PRECISION_16;
1227 *plane_dl = (64 * (*plane_prec_mult) * 4) / ((clock / 1000) *
1228 pixel_size);
1229
1230 entries = (clock / 1000) * 4; /* BPP is always 4 for cursor */
1231 *cursor_prec_mult = (entries > 256) ?
1232 DRAIN_LATENCY_PRECISION_32 : DRAIN_LATENCY_PRECISION_16;
1233 *cursor_dl = (64 * (*cursor_prec_mult) * 4) / ((clock / 1000) * 4);
1234
1235 return true;
1236}
1237
1238/*
1239 * Update drain latency registers of memory arbiter
1240 *
1241 * Valleyview SoC has a new memory arbiter and needs drain latency registers
1242 * to be programmed. Each plane has a drain latency multiplier and a drain
1243 * latency value.
1244 */
1245
1246static void vlv_update_drain_latency(struct drm_device *dev)
1247{
1248 struct drm_i915_private *dev_priv = dev->dev_private;
1249 int planea_prec, planea_dl, planeb_prec, planeb_dl;
1250 int cursora_prec, cursora_dl, cursorb_prec, cursorb_dl;
1251 int plane_prec_mult, cursor_prec_mult; /* Precision multiplier is
1252 either 16 or 32 */
1253
1254 /* For plane A, Cursor A */
1255 if (vlv_compute_drain_latency(dev, 0, &plane_prec_mult, &planea_dl,
1256 &cursor_prec_mult, &cursora_dl)) {
1257 cursora_prec = (cursor_prec_mult == DRAIN_LATENCY_PRECISION_32) ?
1258 DDL_CURSORA_PRECISION_32 : DDL_CURSORA_PRECISION_16;
1259 planea_prec = (plane_prec_mult == DRAIN_LATENCY_PRECISION_32) ?
1260 DDL_PLANEA_PRECISION_32 : DDL_PLANEA_PRECISION_16;
1261
1262 I915_WRITE(VLV_DDL1, cursora_prec |
1263 (cursora_dl << DDL_CURSORA_SHIFT) |
1264 planea_prec | planea_dl);
1265 }
1266
1267 /* For plane B, Cursor B */
1268 if (vlv_compute_drain_latency(dev, 1, &plane_prec_mult, &planeb_dl,
1269 &cursor_prec_mult, &cursorb_dl)) {
1270 cursorb_prec = (cursor_prec_mult == DRAIN_LATENCY_PRECISION_32) ?
1271 DDL_CURSORB_PRECISION_32 : DDL_CURSORB_PRECISION_16;
1272 planeb_prec = (plane_prec_mult == DRAIN_LATENCY_PRECISION_32) ?
1273 DDL_PLANEB_PRECISION_32 : DDL_PLANEB_PRECISION_16;
1274
1275 I915_WRITE(VLV_DDL2, cursorb_prec |
1276 (cursorb_dl << DDL_CURSORB_SHIFT) |
1277 planeb_prec | planeb_dl);
1278 }
1279}
1280
1281#define single_plane_enabled(mask) is_power_of_2(mask)
1282
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03001283static void valleyview_update_wm(struct drm_device *dev)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001284{
1285 static const int sr_latency_ns = 12000;
1286 struct drm_i915_private *dev_priv = dev->dev_private;
1287 int planea_wm, planeb_wm, cursora_wm, cursorb_wm;
1288 int plane_sr, cursor_sr;
Chris Wilsonaf6c4572012-12-11 12:01:43 +00001289 int ignore_plane_sr, ignore_cursor_sr;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001290 unsigned int enabled = 0;
1291
1292 vlv_update_drain_latency(dev);
1293
1294 if (g4x_compute_wm0(dev, 0,
1295 &valleyview_wm_info, latency_ns,
1296 &valleyview_cursor_wm_info, latency_ns,
1297 &planea_wm, &cursora_wm))
1298 enabled |= 1;
1299
1300 if (g4x_compute_wm0(dev, 1,
1301 &valleyview_wm_info, latency_ns,
1302 &valleyview_cursor_wm_info, latency_ns,
1303 &planeb_wm, &cursorb_wm))
1304 enabled |= 2;
1305
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001306 if (single_plane_enabled(enabled) &&
1307 g4x_compute_srwm(dev, ffs(enabled) - 1,
1308 sr_latency_ns,
1309 &valleyview_wm_info,
1310 &valleyview_cursor_wm_info,
Chris Wilsonaf6c4572012-12-11 12:01:43 +00001311 &plane_sr, &ignore_cursor_sr) &&
1312 g4x_compute_srwm(dev, ffs(enabled) - 1,
1313 2*sr_latency_ns,
1314 &valleyview_wm_info,
1315 &valleyview_cursor_wm_info,
Chris Wilson52bd02d2012-12-07 10:43:24 +00001316 &ignore_plane_sr, &cursor_sr)) {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001317 I915_WRITE(FW_BLC_SELF_VLV, FW_CSPWRDWNEN);
Chris Wilson52bd02d2012-12-07 10:43:24 +00001318 } else {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001319 I915_WRITE(FW_BLC_SELF_VLV,
1320 I915_READ(FW_BLC_SELF_VLV) & ~FW_CSPWRDWNEN);
Chris Wilson52bd02d2012-12-07 10:43:24 +00001321 plane_sr = cursor_sr = 0;
1322 }
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001323
1324 DRM_DEBUG_KMS("Setting FIFO watermarks - A: plane=%d, cursor=%d, B: plane=%d, cursor=%d, SR: plane=%d, cursor=%d\n",
1325 planea_wm, cursora_wm,
1326 planeb_wm, cursorb_wm,
1327 plane_sr, cursor_sr);
1328
1329 I915_WRITE(DSPFW1,
1330 (plane_sr << DSPFW_SR_SHIFT) |
1331 (cursorb_wm << DSPFW_CURSORB_SHIFT) |
1332 (planeb_wm << DSPFW_PLANEB_SHIFT) |
1333 planea_wm);
1334 I915_WRITE(DSPFW2,
Chris Wilson8c919b22012-12-04 16:33:19 +00001335 (I915_READ(DSPFW2) & ~DSPFW_CURSORA_MASK) |
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001336 (cursora_wm << DSPFW_CURSORA_SHIFT));
1337 I915_WRITE(DSPFW3,
Chris Wilson8c919b22012-12-04 16:33:19 +00001338 (I915_READ(DSPFW3) & ~DSPFW_CURSOR_SR_MASK) |
1339 (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001340}
1341
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03001342static void g4x_update_wm(struct drm_device *dev)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001343{
1344 static const int sr_latency_ns = 12000;
1345 struct drm_i915_private *dev_priv = dev->dev_private;
1346 int planea_wm, planeb_wm, cursora_wm, cursorb_wm;
1347 int plane_sr, cursor_sr;
1348 unsigned int enabled = 0;
1349
1350 if (g4x_compute_wm0(dev, 0,
1351 &g4x_wm_info, latency_ns,
1352 &g4x_cursor_wm_info, latency_ns,
1353 &planea_wm, &cursora_wm))
1354 enabled |= 1;
1355
1356 if (g4x_compute_wm0(dev, 1,
1357 &g4x_wm_info, latency_ns,
1358 &g4x_cursor_wm_info, latency_ns,
1359 &planeb_wm, &cursorb_wm))
1360 enabled |= 2;
1361
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001362 if (single_plane_enabled(enabled) &&
1363 g4x_compute_srwm(dev, ffs(enabled) - 1,
1364 sr_latency_ns,
1365 &g4x_wm_info,
1366 &g4x_cursor_wm_info,
Chris Wilson52bd02d2012-12-07 10:43:24 +00001367 &plane_sr, &cursor_sr)) {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001368 I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_EN);
Chris Wilson52bd02d2012-12-07 10:43:24 +00001369 } else {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001370 I915_WRITE(FW_BLC_SELF,
1371 I915_READ(FW_BLC_SELF) & ~FW_BLC_SELF_EN);
Chris Wilson52bd02d2012-12-07 10:43:24 +00001372 plane_sr = cursor_sr = 0;
1373 }
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001374
1375 DRM_DEBUG_KMS("Setting FIFO watermarks - A: plane=%d, cursor=%d, B: plane=%d, cursor=%d, SR: plane=%d, cursor=%d\n",
1376 planea_wm, cursora_wm,
1377 planeb_wm, cursorb_wm,
1378 plane_sr, cursor_sr);
1379
1380 I915_WRITE(DSPFW1,
1381 (plane_sr << DSPFW_SR_SHIFT) |
1382 (cursorb_wm << DSPFW_CURSORB_SHIFT) |
1383 (planeb_wm << DSPFW_PLANEB_SHIFT) |
1384 planea_wm);
1385 I915_WRITE(DSPFW2,
Chris Wilson8c919b22012-12-04 16:33:19 +00001386 (I915_READ(DSPFW2) & ~DSPFW_CURSORA_MASK) |
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001387 (cursora_wm << DSPFW_CURSORA_SHIFT));
1388 /* HPLL off in SR has some issues on G4x... disable it */
1389 I915_WRITE(DSPFW3,
Chris Wilson8c919b22012-12-04 16:33:19 +00001390 (I915_READ(DSPFW3) & ~(DSPFW_HPLL_SR_EN | DSPFW_CURSOR_SR_MASK)) |
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001391 (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
1392}
1393
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03001394static void i965_update_wm(struct drm_device *dev)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001395{
1396 struct drm_i915_private *dev_priv = dev->dev_private;
1397 struct drm_crtc *crtc;
1398 int srwm = 1;
1399 int cursor_sr = 16;
1400
1401 /* Calc sr entries for one plane configs */
1402 crtc = single_enabled_crtc(dev);
1403 if (crtc) {
1404 /* self-refresh has much higher latency */
1405 static const int sr_latency_ns = 12000;
1406 int clock = crtc->mode.clock;
1407 int htotal = crtc->mode.htotal;
1408 int hdisplay = crtc->mode.hdisplay;
1409 int pixel_size = crtc->fb->bits_per_pixel / 8;
1410 unsigned long line_time_us;
1411 int entries;
1412
1413 line_time_us = ((htotal * 1000) / clock);
1414
1415 /* Use ns/us then divide to preserve precision */
1416 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
1417 pixel_size * hdisplay;
1418 entries = DIV_ROUND_UP(entries, I915_FIFO_LINE_SIZE);
1419 srwm = I965_FIFO_SIZE - entries;
1420 if (srwm < 0)
1421 srwm = 1;
1422 srwm &= 0x1ff;
1423 DRM_DEBUG_KMS("self-refresh entries: %d, wm: %d\n",
1424 entries, srwm);
1425
1426 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
1427 pixel_size * 64;
1428 entries = DIV_ROUND_UP(entries,
1429 i965_cursor_wm_info.cacheline_size);
1430 cursor_sr = i965_cursor_wm_info.fifo_size -
1431 (entries + i965_cursor_wm_info.guard_size);
1432
1433 if (cursor_sr > i965_cursor_wm_info.max_wm)
1434 cursor_sr = i965_cursor_wm_info.max_wm;
1435
1436 DRM_DEBUG_KMS("self-refresh watermark: display plane %d "
1437 "cursor %d\n", srwm, cursor_sr);
1438
1439 if (IS_CRESTLINE(dev))
1440 I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_EN);
1441 } else {
1442 /* Turn off self refresh if both pipes are enabled */
1443 if (IS_CRESTLINE(dev))
1444 I915_WRITE(FW_BLC_SELF, I915_READ(FW_BLC_SELF)
1445 & ~FW_BLC_SELF_EN);
1446 }
1447
1448 DRM_DEBUG_KMS("Setting FIFO watermarks - A: 8, B: 8, C: 8, SR %d\n",
1449 srwm);
1450
1451 /* 965 has limitations... */
1452 I915_WRITE(DSPFW1, (srwm << DSPFW_SR_SHIFT) |
1453 (8 << 16) | (8 << 8) | (8 << 0));
1454 I915_WRITE(DSPFW2, (8 << 8) | (8 << 0));
1455 /* update cursor SR watermark */
1456 I915_WRITE(DSPFW3, (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
1457}
1458
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03001459static void i9xx_update_wm(struct drm_device *dev)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001460{
1461 struct drm_i915_private *dev_priv = dev->dev_private;
1462 const struct intel_watermark_params *wm_info;
1463 uint32_t fwater_lo;
1464 uint32_t fwater_hi;
1465 int cwm, srwm = 1;
1466 int fifo_size;
1467 int planea_wm, planeb_wm;
1468 struct drm_crtc *crtc, *enabled = NULL;
1469
1470 if (IS_I945GM(dev))
1471 wm_info = &i945_wm_info;
1472 else if (!IS_GEN2(dev))
1473 wm_info = &i915_wm_info;
1474 else
1475 wm_info = &i855_wm_info;
1476
1477 fifo_size = dev_priv->display.get_fifo_size(dev, 0);
1478 crtc = intel_get_crtc_for_plane(dev, 0);
Chris Wilsonf20e0b02012-12-07 10:43:25 +00001479 if (to_intel_crtc(crtc)->active && crtc->fb) {
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001480 int cpp = crtc->fb->bits_per_pixel / 8;
1481 if (IS_GEN2(dev))
1482 cpp = 4;
1483
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001484 planea_wm = intel_calculate_wm(crtc->mode.clock,
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001485 wm_info, fifo_size, cpp,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001486 latency_ns);
1487 enabled = crtc;
1488 } else
1489 planea_wm = fifo_size - wm_info->guard_size;
1490
1491 fifo_size = dev_priv->display.get_fifo_size(dev, 1);
1492 crtc = intel_get_crtc_for_plane(dev, 1);
Chris Wilsonf20e0b02012-12-07 10:43:25 +00001493 if (to_intel_crtc(crtc)->active && crtc->fb) {
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001494 int cpp = crtc->fb->bits_per_pixel / 8;
1495 if (IS_GEN2(dev))
1496 cpp = 4;
1497
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001498 planeb_wm = intel_calculate_wm(crtc->mode.clock,
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001499 wm_info, fifo_size, cpp,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001500 latency_ns);
1501 if (enabled == NULL)
1502 enabled = crtc;
1503 else
1504 enabled = NULL;
1505 } else
1506 planeb_wm = fifo_size - wm_info->guard_size;
1507
1508 DRM_DEBUG_KMS("FIFO watermarks - A: %d, B: %d\n", planea_wm, planeb_wm);
1509
1510 /*
1511 * Overlay gets an aggressive default since video jitter is bad.
1512 */
1513 cwm = 2;
1514
1515 /* Play safe and disable self-refresh before adjusting watermarks. */
1516 if (IS_I945G(dev) || IS_I945GM(dev))
1517 I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_EN_MASK | 0);
1518 else if (IS_I915GM(dev))
1519 I915_WRITE(INSTPM, I915_READ(INSTPM) & ~INSTPM_SELF_EN);
1520
1521 /* Calc sr entries for one plane configs */
1522 if (HAS_FW_BLC(dev) && enabled) {
1523 /* self-refresh has much higher latency */
1524 static const int sr_latency_ns = 6000;
1525 int clock = enabled->mode.clock;
1526 int htotal = enabled->mode.htotal;
1527 int hdisplay = enabled->mode.hdisplay;
1528 int pixel_size = enabled->fb->bits_per_pixel / 8;
1529 unsigned long line_time_us;
1530 int entries;
1531
1532 line_time_us = (htotal * 1000) / clock;
1533
1534 /* Use ns/us then divide to preserve precision */
1535 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
1536 pixel_size * hdisplay;
1537 entries = DIV_ROUND_UP(entries, wm_info->cacheline_size);
1538 DRM_DEBUG_KMS("self-refresh entries: %d\n", entries);
1539 srwm = wm_info->fifo_size - entries;
1540 if (srwm < 0)
1541 srwm = 1;
1542
1543 if (IS_I945G(dev) || IS_I945GM(dev))
1544 I915_WRITE(FW_BLC_SELF,
1545 FW_BLC_SELF_FIFO_MASK | (srwm & 0xff));
1546 else if (IS_I915GM(dev))
1547 I915_WRITE(FW_BLC_SELF, srwm & 0x3f);
1548 }
1549
1550 DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d, B: %d, C: %d, SR %d\n",
1551 planea_wm, planeb_wm, cwm, srwm);
1552
1553 fwater_lo = ((planeb_wm & 0x3f) << 16) | (planea_wm & 0x3f);
1554 fwater_hi = (cwm & 0x1f);
1555
1556 /* Set request length to 8 cachelines per fetch */
1557 fwater_lo = fwater_lo | (1 << 24) | (1 << 8);
1558 fwater_hi = fwater_hi | (1 << 8);
1559
1560 I915_WRITE(FW_BLC, fwater_lo);
1561 I915_WRITE(FW_BLC2, fwater_hi);
1562
1563 if (HAS_FW_BLC(dev)) {
1564 if (enabled) {
1565 if (IS_I945G(dev) || IS_I945GM(dev))
1566 I915_WRITE(FW_BLC_SELF,
1567 FW_BLC_SELF_EN_MASK | FW_BLC_SELF_EN);
1568 else if (IS_I915GM(dev))
1569 I915_WRITE(INSTPM, I915_READ(INSTPM) | INSTPM_SELF_EN);
1570 DRM_DEBUG_KMS("memory self refresh enabled\n");
1571 } else
1572 DRM_DEBUG_KMS("memory self refresh disabled\n");
1573 }
1574}
1575
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03001576static void i830_update_wm(struct drm_device *dev)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001577{
1578 struct drm_i915_private *dev_priv = dev->dev_private;
1579 struct drm_crtc *crtc;
1580 uint32_t fwater_lo;
1581 int planea_wm;
1582
1583 crtc = single_enabled_crtc(dev);
1584 if (crtc == NULL)
1585 return;
1586
1587 planea_wm = intel_calculate_wm(crtc->mode.clock, &i830_wm_info,
1588 dev_priv->display.get_fifo_size(dev, 0),
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001589 4, latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001590 fwater_lo = I915_READ(FW_BLC) & ~0xfff;
1591 fwater_lo |= (3<<8) | planea_wm;
1592
1593 DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d\n", planea_wm);
1594
1595 I915_WRITE(FW_BLC, fwater_lo);
1596}
1597
1598#define ILK_LP0_PLANE_LATENCY 700
1599#define ILK_LP0_CURSOR_LATENCY 1300
1600
1601/*
1602 * Check the wm result.
1603 *
1604 * If any calculated watermark values is larger than the maximum value that
1605 * can be programmed into the associated watermark register, that watermark
1606 * must be disabled.
1607 */
1608static bool ironlake_check_srwm(struct drm_device *dev, int level,
1609 int fbc_wm, int display_wm, int cursor_wm,
1610 const struct intel_watermark_params *display,
1611 const struct intel_watermark_params *cursor)
1612{
1613 struct drm_i915_private *dev_priv = dev->dev_private;
1614
1615 DRM_DEBUG_KMS("watermark %d: display plane %d, fbc lines %d,"
1616 " cursor %d\n", level, display_wm, fbc_wm, cursor_wm);
1617
1618 if (fbc_wm > SNB_FBC_MAX_SRWM) {
1619 DRM_DEBUG_KMS("fbc watermark(%d) is too large(%d), disabling wm%d+\n",
1620 fbc_wm, SNB_FBC_MAX_SRWM, level);
1621
1622 /* fbc has it's own way to disable FBC WM */
1623 I915_WRITE(DISP_ARB_CTL,
1624 I915_READ(DISP_ARB_CTL) | DISP_FBC_WM_DIS);
1625 return false;
1626 }
1627
1628 if (display_wm > display->max_wm) {
1629 DRM_DEBUG_KMS("display watermark(%d) is too large(%d), disabling wm%d+\n",
1630 display_wm, SNB_DISPLAY_MAX_SRWM, level);
1631 return false;
1632 }
1633
1634 if (cursor_wm > cursor->max_wm) {
1635 DRM_DEBUG_KMS("cursor watermark(%d) is too large(%d), disabling wm%d+\n",
1636 cursor_wm, SNB_CURSOR_MAX_SRWM, level);
1637 return false;
1638 }
1639
1640 if (!(fbc_wm || display_wm || cursor_wm)) {
1641 DRM_DEBUG_KMS("latency %d is 0, disabling wm%d+\n", level, level);
1642 return false;
1643 }
1644
1645 return true;
1646}
1647
1648/*
1649 * Compute watermark values of WM[1-3],
1650 */
1651static bool ironlake_compute_srwm(struct drm_device *dev, int level, int plane,
1652 int latency_ns,
1653 const struct intel_watermark_params *display,
1654 const struct intel_watermark_params *cursor,
1655 int *fbc_wm, int *display_wm, int *cursor_wm)
1656{
1657 struct drm_crtc *crtc;
1658 unsigned long line_time_us;
1659 int hdisplay, htotal, pixel_size, clock;
1660 int line_count, line_size;
1661 int small, large;
1662 int entries;
1663
1664 if (!latency_ns) {
1665 *fbc_wm = *display_wm = *cursor_wm = 0;
1666 return false;
1667 }
1668
1669 crtc = intel_get_crtc_for_plane(dev, plane);
1670 hdisplay = crtc->mode.hdisplay;
1671 htotal = crtc->mode.htotal;
1672 clock = crtc->mode.clock;
1673 pixel_size = crtc->fb->bits_per_pixel / 8;
1674
1675 line_time_us = (htotal * 1000) / clock;
1676 line_count = (latency_ns / line_time_us + 1000) / 1000;
1677 line_size = hdisplay * pixel_size;
1678
1679 /* Use the minimum of the small and large buffer method for primary */
1680 small = ((clock * pixel_size / 1000) * latency_ns) / 1000;
1681 large = line_count * line_size;
1682
1683 entries = DIV_ROUND_UP(min(small, large), display->cacheline_size);
1684 *display_wm = entries + display->guard_size;
1685
1686 /*
1687 * Spec says:
1688 * FBC WM = ((Final Primary WM * 64) / number of bytes per line) + 2
1689 */
1690 *fbc_wm = DIV_ROUND_UP(*display_wm * 64, line_size) + 2;
1691
1692 /* calculate the self-refresh watermark for display cursor */
1693 entries = line_count * pixel_size * 64;
1694 entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
1695 *cursor_wm = entries + cursor->guard_size;
1696
1697 return ironlake_check_srwm(dev, level,
1698 *fbc_wm, *display_wm, *cursor_wm,
1699 display, cursor);
1700}
1701
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03001702static void ironlake_update_wm(struct drm_device *dev)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001703{
1704 struct drm_i915_private *dev_priv = dev->dev_private;
1705 int fbc_wm, plane_wm, cursor_wm;
1706 unsigned int enabled;
1707
1708 enabled = 0;
1709 if (g4x_compute_wm0(dev, 0,
1710 &ironlake_display_wm_info,
1711 ILK_LP0_PLANE_LATENCY,
1712 &ironlake_cursor_wm_info,
1713 ILK_LP0_CURSOR_LATENCY,
1714 &plane_wm, &cursor_wm)) {
1715 I915_WRITE(WM0_PIPEA_ILK,
1716 (plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm);
1717 DRM_DEBUG_KMS("FIFO watermarks For pipe A -"
1718 " plane %d, " "cursor: %d\n",
1719 plane_wm, cursor_wm);
1720 enabled |= 1;
1721 }
1722
1723 if (g4x_compute_wm0(dev, 1,
1724 &ironlake_display_wm_info,
1725 ILK_LP0_PLANE_LATENCY,
1726 &ironlake_cursor_wm_info,
1727 ILK_LP0_CURSOR_LATENCY,
1728 &plane_wm, &cursor_wm)) {
1729 I915_WRITE(WM0_PIPEB_ILK,
1730 (plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm);
1731 DRM_DEBUG_KMS("FIFO watermarks For pipe B -"
1732 " plane %d, cursor: %d\n",
1733 plane_wm, cursor_wm);
1734 enabled |= 2;
1735 }
1736
1737 /*
1738 * Calculate and update the self-refresh watermark only when one
1739 * display plane is used.
1740 */
1741 I915_WRITE(WM3_LP_ILK, 0);
1742 I915_WRITE(WM2_LP_ILK, 0);
1743 I915_WRITE(WM1_LP_ILK, 0);
1744
1745 if (!single_plane_enabled(enabled))
1746 return;
1747 enabled = ffs(enabled) - 1;
1748
1749 /* WM1 */
1750 if (!ironlake_compute_srwm(dev, 1, enabled,
1751 ILK_READ_WM1_LATENCY() * 500,
1752 &ironlake_display_srwm_info,
1753 &ironlake_cursor_srwm_info,
1754 &fbc_wm, &plane_wm, &cursor_wm))
1755 return;
1756
1757 I915_WRITE(WM1_LP_ILK,
1758 WM1_LP_SR_EN |
1759 (ILK_READ_WM1_LATENCY() << WM1_LP_LATENCY_SHIFT) |
1760 (fbc_wm << WM1_LP_FBC_SHIFT) |
1761 (plane_wm << WM1_LP_SR_SHIFT) |
1762 cursor_wm);
1763
1764 /* WM2 */
1765 if (!ironlake_compute_srwm(dev, 2, enabled,
1766 ILK_READ_WM2_LATENCY() * 500,
1767 &ironlake_display_srwm_info,
1768 &ironlake_cursor_srwm_info,
1769 &fbc_wm, &plane_wm, &cursor_wm))
1770 return;
1771
1772 I915_WRITE(WM2_LP_ILK,
1773 WM2_LP_EN |
1774 (ILK_READ_WM2_LATENCY() << WM1_LP_LATENCY_SHIFT) |
1775 (fbc_wm << WM1_LP_FBC_SHIFT) |
1776 (plane_wm << WM1_LP_SR_SHIFT) |
1777 cursor_wm);
1778
1779 /*
1780 * WM3 is unsupported on ILK, probably because we don't have latency
1781 * data for that power state
1782 */
1783}
1784
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03001785static void sandybridge_update_wm(struct drm_device *dev)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001786{
1787 struct drm_i915_private *dev_priv = dev->dev_private;
1788 int latency = SNB_READ_WM0_LATENCY() * 100; /* In unit 0.1us */
1789 u32 val;
1790 int fbc_wm, plane_wm, cursor_wm;
1791 unsigned int enabled;
1792
1793 enabled = 0;
1794 if (g4x_compute_wm0(dev, 0,
1795 &sandybridge_display_wm_info, latency,
1796 &sandybridge_cursor_wm_info, latency,
1797 &plane_wm, &cursor_wm)) {
1798 val = I915_READ(WM0_PIPEA_ILK);
1799 val &= ~(WM0_PIPE_PLANE_MASK | WM0_PIPE_CURSOR_MASK);
1800 I915_WRITE(WM0_PIPEA_ILK, val |
1801 ((plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm));
1802 DRM_DEBUG_KMS("FIFO watermarks For pipe A -"
1803 " plane %d, " "cursor: %d\n",
1804 plane_wm, cursor_wm);
1805 enabled |= 1;
1806 }
1807
1808 if (g4x_compute_wm0(dev, 1,
1809 &sandybridge_display_wm_info, latency,
1810 &sandybridge_cursor_wm_info, latency,
1811 &plane_wm, &cursor_wm)) {
1812 val = I915_READ(WM0_PIPEB_ILK);
1813 val &= ~(WM0_PIPE_PLANE_MASK | WM0_PIPE_CURSOR_MASK);
1814 I915_WRITE(WM0_PIPEB_ILK, val |
1815 ((plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm));
1816 DRM_DEBUG_KMS("FIFO watermarks For pipe B -"
1817 " plane %d, cursor: %d\n",
1818 plane_wm, cursor_wm);
1819 enabled |= 2;
1820 }
1821
Chris Wilsonc43d0182012-12-11 12:01:42 +00001822 /*
1823 * Calculate and update the self-refresh watermark only when one
1824 * display plane is used.
1825 *
1826 * SNB support 3 levels of watermark.
1827 *
1828 * WM1/WM2/WM2 watermarks have to be enabled in the ascending order,
1829 * and disabled in the descending order
1830 *
1831 */
1832 I915_WRITE(WM3_LP_ILK, 0);
1833 I915_WRITE(WM2_LP_ILK, 0);
1834 I915_WRITE(WM1_LP_ILK, 0);
1835
1836 if (!single_plane_enabled(enabled) ||
1837 dev_priv->sprite_scaling_enabled)
1838 return;
1839 enabled = ffs(enabled) - 1;
1840
1841 /* WM1 */
1842 if (!ironlake_compute_srwm(dev, 1, enabled,
1843 SNB_READ_WM1_LATENCY() * 500,
1844 &sandybridge_display_srwm_info,
1845 &sandybridge_cursor_srwm_info,
1846 &fbc_wm, &plane_wm, &cursor_wm))
1847 return;
1848
1849 I915_WRITE(WM1_LP_ILK,
1850 WM1_LP_SR_EN |
1851 (SNB_READ_WM1_LATENCY() << WM1_LP_LATENCY_SHIFT) |
1852 (fbc_wm << WM1_LP_FBC_SHIFT) |
1853 (plane_wm << WM1_LP_SR_SHIFT) |
1854 cursor_wm);
1855
1856 /* WM2 */
1857 if (!ironlake_compute_srwm(dev, 2, enabled,
1858 SNB_READ_WM2_LATENCY() * 500,
1859 &sandybridge_display_srwm_info,
1860 &sandybridge_cursor_srwm_info,
1861 &fbc_wm, &plane_wm, &cursor_wm))
1862 return;
1863
1864 I915_WRITE(WM2_LP_ILK,
1865 WM2_LP_EN |
1866 (SNB_READ_WM2_LATENCY() << WM1_LP_LATENCY_SHIFT) |
1867 (fbc_wm << WM1_LP_FBC_SHIFT) |
1868 (plane_wm << WM1_LP_SR_SHIFT) |
1869 cursor_wm);
1870
1871 /* WM3 */
1872 if (!ironlake_compute_srwm(dev, 3, enabled,
1873 SNB_READ_WM3_LATENCY() * 500,
1874 &sandybridge_display_srwm_info,
1875 &sandybridge_cursor_srwm_info,
1876 &fbc_wm, &plane_wm, &cursor_wm))
1877 return;
1878
1879 I915_WRITE(WM3_LP_ILK,
1880 WM3_LP_EN |
1881 (SNB_READ_WM3_LATENCY() << WM1_LP_LATENCY_SHIFT) |
1882 (fbc_wm << WM1_LP_FBC_SHIFT) |
1883 (plane_wm << WM1_LP_SR_SHIFT) |
1884 cursor_wm);
1885}
1886
1887static void ivybridge_update_wm(struct drm_device *dev)
1888{
1889 struct drm_i915_private *dev_priv = dev->dev_private;
1890 int latency = SNB_READ_WM0_LATENCY() * 100; /* In unit 0.1us */
1891 u32 val;
1892 int fbc_wm, plane_wm, cursor_wm;
1893 int ignore_fbc_wm, ignore_plane_wm, ignore_cursor_wm;
1894 unsigned int enabled;
1895
1896 enabled = 0;
1897 if (g4x_compute_wm0(dev, 0,
1898 &sandybridge_display_wm_info, latency,
1899 &sandybridge_cursor_wm_info, latency,
1900 &plane_wm, &cursor_wm)) {
1901 val = I915_READ(WM0_PIPEA_ILK);
1902 val &= ~(WM0_PIPE_PLANE_MASK | WM0_PIPE_CURSOR_MASK);
1903 I915_WRITE(WM0_PIPEA_ILK, val |
1904 ((plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm));
1905 DRM_DEBUG_KMS("FIFO watermarks For pipe A -"
1906 " plane %d, " "cursor: %d\n",
1907 plane_wm, cursor_wm);
1908 enabled |= 1;
1909 }
1910
1911 if (g4x_compute_wm0(dev, 1,
1912 &sandybridge_display_wm_info, latency,
1913 &sandybridge_cursor_wm_info, latency,
1914 &plane_wm, &cursor_wm)) {
1915 val = I915_READ(WM0_PIPEB_ILK);
1916 val &= ~(WM0_PIPE_PLANE_MASK | WM0_PIPE_CURSOR_MASK);
1917 I915_WRITE(WM0_PIPEB_ILK, val |
1918 ((plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm));
1919 DRM_DEBUG_KMS("FIFO watermarks For pipe B -"
1920 " plane %d, cursor: %d\n",
1921 plane_wm, cursor_wm);
1922 enabled |= 2;
1923 }
1924
1925 if (g4x_compute_wm0(dev, 2,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001926 &sandybridge_display_wm_info, latency,
1927 &sandybridge_cursor_wm_info, latency,
1928 &plane_wm, &cursor_wm)) {
1929 val = I915_READ(WM0_PIPEC_IVB);
1930 val &= ~(WM0_PIPE_PLANE_MASK | WM0_PIPE_CURSOR_MASK);
1931 I915_WRITE(WM0_PIPEC_IVB, val |
1932 ((plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm));
1933 DRM_DEBUG_KMS("FIFO watermarks For pipe C -"
1934 " plane %d, cursor: %d\n",
1935 plane_wm, cursor_wm);
1936 enabled |= 3;
1937 }
1938
1939 /*
1940 * Calculate and update the self-refresh watermark only when one
1941 * display plane is used.
1942 *
1943 * SNB support 3 levels of watermark.
1944 *
1945 * WM1/WM2/WM2 watermarks have to be enabled in the ascending order,
1946 * and disabled in the descending order
1947 *
1948 */
1949 I915_WRITE(WM3_LP_ILK, 0);
1950 I915_WRITE(WM2_LP_ILK, 0);
1951 I915_WRITE(WM1_LP_ILK, 0);
1952
1953 if (!single_plane_enabled(enabled) ||
1954 dev_priv->sprite_scaling_enabled)
1955 return;
1956 enabled = ffs(enabled) - 1;
1957
1958 /* WM1 */
1959 if (!ironlake_compute_srwm(dev, 1, enabled,
1960 SNB_READ_WM1_LATENCY() * 500,
1961 &sandybridge_display_srwm_info,
1962 &sandybridge_cursor_srwm_info,
1963 &fbc_wm, &plane_wm, &cursor_wm))
1964 return;
1965
1966 I915_WRITE(WM1_LP_ILK,
1967 WM1_LP_SR_EN |
1968 (SNB_READ_WM1_LATENCY() << WM1_LP_LATENCY_SHIFT) |
1969 (fbc_wm << WM1_LP_FBC_SHIFT) |
1970 (plane_wm << WM1_LP_SR_SHIFT) |
1971 cursor_wm);
1972
1973 /* WM2 */
1974 if (!ironlake_compute_srwm(dev, 2, enabled,
1975 SNB_READ_WM2_LATENCY() * 500,
1976 &sandybridge_display_srwm_info,
1977 &sandybridge_cursor_srwm_info,
1978 &fbc_wm, &plane_wm, &cursor_wm))
1979 return;
1980
1981 I915_WRITE(WM2_LP_ILK,
1982 WM2_LP_EN |
1983 (SNB_READ_WM2_LATENCY() << WM1_LP_LATENCY_SHIFT) |
1984 (fbc_wm << WM1_LP_FBC_SHIFT) |
1985 (plane_wm << WM1_LP_SR_SHIFT) |
1986 cursor_wm);
1987
Chris Wilsonc43d0182012-12-11 12:01:42 +00001988 /* WM3, note we have to correct the cursor latency */
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001989 if (!ironlake_compute_srwm(dev, 3, enabled,
1990 SNB_READ_WM3_LATENCY() * 500,
1991 &sandybridge_display_srwm_info,
1992 &sandybridge_cursor_srwm_info,
Chris Wilsonc43d0182012-12-11 12:01:42 +00001993 &fbc_wm, &plane_wm, &ignore_cursor_wm) ||
1994 !ironlake_compute_srwm(dev, 3, enabled,
1995 2 * SNB_READ_WM3_LATENCY() * 500,
1996 &sandybridge_display_srwm_info,
1997 &sandybridge_cursor_srwm_info,
1998 &ignore_fbc_wm, &ignore_plane_wm, &cursor_wm))
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001999 return;
2000
2001 I915_WRITE(WM3_LP_ILK,
2002 WM3_LP_EN |
2003 (SNB_READ_WM3_LATENCY() << WM1_LP_LATENCY_SHIFT) |
2004 (fbc_wm << WM1_LP_FBC_SHIFT) |
2005 (plane_wm << WM1_LP_SR_SHIFT) |
2006 cursor_wm);
2007}
2008
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002009static void
2010haswell_update_linetime_wm(struct drm_device *dev, int pipe,
2011 struct drm_display_mode *mode)
2012{
2013 struct drm_i915_private *dev_priv = dev->dev_private;
2014 u32 temp;
2015
2016 temp = I915_READ(PIPE_WM_LINETIME(pipe));
2017 temp &= ~PIPE_WM_LINETIME_MASK;
2018
2019 /* The WM are computed with base on how long it takes to fill a single
2020 * row at the given clock rate, multiplied by 8.
2021 * */
2022 temp |= PIPE_WM_LINETIME_TIME(
2023 ((mode->crtc_hdisplay * 1000) / mode->clock) * 8);
2024
2025 /* IPS watermarks are only used by pipe A, and are ignored by
2026 * pipes B and C. They are calculated similarly to the common
2027 * linetime values, except that we are using CD clock frequency
2028 * in MHz instead of pixel rate for the division.
2029 *
2030 * This is a placeholder for the IPS watermark calculation code.
2031 */
2032
2033 I915_WRITE(PIPE_WM_LINETIME(pipe), temp);
2034}
2035
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002036static bool
2037sandybridge_compute_sprite_wm(struct drm_device *dev, int plane,
2038 uint32_t sprite_width, int pixel_size,
2039 const struct intel_watermark_params *display,
2040 int display_latency_ns, int *sprite_wm)
2041{
2042 struct drm_crtc *crtc;
2043 int clock;
2044 int entries, tlb_miss;
2045
2046 crtc = intel_get_crtc_for_plane(dev, plane);
Chris Wilsonf20e0b02012-12-07 10:43:25 +00002047 if (crtc->fb == NULL || !to_intel_crtc(crtc)->active) {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002048 *sprite_wm = display->guard_size;
2049 return false;
2050 }
2051
2052 clock = crtc->mode.clock;
2053
2054 /* Use the small buffer method to calculate the sprite watermark */
2055 entries = ((clock * pixel_size / 1000) * display_latency_ns) / 1000;
2056 tlb_miss = display->fifo_size*display->cacheline_size -
2057 sprite_width * 8;
2058 if (tlb_miss > 0)
2059 entries += tlb_miss;
2060 entries = DIV_ROUND_UP(entries, display->cacheline_size);
2061 *sprite_wm = entries + display->guard_size;
2062 if (*sprite_wm > (int)display->max_wm)
2063 *sprite_wm = display->max_wm;
2064
2065 return true;
2066}
2067
2068static bool
2069sandybridge_compute_sprite_srwm(struct drm_device *dev, int plane,
2070 uint32_t sprite_width, int pixel_size,
2071 const struct intel_watermark_params *display,
2072 int latency_ns, int *sprite_wm)
2073{
2074 struct drm_crtc *crtc;
2075 unsigned long line_time_us;
2076 int clock;
2077 int line_count, line_size;
2078 int small, large;
2079 int entries;
2080
2081 if (!latency_ns) {
2082 *sprite_wm = 0;
2083 return false;
2084 }
2085
2086 crtc = intel_get_crtc_for_plane(dev, plane);
2087 clock = crtc->mode.clock;
2088 if (!clock) {
2089 *sprite_wm = 0;
2090 return false;
2091 }
2092
2093 line_time_us = (sprite_width * 1000) / clock;
2094 if (!line_time_us) {
2095 *sprite_wm = 0;
2096 return false;
2097 }
2098
2099 line_count = (latency_ns / line_time_us + 1000) / 1000;
2100 line_size = sprite_width * pixel_size;
2101
2102 /* Use the minimum of the small and large buffer method for primary */
2103 small = ((clock * pixel_size / 1000) * latency_ns) / 1000;
2104 large = line_count * line_size;
2105
2106 entries = DIV_ROUND_UP(min(small, large), display->cacheline_size);
2107 *sprite_wm = entries + display->guard_size;
2108
2109 return *sprite_wm > 0x3ff ? false : true;
2110}
2111
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03002112static void sandybridge_update_sprite_wm(struct drm_device *dev, int pipe,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002113 uint32_t sprite_width, int pixel_size)
2114{
2115 struct drm_i915_private *dev_priv = dev->dev_private;
2116 int latency = SNB_READ_WM0_LATENCY() * 100; /* In unit 0.1us */
2117 u32 val;
2118 int sprite_wm, reg;
2119 int ret;
2120
2121 switch (pipe) {
2122 case 0:
2123 reg = WM0_PIPEA_ILK;
2124 break;
2125 case 1:
2126 reg = WM0_PIPEB_ILK;
2127 break;
2128 case 2:
2129 reg = WM0_PIPEC_IVB;
2130 break;
2131 default:
2132 return; /* bad pipe */
2133 }
2134
2135 ret = sandybridge_compute_sprite_wm(dev, pipe, sprite_width, pixel_size,
2136 &sandybridge_display_wm_info,
2137 latency, &sprite_wm);
2138 if (!ret) {
2139 DRM_DEBUG_KMS("failed to compute sprite wm for pipe %d\n",
2140 pipe);
2141 return;
2142 }
2143
2144 val = I915_READ(reg);
2145 val &= ~WM0_PIPE_SPRITE_MASK;
2146 I915_WRITE(reg, val | (sprite_wm << WM0_PIPE_SPRITE_SHIFT));
2147 DRM_DEBUG_KMS("sprite watermarks For pipe %d - %d\n", pipe, sprite_wm);
2148
2149
2150 ret = sandybridge_compute_sprite_srwm(dev, pipe, sprite_width,
2151 pixel_size,
2152 &sandybridge_display_srwm_info,
2153 SNB_READ_WM1_LATENCY() * 500,
2154 &sprite_wm);
2155 if (!ret) {
2156 DRM_DEBUG_KMS("failed to compute sprite lp1 wm on pipe %d\n",
2157 pipe);
2158 return;
2159 }
2160 I915_WRITE(WM1S_LP_ILK, sprite_wm);
2161
2162 /* Only IVB has two more LP watermarks for sprite */
2163 if (!IS_IVYBRIDGE(dev))
2164 return;
2165
2166 ret = sandybridge_compute_sprite_srwm(dev, pipe, sprite_width,
2167 pixel_size,
2168 &sandybridge_display_srwm_info,
2169 SNB_READ_WM2_LATENCY() * 500,
2170 &sprite_wm);
2171 if (!ret) {
2172 DRM_DEBUG_KMS("failed to compute sprite lp2 wm on pipe %d\n",
2173 pipe);
2174 return;
2175 }
2176 I915_WRITE(WM2S_LP_IVB, sprite_wm);
2177
2178 ret = sandybridge_compute_sprite_srwm(dev, pipe, sprite_width,
2179 pixel_size,
2180 &sandybridge_display_srwm_info,
2181 SNB_READ_WM3_LATENCY() * 500,
2182 &sprite_wm);
2183 if (!ret) {
2184 DRM_DEBUG_KMS("failed to compute sprite lp3 wm on pipe %d\n",
2185 pipe);
2186 return;
2187 }
2188 I915_WRITE(WM3S_LP_IVB, sprite_wm);
2189}
2190
2191/**
2192 * intel_update_watermarks - update FIFO watermark values based on current modes
2193 *
2194 * Calculate watermark values for the various WM regs based on current mode
2195 * and plane configuration.
2196 *
2197 * There are several cases to deal with here:
2198 * - normal (i.e. non-self-refresh)
2199 * - self-refresh (SR) mode
2200 * - lines are large relative to FIFO size (buffer can hold up to 2)
2201 * - lines are small relative to FIFO size (buffer can hold more than 2
2202 * lines), so need to account for TLB latency
2203 *
2204 * The normal calculation is:
2205 * watermark = dotclock * bytes per pixel * latency
2206 * where latency is platform & configuration dependent (we assume pessimal
2207 * values here).
2208 *
2209 * The SR calculation is:
2210 * watermark = (trunc(latency/line time)+1) * surface width *
2211 * bytes per pixel
2212 * where
2213 * line time = htotal / dotclock
2214 * surface width = hdisplay for normal plane and 64 for cursor
2215 * and latency is assumed to be high, as above.
2216 *
2217 * The final value programmed to the register should always be rounded up,
2218 * and include an extra 2 entries to account for clock crossings.
2219 *
2220 * We don't use the sprite, so we can ignore that. And on Crestline we have
2221 * to set the non-SR watermarks to 8.
2222 */
2223void intel_update_watermarks(struct drm_device *dev)
2224{
2225 struct drm_i915_private *dev_priv = dev->dev_private;
2226
2227 if (dev_priv->display.update_wm)
2228 dev_priv->display.update_wm(dev);
2229}
2230
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002231void intel_update_linetime_watermarks(struct drm_device *dev,
2232 int pipe, struct drm_display_mode *mode)
2233{
2234 struct drm_i915_private *dev_priv = dev->dev_private;
2235
2236 if (dev_priv->display.update_linetime_wm)
2237 dev_priv->display.update_linetime_wm(dev, pipe, mode);
2238}
2239
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03002240void intel_update_sprite_watermarks(struct drm_device *dev, int pipe,
2241 uint32_t sprite_width, int pixel_size)
2242{
2243 struct drm_i915_private *dev_priv = dev->dev_private;
2244
2245 if (dev_priv->display.update_sprite_wm)
2246 dev_priv->display.update_sprite_wm(dev, pipe, sprite_width,
2247 pixel_size);
2248}
2249
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002250static struct drm_i915_gem_object *
2251intel_alloc_context_page(struct drm_device *dev)
2252{
2253 struct drm_i915_gem_object *ctx;
2254 int ret;
2255
2256 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2257
2258 ctx = i915_gem_alloc_object(dev, 4096);
2259 if (!ctx) {
2260 DRM_DEBUG("failed to alloc power context, RC6 disabled\n");
2261 return NULL;
2262 }
2263
Chris Wilson86a1ee22012-08-11 15:41:04 +01002264 ret = i915_gem_object_pin(ctx, 4096, true, false);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002265 if (ret) {
2266 DRM_ERROR("failed to pin power context: %d\n", ret);
2267 goto err_unref;
2268 }
2269
2270 ret = i915_gem_object_set_to_gtt_domain(ctx, 1);
2271 if (ret) {
2272 DRM_ERROR("failed to set-domain on power context: %d\n", ret);
2273 goto err_unpin;
2274 }
2275
2276 return ctx;
2277
2278err_unpin:
2279 i915_gem_object_unpin(ctx);
2280err_unref:
2281 drm_gem_object_unreference(&ctx->base);
2282 mutex_unlock(&dev->struct_mutex);
2283 return NULL;
2284}
2285
Daniel Vetter92703882012-08-09 16:46:01 +02002286/**
2287 * Lock protecting IPS related data structures
Daniel Vetter92703882012-08-09 16:46:01 +02002288 */
2289DEFINE_SPINLOCK(mchdev_lock);
2290
2291/* Global for IPS driver to get at the current i915 device. Protected by
2292 * mchdev_lock. */
2293static struct drm_i915_private *i915_mch_dev;
2294
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002295bool ironlake_set_drps(struct drm_device *dev, u8 val)
2296{
2297 struct drm_i915_private *dev_priv = dev->dev_private;
2298 u16 rgvswctl;
2299
Daniel Vetter92703882012-08-09 16:46:01 +02002300 assert_spin_locked(&mchdev_lock);
2301
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002302 rgvswctl = I915_READ16(MEMSWCTL);
2303 if (rgvswctl & MEMCTL_CMD_STS) {
2304 DRM_DEBUG("gpu busy, RCS change rejected\n");
2305 return false; /* still busy with another command */
2306 }
2307
2308 rgvswctl = (MEMCTL_CMD_CHFREQ << MEMCTL_CMD_SHIFT) |
2309 (val << MEMCTL_FREQ_SHIFT) | MEMCTL_SFCAVM;
2310 I915_WRITE16(MEMSWCTL, rgvswctl);
2311 POSTING_READ16(MEMSWCTL);
2312
2313 rgvswctl |= MEMCTL_CMD_STS;
2314 I915_WRITE16(MEMSWCTL, rgvswctl);
2315
2316 return true;
2317}
2318
Daniel Vetter8090c6b2012-06-24 16:42:32 +02002319static void ironlake_enable_drps(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002320{
2321 struct drm_i915_private *dev_priv = dev->dev_private;
2322 u32 rgvmodectl = I915_READ(MEMMODECTL);
2323 u8 fmax, fmin, fstart, vstart;
2324
Daniel Vetter92703882012-08-09 16:46:01 +02002325 spin_lock_irq(&mchdev_lock);
2326
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002327 /* Enable temp reporting */
2328 I915_WRITE16(PMMISC, I915_READ(PMMISC) | MCPPCE_EN);
2329 I915_WRITE16(TSC1, I915_READ(TSC1) | TSE);
2330
2331 /* 100ms RC evaluation intervals */
2332 I915_WRITE(RCUPEI, 100000);
2333 I915_WRITE(RCDNEI, 100000);
2334
2335 /* Set max/min thresholds to 90ms and 80ms respectively */
2336 I915_WRITE(RCBMAXAVG, 90000);
2337 I915_WRITE(RCBMINAVG, 80000);
2338
2339 I915_WRITE(MEMIHYST, 1);
2340
2341 /* Set up min, max, and cur for interrupt handling */
2342 fmax = (rgvmodectl & MEMMODE_FMAX_MASK) >> MEMMODE_FMAX_SHIFT;
2343 fmin = (rgvmodectl & MEMMODE_FMIN_MASK);
2344 fstart = (rgvmodectl & MEMMODE_FSTART_MASK) >>
2345 MEMMODE_FSTART_SHIFT;
2346
2347 vstart = (I915_READ(PXVFREQ_BASE + (fstart * 4)) & PXVFREQ_PX_MASK) >>
2348 PXVFREQ_PX_SHIFT;
2349
Daniel Vetter20e4d402012-08-08 23:35:39 +02002350 dev_priv->ips.fmax = fmax; /* IPS callback will increase this */
2351 dev_priv->ips.fstart = fstart;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002352
Daniel Vetter20e4d402012-08-08 23:35:39 +02002353 dev_priv->ips.max_delay = fstart;
2354 dev_priv->ips.min_delay = fmin;
2355 dev_priv->ips.cur_delay = fstart;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002356
2357 DRM_DEBUG_DRIVER("fmax: %d, fmin: %d, fstart: %d\n",
2358 fmax, fmin, fstart);
2359
2360 I915_WRITE(MEMINTREN, MEMINT_CX_SUPR_EN | MEMINT_EVAL_CHG_EN);
2361
2362 /*
2363 * Interrupts will be enabled in ironlake_irq_postinstall
2364 */
2365
2366 I915_WRITE(VIDSTART, vstart);
2367 POSTING_READ(VIDSTART);
2368
2369 rgvmodectl |= MEMMODE_SWMODE_EN;
2370 I915_WRITE(MEMMODECTL, rgvmodectl);
2371
Daniel Vetter92703882012-08-09 16:46:01 +02002372 if (wait_for_atomic((I915_READ(MEMSWCTL) & MEMCTL_CMD_STS) == 0, 10))
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002373 DRM_ERROR("stuck trying to change perf mode\n");
Daniel Vetter92703882012-08-09 16:46:01 +02002374 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002375
2376 ironlake_set_drps(dev, fstart);
2377
Daniel Vetter20e4d402012-08-08 23:35:39 +02002378 dev_priv->ips.last_count1 = I915_READ(0x112e4) + I915_READ(0x112e8) +
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002379 I915_READ(0x112e0);
Daniel Vetter20e4d402012-08-08 23:35:39 +02002380 dev_priv->ips.last_time1 = jiffies_to_msecs(jiffies);
2381 dev_priv->ips.last_count2 = I915_READ(0x112f4);
2382 getrawmonotonic(&dev_priv->ips.last_time2);
Daniel Vetter92703882012-08-09 16:46:01 +02002383
2384 spin_unlock_irq(&mchdev_lock);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002385}
2386
Daniel Vetter8090c6b2012-06-24 16:42:32 +02002387static void ironlake_disable_drps(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002388{
2389 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter92703882012-08-09 16:46:01 +02002390 u16 rgvswctl;
2391
2392 spin_lock_irq(&mchdev_lock);
2393
2394 rgvswctl = I915_READ16(MEMSWCTL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002395
2396 /* Ack interrupts, disable EFC interrupt */
2397 I915_WRITE(MEMINTREN, I915_READ(MEMINTREN) & ~MEMINT_EVAL_CHG_EN);
2398 I915_WRITE(MEMINTRSTS, MEMINT_EVAL_CHG);
2399 I915_WRITE(DEIER, I915_READ(DEIER) & ~DE_PCU_EVENT);
2400 I915_WRITE(DEIIR, DE_PCU_EVENT);
2401 I915_WRITE(DEIMR, I915_READ(DEIMR) | DE_PCU_EVENT);
2402
2403 /* Go back to the starting frequency */
Daniel Vetter20e4d402012-08-08 23:35:39 +02002404 ironlake_set_drps(dev, dev_priv->ips.fstart);
Daniel Vetter92703882012-08-09 16:46:01 +02002405 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002406 rgvswctl |= MEMCTL_CMD_STS;
2407 I915_WRITE(MEMSWCTL, rgvswctl);
Daniel Vetter92703882012-08-09 16:46:01 +02002408 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002409
Daniel Vetter92703882012-08-09 16:46:01 +02002410 spin_unlock_irq(&mchdev_lock);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002411}
2412
Daniel Vetteracbe9472012-07-26 11:50:05 +02002413/* There's a funny hw issue where the hw returns all 0 when reading from
2414 * GEN6_RP_INTERRUPT_LIMITS. Hence we always need to compute the desired value
2415 * ourselves, instead of doing a rmw cycle (which might result in us clearing
2416 * all limits and the gpu stuck at whatever frequency it is at atm).
2417 */
Daniel Vetter65bccb52012-08-08 17:42:52 +02002418static u32 gen6_rps_limits(struct drm_i915_private *dev_priv, u8 *val)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002419{
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01002420 u32 limits;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002421
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01002422 limits = 0;
Daniel Vetterc6a828d2012-08-08 23:35:35 +02002423
2424 if (*val >= dev_priv->rps.max_delay)
2425 *val = dev_priv->rps.max_delay;
2426 limits |= dev_priv->rps.max_delay << 24;
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01002427
Daniel Vetter20b46e52012-07-26 11:16:14 +02002428 /* Only set the down limit when we've reached the lowest level to avoid
2429 * getting more interrupts, otherwise leave this clear. This prevents a
2430 * race in the hw when coming out of rc6: There's a tiny window where
2431 * the hw runs at the minimal clock before selecting the desired
2432 * frequency, if the down threshold expires in that window we will not
2433 * receive a down interrupt. */
Daniel Vetterc6a828d2012-08-08 23:35:35 +02002434 if (*val <= dev_priv->rps.min_delay) {
2435 *val = dev_priv->rps.min_delay;
2436 limits |= dev_priv->rps.min_delay << 16;
Daniel Vetter20b46e52012-07-26 11:16:14 +02002437 }
2438
2439 return limits;
2440}
2441
2442void gen6_set_rps(struct drm_device *dev, u8 val)
2443{
2444 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter65bccb52012-08-08 17:42:52 +02002445 u32 limits = gen6_rps_limits(dev_priv, &val);
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01002446
Jesse Barnes4fc688c2012-11-02 11:14:01 -07002447 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Ben Widawsky79249632012-09-07 19:43:42 -07002448 WARN_ON(val > dev_priv->rps.max_delay);
2449 WARN_ON(val < dev_priv->rps.min_delay);
Daniel Vetter004777c2012-08-09 15:07:01 +02002450
Daniel Vetterc6a828d2012-08-08 23:35:35 +02002451 if (val == dev_priv->rps.cur_delay)
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01002452 return;
2453
2454 I915_WRITE(GEN6_RPNSWREQ,
2455 GEN6_FREQUENCY(val) |
2456 GEN6_OFFSET(0) |
2457 GEN6_AGGRESSIVE_TURBO);
2458
2459 /* Make sure we continue to get interrupts
2460 * until we hit the minimum or maximum frequencies.
2461 */
2462 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS, limits);
2463
Ben Widawskyd5570a72012-09-07 19:43:41 -07002464 POSTING_READ(GEN6_RPNSWREQ);
2465
Daniel Vetterc6a828d2012-08-08 23:35:35 +02002466 dev_priv->rps.cur_delay = val;
Daniel Vetterbe2cde92012-08-30 13:26:48 +02002467
2468 trace_intel_gpu_freq_change(val * 50);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002469}
2470
Daniel Vetter8090c6b2012-06-24 16:42:32 +02002471static void gen6_disable_rps(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002472{
2473 struct drm_i915_private *dev_priv = dev->dev_private;
2474
Eugeni Dodonov88509482012-07-02 11:51:08 -03002475 I915_WRITE(GEN6_RC_CONTROL, 0);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002476 I915_WRITE(GEN6_RPNSWREQ, 1 << 31);
2477 I915_WRITE(GEN6_PMINTRMSK, 0xffffffff);
2478 I915_WRITE(GEN6_PMIER, 0);
2479 /* Complete PM interrupt masking here doesn't race with the rps work
2480 * item again unmasking PM interrupts because that is using a different
2481 * register (PMIMR) to mask PM interrupts. The only risk is in leaving
2482 * stale bits in PMIIR and PMIMR which gen6_enable_rps will clean up. */
2483
Daniel Vetterc6a828d2012-08-08 23:35:35 +02002484 spin_lock_irq(&dev_priv->rps.lock);
2485 dev_priv->rps.pm_iir = 0;
2486 spin_unlock_irq(&dev_priv->rps.lock);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002487
2488 I915_WRITE(GEN6_PMIIR, I915_READ(GEN6_PMIIR));
2489}
2490
2491int intel_enable_rc6(const struct drm_device *dev)
2492{
Daniel Vetter456470e2012-08-08 23:35:40 +02002493 /* Respect the kernel parameter if it is set */
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002494 if (i915_enable_rc6 >= 0)
2495 return i915_enable_rc6;
2496
Daniel Vetter456470e2012-08-08 23:35:40 +02002497 if (INTEL_INFO(dev)->gen == 5) {
Daniel Vettercd7988e2012-08-26 20:33:18 +02002498#ifdef CONFIG_INTEL_IOMMU
2499 /* Disable rc6 on ilk if VT-d is on. */
2500 if (intel_iommu_gfx_mapped)
2501 return false;
2502#endif
Daniel Vetter456470e2012-08-08 23:35:40 +02002503 DRM_DEBUG_DRIVER("Ironlake: only RC6 available\n");
Eugeni Dodonov4a637c22012-07-02 11:51:07 -03002504 return INTEL_RC6_ENABLE;
Daniel Vetter456470e2012-08-08 23:35:40 +02002505 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002506
Daniel Vetter456470e2012-08-08 23:35:40 +02002507 if (IS_HASWELL(dev)) {
2508 DRM_DEBUG_DRIVER("Haswell: only RC6 available\n");
2509 return INTEL_RC6_ENABLE;
2510 }
2511
2512 /* snb/ivb have more than one rc6 state. */
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002513 if (INTEL_INFO(dev)->gen == 6) {
2514 DRM_DEBUG_DRIVER("Sandybridge: deep RC6 disabled\n");
2515 return INTEL_RC6_ENABLE;
2516 }
Daniel Vetter456470e2012-08-08 23:35:40 +02002517
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002518 DRM_DEBUG_DRIVER("RC6 and deep RC6 enabled\n");
2519 return (INTEL_RC6_ENABLE | INTEL_RC6p_ENABLE);
2520}
2521
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02002522static void gen6_enable_rps(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002523{
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02002524 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonb4519512012-05-11 14:29:30 +01002525 struct intel_ring_buffer *ring;
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01002526 u32 rp_state_cap;
2527 u32 gt_perf_status;
Ben Widawsky31643d52012-09-26 10:34:01 -07002528 u32 rc6vids, pcu_mbox, rc6_mask = 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002529 u32 gtfifodbg;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002530 int rc6_mode;
Ben Widawsky42c05262012-09-26 10:34:00 -07002531 int i, ret;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002532
Jesse Barnes4fc688c2012-11-02 11:14:01 -07002533 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02002534
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002535 /* Here begins a magic sequence of register writes to enable
2536 * auto-downclocking.
2537 *
2538 * Perhaps there might be some value in exposing these to
2539 * userspace...
2540 */
2541 I915_WRITE(GEN6_RC_STATE, 0);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002542
2543 /* Clear the DBG now so we don't confuse earlier errors */
2544 if ((gtfifodbg = I915_READ(GTFIFODBG))) {
2545 DRM_ERROR("GT fifo had a previous error %x\n", gtfifodbg);
2546 I915_WRITE(GTFIFODBG, gtfifodbg);
2547 }
2548
2549 gen6_gt_force_wake_get(dev_priv);
2550
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01002551 rp_state_cap = I915_READ(GEN6_RP_STATE_CAP);
2552 gt_perf_status = I915_READ(GEN6_GT_PERF_STATUS);
2553
2554 /* In units of 100MHz */
Daniel Vetterc6a828d2012-08-08 23:35:35 +02002555 dev_priv->rps.max_delay = rp_state_cap & 0xff;
2556 dev_priv->rps.min_delay = (rp_state_cap & 0xff0000) >> 16;
2557 dev_priv->rps.cur_delay = 0;
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01002558
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002559 /* disable the counters and set deterministic thresholds */
2560 I915_WRITE(GEN6_RC_CONTROL, 0);
2561
2562 I915_WRITE(GEN6_RC1_WAKE_RATE_LIMIT, 1000 << 16);
2563 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16 | 30);
2564 I915_WRITE(GEN6_RC6pp_WAKE_RATE_LIMIT, 30);
2565 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
2566 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
2567
Chris Wilsonb4519512012-05-11 14:29:30 +01002568 for_each_ring(ring, dev_priv, i)
2569 I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002570
2571 I915_WRITE(GEN6_RC_SLEEP, 0);
2572 I915_WRITE(GEN6_RC1e_THRESHOLD, 1000);
2573 I915_WRITE(GEN6_RC6_THRESHOLD, 50000);
2574 I915_WRITE(GEN6_RC6p_THRESHOLD, 100000);
2575 I915_WRITE(GEN6_RC6pp_THRESHOLD, 64000); /* unused */
2576
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03002577 /* Check if we are enabling RC6 */
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002578 rc6_mode = intel_enable_rc6(dev_priv->dev);
2579 if (rc6_mode & INTEL_RC6_ENABLE)
2580 rc6_mask |= GEN6_RC_CTL_RC6_ENABLE;
2581
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03002582 /* We don't use those on Haswell */
2583 if (!IS_HASWELL(dev)) {
2584 if (rc6_mode & INTEL_RC6p_ENABLE)
2585 rc6_mask |= GEN6_RC_CTL_RC6p_ENABLE;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002586
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03002587 if (rc6_mode & INTEL_RC6pp_ENABLE)
2588 rc6_mask |= GEN6_RC_CTL_RC6pp_ENABLE;
2589 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002590
2591 DRM_INFO("Enabling RC6 states: RC6 %s, RC6p %s, RC6pp %s\n",
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03002592 (rc6_mask & GEN6_RC_CTL_RC6_ENABLE) ? "on" : "off",
2593 (rc6_mask & GEN6_RC_CTL_RC6p_ENABLE) ? "on" : "off",
2594 (rc6_mask & GEN6_RC_CTL_RC6pp_ENABLE) ? "on" : "off");
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002595
2596 I915_WRITE(GEN6_RC_CONTROL,
2597 rc6_mask |
2598 GEN6_RC_CTL_EI_MODE(1) |
2599 GEN6_RC_CTL_HW_ENABLE);
2600
2601 I915_WRITE(GEN6_RPNSWREQ,
2602 GEN6_FREQUENCY(10) |
2603 GEN6_OFFSET(0) |
2604 GEN6_AGGRESSIVE_TURBO);
2605 I915_WRITE(GEN6_RC_VIDEO_FREQ,
2606 GEN6_FREQUENCY(12));
2607
2608 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 1000000);
2609 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS,
Daniel Vetterc6a828d2012-08-08 23:35:35 +02002610 dev_priv->rps.max_delay << 24 |
2611 dev_priv->rps.min_delay << 16);
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03002612
Daniel Vetter1ee9ae32012-08-15 10:41:45 +02002613 I915_WRITE(GEN6_RP_UP_THRESHOLD, 59400);
2614 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 245000);
2615 I915_WRITE(GEN6_RP_UP_EI, 66000);
2616 I915_WRITE(GEN6_RP_DOWN_EI, 350000);
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03002617
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002618 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
2619 I915_WRITE(GEN6_RP_CONTROL,
2620 GEN6_RP_MEDIA_TURBO |
Jesse Barnes89ba8292012-05-22 09:30:33 -07002621 GEN6_RP_MEDIA_HW_NORMAL_MODE |
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002622 GEN6_RP_MEDIA_IS_GFX |
2623 GEN6_RP_ENABLE |
2624 GEN6_RP_UP_BUSY_AVG |
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03002625 (IS_HASWELL(dev) ? GEN7_RP_DOWN_IDLE_AVG : GEN6_RP_DOWN_IDLE_CONT));
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002626
Ben Widawsky42c05262012-09-26 10:34:00 -07002627 ret = sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_MIN_FREQ_TABLE, 0);
2628 if (!ret) {
2629 pcu_mbox = 0;
2630 ret = sandybridge_pcode_read(dev_priv, GEN6_READ_OC_PARAMS, &pcu_mbox);
2631 if (ret && pcu_mbox & (1<<31)) { /* OC supported */
2632 dev_priv->rps.max_delay = pcu_mbox & 0xff;
2633 DRM_DEBUG_DRIVER("overclocking supported, adjusting frequency max to %dMHz\n", pcu_mbox * 50);
2634 }
2635 } else {
2636 DRM_DEBUG_DRIVER("Failed to set the min frequency\n");
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002637 }
2638
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01002639 gen6_set_rps(dev_priv->dev, (gt_perf_status & 0xff00) >> 8);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002640
2641 /* requires MSI enabled */
Chris Wilsonff928262012-07-05 15:02:17 +01002642 I915_WRITE(GEN6_PMIER, GEN6_PM_DEFERRED_EVENTS);
Daniel Vetterc6a828d2012-08-08 23:35:35 +02002643 spin_lock_irq(&dev_priv->rps.lock);
2644 WARN_ON(dev_priv->rps.pm_iir != 0);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002645 I915_WRITE(GEN6_PMIMR, 0);
Daniel Vetterc6a828d2012-08-08 23:35:35 +02002646 spin_unlock_irq(&dev_priv->rps.lock);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002647 /* enable all PM interrupts */
2648 I915_WRITE(GEN6_PMINTRMSK, 0);
2649
Ben Widawsky31643d52012-09-26 10:34:01 -07002650 rc6vids = 0;
2651 ret = sandybridge_pcode_read(dev_priv, GEN6_PCODE_READ_RC6VIDS, &rc6vids);
2652 if (IS_GEN6(dev) && ret) {
2653 DRM_DEBUG_DRIVER("Couldn't check for BIOS workaround\n");
2654 } else if (IS_GEN6(dev) && (GEN6_DECODE_RC6_VID(rc6vids & 0xff) < 450)) {
2655 DRM_DEBUG_DRIVER("You should update your BIOS. Correcting minimum rc6 voltage (%dmV->%dmV)\n",
2656 GEN6_DECODE_RC6_VID(rc6vids & 0xff), 450);
2657 rc6vids &= 0xffff00;
2658 rc6vids |= GEN6_ENCODE_RC6_VID(450);
2659 ret = sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_RC6VIDS, rc6vids);
2660 if (ret)
2661 DRM_ERROR("Couldn't fix incorrect rc6 voltage\n");
2662 }
2663
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002664 gen6_gt_force_wake_put(dev_priv);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002665}
2666
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02002667static void gen6_update_ring_freq(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002668{
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02002669 struct drm_i915_private *dev_priv = dev->dev_private;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002670 int min_freq = 15;
Jean Delvaree3fef092012-11-12 14:18:02 +01002671 int gpu_freq;
2672 unsigned int ia_freq, max_ia_freq;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002673 int scaling_factor = 180;
2674
Jesse Barnes4fc688c2012-11-02 11:14:01 -07002675 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02002676
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002677 max_ia_freq = cpufreq_quick_get_max(0);
2678 /*
2679 * Default to measured freq if none found, PCU will ensure we don't go
2680 * over
2681 */
2682 if (!max_ia_freq)
2683 max_ia_freq = tsc_khz;
2684
2685 /* Convert from kHz to MHz */
2686 max_ia_freq /= 1000;
2687
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002688 /*
2689 * For each potential GPU frequency, load a ring frequency we'd like
2690 * to use for memory access. We do this by specifying the IA frequency
2691 * the PCU should use as a reference to determine the ring frequency.
2692 */
Daniel Vetterc6a828d2012-08-08 23:35:35 +02002693 for (gpu_freq = dev_priv->rps.max_delay; gpu_freq >= dev_priv->rps.min_delay;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002694 gpu_freq--) {
Daniel Vetterc6a828d2012-08-08 23:35:35 +02002695 int diff = dev_priv->rps.max_delay - gpu_freq;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002696
2697 /*
2698 * For GPU frequencies less than 750MHz, just use the lowest
2699 * ring freq.
2700 */
2701 if (gpu_freq < min_freq)
2702 ia_freq = 800;
2703 else
2704 ia_freq = max_ia_freq - ((diff * scaling_factor) / 2);
2705 ia_freq = DIV_ROUND_CLOSEST(ia_freq, 100);
Ben Widawsky42c05262012-09-26 10:34:00 -07002706 ia_freq <<= GEN6_PCODE_FREQ_IA_RATIO_SHIFT;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002707
Ben Widawsky42c05262012-09-26 10:34:00 -07002708 sandybridge_pcode_write(dev_priv,
2709 GEN6_PCODE_WRITE_MIN_FREQ_TABLE,
2710 ia_freq | gpu_freq);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002711 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002712}
2713
Daniel Vetter930ebb42012-06-29 23:32:16 +02002714void ironlake_teardown_rc6(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002715{
2716 struct drm_i915_private *dev_priv = dev->dev_private;
2717
Daniel Vetter3e373942012-11-02 19:55:04 +01002718 if (dev_priv->ips.renderctx) {
2719 i915_gem_object_unpin(dev_priv->ips.renderctx);
2720 drm_gem_object_unreference(&dev_priv->ips.renderctx->base);
2721 dev_priv->ips.renderctx = NULL;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002722 }
2723
Daniel Vetter3e373942012-11-02 19:55:04 +01002724 if (dev_priv->ips.pwrctx) {
2725 i915_gem_object_unpin(dev_priv->ips.pwrctx);
2726 drm_gem_object_unreference(&dev_priv->ips.pwrctx->base);
2727 dev_priv->ips.pwrctx = NULL;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002728 }
2729}
2730
Daniel Vetter930ebb42012-06-29 23:32:16 +02002731static void ironlake_disable_rc6(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002732{
2733 struct drm_i915_private *dev_priv = dev->dev_private;
2734
2735 if (I915_READ(PWRCTXA)) {
2736 /* Wake the GPU, prevent RC6, then restore RSTDBYCTL */
2737 I915_WRITE(RSTDBYCTL, I915_READ(RSTDBYCTL) | RCX_SW_EXIT);
2738 wait_for(((I915_READ(RSTDBYCTL) & RSX_STATUS_MASK) == RSX_STATUS_ON),
2739 50);
2740
2741 I915_WRITE(PWRCTXA, 0);
2742 POSTING_READ(PWRCTXA);
2743
2744 I915_WRITE(RSTDBYCTL, I915_READ(RSTDBYCTL) & ~RCX_SW_EXIT);
2745 POSTING_READ(RSTDBYCTL);
2746 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002747}
2748
2749static int ironlake_setup_rc6(struct drm_device *dev)
2750{
2751 struct drm_i915_private *dev_priv = dev->dev_private;
2752
Daniel Vetter3e373942012-11-02 19:55:04 +01002753 if (dev_priv->ips.renderctx == NULL)
2754 dev_priv->ips.renderctx = intel_alloc_context_page(dev);
2755 if (!dev_priv->ips.renderctx)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002756 return -ENOMEM;
2757
Daniel Vetter3e373942012-11-02 19:55:04 +01002758 if (dev_priv->ips.pwrctx == NULL)
2759 dev_priv->ips.pwrctx = intel_alloc_context_page(dev);
2760 if (!dev_priv->ips.pwrctx) {
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002761 ironlake_teardown_rc6(dev);
2762 return -ENOMEM;
2763 }
2764
2765 return 0;
2766}
2767
Daniel Vetter930ebb42012-06-29 23:32:16 +02002768static void ironlake_enable_rc6(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002769{
2770 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter6d90c952012-04-26 23:28:05 +02002771 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Chris Wilson3e960502012-11-27 16:22:54 +00002772 bool was_interruptible;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002773 int ret;
2774
2775 /* rc6 disabled by default due to repeated reports of hanging during
2776 * boot and resume.
2777 */
2778 if (!intel_enable_rc6(dev))
2779 return;
2780
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02002781 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2782
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002783 ret = ironlake_setup_rc6(dev);
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02002784 if (ret)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002785 return;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002786
Chris Wilson3e960502012-11-27 16:22:54 +00002787 was_interruptible = dev_priv->mm.interruptible;
2788 dev_priv->mm.interruptible = false;
2789
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002790 /*
2791 * GPU can automatically power down the render unit if given a page
2792 * to save state.
2793 */
Daniel Vetter6d90c952012-04-26 23:28:05 +02002794 ret = intel_ring_begin(ring, 6);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002795 if (ret) {
2796 ironlake_teardown_rc6(dev);
Chris Wilson3e960502012-11-27 16:22:54 +00002797 dev_priv->mm.interruptible = was_interruptible;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002798 return;
2799 }
2800
Daniel Vetter6d90c952012-04-26 23:28:05 +02002801 intel_ring_emit(ring, MI_SUSPEND_FLUSH | MI_SUSPEND_FLUSH_EN);
2802 intel_ring_emit(ring, MI_SET_CONTEXT);
Daniel Vetter3e373942012-11-02 19:55:04 +01002803 intel_ring_emit(ring, dev_priv->ips.renderctx->gtt_offset |
Daniel Vetter6d90c952012-04-26 23:28:05 +02002804 MI_MM_SPACE_GTT |
2805 MI_SAVE_EXT_STATE_EN |
2806 MI_RESTORE_EXT_STATE_EN |
2807 MI_RESTORE_INHIBIT);
2808 intel_ring_emit(ring, MI_SUSPEND_FLUSH);
2809 intel_ring_emit(ring, MI_NOOP);
2810 intel_ring_emit(ring, MI_FLUSH);
2811 intel_ring_advance(ring);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002812
2813 /*
2814 * Wait for the command parser to advance past MI_SET_CONTEXT. The HW
2815 * does an implicit flush, combined with MI_FLUSH above, it should be
2816 * safe to assume that renderctx is valid
2817 */
Chris Wilson3e960502012-11-27 16:22:54 +00002818 ret = intel_ring_idle(ring);
2819 dev_priv->mm.interruptible = was_interruptible;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002820 if (ret) {
2821 DRM_ERROR("failed to enable ironlake power power savings\n");
2822 ironlake_teardown_rc6(dev);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002823 return;
2824 }
2825
Daniel Vetter3e373942012-11-02 19:55:04 +01002826 I915_WRITE(PWRCTXA, dev_priv->ips.pwrctx->gtt_offset | PWRCTX_EN);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002827 I915_WRITE(RSTDBYCTL, I915_READ(RSTDBYCTL) & ~RCX_SW_EXIT);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03002828}
2829
Eugeni Dodonovdde18882012-04-18 15:29:24 -03002830static unsigned long intel_pxfreq(u32 vidfreq)
2831{
2832 unsigned long freq;
2833 int div = (vidfreq & 0x3f0000) >> 16;
2834 int post = (vidfreq & 0x3000) >> 12;
2835 int pre = (vidfreq & 0x7);
2836
2837 if (!pre)
2838 return 0;
2839
2840 freq = ((div * 133333) / ((1<<post) * pre));
2841
2842 return freq;
2843}
2844
Daniel Vettereb48eb02012-04-26 23:28:12 +02002845static const struct cparams {
2846 u16 i;
2847 u16 t;
2848 u16 m;
2849 u16 c;
2850} cparams[] = {
2851 { 1, 1333, 301, 28664 },
2852 { 1, 1066, 294, 24460 },
2853 { 1, 800, 294, 25192 },
2854 { 0, 1333, 276, 27605 },
2855 { 0, 1066, 276, 27605 },
2856 { 0, 800, 231, 23784 },
2857};
2858
Chris Wilsonf531dcb2012-09-25 10:16:12 +01002859static unsigned long __i915_chipset_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02002860{
2861 u64 total_count, diff, ret;
2862 u32 count1, count2, count3, m = 0, c = 0;
2863 unsigned long now = jiffies_to_msecs(jiffies), diff1;
2864 int i;
2865
Daniel Vetter02d71952012-08-09 16:44:54 +02002866 assert_spin_locked(&mchdev_lock);
2867
Daniel Vetter20e4d402012-08-08 23:35:39 +02002868 diff1 = now - dev_priv->ips.last_time1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02002869
2870 /* Prevent division-by-zero if we are asking too fast.
2871 * Also, we don't get interesting results if we are polling
2872 * faster than once in 10ms, so just return the saved value
2873 * in such cases.
2874 */
2875 if (diff1 <= 10)
Daniel Vetter20e4d402012-08-08 23:35:39 +02002876 return dev_priv->ips.chipset_power;
Daniel Vettereb48eb02012-04-26 23:28:12 +02002877
2878 count1 = I915_READ(DMIEC);
2879 count2 = I915_READ(DDREC);
2880 count3 = I915_READ(CSIEC);
2881
2882 total_count = count1 + count2 + count3;
2883
2884 /* FIXME: handle per-counter overflow */
Daniel Vetter20e4d402012-08-08 23:35:39 +02002885 if (total_count < dev_priv->ips.last_count1) {
2886 diff = ~0UL - dev_priv->ips.last_count1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02002887 diff += total_count;
2888 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +02002889 diff = total_count - dev_priv->ips.last_count1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02002890 }
2891
2892 for (i = 0; i < ARRAY_SIZE(cparams); i++) {
Daniel Vetter20e4d402012-08-08 23:35:39 +02002893 if (cparams[i].i == dev_priv->ips.c_m &&
2894 cparams[i].t == dev_priv->ips.r_t) {
Daniel Vettereb48eb02012-04-26 23:28:12 +02002895 m = cparams[i].m;
2896 c = cparams[i].c;
2897 break;
2898 }
2899 }
2900
2901 diff = div_u64(diff, diff1);
2902 ret = ((m * diff) + c);
2903 ret = div_u64(ret, 10);
2904
Daniel Vetter20e4d402012-08-08 23:35:39 +02002905 dev_priv->ips.last_count1 = total_count;
2906 dev_priv->ips.last_time1 = now;
Daniel Vettereb48eb02012-04-26 23:28:12 +02002907
Daniel Vetter20e4d402012-08-08 23:35:39 +02002908 dev_priv->ips.chipset_power = ret;
Daniel Vettereb48eb02012-04-26 23:28:12 +02002909
2910 return ret;
2911}
2912
Chris Wilsonf531dcb2012-09-25 10:16:12 +01002913unsigned long i915_chipset_val(struct drm_i915_private *dev_priv)
2914{
2915 unsigned long val;
2916
2917 if (dev_priv->info->gen != 5)
2918 return 0;
2919
2920 spin_lock_irq(&mchdev_lock);
2921
2922 val = __i915_chipset_val(dev_priv);
2923
2924 spin_unlock_irq(&mchdev_lock);
2925
2926 return val;
2927}
2928
Daniel Vettereb48eb02012-04-26 23:28:12 +02002929unsigned long i915_mch_val(struct drm_i915_private *dev_priv)
2930{
2931 unsigned long m, x, b;
2932 u32 tsfs;
2933
2934 tsfs = I915_READ(TSFS);
2935
2936 m = ((tsfs & TSFS_SLOPE_MASK) >> TSFS_SLOPE_SHIFT);
2937 x = I915_READ8(TR1);
2938
2939 b = tsfs & TSFS_INTR_MASK;
2940
2941 return ((m * x) / 127) - b;
2942}
2943
2944static u16 pvid_to_extvid(struct drm_i915_private *dev_priv, u8 pxvid)
2945{
2946 static const struct v_table {
2947 u16 vd; /* in .1 mil */
2948 u16 vm; /* in .1 mil */
2949 } v_table[] = {
2950 { 0, 0, },
2951 { 375, 0, },
2952 { 500, 0, },
2953 { 625, 0, },
2954 { 750, 0, },
2955 { 875, 0, },
2956 { 1000, 0, },
2957 { 1125, 0, },
2958 { 4125, 3000, },
2959 { 4125, 3000, },
2960 { 4125, 3000, },
2961 { 4125, 3000, },
2962 { 4125, 3000, },
2963 { 4125, 3000, },
2964 { 4125, 3000, },
2965 { 4125, 3000, },
2966 { 4125, 3000, },
2967 { 4125, 3000, },
2968 { 4125, 3000, },
2969 { 4125, 3000, },
2970 { 4125, 3000, },
2971 { 4125, 3000, },
2972 { 4125, 3000, },
2973 { 4125, 3000, },
2974 { 4125, 3000, },
2975 { 4125, 3000, },
2976 { 4125, 3000, },
2977 { 4125, 3000, },
2978 { 4125, 3000, },
2979 { 4125, 3000, },
2980 { 4125, 3000, },
2981 { 4125, 3000, },
2982 { 4250, 3125, },
2983 { 4375, 3250, },
2984 { 4500, 3375, },
2985 { 4625, 3500, },
2986 { 4750, 3625, },
2987 { 4875, 3750, },
2988 { 5000, 3875, },
2989 { 5125, 4000, },
2990 { 5250, 4125, },
2991 { 5375, 4250, },
2992 { 5500, 4375, },
2993 { 5625, 4500, },
2994 { 5750, 4625, },
2995 { 5875, 4750, },
2996 { 6000, 4875, },
2997 { 6125, 5000, },
2998 { 6250, 5125, },
2999 { 6375, 5250, },
3000 { 6500, 5375, },
3001 { 6625, 5500, },
3002 { 6750, 5625, },
3003 { 6875, 5750, },
3004 { 7000, 5875, },
3005 { 7125, 6000, },
3006 { 7250, 6125, },
3007 { 7375, 6250, },
3008 { 7500, 6375, },
3009 { 7625, 6500, },
3010 { 7750, 6625, },
3011 { 7875, 6750, },
3012 { 8000, 6875, },
3013 { 8125, 7000, },
3014 { 8250, 7125, },
3015 { 8375, 7250, },
3016 { 8500, 7375, },
3017 { 8625, 7500, },
3018 { 8750, 7625, },
3019 { 8875, 7750, },
3020 { 9000, 7875, },
3021 { 9125, 8000, },
3022 { 9250, 8125, },
3023 { 9375, 8250, },
3024 { 9500, 8375, },
3025 { 9625, 8500, },
3026 { 9750, 8625, },
3027 { 9875, 8750, },
3028 { 10000, 8875, },
3029 { 10125, 9000, },
3030 { 10250, 9125, },
3031 { 10375, 9250, },
3032 { 10500, 9375, },
3033 { 10625, 9500, },
3034 { 10750, 9625, },
3035 { 10875, 9750, },
3036 { 11000, 9875, },
3037 { 11125, 10000, },
3038 { 11250, 10125, },
3039 { 11375, 10250, },
3040 { 11500, 10375, },
3041 { 11625, 10500, },
3042 { 11750, 10625, },
3043 { 11875, 10750, },
3044 { 12000, 10875, },
3045 { 12125, 11000, },
3046 { 12250, 11125, },
3047 { 12375, 11250, },
3048 { 12500, 11375, },
3049 { 12625, 11500, },
3050 { 12750, 11625, },
3051 { 12875, 11750, },
3052 { 13000, 11875, },
3053 { 13125, 12000, },
3054 { 13250, 12125, },
3055 { 13375, 12250, },
3056 { 13500, 12375, },
3057 { 13625, 12500, },
3058 { 13750, 12625, },
3059 { 13875, 12750, },
3060 { 14000, 12875, },
3061 { 14125, 13000, },
3062 { 14250, 13125, },
3063 { 14375, 13250, },
3064 { 14500, 13375, },
3065 { 14625, 13500, },
3066 { 14750, 13625, },
3067 { 14875, 13750, },
3068 { 15000, 13875, },
3069 { 15125, 14000, },
3070 { 15250, 14125, },
3071 { 15375, 14250, },
3072 { 15500, 14375, },
3073 { 15625, 14500, },
3074 { 15750, 14625, },
3075 { 15875, 14750, },
3076 { 16000, 14875, },
3077 { 16125, 15000, },
3078 };
3079 if (dev_priv->info->is_mobile)
3080 return v_table[pxvid].vm;
3081 else
3082 return v_table[pxvid].vd;
3083}
3084
Daniel Vetter02d71952012-08-09 16:44:54 +02003085static void __i915_update_gfx_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02003086{
3087 struct timespec now, diff1;
3088 u64 diff;
3089 unsigned long diffms;
3090 u32 count;
3091
Daniel Vetter02d71952012-08-09 16:44:54 +02003092 assert_spin_locked(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02003093
3094 getrawmonotonic(&now);
Daniel Vetter20e4d402012-08-08 23:35:39 +02003095 diff1 = timespec_sub(now, dev_priv->ips.last_time2);
Daniel Vettereb48eb02012-04-26 23:28:12 +02003096
3097 /* Don't divide by 0 */
3098 diffms = diff1.tv_sec * 1000 + diff1.tv_nsec / 1000000;
3099 if (!diffms)
3100 return;
3101
3102 count = I915_READ(GFXEC);
3103
Daniel Vetter20e4d402012-08-08 23:35:39 +02003104 if (count < dev_priv->ips.last_count2) {
3105 diff = ~0UL - dev_priv->ips.last_count2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02003106 diff += count;
3107 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +02003108 diff = count - dev_priv->ips.last_count2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02003109 }
3110
Daniel Vetter20e4d402012-08-08 23:35:39 +02003111 dev_priv->ips.last_count2 = count;
3112 dev_priv->ips.last_time2 = now;
Daniel Vettereb48eb02012-04-26 23:28:12 +02003113
3114 /* More magic constants... */
3115 diff = diff * 1181;
3116 diff = div_u64(diff, diffms * 10);
Daniel Vetter20e4d402012-08-08 23:35:39 +02003117 dev_priv->ips.gfx_power = diff;
Daniel Vettereb48eb02012-04-26 23:28:12 +02003118}
3119
Daniel Vetter02d71952012-08-09 16:44:54 +02003120void i915_update_gfx_val(struct drm_i915_private *dev_priv)
3121{
3122 if (dev_priv->info->gen != 5)
3123 return;
3124
Daniel Vetter92703882012-08-09 16:46:01 +02003125 spin_lock_irq(&mchdev_lock);
Daniel Vetter02d71952012-08-09 16:44:54 +02003126
3127 __i915_update_gfx_val(dev_priv);
3128
Daniel Vetter92703882012-08-09 16:46:01 +02003129 spin_unlock_irq(&mchdev_lock);
Daniel Vetter02d71952012-08-09 16:44:54 +02003130}
3131
Chris Wilsonf531dcb2012-09-25 10:16:12 +01003132static unsigned long __i915_gfx_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02003133{
3134 unsigned long t, corr, state1, corr2, state2;
3135 u32 pxvid, ext_v;
3136
Daniel Vetter02d71952012-08-09 16:44:54 +02003137 assert_spin_locked(&mchdev_lock);
3138
Daniel Vetterc6a828d2012-08-08 23:35:35 +02003139 pxvid = I915_READ(PXVFREQ_BASE + (dev_priv->rps.cur_delay * 4));
Daniel Vettereb48eb02012-04-26 23:28:12 +02003140 pxvid = (pxvid >> 24) & 0x7f;
3141 ext_v = pvid_to_extvid(dev_priv, pxvid);
3142
3143 state1 = ext_v;
3144
3145 t = i915_mch_val(dev_priv);
3146
3147 /* Revel in the empirically derived constants */
3148
3149 /* Correction factor in 1/100000 units */
3150 if (t > 80)
3151 corr = ((t * 2349) + 135940);
3152 else if (t >= 50)
3153 corr = ((t * 964) + 29317);
3154 else /* < 50 */
3155 corr = ((t * 301) + 1004);
3156
3157 corr = corr * ((150142 * state1) / 10000 - 78642);
3158 corr /= 100000;
Daniel Vetter20e4d402012-08-08 23:35:39 +02003159 corr2 = (corr * dev_priv->ips.corr);
Daniel Vettereb48eb02012-04-26 23:28:12 +02003160
3161 state2 = (corr2 * state1) / 10000;
3162 state2 /= 100; /* convert to mW */
3163
Daniel Vetter02d71952012-08-09 16:44:54 +02003164 __i915_update_gfx_val(dev_priv);
Daniel Vettereb48eb02012-04-26 23:28:12 +02003165
Daniel Vetter20e4d402012-08-08 23:35:39 +02003166 return dev_priv->ips.gfx_power + state2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02003167}
3168
Chris Wilsonf531dcb2012-09-25 10:16:12 +01003169unsigned long i915_gfx_val(struct drm_i915_private *dev_priv)
3170{
3171 unsigned long val;
3172
3173 if (dev_priv->info->gen != 5)
3174 return 0;
3175
3176 spin_lock_irq(&mchdev_lock);
3177
3178 val = __i915_gfx_val(dev_priv);
3179
3180 spin_unlock_irq(&mchdev_lock);
3181
3182 return val;
3183}
3184
Daniel Vettereb48eb02012-04-26 23:28:12 +02003185/**
3186 * i915_read_mch_val - return value for IPS use
3187 *
3188 * Calculate and return a value for the IPS driver to use when deciding whether
3189 * we have thermal and power headroom to increase CPU or GPU power budget.
3190 */
3191unsigned long i915_read_mch_val(void)
3192{
3193 struct drm_i915_private *dev_priv;
3194 unsigned long chipset_val, graphics_val, ret = 0;
3195
Daniel Vetter92703882012-08-09 16:46:01 +02003196 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02003197 if (!i915_mch_dev)
3198 goto out_unlock;
3199 dev_priv = i915_mch_dev;
3200
Chris Wilsonf531dcb2012-09-25 10:16:12 +01003201 chipset_val = __i915_chipset_val(dev_priv);
3202 graphics_val = __i915_gfx_val(dev_priv);
Daniel Vettereb48eb02012-04-26 23:28:12 +02003203
3204 ret = chipset_val + graphics_val;
3205
3206out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02003207 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02003208
3209 return ret;
3210}
3211EXPORT_SYMBOL_GPL(i915_read_mch_val);
3212
3213/**
3214 * i915_gpu_raise - raise GPU frequency limit
3215 *
3216 * Raise the limit; IPS indicates we have thermal headroom.
3217 */
3218bool i915_gpu_raise(void)
3219{
3220 struct drm_i915_private *dev_priv;
3221 bool ret = true;
3222
Daniel Vetter92703882012-08-09 16:46:01 +02003223 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02003224 if (!i915_mch_dev) {
3225 ret = false;
3226 goto out_unlock;
3227 }
3228 dev_priv = i915_mch_dev;
3229
Daniel Vetter20e4d402012-08-08 23:35:39 +02003230 if (dev_priv->ips.max_delay > dev_priv->ips.fmax)
3231 dev_priv->ips.max_delay--;
Daniel Vettereb48eb02012-04-26 23:28:12 +02003232
3233out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02003234 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02003235
3236 return ret;
3237}
3238EXPORT_SYMBOL_GPL(i915_gpu_raise);
3239
3240/**
3241 * i915_gpu_lower - lower GPU frequency limit
3242 *
3243 * IPS indicates we're close to a thermal limit, so throttle back the GPU
3244 * frequency maximum.
3245 */
3246bool i915_gpu_lower(void)
3247{
3248 struct drm_i915_private *dev_priv;
3249 bool ret = true;
3250
Daniel Vetter92703882012-08-09 16:46:01 +02003251 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02003252 if (!i915_mch_dev) {
3253 ret = false;
3254 goto out_unlock;
3255 }
3256 dev_priv = i915_mch_dev;
3257
Daniel Vetter20e4d402012-08-08 23:35:39 +02003258 if (dev_priv->ips.max_delay < dev_priv->ips.min_delay)
3259 dev_priv->ips.max_delay++;
Daniel Vettereb48eb02012-04-26 23:28:12 +02003260
3261out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02003262 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02003263
3264 return ret;
3265}
3266EXPORT_SYMBOL_GPL(i915_gpu_lower);
3267
3268/**
3269 * i915_gpu_busy - indicate GPU business to IPS
3270 *
3271 * Tell the IPS driver whether or not the GPU is busy.
3272 */
3273bool i915_gpu_busy(void)
3274{
3275 struct drm_i915_private *dev_priv;
Chris Wilsonf047e392012-07-21 12:31:41 +01003276 struct intel_ring_buffer *ring;
Daniel Vettereb48eb02012-04-26 23:28:12 +02003277 bool ret = false;
Chris Wilsonf047e392012-07-21 12:31:41 +01003278 int i;
Daniel Vettereb48eb02012-04-26 23:28:12 +02003279
Daniel Vetter92703882012-08-09 16:46:01 +02003280 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02003281 if (!i915_mch_dev)
3282 goto out_unlock;
3283 dev_priv = i915_mch_dev;
3284
Chris Wilsonf047e392012-07-21 12:31:41 +01003285 for_each_ring(ring, dev_priv, i)
3286 ret |= !list_empty(&ring->request_list);
Daniel Vettereb48eb02012-04-26 23:28:12 +02003287
3288out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02003289 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02003290
3291 return ret;
3292}
3293EXPORT_SYMBOL_GPL(i915_gpu_busy);
3294
3295/**
3296 * i915_gpu_turbo_disable - disable graphics turbo
3297 *
3298 * Disable graphics turbo by resetting the max frequency and setting the
3299 * current frequency to the default.
3300 */
3301bool i915_gpu_turbo_disable(void)
3302{
3303 struct drm_i915_private *dev_priv;
3304 bool ret = true;
3305
Daniel Vetter92703882012-08-09 16:46:01 +02003306 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02003307 if (!i915_mch_dev) {
3308 ret = false;
3309 goto out_unlock;
3310 }
3311 dev_priv = i915_mch_dev;
3312
Daniel Vetter20e4d402012-08-08 23:35:39 +02003313 dev_priv->ips.max_delay = dev_priv->ips.fstart;
Daniel Vettereb48eb02012-04-26 23:28:12 +02003314
Daniel Vetter20e4d402012-08-08 23:35:39 +02003315 if (!ironlake_set_drps(dev_priv->dev, dev_priv->ips.fstart))
Daniel Vettereb48eb02012-04-26 23:28:12 +02003316 ret = false;
3317
3318out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02003319 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02003320
3321 return ret;
3322}
3323EXPORT_SYMBOL_GPL(i915_gpu_turbo_disable);
3324
3325/**
3326 * Tells the intel_ips driver that the i915 driver is now loaded, if
3327 * IPS got loaded first.
3328 *
3329 * This awkward dance is so that neither module has to depend on the
3330 * other in order for IPS to do the appropriate communication of
3331 * GPU turbo limits to i915.
3332 */
3333static void
3334ips_ping_for_i915_load(void)
3335{
3336 void (*link)(void);
3337
3338 link = symbol_get(ips_link_to_i915_driver);
3339 if (link) {
3340 link();
3341 symbol_put(ips_link_to_i915_driver);
3342 }
3343}
3344
3345void intel_gpu_ips_init(struct drm_i915_private *dev_priv)
3346{
Daniel Vetter02d71952012-08-09 16:44:54 +02003347 /* We only register the i915 ips part with intel-ips once everything is
3348 * set up, to avoid intel-ips sneaking in and reading bogus values. */
Daniel Vetter92703882012-08-09 16:46:01 +02003349 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02003350 i915_mch_dev = dev_priv;
Daniel Vetter92703882012-08-09 16:46:01 +02003351 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02003352
3353 ips_ping_for_i915_load();
3354}
3355
3356void intel_gpu_ips_teardown(void)
3357{
Daniel Vetter92703882012-08-09 16:46:01 +02003358 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02003359 i915_mch_dev = NULL;
Daniel Vetter92703882012-08-09 16:46:01 +02003360 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02003361}
Daniel Vetter8090c6b2012-06-24 16:42:32 +02003362static void intel_init_emon(struct drm_device *dev)
Eugeni Dodonovdde18882012-04-18 15:29:24 -03003363{
3364 struct drm_i915_private *dev_priv = dev->dev_private;
3365 u32 lcfuse;
3366 u8 pxw[16];
3367 int i;
3368
3369 /* Disable to program */
3370 I915_WRITE(ECR, 0);
3371 POSTING_READ(ECR);
3372
3373 /* Program energy weights for various events */
3374 I915_WRITE(SDEW, 0x15040d00);
3375 I915_WRITE(CSIEW0, 0x007f0000);
3376 I915_WRITE(CSIEW1, 0x1e220004);
3377 I915_WRITE(CSIEW2, 0x04000004);
3378
3379 for (i = 0; i < 5; i++)
3380 I915_WRITE(PEW + (i * 4), 0);
3381 for (i = 0; i < 3; i++)
3382 I915_WRITE(DEW + (i * 4), 0);
3383
3384 /* Program P-state weights to account for frequency power adjustment */
3385 for (i = 0; i < 16; i++) {
3386 u32 pxvidfreq = I915_READ(PXVFREQ_BASE + (i * 4));
3387 unsigned long freq = intel_pxfreq(pxvidfreq);
3388 unsigned long vid = (pxvidfreq & PXVFREQ_PX_MASK) >>
3389 PXVFREQ_PX_SHIFT;
3390 unsigned long val;
3391
3392 val = vid * vid;
3393 val *= (freq / 1000);
3394 val *= 255;
3395 val /= (127*127*900);
3396 if (val > 0xff)
3397 DRM_ERROR("bad pxval: %ld\n", val);
3398 pxw[i] = val;
3399 }
3400 /* Render standby states get 0 weight */
3401 pxw[14] = 0;
3402 pxw[15] = 0;
3403
3404 for (i = 0; i < 4; i++) {
3405 u32 val = (pxw[i*4] << 24) | (pxw[(i*4)+1] << 16) |
3406 (pxw[(i*4)+2] << 8) | (pxw[(i*4)+3]);
3407 I915_WRITE(PXW + (i * 4), val);
3408 }
3409
3410 /* Adjust magic regs to magic values (more experimental results) */
3411 I915_WRITE(OGW0, 0);
3412 I915_WRITE(OGW1, 0);
3413 I915_WRITE(EG0, 0x00007f00);
3414 I915_WRITE(EG1, 0x0000000e);
3415 I915_WRITE(EG2, 0x000e0000);
3416 I915_WRITE(EG3, 0x68000300);
3417 I915_WRITE(EG4, 0x42000000);
3418 I915_WRITE(EG5, 0x00140031);
3419 I915_WRITE(EG6, 0);
3420 I915_WRITE(EG7, 0);
3421
3422 for (i = 0; i < 8; i++)
3423 I915_WRITE(PXWL + (i * 4), 0);
3424
3425 /* Enable PMON + select events */
3426 I915_WRITE(ECR, 0x80000019);
3427
3428 lcfuse = I915_READ(LCFUSE02);
3429
Daniel Vetter20e4d402012-08-08 23:35:39 +02003430 dev_priv->ips.corr = (lcfuse & LCFUSE_HIV_MASK);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03003431}
3432
Daniel Vetter8090c6b2012-06-24 16:42:32 +02003433void intel_disable_gt_powersave(struct drm_device *dev)
3434{
Jesse Barnes1a01ab32012-11-02 11:14:00 -07003435 struct drm_i915_private *dev_priv = dev->dev_private;
3436
Daniel Vetter930ebb42012-06-29 23:32:16 +02003437 if (IS_IRONLAKE_M(dev)) {
Daniel Vetter8090c6b2012-06-24 16:42:32 +02003438 ironlake_disable_drps(dev);
Daniel Vetter930ebb42012-06-29 23:32:16 +02003439 ironlake_disable_rc6(dev);
3440 } else if (INTEL_INFO(dev)->gen >= 6 && !IS_VALLEYVIEW(dev)) {
Jesse Barnes1a01ab32012-11-02 11:14:00 -07003441 cancel_delayed_work_sync(&dev_priv->rps.delayed_resume_work);
Jesse Barnes4fc688c2012-11-02 11:14:01 -07003442 mutex_lock(&dev_priv->rps.hw_lock);
Daniel Vetter8090c6b2012-06-24 16:42:32 +02003443 gen6_disable_rps(dev);
Jesse Barnes4fc688c2012-11-02 11:14:01 -07003444 mutex_unlock(&dev_priv->rps.hw_lock);
Daniel Vetter930ebb42012-06-29 23:32:16 +02003445 }
Daniel Vetter8090c6b2012-06-24 16:42:32 +02003446}
3447
Jesse Barnes1a01ab32012-11-02 11:14:00 -07003448static void intel_gen6_powersave_work(struct work_struct *work)
3449{
3450 struct drm_i915_private *dev_priv =
3451 container_of(work, struct drm_i915_private,
3452 rps.delayed_resume_work.work);
3453 struct drm_device *dev = dev_priv->dev;
3454
Jesse Barnes4fc688c2012-11-02 11:14:01 -07003455 mutex_lock(&dev_priv->rps.hw_lock);
Jesse Barnes1a01ab32012-11-02 11:14:00 -07003456 gen6_enable_rps(dev);
3457 gen6_update_ring_freq(dev);
Jesse Barnes4fc688c2012-11-02 11:14:01 -07003458 mutex_unlock(&dev_priv->rps.hw_lock);
Jesse Barnes1a01ab32012-11-02 11:14:00 -07003459}
3460
Daniel Vetter8090c6b2012-06-24 16:42:32 +02003461void intel_enable_gt_powersave(struct drm_device *dev)
3462{
Jesse Barnes1a01ab32012-11-02 11:14:00 -07003463 struct drm_i915_private *dev_priv = dev->dev_private;
3464
Daniel Vetter8090c6b2012-06-24 16:42:32 +02003465 if (IS_IRONLAKE_M(dev)) {
3466 ironlake_enable_drps(dev);
3467 ironlake_enable_rc6(dev);
3468 intel_init_emon(dev);
Eugeni Dodonov7cf50fc2012-07-02 11:51:06 -03003469 } else if ((IS_GEN6(dev) || IS_GEN7(dev)) && !IS_VALLEYVIEW(dev)) {
Jesse Barnes1a01ab32012-11-02 11:14:00 -07003470 /*
3471 * PCU communication is slow and this doesn't need to be
3472 * done at any specific time, so do this out of our fast path
3473 * to make resume and init faster.
3474 */
3475 schedule_delayed_work(&dev_priv->rps.delayed_resume_work,
3476 round_jiffies_up_relative(HZ));
Daniel Vetter8090c6b2012-06-24 16:42:32 +02003477 }
3478}
3479
Daniel Vetter3107bd42012-10-31 22:52:31 +01003480static void ibx_init_clock_gating(struct drm_device *dev)
3481{
3482 struct drm_i915_private *dev_priv = dev->dev_private;
3483
3484 /*
3485 * On Ibex Peak and Cougar Point, we need to disable clock
3486 * gating for the panel power sequencer or it will fail to
3487 * start up when no ports are active.
3488 */
3489 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE);
3490}
3491
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03003492static void ironlake_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03003493{
3494 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau231e54f2012-10-19 17:55:41 +01003495 uint32_t dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03003496
3497 /* Required for FBC */
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01003498 dspclk_gate |= ILK_DPFCRUNIT_CLOCK_GATE_DISABLE |
3499 ILK_DPFCUNIT_CLOCK_GATE_DISABLE |
3500 ILK_DPFDUNIT_CLOCK_GATE_ENABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03003501
3502 I915_WRITE(PCH_3DCGDIS0,
3503 MARIUNIT_CLOCK_GATE_DISABLE |
3504 SVSMUNIT_CLOCK_GATE_DISABLE);
3505 I915_WRITE(PCH_3DCGDIS1,
3506 VFMUNIT_CLOCK_GATE_DISABLE);
3507
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03003508 /*
3509 * According to the spec the following bits should be set in
3510 * order to enable memory self-refresh
3511 * The bit 22/21 of 0x42004
3512 * The bit 5 of 0x42020
3513 * The bit 15 of 0x45000
3514 */
3515 I915_WRITE(ILK_DISPLAY_CHICKEN2,
3516 (I915_READ(ILK_DISPLAY_CHICKEN2) |
3517 ILK_DPARB_GATE | ILK_VSDPFD_FULL));
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01003518 dspclk_gate |= ILK_DPARBUNIT_CLOCK_GATE_ENABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03003519 I915_WRITE(DISP_ARB_CTL,
3520 (I915_READ(DISP_ARB_CTL) |
3521 DISP_FBC_WM_DIS));
3522 I915_WRITE(WM3_LP_ILK, 0);
3523 I915_WRITE(WM2_LP_ILK, 0);
3524 I915_WRITE(WM1_LP_ILK, 0);
3525
3526 /*
3527 * Based on the document from hardware guys the following bits
3528 * should be set unconditionally in order to enable FBC.
3529 * The bit 22 of 0x42000
3530 * The bit 22 of 0x42004
3531 * The bit 7,8,9 of 0x42020.
3532 */
3533 if (IS_IRONLAKE_M(dev)) {
3534 I915_WRITE(ILK_DISPLAY_CHICKEN1,
3535 I915_READ(ILK_DISPLAY_CHICKEN1) |
3536 ILK_FBCQ_DIS);
3537 I915_WRITE(ILK_DISPLAY_CHICKEN2,
3538 I915_READ(ILK_DISPLAY_CHICKEN2) |
3539 ILK_DPARB_GATE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03003540 }
3541
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01003542 I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
3543
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03003544 I915_WRITE(ILK_DISPLAY_CHICKEN2,
3545 I915_READ(ILK_DISPLAY_CHICKEN2) |
3546 ILK_ELPIN_409_SELECT);
3547 I915_WRITE(_3D_CHICKEN2,
3548 _3D_CHICKEN2_WM_READ_PIPELINED << 16 |
3549 _3D_CHICKEN2_WM_READ_PIPELINED);
Daniel Vetter4358a372012-10-18 11:49:51 +02003550
3551 /* WaDisableRenderCachePipelinedFlush */
3552 I915_WRITE(CACHE_MODE_0,
3553 _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
Daniel Vetter3107bd42012-10-31 22:52:31 +01003554
3555 ibx_init_clock_gating(dev);
3556}
3557
3558static void cpt_init_clock_gating(struct drm_device *dev)
3559{
3560 struct drm_i915_private *dev_priv = dev->dev_private;
3561 int pipe;
3562
3563 /*
3564 * On Ibex Peak and Cougar Point, we need to disable clock
3565 * gating for the panel power sequencer or it will fail to
3566 * start up when no ports are active.
3567 */
3568 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE);
3569 I915_WRITE(SOUTH_CHICKEN2, I915_READ(SOUTH_CHICKEN2) |
3570 DPLS_EDP_PPS_FIX_DIS);
Takashi Iwai335c07b2012-12-11 11:46:29 +01003571 /* The below fixes the weird display corruption, a few pixels shifted
3572 * downward, on (only) LVDS of some HP laptops with IVY.
3573 */
3574 for_each_pipe(pipe)
3575 I915_WRITE(TRANS_CHICKEN2(pipe), TRANS_CHICKEN2_TIMING_OVERRIDE);
Daniel Vetter3107bd42012-10-31 22:52:31 +01003576 /* WADP0ClockGatingDisable */
3577 for_each_pipe(pipe) {
3578 I915_WRITE(TRANS_CHICKEN1(pipe),
3579 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
3580 }
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03003581}
3582
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03003583static void gen6_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03003584{
3585 struct drm_i915_private *dev_priv = dev->dev_private;
3586 int pipe;
Damien Lespiau231e54f2012-10-19 17:55:41 +01003587 uint32_t dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03003588
Damien Lespiau231e54f2012-10-19 17:55:41 +01003589 I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03003590
3591 I915_WRITE(ILK_DISPLAY_CHICKEN2,
3592 I915_READ(ILK_DISPLAY_CHICKEN2) |
3593 ILK_ELPIN_409_SELECT);
3594
Daniel Vetter42839082012-12-14 23:38:28 +01003595 /* WaDisableHiZPlanesWhenMSAAEnabled */
3596 I915_WRITE(_3D_CHICKEN,
3597 _MASKED_BIT_ENABLE(_3D_CHICKEN_HIZ_PLANE_DISABLE_MSAA_4X_SNB));
3598
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03003599 I915_WRITE(WM3_LP_ILK, 0);
3600 I915_WRITE(WM2_LP_ILK, 0);
3601 I915_WRITE(WM1_LP_ILK, 0);
3602
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03003603 I915_WRITE(CACHE_MODE_0,
Daniel Vetter50743292012-04-26 22:02:54 +02003604 _MASKED_BIT_DISABLE(CM0_STC_EVICT_DISABLE_LRA_SNB));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03003605
3606 I915_WRITE(GEN6_UCGCTL1,
3607 I915_READ(GEN6_UCGCTL1) |
3608 GEN6_BLBUNIT_CLOCK_GATE_DISABLE |
3609 GEN6_CSUNIT_CLOCK_GATE_DISABLE);
3610
3611 /* According to the BSpec vol1g, bit 12 (RCPBUNIT) clock
3612 * gating disable must be set. Failure to set it results in
3613 * flickering pixels due to Z write ordering failures after
3614 * some amount of runtime in the Mesa "fire" demo, and Unigine
3615 * Sanctuary and Tropics, and apparently anything else with
3616 * alpha test or pixel discard.
3617 *
3618 * According to the spec, bit 11 (RCCUNIT) must also be set,
3619 * but we didn't debug actual testcases to find it out.
Jesse Barnes0f846f82012-06-14 11:04:47 -07003620 *
3621 * Also apply WaDisableVDSUnitClockGating and
3622 * WaDisableRCPBUnitClockGating.
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03003623 */
3624 I915_WRITE(GEN6_UCGCTL2,
Jesse Barnes0f846f82012-06-14 11:04:47 -07003625 GEN7_VDSUNIT_CLOCK_GATE_DISABLE |
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03003626 GEN6_RCPBUNIT_CLOCK_GATE_DISABLE |
3627 GEN6_RCCUNIT_CLOCK_GATE_DISABLE);
3628
3629 /* Bspec says we need to always set all mask bits. */
Kenneth Graunke26b6e442012-10-07 08:51:07 -07003630 I915_WRITE(_3D_CHICKEN3, (0xFFFF << 16) |
3631 _3D_CHICKEN3_SF_DISABLE_FASTCLIP_CULL);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03003632
3633 /*
3634 * According to the spec the following bits should be
3635 * set in order to enable memory self-refresh and fbc:
3636 * The bit21 and bit22 of 0x42000
3637 * The bit21 and bit22 of 0x42004
3638 * The bit5 and bit7 of 0x42020
3639 * The bit14 of 0x70180
3640 * The bit14 of 0x71180
3641 */
3642 I915_WRITE(ILK_DISPLAY_CHICKEN1,
3643 I915_READ(ILK_DISPLAY_CHICKEN1) |
3644 ILK_FBCQ_DIS | ILK_PABSTRETCH_DIS);
3645 I915_WRITE(ILK_DISPLAY_CHICKEN2,
3646 I915_READ(ILK_DISPLAY_CHICKEN2) |
3647 ILK_DPARB_GATE | ILK_VSDPFD_FULL);
Damien Lespiau231e54f2012-10-19 17:55:41 +01003648 I915_WRITE(ILK_DSPCLK_GATE_D,
3649 I915_READ(ILK_DSPCLK_GATE_D) |
3650 ILK_DPARBUNIT_CLOCK_GATE_ENABLE |
3651 ILK_DPFDUNIT_CLOCK_GATE_ENABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03003652
Paulo Zanonib3bf0762012-11-20 13:27:44 -02003653 /* WaMbcDriverBootEnable */
Jesse Barnesb4ae3f22012-06-14 11:04:48 -07003654 I915_WRITE(GEN6_MBCTL, I915_READ(GEN6_MBCTL) |
3655 GEN6_MBCTL_ENABLE_BOOT_FETCH);
3656
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03003657 for_each_pipe(pipe) {
3658 I915_WRITE(DSPCNTR(pipe),
3659 I915_READ(DSPCNTR(pipe)) |
3660 DISPPLANE_TRICKLE_FEED_DISABLE);
3661 intel_flush_display_plane(dev_priv, pipe);
3662 }
Ben Widawskyf8f2ac92012-10-03 19:34:24 -07003663
3664 /* The default value should be 0x200 according to docs, but the two
3665 * platforms I checked have a 0 for this. (Maybe BIOS overrides?) */
3666 I915_WRITE(GEN6_GT_MODE, _MASKED_BIT_DISABLE(0xffff));
3667 I915_WRITE(GEN6_GT_MODE, _MASKED_BIT_ENABLE(GEN6_GT_MODE_HI));
Daniel Vetter3107bd42012-10-31 22:52:31 +01003668
3669 cpt_init_clock_gating(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03003670}
3671
3672static void gen7_setup_fixed_func_scheduler(struct drm_i915_private *dev_priv)
3673{
3674 uint32_t reg = I915_READ(GEN7_FF_THREAD_MODE);
3675
3676 reg &= ~GEN7_FF_SCHED_MASK;
3677 reg |= GEN7_FF_TS_SCHED_HW;
3678 reg |= GEN7_FF_VS_SCHED_HW;
3679 reg |= GEN7_FF_DS_SCHED_HW;
3680
3681 I915_WRITE(GEN7_FF_THREAD_MODE, reg);
3682}
3683
Paulo Zanoni17a303e2012-11-20 15:12:07 -02003684static void lpt_init_clock_gating(struct drm_device *dev)
3685{
3686 struct drm_i915_private *dev_priv = dev->dev_private;
3687
3688 /*
3689 * TODO: this bit should only be enabled when really needed, then
3690 * disabled when not needed anymore in order to save power.
3691 */
3692 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE)
3693 I915_WRITE(SOUTH_DSPCLK_GATE_D,
3694 I915_READ(SOUTH_DSPCLK_GATE_D) |
3695 PCH_LP_PARTITION_LEVEL_DISABLE);
3696}
3697
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03003698static void haswell_init_clock_gating(struct drm_device *dev)
3699{
3700 struct drm_i915_private *dev_priv = dev->dev_private;
3701 int pipe;
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03003702
3703 I915_WRITE(WM3_LP_ILK, 0);
3704 I915_WRITE(WM2_LP_ILK, 0);
3705 I915_WRITE(WM1_LP_ILK, 0);
3706
3707 /* According to the spec, bit 13 (RCZUNIT) must be set on IVB.
3708 * This implements the WaDisableRCZUnitClockGating workaround.
3709 */
3710 I915_WRITE(GEN6_UCGCTL2, GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
3711
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03003712 /* Apply the WaDisableRHWOOptimizationForRenderHang workaround. */
3713 I915_WRITE(GEN7_COMMON_SLICE_CHICKEN1,
3714 GEN7_CSC1_RHWO_OPT_DISABLE_IN_RCC);
3715
3716 /* WaApplyL3ControlAndL3ChickenMode requires those two on Ivy Bridge */
3717 I915_WRITE(GEN7_L3CNTLREG1,
3718 GEN7_WA_FOR_GEN7_L3_CONTROL);
3719 I915_WRITE(GEN7_L3_CHICKEN_MODE_REGISTER,
3720 GEN7_WA_L3_CHICKEN_MODE);
3721
3722 /* This is required by WaCatErrorRejectionIssue */
3723 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
3724 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
3725 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
3726
3727 for_each_pipe(pipe) {
3728 I915_WRITE(DSPCNTR(pipe),
3729 I915_READ(DSPCNTR(pipe)) |
3730 DISPPLANE_TRICKLE_FEED_DISABLE);
3731 intel_flush_display_plane(dev_priv, pipe);
3732 }
3733
3734 gen7_setup_fixed_func_scheduler(dev_priv);
3735
3736 /* WaDisable4x2SubspanOptimization */
3737 I915_WRITE(CACHE_MODE_1,
3738 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Eugeni Dodonov1544d9d2012-07-02 11:51:10 -03003739
Paulo Zanonib3bf0762012-11-20 13:27:44 -02003740 /* WaMbcDriverBootEnable */
3741 I915_WRITE(GEN6_MBCTL, I915_READ(GEN6_MBCTL) |
3742 GEN6_MBCTL_ENABLE_BOOT_FETCH);
3743
Eugeni Dodonov1544d9d2012-07-02 11:51:10 -03003744 /* XXX: This is a workaround for early silicon revisions and should be
3745 * removed later.
3746 */
3747 I915_WRITE(WM_DBG,
3748 I915_READ(WM_DBG) |
3749 WM_DBG_DISALLOW_MULTIPLE_LP |
3750 WM_DBG_DISALLOW_SPRITE |
3751 WM_DBG_DISALLOW_MAXFIFO);
3752
Paulo Zanoni17a303e2012-11-20 15:12:07 -02003753 lpt_init_clock_gating(dev);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03003754}
3755
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03003756static void ivybridge_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03003757{
3758 struct drm_i915_private *dev_priv = dev->dev_private;
3759 int pipe;
Ben Widawsky20848222012-05-04 18:58:59 -07003760 uint32_t snpcr;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03003761
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03003762 I915_WRITE(WM3_LP_ILK, 0);
3763 I915_WRITE(WM2_LP_ILK, 0);
3764 I915_WRITE(WM1_LP_ILK, 0);
3765
Damien Lespiau231e54f2012-10-19 17:55:41 +01003766 I915_WRITE(ILK_DSPCLK_GATE_D, ILK_VRHUNIT_CLOCK_GATE_DISABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03003767
Jesse Barnes87f80202012-10-02 17:43:41 -05003768 /* WaDisableEarlyCull */
3769 I915_WRITE(_3D_CHICKEN3,
3770 _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
3771
Damien Lespiau62cb9442012-10-04 18:49:23 +01003772 /* WaDisableBackToBackFlipFix */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03003773 I915_WRITE(IVB_CHICKEN3,
3774 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
3775 CHICKEN3_DGMG_DONE_FIX_DISABLE);
3776
Jesse Barnes12f33822012-10-25 12:15:45 -07003777 /* WaDisablePSDDualDispatchEnable */
3778 if (IS_IVB_GT1(dev))
3779 I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
3780 _MASKED_BIT_ENABLE(GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
3781 else
3782 I915_WRITE(GEN7_HALF_SLICE_CHICKEN1_GT2,
3783 _MASKED_BIT_ENABLE(GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
3784
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03003785 /* Apply the WaDisableRHWOOptimizationForRenderHang workaround. */
3786 I915_WRITE(GEN7_COMMON_SLICE_CHICKEN1,
3787 GEN7_CSC1_RHWO_OPT_DISABLE_IN_RCC);
3788
3789 /* WaApplyL3ControlAndL3ChickenMode requires those two on Ivy Bridge */
3790 I915_WRITE(GEN7_L3CNTLREG1,
3791 GEN7_WA_FOR_GEN7_L3_CONTROL);
3792 I915_WRITE(GEN7_L3_CHICKEN_MODE_REGISTER,
Jesse Barnes8ab43972012-10-25 12:15:42 -07003793 GEN7_WA_L3_CHICKEN_MODE);
3794 if (IS_IVB_GT1(dev))
3795 I915_WRITE(GEN7_ROW_CHICKEN2,
3796 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
3797 else
3798 I915_WRITE(GEN7_ROW_CHICKEN2_GT2,
3799 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
3800
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03003801
Jesse Barnes61939d92012-10-02 17:43:38 -05003802 /* WaForceL3Serialization */
3803 I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
3804 ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
3805
Jesse Barnes0f846f82012-06-14 11:04:47 -07003806 /* According to the BSpec vol1g, bit 12 (RCPBUNIT) clock
3807 * gating disable must be set. Failure to set it results in
3808 * flickering pixels due to Z write ordering failures after
3809 * some amount of runtime in the Mesa "fire" demo, and Unigine
3810 * Sanctuary and Tropics, and apparently anything else with
3811 * alpha test or pixel discard.
3812 *
3813 * According to the spec, bit 11 (RCCUNIT) must also be set,
3814 * but we didn't debug actual testcases to find it out.
3815 *
3816 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
3817 * This implements the WaDisableRCZUnitClockGating workaround.
3818 */
3819 I915_WRITE(GEN6_UCGCTL2,
3820 GEN6_RCZUNIT_CLOCK_GATE_DISABLE |
3821 GEN6_RCCUNIT_CLOCK_GATE_DISABLE);
3822
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03003823 /* This is required by WaCatErrorRejectionIssue */
3824 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
3825 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
3826 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
3827
3828 for_each_pipe(pipe) {
3829 I915_WRITE(DSPCNTR(pipe),
3830 I915_READ(DSPCNTR(pipe)) |
3831 DISPPLANE_TRICKLE_FEED_DISABLE);
3832 intel_flush_display_plane(dev_priv, pipe);
3833 }
3834
Paulo Zanonib3bf0762012-11-20 13:27:44 -02003835 /* WaMbcDriverBootEnable */
Jesse Barnesb4ae3f22012-06-14 11:04:48 -07003836 I915_WRITE(GEN6_MBCTL, I915_READ(GEN6_MBCTL) |
3837 GEN6_MBCTL_ENABLE_BOOT_FETCH);
3838
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03003839 gen7_setup_fixed_func_scheduler(dev_priv);
Daniel Vetter97e19302012-04-24 16:00:21 +02003840
3841 /* WaDisable4x2SubspanOptimization */
3842 I915_WRITE(CACHE_MODE_1,
3843 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Ben Widawsky20848222012-05-04 18:58:59 -07003844
3845 snpcr = I915_READ(GEN6_MBCUNIT_SNPCR);
3846 snpcr &= ~GEN6_MBC_SNPCR_MASK;
3847 snpcr |= GEN6_MBC_SNPCR_MED;
3848 I915_WRITE(GEN6_MBCUNIT_SNPCR, snpcr);
Daniel Vetter3107bd42012-10-31 22:52:31 +01003849
3850 cpt_init_clock_gating(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03003851}
3852
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03003853static void valleyview_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03003854{
3855 struct drm_i915_private *dev_priv = dev->dev_private;
3856 int pipe;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03003857
3858 I915_WRITE(WM3_LP_ILK, 0);
3859 I915_WRITE(WM2_LP_ILK, 0);
3860 I915_WRITE(WM1_LP_ILK, 0);
3861
Damien Lespiau231e54f2012-10-19 17:55:41 +01003862 I915_WRITE(ILK_DSPCLK_GATE_D, ILK_VRHUNIT_CLOCK_GATE_DISABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03003863
Jesse Barnes87f80202012-10-02 17:43:41 -05003864 /* WaDisableEarlyCull */
3865 I915_WRITE(_3D_CHICKEN3,
3866 _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
3867
Damien Lespiau62cb9442012-10-04 18:49:23 +01003868 /* WaDisableBackToBackFlipFix */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03003869 I915_WRITE(IVB_CHICKEN3,
3870 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
3871 CHICKEN3_DGMG_DONE_FIX_DISABLE);
3872
Jesse Barnes12f33822012-10-25 12:15:45 -07003873 I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
3874 _MASKED_BIT_ENABLE(GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
3875
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03003876 /* Apply the WaDisableRHWOOptimizationForRenderHang workaround. */
3877 I915_WRITE(GEN7_COMMON_SLICE_CHICKEN1,
3878 GEN7_CSC1_RHWO_OPT_DISABLE_IN_RCC);
3879
3880 /* WaApplyL3ControlAndL3ChickenMode requires those two on Ivy Bridge */
Jesse Barnesd0cf5ea2012-10-25 12:15:41 -07003881 I915_WRITE(GEN7_L3CNTLREG1, I915_READ(GEN7_L3CNTLREG1) | GEN7_L3AGDIS);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03003882 I915_WRITE(GEN7_L3_CHICKEN_MODE_REGISTER, GEN7_WA_L3_CHICKEN_MODE);
3883
Jesse Barnes61939d92012-10-02 17:43:38 -05003884 /* WaForceL3Serialization */
3885 I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
3886 ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
3887
Jesse Barnes8ab43972012-10-25 12:15:42 -07003888 /* WaDisableDopClockGating */
3889 I915_WRITE(GEN7_ROW_CHICKEN2,
3890 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
3891
Jesse Barnes5c9664d2012-10-25 12:15:43 -07003892 /* WaForceL3Serialization */
3893 I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
3894 ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
3895
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03003896 /* This is required by WaCatErrorRejectionIssue */
3897 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
3898 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
3899 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
3900
Paulo Zanonib3bf0762012-11-20 13:27:44 -02003901 /* WaMbcDriverBootEnable */
Jesse Barnesb4ae3f22012-06-14 11:04:48 -07003902 I915_WRITE(GEN6_MBCTL, I915_READ(GEN6_MBCTL) |
3903 GEN6_MBCTL_ENABLE_BOOT_FETCH);
3904
Jesse Barnes0f846f82012-06-14 11:04:47 -07003905
3906 /* According to the BSpec vol1g, bit 12 (RCPBUNIT) clock
3907 * gating disable must be set. Failure to set it results in
3908 * flickering pixels due to Z write ordering failures after
3909 * some amount of runtime in the Mesa "fire" demo, and Unigine
3910 * Sanctuary and Tropics, and apparently anything else with
3911 * alpha test or pixel discard.
3912 *
3913 * According to the spec, bit 11 (RCCUNIT) must also be set,
3914 * but we didn't debug actual testcases to find it out.
3915 *
3916 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
3917 * This implements the WaDisableRCZUnitClockGating workaround.
3918 *
3919 * Also apply WaDisableVDSUnitClockGating and
3920 * WaDisableRCPBUnitClockGating.
3921 */
3922 I915_WRITE(GEN6_UCGCTL2,
3923 GEN7_VDSUNIT_CLOCK_GATE_DISABLE |
Jesse Barnes6edaa7f2012-06-14 11:04:49 -07003924 GEN7_TDLUNIT_CLOCK_GATE_DISABLE |
Jesse Barnes0f846f82012-06-14 11:04:47 -07003925 GEN6_RCZUNIT_CLOCK_GATE_DISABLE |
3926 GEN6_RCPBUNIT_CLOCK_GATE_DISABLE |
3927 GEN6_RCCUNIT_CLOCK_GATE_DISABLE);
3928
Jesse Barnese3f33d42012-06-14 11:04:50 -07003929 I915_WRITE(GEN7_UCGCTL4, GEN7_L3BANK2X_CLOCK_GATE_DISABLE);
3930
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03003931 for_each_pipe(pipe) {
3932 I915_WRITE(DSPCNTR(pipe),
3933 I915_READ(DSPCNTR(pipe)) |
3934 DISPPLANE_TRICKLE_FEED_DISABLE);
3935 intel_flush_display_plane(dev_priv, pipe);
3936 }
3937
Daniel Vetter6b26c862012-04-24 14:04:12 +02003938 I915_WRITE(CACHE_MODE_1,
3939 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Jesse Barnes79831172012-06-20 10:53:12 -07003940
3941 /*
3942 * On ValleyView, the GUnit needs to signal the GT
3943 * when flip and other events complete. So enable
3944 * all the GUnit->GT interrupts here
3945 */
3946 I915_WRITE(VLV_DPFLIPSTAT, PIPEB_LINE_COMPARE_INT_EN |
3947 PIPEB_HLINE_INT_EN | PIPEB_VBLANK_INT_EN |
3948 SPRITED_FLIPDONE_INT_EN | SPRITEC_FLIPDONE_INT_EN |
3949 PLANEB_FLIPDONE_INT_EN | PIPEA_LINE_COMPARE_INT_EN |
3950 PIPEA_HLINE_INT_EN | PIPEA_VBLANK_INT_EN |
3951 SPRITEB_FLIPDONE_INT_EN | SPRITEA_FLIPDONE_INT_EN |
3952 PLANEA_FLIPDONE_INT_EN);
Jesse Barnes2d809572012-10-25 12:15:44 -07003953
3954 /*
3955 * WaDisableVLVClockGating_VBIIssue
3956 * Disable clock gating on th GCFG unit to prevent a delay
3957 * in the reporting of vblank events.
3958 */
3959 I915_WRITE(VLV_GUNIT_CLOCK_GATE, GCFG_DIS);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03003960}
3961
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03003962static void g4x_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03003963{
3964 struct drm_i915_private *dev_priv = dev->dev_private;
3965 uint32_t dspclk_gate;
3966
3967 I915_WRITE(RENCLK_GATE_D1, 0);
3968 I915_WRITE(RENCLK_GATE_D2, VF_UNIT_CLOCK_GATE_DISABLE |
3969 GS_UNIT_CLOCK_GATE_DISABLE |
3970 CL_UNIT_CLOCK_GATE_DISABLE);
3971 I915_WRITE(RAMCLK_GATE_D, 0);
3972 dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE |
3973 OVRUNIT_CLOCK_GATE_DISABLE |
3974 OVCUNIT_CLOCK_GATE_DISABLE;
3975 if (IS_GM45(dev))
3976 dspclk_gate |= DSSUNIT_CLOCK_GATE_DISABLE;
3977 I915_WRITE(DSPCLK_GATE_D, dspclk_gate);
Daniel Vetter4358a372012-10-18 11:49:51 +02003978
3979 /* WaDisableRenderCachePipelinedFlush */
3980 I915_WRITE(CACHE_MODE_0,
3981 _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03003982}
3983
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03003984static void crestline_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03003985{
3986 struct drm_i915_private *dev_priv = dev->dev_private;
3987
3988 I915_WRITE(RENCLK_GATE_D1, I965_RCC_CLOCK_GATE_DISABLE);
3989 I915_WRITE(RENCLK_GATE_D2, 0);
3990 I915_WRITE(DSPCLK_GATE_D, 0);
3991 I915_WRITE(RAMCLK_GATE_D, 0);
3992 I915_WRITE16(DEUC, 0);
3993}
3994
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03003995static void broadwater_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03003996{
3997 struct drm_i915_private *dev_priv = dev->dev_private;
3998
3999 I915_WRITE(RENCLK_GATE_D1, I965_RCZ_CLOCK_GATE_DISABLE |
4000 I965_RCC_CLOCK_GATE_DISABLE |
4001 I965_RCPB_CLOCK_GATE_DISABLE |
4002 I965_ISC_CLOCK_GATE_DISABLE |
4003 I965_FBC_CLOCK_GATE_DISABLE);
4004 I915_WRITE(RENCLK_GATE_D2, 0);
4005}
4006
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03004007static void gen3_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004008{
4009 struct drm_i915_private *dev_priv = dev->dev_private;
4010 u32 dstate = I915_READ(D_STATE);
4011
4012 dstate |= DSTATE_PLL_D3_OFF | DSTATE_GFX_CLOCK_GATING |
4013 DSTATE_DOT_CLOCK_GATING;
4014 I915_WRITE(D_STATE, dstate);
Chris Wilson13a86b82012-04-24 14:51:43 +01004015
4016 if (IS_PINEVIEW(dev))
4017 I915_WRITE(ECOSKPD, _MASKED_BIT_ENABLE(ECO_GATING_CX_ONLY));
Daniel Vetter974a3b02012-09-09 11:54:16 +02004018
4019 /* IIR "flip pending" means done if this bit is set */
4020 I915_WRITE(ECOSKPD, _MASKED_BIT_DISABLE(ECO_FLIP_DONE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004021}
4022
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03004023static void i85x_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004024{
4025 struct drm_i915_private *dev_priv = dev->dev_private;
4026
4027 I915_WRITE(RENCLK_GATE_D1, SV_CLOCK_GATE_DISABLE);
4028}
4029
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03004030static void i830_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004031{
4032 struct drm_i915_private *dev_priv = dev->dev_private;
4033
4034 I915_WRITE(DSPCLK_GATE_D, OVRUNIT_CLOCK_GATE_DISABLE);
4035}
4036
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004037void intel_init_clock_gating(struct drm_device *dev)
4038{
4039 struct drm_i915_private *dev_priv = dev->dev_private;
4040
4041 dev_priv->display.init_clock_gating(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03004042}
4043
Eugeni Dodonovd0d3e512012-05-09 15:37:16 -03004044/* Starting with Haswell, we have different power wells for
4045 * different parts of the GPU. This attempts to enable them all.
4046 */
4047void intel_init_power_wells(struct drm_device *dev)
4048{
4049 struct drm_i915_private *dev_priv = dev->dev_private;
4050 unsigned long power_wells[] = {
4051 HSW_PWR_WELL_CTL1,
4052 HSW_PWR_WELL_CTL2,
4053 HSW_PWR_WELL_CTL4
4054 };
4055 int i;
4056
4057 if (!IS_HASWELL(dev))
4058 return;
4059
4060 mutex_lock(&dev->struct_mutex);
4061
4062 for (i = 0; i < ARRAY_SIZE(power_wells); i++) {
4063 int well = I915_READ(power_wells[i]);
4064
4065 if ((well & HSW_PWR_WELL_STATE) == 0) {
4066 I915_WRITE(power_wells[i], well & HSW_PWR_WELL_ENABLE);
Zhenyu Wang263b30d2012-10-30 19:16:34 +08004067 if (wait_for((I915_READ(power_wells[i]) & HSW_PWR_WELL_STATE), 20))
Eugeni Dodonovd0d3e512012-05-09 15:37:16 -03004068 DRM_ERROR("Error enabling power well %lx\n", power_wells[i]);
4069 }
4070 }
4071
4072 mutex_unlock(&dev->struct_mutex);
4073}
4074
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03004075/* Set up chip specific power management-related functions */
4076void intel_init_pm(struct drm_device *dev)
4077{
4078 struct drm_i915_private *dev_priv = dev->dev_private;
4079
4080 if (I915_HAS_FBC(dev)) {
4081 if (HAS_PCH_SPLIT(dev)) {
4082 dev_priv->display.fbc_enabled = ironlake_fbc_enabled;
4083 dev_priv->display.enable_fbc = ironlake_enable_fbc;
4084 dev_priv->display.disable_fbc = ironlake_disable_fbc;
4085 } else if (IS_GM45(dev)) {
4086 dev_priv->display.fbc_enabled = g4x_fbc_enabled;
4087 dev_priv->display.enable_fbc = g4x_enable_fbc;
4088 dev_priv->display.disable_fbc = g4x_disable_fbc;
4089 } else if (IS_CRESTLINE(dev)) {
4090 dev_priv->display.fbc_enabled = i8xx_fbc_enabled;
4091 dev_priv->display.enable_fbc = i8xx_enable_fbc;
4092 dev_priv->display.disable_fbc = i8xx_disable_fbc;
4093 }
4094 /* 855GM needs testing */
4095 }
4096
Daniel Vetterc921aba2012-04-26 23:28:17 +02004097 /* For cxsr */
4098 if (IS_PINEVIEW(dev))
4099 i915_pineview_get_mem_freq(dev);
4100 else if (IS_GEN5(dev))
4101 i915_ironlake_get_mem_freq(dev);
4102
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03004103 /* For FIFO watermark updates */
4104 if (HAS_PCH_SPLIT(dev)) {
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03004105 if (IS_GEN5(dev)) {
4106 if (I915_READ(MLTR_ILK) & ILK_SRLT_MASK)
4107 dev_priv->display.update_wm = ironlake_update_wm;
4108 else {
4109 DRM_DEBUG_KMS("Failed to get proper latency. "
4110 "Disable CxSR\n");
4111 dev_priv->display.update_wm = NULL;
4112 }
4113 dev_priv->display.init_clock_gating = ironlake_init_clock_gating;
4114 } else if (IS_GEN6(dev)) {
4115 if (SNB_READ_WM0_LATENCY()) {
4116 dev_priv->display.update_wm = sandybridge_update_wm;
4117 dev_priv->display.update_sprite_wm = sandybridge_update_sprite_wm;
4118 } else {
4119 DRM_DEBUG_KMS("Failed to read display plane latency. "
4120 "Disable CxSR\n");
4121 dev_priv->display.update_wm = NULL;
4122 }
4123 dev_priv->display.init_clock_gating = gen6_init_clock_gating;
4124 } else if (IS_IVYBRIDGE(dev)) {
4125 /* FIXME: detect B0+ stepping and use auto training */
4126 if (SNB_READ_WM0_LATENCY()) {
Chris Wilsonc43d0182012-12-11 12:01:42 +00004127 dev_priv->display.update_wm = ivybridge_update_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03004128 dev_priv->display.update_sprite_wm = sandybridge_update_sprite_wm;
4129 } else {
4130 DRM_DEBUG_KMS("Failed to read display plane latency. "
4131 "Disable CxSR\n");
4132 dev_priv->display.update_wm = NULL;
4133 }
4134 dev_priv->display.init_clock_gating = ivybridge_init_clock_gating;
Eugeni Dodonov6b8a5ee2012-05-09 15:37:23 -03004135 } else if (IS_HASWELL(dev)) {
4136 if (SNB_READ_WM0_LATENCY()) {
4137 dev_priv->display.update_wm = sandybridge_update_wm;
4138 dev_priv->display.update_sprite_wm = sandybridge_update_sprite_wm;
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03004139 dev_priv->display.update_linetime_wm = haswell_update_linetime_wm;
Eugeni Dodonov6b8a5ee2012-05-09 15:37:23 -03004140 } else {
4141 DRM_DEBUG_KMS("Failed to read display plane latency. "
4142 "Disable CxSR\n");
4143 dev_priv->display.update_wm = NULL;
4144 }
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03004145 dev_priv->display.init_clock_gating = haswell_init_clock_gating;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03004146 } else
4147 dev_priv->display.update_wm = NULL;
4148 } else if (IS_VALLEYVIEW(dev)) {
4149 dev_priv->display.update_wm = valleyview_update_wm;
4150 dev_priv->display.init_clock_gating =
4151 valleyview_init_clock_gating;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03004152 } else if (IS_PINEVIEW(dev)) {
4153 if (!intel_get_cxsr_latency(IS_PINEVIEW_G(dev),
4154 dev_priv->is_ddr3,
4155 dev_priv->fsb_freq,
4156 dev_priv->mem_freq)) {
4157 DRM_INFO("failed to find known CxSR latency "
4158 "(found ddr%s fsb freq %d, mem freq %d), "
4159 "disabling CxSR\n",
4160 (dev_priv->is_ddr3 == 1) ? "3" : "2",
4161 dev_priv->fsb_freq, dev_priv->mem_freq);
4162 /* Disable CxSR and never update its watermark again */
4163 pineview_disable_cxsr(dev);
4164 dev_priv->display.update_wm = NULL;
4165 } else
4166 dev_priv->display.update_wm = pineview_update_wm;
4167 dev_priv->display.init_clock_gating = gen3_init_clock_gating;
4168 } else if (IS_G4X(dev)) {
4169 dev_priv->display.update_wm = g4x_update_wm;
4170 dev_priv->display.init_clock_gating = g4x_init_clock_gating;
4171 } else if (IS_GEN4(dev)) {
4172 dev_priv->display.update_wm = i965_update_wm;
4173 if (IS_CRESTLINE(dev))
4174 dev_priv->display.init_clock_gating = crestline_init_clock_gating;
4175 else if (IS_BROADWATER(dev))
4176 dev_priv->display.init_clock_gating = broadwater_init_clock_gating;
4177 } else if (IS_GEN3(dev)) {
4178 dev_priv->display.update_wm = i9xx_update_wm;
4179 dev_priv->display.get_fifo_size = i9xx_get_fifo_size;
4180 dev_priv->display.init_clock_gating = gen3_init_clock_gating;
4181 } else if (IS_I865G(dev)) {
4182 dev_priv->display.update_wm = i830_update_wm;
4183 dev_priv->display.init_clock_gating = i85x_init_clock_gating;
4184 dev_priv->display.get_fifo_size = i830_get_fifo_size;
4185 } else if (IS_I85X(dev)) {
4186 dev_priv->display.update_wm = i9xx_update_wm;
4187 dev_priv->display.get_fifo_size = i85x_get_fifo_size;
4188 dev_priv->display.init_clock_gating = i85x_init_clock_gating;
4189 } else {
4190 dev_priv->display.update_wm = i830_update_wm;
4191 dev_priv->display.init_clock_gating = i830_init_clock_gating;
4192 if (IS_845G(dev))
4193 dev_priv->display.get_fifo_size = i845_get_fifo_size;
4194 else
4195 dev_priv->display.get_fifo_size = i830_get_fifo_size;
4196 }
4197}
4198
Eugeni Dodonov65901902012-07-02 11:51:11 -03004199static void __gen6_gt_wait_for_thread_c0(struct drm_i915_private *dev_priv)
4200{
4201 u32 gt_thread_status_mask;
4202
4203 if (IS_HASWELL(dev_priv->dev))
4204 gt_thread_status_mask = GEN6_GT_THREAD_STATUS_CORE_MASK_HSW;
4205 else
4206 gt_thread_status_mask = GEN6_GT_THREAD_STATUS_CORE_MASK;
4207
4208 /* w/a for a sporadic read returning 0 by waiting for the GT
4209 * thread to wake up.
4210 */
4211 if (wait_for_atomic_us((I915_READ_NOTRACE(GEN6_GT_THREAD_STATUS_REG) & gt_thread_status_mask) == 0, 500))
4212 DRM_ERROR("GT thread status wait timed out\n");
4213}
4214
Chris Wilson16995a92012-10-18 11:46:10 +01004215static void __gen6_gt_force_wake_reset(struct drm_i915_private *dev_priv)
4216{
4217 I915_WRITE_NOTRACE(FORCEWAKE, 0);
4218 POSTING_READ(ECOBUS); /* something from same cacheline, but !FORCEWAKE */
4219}
4220
Eugeni Dodonov65901902012-07-02 11:51:11 -03004221static void __gen6_gt_force_wake_get(struct drm_i915_private *dev_priv)
4222{
4223 u32 forcewake_ack;
4224
4225 if (IS_HASWELL(dev_priv->dev))
4226 forcewake_ack = FORCEWAKE_ACK_HSW;
4227 else
4228 forcewake_ack = FORCEWAKE_ACK;
4229
Ben Widawsky057d3862012-09-01 22:59:49 -07004230 if (wait_for_atomic((I915_READ_NOTRACE(forcewake_ack) & 1) == 0,
4231 FORCEWAKE_ACK_TIMEOUT_MS))
Daniel Vetter8a038fd2012-08-24 17:26:21 +02004232 DRM_ERROR("Timed out waiting for forcewake old ack to clear.\n");
Eugeni Dodonov65901902012-07-02 11:51:11 -03004233
Chris Wilsonc5836c22012-10-17 12:09:55 +01004234 I915_WRITE_NOTRACE(FORCEWAKE, FORCEWAKE_KERNEL);
Ben Widawsky8dee3ee2012-09-01 22:59:50 -07004235 POSTING_READ(ECOBUS); /* something from same cacheline, but !FORCEWAKE */
Eugeni Dodonov65901902012-07-02 11:51:11 -03004236
Ben Widawsky057d3862012-09-01 22:59:49 -07004237 if (wait_for_atomic((I915_READ_NOTRACE(forcewake_ack) & 1),
4238 FORCEWAKE_ACK_TIMEOUT_MS))
Daniel Vetter8a038fd2012-08-24 17:26:21 +02004239 DRM_ERROR("Timed out waiting for forcewake to ack request.\n");
Eugeni Dodonov65901902012-07-02 11:51:11 -03004240
4241 __gen6_gt_wait_for_thread_c0(dev_priv);
4242}
4243
Chris Wilson16995a92012-10-18 11:46:10 +01004244static void __gen6_gt_force_wake_mt_reset(struct drm_i915_private *dev_priv)
4245{
4246 I915_WRITE_NOTRACE(FORCEWAKE_MT, _MASKED_BIT_DISABLE(0xffff));
4247 POSTING_READ(ECOBUS); /* something from same cacheline, but !FORCEWAKE */
4248}
4249
Eugeni Dodonov65901902012-07-02 11:51:11 -03004250static void __gen6_gt_force_wake_mt_get(struct drm_i915_private *dev_priv)
4251{
4252 u32 forcewake_ack;
4253
4254 if (IS_HASWELL(dev_priv->dev))
4255 forcewake_ack = FORCEWAKE_ACK_HSW;
4256 else
4257 forcewake_ack = FORCEWAKE_MT_ACK;
4258
Ben Widawsky057d3862012-09-01 22:59:49 -07004259 if (wait_for_atomic((I915_READ_NOTRACE(forcewake_ack) & 1) == 0,
4260 FORCEWAKE_ACK_TIMEOUT_MS))
Daniel Vetter8a038fd2012-08-24 17:26:21 +02004261 DRM_ERROR("Timed out waiting for forcewake old ack to clear.\n");
Eugeni Dodonov65901902012-07-02 11:51:11 -03004262
Chris Wilsonc5836c22012-10-17 12:09:55 +01004263 I915_WRITE_NOTRACE(FORCEWAKE_MT, _MASKED_BIT_ENABLE(FORCEWAKE_KERNEL));
Ben Widawsky8dee3ee2012-09-01 22:59:50 -07004264 POSTING_READ(ECOBUS); /* something from same cacheline, but !FORCEWAKE */
Eugeni Dodonov65901902012-07-02 11:51:11 -03004265
Ben Widawsky057d3862012-09-01 22:59:49 -07004266 if (wait_for_atomic((I915_READ_NOTRACE(forcewake_ack) & 1),
4267 FORCEWAKE_ACK_TIMEOUT_MS))
Daniel Vetter8a038fd2012-08-24 17:26:21 +02004268 DRM_ERROR("Timed out waiting for forcewake to ack request.\n");
Eugeni Dodonov65901902012-07-02 11:51:11 -03004269
4270 __gen6_gt_wait_for_thread_c0(dev_priv);
4271}
4272
4273/*
4274 * Generally this is called implicitly by the register read function. However,
4275 * if some sequence requires the GT to not power down then this function should
4276 * be called at the beginning of the sequence followed by a call to
4277 * gen6_gt_force_wake_put() at the end of the sequence.
4278 */
4279void gen6_gt_force_wake_get(struct drm_i915_private *dev_priv)
4280{
4281 unsigned long irqflags;
4282
4283 spin_lock_irqsave(&dev_priv->gt_lock, irqflags);
4284 if (dev_priv->forcewake_count++ == 0)
4285 dev_priv->gt.force_wake_get(dev_priv);
4286 spin_unlock_irqrestore(&dev_priv->gt_lock, irqflags);
4287}
4288
4289void gen6_gt_check_fifodbg(struct drm_i915_private *dev_priv)
4290{
4291 u32 gtfifodbg;
4292 gtfifodbg = I915_READ_NOTRACE(GTFIFODBG);
4293 if (WARN(gtfifodbg & GT_FIFO_CPU_ERROR_MASK,
4294 "MMIO read or write has been dropped %x\n", gtfifodbg))
4295 I915_WRITE_NOTRACE(GTFIFODBG, GT_FIFO_CPU_ERROR_MASK);
4296}
4297
4298static void __gen6_gt_force_wake_put(struct drm_i915_private *dev_priv)
4299{
4300 I915_WRITE_NOTRACE(FORCEWAKE, 0);
Ben Widawsky8dee3ee2012-09-01 22:59:50 -07004301 /* gen6_gt_check_fifodbg doubles as the POSTING_READ */
Eugeni Dodonov65901902012-07-02 11:51:11 -03004302 gen6_gt_check_fifodbg(dev_priv);
4303}
4304
4305static void __gen6_gt_force_wake_mt_put(struct drm_i915_private *dev_priv)
4306{
Chris Wilsonc5836c22012-10-17 12:09:55 +01004307 I915_WRITE_NOTRACE(FORCEWAKE_MT, _MASKED_BIT_DISABLE(FORCEWAKE_KERNEL));
Ben Widawsky8dee3ee2012-09-01 22:59:50 -07004308 /* gen6_gt_check_fifodbg doubles as the POSTING_READ */
Eugeni Dodonov65901902012-07-02 11:51:11 -03004309 gen6_gt_check_fifodbg(dev_priv);
4310}
4311
4312/*
4313 * see gen6_gt_force_wake_get()
4314 */
4315void gen6_gt_force_wake_put(struct drm_i915_private *dev_priv)
4316{
4317 unsigned long irqflags;
4318
4319 spin_lock_irqsave(&dev_priv->gt_lock, irqflags);
4320 if (--dev_priv->forcewake_count == 0)
4321 dev_priv->gt.force_wake_put(dev_priv);
4322 spin_unlock_irqrestore(&dev_priv->gt_lock, irqflags);
4323}
4324
4325int __gen6_gt_wait_for_fifo(struct drm_i915_private *dev_priv)
4326{
4327 int ret = 0;
4328
4329 if (dev_priv->gt_fifo_count < GT_FIFO_NUM_RESERVED_ENTRIES) {
4330 int loop = 500;
4331 u32 fifo = I915_READ_NOTRACE(GT_FIFO_FREE_ENTRIES);
4332 while (fifo <= GT_FIFO_NUM_RESERVED_ENTRIES && loop--) {
4333 udelay(10);
4334 fifo = I915_READ_NOTRACE(GT_FIFO_FREE_ENTRIES);
4335 }
4336 if (WARN_ON(loop < 0 && fifo <= GT_FIFO_NUM_RESERVED_ENTRIES))
4337 ++ret;
4338 dev_priv->gt_fifo_count = fifo;
4339 }
4340 dev_priv->gt_fifo_count--;
4341
4342 return ret;
4343}
4344
Chris Wilson16995a92012-10-18 11:46:10 +01004345static void vlv_force_wake_reset(struct drm_i915_private *dev_priv)
4346{
4347 I915_WRITE_NOTRACE(FORCEWAKE_VLV, _MASKED_BIT_DISABLE(0xffff));
4348}
4349
Eugeni Dodonov65901902012-07-02 11:51:11 -03004350static void vlv_force_wake_get(struct drm_i915_private *dev_priv)
4351{
Ben Widawsky057d3862012-09-01 22:59:49 -07004352 if (wait_for_atomic((I915_READ_NOTRACE(FORCEWAKE_ACK_VLV) & 1) == 0,
4353 FORCEWAKE_ACK_TIMEOUT_MS))
Daniel Vetter8a038fd2012-08-24 17:26:21 +02004354 DRM_ERROR("Timed out waiting for forcewake old ack to clear.\n");
Eugeni Dodonov65901902012-07-02 11:51:11 -03004355
Chris Wilsonc5836c22012-10-17 12:09:55 +01004356 I915_WRITE_NOTRACE(FORCEWAKE_VLV, _MASKED_BIT_ENABLE(FORCEWAKE_KERNEL));
Eugeni Dodonov65901902012-07-02 11:51:11 -03004357
Ben Widawsky057d3862012-09-01 22:59:49 -07004358 if (wait_for_atomic((I915_READ_NOTRACE(FORCEWAKE_ACK_VLV) & 1),
4359 FORCEWAKE_ACK_TIMEOUT_MS))
Daniel Vetter8a038fd2012-08-24 17:26:21 +02004360 DRM_ERROR("Timed out waiting for forcewake to ack request.\n");
Eugeni Dodonov65901902012-07-02 11:51:11 -03004361
4362 __gen6_gt_wait_for_thread_c0(dev_priv);
4363}
4364
4365static void vlv_force_wake_put(struct drm_i915_private *dev_priv)
4366{
Chris Wilsonc5836c22012-10-17 12:09:55 +01004367 I915_WRITE_NOTRACE(FORCEWAKE_VLV, _MASKED_BIT_DISABLE(FORCEWAKE_KERNEL));
Daniel Vetter5ab140a2012-08-24 17:26:20 +02004368 /* The below doubles as a POSTING_READ */
4369 gen6_gt_check_fifodbg(dev_priv);
Eugeni Dodonov65901902012-07-02 11:51:11 -03004370}
4371
Chris Wilson16995a92012-10-18 11:46:10 +01004372void intel_gt_reset(struct drm_device *dev)
4373{
4374 struct drm_i915_private *dev_priv = dev->dev_private;
4375
4376 if (IS_VALLEYVIEW(dev)) {
4377 vlv_force_wake_reset(dev_priv);
4378 } else if (INTEL_INFO(dev)->gen >= 6) {
4379 __gen6_gt_force_wake_reset(dev_priv);
4380 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
4381 __gen6_gt_force_wake_mt_reset(dev_priv);
4382 }
4383}
4384
Eugeni Dodonov65901902012-07-02 11:51:11 -03004385void intel_gt_init(struct drm_device *dev)
4386{
4387 struct drm_i915_private *dev_priv = dev->dev_private;
4388
4389 spin_lock_init(&dev_priv->gt_lock);
4390
Chris Wilson16995a92012-10-18 11:46:10 +01004391 intel_gt_reset(dev);
4392
Eugeni Dodonov65901902012-07-02 11:51:11 -03004393 if (IS_VALLEYVIEW(dev)) {
4394 dev_priv->gt.force_wake_get = vlv_force_wake_get;
4395 dev_priv->gt.force_wake_put = vlv_force_wake_put;
Daniel Vetter36ec8f82012-10-18 14:44:35 +02004396 } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
4397 dev_priv->gt.force_wake_get = __gen6_gt_force_wake_mt_get;
4398 dev_priv->gt.force_wake_put = __gen6_gt_force_wake_mt_put;
4399 } else if (IS_GEN6(dev)) {
Eugeni Dodonov65901902012-07-02 11:51:11 -03004400 dev_priv->gt.force_wake_get = __gen6_gt_force_wake_get;
4401 dev_priv->gt.force_wake_put = __gen6_gt_force_wake_put;
Eugeni Dodonov65901902012-07-02 11:51:11 -03004402 }
Jesse Barnes1a01ab32012-11-02 11:14:00 -07004403 INIT_DELAYED_WORK(&dev_priv->rps.delayed_resume_work,
4404 intel_gen6_powersave_work);
Eugeni Dodonov65901902012-07-02 11:51:11 -03004405}
4406
Ben Widawsky42c05262012-09-26 10:34:00 -07004407int sandybridge_pcode_read(struct drm_i915_private *dev_priv, u8 mbox, u32 *val)
4408{
Jesse Barnes4fc688c2012-11-02 11:14:01 -07004409 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Ben Widawsky42c05262012-09-26 10:34:00 -07004410
4411 if (I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) {
4412 DRM_DEBUG_DRIVER("warning: pcode (read) mailbox access failed\n");
4413 return -EAGAIN;
4414 }
4415
4416 I915_WRITE(GEN6_PCODE_DATA, *val);
4417 I915_WRITE(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY | mbox);
4418
4419 if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
4420 500)) {
4421 DRM_ERROR("timeout waiting for pcode read (%d) to finish\n", mbox);
4422 return -ETIMEDOUT;
4423 }
4424
4425 *val = I915_READ(GEN6_PCODE_DATA);
4426 I915_WRITE(GEN6_PCODE_DATA, 0);
4427
4428 return 0;
4429}
4430
4431int sandybridge_pcode_write(struct drm_i915_private *dev_priv, u8 mbox, u32 val)
4432{
Jesse Barnes4fc688c2012-11-02 11:14:01 -07004433 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Ben Widawsky42c05262012-09-26 10:34:00 -07004434
4435 if (I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) {
4436 DRM_DEBUG_DRIVER("warning: pcode (write) mailbox access failed\n");
4437 return -EAGAIN;
4438 }
4439
4440 I915_WRITE(GEN6_PCODE_DATA, val);
4441 I915_WRITE(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY | mbox);
4442
4443 if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
4444 500)) {
4445 DRM_ERROR("timeout waiting for pcode write (%d) to finish\n", mbox);
4446 return -ETIMEDOUT;
4447 }
4448
4449 I915_WRITE(GEN6_PCODE_DATA, 0);
4450
4451 return 0;
4452}