All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 1/3] drm: atomic-helper: Implement drm_atomic_helper_duplicate_state()
@ 2015-08-11 11:37 Thierry Reding
  2015-08-11 11:37 ` [PATCH 2/3] drm/atomic-helper: Implement subsystem-level suspend/resume Thierry Reding
                   ` (4 more replies)
  0 siblings, 5 replies; 8+ messages in thread
From: Thierry Reding @ 2015-08-11 11:37 UTC (permalink / raw)
  To: dri-devel

From: Thierry Reding <treding@nvidia.com>

This function can be used to duplicate an atomic state object. This is
useful for example to implement suspend/resume, where the state before
suspend can be saved and restored upon resume.

Signed-off-by: Thierry Reding <treding@nvidia.com>
---
 drivers/gpu/drm/drm_atomic_helper.c | 90 +++++++++++++++++++++++++++++++++++++
 include/drm/drm_atomic_helper.h     |  2 +
 2 files changed, 92 insertions(+)

diff --git a/drivers/gpu/drm/drm_atomic_helper.c b/drivers/gpu/drm/drm_atomic_helper.c
index fd06595c2ee5..f842499ca79e 100644
--- a/drivers/gpu/drm/drm_atomic_helper.c
+++ b/drivers/gpu/drm/drm_atomic_helper.c
@@ -2358,6 +2358,96 @@ drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector)
 EXPORT_SYMBOL(drm_atomic_helper_connector_duplicate_state);
 
 /**
+ * drm_atomic_helper_duplicate_state - duplicate an atomic state object
+ * @dev: DRM device
+ *
+ * Makes a copy of the current atomic state by looping over all objects and
+ * duplicating their respective states.
+ *
+ * Returns: A pointer to the copy of the atomic state object on success or an
+ * ERR_PTR()-encoded error code on failure.
+ */
+struct drm_atomic_state *
+drm_atomic_helper_duplicate_state(struct drm_device *dev)
+{
+	struct drm_modeset_acquire_ctx ctx;
+	struct drm_atomic_state *state;
+	struct drm_connector *conn;
+	struct drm_plane *plane;
+	struct drm_crtc *crtc;
+	int err;
+
+	state = drm_atomic_state_alloc(dev);
+	if (!state)
+		return ERR_PTR(-ENOMEM);
+
+	drm_modeset_acquire_init(&ctx, 0);
+	state->acquire_ctx = &ctx;
+
+	err = drm_modeset_lock(&dev->mode_config.connection_mutex, &ctx);
+	if (err < 0)
+		goto free;
+
+	drm_for_each_crtc(crtc, dev) {
+		unsigned int index = drm_crtc_index(crtc);
+		struct drm_crtc_state *crtc_state;
+
+		crtc_state = crtc->funcs->atomic_duplicate_state(crtc);
+		if (!crtc_state) {
+			err = -ENOMEM;
+			goto unlock;
+		}
+
+		state->crtc_states[index] = crtc_state;
+		state->crtcs[index] = crtc;
+		crtc_state->state = state;
+	}
+
+	drm_for_each_plane(plane, dev) {
+		unsigned int index = drm_plane_index(plane);
+		struct drm_plane_state *plane_state;
+
+		plane_state = plane->funcs->atomic_duplicate_state(plane);
+		if (!plane_state) {
+			err = -ENOMEM;
+			goto unlock;
+		}
+
+		state->plane_states[index] = plane_state;
+		state->planes[index] = plane;
+		plane_state->state = state;
+	}
+
+	drm_for_each_connector(conn, dev) {
+		unsigned int index = drm_connector_index(conn);
+		struct drm_connector_state *conn_state;
+
+		conn_state = conn->funcs->atomic_duplicate_state(conn);
+		if (!conn_state) {
+			err = -ENOMEM;
+			goto unlock;
+		}
+
+		state->connector_states[index] = conn_state;
+		state->connectors[index] = conn;
+		conn_state->state = state;
+	}
+
+unlock:
+	drm_modeset_drop_locks(&ctx);
+free:
+	drm_modeset_acquire_fini(&ctx);
+
+	if (err < 0) {
+		drm_atomic_state_free(state);
+		state = ERR_PTR(err);
+	}
+
+	return state;
+}
+EXPORT_SYMBOL(drm_atomic_helper_duplicate_state);
+
+/**
  * __drm_atomic_helper_connector_destroy_state - release connector state
  * @connector: connector object
  * @state: connector state object to release
diff --git a/include/drm/drm_atomic_helper.h b/include/drm/drm_atomic_helper.h
index 4ffe9dca07c4..5d27612997ce 100644
--- a/include/drm/drm_atomic_helper.h
+++ b/include/drm/drm_atomic_helper.h
@@ -118,6 +118,8 @@ __drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector,
 					   struct drm_connector_state *state);
 struct drm_connector_state *
 drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector);
+struct drm_atomic_state *
+drm_atomic_helper_duplicate_state(struct drm_device *dev);
 void
 __drm_atomic_helper_connector_destroy_state(struct drm_connector *connector,
 					    struct drm_connector_state *state);
-- 
2.4.5

_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
http://lists.freedesktop.org/mailman/listinfo/dri-devel

^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [PATCH 2/3] drm/atomic-helper: Implement subsystem-level suspend/resume
  2015-08-11 11:37 [PATCH 1/3] drm: atomic-helper: Implement drm_atomic_helper_duplicate_state() Thierry Reding
@ 2015-08-11 11:37 ` Thierry Reding
  2015-08-11 11:55   ` Maarten Lankhorst
  2015-08-11 11:59   ` Daniel Vetter
  2015-08-11 11:37 ` [PATCH 3/3] drm/tegra: " Thierry Reding
                   ` (3 subsequent siblings)
  4 siblings, 2 replies; 8+ messages in thread
From: Thierry Reding @ 2015-08-11 11:37 UTC (permalink / raw)
  To: dri-devel

From: Thierry Reding <treding@nvidia.com>

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

Signed-off-by: Thierry Reding <treding@nvidia.com>
---
 drivers/gpu/drm/drm_atomic_helper.c | 191 ++++++++++++++++++++++++++++++++++++
 include/drm/drm_atomic_helper.h     |   5 +
 2 files changed, 196 insertions(+)

diff --git a/drivers/gpu/drm/drm_atomic_helper.c b/drivers/gpu/drm/drm_atomic_helper.c
index f842499ca79e..aface21b713e 100644
--- a/drivers/gpu/drm/drm_atomic_helper.c
+++ b/drivers/gpu/drm/drm_atomic_helper.c
@@ -1751,6 +1751,197 @@ backoff:
 EXPORT_SYMBOL(drm_atomic_helper_set_config);
 
 /**
+ * drm_atomic_helper_disable_all - disable all currently active outputs
+ * @dev: DRM device
+ *
+ * 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.
+ *
+ * Returns: 0 on success or a negative error code on failure.
+ */
+int drm_atomic_helper_disable_all(struct drm_device *dev)
+{
+	struct drm_mode_config *config = &dev->mode_config;
+	struct drm_connector_state *conn_state;
+	struct drm_modeset_acquire_ctx ctx;
+	struct drm_atomic_state *state;
+	struct drm_connector *conn;
+	unsigned int i;
+	int err;
+
+	state = drm_atomic_state_alloc(dev);
+	if (!state)
+		return -ENOMEM;
+
+	drm_modeset_acquire_init(&ctx, 0);
+
+	state->allow_modeset = true;
+	state->acquire_ctx = &ctx;
+
+retry:
+	err = drm_modeset_lock(&config->connection_mutex, &ctx);
+	if (err < 0)
+		goto free;
+
+	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 unlock;
+		}
+
+		err = drm_atomic_add_affected_connectors(state, crtc);
+		if (err < 0)
+			goto unlock;
+
+		conn->dpms = DRM_MODE_DPMS_OFF;
+		crtc_state->active = false;
+	}
+
+	err = drm_atomic_commit(state);
+	if (err < 0) {
+		/* restore DPMS mode */
+		for_each_connector_in_state(state, conn, conn_state, i)
+			conn->dpms = DRM_MODE_DPMS_ON;
+
+		if (err == -EDEADLK) {
+			drm_atomic_state_clear(state);
+			drm_modeset_backoff(&ctx);
+			goto retry;
+		}
+	}
+
+unlock:
+	drm_modeset_drop_locks(&ctx);
+free:
+	drm_modeset_acquire_fini(&ctx);
+
+	if (err < 0)
+		drm_atomic_state_free(state);
+
+	return err;
+}
+EXPORT_SYMBOL(drm_atomic_helper_disable_all);
+
+/**
+ * drm_atomic_helper_restore_commit - restores an atomic state object
+ * @state: atomic state object
+ *
+ * Takes an atomic state object for which no locks have been taken (such as
+ * atomic state objects obtained from drm_atomic_helper_duplicate_state()),
+ * grabs all the necessary locks, commits the state and releases the locks
+ * while gracefully handling deadlocks.
+ *
+ * Returns: 0 on success or a negative error code on failure.
+ */
+static int drm_atomic_helper_restore_commit(struct drm_atomic_state *state)
+{
+	struct drm_mode_config *config = &state->dev->mode_config;
+	struct drm_plane_state *plane_state;
+	struct drm_modeset_acquire_ctx ctx;
+	struct drm_crtc_state *crtc_state;
+	struct drm_plane *plane;
+	struct drm_crtc *crtc;
+	unsigned int i;
+	int err;
+
+	drm_modeset_acquire_init(&ctx, 0);
+	state->allow_modeset = true;
+	state->acquire_ctx = &ctx;
+
+retry:
+	err = drm_modeset_lock(&config->connection_mutex, &ctx);
+	if (err < 0)
+		goto fini;
+
+	for_each_crtc_in_state(state, crtc, crtc_state, i) {
+		err = drm_modeset_lock(&crtc->mutex, &ctx);
+		if (err < 0)
+			goto unlock;
+	}
+
+	for_each_plane_in_state(state, plane, plane_state, i) {
+		err = drm_modeset_lock(&plane->mutex, &ctx);
+		if (err < 0)
+			goto unlock;
+	}
+
+	err = drm_atomic_commit(state);
+	if (err == -EDEADLK) {
+		drm_modeset_backoff(&ctx);
+		goto retry;
+	}
+
+unlock:
+	drm_modeset_drop_locks(&ctx);
+fini:
+	drm_modeset_acquire_fini(&ctx);
+
+	return err;
+}
+
+/**
+ * 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.
+ *
+ * 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.
+ */
+struct drm_atomic_state *drm_atomic_helper_suspend(struct drm_device *dev)
+{
+	struct drm_atomic_state *state;
+	int err;
+
+	state = drm_atomic_helper_duplicate_state(dev);
+	if (IS_ERR(state))
+		return state;
+
+	err = drm_atomic_helper_disable_all(dev);
+	if (err < 0) {
+		drm_atomic_state_free(state);
+		return ERR_PTR(err);
+	}
+
+	return state;
+}
+EXPORT_SYMBOL(drm_atomic_helper_suspend);
+
+/**
+ * drm_atomic_helper_resume - subsystem-level resume helper
+ * @dev: DRM device
+ * @state: atomic state to resume to
+ *
+ * Takes an atomic state object, grabs all the necessary locks and commits it.
+ * This can be used in conjunction with the drm_atomic_helper_suspend() helper
+ * to implement suspend/resume for atomic mode-setting enabled drivers.
+ *
+ * Returns: 0 on success or a negative error code on failure.
+ */
+int drm_atomic_helper_resume(struct drm_device *dev,
+				    struct drm_atomic_state *state)
+{
+	drm_mode_config_reset(dev);
+
+	return drm_atomic_helper_restore_commit(state);
+}
+EXPORT_SYMBOL(drm_atomic_helper_resume);
+
+/**
  * drm_atomic_helper_crtc_set_property - helper for crtc properties
  * @crtc: DRM crtc
  * @property: DRM property
diff --git a/include/drm/drm_atomic_helper.h b/include/drm/drm_atomic_helper.h
index 5d27612997ce..080f131c9f2e 100644
--- a/include/drm/drm_atomic_helper.h
+++ b/include/drm/drm_atomic_helper.h
@@ -75,6 +75,11 @@ int drm_atomic_helper_update_plane(struct drm_plane *plane,
 int drm_atomic_helper_disable_plane(struct drm_plane *plane);
 int drm_atomic_helper_set_config(struct drm_mode_set *set);
 
+int drm_atomic_helper_disable_all(struct drm_device *dev);
+struct drm_atomic_state *drm_atomic_helper_suspend(struct drm_device *dev);
+int drm_atomic_helper_resume(struct drm_device *dev,
+			     struct drm_atomic_state *state);
+
 int drm_atomic_helper_crtc_set_property(struct drm_crtc *crtc,
 					struct drm_property *property,
 					uint64_t val);
-- 
2.4.5

_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
http://lists.freedesktop.org/mailman/listinfo/dri-devel

^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [PATCH 3/3] drm/tegra: Implement subsystem-level suspend/resume
  2015-08-11 11:37 [PATCH 1/3] drm: atomic-helper: Implement drm_atomic_helper_duplicate_state() Thierry Reding
  2015-08-11 11:37 ` [PATCH 2/3] drm/atomic-helper: Implement subsystem-level suspend/resume Thierry Reding
@ 2015-08-11 11:37 ` Thierry Reding
  2015-08-11 11:49 ` [PATCH 1/3] drm: atomic-helper: Implement drm_atomic_helper_duplicate_state() Daniel Vetter
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 8+ messages in thread
From: Thierry Reding @ 2015-08-11 11:37 UTC (permalink / raw)
  To: dri-devel

From: Thierry Reding <treding@nvidia.com>

Use the drm_atomic_helper_suspend() and drm_atomic_helper_resume()
helpers to implement subsystem-level suspend/resume.

Signed-off-by: Thierry Reding <treding@nvidia.com>
---
 drivers/gpu/drm/tegra/drm.c | 9 +++++++++
 drivers/gpu/drm/tegra/drm.h | 2 ++
 2 files changed, 11 insertions(+)

diff --git a/drivers/gpu/drm/tegra/drm.c b/drivers/gpu/drm/tegra/drm.c
index 767dce20736c..fe5fcb777fb4 100644
--- a/drivers/gpu/drm/tegra/drm.c
+++ b/drivers/gpu/drm/tegra/drm.c
@@ -1022,16 +1022,25 @@ static int host1x_drm_remove(struct host1x_device *dev)
 static int host1x_drm_suspend(struct device *dev)
 {
 	struct drm_device *drm = dev_get_drvdata(dev);
+	struct tegra_drm *tegra = drm->dev_private;
 
 	drm_kms_helper_poll_disable(drm);
 
+	tegra->state = drm_atomic_helper_suspend(drm);
+	if (IS_ERR(tegra->state)) {
+		drm_kms_helper_poll_enable(drm);
+		return PTR_ERR(tegra->state);
+	}
+
 	return 0;
 }
 
 static int host1x_drm_resume(struct device *dev)
 {
 	struct drm_device *drm = dev_get_drvdata(dev);
+	struct tegra_drm *tegra = drm->dev_private;
 
+	drm_atomic_helper_resume(drm, tegra->state);
 	drm_kms_helper_poll_enable(drm);
 
 	return 0;
diff --git a/drivers/gpu/drm/tegra/drm.h b/drivers/gpu/drm/tegra/drm.h
index dfbc0868dc6f..c31d43150ce1 100644
--- a/drivers/gpu/drm/tegra/drm.h
+++ b/drivers/gpu/drm/tegra/drm.h
@@ -57,6 +57,8 @@ struct tegra_drm {
 		struct work_struct work;
 		struct mutex lock;
 	} commit;
+
+	struct drm_atomic_state *state;
 };
 
 struct tegra_drm_client;
-- 
2.4.5

_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
http://lists.freedesktop.org/mailman/listinfo/dri-devel

^ permalink raw reply related	[flat|nested] 8+ messages in thread

* Re: [PATCH 1/3] drm: atomic-helper: Implement drm_atomic_helper_duplicate_state()
  2015-08-11 11:37 [PATCH 1/3] drm: atomic-helper: Implement drm_atomic_helper_duplicate_state() Thierry Reding
  2015-08-11 11:37 ` [PATCH 2/3] drm/atomic-helper: Implement subsystem-level suspend/resume Thierry Reding
  2015-08-11 11:37 ` [PATCH 3/3] drm/tegra: " Thierry Reding
@ 2015-08-11 11:49 ` Daniel Vetter
  2015-08-11 11:51 ` Maarten Lankhorst
  2015-08-11 11:52 ` Daniel Vetter
  4 siblings, 0 replies; 8+ messages in thread
From: Daniel Vetter @ 2015-08-11 11:49 UTC (permalink / raw)
  To: Thierry Reding; +Cc: dri-devel

On Tue, Aug 11, 2015 at 01:37:43PM +0200, Thierry Reding wrote:
> From: Thierry Reding <treding@nvidia.com>
> 
> This function can be used to duplicate an atomic state object. This is
> useful for example to implement suspend/resume, where the state before
> suspend can be saved and restored upon resume.
> 
> Signed-off-by: Thierry Reding <treding@nvidia.com>
> ---
>  drivers/gpu/drm/drm_atomic_helper.c | 90 +++++++++++++++++++++++++++++++++++++
>  include/drm/drm_atomic_helper.h     |  2 +
>  2 files changed, 92 insertions(+)
> 
> diff --git a/drivers/gpu/drm/drm_atomic_helper.c b/drivers/gpu/drm/drm_atomic_helper.c
> index fd06595c2ee5..f842499ca79e 100644
> --- a/drivers/gpu/drm/drm_atomic_helper.c
> +++ b/drivers/gpu/drm/drm_atomic_helper.c
> @@ -2358,6 +2358,96 @@ drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector)
>  EXPORT_SYMBOL(drm_atomic_helper_connector_duplicate_state);
>  
>  /**
> + * drm_atomic_helper_duplicate_state - duplicate an atomic state object
> + * @dev: DRM device
> + *
> + * Makes a copy of the current atomic state by looping over all objects and
> + * duplicating their respective states.

This needs a big warning that drivers better know what they're doing for
two reasons: It promises that atomic states can be treated as persistent
bits (e.g. if a driver has an update-counter somewhere that won't work).
And it drops locks again so will race if that's not otherwise excluded
(like in suspend/resume where nothing else is allowed to run if you shut
down fbcon with fbdev_set_suspend() before calling this function).

Maybe needs some rewording, but all of the above (including details like
fbdev_set_suspend) must be mentioned.

> + *
> + * Returns: A pointer to the copy of the atomic state object on success or an
> + * ERR_PTR()-encoded error code on failure.
> + */
> +struct drm_atomic_state *
> +drm_atomic_helper_duplicate_state(struct drm_device *dev)
> +{
> +	struct drm_modeset_acquire_ctx ctx;
> +	struct drm_atomic_state *state;
> +	struct drm_connector *conn;
> +	struct drm_plane *plane;
> +	struct drm_crtc *crtc;
> +	int err;
> +
> +	state = drm_atomic_state_alloc(dev);
> +	if (!state)
> +		return ERR_PTR(-ENOMEM);
> +
> +	drm_modeset_acquire_init(&ctx, 0);
> +	state->acquire_ctx = &ctx;
> +
> +	err = drm_modeset_lock(&dev->mode_config.connection_mutex, &ctx);
> +	if (err < 0)
> +		goto free;

Just use drm_modeset_lock_all. You need them all anyway and for
suspend/resume there won't be any contention anyway.

Otherwise lgtm.
-Daniel

> +
> +	drm_for_each_crtc(crtc, dev) {
> +		unsigned int index = drm_crtc_index(crtc);
> +		struct drm_crtc_state *crtc_state;
> +
> +		crtc_state = crtc->funcs->atomic_duplicate_state(crtc);
> +		if (!crtc_state) {
> +			err = -ENOMEM;
> +			goto unlock;
> +		}
> +
> +		state->crtc_states[index] = crtc_state;
> +		state->crtcs[index] = crtc;
> +		crtc_state->state = state;
> +	}
> +
> +	drm_for_each_plane(plane, dev) {
> +		unsigned int index = drm_plane_index(plane);
> +		struct drm_plane_state *plane_state;
> +
> +		plane_state = plane->funcs->atomic_duplicate_state(plane);
> +		if (!plane_state) {
> +			err = -ENOMEM;
> +			goto unlock;
> +		}
> +
> +		state->plane_states[index] = plane_state;
> +		state->planes[index] = plane;
> +		plane_state->state = state;
> +	}
> +
> +	drm_for_each_connector(conn, dev) {
> +		unsigned int index = drm_connector_index(conn);
> +		struct drm_connector_state *conn_state;
> +
> +		conn_state = conn->funcs->atomic_duplicate_state(conn);
> +		if (!conn_state) {
> +			err = -ENOMEM;
> +			goto unlock;
> +		}
> +
> +		state->connector_states[index] = conn_state;
> +		state->connectors[index] = conn;
> +		conn_state->state = state;
> +	}
> +
> +unlock:
> +	drm_modeset_drop_locks(&ctx);
> +free:
> +	drm_modeset_acquire_fini(&ctx);
> +
> +	if (err < 0) {
> +		drm_atomic_state_free(state);
> +		state = ERR_PTR(err);
> +	}
> +
> +	return state;
> +}
> +EXPORT_SYMBOL(drm_atomic_helper_duplicate_state);
> +
> +/**
>   * __drm_atomic_helper_connector_destroy_state - release connector state
>   * @connector: connector object
>   * @state: connector state object to release
> diff --git a/include/drm/drm_atomic_helper.h b/include/drm/drm_atomic_helper.h
> index 4ffe9dca07c4..5d27612997ce 100644
> --- a/include/drm/drm_atomic_helper.h
> +++ b/include/drm/drm_atomic_helper.h
> @@ -118,6 +118,8 @@ __drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector,
>  					   struct drm_connector_state *state);
>  struct drm_connector_state *
>  drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector);
> +struct drm_atomic_state *
> +drm_atomic_helper_duplicate_state(struct drm_device *dev);
>  void
>  __drm_atomic_helper_connector_destroy_state(struct drm_connector *connector,
>  					    struct drm_connector_state *state);
> -- 
> 2.4.5
> 

-- 
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
http://lists.freedesktop.org/mailman/listinfo/dri-devel

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH 1/3] drm: atomic-helper: Implement drm_atomic_helper_duplicate_state()
  2015-08-11 11:37 [PATCH 1/3] drm: atomic-helper: Implement drm_atomic_helper_duplicate_state() Thierry Reding
                   ` (2 preceding siblings ...)
  2015-08-11 11:49 ` [PATCH 1/3] drm: atomic-helper: Implement drm_atomic_helper_duplicate_state() Daniel Vetter
@ 2015-08-11 11:51 ` Maarten Lankhorst
  2015-08-11 11:52 ` Daniel Vetter
  4 siblings, 0 replies; 8+ messages in thread
From: Maarten Lankhorst @ 2015-08-11 11:51 UTC (permalink / raw)
  To: Thierry Reding, dri-devel

Hey,

This patch series would be less complicated if you assume the caller locked with drm_modeset_lock_all
and use the global acquire_ctx in each function. You're locking all state here after all..

Also you should use drm_atomic_get_*_state instead of calling those duplicate_state functions directly.

Op 11-08-15 om 13:37 schreef Thierry Reding:
> From: Thierry Reding <treding@nvidia.com>
>
> This function can be used to duplicate an atomic state object. This is
> useful for example to implement suspend/resume, where the state before
> suspend can be saved and restored upon resume.
>
> Signed-off-by: Thierry Reding <treding@nvidia.com>
> ---
>  drivers/gpu/drm/drm_atomic_helper.c | 90 +++++++++++++++++++++++++++++++++++++
>  include/drm/drm_atomic_helper.h     |  2 +
>  2 files changed, 92 insertions(+)
>
> diff --git a/drivers/gpu/drm/drm_atomic_helper.c b/drivers/gpu/drm/drm_atomic_helper.c
> index fd06595c2ee5..f842499ca79e 100644
> --- a/drivers/gpu/drm/drm_atomic_helper.c
> +++ b/drivers/gpu/drm/drm_atomic_helper.c
> @@ -2358,6 +2358,96 @@ drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector)
>  EXPORT_SYMBOL(drm_atomic_helper_connector_duplicate_state);
>  
>  /**
> + * drm_atomic_helper_duplicate_state - duplicate an atomic state object
> + * @dev: DRM device
> + *
> + * Makes a copy of the current atomic state by looping over all objects and
> + * duplicating their respective states.
> + *
> + * Returns: A pointer to the copy of the atomic state object on success or an
> + * ERR_PTR()-encoded error code on failure.
> + */
> +struct drm_atomic_state *
> +drm_atomic_helper_duplicate_state(struct drm_device *dev)
> +{
> +	struct drm_modeset_acquire_ctx ctx;
> +	struct drm_atomic_state *state;
> +	struct drm_connector *conn;
> +	struct drm_plane *plane;
> +	struct drm_crtc *crtc;
> +	int err;
> +
> +	state = drm_atomic_state_alloc(dev);
> +	if (!state)
> +		return ERR_PTR(-ENOMEM);
> +
> +	drm_modeset_acquire_init(&ctx, 0);
> +	state->acquire_ctx = &ctx;
> +
> +	err = drm_modeset_lock(&dev->mode_config.connection_mutex, &ctx);
> +	if (err < 0)
> +		goto free;
> +
> +	drm_for_each_crtc(crtc, dev) {
> +		unsigned int index = drm_crtc_index(crtc);
> +		struct drm_crtc_state *crtc_state;
> +
> +		crtc_state = crtc->funcs->atomic_duplicate_state(crtc);
> +		if (!crtc_state) {
> +			err = -ENOMEM;
> +			goto unlock;
> +		}
> +
> +		state->crtc_states[index] = crtc_state;
> +		state->crtcs[index] = crtc;
> +		crtc_state->state = state;
> +	}
> +
> +	drm_for_each_plane(plane, dev) {
> +		unsigned int index = drm_plane_index(plane);
> +		struct drm_plane_state *plane_state;
> +
> +		plane_state = plane->funcs->atomic_duplicate_state(plane);
> +		if (!plane_state) {
> +			err = -ENOMEM;
> +			goto unlock;
> +		}
> +
> +		state->plane_states[index] = plane_state;
> +		state->planes[index] = plane;
> +		plane_state->state = state;
> +	}
> +
> +	drm_for_each_connector(conn, dev) {
> +		unsigned int index = drm_connector_index(conn);
> +		struct drm_connector_state *conn_state;
> +
> +		conn_state = conn->funcs->atomic_duplicate_state(conn);
> +		if (!conn_state) {
> +			err = -ENOMEM;
> +			goto unlock;
> +		}
> +
> +		state->connector_states[index] = conn_state;
> +		state->connectors[index] = conn;
> +		conn_state->state = state;
> +	}
> +
> +unlock:
> +	drm_modeset_drop_locks(&ctx);
> +free:
> +	drm_modeset_acquire_fini(&ctx);
> +
> +	if (err < 0) {
> +		drm_atomic_state_free(state);
> +		state = ERR_PTR(err);
> +	}
> +
> +	return state;
> +}
> +EXPORT_SYMBOL(drm_atomic_helper_duplicate_state);
> +
> +/**
>   * __drm_atomic_helper_connector_destroy_state - release connector state
>   * @connector: connector object
>   * @state: connector state object to release
> diff --git a/include/drm/drm_atomic_helper.h b/include/drm/drm_atomic_helper.h
> index 4ffe9dca07c4..5d27612997ce 100644
> --- a/include/drm/drm_atomic_helper.h
> +++ b/include/drm/drm_atomic_helper.h
> @@ -118,6 +118,8 @@ __drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector,
>  					   struct drm_connector_state *state);
>  struct drm_connector_state *
>  drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector);
> +struct drm_atomic_state *
> +drm_atomic_helper_duplicate_state(struct drm_device *dev);
>  void
>  __drm_atomic_helper_connector_destroy_state(struct drm_connector *connector,
>  					    struct drm_connector_state *state);

_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
http://lists.freedesktop.org/mailman/listinfo/dri-devel

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH 1/3] drm: atomic-helper: Implement drm_atomic_helper_duplicate_state()
  2015-08-11 11:37 [PATCH 1/3] drm: atomic-helper: Implement drm_atomic_helper_duplicate_state() Thierry Reding
                   ` (3 preceding siblings ...)
  2015-08-11 11:51 ` Maarten Lankhorst
@ 2015-08-11 11:52 ` Daniel Vetter
  4 siblings, 0 replies; 8+ messages in thread
From: Daniel Vetter @ 2015-08-11 11:52 UTC (permalink / raw)
  To: Thierry Reding; +Cc: dri-devel

On Tue, Aug 11, 2015 at 01:37:43PM +0200, Thierry Reding wrote:
> From: Thierry Reding <treding@nvidia.com>
> 
> This function can be used to duplicate an atomic state object. This is
> useful for example to implement suspend/resume, where the state before
> suspend can be saved and restored upon resume.
> 
> Signed-off-by: Thierry Reding <treding@nvidia.com>
> ---
>  drivers/gpu/drm/drm_atomic_helper.c | 90 +++++++++++++++++++++++++++++++++++++
>  include/drm/drm_atomic_helper.h     |  2 +
>  2 files changed, 92 insertions(+)
> 
> diff --git a/drivers/gpu/drm/drm_atomic_helper.c b/drivers/gpu/drm/drm_atomic_helper.c
> index fd06595c2ee5..f842499ca79e 100644
> --- a/drivers/gpu/drm/drm_atomic_helper.c
> +++ b/drivers/gpu/drm/drm_atomic_helper.c
> @@ -2358,6 +2358,96 @@ drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector)
>  EXPORT_SYMBOL(drm_atomic_helper_connector_duplicate_state);
>  
>  /**
> + * drm_atomic_helper_duplicate_state - duplicate an atomic state object
> + * @dev: DRM device
> + *
> + * Makes a copy of the current atomic state by looping over all objects and
> + * duplicating their respective states.
> + *
> + * Returns: A pointer to the copy of the atomic state object on success or an
> + * ERR_PTR()-encoded error code on failure.
> + */
> +struct drm_atomic_state *
> +drm_atomic_helper_duplicate_state(struct drm_device *dev)
> +{
> +	struct drm_modeset_acquire_ctx ctx;
> +	struct drm_atomic_state *state;
> +	struct drm_connector *conn;
> +	struct drm_plane *plane;
> +	struct drm_crtc *crtc;
> +	int err;
> +
> +	state = drm_atomic_state_alloc(dev);
> +	if (!state)
> +		return ERR_PTR(-ENOMEM);
> +
> +	drm_modeset_acquire_init(&ctx, 0);
> +	state->acquire_ctx = &ctx;
> +
> +	err = drm_modeset_lock(&dev->mode_config.connection_mutex, &ctx);
> +	if (err < 0)
> +		goto free;
> +
> +	drm_for_each_crtc(crtc, dev) {
> +		unsigned int index = drm_crtc_index(crtc);
> +		struct drm_crtc_state *crtc_state;
> +
> +		crtc_state = crtc->funcs->atomic_duplicate_state(crtc);
> +		if (!crtc_state) {
> +			err = -ENOMEM;
> +			goto unlock;
> +		}
> +
> +		state->crtc_states[index] = crtc_state;
> +		state->crtcs[index] = crtc;
> +		crtc_state->state = state;
> +	}
> +
> +	drm_for_each_plane(plane, dev) {
> +		unsigned int index = drm_plane_index(plane);
> +		struct drm_plane_state *plane_state;
> +
> +		plane_state = plane->funcs->atomic_duplicate_state(plane);
> +		if (!plane_state) {
> +			err = -ENOMEM;
> +			goto unlock;
> +		}
> +
> +		state->plane_states[index] = plane_state;
> +		state->planes[index] = plane;
> +		plane_state->state = state;
> +	}
> +
> +	drm_for_each_connector(conn, dev) {
> +		unsigned int index = drm_connector_index(conn);
> +		struct drm_connector_state *conn_state;
> +
> +		conn_state = conn->funcs->atomic_duplicate_state(conn);
> +		if (!conn_state) {
> +			err = -ENOMEM;
> +			goto unlock;
> +		}
> +
> +		state->connector_states[index] = conn_state;
> +		state->connectors[index] = conn;
> +		conn_state->state = state;
> +	}
> +
> +unlock:
> +	drm_modeset_drop_locks(&ctx);
> +free:
> +	drm_modeset_acquire_fini(&ctx);

One more: I think we should clear state->acquire_ctx here, just to make
sure it's not accidentally reused.
-Daniel

> +
> +	if (err < 0) {
> +		drm_atomic_state_free(state);
> +		state = ERR_PTR(err);
> +	}
> +
> +	return state;
> +}
> +EXPORT_SYMBOL(drm_atomic_helper_duplicate_state);
> +
> +/**
>   * __drm_atomic_helper_connector_destroy_state - release connector state
>   * @connector: connector object
>   * @state: connector state object to release
> diff --git a/include/drm/drm_atomic_helper.h b/include/drm/drm_atomic_helper.h
> index 4ffe9dca07c4..5d27612997ce 100644
> --- a/include/drm/drm_atomic_helper.h
> +++ b/include/drm/drm_atomic_helper.h
> @@ -118,6 +118,8 @@ __drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector,
>  					   struct drm_connector_state *state);
>  struct drm_connector_state *
>  drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector);
> +struct drm_atomic_state *
> +drm_atomic_helper_duplicate_state(struct drm_device *dev);
>  void
>  __drm_atomic_helper_connector_destroy_state(struct drm_connector *connector,
>  					    struct drm_connector_state *state);
> -- 
> 2.4.5
> 

-- 
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
http://lists.freedesktop.org/mailman/listinfo/dri-devel

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH 2/3] drm/atomic-helper: Implement subsystem-level suspend/resume
  2015-08-11 11:37 ` [PATCH 2/3] drm/atomic-helper: Implement subsystem-level suspend/resume Thierry Reding
@ 2015-08-11 11:55   ` Maarten Lankhorst
  2015-08-11 11:59   ` Daniel Vetter
  1 sibling, 0 replies; 8+ messages in thread
From: Maarten Lankhorst @ 2015-08-11 11:55 UTC (permalink / raw)
  To: Thierry Reding, dri-devel

Op 11-08-15 om 13:37 schreef Thierry Reding:
> From: Thierry Reding <treding@nvidia.com>
>
> Provide subsystem-level suspend and resume helpers that can be used to
> implement suspend/resume on atomic mode-setting enabled drivers.
>
> Signed-off-by: Thierry Reding <treding@nvidia.com>
> ---
>  drivers/gpu/drm/drm_atomic_helper.c | 191 ++++++++++++++++++++++++++++++++++++
>  include/drm/drm_atomic_helper.h     |   5 +
>  2 files changed, 196 insertions(+)
>
> diff --git a/drivers/gpu/drm/drm_atomic_helper.c b/drivers/gpu/drm/drm_atomic_helper.c
> index f842499ca79e..aface21b713e 100644
> --- a/drivers/gpu/drm/drm_atomic_helper.c
> +++ b/drivers/gpu/drm/drm_atomic_helper.c
> @@ -1751,6 +1751,197 @@ backoff:
>  EXPORT_SYMBOL(drm_atomic_helper_set_config);
>  
>  /**
> + * drm_atomic_helper_disable_all - disable all currently active outputs
> + * @dev: DRM device
> + *
> + * 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.
> + *
> + * Returns: 0 on success or a negative error code on failure.
> + */
> +int drm_atomic_helper_disable_all(struct drm_device *dev)
> +{
> +	struct drm_mode_config *config = &dev->mode_config;
> +	struct drm_connector_state *conn_state;
> +	struct drm_modeset_acquire_ctx ctx;
> +	struct drm_atomic_state *state;
> +	struct drm_connector *conn;
> +	unsigned int i;
> +	int err;
> +
> +	state = drm_atomic_state_alloc(dev);
> +	if (!state)
> +		return -ENOMEM;
> +
> +	drm_modeset_acquire_init(&ctx, 0);
> +
> +	state->allow_modeset = true;
> +	state->acquire_ctx = &ctx;
> +
> +retry:
> +	err = drm_modeset_lock(&config->connection_mutex, &ctx);
> +	if (err < 0)
> +		goto free;
> +
> +	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 unlock;
> +		}
> +
> +		err = drm_atomic_add_affected_connectors(state, crtc);
> +		if (err < 0)
> +			goto unlock;
> +
> +		conn->dpms = DRM_MODE_DPMS_OFF;
> +		crtc_state->active = false;
> +	}
> +
> +	err = drm_atomic_commit(state);
> +	if (err < 0) {
> +		/* restore DPMS mode */
> +		for_each_connector_in_state(state, conn, conn_state, i)
> +			conn->dpms = DRM_MODE_DPMS_ON;
> +
> +		if (err == -EDEADLK) {
> +			drm_atomic_state_clear(state);
> +			drm_modeset_backoff(&ctx);
> +			goto retry;
> +		}
> +	}
> +
> +unlock:
> +	drm_modeset_drop_locks(&ctx);
> +free:
> +	drm_modeset_acquire_fini(&ctx);
> +
> +	if (err < 0)
> +		drm_atomic_state_free(state);
> +
> +	return err;
> +}
> +EXPORT_SYMBOL(drm_atomic_helper_disable_all);
Use lock_all again. And this version is not handling -EDEADLK from get_*_state so it doesn't work as intended. :-)
In case of doubt, try with CONFIG_DEBUG_WW_MUTEX_SLOWPATH.
> +/**
> + * drm_atomic_helper_restore_commit - restores an atomic state object
> + * @state: atomic state object
> + *
> + * Takes an atomic state object for which no locks have been taken (such as
> + * atomic state objects obtained from drm_atomic_helper_duplicate_state()),
> + * grabs all the necessary locks, commits the state and releases the locks
> + * while gracefully handling deadlocks.
> + *
> + * Returns: 0 on success or a negative error code on failure.
> + */
> +static int drm_atomic_helper_restore_commit(struct drm_atomic_state *state)
> +{
> +	struct drm_mode_config *config = &state->dev->mode_config;
> +	struct drm_plane_state *plane_state;
> +	struct drm_modeset_acquire_ctx ctx;
> +	struct drm_crtc_state *crtc_state;
> +	struct drm_plane *plane;
> +	struct drm_crtc *crtc;
> +	unsigned int i;
> +	int err;
> +
> +	drm_modeset_acquire_init(&ctx, 0);
> +	state->allow_modeset = true;
> +	state->acquire_ctx = &ctx;
> +
> +retry:
> +	err = drm_modeset_lock(&config->connection_mutex, &ctx);
> +	if (err < 0)
> +		goto fini;
> +
> +	for_each_crtc_in_state(state, crtc, crtc_state, i) {
> +		err = drm_modeset_lock(&crtc->mutex, &ctx);
> +		if (err < 0)
> +			goto unlock;
> +	}
> +
> +	for_each_plane_in_state(state, plane, plane_state, i) {
> +		err = drm_modeset_lock(&plane->mutex, &ctx);
> +		if (err < 0)
> +			goto unlock;
> +	}
> +
> +	err = drm_atomic_commit(state);
> +	if (err == -EDEADLK) {
> +		drm_modeset_backoff(&ctx);
> +		goto retry;
> +	}
> +
> +unlock:
> +	drm_modeset_drop_locks(&ctx);
> +fini:
> +	drm_modeset_acquire_fini(&ctx);
> +
> +	return err;
> +}

