Quellcodebibliothek Statistik Leitseite products/Sources/formale Sprachen/C/Linux/drivers/gpu/drm/i915/display/   (Open Source Betriebssystem Version 6.17.9©)  Datei vom 24.10.2025 mit Größe 122 kB image not shown  

Quelle  i9xx_wm.c   Sprache: C

 
// SPDX-License-Identifier: MIT
/*
 * Copyright © 2023 Intel Corporation
 */


#include "i915_drv.h"
#include "i915_reg.h"
#include "i9xx_wm.h"
#include "i9xx_wm_regs.h"
#include "intel_atomic.h"
#include "intel_bo.h"
#include "intel_de.h"
#include "intel_display.h"
#include "intel_display_regs.h"
#include "intel_display_trace.h"
#include "intel_fb.h"
#include "intel_mchbar_regs.h"
#include "intel_wm.h"
#include "skl_watermark.h"
#include "vlv_sideband.h"

struct intel_watermark_params {
 u16 fifo_size;
 u16 max_wm;
 u8 default_wm;
 u8 guard_size;
 u8 cacheline_size;
};

/* used in computing the new watermarks state */
struct intel_wm_config {
 unsigned int num_pipes_active;
 bool sprites_enabled;
 bool sprites_scaled;
};

struct cxsr_latency {
 bool is_desktop : 1;
 bool is_ddr3 : 1;
 u16 fsb_freq;
 u16 mem_freq;
 u16 display_sr;
 u16 display_hpll_disable;
 u16 cursor_sr;
 u16 cursor_hpll_disable;
};

static const struct cxsr_latency cxsr_latency_table[] = {
 {1, 0, 800, 400, 3382, 33382, 3983, 33983},    /* DDR2-400 SC */
 {1, 0, 800, 667, 3354, 33354, 3807, 33807},    /* DDR2-667 SC */
 {1, 0, 800, 800, 3347, 33347, 3763, 33763},    /* DDR2-800 SC */
 {1, 1, 800, 667, 6420, 36420, 6873, 36873},    /* DDR3-667 SC */
 {1, 1, 800, 800, 5902, 35902, 6318, 36318},    /* DDR3-800 SC */

 {1, 0, 667, 400, 3400, 33400, 4021, 34021},    /* DDR2-400 SC */
 {1, 0, 667, 667, 3372, 33372, 3845, 33845},    /* DDR2-667 SC */
 {1, 0, 667, 800, 3386, 33386, 3822, 33822},    /* DDR2-800 SC */
 {1, 1, 667, 667, 6438, 36438, 6911, 36911},    /* DDR3-667 SC */
 {1, 1, 667, 800, 5941, 35941, 6377, 36377},    /* DDR3-800 SC */

 {1, 0, 400, 400, 3472, 33472, 4173, 34173},    /* DDR2-400 SC */
 {1, 0, 400, 667, 3443, 33443, 3996, 33996},    /* DDR2-667 SC */
 {1, 0, 400, 800, 3430, 33430, 3946, 33946},    /* DDR2-800 SC */
 {1, 1, 400, 667, 6509, 36509, 7062, 37062},    /* DDR3-667 SC */
 {1, 1, 400, 800, 5985, 35985, 6501, 36501},    /* DDR3-800 SC */

 {0, 0, 800, 400, 3438, 33438, 4065, 34065},    /* DDR2-400 SC */
 {0, 0, 800, 667, 3410, 33410, 3889, 33889},    /* DDR2-667 SC */
 {0, 0, 800, 800, 3403, 33403, 3845, 33845},    /* DDR2-800 SC */
 {0, 1, 800, 667, 6476, 36476, 6955, 36955},    /* DDR3-667 SC */
 {0, 1, 800, 800, 5958, 35958, 6400, 36400},    /* DDR3-800 SC */

 {0, 0, 667, 400, 3456, 33456, 4103, 34106},    /* DDR2-400 SC */
 {0, 0, 667, 667, 3428, 33428, 3927, 33927},    /* DDR2-667 SC */
 {0, 0, 667, 800, 3443, 33443, 3905, 33905},    /* DDR2-800 SC */
 {0, 1, 667, 667, 6494, 36494, 6993, 36993},    /* DDR3-667 SC */
 {0, 1, 667, 800, 5998, 35998, 6460, 36460},    /* DDR3-800 SC */

 {0, 0, 400, 400, 3528, 33528, 4255, 34255},    /* DDR2-400 SC */
 {0, 0, 400, 667, 3500, 33500, 4079, 34079},    /* DDR2-667 SC */
 {0, 0, 400, 800, 3487, 33487, 4029, 34029},    /* DDR2-800 SC */
 {0, 1, 400, 667, 6566, 36566, 7145, 37145},    /* DDR3-667 SC */
 {0, 1, 400, 800, 6042, 36042, 6584, 36584},    /* DDR3-800 SC */
};

static const struct cxsr_latency *pnv_get_cxsr_latency(struct intel_display *display)
{
 struct drm_i915_private *i915 = to_i915(display->drm);
 int i;

 for (i = 0; i < ARRAY_SIZE(cxsr_latency_table); i++) {
  const struct cxsr_latency *latency = &cxsr_latency_table[i];
  bool is_desktop = !display->platform.mobile;

  if (is_desktop == latency->is_desktop &&
      i915->is_ddr3 == latency->is_ddr3 &&
      DIV_ROUND_CLOSEST(i915->fsb_freq, 1000) == latency->fsb_freq &&
      DIV_ROUND_CLOSEST(i915->mem_freq, 1000) == latency->mem_freq)
   return latency;
 }

 drm_dbg_kms(display->drm,
      "Could not find CxSR latency for DDR%s, FSB %u kHz, MEM %u kHz\n",
      i915->is_ddr3 ? "3" : "2", i915->fsb_freq, i915->mem_freq);

 return NULL;
}

static void chv_set_memory_dvfs(struct intel_display *display, bool enable)
{
 u32 val;

 vlv_punit_get(display->drm);

 val = vlv_punit_read(display->drm, PUNIT_REG_DDR_SETUP2);
 if (enable)
  val &= ~FORCE_DDR_HIGH_FREQ;
 else
  val |= FORCE_DDR_HIGH_FREQ;
 val &= ~FORCE_DDR_LOW_FREQ;
 val |= FORCE_DDR_FREQ_REQ_ACK;
 vlv_punit_write(display->drm, PUNIT_REG_DDR_SETUP2, val);

 if (wait_for((vlv_punit_read(display->drm, PUNIT_REG_DDR_SETUP2) &
        FORCE_DDR_FREQ_REQ_ACK) == 0, 3))
  drm_err(display->drm,
   "timed out waiting for Punit DDR DVFS request\n");

 vlv_punit_put(display->drm);
}

static void chv_set_memory_pm5(struct intel_display *display, bool enable)
{
 u32 val;

 vlv_punit_get(display->drm);

 val = vlv_punit_read(display->drm, PUNIT_REG_DSPSSPM);
 if (enable)
  val |= DSP_MAXFIFO_PM5_ENABLE;
 else
  val &= ~DSP_MAXFIFO_PM5_ENABLE;
 vlv_punit_write(display->drm, PUNIT_REG_DSPSSPM, val);

 vlv_punit_put(display->drm);
}

