]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/gpu/drm/i915/intel_pm.c
drm/i915: make fbdev initialization asynchronous v2
[mirror_ubuntu-bionic-kernel.git] / drivers / gpu / drm / i915 / intel_pm.c
CommitLineData
85208be0
ED
1/*
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
2b4e57bd 28#include <linux/cpufreq.h>
85208be0
ED
29#include "i915_drv.h"
30#include "intel_drv.h"
eb48eb00
DV
31#include "../../../platform/x86/intel_ips.h"
32#include <linux/module.h>
f9dcb0df 33#include <linux/vgaarb.h>
f4db9321 34#include <drm/i915_powerwell.h>
8a187455 35#include <linux/pm_runtime.h>
85208be0 36
dc39fff7
BW
37/**
38 * RC6 is a special power stage which allows the GPU to enter an very
39 * low-voltage mode when idle, using down to 0V while at this stage. This
40 * stage is entered automatically when the GPU is idle when RC6 support is
41 * enabled, and as soon as new workload arises GPU wakes up automatically as well.
42 *
43 * There are different RC6 modes available in Intel GPU, which differentiate
44 * among each other with the latency required to enter and leave RC6 and
45 * voltage consumed by the GPU in different states.
46 *
47 * The combination of the following flags define which states GPU is allowed
48 * to enter, while RC6 is the normal RC6 state, RC6p is the deep RC6, and
49 * RC6pp is deepest RC6. Their support by hardware varies according to the
50 * GPU, BIOS, chipset and platform. RC6 is usually the safest one and the one
51 * which brings the most power savings; deeper states save more power, but
52 * require higher latency to switch to and wake up.
53 */
54#define INTEL_RC6_ENABLE (1<<0)
55#define INTEL_RC6p_ENABLE (1<<1)
56#define INTEL_RC6pp_ENABLE (1<<2)
57
f6750b3c
ED
58/* FBC, or Frame Buffer Compression, is a technique employed to compress the
59 * framebuffer contents in-memory, aiming at reducing the required bandwidth
60 * during in-memory transfers and, therefore, reduce the power packet.
85208be0 61 *
f6750b3c
ED
62 * The benefits of FBC are mostly visible with solid backgrounds and
63 * variation-less patterns.
85208be0 64 *
f6750b3c
ED
65 * FBC-related functionality can be enabled by the means of the
66 * i915.i915_enable_fbc parameter
85208be0
ED
67 */
68
1fa61106 69static void i8xx_disable_fbc(struct drm_device *dev)
85208be0
ED
70{
71 struct drm_i915_private *dev_priv = dev->dev_private;
72 u32 fbc_ctl;
73
74 /* Disable compression */
75 fbc_ctl = I915_READ(FBC_CONTROL);
76 if ((fbc_ctl & FBC_CTL_EN) == 0)
77 return;
78
79 fbc_ctl &= ~FBC_CTL_EN;
80 I915_WRITE(FBC_CONTROL, fbc_ctl);
81
82 /* Wait for compressing bit to clear */
83 if (wait_for((I915_READ(FBC_STATUS) & FBC_STAT_COMPRESSING) == 0, 10)) {
84 DRM_DEBUG_KMS("FBC idle timed out\n");
85 return;
86 }
87
88 DRM_DEBUG_KMS("disabled FBC\n");
89}
90
993495ae 91static void i8xx_enable_fbc(struct drm_crtc *crtc)
85208be0
ED
92{
93 struct drm_device *dev = crtc->dev;
94 struct drm_i915_private *dev_priv = dev->dev_private;
f4510a27 95 struct drm_framebuffer *fb = crtc->primary->fb;
2ff8fde1 96 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
85208be0
ED
97 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
98 int cfb_pitch;
7f2cf220 99 int i;
159f9875 100 u32 fbc_ctl;
85208be0 101
5c3fe8b0 102 cfb_pitch = dev_priv->fbc.size / FBC_LL_SIZE;
85208be0
ED
103 if (fb->pitches[0] < cfb_pitch)
104 cfb_pitch = fb->pitches[0];
105
42a430f5
VS
106 /* FBC_CTL wants 32B or 64B units */
107 if (IS_GEN2(dev))
108 cfb_pitch = (cfb_pitch / 32) - 1;
109 else
110 cfb_pitch = (cfb_pitch / 64) - 1;
85208be0
ED
111
112 /* Clear old tags */
113 for (i = 0; i < (FBC_LL_SIZE / 32) + 1; i++)
114 I915_WRITE(FBC_TAG + (i * 4), 0);
115
159f9875
VS
116 if (IS_GEN4(dev)) {
117 u32 fbc_ctl2;
118
119 /* Set it up... */
120 fbc_ctl2 = FBC_CTL_FENCE_DBL | FBC_CTL_IDLE_IMM | FBC_CTL_CPU_FENCE;
7f2cf220 121 fbc_ctl2 |= FBC_CTL_PLANE(intel_crtc->plane);
159f9875
VS
122 I915_WRITE(FBC_CONTROL2, fbc_ctl2);
123 I915_WRITE(FBC_FENCE_OFF, crtc->y);
124 }
85208be0
ED
125
126 /* enable it... */
993495ae
VS
127 fbc_ctl = I915_READ(FBC_CONTROL);
128 fbc_ctl &= 0x3fff << FBC_CTL_INTERVAL_SHIFT;
129 fbc_ctl |= FBC_CTL_EN | FBC_CTL_PERIODIC;
85208be0
ED
130 if (IS_I945GM(dev))
131 fbc_ctl |= FBC_CTL_C3_IDLE; /* 945 needs special SR handling */
132 fbc_ctl |= (cfb_pitch & 0xff) << FBC_CTL_STRIDE_SHIFT;
85208be0
ED
133 fbc_ctl |= obj->fence_reg;
134 I915_WRITE(FBC_CONTROL, fbc_ctl);
135
5cd5410e 136 DRM_DEBUG_KMS("enabled FBC, pitch %d, yoff %d, plane %c\n",
84f44ce7 137 cfb_pitch, crtc->y, plane_name(intel_crtc->plane));
85208be0
ED
138}
139
1fa61106 140static bool i8xx_fbc_enabled(struct drm_device *dev)
85208be0
ED
141{
142 struct drm_i915_private *dev_priv = dev->dev_private;
143
144 return I915_READ(FBC_CONTROL) & FBC_CTL_EN;
145}
146
993495ae 147static void g4x_enable_fbc(struct drm_crtc *crtc)
85208be0
ED
148{
149 struct drm_device *dev = crtc->dev;
150 struct drm_i915_private *dev_priv = dev->dev_private;
f4510a27 151 struct drm_framebuffer *fb = crtc->primary->fb;
2ff8fde1 152 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
85208be0 153 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
85208be0
ED
154 u32 dpfc_ctl;
155
3fa2e0ee
VS
156 dpfc_ctl = DPFC_CTL_PLANE(intel_crtc->plane) | DPFC_SR_EN;
157 if (drm_format_plane_cpp(fb->pixel_format, 0) == 2)
158 dpfc_ctl |= DPFC_CTL_LIMIT_2X;
159 else
160 dpfc_ctl |= DPFC_CTL_LIMIT_1X;
85208be0 161 dpfc_ctl |= DPFC_CTL_FENCE_EN | obj->fence_reg;
85208be0 162
85208be0
ED
163 I915_WRITE(DPFC_FENCE_YOFF, crtc->y);
164
165 /* enable it... */
fe74c1a5 166 I915_WRITE(DPFC_CONTROL, dpfc_ctl | DPFC_CTL_EN);
85208be0 167
84f44ce7 168 DRM_DEBUG_KMS("enabled fbc on plane %c\n", plane_name(intel_crtc->plane));
85208be0
ED
169}
170
1fa61106 171static void g4x_disable_fbc(struct drm_device *dev)
85208be0
ED
172{
173 struct drm_i915_private *dev_priv = dev->dev_private;
174 u32 dpfc_ctl;
175
176 /* Disable compression */
177 dpfc_ctl = I915_READ(DPFC_CONTROL);
178 if (dpfc_ctl & DPFC_CTL_EN) {
179 dpfc_ctl &= ~DPFC_CTL_EN;
180 I915_WRITE(DPFC_CONTROL, dpfc_ctl);
181
182 DRM_DEBUG_KMS("disabled FBC\n");
183 }
184}
185
1fa61106 186static bool g4x_fbc_enabled(struct drm_device *dev)
85208be0
ED
187{
188 struct drm_i915_private *dev_priv = dev->dev_private;
189
190 return I915_READ(DPFC_CONTROL) & DPFC_CTL_EN;
191}
192
193static void sandybridge_blit_fbc_update(struct drm_device *dev)
194{
195 struct drm_i915_private *dev_priv = dev->dev_private;
196 u32 blt_ecoskpd;
197
198 /* Make sure blitter notifies FBC of writes */
940aece4
D
199
200 /* Blitter is part of Media powerwell on VLV. No impact of
201 * his param in other platforms for now */
202 gen6_gt_force_wake_get(dev_priv, FORCEWAKE_MEDIA);
c8d9a590 203
85208be0
ED
204 blt_ecoskpd = I915_READ(GEN6_BLITTER_ECOSKPD);
205 blt_ecoskpd |= GEN6_BLITTER_FBC_NOTIFY <<
206 GEN6_BLITTER_LOCK_SHIFT;
207 I915_WRITE(GEN6_BLITTER_ECOSKPD, blt_ecoskpd);
208 blt_ecoskpd |= GEN6_BLITTER_FBC_NOTIFY;
209 I915_WRITE(GEN6_BLITTER_ECOSKPD, blt_ecoskpd);
210 blt_ecoskpd &= ~(GEN6_BLITTER_FBC_NOTIFY <<
211 GEN6_BLITTER_LOCK_SHIFT);
212 I915_WRITE(GEN6_BLITTER_ECOSKPD, blt_ecoskpd);
213 POSTING_READ(GEN6_BLITTER_ECOSKPD);
c8d9a590 214
940aece4 215 gen6_gt_force_wake_put(dev_priv, FORCEWAKE_MEDIA);
85208be0
ED
216}
217
993495ae 218static void ironlake_enable_fbc(struct drm_crtc *crtc)
85208be0
ED
219{
220 struct drm_device *dev = crtc->dev;
221 struct drm_i915_private *dev_priv = dev->dev_private;
f4510a27 222 struct drm_framebuffer *fb = crtc->primary->fb;
2ff8fde1 223 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
85208be0 224 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
85208be0
ED
225 u32 dpfc_ctl;
226
46f3dab9 227 dpfc_ctl = DPFC_CTL_PLANE(intel_crtc->plane);
3fa2e0ee 228 if (drm_format_plane_cpp(fb->pixel_format, 0) == 2)
5e59f717
BW
229 dev_priv->fbc.threshold++;
230
231 switch (dev_priv->fbc.threshold) {
232 case 4:
233 case 3:
234 dpfc_ctl |= DPFC_CTL_LIMIT_4X;
235 break;
236 case 2:
3fa2e0ee 237 dpfc_ctl |= DPFC_CTL_LIMIT_2X;
5e59f717
BW
238 break;
239 case 1:
3fa2e0ee 240 dpfc_ctl |= DPFC_CTL_LIMIT_1X;
5e59f717
BW
241 break;
242 }
d629336b
VS
243 dpfc_ctl |= DPFC_CTL_FENCE_EN;
244 if (IS_GEN5(dev))
245 dpfc_ctl |= obj->fence_reg;
85208be0 246
85208be0 247 I915_WRITE(ILK_DPFC_FENCE_YOFF, crtc->y);
f343c5f6 248 I915_WRITE(ILK_FBC_RT_BASE, i915_gem_obj_ggtt_offset(obj) | ILK_FBC_RT_VALID);
85208be0
ED
249 /* enable it... */
250 I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl | DPFC_CTL_EN);
251
252 if (IS_GEN6(dev)) {
253 I915_WRITE(SNB_DPFC_CTL_SA,
254 SNB_CPU_FENCE_ENABLE | obj->fence_reg);
255 I915_WRITE(DPFC_CPU_FENCE_OFFSET, crtc->y);
256 sandybridge_blit_fbc_update(dev);
257 }
258
84f44ce7 259 DRM_DEBUG_KMS("enabled fbc on plane %c\n", plane_name(intel_crtc->plane));
85208be0
ED
260}
261
1fa61106 262static void ironlake_disable_fbc(struct drm_device *dev)
85208be0
ED
263{
264 struct drm_i915_private *dev_priv = dev->dev_private;
265 u32 dpfc_ctl;
266
267 /* Disable compression */
268 dpfc_ctl = I915_READ(ILK_DPFC_CONTROL);
269 if (dpfc_ctl & DPFC_CTL_EN) {
270 dpfc_ctl &= ~DPFC_CTL_EN;
271 I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl);
272
273 DRM_DEBUG_KMS("disabled FBC\n");
274 }
275}
276
1fa61106 277static bool ironlake_fbc_enabled(struct drm_device *dev)
85208be0
ED
278{
279 struct drm_i915_private *dev_priv = dev->dev_private;
280
281 return I915_READ(ILK_DPFC_CONTROL) & DPFC_CTL_EN;
282}
283
993495ae 284static void gen7_enable_fbc(struct drm_crtc *crtc)
abe959c7
RV
285{
286 struct drm_device *dev = crtc->dev;
287 struct drm_i915_private *dev_priv = dev->dev_private;
f4510a27 288 struct drm_framebuffer *fb = crtc->primary->fb;
2ff8fde1 289 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
abe959c7 290 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3fa2e0ee 291 u32 dpfc_ctl;
abe959c7 292
3fa2e0ee
VS
293 dpfc_ctl = IVB_DPFC_CTL_PLANE(intel_crtc->plane);
294 if (drm_format_plane_cpp(fb->pixel_format, 0) == 2)
5e59f717
BW
295 dev_priv->fbc.threshold++;
296
297 switch (dev_priv->fbc.threshold) {
298 case 4:
299 case 3:
300 dpfc_ctl |= DPFC_CTL_LIMIT_4X;
301 break;
302 case 2:
3fa2e0ee 303 dpfc_ctl |= DPFC_CTL_LIMIT_2X;
5e59f717
BW
304 break;
305 case 1:
3fa2e0ee 306 dpfc_ctl |= DPFC_CTL_LIMIT_1X;
5e59f717
BW
307 break;
308 }
309
3fa2e0ee
VS
310 dpfc_ctl |= IVB_DPFC_CTL_FENCE_EN;
311
da46f936
RV
312 if (dev_priv->fbc.false_color)
313 dpfc_ctl |= FBC_CTL_FALSE_COLOR;
314
3fa2e0ee 315 I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl | DPFC_CTL_EN);
abe959c7 316
891348b2 317 if (IS_IVYBRIDGE(dev)) {
7dd23ba0 318 /* WaFbcAsynchFlipDisableFbcQueue:ivb */
2adb6db8
VS
319 I915_WRITE(ILK_DISPLAY_CHICKEN1,
320 I915_READ(ILK_DISPLAY_CHICKEN1) |
321 ILK_FBCQ_DIS);
28554164 322 } else {
2adb6db8 323 /* WaFbcAsynchFlipDisableFbcQueue:hsw,bdw */
8f670bb1
VS
324 I915_WRITE(CHICKEN_PIPESL_1(intel_crtc->pipe),
325 I915_READ(CHICKEN_PIPESL_1(intel_crtc->pipe)) |
326 HSW_FBCQ_DIS);
891348b2 327 }
b74ea102 328
abe959c7
RV
329 I915_WRITE(SNB_DPFC_CTL_SA,
330 SNB_CPU_FENCE_ENABLE | obj->fence_reg);
331 I915_WRITE(DPFC_CPU_FENCE_OFFSET, crtc->y);
332
333 sandybridge_blit_fbc_update(dev);
334
b19870ee 335 DRM_DEBUG_KMS("enabled fbc on plane %c\n", plane_name(intel_crtc->plane));
abe959c7
RV
336}
337
85208be0
ED
338bool intel_fbc_enabled(struct drm_device *dev)
339{
340 struct drm_i915_private *dev_priv = dev->dev_private;
341
342 if (!dev_priv->display.fbc_enabled)
343 return false;
344
345 return dev_priv->display.fbc_enabled(dev);
346}
347
c5ad011d
RV
348void gen8_fbc_sw_flush(struct drm_device *dev, u32 value)
349{
350 struct drm_i915_private *dev_priv = dev->dev_private;
351
352 if (!IS_GEN8(dev))
353 return;
354
355 I915_WRITE(MSG_FBC_REND_STATE, value);
356}
357
85208be0
ED
358static void intel_fbc_work_fn(struct work_struct *__work)
359{
360 struct intel_fbc_work *work =
361 container_of(to_delayed_work(__work),
362 struct intel_fbc_work, work);
363 struct drm_device *dev = work->crtc->dev;
364 struct drm_i915_private *dev_priv = dev->dev_private;
365
366 mutex_lock(&dev->struct_mutex);
5c3fe8b0 367 if (work == dev_priv->fbc.fbc_work) {
85208be0
ED
368 /* Double check that we haven't switched fb without cancelling
369 * the prior work.
370 */
f4510a27 371 if (work->crtc->primary->fb == work->fb) {
993495ae 372 dev_priv->display.enable_fbc(work->crtc);
85208be0 373
5c3fe8b0 374 dev_priv->fbc.plane = to_intel_crtc(work->crtc)->plane;
f4510a27 375 dev_priv->fbc.fb_id = work->crtc->primary->fb->base.id;
5c3fe8b0 376 dev_priv->fbc.y = work->crtc->y;
85208be0
ED
377 }
378
5c3fe8b0 379 dev_priv->fbc.fbc_work = NULL;
85208be0
ED
380 }
381 mutex_unlock(&dev->struct_mutex);
382
383 kfree(work);
384}
385
386static void intel_cancel_fbc_work(struct drm_i915_private *dev_priv)
387{
5c3fe8b0 388 if (dev_priv->fbc.fbc_work == NULL)
85208be0
ED
389 return;
390
391 DRM_DEBUG_KMS("cancelling pending FBC enable\n");
392
393 /* Synchronisation is provided by struct_mutex and checking of
5c3fe8b0 394 * dev_priv->fbc.fbc_work, so we can perform the cancellation
85208be0
ED
395 * entirely asynchronously.
396 */
5c3fe8b0 397 if (cancel_delayed_work(&dev_priv->fbc.fbc_work->work))
85208be0 398 /* tasklet was killed before being run, clean up */
5c3fe8b0 399 kfree(dev_priv->fbc.fbc_work);
85208be0
ED
400
401 /* Mark the work as no longer wanted so that if it does
402 * wake-up (because the work was already running and waiting
403 * for our mutex), it will discover that is no longer
404 * necessary to run.
405 */
5c3fe8b0 406 dev_priv->fbc.fbc_work = NULL;
85208be0
ED
407}
408
993495ae 409static void intel_enable_fbc(struct drm_crtc *crtc)
85208be0
ED
410{
411 struct intel_fbc_work *work;
412 struct drm_device *dev = crtc->dev;
413 struct drm_i915_private *dev_priv = dev->dev_private;
414
415 if (!dev_priv->display.enable_fbc)
416 return;
417
418 intel_cancel_fbc_work(dev_priv);
419
b14c5679 420 work = kzalloc(sizeof(*work), GFP_KERNEL);
85208be0 421 if (work == NULL) {
6cdcb5e7 422 DRM_ERROR("Failed to allocate FBC work structure\n");
993495ae 423 dev_priv->display.enable_fbc(crtc);
85208be0
ED
424 return;
425 }
426
427 work->crtc = crtc;
f4510a27 428 work->fb = crtc->primary->fb;
85208be0
ED
429 INIT_DELAYED_WORK(&work->work, intel_fbc_work_fn);
430
5c3fe8b0 431 dev_priv->fbc.fbc_work = work;
85208be0 432
85208be0
ED
433 /* Delay the actual enabling to let pageflipping cease and the
434 * display to settle before starting the compression. Note that
435 * this delay also serves a second purpose: it allows for a
436 * vblank to pass after disabling the FBC before we attempt
437 * to modify the control registers.
438 *
439 * A more complicated solution would involve tracking vblanks
440 * following the termination of the page-flipping sequence
441 * and indeed performing the enable as a co-routine and not
442 * waiting synchronously upon the vblank.
7457d617
DL
443 *
444 * WaFbcWaitForVBlankBeforeEnable:ilk,snb
85208be0
ED
445 */
446 schedule_delayed_work(&work->work, msecs_to_jiffies(50));
447}
448
449void intel_disable_fbc(struct drm_device *dev)
450{
451 struct drm_i915_private *dev_priv = dev->dev_private;
452
453 intel_cancel_fbc_work(dev_priv);
454
455 if (!dev_priv->display.disable_fbc)
456 return;
457
458 dev_priv->display.disable_fbc(dev);
5c3fe8b0 459 dev_priv->fbc.plane = -1;
85208be0
ED
460}
461
29ebf90f
CW
462static bool set_no_fbc_reason(struct drm_i915_private *dev_priv,
463 enum no_fbc_reason reason)
464{
465 if (dev_priv->fbc.no_fbc_reason == reason)
466 return false;
467
468 dev_priv->fbc.no_fbc_reason = reason;
469 return true;
470}
471
85208be0
ED
472/**
473 * intel_update_fbc - enable/disable FBC as needed
474 * @dev: the drm_device
475 *
476 * Set up the framebuffer compression hardware at mode set time. We
477 * enable it if possible:
478 * - plane A only (on pre-965)
479 * - no pixel mulitply/line duplication
480 * - no alpha buffer discard
481 * - no dual wide
f85da868 482 * - framebuffer <= max_hdisplay in width, max_vdisplay in height
85208be0
ED
483 *
484 * We can't assume that any compression will take place (worst case),
485 * so the compressed buffer has to be the same size as the uncompressed
486 * one. It also must reside (along with the line length buffer) in
487 * stolen memory.
488 *
489 * We need to enable/disable FBC on a global basis.
490 */
491void intel_update_fbc(struct drm_device *dev)
492{
493 struct drm_i915_private *dev_priv = dev->dev_private;
494 struct drm_crtc *crtc = NULL, *tmp_crtc;
495 struct intel_crtc *intel_crtc;
496 struct drm_framebuffer *fb;
85208be0 497 struct drm_i915_gem_object *obj;
ef644fda 498 const struct drm_display_mode *adjusted_mode;
37327abd 499 unsigned int max_width, max_height;
85208be0 500
3a77c4c4 501 if (!HAS_FBC(dev)) {
29ebf90f 502 set_no_fbc_reason(dev_priv, FBC_UNSUPPORTED);
85208be0 503 return;
29ebf90f 504 }
85208be0 505
d330a953 506 if (!i915.powersave) {
29ebf90f
CW
507 if (set_no_fbc_reason(dev_priv, FBC_MODULE_PARAM))
508 DRM_DEBUG_KMS("fbc disabled per module param\n");
85208be0 509 return;
29ebf90f 510 }
85208be0
ED
511
512 /*
513 * If FBC is already on, we just have to verify that we can
514 * keep it that way...
515 * Need to disable if:
516 * - more than one pipe is active
517 * - changing FBC params (stride, fence, mode)
518 * - new fb is too large to fit in compressed buffer
519 * - going to an unsupported config (interlace, pixel multiply, etc.)
520 */
70e1e0ec 521 for_each_crtc(dev, tmp_crtc) {
3490ea5d 522 if (intel_crtc_active(tmp_crtc) &&
4c445e0e 523 to_intel_crtc(tmp_crtc)->primary_enabled) {
85208be0 524 if (crtc) {
29ebf90f
CW
525 if (set_no_fbc_reason(dev_priv, FBC_MULTIPLE_PIPES))
526 DRM_DEBUG_KMS("more than one pipe active, disabling compression\n");
85208be0
ED
527 goto out_disable;
528 }
529 crtc = tmp_crtc;
530 }
531 }
532
f4510a27 533 if (!crtc || crtc->primary->fb == NULL) {
29ebf90f
CW
534 if (set_no_fbc_reason(dev_priv, FBC_NO_OUTPUT))
535 DRM_DEBUG_KMS("no output, disabling\n");
85208be0
ED
536 goto out_disable;
537 }
538
539 intel_crtc = to_intel_crtc(crtc);
f4510a27 540 fb = crtc->primary->fb;
2ff8fde1 541 obj = intel_fb_obj(fb);
ef644fda 542 adjusted_mode = &intel_crtc->config.adjusted_mode;
85208be0 543
0368920e 544 if (i915.enable_fbc < 0) {
29ebf90f
CW
545 if (set_no_fbc_reason(dev_priv, FBC_CHIP_DEFAULT))
546 DRM_DEBUG_KMS("disabled per chip default\n");
8a5729a3 547 goto out_disable;
85208be0 548 }
d330a953 549 if (!i915.enable_fbc) {
29ebf90f
CW
550 if (set_no_fbc_reason(dev_priv, FBC_MODULE_PARAM))
551 DRM_DEBUG_KMS("fbc disabled per module param\n");
85208be0
ED
552 goto out_disable;
553 }
ef644fda
VS
554 if ((adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) ||
555 (adjusted_mode->flags & DRM_MODE_FLAG_DBLSCAN)) {
29ebf90f
CW
556 if (set_no_fbc_reason(dev_priv, FBC_UNSUPPORTED_MODE))
557 DRM_DEBUG_KMS("mode incompatible with compression, "
558 "disabling\n");
85208be0
ED
559 goto out_disable;
560 }
f85da868 561
032843a5
DS
562 if (INTEL_INFO(dev)->gen >= 8 || IS_HASWELL(dev)) {
563 max_width = 4096;
564 max_height = 4096;
565 } else if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
37327abd
VS
566 max_width = 4096;
567 max_height = 2048;
f85da868 568 } else {
37327abd
VS
569 max_width = 2048;
570 max_height = 1536;
f85da868 571 }
37327abd
VS
572 if (intel_crtc->config.pipe_src_w > max_width ||
573 intel_crtc->config.pipe_src_h > max_height) {
29ebf90f
CW
574 if (set_no_fbc_reason(dev_priv, FBC_MODE_TOO_LARGE))
575 DRM_DEBUG_KMS("mode too large for compression, disabling\n");
85208be0
ED
576 goto out_disable;
577 }
8f94d24b 578 if ((INTEL_INFO(dev)->gen < 4 || HAS_DDI(dev)) &&
c5a44aa0 579 intel_crtc->plane != PLANE_A) {
29ebf90f 580 if (set_no_fbc_reason(dev_priv, FBC_BAD_PLANE))
c5a44aa0 581 DRM_DEBUG_KMS("plane not A, disabling compression\n");
85208be0
ED
582 goto out_disable;
583 }
584
585 /* The use of a CPU fence is mandatory in order to detect writes
586 * by the CPU to the scanout and trigger updates to the FBC.
587 */
588 if (obj->tiling_mode != I915_TILING_X ||
589 obj->fence_reg == I915_FENCE_REG_NONE) {
29ebf90f
CW
590 if (set_no_fbc_reason(dev_priv, FBC_NOT_TILED))
591 DRM_DEBUG_KMS("framebuffer not tiled or fenced, disabling compression\n");
85208be0
ED
592 goto out_disable;
593 }
48404c1e
SJ
594 if (INTEL_INFO(dev)->gen <= 4 && !IS_G4X(dev) &&
595 to_intel_plane(crtc->primary)->rotation != BIT(DRM_ROTATE_0)) {
596 if (set_no_fbc_reason(dev_priv, FBC_UNSUPPORTED_MODE))
597 DRM_DEBUG_KMS("Rotation unsupported, disabling\n");
598 goto out_disable;
599 }
85208be0
ED
600
601 /* If the kernel debugger is active, always disable compression */
602 if (in_dbg_master())
603 goto out_disable;
604
2ff8fde1 605 if (i915_gem_stolen_setup_compression(dev, obj->base.size,
5e59f717 606 drm_format_plane_cpp(fb->pixel_format, 0))) {
29ebf90f
CW
607 if (set_no_fbc_reason(dev_priv, FBC_STOLEN_TOO_SMALL))
608 DRM_DEBUG_KMS("framebuffer too large, disabling compression\n");
11be49eb
CW
609 goto out_disable;
610 }
611
85208be0
ED
612 /* If the scanout has not changed, don't modify the FBC settings.
613 * Note that we make the fundamental assumption that the fb->obj
614 * cannot be unpinned (and have its GTT offset and fence revoked)
615 * without first being decoupled from the scanout and FBC disabled.
616 */
5c3fe8b0
BW
617 if (dev_priv->fbc.plane == intel_crtc->plane &&
618 dev_priv->fbc.fb_id == fb->base.id &&
619 dev_priv->fbc.y == crtc->y)
85208be0
ED
620 return;
621
622 if (intel_fbc_enabled(dev)) {
623 /* We update FBC along two paths, after changing fb/crtc
624 * configuration (modeswitching) and after page-flipping
625 * finishes. For the latter, we know that not only did
626 * we disable the FBC at the start of the page-flip
627 * sequence, but also more than one vblank has passed.
628 *
629 * For the former case of modeswitching, it is possible
630 * to switch between two FBC valid configurations
631 * instantaneously so we do need to disable the FBC
632 * before we can modify its control registers. We also
633 * have to wait for the next vblank for that to take
634 * effect. However, since we delay enabling FBC we can
635 * assume that a vblank has passed since disabling and
636 * that we can safely alter the registers in the deferred
637 * callback.
638 *
639 * In the scenario that we go from a valid to invalid
640 * and then back to valid FBC configuration we have
641 * no strict enforcement that a vblank occurred since
642 * disabling the FBC. However, along all current pipe
643 * disabling paths we do need to wait for a vblank at
644 * some point. And we wait before enabling FBC anyway.
645 */
646 DRM_DEBUG_KMS("disabling active FBC for update\n");
647 intel_disable_fbc(dev);
648 }
649
993495ae 650 intel_enable_fbc(crtc);
29ebf90f 651 dev_priv->fbc.no_fbc_reason = FBC_OK;
85208be0
ED
652 return;
653
654out_disable:
655 /* Multiple disables should be harmless */
656 if (intel_fbc_enabled(dev)) {
657 DRM_DEBUG_KMS("unsupported config, disabling FBC\n");
658 intel_disable_fbc(dev);
659 }
11be49eb 660 i915_gem_stolen_cleanup_compression(dev);
85208be0
ED
661}
662
c921aba8
DV
663static void i915_pineview_get_mem_freq(struct drm_device *dev)
664{
50227e1c 665 struct drm_i915_private *dev_priv = dev->dev_private;
c921aba8
DV
666 u32 tmp;
667
668 tmp = I915_READ(CLKCFG);
669
670 switch (tmp & CLKCFG_FSB_MASK) {
671 case CLKCFG_FSB_533:
672 dev_priv->fsb_freq = 533; /* 133*4 */
673 break;
674 case CLKCFG_FSB_800:
675 dev_priv->fsb_freq = 800; /* 200*4 */
676 break;
677 case CLKCFG_FSB_667:
678 dev_priv->fsb_freq = 667; /* 167*4 */
679 break;
680 case CLKCFG_FSB_400:
681 dev_priv->fsb_freq = 400; /* 100*4 */
682 break;
683 }
684
685 switch (tmp & CLKCFG_MEM_MASK) {
686 case CLKCFG_MEM_533:
687 dev_priv->mem_freq = 533;
688 break;
689 case CLKCFG_MEM_667:
690 dev_priv->mem_freq = 667;
691 break;
692 case CLKCFG_MEM_800:
693 dev_priv->mem_freq = 800;
694 break;
695 }
696
697 /* detect pineview DDR3 setting */
698 tmp = I915_READ(CSHRDDR3CTL);
699 dev_priv->is_ddr3 = (tmp & CSHRDDR3CTL_DDR3) ? 1 : 0;
700}
701
702static void i915_ironlake_get_mem_freq(struct drm_device *dev)
703{
50227e1c 704 struct drm_i915_private *dev_priv = dev->dev_private;
c921aba8
DV
705 u16 ddrpll, csipll;
706
707 ddrpll = I915_READ16(DDRMPLL1);
708 csipll = I915_READ16(CSIPLL0);
709
710 switch (ddrpll & 0xff) {
711 case 0xc:
712 dev_priv->mem_freq = 800;
713 break;
714 case 0x10:
715 dev_priv->mem_freq = 1066;
716 break;
717 case 0x14:
718 dev_priv->mem_freq = 1333;
719 break;
720 case 0x18:
721 dev_priv->mem_freq = 1600;
722 break;
723 default:
724 DRM_DEBUG_DRIVER("unknown memory frequency 0x%02x\n",
725 ddrpll & 0xff);
726 dev_priv->mem_freq = 0;
727 break;
728 }
729
20e4d407 730 dev_priv->ips.r_t = dev_priv->mem_freq;
c921aba8
DV
731
732 switch (csipll & 0x3ff) {
733 case 0x00c:
734 dev_priv->fsb_freq = 3200;
735 break;
736 case 0x00e:
737 dev_priv->fsb_freq = 3733;
738 break;
739 case 0x010:
740 dev_priv->fsb_freq = 4266;
741 break;
742 case 0x012:
743 dev_priv->fsb_freq = 4800;
744 break;
745 case 0x014:
746 dev_priv->fsb_freq = 5333;
747 break;
748 case 0x016:
749 dev_priv->fsb_freq = 5866;
750 break;
751 case 0x018:
752 dev_priv->fsb_freq = 6400;
753 break;
754 default:
755 DRM_DEBUG_DRIVER("unknown fsb frequency 0x%04x\n",
756 csipll & 0x3ff);
757 dev_priv->fsb_freq = 0;
758 break;
759 }
760
761 if (dev_priv->fsb_freq == 3200) {
20e4d407 762 dev_priv->ips.c_m = 0;
c921aba8 763 } else if (dev_priv->fsb_freq > 3200 && dev_priv->fsb_freq <= 4800) {
20e4d407 764 dev_priv->ips.c_m = 1;
c921aba8 765 } else {
20e4d407 766 dev_priv->ips.c_m = 2;
c921aba8
DV
767 }
768}
769
b445e3b0
ED
770static const struct cxsr_latency cxsr_latency_table[] = {
771 {1, 0, 800, 400, 3382, 33382, 3983, 33983}, /* DDR2-400 SC */
772 {1, 0, 800, 667, 3354, 33354, 3807, 33807}, /* DDR2-667 SC */
773 {1, 0, 800, 800, 3347, 33347, 3763, 33763}, /* DDR2-800 SC */
774 {1, 1, 800, 667, 6420, 36420, 6873, 36873}, /* DDR3-667 SC */
775 {1, 1, 800, 800, 5902, 35902, 6318, 36318}, /* DDR3-800 SC */
776
777 {1, 0, 667, 400, 3400, 33400, 4021, 34021}, /* DDR2-400 SC */
778 {1, 0, 667, 667, 3372, 33372, 3845, 33845}, /* DDR2-667 SC */
779 {1, 0, 667, 800, 3386, 33386, 3822, 33822}, /* DDR2-800 SC */
780 {1, 1, 667, 667, 6438, 36438, 6911, 36911}, /* DDR3-667 SC */
781 {1, 1, 667, 800, 5941, 35941, 6377, 36377}, /* DDR3-800 SC */
782
783 {1, 0, 400, 400, 3472, 33472, 4173, 34173}, /* DDR2-400 SC */
784 {1, 0, 400, 667, 3443, 33443, 3996, 33996}, /* DDR2-667 SC */
785 {1, 0, 400, 800, 3430, 33430, 3946, 33946}, /* DDR2-800 SC */
786 {1, 1, 400, 667, 6509, 36509, 7062, 37062}, /* DDR3-667 SC */
787 {1, 1, 400, 800, 5985, 35985, 6501, 36501}, /* DDR3-800 SC */
788
789 {0, 0, 800, 400, 3438, 33438, 4065, 34065}, /* DDR2-400 SC */
790 {0, 0, 800, 667, 3410, 33410, 3889, 33889}, /* DDR2-667 SC */
791 {0, 0, 800, 800, 3403, 33403, 3845, 33845}, /* DDR2-800 SC */
792 {0, 1, 800, 667, 6476, 36476, 6955, 36955}, /* DDR3-667 SC */
793 {0, 1, 800, 800, 5958, 35958, 6400, 36400}, /* DDR3-800 SC */
794
795 {0, 0, 667, 400, 3456, 33456, 4103, 34106}, /* DDR2-400 SC */
796 {0, 0, 667, 667, 3428, 33428, 3927, 33927}, /* DDR2-667 SC */
797 {0, 0, 667, 800, 3443, 33443, 3905, 33905}, /* DDR2-800 SC */
798 {0, 1, 667, 667, 6494, 36494, 6993, 36993}, /* DDR3-667 SC */
799 {0, 1, 667, 800, 5998, 35998, 6460, 36460}, /* DDR3-800 SC */
800
801 {0, 0, 400, 400, 3528, 33528, 4255, 34255}, /* DDR2-400 SC */
802 {0, 0, 400, 667, 3500, 33500, 4079, 34079}, /* DDR2-667 SC */
803 {0, 0, 400, 800, 3487, 33487, 4029, 34029}, /* DDR2-800 SC */
804 {0, 1, 400, 667, 6566, 36566, 7145, 37145}, /* DDR3-667 SC */
805 {0, 1, 400, 800, 6042, 36042, 6584, 36584}, /* DDR3-800 SC */
806};
807
63c62275 808static const struct cxsr_latency *intel_get_cxsr_latency(int is_desktop,
b445e3b0
ED
809 int is_ddr3,
810 int fsb,
811 int mem)
812{
813 const struct cxsr_latency *latency;
814 int i;
815
816 if (fsb == 0 || mem == 0)
817 return NULL;
818
819 for (i = 0; i < ARRAY_SIZE(cxsr_latency_table); i++) {
820 latency = &cxsr_latency_table[i];
821 if (is_desktop == latency->is_desktop &&
822 is_ddr3 == latency->is_ddr3 &&
823 fsb == latency->fsb_freq && mem == latency->mem_freq)
824 return latency;
825 }
826
827 DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
828
829 return NULL;
830}
831
5209b1f4 832void intel_set_memory_cxsr(struct drm_i915_private *dev_priv, bool enable)
b445e3b0 833{
5209b1f4
ID
834 struct drm_device *dev = dev_priv->dev;
835 u32 val;
b445e3b0 836
5209b1f4
ID
837 if (IS_VALLEYVIEW(dev)) {
838 I915_WRITE(FW_BLC_SELF_VLV, enable ? FW_CSPWRDWNEN : 0);
839 } else if (IS_G4X(dev) || IS_CRESTLINE(dev)) {
840 I915_WRITE(FW_BLC_SELF, enable ? FW_BLC_SELF_EN : 0);
841 } else if (IS_PINEVIEW(dev)) {
842 val = I915_READ(DSPFW3) & ~PINEVIEW_SELF_REFRESH_EN;
843 val |= enable ? PINEVIEW_SELF_REFRESH_EN : 0;
844 I915_WRITE(DSPFW3, val);
845 } else if (IS_I945G(dev) || IS_I945GM(dev)) {
846 val = enable ? _MASKED_BIT_ENABLE(FW_BLC_SELF_EN) :
847 _MASKED_BIT_DISABLE(FW_BLC_SELF_EN);
848 I915_WRITE(FW_BLC_SELF, val);
849 } else if (IS_I915GM(dev)) {
850 val = enable ? _MASKED_BIT_ENABLE(INSTPM_SELF_EN) :
851 _MASKED_BIT_DISABLE(INSTPM_SELF_EN);
852 I915_WRITE(INSTPM, val);
853 } else {
854 return;
855 }
b445e3b0 856
5209b1f4
ID
857 DRM_DEBUG_KMS("memory self-refresh is %s\n",
858 enable ? "enabled" : "disabled");
b445e3b0
ED
859}
860
861/*
862 * Latency for FIFO fetches is dependent on several factors:
863 * - memory configuration (speed, channels)
864 * - chipset
865 * - current MCH state
866 * It can be fairly high in some situations, so here we assume a fairly
867 * pessimal value. It's a tradeoff between extra memory fetches (if we
868 * set this value too high, the FIFO will fetch frequently to stay full)
869 * and power consumption (set it too low to save power and we might see
870 * FIFO underruns and display "flicker").
871 *
872 * A value of 5us seems to be a good balance; safe for very low end
873 * platforms but not overly aggressive on lower latency configs.
874 */
875static const int latency_ns = 5000;
876
1fa61106 877static int i9xx_get_fifo_size(struct drm_device *dev, int plane)
b445e3b0
ED
878{
879 struct drm_i915_private *dev_priv = dev->dev_private;
880 uint32_t dsparb = I915_READ(DSPARB);
881 int size;
882
883 size = dsparb & 0x7f;
884 if (plane)
885 size = ((dsparb >> DSPARB_CSTART_SHIFT) & 0x7f) - size;
886
887 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
888 plane ? "B" : "A", size);
889
890 return size;
891}
892
feb56b93 893static int i830_get_fifo_size(struct drm_device *dev, int plane)
b445e3b0
ED
894{
895 struct drm_i915_private *dev_priv = dev->dev_private;
896 uint32_t dsparb = I915_READ(DSPARB);
897 int size;
898
899 size = dsparb & 0x1ff;
900 if (plane)
901 size = ((dsparb >> DSPARB_BEND_SHIFT) & 0x1ff) - size;
902 size >>= 1; /* Convert to cachelines */
903
904 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
905 plane ? "B" : "A", size);
906
907 return size;
908}
909
1fa61106 910static int i845_get_fifo_size(struct drm_device *dev, int plane)
b445e3b0
ED
911{
912 struct drm_i915_private *dev_priv = dev->dev_private;
913 uint32_t dsparb = I915_READ(DSPARB);
914 int size;
915
916 size = dsparb & 0x7f;
917 size >>= 2; /* Convert to cachelines */
918
919 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
920 plane ? "B" : "A",
921 size);
922
923 return size;
924}
925
b445e3b0
ED
926/* Pineview has different values for various configs */
927static const struct intel_watermark_params pineview_display_wm = {
e0f0273e
VS
928 .fifo_size = PINEVIEW_DISPLAY_FIFO,
929 .max_wm = PINEVIEW_MAX_WM,
930 .default_wm = PINEVIEW_DFT_WM,
931 .guard_size = PINEVIEW_GUARD_WM,
932 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
b445e3b0
ED
933};
934static const struct intel_watermark_params pineview_display_hplloff_wm = {
e0f0273e
VS
935 .fifo_size = PINEVIEW_DISPLAY_FIFO,
936 .max_wm = PINEVIEW_MAX_WM,
937 .default_wm = PINEVIEW_DFT_HPLLOFF_WM,
938 .guard_size = PINEVIEW_GUARD_WM,
939 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
b445e3b0
ED
940};
941static const struct intel_watermark_params pineview_cursor_wm = {
e0f0273e
VS
942 .fifo_size = PINEVIEW_CURSOR_FIFO,
943 .max_wm = PINEVIEW_CURSOR_MAX_WM,
944 .default_wm = PINEVIEW_CURSOR_DFT_WM,
945 .guard_size = PINEVIEW_CURSOR_GUARD_WM,
946 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
b445e3b0
ED
947};
948static const struct intel_watermark_params pineview_cursor_hplloff_wm = {
e0f0273e
VS
949 .fifo_size = PINEVIEW_CURSOR_FIFO,
950 .max_wm = PINEVIEW_CURSOR_MAX_WM,
951 .default_wm = PINEVIEW_CURSOR_DFT_WM,
952 .guard_size = PINEVIEW_CURSOR_GUARD_WM,
953 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
b445e3b0
ED
954};
955static const struct intel_watermark_params g4x_wm_info = {
e0f0273e
VS
956 .fifo_size = G4X_FIFO_SIZE,
957 .max_wm = G4X_MAX_WM,
958 .default_wm = G4X_MAX_WM,
959 .guard_size = 2,
960 .cacheline_size = G4X_FIFO_LINE_SIZE,
b445e3b0
ED
961};
962static const struct intel_watermark_params g4x_cursor_wm_info = {
e0f0273e
VS
963 .fifo_size = I965_CURSOR_FIFO,
964 .max_wm = I965_CURSOR_MAX_WM,
965 .default_wm = I965_CURSOR_DFT_WM,
966 .guard_size = 2,
967 .cacheline_size = G4X_FIFO_LINE_SIZE,
b445e3b0
ED
968};
969static const struct intel_watermark_params valleyview_wm_info = {
e0f0273e
VS
970 .fifo_size = VALLEYVIEW_FIFO_SIZE,
971 .max_wm = VALLEYVIEW_MAX_WM,
972 .default_wm = VALLEYVIEW_MAX_WM,
973 .guard_size = 2,
974 .cacheline_size = G4X_FIFO_LINE_SIZE,
b445e3b0
ED
975};
976static const struct intel_watermark_params valleyview_cursor_wm_info = {
e0f0273e
VS
977 .fifo_size = I965_CURSOR_FIFO,
978 .max_wm = VALLEYVIEW_CURSOR_MAX_WM,
979 .default_wm = I965_CURSOR_DFT_WM,
980 .guard_size = 2,
981 .cacheline_size = G4X_FIFO_LINE_SIZE,
b445e3b0
ED
982};
983static const struct intel_watermark_params i965_cursor_wm_info = {
e0f0273e
VS
984 .fifo_size = I965_CURSOR_FIFO,
985 .max_wm = I965_CURSOR_MAX_WM,
986 .default_wm = I965_CURSOR_DFT_WM,
987 .guard_size = 2,
988 .cacheline_size = I915_FIFO_LINE_SIZE,
b445e3b0
ED
989};
990static const struct intel_watermark_params i945_wm_info = {
e0f0273e
VS
991 .fifo_size = I945_FIFO_SIZE,
992 .max_wm = I915_MAX_WM,
993 .default_wm = 1,
994 .guard_size = 2,
995 .cacheline_size = I915_FIFO_LINE_SIZE,
b445e3b0
ED
996};
997static const struct intel_watermark_params i915_wm_info = {
e0f0273e
VS
998 .fifo_size = I915_FIFO_SIZE,
999 .max_wm = I915_MAX_WM,
1000 .default_wm = 1,
1001 .guard_size = 2,
1002 .cacheline_size = I915_FIFO_LINE_SIZE,
b445e3b0 1003};
feb56b93 1004static const struct intel_watermark_params i830_wm_info = {
e0f0273e
VS
1005 .fifo_size = I855GM_FIFO_SIZE,
1006 .max_wm = I915_MAX_WM,
1007 .default_wm = 1,
1008 .guard_size = 2,
1009 .cacheline_size = I830_FIFO_LINE_SIZE,
b445e3b0 1010};
feb56b93 1011static const struct intel_watermark_params i845_wm_info = {
e0f0273e
VS
1012 .fifo_size = I830_FIFO_SIZE,
1013 .max_wm = I915_MAX_WM,
1014 .default_wm = 1,
1015 .guard_size = 2,
1016 .cacheline_size = I830_FIFO_LINE_SIZE,
b445e3b0
ED
1017};
1018
b445e3b0
ED
1019/**
1020 * intel_calculate_wm - calculate watermark level
1021 * @clock_in_khz: pixel clock
1022 * @wm: chip FIFO params
1023 * @pixel_size: display pixel size
1024 * @latency_ns: memory latency for the platform
1025 *
1026 * Calculate the watermark level (the level at which the display plane will
1027 * start fetching from memory again). Each chip has a different display
1028 * FIFO size and allocation, so the caller needs to figure that out and pass
1029 * in the correct intel_watermark_params structure.
1030 *
1031 * As the pixel clock runs, the FIFO will be drained at a rate that depends
1032 * on the pixel size. When it reaches the watermark level, it'll start
1033 * fetching FIFO line sized based chunks from memory until the FIFO fills
1034 * past the watermark point. If the FIFO drains completely, a FIFO underrun
1035 * will occur, and a display engine hang could result.
1036 */
1037static unsigned long intel_calculate_wm(unsigned long clock_in_khz,
1038 const struct intel_watermark_params *wm,
1039 int fifo_size,
1040 int pixel_size,
1041 unsigned long latency_ns)
1042{
1043 long entries_required, wm_size;
1044
1045 /*
1046 * Note: we need to make sure we don't overflow for various clock &
1047 * latency values.
1048 * clocks go from a few thousand to several hundred thousand.
1049 * latency is usually a few thousand
1050 */
1051 entries_required = ((clock_in_khz / 1000) * pixel_size * latency_ns) /
1052 1000;
1053 entries_required = DIV_ROUND_UP(entries_required, wm->cacheline_size);
1054
1055 DRM_DEBUG_KMS("FIFO entries required for mode: %ld\n", entries_required);
1056
1057 wm_size = fifo_size - (entries_required + wm->guard_size);
1058
1059 DRM_DEBUG_KMS("FIFO watermark level: %ld\n", wm_size);
1060
1061 /* Don't promote wm_size to unsigned... */
1062 if (wm_size > (long)wm->max_wm)
1063 wm_size = wm->max_wm;
1064 if (wm_size <= 0)
1065 wm_size = wm->default_wm;
1066 return wm_size;
1067}
1068
1069static struct drm_crtc *single_enabled_crtc(struct drm_device *dev)
1070{
1071 struct drm_crtc *crtc, *enabled = NULL;
1072
70e1e0ec 1073 for_each_crtc(dev, crtc) {
3490ea5d 1074 if (intel_crtc_active(crtc)) {
b445e3b0
ED
1075 if (enabled)
1076 return NULL;
1077 enabled = crtc;
1078 }
1079 }
1080
1081 return enabled;
1082}
1083
46ba614c 1084static void pineview_update_wm(struct drm_crtc *unused_crtc)
b445e3b0 1085{
46ba614c 1086 struct drm_device *dev = unused_crtc->dev;
b445e3b0
ED
1087 struct drm_i915_private *dev_priv = dev->dev_private;
1088 struct drm_crtc *crtc;
1089 const struct cxsr_latency *latency;
1090 u32 reg;
1091 unsigned long wm;
1092
1093 latency = intel_get_cxsr_latency(IS_PINEVIEW_G(dev), dev_priv->is_ddr3,
1094 dev_priv->fsb_freq, dev_priv->mem_freq);
1095 if (!latency) {
1096 DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
5209b1f4 1097 intel_set_memory_cxsr(dev_priv, false);
b445e3b0
ED
1098 return;
1099 }
1100
1101 crtc = single_enabled_crtc(dev);
1102 if (crtc) {
241bfc38 1103 const struct drm_display_mode *adjusted_mode;
f4510a27 1104 int pixel_size = crtc->primary->fb->bits_per_pixel / 8;
241bfc38
DL
1105 int clock;
1106
1107 adjusted_mode = &to_intel_crtc(crtc)->config.adjusted_mode;
1108 clock = adjusted_mode->crtc_clock;
b445e3b0
ED
1109
1110 /* Display SR */
1111 wm = intel_calculate_wm(clock, &pineview_display_wm,
1112 pineview_display_wm.fifo_size,
1113 pixel_size, latency->display_sr);
1114 reg = I915_READ(DSPFW1);
1115 reg &= ~DSPFW_SR_MASK;
1116 reg |= wm << DSPFW_SR_SHIFT;
1117 I915_WRITE(DSPFW1, reg);
1118 DRM_DEBUG_KMS("DSPFW1 register is %x\n", reg);
1119
1120 /* cursor SR */
1121 wm = intel_calculate_wm(clock, &pineview_cursor_wm,
1122 pineview_display_wm.fifo_size,
1123 pixel_size, latency->cursor_sr);
1124 reg = I915_READ(DSPFW3);
1125 reg &= ~DSPFW_CURSOR_SR_MASK;
1126 reg |= (wm & 0x3f) << DSPFW_CURSOR_SR_SHIFT;
1127 I915_WRITE(DSPFW3, reg);
1128
1129 /* Display HPLL off SR */
1130 wm = intel_calculate_wm(clock, &pineview_display_hplloff_wm,
1131 pineview_display_hplloff_wm.fifo_size,
1132 pixel_size, latency->display_hpll_disable);
1133 reg = I915_READ(DSPFW3);
1134 reg &= ~DSPFW_HPLL_SR_MASK;
1135 reg |= wm & DSPFW_HPLL_SR_MASK;
1136 I915_WRITE(DSPFW3, reg);
1137
1138 /* cursor HPLL off SR */
1139 wm = intel_calculate_wm(clock, &pineview_cursor_hplloff_wm,
1140 pineview_display_hplloff_wm.fifo_size,
1141 pixel_size, latency->cursor_hpll_disable);
1142 reg = I915_READ(DSPFW3);
1143 reg &= ~DSPFW_HPLL_CURSOR_MASK;
1144 reg |= (wm & 0x3f) << DSPFW_HPLL_CURSOR_SHIFT;
1145 I915_WRITE(DSPFW3, reg);
1146 DRM_DEBUG_KMS("DSPFW3 register is %x\n", reg);
1147
5209b1f4 1148 intel_set_memory_cxsr(dev_priv, true);
b445e3b0 1149 } else {
5209b1f4 1150 intel_set_memory_cxsr(dev_priv, false);
b445e3b0
ED
1151 }
1152}
1153
1154static bool g4x_compute_wm0(struct drm_device *dev,
1155 int plane,
1156 const struct intel_watermark_params *display,
1157 int display_latency_ns,
1158 const struct intel_watermark_params *cursor,
1159 int cursor_latency_ns,
1160 int *plane_wm,
1161 int *cursor_wm)
1162{
1163 struct drm_crtc *crtc;
4fe8590a 1164 const struct drm_display_mode *adjusted_mode;
b445e3b0
ED
1165 int htotal, hdisplay, clock, pixel_size;
1166 int line_time_us, line_count;
1167 int entries, tlb_miss;
1168
1169 crtc = intel_get_crtc_for_plane(dev, plane);
3490ea5d 1170 if (!intel_crtc_active(crtc)) {
b445e3b0
ED
1171 *cursor_wm = cursor->guard_size;
1172 *plane_wm = display->guard_size;
1173 return false;
1174 }
1175
4fe8590a 1176 adjusted_mode = &to_intel_crtc(crtc)->config.adjusted_mode;
241bfc38 1177 clock = adjusted_mode->crtc_clock;
fec8cba3 1178 htotal = adjusted_mode->crtc_htotal;
37327abd 1179 hdisplay = to_intel_crtc(crtc)->config.pipe_src_w;
f4510a27 1180 pixel_size = crtc->primary->fb->bits_per_pixel / 8;
b445e3b0
ED
1181
1182 /* Use the small buffer method to calculate plane watermark */
1183 entries = ((clock * pixel_size / 1000) * display_latency_ns) / 1000;
1184 tlb_miss = display->fifo_size*display->cacheline_size - hdisplay * 8;
1185 if (tlb_miss > 0)
1186 entries += tlb_miss;
1187 entries = DIV_ROUND_UP(entries, display->cacheline_size);
1188 *plane_wm = entries + display->guard_size;
1189 if (*plane_wm > (int)display->max_wm)
1190 *plane_wm = display->max_wm;
1191
1192 /* Use the large buffer method to calculate cursor watermark */
922044c9 1193 line_time_us = max(htotal * 1000 / clock, 1);
b445e3b0 1194 line_count = (cursor_latency_ns / line_time_us + 1000) / 1000;
7bb836dd 1195 entries = line_count * to_intel_crtc(crtc)->cursor_width * pixel_size;
b445e3b0
ED
1196 tlb_miss = cursor->fifo_size*cursor->cacheline_size - hdisplay * 8;
1197 if (tlb_miss > 0)
1198 entries += tlb_miss;
1199 entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
1200 *cursor_wm = entries + cursor->guard_size;
1201 if (*cursor_wm > (int)cursor->max_wm)
1202 *cursor_wm = (int)cursor->max_wm;
1203
1204 return true;
1205}
1206
1207/*
1208 * Check the wm result.
1209 *
1210 * If any calculated watermark values is larger than the maximum value that
1211 * can be programmed into the associated watermark register, that watermark
1212 * must be disabled.
1213 */
1214static bool g4x_check_srwm(struct drm_device *dev,
1215 int display_wm, int cursor_wm,
1216 const struct intel_watermark_params *display,
1217 const struct intel_watermark_params *cursor)
1218{
1219 DRM_DEBUG_KMS("SR watermark: display plane %d, cursor %d\n",
1220 display_wm, cursor_wm);
1221
1222 if (display_wm > display->max_wm) {
1223 DRM_DEBUG_KMS("display watermark is too large(%d/%ld), disabling\n",
1224 display_wm, display->max_wm);
1225 return false;
1226 }
1227
1228 if (cursor_wm > cursor->max_wm) {
1229 DRM_DEBUG_KMS("cursor watermark is too large(%d/%ld), disabling\n",
1230 cursor_wm, cursor->max_wm);
1231 return false;
1232 }
1233
1234 if (!(display_wm || cursor_wm)) {
1235 DRM_DEBUG_KMS("SR latency is 0, disabling\n");
1236 return false;
1237 }
1238
1239 return true;
1240}
1241
1242static bool g4x_compute_srwm(struct drm_device *dev,
1243 int plane,
1244 int latency_ns,
1245 const struct intel_watermark_params *display,
1246 const struct intel_watermark_params *cursor,
1247 int *display_wm, int *cursor_wm)
1248{
1249 struct drm_crtc *crtc;
4fe8590a 1250 const struct drm_display_mode *adjusted_mode;
b445e3b0
ED
1251 int hdisplay, htotal, pixel_size, clock;
1252 unsigned long line_time_us;
1253 int line_count, line_size;
1254 int small, large;
1255 int entries;
1256
1257 if (!latency_ns) {
1258 *display_wm = *cursor_wm = 0;
1259 return false;
1260 }
1261
1262 crtc = intel_get_crtc_for_plane(dev, plane);
4fe8590a 1263 adjusted_mode = &to_intel_crtc(crtc)->config.adjusted_mode;
241bfc38 1264 clock = adjusted_mode->crtc_clock;
fec8cba3 1265 htotal = adjusted_mode->crtc_htotal;
37327abd 1266 hdisplay = to_intel_crtc(crtc)->config.pipe_src_w;
f4510a27 1267 pixel_size = crtc->primary->fb->bits_per_pixel / 8;
b445e3b0 1268
922044c9 1269 line_time_us = max(htotal * 1000 / clock, 1);
b445e3b0
ED
1270 line_count = (latency_ns / line_time_us + 1000) / 1000;
1271 line_size = hdisplay * pixel_size;
1272
1273 /* Use the minimum of the small and large buffer method for primary */
1274 small = ((clock * pixel_size / 1000) * latency_ns) / 1000;
1275 large = line_count * line_size;
1276
1277 entries = DIV_ROUND_UP(min(small, large), display->cacheline_size);
1278 *display_wm = entries + display->guard_size;
1279
1280 /* calculate the self-refresh watermark for display cursor */
7bb836dd 1281 entries = line_count * pixel_size * to_intel_crtc(crtc)->cursor_width;
b445e3b0
ED
1282 entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
1283 *cursor_wm = entries + cursor->guard_size;
1284
1285 return g4x_check_srwm(dev,
1286 *display_wm, *cursor_wm,
1287 display, cursor);
1288}
1289
0948c265
GB
1290static bool vlv_compute_drain_latency(struct drm_crtc *crtc,
1291 int pixel_size,
1292 int *prec_mult,
1293 int *drain_latency)
b445e3b0 1294{
b445e3b0 1295 int entries;
0948c265 1296 int clock = to_intel_crtc(crtc)->config.adjusted_mode.crtc_clock;
b445e3b0 1297
0948c265 1298 if (WARN(clock == 0, "Pixel clock is zero!\n"))
b445e3b0
ED
1299 return false;
1300
0948c265
GB
1301 if (WARN(pixel_size == 0, "Pixel size is zero!\n"))
1302 return false;
b445e3b0 1303
a398e9c7 1304 entries = DIV_ROUND_UP(clock, 1000) * pixel_size;
0948c265
GB
1305 *prec_mult = (entries > 128) ? DRAIN_LATENCY_PRECISION_64 :
1306 DRAIN_LATENCY_PRECISION_32;
1307 *drain_latency = (64 * (*prec_mult) * 4) / entries;
b445e3b0 1308
a398e9c7
GB
1309 if (*drain_latency > DRAIN_LATENCY_MASK)
1310 *drain_latency = DRAIN_LATENCY_MASK;
b445e3b0
ED
1311
1312 return true;
1313}
1314
1315/*
1316 * Update drain latency registers of memory arbiter
1317 *
1318 * Valleyview SoC has a new memory arbiter and needs drain latency registers
1319 * to be programmed. Each plane has a drain latency multiplier and a drain
1320 * latency value.
1321 */
1322
41aad816 1323static void vlv_update_drain_latency(struct drm_crtc *crtc)
b445e3b0 1324{
0948c265
GB
1325 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
1326 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1327 int pixel_size;
1328 int drain_latency;
1329 enum pipe pipe = intel_crtc->pipe;
1330 int plane_prec, prec_mult, plane_dl;
b445e3b0 1331
0948c265
GB
1332 plane_dl = I915_READ(VLV_DDL(pipe)) & ~(DDL_PLANE_PRECISION_64 |
1333 DRAIN_LATENCY_MASK | DDL_CURSOR_PRECISION_64 |
1334 (DRAIN_LATENCY_MASK << DDL_CURSOR_SHIFT));
1335
1336 if (!intel_crtc_active(crtc)) {
1337 I915_WRITE(VLV_DDL(pipe), plane_dl);
1338 return;
1339 }
b445e3b0 1340
0948c265
GB
1341 /* Primary plane Drain Latency */
1342 pixel_size = crtc->primary->fb->bits_per_pixel / 8; /* BPP */
1343 if (vlv_compute_drain_latency(crtc, pixel_size, &prec_mult, &drain_latency)) {
1344 plane_prec = (prec_mult == DRAIN_LATENCY_PRECISION_64) ?
1345 DDL_PLANE_PRECISION_64 :
1346 DDL_PLANE_PRECISION_32;
1347 plane_dl |= plane_prec | drain_latency;
b445e3b0
ED
1348 }
1349
0948c265
GB
1350 /* Cursor Drain Latency
1351 * BPP is always 4 for cursor
1352 */
1353 pixel_size = 4;
b445e3b0 1354
0948c265
GB
1355 /* Program cursor DL only if it is enabled */
1356 if (intel_crtc->cursor_base &&
1357 vlv_compute_drain_latency(crtc, pixel_size, &prec_mult, &drain_latency)) {
1358 plane_prec = (prec_mult == DRAIN_LATENCY_PRECISION_64) ?
1359 DDL_CURSOR_PRECISION_64 :
1360 DDL_CURSOR_PRECISION_32;
1361 plane_dl |= plane_prec | (drain_latency << DDL_CURSOR_SHIFT);
b445e3b0 1362 }
0948c265
GB
1363
1364 I915_WRITE(VLV_DDL(pipe), plane_dl);
b445e3b0
ED
1365}
1366
1367#define single_plane_enabled(mask) is_power_of_2(mask)
1368
46ba614c 1369static void valleyview_update_wm(struct drm_crtc *crtc)
b445e3b0 1370{
46ba614c 1371 struct drm_device *dev = crtc->dev;
b445e3b0
ED
1372 static const int sr_latency_ns = 12000;
1373 struct drm_i915_private *dev_priv = dev->dev_private;
1374 int planea_wm, planeb_wm, cursora_wm, cursorb_wm;
1375 int plane_sr, cursor_sr;
af6c4575 1376 int ignore_plane_sr, ignore_cursor_sr;
b445e3b0 1377 unsigned int enabled = 0;
9858425c 1378 bool cxsr_enabled;
b445e3b0 1379
41aad816 1380 vlv_update_drain_latency(crtc);
b445e3b0 1381
51cea1f4 1382 if (g4x_compute_wm0(dev, PIPE_A,
b445e3b0
ED
1383 &valleyview_wm_info, latency_ns,
1384 &valleyview_cursor_wm_info, latency_ns,
1385 &planea_wm, &cursora_wm))
51cea1f4 1386 enabled |= 1 << PIPE_A;
b445e3b0 1387
51cea1f4 1388 if (g4x_compute_wm0(dev, PIPE_B,
b445e3b0
ED
1389 &valleyview_wm_info, latency_ns,
1390 &valleyview_cursor_wm_info, latency_ns,
1391 &planeb_wm, &cursorb_wm))
51cea1f4 1392 enabled |= 1 << PIPE_B;
b445e3b0 1393
b445e3b0
ED
1394 if (single_plane_enabled(enabled) &&
1395 g4x_compute_srwm(dev, ffs(enabled) - 1,
1396 sr_latency_ns,
1397 &valleyview_wm_info,
1398 &valleyview_cursor_wm_info,
af6c4575
CW
1399 &plane_sr, &ignore_cursor_sr) &&
1400 g4x_compute_srwm(dev, ffs(enabled) - 1,
1401 2*sr_latency_ns,
1402 &valleyview_wm_info,
1403 &valleyview_cursor_wm_info,
52bd02d8 1404 &ignore_plane_sr, &cursor_sr)) {
9858425c 1405 cxsr_enabled = true;
52bd02d8 1406 } else {
9858425c 1407 cxsr_enabled = false;
5209b1f4 1408 intel_set_memory_cxsr(dev_priv, false);
52bd02d8
CW
1409 plane_sr = cursor_sr = 0;
1410 }
b445e3b0 1411
a5043453
VS
1412 DRM_DEBUG_KMS("Setting FIFO watermarks - A: plane=%d, cursor=%d, "
1413 "B: plane=%d, cursor=%d, SR: plane=%d, cursor=%d\n",
b445e3b0
ED
1414 planea_wm, cursora_wm,
1415 planeb_wm, cursorb_wm,
1416 plane_sr, cursor_sr);
1417
1418 I915_WRITE(DSPFW1,
1419 (plane_sr << DSPFW_SR_SHIFT) |
1420 (cursorb_wm << DSPFW_CURSORB_SHIFT) |
1421 (planeb_wm << DSPFW_PLANEB_SHIFT) |
0a560674 1422 (planea_wm << DSPFW_PLANEA_SHIFT));
b445e3b0 1423 I915_WRITE(DSPFW2,
8c919b28 1424 (I915_READ(DSPFW2) & ~DSPFW_CURSORA_MASK) |
b445e3b0
ED
1425 (cursora_wm << DSPFW_CURSORA_SHIFT));
1426 I915_WRITE(DSPFW3,
8c919b28
CW
1427 (I915_READ(DSPFW3) & ~DSPFW_CURSOR_SR_MASK) |
1428 (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
9858425c
ID
1429
1430 if (cxsr_enabled)
1431 intel_set_memory_cxsr(dev_priv, true);
b445e3b0
ED
1432}
1433
3c2777fd
VS
1434static void cherryview_update_wm(struct drm_crtc *crtc)
1435{
1436 struct drm_device *dev = crtc->dev;
1437 static const int sr_latency_ns = 12000;
1438 struct drm_i915_private *dev_priv = dev->dev_private;
1439 int planea_wm, planeb_wm, planec_wm;
1440 int cursora_wm, cursorb_wm, cursorc_wm;
1441 int plane_sr, cursor_sr;
1442 int ignore_plane_sr, ignore_cursor_sr;
1443 unsigned int enabled = 0;
1444 bool cxsr_enabled;
1445
1446 vlv_update_drain_latency(crtc);
1447
1448 if (g4x_compute_wm0(dev, PIPE_A,
1449 &valleyview_wm_info, latency_ns,
1450 &valleyview_cursor_wm_info, latency_ns,
1451 &planea_wm, &cursora_wm))
1452 enabled |= 1 << PIPE_A;
1453
1454 if (g4x_compute_wm0(dev, PIPE_B,
1455 &valleyview_wm_info, latency_ns,
1456 &valleyview_cursor_wm_info, latency_ns,
1457 &planeb_wm, &cursorb_wm))
1458 enabled |= 1 << PIPE_B;
1459
1460 if (g4x_compute_wm0(dev, PIPE_C,
1461 &valleyview_wm_info, latency_ns,
1462 &valleyview_cursor_wm_info, latency_ns,
1463 &planec_wm, &cursorc_wm))
1464 enabled |= 1 << PIPE_C;
1465
1466 if (single_plane_enabled(enabled) &&
1467 g4x_compute_srwm(dev, ffs(enabled) - 1,
1468 sr_latency_ns,
1469 &valleyview_wm_info,
1470 &valleyview_cursor_wm_info,
1471 &plane_sr, &ignore_cursor_sr) &&
1472 g4x_compute_srwm(dev, ffs(enabled) - 1,
1473 2*sr_latency_ns,
1474 &valleyview_wm_info,
1475 &valleyview_cursor_wm_info,
1476 &ignore_plane_sr, &cursor_sr)) {
1477 cxsr_enabled = true;
1478 } else {
1479 cxsr_enabled = false;
1480 intel_set_memory_cxsr(dev_priv, false);
1481 plane_sr = cursor_sr = 0;
1482 }
1483
1484 DRM_DEBUG_KMS("Setting FIFO watermarks - A: plane=%d, cursor=%d, "
1485 "B: plane=%d, cursor=%d, C: plane=%d, cursor=%d, "
1486 "SR: plane=%d, cursor=%d\n",
1487 planea_wm, cursora_wm,
1488 planeb_wm, cursorb_wm,
1489 planec_wm, cursorc_wm,
1490 plane_sr, cursor_sr);
1491
1492 I915_WRITE(DSPFW1,
1493 (plane_sr << DSPFW_SR_SHIFT) |
1494 (cursorb_wm << DSPFW_CURSORB_SHIFT) |
1495 (planeb_wm << DSPFW_PLANEB_SHIFT) |
1496 (planea_wm << DSPFW_PLANEA_SHIFT));
1497 I915_WRITE(DSPFW2,
1498 (I915_READ(DSPFW2) & ~DSPFW_CURSORA_MASK) |
1499 (cursora_wm << DSPFW_CURSORA_SHIFT));
1500 I915_WRITE(DSPFW3,
1501 (I915_READ(DSPFW3) & ~DSPFW_CURSOR_SR_MASK) |
1502 (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
1503 I915_WRITE(DSPFW9_CHV,
1504 (I915_READ(DSPFW9_CHV) & ~(DSPFW_PLANEC_MASK |
1505 DSPFW_CURSORC_MASK)) |
1506 (planec_wm << DSPFW_PLANEC_SHIFT) |
1507 (cursorc_wm << DSPFW_CURSORC_SHIFT));
1508
1509 if (cxsr_enabled)
1510 intel_set_memory_cxsr(dev_priv, true);
1511}
1512
01e184cc
GB
1513static void valleyview_update_sprite_wm(struct drm_plane *plane,
1514 struct drm_crtc *crtc,
1515 uint32_t sprite_width,
1516 uint32_t sprite_height,
1517 int pixel_size,
1518 bool enabled, bool scaled)
1519{
1520 struct drm_device *dev = crtc->dev;
1521 struct drm_i915_private *dev_priv = dev->dev_private;
1522 int pipe = to_intel_plane(plane)->pipe;
1523 int sprite = to_intel_plane(plane)->plane;
1524 int drain_latency;
1525 int plane_prec;
1526 int sprite_dl;
1527 int prec_mult;
1528
1529 sprite_dl = I915_READ(VLV_DDL(pipe)) & ~(DDL_SPRITE_PRECISION_64(sprite) |
1530 (DRAIN_LATENCY_MASK << DDL_SPRITE_SHIFT(sprite)));
1531
1532 if (enabled && vlv_compute_drain_latency(crtc, pixel_size, &prec_mult,
1533 &drain_latency)) {
1534 plane_prec = (prec_mult == DRAIN_LATENCY_PRECISION_64) ?
1535 DDL_SPRITE_PRECISION_64(sprite) :
1536 DDL_SPRITE_PRECISION_32(sprite);
1537 sprite_dl |= plane_prec |
1538 (drain_latency << DDL_SPRITE_SHIFT(sprite));
1539 }
1540
1541 I915_WRITE(VLV_DDL(pipe), sprite_dl);
1542}
1543
46ba614c 1544static void g4x_update_wm(struct drm_crtc *crtc)
b445e3b0 1545{
46ba614c 1546 struct drm_device *dev = crtc->dev;
b445e3b0
ED
1547 static const int sr_latency_ns = 12000;
1548 struct drm_i915_private *dev_priv = dev->dev_private;
1549 int planea_wm, planeb_wm, cursora_wm, cursorb_wm;
1550 int plane_sr, cursor_sr;
1551 unsigned int enabled = 0;
9858425c 1552 bool cxsr_enabled;
b445e3b0 1553
51cea1f4 1554 if (g4x_compute_wm0(dev, PIPE_A,
b445e3b0
ED
1555 &g4x_wm_info, latency_ns,
1556 &g4x_cursor_wm_info, latency_ns,
1557 &planea_wm, &cursora_wm))
51cea1f4 1558 enabled |= 1 << PIPE_A;
b445e3b0 1559
51cea1f4 1560 if (g4x_compute_wm0(dev, PIPE_B,
b445e3b0
ED
1561 &g4x_wm_info, latency_ns,
1562 &g4x_cursor_wm_info, latency_ns,
1563 &planeb_wm, &cursorb_wm))
51cea1f4 1564 enabled |= 1 << PIPE_B;
b445e3b0 1565
b445e3b0
ED
1566 if (single_plane_enabled(enabled) &&
1567 g4x_compute_srwm(dev, ffs(enabled) - 1,
1568 sr_latency_ns,
1569 &g4x_wm_info,
1570 &g4x_cursor_wm_info,
52bd02d8 1571 &plane_sr, &cursor_sr)) {
9858425c 1572 cxsr_enabled = true;
52bd02d8 1573 } else {
9858425c 1574 cxsr_enabled = false;
5209b1f4 1575 intel_set_memory_cxsr(dev_priv, false);
52bd02d8
CW
1576 plane_sr = cursor_sr = 0;
1577 }
b445e3b0 1578
a5043453
VS
1579 DRM_DEBUG_KMS("Setting FIFO watermarks - A: plane=%d, cursor=%d, "
1580 "B: plane=%d, cursor=%d, SR: plane=%d, cursor=%d\n",
b445e3b0
ED
1581 planea_wm, cursora_wm,
1582 planeb_wm, cursorb_wm,
1583 plane_sr, cursor_sr);
1584
1585 I915_WRITE(DSPFW1,
1586 (plane_sr << DSPFW_SR_SHIFT) |
1587 (cursorb_wm << DSPFW_CURSORB_SHIFT) |
1588 (planeb_wm << DSPFW_PLANEB_SHIFT) |
0a560674 1589 (planea_wm << DSPFW_PLANEA_SHIFT));
b445e3b0 1590 I915_WRITE(DSPFW2,
8c919b28 1591 (I915_READ(DSPFW2) & ~DSPFW_CURSORA_MASK) |
b445e3b0
ED
1592 (cursora_wm << DSPFW_CURSORA_SHIFT));
1593 /* HPLL off in SR has some issues on G4x... disable it */
1594 I915_WRITE(DSPFW3,
8c919b28 1595 (I915_READ(DSPFW3) & ~(DSPFW_HPLL_SR_EN | DSPFW_CURSOR_SR_MASK)) |
b445e3b0 1596 (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
9858425c
ID
1597
1598 if (cxsr_enabled)
1599 intel_set_memory_cxsr(dev_priv, true);
b445e3b0
ED
1600}
1601
46ba614c 1602static void i965_update_wm(struct drm_crtc *unused_crtc)
b445e3b0 1603{
46ba614c 1604 struct drm_device *dev = unused_crtc->dev;
b445e3b0
ED
1605 struct drm_i915_private *dev_priv = dev->dev_private;
1606 struct drm_crtc *crtc;
1607 int srwm = 1;
1608 int cursor_sr = 16;
9858425c 1609 bool cxsr_enabled;
b445e3b0
ED
1610
1611 /* Calc sr entries for one plane configs */
1612 crtc = single_enabled_crtc(dev);
1613 if (crtc) {
1614 /* self-refresh has much higher latency */
1615 static const int sr_latency_ns = 12000;
4fe8590a
VS
1616 const struct drm_display_mode *adjusted_mode =
1617 &to_intel_crtc(crtc)->config.adjusted_mode;
241bfc38 1618 int clock = adjusted_mode->crtc_clock;
fec8cba3 1619 int htotal = adjusted_mode->crtc_htotal;
37327abd 1620 int hdisplay = to_intel_crtc(crtc)->config.pipe_src_w;
f4510a27 1621 int pixel_size = crtc->primary->fb->bits_per_pixel / 8;
b445e3b0
ED
1622 unsigned long line_time_us;
1623 int entries;
1624
922044c9 1625 line_time_us = max(htotal * 1000 / clock, 1);
b445e3b0
ED
1626
1627 /* Use ns/us then divide to preserve precision */
1628 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
1629 pixel_size * hdisplay;
1630 entries = DIV_ROUND_UP(entries, I915_FIFO_LINE_SIZE);
1631 srwm = I965_FIFO_SIZE - entries;
1632 if (srwm < 0)
1633 srwm = 1;
1634 srwm &= 0x1ff;
1635 DRM_DEBUG_KMS("self-refresh entries: %d, wm: %d\n",
1636 entries, srwm);
1637
1638 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
7bb836dd 1639 pixel_size * to_intel_crtc(crtc)->cursor_width;
b445e3b0
ED
1640 entries = DIV_ROUND_UP(entries,
1641 i965_cursor_wm_info.cacheline_size);
1642 cursor_sr = i965_cursor_wm_info.fifo_size -
1643 (entries + i965_cursor_wm_info.guard_size);
1644
1645 if (cursor_sr > i965_cursor_wm_info.max_wm)
1646 cursor_sr = i965_cursor_wm_info.max_wm;
1647
1648 DRM_DEBUG_KMS("self-refresh watermark: display plane %d "
1649 "cursor %d\n", srwm, cursor_sr);
1650
9858425c 1651 cxsr_enabled = true;
b445e3b0 1652 } else {
9858425c 1653 cxsr_enabled = false;
b445e3b0 1654 /* Turn off self refresh if both pipes are enabled */
5209b1f4 1655 intel_set_memory_cxsr(dev_priv, false);
b445e3b0
ED
1656 }
1657
1658 DRM_DEBUG_KMS("Setting FIFO watermarks - A: 8, B: 8, C: 8, SR %d\n",
1659 srwm);
1660
1661 /* 965 has limitations... */
1662 I915_WRITE(DSPFW1, (srwm << DSPFW_SR_SHIFT) |
0a560674
VS
1663 (8 << DSPFW_CURSORB_SHIFT) |
1664 (8 << DSPFW_PLANEB_SHIFT) |
1665 (8 << DSPFW_PLANEA_SHIFT));
1666 I915_WRITE(DSPFW2, (8 << DSPFW_CURSORA_SHIFT) |
1667 (8 << DSPFW_PLANEC_SHIFT_OLD));
b445e3b0
ED
1668 /* update cursor SR watermark */
1669 I915_WRITE(DSPFW3, (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
9858425c
ID
1670
1671 if (cxsr_enabled)
1672 intel_set_memory_cxsr(dev_priv, true);
b445e3b0
ED
1673}
1674
46ba614c 1675static void i9xx_update_wm(struct drm_crtc *unused_crtc)
b445e3b0 1676{
46ba614c 1677 struct drm_device *dev = unused_crtc->dev;
b445e3b0
ED
1678 struct drm_i915_private *dev_priv = dev->dev_private;
1679 const struct intel_watermark_params *wm_info;
1680 uint32_t fwater_lo;
1681 uint32_t fwater_hi;
1682 int cwm, srwm = 1;
1683 int fifo_size;
1684 int planea_wm, planeb_wm;
1685 struct drm_crtc *crtc, *enabled = NULL;
1686
1687 if (IS_I945GM(dev))
1688 wm_info = &i945_wm_info;
1689 else if (!IS_GEN2(dev))
1690 wm_info = &i915_wm_info;
1691 else
feb56b93 1692 wm_info = &i830_wm_info;
b445e3b0
ED
1693
1694 fifo_size = dev_priv->display.get_fifo_size(dev, 0);
1695 crtc = intel_get_crtc_for_plane(dev, 0);
3490ea5d 1696 if (intel_crtc_active(crtc)) {
241bfc38 1697 const struct drm_display_mode *adjusted_mode;
f4510a27 1698 int cpp = crtc->primary->fb->bits_per_pixel / 8;
b9e0bda3
CW
1699 if (IS_GEN2(dev))
1700 cpp = 4;
1701
241bfc38
DL
1702 adjusted_mode = &to_intel_crtc(crtc)->config.adjusted_mode;
1703 planea_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
b9e0bda3 1704 wm_info, fifo_size, cpp,
b445e3b0
ED
1705 latency_ns);
1706 enabled = crtc;
1707 } else
1708 planea_wm = fifo_size - wm_info->guard_size;
1709
1710 fifo_size = dev_priv->display.get_fifo_size(dev, 1);
1711 crtc = intel_get_crtc_for_plane(dev, 1);
3490ea5d 1712 if (intel_crtc_active(crtc)) {
241bfc38 1713 const struct drm_display_mode *adjusted_mode;
f4510a27 1714 int cpp = crtc->primary->fb->bits_per_pixel / 8;
b9e0bda3
CW
1715 if (IS_GEN2(dev))
1716 cpp = 4;
1717
241bfc38
DL
1718 adjusted_mode = &to_intel_crtc(crtc)->config.adjusted_mode;
1719 planeb_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
b9e0bda3 1720 wm_info, fifo_size, cpp,
b445e3b0
ED
1721 latency_ns);
1722 if (enabled == NULL)
1723 enabled = crtc;
1724 else
1725 enabled = NULL;
1726 } else
1727 planeb_wm = fifo_size - wm_info->guard_size;
1728
1729 DRM_DEBUG_KMS("FIFO watermarks - A: %d, B: %d\n", planea_wm, planeb_wm);
1730
2ab1bc9d 1731 if (IS_I915GM(dev) && enabled) {
2ff8fde1 1732 struct drm_i915_gem_object *obj;
2ab1bc9d 1733
2ff8fde1 1734 obj = intel_fb_obj(enabled->primary->fb);
2ab1bc9d
DV
1735
1736 /* self-refresh seems busted with untiled */
2ff8fde1 1737 if (obj->tiling_mode == I915_TILING_NONE)
2ab1bc9d
DV
1738 enabled = NULL;
1739 }
1740
b445e3b0
ED
1741 /*
1742 * Overlay gets an aggressive default since video jitter is bad.
1743 */
1744 cwm = 2;
1745
1746 /* Play safe and disable self-refresh before adjusting watermarks. */
5209b1f4 1747 intel_set_memory_cxsr(dev_priv, false);
b445e3b0
ED
1748
1749 /* Calc sr entries for one plane configs */
1750 if (HAS_FW_BLC(dev) && enabled) {
1751 /* self-refresh has much higher latency */
1752 static const int sr_latency_ns = 6000;
4fe8590a
VS
1753 const struct drm_display_mode *adjusted_mode =
1754 &to_intel_crtc(enabled)->config.adjusted_mode;
241bfc38 1755 int clock = adjusted_mode->crtc_clock;
fec8cba3 1756 int htotal = adjusted_mode->crtc_htotal;
f727b490 1757 int hdisplay = to_intel_crtc(enabled)->config.pipe_src_w;
f4510a27 1758 int pixel_size = enabled->primary->fb->bits_per_pixel / 8;
b445e3b0
ED
1759 unsigned long line_time_us;
1760 int entries;
1761
922044c9 1762 line_time_us = max(htotal * 1000 / clock, 1);
b445e3b0
ED
1763
1764 /* Use ns/us then divide to preserve precision */
1765 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
1766 pixel_size * hdisplay;
1767 entries = DIV_ROUND_UP(entries, wm_info->cacheline_size);
1768 DRM_DEBUG_KMS("self-refresh entries: %d\n", entries);
1769 srwm = wm_info->fifo_size - entries;
1770 if (srwm < 0)
1771 srwm = 1;
1772
1773 if (IS_I945G(dev) || IS_I945GM(dev))
1774 I915_WRITE(FW_BLC_SELF,
1775 FW_BLC_SELF_FIFO_MASK | (srwm & 0xff));
1776 else if (IS_I915GM(dev))
1777 I915_WRITE(FW_BLC_SELF, srwm & 0x3f);
1778 }
1779
1780 DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d, B: %d, C: %d, SR %d\n",
1781 planea_wm, planeb_wm, cwm, srwm);
1782
1783 fwater_lo = ((planeb_wm & 0x3f) << 16) | (planea_wm & 0x3f);
1784 fwater_hi = (cwm & 0x1f);
1785
1786 /* Set request length to 8 cachelines per fetch */
1787 fwater_lo = fwater_lo | (1 << 24) | (1 << 8);
1788 fwater_hi = fwater_hi | (1 << 8);
1789
1790 I915_WRITE(FW_BLC, fwater_lo);
1791 I915_WRITE(FW_BLC2, fwater_hi);
1792
5209b1f4
ID
1793 if (enabled)
1794 intel_set_memory_cxsr(dev_priv, true);
b445e3b0
ED
1795}
1796
feb56b93 1797static void i845_update_wm(struct drm_crtc *unused_crtc)
b445e3b0 1798{
46ba614c 1799 struct drm_device *dev = unused_crtc->dev;
b445e3b0
ED
1800 struct drm_i915_private *dev_priv = dev->dev_private;
1801 struct drm_crtc *crtc;
241bfc38 1802 const struct drm_display_mode *adjusted_mode;
b445e3b0
ED
1803 uint32_t fwater_lo;
1804 int planea_wm;
1805
1806 crtc = single_enabled_crtc(dev);
1807 if (crtc == NULL)
1808 return;
1809
241bfc38
DL
1810 adjusted_mode = &to_intel_crtc(crtc)->config.adjusted_mode;
1811 planea_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
feb56b93 1812 &i845_wm_info,
b445e3b0 1813 dev_priv->display.get_fifo_size(dev, 0),
b9e0bda3 1814 4, latency_ns);
b445e3b0
ED
1815 fwater_lo = I915_READ(FW_BLC) & ~0xfff;
1816 fwater_lo |= (3<<8) | planea_wm;
1817
1818 DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d\n", planea_wm);
1819
1820 I915_WRITE(FW_BLC, fwater_lo);
1821}
1822
3658729a
VS
1823static uint32_t ilk_pipe_pixel_rate(struct drm_device *dev,
1824 struct drm_crtc *crtc)
801bcfff
PZ
1825{
1826 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
fd4daa9c 1827 uint32_t pixel_rate;
801bcfff 1828
241bfc38 1829 pixel_rate = intel_crtc->config.adjusted_mode.crtc_clock;
801bcfff
PZ
1830
1831 /* We only use IF-ID interlacing. If we ever use PF-ID we'll need to
1832 * adjust the pixel_rate here. */
1833
fd4daa9c 1834 if (intel_crtc->config.pch_pfit.enabled) {
801bcfff 1835 uint64_t pipe_w, pipe_h, pfit_w, pfit_h;
fd4daa9c 1836 uint32_t pfit_size = intel_crtc->config.pch_pfit.size;
801bcfff 1837
37327abd
VS
1838 pipe_w = intel_crtc->config.pipe_src_w;
1839 pipe_h = intel_crtc->config.pipe_src_h;
801bcfff
PZ
1840 pfit_w = (pfit_size >> 16) & 0xFFFF;
1841 pfit_h = pfit_size & 0xFFFF;
1842 if (pipe_w < pfit_w)
1843 pipe_w = pfit_w;
1844 if (pipe_h < pfit_h)
1845 pipe_h = pfit_h;
1846
1847 pixel_rate = div_u64((uint64_t) pixel_rate * pipe_w * pipe_h,
1848 pfit_w * pfit_h);
1849 }
1850
1851 return pixel_rate;
1852}
1853
37126462 1854/* latency must be in 0.1us units. */
23297044 1855static uint32_t ilk_wm_method1(uint32_t pixel_rate, uint8_t bytes_per_pixel,
801bcfff
PZ
1856 uint32_t latency)
1857{
1858 uint64_t ret;
1859
3312ba65
VS
1860 if (WARN(latency == 0, "Latency value missing\n"))
1861 return UINT_MAX;
1862
801bcfff
PZ
1863 ret = (uint64_t) pixel_rate * bytes_per_pixel * latency;
1864 ret = DIV_ROUND_UP_ULL(ret, 64 * 10000) + 2;
1865
1866 return ret;
1867}
1868
37126462 1869/* latency must be in 0.1us units. */
23297044 1870static uint32_t ilk_wm_method2(uint32_t pixel_rate, uint32_t pipe_htotal,
801bcfff
PZ
1871 uint32_t horiz_pixels, uint8_t bytes_per_pixel,
1872 uint32_t latency)
1873{
1874 uint32_t ret;
1875
3312ba65
VS
1876 if (WARN(latency == 0, "Latency value missing\n"))
1877 return UINT_MAX;
1878
801bcfff
PZ
1879 ret = (latency * pixel_rate) / (pipe_htotal * 10000);
1880 ret = (ret + 1) * horiz_pixels * bytes_per_pixel;
1881 ret = DIV_ROUND_UP(ret, 64) + 2;
1882 return ret;
1883}
1884
23297044 1885static uint32_t ilk_wm_fbc(uint32_t pri_val, uint32_t horiz_pixels,
cca32e9a
PZ
1886 uint8_t bytes_per_pixel)
1887{
1888 return DIV_ROUND_UP(pri_val * 64, horiz_pixels * bytes_per_pixel) + 2;
1889}
1890
820c1980 1891struct ilk_pipe_wm_parameters {
801bcfff 1892 bool active;
801bcfff
PZ
1893 uint32_t pipe_htotal;
1894 uint32_t pixel_rate;
c35426d2
VS
1895 struct intel_plane_wm_parameters pri;
1896 struct intel_plane_wm_parameters spr;
1897 struct intel_plane_wm_parameters cur;
801bcfff
PZ
1898};
1899
820c1980 1900struct ilk_wm_maximums {
cca32e9a
PZ
1901 uint16_t pri;
1902 uint16_t spr;
1903 uint16_t cur;
1904 uint16_t fbc;
1905};
1906
240264f4
VS
1907/* used in computing the new watermarks state */
1908struct intel_wm_config {
1909 unsigned int num_pipes_active;
1910 bool sprites_enabled;
1911 bool sprites_scaled;
240264f4
VS
1912};
1913
37126462
VS
1914/*
1915 * For both WM_PIPE and WM_LP.
1916 * mem_value must be in 0.1us units.
1917 */
820c1980 1918static uint32_t ilk_compute_pri_wm(const struct ilk_pipe_wm_parameters *params,
cca32e9a
PZ
1919 uint32_t mem_value,
1920 bool is_lp)
801bcfff 1921{
cca32e9a
PZ
1922 uint32_t method1, method2;
1923
c35426d2 1924 if (!params->active || !params->pri.enabled)
801bcfff
PZ
1925 return 0;
1926
23297044 1927 method1 = ilk_wm_method1(params->pixel_rate,
c35426d2 1928 params->pri.bytes_per_pixel,
cca32e9a
PZ
1929 mem_value);
1930
1931 if (!is_lp)
1932 return method1;
1933
23297044 1934 method2 = ilk_wm_method2(params->pixel_rate,
cca32e9a 1935 params->pipe_htotal,
c35426d2
VS
1936 params->pri.horiz_pixels,
1937 params->pri.bytes_per_pixel,
cca32e9a
PZ
1938 mem_value);
1939
1940 return min(method1, method2);
801bcfff
PZ
1941}
1942
37126462
VS
1943/*
1944 * For both WM_PIPE and WM_LP.
1945 * mem_value must be in 0.1us units.
1946 */
820c1980 1947static uint32_t ilk_compute_spr_wm(const struct ilk_pipe_wm_parameters *params,
801bcfff
PZ
1948 uint32_t mem_value)
1949{
1950 uint32_t method1, method2;
1951
c35426d2 1952 if (!params->active || !params->spr.enabled)
801bcfff
PZ
1953 return 0;
1954
23297044 1955 method1 = ilk_wm_method1(params->pixel_rate,
c35426d2 1956 params->spr.bytes_per_pixel,
801bcfff 1957 mem_value);
23297044 1958 method2 = ilk_wm_method2(params->pixel_rate,
801bcfff 1959 params->pipe_htotal,
c35426d2
VS
1960 params->spr.horiz_pixels,
1961 params->spr.bytes_per_pixel,
801bcfff
PZ
1962 mem_value);
1963 return min(method1, method2);
1964}
1965
37126462
VS
1966/*
1967 * For both WM_PIPE and WM_LP.
1968 * mem_value must be in 0.1us units.
1969 */
820c1980 1970static uint32_t ilk_compute_cur_wm(const struct ilk_pipe_wm_parameters *params,
801bcfff
PZ
1971 uint32_t mem_value)
1972{
c35426d2 1973 if (!params->active || !params->cur.enabled)
801bcfff
PZ
1974 return 0;
1975
23297044 1976 return ilk_wm_method2(params->pixel_rate,
801bcfff 1977 params->pipe_htotal,
c35426d2
VS
1978 params->cur.horiz_pixels,
1979 params->cur.bytes_per_pixel,
801bcfff
PZ
1980 mem_value);
1981}
1982
cca32e9a 1983/* Only for WM_LP. */
820c1980 1984static uint32_t ilk_compute_fbc_wm(const struct ilk_pipe_wm_parameters *params,
1fda9882 1985 uint32_t pri_val)
cca32e9a 1986{
c35426d2 1987 if (!params->active || !params->pri.enabled)
cca32e9a
PZ
1988 return 0;
1989
23297044 1990 return ilk_wm_fbc(pri_val,
c35426d2
VS
1991 params->pri.horiz_pixels,
1992 params->pri.bytes_per_pixel);
cca32e9a
PZ
1993}
1994
158ae64f
VS
1995static unsigned int ilk_display_fifo_size(const struct drm_device *dev)
1996{
416f4727
VS
1997 if (INTEL_INFO(dev)->gen >= 8)
1998 return 3072;
1999 else if (INTEL_INFO(dev)->gen >= 7)
158ae64f
VS
2000 return 768;
2001 else
2002 return 512;
2003}
2004
4e975081
VS
2005static unsigned int ilk_plane_wm_reg_max(const struct drm_device *dev,
2006 int level, bool is_sprite)
2007{
2008 if (INTEL_INFO(dev)->gen >= 8)
2009 /* BDW primary/sprite plane watermarks */
2010 return level == 0 ? 255 : 2047;
2011 else if (INTEL_INFO(dev)->gen >= 7)
2012 /* IVB/HSW primary/sprite plane watermarks */
2013 return level == 0 ? 127 : 1023;
2014 else if (!is_sprite)
2015 /* ILK/SNB primary plane watermarks */
2016 return level == 0 ? 127 : 511;
2017 else
2018 /* ILK/SNB sprite plane watermarks */
2019 return level == 0 ? 63 : 255;
2020}
2021
2022static unsigned int ilk_cursor_wm_reg_max(const struct drm_device *dev,
2023 int level)
2024{
2025 if (INTEL_INFO(dev)->gen >= 7)
2026 return level == 0 ? 63 : 255;
2027 else
2028 return level == 0 ? 31 : 63;
2029}
2030
2031static unsigned int ilk_fbc_wm_reg_max(const struct drm_device *dev)
2032{
2033 if (INTEL_INFO(dev)->gen >= 8)
2034 return 31;
2035 else
2036 return 15;
2037}
2038
158ae64f
VS
2039/* Calculate the maximum primary/sprite plane watermark */
2040static unsigned int ilk_plane_wm_max(const struct drm_device *dev,
2041 int level,
240264f4 2042 const struct intel_wm_config *config,
158ae64f
VS
2043 enum intel_ddb_partitioning ddb_partitioning,
2044 bool is_sprite)
2045{
2046 unsigned int fifo_size = ilk_display_fifo_size(dev);
158ae64f
VS
2047
2048 /* if sprites aren't enabled, sprites get nothing */
240264f4 2049 if (is_sprite && !config->sprites_enabled)
158ae64f
VS
2050 return 0;
2051
2052 /* HSW allows LP1+ watermarks even with multiple pipes */
240264f4 2053 if (level == 0 || config->num_pipes_active > 1) {
158ae64f
VS
2054 fifo_size /= INTEL_INFO(dev)->num_pipes;
2055
2056 /*
2057 * For some reason the non self refresh
2058 * FIFO size is only half of the self
2059 * refresh FIFO size on ILK/SNB.
2060 */
2061 if (INTEL_INFO(dev)->gen <= 6)
2062 fifo_size /= 2;
2063 }
2064
240264f4 2065 if (config->sprites_enabled) {
158ae64f
VS
2066 /* level 0 is always calculated with 1:1 split */
2067 if (level > 0 && ddb_partitioning == INTEL_DDB_PART_5_6) {
2068 if (is_sprite)
2069 fifo_size *= 5;
2070 fifo_size /= 6;
2071 } else {
2072 fifo_size /= 2;
2073 }
2074 }
2075
2076 /* clamp to max that the registers can hold */
4e975081 2077 return min(fifo_size, ilk_plane_wm_reg_max(dev, level, is_sprite));
158ae64f
VS
2078}
2079
2080/* Calculate the maximum cursor plane watermark */
2081static unsigned int ilk_cursor_wm_max(const struct drm_device *dev,
240264f4
VS
2082 int level,
2083 const struct intel_wm_config *config)
158ae64f
VS
2084{
2085 /* HSW LP1+ watermarks w/ multiple pipes */
240264f4 2086 if (level > 0 && config->num_pipes_active > 1)
158ae64f
VS
2087 return 64;
2088
2089 /* otherwise just report max that registers can hold */
4e975081 2090 return ilk_cursor_wm_reg_max(dev, level);
158ae64f
VS
2091}
2092
d34ff9c6 2093static void ilk_compute_wm_maximums(const struct drm_device *dev,
34982fe1
VS
2094 int level,
2095 const struct intel_wm_config *config,
2096 enum intel_ddb_partitioning ddb_partitioning,
820c1980 2097 struct ilk_wm_maximums *max)
158ae64f 2098{
240264f4
VS
2099 max->pri = ilk_plane_wm_max(dev, level, config, ddb_partitioning, false);
2100 max->spr = ilk_plane_wm_max(dev, level, config, ddb_partitioning, true);
2101 max->cur = ilk_cursor_wm_max(dev, level, config);
4e975081 2102 max->fbc = ilk_fbc_wm_reg_max(dev);
158ae64f
VS
2103}
2104
a3cb4048
VS
2105static void ilk_compute_wm_reg_maximums(struct drm_device *dev,
2106 int level,
2107 struct ilk_wm_maximums *max)
2108{
2109 max->pri = ilk_plane_wm_reg_max(dev, level, false);
2110 max->spr = ilk_plane_wm_reg_max(dev, level, true);
2111 max->cur = ilk_cursor_wm_reg_max(dev, level);
2112 max->fbc = ilk_fbc_wm_reg_max(dev);
2113}
2114
d9395655 2115static bool ilk_validate_wm_level(int level,
820c1980 2116 const struct ilk_wm_maximums *max,
d9395655 2117 struct intel_wm_level *result)
a9786a11
VS
2118{
2119 bool ret;
2120
2121 /* already determined to be invalid? */
2122 if (!result->enable)
2123 return false;
2124
2125 result->enable = result->pri_val <= max->pri &&
2126 result->spr_val <= max->spr &&
2127 result->cur_val <= max->cur;
2128
2129 ret = result->enable;
2130
2131 /*
2132 * HACK until we can pre-compute everything,
2133 * and thus fail gracefully if LP0 watermarks
2134 * are exceeded...
2135 */
2136 if (level == 0 && !result->enable) {
2137 if (result->pri_val > max->pri)
2138 DRM_DEBUG_KMS("Primary WM%d too large %u (max %u)\n",
2139 level, result->pri_val, max->pri);
2140 if (result->spr_val > max->spr)
2141 DRM_DEBUG_KMS("Sprite WM%d too large %u (max %u)\n",
2142 level, result->spr_val, max->spr);
2143 if (result->cur_val > max->cur)
2144 DRM_DEBUG_KMS("Cursor WM%d too large %u (max %u)\n",
2145 level, result->cur_val, max->cur);
2146
2147 result->pri_val = min_t(uint32_t, result->pri_val, max->pri);
2148 result->spr_val = min_t(uint32_t, result->spr_val, max->spr);
2149 result->cur_val = min_t(uint32_t, result->cur_val, max->cur);
2150 result->enable = true;
2151 }
2152
a9786a11
VS
2153 return ret;
2154}
2155
d34ff9c6 2156static void ilk_compute_wm_level(const struct drm_i915_private *dev_priv,
6f5ddd17 2157 int level,
820c1980 2158 const struct ilk_pipe_wm_parameters *p,
1fd527cc 2159 struct intel_wm_level *result)
6f5ddd17
VS
2160{
2161 uint16_t pri_latency = dev_priv->wm.pri_latency[level];
2162 uint16_t spr_latency = dev_priv->wm.spr_latency[level];
2163 uint16_t cur_latency = dev_priv->wm.cur_latency[level];
2164
2165 /* WM1+ latency values stored in 0.5us units */
2166 if (level > 0) {
2167 pri_latency *= 5;
2168 spr_latency *= 5;
2169 cur_latency *= 5;
2170 }
2171
2172 result->pri_val = ilk_compute_pri_wm(p, pri_latency, level);
2173 result->spr_val = ilk_compute_spr_wm(p, spr_latency);
2174 result->cur_val = ilk_compute_cur_wm(p, cur_latency);
2175 result->fbc_val = ilk_compute_fbc_wm(p, result->pri_val);
2176 result->enable = true;
2177}
2178
801bcfff
PZ
2179static uint32_t
2180hsw_compute_linetime_wm(struct drm_device *dev, struct drm_crtc *crtc)
1f8eeabf
ED
2181{
2182 struct drm_i915_private *dev_priv = dev->dev_private;
1011d8c4 2183 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1011d8c4 2184 struct drm_display_mode *mode = &intel_crtc->config.adjusted_mode;
85a02deb 2185 u32 linetime, ips_linetime;
1f8eeabf 2186
801bcfff
PZ
2187 if (!intel_crtc_active(crtc))
2188 return 0;
1011d8c4 2189
1f8eeabf
ED
2190 /* The WM are computed with base on how long it takes to fill a single
2191 * row at the given clock rate, multiplied by 8.
2192 * */
fec8cba3
JB
2193 linetime = DIV_ROUND_CLOSEST(mode->crtc_htotal * 1000 * 8,
2194 mode->crtc_clock);
2195 ips_linetime = DIV_ROUND_CLOSEST(mode->crtc_htotal * 1000 * 8,
85a02deb 2196 intel_ddi_get_cdclk_freq(dev_priv));
1f8eeabf 2197
801bcfff
PZ
2198 return PIPE_WM_LINETIME_IPS_LINETIME(ips_linetime) |
2199 PIPE_WM_LINETIME_TIME(linetime);
1f8eeabf
ED
2200}
2201
12b134df
VS
2202static void intel_read_wm_latency(struct drm_device *dev, uint16_t wm[5])
2203{
2204 struct drm_i915_private *dev_priv = dev->dev_private;
2205
a42a5719 2206 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
12b134df
VS
2207 uint64_t sskpd = I915_READ64(MCH_SSKPD);
2208
2209 wm[0] = (sskpd >> 56) & 0xFF;
2210 if (wm[0] == 0)
2211 wm[0] = sskpd & 0xF;
e5d5019e
VS
2212 wm[1] = (sskpd >> 4) & 0xFF;
2213 wm[2] = (sskpd >> 12) & 0xFF;
2214 wm[3] = (sskpd >> 20) & 0x1FF;
2215 wm[4] = (sskpd >> 32) & 0x1FF;
63cf9a13
VS
2216 } else if (INTEL_INFO(dev)->gen >= 6) {
2217 uint32_t sskpd = I915_READ(MCH_SSKPD);
2218
2219 wm[0] = (sskpd >> SSKPD_WM0_SHIFT) & SSKPD_WM_MASK;
2220 wm[1] = (sskpd >> SSKPD_WM1_SHIFT) & SSKPD_WM_MASK;
2221 wm[2] = (sskpd >> SSKPD_WM2_SHIFT) & SSKPD_WM_MASK;
2222 wm[3] = (sskpd >> SSKPD_WM3_SHIFT) & SSKPD_WM_MASK;
3a88d0ac
VS
2223 } else if (INTEL_INFO(dev)->gen >= 5) {
2224 uint32_t mltr = I915_READ(MLTR_ILK);
2225
2226 /* ILK primary LP0 latency is 700 ns */
2227 wm[0] = 7;
2228 wm[1] = (mltr >> MLTR_WM1_SHIFT) & ILK_SRLT_MASK;
2229 wm[2] = (mltr >> MLTR_WM2_SHIFT) & ILK_SRLT_MASK;
12b134df
VS
2230 }
2231}
2232
53615a5e
VS
2233static void intel_fixup_spr_wm_latency(struct drm_device *dev, uint16_t wm[5])
2234{
2235 /* ILK sprite LP0 latency is 1300 ns */
2236 if (INTEL_INFO(dev)->gen == 5)
2237 wm[0] = 13;
2238}
2239
2240static void intel_fixup_cur_wm_latency(struct drm_device *dev, uint16_t wm[5])
2241{
2242 /* ILK cursor LP0 latency is 1300 ns */
2243 if (INTEL_INFO(dev)->gen == 5)
2244 wm[0] = 13;
2245
2246 /* WaDoubleCursorLP3Latency:ivb */
2247 if (IS_IVYBRIDGE(dev))
2248 wm[3] *= 2;
2249}
2250
546c81fd 2251int ilk_wm_max_level(const struct drm_device *dev)
26ec971e 2252{
26ec971e 2253 /* how many WM levels are we expecting */
a42a5719 2254 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
ad0d6dc4 2255 return 4;
26ec971e 2256 else if (INTEL_INFO(dev)->gen >= 6)
ad0d6dc4 2257 return 3;
26ec971e 2258 else
ad0d6dc4
VS
2259 return 2;
2260}
ad0d6dc4
VS
2261static void intel_print_wm_latency(struct drm_device *dev,
2262 const char *name,
2263 const uint16_t wm[5])
2264{
2265 int level, max_level = ilk_wm_max_level(dev);
26ec971e
VS
2266
2267 for (level = 0; level <= max_level; level++) {
2268 unsigned int latency = wm[level];
2269
2270 if (latency == 0) {
2271 DRM_ERROR("%s WM%d latency not provided\n",
2272 name, level);
2273 continue;
2274 }
2275
2276 /* WM1+ latency values in 0.5us units */
2277 if (level > 0)
2278 latency *= 5;
2279
2280 DRM_DEBUG_KMS("%s WM%d latency %u (%u.%u usec)\n",
2281 name, level, wm[level],
2282 latency / 10, latency % 10);
2283 }
2284}
2285
e95a2f75
VS
2286static bool ilk_increase_wm_latency(struct drm_i915_private *dev_priv,
2287 uint16_t wm[5], uint16_t min)
2288{
2289 int level, max_level = ilk_wm_max_level(dev_priv->dev);
2290
2291 if (wm[0] >= min)
2292 return false;
2293
2294 wm[0] = max(wm[0], min);
2295 for (level = 1; level <= max_level; level++)
2296 wm[level] = max_t(uint16_t, wm[level], DIV_ROUND_UP(min, 5));
2297
2298 return true;
2299}
2300
2301static void snb_wm_latency_quirk(struct drm_device *dev)
2302{
2303 struct drm_i915_private *dev_priv = dev->dev_private;
2304 bool changed;
2305
2306 /*
2307 * The BIOS provided WM memory latency values are often
2308 * inadequate for high resolution displays. Adjust them.
2309 */
2310 changed = ilk_increase_wm_latency(dev_priv, dev_priv->wm.pri_latency, 12) |
2311 ilk_increase_wm_latency(dev_priv, dev_priv->wm.spr_latency, 12) |
2312 ilk_increase_wm_latency(dev_priv, dev_priv->wm.cur_latency, 12);
2313
2314 if (!changed)
2315 return;
2316
2317 DRM_DEBUG_KMS("WM latency values increased to avoid potential underruns\n");
2318 intel_print_wm_latency(dev, "Primary", dev_priv->wm.pri_latency);
2319 intel_print_wm_latency(dev, "Sprite", dev_priv->wm.spr_latency);
2320 intel_print_wm_latency(dev, "Cursor", dev_priv->wm.cur_latency);
2321}
2322
fa50ad61 2323static void ilk_setup_wm_latency(struct drm_device *dev)
53615a5e
VS
2324{
2325 struct drm_i915_private *dev_priv = dev->dev_private;
2326
2327 intel_read_wm_latency(dev, dev_priv->wm.pri_latency);
2328
2329 memcpy(dev_priv->wm.spr_latency, dev_priv->wm.pri_latency,
2330 sizeof(dev_priv->wm.pri_latency));
2331 memcpy(dev_priv->wm.cur_latency, dev_priv->wm.pri_latency,
2332 sizeof(dev_priv->wm.pri_latency));
2333
2334 intel_fixup_spr_wm_latency(dev, dev_priv->wm.spr_latency);
2335 intel_fixup_cur_wm_latency(dev, dev_priv->wm.cur_latency);
26ec971e
VS
2336
2337 intel_print_wm_latency(dev, "Primary", dev_priv->wm.pri_latency);
2338 intel_print_wm_latency(dev, "Sprite", dev_priv->wm.spr_latency);
2339 intel_print_wm_latency(dev, "Cursor", dev_priv->wm.cur_latency);
e95a2f75
VS
2340
2341 if (IS_GEN6(dev))
2342 snb_wm_latency_quirk(dev);
53615a5e
VS
2343}
2344
820c1980 2345static void ilk_compute_wm_parameters(struct drm_crtc *crtc,
2a44b76b 2346 struct ilk_pipe_wm_parameters *p)
1011d8c4 2347{
7c4a395f
VS
2348 struct drm_device *dev = crtc->dev;
2349 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2350 enum pipe pipe = intel_crtc->pipe;
7c4a395f 2351 struct drm_plane *plane;
1011d8c4 2352
2a44b76b
VS
2353 if (!intel_crtc_active(crtc))
2354 return;
801bcfff 2355
2a44b76b
VS
2356 p->active = true;
2357 p->pipe_htotal = intel_crtc->config.adjusted_mode.crtc_htotal;
2358 p->pixel_rate = ilk_pipe_pixel_rate(dev, crtc);
2359 p->pri.bytes_per_pixel = crtc->primary->fb->bits_per_pixel / 8;
2360 p->cur.bytes_per_pixel = 4;
2361 p->pri.horiz_pixels = intel_crtc->config.pipe_src_w;
2362 p->cur.horiz_pixels = intel_crtc->cursor_width;
2363 /* TODO: for now, assume primary and cursor planes are always enabled. */
2364 p->pri.enabled = true;
2365 p->cur.enabled = true;
7c4a395f 2366
af2b653b 2367 drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
801bcfff 2368 struct intel_plane *intel_plane = to_intel_plane(plane);
801bcfff 2369
2a44b76b 2370 if (intel_plane->pipe == pipe) {
7c4a395f 2371 p->spr = intel_plane->wm;
2a44b76b
VS
2372 break;
2373 }
2374 }
2375}
2376
2377static void ilk_compute_wm_config(struct drm_device *dev,
2378 struct intel_wm_config *config)
2379{
2380 struct intel_crtc *intel_crtc;
2381
2382 /* Compute the currently _active_ config */
d3fcc808 2383 for_each_intel_crtc(dev, intel_crtc) {
2a44b76b 2384 const struct intel_pipe_wm *wm = &intel_crtc->wm.active;
cca32e9a 2385
2a44b76b
VS
2386 if (!wm->pipe_enabled)
2387 continue;
cca32e9a 2388
2a44b76b
VS
2389 config->sprites_enabled |= wm->sprites_enabled;
2390 config->sprites_scaled |= wm->sprites_scaled;
2391 config->num_pipes_active++;
cca32e9a 2392 }
801bcfff
PZ
2393}
2394
0b2ae6d7
VS
2395/* Compute new watermarks for the pipe */
2396static bool intel_compute_pipe_wm(struct drm_crtc *crtc,
820c1980 2397 const struct ilk_pipe_wm_parameters *params,
0b2ae6d7
VS
2398 struct intel_pipe_wm *pipe_wm)
2399{
2400 struct drm_device *dev = crtc->dev;
d34ff9c6 2401 const struct drm_i915_private *dev_priv = dev->dev_private;
0b2ae6d7
VS
2402 int level, max_level = ilk_wm_max_level(dev);
2403 /* LP0 watermark maximums depend on this pipe alone */
2404 struct intel_wm_config config = {
2405 .num_pipes_active = 1,
2406 .sprites_enabled = params->spr.enabled,
2407 .sprites_scaled = params->spr.scaled,
2408 };
820c1980 2409 struct ilk_wm_maximums max;
0b2ae6d7 2410
2a44b76b
VS
2411 pipe_wm->pipe_enabled = params->active;
2412 pipe_wm->sprites_enabled = params->spr.enabled;
2413 pipe_wm->sprites_scaled = params->spr.scaled;
2414
7b39a0b7
VS
2415 /* ILK/SNB: LP2+ watermarks only w/o sprites */
2416 if (INTEL_INFO(dev)->gen <= 6 && params->spr.enabled)
2417 max_level = 1;
2418
2419 /* ILK/SNB/IVB: LP1+ watermarks only w/o scaling */
2420 if (params->spr.scaled)
2421 max_level = 0;
2422
a3cb4048 2423 ilk_compute_wm_level(dev_priv, 0, params, &pipe_wm->wm[0]);
0b2ae6d7 2424
a42a5719 2425 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
ce0e0713 2426 pipe_wm->linetime = hsw_compute_linetime_wm(dev, crtc);
0b2ae6d7 2427
a3cb4048
VS
2428 /* LP0 watermarks always use 1/2 DDB partitioning */
2429 ilk_compute_wm_maximums(dev, 0, &config, INTEL_DDB_PART_1_2, &max);
2430
0b2ae6d7 2431 /* At least LP0 must be valid */
a3cb4048
VS
2432 if (!ilk_validate_wm_level(0, &max, &pipe_wm->wm[0]))
2433 return false;
2434
2435 ilk_compute_wm_reg_maximums(dev, 1, &max);
2436
2437 for (level = 1; level <= max_level; level++) {
2438 struct intel_wm_level wm = {};
2439
2440 ilk_compute_wm_level(dev_priv, level, params, &wm);
2441
2442 /*
2443 * Disable any watermark level that exceeds the
2444 * register maximums since such watermarks are
2445 * always invalid.
2446 */
2447 if (!ilk_validate_wm_level(level, &max, &wm))
2448 break;
2449
2450 pipe_wm->wm[level] = wm;
2451 }
2452
2453 return true;
0b2ae6d7
VS
2454}
2455
2456/*
2457 * Merge the watermarks from all active pipes for a specific level.
2458 */
2459static void ilk_merge_wm_level(struct drm_device *dev,
2460 int level,
2461 struct intel_wm_level *ret_wm)
2462{
2463 const struct intel_crtc *intel_crtc;
2464
d52fea5b
VS
2465 ret_wm->enable = true;
2466
d3fcc808 2467 for_each_intel_crtc(dev, intel_crtc) {
fe392efd
VS
2468 const struct intel_pipe_wm *active = &intel_crtc->wm.active;
2469 const struct intel_wm_level *wm = &active->wm[level];
2470
2471 if (!active->pipe_enabled)
2472 continue;
0b2ae6d7 2473
d52fea5b
VS
2474 /*
2475 * The watermark values may have been used in the past,
2476 * so we must maintain them in the registers for some
2477 * time even if the level is now disabled.
2478 */
0b2ae6d7 2479 if (!wm->enable)
d52fea5b 2480 ret_wm->enable = false;
0b2ae6d7
VS
2481
2482 ret_wm->pri_val = max(ret_wm->pri_val, wm->pri_val);
2483 ret_wm->spr_val = max(ret_wm->spr_val, wm->spr_val);
2484 ret_wm->cur_val = max(ret_wm->cur_val, wm->cur_val);
2485 ret_wm->fbc_val = max(ret_wm->fbc_val, wm->fbc_val);
2486 }
0b2ae6d7
VS
2487}
2488
2489/*
2490 * Merge all low power watermarks for all active pipes.
2491 */
2492static void ilk_wm_merge(struct drm_device *dev,
0ba22e26 2493 const struct intel_wm_config *config,
820c1980 2494 const struct ilk_wm_maximums *max,
0b2ae6d7
VS
2495 struct intel_pipe_wm *merged)
2496{
2497 int level, max_level = ilk_wm_max_level(dev);
d52fea5b 2498 int last_enabled_level = max_level;
0b2ae6d7 2499
0ba22e26
VS
2500 /* ILK/SNB/IVB: LP1+ watermarks only w/ single pipe */
2501 if ((INTEL_INFO(dev)->gen <= 6 || IS_IVYBRIDGE(dev)) &&
2502 config->num_pipes_active > 1)
2503 return;
2504
6c8b6c28
VS
2505 /* ILK: FBC WM must be disabled always */
2506 merged->fbc_wm_enabled = INTEL_INFO(dev)->gen >= 6;
0b2ae6d7
VS
2507
2508 /* merge each WM1+ level */
2509 for (level = 1; level <= max_level; level++) {
2510 struct intel_wm_level *wm = &merged->wm[level];
2511
2512 ilk_merge_wm_level(dev, level, wm);
2513
d52fea5b
VS
2514 if (level > last_enabled_level)
2515 wm->enable = false;
2516 else if (!ilk_validate_wm_level(level, max, wm))
2517 /* make sure all following levels get disabled */
2518 last_enabled_level = level - 1;
0b2ae6d7
VS
2519
2520 /*
2521 * The spec says it is preferred to disable
2522 * FBC WMs instead of disabling a WM level.
2523 */
2524 if (wm->fbc_val > max->fbc) {
d52fea5b
VS
2525 if (wm->enable)
2526 merged->fbc_wm_enabled = false;
0b2ae6d7
VS
2527 wm->fbc_val = 0;
2528 }
2529 }
6c8b6c28
VS
2530
2531 /* ILK: LP2+ must be disabled when FBC WM is disabled but FBC enabled */
2532 /*
2533 * FIXME this is racy. FBC might get enabled later.
2534 * What we should check here is whether FBC can be
2535 * enabled sometime later.
2536 */
2537 if (IS_GEN5(dev) && !merged->fbc_wm_enabled && intel_fbc_enabled(dev)) {
2538 for (level = 2; level <= max_level; level++) {
2539 struct intel_wm_level *wm = &merged->wm[level];
2540
2541 wm->enable = false;
2542 }
2543 }
0b2ae6d7
VS
2544}
2545
b380ca3c
VS
2546static int ilk_wm_lp_to_level(int wm_lp, const struct intel_pipe_wm *pipe_wm)
2547{
2548 /* LP1,LP2,LP3 levels are either 1,2,3 or 1,3,4 */
2549 return wm_lp + (wm_lp >= 2 && pipe_wm->wm[4].enable);
2550}
2551
a68d68ee
VS
2552/* The value we need to program into the WM_LPx latency field */
2553static unsigned int ilk_wm_lp_latency(struct drm_device *dev, int level)
2554{
2555 struct drm_i915_private *dev_priv = dev->dev_private;
2556
a42a5719 2557 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
a68d68ee
VS
2558 return 2 * level;
2559 else
2560 return dev_priv->wm.pri_latency[level];
2561}
2562
820c1980 2563static void ilk_compute_wm_results(struct drm_device *dev,
0362c781 2564 const struct intel_pipe_wm *merged,
609cedef 2565 enum intel_ddb_partitioning partitioning,
820c1980 2566 struct ilk_wm_values *results)
801bcfff 2567{
0b2ae6d7
VS
2568 struct intel_crtc *intel_crtc;
2569 int level, wm_lp;
cca32e9a 2570
0362c781 2571 results->enable_fbc_wm = merged->fbc_wm_enabled;
609cedef 2572 results->partitioning = partitioning;
cca32e9a 2573
0b2ae6d7 2574 /* LP1+ register values */
cca32e9a 2575 for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
1fd527cc 2576 const struct intel_wm_level *r;
801bcfff 2577
b380ca3c 2578 level = ilk_wm_lp_to_level(wm_lp, merged);
0b2ae6d7 2579
0362c781 2580 r = &merged->wm[level];
cca32e9a 2581
d52fea5b
VS
2582 /*
2583 * Maintain the watermark values even if the level is
2584 * disabled. Doing otherwise could cause underruns.
2585 */
2586 results->wm_lp[wm_lp - 1] =
a68d68ee 2587 (ilk_wm_lp_latency(dev, level) << WM1_LP_LATENCY_SHIFT) |
416f4727
VS
2588 (r->pri_val << WM1_LP_SR_SHIFT) |
2589 r->cur_val;
2590
d52fea5b
VS
2591 if (r->enable)
2592 results->wm_lp[wm_lp - 1] |= WM1_LP_SR_EN;
2593
416f4727
VS
2594 if (INTEL_INFO(dev)->gen >= 8)
2595 results->wm_lp[wm_lp - 1] |=
2596 r->fbc_val << WM1_LP_FBC_SHIFT_BDW;
2597 else
2598 results->wm_lp[wm_lp - 1] |=
2599 r->fbc_val << WM1_LP_FBC_SHIFT;
2600
d52fea5b
VS
2601 /*
2602 * Always set WM1S_LP_EN when spr_val != 0, even if the
2603 * level is disabled. Doing otherwise could cause underruns.
2604 */
6cef2b8a
VS
2605 if (INTEL_INFO(dev)->gen <= 6 && r->spr_val) {
2606 WARN_ON(wm_lp != 1);
2607 results->wm_lp_spr[wm_lp - 1] = WM1S_LP_EN | r->spr_val;
2608 } else
2609 results->wm_lp_spr[wm_lp - 1] = r->spr_val;
cca32e9a 2610 }
801bcfff 2611
0b2ae6d7 2612 /* LP0 register values */
d3fcc808 2613 for_each_intel_crtc(dev, intel_crtc) {
0b2ae6d7
VS
2614 enum pipe pipe = intel_crtc->pipe;
2615 const struct intel_wm_level *r =
2616 &intel_crtc->wm.active.wm[0];
2617
2618 if (WARN_ON(!r->enable))
2619 continue;
2620
2621 results->wm_linetime[pipe] = intel_crtc->wm.active.linetime;
1011d8c4 2622
0b2ae6d7
VS
2623 results->wm_pipe[pipe] =
2624 (r->pri_val << WM0_PIPE_PLANE_SHIFT) |
2625 (r->spr_val << WM0_PIPE_SPRITE_SHIFT) |
2626 r->cur_val;
801bcfff
PZ
2627 }
2628}
2629
861f3389
PZ
2630/* Find the result with the highest level enabled. Check for enable_fbc_wm in
2631 * case both are at the same level. Prefer r1 in case they're the same. */
820c1980 2632static struct intel_pipe_wm *ilk_find_best_result(struct drm_device *dev,
198a1e9b
VS
2633 struct intel_pipe_wm *r1,
2634 struct intel_pipe_wm *r2)
861f3389 2635{
198a1e9b
VS
2636 int level, max_level = ilk_wm_max_level(dev);
2637 int level1 = 0, level2 = 0;
861f3389 2638
198a1e9b
VS
2639 for (level = 1; level <= max_level; level++) {
2640 if (r1->wm[level].enable)
2641 level1 = level;
2642 if (r2->wm[level].enable)
2643 level2 = level;
861f3389
PZ
2644 }
2645
198a1e9b
VS
2646 if (level1 == level2) {
2647 if (r2->fbc_wm_enabled && !r1->fbc_wm_enabled)
861f3389
PZ
2648 return r2;
2649 else
2650 return r1;
198a1e9b 2651 } else if (level1 > level2) {
861f3389
PZ
2652 return r1;
2653 } else {
2654 return r2;
2655 }
2656}
2657
49a687c4
VS
2658/* dirty bits used to track which watermarks need changes */
2659#define WM_DIRTY_PIPE(pipe) (1 << (pipe))
2660#define WM_DIRTY_LINETIME(pipe) (1 << (8 + (pipe)))
2661#define WM_DIRTY_LP(wm_lp) (1 << (15 + (wm_lp)))
2662#define WM_DIRTY_LP_ALL (WM_DIRTY_LP(1) | WM_DIRTY_LP(2) | WM_DIRTY_LP(3))
2663#define WM_DIRTY_FBC (1 << 24)
2664#define WM_DIRTY_DDB (1 << 25)
2665
055e393f 2666static unsigned int ilk_compute_wm_dirty(struct drm_i915_private *dev_priv,
820c1980
ID
2667 const struct ilk_wm_values *old,
2668 const struct ilk_wm_values *new)
49a687c4
VS
2669{
2670 unsigned int dirty = 0;
2671 enum pipe pipe;
2672 int wm_lp;
2673
055e393f 2674 for_each_pipe(dev_priv, pipe) {
49a687c4
VS
2675 if (old->wm_linetime[pipe] != new->wm_linetime[pipe]) {
2676 dirty |= WM_DIRTY_LINETIME(pipe);
2677 /* Must disable LP1+ watermarks too */
2678 dirty |= WM_DIRTY_LP_ALL;
2679 }
2680
2681 if (old->wm_pipe[pipe] != new->wm_pipe[pipe]) {
2682 dirty |= WM_DIRTY_PIPE(pipe);
2683 /* Must disable LP1+ watermarks too */
2684 dirty |= WM_DIRTY_LP_ALL;
2685 }
2686 }
2687
2688 if (old->enable_fbc_wm != new->enable_fbc_wm) {
2689 dirty |= WM_DIRTY_FBC;
2690 /* Must disable LP1+ watermarks too */
2691 dirty |= WM_DIRTY_LP_ALL;
2692 }
2693
2694 if (old->partitioning != new->partitioning) {
2695 dirty |= WM_DIRTY_DDB;
2696 /* Must disable LP1+ watermarks too */
2697 dirty |= WM_DIRTY_LP_ALL;
2698 }
2699
2700 /* LP1+ watermarks already deemed dirty, no need to continue */
2701 if (dirty & WM_DIRTY_LP_ALL)
2702 return dirty;
2703
2704 /* Find the lowest numbered LP1+ watermark in need of an update... */
2705 for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
2706 if (old->wm_lp[wm_lp - 1] != new->wm_lp[wm_lp - 1] ||
2707 old->wm_lp_spr[wm_lp - 1] != new->wm_lp_spr[wm_lp - 1])
2708 break;
2709 }
2710
2711 /* ...and mark it and all higher numbered LP1+ watermarks as dirty */
2712 for (; wm_lp <= 3; wm_lp++)
2713 dirty |= WM_DIRTY_LP(wm_lp);
2714
2715 return dirty;
2716}
2717
8553c18e
VS
2718static bool _ilk_disable_lp_wm(struct drm_i915_private *dev_priv,
2719 unsigned int dirty)
801bcfff 2720{
820c1980 2721 struct ilk_wm_values *previous = &dev_priv->wm.hw;
8553c18e 2722 bool changed = false;
801bcfff 2723
facd619b
VS
2724 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] & WM1_LP_SR_EN) {
2725 previous->wm_lp[2] &= ~WM1_LP_SR_EN;
2726 I915_WRITE(WM3_LP_ILK, previous->wm_lp[2]);
8553c18e 2727 changed = true;
facd619b
VS
2728 }
2729 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] & WM1_LP_SR_EN) {
2730 previous->wm_lp[1] &= ~WM1_LP_SR_EN;
2731 I915_WRITE(WM2_LP_ILK, previous->wm_lp[1]);
8553c18e 2732 changed = true;
facd619b
VS
2733 }
2734 if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] & WM1_LP_SR_EN) {
2735 previous->wm_lp[0] &= ~WM1_LP_SR_EN;
2736 I915_WRITE(WM1_LP_ILK, previous->wm_lp[0]);
8553c18e 2737 changed = true;
facd619b 2738 }
801bcfff 2739
facd619b
VS
2740 /*
2741 * Don't touch WM1S_LP_EN here.
2742 * Doing so could cause underruns.
2743 */
6cef2b8a 2744
8553c18e
VS
2745 return changed;
2746}
2747
2748/*
2749 * The spec says we shouldn't write when we don't need, because every write
2750 * causes WMs to be re-evaluated, expending some power.
2751 */
820c1980
ID
2752static void ilk_write_wm_values(struct drm_i915_private *dev_priv,
2753 struct ilk_wm_values *results)
8553c18e
VS
2754{
2755 struct drm_device *dev = dev_priv->dev;
820c1980 2756 struct ilk_wm_values *previous = &dev_priv->wm.hw;
8553c18e
VS
2757 unsigned int dirty;
2758 uint32_t val;
2759
055e393f 2760 dirty = ilk_compute_wm_dirty(dev_priv, previous, results);
8553c18e
VS
2761 if (!dirty)
2762 return;
2763
2764 _ilk_disable_lp_wm(dev_priv, dirty);
2765
49a687c4 2766 if (dirty & WM_DIRTY_PIPE(PIPE_A))
801bcfff 2767 I915_WRITE(WM0_PIPEA_ILK, results->wm_pipe[0]);
49a687c4 2768 if (dirty & WM_DIRTY_PIPE(PIPE_B))
801bcfff 2769 I915_WRITE(WM0_PIPEB_ILK, results->wm_pipe[1]);
49a687c4 2770 if (dirty & WM_DIRTY_PIPE(PIPE_C))
801bcfff
PZ
2771 I915_WRITE(WM0_PIPEC_IVB, results->wm_pipe[2]);
2772
49a687c4 2773 if (dirty & WM_DIRTY_LINETIME(PIPE_A))
801bcfff 2774 I915_WRITE(PIPE_WM_LINETIME(PIPE_A), results->wm_linetime[0]);
49a687c4 2775 if (dirty & WM_DIRTY_LINETIME(PIPE_B))
801bcfff 2776 I915_WRITE(PIPE_WM_LINETIME(PIPE_B), results->wm_linetime[1]);
49a687c4 2777 if (dirty & WM_DIRTY_LINETIME(PIPE_C))
801bcfff
PZ
2778 I915_WRITE(PIPE_WM_LINETIME(PIPE_C), results->wm_linetime[2]);
2779
49a687c4 2780 if (dirty & WM_DIRTY_DDB) {
a42a5719 2781 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
ac9545fd
VS
2782 val = I915_READ(WM_MISC);
2783 if (results->partitioning == INTEL_DDB_PART_1_2)
2784 val &= ~WM_MISC_DATA_PARTITION_5_6;
2785 else
2786 val |= WM_MISC_DATA_PARTITION_5_6;
2787 I915_WRITE(WM_MISC, val);
2788 } else {
2789 val = I915_READ(DISP_ARB_CTL2);
2790 if (results->partitioning == INTEL_DDB_PART_1_2)
2791 val &= ~DISP_DATA_PARTITION_5_6;
2792 else
2793 val |= DISP_DATA_PARTITION_5_6;
2794 I915_WRITE(DISP_ARB_CTL2, val);
2795 }
1011d8c4
PZ
2796 }
2797
49a687c4 2798 if (dirty & WM_DIRTY_FBC) {
cca32e9a
PZ
2799 val = I915_READ(DISP_ARB_CTL);
2800 if (results->enable_fbc_wm)
2801 val &= ~DISP_FBC_WM_DIS;
2802 else
2803 val |= DISP_FBC_WM_DIS;
2804 I915_WRITE(DISP_ARB_CTL, val);
2805 }
2806
954911eb
ID
2807 if (dirty & WM_DIRTY_LP(1) &&
2808 previous->wm_lp_spr[0] != results->wm_lp_spr[0])
2809 I915_WRITE(WM1S_LP_ILK, results->wm_lp_spr[0]);
2810
2811 if (INTEL_INFO(dev)->gen >= 7) {
6cef2b8a
VS
2812 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp_spr[1] != results->wm_lp_spr[1])
2813 I915_WRITE(WM2S_LP_IVB, results->wm_lp_spr[1]);
2814 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp_spr[2] != results->wm_lp_spr[2])
2815 I915_WRITE(WM3S_LP_IVB, results->wm_lp_spr[2]);
2816 }
801bcfff 2817
facd619b 2818 if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] != results->wm_lp[0])
801bcfff 2819 I915_WRITE(WM1_LP_ILK, results->wm_lp[0]);
facd619b 2820 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] != results->wm_lp[1])
801bcfff 2821 I915_WRITE(WM2_LP_ILK, results->wm_lp[1]);
facd619b 2822 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] != results->wm_lp[2])
801bcfff 2823 I915_WRITE(WM3_LP_ILK, results->wm_lp[2]);
609cedef
VS
2824
2825 dev_priv->wm.hw = *results;
801bcfff
PZ
2826}
2827
8553c18e
VS
2828static bool ilk_disable_lp_wm(struct drm_device *dev)
2829{
2830 struct drm_i915_private *dev_priv = dev->dev_private;
2831
2832 return _ilk_disable_lp_wm(dev_priv, WM_DIRTY_LP_ALL);
2833}
2834
820c1980 2835static void ilk_update_wm(struct drm_crtc *crtc)
801bcfff 2836{
7c4a395f 2837 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
46ba614c 2838 struct drm_device *dev = crtc->dev;
801bcfff 2839 struct drm_i915_private *dev_priv = dev->dev_private;
820c1980
ID
2840 struct ilk_wm_maximums max;
2841 struct ilk_pipe_wm_parameters params = {};
2842 struct ilk_wm_values results = {};
77c122bc 2843 enum intel_ddb_partitioning partitioning;
7c4a395f 2844 struct intel_pipe_wm pipe_wm = {};
198a1e9b 2845 struct intel_pipe_wm lp_wm_1_2 = {}, lp_wm_5_6 = {}, *best_lp_wm;
a485bfb8 2846 struct intel_wm_config config = {};
7c4a395f 2847
2a44b76b 2848 ilk_compute_wm_parameters(crtc, &params);
7c4a395f
VS
2849
2850 intel_compute_pipe_wm(crtc, &params, &pipe_wm);
2851
2852 if (!memcmp(&intel_crtc->wm.active, &pipe_wm, sizeof(pipe_wm)))
2853 return;
861f3389 2854
7c4a395f 2855 intel_crtc->wm.active = pipe_wm;
861f3389 2856
2a44b76b
VS
2857 ilk_compute_wm_config(dev, &config);
2858
34982fe1 2859 ilk_compute_wm_maximums(dev, 1, &config, INTEL_DDB_PART_1_2, &max);
0ba22e26 2860 ilk_wm_merge(dev, &config, &max, &lp_wm_1_2);
a485bfb8
VS
2861
2862 /* 5/6 split only in single pipe config on IVB+ */
ec98c8d1
VS
2863 if (INTEL_INFO(dev)->gen >= 7 &&
2864 config.num_pipes_active == 1 && config.sprites_enabled) {
34982fe1 2865 ilk_compute_wm_maximums(dev, 1, &config, INTEL_DDB_PART_5_6, &max);
0ba22e26 2866 ilk_wm_merge(dev, &config, &max, &lp_wm_5_6);
0362c781 2867
820c1980 2868 best_lp_wm = ilk_find_best_result(dev, &lp_wm_1_2, &lp_wm_5_6);
861f3389 2869 } else {
198a1e9b 2870 best_lp_wm = &lp_wm_1_2;
861f3389
PZ
2871 }
2872
198a1e9b 2873 partitioning = (best_lp_wm == &lp_wm_1_2) ?
77c122bc 2874 INTEL_DDB_PART_1_2 : INTEL_DDB_PART_5_6;
801bcfff 2875
820c1980 2876 ilk_compute_wm_results(dev, best_lp_wm, partitioning, &results);
609cedef 2877
820c1980 2878 ilk_write_wm_values(dev_priv, &results);
1011d8c4
PZ
2879}
2880
ed57cb8a
DL
2881static void
2882ilk_update_sprite_wm(struct drm_plane *plane,
2883 struct drm_crtc *crtc,
2884 uint32_t sprite_width, uint32_t sprite_height,
2885 int pixel_size, bool enabled, bool scaled)
526682e9 2886{
8553c18e 2887 struct drm_device *dev = plane->dev;
adf3d35e 2888 struct intel_plane *intel_plane = to_intel_plane(plane);
526682e9 2889
adf3d35e
VS
2890 intel_plane->wm.enabled = enabled;
2891 intel_plane->wm.scaled = scaled;
2892 intel_plane->wm.horiz_pixels = sprite_width;
ed57cb8a 2893 intel_plane->wm.vert_pixels = sprite_width;
adf3d35e 2894 intel_plane->wm.bytes_per_pixel = pixel_size;
526682e9 2895
8553c18e
VS
2896 /*
2897 * IVB workaround: must disable low power watermarks for at least
2898 * one frame before enabling scaling. LP watermarks can be re-enabled
2899 * when scaling is disabled.
2900 *
2901 * WaCxSRDisabledForSpriteScaling:ivb
2902 */
2903 if (IS_IVYBRIDGE(dev) && scaled && ilk_disable_lp_wm(dev))
2904 intel_wait_for_vblank(dev, intel_plane->pipe);
2905
820c1980 2906 ilk_update_wm(crtc);
526682e9
PZ
2907}
2908
243e6a44
VS
2909static void ilk_pipe_wm_get_hw_state(struct drm_crtc *crtc)
2910{
2911 struct drm_device *dev = crtc->dev;
2912 struct drm_i915_private *dev_priv = dev->dev_private;
820c1980 2913 struct ilk_wm_values *hw = &dev_priv->wm.hw;
243e6a44
VS
2914 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2915 struct intel_pipe_wm *active = &intel_crtc->wm.active;
2916 enum pipe pipe = intel_crtc->pipe;
2917 static const unsigned int wm0_pipe_reg[] = {
2918 [PIPE_A] = WM0_PIPEA_ILK,
2919 [PIPE_B] = WM0_PIPEB_ILK,
2920 [PIPE_C] = WM0_PIPEC_IVB,
2921 };
2922
2923 hw->wm_pipe[pipe] = I915_READ(wm0_pipe_reg[pipe]);
a42a5719 2924 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
ce0e0713 2925 hw->wm_linetime[pipe] = I915_READ(PIPE_WM_LINETIME(pipe));
243e6a44 2926
2a44b76b
VS
2927 active->pipe_enabled = intel_crtc_active(crtc);
2928
2929 if (active->pipe_enabled) {
243e6a44
VS
2930 u32 tmp = hw->wm_pipe[pipe];
2931
2932 /*
2933 * For active pipes LP0 watermark is marked as
2934 * enabled, and LP1+ watermaks as disabled since
2935 * we can't really reverse compute them in case
2936 * multiple pipes are active.
2937 */
2938 active->wm[0].enable = true;
2939 active->wm[0].pri_val = (tmp & WM0_PIPE_PLANE_MASK) >> WM0_PIPE_PLANE_SHIFT;
2940 active->wm[0].spr_val = (tmp & WM0_PIPE_SPRITE_MASK) >> WM0_PIPE_SPRITE_SHIFT;
2941 active->wm[0].cur_val = tmp & WM0_PIPE_CURSOR_MASK;
2942 active->linetime = hw->wm_linetime[pipe];
2943 } else {
2944 int level, max_level = ilk_wm_max_level(dev);
2945
2946 /*
2947 * For inactive pipes, all watermark levels
2948 * should be marked as enabled but zeroed,
2949 * which is what we'd compute them to.
2950 */
2951 for (level = 0; level <= max_level; level++)
2952 active->wm[level].enable = true;
2953 }
2954}
2955
2956void ilk_wm_get_hw_state(struct drm_device *dev)
2957{
2958 struct drm_i915_private *dev_priv = dev->dev_private;
820c1980 2959 struct ilk_wm_values *hw = &dev_priv->wm.hw;
243e6a44
VS
2960 struct drm_crtc *crtc;
2961
70e1e0ec 2962 for_each_crtc(dev, crtc)
243e6a44
VS
2963 ilk_pipe_wm_get_hw_state(crtc);
2964
2965 hw->wm_lp[0] = I915_READ(WM1_LP_ILK);
2966 hw->wm_lp[1] = I915_READ(WM2_LP_ILK);
2967 hw->wm_lp[2] = I915_READ(WM3_LP_ILK);
2968
2969 hw->wm_lp_spr[0] = I915_READ(WM1S_LP_ILK);
cfa7698b
VS
2970 if (INTEL_INFO(dev)->gen >= 7) {
2971 hw->wm_lp_spr[1] = I915_READ(WM2S_LP_IVB);
2972 hw->wm_lp_spr[2] = I915_READ(WM3S_LP_IVB);
2973 }
243e6a44 2974
a42a5719 2975 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
ac9545fd
VS
2976 hw->partitioning = (I915_READ(WM_MISC) & WM_MISC_DATA_PARTITION_5_6) ?
2977 INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
2978 else if (IS_IVYBRIDGE(dev))
2979 hw->partitioning = (I915_READ(DISP_ARB_CTL2) & DISP_DATA_PARTITION_5_6) ?
2980 INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
243e6a44
VS
2981
2982 hw->enable_fbc_wm =
2983 !(I915_READ(DISP_ARB_CTL) & DISP_FBC_WM_DIS);
2984}
2985
b445e3b0
ED
2986/**
2987 * intel_update_watermarks - update FIFO watermark values based on current modes
2988 *
2989 * Calculate watermark values for the various WM regs based on current mode
2990 * and plane configuration.
2991 *
2992 * There are several cases to deal with here:
2993 * - normal (i.e. non-self-refresh)
2994 * - self-refresh (SR) mode
2995 * - lines are large relative to FIFO size (buffer can hold up to 2)
2996 * - lines are small relative to FIFO size (buffer can hold more than 2
2997 * lines), so need to account for TLB latency
2998 *
2999 * The normal calculation is:
3000 * watermark = dotclock * bytes per pixel * latency
3001 * where latency is platform & configuration dependent (we assume pessimal
3002 * values here).
3003 *
3004 * The SR calculation is:
3005 * watermark = (trunc(latency/line time)+1) * surface width *
3006 * bytes per pixel
3007 * where
3008 * line time = htotal / dotclock
3009 * surface width = hdisplay for normal plane and 64 for cursor
3010 * and latency is assumed to be high, as above.
3011 *
3012 * The final value programmed to the register should always be rounded up,
3013 * and include an extra 2 entries to account for clock crossings.
3014 *
3015 * We don't use the sprite, so we can ignore that. And on Crestline we have
3016 * to set the non-SR watermarks to 8.
3017 */
46ba614c 3018void intel_update_watermarks(struct drm_crtc *crtc)
b445e3b0 3019{
46ba614c 3020 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
b445e3b0
ED
3021
3022 if (dev_priv->display.update_wm)
46ba614c 3023 dev_priv->display.update_wm(crtc);
b445e3b0
ED
3024}
3025
adf3d35e
VS
3026void intel_update_sprite_watermarks(struct drm_plane *plane,
3027 struct drm_crtc *crtc,
ed57cb8a
DL
3028 uint32_t sprite_width,
3029 uint32_t sprite_height,
3030 int pixel_size,
39db4a4d 3031 bool enabled, bool scaled)
b445e3b0 3032{
adf3d35e 3033 struct drm_i915_private *dev_priv = plane->dev->dev_private;
b445e3b0
ED
3034
3035 if (dev_priv->display.update_sprite_wm)
ed57cb8a
DL
3036 dev_priv->display.update_sprite_wm(plane, crtc,
3037 sprite_width, sprite_height,
39db4a4d 3038 pixel_size, enabled, scaled);
b445e3b0
ED
3039}
3040
2b4e57bd
ED
3041static struct drm_i915_gem_object *
3042intel_alloc_context_page(struct drm_device *dev)
3043{
3044 struct drm_i915_gem_object *ctx;
3045 int ret;
3046
3047 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
3048
3049 ctx = i915_gem_alloc_object(dev, 4096);
3050 if (!ctx) {
3051 DRM_DEBUG("failed to alloc power context, RC6 disabled\n");
3052 return NULL;
3053 }
3054
c69766f2 3055 ret = i915_gem_obj_ggtt_pin(ctx, 4096, 0);
2b4e57bd
ED
3056 if (ret) {
3057 DRM_ERROR("failed to pin power context: %d\n", ret);
3058 goto err_unref;
3059 }
3060
3061 ret = i915_gem_object_set_to_gtt_domain(ctx, 1);
3062 if (ret) {
3063 DRM_ERROR("failed to set-domain on power context: %d\n", ret);
3064 goto err_unpin;
3065 }
3066
3067 return ctx;
3068
3069err_unpin:
d7f46fc4 3070 i915_gem_object_ggtt_unpin(ctx);
2b4e57bd
ED
3071err_unref:
3072 drm_gem_object_unreference(&ctx->base);
2b4e57bd
ED
3073 return NULL;
3074}
3075
9270388e
DV
3076/**
3077 * Lock protecting IPS related data structures
9270388e
DV
3078 */
3079DEFINE_SPINLOCK(mchdev_lock);
3080
3081/* Global for IPS driver to get at the current i915 device. Protected by
3082 * mchdev_lock. */
3083static struct drm_i915_private *i915_mch_dev;
3084
2b4e57bd
ED
3085bool ironlake_set_drps(struct drm_device *dev, u8 val)
3086{
3087 struct drm_i915_private *dev_priv = dev->dev_private;
3088 u16 rgvswctl;
3089
9270388e
DV
3090 assert_spin_locked(&mchdev_lock);
3091
2b4e57bd
ED
3092 rgvswctl = I915_READ16(MEMSWCTL);
3093 if (rgvswctl & MEMCTL_CMD_STS) {
3094 DRM_DEBUG("gpu busy, RCS change rejected\n");
3095 return false; /* still busy with another command */
3096 }
3097
3098 rgvswctl = (MEMCTL_CMD_CHFREQ << MEMCTL_CMD_SHIFT) |
3099 (val << MEMCTL_FREQ_SHIFT) | MEMCTL_SFCAVM;
3100 I915_WRITE16(MEMSWCTL, rgvswctl);
3101 POSTING_READ16(MEMSWCTL);
3102
3103 rgvswctl |= MEMCTL_CMD_STS;
3104 I915_WRITE16(MEMSWCTL, rgvswctl);
3105
3106 return true;
3107}
3108
8090c6b9 3109static void ironlake_enable_drps(struct drm_device *dev)
2b4e57bd
ED
3110{
3111 struct drm_i915_private *dev_priv = dev->dev_private;
3112 u32 rgvmodectl = I915_READ(MEMMODECTL);
3113 u8 fmax, fmin, fstart, vstart;
3114
9270388e
DV
3115 spin_lock_irq(&mchdev_lock);
3116
2b4e57bd
ED
3117 /* Enable temp reporting */
3118 I915_WRITE16(PMMISC, I915_READ(PMMISC) | MCPPCE_EN);
3119 I915_WRITE16(TSC1, I915_READ(TSC1) | TSE);
3120
3121 /* 100ms RC evaluation intervals */
3122 I915_WRITE(RCUPEI, 100000);
3123 I915_WRITE(RCDNEI, 100000);
3124
3125 /* Set max/min thresholds to 90ms and 80ms respectively */
3126 I915_WRITE(RCBMAXAVG, 90000);
3127 I915_WRITE(RCBMINAVG, 80000);
3128
3129 I915_WRITE(MEMIHYST, 1);
3130
3131 /* Set up min, max, and cur for interrupt handling */
3132 fmax = (rgvmodectl & MEMMODE_FMAX_MASK) >> MEMMODE_FMAX_SHIFT;
3133 fmin = (rgvmodectl & MEMMODE_FMIN_MASK);
3134 fstart = (rgvmodectl & MEMMODE_FSTART_MASK) >>
3135 MEMMODE_FSTART_SHIFT;
3136
3137 vstart = (I915_READ(PXVFREQ_BASE + (fstart * 4)) & PXVFREQ_PX_MASK) >>
3138 PXVFREQ_PX_SHIFT;
3139
20e4d407
DV
3140 dev_priv->ips.fmax = fmax; /* IPS callback will increase this */
3141 dev_priv->ips.fstart = fstart;
2b4e57bd 3142
20e4d407
DV
3143 dev_priv->ips.max_delay = fstart;
3144 dev_priv->ips.min_delay = fmin;
3145 dev_priv->ips.cur_delay = fstart;
2b4e57bd
ED
3146
3147 DRM_DEBUG_DRIVER("fmax: %d, fmin: %d, fstart: %d\n",
3148 fmax, fmin, fstart);
3149
3150 I915_WRITE(MEMINTREN, MEMINT_CX_SUPR_EN | MEMINT_EVAL_CHG_EN);
3151
3152 /*
3153 * Interrupts will be enabled in ironlake_irq_postinstall
3154 */
3155
3156 I915_WRITE(VIDSTART, vstart);
3157 POSTING_READ(VIDSTART);
3158
3159 rgvmodectl |= MEMMODE_SWMODE_EN;
3160 I915_WRITE(MEMMODECTL, rgvmodectl);
3161
9270388e 3162 if (wait_for_atomic((I915_READ(MEMSWCTL) & MEMCTL_CMD_STS) == 0, 10))
2b4e57bd 3163 DRM_ERROR("stuck trying to change perf mode\n");
9270388e 3164 mdelay(1);
2b4e57bd
ED
3165
3166 ironlake_set_drps(dev, fstart);
3167
20e4d407 3168 dev_priv->ips.last_count1 = I915_READ(0x112e4) + I915_READ(0x112e8) +
2b4e57bd 3169 I915_READ(0x112e0);
20e4d407
DV
3170 dev_priv->ips.last_time1 = jiffies_to_msecs(jiffies);
3171 dev_priv->ips.last_count2 = I915_READ(0x112f4);
5ed0bdf2 3172 dev_priv->ips.last_time2 = ktime_get_raw_ns();
9270388e
DV
3173
3174 spin_unlock_irq(&mchdev_lock);
2b4e57bd
ED
3175}
3176
8090c6b9 3177static void ironlake_disable_drps(struct drm_device *dev)
2b4e57bd
ED
3178{
3179 struct drm_i915_private *dev_priv = dev->dev_private;
9270388e
DV
3180 u16 rgvswctl;
3181
3182 spin_lock_irq(&mchdev_lock);
3183
3184 rgvswctl = I915_READ16(MEMSWCTL);
2b4e57bd
ED
3185
3186 /* Ack interrupts, disable EFC interrupt */
3187 I915_WRITE(MEMINTREN, I915_READ(MEMINTREN) & ~MEMINT_EVAL_CHG_EN);
3188 I915_WRITE(MEMINTRSTS, MEMINT_EVAL_CHG);
3189 I915_WRITE(DEIER, I915_READ(DEIER) & ~DE_PCU_EVENT);
3190 I915_WRITE(DEIIR, DE_PCU_EVENT);
3191 I915_WRITE(DEIMR, I915_READ(DEIMR) | DE_PCU_EVENT);
3192
3193 /* Go back to the starting frequency */
20e4d407 3194 ironlake_set_drps(dev, dev_priv->ips.fstart);
9270388e 3195 mdelay(1);
2b4e57bd
ED
3196 rgvswctl |= MEMCTL_CMD_STS;
3197 I915_WRITE(MEMSWCTL, rgvswctl);
9270388e 3198 mdelay(1);
2b4e57bd 3199
9270388e 3200 spin_unlock_irq(&mchdev_lock);
2b4e57bd
ED
3201}
3202
acbe9475
DV
3203/* There's a funny hw issue where the hw returns all 0 when reading from
3204 * GEN6_RP_INTERRUPT_LIMITS. Hence we always need to compute the desired value
3205 * ourselves, instead of doing a rmw cycle (which might result in us clearing
3206 * all limits and the gpu stuck at whatever frequency it is at atm).
3207 */
6917c7b9 3208static u32 gen6_rps_limits(struct drm_i915_private *dev_priv, u8 val)
2b4e57bd 3209{
7b9e0ae6 3210 u32 limits;
2b4e57bd 3211
20b46e59
DV
3212 /* Only set the down limit when we've reached the lowest level to avoid
3213 * getting more interrupts, otherwise leave this clear. This prevents a
3214 * race in the hw when coming out of rc6: There's a tiny window where
3215 * the hw runs at the minimal clock before selecting the desired
3216 * frequency, if the down threshold expires in that window we will not
3217 * receive a down interrupt. */
b39fb297
BW
3218 limits = dev_priv->rps.max_freq_softlimit << 24;
3219 if (val <= dev_priv->rps.min_freq_softlimit)
3220 limits |= dev_priv->rps.min_freq_softlimit << 16;
20b46e59
DV
3221
3222 return limits;
3223}
3224
dd75fdc8
CW
3225static void gen6_set_rps_thresholds(struct drm_i915_private *dev_priv, u8 val)
3226{
3227 int new_power;
3228
c76bb61a
DS
3229 if (dev_priv->rps.is_bdw_sw_turbo)
3230 return;
3231
dd75fdc8
CW
3232 new_power = dev_priv->rps.power;
3233 switch (dev_priv->rps.power) {
3234 case LOW_POWER:
b39fb297 3235 if (val > dev_priv->rps.efficient_freq + 1 && val > dev_priv->rps.cur_freq)
dd75fdc8
CW
3236 new_power = BETWEEN;
3237 break;
3238
3239 case BETWEEN:
b39fb297 3240 if (val <= dev_priv->rps.efficient_freq && val < dev_priv->rps.cur_freq)
dd75fdc8 3241 new_power = LOW_POWER;
b39fb297 3242 else if (val >= dev_priv->rps.rp0_freq && val > dev_priv->rps.cur_freq)
dd75fdc8
CW
3243 new_power = HIGH_POWER;
3244 break;
3245
3246 case HIGH_POWER:
b39fb297 3247 if (val < (dev_priv->rps.rp1_freq + dev_priv->rps.rp0_freq) >> 1 && val < dev_priv->rps.cur_freq)
dd75fdc8
CW
3248 new_power = BETWEEN;
3249 break;
3250 }
3251 /* Max/min bins are special */
b39fb297 3252 if (val == dev_priv->rps.min_freq_softlimit)
dd75fdc8 3253 new_power = LOW_POWER;
b39fb297 3254 if (val == dev_priv->rps.max_freq_softlimit)
dd75fdc8
CW
3255 new_power = HIGH_POWER;
3256 if (new_power == dev_priv->rps.power)
3257 return;
3258
3259 /* Note the units here are not exactly 1us, but 1280ns. */
3260 switch (new_power) {
3261 case LOW_POWER:
3262 /* Upclock if more than 95% busy over 16ms */
3263 I915_WRITE(GEN6_RP_UP_EI, 12500);
3264 I915_WRITE(GEN6_RP_UP_THRESHOLD, 11800);
3265
3266 /* Downclock if less than 85% busy over 32ms */
3267 I915_WRITE(GEN6_RP_DOWN_EI, 25000);
3268 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 21250);
3269
3270 I915_WRITE(GEN6_RP_CONTROL,
3271 GEN6_RP_MEDIA_TURBO |
3272 GEN6_RP_MEDIA_HW_NORMAL_MODE |
3273 GEN6_RP_MEDIA_IS_GFX |
3274 GEN6_RP_ENABLE |
3275 GEN6_RP_UP_BUSY_AVG |
3276 GEN6_RP_DOWN_IDLE_AVG);
3277 break;
3278
3279 case BETWEEN:
3280 /* Upclock if more than 90% busy over 13ms */
3281 I915_WRITE(GEN6_RP_UP_EI, 10250);
3282 I915_WRITE(GEN6_RP_UP_THRESHOLD, 9225);
3283
3284 /* Downclock if less than 75% busy over 32ms */
3285 I915_WRITE(GEN6_RP_DOWN_EI, 25000);
3286 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 18750);
3287
3288 I915_WRITE(GEN6_RP_CONTROL,
3289 GEN6_RP_MEDIA_TURBO |
3290 GEN6_RP_MEDIA_HW_NORMAL_MODE |
3291 GEN6_RP_MEDIA_IS_GFX |
3292 GEN6_RP_ENABLE |
3293 GEN6_RP_UP_BUSY_AVG |
3294 GEN6_RP_DOWN_IDLE_AVG);
3295 break;
3296
3297 case HIGH_POWER:
3298 /* Upclock if more than 85% busy over 10ms */
3299 I915_WRITE(GEN6_RP_UP_EI, 8000);
3300 I915_WRITE(GEN6_RP_UP_THRESHOLD, 6800);
3301
3302 /* Downclock if less than 60% busy over 32ms */
3303 I915_WRITE(GEN6_RP_DOWN_EI, 25000);
3304 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 15000);
3305
3306 I915_WRITE(GEN6_RP_CONTROL,
3307 GEN6_RP_MEDIA_TURBO |
3308 GEN6_RP_MEDIA_HW_NORMAL_MODE |
3309 GEN6_RP_MEDIA_IS_GFX |
3310 GEN6_RP_ENABLE |
3311 GEN6_RP_UP_BUSY_AVG |
3312 GEN6_RP_DOWN_IDLE_AVG);
3313 break;
3314 }
3315
3316 dev_priv->rps.power = new_power;
3317 dev_priv->rps.last_adj = 0;
3318}
3319
2876ce73
CW
3320static u32 gen6_rps_pm_mask(struct drm_i915_private *dev_priv, u8 val)
3321{
3322 u32 mask = 0;
3323
3324 if (val > dev_priv->rps.min_freq_softlimit)
3325 mask |= GEN6_PM_RP_DOWN_THRESHOLD | GEN6_PM_RP_DOWN_TIMEOUT;
3326 if (val < dev_priv->rps.max_freq_softlimit)
3327 mask |= GEN6_PM_RP_UP_THRESHOLD;
3328
7b3c29f6
CW
3329 mask |= dev_priv->pm_rps_events & (GEN6_PM_RP_DOWN_EI_EXPIRED | GEN6_PM_RP_UP_EI_EXPIRED);
3330 mask &= dev_priv->pm_rps_events;
3331
2876ce73
CW
3332 /* IVB and SNB hard hangs on looping batchbuffer
3333 * if GEN6_PM_UP_EI_EXPIRED is masked.
3334 */
3335 if (INTEL_INFO(dev_priv->dev)->gen <= 7 && !IS_HASWELL(dev_priv->dev))
3336 mask |= GEN6_PM_RP_UP_EI_EXPIRED;
3337
baccd458
D
3338 if (IS_GEN8(dev_priv->dev))
3339 mask |= GEN8_PMINTR_REDIRECT_TO_NON_DISP;
3340
2876ce73
CW
3341 return ~mask;
3342}
3343
b8a5ff8d
JM
3344/* gen6_set_rps is called to update the frequency request, but should also be
3345 * called when the range (min_delay and max_delay) is modified so that we can
3346 * update the GEN6_RP_INTERRUPT_LIMITS register accordingly. */
20b46e59
DV
3347void gen6_set_rps(struct drm_device *dev, u8 val)
3348{
3349 struct drm_i915_private *dev_priv = dev->dev_private;
7b9e0ae6 3350
4fc688ce 3351 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
b39fb297
BW
3352 WARN_ON(val > dev_priv->rps.max_freq_softlimit);
3353 WARN_ON(val < dev_priv->rps.min_freq_softlimit);
004777cb 3354
eb64cad1
CW
3355 /* min/max delay may still have been modified so be sure to
3356 * write the limits value.
3357 */
3358 if (val != dev_priv->rps.cur_freq) {
3359 gen6_set_rps_thresholds(dev_priv, val);
b8a5ff8d 3360
50e6a2a7 3361 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
eb64cad1
CW
3362 I915_WRITE(GEN6_RPNSWREQ,
3363 HSW_FREQUENCY(val));
3364 else
3365 I915_WRITE(GEN6_RPNSWREQ,
3366 GEN6_FREQUENCY(val) |
3367 GEN6_OFFSET(0) |
3368 GEN6_AGGRESSIVE_TURBO);
b8a5ff8d 3369 }
7b9e0ae6 3370
7b9e0ae6
CW
3371 /* Make sure we continue to get interrupts
3372 * until we hit the minimum or maximum frequencies.
3373 */
eb64cad1 3374 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS, gen6_rps_limits(dev_priv, val));
2876ce73 3375 I915_WRITE(GEN6_PMINTRMSK, gen6_rps_pm_mask(dev_priv, val));
7b9e0ae6 3376
d5570a72
BW
3377 POSTING_READ(GEN6_RPNSWREQ);
3378
b39fb297 3379 dev_priv->rps.cur_freq = val;
be2cde9a 3380 trace_intel_gpu_freq_change(val * 50);
2b4e57bd
ED
3381}
3382
76c3552f
D
3383/* vlv_set_rps_idle: Set the frequency to Rpn if Gfx clocks are down
3384 *
3385 * * If Gfx is Idle, then
3386 * 1. Mask Turbo interrupts
3387 * 2. Bring up Gfx clock
3388 * 3. Change the freq to Rpn and wait till P-Unit updates freq
3389 * 4. Clear the Force GFX CLK ON bit so that Gfx can down
3390 * 5. Unmask Turbo interrupts
3391*/
3392static void vlv_set_rps_idle(struct drm_i915_private *dev_priv)
3393{
5549d25f
D
3394 struct drm_device *dev = dev_priv->dev;
3395
3396 /* Latest VLV doesn't need to force the gfx clock */
3397 if (dev->pdev->revision >= 0xd) {
3398 valleyview_set_rps(dev_priv->dev, dev_priv->rps.min_freq_softlimit);
3399 return;
3400 }
3401
76c3552f
D
3402 /*
3403 * When we are idle. Drop to min voltage state.
3404 */
3405
b39fb297 3406 if (dev_priv->rps.cur_freq <= dev_priv->rps.min_freq_softlimit)
76c3552f
D
3407 return;
3408
3409 /* Mask turbo interrupt so that they will not come in between */
3410 I915_WRITE(GEN6_PMINTRMSK, 0xffffffff);
3411
650ad970 3412 vlv_force_gfx_clock(dev_priv, true);
76c3552f 3413
b39fb297 3414 dev_priv->rps.cur_freq = dev_priv->rps.min_freq_softlimit;
76c3552f
D
3415
3416 vlv_punit_write(dev_priv, PUNIT_REG_GPU_FREQ_REQ,
b39fb297 3417 dev_priv->rps.min_freq_softlimit);
76c3552f
D
3418
3419 if (wait_for(((vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS))
3420 & GENFREQSTATUS) == 0, 5))
3421 DRM_ERROR("timed out waiting for Punit\n");
3422
650ad970 3423 vlv_force_gfx_clock(dev_priv, false);
76c3552f 3424
2876ce73
CW
3425 I915_WRITE(GEN6_PMINTRMSK,
3426 gen6_rps_pm_mask(dev_priv, dev_priv->rps.cur_freq));
76c3552f
D
3427}
3428
b29c19b6
CW
3429void gen6_rps_idle(struct drm_i915_private *dev_priv)
3430{
691bb717
DL
3431 struct drm_device *dev = dev_priv->dev;
3432
b29c19b6 3433 mutex_lock(&dev_priv->rps.hw_lock);
c0951f0c 3434 if (dev_priv->rps.enabled) {
34638118
D
3435 if (IS_CHERRYVIEW(dev))
3436 valleyview_set_rps(dev_priv->dev, dev_priv->rps.min_freq_softlimit);
3437 else if (IS_VALLEYVIEW(dev))
76c3552f 3438 vlv_set_rps_idle(dev_priv);
c76bb61a
DS
3439 else if (!dev_priv->rps.is_bdw_sw_turbo
3440 || atomic_read(&dev_priv->rps.sw_turbo.flip_received)){
b39fb297 3441 gen6_set_rps(dev_priv->dev, dev_priv->rps.min_freq_softlimit);
c76bb61a
DS
3442 }
3443
c0951f0c
CW
3444 dev_priv->rps.last_adj = 0;
3445 }
b29c19b6
CW
3446 mutex_unlock(&dev_priv->rps.hw_lock);
3447}
3448
3449void gen6_rps_boost(struct drm_i915_private *dev_priv)
3450{
691bb717
DL
3451 struct drm_device *dev = dev_priv->dev;
3452
b29c19b6 3453 mutex_lock(&dev_priv->rps.hw_lock);
c0951f0c 3454 if (dev_priv->rps.enabled) {
691bb717 3455 if (IS_VALLEYVIEW(dev))
b39fb297 3456 valleyview_set_rps(dev_priv->dev, dev_priv->rps.max_freq_softlimit);
c76bb61a
DS
3457 else if (!dev_priv->rps.is_bdw_sw_turbo
3458 || atomic_read(&dev_priv->rps.sw_turbo.flip_received)){
b39fb297 3459 gen6_set_rps(dev_priv->dev, dev_priv->rps.max_freq_softlimit);
c76bb61a
DS
3460 }
3461
c0951f0c
CW
3462 dev_priv->rps.last_adj = 0;
3463 }
b29c19b6
CW
3464 mutex_unlock(&dev_priv->rps.hw_lock);
3465}
3466
0a073b84
JB
3467void valleyview_set_rps(struct drm_device *dev, u8 val)
3468{
3469 struct drm_i915_private *dev_priv = dev->dev_private;
7a67092a 3470
0a073b84 3471 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
b39fb297
BW
3472 WARN_ON(val > dev_priv->rps.max_freq_softlimit);
3473 WARN_ON(val < dev_priv->rps.min_freq_softlimit);
0a073b84 3474
73008b98 3475 DRM_DEBUG_DRIVER("GPU freq request from %d MHz (%u) to %d MHz (%u)\n",
b39fb297
BW
3476 vlv_gpu_freq(dev_priv, dev_priv->rps.cur_freq),
3477 dev_priv->rps.cur_freq,
2ec3815f 3478 vlv_gpu_freq(dev_priv, val), val);
0a073b84 3479
1c14762d
VS
3480 if (WARN_ONCE(IS_CHERRYVIEW(dev) && (val & 1),
3481 "Odd GPU freq value\n"))
3482 val &= ~1;
3483
2876ce73
CW
3484 if (val != dev_priv->rps.cur_freq)
3485 vlv_punit_write(dev_priv, PUNIT_REG_GPU_FREQ_REQ, val);
0a073b84 3486
09c87db8 3487 I915_WRITE(GEN6_PMINTRMSK, gen6_rps_pm_mask(dev_priv, val));
0a073b84 3488
b39fb297 3489 dev_priv->rps.cur_freq = val;
2ec3815f 3490 trace_intel_gpu_freq_change(vlv_gpu_freq(dev_priv, val));
0a073b84
JB
3491}
3492
0961021a
BW
3493static void gen8_disable_rps_interrupts(struct drm_device *dev)
3494{
3495 struct drm_i915_private *dev_priv = dev->dev_private;
c76bb61a
DS
3496 if (IS_BROADWELL(dev) && dev_priv->rps.is_bdw_sw_turbo){
3497 if (atomic_read(&dev_priv->rps.sw_turbo.flip_received))
3498 del_timer(&dev_priv->rps.sw_turbo.flip_timer);
3499 dev_priv-> rps.is_bdw_sw_turbo = false;
3500 } else {
3501 I915_WRITE(GEN6_PMINTRMSK, ~GEN8_PMINTR_REDIRECT_TO_NON_DISP);
3502 I915_WRITE(GEN8_GT_IER(2), I915_READ(GEN8_GT_IER(2)) &
3503 ~dev_priv->pm_rps_events);
3504 /* Complete PM interrupt masking here doesn't race with the rps work
3505 * item again unmasking PM interrupts because that is using a different
3506 * register (GEN8_GT_IMR(2)) to mask PM interrupts. The only risk is in
3507 * leaving stale bits in GEN8_GT_IIR(2) and GEN8_GT_IMR(2) which
3508 * gen8_enable_rps will clean up. */
3509
3510 spin_lock_irq(&dev_priv->irq_lock);
3511 dev_priv->rps.pm_iir = 0;
3512 spin_unlock_irq(&dev_priv->irq_lock);
3513
3514 I915_WRITE(GEN8_GT_IIR(2), dev_priv->pm_rps_events);
3515 }
0961021a
BW
3516}
3517
44fc7d5c 3518static void gen6_disable_rps_interrupts(struct drm_device *dev)
2b4e57bd
ED
3519{
3520 struct drm_i915_private *dev_priv = dev->dev_private;
3521
2b4e57bd 3522 I915_WRITE(GEN6_PMINTRMSK, 0xffffffff);
a6706b45
D
3523 I915_WRITE(GEN6_PMIER, I915_READ(GEN6_PMIER) &
3524 ~dev_priv->pm_rps_events);
2b4e57bd
ED
3525 /* Complete PM interrupt masking here doesn't race with the rps work
3526 * item again unmasking PM interrupts because that is using a different
3527 * register (PMIMR) to mask PM interrupts. The only risk is in leaving
3528 * stale bits in PMIIR and PMIMR which gen6_enable_rps will clean up. */
3529
59cdb63d 3530 spin_lock_irq(&dev_priv->irq_lock);
c6a828d3 3531 dev_priv->rps.pm_iir = 0;
59cdb63d 3532 spin_unlock_irq(&dev_priv->irq_lock);
2b4e57bd 3533
a6706b45 3534 I915_WRITE(GEN6_PMIIR, dev_priv->pm_rps_events);
2b4e57bd
ED
3535}
3536
44fc7d5c 3537static void gen6_disable_rps(struct drm_device *dev)
d20d4f0c
JB
3538{
3539 struct drm_i915_private *dev_priv = dev->dev_private;
3540
3541 I915_WRITE(GEN6_RC_CONTROL, 0);
44fc7d5c 3542 I915_WRITE(GEN6_RPNSWREQ, 1 << 31);
d20d4f0c 3543
0961021a
BW
3544 if (IS_BROADWELL(dev))
3545 gen8_disable_rps_interrupts(dev);
3546 else
3547 gen6_disable_rps_interrupts(dev);
44fc7d5c
DV
3548}
3549
38807746
D
3550static void cherryview_disable_rps(struct drm_device *dev)
3551{
3552 struct drm_i915_private *dev_priv = dev->dev_private;
3553
3554 I915_WRITE(GEN6_RC_CONTROL, 0);
3497a562
D
3555
3556 gen8_disable_rps_interrupts(dev);
38807746
D
3557}
3558
44fc7d5c
DV
3559static void valleyview_disable_rps(struct drm_device *dev)
3560{
3561 struct drm_i915_private *dev_priv = dev->dev_private;
3562
98a2e5f9
D
3563 /* we're doing forcewake before Disabling RC6,
3564 * This what the BIOS expects when going into suspend */
3565 gen6_gt_force_wake_get(dev_priv, FORCEWAKE_ALL);
3566
44fc7d5c 3567 I915_WRITE(GEN6_RC_CONTROL, 0);
d20d4f0c 3568
98a2e5f9
D
3569 gen6_gt_force_wake_put(dev_priv, FORCEWAKE_ALL);
3570
44fc7d5c 3571 gen6_disable_rps_interrupts(dev);
d20d4f0c
JB
3572}
3573
dc39fff7
BW
3574static void intel_print_rc6_info(struct drm_device *dev, u32 mode)
3575{
91ca689a
ID
3576 if (IS_VALLEYVIEW(dev)) {
3577 if (mode & (GEN7_RC_CTL_TO_MODE | GEN6_RC_CTL_EI_MODE(1)))
3578 mode = GEN6_RC_CTL_RC6_ENABLE;
3579 else
3580 mode = 0;
3581 }
8dfd1f04
DV
3582 DRM_DEBUG_KMS("Enabling RC6 states: RC6 %s, RC6p %s, RC6pp %s\n",
3583 (mode & GEN6_RC_CTL_RC6_ENABLE) ? "on" : "off",
3584 (mode & GEN6_RC_CTL_RC6p_ENABLE) ? "on" : "off",
3585 (mode & GEN6_RC_CTL_RC6pp_ENABLE) ? "on" : "off");
dc39fff7
BW
3586}
3587
e6069ca8 3588static int sanitize_rc6_option(const struct drm_device *dev, int enable_rc6)
2b4e57bd 3589{
eb4926e4
DL
3590 /* No RC6 before Ironlake */
3591 if (INTEL_INFO(dev)->gen < 5)
3592 return 0;
3593
e6069ca8
ID
3594 /* RC6 is only on Ironlake mobile not on desktop */
3595 if (INTEL_INFO(dev)->gen == 5 && !IS_IRONLAKE_M(dev))
3596 return 0;
3597
456470eb 3598 /* Respect the kernel parameter if it is set */
e6069ca8
ID
3599 if (enable_rc6 >= 0) {
3600 int mask;
3601
3602 if (INTEL_INFO(dev)->gen == 6 || IS_IVYBRIDGE(dev))
3603 mask = INTEL_RC6_ENABLE | INTEL_RC6p_ENABLE |
3604 INTEL_RC6pp_ENABLE;
3605 else
3606 mask = INTEL_RC6_ENABLE;
3607
3608 if ((enable_rc6 & mask) != enable_rc6)
8dfd1f04
DV
3609 DRM_DEBUG_KMS("Adjusting RC6 mask to %d (requested %d, valid %d)\n",
3610 enable_rc6 & mask, enable_rc6, mask);
e6069ca8
ID
3611
3612 return enable_rc6 & mask;
3613 }
2b4e57bd 3614
6567d748
CW
3615 /* Disable RC6 on Ironlake */
3616 if (INTEL_INFO(dev)->gen == 5)
3617 return 0;
2b4e57bd 3618
8bade1ad 3619 if (IS_IVYBRIDGE(dev))
cca84a1f 3620 return (INTEL_RC6_ENABLE | INTEL_RC6p_ENABLE);
8bade1ad
BW
3621
3622 return INTEL_RC6_ENABLE;
2b4e57bd
ED
3623}
3624
e6069ca8
ID
3625int intel_enable_rc6(const struct drm_device *dev)
3626{
3627 return i915.enable_rc6;
3628}
3629
0961021a
BW
3630static void gen8_enable_rps_interrupts(struct drm_device *dev)
3631{
3632 struct drm_i915_private *dev_priv = dev->dev_private;
3633
3634 spin_lock_irq(&dev_priv->irq_lock);
3635 WARN_ON(dev_priv->rps.pm_iir);
480c8033 3636 gen8_enable_pm_irq(dev_priv, dev_priv->pm_rps_events);
0961021a
BW
3637 I915_WRITE(GEN8_GT_IIR(2), dev_priv->pm_rps_events);
3638 spin_unlock_irq(&dev_priv->irq_lock);
3639}
3640
44fc7d5c
DV
3641static void gen6_enable_rps_interrupts(struct drm_device *dev)
3642{
3643 struct drm_i915_private *dev_priv = dev->dev_private;
3644
3645 spin_lock_irq(&dev_priv->irq_lock);
a0b3335a 3646 WARN_ON(dev_priv->rps.pm_iir);
480c8033 3647 gen6_enable_pm_irq(dev_priv, dev_priv->pm_rps_events);
a6706b45 3648 I915_WRITE(GEN6_PMIIR, dev_priv->pm_rps_events);
44fc7d5c 3649 spin_unlock_irq(&dev_priv->irq_lock);
44fc7d5c
DV
3650}
3651
3280e8b0
BW
3652static void parse_rp_state_cap(struct drm_i915_private *dev_priv, u32 rp_state_cap)
3653{
3654 /* All of these values are in units of 50MHz */
3655 dev_priv->rps.cur_freq = 0;
3656 /* static values from HW: RP0 < RPe < RP1 < RPn (min_freq) */
3657 dev_priv->rps.rp1_freq = (rp_state_cap >> 8) & 0xff;
3658 dev_priv->rps.rp0_freq = (rp_state_cap >> 0) & 0xff;
3659 dev_priv->rps.min_freq = (rp_state_cap >> 16) & 0xff;
3660 /* XXX: only BYT has a special efficient freq */
3661 dev_priv->rps.efficient_freq = dev_priv->rps.rp1_freq;
3662 /* hw_max = RP0 until we check for overclocking */
3663 dev_priv->rps.max_freq = dev_priv->rps.rp0_freq;
3664
3665 /* Preserve min/max settings in case of re-init */
3666 if (dev_priv->rps.max_freq_softlimit == 0)
3667 dev_priv->rps.max_freq_softlimit = dev_priv->rps.max_freq;
3668
3669 if (dev_priv->rps.min_freq_softlimit == 0)
3670 dev_priv->rps.min_freq_softlimit = dev_priv->rps.min_freq;
3671}
3672
c76bb61a
DS
3673static void bdw_sw_calculate_freq(struct drm_device *dev,
3674 struct intel_rps_bdw_cal *c, u32 *cur_time, u32 *c0)
3675{
3676 struct drm_i915_private *dev_priv = dev->dev_private;
3677 u64 busy = 0;
3678 u32 busyness_pct = 0;
3679 u32 elapsed_time = 0;
3680 u16 new_freq = 0;
3681
3682 if (!c || !cur_time || !c0)
3683 return;
3684
3685 if (0 == c->last_c0)
3686 goto out;
3687
3688 /* Check Evaluation interval */
3689 elapsed_time = *cur_time - c->last_ts;
3690 if (elapsed_time < c->eval_interval)
3691 return;
3692
3693 mutex_lock(&dev_priv->rps.hw_lock);
3694
3695 /*
3696 * c0 unit in 32*1.28 usec, elapsed_time unit in 1 usec.
3697 * Whole busyness_pct calculation should be
3698 * busy = ((u64)(*c0 - c->last_c0) << 5 << 7) / 100;
3699 * busyness_pct = (u32)(busy * 100 / elapsed_time);
3700 * The final formula is to simplify CPU calculation
3701 */
3702 busy = (u64)(*c0 - c->last_c0) << 12;
3703 do_div(busy, elapsed_time);
3704 busyness_pct = (u32)busy;
3705
3706 if (c->is_up && busyness_pct >= c->it_threshold_pct)
3707 new_freq = (u16)dev_priv->rps.cur_freq + 3;
3708 if (!c->is_up && busyness_pct <= c->it_threshold_pct)
3709 new_freq = (u16)dev_priv->rps.cur_freq - 1;
3710
3711 /* Adjust to new frequency busyness and compare with threshold */
3712 if (0 != new_freq) {
3713 if (new_freq > dev_priv->rps.max_freq_softlimit)
3714 new_freq = dev_priv->rps.max_freq_softlimit;
3715 else if (new_freq < dev_priv->rps.min_freq_softlimit)
3716 new_freq = dev_priv->rps.min_freq_softlimit;
3717
3718 gen6_set_rps(dev, new_freq);
3719 }
3720
3721 mutex_unlock(&dev_priv->rps.hw_lock);
3722
3723out:
3724 c->last_c0 = *c0;
3725 c->last_ts = *cur_time;
3726}
3727
3728static void gen8_set_frequency_RP0(struct work_struct *work)
3729{
3730 struct intel_rps_bdw_turbo *p_bdw_turbo =
3731 container_of(work, struct intel_rps_bdw_turbo, work_max_freq);
3732 struct intel_gen6_power_mgmt *p_power_mgmt =
3733 container_of(p_bdw_turbo, struct intel_gen6_power_mgmt, sw_turbo);
3734 struct drm_i915_private *dev_priv =
3735 container_of(p_power_mgmt, struct drm_i915_private, rps);
3736
3737 mutex_lock(&dev_priv->rps.hw_lock);
3738 gen6_set_rps(dev_priv->dev, dev_priv->rps.rp0_freq);
3739 mutex_unlock(&dev_priv->rps.hw_lock);
3740}
3741
3742static void flip_active_timeout_handler(unsigned long var)
3743{
3744 struct drm_i915_private *dev_priv = (struct drm_i915_private *) var;
3745
3746 del_timer(&dev_priv->rps.sw_turbo.flip_timer);
3747 atomic_set(&dev_priv->rps.sw_turbo.flip_received, false);
3748
3749 queue_work(dev_priv->wq, &dev_priv->rps.sw_turbo.work_max_freq);
3750}
3751
3752void bdw_software_turbo(struct drm_device *dev)
3753{
3754 struct drm_i915_private *dev_priv = dev->dev_private;
3755
3756 u32 current_time = I915_READ(TIMESTAMP_CTR); /* unit in usec */
3757 u32 current_c0 = I915_READ(MCHBAR_PCU_C0); /* unit in 32*1.28 usec */
3758
3759 bdw_sw_calculate_freq(dev, &dev_priv->rps.sw_turbo.up,
3760 &current_time, &current_c0);
3761 bdw_sw_calculate_freq(dev, &dev_priv->rps.sw_turbo.down,
3762 &current_time, &current_c0);
3763}
3764
6edee7f3
BW
3765static void gen8_enable_rps(struct drm_device *dev)
3766{
3767 struct drm_i915_private *dev_priv = dev->dev_private;
a4872ba6 3768 struct intel_engine_cs *ring;
6edee7f3 3769 uint32_t rc6_mask = 0, rp_state_cap;
c76bb61a
DS
3770 uint32_t threshold_up_pct, threshold_down_pct;
3771 uint32_t ei_up, ei_down; /* up and down evaluation interval */
3772 u32 rp_ctl_flag;
6edee7f3
BW
3773 int unused;
3774
c76bb61a
DS
3775 /* Use software Turbo for BDW */
3776 dev_priv->rps.is_bdw_sw_turbo = IS_BROADWELL(dev);
3777
6edee7f3
BW
3778 /* 1a: Software RC state - RC0 */
3779 I915_WRITE(GEN6_RC_STATE, 0);
3780
3781 /* 1c & 1d: Get forcewake during program sequence. Although the driver
3782 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
c8d9a590 3783 gen6_gt_force_wake_get(dev_priv, FORCEWAKE_ALL);
6edee7f3
BW
3784
3785 /* 2a: Disable RC states. */
3786 I915_WRITE(GEN6_RC_CONTROL, 0);
3787
3788 rp_state_cap = I915_READ(GEN6_RP_STATE_CAP);
3280e8b0 3789 parse_rp_state_cap(dev_priv, rp_state_cap);
6edee7f3
BW
3790
3791 /* 2b: Program RC6 thresholds.*/
3792 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16);
3793 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
3794 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
3795 for_each_ring(ring, dev_priv, unused)
3796 I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
3797 I915_WRITE(GEN6_RC_SLEEP, 0);
0d68b25e
TR
3798 if (IS_BROADWELL(dev))
3799 I915_WRITE(GEN6_RC6_THRESHOLD, 625); /* 800us/1.28 for TO */
3800 else
3801 I915_WRITE(GEN6_RC6_THRESHOLD, 50000); /* 50/125ms per EI */
6edee7f3
BW
3802
3803 /* 3: Enable RC6 */
3804 if (intel_enable_rc6(dev) & INTEL_RC6_ENABLE)
3805 rc6_mask = GEN6_RC_CTL_RC6_ENABLE;
abbf9d2c 3806 intel_print_rc6_info(dev, rc6_mask);
0d68b25e
TR
3807 if (IS_BROADWELL(dev))
3808 I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
3809 GEN7_RC_CTL_TO_MODE |
3810 rc6_mask);
3811 else
3812 I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
3813 GEN6_RC_CTL_EI_MODE(1) |
3814 rc6_mask);
6edee7f3
BW
3815
3816 /* 4 Program defaults and thresholds for RPS*/
f9bdc585
BW
3817 I915_WRITE(GEN6_RPNSWREQ,
3818 HSW_FREQUENCY(dev_priv->rps.rp1_freq));
3819 I915_WRITE(GEN6_RC_VIDEO_FREQ,
3820 HSW_FREQUENCY(dev_priv->rps.rp1_freq));
c76bb61a
DS
3821 ei_up = 84480; /* 84.48ms */
3822 ei_down = 448000;
3823 threshold_up_pct = 90; /* x percent busy */
3824 threshold_down_pct = 70;
3825
3826 if (dev_priv->rps.is_bdw_sw_turbo) {
3827 dev_priv->rps.sw_turbo.up.it_threshold_pct = threshold_up_pct;
3828 dev_priv->rps.sw_turbo.up.eval_interval = ei_up;
3829 dev_priv->rps.sw_turbo.up.is_up = true;
3830 dev_priv->rps.sw_turbo.up.last_ts = 0;
3831 dev_priv->rps.sw_turbo.up.last_c0 = 0;
3832
3833 dev_priv->rps.sw_turbo.down.it_threshold_pct = threshold_down_pct;
3834 dev_priv->rps.sw_turbo.down.eval_interval = ei_down;
3835 dev_priv->rps.sw_turbo.down.is_up = false;
3836 dev_priv->rps.sw_turbo.down.last_ts = 0;
3837 dev_priv->rps.sw_turbo.down.last_c0 = 0;
3838
3839 /* Start the timer to track if flip comes*/
3840 dev_priv->rps.sw_turbo.timeout = 200*1000; /* in us */
3841
3842 init_timer(&dev_priv->rps.sw_turbo.flip_timer);
3843 dev_priv->rps.sw_turbo.flip_timer.function = flip_active_timeout_handler;
3844 dev_priv->rps.sw_turbo.flip_timer.data = (unsigned long) dev_priv;
3845 dev_priv->rps.sw_turbo.flip_timer.expires =
3846 usecs_to_jiffies(dev_priv->rps.sw_turbo.timeout) + jiffies;
3847 add_timer(&dev_priv->rps.sw_turbo.flip_timer);
3848 INIT_WORK(&dev_priv->rps.sw_turbo.work_max_freq, gen8_set_frequency_RP0);
3849
3850 atomic_set(&dev_priv->rps.sw_turbo.flip_received, true);
3851 } else {
3852 /* NB: Docs say 1s, and 1000000 - which aren't equivalent
3853 * 1 second timeout*/
3854 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, FREQ_1_28_US(1000000));
3855
3856 /* Docs recommend 900MHz, and 300 MHz respectively */
3857 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS,
3858 dev_priv->rps.max_freq_softlimit << 24 |
3859 dev_priv->rps.min_freq_softlimit << 16);
3860
3861 I915_WRITE(GEN6_RP_UP_THRESHOLD,
3862 FREQ_1_28_US(ei_up * threshold_up_pct / 100));
3863 I915_WRITE(GEN6_RP_DOWN_THRESHOLD,
3864 FREQ_1_28_US(ei_down * threshold_down_pct / 100));
3865 I915_WRITE(GEN6_RP_UP_EI,
3866 FREQ_1_28_US(ei_up));
3867 I915_WRITE(GEN6_RP_DOWN_EI,
3868 FREQ_1_28_US(ei_down));
3869
3870 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
3871 }
6edee7f3
BW
3872
3873 /* 5: Enable RPS */
c76bb61a
DS
3874 rp_ctl_flag = GEN6_RP_MEDIA_TURBO |
3875 GEN6_RP_MEDIA_HW_NORMAL_MODE |
3876 GEN6_RP_MEDIA_IS_GFX |
3877 GEN6_RP_UP_BUSY_AVG |
3878 GEN6_RP_DOWN_IDLE_AVG;
3879 if (!dev_priv->rps.is_bdw_sw_turbo)
3880 rp_ctl_flag |= GEN6_RP_ENABLE;
3881
3882 I915_WRITE(GEN6_RP_CONTROL, rp_ctl_flag);
3883
3884 /* 6: Ring frequency + overclocking
3885 * (our driver does this later */
6edee7f3 3886 gen6_set_rps(dev, (I915_READ(GEN6_GT_PERF_STATUS) & 0xff00) >> 8);
c76bb61a
DS
3887 if (!dev_priv->rps.is_bdw_sw_turbo)
3888 gen8_enable_rps_interrupts(dev);
6edee7f3 3889
c8d9a590 3890 gen6_gt_force_wake_put(dev_priv, FORCEWAKE_ALL);
6edee7f3
BW
3891}
3892
79f5b2c7 3893static void gen6_enable_rps(struct drm_device *dev)
2b4e57bd 3894{
79f5b2c7 3895 struct drm_i915_private *dev_priv = dev->dev_private;
a4872ba6 3896 struct intel_engine_cs *ring;
2a5913a8 3897 u32 rp_state_cap;
d060c169 3898 u32 rc6vids, pcu_mbox = 0, rc6_mask = 0;
2b4e57bd 3899 u32 gtfifodbg;
2b4e57bd 3900 int rc6_mode;
42c0526c 3901 int i, ret;
2b4e57bd 3902
4fc688ce 3903 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
79f5b2c7 3904
2b4e57bd
ED
3905 /* Here begins a magic sequence of register writes to enable
3906 * auto-downclocking.
3907 *
3908 * Perhaps there might be some value in exposing these to
3909 * userspace...
3910 */
3911 I915_WRITE(GEN6_RC_STATE, 0);
2b4e57bd
ED
3912
3913 /* Clear the DBG now so we don't confuse earlier errors */
3914 if ((gtfifodbg = I915_READ(GTFIFODBG))) {
3915 DRM_ERROR("GT fifo had a previous error %x\n", gtfifodbg);
3916 I915_WRITE(GTFIFODBG, gtfifodbg);
3917 }
3918
c8d9a590 3919 gen6_gt_force_wake_get(dev_priv, FORCEWAKE_ALL);
2b4e57bd 3920
7b9e0ae6 3921 rp_state_cap = I915_READ(GEN6_RP_STATE_CAP);
7b9e0ae6 3922
3280e8b0 3923 parse_rp_state_cap(dev_priv, rp_state_cap);
dd0a1aa1 3924
2b4e57bd
ED
3925 /* disable the counters and set deterministic thresholds */
3926 I915_WRITE(GEN6_RC_CONTROL, 0);
3927
3928 I915_WRITE(GEN6_RC1_WAKE_RATE_LIMIT, 1000 << 16);
3929 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16 | 30);
3930 I915_WRITE(GEN6_RC6pp_WAKE_RATE_LIMIT, 30);
3931 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
3932 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
3933
b4519513
CW
3934 for_each_ring(ring, dev_priv, i)
3935 I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
2b4e57bd
ED
3936
3937 I915_WRITE(GEN6_RC_SLEEP, 0);
3938 I915_WRITE(GEN6_RC1e_THRESHOLD, 1000);
29c78f60 3939 if (IS_IVYBRIDGE(dev))
351aa566
SM
3940 I915_WRITE(GEN6_RC6_THRESHOLD, 125000);
3941 else
3942 I915_WRITE(GEN6_RC6_THRESHOLD, 50000);
0920a487 3943 I915_WRITE(GEN6_RC6p_THRESHOLD, 150000);
2b4e57bd
ED
3944 I915_WRITE(GEN6_RC6pp_THRESHOLD, 64000); /* unused */
3945
5a7dc92a 3946 /* Check if we are enabling RC6 */
2b4e57bd
ED
3947 rc6_mode = intel_enable_rc6(dev_priv->dev);
3948 if (rc6_mode & INTEL_RC6_ENABLE)
3949 rc6_mask |= GEN6_RC_CTL_RC6_ENABLE;
3950
5a7dc92a
ED
3951 /* We don't use those on Haswell */
3952 if (!IS_HASWELL(dev)) {
3953 if (rc6_mode & INTEL_RC6p_ENABLE)
3954 rc6_mask |= GEN6_RC_CTL_RC6p_ENABLE;
2b4e57bd 3955
5a7dc92a
ED
3956 if (rc6_mode & INTEL_RC6pp_ENABLE)
3957 rc6_mask |= GEN6_RC_CTL_RC6pp_ENABLE;
3958 }
2b4e57bd 3959
dc39fff7 3960 intel_print_rc6_info(dev, rc6_mask);
2b4e57bd
ED
3961
3962 I915_WRITE(GEN6_RC_CONTROL,
3963 rc6_mask |
3964 GEN6_RC_CTL_EI_MODE(1) |
3965 GEN6_RC_CTL_HW_ENABLE);
3966
dd75fdc8
CW
3967 /* Power down if completely idle for over 50ms */
3968 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 50000);
2b4e57bd 3969 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
2b4e57bd 3970
42c0526c 3971 ret = sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_MIN_FREQ_TABLE, 0);
d060c169 3972 if (ret)
42c0526c 3973 DRM_DEBUG_DRIVER("Failed to set the min frequency\n");
d060c169
BW
3974
3975 ret = sandybridge_pcode_read(dev_priv, GEN6_READ_OC_PARAMS, &pcu_mbox);
3976 if (!ret && (pcu_mbox & (1<<31))) { /* OC supported */
3977 DRM_DEBUG_DRIVER("Overclocking supported. Max: %dMHz, Overclock max: %dMHz\n",
b39fb297 3978 (dev_priv->rps.max_freq_softlimit & 0xff) * 50,
d060c169 3979 (pcu_mbox & 0xff) * 50);
b39fb297 3980 dev_priv->rps.max_freq = pcu_mbox & 0xff;
2b4e57bd
ED
3981 }
3982
dd75fdc8 3983 dev_priv->rps.power = HIGH_POWER; /* force a reset */
b39fb297 3984 gen6_set_rps(dev_priv->dev, dev_priv->rps.min_freq_softlimit);
2b4e57bd 3985
44fc7d5c 3986 gen6_enable_rps_interrupts(dev);
2b4e57bd 3987
31643d54
BW
3988 rc6vids = 0;
3989 ret = sandybridge_pcode_read(dev_priv, GEN6_PCODE_READ_RC6VIDS, &rc6vids);
3990 if (IS_GEN6(dev) && ret) {
3991 DRM_DEBUG_DRIVER("Couldn't check for BIOS workaround\n");
3992 } else if (IS_GEN6(dev) && (GEN6_DECODE_RC6_VID(rc6vids & 0xff) < 450)) {
3993 DRM_DEBUG_DRIVER("You should update your BIOS. Correcting minimum rc6 voltage (%dmV->%dmV)\n",
3994 GEN6_DECODE_RC6_VID(rc6vids & 0xff), 450);
3995 rc6vids &= 0xffff00;
3996 rc6vids |= GEN6_ENCODE_RC6_VID(450);
3997 ret = sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_RC6VIDS, rc6vids);
3998 if (ret)
3999 DRM_ERROR("Couldn't fix incorrect rc6 voltage\n");
4000 }
4001
c8d9a590 4002 gen6_gt_force_wake_put(dev_priv, FORCEWAKE_ALL);
2b4e57bd
ED
4003}
4004
c2bc2fc5 4005static void __gen6_update_ring_freq(struct drm_device *dev)
2b4e57bd 4006{
79f5b2c7 4007 struct drm_i915_private *dev_priv = dev->dev_private;
2b4e57bd 4008 int min_freq = 15;
3ebecd07
CW
4009 unsigned int gpu_freq;
4010 unsigned int max_ia_freq, min_ring_freq;
2b4e57bd 4011 int scaling_factor = 180;
eda79642 4012 struct cpufreq_policy *policy;
2b4e57bd 4013
4fc688ce 4014 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
79f5b2c7 4015
eda79642
BW
4016 policy = cpufreq_cpu_get(0);
4017 if (policy) {
4018 max_ia_freq = policy->cpuinfo.max_freq;
4019 cpufreq_cpu_put(policy);
4020 } else {
4021 /*
4022 * Default to measured freq if none found, PCU will ensure we
4023 * don't go over
4024 */
2b4e57bd 4025 max_ia_freq = tsc_khz;
eda79642 4026 }
2b4e57bd
ED
4027
4028 /* Convert from kHz to MHz */
4029 max_ia_freq /= 1000;
4030
153b4b95 4031 min_ring_freq = I915_READ(DCLK) & 0xf;
f6aca45c
BW
4032 /* convert DDR frequency from units of 266.6MHz to bandwidth */
4033 min_ring_freq = mult_frac(min_ring_freq, 8, 3);
3ebecd07 4034
2b4e57bd
ED
4035 /*
4036 * For each potential GPU frequency, load a ring frequency we'd like
4037 * to use for memory access. We do this by specifying the IA frequency
4038 * the PCU should use as a reference to determine the ring frequency.
4039 */
b39fb297 4040 for (gpu_freq = dev_priv->rps.max_freq_softlimit; gpu_freq >= dev_priv->rps.min_freq_softlimit;
2b4e57bd 4041 gpu_freq--) {
b39fb297 4042 int diff = dev_priv->rps.max_freq_softlimit - gpu_freq;
3ebecd07
CW
4043 unsigned int ia_freq = 0, ring_freq = 0;
4044
46c764d4
BW
4045 if (INTEL_INFO(dev)->gen >= 8) {
4046 /* max(2 * GT, DDR). NB: GT is 50MHz units */
4047 ring_freq = max(min_ring_freq, gpu_freq);
4048 } else if (IS_HASWELL(dev)) {
f6aca45c 4049 ring_freq = mult_frac(gpu_freq, 5, 4);
3ebecd07
CW
4050 ring_freq = max(min_ring_freq, ring_freq);
4051 /* leave ia_freq as the default, chosen by cpufreq */
4052 } else {
4053 /* On older processors, there is no separate ring
4054 * clock domain, so in order to boost the bandwidth
4055 * of the ring, we need to upclock the CPU (ia_freq).
4056 *
4057 * For GPU frequencies less than 750MHz,
4058 * just use the lowest ring freq.
4059 */
4060 if (gpu_freq < min_freq)
4061 ia_freq = 800;
4062 else
4063 ia_freq = max_ia_freq - ((diff * scaling_factor) / 2);
4064 ia_freq = DIV_ROUND_CLOSEST(ia_freq, 100);
4065 }
2b4e57bd 4066
42c0526c
BW
4067 sandybridge_pcode_write(dev_priv,
4068 GEN6_PCODE_WRITE_MIN_FREQ_TABLE,
3ebecd07
CW
4069 ia_freq << GEN6_PCODE_FREQ_IA_RATIO_SHIFT |
4070 ring_freq << GEN6_PCODE_FREQ_RING_RATIO_SHIFT |
4071 gpu_freq);
2b4e57bd 4072 }
2b4e57bd
ED
4073}
4074
c2bc2fc5
ID
4075void gen6_update_ring_freq(struct drm_device *dev)
4076{
4077 struct drm_i915_private *dev_priv = dev->dev_private;
4078
4079 if (INTEL_INFO(dev)->gen < 6 || IS_VALLEYVIEW(dev))
4080 return;
4081
4082 mutex_lock(&dev_priv->rps.hw_lock);
4083 __gen6_update_ring_freq(dev);
4084 mutex_unlock(&dev_priv->rps.hw_lock);
4085}
4086
03af2045 4087static int cherryview_rps_max_freq(struct drm_i915_private *dev_priv)
2b6b3a09
D
4088{
4089 u32 val, rp0;
4090
4091 val = vlv_punit_read(dev_priv, PUNIT_GPU_STATUS_REG);
4092 rp0 = (val >> PUNIT_GPU_STATUS_MAX_FREQ_SHIFT) & PUNIT_GPU_STATUS_MAX_FREQ_MASK;
4093
4094 return rp0;
4095}
4096
4097static int cherryview_rps_rpe_freq(struct drm_i915_private *dev_priv)
4098{
4099 u32 val, rpe;
4100
4101 val = vlv_punit_read(dev_priv, PUNIT_GPU_DUTYCYCLE_REG);
4102 rpe = (val >> PUNIT_GPU_DUTYCYCLE_RPE_FREQ_SHIFT) & PUNIT_GPU_DUTYCYCLE_RPE_FREQ_MASK;
4103
4104 return rpe;
4105}
4106
7707df4a
D
4107static int cherryview_rps_guar_freq(struct drm_i915_private *dev_priv)
4108{
4109 u32 val, rp1;
4110
4111 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
4112 rp1 = (val >> PUNIT_GPU_STATUS_MAX_FREQ_SHIFT) & PUNIT_GPU_STATUS_MAX_FREQ_MASK;
4113
4114 return rp1;
4115}
4116
03af2045 4117static int cherryview_rps_min_freq(struct drm_i915_private *dev_priv)
2b6b3a09
D
4118{
4119 u32 val, rpn;
4120
4121 val = vlv_punit_read(dev_priv, PUNIT_GPU_STATUS_REG);
4122 rpn = (val >> PUNIT_GPU_STATIS_GFX_MIN_FREQ_SHIFT) & PUNIT_GPU_STATUS_GFX_MIN_FREQ_MASK;
4123 return rpn;
4124}
4125
f8f2b001
D
4126static int valleyview_rps_guar_freq(struct drm_i915_private *dev_priv)
4127{
4128 u32 val, rp1;
4129
4130 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FREQ_FUSE);
4131
4132 rp1 = (val & FB_GFX_FGUARANTEED_FREQ_FUSE_MASK) >> FB_GFX_FGUARANTEED_FREQ_FUSE_SHIFT;
4133
4134 return rp1;
4135}
4136
03af2045 4137static int valleyview_rps_max_freq(struct drm_i915_private *dev_priv)
0a073b84
JB
4138{
4139 u32 val, rp0;
4140
64936258 4141 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FREQ_FUSE);
0a073b84
JB
4142
4143 rp0 = (val & FB_GFX_MAX_FREQ_FUSE_MASK) >> FB_GFX_MAX_FREQ_FUSE_SHIFT;
4144 /* Clamp to max */
4145 rp0 = min_t(u32, rp0, 0xea);
4146
4147 return rp0;
4148}
4149
4150static int valleyview_rps_rpe_freq(struct drm_i915_private *dev_priv)
4151{
4152 u32 val, rpe;
4153
64936258 4154 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FMAX_FUSE_LO);
0a073b84 4155 rpe = (val & FB_FMAX_VMIN_FREQ_LO_MASK) >> FB_FMAX_VMIN_FREQ_LO_SHIFT;
64936258 4156 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FMAX_FUSE_HI);
0a073b84
JB
4157 rpe |= (val & FB_FMAX_VMIN_FREQ_HI_MASK) << 5;
4158
4159 return rpe;
4160}
4161
03af2045 4162static int valleyview_rps_min_freq(struct drm_i915_private *dev_priv)
0a073b84 4163{
64936258 4164 return vlv_punit_read(dev_priv, PUNIT_REG_GPU_LFM) & 0xff;
0a073b84
JB
4165}
4166
ae48434c
ID
4167/* Check that the pctx buffer wasn't move under us. */
4168static void valleyview_check_pctx(struct drm_i915_private *dev_priv)
4169{
4170 unsigned long pctx_addr = I915_READ(VLV_PCBR) & ~4095;
4171
4172 WARN_ON(pctx_addr != dev_priv->mm.stolen_base +
4173 dev_priv->vlv_pctx->stolen->start);
4174}
4175
38807746
D
4176
4177/* Check that the pcbr address is not empty. */
4178static void cherryview_check_pctx(struct drm_i915_private *dev_priv)
4179{
4180 unsigned long pctx_addr = I915_READ(VLV_PCBR) & ~4095;
4181
4182 WARN_ON((pctx_addr >> VLV_PCBR_ADDR_SHIFT) == 0);
4183}
4184
4185static void cherryview_setup_pctx(struct drm_device *dev)
4186{
4187 struct drm_i915_private *dev_priv = dev->dev_private;
4188 unsigned long pctx_paddr, paddr;
4189 struct i915_gtt *gtt = &dev_priv->gtt;
4190 u32 pcbr;
4191 int pctx_size = 32*1024;
4192
4193 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
4194
4195 pcbr = I915_READ(VLV_PCBR);
4196 if ((pcbr >> VLV_PCBR_ADDR_SHIFT) == 0) {
4197 paddr = (dev_priv->mm.stolen_base +
4198 (gtt->stolen_size - pctx_size));
4199
4200 pctx_paddr = (paddr & (~4095));
4201 I915_WRITE(VLV_PCBR, pctx_paddr);
4202 }
4203}
4204
c9cddffc
JB
4205static void valleyview_setup_pctx(struct drm_device *dev)
4206{
4207 struct drm_i915_private *dev_priv = dev->dev_private;
4208 struct drm_i915_gem_object *pctx;
4209 unsigned long pctx_paddr;
4210 u32 pcbr;
4211 int pctx_size = 24*1024;
4212
17b0c1f7
ID
4213 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
4214
c9cddffc
JB
4215 pcbr = I915_READ(VLV_PCBR);
4216 if (pcbr) {
4217 /* BIOS set it up already, grab the pre-alloc'd space */
4218 int pcbr_offset;
4219
4220 pcbr_offset = (pcbr & (~4095)) - dev_priv->mm.stolen_base;
4221 pctx = i915_gem_object_create_stolen_for_preallocated(dev_priv->dev,
4222 pcbr_offset,
190d6cd5 4223 I915_GTT_OFFSET_NONE,
c9cddffc
JB
4224 pctx_size);
4225 goto out;
4226 }
4227
4228 /*
4229 * From the Gunit register HAS:
4230 * The Gfx driver is expected to program this register and ensure
4231 * proper allocation within Gfx stolen memory. For example, this
4232 * register should be programmed such than the PCBR range does not
4233 * overlap with other ranges, such as the frame buffer, protected
4234 * memory, or any other relevant ranges.
4235 */
4236 pctx = i915_gem_object_create_stolen(dev, pctx_size);
4237 if (!pctx) {
4238 DRM_DEBUG("not enough stolen space for PCTX, disabling\n");
4239 return;
4240 }
4241
4242 pctx_paddr = dev_priv->mm.stolen_base + pctx->stolen->start;
4243 I915_WRITE(VLV_PCBR, pctx_paddr);
4244
4245out:
4246 dev_priv->vlv_pctx = pctx;
4247}
4248
ae48434c
ID
4249static void valleyview_cleanup_pctx(struct drm_device *dev)
4250{
4251 struct drm_i915_private *dev_priv = dev->dev_private;
4252
4253 if (WARN_ON(!dev_priv->vlv_pctx))
4254 return;
4255
4256 drm_gem_object_unreference(&dev_priv->vlv_pctx->base);
4257 dev_priv->vlv_pctx = NULL;
4258}
4259
4e80519e
ID
4260static void valleyview_init_gt_powersave(struct drm_device *dev)
4261{
4262 struct drm_i915_private *dev_priv = dev->dev_private;
2bb25c17 4263 u32 val;
4e80519e
ID
4264
4265 valleyview_setup_pctx(dev);
4266
4267 mutex_lock(&dev_priv->rps.hw_lock);
4268
2bb25c17
VS
4269 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
4270 switch ((val >> 6) & 3) {
4271 case 0:
4272 case 1:
4273 dev_priv->mem_freq = 800;
4274 break;
4275 case 2:
4276 dev_priv->mem_freq = 1066;
4277 break;
4278 case 3:
4279 dev_priv->mem_freq = 1333;
4280 break;
4281 }
4282 DRM_DEBUG_DRIVER("DDR speed: %d MHz", dev_priv->mem_freq);
4283
4e80519e
ID
4284 dev_priv->rps.max_freq = valleyview_rps_max_freq(dev_priv);
4285 dev_priv->rps.rp0_freq = dev_priv->rps.max_freq;
4286 DRM_DEBUG_DRIVER("max GPU freq: %d MHz (%u)\n",
4287 vlv_gpu_freq(dev_priv, dev_priv->rps.max_freq),
4288 dev_priv->rps.max_freq);
4289
4290 dev_priv->rps.efficient_freq = valleyview_rps_rpe_freq(dev_priv);
4291 DRM_DEBUG_DRIVER("RPe GPU freq: %d MHz (%u)\n",
4292 vlv_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
4293 dev_priv->rps.efficient_freq);
4294
f8f2b001
D
4295 dev_priv->rps.rp1_freq = valleyview_rps_guar_freq(dev_priv);
4296 DRM_DEBUG_DRIVER("RP1(Guar Freq) GPU freq: %d MHz (%u)\n",
4297 vlv_gpu_freq(dev_priv, dev_priv->rps.rp1_freq),
4298 dev_priv->rps.rp1_freq);
4299
4e80519e
ID
4300 dev_priv->rps.min_freq = valleyview_rps_min_freq(dev_priv);
4301 DRM_DEBUG_DRIVER("min GPU freq: %d MHz (%u)\n",
4302 vlv_gpu_freq(dev_priv, dev_priv->rps.min_freq),
4303 dev_priv->rps.min_freq);
4304
4305 /* Preserve min/max settings in case of re-init */
4306 if (dev_priv->rps.max_freq_softlimit == 0)
4307 dev_priv->rps.max_freq_softlimit = dev_priv->rps.max_freq;
4308
4309 if (dev_priv->rps.min_freq_softlimit == 0)
4310 dev_priv->rps.min_freq_softlimit = dev_priv->rps.min_freq;
4311
4312 mutex_unlock(&dev_priv->rps.hw_lock);
4313}
4314
38807746
D
4315static void cherryview_init_gt_powersave(struct drm_device *dev)
4316{
2b6b3a09 4317 struct drm_i915_private *dev_priv = dev->dev_private;
2bb25c17 4318 u32 val;
2b6b3a09 4319
38807746 4320 cherryview_setup_pctx(dev);
2b6b3a09
D
4321
4322 mutex_lock(&dev_priv->rps.hw_lock);
4323
2bb25c17
VS
4324 val = vlv_punit_read(dev_priv, CCK_FUSE_REG);
4325 switch ((val >> 2) & 0x7) {
4326 case 0:
4327 case 1:
4328 dev_priv->rps.cz_freq = 200;
4329 dev_priv->mem_freq = 1600;
4330 break;
4331 case 2:
4332 dev_priv->rps.cz_freq = 267;
4333 dev_priv->mem_freq = 1600;
4334 break;
4335 case 3:
4336 dev_priv->rps.cz_freq = 333;
4337 dev_priv->mem_freq = 2000;
4338 break;
4339 case 4:
4340 dev_priv->rps.cz_freq = 320;
4341 dev_priv->mem_freq = 1600;
4342 break;
4343 case 5:
4344 dev_priv->rps.cz_freq = 400;
4345 dev_priv->mem_freq = 1600;
4346 break;
4347 }
4348 DRM_DEBUG_DRIVER("DDR speed: %d MHz", dev_priv->mem_freq);
4349
2b6b3a09
D
4350 dev_priv->rps.max_freq = cherryview_rps_max_freq(dev_priv);
4351 dev_priv->rps.rp0_freq = dev_priv->rps.max_freq;
4352 DRM_DEBUG_DRIVER("max GPU freq: %d MHz (%u)\n",
4353 vlv_gpu_freq(dev_priv, dev_priv->rps.max_freq),
4354 dev_priv->rps.max_freq);
4355
4356 dev_priv->rps.efficient_freq = cherryview_rps_rpe_freq(dev_priv);
4357 DRM_DEBUG_DRIVER("RPe GPU freq: %d MHz (%u)\n",
4358 vlv_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
4359 dev_priv->rps.efficient_freq);
4360
7707df4a
D
4361 dev_priv->rps.rp1_freq = cherryview_rps_guar_freq(dev_priv);
4362 DRM_DEBUG_DRIVER("RP1(Guar) GPU freq: %d MHz (%u)\n",
4363 vlv_gpu_freq(dev_priv, dev_priv->rps.rp1_freq),
4364 dev_priv->rps.rp1_freq);
4365
2b6b3a09
D
4366 dev_priv->rps.min_freq = cherryview_rps_min_freq(dev_priv);
4367 DRM_DEBUG_DRIVER("min GPU freq: %d MHz (%u)\n",
4368 vlv_gpu_freq(dev_priv, dev_priv->rps.min_freq),
4369 dev_priv->rps.min_freq);
4370
1c14762d
VS
4371 WARN_ONCE((dev_priv->rps.max_freq |
4372 dev_priv->rps.efficient_freq |
4373 dev_priv->rps.rp1_freq |
4374 dev_priv->rps.min_freq) & 1,
4375 "Odd GPU freq values\n");
4376
2b6b3a09
D
4377 /* Preserve min/max settings in case of re-init */
4378 if (dev_priv->rps.max_freq_softlimit == 0)
4379 dev_priv->rps.max_freq_softlimit = dev_priv->rps.max_freq;
4380
4381 if (dev_priv->rps.min_freq_softlimit == 0)
4382 dev_priv->rps.min_freq_softlimit = dev_priv->rps.min_freq;
4383
4384 mutex_unlock(&dev_priv->rps.hw_lock);
38807746
D
4385}
4386
4e80519e
ID
4387static void valleyview_cleanup_gt_powersave(struct drm_device *dev)
4388{
4389 valleyview_cleanup_pctx(dev);
4390}
4391
38807746
D
4392static void cherryview_enable_rps(struct drm_device *dev)
4393{
4394 struct drm_i915_private *dev_priv = dev->dev_private;
4395 struct intel_engine_cs *ring;
2b6b3a09 4396 u32 gtfifodbg, val, rc6_mode = 0, pcbr;
38807746
D
4397 int i;
4398
4399 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
4400
4401 gtfifodbg = I915_READ(GTFIFODBG);
4402 if (gtfifodbg) {
4403 DRM_DEBUG_DRIVER("GT fifo had a previous error %x\n",
4404 gtfifodbg);
4405 I915_WRITE(GTFIFODBG, gtfifodbg);
4406 }
4407
4408 cherryview_check_pctx(dev_priv);
4409
4410 /* 1a & 1b: Get forcewake during program sequence. Although the driver
4411 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
4412 gen6_gt_force_wake_get(dev_priv, FORCEWAKE_ALL);
4413
4414 /* 2a: Program RC6 thresholds.*/
4415 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16);
4416 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
4417 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
4418
4419 for_each_ring(ring, dev_priv, i)
4420 I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
4421 I915_WRITE(GEN6_RC_SLEEP, 0);
4422
4423 I915_WRITE(GEN6_RC6_THRESHOLD, 50000); /* 50/125ms per EI */
4424
4425 /* allows RC6 residency counter to work */
4426 I915_WRITE(VLV_COUNTER_CONTROL,
4427 _MASKED_BIT_ENABLE(VLV_COUNT_RANGE_HIGH |
4428 VLV_MEDIA_RC6_COUNT_EN |
4429 VLV_RENDER_RC6_COUNT_EN));
4430
4431 /* For now we assume BIOS is allocating and populating the PCBR */
4432 pcbr = I915_READ(VLV_PCBR);
4433
4434 DRM_DEBUG_DRIVER("PCBR offset : 0x%x\n", pcbr);
4435
4436 /* 3: Enable RC6 */
4437 if ((intel_enable_rc6(dev) & INTEL_RC6_ENABLE) &&
4438 (pcbr >> VLV_PCBR_ADDR_SHIFT))
4439 rc6_mode = GEN6_RC_CTL_EI_MODE(1);
4440
4441 I915_WRITE(GEN6_RC_CONTROL, rc6_mode);
4442
2b6b3a09
D
4443 /* 4 Program defaults and thresholds for RPS*/
4444 I915_WRITE(GEN6_RP_UP_THRESHOLD, 59400);
4445 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 245000);
4446 I915_WRITE(GEN6_RP_UP_EI, 66000);
4447 I915_WRITE(GEN6_RP_DOWN_EI, 350000);
4448
4449 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
4450
7405f42c
TR
4451 /* WaDisablePwrmtrEvent:chv (pre-production hw) */
4452 I915_WRITE(0xA80C, I915_READ(0xA80C) & 0x00ffffff);
4453 I915_WRITE(0xA810, I915_READ(0xA810) & 0xffffff00);
4454
2b6b3a09
D
4455 /* 5: Enable RPS */
4456 I915_WRITE(GEN6_RP_CONTROL,
4457 GEN6_RP_MEDIA_HW_NORMAL_MODE |
7405f42c 4458 GEN6_RP_MEDIA_IS_GFX | /* WaSetMaskForGfxBusyness:chv (pre-production hw ?) */
2b6b3a09
D
4459 GEN6_RP_ENABLE |
4460 GEN6_RP_UP_BUSY_AVG |
4461 GEN6_RP_DOWN_IDLE_AVG);
4462
4463 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
4464
4465 DRM_DEBUG_DRIVER("GPLL enabled? %s\n", val & 0x10 ? "yes" : "no");
4466 DRM_DEBUG_DRIVER("GPU status: 0x%08x\n", val);
4467
4468 dev_priv->rps.cur_freq = (val >> 8) & 0xff;
4469 DRM_DEBUG_DRIVER("current GPU freq: %d MHz (%u)\n",
4470 vlv_gpu_freq(dev_priv, dev_priv->rps.cur_freq),
4471 dev_priv->rps.cur_freq);
4472
4473 DRM_DEBUG_DRIVER("setting GPU freq to %d MHz (%u)\n",
4474 vlv_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
4475 dev_priv->rps.efficient_freq);
4476
4477 valleyview_set_rps(dev_priv->dev, dev_priv->rps.efficient_freq);
4478
3497a562
D
4479 gen8_enable_rps_interrupts(dev);
4480
38807746
D
4481 gen6_gt_force_wake_put(dev_priv, FORCEWAKE_ALL);
4482}
4483
0a073b84
JB
4484static void valleyview_enable_rps(struct drm_device *dev)
4485{
4486 struct drm_i915_private *dev_priv = dev->dev_private;
a4872ba6 4487 struct intel_engine_cs *ring;
2a5913a8 4488 u32 gtfifodbg, val, rc6_mode = 0;
0a073b84
JB
4489 int i;
4490
4491 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
4492
ae48434c
ID
4493 valleyview_check_pctx(dev_priv);
4494
0a073b84 4495 if ((gtfifodbg = I915_READ(GTFIFODBG))) {
f7d85c1e
JB
4496 DRM_DEBUG_DRIVER("GT fifo had a previous error %x\n",
4497 gtfifodbg);
0a073b84
JB
4498 I915_WRITE(GTFIFODBG, gtfifodbg);
4499 }
4500
c8d9a590
D
4501 /* If VLV, Forcewake all wells, else re-direct to regular path */
4502 gen6_gt_force_wake_get(dev_priv, FORCEWAKE_ALL);
0a073b84
JB
4503
4504 I915_WRITE(GEN6_RP_UP_THRESHOLD, 59400);
4505 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 245000);
4506 I915_WRITE(GEN6_RP_UP_EI, 66000);
4507 I915_WRITE(GEN6_RP_DOWN_EI, 350000);
4508
4509 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
31685c25 4510 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 0xf4240);
0a073b84
JB
4511
4512 I915_WRITE(GEN6_RP_CONTROL,
4513 GEN6_RP_MEDIA_TURBO |
4514 GEN6_RP_MEDIA_HW_NORMAL_MODE |
4515 GEN6_RP_MEDIA_IS_GFX |
4516 GEN6_RP_ENABLE |
4517 GEN6_RP_UP_BUSY_AVG |
4518 GEN6_RP_DOWN_IDLE_CONT);
4519
4520 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 0x00280000);
4521 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
4522 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
4523
4524 for_each_ring(ring, dev_priv, i)
4525 I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
4526
2f0aa304 4527 I915_WRITE(GEN6_RC6_THRESHOLD, 0x557);
0a073b84
JB
4528
4529 /* allows RC6 residency counter to work */
49798eb2 4530 I915_WRITE(VLV_COUNTER_CONTROL,
31685c25
D
4531 _MASKED_BIT_ENABLE(VLV_MEDIA_RC0_COUNT_EN |
4532 VLV_RENDER_RC0_COUNT_EN |
49798eb2
JB
4533 VLV_MEDIA_RC6_COUNT_EN |
4534 VLV_RENDER_RC6_COUNT_EN));
31685c25 4535
a2b23fe0 4536 if (intel_enable_rc6(dev) & INTEL_RC6_ENABLE)
6b88f295 4537 rc6_mode = GEN7_RC_CTL_TO_MODE | VLV_RC_CTL_CTX_RST_PARALLEL;
dc39fff7
BW
4538
4539 intel_print_rc6_info(dev, rc6_mode);
4540
a2b23fe0 4541 I915_WRITE(GEN6_RC_CONTROL, rc6_mode);
0a073b84 4542
64936258 4543 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
0a073b84
JB
4544
4545 DRM_DEBUG_DRIVER("GPLL enabled? %s\n", val & 0x10 ? "yes" : "no");
4546 DRM_DEBUG_DRIVER("GPU status: 0x%08x\n", val);
4547
b39fb297 4548 dev_priv->rps.cur_freq = (val >> 8) & 0xff;
73008b98 4549 DRM_DEBUG_DRIVER("current GPU freq: %d MHz (%u)\n",
b39fb297
BW
4550 vlv_gpu_freq(dev_priv, dev_priv->rps.cur_freq),
4551 dev_priv->rps.cur_freq);
0a073b84 4552
73008b98 4553 DRM_DEBUG_DRIVER("setting GPU freq to %d MHz (%u)\n",
b39fb297
BW
4554 vlv_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
4555 dev_priv->rps.efficient_freq);
0a073b84 4556
b39fb297 4557 valleyview_set_rps(dev_priv->dev, dev_priv->rps.efficient_freq);
0a073b84 4558
44fc7d5c 4559 gen6_enable_rps_interrupts(dev);
0a073b84 4560
c8d9a590 4561 gen6_gt_force_wake_put(dev_priv, FORCEWAKE_ALL);
0a073b84
JB
4562}
4563
930ebb46 4564void ironlake_teardown_rc6(struct drm_device *dev)
2b4e57bd
ED
4565{
4566 struct drm_i915_private *dev_priv = dev->dev_private;
4567
3e373948 4568 if (dev_priv->ips.renderctx) {
d7f46fc4 4569 i915_gem_object_ggtt_unpin(dev_priv->ips.renderctx);
3e373948
DV
4570 drm_gem_object_unreference(&dev_priv->ips.renderctx->base);
4571 dev_priv->ips.renderctx = NULL;
2b4e57bd
ED
4572 }
4573
3e373948 4574 if (dev_priv->ips.pwrctx) {
d7f46fc4 4575 i915_gem_object_ggtt_unpin(dev_priv->ips.pwrctx);
3e373948
DV
4576 drm_gem_object_unreference(&dev_priv->ips.pwrctx->base);
4577 dev_priv->ips.pwrctx = NULL;
2b4e57bd
ED
4578 }
4579}
4580
930ebb46 4581static void ironlake_disable_rc6(struct drm_device *dev)
2b4e57bd
ED
4582{
4583 struct drm_i915_private *dev_priv = dev->dev_private;
4584
4585 if (I915_READ(PWRCTXA)) {
4586 /* Wake the GPU, prevent RC6, then restore RSTDBYCTL */
4587 I915_WRITE(RSTDBYCTL, I915_READ(RSTDBYCTL) | RCX_SW_EXIT);
4588 wait_for(((I915_READ(RSTDBYCTL) & RSX_STATUS_MASK) == RSX_STATUS_ON),
4589 50);
4590
4591 I915_WRITE(PWRCTXA, 0);
4592 POSTING_READ(PWRCTXA);
4593
4594 I915_WRITE(RSTDBYCTL, I915_READ(RSTDBYCTL) & ~RCX_SW_EXIT);
4595 POSTING_READ(RSTDBYCTL);
4596 }
2b4e57bd
ED
4597}
4598
4599static int ironlake_setup_rc6(struct drm_device *dev)
4600{
4601 struct drm_i915_private *dev_priv = dev->dev_private;
4602
3e373948
DV
4603 if (dev_priv->ips.renderctx == NULL)
4604 dev_priv->ips.renderctx = intel_alloc_context_page(dev);
4605 if (!dev_priv->ips.renderctx)
2b4e57bd
ED
4606 return -ENOMEM;
4607
3e373948
DV
4608 if (dev_priv->ips.pwrctx == NULL)
4609 dev_priv->ips.pwrctx = intel_alloc_context_page(dev);
4610 if (!dev_priv->ips.pwrctx) {
2b4e57bd
ED
4611 ironlake_teardown_rc6(dev);
4612 return -ENOMEM;
4613 }
4614
4615 return 0;
4616}
4617
930ebb46 4618static void ironlake_enable_rc6(struct drm_device *dev)
2b4e57bd
ED
4619{
4620 struct drm_i915_private *dev_priv = dev->dev_private;
a4872ba6 4621 struct intel_engine_cs *ring = &dev_priv->ring[RCS];
3e960501 4622 bool was_interruptible;
2b4e57bd
ED
4623 int ret;
4624
4625 /* rc6 disabled by default due to repeated reports of hanging during
4626 * boot and resume.
4627 */
4628 if (!intel_enable_rc6(dev))
4629 return;
4630
79f5b2c7
DV
4631 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
4632
2b4e57bd 4633 ret = ironlake_setup_rc6(dev);
79f5b2c7 4634 if (ret)
2b4e57bd 4635 return;
2b4e57bd 4636
3e960501
CW
4637 was_interruptible = dev_priv->mm.interruptible;
4638 dev_priv->mm.interruptible = false;
4639
2b4e57bd
ED
4640 /*
4641 * GPU can automatically power down the render unit if given a page
4642 * to save state.
4643 */
6d90c952 4644 ret = intel_ring_begin(ring, 6);
2b4e57bd
ED
4645 if (ret) {
4646 ironlake_teardown_rc6(dev);
3e960501 4647 dev_priv->mm.interruptible = was_interruptible;
2b4e57bd
ED
4648 return;
4649 }
4650
6d90c952
DV
4651 intel_ring_emit(ring, MI_SUSPEND_FLUSH | MI_SUSPEND_FLUSH_EN);
4652 intel_ring_emit(ring, MI_SET_CONTEXT);
f343c5f6 4653 intel_ring_emit(ring, i915_gem_obj_ggtt_offset(dev_priv->ips.renderctx) |
6d90c952
DV
4654 MI_MM_SPACE_GTT |
4655 MI_SAVE_EXT_STATE_EN |
4656 MI_RESTORE_EXT_STATE_EN |
4657 MI_RESTORE_INHIBIT);
4658 intel_ring_emit(ring, MI_SUSPEND_FLUSH);
4659 intel_ring_emit(ring, MI_NOOP);
4660 intel_ring_emit(ring, MI_FLUSH);
4661 intel_ring_advance(ring);
2b4e57bd
ED
4662
4663 /*
4664 * Wait for the command parser to advance past MI_SET_CONTEXT. The HW
4665 * does an implicit flush, combined with MI_FLUSH above, it should be
4666 * safe to assume that renderctx is valid
4667 */
3e960501
CW
4668 ret = intel_ring_idle(ring);
4669 dev_priv->mm.interruptible = was_interruptible;
2b4e57bd 4670 if (ret) {
def27a58 4671 DRM_ERROR("failed to enable ironlake power savings\n");
2b4e57bd 4672 ironlake_teardown_rc6(dev);
2b4e57bd
ED
4673 return;
4674 }
4675
f343c5f6 4676 I915_WRITE(PWRCTXA, i915_gem_obj_ggtt_offset(dev_priv->ips.pwrctx) | PWRCTX_EN);
2b4e57bd 4677 I915_WRITE(RSTDBYCTL, I915_READ(RSTDBYCTL) & ~RCX_SW_EXIT);
dc39fff7 4678
91ca689a 4679 intel_print_rc6_info(dev, GEN6_RC_CTL_RC6_ENABLE);
2b4e57bd
ED
4680}
4681
dde18883
ED
4682static unsigned long intel_pxfreq(u32 vidfreq)
4683{
4684 unsigned long freq;
4685 int div = (vidfreq & 0x3f0000) >> 16;
4686 int post = (vidfreq & 0x3000) >> 12;
4687 int pre = (vidfreq & 0x7);
4688
4689 if (!pre)
4690 return 0;
4691
4692 freq = ((div * 133333) / ((1<<post) * pre));
4693
4694 return freq;
4695}
4696
eb48eb00
DV
4697static const struct cparams {
4698 u16 i;
4699 u16 t;
4700 u16 m;
4701 u16 c;
4702} cparams[] = {
4703 { 1, 1333, 301, 28664 },
4704 { 1, 1066, 294, 24460 },
4705 { 1, 800, 294, 25192 },
4706 { 0, 1333, 276, 27605 },
4707 { 0, 1066, 276, 27605 },
4708 { 0, 800, 231, 23784 },
4709};
4710
f531dcb2 4711static unsigned long __i915_chipset_val(struct drm_i915_private *dev_priv)
eb48eb00
DV
4712{
4713 u64 total_count, diff, ret;
4714 u32 count1, count2, count3, m = 0, c = 0;
4715 unsigned long now = jiffies_to_msecs(jiffies), diff1;
4716 int i;
4717
02d71956
DV
4718 assert_spin_locked(&mchdev_lock);
4719
20e4d407 4720 diff1 = now - dev_priv->ips.last_time1;
eb48eb00
DV
4721
4722 /* Prevent division-by-zero if we are asking too fast.
4723 * Also, we don't get interesting results if we are polling
4724 * faster than once in 10ms, so just return the saved value
4725 * in such cases.
4726 */
4727 if (diff1 <= 10)
20e4d407 4728 return dev_priv->ips.chipset_power;
eb48eb00
DV
4729
4730 count1 = I915_READ(DMIEC);
4731 count2 = I915_READ(DDREC);
4732 count3 = I915_READ(CSIEC);
4733
4734 total_count = count1 + count2 + count3;
4735
4736 /* FIXME: handle per-counter overflow */
20e4d407
DV
4737 if (total_count < dev_priv->ips.last_count1) {
4738 diff = ~0UL - dev_priv->ips.last_count1;
eb48eb00
DV
4739 diff += total_count;
4740 } else {
20e4d407 4741 diff = total_count - dev_priv->ips.last_count1;
eb48eb00
DV
4742 }
4743
4744 for (i = 0; i < ARRAY_SIZE(cparams); i++) {
20e4d407
DV
4745 if (cparams[i].i == dev_priv->ips.c_m &&
4746 cparams[i].t == dev_priv->ips.r_t) {
eb48eb00
DV
4747 m = cparams[i].m;
4748 c = cparams[i].c;
4749 break;
4750 }
4751 }
4752
4753 diff = div_u64(diff, diff1);
4754 ret = ((m * diff) + c);
4755 ret = div_u64(ret, 10);
4756
20e4d407
DV
4757 dev_priv->ips.last_count1 = total_count;
4758 dev_priv->ips.last_time1 = now;
eb48eb00 4759
20e4d407 4760 dev_priv->ips.chipset_power = ret;
eb48eb00
DV
4761
4762 return ret;
4763}
4764
f531dcb2
CW
4765unsigned long i915_chipset_val(struct drm_i915_private *dev_priv)
4766{
3d13ef2e 4767 struct drm_device *dev = dev_priv->dev;
f531dcb2
CW
4768 unsigned long val;
4769
3d13ef2e 4770 if (INTEL_INFO(dev)->gen != 5)
f531dcb2
CW
4771 return 0;
4772
4773 spin_lock_irq(&mchdev_lock);
4774
4775 val = __i915_chipset_val(dev_priv);
4776
4777 spin_unlock_irq(&mchdev_lock);
4778
4779 return val;
4780}
4781
eb48eb00
DV
4782unsigned long i915_mch_val(struct drm_i915_private *dev_priv)
4783{
4784 unsigned long m, x, b;
4785 u32 tsfs;
4786
4787 tsfs = I915_READ(TSFS);
4788
4789 m = ((tsfs & TSFS_SLOPE_MASK) >> TSFS_SLOPE_SHIFT);
4790 x = I915_READ8(TR1);
4791
4792 b = tsfs & TSFS_INTR_MASK;
4793
4794 return ((m * x) / 127) - b;
4795}
4796
4797static u16 pvid_to_extvid(struct drm_i915_private *dev_priv, u8 pxvid)
4798{
3d13ef2e 4799 struct drm_device *dev = dev_priv->dev;
eb48eb00
DV
4800 static const struct v_table {
4801 u16 vd; /* in .1 mil */
4802 u16 vm; /* in .1 mil */
4803 } v_table[] = {
4804 { 0, 0, },
4805 { 375, 0, },
4806 { 500, 0, },
4807 { 625, 0, },
4808 { 750, 0, },
4809 { 875, 0, },
4810 { 1000, 0, },
4811 { 1125, 0, },
4812 { 4125, 3000, },
4813 { 4125, 3000, },
4814 { 4125, 3000, },
4815 { 4125, 3000, },
4816 { 4125, 3000, },
4817 { 4125, 3000, },
4818 { 4125, 3000, },
4819 { 4125, 3000, },
4820 { 4125, 3000, },
4821 { 4125, 3000, },
4822 { 4125, 3000, },
4823 { 4125, 3000, },
4824 { 4125, 3000, },
4825 { 4125, 3000, },
4826 { 4125, 3000, },
4827 { 4125, 3000, },
4828 { 4125, 3000, },
4829 { 4125, 3000, },
4830 { 4125, 3000, },
4831 { 4125, 3000, },
4832 { 4125, 3000, },
4833 { 4125, 3000, },
4834 { 4125, 3000, },
4835 { 4125, 3000, },
4836 { 4250, 3125, },
4837 { 4375, 3250, },
4838 { 4500, 3375, },
4839 { 4625, 3500, },
4840 { 4750, 3625, },
4841 { 4875, 3750, },
4842 { 5000, 3875, },
4843 { 5125, 4000, },
4844 { 5250, 4125, },
4845 { 5375, 4250, },
4846 { 5500, 4375, },
4847 { 5625, 4500, },
4848 { 5750, 4625, },
4849 { 5875, 4750, },
4850 { 6000, 4875, },
4851 { 6125, 5000, },
4852 { 6250, 5125, },
4853 { 6375, 5250, },
4854 { 6500, 5375, },
4855 { 6625, 5500, },
4856 { 6750, 5625, },
4857 { 6875, 5750, },
4858 { 7000, 5875, },
4859 { 7125, 6000, },
4860 { 7250, 6125, },
4861 { 7375, 6250, },
4862 { 7500, 6375, },
4863 { 7625, 6500, },
4864 { 7750, 6625, },
4865 { 7875, 6750, },
4866 { 8000, 6875, },
4867 { 8125, 7000, },
4868 { 8250, 7125, },
4869 { 8375, 7250, },
4870 { 8500, 7375, },
4871 { 8625, 7500, },
4872 { 8750, 7625, },
4873 { 8875, 7750, },
4874 { 9000, 7875, },
4875 { 9125, 8000, },
4876 { 9250, 8125, },
4877 { 9375, 8250, },
4878 { 9500, 8375, },
4879 { 9625, 8500, },
4880 { 9750, 8625, },
4881 { 9875, 8750, },
4882 { 10000, 8875, },
4883 { 10125, 9000, },
4884 { 10250, 9125, },
4885 { 10375, 9250, },
4886 { 10500, 9375, },
4887 { 10625, 9500, },
4888 { 10750, 9625, },
4889 { 10875, 9750, },
4890 { 11000, 9875, },
4891 { 11125, 10000, },
4892 { 11250, 10125, },
4893 { 11375, 10250, },
4894 { 11500, 10375, },
4895 { 11625, 10500, },
4896 { 11750, 10625, },
4897 { 11875, 10750, },
4898 { 12000, 10875, },
4899 { 12125, 11000, },
4900 { 12250, 11125, },
4901 { 12375, 11250, },
4902 { 12500, 11375, },
4903 { 12625, 11500, },
4904 { 12750, 11625, },
4905 { 12875, 11750, },
4906 { 13000, 11875, },
4907 { 13125, 12000, },
4908 { 13250, 12125, },
4909 { 13375, 12250, },
4910 { 13500, 12375, },
4911 { 13625, 12500, },
4912 { 13750, 12625, },
4913 { 13875, 12750, },
4914 { 14000, 12875, },
4915 { 14125, 13000, },
4916 { 14250, 13125, },
4917 { 14375, 13250, },
4918 { 14500, 13375, },
4919 { 14625, 13500, },
4920 { 14750, 13625, },
4921 { 14875, 13750, },
4922 { 15000, 13875, },
4923 { 15125, 14000, },
4924 { 15250, 14125, },
4925 { 15375, 14250, },
4926 { 15500, 14375, },
4927 { 15625, 14500, },
4928 { 15750, 14625, },
4929 { 15875, 14750, },
4930 { 16000, 14875, },
4931 { 16125, 15000, },
4932 };
3d13ef2e 4933 if (INTEL_INFO(dev)->is_mobile)
eb48eb00
DV
4934 return v_table[pxvid].vm;
4935 else
4936 return v_table[pxvid].vd;
4937}
4938
02d71956 4939static void __i915_update_gfx_val(struct drm_i915_private *dev_priv)
eb48eb00 4940{
5ed0bdf2 4941 u64 now, diff, diffms;
eb48eb00
DV
4942 u32 count;
4943
02d71956 4944 assert_spin_locked(&mchdev_lock);
eb48eb00 4945
5ed0bdf2
TG
4946 now = ktime_get_raw_ns();
4947 diffms = now - dev_priv->ips.last_time2;
4948 do_div(diffms, NSEC_PER_MSEC);
eb48eb00
DV
4949
4950 /* Don't divide by 0 */
eb48eb00
DV
4951 if (!diffms)
4952 return;
4953
4954 count = I915_READ(GFXEC);
4955
20e4d407
DV
4956 if (count < dev_priv->ips.last_count2) {
4957 diff = ~0UL - dev_priv->ips.last_count2;
eb48eb00
DV
4958 diff += count;
4959 } else {
20e4d407 4960 diff = count - dev_priv->ips.last_count2;
eb48eb00
DV
4961 }
4962
20e4d407
DV
4963 dev_priv->ips.last_count2 = count;
4964 dev_priv->ips.last_time2 = now;
eb48eb00
DV
4965
4966 /* More magic constants... */
4967 diff = diff * 1181;
4968 diff = div_u64(diff, diffms * 10);
20e4d407 4969 dev_priv->ips.gfx_power = diff;
eb48eb00
DV
4970}
4971
02d71956
DV
4972void i915_update_gfx_val(struct drm_i915_private *dev_priv)
4973{
3d13ef2e
DL
4974 struct drm_device *dev = dev_priv->dev;
4975
4976 if (INTEL_INFO(dev)->gen != 5)
02d71956
DV
4977 return;
4978
9270388e 4979 spin_lock_irq(&mchdev_lock);
02d71956
DV
4980
4981 __i915_update_gfx_val(dev_priv);
4982
9270388e 4983 spin_unlock_irq(&mchdev_lock);
02d71956
DV
4984}
4985
f531dcb2 4986static unsigned long __i915_gfx_val(struct drm_i915_private *dev_priv)
eb48eb00
DV
4987{
4988 unsigned long t, corr, state1, corr2, state2;
4989 u32 pxvid, ext_v;
4990
02d71956
DV
4991 assert_spin_locked(&mchdev_lock);
4992
b39fb297 4993 pxvid = I915_READ(PXVFREQ_BASE + (dev_priv->rps.cur_freq * 4));
eb48eb00
DV
4994 pxvid = (pxvid >> 24) & 0x7f;
4995 ext_v = pvid_to_extvid(dev_priv, pxvid);
4996
4997 state1 = ext_v;
4998
4999 t = i915_mch_val(dev_priv);
5000
5001 /* Revel in the empirically derived constants */
5002
5003 /* Correction factor in 1/100000 units */
5004 if (t > 80)
5005 corr = ((t * 2349) + 135940);
5006 else if (t >= 50)
5007 corr = ((t * 964) + 29317);
5008 else /* < 50 */
5009 corr = ((t * 301) + 1004);
5010
5011 corr = corr * ((150142 * state1) / 10000 - 78642);
5012 corr /= 100000;
20e4d407 5013 corr2 = (corr * dev_priv->ips.corr);
eb48eb00
DV
5014
5015 state2 = (corr2 * state1) / 10000;
5016 state2 /= 100; /* convert to mW */
5017
02d71956 5018 __i915_update_gfx_val(dev_priv);
eb48eb00 5019
20e4d407 5020 return dev_priv->ips.gfx_power + state2;
eb48eb00
DV
5021}
5022
f531dcb2
CW
5023unsigned long i915_gfx_val(struct drm_i915_private *dev_priv)
5024{
3d13ef2e 5025 struct drm_device *dev = dev_priv->dev;
f531dcb2
CW
5026 unsigned long val;
5027
3d13ef2e 5028 if (INTEL_INFO(dev)->gen != 5)
f531dcb2
CW
5029 return 0;
5030
5031 spin_lock_irq(&mchdev_lock);
5032
5033 val = __i915_gfx_val(dev_priv);
5034
5035 spin_unlock_irq(&mchdev_lock);
5036
5037 return val;
5038}
5039
eb48eb00
DV
5040/**
5041 * i915_read_mch_val - return value for IPS use
5042 *
5043 * Calculate and return a value for the IPS driver to use when deciding whether
5044 * we have thermal and power headroom to increase CPU or GPU power budget.
5045 */
5046unsigned long i915_read_mch_val(void)
5047{
5048 struct drm_i915_private *dev_priv;
5049 unsigned long chipset_val, graphics_val, ret = 0;
5050
9270388e 5051 spin_lock_irq(&mchdev_lock);
eb48eb00
DV
5052 if (!i915_mch_dev)
5053 goto out_unlock;
5054 dev_priv = i915_mch_dev;
5055
f531dcb2
CW
5056 chipset_val = __i915_chipset_val(dev_priv);
5057 graphics_val = __i915_gfx_val(dev_priv);
eb48eb00
DV
5058
5059 ret = chipset_val + graphics_val;
5060
5061out_unlock:
9270388e 5062 spin_unlock_irq(&mchdev_lock);
eb48eb00
DV
5063
5064 return ret;
5065}
5066EXPORT_SYMBOL_GPL(i915_read_mch_val);
5067
5068/**
5069 * i915_gpu_raise - raise GPU frequency limit
5070 *
5071 * Raise the limit; IPS indicates we have thermal headroom.
5072 */
5073bool i915_gpu_raise(void)
5074{
5075 struct drm_i915_private *dev_priv;
5076 bool ret = true;
5077
9270388e 5078 spin_lock_irq(&mchdev_lock);
eb48eb00
DV
5079 if (!i915_mch_dev) {
5080 ret = false;
5081 goto out_unlock;
5082 }
5083 dev_priv = i915_mch_dev;
5084
20e4d407
DV
5085 if (dev_priv->ips.max_delay > dev_priv->ips.fmax)
5086 dev_priv->ips.max_delay--;
eb48eb00
DV
5087
5088out_unlock:
9270388e 5089 spin_unlock_irq(&mchdev_lock);
eb48eb00
DV
5090
5091 return ret;
5092}
5093EXPORT_SYMBOL_GPL(i915_gpu_raise);
5094
5095/**
5096 * i915_gpu_lower - lower GPU frequency limit
5097 *
5098 * IPS indicates we're close to a thermal limit, so throttle back the GPU
5099 * frequency maximum.
5100 */
5101bool i915_gpu_lower(void)
5102{
5103 struct drm_i915_private *dev_priv;
5104 bool ret = true;
5105
9270388e 5106 spin_lock_irq(&mchdev_lock);
eb48eb00
DV
5107 if (!i915_mch_dev) {
5108 ret = false;
5109 goto out_unlock;
5110 }
5111 dev_priv = i915_mch_dev;
5112
20e4d407
DV
5113 if (dev_priv->ips.max_delay < dev_priv->ips.min_delay)
5114 dev_priv->ips.max_delay++;
eb48eb00
DV
5115
5116out_unlock:
9270388e 5117 spin_unlock_irq(&mchdev_lock);
eb48eb00
DV
5118
5119 return ret;
5120}
5121EXPORT_SYMBOL_GPL(i915_gpu_lower);
5122
5123/**
5124 * i915_gpu_busy - indicate GPU business to IPS
5125 *
5126 * Tell the IPS driver whether or not the GPU is busy.
5127 */
5128bool i915_gpu_busy(void)
5129{
5130 struct drm_i915_private *dev_priv;
a4872ba6 5131 struct intel_engine_cs *ring;
eb48eb00 5132 bool ret = false;
f047e395 5133 int i;
eb48eb00 5134
9270388e 5135 spin_lock_irq(&mchdev_lock);
eb48eb00
DV
5136 if (!i915_mch_dev)
5137 goto out_unlock;
5138 dev_priv = i915_mch_dev;
5139
f047e395
CW
5140 for_each_ring(ring, dev_priv, i)
5141 ret |= !list_empty(&ring->request_list);
eb48eb00
DV
5142
5143out_unlock:
9270388e 5144 spin_unlock_irq(&mchdev_lock);
eb48eb00
DV
5145
5146 return ret;
5147}
5148EXPORT_SYMBOL_GPL(i915_gpu_busy);
5149
5150/**
5151 * i915_gpu_turbo_disable - disable graphics turbo
5152 *
5153 * Disable graphics turbo by resetting the max frequency and setting the
5154 * current frequency to the default.
5155 */
5156bool i915_gpu_turbo_disable(void)
5157{
5158 struct drm_i915_private *dev_priv;
5159 bool ret = true;
5160
9270388e 5161 spin_lock_irq(&mchdev_lock);
eb48eb00
DV
5162 if (!i915_mch_dev) {
5163 ret = false;
5164 goto out_unlock;
5165 }
5166 dev_priv = i915_mch_dev;
5167
20e4d407 5168 dev_priv->ips.max_delay = dev_priv->ips.fstart;
eb48eb00 5169
20e4d407 5170 if (!ironlake_set_drps(dev_priv->dev, dev_priv->ips.fstart))
eb48eb00
DV
5171 ret = false;
5172
5173out_unlock:
9270388e 5174 spin_unlock_irq(&mchdev_lock);
eb48eb00
DV
5175
5176 return ret;
5177}
5178EXPORT_SYMBOL_GPL(i915_gpu_turbo_disable);
5179
5180/**
5181 * Tells the intel_ips driver that the i915 driver is now loaded, if
5182 * IPS got loaded first.
5183 *
5184 * This awkward dance is so that neither module has to depend on the
5185 * other in order for IPS to do the appropriate communication of
5186 * GPU turbo limits to i915.
5187 */
5188static void
5189ips_ping_for_i915_load(void)
5190{
5191 void (*link)(void);
5192
5193 link = symbol_get(ips_link_to_i915_driver);
5194 if (link) {
5195 link();
5196 symbol_put(ips_link_to_i915_driver);
5197 }
5198}
5199
5200void intel_gpu_ips_init(struct drm_i915_private *dev_priv)
5201{
02d71956
DV
5202 /* We only register the i915 ips part with intel-ips once everything is
5203 * set up, to avoid intel-ips sneaking in and reading bogus values. */
9270388e 5204 spin_lock_irq(&mchdev_lock);
eb48eb00 5205 i915_mch_dev = dev_priv;
9270388e 5206 spin_unlock_irq(&mchdev_lock);
eb48eb00
DV
5207
5208 ips_ping_for_i915_load();
5209}
5210
5211void intel_gpu_ips_teardown(void)
5212{
9270388e 5213 spin_lock_irq(&mchdev_lock);
eb48eb00 5214 i915_mch_dev = NULL;
9270388e 5215 spin_unlock_irq(&mchdev_lock);
eb48eb00 5216}
76c3552f 5217
8090c6b9 5218static void intel_init_emon(struct drm_device *dev)
dde18883
ED
5219{
5220 struct drm_i915_private *dev_priv = dev->dev_private;
5221 u32 lcfuse;
5222 u8 pxw[16];
5223 int i;
5224
5225 /* Disable to program */
5226 I915_WRITE(ECR, 0);
5227 POSTING_READ(ECR);
5228
5229 /* Program energy weights for various events */
5230 I915_WRITE(SDEW, 0x15040d00);
5231 I915_WRITE(CSIEW0, 0x007f0000);
5232 I915_WRITE(CSIEW1, 0x1e220004);
5233 I915_WRITE(CSIEW2, 0x04000004);
5234
5235 for (i = 0; i < 5; i++)
5236 I915_WRITE(PEW + (i * 4), 0);
5237 for (i = 0; i < 3; i++)
5238 I915_WRITE(DEW + (i * 4), 0);
5239
5240 /* Program P-state weights to account for frequency power adjustment */
5241 for (i = 0; i < 16; i++) {
5242 u32 pxvidfreq = I915_READ(PXVFREQ_BASE + (i * 4));
5243 unsigned long freq = intel_pxfreq(pxvidfreq);
5244 unsigned long vid = (pxvidfreq & PXVFREQ_PX_MASK) >>
5245 PXVFREQ_PX_SHIFT;
5246 unsigned long val;
5247
5248 val = vid * vid;
5249 val *= (freq / 1000);
5250 val *= 255;
5251 val /= (127*127*900);
5252 if (val > 0xff)
5253 DRM_ERROR("bad pxval: %ld\n", val);
5254 pxw[i] = val;
5255 }
5256 /* Render standby states get 0 weight */
5257 pxw[14] = 0;
5258 pxw[15] = 0;
5259
5260 for (i = 0; i < 4; i++) {
5261 u32 val = (pxw[i*4] << 24) | (pxw[(i*4)+1] << 16) |
5262 (pxw[(i*4)+2] << 8) | (pxw[(i*4)+3]);
5263 I915_WRITE(PXW + (i * 4), val);
5264 }
5265
5266 /* Adjust magic regs to magic values (more experimental results) */
5267 I915_WRITE(OGW0, 0);
5268 I915_WRITE(OGW1, 0);
5269 I915_WRITE(EG0, 0x00007f00);
5270 I915_WRITE(EG1, 0x0000000e);
5271 I915_WRITE(EG2, 0x000e0000);
5272 I915_WRITE(EG3, 0x68000300);
5273 I915_WRITE(EG4, 0x42000000);
5274 I915_WRITE(EG5, 0x00140031);
5275 I915_WRITE(EG6, 0);
5276 I915_WRITE(EG7, 0);
5277
5278 for (i = 0; i < 8; i++)
5279 I915_WRITE(PXWL + (i * 4), 0);
5280
5281 /* Enable PMON + select events */
5282 I915_WRITE(ECR, 0x80000019);
5283
5284 lcfuse = I915_READ(LCFUSE02);
5285
20e4d407 5286 dev_priv->ips.corr = (lcfuse & LCFUSE_HIV_MASK);
dde18883
ED
5287}
5288
ae48434c
ID
5289void intel_init_gt_powersave(struct drm_device *dev)
5290{
e6069ca8
ID
5291 i915.enable_rc6 = sanitize_rc6_option(dev, i915.enable_rc6);
5292
38807746
D
5293 if (IS_CHERRYVIEW(dev))
5294 cherryview_init_gt_powersave(dev);
5295 else if (IS_VALLEYVIEW(dev))
4e80519e 5296 valleyview_init_gt_powersave(dev);
ae48434c
ID
5297}
5298
5299void intel_cleanup_gt_powersave(struct drm_device *dev)
5300{
38807746
D
5301 if (IS_CHERRYVIEW(dev))
5302 return;
5303 else if (IS_VALLEYVIEW(dev))
4e80519e 5304 valleyview_cleanup_gt_powersave(dev);
ae48434c
ID
5305}
5306
156c7ca0
JB
5307/**
5308 * intel_suspend_gt_powersave - suspend PM work and helper threads
5309 * @dev: drm device
5310 *
5311 * We don't want to disable RC6 or other features here, we just want
5312 * to make sure any work we've queued has finished and won't bother
5313 * us while we're suspended.
5314 */
5315void intel_suspend_gt_powersave(struct drm_device *dev)
5316{
5317 struct drm_i915_private *dev_priv = dev->dev_private;
5318
5319 /* Interrupts should be disabled already to avoid re-arming. */
9df7575f 5320 WARN_ON(intel_irqs_enabled(dev_priv));
156c7ca0
JB
5321
5322 flush_delayed_work(&dev_priv->rps.delayed_resume_work);
5323
5324 cancel_work_sync(&dev_priv->rps.work);
b47adc17
D
5325
5326 /* Force GPU to min freq during suspend */
5327 gen6_rps_idle(dev_priv);
156c7ca0
JB
5328}
5329
8090c6b9
DV
5330void intel_disable_gt_powersave(struct drm_device *dev)
5331{
1a01ab3b
JB
5332 struct drm_i915_private *dev_priv = dev->dev_private;
5333
fd0c0642 5334 /* Interrupts should be disabled already to avoid re-arming. */
9df7575f 5335 WARN_ON(intel_irqs_enabled(dev_priv));
fd0c0642 5336
930ebb46 5337 if (IS_IRONLAKE_M(dev)) {
8090c6b9 5338 ironlake_disable_drps(dev);
930ebb46 5339 ironlake_disable_rc6(dev);
38807746 5340 } else if (INTEL_INFO(dev)->gen >= 6) {
10d8d366 5341 intel_suspend_gt_powersave(dev);
e494837a 5342
4fc688ce 5343 mutex_lock(&dev_priv->rps.hw_lock);
38807746
D
5344 if (IS_CHERRYVIEW(dev))
5345 cherryview_disable_rps(dev);
5346 else if (IS_VALLEYVIEW(dev))
d20d4f0c
JB
5347 valleyview_disable_rps(dev);
5348 else
5349 gen6_disable_rps(dev);
c0951f0c 5350 dev_priv->rps.enabled = false;
4fc688ce 5351 mutex_unlock(&dev_priv->rps.hw_lock);
930ebb46 5352 }
8090c6b9
DV
5353}
5354
1a01ab3b
JB
5355static void intel_gen6_powersave_work(struct work_struct *work)
5356{
5357 struct drm_i915_private *dev_priv =
5358 container_of(work, struct drm_i915_private,
5359 rps.delayed_resume_work.work);
5360 struct drm_device *dev = dev_priv->dev;
5361
c76bb61a
DS
5362 dev_priv->rps.is_bdw_sw_turbo = false;
5363
4fc688ce 5364 mutex_lock(&dev_priv->rps.hw_lock);
0a073b84 5365
38807746
D
5366 if (IS_CHERRYVIEW(dev)) {
5367 cherryview_enable_rps(dev);
5368 } else if (IS_VALLEYVIEW(dev)) {
0a073b84 5369 valleyview_enable_rps(dev);
6edee7f3
BW
5370 } else if (IS_BROADWELL(dev)) {
5371 gen8_enable_rps(dev);
c2bc2fc5 5372 __gen6_update_ring_freq(dev);
0a073b84
JB
5373 } else {
5374 gen6_enable_rps(dev);
c2bc2fc5 5375 __gen6_update_ring_freq(dev);
0a073b84 5376 }
c0951f0c 5377 dev_priv->rps.enabled = true;
4fc688ce 5378 mutex_unlock(&dev_priv->rps.hw_lock);
c6df39b5
ID
5379
5380 intel_runtime_pm_put(dev_priv);
1a01ab3b
JB
5381}
5382
8090c6b9
DV
5383void intel_enable_gt_powersave(struct drm_device *dev)
5384{
1a01ab3b
JB
5385 struct drm_i915_private *dev_priv = dev->dev_private;
5386
8090c6b9 5387 if (IS_IRONLAKE_M(dev)) {
dc1d0136 5388 mutex_lock(&dev->struct_mutex);
8090c6b9
DV
5389 ironlake_enable_drps(dev);
5390 ironlake_enable_rc6(dev);
5391 intel_init_emon(dev);
dc1d0136 5392 mutex_unlock(&dev->struct_mutex);
38807746 5393 } else if (INTEL_INFO(dev)->gen >= 6) {
1a01ab3b
JB
5394 /*
5395 * PCU communication is slow and this doesn't need to be
5396 * done at any specific time, so do this out of our fast path
5397 * to make resume and init faster.
c6df39b5
ID
5398 *
5399 * We depend on the HW RC6 power context save/restore
5400 * mechanism when entering D3 through runtime PM suspend. So
5401 * disable RPM until RPS/RC6 is properly setup. We can only
5402 * get here via the driver load/system resume/runtime resume
5403 * paths, so the _noresume version is enough (and in case of
5404 * runtime resume it's necessary).
1a01ab3b 5405 */
c6df39b5
ID
5406 if (schedule_delayed_work(&dev_priv->rps.delayed_resume_work,
5407 round_jiffies_up_relative(HZ)))
5408 intel_runtime_pm_get_noresume(dev_priv);
8090c6b9
DV
5409 }
5410}
5411
c6df39b5
ID
5412void intel_reset_gt_powersave(struct drm_device *dev)
5413{
5414 struct drm_i915_private *dev_priv = dev->dev_private;
5415
5416 dev_priv->rps.enabled = false;
5417 intel_enable_gt_powersave(dev);
5418}
5419
3107bd48
DV
5420static void ibx_init_clock_gating(struct drm_device *dev)
5421{
5422 struct drm_i915_private *dev_priv = dev->dev_private;
5423
5424 /*
5425 * On Ibex Peak and Cougar Point, we need to disable clock
5426 * gating for the panel power sequencer or it will fail to
5427 * start up when no ports are active.
5428 */
5429 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE);
5430}
5431
0e088b8f
VS
5432static void g4x_disable_trickle_feed(struct drm_device *dev)
5433{
5434 struct drm_i915_private *dev_priv = dev->dev_private;
5435 int pipe;
5436
055e393f 5437 for_each_pipe(dev_priv, pipe) {
0e088b8f
VS
5438 I915_WRITE(DSPCNTR(pipe),
5439 I915_READ(DSPCNTR(pipe)) |
5440 DISPPLANE_TRICKLE_FEED_DISABLE);
1dba99f4 5441 intel_flush_primary_plane(dev_priv, pipe);
0e088b8f
VS
5442 }
5443}
5444
017636cc
VS
5445static void ilk_init_lp_watermarks(struct drm_device *dev)
5446{
5447 struct drm_i915_private *dev_priv = dev->dev_private;
5448
5449 I915_WRITE(WM3_LP_ILK, I915_READ(WM3_LP_ILK) & ~WM1_LP_SR_EN);
5450 I915_WRITE(WM2_LP_ILK, I915_READ(WM2_LP_ILK) & ~WM1_LP_SR_EN);
5451 I915_WRITE(WM1_LP_ILK, I915_READ(WM1_LP_ILK) & ~WM1_LP_SR_EN);
5452
5453 /*
5454 * Don't touch WM1S_LP_EN here.
5455 * Doing so could cause underruns.
5456 */
5457}
5458
1fa61106 5459static void ironlake_init_clock_gating(struct drm_device *dev)
6f1d69b0
ED
5460{
5461 struct drm_i915_private *dev_priv = dev->dev_private;
231e54f6 5462 uint32_t dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
6f1d69b0 5463
f1e8fa56
DL
5464 /*
5465 * Required for FBC
5466 * WaFbcDisableDpfcClockGating:ilk
5467 */
4d47e4f5
DL
5468 dspclk_gate |= ILK_DPFCRUNIT_CLOCK_GATE_DISABLE |
5469 ILK_DPFCUNIT_CLOCK_GATE_DISABLE |
5470 ILK_DPFDUNIT_CLOCK_GATE_ENABLE;
6f1d69b0
ED
5471
5472 I915_WRITE(PCH_3DCGDIS0,
5473 MARIUNIT_CLOCK_GATE_DISABLE |
5474 SVSMUNIT_CLOCK_GATE_DISABLE);
5475 I915_WRITE(PCH_3DCGDIS1,
5476 VFMUNIT_CLOCK_GATE_DISABLE);
5477
6f1d69b0
ED
5478 /*
5479 * According to the spec the following bits should be set in
5480 * order to enable memory self-refresh
5481 * The bit 22/21 of 0x42004
5482 * The bit 5 of 0x42020
5483 * The bit 15 of 0x45000
5484 */
5485 I915_WRITE(ILK_DISPLAY_CHICKEN2,
5486 (I915_READ(ILK_DISPLAY_CHICKEN2) |
5487 ILK_DPARB_GATE | ILK_VSDPFD_FULL));
4d47e4f5 5488 dspclk_gate |= ILK_DPARBUNIT_CLOCK_GATE_ENABLE;
6f1d69b0
ED
5489 I915_WRITE(DISP_ARB_CTL,
5490 (I915_READ(DISP_ARB_CTL) |
5491 DISP_FBC_WM_DIS));
017636cc
VS
5492
5493 ilk_init_lp_watermarks(dev);
6f1d69b0
ED
5494
5495 /*
5496 * Based on the document from hardware guys the following bits
5497 * should be set unconditionally in order to enable FBC.
5498 * The bit 22 of 0x42000
5499 * The bit 22 of 0x42004
5500 * The bit 7,8,9 of 0x42020.
5501 */
5502 if (IS_IRONLAKE_M(dev)) {
4bb35334 5503 /* WaFbcAsynchFlipDisableFbcQueue:ilk */
6f1d69b0
ED
5504 I915_WRITE(ILK_DISPLAY_CHICKEN1,
5505 I915_READ(ILK_DISPLAY_CHICKEN1) |
5506 ILK_FBCQ_DIS);
5507 I915_WRITE(ILK_DISPLAY_CHICKEN2,
5508 I915_READ(ILK_DISPLAY_CHICKEN2) |
5509 ILK_DPARB_GATE);
6f1d69b0
ED
5510 }
5511
4d47e4f5
DL
5512 I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
5513
6f1d69b0
ED
5514 I915_WRITE(ILK_DISPLAY_CHICKEN2,
5515 I915_READ(ILK_DISPLAY_CHICKEN2) |
5516 ILK_ELPIN_409_SELECT);
5517 I915_WRITE(_3D_CHICKEN2,
5518 _3D_CHICKEN2_WM_READ_PIPELINED << 16 |
5519 _3D_CHICKEN2_WM_READ_PIPELINED);
4358a374 5520
ecdb4eb7 5521 /* WaDisableRenderCachePipelinedFlush:ilk */
4358a374
DV
5522 I915_WRITE(CACHE_MODE_0,
5523 _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
3107bd48 5524
4e04632e
AG
5525 /* WaDisable_RenderCache_OperationalFlush:ilk */
5526 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
5527
0e088b8f 5528 g4x_disable_trickle_feed(dev);
bdad2b2f 5529
3107bd48
DV
5530 ibx_init_clock_gating(dev);
5531}
5532
5533static void cpt_init_clock_gating(struct drm_device *dev)
5534{
5535 struct drm_i915_private *dev_priv = dev->dev_private;
5536 int pipe;
3f704fa2 5537 uint32_t val;
3107bd48
DV
5538
5539 /*
5540 * On Ibex Peak and Cougar Point, we need to disable clock
5541 * gating for the panel power sequencer or it will fail to
5542 * start up when no ports are active.
5543 */
cd664078
JB
5544 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE |
5545 PCH_DPLUNIT_CLOCK_GATE_DISABLE |
5546 PCH_CPUNIT_CLOCK_GATE_DISABLE);
3107bd48
DV
5547 I915_WRITE(SOUTH_CHICKEN2, I915_READ(SOUTH_CHICKEN2) |
5548 DPLS_EDP_PPS_FIX_DIS);
335c07b7
TI
5549 /* The below fixes the weird display corruption, a few pixels shifted
5550 * downward, on (only) LVDS of some HP laptops with IVY.
5551 */
055e393f 5552 for_each_pipe(dev_priv, pipe) {
dc4bd2d1
PZ
5553 val = I915_READ(TRANS_CHICKEN2(pipe));
5554 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
5555 val &= ~TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
41aa3448 5556 if (dev_priv->vbt.fdi_rx_polarity_inverted)
3f704fa2 5557 val |= TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
dc4bd2d1
PZ
5558 val &= ~TRANS_CHICKEN2_FRAME_START_DELAY_MASK;
5559 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_COUNTER;
5560 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_MODESWITCH;
3f704fa2
PZ
5561 I915_WRITE(TRANS_CHICKEN2(pipe), val);
5562 }
3107bd48 5563 /* WADP0ClockGatingDisable */
055e393f 5564 for_each_pipe(dev_priv, pipe) {
3107bd48
DV
5565 I915_WRITE(TRANS_CHICKEN1(pipe),
5566 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
5567 }
6f1d69b0
ED
5568}
5569
1d7aaa0c
DV
5570static void gen6_check_mch_setup(struct drm_device *dev)
5571{
5572 struct drm_i915_private *dev_priv = dev->dev_private;
5573 uint32_t tmp;
5574
5575 tmp = I915_READ(MCH_SSKPD);
df662a28
DV
5576 if ((tmp & MCH_SSKPD_WM0_MASK) != MCH_SSKPD_WM0_VAL)
5577 DRM_DEBUG_KMS("Wrong MCH_SSKPD value: 0x%08x This can cause underruns.\n",
5578 tmp);
1d7aaa0c
DV
5579}
5580
1fa61106 5581static void gen6_init_clock_gating(struct drm_device *dev)
6f1d69b0
ED
5582{
5583 struct drm_i915_private *dev_priv = dev->dev_private;
231e54f6 5584 uint32_t dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
6f1d69b0 5585
231e54f6 5586 I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
6f1d69b0
ED
5587
5588 I915_WRITE(ILK_DISPLAY_CHICKEN2,
5589 I915_READ(ILK_DISPLAY_CHICKEN2) |
5590 ILK_ELPIN_409_SELECT);
5591
ecdb4eb7 5592 /* WaDisableHiZPlanesWhenMSAAEnabled:snb */
4283908e
DV
5593 I915_WRITE(_3D_CHICKEN,
5594 _MASKED_BIT_ENABLE(_3D_CHICKEN_HIZ_PLANE_DISABLE_MSAA_4X_SNB));
5595
ecdb4eb7 5596 /* WaSetupGtModeTdRowDispatch:snb */
6547fbdb
DV
5597 if (IS_SNB_GT1(dev))
5598 I915_WRITE(GEN6_GT_MODE,
5599 _MASKED_BIT_ENABLE(GEN6_TD_FOUR_ROW_DISPATCH_DISABLE));
5600
4e04632e
AG
5601 /* WaDisable_RenderCache_OperationalFlush:snb */
5602 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
5603
8d85d272
VS
5604 /*
5605 * BSpec recoomends 8x4 when MSAA is used,
5606 * however in practice 16x4 seems fastest.
c5c98a58
VS
5607 *
5608 * Note that PS/WM thread counts depend on the WIZ hashing
5609 * disable bit, which we don't touch here, but it's good
5610 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
8d85d272
VS
5611 */
5612 I915_WRITE(GEN6_GT_MODE,
5613 GEN6_WIZ_HASHING_MASK | GEN6_WIZ_HASHING_16x4);
5614
017636cc 5615 ilk_init_lp_watermarks(dev);
6f1d69b0 5616
6f1d69b0 5617 I915_WRITE(CACHE_MODE_0,
50743298 5618 _MASKED_BIT_DISABLE(CM0_STC_EVICT_DISABLE_LRA_SNB));
6f1d69b0
ED
5619
5620 I915_WRITE(GEN6_UCGCTL1,
5621 I915_READ(GEN6_UCGCTL1) |
5622 GEN6_BLBUNIT_CLOCK_GATE_DISABLE |
5623 GEN6_CSUNIT_CLOCK_GATE_DISABLE);
5624
5625 /* According to the BSpec vol1g, bit 12 (RCPBUNIT) clock
5626 * gating disable must be set. Failure to set it results in
5627 * flickering pixels due to Z write ordering failures after
5628 * some amount of runtime in the Mesa "fire" demo, and Unigine
5629 * Sanctuary and Tropics, and apparently anything else with
5630 * alpha test or pixel discard.
5631 *
5632 * According to the spec, bit 11 (RCCUNIT) must also be set,
5633 * but we didn't debug actual testcases to find it out.
0f846f81 5634 *
ef59318c
VS
5635 * WaDisableRCCUnitClockGating:snb
5636 * WaDisableRCPBUnitClockGating:snb
6f1d69b0
ED
5637 */
5638 I915_WRITE(GEN6_UCGCTL2,
5639 GEN6_RCPBUNIT_CLOCK_GATE_DISABLE |
5640 GEN6_RCCUNIT_CLOCK_GATE_DISABLE);
5641
5eb146dd 5642 /* WaStripsFansDisableFastClipPerformanceFix:snb */
743b57d8
VS
5643 I915_WRITE(_3D_CHICKEN3,
5644 _MASKED_BIT_ENABLE(_3D_CHICKEN3_SF_DISABLE_FASTCLIP_CULL));
6f1d69b0 5645
e927ecde
VS
5646 /*
5647 * Bspec says:
5648 * "This bit must be set if 3DSTATE_CLIP clip mode is set to normal and
5649 * 3DSTATE_SF number of SF output attributes is more than 16."
5650 */
5651 I915_WRITE(_3D_CHICKEN3,
5652 _MASKED_BIT_ENABLE(_3D_CHICKEN3_SF_DISABLE_PIPELINED_ATTR_FETCH));
5653
6f1d69b0
ED
5654 /*
5655 * According to the spec the following bits should be
5656 * set in order to enable memory self-refresh and fbc:
5657 * The bit21 and bit22 of 0x42000
5658 * The bit21 and bit22 of 0x42004
5659 * The bit5 and bit7 of 0x42020
5660 * The bit14 of 0x70180
5661 * The bit14 of 0x71180
4bb35334
DL
5662 *
5663 * WaFbcAsynchFlipDisableFbcQueue:snb
6f1d69b0
ED
5664 */
5665 I915_WRITE(ILK_DISPLAY_CHICKEN1,
5666 I915_READ(ILK_DISPLAY_CHICKEN1) |
5667 ILK_FBCQ_DIS | ILK_PABSTRETCH_DIS);
5668 I915_WRITE(ILK_DISPLAY_CHICKEN2,
5669 I915_READ(ILK_DISPLAY_CHICKEN2) |
5670 ILK_DPARB_GATE | ILK_VSDPFD_FULL);
231e54f6
DL
5671 I915_WRITE(ILK_DSPCLK_GATE_D,
5672 I915_READ(ILK_DSPCLK_GATE_D) |
5673 ILK_DPARBUNIT_CLOCK_GATE_ENABLE |
5674 ILK_DPFDUNIT_CLOCK_GATE_ENABLE);
6f1d69b0 5675
0e088b8f 5676 g4x_disable_trickle_feed(dev);
f8f2ac9a 5677
3107bd48 5678 cpt_init_clock_gating(dev);
1d7aaa0c
DV
5679
5680 gen6_check_mch_setup(dev);
6f1d69b0
ED
5681}
5682
5683static void gen7_setup_fixed_func_scheduler(struct drm_i915_private *dev_priv)
5684{
5685 uint32_t reg = I915_READ(GEN7_FF_THREAD_MODE);
5686
3aad9059 5687 /*
46680e0a 5688 * WaVSThreadDispatchOverride:ivb,vlv
3aad9059
VS
5689 *
5690 * This actually overrides the dispatch
5691 * mode for all thread types.
5692 */
6f1d69b0
ED
5693 reg &= ~GEN7_FF_SCHED_MASK;
5694 reg |= GEN7_FF_TS_SCHED_HW;
5695 reg |= GEN7_FF_VS_SCHED_HW;
5696 reg |= GEN7_FF_DS_SCHED_HW;
5697
5698 I915_WRITE(GEN7_FF_THREAD_MODE, reg);
5699}
5700
17a303ec
PZ
5701static void lpt_init_clock_gating(struct drm_device *dev)
5702{
5703 struct drm_i915_private *dev_priv = dev->dev_private;
5704
5705 /*
5706 * TODO: this bit should only be enabled when really needed, then
5707 * disabled when not needed anymore in order to save power.
5708 */
5709 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE)
5710 I915_WRITE(SOUTH_DSPCLK_GATE_D,
5711 I915_READ(SOUTH_DSPCLK_GATE_D) |
5712 PCH_LP_PARTITION_LEVEL_DISABLE);
0a790cdb
PZ
5713
5714 /* WADPOClockGatingDisable:hsw */
5715 I915_WRITE(_TRANSA_CHICKEN1,
5716 I915_READ(_TRANSA_CHICKEN1) |
5717 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
17a303ec
PZ
5718}
5719
7d708ee4
ID
5720static void lpt_suspend_hw(struct drm_device *dev)
5721{
5722 struct drm_i915_private *dev_priv = dev->dev_private;
5723
5724 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
5725 uint32_t val = I915_READ(SOUTH_DSPCLK_GATE_D);
5726
5727 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
5728 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
5729 }
5730}
5731
47c2bd97 5732static void broadwell_init_clock_gating(struct drm_device *dev)
1020a5c2
BW
5733{
5734 struct drm_i915_private *dev_priv = dev->dev_private;
07d27e20 5735 enum pipe pipe;
1020a5c2
BW
5736
5737 I915_WRITE(WM3_LP_ILK, 0);
5738 I915_WRITE(WM2_LP_ILK, 0);
5739 I915_WRITE(WM1_LP_ILK, 0);
50ed5fbd
BW
5740
5741 /* FIXME(BDW): Check all the w/a, some might only apply to
5742 * pre-production hw. */
5743
c8966e10 5744
4afe8d33
BW
5745 I915_WRITE(GAMTARBMODE, _MASKED_BIT_ENABLE(ARB_MODE_BWGTLB_DISABLE));
5746
7f88da0c 5747 I915_WRITE(_3D_CHICKEN3,
b3f9ad93 5748 _MASKED_BIT_ENABLE(_3D_CHICKEN_SDE_LIMIT_FIFO_POLY_DEPTH(2)));
7f88da0c 5749
242a4018 5750
ab57fff1 5751 /* WaSwitchSolVfFArbitrationPriority:bdw */
50ed5fbd 5752 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
fe4ab3ce 5753
ab57fff1 5754 /* WaPsrDPAMaskVBlankInSRD:bdw */
fe4ab3ce
BW
5755 I915_WRITE(CHICKEN_PAR1_1,
5756 I915_READ(CHICKEN_PAR1_1) | DPA_MASK_VBLANK_SRD);
5757
ab57fff1 5758 /* WaPsrDPRSUnmaskVBlankInSRD:bdw */
055e393f 5759 for_each_pipe(dev_priv, pipe) {
07d27e20 5760 I915_WRITE(CHICKEN_PIPESL_1(pipe),
c7c65622 5761 I915_READ(CHICKEN_PIPESL_1(pipe)) |
8f670bb1 5762 BDW_DPRS_MASK_VBLANK_SRD);
fe4ab3ce 5763 }
63801f21 5764
ab57fff1
BW
5765 /* WaVSRefCountFullforceMissDisable:bdw */
5766 /* WaDSRefCountFullforceMissDisable:bdw */
5767 I915_WRITE(GEN7_FF_THREAD_MODE,
5768 I915_READ(GEN7_FF_THREAD_MODE) &
5769 ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
36075a4c 5770
295e8bb7
VS
5771 I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
5772 _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
4f1ca9e9
VS
5773
5774 /* WaDisableSDEUnitClockGating:bdw */
5775 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
5776 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
5d708680 5777
89d6b2b8 5778 lpt_init_clock_gating(dev);
1020a5c2
BW
5779}
5780
cad2a2d7
ED
5781static void haswell_init_clock_gating(struct drm_device *dev)
5782{
5783 struct drm_i915_private *dev_priv = dev->dev_private;
cad2a2d7 5784
017636cc 5785 ilk_init_lp_watermarks(dev);
cad2a2d7 5786
f3fc4884
FJ
5787 /* L3 caching of data atomics doesn't work -- disable it. */
5788 I915_WRITE(HSW_SCRATCH1, HSW_SCRATCH1_L3_DATA_ATOMICS_DISABLE);
5789 I915_WRITE(HSW_ROW_CHICKEN3,
5790 _MASKED_BIT_ENABLE(HSW_ROW_CHICKEN3_L3_GLOBAL_ATOMICS_DISABLE));
5791
ecdb4eb7 5792 /* This is required by WaCatErrorRejectionIssue:hsw */
cad2a2d7
ED
5793 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
5794 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
5795 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
5796
e36ea7ff
VS
5797 /* WaVSRefCountFullforceMissDisable:hsw */
5798 I915_WRITE(GEN7_FF_THREAD_MODE,
5799 I915_READ(GEN7_FF_THREAD_MODE) & ~GEN7_FF_VS_REF_CNT_FFME);
cad2a2d7 5800
4e04632e
AG
5801 /* WaDisable_RenderCache_OperationalFlush:hsw */
5802 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
5803
fe27c606
CW
5804 /* enable HiZ Raw Stall Optimization */
5805 I915_WRITE(CACHE_MODE_0_GEN7,
5806 _MASKED_BIT_DISABLE(HIZ_RAW_STALL_OPT_DISABLE));
5807
ecdb4eb7 5808 /* WaDisable4x2SubspanOptimization:hsw */
cad2a2d7
ED
5809 I915_WRITE(CACHE_MODE_1,
5810 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
1544d9d5 5811
a12c4967
VS
5812 /*
5813 * BSpec recommends 8x4 when MSAA is used,
5814 * however in practice 16x4 seems fastest.
c5c98a58
VS
5815 *
5816 * Note that PS/WM thread counts depend on the WIZ hashing
5817 * disable bit, which we don't touch here, but it's good
5818 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
a12c4967
VS
5819 */
5820 I915_WRITE(GEN7_GT_MODE,
5821 GEN6_WIZ_HASHING_MASK | GEN6_WIZ_HASHING_16x4);
5822
ecdb4eb7 5823 /* WaSwitchSolVfFArbitrationPriority:hsw */
e3dff585
BW
5824 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
5825
90a88643
PZ
5826 /* WaRsPkgCStateDisplayPMReq:hsw */
5827 I915_WRITE(CHICKEN_PAR1_1,
5828 I915_READ(CHICKEN_PAR1_1) | FORCE_ARB_IDLE_PLANES);
1544d9d5 5829
17a303ec 5830 lpt_init_clock_gating(dev);
cad2a2d7
ED
5831}
5832
1fa61106 5833static void ivybridge_init_clock_gating(struct drm_device *dev)
6f1d69b0
ED
5834{
5835 struct drm_i915_private *dev_priv = dev->dev_private;
20848223 5836 uint32_t snpcr;
6f1d69b0 5837
017636cc 5838 ilk_init_lp_watermarks(dev);
6f1d69b0 5839
231e54f6 5840 I915_WRITE(ILK_DSPCLK_GATE_D, ILK_VRHUNIT_CLOCK_GATE_DISABLE);
6f1d69b0 5841
ecdb4eb7 5842 /* WaDisableEarlyCull:ivb */
87f8020e
JB
5843 I915_WRITE(_3D_CHICKEN3,
5844 _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
5845
ecdb4eb7 5846 /* WaDisableBackToBackFlipFix:ivb */
6f1d69b0
ED
5847 I915_WRITE(IVB_CHICKEN3,
5848 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
5849 CHICKEN3_DGMG_DONE_FIX_DISABLE);
5850
ecdb4eb7 5851 /* WaDisablePSDDualDispatchEnable:ivb */
12f3382b
JB
5852 if (IS_IVB_GT1(dev))
5853 I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
5854 _MASKED_BIT_ENABLE(GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
12f3382b 5855
4e04632e
AG
5856 /* WaDisable_RenderCache_OperationalFlush:ivb */
5857 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
5858
ecdb4eb7 5859 /* Apply the WaDisableRHWOOptimizationForRenderHang:ivb workaround. */
6f1d69b0
ED
5860 I915_WRITE(GEN7_COMMON_SLICE_CHICKEN1,
5861 GEN7_CSC1_RHWO_OPT_DISABLE_IN_RCC);
5862
ecdb4eb7 5863 /* WaApplyL3ControlAndL3ChickenMode:ivb */
6f1d69b0
ED
5864 I915_WRITE(GEN7_L3CNTLREG1,
5865 GEN7_WA_FOR_GEN7_L3_CONTROL);
5866 I915_WRITE(GEN7_L3_CHICKEN_MODE_REGISTER,
8ab43976
JB
5867 GEN7_WA_L3_CHICKEN_MODE);
5868 if (IS_IVB_GT1(dev))
5869 I915_WRITE(GEN7_ROW_CHICKEN2,
5870 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
412236c2
VS
5871 else {
5872 /* must write both registers */
5873 I915_WRITE(GEN7_ROW_CHICKEN2,
5874 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
8ab43976
JB
5875 I915_WRITE(GEN7_ROW_CHICKEN2_GT2,
5876 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
412236c2 5877 }
6f1d69b0 5878
ecdb4eb7 5879 /* WaForceL3Serialization:ivb */
61939d97
JB
5880 I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
5881 ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
5882
1b80a19a 5883 /*
0f846f81 5884 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
ecdb4eb7 5885 * This implements the WaDisableRCZUnitClockGating:ivb workaround.
0f846f81
JB
5886 */
5887 I915_WRITE(GEN6_UCGCTL2,
28acf3b2 5888 GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
0f846f81 5889
ecdb4eb7 5890 /* This is required by WaCatErrorRejectionIssue:ivb */
6f1d69b0
ED
5891 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
5892 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
5893 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
5894
0e088b8f 5895 g4x_disable_trickle_feed(dev);
6f1d69b0
ED
5896
5897 gen7_setup_fixed_func_scheduler(dev_priv);
97e1930f 5898
22721343
CW
5899 if (0) { /* causes HiZ corruption on ivb:gt1 */
5900 /* enable HiZ Raw Stall Optimization */
5901 I915_WRITE(CACHE_MODE_0_GEN7,
5902 _MASKED_BIT_DISABLE(HIZ_RAW_STALL_OPT_DISABLE));
5903 }
116f2b6d 5904
ecdb4eb7 5905 /* WaDisable4x2SubspanOptimization:ivb */
97e1930f
DV
5906 I915_WRITE(CACHE_MODE_1,
5907 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
20848223 5908
a607c1a4
VS
5909 /*
5910 * BSpec recommends 8x4 when MSAA is used,
5911 * however in practice 16x4 seems fastest.
c5c98a58
VS
5912 *
5913 * Note that PS/WM thread counts depend on the WIZ hashing
5914 * disable bit, which we don't touch here, but it's good
5915 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
a607c1a4
VS
5916 */
5917 I915_WRITE(GEN7_GT_MODE,
5918 GEN6_WIZ_HASHING_MASK | GEN6_WIZ_HASHING_16x4);
5919
20848223
BW
5920 snpcr = I915_READ(GEN6_MBCUNIT_SNPCR);
5921 snpcr &= ~GEN6_MBC_SNPCR_MASK;
5922 snpcr |= GEN6_MBC_SNPCR_MED;
5923 I915_WRITE(GEN6_MBCUNIT_SNPCR, snpcr);
3107bd48 5924
ab5c608b
BW
5925 if (!HAS_PCH_NOP(dev))
5926 cpt_init_clock_gating(dev);
1d7aaa0c
DV
5927
5928 gen6_check_mch_setup(dev);
6f1d69b0
ED
5929}
5930
1fa61106 5931static void valleyview_init_clock_gating(struct drm_device *dev)
6f1d69b0
ED
5932{
5933 struct drm_i915_private *dev_priv = dev->dev_private;
6f1d69b0 5934
d7fe0cc0 5935 I915_WRITE(DSPCLK_GATE_D, VRHUNIT_CLOCK_GATE_DISABLE);
6f1d69b0 5936
ecdb4eb7 5937 /* WaDisableEarlyCull:vlv */
87f8020e
JB
5938 I915_WRITE(_3D_CHICKEN3,
5939 _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
5940
ecdb4eb7 5941 /* WaDisableBackToBackFlipFix:vlv */
6f1d69b0
ED
5942 I915_WRITE(IVB_CHICKEN3,
5943 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
5944 CHICKEN3_DGMG_DONE_FIX_DISABLE);
5945
fad7d36e 5946 /* WaPsdDispatchEnable:vlv */
ecdb4eb7 5947 /* WaDisablePSDDualDispatchEnable:vlv */
12f3382b 5948 I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
d3bc0303
JB
5949 _MASKED_BIT_ENABLE(GEN7_MAX_PS_THREAD_DEP |
5950 GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
12f3382b 5951
4e04632e
AG
5952 /* WaDisable_RenderCache_OperationalFlush:vlv */
5953 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
5954
ecdb4eb7 5955 /* WaForceL3Serialization:vlv */
61939d97
JB
5956 I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
5957 ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
5958
ecdb4eb7 5959 /* WaDisableDopClockGating:vlv */
8ab43976
JB
5960 I915_WRITE(GEN7_ROW_CHICKEN2,
5961 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
5962
ecdb4eb7 5963 /* This is required by WaCatErrorRejectionIssue:vlv */
6f1d69b0
ED
5964 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
5965 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
5966 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
5967
46680e0a
VS
5968 gen7_setup_fixed_func_scheduler(dev_priv);
5969
3c0edaeb 5970 /*
0f846f81 5971 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
ecdb4eb7 5972 * This implements the WaDisableRCZUnitClockGating:vlv workaround.
0f846f81
JB
5973 */
5974 I915_WRITE(GEN6_UCGCTL2,
3c0edaeb 5975 GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
0f846f81 5976
c98f5062
AG
5977 /* WaDisableL3Bank2xClockGate:vlv
5978 * Disabling L3 clock gating- MMIO 940c[25] = 1
5979 * Set bit 25, to disable L3_BANK_2x_CLK_GATING */
5980 I915_WRITE(GEN7_UCGCTL4,
5981 I915_READ(GEN7_UCGCTL4) | GEN7_L3BANK2X_CLOCK_GATE_DISABLE);
e3f33d46 5982
e0d8d59b 5983 I915_WRITE(MI_ARB_VLV, MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE);
6f1d69b0 5984
afd58e79
VS
5985 /*
5986 * BSpec says this must be set, even though
5987 * WaDisable4x2SubspanOptimization isn't listed for VLV.
5988 */
6b26c86d
DV
5989 I915_WRITE(CACHE_MODE_1,
5990 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
7983117f 5991
031994ee
VS
5992 /*
5993 * WaIncreaseL3CreditsForVLVB0:vlv
5994 * This is the hardware default actually.
5995 */
5996 I915_WRITE(GEN7_L3SQCREG1, VLV_B0_WA_L3SQCREG1_VALUE);
5997
2d809570 5998 /*
ecdb4eb7 5999 * WaDisableVLVClockGating_VBIIssue:vlv
2d809570
JB
6000 * Disable clock gating on th GCFG unit to prevent a delay
6001 * in the reporting of vblank events.
6002 */
7a0d1eed 6003 I915_WRITE(VLV_GUNIT_CLOCK_GATE, GCFG_DIS);
6f1d69b0
ED
6004}
6005
a4565da8
VS
6006static void cherryview_init_clock_gating(struct drm_device *dev)
6007{
6008 struct drm_i915_private *dev_priv = dev->dev_private;
6009
6010 I915_WRITE(DSPCLK_GATE_D, VRHUNIT_CLOCK_GATE_DISABLE);
6011
6012 I915_WRITE(MI_ARB_VLV, MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE);
dd811e70 6013
232ce337
VS
6014 /* WaVSRefCountFullforceMissDisable:chv */
6015 /* WaDSRefCountFullforceMissDisable:chv */
6016 I915_WRITE(GEN7_FF_THREAD_MODE,
6017 I915_READ(GEN7_FF_THREAD_MODE) &
6018 ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
acea6f95
VS
6019
6020 /* WaDisableSemaphoreAndSyncFlipWait:chv */
6021 I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
6022 _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
0846697c
VS
6023
6024 /* WaDisableCSUnitClockGating:chv */
6025 I915_WRITE(GEN6_UCGCTL1, I915_READ(GEN6_UCGCTL1) |
6026 GEN6_CSUNIT_CLOCK_GATE_DISABLE);
c631780f
VS
6027
6028 /* WaDisableSDEUnitClockGating:chv */
6029 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
6030 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
e0d34ce7 6031
e4443e45
VS
6032 /* WaDisableGunitClockGating:chv (pre-production hw) */
6033 I915_WRITE(VLV_GUNIT_CLOCK_GATE, I915_READ(VLV_GUNIT_CLOCK_GATE) |
6034 GINT_DIS);
6035
6036 /* WaDisableFfDopClockGating:chv (pre-production hw) */
6037 I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
6038 _MASKED_BIT_ENABLE(GEN8_FF_DOP_CLOCK_GATE_DISABLE));
6039
6040 /* WaDisableDopClockGating:chv (pre-production hw) */
e4443e45
VS
6041 I915_WRITE(GEN6_UCGCTL1, I915_READ(GEN6_UCGCTL1) |
6042 GEN6_EU_TCUNIT_CLOCK_GATE_DISABLE);
a4565da8
VS
6043}
6044
1fa61106 6045static void g4x_init_clock_gating(struct drm_device *dev)
6f1d69b0
ED
6046{
6047 struct drm_i915_private *dev_priv = dev->dev_private;
6048 uint32_t dspclk_gate;
6049
6050 I915_WRITE(RENCLK_GATE_D1, 0);
6051 I915_WRITE(RENCLK_GATE_D2, VF_UNIT_CLOCK_GATE_DISABLE |
6052 GS_UNIT_CLOCK_GATE_DISABLE |
6053 CL_UNIT_CLOCK_GATE_DISABLE);
6054 I915_WRITE(RAMCLK_GATE_D, 0);
6055 dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE |
6056 OVRUNIT_CLOCK_GATE_DISABLE |
6057 OVCUNIT_CLOCK_GATE_DISABLE;
6058 if (IS_GM45(dev))
6059 dspclk_gate |= DSSUNIT_CLOCK_GATE_DISABLE;
6060 I915_WRITE(DSPCLK_GATE_D, dspclk_gate);
4358a374
DV
6061
6062 /* WaDisableRenderCachePipelinedFlush */
6063 I915_WRITE(CACHE_MODE_0,
6064 _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
de1aa629 6065
4e04632e
AG
6066 /* WaDisable_RenderCache_OperationalFlush:g4x */
6067 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
6068
0e088b8f 6069 g4x_disable_trickle_feed(dev);
6f1d69b0
ED
6070}
6071
1fa61106 6072static void crestline_init_clock_gating(struct drm_device *dev)
6f1d69b0
ED
6073{
6074 struct drm_i915_private *dev_priv = dev->dev_private;
6075
6076 I915_WRITE(RENCLK_GATE_D1, I965_RCC_CLOCK_GATE_DISABLE);
6077 I915_WRITE(RENCLK_GATE_D2, 0);
6078 I915_WRITE(DSPCLK_GATE_D, 0);
6079 I915_WRITE(RAMCLK_GATE_D, 0);
6080 I915_WRITE16(DEUC, 0);
20f94967
VS
6081 I915_WRITE(MI_ARB_STATE,
6082 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
4e04632e
AG
6083
6084 /* WaDisable_RenderCache_OperationalFlush:gen4 */
6085 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
6f1d69b0
ED
6086}
6087
1fa61106 6088static void broadwater_init_clock_gating(struct drm_device *dev)
6f1d69b0
ED
6089{
6090 struct drm_i915_private *dev_priv = dev->dev_private;
6091
6092 I915_WRITE(RENCLK_GATE_D1, I965_RCZ_CLOCK_GATE_DISABLE |
6093 I965_RCC_CLOCK_GATE_DISABLE |
6094 I965_RCPB_CLOCK_GATE_DISABLE |
6095 I965_ISC_CLOCK_GATE_DISABLE |
6096 I965_FBC_CLOCK_GATE_DISABLE);
6097 I915_WRITE(RENCLK_GATE_D2, 0);
20f94967
VS
6098 I915_WRITE(MI_ARB_STATE,
6099 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
4e04632e
AG
6100
6101 /* WaDisable_RenderCache_OperationalFlush:gen4 */
6102 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
6f1d69b0
ED
6103}
6104
1fa61106 6105static void gen3_init_clock_gating(struct drm_device *dev)
6f1d69b0
ED
6106{
6107 struct drm_i915_private *dev_priv = dev->dev_private;
6108 u32 dstate = I915_READ(D_STATE);
6109
6110 dstate |= DSTATE_PLL_D3_OFF | DSTATE_GFX_CLOCK_GATING |
6111 DSTATE_DOT_CLOCK_GATING;
6112 I915_WRITE(D_STATE, dstate);
13a86b85
CW
6113
6114 if (IS_PINEVIEW(dev))
6115 I915_WRITE(ECOSKPD, _MASKED_BIT_ENABLE(ECO_GATING_CX_ONLY));
974a3b0f
DV
6116
6117 /* IIR "flip pending" means done if this bit is set */
6118 I915_WRITE(ECOSKPD, _MASKED_BIT_DISABLE(ECO_FLIP_DONE));
12fabbcb
VS
6119
6120 /* interrupts should cause a wake up from C3 */
3299254f 6121 I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_AGPBUSY_INT_EN));
dbb42748
VS
6122
6123 /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
6124 I915_WRITE(MI_ARB_STATE, _MASKED_BIT_ENABLE(MI_ARB_C3_LP_WRITE_ENABLE));
6f1d69b0
ED
6125}
6126
1fa61106 6127static void i85x_init_clock_gating(struct drm_device *dev)
6f1d69b0
ED
6128{
6129 struct drm_i915_private *dev_priv = dev->dev_private;
6130
6131 I915_WRITE(RENCLK_GATE_D1, SV_CLOCK_GATE_DISABLE);
54e472ae
VS
6132
6133 /* interrupts should cause a wake up from C3 */
6134 I915_WRITE(MI_STATE, _MASKED_BIT_ENABLE(MI_AGPBUSY_INT_EN) |
6135 _MASKED_BIT_DISABLE(MI_AGPBUSY_830_MODE));
6f1d69b0
ED
6136}
6137
1fa61106 6138static void i830_init_clock_gating(struct drm_device *dev)
6f1d69b0
ED
6139{
6140 struct drm_i915_private *dev_priv = dev->dev_private;
6141
6142 I915_WRITE(DSPCLK_GATE_D, OVRUNIT_CLOCK_GATE_DISABLE);
6143}
6144
6f1d69b0
ED
6145void intel_init_clock_gating(struct drm_device *dev)
6146{
6147 struct drm_i915_private *dev_priv = dev->dev_private;
6148
6149 dev_priv->display.init_clock_gating(dev);
6f1d69b0
ED
6150}
6151
7d708ee4
ID
6152void intel_suspend_hw(struct drm_device *dev)
6153{
6154 if (HAS_PCH_LPT(dev))
6155 lpt_suspend_hw(dev);
6156}
6157
c1ca727f
ID
6158#define for_each_power_well(i, power_well, domain_mask, power_domains) \
6159 for (i = 0; \
6160 i < (power_domains)->power_well_count && \
6161 ((power_well) = &(power_domains)->power_wells[i]); \
6162 i++) \
6163 if ((power_well)->domains & (domain_mask))
6164
6165#define for_each_power_well_rev(i, power_well, domain_mask, power_domains) \
6166 for (i = (power_domains)->power_well_count - 1; \
6167 i >= 0 && ((power_well) = &(power_domains)->power_wells[i]);\
6168 i--) \
6169 if ((power_well)->domains & (domain_mask))
6170
15d199ea
PZ
6171/**
6172 * We should only use the power well if we explicitly asked the hardware to
6173 * enable it, so check if it's enabled and also check if we've requested it to
6174 * be enabled.
6175 */
da7e29bd 6176static bool hsw_power_well_enabled(struct drm_i915_private *dev_priv,
c1ca727f
ID
6177 struct i915_power_well *power_well)
6178{
c1ca727f
ID
6179 return I915_READ(HSW_PWR_WELL_DRIVER) ==
6180 (HSW_PWR_WELL_ENABLE_REQUEST | HSW_PWR_WELL_STATE_ENABLED);
6181}
6182
bfafe93a
ID
6183bool intel_display_power_enabled_unlocked(struct drm_i915_private *dev_priv,
6184 enum intel_display_power_domain domain)
ddf9c536 6185{
ddf9c536 6186 struct i915_power_domains *power_domains;
b8c000d9
ID
6187 struct i915_power_well *power_well;
6188 bool is_enabled;
6189 int i;
6190
6191 if (dev_priv->pm.suspended)
6192 return false;
ddf9c536
ID
6193
6194 power_domains = &dev_priv->power_domains;
bfafe93a 6195
b8c000d9 6196 is_enabled = true;
bfafe93a 6197
b8c000d9
ID
6198 for_each_power_well_rev(i, power_well, BIT(domain), power_domains) {
6199 if (power_well->always_on)
6200 continue;
ddf9c536 6201
bfafe93a 6202 if (!power_well->hw_enabled) {
b8c000d9
ID
6203 is_enabled = false;
6204 break;
6205 }
6206 }
bfafe93a 6207
b8c000d9 6208 return is_enabled;
ddf9c536
ID
6209}
6210
da7e29bd 6211bool intel_display_power_enabled(struct drm_i915_private *dev_priv,
b97186f0 6212 enum intel_display_power_domain domain)
15d199ea 6213{
c1ca727f 6214 struct i915_power_domains *power_domains;
bfafe93a 6215 bool ret;
882244a3 6216
c1ca727f
ID
6217 power_domains = &dev_priv->power_domains;
6218
c1ca727f 6219 mutex_lock(&power_domains->lock);
bfafe93a 6220 ret = intel_display_power_enabled_unlocked(dev_priv, domain);
c1ca727f
ID
6221 mutex_unlock(&power_domains->lock);
6222
bfafe93a 6223 return ret;
15d199ea
PZ
6224}
6225
93c73e8c
ID
6226/*
6227 * Starting with Haswell, we have a "Power Down Well" that can be turned off
6228 * when not needed anymore. We have 4 registers that can request the power well
6229 * to be enabled, and it will only be disabled if none of the registers is
6230 * requesting it to be enabled.
6231 */
d5e8fdc8
PZ
6232static void hsw_power_well_post_enable(struct drm_i915_private *dev_priv)
6233{
6234 struct drm_device *dev = dev_priv->dev;
d5e8fdc8 6235
f9dcb0df
PZ
6236 /*
6237 * After we re-enable the power well, if we touch VGA register 0x3d5
6238 * we'll get unclaimed register interrupts. This stops after we write
6239 * anything to the VGA MSR register. The vgacon module uses this
6240 * register all the time, so if we unbind our driver and, as a
6241 * consequence, bind vgacon, we'll get stuck in an infinite loop at
6242 * console_unlock(). So make here we touch the VGA MSR register, making
6243 * sure vgacon can keep working normally without triggering interrupts
6244 * and error messages.
6245 */
6246 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
6247 outb(inb(VGA_MSR_READ), VGA_MSR_WRITE);
6248 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
6249
d49bdb0e
PZ
6250 if (IS_BROADWELL(dev))
6251 gen8_irq_power_well_post_enable(dev_priv);
d5e8fdc8
PZ
6252}
6253
da7e29bd 6254static void hsw_set_power_well(struct drm_i915_private *dev_priv,
c1ca727f 6255 struct i915_power_well *power_well, bool enable)
d0d3e513 6256{
fa42e23c
PZ
6257 bool is_enabled, enable_requested;
6258 uint32_t tmp;
d0d3e513 6259
fa42e23c 6260 tmp = I915_READ(HSW_PWR_WELL_DRIVER);
6aedd1f5
PZ
6261 is_enabled = tmp & HSW_PWR_WELL_STATE_ENABLED;
6262 enable_requested = tmp & HSW_PWR_WELL_ENABLE_REQUEST;
d0d3e513 6263
fa42e23c
PZ
6264 if (enable) {
6265 if (!enable_requested)
6aedd1f5
PZ
6266 I915_WRITE(HSW_PWR_WELL_DRIVER,
6267 HSW_PWR_WELL_ENABLE_REQUEST);
d0d3e513 6268
fa42e23c
PZ
6269 if (!is_enabled) {
6270 DRM_DEBUG_KMS("Enabling power well\n");
6271 if (wait_for((I915_READ(HSW_PWR_WELL_DRIVER) &
6aedd1f5 6272 HSW_PWR_WELL_STATE_ENABLED), 20))
fa42e23c
PZ
6273 DRM_ERROR("Timeout enabling power well\n");
6274 }
596cc11e 6275
d5e8fdc8 6276 hsw_power_well_post_enable(dev_priv);
fa42e23c
PZ
6277 } else {
6278 if (enable_requested) {
6279 I915_WRITE(HSW_PWR_WELL_DRIVER, 0);
9dbd8feb 6280 POSTING_READ(HSW_PWR_WELL_DRIVER);
fa42e23c 6281 DRM_DEBUG_KMS("Requesting to disable the power well\n");
d0d3e513
ED
6282 }
6283 }
fa42e23c 6284}
d0d3e513 6285
c6cb582e
ID
6286static void hsw_power_well_sync_hw(struct drm_i915_private *dev_priv,
6287 struct i915_power_well *power_well)
6288{
6289 hsw_set_power_well(dev_priv, power_well, power_well->count > 0);
6290
6291 /*
6292 * We're taking over the BIOS, so clear any requests made by it since
6293 * the driver is in charge now.
6294 */
6295 if (I915_READ(HSW_PWR_WELL_BIOS) & HSW_PWR_WELL_ENABLE_REQUEST)
6296 I915_WRITE(HSW_PWR_WELL_BIOS, 0);
6297}
6298
6299static void hsw_power_well_enable(struct drm_i915_private *dev_priv,
6300 struct i915_power_well *power_well)
6301{
c6cb582e
ID
6302 hsw_set_power_well(dev_priv, power_well, true);
6303}
6304
6305static void hsw_power_well_disable(struct drm_i915_private *dev_priv,
6306 struct i915_power_well *power_well)
6307{
6308 hsw_set_power_well(dev_priv, power_well, false);
c6cb582e
ID
6309}
6310
a45f4466
ID
6311static void i9xx_always_on_power_well_noop(struct drm_i915_private *dev_priv,
6312 struct i915_power_well *power_well)
6313{
6314}
6315
6316static bool i9xx_always_on_power_well_enabled(struct drm_i915_private *dev_priv,
6317 struct i915_power_well *power_well)
6318{
6319 return true;
6320}
6321
d2011dc8
VS
6322static void vlv_set_power_well(struct drm_i915_private *dev_priv,
6323 struct i915_power_well *power_well, bool enable)
77961eb9 6324{
d2011dc8 6325 enum punit_power_well power_well_id = power_well->data;
77961eb9
ID
6326 u32 mask;
6327 u32 state;
6328 u32 ctrl;
6329
6330 mask = PUNIT_PWRGT_MASK(power_well_id);
6331 state = enable ? PUNIT_PWRGT_PWR_ON(power_well_id) :
6332 PUNIT_PWRGT_PWR_GATE(power_well_id);
6333
6334 mutex_lock(&dev_priv->rps.hw_lock);
6335
6336#define COND \
6337 ((vlv_punit_read(dev_priv, PUNIT_REG_PWRGT_STATUS) & mask) == state)
6338
6339 if (COND)
6340 goto out;
6341
6342 ctrl = vlv_punit_read(dev_priv, PUNIT_REG_PWRGT_CTRL);
6343 ctrl &= ~mask;
6344 ctrl |= state;
6345 vlv_punit_write(dev_priv, PUNIT_REG_PWRGT_CTRL, ctrl);
6346
6347 if (wait_for(COND, 100))
6348 DRM_ERROR("timout setting power well state %08x (%08x)\n",
6349 state,
6350 vlv_punit_read(dev_priv, PUNIT_REG_PWRGT_CTRL));
6351
6352#undef COND
6353
6354out:
6355 mutex_unlock(&dev_priv->rps.hw_lock);
6356}
6357
6358static void vlv_power_well_sync_hw(struct drm_i915_private *dev_priv,
6359 struct i915_power_well *power_well)
6360{
6361 vlv_set_power_well(dev_priv, power_well, power_well->count > 0);
6362}
6363
6364static void vlv_power_well_enable(struct drm_i915_private *dev_priv,
6365 struct i915_power_well *power_well)
6366{
6367 vlv_set_power_well(dev_priv, power_well, true);
6368}
6369
6370static void vlv_power_well_disable(struct drm_i915_private *dev_priv,
6371 struct i915_power_well *power_well)
6372{
6373 vlv_set_power_well(dev_priv, power_well, false);
6374}
6375
6376static bool vlv_power_well_enabled(struct drm_i915_private *dev_priv,
6377 struct i915_power_well *power_well)
6378{
6379 int power_well_id = power_well->data;
6380 bool enabled = false;
6381 u32 mask;
6382 u32 state;
6383 u32 ctrl;
6384
6385 mask = PUNIT_PWRGT_MASK(power_well_id);
6386 ctrl = PUNIT_PWRGT_PWR_ON(power_well_id);
6387
6388 mutex_lock(&dev_priv->rps.hw_lock);
6389
6390 state = vlv_punit_read(dev_priv, PUNIT_REG_PWRGT_STATUS) & mask;
6391 /*
6392 * We only ever set the power-on and power-gate states, anything
6393 * else is unexpected.
6394 */
6395 WARN_ON(state != PUNIT_PWRGT_PWR_ON(power_well_id) &&
6396 state != PUNIT_PWRGT_PWR_GATE(power_well_id));
6397 if (state == ctrl)
6398 enabled = true;
6399
6400 /*
6401 * A transient state at this point would mean some unexpected party
6402 * is poking at the power controls too.
6403 */
6404 ctrl = vlv_punit_read(dev_priv, PUNIT_REG_PWRGT_CTRL) & mask;
6405 WARN_ON(ctrl != state);
6406
6407 mutex_unlock(&dev_priv->rps.hw_lock);
6408
6409 return enabled;
6410}
6411
6412static void vlv_display_power_well_enable(struct drm_i915_private *dev_priv,
6413 struct i915_power_well *power_well)
6414{
6415 WARN_ON_ONCE(power_well->data != PUNIT_POWER_WELL_DISP2D);
6416
6417 vlv_set_power_well(dev_priv, power_well, true);
6418
6419 spin_lock_irq(&dev_priv->irq_lock);
6420 valleyview_enable_display_irqs(dev_priv);
6421 spin_unlock_irq(&dev_priv->irq_lock);
6422
6423 /*
0d116a29
ID
6424 * During driver initialization/resume we can avoid restoring the
6425 * part of the HW/SW state that will be inited anyway explicitly.
77961eb9 6426 */
0d116a29
ID
6427 if (dev_priv->power_domains.initializing)
6428 return;
6429
6430 intel_hpd_init(dev_priv->dev);
77961eb9
ID
6431
6432 i915_redisable_vga_power_on(dev_priv->dev);
6433}
6434
6435static void vlv_display_power_well_disable(struct drm_i915_private *dev_priv,
6436 struct i915_power_well *power_well)
6437{
77961eb9
ID
6438 WARN_ON_ONCE(power_well->data != PUNIT_POWER_WELL_DISP2D);
6439
6440 spin_lock_irq(&dev_priv->irq_lock);
77961eb9
ID
6441 valleyview_disable_display_irqs(dev_priv);
6442 spin_unlock_irq(&dev_priv->irq_lock);
6443
77961eb9
ID
6444 vlv_set_power_well(dev_priv, power_well, false);
6445}
6446
aa519f23
VS
6447static void vlv_dpio_cmn_power_well_enable(struct drm_i915_private *dev_priv,
6448 struct i915_power_well *power_well)
6449{
6450 WARN_ON_ONCE(power_well->data != PUNIT_POWER_WELL_DPIO_CMN_BC);
6451
6452 /*
6453 * Enable the CRI clock source so we can get at the
6454 * display and the reference clock for VGA
6455 * hotplug / manual detection.
6456 */
6457 I915_WRITE(DPLL(PIPE_B), I915_READ(DPLL(PIPE_B)) |
6458 DPLL_REFA_CLK_ENABLE_VLV | DPLL_INTEGRATED_CRI_CLK_VLV);
6459 udelay(1); /* >10ns for cmnreset, >0ns for sidereset */
6460
6461 vlv_set_power_well(dev_priv, power_well, true);
6462
6463 /*
6464 * From VLV2A0_DP_eDP_DPIO_driver_vbios_notes_10.docx -
6465 * 6. De-assert cmn_reset/side_reset. Same as VLV X0.
6466 * a. GUnit 0x2110 bit[0] set to 1 (def 0)
6467 * b. The other bits such as sfr settings / modesel may all
6468 * be set to 0.
6469 *
6470 * This should only be done on init and resume from S3 with
6471 * both PLLs disabled, or we risk losing DPIO and PLL
6472 * synchronization.
6473 */
6474 I915_WRITE(DPIO_CTL, I915_READ(DPIO_CTL) | DPIO_CMNRST);
6475}
6476
6477static void vlv_dpio_cmn_power_well_disable(struct drm_i915_private *dev_priv,
6478 struct i915_power_well *power_well)
6479{
aa519f23
VS
6480 enum pipe pipe;
6481
6482 WARN_ON_ONCE(power_well->data != PUNIT_POWER_WELL_DPIO_CMN_BC);
6483
055e393f 6484 for_each_pipe(dev_priv, pipe)
aa519f23
VS
6485 assert_pll_disabled(dev_priv, pipe);
6486
6487 /* Assert common reset */
6488 I915_WRITE(DPIO_CTL, I915_READ(DPIO_CTL) & ~DPIO_CMNRST);
6489
6490 vlv_set_power_well(dev_priv, power_well, false);
6491}
6492
5d6f7ea7
VS
6493static void chv_dpio_cmn_power_well_enable(struct drm_i915_private *dev_priv,
6494 struct i915_power_well *power_well)
6495{
6496 enum dpio_phy phy;
6497
6498 WARN_ON_ONCE(power_well->data != PUNIT_POWER_WELL_DPIO_CMN_BC &&
6499 power_well->data != PUNIT_POWER_WELL_DPIO_CMN_D);
6500
6501 /*
6502 * Enable the CRI clock source so we can get at the
6503 * display and the reference clock for VGA
6504 * hotplug / manual detection.
6505 */
6506 if (power_well->data == PUNIT_POWER_WELL_DPIO_CMN_BC) {
6507 phy = DPIO_PHY0;
6508 I915_WRITE(DPLL(PIPE_B), I915_READ(DPLL(PIPE_B)) |
6509 DPLL_REFA_CLK_ENABLE_VLV);
6510 I915_WRITE(DPLL(PIPE_B), I915_READ(DPLL(PIPE_B)) |
6511 DPLL_REFA_CLK_ENABLE_VLV | DPLL_INTEGRATED_CRI_CLK_VLV);
6512 } else {
6513 phy = DPIO_PHY1;
6514 I915_WRITE(DPLL(PIPE_C), I915_READ(DPLL(PIPE_C)) |
6515 DPLL_REFA_CLK_ENABLE_VLV | DPLL_INTEGRATED_CRI_CLK_VLV);
6516 }
6517 udelay(1); /* >10ns for cmnreset, >0ns for sidereset */
6518 vlv_set_power_well(dev_priv, power_well, true);
6519
6520 /* Poll for phypwrgood signal */
6521 if (wait_for(I915_READ(DISPLAY_PHY_STATUS) & PHY_POWERGOOD(phy), 1))
6522 DRM_ERROR("Display PHY %d is not power up\n", phy);
6523
efd814b7
VS
6524 I915_WRITE(DISPLAY_PHY_CONTROL, I915_READ(DISPLAY_PHY_CONTROL) |
6525 PHY_COM_LANE_RESET_DEASSERT(phy));
5d6f7ea7
VS
6526}
6527
6528static void chv_dpio_cmn_power_well_disable(struct drm_i915_private *dev_priv,
6529 struct i915_power_well *power_well)
6530{
6531 enum dpio_phy phy;
6532
6533 WARN_ON_ONCE(power_well->data != PUNIT_POWER_WELL_DPIO_CMN_BC &&
6534 power_well->data != PUNIT_POWER_WELL_DPIO_CMN_D);
6535
6536 if (power_well->data == PUNIT_POWER_WELL_DPIO_CMN_BC) {
6537 phy = DPIO_PHY0;
6538 assert_pll_disabled(dev_priv, PIPE_A);
6539 assert_pll_disabled(dev_priv, PIPE_B);
6540 } else {
6541 phy = DPIO_PHY1;
6542 assert_pll_disabled(dev_priv, PIPE_C);
6543 }
6544
efd814b7
VS
6545 I915_WRITE(DISPLAY_PHY_CONTROL, I915_READ(DISPLAY_PHY_CONTROL) &
6546 ~PHY_COM_LANE_RESET_DEASSERT(phy));
5d6f7ea7
VS
6547
6548 vlv_set_power_well(dev_priv, power_well, false);
6549}
6550
26972b0a
VS
6551static bool chv_pipe_power_well_enabled(struct drm_i915_private *dev_priv,
6552 struct i915_power_well *power_well)
6553{
6554 enum pipe pipe = power_well->data;
6555 bool enabled;
6556 u32 state, ctrl;
6557
6558 mutex_lock(&dev_priv->rps.hw_lock);
6559
6560 state = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) & DP_SSS_MASK(pipe);
6561 /*
6562 * We only ever set the power-on and power-gate states, anything
6563 * else is unexpected.
6564 */
6565 WARN_ON(state != DP_SSS_PWR_ON(pipe) && state != DP_SSS_PWR_GATE(pipe));
6566 enabled = state == DP_SSS_PWR_ON(pipe);
6567
6568 /*
6569 * A transient state at this point would mean some unexpected party
6570 * is poking at the power controls too.
6571 */
6572 ctrl = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) & DP_SSC_MASK(pipe);
6573 WARN_ON(ctrl << 16 != state);
6574
6575 mutex_unlock(&dev_priv->rps.hw_lock);
6576
6577 return enabled;
6578}
6579
6580static void chv_set_pipe_power_well(struct drm_i915_private *dev_priv,
6581 struct i915_power_well *power_well,
6582 bool enable)
6583{
6584 enum pipe pipe = power_well->data;
6585 u32 state;
6586 u32 ctrl;
6587
6588 state = enable ? DP_SSS_PWR_ON(pipe) : DP_SSS_PWR_GATE(pipe);
6589
6590 mutex_lock(&dev_priv->rps.hw_lock);
6591
6592#define COND \
6593 ((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) & DP_SSS_MASK(pipe)) == state)
6594
6595 if (COND)
6596 goto out;
6597
6598 ctrl = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
6599 ctrl &= ~DP_SSC_MASK(pipe);
6600 ctrl |= enable ? DP_SSC_PWR_ON(pipe) : DP_SSC_PWR_GATE(pipe);
6601 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, ctrl);
6602
6603 if (wait_for(COND, 100))
6604 DRM_ERROR("timout setting power well state %08x (%08x)\n",
6605 state,
6606 vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ));
6607
6608#undef COND
6609
6610out:
6611 mutex_unlock(&dev_priv->rps.hw_lock);
6612}
6613
6614static void chv_pipe_power_well_sync_hw(struct drm_i915_private *dev_priv,
6615 struct i915_power_well *power_well)
6616{
6617 chv_set_pipe_power_well(dev_priv, power_well, power_well->count > 0);
6618}
6619
6620static void chv_pipe_power_well_enable(struct drm_i915_private *dev_priv,
6621 struct i915_power_well *power_well)
6622{
6623 WARN_ON_ONCE(power_well->data != PIPE_A &&
6624 power_well->data != PIPE_B &&
6625 power_well->data != PIPE_C);
6626
6627 chv_set_pipe_power_well(dev_priv, power_well, true);
6628}
6629
6630static void chv_pipe_power_well_disable(struct drm_i915_private *dev_priv,
6631 struct i915_power_well *power_well)
6632{
6633 WARN_ON_ONCE(power_well->data != PIPE_A &&
6634 power_well->data != PIPE_B &&
6635 power_well->data != PIPE_C);
6636
6637 chv_set_pipe_power_well(dev_priv, power_well, false);
6638}
6639
25eaa003
ID
6640static void check_power_well_state(struct drm_i915_private *dev_priv,
6641 struct i915_power_well *power_well)
6642{
6643 bool enabled = power_well->ops->is_enabled(dev_priv, power_well);
6644
6645 if (power_well->always_on || !i915.disable_power_well) {
6646 if (!enabled)
6647 goto mismatch;
6648
6649 return;
6650 }
6651
6652 if (enabled != (power_well->count > 0))
6653 goto mismatch;
6654
6655 return;
6656
6657mismatch:
6658 WARN(1, "state mismatch for '%s' (always_on %d hw state %d use-count %d disable_power_well %d\n",
6659 power_well->name, power_well->always_on, enabled,
6660 power_well->count, i915.disable_power_well);
6661}
6662
da7e29bd 6663void intel_display_power_get(struct drm_i915_private *dev_priv,
6765625e
VS
6664 enum intel_display_power_domain domain)
6665{
83c00f55 6666 struct i915_power_domains *power_domains;
c1ca727f
ID
6667 struct i915_power_well *power_well;
6668 int i;
6765625e 6669
9e6ea71a
PZ
6670 intel_runtime_pm_get(dev_priv);
6671
83c00f55
ID
6672 power_domains = &dev_priv->power_domains;
6673
6674 mutex_lock(&power_domains->lock);
1da51581 6675
25eaa003
ID
6676 for_each_power_well(i, power_well, BIT(domain), power_domains) {
6677 if (!power_well->count++) {
6678 DRM_DEBUG_KMS("enabling %s\n", power_well->name);
c6cb582e 6679 power_well->ops->enable(dev_priv, power_well);
bfafe93a 6680 power_well->hw_enabled = true;
25eaa003
ID
6681 }
6682
6683 check_power_well_state(dev_priv, power_well);
6684 }
1da51581 6685
ddf9c536
ID
6686 power_domains->domain_use_count[domain]++;
6687
83c00f55 6688 mutex_unlock(&power_domains->lock);
6765625e
VS
6689}
6690
da7e29bd 6691void intel_display_power_put(struct drm_i915_private *dev_priv,
6765625e
VS
6692 enum intel_display_power_domain domain)
6693{
83c00f55 6694 struct i915_power_domains *power_domains;
c1ca727f
ID
6695 struct i915_power_well *power_well;
6696 int i;
6765625e 6697
83c00f55
ID
6698 power_domains = &dev_priv->power_domains;
6699
6700 mutex_lock(&power_domains->lock);
1da51581 6701
1da51581
ID
6702 WARN_ON(!power_domains->domain_use_count[domain]);
6703 power_domains->domain_use_count[domain]--;
ddf9c536 6704
70bf407c
ID
6705 for_each_power_well_rev(i, power_well, BIT(domain), power_domains) {
6706 WARN_ON(!power_well->count);
6707
25eaa003
ID
6708 if (!--power_well->count && i915.disable_power_well) {
6709 DRM_DEBUG_KMS("disabling %s\n", power_well->name);
bfafe93a 6710 power_well->hw_enabled = false;
c6cb582e 6711 power_well->ops->disable(dev_priv, power_well);
25eaa003
ID
6712 }
6713
6714 check_power_well_state(dev_priv, power_well);
70bf407c 6715 }
1da51581 6716
83c00f55 6717 mutex_unlock(&power_domains->lock);
9e6ea71a
PZ
6718
6719 intel_runtime_pm_put(dev_priv);
6765625e
VS
6720}
6721
83c00f55 6722static struct i915_power_domains *hsw_pwr;
a38911a3
WX
6723
6724/* Display audio driver power well request */
74b0c2d7 6725int i915_request_power_well(void)
a38911a3 6726{
b4ed4484
ID
6727 struct drm_i915_private *dev_priv;
6728
74b0c2d7
TI
6729 if (!hsw_pwr)
6730 return -ENODEV;
a38911a3 6731
b4ed4484
ID
6732 dev_priv = container_of(hsw_pwr, struct drm_i915_private,
6733 power_domains);
da7e29bd 6734 intel_display_power_get(dev_priv, POWER_DOMAIN_AUDIO);
74b0c2d7 6735 return 0;
a38911a3
WX
6736}
6737EXPORT_SYMBOL_GPL(i915_request_power_well);
6738
6739/* Display audio driver power well release */
74b0c2d7 6740int i915_release_power_well(void)
a38911a3 6741{
b4ed4484
ID
6742 struct drm_i915_private *dev_priv;
6743
74b0c2d7
TI
6744 if (!hsw_pwr)
6745 return -ENODEV;
a38911a3 6746
b4ed4484
ID
6747 dev_priv = container_of(hsw_pwr, struct drm_i915_private,
6748 power_domains);
da7e29bd 6749 intel_display_power_put(dev_priv, POWER_DOMAIN_AUDIO);
74b0c2d7 6750 return 0;
a38911a3
WX
6751}
6752EXPORT_SYMBOL_GPL(i915_release_power_well);
6753
c149dcb5
JN
6754/*
6755 * Private interface for the audio driver to get CDCLK in kHz.
6756 *
6757 * Caller must request power well using i915_request_power_well() prior to
6758 * making the call.
6759 */
6760int i915_get_cdclk_freq(void)
6761{
6762 struct drm_i915_private *dev_priv;
6763
6764 if (!hsw_pwr)
6765 return -ENODEV;
6766
6767 dev_priv = container_of(hsw_pwr, struct drm_i915_private,
6768 power_domains);
6769
6770 return intel_ddi_get_cdclk_freq(dev_priv);
6771}
6772EXPORT_SYMBOL_GPL(i915_get_cdclk_freq);
6773
6774
efcad917
ID
6775#define POWER_DOMAIN_MASK (BIT(POWER_DOMAIN_NUM) - 1)
6776
6777#define HSW_ALWAYS_ON_POWER_DOMAINS ( \
6778 BIT(POWER_DOMAIN_PIPE_A) | \
f5938f36 6779 BIT(POWER_DOMAIN_TRANSCODER_EDP) | \
319be8ae
ID
6780 BIT(POWER_DOMAIN_PORT_DDI_A_2_LANES) | \
6781 BIT(POWER_DOMAIN_PORT_DDI_A_4_LANES) | \
6782 BIT(POWER_DOMAIN_PORT_DDI_B_2_LANES) | \
6783 BIT(POWER_DOMAIN_PORT_DDI_B_4_LANES) | \
6784 BIT(POWER_DOMAIN_PORT_DDI_C_2_LANES) | \
6785 BIT(POWER_DOMAIN_PORT_DDI_C_4_LANES) | \
6786 BIT(POWER_DOMAIN_PORT_DDI_D_2_LANES) | \
6787 BIT(POWER_DOMAIN_PORT_DDI_D_4_LANES) | \
6788 BIT(POWER_DOMAIN_PORT_CRT) | \
bd2bb1b9 6789 BIT(POWER_DOMAIN_PLLS) | \
f5938f36 6790 BIT(POWER_DOMAIN_INIT))
efcad917
ID
6791#define HSW_DISPLAY_POWER_DOMAINS ( \
6792 (POWER_DOMAIN_MASK & ~HSW_ALWAYS_ON_POWER_DOMAINS) | \
6793 BIT(POWER_DOMAIN_INIT))
6794
6795#define BDW_ALWAYS_ON_POWER_DOMAINS ( \
6796 HSW_ALWAYS_ON_POWER_DOMAINS | \
6797 BIT(POWER_DOMAIN_PIPE_A_PANEL_FITTER))
6798#define BDW_DISPLAY_POWER_DOMAINS ( \
6799 (POWER_DOMAIN_MASK & ~BDW_ALWAYS_ON_POWER_DOMAINS) | \
6800 BIT(POWER_DOMAIN_INIT))
6801
77961eb9
ID
6802#define VLV_ALWAYS_ON_POWER_DOMAINS BIT(POWER_DOMAIN_INIT)
6803#define VLV_DISPLAY_POWER_DOMAINS POWER_DOMAIN_MASK
6804
6805#define VLV_DPIO_CMN_BC_POWER_DOMAINS ( \
6806 BIT(POWER_DOMAIN_PORT_DDI_B_2_LANES) | \
6807 BIT(POWER_DOMAIN_PORT_DDI_B_4_LANES) | \
6808 BIT(POWER_DOMAIN_PORT_DDI_C_2_LANES) | \
6809 BIT(POWER_DOMAIN_PORT_DDI_C_4_LANES) | \
6810 BIT(POWER_DOMAIN_PORT_CRT) | \
6811 BIT(POWER_DOMAIN_INIT))
6812
6813#define VLV_DPIO_TX_B_LANES_01_POWER_DOMAINS ( \
6814 BIT(POWER_DOMAIN_PORT_DDI_B_2_LANES) | \
6815 BIT(POWER_DOMAIN_PORT_DDI_B_4_LANES) | \
6816 BIT(POWER_DOMAIN_INIT))
6817
6818#define VLV_DPIO_TX_B_LANES_23_POWER_DOMAINS ( \
6819 BIT(POWER_DOMAIN_PORT_DDI_B_4_LANES) | \
6820 BIT(POWER_DOMAIN_INIT))
6821
6822#define VLV_DPIO_TX_C_LANES_01_POWER_DOMAINS ( \
6823 BIT(POWER_DOMAIN_PORT_DDI_C_2_LANES) | \
6824 BIT(POWER_DOMAIN_PORT_DDI_C_4_LANES) | \
6825 BIT(POWER_DOMAIN_INIT))
6826
6827#define VLV_DPIO_TX_C_LANES_23_POWER_DOMAINS ( \
6828 BIT(POWER_DOMAIN_PORT_DDI_C_4_LANES) | \
6829 BIT(POWER_DOMAIN_INIT))
6830
26972b0a
VS
6831#define CHV_PIPE_A_POWER_DOMAINS ( \
6832 BIT(POWER_DOMAIN_PIPE_A) | \
6833 BIT(POWER_DOMAIN_INIT))
6834
6835#define CHV_PIPE_B_POWER_DOMAINS ( \
6836 BIT(POWER_DOMAIN_PIPE_B) | \
6837 BIT(POWER_DOMAIN_INIT))
6838
6839#define CHV_PIPE_C_POWER_DOMAINS ( \
6840 BIT(POWER_DOMAIN_PIPE_C) | \
6841 BIT(POWER_DOMAIN_INIT))
6842
5d6f7ea7
VS
6843#define CHV_DPIO_CMN_BC_POWER_DOMAINS ( \
6844 BIT(POWER_DOMAIN_PORT_DDI_B_2_LANES) | \
6845 BIT(POWER_DOMAIN_PORT_DDI_B_4_LANES) | \
6846 BIT(POWER_DOMAIN_PORT_DDI_C_2_LANES) | \
6847 BIT(POWER_DOMAIN_PORT_DDI_C_4_LANES) | \
6848 BIT(POWER_DOMAIN_INIT))
6849
6850#define CHV_DPIO_CMN_D_POWER_DOMAINS ( \
6851 BIT(POWER_DOMAIN_PORT_DDI_D_2_LANES) | \
6852 BIT(POWER_DOMAIN_PORT_DDI_D_4_LANES) | \
6853 BIT(POWER_DOMAIN_INIT))
6854
2ce147f3
VS
6855#define CHV_DPIO_TX_D_LANES_01_POWER_DOMAINS ( \
6856 BIT(POWER_DOMAIN_PORT_DDI_D_2_LANES) | \
6857 BIT(POWER_DOMAIN_PORT_DDI_D_4_LANES) | \
6858 BIT(POWER_DOMAIN_INIT))
6859
6860#define CHV_DPIO_TX_D_LANES_23_POWER_DOMAINS ( \
6861 BIT(POWER_DOMAIN_PORT_DDI_D_4_LANES) | \
6862 BIT(POWER_DOMAIN_INIT))
6863
a45f4466
ID
6864static const struct i915_power_well_ops i9xx_always_on_power_well_ops = {
6865 .sync_hw = i9xx_always_on_power_well_noop,
6866 .enable = i9xx_always_on_power_well_noop,
6867 .disable = i9xx_always_on_power_well_noop,
6868 .is_enabled = i9xx_always_on_power_well_enabled,
6869};
c6cb582e 6870
26972b0a
VS
6871static const struct i915_power_well_ops chv_pipe_power_well_ops = {
6872 .sync_hw = chv_pipe_power_well_sync_hw,
6873 .enable = chv_pipe_power_well_enable,
6874 .disable = chv_pipe_power_well_disable,
6875 .is_enabled = chv_pipe_power_well_enabled,
6876};
6877
5d6f7ea7
VS
6878static const struct i915_power_well_ops chv_dpio_cmn_power_well_ops = {
6879 .sync_hw = vlv_power_well_sync_hw,
6880 .enable = chv_dpio_cmn_power_well_enable,
6881 .disable = chv_dpio_cmn_power_well_disable,
6882 .is_enabled = vlv_power_well_enabled,
6883};
6884
1c2256df
ID
6885static struct i915_power_well i9xx_always_on_power_well[] = {
6886 {
6887 .name = "always-on",
6888 .always_on = 1,
6889 .domains = POWER_DOMAIN_MASK,
c6cb582e 6890 .ops = &i9xx_always_on_power_well_ops,
1c2256df
ID
6891 },
6892};
6893
c6cb582e
ID
6894static const struct i915_power_well_ops hsw_power_well_ops = {
6895 .sync_hw = hsw_power_well_sync_hw,
6896 .enable = hsw_power_well_enable,
6897 .disable = hsw_power_well_disable,
6898 .is_enabled = hsw_power_well_enabled,
6899};
6900
c1ca727f 6901static struct i915_power_well hsw_power_wells[] = {
6f3ef5dd
ID
6902 {
6903 .name = "always-on",
6904 .always_on = 1,
6905 .domains = HSW_ALWAYS_ON_POWER_DOMAINS,
c6cb582e 6906 .ops = &i9xx_always_on_power_well_ops,
6f3ef5dd 6907 },
c1ca727f
ID
6908 {
6909 .name = "display",
efcad917 6910 .domains = HSW_DISPLAY_POWER_DOMAINS,
c6cb582e 6911 .ops = &hsw_power_well_ops,
c1ca727f
ID
6912 },
6913};
6914
6915static struct i915_power_well bdw_power_wells[] = {
6f3ef5dd
ID
6916 {
6917 .name = "always-on",
6918 .always_on = 1,
6919 .domains = BDW_ALWAYS_ON_POWER_DOMAINS,
c6cb582e 6920 .ops = &i9xx_always_on_power_well_ops,
6f3ef5dd 6921 },
c1ca727f
ID
6922 {
6923 .name = "display",
efcad917 6924 .domains = BDW_DISPLAY_POWER_DOMAINS,
c6cb582e 6925 .ops = &hsw_power_well_ops,
c1ca727f
ID
6926 },
6927};
6928
77961eb9
ID
6929static const struct i915_power_well_ops vlv_display_power_well_ops = {
6930 .sync_hw = vlv_power_well_sync_hw,
6931 .enable = vlv_display_power_well_enable,
6932 .disable = vlv_display_power_well_disable,
6933 .is_enabled = vlv_power_well_enabled,
6934};
6935
aa519f23
VS
6936static const struct i915_power_well_ops vlv_dpio_cmn_power_well_ops = {
6937 .sync_hw = vlv_power_well_sync_hw,
6938 .enable = vlv_dpio_cmn_power_well_enable,
6939 .disable = vlv_dpio_cmn_power_well_disable,
6940 .is_enabled = vlv_power_well_enabled,
6941};
6942
77961eb9
ID
6943static const struct i915_power_well_ops vlv_dpio_power_well_ops = {
6944 .sync_hw = vlv_power_well_sync_hw,
6945 .enable = vlv_power_well_enable,
6946 .disable = vlv_power_well_disable,
6947 .is_enabled = vlv_power_well_enabled,
6948};
6949
6950static struct i915_power_well vlv_power_wells[] = {
6951 {
6952 .name = "always-on",
6953 .always_on = 1,
6954 .domains = VLV_ALWAYS_ON_POWER_DOMAINS,
6955 .ops = &i9xx_always_on_power_well_ops,
6956 },
6957 {
6958 .name = "display",
6959 .domains = VLV_DISPLAY_POWER_DOMAINS,
6960 .data = PUNIT_POWER_WELL_DISP2D,
6961 .ops = &vlv_display_power_well_ops,
6962 },
77961eb9
ID
6963 {
6964 .name = "dpio-tx-b-01",
6965 .domains = VLV_DPIO_TX_B_LANES_01_POWER_DOMAINS |
6966 VLV_DPIO_TX_B_LANES_23_POWER_DOMAINS |
6967 VLV_DPIO_TX_C_LANES_01_POWER_DOMAINS |
6968 VLV_DPIO_TX_C_LANES_23_POWER_DOMAINS,
6969 .ops = &vlv_dpio_power_well_ops,
6970 .data = PUNIT_POWER_WELL_DPIO_TX_B_LANES_01,
6971 },
6972 {
6973 .name = "dpio-tx-b-23",
6974 .domains = VLV_DPIO_TX_B_LANES_01_POWER_DOMAINS |
6975 VLV_DPIO_TX_B_LANES_23_POWER_DOMAINS |
6976 VLV_DPIO_TX_C_LANES_01_POWER_DOMAINS |
6977 VLV_DPIO_TX_C_LANES_23_POWER_DOMAINS,
6978 .ops = &vlv_dpio_power_well_ops,
6979 .data = PUNIT_POWER_WELL_DPIO_TX_B_LANES_23,
6980 },
6981 {
6982 .name = "dpio-tx-c-01",
6983 .domains = VLV_DPIO_TX_B_LANES_01_POWER_DOMAINS |
6984 VLV_DPIO_TX_B_LANES_23_POWER_DOMAINS |
6985 VLV_DPIO_TX_C_LANES_01_POWER_DOMAINS |
6986 VLV_DPIO_TX_C_LANES_23_POWER_DOMAINS,
6987 .ops = &vlv_dpio_power_well_ops,
6988 .data = PUNIT_POWER_WELL_DPIO_TX_C_LANES_01,
6989 },
6990 {
6991 .name = "dpio-tx-c-23",
6992 .domains = VLV_DPIO_TX_B_LANES_01_POWER_DOMAINS |
6993 VLV_DPIO_TX_B_LANES_23_POWER_DOMAINS |
6994 VLV_DPIO_TX_C_LANES_01_POWER_DOMAINS |
6995 VLV_DPIO_TX_C_LANES_23_POWER_DOMAINS,
6996 .ops = &vlv_dpio_power_well_ops,
6997 .data = PUNIT_POWER_WELL_DPIO_TX_C_LANES_23,
6998 },
f099a3c6
JB
6999 {
7000 .name = "dpio-common",
7001 .domains = VLV_DPIO_CMN_BC_POWER_DOMAINS,
7002 .data = PUNIT_POWER_WELL_DPIO_CMN_BC,
aa519f23 7003 .ops = &vlv_dpio_cmn_power_well_ops,
f099a3c6 7004 },
77961eb9
ID
7005};
7006
4811ff4f
VS
7007static struct i915_power_well chv_power_wells[] = {
7008 {
7009 .name = "always-on",
7010 .always_on = 1,
7011 .domains = VLV_ALWAYS_ON_POWER_DOMAINS,
7012 .ops = &i9xx_always_on_power_well_ops,
7013 },
f07057d1
VS
7014#if 0
7015 {
7016 .name = "display",
7017 .domains = VLV_DISPLAY_POWER_DOMAINS,
7018 .data = PUNIT_POWER_WELL_DISP2D,
7019 .ops = &vlv_display_power_well_ops,
7020 },
26972b0a
VS
7021 {
7022 .name = "pipe-a",
7023 .domains = CHV_PIPE_A_POWER_DOMAINS,
7024 .data = PIPE_A,
7025 .ops = &chv_pipe_power_well_ops,
7026 },
7027 {
7028 .name = "pipe-b",
7029 .domains = CHV_PIPE_B_POWER_DOMAINS,
7030 .data = PIPE_B,
7031 .ops = &chv_pipe_power_well_ops,
7032 },
7033 {
7034 .name = "pipe-c",
7035 .domains = CHV_PIPE_C_POWER_DOMAINS,
7036 .data = PIPE_C,
7037 .ops = &chv_pipe_power_well_ops,
7038 },
f07057d1 7039#endif
5d6f7ea7
VS
7040 {
7041 .name = "dpio-common-bc",
3dd7b974
VS
7042 /*
7043 * XXX: cmnreset for one PHY seems to disturb the other.
7044 * As a workaround keep both powered on at the same
7045 * time for now.
7046 */
7047 .domains = CHV_DPIO_CMN_BC_POWER_DOMAINS | CHV_DPIO_CMN_D_POWER_DOMAINS,
5d6f7ea7
VS
7048 .data = PUNIT_POWER_WELL_DPIO_CMN_BC,
7049 .ops = &chv_dpio_cmn_power_well_ops,
7050 },
7051 {
7052 .name = "dpio-common-d",
3dd7b974
VS
7053 /*
7054 * XXX: cmnreset for one PHY seems to disturb the other.
7055 * As a workaround keep both powered on at the same
7056 * time for now.
7057 */
7058 .domains = CHV_DPIO_CMN_BC_POWER_DOMAINS | CHV_DPIO_CMN_D_POWER_DOMAINS,
5d6f7ea7
VS
7059 .data = PUNIT_POWER_WELL_DPIO_CMN_D,
7060 .ops = &chv_dpio_cmn_power_well_ops,
7061 },
82583565
VS
7062#if 0
7063 {
7064 .name = "dpio-tx-b-01",
7065 .domains = VLV_DPIO_TX_B_LANES_01_POWER_DOMAINS |
7066 VLV_DPIO_TX_B_LANES_23_POWER_DOMAINS,
7067 .ops = &vlv_dpio_power_well_ops,
7068 .data = PUNIT_POWER_WELL_DPIO_TX_B_LANES_01,
7069 },
7070 {
7071 .name = "dpio-tx-b-23",
7072 .domains = VLV_DPIO_TX_B_LANES_01_POWER_DOMAINS |
7073 VLV_DPIO_TX_B_LANES_23_POWER_DOMAINS,
7074 .ops = &vlv_dpio_power_well_ops,
7075 .data = PUNIT_POWER_WELL_DPIO_TX_B_LANES_23,
7076 },
7077 {
7078 .name = "dpio-tx-c-01",
7079 .domains = VLV_DPIO_TX_C_LANES_01_POWER_DOMAINS |
7080 VLV_DPIO_TX_C_LANES_23_POWER_DOMAINS,
7081 .ops = &vlv_dpio_power_well_ops,
7082 .data = PUNIT_POWER_WELL_DPIO_TX_C_LANES_01,
7083 },
7084 {
7085 .name = "dpio-tx-c-23",
7086 .domains = VLV_DPIO_TX_C_LANES_01_POWER_DOMAINS |
7087 VLV_DPIO_TX_C_LANES_23_POWER_DOMAINS,
7088 .ops = &vlv_dpio_power_well_ops,
7089 .data = PUNIT_POWER_WELL_DPIO_TX_C_LANES_23,
7090 },
2ce147f3
VS
7091 {
7092 .name = "dpio-tx-d-01",
7093 .domains = CHV_DPIO_TX_D_LANES_01_POWER_DOMAINS |
7094 CHV_DPIO_TX_D_LANES_23_POWER_DOMAINS,
7095 .ops = &vlv_dpio_power_well_ops,
7096 .data = PUNIT_POWER_WELL_DPIO_TX_D_LANES_01,
7097 },
7098 {
7099 .name = "dpio-tx-d-23",
7100 .domains = CHV_DPIO_TX_D_LANES_01_POWER_DOMAINS |
7101 CHV_DPIO_TX_D_LANES_23_POWER_DOMAINS,
7102 .ops = &vlv_dpio_power_well_ops,
7103 .data = PUNIT_POWER_WELL_DPIO_TX_D_LANES_23,
7104 },
82583565 7105#endif
4811ff4f
VS
7106};
7107
d2011dc8
VS
7108static struct i915_power_well *lookup_power_well(struct drm_i915_private *dev_priv,
7109 enum punit_power_well power_well_id)
7110{
7111 struct i915_power_domains *power_domains = &dev_priv->power_domains;
7112 struct i915_power_well *power_well;
7113 int i;
7114
7115 for_each_power_well(i, power_well, POWER_DOMAIN_MASK, power_domains) {
7116 if (power_well->data == power_well_id)
7117 return power_well;
7118 }
7119
7120 return NULL;
7121}
7122
c1ca727f
ID
7123#define set_power_wells(power_domains, __power_wells) ({ \
7124 (power_domains)->power_wells = (__power_wells); \
7125 (power_domains)->power_well_count = ARRAY_SIZE(__power_wells); \
7126})
7127
da7e29bd 7128int intel_power_domains_init(struct drm_i915_private *dev_priv)
a38911a3 7129{
83c00f55 7130 struct i915_power_domains *power_domains = &dev_priv->power_domains;
c1ca727f 7131
83c00f55 7132 mutex_init(&power_domains->lock);
a38911a3 7133
c1ca727f
ID
7134 /*
7135 * The enabling order will be from lower to higher indexed wells,
7136 * the disabling order is reversed.
7137 */
da7e29bd 7138 if (IS_HASWELL(dev_priv->dev)) {
c1ca727f
ID
7139 set_power_wells(power_domains, hsw_power_wells);
7140 hsw_pwr = power_domains;
da7e29bd 7141 } else if (IS_BROADWELL(dev_priv->dev)) {
c1ca727f
ID
7142 set_power_wells(power_domains, bdw_power_wells);
7143 hsw_pwr = power_domains;
4811ff4f
VS
7144 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
7145 set_power_wells(power_domains, chv_power_wells);
77961eb9
ID
7146 } else if (IS_VALLEYVIEW(dev_priv->dev)) {
7147 set_power_wells(power_domains, vlv_power_wells);
c1ca727f 7148 } else {
1c2256df 7149 set_power_wells(power_domains, i9xx_always_on_power_well);
c1ca727f 7150 }
a38911a3
WX
7151
7152 return 0;
7153}
7154
da7e29bd 7155void intel_power_domains_remove(struct drm_i915_private *dev_priv)
a38911a3
WX
7156{
7157 hsw_pwr = NULL;
7158}
7159
da7e29bd 7160static void intel_power_domains_resume(struct drm_i915_private *dev_priv)
9cdb826c 7161{
83c00f55
ID
7162 struct i915_power_domains *power_domains = &dev_priv->power_domains;
7163 struct i915_power_well *power_well;
c1ca727f 7164 int i;
9cdb826c 7165
83c00f55 7166 mutex_lock(&power_domains->lock);
bfafe93a 7167 for_each_power_well(i, power_well, POWER_DOMAIN_MASK, power_domains) {
a45f4466 7168 power_well->ops->sync_hw(dev_priv, power_well);
bfafe93a
ID
7169 power_well->hw_enabled = power_well->ops->is_enabled(dev_priv,
7170 power_well);
7171 }
83c00f55 7172 mutex_unlock(&power_domains->lock);
a38911a3
WX
7173}
7174
d2011dc8
VS
7175static void vlv_cmnlane_wa(struct drm_i915_private *dev_priv)
7176{
7177 struct i915_power_well *cmn =
7178 lookup_power_well(dev_priv, PUNIT_POWER_WELL_DPIO_CMN_BC);
7179 struct i915_power_well *disp2d =
7180 lookup_power_well(dev_priv, PUNIT_POWER_WELL_DISP2D);
7181
7182 /* nothing to do if common lane is already off */
7183 if (!cmn->ops->is_enabled(dev_priv, cmn))
7184 return;
7185
7186 /* If the display might be already active skip this */
7187 if (disp2d->ops->is_enabled(dev_priv, disp2d) &&
7188 I915_READ(DPIO_CTL) & DPIO_CMNRST)
7189 return;
7190
7191 DRM_DEBUG_KMS("toggling display PHY side reset\n");
7192
7193 /* cmnlane needs DPLL registers */
7194 disp2d->ops->enable(dev_priv, disp2d);
7195
7196 /*
7197 * From VLV2A0_DP_eDP_HDMI_DPIO_driver_vbios_notes_11.docx:
7198 * Need to assert and de-assert PHY SB reset by gating the
7199 * common lane power, then un-gating it.
7200 * Simply ungating isn't enough to reset the PHY enough to get
7201 * ports and lanes running.
7202 */
7203 cmn->ops->disable(dev_priv, cmn);
7204}
7205
da7e29bd 7206void intel_power_domains_init_hw(struct drm_i915_private *dev_priv)
d0d3e513 7207{
d2011dc8 7208 struct drm_device *dev = dev_priv->dev;
0d116a29
ID
7209 struct i915_power_domains *power_domains = &dev_priv->power_domains;
7210
7211 power_domains->initializing = true;
d2011dc8
VS
7212
7213 if (IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
7214 mutex_lock(&power_domains->lock);
7215 vlv_cmnlane_wa(dev_priv);
7216 mutex_unlock(&power_domains->lock);
7217 }
7218
fa42e23c 7219 /* For now, we need the power well to be always enabled. */
da7e29bd
ID
7220 intel_display_set_init_power(dev_priv, true);
7221 intel_power_domains_resume(dev_priv);
0d116a29 7222 power_domains->initializing = false;
d0d3e513
ED
7223}
7224
c67a470b
PZ
7225void intel_aux_display_runtime_get(struct drm_i915_private *dev_priv)
7226{
d361ae26 7227 intel_runtime_pm_get(dev_priv);
c67a470b
PZ
7228}
7229
7230void intel_aux_display_runtime_put(struct drm_i915_private *dev_priv)
7231{
d361ae26 7232 intel_runtime_pm_put(dev_priv);
c67a470b
PZ
7233}
7234
8a187455
PZ
7235void intel_runtime_pm_get(struct drm_i915_private *dev_priv)
7236{
7237 struct drm_device *dev = dev_priv->dev;
7238 struct device *device = &dev->pdev->dev;
7239
7240 if (!HAS_RUNTIME_PM(dev))
7241 return;
7242
7243 pm_runtime_get_sync(device);
7244 WARN(dev_priv->pm.suspended, "Device still suspended.\n");
7245}
7246
c6df39b5
ID
7247void intel_runtime_pm_get_noresume(struct drm_i915_private *dev_priv)
7248{
7249 struct drm_device *dev = dev_priv->dev;
7250 struct device *device = &dev->pdev->dev;
7251
7252 if (!HAS_RUNTIME_PM(dev))
7253 return;
7254
7255 WARN(dev_priv->pm.suspended, "Getting nosync-ref while suspended.\n");
7256 pm_runtime_get_noresume(device);
7257}
7258
8a187455
PZ
7259void intel_runtime_pm_put(struct drm_i915_private *dev_priv)
7260{
7261 struct drm_device *dev = dev_priv->dev;
7262 struct device *device = &dev->pdev->dev;
7263
7264 if (!HAS_RUNTIME_PM(dev))
7265 return;
7266
7267 pm_runtime_mark_last_busy(device);
7268 pm_runtime_put_autosuspend(device);
7269}
7270
7271void intel_init_runtime_pm(struct drm_i915_private *dev_priv)
7272{
7273 struct drm_device *dev = dev_priv->dev;
7274 struct device *device = &dev->pdev->dev;
7275
8a187455
PZ
7276 if (!HAS_RUNTIME_PM(dev))
7277 return;
7278
7279 pm_runtime_set_active(device);
7280
aeab0b5a
ID
7281 /*
7282 * RPM depends on RC6 to save restore the GT HW context, so make RC6 a
7283 * requirement.
7284 */
7285 if (!intel_enable_rc6(dev)) {
7286 DRM_INFO("RC6 disabled, disabling runtime PM support\n");
7287 return;
7288 }
7289
8a187455
PZ
7290 pm_runtime_set_autosuspend_delay(device, 10000); /* 10s */
7291 pm_runtime_mark_last_busy(device);
7292 pm_runtime_use_autosuspend(device);
ba0239e0
PZ
7293
7294 pm_runtime_put_autosuspend(device);
8a187455
PZ
7295}
7296
7297void intel_fini_runtime_pm(struct drm_i915_private *dev_priv)
7298{
7299 struct drm_device *dev = dev_priv->dev;
7300 struct device *device = &dev->pdev->dev;
7301
7302 if (!HAS_RUNTIME_PM(dev))
7303 return;
7304
aeab0b5a
ID
7305 if (!intel_enable_rc6(dev))
7306 return;
7307
8a187455
PZ
7308 /* Make sure we're not suspended first. */
7309 pm_runtime_get_sync(device);
7310 pm_runtime_disable(device);
7311}
7312
1fa61106
ED
7313/* Set up chip specific power management-related functions */
7314void intel_init_pm(struct drm_device *dev)
7315{
7316 struct drm_i915_private *dev_priv = dev->dev_private;
7317
3a77c4c4 7318 if (HAS_FBC(dev)) {
40045465 7319 if (INTEL_INFO(dev)->gen >= 7) {
1fa61106 7320 dev_priv->display.fbc_enabled = ironlake_fbc_enabled;
40045465
VS
7321 dev_priv->display.enable_fbc = gen7_enable_fbc;
7322 dev_priv->display.disable_fbc = ironlake_disable_fbc;
7323 } else if (INTEL_INFO(dev)->gen >= 5) {
7324 dev_priv->display.fbc_enabled = ironlake_fbc_enabled;
7325 dev_priv->display.enable_fbc = ironlake_enable_fbc;
1fa61106
ED
7326 dev_priv->display.disable_fbc = ironlake_disable_fbc;
7327 } else if (IS_GM45(dev)) {
7328 dev_priv->display.fbc_enabled = g4x_fbc_enabled;
7329 dev_priv->display.enable_fbc = g4x_enable_fbc;
7330 dev_priv->display.disable_fbc = g4x_disable_fbc;
40045465 7331 } else {
1fa61106
ED
7332 dev_priv->display.fbc_enabled = i8xx_fbc_enabled;
7333 dev_priv->display.enable_fbc = i8xx_enable_fbc;
7334 dev_priv->display.disable_fbc = i8xx_disable_fbc;
993495ae
VS
7335
7336 /* This value was pulled out of someone's hat */
7337 I915_WRITE(FBC_CONTROL, 500 << FBC_CTL_INTERVAL_SHIFT);
1fa61106 7338 }
1fa61106
ED
7339 }
7340
c921aba8
DV
7341 /* For cxsr */
7342 if (IS_PINEVIEW(dev))
7343 i915_pineview_get_mem_freq(dev);
7344 else if (IS_GEN5(dev))
7345 i915_ironlake_get_mem_freq(dev);
7346
1fa61106
ED
7347 /* For FIFO watermark updates */
7348 if (HAS_PCH_SPLIT(dev)) {
fa50ad61 7349 ilk_setup_wm_latency(dev);
53615a5e 7350
bd602544
VS
7351 if ((IS_GEN5(dev) && dev_priv->wm.pri_latency[1] &&
7352 dev_priv->wm.spr_latency[1] && dev_priv->wm.cur_latency[1]) ||
7353 (!IS_GEN5(dev) && dev_priv->wm.pri_latency[0] &&
7354 dev_priv->wm.spr_latency[0] && dev_priv->wm.cur_latency[0])) {
7355 dev_priv->display.update_wm = ilk_update_wm;
7356 dev_priv->display.update_sprite_wm = ilk_update_sprite_wm;
7357 } else {
7358 DRM_DEBUG_KMS("Failed to read display plane latency. "
7359 "Disable CxSR\n");
7360 }
7361
7362 if (IS_GEN5(dev))
1fa61106 7363 dev_priv->display.init_clock_gating = ironlake_init_clock_gating;
bd602544 7364 else if (IS_GEN6(dev))
1fa61106 7365 dev_priv->display.init_clock_gating = gen6_init_clock_gating;
bd602544 7366 else if (IS_IVYBRIDGE(dev))
1fa61106 7367 dev_priv->display.init_clock_gating = ivybridge_init_clock_gating;
bd602544 7368 else if (IS_HASWELL(dev))
cad2a2d7 7369 dev_priv->display.init_clock_gating = haswell_init_clock_gating;
bd602544 7370 else if (INTEL_INFO(dev)->gen == 8)
47c2bd97 7371 dev_priv->display.init_clock_gating = broadwell_init_clock_gating;
a4565da8 7372 } else if (IS_CHERRYVIEW(dev)) {
3c2777fd 7373 dev_priv->display.update_wm = cherryview_update_wm;
01e184cc 7374 dev_priv->display.update_sprite_wm = valleyview_update_sprite_wm;
a4565da8
VS
7375 dev_priv->display.init_clock_gating =
7376 cherryview_init_clock_gating;
1fa61106
ED
7377 } else if (IS_VALLEYVIEW(dev)) {
7378 dev_priv->display.update_wm = valleyview_update_wm;
01e184cc 7379 dev_priv->display.update_sprite_wm = valleyview_update_sprite_wm;
1fa61106
ED
7380 dev_priv->display.init_clock_gating =
7381 valleyview_init_clock_gating;
1fa61106
ED
7382 } else if (IS_PINEVIEW(dev)) {
7383 if (!intel_get_cxsr_latency(IS_PINEVIEW_G(dev),
7384 dev_priv->is_ddr3,
7385 dev_priv->fsb_freq,
7386 dev_priv->mem_freq)) {
7387 DRM_INFO("failed to find known CxSR latency "
7388 "(found ddr%s fsb freq %d, mem freq %d), "
7389 "disabling CxSR\n",
7390 (dev_priv->is_ddr3 == 1) ? "3" : "2",
7391 dev_priv->fsb_freq, dev_priv->mem_freq);
7392 /* Disable CxSR and never update its watermark again */
5209b1f4 7393 intel_set_memory_cxsr(dev_priv, false);
1fa61106
ED
7394 dev_priv->display.update_wm = NULL;
7395 } else
7396 dev_priv->display.update_wm = pineview_update_wm;
7397 dev_priv->display.init_clock_gating = gen3_init_clock_gating;
7398 } else if (IS_G4X(dev)) {
7399 dev_priv->display.update_wm = g4x_update_wm;
7400 dev_priv->display.init_clock_gating = g4x_init_clock_gating;
7401 } else if (IS_GEN4(dev)) {
7402 dev_priv->display.update_wm = i965_update_wm;
7403 if (IS_CRESTLINE(dev))
7404 dev_priv->display.init_clock_gating = crestline_init_clock_gating;
7405 else if (IS_BROADWATER(dev))
7406 dev_priv->display.init_clock_gating = broadwater_init_clock_gating;
7407 } else if (IS_GEN3(dev)) {
7408 dev_priv->display.update_wm = i9xx_update_wm;
7409 dev_priv->display.get_fifo_size = i9xx_get_fifo_size;
7410 dev_priv->display.init_clock_gating = gen3_init_clock_gating;
feb56b93
DV
7411 } else if (IS_GEN2(dev)) {
7412 if (INTEL_INFO(dev)->num_pipes == 1) {
7413 dev_priv->display.update_wm = i845_update_wm;
1fa61106 7414 dev_priv->display.get_fifo_size = i845_get_fifo_size;
feb56b93
DV
7415 } else {
7416 dev_priv->display.update_wm = i9xx_update_wm;
1fa61106 7417 dev_priv->display.get_fifo_size = i830_get_fifo_size;
feb56b93
DV
7418 }
7419
7420 if (IS_I85X(dev) || IS_I865G(dev))
7421 dev_priv->display.init_clock_gating = i85x_init_clock_gating;
7422 else
7423 dev_priv->display.init_clock_gating = i830_init_clock_gating;
7424 } else {
7425 DRM_ERROR("unexpected fall-through in intel_init_pm\n");
1fa61106
ED
7426 }
7427}
7428
42c0526c
BW
7429int sandybridge_pcode_read(struct drm_i915_private *dev_priv, u8 mbox, u32 *val)
7430{
4fc688ce 7431 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
42c0526c
BW
7432
7433 if (I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) {
7434 DRM_DEBUG_DRIVER("warning: pcode (read) mailbox access failed\n");
7435 return -EAGAIN;
7436 }
7437
7438 I915_WRITE(GEN6_PCODE_DATA, *val);
7439 I915_WRITE(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY | mbox);
7440
7441 if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
7442 500)) {
7443 DRM_ERROR("timeout waiting for pcode read (%d) to finish\n", mbox);
7444 return -ETIMEDOUT;
7445 }
7446
7447 *val = I915_READ(GEN6_PCODE_DATA);
7448 I915_WRITE(GEN6_PCODE_DATA, 0);
7449
7450 return 0;
7451}
7452
7453int sandybridge_pcode_write(struct drm_i915_private *dev_priv, u8 mbox, u32 val)
7454{
4fc688ce 7455 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
42c0526c
BW
7456
7457 if (I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) {
7458 DRM_DEBUG_DRIVER("warning: pcode (write) mailbox access failed\n");
7459 return -EAGAIN;
7460 }
7461
7462 I915_WRITE(GEN6_PCODE_DATA, val);
7463 I915_WRITE(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY | mbox);
7464
7465 if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
7466 500)) {
7467 DRM_ERROR("timeout waiting for pcode write (%d) to finish\n", mbox);
7468 return -ETIMEDOUT;
7469 }
7470
7471 I915_WRITE(GEN6_PCODE_DATA, 0);
7472
7473 return 0;
7474}
a0e4e199 7475
b55dd647 7476static int byt_gpu_freq(struct drm_i915_private *dev_priv, int val)
855ba3be 7477{
07ab118b 7478 int div;
855ba3be 7479
07ab118b 7480 /* 4 x czclk */
2ec3815f 7481 switch (dev_priv->mem_freq) {
855ba3be 7482 case 800:
07ab118b 7483 div = 10;
855ba3be
JB
7484 break;
7485 case 1066:
07ab118b 7486 div = 12;
855ba3be
JB
7487 break;
7488 case 1333:
07ab118b 7489 div = 16;
855ba3be
JB
7490 break;
7491 default:
7492 return -1;
7493 }
7494
2ec3815f 7495 return DIV_ROUND_CLOSEST(dev_priv->mem_freq * (val + 6 - 0xbd), 4 * div);
855ba3be
JB
7496}
7497
b55dd647 7498static int byt_freq_opcode(struct drm_i915_private *dev_priv, int val)
855ba3be 7499{
07ab118b 7500 int mul;
855ba3be 7501
07ab118b 7502 /* 4 x czclk */
2ec3815f 7503 switch (dev_priv->mem_freq) {
855ba3be 7504 case 800:
07ab118b 7505 mul = 10;
855ba3be
JB
7506 break;
7507 case 1066:
07ab118b 7508 mul = 12;
855ba3be
JB
7509 break;
7510 case 1333:
07ab118b 7511 mul = 16;
855ba3be
JB
7512 break;
7513 default:
7514 return -1;
7515 }
7516
2ec3815f 7517 return DIV_ROUND_CLOSEST(4 * mul * val, dev_priv->mem_freq) + 0xbd - 6;
855ba3be
JB
7518}
7519
b55dd647 7520static int chv_gpu_freq(struct drm_i915_private *dev_priv, int val)
22b1b2f8
D
7521{
7522 int div, freq;
7523
7524 switch (dev_priv->rps.cz_freq) {
7525 case 200:
7526 div = 5;
7527 break;
7528 case 267:
7529 div = 6;
7530 break;
7531 case 320:
7532 case 333:
7533 case 400:
7534 div = 8;
7535 break;
7536 default:
7537 return -1;
7538 }
7539
7540 freq = (DIV_ROUND_CLOSEST((dev_priv->rps.cz_freq * val), 2 * div) / 2);
7541
7542 return freq;
7543}
7544
b55dd647 7545static int chv_freq_opcode(struct drm_i915_private *dev_priv, int val)
22b1b2f8
D
7546{
7547 int mul, opcode;
7548
7549 switch (dev_priv->rps.cz_freq) {
7550 case 200:
7551 mul = 5;
7552 break;
7553 case 267:
7554 mul = 6;
7555 break;
7556 case 320:
7557 case 333:
7558 case 400:
7559 mul = 8;
7560 break;
7561 default:
7562 return -1;
7563 }
7564
1c14762d 7565 /* CHV needs even values */
22b1b2f8
D
7566 opcode = (DIV_ROUND_CLOSEST((val * 2 * mul), dev_priv->rps.cz_freq) * 2);
7567
7568 return opcode;
7569}
7570
7571int vlv_gpu_freq(struct drm_i915_private *dev_priv, int val)
7572{
7573 int ret = -1;
7574
7575 if (IS_CHERRYVIEW(dev_priv->dev))
7576 ret = chv_gpu_freq(dev_priv, val);
7577 else if (IS_VALLEYVIEW(dev_priv->dev))
7578 ret = byt_gpu_freq(dev_priv, val);
7579
7580 return ret;
7581}
7582
7583int vlv_freq_opcode(struct drm_i915_private *dev_priv, int val)
7584{
7585 int ret = -1;
7586
7587 if (IS_CHERRYVIEW(dev_priv->dev))
7588 ret = chv_freq_opcode(dev_priv, val);
7589 else if (IS_VALLEYVIEW(dev_priv->dev))
7590 ret = byt_freq_opcode(dev_priv, val);
7591
7592 return ret;
7593}
7594
f742a552 7595void intel_pm_setup(struct drm_device *dev)
907b28c5
CW
7596{
7597 struct drm_i915_private *dev_priv = dev->dev_private;
7598
f742a552
DV
7599 mutex_init(&dev_priv->rps.hw_lock);
7600
907b28c5
CW
7601 INIT_DELAYED_WORK(&dev_priv->rps.delayed_resume_work,
7602 intel_gen6_powersave_work);
5d584b2e 7603
33688d95 7604 dev_priv->pm.suspended = false;
9df7575f 7605 dev_priv->pm._irqs_disabled = false;
907b28c5 7606}