^^

drm_modeset_lock_all(dev);
state->acquire_ctx = ...;
ret = drm_atomic_commit(state);
drm_modeset_unlock_all(dev);

and fold it into atomic_helper_resume
> +/**
> + * 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.
> + *
> + * 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.
> + */
> +struct drm_atomic_state *drm_atomic_helper_suspend(struct drm_device *dev)
> +{
> +	struct drm_atomic_state *state;
> +	int err;
drm_modeset_lock_all();
> +	state = drm_atomic_helper_duplicate_state(dev);
> +	if (IS_ERR(state))
> +		return state;
> +
> +	err = drm_atomic_helper_disable_all(dev);
> +	if (err < 0) {
> +		drm_atomic_state_free(state);
> +		return ERR_PTR(err);
> +	}
drm_modeset_lock_unlock_all();
> +	return state;
> +}
> +EXPORT_SYMBOL(drm_atomic_helper_suspend);


> +/**
> + * drm_atomic_helper_resume - subsystem-level resume helper
> + * @dev: DRM device
> + * @state: atomic state to resume to
> + *
> + * Takes an atomic state object, grabs all the necessary locks and commits it.
> + * This can be used in conjunction with the drm_atomic_helper_suspend() helper
> + * to implement suspend/resume for atomic mode-setting enabled drivers.
> + *
> + * Returns: 0 on success or a negative error code on failure.
> + */
> +int drm_atomic_helper_resume(struct drm_device *dev,
> +				    struct drm_atomic_state *state)
> +{
> +	drm_mode_config_reset(dev);
> +
> +	return drm_atomic_helper_restore_commit(state);
> +}
> +EXPORT_SYMBOL(drm_atomic_helper_resume);
> +
> +/**
>   * drm_atomic_helper_crtc_set_property - helper for crtc properties
>   * @crtc: DRM crtc
>   * @property: DRM property
> diff --git a/include/drm/drm_atomic_helper.h b/include/drm/drm_atomic_helper.h
> index 5d27612997ce..080f131c9f2e 100644
> --- a/include/drm/drm_atomic_helper.h
> +++ b/include/drm/drm_atomic_helper.h
> @@ -75,6 +75,11 @@ int drm_atomic_helper_update_plane(struct drm_plane *plane,
>  int drm_atomic_helper_disable_plane(struct drm_plane *plane);
>  int drm_atomic_helper_set_config(struct drm_mode_set *set);
>  
> +int drm_atomic_helper_disable_all(struct drm_device *dev);
> +struct drm_atomic_state *drm_atomic_helper_suspend(struct drm_device *dev);
> +int drm_atomic_helper_resume(struct drm_device *dev,
> +			     struct drm_atomic_state *state);
> +
>  int drm_atomic_helper_crtc_set_property(struct drm_crtc *crtc,
>  					struct drm_property *property,
>  					uint64_t val);

_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
http://lists.freedesktop.org/mailman/listinfo/dri-devel

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH 2/3] drm/atomic-helper: Implement subsystem-level suspend/resume
  2015-08-11 11:37 ` [PATCH 2/3] drm/atomic-helper: Implement subsystem-level suspend/resume Thierry Reding
  2015-08-11 11:55   ` Maarten Lankhorst
@ 2015-08-11 11:59   ` Daniel Vetter
  1 sibling, 0 replies; 8+ messages in thread
From: Daniel Vetter @ 2015-08-11 11:59 UTC (permalink / raw)
  To: Thierry Reding; +Cc: dri-devel

On Tue, Aug 11, 2015 at 01:37:44PM +0200, Thierry Reding wrote:
> From: Thierry Reding <treding@nvidia.com>
> 
> Provide subsystem-level suspend and resume helpers that can be used to
> implement suspend/resume on atomic mode-setting enabled drivers.
> 
> Signed-off-by: Thierry Reding <treding@nvidia.com>
> ---
>  drivers/gpu/drm/drm_atomic_helper.c | 191 ++++++++++++++++++++++++++++++++++++
>  include/drm/drm_atomic_helper.h     |   5 +
>  2 files changed, 196 insertions(+)
> 
> diff --git a/drivers/gpu/drm/drm_atomic_helper.c b/drivers/gpu/drm/drm_atomic_helper.c
> index f842499ca79e..aface21b713e 100644
> --- a/drivers/gpu/drm/drm_atomic_helper.c
> +++ b/drivers/gpu/drm/drm_atomic_helper.c
> @@ -1751,6 +1751,197 @@ backoff:
>  EXPORT_SYMBOL(drm_atomic_helper_set_config);
>  
>  /**
> + * drm_atomic_helper_disable_all - disable all currently active outputs
> + * @dev: DRM device
> + *
> + * 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.

Imo the subordinate helpers here should also mention how they're used in
the suspend/resume helpers and reference them (soonish all those function
references will be hyperlinks, making them even more useful). Same for the
other subordinate helpers (also duplicate_state from the previous patch).

> + *
> + * Returns: 0 on success or a negative error code on failure.
> + */
> +int drm_atomic_helper_disable_all(struct drm_device *dev)
> +{
> +	struct drm_mode_config *config = &dev->mode_config;
> +	struct drm_connector_state *conn_state;
> +	struct drm_modeset_acquire_ctx ctx;
> +	struct drm_atomic_state *state;
> +	struct drm_connector *conn;
> +	unsigned int i;
> +	int err;
> +
> +	state = drm_atomic_state_alloc(dev);
> +	if (!state)
> +		return -ENOMEM;
> +
> +	drm_modeset_acquire_init(&ctx, 0);
> +
> +	state->allow_modeset = true;
> +	state->acquire_ctx = &ctx;
> +
> +retry:
> +	err = drm_modeset_lock(&config->connection_mutex, &ctx);
> +	if (err < 0)
> +		goto free;

Same comment about locking as with the duplicate_state helper.

> +
> +	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 unlock;
> +		}
> +
> +		err = drm_atomic_add_affected_connectors(state, crtc);
> +		if (err < 0)
> +			goto unlock;
> +
> +		conn->dpms = DRM_MODE_DPMS_OFF;
> +		crtc_state->active = false;
> +	}
> +
> +	err = drm_atomic_commit(state);
> +	if (err < 0) {
> +		/* restore DPMS mode */
> +		for_each_connector_in_state(state, conn, conn_state, i)
> +			conn->dpms = DRM_MODE_DPMS_ON;
> +
> +		if (err == -EDEADLK) {
> +			drm_atomic_state_clear(state);
> +			drm_modeset_backoff(&ctx);
> +			goto retry;
> +		}
> +	}
> +
> +unlock:
> +	drm_modeset_drop_locks(&ctx);
> +free:
> +	drm_modeset_acquire_fini(&ctx);
> +
> +	if (err < 0)
> +		drm_atomic_state_free(state);
> +
> +	return err;
> +}
> +EXPORT_SYMBOL(drm_atomic_helper_disable_all);
> +
> +/**
> + * drm_atomic_helper_restore_commit - restores an atomic state object
> + * @state: atomic state object
> + *
> + * Takes an atomic state object for which no locks have been taken (such as
> + * atomic state objects obtained from drm_atomic_helper_duplicate_state()),
> + * grabs all the necessary locks, commits the state and releases the locks
> + * while gracefully handling deadlocks.

I think this also needs some warnings that reusing atomic states while
locks have been droppped could be risky, similar to duplicate_state().

> + *
> + * Returns: 0 on success or a negative error code on failure.
> + */
> +static int drm_atomic_helper_restore_commit(struct drm_atomic_state *state)
> +{
> +	struct drm_mode_config *config = &state->dev->mode_config;
> +	struct drm_plane_state *plane_state;
> +	struct drm_modeset_acquire_ctx ctx;
> +	struct drm_crtc_state *crtc_state;
> +	struct drm_plane *plane;
> +	struct drm_crtc *crtc;
> +	unsigned int i;
> +	int err;
> +
> +	drm_modeset_acquire_init(&ctx, 0);
> +	state->allow_modeset = true;
> +	state->acquire_ctx = &ctx;
> +
> +retry:
> +	err = drm_modeset_lock(&config->connection_mutex, &ctx);
> +	if (err < 0)
> +		goto fini;
> +
> +	for_each_crtc_in_state(state, crtc, crtc_state, i) {
> +		err = drm_modeset_lock(&crtc->mutex, &ctx);
> +		if (err < 0)
> +			goto unlock;
> +	}
> +
> +	for_each_plane_in_state(state, plane, plane_state, i) {
> +		err = drm_modeset_lock(&plane->mutex, &ctx);
> +		if (err < 0)
> +			goto unlock;
> +	}
> +
> +	err = drm_atomic_commit(state);
> +	if (err == -EDEADLK) {
> +		drm_modeset_backoff(&ctx);
> +		goto retry;
> +	}
> +
> +unlock:
> +	drm_modeset_drop_locks(&ctx);
> +fini:
> +	drm_modeset_acquire_fini(&ctx);
> +
> +	return err;
> +}
> +
> +/**
> + * 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.
> + *
> + * 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.

Style-nit: There should be a newline after "Returns:" for consistency with
other kerneldoc in drm.

> + */
> +struct drm_atomic_state *drm_atomic_helper_suspend(struct drm_device *dev)
> +{
> +	struct drm_atomic_state *state;
> +	int err;
> +
> +	state = drm_atomic_helper_duplicate_state(dev);
> +	if (IS_ERR(state))
> +		return state;
> +
> +	err = drm_atomic_helper_disable_all(dev);
> +	if (err < 0) {
> +		drm_atomic_state_free(state);

This will hit a locking WARN_ON. Maybe better to pull out the
drm_modset_lock all from these two functions and just pass them the
acquire ctx? Otoh drm-misc already has a patch from Maarten to remove that
locking check since its violated with drm dp mst stuff iirc.

> +		return ERR_PTR(err);
> +	}
> +
> +	return state;
> +}
> +EXPORT_SYMBOL(drm_atomic_helper_suspend);
> +
> +/**
> + * drm_atomic_helper_resume - subsystem-level resume helper
> + * @dev: DRM device
> + * @state: atomic state to resume to
> + *
> + * Takes an atomic state object, grabs all the necessary locks and commits it.
> + * This can be used in conjunction with the drm_atomic_helper_suspend() helper
> + * to implement suspend/resume for atomic mode-setting enabled drivers.

Kerneldoc should also mention that it calls reset() first to make sure sw
and hw state agree again. Otherwise the atomic_commit might go "well
there's nothing to do here", so it's a crucial step.

Looks great otherwise.
-Daniel

> + *
> + * Returns: 0 on success or a negative error code on failure.
> + */
> +int drm_atomic_helper_resume(struct drm_device *dev,
> +				    struct drm_atomic_state *state)
> +{
> +	drm_mode_config_reset(dev);
> +
> +	return drm_atomic_helper_restore_commit(state);
> +}
> +EXPORT_SYMBOL(drm_atomic_helper_resume);
> +
> +/**
>   * drm_atomic_helper_crtc_set_property - helper for crtc properties
>   * @crtc: DRM crtc
>   * @property: DRM property
> diff --git a/include/drm/drm_atomic_helper.h b/include/drm/drm_atomic_helper.h
> index 5d27612997ce..080f131c9f2e 100644
> --- a/include/drm/drm_atomic_helper.h
> +++ b/include/drm/drm_atomic_helper.h
> @@ -75,6 +75,11 @@ int drm_atomic_helper_update_plane(struct drm_plane *plane,
>  int drm_atomic_helper_disable_plane(struct drm_plane *plane);
>  int drm_atomic_helper_set_config(struct drm_mode_set *set);
>  
> +int drm_atomic_helper_disable_all(struct drm_device *dev);
> +struct drm_atomic_state *drm_atomic_helper_suspend(struct drm_device *dev);
> +int drm_atomic_helper_resume(struct drm_device *dev,
> +			     struct drm_atomic_state *state);
> +
>  int drm_atomic_helper_crtc_set_property(struct drm_crtc *crtc,
>  					struct drm_property *property,
>  					uint64_t val);
> -- 
> 2.4.5
> 

-- 
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
http://lists.freedesktop.org/mailman/listinfo/dri-devel

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2015-08-11 11:59 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-08-11 11:37 [PATCH 1/3] drm: atomic-helper: Implement drm_atomic_helper_duplicate_state() Thierry Reding
2015-08-11 11:37 ` [PATCH 2/3] drm/atomic-helper: Implement subsystem-level suspend/resume Thierry Reding
2015-08-11 11:55   ` Maarten Lankhorst
2015-08-11 11:59   ` Daniel Vetter
2015-08-11 11:37 ` [PATCH 3/3] drm/tegra: " Thierry Reding
2015-08-11 11:49 ` [PATCH 1/3] drm: atomic-helper: Implement drm_atomic_helper_duplicate_state() Daniel Vetter
2015-08-11 11:51 ` Maarten Lankhorst
2015-08-11 11:52 ` Daniel Vetter

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.