#define FW_WM(value, plane) \
 (((value) << DSPFW_ ## plane ## _SHIFT) & DSPFW_ ## plane ## _MASK)

static bool _intel_set_memory_cxsr(struct intel_display *display, bool enable)
{
 bool was_enabled;
 u32 val;

 if (display->platform.valleyview || display->platform.cherryview) {
  was_enabled = intel_de_read(display, FW_BLC_SELF_VLV) & FW_CSPWRDWNEN;
  intel_de_write(display, FW_BLC_SELF_VLV, enable ? FW_CSPWRDWNEN : 0);
  intel_de_posting_read(display, FW_BLC_SELF_VLV);
 } else if (display->platform.g4x || display->platform.i965gm) {
  was_enabled = intel_de_read(display, FW_BLC_SELF) & FW_BLC_SELF_EN;
  intel_de_write(display, FW_BLC_SELF, enable ? FW_BLC_SELF_EN : 0);
  intel_de_posting_read(display, FW_BLC_SELF);
 } else if (display->platform.pineview) {
  val = intel_de_read(display, DSPFW3(display));
  was_enabled = val & PINEVIEW_SELF_REFRESH_EN;
  if (enable)
   val |= PINEVIEW_SELF_REFRESH_EN;
  else
   val &= ~PINEVIEW_SELF_REFRESH_EN;
  intel_de_write(display, DSPFW3(display), val);
  intel_de_posting_read(display, DSPFW3(display));
 } else if (display->platform.i945g || display->platform.i945gm) {
  was_enabled = intel_de_read(display, FW_BLC_SELF) & FW_BLC_SELF_EN;
  val = enable ? _MASKED_BIT_ENABLE(FW_BLC_SELF_EN) :
          _MASKED_BIT_DISABLE(FW_BLC_SELF_EN);
  intel_de_write(display, FW_BLC_SELF, val);
  intel_de_posting_read(display, FW_BLC_SELF);
 } else if (display->platform.i915gm) {
  /*
 * FIXME can't find a bit like this for 915G, and
 * yet it does have the related watermark in
 * FW_BLC_SELF. What's going on?
 */

  was_enabled = intel_de_read(display, INSTPM) & INSTPM_SELF_EN;
  val = enable ? _MASKED_BIT_ENABLE(INSTPM_SELF_EN) :
          _MASKED_BIT_DISABLE(INSTPM_SELF_EN);
  intel_de_write(display, INSTPM, val);
  intel_de_posting_read(display, INSTPM);
 } else {
  return false;
 }

 trace_intel_memory_cxsr(display, was_enabled, enable);

 drm_dbg_kms(display->drm, "memory self-refresh is %s (was %s)\n",
      str_enabled_disabled(enable),
      str_enabled_disabled(was_enabled));

 return was_enabled;
}

/**
 * intel_set_memory_cxsr - Configure CxSR state
 * @display: display device
 * @enable: Allow vs. disallow CxSR
 *
 * Allow or disallow the system to enter a special CxSR
 * (C-state self refresh) state. What typically happens in CxSR mode
 * is that several display FIFOs may get combined into a single larger
 * FIFO for a particular plane (so called max FIFO mode) to allow the
 * system to defer memory fetches longer, and the memory will enter
 * self refresh.
 *
 * Note that enabling CxSR does not guarantee that the system enter
 * this special mode, nor does it guarantee that the system stays
 * in that mode once entered. So this just allows/disallows the system
 * to autonomously utilize the CxSR mode. Other factors such as core
 * C-states will affect when/if the system actually enters/exits the
 * CxSR mode.
 *
 * Note that on VLV/CHV this actually only controls the max FIFO mode,
 * and the system is free to enter/exit memory self refresh at any time
 * even when the use of CxSR has been disallowed.
 *
 * While the system is actually in the CxSR/max FIFO mode, some plane
 * control registers will not get latched on vblank. Thus in order to
 * guarantee the system will respond to changes in the plane registers
 * we must always disallow CxSR prior to making changes to those registers.
 * Unfortunately the system will re-evaluate the CxSR conditions at
 * frame start which happens after vblank start (which is when the plane
 * registers would get latched), so we can't proceed with the plane update
 * during the same frame where we disallowed CxSR.
 *
 * Certain platforms also have a deeper HPLL SR mode. Fortunately the
 * HPLL SR mode depends on CxSR itself, so we don't have to hand hold
 * the hardware w.r.t. HPLL SR when writing to plane registers.
 * Disallowing just CxSR is sufficient.
 */

bool intel_set_memory_cxsr(struct intel_display *display, bool enable)
{
 bool ret;

 mutex_lock(&display->wm.wm_mutex);
 ret = _intel_set_memory_cxsr(display, enable);
 if (display->platform.valleyview || display->platform.cherryview)
  display->wm.vlv.cxsr = enable;
 else if (display->platform.g4x)
  display->wm.g4x.cxsr = enable;
 mutex_unlock(&display->wm.wm_mutex);

 return ret;
}

/*
 * Latency for FIFO fetches is dependent on several factors:
 *   - memory configuration (speed, channels)
 *   - chipset
 *   - current MCH state
 * It can be fairly high in some situations, so here we assume a fairly
 * pessimal value.  It's a tradeoff between extra memory fetches (if we
 * set this value too high, the FIFO will fetch frequently to stay full)
 * and power consumption (set it too low to save power and we might see
 * FIFO underruns and display "flicker").
 *
 * A value of 5us seems to be a good balance; safe for very low end
 * platforms but not overly aggressive on lower latency configs.
 */

static const int pessimal_latency_ns = 5000;

#define VLV_FIFO_START(dsparb, dsparb2, lo_shift, hi_shift) \
 ((((dsparb) >> (lo_shift)) & 0xff) | ((((dsparb2) >> (hi_shift)) & 0x1) << 8))

static void vlv_get_fifo_size(struct intel_crtc_state *crtc_state)
{
 struct intel_display *display = to_intel_display(crtc_state);
 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
 struct vlv_fifo_state *fifo_state = &crtc_state->wm.vlv.fifo_state;
 enum pipe pipe = crtc->pipe;
 int sprite0_start, sprite1_start;
 u32 dsparb, dsparb2, dsparb3;

 switch (pipe) {
 case PIPE_A:
  dsparb = intel_de_read(display, DSPARB(display));
  dsparb2 = intel_de_read(display, DSPARB2);
  sprite0_start = VLV_FIFO_START(dsparb, dsparb2, 0, 0);
  sprite1_start = VLV_FIFO_START(dsparb, dsparb2, 8, 4);
  break;
 case PIPE_B:
  dsparb = intel_de_read(display, DSPARB(display));
  dsparb2 = intel_de_read(display, DSPARB2);
  sprite0_start = VLV_FIFO_START(dsparb, dsparb2, 16, 8);
  sprite1_start = VLV_FIFO_START(dsparb, dsparb2, 24, 12);
  break;
 case PIPE_C:
  dsparb2 = intel_de_read(display, DSPARB2);
  dsparb3 = intel_de_read(display, DSPARB3);
  sprite0_start = VLV_FIFO_START(dsparb3, dsparb2, 0, 16);
  sprite1_start = VLV_FIFO_START(dsparb3, dsparb2, 8, 20);
  break;
 default:
  MISSING_CASE(pipe);
  return;
 }

 fifo_state->plane[PLANE_PRIMARY] = sprite0_start;
 fifo_state->plane[PLANE_SPRITE0] = sprite1_start - sprite0_start;
 fifo_state->plane[PLANE_SPRITE1] = 511 - sprite1_start;
 fifo_state->plane[PLANE_CURSOR] = 63;
}

static int i9xx_get_fifo_size(struct intel_display *display,
         enum i9xx_plane_id i9xx_plane)
{
 u32 dsparb = intel_de_read(display, DSPARB(display));
 int size;

 size = dsparb & 0x7f;
 if (i9xx_plane == PLANE_B)
  size = ((dsparb >> DSPARB_CSTART_SHIFT) & 0x7f) - size;

 drm_dbg_kms(display->drm, "FIFO size - (0x%08x) %c: %d\n",
      dsparb, plane_name(i9xx_plane), size);

 return size;
}

static int i830_get_fifo_size(struct intel_display *display,
         enum i9xx_plane_id i9xx_plane)
{
 u32 dsparb = intel_de_read(display, DSPARB(display));
 int size;

 size = dsparb & 0x1ff;
 if (i9xx_plane == PLANE_B)
  size = ((dsparb >> DSPARB_BEND_SHIFT) & 0x1ff) - size;
 size >>= 1; /* Convert to cachelines */

 drm_dbg_kms(display->drm, "FIFO size - (0x%08x) %c: %d\n",
      dsparb, plane_name(i9xx_plane), size);

 return size;
}

static int i845_get_fifo_size(struct intel_display *display,
         enum i9xx_plane_id i9xx_plane)
{
 u32 dsparb = intel_de_read(display, DSPARB(display));
 int size;

 size = dsparb & 0x7f;
 size >>= 2; /* Convert to cachelines */

 drm_dbg_kms(display->drm, "FIFO size - (0x%08x) %c: %d\n",
      dsparb, plane_name(i9xx_plane), size);

 return size;
}

/* Pineview has different values for various configs */
static const struct intel_watermark_params pnv_display_wm = {
 .fifo_size = PINEVIEW_DISPLAY_FIFO,
 .max_wm = PINEVIEW_MAX_WM,
 .default_wm = PINEVIEW_DFT_WM,
 .guard_size = PINEVIEW_GUARD_WM,
 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
};

static const struct intel_watermark_params pnv_display_hplloff_wm = {
 .fifo_size = PINEVIEW_DISPLAY_FIFO,
 .max_wm = PINEVIEW_MAX_WM,
 .default_wm = PINEVIEW_DFT_HPLLOFF_WM,
 .guard_size = PINEVIEW_GUARD_WM,
 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
};

static const struct intel_watermark_params pnv_cursor_wm = {
 .fifo_size = PINEVIEW_CURSOR_FIFO,
 .max_wm = PINEVIEW_CURSOR_MAX_WM,
 .default_wm = PINEVIEW_CURSOR_DFT_WM,
 .guard_size = PINEVIEW_CURSOR_GUARD_WM,
 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
};

static const struct intel_watermark_params pnv_cursor_hplloff_wm = {
 .fifo_size = PINEVIEW_CURSOR_FIFO,
 .max_wm = PINEVIEW_CURSOR_MAX_WM,
 .default_wm = PINEVIEW_CURSOR_DFT_WM,
 .guard_size = PINEVIEW_CURSOR_GUARD_WM,
 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
};

static const struct intel_watermark_params i965_cursor_wm_info = {
 .fifo_size = I965_CURSOR_FIFO,
 .max_wm = I965_CURSOR_MAX_WM,
 .default_wm = I965_CURSOR_DFT_WM,
 .guard_size = 2,
 .cacheline_size = I915_FIFO_LINE_SIZE,
};

static const struct intel_watermark_params i945_wm_info = {
 .fifo_size = I945_FIFO_SIZE,
 .max_wm = I915_MAX_WM,
 .default_wm = 1,
 .guard_size = 2,
 .cacheline_size = I915_FIFO_LINE_SIZE,
};

static const struct intel_watermark_params i915_wm_info = {
 .fifo_size = I915_FIFO_SIZE,
 .max_wm = I915_MAX_WM,
 .default_wm = 1,
 .guard_size = 2,
 .cacheline_size = I915_FIFO_LINE_SIZE,
};

static const struct intel_watermark_params i830_a_wm_info = {
 .fifo_size = I855GM_FIFO_SIZE,
 .max_wm = I915_MAX_WM,
 .default_wm = 1,
 .guard_size = 2,
 .cacheline_size = I830_FIFO_LINE_SIZE,
};

static const struct intel_watermark_params i830_bc_wm_info = {
 .fifo_size = I855GM_FIFO_SIZE,
 .max_wm = I915_MAX_WM / 2,
 .default_wm = 1,
 .guard_size = 2,
 .cacheline_size = I830_FIFO_LINE_SIZE,
};

static const struct intel_watermark_params i845_wm_info = {
 .fifo_size = I830_FIFO_SIZE,
 .max_wm = I915_MAX_WM,
 .default_wm = 1,
 .guard_size = 2,
 .cacheline_size = I830_FIFO_LINE_SIZE,
};

/**
 * intel_wm_method1 - Method 1 / "small buffer" watermark formula
 * @pixel_rate: Pipe pixel rate in kHz
 * @cpp: Plane bytes per pixel
 * @latency: Memory wakeup latency in 0.1us units
 *
 * Compute the watermark using the method 1 or "small buffer"
 * formula. The caller may additionally add extra cachelines
 * to account for TLB misses and clock crossings.
 *
 * This method is concerned with the short term drain rate
 * of the FIFO, ie. it does not account for blanking periods
 * which would effectively reduce the average drain rate across
 * a longer period. The name "small" refers to the fact the
 * FIFO is relatively small compared to the amount of data
 * fetched.
 *
 * The FIFO level vs. time graph might look something like:
 *
 *   |\   |\
 *   | \  | \
 * __---__---__ (- plane active, _ blanking)
 * -> time
 *
 * or perhaps like this:
 *
 *   |\|\  |\|\
 * __----__----__ (- plane active, _ blanking)
 * -> time
 *
 * Returns:
 * The watermark in bytes
 */

static unsigned int intel_wm_method1(unsigned int pixel_rate,
         unsigned int cpp,
         unsigned int latency)
{
 u64 ret;

 ret = mul_u32_u32(pixel_rate, cpp * latency);
 ret = DIV_ROUND_UP_ULL(ret, 10000);

 return ret;
}

/**
 * intel_wm_method2 - Method 2 / "large buffer" watermark formula
 * @pixel_rate: Pipe pixel rate in kHz
 * @htotal: Pipe horizontal total
 * @width: Plane width in pixels
 * @cpp: Plane bytes per pixel
 * @latency: Memory wakeup latency in 0.1us units
 *
 * Compute the watermark using the method 2 or "large buffer"
 * formula. The caller may additionally add extra cachelines
 * to account for TLB misses and clock crossings.
 *
 * This method is concerned with the long term drain rate
 * of the FIFO, ie. it does account for blanking periods
 * which effectively reduce the average drain rate across
 * a longer period. The name "large" refers to the fact the
 * FIFO is relatively large compared to the amount of data
 * fetched.
 *
 * The FIFO level vs. time graph might look something like:
 *
 *    |\___       |\___
 *    |    \___   |    \___
 *    |        \  |        \
 * __ --__--__--__--__--__--__ (- plane active, _ blanking)
 * -> time
 *
 * Returns:
 * The watermark in bytes
 */

static unsigned int intel_wm_method2(unsigned int pixel_rate,
         unsigned int htotal,
         unsigned int width,
         unsigned int cpp,
         unsigned int latency)
{
 unsigned int ret;

 /*
 * FIXME remove once all users are computing
 * watermarks in the correct place.
 */

 if (WARN_ON_ONCE(htotal == 0))
  htotal = 1;

 ret = (latency * pixel_rate) / (htotal * 10000);
 ret = (ret + 1) * width * cpp;

 return ret;
}

/**
 * intel_calculate_wm - calculate watermark level
 * @display: display device
 * @pixel_rate: pixel clock
 * @wm: chip FIFO params
 * @fifo_size: size of the FIFO buffer
 * @cpp: bytes per pixel
 * @latency_ns: memory latency for the platform
 *
 * Calculate the watermark level (the level at which the display plane will
 * start fetching from memory again).  Each chip has a different display
 * FIFO size and allocation, so the caller needs to figure that out and pass
 * in the correct intel_watermark_params structure.
 *
 * As the pixel clock runs, the FIFO will be drained at a rate that depends
 * on the pixel size.  When it reaches the watermark level, it'll start
 * fetching FIFO line sized based chunks from memory until the FIFO fills
 * past the watermark point.  If the FIFO drains completely, a FIFO underrun
 * will occur, and a display engine hang could result.
 */

static unsigned int intel_calculate_wm(struct intel_display *display,
           int pixel_rate,
           const struct intel_watermark_params *wm,
           int fifo_size, int cpp,
           unsigned int latency_ns)
{
 int entries, wm_size;

 /*
 * Note: we need to make sure we don't overflow for various clock &
 * latency values.
 * clocks go from a few thousand to several hundred thousand.
 * latency is usually a few thousand
 */

 entries = intel_wm_method1(pixel_rate, cpp,
       latency_ns / 100);
 entries = DIV_ROUND_UP(entries, wm->cacheline_size) +
  wm->guard_size;
 drm_dbg_kms(display->drm, "FIFO entries required for mode: %d\n", entries);

 wm_size = fifo_size - entries;
 drm_dbg_kms(display->drm, "FIFO watermark level: %d\n", wm_size);

 /* Don't promote wm_size to unsigned... */
 if (wm_size > wm->max_wm)
  wm_size = wm->max_wm;
 if (wm_size <= 0)
  wm_size = wm->default_wm;

 /*
 * Bspec seems to indicate that the value shouldn't be lower than
 * 'burst size + 1'. Certainly 830 is quite unhappy with low values.
 * Lets go for 8 which is the burst size since certain platforms
 * already use a hardcoded 8 (which is what the spec says should be
 * done).
 */

 if (wm_size <= 8)
  wm_size = 8;

 return wm_size;
}

static bool is_disabling(int old, int newint threshold)
{
 return old >= threshold && new < threshold;
}

static bool is_enabling(int old, int newint threshold)
{
 return old < threshold && new >= threshold;
}

static bool intel_crtc_active(struct intel_crtc *crtc)
{
 /* Be paranoid as we can arrive here with only partial
 * state retrieved from the hardware during setup.
 *
 * We can ditch the adjusted_mode.crtc_clock check as soon
 * as Haswell has gained clock readout/fastboot support.
 *
 * We can ditch the crtc->primary->state->fb check as soon as we can
 * properly reconstruct framebuffers.
 *
 * FIXME: The intel_crtc->active here should be switched to
 * crtc->state->active once we have proper CRTC states wired up
 * for atomic.
 */

 return crtc->active && crtc->base.primary->state->fb &&
  crtc->config->hw.adjusted_mode.crtc_clock;
}

static struct intel_crtc *single_enabled_crtc(struct intel_display *display)
{
 struct intel_crtc *crtc, *enabled = NULL;

 for_each_intel_crtc(display->drm, crtc) {
  if (intel_crtc_active(crtc)) {
   if (enabled)
    return NULL;
   enabled = crtc;
  }
 }

 return enabled;
}

static void pnv_update_wm(struct intel_display *display)
{
 struct intel_crtc *crtc;
 const struct cxsr_latency *latency;
 u32 reg;
 unsigned int wm;

 latency = pnv_get_cxsr_latency(display);
 if (!latency) {
  drm_dbg_kms(display->drm, "Unknown FSB/MEM, disabling CxSR\n");
  intel_set_memory_cxsr(display, false);
  return;
 }

 crtc = single_enabled_crtc(display);
 if (crtc) {
  const struct drm_framebuffer *fb =
   crtc->base.primary->state->fb;
  int pixel_rate = crtc->config->pixel_rate;
  int cpp = fb->format->cpp[0];

  /* Display SR */
  wm = intel_calculate_wm(display, pixel_rate,
     &pnv_display_wm,
     pnv_display_wm.fifo_size,
     cpp, latency->display_sr);
  reg = intel_de_read(display, DSPFW1(display));
  reg &= ~DSPFW_SR_MASK;
  reg |= FW_WM(wm, SR);
  intel_de_write(display, DSPFW1(display), reg);
  drm_dbg_kms(display->drm, "DSPFW1 register is %x\n", reg);

  /* cursor SR */
  wm = intel_calculate_wm(display, pixel_rate,
     &pnv_cursor_wm,
     pnv_display_wm.fifo_size,
     4, latency->cursor_sr);
  intel_de_rmw(display, DSPFW3(display),
        DSPFW_CURSOR_SR_MASK, FW_WM(wm, CURSOR_SR));

  /* Display HPLL off SR */
  wm = intel_calculate_wm(display, pixel_rate,
     &pnv_display_hplloff_wm,
     pnv_display_hplloff_wm.fifo_size,
     cpp, latency->display_hpll_disable);
  intel_de_rmw(display, DSPFW3(display),
        DSPFW_HPLL_SR_MASK, FW_WM(wm, HPLL_SR));

  /* cursor HPLL off SR */
  wm = intel_calculate_wm(display, pixel_rate,
     &pnv_cursor_hplloff_wm,
     pnv_display_hplloff_wm.fifo_size,
     4, latency->cursor_hpll_disable);
  reg = intel_de_read(display, DSPFW3(display));
  reg &= ~DSPFW_HPLL_CURSOR_MASK;
  reg |= FW_WM(wm, HPLL_CURSOR);
  intel_de_write(display, DSPFW3(display), reg);
  drm_dbg_kms(display->drm, "DSPFW3 register is %x\n", reg);

  intel_set_memory_cxsr(display, true);
 } else {
  intel_set_memory_cxsr(display, false);
 }
}

static bool i9xx_wm_need_update(const struct intel_plane_state *old_plane_state,
    const struct intel_plane_state *new_plane_state)
{
 /* Update watermarks on tiling or size changes. */
 if (old_plane_state->uapi.visible != new_plane_state->uapi.visible)
  return true;

 if (!old_plane_state->hw.fb || !new_plane_state->hw.fb)
  return false;

 if (old_plane_state->hw.fb->modifier != new_plane_state->hw.fb->modifier ||
     old_plane_state->hw.rotation != new_plane_state->hw.rotation ||
     drm_rect_width(&old_plane_state->uapi.src) != drm_rect_width(&new_plane_state->uapi.src) ||
     drm_rect_height(&old_plane_state->uapi.src) != drm_rect_height(&new_plane_state->uapi.src) ||
     drm_rect_width(&old_plane_state->uapi.dst) != drm_rect_width(&new_plane_state->uapi.dst) ||
     drm_rect_height(&old_plane_state->uapi.dst) != drm_rect_height(&new_plane_state->uapi.dst))
  return true;

 return false;
}

static void i9xx_wm_compute(struct intel_crtc_state *new_crtc_state,
       const struct intel_plane_state *old_plane_state,
       const struct intel_plane_state *new_plane_state)
{
 bool turn_off, turn_on, visible, was_visible, mode_changed;

 mode_changed = intel_crtc_needs_modeset(new_crtc_state);
 was_visible = old_plane_state->uapi.visible;
 visible = new_plane_state->uapi.visible;

 if (!was_visible && !visible)
  return;

 turn_off = was_visible && (!visible || mode_changed);
 turn_on = visible && (!was_visible || mode_changed);

 /* FIXME nuke when all wm code is atomic */
 if (turn_on) {
  new_crtc_state->update_wm_pre = true;
 } else if (turn_off) {
  new_crtc_state->update_wm_post = true;
 } else if (i9xx_wm_need_update(old_plane_state, new_plane_state)) {
  /* FIXME bollocks */
  new_crtc_state->update_wm_pre = true;
  new_crtc_state->update_wm_post = true;
 }
}

static int i9xx_compute_watermarks(struct intel_atomic_state *state,
       struct intel_crtc *crtc)
{
 struct intel_crtc_state *new_crtc_state =
  intel_atomic_get_new_crtc_state(state, crtc);
 const struct intel_plane_state *old_plane_state;
 const struct intel_plane_state *new_plane_state;
 struct intel_plane *plane;
 int i;

 for_each_oldnew_intel_plane_in_state(state, plane, old_plane_state,
          new_plane_state, i) {
  if (plane->pipe != crtc->pipe)
   continue;

  i9xx_wm_compute(new_crtc_state, old_plane_state, new_plane_state);
 }

 return 0;
}

/*
 * Documentation says:
 * "If the line size is small, the TLB fetches can get in the way of the
 *  data fetches, causing some lag in the pixel data return which is not
 *  accounted for in the above formulas. The following adjustment only
 *  needs to be applied if eight whole lines fit in the buffer at once.
 *  The WM is adjusted upwards by the difference between the FIFO size
 *  and the size of 8 whole lines. This adjustment is always performed
 *  in the actual pixel depth regardless of whether FBC is enabled or not."
 */

static unsigned int g4x_tlb_miss_wa(int fifo_size, int width, int cpp)
{
 int tlb_miss = fifo_size * 64 - width * cpp * 8;

 return max(0, tlb_miss);
}

static void g4x_write_wm_values(struct intel_display *display,
    const struct g4x_wm_values *wm)
{
 enum pipe pipe;

 for_each_pipe(display, pipe)
  trace_g4x_wm(intel_crtc_for_pipe(display, pipe), wm);

 intel_de_write(display, DSPFW1(display),
         FW_WM(wm->sr.plane, SR) |
         FW_WM(wm->pipe[PIPE_B].plane[PLANE_CURSOR], CURSORB) |
         FW_WM(wm->pipe[PIPE_B].plane[PLANE_PRIMARY], PLANEB) |
         FW_WM(wm->pipe[PIPE_A].plane[PLANE_PRIMARY], PLANEA));
 intel_de_write(display, DSPFW2(display),
         (wm->fbc_en ? DSPFW_FBC_SR_EN : 0) |
         FW_WM(wm->sr.fbc, FBC_SR) |
         FW_WM(wm->hpll.fbc, FBC_HPLL_SR) |
         FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE0], SPRITEB) |
         FW_WM(wm->pipe[PIPE_A].plane[PLANE_CURSOR], CURSORA) |
         FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE0], SPRITEA));
 intel_de_write(display, DSPFW3(display),
         (wm->hpll_en ? DSPFW_HPLL_SR_EN : 0) |
         FW_WM(wm->sr.cursor, CURSOR_SR) |
         FW_WM(wm->hpll.cursor, HPLL_CURSOR) |
         FW_WM(wm->hpll.plane, HPLL_SR));

 intel_de_posting_read(display, DSPFW1(display));
}

