drm/i915: add the FBC mutex

Make sure we're not going to have weird races in really weird cases
where a lot of different CRTCs are doing rendering and modesets at the
same time.

With this change and the stolen_lock from the previous patch, we can
start removing the struct_mutex locking we have around FBC in the next
patches.

v2:
 - Rebase (6 months later)
 - Also lock debugfs and stolen.
v3:
 - Don't lock a single value read (Chris).
 - Replace lockdep assertions with WARNs (Daniel).
 - Improve commit message.
 - Don't forget intel_pre_plane_update() locking.
v4:
 - Don't remove struct_mutex at intel_pre_plane_update() (Chris).
 - Add comment regarding locking dependencies (Chris).
 - Rebase after the stolen code rework.
 - Rebase again after drm-intel-nightly changes.
v5:
 - Rebase after the new stolen_lock patch.

Reviewed-by: Chris Wilson <chris@chris-wilson.co.uk> (v4)
Signed-off-by: Paulo Zanoni <paulo.r.zanoni@intel.com>
Signed-off-by: Daniel Vetter <daniel.vetter@ffwll.ch>
This commit is contained in:
Paulo Zanoni 2015-07-02 19:25:10 -03:00 committed by Daniel Vetter
parent 92e97d2f47
commit 25ad93fd9f
5 changed files with 96 additions and 19 deletions

View File

@ -1633,6 +1633,7 @@ static int i915_fbc_status(struct seq_file *m, void *unused)
} }
intel_runtime_pm_get(dev_priv); intel_runtime_pm_get(dev_priv);
mutex_lock(&dev_priv->fbc.lock);
if (intel_fbc_enabled(dev)) if (intel_fbc_enabled(dev))
seq_puts(m, "FBC enabled\n"); seq_puts(m, "FBC enabled\n");
@ -1645,6 +1646,7 @@ static int i915_fbc_status(struct seq_file *m, void *unused)
yesno(I915_READ(FBC_STATUS2) & yesno(I915_READ(FBC_STATUS2) &
FBC_COMPRESSION_MASK)); FBC_COMPRESSION_MASK));
mutex_unlock(&dev_priv->fbc.lock);
intel_runtime_pm_put(dev_priv); intel_runtime_pm_put(dev_priv);
return 0; return 0;
@ -1675,6 +1677,7 @@ static int i915_fbc_fc_set(void *data, u64 val)
return -ENODEV; return -ENODEV;
drm_modeset_lock_all(dev); drm_modeset_lock_all(dev);
mutex_lock(&dev_priv->fbc.lock);
reg = I915_READ(ILK_DPFC_CONTROL); reg = I915_READ(ILK_DPFC_CONTROL);
dev_priv->fbc.false_color = val; dev_priv->fbc.false_color = val;
@ -1683,6 +1686,7 @@ static int i915_fbc_fc_set(void *data, u64 val)
(reg | FBC_CTL_FALSE_COLOR) : (reg | FBC_CTL_FALSE_COLOR) :
(reg & ~FBC_CTL_FALSE_COLOR)); (reg & ~FBC_CTL_FALSE_COLOR));
mutex_unlock(&dev_priv->fbc.lock);
drm_modeset_unlock_all(dev); drm_modeset_unlock_all(dev);
return 0; return 0;
} }

View File

