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