#define FW_WM_VLV(value, plane) \
 (((value) << DSPFW_ ## plane ## _SHIFT) & DSPFW_ ## plane ## _MASK_VLV)

static void vlv_write_wm_values(struct intel_display *display,
    const struct vlv_wm_values *wm)
{
 enum pipe pipe;

 for_each_pipe(display, pipe) {
  trace_vlv_wm(intel_crtc_for_pipe(display, pipe), wm);

  intel_de_write(display, VLV_DDL(pipe),
          (wm->ddl[pipe].plane[PLANE_CURSOR] << DDL_CURSOR_SHIFT) |
          (wm->ddl[pipe].plane[PLANE_SPRITE1] << DDL_SPRITE_SHIFT(1)) |
          (wm->ddl[pipe].plane[PLANE_SPRITE0] << DDL_SPRITE_SHIFT(0)) |
          (wm->ddl[pipe].plane[PLANE_PRIMARY] << DDL_PLANE_SHIFT));
 }

 /*
 * Zero the (unused) WM1 watermarks, and also clear all the
 * high order bits so that there are no out of bounds values
 * present in the registers during the reprogramming.
 */

 intel_de_write(display, DSPHOWM, 0);
 intel_de_write(display, DSPHOWM1, 0);
 intel_de_write(display, DSPFW4, 0);
 intel_de_write(display, DSPFW5, 0);
 intel_de_write(display, DSPFW6, 0);

 intel_de_write(display, DSPFW1(display),
         FW_WM(wm->sr.plane, SR) |
         FW_WM(wm->pipe[PIPE_B].plane[PLANE_CURSOR], CURSORB) |
         FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_PRIMARY], PLANEB) |
         FW_WM_VLV(wm->pipe[PIPE_A].plane[PLANE_PRIMARY], PLANEA));
 intel_de_write(display, DSPFW2(display),
         FW_WM_VLV(wm->pipe[PIPE_A].plane[PLANE_SPRITE1], SPRITEB) |
         FW_WM(wm->pipe[PIPE_A].plane[PLANE_CURSOR], CURSORA) |
         FW_WM_VLV(wm->pipe[PIPE_A].plane[PLANE_SPRITE0], SPRITEA));
 intel_de_write(display, DSPFW3(display),
         FW_WM(wm->sr.cursor, CURSOR_SR));

 if (display->platform.cherryview) {
  intel_de_write(display, DSPFW7_CHV,
          FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_SPRITE1], SPRITED) |
          FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_SPRITE0], SPRITEC));
  intel_de_write(display, DSPFW8_CHV,
          FW_WM_VLV(wm->pipe[PIPE_C].plane[PLANE_SPRITE1], SPRITEF) |
          FW_WM_VLV(wm->pipe[PIPE_C].plane[PLANE_SPRITE0], SPRITEE));
  intel_de_write(display, DSPFW9_CHV,
          FW_WM_VLV(wm->pipe[PIPE_C].plane[PLANE_PRIMARY], PLANEC) |
          FW_WM(wm->pipe[PIPE_C].plane[PLANE_CURSOR], CURSORC));
  intel_de_write(display, DSPHOWM,
          FW_WM(wm->sr.plane >> 9, SR_HI) |
          FW_WM(wm->pipe[PIPE_C].plane[PLANE_SPRITE1] >> 8, SPRITEF_HI) |
          FW_WM(wm->pipe[PIPE_C].plane[PLANE_SPRITE0] >> 8, SPRITEE_HI) |
          FW_WM(wm->pipe[PIPE_C].plane[PLANE_PRIMARY] >> 8, PLANEC_HI) |
          FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE1] >> 8, SPRITED_HI) |
          FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE0] >> 8, SPRITEC_HI) |
          FW_WM(wm->pipe[PIPE_B].plane[PLANE_PRIMARY] >> 8, PLANEB_HI) |
          FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE1] >> 8, SPRITEB_HI) |
          FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE0] >> 8, SPRITEA_HI) |
          FW_WM(wm->pipe[PIPE_A].plane[PLANE_PRIMARY] >> 8, PLANEA_HI));
 } else {
  intel_de_write(display, DSPFW7,
          FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_SPRITE1], SPRITED) |
          FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_SPRITE0], SPRITEC));
  intel_de_write(display, DSPHOWM,
          FW_WM(wm->sr.plane >> 9, SR_HI) |
          FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE1] >> 8, SPRITED_HI) |
          FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE0] >> 8, SPRITEC_HI) |
          FW_WM(wm->pipe[PIPE_B].plane[PLANE_PRIMARY] >> 8, PLANEB_HI) |
          FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE1] >> 8, SPRITEB_HI) |
          FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE0] >> 8, SPRITEA_HI) |
          FW_WM(wm->pipe[PIPE_A].plane[PLANE_PRIMARY] >> 8, PLANEA_HI));
 }

 intel_de_posting_read(display, DSPFW1(display));
}