@ -899,6 +899,9 @@ enum fb_op_origin {
}; };
struct i915_fbc { struct i915_fbc {
/* This is always the inner lock when overlapping with struct_mutex and
* it's the outer lock when overlapping with stolen_lock. */
struct mutex lock;
unsigned long uncompressed_size; unsigned long uncompressed_size;
unsigned threshold; unsigned threshold;
unsigned int fb_id; unsigned int fb_id;

View File

@ -4783,11 +4783,9 @@ static void intel_pre_plane_update(struct intel_crtc *crtc)
if (atomic->wait_for_flips) if (atomic->wait_for_flips)
intel_crtc_wait_for_pending_flips(&crtc->base); intel_crtc_wait_for_pending_flips(&crtc->base);
if (atomic->disable_fbc && if (atomic->disable_fbc) {
dev_priv->fbc.crtc == crtc) {
mutex_lock(&dev->struct_mutex); mutex_lock(&dev->struct_mutex);
if (dev_priv->fbc.crtc == crtc) intel_fbc_disable_crtc(crtc);
intel_fbc_disable(dev);
mutex_unlock(&dev->struct_mutex); mutex_unlock(&dev->struct_mutex);
} }

View File

@ -1252,6 +1252,7 @@ bool intel_fbc_enabled(struct drm_device *dev);
void intel_fbc_update(struct drm_device *dev); void intel_fbc_update(struct drm_device *dev);
void intel_fbc_init(struct drm_i915_private *dev_priv); void intel_fbc_init(struct drm_i915_private *dev_priv);
void intel_fbc_disable(struct drm_device *dev); void intel_fbc_disable(struct drm_device *dev);
void intel_fbc_disable_crtc(struct intel_crtc *crtc);
void intel_fbc_invalidate(struct drm_i915_private *dev_priv, void intel_fbc_invalidate(struct drm_i915_private *dev_priv,
unsigned int frontbuffer_bits, unsigned int frontbuffer_bits,
enum fb_op_origin origin); enum fb_op_origin origin);

View File

@ -336,6 +336,7 @@ static void intel_fbc_work_fn(struct work_struct *__work)
struct drm_i915_private *dev_priv = dev->dev_private; struct drm_i915_private *dev_priv = dev->dev_private;
mutex_lock(&dev->struct_mutex); mutex_lock(&dev->struct_mutex);
mutex_lock(&dev_priv->fbc.lock);
if (work == dev_priv->fbc.fbc_work) { if (work == dev_priv->fbc.fbc_work) {
/* Double check that we haven't switched fb without cancelling /* Double check that we haven't switched fb without cancelling
* the prior work. * the prior work.
@ -350,6 +351,7 @@ static void intel_fbc_work_fn(struct work_struct *__work)
dev_priv->fbc.fbc_work = NULL; dev_priv->fbc.fbc_work = NULL;
} }
mutex_unlock(&dev_priv->fbc.lock);
mutex_unlock(&dev->struct_mutex); mutex_unlock(&dev->struct_mutex);
kfree(work); kfree(work);
@ -357,6 +359,8 @@ static void intel_fbc_work_fn(struct work_struct *__work)
static void intel_fbc_cancel_work(struct drm_i915_private *dev_priv) static void intel_fbc_cancel_work(struct drm_i915_private *dev_priv)
{ {
WARN_ON(!mutex_is_locked(&dev_priv->fbc.lock));
if (dev_priv->fbc.fbc_work == NULL) if (dev_priv->fbc.fbc_work == NULL)
return; return;
@ -384,6 +388,8 @@ static void intel_fbc_enable(struct drm_crtc *crtc)
struct drm_device *dev = crtc->dev; struct drm_device *dev = crtc->dev;
struct drm_i915_private *dev_priv = dev->dev_private; struct drm_i915_private *dev_priv = dev->dev_private;
WARN_ON(!mutex_is_locked(&dev_priv->fbc.lock));
if (!dev_priv->display.enable_fbc) if (!dev_priv->display.enable_fbc)
return; return;
@ -418,6 +424,21 @@ static void intel_fbc_enable(struct drm_crtc *crtc)
schedule_delayed_work(&work->work, msecs_to_jiffies(50)); schedule_delayed_work(&work->work, msecs_to_jiffies(50));
} }
static void __intel_fbc_disable(struct drm_device *dev)
{
struct drm_i915_private *dev_priv = dev->dev_private;
WARN_ON(!mutex_is_locked(&dev_priv->fbc.lock));
intel_fbc_cancel_work(dev_priv);
if (!dev_priv->display.disable_fbc)
return;
dev_priv->display.disable_fbc(dev);
dev_priv->fbc.crtc = NULL;
}
/** /**
* intel_fbc_disable - disable FBC * intel_fbc_disable - disable FBC
* @dev: the drm_device * @dev: the drm_device
@ -428,13 +449,26 @@ void intel_fbc_disable(struct drm_device *dev)
{ {
struct drm_i915_private *dev_priv = dev->dev_private; struct drm_i915_private *dev_priv = dev->dev_private;
intel_fbc_cancel_work(dev_priv); mutex_lock(&dev_priv->fbc.lock);
__intel_fbc_disable(dev);
mutex_unlock(&dev_priv->fbc.lock);
}
if (!dev_priv->display.disable_fbc) /*
return; * intel_fbc_disable_crtc - disable FBC if it's associated with crtc
* @crtc: the CRTC
*
* This function disables FBC if it's associated with the provided CRTC.
*/
void intel_fbc_disable_crtc(struct intel_crtc *crtc)
{
struct drm_device *dev = crtc->base.dev;
struct drm_i915_private *dev_priv = dev->dev_private;
dev_priv->display.disable_fbc(dev); mutex_lock(&dev_priv->fbc.lock);
dev_priv->fbc.crtc = NULL; if (dev_priv->fbc.crtc == crtc)
__intel_fbc_disable(dev);
mutex_unlock(&dev_priv->fbc.lock);
} }
const char *intel_no_fbc_reason_str(enum no_fbc_reason reason) const char *intel_no_fbc_reason_str(enum no_fbc_reason reason)
@ -607,7 +641,7 @@ static int intel_fbc_alloc_cfb(struct drm_device *dev, int size, int fb_cpp)
return -ENOSPC; return -ENOSPC;
} }
void intel_fbc_cleanup_cfb(struct drm_device *dev) static void __intel_fbc_cleanup_cfb(struct drm_device *dev)
{ {
struct drm_i915_private *dev_priv = dev->dev_private; struct drm_i915_private *dev_priv = dev->dev_private;
@ -625,6 +659,15 @@ void intel_fbc_cleanup_cfb(struct drm_device *dev)
dev_priv->fbc.uncompressed_size = 0; dev_priv->fbc.uncompressed_size = 0;
} }
void intel_fbc_cleanup_cfb(struct drm_device *dev)
{
struct drm_i915_private *dev_priv = dev->dev_private;
mutex_lock(&dev_priv->fbc.lock);
__intel_fbc_cleanup_cfb(dev);
mutex_unlock(&dev_priv->fbc.lock);
}
static int intel_fbc_setup_cfb(struct drm_device *dev, int size, int fb_cpp) static int intel_fbc_setup_cfb(struct drm_device *dev, int size, int fb_cpp)
{ {
struct drm_i915_private *dev_priv = dev->dev_private; struct drm_i915_private *dev_priv = dev->dev_private;
@ -633,13 +676,13 @@ static int intel_fbc_setup_cfb(struct drm_device *dev, int size, int fb_cpp)
return 0; return 0;
/* Release any current block */ /* Release any current block */
intel_fbc_cleanup_cfb(dev); __intel_fbc_cleanup_cfb(dev);
return intel_fbc_alloc_cfb(dev, size, fb_cpp); return intel_fbc_alloc_cfb(dev, size, fb_cpp);
} }
/** /**
* intel_fbc_update - enable/disable FBC as needed * __intel_fbc_update - enable/disable FBC as needed, unlocked
* @dev: the drm_device * @dev: the drm_device
* *
* Set up the framebuffer compression hardware at mode set time. We * Set up the framebuffer compression hardware at mode set time. We
@ -657,7 +700,7 @@ static int intel_fbc_setup_cfb(struct drm_device *dev, int size, int fb_cpp)
* *
* We need to enable/disable FBC on a global basis. * We need to enable/disable FBC on a global basis.
*/ */
void intel_fbc_update(struct drm_device *dev) static void __intel_fbc_update(struct drm_device *dev)
{ {
struct drm_i915_private *dev_priv = dev->dev_private; struct drm_i915_private *dev_priv = dev->dev_private;
struct drm_crtc *crtc = NULL; struct drm_crtc *crtc = NULL;
@ -670,6 +713,8 @@ void intel_fbc_update(struct drm_device *dev)
if (!HAS_FBC(dev)) if (!HAS_FBC(dev))
return; return;
WARN_ON(!mutex_is_locked(&dev_priv->fbc.lock));
/* disable framebuffer compression in vGPU */ /* disable framebuffer compression in vGPU */
if (intel_vgpu_active(dev)) if (intel_vgpu_active(dev))
i915.enable_fbc = 0; i915.enable_fbc = 0;
@ -788,7 +833,7 @@ void intel_fbc_update(struct drm_device *dev)
* some point. And we wait before enabling FBC anyway. * some point. And we wait before enabling FBC anyway.
*/ */
DRM_DEBUG_KMS("disabling active FBC for update\n"); DRM_DEBUG_KMS("disabling active FBC for update\n");
intel_fbc_disable(dev); __intel_fbc_disable(dev);
} }
intel_fbc_enable(crtc); intel_fbc_enable(crtc);
@ -799,9 +844,24 @@ void intel_fbc_update(struct drm_device *dev)
/* Multiple disables should be harmless */ /* Multiple disables should be harmless */
if (intel_fbc_enabled(dev)) { if (intel_fbc_enabled(dev)) {
DRM_DEBUG_KMS("unsupported config, disabling FBC\n"); DRM_DEBUG_KMS("unsupported config, disabling FBC\n");
intel_fbc_disable(dev); __intel_fbc_disable(dev);
} }
intel_fbc_cleanup_cfb(dev); __intel_fbc_cleanup_cfb(dev);
}
/*
* intel_fbc_update - enable/disable FBC as needed
* @dev: the drm_device
*
* This function reevaluates the overall state and enables or disables FBC.
*/
void intel_fbc_update(struct drm_device *dev)
{
struct drm_i915_private *dev_priv = dev->dev_private;
mutex_lock(&dev_priv->fbc.lock);
__intel_fbc_update(dev);
mutex_unlock(&dev_priv->fbc.lock);
} }
void intel_fbc_invalidate(struct drm_i915_private *dev_priv, void intel_fbc_invalidate(struct drm_i915_private *dev_priv,
@ -814,6 +874,8 @@ void intel_fbc_invalidate(struct drm_i915_private *dev_priv,
if (origin == ORIGIN_GTT) if (origin == ORIGIN_GTT)
return; return;
mutex_lock(&dev_priv->fbc.lock);
if (dev_priv->fbc.enabled) if (dev_priv->fbc.enabled)
fbc_bits = INTEL_FRONTBUFFER_PRIMARY(dev_priv->fbc.crtc->pipe); fbc_bits = INTEL_FRONTBUFFER_PRIMARY(dev_priv->fbc.crtc->pipe);
else if (dev_priv->fbc.fbc_work) else if (dev_priv->fbc.fbc_work)
@ -825,7 +887,9 @@ void intel_fbc_invalidate(struct drm_i915_private *dev_priv,
dev_priv->fbc.busy_bits |= (fbc_bits & frontbuffer_bits); dev_priv->fbc.busy_bits |= (fbc_bits & frontbuffer_bits);
if (dev_priv->fbc.busy_bits) if (dev_priv->fbc.busy_bits)
intel_fbc_disable(dev); __intel_fbc_disable(dev);
mutex_unlock(&dev_priv->fbc.lock);
} }
void intel_fbc_flush(struct drm_i915_private *dev_priv, void intel_fbc_flush(struct drm_i915_private *dev_priv,
@ -833,13 +897,18 @@ void intel_fbc_flush(struct drm_i915_private *dev_priv,
{ {
struct drm_device *dev = dev_priv->dev; struct drm_device *dev = dev_priv->dev;
mutex_lock(&dev_priv->fbc.lock);
if (!dev_priv->fbc.busy_bits) if (!dev_priv->fbc.busy_bits)
return; goto out;
dev_priv->fbc.busy_bits &= ~frontbuffer_bits; dev_priv->fbc.busy_bits &= ~frontbuffer_bits;
if (!dev_priv->fbc.busy_bits) if (!dev_priv->fbc.busy_bits)
intel_fbc_update(dev); __intel_fbc_update(dev);
out:
mutex_unlock(&dev_priv->fbc.lock);
} }
/** /**
@ -852,6 +921,8 @@ void intel_fbc_init(struct drm_i915_private *dev_priv)
{ {
enum pipe pipe; enum pipe pipe;
mutex_init(&dev_priv->fbc.lock);
if (!HAS_FBC(dev_priv)) { if (!HAS_FBC(dev_priv)) {
dev_priv->fbc.enabled = false; dev_priv->fbc.enabled = false;
dev_priv->fbc.no_fbc_reason = FBC_UNSUPPORTED; dev_priv->fbc.no_fbc_reason = FBC_UNSUPPORTED;