Once the SRO state has been built from hardware, it needs to be
installed as the current state of each KMS object.

Add drm_atomic_sro_install_state() which walks through the SRO state
container and assigns each readout state to its object's state
pointer. Before each assignment, it calls the optional
atomic_sro_install_state hook to give drivers a chance to acquire the
resources they need to keep the hardware state active.

Also introduce the atomic_sro_install_state hook in the plane, CRTC,
connector, and private object funcs vtables.

Signed-off-by: Maxime Ripard <[email protected]>
---
 drivers/gpu/drm/drm_atomic_sro.c | 97 ++++++++++++++++++++++++++++++++++++++++
 include/drm/drm_atomic.h         | 18 ++++++++
 include/drm/drm_atomic_sro.h     |  2 +
 include/drm/drm_connector.h      | 18 ++++++++
 include/drm/drm_crtc.h           | 18 ++++++++
 include/drm/drm_plane.h          | 18 ++++++++
 6 files changed, 171 insertions(+)

diff --git a/drivers/gpu/drm/drm_atomic_sro.c b/drivers/gpu/drm/drm_atomic_sro.c
index f90f7cda182b..0c63f880002d 100644
--- a/drivers/gpu/drm/drm_atomic_sro.c
+++ b/drivers/gpu/drm/drm_atomic_sro.c
@@ -515,5 +515,102 @@ void drm_atomic_sro_set_private_obj_state(struct 
drm_atomic_sro_state *state,
        drm_dbg_atomic(state->dev,
                       "Added new private object %p state %p to readout state 
%p\n", obj,
                       obj_state, state);
 }
 EXPORT_SYMBOL(drm_atomic_sro_set_private_obj_state);