#undef FW_WM_VLV

static void g4x_setup_wm_latency(struct intel_display *display)
{
 /* all latencies in usec */
 display->wm.pri_latency[G4X_WM_LEVEL_NORMAL] = 5;
 display->wm.pri_latency[G4X_WM_LEVEL_SR] = 12;
 display->wm.pri_latency[G4X_WM_LEVEL_HPLL] = 35;

 display->wm.num_levels = G4X_WM_LEVEL_HPLL + 1;
}

static int g4x_plane_fifo_size(enum plane_id plane_id, int level)
{
 /*
 * DSPCNTR[13] supposedly controls whether the
 * primary plane can use the FIFO space otherwise
 * reserved for the sprite plane. It's not 100% clear
 * what the actual FIFO size is, but it looks like we
 * can happily set both primary and sprite watermarks
 * up to 127 cachelines. So that would seem to mean
 * that either DSPCNTR[13] doesn't do anything, or that
 * the total FIFO is >= 256 cachelines in size. Either
 * way, we don't seem to have to worry about this
 * repartitioning as the maximum watermark value the
 * register can hold for each plane is lower than the
 * minimum FIFO size.
 */

 switch (plane_id) {
 case PLANE_CURSOR:
  return 63;
 case PLANE_PRIMARY:
  return level == G4X_WM_LEVEL_NORMAL ? 127 : 511;
 case PLANE_SPRITE0:
  return level == G4X_WM_LEVEL_NORMAL ? 127 : 0;
 default:
  MISSING_CASE(plane_id);
  return 0;
 }
}

static int g4x_fbc_fifo_size(int level)
{
 switch (level) {
 case G4X_WM_LEVEL_SR:
  return 7;
 case G4X_WM_LEVEL_HPLL:
  return 15;
 default:
  MISSING_CASE(level);
  return 0;
 }
}

static u16 g4x_compute_wm(const struct intel_crtc_state *crtc_state,
     const struct intel_plane_state *plane_state,
     int level)
{
 struct intel_display *display = to_intel_display(plane_state);
 struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
 const struct drm_display_mode *pipe_mode =
  &crtc_state->hw.pipe_mode;
 unsigned int latency = display->wm.pri_latency[level] * 10;
 unsigned int pixel_rate, htotal, cpp, width, wm;

 if (latency == 0)
  return USHRT_MAX;

 if (!intel_wm_plane_visible(crtc_state, plane_state))
  return 0;

 cpp = plane_state->hw.fb->format->cpp[0];

 /*
 * WaUse32BppForSRWM:ctg,elk
 *
 * The spec fails to list this restriction for the
 * HPLL watermark, which seems a little strange.
 * Let's use 32bpp for the HPLL watermark as well.
 */

 if (plane->id == PLANE_PRIMARY &&
     level != G4X_WM_LEVEL_NORMAL)
  cpp = max(cpp, 4u);

 pixel_rate = crtc_state->pixel_rate;
 htotal = pipe_mode->crtc_htotal;
 width = drm_rect_width(&plane_state->uapi.src) >> 16;

 if (plane->id == PLANE_CURSOR) {
  wm = intel_wm_method2(pixel_rate, htotal, width, cpp, latency);
 } else if (plane->id == PLANE_PRIMARY &&
     level == G4X_WM_LEVEL_NORMAL) {
  wm = intel_wm_method1(pixel_rate, cpp, latency);
 } else {
  unsigned int small, large;

  small = intel_wm_method1(pixel_rate, cpp, latency);
  large = intel_wm_method2(pixel_rate, htotal, width, cpp, latency);

  wm = min(small, large);
 }

 wm += g4x_tlb_miss_wa(g4x_plane_fifo_size(plane->id, level),
         width, cpp);

 wm = DIV_ROUND_UP(wm, 64) + 2;

 return min_t(unsigned int, wm, USHRT_MAX);
}

