[PATCH v4 2/3] drm/atomic-helper: Implement subsystem-level suspend/resume

2015-12-01 Thread Daniel Vetter
On Tue, Dec 01, 2015 at 10:57:00AM +0100, Thierry Reding wrote:
> From: Thierry Reding 
> 
> Provide subsystem-level suspend and resume helpers that can be used to
> implement suspend/resume on atomic mode-setting enabled drivers.
> 
> v2: simplify locking, enhance kerneldoc comments
> v3: pass lock acquisition context by parameter, improve kerneldoc
> v4: - remove redundant code (already provided by atomic helpers)
>   (Maarten Lankhorst)
> - move backoff dance from drm_modeset_lock_all_ctx() into suspend
>   helper (Daniel Vetter)
> 
> Signed-off-by: Thierry Reding 
> ---
>  drivers/gpu/drm/drm_atomic_helper.c | 163 
> +++-
>  drivers/gpu/drm/drm_crtc_helper.c   |   6 ++
>  include/drm/drm_atomic_helper.h |   6 ++
>  3 files changed, 174 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/gpu/drm/drm_atomic_helper.c 
> b/drivers/gpu/drm/drm_atomic_helper.c
> index 3731a26979bc..1b51722b2a00 100644
> --- a/drivers/gpu/drm/drm_atomic_helper.c
> +++ b/drivers/gpu/drm/drm_atomic_helper.c
> @@ -1818,6 +1818,162 @@ commit:
>  }
>  
>  /**
> + * drm_atomic_helper_disable_all - disable all currently active outputs
> + * @dev: DRM device
> + * @ctx: lock acquisition context
> + *
> + * Loops through all connectors, finding those that aren't turned off and 
> then
> + * turns them off by setting their DPMS mode to OFF and deactivating the CRTC
> + * that they are connected to.
> + *
> + * This is used for example in suspend/resume to disable all currently active
> + * functions when suspending.
> + *
> + * Note that if callers haven't already acquired all modeset locks this might
> + * return -EDEADLK, which must be handled by calling drm_modeset_backoff().
> + *
> + * Returns:
> + * 0 on success or a negative error code on failure.
> + *
> + * See also:
> + * drm_atomic_helper_suspend(), drm_atomic_helper_resume()
> + */
> +int drm_atomic_helper_disable_all(struct drm_device *dev,
> +   struct drm_modeset_acquire_ctx *ctx)
> +{
> + struct drm_atomic_state *state;
> + struct drm_connector *conn;
> + int err;
> +
> + state = drm_atomic_state_alloc(dev);
> + if (!state)
> + return -ENOMEM;
> +
> + state->acquire_ctx = ctx;
> +
> + drm_for_each_connector(conn, dev) {
> + struct drm_crtc *crtc = conn->state->crtc;
> + struct drm_crtc_state *crtc_state;
> +
> + if (!crtc || conn->dpms != DRM_MODE_DPMS_ON)
> + continue;
> +
> + crtc_state = drm_atomic_get_crtc_state(state, crtc);
> + if (IS_ERR(crtc_state)) {
> + err = PTR_ERR(crtc_state);
> + goto free;
> + }
> +
> + crtc_state->active = false;
> + }
> +
> + err = drm_atomic_commit(state);
> +
> +free:
> + if (err < 0)
> + drm_atomic_state_free(state);
> +
> + return err;
> +}
> +EXPORT_SYMBOL(drm_atomic_helper_disable_all);
> +
> +/**
> + * drm_atomic_helper_suspend - subsystem-level suspend helper
> + * @dev: DRM device
> + *
> + * Duplicates the current atomic state, disables all active outputs and then
> + * returns a pointer to the original atomic state to the caller. Drivers can
> + * pass this pointer to the drm_atomic_helper_resume() helper upon resume to
> + * restore the output configuration that was active at the time the system
> + * entered suspend.
> + *
> + * Note that it is potentially unsafe to use this. The atomic state object
> + * returned by this function is assumed to be persistent. Drivers must ensure
> + * that this holds true. Before calling this function, drivers must make sure
> + * to suspend fbdev emulation so that nothing can be using the device.
> + *
> + * Returns:
> + * A pointer to a copy of the state before suspend on success or an 
> ERR_PTR()-
> + * encoded error code on failure. Drivers should store the returned atomic
> + * state object and pass it to the drm_atomic_helper_resume() helper upon
> + * resume.
> + *
> + * See also:
> + * drm_atomic_helper_duplicate_state(), drm_atomic_helper_disable_all(),
> + * drm_atomic_helper_resume()
> + */
> +struct drm_atomic_state *drm_atomic_helper_suspend(struct drm_device *dev)
> +{
> + struct drm_modeset_acquire_ctx ctx;
> + struct drm_atomic_state *state;
> + int err;
> +
> + drm_modeset_acquire_init(, 0);
> +
> +retry:
> + err = drm_modeset_lock_all_ctx(dev, );
> + if (err < 0) {
> + if (err == -EDEADLK) {
> + drm_modeset_backoff();
> + goto retry;
> + }
> +
> + state = ERR_PTR(err);
> + goto fini;
> + }
> +
> + state = drm_atomic_helper_duplicate_state(dev, );
> + if (IS_ERR(state))
> + goto unlock;
> +
> + err = drm_atomic_helper_disable_all(dev, );

If the driver has some internal state protected with it's own locking the
above two function might return 

[PATCH v4 2/3] drm/atomic-helper: Implement subsystem-level suspend/resume

2015-12-01 Thread Thierry Reding
From: Thierry Reding 

Provide subsystem-level suspend and resume helpers that can be used to
implement suspend/resume on atomic mode-setting enabled drivers.

v2: simplify locking, enhance kerneldoc comments
v3: pass lock acquisition context by parameter, improve kerneldoc
v4: - remove redundant code (already provided by atomic helpers)
  (Maarten Lankhorst)
- move backoff dance from drm_modeset_lock_all_ctx() into suspend
  helper (Daniel Vetter)

Signed-off-by: Thierry Reding 
---
 drivers/gpu/drm/drm_atomic_helper.c | 163 +++-
 drivers/gpu/drm/drm_crtc_helper.c   |   6 ++
 include/drm/drm_atomic_helper.h |   6 ++
 3 files changed, 174 insertions(+), 1 deletion(-)

diff --git a/drivers/gpu/drm/drm_atomic_helper.c 
b/drivers/gpu/drm/drm_atomic_helper.c
index 3731a26979bc..1b51722b2a00 100644
--- a/drivers/gpu/drm/drm_atomic_helper.c
+++ b/drivers/gpu/drm/drm_atomic_helper.c
@@ -1818,6 +1818,162 @@ commit:
 }

 /**
+ * drm_atomic_helper_disable_all - disable all currently active outputs
+ * @dev: DRM device
+ * @ctx: lock acquisition context
+ *
+ * Loops through all connectors, finding those that aren't turned off and then
+ * turns them off by setting their DPMS mode to OFF and deactivating the CRTC
+ * that they are connected to.
+ *
+ * This is used for example in suspend/resume to disable all currently active
+ * functions when suspending.
+ *
+ * Note that if callers haven't already acquired all modeset locks this might
+ * return -EDEADLK, which must be handled by calling drm_modeset_backoff().
+ *
+ * Returns:
+ * 0 on success or a negative error code on failure.
+ *
+ * See also:
+ * drm_atomic_helper_suspend(), drm_atomic_helper_resume()
+ */
+int drm_atomic_helper_disable_all(struct drm_device *dev,
+ struct drm_modeset_acquire_ctx *ctx)
+{
+   struct drm_atomic_state *state;
+   struct drm_connector *conn;
+   int err;
+
+   state = drm_atomic_state_alloc(dev);
+   if (!state)
+   return -ENOMEM;
+
+   state->acquire_ctx = ctx;
+
+   drm_for_each_connector(conn, dev) {
+   struct drm_crtc *crtc = conn->state->crtc;
+   struct drm_crtc_state *crtc_state;
+
+   if (!crtc || conn->dpms != DRM_MODE_DPMS_ON)
+   continue;
+
+   crtc_state = drm_atomic_get_crtc_state(state, crtc);
+   if (IS_ERR(crtc_state)) {
+   err = PTR_ERR(crtc_state);
+   goto free;
+   }
+
+   crtc_state->active = false;
+   }
+
+   err = drm_atomic_commit(state);
+
+free:
+   if (err < 0)
+   drm_atomic_state_free(state);
+
+   return err;
+}
+EXPORT_SYMBOL(drm_atomic_helper_disable_all);
+
+/**
+ * drm_atomic_helper_suspend - subsystem-level suspend helper
+ * @dev: DRM device
+ *
+ * Duplicates the current atomic state, disables all active outputs and then
+ * returns a pointer to the original atomic state to the caller. Drivers can
+ * pass this pointer to the drm_atomic_helper_resume() helper upon resume to
+ * restore the output configuration that was active at the time the system
+ * entered suspend.
+ *
+ * Note that it is potentially unsafe to use this. The atomic state object
+ * returned by this function is assumed to be persistent. Drivers must ensure
+ * that this holds true. Before calling this function, drivers must make sure
+ * to suspend fbdev emulation so that nothing can be using the device.
+ *
+ * Returns:
+ * A pointer to a copy of the state before suspend on success or an ERR_PTR()-
+ * encoded error code on failure. Drivers should store the returned atomic
+ * state object and pass it to the drm_atomic_helper_resume() helper upon
+ * resume.
+ *
+ * See also:
+ * drm_atomic_helper_duplicate_state(), drm_atomic_helper_disable_all(),
+ * drm_atomic_helper_resume()
+ */
+struct drm_atomic_state *drm_atomic_helper_suspend(struct drm_device *dev)
+{
+   struct drm_modeset_acquire_ctx ctx;
+   struct drm_atomic_state *state;
+   int err;
+
+   drm_modeset_acquire_init(, 0);
+
+retry:
+   err = drm_modeset_lock_all_ctx(dev, );
+   if (err < 0) {
+   if (err == -EDEADLK) {
+   drm_modeset_backoff();
+   goto retry;
+   }
+
+   state = ERR_PTR(err);
+   goto fini;
+   }
+
+   state = drm_atomic_helper_duplicate_state(dev, );
+   if (IS_ERR(state))
+   goto unlock;
+
+   err = drm_atomic_helper_disable_all(dev, );
+   if (err < 0) {
+   drm_atomic_state_free(state);
+   state = ERR_PTR(err);
+   goto unlock;
+   }
+
+unlock:
+   drm_modeset_drop_locks();
+fini:
+   drm_modeset_acquire_fini();
+   return state;
+}
+EXPORT_SYMBOL(drm_atomic_helper_suspend);
+
+/**
+ * drm_atomic_helper_resume - subsystem-level