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
28 #include <linux/module.h>
29 #include <linux/string_helpers.h>
30 #include <linux/pm_runtime.h>
32 #include <drm/drm_atomic_helper.h>
33 #include <drm/drm_blend.h>
34 #include <drm/drm_fourcc.h>
36 #include "display/intel_atomic.h"
37 #include "display/intel_atomic_plane.h"
38 #include "display/intel_bw.h"
39 #include "display/intel_de.h"
40 #include "display/intel_display_trace.h"
41 #include "display/intel_display_types.h"
42 #include "display/intel_fb.h"
43 #include "display/intel_fbc.h"
44 #include "display/intel_sprite.h"
45 #include "display/skl_universal_plane.h"
47 #include "gt/intel_engine_regs.h"
48 #include "gt/intel_gt_regs.h"
49 #include "gt/intel_llc.h"
52 #include "i915_fixed.h"
54 #include "intel_mchbar_regs.h"
55 #include "intel_pcode.h"
57 #include "vlv_sideband.h"
58 #include "../../../platform/x86/intel_ips.h"
60 static void skl_sagv_disable(struct drm_i915_private *dev_priv);
62 struct drm_i915_clock_gating_funcs {
63 void (*init_clock_gating)(struct drm_i915_private *i915);
66 /* Stores plane specific WM parameters */
67 struct skl_wm_params {
68 bool x_tiled, y_tiled;
75 u32 plane_bytes_per_line;
76 uint_fixed_16_16_t plane_blocks_per_line;
77 uint_fixed_16_16_t y_tile_minimum;
82 /* used in computing the new watermarks state */
83 struct intel_wm_config {
84 unsigned int num_pipes_active;
89 static void gen9_init_clock_gating(struct drm_i915_private *dev_priv)
91 if (HAS_LLC(dev_priv)) {
93 * WaCompressedResourceDisplayNewHashMode:skl,kbl
94 * Display WA #0390: skl,kbl
96 * Must match Sampler, Pixel Back End, and Media. See
97 * WaCompressedResourceSamplerPbeMediaNewHashMode.
99 intel_uncore_write(&dev_priv->uncore, CHICKEN_PAR1_1,
100 intel_uncore_read(&dev_priv->uncore, CHICKEN_PAR1_1) |
101 SKL_DE_COMPRESSED_HASH_MODE);
104 /* See Bspec note for PSR2_CTL bit 31, Wa#828:skl,bxt,kbl,cfl */
105 intel_uncore_write(&dev_priv->uncore, CHICKEN_PAR1_1,
106 intel_uncore_read(&dev_priv->uncore, CHICKEN_PAR1_1) | SKL_EDP_PSR_FIX_RDWRAP);
108 /* WaEnableChickenDCPR:skl,bxt,kbl,glk,cfl */
109 intel_uncore_write(&dev_priv->uncore, GEN8_CHICKEN_DCPR_1,
110 intel_uncore_read(&dev_priv->uncore, GEN8_CHICKEN_DCPR_1) | MASK_WAKEMEM);
113 * WaFbcWakeMemOn:skl,bxt,kbl,glk,cfl
114 * Display WA #0859: skl,bxt,kbl,glk,cfl
116 intel_uncore_write(&dev_priv->uncore, DISP_ARB_CTL, intel_uncore_read(&dev_priv->uncore, DISP_ARB_CTL) |
117 DISP_FBC_MEMORY_WAKE);
120 static void bxt_init_clock_gating(struct drm_i915_private *dev_priv)
122 gen9_init_clock_gating(dev_priv);
124 /* WaDisableSDEUnitClockGating:bxt */
125 intel_uncore_write(&dev_priv->uncore, GEN8_UCGCTL6, intel_uncore_read(&dev_priv->uncore, GEN8_UCGCTL6) |
126 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
130 * GEN8_HDCUNIT_CLOCK_GATE_DISABLE_HDCREQ applies on 3x6 GT SKUs only.
132 intel_uncore_write(&dev_priv->uncore, GEN8_UCGCTL6, intel_uncore_read(&dev_priv->uncore, GEN8_UCGCTL6) |
133 GEN8_HDCUNIT_CLOCK_GATE_DISABLE_HDCREQ);
136 * Wa: Backlight PWM may stop in the asserted state, causing backlight
139 intel_uncore_write(&dev_priv->uncore, GEN9_CLKGATE_DIS_0, intel_uncore_read(&dev_priv->uncore, GEN9_CLKGATE_DIS_0) |
140 PWM1_GATING_DIS | PWM2_GATING_DIS);
143 * Lower the display internal timeout.
144 * This is needed to avoid any hard hangs when DSI port PLL
145 * is off and a MMIO access is attempted by any privilege
146 * application, using batch buffers or any other means.
148 intel_uncore_write(&dev_priv->uncore, RM_TIMEOUT, MMIO_TIMEOUT_US(950));
151 * WaFbcTurnOffFbcWatermark:bxt
152 * Display WA #0562: bxt
154 intel_uncore_write(&dev_priv->uncore, DISP_ARB_CTL, intel_uncore_read(&dev_priv->uncore, DISP_ARB_CTL) |
158 * WaFbcHighMemBwCorruptionAvoidance:bxt
159 * Display WA #0883: bxt
161 intel_uncore_write(&dev_priv->uncore, ILK_DPFC_CHICKEN(INTEL_FBC_A),
162 intel_uncore_read(&dev_priv->uncore, ILK_DPFC_CHICKEN(INTEL_FBC_A)) |
163 DPFC_DISABLE_DUMMY0);
166 static void glk_init_clock_gating(struct drm_i915_private *dev_priv)
168 gen9_init_clock_gating(dev_priv);
171 * WaDisablePWMClockGating:glk
172 * Backlight PWM may stop in the asserted state, causing backlight
175 intel_uncore_write(&dev_priv->uncore, GEN9_CLKGATE_DIS_0, intel_uncore_read(&dev_priv->uncore, GEN9_CLKGATE_DIS_0) |
176 PWM1_GATING_DIS | PWM2_GATING_DIS);
179 static void pnv_get_mem_freq(struct drm_i915_private *dev_priv)
183 tmp = intel_uncore_read(&dev_priv->uncore, CLKCFG);
185 switch (tmp & CLKCFG_FSB_MASK) {
187 dev_priv->fsb_freq = 533; /* 133*4 */
190 dev_priv->fsb_freq = 800; /* 200*4 */
193 dev_priv->fsb_freq = 667; /* 167*4 */
196 dev_priv->fsb_freq = 400; /* 100*4 */
200 switch (tmp & CLKCFG_MEM_MASK) {
202 dev_priv->mem_freq = 533;
205 dev_priv->mem_freq = 667;
208 dev_priv->mem_freq = 800;
212 /* detect pineview DDR3 setting */
213 tmp = intel_uncore_read(&dev_priv->uncore, CSHRDDR3CTL);
214 dev_priv->is_ddr3 = (tmp & CSHRDDR3CTL_DDR3) ? 1 : 0;
217 static void ilk_get_mem_freq(struct drm_i915_private *dev_priv)
221 ddrpll = intel_uncore_read16(&dev_priv->uncore, DDRMPLL1);
222 csipll = intel_uncore_read16(&dev_priv->uncore, CSIPLL0);
224 switch (ddrpll & 0xff) {
226 dev_priv->mem_freq = 800;
229 dev_priv->mem_freq = 1066;
232 dev_priv->mem_freq = 1333;
235 dev_priv->mem_freq = 1600;
238 drm_dbg(&dev_priv->drm, "unknown memory frequency 0x%02x\n",
240 dev_priv->mem_freq = 0;
244 switch (csipll & 0x3ff) {
246 dev_priv->fsb_freq = 3200;
249 dev_priv->fsb_freq = 3733;
252 dev_priv->fsb_freq = 4266;
255 dev_priv->fsb_freq = 4800;
258 dev_priv->fsb_freq = 5333;
261 dev_priv->fsb_freq = 5866;
264 dev_priv->fsb_freq = 6400;
267 drm_dbg(&dev_priv->drm, "unknown fsb frequency 0x%04x\n",
269 dev_priv->fsb_freq = 0;
274 static const struct cxsr_latency cxsr_latency_table[] = {
275 {1, 0, 800, 400, 3382, 33382, 3983, 33983}, /* DDR2-400 SC */
276 {1, 0, 800, 667, 3354, 33354, 3807, 33807}, /* DDR2-667 SC */
277 {1, 0, 800, 800, 3347, 33347, 3763, 33763}, /* DDR2-800 SC */
278 {1, 1, 800, 667, 6420, 36420, 6873, 36873}, /* DDR3-667 SC */
279 {1, 1, 800, 800, 5902, 35902, 6318, 36318}, /* DDR3-800 SC */
281 {1, 0, 667, 400, 3400, 33400, 4021, 34021}, /* DDR2-400 SC */
282 {1, 0, 667, 667, 3372, 33372, 3845, 33845}, /* DDR2-667 SC */
283 {1, 0, 667, 800, 3386, 33386, 3822, 33822}, /* DDR2-800 SC */
284 {1, 1, 667, 667, 6438, 36438, 6911, 36911}, /* DDR3-667 SC */
285 {1, 1, 667, 800, 5941, 35941, 6377, 36377}, /* DDR3-800 SC */
287 {1, 0, 400, 400, 3472, 33472, 4173, 34173}, /* DDR2-400 SC */
288 {1, 0, 400, 667, 3443, 33443, 3996, 33996}, /* DDR2-667 SC */
289 {1, 0, 400, 800, 3430, 33430, 3946, 33946}, /* DDR2-800 SC */
290 {1, 1, 400, 667, 6509, 36509, 7062, 37062}, /* DDR3-667 SC */
291 {1, 1, 400, 800, 5985, 35985, 6501, 36501}, /* DDR3-800 SC */
293 {0, 0, 800, 400, 3438, 33438, 4065, 34065}, /* DDR2-400 SC */
294 {0, 0, 800, 667, 3410, 33410, 3889, 33889}, /* DDR2-667 SC */
295 {0, 0, 800, 800, 3403, 33403, 3845, 33845}, /* DDR2-800 SC */
296 {0, 1, 800, 667, 6476, 36476, 6955, 36955}, /* DDR3-667 SC */
297 {0, 1, 800, 800, 5958, 35958, 6400, 36400}, /* DDR3-800 SC */
299 {0, 0, 667, 400, 3456, 33456, 4103, 34106}, /* DDR2-400 SC */
300 {0, 0, 667, 667, 3428, 33428, 3927, 33927}, /* DDR2-667 SC */
301 {0, 0, 667, 800, 3443, 33443, 3905, 33905}, /* DDR2-800 SC */
302 {0, 1, 667, 667, 6494, 36494, 6993, 36993}, /* DDR3-667 SC */
303 {0, 1, 667, 800, 5998, 35998, 6460, 36460}, /* DDR3-800 SC */
305 {0, 0, 400, 400, 3528, 33528, 4255, 34255}, /* DDR2-400 SC */
306 {0, 0, 400, 667, 3500, 33500, 4079, 34079}, /* DDR2-667 SC */
307 {0, 0, 400, 800, 3487, 33487, 4029, 34029}, /* DDR2-800 SC */
308 {0, 1, 400, 667, 6566, 36566, 7145, 37145}, /* DDR3-667 SC */
309 {0, 1, 400, 800, 6042, 36042, 6584, 36584}, /* DDR3-800 SC */
312 static const struct cxsr_latency *intel_get_cxsr_latency(bool is_desktop,
317 const struct cxsr_latency *latency;
320 if (fsb == 0 || mem == 0)
323 for (i = 0; i < ARRAY_SIZE(cxsr_latency_table); i++) {
324 latency = &cxsr_latency_table[i];
325 if (is_desktop == latency->is_desktop &&
326 is_ddr3 == latency->is_ddr3 &&
327 fsb == latency->fsb_freq && mem == latency->mem_freq)
331 DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
336 static void chv_set_memory_dvfs(struct drm_i915_private *dev_priv, bool enable)
340 vlv_punit_get(dev_priv);
342 val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
344 val &= ~FORCE_DDR_HIGH_FREQ;
346 val |= FORCE_DDR_HIGH_FREQ;
347 val &= ~FORCE_DDR_LOW_FREQ;
348 val |= FORCE_DDR_FREQ_REQ_ACK;
349 vlv_punit_write(dev_priv, PUNIT_REG_DDR_SETUP2, val);
351 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2) &
352 FORCE_DDR_FREQ_REQ_ACK) == 0, 3))
353 drm_err(&dev_priv->drm,
354 "timed out waiting for Punit DDR DVFS request\n");
356 vlv_punit_put(dev_priv);
359 static void chv_set_memory_pm5(struct drm_i915_private *dev_priv, bool enable)
363 vlv_punit_get(dev_priv);
365 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPSSPM);
367 val |= DSP_MAXFIFO_PM5_ENABLE;
369 val &= ~DSP_MAXFIFO_PM5_ENABLE;
370 vlv_punit_write(dev_priv, PUNIT_REG_DSPSSPM, val);
372 vlv_punit_put(dev_priv);
375 #define FW_WM(value, plane) \
376 (((value) << DSPFW_ ## plane ## _SHIFT) & DSPFW_ ## plane ## _MASK)
378 static bool _intel_set_memory_cxsr(struct drm_i915_private *dev_priv, bool enable)
383 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
384 was_enabled = intel_uncore_read(&dev_priv->uncore, FW_BLC_SELF_VLV) & FW_CSPWRDWNEN;
385 intel_uncore_write(&dev_priv->uncore, FW_BLC_SELF_VLV, enable ? FW_CSPWRDWNEN : 0);
386 intel_uncore_posting_read(&dev_priv->uncore, FW_BLC_SELF_VLV);
387 } else if (IS_G4X(dev_priv) || IS_I965GM(dev_priv)) {
388 was_enabled = intel_uncore_read(&dev_priv->uncore, FW_BLC_SELF) & FW_BLC_SELF_EN;
389 intel_uncore_write(&dev_priv->uncore, FW_BLC_SELF, enable ? FW_BLC_SELF_EN : 0);
390 intel_uncore_posting_read(&dev_priv->uncore, FW_BLC_SELF);
391 } else if (IS_PINEVIEW(dev_priv)) {
392 val = intel_uncore_read(&dev_priv->uncore, DSPFW3);
393 was_enabled = val & PINEVIEW_SELF_REFRESH_EN;
395 val |= PINEVIEW_SELF_REFRESH_EN;
397 val &= ~PINEVIEW_SELF_REFRESH_EN;
398 intel_uncore_write(&dev_priv->uncore, DSPFW3, val);
399 intel_uncore_posting_read(&dev_priv->uncore, DSPFW3);
400 } else if (IS_I945G(dev_priv) || IS_I945GM(dev_priv)) {
401 was_enabled = intel_uncore_read(&dev_priv->uncore, FW_BLC_SELF) & FW_BLC_SELF_EN;
402 val = enable ? _MASKED_BIT_ENABLE(FW_BLC_SELF_EN) :
403 _MASKED_BIT_DISABLE(FW_BLC_SELF_EN);
404 intel_uncore_write(&dev_priv->uncore, FW_BLC_SELF, val);
405 intel_uncore_posting_read(&dev_priv->uncore, FW_BLC_SELF);
406 } else if (IS_I915GM(dev_priv)) {
408 * FIXME can't find a bit like this for 915G, and
409 * and yet it does have the related watermark in
410 * FW_BLC_SELF. What's going on?
412 was_enabled = intel_uncore_read(&dev_priv->uncore, INSTPM) & INSTPM_SELF_EN;
413 val = enable ? _MASKED_BIT_ENABLE(INSTPM_SELF_EN) :
414 _MASKED_BIT_DISABLE(INSTPM_SELF_EN);
415 intel_uncore_write(&dev_priv->uncore, INSTPM, val);
416 intel_uncore_posting_read(&dev_priv->uncore, INSTPM);
421 trace_intel_memory_cxsr(dev_priv, was_enabled, enable);
423 drm_dbg_kms(&dev_priv->drm, "memory self-refresh is %s (was %s)\n",
424 str_enabled_disabled(enable),
425 str_enabled_disabled(was_enabled));
431 * intel_set_memory_cxsr - Configure CxSR state
432 * @dev_priv: i915 device
433 * @enable: Allow vs. disallow CxSR
435 * Allow or disallow the system to enter a special CxSR
436 * (C-state self refresh) state. What typically happens in CxSR mode
437 * is that several display FIFOs may get combined into a single larger
438 * FIFO for a particular plane (so called max FIFO mode) to allow the
439 * system to defer memory fetches longer, and the memory will enter
442 * Note that enabling CxSR does not guarantee that the system enter
443 * this special mode, nor does it guarantee that the system stays
444 * in that mode once entered. So this just allows/disallows the system
445 * to autonomously utilize the CxSR mode. Other factors such as core
446 * C-states will affect when/if the system actually enters/exits the
449 * Note that on VLV/CHV this actually only controls the max FIFO mode,
450 * and the system is free to enter/exit memory self refresh at any time
451 * even when the use of CxSR has been disallowed.
453 * While the system is actually in the CxSR/max FIFO mode, some plane
454 * control registers will not get latched on vblank. Thus in order to
455 * guarantee the system will respond to changes in the plane registers
456 * we must always disallow CxSR prior to making changes to those registers.
457 * Unfortunately the system will re-evaluate the CxSR conditions at
458 * frame start which happens after vblank start (which is when the plane
459 * registers would get latched), so we can't proceed with the plane update
460 * during the same frame where we disallowed CxSR.
462 * Certain platforms also have a deeper HPLL SR mode. Fortunately the
463 * HPLL SR mode depends on CxSR itself, so we don't have to hand hold
464 * the hardware w.r.t. HPLL SR when writing to plane registers.
465 * Disallowing just CxSR is sufficient.
467 bool intel_set_memory_cxsr(struct drm_i915_private *dev_priv, bool enable)
471 mutex_lock(&dev_priv->display.wm.wm_mutex);
472 ret = _intel_set_memory_cxsr(dev_priv, enable);
473 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
474 dev_priv->display.wm.vlv.cxsr = enable;
475 else if (IS_G4X(dev_priv))
476 dev_priv->display.wm.g4x.cxsr = enable;
477 mutex_unlock(&dev_priv->display.wm.wm_mutex);
483 * Latency for FIFO fetches is dependent on several factors:
484 * - memory configuration (speed, channels)
486 * - current MCH state
487 * It can be fairly high in some situations, so here we assume a fairly
488 * pessimal value. It's a tradeoff between extra memory fetches (if we
489 * set this value too high, the FIFO will fetch frequently to stay full)
490 * and power consumption (set it too low to save power and we might see
491 * FIFO underruns and display "flicker").
493 * A value of 5us seems to be a good balance; safe for very low end
494 * platforms but not overly aggressive on lower latency configs.
496 static const int pessimal_latency_ns = 5000;
498 #define VLV_FIFO_START(dsparb, dsparb2, lo_shift, hi_shift) \
499 ((((dsparb) >> (lo_shift)) & 0xff) | ((((dsparb2) >> (hi_shift)) & 0x1) << 8))
501 static void vlv_get_fifo_size(struct intel_crtc_state *crtc_state)
503 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
504 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
505 struct vlv_fifo_state *fifo_state = &crtc_state->wm.vlv.fifo_state;
506 enum pipe pipe = crtc->pipe;
507 int sprite0_start, sprite1_start;
508 u32 dsparb, dsparb2, dsparb3;
512 dsparb = intel_uncore_read(&dev_priv->uncore, DSPARB);
513 dsparb2 = intel_uncore_read(&dev_priv->uncore, DSPARB2);
514 sprite0_start = VLV_FIFO_START(dsparb, dsparb2, 0, 0);
515 sprite1_start = VLV_FIFO_START(dsparb, dsparb2, 8, 4);
518 dsparb = intel_uncore_read(&dev_priv->uncore, DSPARB);
519 dsparb2 = intel_uncore_read(&dev_priv->uncore, DSPARB2);
520 sprite0_start = VLV_FIFO_START(dsparb, dsparb2, 16, 8);
521 sprite1_start = VLV_FIFO_START(dsparb, dsparb2, 24, 12);
524 dsparb2 = intel_uncore_read(&dev_priv->uncore, DSPARB2);
525 dsparb3 = intel_uncore_read(&dev_priv->uncore, DSPARB3);
526 sprite0_start = VLV_FIFO_START(dsparb3, dsparb2, 0, 16);
527 sprite1_start = VLV_FIFO_START(dsparb3, dsparb2, 8, 20);
534 fifo_state->plane[PLANE_PRIMARY] = sprite0_start;
535 fifo_state->plane[PLANE_SPRITE0] = sprite1_start - sprite0_start;
536 fifo_state->plane[PLANE_SPRITE1] = 511 - sprite1_start;
537 fifo_state->plane[PLANE_CURSOR] = 63;
540 static int i9xx_get_fifo_size(struct drm_i915_private *dev_priv,
541 enum i9xx_plane_id i9xx_plane)
543 u32 dsparb = intel_uncore_read(&dev_priv->uncore, DSPARB);
546 size = dsparb & 0x7f;
547 if (i9xx_plane == PLANE_B)
548 size = ((dsparb >> DSPARB_CSTART_SHIFT) & 0x7f) - size;
550 drm_dbg_kms(&dev_priv->drm, "FIFO size - (0x%08x) %c: %d\n",
551 dsparb, plane_name(i9xx_plane), size);
556 static int i830_get_fifo_size(struct drm_i915_private *dev_priv,
557 enum i9xx_plane_id i9xx_plane)
559 u32 dsparb = intel_uncore_read(&dev_priv->uncore, DSPARB);
562 size = dsparb & 0x1ff;
563 if (i9xx_plane == PLANE_B)
564 size = ((dsparb >> DSPARB_BEND_SHIFT) & 0x1ff) - size;
565 size >>= 1; /* Convert to cachelines */
567 drm_dbg_kms(&dev_priv->drm, "FIFO size - (0x%08x) %c: %d\n",
568 dsparb, plane_name(i9xx_plane), size);
573 static int i845_get_fifo_size(struct drm_i915_private *dev_priv,
574 enum i9xx_plane_id i9xx_plane)
576 u32 dsparb = intel_uncore_read(&dev_priv->uncore, DSPARB);
579 size = dsparb & 0x7f;
580 size >>= 2; /* Convert to cachelines */
582 drm_dbg_kms(&dev_priv->drm, "FIFO size - (0x%08x) %c: %d\n",
583 dsparb, plane_name(i9xx_plane), size);
588 /* Pineview has different values for various configs */
589 static const struct intel_watermark_params pnv_display_wm = {
590 .fifo_size = PINEVIEW_DISPLAY_FIFO,
591 .max_wm = PINEVIEW_MAX_WM,
592 .default_wm = PINEVIEW_DFT_WM,
593 .guard_size = PINEVIEW_GUARD_WM,
594 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
597 static const struct intel_watermark_params pnv_display_hplloff_wm = {
598 .fifo_size = PINEVIEW_DISPLAY_FIFO,
599 .max_wm = PINEVIEW_MAX_WM,
600 .default_wm = PINEVIEW_DFT_HPLLOFF_WM,
601 .guard_size = PINEVIEW_GUARD_WM,
602 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
605 static const struct intel_watermark_params pnv_cursor_wm = {
606 .fifo_size = PINEVIEW_CURSOR_FIFO,
607 .max_wm = PINEVIEW_CURSOR_MAX_WM,
608 .default_wm = PINEVIEW_CURSOR_DFT_WM,
609 .guard_size = PINEVIEW_CURSOR_GUARD_WM,
610 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
613 static const struct intel_watermark_params pnv_cursor_hplloff_wm = {
614 .fifo_size = PINEVIEW_CURSOR_FIFO,
615 .max_wm = PINEVIEW_CURSOR_MAX_WM,
616 .default_wm = PINEVIEW_CURSOR_DFT_WM,
617 .guard_size = PINEVIEW_CURSOR_GUARD_WM,
618 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
621 static const struct intel_watermark_params i965_cursor_wm_info = {
622 .fifo_size = I965_CURSOR_FIFO,
623 .max_wm = I965_CURSOR_MAX_WM,
624 .default_wm = I965_CURSOR_DFT_WM,
626 .cacheline_size = I915_FIFO_LINE_SIZE,
629 static const struct intel_watermark_params i945_wm_info = {
630 .fifo_size = I945_FIFO_SIZE,
631 .max_wm = I915_MAX_WM,
634 .cacheline_size = I915_FIFO_LINE_SIZE,
637 static const struct intel_watermark_params i915_wm_info = {
638 .fifo_size = I915_FIFO_SIZE,
639 .max_wm = I915_MAX_WM,
642 .cacheline_size = I915_FIFO_LINE_SIZE,
645 static const struct intel_watermark_params i830_a_wm_info = {
646 .fifo_size = I855GM_FIFO_SIZE,
647 .max_wm = I915_MAX_WM,
650 .cacheline_size = I830_FIFO_LINE_SIZE,
653 static const struct intel_watermark_params i830_bc_wm_info = {
654 .fifo_size = I855GM_FIFO_SIZE,
655 .max_wm = I915_MAX_WM/2,
658 .cacheline_size = I830_FIFO_LINE_SIZE,
661 static const struct intel_watermark_params i845_wm_info = {
662 .fifo_size = I830_FIFO_SIZE,
663 .max_wm = I915_MAX_WM,
666 .cacheline_size = I830_FIFO_LINE_SIZE,
670 * intel_wm_method1 - Method 1 / "small buffer" watermark formula
671 * @pixel_rate: Pipe pixel rate in kHz
672 * @cpp: Plane bytes per pixel
673 * @latency: Memory wakeup latency in 0.1us units
675 * Compute the watermark using the method 1 or "small buffer"
676 * formula. The caller may additonally add extra cachelines
677 * to account for TLB misses and clock crossings.
679 * This method is concerned with the short term drain rate
680 * of the FIFO, ie. it does not account for blanking periods
681 * which would effectively reduce the average drain rate across
682 * a longer period. The name "small" refers to the fact the
683 * FIFO is relatively small compared to the amount of data
686 * The FIFO level vs. time graph might look something like:
690 * __---__---__ (- plane active, _ blanking)
693 * or perhaps like this:
696 * __----__----__ (- plane active, _ blanking)
700 * The watermark in bytes
702 static unsigned int intel_wm_method1(unsigned int pixel_rate,
704 unsigned int latency)
708 ret = mul_u32_u32(pixel_rate, cpp * latency);
709 ret = DIV_ROUND_UP_ULL(ret, 10000);
715 * intel_wm_method2 - Method 2 / "large buffer" watermark formula
716 * @pixel_rate: Pipe pixel rate in kHz
717 * @htotal: Pipe horizontal total
718 * @width: Plane width in pixels
719 * @cpp: Plane bytes per pixel
720 * @latency: Memory wakeup latency in 0.1us units
722 * Compute the watermark using the method 2 or "large buffer"
723 * formula. The caller may additonally add extra cachelines
724 * to account for TLB misses and clock crossings.
726 * This method is concerned with the long term drain rate
727 * of the FIFO, ie. it does account for blanking periods
728 * which effectively reduce the average drain rate across
729 * a longer period. The name "large" refers to the fact the
730 * FIFO is relatively large compared to the amount of data
733 * The FIFO level vs. time graph might look something like:
738 * __ --__--__--__--__--__--__ (- plane active, _ blanking)
742 * The watermark in bytes
744 static unsigned int intel_wm_method2(unsigned int pixel_rate,
748 unsigned int latency)
753 * FIXME remove once all users are computing
754 * watermarks in the correct place.
756 if (WARN_ON_ONCE(htotal == 0))
759 ret = (latency * pixel_rate) / (htotal * 10000);
760 ret = (ret + 1) * width * cpp;
766 * intel_calculate_wm - calculate watermark level
767 * @pixel_rate: pixel clock
768 * @wm: chip FIFO params
769 * @fifo_size: size of the FIFO buffer
770 * @cpp: bytes per pixel
771 * @latency_ns: memory latency for the platform
773 * Calculate the watermark level (the level at which the display plane will
774 * start fetching from memory again). Each chip has a different display
775 * FIFO size and allocation, so the caller needs to figure that out and pass
776 * in the correct intel_watermark_params structure.
778 * As the pixel clock runs, the FIFO will be drained at a rate that depends
779 * on the pixel size. When it reaches the watermark level, it'll start
780 * fetching FIFO line sized based chunks from memory until the FIFO fills
781 * past the watermark point. If the FIFO drains completely, a FIFO underrun
782 * will occur, and a display engine hang could result.
784 static unsigned int intel_calculate_wm(int pixel_rate,
785 const struct intel_watermark_params *wm,
786 int fifo_size, int cpp,
787 unsigned int latency_ns)
789 int entries, wm_size;
792 * Note: we need to make sure we don't overflow for various clock &
794 * clocks go from a few thousand to several hundred thousand.
795 * latency is usually a few thousand
797 entries = intel_wm_method1(pixel_rate, cpp,
799 entries = DIV_ROUND_UP(entries, wm->cacheline_size) +
801 DRM_DEBUG_KMS("FIFO entries required for mode: %d\n", entries);
803 wm_size = fifo_size - entries;
804 DRM_DEBUG_KMS("FIFO watermark level: %d\n", wm_size);
806 /* Don't promote wm_size to unsigned... */
807 if (wm_size > wm->max_wm)
808 wm_size = wm->max_wm;
810 wm_size = wm->default_wm;
813 * Bspec seems to indicate that the value shouldn't be lower than
814 * 'burst size + 1'. Certainly 830 is quite unhappy with low values.
815 * Lets go for 8 which is the burst size since certain platforms
816 * already use a hardcoded 8 (which is what the spec says should be
825 static bool is_disabling(int old, int new, int threshold)
827 return old >= threshold && new < threshold;
830 static bool is_enabling(int old, int new, int threshold)
832 return old < threshold && new >= threshold;
835 static int intel_wm_num_levels(struct drm_i915_private *dev_priv)
837 return dev_priv->display.wm.max_level + 1;
840 static bool intel_wm_plane_visible(const struct intel_crtc_state *crtc_state,
841 const struct intel_plane_state *plane_state)
843 struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
845 /* FIXME check the 'enable' instead */
846 if (!crtc_state->hw.active)
850 * Treat cursor with fb as always visible since cursor updates
851 * can happen faster than the vrefresh rate, and the current
852 * watermark code doesn't handle that correctly. Cursor updates
853 * which set/clear the fb or change the cursor size are going
854 * to get throttled by intel_legacy_cursor_update() to work
855 * around this problem with the watermark code.
857 if (plane->id == PLANE_CURSOR)
858 return plane_state->hw.fb != NULL;
860 return plane_state->uapi.visible;
863 static bool intel_crtc_active(struct intel_crtc *crtc)
865 /* Be paranoid as we can arrive here with only partial
866 * state retrieved from the hardware during setup.
868 * We can ditch the adjusted_mode.crtc_clock check as soon
869 * as Haswell has gained clock readout/fastboot support.
871 * We can ditch the crtc->primary->state->fb check as soon as we can
872 * properly reconstruct framebuffers.
874 * FIXME: The intel_crtc->active here should be switched to
875 * crtc->state->active once we have proper CRTC states wired up
878 return crtc && crtc->active && crtc->base.primary->state->fb &&
879 crtc->config->hw.adjusted_mode.crtc_clock;
882 static struct intel_crtc *single_enabled_crtc(struct drm_i915_private *dev_priv)
884 struct intel_crtc *crtc, *enabled = NULL;
886 for_each_intel_crtc(&dev_priv->drm, crtc) {
887 if (intel_crtc_active(crtc)) {
897 static void pnv_update_wm(struct drm_i915_private *dev_priv)
899 struct intel_crtc *crtc;
900 const struct cxsr_latency *latency;
904 latency = intel_get_cxsr_latency(!IS_MOBILE(dev_priv),
909 drm_dbg_kms(&dev_priv->drm,
910 "Unknown FSB/MEM found, disable CxSR\n");
911 intel_set_memory_cxsr(dev_priv, false);
915 crtc = single_enabled_crtc(dev_priv);
917 const struct drm_framebuffer *fb =
918 crtc->base.primary->state->fb;
919 int pixel_rate = crtc->config->pixel_rate;
920 int cpp = fb->format->cpp[0];
923 wm = intel_calculate_wm(pixel_rate, &pnv_display_wm,
924 pnv_display_wm.fifo_size,
925 cpp, latency->display_sr);
926 reg = intel_uncore_read(&dev_priv->uncore, DSPFW1);
927 reg &= ~DSPFW_SR_MASK;
928 reg |= FW_WM(wm, SR);
929 intel_uncore_write(&dev_priv->uncore, DSPFW1, reg);
930 drm_dbg_kms(&dev_priv->drm, "DSPFW1 register is %x\n", reg);
933 wm = intel_calculate_wm(pixel_rate, &pnv_cursor_wm,
934 pnv_display_wm.fifo_size,
935 4, latency->cursor_sr);
936 reg = intel_uncore_read(&dev_priv->uncore, DSPFW3);
937 reg &= ~DSPFW_CURSOR_SR_MASK;
938 reg |= FW_WM(wm, CURSOR_SR);
939 intel_uncore_write(&dev_priv->uncore, DSPFW3, reg);
941 /* Display HPLL off SR */
942 wm = intel_calculate_wm(pixel_rate, &pnv_display_hplloff_wm,
943 pnv_display_hplloff_wm.fifo_size,
944 cpp, latency->display_hpll_disable);
945 reg = intel_uncore_read(&dev_priv->uncore, DSPFW3);
946 reg &= ~DSPFW_HPLL_SR_MASK;
947 reg |= FW_WM(wm, HPLL_SR);
948 intel_uncore_write(&dev_priv->uncore, DSPFW3, reg);
950 /* cursor HPLL off SR */
951 wm = intel_calculate_wm(pixel_rate, &pnv_cursor_hplloff_wm,
952 pnv_display_hplloff_wm.fifo_size,
953 4, latency->cursor_hpll_disable);
954 reg = intel_uncore_read(&dev_priv->uncore, DSPFW3);
955 reg &= ~DSPFW_HPLL_CURSOR_MASK;
956 reg |= FW_WM(wm, HPLL_CURSOR);
957 intel_uncore_write(&dev_priv->uncore, DSPFW3, reg);
958 drm_dbg_kms(&dev_priv->drm, "DSPFW3 register is %x\n", reg);
960 intel_set_memory_cxsr(dev_priv, true);
962 intel_set_memory_cxsr(dev_priv, false);
967 * Documentation says:
968 * "If the line size is small, the TLB fetches can get in the way of the
969 * data fetches, causing some lag in the pixel data return which is not
970 * accounted for in the above formulas. The following adjustment only
971 * needs to be applied if eight whole lines fit in the buffer at once.
972 * The WM is adjusted upwards by the difference between the FIFO size
973 * and the size of 8 whole lines. This adjustment is always performed
974 * in the actual pixel depth regardless of whether FBC is enabled or not."
976 static unsigned int g4x_tlb_miss_wa(int fifo_size, int width, int cpp)
978 int tlb_miss = fifo_size * 64 - width * cpp * 8;
980 return max(0, tlb_miss);
983 static void g4x_write_wm_values(struct drm_i915_private *dev_priv,
984 const struct g4x_wm_values *wm)
988 for_each_pipe(dev_priv, pipe)
989 trace_g4x_wm(intel_crtc_for_pipe(dev_priv, pipe), wm);
991 intel_uncore_write(&dev_priv->uncore, DSPFW1,
992 FW_WM(wm->sr.plane, SR) |
993 FW_WM(wm->pipe[PIPE_B].plane[PLANE_CURSOR], CURSORB) |
994 FW_WM(wm->pipe[PIPE_B].plane[PLANE_PRIMARY], PLANEB) |
995 FW_WM(wm->pipe[PIPE_A].plane[PLANE_PRIMARY], PLANEA));
996 intel_uncore_write(&dev_priv->uncore, DSPFW2,
997 (wm->fbc_en ? DSPFW_FBC_SR_EN : 0) |
998 FW_WM(wm->sr.fbc, FBC_SR) |
999 FW_WM(wm->hpll.fbc, FBC_HPLL_SR) |
1000 FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE0], SPRITEB) |
1001 FW_WM(wm->pipe[PIPE_A].plane[PLANE_CURSOR], CURSORA) |
1002 FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE0], SPRITEA));
1003 intel_uncore_write(&dev_priv->uncore, DSPFW3,
1004 (wm->hpll_en ? DSPFW_HPLL_SR_EN : 0) |
1005 FW_WM(wm->sr.cursor, CURSOR_SR) |
1006 FW_WM(wm->hpll.cursor, HPLL_CURSOR) |
1007 FW_WM(wm->hpll.plane, HPLL_SR));
1009 intel_uncore_posting_read(&dev_priv->uncore, DSPFW1);
1012 #define FW_WM_VLV(value, plane) \
1013 (((value) << DSPFW_ ## plane ## _SHIFT) & DSPFW_ ## plane ## _MASK_VLV)
1015 static void vlv_write_wm_values(struct drm_i915_private *dev_priv,
1016 const struct vlv_wm_values *wm)
1020 for_each_pipe(dev_priv, pipe) {
1021 trace_vlv_wm(intel_crtc_for_pipe(dev_priv, pipe), wm);
1023 intel_uncore_write(&dev_priv->uncore, VLV_DDL(pipe),
1024 (wm->ddl[pipe].plane[PLANE_CURSOR] << DDL_CURSOR_SHIFT) |
1025 (wm->ddl[pipe].plane[PLANE_SPRITE1] << DDL_SPRITE_SHIFT(1)) |
1026 (wm->ddl[pipe].plane[PLANE_SPRITE0] << DDL_SPRITE_SHIFT(0)) |
1027 (wm->ddl[pipe].plane[PLANE_PRIMARY] << DDL_PLANE_SHIFT));
1031 * Zero the (unused) WM1 watermarks, and also clear all the
1032 * high order bits so that there are no out of bounds values
1033 * present in the registers during the reprogramming.
1035 intel_uncore_write(&dev_priv->uncore, DSPHOWM, 0);
1036 intel_uncore_write(&dev_priv->uncore, DSPHOWM1, 0);
1037 intel_uncore_write(&dev_priv->uncore, DSPFW4, 0);
1038 intel_uncore_write(&dev_priv->uncore, DSPFW5, 0);
1039 intel_uncore_write(&dev_priv->uncore, DSPFW6, 0);
1041 intel_uncore_write(&dev_priv->uncore, DSPFW1,
1042 FW_WM(wm->sr.plane, SR) |
1043 FW_WM(wm->pipe[PIPE_B].plane[PLANE_CURSOR], CURSORB) |
1044 FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_PRIMARY], PLANEB) |
1045 FW_WM_VLV(wm->pipe[PIPE_A].plane[PLANE_PRIMARY], PLANEA));
1046 intel_uncore_write(&dev_priv->uncore, DSPFW2,
1047 FW_WM_VLV(wm->pipe[PIPE_A].plane[PLANE_SPRITE1], SPRITEB) |
1048 FW_WM(wm->pipe[PIPE_A].plane[PLANE_CURSOR], CURSORA) |
1049 FW_WM_VLV(wm->pipe[PIPE_A].plane[PLANE_SPRITE0], SPRITEA));
1050 intel_uncore_write(&dev_priv->uncore, DSPFW3,
1051 FW_WM(wm->sr.cursor, CURSOR_SR));
1053 if (IS_CHERRYVIEW(dev_priv)) {
1054 intel_uncore_write(&dev_priv->uncore, DSPFW7_CHV,
1055 FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_SPRITE1], SPRITED) |
1056 FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_SPRITE0], SPRITEC));
1057 intel_uncore_write(&dev_priv->uncore, DSPFW8_CHV,
1058 FW_WM_VLV(wm->pipe[PIPE_C].plane[PLANE_SPRITE1], SPRITEF) |
1059 FW_WM_VLV(wm->pipe[PIPE_C].plane[PLANE_SPRITE0], SPRITEE));
1060 intel_uncore_write(&dev_priv->uncore, DSPFW9_CHV,
1061 FW_WM_VLV(wm->pipe[PIPE_C].plane[PLANE_PRIMARY], PLANEC) |
1062 FW_WM(wm->pipe[PIPE_C].plane[PLANE_CURSOR], CURSORC));
1063 intel_uncore_write(&dev_priv->uncore, DSPHOWM,
1064 FW_WM(wm->sr.plane >> 9, SR_HI) |
1065 FW_WM(wm->pipe[PIPE_C].plane[PLANE_SPRITE1] >> 8, SPRITEF_HI) |
1066 FW_WM(wm->pipe[PIPE_C].plane[PLANE_SPRITE0] >> 8, SPRITEE_HI) |
1067 FW_WM(wm->pipe[PIPE_C].plane[PLANE_PRIMARY] >> 8, PLANEC_HI) |
1068 FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE1] >> 8, SPRITED_HI) |
1069 FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE0] >> 8, SPRITEC_HI) |
1070 FW_WM(wm->pipe[PIPE_B].plane[PLANE_PRIMARY] >> 8, PLANEB_HI) |
1071 FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE1] >> 8, SPRITEB_HI) |
1072 FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE0] >> 8, SPRITEA_HI) |
1073 FW_WM(wm->pipe[PIPE_A].plane[PLANE_PRIMARY] >> 8, PLANEA_HI));
1075 intel_uncore_write(&dev_priv->uncore, DSPFW7,
1076 FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_SPRITE1], SPRITED) |
1077 FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_SPRITE0], SPRITEC));
1078 intel_uncore_write(&dev_priv->uncore, DSPHOWM,
1079 FW_WM(wm->sr.plane >> 9, SR_HI) |
1080 FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE1] >> 8, SPRITED_HI) |
1081 FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE0] >> 8, SPRITEC_HI) |
1082 FW_WM(wm->pipe[PIPE_B].plane[PLANE_PRIMARY] >> 8, PLANEB_HI) |
1083 FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE1] >> 8, SPRITEB_HI) |
1084 FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE0] >> 8, SPRITEA_HI) |
1085 FW_WM(wm->pipe[PIPE_A].plane[PLANE_PRIMARY] >> 8, PLANEA_HI));
1088 intel_uncore_posting_read(&dev_priv->uncore, DSPFW1);
1093 static void g4x_setup_wm_latency(struct drm_i915_private *dev_priv)
1095 /* all latencies in usec */
1096 dev_priv->display.wm.pri_latency[G4X_WM_LEVEL_NORMAL] = 5;
1097 dev_priv->display.wm.pri_latency[G4X_WM_LEVEL_SR] = 12;
1098 dev_priv->display.wm.pri_latency[G4X_WM_LEVEL_HPLL] = 35;
1100 dev_priv->display.wm.max_level = G4X_WM_LEVEL_HPLL;
1103 static int g4x_plane_fifo_size(enum plane_id plane_id, int level)
1106 * DSPCNTR[13] supposedly controls whether the
1107 * primary plane can use the FIFO space otherwise
1108 * reserved for the sprite plane. It's not 100% clear
1109 * what the actual FIFO size is, but it looks like we
1110 * can happily set both primary and sprite watermarks
1111 * up to 127 cachelines. So that would seem to mean
1112 * that either DSPCNTR[13] doesn't do anything, or that
1113 * the total FIFO is >= 256 cachelines in size. Either
1114 * way, we don't seem to have to worry about this
1115 * repartitioning as the maximum watermark value the
1116 * register can hold for each plane is lower than the
1117 * minimum FIFO size.
1123 return level == G4X_WM_LEVEL_NORMAL ? 127 : 511;
1125 return level == G4X_WM_LEVEL_NORMAL ? 127 : 0;
1127 MISSING_CASE(plane_id);
1132 static int g4x_fbc_fifo_size(int level)
1135 case G4X_WM_LEVEL_SR:
1137 case G4X_WM_LEVEL_HPLL:
1140 MISSING_CASE(level);
1145 static u16 g4x_compute_wm(const struct intel_crtc_state *crtc_state,
1146 const struct intel_plane_state *plane_state,
1149 struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
1150 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
1151 const struct drm_display_mode *pipe_mode =
1152 &crtc_state->hw.pipe_mode;
1153 unsigned int latency = dev_priv->display.wm.pri_latency[level] * 10;
1154 unsigned int pixel_rate, htotal, cpp, width, wm;
1159 if (!intel_wm_plane_visible(crtc_state, plane_state))
1162 cpp = plane_state->hw.fb->format->cpp[0];
1165 * WaUse32BppForSRWM:ctg,elk
1167 * The spec fails to list this restriction for the
1168 * HPLL watermark, which seems a little strange.
1169 * Let's use 32bpp for the HPLL watermark as well.
1171 if (plane->id == PLANE_PRIMARY &&
1172 level != G4X_WM_LEVEL_NORMAL)
1175 pixel_rate = crtc_state->pixel_rate;
1176 htotal = pipe_mode->crtc_htotal;
1177 width = drm_rect_width(&plane_state->uapi.src) >> 16;
1179 if (plane->id == PLANE_CURSOR) {
1180 wm = intel_wm_method2(pixel_rate, htotal, width, cpp, latency);
1181 } else if (plane->id == PLANE_PRIMARY &&
1182 level == G4X_WM_LEVEL_NORMAL) {
1183 wm = intel_wm_method1(pixel_rate, cpp, latency);
1185 unsigned int small, large;
1187 small = intel_wm_method1(pixel_rate, cpp, latency);
1188 large = intel_wm_method2(pixel_rate, htotal, width, cpp, latency);
1190 wm = min(small, large);
1193 wm += g4x_tlb_miss_wa(g4x_plane_fifo_size(plane->id, level),
1196 wm = DIV_ROUND_UP(wm, 64) + 2;
1198 return min_t(unsigned int, wm, USHRT_MAX);
1201 static bool g4x_raw_plane_wm_set(struct intel_crtc_state *crtc_state,
1202 int level, enum plane_id plane_id, u16 value)
1204 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
1207 for (; level < intel_wm_num_levels(dev_priv); level++) {
1208 struct g4x_pipe_wm *raw = &crtc_state->wm.g4x.raw[level];
1210 dirty |= raw->plane[plane_id] != value;
1211 raw->plane[plane_id] = value;
1217 static bool g4x_raw_fbc_wm_set(struct intel_crtc_state *crtc_state,
1218 int level, u16 value)
1220 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
1223 /* NORMAL level doesn't have an FBC watermark */
1224 level = max(level, G4X_WM_LEVEL_SR);
1226 for (; level < intel_wm_num_levels(dev_priv); level++) {
1227 struct g4x_pipe_wm *raw = &crtc_state->wm.g4x.raw[level];
1229 dirty |= raw->fbc != value;
1236 static u32 ilk_compute_fbc_wm(const struct intel_crtc_state *crtc_state,
1237 const struct intel_plane_state *plane_state,
1240 static bool g4x_raw_plane_wm_compute(struct intel_crtc_state *crtc_state,
1241 const struct intel_plane_state *plane_state)
1243 struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
1244 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
1245 int num_levels = intel_wm_num_levels(to_i915(plane->base.dev));
1246 enum plane_id plane_id = plane->id;
1250 if (!intel_wm_plane_visible(crtc_state, plane_state)) {
1251 dirty |= g4x_raw_plane_wm_set(crtc_state, 0, plane_id, 0);
1252 if (plane_id == PLANE_PRIMARY)
1253 dirty |= g4x_raw_fbc_wm_set(crtc_state, 0, 0);
1257 for (level = 0; level < num_levels; level++) {
1258 struct g4x_pipe_wm *raw = &crtc_state->wm.g4x.raw[level];
1261 wm = g4x_compute_wm(crtc_state, plane_state, level);
1262 max_wm = g4x_plane_fifo_size(plane_id, level);
1267 dirty |= raw->plane[plane_id] != wm;
1268 raw->plane[plane_id] = wm;
1270 if (plane_id != PLANE_PRIMARY ||
1271 level == G4X_WM_LEVEL_NORMAL)
1274 wm = ilk_compute_fbc_wm(crtc_state, plane_state,
1275 raw->plane[plane_id]);
1276 max_wm = g4x_fbc_fifo_size(level);
1279 * FBC wm is not mandatory as we
1280 * can always just disable its use.
1285 dirty |= raw->fbc != wm;
1289 /* mark watermarks as invalid */
1290 dirty |= g4x_raw_plane_wm_set(crtc_state, level, plane_id, USHRT_MAX);
1292 if (plane_id == PLANE_PRIMARY)
1293 dirty |= g4x_raw_fbc_wm_set(crtc_state, level, USHRT_MAX);
1297 drm_dbg_kms(&dev_priv->drm,
1298 "%s watermarks: normal=%d, SR=%d, HPLL=%d\n",
1300 crtc_state->wm.g4x.raw[G4X_WM_LEVEL_NORMAL].plane[plane_id],
1301 crtc_state->wm.g4x.raw[G4X_WM_LEVEL_SR].plane[plane_id],
1302 crtc_state->wm.g4x.raw[G4X_WM_LEVEL_HPLL].plane[plane_id]);
1304 if (plane_id == PLANE_PRIMARY)
1305 drm_dbg_kms(&dev_priv->drm,
1306 "FBC watermarks: SR=%d, HPLL=%d\n",
1307 crtc_state->wm.g4x.raw[G4X_WM_LEVEL_SR].fbc,
1308 crtc_state->wm.g4x.raw[G4X_WM_LEVEL_HPLL].fbc);
1314 static bool g4x_raw_plane_wm_is_valid(const struct intel_crtc_state *crtc_state,
1315 enum plane_id plane_id, int level)
1317 const struct g4x_pipe_wm *raw = &crtc_state->wm.g4x.raw[level];
1319 return raw->plane[plane_id] <= g4x_plane_fifo_size(plane_id, level);
1322 static bool g4x_raw_crtc_wm_is_valid(const struct intel_crtc_state *crtc_state,
1325 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
1327 if (level > dev_priv->display.wm.max_level)
1330 return g4x_raw_plane_wm_is_valid(crtc_state, PLANE_PRIMARY, level) &&
1331 g4x_raw_plane_wm_is_valid(crtc_state, PLANE_SPRITE0, level) &&
1332 g4x_raw_plane_wm_is_valid(crtc_state, PLANE_CURSOR, level);
1335 /* mark all levels starting from 'level' as invalid */
1336 static void g4x_invalidate_wms(struct intel_crtc *crtc,
1337 struct g4x_wm_state *wm_state, int level)
1339 if (level <= G4X_WM_LEVEL_NORMAL) {
1340 enum plane_id plane_id;
1342 for_each_plane_id_on_crtc(crtc, plane_id)
1343 wm_state->wm.plane[plane_id] = USHRT_MAX;
1346 if (level <= G4X_WM_LEVEL_SR) {
1347 wm_state->cxsr = false;
1348 wm_state->sr.cursor = USHRT_MAX;
1349 wm_state->sr.plane = USHRT_MAX;
1350 wm_state->sr.fbc = USHRT_MAX;
1353 if (level <= G4X_WM_LEVEL_HPLL) {
1354 wm_state->hpll_en = false;
1355 wm_state->hpll.cursor = USHRT_MAX;
1356 wm_state->hpll.plane = USHRT_MAX;
1357 wm_state->hpll.fbc = USHRT_MAX;
1361 static bool g4x_compute_fbc_en(const struct g4x_wm_state *wm_state,
1364 if (level < G4X_WM_LEVEL_SR)
1367 if (level >= G4X_WM_LEVEL_SR &&
1368 wm_state->sr.fbc > g4x_fbc_fifo_size(G4X_WM_LEVEL_SR))
1371 if (level >= G4X_WM_LEVEL_HPLL &&
1372 wm_state->hpll.fbc > g4x_fbc_fifo_size(G4X_WM_LEVEL_HPLL))
1378 static int g4x_compute_pipe_wm(struct intel_atomic_state *state,
1379 struct intel_crtc *crtc)
1381 struct intel_crtc_state *crtc_state =
1382 intel_atomic_get_new_crtc_state(state, crtc);
1383 struct g4x_wm_state *wm_state = &crtc_state->wm.g4x.optimal;
1384 u8 active_planes = crtc_state->active_planes & ~BIT(PLANE_CURSOR);
1385 const struct g4x_pipe_wm *raw;
1386 const struct intel_plane_state *old_plane_state;
1387 const struct intel_plane_state *new_plane_state;
1388 struct intel_plane *plane;
1389 enum plane_id plane_id;
1391 unsigned int dirty = 0;
1393 for_each_oldnew_intel_plane_in_state(state, plane,
1395 new_plane_state, i) {
1396 if (new_plane_state->hw.crtc != &crtc->base &&
1397 old_plane_state->hw.crtc != &crtc->base)
1400 if (g4x_raw_plane_wm_compute(crtc_state, new_plane_state))
1401 dirty |= BIT(plane->id);
1407 level = G4X_WM_LEVEL_NORMAL;
1408 if (!g4x_raw_crtc_wm_is_valid(crtc_state, level))
1411 raw = &crtc_state->wm.g4x.raw[level];
1412 for_each_plane_id_on_crtc(crtc, plane_id)
1413 wm_state->wm.plane[plane_id] = raw->plane[plane_id];
1415 level = G4X_WM_LEVEL_SR;
1416 if (!g4x_raw_crtc_wm_is_valid(crtc_state, level))
1419 raw = &crtc_state->wm.g4x.raw[level];
1420 wm_state->sr.plane = raw->plane[PLANE_PRIMARY];
1421 wm_state->sr.cursor = raw->plane[PLANE_CURSOR];
1422 wm_state->sr.fbc = raw->fbc;
1424 wm_state->cxsr = active_planes == BIT(PLANE_PRIMARY);
1426 level = G4X_WM_LEVEL_HPLL;
1427 if (!g4x_raw_crtc_wm_is_valid(crtc_state, level))
1430 raw = &crtc_state->wm.g4x.raw[level];
1431 wm_state->hpll.plane = raw->plane[PLANE_PRIMARY];
1432 wm_state->hpll.cursor = raw->plane[PLANE_CURSOR];
1433 wm_state->hpll.fbc = raw->fbc;
1435 wm_state->hpll_en = wm_state->cxsr;
1440 if (level == G4X_WM_LEVEL_NORMAL)
1443 /* invalidate the higher levels */
1444 g4x_invalidate_wms(crtc, wm_state, level);
1447 * Determine if the FBC watermark(s) can be used. IF
1448 * this isn't the case we prefer to disable the FBC
1449 * watermark(s) rather than disable the SR/HPLL
1450 * level(s) entirely. 'level-1' is the highest valid
1453 wm_state->fbc_en = g4x_compute_fbc_en(wm_state, level - 1);
1458 static int g4x_compute_intermediate_wm(struct intel_atomic_state *state,
1459 struct intel_crtc *crtc)
1461 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1462 struct intel_crtc_state *new_crtc_state =
1463 intel_atomic_get_new_crtc_state(state, crtc);
1464 const struct intel_crtc_state *old_crtc_state =
1465 intel_atomic_get_old_crtc_state(state, crtc);
1466 struct g4x_wm_state *intermediate = &new_crtc_state->wm.g4x.intermediate;
1467 const struct g4x_wm_state *optimal = &new_crtc_state->wm.g4x.optimal;
1468 const struct g4x_wm_state *active = &old_crtc_state->wm.g4x.optimal;
1469 enum plane_id plane_id;
1471 if (!new_crtc_state->hw.active ||
1472 drm_atomic_crtc_needs_modeset(&new_crtc_state->uapi)) {
1473 *intermediate = *optimal;
1475 intermediate->cxsr = false;
1476 intermediate->hpll_en = false;
1480 intermediate->cxsr = optimal->cxsr && active->cxsr &&
1481 !new_crtc_state->disable_cxsr;
1482 intermediate->hpll_en = optimal->hpll_en && active->hpll_en &&
1483 !new_crtc_state->disable_cxsr;
1484 intermediate->fbc_en = optimal->fbc_en && active->fbc_en;
1486 for_each_plane_id_on_crtc(crtc, plane_id) {
1487 intermediate->wm.plane[plane_id] =
1488 max(optimal->wm.plane[plane_id],
1489 active->wm.plane[plane_id]);
1491 drm_WARN_ON(&dev_priv->drm, intermediate->wm.plane[plane_id] >
1492 g4x_plane_fifo_size(plane_id, G4X_WM_LEVEL_NORMAL));
1495 intermediate->sr.plane = max(optimal->sr.plane,
1497 intermediate->sr.cursor = max(optimal->sr.cursor,
1499 intermediate->sr.fbc = max(optimal->sr.fbc,
1502 intermediate->hpll.plane = max(optimal->hpll.plane,
1503 active->hpll.plane);
1504 intermediate->hpll.cursor = max(optimal->hpll.cursor,
1505 active->hpll.cursor);
1506 intermediate->hpll.fbc = max(optimal->hpll.fbc,
1509 drm_WARN_ON(&dev_priv->drm,
1510 (intermediate->sr.plane >
1511 g4x_plane_fifo_size(PLANE_PRIMARY, G4X_WM_LEVEL_SR) ||
1512 intermediate->sr.cursor >
1513 g4x_plane_fifo_size(PLANE_CURSOR, G4X_WM_LEVEL_SR)) &&
1514 intermediate->cxsr);
1515 drm_WARN_ON(&dev_priv->drm,
1516 (intermediate->sr.plane >
1517 g4x_plane_fifo_size(PLANE_PRIMARY, G4X_WM_LEVEL_HPLL) ||
1518 intermediate->sr.cursor >
1519 g4x_plane_fifo_size(PLANE_CURSOR, G4X_WM_LEVEL_HPLL)) &&
1520 intermediate->hpll_en);
1522 drm_WARN_ON(&dev_priv->drm,
1523 intermediate->sr.fbc > g4x_fbc_fifo_size(1) &&
1524 intermediate->fbc_en && intermediate->cxsr);
1525 drm_WARN_ON(&dev_priv->drm,
1526 intermediate->hpll.fbc > g4x_fbc_fifo_size(2) &&
1527 intermediate->fbc_en && intermediate->hpll_en);
1531 * If our intermediate WM are identical to the final WM, then we can
1532 * omit the post-vblank programming; only update if it's different.
1534 if (memcmp(intermediate, optimal, sizeof(*intermediate)) != 0)
1535 new_crtc_state->wm.need_postvbl_update = true;
1540 static void g4x_merge_wm(struct drm_i915_private *dev_priv,
1541 struct g4x_wm_values *wm)
1543 struct intel_crtc *crtc;
1544 int num_active_pipes = 0;
1550 for_each_intel_crtc(&dev_priv->drm, crtc) {
1551 const struct g4x_wm_state *wm_state = &crtc->wm.active.g4x;
1556 if (!wm_state->cxsr)
1558 if (!wm_state->hpll_en)
1559 wm->hpll_en = false;
1560 if (!wm_state->fbc_en)
1566 if (num_active_pipes != 1) {
1568 wm->hpll_en = false;
1572 for_each_intel_crtc(&dev_priv->drm, crtc) {
1573 const struct g4x_wm_state *wm_state = &crtc->wm.active.g4x;
1574 enum pipe pipe = crtc->pipe;
1576 wm->pipe[pipe] = wm_state->wm;
1577 if (crtc->active && wm->cxsr)
1578 wm->sr = wm_state->sr;
1579 if (crtc->active && wm->hpll_en)
1580 wm->hpll = wm_state->hpll;
1584 static void g4x_program_watermarks(struct drm_i915_private *dev_priv)
1586 struct g4x_wm_values *old_wm = &dev_priv->display.wm.g4x;
1587 struct g4x_wm_values new_wm = {};
1589 g4x_merge_wm(dev_priv, &new_wm);
1591 if (memcmp(old_wm, &new_wm, sizeof(new_wm)) == 0)
1594 if (is_disabling(old_wm->cxsr, new_wm.cxsr, true))
1595 _intel_set_memory_cxsr(dev_priv, false);
1597 g4x_write_wm_values(dev_priv, &new_wm);
1599 if (is_enabling(old_wm->cxsr, new_wm.cxsr, true))
1600 _intel_set_memory_cxsr(dev_priv, true);
1605 static void g4x_initial_watermarks(struct intel_atomic_state *state,
1606 struct intel_crtc *crtc)
1608 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1609 const struct intel_crtc_state *crtc_state =
1610 intel_atomic_get_new_crtc_state(state, crtc);
1612 mutex_lock(&dev_priv->display.wm.wm_mutex);
1613 crtc->wm.active.g4x = crtc_state->wm.g4x.intermediate;
1614 g4x_program_watermarks(dev_priv);
1615 mutex_unlock(&dev_priv->display.wm.wm_mutex);
1618 static void g4x_optimize_watermarks(struct intel_atomic_state *state,
1619 struct intel_crtc *crtc)
1621 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1622 const struct intel_crtc_state *crtc_state =
1623 intel_atomic_get_new_crtc_state(state, crtc);
1625 if (!crtc_state->wm.need_postvbl_update)
1628 mutex_lock(&dev_priv->display.wm.wm_mutex);
1629 crtc->wm.active.g4x = crtc_state->wm.g4x.optimal;
1630 g4x_program_watermarks(dev_priv);
1631 mutex_unlock(&dev_priv->display.wm.wm_mutex);
1634 /* latency must be in 0.1us units. */
1635 static unsigned int vlv_wm_method2(unsigned int pixel_rate,
1636 unsigned int htotal,
1639 unsigned int latency)
1643 ret = intel_wm_method2(pixel_rate, htotal,
1644 width, cpp, latency);
1645 ret = DIV_ROUND_UP(ret, 64);
1650 static void vlv_setup_wm_latency(struct drm_i915_private *dev_priv)
1652 /* all latencies in usec */
1653 dev_priv->display.wm.pri_latency[VLV_WM_LEVEL_PM2] = 3;
1655 dev_priv->display.wm.max_level = VLV_WM_LEVEL_PM2;
1657 if (IS_CHERRYVIEW(dev_priv)) {
1658 dev_priv->display.wm.pri_latency[VLV_WM_LEVEL_PM5] = 12;
1659 dev_priv->display.wm.pri_latency[VLV_WM_LEVEL_DDR_DVFS] = 33;
1661 dev_priv->display.wm.max_level = VLV_WM_LEVEL_DDR_DVFS;
1665 static u16 vlv_compute_wm_level(const struct intel_crtc_state *crtc_state,
1666 const struct intel_plane_state *plane_state,
1669 struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
1670 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
1671 const struct drm_display_mode *pipe_mode =
1672 &crtc_state->hw.pipe_mode;
1673 unsigned int pixel_rate, htotal, cpp, width, wm;
1675 if (dev_priv->display.wm.pri_latency[level] == 0)
1678 if (!intel_wm_plane_visible(crtc_state, plane_state))
1681 cpp = plane_state->hw.fb->format->cpp[0];
1682 pixel_rate = crtc_state->pixel_rate;
1683 htotal = pipe_mode->crtc_htotal;
1684 width = drm_rect_width(&plane_state->uapi.src) >> 16;
1686 if (plane->id == PLANE_CURSOR) {
1688 * FIXME the formula gives values that are
1689 * too big for the cursor FIFO, and hence we
1690 * would never be able to use cursors. For
1691 * now just hardcode the watermark.
1695 wm = vlv_wm_method2(pixel_rate, htotal, width, cpp,
1696 dev_priv->display.wm.pri_latency[level] * 10);
1699 return min_t(unsigned int, wm, USHRT_MAX);
1702 static bool vlv_need_sprite0_fifo_workaround(unsigned int active_planes)
1704 return (active_planes & (BIT(PLANE_SPRITE0) |
1705 BIT(PLANE_SPRITE1))) == BIT(PLANE_SPRITE1);
1708 static int vlv_compute_fifo(struct intel_crtc_state *crtc_state)
1710 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
1711 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1712 const struct g4x_pipe_wm *raw =
1713 &crtc_state->wm.vlv.raw[VLV_WM_LEVEL_PM2];
1714 struct vlv_fifo_state *fifo_state = &crtc_state->wm.vlv.fifo_state;
1715 u8 active_planes = crtc_state->active_planes & ~BIT(PLANE_CURSOR);
1716 int num_active_planes = hweight8(active_planes);
1717 const int fifo_size = 511;
1718 int fifo_extra, fifo_left = fifo_size;
1719 int sprite0_fifo_extra = 0;
1720 unsigned int total_rate;
1721 enum plane_id plane_id;
1724 * When enabling sprite0 after sprite1 has already been enabled
1725 * we tend to get an underrun unless sprite0 already has some
1726 * FIFO space allcoated. Hence we always allocate at least one
1727 * cacheline for sprite0 whenever sprite1 is enabled.
1729 * All other plane enable sequences appear immune to this problem.
1731 if (vlv_need_sprite0_fifo_workaround(active_planes))
1732 sprite0_fifo_extra = 1;
1734 total_rate = raw->plane[PLANE_PRIMARY] +
1735 raw->plane[PLANE_SPRITE0] +
1736 raw->plane[PLANE_SPRITE1] +
1739 if (total_rate > fifo_size)
1742 if (total_rate == 0)
1745 for_each_plane_id_on_crtc(crtc, plane_id) {
1748 if ((active_planes & BIT(plane_id)) == 0) {
1749 fifo_state->plane[plane_id] = 0;
1753 rate = raw->plane[plane_id];
1754 fifo_state->plane[plane_id] = fifo_size * rate / total_rate;
1755 fifo_left -= fifo_state->plane[plane_id];
1758 fifo_state->plane[PLANE_SPRITE0] += sprite0_fifo_extra;
1759 fifo_left -= sprite0_fifo_extra;
1761 fifo_state->plane[PLANE_CURSOR] = 63;
1763 fifo_extra = DIV_ROUND_UP(fifo_left, num_active_planes ?: 1);
1765 /* spread the remainder evenly */
1766 for_each_plane_id_on_crtc(crtc, plane_id) {
1772 if ((active_planes & BIT(plane_id)) == 0)
1775 plane_extra = min(fifo_extra, fifo_left);
1776 fifo_state->plane[plane_id] += plane_extra;
1777 fifo_left -= plane_extra;
1780 drm_WARN_ON(&dev_priv->drm, active_planes != 0 && fifo_left != 0);
1782 /* give it all to the first plane if none are active */
1783 if (active_planes == 0) {
1784 drm_WARN_ON(&dev_priv->drm, fifo_left != fifo_size);
1785 fifo_state->plane[PLANE_PRIMARY] = fifo_left;
1791 /* mark all levels starting from 'level' as invalid */
1792 static void vlv_invalidate_wms(struct intel_crtc *crtc,
1793 struct vlv_wm_state *wm_state, int level)
1795 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1797 for (; level < intel_wm_num_levels(dev_priv); level++) {
1798 enum plane_id plane_id;
1800 for_each_plane_id_on_crtc(crtc, plane_id)
1801 wm_state->wm[level].plane[plane_id] = USHRT_MAX;
1803 wm_state->sr[level].cursor = USHRT_MAX;
1804 wm_state->sr[level].plane = USHRT_MAX;
1808 static u16 vlv_invert_wm_value(u16 wm, u16 fifo_size)
1813 return fifo_size - wm;
1817 * Starting from 'level' set all higher
1818 * levels to 'value' in the "raw" watermarks.
1820 static bool vlv_raw_plane_wm_set(struct intel_crtc_state *crtc_state,
1821 int level, enum plane_id plane_id, u16 value)
1823 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
1824 int num_levels = intel_wm_num_levels(dev_priv);
1827 for (; level < num_levels; level++) {
1828 struct g4x_pipe_wm *raw = &crtc_state->wm.vlv.raw[level];
1830 dirty |= raw->plane[plane_id] != value;
1831 raw->plane[plane_id] = value;
1837 static bool vlv_raw_plane_wm_compute(struct intel_crtc_state *crtc_state,
1838 const struct intel_plane_state *plane_state)
1840 struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
1841 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
1842 enum plane_id plane_id = plane->id;
1843 int num_levels = intel_wm_num_levels(to_i915(plane->base.dev));
1847 if (!intel_wm_plane_visible(crtc_state, plane_state)) {
1848 dirty |= vlv_raw_plane_wm_set(crtc_state, 0, plane_id, 0);
1852 for (level = 0; level < num_levels; level++) {
1853 struct g4x_pipe_wm *raw = &crtc_state->wm.vlv.raw[level];
1854 int wm = vlv_compute_wm_level(crtc_state, plane_state, level);
1855 int max_wm = plane_id == PLANE_CURSOR ? 63 : 511;
1860 dirty |= raw->plane[plane_id] != wm;
1861 raw->plane[plane_id] = wm;
1864 /* mark all higher levels as invalid */
1865 dirty |= vlv_raw_plane_wm_set(crtc_state, level, plane_id, USHRT_MAX);
1869 drm_dbg_kms(&dev_priv->drm,
1870 "%s watermarks: PM2=%d, PM5=%d, DDR DVFS=%d\n",
1872 crtc_state->wm.vlv.raw[VLV_WM_LEVEL_PM2].plane[plane_id],
1873 crtc_state->wm.vlv.raw[VLV_WM_LEVEL_PM5].plane[plane_id],
1874 crtc_state->wm.vlv.raw[VLV_WM_LEVEL_DDR_DVFS].plane[plane_id]);
1879 static bool vlv_raw_plane_wm_is_valid(const struct intel_crtc_state *crtc_state,
1880 enum plane_id plane_id, int level)
1882 const struct g4x_pipe_wm *raw =
1883 &crtc_state->wm.vlv.raw[level];
1884 const struct vlv_fifo_state *fifo_state =
1885 &crtc_state->wm.vlv.fifo_state;
1887 return raw->plane[plane_id] <= fifo_state->plane[plane_id];
1890 static bool vlv_raw_crtc_wm_is_valid(const struct intel_crtc_state *crtc_state, int level)
1892 return vlv_raw_plane_wm_is_valid(crtc_state, PLANE_PRIMARY, level) &&
1893 vlv_raw_plane_wm_is_valid(crtc_state, PLANE_SPRITE0, level) &&
1894 vlv_raw_plane_wm_is_valid(crtc_state, PLANE_SPRITE1, level) &&
1895 vlv_raw_plane_wm_is_valid(crtc_state, PLANE_CURSOR, level);
1898 static int vlv_compute_pipe_wm(struct intel_atomic_state *state,
1899 struct intel_crtc *crtc)
1901 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1902 struct intel_crtc_state *crtc_state =
1903 intel_atomic_get_new_crtc_state(state, crtc);
1904 struct vlv_wm_state *wm_state = &crtc_state->wm.vlv.optimal;
1905 const struct vlv_fifo_state *fifo_state =
1906 &crtc_state->wm.vlv.fifo_state;
1907 u8 active_planes = crtc_state->active_planes & ~BIT(PLANE_CURSOR);
1908 int num_active_planes = hweight8(active_planes);
1909 bool needs_modeset = drm_atomic_crtc_needs_modeset(&crtc_state->uapi);
1910 const struct intel_plane_state *old_plane_state;
1911 const struct intel_plane_state *new_plane_state;
1912 struct intel_plane *plane;
1913 enum plane_id plane_id;
1915 unsigned int dirty = 0;
1917 for_each_oldnew_intel_plane_in_state(state, plane,
1919 new_plane_state, i) {
1920 if (new_plane_state->hw.crtc != &crtc->base &&
1921 old_plane_state->hw.crtc != &crtc->base)
1924 if (vlv_raw_plane_wm_compute(crtc_state, new_plane_state))
1925 dirty |= BIT(plane->id);
1929 * DSPARB registers may have been reset due to the
1930 * power well being turned off. Make sure we restore
1931 * them to a consistent state even if no primary/sprite
1932 * planes are initially active.
1935 crtc_state->fifo_changed = true;
1940 /* cursor changes don't warrant a FIFO recompute */
1941 if (dirty & ~BIT(PLANE_CURSOR)) {
1942 const struct intel_crtc_state *old_crtc_state =
1943 intel_atomic_get_old_crtc_state(state, crtc);
1944 const struct vlv_fifo_state *old_fifo_state =
1945 &old_crtc_state->wm.vlv.fifo_state;
1947 ret = vlv_compute_fifo(crtc_state);
1951 if (needs_modeset ||
1952 memcmp(old_fifo_state, fifo_state,
1953 sizeof(*fifo_state)) != 0)
1954 crtc_state->fifo_changed = true;
1957 /* initially allow all levels */
1958 wm_state->num_levels = intel_wm_num_levels(dev_priv);
1960 * Note that enabling cxsr with no primary/sprite planes
1961 * enabled can wedge the pipe. Hence we only allow cxsr
1962 * with exactly one enabled primary/sprite plane.
1964 wm_state->cxsr = crtc->pipe != PIPE_C && num_active_planes == 1;
1966 for (level = 0; level < wm_state->num_levels; level++) {
1967 const struct g4x_pipe_wm *raw = &crtc_state->wm.vlv.raw[level];
1968 const int sr_fifo_size = INTEL_NUM_PIPES(dev_priv) * 512 - 1;
1970 if (!vlv_raw_crtc_wm_is_valid(crtc_state, level))
1973 for_each_plane_id_on_crtc(crtc, plane_id) {
1974 wm_state->wm[level].plane[plane_id] =
1975 vlv_invert_wm_value(raw->plane[plane_id],
1976 fifo_state->plane[plane_id]);
1979 wm_state->sr[level].plane =
1980 vlv_invert_wm_value(max3(raw->plane[PLANE_PRIMARY],
1981 raw->plane[PLANE_SPRITE0],
1982 raw->plane[PLANE_SPRITE1]),
1985 wm_state->sr[level].cursor =
1986 vlv_invert_wm_value(raw->plane[PLANE_CURSOR],
1993 /* limit to only levels we can actually handle */
1994 wm_state->num_levels = level;
1996 /* invalidate the higher levels */
1997 vlv_invalidate_wms(crtc, wm_state, level);
2002 #define VLV_FIFO(plane, value) \
2003 (((value) << DSPARB_ ## plane ## _SHIFT_VLV) & DSPARB_ ## plane ## _MASK_VLV)
2005 static void vlv_atomic_update_fifo(struct intel_atomic_state *state,
2006 struct intel_crtc *crtc)
2008 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
2009 struct intel_uncore *uncore = &dev_priv->uncore;
2010 const struct intel_crtc_state *crtc_state =
2011 intel_atomic_get_new_crtc_state(state, crtc);
2012 const struct vlv_fifo_state *fifo_state =
2013 &crtc_state->wm.vlv.fifo_state;
2014 int sprite0_start, sprite1_start, fifo_size;
2015 u32 dsparb, dsparb2, dsparb3;
2017 if (!crtc_state->fifo_changed)
2020 sprite0_start = fifo_state->plane[PLANE_PRIMARY];
2021 sprite1_start = fifo_state->plane[PLANE_SPRITE0] + sprite0_start;
2022 fifo_size = fifo_state->plane[PLANE_SPRITE1] + sprite1_start;
2024 drm_WARN_ON(&dev_priv->drm, fifo_state->plane[PLANE_CURSOR] != 63);
2025 drm_WARN_ON(&dev_priv->drm, fifo_size != 511);
2027 trace_vlv_fifo_size(crtc, sprite0_start, sprite1_start, fifo_size);
2030 * uncore.lock serves a double purpose here. It allows us to
2031 * use the less expensive I915_{READ,WRITE}_FW() functions, and
2032 * it protects the DSPARB registers from getting clobbered by
2033 * parallel updates from multiple pipes.
2035 * intel_pipe_update_start() has already disabled interrupts
2036 * for us, so a plain spin_lock() is sufficient here.
2038 spin_lock(&uncore->lock);
2040 switch (crtc->pipe) {
2042 dsparb = intel_uncore_read_fw(uncore, DSPARB);
2043 dsparb2 = intel_uncore_read_fw(uncore, DSPARB2);
2045 dsparb &= ~(VLV_FIFO(SPRITEA, 0xff) |
2046 VLV_FIFO(SPRITEB, 0xff));
2047 dsparb |= (VLV_FIFO(SPRITEA, sprite0_start) |
2048 VLV_FIFO(SPRITEB, sprite1_start));
2050 dsparb2 &= ~(VLV_FIFO(SPRITEA_HI, 0x1) |
2051 VLV_FIFO(SPRITEB_HI, 0x1));
2052 dsparb2 |= (VLV_FIFO(SPRITEA_HI, sprite0_start >> 8) |
2053 VLV_FIFO(SPRITEB_HI, sprite1_start >> 8));
2055 intel_uncore_write_fw(uncore, DSPARB, dsparb);
2056 intel_uncore_write_fw(uncore, DSPARB2, dsparb2);
2059 dsparb = intel_uncore_read_fw(uncore, DSPARB);
2060 dsparb2 = intel_uncore_read_fw(uncore, DSPARB2);
2062 dsparb &= ~(VLV_FIFO(SPRITEC, 0xff) |
2063 VLV_FIFO(SPRITED, 0xff));
2064 dsparb |= (VLV_FIFO(SPRITEC, sprite0_start) |
2065 VLV_FIFO(SPRITED, sprite1_start));
2067 dsparb2 &= ~(VLV_FIFO(SPRITEC_HI, 0xff) |
2068 VLV_FIFO(SPRITED_HI, 0xff));
2069 dsparb2 |= (VLV_FIFO(SPRITEC_HI, sprite0_start >> 8) |
2070 VLV_FIFO(SPRITED_HI, sprite1_start >> 8));
2072 intel_uncore_write_fw(uncore, DSPARB, dsparb);
2073 intel_uncore_write_fw(uncore, DSPARB2, dsparb2);
2076 dsparb3 = intel_uncore_read_fw(uncore, DSPARB3);
2077 dsparb2 = intel_uncore_read_fw(uncore, DSPARB2);
2079 dsparb3 &= ~(VLV_FIFO(SPRITEE, 0xff) |
2080 VLV_FIFO(SPRITEF, 0xff));
2081 dsparb3 |= (VLV_FIFO(SPRITEE, sprite0_start) |
2082 VLV_FIFO(SPRITEF, sprite1_start));
2084 dsparb2 &= ~(VLV_FIFO(SPRITEE_HI, 0xff) |
2085 VLV_FIFO(SPRITEF_HI, 0xff));
2086 dsparb2 |= (VLV_FIFO(SPRITEE_HI, sprite0_start >> 8) |
2087 VLV_FIFO(SPRITEF_HI, sprite1_start >> 8));
2089 intel_uncore_write_fw(uncore, DSPARB3, dsparb3);
2090 intel_uncore_write_fw(uncore, DSPARB2, dsparb2);
2096 intel_uncore_posting_read_fw(uncore, DSPARB);
2098 spin_unlock(&uncore->lock);
2103 static int vlv_compute_intermediate_wm(struct intel_atomic_state *state,
2104 struct intel_crtc *crtc)
2106 struct intel_crtc_state *new_crtc_state =
2107 intel_atomic_get_new_crtc_state(state, crtc);
2108 const struct intel_crtc_state *old_crtc_state =
2109 intel_atomic_get_old_crtc_state(state, crtc);
2110 struct vlv_wm_state *intermediate = &new_crtc_state->wm.vlv.intermediate;
2111 const struct vlv_wm_state *optimal = &new_crtc_state->wm.vlv.optimal;
2112 const struct vlv_wm_state *active = &old_crtc_state->wm.vlv.optimal;
2115 if (!new_crtc_state->hw.active ||
2116 drm_atomic_crtc_needs_modeset(&new_crtc_state->uapi)) {
2117 *intermediate = *optimal;
2119 intermediate->cxsr = false;
2123 intermediate->num_levels = min(optimal->num_levels, active->num_levels);
2124 intermediate->cxsr = optimal->cxsr && active->cxsr &&
2125 !new_crtc_state->disable_cxsr;
2127 for (level = 0; level < intermediate->num_levels; level++) {
2128 enum plane_id plane_id;
2130 for_each_plane_id_on_crtc(crtc, plane_id) {
2131 intermediate->wm[level].plane[plane_id] =
2132 min(optimal->wm[level].plane[plane_id],
2133 active->wm[level].plane[plane_id]);
2136 intermediate->sr[level].plane = min(optimal->sr[level].plane,
2137 active->sr[level].plane);
2138 intermediate->sr[level].cursor = min(optimal->sr[level].cursor,
2139 active->sr[level].cursor);
2142 vlv_invalidate_wms(crtc, intermediate, level);
2146 * If our intermediate WM are identical to the final WM, then we can
2147 * omit the post-vblank programming; only update if it's different.
2149 if (memcmp(intermediate, optimal, sizeof(*intermediate)) != 0)
2150 new_crtc_state->wm.need_postvbl_update = true;
2155 static void vlv_merge_wm(struct drm_i915_private *dev_priv,
2156 struct vlv_wm_values *wm)
2158 struct intel_crtc *crtc;
2159 int num_active_pipes = 0;
2161 wm->level = dev_priv->display.wm.max_level;
2164 for_each_intel_crtc(&dev_priv->drm, crtc) {
2165 const struct vlv_wm_state *wm_state = &crtc->wm.active.vlv;
2170 if (!wm_state->cxsr)
2174 wm->level = min_t(int, wm->level, wm_state->num_levels - 1);
2177 if (num_active_pipes != 1)
2180 if (num_active_pipes > 1)
2181 wm->level = VLV_WM_LEVEL_PM2;
2183 for_each_intel_crtc(&dev_priv->drm, crtc) {
2184 const struct vlv_wm_state *wm_state = &crtc->wm.active.vlv;
2185 enum pipe pipe = crtc->pipe;
2187 wm->pipe[pipe] = wm_state->wm[wm->level];
2188 if (crtc->active && wm->cxsr)
2189 wm->sr = wm_state->sr[wm->level];
2191 wm->ddl[pipe].plane[PLANE_PRIMARY] = DDL_PRECISION_HIGH | 2;
2192 wm->ddl[pipe].plane[PLANE_SPRITE0] = DDL_PRECISION_HIGH | 2;
2193 wm->ddl[pipe].plane[PLANE_SPRITE1] = DDL_PRECISION_HIGH | 2;
2194 wm->ddl[pipe].plane[PLANE_CURSOR] = DDL_PRECISION_HIGH | 2;
2198 static void vlv_program_watermarks(struct drm_i915_private *dev_priv)
2200 struct vlv_wm_values *old_wm = &dev_priv->display.wm.vlv;
2201 struct vlv_wm_values new_wm = {};
2203 vlv_merge_wm(dev_priv, &new_wm);
2205 if (memcmp(old_wm, &new_wm, sizeof(new_wm)) == 0)
2208 if (is_disabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_DDR_DVFS))
2209 chv_set_memory_dvfs(dev_priv, false);
2211 if (is_disabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_PM5))
2212 chv_set_memory_pm5(dev_priv, false);
2214 if (is_disabling(old_wm->cxsr, new_wm.cxsr, true))
2215 _intel_set_memory_cxsr(dev_priv, false);
2217 vlv_write_wm_values(dev_priv, &new_wm);
2219 if (is_enabling(old_wm->cxsr, new_wm.cxsr, true))
2220 _intel_set_memory_cxsr(dev_priv, true);
2222 if (is_enabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_PM5))
2223 chv_set_memory_pm5(dev_priv, true);
2225 if (is_enabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_DDR_DVFS))
2226 chv_set_memory_dvfs(dev_priv, true);
2231 static void vlv_initial_watermarks(struct intel_atomic_state *state,
2232 struct intel_crtc *crtc)
2234 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
2235 const struct intel_crtc_state *crtc_state =
2236 intel_atomic_get_new_crtc_state(state, crtc);
2238 mutex_lock(&dev_priv->display.wm.wm_mutex);
2239 crtc->wm.active.vlv = crtc_state->wm.vlv.intermediate;
2240 vlv_program_watermarks(dev_priv);
2241 mutex_unlock(&dev_priv->display.wm.wm_mutex);
2244 static void vlv_optimize_watermarks(struct intel_atomic_state *state,
2245 struct intel_crtc *crtc)
2247 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
2248 const struct intel_crtc_state *crtc_state =
2249 intel_atomic_get_new_crtc_state(state, crtc);
2251 if (!crtc_state->wm.need_postvbl_update)
2254 mutex_lock(&dev_priv->display.wm.wm_mutex);
2255 crtc->wm.active.vlv = crtc_state->wm.vlv.optimal;
2256 vlv_program_watermarks(dev_priv);
2257 mutex_unlock(&dev_priv->display.wm.wm_mutex);
2260 static void i965_update_wm(struct drm_i915_private *dev_priv)
2262 struct intel_crtc *crtc;
2267 /* Calc sr entries for one plane configs */
2268 crtc = single_enabled_crtc(dev_priv);
2270 /* self-refresh has much higher latency */
2271 static const int sr_latency_ns = 12000;
2272 const struct drm_display_mode *pipe_mode =
2273 &crtc->config->hw.pipe_mode;
2274 const struct drm_framebuffer *fb =
2275 crtc->base.primary->state->fb;
2276 int pixel_rate = crtc->config->pixel_rate;
2277 int htotal = pipe_mode->crtc_htotal;
2278 int width = drm_rect_width(&crtc->base.primary->state->src) >> 16;
2279 int cpp = fb->format->cpp[0];
2282 entries = intel_wm_method2(pixel_rate, htotal,
2283 width, cpp, sr_latency_ns / 100);
2284 entries = DIV_ROUND_UP(entries, I915_FIFO_LINE_SIZE);
2285 srwm = I965_FIFO_SIZE - entries;
2289 drm_dbg_kms(&dev_priv->drm,
2290 "self-refresh entries: %d, wm: %d\n",
2293 entries = intel_wm_method2(pixel_rate, htotal,
2294 crtc->base.cursor->state->crtc_w, 4,
2295 sr_latency_ns / 100);
2296 entries = DIV_ROUND_UP(entries,
2297 i965_cursor_wm_info.cacheline_size) +
2298 i965_cursor_wm_info.guard_size;
2300 cursor_sr = i965_cursor_wm_info.fifo_size - entries;
2301 if (cursor_sr > i965_cursor_wm_info.max_wm)
2302 cursor_sr = i965_cursor_wm_info.max_wm;
2304 drm_dbg_kms(&dev_priv->drm,
2305 "self-refresh watermark: display plane %d "
2306 "cursor %d\n", srwm, cursor_sr);
2308 cxsr_enabled = true;
2310 cxsr_enabled = false;
2311 /* Turn off self refresh if both pipes are enabled */
2312 intel_set_memory_cxsr(dev_priv, false);
2315 drm_dbg_kms(&dev_priv->drm,
2316 "Setting FIFO watermarks - A: 8, B: 8, C: 8, SR %d\n",
2319 /* 965 has limitations... */
2320 intel_uncore_write(&dev_priv->uncore, DSPFW1, FW_WM(srwm, SR) |
2324 intel_uncore_write(&dev_priv->uncore, DSPFW2, FW_WM(8, CURSORA) |
2325 FW_WM(8, PLANEC_OLD));
2326 /* update cursor SR watermark */
2327 intel_uncore_write(&dev_priv->uncore, DSPFW3, FW_WM(cursor_sr, CURSOR_SR));
2330 intel_set_memory_cxsr(dev_priv, true);
2335 static struct intel_crtc *intel_crtc_for_plane(struct drm_i915_private *i915,
2336 enum i9xx_plane_id i9xx_plane)
2338 struct intel_plane *plane;
2340 for_each_intel_plane(&i915->drm, plane) {
2341 if (plane->id == PLANE_PRIMARY &&
2342 plane->i9xx_plane == i9xx_plane)
2343 return intel_crtc_for_pipe(i915, plane->pipe);
2349 static void i9xx_update_wm(struct drm_i915_private *dev_priv)
2351 const struct intel_watermark_params *wm_info;
2356 int planea_wm, planeb_wm;
2357 struct intel_crtc *crtc;
2359 if (IS_I945GM(dev_priv))
2360 wm_info = &i945_wm_info;
2361 else if (DISPLAY_VER(dev_priv) != 2)
2362 wm_info = &i915_wm_info;
2364 wm_info = &i830_a_wm_info;
2366 if (DISPLAY_VER(dev_priv) == 2)
2367 fifo_size = i830_get_fifo_size(dev_priv, PLANE_A);
2369 fifo_size = i9xx_get_fifo_size(dev_priv, PLANE_A);
2370 crtc = intel_crtc_for_plane(dev_priv, PLANE_A);
2371 if (intel_crtc_active(crtc)) {
2372 const struct drm_framebuffer *fb =
2373 crtc->base.primary->state->fb;
2376 if (DISPLAY_VER(dev_priv) == 2)
2379 cpp = fb->format->cpp[0];
2381 planea_wm = intel_calculate_wm(crtc->config->pixel_rate,
2382 wm_info, fifo_size, cpp,
2383 pessimal_latency_ns);
2385 planea_wm = fifo_size - wm_info->guard_size;
2386 if (planea_wm > (long)wm_info->max_wm)
2387 planea_wm = wm_info->max_wm;
2390 if (DISPLAY_VER(dev_priv) == 2)
2391 wm_info = &i830_bc_wm_info;
2393 if (DISPLAY_VER(dev_priv) == 2)
2394 fifo_size = i830_get_fifo_size(dev_priv, PLANE_B);
2396 fifo_size = i9xx_get_fifo_size(dev_priv, PLANE_B);
2397 crtc = intel_crtc_for_plane(dev_priv, PLANE_B);
2398 if (intel_crtc_active(crtc)) {
2399 const struct drm_framebuffer *fb =
2400 crtc->base.primary->state->fb;
2403 if (DISPLAY_VER(dev_priv) == 2)
2406 cpp = fb->format->cpp[0];
2408 planeb_wm = intel_calculate_wm(crtc->config->pixel_rate,
2409 wm_info, fifo_size, cpp,
2410 pessimal_latency_ns);
2412 planeb_wm = fifo_size - wm_info->guard_size;
2413 if (planeb_wm > (long)wm_info->max_wm)
2414 planeb_wm = wm_info->max_wm;
2417 drm_dbg_kms(&dev_priv->drm,
2418 "FIFO watermarks - A: %d, B: %d\n", planea_wm, planeb_wm);
2420 crtc = single_enabled_crtc(dev_priv);
2421 if (IS_I915GM(dev_priv) && crtc) {
2422 struct drm_i915_gem_object *obj;
2424 obj = intel_fb_obj(crtc->base.primary->state->fb);
2426 /* self-refresh seems busted with untiled */
2427 if (!i915_gem_object_is_tiled(obj))
2432 * Overlay gets an aggressive default since video jitter is bad.
2436 /* Play safe and disable self-refresh before adjusting watermarks. */
2437 intel_set_memory_cxsr(dev_priv, false);
2439 /* Calc sr entries for one plane configs */
2440 if (HAS_FW_BLC(dev_priv) && crtc) {
2441 /* self-refresh has much higher latency */
2442 static const int sr_latency_ns = 6000;
2443 const struct drm_display_mode *pipe_mode =
2444 &crtc->config->hw.pipe_mode;
2445 const struct drm_framebuffer *fb =
2446 crtc->base.primary->state->fb;
2447 int pixel_rate = crtc->config->pixel_rate;
2448 int htotal = pipe_mode->crtc_htotal;
2449 int width = drm_rect_width(&crtc->base.primary->state->src) >> 16;
2453 if (IS_I915GM(dev_priv) || IS_I945GM(dev_priv))
2456 cpp = fb->format->cpp[0];
2458 entries = intel_wm_method2(pixel_rate, htotal, width, cpp,
2459 sr_latency_ns / 100);
2460 entries = DIV_ROUND_UP(entries, wm_info->cacheline_size);
2461 drm_dbg_kms(&dev_priv->drm,
2462 "self-refresh entries: %d\n", entries);
2463 srwm = wm_info->fifo_size - entries;
2467 if (IS_I945G(dev_priv) || IS_I945GM(dev_priv))
2468 intel_uncore_write(&dev_priv->uncore, FW_BLC_SELF,
2469 FW_BLC_SELF_FIFO_MASK | (srwm & 0xff));
2471 intel_uncore_write(&dev_priv->uncore, FW_BLC_SELF, srwm & 0x3f);
2474 drm_dbg_kms(&dev_priv->drm,
2475 "Setting FIFO watermarks - A: %d, B: %d, C: %d, SR %d\n",
2476 planea_wm, planeb_wm, cwm, srwm);
2478 fwater_lo = ((planeb_wm & 0x3f) << 16) | (planea_wm & 0x3f);
2479 fwater_hi = (cwm & 0x1f);
2481 /* Set request length to 8 cachelines per fetch */
2482 fwater_lo = fwater_lo | (1 << 24) | (1 << 8);
2483 fwater_hi = fwater_hi | (1 << 8);
2485 intel_uncore_write(&dev_priv->uncore, FW_BLC, fwater_lo);
2486 intel_uncore_write(&dev_priv->uncore, FW_BLC2, fwater_hi);
2489 intel_set_memory_cxsr(dev_priv, true);
2492 static void i845_update_wm(struct drm_i915_private *dev_priv)
2494 struct intel_crtc *crtc;
2498 crtc = single_enabled_crtc(dev_priv);
2502 planea_wm = intel_calculate_wm(crtc->config->pixel_rate,
2504 i845_get_fifo_size(dev_priv, PLANE_A),
2505 4, pessimal_latency_ns);
2506 fwater_lo = intel_uncore_read(&dev_priv->uncore, FW_BLC) & ~0xfff;
2507 fwater_lo |= (3<<8) | planea_wm;
2509 drm_dbg_kms(&dev_priv->drm,
2510 "Setting FIFO watermarks - A: %d\n", planea_wm);
2512 intel_uncore_write(&dev_priv->uncore, FW_BLC, fwater_lo);
2515 /* latency must be in 0.1us units. */
2516 static unsigned int ilk_wm_method1(unsigned int pixel_rate,
2518 unsigned int latency)
2522 ret = intel_wm_method1(pixel_rate, cpp, latency);
2523 ret = DIV_ROUND_UP(ret, 64) + 2;
2528 /* latency must be in 0.1us units. */
2529 static unsigned int ilk_wm_method2(unsigned int pixel_rate,
2530 unsigned int htotal,
2533 unsigned int latency)
2537 ret = intel_wm_method2(pixel_rate, htotal,
2538 width, cpp, latency);
2539 ret = DIV_ROUND_UP(ret, 64) + 2;
2544 static u32 ilk_wm_fbc(u32 pri_val, u32 horiz_pixels, u8 cpp)
2547 * Neither of these should be possible since this function shouldn't be
2548 * called if the CRTC is off or the plane is invisible. But let's be
2549 * extra paranoid to avoid a potential divide-by-zero if we screw up
2550 * elsewhere in the driver.
2554 if (WARN_ON(!horiz_pixels))
2557 return DIV_ROUND_UP(pri_val * 64, horiz_pixels * cpp) + 2;
2560 struct ilk_wm_maximums {
2568 * For both WM_PIPE and WM_LP.
2569 * mem_value must be in 0.1us units.
2571 static u32 ilk_compute_pri_wm(const struct intel_crtc_state *crtc_state,
2572 const struct intel_plane_state *plane_state,
2573 u32 mem_value, bool is_lp)
2575 u32 method1, method2;
2581 if (!intel_wm_plane_visible(crtc_state, plane_state))
2584 cpp = plane_state->hw.fb->format->cpp[0];
2586 method1 = ilk_wm_method1(crtc_state->pixel_rate, cpp, mem_value);
2591 method2 = ilk_wm_method2(crtc_state->pixel_rate,
2592 crtc_state->hw.pipe_mode.crtc_htotal,
2593 drm_rect_width(&plane_state->uapi.src) >> 16,
2596 return min(method1, method2);
2600 * For both WM_PIPE and WM_LP.
2601 * mem_value must be in 0.1us units.
2603 static u32 ilk_compute_spr_wm(const struct intel_crtc_state *crtc_state,
2604 const struct intel_plane_state *plane_state,
2607 u32 method1, method2;
2613 if (!intel_wm_plane_visible(crtc_state, plane_state))
2616 cpp = plane_state->hw.fb->format->cpp[0];
2618 method1 = ilk_wm_method1(crtc_state->pixel_rate, cpp, mem_value);
2619 method2 = ilk_wm_method2(crtc_state->pixel_rate,
2620 crtc_state->hw.pipe_mode.crtc_htotal,
2621 drm_rect_width(&plane_state->uapi.src) >> 16,
2623 return min(method1, method2);
2627 * For both WM_PIPE and WM_LP.
2628 * mem_value must be in 0.1us units.
2630 static u32 ilk_compute_cur_wm(const struct intel_crtc_state *crtc_state,
2631 const struct intel_plane_state *plane_state,
2639 if (!intel_wm_plane_visible(crtc_state, plane_state))
2642 cpp = plane_state->hw.fb->format->cpp[0];
2644 return ilk_wm_method2(crtc_state->pixel_rate,
2645 crtc_state->hw.pipe_mode.crtc_htotal,
2646 drm_rect_width(&plane_state->uapi.src) >> 16,
2650 /* Only for WM_LP. */
2651 static u32 ilk_compute_fbc_wm(const struct intel_crtc_state *crtc_state,
2652 const struct intel_plane_state *plane_state,
2657 if (!intel_wm_plane_visible(crtc_state, plane_state))
2660 cpp = plane_state->hw.fb->format->cpp[0];
2662 return ilk_wm_fbc(pri_val, drm_rect_width(&plane_state->uapi.src) >> 16,
2667 ilk_display_fifo_size(const struct drm_i915_private *dev_priv)
2669 if (DISPLAY_VER(dev_priv) >= 8)
2671 else if (DISPLAY_VER(dev_priv) >= 7)
2678 ilk_plane_wm_reg_max(const struct drm_i915_private *dev_priv,
2679 int level, bool is_sprite)
2681 if (DISPLAY_VER(dev_priv) >= 8)
2682 /* BDW primary/sprite plane watermarks */
2683 return level == 0 ? 255 : 2047;
2684 else if (DISPLAY_VER(dev_priv) >= 7)
2685 /* IVB/HSW primary/sprite plane watermarks */
2686 return level == 0 ? 127 : 1023;
2687 else if (!is_sprite)
2688 /* ILK/SNB primary plane watermarks */
2689 return level == 0 ? 127 : 511;
2691 /* ILK/SNB sprite plane watermarks */
2692 return level == 0 ? 63 : 255;
2696 ilk_cursor_wm_reg_max(const struct drm_i915_private *dev_priv, int level)
2698 if (DISPLAY_VER(dev_priv) >= 7)
2699 return level == 0 ? 63 : 255;
2701 return level == 0 ? 31 : 63;
2704 static unsigned int ilk_fbc_wm_reg_max(const struct drm_i915_private *dev_priv)
2706 if (DISPLAY_VER(dev_priv) >= 8)
2712 /* Calculate the maximum primary/sprite plane watermark */
2713 static unsigned int ilk_plane_wm_max(const struct drm_i915_private *dev_priv,
2715 const struct intel_wm_config *config,
2716 enum intel_ddb_partitioning ddb_partitioning,
2719 unsigned int fifo_size = ilk_display_fifo_size(dev_priv);
2721 /* if sprites aren't enabled, sprites get nothing */
2722 if (is_sprite && !config->sprites_enabled)
2725 /* HSW allows LP1+ watermarks even with multiple pipes */
2726 if (level == 0 || config->num_pipes_active > 1) {
2727 fifo_size /= INTEL_NUM_PIPES(dev_priv);
2730 * For some reason the non self refresh
2731 * FIFO size is only half of the self
2732 * refresh FIFO size on ILK/SNB.
2734 if (DISPLAY_VER(dev_priv) <= 6)
2738 if (config->sprites_enabled) {
2739 /* level 0 is always calculated with 1:1 split */
2740 if (level > 0 && ddb_partitioning == INTEL_DDB_PART_5_6) {
2749 /* clamp to max that the registers can hold */
2750 return min(fifo_size, ilk_plane_wm_reg_max(dev_priv, level, is_sprite));
2753 /* Calculate the maximum cursor plane watermark */
2754 static unsigned int ilk_cursor_wm_max(const struct drm_i915_private *dev_priv,
2756 const struct intel_wm_config *config)
2758 /* HSW LP1+ watermarks w/ multiple pipes */
2759 if (level > 0 && config->num_pipes_active > 1)
2762 /* otherwise just report max that registers can hold */
2763 return ilk_cursor_wm_reg_max(dev_priv, level);
2766 static void ilk_compute_wm_maximums(const struct drm_i915_private *dev_priv,
2768 const struct intel_wm_config *config,
2769 enum intel_ddb_partitioning ddb_partitioning,
2770 struct ilk_wm_maximums *max)
2772 max->pri = ilk_plane_wm_max(dev_priv, level, config, ddb_partitioning, false);
2773 max->spr = ilk_plane_wm_max(dev_priv, level, config, ddb_partitioning, true);
2774 max->cur = ilk_cursor_wm_max(dev_priv, level, config);
2775 max->fbc = ilk_fbc_wm_reg_max(dev_priv);
2778 static void ilk_compute_wm_reg_maximums(const struct drm_i915_private *dev_priv,
2780 struct ilk_wm_maximums *max)
2782 max->pri = ilk_plane_wm_reg_max(dev_priv, level, false);
2783 max->spr = ilk_plane_wm_reg_max(dev_priv, level, true);
2784 max->cur = ilk_cursor_wm_reg_max(dev_priv, level);
2785 max->fbc = ilk_fbc_wm_reg_max(dev_priv);
2788 static bool ilk_validate_wm_level(int level,
2789 const struct ilk_wm_maximums *max,
2790 struct intel_wm_level *result)
2794 /* already determined to be invalid? */
2795 if (!result->enable)
2798 result->enable = result->pri_val <= max->pri &&
2799 result->spr_val <= max->spr &&
2800 result->cur_val <= max->cur;
2802 ret = result->enable;
2805 * HACK until we can pre-compute everything,
2806 * and thus fail gracefully if LP0 watermarks
2809 if (level == 0 && !result->enable) {
2810 if (result->pri_val > max->pri)
2811 DRM_DEBUG_KMS("Primary WM%d too large %u (max %u)\n",
2812 level, result->pri_val, max->pri);
2813 if (result->spr_val > max->spr)
2814 DRM_DEBUG_KMS("Sprite WM%d too large %u (max %u)\n",
2815 level, result->spr_val, max->spr);
2816 if (result->cur_val > max->cur)
2817 DRM_DEBUG_KMS("Cursor WM%d too large %u (max %u)\n",
2818 level, result->cur_val, max->cur);
2820 result->pri_val = min_t(u32, result->pri_val, max->pri);
2821 result->spr_val = min_t(u32, result->spr_val, max->spr);
2822 result->cur_val = min_t(u32, result->cur_val, max->cur);
2823 result->enable = true;
2829 static void ilk_compute_wm_level(const struct drm_i915_private *dev_priv,
2830 const struct intel_crtc *crtc,
2832 struct intel_crtc_state *crtc_state,
2833 const struct intel_plane_state *pristate,
2834 const struct intel_plane_state *sprstate,
2835 const struct intel_plane_state *curstate,
2836 struct intel_wm_level *result)
2838 u16 pri_latency = dev_priv->display.wm.pri_latency[level];
2839 u16 spr_latency = dev_priv->display.wm.spr_latency[level];
2840 u16 cur_latency = dev_priv->display.wm.cur_latency[level];
2842 /* WM1+ latency values stored in 0.5us units */
2850 result->pri_val = ilk_compute_pri_wm(crtc_state, pristate,
2851 pri_latency, level);
2852 result->fbc_val = ilk_compute_fbc_wm(crtc_state, pristate, result->pri_val);
2856 result->spr_val = ilk_compute_spr_wm(crtc_state, sprstate, spr_latency);
2859 result->cur_val = ilk_compute_cur_wm(crtc_state, curstate, cur_latency);
2861 result->enable = true;
2865 adjust_wm_latency(struct drm_i915_private *i915,
2866 u16 wm[], int max_level, int read_latency)
2868 bool wm_lv_0_adjust_needed = i915->dram_info.wm_lv_0_adjust_needed;
2872 * If a level n (n > 1) has a 0us latency, all levels m (m >= n)
2873 * need to be disabled. We make sure to sanitize the values out
2874 * of the punit to satisfy this requirement.
2876 for (level = 1; level <= max_level; level++) {
2877 if (wm[level] == 0) {
2878 for (i = level + 1; i <= max_level; i++)
2881 max_level = level - 1;
2887 * WaWmMemoryReadLatency
2889 * punit doesn't take into account the read latency so we need
2890 * to add proper adjustement to each valid level we retrieve
2891 * from the punit when level 0 response data is 0us.
2894 for (level = 0; level <= max_level; level++)
2895 wm[level] += read_latency;
2899 * WA Level-0 adjustment for 16GB DIMMs: SKL+
2900 * If we could not get dimm info enable this WA to prevent from
2901 * any underrun. If not able to get Dimm info assume 16GB dimm
2902 * to avoid any underrun.
2904 if (wm_lv_0_adjust_needed)
2908 static void intel_read_wm_latency(struct drm_i915_private *dev_priv,
2911 struct intel_uncore *uncore = &dev_priv->uncore;
2912 int max_level = ilk_wm_max_level(dev_priv);
2914 if (DISPLAY_VER(dev_priv) >= 14) {
2917 val = intel_uncore_read(uncore, MTL_LATENCY_LP0_LP1);
2918 wm[0] = REG_FIELD_GET(MTL_LATENCY_LEVEL_EVEN_MASK, val);
2919 wm[1] = REG_FIELD_GET(MTL_LATENCY_LEVEL_ODD_MASK, val);
2920 val = intel_uncore_read(uncore, MTL_LATENCY_LP2_LP3);
2921 wm[2] = REG_FIELD_GET(MTL_LATENCY_LEVEL_EVEN_MASK, val);
2922 wm[3] = REG_FIELD_GET(MTL_LATENCY_LEVEL_ODD_MASK, val);
2923 val = intel_uncore_read(uncore, MTL_LATENCY_LP4_LP5);
2924 wm[4] = REG_FIELD_GET(MTL_LATENCY_LEVEL_EVEN_MASK, val);
2925 wm[5] = REG_FIELD_GET(MTL_LATENCY_LEVEL_ODD_MASK, val);
2927 adjust_wm_latency(dev_priv, wm, max_level, 6);
2928 } else if (DISPLAY_VER(dev_priv) >= 9) {
2929 int read_latency = DISPLAY_VER(dev_priv) >= 12 ? 3 : 2;
2930 int mult = IS_DG2(dev_priv) ? 2 : 1;
2934 /* read the first set of memory latencies[0:3] */
2935 val = 0; /* data0 to be programmed to 0 for first set */
2936 ret = snb_pcode_read(&dev_priv->uncore, GEN9_PCODE_READ_MEM_LATENCY,
2940 drm_err(&dev_priv->drm,
2941 "SKL Mailbox read error = %d\n", ret);
2945 wm[0] = (val & GEN9_MEM_LATENCY_LEVEL_MASK) * mult;
2946 wm[1] = ((val >> GEN9_MEM_LATENCY_LEVEL_1_5_SHIFT) &
2947 GEN9_MEM_LATENCY_LEVEL_MASK) * mult;
2948 wm[2] = ((val >> GEN9_MEM_LATENCY_LEVEL_2_6_SHIFT) &
2949 GEN9_MEM_LATENCY_LEVEL_MASK) * mult;
2950 wm[3] = ((val >> GEN9_MEM_LATENCY_LEVEL_3_7_SHIFT) &
2951 GEN9_MEM_LATENCY_LEVEL_MASK) * mult;
2953 /* read the second set of memory latencies[4:7] */
2954 val = 1; /* data0 to be programmed to 1 for second set */
2955 ret = snb_pcode_read(&dev_priv->uncore, GEN9_PCODE_READ_MEM_LATENCY,
2958 drm_err(&dev_priv->drm,
2959 "SKL Mailbox read error = %d\n", ret);
2963 wm[4] = (val & GEN9_MEM_LATENCY_LEVEL_MASK) * mult;
2964 wm[5] = ((val >> GEN9_MEM_LATENCY_LEVEL_1_5_SHIFT) &
2965 GEN9_MEM_LATENCY_LEVEL_MASK) * mult;
2966 wm[6] = ((val >> GEN9_MEM_LATENCY_LEVEL_2_6_SHIFT) &
2967 GEN9_MEM_LATENCY_LEVEL_MASK) * mult;
2968 wm[7] = ((val >> GEN9_MEM_LATENCY_LEVEL_3_7_SHIFT) &
2969 GEN9_MEM_LATENCY_LEVEL_MASK) * mult;
2971 adjust_wm_latency(dev_priv, wm, max_level, read_latency);
2972 } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
2973 u64 sskpd = intel_uncore_read64(uncore, MCH_SSKPD);
2975 wm[0] = REG_FIELD_GET64(SSKPD_NEW_WM0_MASK_HSW, sskpd);
2977 wm[0] = REG_FIELD_GET64(SSKPD_OLD_WM0_MASK_HSW, sskpd);
2978 wm[1] = REG_FIELD_GET64(SSKPD_WM1_MASK_HSW, sskpd);
2979 wm[2] = REG_FIELD_GET64(SSKPD_WM2_MASK_HSW, sskpd);
2980 wm[3] = REG_FIELD_GET64(SSKPD_WM3_MASK_HSW, sskpd);
2981 wm[4] = REG_FIELD_GET64(SSKPD_WM4_MASK_HSW, sskpd);
2982 } else if (DISPLAY_VER(dev_priv) >= 6) {
2983 u32 sskpd = intel_uncore_read(uncore, MCH_SSKPD);
2985 wm[0] = REG_FIELD_GET(SSKPD_WM0_MASK_SNB, sskpd);
2986 wm[1] = REG_FIELD_GET(SSKPD_WM1_MASK_SNB, sskpd);
2987 wm[2] = REG_FIELD_GET(SSKPD_WM2_MASK_SNB, sskpd);
2988 wm[3] = REG_FIELD_GET(SSKPD_WM3_MASK_SNB, sskpd);
2989 } else if (DISPLAY_VER(dev_priv) >= 5) {
2990 u32 mltr = intel_uncore_read(uncore, MLTR_ILK);
2992 /* ILK primary LP0 latency is 700 ns */
2994 wm[1] = REG_FIELD_GET(MLTR_WM1_MASK, mltr);
2995 wm[2] = REG_FIELD_GET(MLTR_WM2_MASK, mltr);
2997 MISSING_CASE(INTEL_DEVID(dev_priv));
3001 static void intel_fixup_spr_wm_latency(struct drm_i915_private *dev_priv,
3004 /* ILK sprite LP0 latency is 1300 ns */
3005 if (DISPLAY_VER(dev_priv) == 5)
3009 static void intel_fixup_cur_wm_latency(struct drm_i915_private *dev_priv,
3012 /* ILK cursor LP0 latency is 1300 ns */
3013 if (DISPLAY_VER(dev_priv) == 5)
3017 int ilk_wm_max_level(const struct drm_i915_private *dev_priv)
3019 /* how many WM levels are we expecting */
3020 if (HAS_HW_SAGV_WM(dev_priv))
3022 else if (DISPLAY_VER(dev_priv) >= 9)
3024 else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
3026 else if (DISPLAY_VER(dev_priv) >= 6)
3032 static void intel_print_wm_latency(struct drm_i915_private *dev_priv,
3036 int level, max_level = ilk_wm_max_level(dev_priv);
3038 for (level = 0; level <= max_level; level++) {
3039 unsigned int latency = wm[level];
3042 drm_dbg_kms(&dev_priv->drm,
3043 "%s WM%d latency not provided\n",
3049 * - latencies are in us on gen9.
3050 * - before then, WM1+ latency values are in 0.5us units
3052 if (DISPLAY_VER(dev_priv) >= 9)
3057 drm_dbg_kms(&dev_priv->drm,
3058 "%s WM%d latency %u (%u.%u usec)\n", name, level,
3059 wm[level], latency / 10, latency % 10);
3063 static bool ilk_increase_wm_latency(struct drm_i915_private *dev_priv,
3066 int level, max_level = ilk_wm_max_level(dev_priv);
3071 wm[0] = max(wm[0], min);
3072 for (level = 1; level <= max_level; level++)
3073 wm[level] = max_t(u16, wm[level], DIV_ROUND_UP(min, 5));
3078 static void snb_wm_latency_quirk(struct drm_i915_private *dev_priv)
3083 * The BIOS provided WM memory latency values are often
3084 * inadequate for high resolution displays. Adjust them.
3086 changed = ilk_increase_wm_latency(dev_priv, dev_priv->display.wm.pri_latency, 12);
3087 changed |= ilk_increase_wm_latency(dev_priv, dev_priv->display.wm.spr_latency, 12);
3088 changed |= ilk_increase_wm_latency(dev_priv, dev_priv->display.wm.cur_latency, 12);
3093 drm_dbg_kms(&dev_priv->drm,
3094 "WM latency values increased to avoid potential underruns\n");
3095 intel_print_wm_latency(dev_priv, "Primary", dev_priv->display.wm.pri_latency);
3096 intel_print_wm_latency(dev_priv, "Sprite", dev_priv->display.wm.spr_latency);
3097 intel_print_wm_latency(dev_priv, "Cursor", dev_priv->display.wm.cur_latency);
3100 static void snb_wm_lp3_irq_quirk(struct drm_i915_private *dev_priv)
3103 * On some SNB machines (Thinkpad X220 Tablet at least)
3104 * LP3 usage can cause vblank interrupts to be lost.
3105 * The DEIIR bit will go high but it looks like the CPU
3106 * never gets interrupted.
3108 * It's not clear whether other interrupt source could
3109 * be affected or if this is somehow limited to vblank
3110 * interrupts only. To play it safe we disable LP3
3111 * watermarks entirely.
3113 if (dev_priv->display.wm.pri_latency[3] == 0 &&
3114 dev_priv->display.wm.spr_latency[3] == 0 &&
3115 dev_priv->display.wm.cur_latency[3] == 0)
3118 dev_priv->display.wm.pri_latency[3] = 0;
3119 dev_priv->display.wm.spr_latency[3] = 0;
3120 dev_priv->display.wm.cur_latency[3] = 0;
3122 drm_dbg_kms(&dev_priv->drm,
3123 "LP3 watermarks disabled due to potential for lost interrupts\n");
3124 intel_print_wm_latency(dev_priv, "Primary", dev_priv->display.wm.pri_latency);
3125 intel_print_wm_latency(dev_priv, "Sprite", dev_priv->display.wm.spr_latency);
3126 intel_print_wm_latency(dev_priv, "Cursor", dev_priv->display.wm.cur_latency);
3129 static void ilk_setup_wm_latency(struct drm_i915_private *dev_priv)
3131 intel_read_wm_latency(dev_priv, dev_priv->display.wm.pri_latency);
3133 memcpy(dev_priv->display.wm.spr_latency, dev_priv->display.wm.pri_latency,
3134 sizeof(dev_priv->display.wm.pri_latency));
3135 memcpy(dev_priv->display.wm.cur_latency, dev_priv->display.wm.pri_latency,
3136 sizeof(dev_priv->display.wm.pri_latency));
3138 intel_fixup_spr_wm_latency(dev_priv, dev_priv->display.wm.spr_latency);
3139 intel_fixup_cur_wm_latency(dev_priv, dev_priv->display.wm.cur_latency);
3141 intel_print_wm_latency(dev_priv, "Primary", dev_priv->display.wm.pri_latency);
3142 intel_print_wm_latency(dev_priv, "Sprite", dev_priv->display.wm.spr_latency);
3143 intel_print_wm_latency(dev_priv, "Cursor", dev_priv->display.wm.cur_latency);
3145 if (DISPLAY_VER(dev_priv) == 6) {
3146 snb_wm_latency_quirk(dev_priv);
3147 snb_wm_lp3_irq_quirk(dev_priv);
3151 static void skl_setup_wm_latency(struct drm_i915_private *dev_priv)
3153 intel_read_wm_latency(dev_priv, dev_priv->display.wm.skl_latency);
3154 intel_print_wm_latency(dev_priv, "Gen9 Plane", dev_priv->display.wm.skl_latency);
3157 static bool ilk_validate_pipe_wm(const struct drm_i915_private *dev_priv,
3158 struct intel_pipe_wm *pipe_wm)
3160 /* LP0 watermark maximums depend on this pipe alone */
3161 const struct intel_wm_config config = {
3162 .num_pipes_active = 1,
3163 .sprites_enabled = pipe_wm->sprites_enabled,
3164 .sprites_scaled = pipe_wm->sprites_scaled,
3166 struct ilk_wm_maximums max;
3168 /* LP0 watermarks always use 1/2 DDB partitioning */
3169 ilk_compute_wm_maximums(dev_priv, 0, &config, INTEL_DDB_PART_1_2, &max);
3171 /* At least LP0 must be valid */
3172 if (!ilk_validate_wm_level(0, &max, &pipe_wm->wm[0])) {
3173 drm_dbg_kms(&dev_priv->drm, "LP0 watermark invalid\n");
3180 /* Compute new watermarks for the pipe */
3181 static int ilk_compute_pipe_wm(struct intel_atomic_state *state,
3182 struct intel_crtc *crtc)
3184 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
3185 struct intel_crtc_state *crtc_state =
3186 intel_atomic_get_new_crtc_state(state, crtc);
3187 struct intel_pipe_wm *pipe_wm;
3188 struct intel_plane *plane;
3189 const struct intel_plane_state *plane_state;
3190 const struct intel_plane_state *pristate = NULL;
3191 const struct intel_plane_state *sprstate = NULL;
3192 const struct intel_plane_state *curstate = NULL;
3193 int level, max_level = ilk_wm_max_level(dev_priv), usable_level;
3194 struct ilk_wm_maximums max;
3196 pipe_wm = &crtc_state->wm.ilk.optimal;
3198 intel_atomic_crtc_state_for_each_plane_state(plane, plane_state, crtc_state) {
3199 if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
3200 pristate = plane_state;
3201 else if (plane->base.type == DRM_PLANE_TYPE_OVERLAY)
3202 sprstate = plane_state;
3203 else if (plane->base.type == DRM_PLANE_TYPE_CURSOR)
3204 curstate = plane_state;
3207 pipe_wm->pipe_enabled = crtc_state->hw.active;
3208 pipe_wm->sprites_enabled = crtc_state->active_planes & BIT(PLANE_SPRITE0);
3209 pipe_wm->sprites_scaled = crtc_state->scaled_planes & BIT(PLANE_SPRITE0);
3211 usable_level = max_level;
3213 /* ILK/SNB: LP2+ watermarks only w/o sprites */
3214 if (DISPLAY_VER(dev_priv) <= 6 && pipe_wm->sprites_enabled)
3217 /* ILK/SNB/IVB: LP1+ watermarks only w/o scaling */
3218 if (pipe_wm->sprites_scaled)
3221 memset(&pipe_wm->wm, 0, sizeof(pipe_wm->wm));
3222 ilk_compute_wm_level(dev_priv, crtc, 0, crtc_state,
3223 pristate, sprstate, curstate, &pipe_wm->wm[0]);
3225 if (!ilk_validate_pipe_wm(dev_priv, pipe_wm))
3228 ilk_compute_wm_reg_maximums(dev_priv, 1, &max);
3230 for (level = 1; level <= usable_level; level++) {
3231 struct intel_wm_level *wm = &pipe_wm->wm[level];
3233 ilk_compute_wm_level(dev_priv, crtc, level, crtc_state,
3234 pristate, sprstate, curstate, wm);
3237 * Disable any watermark level that exceeds the
3238 * register maximums since such watermarks are
3241 if (!ilk_validate_wm_level(level, &max, wm)) {
3242 memset(wm, 0, sizeof(*wm));
3251 * Build a set of 'intermediate' watermark values that satisfy both the old
3252 * state and the new state. These can be programmed to the hardware
3255 static int ilk_compute_intermediate_wm(struct intel_atomic_state *state,
3256 struct intel_crtc *crtc)
3258 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
3259 struct intel_crtc_state *new_crtc_state =
3260 intel_atomic_get_new_crtc_state(state, crtc);
3261 const struct intel_crtc_state *old_crtc_state =
3262 intel_atomic_get_old_crtc_state(state, crtc);
3263 struct intel_pipe_wm *a = &new_crtc_state->wm.ilk.intermediate;
3264 const struct intel_pipe_wm *b = &old_crtc_state->wm.ilk.optimal;
3265 int level, max_level = ilk_wm_max_level(dev_priv);
3268 * Start with the final, target watermarks, then combine with the
3269 * currently active watermarks to get values that are safe both before
3270 * and after the vblank.
3272 *a = new_crtc_state->wm.ilk.optimal;
3273 if (!new_crtc_state->hw.active ||
3274 drm_atomic_crtc_needs_modeset(&new_crtc_state->uapi) ||
3275 state->skip_intermediate_wm)
3278 a->pipe_enabled |= b->pipe_enabled;
3279 a->sprites_enabled |= b->sprites_enabled;
3280 a->sprites_scaled |= b->sprites_scaled;
3282 for (level = 0; level <= max_level; level++) {
3283 struct intel_wm_level *a_wm = &a->wm[level];
3284 const struct intel_wm_level *b_wm = &b->wm[level];
3286 a_wm->enable &= b_wm->enable;
3287 a_wm->pri_val = max(a_wm->pri_val, b_wm->pri_val);
3288 a_wm->spr_val = max(a_wm->spr_val, b_wm->spr_val);
3289 a_wm->cur_val = max(a_wm->cur_val, b_wm->cur_val);
3290 a_wm->fbc_val = max(a_wm->fbc_val, b_wm->fbc_val);
3294 * We need to make sure that these merged watermark values are
3295 * actually a valid configuration themselves. If they're not,
3296 * there's no safe way to transition from the old state to
3297 * the new state, so we need to fail the atomic transaction.
3299 if (!ilk_validate_pipe_wm(dev_priv, a))
3303 * If our intermediate WM are identical to the final WM, then we can
3304 * omit the post-vblank programming; only update if it's different.
3306 if (memcmp(a, &new_crtc_state->wm.ilk.optimal, sizeof(*a)) != 0)
3307 new_crtc_state->wm.need_postvbl_update = true;
3313 * Merge the watermarks from all active pipes for a specific level.
3315 static void ilk_merge_wm_level(struct drm_i915_private *dev_priv,
3317 struct intel_wm_level *ret_wm)
3319 const struct intel_crtc *crtc;
3321 ret_wm->enable = true;
3323 for_each_intel_crtc(&dev_priv->drm, crtc) {
3324 const struct intel_pipe_wm *active = &crtc->wm.active.ilk;
3325 const struct intel_wm_level *wm = &active->wm[level];
3327 if (!active->pipe_enabled)
3331 * The watermark values may have been used in the past,
3332 * so we must maintain them in the registers for some
3333 * time even if the level is now disabled.
3336 ret_wm->enable = false;
3338 ret_wm->pri_val = max(ret_wm->pri_val, wm->pri_val);
3339 ret_wm->spr_val = max(ret_wm->spr_val, wm->spr_val);
3340 ret_wm->cur_val = max(ret_wm->cur_val, wm->cur_val);
3341 ret_wm->fbc_val = max(ret_wm->fbc_val, wm->fbc_val);
3346 * Merge all low power watermarks for all active pipes.
3348 static void ilk_wm_merge(struct drm_i915_private *dev_priv,
3349 const struct intel_wm_config *config,
3350 const struct ilk_wm_maximums *max,
3351 struct intel_pipe_wm *merged)
3353 int level, max_level = ilk_wm_max_level(dev_priv);
3354 int last_enabled_level = max_level;
3356 /* ILK/SNB/IVB: LP1+ watermarks only w/ single pipe */
3357 if ((DISPLAY_VER(dev_priv) <= 6 || IS_IVYBRIDGE(dev_priv)) &&
3358 config->num_pipes_active > 1)
3359 last_enabled_level = 0;
3361 /* ILK: FBC WM must be disabled always */
3362 merged->fbc_wm_enabled = DISPLAY_VER(dev_priv) >= 6;
3364 /* merge each WM1+ level */
3365 for (level = 1; level <= max_level; level++) {
3366 struct intel_wm_level *wm = &merged->wm[level];
3368 ilk_merge_wm_level(dev_priv, level, wm);
3370 if (level > last_enabled_level)
3372 else if (!ilk_validate_wm_level(level, max, wm))
3373 /* make sure all following levels get disabled */
3374 last_enabled_level = level - 1;
3377 * The spec says it is preferred to disable
3378 * FBC WMs instead of disabling a WM level.
3380 if (wm->fbc_val > max->fbc) {
3382 merged->fbc_wm_enabled = false;
3387 /* ILK: LP2+ must be disabled when FBC WM is disabled but FBC enabled */
3388 if (DISPLAY_VER(dev_priv) == 5 && HAS_FBC(dev_priv) &&
3389 dev_priv->params.enable_fbc && !merged->fbc_wm_enabled) {
3390 for (level = 2; level <= max_level; level++) {
3391 struct intel_wm_level *wm = &merged->wm[level];
3398 static int ilk_wm_lp_to_level(int wm_lp, const struct intel_pipe_wm *pipe_wm)
3400 /* LP1,LP2,LP3 levels are either 1,2,3 or 1,3,4 */
3401 return wm_lp + (wm_lp >= 2 && pipe_wm->wm[4].enable);
3404 /* The value we need to program into the WM_LPx latency field */
3405 static unsigned int ilk_wm_lp_latency(struct drm_i915_private *dev_priv,
3408 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
3411 return dev_priv->display.wm.pri_latency[level];
3414 static void ilk_compute_wm_results(struct drm_i915_private *dev_priv,
3415 const struct intel_pipe_wm *merged,
3416 enum intel_ddb_partitioning partitioning,
3417 struct ilk_wm_values *results)
3419 struct intel_crtc *crtc;
3422 results->enable_fbc_wm = merged->fbc_wm_enabled;
3423 results->partitioning = partitioning;
3425 /* LP1+ register values */
3426 for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
3427 const struct intel_wm_level *r;
3429 level = ilk_wm_lp_to_level(wm_lp, merged);
3431 r = &merged->wm[level];
3434 * Maintain the watermark values even if the level is
3435 * disabled. Doing otherwise could cause underruns.
3437 results->wm_lp[wm_lp - 1] =
3438 WM_LP_LATENCY(ilk_wm_lp_latency(dev_priv, level)) |
3439 WM_LP_PRIMARY(r->pri_val) |
3440 WM_LP_CURSOR(r->cur_val);
3443 results->wm_lp[wm_lp - 1] |= WM_LP_ENABLE;
3445 if (DISPLAY_VER(dev_priv) >= 8)
3446 results->wm_lp[wm_lp - 1] |= WM_LP_FBC_BDW(r->fbc_val);
3448 results->wm_lp[wm_lp - 1] |= WM_LP_FBC_ILK(r->fbc_val);
3450 results->wm_lp_spr[wm_lp - 1] = WM_LP_SPRITE(r->spr_val);
3453 * Always set WM_LP_SPRITE_EN when spr_val != 0, even if the
3454 * level is disabled. Doing otherwise could cause underruns.
3456 if (DISPLAY_VER(dev_priv) <= 6 && r->spr_val) {
3457 drm_WARN_ON(&dev_priv->drm, wm_lp != 1);
3458 results->wm_lp_spr[wm_lp - 1] |= WM_LP_SPRITE_ENABLE;
3462 /* LP0 register values */
3463 for_each_intel_crtc(&dev_priv->drm, crtc) {
3464 enum pipe pipe = crtc->pipe;
3465 const struct intel_pipe_wm *pipe_wm = &crtc->wm.active.ilk;
3466 const struct intel_wm_level *r = &pipe_wm->wm[0];
3468 if (drm_WARN_ON(&dev_priv->drm, !r->enable))
3471 results->wm_pipe[pipe] =
3472 WM0_PIPE_PRIMARY(r->pri_val) |
3473 WM0_PIPE_SPRITE(r->spr_val) |
3474 WM0_PIPE_CURSOR(r->cur_val);
3478 /* Find the result with the highest level enabled. Check for enable_fbc_wm in
3479 * case both are at the same level. Prefer r1 in case they're the same. */
3480 static struct intel_pipe_wm *
3481 ilk_find_best_result(struct drm_i915_private *dev_priv,
3482 struct intel_pipe_wm *r1,
3483 struct intel_pipe_wm *r2)
3485 int level, max_level = ilk_wm_max_level(dev_priv);
3486 int level1 = 0, level2 = 0;
3488 for (level = 1; level <= max_level; level++) {
3489 if (r1->wm[level].enable)
3491 if (r2->wm[level].enable)
3495 if (level1 == level2) {
3496 if (r2->fbc_wm_enabled && !r1->fbc_wm_enabled)
3500 } else if (level1 > level2) {
3507 /* dirty bits used to track which watermarks need changes */
3508 #define WM_DIRTY_PIPE(pipe) (1 << (pipe))
3509 #define WM_DIRTY_LP(wm_lp) (1 << (15 + (wm_lp)))
3510 #define WM_DIRTY_LP_ALL (WM_DIRTY_LP(1) | WM_DIRTY_LP(2) | WM_DIRTY_LP(3))
3511 #define WM_DIRTY_FBC (1 << 24)
3512 #define WM_DIRTY_DDB (1 << 25)
3514 static unsigned int ilk_compute_wm_dirty(struct drm_i915_private *dev_priv,
3515 const struct ilk_wm_values *old,
3516 const struct ilk_wm_values *new)
3518 unsigned int dirty = 0;
3522 for_each_pipe(dev_priv, pipe) {
3523 if (old->wm_pipe[pipe] != new->wm_pipe[pipe]) {
3524 dirty |= WM_DIRTY_PIPE(pipe);
3525 /* Must disable LP1+ watermarks too */
3526 dirty |= WM_DIRTY_LP_ALL;
3530 if (old->enable_fbc_wm != new->enable_fbc_wm) {
3531 dirty |= WM_DIRTY_FBC;
3532 /* Must disable LP1+ watermarks too */
3533 dirty |= WM_DIRTY_LP_ALL;
3536 if (old->partitioning != new->partitioning) {
3537 dirty |= WM_DIRTY_DDB;
3538 /* Must disable LP1+ watermarks too */
3539 dirty |= WM_DIRTY_LP_ALL;
3542 /* LP1+ watermarks already deemed dirty, no need to continue */
3543 if (dirty & WM_DIRTY_LP_ALL)
3546 /* Find the lowest numbered LP1+ watermark in need of an update... */
3547 for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
3548 if (old->wm_lp[wm_lp - 1] != new->wm_lp[wm_lp - 1] ||
3549 old->wm_lp_spr[wm_lp - 1] != new->wm_lp_spr[wm_lp - 1])
3553 /* ...and mark it and all higher numbered LP1+ watermarks as dirty */
3554 for (; wm_lp <= 3; wm_lp++)
3555 dirty |= WM_DIRTY_LP(wm_lp);
3560 static bool _ilk_disable_lp_wm(struct drm_i915_private *dev_priv,
3563 struct ilk_wm_values *previous = &dev_priv->display.wm.hw;
3564 bool changed = false;
3566 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] & WM_LP_ENABLE) {
3567 previous->wm_lp[2] &= ~WM_LP_ENABLE;
3568 intel_uncore_write(&dev_priv->uncore, WM3_LP_ILK, previous->wm_lp[2]);
3571 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] & WM_LP_ENABLE) {
3572 previous->wm_lp[1] &= ~WM_LP_ENABLE;
3573 intel_uncore_write(&dev_priv->uncore, WM2_LP_ILK, previous->wm_lp[1]);
3576 if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] & WM_LP_ENABLE) {
3577 previous->wm_lp[0] &= ~WM_LP_ENABLE;
3578 intel_uncore_write(&dev_priv->uncore, WM1_LP_ILK, previous->wm_lp[0]);
3583 * Don't touch WM_LP_SPRITE_ENABLE here.
3584 * Doing so could cause underruns.
3591 * The spec says we shouldn't write when we don't need, because every write
3592 * causes WMs to be re-evaluated, expending some power.
3594 static void ilk_write_wm_values(struct drm_i915_private *dev_priv,
3595 struct ilk_wm_values *results)
3597 struct ilk_wm_values *previous = &dev_priv->display.wm.hw;
3601 dirty = ilk_compute_wm_dirty(dev_priv, previous, results);
3605 _ilk_disable_lp_wm(dev_priv, dirty);
3607 if (dirty & WM_DIRTY_PIPE(PIPE_A))
3608 intel_uncore_write(&dev_priv->uncore, WM0_PIPE_ILK(PIPE_A), results->wm_pipe[0]);
3609 if (dirty & WM_DIRTY_PIPE(PIPE_B))
3610 intel_uncore_write(&dev_priv->uncore, WM0_PIPE_ILK(PIPE_B), results->wm_pipe[1]);
3611 if (dirty & WM_DIRTY_PIPE(PIPE_C))
3612 intel_uncore_write(&dev_priv->uncore, WM0_PIPE_ILK(PIPE_C), results->wm_pipe[2]);
3614 if (dirty & WM_DIRTY_DDB) {
3615 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
3616 val = intel_uncore_read(&dev_priv->uncore, WM_MISC);
3617 if (results->partitioning == INTEL_DDB_PART_1_2)
3618 val &= ~WM_MISC_DATA_PARTITION_5_6;
3620 val |= WM_MISC_DATA_PARTITION_5_6;
3621 intel_uncore_write(&dev_priv->uncore, WM_MISC, val);
3623 val = intel_uncore_read(&dev_priv->uncore, DISP_ARB_CTL2);
3624 if (results->partitioning == INTEL_DDB_PART_1_2)
3625 val &= ~DISP_DATA_PARTITION_5_6;
3627 val |= DISP_DATA_PARTITION_5_6;
3628 intel_uncore_write(&dev_priv->uncore, DISP_ARB_CTL2, val);
3632 if (dirty & WM_DIRTY_FBC) {
3633 val = intel_uncore_read(&dev_priv->uncore, DISP_ARB_CTL);
3634 if (results->enable_fbc_wm)
3635 val &= ~DISP_FBC_WM_DIS;
3637 val |= DISP_FBC_WM_DIS;
3638 intel_uncore_write(&dev_priv->uncore, DISP_ARB_CTL, val);
3641 if (dirty & WM_DIRTY_LP(1) &&
3642 previous->wm_lp_spr[0] != results->wm_lp_spr[0])
3643 intel_uncore_write(&dev_priv->uncore, WM1S_LP_ILK, results->wm_lp_spr[0]);
3645 if (DISPLAY_VER(dev_priv) >= 7) {
3646 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp_spr[1] != results->wm_lp_spr[1])
3647 intel_uncore_write(&dev_priv->uncore, WM2S_LP_IVB, results->wm_lp_spr[1]);
3648 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp_spr[2] != results->wm_lp_spr[2])
3649 intel_uncore_write(&dev_priv->uncore, WM3S_LP_IVB, results->wm_lp_spr[2]);
3652 if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] != results->wm_lp[0])
3653 intel_uncore_write(&dev_priv->uncore, WM1_LP_ILK, results->wm_lp[0]);
3654 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] != results->wm_lp[1])
3655 intel_uncore_write(&dev_priv->uncore, WM2_LP_ILK, results->wm_lp[1]);
3656 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] != results->wm_lp[2])
3657 intel_uncore_write(&dev_priv->uncore, WM3_LP_ILK, results->wm_lp[2]);
3659 dev_priv->display.wm.hw = *results;
3662 bool ilk_disable_lp_wm(struct drm_i915_private *dev_priv)
3664 return _ilk_disable_lp_wm(dev_priv, WM_DIRTY_LP_ALL);
3667 u8 intel_enabled_dbuf_slices_mask(struct drm_i915_private *dev_priv)
3669 u8 enabled_slices = 0;
3670 enum dbuf_slice slice;
3672 for_each_dbuf_slice(dev_priv, slice) {
3673 if (intel_uncore_read(&dev_priv->uncore,
3674 DBUF_CTL_S(slice)) & DBUF_POWER_STATE)
3675 enabled_slices |= BIT(slice);
3678 return enabled_slices;
3682 * FIXME: We still don't have the proper code detect if we need to apply the WA,
3683 * so assume we'll always need it in order to avoid underruns.
3685 static bool skl_needs_memory_bw_wa(struct drm_i915_private *dev_priv)
3687 return DISPLAY_VER(dev_priv) == 9;
3691 intel_has_sagv(struct drm_i915_private *dev_priv)
3693 return DISPLAY_VER(dev_priv) >= 9 && !IS_LP(dev_priv) &&
3694 dev_priv->display.sagv.status != I915_SAGV_NOT_CONTROLLED;
3698 intel_sagv_block_time(struct drm_i915_private *dev_priv)
3700 if (DISPLAY_VER(dev_priv) >= 12) {
3704 ret = snb_pcode_read(&dev_priv->uncore,
3705 GEN12_PCODE_READ_SAGV_BLOCK_TIME_US,
3708 drm_dbg_kms(&dev_priv->drm, "Couldn't read SAGV block time!\n");
3713 } else if (DISPLAY_VER(dev_priv) == 11) {
3715 } else if (DISPLAY_VER(dev_priv) == 9 && !IS_LP(dev_priv)) {
3722 static void intel_sagv_init(struct drm_i915_private *i915)
3724 if (!intel_has_sagv(i915))
3725 i915->display.sagv.status = I915_SAGV_NOT_CONTROLLED;
3728 * Probe to see if we have working SAGV control.
3729 * For icl+ this was already determined by intel_bw_init_hw().
3731 if (DISPLAY_VER(i915) < 11)
3732 skl_sagv_disable(i915);
3734 drm_WARN_ON(&i915->drm, i915->display.sagv.status == I915_SAGV_UNKNOWN);
3736 i915->display.sagv.block_time_us = intel_sagv_block_time(i915);
3738 drm_dbg_kms(&i915->drm, "SAGV supported: %s, original SAGV block time: %u us\n",
3739 str_yes_no(intel_has_sagv(i915)), i915->display.sagv.block_time_us);
3741 /* avoid overflow when adding with wm0 latency/etc. */
3742 if (drm_WARN(&i915->drm, i915->display.sagv.block_time_us > U16_MAX,
3743 "Excessive SAGV block time %u, ignoring\n",
3744 i915->display.sagv.block_time_us))
3745 i915->display.sagv.block_time_us = 0;
3747 if (!intel_has_sagv(i915))
3748 i915->display.sagv.block_time_us = 0;
3752 * SAGV dynamically adjusts the system agent voltage and clock frequencies
3753 * depending on power and performance requirements. The display engine access
3754 * to system memory is blocked during the adjustment time. Because of the
3755 * blocking time, having this enabled can cause full system hangs and/or pipe
3756 * underruns if we don't meet all of the following requirements:
3758 * - <= 1 pipe enabled
3759 * - All planes can enable watermarks for latencies >= SAGV engine block time
3760 * - We're not using an interlaced display configuration
3762 static void skl_sagv_enable(struct drm_i915_private *dev_priv)
3766 if (!intel_has_sagv(dev_priv))
3769 if (dev_priv->display.sagv.status == I915_SAGV_ENABLED)
3772 drm_dbg_kms(&dev_priv->drm, "Enabling SAGV\n");
3773 ret = snb_pcode_write(&dev_priv->uncore, GEN9_PCODE_SAGV_CONTROL,
3776 /* We don't need to wait for SAGV when enabling */
3779 * Some skl systems, pre-release machines in particular,
3780 * don't actually have SAGV.
3782 if (IS_SKYLAKE(dev_priv) && ret == -ENXIO) {
3783 drm_dbg(&dev_priv->drm, "No SAGV found on system, ignoring\n");
3784 dev_priv->display.sagv.status = I915_SAGV_NOT_CONTROLLED;
3786 } else if (ret < 0) {
3787 drm_err(&dev_priv->drm, "Failed to enable SAGV\n");
3791 dev_priv->display.sagv.status = I915_SAGV_ENABLED;
3794 static void skl_sagv_disable(struct drm_i915_private *dev_priv)
3798 if (!intel_has_sagv(dev_priv))
3801 if (dev_priv->display.sagv.status == I915_SAGV_DISABLED)
3804 drm_dbg_kms(&dev_priv->drm, "Disabling SAGV\n");
3805 /* bspec says to keep retrying for at least 1 ms */
3806 ret = skl_pcode_request(&dev_priv->uncore, GEN9_PCODE_SAGV_CONTROL,
3808 GEN9_SAGV_IS_DISABLED, GEN9_SAGV_IS_DISABLED,
3811 * Some skl systems, pre-release machines in particular,
3812 * don't actually have SAGV.
3814 if (IS_SKYLAKE(dev_priv) && ret == -ENXIO) {
3815 drm_dbg(&dev_priv->drm, "No SAGV found on system, ignoring\n");
3816 dev_priv->display.sagv.status = I915_SAGV_NOT_CONTROLLED;
3818 } else if (ret < 0) {
3819 drm_err(&dev_priv->drm, "Failed to disable SAGV (%d)\n", ret);
3823 dev_priv->display.sagv.status = I915_SAGV_DISABLED;
3826 static void skl_sagv_pre_plane_update(struct intel_atomic_state *state)
3828 struct drm_i915_private *i915 = to_i915(state->base.dev);
3829 const struct intel_bw_state *new_bw_state =
3830 intel_atomic_get_new_bw_state(state);
3835 if (!intel_can_enable_sagv(i915, new_bw_state))
3836 skl_sagv_disable(i915);
3839 static void skl_sagv_post_plane_update(struct intel_atomic_state *state)
3841 struct drm_i915_private *i915 = to_i915(state->base.dev);
3842 const struct intel_bw_state *new_bw_state =
3843 intel_atomic_get_new_bw_state(state);
3848 if (intel_can_enable_sagv(i915, new_bw_state))
3849 skl_sagv_enable(i915);
3852 static void icl_sagv_pre_plane_update(struct intel_atomic_state *state)
3854 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
3855 const struct intel_bw_state *old_bw_state =
3856 intel_atomic_get_old_bw_state(state);
3857 const struct intel_bw_state *new_bw_state =
3858 intel_atomic_get_new_bw_state(state);
3859 u16 old_mask, new_mask;
3864 old_mask = old_bw_state->qgv_points_mask;
3865 new_mask = old_bw_state->qgv_points_mask | new_bw_state->qgv_points_mask;
3867 if (old_mask == new_mask)
3870 WARN_ON(!new_bw_state->base.changed);
3872 drm_dbg_kms(&dev_priv->drm, "Restricting QGV points: 0x%x -> 0x%x\n",
3873 old_mask, new_mask);
3876 * Restrict required qgv points before updating the configuration.
3877 * According to BSpec we can't mask and unmask qgv points at the same
3878 * time. Also masking should be done before updating the configuration
3879 * and unmasking afterwards.
3881 icl_pcode_restrict_qgv_points(dev_priv, new_mask);
3884 static void icl_sagv_post_plane_update(struct intel_atomic_state *state)
3886 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
3887 const struct intel_bw_state *old_bw_state =
3888 intel_atomic_get_old_bw_state(state);
3889 const struct intel_bw_state *new_bw_state =
3890 intel_atomic_get_new_bw_state(state);
3891 u16 old_mask, new_mask;
3896 old_mask = old_bw_state->qgv_points_mask | new_bw_state->qgv_points_mask;
3897 new_mask = new_bw_state->qgv_points_mask;
3899 if (old_mask == new_mask)
3902 WARN_ON(!new_bw_state->base.changed);
3904 drm_dbg_kms(&dev_priv->drm, "Relaxing QGV points: 0x%x -> 0x%x\n",
3905 old_mask, new_mask);
3908 * Allow required qgv points after updating the configuration.
3909 * According to BSpec we can't mask and unmask qgv points at the same
3910 * time. Also masking should be done before updating the configuration
3911 * and unmasking afterwards.
3913 icl_pcode_restrict_qgv_points(dev_priv, new_mask);
3916 void intel_sagv_pre_plane_update(struct intel_atomic_state *state)
3918 struct drm_i915_private *i915 = to_i915(state->base.dev);
3921 * Just return if we can't control SAGV or don't have it.
3922 * This is different from situation when we have SAGV but just can't
3923 * afford it due to DBuf limitation - in case if SAGV is completely
3924 * disabled in a BIOS, we are not even allowed to send a PCode request,
3925 * as it will throw an error. So have to check it here.
3927 if (!intel_has_sagv(i915))
3930 if (DISPLAY_VER(i915) >= 11)
3931 icl_sagv_pre_plane_update(state);
3933 skl_sagv_pre_plane_update(state);
3936 void intel_sagv_post_plane_update(struct intel_atomic_state *state)
3938 struct drm_i915_private *i915 = to_i915(state->base.dev);
3941 * Just return if we can't control SAGV or don't have it.
3942 * This is different from situation when we have SAGV but just can't
3943 * afford it due to DBuf limitation - in case if SAGV is completely
3944 * disabled in a BIOS, we are not even allowed to send a PCode request,
3945 * as it will throw an error. So have to check it here.
3947 if (!intel_has_sagv(i915))
3950 if (DISPLAY_VER(i915) >= 11)
3951 icl_sagv_post_plane_update(state);
3953 skl_sagv_post_plane_update(state);
3956 static bool skl_crtc_can_enable_sagv(const struct intel_crtc_state *crtc_state)
3958 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
3959 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
3960 enum plane_id plane_id;
3961 int max_level = INT_MAX;
3963 if (!intel_has_sagv(dev_priv))
3966 if (!crtc_state->hw.active)
3969 if (crtc_state->hw.pipe_mode.flags & DRM_MODE_FLAG_INTERLACE)
3972 for_each_plane_id_on_crtc(crtc, plane_id) {
3973 const struct skl_plane_wm *wm =
3974 &crtc_state->wm.skl.optimal.planes[plane_id];
3977 /* Skip this plane if it's not enabled */
3978 if (!wm->wm[0].enable)
3981 /* Find the highest enabled wm level for this plane */
3982 for (level = ilk_wm_max_level(dev_priv);
3983 !wm->wm[level].enable; --level)
3986 /* Highest common enabled wm level for all planes */
3987 max_level = min(level, max_level);
3990 /* No enabled planes? */
3991 if (max_level == INT_MAX)
3994 for_each_plane_id_on_crtc(crtc, plane_id) {
3995 const struct skl_plane_wm *wm =
3996 &crtc_state->wm.skl.optimal.planes[plane_id];
3999 * All enabled planes must have enabled a common wm level that
4000 * can tolerate memory latencies higher than sagv_block_time_us
4002 if (wm->wm[0].enable && !wm->wm[max_level].can_sagv)
4009 static bool tgl_crtc_can_enable_sagv(const struct intel_crtc_state *crtc_state)
4011 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
4012 enum plane_id plane_id;
4014 if (!crtc_state->hw.active)
4017 for_each_plane_id_on_crtc(crtc, plane_id) {
4018 const struct skl_plane_wm *wm =
4019 &crtc_state->wm.skl.optimal.planes[plane_id];
4021 if (wm->wm[0].enable && !wm->sagv.wm0.enable)
4028 static bool intel_crtc_can_enable_sagv(const struct intel_crtc_state *crtc_state)
4030 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
4031 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
4033 if (DISPLAY_VER(dev_priv) >= 12)
4034 return tgl_crtc_can_enable_sagv(crtc_state);
4036 return skl_crtc_can_enable_sagv(crtc_state);
4039 bool intel_can_enable_sagv(struct drm_i915_private *dev_priv,
4040 const struct intel_bw_state *bw_state)
4042 if (DISPLAY_VER(dev_priv) < 11 &&
4043 bw_state->active_pipes && !is_power_of_2(bw_state->active_pipes))
4046 return bw_state->pipe_sagv_reject == 0;
4049 static int intel_compute_sagv_mask(struct intel_atomic_state *state)
4051 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
4053 struct intel_crtc *crtc;
4054 struct intel_crtc_state *new_crtc_state;
4055 struct intel_bw_state *new_bw_state = NULL;
4056 const struct intel_bw_state *old_bw_state = NULL;
4059 for_each_new_intel_crtc_in_state(state, crtc,
4060 new_crtc_state, i) {
4061 new_bw_state = intel_atomic_get_bw_state(state);
4062 if (IS_ERR(new_bw_state))
4063 return PTR_ERR(new_bw_state);
4065 old_bw_state = intel_atomic_get_old_bw_state(state);
4067 if (intel_crtc_can_enable_sagv(new_crtc_state))
4068 new_bw_state->pipe_sagv_reject &= ~BIT(crtc->pipe);
4070 new_bw_state->pipe_sagv_reject |= BIT(crtc->pipe);
4076 new_bw_state->active_pipes =
4077 intel_calc_active_pipes(state, old_bw_state->active_pipes);
4079 if (new_bw_state->active_pipes != old_bw_state->active_pipes) {
4080 ret = intel_atomic_lock_global_state(&new_bw_state->base);
4085 if (intel_can_enable_sagv(dev_priv, new_bw_state) !=
4086 intel_can_enable_sagv(dev_priv, old_bw_state)) {
4087 ret = intel_atomic_serialize_global_state(&new_bw_state->base);
4090 } else if (new_bw_state->pipe_sagv_reject != old_bw_state->pipe_sagv_reject) {
4091 ret = intel_atomic_lock_global_state(&new_bw_state->base);
4096 for_each_new_intel_crtc_in_state(state, crtc,
4097 new_crtc_state, i) {
4098 struct skl_pipe_wm *pipe_wm = &new_crtc_state->wm.skl.optimal;
4101 * We store use_sagv_wm in the crtc state rather than relying on
4102 * that bw state since we have no convenient way to get at the
4103 * latter from the plane commit hooks (especially in the legacy
4106 pipe_wm->use_sagv_wm = !HAS_HW_SAGV_WM(dev_priv) &&
4107 DISPLAY_VER(dev_priv) >= 12 &&
4108 intel_can_enable_sagv(dev_priv, new_bw_state);
4114 static u16 skl_ddb_entry_init(struct skl_ddb_entry *entry,
4117 entry->start = start;
4123 static int intel_dbuf_slice_size(struct drm_i915_private *dev_priv)
4125 return INTEL_INFO(dev_priv)->display.dbuf.size /
4126 hweight8(INTEL_INFO(dev_priv)->display.dbuf.slice_mask);
4130 skl_ddb_entry_for_slices(struct drm_i915_private *dev_priv, u8 slice_mask,
4131 struct skl_ddb_entry *ddb)
4133 int slice_size = intel_dbuf_slice_size(dev_priv);
4141 ddb->start = (ffs(slice_mask) - 1) * slice_size;
4142 ddb->end = fls(slice_mask) * slice_size;
4144 WARN_ON(ddb->start >= ddb->end);
4145 WARN_ON(ddb->end > INTEL_INFO(dev_priv)->display.dbuf.size);
4148 static unsigned int mbus_ddb_offset(struct drm_i915_private *i915, u8 slice_mask)
4150 struct skl_ddb_entry ddb;
4152 if (slice_mask & (BIT(DBUF_S1) | BIT(DBUF_S2)))
4153 slice_mask = BIT(DBUF_S1);
4154 else if (slice_mask & (BIT(DBUF_S3) | BIT(DBUF_S4)))
4155 slice_mask = BIT(DBUF_S3);
4157 skl_ddb_entry_for_slices(i915, slice_mask, &ddb);
4162 u32 skl_ddb_dbuf_slice_mask(struct drm_i915_private *dev_priv,
4163 const struct skl_ddb_entry *entry)
4165 int slice_size = intel_dbuf_slice_size(dev_priv);
4166 enum dbuf_slice start_slice, end_slice;
4169 if (!skl_ddb_entry_size(entry))
4172 start_slice = entry->start / slice_size;
4173 end_slice = (entry->end - 1) / slice_size;
4176 * Per plane DDB entry can in a really worst case be on multiple slices
4177 * but single entry is anyway contigious.
4179 while (start_slice <= end_slice) {
4180 slice_mask |= BIT(start_slice);
4187 static unsigned int intel_crtc_ddb_weight(const struct intel_crtc_state *crtc_state)
4189 const struct drm_display_mode *pipe_mode = &crtc_state->hw.pipe_mode;
4190 int hdisplay, vdisplay;
4192 if (!crtc_state->hw.active)
4196 * Watermark/ddb requirement highly depends upon width of the
4197 * framebuffer, So instead of allocating DDB equally among pipes
4198 * distribute DDB based on resolution/width of the display.
4200 drm_mode_get_hv_timing(pipe_mode, &hdisplay, &vdisplay);
4205 static void intel_crtc_dbuf_weights(const struct intel_dbuf_state *dbuf_state,
4207 unsigned int *weight_start,
4208 unsigned int *weight_end,
4209 unsigned int *weight_total)
4211 struct drm_i915_private *dev_priv =
4212 to_i915(dbuf_state->base.state->base.dev);
4219 for_each_pipe(dev_priv, pipe) {
4220 int weight = dbuf_state->weight[pipe];
4223 * Do not account pipes using other slice sets
4224 * luckily as of current BSpec slice sets do not partially
4225 * intersect(pipes share either same one slice or same slice set
4226 * i.e no partial intersection), so it is enough to check for
4229 if (dbuf_state->slices[pipe] != dbuf_state->slices[for_pipe])
4232 *weight_total += weight;
4233 if (pipe < for_pipe) {
4234 *weight_start += weight;
4235 *weight_end += weight;
4236 } else if (pipe == for_pipe) {
4237 *weight_end += weight;
4243 skl_crtc_allocate_ddb(struct intel_atomic_state *state, struct intel_crtc *crtc)
4245 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
4246 unsigned int weight_total, weight_start, weight_end;
4247 const struct intel_dbuf_state *old_dbuf_state =
4248 intel_atomic_get_old_dbuf_state(state);
4249 struct intel_dbuf_state *new_dbuf_state =
4250 intel_atomic_get_new_dbuf_state(state);
4251 struct intel_crtc_state *crtc_state;
4252 struct skl_ddb_entry ddb_slices;
4253 enum pipe pipe = crtc->pipe;
4254 unsigned int mbus_offset = 0;
4256 u32 dbuf_slice_mask;
4260 if (new_dbuf_state->weight[pipe] == 0) {
4261 skl_ddb_entry_init(&new_dbuf_state->ddb[pipe], 0, 0);
4265 dbuf_slice_mask = new_dbuf_state->slices[pipe];
4267 skl_ddb_entry_for_slices(dev_priv, dbuf_slice_mask, &ddb_slices);
4268 mbus_offset = mbus_ddb_offset(dev_priv, dbuf_slice_mask);
4269 ddb_range_size = skl_ddb_entry_size(&ddb_slices);
4271 intel_crtc_dbuf_weights(new_dbuf_state, pipe,
4272 &weight_start, &weight_end, &weight_total);
4274 start = ddb_range_size * weight_start / weight_total;
4275 end = ddb_range_size * weight_end / weight_total;
4277 skl_ddb_entry_init(&new_dbuf_state->ddb[pipe],
4278 ddb_slices.start - mbus_offset + start,
4279 ddb_slices.start - mbus_offset + end);
4282 if (old_dbuf_state->slices[pipe] == new_dbuf_state->slices[pipe] &&
4283 skl_ddb_entry_equal(&old_dbuf_state->ddb[pipe],
4284 &new_dbuf_state->ddb[pipe]))
4287 ret = intel_atomic_lock_global_state(&new_dbuf_state->base);
4291 crtc_state = intel_atomic_get_crtc_state(&state->base, crtc);
4292 if (IS_ERR(crtc_state))
4293 return PTR_ERR(crtc_state);
4296 * Used for checking overlaps, so we need absolute
4297 * offsets instead of MBUS relative offsets.
4299 crtc_state->wm.skl.ddb.start = mbus_offset + new_dbuf_state->ddb[pipe].start;
4300 crtc_state->wm.skl.ddb.end = mbus_offset + new_dbuf_state->ddb[pipe].end;
4302 drm_dbg_kms(&dev_priv->drm,
4303 "[CRTC:%d:%s] dbuf slices 0x%x -> 0x%x, ddb (%d - %d) -> (%d - %d), active pipes 0x%x -> 0x%x\n",
4304 crtc->base.base.id, crtc->base.name,
4305 old_dbuf_state->slices[pipe], new_dbuf_state->slices[pipe],
4306 old_dbuf_state->ddb[pipe].start, old_dbuf_state->ddb[pipe].end,
4307 new_dbuf_state->ddb[pipe].start, new_dbuf_state->ddb[pipe].end,
4308 old_dbuf_state->active_pipes, new_dbuf_state->active_pipes);
4313 static int skl_compute_wm_params(const struct intel_crtc_state *crtc_state,
4314 int width, const struct drm_format_info *format,
4315 u64 modifier, unsigned int rotation,
4316 u32 plane_pixel_rate, struct skl_wm_params *wp,
4319 static void skl_compute_plane_wm(const struct intel_crtc_state *crtc_state,
4320 struct intel_plane *plane,
4322 unsigned int latency,
4323 const struct skl_wm_params *wp,
4324 const struct skl_wm_level *result_prev,
4325 struct skl_wm_level *result /* out */);
4328 skl_cursor_allocation(const struct intel_crtc_state *crtc_state,
4331 struct intel_plane *plane = to_intel_plane(crtc_state->uapi.crtc->cursor);
4332 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
4333 int level, max_level = ilk_wm_max_level(dev_priv);
4334 struct skl_wm_level wm = {};
4335 int ret, min_ddb_alloc = 0;
4336 struct skl_wm_params wp;
4338 ret = skl_compute_wm_params(crtc_state, 256,
4339 drm_format_info(DRM_FORMAT_ARGB8888),
4340 DRM_FORMAT_MOD_LINEAR,
4342 crtc_state->pixel_rate, &wp, 0);
4343 drm_WARN_ON(&dev_priv->drm, ret);
4345 for (level = 0; level <= max_level; level++) {
4346 unsigned int latency = dev_priv->display.wm.skl_latency[level];
4348 skl_compute_plane_wm(crtc_state, plane, level, latency, &wp, &wm, &wm);
4349 if (wm.min_ddb_alloc == U16_MAX)
4352 min_ddb_alloc = wm.min_ddb_alloc;
4355 return max(num_active == 1 ? 32 : 8, min_ddb_alloc);
4358 static void skl_ddb_entry_init_from_hw(struct skl_ddb_entry *entry, u32 reg)
4360 skl_ddb_entry_init(entry,
4361 REG_FIELD_GET(PLANE_BUF_START_MASK, reg),
4362 REG_FIELD_GET(PLANE_BUF_END_MASK, reg));
4368 skl_ddb_get_hw_plane_state(struct drm_i915_private *dev_priv,
4369 const enum pipe pipe,
4370 const enum plane_id plane_id,
4371 struct skl_ddb_entry *ddb,
4372 struct skl_ddb_entry *ddb_y)
4376 /* Cursor doesn't support NV12/planar, so no extra calculation needed */
4377 if (plane_id == PLANE_CURSOR) {
4378 val = intel_uncore_read(&dev_priv->uncore, CUR_BUF_CFG(pipe));
4379 skl_ddb_entry_init_from_hw(ddb, val);
4383 val = intel_uncore_read(&dev_priv->uncore, PLANE_BUF_CFG(pipe, plane_id));
4384 skl_ddb_entry_init_from_hw(ddb, val);
4386 if (DISPLAY_VER(dev_priv) >= 11)
4389 val = intel_uncore_read(&dev_priv->uncore, PLANE_NV12_BUF_CFG(pipe, plane_id));
4390 skl_ddb_entry_init_from_hw(ddb_y, val);
4393 static void skl_pipe_ddb_get_hw_state(struct intel_crtc *crtc,
4394 struct skl_ddb_entry *ddb,
4395 struct skl_ddb_entry *ddb_y)
4397 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
4398 enum intel_display_power_domain power_domain;
4399 enum pipe pipe = crtc->pipe;
4400 intel_wakeref_t wakeref;
4401 enum plane_id plane_id;
4403 power_domain = POWER_DOMAIN_PIPE(pipe);
4404 wakeref = intel_display_power_get_if_enabled(dev_priv, power_domain);
4408 for_each_plane_id_on_crtc(crtc, plane_id)
4409 skl_ddb_get_hw_plane_state(dev_priv, pipe,
4414 intel_display_power_put(dev_priv, power_domain, wakeref);
4417 struct dbuf_slice_conf_entry {
4419 u8 dbuf_mask[I915_MAX_PIPES];
4424 * Table taken from Bspec 12716
4425 * Pipes do have some preferred DBuf slice affinity,
4426 * plus there are some hardcoded requirements on how
4427 * those should be distributed for multipipe scenarios.
4428 * For more DBuf slices algorithm can get even more messy
4429 * and less readable, so decided to use a table almost
4430 * as is from BSpec itself - that way it is at least easier
4431 * to compare, change and check.
4433 static const struct dbuf_slice_conf_entry icl_allowed_dbufs[] =
4434 /* Autogenerated with igt/tools/intel_dbuf_map tool: */
4437 .active_pipes = BIT(PIPE_A),
4439 [PIPE_A] = BIT(DBUF_S1),
4443 .active_pipes = BIT(PIPE_B),
4445 [PIPE_B] = BIT(DBUF_S1),
4449 .active_pipes = BIT(PIPE_A) | BIT(PIPE_B),
4451 [PIPE_A] = BIT(DBUF_S1),
4452 [PIPE_B] = BIT(DBUF_S2),
4456 .active_pipes = BIT(PIPE_C),
4458 [PIPE_C] = BIT(DBUF_S2),
4462 .active_pipes = BIT(PIPE_A) | BIT(PIPE_C),
4464 [PIPE_A] = BIT(DBUF_S1),
4465 [PIPE_C] = BIT(DBUF_S2),
4469 .active_pipes = BIT(PIPE_B) | BIT(PIPE_C),
4471 [PIPE_B] = BIT(DBUF_S1),
4472 [PIPE_C] = BIT(DBUF_S2),
4476 .active_pipes = BIT(PIPE_A) | BIT(PIPE_B) | BIT(PIPE_C),
4478 [PIPE_A] = BIT(DBUF_S1),
4479 [PIPE_B] = BIT(DBUF_S1),
4480 [PIPE_C] = BIT(DBUF_S2),
4487 * Table taken from Bspec 49255
4488 * Pipes do have some preferred DBuf slice affinity,
4489 * plus there are some hardcoded requirements on how
4490 * those should be distributed for multipipe scenarios.
4491 * For more DBuf slices algorithm can get even more messy
4492 * and less readable, so decided to use a table almost
4493 * as is from BSpec itself - that way it is at least easier
4494 * to compare, change and check.
4496 static const struct dbuf_slice_conf_entry tgl_allowed_dbufs[] =
4497 /* Autogenerated with igt/tools/intel_dbuf_map tool: */
4500 .active_pipes = BIT(PIPE_A),
4502 [PIPE_A] = BIT(DBUF_S1) | BIT(DBUF_S2),
4506 .active_pipes = BIT(PIPE_B),
4508 [PIPE_B] = BIT(DBUF_S1) | BIT(DBUF_S2),
4512 .active_pipes = BIT(PIPE_A) | BIT(PIPE_B),
4514 [PIPE_A] = BIT(DBUF_S2),
4515 [PIPE_B] = BIT(DBUF_S1),
4519 .active_pipes = BIT(PIPE_C),
4521 [PIPE_C] = BIT(DBUF_S2) | BIT(DBUF_S1),
4525 .active_pipes = BIT(PIPE_A) | BIT(PIPE_C),
4527 [PIPE_A] = BIT(DBUF_S1),
4528 [PIPE_C] = BIT(DBUF_S2),
4532 .active_pipes = BIT(PIPE_B) | BIT(PIPE_C),
4534 [PIPE_B] = BIT(DBUF_S1),
4535 [PIPE_C] = BIT(DBUF_S2),
4539 .active_pipes = BIT(PIPE_A) | BIT(PIPE_B) | BIT(PIPE_C),
4541 [PIPE_A] = BIT(DBUF_S1),
4542 [PIPE_B] = BIT(DBUF_S1),
4543 [PIPE_C] = BIT(DBUF_S2),
4547 .active_pipes = BIT(PIPE_D),
4549 [PIPE_D] = BIT(DBUF_S2) | BIT(DBUF_S1),
4553 .active_pipes = BIT(PIPE_A) | BIT(PIPE_D),
4555 [PIPE_A] = BIT(DBUF_S1),
4556 [PIPE_D] = BIT(DBUF_S2),
4560 .active_pipes = BIT(PIPE_B) | BIT(PIPE_D),
4562 [PIPE_B] = BIT(DBUF_S1),
4563 [PIPE_D] = BIT(DBUF_S2),
4567 .active_pipes = BIT(PIPE_A) | BIT(PIPE_B) | BIT(PIPE_D),
4569 [PIPE_A] = BIT(DBUF_S1),
4570 [PIPE_B] = BIT(DBUF_S1),
4571 [PIPE_D] = BIT(DBUF_S2),
4575 .active_pipes = BIT(PIPE_C) | BIT(PIPE_D),
4577 [PIPE_C] = BIT(DBUF_S1),
4578 [PIPE_D] = BIT(DBUF_S2),
4582 .active_pipes = BIT(PIPE_A) | BIT(PIPE_C) | BIT(PIPE_D),
4584 [PIPE_A] = BIT(DBUF_S1),
4585 [PIPE_C] = BIT(DBUF_S2),
4586 [PIPE_D] = BIT(DBUF_S2),
4590 .active_pipes = BIT(PIPE_B) | BIT(PIPE_C) | BIT(PIPE_D),
4592 [PIPE_B] = BIT(DBUF_S1),
4593 [PIPE_C] = BIT(DBUF_S2),
4594 [PIPE_D] = BIT(DBUF_S2),
4598 .active_pipes = BIT(PIPE_A) | BIT(PIPE_B) | BIT(PIPE_C) | BIT(PIPE_D),
4600 [PIPE_A] = BIT(DBUF_S1),
4601 [PIPE_B] = BIT(DBUF_S1),
4602 [PIPE_C] = BIT(DBUF_S2),
4603 [PIPE_D] = BIT(DBUF_S2),
4609 static const struct dbuf_slice_conf_entry dg2_allowed_dbufs[] = {
4611 .active_pipes = BIT(PIPE_A),
4613 [PIPE_A] = BIT(DBUF_S1) | BIT(DBUF_S2),
4617 .active_pipes = BIT(PIPE_B),
4619 [PIPE_B] = BIT(DBUF_S1) | BIT(DBUF_S2),
4623 .active_pipes = BIT(PIPE_A) | BIT(PIPE_B),
4625 [PIPE_A] = BIT(DBUF_S1),
4626 [PIPE_B] = BIT(DBUF_S2),
4630 .active_pipes = BIT(PIPE_C),
4632 [PIPE_C] = BIT(DBUF_S3) | BIT(DBUF_S4),
4636 .active_pipes = BIT(PIPE_A) | BIT(PIPE_C),
4638 [PIPE_A] = BIT(DBUF_S1) | BIT(DBUF_S2),
4639 [PIPE_C] = BIT(DBUF_S3) | BIT(DBUF_S4),
4643 .active_pipes = BIT(PIPE_B) | BIT(PIPE_C),
4645 [PIPE_B] = BIT(DBUF_S1) | BIT(DBUF_S2),
4646 [PIPE_C] = BIT(DBUF_S3) | BIT(DBUF_S4),
4650 .active_pipes = BIT(PIPE_A) | BIT(PIPE_B) | BIT(PIPE_C),
4652 [PIPE_A] = BIT(DBUF_S1),
4653 [PIPE_B] = BIT(DBUF_S2),
4654 [PIPE_C] = BIT(DBUF_S3) | BIT(DBUF_S4),
4658 .active_pipes = BIT(PIPE_D),
4660 [PIPE_D] = BIT(DBUF_S3) | BIT(DBUF_S4),
4664 .active_pipes = BIT(PIPE_A) | BIT(PIPE_D),
4666 [PIPE_A] = BIT(DBUF_S1) | BIT(DBUF_S2),
4667 [PIPE_D] = BIT(DBUF_S3) | BIT(DBUF_S4),
4671 .active_pipes = BIT(PIPE_B) | BIT(PIPE_D),
4673 [PIPE_B] = BIT(DBUF_S1) | BIT(DBUF_S2),
4674 [PIPE_D] = BIT(DBUF_S3) | BIT(DBUF_S4),
4678 .active_pipes = BIT(PIPE_A) | BIT(PIPE_B) | BIT(PIPE_D),
4680 [PIPE_A] = BIT(DBUF_S1),
4681 [PIPE_B] = BIT(DBUF_S2),
4682 [PIPE_D] = BIT(DBUF_S3) | BIT(DBUF_S4),
4686 .active_pipes = BIT(PIPE_C) | BIT(PIPE_D),
4688 [PIPE_C] = BIT(DBUF_S3),
4689 [PIPE_D] = BIT(DBUF_S4),
4693 .active_pipes = BIT(PIPE_A) | BIT(PIPE_C) | BIT(PIPE_D),
4695 [PIPE_A] = BIT(DBUF_S1) | BIT(DBUF_S2),
4696 [PIPE_C] = BIT(DBUF_S3),
4697 [PIPE_D] = BIT(DBUF_S4),
4701 .active_pipes = BIT(PIPE_B) | BIT(PIPE_C) | BIT(PIPE_D),
4703 [PIPE_B] = BIT(DBUF_S1) | BIT(DBUF_S2),
4704 [PIPE_C] = BIT(DBUF_S3),
4705 [PIPE_D] = BIT(DBUF_S4),
4709 .active_pipes = BIT(PIPE_A) | BIT(PIPE_B) | BIT(PIPE_C) | BIT(PIPE_D),
4711 [PIPE_A] = BIT(DBUF_S1),
4712 [PIPE_B] = BIT(DBUF_S2),
4713 [PIPE_C] = BIT(DBUF_S3),
4714 [PIPE_D] = BIT(DBUF_S4),
4720 static const struct dbuf_slice_conf_entry adlp_allowed_dbufs[] = {
4722 * Keep the join_mbus cases first so check_mbus_joined()
4723 * will prefer them over the !join_mbus cases.
4726 .active_pipes = BIT(PIPE_A),
4728 [PIPE_A] = BIT(DBUF_S1) | BIT(DBUF_S2) | BIT(DBUF_S3) | BIT(DBUF_S4),
4733 .active_pipes = BIT(PIPE_B),
4735 [PIPE_B] = BIT(DBUF_S1) | BIT(DBUF_S2) | BIT(DBUF_S3) | BIT(DBUF_S4),
4740 .active_pipes = BIT(PIPE_A),
4742 [PIPE_A] = BIT(DBUF_S1) | BIT(DBUF_S2),
4747 .active_pipes = BIT(PIPE_B),
4749 [PIPE_B] = BIT(DBUF_S3) | BIT(DBUF_S4),
4754 .active_pipes = BIT(PIPE_A) | BIT(PIPE_B),
4756 [PIPE_A] = BIT(DBUF_S1) | BIT(DBUF_S2),
4757 [PIPE_B] = BIT(DBUF_S3) | BIT(DBUF_S4),
4761 .active_pipes = BIT(PIPE_C),
4763 [PIPE_C] = BIT(DBUF_S3) | BIT(DBUF_S4),
4767 .active_pipes = BIT(PIPE_A) | BIT(PIPE_C),
4769 [PIPE_A] = BIT(DBUF_S1) | BIT(DBUF_S2),
4770 [PIPE_C] = BIT(DBUF_S3) | BIT(DBUF_S4),
4774 .active_pipes = BIT(PIPE_B) | BIT(PIPE_C),
4776 [PIPE_B] = BIT(DBUF_S3) | BIT(DBUF_S4),
4777 [PIPE_C] = BIT(DBUF_S3) | BIT(DBUF_S4),
4781 .active_pipes = BIT(PIPE_A) | BIT(PIPE_B) | BIT(PIPE_C),
4783 [PIPE_A] = BIT(DBUF_S1) | BIT(DBUF_S2),
4784 [PIPE_B] = BIT(DBUF_S3) | BIT(DBUF_S4),
4785 [PIPE_C] = BIT(DBUF_S3) | BIT(DBUF_S4),
4789 .active_pipes = BIT(PIPE_D),
4791 [PIPE_D] = BIT(DBUF_S1) | BIT(DBUF_S2),
4795 .active_pipes = BIT(PIPE_A) | BIT(PIPE_D),
4797 [PIPE_A] = BIT(DBUF_S1) | BIT(DBUF_S2),
4798 [PIPE_D] = BIT(DBUF_S1) | BIT(DBUF_S2),
4802 .active_pipes = BIT(PIPE_B) | BIT(PIPE_D),
4804 [PIPE_B] = BIT(DBUF_S3) | BIT(DBUF_S4),
4805 [PIPE_D] = BIT(DBUF_S1) | BIT(DBUF_S2),
4809 .active_pipes = BIT(PIPE_A) | BIT(PIPE_B) | BIT(PIPE_D),
4811 [PIPE_A] = BIT(DBUF_S1) | BIT(DBUF_S2),
4812 [PIPE_B] = BIT(DBUF_S3) | BIT(DBUF_S4),
4813 [PIPE_D] = BIT(DBUF_S1) | BIT(DBUF_S2),
4817 .active_pipes = BIT(PIPE_C) | BIT(PIPE_D),
4819 [PIPE_C] = BIT(DBUF_S3) | BIT(DBUF_S4),
4820 [PIPE_D] = BIT(DBUF_S1) | BIT(DBUF_S2),
4824 .active_pipes = BIT(PIPE_A) | BIT(PIPE_C) | BIT(PIPE_D),
4826 [PIPE_A] = BIT(DBUF_S1) | BIT(DBUF_S2),
4827 [PIPE_C] = BIT(DBUF_S3) | BIT(DBUF_S4),
4828 [PIPE_D] = BIT(DBUF_S1) | BIT(DBUF_S2),
4832 .active_pipes = BIT(PIPE_B) | BIT(PIPE_C) | BIT(PIPE_D),
4834 [PIPE_B] = BIT(DBUF_S3) | BIT(DBUF_S4),
4835 [PIPE_C] = BIT(DBUF_S3) | BIT(DBUF_S4),
4836 [PIPE_D] = BIT(DBUF_S1) | BIT(DBUF_S2),
4840 .active_pipes = BIT(PIPE_A) | BIT(PIPE_B) | BIT(PIPE_C) | BIT(PIPE_D),
4842 [PIPE_A] = BIT(DBUF_S1) | BIT(DBUF_S2),
4843 [PIPE_B] = BIT(DBUF_S3) | BIT(DBUF_S4),
4844 [PIPE_C] = BIT(DBUF_S3) | BIT(DBUF_S4),
4845 [PIPE_D] = BIT(DBUF_S1) | BIT(DBUF_S2),
4852 static bool check_mbus_joined(u8 active_pipes,
4853 const struct dbuf_slice_conf_entry *dbuf_slices)
4857 for (i = 0; dbuf_slices[i].active_pipes != 0; i++) {
4858 if (dbuf_slices[i].active_pipes == active_pipes)
4859 return dbuf_slices[i].join_mbus;
4864 static bool adlp_check_mbus_joined(u8 active_pipes)
4866 return check_mbus_joined(active_pipes, adlp_allowed_dbufs);
4869 static u8 compute_dbuf_slices(enum pipe pipe, u8 active_pipes, bool join_mbus,
4870 const struct dbuf_slice_conf_entry *dbuf_slices)
4874 for (i = 0; dbuf_slices[i].active_pipes != 0; i++) {
4875 if (dbuf_slices[i].active_pipes == active_pipes &&
4876 dbuf_slices[i].join_mbus == join_mbus)
4877 return dbuf_slices[i].dbuf_mask[pipe];
4883 * This function finds an entry with same enabled pipe configuration and
4884 * returns correspondent DBuf slice mask as stated in BSpec for particular
4887 static u8 icl_compute_dbuf_slices(enum pipe pipe, u8 active_pipes, bool join_mbus)
4890 * FIXME: For ICL this is still a bit unclear as prev BSpec revision
4891 * required calculating "pipe ratio" in order to determine
4892 * if one or two slices can be used for single pipe configurations
4893 * as additional constraint to the existing table.
4894 * However based on recent info, it should be not "pipe ratio"
4895 * but rather ratio between pixel_rate and cdclk with additional
4896 * constants, so for now we are using only table until this is
4897 * clarified. Also this is the reason why crtc_state param is
4898 * still here - we will need it once those additional constraints
4901 return compute_dbuf_slices(pipe, active_pipes, join_mbus,
4905 static u8 tgl_compute_dbuf_slices(enum pipe pipe, u8 active_pipes, bool join_mbus)
4907 return compute_dbuf_slices(pipe, active_pipes, join_mbus,
4911 static u8 adlp_compute_dbuf_slices(enum pipe pipe, u8 active_pipes, bool join_mbus)
4913 return compute_dbuf_slices(pipe, active_pipes, join_mbus,
4914 adlp_allowed_dbufs);
4917 static u8 dg2_compute_dbuf_slices(enum pipe pipe, u8 active_pipes, bool join_mbus)
4919 return compute_dbuf_slices(pipe, active_pipes, join_mbus,
4923 static u8 skl_compute_dbuf_slices(struct intel_crtc *crtc, u8 active_pipes, bool join_mbus)
4925 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
4926 enum pipe pipe = crtc->pipe;
4928 if (IS_DG2(dev_priv))
4929 return dg2_compute_dbuf_slices(pipe, active_pipes, join_mbus);
4930 else if (DISPLAY_VER(dev_priv) >= 13)
4931 return adlp_compute_dbuf_slices(pipe, active_pipes, join_mbus);
4932 else if (DISPLAY_VER(dev_priv) == 12)
4933 return tgl_compute_dbuf_slices(pipe, active_pipes, join_mbus);
4934 else if (DISPLAY_VER(dev_priv) == 11)
4935 return icl_compute_dbuf_slices(pipe, active_pipes, join_mbus);
4937 * For anything else just return one slice yet.
4938 * Should be extended for other platforms.
4940 return active_pipes & BIT(pipe) ? BIT(DBUF_S1) : 0;
4944 use_minimal_wm0_only(const struct intel_crtc_state *crtc_state,
4945 struct intel_plane *plane)
4947 struct drm_i915_private *i915 = to_i915(plane->base.dev);
4949 return DISPLAY_VER(i915) >= 13 &&
4950 crtc_state->uapi.async_flip &&
4955 skl_total_relative_data_rate(const struct intel_crtc_state *crtc_state)
4957 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
4958 struct drm_i915_private *i915 = to_i915(crtc->base.dev);
4959 enum plane_id plane_id;
4962 for_each_plane_id_on_crtc(crtc, plane_id) {
4963 if (plane_id == PLANE_CURSOR)
4966 data_rate += crtc_state->rel_data_rate[plane_id];
4968 if (DISPLAY_VER(i915) < 11)
4969 data_rate += crtc_state->rel_data_rate_y[plane_id];
4975 static const struct skl_wm_level *
4976 skl_plane_wm_level(const struct skl_pipe_wm *pipe_wm,
4977 enum plane_id plane_id,
4980 const struct skl_plane_wm *wm = &pipe_wm->planes[plane_id];
4982 if (level == 0 && pipe_wm->use_sagv_wm)
4983 return &wm->sagv.wm0;
4985 return &wm->wm[level];
4988 static const struct skl_wm_level *
4989 skl_plane_trans_wm(const struct skl_pipe_wm *pipe_wm,
4990 enum plane_id plane_id)
4992 const struct skl_plane_wm *wm = &pipe_wm->planes[plane_id];
4994 if (pipe_wm->use_sagv_wm)
4995 return &wm->sagv.trans_wm;
4997 return &wm->trans_wm;
5001 * We only disable the watermarks for each plane if
5002 * they exceed the ddb allocation of said plane. This
5003 * is done so that we don't end up touching cursor
5004 * watermarks needlessly when some other plane reduces
5005 * our max possible watermark level.
5007 * Bspec has this to say about the PLANE_WM enable bit:
5008 * "All the watermarks at this level for all enabled
5009 * planes must be enabled before the level will be used."
5010 * So this is actually safe to do.
5013 skl_check_wm_level(struct skl_wm_level *wm, const struct skl_ddb_entry *ddb)
5015 if (wm->min_ddb_alloc > skl_ddb_entry_size(ddb))
5016 memset(wm, 0, sizeof(*wm));
5020 skl_check_nv12_wm_level(struct skl_wm_level *wm, struct skl_wm_level *uv_wm,
5021 const struct skl_ddb_entry *ddb_y, const struct skl_ddb_entry *ddb)
5023 if (wm->min_ddb_alloc > skl_ddb_entry_size(ddb_y) ||
5024 uv_wm->min_ddb_alloc > skl_ddb_entry_size(ddb)) {
5025 memset(wm, 0, sizeof(*wm));
5026 memset(uv_wm, 0, sizeof(*uv_wm));
5030 static bool icl_need_wm1_wa(struct drm_i915_private *i915,
5031 enum plane_id plane_id)
5034 * Wa_1408961008:icl, ehl
5035 * Wa_14012656716:tgl, adl
5036 * Underruns with WM1+ disabled
5038 return DISPLAY_VER(i915) == 11 ||
5039 (IS_DISPLAY_VER(i915, 12, 13) && plane_id == PLANE_CURSOR);
5042 struct skl_plane_ddb_iter {
5048 skl_allocate_plane_ddb(struct skl_plane_ddb_iter *iter,
5049 struct skl_ddb_entry *ddb,
5050 const struct skl_wm_level *wm,
5053 u16 size, extra = 0;
5056 extra = min_t(u16, iter->size,
5057 DIV64_U64_ROUND_UP(iter->size * data_rate,
5059 iter->size -= extra;
5060 iter->data_rate -= data_rate;
5064 * Keep ddb entry of all disabled planes explicitly zeroed
5065 * to avoid skl_ddb_add_affected_planes() adding them to
5066 * the state when other planes change their allocations.
5068 size = wm->min_ddb_alloc + extra;
5070 iter->start = skl_ddb_entry_init(ddb, iter->start,
5071 iter->start + size);
5075 skl_crtc_allocate_plane_ddb(struct intel_atomic_state *state,
5076 struct intel_crtc *crtc)
5078 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
5079 struct intel_crtc_state *crtc_state =
5080 intel_atomic_get_new_crtc_state(state, crtc);
5081 const struct intel_dbuf_state *dbuf_state =
5082 intel_atomic_get_new_dbuf_state(state);
5083 const struct skl_ddb_entry *alloc = &dbuf_state->ddb[crtc->pipe];
5084 int num_active = hweight8(dbuf_state->active_pipes);
5085 struct skl_plane_ddb_iter iter;
5086 enum plane_id plane_id;
5091 /* Clear the partitioning for disabled planes. */
5092 memset(crtc_state->wm.skl.plane_ddb, 0, sizeof(crtc_state->wm.skl.plane_ddb));
5093 memset(crtc_state->wm.skl.plane_ddb_y, 0, sizeof(crtc_state->wm.skl.plane_ddb_y));
5095 if (!crtc_state->hw.active)
5098 iter.start = alloc->start;
5099 iter.size = skl_ddb_entry_size(alloc);
5103 /* Allocate fixed number of blocks for cursor. */
5104 cursor_size = skl_cursor_allocation(crtc_state, num_active);
5105 iter.size -= cursor_size;
5106 skl_ddb_entry_init(&crtc_state->wm.skl.plane_ddb[PLANE_CURSOR],
5107 alloc->end - cursor_size, alloc->end);
5109 iter.data_rate = skl_total_relative_data_rate(crtc_state);
5112 * Find the highest watermark level for which we can satisfy the block
5113 * requirement of active planes.
5115 for (level = ilk_wm_max_level(dev_priv); level >= 0; level--) {
5117 for_each_plane_id_on_crtc(crtc, plane_id) {
5118 const struct skl_plane_wm *wm =
5119 &crtc_state->wm.skl.optimal.planes[plane_id];
5121 if (plane_id == PLANE_CURSOR) {
5122 const struct skl_ddb_entry *ddb =
5123 &crtc_state->wm.skl.plane_ddb[plane_id];
5125 if (wm->wm[level].min_ddb_alloc > skl_ddb_entry_size(ddb)) {
5126 drm_WARN_ON(&dev_priv->drm,
5127 wm->wm[level].min_ddb_alloc != U16_MAX);
5134 blocks += wm->wm[level].min_ddb_alloc;
5135 blocks += wm->uv_wm[level].min_ddb_alloc;
5138 if (blocks <= iter.size) {
5139 iter.size -= blocks;
5145 drm_dbg_kms(&dev_priv->drm,
5146 "Requested display configuration exceeds system DDB limitations");
5147 drm_dbg_kms(&dev_priv->drm, "minimum required %d/%d\n",
5152 /* avoid the WARN later when we don't allocate any extra DDB */
5153 if (iter.data_rate == 0)
5157 * Grant each plane the blocks it requires at the highest achievable
5158 * watermark level, plus an extra share of the leftover blocks
5159 * proportional to its relative data rate.
5161 for_each_plane_id_on_crtc(crtc, plane_id) {
5162 struct skl_ddb_entry *ddb =
5163 &crtc_state->wm.skl.plane_ddb[plane_id];
5164 struct skl_ddb_entry *ddb_y =
5165 &crtc_state->wm.skl.plane_ddb_y[plane_id];
5166 const struct skl_plane_wm *wm =
5167 &crtc_state->wm.skl.optimal.planes[plane_id];
5169 if (plane_id == PLANE_CURSOR)
5172 if (DISPLAY_VER(dev_priv) < 11 &&
5173 crtc_state->nv12_planes & BIT(plane_id)) {
5174 skl_allocate_plane_ddb(&iter, ddb_y, &wm->wm[level],
5175 crtc_state->rel_data_rate_y[plane_id]);
5176 skl_allocate_plane_ddb(&iter, ddb, &wm->uv_wm[level],
5177 crtc_state->rel_data_rate[plane_id]);
5179 skl_allocate_plane_ddb(&iter, ddb, &wm->wm[level],
5180 crtc_state->rel_data_rate[plane_id]);
5183 drm_WARN_ON(&dev_priv->drm, iter.size != 0 || iter.data_rate != 0);
5186 * When we calculated watermark values we didn't know how high
5187 * of a level we'd actually be able to hit, so we just marked
5188 * all levels as "enabled." Go back now and disable the ones
5189 * that aren't actually possible.
5191 for (level++; level <= ilk_wm_max_level(dev_priv); level++) {
5192 for_each_plane_id_on_crtc(crtc, plane_id) {
5193 const struct skl_ddb_entry *ddb =
5194 &crtc_state->wm.skl.plane_ddb[plane_id];
5195 const struct skl_ddb_entry *ddb_y =
5196 &crtc_state->wm.skl.plane_ddb_y[plane_id];
5197 struct skl_plane_wm *wm =
5198 &crtc_state->wm.skl.optimal.planes[plane_id];
5200 if (DISPLAY_VER(dev_priv) < 11 &&
5201 crtc_state->nv12_planes & BIT(plane_id))
5202 skl_check_nv12_wm_level(&wm->wm[level],
5206 skl_check_wm_level(&wm->wm[level], ddb);
5208 if (icl_need_wm1_wa(dev_priv, plane_id) &&
5209 level == 1 && wm->wm[0].enable) {
5210 wm->wm[level].blocks = wm->wm[0].blocks;
5211 wm->wm[level].lines = wm->wm[0].lines;
5212 wm->wm[level].ignore_lines = wm->wm[0].ignore_lines;
5218 * Go back and disable the transition and SAGV watermarks
5219 * if it turns out we don't have enough DDB blocks for them.
5221 for_each_plane_id_on_crtc(crtc, plane_id) {
5222 const struct skl_ddb_entry *ddb =
5223 &crtc_state->wm.skl.plane_ddb[plane_id];
5224 const struct skl_ddb_entry *ddb_y =
5225 &crtc_state->wm.skl.plane_ddb_y[plane_id];
5226 struct skl_plane_wm *wm =
5227 &crtc_state->wm.skl.optimal.planes[plane_id];
5229 if (DISPLAY_VER(dev_priv) < 11 &&
5230 crtc_state->nv12_planes & BIT(plane_id)) {
5231 skl_check_wm_level(&wm->trans_wm, ddb_y);
5233 WARN_ON(skl_ddb_entry_size(ddb_y));
5235 skl_check_wm_level(&wm->trans_wm, ddb);
5238 skl_check_wm_level(&wm->sagv.wm0, ddb);
5239 skl_check_wm_level(&wm->sagv.trans_wm, ddb);
5246 * The max latency should be 257 (max the punit can code is 255 and we add 2us
5247 * for the read latency) and cpp should always be <= 8, so that
5248 * should allow pixel_rate up to ~2 GHz which seems sufficient since max
5249 * 2xcdclk is 1350 MHz and the pixel rate should never exceed that.
5251 static uint_fixed_16_16_t
5252 skl_wm_method1(const struct drm_i915_private *dev_priv, u32 pixel_rate,
5253 u8 cpp, u32 latency, u32 dbuf_block_size)
5255 u32 wm_intermediate_val;
5256 uint_fixed_16_16_t ret;
5259 return FP_16_16_MAX;
5261 wm_intermediate_val = latency * pixel_rate * cpp;
5262 ret = div_fixed16(wm_intermediate_val, 1000 * dbuf_block_size);
5264 if (DISPLAY_VER(dev_priv) >= 10)
5265 ret = add_fixed16_u32(ret, 1);
5270 static uint_fixed_16_16_t
5271 skl_wm_method2(u32 pixel_rate, u32 pipe_htotal, u32 latency,
5272 uint_fixed_16_16_t plane_blocks_per_line)
5274 u32 wm_intermediate_val;
5275 uint_fixed_16_16_t ret;
5278 return FP_16_16_MAX;
5280 wm_intermediate_val = latency * pixel_rate;
5281 wm_intermediate_val = DIV_ROUND_UP(wm_intermediate_val,
5282 pipe_htotal * 1000);
5283 ret = mul_u32_fixed16(wm_intermediate_val, plane_blocks_per_line);
5287 static uint_fixed_16_16_t
5288 intel_get_linetime_us(const struct intel_crtc_state *crtc_state)
5290 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
5293 uint_fixed_16_16_t linetime_us;
5295 if (!crtc_state->hw.active)
5296 return u32_to_fixed16(0);
5298 pixel_rate = crtc_state->pixel_rate;
5300 if (drm_WARN_ON(&dev_priv->drm, pixel_rate == 0))
5301 return u32_to_fixed16(0);
5303 crtc_htotal = crtc_state->hw.pipe_mode.crtc_htotal;
5304 linetime_us = div_fixed16(crtc_htotal * 1000, pixel_rate);
5310 skl_compute_wm_params(const struct intel_crtc_state *crtc_state,
5311 int width, const struct drm_format_info *format,
5312 u64 modifier, unsigned int rotation,
5313 u32 plane_pixel_rate, struct skl_wm_params *wp,
5316 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
5317 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
5320 /* only planar format has two planes */
5321 if (color_plane == 1 &&
5322 !intel_format_info_is_yuv_semiplanar(format, modifier)) {
5323 drm_dbg_kms(&dev_priv->drm,
5324 "Non planar format have single plane\n");
5328 wp->y_tiled = modifier == I915_FORMAT_MOD_Y_TILED ||
5329 modifier == I915_FORMAT_MOD_4_TILED ||
5330 modifier == I915_FORMAT_MOD_Yf_TILED ||
5331 modifier == I915_FORMAT_MOD_Y_TILED_CCS ||
5332 modifier == I915_FORMAT_MOD_Yf_TILED_CCS;
5333 wp->x_tiled = modifier == I915_FORMAT_MOD_X_TILED;
5334 wp->rc_surface = modifier == I915_FORMAT_MOD_Y_TILED_CCS ||
5335 modifier == I915_FORMAT_MOD_Yf_TILED_CCS;
5336 wp->is_planar = intel_format_info_is_yuv_semiplanar(format, modifier);
5339 if (color_plane == 1 && wp->is_planar)
5342 wp->cpp = format->cpp[color_plane];
5343 wp->plane_pixel_rate = plane_pixel_rate;
5345 if (DISPLAY_VER(dev_priv) >= 11 &&
5346 modifier == I915_FORMAT_MOD_Yf_TILED && wp->cpp == 1)
5347 wp->dbuf_block_size = 256;
5349 wp->dbuf_block_size = 512;
5351 if (drm_rotation_90_or_270(rotation)) {
5354 wp->y_min_scanlines = 16;
5357 wp->y_min_scanlines = 8;
5360 wp->y_min_scanlines = 4;
5363 MISSING_CASE(wp->cpp);
5367 wp->y_min_scanlines = 4;
5370 if (skl_needs_memory_bw_wa(dev_priv))
5371 wp->y_min_scanlines *= 2;
5373 wp->plane_bytes_per_line = wp->width * wp->cpp;
5375 interm_pbpl = DIV_ROUND_UP(wp->plane_bytes_per_line *
5376 wp->y_min_scanlines,
5377 wp->dbuf_block_size);
5379 if (DISPLAY_VER(dev_priv) >= 10)
5382 wp->plane_blocks_per_line = div_fixed16(interm_pbpl,
5383 wp->y_min_scanlines);
5385 interm_pbpl = DIV_ROUND_UP(wp->plane_bytes_per_line,
5386 wp->dbuf_block_size);
5388 if (!wp->x_tiled || DISPLAY_VER(dev_priv) >= 10)
5391 wp->plane_blocks_per_line = u32_to_fixed16(interm_pbpl);
5394 wp->y_tile_minimum = mul_u32_fixed16(wp->y_min_scanlines,
5395 wp->plane_blocks_per_line);
5397 wp->linetime_us = fixed16_to_u32_round_up(
5398 intel_get_linetime_us(crtc_state));
5404 skl_compute_plane_wm_params(const struct intel_crtc_state *crtc_state,
5405 const struct intel_plane_state *plane_state,
5406 struct skl_wm_params *wp, int color_plane)
5408 const struct drm_framebuffer *fb = plane_state->hw.fb;
5412 * Src coordinates are already rotated by 270 degrees for
5413 * the 90/270 degree plane rotation cases (to match the
5414 * GTT mapping), hence no need to account for rotation here.
5416 width = drm_rect_width(&plane_state->uapi.src) >> 16;
5418 return skl_compute_wm_params(crtc_state, width,
5419 fb->format, fb->modifier,
5420 plane_state->hw.rotation,
5421 intel_plane_pixel_rate(crtc_state, plane_state),
5425 static bool skl_wm_has_lines(struct drm_i915_private *dev_priv, int level)
5427 if (DISPLAY_VER(dev_priv) >= 10)
5430 /* The number of lines are ignored for the level 0 watermark. */
5434 static int skl_wm_max_lines(struct drm_i915_private *dev_priv)
5436 if (DISPLAY_VER(dev_priv) >= 13)
5442 static void skl_compute_plane_wm(const struct intel_crtc_state *crtc_state,
5443 struct intel_plane *plane,
5445 unsigned int latency,
5446 const struct skl_wm_params *wp,
5447 const struct skl_wm_level *result_prev,
5448 struct skl_wm_level *result /* out */)
5450 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
5451 uint_fixed_16_16_t method1, method2;
5452 uint_fixed_16_16_t selected_result;
5453 u32 blocks, lines, min_ddb_alloc = 0;
5456 (use_minimal_wm0_only(crtc_state, plane) && level > 0)) {
5458 result->min_ddb_alloc = U16_MAX;
5463 * WaIncreaseLatencyIPCEnabled: kbl,cfl
5464 * Display WA #1141: kbl,cfl
5466 if ((IS_KABYLAKE(dev_priv) ||
5467 IS_COFFEELAKE(dev_priv) ||
5468 IS_COMETLAKE(dev_priv)) &&
5469 dev_priv->ipc_enabled)
5472 if (skl_needs_memory_bw_wa(dev_priv) && wp->x_tiled)
5475 method1 = skl_wm_method1(dev_priv, wp->plane_pixel_rate,
5476 wp->cpp, latency, wp->dbuf_block_size);
5477 method2 = skl_wm_method2(wp->plane_pixel_rate,
5478 crtc_state->hw.pipe_mode.crtc_htotal,
5480 wp->plane_blocks_per_line);
5483 selected_result = max_fixed16(method2, wp->y_tile_minimum);
5485 if ((wp->cpp * crtc_state->hw.pipe_mode.crtc_htotal /
5486 wp->dbuf_block_size < 1) &&
5487 (wp->plane_bytes_per_line / wp->dbuf_block_size < 1)) {
5488 selected_result = method2;
5489 } else if (latency >= wp->linetime_us) {
5490 if (DISPLAY_VER(dev_priv) == 9)
5491 selected_result = min_fixed16(method1, method2);
5493 selected_result = method2;
5495 selected_result = method1;
5499 blocks = fixed16_to_u32_round_up(selected_result) + 1;
5501 * Lets have blocks at minimum equivalent to plane_blocks_per_line
5502 * as there will be at minimum one line for lines configuration. This
5503 * is a work around for FIFO underruns observed with resolutions like
5504 * 4k 60 Hz in single channel DRAM configurations.
5506 * As per the Bspec 49325, if the ddb allocation can hold at least
5507 * one plane_blocks_per_line, we should have selected method2 in
5508 * the above logic. Assuming that modern versions have enough dbuf
5509 * and method2 guarantees blocks equivalent to at least 1 line,
5510 * select the blocks as plane_blocks_per_line.
5512 * TODO: Revisit the logic when we have better understanding on DRAM
5513 * channels' impact on the level 0 memory latency and the relevant
5516 if (skl_wm_has_lines(dev_priv, level))
5517 blocks = max(blocks,
5518 fixed16_to_u32_round_up(wp->plane_blocks_per_line));
5519 lines = div_round_up_fixed16(selected_result,
5520 wp->plane_blocks_per_line);
5522 if (DISPLAY_VER(dev_priv) == 9) {
5523 /* Display WA #1125: skl,bxt,kbl */
5524 if (level == 0 && wp->rc_surface)
5525 blocks += fixed16_to_u32_round_up(wp->y_tile_minimum);
5527 /* Display WA #1126: skl,bxt,kbl */
5528 if (level >= 1 && level <= 7) {
5530 blocks += fixed16_to_u32_round_up(wp->y_tile_minimum);
5531 lines += wp->y_min_scanlines;
5537 * Make sure result blocks for higher latency levels are
5538 * atleast as high as level below the current level.
5539 * Assumption in DDB algorithm optimization for special
5540 * cases. Also covers Display WA #1125 for RC.
5542 if (result_prev->blocks > blocks)
5543 blocks = result_prev->blocks;
5547 if (DISPLAY_VER(dev_priv) >= 11) {
5551 if (lines % wp->y_min_scanlines == 0)
5552 extra_lines = wp->y_min_scanlines;
5554 extra_lines = wp->y_min_scanlines * 2 -
5555 lines % wp->y_min_scanlines;
5557 min_ddb_alloc = mul_round_up_u32_fixed16(lines + extra_lines,
5558 wp->plane_blocks_per_line);
5560 min_ddb_alloc = blocks + DIV_ROUND_UP(blocks, 10);
5564 if (!skl_wm_has_lines(dev_priv, level))
5567 if (lines > skl_wm_max_lines(dev_priv)) {
5569 result->min_ddb_alloc = U16_MAX;
5574 * If lines is valid, assume we can use this watermark level
5575 * for now. We'll come back and disable it after we calculate the
5576 * DDB allocation if it turns out we don't actually have enough
5577 * blocks to satisfy it.
5579 result->blocks = blocks;
5580 result->lines = lines;
5581 /* Bspec says: value >= plane ddb allocation -> invalid, hence the +1 here */
5582 result->min_ddb_alloc = max(min_ddb_alloc, blocks) + 1;
5583 result->enable = true;
5585 if (DISPLAY_VER(dev_priv) < 12 && dev_priv->display.sagv.block_time_us)
5586 result->can_sagv = latency >= dev_priv->display.sagv.block_time_us;
5590 skl_compute_wm_levels(const struct intel_crtc_state *crtc_state,
5591 struct intel_plane *plane,
5592 const struct skl_wm_params *wm_params,
5593 struct skl_wm_level *levels)
5595 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
5596 int level, max_level = ilk_wm_max_level(dev_priv);
5597 struct skl_wm_level *result_prev = &levels[0];
5599 for (level = 0; level <= max_level; level++) {
5600 struct skl_wm_level *result = &levels[level];
5601 unsigned int latency = dev_priv->display.wm.skl_latency[level];
5603 skl_compute_plane_wm(crtc_state, plane, level, latency,
5604 wm_params, result_prev, result);
5606 result_prev = result;
5610 static void tgl_compute_sagv_wm(const struct intel_crtc_state *crtc_state,
5611 struct intel_plane *plane,
5612 const struct skl_wm_params *wm_params,
5613 struct skl_plane_wm *plane_wm)
5615 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
5616 struct skl_wm_level *sagv_wm = &plane_wm->sagv.wm0;
5617 struct skl_wm_level *levels = plane_wm->wm;
5618 unsigned int latency = 0;
5620 if (dev_priv->display.sagv.block_time_us)
5621 latency = dev_priv->display.sagv.block_time_us + dev_priv->display.wm.skl_latency[0];
5623 skl_compute_plane_wm(crtc_state, plane, 0, latency,
5624 wm_params, &levels[0],
5628 static void skl_compute_transition_wm(struct drm_i915_private *dev_priv,
5629 struct skl_wm_level *trans_wm,
5630 const struct skl_wm_level *wm0,
5631 const struct skl_wm_params *wp)
5633 u16 trans_min, trans_amount, trans_y_tile_min;
5634 u16 wm0_blocks, trans_offset, blocks;
5636 /* Transition WM don't make any sense if ipc is disabled */
5637 if (!dev_priv->ipc_enabled)
5641 * WaDisableTWM:skl,kbl,cfl,bxt
5642 * Transition WM are not recommended by HW team for GEN9
5644 if (DISPLAY_VER(dev_priv) == 9)
5647 if (DISPLAY_VER(dev_priv) >= 11)
5652 /* Display WA #1140: glk,cnl */
5653 if (DISPLAY_VER(dev_priv) == 10)
5656 trans_amount = 10; /* This is configurable amount */
5658 trans_offset = trans_min + trans_amount;
5661 * The spec asks for Selected Result Blocks for wm0 (the real value),
5662 * not Result Blocks (the integer value). Pay attention to the capital
5663 * letters. The value wm_l0->blocks is actually Result Blocks, but
5664 * since Result Blocks is the ceiling of Selected Result Blocks plus 1,
5665 * and since we later will have to get the ceiling of the sum in the
5666 * transition watermarks calculation, we can just pretend Selected
5667 * Result Blocks is Result Blocks minus 1 and it should work for the
5668 * current platforms.
5670 wm0_blocks = wm0->blocks - 1;
5674 (u16)mul_round_up_u32_fixed16(2, wp->y_tile_minimum);
5675 blocks = max(wm0_blocks, trans_y_tile_min) + trans_offset;
5677 blocks = wm0_blocks + trans_offset;
5682 * Just assume we can enable the transition watermark. After
5683 * computing the DDB we'll come back and disable it if that
5684 * assumption turns out to be false.
5686 trans_wm->blocks = blocks;
5687 trans_wm->min_ddb_alloc = max_t(u16, wm0->min_ddb_alloc, blocks + 1);
5688 trans_wm->enable = true;
5691 static int skl_build_plane_wm_single(struct intel_crtc_state *crtc_state,
5692 const struct intel_plane_state *plane_state,
5693 struct intel_plane *plane, int color_plane)
5695 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
5696 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
5697 struct skl_plane_wm *wm = &crtc_state->wm.skl.raw.planes[plane->id];
5698 struct skl_wm_params wm_params;
5701 ret = skl_compute_plane_wm_params(crtc_state, plane_state,
5702 &wm_params, color_plane);
5706 skl_compute_wm_levels(crtc_state, plane, &wm_params, wm->wm);
5708 skl_compute_transition_wm(dev_priv, &wm->trans_wm,
5709 &wm->wm[0], &wm_params);
5711 if (DISPLAY_VER(dev_priv) >= 12) {
5712 tgl_compute_sagv_wm(crtc_state, plane, &wm_params, wm);
5714 skl_compute_transition_wm(dev_priv, &wm->sagv.trans_wm,
5715 &wm->sagv.wm0, &wm_params);
5721 static int skl_build_plane_wm_uv(struct intel_crtc_state *crtc_state,
5722 const struct intel_plane_state *plane_state,
5723 struct intel_plane *plane)
5725 struct skl_plane_wm *wm = &crtc_state->wm.skl.raw.planes[plane->id];
5726 struct skl_wm_params wm_params;
5729 wm->is_planar = true;
5731 /* uv plane watermarks must also be validated for NV12/Planar */
5732 ret = skl_compute_plane_wm_params(crtc_state, plane_state,
5737 skl_compute_wm_levels(crtc_state, plane, &wm_params, wm->uv_wm);
5742 static int skl_build_plane_wm(struct intel_crtc_state *crtc_state,
5743 const struct intel_plane_state *plane_state)
5745 struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
5746 enum plane_id plane_id = plane->id;
5747 struct skl_plane_wm *wm = &crtc_state->wm.skl.raw.planes[plane_id];
5748 const struct drm_framebuffer *fb = plane_state->hw.fb;
5751 memset(wm, 0, sizeof(*wm));
5753 if (!intel_wm_plane_visible(crtc_state, plane_state))
5756 ret = skl_build_plane_wm_single(crtc_state, plane_state,
5761 if (fb->format->is_yuv && fb->format->num_planes > 1) {
5762 ret = skl_build_plane_wm_uv(crtc_state, plane_state,
5771 static int icl_build_plane_wm(struct intel_crtc_state *crtc_state,
5772 const struct intel_plane_state *plane_state)
5774 struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
5775 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
5776 enum plane_id plane_id = plane->id;
5777 struct skl_plane_wm *wm = &crtc_state->wm.skl.raw.planes[plane_id];
5780 /* Watermarks calculated in master */
5781 if (plane_state->planar_slave)
5784 memset(wm, 0, sizeof(*wm));
5786 if (plane_state->planar_linked_plane) {
5787 const struct drm_framebuffer *fb = plane_state->hw.fb;
5789 drm_WARN_ON(&dev_priv->drm,
5790 !intel_wm_plane_visible(crtc_state, plane_state));
5791 drm_WARN_ON(&dev_priv->drm, !fb->format->is_yuv ||
5792 fb->format->num_planes == 1);
5794 ret = skl_build_plane_wm_single(crtc_state, plane_state,
5795 plane_state->planar_linked_plane, 0);
5799 ret = skl_build_plane_wm_single(crtc_state, plane_state,
5803 } else if (intel_wm_plane_visible(crtc_state, plane_state)) {
5804 ret = skl_build_plane_wm_single(crtc_state, plane_state,
5813 static int skl_build_pipe_wm(struct intel_atomic_state *state,
5814 struct intel_crtc *crtc)
5816 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
5817 struct intel_crtc_state *crtc_state =
5818 intel_atomic_get_new_crtc_state(state, crtc);
5819 const struct intel_plane_state *plane_state;
5820 struct intel_plane *plane;
5823 for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
5825 * FIXME should perhaps check {old,new}_plane_crtc->hw.crtc
5826 * instead but we don't populate that correctly for NV12 Y
5827 * planes so for now hack this.
5829 if (plane->pipe != crtc->pipe)
5832 if (DISPLAY_VER(dev_priv) >= 11)
5833 ret = icl_build_plane_wm(crtc_state, plane_state);
5835 ret = skl_build_plane_wm(crtc_state, plane_state);
5840 crtc_state->wm.skl.optimal = crtc_state->wm.skl.raw;
5845 static void skl_ddb_entry_write(struct drm_i915_private *dev_priv,
5847 const struct skl_ddb_entry *entry)
5850 intel_de_write_fw(dev_priv, reg,
5851 PLANE_BUF_END(entry->end - 1) |
5852 PLANE_BUF_START(entry->start));
5854 intel_de_write_fw(dev_priv, reg, 0);
5857 static void skl_write_wm_level(struct drm_i915_private *dev_priv,
5859 const struct skl_wm_level *level)
5865 if (level->ignore_lines)
5866 val |= PLANE_WM_IGNORE_LINES;
5867 val |= REG_FIELD_PREP(PLANE_WM_BLOCKS_MASK, level->blocks);
5868 val |= REG_FIELD_PREP(PLANE_WM_LINES_MASK, level->lines);
5870 intel_de_write_fw(dev_priv, reg, val);
5873 void skl_write_plane_wm(struct intel_plane *plane,
5874 const struct intel_crtc_state *crtc_state)
5876 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
5877 int level, max_level = ilk_wm_max_level(dev_priv);
5878 enum plane_id plane_id = plane->id;
5879 enum pipe pipe = plane->pipe;
5880 const struct skl_pipe_wm *pipe_wm = &crtc_state->wm.skl.optimal;
5881 const struct skl_ddb_entry *ddb =
5882 &crtc_state->wm.skl.plane_ddb[plane_id];
5883 const struct skl_ddb_entry *ddb_y =
5884 &crtc_state->wm.skl.plane_ddb_y[plane_id];
5886 for (level = 0; level <= max_level; level++)
5887 skl_write_wm_level(dev_priv, PLANE_WM(pipe, plane_id, level),
5888 skl_plane_wm_level(pipe_wm, plane_id, level));
5890 skl_write_wm_level(dev_priv, PLANE_WM_TRANS(pipe, plane_id),
5891 skl_plane_trans_wm(pipe_wm, plane_id));
5893 if (HAS_HW_SAGV_WM(dev_priv)) {
5894 const struct skl_plane_wm *wm = &pipe_wm->planes[plane_id];
5896 skl_write_wm_level(dev_priv, PLANE_WM_SAGV(pipe, plane_id),
5898 skl_write_wm_level(dev_priv, PLANE_WM_SAGV_TRANS(pipe, plane_id),
5899 &wm->sagv.trans_wm);
5902 skl_ddb_entry_write(dev_priv,
5903 PLANE_BUF_CFG(pipe, plane_id), ddb);
5905 if (DISPLAY_VER(dev_priv) < 11)
5906 skl_ddb_entry_write(dev_priv,
5907 PLANE_NV12_BUF_CFG(pipe, plane_id), ddb_y);
5910 void skl_write_cursor_wm(struct intel_plane *plane,
5911 const struct intel_crtc_state *crtc_state)
5913 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
5914 int level, max_level = ilk_wm_max_level(dev_priv);
5915 enum plane_id plane_id = plane->id;
5916 enum pipe pipe = plane->pipe;
5917 const struct skl_pipe_wm *pipe_wm = &crtc_state->wm.skl.optimal;
5918 const struct skl_ddb_entry *ddb =
5919 &crtc_state->wm.skl.plane_ddb[plane_id];
5921 for (level = 0; level <= max_level; level++)
5922 skl_write_wm_level(dev_priv, CUR_WM(pipe, level),
5923 skl_plane_wm_level(pipe_wm, plane_id, level));
5925 skl_write_wm_level(dev_priv, CUR_WM_TRANS(pipe),
5926 skl_plane_trans_wm(pipe_wm, plane_id));
5928 if (HAS_HW_SAGV_WM(dev_priv)) {
5929 const struct skl_plane_wm *wm = &pipe_wm->planes[plane_id];
5931 skl_write_wm_level(dev_priv, CUR_WM_SAGV(pipe),
5933 skl_write_wm_level(dev_priv, CUR_WM_SAGV_TRANS(pipe),
5934 &wm->sagv.trans_wm);
5937 skl_ddb_entry_write(dev_priv, CUR_BUF_CFG(pipe), ddb);
5940 static bool skl_wm_level_equals(const struct skl_wm_level *l1,
5941 const struct skl_wm_level *l2)
5943 return l1->enable == l2->enable &&
5944 l1->ignore_lines == l2->ignore_lines &&
5945 l1->lines == l2->lines &&
5946 l1->blocks == l2->blocks;
5949 static bool skl_plane_wm_equals(struct drm_i915_private *dev_priv,
5950 const struct skl_plane_wm *wm1,
5951 const struct skl_plane_wm *wm2)
5953 int level, max_level = ilk_wm_max_level(dev_priv);
5955 for (level = 0; level <= max_level; level++) {
5957 * We don't check uv_wm as the hardware doesn't actually
5958 * use it. It only gets used for calculating the required
5961 if (!skl_wm_level_equals(&wm1->wm[level], &wm2->wm[level]))
5965 return skl_wm_level_equals(&wm1->trans_wm, &wm2->trans_wm) &&
5966 skl_wm_level_equals(&wm1->sagv.wm0, &wm2->sagv.wm0) &&
5967 skl_wm_level_equals(&wm1->sagv.trans_wm, &wm2->sagv.trans_wm);
5970 static bool skl_ddb_entries_overlap(const struct skl_ddb_entry *a,
5971 const struct skl_ddb_entry *b)
5973 return a->start < b->end && b->start < a->end;
5976 static void skl_ddb_entry_union(struct skl_ddb_entry *a,
5977 const struct skl_ddb_entry *b)
5979 if (a->end && b->end) {
5980 a->start = min(a->start, b->start);
5981 a->end = max(a->end, b->end);
5982 } else if (b->end) {
5983 a->start = b->start;
5988 bool skl_ddb_allocation_overlaps(const struct skl_ddb_entry *ddb,
5989 const struct skl_ddb_entry *entries,
5990 int num_entries, int ignore_idx)
5994 for (i = 0; i < num_entries; i++) {
5995 if (i != ignore_idx &&
5996 skl_ddb_entries_overlap(ddb, &entries[i]))
6004 skl_ddb_add_affected_planes(const struct intel_crtc_state *old_crtc_state,
6005 struct intel_crtc_state *new_crtc_state)
6007 struct intel_atomic_state *state = to_intel_atomic_state(new_crtc_state->uapi.state);
6008 struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->uapi.crtc);
6009 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6010 struct intel_plane *plane;
6012 for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane) {
6013 struct intel_plane_state *plane_state;
6014 enum plane_id plane_id = plane->id;
6016 if (skl_ddb_entry_equal(&old_crtc_state->wm.skl.plane_ddb[plane_id],
6017 &new_crtc_state->wm.skl.plane_ddb[plane_id]) &&
6018 skl_ddb_entry_equal(&old_crtc_state->wm.skl.plane_ddb_y[plane_id],
6019 &new_crtc_state->wm.skl.plane_ddb_y[plane_id]))
6022 plane_state = intel_atomic_get_plane_state(state, plane);
6023 if (IS_ERR(plane_state))
6024 return PTR_ERR(plane_state);
6026 new_crtc_state->update_planes |= BIT(plane_id);
6032 static u8 intel_dbuf_enabled_slices(const struct intel_dbuf_state *dbuf_state)
6034 struct drm_i915_private *dev_priv = to_i915(dbuf_state->base.state->base.dev);
6039 * FIXME: For now we always enable slice S1 as per
6040 * the Bspec display initialization sequence.
6042 enabled_slices = BIT(DBUF_S1);
6044 for_each_pipe(dev_priv, pipe)
6045 enabled_slices |= dbuf_state->slices[pipe];
6047 return enabled_slices;
6051 skl_compute_ddb(struct intel_atomic_state *state)
6053 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
6054 const struct intel_dbuf_state *old_dbuf_state;
6055 struct intel_dbuf_state *new_dbuf_state = NULL;
6056 const struct intel_crtc_state *old_crtc_state;
6057 struct intel_crtc_state *new_crtc_state;
6058 struct intel_crtc *crtc;
6061 for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
6062 new_dbuf_state = intel_atomic_get_dbuf_state(state);
6063 if (IS_ERR(new_dbuf_state))
6064 return PTR_ERR(new_dbuf_state);
6066 old_dbuf_state = intel_atomic_get_old_dbuf_state(state);
6070 if (!new_dbuf_state)
6073 new_dbuf_state->active_pipes =
6074 intel_calc_active_pipes(state, old_dbuf_state->active_pipes);
6076 if (old_dbuf_state->active_pipes != new_dbuf_state->active_pipes) {
6077 ret = intel_atomic_lock_global_state(&new_dbuf_state->base);
6082 if (HAS_MBUS_JOINING(dev_priv))
6083 new_dbuf_state->joined_mbus =
6084 adlp_check_mbus_joined(new_dbuf_state->active_pipes);
6086 for_each_intel_crtc(&dev_priv->drm, crtc) {
6087 enum pipe pipe = crtc->pipe;
6089 new_dbuf_state->slices[pipe] =
6090 skl_compute_dbuf_slices(crtc, new_dbuf_state->active_pipes,
6091 new_dbuf_state->joined_mbus);
6093 if (old_dbuf_state->slices[pipe] == new_dbuf_state->slices[pipe])
6096 ret = intel_atomic_lock_global_state(&new_dbuf_state->base);
6101 new_dbuf_state->enabled_slices = intel_dbuf_enabled_slices(new_dbuf_state);
6103 if (old_dbuf_state->enabled_slices != new_dbuf_state->enabled_slices ||
6104 old_dbuf_state->joined_mbus != new_dbuf_state->joined_mbus) {
6105 ret = intel_atomic_serialize_global_state(&new_dbuf_state->base);
6109 if (old_dbuf_state->joined_mbus != new_dbuf_state->joined_mbus) {
6110 /* TODO: Implement vblank synchronized MBUS joining changes */
6111 ret = intel_modeset_all_pipes(state);
6116 drm_dbg_kms(&dev_priv->drm,
6117 "Enabled dbuf slices 0x%x -> 0x%x (total dbuf slices 0x%x), mbus joined? %s->%s\n",
6118 old_dbuf_state->enabled_slices,
6119 new_dbuf_state->enabled_slices,
6120 INTEL_INFO(dev_priv)->display.dbuf.slice_mask,
6121 str_yes_no(old_dbuf_state->joined_mbus),
6122 str_yes_no(new_dbuf_state->joined_mbus));
6125 for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
6126 enum pipe pipe = crtc->pipe;
6128 new_dbuf_state->weight[pipe] = intel_crtc_ddb_weight(new_crtc_state);
6130 if (old_dbuf_state->weight[pipe] == new_dbuf_state->weight[pipe])
6133 ret = intel_atomic_lock_global_state(&new_dbuf_state->base);
6138 for_each_intel_crtc(&dev_priv->drm, crtc) {
6139 ret = skl_crtc_allocate_ddb(state, crtc);
6144 for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
6145 new_crtc_state, i) {
6146 ret = skl_crtc_allocate_plane_ddb(state, crtc);
6150 ret = skl_ddb_add_affected_planes(old_crtc_state,
6159 static char enast(bool enable)
6161 return enable ? '*' : ' ';
6165 skl_print_wm_changes(struct intel_atomic_state *state)
6167 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
6168 const struct intel_crtc_state *old_crtc_state;
6169 const struct intel_crtc_state *new_crtc_state;
6170 struct intel_plane *plane;
6171 struct intel_crtc *crtc;
6174 if (!drm_debug_enabled(DRM_UT_KMS))
6177 for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
6178 new_crtc_state, i) {
6179 const struct skl_pipe_wm *old_pipe_wm, *new_pipe_wm;
6181 old_pipe_wm = &old_crtc_state->wm.skl.optimal;
6182 new_pipe_wm = &new_crtc_state->wm.skl.optimal;
6184 for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane) {
6185 enum plane_id plane_id = plane->id;
6186 const struct skl_ddb_entry *old, *new;
6188 old = &old_crtc_state->wm.skl.plane_ddb[plane_id];
6189 new = &new_crtc_state->wm.skl.plane_ddb[plane_id];
6191 if (skl_ddb_entry_equal(old, new))
6194 drm_dbg_kms(&dev_priv->drm,
6195 "[PLANE:%d:%s] ddb (%4d - %4d) -> (%4d - %4d), size %4d -> %4d\n",
6196 plane->base.base.id, plane->base.name,
6197 old->start, old->end, new->start, new->end,
6198 skl_ddb_entry_size(old), skl_ddb_entry_size(new));
6201 for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane) {
6202 enum plane_id plane_id = plane->id;
6203 const struct skl_plane_wm *old_wm, *new_wm;
6205 old_wm = &old_pipe_wm->planes[plane_id];
6206 new_wm = &new_pipe_wm->planes[plane_id];
6208 if (skl_plane_wm_equals(dev_priv, old_wm, new_wm))
6211 drm_dbg_kms(&dev_priv->drm,
6212 "[PLANE:%d:%s] level %cwm0,%cwm1,%cwm2,%cwm3,%cwm4,%cwm5,%cwm6,%cwm7,%ctwm,%cswm,%cstwm"
6213 " -> %cwm0,%cwm1,%cwm2,%cwm3,%cwm4,%cwm5,%cwm6,%cwm7,%ctwm,%cswm,%cstwm\n",
6214 plane->base.base.id, plane->base.name,
6215 enast(old_wm->wm[0].enable), enast(old_wm->wm[1].enable),
6216 enast(old_wm->wm[2].enable), enast(old_wm->wm[3].enable),
6217 enast(old_wm->wm[4].enable), enast(old_wm->wm[5].enable),
6218 enast(old_wm->wm[6].enable), enast(old_wm->wm[7].enable),
6219 enast(old_wm->trans_wm.enable),
6220 enast(old_wm->sagv.wm0.enable),
6221 enast(old_wm->sagv.trans_wm.enable),
6222 enast(new_wm->wm[0].enable), enast(new_wm->wm[1].enable),
6223 enast(new_wm->wm[2].enable), enast(new_wm->wm[3].enable),
6224 enast(new_wm->wm[4].enable), enast(new_wm->wm[5].enable),
6225 enast(new_wm->wm[6].enable), enast(new_wm->wm[7].enable),
6226 enast(new_wm->trans_wm.enable),
6227 enast(new_wm->sagv.wm0.enable),
6228 enast(new_wm->sagv.trans_wm.enable));
6230 drm_dbg_kms(&dev_priv->drm,
6231 "[PLANE:%d:%s] lines %c%3d,%c%3d,%c%3d,%c%3d,%c%3d,%c%3d,%c%3d,%c%3d,%c%3d,%c%3d,%c%4d"
6232 " -> %c%3d,%c%3d,%c%3d,%c%3d,%c%3d,%c%3d,%c%3d,%c%3d,%c%3d,%c%3d,%c%4d\n",
6233 plane->base.base.id, plane->base.name,
6234 enast(old_wm->wm[0].ignore_lines), old_wm->wm[0].lines,
6235 enast(old_wm->wm[1].ignore_lines), old_wm->wm[1].lines,
6236 enast(old_wm->wm[2].ignore_lines), old_wm->wm[2].lines,
6237 enast(old_wm->wm[3].ignore_lines), old_wm->wm[3].lines,
6238 enast(old_wm->wm[4].ignore_lines), old_wm->wm[4].lines,
6239 enast(old_wm->wm[5].ignore_lines), old_wm->wm[5].lines,
6240 enast(old_wm->wm[6].ignore_lines), old_wm->wm[6].lines,
6241 enast(old_wm->wm[7].ignore_lines), old_wm->wm[7].lines,
6242 enast(old_wm->trans_wm.ignore_lines), old_wm->trans_wm.lines,
6243 enast(old_wm->sagv.wm0.ignore_lines), old_wm->sagv.wm0.lines,
6244 enast(old_wm->sagv.trans_wm.ignore_lines), old_wm->sagv.trans_wm.lines,
6245 enast(new_wm->wm[0].ignore_lines), new_wm->wm[0].lines,
6246 enast(new_wm->wm[1].ignore_lines), new_wm->wm[1].lines,
6247 enast(new_wm->wm[2].ignore_lines), new_wm->wm[2].lines,
6248 enast(new_wm->wm[3].ignore_lines), new_wm->wm[3].lines,
6249 enast(new_wm->wm[4].ignore_lines), new_wm->wm[4].lines,
6250 enast(new_wm->wm[5].ignore_lines), new_wm->wm[5].lines,
6251 enast(new_wm->wm[6].ignore_lines), new_wm->wm[6].lines,
6252 enast(new_wm->wm[7].ignore_lines), new_wm->wm[7].lines,
6253 enast(new_wm->trans_wm.ignore_lines), new_wm->trans_wm.lines,
6254 enast(new_wm->sagv.wm0.ignore_lines), new_wm->sagv.wm0.lines,
6255 enast(new_wm->sagv.trans_wm.ignore_lines), new_wm->sagv.trans_wm.lines);
6257 drm_dbg_kms(&dev_priv->drm,
6258 "[PLANE:%d:%s] blocks %4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%5d"
6259 " -> %4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%5d\n",
6260 plane->base.base.id, plane->base.name,
6261 old_wm->wm[0].blocks, old_wm->wm[1].blocks,
6262 old_wm->wm[2].blocks, old_wm->wm[3].blocks,
6263 old_wm->wm[4].blocks, old_wm->wm[5].blocks,
6264 old_wm->wm[6].blocks, old_wm->wm[7].blocks,
6265 old_wm->trans_wm.blocks,
6266 old_wm->sagv.wm0.blocks,
6267 old_wm->sagv.trans_wm.blocks,
6268 new_wm->wm[0].blocks, new_wm->wm[1].blocks,
6269 new_wm->wm[2].blocks, new_wm->wm[3].blocks,
6270 new_wm->wm[4].blocks, new_wm->wm[5].blocks,
6271 new_wm->wm[6].blocks, new_wm->wm[7].blocks,
6272 new_wm->trans_wm.blocks,
6273 new_wm->sagv.wm0.blocks,
6274 new_wm->sagv.trans_wm.blocks);
6276 drm_dbg_kms(&dev_priv->drm,
6277 "[PLANE:%d:%s] min_ddb %4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%5d"
6278 " -> %4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%5d\n",
6279 plane->base.base.id, plane->base.name,
6280 old_wm->wm[0].min_ddb_alloc, old_wm->wm[1].min_ddb_alloc,
6281 old_wm->wm[2].min_ddb_alloc, old_wm->wm[3].min_ddb_alloc,
6282 old_wm->wm[4].min_ddb_alloc, old_wm->wm[5].min_ddb_alloc,
6283 old_wm->wm[6].min_ddb_alloc, old_wm->wm[7].min_ddb_alloc,
6284 old_wm->trans_wm.min_ddb_alloc,
6285 old_wm->sagv.wm0.min_ddb_alloc,
6286 old_wm->sagv.trans_wm.min_ddb_alloc,
6287 new_wm->wm[0].min_ddb_alloc, new_wm->wm[1].min_ddb_alloc,
6288 new_wm->wm[2].min_ddb_alloc, new_wm->wm[3].min_ddb_alloc,
6289 new_wm->wm[4].min_ddb_alloc, new_wm->wm[5].min_ddb_alloc,
6290 new_wm->wm[6].min_ddb_alloc, new_wm->wm[7].min_ddb_alloc,
6291 new_wm->trans_wm.min_ddb_alloc,
6292 new_wm->sagv.wm0.min_ddb_alloc,
6293 new_wm->sagv.trans_wm.min_ddb_alloc);
6298 static bool skl_plane_selected_wm_equals(struct intel_plane *plane,
6299 const struct skl_pipe_wm *old_pipe_wm,
6300 const struct skl_pipe_wm *new_pipe_wm)
6302 struct drm_i915_private *i915 = to_i915(plane->base.dev);
6303 int level, max_level = ilk_wm_max_level(i915);
6305 for (level = 0; level <= max_level; level++) {
6307 * We don't check uv_wm as the hardware doesn't actually
6308 * use it. It only gets used for calculating the required
6311 if (!skl_wm_level_equals(skl_plane_wm_level(old_pipe_wm, plane->id, level),
6312 skl_plane_wm_level(new_pipe_wm, plane->id, level)))
6316 if (HAS_HW_SAGV_WM(i915)) {
6317 const struct skl_plane_wm *old_wm = &old_pipe_wm->planes[plane->id];
6318 const struct skl_plane_wm *new_wm = &new_pipe_wm->planes[plane->id];
6320 if (!skl_wm_level_equals(&old_wm->sagv.wm0, &new_wm->sagv.wm0) ||
6321 !skl_wm_level_equals(&old_wm->sagv.trans_wm, &new_wm->sagv.trans_wm))
6325 return skl_wm_level_equals(skl_plane_trans_wm(old_pipe_wm, plane->id),
6326 skl_plane_trans_wm(new_pipe_wm, plane->id));
6330 * To make sure the cursor watermark registers are always consistent
6331 * with our computed state the following scenario needs special
6335 * 2. move cursor entirely offscreen
6338 * Step 2. does call .disable_plane() but does not zero the watermarks
6339 * (since we consider an offscreen cursor still active for the purposes
6340 * of watermarks). Step 3. would not normally call .disable_plane()
6341 * because the actual plane visibility isn't changing, and we don't
6342 * deallocate the cursor ddb until the pipe gets disabled. So we must
6343 * force step 3. to call .disable_plane() to update the watermark
6344 * registers properly.
6346 * Other planes do not suffer from this issues as their watermarks are
6347 * calculated based on the actual plane visibility. The only time this
6348 * can trigger for the other planes is during the initial readout as the
6349 * default value of the watermarks registers is not zero.
6351 static int skl_wm_add_affected_planes(struct intel_atomic_state *state,
6352 struct intel_crtc *crtc)
6354 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6355 const struct intel_crtc_state *old_crtc_state =
6356 intel_atomic_get_old_crtc_state(state, crtc);
6357 struct intel_crtc_state *new_crtc_state =
6358 intel_atomic_get_new_crtc_state(state, crtc);
6359 struct intel_plane *plane;
6361 for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane) {
6362 struct intel_plane_state *plane_state;
6363 enum plane_id plane_id = plane->id;
6366 * Force a full wm update for every plane on modeset.
6367 * Required because the reset value of the wm registers
6368 * is non-zero, whereas we want all disabled planes to
6369 * have zero watermarks. So if we turn off the relevant
6370 * power well the hardware state will go out of sync
6371 * with the software state.
6373 if (!drm_atomic_crtc_needs_modeset(&new_crtc_state->uapi) &&
6374 skl_plane_selected_wm_equals(plane,
6375 &old_crtc_state->wm.skl.optimal,
6376 &new_crtc_state->wm.skl.optimal))
6379 plane_state = intel_atomic_get_plane_state(state, plane);
6380 if (IS_ERR(plane_state))
6381 return PTR_ERR(plane_state);
6383 new_crtc_state->update_planes |= BIT(plane_id);
6390 skl_compute_wm(struct intel_atomic_state *state)
6392 struct intel_crtc *crtc;
6393 struct intel_crtc_state *new_crtc_state;
6396 for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
6397 ret = skl_build_pipe_wm(state, crtc);
6402 ret = skl_compute_ddb(state);
6406 ret = intel_compute_sagv_mask(state);
6411 * skl_compute_ddb() will have adjusted the final watermarks
6412 * based on how much ddb is available. Now we can actually
6413 * check if the final watermarks changed.
6415 for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
6416 ret = skl_wm_add_affected_planes(state, crtc);
6421 skl_print_wm_changes(state);
6426 static void ilk_compute_wm_config(struct drm_i915_private *dev_priv,
6427 struct intel_wm_config *config)
6429 struct intel_crtc *crtc;
6431 /* Compute the currently _active_ config */
6432 for_each_intel_crtc(&dev_priv->drm, crtc) {
6433 const struct intel_pipe_wm *wm = &crtc->wm.active.ilk;
6435 if (!wm->pipe_enabled)
6438 config->sprites_enabled |= wm->sprites_enabled;
6439 config->sprites_scaled |= wm->sprites_scaled;
6440 config->num_pipes_active++;
6444 static void ilk_program_watermarks(struct drm_i915_private *dev_priv)
6446 struct intel_pipe_wm lp_wm_1_2 = {}, lp_wm_5_6 = {}, *best_lp_wm;
6447 struct ilk_wm_maximums max;
6448 struct intel_wm_config config = {};
6449 struct ilk_wm_values results = {};
6450 enum intel_ddb_partitioning partitioning;
6452 ilk_compute_wm_config(dev_priv, &config);
6454 ilk_compute_wm_maximums(dev_priv, 1, &config, INTEL_DDB_PART_1_2, &max);
6455 ilk_wm_merge(dev_priv, &config, &max, &lp_wm_1_2);
6457 /* 5/6 split only in single pipe config on IVB+ */
6458 if (DISPLAY_VER(dev_priv) >= 7 &&
6459 config.num_pipes_active == 1 && config.sprites_enabled) {
6460 ilk_compute_wm_maximums(dev_priv, 1, &config, INTEL_DDB_PART_5_6, &max);
6461 ilk_wm_merge(dev_priv, &config, &max, &lp_wm_5_6);
6463 best_lp_wm = ilk_find_best_result(dev_priv, &lp_wm_1_2, &lp_wm_5_6);
6465 best_lp_wm = &lp_wm_1_2;
6468 partitioning = (best_lp_wm == &lp_wm_1_2) ?
6469 INTEL_DDB_PART_1_2 : INTEL_DDB_PART_5_6;
6471 ilk_compute_wm_results(dev_priv, best_lp_wm, partitioning, &results);
6473 ilk_write_wm_values(dev_priv, &results);
6476 static void ilk_initial_watermarks(struct intel_atomic_state *state,
6477 struct intel_crtc *crtc)
6479 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6480 const struct intel_crtc_state *crtc_state =
6481 intel_atomic_get_new_crtc_state(state, crtc);
6483 mutex_lock(&dev_priv->display.wm.wm_mutex);
6484 crtc->wm.active.ilk = crtc_state->wm.ilk.intermediate;
6485 ilk_program_watermarks(dev_priv);
6486 mutex_unlock(&dev_priv->display.wm.wm_mutex);
6489 static void ilk_optimize_watermarks(struct intel_atomic_state *state,
6490 struct intel_crtc *crtc)
6492 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6493 const struct intel_crtc_state *crtc_state =
6494 intel_atomic_get_new_crtc_state(state, crtc);
6496 if (!crtc_state->wm.need_postvbl_update)
6499 mutex_lock(&dev_priv->display.wm.wm_mutex);
6500 crtc->wm.active.ilk = crtc_state->wm.ilk.optimal;
6501 ilk_program_watermarks(dev_priv);
6502 mutex_unlock(&dev_priv->display.wm.wm_mutex);
6505 static void skl_wm_level_from_reg_val(u32 val, struct skl_wm_level *level)
6507 level->enable = val & PLANE_WM_EN;
6508 level->ignore_lines = val & PLANE_WM_IGNORE_LINES;
6509 level->blocks = REG_FIELD_GET(PLANE_WM_BLOCKS_MASK, val);
6510 level->lines = REG_FIELD_GET(PLANE_WM_LINES_MASK, val);
6513 static void skl_pipe_wm_get_hw_state(struct intel_crtc *crtc,
6514 struct skl_pipe_wm *out)
6516 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6517 enum pipe pipe = crtc->pipe;
6518 int level, max_level;
6519 enum plane_id plane_id;
6522 max_level = ilk_wm_max_level(dev_priv);
6524 for_each_plane_id_on_crtc(crtc, plane_id) {
6525 struct skl_plane_wm *wm = &out->planes[plane_id];
6527 for (level = 0; level <= max_level; level++) {
6528 if (plane_id != PLANE_CURSOR)
6529 val = intel_uncore_read(&dev_priv->uncore, PLANE_WM(pipe, plane_id, level));
6531 val = intel_uncore_read(&dev_priv->uncore, CUR_WM(pipe, level));
6533 skl_wm_level_from_reg_val(val, &wm->wm[level]);
6536 if (plane_id != PLANE_CURSOR)
6537 val = intel_uncore_read(&dev_priv->uncore, PLANE_WM_TRANS(pipe, plane_id));
6539 val = intel_uncore_read(&dev_priv->uncore, CUR_WM_TRANS(pipe));
6541 skl_wm_level_from_reg_val(val, &wm->trans_wm);
6543 if (HAS_HW_SAGV_WM(dev_priv)) {
6544 if (plane_id != PLANE_CURSOR)
6545 val = intel_uncore_read(&dev_priv->uncore,
6546 PLANE_WM_SAGV(pipe, plane_id));
6548 val = intel_uncore_read(&dev_priv->uncore,
6551 skl_wm_level_from_reg_val(val, &wm->sagv.wm0);
6553 if (plane_id != PLANE_CURSOR)
6554 val = intel_uncore_read(&dev_priv->uncore,
6555 PLANE_WM_SAGV_TRANS(pipe, plane_id));
6557 val = intel_uncore_read(&dev_priv->uncore,
6558 CUR_WM_SAGV_TRANS(pipe));
6560 skl_wm_level_from_reg_val(val, &wm->sagv.trans_wm);
6561 } else if (DISPLAY_VER(dev_priv) >= 12) {
6562 wm->sagv.wm0 = wm->wm[0];
6563 wm->sagv.trans_wm = wm->trans_wm;
6568 void skl_wm_get_hw_state(struct drm_i915_private *dev_priv)
6570 struct intel_dbuf_state *dbuf_state =
6571 to_intel_dbuf_state(dev_priv->dbuf.obj.state);
6572 struct intel_crtc *crtc;
6574 if (HAS_MBUS_JOINING(dev_priv))
6575 dbuf_state->joined_mbus = intel_de_read(dev_priv, MBUS_CTL) & MBUS_JOIN;
6577 for_each_intel_crtc(&dev_priv->drm, crtc) {
6578 struct intel_crtc_state *crtc_state =
6579 to_intel_crtc_state(crtc->base.state);
6580 enum pipe pipe = crtc->pipe;
6581 unsigned int mbus_offset;
6582 enum plane_id plane_id;
6585 memset(&crtc_state->wm.skl.optimal, 0,
6586 sizeof(crtc_state->wm.skl.optimal));
6587 if (crtc_state->hw.active)
6588 skl_pipe_wm_get_hw_state(crtc, &crtc_state->wm.skl.optimal);
6589 crtc_state->wm.skl.raw = crtc_state->wm.skl.optimal;
6591 memset(&dbuf_state->ddb[pipe], 0, sizeof(dbuf_state->ddb[pipe]));
6593 for_each_plane_id_on_crtc(crtc, plane_id) {
6594 struct skl_ddb_entry *ddb =
6595 &crtc_state->wm.skl.plane_ddb[plane_id];
6596 struct skl_ddb_entry *ddb_y =
6597 &crtc_state->wm.skl.plane_ddb_y[plane_id];
6599 if (!crtc_state->hw.active)
6602 skl_ddb_get_hw_plane_state(dev_priv, crtc->pipe,
6603 plane_id, ddb, ddb_y);
6605 skl_ddb_entry_union(&dbuf_state->ddb[pipe], ddb);
6606 skl_ddb_entry_union(&dbuf_state->ddb[pipe], ddb_y);
6609 dbuf_state->weight[pipe] = intel_crtc_ddb_weight(crtc_state);
6612 * Used for checking overlaps, so we need absolute
6613 * offsets instead of MBUS relative offsets.
6615 slices = skl_compute_dbuf_slices(crtc, dbuf_state->active_pipes,
6616 dbuf_state->joined_mbus);
6617 mbus_offset = mbus_ddb_offset(dev_priv, slices);
6618 crtc_state->wm.skl.ddb.start = mbus_offset + dbuf_state->ddb[pipe].start;
6619 crtc_state->wm.skl.ddb.end = mbus_offset + dbuf_state->ddb[pipe].end;
6621 /* The slices actually used by the planes on the pipe */
6622 dbuf_state->slices[pipe] =
6623 skl_ddb_dbuf_slice_mask(dev_priv, &crtc_state->wm.skl.ddb);
6625 drm_dbg_kms(&dev_priv->drm,
6626 "[CRTC:%d:%s] dbuf slices 0x%x, ddb (%d - %d), active pipes 0x%x, mbus joined: %s\n",
6627 crtc->base.base.id, crtc->base.name,
6628 dbuf_state->slices[pipe], dbuf_state->ddb[pipe].start,
6629 dbuf_state->ddb[pipe].end, dbuf_state->active_pipes,
6630 str_yes_no(dbuf_state->joined_mbus));
6633 dbuf_state->enabled_slices = dev_priv->dbuf.enabled_slices;
6636 static bool skl_dbuf_is_misconfigured(struct drm_i915_private *i915)
6638 const struct intel_dbuf_state *dbuf_state =
6639 to_intel_dbuf_state(i915->dbuf.obj.state);
6640 struct skl_ddb_entry entries[I915_MAX_PIPES] = {};
6641 struct intel_crtc *crtc;
6643 for_each_intel_crtc(&i915->drm, crtc) {
6644 const struct intel_crtc_state *crtc_state =
6645 to_intel_crtc_state(crtc->base.state);
6647 entries[crtc->pipe] = crtc_state->wm.skl.ddb;
6650 for_each_intel_crtc(&i915->drm, crtc) {
6651 const struct intel_crtc_state *crtc_state =
6652 to_intel_crtc_state(crtc->base.state);
6655 slices = skl_compute_dbuf_slices(crtc, dbuf_state->active_pipes,
6656 dbuf_state->joined_mbus);
6657 if (dbuf_state->slices[crtc->pipe] & ~slices)
6660 if (skl_ddb_allocation_overlaps(&crtc_state->wm.skl.ddb, entries,
6661 I915_MAX_PIPES, crtc->pipe))
6668 void skl_wm_sanitize(struct drm_i915_private *i915)
6670 struct intel_crtc *crtc;
6673 * On TGL/RKL (at least) the BIOS likes to assign the planes
6674 * to the wrong DBUF slices. This will cause an infinite loop
6675 * in skl_commit_modeset_enables() as it can't find a way to
6676 * transition between the old bogus DBUF layout to the new
6677 * proper DBUF layout without DBUF allocation overlaps between
6678 * the planes (which cannot be allowed or else the hardware
6679 * may hang). If we detect a bogus DBUF layout just turn off
6680 * all the planes so that skl_commit_modeset_enables() can
6681 * simply ignore them.
6683 if (!skl_dbuf_is_misconfigured(i915))
6686 drm_dbg_kms(&i915->drm, "BIOS has misprogrammed the DBUF, disabling all planes\n");
6688 for_each_intel_crtc(&i915->drm, crtc) {
6689 struct intel_plane *plane = to_intel_plane(crtc->base.primary);
6690 const struct intel_plane_state *plane_state =
6691 to_intel_plane_state(plane->base.state);
6692 struct intel_crtc_state *crtc_state =
6693 to_intel_crtc_state(crtc->base.state);
6695 if (plane_state->uapi.visible)
6696 intel_plane_disable_noatomic(crtc, plane);
6698 drm_WARN_ON(&i915->drm, crtc_state->active_planes != 0);
6700 memset(&crtc_state->wm.skl.ddb, 0, sizeof(crtc_state->wm.skl.ddb));
6704 static void ilk_pipe_wm_get_hw_state(struct intel_crtc *crtc)
6706 struct drm_device *dev = crtc->base.dev;
6707 struct drm_i915_private *dev_priv = to_i915(dev);
6708 struct ilk_wm_values *hw = &dev_priv->display.wm.hw;
6709 struct intel_crtc_state *crtc_state = to_intel_crtc_state(crtc->base.state);
6710 struct intel_pipe_wm *active = &crtc_state->wm.ilk.optimal;
6711 enum pipe pipe = crtc->pipe;
6713 hw->wm_pipe[pipe] = intel_uncore_read(&dev_priv->uncore, WM0_PIPE_ILK(pipe));
6715 memset(active, 0, sizeof(*active));
6717 active->pipe_enabled = crtc->active;
6719 if (active->pipe_enabled) {
6720 u32 tmp = hw->wm_pipe[pipe];
6723 * For active pipes LP0 watermark is marked as
6724 * enabled, and LP1+ watermaks as disabled since
6725 * we can't really reverse compute them in case
6726 * multiple pipes are active.
6728 active->wm[0].enable = true;
6729 active->wm[0].pri_val = REG_FIELD_GET(WM0_PIPE_PRIMARY_MASK, tmp);
6730 active->wm[0].spr_val = REG_FIELD_GET(WM0_PIPE_SPRITE_MASK, tmp);
6731 active->wm[0].cur_val = REG_FIELD_GET(WM0_PIPE_CURSOR_MASK, tmp);
6733 int level, max_level = ilk_wm_max_level(dev_priv);
6736 * For inactive pipes, all watermark levels
6737 * should be marked as enabled but zeroed,
6738 * which is what we'd compute them to.
6740 for (level = 0; level <= max_level; level++)
6741 active->wm[level].enable = true;
6744 crtc->wm.active.ilk = *active;
6747 #define _FW_WM(value, plane) \
6748 (((value) & DSPFW_ ## plane ## _MASK) >> DSPFW_ ## plane ## _SHIFT)
6749 #define _FW_WM_VLV(value, plane) \
6750 (((value) & DSPFW_ ## plane ## _MASK_VLV) >> DSPFW_ ## plane ## _SHIFT)
6752 static void g4x_read_wm_values(struct drm_i915_private *dev_priv,
6753 struct g4x_wm_values *wm)
6757 tmp = intel_uncore_read(&dev_priv->uncore, DSPFW1);
6758 wm->sr.plane = _FW_WM(tmp, SR);
6759 wm->pipe[PIPE_B].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORB);
6760 wm->pipe[PIPE_B].plane[PLANE_PRIMARY] = _FW_WM(tmp, PLANEB);
6761 wm->pipe[PIPE_A].plane[PLANE_PRIMARY] = _FW_WM(tmp, PLANEA);
6763 tmp = intel_uncore_read(&dev_priv->uncore, DSPFW2);
6764 wm->fbc_en = tmp & DSPFW_FBC_SR_EN;
6765 wm->sr.fbc = _FW_WM(tmp, FBC_SR);
6766 wm->hpll.fbc = _FW_WM(tmp, FBC_HPLL_SR);
6767 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] = _FW_WM(tmp, SPRITEB);
6768 wm->pipe[PIPE_A].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORA);
6769 wm->pipe[PIPE_A].plane[PLANE_SPRITE0] = _FW_WM(tmp, SPRITEA);
6771 tmp = intel_uncore_read(&dev_priv->uncore, DSPFW3);
6772 wm->hpll_en = tmp & DSPFW_HPLL_SR_EN;
6773 wm->sr.cursor = _FW_WM(tmp, CURSOR_SR);
6774 wm->hpll.cursor = _FW_WM(tmp, HPLL_CURSOR);
6775 wm->hpll.plane = _FW_WM(tmp, HPLL_SR);
6778 static void vlv_read_wm_values(struct drm_i915_private *dev_priv,
6779 struct vlv_wm_values *wm)
6784 for_each_pipe(dev_priv, pipe) {
6785 tmp = intel_uncore_read(&dev_priv->uncore, VLV_DDL(pipe));
6787 wm->ddl[pipe].plane[PLANE_PRIMARY] =
6788 (tmp >> DDL_PLANE_SHIFT) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
6789 wm->ddl[pipe].plane[PLANE_CURSOR] =
6790 (tmp >> DDL_CURSOR_SHIFT) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
6791 wm->ddl[pipe].plane[PLANE_SPRITE0] =
6792 (tmp >> DDL_SPRITE_SHIFT(0)) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
6793 wm->ddl[pipe].plane[PLANE_SPRITE1] =
6794 (tmp >> DDL_SPRITE_SHIFT(1)) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
6797 tmp = intel_uncore_read(&dev_priv->uncore, DSPFW1);
6798 wm->sr.plane = _FW_WM(tmp, SR);
6799 wm->pipe[PIPE_B].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORB);
6800 wm->pipe[PIPE_B].plane[PLANE_PRIMARY] = _FW_WM_VLV(tmp, PLANEB);
6801 wm->pipe[PIPE_A].plane[PLANE_PRIMARY] = _FW_WM_VLV(tmp, PLANEA);
6803 tmp = intel_uncore_read(&dev_priv->uncore, DSPFW2);
6804 wm->pipe[PIPE_A].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITEB);
6805 wm->pipe[PIPE_A].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORA);
6806 wm->pipe[PIPE_A].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEA);
6808 tmp = intel_uncore_read(&dev_priv->uncore, DSPFW3);
6809 wm->sr.cursor = _FW_WM(tmp, CURSOR_SR);
6811 if (IS_CHERRYVIEW(dev_priv)) {
6812 tmp = intel_uncore_read(&dev_priv->uncore, DSPFW7_CHV);
6813 wm->pipe[PIPE_B].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITED);
6814 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEC);
6816 tmp = intel_uncore_read(&dev_priv->uncore, DSPFW8_CHV);
6817 wm->pipe[PIPE_C].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITEF);
6818 wm->pipe[PIPE_C].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEE);
6820 tmp = intel_uncore_read(&dev_priv->uncore, DSPFW9_CHV);
6821 wm->pipe[PIPE_C].plane[PLANE_PRIMARY] = _FW_WM_VLV(tmp, PLANEC);
6822 wm->pipe[PIPE_C].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORC);
6824 tmp = intel_uncore_read(&dev_priv->uncore, DSPHOWM);
6825 wm->sr.plane |= _FW_WM(tmp, SR_HI) << 9;
6826 wm->pipe[PIPE_C].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITEF_HI) << 8;
6827 wm->pipe[PIPE_C].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEE_HI) << 8;
6828 wm->pipe[PIPE_C].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEC_HI) << 8;
6829 wm->pipe[PIPE_B].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITED_HI) << 8;
6830 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEC_HI) << 8;
6831 wm->pipe[PIPE_B].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEB_HI) << 8;
6832 wm->pipe[PIPE_A].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITEB_HI) << 8;
6833 wm->pipe[PIPE_A].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEA_HI) << 8;
6834 wm->pipe[PIPE_A].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEA_HI) << 8;
6836 tmp = intel_uncore_read(&dev_priv->uncore, DSPFW7);
6837 wm->pipe[PIPE_B].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITED);
6838 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEC);
6840 tmp = intel_uncore_read(&dev_priv->uncore, DSPHOWM);
6841 wm->sr.plane |= _FW_WM(tmp, SR_HI) << 9;
6842 wm->pipe[PIPE_B].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITED_HI) << 8;
6843 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEC_HI) << 8;
6844 wm->pipe[PIPE_B].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEB_HI) << 8;
6845 wm->pipe[PIPE_A].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITEB_HI) << 8;
6846 wm->pipe[PIPE_A].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEA_HI) << 8;
6847 wm->pipe[PIPE_A].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEA_HI) << 8;
6854 void g4x_wm_get_hw_state(struct drm_i915_private *dev_priv)
6856 struct g4x_wm_values *wm = &dev_priv->display.wm.g4x;
6857 struct intel_crtc *crtc;
6859 g4x_read_wm_values(dev_priv, wm);
6861 wm->cxsr = intel_uncore_read(&dev_priv->uncore, FW_BLC_SELF) & FW_BLC_SELF_EN;
6863 for_each_intel_crtc(&dev_priv->drm, crtc) {
6864 struct intel_crtc_state *crtc_state =
6865 to_intel_crtc_state(crtc->base.state);
6866 struct g4x_wm_state *active = &crtc->wm.active.g4x;
6867 struct g4x_pipe_wm *raw;
6868 enum pipe pipe = crtc->pipe;
6869 enum plane_id plane_id;
6870 int level, max_level;
6872 active->cxsr = wm->cxsr;
6873 active->hpll_en = wm->hpll_en;
6874 active->fbc_en = wm->fbc_en;
6876 active->sr = wm->sr;
6877 active->hpll = wm->hpll;
6879 for_each_plane_id_on_crtc(crtc, plane_id) {
6880 active->wm.plane[plane_id] =
6881 wm->pipe[pipe].plane[plane_id];
6884 if (wm->cxsr && wm->hpll_en)
6885 max_level = G4X_WM_LEVEL_HPLL;
6887 max_level = G4X_WM_LEVEL_SR;
6889 max_level = G4X_WM_LEVEL_NORMAL;
6891 level = G4X_WM_LEVEL_NORMAL;
6892 raw = &crtc_state->wm.g4x.raw[level];
6893 for_each_plane_id_on_crtc(crtc, plane_id)
6894 raw->plane[plane_id] = active->wm.plane[plane_id];
6896 level = G4X_WM_LEVEL_SR;
6897 if (level > max_level)
6900 raw = &crtc_state->wm.g4x.raw[level];
6901 raw->plane[PLANE_PRIMARY] = active->sr.plane;
6902 raw->plane[PLANE_CURSOR] = active->sr.cursor;
6903 raw->plane[PLANE_SPRITE0] = 0;
6904 raw->fbc = active->sr.fbc;
6906 level = G4X_WM_LEVEL_HPLL;
6907 if (level > max_level)
6910 raw = &crtc_state->wm.g4x.raw[level];
6911 raw->plane[PLANE_PRIMARY] = active->hpll.plane;
6912 raw->plane[PLANE_CURSOR] = active->hpll.cursor;
6913 raw->plane[PLANE_SPRITE0] = 0;
6914 raw->fbc = active->hpll.fbc;
6918 for_each_plane_id_on_crtc(crtc, plane_id)
6919 g4x_raw_plane_wm_set(crtc_state, level,
6920 plane_id, USHRT_MAX);
6921 g4x_raw_fbc_wm_set(crtc_state, level, USHRT_MAX);
6923 crtc_state->wm.g4x.optimal = *active;
6924 crtc_state->wm.g4x.intermediate = *active;
6926 drm_dbg_kms(&dev_priv->drm,
6927 "Initial watermarks: pipe %c, plane=%d, cursor=%d, sprite=%d\n",
6929 wm->pipe[pipe].plane[PLANE_PRIMARY],
6930 wm->pipe[pipe].plane[PLANE_CURSOR],
6931 wm->pipe[pipe].plane[PLANE_SPRITE0]);
6934 drm_dbg_kms(&dev_priv->drm,
6935 "Initial SR watermarks: plane=%d, cursor=%d fbc=%d\n",
6936 wm->sr.plane, wm->sr.cursor, wm->sr.fbc);
6937 drm_dbg_kms(&dev_priv->drm,
6938 "Initial HPLL watermarks: plane=%d, SR cursor=%d fbc=%d\n",
6939 wm->hpll.plane, wm->hpll.cursor, wm->hpll.fbc);
6940 drm_dbg_kms(&dev_priv->drm, "Initial SR=%s HPLL=%s FBC=%s\n",
6941 str_yes_no(wm->cxsr), str_yes_no(wm->hpll_en),
6942 str_yes_no(wm->fbc_en));
6945 void g4x_wm_sanitize(struct drm_i915_private *dev_priv)
6947 struct intel_plane *plane;
6948 struct intel_crtc *crtc;
6950 mutex_lock(&dev_priv->display.wm.wm_mutex);
6952 for_each_intel_plane(&dev_priv->drm, plane) {
6953 struct intel_crtc *crtc =
6954 intel_crtc_for_pipe(dev_priv, plane->pipe);
6955 struct intel_crtc_state *crtc_state =
6956 to_intel_crtc_state(crtc->base.state);
6957 struct intel_plane_state *plane_state =
6958 to_intel_plane_state(plane->base.state);
6959 struct g4x_wm_state *wm_state = &crtc_state->wm.g4x.optimal;
6960 enum plane_id plane_id = plane->id;
6963 if (plane_state->uapi.visible)
6966 for (level = 0; level < 3; level++) {
6967 struct g4x_pipe_wm *raw =
6968 &crtc_state->wm.g4x.raw[level];
6970 raw->plane[plane_id] = 0;
6971 wm_state->wm.plane[plane_id] = 0;
6974 if (plane_id == PLANE_PRIMARY) {
6975 for (level = 0; level < 3; level++) {
6976 struct g4x_pipe_wm *raw =
6977 &crtc_state->wm.g4x.raw[level];
6981 wm_state->sr.fbc = 0;
6982 wm_state->hpll.fbc = 0;
6983 wm_state->fbc_en = false;
6987 for_each_intel_crtc(&dev_priv->drm, crtc) {
6988 struct intel_crtc_state *crtc_state =
6989 to_intel_crtc_state(crtc->base.state);
6991 crtc_state->wm.g4x.intermediate =
6992 crtc_state->wm.g4x.optimal;
6993 crtc->wm.active.g4x = crtc_state->wm.g4x.optimal;
6996 g4x_program_watermarks(dev_priv);
6998 mutex_unlock(&dev_priv->display.wm.wm_mutex);
7001 void vlv_wm_get_hw_state(struct drm_i915_private *dev_priv)
7003 struct vlv_wm_values *wm = &dev_priv->display.wm.vlv;
7004 struct intel_crtc *crtc;
7007 vlv_read_wm_values(dev_priv, wm);
7009 wm->cxsr = intel_uncore_read(&dev_priv->uncore, FW_BLC_SELF_VLV) & FW_CSPWRDWNEN;
7010 wm->level = VLV_WM_LEVEL_PM2;
7012 if (IS_CHERRYVIEW(dev_priv)) {
7013 vlv_punit_get(dev_priv);
7015 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPSSPM);
7016 if (val & DSP_MAXFIFO_PM5_ENABLE)
7017 wm->level = VLV_WM_LEVEL_PM5;
7020 * If DDR DVFS is disabled in the BIOS, Punit
7021 * will never ack the request. So if that happens
7022 * assume we don't have to enable/disable DDR DVFS
7023 * dynamically. To test that just set the REQ_ACK
7024 * bit to poke the Punit, but don't change the
7025 * HIGH/LOW bits so that we don't actually change
7026 * the current state.
7028 val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
7029 val |= FORCE_DDR_FREQ_REQ_ACK;
7030 vlv_punit_write(dev_priv, PUNIT_REG_DDR_SETUP2, val);
7032 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2) &
7033 FORCE_DDR_FREQ_REQ_ACK) == 0, 3)) {
7034 drm_dbg_kms(&dev_priv->drm,
7035 "Punit not acking DDR DVFS request, "
7036 "assuming DDR DVFS is disabled\n");
7037 dev_priv->display.wm.max_level = VLV_WM_LEVEL_PM5;
7039 val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
7040 if ((val & FORCE_DDR_HIGH_FREQ) == 0)
7041 wm->level = VLV_WM_LEVEL_DDR_DVFS;
7044 vlv_punit_put(dev_priv);
7047 for_each_intel_crtc(&dev_priv->drm, crtc) {
7048 struct intel_crtc_state *crtc_state =
7049 to_intel_crtc_state(crtc->base.state);
7050 struct vlv_wm_state *active = &crtc->wm.active.vlv;
7051 const struct vlv_fifo_state *fifo_state =
7052 &crtc_state->wm.vlv.fifo_state;
7053 enum pipe pipe = crtc->pipe;
7054 enum plane_id plane_id;
7057 vlv_get_fifo_size(crtc_state);
7059 active->num_levels = wm->level + 1;
7060 active->cxsr = wm->cxsr;
7062 for (level = 0; level < active->num_levels; level++) {
7063 struct g4x_pipe_wm *raw =
7064 &crtc_state->wm.vlv.raw[level];
7066 active->sr[level].plane = wm->sr.plane;
7067 active->sr[level].cursor = wm->sr.cursor;
7069 for_each_plane_id_on_crtc(crtc, plane_id) {
7070 active->wm[level].plane[plane_id] =
7071 wm->pipe[pipe].plane[plane_id];
7073 raw->plane[plane_id] =
7074 vlv_invert_wm_value(active->wm[level].plane[plane_id],
7075 fifo_state->plane[plane_id]);
7079 for_each_plane_id_on_crtc(crtc, plane_id)
7080 vlv_raw_plane_wm_set(crtc_state, level,
7081 plane_id, USHRT_MAX);
7082 vlv_invalidate_wms(crtc, active, level);
7084 crtc_state->wm.vlv.optimal = *active;
7085 crtc_state->wm.vlv.intermediate = *active;
7087 drm_dbg_kms(&dev_priv->drm,
7088 "Initial watermarks: pipe %c, plane=%d, cursor=%d, sprite0=%d, sprite1=%d\n",
7090 wm->pipe[pipe].plane[PLANE_PRIMARY],
7091 wm->pipe[pipe].plane[PLANE_CURSOR],
7092 wm->pipe[pipe].plane[PLANE_SPRITE0],
7093 wm->pipe[pipe].plane[PLANE_SPRITE1]);
7096 drm_dbg_kms(&dev_priv->drm,
7097 "Initial watermarks: SR plane=%d, SR cursor=%d level=%d cxsr=%d\n",
7098 wm->sr.plane, wm->sr.cursor, wm->level, wm->cxsr);
7101 void vlv_wm_sanitize(struct drm_i915_private *dev_priv)
7103 struct intel_plane *plane;
7104 struct intel_crtc *crtc;
7106 mutex_lock(&dev_priv->display.wm.wm_mutex);
7108 for_each_intel_plane(&dev_priv->drm, plane) {
7109 struct intel_crtc *crtc =
7110 intel_crtc_for_pipe(dev_priv, plane->pipe);
7111 struct intel_crtc_state *crtc_state =
7112 to_intel_crtc_state(crtc->base.state);
7113 struct intel_plane_state *plane_state =
7114 to_intel_plane_state(plane->base.state);
7115 struct vlv_wm_state *wm_state = &crtc_state->wm.vlv.optimal;
7116 const struct vlv_fifo_state *fifo_state =
7117 &crtc_state->wm.vlv.fifo_state;
7118 enum plane_id plane_id = plane->id;
7121 if (plane_state->uapi.visible)
7124 for (level = 0; level < wm_state->num_levels; level++) {
7125 struct g4x_pipe_wm *raw =
7126 &crtc_state->wm.vlv.raw[level];
7128 raw->plane[plane_id] = 0;
7130 wm_state->wm[level].plane[plane_id] =
7131 vlv_invert_wm_value(raw->plane[plane_id],
7132 fifo_state->plane[plane_id]);
7136 for_each_intel_crtc(&dev_priv->drm, crtc) {
7137 struct intel_crtc_state *crtc_state =
7138 to_intel_crtc_state(crtc->base.state);
7140 crtc_state->wm.vlv.intermediate =
7141 crtc_state->wm.vlv.optimal;
7142 crtc->wm.active.vlv = crtc_state->wm.vlv.optimal;
7145 vlv_program_watermarks(dev_priv);
7147 mutex_unlock(&dev_priv->display.wm.wm_mutex);
7151 * FIXME should probably kill this and improve
7152 * the real watermark readout/sanitation instead
7154 static void ilk_init_lp_watermarks(struct drm_i915_private *dev_priv)
7156 intel_uncore_write(&dev_priv->uncore, WM3_LP_ILK, intel_uncore_read(&dev_priv->uncore, WM3_LP_ILK) & ~WM_LP_ENABLE);
7157 intel_uncore_write(&dev_priv->uncore, WM2_LP_ILK, intel_uncore_read(&dev_priv->uncore, WM2_LP_ILK) & ~WM_LP_ENABLE);
7158 intel_uncore_write(&dev_priv->uncore, WM1_LP_ILK, intel_uncore_read(&dev_priv->uncore, WM1_LP_ILK) & ~WM_LP_ENABLE);
7161 * Don't touch WM_LP_SPRITE_ENABLE here.
7162 * Doing so could cause underruns.
7166 void ilk_wm_get_hw_state(struct drm_i915_private *dev_priv)
7168 struct ilk_wm_values *hw = &dev_priv->display.wm.hw;
7169 struct intel_crtc *crtc;
7171 ilk_init_lp_watermarks(dev_priv);
7173 for_each_intel_crtc(&dev_priv->drm, crtc)
7174 ilk_pipe_wm_get_hw_state(crtc);
7176 hw->wm_lp[0] = intel_uncore_read(&dev_priv->uncore, WM1_LP_ILK);
7177 hw->wm_lp[1] = intel_uncore_read(&dev_priv->uncore, WM2_LP_ILK);
7178 hw->wm_lp[2] = intel_uncore_read(&dev_priv->uncore, WM3_LP_ILK);
7180 hw->wm_lp_spr[0] = intel_uncore_read(&dev_priv->uncore, WM1S_LP_ILK);
7181 if (DISPLAY_VER(dev_priv) >= 7) {
7182 hw->wm_lp_spr[1] = intel_uncore_read(&dev_priv->uncore, WM2S_LP_IVB);
7183 hw->wm_lp_spr[2] = intel_uncore_read(&dev_priv->uncore, WM3S_LP_IVB);
7186 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
7187 hw->partitioning = (intel_uncore_read(&dev_priv->uncore, WM_MISC) & WM_MISC_DATA_PARTITION_5_6) ?
7188 INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
7189 else if (IS_IVYBRIDGE(dev_priv))
7190 hw->partitioning = (intel_uncore_read(&dev_priv->uncore, DISP_ARB_CTL2) & DISP_DATA_PARTITION_5_6) ?
7191 INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
7194 !(intel_uncore_read(&dev_priv->uncore, DISP_ARB_CTL) & DISP_FBC_WM_DIS);
7197 void intel_wm_state_verify(struct intel_crtc *crtc,
7198 struct intel_crtc_state *new_crtc_state)
7200 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7201 struct skl_hw_state {
7202 struct skl_ddb_entry ddb[I915_MAX_PLANES];
7203 struct skl_ddb_entry ddb_y[I915_MAX_PLANES];
7204 struct skl_pipe_wm wm;
7206 const struct skl_pipe_wm *sw_wm = &new_crtc_state->wm.skl.optimal;
7207 int level, max_level = ilk_wm_max_level(dev_priv);
7208 struct intel_plane *plane;
7209 u8 hw_enabled_slices;
7211 if (DISPLAY_VER(dev_priv) < 9 || !new_crtc_state->hw.active)
7214 hw = kzalloc(sizeof(*hw), GFP_KERNEL);
7218 skl_pipe_wm_get_hw_state(crtc, &hw->wm);
7220 skl_pipe_ddb_get_hw_state(crtc, hw->ddb, hw->ddb_y);
7222 hw_enabled_slices = intel_enabled_dbuf_slices_mask(dev_priv);
7224 if (DISPLAY_VER(dev_priv) >= 11 &&
7225 hw_enabled_slices != dev_priv->dbuf.enabled_slices)
7226 drm_err(&dev_priv->drm,
7227 "mismatch in DBUF Slices (expected 0x%x, got 0x%x)\n",
7228 dev_priv->dbuf.enabled_slices,
7231 for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane) {
7232 const struct skl_ddb_entry *hw_ddb_entry, *sw_ddb_entry;
7233 const struct skl_wm_level *hw_wm_level, *sw_wm_level;
7236 for (level = 0; level <= max_level; level++) {
7237 hw_wm_level = &hw->wm.planes[plane->id].wm[level];
7238 sw_wm_level = skl_plane_wm_level(sw_wm, plane->id, level);
7240 if (skl_wm_level_equals(hw_wm_level, sw_wm_level))
7243 drm_err(&dev_priv->drm,
7244 "[PLANE:%d:%s] mismatch in WM%d (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
7245 plane->base.base.id, plane->base.name, level,
7246 sw_wm_level->enable,
7247 sw_wm_level->blocks,
7249 hw_wm_level->enable,
7250 hw_wm_level->blocks,
7251 hw_wm_level->lines);
7254 hw_wm_level = &hw->wm.planes[plane->id].trans_wm;
7255 sw_wm_level = skl_plane_trans_wm(sw_wm, plane->id);
7257 if (!skl_wm_level_equals(hw_wm_level, sw_wm_level)) {
7258 drm_err(&dev_priv->drm,
7259 "[PLANE:%d:%s] mismatch in trans WM (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
7260 plane->base.base.id, plane->base.name,
7261 sw_wm_level->enable,
7262 sw_wm_level->blocks,
7264 hw_wm_level->enable,
7265 hw_wm_level->blocks,
7266 hw_wm_level->lines);
7269 hw_wm_level = &hw->wm.planes[plane->id].sagv.wm0;
7270 sw_wm_level = &sw_wm->planes[plane->id].sagv.wm0;
7272 if (HAS_HW_SAGV_WM(dev_priv) &&
7273 !skl_wm_level_equals(hw_wm_level, sw_wm_level)) {
7274 drm_err(&dev_priv->drm,
7275 "[PLANE:%d:%s] mismatch in SAGV WM (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
7276 plane->base.base.id, plane->base.name,
7277 sw_wm_level->enable,
7278 sw_wm_level->blocks,
7280 hw_wm_level->enable,
7281 hw_wm_level->blocks,
7282 hw_wm_level->lines);
7285 hw_wm_level = &hw->wm.planes[plane->id].sagv.trans_wm;
7286 sw_wm_level = &sw_wm->planes[plane->id].sagv.trans_wm;
7288 if (HAS_HW_SAGV_WM(dev_priv) &&
7289 !skl_wm_level_equals(hw_wm_level, sw_wm_level)) {
7290 drm_err(&dev_priv->drm,
7291 "[PLANE:%d:%s] mismatch in SAGV trans WM (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
7292 plane->base.base.id, plane->base.name,
7293 sw_wm_level->enable,
7294 sw_wm_level->blocks,
7296 hw_wm_level->enable,
7297 hw_wm_level->blocks,
7298 hw_wm_level->lines);
7302 hw_ddb_entry = &hw->ddb[PLANE_CURSOR];
7303 sw_ddb_entry = &new_crtc_state->wm.skl.plane_ddb[PLANE_CURSOR];
7305 if (!skl_ddb_entry_equal(hw_ddb_entry, sw_ddb_entry)) {
7306 drm_err(&dev_priv->drm,
7307 "[PLANE:%d:%s] mismatch in DDB (expected (%u,%u), found (%u,%u))\n",
7308 plane->base.base.id, plane->base.name,
7309 sw_ddb_entry->start, sw_ddb_entry->end,
7310 hw_ddb_entry->start, hw_ddb_entry->end);
7317 void intel_enable_ipc(struct drm_i915_private *dev_priv)
7321 if (!HAS_IPC(dev_priv))
7324 val = intel_uncore_read(&dev_priv->uncore, DISP_ARB_CTL2);
7326 if (dev_priv->ipc_enabled)
7327 val |= DISP_IPC_ENABLE;
7329 val &= ~DISP_IPC_ENABLE;
7331 intel_uncore_write(&dev_priv->uncore, DISP_ARB_CTL2, val);
7334 static bool intel_can_enable_ipc(struct drm_i915_private *dev_priv)
7336 /* Display WA #0477 WaDisableIPC: skl */
7337 if (IS_SKYLAKE(dev_priv))
7340 /* Display WA #1141: SKL:all KBL:all CFL */
7341 if (IS_KABYLAKE(dev_priv) ||
7342 IS_COFFEELAKE(dev_priv) ||
7343 IS_COMETLAKE(dev_priv))
7344 return dev_priv->dram_info.symmetric_memory;
7349 void intel_init_ipc(struct drm_i915_private *dev_priv)
7351 if (!HAS_IPC(dev_priv))
7354 dev_priv->ipc_enabled = intel_can_enable_ipc(dev_priv);
7356 intel_enable_ipc(dev_priv);
7359 static void ibx_init_clock_gating(struct drm_i915_private *dev_priv)
7362 * On Ibex Peak and Cougar Point, we need to disable clock
7363 * gating for the panel power sequencer or it will fail to
7364 * start up when no ports are active.
7366 intel_uncore_write(&dev_priv->uncore, SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE);
7369 static void g4x_disable_trickle_feed(struct drm_i915_private *dev_priv)
7373 for_each_pipe(dev_priv, pipe) {
7374 intel_uncore_write(&dev_priv->uncore, DSPCNTR(pipe),
7375 intel_uncore_read(&dev_priv->uncore, DSPCNTR(pipe)) |
7376 DISP_TRICKLE_FEED_DISABLE);
7378 intel_uncore_write(&dev_priv->uncore, DSPSURF(pipe), intel_uncore_read(&dev_priv->uncore, DSPSURF(pipe)));
7379 intel_uncore_posting_read(&dev_priv->uncore, DSPSURF(pipe));
7383 static void ilk_init_clock_gating(struct drm_i915_private *dev_priv)
7385 u32 dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
7389 * WaFbcDisableDpfcClockGating:ilk
7391 dspclk_gate |= ILK_DPFCRUNIT_CLOCK_GATE_DISABLE |
7392 ILK_DPFCUNIT_CLOCK_GATE_DISABLE |
7393 ILK_DPFDUNIT_CLOCK_GATE_ENABLE;
7395 intel_uncore_write(&dev_priv->uncore, PCH_3DCGDIS0,
7396 MARIUNIT_CLOCK_GATE_DISABLE |
7397 SVSMUNIT_CLOCK_GATE_DISABLE);
7398 intel_uncore_write(&dev_priv->uncore, PCH_3DCGDIS1,
7399 VFMUNIT_CLOCK_GATE_DISABLE);
7402 * According to the spec the following bits should be set in
7403 * order to enable memory self-refresh
7404 * The bit 22/21 of 0x42004
7405 * The bit 5 of 0x42020
7406 * The bit 15 of 0x45000
7408 intel_uncore_write(&dev_priv->uncore, ILK_DISPLAY_CHICKEN2,
7409 (intel_uncore_read(&dev_priv->uncore, ILK_DISPLAY_CHICKEN2) |
7410 ILK_DPARB_GATE | ILK_VSDPFD_FULL));
7411 dspclk_gate |= ILK_DPARBUNIT_CLOCK_GATE_ENABLE;
7412 intel_uncore_write(&dev_priv->uncore, DISP_ARB_CTL,
7413 (intel_uncore_read(&dev_priv->uncore, DISP_ARB_CTL) |
7417 * Based on the document from hardware guys the following bits
7418 * should be set unconditionally in order to enable FBC.
7419 * The bit 22 of 0x42000
7420 * The bit 22 of 0x42004
7421 * The bit 7,8,9 of 0x42020.
7423 if (IS_IRONLAKE_M(dev_priv)) {
7424 /* WaFbcAsynchFlipDisableFbcQueue:ilk */
7425 intel_uncore_write(&dev_priv->uncore, ILK_DISPLAY_CHICKEN1,
7426 intel_uncore_read(&dev_priv->uncore, ILK_DISPLAY_CHICKEN1) |
7428 intel_uncore_write(&dev_priv->uncore, ILK_DISPLAY_CHICKEN2,
7429 intel_uncore_read(&dev_priv->uncore, ILK_DISPLAY_CHICKEN2) |
7433 intel_uncore_write(&dev_priv->uncore, ILK_DSPCLK_GATE_D, dspclk_gate);
7435 intel_uncore_write(&dev_priv->uncore, ILK_DISPLAY_CHICKEN2,
7436 intel_uncore_read(&dev_priv->uncore, ILK_DISPLAY_CHICKEN2) |
7437 ILK_ELPIN_409_SELECT);
7439 g4x_disable_trickle_feed(dev_priv);
7441 ibx_init_clock_gating(dev_priv);
7444 static void cpt_init_clock_gating(struct drm_i915_private *dev_priv)
7450 * On Ibex Peak and Cougar Point, we need to disable clock
7451 * gating for the panel power sequencer or it will fail to
7452 * start up when no ports are active.
7454 intel_uncore_write(&dev_priv->uncore, SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE |
7455 PCH_DPLUNIT_CLOCK_GATE_DISABLE |
7456 PCH_CPUNIT_CLOCK_GATE_DISABLE);
7457 intel_uncore_write(&dev_priv->uncore, SOUTH_CHICKEN2, intel_uncore_read(&dev_priv->uncore, SOUTH_CHICKEN2) |
7458 DPLS_EDP_PPS_FIX_DIS);
7459 /* The below fixes the weird display corruption, a few pixels shifted
7460 * downward, on (only) LVDS of some HP laptops with IVY.
7462 for_each_pipe(dev_priv, pipe) {
7463 val = intel_uncore_read(&dev_priv->uncore, TRANS_CHICKEN2(pipe));
7464 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
7465 val &= ~TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
7466 if (dev_priv->vbt.fdi_rx_polarity_inverted)
7467 val |= TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
7468 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_COUNTER;
7469 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_MODESWITCH;
7470 intel_uncore_write(&dev_priv->uncore, TRANS_CHICKEN2(pipe), val);
7472 /* WADP0ClockGatingDisable */
7473 for_each_pipe(dev_priv, pipe) {
7474 intel_uncore_write(&dev_priv->uncore, TRANS_CHICKEN1(pipe),
7475 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
7479 static void gen6_check_mch_setup(struct drm_i915_private *dev_priv)
7483 tmp = intel_uncore_read(&dev_priv->uncore, MCH_SSKPD);
7484 if (REG_FIELD_GET(SSKPD_WM0_MASK_SNB, tmp) != 12)
7485 drm_dbg_kms(&dev_priv->drm,
7486 "Wrong MCH_SSKPD value: 0x%08x This can cause underruns.\n",
7490 static void gen6_init_clock_gating(struct drm_i915_private *dev_priv)
7492 u32 dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
7494 intel_uncore_write(&dev_priv->uncore, ILK_DSPCLK_GATE_D, dspclk_gate);
7496 intel_uncore_write(&dev_priv->uncore, ILK_DISPLAY_CHICKEN2,
7497 intel_uncore_read(&dev_priv->uncore, ILK_DISPLAY_CHICKEN2) |
7498 ILK_ELPIN_409_SELECT);
7500 intel_uncore_write(&dev_priv->uncore, GEN6_UCGCTL1,
7501 intel_uncore_read(&dev_priv->uncore, GEN6_UCGCTL1) |
7502 GEN6_BLBUNIT_CLOCK_GATE_DISABLE |
7503 GEN6_CSUNIT_CLOCK_GATE_DISABLE);
7505 /* According to the BSpec vol1g, bit 12 (RCPBUNIT) clock
7506 * gating disable must be set. Failure to set it results in
7507 * flickering pixels due to Z write ordering failures after
7508 * some amount of runtime in the Mesa "fire" demo, and Unigine
7509 * Sanctuary and Tropics, and apparently anything else with
7510 * alpha test or pixel discard.
7512 * According to the spec, bit 11 (RCCUNIT) must also be set,
7513 * but we didn't debug actual testcases to find it out.
7515 * WaDisableRCCUnitClockGating:snb
7516 * WaDisableRCPBUnitClockGating:snb
7518 intel_uncore_write(&dev_priv->uncore, GEN6_UCGCTL2,
7519 GEN6_RCPBUNIT_CLOCK_GATE_DISABLE |
7520 GEN6_RCCUNIT_CLOCK_GATE_DISABLE);
7523 * According to the spec the following bits should be
7524 * set in order to enable memory self-refresh and fbc:
7525 * The bit21 and bit22 of 0x42000
7526 * The bit21 and bit22 of 0x42004
7527 * The bit5 and bit7 of 0x42020
7528 * The bit14 of 0x70180
7529 * The bit14 of 0x71180
7531 * WaFbcAsynchFlipDisableFbcQueue:snb
7533 intel_uncore_write(&dev_priv->uncore, ILK_DISPLAY_CHICKEN1,
7534 intel_uncore_read(&dev_priv->uncore, ILK_DISPLAY_CHICKEN1) |
7535 ILK_FBCQ_DIS | ILK_PABSTRETCH_DIS);
7536 intel_uncore_write(&dev_priv->uncore, ILK_DISPLAY_CHICKEN2,
7537 intel_uncore_read(&dev_priv->uncore, ILK_DISPLAY_CHICKEN2) |
7538 ILK_DPARB_GATE | ILK_VSDPFD_FULL);
7539 intel_uncore_write(&dev_priv->uncore, ILK_DSPCLK_GATE_D,
7540 intel_uncore_read(&dev_priv->uncore, ILK_DSPCLK_GATE_D) |
7541 ILK_DPARBUNIT_CLOCK_GATE_ENABLE |
7542 ILK_DPFDUNIT_CLOCK_GATE_ENABLE);
7544 g4x_disable_trickle_feed(dev_priv);
7546 cpt_init_clock_gating(dev_priv);
7548 gen6_check_mch_setup(dev_priv);
7551 static void lpt_init_clock_gating(struct drm_i915_private *dev_priv)
7554 * TODO: this bit should only be enabled when really needed, then
7555 * disabled when not needed anymore in order to save power.
7557 if (HAS_PCH_LPT_LP(dev_priv))
7558 intel_uncore_write(&dev_priv->uncore, SOUTH_DSPCLK_GATE_D,
7559 intel_uncore_read(&dev_priv->uncore, SOUTH_DSPCLK_GATE_D) |
7560 PCH_LP_PARTITION_LEVEL_DISABLE);
7562 /* WADPOClockGatingDisable:hsw */
7563 intel_uncore_write(&dev_priv->uncore, TRANS_CHICKEN1(PIPE_A),
7564 intel_uncore_read(&dev_priv->uncore, TRANS_CHICKEN1(PIPE_A)) |
7565 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
7568 static void lpt_suspend_hw(struct drm_i915_private *dev_priv)
7570 if (HAS_PCH_LPT_LP(dev_priv)) {
7571 u32 val = intel_uncore_read(&dev_priv->uncore, SOUTH_DSPCLK_GATE_D);
7573 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
7574 intel_uncore_write(&dev_priv->uncore, SOUTH_DSPCLK_GATE_D, val);
7578 static void gen8_set_l3sqc_credits(struct drm_i915_private *dev_priv,
7579 int general_prio_credits,
7580 int high_prio_credits)
7585 /* WaTempDisableDOPClkGating:bdw */
7586 misccpctl = intel_uncore_read(&dev_priv->uncore, GEN7_MISCCPCTL);
7587 intel_uncore_write(&dev_priv->uncore, GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
7589 val = intel_uncore_read(&dev_priv->uncore, GEN8_L3SQCREG1);
7590 val &= ~L3_PRIO_CREDITS_MASK;
7591 val |= L3_GENERAL_PRIO_CREDITS(general_prio_credits);
7592 val |= L3_HIGH_PRIO_CREDITS(high_prio_credits);
7593 intel_uncore_write(&dev_priv->uncore, GEN8_L3SQCREG1, val);
7596 * Wait at least 100 clocks before re-enabling clock gating.
7597 * See the definition of L3SQCREG1 in BSpec.
7599 intel_uncore_posting_read(&dev_priv->uncore, GEN8_L3SQCREG1);
7601 intel_uncore_write(&dev_priv->uncore, GEN7_MISCCPCTL, misccpctl);
7604 static void icl_init_clock_gating(struct drm_i915_private *dev_priv)
7606 /* Wa_1409120013:icl,ehl */
7607 intel_uncore_write(&dev_priv->uncore, ILK_DPFC_CHICKEN(INTEL_FBC_A),
7608 DPFC_CHICKEN_COMP_DUMMY_PIXEL);
7610 /*Wa_14010594013:icl, ehl */
7611 intel_uncore_rmw(&dev_priv->uncore, GEN8_CHICKEN_DCPR_1,
7612 0, ICL_DELAY_PMRSP);
7615 static void gen12lp_init_clock_gating(struct drm_i915_private *dev_priv)
7617 /* Wa_1409120013:tgl,rkl,adl-s,dg1,dg2 */
7618 if (IS_TIGERLAKE(dev_priv) || IS_ROCKETLAKE(dev_priv) ||
7619 IS_ALDERLAKE_S(dev_priv) || IS_DG1(dev_priv) || IS_DG2(dev_priv))
7620 intel_uncore_write(&dev_priv->uncore, ILK_DPFC_CHICKEN(INTEL_FBC_A),
7621 DPFC_CHICKEN_COMP_DUMMY_PIXEL);
7623 /* Wa_1409825376:tgl (pre-prod)*/
7624 if (IS_TGL_DISPLAY_STEP(dev_priv, STEP_A0, STEP_C0))
7625 intel_uncore_write(&dev_priv->uncore, GEN9_CLKGATE_DIS_3, intel_uncore_read(&dev_priv->uncore, GEN9_CLKGATE_DIS_3) |
7626 TGL_VRH_GATING_DIS);
7628 /* Wa_14013723622:tgl,rkl,dg1,adl-s */
7629 if (DISPLAY_VER(dev_priv) == 12)
7630 intel_uncore_rmw(&dev_priv->uncore, CLKREQ_POLICY,
7631 CLKREQ_POLICY_MEM_UP_OVRD, 0);
7634 static void adlp_init_clock_gating(struct drm_i915_private *dev_priv)
7636 gen12lp_init_clock_gating(dev_priv);
7638 /* Wa_22011091694:adlp */
7639 intel_de_rmw(dev_priv, GEN9_CLKGATE_DIS_5, 0, DPCE_GATING_DIS);
7641 /* Bspec/49189 Initialize Sequence */
7642 intel_de_rmw(dev_priv, GEN8_CHICKEN_DCPR_1, DDI_CLOCK_REG_ACCESS, 0);
7645 static void dg1_init_clock_gating(struct drm_i915_private *dev_priv)
7647 gen12lp_init_clock_gating(dev_priv);
7649 /* Wa_1409836686:dg1[a0] */
7650 if (IS_DG1_GRAPHICS_STEP(dev_priv, STEP_A0, STEP_B0))
7651 intel_uncore_write(&dev_priv->uncore, GEN9_CLKGATE_DIS_3, intel_uncore_read(&dev_priv->uncore, GEN9_CLKGATE_DIS_3) |
7655 static void xehpsdv_init_clock_gating(struct drm_i915_private *dev_priv)
7657 /* Wa_22010146351:xehpsdv */
7658 if (IS_XEHPSDV_GRAPHICS_STEP(dev_priv, STEP_A0, STEP_B0))
7659 intel_uncore_rmw(&dev_priv->uncore, XEHP_CLOCK_GATE_DIS, 0, SGR_DIS);
7662 static void dg2_init_clock_gating(struct drm_i915_private *i915)
7664 /* Wa_22010954014:dg2 */
7665 intel_uncore_rmw(&i915->uncore, XEHP_CLOCK_GATE_DIS, 0,
7669 * Wa_14010733611:dg2_g10
7670 * Wa_22010146351:dg2_g10
7672 if (IS_DG2_GRAPHICS_STEP(i915, G10, STEP_A0, STEP_B0))
7673 intel_uncore_rmw(&i915->uncore, XEHP_CLOCK_GATE_DIS, 0,
7674 SGR_DIS | SGGI_DIS);
7677 static void pvc_init_clock_gating(struct drm_i915_private *dev_priv)
7679 /* Wa_14012385139:pvc */
7680 if (IS_PVC_BD_STEP(dev_priv, STEP_A0, STEP_B0))
7681 intel_uncore_rmw(&dev_priv->uncore, XEHP_CLOCK_GATE_DIS, 0, SGR_DIS);
7683 /* Wa_22010954014:pvc */
7684 if (IS_PVC_BD_STEP(dev_priv, STEP_A0, STEP_B0))
7685 intel_uncore_rmw(&dev_priv->uncore, XEHP_CLOCK_GATE_DIS, 0, SGSI_SIDECLK_DIS);
7688 static void cnp_init_clock_gating(struct drm_i915_private *dev_priv)
7690 if (!HAS_PCH_CNP(dev_priv))
7693 /* Display WA #1181 WaSouthDisplayDisablePWMCGEGating: cnp */
7694 intel_uncore_write(&dev_priv->uncore, SOUTH_DSPCLK_GATE_D, intel_uncore_read(&dev_priv->uncore, SOUTH_DSPCLK_GATE_D) |
7695 CNP_PWM_CGE_GATING_DISABLE);
7698 static void cfl_init_clock_gating(struct drm_i915_private *dev_priv)
7700 cnp_init_clock_gating(dev_priv);
7701 gen9_init_clock_gating(dev_priv);
7703 /* WAC6entrylatency:cfl */
7704 intel_uncore_write(&dev_priv->uncore, FBC_LLC_READ_CTRL, intel_uncore_read(&dev_priv->uncore, FBC_LLC_READ_CTRL) |
7705 FBC_LLC_FULLY_OPEN);
7708 * WaFbcTurnOffFbcWatermark:cfl
7709 * Display WA #0562: cfl
7711 intel_uncore_write(&dev_priv->uncore, DISP_ARB_CTL, intel_uncore_read(&dev_priv->uncore, DISP_ARB_CTL) |
7715 * WaFbcNukeOnHostModify:cfl
7716 * Display WA #0873: cfl
7718 intel_uncore_write(&dev_priv->uncore, ILK_DPFC_CHICKEN(INTEL_FBC_A),
7719 intel_uncore_read(&dev_priv->uncore, ILK_DPFC_CHICKEN(INTEL_FBC_A)) |
7720 DPFC_NUKE_ON_ANY_MODIFICATION);
7723 static void kbl_init_clock_gating(struct drm_i915_private *dev_priv)
7725 gen9_init_clock_gating(dev_priv);
7727 /* WAC6entrylatency:kbl */
7728 intel_uncore_write(&dev_priv->uncore, FBC_LLC_READ_CTRL, intel_uncore_read(&dev_priv->uncore, FBC_LLC_READ_CTRL) |
7729 FBC_LLC_FULLY_OPEN);
7731 /* WaDisableSDEUnitClockGating:kbl */
7732 if (IS_KBL_GRAPHICS_STEP(dev_priv, 0, STEP_C0))
7733 intel_uncore_write(&dev_priv->uncore, GEN8_UCGCTL6, intel_uncore_read(&dev_priv->uncore, GEN8_UCGCTL6) |
7734 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
7736 /* WaDisableGamClockGating:kbl */
7737 if (IS_KBL_GRAPHICS_STEP(dev_priv, 0, STEP_C0))
7738 intel_uncore_write(&dev_priv->uncore, GEN6_UCGCTL1, intel_uncore_read(&dev_priv->uncore, GEN6_UCGCTL1) |
7739 GEN6_GAMUNIT_CLOCK_GATE_DISABLE);
7742 * WaFbcTurnOffFbcWatermark:kbl
7743 * Display WA #0562: kbl
7745 intel_uncore_write(&dev_priv->uncore, DISP_ARB_CTL, intel_uncore_read(&dev_priv->uncore, DISP_ARB_CTL) |
7749 * WaFbcNukeOnHostModify:kbl
7750 * Display WA #0873: kbl
7752 intel_uncore_write(&dev_priv->uncore, ILK_DPFC_CHICKEN(INTEL_FBC_A),
7753 intel_uncore_read(&dev_priv->uncore, ILK_DPFC_CHICKEN(INTEL_FBC_A)) |
7754 DPFC_NUKE_ON_ANY_MODIFICATION);
7757 static void skl_init_clock_gating(struct drm_i915_private *dev_priv)
7759 gen9_init_clock_gating(dev_priv);
7761 /* WaDisableDopClockGating:skl */
7762 intel_uncore_write(&dev_priv->uncore, GEN7_MISCCPCTL, intel_uncore_read(&dev_priv->uncore, GEN7_MISCCPCTL) &
7763 ~GEN7_DOP_CLOCK_GATE_ENABLE);
7765 /* WAC6entrylatency:skl */
7766 intel_uncore_write(&dev_priv->uncore, FBC_LLC_READ_CTRL, intel_uncore_read(&dev_priv->uncore, FBC_LLC_READ_CTRL) |
7767 FBC_LLC_FULLY_OPEN);
7770 * WaFbcTurnOffFbcWatermark:skl
7771 * Display WA #0562: skl
7773 intel_uncore_write(&dev_priv->uncore, DISP_ARB_CTL, intel_uncore_read(&dev_priv->uncore, DISP_ARB_CTL) |
7777 * WaFbcNukeOnHostModify:skl
7778 * Display WA #0873: skl
7780 intel_uncore_write(&dev_priv->uncore, ILK_DPFC_CHICKEN(INTEL_FBC_A),
7781 intel_uncore_read(&dev_priv->uncore, ILK_DPFC_CHICKEN(INTEL_FBC_A)) |
7782 DPFC_NUKE_ON_ANY_MODIFICATION);
7785 * WaFbcHighMemBwCorruptionAvoidance:skl
7786 * Display WA #0883: skl
7788 intel_uncore_write(&dev_priv->uncore, ILK_DPFC_CHICKEN(INTEL_FBC_A),
7789 intel_uncore_read(&dev_priv->uncore, ILK_DPFC_CHICKEN(INTEL_FBC_A)) |
7790 DPFC_DISABLE_DUMMY0);
7793 static void bdw_init_clock_gating(struct drm_i915_private *dev_priv)
7797 /* WaFbcAsynchFlipDisableFbcQueue:hsw,bdw */
7798 intel_uncore_write(&dev_priv->uncore, CHICKEN_PIPESL_1(PIPE_A),
7799 intel_uncore_read(&dev_priv->uncore, CHICKEN_PIPESL_1(PIPE_A)) |
7802 /* WaSwitchSolVfFArbitrationPriority:bdw */
7803 intel_uncore_write(&dev_priv->uncore, GAM_ECOCHK, intel_uncore_read(&dev_priv->uncore, GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
7805 /* WaPsrDPAMaskVBlankInSRD:bdw */
7806 intel_uncore_write(&dev_priv->uncore, CHICKEN_PAR1_1,
7807 intel_uncore_read(&dev_priv->uncore, CHICKEN_PAR1_1) | DPA_MASK_VBLANK_SRD);
7809 for_each_pipe(dev_priv, pipe) {
7810 /* WaPsrDPRSUnmaskVBlankInSRD:bdw */
7811 intel_uncore_write(&dev_priv->uncore, CHICKEN_PIPESL_1(pipe),
7812 intel_uncore_read(&dev_priv->uncore, CHICKEN_PIPESL_1(pipe)) |
7813 BDW_DPRS_MASK_VBLANK_SRD);
7816 /* WaVSRefCountFullforceMissDisable:bdw */
7817 /* WaDSRefCountFullforceMissDisable:bdw */
7818 intel_uncore_write(&dev_priv->uncore, GEN7_FF_THREAD_MODE,
7819 intel_uncore_read(&dev_priv->uncore, GEN7_FF_THREAD_MODE) &
7820 ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
7822 intel_uncore_write(&dev_priv->uncore, RING_PSMI_CTL(RENDER_RING_BASE),
7823 _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
7825 /* WaDisableSDEUnitClockGating:bdw */
7826 intel_uncore_write(&dev_priv->uncore, GEN8_UCGCTL6, intel_uncore_read(&dev_priv->uncore, GEN8_UCGCTL6) |
7827 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
7829 /* WaProgramL3SqcReg1Default:bdw */
7830 gen8_set_l3sqc_credits(dev_priv, 30, 2);
7832 /* WaKVMNotificationOnConfigChange:bdw */
7833 intel_uncore_write(&dev_priv->uncore, CHICKEN_PAR2_1, intel_uncore_read(&dev_priv->uncore, CHICKEN_PAR2_1)
7834 | KVM_CONFIG_CHANGE_NOTIFICATION_SELECT);
7836 lpt_init_clock_gating(dev_priv);
7838 /* WaDisableDopClockGating:bdw
7840 * Also see the CHICKEN2 write in bdw_init_workarounds() to disable DOP
7843 intel_uncore_write(&dev_priv->uncore, GEN6_UCGCTL1,
7844 intel_uncore_read(&dev_priv->uncore, GEN6_UCGCTL1) | GEN6_EU_TCUNIT_CLOCK_GATE_DISABLE);
7847 static void hsw_init_clock_gating(struct drm_i915_private *dev_priv)
7849 /* WaFbcAsynchFlipDisableFbcQueue:hsw,bdw */
7850 intel_uncore_write(&dev_priv->uncore, CHICKEN_PIPESL_1(PIPE_A),
7851 intel_uncore_read(&dev_priv->uncore, CHICKEN_PIPESL_1(PIPE_A)) |
7854 /* This is required by WaCatErrorRejectionIssue:hsw */
7855 intel_uncore_write(&dev_priv->uncore, GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
7856 intel_uncore_read(&dev_priv->uncore, GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
7857 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
7859 /* WaSwitchSolVfFArbitrationPriority:hsw */
7860 intel_uncore_write(&dev_priv->uncore, GAM_ECOCHK, intel_uncore_read(&dev_priv->uncore, GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
7862 lpt_init_clock_gating(dev_priv);
7865 static void ivb_init_clock_gating(struct drm_i915_private *dev_priv)
7869 intel_uncore_write(&dev_priv->uncore, ILK_DSPCLK_GATE_D, ILK_VRHUNIT_CLOCK_GATE_DISABLE);
7871 /* WaFbcAsynchFlipDisableFbcQueue:ivb */
7872 intel_uncore_write(&dev_priv->uncore, ILK_DISPLAY_CHICKEN1,
7873 intel_uncore_read(&dev_priv->uncore, ILK_DISPLAY_CHICKEN1) |
7876 /* WaDisableBackToBackFlipFix:ivb */
7877 intel_uncore_write(&dev_priv->uncore, IVB_CHICKEN3,
7878 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
7879 CHICKEN3_DGMG_DONE_FIX_DISABLE);
7881 if (IS_IVB_GT1(dev_priv))
7882 intel_uncore_write(&dev_priv->uncore, GEN7_ROW_CHICKEN2,
7883 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
7885 /* must write both registers */
7886 intel_uncore_write(&dev_priv->uncore, GEN7_ROW_CHICKEN2,
7887 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
7888 intel_uncore_write(&dev_priv->uncore, GEN7_ROW_CHICKEN2_GT2,
7889 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
7893 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
7894 * This implements the WaDisableRCZUnitClockGating:ivb workaround.
7896 intel_uncore_write(&dev_priv->uncore, GEN6_UCGCTL2,
7897 GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
7899 /* This is required by WaCatErrorRejectionIssue:ivb */
7900 intel_uncore_write(&dev_priv->uncore, GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
7901 intel_uncore_read(&dev_priv->uncore, GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
7902 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
7904 g4x_disable_trickle_feed(dev_priv);
7906 snpcr = intel_uncore_read(&dev_priv->uncore, GEN6_MBCUNIT_SNPCR);
7907 snpcr &= ~GEN6_MBC_SNPCR_MASK;
7908 snpcr |= GEN6_MBC_SNPCR_MED;
7909 intel_uncore_write(&dev_priv->uncore, GEN6_MBCUNIT_SNPCR, snpcr);
7911 if (!HAS_PCH_NOP(dev_priv))
7912 cpt_init_clock_gating(dev_priv);
7914 gen6_check_mch_setup(dev_priv);
7917 static void vlv_init_clock_gating(struct drm_i915_private *dev_priv)
7919 /* WaDisableBackToBackFlipFix:vlv */
7920 intel_uncore_write(&dev_priv->uncore, IVB_CHICKEN3,
7921 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
7922 CHICKEN3_DGMG_DONE_FIX_DISABLE);
7924 /* WaDisableDopClockGating:vlv */
7925 intel_uncore_write(&dev_priv->uncore, GEN7_ROW_CHICKEN2,
7926 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
7928 /* This is required by WaCatErrorRejectionIssue:vlv */
7929 intel_uncore_write(&dev_priv->uncore, GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
7930 intel_uncore_read(&dev_priv->uncore, GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
7931 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
7934 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
7935 * This implements the WaDisableRCZUnitClockGating:vlv workaround.
7937 intel_uncore_write(&dev_priv->uncore, GEN6_UCGCTL2,
7938 GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
7940 /* WaDisableL3Bank2xClockGate:vlv
7941 * Disabling L3 clock gating- MMIO 940c[25] = 1
7942 * Set bit 25, to disable L3_BANK_2x_CLK_GATING */
7943 intel_uncore_write(&dev_priv->uncore, GEN7_UCGCTL4,
7944 intel_uncore_read(&dev_priv->uncore, GEN7_UCGCTL4) | GEN7_L3BANK2X_CLOCK_GATE_DISABLE);
7947 * WaDisableVLVClockGating_VBIIssue:vlv
7948 * Disable clock gating on th GCFG unit to prevent a delay
7949 * in the reporting of vblank events.
7951 intel_uncore_write(&dev_priv->uncore, VLV_GUNIT_CLOCK_GATE, GCFG_DIS);
7954 static void chv_init_clock_gating(struct drm_i915_private *dev_priv)
7956 /* WaVSRefCountFullforceMissDisable:chv */
7957 /* WaDSRefCountFullforceMissDisable:chv */
7958 intel_uncore_write(&dev_priv->uncore, GEN7_FF_THREAD_MODE,
7959 intel_uncore_read(&dev_priv->uncore, GEN7_FF_THREAD_MODE) &
7960 ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
7962 /* WaDisableSemaphoreAndSyncFlipWait:chv */
7963 intel_uncore_write(&dev_priv->uncore, RING_PSMI_CTL(RENDER_RING_BASE),
7964 _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
7966 /* WaDisableCSUnitClockGating:chv */
7967 intel_uncore_write(&dev_priv->uncore, GEN6_UCGCTL1, intel_uncore_read(&dev_priv->uncore, GEN6_UCGCTL1) |
7968 GEN6_CSUNIT_CLOCK_GATE_DISABLE);
7970 /* WaDisableSDEUnitClockGating:chv */
7971 intel_uncore_write(&dev_priv->uncore, GEN8_UCGCTL6, intel_uncore_read(&dev_priv->uncore, GEN8_UCGCTL6) |
7972 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
7975 * WaProgramL3SqcReg1Default:chv
7976 * See gfxspecs/Related Documents/Performance Guide/
7977 * LSQC Setting Recommendations.
7979 gen8_set_l3sqc_credits(dev_priv, 38, 2);
7982 static void g4x_init_clock_gating(struct drm_i915_private *dev_priv)
7986 intel_uncore_write(&dev_priv->uncore, RENCLK_GATE_D1, 0);
7987 intel_uncore_write(&dev_priv->uncore, RENCLK_GATE_D2, VF_UNIT_CLOCK_GATE_DISABLE |
7988 GS_UNIT_CLOCK_GATE_DISABLE |
7989 CL_UNIT_CLOCK_GATE_DISABLE);
7990 intel_uncore_write(&dev_priv->uncore, RAMCLK_GATE_D, 0);
7991 dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE |
7992 OVRUNIT_CLOCK_GATE_DISABLE |
7993 OVCUNIT_CLOCK_GATE_DISABLE;
7994 if (IS_GM45(dev_priv))
7995 dspclk_gate |= DSSUNIT_CLOCK_GATE_DISABLE;
7996 intel_uncore_write(&dev_priv->uncore, DSPCLK_GATE_D, dspclk_gate);
7998 g4x_disable_trickle_feed(dev_priv);
8001 static void i965gm_init_clock_gating(struct drm_i915_private *dev_priv)
8003 struct intel_uncore *uncore = &dev_priv->uncore;
8005 intel_uncore_write(uncore, RENCLK_GATE_D1, I965_RCC_CLOCK_GATE_DISABLE);
8006 intel_uncore_write(uncore, RENCLK_GATE_D2, 0);
8007 intel_uncore_write(uncore, DSPCLK_GATE_D, 0);
8008 intel_uncore_write(uncore, RAMCLK_GATE_D, 0);
8009 intel_uncore_write16(uncore, DEUC, 0);
8010 intel_uncore_write(uncore,
8012 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
8015 static void i965g_init_clock_gating(struct drm_i915_private *dev_priv)
8017 intel_uncore_write(&dev_priv->uncore, RENCLK_GATE_D1, I965_RCZ_CLOCK_GATE_DISABLE |
8018 I965_RCC_CLOCK_GATE_DISABLE |
8019 I965_RCPB_CLOCK_GATE_DISABLE |
8020 I965_ISC_CLOCK_GATE_DISABLE |
8021 I965_FBC_CLOCK_GATE_DISABLE);
8022 intel_uncore_write(&dev_priv->uncore, RENCLK_GATE_D2, 0);
8023 intel_uncore_write(&dev_priv->uncore, MI_ARB_STATE,
8024 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
8027 static void gen3_init_clock_gating(struct drm_i915_private *dev_priv)
8029 u32 dstate = intel_uncore_read(&dev_priv->uncore, D_STATE);
8031 dstate |= DSTATE_PLL_D3_OFF | DSTATE_GFX_CLOCK_GATING |
8032 DSTATE_DOT_CLOCK_GATING;
8033 intel_uncore_write(&dev_priv->uncore, D_STATE, dstate);
8035 if (IS_PINEVIEW(dev_priv))
8036 intel_uncore_write(&dev_priv->uncore, ECOSKPD(RENDER_RING_BASE),
8037 _MASKED_BIT_ENABLE(ECO_GATING_CX_ONLY));
8039 /* IIR "flip pending" means done if this bit is set */
8040 intel_uncore_write(&dev_priv->uncore, ECOSKPD(RENDER_RING_BASE),
8041 _MASKED_BIT_DISABLE(ECO_FLIP_DONE));
8043 /* interrupts should cause a wake up from C3 */
8044 intel_uncore_write(&dev_priv->uncore, INSTPM, _MASKED_BIT_ENABLE(INSTPM_AGPBUSY_INT_EN));
8046 /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
8047 intel_uncore_write(&dev_priv->uncore, MI_ARB_STATE, _MASKED_BIT_ENABLE(MI_ARB_C3_LP_WRITE_ENABLE));
8049 intel_uncore_write(&dev_priv->uncore, MI_ARB_STATE,
8050 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
8053 static void i85x_init_clock_gating(struct drm_i915_private *dev_priv)
8055 intel_uncore_write(&dev_priv->uncore, RENCLK_GATE_D1, SV_CLOCK_GATE_DISABLE);
8057 /* interrupts should cause a wake up from C3 */
8058 intel_uncore_write(&dev_priv->uncore, MI_STATE, _MASKED_BIT_ENABLE(MI_AGPBUSY_INT_EN) |
8059 _MASKED_BIT_DISABLE(MI_AGPBUSY_830_MODE));
8061 intel_uncore_write(&dev_priv->uncore, MEM_MODE,
8062 _MASKED_BIT_ENABLE(MEM_DISPLAY_TRICKLE_FEED_DISABLE));
8065 * Have FBC ignore 3D activity since we use software
8066 * render tracking, and otherwise a pure 3D workload
8067 * (even if it just renders a single frame and then does
8068 * abosultely nothing) would not allow FBC to recompress
8069 * until a 2D blit occurs.
8071 intel_uncore_write(&dev_priv->uncore, SCPD0,
8072 _MASKED_BIT_ENABLE(SCPD_FBC_IGNORE_3D));
8075 static void i830_init_clock_gating(struct drm_i915_private *dev_priv)
8077 intel_uncore_write(&dev_priv->uncore, MEM_MODE,
8078 _MASKED_BIT_ENABLE(MEM_DISPLAY_A_TRICKLE_FEED_DISABLE) |
8079 _MASKED_BIT_ENABLE(MEM_DISPLAY_B_TRICKLE_FEED_DISABLE));
8082 void intel_init_clock_gating(struct drm_i915_private *dev_priv)
8084 dev_priv->clock_gating_funcs->init_clock_gating(dev_priv);
8087 void intel_suspend_hw(struct drm_i915_private *dev_priv)
8089 if (HAS_PCH_LPT(dev_priv))
8090 lpt_suspend_hw(dev_priv);
8093 static void nop_init_clock_gating(struct drm_i915_private *dev_priv)
8095 drm_dbg_kms(&dev_priv->drm,
8096 "No clock gating settings or workarounds applied.\n");
8099 #define CG_FUNCS(platform) \
8100 static const struct drm_i915_clock_gating_funcs platform##_clock_gating_funcs = { \
8101 .init_clock_gating = platform##_init_clock_gating, \
8133 * intel_init_clock_gating_hooks - setup the clock gating hooks
8134 * @dev_priv: device private
8136 * Setup the hooks that configure which clocks of a given platform can be
8137 * gated and also apply various GT and display specific workarounds for these
8138 * platforms. Note that some GT specific workarounds are applied separately
8139 * when GPU contexts or batchbuffers start their execution.
8141 void intel_init_clock_gating_hooks(struct drm_i915_private *dev_priv)
8143 if (IS_PONTEVECCHIO(dev_priv))
8144 dev_priv->clock_gating_funcs = &pvc_clock_gating_funcs;
8145 else if (IS_DG2(dev_priv))
8146 dev_priv->clock_gating_funcs = &dg2_clock_gating_funcs;
8147 else if (IS_XEHPSDV(dev_priv))
8148 dev_priv->clock_gating_funcs = &xehpsdv_clock_gating_funcs;
8149 else if (IS_ALDERLAKE_P(dev_priv))
8150 dev_priv->clock_gating_funcs = &adlp_clock_gating_funcs;
8151 else if (IS_DG1(dev_priv))
8152 dev_priv->clock_gating_funcs = &dg1_clock_gating_funcs;
8153 else if (GRAPHICS_VER(dev_priv) == 12)
8154 dev_priv->clock_gating_funcs = &gen12lp_clock_gating_funcs;
8155 else if (GRAPHICS_VER(dev_priv) == 11)
8156 dev_priv->clock_gating_funcs = &icl_clock_gating_funcs;
8157 else if (IS_COFFEELAKE(dev_priv) || IS_COMETLAKE(dev_priv))
8158 dev_priv->clock_gating_funcs = &cfl_clock_gating_funcs;
8159 else if (IS_SKYLAKE(dev_priv))
8160 dev_priv->clock_gating_funcs = &skl_clock_gating_funcs;
8161 else if (IS_KABYLAKE(dev_priv))
8162 dev_priv->clock_gating_funcs = &kbl_clock_gating_funcs;
8163 else if (IS_BROXTON(dev_priv))
8164 dev_priv->clock_gating_funcs = &bxt_clock_gating_funcs;
8165 else if (IS_GEMINILAKE(dev_priv))
8166 dev_priv->clock_gating_funcs = &glk_clock_gating_funcs;
8167 else if (IS_BROADWELL(dev_priv))
8168 dev_priv->clock_gating_funcs = &bdw_clock_gating_funcs;
8169 else if (IS_CHERRYVIEW(dev_priv))
8170 dev_priv->clock_gating_funcs = &chv_clock_gating_funcs;
8171 else if (IS_HASWELL(dev_priv))
8172 dev_priv->clock_gating_funcs = &hsw_clock_gating_funcs;
8173 else if (IS_IVYBRIDGE(dev_priv))
8174 dev_priv->clock_gating_funcs = &ivb_clock_gating_funcs;
8175 else if (IS_VALLEYVIEW(dev_priv))
8176 dev_priv->clock_gating_funcs = &vlv_clock_gating_funcs;
8177 else if (GRAPHICS_VER(dev_priv) == 6)
8178 dev_priv->clock_gating_funcs = &gen6_clock_gating_funcs;
8179 else if (GRAPHICS_VER(dev_priv) == 5)
8180 dev_priv->clock_gating_funcs = &ilk_clock_gating_funcs;
8181 else if (IS_G4X(dev_priv))
8182 dev_priv->clock_gating_funcs = &g4x_clock_gating_funcs;
8183 else if (IS_I965GM(dev_priv))
8184 dev_priv->clock_gating_funcs = &i965gm_clock_gating_funcs;
8185 else if (IS_I965G(dev_priv))
8186 dev_priv->clock_gating_funcs = &i965g_clock_gating_funcs;
8187 else if (GRAPHICS_VER(dev_priv) == 3)
8188 dev_priv->clock_gating_funcs = &gen3_clock_gating_funcs;
8189 else if (IS_I85X(dev_priv) || IS_I865G(dev_priv))
8190 dev_priv->clock_gating_funcs = &i85x_clock_gating_funcs;
8191 else if (GRAPHICS_VER(dev_priv) == 2)
8192 dev_priv->clock_gating_funcs = &i830_clock_gating_funcs;
8194 MISSING_CASE(INTEL_DEVID(dev_priv));
8195 dev_priv->clock_gating_funcs = &nop_clock_gating_funcs;
8199 static const struct intel_wm_funcs skl_wm_funcs = {
8200 .compute_global_watermarks = skl_compute_wm,
8203 static const struct intel_wm_funcs ilk_wm_funcs = {
8204 .compute_pipe_wm = ilk_compute_pipe_wm,
8205 .compute_intermediate_wm = ilk_compute_intermediate_wm,
8206 .initial_watermarks = ilk_initial_watermarks,
8207 .optimize_watermarks = ilk_optimize_watermarks,
8210 static const struct intel_wm_funcs vlv_wm_funcs = {
8211 .compute_pipe_wm = vlv_compute_pipe_wm,
8212 .compute_intermediate_wm = vlv_compute_intermediate_wm,
8213 .initial_watermarks = vlv_initial_watermarks,
8214 .optimize_watermarks = vlv_optimize_watermarks,
8215 .atomic_update_watermarks = vlv_atomic_update_fifo,
8218 static const struct intel_wm_funcs g4x_wm_funcs = {
8219 .compute_pipe_wm = g4x_compute_pipe_wm,
8220 .compute_intermediate_wm = g4x_compute_intermediate_wm,
8221 .initial_watermarks = g4x_initial_watermarks,
8222 .optimize_watermarks = g4x_optimize_watermarks,
8225 static const struct intel_wm_funcs pnv_wm_funcs = {
8226 .update_wm = pnv_update_wm,
8229 static const struct intel_wm_funcs i965_wm_funcs = {
8230 .update_wm = i965_update_wm,
8233 static const struct intel_wm_funcs i9xx_wm_funcs = {
8234 .update_wm = i9xx_update_wm,
8237 static const struct intel_wm_funcs i845_wm_funcs = {
8238 .update_wm = i845_update_wm,
8241 static const struct intel_wm_funcs nop_funcs = {
8244 /* Set up chip specific power management-related functions */
8245 void intel_init_pm(struct drm_i915_private *dev_priv)
8248 if (IS_PINEVIEW(dev_priv))
8249 pnv_get_mem_freq(dev_priv);
8250 else if (GRAPHICS_VER(dev_priv) == 5)
8251 ilk_get_mem_freq(dev_priv);
8253 intel_sagv_init(dev_priv);
8255 /* For FIFO watermark updates */
8256 if (DISPLAY_VER(dev_priv) >= 9) {
8257 skl_setup_wm_latency(dev_priv);
8258 dev_priv->display.funcs.wm = &skl_wm_funcs;
8259 } else if (HAS_PCH_SPLIT(dev_priv)) {
8260 ilk_setup_wm_latency(dev_priv);
8262 if ((DISPLAY_VER(dev_priv) == 5 && dev_priv->display.wm.pri_latency[1] &&
8263 dev_priv->display.wm.spr_latency[1] && dev_priv->display.wm.cur_latency[1]) ||
8264 (DISPLAY_VER(dev_priv) != 5 && dev_priv->display.wm.pri_latency[0] &&
8265 dev_priv->display.wm.spr_latency[0] && dev_priv->display.wm.cur_latency[0])) {
8266 dev_priv->display.funcs.wm = &ilk_wm_funcs;
8268 drm_dbg_kms(&dev_priv->drm,
8269 "Failed to read display plane latency. "
8271 dev_priv->display.funcs.wm = &nop_funcs;
8273 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
8274 vlv_setup_wm_latency(dev_priv);
8275 dev_priv->display.funcs.wm = &vlv_wm_funcs;
8276 } else if (IS_G4X(dev_priv)) {
8277 g4x_setup_wm_latency(dev_priv);
8278 dev_priv->display.funcs.wm = &g4x_wm_funcs;
8279 } else if (IS_PINEVIEW(dev_priv)) {
8280 if (!intel_get_cxsr_latency(!IS_MOBILE(dev_priv),
8283 dev_priv->mem_freq)) {
8284 drm_info(&dev_priv->drm,
8285 "failed to find known CxSR latency "
8286 "(found ddr%s fsb freq %d, mem freq %d), "
8288 (dev_priv->is_ddr3 == 1) ? "3" : "2",
8289 dev_priv->fsb_freq, dev_priv->mem_freq);
8290 /* Disable CxSR and never update its watermark again */
8291 intel_set_memory_cxsr(dev_priv, false);
8292 dev_priv->display.funcs.wm = &nop_funcs;
8294 dev_priv->display.funcs.wm = &pnv_wm_funcs;
8295 } else if (DISPLAY_VER(dev_priv) == 4) {
8296 dev_priv->display.funcs.wm = &i965_wm_funcs;
8297 } else if (DISPLAY_VER(dev_priv) == 3) {
8298 dev_priv->display.funcs.wm = &i9xx_wm_funcs;
8299 } else if (DISPLAY_VER(dev_priv) == 2) {
8300 if (INTEL_NUM_PIPES(dev_priv) == 1)
8301 dev_priv->display.funcs.wm = &i845_wm_funcs;
8303 dev_priv->display.funcs.wm = &i9xx_wm_funcs;
8305 drm_err(&dev_priv->drm,
8306 "unexpected fall-through in %s\n", __func__);
8307 dev_priv->display.funcs.wm = &nop_funcs;
8311 void intel_pm_setup(struct drm_i915_private *dev_priv)
8313 dev_priv->runtime_pm.suspended = false;
8314 atomic_set(&dev_priv->runtime_pm.wakeref_count, 0);
8317 static struct intel_global_state *intel_dbuf_duplicate_state(struct intel_global_obj *obj)
8319 struct intel_dbuf_state *dbuf_state;
8321 dbuf_state = kmemdup(obj->state, sizeof(*dbuf_state), GFP_KERNEL);
8325 return &dbuf_state->base;
8328 static void intel_dbuf_destroy_state(struct intel_global_obj *obj,
8329 struct intel_global_state *state)
8334 static const struct intel_global_state_funcs intel_dbuf_funcs = {
8335 .atomic_duplicate_state = intel_dbuf_duplicate_state,
8336 .atomic_destroy_state = intel_dbuf_destroy_state,
8339 struct intel_dbuf_state *
8340 intel_atomic_get_dbuf_state(struct intel_atomic_state *state)
8342 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
8343 struct intel_global_state *dbuf_state;
8345 dbuf_state = intel_atomic_get_global_obj_state(state, &dev_priv->dbuf.obj);
8346 if (IS_ERR(dbuf_state))
8347 return ERR_CAST(dbuf_state);
8349 return to_intel_dbuf_state(dbuf_state);
8352 int intel_dbuf_init(struct drm_i915_private *dev_priv)
8354 struct intel_dbuf_state *dbuf_state;
8356 dbuf_state = kzalloc(sizeof(*dbuf_state), GFP_KERNEL);
8360 intel_atomic_global_obj_init(dev_priv, &dev_priv->dbuf.obj,
8361 &dbuf_state->base, &intel_dbuf_funcs);
8367 * Configure MBUS_CTL and all DBUF_CTL_S of each slice to join_mbus state before
8368 * update the request state of all DBUS slices.
8370 static void update_mbus_pre_enable(struct intel_atomic_state *state)
8372 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
8373 u32 mbus_ctl, dbuf_min_tracker_val;
8374 enum dbuf_slice slice;
8375 const struct intel_dbuf_state *dbuf_state =
8376 intel_atomic_get_new_dbuf_state(state);
8378 if (!HAS_MBUS_JOINING(dev_priv))
8382 * TODO: Implement vblank synchronized MBUS joining changes.
8383 * Must be properly coordinated with dbuf reprogramming.
8385 if (dbuf_state->joined_mbus) {
8386 mbus_ctl = MBUS_HASHING_MODE_1x4 | MBUS_JOIN |
8387 MBUS_JOIN_PIPE_SELECT_NONE;
8388 dbuf_min_tracker_val = DBUF_MIN_TRACKER_STATE_SERVICE(3);
8390 mbus_ctl = MBUS_HASHING_MODE_2x2 |
8391 MBUS_JOIN_PIPE_SELECT_NONE;
8392 dbuf_min_tracker_val = DBUF_MIN_TRACKER_STATE_SERVICE(1);
8395 intel_de_rmw(dev_priv, MBUS_CTL,
8396 MBUS_HASHING_MODE_MASK | MBUS_JOIN |
8397 MBUS_JOIN_PIPE_SELECT_MASK, mbus_ctl);
8399 for_each_dbuf_slice(dev_priv, slice)
8400 intel_de_rmw(dev_priv, DBUF_CTL_S(slice),
8401 DBUF_MIN_TRACKER_STATE_SERVICE_MASK,
8402 dbuf_min_tracker_val);
8405 void intel_dbuf_pre_plane_update(struct intel_atomic_state *state)
8407 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
8408 const struct intel_dbuf_state *new_dbuf_state =
8409 intel_atomic_get_new_dbuf_state(state);
8410 const struct intel_dbuf_state *old_dbuf_state =
8411 intel_atomic_get_old_dbuf_state(state);
8413 if (!new_dbuf_state ||
8414 ((new_dbuf_state->enabled_slices == old_dbuf_state->enabled_slices)
8415 && (new_dbuf_state->joined_mbus == old_dbuf_state->joined_mbus)))
8418 WARN_ON(!new_dbuf_state->base.changed);
8420 update_mbus_pre_enable(state);
8421 gen9_dbuf_slices_update(dev_priv,
8422 old_dbuf_state->enabled_slices |
8423 new_dbuf_state->enabled_slices);
8426 void intel_dbuf_post_plane_update(struct intel_atomic_state *state)
8428 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
8429 const struct intel_dbuf_state *new_dbuf_state =
8430 intel_atomic_get_new_dbuf_state(state);
8431 const struct intel_dbuf_state *old_dbuf_state =
8432 intel_atomic_get_old_dbuf_state(state);
8434 if (!new_dbuf_state ||
8435 ((new_dbuf_state->enabled_slices == old_dbuf_state->enabled_slices)
8436 && (new_dbuf_state->joined_mbus == old_dbuf_state->joined_mbus)))
8439 WARN_ON(!new_dbuf_state->base.changed);
8441 gen9_dbuf_slices_update(dev_priv,
8442 new_dbuf_state->enabled_slices);
8445 void intel_mbus_dbox_update(struct intel_atomic_state *state)
8447 struct drm_i915_private *i915 = to_i915(state->base.dev);
8448 const struct intel_dbuf_state *new_dbuf_state, *old_dbuf_state;
8449 const struct intel_crtc_state *new_crtc_state;
8450 const struct intel_crtc *crtc;
8454 if (DISPLAY_VER(i915) < 11)
8457 new_dbuf_state = intel_atomic_get_new_dbuf_state(state);
8458 old_dbuf_state = intel_atomic_get_old_dbuf_state(state);
8459 if (!new_dbuf_state ||
8460 (new_dbuf_state->joined_mbus == old_dbuf_state->joined_mbus &&
8461 new_dbuf_state->active_pipes == old_dbuf_state->active_pipes))
8464 if (DISPLAY_VER(i915) >= 12) {
8465 val |= MBUS_DBOX_B2B_TRANSACTIONS_MAX(16);
8466 val |= MBUS_DBOX_B2B_TRANSACTIONS_DELAY(1);
8467 val |= MBUS_DBOX_REGULATE_B2B_TRANSACTIONS_EN;
8470 /* Wa_22010947358:adl-p */
8471 if (IS_ALDERLAKE_P(i915))
8472 val |= new_dbuf_state->joined_mbus ? MBUS_DBOX_A_CREDIT(6) :
8473 MBUS_DBOX_A_CREDIT(4);
8475 val |= MBUS_DBOX_A_CREDIT(2);
8477 if (IS_ALDERLAKE_P(i915)) {
8478 val |= MBUS_DBOX_BW_CREDIT(2);
8479 val |= MBUS_DBOX_B_CREDIT(8);
8480 } else if (DISPLAY_VER(i915) >= 12) {
8481 val |= MBUS_DBOX_BW_CREDIT(2);
8482 val |= MBUS_DBOX_B_CREDIT(12);
8484 val |= MBUS_DBOX_BW_CREDIT(1);
8485 val |= MBUS_DBOX_B_CREDIT(8);
8488 for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
8489 if (!new_crtc_state->hw.active ||
8490 !intel_crtc_needs_modeset(new_crtc_state))
8493 intel_de_write(i915, PIPE_MBUS_DBOX_CTL(crtc->pipe), val);