static bool g4x_raw_plane_wm_set(struct intel_crtc_state *crtc_state,
     int level, enum plane_id plane_id, u16 value)
{
 struct intel_display *display = to_intel_display(crtc_state);
 bool dirty = false;

 for (; level < display->wm.num_levels; level++) {
  struct g4x_pipe_wm *raw = &crtc_state->wm.g4x.raw[level];

  dirty |= raw->plane[plane_id] != value;
  raw->plane[plane_id] = value;
 }

 return dirty;
}

static bool g4x_raw_fbc_wm_set(struct intel_crtc_state *crtc_state,
          int level, u16 value)
{
 struct intel_display *display = to_intel_display(crtc_state);
 bool dirty = false;

 /* NORMAL level doesn't have an FBC watermark */
 level = max(level, G4X_WM_LEVEL_SR);

 for (; level < display->wm.num_levels; level++) {
  struct g4x_pipe_wm *raw = &crtc_state->wm.g4x.raw[level];

  dirty |= raw->fbc != value;
  raw->fbc = value;
 }

 return dirty;
}

static u32 ilk_compute_fbc_wm(const struct intel_crtc_state *crtc_state,
         const struct intel_plane_state *plane_state,
         u32 pri_val);

static bool g4x_raw_plane_wm_compute(struct intel_crtc_state *crtc_state,
         const struct intel_plane_state *plane_state)
{
 struct intel_display *display = to_intel_display(crtc_state);
 struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
 enum plane_id plane_id = plane->id;
 bool dirty = false;
 int level;

 if (!intel_wm_plane_visible(crtc_state, plane_state)) {
  dirty |= g4x_raw_plane_wm_set(crtc_state, 0, plane_id, 0);
  if (plane_id == PLANE_PRIMARY)
   dirty |= g4x_raw_fbc_wm_set(crtc_state, 0, 0);
  goto out;
 }

 for (level = 0; level < display->wm.num_levels; level++) {
  struct g4x_pipe_wm *raw = &crtc_state->wm.g4x.raw[level];
  int wm, max_wm;

  wm = g4x_compute_wm(crtc_state, plane_state, level);
  max_wm = g4x_plane_fifo_size(plane_id, level);

  if (wm > max_wm)
   break;

  dirty |= raw->plane[plane_id] != wm;
  raw->plane[plane_id] = wm;

  if (plane_id != PLANE_PRIMARY ||
      level == G4X_WM_LEVEL_NORMAL)
   continue;

  wm = ilk_compute_fbc_wm(crtc_state, plane_state,
     raw->plane[plane_id]);
  max_wm = g4x_fbc_fifo_size(level);

  /*
 * FBC wm is not mandatory as we
 * can always just disable its use.
 */

  if (wm > max_wm)
   wm = USHRT_MAX;

  dirty |= raw->fbc != wm;
  raw->fbc = wm;
 }

 /* mark watermarks as invalid */
 dirty |= g4x_raw_plane_wm_set(crtc_state, level, plane_id, USHRT_MAX);

 if (plane_id == PLANE_PRIMARY)
  dirty |= g4x_raw_fbc_wm_set(crtc_state, level, USHRT_MAX);

 out:
 if (dirty) {
  drm_dbg_kms(display->drm,
       "%s watermarks: normal=%d, SR=%d, HPLL=%d\n",
       plane->base.name,
       crtc_state->wm.g4x.raw[G4X_WM_LEVEL_NORMAL].plane[plane_id],
       crtc_state->wm.g4x.raw[G4X_WM_LEVEL_SR].plane[plane_id],
       crtc_state->wm.g4x.raw[G4X_WM_LEVEL_HPLL].plane[plane_id]);

  if (plane_id == PLANE_PRIMARY)
   drm_dbg_kms(display->drm,
        "FBC watermarks: SR=%d, HPLL=%d\n",
        crtc_state->wm.g4x.raw[G4X_WM_LEVEL_SR].fbc,
        crtc_state->wm.g4x.raw[G4X_WM_LEVEL_HPLL].fbc);
 }

 return dirty;
}

static bool g4x_raw_plane_wm_is_valid(const struct intel_crtc_state *crtc_state,
          enum plane_id plane_id, int level)
{
 const struct g4x_pipe_wm *raw = &crtc_state->wm.g4x.raw[level];

 return raw->plane[plane_id] <= g4x_plane_fifo_size(plane_id, level);
}

static bool g4x_raw_crtc_wm_is_valid(const struct intel_crtc_state *crtc_state,
         int level)
{
 struct intel_display *display = to_intel_display(crtc_state);

 if (level >= display->wm.num_levels)
  return false;

 return g4x_raw_plane_wm_is_valid(crtc_state, PLANE_PRIMARY, level) &&
  g4x_raw_plane_wm_is_valid(crtc_state, PLANE_SPRITE0, level) &&
  g4x_raw_plane_wm_is_valid(crtc_state, PLANE_CURSOR, level);
}

/* mark all levels starting from 'level' as invalid */
static void g4x_invalidate_wms(struct intel_crtc *crtc,
          struct g4x_wm_state *wm_state, int level)
{
 if (level <= G4X_WM_LEVEL_NORMAL) {
  enum plane_id plane_id;

  for_each_plane_id_on_crtc(crtc, plane_id)
   wm_state->wm.plane[plane_id] = USHRT_MAX;
 }

 if (level <= G4X_WM_LEVEL_SR) {
  wm_state->cxsr = false;
  wm_state->sr.cursor = USHRT_MAX;
  wm_state->sr.plane = USHRT_MAX;
  wm_state->sr.fbc = USHRT_MAX;
 }

 if (level <= G4X_WM_LEVEL_HPLL) {
  wm_state->hpll_en = false;
  wm_state->hpll.cursor = USHRT_MAX;
  wm_state->hpll.plane = USHRT_MAX;
  wm_state->hpll.fbc = USHRT_MAX;
 }
}

static bool g4x_compute_fbc_en(const struct g4x_wm_state *wm_state,
          int level)
{
 if (level < G4X_WM_LEVEL_SR)
  return false;

 if (level >= G4X_WM_LEVEL_SR &&
     wm_state->sr.fbc > g4x_fbc_fifo_size(G4X_WM_LEVEL_SR))
  return false;

 if (level >= G4X_WM_LEVEL_HPLL &&
     wm_state->hpll.fbc > g4x_fbc_fifo_size(G4X_WM_LEVEL_HPLL))
  return false;

 return true;
}

static int _g4x_compute_pipe_wm(struct intel_crtc_state *crtc_state)
{
 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
 struct g4x_wm_state *wm_state = &crtc_state->wm.g4x.optimal;
 u8 active_planes = crtc_state->active_planes & ~BIT(PLANE_CURSOR);
 const struct g4x_pipe_wm *raw;
 enum plane_id plane_id;
 int level;

 level = G4X_WM_LEVEL_NORMAL;
 if (!g4x_raw_crtc_wm_is_valid(crtc_state, level))
  goto out;

 raw = &crtc_state->wm.g4x.raw[level];
 for_each_plane_id_on_crtc(crtc, plane_id)
  wm_state->wm.plane[plane_id] = raw->plane[plane_id];

 level = G4X_WM_LEVEL_SR;
 if (!g4x_raw_crtc_wm_is_valid(crtc_state, level))
  goto out;

 raw = &crtc_state->wm.g4x.raw[level];
 wm_state->sr.plane = raw->plane[PLANE_PRIMARY];
 wm_state->sr.cursor = raw->plane[PLANE_CURSOR];
 wm_state->sr.fbc = raw->fbc;

 wm_state->cxsr = active_planes == BIT(PLANE_PRIMARY);

 level = G4X_WM_LEVEL_HPLL;
 if (!g4x_raw_crtc_wm_is_valid(crtc_state, level))
  goto out;

 raw = &crtc_state->wm.g4x.raw[level];
 wm_state->hpll.plane = raw->plane[PLANE_PRIMARY];
 wm_state->hpll.cursor = raw->plane[PLANE_CURSOR];
 wm_state->hpll.fbc = raw->fbc;

 wm_state->hpll_en = wm_state->cxsr;

 level++;

 out:
 if (level == G4X_WM_LEVEL_NORMAL)
  return -EINVAL;

 /* invalidate the higher levels */
 g4x_invalidate_wms(crtc, wm_state, level);

 /*
 * Determine if the FBC watermark(s) can be used. IF
 * this isn't the case we prefer to disable the FBC
 * watermark(s) rather than disable the SR/HPLL
 * level(s) entirely. 'level-1' is the highest valid
 * level here.
 */

 wm_state->fbc_en = g4x_compute_fbc_en(wm_state, level - 1);

 return 0;
}

static int g4x_compute_pipe_wm(struct intel_atomic_state *state,
          struct intel_crtc *crtc)
{
 struct intel_crtc_state *crtc_state =
  intel_atomic_get_new_crtc_state(state, crtc);
 const struct intel_plane_state *old_plane_state;
 const struct intel_plane_state *new_plane_state;
 struct intel_plane *plane;
 unsigned int dirty = 0;
 int i;

 for_each_oldnew_intel_plane_in_state(state, plane,
          old_plane_state,
          new_plane_state, i) {
  if (new_plane_state->hw.crtc != &crtc->base &&
      old_plane_state->hw.crtc != &crtc->base)
   continue;

  if (g4x_raw_plane_wm_compute(crtc_state, new_plane_state))
   dirty |= BIT(plane->id);
 }

 if (!dirty)
  return 0;

 return _g4x_compute_pipe_wm(crtc_state);
}

static int g4x_compute_intermediate_wm(struct intel_atomic_state *state,
           struct intel_crtc *crtc)
{
 struct intel_display *display = to_intel_display(state);
 struct intel_crtc_state *new_crtc_state =
  intel_atomic_get_new_crtc_state(state, crtc);
 const struct intel_crtc_state *old_crtc_state =
  intel_atomic_get_old_crtc_state(state, crtc);
 struct g4x_wm_state *intermediate = &new_crtc_state->wm.g4x.intermediate;
 const struct g4x_wm_state *optimal = &new_crtc_state->wm.g4x.optimal;
 const struct g4x_wm_state *active = &old_crtc_state->wm.g4x.optimal;
 enum plane_id plane_id;