+
+/**
+ * drm_atomic_sro_install_state - install readout state into DRM objects
+ * @state: SRO state to install
+ *
+ * Takes a &struct drm_atomic_sro_state built by
+ * drm_atomic_helper_sro_build_state() and installs its contents as the
+ * current state of each DRM object (setting &drm_crtc.state,
+ * &drm_plane.state, &drm_connector.state and &drm_private_obj.state).
+ *
+ * For each object, the optional atomic_sro_install_state hook is
+ * called before the state pointer is updated, allowing drivers to
+ * perform any needed action.
+ */
+void drm_atomic_sro_install_state(struct drm_atomic_sro_state *state)
+{
+       unsigned int i;
+
+       for (i = 0; i < state->dev->mode_config.num_connector; i++) {
+               struct drm_connector *conn = state->connectors[i].ptr;
+               const struct drm_connector_funcs *conn_funcs = conn->funcs;
+               struct drm_connector_state *conn_state =
+                       state->connectors[i].state;
+
+               if (conn->state) {
+                       conn_funcs->atomic_destroy_state(conn, conn->state);
+                       conn->state = NULL;
+               }
+
+               if (conn_funcs->atomic_sro_install_state)
+                       conn_funcs->atomic_sro_install_state(conn, conn_state);
+
+               conn->state = conn_state;
+               state->connectors[i].state = NULL;
+               state->connectors[i].ptr = NULL;
+               drm_connector_put(conn);
+       }
+
+       for (i = 0; i < state->dev->mode_config.num_crtc; i++) {
+               struct drm_crtc *crtc = state->crtcs[i].ptr;
+               const struct drm_crtc_funcs *crtc_funcs = crtc->funcs;
+               struct drm_crtc_state *crtc_state = state->crtcs[i].state;
+
+               if (crtc->state) {
+                       crtc_funcs->atomic_destroy_state(crtc, crtc->state);
+                       crtc->state = NULL;
+               }
+
+               if (crtc_funcs->atomic_sro_install_state)
+                       crtc_funcs->atomic_sro_install_state(crtc, crtc_state);
+
+               crtc->state = crtc_state;
+               state->crtcs[i].state = NULL;
+               state->crtcs[i].ptr = NULL;
+       }
+
+       for (i = 0; i < state->dev->mode_config.num_total_plane; i++) {
+               struct drm_plane *plane = state->planes[i].ptr;
+               const struct drm_plane_funcs *plane_funcs = plane->funcs;
+               struct drm_plane_state *plane_state = state->planes[i].state;
+
+               if (plane->state) {
+                       plane_funcs->atomic_destroy_state(plane, plane->state);
+                       plane->state = NULL;
+               }
+
+               if (plane_funcs->atomic_sro_install_state)
+                       plane_funcs->atomic_sro_install_state(plane,
+                                                             plane_state);
+
+               plane->state = plane_state;
+               state->planes[i].state = NULL;
+               state->planes[i].ptr = NULL;
+       }
+
+       for (i = 0; i < count_private_obj(state->dev); i++) {
+               struct drm_private_obj *obj = state->private_objs[i].ptr;
+               const struct drm_private_state_funcs *obj_funcs = obj->funcs;
+               struct drm_private_state *obj_state =
+                       state->private_objs[i].state;
+
+               if (obj->state) {
+                       obj_funcs->atomic_destroy_state(obj, obj->state);
+                       obj->state = NULL;
+               }
+
+               if (obj_funcs->atomic_sro_install_state)
+                       obj_funcs->atomic_sro_install_state(obj,
+                                                             obj_state);
+
+               obj->state = obj_state;
+               state->private_objs[i].state = NULL;
+               state->private_objs[i].ptr = NULL;
+       }
+       state->num_private_objs = 0;
+}
+EXPORT_SYMBOL(drm_atomic_sro_install_state);
diff --git a/include/drm/drm_atomic.h b/include/drm/drm_atomic.h
index 657f5f4c223f..0e5c87f258ef 100644
--- a/include/drm/drm_atomic.h
+++ b/include/drm/drm_atomic.h
@@ -321,10 +321,28 @@ struct drm_private_state_funcs {
         */
        int (*atomic_sro_readout_state)(struct drm_private_obj *obj,
                                        struct drm_atomic_sro_state *state,
                                        struct drm_private_state *obj_state);
 
+       /**
+        * @atomic_sro_install_state:
+        *
+        * This optional hook is called when a state read out from
+        * hardware is about to be installed as the private object's
+        * current state.
+        *
+        * It allows drivers to acquire the resources needed to keep
+        * the current hardware state active, such as power domains,
+        * clocks, or interrupts.
+        *
+        * This hook cannot fail. It is called during
+        * drm_atomic_sro_install_state(), which is part of the
+        * hardware state readout initialization sequence.
+        */
+       void (*atomic_sro_install_state)(struct drm_private_obj *obj,
+                                        struct drm_private_state *obj_state);
+
        /**
         * @atomic_print_state:
         *
         * If driver subclasses &struct drm_private_state, it should implement
         * this optional hook for printing additional driver specific state.
diff --git a/include/drm/drm_atomic_sro.h b/include/drm/drm_atomic_sro.h
index 6e5262384c71..195154850ab4 100644
--- a/include/drm/drm_atomic_sro.h
+++ b/include/drm/drm_atomic_sro.h
@@ -48,6 +48,8 @@ drm_atomic_sro_get_private_obj_state(struct 
drm_atomic_sro_state *state,
                                     struct drm_private_obj *private_obj);
 void drm_atomic_sro_set_private_obj_state(struct drm_atomic_sro_state *state,
                                          struct drm_private_obj *obj,
                                          struct drm_private_state *obj_state);
 
+void drm_atomic_sro_install_state(struct drm_atomic_sro_state *state);
+
 #endif /* DRM_ATOMIC_SRO_H_ */
diff --git a/include/drm/drm_connector.h b/include/drm/drm_connector.h
index 7e2fda565f63..96ac5d472198 100644
--- a/include/drm/drm_connector.h
+++ b/include/drm/drm_connector.h
@@ -1686,10 +1686,28 @@ struct drm_connector_funcs {
         */
        int (*atomic_sro_readout_state)(struct drm_connector *connector,
                                        struct drm_atomic_sro_state *state,
                                        struct drm_connector_state *conn_state);
 
+       /**
+        * @atomic_sro_install_state:
+        *
+        * This optional hook is called when a state read out from
+        * hardware is about to be installed as the connector's current
+        * state.
+        *
+        * It allows drivers to acquire the resources needed to keep
+        * the current hardware state active, such as power domains,
+        * clocks, or interrupts.
+        *
+        * This hook cannot fail. It is called during
+        * drm_atomic_sro_install_state(), which is part of the
+        * hardware state readout initialization sequence.
+        */
+       void (*atomic_sro_install_state)(struct drm_connector *conn,
+                                        struct drm_connector_state 
*conn_state);
+
        /**
         * @atomic_duplicate_state:
         *
         * Duplicate the current atomic state for this connector and return it.
         * The core and helpers guarantee that any atomic state duplicated with
diff --git a/include/drm/drm_crtc.h b/include/drm/drm_crtc.h
index fde0e5f87665..4689a4a68c28 100644
--- a/include/drm/drm_crtc.h
+++ b/include/drm/drm_crtc.h
@@ -678,10 +678,28 @@ struct drm_crtc_funcs {
         */
        int (*atomic_sro_readout_state)(struct drm_crtc *crtc,
                                        struct drm_atomic_sro_state *state,
                                        struct drm_crtc_state *crtc_state);
 
+       /**
+        * @atomic_sro_install_state:
+        *
+        * This optional hook is called when a state read out from
+        * hardware is about to be installed as the CRTC's current
+        * state.
+        *
+        * It allows drivers to acquire the resources needed to keep
+        * the current hardware state active, such as power domains,
+        * clocks, or interrupts.
+        *
+        * This hook cannot fail. It is called during
+        * drm_atomic_sro_install_state(), which is part of the
+        * hardware state readout initialization sequence.
+        */
+       void (*atomic_sro_install_state)(struct drm_crtc *crtc,
+                                        struct drm_crtc_state *crtc_state);
+
        /**
         * @atomic_duplicate_state:
         *
         * Duplicate the current atomic state for this CRTC and return it.
         * The core and helpers guarantee that any atomic state duplicated with
diff --git a/include/drm/drm_plane.h b/include/drm/drm_plane.h
index d841fee3a9cd..358c4f4062b4 100644
--- a/include/drm/drm_plane.h
+++ b/include/drm/drm_plane.h
@@ -428,10 +428,28 @@ struct drm_plane_funcs {
         */
        int (*atomic_sro_readout_state)(struct drm_plane *plane,
                                        struct drm_atomic_sro_state *state,
                                        struct drm_plane_state *plane_state);
 
+       /**
+        * @atomic_sro_install_state:
+        *
+        * This optional hook is called when a state read out from
+        * hardware is about to be installed as the plane's current
+        * state.
+        *
+        * It allows drivers to acquire the resources needed to keep
+        * the current hardware state active, such as power domains,
+        * clocks, or interrupts.
+        *
+        * This hook cannot fail. It is called during
+        * drm_atomic_sro_install_state(), which is part of the
+        * hardware state readout initialization sequence.
+        */
+       void (*atomic_sro_install_state)(struct drm_plane *plane,
+                                        struct drm_plane_state *plane_state);
+
        /**
         * @atomic_duplicate_state:
         *
         * Duplicate the current atomic state for this plane and return it.
         * The core and helpers guarantee that any atomic state duplicated with

-- 
2.54.0

Reply via email to