[PATCH v10 3/9] drm/atomic: Allow filling a commit with pristine object states
From: Maxime Ripard
Date: Thu Nov 12 2026 - 11:36:02 EST
The upcoming DRM_MODE_ATOMIC_RESET flag will need to create an atomic
commit that brings the entire device back to a pristine state, as if
no configuration had ever been applied.
Create drm_atomic_commit_fill_with_defaults() which iterates over all
CRTCs, planes, connectors, and color operations in the device and
inserts a fresh default state for each one into the commit. This uses
the atomic_create_state() hooks rather than atomic_duplicate_state(),
since atomic_create_state() provides exactly this pristine state on a
per-object basis.
A lessee only gets the objects of its lease reset, the other ones
keeping their current state. Partial pipelines aren't supported
though: detaching a plane or a connector from its CRTC, and disabling
a CRTC driving them, both need the two sides in the commit, so a lease
that splits a pipeline is rejected with -ENOENT. validate_lease() is
expected to keep that from happening, since it requires a lease to
hold at least a CRTC, a connector and a plane, but it only looks at
the object types, not at what they are bound to.
Part of the state the atomic helpers maintain is only recomputed when
something changed, which a reset re-applying the same configuration
doesn't trigger. The CRTC adjusted_mode and the connector best_encoder
are therefore carried over like a duplicated state would, and the
CRTCs ask for a zpos normalization.
The color management changes are reported through color_mgmt_changed
for the same reason: the userspace property handlers only set it when
a property write changes the value stored in the new state, so neither
restoring a default nor leaving a property untouched would, even
though the reset clears whatever is currently programmed.
Signed-off-by: Maxime Ripard <mripard@xxxxxxxxxx>
---
drivers/gpu/drm/drm_atomic.c | 285 +++++++++++++++++++++++++++++++++++++++++++
include/drm/drm_atomic.h | 2 +
2 files changed, 287 insertions(+)
diff --git a/drivers/gpu/drm/drm_atomic.c b/drivers/gpu/drm/drm_atomic.c
index 7a77051096c9..4fcce3bb0aa1 100644
--- a/drivers/gpu/drm/drm_atomic.c
+++ b/drivers/gpu/drm/drm_atomic.c
@@ -1647,10 +1647,295 @@ bool drm_atomic_implements_create_state(struct drm_device *dev)
return true;
}
EXPORT_SYMBOL(drm_atomic_implements_create_state);
+/**
+ * drm_atomic_commit_fill_with_defaults - populate a commit with pristine states
+ * @file_priv: DRM file the reset was requested from, or NULL
+ * @commit: atomic commit to fill
+ *
+ * Iterate over all CRTCs, planes, connectors, and color operations in
+ * the device and insert a freshly created default state for each one
+ * into @commit. The states are created through the atomic_create_state()
+ * hooks, producing the same initial state the driver starts with rather
+ * than a copy of the current hardware state.
+ *
+ * In the default state, all objects are disabled: CRTCs are inactive,
+ * planes are disabled, and connectors are unbound. Driver-specific
+ * properties such as color pipeline settings or max_bpc may carry
+ * non-zero defaults appropriate for basic display output.
+ *
+ * If @file_priv is a lessee, only the CRTCs, planes and connectors part
+ * of its lease are reset, and the other objects keep their current
+ * state. @file_priv is optional: if NULL, it is considered to not hold
+ * any lease, and all objects are reset.
+ *
+ * Partial pipelines aren't supported. Resetting a plane or a connector
+ * detaches it from the CRTC it is bound to, and resetting a CRTC
+ * disables the planes and connectors bound to it, neither of which the
+ * atomic helpers can sort out with only one side of the pair in the
+ * commit. A lease covering a plane or a connector without the CRTC it
+ * is bound to, or a CRTC without all the planes and connectors bound
+ * to it, would thus require reaching outside of it, so the reset is
+ * rejected instead.
+ *
+ * Such a lease shouldn't exist in the first place: validate_lease()
+ * requires a lease to hold at least a CRTC, a connector and a plane,
+ * so leases are meant to describe whole pipelines. It only looks at
+ * the object types though, not at what they are bound to, so the
+ * reset still has to guard against it.
+ *
+ * This is meant to be used with the %DRM_MODE_ATOMIC_RESET flag, which
+ * needs to bring the device back to a known baseline before applying
+ * userspace property changes on top.
+ *
+ * Returns:
+ * 0 on success, -ENOENT if a leased plane or connector is bound to a
+ * CRTC that isn't leased, or a negative error code on failure.
+ */
+int drm_atomic_commit_fill_with_defaults(struct drm_file *file_priv,
+ struct drm_atomic_commit *commit)
+{
+ struct drm_device *dev = commit->dev;
+ struct drm_mode_config *config = &dev->mode_config;
+ struct drm_crtc *crtc;
+ struct drm_plane *plane, *crtc_plane;
+ struct drm_connector *connector;
+ struct drm_connector_list_iter conn_iter;
+ struct drm_colorop *colorop;
+ int ret;
+
+ drm_WARN_ON(dev, !commit->acquire_ctx);
+
+ /*
+ * Private objects are ignored because none have userspace
+ * properties we might want to reset. atomic_check
+ * implementations will derive or infer their private obj state
+ * from the state that will end up being committed anyway.
+ */
+ drm_for_each_colorop(colorop, dev) {
+ struct drm_colorop_state *colorop_state;
+
+ if (!drm_lease_held(file_priv, colorop->plane->base.id))
+ continue;
+
+ ret = drm_modeset_lock(&colorop->plane->mutex, commit->acquire_ctx);
+ if (ret)
+ return ret;
+
+ colorop_state = drm_atomic_helper_colorop_create_state(colorop);
+ if (IS_ERR(colorop_state))
+ return PTR_ERR(colorop_state);
+
+ drm_atomic_commit_set_colorop_state(commit, colorop, colorop_state);
+ }
+
+ drm_for_each_plane(plane, dev) {
+ struct drm_plane_state *plane_state;
+
+ if (!drm_lease_held(file_priv, plane->base.id))
+ continue;
+
+ ret = drm_modeset_lock(&plane->mutex, commit->acquire_ctx);
+ if (ret)
+ return ret;
+
+ /*
+ * The pristine state detaches the plane from the CRTC it
+ * is enabled on, which the atomic helpers can only sort
+ * out with that CRTC in the commit. Resetting a plane
+ * enabled on a CRTC that isn't leased would thus require
+ * touching an object the lease doesn't cover, so report it
+ * like any other access outside of the lease.
+ */
+ if (plane->state->crtc &&
+ !drm_lease_held(file_priv, plane->state->crtc->base.id)) {
+ drm_dbg_atomic(dev,
+ "[PLANE:%d:%s] is enabled on [CRTC:%d:%s], which isn't leased\n",
+ plane->base.id, plane->name,
+ plane->state->crtc->base.id,
+ plane->state->crtc->name);
+ return -ENOENT;
+ }
+
+ plane_state = plane->funcs->atomic_create_state(plane);
+ if (IS_ERR(plane_state))
+ return PTR_ERR(plane_state);
+
+ /*
+ * Same thing for the planes: report a color pipeline change
+ * compared to the current state.
+ */
+ plane_state->color_mgmt_changed =
+ plane->state->color_pipeline != plane_state->color_pipeline;
+
+ drm_atomic_commit_set_plane_state(commit, plane, plane_state);
+ }
+
+ drm_for_each_crtc(crtc, dev) {
+ struct drm_crtc_state *crtc_state;
+
+ if (!drm_lease_held(file_priv, crtc->base.id))
+ continue;
+
+ ret = drm_modeset_lock(&crtc->mutex, commit->acquire_ctx);
+ if (ret)
+ return ret;
+
+ /*
+ * The reset disables the CRTC, which the planes enabled on
+ * it can't stay bound to. Disabling them is only an option
+ * for the ones the lease covers, so a CRTC driving a plane
+ * it doesn't is rejected like the other way around.
+ */
+ drm_for_each_plane_mask(crtc_plane, dev, crtc->state->plane_mask) {
+ if (drm_lease_held(file_priv, crtc_plane->base.id))
+ continue;
+
+ drm_dbg_atomic(dev,
+ "[CRTC:%d:%s] drives [PLANE:%d:%s], which isn't leased\n",
+ crtc->base.id, crtc->name,
+ crtc_plane->base.id, crtc_plane->name);
+ return -ENOENT;
+ }
+
+ crtc_state = crtc->funcs->atomic_create_state(crtc);
+ if (IS_ERR(crtc_state))
+ return PTR_ERR(crtc_state);
+
+ /*
+ * The userspace property handlers only set
+ * color_mgmt_changed when a property write changes the
+ * value stored in the new state. Since the pristine states
+ * we create here are already set to their default values,
+ * restoring those defaults wouldn't set the flag, and
+ * neither would leaving the properties untouched, even
+ * though the commit clears whatever LUT or CTM is
+ * currently programmed. Compare with the current state
+ * so that the drivers reprogram the color management
+ * hardware.
+ */
+ crtc_state->color_mgmt_changed =
+ crtc->state->degamma_lut != crtc_state->degamma_lut ||
+ crtc->state->ctm != crtc_state->ctm ||
+ crtc->state->gamma_lut != crtc_state->gamma_lut;
+
+ /*
+ * adjusted_mode is derived from the mode by the atomic
+ * helpers rather than set by userspace, and mode_fixup()
+ * only recomputes it when the mode or the connectors
+ * changed. A duplicated state carries it over, so nothing
+ * needs to be recomputed when nothing changes, but the
+ * pristine states we create here would be left with a
+ * zeroed adjusted_mode on a CRTC that stays enabled across
+ * the commit. Carry it over too, so that the drivers and
+ * the vblank timestamping constants keep seeing the
+ * timings currently programmed.
+ */
+ drm_mode_copy(&crtc_state->adjusted_mode,
+ &crtc->state->adjusted_mode);
+
+ /*
+ * The planes come back with the default zpos, which the
+ * atomic helpers only normalize when the planes bound to a
+ * CRTC or one of their zpos changed. Re-applying the same
+ * configuration on top of the reset wouldn't trigger
+ * either, so ask for a normalization explicitly.
+ */
+ crtc_state->zpos_changed = true;
+
+ drm_atomic_commit_set_crtc_state(commit, crtc, crtc_state);
+ }
+
+ ret = drm_modeset_lock(&config->connection_mutex, commit->acquire_ctx);
+ if (ret)
+ return ret;
+
+ drm_connector_list_iter_begin(dev, &conn_iter);
+ drm_for_each_connector_iter(connector, &conn_iter) {
+ struct drm_connector_state *connector_state;
+
+ if (!drm_lease_held(file_priv, connector->base.id)) {
+ /*
+ * Same as the planes of a leased CRTC: the reset
+ * disables that CRTC, and a connector the lease
+ * doesn't cover can't be unbound from it.
+ */
+ if (connector->state->crtc &&
+ drm_lease_held(file_priv,
+ connector->state->crtc->base.id)) {
+ drm_dbg_atomic(dev,
+ "[CONNECTOR:%d:%s] isn't leased, but [CRTC:%d:%s] it is bound to is\n",
+ connector->base.id, connector->name,
+ connector->state->crtc->base.id,
+ connector->state->crtc->name);
+ drm_connector_list_iter_end(&conn_iter);
+ return -ENOENT;
+ }
+
+ continue;
+ }
+
+ /*
+ * Same as the planes above: unbinding the connector from
+ * an unleased CRTC would need that CRTC in the commit.
+ */
+ if (connector->state->crtc &&
+ !drm_lease_held(file_priv, connector->state->crtc->base.id)) {
+ drm_dbg_atomic(dev,
+ "[CONNECTOR:%d:%s] is bound to [CRTC:%d:%s], which isn't leased\n",
+ connector->base.id, connector->name,
+ connector->state->crtc->base.id,
+ connector->state->crtc->name);
+ drm_connector_list_iter_end(&conn_iter);
+ return -ENOENT;
+ }
+
+ /*
+ * drm_atomic_implements_create_state() already checked
+ * this, but connectors can be hotplugged between that
+ * lockless check and the iteration here.
+ */
+ if (!connector->funcs->atomic_create_state) {
+ drm_connector_list_iter_end(&conn_iter);
+ return -EOPNOTSUPP;
+ }
+
+ connector_state = connector->funcs->atomic_create_state(connector);
+ if (IS_ERR(connector_state)) {
+ drm_connector_list_iter_end(&conn_iter);
+ ret = PTR_ERR(connector_state);
+ return ret;
+ }
+
+ /*
+ * best_encoder is a routing information maintained by the
+ * atomic helpers rather than a userspace property, and it
+ * is only carried over to a new state when that state is a
+ * duplicate of the current one. Since
+ * update_connector_routing() uses it to detect the commits
+ * that keep the connector routing unchanged, carry the
+ * encoder currently in use over so that a reset on which
+ * userspace re-applies the exact same configuration doesn't
+ * require a modeset.
+ */
+ connector_state->best_encoder = connector->state->best_encoder;
+
+ ret = drm_atomic_commit_set_connector_state(commit, connector, connector_state);
+ if (ret) {
+ connector->funcs->atomic_destroy_state(connector, connector_state);
+ drm_connector_list_iter_end(&conn_iter);
+ return ret;
+ }
+ }
+ drm_connector_list_iter_end(&conn_iter);
+
+ return 0;
+}
+EXPORT_SYMBOL(drm_atomic_commit_fill_with_defaults);
+
/**
* drm_atomic_add_encoder_bridges - add bridges attached to an encoder
* @state: atomic state
* @encoder: DRM encoder
*
diff --git a/include/drm/drm_atomic.h b/include/drm/drm_atomic.h
index 72c3d420f40c..cac74d43a34d 100644
--- a/include/drm/drm_atomic.h
+++ b/include/drm/drm_atomic.h
@@ -701,10 +701,12 @@ int drm_crtc_commit_wait(struct drm_crtc_commit *commit);
struct drm_atomic_commit * __must_check
drm_atomic_commit_alloc(struct drm_device *dev);
void drm_atomic_commit_clear(struct drm_atomic_commit *state);
bool drm_atomic_implements_create_state(struct drm_device *dev);
+int drm_atomic_commit_fill_with_defaults(struct drm_file *file_priv,
+ struct drm_atomic_commit *commit);
/**
* drm_atomic_commit_get - acquire a reference to the atomic state
* @state: The atomic state
*
--
2.56.0