 if (!new_crtc_state->hw.active ||
     intel_crtc_needs_modeset(new_crtc_state)) {
  *intermediate = *optimal;

  intermediate->cxsr = false;
  intermediate->hpll_en = false;
  goto out;
 }

 intermediate->cxsr = optimal->cxsr && active->cxsr &&
  !new_crtc_state->disable_cxsr;
 intermediate->hpll_en = optimal->hpll_en && active->hpll_en &&
  !new_crtc_state->disable_cxsr;
 intermediate->fbc_en = optimal->fbc_en && active->fbc_en;

 for_each_plane_id_on_crtc(crtc, plane_id) {
  intermediate->wm.plane[plane_id] =
   max(optimal->wm.plane[plane_id],
       active->wm.plane[plane_id]);

  drm_WARN_ON(display->drm, intermediate->wm.plane[plane_id] >
       g4x_plane_fifo_size(plane_id, G4X_WM_LEVEL_NORMAL));
 }

 intermediate->sr.plane = max(optimal->sr.plane,
         active->sr.plane);
 intermediate->sr.cursor = max(optimal->sr.cursor,
          active->sr.cursor);
 intermediate->sr.fbc = max(optimal->sr.fbc,
       active->sr.fbc);

 intermediate->hpll.plane = max(optimal->hpll.plane,
           active->hpll.plane);
 intermediate->hpll.cursor = max(optimal->hpll.cursor,
     active->hpll.cursor);
 intermediate->hpll.fbc = max(optimal->hpll.fbc,
         active->hpll.fbc);

 drm_WARN_ON(display->drm,
      (intermediate->sr.plane >
       g4x_plane_fifo_size(PLANE_PRIMARY, G4X_WM_LEVEL_SR) ||
       intermediate->sr.cursor >
       g4x_plane_fifo_size(PLANE_CURSOR, G4X_WM_LEVEL_SR)) &&
      intermediate->cxsr);
 drm_WARN_ON(display->drm,
      (intermediate->sr.plane >
       g4x_plane_fifo_size(PLANE_PRIMARY, G4X_WM_LEVEL_HPLL) ||
       intermediate->sr.cursor >
       g4x_plane_fifo_size(PLANE_CURSOR, G4X_WM_LEVEL_HPLL)) &&
      intermediate->hpll_en);

 drm_WARN_ON(display->drm,
      intermediate->sr.fbc > g4x_fbc_fifo_size(1) &&
      intermediate->fbc_en && intermediate->cxsr);
 drm_WARN_ON(display->drm,
      intermediate->hpll.fbc > g4x_fbc_fifo_size(2) &&
      intermediate->fbc_en && intermediate->hpll_en);

out:
 /*
 * If our intermediate WM are identical to the final WM, then we can
 * omit the post-vblank programming; only update if it's different.
 */

 if (memcmp(intermediate, optimal, sizeof(*intermediate)) != 0)
  new_crtc_state->wm.need_postvbl_update = true;

 return 0;
}

static int g4x_compute_watermarks(struct intel_atomic_state *state,
      struct intel_crtc *crtc)
{
 int ret;

 ret = g4x_compute_pipe_wm(state, crtc);
 if (ret)
  return ret;

 ret = g4x_compute_intermediate_wm(state, crtc);
 if (ret)
  return ret;

 return 0;
}

static void g4x_merge_wm(struct intel_display *display,
    struct g4x_wm_values *wm)
{
 struct intel_crtc *crtc;
 int num_active_pipes = 0;

 wm->cxsr = true;
 wm->hpll_en = true;
 wm->fbc_en = true;

 for_each_intel_crtc(display->drm, crtc) {
  const struct g4x_wm_state *wm_state = &crtc->wm.active.g4x;

  if (!crtc->active)
   continue;

  if (!wm_state->cxsr)
   wm->cxsr = false;
  if (!wm_state->hpll_en)
   wm->hpll_en = false;
  if (!wm_state->fbc_en)
   wm->fbc_en = false;

  num_active_pipes++;
 }

 if (num_active_pipes != 1) {
  wm->cxsr = false;
  wm->hpll_en = false;
  wm->fbc_en = false;
 }

 for_each_intel_crtc(display->drm, crtc) {
  const struct g4x_wm_state *wm_state = &crtc->wm.active.g4x;
  enum pipe pipe = crtc->pipe;

  wm->pipe[pipe] = wm_state->wm;
  if (crtc->active && wm->cxsr)
   wm->sr = wm_state->sr;
  if (crtc->active && wm->hpll_en)
   wm->hpll = wm_state->hpll;
 }
}

static void g4x_program_watermarks(struct intel_display *display)
{
 struct g4x_wm_values *old_wm = &display->wm.g4x;
 struct g4x_wm_values new_wm = {};

 g4x_merge_wm(display, &new_wm);

 if (memcmp(old_wm, &new_wm, sizeof(new_wm)) == 0)
  return;

 if (is_disabling(old_wm->cxsr, new_wm.cxsr, true))
  _intel_set_memory_cxsr(display, false);

 g4x_write_wm_values(display, &new_wm);

 if (is_enabling(old_wm->cxsr, new_wm.cxsr, true))
  _intel_set_memory_cxsr(display, true);

 *old_wm = new_wm;
}

static void g4x_initial_watermarks(struct intel_atomic_state *state,
       struct intel_crtc *crtc)
{
 struct intel_display *display = to_intel_display(crtc);
 const struct intel_crtc_state *crtc_state =
  intel_atomic_get_new_crtc_state(state, crtc);

 mutex_lock(&display->wm.wm_mutex);
 crtc->wm.active.g4x = crtc_state->wm.g4x.intermediate;
 g4x_program_watermarks(display);
 mutex_unlock(&display->wm.wm_mutex);
}

static void g4x_optimize_watermarks(struct intel_atomic_state *state,
        struct intel_crtc *crtc)
{
 struct intel_display *display = to_intel_display(crtc);
 const struct intel_crtc_state *crtc_state =
  intel_atomic_get_new_crtc_state(state, crtc);

 if (!crtc_state->wm.need_postvbl_update)
  return;

 mutex_lock(&display->wm.wm_mutex);
 crtc->wm.active.g4x = crtc_state->wm.g4x.optimal;
 g4x_program_watermarks(display);
 mutex_unlock(&display->wm.wm_mutex);
}

/* latency must be in 0.1us units. */
static unsigned int vlv_wm_method2(unsigned int pixel_rate,
       unsigned int htotal,
       unsigned int width,
       unsigned int cpp,
       unsigned int latency)
{
 unsigned int ret;

 ret = intel_wm_method2(pixel_rate, htotal,
          width, cpp, latency);
 ret = DIV_ROUND_UP(ret, 64);

 return ret;
}

static void vlv_setup_wm_latency(struct intel_display *display)
{
 /* all latencies in usec */
 display->wm.pri_latency[VLV_WM_LEVEL_PM2] = 3;

 display->wm.num_levels = VLV_WM_LEVEL_PM2 + 1;

 if (display->platform.cherryview) {
  display->wm.pri_latency[VLV_WM_LEVEL_PM5] = 12;
  display->wm.pri_latency[VLV_WM_LEVEL_DDR_DVFS] = 33;

  display->wm.num_levels = VLV_WM_LEVEL_DDR_DVFS + 1;
 }
}

static u16 vlv_compute_wm_level(const struct intel_crtc_state *crtc_state,
    const struct intel_plane_state *plane_state,
    int level)
{
 struct intel_display *display = to_intel_display(plane_state);
 struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
 const struct drm_display_mode *pipe_mode =
  &crtc_state->hw.pipe_mode;
 unsigned int pixel_rate, htotal, cpp, width, wm;

 if (display->wm.pri_latency[level] == 0)
  return USHRT_MAX;

 if (!intel_wm_plane_visible(crtc_state, plane_state))
  return 0;

 cpp = plane_state->hw.fb->format->cpp[0];
 pixel_rate = crtc_state->pixel_rate;
 htotal = pipe_mode->crtc_htotal;
 width = drm_rect_width(&plane_state->uapi.src) >> 16;

 if (plane->id == PLANE_CURSOR) {
  /*
 * FIXME the formula gives values that are
 * too big for the cursor FIFO, and hence we
 * would never be able to use cursors. For
 * now just hardcode the watermark.
 */

  wm = 63;
 } else {
  wm = vlv_wm_method2(pixel_rate, htotal, width, cpp,
        display->wm.pri_latency[level] * 10);
 }

 return min_t(unsigned int, wm, USHRT_MAX);
}

static bool vlv_need_sprite0_fifo_workaround(unsigned int active_planes)
{
 return (active_planes & (BIT(PLANE_SPRITE0) |
     BIT(PLANE_SPRITE1))) == BIT(PLANE_SPRITE1);
}

static int vlv_compute_fifo(struct intel_crtc_state *crtc_state)
{
 struct intel_display *display = to_intel_display(crtc_state);
 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
 const struct g4x_pipe_wm *raw =
  &crtc_state->wm.vlv.raw[VLV_WM_LEVEL_PM2];
 struct vlv_fifo_state *fifo_state = &crtc_state->wm.vlv.fifo_state;
 u8 active_planes = crtc_state->active_planes & ~BIT(PLANE_CURSOR);
 int num_active_planes = hweight8(active_planes);
 const int fifo_size = 511;
 int fifo_extra, fifo_left = fifo_size;
 int sprite0_fifo_extra = 0;
 unsigned int total_rate;
 enum plane_id plane_id;

 /*
 * When enabling sprite0 after sprite1 has already been enabled
 * we tend to get an underrun unless sprite0 already has some
 * FIFO space allocated. Hence we always allocate at least one
 * cacheline for sprite0 whenever sprite1 is enabled.
 *
 * All other plane enable sequences appear immune to this problem.
 */

 if (vlv_need_sprite0_fifo_workaround(active_planes))
  sprite0_fifo_extra = 1;

 total_rate = raw->plane[PLANE_PRIMARY] +
  raw->plane[PLANE_SPRITE0] +
  raw->plane[PLANE_SPRITE1] +
  sprite0_fifo_extra;

 if (total_rate > fifo_size)
  return -EINVAL;

 if (total_rate == 0)
  total_rate = 1;

 for_each_plane_id_on_crtc(crtc, plane_id) {
  unsigned int rate;

  if ((active_planes & BIT(plane_id)) == 0) {
   fifo_state->plane[plane_id] = 0;
   continue;
  }

  rate = raw->plane[plane_id];
  fifo_state->plane[plane_id] = fifo_size * rate / total_rate;
  fifo_left -= fifo_state->plane[plane_id];
 }

 fifo_state->plane[PLANE_SPRITE0] += sprite0_fifo_extra;
 fifo_left -= sprite0_fifo_extra;

 fifo_state->plane[PLANE_CURSOR] = 63;

 fifo_extra = DIV_ROUND_UP(fifo_left, num_active_planes ?: 1);

 /* spread the remainder evenly */
 for_each_plane_id_on_crtc(crtc, plane_id) {
  int plane_extra;

  if (fifo_left == 0)
   break;

  if ((active_planes & BIT(plane_id)) == 0)
   continue;

  plane_extra = min(fifo_extra, fifo_left);
  fifo_state->plane[plane_id] += plane_extra;
  fifo_left -= plane_extra;
 }

 drm_WARN_ON(display->drm, active_planes != 0 && fifo_left != 0);

 /* give it all to the first plane if none are active */
 if (active_planes == 0) {
  drm_WARN_ON(display->drm, fifo_left != fifo_size);
  fifo_state->plane[PLANE_PRIMARY] = fifo_left;
 }

 return 0;
}

