drm/i915: Factor out a helper for handling atomic modeset locks/state
authorImre Deak <imre.deak@intel.com>
Wed, 10 May 2023 10:31:29 +0000 (13:31 +0300)
committerImre Deak <imre.deak@intel.com>
Tue, 16 May 2023 13:53:49 +0000 (16:53 +0300)
This patch simplifying the handling of modeset locks and atomic state
for an atomic commit is based on

https://lore.kernel.org/all/20210715184954.7794-2-ville.syrjala@linux.intel.com/

adding the helper to i915. I find this approach preferrable than
open-coding the corresponding steps (fixed for me an atomic
state reset during a DEADLK retry, which I missed in the open-coded
version) and also better than the existing
DRM_MODESET_LOCK_ALL_BEGIN/END macros for the reasons described in the
above original patchset.

This change takes the helper into use only for atomic commits during DDI
hotplug handling, as a preparation for a follow-up patch adding a
similar commit started from the same spot. Other places doing a
driver-internal atomic commit is to be converted by a follow-up
patchset.

Cc: Ville Syrjälä <ville.syrjala@linux.intel.com>
Reviewed-by: Ville Syrjälä <ville.syrjala@linux.intel.com>
Signed-off-by: Imre Deak <imre.deak@intel.com>
Link: https://patchwork.freedesktop.org/patch/msgid/20230510103131.1618266-13-imre.deak@intel.com
drivers/gpu/drm/i915/Makefile
drivers/gpu/drm/i915/display/intel_ddi.c
drivers/gpu/drm/i915/display/intel_modeset_lock.c [new file with mode: 0644]
drivers/gpu/drm/i915/display/intel_modeset_lock.h [new file with mode: 0644]

index 1f4c9b99c2c9654a828feeccdee095374390c44d..e3b699e7a755f1dc8dc5045f9867e208cf722eaf 100644 (file)
@@ -265,6 +265,7 @@ i915-y += \
        display/intel_hti.o \
        display/intel_load_detect.o \
        display/intel_lpe_audio.o \
+       display/intel_modeset_lock.o \
        display/intel_modeset_verify.o \
        display/intel_modeset_setup.o \
        display/intel_overlay.o \
index db056bb084d1654cecaa29f9cb4525b121e772ac..b8ffbbbb46426b58aeb5dda69f532860fd05f9e9 100644 (file)
@@ -63,6 +63,7 @@
 #include "intel_hti.h"
 #include "intel_lspcon.h"
 #include "intel_mg_phy_regs.h"
+#include "intel_modeset_lock.h"
 #include "intel_pps.h"
 #include "intel_psr.h"
 #include "intel_quirks.h"
@@ -4401,26 +4402,14 @@ intel_ddi_hotplug(struct intel_encoder *encoder,
 
        state = intel_encoder_hotplug(encoder, connector);
 
-       drm_modeset_acquire_init(&ctx, 0);
-
-       for (;;) {
+       intel_modeset_lock_ctx_retry(&ctx, NULL, 0, ret) {
                if (connector->base.connector_type == DRM_MODE_CONNECTOR_HDMIA)
                        ret = intel_hdmi_reset_link(encoder, &ctx);
                else
                        ret = intel_dp_retrain_link(encoder, &ctx);
-
-               if (ret == -EDEADLK) {
-                       drm_modeset_backoff(&ctx);
-                       continue;
-               }
-
-               break;
        }
 
-       drm_modeset_drop_locks(&ctx);
-       drm_modeset_acquire_fini(&ctx);
-       drm_WARN(encoder->base.dev, ret,
-                "Acquiring modeset locks failed with %i\n", ret);
+       drm_WARN_ON(encoder->base.dev, ret);
 
        /*
         * Unpowered type-c dongles can take some time to boot and be
diff --git a/drivers/gpu/drm/i915/display/intel_modeset_lock.c b/drivers/gpu/drm/i915/display/intel_modeset_lock.c
new file mode 100644 (file)
index 0000000..8fb6fd8
--- /dev/null
@@ -0,0 +1,50 @@
+// SPDX-License-Identifier: MIT
+/*
+ * Copyright © 2023 Intel Corporation
+ */
+
+#include <drm/drm_modeset_lock.h>
+
+#include "intel_display_types.h"
+#include "intel_modeset_lock.h"
+
+void _intel_modeset_lock_begin(struct drm_modeset_acquire_ctx *ctx,
+                              struct intel_atomic_state *state,
+                              unsigned int flags, int *ret)
+{
+       drm_modeset_acquire_init(ctx, flags);
+
+       if (state)
+               state->base.acquire_ctx = ctx;
+
+       *ret = -EDEADLK;
+}
+
+bool _intel_modeset_lock_loop(int *ret)
+{
+       if (*ret == -EDEADLK) {
+               *ret = 0;
+               return true;
+       }
+
+       return false;
+}
+
+void _intel_modeset_lock_end(struct drm_modeset_acquire_ctx *ctx,
+                            struct intel_atomic_state *state,
+                            int *ret)
+{
+       if (*ret == -EDEADLK) {
+               if (state)
+                       drm_atomic_state_clear(&state->base);
+
+               *ret = drm_modeset_backoff(ctx);
+               if (*ret == 0) {
+                       *ret = -EDEADLK;
+                       return;
+               }
+       }
+
+       drm_modeset_drop_locks(ctx);
+       drm_modeset_acquire_fini(ctx);
+}
diff --git a/drivers/gpu/drm/i915/display/intel_modeset_lock.h b/drivers/gpu/drm/i915/display/intel_modeset_lock.h
new file mode 100644 (file)
index 0000000..edb5099
--- /dev/null
@@ -0,0 +1,33 @@
+/* SPDX-License-Identifier: MIT */
+/*
+ * Copyright © 2023 Intel Corporation
+ */
+
+#ifndef __INTEL_MODESET_LOCK_H__
+#define __INTEL_MODESET_LOCK_H__
+
+#include <linux/types.h>
+
+struct drm_modeset_acquire_ctx;
+struct intel_atomic_state;
+
+void _intel_modeset_lock_begin(struct drm_modeset_acquire_ctx *ctx,
+                              struct intel_atomic_state *state,
+                              unsigned int flags,
+                              int *ret);
+bool _intel_modeset_lock_loop(int *ret);
+void _intel_modeset_lock_end(struct drm_modeset_acquire_ctx *ctx,
+                            struct intel_atomic_state *state,
+                            int *ret);
+
+/*
+ * Note that one must always use "continue" rather than
+ * "break" or "return" to handle errors within the
+ * intel_modeset_lock_ctx_retry() block.
+ */
+#define intel_modeset_lock_ctx_retry(ctx, state, flags, ret) \
+       for (_intel_modeset_lock_begin((ctx), (state), (flags), &(ret)); \
+            _intel_modeset_lock_loop(&(ret)); \
+            _intel_modeset_lock_end((ctx), (state), &(ret)))
+
+#endif /* __INTEL_MODESET_LOCK_H__ */