The states of an atomic commit usually come from
intel_crtc_duplicate_state(), which carries the whole current state
over. The upcoming DRM_MODE_ATOMIC_RESET flag of the atomic ioctl
builds its commits out of states created through atomic_create_state()
instead: those come pristine, with only the uapi properties userspace
applied on top of the defaults.

A CRTC that stays enabled across such a commit, because userspace
re-applies the same configuration, takes the nomodeset path of
intel_atomic_check(): that path only updates the color management
part of the hw state and relies on everything else having been carried
over by the duplicate. Without the carry-over, the pipe mode, the
transcoder, the DPLL, the scaler bookkeeping and all the other derived
state would be zeroed on a CRTC the hardware keeps driving, and the
scaler staging would fail to find a free scaler for the planes still
using one.

Carry the current state over the created ones like a duplicate would,
keeping the uapi state userspace just configured. A created state is
told apart from a duplicated one through its invalid transcoder: the
state of an active CRTC always holds a valid one, a duplicate carries
it over, and a created state comes reset.

The planes get the same treatment for their scaler assignment: the
scaler staging frees the scaler of a plane through the new state, and
assigns it one when it still needs scaling, both of which need the
assignment the current state holds. Carry the scaler id over when the
current state has one and the new state doesn't.

Signed-off-by: Maxime Ripard <[email protected]>
---
 drivers/gpu/drm/i915/display/intel_display.c | 114 +++++++++++++++++++++++++++
 1 file changed, 114 insertions(+)

diff --git a/drivers/gpu/drm/i915/display/intel_display.c 
b/drivers/gpu/drm/i915/display/intel_display.c
index 1172e48daf23..d645203104a7 100644
--- a/drivers/gpu/drm/i915/display/intel_display.c
+++ b/drivers/gpu/drm/i915/display/intel_display.c
@@ -6517,10 +6517,120 @@ static int intel_atomic_check_config_and_link(struct 
intel_atomic_state *state)
                        break;
        }
 
        return ret;
 }
+/*
+ * intel_atomic_carry_over_state - carry the current state over created states
+ * @state: atomic state
+ *
+ * The states of a commit usually come from intel_crtc_duplicate_state(),
+ * which carries the whole current state over. The upcoming
+ * DRM_MODE_ATOMIC_RESET flag of the atomic ioctl builds its commits out
+ * of states created through atomic_create_state() instead: those come
+ * pristine, with only the uapi properties userspace applied on top of
+ * the defaults.
+ *
+ * A CRTC that stays enabled across such a commit, because userspace
+ * re-applies the same configuration, takes the nomodeset path of
+ * intel_atomic_check(): that path only updates the color management
+ * part of the hw state and relies on everything else having been
+ * carried over by the duplicate. Take the current state over like a
+ * duplicate would, keeping the uapi state userspace just configured:
+ * without it, the pipe mode, the transcoder, the DPLL, the scaler
+ * bookkeeping and all the other derived state would be zeroed on a
+ * CRTC the hardware keeps driving.
+ *
+ * A created state is told apart from a duplicated one through its
+ * invalid transcoder: the state of an active CRTC always holds a valid
+ * one, a duplicate carries it over, and a created state comes reset.
+ *
+ * The planes need the same for their scaler assignment: the scaler
+ * staging frees the scaler of a plane through the new state, and
+ * assigns it one when it still needs scaling, both of which need the
+ * assignment the current state holds. A created plane state comes with
+ * none, and with the scaler still marked in use in the carried CRTC
+ * state, it could neither be freed nor replaced.
+ */
+static int intel_atomic_carry_over_state(struct intel_atomic_state *state)
+{
+       struct intel_crtc_state *old_crtc_state, *new_crtc_state;
+       struct intel_plane_state *old_plane_state, *new_plane_state;
+       struct intel_crtc *crtc;
+       struct intel_plane *plane;
+       int i;
+
+       for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
+                                           new_crtc_state) {
+               struct intel_crtc_state *carried_state;
+
+               if (new_crtc_state->cpu_transcoder != INVALID_TRANSCODER ||
+                   !old_crtc_state->hw.active)
+                       continue;
+
+               carried_state = kmemdup(old_crtc_state, sizeof(*carried_state),
+                                       GFP_KERNEL);
+               if (!carried_state)
+                       return -ENOMEM;
+
+               /* Keep the uapi state userspace just configured. */
+               carried_state->uapi = new_crtc_state->uapi;
+
+               intel_crtc_free_hw_state(new_crtc_state);
+               if (new_crtc_state->dp_tunnel_ref.tunnel)
+                       drm_dp_tunnel_ref_put(&new_crtc_state->dp_tunnel_ref);
+
+               memcpy(new_crtc_state, carried_state, sizeof(*new_crtc_state));
+               kfree(carried_state);
+
+               /* Take the references a duplicated state would carry. */
+               if (new_crtc_state->hw.degamma_lut)
+                       drm_property_blob_get(new_crtc_state->hw.degamma_lut);
+               if (new_crtc_state->hw.ctm)
+                       drm_property_blob_get(new_crtc_state->hw.ctm);
+               if (new_crtc_state->hw.gamma_lut)
+                       drm_property_blob_get(new_crtc_state->hw.gamma_lut);
+
+               if (new_crtc_state->pre_csc_lut)
+                       drm_property_blob_get(new_crtc_state->pre_csc_lut);
+               if (new_crtc_state->post_csc_lut)
+                       drm_property_blob_get(new_crtc_state->post_csc_lut);
+
+               if (new_crtc_state->dp_tunnel_ref.tunnel)
+                       
drm_dp_tunnel_ref_get(new_crtc_state->dp_tunnel_ref.tunnel,
+                                             &new_crtc_state->dp_tunnel_ref);
+
+               /* Clear the transient flags, like a duplicate would. */
+               new_crtc_state->update_pipe = false;
+               new_crtc_state->update_m_n = false;
+               new_crtc_state->update_lrr = false;
+               new_crtc_state->disable_cxsr = false;
+               new_crtc_state->update_wm_pre = false;
+               new_crtc_state->update_wm_post = false;
+               new_crtc_state->fifo_changed = false;
+               new_crtc_state->preload_luts = false;
+               new_crtc_state->wm.need_postvbl_update = false;
+               new_crtc_state->do_async_flip = false;
+               new_crtc_state->fb_bits = 0;
+               new_crtc_state->update_planes = 0;
+               new_crtc_state->dsb_color = NULL;
+               new_crtc_state->dsb_commit = NULL;
+               new_crtc_state->use_dsb = false;
+               new_crtc_state->use_flipq = false;
+       }
+
+       for_each_oldnew_intel_plane_in_state(state, plane, old_plane_state,
+                                             new_plane_state, i) {
+               if (new_plane_state->scaler_id < 0 &&
+                   old_plane_state->scaler_id >= 0)
+                       new_plane_state->scaler_id =
+                               old_plane_state->scaler_id;
+       }
+
+       return 0;
+}
+
 /**
  * intel_atomic_check - validate state object
  * @dev: drm device
  * @_state: state to validate
  */
@@ -6534,10 +6644,14 @@ int intel_atomic_check(struct drm_device *dev,
        int ret;
 
        if (!intel_display_driver_check_access(display))
                return -ENODEV;
 
+       ret = intel_atomic_carry_over_state(state);
+       if (ret)
+               goto fail;
+
        for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state, 
new_crtc_state) {
                /*
                 * crtc's state no longer considered to be inherited
                 * after the first userspace/client initiated commit.
                 */

-- 
2.56.1

Reply via email to