2 * Copyright © 2012 Intel Corporation
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:
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
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
24 * Eugeni Dodonov <eugeni.dodonov@intel.com>
28 #include <linux/cpufreq.h>
30 #include "intel_drv.h"
31 #include "../../../platform/x86/intel_ips.h"
32 #include <linux/module.h>
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.
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.
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.
51 #define INTEL_RC6_ENABLE (1<<0)
52 #define INTEL_RC6p_ENABLE (1<<1)
53 #define INTEL_RC6pp_ENABLE (1<<2)
55 static void gen9_init_clock_gating(struct drm_device *dev)
57 struct drm_i915_private *dev_priv = dev->dev_private;
59 /* WaEnableLbsSlaRetryTimerDecrement:skl */
60 I915_WRITE(BDW_SCRATCH1, I915_READ(BDW_SCRATCH1) |
61 GEN9_LBS_SLA_RETRY_TIMER_DECREMENT_ENABLE);
64 static void skl_init_clock_gating(struct drm_device *dev)
66 struct drm_i915_private *dev_priv = dev->dev_private;
68 gen9_init_clock_gating(dev);
70 if (INTEL_REVID(dev) == SKL_REVID_A0) {
72 * WaDisableSDEUnitClockGating:skl
73 * WaSetGAPSunitClckGateDisable:skl
75 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
76 GEN8_GAPSUNIT_CLOCK_GATE_DISABLE |
77 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
80 if (INTEL_REVID(dev) <= SKL_REVID_D0) {
81 /* WaDisableHDCInvalidation:skl */
82 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) |
83 BDW_DISABLE_HDC_INVALIDATION);
85 /* WaDisableChickenBitTSGBarrierAckForFFSliceCS:skl */
86 I915_WRITE(FF_SLICE_CS_CHICKEN2,
87 I915_READ(FF_SLICE_CS_CHICKEN2) |
88 GEN9_TSG_BARRIER_ACK_DISABLE);
91 if (INTEL_REVID(dev) <= SKL_REVID_E0)
92 /* WaDisableLSQCROPERFforOCL:skl */
93 I915_WRITE(GEN8_L3SQCREG4, I915_READ(GEN8_L3SQCREG4) |
94 GEN8_LQSC_RO_PERF_DIS);
97 static void i915_pineview_get_mem_freq(struct drm_device *dev)
99 struct drm_i915_private *dev_priv = dev->dev_private;
102 tmp = I915_READ(CLKCFG);
104 switch (tmp & CLKCFG_FSB_MASK) {
106 dev_priv->fsb_freq = 533; /* 133*4 */
109 dev_priv->fsb_freq = 800; /* 200*4 */
112 dev_priv->fsb_freq = 667; /* 167*4 */
115 dev_priv->fsb_freq = 400; /* 100*4 */
119 switch (tmp & CLKCFG_MEM_MASK) {
121 dev_priv->mem_freq = 533;
124 dev_priv->mem_freq = 667;
127 dev_priv->mem_freq = 800;
131 /* detect pineview DDR3 setting */
132 tmp = I915_READ(CSHRDDR3CTL);
133 dev_priv->is_ddr3 = (tmp & CSHRDDR3CTL_DDR3) ? 1 : 0;
136 static void i915_ironlake_get_mem_freq(struct drm_device *dev)
138 struct drm_i915_private *dev_priv = dev->dev_private;
141 ddrpll = I915_READ16(DDRMPLL1);
142 csipll = I915_READ16(CSIPLL0);
144 switch (ddrpll & 0xff) {
146 dev_priv->mem_freq = 800;
149 dev_priv->mem_freq = 1066;
152 dev_priv->mem_freq = 1333;
155 dev_priv->mem_freq = 1600;
158 DRM_DEBUG_DRIVER("unknown memory frequency 0x%02x\n",
160 dev_priv->mem_freq = 0;
164 dev_priv->ips.r_t = dev_priv->mem_freq;
166 switch (csipll & 0x3ff) {
168 dev_priv->fsb_freq = 3200;
171 dev_priv->fsb_freq = 3733;
174 dev_priv->fsb_freq = 4266;
177 dev_priv->fsb_freq = 4800;
180 dev_priv->fsb_freq = 5333;
183 dev_priv->fsb_freq = 5866;
186 dev_priv->fsb_freq = 6400;
189 DRM_DEBUG_DRIVER("unknown fsb frequency 0x%04x\n",
191 dev_priv->fsb_freq = 0;
195 if (dev_priv->fsb_freq == 3200) {
196 dev_priv->ips.c_m = 0;
197 } else if (dev_priv->fsb_freq > 3200 && dev_priv->fsb_freq <= 4800) {
198 dev_priv->ips.c_m = 1;
200 dev_priv->ips.c_m = 2;
204 static const struct cxsr_latency cxsr_latency_table[] = {
205 {1, 0, 800, 400, 3382, 33382, 3983, 33983}, /* DDR2-400 SC */
206 {1, 0, 800, 667, 3354, 33354, 3807, 33807}, /* DDR2-667 SC */
207 {1, 0, 800, 800, 3347, 33347, 3763, 33763}, /* DDR2-800 SC */
208 {1, 1, 800, 667, 6420, 36420, 6873, 36873}, /* DDR3-667 SC */
209 {1, 1, 800, 800, 5902, 35902, 6318, 36318}, /* DDR3-800 SC */
211 {1, 0, 667, 400, 3400, 33400, 4021, 34021}, /* DDR2-400 SC */
212 {1, 0, 667, 667, 3372, 33372, 3845, 33845}, /* DDR2-667 SC */
213 {1, 0, 667, 800, 3386, 33386, 3822, 33822}, /* DDR2-800 SC */
214 {1, 1, 667, 667, 6438, 36438, 6911, 36911}, /* DDR3-667 SC */
215 {1, 1, 667, 800, 5941, 35941, 6377, 36377}, /* DDR3-800 SC */
217 {1, 0, 400, 400, 3472, 33472, 4173, 34173}, /* DDR2-400 SC */
218 {1, 0, 400, 667, 3443, 33443, 3996, 33996}, /* DDR2-667 SC */
219 {1, 0, 400, 800, 3430, 33430, 3946, 33946}, /* DDR2-800 SC */
220 {1, 1, 400, 667, 6509, 36509, 7062, 37062}, /* DDR3-667 SC */
221 {1, 1, 400, 800, 5985, 35985, 6501, 36501}, /* DDR3-800 SC */
223 {0, 0, 800, 400, 3438, 33438, 4065, 34065}, /* DDR2-400 SC */
224 {0, 0, 800, 667, 3410, 33410, 3889, 33889}, /* DDR2-667 SC */
225 {0, 0, 800, 800, 3403, 33403, 3845, 33845}, /* DDR2-800 SC */
226 {0, 1, 800, 667, 6476, 36476, 6955, 36955}, /* DDR3-667 SC */
227 {0, 1, 800, 800, 5958, 35958, 6400, 36400}, /* DDR3-800 SC */
229 {0, 0, 667, 400, 3456, 33456, 4103, 34106}, /* DDR2-400 SC */
230 {0, 0, 667, 667, 3428, 33428, 3927, 33927}, /* DDR2-667 SC */
231 {0, 0, 667, 800, 3443, 33443, 3905, 33905}, /* DDR2-800 SC */
232 {0, 1, 667, 667, 6494, 36494, 6993, 36993}, /* DDR3-667 SC */
233 {0, 1, 667, 800, 5998, 35998, 6460, 36460}, /* DDR3-800 SC */
235 {0, 0, 400, 400, 3528, 33528, 4255, 34255}, /* DDR2-400 SC */
236 {0, 0, 400, 667, 3500, 33500, 4079, 34079}, /* DDR2-667 SC */
237 {0, 0, 400, 800, 3487, 33487, 4029, 34029}, /* DDR2-800 SC */
238 {0, 1, 400, 667, 6566, 36566, 7145, 37145}, /* DDR3-667 SC */
239 {0, 1, 400, 800, 6042, 36042, 6584, 36584}, /* DDR3-800 SC */
242 static const struct cxsr_latency *intel_get_cxsr_latency(int is_desktop,
247 const struct cxsr_latency *latency;
250 if (fsb == 0 || mem == 0)
253 for (i = 0; i < ARRAY_SIZE(cxsr_latency_table); i++) {
254 latency = &cxsr_latency_table[i];
255 if (is_desktop == latency->is_desktop &&
256 is_ddr3 == latency->is_ddr3 &&
257 fsb == latency->fsb_freq && mem == latency->mem_freq)
261 DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
266 void intel_set_memory_cxsr(struct drm_i915_private *dev_priv, bool enable)
268 struct drm_device *dev = dev_priv->dev;
271 if (IS_VALLEYVIEW(dev)) {
272 I915_WRITE(FW_BLC_SELF_VLV, enable ? FW_CSPWRDWNEN : 0);
273 } else if (IS_G4X(dev) || IS_CRESTLINE(dev)) {
274 I915_WRITE(FW_BLC_SELF, enable ? FW_BLC_SELF_EN : 0);
275 } else if (IS_PINEVIEW(dev)) {
276 val = I915_READ(DSPFW3) & ~PINEVIEW_SELF_REFRESH_EN;
277 val |= enable ? PINEVIEW_SELF_REFRESH_EN : 0;
278 I915_WRITE(DSPFW3, val);
279 } else if (IS_I945G(dev) || IS_I945GM(dev)) {
280 val = enable ? _MASKED_BIT_ENABLE(FW_BLC_SELF_EN) :
281 _MASKED_BIT_DISABLE(FW_BLC_SELF_EN);
282 I915_WRITE(FW_BLC_SELF, val);
283 } else if (IS_I915GM(dev)) {
284 val = enable ? _MASKED_BIT_ENABLE(INSTPM_SELF_EN) :
285 _MASKED_BIT_DISABLE(INSTPM_SELF_EN);
286 I915_WRITE(INSTPM, val);
291 DRM_DEBUG_KMS("memory self-refresh is %s\n",
292 enable ? "enabled" : "disabled");
296 * Latency for FIFO fetches is dependent on several factors:
297 * - memory configuration (speed, channels)
299 * - current MCH state
300 * It can be fairly high in some situations, so here we assume a fairly
301 * pessimal value. It's a tradeoff between extra memory fetches (if we
302 * set this value too high, the FIFO will fetch frequently to stay full)
303 * and power consumption (set it too low to save power and we might see
304 * FIFO underruns and display "flicker").
306 * A value of 5us seems to be a good balance; safe for very low end
307 * platforms but not overly aggressive on lower latency configs.
309 static const int pessimal_latency_ns = 5000;
311 #define VLV_FIFO_START(dsparb, dsparb2, lo_shift, hi_shift) \
312 ((((dsparb) >> (lo_shift)) & 0xff) | ((((dsparb2) >> (hi_shift)) & 0x1) << 8))
314 static int vlv_get_fifo_size(struct drm_device *dev,
315 enum pipe pipe, int plane)
317 struct drm_i915_private *dev_priv = dev->dev_private;
318 int sprite0_start, sprite1_start, size;
321 uint32_t dsparb, dsparb2, dsparb3;
323 dsparb = I915_READ(DSPARB);
324 dsparb2 = I915_READ(DSPARB2);
325 sprite0_start = VLV_FIFO_START(dsparb, dsparb2, 0, 0);
326 sprite1_start = VLV_FIFO_START(dsparb, dsparb2, 8, 4);
329 dsparb = I915_READ(DSPARB);
330 dsparb2 = I915_READ(DSPARB2);
331 sprite0_start = VLV_FIFO_START(dsparb, dsparb2, 16, 8);
332 sprite1_start = VLV_FIFO_START(dsparb, dsparb2, 24, 12);
335 dsparb2 = I915_READ(DSPARB2);
336 dsparb3 = I915_READ(DSPARB3);
337 sprite0_start = VLV_FIFO_START(dsparb3, dsparb2, 0, 16);
338 sprite1_start = VLV_FIFO_START(dsparb3, dsparb2, 8, 20);
346 size = sprite0_start;
349 size = sprite1_start - sprite0_start;
352 size = 512 - 1 - sprite1_start;
358 DRM_DEBUG_KMS("Pipe %c %s %c FIFO size: %d\n",
359 pipe_name(pipe), plane == 0 ? "primary" : "sprite",
360 plane == 0 ? plane_name(pipe) : sprite_name(pipe, plane - 1),
366 static int i9xx_get_fifo_size(struct drm_device *dev, int plane)
368 struct drm_i915_private *dev_priv = dev->dev_private;
369 uint32_t dsparb = I915_READ(DSPARB);
372 size = dsparb & 0x7f;
374 size = ((dsparb >> DSPARB_CSTART_SHIFT) & 0x7f) - size;
376 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
377 plane ? "B" : "A", size);
382 static int i830_get_fifo_size(struct drm_device *dev, int plane)
384 struct drm_i915_private *dev_priv = dev->dev_private;
385 uint32_t dsparb = I915_READ(DSPARB);
388 size = dsparb & 0x1ff;
390 size = ((dsparb >> DSPARB_BEND_SHIFT) & 0x1ff) - size;
391 size >>= 1; /* Convert to cachelines */
393 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
394 plane ? "B" : "A", size);
399 static int i845_get_fifo_size(struct drm_device *dev, int plane)
401 struct drm_i915_private *dev_priv = dev->dev_private;
402 uint32_t dsparb = I915_READ(DSPARB);
405 size = dsparb & 0x7f;
406 size >>= 2; /* Convert to cachelines */
408 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
415 /* Pineview has different values for various configs */
416 static const struct intel_watermark_params pineview_display_wm = {
417 .fifo_size = PINEVIEW_DISPLAY_FIFO,
418 .max_wm = PINEVIEW_MAX_WM,
419 .default_wm = PINEVIEW_DFT_WM,
420 .guard_size = PINEVIEW_GUARD_WM,
421 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
423 static const struct intel_watermark_params pineview_display_hplloff_wm = {
424 .fifo_size = PINEVIEW_DISPLAY_FIFO,
425 .max_wm = PINEVIEW_MAX_WM,
426 .default_wm = PINEVIEW_DFT_HPLLOFF_WM,
427 .guard_size = PINEVIEW_GUARD_WM,
428 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
430 static const struct intel_watermark_params pineview_cursor_wm = {
431 .fifo_size = PINEVIEW_CURSOR_FIFO,
432 .max_wm = PINEVIEW_CURSOR_MAX_WM,
433 .default_wm = PINEVIEW_CURSOR_DFT_WM,
434 .guard_size = PINEVIEW_CURSOR_GUARD_WM,
435 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
437 static const struct intel_watermark_params pineview_cursor_hplloff_wm = {
438 .fifo_size = PINEVIEW_CURSOR_FIFO,
439 .max_wm = PINEVIEW_CURSOR_MAX_WM,
440 .default_wm = PINEVIEW_CURSOR_DFT_WM,
441 .guard_size = PINEVIEW_CURSOR_GUARD_WM,
442 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
444 static const struct intel_watermark_params g4x_wm_info = {
445 .fifo_size = G4X_FIFO_SIZE,
446 .max_wm = G4X_MAX_WM,
447 .default_wm = G4X_MAX_WM,
449 .cacheline_size = G4X_FIFO_LINE_SIZE,
451 static const struct intel_watermark_params g4x_cursor_wm_info = {
452 .fifo_size = I965_CURSOR_FIFO,
453 .max_wm = I965_CURSOR_MAX_WM,
454 .default_wm = I965_CURSOR_DFT_WM,
456 .cacheline_size = G4X_FIFO_LINE_SIZE,
458 static const struct intel_watermark_params valleyview_wm_info = {
459 .fifo_size = VALLEYVIEW_FIFO_SIZE,
460 .max_wm = VALLEYVIEW_MAX_WM,
461 .default_wm = VALLEYVIEW_MAX_WM,
463 .cacheline_size = G4X_FIFO_LINE_SIZE,
465 static const struct intel_watermark_params valleyview_cursor_wm_info = {
466 .fifo_size = I965_CURSOR_FIFO,
467 .max_wm = VALLEYVIEW_CURSOR_MAX_WM,
468 .default_wm = I965_CURSOR_DFT_WM,
470 .cacheline_size = G4X_FIFO_LINE_SIZE,
472 static const struct intel_watermark_params i965_cursor_wm_info = {
473 .fifo_size = I965_CURSOR_FIFO,
474 .max_wm = I965_CURSOR_MAX_WM,
475 .default_wm = I965_CURSOR_DFT_WM,
477 .cacheline_size = I915_FIFO_LINE_SIZE,
479 static const struct intel_watermark_params i945_wm_info = {
480 .fifo_size = I945_FIFO_SIZE,
481 .max_wm = I915_MAX_WM,
484 .cacheline_size = I915_FIFO_LINE_SIZE,
486 static const struct intel_watermark_params i915_wm_info = {
487 .fifo_size = I915_FIFO_SIZE,
488 .max_wm = I915_MAX_WM,
491 .cacheline_size = I915_FIFO_LINE_SIZE,
493 static const struct intel_watermark_params i830_a_wm_info = {
494 .fifo_size = I855GM_FIFO_SIZE,
495 .max_wm = I915_MAX_WM,
498 .cacheline_size = I830_FIFO_LINE_SIZE,
500 static const struct intel_watermark_params i830_bc_wm_info = {
501 .fifo_size = I855GM_FIFO_SIZE,
502 .max_wm = I915_MAX_WM/2,
505 .cacheline_size = I830_FIFO_LINE_SIZE,
507 static const struct intel_watermark_params i845_wm_info = {
508 .fifo_size = I830_FIFO_SIZE,
509 .max_wm = I915_MAX_WM,
512 .cacheline_size = I830_FIFO_LINE_SIZE,
516 * intel_calculate_wm - calculate watermark level
517 * @clock_in_khz: pixel clock
518 * @wm: chip FIFO params
519 * @pixel_size: display pixel size
520 * @latency_ns: memory latency for the platform
522 * Calculate the watermark level (the level at which the display plane will
523 * start fetching from memory again). Each chip has a different display
524 * FIFO size and allocation, so the caller needs to figure that out and pass
525 * in the correct intel_watermark_params structure.
527 * As the pixel clock runs, the FIFO will be drained at a rate that depends
528 * on the pixel size. When it reaches the watermark level, it'll start
529 * fetching FIFO line sized based chunks from memory until the FIFO fills
530 * past the watermark point. If the FIFO drains completely, a FIFO underrun
531 * will occur, and a display engine hang could result.
533 static unsigned long intel_calculate_wm(unsigned long clock_in_khz,
534 const struct intel_watermark_params *wm,
537 unsigned long latency_ns)
539 long entries_required, wm_size;
542 * Note: we need to make sure we don't overflow for various clock &
544 * clocks go from a few thousand to several hundred thousand.
545 * latency is usually a few thousand
547 entries_required = ((clock_in_khz / 1000) * pixel_size * latency_ns) /
549 entries_required = DIV_ROUND_UP(entries_required, wm->cacheline_size);
551 DRM_DEBUG_KMS("FIFO entries required for mode: %ld\n", entries_required);
553 wm_size = fifo_size - (entries_required + wm->guard_size);
555 DRM_DEBUG_KMS("FIFO watermark level: %ld\n", wm_size);
557 /* Don't promote wm_size to unsigned... */
558 if (wm_size > (long)wm->max_wm)
559 wm_size = wm->max_wm;
561 wm_size = wm->default_wm;
564 * Bspec seems to indicate that the value shouldn't be lower than
565 * 'burst size + 1'. Certainly 830 is quite unhappy with low values.
566 * Lets go for 8 which is the burst size since certain platforms
567 * already use a hardcoded 8 (which is what the spec says should be
576 static struct drm_crtc *single_enabled_crtc(struct drm_device *dev)
578 struct drm_crtc *crtc, *enabled = NULL;
580 for_each_crtc(dev, crtc) {
581 if (intel_crtc_active(crtc)) {
591 static void pineview_update_wm(struct drm_crtc *unused_crtc)
593 struct drm_device *dev = unused_crtc->dev;
594 struct drm_i915_private *dev_priv = dev->dev_private;
595 struct drm_crtc *crtc;
596 const struct cxsr_latency *latency;
600 latency = intel_get_cxsr_latency(IS_PINEVIEW_G(dev), dev_priv->is_ddr3,
601 dev_priv->fsb_freq, dev_priv->mem_freq);
603 DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
604 intel_set_memory_cxsr(dev_priv, false);
608 crtc = single_enabled_crtc(dev);
610 const struct drm_display_mode *adjusted_mode;
611 int pixel_size = crtc->primary->state->fb->bits_per_pixel / 8;
614 adjusted_mode = &to_intel_crtc(crtc)->config->base.adjusted_mode;
615 clock = adjusted_mode->crtc_clock;
618 wm = intel_calculate_wm(clock, &pineview_display_wm,
619 pineview_display_wm.fifo_size,
620 pixel_size, latency->display_sr);
621 reg = I915_READ(DSPFW1);
622 reg &= ~DSPFW_SR_MASK;
623 reg |= wm << DSPFW_SR_SHIFT;
624 I915_WRITE(DSPFW1, reg);
625 DRM_DEBUG_KMS("DSPFW1 register is %x\n", reg);
628 wm = intel_calculate_wm(clock, &pineview_cursor_wm,
629 pineview_display_wm.fifo_size,
630 pixel_size, latency->cursor_sr);
631 reg = I915_READ(DSPFW3);
632 reg &= ~DSPFW_CURSOR_SR_MASK;
633 reg |= (wm & 0x3f) << DSPFW_CURSOR_SR_SHIFT;
634 I915_WRITE(DSPFW3, reg);
636 /* Display HPLL off SR */
637 wm = intel_calculate_wm(clock, &pineview_display_hplloff_wm,
638 pineview_display_hplloff_wm.fifo_size,
639 pixel_size, latency->display_hpll_disable);
640 reg = I915_READ(DSPFW3);
641 reg &= ~DSPFW_HPLL_SR_MASK;
642 reg |= wm & DSPFW_HPLL_SR_MASK;
643 I915_WRITE(DSPFW3, reg);
645 /* cursor HPLL off SR */
646 wm = intel_calculate_wm(clock, &pineview_cursor_hplloff_wm,
647 pineview_display_hplloff_wm.fifo_size,
648 pixel_size, latency->cursor_hpll_disable);
649 reg = I915_READ(DSPFW3);
650 reg &= ~DSPFW_HPLL_CURSOR_MASK;
651 reg |= (wm & 0x3f) << DSPFW_HPLL_CURSOR_SHIFT;
652 I915_WRITE(DSPFW3, reg);
653 DRM_DEBUG_KMS("DSPFW3 register is %x\n", reg);
655 intel_set_memory_cxsr(dev_priv, true);
657 intel_set_memory_cxsr(dev_priv, false);
661 static bool g4x_compute_wm0(struct drm_device *dev,
663 const struct intel_watermark_params *display,
664 int display_latency_ns,
665 const struct intel_watermark_params *cursor,
666 int cursor_latency_ns,
670 struct drm_crtc *crtc;
671 const struct drm_display_mode *adjusted_mode;
672 int htotal, hdisplay, clock, pixel_size;
673 int line_time_us, line_count;
674 int entries, tlb_miss;
676 crtc = intel_get_crtc_for_plane(dev, plane);
677 if (!intel_crtc_active(crtc)) {
678 *cursor_wm = cursor->guard_size;
679 *plane_wm = display->guard_size;
683 adjusted_mode = &to_intel_crtc(crtc)->config->base.adjusted_mode;
684 clock = adjusted_mode->crtc_clock;
685 htotal = adjusted_mode->crtc_htotal;
686 hdisplay = to_intel_crtc(crtc)->config->pipe_src_w;
687 pixel_size = crtc->primary->state->fb->bits_per_pixel / 8;
689 /* Use the small buffer method to calculate plane watermark */
690 entries = ((clock * pixel_size / 1000) * display_latency_ns) / 1000;
691 tlb_miss = display->fifo_size*display->cacheline_size - hdisplay * 8;
694 entries = DIV_ROUND_UP(entries, display->cacheline_size);
695 *plane_wm = entries + display->guard_size;
696 if (*plane_wm > (int)display->max_wm)
697 *plane_wm = display->max_wm;
699 /* Use the large buffer method to calculate cursor watermark */
700 line_time_us = max(htotal * 1000 / clock, 1);
701 line_count = (cursor_latency_ns / line_time_us + 1000) / 1000;
702 entries = line_count * crtc->cursor->state->crtc_w * pixel_size;
703 tlb_miss = cursor->fifo_size*cursor->cacheline_size - hdisplay * 8;
706 entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
707 *cursor_wm = entries + cursor->guard_size;
708 if (*cursor_wm > (int)cursor->max_wm)
709 *cursor_wm = (int)cursor->max_wm;
715 * Check the wm result.
717 * If any calculated watermark values is larger than the maximum value that
718 * can be programmed into the associated watermark register, that watermark
721 static bool g4x_check_srwm(struct drm_device *dev,
722 int display_wm, int cursor_wm,
723 const struct intel_watermark_params *display,
724 const struct intel_watermark_params *cursor)
726 DRM_DEBUG_KMS("SR watermark: display plane %d, cursor %d\n",
727 display_wm, cursor_wm);
729 if (display_wm > display->max_wm) {
730 DRM_DEBUG_KMS("display watermark is too large(%d/%ld), disabling\n",
731 display_wm, display->max_wm);
735 if (cursor_wm > cursor->max_wm) {
736 DRM_DEBUG_KMS("cursor watermark is too large(%d/%ld), disabling\n",
737 cursor_wm, cursor->max_wm);
741 if (!(display_wm || cursor_wm)) {
742 DRM_DEBUG_KMS("SR latency is 0, disabling\n");
749 static bool g4x_compute_srwm(struct drm_device *dev,
752 const struct intel_watermark_params *display,
753 const struct intel_watermark_params *cursor,
754 int *display_wm, int *cursor_wm)
756 struct drm_crtc *crtc;
757 const struct drm_display_mode *adjusted_mode;
758 int hdisplay, htotal, pixel_size, clock;
759 unsigned long line_time_us;
760 int line_count, line_size;
765 *display_wm = *cursor_wm = 0;
769 crtc = intel_get_crtc_for_plane(dev, plane);
770 adjusted_mode = &to_intel_crtc(crtc)->config->base.adjusted_mode;
771 clock = adjusted_mode->crtc_clock;
772 htotal = adjusted_mode->crtc_htotal;
773 hdisplay = to_intel_crtc(crtc)->config->pipe_src_w;
774 pixel_size = crtc->primary->state->fb->bits_per_pixel / 8;
776 line_time_us = max(htotal * 1000 / clock, 1);
777 line_count = (latency_ns / line_time_us + 1000) / 1000;
778 line_size = hdisplay * pixel_size;
780 /* Use the minimum of the small and large buffer method for primary */
781 small = ((clock * pixel_size / 1000) * latency_ns) / 1000;
782 large = line_count * line_size;
784 entries = DIV_ROUND_UP(min(small, large), display->cacheline_size);
785 *display_wm = entries + display->guard_size;
787 /* calculate the self-refresh watermark for display cursor */
788 entries = line_count * pixel_size * crtc->cursor->state->crtc_w;
789 entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
790 *cursor_wm = entries + cursor->guard_size;
792 return g4x_check_srwm(dev,
793 *display_wm, *cursor_wm,
797 static void vlv_write_wm_values(struct intel_crtc *crtc,
798 const struct vlv_wm_values *wm)
800 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
801 enum pipe pipe = crtc->pipe;
803 I915_WRITE(VLV_DDL(pipe),
804 (wm->ddl[pipe].cursor << DDL_CURSOR_SHIFT) |
805 (wm->ddl[pipe].sprite[1] << DDL_SPRITE_SHIFT(1)) |
806 (wm->ddl[pipe].sprite[0] << DDL_SPRITE_SHIFT(0)) |
807 (wm->ddl[pipe].primary << DDL_PLANE_SHIFT));
809 dev_priv->wm.vlv = *wm;
812 static uint8_t vlv_compute_drain_latency(struct drm_crtc *crtc,
813 struct drm_plane *plane)
815 struct drm_device *dev = crtc->dev;
816 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
817 int entries, prec_mult, drain_latency, pixel_size;
818 int clock = intel_crtc->config->base.adjusted_mode.crtc_clock;
819 const int high_precision = IS_CHERRYVIEW(dev) ? 16 : 64;
822 * FIXME the plane might have an fb
823 * but be invisible (eg. due to clipping)
825 if (!intel_crtc->active || !plane->state->fb)
828 if (WARN(clock == 0, "Pixel clock is zero!\n"))
831 pixel_size = drm_format_plane_cpp(plane->state->fb->pixel_format, 0);
833 if (WARN(pixel_size == 0, "Pixel size is zero!\n"))
836 entries = DIV_ROUND_UP(clock, 1000) * pixel_size;
838 prec_mult = high_precision;
839 drain_latency = 64 * prec_mult * 4 / entries;
841 if (drain_latency > DRAIN_LATENCY_MASK) {
843 drain_latency = 64 * prec_mult * 4 / entries;
846 if (drain_latency > DRAIN_LATENCY_MASK)
847 drain_latency = DRAIN_LATENCY_MASK;
849 return drain_latency | (prec_mult == high_precision ?
850 DDL_PRECISION_HIGH : DDL_PRECISION_LOW);
854 * Update drain latency registers of memory arbiter
856 * Valleyview SoC has a new memory arbiter and needs drain latency registers
857 * to be programmed. Each plane has a drain latency multiplier and a drain
861 static void vlv_update_drain_latency(struct drm_crtc *crtc)
863 struct drm_device *dev = crtc->dev;
864 struct drm_i915_private *dev_priv = dev->dev_private;
865 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
866 enum pipe pipe = intel_crtc->pipe;
867 struct vlv_wm_values wm = dev_priv->wm.vlv;
869 wm.ddl[pipe].primary = vlv_compute_drain_latency(crtc, crtc->primary);
870 wm.ddl[pipe].cursor = vlv_compute_drain_latency(crtc, crtc->cursor);
872 vlv_write_wm_values(intel_crtc, &wm);
875 #define single_plane_enabled(mask) is_power_of_2(mask)
877 static void valleyview_update_wm(struct drm_crtc *crtc)
879 struct drm_device *dev = crtc->dev;
880 static const int sr_latency_ns = 12000;
881 struct drm_i915_private *dev_priv = dev->dev_private;
882 int planea_wm, planeb_wm, cursora_wm, cursorb_wm;
883 int plane_sr, cursor_sr;
884 int ignore_plane_sr, ignore_cursor_sr;
885 unsigned int enabled = 0;
888 vlv_update_drain_latency(crtc);
890 if (g4x_compute_wm0(dev, PIPE_A,
891 &valleyview_wm_info, pessimal_latency_ns,
892 &valleyview_cursor_wm_info, pessimal_latency_ns,
893 &planea_wm, &cursora_wm))
894 enabled |= 1 << PIPE_A;
896 if (g4x_compute_wm0(dev, PIPE_B,
897 &valleyview_wm_info, pessimal_latency_ns,
898 &valleyview_cursor_wm_info, pessimal_latency_ns,
899 &planeb_wm, &cursorb_wm))
900 enabled |= 1 << PIPE_B;
902 if (single_plane_enabled(enabled) &&
903 g4x_compute_srwm(dev, ffs(enabled) - 1,
906 &valleyview_cursor_wm_info,
907 &plane_sr, &ignore_cursor_sr) &&
908 g4x_compute_srwm(dev, ffs(enabled) - 1,
911 &valleyview_cursor_wm_info,
912 &ignore_plane_sr, &cursor_sr)) {
915 cxsr_enabled = false;
916 intel_set_memory_cxsr(dev_priv, false);
917 plane_sr = cursor_sr = 0;
920 DRM_DEBUG_KMS("Setting FIFO watermarks - A: plane=%d, cursor=%d, "
921 "B: plane=%d, cursor=%d, SR: plane=%d, cursor=%d\n",
922 planea_wm, cursora_wm,
923 planeb_wm, cursorb_wm,
924 plane_sr, cursor_sr);
927 (plane_sr << DSPFW_SR_SHIFT) |
928 (cursorb_wm << DSPFW_CURSORB_SHIFT) |
929 (planeb_wm << DSPFW_PLANEB_SHIFT) |
930 (planea_wm << DSPFW_PLANEA_SHIFT));
932 (I915_READ(DSPFW2) & ~DSPFW_CURSORA_MASK) |
933 (cursora_wm << DSPFW_CURSORA_SHIFT));
935 (I915_READ(DSPFW3) & ~DSPFW_CURSOR_SR_MASK) |
936 (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
939 intel_set_memory_cxsr(dev_priv, true);
942 static void cherryview_update_wm(struct drm_crtc *crtc)
944 struct drm_device *dev = crtc->dev;
945 static const int sr_latency_ns = 12000;
946 struct drm_i915_private *dev_priv = dev->dev_private;
947 int planea_wm, planeb_wm, planec_wm;
948 int cursora_wm, cursorb_wm, cursorc_wm;
949 int plane_sr, cursor_sr;
950 int ignore_plane_sr, ignore_cursor_sr;
951 unsigned int enabled = 0;
954 vlv_update_drain_latency(crtc);
956 if (g4x_compute_wm0(dev, PIPE_A,
957 &valleyview_wm_info, pessimal_latency_ns,
958 &valleyview_cursor_wm_info, pessimal_latency_ns,
959 &planea_wm, &cursora_wm))
960 enabled |= 1 << PIPE_A;
962 if (g4x_compute_wm0(dev, PIPE_B,
963 &valleyview_wm_info, pessimal_latency_ns,
964 &valleyview_cursor_wm_info, pessimal_latency_ns,
965 &planeb_wm, &cursorb_wm))
966 enabled |= 1 << PIPE_B;
968 if (g4x_compute_wm0(dev, PIPE_C,
969 &valleyview_wm_info, pessimal_latency_ns,
970 &valleyview_cursor_wm_info, pessimal_latency_ns,
971 &planec_wm, &cursorc_wm))
972 enabled |= 1 << PIPE_C;
974 if (single_plane_enabled(enabled) &&
975 g4x_compute_srwm(dev, ffs(enabled) - 1,
978 &valleyview_cursor_wm_info,
979 &plane_sr, &ignore_cursor_sr) &&
980 g4x_compute_srwm(dev, ffs(enabled) - 1,
983 &valleyview_cursor_wm_info,
984 &ignore_plane_sr, &cursor_sr)) {
987 cxsr_enabled = false;
988 intel_set_memory_cxsr(dev_priv, false);
989 plane_sr = cursor_sr = 0;
992 DRM_DEBUG_KMS("Setting FIFO watermarks - A: plane=%d, cursor=%d, "
993 "B: plane=%d, cursor=%d, C: plane=%d, cursor=%d, "
994 "SR: plane=%d, cursor=%d\n",
995 planea_wm, cursora_wm,
996 planeb_wm, cursorb_wm,
997 planec_wm, cursorc_wm,
998 plane_sr, cursor_sr);
1001 (plane_sr << DSPFW_SR_SHIFT) |
1002 (cursorb_wm << DSPFW_CURSORB_SHIFT) |
1003 (planeb_wm << DSPFW_PLANEB_SHIFT) |
1004 (planea_wm << DSPFW_PLANEA_SHIFT));
1006 (I915_READ(DSPFW2) & ~DSPFW_CURSORA_MASK) |
1007 (cursora_wm << DSPFW_CURSORA_SHIFT));
1009 (I915_READ(DSPFW3) & ~DSPFW_CURSOR_SR_MASK) |
1010 (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
1011 I915_WRITE(DSPFW9_CHV,
1012 (I915_READ(DSPFW9_CHV) & ~(DSPFW_PLANEC_MASK |
1013 DSPFW_CURSORC_MASK)) |
1014 (planec_wm << DSPFW_PLANEC_SHIFT) |
1015 (cursorc_wm << DSPFW_CURSORC_SHIFT));
1018 intel_set_memory_cxsr(dev_priv, true);
1021 static void valleyview_update_sprite_wm(struct drm_plane *plane,
1022 struct drm_crtc *crtc,
1023 uint32_t sprite_width,
1024 uint32_t sprite_height,
1026 bool enabled, bool scaled)
1028 struct drm_device *dev = crtc->dev;
1029 struct drm_i915_private *dev_priv = dev->dev_private;
1030 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1031 enum pipe pipe = intel_crtc->pipe;
1032 int sprite = to_intel_plane(plane)->plane;
1033 struct vlv_wm_values wm = dev_priv->wm.vlv;
1036 wm.ddl[pipe].sprite[sprite] =
1037 vlv_compute_drain_latency(crtc, plane);
1039 wm.ddl[pipe].sprite[sprite] = 0;
1041 vlv_write_wm_values(intel_crtc, &wm);
1044 static void g4x_update_wm(struct drm_crtc *crtc)
1046 struct drm_device *dev = crtc->dev;
1047 static const int sr_latency_ns = 12000;
1048 struct drm_i915_private *dev_priv = dev->dev_private;
1049 int planea_wm, planeb_wm, cursora_wm, cursorb_wm;
1050 int plane_sr, cursor_sr;
1051 unsigned int enabled = 0;
1054 if (g4x_compute_wm0(dev, PIPE_A,
1055 &g4x_wm_info, pessimal_latency_ns,
1056 &g4x_cursor_wm_info, pessimal_latency_ns,
1057 &planea_wm, &cursora_wm))
1058 enabled |= 1 << PIPE_A;
1060 if (g4x_compute_wm0(dev, PIPE_B,
1061 &g4x_wm_info, pessimal_latency_ns,
1062 &g4x_cursor_wm_info, pessimal_latency_ns,
1063 &planeb_wm, &cursorb_wm))
1064 enabled |= 1 << PIPE_B;
1066 if (single_plane_enabled(enabled) &&
1067 g4x_compute_srwm(dev, ffs(enabled) - 1,
1070 &g4x_cursor_wm_info,
1071 &plane_sr, &cursor_sr)) {
1072 cxsr_enabled = true;
1074 cxsr_enabled = false;
1075 intel_set_memory_cxsr(dev_priv, false);
1076 plane_sr = cursor_sr = 0;
1079 DRM_DEBUG_KMS("Setting FIFO watermarks - A: plane=%d, cursor=%d, "
1080 "B: plane=%d, cursor=%d, SR: plane=%d, cursor=%d\n",
1081 planea_wm, cursora_wm,
1082 planeb_wm, cursorb_wm,
1083 plane_sr, cursor_sr);
1086 (plane_sr << DSPFW_SR_SHIFT) |
1087 (cursorb_wm << DSPFW_CURSORB_SHIFT) |
1088 (planeb_wm << DSPFW_PLANEB_SHIFT) |
1089 (planea_wm << DSPFW_PLANEA_SHIFT));
1091 (I915_READ(DSPFW2) & ~DSPFW_CURSORA_MASK) |
1092 (cursora_wm << DSPFW_CURSORA_SHIFT));
1093 /* HPLL off in SR has some issues on G4x... disable it */
1095 (I915_READ(DSPFW3) & ~(DSPFW_HPLL_SR_EN | DSPFW_CURSOR_SR_MASK)) |
1096 (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
1099 intel_set_memory_cxsr(dev_priv, true);
1102 static void i965_update_wm(struct drm_crtc *unused_crtc)
1104 struct drm_device *dev = unused_crtc->dev;
1105 struct drm_i915_private *dev_priv = dev->dev_private;
1106 struct drm_crtc *crtc;
1111 /* Calc sr entries for one plane configs */
1112 crtc = single_enabled_crtc(dev);
1114 /* self-refresh has much higher latency */
1115 static const int sr_latency_ns = 12000;
1116 const struct drm_display_mode *adjusted_mode =
1117 &to_intel_crtc(crtc)->config->base.adjusted_mode;
1118 int clock = adjusted_mode->crtc_clock;
1119 int htotal = adjusted_mode->crtc_htotal;
1120 int hdisplay = to_intel_crtc(crtc)->config->pipe_src_w;
1121 int pixel_size = crtc->primary->state->fb->bits_per_pixel / 8;
1122 unsigned long line_time_us;
1125 line_time_us = max(htotal * 1000 / clock, 1);
1127 /* Use ns/us then divide to preserve precision */
1128 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
1129 pixel_size * hdisplay;
1130 entries = DIV_ROUND_UP(entries, I915_FIFO_LINE_SIZE);
1131 srwm = I965_FIFO_SIZE - entries;
1135 DRM_DEBUG_KMS("self-refresh entries: %d, wm: %d\n",
1138 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
1139 pixel_size * crtc->cursor->state->crtc_w;
1140 entries = DIV_ROUND_UP(entries,
1141 i965_cursor_wm_info.cacheline_size);
1142 cursor_sr = i965_cursor_wm_info.fifo_size -
1143 (entries + i965_cursor_wm_info.guard_size);
1145 if (cursor_sr > i965_cursor_wm_info.max_wm)
1146 cursor_sr = i965_cursor_wm_info.max_wm;
1148 DRM_DEBUG_KMS("self-refresh watermark: display plane %d "
1149 "cursor %d\n", srwm, cursor_sr);
1151 cxsr_enabled = true;
1153 cxsr_enabled = false;
1154 /* Turn off self refresh if both pipes are enabled */
1155 intel_set_memory_cxsr(dev_priv, false);
1158 DRM_DEBUG_KMS("Setting FIFO watermarks - A: 8, B: 8, C: 8, SR %d\n",
1161 /* 965 has limitations... */
1162 I915_WRITE(DSPFW1, (srwm << DSPFW_SR_SHIFT) |
1163 (8 << DSPFW_CURSORB_SHIFT) |
1164 (8 << DSPFW_PLANEB_SHIFT) |
1165 (8 << DSPFW_PLANEA_SHIFT));
1166 I915_WRITE(DSPFW2, (8 << DSPFW_CURSORA_SHIFT) |
1167 (8 << DSPFW_PLANEC_SHIFT_OLD));
1168 /* update cursor SR watermark */
1169 I915_WRITE(DSPFW3, (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
1172 intel_set_memory_cxsr(dev_priv, true);
1175 static void i9xx_update_wm(struct drm_crtc *unused_crtc)
1177 struct drm_device *dev = unused_crtc->dev;
1178 struct drm_i915_private *dev_priv = dev->dev_private;
1179 const struct intel_watermark_params *wm_info;
1184 int planea_wm, planeb_wm;
1185 struct drm_crtc *crtc, *enabled = NULL;
1188 wm_info = &i945_wm_info;
1189 else if (!IS_GEN2(dev))
1190 wm_info = &i915_wm_info;
1192 wm_info = &i830_a_wm_info;
1194 fifo_size = dev_priv->display.get_fifo_size(dev, 0);
1195 crtc = intel_get_crtc_for_plane(dev, 0);
1196 if (intel_crtc_active(crtc)) {
1197 const struct drm_display_mode *adjusted_mode;
1198 int cpp = crtc->primary->state->fb->bits_per_pixel / 8;
1202 adjusted_mode = &to_intel_crtc(crtc)->config->base.adjusted_mode;
1203 planea_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
1204 wm_info, fifo_size, cpp,
1205 pessimal_latency_ns);
1208 planea_wm = fifo_size - wm_info->guard_size;
1209 if (planea_wm > (long)wm_info->max_wm)
1210 planea_wm = wm_info->max_wm;
1214 wm_info = &i830_bc_wm_info;
1216 fifo_size = dev_priv->display.get_fifo_size(dev, 1);
1217 crtc = intel_get_crtc_for_plane(dev, 1);
1218 if (intel_crtc_active(crtc)) {
1219 const struct drm_display_mode *adjusted_mode;
1220 int cpp = crtc->primary->state->fb->bits_per_pixel / 8;
1224 adjusted_mode = &to_intel_crtc(crtc)->config->base.adjusted_mode;
1225 planeb_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
1226 wm_info, fifo_size, cpp,
1227 pessimal_latency_ns);
1228 if (enabled == NULL)
1233 planeb_wm = fifo_size - wm_info->guard_size;
1234 if (planeb_wm > (long)wm_info->max_wm)
1235 planeb_wm = wm_info->max_wm;
1238 DRM_DEBUG_KMS("FIFO watermarks - A: %d, B: %d\n", planea_wm, planeb_wm);
1240 if (IS_I915GM(dev) && enabled) {
1241 struct drm_i915_gem_object *obj;
1243 obj = intel_fb_obj(enabled->primary->state->fb);
1245 /* self-refresh seems busted with untiled */
1246 if (obj->tiling_mode == I915_TILING_NONE)
1251 * Overlay gets an aggressive default since video jitter is bad.
1255 /* Play safe and disable self-refresh before adjusting watermarks. */
1256 intel_set_memory_cxsr(dev_priv, false);
1258 /* Calc sr entries for one plane configs */
1259 if (HAS_FW_BLC(dev) && enabled) {
1260 /* self-refresh has much higher latency */
1261 static const int sr_latency_ns = 6000;
1262 const struct drm_display_mode *adjusted_mode =
1263 &to_intel_crtc(enabled)->config->base.adjusted_mode;
1264 int clock = adjusted_mode->crtc_clock;
1265 int htotal = adjusted_mode->crtc_htotal;
1266 int hdisplay = to_intel_crtc(enabled)->config->pipe_src_w;
1267 int pixel_size = enabled->primary->state->fb->bits_per_pixel / 8;
1268 unsigned long line_time_us;
1271 line_time_us = max(htotal * 1000 / clock, 1);
1273 /* Use ns/us then divide to preserve precision */
1274 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
1275 pixel_size * hdisplay;
1276 entries = DIV_ROUND_UP(entries, wm_info->cacheline_size);
1277 DRM_DEBUG_KMS("self-refresh entries: %d\n", entries);
1278 srwm = wm_info->fifo_size - entries;
1282 if (IS_I945G(dev) || IS_I945GM(dev))
1283 I915_WRITE(FW_BLC_SELF,
1284 FW_BLC_SELF_FIFO_MASK | (srwm & 0xff));
1285 else if (IS_I915GM(dev))
1286 I915_WRITE(FW_BLC_SELF, srwm & 0x3f);
1289 DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d, B: %d, C: %d, SR %d\n",
1290 planea_wm, planeb_wm, cwm, srwm);
1292 fwater_lo = ((planeb_wm & 0x3f) << 16) | (planea_wm & 0x3f);
1293 fwater_hi = (cwm & 0x1f);
1295 /* Set request length to 8 cachelines per fetch */
1296 fwater_lo = fwater_lo | (1 << 24) | (1 << 8);
1297 fwater_hi = fwater_hi | (1 << 8);
1299 I915_WRITE(FW_BLC, fwater_lo);
1300 I915_WRITE(FW_BLC2, fwater_hi);
1303 intel_set_memory_cxsr(dev_priv, true);
1306 static void i845_update_wm(struct drm_crtc *unused_crtc)
1308 struct drm_device *dev = unused_crtc->dev;
1309 struct drm_i915_private *dev_priv = dev->dev_private;
1310 struct drm_crtc *crtc;
1311 const struct drm_display_mode *adjusted_mode;
1315 crtc = single_enabled_crtc(dev);
1319 adjusted_mode = &to_intel_crtc(crtc)->config->base.adjusted_mode;
1320 planea_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
1322 dev_priv->display.get_fifo_size(dev, 0),
1323 4, pessimal_latency_ns);
1324 fwater_lo = I915_READ(FW_BLC) & ~0xfff;
1325 fwater_lo |= (3<<8) | planea_wm;
1327 DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d\n", planea_wm);
1329 I915_WRITE(FW_BLC, fwater_lo);
1332 static uint32_t ilk_pipe_pixel_rate(struct drm_device *dev,
1333 struct drm_crtc *crtc)
1335 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1336 uint32_t pixel_rate;
1338 pixel_rate = intel_crtc->config->base.adjusted_mode.crtc_clock;
1340 /* We only use IF-ID interlacing. If we ever use PF-ID we'll need to
1341 * adjust the pixel_rate here. */
1343 if (intel_crtc->config->pch_pfit.enabled) {
1344 uint64_t pipe_w, pipe_h, pfit_w, pfit_h;
1345 uint32_t pfit_size = intel_crtc->config->pch_pfit.size;
1347 pipe_w = intel_crtc->config->pipe_src_w;
1348 pipe_h = intel_crtc->config->pipe_src_h;
1349 pfit_w = (pfit_size >> 16) & 0xFFFF;
1350 pfit_h = pfit_size & 0xFFFF;
1351 if (pipe_w < pfit_w)
1353 if (pipe_h < pfit_h)
1356 pixel_rate = div_u64((uint64_t) pixel_rate * pipe_w * pipe_h,
1363 /* latency must be in 0.1us units. */
1364 static uint32_t ilk_wm_method1(uint32_t pixel_rate, uint8_t bytes_per_pixel,
1369 if (WARN(latency == 0, "Latency value missing\n"))
1372 ret = (uint64_t) pixel_rate * bytes_per_pixel * latency;
1373 ret = DIV_ROUND_UP_ULL(ret, 64 * 10000) + 2;
1378 /* latency must be in 0.1us units. */
1379 static uint32_t ilk_wm_method2(uint32_t pixel_rate, uint32_t pipe_htotal,
1380 uint32_t horiz_pixels, uint8_t bytes_per_pixel,
1385 if (WARN(latency == 0, "Latency value missing\n"))
1388 ret = (latency * pixel_rate) / (pipe_htotal * 10000);
1389 ret = (ret + 1) * horiz_pixels * bytes_per_pixel;
1390 ret = DIV_ROUND_UP(ret, 64) + 2;
1394 static uint32_t ilk_wm_fbc(uint32_t pri_val, uint32_t horiz_pixels,
1395 uint8_t bytes_per_pixel)
1397 return DIV_ROUND_UP(pri_val * 64, horiz_pixels * bytes_per_pixel) + 2;
1400 struct skl_pipe_wm_parameters {
1402 uint32_t pipe_htotal;
1403 uint32_t pixel_rate; /* in KHz */
1404 struct intel_plane_wm_parameters plane[I915_MAX_PLANES];
1405 struct intel_plane_wm_parameters cursor;
1408 struct ilk_pipe_wm_parameters {
1410 uint32_t pipe_htotal;
1411 uint32_t pixel_rate;
1412 struct intel_plane_wm_parameters pri;
1413 struct intel_plane_wm_parameters spr;
1414 struct intel_plane_wm_parameters cur;
1417 struct ilk_wm_maximums {
1424 /* used in computing the new watermarks state */
1425 struct intel_wm_config {
1426 unsigned int num_pipes_active;
1427 bool sprites_enabled;
1428 bool sprites_scaled;
1432 * For both WM_PIPE and WM_LP.
1433 * mem_value must be in 0.1us units.
1435 static uint32_t ilk_compute_pri_wm(const struct ilk_pipe_wm_parameters *params,
1439 uint32_t method1, method2;
1441 if (!params->active || !params->pri.enabled)
1444 method1 = ilk_wm_method1(params->pixel_rate,
1445 params->pri.bytes_per_pixel,
1451 method2 = ilk_wm_method2(params->pixel_rate,
1452 params->pipe_htotal,
1453 params->pri.horiz_pixels,
1454 params->pri.bytes_per_pixel,
1457 return min(method1, method2);
1461 * For both WM_PIPE and WM_LP.
1462 * mem_value must be in 0.1us units.
1464 static uint32_t ilk_compute_spr_wm(const struct ilk_pipe_wm_parameters *params,
1467 uint32_t method1, method2;
1469 if (!params->active || !params->spr.enabled)
1472 method1 = ilk_wm_method1(params->pixel_rate,
1473 params->spr.bytes_per_pixel,
1475 method2 = ilk_wm_method2(params->pixel_rate,
1476 params->pipe_htotal,
1477 params->spr.horiz_pixels,
1478 params->spr.bytes_per_pixel,
1480 return min(method1, method2);
1484 * For both WM_PIPE and WM_LP.
1485 * mem_value must be in 0.1us units.
1487 static uint32_t ilk_compute_cur_wm(const struct ilk_pipe_wm_parameters *params,
1490 if (!params->active || !params->cur.enabled)
1493 return ilk_wm_method2(params->pixel_rate,
1494 params->pipe_htotal,
1495 params->cur.horiz_pixels,
1496 params->cur.bytes_per_pixel,
1500 /* Only for WM_LP. */
1501 static uint32_t ilk_compute_fbc_wm(const struct ilk_pipe_wm_parameters *params,
1504 if (!params->active || !params->pri.enabled)
1507 return ilk_wm_fbc(pri_val,
1508 params->pri.horiz_pixels,
1509 params->pri.bytes_per_pixel);
1512 static unsigned int ilk_display_fifo_size(const struct drm_device *dev)
1514 if (INTEL_INFO(dev)->gen >= 8)
1516 else if (INTEL_INFO(dev)->gen >= 7)
1522 static unsigned int ilk_plane_wm_reg_max(const struct drm_device *dev,
1523 int level, bool is_sprite)
1525 if (INTEL_INFO(dev)->gen >= 8)
1526 /* BDW primary/sprite plane watermarks */
1527 return level == 0 ? 255 : 2047;
1528 else if (INTEL_INFO(dev)->gen >= 7)
1529 /* IVB/HSW primary/sprite plane watermarks */
1530 return level == 0 ? 127 : 1023;
1531 else if (!is_sprite)
1532 /* ILK/SNB primary plane watermarks */
1533 return level == 0 ? 127 : 511;
1535 /* ILK/SNB sprite plane watermarks */
1536 return level == 0 ? 63 : 255;
1539 static unsigned int ilk_cursor_wm_reg_max(const struct drm_device *dev,
1542 if (INTEL_INFO(dev)->gen >= 7)
1543 return level == 0 ? 63 : 255;
1545 return level == 0 ? 31 : 63;
1548 static unsigned int ilk_fbc_wm_reg_max(const struct drm_device *dev)
1550 if (INTEL_INFO(dev)->gen >= 8)
1556 /* Calculate the maximum primary/sprite plane watermark */
1557 static unsigned int ilk_plane_wm_max(const struct drm_device *dev,
1559 const struct intel_wm_config *config,
1560 enum intel_ddb_partitioning ddb_partitioning,
1563 unsigned int fifo_size = ilk_display_fifo_size(dev);
1565 /* if sprites aren't enabled, sprites get nothing */
1566 if (is_sprite && !config->sprites_enabled)
1569 /* HSW allows LP1+ watermarks even with multiple pipes */
1570 if (level == 0 || config->num_pipes_active > 1) {
1571 fifo_size /= INTEL_INFO(dev)->num_pipes;
1574 * For some reason the non self refresh
1575 * FIFO size is only half of the self
1576 * refresh FIFO size on ILK/SNB.
1578 if (INTEL_INFO(dev)->gen <= 6)
1582 if (config->sprites_enabled) {
1583 /* level 0 is always calculated with 1:1 split */
1584 if (level > 0 && ddb_partitioning == INTEL_DDB_PART_5_6) {
1593 /* clamp to max that the registers can hold */
1594 return min(fifo_size, ilk_plane_wm_reg_max(dev, level, is_sprite));
1597 /* Calculate the maximum cursor plane watermark */
1598 static unsigned int ilk_cursor_wm_max(const struct drm_device *dev,
1600 const struct intel_wm_config *config)
1602 /* HSW LP1+ watermarks w/ multiple pipes */
1603 if (level > 0 && config->num_pipes_active > 1)
1606 /* otherwise just report max that registers can hold */
1607 return ilk_cursor_wm_reg_max(dev, level);
1610 static void ilk_compute_wm_maximums(const struct drm_device *dev,
1612 const struct intel_wm_config *config,
1613 enum intel_ddb_partitioning ddb_partitioning,
1614 struct ilk_wm_maximums *max)
1616 max->pri = ilk_plane_wm_max(dev, level, config, ddb_partitioning, false);
1617 max->spr = ilk_plane_wm_max(dev, level, config, ddb_partitioning, true);
1618 max->cur = ilk_cursor_wm_max(dev, level, config);
1619 max->fbc = ilk_fbc_wm_reg_max(dev);
1622 static void ilk_compute_wm_reg_maximums(struct drm_device *dev,
1624 struct ilk_wm_maximums *max)
1626 max->pri = ilk_plane_wm_reg_max(dev, level, false);
1627 max->spr = ilk_plane_wm_reg_max(dev, level, true);
1628 max->cur = ilk_cursor_wm_reg_max(dev, level);
1629 max->fbc = ilk_fbc_wm_reg_max(dev);
1632 static bool ilk_validate_wm_level(int level,
1633 const struct ilk_wm_maximums *max,
1634 struct intel_wm_level *result)
1638 /* already determined to be invalid? */
1639 if (!result->enable)
1642 result->enable = result->pri_val <= max->pri &&
1643 result->spr_val <= max->spr &&
1644 result->cur_val <= max->cur;
1646 ret = result->enable;
1649 * HACK until we can pre-compute everything,
1650 * and thus fail gracefully if LP0 watermarks
1653 if (level == 0 && !result->enable) {
1654 if (result->pri_val > max->pri)
1655 DRM_DEBUG_KMS("Primary WM%d too large %u (max %u)\n",
1656 level, result->pri_val, max->pri);
1657 if (result->spr_val > max->spr)
1658 DRM_DEBUG_KMS("Sprite WM%d too large %u (max %u)\n",
1659 level, result->spr_val, max->spr);
1660 if (result->cur_val > max->cur)
1661 DRM_DEBUG_KMS("Cursor WM%d too large %u (max %u)\n",
1662 level, result->cur_val, max->cur);
1664 result->pri_val = min_t(uint32_t, result->pri_val, max->pri);
1665 result->spr_val = min_t(uint32_t, result->spr_val, max->spr);
1666 result->cur_val = min_t(uint32_t, result->cur_val, max->cur);
1667 result->enable = true;
1673 static void ilk_compute_wm_level(const struct drm_i915_private *dev_priv,
1675 const struct ilk_pipe_wm_parameters *p,
1676 struct intel_wm_level *result)
1678 uint16_t pri_latency = dev_priv->wm.pri_latency[level];
1679 uint16_t spr_latency = dev_priv->wm.spr_latency[level];
1680 uint16_t cur_latency = dev_priv->wm.cur_latency[level];
1682 /* WM1+ latency values stored in 0.5us units */
1689 result->pri_val = ilk_compute_pri_wm(p, pri_latency, level);
1690 result->spr_val = ilk_compute_spr_wm(p, spr_latency);
1691 result->cur_val = ilk_compute_cur_wm(p, cur_latency);
1692 result->fbc_val = ilk_compute_fbc_wm(p, result->pri_val);
1693 result->enable = true;
1697 hsw_compute_linetime_wm(struct drm_device *dev, struct drm_crtc *crtc)
1699 struct drm_i915_private *dev_priv = dev->dev_private;
1700 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1701 struct drm_display_mode *mode = &intel_crtc->config->base.adjusted_mode;
1702 u32 linetime, ips_linetime;
1704 if (!intel_crtc_active(crtc))
1707 /* The WM are computed with base on how long it takes to fill a single
1708 * row at the given clock rate, multiplied by 8.
1710 linetime = DIV_ROUND_CLOSEST(mode->crtc_htotal * 1000 * 8,
1712 ips_linetime = DIV_ROUND_CLOSEST(mode->crtc_htotal * 1000 * 8,
1713 intel_ddi_get_cdclk_freq(dev_priv));
1715 return PIPE_WM_LINETIME_IPS_LINETIME(ips_linetime) |
1716 PIPE_WM_LINETIME_TIME(linetime);
1719 static void intel_read_wm_latency(struct drm_device *dev, uint16_t wm[8])
1721 struct drm_i915_private *dev_priv = dev->dev_private;
1726 int level, max_level = ilk_wm_max_level(dev);
1728 /* read the first set of memory latencies[0:3] */
1729 val = 0; /* data0 to be programmed to 0 for first set */
1730 mutex_lock(&dev_priv->rps.hw_lock);
1731 ret = sandybridge_pcode_read(dev_priv,
1732 GEN9_PCODE_READ_MEM_LATENCY,
1734 mutex_unlock(&dev_priv->rps.hw_lock);
1737 DRM_ERROR("SKL Mailbox read error = %d\n", ret);
1741 wm[0] = val & GEN9_MEM_LATENCY_LEVEL_MASK;
1742 wm[1] = (val >> GEN9_MEM_LATENCY_LEVEL_1_5_SHIFT) &
1743 GEN9_MEM_LATENCY_LEVEL_MASK;
1744 wm[2] = (val >> GEN9_MEM_LATENCY_LEVEL_2_6_SHIFT) &
1745 GEN9_MEM_LATENCY_LEVEL_MASK;
1746 wm[3] = (val >> GEN9_MEM_LATENCY_LEVEL_3_7_SHIFT) &
1747 GEN9_MEM_LATENCY_LEVEL_MASK;
1749 /* read the second set of memory latencies[4:7] */
1750 val = 1; /* data0 to be programmed to 1 for second set */
1751 mutex_lock(&dev_priv->rps.hw_lock);
1752 ret = sandybridge_pcode_read(dev_priv,
1753 GEN9_PCODE_READ_MEM_LATENCY,
1755 mutex_unlock(&dev_priv->rps.hw_lock);
1757 DRM_ERROR("SKL Mailbox read error = %d\n", ret);
1761 wm[4] = val & GEN9_MEM_LATENCY_LEVEL_MASK;
1762 wm[5] = (val >> GEN9_MEM_LATENCY_LEVEL_1_5_SHIFT) &
1763 GEN9_MEM_LATENCY_LEVEL_MASK;
1764 wm[6] = (val >> GEN9_MEM_LATENCY_LEVEL_2_6_SHIFT) &
1765 GEN9_MEM_LATENCY_LEVEL_MASK;
1766 wm[7] = (val >> GEN9_MEM_LATENCY_LEVEL_3_7_SHIFT) &
1767 GEN9_MEM_LATENCY_LEVEL_MASK;
1770 * WaWmMemoryReadLatency:skl
1772 * punit doesn't take into account the read latency so we need
1773 * to add 2us to the various latency levels we retrieve from
1775 * - W0 is a bit special in that it's the only level that
1776 * can't be disabled if we want to have display working, so
1777 * we always add 2us there.
1778 * - For levels >=1, punit returns 0us latency when they are
1779 * disabled, so we respect that and don't add 2us then
1781 * Additionally, if a level n (n > 1) has a 0us latency, all
1782 * levels m (m >= n) need to be disabled. We make sure to
1783 * sanitize the values out of the punit to satisfy this
1787 for (level = 1; level <= max_level; level++)
1791 for (i = level + 1; i <= max_level; i++)
1796 } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
1797 uint64_t sskpd = I915_READ64(MCH_SSKPD);
1799 wm[0] = (sskpd >> 56) & 0xFF;
1801 wm[0] = sskpd & 0xF;
1802 wm[1] = (sskpd >> 4) & 0xFF;
1803 wm[2] = (sskpd >> 12) & 0xFF;
1804 wm[3] = (sskpd >> 20) & 0x1FF;
1805 wm[4] = (sskpd >> 32) & 0x1FF;
1806 } else if (INTEL_INFO(dev)->gen >= 6) {
1807 uint32_t sskpd = I915_READ(MCH_SSKPD);
1809 wm[0] = (sskpd >> SSKPD_WM0_SHIFT) & SSKPD_WM_MASK;
1810 wm[1] = (sskpd >> SSKPD_WM1_SHIFT) & SSKPD_WM_MASK;
1811 wm[2] = (sskpd >> SSKPD_WM2_SHIFT) & SSKPD_WM_MASK;
1812 wm[3] = (sskpd >> SSKPD_WM3_SHIFT) & SSKPD_WM_MASK;
1813 } else if (INTEL_INFO(dev)->gen >= 5) {
1814 uint32_t mltr = I915_READ(MLTR_ILK);
1816 /* ILK primary LP0 latency is 700 ns */
1818 wm[1] = (mltr >> MLTR_WM1_SHIFT) & ILK_SRLT_MASK;
1819 wm[2] = (mltr >> MLTR_WM2_SHIFT) & ILK_SRLT_MASK;
1823 static void intel_fixup_spr_wm_latency(struct drm_device *dev, uint16_t wm[5])
1825 /* ILK sprite LP0 latency is 1300 ns */
1826 if (INTEL_INFO(dev)->gen == 5)
1830 static void intel_fixup_cur_wm_latency(struct drm_device *dev, uint16_t wm[5])
1832 /* ILK cursor LP0 latency is 1300 ns */
1833 if (INTEL_INFO(dev)->gen == 5)
1836 /* WaDoubleCursorLP3Latency:ivb */
1837 if (IS_IVYBRIDGE(dev))
1841 int ilk_wm_max_level(const struct drm_device *dev)
1843 /* how many WM levels are we expecting */
1846 else if (IS_HASWELL(dev) || IS_BROADWELL(dev))
1848 else if (INTEL_INFO(dev)->gen >= 6)
1854 static void intel_print_wm_latency(struct drm_device *dev,
1856 const uint16_t wm[8])
1858 int level, max_level = ilk_wm_max_level(dev);
1860 for (level = 0; level <= max_level; level++) {
1861 unsigned int latency = wm[level];
1864 DRM_ERROR("%s WM%d latency not provided\n",
1870 * - latencies are in us on gen9.
1871 * - before then, WM1+ latency values are in 0.5us units
1878 DRM_DEBUG_KMS("%s WM%d latency %u (%u.%u usec)\n",
1879 name, level, wm[level],
1880 latency / 10, latency % 10);
1884 static bool ilk_increase_wm_latency(struct drm_i915_private *dev_priv,
1885 uint16_t wm[5], uint16_t min)
1887 int level, max_level = ilk_wm_max_level(dev_priv->dev);
1892 wm[0] = max(wm[0], min);
1893 for (level = 1; level <= max_level; level++)
1894 wm[level] = max_t(uint16_t, wm[level], DIV_ROUND_UP(min, 5));
1899 static void snb_wm_latency_quirk(struct drm_device *dev)
1901 struct drm_i915_private *dev_priv = dev->dev_private;
1905 * The BIOS provided WM memory latency values are often
1906 * inadequate for high resolution displays. Adjust them.
1908 changed = ilk_increase_wm_latency(dev_priv, dev_priv->wm.pri_latency, 12) |
1909 ilk_increase_wm_latency(dev_priv, dev_priv->wm.spr_latency, 12) |
1910 ilk_increase_wm_latency(dev_priv, dev_priv->wm.cur_latency, 12);
1915 DRM_DEBUG_KMS("WM latency values increased to avoid potential underruns\n");
1916 intel_print_wm_latency(dev, "Primary", dev_priv->wm.pri_latency);
1917 intel_print_wm_latency(dev, "Sprite", dev_priv->wm.spr_latency);
1918 intel_print_wm_latency(dev, "Cursor", dev_priv->wm.cur_latency);
1921 static void ilk_setup_wm_latency(struct drm_device *dev)
1923 struct drm_i915_private *dev_priv = dev->dev_private;
1925 intel_read_wm_latency(dev, dev_priv->wm.pri_latency);
1927 memcpy(dev_priv->wm.spr_latency, dev_priv->wm.pri_latency,
1928 sizeof(dev_priv->wm.pri_latency));
1929 memcpy(dev_priv->wm.cur_latency, dev_priv->wm.pri_latency,
1930 sizeof(dev_priv->wm.pri_latency));
1932 intel_fixup_spr_wm_latency(dev, dev_priv->wm.spr_latency);
1933 intel_fixup_cur_wm_latency(dev, dev_priv->wm.cur_latency);
1935 intel_print_wm_latency(dev, "Primary", dev_priv->wm.pri_latency);
1936 intel_print_wm_latency(dev, "Sprite", dev_priv->wm.spr_latency);
1937 intel_print_wm_latency(dev, "Cursor", dev_priv->wm.cur_latency);
1940 snb_wm_latency_quirk(dev);
1943 static void skl_setup_wm_latency(struct drm_device *dev)
1945 struct drm_i915_private *dev_priv = dev->dev_private;
1947 intel_read_wm_latency(dev, dev_priv->wm.skl_latency);
1948 intel_print_wm_latency(dev, "Gen9 Plane", dev_priv->wm.skl_latency);
1951 static void ilk_compute_wm_parameters(struct drm_crtc *crtc,
1952 struct ilk_pipe_wm_parameters *p)
1954 struct drm_device *dev = crtc->dev;
1955 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1956 enum pipe pipe = intel_crtc->pipe;
1957 struct drm_plane *plane;
1959 if (!intel_crtc_active(crtc))
1963 p->pipe_htotal = intel_crtc->config->base.adjusted_mode.crtc_htotal;
1964 p->pixel_rate = ilk_pipe_pixel_rate(dev, crtc);
1965 p->pri.bytes_per_pixel = crtc->primary->state->fb->bits_per_pixel / 8;
1966 p->cur.bytes_per_pixel = 4;
1967 p->pri.horiz_pixels = intel_crtc->config->pipe_src_w;
1968 p->cur.horiz_pixels = intel_crtc->base.cursor->state->crtc_w;
1969 /* TODO: for now, assume primary and cursor planes are always enabled. */
1970 p->pri.enabled = true;
1971 p->cur.enabled = true;
1973 drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
1974 struct intel_plane *intel_plane = to_intel_plane(plane);
1976 if (intel_plane->pipe == pipe) {
1977 p->spr = intel_plane->wm;
1983 static void ilk_compute_wm_config(struct drm_device *dev,
1984 struct intel_wm_config *config)
1986 struct intel_crtc *intel_crtc;
1988 /* Compute the currently _active_ config */
1989 for_each_intel_crtc(dev, intel_crtc) {
1990 const struct intel_pipe_wm *wm = &intel_crtc->wm.active;
1992 if (!wm->pipe_enabled)
1995 config->sprites_enabled |= wm->sprites_enabled;
1996 config->sprites_scaled |= wm->sprites_scaled;
1997 config->num_pipes_active++;
2001 /* Compute new watermarks for the pipe */
2002 static bool intel_compute_pipe_wm(struct drm_crtc *crtc,
2003 const struct ilk_pipe_wm_parameters *params,
2004 struct intel_pipe_wm *pipe_wm)
2006 struct drm_device *dev = crtc->dev;
2007 const struct drm_i915_private *dev_priv = dev->dev_private;
2008 int level, max_level = ilk_wm_max_level(dev);
2009 /* LP0 watermark maximums depend on this pipe alone */
2010 struct intel_wm_config config = {
2011 .num_pipes_active = 1,
2012 .sprites_enabled = params->spr.enabled,
2013 .sprites_scaled = params->spr.scaled,
2015 struct ilk_wm_maximums max;
2017 pipe_wm->pipe_enabled = params->active;
2018 pipe_wm->sprites_enabled = params->spr.enabled;
2019 pipe_wm->sprites_scaled = params->spr.scaled;
2021 /* ILK/SNB: LP2+ watermarks only w/o sprites */
2022 if (INTEL_INFO(dev)->gen <= 6 && params->spr.enabled)
2025 /* ILK/SNB/IVB: LP1+ watermarks only w/o scaling */
2026 if (params->spr.scaled)
2029 ilk_compute_wm_level(dev_priv, 0, params, &pipe_wm->wm[0]);
2031 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
2032 pipe_wm->linetime = hsw_compute_linetime_wm(dev, crtc);
2034 /* LP0 watermarks always use 1/2 DDB partitioning */
2035 ilk_compute_wm_maximums(dev, 0, &config, INTEL_DDB_PART_1_2, &max);
2037 /* At least LP0 must be valid */
2038 if (!ilk_validate_wm_level(0, &max, &pipe_wm->wm[0]))
2041 ilk_compute_wm_reg_maximums(dev, 1, &max);
2043 for (level = 1; level <= max_level; level++) {
2044 struct intel_wm_level wm = {};
2046 ilk_compute_wm_level(dev_priv, level, params, &wm);
2049 * Disable any watermark level that exceeds the
2050 * register maximums since such watermarks are
2053 if (!ilk_validate_wm_level(level, &max, &wm))
2056 pipe_wm->wm[level] = wm;
2063 * Merge the watermarks from all active pipes for a specific level.
2065 static void ilk_merge_wm_level(struct drm_device *dev,
2067 struct intel_wm_level *ret_wm)
2069 const struct intel_crtc *intel_crtc;
2071 ret_wm->enable = true;
2073 for_each_intel_crtc(dev, intel_crtc) {
2074 const struct intel_pipe_wm *active = &intel_crtc->wm.active;
2075 const struct intel_wm_level *wm = &active->wm[level];
2077 if (!active->pipe_enabled)
2081 * The watermark values may have been used in the past,
2082 * so we must maintain them in the registers for some
2083 * time even if the level is now disabled.
2086 ret_wm->enable = false;
2088 ret_wm->pri_val = max(ret_wm->pri_val, wm->pri_val);
2089 ret_wm->spr_val = max(ret_wm->spr_val, wm->spr_val);
2090 ret_wm->cur_val = max(ret_wm->cur_val, wm->cur_val);
2091 ret_wm->fbc_val = max(ret_wm->fbc_val, wm->fbc_val);
2096 * Merge all low power watermarks for all active pipes.
2098 static void ilk_wm_merge(struct drm_device *dev,
2099 const struct intel_wm_config *config,
2100 const struct ilk_wm_maximums *max,
2101 struct intel_pipe_wm *merged)
2103 int level, max_level = ilk_wm_max_level(dev);
2104 int last_enabled_level = max_level;
2106 /* ILK/SNB/IVB: LP1+ watermarks only w/ single pipe */
2107 if ((INTEL_INFO(dev)->gen <= 6 || IS_IVYBRIDGE(dev)) &&
2108 config->num_pipes_active > 1)
2111 /* ILK: FBC WM must be disabled always */
2112 merged->fbc_wm_enabled = INTEL_INFO(dev)->gen >= 6;
2114 /* merge each WM1+ level */
2115 for (level = 1; level <= max_level; level++) {
2116 struct intel_wm_level *wm = &merged->wm[level];
2118 ilk_merge_wm_level(dev, level, wm);
2120 if (level > last_enabled_level)
2122 else if (!ilk_validate_wm_level(level, max, wm))
2123 /* make sure all following levels get disabled */
2124 last_enabled_level = level - 1;
2127 * The spec says it is preferred to disable
2128 * FBC WMs instead of disabling a WM level.
2130 if (wm->fbc_val > max->fbc) {
2132 merged->fbc_wm_enabled = false;
2137 /* ILK: LP2+ must be disabled when FBC WM is disabled but FBC enabled */
2139 * FIXME this is racy. FBC might get enabled later.
2140 * What we should check here is whether FBC can be
2141 * enabled sometime later.
2143 if (IS_GEN5(dev) && !merged->fbc_wm_enabled && intel_fbc_enabled(dev)) {
2144 for (level = 2; level <= max_level; level++) {
2145 struct intel_wm_level *wm = &merged->wm[level];
2152 static int ilk_wm_lp_to_level(int wm_lp, const struct intel_pipe_wm *pipe_wm)
2154 /* LP1,LP2,LP3 levels are either 1,2,3 or 1,3,4 */
2155 return wm_lp + (wm_lp >= 2 && pipe_wm->wm[4].enable);
2158 /* The value we need to program into the WM_LPx latency field */
2159 static unsigned int ilk_wm_lp_latency(struct drm_device *dev, int level)
2161 struct drm_i915_private *dev_priv = dev->dev_private;
2163 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
2166 return dev_priv->wm.pri_latency[level];
2169 static void ilk_compute_wm_results(struct drm_device *dev,
2170 const struct intel_pipe_wm *merged,
2171 enum intel_ddb_partitioning partitioning,
2172 struct ilk_wm_values *results)
2174 struct intel_crtc *intel_crtc;
2177 results->enable_fbc_wm = merged->fbc_wm_enabled;
2178 results->partitioning = partitioning;
2180 /* LP1+ register values */
2181 for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
2182 const struct intel_wm_level *r;
2184 level = ilk_wm_lp_to_level(wm_lp, merged);
2186 r = &merged->wm[level];
2189 * Maintain the watermark values even if the level is
2190 * disabled. Doing otherwise could cause underruns.
2192 results->wm_lp[wm_lp - 1] =
2193 (ilk_wm_lp_latency(dev, level) << WM1_LP_LATENCY_SHIFT) |
2194 (r->pri_val << WM1_LP_SR_SHIFT) |
2198 results->wm_lp[wm_lp - 1] |= WM1_LP_SR_EN;
2200 if (INTEL_INFO(dev)->gen >= 8)
2201 results->wm_lp[wm_lp - 1] |=
2202 r->fbc_val << WM1_LP_FBC_SHIFT_BDW;
2204 results->wm_lp[wm_lp - 1] |=
2205 r->fbc_val << WM1_LP_FBC_SHIFT;
2208 * Always set WM1S_LP_EN when spr_val != 0, even if the
2209 * level is disabled. Doing otherwise could cause underruns.
2211 if (INTEL_INFO(dev)->gen <= 6 && r->spr_val) {
2212 WARN_ON(wm_lp != 1);
2213 results->wm_lp_spr[wm_lp - 1] = WM1S_LP_EN | r->spr_val;
2215 results->wm_lp_spr[wm_lp - 1] = r->spr_val;
2218 /* LP0 register values */
2219 for_each_intel_crtc(dev, intel_crtc) {
2220 enum pipe pipe = intel_crtc->pipe;
2221 const struct intel_wm_level *r =
2222 &intel_crtc->wm.active.wm[0];
2224 if (WARN_ON(!r->enable))
2227 results->wm_linetime[pipe] = intel_crtc->wm.active.linetime;
2229 results->wm_pipe[pipe] =
2230 (r->pri_val << WM0_PIPE_PLANE_SHIFT) |
2231 (r->spr_val << WM0_PIPE_SPRITE_SHIFT) |
2236 /* Find the result with the highest level enabled. Check for enable_fbc_wm in
2237 * case both are at the same level. Prefer r1 in case they're the same. */
2238 static struct intel_pipe_wm *ilk_find_best_result(struct drm_device *dev,
2239 struct intel_pipe_wm *r1,
2240 struct intel_pipe_wm *r2)
2242 int level, max_level = ilk_wm_max_level(dev);
2243 int level1 = 0, level2 = 0;
2245 for (level = 1; level <= max_level; level++) {
2246 if (r1->wm[level].enable)
2248 if (r2->wm[level].enable)
2252 if (level1 == level2) {
2253 if (r2->fbc_wm_enabled && !r1->fbc_wm_enabled)
2257 } else if (level1 > level2) {
2264 /* dirty bits used to track which watermarks need changes */
2265 #define WM_DIRTY_PIPE(pipe) (1 << (pipe))
2266 #define WM_DIRTY_LINETIME(pipe) (1 << (8 + (pipe)))
2267 #define WM_DIRTY_LP(wm_lp) (1 << (15 + (wm_lp)))
2268 #define WM_DIRTY_LP_ALL (WM_DIRTY_LP(1) | WM_DIRTY_LP(2) | WM_DIRTY_LP(3))
2269 #define WM_DIRTY_FBC (1 << 24)
2270 #define WM_DIRTY_DDB (1 << 25)
2272 static unsigned int ilk_compute_wm_dirty(struct drm_i915_private *dev_priv,
2273 const struct ilk_wm_values *old,
2274 const struct ilk_wm_values *new)
2276 unsigned int dirty = 0;
2280 for_each_pipe(dev_priv, pipe) {
2281 if (old->wm_linetime[pipe] != new->wm_linetime[pipe]) {
2282 dirty |= WM_DIRTY_LINETIME(pipe);
2283 /* Must disable LP1+ watermarks too */
2284 dirty |= WM_DIRTY_LP_ALL;
2287 if (old->wm_pipe[pipe] != new->wm_pipe[pipe]) {
2288 dirty |= WM_DIRTY_PIPE(pipe);
2289 /* Must disable LP1+ watermarks too */
2290 dirty |= WM_DIRTY_LP_ALL;
2294 if (old->enable_fbc_wm != new->enable_fbc_wm) {
2295 dirty |= WM_DIRTY_FBC;
2296 /* Must disable LP1+ watermarks too */
2297 dirty |= WM_DIRTY_LP_ALL;
2300 if (old->partitioning != new->partitioning) {
2301 dirty |= WM_DIRTY_DDB;
2302 /* Must disable LP1+ watermarks too */
2303 dirty |= WM_DIRTY_LP_ALL;
2306 /* LP1+ watermarks already deemed dirty, no need to continue */
2307 if (dirty & WM_DIRTY_LP_ALL)
2310 /* Find the lowest numbered LP1+ watermark in need of an update... */
2311 for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
2312 if (old->wm_lp[wm_lp - 1] != new->wm_lp[wm_lp - 1] ||
2313 old->wm_lp_spr[wm_lp - 1] != new->wm_lp_spr[wm_lp - 1])
2317 /* ...and mark it and all higher numbered LP1+ watermarks as dirty */
2318 for (; wm_lp <= 3; wm_lp++)
2319 dirty |= WM_DIRTY_LP(wm_lp);
2324 static bool _ilk_disable_lp_wm(struct drm_i915_private *dev_priv,
2327 struct ilk_wm_values *previous = &dev_priv->wm.hw;
2328 bool changed = false;
2330 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] & WM1_LP_SR_EN) {
2331 previous->wm_lp[2] &= ~WM1_LP_SR_EN;
2332 I915_WRITE(WM3_LP_ILK, previous->wm_lp[2]);
2335 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] & WM1_LP_SR_EN) {
2336 previous->wm_lp[1] &= ~WM1_LP_SR_EN;
2337 I915_WRITE(WM2_LP_ILK, previous->wm_lp[1]);
2340 if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] & WM1_LP_SR_EN) {
2341 previous->wm_lp[0] &= ~WM1_LP_SR_EN;
2342 I915_WRITE(WM1_LP_ILK, previous->wm_lp[0]);
2347 * Don't touch WM1S_LP_EN here.
2348 * Doing so could cause underruns.
2355 * The spec says we shouldn't write when we don't need, because every write
2356 * causes WMs to be re-evaluated, expending some power.
2358 static void ilk_write_wm_values(struct drm_i915_private *dev_priv,
2359 struct ilk_wm_values *results)
2361 struct drm_device *dev = dev_priv->dev;
2362 struct ilk_wm_values *previous = &dev_priv->wm.hw;
2366 dirty = ilk_compute_wm_dirty(dev_priv, previous, results);
2370 _ilk_disable_lp_wm(dev_priv, dirty);
2372 if (dirty & WM_DIRTY_PIPE(PIPE_A))
2373 I915_WRITE(WM0_PIPEA_ILK, results->wm_pipe[0]);
2374 if (dirty & WM_DIRTY_PIPE(PIPE_B))
2375 I915_WRITE(WM0_PIPEB_ILK, results->wm_pipe[1]);
2376 if (dirty & WM_DIRTY_PIPE(PIPE_C))
2377 I915_WRITE(WM0_PIPEC_IVB, results->wm_pipe[2]);
2379 if (dirty & WM_DIRTY_LINETIME(PIPE_A))
2380 I915_WRITE(PIPE_WM_LINETIME(PIPE_A), results->wm_linetime[0]);
2381 if (dirty & WM_DIRTY_LINETIME(PIPE_B))
2382 I915_WRITE(PIPE_WM_LINETIME(PIPE_B), results->wm_linetime[1]);
2383 if (dirty & WM_DIRTY_LINETIME(PIPE_C))
2384 I915_WRITE(PIPE_WM_LINETIME(PIPE_C), results->wm_linetime[2]);
2386 if (dirty & WM_DIRTY_DDB) {
2387 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
2388 val = I915_READ(WM_MISC);
2389 if (results->partitioning == INTEL_DDB_PART_1_2)
2390 val &= ~WM_MISC_DATA_PARTITION_5_6;
2392 val |= WM_MISC_DATA_PARTITION_5_6;
2393 I915_WRITE(WM_MISC, val);
2395 val = I915_READ(DISP_ARB_CTL2);
2396 if (results->partitioning == INTEL_DDB_PART_1_2)
2397 val &= ~DISP_DATA_PARTITION_5_6;
2399 val |= DISP_DATA_PARTITION_5_6;
2400 I915_WRITE(DISP_ARB_CTL2, val);
2404 if (dirty & WM_DIRTY_FBC) {
2405 val = I915_READ(DISP_ARB_CTL);
2406 if (results->enable_fbc_wm)
2407 val &= ~DISP_FBC_WM_DIS;
2409 val |= DISP_FBC_WM_DIS;
2410 I915_WRITE(DISP_ARB_CTL, val);
2413 if (dirty & WM_DIRTY_LP(1) &&
2414 previous->wm_lp_spr[0] != results->wm_lp_spr[0])
2415 I915_WRITE(WM1S_LP_ILK, results->wm_lp_spr[0]);
2417 if (INTEL_INFO(dev)->gen >= 7) {
2418 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp_spr[1] != results->wm_lp_spr[1])
2419 I915_WRITE(WM2S_LP_IVB, results->wm_lp_spr[1]);
2420 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp_spr[2] != results->wm_lp_spr[2])
2421 I915_WRITE(WM3S_LP_IVB, results->wm_lp_spr[2]);
2424 if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] != results->wm_lp[0])
2425 I915_WRITE(WM1_LP_ILK, results->wm_lp[0]);
2426 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] != results->wm_lp[1])
2427 I915_WRITE(WM2_LP_ILK, results->wm_lp[1]);
2428 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] != results->wm_lp[2])
2429 I915_WRITE(WM3_LP_ILK, results->wm_lp[2]);
2431 dev_priv->wm.hw = *results;
2434 static bool ilk_disable_lp_wm(struct drm_device *dev)
2436 struct drm_i915_private *dev_priv = dev->dev_private;
2438 return _ilk_disable_lp_wm(dev_priv, WM_DIRTY_LP_ALL);
2442 * On gen9, we need to allocate Display Data Buffer (DDB) portions to the
2443 * different active planes.
2446 #define SKL_DDB_SIZE 896 /* in blocks */
2449 skl_ddb_get_pipe_allocation_limits(struct drm_device *dev,
2450 struct drm_crtc *for_crtc,
2451 const struct intel_wm_config *config,
2452 const struct skl_pipe_wm_parameters *params,
2453 struct skl_ddb_entry *alloc /* out */)
2455 struct drm_crtc *crtc;
2456 unsigned int pipe_size, ddb_size;
2457 int nth_active_pipe;
2459 if (!params->active) {
2465 ddb_size = SKL_DDB_SIZE;
2467 ddb_size -= 4; /* 4 blocks for bypass path allocation */
2469 nth_active_pipe = 0;
2470 for_each_crtc(dev, crtc) {
2471 if (!intel_crtc_active(crtc))
2474 if (crtc == for_crtc)
2480 pipe_size = ddb_size / config->num_pipes_active;
2481 alloc->start = nth_active_pipe * ddb_size / config->num_pipes_active;
2482 alloc->end = alloc->start + pipe_size;
2485 static unsigned int skl_cursor_allocation(const struct intel_wm_config *config)
2487 if (config->num_pipes_active == 1)
2493 static void skl_ddb_entry_init_from_hw(struct skl_ddb_entry *entry, u32 reg)
2495 entry->start = reg & 0x3ff;
2496 entry->end = (reg >> 16) & 0x3ff;
2501 void skl_ddb_get_hw_state(struct drm_i915_private *dev_priv,
2502 struct skl_ddb_allocation *ddb /* out */)
2508 for_each_pipe(dev_priv, pipe) {
2509 for_each_plane(dev_priv, pipe, plane) {
2510 val = I915_READ(PLANE_BUF_CFG(pipe, plane));
2511 skl_ddb_entry_init_from_hw(&ddb->plane[pipe][plane],
2515 val = I915_READ(CUR_BUF_CFG(pipe));
2516 skl_ddb_entry_init_from_hw(&ddb->cursor[pipe], val);
2521 skl_plane_relative_data_rate(const struct intel_plane_wm_parameters *p)
2523 return p->horiz_pixels * p->vert_pixels * p->bytes_per_pixel;
2527 * We don't overflow 32 bits. Worst case is 3 planes enabled, each fetching
2528 * a 8192x4096@32bpp framebuffer:
2529 * 3 * 4096 * 8192 * 4 < 2^32
2532 skl_get_total_relative_data_rate(struct intel_crtc *intel_crtc,
2533 const struct skl_pipe_wm_parameters *params)
2535 unsigned int total_data_rate = 0;
2538 for (plane = 0; plane < intel_num_planes(intel_crtc); plane++) {
2539 const struct intel_plane_wm_parameters *p;
2541 p = ¶ms->plane[plane];
2545 total_data_rate += skl_plane_relative_data_rate(p);
2548 return total_data_rate;
2552 skl_allocate_pipe_ddb(struct drm_crtc *crtc,
2553 const struct intel_wm_config *config,
2554 const struct skl_pipe_wm_parameters *params,
2555 struct skl_ddb_allocation *ddb /* out */)
2557 struct drm_device *dev = crtc->dev;
2558 struct drm_i915_private *dev_priv = dev->dev_private;
2559 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2560 enum pipe pipe = intel_crtc->pipe;
2561 struct skl_ddb_entry *alloc = &ddb->pipe[pipe];
2562 uint16_t alloc_size, start, cursor_blocks;
2563 uint16_t minimum[I915_MAX_PLANES];
2564 unsigned int total_data_rate;
2567 skl_ddb_get_pipe_allocation_limits(dev, crtc, config, params, alloc);
2568 alloc_size = skl_ddb_entry_size(alloc);
2569 if (alloc_size == 0) {
2570 memset(ddb->plane[pipe], 0, sizeof(ddb->plane[pipe]));
2571 memset(&ddb->cursor[pipe], 0, sizeof(ddb->cursor[pipe]));
2575 cursor_blocks = skl_cursor_allocation(config);
2576 ddb->cursor[pipe].start = alloc->end - cursor_blocks;
2577 ddb->cursor[pipe].end = alloc->end;
2579 alloc_size -= cursor_blocks;
2580 alloc->end -= cursor_blocks;
2582 /* 1. Allocate the mininum required blocks for each active plane */
2583 for_each_plane(dev_priv, pipe, plane) {
2584 const struct intel_plane_wm_parameters *p;
2586 p = ¶ms->plane[plane];
2591 alloc_size -= minimum[plane];
2595 * 2. Distribute the remaining space in proportion to the amount of
2596 * data each plane needs to fetch from memory.
2598 * FIXME: we may not allocate every single block here.
2600 total_data_rate = skl_get_total_relative_data_rate(intel_crtc, params);
2602 start = alloc->start;
2603 for (plane = 0; plane < intel_num_planes(intel_crtc); plane++) {
2604 const struct intel_plane_wm_parameters *p;
2605 unsigned int data_rate;
2606 uint16_t plane_blocks;
2608 p = ¶ms->plane[plane];
2612 data_rate = skl_plane_relative_data_rate(p);
2615 * promote the expression to 64 bits to avoid overflowing, the
2616 * result is < available as data_rate / total_data_rate < 1
2618 plane_blocks = minimum[plane];
2619 plane_blocks += div_u64((uint64_t)alloc_size * data_rate,
2622 ddb->plane[pipe][plane].start = start;
2623 ddb->plane[pipe][plane].end = start + plane_blocks;
2625 start += plane_blocks;
2630 static uint32_t skl_pipe_pixel_rate(const struct intel_crtc_state *config)
2632 /* TODO: Take into account the scalers once we support them */
2633 return config->base.adjusted_mode.crtc_clock;
2637 * The max latency should be 257 (max the punit can code is 255 and we add 2us
2638 * for the read latency) and bytes_per_pixel should always be <= 8, so that
2639 * should allow pixel_rate up to ~2 GHz which seems sufficient since max
2640 * 2xcdclk is 1350 MHz and the pixel rate should never exceed that.
2642 static uint32_t skl_wm_method1(uint32_t pixel_rate, uint8_t bytes_per_pixel,
2645 uint32_t wm_intermediate_val, ret;
2650 wm_intermediate_val = latency * pixel_rate * bytes_per_pixel / 512;
2651 ret = DIV_ROUND_UP(wm_intermediate_val, 1000);
2656 static uint32_t skl_wm_method2(uint32_t pixel_rate, uint32_t pipe_htotal,
2657 uint32_t horiz_pixels, uint8_t bytes_per_pixel,
2658 uint64_t tiling, uint32_t latency)
2661 uint32_t plane_bytes_per_line, plane_blocks_per_line;
2662 uint32_t wm_intermediate_val;
2667 plane_bytes_per_line = horiz_pixels * bytes_per_pixel;
2669 if (tiling == I915_FORMAT_MOD_Y_TILED ||
2670 tiling == I915_FORMAT_MOD_Yf_TILED) {
2671 plane_bytes_per_line *= 4;
2672 plane_blocks_per_line = DIV_ROUND_UP(plane_bytes_per_line, 512);
2673 plane_blocks_per_line /= 4;
2675 plane_blocks_per_line = DIV_ROUND_UP(plane_bytes_per_line, 512);
2678 wm_intermediate_val = latency * pixel_rate;
2679 ret = DIV_ROUND_UP(wm_intermediate_val, pipe_htotal * 1000) *
2680 plane_blocks_per_line;
2685 static bool skl_ddb_allocation_changed(const struct skl_ddb_allocation *new_ddb,
2686 const struct intel_crtc *intel_crtc)
2688 struct drm_device *dev = intel_crtc->base.dev;
2689 struct drm_i915_private *dev_priv = dev->dev_private;
2690 const struct skl_ddb_allocation *cur_ddb = &dev_priv->wm.skl_hw.ddb;
2691 enum pipe pipe = intel_crtc->pipe;
2693 if (memcmp(new_ddb->plane[pipe], cur_ddb->plane[pipe],
2694 sizeof(new_ddb->plane[pipe])))
2697 if (memcmp(&new_ddb->cursor[pipe], &cur_ddb->cursor[pipe],
2698 sizeof(new_ddb->cursor[pipe])))
2704 static void skl_compute_wm_global_parameters(struct drm_device *dev,
2705 struct intel_wm_config *config)
2707 struct drm_crtc *crtc;
2708 struct drm_plane *plane;
2710 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
2711 config->num_pipes_active += intel_crtc_active(crtc);
2713 /* FIXME: I don't think we need those two global parameters on SKL */
2714 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
2715 struct intel_plane *intel_plane = to_intel_plane(plane);
2717 config->sprites_enabled |= intel_plane->wm.enabled;
2718 config->sprites_scaled |= intel_plane->wm.scaled;
2722 static void skl_compute_wm_pipe_parameters(struct drm_crtc *crtc,
2723 struct skl_pipe_wm_parameters *p)
2725 struct drm_device *dev = crtc->dev;
2726 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2727 enum pipe pipe = intel_crtc->pipe;
2728 struct drm_plane *plane;
2729 struct drm_framebuffer *fb;
2730 int i = 1; /* Index for sprite planes start */
2732 p->active = intel_crtc_active(crtc);
2734 p->pipe_htotal = intel_crtc->config->base.adjusted_mode.crtc_htotal;
2735 p->pixel_rate = skl_pipe_pixel_rate(intel_crtc->config);
2738 * For now, assume primary and cursor planes are always enabled.
2740 p->plane[0].enabled = true;
2741 p->plane[0].bytes_per_pixel =
2742 crtc->primary->state->fb->bits_per_pixel / 8;
2743 p->plane[0].horiz_pixels = intel_crtc->config->pipe_src_w;
2744 p->plane[0].vert_pixels = intel_crtc->config->pipe_src_h;
2745 p->plane[0].tiling = DRM_FORMAT_MOD_NONE;
2746 fb = crtc->primary->state->fb;
2748 * Framebuffer can be NULL on plane disable, but it does not
2749 * matter for watermarks if we assume no tiling in that case.
2752 p->plane[0].tiling = fb->modifier[0];
2754 p->cursor.enabled = true;
2755 p->cursor.bytes_per_pixel = 4;
2756 p->cursor.horiz_pixels = intel_crtc->base.cursor->state->crtc_w ?
2757 intel_crtc->base.cursor->state->crtc_w : 64;
2760 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
2761 struct intel_plane *intel_plane = to_intel_plane(plane);
2763 if (intel_plane->pipe == pipe &&
2764 plane->type == DRM_PLANE_TYPE_OVERLAY)
2765 p->plane[i++] = intel_plane->wm;
2769 static bool skl_compute_plane_wm(const struct drm_i915_private *dev_priv,
2770 struct skl_pipe_wm_parameters *p,
2771 struct intel_plane_wm_parameters *p_params,
2772 uint16_t ddb_allocation,
2774 uint16_t *out_blocks, /* out */
2775 uint8_t *out_lines /* out */)
2777 uint32_t latency = dev_priv->wm.skl_latency[level];
2778 uint32_t method1, method2;
2779 uint32_t plane_bytes_per_line, plane_blocks_per_line;
2780 uint32_t res_blocks, res_lines;
2781 uint32_t selected_result;
2783 if (latency == 0 || !p->active || !p_params->enabled)
2786 method1 = skl_wm_method1(p->pixel_rate,
2787 p_params->bytes_per_pixel,
2789 method2 = skl_wm_method2(p->pixel_rate,
2791 p_params->horiz_pixels,
2792 p_params->bytes_per_pixel,
2796 plane_bytes_per_line = p_params->horiz_pixels *
2797 p_params->bytes_per_pixel;
2798 plane_blocks_per_line = DIV_ROUND_UP(plane_bytes_per_line, 512);
2800 if (p_params->tiling == I915_FORMAT_MOD_Y_TILED ||
2801 p_params->tiling == I915_FORMAT_MOD_Yf_TILED) {
2802 uint32_t y_tile_minimum = plane_blocks_per_line * 4;
2803 selected_result = max(method2, y_tile_minimum);
2805 if ((ddb_allocation / plane_blocks_per_line) >= 1)
2806 selected_result = min(method1, method2);
2808 selected_result = method1;
2811 res_blocks = selected_result + 1;
2812 res_lines = DIV_ROUND_UP(selected_result, plane_blocks_per_line);
2814 if (level >= 1 && level <= 7) {
2815 if (p_params->tiling == I915_FORMAT_MOD_Y_TILED ||
2816 p_params->tiling == I915_FORMAT_MOD_Yf_TILED)
2822 if (res_blocks >= ddb_allocation || res_lines > 31)
2825 *out_blocks = res_blocks;
2826 *out_lines = res_lines;
2831 static void skl_compute_wm_level(const struct drm_i915_private *dev_priv,
2832 struct skl_ddb_allocation *ddb,
2833 struct skl_pipe_wm_parameters *p,
2837 struct skl_wm_level *result)
2839 uint16_t ddb_blocks;
2842 for (i = 0; i < num_planes; i++) {
2843 ddb_blocks = skl_ddb_entry_size(&ddb->plane[pipe][i]);
2845 result->plane_en[i] = skl_compute_plane_wm(dev_priv,
2849 &result->plane_res_b[i],
2850 &result->plane_res_l[i]);
2853 ddb_blocks = skl_ddb_entry_size(&ddb->cursor[pipe]);
2854 result->cursor_en = skl_compute_plane_wm(dev_priv, p, &p->cursor,
2856 &result->cursor_res_b,
2857 &result->cursor_res_l);
2861 skl_compute_linetime_wm(struct drm_crtc *crtc, struct skl_pipe_wm_parameters *p)
2863 if (!intel_crtc_active(crtc))
2866 return DIV_ROUND_UP(8 * p->pipe_htotal * 1000, p->pixel_rate);
2870 static void skl_compute_transition_wm(struct drm_crtc *crtc,
2871 struct skl_pipe_wm_parameters *params,
2872 struct skl_wm_level *trans_wm /* out */)
2874 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2877 if (!params->active)
2880 /* Until we know more, just disable transition WMs */
2881 for (i = 0; i < intel_num_planes(intel_crtc); i++)
2882 trans_wm->plane_en[i] = false;
2883 trans_wm->cursor_en = false;
2886 static void skl_compute_pipe_wm(struct drm_crtc *crtc,
2887 struct skl_ddb_allocation *ddb,
2888 struct skl_pipe_wm_parameters *params,
2889 struct skl_pipe_wm *pipe_wm)
2891 struct drm_device *dev = crtc->dev;
2892 const struct drm_i915_private *dev_priv = dev->dev_private;
2893 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2894 int level, max_level = ilk_wm_max_level(dev);
2896 for (level = 0; level <= max_level; level++) {
2897 skl_compute_wm_level(dev_priv, ddb, params, intel_crtc->pipe,
2898 level, intel_num_planes(intel_crtc),
2899 &pipe_wm->wm[level]);
2901 pipe_wm->linetime = skl_compute_linetime_wm(crtc, params);
2903 skl_compute_transition_wm(crtc, params, &pipe_wm->trans_wm);
2906 static void skl_compute_wm_results(struct drm_device *dev,
2907 struct skl_pipe_wm_parameters *p,
2908 struct skl_pipe_wm *p_wm,
2909 struct skl_wm_values *r,
2910 struct intel_crtc *intel_crtc)
2912 int level, max_level = ilk_wm_max_level(dev);
2913 enum pipe pipe = intel_crtc->pipe;
2917 for (level = 0; level <= max_level; level++) {
2918 for (i = 0; i < intel_num_planes(intel_crtc); i++) {
2921 temp |= p_wm->wm[level].plane_res_l[i] <<
2922 PLANE_WM_LINES_SHIFT;
2923 temp |= p_wm->wm[level].plane_res_b[i];
2924 if (p_wm->wm[level].plane_en[i])
2925 temp |= PLANE_WM_EN;
2927 r->plane[pipe][i][level] = temp;
2932 temp |= p_wm->wm[level].cursor_res_l << PLANE_WM_LINES_SHIFT;
2933 temp |= p_wm->wm[level].cursor_res_b;
2935 if (p_wm->wm[level].cursor_en)
2936 temp |= PLANE_WM_EN;
2938 r->cursor[pipe][level] = temp;
2942 /* transition WMs */
2943 for (i = 0; i < intel_num_planes(intel_crtc); i++) {
2945 temp |= p_wm->trans_wm.plane_res_l[i] << PLANE_WM_LINES_SHIFT;
2946 temp |= p_wm->trans_wm.plane_res_b[i];
2947 if (p_wm->trans_wm.plane_en[i])
2948 temp |= PLANE_WM_EN;
2950 r->plane_trans[pipe][i] = temp;
2954 temp |= p_wm->trans_wm.cursor_res_l << PLANE_WM_LINES_SHIFT;
2955 temp |= p_wm->trans_wm.cursor_res_b;
2956 if (p_wm->trans_wm.cursor_en)
2957 temp |= PLANE_WM_EN;
2959 r->cursor_trans[pipe] = temp;
2961 r->wm_linetime[pipe] = p_wm->linetime;
2964 static void skl_ddb_entry_write(struct drm_i915_private *dev_priv, uint32_t reg,
2965 const struct skl_ddb_entry *entry)
2968 I915_WRITE(reg, (entry->end - 1) << 16 | entry->start);
2973 static void skl_write_wm_values(struct drm_i915_private *dev_priv,
2974 const struct skl_wm_values *new)
2976 struct drm_device *dev = dev_priv->dev;
2977 struct intel_crtc *crtc;
2979 list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head) {
2980 int i, level, max_level = ilk_wm_max_level(dev);
2981 enum pipe pipe = crtc->pipe;
2983 if (!new->dirty[pipe])
2986 I915_WRITE(PIPE_WM_LINETIME(pipe), new->wm_linetime[pipe]);
2988 for (level = 0; level <= max_level; level++) {
2989 for (i = 0; i < intel_num_planes(crtc); i++)
2990 I915_WRITE(PLANE_WM(pipe, i, level),
2991 new->plane[pipe][i][level]);
2992 I915_WRITE(CUR_WM(pipe, level),
2993 new->cursor[pipe][level]);
2995 for (i = 0; i < intel_num_planes(crtc); i++)
2996 I915_WRITE(PLANE_WM_TRANS(pipe, i),
2997 new->plane_trans[pipe][i]);
2998 I915_WRITE(CUR_WM_TRANS(pipe), new->cursor_trans[pipe]);
3000 for (i = 0; i < intel_num_planes(crtc); i++)
3001 skl_ddb_entry_write(dev_priv,
3002 PLANE_BUF_CFG(pipe, i),
3003 &new->ddb.plane[pipe][i]);
3005 skl_ddb_entry_write(dev_priv, CUR_BUF_CFG(pipe),
3006 &new->ddb.cursor[pipe]);
3011 * When setting up a new DDB allocation arrangement, we need to correctly
3012 * sequence the times at which the new allocations for the pipes are taken into
3013 * account or we'll have pipes fetching from space previously allocated to
3016 * Roughly the sequence looks like:
3017 * 1. re-allocate the pipe(s) with the allocation being reduced and not
3018 * overlapping with a previous light-up pipe (another way to put it is:
3019 * pipes with their new allocation strickly included into their old ones).
3020 * 2. re-allocate the other pipes that get their allocation reduced
3021 * 3. allocate the pipes having their allocation increased
3023 * Steps 1. and 2. are here to take care of the following case:
3024 * - Initially DDB looks like this:
3027 * - pipe B has a reduced DDB allocation that overlaps with the old pipe C
3031 * We need to sequence the re-allocation: C, B, A (and not B, C, A).
3035 skl_wm_flush_pipe(struct drm_i915_private *dev_priv, enum pipe pipe, int pass)
3039 DRM_DEBUG_KMS("flush pipe %c (pass %d)\n", pipe_name(pipe), pass);
3041 for_each_plane(dev_priv, pipe, plane) {
3042 I915_WRITE(PLANE_SURF(pipe, plane),
3043 I915_READ(PLANE_SURF(pipe, plane)));
3045 I915_WRITE(CURBASE(pipe), I915_READ(CURBASE(pipe)));
3049 skl_ddb_allocation_included(const struct skl_ddb_allocation *old,
3050 const struct skl_ddb_allocation *new,
3053 uint16_t old_size, new_size;
3055 old_size = skl_ddb_entry_size(&old->pipe[pipe]);
3056 new_size = skl_ddb_entry_size(&new->pipe[pipe]);
3058 return old_size != new_size &&
3059 new->pipe[pipe].start >= old->pipe[pipe].start &&
3060 new->pipe[pipe].end <= old->pipe[pipe].end;
3063 static void skl_flush_wm_values(struct drm_i915_private *dev_priv,
3064 struct skl_wm_values *new_values)
3066 struct drm_device *dev = dev_priv->dev;
3067 struct skl_ddb_allocation *cur_ddb, *new_ddb;
3068 bool reallocated[I915_MAX_PIPES] = {false, false, false};
3069 struct intel_crtc *crtc;
3072 new_ddb = &new_values->ddb;
3073 cur_ddb = &dev_priv->wm.skl_hw.ddb;
3076 * First pass: flush the pipes with the new allocation contained into
3079 * We'll wait for the vblank on those pipes to ensure we can safely
3080 * re-allocate the freed space without this pipe fetching from it.
3082 for_each_intel_crtc(dev, crtc) {
3088 if (!skl_ddb_allocation_included(cur_ddb, new_ddb, pipe))
3091 skl_wm_flush_pipe(dev_priv, pipe, 1);
3092 intel_wait_for_vblank(dev, pipe);
3094 reallocated[pipe] = true;
3099 * Second pass: flush the pipes that are having their allocation
3100 * reduced, but overlapping with a previous allocation.
3102 * Here as well we need to wait for the vblank to make sure the freed
3103 * space is not used anymore.
3105 for_each_intel_crtc(dev, crtc) {
3111 if (reallocated[pipe])
3114 if (skl_ddb_entry_size(&new_ddb->pipe[pipe]) <
3115 skl_ddb_entry_size(&cur_ddb->pipe[pipe])) {
3116 skl_wm_flush_pipe(dev_priv, pipe, 2);
3117 intel_wait_for_vblank(dev, pipe);
3118 reallocated[pipe] = true;
3123 * Third pass: flush the pipes that got more space allocated.
3125 * We don't need to actively wait for the update here, next vblank
3126 * will just get more DDB space with the correct WM values.
3128 for_each_intel_crtc(dev, crtc) {
3135 * At this point, only the pipes more space than before are
3136 * left to re-allocate.
3138 if (reallocated[pipe])
3141 skl_wm_flush_pipe(dev_priv, pipe, 3);
3145 static bool skl_update_pipe_wm(struct drm_crtc *crtc,
3146 struct skl_pipe_wm_parameters *params,
3147 struct intel_wm_config *config,
3148 struct skl_ddb_allocation *ddb, /* out */
3149 struct skl_pipe_wm *pipe_wm /* out */)
3151 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3153 skl_compute_wm_pipe_parameters(crtc, params);
3154 skl_allocate_pipe_ddb(crtc, config, params, ddb);
3155 skl_compute_pipe_wm(crtc, ddb, params, pipe_wm);
3157 if (!memcmp(&intel_crtc->wm.skl_active, pipe_wm, sizeof(*pipe_wm)))
3160 intel_crtc->wm.skl_active = *pipe_wm;
3164 static void skl_update_other_pipe_wm(struct drm_device *dev,
3165 struct drm_crtc *crtc,
3166 struct intel_wm_config *config,
3167 struct skl_wm_values *r)
3169 struct intel_crtc *intel_crtc;
3170 struct intel_crtc *this_crtc = to_intel_crtc(crtc);
3173 * If the WM update hasn't changed the allocation for this_crtc (the
3174 * crtc we are currently computing the new WM values for), other
3175 * enabled crtcs will keep the same allocation and we don't need to
3176 * recompute anything for them.
3178 if (!skl_ddb_allocation_changed(&r->ddb, this_crtc))
3182 * Otherwise, because of this_crtc being freshly enabled/disabled, the
3183 * other active pipes need new DDB allocation and WM values.
3185 list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
3187 struct skl_pipe_wm_parameters params = {};
3188 struct skl_pipe_wm pipe_wm = {};
3191 if (this_crtc->pipe == intel_crtc->pipe)
3194 if (!intel_crtc->active)
3197 wm_changed = skl_update_pipe_wm(&intel_crtc->base,
3202 * If we end up re-computing the other pipe WM values, it's
3203 * because it was really needed, so we expect the WM values to
3206 WARN_ON(!wm_changed);
3208 skl_compute_wm_results(dev, ¶ms, &pipe_wm, r, intel_crtc);
3209 r->dirty[intel_crtc->pipe] = true;
3213 static void skl_update_wm(struct drm_crtc *crtc)
3215 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3216 struct drm_device *dev = crtc->dev;
3217 struct drm_i915_private *dev_priv = dev->dev_private;
3218 struct skl_pipe_wm_parameters params = {};
3219 struct skl_wm_values *results = &dev_priv->wm.skl_results;
3220 struct skl_pipe_wm pipe_wm = {};
3221 struct intel_wm_config config = {};
3223 memset(results, 0, sizeof(*results));
3225 skl_compute_wm_global_parameters(dev, &config);
3227 if (!skl_update_pipe_wm(crtc, ¶ms, &config,
3228 &results->ddb, &pipe_wm))
3231 skl_compute_wm_results(dev, ¶ms, &pipe_wm, results, intel_crtc);
3232 results->dirty[intel_crtc->pipe] = true;
3234 skl_update_other_pipe_wm(dev, crtc, &config, results);
3235 skl_write_wm_values(dev_priv, results);
3236 skl_flush_wm_values(dev_priv, results);
3238 /* store the new configuration */
3239 dev_priv->wm.skl_hw = *results;
3243 skl_update_sprite_wm(struct drm_plane *plane, struct drm_crtc *crtc,
3244 uint32_t sprite_width, uint32_t sprite_height,
3245 int pixel_size, bool enabled, bool scaled)
3247 struct intel_plane *intel_plane = to_intel_plane(plane);
3248 struct drm_framebuffer *fb = plane->state->fb;
3250 intel_plane->wm.enabled = enabled;
3251 intel_plane->wm.scaled = scaled;
3252 intel_plane->wm.horiz_pixels = sprite_width;
3253 intel_plane->wm.vert_pixels = sprite_height;
3254 intel_plane->wm.bytes_per_pixel = pixel_size;
3255 intel_plane->wm.tiling = DRM_FORMAT_MOD_NONE;
3257 * Framebuffer can be NULL on plane disable, but it does not
3258 * matter for watermarks if we assume no tiling in that case.
3261 intel_plane->wm.tiling = fb->modifier[0];
3263 skl_update_wm(crtc);
3266 static void ilk_update_wm(struct drm_crtc *crtc)
3268 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3269 struct drm_device *dev = crtc->dev;
3270 struct drm_i915_private *dev_priv = dev->dev_private;
3271 struct ilk_wm_maximums max;
3272 struct ilk_pipe_wm_parameters params = {};
3273 struct ilk_wm_values results = {};
3274 enum intel_ddb_partitioning partitioning;
3275 struct intel_pipe_wm pipe_wm = {};
3276 struct intel_pipe_wm lp_wm_1_2 = {}, lp_wm_5_6 = {}, *best_lp_wm;
3277 struct intel_wm_config config = {};
3279 ilk_compute_wm_parameters(crtc, ¶ms);
3281 intel_compute_pipe_wm(crtc, ¶ms, &pipe_wm);
3283 if (!memcmp(&intel_crtc->wm.active, &pipe_wm, sizeof(pipe_wm)))
3286 intel_crtc->wm.active = pipe_wm;
3288 ilk_compute_wm_config(dev, &config);
3290 ilk_compute_wm_maximums(dev, 1, &config, INTEL_DDB_PART_1_2, &max);
3291 ilk_wm_merge(dev, &config, &max, &lp_wm_1_2);
3293 /* 5/6 split only in single pipe config on IVB+ */
3294 if (INTEL_INFO(dev)->gen >= 7 &&
3295 config.num_pipes_active == 1 && config.sprites_enabled) {
3296 ilk_compute_wm_maximums(dev, 1, &config, INTEL_DDB_PART_5_6, &max);
3297 ilk_wm_merge(dev, &config, &max, &lp_wm_5_6);
3299 best_lp_wm = ilk_find_best_result(dev, &lp_wm_1_2, &lp_wm_5_6);
3301 best_lp_wm = &lp_wm_1_2;
3304 partitioning = (best_lp_wm == &lp_wm_1_2) ?
3305 INTEL_DDB_PART_1_2 : INTEL_DDB_PART_5_6;
3307 ilk_compute_wm_results(dev, best_lp_wm, partitioning, &results);
3309 ilk_write_wm_values(dev_priv, &results);
3313 ilk_update_sprite_wm(struct drm_plane *plane,
3314 struct drm_crtc *crtc,
3315 uint32_t sprite_width, uint32_t sprite_height,
3316 int pixel_size, bool enabled, bool scaled)
3318 struct drm_device *dev = plane->dev;
3319 struct intel_plane *intel_plane = to_intel_plane(plane);
3321 intel_plane->wm.enabled = enabled;
3322 intel_plane->wm.scaled = scaled;
3323 intel_plane->wm.horiz_pixels = sprite_width;
3324 intel_plane->wm.vert_pixels = sprite_width;
3325 intel_plane->wm.bytes_per_pixel = pixel_size;
3328 * IVB workaround: must disable low power watermarks for at least
3329 * one frame before enabling scaling. LP watermarks can be re-enabled
3330 * when scaling is disabled.
3332 * WaCxSRDisabledForSpriteScaling:ivb
3334 if (IS_IVYBRIDGE(dev) && scaled && ilk_disable_lp_wm(dev))
3335 intel_wait_for_vblank(dev, intel_plane->pipe);
3337 ilk_update_wm(crtc);
3340 static void skl_pipe_wm_active_state(uint32_t val,
3341 struct skl_pipe_wm *active,
3347 bool is_enabled = (val & PLANE_WM_EN) != 0;
3351 active->wm[level].plane_en[i] = is_enabled;
3352 active->wm[level].plane_res_b[i] =
3353 val & PLANE_WM_BLOCKS_MASK;
3354 active->wm[level].plane_res_l[i] =
3355 (val >> PLANE_WM_LINES_SHIFT) &
3356 PLANE_WM_LINES_MASK;
3358 active->wm[level].cursor_en = is_enabled;
3359 active->wm[level].cursor_res_b =
3360 val & PLANE_WM_BLOCKS_MASK;
3361 active->wm[level].cursor_res_l =
3362 (val >> PLANE_WM_LINES_SHIFT) &
3363 PLANE_WM_LINES_MASK;
3367 active->trans_wm.plane_en[i] = is_enabled;
3368 active->trans_wm.plane_res_b[i] =
3369 val & PLANE_WM_BLOCKS_MASK;
3370 active->trans_wm.plane_res_l[i] =
3371 (val >> PLANE_WM_LINES_SHIFT) &
3372 PLANE_WM_LINES_MASK;
3374 active->trans_wm.cursor_en = is_enabled;
3375 active->trans_wm.cursor_res_b =
3376 val & PLANE_WM_BLOCKS_MASK;
3377 active->trans_wm.cursor_res_l =
3378 (val >> PLANE_WM_LINES_SHIFT) &
3379 PLANE_WM_LINES_MASK;
3384 static void skl_pipe_wm_get_hw_state(struct drm_crtc *crtc)
3386 struct drm_device *dev = crtc->dev;
3387 struct drm_i915_private *dev_priv = dev->dev_private;
3388 struct skl_wm_values *hw = &dev_priv->wm.skl_hw;
3389 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3390 struct skl_pipe_wm *active = &intel_crtc->wm.skl_active;
3391 enum pipe pipe = intel_crtc->pipe;
3392 int level, i, max_level;
3395 max_level = ilk_wm_max_level(dev);
3397 hw->wm_linetime[pipe] = I915_READ(PIPE_WM_LINETIME(pipe));
3399 for (level = 0; level <= max_level; level++) {
3400 for (i = 0; i < intel_num_planes(intel_crtc); i++)
3401 hw->plane[pipe][i][level] =
3402 I915_READ(PLANE_WM(pipe, i, level));
3403 hw->cursor[pipe][level] = I915_READ(CUR_WM(pipe, level));
3406 for (i = 0; i < intel_num_planes(intel_crtc); i++)
3407 hw->plane_trans[pipe][i] = I915_READ(PLANE_WM_TRANS(pipe, i));
3408 hw->cursor_trans[pipe] = I915_READ(CUR_WM_TRANS(pipe));
3410 if (!intel_crtc_active(crtc))
3413 hw->dirty[pipe] = true;
3415 active->linetime = hw->wm_linetime[pipe];
3417 for (level = 0; level <= max_level; level++) {
3418 for (i = 0; i < intel_num_planes(intel_crtc); i++) {
3419 temp = hw->plane[pipe][i][level];
3420 skl_pipe_wm_active_state(temp, active, false,
3423 temp = hw->cursor[pipe][level];
3424 skl_pipe_wm_active_state(temp, active, false, true, i, level);
3427 for (i = 0; i < intel_num_planes(intel_crtc); i++) {
3428 temp = hw->plane_trans[pipe][i];
3429 skl_pipe_wm_active_state(temp, active, true, false, i, 0);
3432 temp = hw->cursor_trans[pipe];
3433 skl_pipe_wm_active_state(temp, active, true, true, i, 0);
3436 void skl_wm_get_hw_state(struct drm_device *dev)
3438 struct drm_i915_private *dev_priv = dev->dev_private;
3439 struct skl_ddb_allocation *ddb = &dev_priv->wm.skl_hw.ddb;
3440 struct drm_crtc *crtc;
3442 skl_ddb_get_hw_state(dev_priv, ddb);
3443 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
3444 skl_pipe_wm_get_hw_state(crtc);
3447 static void ilk_pipe_wm_get_hw_state(struct drm_crtc *crtc)
3449 struct drm_device *dev = crtc->dev;
3450 struct drm_i915_private *dev_priv = dev->dev_private;
3451 struct ilk_wm_values *hw = &dev_priv->wm.hw;
3452 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3453 struct intel_pipe_wm *active = &intel_crtc->wm.active;
3454 enum pipe pipe = intel_crtc->pipe;
3455 static const unsigned int wm0_pipe_reg[] = {
3456 [PIPE_A] = WM0_PIPEA_ILK,
3457 [PIPE_B] = WM0_PIPEB_ILK,
3458 [PIPE_C] = WM0_PIPEC_IVB,
3461 hw->wm_pipe[pipe] = I915_READ(wm0_pipe_reg[pipe]);
3462 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
3463 hw->wm_linetime[pipe] = I915_READ(PIPE_WM_LINETIME(pipe));
3465 active->pipe_enabled = intel_crtc_active(crtc);
3467 if (active->pipe_enabled) {
3468 u32 tmp = hw->wm_pipe[pipe];
3471 * For active pipes LP0 watermark is marked as
3472 * enabled, and LP1+ watermaks as disabled since
3473 * we can't really reverse compute them in case
3474 * multiple pipes are active.
3476 active->wm[0].enable = true;
3477 active->wm[0].pri_val = (tmp & WM0_PIPE_PLANE_MASK) >> WM0_PIPE_PLANE_SHIFT;
3478 active->wm[0].spr_val = (tmp & WM0_PIPE_SPRITE_MASK) >> WM0_PIPE_SPRITE_SHIFT;
3479 active->wm[0].cur_val = tmp & WM0_PIPE_CURSOR_MASK;
3480 active->linetime = hw->wm_linetime[pipe];
3482 int level, max_level = ilk_wm_max_level(dev);
3485 * For inactive pipes, all watermark levels
3486 * should be marked as enabled but zeroed,
3487 * which is what we'd compute them to.
3489 for (level = 0; level <= max_level; level++)
3490 active->wm[level].enable = true;
3494 void ilk_wm_get_hw_state(struct drm_device *dev)
3496 struct drm_i915_private *dev_priv = dev->dev_private;
3497 struct ilk_wm_values *hw = &dev_priv->wm.hw;
3498 struct drm_crtc *crtc;
3500 for_each_crtc(dev, crtc)
3501 ilk_pipe_wm_get_hw_state(crtc);
3503 hw->wm_lp[0] = I915_READ(WM1_LP_ILK);
3504 hw->wm_lp[1] = I915_READ(WM2_LP_ILK);
3505 hw->wm_lp[2] = I915_READ(WM3_LP_ILK);
3507 hw->wm_lp_spr[0] = I915_READ(WM1S_LP_ILK);
3508 if (INTEL_INFO(dev)->gen >= 7) {
3509 hw->wm_lp_spr[1] = I915_READ(WM2S_LP_IVB);
3510 hw->wm_lp_spr[2] = I915_READ(WM3S_LP_IVB);
3513 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
3514 hw->partitioning = (I915_READ(WM_MISC) & WM_MISC_DATA_PARTITION_5_6) ?
3515 INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
3516 else if (IS_IVYBRIDGE(dev))
3517 hw->partitioning = (I915_READ(DISP_ARB_CTL2) & DISP_DATA_PARTITION_5_6) ?
3518 INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
3521 !(I915_READ(DISP_ARB_CTL) & DISP_FBC_WM_DIS);
3525 * intel_update_watermarks - update FIFO watermark values based on current modes
3527 * Calculate watermark values for the various WM regs based on current mode
3528 * and plane configuration.
3530 * There are several cases to deal with here:
3531 * - normal (i.e. non-self-refresh)
3532 * - self-refresh (SR) mode
3533 * - lines are large relative to FIFO size (buffer can hold up to 2)
3534 * - lines are small relative to FIFO size (buffer can hold more than 2
3535 * lines), so need to account for TLB latency
3537 * The normal calculation is:
3538 * watermark = dotclock * bytes per pixel * latency
3539 * where latency is platform & configuration dependent (we assume pessimal
3542 * The SR calculation is:
3543 * watermark = (trunc(latency/line time)+1) * surface width *
3546 * line time = htotal / dotclock
3547 * surface width = hdisplay for normal plane and 64 for cursor
3548 * and latency is assumed to be high, as above.
3550 * The final value programmed to the register should always be rounded up,
3551 * and include an extra 2 entries to account for clock crossings.
3553 * We don't use the sprite, so we can ignore that. And on Crestline we have
3554 * to set the non-SR watermarks to 8.
3556 void intel_update_watermarks(struct drm_crtc *crtc)
3558 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
3560 if (dev_priv->display.update_wm)
3561 dev_priv->display.update_wm(crtc);
3564 void intel_update_sprite_watermarks(struct drm_plane *plane,
3565 struct drm_crtc *crtc,
3566 uint32_t sprite_width,
3567 uint32_t sprite_height,
3569 bool enabled, bool scaled)
3571 struct drm_i915_private *dev_priv = plane->dev->dev_private;
3573 if (dev_priv->display.update_sprite_wm)
3574 dev_priv->display.update_sprite_wm(plane, crtc,
3575 sprite_width, sprite_height,
3576 pixel_size, enabled, scaled);
3580 * Lock protecting IPS related data structures
3582 DEFINE_SPINLOCK(mchdev_lock);
3584 /* Global for IPS driver to get at the current i915 device. Protected by
3586 static struct drm_i915_private *i915_mch_dev;
3588 bool ironlake_set_drps(struct drm_device *dev, u8 val)
3590 struct drm_i915_private *dev_priv = dev->dev_private;
3593 assert_spin_locked(&mchdev_lock);
3595 rgvswctl = I915_READ16(MEMSWCTL);
3596 if (rgvswctl & MEMCTL_CMD_STS) {
3597 DRM_DEBUG("gpu busy, RCS change rejected\n");
3598 return false; /* still busy with another command */
3601 rgvswctl = (MEMCTL_CMD_CHFREQ << MEMCTL_CMD_SHIFT) |
3602 (val << MEMCTL_FREQ_SHIFT) | MEMCTL_SFCAVM;
3603 I915_WRITE16(MEMSWCTL, rgvswctl);
3604 POSTING_READ16(MEMSWCTL);
3606 rgvswctl |= MEMCTL_CMD_STS;
3607 I915_WRITE16(MEMSWCTL, rgvswctl);
3612 static void ironlake_enable_drps(struct drm_device *dev)
3614 struct drm_i915_private *dev_priv = dev->dev_private;
3615 u32 rgvmodectl = I915_READ(MEMMODECTL);
3616 u8 fmax, fmin, fstart, vstart;
3618 spin_lock_irq(&mchdev_lock);
3620 /* Enable temp reporting */
3621 I915_WRITE16(PMMISC, I915_READ(PMMISC) | MCPPCE_EN);
3622 I915_WRITE16(TSC1, I915_READ(TSC1) | TSE);
3624 /* 100ms RC evaluation intervals */
3625 I915_WRITE(RCUPEI, 100000);
3626 I915_WRITE(RCDNEI, 100000);
3628 /* Set max/min thresholds to 90ms and 80ms respectively */
3629 I915_WRITE(RCBMAXAVG, 90000);
3630 I915_WRITE(RCBMINAVG, 80000);
3632 I915_WRITE(MEMIHYST, 1);
3634 /* Set up min, max, and cur for interrupt handling */
3635 fmax = (rgvmodectl & MEMMODE_FMAX_MASK) >> MEMMODE_FMAX_SHIFT;
3636 fmin = (rgvmodectl & MEMMODE_FMIN_MASK);
3637 fstart = (rgvmodectl & MEMMODE_FSTART_MASK) >>
3638 MEMMODE_FSTART_SHIFT;
3640 vstart = (I915_READ(PXVFREQ_BASE + (fstart * 4)) & PXVFREQ_PX_MASK) >>
3643 dev_priv->ips.fmax = fmax; /* IPS callback will increase this */
3644 dev_priv->ips.fstart = fstart;
3646 dev_priv->ips.max_delay = fstart;
3647 dev_priv->ips.min_delay = fmin;
3648 dev_priv->ips.cur_delay = fstart;
3650 DRM_DEBUG_DRIVER("fmax: %d, fmin: %d, fstart: %d\n",
3651 fmax, fmin, fstart);
3653 I915_WRITE(MEMINTREN, MEMINT_CX_SUPR_EN | MEMINT_EVAL_CHG_EN);
3656 * Interrupts will be enabled in ironlake_irq_postinstall
3659 I915_WRITE(VIDSTART, vstart);
3660 POSTING_READ(VIDSTART);
3662 rgvmodectl |= MEMMODE_SWMODE_EN;
3663 I915_WRITE(MEMMODECTL, rgvmodectl);
3665 if (wait_for_atomic((I915_READ(MEMSWCTL) & MEMCTL_CMD_STS) == 0, 10))
3666 DRM_ERROR("stuck trying to change perf mode\n");
3669 ironlake_set_drps(dev, fstart);
3671 dev_priv->ips.last_count1 = I915_READ(0x112e4) + I915_READ(0x112e8) +
3673 dev_priv->ips.last_time1 = jiffies_to_msecs(jiffies);
3674 dev_priv->ips.last_count2 = I915_READ(0x112f4);
3675 dev_priv->ips.last_time2 = ktime_get_raw_ns();
3677 spin_unlock_irq(&mchdev_lock);
3680 static void ironlake_disable_drps(struct drm_device *dev)
3682 struct drm_i915_private *dev_priv = dev->dev_private;
3685 spin_lock_irq(&mchdev_lock);
3687 rgvswctl = I915_READ16(MEMSWCTL);
3689 /* Ack interrupts, disable EFC interrupt */
3690 I915_WRITE(MEMINTREN, I915_READ(MEMINTREN) & ~MEMINT_EVAL_CHG_EN);
3691 I915_WRITE(MEMINTRSTS, MEMINT_EVAL_CHG);
3692 I915_WRITE(DEIER, I915_READ(DEIER) & ~DE_PCU_EVENT);
3693 I915_WRITE(DEIIR, DE_PCU_EVENT);
3694 I915_WRITE(DEIMR, I915_READ(DEIMR) | DE_PCU_EVENT);
3696 /* Go back to the starting frequency */
3697 ironlake_set_drps(dev, dev_priv->ips.fstart);
3699 rgvswctl |= MEMCTL_CMD_STS;
3700 I915_WRITE(MEMSWCTL, rgvswctl);
3703 spin_unlock_irq(&mchdev_lock);
3706 /* There's a funny hw issue where the hw returns all 0 when reading from
3707 * GEN6_RP_INTERRUPT_LIMITS. Hence we always need to compute the desired value
3708 * ourselves, instead of doing a rmw cycle (which might result in us clearing
3709 * all limits and the gpu stuck at whatever frequency it is at atm).
3711 static u32 gen6_rps_limits(struct drm_i915_private *dev_priv, u8 val)
3715 /* Only set the down limit when we've reached the lowest level to avoid
3716 * getting more interrupts, otherwise leave this clear. This prevents a
3717 * race in the hw when coming out of rc6: There's a tiny window where
3718 * the hw runs at the minimal clock before selecting the desired
3719 * frequency, if the down threshold expires in that window we will not
3720 * receive a down interrupt. */
3721 limits = dev_priv->rps.max_freq_softlimit << 24;
3722 if (val <= dev_priv->rps.min_freq_softlimit)
3723 limits |= dev_priv->rps.min_freq_softlimit << 16;
3728 static void gen6_set_rps_thresholds(struct drm_i915_private *dev_priv, u8 val)
3732 new_power = dev_priv->rps.power;
3733 switch (dev_priv->rps.power) {
3735 if (val > dev_priv->rps.efficient_freq + 1 && val > dev_priv->rps.cur_freq)
3736 new_power = BETWEEN;
3740 if (val <= dev_priv->rps.efficient_freq && val < dev_priv->rps.cur_freq)
3741 new_power = LOW_POWER;
3742 else if (val >= dev_priv->rps.rp0_freq && val > dev_priv->rps.cur_freq)
3743 new_power = HIGH_POWER;
3747 if (val < (dev_priv->rps.rp1_freq + dev_priv->rps.rp0_freq) >> 1 && val < dev_priv->rps.cur_freq)
3748 new_power = BETWEEN;
3751 /* Max/min bins are special */
3752 if (val == dev_priv->rps.min_freq_softlimit)
3753 new_power = LOW_POWER;
3754 if (val == dev_priv->rps.max_freq_softlimit)
3755 new_power = HIGH_POWER;
3756 if (new_power == dev_priv->rps.power)
3759 /* Note the units here are not exactly 1us, but 1280ns. */
3760 switch (new_power) {
3762 /* Upclock if more than 95% busy over 16ms */
3763 I915_WRITE(GEN6_RP_UP_EI, 12500);
3764 I915_WRITE(GEN6_RP_UP_THRESHOLD, 11800);
3766 /* Downclock if less than 85% busy over 32ms */
3767 I915_WRITE(GEN6_RP_DOWN_EI, 25000);
3768 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 21250);
3770 I915_WRITE(GEN6_RP_CONTROL,
3771 GEN6_RP_MEDIA_TURBO |
3772 GEN6_RP_MEDIA_HW_NORMAL_MODE |
3773 GEN6_RP_MEDIA_IS_GFX |
3775 GEN6_RP_UP_BUSY_AVG |
3776 GEN6_RP_DOWN_IDLE_AVG);
3780 /* Upclock if more than 90% busy over 13ms */
3781 I915_WRITE(GEN6_RP_UP_EI, 10250);
3782 I915_WRITE(GEN6_RP_UP_THRESHOLD, 9225);
3784 /* Downclock if less than 75% busy over 32ms */
3785 I915_WRITE(GEN6_RP_DOWN_EI, 25000);
3786 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 18750);
3788 I915_WRITE(GEN6_RP_CONTROL,
3789 GEN6_RP_MEDIA_TURBO |
3790 GEN6_RP_MEDIA_HW_NORMAL_MODE |
3791 GEN6_RP_MEDIA_IS_GFX |
3793 GEN6_RP_UP_BUSY_AVG |
3794 GEN6_RP_DOWN_IDLE_AVG);
3798 /* Upclock if more than 85% busy over 10ms */
3799 I915_WRITE(GEN6_RP_UP_EI, 8000);
3800 I915_WRITE(GEN6_RP_UP_THRESHOLD, 6800);
3802 /* Downclock if less than 60% busy over 32ms */
3803 I915_WRITE(GEN6_RP_DOWN_EI, 25000);
3804 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 15000);
3806 I915_WRITE(GEN6_RP_CONTROL,
3807 GEN6_RP_MEDIA_TURBO |
3808 GEN6_RP_MEDIA_HW_NORMAL_MODE |
3809 GEN6_RP_MEDIA_IS_GFX |
3811 GEN6_RP_UP_BUSY_AVG |
3812 GEN6_RP_DOWN_IDLE_AVG);
3816 dev_priv->rps.power = new_power;
3817 dev_priv->rps.last_adj = 0;
3820 static u32 gen6_rps_pm_mask(struct drm_i915_private *dev_priv, u8 val)
3824 if (val > dev_priv->rps.min_freq_softlimit)
3825 mask |= GEN6_PM_RP_DOWN_THRESHOLD | GEN6_PM_RP_DOWN_TIMEOUT;
3826 if (val < dev_priv->rps.max_freq_softlimit)
3827 mask |= GEN6_PM_RP_UP_THRESHOLD;
3829 mask |= dev_priv->pm_rps_events & (GEN6_PM_RP_DOWN_EI_EXPIRED | GEN6_PM_RP_UP_EI_EXPIRED);
3830 mask &= dev_priv->pm_rps_events;
3832 return gen6_sanitize_rps_pm_mask(dev_priv, ~mask);
3835 /* gen6_set_rps is called to update the frequency request, but should also be
3836 * called when the range (min_delay and max_delay) is modified so that we can
3837 * update the GEN6_RP_INTERRUPT_LIMITS register accordingly. */
3838 static void gen6_set_rps(struct drm_device *dev, u8 val)
3840 struct drm_i915_private *dev_priv = dev->dev_private;
3842 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
3843 WARN_ON(val > dev_priv->rps.max_freq_softlimit);
3844 WARN_ON(val < dev_priv->rps.min_freq_softlimit);
3846 /* min/max delay may still have been modified so be sure to
3847 * write the limits value.
3849 if (val != dev_priv->rps.cur_freq) {
3850 gen6_set_rps_thresholds(dev_priv, val);
3852 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
3853 I915_WRITE(GEN6_RPNSWREQ,
3854 HSW_FREQUENCY(val));
3856 I915_WRITE(GEN6_RPNSWREQ,
3857 GEN6_FREQUENCY(val) |
3859 GEN6_AGGRESSIVE_TURBO);
3862 /* Make sure we continue to get interrupts
3863 * until we hit the minimum or maximum frequencies.
3865 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS, gen6_rps_limits(dev_priv, val));
3866 I915_WRITE(GEN6_PMINTRMSK, gen6_rps_pm_mask(dev_priv, val));
3868 POSTING_READ(GEN6_RPNSWREQ);
3870 dev_priv->rps.cur_freq = val;
3871 trace_intel_gpu_freq_change(val * 50);
3874 static void valleyview_set_rps(struct drm_device *dev, u8 val)
3876 struct drm_i915_private *dev_priv = dev->dev_private;
3878 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
3879 WARN_ON(val > dev_priv->rps.max_freq_softlimit);
3880 WARN_ON(val < dev_priv->rps.min_freq_softlimit);
3882 if (WARN_ONCE(IS_CHERRYVIEW(dev) && (val & 1),
3883 "Odd GPU freq value\n"))
3886 if (val != dev_priv->rps.cur_freq)
3887 vlv_punit_write(dev_priv, PUNIT_REG_GPU_FREQ_REQ, val);
3889 I915_WRITE(GEN6_PMINTRMSK, gen6_rps_pm_mask(dev_priv, val));
3891 dev_priv->rps.cur_freq = val;
3892 trace_intel_gpu_freq_change(intel_gpu_freq(dev_priv, val));
3895 /* vlv_set_rps_idle: Set the frequency to Rpn if Gfx clocks are down
3897 * * If Gfx is Idle, then
3898 * 1. Mask Turbo interrupts
3899 * 2. Bring up Gfx clock
3900 * 3. Change the freq to Rpn and wait till P-Unit updates freq
3901 * 4. Clear the Force GFX CLK ON bit so that Gfx can down
3902 * 5. Unmask Turbo interrupts
3904 static void vlv_set_rps_idle(struct drm_i915_private *dev_priv)
3906 struct drm_device *dev = dev_priv->dev;
3908 /* CHV and latest VLV don't need to force the gfx clock */
3909 if (IS_CHERRYVIEW(dev) || dev->pdev->revision >= 0xd) {
3910 valleyview_set_rps(dev_priv->dev, dev_priv->rps.min_freq_softlimit);
3915 * When we are idle. Drop to min voltage state.
3918 if (dev_priv->rps.cur_freq <= dev_priv->rps.min_freq_softlimit)
3921 /* Mask turbo interrupt so that they will not come in between */
3922 I915_WRITE(GEN6_PMINTRMSK,
3923 gen6_sanitize_rps_pm_mask(dev_priv, ~0));
3925 vlv_force_gfx_clock(dev_priv, true);
3927 dev_priv->rps.cur_freq = dev_priv->rps.min_freq_softlimit;
3929 vlv_punit_write(dev_priv, PUNIT_REG_GPU_FREQ_REQ,
3930 dev_priv->rps.min_freq_softlimit);
3932 if (wait_for(((vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS))
3933 & GENFREQSTATUS) == 0, 100))
3934 DRM_ERROR("timed out waiting for Punit\n");
3936 vlv_force_gfx_clock(dev_priv, false);
3938 I915_WRITE(GEN6_PMINTRMSK,
3939 gen6_rps_pm_mask(dev_priv, dev_priv->rps.cur_freq));
3942 void gen6_rps_idle(struct drm_i915_private *dev_priv)
3944 struct drm_device *dev = dev_priv->dev;
3946 mutex_lock(&dev_priv->rps.hw_lock);
3947 if (dev_priv->rps.enabled) {
3948 if (IS_VALLEYVIEW(dev))
3949 vlv_set_rps_idle(dev_priv);
3951 gen6_set_rps(dev_priv->dev, dev_priv->rps.min_freq_softlimit);
3952 dev_priv->rps.last_adj = 0;
3954 mutex_unlock(&dev_priv->rps.hw_lock);
3957 void gen6_rps_boost(struct drm_i915_private *dev_priv)
3959 mutex_lock(&dev_priv->rps.hw_lock);
3960 if (dev_priv->rps.enabled) {
3961 intel_set_rps(dev_priv->dev, dev_priv->rps.max_freq_softlimit);
3962 dev_priv->rps.last_adj = 0;
3964 mutex_unlock(&dev_priv->rps.hw_lock);
3967 void intel_set_rps(struct drm_device *dev, u8 val)
3969 if (IS_VALLEYVIEW(dev))
3970 valleyview_set_rps(dev, val);
3972 gen6_set_rps(dev, val);
3975 static void gen9_disable_rps(struct drm_device *dev)
3977 struct drm_i915_private *dev_priv = dev->dev_private;
3979 I915_WRITE(GEN6_RC_CONTROL, 0);
3980 I915_WRITE(GEN9_PG_ENABLE, 0);
3983 static void gen6_disable_rps(struct drm_device *dev)
3985 struct drm_i915_private *dev_priv = dev->dev_private;
3987 I915_WRITE(GEN6_RC_CONTROL, 0);
3988 I915_WRITE(GEN6_RPNSWREQ, 1 << 31);
3991 static void cherryview_disable_rps(struct drm_device *dev)
3993 struct drm_i915_private *dev_priv = dev->dev_private;
3995 I915_WRITE(GEN6_RC_CONTROL, 0);
3998 static void valleyview_disable_rps(struct drm_device *dev)
4000 struct drm_i915_private *dev_priv = dev->dev_private;
4002 /* we're doing forcewake before Disabling RC6,
4003 * This what the BIOS expects when going into suspend */
4004 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
4006 I915_WRITE(GEN6_RC_CONTROL, 0);
4008 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
4011 static void intel_print_rc6_info(struct drm_device *dev, u32 mode)
4013 if (IS_VALLEYVIEW(dev)) {
4014 if (mode & (GEN7_RC_CTL_TO_MODE | GEN6_RC_CTL_EI_MODE(1)))
4015 mode = GEN6_RC_CTL_RC6_ENABLE;
4020 DRM_DEBUG_KMS("Enabling RC6 states: RC6 %s RC6p %s RC6pp %s\n",
4021 (mode & GEN6_RC_CTL_RC6_ENABLE) ? "on" : "off",
4022 (mode & GEN6_RC_CTL_RC6p_ENABLE) ? "on" : "off",
4023 (mode & GEN6_RC_CTL_RC6pp_ENABLE) ? "on" : "off");
4026 DRM_DEBUG_KMS("Enabling RC6 states: RC6 %s\n",
4027 (mode & GEN6_RC_CTL_RC6_ENABLE) ? "on" : "off");
4030 static int sanitize_rc6_option(const struct drm_device *dev, int enable_rc6)
4032 /* No RC6 before Ironlake */
4033 if (INTEL_INFO(dev)->gen < 5)
4036 /* RC6 is only on Ironlake mobile not on desktop */
4037 if (INTEL_INFO(dev)->gen == 5 && !IS_IRONLAKE_M(dev))
4040 /* Respect the kernel parameter if it is set */
4041 if (enable_rc6 >= 0) {
4045 mask = INTEL_RC6_ENABLE | INTEL_RC6p_ENABLE |
4048 mask = INTEL_RC6_ENABLE;
4050 if ((enable_rc6 & mask) != enable_rc6)
4051 DRM_DEBUG_KMS("Adjusting RC6 mask to %d (requested %d, valid %d)\n",
4052 enable_rc6 & mask, enable_rc6, mask);
4054 return enable_rc6 & mask;
4057 /* Disable RC6 on Ironlake */
4058 if (INTEL_INFO(dev)->gen == 5)
4061 if (IS_IVYBRIDGE(dev))
4062 return (INTEL_RC6_ENABLE | INTEL_RC6p_ENABLE);
4064 return INTEL_RC6_ENABLE;
4067 int intel_enable_rc6(const struct drm_device *dev)
4069 return i915.enable_rc6;
4072 static void gen6_init_rps_frequencies(struct drm_device *dev)
4074 struct drm_i915_private *dev_priv = dev->dev_private;
4075 uint32_t rp_state_cap;
4076 u32 ddcc_status = 0;
4079 rp_state_cap = I915_READ(GEN6_RP_STATE_CAP);
4080 /* All of these values are in units of 50MHz */
4081 dev_priv->rps.cur_freq = 0;
4082 /* static values from HW: RP0 > RP1 > RPn (min_freq) */
4083 dev_priv->rps.rp0_freq = (rp_state_cap >> 0) & 0xff;
4084 dev_priv->rps.rp1_freq = (rp_state_cap >> 8) & 0xff;
4085 dev_priv->rps.min_freq = (rp_state_cap >> 16) & 0xff;
4086 /* hw_max = RP0 until we check for overclocking */
4087 dev_priv->rps.max_freq = dev_priv->rps.rp0_freq;
4089 dev_priv->rps.efficient_freq = dev_priv->rps.rp1_freq;
4090 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
4091 ret = sandybridge_pcode_read(dev_priv,
4092 HSW_PCODE_DYNAMIC_DUTY_CYCLE_CONTROL,
4095 dev_priv->rps.efficient_freq =
4097 ((ddcc_status >> 8) & 0xff),
4098 dev_priv->rps.min_freq,
4099 dev_priv->rps.max_freq);
4102 /* Preserve min/max settings in case of re-init */
4103 if (dev_priv->rps.max_freq_softlimit == 0)
4104 dev_priv->rps.max_freq_softlimit = dev_priv->rps.max_freq;
4106 if (dev_priv->rps.min_freq_softlimit == 0) {
4107 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
4108 dev_priv->rps.min_freq_softlimit =
4109 /* max(RPe, 450 MHz) */
4110 max(dev_priv->rps.efficient_freq, (u8) 9);
4112 dev_priv->rps.min_freq_softlimit =
4113 dev_priv->rps.min_freq;
4117 /* See the Gen9_GT_PM_Programming_Guide doc for the below */
4118 static void gen9_enable_rps(struct drm_device *dev)
4120 struct drm_i915_private *dev_priv = dev->dev_private;
4122 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
4124 gen6_init_rps_frequencies(dev);
4126 I915_WRITE(GEN6_RPNSWREQ, 0xc800000);
4127 I915_WRITE(GEN6_RC_VIDEO_FREQ, 0xc800000);
4129 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 0xf4240);
4130 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS, 0x12060000);
4131 I915_WRITE(GEN6_RP_UP_THRESHOLD, 0xe808);
4132 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 0x3bd08);
4133 I915_WRITE(GEN6_RP_UP_EI, 0x101d0);
4134 I915_WRITE(GEN6_RP_DOWN_EI, 0x55730);
4135 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 0xa);
4136 I915_WRITE(GEN6_PMINTRMSK, 0x6);
4137 I915_WRITE(GEN6_RP_CONTROL, GEN6_RP_MEDIA_TURBO |
4138 GEN6_RP_MEDIA_HW_MODE | GEN6_RP_MEDIA_IS_GFX |
4139 GEN6_RP_ENABLE | GEN6_RP_UP_BUSY_AVG |
4140 GEN6_RP_DOWN_IDLE_AVG);
4142 gen6_enable_rps_interrupts(dev);
4144 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
4147 static void gen9_enable_rc6(struct drm_device *dev)
4149 struct drm_i915_private *dev_priv = dev->dev_private;
4150 struct intel_engine_cs *ring;
4151 uint32_t rc6_mask = 0;
4154 /* 1a: Software RC state - RC0 */
4155 I915_WRITE(GEN6_RC_STATE, 0);
4157 /* 1b: Get forcewake during program sequence. Although the driver
4158 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
4159 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
4161 /* 2a: Disable RC states. */
4162 I915_WRITE(GEN6_RC_CONTROL, 0);
4164 /* 2b: Program RC6 thresholds.*/
4165 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 54 << 16);
4166 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
4167 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
4168 for_each_ring(ring, dev_priv, unused)
4169 I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
4170 I915_WRITE(GEN6_RC_SLEEP, 0);
4171 I915_WRITE(GEN6_RC6_THRESHOLD, 37500); /* 37.5/125ms per EI */
4173 /* 2c: Program Coarse Power Gating Policies. */
4174 I915_WRITE(GEN9_MEDIA_PG_IDLE_HYSTERESIS, 25);
4175 I915_WRITE(GEN9_RENDER_PG_IDLE_HYSTERESIS, 25);
4177 /* 3a: Enable RC6 */
4178 if (intel_enable_rc6(dev) & INTEL_RC6_ENABLE)
4179 rc6_mask = GEN6_RC_CTL_RC6_ENABLE;
4180 DRM_INFO("RC6 %s\n", (rc6_mask & GEN6_RC_CTL_RC6_ENABLE) ?
4182 I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
4183 GEN6_RC_CTL_EI_MODE(1) |
4186 /* 3b: Enable Coarse Power Gating only when RC6 is enabled */
4187 I915_WRITE(GEN9_PG_ENABLE, (rc6_mask & GEN6_RC_CTL_RC6_ENABLE) ? 3 : 0);
4189 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
4193 static void gen8_enable_rps(struct drm_device *dev)
4195 struct drm_i915_private *dev_priv = dev->dev_private;
4196 struct intel_engine_cs *ring;
4197 uint32_t rc6_mask = 0;
4200 /* 1a: Software RC state - RC0 */
4201 I915_WRITE(GEN6_RC_STATE, 0);
4203 /* 1c & 1d: Get forcewake during program sequence. Although the driver
4204 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
4205 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
4207 /* 2a: Disable RC states. */
4208 I915_WRITE(GEN6_RC_CONTROL, 0);
4210 /* Initialize rps frequencies */
4211 gen6_init_rps_frequencies(dev);
4213 /* 2b: Program RC6 thresholds.*/
4214 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16);
4215 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
4216 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
4217 for_each_ring(ring, dev_priv, unused)
4218 I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
4219 I915_WRITE(GEN6_RC_SLEEP, 0);
4220 if (IS_BROADWELL(dev))
4221 I915_WRITE(GEN6_RC6_THRESHOLD, 625); /* 800us/1.28 for TO */
4223 I915_WRITE(GEN6_RC6_THRESHOLD, 50000); /* 50/125ms per EI */
4226 if (intel_enable_rc6(dev) & INTEL_RC6_ENABLE)
4227 rc6_mask = GEN6_RC_CTL_RC6_ENABLE;
4228 intel_print_rc6_info(dev, rc6_mask);
4229 if (IS_BROADWELL(dev))
4230 I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
4231 GEN7_RC_CTL_TO_MODE |
4234 I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
4235 GEN6_RC_CTL_EI_MODE(1) |
4238 /* 4 Program defaults and thresholds for RPS*/
4239 I915_WRITE(GEN6_RPNSWREQ,
4240 HSW_FREQUENCY(dev_priv->rps.rp1_freq));
4241 I915_WRITE(GEN6_RC_VIDEO_FREQ,
4242 HSW_FREQUENCY(dev_priv->rps.rp1_freq));
4243 /* NB: Docs say 1s, and 1000000 - which aren't equivalent */
4244 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 100000000 / 128); /* 1 second timeout */
4246 /* Docs recommend 900MHz, and 300 MHz respectively */
4247 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS,
4248 dev_priv->rps.max_freq_softlimit << 24 |
4249 dev_priv->rps.min_freq_softlimit << 16);
4251 I915_WRITE(GEN6_RP_UP_THRESHOLD, 7600000 / 128); /* 76ms busyness per EI, 90% */
4252 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 31300000 / 128); /* 313ms busyness per EI, 70%*/
4253 I915_WRITE(GEN6_RP_UP_EI, 66000); /* 84.48ms, XXX: random? */
4254 I915_WRITE(GEN6_RP_DOWN_EI, 350000); /* 448ms, XXX: random? */
4256 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
4259 I915_WRITE(GEN6_RP_CONTROL,
4260 GEN6_RP_MEDIA_TURBO |
4261 GEN6_RP_MEDIA_HW_NORMAL_MODE |
4262 GEN6_RP_MEDIA_IS_GFX |
4264 GEN6_RP_UP_BUSY_AVG |
4265 GEN6_RP_DOWN_IDLE_AVG);
4267 /* 6: Ring frequency + overclocking (our driver does this later */
4269 dev_priv->rps.power = HIGH_POWER; /* force a reset */
4270 gen6_set_rps(dev_priv->dev, dev_priv->rps.min_freq_softlimit);
4272 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
4275 static void gen6_enable_rps(struct drm_device *dev)
4277 struct drm_i915_private *dev_priv = dev->dev_private;
4278 struct intel_engine_cs *ring;
4279 u32 rc6vids, pcu_mbox = 0, rc6_mask = 0;
4284 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
4286 /* Here begins a magic sequence of register writes to enable
4287 * auto-downclocking.
4289 * Perhaps there might be some value in exposing these to
4292 I915_WRITE(GEN6_RC_STATE, 0);
4294 /* Clear the DBG now so we don't confuse earlier errors */
4295 if ((gtfifodbg = I915_READ(GTFIFODBG))) {
4296 DRM_ERROR("GT fifo had a previous error %x\n", gtfifodbg);
4297 I915_WRITE(GTFIFODBG, gtfifodbg);
4300 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
4302 /* Initialize rps frequencies */
4303 gen6_init_rps_frequencies(dev);
4305 /* disable the counters and set deterministic thresholds */
4306 I915_WRITE(GEN6_RC_CONTROL, 0);
4308 I915_WRITE(GEN6_RC1_WAKE_RATE_LIMIT, 1000 << 16);
4309 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16 | 30);
4310 I915_WRITE(GEN6_RC6pp_WAKE_RATE_LIMIT, 30);
4311 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
4312 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
4314 for_each_ring(ring, dev_priv, i)
4315 I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
4317 I915_WRITE(GEN6_RC_SLEEP, 0);
4318 I915_WRITE(GEN6_RC1e_THRESHOLD, 1000);
4319 if (IS_IVYBRIDGE(dev))
4320 I915_WRITE(GEN6_RC6_THRESHOLD, 125000);
4322 I915_WRITE(GEN6_RC6_THRESHOLD, 50000);
4323 I915_WRITE(GEN6_RC6p_THRESHOLD, 150000);
4324 I915_WRITE(GEN6_RC6pp_THRESHOLD, 64000); /* unused */
4326 /* Check if we are enabling RC6 */
4327 rc6_mode = intel_enable_rc6(dev_priv->dev);
4328 if (rc6_mode & INTEL_RC6_ENABLE)
4329 rc6_mask |= GEN6_RC_CTL_RC6_ENABLE;
4331 /* We don't use those on Haswell */
4332 if (!IS_HASWELL(dev)) {
4333 if (rc6_mode & INTEL_RC6p_ENABLE)
4334 rc6_mask |= GEN6_RC_CTL_RC6p_ENABLE;
4336 if (rc6_mode & INTEL_RC6pp_ENABLE)
4337 rc6_mask |= GEN6_RC_CTL_RC6pp_ENABLE;
4340 intel_print_rc6_info(dev, rc6_mask);
4342 I915_WRITE(GEN6_RC_CONTROL,
4344 GEN6_RC_CTL_EI_MODE(1) |
4345 GEN6_RC_CTL_HW_ENABLE);
4347 /* Power down if completely idle for over 50ms */
4348 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 50000);
4349 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
4351 ret = sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_MIN_FREQ_TABLE, 0);
4353 DRM_DEBUG_DRIVER("Failed to set the min frequency\n");
4355 ret = sandybridge_pcode_read(dev_priv, GEN6_READ_OC_PARAMS, &pcu_mbox);
4356 if (!ret && (pcu_mbox & (1<<31))) { /* OC supported */
4357 DRM_DEBUG_DRIVER("Overclocking supported. Max: %dMHz, Overclock max: %dMHz\n",
4358 (dev_priv->rps.max_freq_softlimit & 0xff) * 50,
4359 (pcu_mbox & 0xff) * 50);
4360 dev_priv->rps.max_freq = pcu_mbox & 0xff;
4363 dev_priv->rps.power = HIGH_POWER; /* force a reset */
4364 gen6_set_rps(dev_priv->dev, dev_priv->rps.min_freq_softlimit);
4367 ret = sandybridge_pcode_read(dev_priv, GEN6_PCODE_READ_RC6VIDS, &rc6vids);
4368 if (IS_GEN6(dev) && ret) {
4369 DRM_DEBUG_DRIVER("Couldn't check for BIOS workaround\n");
4370 } else if (IS_GEN6(dev) && (GEN6_DECODE_RC6_VID(rc6vids & 0xff) < 450)) {
4371 DRM_DEBUG_DRIVER("You should update your BIOS. Correcting minimum rc6 voltage (%dmV->%dmV)\n",
4372 GEN6_DECODE_RC6_VID(rc6vids & 0xff), 450);
4373 rc6vids &= 0xffff00;
4374 rc6vids |= GEN6_ENCODE_RC6_VID(450);
4375 ret = sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_RC6VIDS, rc6vids);
4377 DRM_ERROR("Couldn't fix incorrect rc6 voltage\n");
4380 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
4383 static void __gen6_update_ring_freq(struct drm_device *dev)
4385 struct drm_i915_private *dev_priv = dev->dev_private;
4387 unsigned int gpu_freq;
4388 unsigned int max_ia_freq, min_ring_freq;
4389 int scaling_factor = 180;
4390 struct cpufreq_policy *policy;
4392 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
4394 policy = cpufreq_cpu_get(0);
4396 max_ia_freq = policy->cpuinfo.max_freq;
4397 cpufreq_cpu_put(policy);
4400 * Default to measured freq if none found, PCU will ensure we
4403 max_ia_freq = tsc_khz;
4406 /* Convert from kHz to MHz */
4407 max_ia_freq /= 1000;
4409 min_ring_freq = I915_READ(DCLK) & 0xf;
4410 /* convert DDR frequency from units of 266.6MHz to bandwidth */
4411 min_ring_freq = mult_frac(min_ring_freq, 8, 3);
4414 * For each potential GPU frequency, load a ring frequency we'd like
4415 * to use for memory access. We do this by specifying the IA frequency
4416 * the PCU should use as a reference to determine the ring frequency.
4418 for (gpu_freq = dev_priv->rps.max_freq; gpu_freq >= dev_priv->rps.min_freq;
4420 int diff = dev_priv->rps.max_freq - gpu_freq;
4421 unsigned int ia_freq = 0, ring_freq = 0;
4423 if (INTEL_INFO(dev)->gen >= 8) {
4424 /* max(2 * GT, DDR). NB: GT is 50MHz units */
4425 ring_freq = max(min_ring_freq, gpu_freq);
4426 } else if (IS_HASWELL(dev)) {
4427 ring_freq = mult_frac(gpu_freq, 5, 4);
4428 ring_freq = max(min_ring_freq, ring_freq);
4429 /* leave ia_freq as the default, chosen by cpufreq */
4431 /* On older processors, there is no separate ring
4432 * clock domain, so in order to boost the bandwidth
4433 * of the ring, we need to upclock the CPU (ia_freq).
4435 * For GPU frequencies less than 750MHz,
4436 * just use the lowest ring freq.
4438 if (gpu_freq < min_freq)
4441 ia_freq = max_ia_freq - ((diff * scaling_factor) / 2);
4442 ia_freq = DIV_ROUND_CLOSEST(ia_freq, 100);
4445 sandybridge_pcode_write(dev_priv,
4446 GEN6_PCODE_WRITE_MIN_FREQ_TABLE,
4447 ia_freq << GEN6_PCODE_FREQ_IA_RATIO_SHIFT |
4448 ring_freq << GEN6_PCODE_FREQ_RING_RATIO_SHIFT |
4453 void gen6_update_ring_freq(struct drm_device *dev)
4455 struct drm_i915_private *dev_priv = dev->dev_private;
4457 if (INTEL_INFO(dev)->gen < 6 || IS_VALLEYVIEW(dev))
4460 mutex_lock(&dev_priv->rps.hw_lock);
4461 __gen6_update_ring_freq(dev);
4462 mutex_unlock(&dev_priv->rps.hw_lock);
4465 static int cherryview_rps_max_freq(struct drm_i915_private *dev_priv)
4467 struct drm_device *dev = dev_priv->dev;
4470 if (dev->pdev->revision >= 0x20) {
4471 val = vlv_punit_read(dev_priv, FB_GFX_FMAX_AT_VMAX_FUSE);
4473 switch (INTEL_INFO(dev)->eu_total) {
4475 /* (2 * 4) config */
4476 rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS4EU_FUSE_SHIFT);
4479 /* (2 * 6) config */
4480 rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS6EU_FUSE_SHIFT);
4483 /* (2 * 8) config */
4485 /* Setting (2 * 8) Min RP0 for any other combination */
4486 rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS8EU_FUSE_SHIFT);
4489 rp0 = (rp0 & FB_GFX_FREQ_FUSE_MASK);
4491 /* For pre-production hardware */
4492 val = vlv_punit_read(dev_priv, PUNIT_GPU_STATUS_REG);
4493 rp0 = (val >> PUNIT_GPU_STATUS_MAX_FREQ_SHIFT) &
4494 PUNIT_GPU_STATUS_MAX_FREQ_MASK;
4499 static int cherryview_rps_rpe_freq(struct drm_i915_private *dev_priv)
4503 val = vlv_punit_read(dev_priv, PUNIT_GPU_DUTYCYCLE_REG);
4504 rpe = (val >> PUNIT_GPU_DUTYCYCLE_RPE_FREQ_SHIFT) & PUNIT_GPU_DUTYCYCLE_RPE_FREQ_MASK;
4509 static int cherryview_rps_guar_freq(struct drm_i915_private *dev_priv)
4511 struct drm_device *dev = dev_priv->dev;
4514 if (dev->pdev->revision >= 0x20) {
4515 val = vlv_punit_read(dev_priv, FB_GFX_FMAX_AT_VMAX_FUSE);
4516 rp1 = (val & FB_GFX_FREQ_FUSE_MASK);
4518 /* For pre-production hardware */
4519 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
4520 rp1 = ((val >> PUNIT_GPU_STATUS_MAX_FREQ_SHIFT) &
4521 PUNIT_GPU_STATUS_MAX_FREQ_MASK);
4526 static int cherryview_rps_min_freq(struct drm_i915_private *dev_priv)
4528 struct drm_device *dev = dev_priv->dev;
4531 if (dev->pdev->revision >= 0x20) {
4532 val = vlv_punit_read(dev_priv, FB_GFX_FMIN_AT_VMIN_FUSE);
4533 rpn = ((val >> FB_GFX_FMIN_AT_VMIN_FUSE_SHIFT) &
4534 FB_GFX_FREQ_FUSE_MASK);
4535 } else { /* For pre-production hardware */
4536 val = vlv_punit_read(dev_priv, PUNIT_GPU_STATUS_REG);
4537 rpn = ((val >> PUNIT_GPU_STATIS_GFX_MIN_FREQ_SHIFT) &
4538 PUNIT_GPU_STATUS_GFX_MIN_FREQ_MASK);
4544 static int valleyview_rps_guar_freq(struct drm_i915_private *dev_priv)
4548 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FREQ_FUSE);
4550 rp1 = (val & FB_GFX_FGUARANTEED_FREQ_FUSE_MASK) >> FB_GFX_FGUARANTEED_FREQ_FUSE_SHIFT;
4555 static int valleyview_rps_max_freq(struct drm_i915_private *dev_priv)
4559 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FREQ_FUSE);
4561 rp0 = (val & FB_GFX_MAX_FREQ_FUSE_MASK) >> FB_GFX_MAX_FREQ_FUSE_SHIFT;
4563 rp0 = min_t(u32, rp0, 0xea);
4568 static int valleyview_rps_rpe_freq(struct drm_i915_private *dev_priv)
4572 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FMAX_FUSE_LO);
4573 rpe = (val & FB_FMAX_VMIN_FREQ_LO_MASK) >> FB_FMAX_VMIN_FREQ_LO_SHIFT;
4574 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FMAX_FUSE_HI);
4575 rpe |= (val & FB_FMAX_VMIN_FREQ_HI_MASK) << 5;
4580 static int valleyview_rps_min_freq(struct drm_i915_private *dev_priv)
4582 return vlv_punit_read(dev_priv, PUNIT_REG_GPU_LFM) & 0xff;
4585 /* Check that the pctx buffer wasn't move under us. */
4586 static void valleyview_check_pctx(struct drm_i915_private *dev_priv)
4588 unsigned long pctx_addr = I915_READ(VLV_PCBR) & ~4095;
4590 WARN_ON(pctx_addr != dev_priv->mm.stolen_base +
4591 dev_priv->vlv_pctx->stolen->start);
4595 /* Check that the pcbr address is not empty. */
4596 static void cherryview_check_pctx(struct drm_i915_private *dev_priv)
4598 unsigned long pctx_addr = I915_READ(VLV_PCBR) & ~4095;
4600 WARN_ON((pctx_addr >> VLV_PCBR_ADDR_SHIFT) == 0);
4603 static void cherryview_setup_pctx(struct drm_device *dev)
4605 struct drm_i915_private *dev_priv = dev->dev_private;
4606 unsigned long pctx_paddr, paddr;
4607 struct i915_gtt *gtt = &dev_priv->gtt;
4609 int pctx_size = 32*1024;
4611 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
4613 pcbr = I915_READ(VLV_PCBR);
4614 if ((pcbr >> VLV_PCBR_ADDR_SHIFT) == 0) {
4615 DRM_DEBUG_DRIVER("BIOS didn't set up PCBR, fixing up\n");
4616 paddr = (dev_priv->mm.stolen_base +
4617 (gtt->stolen_size - pctx_size));
4619 pctx_paddr = (paddr & (~4095));
4620 I915_WRITE(VLV_PCBR, pctx_paddr);
4623 DRM_DEBUG_DRIVER("PCBR: 0x%08x\n", I915_READ(VLV_PCBR));
4626 static void valleyview_setup_pctx(struct drm_device *dev)
4628 struct drm_i915_private *dev_priv = dev->dev_private;
4629 struct drm_i915_gem_object *pctx;
4630 unsigned long pctx_paddr;
4632 int pctx_size = 24*1024;
4634 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
4636 pcbr = I915_READ(VLV_PCBR);
4638 /* BIOS set it up already, grab the pre-alloc'd space */
4641 pcbr_offset = (pcbr & (~4095)) - dev_priv->mm.stolen_base;
4642 pctx = i915_gem_object_create_stolen_for_preallocated(dev_priv->dev,
4644 I915_GTT_OFFSET_NONE,
4649 DRM_DEBUG_DRIVER("BIOS didn't set up PCBR, fixing up\n");
4652 * From the Gunit register HAS:
4653 * The Gfx driver is expected to program this register and ensure
4654 * proper allocation within Gfx stolen memory. For example, this
4655 * register should be programmed such than the PCBR range does not
4656 * overlap with other ranges, such as the frame buffer, protected
4657 * memory, or any other relevant ranges.
4659 pctx = i915_gem_object_create_stolen(dev, pctx_size);
4661 DRM_DEBUG("not enough stolen space for PCTX, disabling\n");
4665 pctx_paddr = dev_priv->mm.stolen_base + pctx->stolen->start;
4666 I915_WRITE(VLV_PCBR, pctx_paddr);
4669 DRM_DEBUG_DRIVER("PCBR: 0x%08x\n", I915_READ(VLV_PCBR));
4670 dev_priv->vlv_pctx = pctx;
4673 static void valleyview_cleanup_pctx(struct drm_device *dev)
4675 struct drm_i915_private *dev_priv = dev->dev_private;
4677 if (WARN_ON(!dev_priv->vlv_pctx))
4680 drm_gem_object_unreference(&dev_priv->vlv_pctx->base);
4681 dev_priv->vlv_pctx = NULL;
4684 static void valleyview_init_gt_powersave(struct drm_device *dev)
4686 struct drm_i915_private *dev_priv = dev->dev_private;
4689 valleyview_setup_pctx(dev);
4691 mutex_lock(&dev_priv->rps.hw_lock);
4693 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
4694 switch ((val >> 6) & 3) {
4697 dev_priv->mem_freq = 800;
4700 dev_priv->mem_freq = 1066;
4703 dev_priv->mem_freq = 1333;
4706 DRM_DEBUG_DRIVER("DDR speed: %d MHz\n", dev_priv->mem_freq);
4708 dev_priv->rps.max_freq = valleyview_rps_max_freq(dev_priv);
4709 dev_priv->rps.rp0_freq = dev_priv->rps.max_freq;
4710 DRM_DEBUG_DRIVER("max GPU freq: %d MHz (%u)\n",
4711 intel_gpu_freq(dev_priv, dev_priv->rps.max_freq),
4712 dev_priv->rps.max_freq);
4714 dev_priv->rps.efficient_freq = valleyview_rps_rpe_freq(dev_priv);
4715 DRM_DEBUG_DRIVER("RPe GPU freq: %d MHz (%u)\n",
4716 intel_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
4717 dev_priv->rps.efficient_freq);
4719 dev_priv->rps.rp1_freq = valleyview_rps_guar_freq(dev_priv);
4720 DRM_DEBUG_DRIVER("RP1(Guar Freq) GPU freq: %d MHz (%u)\n",
4721 intel_gpu_freq(dev_priv, dev_priv->rps.rp1_freq),
4722 dev_priv->rps.rp1_freq);
4724 dev_priv->rps.min_freq = valleyview_rps_min_freq(dev_priv);
4725 DRM_DEBUG_DRIVER("min GPU freq: %d MHz (%u)\n",
4726 intel_gpu_freq(dev_priv, dev_priv->rps.min_freq),
4727 dev_priv->rps.min_freq);
4729 /* Preserve min/max settings in case of re-init */
4730 if (dev_priv->rps.max_freq_softlimit == 0)
4731 dev_priv->rps.max_freq_softlimit = dev_priv->rps.max_freq;
4733 if (dev_priv->rps.min_freq_softlimit == 0)
4734 dev_priv->rps.min_freq_softlimit = dev_priv->rps.min_freq;
4736 mutex_unlock(&dev_priv->rps.hw_lock);
4739 static void cherryview_init_gt_powersave(struct drm_device *dev)
4741 struct drm_i915_private *dev_priv = dev->dev_private;
4744 cherryview_setup_pctx(dev);
4746 mutex_lock(&dev_priv->rps.hw_lock);
4748 mutex_lock(&dev_priv->dpio_lock);
4749 val = vlv_cck_read(dev_priv, CCK_FUSE_REG);
4750 mutex_unlock(&dev_priv->dpio_lock);
4752 switch ((val >> 2) & 0x7) {
4755 dev_priv->rps.cz_freq = 200;
4756 dev_priv->mem_freq = 1600;
4759 dev_priv->rps.cz_freq = 267;
4760 dev_priv->mem_freq = 1600;
4763 dev_priv->rps.cz_freq = 333;
4764 dev_priv->mem_freq = 2000;
4767 dev_priv->rps.cz_freq = 320;
4768 dev_priv->mem_freq = 1600;
4771 dev_priv->rps.cz_freq = 400;
4772 dev_priv->mem_freq = 1600;
4775 DRM_DEBUG_DRIVER("DDR speed: %d MHz\n", dev_priv->mem_freq);
4777 dev_priv->rps.max_freq = cherryview_rps_max_freq(dev_priv);
4778 dev_priv->rps.rp0_freq = dev_priv->rps.max_freq;
4779 DRM_DEBUG_DRIVER("max GPU freq: %d MHz (%u)\n",
4780 intel_gpu_freq(dev_priv, dev_priv->rps.max_freq),
4781 dev_priv->rps.max_freq);
4783 dev_priv->rps.efficient_freq = cherryview_rps_rpe_freq(dev_priv);
4784 DRM_DEBUG_DRIVER("RPe GPU freq: %d MHz (%u)\n",
4785 intel_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
4786 dev_priv->rps.efficient_freq);
4788 dev_priv->rps.rp1_freq = cherryview_rps_guar_freq(dev_priv);
4789 DRM_DEBUG_DRIVER("RP1(Guar) GPU freq: %d MHz (%u)\n",
4790 intel_gpu_freq(dev_priv, dev_priv->rps.rp1_freq),
4791 dev_priv->rps.rp1_freq);
4793 dev_priv->rps.min_freq = cherryview_rps_min_freq(dev_priv);
4794 DRM_DEBUG_DRIVER("min GPU freq: %d MHz (%u)\n",
4795 intel_gpu_freq(dev_priv, dev_priv->rps.min_freq),
4796 dev_priv->rps.min_freq);
4798 WARN_ONCE((dev_priv->rps.max_freq |
4799 dev_priv->rps.efficient_freq |
4800 dev_priv->rps.rp1_freq |
4801 dev_priv->rps.min_freq) & 1,
4802 "Odd GPU freq values\n");
4804 /* Preserve min/max settings in case of re-init */
4805 if (dev_priv->rps.max_freq_softlimit == 0)
4806 dev_priv->rps.max_freq_softlimit = dev_priv->rps.max_freq;
4808 if (dev_priv->rps.min_freq_softlimit == 0)
4809 dev_priv->rps.min_freq_softlimit = dev_priv->rps.min_freq;
4811 mutex_unlock(&dev_priv->rps.hw_lock);
4814 static void valleyview_cleanup_gt_powersave(struct drm_device *dev)
4816 valleyview_cleanup_pctx(dev);
4819 static void cherryview_enable_rps(struct drm_device *dev)
4821 struct drm_i915_private *dev_priv = dev->dev_private;
4822 struct intel_engine_cs *ring;
4823 u32 gtfifodbg, val, rc6_mode = 0, pcbr;
4826 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
4828 gtfifodbg = I915_READ(GTFIFODBG);
4830 DRM_DEBUG_DRIVER("GT fifo had a previous error %x\n",
4832 I915_WRITE(GTFIFODBG, gtfifodbg);
4835 cherryview_check_pctx(dev_priv);
4837 /* 1a & 1b: Get forcewake during program sequence. Although the driver
4838 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
4839 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
4841 /* Disable RC states. */
4842 I915_WRITE(GEN6_RC_CONTROL, 0);
4844 /* 2a: Program RC6 thresholds.*/
4845 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16);
4846 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
4847 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
4849 for_each_ring(ring, dev_priv, i)
4850 I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
4851 I915_WRITE(GEN6_RC_SLEEP, 0);
4853 /* TO threshold set to 1750 us ( 0x557 * 1.28 us) */
4854 I915_WRITE(GEN6_RC6_THRESHOLD, 0x557);
4856 /* allows RC6 residency counter to work */
4857 I915_WRITE(VLV_COUNTER_CONTROL,
4858 _MASKED_BIT_ENABLE(VLV_COUNT_RANGE_HIGH |
4859 VLV_MEDIA_RC6_COUNT_EN |
4860 VLV_RENDER_RC6_COUNT_EN));
4862 /* For now we assume BIOS is allocating and populating the PCBR */
4863 pcbr = I915_READ(VLV_PCBR);
4866 if ((intel_enable_rc6(dev) & INTEL_RC6_ENABLE) &&
4867 (pcbr >> VLV_PCBR_ADDR_SHIFT))
4868 rc6_mode = GEN7_RC_CTL_TO_MODE;
4870 I915_WRITE(GEN6_RC_CONTROL, rc6_mode);
4872 /* 4 Program defaults and thresholds for RPS*/
4873 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 1000000);
4874 I915_WRITE(GEN6_RP_UP_THRESHOLD, 59400);
4875 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 245000);
4876 I915_WRITE(GEN6_RP_UP_EI, 66000);
4877 I915_WRITE(GEN6_RP_DOWN_EI, 350000);
4879 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
4882 I915_WRITE(GEN6_RP_CONTROL,
4883 GEN6_RP_MEDIA_HW_NORMAL_MODE |
4884 GEN6_RP_MEDIA_IS_GFX |
4886 GEN6_RP_UP_BUSY_AVG |
4887 GEN6_RP_DOWN_IDLE_AVG);
4889 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
4891 /* RPS code assumes GPLL is used */
4892 WARN_ONCE((val & GPLLENABLE) == 0, "GPLL not enabled\n");
4894 DRM_DEBUG_DRIVER("GPLL enabled? %s\n", val & GPLLENABLE ? "yes" : "no");
4895 DRM_DEBUG_DRIVER("GPU status: 0x%08x\n", val);
4897 dev_priv->rps.cur_freq = (val >> 8) & 0xff;
4898 DRM_DEBUG_DRIVER("current GPU freq: %d MHz (%u)\n",
4899 intel_gpu_freq(dev_priv, dev_priv->rps.cur_freq),
4900 dev_priv->rps.cur_freq);
4902 DRM_DEBUG_DRIVER("setting GPU freq to %d MHz (%u)\n",
4903 intel_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
4904 dev_priv->rps.efficient_freq);
4906 valleyview_set_rps(dev_priv->dev, dev_priv->rps.efficient_freq);
4908 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
4911 static void valleyview_enable_rps(struct drm_device *dev)
4913 struct drm_i915_private *dev_priv = dev->dev_private;
4914 struct intel_engine_cs *ring;
4915 u32 gtfifodbg, val, rc6_mode = 0;
4918 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
4920 valleyview_check_pctx(dev_priv);
4922 if ((gtfifodbg = I915_READ(GTFIFODBG))) {
4923 DRM_DEBUG_DRIVER("GT fifo had a previous error %x\n",
4925 I915_WRITE(GTFIFODBG, gtfifodbg);
4928 /* If VLV, Forcewake all wells, else re-direct to regular path */
4929 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
4931 /* Disable RC states. */
4932 I915_WRITE(GEN6_RC_CONTROL, 0);
4934 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 1000000);
4935 I915_WRITE(GEN6_RP_UP_THRESHOLD, 59400);
4936 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 245000);
4937 I915_WRITE(GEN6_RP_UP_EI, 66000);
4938 I915_WRITE(GEN6_RP_DOWN_EI, 350000);
4940 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
4942 I915_WRITE(GEN6_RP_CONTROL,
4943 GEN6_RP_MEDIA_TURBO |
4944 GEN6_RP_MEDIA_HW_NORMAL_MODE |
4945 GEN6_RP_MEDIA_IS_GFX |
4947 GEN6_RP_UP_BUSY_AVG |
4948 GEN6_RP_DOWN_IDLE_CONT);
4950 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 0x00280000);
4951 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
4952 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
4954 for_each_ring(ring, dev_priv, i)
4955 I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
4957 I915_WRITE(GEN6_RC6_THRESHOLD, 0x557);
4959 /* allows RC6 residency counter to work */
4960 I915_WRITE(VLV_COUNTER_CONTROL,
4961 _MASKED_BIT_ENABLE(VLV_MEDIA_RC0_COUNT_EN |
4962 VLV_RENDER_RC0_COUNT_EN |
4963 VLV_MEDIA_RC6_COUNT_EN |
4964 VLV_RENDER_RC6_COUNT_EN));
4966 if (intel_enable_rc6(dev) & INTEL_RC6_ENABLE)
4967 rc6_mode = GEN7_RC_CTL_TO_MODE | VLV_RC_CTL_CTX_RST_PARALLEL;
4969 intel_print_rc6_info(dev, rc6_mode);
4971 I915_WRITE(GEN6_RC_CONTROL, rc6_mode);
4973 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
4975 /* RPS code assumes GPLL is used */
4976 WARN_ONCE((val & GPLLENABLE) == 0, "GPLL not enabled\n");
4978 DRM_DEBUG_DRIVER("GPLL enabled? %s\n", val & GPLLENABLE ? "yes" : "no");
4979 DRM_DEBUG_DRIVER("GPU status: 0x%08x\n", val);
4981 dev_priv->rps.cur_freq = (val >> 8) & 0xff;
4982 DRM_DEBUG_DRIVER("current GPU freq: %d MHz (%u)\n",
4983 intel_gpu_freq(dev_priv, dev_priv->rps.cur_freq),
4984 dev_priv->rps.cur_freq);
4986 DRM_DEBUG_DRIVER("setting GPU freq to %d MHz (%u)\n",
4987 intel_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
4988 dev_priv->rps.efficient_freq);
4990 valleyview_set_rps(dev_priv->dev, dev_priv->rps.efficient_freq);
4992 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
4995 static unsigned long intel_pxfreq(u32 vidfreq)
4998 int div = (vidfreq & 0x3f0000) >> 16;
4999 int post = (vidfreq & 0x3000) >> 12;
5000 int pre = (vidfreq & 0x7);
5005 freq = ((div * 133333) / ((1<<post) * pre));
5010 static const struct cparams {
5016 { 1, 1333, 301, 28664 },
5017 { 1, 1066, 294, 24460 },
5018 { 1, 800, 294, 25192 },
5019 { 0, 1333, 276, 27605 },
5020 { 0, 1066, 276, 27605 },
5021 { 0, 800, 231, 23784 },
5024 static unsigned long __i915_chipset_val(struct drm_i915_private *dev_priv)
5026 u64 total_count, diff, ret;
5027 u32 count1, count2, count3, m = 0, c = 0;
5028 unsigned long now = jiffies_to_msecs(jiffies), diff1;
5031 assert_spin_locked(&mchdev_lock);
5033 diff1 = now - dev_priv->ips.last_time1;
5035 /* Prevent division-by-zero if we are asking too fast.
5036 * Also, we don't get interesting results if we are polling
5037 * faster than once in 10ms, so just return the saved value
5041 return dev_priv->ips.chipset_power;
5043 count1 = I915_READ(DMIEC);
5044 count2 = I915_READ(DDREC);
5045 count3 = I915_READ(CSIEC);
5047 total_count = count1 + count2 + count3;
5049 /* FIXME: handle per-counter overflow */
5050 if (total_count < dev_priv->ips.last_count1) {
5051 diff = ~0UL - dev_priv->ips.last_count1;
5052 diff += total_count;
5054 diff = total_count - dev_priv->ips.last_count1;
5057 for (i = 0; i < ARRAY_SIZE(cparams); i++) {
5058 if (cparams[i].i == dev_priv->ips.c_m &&
5059 cparams[i].t == dev_priv->ips.r_t) {
5066 diff = div_u64(diff, diff1);
5067 ret = ((m * diff) + c);
5068 ret = div_u64(ret, 10);
5070 dev_priv->ips.last_count1 = total_count;
5071 dev_priv->ips.last_time1 = now;
5073 dev_priv->ips.chipset_power = ret;
5078 unsigned long i915_chipset_val(struct drm_i915_private *dev_priv)
5080 struct drm_device *dev = dev_priv->dev;
5083 if (INTEL_INFO(dev)->gen != 5)
5086 spin_lock_irq(&mchdev_lock);
5088 val = __i915_chipset_val(dev_priv);
5090 spin_unlock_irq(&mchdev_lock);
5095 unsigned long i915_mch_val(struct drm_i915_private *dev_priv)
5097 unsigned long m, x, b;
5100 tsfs = I915_READ(TSFS);
5102 m = ((tsfs & TSFS_SLOPE_MASK) >> TSFS_SLOPE_SHIFT);
5103 x = I915_READ8(TR1);
5105 b = tsfs & TSFS_INTR_MASK;
5107 return ((m * x) / 127) - b;
5110 static int _pxvid_to_vd(u8 pxvid)
5115 if (pxvid >= 8 && pxvid < 31)
5118 return (pxvid + 2) * 125;
5121 static u32 pvid_to_extvid(struct drm_i915_private *dev_priv, u8 pxvid)
5123 struct drm_device *dev = dev_priv->dev;
5124 const int vd = _pxvid_to_vd(pxvid);
5125 const int vm = vd - 1125;
5127 if (INTEL_INFO(dev)->is_mobile)
5128 return vm > 0 ? vm : 0;
5133 static void __i915_update_gfx_val(struct drm_i915_private *dev_priv)
5135 u64 now, diff, diffms;
5138 assert_spin_locked(&mchdev_lock);
5140 now = ktime_get_raw_ns();
5141 diffms = now - dev_priv->ips.last_time2;
5142 do_div(diffms, NSEC_PER_MSEC);
5144 /* Don't divide by 0 */
5148 count = I915_READ(GFXEC);
5150 if (count < dev_priv->ips.last_count2) {
5151 diff = ~0UL - dev_priv->ips.last_count2;
5154 diff = count - dev_priv->ips.last_count2;
5157 dev_priv->ips.last_count2 = count;
5158 dev_priv->ips.last_time2 = now;
5160 /* More magic constants... */
5162 diff = div_u64(diff, diffms * 10);
5163 dev_priv->ips.gfx_power = diff;
5166 void i915_update_gfx_val(struct drm_i915_private *dev_priv)
5168 struct drm_device *dev = dev_priv->dev;
5170 if (INTEL_INFO(dev)->gen != 5)
5173 spin_lock_irq(&mchdev_lock);
5175 __i915_update_gfx_val(dev_priv);
5177 spin_unlock_irq(&mchdev_lock);
5180 static unsigned long __i915_gfx_val(struct drm_i915_private *dev_priv)
5182 unsigned long t, corr, state1, corr2, state2;
5185 assert_spin_locked(&mchdev_lock);
5187 pxvid = I915_READ(PXVFREQ_BASE + (dev_priv->rps.cur_freq * 4));
5188 pxvid = (pxvid >> 24) & 0x7f;
5189 ext_v = pvid_to_extvid(dev_priv, pxvid);
5193 t = i915_mch_val(dev_priv);
5195 /* Revel in the empirically derived constants */
5197 /* Correction factor in 1/100000 units */
5199 corr = ((t * 2349) + 135940);
5201 corr = ((t * 964) + 29317);
5203 corr = ((t * 301) + 1004);
5205 corr = corr * ((150142 * state1) / 10000 - 78642);
5207 corr2 = (corr * dev_priv->ips.corr);
5209 state2 = (corr2 * state1) / 10000;
5210 state2 /= 100; /* convert to mW */
5212 __i915_update_gfx_val(dev_priv);
5214 return dev_priv->ips.gfx_power + state2;
5217 unsigned long i915_gfx_val(struct drm_i915_private *dev_priv)
5219 struct drm_device *dev = dev_priv->dev;
5222 if (INTEL_INFO(dev)->gen != 5)
5225 spin_lock_irq(&mchdev_lock);
5227 val = __i915_gfx_val(dev_priv);
5229 spin_unlock_irq(&mchdev_lock);
5235 * i915_read_mch_val - return value for IPS use
5237 * Calculate and return a value for the IPS driver to use when deciding whether
5238 * we have thermal and power headroom to increase CPU or GPU power budget.
5240 unsigned long i915_read_mch_val(void)
5242 struct drm_i915_private *dev_priv;
5243 unsigned long chipset_val, graphics_val, ret = 0;
5245 spin_lock_irq(&mchdev_lock);
5248 dev_priv = i915_mch_dev;
5250 chipset_val = __i915_chipset_val(dev_priv);
5251 graphics_val = __i915_gfx_val(dev_priv);
5253 ret = chipset_val + graphics_val;
5256 spin_unlock_irq(&mchdev_lock);
5260 EXPORT_SYMBOL_GPL(i915_read_mch_val);
5263 * i915_gpu_raise - raise GPU frequency limit
5265 * Raise the limit; IPS indicates we have thermal headroom.
5267 bool i915_gpu_raise(void)
5269 struct drm_i915_private *dev_priv;
5272 spin_lock_irq(&mchdev_lock);
5273 if (!i915_mch_dev) {
5277 dev_priv = i915_mch_dev;
5279 if (dev_priv->ips.max_delay > dev_priv->ips.fmax)
5280 dev_priv->ips.max_delay--;
5283 spin_unlock_irq(&mchdev_lock);
5287 EXPORT_SYMBOL_GPL(i915_gpu_raise);
5290 * i915_gpu_lower - lower GPU frequency limit
5292 * IPS indicates we're close to a thermal limit, so throttle back the GPU
5293 * frequency maximum.
5295 bool i915_gpu_lower(void)
5297 struct drm_i915_private *dev_priv;
5300 spin_lock_irq(&mchdev_lock);
5301 if (!i915_mch_dev) {
5305 dev_priv = i915_mch_dev;
5307 if (dev_priv->ips.max_delay < dev_priv->ips.min_delay)
5308 dev_priv->ips.max_delay++;
5311 spin_unlock_irq(&mchdev_lock);
5315 EXPORT_SYMBOL_GPL(i915_gpu_lower);
5318 * i915_gpu_busy - indicate GPU business to IPS
5320 * Tell the IPS driver whether or not the GPU is busy.
5322 bool i915_gpu_busy(void)
5324 struct drm_i915_private *dev_priv;
5325 struct intel_engine_cs *ring;
5329 spin_lock_irq(&mchdev_lock);
5332 dev_priv = i915_mch_dev;
5334 for_each_ring(ring, dev_priv, i)
5335 ret |= !list_empty(&ring->request_list);
5338 spin_unlock_irq(&mchdev_lock);
5342 EXPORT_SYMBOL_GPL(i915_gpu_busy);
5345 * i915_gpu_turbo_disable - disable graphics turbo
5347 * Disable graphics turbo by resetting the max frequency and setting the
5348 * current frequency to the default.
5350 bool i915_gpu_turbo_disable(void)
5352 struct drm_i915_private *dev_priv;
5355 spin_lock_irq(&mchdev_lock);
5356 if (!i915_mch_dev) {
5360 dev_priv = i915_mch_dev;
5362 dev_priv->ips.max_delay = dev_priv->ips.fstart;
5364 if (!ironlake_set_drps(dev_priv->dev, dev_priv->ips.fstart))
5368 spin_unlock_irq(&mchdev_lock);
5372 EXPORT_SYMBOL_GPL(i915_gpu_turbo_disable);
5375 * Tells the intel_ips driver that the i915 driver is now loaded, if
5376 * IPS got loaded first.
5378 * This awkward dance is so that neither module has to depend on the
5379 * other in order for IPS to do the appropriate communication of
5380 * GPU turbo limits to i915.
5383 ips_ping_for_i915_load(void)
5387 link = symbol_get(ips_link_to_i915_driver);
5390 symbol_put(ips_link_to_i915_driver);
5394 void intel_gpu_ips_init(struct drm_i915_private *dev_priv)
5396 /* We only register the i915 ips part with intel-ips once everything is
5397 * set up, to avoid intel-ips sneaking in and reading bogus values. */
5398 spin_lock_irq(&mchdev_lock);
5399 i915_mch_dev = dev_priv;
5400 spin_unlock_irq(&mchdev_lock);
5402 ips_ping_for_i915_load();
5405 void intel_gpu_ips_teardown(void)
5407 spin_lock_irq(&mchdev_lock);
5408 i915_mch_dev = NULL;
5409 spin_unlock_irq(&mchdev_lock);
5412 static void intel_init_emon(struct drm_device *dev)
5414 struct drm_i915_private *dev_priv = dev->dev_private;
5419 /* Disable to program */
5423 /* Program energy weights for various events */
5424 I915_WRITE(SDEW, 0x15040d00);
5425 I915_WRITE(CSIEW0, 0x007f0000);
5426 I915_WRITE(CSIEW1, 0x1e220004);
5427 I915_WRITE(CSIEW2, 0x04000004);
5429 for (i = 0; i < 5; i++)
5430 I915_WRITE(PEW + (i * 4), 0);
5431 for (i = 0; i < 3; i++)
5432 I915_WRITE(DEW + (i * 4), 0);
5434 /* Program P-state weights to account for frequency power adjustment */
5435 for (i = 0; i < 16; i++) {
5436 u32 pxvidfreq = I915_READ(PXVFREQ_BASE + (i * 4));
5437 unsigned long freq = intel_pxfreq(pxvidfreq);
5438 unsigned long vid = (pxvidfreq & PXVFREQ_PX_MASK) >>
5443 val *= (freq / 1000);
5445 val /= (127*127*900);
5447 DRM_ERROR("bad pxval: %ld\n", val);
5450 /* Render standby states get 0 weight */
5454 for (i = 0; i < 4; i++) {
5455 u32 val = (pxw[i*4] << 24) | (pxw[(i*4)+1] << 16) |
5456 (pxw[(i*4)+2] << 8) | (pxw[(i*4)+3]);
5457 I915_WRITE(PXW + (i * 4), val);
5460 /* Adjust magic regs to magic values (more experimental results) */
5461 I915_WRITE(OGW0, 0);
5462 I915_WRITE(OGW1, 0);
5463 I915_WRITE(EG0, 0x00007f00);
5464 I915_WRITE(EG1, 0x0000000e);
5465 I915_WRITE(EG2, 0x000e0000);
5466 I915_WRITE(EG3, 0x68000300);
5467 I915_WRITE(EG4, 0x42000000);
5468 I915_WRITE(EG5, 0x00140031);
5472 for (i = 0; i < 8; i++)
5473 I915_WRITE(PXWL + (i * 4), 0);
5475 /* Enable PMON + select events */
5476 I915_WRITE(ECR, 0x80000019);
5478 lcfuse = I915_READ(LCFUSE02);
5480 dev_priv->ips.corr = (lcfuse & LCFUSE_HIV_MASK);
5483 void intel_init_gt_powersave(struct drm_device *dev)
5485 i915.enable_rc6 = sanitize_rc6_option(dev, i915.enable_rc6);
5487 if (IS_CHERRYVIEW(dev))
5488 cherryview_init_gt_powersave(dev);
5489 else if (IS_VALLEYVIEW(dev))
5490 valleyview_init_gt_powersave(dev);
5493 void intel_cleanup_gt_powersave(struct drm_device *dev)
5495 if (IS_CHERRYVIEW(dev))
5497 else if (IS_VALLEYVIEW(dev))
5498 valleyview_cleanup_gt_powersave(dev);
5501 static void gen6_suspend_rps(struct drm_device *dev)
5503 struct drm_i915_private *dev_priv = dev->dev_private;
5505 flush_delayed_work(&dev_priv->rps.delayed_resume_work);
5508 * TODO: disable RPS interrupts on GEN9+ too once RPS support
5511 if (INTEL_INFO(dev)->gen < 9)
5512 gen6_disable_rps_interrupts(dev);
5516 * intel_suspend_gt_powersave - suspend PM work and helper threads
5519 * We don't want to disable RC6 or other features here, we just want
5520 * to make sure any work we've queued has finished and won't bother
5521 * us while we're suspended.
5523 void intel_suspend_gt_powersave(struct drm_device *dev)
5525 struct drm_i915_private *dev_priv = dev->dev_private;
5527 if (INTEL_INFO(dev)->gen < 6)
5530 gen6_suspend_rps(dev);
5532 /* Force GPU to min freq during suspend */
5533 gen6_rps_idle(dev_priv);
5536 void intel_disable_gt_powersave(struct drm_device *dev)
5538 struct drm_i915_private *dev_priv = dev->dev_private;
5540 if (IS_IRONLAKE_M(dev)) {
5541 ironlake_disable_drps(dev);
5542 } else if (INTEL_INFO(dev)->gen >= 6) {
5543 intel_suspend_gt_powersave(dev);
5545 mutex_lock(&dev_priv->rps.hw_lock);
5546 if (INTEL_INFO(dev)->gen >= 9)
5547 gen9_disable_rps(dev);
5548 else if (IS_CHERRYVIEW(dev))
5549 cherryview_disable_rps(dev);
5550 else if (IS_VALLEYVIEW(dev))
5551 valleyview_disable_rps(dev);
5553 gen6_disable_rps(dev);
5555 dev_priv->rps.enabled = false;
5556 mutex_unlock(&dev_priv->rps.hw_lock);
5560 static void intel_gen6_powersave_work(struct work_struct *work)
5562 struct drm_i915_private *dev_priv =
5563 container_of(work, struct drm_i915_private,
5564 rps.delayed_resume_work.work);
5565 struct drm_device *dev = dev_priv->dev;
5567 mutex_lock(&dev_priv->rps.hw_lock);
5570 * TODO: reset/enable RPS interrupts on GEN9+ too, once RPS support is
5573 if (INTEL_INFO(dev)->gen < 9)
5574 gen6_reset_rps_interrupts(dev);
5576 if (IS_CHERRYVIEW(dev)) {
5577 cherryview_enable_rps(dev);
5578 } else if (IS_VALLEYVIEW(dev)) {
5579 valleyview_enable_rps(dev);
5580 } else if (INTEL_INFO(dev)->gen >= 9) {
5581 gen9_enable_rc6(dev);
5582 gen9_enable_rps(dev);
5583 __gen6_update_ring_freq(dev);
5584 } else if (IS_BROADWELL(dev)) {
5585 gen8_enable_rps(dev);
5586 __gen6_update_ring_freq(dev);
5588 gen6_enable_rps(dev);
5589 __gen6_update_ring_freq(dev);
5591 dev_priv->rps.enabled = true;
5593 if (INTEL_INFO(dev)->gen < 9)
5594 gen6_enable_rps_interrupts(dev);
5596 mutex_unlock(&dev_priv->rps.hw_lock);
5598 intel_runtime_pm_put(dev_priv);
5601 void intel_enable_gt_powersave(struct drm_device *dev)
5603 struct drm_i915_private *dev_priv = dev->dev_private;
5605 /* Powersaving is controlled by the host when inside a VM */
5606 if (intel_vgpu_active(dev))
5609 if (IS_IRONLAKE_M(dev)) {
5610 mutex_lock(&dev->struct_mutex);
5611 ironlake_enable_drps(dev);
5612 intel_init_emon(dev);
5613 mutex_unlock(&dev->struct_mutex);
5614 } else if (INTEL_INFO(dev)->gen >= 6) {
5616 * PCU communication is slow and this doesn't need to be
5617 * done at any specific time, so do this out of our fast path
5618 * to make resume and init faster.
5620 * We depend on the HW RC6 power context save/restore
5621 * mechanism when entering D3 through runtime PM suspend. So
5622 * disable RPM until RPS/RC6 is properly setup. We can only
5623 * get here via the driver load/system resume/runtime resume
5624 * paths, so the _noresume version is enough (and in case of
5625 * runtime resume it's necessary).
5627 if (schedule_delayed_work(&dev_priv->rps.delayed_resume_work,
5628 round_jiffies_up_relative(HZ)))
5629 intel_runtime_pm_get_noresume(dev_priv);
5633 void intel_reset_gt_powersave(struct drm_device *dev)
5635 struct drm_i915_private *dev_priv = dev->dev_private;
5637 if (INTEL_INFO(dev)->gen < 6)
5640 gen6_suspend_rps(dev);
5641 dev_priv->rps.enabled = false;
5644 static void ibx_init_clock_gating(struct drm_device *dev)
5646 struct drm_i915_private *dev_priv = dev->dev_private;
5649 * On Ibex Peak and Cougar Point, we need to disable clock
5650 * gating for the panel power sequencer or it will fail to
5651 * start up when no ports are active.
5653 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE);
5656 static void g4x_disable_trickle_feed(struct drm_device *dev)
5658 struct drm_i915_private *dev_priv = dev->dev_private;
5661 for_each_pipe(dev_priv, pipe) {
5662 I915_WRITE(DSPCNTR(pipe),
5663 I915_READ(DSPCNTR(pipe)) |
5664 DISPPLANE_TRICKLE_FEED_DISABLE);
5665 intel_flush_primary_plane(dev_priv, pipe);
5669 static void ilk_init_lp_watermarks(struct drm_device *dev)
5671 struct drm_i915_private *dev_priv = dev->dev_private;
5673 I915_WRITE(WM3_LP_ILK, I915_READ(WM3_LP_ILK) & ~WM1_LP_SR_EN);
5674 I915_WRITE(WM2_LP_ILK, I915_READ(WM2_LP_ILK) & ~WM1_LP_SR_EN);
5675 I915_WRITE(WM1_LP_ILK, I915_READ(WM1_LP_ILK) & ~WM1_LP_SR_EN);
5678 * Don't touch WM1S_LP_EN here.
5679 * Doing so could cause underruns.
5683 static void ironlake_init_clock_gating(struct drm_device *dev)
5685 struct drm_i915_private *dev_priv = dev->dev_private;
5686 uint32_t dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
5690 * WaFbcDisableDpfcClockGating:ilk
5692 dspclk_gate |= ILK_DPFCRUNIT_CLOCK_GATE_DISABLE |
5693 ILK_DPFCUNIT_CLOCK_GATE_DISABLE |
5694 ILK_DPFDUNIT_CLOCK_GATE_ENABLE;
5696 I915_WRITE(PCH_3DCGDIS0,
5697 MARIUNIT_CLOCK_GATE_DISABLE |
5698 SVSMUNIT_CLOCK_GATE_DISABLE);
5699 I915_WRITE(PCH_3DCGDIS1,
5700 VFMUNIT_CLOCK_GATE_DISABLE);
5703 * According to the spec the following bits should be set in
5704 * order to enable memory self-refresh
5705 * The bit 22/21 of 0x42004
5706 * The bit 5 of 0x42020
5707 * The bit 15 of 0x45000
5709 I915_WRITE(ILK_DISPLAY_CHICKEN2,
5710 (I915_READ(ILK_DISPLAY_CHICKEN2) |
5711 ILK_DPARB_GATE | ILK_VSDPFD_FULL));
5712 dspclk_gate |= ILK_DPARBUNIT_CLOCK_GATE_ENABLE;
5713 I915_WRITE(DISP_ARB_CTL,
5714 (I915_READ(DISP_ARB_CTL) |
5717 ilk_init_lp_watermarks(dev);
5720 * Based on the document from hardware guys the following bits
5721 * should be set unconditionally in order to enable FBC.
5722 * The bit 22 of 0x42000
5723 * The bit 22 of 0x42004
5724 * The bit 7,8,9 of 0x42020.
5726 if (IS_IRONLAKE_M(dev)) {
5727 /* WaFbcAsynchFlipDisableFbcQueue:ilk */
5728 I915_WRITE(ILK_DISPLAY_CHICKEN1,
5729 I915_READ(ILK_DISPLAY_CHICKEN1) |
5731 I915_WRITE(ILK_DISPLAY_CHICKEN2,
5732 I915_READ(ILK_DISPLAY_CHICKEN2) |
5736 I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
5738 I915_WRITE(ILK_DISPLAY_CHICKEN2,
5739 I915_READ(ILK_DISPLAY_CHICKEN2) |
5740 ILK_ELPIN_409_SELECT);
5741 I915_WRITE(_3D_CHICKEN2,
5742 _3D_CHICKEN2_WM_READ_PIPELINED << 16 |
5743 _3D_CHICKEN2_WM_READ_PIPELINED);
5745 /* WaDisableRenderCachePipelinedFlush:ilk */
5746 I915_WRITE(CACHE_MODE_0,
5747 _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
5749 /* WaDisable_RenderCache_OperationalFlush:ilk */
5750 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
5752 g4x_disable_trickle_feed(dev);
5754 ibx_init_clock_gating(dev);
5757 static void cpt_init_clock_gating(struct drm_device *dev)
5759 struct drm_i915_private *dev_priv = dev->dev_private;
5764 * On Ibex Peak and Cougar Point, we need to disable clock
5765 * gating for the panel power sequencer or it will fail to
5766 * start up when no ports are active.
5768 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE |
5769 PCH_DPLUNIT_CLOCK_GATE_DISABLE |
5770 PCH_CPUNIT_CLOCK_GATE_DISABLE);
5771 I915_WRITE(SOUTH_CHICKEN2, I915_READ(SOUTH_CHICKEN2) |
5772 DPLS_EDP_PPS_FIX_DIS);
5773 /* The below fixes the weird display corruption, a few pixels shifted
5774 * downward, on (only) LVDS of some HP laptops with IVY.
5776 for_each_pipe(dev_priv, pipe) {
5777 val = I915_READ(TRANS_CHICKEN2(pipe));
5778 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
5779 val &= ~TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
5780 if (dev_priv->vbt.fdi_rx_polarity_inverted)
5781 val |= TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
5782 val &= ~TRANS_CHICKEN2_FRAME_START_DELAY_MASK;
5783 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_COUNTER;
5784 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_MODESWITCH;
5785 I915_WRITE(TRANS_CHICKEN2(pipe), val);
5787 /* WADP0ClockGatingDisable */
5788 for_each_pipe(dev_priv, pipe) {
5789 I915_WRITE(TRANS_CHICKEN1(pipe),
5790 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
5794 static void gen6_check_mch_setup(struct drm_device *dev)
5796 struct drm_i915_private *dev_priv = dev->dev_private;
5799 tmp = I915_READ(MCH_SSKPD);
5800 if ((tmp & MCH_SSKPD_WM0_MASK) != MCH_SSKPD_WM0_VAL)
5801 DRM_DEBUG_KMS("Wrong MCH_SSKPD value: 0x%08x This can cause underruns.\n",
5805 static void gen6_init_clock_gating(struct drm_device *dev)
5807 struct drm_i915_private *dev_priv = dev->dev_private;
5808 uint32_t dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
5810 I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
5812 I915_WRITE(ILK_DISPLAY_CHICKEN2,
5813 I915_READ(ILK_DISPLAY_CHICKEN2) |
5814 ILK_ELPIN_409_SELECT);
5816 /* WaDisableHiZPlanesWhenMSAAEnabled:snb */
5817 I915_WRITE(_3D_CHICKEN,
5818 _MASKED_BIT_ENABLE(_3D_CHICKEN_HIZ_PLANE_DISABLE_MSAA_4X_SNB));
5820 /* WaDisable_RenderCache_OperationalFlush:snb */
5821 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
5824 * BSpec recoomends 8x4 when MSAA is used,
5825 * however in practice 16x4 seems fastest.
5827 * Note that PS/WM thread counts depend on the WIZ hashing
5828 * disable bit, which we don't touch here, but it's good
5829 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
5831 I915_WRITE(GEN6_GT_MODE,
5832 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
5834 ilk_init_lp_watermarks(dev);
5836 I915_WRITE(CACHE_MODE_0,
5837 _MASKED_BIT_DISABLE(CM0_STC_EVICT_DISABLE_LRA_SNB));
5839 I915_WRITE(GEN6_UCGCTL1,
5840 I915_READ(GEN6_UCGCTL1) |
5841 GEN6_BLBUNIT_CLOCK_GATE_DISABLE |
5842 GEN6_CSUNIT_CLOCK_GATE_DISABLE);
5844 /* According to the BSpec vol1g, bit 12 (RCPBUNIT) clock
5845 * gating disable must be set. Failure to set it results in
5846 * flickering pixels due to Z write ordering failures after
5847 * some amount of runtime in the Mesa "fire" demo, and Unigine
5848 * Sanctuary and Tropics, and apparently anything else with
5849 * alpha test or pixel discard.
5851 * According to the spec, bit 11 (RCCUNIT) must also be set,
5852 * but we didn't debug actual testcases to find it out.
5854 * WaDisableRCCUnitClockGating:snb
5855 * WaDisableRCPBUnitClockGating:snb
5857 I915_WRITE(GEN6_UCGCTL2,
5858 GEN6_RCPBUNIT_CLOCK_GATE_DISABLE |
5859 GEN6_RCCUNIT_CLOCK_GATE_DISABLE);
5861 /* WaStripsFansDisableFastClipPerformanceFix:snb */
5862 I915_WRITE(_3D_CHICKEN3,
5863 _MASKED_BIT_ENABLE(_3D_CHICKEN3_SF_DISABLE_FASTCLIP_CULL));
5867 * "This bit must be set if 3DSTATE_CLIP clip mode is set to normal and
5868 * 3DSTATE_SF number of SF output attributes is more than 16."
5870 I915_WRITE(_3D_CHICKEN3,
5871 _MASKED_BIT_ENABLE(_3D_CHICKEN3_SF_DISABLE_PIPELINED_ATTR_FETCH));
5874 * According to the spec the following bits should be
5875 * set in order to enable memory self-refresh and fbc:
5876 * The bit21 and bit22 of 0x42000
5877 * The bit21 and bit22 of 0x42004
5878 * The bit5 and bit7 of 0x42020
5879 * The bit14 of 0x70180
5880 * The bit14 of 0x71180
5882 * WaFbcAsynchFlipDisableFbcQueue:snb
5884 I915_WRITE(ILK_DISPLAY_CHICKEN1,
5885 I915_READ(ILK_DISPLAY_CHICKEN1) |
5886 ILK_FBCQ_DIS | ILK_PABSTRETCH_DIS);
5887 I915_WRITE(ILK_DISPLAY_CHICKEN2,
5888 I915_READ(ILK_DISPLAY_CHICKEN2) |
5889 ILK_DPARB_GATE | ILK_VSDPFD_FULL);
5890 I915_WRITE(ILK_DSPCLK_GATE_D,
5891 I915_READ(ILK_DSPCLK_GATE_D) |
5892 ILK_DPARBUNIT_CLOCK_GATE_ENABLE |
5893 ILK_DPFDUNIT_CLOCK_GATE_ENABLE);
5895 g4x_disable_trickle_feed(dev);
5897 cpt_init_clock_gating(dev);
5899 gen6_check_mch_setup(dev);
5902 static void gen7_setup_fixed_func_scheduler(struct drm_i915_private *dev_priv)
5904 uint32_t reg = I915_READ(GEN7_FF_THREAD_MODE);
5907 * WaVSThreadDispatchOverride:ivb,vlv
5909 * This actually overrides the dispatch
5910 * mode for all thread types.
5912 reg &= ~GEN7_FF_SCHED_MASK;
5913 reg |= GEN7_FF_TS_SCHED_HW;
5914 reg |= GEN7_FF_VS_SCHED_HW;
5915 reg |= GEN7_FF_DS_SCHED_HW;
5917 I915_WRITE(GEN7_FF_THREAD_MODE, reg);
5920 static void lpt_init_clock_gating(struct drm_device *dev)
5922 struct drm_i915_private *dev_priv = dev->dev_private;
5925 * TODO: this bit should only be enabled when really needed, then
5926 * disabled when not needed anymore in order to save power.
5928 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE)
5929 I915_WRITE(SOUTH_DSPCLK_GATE_D,
5930 I915_READ(SOUTH_DSPCLK_GATE_D) |
5931 PCH_LP_PARTITION_LEVEL_DISABLE);
5933 /* WADPOClockGatingDisable:hsw */
5934 I915_WRITE(_TRANSA_CHICKEN1,
5935 I915_READ(_TRANSA_CHICKEN1) |
5936 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
5939 static void lpt_suspend_hw(struct drm_device *dev)
5941 struct drm_i915_private *dev_priv = dev->dev_private;
5943 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
5944 uint32_t val = I915_READ(SOUTH_DSPCLK_GATE_D);
5946 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
5947 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
5951 static void broadwell_init_clock_gating(struct drm_device *dev)
5953 struct drm_i915_private *dev_priv = dev->dev_private;
5956 I915_WRITE(WM3_LP_ILK, 0);
5957 I915_WRITE(WM2_LP_ILK, 0);
5958 I915_WRITE(WM1_LP_ILK, 0);
5960 /* WaSwitchSolVfFArbitrationPriority:bdw */
5961 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
5963 /* WaPsrDPAMaskVBlankInSRD:bdw */
5964 I915_WRITE(CHICKEN_PAR1_1,
5965 I915_READ(CHICKEN_PAR1_1) | DPA_MASK_VBLANK_SRD);
5967 /* WaPsrDPRSUnmaskVBlankInSRD:bdw */
5968 for_each_pipe(dev_priv, pipe) {
5969 I915_WRITE(CHICKEN_PIPESL_1(pipe),
5970 I915_READ(CHICKEN_PIPESL_1(pipe)) |
5971 BDW_DPRS_MASK_VBLANK_SRD);
5974 /* WaVSRefCountFullforceMissDisable:bdw */
5975 /* WaDSRefCountFullforceMissDisable:bdw */
5976 I915_WRITE(GEN7_FF_THREAD_MODE,
5977 I915_READ(GEN7_FF_THREAD_MODE) &
5978 ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
5980 I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
5981 _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
5983 /* WaDisableSDEUnitClockGating:bdw */
5984 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
5985 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
5987 lpt_init_clock_gating(dev);
5990 static void haswell_init_clock_gating(struct drm_device *dev)
5992 struct drm_i915_private *dev_priv = dev->dev_private;
5994 ilk_init_lp_watermarks(dev);
5996 /* L3 caching of data atomics doesn't work -- disable it. */
5997 I915_WRITE(HSW_SCRATCH1, HSW_SCRATCH1_L3_DATA_ATOMICS_DISABLE);
5998 I915_WRITE(HSW_ROW_CHICKEN3,
5999 _MASKED_BIT_ENABLE(HSW_ROW_CHICKEN3_L3_GLOBAL_ATOMICS_DISABLE));
6001 /* This is required by WaCatErrorRejectionIssue:hsw */
6002 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
6003 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
6004 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
6006 /* WaVSRefCountFullforceMissDisable:hsw */
6007 I915_WRITE(GEN7_FF_THREAD_MODE,
6008 I915_READ(GEN7_FF_THREAD_MODE) & ~GEN7_FF_VS_REF_CNT_FFME);
6010 /* WaDisable_RenderCache_OperationalFlush:hsw */
6011 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
6013 /* enable HiZ Raw Stall Optimization */
6014 I915_WRITE(CACHE_MODE_0_GEN7,
6015 _MASKED_BIT_DISABLE(HIZ_RAW_STALL_OPT_DISABLE));
6017 /* WaDisable4x2SubspanOptimization:hsw */
6018 I915_WRITE(CACHE_MODE_1,
6019 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
6022 * BSpec recommends 8x4 when MSAA is used,
6023 * however in practice 16x4 seems fastest.
6025 * Note that PS/WM thread counts depend on the WIZ hashing
6026 * disable bit, which we don't touch here, but it's good
6027 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
6029 I915_WRITE(GEN7_GT_MODE,
6030 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
6032 /* WaSampleCChickenBitEnable:hsw */
6033 I915_WRITE(HALF_SLICE_CHICKEN3,
6034 _MASKED_BIT_ENABLE(HSW_SAMPLE_C_PERFORMANCE));
6036 /* WaSwitchSolVfFArbitrationPriority:hsw */
6037 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
6039 /* WaRsPkgCStateDisplayPMReq:hsw */
6040 I915_WRITE(CHICKEN_PAR1_1,
6041 I915_READ(CHICKEN_PAR1_1) | FORCE_ARB_IDLE_PLANES);
6043 lpt_init_clock_gating(dev);
6046 static void ivybridge_init_clock_gating(struct drm_device *dev)
6048 struct drm_i915_private *dev_priv = dev->dev_private;
6051 ilk_init_lp_watermarks(dev);
6053 I915_WRITE(ILK_DSPCLK_GATE_D, ILK_VRHUNIT_CLOCK_GATE_DISABLE);
6055 /* WaDisableEarlyCull:ivb */
6056 I915_WRITE(_3D_CHICKEN3,
6057 _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
6059 /* WaDisableBackToBackFlipFix:ivb */
6060 I915_WRITE(IVB_CHICKEN3,
6061 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
6062 CHICKEN3_DGMG_DONE_FIX_DISABLE);
6064 /* WaDisablePSDDualDispatchEnable:ivb */
6065 if (IS_IVB_GT1(dev))
6066 I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
6067 _MASKED_BIT_ENABLE(GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
6069 /* WaDisable_RenderCache_OperationalFlush:ivb */
6070 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
6072 /* Apply the WaDisableRHWOOptimizationForRenderHang:ivb workaround. */
6073 I915_WRITE(GEN7_COMMON_SLICE_CHICKEN1,
6074 GEN7_CSC1_RHWO_OPT_DISABLE_IN_RCC);
6076 /* WaApplyL3ControlAndL3ChickenMode:ivb */
6077 I915_WRITE(GEN7_L3CNTLREG1,
6078 GEN7_WA_FOR_GEN7_L3_CONTROL);
6079 I915_WRITE(GEN7_L3_CHICKEN_MODE_REGISTER,
6080 GEN7_WA_L3_CHICKEN_MODE);
6081 if (IS_IVB_GT1(dev))
6082 I915_WRITE(GEN7_ROW_CHICKEN2,
6083 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
6085 /* must write both registers */
6086 I915_WRITE(GEN7_ROW_CHICKEN2,
6087 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
6088 I915_WRITE(GEN7_ROW_CHICKEN2_GT2,
6089 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
6092 /* WaForceL3Serialization:ivb */
6093 I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
6094 ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
6097 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
6098 * This implements the WaDisableRCZUnitClockGating:ivb workaround.
6100 I915_WRITE(GEN6_UCGCTL2,
6101 GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
6103 /* This is required by WaCatErrorRejectionIssue:ivb */
6104 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
6105 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
6106 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
6108 g4x_disable_trickle_feed(dev);
6110 gen7_setup_fixed_func_scheduler(dev_priv);
6112 if (0) { /* causes HiZ corruption on ivb:gt1 */
6113 /* enable HiZ Raw Stall Optimization */
6114 I915_WRITE(CACHE_MODE_0_GEN7,
6115 _MASKED_BIT_DISABLE(HIZ_RAW_STALL_OPT_DISABLE));
6118 /* WaDisable4x2SubspanOptimization:ivb */
6119 I915_WRITE(CACHE_MODE_1,
6120 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
6123 * BSpec recommends 8x4 when MSAA is used,
6124 * however in practice 16x4 seems fastest.
6126 * Note that PS/WM thread counts depend on the WIZ hashing
6127 * disable bit, which we don't touch here, but it's good
6128 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
6130 I915_WRITE(GEN7_GT_MODE,
6131 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
6133 snpcr = I915_READ(GEN6_MBCUNIT_SNPCR);
6134 snpcr &= ~GEN6_MBC_SNPCR_MASK;
6135 snpcr |= GEN6_MBC_SNPCR_MED;
6136 I915_WRITE(GEN6_MBCUNIT_SNPCR, snpcr);
6138 if (!HAS_PCH_NOP(dev))
6139 cpt_init_clock_gating(dev);
6141 gen6_check_mch_setup(dev);
6144 static void vlv_init_display_clock_gating(struct drm_i915_private *dev_priv)
6146 I915_WRITE(DSPCLK_GATE_D, VRHUNIT_CLOCK_GATE_DISABLE);
6149 * Disable trickle feed and enable pnd deadline calculation
6151 I915_WRITE(MI_ARB_VLV, MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE);
6152 I915_WRITE(CBR1_VLV, 0);
6155 static void valleyview_init_clock_gating(struct drm_device *dev)
6157 struct drm_i915_private *dev_priv = dev->dev_private;
6159 vlv_init_display_clock_gating(dev_priv);
6161 /* WaDisableEarlyCull:vlv */
6162 I915_WRITE(_3D_CHICKEN3,
6163 _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
6165 /* WaDisableBackToBackFlipFix:vlv */
6166 I915_WRITE(IVB_CHICKEN3,
6167 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
6168 CHICKEN3_DGMG_DONE_FIX_DISABLE);
6170 /* WaPsdDispatchEnable:vlv */
6171 /* WaDisablePSDDualDispatchEnable:vlv */
6172 I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
6173 _MASKED_BIT_ENABLE(GEN7_MAX_PS_THREAD_DEP |
6174 GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
6176 /* WaDisable_RenderCache_OperationalFlush:vlv */
6177 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
6179 /* WaForceL3Serialization:vlv */
6180 I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
6181 ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
6183 /* WaDisableDopClockGating:vlv */
6184 I915_WRITE(GEN7_ROW_CHICKEN2,
6185 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
6187 /* This is required by WaCatErrorRejectionIssue:vlv */
6188 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
6189 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
6190 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
6192 gen7_setup_fixed_func_scheduler(dev_priv);
6195 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
6196 * This implements the WaDisableRCZUnitClockGating:vlv workaround.
6198 I915_WRITE(GEN6_UCGCTL2,
6199 GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
6201 /* WaDisableL3Bank2xClockGate:vlv
6202 * Disabling L3 clock gating- MMIO 940c[25] = 1
6203 * Set bit 25, to disable L3_BANK_2x_CLK_GATING */
6204 I915_WRITE(GEN7_UCGCTL4,
6205 I915_READ(GEN7_UCGCTL4) | GEN7_L3BANK2X_CLOCK_GATE_DISABLE);
6208 * BSpec says this must be set, even though
6209 * WaDisable4x2SubspanOptimization isn't listed for VLV.
6211 I915_WRITE(CACHE_MODE_1,
6212 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
6215 * BSpec recommends 8x4 when MSAA is used,
6216 * however in practice 16x4 seems fastest.
6218 * Note that PS/WM thread counts depend on the WIZ hashing
6219 * disable bit, which we don't touch here, but it's good
6220 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
6222 I915_WRITE(GEN7_GT_MODE,
6223 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
6226 * WaIncreaseL3CreditsForVLVB0:vlv
6227 * This is the hardware default actually.
6229 I915_WRITE(GEN7_L3SQCREG1, VLV_B0_WA_L3SQCREG1_VALUE);
6232 * WaDisableVLVClockGating_VBIIssue:vlv
6233 * Disable clock gating on th GCFG unit to prevent a delay
6234 * in the reporting of vblank events.
6236 I915_WRITE(VLV_GUNIT_CLOCK_GATE, GCFG_DIS);
6239 static void cherryview_init_clock_gating(struct drm_device *dev)
6241 struct drm_i915_private *dev_priv = dev->dev_private;
6243 vlv_init_display_clock_gating(dev_priv);
6245 /* WaVSRefCountFullforceMissDisable:chv */
6246 /* WaDSRefCountFullforceMissDisable:chv */
6247 I915_WRITE(GEN7_FF_THREAD_MODE,
6248 I915_READ(GEN7_FF_THREAD_MODE) &
6249 ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
6251 /* WaDisableSemaphoreAndSyncFlipWait:chv */
6252 I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
6253 _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
6255 /* WaDisableCSUnitClockGating:chv */
6256 I915_WRITE(GEN6_UCGCTL1, I915_READ(GEN6_UCGCTL1) |
6257 GEN6_CSUNIT_CLOCK_GATE_DISABLE);
6259 /* WaDisableSDEUnitClockGating:chv */
6260 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
6261 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
6264 static void g4x_init_clock_gating(struct drm_device *dev)
6266 struct drm_i915_private *dev_priv = dev->dev_private;
6267 uint32_t dspclk_gate;
6269 I915_WRITE(RENCLK_GATE_D1, 0);
6270 I915_WRITE(RENCLK_GATE_D2, VF_UNIT_CLOCK_GATE_DISABLE |
6271 GS_UNIT_CLOCK_GATE_DISABLE |
6272 CL_UNIT_CLOCK_GATE_DISABLE);
6273 I915_WRITE(RAMCLK_GATE_D, 0);
6274 dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE |
6275 OVRUNIT_CLOCK_GATE_DISABLE |
6276 OVCUNIT_CLOCK_GATE_DISABLE;
6278 dspclk_gate |= DSSUNIT_CLOCK_GATE_DISABLE;
6279 I915_WRITE(DSPCLK_GATE_D, dspclk_gate);
6281 /* WaDisableRenderCachePipelinedFlush */
6282 I915_WRITE(CACHE_MODE_0,
6283 _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
6285 /* WaDisable_RenderCache_OperationalFlush:g4x */
6286 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
6288 g4x_disable_trickle_feed(dev);
6291 static void crestline_init_clock_gating(struct drm_device *dev)
6293 struct drm_i915_private *dev_priv = dev->dev_private;
6295 I915_WRITE(RENCLK_GATE_D1, I965_RCC_CLOCK_GATE_DISABLE);
6296 I915_WRITE(RENCLK_GATE_D2, 0);
6297 I915_WRITE(DSPCLK_GATE_D, 0);
6298 I915_WRITE(RAMCLK_GATE_D, 0);
6299 I915_WRITE16(DEUC, 0);
6300 I915_WRITE(MI_ARB_STATE,
6301 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
6303 /* WaDisable_RenderCache_OperationalFlush:gen4 */
6304 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
6307 static void broadwater_init_clock_gating(struct drm_device *dev)
6309 struct drm_i915_private *dev_priv = dev->dev_private;
6311 I915_WRITE(RENCLK_GATE_D1, I965_RCZ_CLOCK_GATE_DISABLE |
6312 I965_RCC_CLOCK_GATE_DISABLE |
6313 I965_RCPB_CLOCK_GATE_DISABLE |
6314 I965_ISC_CLOCK_GATE_DISABLE |
6315 I965_FBC_CLOCK_GATE_DISABLE);
6316 I915_WRITE(RENCLK_GATE_D2, 0);
6317 I915_WRITE(MI_ARB_STATE,
6318 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
6320 /* WaDisable_RenderCache_OperationalFlush:gen4 */
6321 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
6324 static void gen3_init_clock_gating(struct drm_device *dev)
6326 struct drm_i915_private *dev_priv = dev->dev_private;
6327 u32 dstate = I915_READ(D_STATE);
6329 dstate |= DSTATE_PLL_D3_OFF | DSTATE_GFX_CLOCK_GATING |
6330 DSTATE_DOT_CLOCK_GATING;
6331 I915_WRITE(D_STATE, dstate);
6333 if (IS_PINEVIEW(dev))
6334 I915_WRITE(ECOSKPD, _MASKED_BIT_ENABLE(ECO_GATING_CX_ONLY));
6336 /* IIR "flip pending" means done if this bit is set */
6337 I915_WRITE(ECOSKPD, _MASKED_BIT_DISABLE(ECO_FLIP_DONE));
6339 /* interrupts should cause a wake up from C3 */
6340 I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_AGPBUSY_INT_EN));
6342 /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
6343 I915_WRITE(MI_ARB_STATE, _MASKED_BIT_ENABLE(MI_ARB_C3_LP_WRITE_ENABLE));
6345 I915_WRITE(MI_ARB_STATE,
6346 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
6349 static void i85x_init_clock_gating(struct drm_device *dev)
6351 struct drm_i915_private *dev_priv = dev->dev_private;
6353 I915_WRITE(RENCLK_GATE_D1, SV_CLOCK_GATE_DISABLE);
6355 /* interrupts should cause a wake up from C3 */
6356 I915_WRITE(MI_STATE, _MASKED_BIT_ENABLE(MI_AGPBUSY_INT_EN) |
6357 _MASKED_BIT_DISABLE(MI_AGPBUSY_830_MODE));
6359 I915_WRITE(MEM_MODE,
6360 _MASKED_BIT_ENABLE(MEM_DISPLAY_TRICKLE_FEED_DISABLE));
6363 static void i830_init_clock_gating(struct drm_device *dev)
6365 struct drm_i915_private *dev_priv = dev->dev_private;
6367 I915_WRITE(DSPCLK_GATE_D, OVRUNIT_CLOCK_GATE_DISABLE);
6369 I915_WRITE(MEM_MODE,
6370 _MASKED_BIT_ENABLE(MEM_DISPLAY_A_TRICKLE_FEED_DISABLE) |
6371 _MASKED_BIT_ENABLE(MEM_DISPLAY_B_TRICKLE_FEED_DISABLE));
6374 void intel_init_clock_gating(struct drm_device *dev)
6376 struct drm_i915_private *dev_priv = dev->dev_private;
6378 if (dev_priv->display.init_clock_gating)
6379 dev_priv->display.init_clock_gating(dev);
6382 void intel_suspend_hw(struct drm_device *dev)
6384 if (HAS_PCH_LPT(dev))
6385 lpt_suspend_hw(dev);
6388 /* Set up chip specific power management-related functions */
6389 void intel_init_pm(struct drm_device *dev)
6391 struct drm_i915_private *dev_priv = dev->dev_private;
6393 intel_fbc_init(dev_priv);
6396 if (IS_PINEVIEW(dev))
6397 i915_pineview_get_mem_freq(dev);
6398 else if (IS_GEN5(dev))
6399 i915_ironlake_get_mem_freq(dev);
6401 /* For FIFO watermark updates */
6402 if (INTEL_INFO(dev)->gen >= 9) {
6403 skl_setup_wm_latency(dev);
6405 dev_priv->display.init_clock_gating = skl_init_clock_gating;
6406 dev_priv->display.update_wm = skl_update_wm;
6407 dev_priv->display.update_sprite_wm = skl_update_sprite_wm;
6408 } else if (HAS_PCH_SPLIT(dev)) {
6409 ilk_setup_wm_latency(dev);
6411 if ((IS_GEN5(dev) && dev_priv->wm.pri_latency[1] &&
6412 dev_priv->wm.spr_latency[1] && dev_priv->wm.cur_latency[1]) ||
6413 (!IS_GEN5(dev) && dev_priv->wm.pri_latency[0] &&
6414 dev_priv->wm.spr_latency[0] && dev_priv->wm.cur_latency[0])) {
6415 dev_priv->display.update_wm = ilk_update_wm;
6416 dev_priv->display.update_sprite_wm = ilk_update_sprite_wm;
6418 DRM_DEBUG_KMS("Failed to read display plane latency. "
6423 dev_priv->display.init_clock_gating = ironlake_init_clock_gating;
6424 else if (IS_GEN6(dev))
6425 dev_priv->display.init_clock_gating = gen6_init_clock_gating;
6426 else if (IS_IVYBRIDGE(dev))
6427 dev_priv->display.init_clock_gating = ivybridge_init_clock_gating;
6428 else if (IS_HASWELL(dev))
6429 dev_priv->display.init_clock_gating = haswell_init_clock_gating;
6430 else if (INTEL_INFO(dev)->gen == 8)
6431 dev_priv->display.init_clock_gating = broadwell_init_clock_gating;
6432 } else if (IS_CHERRYVIEW(dev)) {
6433 dev_priv->display.update_wm = cherryview_update_wm;
6434 dev_priv->display.update_sprite_wm = valleyview_update_sprite_wm;
6435 dev_priv->display.init_clock_gating =
6436 cherryview_init_clock_gating;
6437 } else if (IS_VALLEYVIEW(dev)) {
6438 dev_priv->display.update_wm = valleyview_update_wm;
6439 dev_priv->display.update_sprite_wm = valleyview_update_sprite_wm;
6440 dev_priv->display.init_clock_gating =
6441 valleyview_init_clock_gating;
6442 } else if (IS_PINEVIEW(dev)) {
6443 if (!intel_get_cxsr_latency(IS_PINEVIEW_G(dev),
6446 dev_priv->mem_freq)) {
6447 DRM_INFO("failed to find known CxSR latency "
6448 "(found ddr%s fsb freq %d, mem freq %d), "
6450 (dev_priv->is_ddr3 == 1) ? "3" : "2",
6451 dev_priv->fsb_freq, dev_priv->mem_freq);
6452 /* Disable CxSR and never update its watermark again */
6453 intel_set_memory_cxsr(dev_priv, false);
6454 dev_priv->display.update_wm = NULL;
6456 dev_priv->display.update_wm = pineview_update_wm;
6457 dev_priv->display.init_clock_gating = gen3_init_clock_gating;
6458 } else if (IS_G4X(dev)) {
6459 dev_priv->display.update_wm = g4x_update_wm;
6460 dev_priv->display.init_clock_gating = g4x_init_clock_gating;
6461 } else if (IS_GEN4(dev)) {
6462 dev_priv->display.update_wm = i965_update_wm;
6463 if (IS_CRESTLINE(dev))
6464 dev_priv->display.init_clock_gating = crestline_init_clock_gating;
6465 else if (IS_BROADWATER(dev))
6466 dev_priv->display.init_clock_gating = broadwater_init_clock_gating;
6467 } else if (IS_GEN3(dev)) {
6468 dev_priv->display.update_wm = i9xx_update_wm;
6469 dev_priv->display.get_fifo_size = i9xx_get_fifo_size;
6470 dev_priv->display.init_clock_gating = gen3_init_clock_gating;
6471 } else if (IS_GEN2(dev)) {
6472 if (INTEL_INFO(dev)->num_pipes == 1) {
6473 dev_priv->display.update_wm = i845_update_wm;
6474 dev_priv->display.get_fifo_size = i845_get_fifo_size;
6476 dev_priv->display.update_wm = i9xx_update_wm;
6477 dev_priv->display.get_fifo_size = i830_get_fifo_size;
6480 if (IS_I85X(dev) || IS_I865G(dev))
6481 dev_priv->display.init_clock_gating = i85x_init_clock_gating;
6483 dev_priv->display.init_clock_gating = i830_init_clock_gating;
6485 DRM_ERROR("unexpected fall-through in intel_init_pm\n");
6489 int sandybridge_pcode_read(struct drm_i915_private *dev_priv, u32 mbox, u32 *val)
6491 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
6493 if (I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) {
6494 DRM_DEBUG_DRIVER("warning: pcode (read) mailbox access failed\n");
6498 I915_WRITE(GEN6_PCODE_DATA, *val);
6499 I915_WRITE(GEN6_PCODE_DATA1, 0);
6500 I915_WRITE(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY | mbox);
6502 if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
6504 DRM_ERROR("timeout waiting for pcode read (%d) to finish\n", mbox);
6508 *val = I915_READ(GEN6_PCODE_DATA);
6509 I915_WRITE(GEN6_PCODE_DATA, 0);
6514 int sandybridge_pcode_write(struct drm_i915_private *dev_priv, u32 mbox, u32 val)
6516 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
6518 if (I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) {
6519 DRM_DEBUG_DRIVER("warning: pcode (write) mailbox access failed\n");
6523 I915_WRITE(GEN6_PCODE_DATA, val);
6524 I915_WRITE(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY | mbox);
6526 if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
6528 DRM_ERROR("timeout waiting for pcode write (%d) to finish\n", mbox);
6532 I915_WRITE(GEN6_PCODE_DATA, 0);
6537 static int vlv_gpu_freq_div(unsigned int czclk_freq)
6539 switch (czclk_freq) {
6554 static int byt_gpu_freq(struct drm_i915_private *dev_priv, int val)
6556 int div, czclk_freq = DIV_ROUND_CLOSEST(dev_priv->mem_freq, 4);
6558 div = vlv_gpu_freq_div(czclk_freq);
6562 return DIV_ROUND_CLOSEST(czclk_freq * (val + 6 - 0xbd), div);
6565 static int byt_freq_opcode(struct drm_i915_private *dev_priv, int val)
6567 int mul, czclk_freq = DIV_ROUND_CLOSEST(dev_priv->mem_freq, 4);
6569 mul = vlv_gpu_freq_div(czclk_freq);
6573 return DIV_ROUND_CLOSEST(mul * val, czclk_freq) + 0xbd - 6;
6576 static int chv_gpu_freq(struct drm_i915_private *dev_priv, int val)
6578 int div, czclk_freq = dev_priv->rps.cz_freq;
6580 div = vlv_gpu_freq_div(czclk_freq) / 2;
6584 return DIV_ROUND_CLOSEST(czclk_freq * val, 2 * div) / 2;
6587 static int chv_freq_opcode(struct drm_i915_private *dev_priv, int val)
6589 int mul, czclk_freq = dev_priv->rps.cz_freq;
6591 mul = vlv_gpu_freq_div(czclk_freq) / 2;
6595 /* CHV needs even values */
6596 return DIV_ROUND_CLOSEST(val * 2 * mul, czclk_freq) * 2;
6599 int intel_gpu_freq(struct drm_i915_private *dev_priv, int val)
6601 if (IS_CHERRYVIEW(dev_priv->dev))
6602 return chv_gpu_freq(dev_priv, val);
6603 else if (IS_VALLEYVIEW(dev_priv->dev))
6604 return byt_gpu_freq(dev_priv, val);
6606 return val * GT_FREQUENCY_MULTIPLIER;
6609 int intel_freq_opcode(struct drm_i915_private *dev_priv, int val)
6611 if (IS_CHERRYVIEW(dev_priv->dev))
6612 return chv_freq_opcode(dev_priv, val);
6613 else if (IS_VALLEYVIEW(dev_priv->dev))
6614 return byt_freq_opcode(dev_priv, val);
6616 return val / GT_FREQUENCY_MULTIPLIER;
6619 void intel_pm_setup(struct drm_device *dev)
6621 struct drm_i915_private *dev_priv = dev->dev_private;
6623 mutex_init(&dev_priv->rps.hw_lock);
6625 INIT_DELAYED_WORK(&dev_priv->rps.delayed_resume_work,
6626 intel_gen6_powersave_work);
6628 dev_priv->pm.suspended = false;