/* mark all levels starting from 'level' as invalid */
static void vlv_invalidate_wms(struct intel_crtc *crtc,
          struct vlv_wm_state *wm_state, int level)
{
 struct intel_display *display = to_intel_display(crtc);

 for (; level < display->wm.num_levels; level++) {
  enum plane_id plane_id;

  for_each_plane_id_on_crtc(crtc, plane_id)
   wm_state->wm[level].plane[plane_id] = USHRT_MAX;

  wm_state->sr[level].cursor = USHRT_MAX;
  wm_state->sr[level].plane = USHRT_MAX;
 }
}

static u16 vlv_invert_wm_value(u16 wm, u16 fifo_size)
{
 if (wm > fifo_size)
  return USHRT_MAX;
 else
  return fifo_size - wm;
}

/*
 * Starting from 'level' set all higher
 * levels to 'value' in the "raw" watermarks.
 */

static bool vlv_raw_plane_wm_set(struct intel_crtc_state *crtc_state,
     int level, enum plane_id plane_id, u16 value)
{
 struct intel_display *display = to_intel_display(crtc_state);
 bool dirty = false;

 for (; level < display->wm.num_levels; level++) {
  struct g4x_pipe_wm *raw = &crtc_state->wm.vlv.raw[level];

  dirty |= raw->plane[plane_id] != value;
  raw->plane[plane_id] = value;
 }

 return dirty;
}

static bool vlv_raw_plane_wm_compute(struct intel_crtc_state *crtc_state,
         const struct intel_plane_state *plane_state)
{
 struct intel_display *display = to_intel_display(crtc_state);
 struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
 enum plane_id plane_id = plane->id;
 int level;
 bool dirty = false;

 if (!intel_wm_plane_visible(crtc_state, plane_state)) {
  dirty |= vlv_raw_plane_wm_set(crtc_state, 0, plane_id, 0);
  goto out;
 }

 for (level = 0; level < display->wm.num_levels; level++) {
  struct g4x_pipe_wm *raw = &crtc_state->wm.vlv.raw[level];
  int wm = vlv_compute_wm_level(crtc_state, plane_state, level);
  int max_wm = plane_id == PLANE_CURSOR ? 63 : 511;

  if (wm > max_wm)
   break;

  dirty |= raw->plane[plane_id] != wm;
  raw->plane[plane_id] = wm;
 }

 /* mark all higher levels as invalid */
 dirty |= vlv_raw_plane_wm_set(crtc_state, level, plane_id, USHRT_MAX);

out:
 if (dirty)
  drm_dbg_kms(display->drm,
       "%s watermarks: PM2=%d, PM5=%d, DDR DVFS=%d\n",
       plane->base.name,
       crtc_state->wm.vlv.raw[VLV_WM_LEVEL_PM2].plane[plane_id],
       crtc_state->wm.vlv.raw[VLV_WM_LEVEL_PM5].plane[plane_id],
       crtc_state->wm.vlv.raw[VLV_WM_LEVEL_DDR_DVFS].plane[plane_id]);

 return dirty;
}

static bool vlv_raw_plane_wm_is_valid(const struct intel_crtc_state *crtc_state,
          enum plane_id plane_id, int level)
{
 const struct g4x_pipe_wm *raw =
  &crtc_state->wm.vlv.raw[level];
 const struct vlv_fifo_state *fifo_state =
  &crtc_state->wm.vlv.fifo_state;

 return raw->plane[plane_id] <= fifo_state->plane[plane_id];
}

static bool vlv_raw_crtc_wm_is_valid(const struct intel_crtc_state *crtc_state, int level)
{
 return vlv_raw_plane_wm_is_valid(crtc_state, PLANE_PRIMARY, level) &&
  vlv_raw_plane_wm_is_valid(crtc_state, PLANE_SPRITE0, level) &&
  vlv_raw_plane_wm_is_valid(crtc_state, PLANE_SPRITE1, level) &&
  vlv_raw_plane_wm_is_valid(crtc_state, PLANE_CURSOR, level);
}

static int _vlv_compute_pipe_wm(struct intel_crtc_state *crtc_state)
{
 struct intel_display *display = to_intel_display(crtc_state);
 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
 struct vlv_wm_state *wm_state = &crtc_state->wm.vlv.optimal;
 const struct vlv_fifo_state *fifo_state =
  &crtc_state->wm.vlv.fifo_state;
 u8 active_planes = crtc_state->active_planes & ~BIT(PLANE_CURSOR);
 int num_active_planes = hweight8(active_planes);
 enum plane_id plane_id;
 int level;

 /* initially allow all levels */
 wm_state->num_levels = display->wm.num_levels;
 /*
 * Note that enabling cxsr with no primary/sprite planes
 * enabled can wedge the pipe. Hence we only allow cxsr
 * with exactly one enabled primary/sprite plane.
 */

 wm_state->cxsr = crtc->pipe != PIPE_C && num_active_planes == 1;

 for (level = 0; level < wm_state->num_levels; level++) {
  const struct g4x_pipe_wm *raw = &crtc_state->wm.vlv.raw[level];
  const int sr_fifo_size = INTEL_NUM_PIPES(display) * 512 - 1;

  if (!vlv_raw_crtc_wm_is_valid(crtc_state, level))
   break;

  for_each_plane_id_on_crtc(crtc, plane_id) {
   wm_state->wm[level].plane[plane_id] =
    vlv_invert_wm_value(raw->plane[plane_id],
          fifo_state->plane[plane_id]);
  }

  wm_state->sr[level].plane =
   vlv_invert_wm_value(max3(raw->plane[PLANE_PRIMARY],
       raw->plane[PLANE_SPRITE0],
       raw->plane[PLANE_SPRITE1]),
         sr_fifo_size);

  wm_state->sr[level].cursor =
   vlv_invert_wm_value(raw->plane[PLANE_CURSOR],
         63);
 }

 if (level == 0)
  return -EINVAL;

 /* limit to only levels we can actually handle */
 wm_state->num_levels = level;

 /* invalidate the higher levels */
 vlv_invalidate_wms(crtc, wm_state, level);

 return 0;
}

static int vlv_compute_pipe_wm(struct intel_atomic_state *state,
          struct intel_crtc *crtc)
{
 struct intel_crtc_state *crtc_state =
  intel_atomic_get_new_crtc_state(state, crtc);
 const struct intel_plane_state *old_plane_state;
 const struct intel_plane_state *new_plane_state;
 struct intel_plane *plane;
 unsigned int dirty = 0;
 int i;

 for_each_oldnew_intel_plane_in_state(state, plane,
          old_plane_state,
          new_plane_state, i) {
  if (new_plane_state->hw.crtc != &crtc->base &&
      old_plane_state->hw.crtc != &crtc->base)
   continue;

  if (vlv_raw_plane_wm_compute(crtc_state, new_plane_state))
   dirty |= BIT(plane->id);
 }

 /*
 * DSPARB registers may have been reset due to the
 * power well being turned off. Make sure we restore
 * them to a consistent state even if no primary/sprite
 * planes are initially active. We also force a FIFO
 * recomputation so that we are sure to sanitize the
 * FIFO setting we took over from the BIOS even if there
 * are no active planes on the crtc.
 */

 if (intel_crtc_needs_modeset(crtc_state))
  dirty = ~0;

 if (!dirty)
  return 0;

 /* cursor changes don't warrant a FIFO recompute */
 if (dirty & ~BIT(PLANE_CURSOR)) {
  const struct intel_crtc_state *old_crtc_state =
   intel_atomic_get_old_crtc_state(state, crtc);
  const struct vlv_fifo_state *old_fifo_state =
   &old_crtc_state->wm.vlv.fifo_state;
  const struct vlv_fifo_state *new_fifo_state =
   &crtc_state->wm.vlv.fifo_state;
  int ret;

  ret = vlv_compute_fifo(crtc_state);
  if (ret)
   return ret;

  if (intel_crtc_needs_modeset(crtc_state) ||
      memcmp(old_fifo_state, new_fifo_state,
      sizeof(*new_fifo_state)) != 0)
   crtc_state->fifo_changed = true;
 }

 return _vlv_compute_pipe_wm(crtc_state);
}

#define VLV_FIFO(plane, value) \
 (((value) << DSPARB_ ## plane ## _SHIFT_VLV) & DSPARB_ ## plane ## _MASK_VLV)

static void vlv_atomic_update_fifo(struct intel_atomic_state *state,
       struct intel_crtc *crtc)
{
 struct intel_display *display = to_intel_display(crtc);
 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
 struct intel_uncore *uncore = &dev_priv->uncore;
 const struct intel_crtc_state *crtc_state =
  intel_atomic_get_new_crtc_state(state, crtc);
 const struct vlv_fifo_state *fifo_state =
  &crtc_state->wm.vlv.fifo_state;
 int sprite0_start, sprite1_start, fifo_size;
 u32 dsparb, dsparb2, dsparb3;

 if (!crtc_state->fifo_changed)
  return;

 sprite0_start = fifo_state->plane[PLANE_PRIMARY];
 sprite1_start = fifo_state->plane[PLANE_SPRITE0] + sprite0_start;
 fifo_size = fifo_state->plane[PLANE_SPRITE1] + sprite1_start;

 drm_WARN_ON(display->drm, fifo_state->plane[PLANE_CURSOR] != 63);
 drm_WARN_ON(display->drm, fifo_size != 511);

 trace_vlv_fifo_size(crtc, sprite0_start, sprite1_start, fifo_size);

 /*
 * uncore.lock serves a double purpose here. It allows us to
 * use the less expensive I915_{READ,WRITE}_FW() functions, and
 * it protects the DSPARB registers from getting clobbered by
 * parallel updates from multiple pipes.
 *
 * intel_pipe_update_start() has already disabled interrupts
 * for us, so a plain spin_lock() is sufficient here.
 */

 spin_lock(&uncore->lock);

 switch (crtc->pipe) {
 case PIPE_A:
  dsparb = intel_de_read_fw(display, DSPARB(display));
  dsparb2 = intel_de_read_fw(display, DSPARB2);

  dsparb &= ~(VLV_FIFO(SPRITEA, 0xff) |
       VLV_FIFO(SPRITEB, 0xff));
  dsparb |= (VLV_FIFO(SPRITEA, sprite0_start) |
      VLV_FIFO(SPRITEB, sprite1_start));

  dsparb2 &= ~(VLV_FIFO(SPRITEA_HI, 0x1) |
        VLV_FIFO(SPRITEB_HI, 0x1));
  dsparb2 |= (VLV_FIFO(SPRITEA_HI, sprite0_start >> 8) |
      VLV_FIFO(SPRITEB_HI, sprite1_start >> 8));

  intel_de_write_fw(display, DSPARB(display), dsparb);
  intel_de_write_fw(display, DSPARB2, dsparb2);
  break;
 case PIPE_B:
  dsparb = intel_de_read_fw(display, DSPARB(display));
  dsparb2 = intel_de_read_fw(display, DSPARB2);

  dsparb &= ~(VLV_FIFO(SPRITEC, 0xff) |
       VLV_FIFO(SPRITED, 0xff));
  dsparb |= (VLV_FIFO(SPRITEC, sprite0_start) |
      VLV_FIFO(SPRITED, sprite1_start));

  dsparb2 &= ~(VLV_FIFO(SPRITEC_HI, 0xff) |
        VLV_FIFO(SPRITED_HI, 0xff));
  dsparb2 |= (VLV_FIFO(SPRITEC_HI, sprite0_start >> 8) |
      VLV_FIFO(SPRITED_HI, sprite1_start >> 8));

  intel_de_write_fw(display, DSPARB(display), dsparb);
  intel_de_write_fw(display, DSPARB2, dsparb2);
  break;
 case PIPE_C:
  dsparb3 = intel_de_read_fw(display, DSPARB3);
  dsparb2 = intel_de_read_fw(display, DSPARB2);

  dsparb3 &= ~(VLV_FIFO(SPRITEE, 0xff) |
        VLV_FIFO(SPRITEF, 0xff));
  dsparb3 |= (VLV_FIFO(SPRITEE, sprite0_start) |
       VLV_FIFO(SPRITEF, sprite1_start));

  dsparb2 &= ~(VLV_FIFO(SPRITEE_HI, 0xff) |
        VLV_FIFO(SPRITEF_HI, 0xff));
  dsparb2 |= (VLV_FIFO(SPRITEE_HI, sprite0_start >> 8) |
      VLV_FIFO(SPRITEF_HI, sprite1_start >> 8));

  intel_de_write_fw(display, DSPARB3, dsparb3);
  intel_de_write_fw(display, DSPARB2, dsparb2);
  break;
 default:
  break;
 }

 intel_de_read_fw(display, DSPARB(display));

 spin_unlock(&uncore->lock);
}

#undef VLV_FIFO

static int vlv_compute_intermediate_wm(struct intel_atomic_state *state,
           struct intel_crtc *crtc)
{
 struct intel_crtc_state *new_crtc_state =
  intel_atomic_get_new_crtc_state(state, crtc);
 const struct intel_crtc_state *old_crtc_state =
  intel_atomic_get_old_crtc_state(state, crtc);
 struct vlv_wm_state *intermediate = &new_crtc_state->wm.vlv.intermediate;
 const struct vlv_wm_state *optimal = &new_crtc_state->wm.vlv.optimal;
 const struct vlv_wm_state *active = &old_crtc_state->wm.vlv.optimal;
 int level;

 if (!new_crtc_state->hw.active ||
     intel_crtc_needs_modeset(new_crtc_state)) {
  *intermediate = *optimal;

  intermediate->cxsr = false;
  goto out;
 }

 intermediate->num_levels = min(optimal->num_levels, active->num_levels);
 intermediate->cxsr = optimal->cxsr && active->cxsr &&
  !new_crtc_state->disable_cxsr;

 for (level = 0; level < intermediate->num_levels; level++) {
  enum plane_id plane_id;

  for_each_plane_id_on_crtc(crtc, plane_id) {
   intermediate->wm[level].plane[plane_id] =
    min(optimal->wm[level].plane[plane_id],
        active->wm[level].plane[plane_id]);
  }

  intermediate->sr[level].plane = min(optimal->sr[level].plane,
          active->sr[level].plane);
  intermediate->sr[level].cursor = min(optimal->sr[level].cursor,
           active->sr[level].cursor);
 }

 vlv_invalidate_wms(crtc, intermediate, level);

out:
 /*
 * If our intermediate WM are identical to the final WM, then we can
 * omit the post-vblank programming; only update if it's different.
 */

 if (memcmp(intermediate, optimal, sizeof(*intermediate)) != 0)
  new_crtc_state->wm.need_postvbl_update = true;

 return 0;
}

static int vlv_compute_watermarks(struct intel_atomic_state *state,
      struct intel_crtc *crtc)
{
 int ret;

 ret = vlv_compute_pipe_wm(state, crtc);
 if (ret)
  return ret;

 ret = vlv_compute_intermediate_wm(state, crtc);
 if (ret)
  return ret;

 return 0;
}

static void vlv_merge_wm(struct intel_display *display,
    struct vlv_wm_values *wm)
{
 struct intel_crtc *crtc;
 int num_active_pipes = 0;

 wm->level = display->wm.num_levels - 1;
 wm->cxsr = true;

 for_each_intel_crtc(display->drm, crtc) {
  const struct vlv_wm_state *wm_state = &crtc->wm.active.vlv;

  if (!crtc->active)
   continue;

  if (!wm_state->cxsr)
   wm->cxsr = false;

  num_active_pipes++;
  wm->level = min_t(int, wm->level, wm_state->num_levels - 1);
 }

 if (num_active_pipes != 1)
  wm->cxsr = false;

 if (num_active_pipes > 1)
  wm->level = VLV_WM_LEVEL_PM2;

 for_each_intel_crtc(display->drm, crtc) {
  const struct vlv_wm_state *wm_state = &crtc->wm.active.vlv;
  enum pipe pipe = crtc->pipe;

  wm->pipe[pipe] = wm_state->wm[wm->level];
  if (crtc->active && wm->cxsr)
   wm->sr = wm_state->sr[wm->level];

  wm->ddl[pipe].plane[PLANE_PRIMARY] = DDL_PRECISION_HIGH | 2;
  wm->ddl[pipe].plane[PLANE_SPRITE0] = DDL_PRECISION_HIGH | 2;
  wm->ddl[pipe].plane[PLANE_SPRITE1] = DDL_PRECISION_HIGH | 2;
  wm->ddl[pipe].plane[PLANE_CURSOR] = DDL_PRECISION_HIGH | 2;
 }
}

static void vlv_program_watermarks(struct intel_display *display)
{
 struct vlv_wm_values *old_wm = &display->wm.vlv;
 struct vlv_wm_values new_wm = {};

 vlv_merge_wm(display, &new_wm);

 if (memcmp(old_wm, &new_wm, sizeof(new_wm)) == 0)
  return;

 if (is_disabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_DDR_DVFS))
  chv_set_memory_dvfs(display, false);

 if (is_disabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_PM5))
  chv_set_memory_pm5(display, false);

 if (is_disabling(old_wm->cxsr, new_wm.cxsr, true))
  _intel_set_memory_cxsr(display, false);

 vlv_write_wm_values(display, &new_wm);

 if (is_enabling(old_wm->cxsr, new_wm.cxsr, true))
  _intel_set_memory_cxsr(display, true);

 if (is_enabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_PM5))
  chv_set_memory_pm5(display, true);

 if (is_enabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_DDR_DVFS))
  chv_set_memory_dvfs(display, true);

 *old_wm = new_wm;
}

static void vlv_initial_watermarks(struct intel_atomic_state *state,
       struct intel_crtc *crtc)
{
 struct intel_display *display = to_intel_display(crtc);
 const struct intel_crtc_state *crtc_state =
  intel_atomic_get_new_crtc_state(state, crtc);

 mutex_lock(&display->wm.wm_mutex);
 crtc->wm.active.vlv = crtc_state->wm.vlv.intermediate;
 vlv_program_watermarks(display);
 mutex_unlock(&display->wm.wm_mutex);
}

static void vlv_optimize_watermarks(struct intel_atomic_state *state,
        struct intel_crtc *crtc)
{
 struct intel_display *display = to_intel_display(crtc);
 const struct intel_crtc_state *crtc_state =
  intel_atomic_get_new_crtc_state(state, crtc);

 if (!crtc_state->wm.need_postvbl_update)
  return;

 mutex_lock(&display->wm.wm_mutex);
 crtc->wm.active.vlv = crtc_state->wm.vlv.optimal;
 vlv_program_watermarks(display);
 mutex_unlock(&display->wm.wm_mutex);
}

static void i965_update_wm(struct intel_display *display)
{
 struct intel_crtc *crtc;
 int srwm = 1;
 int cursor_sr = 16;
 bool cxsr_enabled;

 /* Calc sr entries for one plane configs */
 crtc = single_enabled_crtc(display);
 if (crtc) {
  /* self-refresh has much higher latency */
  static const int sr_latency_ns = 12000;
  const struct drm_display_mode *pipe_mode =
   &crtc->config->hw.pipe_mode;
  const struct drm_framebuffer *fb =
--> --------------------

--> maximum size reached

--> --------------------

Messung V0.5
C=95 H=96 G=95

¤ Dauer der Verarbeitung: 0.18 Sekunden  ¤

*© Formatika GbR, Deutschland






Wurzel

Suchen

Beweissystem der NASA

Beweissystem Isabelle

NIST Cobol Testsuite

Cephes Mathematical Library

Wiener Entwicklungsmethode

Haftungshinweis

Die Informationen auf dieser Webseite wurden nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit, noch Qualität der bereit gestellten Informationen zugesichert.

Bemerkung:

Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.