Skip to contentSkip to conversation

drm: Implement state readout support

PATCH In review Maxime Ripard 4 participants 66 messages Nov 20 – Nov 25

Subsystems (16)
lore
Share & tools
lore

Maxime Ripard

author maintainer
#0 lore raw

v9 cover drm: Implement state readout support

Hi,

Here's a series that implement what i915 calls "fastboot", ie, initializing the initial KMS state from the hardware state at boot, to skip the first modeset if the firmware already set up the display.

This series creates the infrastructure in KMS to create that state by relying on driver specific hooks. It also implements some infrastructure to check during non-blocking commits that the readout helpers work properly by reading out the state that was just committed and comparing it to what was supposed to be committed.

This relies on another set of driver hooks to compare the entities states, with helpers providing the default implementation.

It then implements the readout support in the TIDSS driver, and was tested with the SK-AM62 board. This board in particular is pretty interesting, since it relies on an DPI to HDMI bridge, and uses the drm_bridge_connector infrastructure.

So the readout works with the current state of the art on embedded-ish platforms.

Unlike the earlier RFCs, it now works fine: the state is picked up properly, doesn't trigger a modeset if what was programmed is the one the first modeset tries to pick as well, will switch properly if it isn't, etc.

Let me know what you think,
Maxime

---
Changes in v9:
- Add drm_atomic_get_connector_for_encoder_locked to make locks handling
easier
- Drop drm_atomic_private_obj_set_name()
- Turn drm_atomic_sro_install_state into a hook and a helper
- Clarify bridge bus flags and formats expectations
- Add bridge install_state hook
- Fix a whole bunch of sashiko issues
- Ease the framebuffer comparisons, since it's often impossible to
readout the offsets, width and height from the registers
- Ease the plane comparisons since it's often impossible to readout the
src_x and src_y coordinates
- Ease the plane zpos comparison and only compare one of zpos or normalized_zpos, dependending on mode_config.normalize_zpos value
- Only compare connector states if both states are bound to a crtc
- Clarify vblank expectations for the CRTCs, and add unit tests
- Split the tidss readout support into multiple patches to ease review
- Link to v8: https://lore.kernel.org/r/[email protected]

Changes in v8:
- Convert ingenic to properly allocated bridge
- Improve locking
- Export more functions needed to compile helpers as modules
- Fix idempotency of drm_atomic_private_obj_fini
- Fix DP-MST private objects name
- Handle framebuffers without a GEM object in drm_gem_plane_helper_prepare_fb
- Improve SRO container accessors safety
- Add documentation for kernel parameter
- Drop some easy to trigger WARN calls
- Handle plane, connector and encoder masks better when doing a partial
update (and readout)
- Only compare the states if they are both active, other comparisons improvements
- More kunit tests to test kernel parameter behaviour, DPMS
- Fix memomry leaks in kunit tests
- Fix bridge_connector of bridges with and whithout SRO support
- Improve runtime_pm handling for tidss hw_reset
- Tentative support for tidss OLDI and multiple plane and CRTCs
- Improve error handling for sii902x
- Implement readout state for panel bridge
- Link to v7: https://lore.kernel.org/r/[email protected]

Changes in v7:
- Add kunit tests for state readout handling
- Fix kunit test breakages
- Fix sashiko reviews
- Rename drm_atomic_sro.c to drm_atomic_state_readout.c
- Improve documentation
- Fix comparisons macros and functions
- Improve tidss readout implementation
- When committing after a commit, only readout and compare the objects
part of that commit (and we have the locks for)
- Add kunit tests for the read-out logic
- Improve tidss read-out implementation
- Improve si902x read-out implementation
- Link to v6: https://lore.kernel.org/r/[email protected]

Changes in v6:
- Fix private_obj_init error handling
- Fix bridge name lifetime
- Show the bridge name in print_state
- Rebase on current drm-misc-next
- Link to v5: https://lore.kernel.org/r/[email protected]

Changes in v5:
- Implement readout for bridge/display-connector
- Implement readout for tidss encoders
- Fix sii902 readout implementation
- Add atomic_print_state implementation to tidss crtc
- Rebase over latest changes. In particular, the CRTC conversion to atomic_create_state and the introduction of crtc's hw_reset made me
create a new helper and hook to trigger a device-wide hw_reset
- Add a bunch of NULL pointer checks everywhere
- Fix compare macros calls to __stringify
- Create documentation section for sro
- Fix error path in drm_atomic_sro_state_init
- Improve bisectability
- Link to v4: https://lore.kernel.org/r/[email protected]

Changes in v4:
- Dropped the HACK patch that went into the v3 resend
- Hopefully the kernel.org smtp works this time
- Link to v3: https://lore.kernel.org/r/[email protected]

Changes in v3:
- Rebase on top of the current drm-misc-next, with rename of drm_atomic_state and the various preliminary series merged.
- Use drm_printf_indent when possible
- Change the access mode for the atomic_readout parameter from 000 to 400
- Use drm_drv_uses_atomic_modeset() instead of drm_core_check_feature()
- Introduce a hw_reset hook to deal with device hardware reset when needed (ie, SRO fails or is disabled)
- Improve mode flags readout
- Fix plane alpha readout
- Add sii902x state readout

- Link to v2: https://lore.kernel.org/r/[email protected]

Changes in v2:
- Get rid of patches already applied
- Rebase on top of the current atomic_create_state work
- Use the SRO prefix everywhere
- Create our own states container structure instead of trying to plumb
it into a drm_atomic_state
- Make a lot more use of helpers
- Add a hook to enable the hardware resources when the readout state
becomes active
- Move all the tidss readout code into tidss_dispc.c
- Write documentation
- Add drm_private_obj and drm_bridge name for easier debugging
- Add drm_private_obj_is_bridge()
- Link to v1: https://lore.kernel.org/r/[email protected]

---
Maxime Ripard (42):
drm/atomic: Add drm_atomic_get_connector_for_encoder_locked() drm/bridge-helper: Don't unlock the connection mutex after a backoff drm/tests: hdmi-state-helper: Handle EDEADLK in the 420-only test drm/tests: hdmi-state-helper: Handle EDEADLK in the color format test drm/colorop: Export drm_atomic_helper_colorop_create_state() drm/atomic: Fix private obj error handling
drm/atomic: Add drm_private_obj name
drm/bridge: Switch print_state to use bridge name
drm/bridge: Add drm_private_obj_is_bridge()
drm/fb-dma-helper: Handle framebuffers without a GEM object drm/gem-atomic-helper: Handle framebuffers without a GEM object drm/atomic: Export drm_atomic_*_print_state
drm/atomic_sro: Create drm_atomic_sro_state container drm/atomic_sro: Create kernel parameter to force or disable readout drm/atomic_sro: Add atomic state readout infrastructure drm/atomic_sro: Create documentation
drm/bridge: Handle bridges with hardware state readout drm/mode-config: Read out hardware state in drm_mode_config_create_initial_state drm/atomic_sro: Provide helpers to implement hardware state readout drm/mode-config: Introduce hw_reset hook
drm/modeset-helper: Add drm_mode_config_helper_hw_reset() drm/atomic_helper: Compare actual and readout states once the commit is done drm/atomic-state-helper: Provide comparison macros drm/atomic-state-helper: Provide atomic_compare_state helpers drm/atomic-helper: Document State Read-Out as a state initialization approach drm/tests: kunit: Add a drm_printer helper
drm/tests: Add tests for the atomic state readout
drm/encoder: Create atomic_sro_get_current_crtc hook
drm/bridge: display-connector: Implement readout support drm/bridge_connector: Implement hw readout for connector drm/tidss: Implement hw_reset
drm/tidss: crtc: Implement hardware state readout
drm/tidss: encoder: Implement hardware state readout
drm/tidss: kms: Hook up hardware state readout
drm/tidss: plane: Implement hardware state readout
drm/tidss: dispc: Add hardware state readout helpers
drm/tidss: encoder: Implement atomic_sro_get_current_crtc drm/bridge: sii902x: Implement hw state readout
drm/panel: Implement state readout support
[DO NOT MERGE] fixup! drm/tidss: dispc: Add hardware state readout helpers [DO NOT MERGE] drm/tidss: oldi: Implement hardware state readout [HACK] drm/panel: Prepare and enable panels read out as active

 Documentation/admin-guide/kernel-parameters.txt    |   12 +
 Documentation/gpu/drm-kms-helpers.rst              |   26 +
 drivers/gpu/drm/Makefile                           |    2 +
 drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm.c  |    1 +
 .../drm/arm/display/komeda/komeda_private_obj.c    |    8 +
 drivers/gpu/drm/bridge/display-connector.c         |   75 +
 drivers/gpu/drm/bridge/sii902x.c                   |  130 +-
 drivers/gpu/drm/display/drm_bridge_connector.c     |   65 +-
 drivers/gpu/drm/display/drm_dp_mst_topology.c      |    2 +-
 drivers/gpu/drm/display/drm_dp_tunnel.c            |   15 +-
 drivers/gpu/drm/drm_atomic.c                       |  119 +-
 drivers/gpu/drm/drm_atomic_helper.c                |   45 +
 drivers/gpu/drm/drm_atomic_state_helper.c          |   20 +-
 drivers/gpu/drm/drm_atomic_state_readout.c         |  909 +++++++++
 drivers/gpu/drm/drm_atomic_state_readout_helper.c  | 1360 ++++++++++++++
 drivers/gpu/drm/drm_bridge.c                       |  180 +-
 drivers/gpu/drm/drm_bridge_helper.c                |   31 +-
 drivers/gpu/drm/drm_colorop.c                      |    1 +
 drivers/gpu/drm/drm_fb_dma_helper.c                |   10 +
 drivers/gpu/drm/drm_gem_atomic_helper.c            |   14 +-
 drivers/gpu/drm/drm_internal.h                     |   13 +
 drivers/gpu/drm/drm_mode_config.c                  |   75 +-
 drivers/gpu/drm/drm_modeset_helper.c               |   31 +
 drivers/gpu/drm/drm_panel.c                        |  152 ++
 drivers/gpu/drm/ingenic/ingenic-drm-drv.c          |    1 +
 drivers/gpu/drm/ingenic/ingenic-ipu.c              |    1 +
 drivers/gpu/drm/msm/disp/dpu1/dpu_kms.c            |    1 +
 drivers/gpu/drm/msm/disp/mdp5/mdp5_kms.c           |    1 +
 drivers/gpu/drm/omapdrm/omap_drv.c                 |    1 +
 drivers/gpu/drm/tegra/hub.c                        |    1 +
 drivers/gpu/drm/tests/Makefile                     |    1 +
 .../gpu/drm/tests/drm_atomic_state_readout_test.c  | 1934 ++++++++++++++++++++
 drivers/gpu/drm/tests/drm_hdmi_state_helper_test.c |   30 +-
 drivers/gpu/drm/tests/drm_kunit_helpers.c          |   78 +
 drivers/gpu/drm/tidss/tidss_crtc.c                 |  132 ++
 drivers/gpu/drm/tidss/tidss_dispc.c                |  504 ++++-
 drivers/gpu/drm/tidss/tidss_dispc.h                |   24 +
 drivers/gpu/drm/tidss/tidss_encoder.c              |   57 +
 drivers/gpu/drm/tidss/tidss_kms.c                  |   21 +-
 drivers/gpu/drm/tidss/tidss_oldi.c                 |  157 ++
 drivers/gpu/drm/tidss/tidss_plane.c                |  224 +++
 drivers/gpu/drm/vc4/vc4_kms.c                      |    3 +
 include/drm/drm_atomic.h                           |   81 +
 include/drm/drm_atomic_state_readout.h             |  241 +++
 include/drm/drm_atomic_state_readout_helper.h      |  284 +++
 include/drm/drm_bridge.h                           |  135 ++
 include/drm/drm_connector.h                        |   75 +
 include/drm/drm_crtc.h                             |   80 +
 include/drm/drm_encoder.h                          |   18 +
 include/drm/drm_kunit_helpers.h                    |    4 +
 include/drm/drm_mode_config.h                      |   62 +
 include/drm/drm_modeset_helper.h                   |    1 +
 include/drm/drm_modeset_helper_vtables.h           |   63 +
 include/drm/drm_plane.h                            |   75 +
 54 files changed, 7467 insertions(+), 119 deletions(-)

---
base-commit: e6e635c4407e5bb8ddc6017a2e9825c17240e799
change-id: 20250730-drm-state-readout-108f089c1c30
prerequisite-patch-id: f8c3385d46e2be7cba68c715e31f1b3ea80a45eb

Best regards,

Signature · 1 line
Maxime Ripard <[email protected]>
author maintainer
#1 lore raw

PATCH v9 1/42 drm/atomic: Add drm_atomic_get_connector_for_encoder_locked()

drm_atomic_get_connector_for_encoder() takes the connection mutex through the acquire context it is given, looks up the connector routed to an encoder, and releases the mutex before returning.

However, callers that need to look at the state of the connector they just found have to take the connection mutex again afterwards, which leaves a window where a concurrent commit can re-route the connector. And if the caller already held the connection mutex through the same acquire context, drm_atomic_get_connector_for_encoder() releases it from under it.

Split the lookup into drm_atomic_get_connector_for_encoder_locked(), which expects the caller to hold the connection mutex, and make drm_atomic_get_connector_for_encoder() a wrapper around it that takes and releases the mutex.

---
 drivers/gpu/drm/drm_atomic.c | 57 ++++++++++++++++++++++++++++++++++----------
 include/drm/drm_atomic.h     |  2 ++
 2 files changed, 46 insertions(+), 13 deletions(-)
drivers/gpu/drm/drm_atomic.c +44 -13
index 81591173265d..d2a46ba7394f 100644
@@ -1289,10 +1289,52 @@ drm_atomic_get_new_connector_for_encoder(const struct drm_atomic_commit *state,
 
 	return NULL;
 }
 EXPORT_SYMBOL(drm_atomic_get_new_connector_for_encoder);
 
+/**
+ * drm_atomic_get_connector_for_encoder_locked - Get connector currently assigned to an encoder
+ * @encoder: The encoder to find the connector of
+ *
+ * This function finds and returns the connector currently assigned to
+ * an @encoder.
+ *
+ * It is similar to drm_atomic_get_connector_for_encoder(), but expects
+ * the caller to hold &drm_mode_config.connection_mutex. This is useful
+ * for callers that need to keep looking at the connector state once the
+ * connector has been found, since the connection mutex must be held for
+ * the whole sequence.
+ *
+ * Returns:
+ * The connector connected to @encoder, or an error pointer otherwise.
+ */
+struct drm_connector *
+drm_atomic_get_connector_for_encoder_locked(const struct drm_encoder *encoder)
+{
+	struct drm_connector_list_iter conn_iter;
+	struct drm_connector *out_connector = ERR_PTR(-EINVAL);
+	struct drm_connector *connector;
+	struct drm_device *dev = encoder->dev;
+
+	drm_modeset_lock_assert_held(&dev->mode_config.connection_mutex);
+
+	drm_connector_list_iter_begin(dev, &conn_iter);
+	drm_for_each_connector_iter(connector, &conn_iter) {
+		if (!connector->state)
+			continue;
+
+		if (encoder == connector->state->best_encoder) {
+			out_connector = connector;
+			break;
+		}
+	}
+	drm_connector_list_iter_end(&conn_iter);
+
+	return out_connector;
+}
+EXPORT_SYMBOL(drm_atomic_get_connector_for_encoder_locked);
+
 /**
  * drm_atomic_get_connector_for_encoder - Get connector currently assigned to an encoder
  * @encoder: The encoder to find the connector of
  * @ctx: Modeset locking context
  *
@@ -1314,31 +1356,20 @@ EXPORT_SYMBOL(drm_atomic_get_new_connector_for_encoder);
  */
 struct drm_connector *
 drm_atomic_get_connector_for_encoder(const struct drm_encoder *encoder,
 				     struct drm_modeset_acquire_ctx *ctx)
 {
-	struct drm_connector_list_iter conn_iter;
-	struct drm_connector *out_connector = ERR_PTR(-EINVAL);
-	struct drm_connector *connector;
+	struct drm_connector *out_connector;
 	struct drm_device *dev = encoder->dev;
 	int ret;
 
 	ret = drm_modeset_lock(&dev->mode_config.connection_mutex, ctx);
 	if (ret)
 		return ERR_PTR(ret);
 
-	drm_connector_list_iter_begin(dev, &conn_iter);
-	drm_for_each_connector_iter(connector, &conn_iter) {
-		if (!connector->state)
-			continue;
+	out_connector = drm_atomic_get_connector_for_encoder_locked(encoder);
 
-		if (encoder == connector->state->best_encoder) {
-			out_connector = connector;
-			break;
-		}
-	}
-	drm_connector_list_iter_end(&conn_iter);
 	drm_modeset_unlock(&dev->mode_config.connection_mutex);
 
 	return out_connector;
 }
 EXPORT_SYMBOL(drm_atomic_get_connector_for_encoder);
include/drm/drm_atomic.h +2 -0
index 3ae35b09c0cf..c8d0f18fd0fd 100644
@@ -775,10 +775,12 @@ drm_atomic_get_old_connector_for_encoder(const struct drm_atomic_commit *state,
 					 struct drm_encoder *encoder);
 struct drm_connector *
 drm_atomic_get_new_connector_for_encoder(const struct drm_atomic_commit *state,
 					 struct drm_encoder *encoder);
 struct drm_connector *
+drm_atomic_get_connector_for_encoder_locked(const struct drm_encoder *encoder);
+struct drm_connector *
 drm_atomic_get_connector_for_encoder(const struct drm_encoder *encoder,
 				     struct drm_modeset_acquire_ctx *ctx);
 
 struct drm_crtc *
 drm_atomic_get_old_crtc_for_encoder(struct drm_atomic_commit *state,
Signature · 1 line
2.56.1
maintainer Reviewed-by on v9 1/42
#2 lore raw

Re: [PATCH v9 01/42] drm/atomic: Add drm_atomic_get_connector_for_encoder_locked()

Hi,

Quoted 16 lines · Maxime Ripard

drm_atomic_get_connector_for_encoder() takes the connection mutex through the acquire context it is given, looks up the connector routed to an encoder, and releases the mutex before returning.

However, callers that need to look at the state of the connector they just found have to take the connection mutex again afterwards, which leaves a window where a concurrent commit can re-route the connector. And if the caller already held the connection mutex through the same acquire context, drm_atomic_get_connector_for_encoder() releases it from under it.

Split the lookup into drm_atomic_get_connector_for_encoder_locked(), which expects the caller to hold the connection mutex, and make drm_atomic_get_connector_for_encoder() a wrapper around it that takes and releases the mutex.

Quoted 5 lines
---
  drivers/gpu/drm/drm_atomic.c | 57 ++++++++++++++++++++++++++++++++++----------
  include/drm/drm_atomic.h     |  2 ++
  2 files changed, 46 insertions(+), 13 deletions(-)

Tomi

author maintainer
#3 lore raw

PATCH v9 2/42 drm/bridge-helper: Don't unlock the connection mutex after a backoff

In reply to Maxime Ripard

drm_bridge_helper_reset_crtc() takes the connection mutex using the acquire context provided by the caller, and then relies on drm_atomic_get_connector_for_encoder() and drm_atomic_helper_reset_crtc() that both use the same context.

However, drm_atomic_get_connector_for_encoder() takes the connection mutex itself and unlocks it on its way out. The connection mutex taken by drm_bridge_helper_reset_crtc() was thus already released when the function unlocked it again on its exit path, and the connector state was looked at without holding it.

The callers already have to release the context locks through drm_modeset_drop_locks() and to handle the -EDEADLK error, so we can leave the connection mutex release to them. Look the connector up with drm_atomic_get_connector_for_encoder_locked() instead, so that the connection mutex stays held from the lookup until the CRTC reset, and make sure the connector is bound to a CRTC before resetting it.

Fixes 56ae62124177 ("drm/bridge: Add helper to reset bridge pipeline")
---
 drivers/gpu/drm/drm_bridge_helper.c | 31 +++++++++++++++++--------------
 1 file changed, 17 insertions(+), 14 deletions(-)
drivers/gpu/drm/drm_bridge_helper.c +17 -14
index 0a3c8fee66b3..53c051cb2d65 100644
@@ -17,10 +17,14 @@
  * between the CRTC and connector that bridge is connected to.
  *
  * As it relies on drm_atomic_helper_reset_crtc(), the same limitations
  * apply.
  *
+ * All the locks taken by this function use @ctx: the caller is
+ * responsible for releasing them with drm_modeset_drop_locks(), and
+ * for handling an -EDEADLK error with drm_modeset_backoff().
+ *
  * Returns:
  *
  * 0 on success or a negative error code on failure. If the error
  * returned is EDEADLK, the whole atomic sequence must be restarted.
  */
@@ -31,28 +35,27 @@ int drm_bridge_helper_reset_crtc(struct drm_bridge *bridge,
 	struct drm_encoder *encoder = bridge->encoder;
 	struct drm_device *dev = encoder->dev;
 	struct drm_crtc *crtc;
 	int ret;
 
+	/*
+	 * Hold the connection mutex across the connector lookup and
+	 * the accesses to its state, so that a concurrent commit can't
+	 * re-route the connector in between. Every lock taken with @ctx
+	 * is released by the caller through drm_modeset_drop_locks(),
+	 * so we must not unlock it ourselves.
+	 */
 	ret = drm_modeset_lock(&dev->mode_config.connection_mutex, ctx);
 	if (ret)
 		return ret;
 
-	connector = drm_atomic_get_connector_for_encoder(encoder, ctx);
-	if (IS_ERR(connector)) {
-		ret = PTR_ERR(connector);
-		goto out;
-	}
-
-	if (!connector->state) {
-		ret = -EINVAL;
-		goto out;
-	}
+	connector = drm_atomic_get_connector_for_encoder_locked(encoder);
+	if (IS_ERR(connector))
+		return PTR_ERR(connector);
 
 	crtc = connector->state->crtc;
-	ret = drm_atomic_helper_reset_crtc(crtc, ctx);
+	if (!crtc)
+		return -EINVAL;
 
-out:
-	drm_modeset_unlock(&dev->mode_config.connection_mutex);
-	return ret;
+	return drm_atomic_helper_reset_crtc(crtc, ctx);
 }
 EXPORT_SYMBOL(drm_bridge_helper_reset_crtc);
Signature · 1 line
2.56.1
author maintainer
#4 lore raw

PATCH v9 3/42 drm/tests: hdmi-state-helper: Handle EDEADLK in the 420-only test

In reply to Maxime Ripard

The atomic state API is allowed to return -EDEADLK whenever the wound/wait mutex code detects a deadlock, and the caller is then expected to back off and restart the whole sequence.

drm_test_check_hdmi_color_format_420_only() wasn't doing any of that, and reported a spurious failure when drm_atomic_set_crtc_for_connector() returned -EDEADLK. Since the ticket ordering depends on the execution, this could only be observed occasionally: the structurally identical drm_test_check_hdmi_color_format() test hasn't been observed to fail so far.

Add the usual backoff and restart pattern.

Fixes f7235a4805a4 ("drm/tests: hdmi: Add tests for the color_format property")
---
 drivers/gpu/drm/tests/drm_hdmi_state_helper_test.c | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)
drivers/gpu/drm/tests/drm_hdmi_state_helper_test.c +15 -1
index 336bb644cb7c..aafe78b7cbbd 100644
@@ -2398,35 +2398,49 @@ static void drm_test_check_hdmi_color_format_420_only(struct kunit *test)
 	KUNIT_ASSERT_NOT_NULL(test, dank);
 
 	state = drm_kunit_helper_atomic_state_alloc(test, &priv->drm, &ctx);
 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, state);
 
+retry:
 	conn_state = drm_atomic_get_connector_state(state, &priv->connector);
 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, conn_state);
 
 	conn_state->color_format = param->fmt;
 
 	ret = drm_atomic_set_crtc_for_connector(conn_state, priv->crtc);
+	if (ret == -EDEADLK)
+		goto backoff;
 	KUNIT_ASSERT_EQ(test, ret, 0);
 
 	crtc_state = drm_atomic_get_crtc_state(state, priv->crtc);
+	if (IS_ERR(crtc_state) && PTR_ERR(crtc_state) == -EDEADLK)
+		goto backoff;
 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, crtc_state);
 
 	ret = drm_atomic_set_mode_for_crtc(crtc_state, dank);
 	KUNIT_ASSERT_EQ(test, ret, 0);
 
 	crtc_state->enable = true;
 	crtc_state->active = true;
 
 	ret = drm_atomic_check_only(state);
+	if (ret == -EDEADLK)
+		goto backoff;
 	KUNIT_EXPECT_EQ(test, ret, param->expected_ret);
 	if (!param->expected_ret)
 		KUNIT_EXPECT_EQ(test, conn_state->hdmi.output_format, param->expected);
 
 	drm_modeset_drop_locks(&ctx);
 	drm_modeset_acquire_fini(&ctx);
-};
+
+	return;
+
+backoff:
+	drm_atomic_commit_clear(state);
+	drm_modeset_backoff(&ctx);
+	goto retry;
+}
 
 static const struct color_format_test_param hdmi_color_format_420_only_params[] = {
 	{
 		.fmt = DRM_CONNECTOR_COLOR_FORMAT_RGB444,
 		.expected = DRM_OUTPUT_COLOR_FORMAT_RGB444,
Signature · 1 line
2.56.1
author maintainer
#5 lore raw

PATCH v9 4/42 drm/tests: hdmi-state-helper: Handle EDEADLK in the color format test

In reply to Maxime Ripard

The atomic state API is allowed to return -EDEADLK whenever the wound/wait mutex code detects a deadlock, and the caller is then expected to back off and restart the whole sequence.

drm_test_check_hdmi_color_format() wasn't doing any of that, and reported a spurious failure when drm_atomic_set_crtc_for_connector() returned -EDEADLK, just like its 420-only counterpart fixed earlier. Since the ticket ordering depends on the execution, this could only be observed occasionally.

The assertion also aborted before the locking context could be released, leaving connection_mutex held when the device was released, which drm_modeset_lock_fini() then reported through drm_mode_config_cleanup().

Add the usual backoff and restart pattern.

Fixes f7235a4805a4 ("drm/tests: hdmi: Add tests for the color_format property")
---
 drivers/gpu/drm/tests/drm_hdmi_state_helper_test.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
drivers/gpu/drm/tests/drm_hdmi_state_helper_test.c +14 -0
index aafe78b7cbbd..8e3e9bb4a202 100644
@@ -2295,33 +2295,47 @@ static void drm_test_check_hdmi_color_format(struct kunit *test)
 	KUNIT_ASSERT_TRUE(test, drm_mode_is_420(info, preferred));
 
 	state = drm_kunit_helper_atomic_state_alloc(test, &priv->drm, &ctx);
 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, state);
 
+retry:
 	conn_state = drm_atomic_get_connector_state(state, &priv->connector);
 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, conn_state);
 
 	conn_state->color_format = param->fmt;
 
 	ret = drm_atomic_set_crtc_for_connector(conn_state, priv->crtc);
+	if (ret == -EDEADLK)
+		goto backoff;
 	KUNIT_ASSERT_EQ(test, ret, 0);
 
 	crtc_state = drm_atomic_get_crtc_state(state, priv->crtc);
+	if (IS_ERR(crtc_state) && PTR_ERR(crtc_state) == -EDEADLK)
+		goto backoff;
 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, crtc_state);
 
 	ret = drm_atomic_set_mode_for_crtc(crtc_state, preferred);
 	KUNIT_ASSERT_EQ(test, ret, 0);
 
 	crtc_state->enable = true;
 	crtc_state->active = true;
 
 	ret = drm_atomic_check_only(state);
+	if (ret == -EDEADLK)
+		goto backoff;
 	KUNIT_EXPECT_EQ(test, ret, param->expected_ret);
 	KUNIT_EXPECT_EQ(test, conn_state->hdmi.output_format, param->expected);
 
 	drm_modeset_drop_locks(&ctx);
 	drm_modeset_acquire_fini(&ctx);
+
+	return;
+
+backoff:
+	drm_atomic_commit_clear(state);
+	drm_modeset_backoff(&ctx);
+	goto retry;
 }
 
 static const struct color_format_test_param hdmi_color_format_params[] = {
 	{
 		.fmt = DRM_CONNECTOR_COLOR_FORMAT_AUTO,
Signature · 1 line
2.56.1
author maintainer
#6 lore raw

PATCH v9 5/42 drm/colorop: Export drm_atomic_helper_colorop_create_state()

In reply to Maxime Ripard

drm_atomic_helper_colorop_create_state() is only called by drm_mode_config_create_state(), which lives in the same module, so it never needed to be exported.

The atomic state readout helpers we'll introduce call it from drm_kms_helper to allocate the colorop states. Without the export, building the kernel with CONFIG_DRM_KMS_HELPER=m fails at modpost with an undefined symbol.

---
 drivers/gpu/drm/drm_colorop.c | 1 +
 1 file changed, 1 insertion(+)
drivers/gpu/drm/drm_colorop.c +1 -0
index 95da03d6c65c..319557d1436f 100644
@@ -646,10 +646,11 @@ drm_atomic_helper_colorop_create_state(struct drm_colorop *colorop)
 
 	__drm_colorop_state_init(state, colorop);
 
 	return state;
 }
+EXPORT_SYMBOL(drm_atomic_helper_colorop_create_state);
 
 /**
  * __drm_colorop_reset - reset state on colorop
  * @colorop: drm colorop
  * @colorop_state: colorop state to assign
Signature · 1 line
2.56.1
maintainer Reviewed-by on v9 5/42
#7 lore raw

Re: [PATCH v9 05/42] drm/colorop: Export drm_atomic_helper_colorop_create_state()

Hi,

Quoted 21 lines · Maxime Ripard

drm_atomic_helper_colorop_create_state() is only called by drm_mode_config_create_state(), which lives in the same module, so it never needed to be exported.

The atomic state readout helpers we'll introduce call it from drm_kms_helper to allocate the colorop states. Without the export, building the kernel with CONFIG_DRM_KMS_HELPER=m fails at modpost with an undefined symbol.

---
  drivers/gpu/drm/drm_colorop.c | 1 +
  1 file changed, 1 insertion(+)
drivers/gpu/drm/drm_colorop.c +0 -0
index 95da03d6c65c..319557d1436f 100644
@@ -646,10 +646,11 @@ drm_atomic_helper_colorop_create_state(struct drm_colorop *colorop)
 

drivers/gpu/drm/drm_colorop.c drm_atomic_helper_colorop_create_state(struct drm_colorop *colorop) · line 656

Quoted 10 lines
drivers/gpu/drm/drm_colorop.c +1 -0
  	__drm_colorop_state_init(state, colorop);
 
  	return state;
  }
+EXPORT_SYMBOL(drm_atomic_helper_colorop_create_state);
 
  /**
   * __drm_colorop_reset - reset state on colorop
   * @colorop: drm colorop
   * @colorop_state: colorop state to assign

Tomi

author maintainer
#8 lore raw

PATCH v9 6/42 drm/atomic: Fix private obj error handling

In reply to Maxime Ripard

drm_atomic_private_obj_init() initializes the modeset lock and adds the object to the privobj_list before calling atomic_create_state().

However, if atomic_create_state() fails, the function returns the error directly without removing the object from the list or cleaning up the lock. This leaves a dangling entry in privobj_list and a leaked lock.

Fix this by unwinding both operations in an error path.

Fixes 47b5ac7daa46 ("drm/atomic: Add new atomic_create_state callback to drm_private_obj")
---
 drivers/gpu/drm/drm_atomic.c | 33 ++++++++++++++++++++++++++++++---
 1 file changed, 30 insertions(+), 3 deletions(-)
drivers/gpu/drm/drm_atomic.c +30 -3
index d2a46ba7394f..11a533184250 100644
@@ -1060,25 +1060,41 @@ static void drm_atomic_plane_print_state(struct drm_printer *p,
 int drm_atomic_private_obj_init(struct drm_device *dev,
 				struct drm_private_obj *obj,
 				const struct drm_private_state_funcs *funcs)
 {
 	struct drm_private_state *state;
+	int ret;
+
 	memset(obj, 0, sizeof(*obj));
 
+	/*
+	 * Keep the list node in a state drm_atomic_private_obj_fini()
+	 * can safely handle if we fail before adding it to the private
+	 * objects list.
+	 */
+	INIT_LIST_HEAD(&obj->head);
+
 	drm_modeset_lock_init(&obj->lock);
 
 	obj->dev = dev;
 	obj->funcs = funcs;
 	list_add_tail(&obj->head, &dev->mode_config.privobj_list);
 
 	state = obj->funcs->atomic_create_state(obj);
-	if (IS_ERR(state))
-		return PTR_ERR(state);
+	if (IS_ERR(state)) {
+		ret = PTR_ERR(state);
+		goto err_list_del;
+	}
 
 	obj->state = state;
 
 	return 0;
+
+err_list_del:
+	list_del_init(&obj->head);
+	drm_modeset_lock_fini(&obj->lock);
+	return ret;
 }
 EXPORT_SYMBOL(drm_atomic_private_obj_init);
 
 /**
  * drm_atomic_private_obj_fini - finalize private object
@@ -1087,11 +1103,22 @@ EXPORT_SYMBOL(drm_atomic_private_obj_init);
  * Finalize the private object.
  */
 void
 drm_atomic_private_obj_fini(struct drm_private_obj *obj)
 {
-	list_del(&obj->head);
+	/*
+	 * Some callers ignore drm_atomic_private_obj_init() failures
+	 * and unconditionally register a teardown action that calls
+	 * this function. Use list_del_init() rather than list_del() so
+	 * that calling drm_atomic_private_obj_fini() twice, after both
+	 * a failed and a successful drm_atomic_private_obj_init(),
+	 * remains safe.
+	 */
+	if (list_empty(&obj->head))
+		return;
+
+	list_del_init(&obj->head);
 	obj->funcs->atomic_destroy_state(obj, obj->state);
 	drm_modeset_lock_fini(&obj->lock);
 }
 EXPORT_SYMBOL(drm_atomic_private_obj_fini);
 
Signature · 1 line
2.56.1
author maintainer
#9 lore raw

PATCH v9 7/42 drm/atomic: Add drm_private_obj name

In reply to Maxime Ripard

drm_private_obj does not carry a human-readable name, which makes debug messages hard to interpret when multiple private objects are registered on the same device.

Add a name field to drm_private_obj, passed through drm_atomic_private_obj_init() and freed in drm_atomic_private_obj_fini(). Update the existing callers to pass a name.

---
 drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm.c       |  1 +
 drivers/gpu/drm/arm/display/komeda/komeda_private_obj.c |  8 ++++++++
 drivers/gpu/drm/display/drm_dp_mst_topology.c           |  2 +-
 drivers/gpu/drm/display/drm_dp_tunnel.c                 | 15 +++++++++++++--
 drivers/gpu/drm/drm_atomic.c                            | 13 +++++++++++++
 drivers/gpu/drm/drm_bridge.c                            | 10 +++++++++-
 drivers/gpu/drm/ingenic/ingenic-drm-drv.c               |  1 +
 drivers/gpu/drm/ingenic/ingenic-ipu.c                   |  1 +
 drivers/gpu/drm/msm/disp/dpu1/dpu_kms.c                 |  1 +
 drivers/gpu/drm/msm/disp/mdp5/mdp5_kms.c                |  1 +
 drivers/gpu/drm/omapdrm/omap_drv.c                      |  1 +
 drivers/gpu/drm/tegra/hub.c                             |  1 +
 drivers/gpu/drm/vc4/vc4_kms.c                           |  3 +++
 include/drm/drm_atomic.h                                |  7 +++++++
 include/drm/drm_bridge.h                                |  2 ++
 15 files changed, 63 insertions(+), 4 deletions(-)
drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm.c +1 -0
index 0ea45ab12119..a92c58efe21a 100644
@@ -2299,10 +2299,11 @@ STATIC_IFN_KUNIT int amdgpu_dm_mode_config_init(struct amdgpu_device *adev)
 	/* indicates support for immediate flip */
 	adev_to_drm(adev)->mode_config.async_page_flip = true;
 
 	drm_atomic_private_obj_init(adev_to_drm(adev),
 				    &adev->dm.atomic_obj,
+				    "amdgpu_dc_state",
 				    &dm_atomic_state_funcs);
 
 	r = amdgpu_display_modeset_create_props(adev);
 	if (r)
 		return r;
drivers/gpu/drm/arm/display/komeda/komeda_private_obj.c +8 -0
index 77b3f361091f..8a3c9fcf3844 100644
@@ -64,10 +64,11 @@ static const struct drm_private_state_funcs komeda_layer_obj_funcs = {
 
 static int komeda_layer_obj_add(struct komeda_kms_dev *kms,
 				struct komeda_layer *layer)
 {
 	drm_atomic_private_obj_init(&kms->base, &layer->base.obj,
+				    "komeda_layer",
 				    &komeda_layer_obj_funcs);
 	return 0;
 }
 
 static struct drm_private_state *
@@ -117,10 +118,11 @@ static const struct drm_private_state_funcs komeda_scaler_obj_funcs = {
 static int komeda_scaler_obj_add(struct komeda_kms_dev *kms,
 				 struct komeda_scaler *scaler)
 {
 	drm_atomic_private_obj_init(&kms->base,
 				    &scaler->base.obj,
+				    "komeda_scaler",
 				    &komeda_scaler_obj_funcs);
 	return 0;
 }
 
 static struct drm_private_state *
@@ -169,10 +171,11 @@ static const struct drm_private_state_funcs komeda_compiz_obj_funcs = {
 
 static int komeda_compiz_obj_add(struct komeda_kms_dev *kms,
 				 struct komeda_compiz *compiz)
 {
 	drm_atomic_private_obj_init(&kms->base, &compiz->base.obj,
+				    "komeda_compiz",
 				    &komeda_compiz_obj_funcs);
 
 	return 0;
 }
 
@@ -223,10 +226,11 @@ static const struct drm_private_state_funcs komeda_splitter_obj_funcs = {
 static int komeda_splitter_obj_add(struct komeda_kms_dev *kms,
 				   struct komeda_splitter *splitter)
 {
 	drm_atomic_private_obj_init(&kms->base,
 				    &splitter->base.obj,
+				    "komeda_splitter",
 				    &komeda_splitter_obj_funcs);
 
 	return 0;
 }
 
@@ -276,10 +280,11 @@ static const struct drm_private_state_funcs komeda_merger_obj_funcs = {
 static int komeda_merger_obj_add(struct komeda_kms_dev *kms,
 				 struct komeda_merger *merger)
 {
 	drm_atomic_private_obj_init(&kms->base,
 				    &merger->base.obj,
+				    "komeda_merger",
 				    &komeda_merger_obj_funcs);
 
 	return 0;
 }
 
@@ -329,10 +334,11 @@ static const struct drm_private_state_funcs komeda_improc_obj_funcs = {
 
 static int komeda_improc_obj_add(struct komeda_kms_dev *kms,
 				 struct komeda_improc *improc)
 {
 	drm_atomic_private_obj_init(&kms->base, &improc->base.obj,
+				    "komeda_improc",
 				    &komeda_improc_obj_funcs);
 
 	return 0;
 }
 
@@ -382,10 +388,11 @@ static const struct drm_private_state_funcs komeda_timing_ctrlr_obj_funcs = {
 
 static int komeda_timing_ctrlr_obj_add(struct komeda_kms_dev *kms,
 				       struct komeda_timing_ctrlr *ctrlr)
 {
 	drm_atomic_private_obj_init(&kms->base, &ctrlr->base.obj,
+				    "komeda_timing_ctrlr",
 				    &komeda_timing_ctrlr_obj_funcs);
 
 	return 0;
 }
 
@@ -436,10 +443,11 @@ static const struct drm_private_state_funcs komeda_pipeline_obj_funcs = {
 
 static int komeda_pipeline_obj_add(struct komeda_kms_dev *kms,
 				   struct komeda_pipeline *pipe)
 {
 	drm_atomic_private_obj_init(&kms->base, &pipe->obj,
+				    "komeda_pipeline",
 				    &komeda_pipeline_obj_funcs);
 
 	return 0;
 }
 
drivers/gpu/drm/display/drm_dp_mst_topology.c +1 -1
index b7fce0bf8acc..c7c3f6b0cf76 100644
@@ -5772,11 +5772,11 @@ int drm_dp_mst_topology_mgr_init(struct drm_dp_mst_topology_mgr *mgr,
 	mgr->aux = aux;
 	mgr->max_dpcd_transaction_bytes = max_dpcd_transaction_bytes;
 	mgr->max_payloads = max_payloads;
 	mgr->conn_base_id = conn_base_id;
 
-	drm_atomic_private_obj_init(dev, &mgr->base,
+	drm_atomic_private_obj_init(dev, &mgr->base, "drm_dp_mst_topology",
 				    &drm_dp_mst_topology_state_funcs);
 
 	return 0;
 }
 EXPORT_SYMBOL(drm_dp_mst_topology_mgr_init);
drivers/gpu/drm/display/drm_dp_tunnel.c +13 -2
index d29ad4116e64..381b68f6c3a3 100644
@@ -1725,16 +1725,27 @@ drm_dp_tunnel_atomic_get_new_state(struct drm_atomic_commit *state,
 }
 EXPORT_SYMBOL(drm_dp_tunnel_atomic_get_new_state);
 
 static bool init_group(struct drm_dp_tunnel_mgr *mgr, struct drm_dp_tunnel_group *group)
 {
+	char name[32];
+
 	group->mgr = mgr;
 	group->available_bw = -1;
 	INIT_LIST_HEAD(&group->tunnels);
 
-	drm_atomic_private_obj_init(mgr->dev, &group->base,
-				    &tunnel_group_funcs);
+	/*
+	 * The group identifier, and thus the group name, is only known
+	 * once a tunnel has been added to the group. Name the private
+	 * object after the group index.
+	 */
+	snprintf(name, sizeof(name), "dp-tunnel-group-%d",
+		 (int)(group - mgr->groups));
+
+	if (drm_atomic_private_obj_init(mgr->dev, &group->base, name,
+					&tunnel_group_funcs))
+		return false;
 
 	return true;
 }
 
 static void cleanup_group(struct drm_dp_tunnel_group *group)
drivers/gpu/drm/drm_atomic.c +13 -0
index 11a533184250..bfbd5ef1a6a7 100644
@@ -1046,10 +1046,11 @@ static void drm_atomic_plane_print_state(struct drm_printer *p,
 
 /**
  * drm_atomic_private_obj_init - initialize private object
  * @dev: DRM device this object will be attached to
  * @obj: private object
+ * @name: human-readable name for debug messages
  * @funcs: pointer to the struct of function pointers that identify the object
  * type
  *
  * Initialize the private object, which can be embedded into any
  * driver private object that needs its own atomic state.
@@ -1057,10 +1058,11 @@ static void drm_atomic_plane_print_state(struct drm_printer *p,
  * RETURNS:
  * Zero on success, error code on failure
  */
 int drm_atomic_private_obj_init(struct drm_device *dev,
 				struct drm_private_obj *obj,
+				const char *name,
 				const struct drm_private_state_funcs *funcs)
 {
 	struct drm_private_state *state;
 	int ret;
 
@@ -1075,10 +1077,16 @@ int drm_atomic_private_obj_init(struct drm_device *dev,
 
 	drm_modeset_lock_init(&obj->lock);
 
 	obj->dev = dev;
 	obj->funcs = funcs;
+	obj->name = kstrdup(name, GFP_KERNEL);
+	if (!obj->name) {
+		ret = -ENOMEM;
+		goto err_lock_fini;
+	}
+
 	list_add_tail(&obj->head, &dev->mode_config.privobj_list);
 
 	state = obj->funcs->atomic_create_state(obj);
 	if (IS_ERR(state)) {
 		ret = PTR_ERR(state);
@@ -1089,10 +1097,13 @@ int drm_atomic_private_obj_init(struct drm_device *dev,
 
 	return 0;
 
 err_list_del:
 	list_del_init(&obj->head);
+	kfree(obj->name);
+	obj->name = NULL;
+err_lock_fini:
 	drm_modeset_lock_fini(&obj->lock);
 	return ret;
 }
 EXPORT_SYMBOL(drm_atomic_private_obj_init);
 
@@ -1116,10 +1127,12 @@ drm_atomic_private_obj_fini(struct drm_private_obj *obj)
 	if (list_empty(&obj->head))
 		return;
 
 	list_del_init(&obj->head);
 	obj->funcs->atomic_destroy_state(obj, obj->state);
+	kfree(obj->name);
+	obj->name = NULL;
 	drm_modeset_lock_fini(&obj->lock);
 }
 EXPORT_SYMBOL(drm_atomic_private_obj_fini);
 
 static int drm_atomic_commit_set_private_obj_state(struct drm_atomic_commit *commit,
drivers/gpu/drm/drm_bridge.c +9 -1
index 8ae2f9b1db44..11aaa4ae5102 100644
@@ -316,10 +316,11 @@ static void __drm_bridge_free(struct kref *kref)
 	if (bridge->funcs->destroy)
 		bridge->funcs->destroy(bridge);
 
 	drm_bridge_put(bridge->next_bridge);
 
+	kfree(bridge->name);
 	kfree(bridge->container);
 }
 
 /**
  * drm_bridge_get - Acquire a bridge reference
@@ -417,10 +418,17 @@ void *__devm_drm_bridge_alloc(struct device *dev, size_t size, size_t offset,
 	container = kzalloc(size, GFP_KERNEL);
 	if (!container)
 		return ERR_PTR(-ENOMEM);
 
 	bridge = container + offset;
+
+	bridge->name = kasprintf(GFP_KERNEL, "bridge-%s", dev_name(dev));
+	if (!bridge->name) {
+		kfree(container);
+		return ERR_PTR(-ENOMEM);
+	}
+
 	INIT_LIST_HEAD(&bridge->list);
 	bridge->container = container;
 	bridge->funcs = funcs;
 	kref_init(&bridge->refcount);
 
@@ -652,11 +660,11 @@ int drm_bridge_attach(struct drm_encoder *encoder, struct drm_bridge *bridge,
 		ret = bridge->funcs->attach(bridge, encoder, flags);
 		if (ret < 0)
 			goto err_reset_bridge;
 	}
 
-	drm_atomic_private_obj_init(bridge->dev, &bridge->base,
+	drm_atomic_private_obj_init(bridge->dev, &bridge->base, bridge->name,
 				    &drm_bridge_priv_state_funcs);
 
 	return 0;
 
 err_reset_bridge:
drivers/gpu/drm/ingenic/ingenic-drm-drv.c +1 -0
index 520912f9ed0d..63ca70ab535c 100644
@@ -1411,10 +1411,11 @@ static int ingenic_drm_bind(struct device *dev, bool has_components)
 		dev_err(dev, "Unable to register clock notifier\n");
 		goto err_devclk_disable;
 	}
 
 	drm_atomic_private_obj_init(drm, &priv->private_obj,
+				    "ingenic_drm",
 				    &ingenic_drm_private_state_funcs);
 
 	ret = drmm_add_action_or_reset(drm, ingenic_drm_atomic_private_obj_fini,
 				       &priv->private_obj);
 	if (ret)
drivers/gpu/drm/ingenic/ingenic-ipu.c +1 -0
index 7f3cc8a75898..0b17a7875d9f 100644
@@ -900,10 +900,11 @@ static int ingenic_ipu_bind(struct device *dev, struct device *master, void *d)
 		dev_err(dev, "Unable to prepare clock\n");
 		return err;
 	}
 
 	drm_atomic_private_obj_init(drm, &ipu->private_obj,
+				    "ingenic_ipu_state",
 				    &ingenic_ipu_private_state_funcs);
 
 	return 0;
 }
 
drivers/gpu/drm/msm/disp/dpu1/dpu_kms.c +1 -0
index 1b6286dccfc0..f14fb619ebb6 100644
@@ -1153,10 +1153,11 @@ static int dpu_kms_hw_init(struct msm_kms *kms)
 
 	dev->mode_config.cursor_width = 512;
 	dev->mode_config.cursor_height = 512;
 
 	drm_atomic_private_obj_init(dpu_kms->dev, &dpu_kms->global_state,
+				    "dpu_kms_global_state",
 				    &dpu_kms_global_state_funcs);
 
 	atomic_set(&dpu_kms->bandwidth_ref, 0);
 
 	rc = pm_runtime_resume_and_get(&dpu_kms->pdev->dev);
drivers/gpu/drm/msm/disp/mdp5/mdp5_kms.c +1 -0
index ff6751b2ae62..1dc6d36e4de7 100644
@@ -710,10 +710,11 @@ static int mdp5_init(struct platform_device *pdev, struct drm_device *dev)
 	int ret;
 
 	mdp5_kms->dev = dev;
 
 	drm_atomic_private_obj_init(mdp5_kms->dev, &mdp5_kms->glob_state,
+				    "mdp5_global_state",
 				    &mdp5_global_state_funcs);
 
 	/* we need to set a default rate before enabling.  Set a safe
 	 * rate first, then figure out hw revision, and then set a
 	 * more optimal rate:
drivers/gpu/drm/omapdrm/omap_drv.c +1 -0
index 89a6d3b4aa6f..7430a8119bc0 100644
@@ -297,10 +297,11 @@ static const struct drm_private_state_funcs omap_global_state_funcs = {
 static int omap_global_obj_init(struct drm_device *dev)
 {
 	struct omap_drm_private *priv = dev->dev_private;
 
 	drm_atomic_private_obj_init(dev, &priv->glob_obj,
+				    "omap_global",
 				    &omap_global_state_funcs);
 	return 0;
 }
 
 static void omap_global_obj_fini(struct omap_drm_private *priv)
drivers/gpu/drm/tegra/hub.c +1 -0
index 12429ca53ead..b220e298d27a 100644
@@ -958,10 +958,11 @@ static int tegra_display_hub_init(struct host1x_client *client)
 	struct tegra_display_hub *hub = to_tegra_display_hub(client);
 	struct drm_device *drm = dev_get_drvdata(client->host);
 	struct tegra_drm *tegra = drm->dev_private;
 
 	drm_atomic_private_obj_init(drm, &hub->base,
+				    "tegra_display_hub",
 				    &tegra_display_hub_state_funcs);
 
 	tegra->hub = hub;
 
 	return 0;
drivers/gpu/drm/vc4/vc4_kms.c +3 -0
index ab551eb2da50..e43d59cff4df 100644
@@ -115,10 +115,11 @@ static void vc4_ctm_obj_fini(struct drm_device *dev, void *unused)
 static int vc4_ctm_obj_init(struct vc4_dev *vc4)
 {
 	drm_modeset_lock_init(&vc4->ctm_state_lock);
 
 	drm_atomic_private_obj_init(&vc4->base, &vc4->ctm_manager,
+				    "vc4_ctm",
 				    &vc4_ctm_state_funcs);
 
 	return drmm_add_action_or_reset(&vc4->base, vc4_ctm_obj_fini, NULL);
 }
 
@@ -755,10 +756,11 @@ static void vc4_load_tracker_obj_fini(struct drm_device *dev, void *unused)
 }
 
 static int vc4_load_tracker_obj_init(struct vc4_dev *vc4)
 {
 	drm_atomic_private_obj_init(&vc4->base, &vc4->load_tracker,
+				    "vc4_load_tracker",
 				    &vc4_load_tracker_state_funcs);
 
 	return drmm_add_action_or_reset(&vc4->base, vc4_load_tracker_obj_fini, NULL);
 }
 
@@ -846,10 +848,11 @@ static void vc4_hvs_channels_obj_fini(struct drm_device *dev, void *unused)
 }
 
 static int vc4_hvs_channels_obj_init(struct vc4_dev *vc4)
 {
 	drm_atomic_private_obj_init(&vc4->base, &vc4->hvs_channels,
+				    "vc4_hvs_channels",
 				    &vc4_hvs_state_funcs);
 
 	return drmm_add_action_or_reset(&vc4->base, vc4_hvs_channels_obj_fini, NULL);
 }
 
include/drm/drm_atomic.h +7 -0
index c8d0f18fd0fd..6babffaa7733 100644
@@ -373,10 +373,15 @@ struct drm_private_obj {
 	/**
 	 * @dev: parent DRM device
 	 */
 	struct drm_device *dev;
 
+	/**
+	 * @name: human-readable name for debug messages
+	 */
+	const char *name;
+
 	/**
 	 * @head: List entry used to attach a private object to a &drm_device
 	 * (queued to &drm_mode_config.privobj_list).
 	 */
 	struct list_head head;
@@ -755,11 +760,13 @@ struct drm_connector_state * __must_check
 drm_atomic_get_connector_state(struct drm_atomic_commit *state,
 			       struct drm_connector *connector);
 
 int drm_atomic_private_obj_init(struct drm_device *dev,
 				struct drm_private_obj *obj,
+				const char *name,
 				const struct drm_private_state_funcs *funcs);
+
 void drm_atomic_private_obj_fini(struct drm_private_obj *obj);
 
 struct drm_private_state * __must_check
 drm_atomic_get_private_obj_state(struct drm_atomic_commit *state,
 				 struct drm_private_obj *obj);
include/drm/drm_bridge.h +2 -0
index 1981d24a700d..54cf0d223bba 100644
@@ -1029,10 +1029,12 @@ enum drm_bridge_ops {
 struct drm_bridge {
 	/** @base: inherit from &drm_private_object */
 	struct drm_private_obj base;
 	/** @dev: DRM device this bridge belongs to */
 	struct drm_device *dev;
+	/** @name: human-readable name for debug messages */
+	const char *name;
 	/** @encoder: encoder to which this bridge is connected */
 	struct drm_encoder *encoder;
 	/** @chain_node: used to form a bridge chain */
 	struct list_head chain_node;
 	/** @of_node: device node pointer to the bridge */
Signature · 1 line
2.56.1
maintainer Reviewed-by on v9 7/42
#10 lore raw

Re: [PATCH v9 07/42] drm/atomic: Add drm_private_obj name

Hi,

Quoted 21 lines · Maxime Ripard

drm_private_obj does not carry a human-readable name, which makes debug messages hard to interpret when multiple private objects are registered on the same device.

Add a name field to drm_private_obj, passed through drm_atomic_private_obj_init() and freed in drm_atomic_private_obj_fini(). Update the existing callers to pass a name.

---
  drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm.c       |  1 +
  drivers/gpu/drm/arm/display/komeda/komeda_private_obj.c |  8 ++++++++
  drivers/gpu/drm/display/drm_dp_mst_topology.c           |  2 +-
  drivers/gpu/drm/display/drm_dp_tunnel.c                 | 15 +++++++++++++--
  drivers/gpu/drm/drm_atomic.c                            | 13 +++++++++++++
  drivers/gpu/drm/drm_bridge.c                            | 10 +++++++++-
  drivers/gpu/drm/ingenic/ingenic-drm-drv.c               |  1 +
  drivers/gpu/drm/ingenic/ingenic-ipu.c                   |  1 +
Quoted 9 lines
  drivers/gpu/drm/msm/disp/dpu1/dpu_kms.c                 |  1 +
  drivers/gpu/drm/msm/disp/mdp5/mdp5_kms.c                |  1 +
  drivers/gpu/drm/omapdrm/omap_drv.c                      |  1 +
  drivers/gpu/drm/tegra/hub.c                             |  1 +
  drivers/gpu/drm/vc4/vc4_kms.c                           |  3 +++
  include/drm/drm_atomic.h                                |  7 +++++++
  include/drm/drm_bridge.h                                |  2 ++
  15 files changed, 63 insertions(+), 4 deletions(-)

Tomi

author maintainer
#11 lore raw

PATCH v9 8/42 drm/bridge: Switch print_state to use bridge name

In reply to Maxime Ripard

drm_bridge_atomic_print_priv_state() currently identifies bridges by printing the symbol name of bridge->funcs using the %ps format specifier. This produces output like a raw function pointer name, which is not particularly user-friendly for debugging.

Since bridges now carry a human-readable name, switch to printing bridge->name instead. This makes the state readout output more consistent and easier to interpret.

---
 drivers/gpu/drm/drm_bridge.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/gpu/drm/drm_bridge.c +2 -2
index 11aaa4ae5102..aed3fcf0e180 100644
@@ -570,13 +570,13 @@ drm_bridge_atomic_print_priv_state(struct drm_printer *p,
 {
 	const struct drm_bridge_state *state = drm_priv_to_bridge_state(s);
 	struct drm_bridge *bridge = drm_priv_to_bridge(s->obj);
 
 	if (bridge->of_node)
-		drm_printf(p, "bridge: %ps (%pOFfc)\n", bridge->funcs, bridge->of_node);
+		drm_printf(p, "bridge: %s (%pOFfc)\n", bridge->name, bridge->of_node);
 	else
-		drm_printf(p, "bridge: %ps\n", bridge->funcs);
+		drm_printf(p, "bridge: %s\n", bridge->name);
 
 	drm_printf_indent(p, 1, "input bus configuration:");
 	drm_printf_indent(p, 2, "code: %04x", state->input_bus_cfg.format);
 	drm_printf_indent(p, 2, "flags: %08x", state->input_bus_cfg.flags);
 	drm_printf_indent(p, 1, "output bus configuration:");
Signature · 1 line
2.56.1
maintainer Reviewed-by on v9 8/42
#12 lore raw

Re: [PATCH v9 08/42] drm/bridge: Switch print_state to use bridge name

Quoted 27 lines · Maxime Ripard

drm_bridge_atomic_print_priv_state() currently identifies bridges by printing the symbol name of bridge->funcs using the %ps format specifier. This produces output like a raw function pointer name, which is not particularly user-friendly for debugging.

Since bridges now carry a human-readable name, switch to printing bridge->name instead. This makes the state readout output more consistent and easier to interpret.

---
  drivers/gpu/drm/drm_bridge.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
drivers/gpu/drm/drm_bridge.c +0 -0
index 11aaa4ae5102..aed3fcf0e180 100644
@@ -570,13 +570,13 @@ drm_bridge_atomic_print_priv_state(struct drm_printer *p,
  {
  	const struct drm_bridge_state *state = drm_priv_to_bridge_state(s);
  	struct drm_bridge *bridge = drm_priv_to_bridge(s->obj);
 
  	if (bridge->of_node)

drivers/gpu/drm/drm_bridge.c drm_bridge_atomic_print_priv_state(struct drm_printer *p, · line 582

Quoted 10 lines
drivers/gpu/drm/drm_bridge.c +2 -2
-		drm_printf(p, "bridge: %ps (%pOFfc)\n", bridge->funcs, bridge->of_node);
+		drm_printf(p, "bridge: %s (%pOFfc)\n", bridge->name, bridge->of_node);
  	else
-		drm_printf(p, "bridge: %ps\n", bridge->funcs);
+		drm_printf(p, "bridge: %s\n", bridge->name);
 
  	drm_printf_indent(p, 1, "input bus configuration:");
  	drm_printf_indent(p, 2, "code: %04x", state->input_bus_cfg.format);
  	drm_printf_indent(p, 2, "flags: %08x", state->input_bus_cfg.flags);
  	drm_printf_indent(p, 1, "output bus configuration:");

Tomi

author maintainer
#13 lore raw

PATCH v9 9/42 drm/bridge: Add drm_private_obj_is_bridge()

In reply to Maxime Ripard

The SRO readout helpers need to distinguish bridge-backed private objects from other private objects, because bridges require special readout ordering through the encoder bridge chains.

Add drm_private_obj_is_bridge() which checks whether a private object's funcs pointer matches the bridge private state funcs.

---
 drivers/gpu/drm/drm_bridge.c | 15 +++++++++++++++
 include/drm/drm_bridge.h     |  2 ++
 2 files changed, 17 insertions(+)
drivers/gpu/drm/drm_bridge.c +15 -0
index aed3fcf0e180..b6cdbc172f68 100644
@@ -589,10 +589,25 @@ static const struct drm_private_state_funcs drm_bridge_priv_state_funcs = {
 	.atomic_duplicate_state = drm_bridge_atomic_duplicate_priv_state,
 	.atomic_destroy_state = drm_bridge_atomic_destroy_priv_state,
 	.atomic_print_state = drm_bridge_atomic_print_priv_state,
 };
 
+/**
+ * drm_private_obj_is_bridge - check if a private object backs a bridge
+ * @obj: private object to check
+ *
+ * RETURNS:
+ *
+ * True if @obj is the &drm_private_obj embedded in a &struct drm_bridge,
+ * false otherwise.
+ */
+bool drm_private_obj_is_bridge(struct drm_private_obj *obj)
+{
+	return obj && obj->funcs == &drm_bridge_priv_state_funcs;
+}
+EXPORT_SYMBOL(drm_private_obj_is_bridge);
+
 /**
  * drm_bridge_attach - attach the bridge to an encoder's chain
  *
  * @encoder: DRM encoder
  * @bridge: bridge to attach
include/drm/drm_bridge.h +2 -0
index 54cf0d223bba..a22a0bdf3251 100644
@@ -1232,10 +1232,12 @@ static inline struct drm_bridge *
 drm_priv_to_bridge(struct drm_private_obj *priv)
 {
 	return container_of(priv, struct drm_bridge, base);
 }
 
+bool drm_private_obj_is_bridge(struct drm_private_obj *obj);
+
 bool drm_bridge_enter(struct drm_bridge *bridge, int *idx);
 void drm_bridge_exit(int idx);
 void drm_bridge_unplug(struct drm_bridge *bridge);
 
 struct drm_bridge *drm_bridge_get(struct drm_bridge *bridge);
Signature · 1 line
2.56.1
maintainer Reviewed-by on v9 9/42
#14 lore raw

Re: [PATCH v9 09/42] drm/bridge: Add drm_private_obj_is_bridge()

Hi,

Quoted 52 lines · Maxime Ripard

The SRO readout helpers need to distinguish bridge-backed private objects from other private objects, because bridges require special readout ordering through the encoder bridge chains.

Add drm_private_obj_is_bridge() which checks whether a private object's funcs pointer matches the bridge private state funcs.

---
  drivers/gpu/drm/drm_bridge.c | 15 +++++++++++++++
  include/drm/drm_bridge.h     |  2 ++
  2 files changed, 17 insertions(+)
drivers/gpu/drm/drm_bridge.c +15 -0
index aed3fcf0e180..b6cdbc172f68 100644
@@ -589,10 +589,25 @@ static const struct drm_private_state_funcs drm_bridge_priv_state_funcs = {
  	.atomic_duplicate_state = drm_bridge_atomic_duplicate_priv_state,
  	.atomic_destroy_state = drm_bridge_atomic_destroy_priv_state,
  	.atomic_print_state = drm_bridge_atomic_print_priv_state,
  };
 
+/**
+ * drm_private_obj_is_bridge - check if a private object backs a bridge
+ * @obj: private object to check
+ *
+ * RETURNS:
+ *
+ * True if @obj is the &drm_private_obj embedded in a &struct drm_bridge,
+ * false otherwise.
+ */
+bool drm_private_obj_is_bridge(struct drm_private_obj *obj)
+{
+	return obj && obj->funcs == &drm_bridge_priv_state_funcs;
+}
+EXPORT_SYMBOL(drm_private_obj_is_bridge);
+
  /**
   * drm_bridge_attach - attach the bridge to an encoder's chain
   *
   * @encoder: DRM encoder
   * @bridge: bridge to attach
include/drm/drm_bridge.h +0 -0
index 54cf0d223bba..a22a0bdf3251 100644
@@ -1232,10 +1232,12 @@ static inline struct drm_bridge *
  drm_priv_to_bridge(struct drm_private_obj *priv)
  {

include/drm/drm_bridge.h static inline struct drm_bridge * · line 1243

Quoted 10 lines
include/drm/drm_bridge.h +2 -0
  	return container_of(priv, struct drm_bridge, base);
  }
 
+bool drm_private_obj_is_bridge(struct drm_private_obj *obj);
+
  bool drm_bridge_enter(struct drm_bridge *bridge, int *idx);
  void drm_bridge_exit(int idx);
  void drm_bridge_unplug(struct drm_bridge *bridge);
 
  struct drm_bridge *drm_bridge_get(struct drm_bridge *bridge);

I can't help but feel that this is a bit hacky. Nevertheless, it's simple and I understand the need to deal with bridges with SRO.

Tomi

author maintainer
#15 lore raw

PATCH v9 10/42 drm/fb-dma-helper: Handle framebuffers without a GEM object

In reply to Maxime Ripard

The state readout documentation now states that a driver should return an error when the hardware was programmed with a configuration it cannot represent, so that the state initialization falls back to the regular path.

However, a readout can also succeed with objects that cannot be fully reconstructed. The framebuffer the firmware left in the hardware was allocated outside of the kernel memory allocator, so drivers won't have any GEM object to attach to its planes.

drm_fb_dma_get_scanout_buffer() dereferences the GEM object of the first plane to fill the scanout buffer description used by drm_panic, and would thus crash during a panic happening before userspace had a chance to replace the framebuffer read out from the hardware state.

Return -ENODEV for such framebuffers, like we already do for imported buffers that are not accessible from the CPU.

---

Cc Jocelyn Falempe <[email protected]>
---
 drivers/gpu/drm/drm_fb_dma_helper.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
drivers/gpu/drm/drm_fb_dma_helper.c +10 -0
index fd71969d2fb1..e24036e1f3ba 100644
@@ -175,10 +175,20 @@ int drm_fb_dma_get_scanout_buffer(struct drm_plane *plane,
 	fb = plane->state->fb;
 	/* Only support linear modifier */
 	if (fb->modifier != DRM_FORMAT_MOD_LINEAR)
 		return -ENODEV;
 
+	/*
+	 * The framebuffer has no GEM object backing its first plane,
+	 * like a framebuffer reconstructed from a hardware state
+	 * readout: we cannot access its contents. Check the framebuffer
+	 * directly, since drm_fb_dma_get_gem_obj() warns about a
+	 * missing GEM object.
+	 */
+	if (!fb->obj[0])
+		return -ENODEV;
+
 	dma_obj = drm_fb_dma_get_gem_obj(fb, 0);
 
 	/* Buffer should be accessible from the CPU */
 	if (drm_gem_is_imported(&dma_obj->base))
 		return -ENODEV;
Signature · 1 line
2.56.1
maintainer
#16 lore raw

Re: [PATCH v9 10/42] drm/fb-dma-helper: Handle framebuffers without a GEM object

Hi,

Quoted 44 lines · Maxime Ripard

The state readout documentation now states that a driver should return an error when the hardware was programmed with a configuration it cannot represent, so that the state initialization falls back to the regular path.

However, a readout can also succeed with objects that cannot be fully reconstructed. The framebuffer the firmware left in the hardware was allocated outside of the kernel memory allocator, so drivers won't have any GEM object to attach to its planes.

drm_fb_dma_get_scanout_buffer() dereferences the GEM object of the first plane to fill the scanout buffer description used by drm_panic, and would thus crash during a panic happening before userspace had a chance to replace the framebuffer read out from the hardware state.

Return -ENODEV for such framebuffers, like we already do for imported buffers that are not accessible from the CPU.

---

Cc Jocelyn Falempe <[email protected]>
---
  drivers/gpu/drm/drm_fb_dma_helper.c | 10 ++++++++++
  1 file changed, 10 insertions(+)
drivers/gpu/drm/drm_fb_dma_helper.c +5 -0
index fd71969d2fb1..e24036e1f3ba 100644
@@ -175,10 +175,20 @@ int drm_fb_dma_get_scanout_buffer(struct drm_plane *plane,
  	fb = plane->state->fb;
  	/* Only support linear modifier */
  	if (fb->modifier != DRM_FORMAT_MOD_LINEAR)
  		return -ENODEV;
 
+	/*
+	 * The framebuffer has no GEM object backing its first plane,
+	 * like a framebuffer reconstructed from a hardware state
+	 * readout: we cannot access its contents. Check the framebuffer
+	 * directly, since drm_fb_dma_get_gem_obj() warns about a

drivers/gpu/drm/drm_fb_dma_helper.c int drm_fb_dma_get_scanout_buffer(struct drm_plane *plane, · line 194

Quoted 10 lines
drivers/gpu/drm/drm_fb_dma_helper.c +5 -0
+	 * missing GEM object.
+	 */
+	if (!fb->obj[0])
+		return -ENODEV;
+
  	dma_obj = drm_fb_dma_get_gem_obj(fb, 0);
 
  	/* Buffer should be accessible from the CPU */
  	if (drm_gem_is_imported(&dma_obj->base))
  		return -ENODEV;

Are this and the next one needed? I'm not that familiar with the buffer management, but in my tidss changes I implemented (or hacked? I'm not sure if it's proper...) GEM object creation for the bootsplash fb (you can then e.g. see the FB dma address from debugfs). If it is okay-ish, I wonder if all drivers can just do something like that with SRO.

Tomi

author maintainer
#17 lore raw

PATCH v9 11/42 drm/gem-atomic-helper: Handle framebuffers without a GEM object

In reply to Maxime Ripard

A framebuffer reconstructed from a hardware state readout describes a buffer that the firmware allocated, outside of the kernel memory allocator, so its planes have no GEM object attached.

drm_gem_plane_helper_prepare_fb() retrieves the GEM object of every plane through drm_gem_fb_get_obj() to extract the implicit fences from its reservation. For a readout framebuffer, this triggers drm_WARN_ON_ONCE(!fb->obj[plane]), returns NULL, and fails the whole commit with -EINVAL.

This can be hit as soon as the state readout installs such a framebuffer on a plane: any commit that includes the plane without userspace providing a new framebuffer will duplicate its current state, readout framebuffer included, and run prepare_fb() on it. Blanking the display through DPMS right after boot is such a case, since it pulls the plane in through drm_atomic_add_affected_planes().

Access the framebuffer obj[] array directly and skip the planes without a GEM object: there is no reservation to extract implicit fences from anyway. This mirrors what drm_fb_dma_get_scanout_buffer() does for such framebuffers.

---
 drivers/gpu/drm/drm_gem_atomic_helper.c | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)
drivers/gpu/drm/drm_gem_atomic_helper.c +9 -5
index c952cfed84c1..bab354d12fe7 100644
@@ -135,17 +135,21 @@ int drm_gem_plane_helper_prepare_fb(struct drm_plane *plane,
 	 * will break all the benefits of explicit fencing.
 	 */
 	usage = fence ? DMA_RESV_USAGE_KERNEL : DMA_RESV_USAGE_WRITE;
 
 	for (i = 0; i < state->fb->format->num_planes; ++i) {
-		struct drm_gem_object *obj = drm_gem_fb_get_obj(state->fb, i);
+		struct drm_gem_object *obj = state->fb->obj[i];
 		struct dma_fence *new;
 
-		if (!obj) {
-			ret = -EINVAL;
-			goto error;
-		}
+		/*
+		 * A framebuffer reconstructed from a hardware state
+		 * readout has no GEM object backing its planes, as it
+		 * describes a buffer allocated outside of the kernel:
+		 * there is no implicit fencing to wait on.
+		 */
+		if (!obj)
+			continue;
 
 		ret = dma_resv_get_singleton(obj->resv, usage, &new);
 		if (ret)
 			goto error;
 
Signature · 1 line
2.56.1
author maintainer
#18 lore raw

PATCH v9 12/42 drm/atomic: Export drm_atomic_*_print_state

In reply to Maxime Ripard

The drm_atomic_plane_print_state(), drm_atomic_crtc_print_state(), drm_atomic_connector_print_state(), and drm_atomic_private_obj_print_state() functions are currently static.

The SRO infrastructure needs to call them from drm_atomic_state_readout.c to print the readout state for debugging purposes. Make them available by dropping the static qualifier and declaring them in drm_internal.h.

---
 drivers/gpu/drm/drm_atomic.c   | 16 ++++++++--------
 drivers/gpu/drm/drm_internal.h | 13 +++++++++++++
 2 files changed, 21 insertions(+), 8 deletions(-)
drivers/gpu/drm/drm_atomic.c +8 -8
index bfbd5ef1a6a7..ed0882a31a0a 100644
@@ -556,12 +556,12 @@ static int drm_atomic_crtc_check(const struct drm_crtc_state *old_crtc_state,
 	}
 
 	return 0;
 }
 
-static void drm_atomic_crtc_print_state(struct drm_printer *p,
-		const struct drm_crtc_state *state)
+void drm_atomic_crtc_print_state(struct drm_printer *p,
+				 const struct drm_crtc_state *state)
 {
 	struct drm_crtc *crtc = state->crtc;
 
 	drm_printf(p, "crtc[%u]: %s\n", crtc->base.id, crtc->name);
 	drm_printf_indent(p, 1, "enable=%d\n", state->enable);
@@ -977,12 +977,12 @@ static void drm_atomic_colorop_print_state(struct drm_printer *p,
 	}
 
 	drm_printf_indent(p, 1, "next=%d\n", colorop->next ? colorop->next->base.id : 0);
 }
 
-static void drm_atomic_plane_print_state(struct drm_printer *p,
-		const struct drm_plane_state *state)
+void drm_atomic_plane_print_state(struct drm_printer *p,
+				  const struct drm_plane_state *state)
 {
 	struct drm_plane *plane = state->plane;
 	struct drm_rect src  = drm_plane_state_src(state);
 	struct drm_rect dest = drm_plane_state_dest(state);
 
@@ -1565,12 +1565,12 @@ drm_atomic_get_connector_state(struct drm_atomic_commit *state,
 
 	return connector_state;
 }
 EXPORT_SYMBOL(drm_atomic_get_connector_state);
 
-static void drm_atomic_connector_print_state(struct drm_printer *p,
-		const struct drm_connector_state *state)
+void drm_atomic_connector_print_state(struct drm_printer *p,
+				      const struct drm_connector_state *state)
 {
 	struct drm_connector *connector = state->connector;
 
 	drm_printf(p, "connector[%u]: %s\n", connector->base.id, connector->name);
 	drm_printf_indent(p, 1, "crtc=%s\n", state->crtc ? state->crtc->name : "(null)");
@@ -2211,12 +2211,12 @@ int __drm_atomic_helper_set_config(struct drm_mode_set *set,
 
 	return 0;
 }
 EXPORT_SYMBOL(__drm_atomic_helper_set_config);
 
-static void drm_atomic_private_obj_print_state(struct drm_printer *p,
-					       const struct drm_private_state *state)
+void drm_atomic_private_obj_print_state(struct drm_printer *p,
+					const struct drm_private_state *state)
 {
 	struct drm_private_obj *obj = state->obj;
 
 	if (obj->funcs->atomic_print_state)
 		obj->funcs->atomic_print_state(p, state);
drivers/gpu/drm/drm_internal.h +13 -0
index f893b1e3a596..815486557f51 100644
@@ -37,19 +37,32 @@
 
 struct dentry;
 struct dma_buf;
 struct iosys_map;
 struct drm_connector;
+struct drm_connector_state;
 struct drm_crtc;
+struct drm_crtc_state;
 struct drm_framebuffer;
 struct drm_gem_object;
 struct drm_master;
 struct drm_minor;
+struct drm_plane_state;
 struct drm_prime_file_private;
 struct drm_printer;
+struct drm_private_state;
 struct drm_vblank_crtc;
 
+/* drm_atomic.c */
+void drm_atomic_plane_print_state(struct drm_printer *p,
+				  const struct drm_plane_state *state);
+void drm_atomic_crtc_print_state(struct drm_printer *p,
+				 const struct drm_crtc_state *state);
+void drm_atomic_connector_print_state(struct drm_printer *p,
+				      const struct drm_connector_state *state);
+void drm_atomic_private_obj_print_state(struct drm_printer *p,
+					const struct drm_private_state *state);
 /* drm_client_event.c */
 #if defined(CONFIG_DRM_CLIENT)
 void drm_client_debugfs_init(struct drm_device *dev);
 #else
 static inline void drm_client_debugfs_init(struct drm_device *dev)
Signature · 1 line
2.56.1
maintainer Reviewed-by on v9 12/42
#19 lore raw

Re: [PATCH v9 12/42] drm/atomic: Export drm_atomic_*_print_state

Quoted 9 lines · Maxime Ripard

The drm_atomic_plane_print_state(), drm_atomic_crtc_print_state(), drm_atomic_connector_print_state(), and drm_atomic_private_obj_print_state() functions are currently static.

The SRO infrastructure needs to call them from drm_atomic_state_readout.c to print the readout state for debugging purposes. Make them available by dropping the static qualifier and declaring them in drm_internal.h.

Quoted 6 lines
---
  drivers/gpu/drm/drm_atomic.c   | 16 ++++++++--------
  drivers/gpu/drm/drm_internal.h | 13 +++++++++++++
  2 files changed, 21 insertions(+), 8 deletions(-)

Tomi

author maintainer
#20 lore raw

PATCH v9 13/42 drm/atomic_sro: Create drm_atomic_sro_state container

In reply to Maxime Ripard

The SRO infrastructure needs a state container to collect the readout states for all KMS objects. Unlike drm_atomic_commit which tracks old and new states for atomic commits, this container only holds a single state per object: the one read out from the hardware.

Create struct drm_atomic_sro_state with arrays for planes, CRTCs, connectors, and private objects, along with accessors to get and set individual object states within it.

---
 drivers/gpu/drm/Makefile                   |   1 +
 drivers/gpu/drm/drm_atomic_state_readout.c | 491 +++++++++++++++++++++++++++++
 include/drm/drm_atomic_state_readout.h     |  51 +++
 3 files changed, 543 insertions(+)
drivers/gpu/drm/Makefile +1 -0
index 2c6bc093a923..e94b77497514 100644
@@ -32,10 +32,11 @@ endif
 # Enable -Werror in CI and development
 subdir-ccflags-$(CONFIG_DRM_WERROR) += -Werror
 
 drm-y := \
 	drm_atomic.o \
+	drm_atomic_state_readout.o \
 	drm_atomic_uapi.o \
 	drm_auth.o \
 	drm_blend.o \
 	drm_bridge.o \
 	drm_cache.o \
drivers/gpu/drm/drm_atomic_state_readout.c new file +491 -0

491 lines not on this page · raw message

include/drm/drm_atomic_state_readout.h new file +51 -0

51 lines not on this page · raw message

Signature · 1 line
2.56.1
maintainer
#21 lore raw

Re: [PATCH v9 13/42] drm/atomic_sro: Create drm_atomic_sro_state container

Hi,

Quoted 73 lines · Maxime Ripard

The SRO infrastructure needs a state container to collect the readout states for all KMS objects. Unlike drm_atomic_commit which tracks old and new states for atomic commits, this container only holds a single state per object: the one read out from the hardware.

Create struct drm_atomic_sro_state with arrays for planes, CRTCs, connectors, and private objects, along with accessors to get and set individual object states within it.

---
  drivers/gpu/drm/Makefile                   |   1 +
  drivers/gpu/drm/drm_atomic_state_readout.c | 491 +++++++++++++++++++++++++++++
  include/drm/drm_atomic_state_readout.h     |  51 +++
  3 files changed, 543 insertions(+)
drivers/gpu/drm/Makefile +1 -0
index 2c6bc093a923..e94b77497514 100644
@@ -32,10 +32,11 @@ endif
  # Enable -Werror in CI and development
  subdir-ccflags-$(CONFIG_DRM_WERROR) += -Werror
 
  drm-y := \
  	drm_atomic.o \
+	drm_atomic_state_readout.o \
  	drm_atomic_uapi.o \
  	drm_auth.o \
  	drm_blend.o \
  	drm_bridge.o \
  	drm_cache.o \
drivers/gpu/drm/drm_atomic_state_readout.c new file +34 -0
new file mode 100644
index 000000000000..c00631800234
@@ -0,0 +1,491 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+#include <drm/drm_atomic.h>
+#include <drm/drm_atomic_state_readout.h>
+#include <drm/drm_bridge.h>
+#include <drm/drm_connector.h>
+#include <drm/drm_crtc.h>
+#include <drm/drm_drv.h>
+#include <drm/drm_plane.h>
+#include <drm/drm_print.h>
+#include <linux/module.h>
+#include <linux/sched/mm.h>
+
+#include "drm_internal.h"
+#include "drm_crtc_internal.h"
+
+struct __drm_atomic_sro_plane {
+	struct drm_plane *ptr;
+	struct drm_plane_state *state;
+};
+
+struct __drm_atomic_sro_crtc {
+	struct drm_crtc *ptr;
+	struct drm_crtc_state *state;
+};
+
+struct __drm_atomic_sro_connector {
+	struct drm_connector *ptr;
+	struct drm_connector_state *state;
+};
+
+struct __drm_atomic_sro_private_obj {
+	struct drm_private_obj *ptr;
+	struct drm_private_state *state;

drivers/gpu/drm/drm_atomic_state_readout.c line 44

Quoted 10 lines
drivers/gpu/drm/drm_atomic_state_readout.c new file +10 -0
+};
+
+struct drm_atomic_sro_state {
+	struct drm_device *dev;
+	struct __drm_atomic_sro_plane *planes;
+	struct __drm_atomic_sro_crtc *crtcs;
+	struct __drm_atomic_sro_connector *connectors;
+	unsigned int max_connector_index;
+	struct __drm_atomic_sro_private_obj *private_objs;
+	unsigned int num_private_objs;

The private_objs makes me feel a bit uneasy. You add to private_objs array dynamically, without any check for the array size. An extra call to drm_atomic_sro_set_private_obj_state() will write over the array.

If you're sure that can never happen, fine for me.

Tomi

author maintainer
#22 lore raw

Re: [PATCH v9 13/42] drm/atomic_sro: Create drm_atomic_sro_state container

Hi Tomi,

Thanks for your review,

Quoted 88 lines · Tomi Valkeinen
Quoted 72 lines · Maxime Ripard

The SRO infrastructure needs a state container to collect the readout states for all KMS objects. Unlike drm_atomic_commit which tracks old and new states for atomic commits, this container only holds a single state per object: the one read out from the hardware.

Create struct drm_atomic_sro_state with arrays for planes, CRTCs, connectors, and private objects, along with accessors to get and set individual object states within it.

---
  drivers/gpu/drm/Makefile                   |   1 +
  drivers/gpu/drm/drm_atomic_state_readout.c | 491 +++++++++++++++++++++++++++++
  include/drm/drm_atomic_state_readout.h     |  51 +++
  3 files changed, 543 insertions(+)
drivers/gpu/drm/Makefile +1 -0
index 2c6bc093a923..e94b77497514 100644
@@ -32,10 +32,11 @@ endif
  # Enable -Werror in CI and development
  subdir-ccflags-$(CONFIG_DRM_WERROR) += -Werror
  drm-y := \
  	drm_atomic.o \
+	drm_atomic_state_readout.o \
  	drm_atomic_uapi.o \
  	drm_auth.o \
  	drm_blend.o \
  	drm_bridge.o \
  	drm_cache.o \
drivers/gpu/drm/drm_atomic_state_readout.c new file +34 -0
new file mode 100644
index 000000000000..c00631800234
@@ -0,0 +1,491 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+#include <drm/drm_atomic.h>
+#include <drm/drm_atomic_state_readout.h>
+#include <drm/drm_bridge.h>
+#include <drm/drm_connector.h>
+#include <drm/drm_crtc.h>
+#include <drm/drm_drv.h>
+#include <drm/drm_plane.h>
+#include <drm/drm_print.h>
+#include <linux/module.h>
+#include <linux/sched/mm.h>
+
+#include "drm_internal.h"
+#include "drm_crtc_internal.h"
+
+struct __drm_atomic_sro_plane {
+	struct drm_plane *ptr;
+	struct drm_plane_state *state;
+};
+
+struct __drm_atomic_sro_crtc {
+	struct drm_crtc *ptr;
+	struct drm_crtc_state *state;
+};
+
+struct __drm_atomic_sro_connector {
+	struct drm_connector *ptr;
+	struct drm_connector_state *state;
+};
+
+struct __drm_atomic_sro_private_obj {
+	struct drm_private_obj *ptr;
+	struct drm_private_state *state;

drivers/gpu/drm/drm_atomic_state_readout.c line 44

Quoted 10 lines
drivers/gpu/drm/drm_atomic_state_readout.c new file +10 -0
+};
+
+struct drm_atomic_sro_state {
+	struct drm_device *dev;
+	struct __drm_atomic_sro_plane *planes;
+	struct __drm_atomic_sro_crtc *crtcs;
+	struct __drm_atomic_sro_connector *connectors;
+	unsigned int max_connector_index;
+	struct __drm_atomic_sro_private_obj *private_objs;
+	unsigned int num_private_objs;

The private_objs makes me feel a bit uneasy. You add to private_objs array dynamically, without any check for the array size. An extra call to drm_atomic_sro_set_private_obj_state() will write over the array.

Quoted 1 line

If you're sure that can never happen, fine for me.

I don't think it's a concern for private_objs. They do not support dynamic registration, and we initialize the array with the total number of private_objs registered, so we over-allocate and cannot (I think?) end up in a situation where we have a private_obj without a slot.

Maxime

author maintainer
#23 lore raw

PATCH v9 14/42 drm/atomic_sro: Create kernel parameter to force or disable readout

In reply to Maxime Ripard

The hardware state readout is useful, but might need to be disabled in case of bugs, or its checks relaxed during development when not all hooks are implemented yet.

Add a module parameter to control the readout behavior: it can be disabled entirely, or the checks for missing compare or readout hooks can be skipped independently.

---
 Documentation/admin-guide/kernel-parameters.txt | 12 ++++++++++
 drivers/gpu/drm/drm_atomic_state_readout.c      | 29 +++++++++++++++++++++++++
 include/drm/drm_atomic_state_readout.h          | 11 ++++++++++
 3 files changed, 52 insertions(+)
Documentation/admin-guide/kernel-parameters.txt +12 -0
index 0b3c140babd9..59ff44c49f31 100644
@@ -1375,10 +1375,22 @@ Kernel parameters
 			the EDID name. Each connector may use a unique EDID
 			data set by separating the files with a comma. An EDID
 			data set with no connector name will be used for
 			any connectors not explicitly specified.
 
+	drm.atomic_readout=	[DRM]
+			Control the KMS atomic hardware state readout
+			(SRO), used to initialize the atomic state from
+			the state the firmware left the hardware in at
+			boot instead of programming a new one.
+			0: disable the state readout entirely,
+			1: enable it (default),
+			2: ignore the objects missing a comparison
+			   implementation,
+			3: ignore the objects missing a readout or
+			   comparison implementation.
+
 	dscc4.setup=	[NET]
 
 	dt_cpu_ftrs=	[PPC,EARLY]
 			Format: {"off" | "known"}
 			Control how the dt_cpu_ftrs device-tree binding is
drivers/gpu/drm/drm_atomic_state_readout.c +29 -0
index c00631800234..b8ca4cbbc5b2 100644
@@ -12,10 +12,39 @@
 #include <linux/sched/mm.h>
 
 #include "drm_internal.h"
 #include "drm_crtc_internal.h"
 
+static unsigned int atomic_readout = DRM_ATOMIC_READOUT_ENABLED;
+module_param_unsafe(atomic_readout, uint, 0400);
+MODULE_PARM_DESC(atomic_readout,
+		 "Enable Hardware State Readout (0 = disabled, 1 = enabled, 2 = ignore missing compares, 3 = ignore missing readouts and compares, default = 1)");
+
+/**
+ * drm_atomic_sro_device_can_readout - check if a device supports hardware state readout
+ * @dev: DRM device to check
+ *
+ * Verifies that the device is an atomic driver, that readout is
+ * enabled, and that all KMS objects implement the relevant hooks.
+ *
+ * RETURNS:
+ *
+ * True if the device supports full hardware state readout, false
+ * otherwise.
+ */
+bool drm_atomic_sro_device_can_readout(struct drm_device *dev)
+{
+	if (!drm_drv_uses_atomic_modeset(dev))
+		return false;
+
+	if (atomic_readout == DRM_ATOMIC_READOUT_DISABLED)
+		return false;
+
+	return true;
+}
+EXPORT_SYMBOL(drm_atomic_sro_device_can_readout);
+
 struct __drm_atomic_sro_plane {
 	struct drm_plane *ptr;
 	struct drm_plane_state *state;
 };
 
include/drm/drm_atomic_state_readout.h +11 -0
index 5a9333a05796..65d45ca4be76 100644
@@ -1,10 +1,12 @@
 /* SPDX-License-Identifier: GPL-2.0-or-later */
 
 #ifndef DRM_ATOMIC_SRO_H_
 #define DRM_ATOMIC_SRO_H_
 
+#include <linux/types.h>
+
 struct drm_atomic_sro_state;
 struct drm_connector;
 struct drm_connector_state;
 struct drm_crtc;
 struct drm_crtc_state;
@@ -13,10 +15,19 @@ struct drm_plane;
 struct drm_plane_state;
 struct drm_printer;
 struct drm_private_obj;
 struct drm_private_state;
 
+enum drm_atomic_readout_status {
+	DRM_ATOMIC_READOUT_DISABLED = 0,
+	DRM_ATOMIC_READOUT_ENABLED,
+	DRM_ATOMIC_READOUT_SKIP_MISSING_COMPARE,
+	DRM_ATOMIC_READOUT_SKIP_MISSING_READOUT,
+};
+
+bool drm_atomic_sro_device_can_readout(struct drm_device *dev);
+
 struct drm_atomic_sro_state *drm_atomic_sro_state_alloc(struct drm_device *dev);
 void drm_atomic_sro_state_free(struct drm_atomic_sro_state *state);
 void drm_atomic_sro_state_print(const struct drm_atomic_sro_state *state,
 				struct drm_printer *p);
 
Signature · 1 line
2.56.1
maintainer Reviewed-by on v9 14/42
#24 lore raw

Re: [PATCH v9 14/42] drm/atomic_sro: Create kernel parameter to force or disable readout

Hi,

Quoted 12 lines · Maxime Ripard

The hardware state readout is useful, but might need to be disabled in case of bugs, or its checks relaxed during development when not all hooks are implemented yet.

Add a module parameter to control the readout behavior: it can be disabled entirely, or the checks for missing compare or readout hooks can be skipped independently.

Quoted 5 lines
---
  Documentation/admin-guide/kernel-parameters.txt | 12 ++++++++++
  drivers/gpu/drm/drm_atomic_state_readout.c      | 29 +++++++++++++++++++++++++
  include/drm/drm_atomic_state_readout.h          | 11 ++++++++++
  3 files changed, 52 insertions(+)

Tomi

author maintainer
#25 lore raw

PATCH v9 15/42 drm/atomic_sro: Add atomic state readout infrastructure

In reply to Maxime Ripard

In order to enable drivers to read their initial state from the hardware, each KMS object needs a hook to fill a pre-allocated state from the hardware registers.

Introduce the atomic_sro_readout_state hook in every KMS object funcs
vtable: drm_crtc_funcs, drm_plane_funcs, drm_connector_funcs, drm_bridge_funcs, and drm_private_state_funcs.

Also introduce the drm_mode_config_funcs.atomic_sro_readout_state and drm_mode_config_helper_funcs.atomic_sro_build_state hooks, which are the top-level entry points for drivers and helpers respectively.

The states read out aren't the current states of the objects yet, so they also need to be installed. Introduce the drm_mode_config_helper_funcs.atomic_sro_install_state hook to install a whole drm_atomic_sro_state, and an optional atomic_sro_install_state hook in drm_crtc_funcs, drm_plane_funcs, drm_connector_funcs and drm_private_state_funcs. The latter is called before the state of an object is replaced, so that drivers can acquire the resources needed to keep the hardware state active.

Finally, add drm_atomic_sro_can_readout() to verify that all objects on a device implement the readout hook, and wire it into drm_atomic_sro_device_can_readout().

---
 drivers/gpu/drm/drm_atomic_state_readout.c |  95 ++++++++++-----
 include/drm/drm_atomic.h                   |  49 ++++++++
 include/drm/drm_atomic_state_readout.h     | 178 ++++++++++++++++++++++++++++-
 include/drm/drm_bridge.h                   |  47 ++++++++
 include/drm/drm_connector.h                |  50 ++++++++
 include/drm/drm_crtc.h                     |  55 +++++++++
 include/drm/drm_mode_config.h              |  22 ++++
 include/drm/drm_modeset_helper_vtables.h   |  58 ++++++++++
 include/drm/drm_plane.h                    |  50 ++++++++
 9 files changed, 573 insertions(+), 31 deletions(-)
drivers/gpu/drm/drm_atomic_state_readout.c +65 -30
index b8ca4cbbc5b2..dbffbb839e51 100644
@@ -4,12 +4,14 @@
 #include <drm/drm_atomic_state_readout.h>
 #include <drm/drm_bridge.h>
 #include <drm/drm_connector.h>
 #include <drm/drm_crtc.h>
 #include <drm/drm_drv.h>
+#include <drm/drm_modeset_helper_vtables.h>
 #include <drm/drm_plane.h>
 #include <drm/drm_print.h>
+#include <drm/drm_vblank.h>
 #include <linux/module.h>
 #include <linux/sched/mm.h>
 
 #include "drm_internal.h"
 #include "drm_crtc_internal.h"
@@ -17,10 +19,67 @@
 static unsigned int atomic_readout = DRM_ATOMIC_READOUT_ENABLED;
 module_param_unsafe(atomic_readout, uint, 0400);
 MODULE_PARM_DESC(atomic_readout,
 		 "Enable Hardware State Readout (0 = disabled, 1 = enabled, 2 = ignore missing compares, 3 = ignore missing readouts and compares, default = 1)");
 
+static bool drm_atomic_sro_can_readout(struct drm_device *dev)
+{
+	struct drm_crtc *crtc;
+	struct drm_plane *plane;
+	struct drm_connector *connector;
+	struct drm_private_obj *privobj;
+	struct drm_connector_list_iter conn_iter;
+
+	if (!dev->mode_config.helper_private ||
+	    !dev->mode_config.helper_private->atomic_sro_build_state)
+		return false;
+
+	if (atomic_readout == DRM_ATOMIC_READOUT_SKIP_MISSING_READOUT)
+		return true;
+
+	if (!dev->mode_config.funcs->atomic_sro_readout_state)
+		return false;
+
+	drm_for_each_privobj(privobj, dev) {
+		if (!privobj->funcs->atomic_sro_readout_state) {
+			drm_dbg_atomic(dev,
+				       "Private object %s missing readout callback",
+				       privobj->name);
+			return false;
+		}
+	}
+
+	drm_for_each_plane(plane, dev) {
+		if (!plane->funcs->atomic_sro_readout_state) {
+			drm_dbg_atomic(dev, "Plane %s missing readout callback",
+				       plane->name);
+			return false;
+		}
+	}
+
+	drm_for_each_crtc(crtc, dev) {
+		if (!crtc->funcs->atomic_sro_readout_state) {
+			drm_dbg_atomic(dev, "CRTC %s missing readout callback",
+				       crtc->name);
+			return false;
+		}
+	}
+
+	drm_connector_list_iter_begin(dev, &conn_iter);
+	drm_for_each_connector_iter(connector, &conn_iter) {
+		if (!connector->funcs->atomic_sro_readout_state) {
+			drm_dbg_atomic(dev, "Connector %s missing readout callback",
+				       connector->name);
+			drm_connector_list_iter_end(&conn_iter);
+			return false;
+		}
+	}
+	drm_connector_list_iter_end(&conn_iter);
+
+	return true;
+}
+
 /**
  * drm_atomic_sro_device_can_readout - check if a device supports hardware state readout
  * @dev: DRM device to check
  *
  * Verifies that the device is an atomic driver, that readout is
@@ -31,50 +90,26 @@ MODULE_PARM_DESC(atomic_readout,
  * True if the device supports full hardware state readout, false
  * otherwise.
  */
 bool drm_atomic_sro_device_can_readout(struct drm_device *dev)
 {
+	bool ret;
+
 	if (!drm_drv_uses_atomic_modeset(dev))
 		return false;
 
 	if (atomic_readout == DRM_ATOMIC_READOUT_DISABLED)
 		return false;
 
+	ret = drm_atomic_sro_can_readout(dev);
+	if (!ret)
+		return false;
+
 	return true;
 }
 EXPORT_SYMBOL(drm_atomic_sro_device_can_readout);
 
-struct __drm_atomic_sro_plane {
-	struct drm_plane *ptr;
-	struct drm_plane_state *state;
-};
-
-struct __drm_atomic_sro_crtc {
-	struct drm_crtc *ptr;
-	struct drm_crtc_state *state;
-};
-
-struct __drm_atomic_sro_connector {
-	struct drm_connector *ptr;
-	struct drm_connector_state *state;
-};
-
-struct __drm_atomic_sro_private_obj {
-	struct drm_private_obj *ptr;
-	struct drm_private_state *state;
-};
-
-struct drm_atomic_sro_state {
-	struct drm_device *dev;
-	struct __drm_atomic_sro_plane *planes;
-	struct __drm_atomic_sro_crtc *crtcs;
-	struct __drm_atomic_sro_connector *connectors;
-	unsigned int max_connector_index;
-	struct __drm_atomic_sro_private_obj *private_objs;
-	unsigned int num_private_objs;
-};
-
 static unsigned int max_connector_index(struct drm_device *dev)
 {
 	struct drm_connector_list_iter conn_iter;
 	struct drm_connector *connector;
 	unsigned int max = 0;
include/drm/drm_atomic.h +49 -0

59 lines not on this page · raw message

include/drm/drm_atomic_state_readout.h +177 -1

198 lines not on this page · raw message

include/drm/drm_bridge.h +47 -0

57 lines not on this page · raw message

include/drm/drm_connector.h +50 -0

70 lines not on this page · raw message

include/drm/drm_crtc.h +55 -0

75 lines not on this page · raw message

include/drm/drm_mode_config.h +22 -0
index 084517beb924..dd555f8727ad 100644
@@ -319,10 +319,32 @@ struct drm_mode_config_funcs {
 	 *
 	 * Subclassing of &drm_atomic_commit is deprecated in favour of using
 	 * &drm_private_state and &drm_private_obj.
 	 */
 	void (*atomic_state_free)(struct drm_atomic_commit *state);
+
+	/**
+	 * @atomic_sro_readout_state:
+	 *
+	 * This optional hook is called at initialization time to read
+	 * out the hardware state and initialize the DRM objects' atomic
+	 * states from it.
+	 *
+	 * When implemented, the framework will prefer hardware state
+	 * readout over creating pristine default states, enabling the
+	 * first modeset to be skipped if the firmware already set up
+	 * the display (flicker-free boot).
+	 *
+	 * If the readout fails, for example because the hardware was
+	 * programmed with a configuration the driver doesn't support,
+	 * the state initialization will fall back to the regular path.
+	 *
+	 * RETURNS:
+	 *
+	 * 0 on success, a negative error code otherwise.
+	 */
+	 int (*atomic_sro_readout_state)(struct drm_device *dev);
 };
 
 /**
  * struct drm_mode_config - Mode configuration control structure
  * @min_width: minimum fb pixel width on this device
include/drm/drm_modeset_helper_vtables.h +58 -0

66 lines not on this page · raw message

include/drm/drm_plane.h +50 -0

70 lines not on this page · raw message

Signature · 1 line
2.56.1
#26 lore raw

Re: [PATCH v9 15/42] drm/atomic_sro: Add atomic state readout infrastructure

Hi Maxime,

For a long time, we have implemented something similar through our own hack. Thank you very much for this series. Please see below:

Quoted 28 lines · Maxime Ripard

In order to enable drivers to read their initial state from the hardware, each KMS object needs a hook to fill a pre-allocated state from the hardware registers.

Introduce the atomic_sro_readout_state hook in every KMS object funcs
vtable: drm_crtc_funcs, drm_plane_funcs, drm_connector_funcs, drm_bridge_funcs, and drm_private_state_funcs.

Also introduce the drm_mode_config_funcs.atomic_sro_readout_state and drm_mode_config_helper_funcs.atomic_sro_build_state hooks, which are the top-level entry points for drivers and helpers respectively.

The states read out aren't the current states of the objects yet, so they also need to be installed. Introduce the drm_mode_config_helper_funcs.atomic_sro_install_state hook to install a whole drm_atomic_sro_state, and an optional atomic_sro_install_state hook in drm_crtc_funcs, drm_plane_funcs, drm_connector_funcs and drm_private_state_funcs. The latter is called before the state of an object is replaced, so that drivers can acquire the resources needed to keep the hardware state active.

Finally, add drm_atomic_sro_can_readout() to verify that all objects on a device implement the readout hook, and wire it into drm_atomic_sro_device_can_readout().

---

[...]

Quoted 10 lines
include/drm/drm_plane.h +10 -0
+
+	/**
+	 * @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,

include/drm/drm_plane.h struct drm_plane_funcs { · line 446

Quoted 10 lines
include/drm/drm_plane.h +10 -0
+	 * clocks, or interrupts.
+	 *
+	 * This hook cannot fail. It is called by the
+	 * &drm_mode_config_helper_funcs.atomic_sro_install_state
+	 * implementation, 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);
+

Due to the presence of clk_disable_unused(), the relevant clk is likely to be disabled prematurely, which may cause symptoms such as a black screen.

How does atomic_sro_install_state() ensure that it is called early enough, thereby enabling the driver to turn on the corresponding clock?

Or is this something that driver implementers need to consider, and should the DRM core provide some kind of helper? Thank you.

Signature · 2 lines
Best, 
Chaoyi
author maintainer
#27 lore raw

Re: [PATCH v9 15/42] drm/atomic_sro: Add atomic state readout infrastructure

Hi Chaoyi,

Quoted 4 lines · Chaoyi Chen

Hi Maxime,

For a long time, we have implemented something similar through our own hack. Thank you very much for this series.

I'm glad it's useful :)

Quoted 59 lines

Please see below:

Quoted 28 lines · Maxime Ripard

In order to enable drivers to read their initial state from the hardware, each KMS object needs a hook to fill a pre-allocated state from the hardware registers.

Introduce the atomic_sro_readout_state hook in every KMS object funcs
vtable: drm_crtc_funcs, drm_plane_funcs, drm_connector_funcs, drm_bridge_funcs, and drm_private_state_funcs.

Also introduce the drm_mode_config_funcs.atomic_sro_readout_state and drm_mode_config_helper_funcs.atomic_sro_build_state hooks, which are the top-level entry points for drivers and helpers respectively.

The states read out aren't the current states of the objects yet, so they also need to be installed. Introduce the drm_mode_config_helper_funcs.atomic_sro_install_state hook to install a whole drm_atomic_sro_state, and an optional atomic_sro_install_state hook in drm_crtc_funcs, drm_plane_funcs, drm_connector_funcs and drm_private_state_funcs. The latter is called before the state of an object is replaced, so that drivers can acquire the resources needed to keep the hardware state active.

Finally, add drm_atomic_sro_can_readout() to verify that all objects on a device implement the readout hook, and wire it into drm_atomic_sro_device_can_readout().

---

[...]

Quoted 10 lines
include/drm/drm_plane.h +10 -0
+
+	/**
+	 * @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,

include/drm/drm_plane.h struct drm_plane_funcs { · line 446

Quoted 10 lines
include/drm/drm_plane.h +10 -0
+	 * clocks, or interrupts.
+	 *
+	 * This hook cannot fail. It is called by the
+	 * &drm_mode_config_helper_funcs.atomic_sro_install_state
+	 * implementation, 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);
+

Due to the presence of clk_disable_unused(), the relevant clk is likely to be disabled prematurely, which may cause symptoms such as a black screen.

Quoted 5 lines

How does atomic_sro_install_state() ensure that it is called early enough, thereby enabling the driver to turn on the corresponding clock?

Or is this something that driver implementers need to consider, and should the DRM core provide some kind of helper? Thank you.

I guess you have mainly three options, depending on what your preferred setup is:

- If your display driver is compiled as a built-in, it's a non-issue since the read-out will have been done before clk_disable_unused() runs.

- The typical "distro" setup is that simpledrm will be builtin, grab the clock, we'll then load the driver as a module, do the readout, call install_state to get a clock reference and then drop simpledrm and its clock reference.

In such a case, you always have a prepare / enable count > 1 so the clock never gets disabled by clk_disable_unused().

- If you don't want to have simpledrm but have the display driver as a module, and thus need to keep the clock running even though clk_disable_unused runs, I guess your best bet is to look into the sync_state Brian has been working on:
https://lore.kernel.org/linux-clk/[email protected]/

Maxime

#28 lore raw

Re: [PATCH v9 15/42] drm/atomic_sro: Add atomic state readout infrastructure

Quoted 90 lines · Maxime Ripard

Hi Chaoyi,

Quoted 4 lines · Chaoyi Chen

Hi Maxime,

For a long time, we have implemented something similar through our own hack. Thank you very much for this series.

I'm glad it's useful :)

Quoted 59 lines

Please see below:

Quoted 28 lines · Maxime Ripard

In order to enable drivers to read their initial state from the hardware, each KMS object needs a hook to fill a pre-allocated state from the hardware registers.

Introduce the atomic_sro_readout_state hook in every KMS object funcs
vtable: drm_crtc_funcs, drm_plane_funcs, drm_connector_funcs, drm_bridge_funcs, and drm_private_state_funcs.

Also introduce the drm_mode_config_funcs.atomic_sro_readout_state and drm_mode_config_helper_funcs.atomic_sro_build_state hooks, which are the top-level entry points for drivers and helpers respectively.

The states read out aren't the current states of the objects yet, so they also need to be installed. Introduce the drm_mode_config_helper_funcs.atomic_sro_install_state hook to install a whole drm_atomic_sro_state, and an optional atomic_sro_install_state hook in drm_crtc_funcs, drm_plane_funcs, drm_connector_funcs and drm_private_state_funcs. The latter is called before the state of an object is replaced, so that drivers can acquire the resources needed to keep the hardware state active.

Finally, add drm_atomic_sro_can_readout() to verify that all objects on a device implement the readout hook, and wire it into drm_atomic_sro_device_can_readout().

---

[...]

Quoted 10 lines
include/drm/drm_plane.h +10 -0
+
+	/**
+	 * @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,

include/drm/drm_plane.h struct drm_plane_funcs { · line 446

Quoted 10 lines
include/drm/drm_plane.h +10 -0
+	 * clocks, or interrupts.
+	 *
+	 * This hook cannot fail. It is called by the
+	 * &drm_mode_config_helper_funcs.atomic_sro_install_state
+	 * implementation, 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);
+

Due to the presence of clk_disable_unused(), the relevant clk is likely to be disabled prematurely, which may cause symptoms such as a black screen.

Quoted 5 lines

How does atomic_sro_install_state() ensure that it is called early enough, thereby enabling the driver to turn on the corresponding clock?

Or is this something that driver implementers need to consider, and should the DRM core provide some kind of helper? Thank you.

I guess you have mainly three options, depending on what your preferred setup is:

- If your display driver is compiled as a built-in, it's a non-issue since the read-out will have been done before clk_disable_unused() runs.

- The typical "distro" setup is that simpledrm will be builtin, grab the clock, we'll then load the driver as a module, do the readout, call install_state to get a clock reference and then drop simpledrm and its clock reference.

In such a case, you always have a prepare / enable count > 1 so the clock never gets disabled by clk_disable_unused().

Quoted 7 lines

- If you don't want to have simpledrm but have the display driver as a module, and thus need to keep the clock running even though clk_disable_unused runs, I guess your best bet is to look into the sync_state Brian has been working on:
https://lore.kernel.org/linux-clk/[email protected]/

Maxime

Very insightful. Thank you for sharing this information. :)

Signature · 2 lines
Best, 
Chaoyi
author maintainer
#29 lore raw

PATCH v9 16/42 drm/atomic_sro: Create documentation

In reply to Maxime Ripard

The SRO infrastructure has grown to span multiple files and hooks but lacks an overview section explaining the overall design and how drivers should integrate with it.

Add a DOC: overview section to drm_atomic_state_readout.c describing the various SRO phases and how drivers wire up the callbacks.

---
 Documentation/gpu/drm-kms-helpers.rst      | 17 ++++++++++++++
 drivers/gpu/drm/drm_atomic_state_readout.c | 37 ++++++++++++++++++++++++++++++
 2 files changed, 54 insertions(+)
Documentation/gpu/drm-kms-helpers.rst +17 -0
index d64f941d34f5..b75bde712faf 100644
@@ -80,10 +80,27 @@ Atomic State Helper Reference
 -----------------------------
 
 .. kernel-doc:: drivers/gpu/drm/drm_atomic_state_helper.c
    :export:
 
+.. _drm_atomic_sro:
+
+Atomic State Read-Out
+---------------------
+
+.. kernel-doc:: drivers/gpu/drm/drm_atomic_state_readout.c
+   :doc: overview
+
+Atomic State Read-Out Reference
+--------------------------------
+
+.. kernel-doc:: include/drm/drm_atomic_state_readout.h
+   :internal:
+
+.. kernel-doc:: drivers/gpu/drm/drm_atomic_state_readout.c
+   :export:
+
 GEM Atomic Helper Reference
 ---------------------------
 
 .. kernel-doc:: drivers/gpu/drm/drm_gem_atomic_helper.c
    :doc: overview
drivers/gpu/drm/drm_atomic_state_readout.c +37 -0
index dbffbb839e51..67a90202d698 100644
@@ -14,10 +14,47 @@
 #include <linux/sched/mm.h>
 
 #include "drm_internal.h"
 #include "drm_crtc_internal.h"
 
+/**
+ * DOC: overview
+ *
+ * The atomic State Read-Out (SRO) infrastructure allows drivers to
+ * initialize the KMS atomic state from the hardware state left by the
+ * firmware at boot, rather than programming a new state. This enables
+ * flicker-free boot (also called "fastboot" by i915): if the
+ * firmware already configured the display, the first userspace
+ * modeset can be skipped when the requested mode matches.
+ *
+ * The SRO lifecycle has two phases. The first phase is the readout
+ * itself: at driver registration time, each KMS object (CRTCs, planes,
+ * connectors, bridges, private objects) has its
+ * atomic_sro_readout_state hook called to populate a
+ * &struct drm_atomic_sro_state from hardware registers.
+ *
+ * The hardware may have been programmed with a configuration that
+ * the driver doesn't support. The readout hooks are then expected
+ * to return a negative error code, and the state initialization
+ * will fall back to the regular path.
+ *
+ * The second phase is the installation. Once all states have been read
+ * out, &drm_mode_config_helper_funcs.atomic_sro_install_state walks
+ * through the &struct drm_atomic_sro_state and assigns each readout
+ * state as the object's current state. Before doing so, it calls the
+ * optional atomic_sro_install_state hook on each object. This gives
+ * drivers a chance to acquire the resources needed to keep the hardware
+ * state active, such as power domains, clocks, or interrupts. This hook
+ * cannot fail.
+ *
+ * Drivers integrate with SRO by implementing the readout hooks in their
+ * object funcs vtables and setting the
+ * &drm_mode_config_funcs.atomic_sro_readout_state,
+ * &drm_mode_config_helper_funcs.atomic_sro_install_state, and
+ * &drm_mode_config_helper_funcs.atomic_sro_build_state callbacks.
+ */
+
 static unsigned int atomic_readout = DRM_ATOMIC_READOUT_ENABLED;
 module_param_unsafe(atomic_readout, uint, 0400);
 MODULE_PARM_DESC(atomic_readout,
 		 "Enable Hardware State Readout (0 = disabled, 1 = enabled, 2 = ignore missing compares, 3 = ignore missing readouts and compares, default = 1)");
 
Signature · 1 line
2.56.1
author maintainer
#30 lore raw

PATCH v9 17/42 drm/bridge: Handle bridges with hardware state readout

In reply to Maxime Ripard

Bridges use drm_private_obj for their atomic state, but their readout needs special handling compared to other private objects.

Bridge drivers need the CRTC and connector states for their pipeline to properly read out their own state. Since bridge registration does not guarantee ordering, the readout must traverse each encoder's bridge chain to ensure each bridge can query the state of preceding bridges.

Add a drm_bridge_atomic_readout_priv_state() wrapper that looks up the connector and CRTC states for the bridge's encoder, then forwards to the bridge's atomic_sro_readout_state hook. Create a separate drm_private_state_funcs vtable for bridges that support SRO so that drm_atomic_sro_device_can_readout() can properly discriminate bridges with and without SRO support.

The same applies when installing the state. Add an atomic_sro_install_state hook to drm_bridge_funcs, and a drm_bridge_atomic_install_priv_state() wrapper in that vtable that calls it before the state of the bridge is replaced. The wrapper looks up the connector and CRTC states of the pipeline the bridge is part of, and only calls the hook if the pipeline was read out as active.

---
 drivers/gpu/drm/drm_bridge.c | 138 ++++++++++++++++++++++++++++++++++++++++++-
 include/drm/drm_bridge.h     |  27 +++++++++
 2 files changed, 162 insertions(+), 3 deletions(-)
drivers/gpu/drm/drm_bridge.c +135 -3
index b6cdbc172f68..868558a09f95 100644
@@ -28,10 +28,11 @@
 #include <linux/module.h>
 #include <linux/mutex.h>
 #include <linux/srcu.h>
 
 #include <drm/drm_atomic_state_helper.h>
+#include <drm/drm_atomic_state_readout.h>
 #include <drm/drm_bridge.h>
 #include <drm/drm_debugfs.h>
 #include <drm/drm_edid.h>
 #include <drm/drm_encoder.h>
 #include <drm/drm_file.h>
@@ -562,10 +563,118 @@ drm_bridge_atomic_create_priv_state(struct drm_private_obj *obj)
 		return ERR_CAST(state);
 
 	return &state->base;
 }
 
+static struct drm_connector_state *
+find_connector_state_for_encoder(struct drm_atomic_sro_state *state,
+				struct drm_encoder *encoder)
+{
+	struct drm_connector_list_iter conn_iter;
+	struct drm_connector *connector;
+
+	drm_connector_list_iter_begin(drm_atomic_sro_state_get_device(state),
+				      &conn_iter);
+	drm_for_each_connector_iter(connector, &conn_iter) {
+		struct drm_connector_state *conn_state =
+			drm_atomic_sro_get_connector_state(state, connector);
+
+		/*
+		 * The connector may not have a state in the readout
+		 * state: when the readout is limited to a commit, only
+		 * the connectors of that commit are read out.
+		 */
+		if (!conn_state)
+			continue;
+
+		if (encoder == conn_state->best_encoder) {
+			drm_connector_list_iter_end(&conn_iter);
+			return conn_state;
+		}
+	}
+	drm_connector_list_iter_end(&conn_iter);
+
+	return NULL;
+}
+
+static int
+drm_bridge_atomic_readout_priv_state(struct drm_private_obj *obj,
+				     struct drm_atomic_sro_state *state,
+				     struct drm_private_state *priv_state)
+{
+	struct drm_bridge *bridge = drm_priv_to_bridge(obj);
+	struct drm_encoder *encoder = bridge->encoder;
+	const struct drm_bridge_funcs *bridge_funcs = bridge->funcs;
+	struct drm_crtc_state *crtc_state;
+	struct drm_bridge_state *bridge_state;
+	struct drm_connector_state *conn_state;
+	int ret;
+
+	/*
+	 * If the bridge's encoder has no routed connector or no
+	 * active CRTC, the pipeline is disabled and there is nothing
+	 * to read out. Keep the pristine default state created by
+	 * &drm_mode_config_helper_funcs.atomic_sro_build_state.
+	 */
+	conn_state = find_connector_state_for_encoder(state, encoder);
+	if (!conn_state)
+		return 0;
+
+	if (!conn_state->crtc)
+		return 0;
+
+	crtc_state = drm_atomic_sro_get_crtc_state(state, conn_state->crtc);
+	if (!crtc_state)
+		return 0;
+
+	bridge_state = drm_priv_to_bridge_state(priv_state);
+	if (bridge_funcs->atomic_sro_readout_state) {
+		ret = bridge_funcs->atomic_sro_readout_state(
+			bridge, state, bridge_state, crtc_state, conn_state);
+		if (ret)
+			return ret;
+	}
+
+	return 0;
+}
+
+static void
+drm_bridge_atomic_install_priv_state(struct drm_private_obj *obj,
+				     struct drm_private_state *priv_state)
+{
+	struct drm_bridge *bridge = drm_priv_to_bridge(obj);
+	struct drm_connector_state *conn_state;
+	struct drm_crtc_state *crtc_state;
+	struct drm_connector *connector;
+
+	if (!bridge->funcs->atomic_sro_install_state)
+		return;
+
+	/*
+	 * &drm_mode_config_helper_funcs.atomic_sro_install_state
+	 * installs the connector and CRTC states before the private
+	 * objects ones, so we can look up the pipeline through the
+	 * current states. The state readout runs with all the modeset
+	 * locks held, including the connection mutex.
+	 */
+	connector = drm_atomic_get_connector_for_encoder_locked(bridge->encoder);
+	if (IS_ERR(connector))
+		return;
+
+	conn_state = connector->state;
+	if (!conn_state->crtc)
+		return;
+
+	crtc_state = conn_state->crtc->state;
+	if (!crtc_state || !crtc_state->active)
+		return;
+
+	bridge->funcs->atomic_sro_install_state(bridge,
+						drm_priv_to_bridge_state(priv_state),
+						crtc_state, conn_state);
+}
+
 static void
 drm_bridge_atomic_print_priv_state(struct drm_printer *p,
 				   const struct drm_private_state *s)
 {
 	const struct drm_bridge_state *state = drm_priv_to_bridge_state(s);
@@ -589,10 +698,19 @@ static const struct drm_private_state_funcs drm_bridge_priv_state_funcs = {
 	.atomic_duplicate_state = drm_bridge_atomic_duplicate_priv_state,
 	.atomic_destroy_state = drm_bridge_atomic_destroy_priv_state,
 	.atomic_print_state = drm_bridge_atomic_print_priv_state,
 };
 
+static const struct drm_private_state_funcs drm_bridge_priv_state_funcs_with_sro = {
+	.atomic_sro_readout_state = drm_bridge_atomic_readout_priv_state,
+	.atomic_sro_install_state = drm_bridge_atomic_install_priv_state,
+	.atomic_create_state = drm_bridge_atomic_create_priv_state,
+	.atomic_duplicate_state = drm_bridge_atomic_duplicate_priv_state,
+	.atomic_destroy_state = drm_bridge_atomic_destroy_priv_state,
+	.atomic_print_state = drm_bridge_atomic_print_priv_state,
+};
+
 /**
  * drm_private_obj_is_bridge - check if a private object backs a bridge
  * @obj: private object to check
  *
  * RETURNS:
@@ -600,11 +718,13 @@ static const struct drm_private_state_funcs drm_bridge_priv_state_funcs = {
  * True if @obj is the &drm_private_obj embedded in a &struct drm_bridge,
  * false otherwise.
  */
 bool drm_private_obj_is_bridge(struct drm_private_obj *obj)
 {
-	return obj && obj->funcs == &drm_bridge_priv_state_funcs;
+	return obj &&
+	       (obj->funcs == &drm_bridge_priv_state_funcs ||
+		obj->funcs == &drm_bridge_priv_state_funcs_with_sro);
 }
 EXPORT_SYMBOL(drm_private_obj_is_bridge);
 
 /**
  * drm_bridge_attach - attach the bridge to an encoder's chain
@@ -675,15 +795,27 @@ int drm_bridge_attach(struct drm_encoder *encoder, struct drm_bridge *bridge,
 		ret = bridge->funcs->attach(bridge, encoder, flags);
 		if (ret < 0)
 			goto err_reset_bridge;
 	}
 
-	drm_atomic_private_obj_init(bridge->dev, &bridge->base, bridge->name,
-				    &drm_bridge_priv_state_funcs);
+	if (bridge->funcs &&
+	    bridge->funcs->atomic_sro_readout_state)
+		ret = drm_atomic_private_obj_init(bridge->dev, &bridge->base,
+						  bridge->name,
+						  &drm_bridge_priv_state_funcs_with_sro);
+	else
+		ret = drm_atomic_private_obj_init(bridge->dev, &bridge->base,
+						  bridge->name,
+						  &drm_bridge_priv_state_funcs);
+	if (ret)
+		goto err_detach_bridge;
 
 	return 0;
 
+err_detach_bridge:
+	if (bridge->funcs->detach)
+		bridge->funcs->detach(bridge);
 err_reset_bridge:
 	bridge->dev = NULL;
 	bridge->encoder = NULL;
 	mutex_lock(&encoder->bridge_chain_mutex);
 	list_del(&bridge->chain_node);
include/drm/drm_bridge.h +27 -0
index ab0a6c03c8dd..7e30877a81cc 100644
@@ -325,10 +325,37 @@ struct drm_bridge_funcs {
 					struct drm_atomic_sro_state *state,
 					struct drm_bridge_state *bridge_state,
 					struct drm_crtc_state *crtc_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 bridge's current
+	 * state, and only if the bridge pipeline was read out as
+	 * active.
+	 *
+	 * It allows drivers to acquire the resources needed to keep
+	 * the current hardware state active, such as power supplies,
+	 * clocks, or interrupts, so that the next disable of the
+	 * pipeline releases resources that were actually acquired.
+	 *
+	 * The @crtc_state and @conn_state parameters point to the CRTC
+	 * and connector states for the pipeline this bridge is part
+	 * of, which have already been installed.
+	 *
+	 * This hook cannot fail. It is called by the
+	 * &drm_mode_config_helper_funcs.atomic_sro_install_state
+	 * implementation, which is part of the hardware state readout
+	 * initialization sequence.
+	 */
+	void (*atomic_sro_install_state)(struct drm_bridge *bridge,
+					 struct drm_bridge_state *bridge_state,
+					 struct drm_crtc_state *crtc_state,
+					 struct drm_connector_state *conn_state);
+
 	/**
 	 * @atomic_duplicate_state:
 	 *
 	 * Duplicate the current bridge state object (which is guaranteed to be
 	 * non-NULL).
Signature · 1 line
2.56.1
#31 lore raw

Re: [PATCH v9 17/42] drm/bridge: Handle bridges with hardware state readout

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] drm/bridge: check crtc_state->active in drm_bridge_atomic_readout_priv_state() --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] drm/bridge: check crtc_state->active in drm_bridge_atomic_readout_priv_state()
--

commit f5a0fcf4814d5306f865f770861315035338896e
Author: Maxime Ripard <[email protected]>

drm/bridge: Handle bridges with hardware state readout

This commit adds hardware state readout and installation wrappers for DRM bridges, looking up the corresponding connector and CRTC states for the bridge's pipeline before invoking the bridge hooks.

Quoted 4 lines
drivers/gpu/drm/drm_bridge.c +0 -0
index b6cdbc172f689..868558a09f951 100644

[ ... ]

Quoted 19 lines
drivers/gpu/drm/drm_bridge.c +19 -0
+static int
+drm_bridge_atomic_readout_priv_state(struct drm_private_obj *obj,
+				     struct drm_atomic_sro_state *state,
+				     struct drm_private_state *priv_state)
+{
+	struct drm_bridge *bridge = drm_priv_to_bridge(obj);
+	struct drm_encoder *encoder = bridge->encoder;
+	const struct drm_bridge_funcs *bridge_funcs = bridge->funcs;
+	struct drm_crtc_state *crtc_state;
+	struct drm_bridge_state *bridge_state;
+	struct drm_connector_state *conn_state;
+	int ret;
+
+	/*
+	 * If the bridge's encoder has no routed connector or no
+	 * active CRTC, the pipeline is disabled and there is nothing
+	 * to read out. Keep the pristine default state created by
+	 * &drm_mode_config_helper_funcs.atomic_sro_build_state.
+	 */

drivers/gpu/drm/drm_bridge.c drm_bridge_atomic_create_priv_state(struct drm_private_obj *obj) · line 627

Quoted 10 lines
drivers/gpu/drm/drm_bridge.c +10 -0
+	conn_state = find_connector_state_for_encoder(state, encoder);
+	if (!conn_state)
+		return 0;
+
+	if (!conn_state->crtc)
+		return 0;
+
+	crtc_state = drm_atomic_sro_get_crtc_state(state, conn_state->crtc);
+	if (!crtc_state)
+		return 0;

[Severity: Medium]
Should this check !crtc_state->active as well, matching the comment above and drm_bridge_atomic_install_priv_state() below?

drm_atomic_helper_sro_readout_crtcs_state() populates a non-NULL crtc_state in the readout container even when the CRTC is inactive, so checking only !crtc_state will still invoke atomic_sro_readout_state() on a disabled pipeline if conn_state->crtc is set while crtc_state->active is false.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=17
author maintainer
#32 lore raw

PATCH v9 18/42 drm/mode-config: Read out hardware state in drm_mode_config_create_initial_state

In reply to Maxime Ripard

drm_mode_config_create_initial_state() creates the initial state for all KMS objects at driver registration time.

Call the drm_mode_config_funcs.atomic_sro_readout_state hook from it so that the initial state is populated from the hardware rather than being left as a pristine default. If the readout isn't supported or fails, fall back to allocating pristine states, which moves to a static drm_mode_config_create_state() helper.

---
 drivers/gpu/drm/drm_mode_config.c | 69 ++++++++++++++++++++++++++++++---------
 1 file changed, 54 insertions(+), 15 deletions(-)
drivers/gpu/drm/drm_mode_config.c +54 -15
index c1e993a78702..d6f92e1a54d2 100644
@@ -29,10 +29,11 @@
 #include <drm/drm_file.h>
 #include <drm/drm_framebuffer.h>
 #include <drm/drm_managed.h>
 #include <drm/drm_mode_config.h>
 #include <drm/drm_modeset_helper_vtables.h>
+#include <drm/drm_modeset_lock.h>
 #include <drm/drm_print.h>
 #include <drm/drm_colorop.h>
 #include <linux/dma-resv.h>
 
 #include "drm_crtc_internal.h"
@@ -320,25 +321,11 @@ void drm_mode_config_reset(struct drm_device *dev)
 	}
 	drm_connector_list_iter_end(&conn_iter);
 }
 EXPORT_SYMBOL(drm_mode_config_reset);
 
-/**
- * drm_mode_config_create_initial_state - Allocates the initial state
- * @dev: drm device
- *
- * This functions creates the initial state for all the objects. Drivers
- * can use this in e.g. probe to initialize their software state.
- *
- * It has two main differences with drm_mode_config_reset(): the reset()
- * hooks aren't called and thus the hardware will be left untouched, but
- * also the &drm_private_obj structures will be initialized as opposed
- * to drm_mode_config_reset() that skips them.
- *
- * Returns: 0 on success, negative error value on failure.
- */
-int drm_mode_config_create_initial_state(struct drm_device *dev)
+static int drm_mode_config_create_state(struct drm_device *dev)
 {
 	struct drm_crtc *crtc;
 	struct drm_colorop *colorop;
 	struct drm_plane *plane;
 	struct drm_connector *connector;
@@ -406,10 +393,62 @@ int drm_mode_config_create_initial_state(struct drm_device *dev)
 	}
 	drm_connector_list_iter_end(&conn_iter);
 
 	return 0;
 }
+
+/**
+ * drm_mode_config_create_initial_state - Allocates the initial state
+ * @dev: drm device
+ *
+ * This functions creates the initial state for all the objects. Drivers
+ * can use this in e.g. probe to initialize their state.
+ *
+ * It creates the initial state in several steps. It first tries to
+ * read-out the initial state from the hardware for all objects if state
+ * read-out is enabled and the driver supports it. Then, if state
+ * read-out isn't supported or fails, it creates a new, pristine state
+ * using the objects atomic_create_state callback.
+ *
+ * For historical reasons, drm_mode_config_reset() is similar, but
+ * doesn't support state read-out, and will always reset the hardware.
+ * It will also ignore all &drm_private_obj.
+ *
+ * The function holds all the modeset locks for the duration of the
+ * readout and the state creation, so drivers don't need to guarantee
+ * any kind of exclusivity themselves.
+ *
+ * Returns: 0 on success, negative error value on failure.
+ */
+int drm_mode_config_create_initial_state(struct drm_device *dev)
+{
+	struct drm_modeset_acquire_ctx ctx;
+	int ret;
+
+	/*
+	 * The initial state is usually created while the device is
+	 * being initialized and nothing can access it concurrently,
+	 * but holding all the modeset locks puts the readout, the
+	 * installation of its result and the creation of the pristine
+	 * states under the same locking contract as the regular
+	 * atomic paths.
+	 */
+	DRM_MODESET_LOCK_ALL_BEGIN(dev, ctx, 0, ret);
+
+	if (dev->mode_config.funcs->atomic_sro_readout_state) {
+		ret = dev->mode_config.funcs->atomic_sro_readout_state(dev);
+		if (!ret)
+			goto out;
+	}
+
+	ret = drm_mode_config_create_state(dev);
+
+out:
+	DRM_MODESET_LOCK_ALL_END(dev, ctx, ret);
+
+	return ret;
+}
 EXPORT_SYMBOL(drm_mode_config_create_initial_state);
 
 /*
  * Global properties
  */
Signature · 1 line
2.56.1
author maintainer
#33 lore raw

PATCH v9 19/42 drm/atomic_sro: Provide helpers to implement hardware state readout

In reply to Maxime Ripard

Drivers implementing SRO need to iterate over their KMS objects, allocate a pristine state for each, call the readout hook, and store the result in the SRO state container.

Provide helpers for each object type (drm_atomic_helper_sro_readout_planes_state(), drm_atomic_helper_sro_readout_crtcs_state(), etc.) and a top-level drm_atomic_helper_sro_build_state() that calls them in sequence.

Once built, the state must be installed as the current state of the objects. Provide drm_atomic_helper_sro_install_state(), a default &drm_mode_config_helper_funcs.atomic_sro_install_state implementation that resets the vblank state of the CRTCs, calls the atomic_sro_install_state hook of each object, and replaces the state of the object with the one that was read out.

Finally, provide drm_atomic_helper_sro_readout_state(), a default &drm_mode_config_funcs.atomic_sro_readout_state implementation tying the above together: it builds the state, installs it, and frees the container.

---
 Documentation/gpu/drm-kms-helpers.rst             |   9 +
 drivers/gpu/drm/Makefile                          |   1 +
 drivers/gpu/drm/drm_atomic_state_readout.c        |   6 +-
 drivers/gpu/drm/drm_atomic_state_readout_helper.c | 939 ++++++++++++++++++++++
 include/drm/drm_atomic_state_readout_helper.h     |  36 +
 include/drm/drm_mode_config.h                     |   5 +-
 include/drm/drm_modeset_helper_vtables.h          |   7 +-
 7 files changed, 1000 insertions(+), 3 deletions(-)
Documentation/gpu/drm-kms-helpers.rst +9 -0
index b75bde712faf..a6dd8dbb3218 100644
@@ -97,10 +97,19 @@ Atomic State Read-Out Reference
    :internal:
 
 .. kernel-doc:: drivers/gpu/drm/drm_atomic_state_readout.c
    :export:
 
+Atomic State Read-Out Helpers Reference
+----------------------------------------
+
+.. kernel-doc:: include/drm/drm_atomic_state_readout_helper.h
+   :internal:
+
+.. kernel-doc:: drivers/gpu/drm/drm_atomic_state_readout_helper.c
+   :export:
+
 GEM Atomic Helper Reference
 ---------------------------
 
 .. kernel-doc:: drivers/gpu/drm/drm_gem_atomic_helper.c
    :doc: overview
drivers/gpu/drm/Makefile +1 -0
index e94b77497514..7c8b778f4d5e 100644
@@ -141,10 +141,11 @@ obj-$(CONFIG_DRM_TTM_HELPER) += drm_ttm_helper.o
 #
 
 drm_kms_helper-y := \
 	drm_atomic_helper.o \
 	drm_atomic_state_helper.o \
+	drm_atomic_state_readout_helper.o \
 	drm_bridge_helper.o \
 	drm_crtc_helper.o \
 	drm_damage_helper.o \
 	drm_flip_work.o \
 	drm_format_helper.o \
drivers/gpu/drm/drm_atomic_state_readout.c +5 -1
index 67a90202d698..3e50b0796cbe 100644
@@ -48,11 +48,15 @@
  *
  * Drivers integrate with SRO by implementing the readout hooks in their
  * object funcs vtables and setting the
  * &drm_mode_config_funcs.atomic_sro_readout_state,
  * &drm_mode_config_helper_funcs.atomic_sro_install_state, and
- * &drm_mode_config_helper_funcs.atomic_sro_build_state callbacks.
+ * &drm_mode_config_helper_funcs.atomic_sro_build_state callbacks. The
+ * default helpers drm_atomic_helper_sro_readout_state(),
+ * drm_atomic_helper_sro_install_state(), and
+ * drm_atomic_helper_sro_build_state() handle the standard readout
+ * sequence.
  */
 
 static unsigned int atomic_readout = DRM_ATOMIC_READOUT_ENABLED;
 module_param_unsafe(atomic_readout, uint, 0400);
 MODULE_PARM_DESC(atomic_readout,
drivers/gpu/drm/drm_atomic_state_readout_helper.c new file +939 -0

939 lines not on this page · raw message

include/drm/drm_atomic_state_readout_helper.h new file +36 -0
new file mode 100644
index 000000000000..5e230da4b8fe
@@ -0,0 +1,36 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+
+#ifndef DRM_ATOMIC_SRO_HELPER_H_
+#define DRM_ATOMIC_SRO_HELPER_H_
+
+struct drm_atomic_commit;
+struct drm_atomic_sro_state;
+struct drm_device;
+
+int
+drm_atomic_helper_sro_readout_planes_state(struct drm_device *dev,
+					   struct drm_atomic_sro_state *state,
+					   struct drm_atomic_commit *commit);
+int
+drm_atomic_helper_sro_readout_crtcs_state(struct drm_device *dev,
+					  struct drm_atomic_sro_state *state,
+					  struct drm_atomic_commit *commit);
+int
+drm_atomic_helper_sro_readout_connectors_state(struct drm_device *dev,
+					       struct drm_atomic_sro_state *state,
+					       struct drm_atomic_commit *commit);
+int
+drm_atomic_helper_sro_readout_bridges_state(struct drm_device *dev,
+					    struct drm_atomic_sro_state *state,
+					    struct drm_atomic_commit *commit);
+int
+drm_atomic_helper_sro_readout_private_objs_state(struct drm_device *dev,
+						 struct drm_atomic_sro_state *state,
+						 struct drm_atomic_commit *commit);
+struct drm_atomic_sro_state *
+drm_atomic_helper_sro_build_state(struct drm_device *dev,
+				  struct drm_atomic_commit *commit);
+int drm_atomic_helper_sro_readout_state(struct drm_device *dev);
+void drm_atomic_helper_sro_install_state(struct drm_atomic_sro_state *state);
+
+#endif /* DRM_ATOMIC_SRO_HELPER_H_ */
include/drm/drm_mode_config.h +4 -1
index dd555f8727ad..e1124b314524 100644
@@ -336,15 +336,18 @@ struct drm_mode_config_funcs {
 	 *
 	 * If the readout fails, for example because the hardware was
 	 * programmed with a configuration the driver doesn't support,
 	 * the state initialization will fall back to the regular path.
 	 *
+	 * The default implementation is
+	 * drm_atomic_helper_sro_readout_state().
+	 *
 	 * RETURNS:
 	 *
 	 * 0 on success, a negative error code otherwise.
 	 */
-	 int (*atomic_sro_readout_state)(struct drm_device *dev);
+	int (*atomic_sro_readout_state)(struct drm_device *dev);
 };
 
 /**
  * struct drm_mode_config - Mode configuration control structure
  * @min_width: minimum fb pixel width on this device
include/drm/drm_modeset_helper_vtables.h +6 -1
index 8a56240702d6..650cb602ce70 100644
@@ -1618,10 +1618,13 @@ struct drm_mode_config_helper_funcs {
 	 * @commit are read out. The caller must hold the locks of the
 	 * objects in @commit through its acquire context, and the readout
 	 * hooks must not access the state of any object outside of the
 	 * commit, since its locks aren't held.
 	 *
+	 * The default implementation is
+	 * drm_atomic_helper_sro_build_state().
+	 *
 	 * RETURNS:
 	 *
 	 * A &struct drm_atomic_sro_state on success, an error pointer
 	 * otherwise.
 	 */
@@ -1650,11 +1653,13 @@ struct drm_mode_config_helper_funcs {
 	 * hardware state active.
 	 *
 	 * This hook cannot fail.
 	 *
 	 * This hook is mandatory for drivers implementing SRO, since
-	 * the read out states are dropped without it.
+	 * the read out states are dropped without it. Most drivers
+	 * should use drm_atomic_helper_sro_install_state(), which
+	 * implements all of the above.
 	 */
 	void (*atomic_sro_install_state)(struct drm_atomic_sro_state *state);
 };
 
 #endif
Signature · 1 line
2.56.1
#34 lore raw

Re: [PATCH v9 19/42] drm/atomic_sro: Provide helpers to implement hardware state readout

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] drm/atomic_sro: out-of-bounds write in drm_atomic_sro_set_connector_state() --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] drm/atomic_sro: out-of-bounds write in drm_atomic_sro_set_connector_state()
--

commit b5d5ae4218e75eb40be891ff0550153b170b927a
Author: Maxime Ripard <[email protected]>

drm/atomic_sro: Provide helpers to implement hardware state readout

This commit adds helpers to read out hardware state across KMS objects, build a drm_atomic_sro_state container, and install the read-out states.

Quoted 5 lines
drivers/gpu/drm/drm_atomic_state_readout_helper.c new file +0 -0
new file mode 100644
index 0000000000000..f290d2fe5496e

[ ... ]

Quoted 46 lines
drivers/gpu/drm/drm_atomic_state_readout_helper.c new file +46 -0
+static int
+drm_atomic_helper_sro_readout_connector_state(struct drm_device *dev,
+					      struct drm_atomic_sro_state *state,
+					      struct drm_connector *connector)
+{
+	const struct drm_connector_funcs *conn_funcs = connector->funcs;
+	struct drm_connector_state *conn_state;
+	int ret;
+
+	if (!conn_funcs->atomic_sro_readout_state) {
+		/*
+		 * Missing readout implementations are only allowed
+		 * when drm.atomic_readout skips them, but the object
+		 * still needs a valid state: fall back to a pristine
+		 * state, like drm_mode_config_create_state() would.
+		 */
+		drm_warn(dev, "No connector readout implementation.");
+
+		conn_state = conn_funcs->atomic_create_state(connector);
+		if (drm_WARN_ON(dev, IS_ERR(conn_state)))
+			return PTR_ERR(conn_state);
+
+		drm_atomic_sro_set_connector_state(state, connector, conn_state);
+		return 0;
+	}
+
+	drm_dbg_kms(dev, "Initializing connector %s state.\n",
+		    connector->name);
+
+	conn_state = conn_funcs->atomic_create_state(connector);
+	if (drm_WARN_ON(dev, IS_ERR(conn_state)))
+		return PTR_ERR(conn_state);
+
+	ret = conn_funcs->atomic_sro_readout_state(connector, state,
+						   conn_state);
+	if (ret) {
+		drm_dbg_kms(dev, "Connector %s readout failed: %pe\n",
+			    connector->name, ERR_PTR(ret));
+		conn_funcs->atomic_destroy_state(connector, conn_state);
+		return ret;
+	}
+
+	drm_atomic_sro_set_connector_state(state, connector, conn_state);
+
+	return 0;
+}

[ ... ]

Quoted 7 lines
drivers/gpu/drm/drm_atomic_state_readout_helper.c new file +7 -0
+int
+drm_atomic_helper_sro_readout_connectors_state(struct drm_device *dev,
+					       struct drm_atomic_sro_state *state,
+					       struct drm_atomic_commit *commit)
+{
+	struct drm_connector_list_iter conn_iter;
+	struct drm_connector *connector;

drivers/gpu/drm/drm_atomic_state_readout_helper.c line 350

Quoted 10 lines
drivers/gpu/drm/drm_atomic_state_readout_helper.c new file +10 -0
+	int ret;
+
+	might_alloc(GFP_KERNEL);
+
+	if (commit)
+		return drm_atomic_helper_sro_readout_commit_connectors_state(dev, state, commit);
+
+	drm_connector_list_iter_begin(dev, &conn_iter);
+	drm_for_each_connector_iter(connector, &conn_iter) {
+		ret = drm_atomic_helper_sro_readout_connector_state(dev, state, connector);

[Severity: Medium]
Can this write out of bounds of state->connectors[] if a dynamic connector is registered after drm_atomic_sro_state_alloc()?

drm_atomic_sro_state_alloc() sizes state->connectors to state->max_connector_index + 1, and the kerneldoc for max_connector_index notes that connectors registered afterwards have no entry in state->connectors. However, drm_connector_dynamic_register() adds new connectors under config->connector_list_lock without taking modeset locks. If a DP MST hotplug worker registers a connector between drm_atomic_sro_state_alloc() and drm_atomic_helper_sro_readout_connectors_state(), the connector list iterator visits it with drm_connector_index(connector) > state->max_connector_index, and drm_atomic_sro_set_connector_state() writes past the end of state->connectors[]:

drivers/gpu/drm/drm_atomic_state_readout.c:drm_atomic_sro_set_connector_state() {
	unsigned int index = drm_connector_index(conn);

	drm_connector_get(conn);
	state->connectors[index].ptr = conn;
	state->connectors[index].state = conn_state;
    ...
}

Because drm_atomic_helper_sro_install_state() and drm_atomic_sro_state_free() only iterate up to state->max_connector_index, this also leaks the connector reference and conn_state. Should drm_atomic_helper_sro_readout_connector_state() skip connectors with drm_connector_index(connector) > state->max_connector_index?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=19
author maintainer
#35 lore raw

PATCH v9 20/42 drm/mode-config: Introduce hw_reset hook

In reply to Maxime Ripard

drm_mode_config_create_initial_state() tries to read out the current hardware state to build the initial atomic state. If state readout is disabled or fails, it falls back to creating pristine software state using the objects' atomic_create_state callbacks.

However, creating pristine state without resetting the hardware leaves the hardware and software states out of sync. The legacy drm_mode_config_reset() handles this by calling per-object .reset callbacks that both reset the hardware and allocate new state, but these two concerns are intertwined and cannot be reused independently.

Introduce a new hw_reset hook in drm_mode_config_funcs that only performs the hardware reset, without allocating any software state. Call it in drm_mode_config_create_initial_state() after state readout fails, right before falling back to drm_mode_config_create_state(). This separates the hardware reset from state creation, allowing drivers that implement state readout to share their reset logic with the fallback path.

---
 drivers/gpu/drm/drm_mode_config.c |  8 +++++++-
 include/drm/drm_mode_config.h     | 32 ++++++++++++++++++++++++++++++++
 2 files changed, 39 insertions(+), 1 deletion(-)
drivers/gpu/drm/drm_mode_config.c +7 -1
index d6f92e1a54d2..0259ec48cfbc 100644
@@ -404,11 +404,12 @@ static int drm_mode_config_create_state(struct drm_device *dev)
  * can use this in e.g. probe to initialize their state.
  *
  * It creates the initial state in several steps. It first tries to
  * read-out the initial state from the hardware for all objects if state
  * read-out is enabled and the driver supports it. Then, if state
- * read-out isn't supported or fails, it creates a new, pristine state
+ * read-out isn't supported or fails, it resets the hardware using
+ * &drm_mode_config_funcs.hw_reset() and creates a new, pristine state
  * using the objects atomic_create_state callback.
  *
  * For historical reasons, drm_mode_config_reset() is similar, but
  * doesn't support state read-out, and will always reset the hardware.
  * It will also ignore all &drm_private_obj.
@@ -438,10 +439,15 @@ int drm_mode_config_create_initial_state(struct drm_device *dev)
 		ret = dev->mode_config.funcs->atomic_sro_readout_state(dev);
 		if (!ret)
 			goto out;
 	}
 
+	/* Reset the hardware first ... */
+	if (dev->mode_config.funcs->hw_reset)
+		dev->mode_config.funcs->hw_reset(dev);
+
+	/* ... then allocate a new pristine state */
 	ret = drm_mode_config_create_state(dev);
 
 out:
 	DRM_MODESET_LOCK_ALL_END(dev, ctx, ret);
 
include/drm/drm_mode_config.h +32 -0
index e1124b314524..42d37f4aba84 100644
@@ -107,10 +107,42 @@ struct drm_mode_config_funcs {
 	 * should be checked in the .mode_valid() hook for each specific object.
 	 */
 	enum drm_mode_status (*mode_valid)(struct drm_device *dev,
 					   const struct drm_display_mode *mode);
 
+	/**
+	 * @hw_reset:
+	 *
+	 * Optional hook for device-specific hardware reset.
+	 *
+	 * Unlike drm_mode_config_reset(), which both resets hardware
+	 * and creates new software state through per-object .reset
+	 * callbacks, this hook only resets the hardware to a known good
+	 * state without touching the software state at all.
+	 *
+	 * This hook is called by
+	 * drm_mode_config_create_initial_state() when the hardware
+	 * state readout (atomic_sro_readout_state) is not supported
+	 * or fails. The call sequence is:
+	 *
+	 * 1. Try atomic_sro_readout_state() to read the hardware state
+	 * 2. If that fails, call hw_reset() to put the hardware in a
+	 *    known state
+	 * 3. Create pristine software state via atomic_create_state
+	 *    callbacks
+	 *
+	 * Drivers that implement state readout and need a hardware
+	 * reset on the fallback path should implement this hook.
+	 * Drivers that don't implement state readout at all can keep
+	 * using drm_mode_config_reset() instead.
+	 *
+	 * This hook is optional. If not provided, the fallback path
+	 * will create software state without resetting the hardware
+	 * first.
+	 */
+	void (*hw_reset)(struct drm_device *dev);
+
 	/**
 	 * @atomic_check:
 	 *
 	 * This is the only hook to validate an atomic modeset update. This
 	 * function must reject any modeset and state changes which the hardware
Signature · 1 line
2.56.1
author maintainer
#36 lore raw

PATCH v9 21/42 drm/modeset-helper: Add drm_mode_config_helper_hw_reset()

In reply to Maxime Ripard

drm_mode_config_create_initial_state() calls &drm_mode_config_funcs.hw_reset when the hardware state readout fails, to put the hardware in a known-good state before creating pristine software state.

Provide drm_mode_config_helper_hw_reset() as a default implementation of &drm_mode_config_funcs.hw_reset that resets all the objects requiring it.

---
 drivers/gpu/drm/drm_modeset_helper.c | 31 +++++++++++++++++++++++++++++++
 include/drm/drm_mode_config.h        |  5 +++++
 include/drm/drm_modeset_helper.h     |  1 +
 3 files changed, 37 insertions(+)
drivers/gpu/drm/drm_modeset_helper.c +31 -0
index cca623eb23e5..1a49ab8a0e41 100644
@@ -260,5 +260,36 @@ int drm_mode_config_helper_resume(struct drm_device *dev)
 		drm_kms_helper_poll_enable(dev);
 
 	return ret;
 }
 EXPORT_SYMBOL(drm_mode_config_helper_resume);
+
+/**
+ * drm_mode_config_helper_hw_reset - default &drm_mode_config_funcs.hw_reset implementation
+ * @dev: DRM device
+ *
+ * Resets all KMS objects that provide a hardware reset hook to a
+ * known-good state, without touching the software state.
+ *
+ * Drivers can assign this function as their
+ * &drm_mode_config_funcs.hw_reset implementation.
+ */
+void drm_mode_config_helper_hw_reset(struct drm_device *dev)
+{
+	struct drm_crtc *crtc;
+
+	drm_for_each_crtc(crtc, dev) {
+		const struct drm_crtc_helper_funcs *helper_funcs =
+			crtc->helper_private;
+
+		if (helper_funcs && helper_funcs->hw_reset)
+			helper_funcs->hw_reset(crtc);
+	}
+
+	/*
+	 * TODO: Reset encoders once the semantics of
+	 * encoder->funcs->reset have been clarified. Some drivers
+	 * (e.g. i915) reset both hardware and software state, while
+	 * others (e.g. loongson) only reset hardware registers.
+	 */
+}
+EXPORT_SYMBOL(drm_mode_config_helper_hw_reset);
include/drm/drm_mode_config.h +5 -0
index 42d37f4aba84..9f8674870248 100644
@@ -133,10 +133,15 @@ struct drm_mode_config_funcs {
 	 * Drivers that implement state readout and need a hardware
 	 * reset on the fallback path should implement this hook.
 	 * Drivers that don't implement state readout at all can keep
 	 * using drm_mode_config_reset() instead.
 	 *
+	 * drm_mode_config_helper_hw_reset() resets all the objects
+	 * that require it, and can be used as the implementation of
+	 * this hook, or called from it once the device specific reset
+	 * is done.
+	 *
 	 * This hook is optional. If not provided, the fallback path
 	 * will create software state without resetting the hardware
 	 * first.
 	 */
 	void (*hw_reset)(struct drm_device *dev);
include/drm/drm_modeset_helper.h +1 -0
index 7e3d4c5a7f66..bd4193fbd907 100644
@@ -40,7 +40,8 @@ void drm_helper_mode_fill_fb_struct(struct drm_device *dev,
 int drm_crtc_init(struct drm_device *dev, struct drm_crtc *crtc,
 		  const struct drm_crtc_funcs *funcs);
 
 int drm_mode_config_helper_suspend(struct drm_device *dev);
 int drm_mode_config_helper_resume(struct drm_device *dev);
+void drm_mode_config_helper_hw_reset(struct drm_device *dev);
 
 #endif
Signature · 1 line
2.56.1
author maintainer
#37 lore raw

PATCH v9 22/42 drm/atomic_helper: Compare actual and readout states once the commit is done

In reply to Maxime Ripard

The new atomic state readout infrastructure can be hard to test because getting access to every firmware variation is hard, but also because most firmware setup will be pretty basic and won't test a wide range of features. Noticing whether it was successful or not is also not very convenient.

In order to make it easier, we can however provide some infrastructure to read out a new state every time a blocking commit is made, and compare the readout one with the committed one.

To do so, we introduce a new hook for every state, atomic_sro_compare_state, that takes two state instances and is supposed to return whether they are identical or not.

---
 drivers/gpu/drm/drm_atomic_helper.c        |  45 +++++
 drivers/gpu/drm/drm_atomic_state_readout.c | 309 ++++++++++++++++++++++++++++-
 drivers/gpu/drm/drm_bridge.c               |   3 +-
 include/drm/drm_atomic.h                   |  23 +++
 include/drm/drm_atomic_state_readout.h     |   3 +
 include/drm/drm_bridge.h                   |  22 ++
 include/drm/drm_connector.h                |  22 ++
 include/drm/drm_crtc.h                     |  22 ++
 include/drm/drm_plane.h                    |  22 ++
 9 files changed, 468 insertions(+), 3 deletions(-)
drivers/gpu/drm/drm_atomic_helper.c +45 -0
index ccfe3840d505..1ef87b753f8c 100644
@@ -30,10 +30,11 @@
 #include <linux/ktime.h>
 #include <linux/media-bus-format.h>
 
 #include <drm/drm_atomic.h>
 #include <drm/drm_atomic_helper.h>
+#include <drm/drm_atomic_state_readout.h>
 #include <drm/drm_atomic_uapi.h>
 #include <drm/drm_blend.h>
 #include <drm/drm_bridge.h>
 #include <drm/drm_colorop.h>
 #include <drm/drm_damage_helper.h>
@@ -2085,10 +2086,54 @@ static void commit_tail(struct drm_atomic_commit *state, bool nonblock)
 						 (unsigned long)commit_time_ms,
 						 new_self_refresh_mask);
 
 	drm_atomic_helper_commit_cleanup_done(state);
 
+	/*
+	 * drm_atomic_sro_readout_and_compare() will compare the @state new
+	 * states with whatever it will read from the hardware.
+	 *
+	 * Since drm_atomic_helper_swap_state() has already been called, this
+	 * means that new states have now been installed in their respective
+	 * object state pointer, and their access must be protected by their
+	 * respective locks.
+	 *
+	 * If we're doing a non-blocking commit however, the locks will be
+	 * dropped as soon as we return from drm_atomic_nonblocking_commit()
+	 * through drm_modeset_drop_locks(), while this function will be
+	 * scheduled to execute later on. This means that by the time we get
+	 * here, we probably have dropped the locks that protect the new states
+	 * and it's not safe to access them anymore.
+	 *
+	 * Thus, we can only safely call drm_atomic_sro_readout_and_compare() if
+	 * the commit is blocking, and we still hold all the locks.
+	 *
+	 * The readout is limited to the objects that have a state in
+	 * the commit, which are exactly the objects whose locks we
+	 * hold, so it can't race with a concurrent commit touching
+	 * the other objects.
+	 *
+	 * The other thing to consider is that it will take time to readout and
+	 * compare all the states, and we don't want to delay fence signalling
+	 * since some other part of the kernel or hardware might be waiting on
+	 * us. Fortunately, the fence is signalled through
+	 * &drm_crtc_commit.flip_done and drm_crtc_send_vblank_event() ->
+	 * drm_send_event_helper() with drm_pending_event.completion being set
+	 * to drm_crtc_commit.flip_done in drm_atomic_helper_setup_commit().
+	 *
+	 * We later wait for flip_done in
+	 * drm_atomic_helper_wait_for_flip_done(), which is typically part of
+	 * drm_mode_config_helper_funcs.atomic_commit_tail, or the equivalent
+	 * drm_atomic_helper_wait_for_vblank() which is part of
+	 * drm_atomic_helper_commit_tail().
+	 *
+	 * Either way, we're past that point here, so we wouldn't delay the
+	 * fence signalling.
+	 */
+	if (!nonblock)
+		drm_atomic_sro_readout_and_compare(state);
+
 	drm_atomic_commit_put(state);
 }
 
 static void commit_work(struct work_struct *work)
 {
drivers/gpu/drm/drm_atomic_state_readout.c +307 -2

354 lines not on this page · raw message

drivers/gpu/drm/drm_bridge.c +2 -1
index 868558a09f95..fe9b535d02bd 100644
@@ -796,11 +796,12 @@ int drm_bridge_attach(struct drm_encoder *encoder, struct drm_bridge *bridge,
 		if (ret < 0)
 			goto err_reset_bridge;
 	}
 
 	if (bridge->funcs &&
-	    bridge->funcs->atomic_sro_readout_state)
+	    bridge->funcs->atomic_sro_readout_state &&
+	    bridge->funcs->atomic_sro_compare_state)
 		ret = drm_atomic_private_obj_init(bridge->dev, &bridge->base,
 						  bridge->name,
 						  &drm_bridge_priv_state_funcs_with_sro);
 	else
 		ret = drm_atomic_private_obj_init(bridge->dev, &bridge->base,
include/drm/drm_atomic.h +23 -0
index f82e24fce7dc..a9ce086c97ac 100644
@@ -361,10 +361,33 @@ struct drm_private_state_funcs {
 	 * initialization sequence.
 	 */
 	void (*atomic_sro_install_state)(struct drm_private_obj *obj,
 					 struct drm_private_state *obj_state);
 
+	/**
+	 * @atomic_sro_compare_state:
+	 *
+	 * Compares two &struct drm_private_state instances and reports
+	 * any mismatches through @p.
+	 *
+	 * It is called after blocking commits to verify that the
+	 * committed state matches what can be read back from the
+	 * hardware. Drivers subclassing the state should implement this
+	 * to compare their driver-private fields.
+	 *
+	 * This hook is mandatory for drivers implementing SRO, but can
+	 * be left unassigned otherwise.
+	 *
+	 * RETURNS:
+	 *
+	 * True if the states are identical, false otherwise.
+	 */
+	bool (*atomic_sro_compare_state)(struct drm_private_obj *obj,
+					 struct drm_printer *p,
+					 struct drm_private_state *a,
+					 struct drm_private_state *b);
+
 	/**
 	 * @atomic_print_state:
 	 *
 	 * If driver subclasses &struct drm_private_state, it should implement
 	 * this optional hook for printing additional driver specific state.
include/drm/drm_atomic_state_readout.h +3 -0
index b62d66783f84..68f04583f8ad 100644
@@ -3,10 +3,11 @@
 #ifndef DRM_ATOMIC_SRO_H_
 #define DRM_ATOMIC_SRO_H_
 
 #include <linux/types.h>
 
+struct drm_atomic_commit;
 struct drm_connector;
 struct drm_connector_state;
 struct drm_crtc;
 struct drm_crtc_state;
 struct drm_device;
@@ -233,6 +234,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);
 
+bool drm_atomic_sro_readout_and_compare(struct drm_atomic_commit *commit);
+
 #endif /* DRM_ATOMIC_SRO_H_ */
include/drm/drm_bridge.h +22 -0
index 7e30877a81cc..d812e809ae81 100644
@@ -352,10 +352,32 @@ struct drm_bridge_funcs {
 	void (*atomic_sro_install_state)(struct drm_bridge *bridge,
 					 struct drm_bridge_state *bridge_state,
 					 struct drm_crtc_state *crtc_state,
 					 struct drm_connector_state *conn_state);
 
+	/**
+	 * @atomic_sro_compare_state:
+	 *
+	 * Compares two &struct drm_bridge_state instances and reports
+	 * any mismatches through @p.
+	 *
+	 * It is called after blocking commits to verify that the
+	 * committed state matches what can be read back from the
+	 * hardware.
+	 *
+	 * This hook is mandatory for drivers implementing SRO, but can
+	 * be left unassigned otherwise.
+	 *
+	 * RETURNS:
+	 *
+	 * True if the states are identical, false otherwise.
+	 */
+	bool (*atomic_sro_compare_state)(struct drm_bridge *bridge,
+					 struct drm_printer *p,
+					 struct drm_bridge_state *a,
+					 struct drm_bridge_state *b);
+
 	/**
 	 * @atomic_duplicate_state:
 	 *
 	 * Duplicate the current bridge state object (which is guaranteed to be
 	 * non-NULL).
include/drm/drm_connector.h +22 -0
index 3625cd1b66ed..495c9a806d6c 100644
@@ -1905,10 +1905,32 @@ struct drm_connector_funcs {
 	 * This callback is mandatory for atomic drivers.
 	 */
 	void (*atomic_destroy_state)(struct drm_connector *connector,
 				     struct drm_connector_state *state);
 
+	/**
+	 * @atomic_sro_compare_state:
+	 *
+	 * Compares two &struct drm_connector_state instances and
+	 * reports any mismatches through @p.
+	 *
+	 * It is called after blocking commits to verify that the
+	 * committed state matches what can be read back from the
+	 * hardware.
+	 *
+	 * This hook is mandatory for drivers implementing SRO, but can
+	 * be left unassigned otherwise.
+	 *
+	 * RETURNS:
+	 *
+	 * True if the states are identical, false otherwise.
+	 */
+	bool (*atomic_sro_compare_state)(struct drm_connector *connector,
+					 struct drm_printer *p,
+					 struct drm_connector_state *a,
+					 struct drm_connector_state *b);
+
 	/**
 	 * @atomic_set_property:
 	 *
 	 * Decode a driver-private property value and store the decoded value
 	 * into the passed-in state structure. Since the atomic core decodes all
include/drm/drm_crtc.h +22 -0
index 2def0b58a10d..5a94fdb0f7c0 100644
@@ -739,10 +739,32 @@ struct drm_crtc_funcs {
 	 * This callback is mandatory for atomic drivers.
 	 */
 	void (*atomic_destroy_state)(struct drm_crtc *crtc,
 				     struct drm_crtc_state *state);
 
+	/**
+	 * @atomic_sro_compare_state:
+	 *
+	 * Compares two &struct drm_crtc_state instances and reports
+	 * any mismatches through @p.
+	 *
+	 * It is called after blocking commits to verify that the
+	 * committed state matches what can be read back from the
+	 * hardware.
+	 *
+	 * This hook is mandatory for drivers implementing SRO, but can
+	 * be left unassigned otherwise.
+	 *
+	 * RETURNS:
+	 *
+	 * True if the states are identical, false otherwise.
+	 */
+	bool (*atomic_sro_compare_state)(struct drm_crtc *crtc,
+					 struct drm_printer *p,
+					 struct drm_crtc_state *a,
+					 struct drm_crtc_state *b);
+
 	/**
 	 * @atomic_set_property:
 	 *
 	 * Decode a driver-private property value and store the decoded value
 	 * into the passed-in state structure. Since the atomic core decodes all
include/drm/drm_plane.h +22 -0
index fe5f778cb636..d7af4b58ab31 100644
@@ -486,10 +486,32 @@ struct drm_plane_funcs {
 	 * This callback is mandatory for atomic drivers.
 	 */
 	void (*atomic_destroy_state)(struct drm_plane *plane,
 				     struct drm_plane_state *state);
 
+	/**
+	 * @atomic_sro_compare_state:
+	 *
+	 * Compares two &struct drm_plane_state instances and reports
+	 * any mismatches through @p.
+	 *
+	 * It is called after blocking commits to verify that the
+	 * committed state matches what can be read back from the
+	 * hardware.
+	 *
+	 * This hook is mandatory for drivers implementing SRO, but can
+	 * be left unassigned otherwise.
+	 *
+	 * RETURNS:
+	 *
+	 * True if the states are identical, false otherwise.
+	 */
+	bool (*atomic_sro_compare_state)(struct drm_plane *plane,
+					 struct drm_printer *p,
+					 struct drm_plane_state *a,
+					 struct drm_plane_state *b);
+
 	/**
 	 * @atomic_set_property:
 	 *
 	 * Decode a driver-private property value and store the decoded value
 	 * into the passed-in state structure. Since the atomic core decodes all
Signature · 1 line
2.56.1
#38 lore raw

Re: [PATCH v9 22/42] drm/atomic_helper: Compare actual and readout states once the commit is done

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [Medium] drm/bridge: drm_bridge_attach() disables readout if compare hook is unset - [Low] drm/atomic_sro…

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [Medium] drm/bridge: drm_bridge_attach() disables readout if compare hook is unset
- [Low] drm/atomic_sro: duplicated line in DOC: overview comment --

commit 00233166cd25376785bc177974713d5ccca22687
Author: Maxime Ripard <[email protected]>

drm/atomic_helper: Compare actual and readout states once the commit is done

This commit introduces atomic_sro_compare_state callbacks for KMS objects and adds drm_atomic_sro_readout_and_compare() to read back and compare hardware state against the committed state after blocking atomic commits.

Quoted 22 lines
drivers/gpu/drm/drm_atomic_state_readout.c +5 -2
index 3e50b0796cbec..4b6512ef1aae7 100644
@@ -26,7 +26,7 @@
  * firmware already configured the display, the first userspace
  * modeset can be skipped when the requested mode matches.
  *
- * The SRO lifecycle has two phases. The first phase is the readout
+ * The SRO lifecycle has three phases. The first phase is the readout
  * itself: at driver registration time, each KMS object (CRTCs, planes,
  * connectors, bridges, private objects) has its
  * atomic_sro_readout_state hook called to populate a
@@ -46,7 +46,17 @@
  * state active, such as power domains, clocks, or interrupts. This hook
  * cannot fail.
  *
- * Drivers integrate with SRO by implementing the readout hooks in their
+ * The third phase is the comparison. Because getting access to every
+ * firmware variation is hard, and most firmware setups will be fairly
+ * basic, the framework also provides a way to verify the readout
+ * implementation during normal operation. After every blocking commit,

drivers/gpu/drm/drm_atomic_state_readout.c line 62

Quoted 10 lines
drivers/gpu/drm/drm_atomic_state_readout.c +7 -0
+ * the framework reads out a fresh state from hardware and compares it
+ * to the committed state using the atomic_sro_compare_state hooks. Any
+ * mismatch is reported, which allows to catch readout bugs without
+ * needing specific firmware configurations.
+ *
+ * Drivers integrate with SRO by implementing the readout and compare
+ * hooks in their object funcs vtables and setting the
  * object funcs vtables and setting the
  * &drm_mode_config_funcs.atomic_sro_readout_state,
  * &drm_mode_config_helper_funcs.atomic_sro_install_state, and

[Severity: Low]
This isn't a bug, but "object funcs vtables and setting the" is duplicated in the overview comment.

[ ... ]

Quoted 5 lines
drivers/gpu/drm/drm_bridge.c +0 -0
index 868558a09f951..fe9b535d02bd5 100644

drivers/gpu/drm/drm_bridge.c int drm_bridge_attach(struct drm_encoder *encoder, struct drm_bridge *bridge, · line 805

Quoted 9 lines
drivers/gpu/drm/drm_bridge.c +2 -1
@@ -798,7 +798,8 @@ int drm_bridge_attach(struct drm_encoder *encoder, struct drm_bridge *bridge,
 	}
 
 	if (bridge->funcs &&
-	    bridge->funcs->atomic_sro_readout_state)
+	    bridge->funcs->atomic_sro_readout_state &&
+	    bridge->funcs->atomic_sro_compare_state)
 		ret = drm_atomic_private_obj_init(bridge->dev, &bridge->base,
 						  bridge->name,
 						  &drm_bridge_priv_state_funcs_with_sro);

[Severity: Medium]
Does requiring bridge->funcs->atomic_sro_compare_state here break bridge state readout when atomic_readout is set to DRM_ATOMIC_READOUT_SKIP_MISSING_COMPARE or DRM_ATOMIC_READOUT_SKIP_MISSING_READOUT?

If a bridge implements atomic_sro_readout_state without atomic_sro_compare_state, drm_bridge_attach() falls back to drm_bridge_priv_state_funcs, which leaves privobj->funcs->atomic_sro_readout_state and privobj->funcs->atomic_sro_install_state NULL. drm_atomic_sro_can_readout() then rejects the device under DRM_ATOMIC_READOUT_SKIP_MISSING_COMPARE, and drm_atomic_helper_sro_readout_bridge_state() skips the bridge's readout hook under DRM_ATOMIC_READOUT_SKIP_MISSING_READOUT.

Also, at this point in the series, drm_bridge_priv_state_funcs_with_sro does not yet set .atomic_sro_compare_state (wired in the later commit "drm/atomic-state-helper: Provide atomic_compare_state helpers"), so drm_atomic_sro_can_compare() returns false under DRM_ATOMIC_READOUT_ENABLED for any device with an attached bridge until that commit.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=22
author maintainer
#39 lore raw

PATCH v9 23/42 drm/atomic-state-helper: Provide comparison macros

In reply to Maxime Ripard

In order to make the implementation of atomic_sro_compare_state easier and reduce the boilerplate, introduce a set of STATE_CHECK_* macros to compare state field values depending on their type.

---
 drivers/gpu/drm/drm_atomic_state_readout_helper.c |  30 +++
 include/drm/drm_atomic_state_readout_helper.h     | 220 ++++++++++++++++++++++
 2 files changed, 250 insertions(+)
drivers/gpu/drm/drm_atomic_state_readout_helper.c +30 -0
index f290d2fe5496..80eb467baefc 100644
@@ -935,5 +935,35 @@ void drm_atomic_helper_sro_install_state(struct drm_atomic_sro_state *state)
 		state->private_objs[i].state = NULL;
 		state->private_objs[i].ptr = NULL;
 	}
 }
 EXPORT_SYMBOL(drm_atomic_helper_sro_install_state);
+
+/**
+ * drm_atomic_helper_print_state_mismatch - report a state comparison mismatch
+ * @p: printer to report through
+ * @name: human-readable name of the object (e.g. plane or CRTC name)
+ * @field: name of the mismatching field
+ * @format: printf-style format string describing the mismatch
+ *
+ * Helper used by atomic_sro_compare_state implementations and the
+ * STATE_CHECK_* macros to report a field-level mismatch between the
+ * committed state and the state read back from hardware.
+ */
+void __printf(4, 5)
+drm_atomic_helper_print_state_mismatch(struct drm_printer *p,
+				       const char *name,
+				       const char *field,
+				       const char *format, ...)
+{
+	struct va_format vaf;
+	va_list args;
+
+	va_start(args, format);
+	vaf.fmt = format;
+	vaf.va = &args;
+
+	drm_printf(p, "%s configuration mismatch in %s %pV\n", name, field, &vaf);
+
+	va_end(args);
+}
+EXPORT_SYMBOL(drm_atomic_helper_print_state_mismatch);
include/drm/drm_atomic_state_readout_helper.h +220 -0

239 lines not on this page · raw message

Signature · 1 line
2.56.1
author maintainer
#40 lore raw

PATCH v9 24/42 drm/atomic-state-helper: Provide atomic_compare_state helpers

In reply to Maxime Ripard

Now that we have introduced some new infrastructure to compare state instances, provide helpers for the default states.

---
 drivers/gpu/drm/drm_atomic_state_readout_helper.c | 391 ++++++++++++++++++++++
 drivers/gpu/drm/drm_bridge.c                      |  16 +
 include/drm/drm_atomic_state_readout_helper.h     |  28 ++
 include/drm/drm_bridge.h                          |   5 +-
 include/drm/drm_connector.h                       |   5 +-
 include/drm/drm_crtc.h                            |   5 +-
 include/drm/drm_plane.h                           |   5 +-
 7 files changed, 451 insertions(+), 4 deletions(-)
drivers/gpu/drm/drm_atomic_state_readout_helper.c +391 -0
index 80eb467baefc..d39d8e40f894 100644
@@ -6,10 +6,11 @@
 #include <drm/drm_bridge.h>
 #include <drm/drm_colorop.h>
 #include <drm/drm_connector.h>
 #include <drm/drm_crtc.h>
 #include <drm/drm_encoder.h>
+#include <drm/drm_framebuffer.h>
 #include <drm/drm_modeset_helper_vtables.h>
 #include <drm/drm_plane.h>
 #include <drm/drm_print.h>
 #include <drm/drm_vblank.h>
 
@@ -936,10 +937,400 @@ void drm_atomic_helper_sro_install_state(struct drm_atomic_sro_state *state)
 		state->private_objs[i].ptr = NULL;
 	}
 }
 EXPORT_SYMBOL(drm_atomic_helper_sro_install_state);
 
+static bool drm_atomic_helper_fb_compare(struct drm_printer *p,
+					 struct drm_framebuffer *expected,
+					 struct drm_framebuffer *actual)
+{
+	unsigned int i;
+	bool ret = true;
+
+	STATE_CHECK_FORMAT_INFO(ret, p, "framebuffer", expected, actual, format);
+
+	/*
+	 * A framebuffer reconstructed from the hardware only covers the
+	 * part of the buffer that is scanned out, and its per-plane
+	 * offsets are baked into the DMA base address: the buffer
+	 * geometry and the offsets can't be read back and aren't
+	 * compared. The scanned out geometry is still checked through
+	 * the source and destination rectangles of the plane state.
+	 */
+	for (i = 0; i < expected->format->num_planes; i++)
+		STATE_CHECK_U32(ret, p, "framebuffer", expected, actual, pitches[i]);
+
+	STATE_CHECK_U64(ret, p, "framebuffer", expected, actual, modifier);
+	STATE_CHECK_S32_X(ret, p, "framebuffer", expected, actual, flags);
+
+	return ret;
+}
+
+/**
+ * drm_atomic_helper_plane_compare_state - default &drm_plane_funcs.atomic_sro_compare_state hook
+ * @plane: the plane being compared
+ * @p: printer for reporting mismatches
+ * @expected: the committed &struct drm_plane_state
+ * @actual: the &struct drm_plane_state read out from hardware
+ *
+ * Compares the base &struct drm_plane_state fields of @actual to
+ * @expected and reports any mismatches through @p. Drivers subclassing
+ * the plane state should call this first, then compare their own fields.
+ *
+ * RETURNS:
+ *
+ * True if the states are identical, false otherwise.
+ */
+bool drm_atomic_helper_plane_compare_state(struct drm_plane *plane,
+					   struct drm_printer *p,
+					   struct drm_plane_state *expected,
+					   struct drm_plane_state *actual)
+{
+	bool ret = true;
+
+	STATE_CHECK_PTR(ret, p, plane->name, expected, actual, plane);
+	STATE_CHECK_PTR(ret, p, plane->name, expected, actual, crtc);
+
+	/* Both disabled, geometry is meaningless. */
+	if (!expected->fb && !actual->fb)
+		return ret;
+
+	/* One enabled and the other disabled, that's a mismatch. */
+	if (!expected->fb || !actual->fb) {
+		drm_atomic_helper_print_state_mismatch(p,
+						       plane->name,
+						       "fb",
+						       "expected framebuffer is %s, got %s",
+						       expected->fb ? "non-NULL" : "NULL",
+						       actual->fb ? "non-NULL" : "NULL");
+		return false;
+	}
+
+	/* Both enabled, compare framebuffer and geometry. */
+	if (!drm_atomic_helper_fb_compare(p, expected->fb, actual->fb))
+		ret = false;
+
+	STATE_CHECK_S32(ret, p, plane->name, expected, actual, crtc_x);
+	STATE_CHECK_S32(ret, p, plane->name, expected, actual, crtc_y);
+	STATE_CHECK_U32(ret, p, plane->name, expected, actual, crtc_w);
+	STATE_CHECK_U32(ret, p, plane->name, expected, actual, crtc_h);
+
+	/*
+	 * NOTE: We can't check src_x and src_y: the source offsets are
+	 * baked into the DMA base address and cannot be read back.
+	 */
+	STATE_CHECK_U32_16_16(ret, p, plane->name, expected, actual, src_w);
+	STATE_CHECK_U32_16_16(ret, p, plane->name, expected, actual, src_h);
+	STATE_CHECK_S32(ret, p, plane->name, expected, actual, hotspot_x);
+	STATE_CHECK_S32(ret, p, plane->name, expected, actual, hotspot_y);
+	STATE_CHECK_U16(ret, p, plane->name, expected, actual, alpha);
+	STATE_CHECK_U16(ret, p, plane->name, expected, actual, pixel_blend_mode);
+	STATE_CHECK_U32(ret, p, plane->name, expected, actual, rotation);
+
+	/*
+	 * When the zpos are normalized, the zpos property is a
+	 * userspace request turned into normalized_zpos during
+	 * atomic_check, and the hardware only stores the normalized
+	 * result. Otherwise, the driver programs the zpos property
+	 * directly and normalized_zpos isn't updated.
+	 */
+	if (plane->dev->mode_config.normalize_zpos)
+		STATE_CHECK_U32(ret, p, plane->name, expected, actual, normalized_zpos);
+	else
+		STATE_CHECK_U32(ret, p, plane->name, expected, actual, zpos);
+
+	STATE_CHECK_U32(ret, p, plane->name, expected, actual, color_encoding);
+	STATE_CHECK_U32(ret, p, plane->name, expected, actual, color_range);
+
+	/*
+	 * NOTE: We can't check fb_damage_clips and ignore_damage_clips
+	 * because they are transient, per-commit software fields that
+	 * are reset when the state is duplicated and never stored in
+	 * the hardware.
+	 */
+
+	STATE_CHECK_PTR(ret, p, plane->name, expected, actual, color_pipeline);
+	STATE_CHECK_S32(ret, p, plane->name, expected, actual, src.x1);
+	STATE_CHECK_S32(ret, p, plane->name, expected, actual, src.x2);
+	STATE_CHECK_S32(ret, p, plane->name, expected, actual, src.y1);
+	STATE_CHECK_S32(ret, p, plane->name, expected, actual, src.y2);
+	STATE_CHECK_S32(ret, p, plane->name, expected, actual, dst.x1);
+	STATE_CHECK_S32(ret, p, plane->name, expected, actual, dst.x2);
+	STATE_CHECK_S32(ret, p, plane->name, expected, actual, dst.y1);
+	STATE_CHECK_S32(ret, p, plane->name, expected, actual, dst.y2);
+	STATE_CHECK_BOOL(ret, p, plane->name, expected, actual, visible);
+	STATE_CHECK_U32(ret, p, plane->name, expected, actual, scaling_filter);
+
+	return ret;
+}
+EXPORT_SYMBOL(drm_atomic_helper_plane_compare_state);
+
+/**
+ * drm_atomic_helper_crtc_compare_state - default &drm_crtc_funcs.atomic_sro_compare_state hook
+ * @crtc: the CRTC being compared
+ * @p: printer for reporting mismatches
+ * @expected: the committed &struct drm_crtc_state
+ * @actual: the &struct drm_crtc_state read out from hardware
+ *
+ * Compares the base &struct drm_crtc_state fields of @actual to
+ * @expected and reports any mismatches through @p. Drivers subclassing
+ * the CRTC state should call this first, then compare their own fields.
+ *
+ * RETURNS:
+ *
+ * True if the states are identical, false otherwise.
+ */
+bool drm_atomic_helper_crtc_compare_state(struct drm_crtc *crtc,
+					  struct drm_printer *p,
+					  struct drm_crtc_state *expected,
+					  struct drm_crtc_state *actual)
+{
+	bool ret = true;
+
+	STATE_CHECK_PTR(ret, p, crtc->name, expected, actual, crtc);
+	STATE_CHECK_BOOL(ret, p, crtc->name, expected, actual, active);
+
+	/* Both inactive, mode and properties are meaningless. */
+	if (!expected->active && !actual->active)
+		return ret;
+
+	/* One active and the other inactive, that's a mismatch. */
+	if (!expected->active || !actual->active)
+		return false;
+
+	/*
+	 * Both active, compare everything. enable is only checked at
+	 * this point: a DPMS off commit keeps enable set while the
+	 * hardware, and thus the readout, reports an inactive CRTC.
+	 */
+	STATE_CHECK_BOOL(ret, p, crtc->name, expected, actual, enable);
+
+	/* Both active, compare everything. */
+	STATE_CHECK_BOOL(ret, p, crtc->name, expected, actual, no_vblank);
+	STATE_CHECK_U32(ret, p, crtc->name, expected, actual, plane_mask);
+	STATE_CHECK_U32(ret, p, crtc->name, expected, actual, connector_mask);
+	STATE_CHECK_U32(ret, p, crtc->name, expected, actual, encoder_mask);
+
+	STATE_CHECK_DISPLAY_MODE(ret, p, crtc->name, expected, actual, mode);
+	STATE_CHECK_DISPLAY_MODE(ret, p, crtc->name, expected, actual, adjusted_mode);
+
+	/*
+	 * NOTE: We can't check mode_blob: its payload also carries the
+	 * mode type and name, and neither can be reconstructed from the
+	 * hardware. The modes themselves are covered by the checks
+	 * above.
+	 */
+	STATE_CHECK_PROPERTY_BLOB(ret, p, crtc->name, expected, actual, degamma_lut);
+	STATE_CHECK_PROPERTY_BLOB(ret, p, crtc->name, expected, actual, ctm);
+	STATE_CHECK_PROPERTY_BLOB(ret, p, crtc->name, expected, actual, gamma_lut);
+	STATE_CHECK_BOOL(ret, p, crtc->name, expected, actual, vrr_enabled);
+	STATE_CHECK_BOOL(ret, p, crtc->name, expected, actual, self_refresh_active);
+	STATE_CHECK_U32(ret, p, crtc->name, expected, actual, scaling_filter);
+	STATE_CHECK_U64(ret, p, crtc->name, expected, actual, background_color);
+
+	return ret;
+}
+EXPORT_SYMBOL(drm_atomic_helper_crtc_compare_state);
+
+/**
+ * drm_atomic_helper_connector_compare_state - default &drm_connector_funcs.atomic_sro_compare_state hook
+ * @conn: the connector being compared
+ * @p: printer for reporting mismatches
+ * @expected: the committed &struct drm_connector_state
+ * @actual: the &struct drm_connector_state read out from hardware
+ *
+ * Compares the base &struct drm_connector_state fields of @actual to
+ * @expected and reports any mismatches through @p. Drivers subclassing
+ * the connector state should call this first, then compare their own
+ * fields.
+ *
+ * RETURNS:
+ *
+ * True if the states are identical, false otherwise.
+ */
+bool drm_atomic_helper_connector_compare_state(struct drm_connector *conn,
+					       struct drm_printer *p,
+					       struct drm_connector_state *expected,
+					       struct drm_connector_state *actual)
+{
+	bool ret = true;
+
+	STATE_CHECK_PTR(ret, p, conn->name, expected, actual, connector);
+
+	/*
+	 * Both unbound, the routing and the negotiated properties are
+	 * meaningless: a disabled connector keeps the values it was
+	 * last enabled with, while a state read out from a disabled
+	 * pipeline is pristine.
+	 */
+	if (!expected->crtc && !actual->crtc)
+		return ret;
+
+	STATE_CHECK_PTR(ret, p, conn->name, expected, actual, crtc);
+
+	/*
+	 * One bound and the other unbound, that's a mismatch, and the
+	 * negotiated properties of the unbound one are meaningless.
+	 */
+	if (!expected->crtc || !actual->crtc)
+		return false;
+
+	STATE_CHECK_PTR(ret, p, conn->name, expected, actual, best_encoder);
+	STATE_CHECK_U32(ret, p, conn->name, expected, actual, link_status);
+
+	STATE_CHECK_U32(ret, p, conn->name, expected, actual, tv.select_subconnector);
+	STATE_CHECK_U32(ret, p, conn->name, expected, actual, tv.subconnector);
+	STATE_CHECK_U32(ret, p, conn->name, expected, actual, tv.margins.left);
+	STATE_CHECK_U32(ret, p, conn->name, expected, actual, tv.margins.right);
+	STATE_CHECK_U32(ret, p, conn->name, expected, actual, tv.margins.top);
+	STATE_CHECK_U32(ret, p, conn->name, expected, actual, tv.margins.bottom);
+	STATE_CHECK_U32(ret, p, conn->name, expected, actual, tv.legacy_mode);
+	STATE_CHECK_U32(ret, p, conn->name, expected, actual, tv.mode);
+	STATE_CHECK_U32(ret, p, conn->name, expected, actual, tv.brightness);
+	STATE_CHECK_U32(ret, p, conn->name, expected, actual, tv.contrast);
+	STATE_CHECK_U32(ret, p, conn->name, expected, actual, tv.flicker_reduction);
+	STATE_CHECK_U32(ret, p, conn->name, expected, actual, tv.overscan);
+	STATE_CHECK_U32(ret, p, conn->name, expected, actual, tv.saturation);
+	STATE_CHECK_U32(ret, p, conn->name, expected, actual, tv.hue);
+
+	STATE_CHECK_BOOL(ret, p, conn->name, expected, actual, self_refresh_aware);
+	STATE_CHECK_U32(ret, p, conn->name, expected, actual, picture_aspect_ratio);
+	STATE_CHECK_U32(ret, p, conn->name, expected, actual, content_type);
+	STATE_CHECK_U32(ret, p, conn->name, expected, actual, hdcp_content_type);
+	STATE_CHECK_U32(ret, p, conn->name, expected, actual, scaling_mode);
+	STATE_CHECK_U32(ret, p, conn->name, expected, actual, content_protection);
+	STATE_CHECK_U32(ret, p, conn->name, expected, actual, colorspace);
+
+	/*
+	 * NOTE: We can't check max_bpc, max_requested_bpc,
+	 * color_format or hdmi.broadcast_rgb because they are
+	 * userspace requests negotiated during atomic_check into
+	 * output_bpc, output_format and hdmi.is_limited_range. The
+	 * hardware only stores the negotiated result.
+	 */
+
+	STATE_CHECK_U32(ret, p, conn->name, expected, actual, privacy_screen_sw_state);
+
+	/*
+	 * NOTE: We can't check hdr_output_metadata: it's a userspace
+	 * request, and the hardware only stores the resulting HDR
+	 * infoframe, compared below.
+	 */
+
+	/*
+	 * NOTE: The infoframe payloads are only compared when the
+	 * infoframe is enabled in both states: the disabled ones keep
+	 * stale data from earlier commits around in the expected
+	 * state, while the readout state is pristine.
+	 */
+	STATE_CHECK_BOOL(ret, p, conn->name, expected, actual, hdmi.infoframes.avi.set);
+	if (expected->hdmi.infoframes.avi.set && actual->hdmi.infoframes.avi.set)
+		STATE_CHECK_INFOFRAME(ret, p, conn->name, expected, actual, hdmi.infoframes.avi.data);
+	STATE_CHECK_BOOL(ret, p, conn->name, expected, actual, hdmi.infoframes.hdr_drm.set);
+	if (expected->hdmi.infoframes.hdr_drm.set && actual->hdmi.infoframes.hdr_drm.set)
+		STATE_CHECK_INFOFRAME(ret, p, conn->name, expected, actual, hdmi.infoframes.hdr_drm.data);
+	STATE_CHECK_BOOL(ret, p, conn->name, expected, actual, hdmi.infoframes.spd.set);
+	if (expected->hdmi.infoframes.spd.set && actual->hdmi.infoframes.spd.set)
+		STATE_CHECK_INFOFRAME(ret, p, conn->name, expected, actual, hdmi.infoframes.spd.data);
+	STATE_CHECK_BOOL(ret, p, conn->name, expected, actual, hdmi.infoframes.hdmi.set);
+	if (expected->hdmi.infoframes.hdmi.set && actual->hdmi.infoframes.hdmi.set)
+		STATE_CHECK_INFOFRAME(ret, p, conn->name, expected, actual, hdmi.infoframes.hdmi.data);
+
+	STATE_CHECK_BOOL(ret, p, conn->name, expected, actual, hdmi.is_limited_range);
+	STATE_CHECK_U32(ret, p, conn->name, expected, actual, hdmi.output_bpc);
+	STATE_CHECK_U32(ret, p, conn->name, expected, actual, hdmi.output_format);
+	STATE_CHECK_U64(ret, p, conn->name, expected, actual, hdmi.tmds_char_rate);
+
+	/*
+	 * hdmi.scrambler_needed is derived from hdmi.tmds_char_rate, so
+	 * comparing tmds_char_rate already covers it.
+	 */
+
+	return ret;
+}
+EXPORT_SYMBOL(drm_atomic_helper_connector_compare_state);
+
+/**
+ * drm_atomic_helper_bridge_compare_state - default &drm_bridge_funcs.atomic_sro_compare_state hook
+ * @bridge: the bridge being compared
+ * @p: printer for reporting mismatches
+ * @expected: the committed &struct drm_bridge_state
+ * @actual: the &struct drm_bridge_state read out from hardware
+ *
+ * Compares the base &struct drm_bridge_state fields of @actual to
+ * @expected and reports any mismatches through @p. Drivers subclassing
+ * the bridge state should call this first, then compare their own
+ * fields.
+ *
+ * The compared fields are the format and the flags of both the input
+ * and the output &struct drm_bus_cfg. @expected holds what
+ * drm_atomic_bridge_chain_check() computed, so the
+ * &drm_bridge_funcs.atomic_sro_readout_state implementation that
+ * filled @actual is expected to reproduce those values, which depend
+ * on the order in which each of them is determined.
+ *
+ * During a regular atomic check, the bus formats are negotiated from
+ * the last bridge to the first one, and the bus flags are propagated
+ * in the same direction: the output flags of a bridge are those of the
+ * input of the next element in the chain. During the readout, the
+ * bridges are however visited from the first to the last one, so the
+ * state of the previous bridge is available but the state of the next
+ * one is not. A readout implementation is thus expected to:
+ *
+ * - Report the input and output formats the hardware is configured
+ *   with, or the ones the format negotiation would have selected if
+ *   the hardware doesn't store them. A bridge that doesn't implement
+ *   &drm_bridge_funcs.atomic_get_input_bus_fmts reports
+ *   MEDIA_BUS_FMT_FIXED as its input format.
+ *
+ * - Report the input flags the hardware is configured with. If it
+ *   doesn't store them, they can be taken from the output flags of the
+ *   previous bridge if it was read out, from the CRTC state for the
+ *   first bridge, or from the same source as
+ *   &drm_bridge_timings.input_bus_flags if the bridge sets them
+ *   statically in &drm_bridge_funcs.atomic_check.
+ *
+ * - Report the output flags the hardware produces. They can't be
+ *   copied from the next bridge, which hasn't been read out yet. They
+ *   should be read from the hardware when it stores them. The last
+ *   bridge of a chain can use &drm_display_info.bus_flags, and a
+ *   bridge forwarding its signals unmodified can use its own input
+ *   flags, which is what the default flag propagation of the atomic
+ *   check does.
+ *
+ * - Apply the same adjustments its &drm_bridge_funcs.atomic_check
+ *   implementation does to the flags, since @expected includes them.
+ *
+ * Since the output flags are compared too, an output flag a bridge
+ * can't actually read, or guesses incorrectly, shows up as a mismatch.
+ * Likewise, hardware that can only store a flag as one of two values
+ * can't report that a flag was left unset in @expected. Drivers
+ * hitting that limitation can provide their own
+ * &drm_bridge_funcs.atomic_sro_compare_state implementation that
+ * ignores the flags the hardware can't report.
+ *
+ * RETURNS:
+ *
+ * True if the states are identical, false otherwise.
+ */
+bool drm_atomic_helper_bridge_compare_state(struct drm_bridge *bridge,
+					    struct drm_printer *p,
+					    struct drm_bridge_state *expected,
+					    struct drm_bridge_state *actual)
+{
+	bool ret = true;
+
+	STATE_CHECK_PTR(ret, p, bridge->name, expected, actual, bridge);
+	STATE_CHECK_U32_X(ret, p, bridge->name, expected, actual, input_bus_cfg.format);
+	STATE_CHECK_U32_X(ret, p, bridge->name, expected, actual, input_bus_cfg.flags);
+	STATE_CHECK_U32_X(ret, p, bridge->name, expected, actual, output_bus_cfg.format);
+	STATE_CHECK_U32_X(ret, p, bridge->name, expected, actual, output_bus_cfg.flags);
+
+	return ret;
+}
+EXPORT_SYMBOL(drm_atomic_helper_bridge_compare_state);
+
 /**
  * drm_atomic_helper_print_state_mismatch - report a state comparison mismatch
  * @p: printer to report through
  * @name: human-readable name of the object (e.g. plane or CRTC name)
  * @field: name of the mismatching field
drivers/gpu/drm/drm_bridge.c +16 -0
index fe9b535d02bd..0d0c39cdc775 100644
@@ -635,10 +635,25 @@ drm_bridge_atomic_readout_priv_state(struct drm_private_obj *obj,
 	}
 
 	return 0;
 }
 
+static bool drm_bridge_atomic_compare_priv_state(struct drm_private_obj *obj,
+						 struct drm_printer *p,
+						 struct drm_private_state *a,
+						 struct drm_private_state *b)
+{
+	struct drm_bridge *bridge = drm_priv_to_bridge(obj);
+	struct drm_bridge_state *state_a = drm_priv_to_bridge_state(a);
+	struct drm_bridge_state *state_b = drm_priv_to_bridge_state(b);
+
+	if (bridge->funcs->atomic_sro_compare_state)
+		return bridge->funcs->atomic_sro_compare_state(bridge, p, state_a, state_b);
+	else
+		return false;
+}
+
 static void
 drm_bridge_atomic_install_priv_state(struct drm_private_obj *obj,
 				     struct drm_private_state *priv_state)
 {
 	struct drm_bridge *bridge = drm_priv_to_bridge(obj);
@@ -699,10 +714,11 @@ static const struct drm_private_state_funcs drm_bridge_priv_state_funcs = {
 	.atomic_destroy_state = drm_bridge_atomic_destroy_priv_state,
 	.atomic_print_state = drm_bridge_atomic_print_priv_state,
 };
 
 static const struct drm_private_state_funcs drm_bridge_priv_state_funcs_with_sro = {
+	.atomic_sro_compare_state = drm_bridge_atomic_compare_priv_state,
 	.atomic_sro_readout_state = drm_bridge_atomic_readout_priv_state,
 	.atomic_sro_install_state = drm_bridge_atomic_install_priv_state,
 	.atomic_create_state = drm_bridge_atomic_create_priv_state,
 	.atomic_duplicate_state = drm_bridge_atomic_duplicate_priv_state,
 	.atomic_destroy_state = drm_bridge_atomic_destroy_priv_state,
include/drm/drm_atomic_state_readout_helper.h +28 -0
index 603fe13a732e..6125ea668fa8 100644
@@ -5,11 +5,19 @@
 
 #include <linux/string_choices.h>
 
 struct drm_atomic_commit;
 struct drm_atomic_sro_state;
+struct drm_bridge;
+struct drm_bridge_state;
+struct drm_connector;
+struct drm_connector_state;
+struct drm_crtc;
+struct drm_crtc_state;
 struct drm_device;
+struct drm_plane;
+struct drm_plane_state;
 struct drm_printer;
 
 int
 drm_atomic_helper_sro_readout_planes_state(struct drm_device *dev,
 					   struct drm_atomic_sro_state *state,
@@ -34,10 +42,30 @@ struct drm_atomic_sro_state *
 drm_atomic_helper_sro_build_state(struct drm_device *dev,
 				  struct drm_atomic_commit *commit);
 int drm_atomic_helper_sro_readout_state(struct drm_device *dev);
 void drm_atomic_helper_sro_install_state(struct drm_atomic_sro_state *state);
 
+bool drm_atomic_helper_connector_compare_state(struct drm_connector *connector,
+					       struct drm_printer *p,
+					       struct drm_connector_state *expected,
+					       struct drm_connector_state *actual);
+
+bool drm_atomic_helper_crtc_compare_state(struct drm_crtc *crtc,
+					  struct drm_printer *p,
+					  struct drm_crtc_state *expected,
+					  struct drm_crtc_state *actual);
+
+bool drm_atomic_helper_plane_compare_state(struct drm_plane *plane,
+					   struct drm_printer *p,
+					   struct drm_plane_state *expected,
+					   struct drm_plane_state *actual);
+
+bool drm_atomic_helper_bridge_compare_state(struct drm_bridge *bridge,
+					    struct drm_printer *p,
+					    struct drm_bridge_state *expected,
+					    struct drm_bridge_state *actual);
+
 void __printf(4, 5)
 drm_atomic_helper_print_state_mismatch(struct drm_printer *p,
 				       const char *name,
 				       const char *field,
 				       const char *format, ...);
include/drm/drm_bridge.h +4 -1
index d812e809ae81..a74407ee5a9c 100644
@@ -360,11 +360,14 @@ struct drm_bridge_funcs {
 	 * Compares two &struct drm_bridge_state instances and reports
 	 * any mismatches through @p.
 	 *
 	 * It is called after blocking commits to verify that the
 	 * committed state matches what can be read back from the
-	 * hardware.
+	 * hardware. Drivers subclassing the state should implement this
+	 * to compare their driver-private fields, calling
+	 * drm_atomic_helper_bridge_compare_state() first for the base
+	 * state fields.
 	 *
 	 * This hook is mandatory for drivers implementing SRO, but can
 	 * be left unassigned otherwise.
 	 *
 	 * RETURNS:
include/drm/drm_connector.h +4 -1
index 495c9a806d6c..dd00659c5c4d 100644
@@ -1913,11 +1913,14 @@ struct drm_connector_funcs {
 	 * Compares two &struct drm_connector_state instances and
 	 * reports any mismatches through @p.
 	 *
 	 * It is called after blocking commits to verify that the
 	 * committed state matches what can be read back from the
-	 * hardware.
+	 * hardware. Drivers subclassing the state should implement this
+	 * to compare their driver-private fields, calling
+	 * drm_atomic_helper_connector_compare_state() first for the
+	 * base state fields.
 	 *
 	 * This hook is mandatory for drivers implementing SRO, but can
 	 * be left unassigned otherwise.
 	 *
 	 * RETURNS:
include/drm/drm_crtc.h +4 -1
index 5a94fdb0f7c0..4f1a99626fca 100644
@@ -747,11 +747,14 @@ struct drm_crtc_funcs {
 	 * Compares two &struct drm_crtc_state instances and reports
 	 * any mismatches through @p.
 	 *
 	 * It is called after blocking commits to verify that the
 	 * committed state matches what can be read back from the
-	 * hardware.
+	 * hardware. Drivers subclassing the state should implement this
+	 * to compare their driver-private fields, calling
+	 * drm_atomic_helper_crtc_compare_state() first for the base
+	 * state fields.
 	 *
 	 * This hook is mandatory for drivers implementing SRO, but can
 	 * be left unassigned otherwise.
 	 *
 	 * RETURNS:
include/drm/drm_plane.h +4 -1
index d7af4b58ab31..f0c44afac016 100644
@@ -494,11 +494,14 @@ struct drm_plane_funcs {
 	 * Compares two &struct drm_plane_state instances and reports
 	 * any mismatches through @p.
 	 *
 	 * It is called after blocking commits to verify that the
 	 * committed state matches what can be read back from the
-	 * hardware.
+	 * hardware. Drivers subclassing the state should implement this
+	 * to compare their driver-private fields, calling
+	 * drm_atomic_helper_plane_compare_state() first for the base
+	 * state fields.
 	 *
 	 * This hook is mandatory for drivers implementing SRO, but can
 	 * be left unassigned otherwise.
 	 *
 	 * RETURNS:
Signature · 1 line
2.56.1
#41 lore raw

Re: [PATCH v9 24/42] drm/atomic-state-helper: Provide atomic_compare_state helpers

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] drm/atomic-state-helper: false mismatch on cropped plane src rect in compare --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] drm/atomic-state-helper: false mismatch on cropped plane src rect in compare
--

commit 2ca1ed51cdf19bd9781aa9d57f8d3b278be9993f
Author: Maxime Ripard <[email protected]>

drm/atomic-state-helper: Provide atomic_compare_state helpers

This commit adds default atomic state comparison helpers for planes, CRTCs, connectors, and bridges to support post-commit hardware state readout verification.

Quoted 4 lines
drivers/gpu/drm/drm_atomic_state_readout_helper.c +0 -0
index 80eb467baefcf..d39d8e40f8947 100644

[ ... ]

Quoted 1 line
drivers/gpu/drm/drm_atomic_state_readout_helper.c +0 -0
@@ -938,6 +939,396 @@ void drm_atomic_helper_sro_install_state(struct drm_atomic_sro_state *state)

[ ... ]

Quoted 5 lines
drivers/gpu/drm/drm_atomic_state_readout_helper.c +5 -0
+bool drm_atomic_helper_plane_compare_state(struct drm_plane *plane,
+					   struct drm_printer *p,
+					   struct drm_plane_state *expected,
+					   struct drm_plane_state *actual)
+{

[ ... ]

Quoted 6 lines
drivers/gpu/drm/drm_atomic_state_readout_helper.c +6 -0
+	/*
+	 * NOTE: We can't check src_x and src_y: the source offsets are
+	 * baked into the DMA base address and cannot be read back.
+	 */
+	STATE_CHECK_U32_16_16(ret, p, plane->name, expected, actual, src_w);
+	STATE_CHECK_U32_16_16(ret, p, plane->name, expected, actual, src_h);

[ ... ]

drivers/gpu/drm/drm_atomic_state_readout_helper.c void drm_atomic_helper_sro_install_state(struct drm_atomic_sro_state *state) · line 1055

Quoted 5 lines
drivers/gpu/drm/drm_atomic_state_readout_helper.c +5 -0
+	STATE_CHECK_PTR(ret, p, plane->name, expected, actual, color_pipeline);
+	STATE_CHECK_S32(ret, p, plane->name, expected, actual, src.x1);
+	STATE_CHECK_S32(ret, p, plane->name, expected, actual, src.x2);
+	STATE_CHECK_S32(ret, p, plane->name, expected, actual, src.y1);
+	STATE_CHECK_S32(ret, p, plane->name, expected, actual, src.y2);

[Severity: Medium]
Won't comparing src.{x1,x2,y1,y2} always report a false mismatch whenever a plane is committed with a non-zero src_x or src_y?

drm_atomic_helper_check_plane_state() initializes expected->src from drm_plane_state_src() with x1 = src_x, y1 = src_y, x2 = src_x + src_w, and y2 = src_y + src_h before clipping. Since src_x and src_y are baked into the DMA base address and cannot be read back, actual->src read out from hardware will be rooted at (0, 0) instead of (src_x, src_y). In addition, src.{x1,x2,y1,y2} are 16.16 fixed-point values, so STATE_CHECK_S32 prints them as raw integers on mismatch.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=24
author maintainer
#42 lore raw

PATCH v9 25/42 drm/atomic-helper: Document State Read-Out as a state initialization approach

In reply to Maxime Ripard

The "atomic state reset and initialization" DOC section describes the problem of needing correct atomic state at driver load and after suspend, but only mentions the reset approach and a vague reference to i915's read-out as an alternative.

Now that the State Read-Out (SRO) infrastructure provides a generic solution, update the documentation to describe both approaches and cross-reference the SRO section. Also mention drm_mode_config_create_initial_state() which combines both.

---
 drivers/gpu/drm/drm_atomic_state_helper.c | 20 ++++++++++++++------
 1 file changed, 14 insertions(+), 6 deletions(-)
drivers/gpu/drm/drm_atomic_state_helper.c +14 -6
index 267f44330629..c5bf70e6ad53 100644
@@ -45,17 +45,25 @@
  * DOC: atomic state reset and initialization
  *
  * Both the drm core and the atomic helpers assume that there is always the full
  * and correct atomic software state for all connectors, CRTCs and planes
  * available. Which is a bit a problem on driver load and also after system
- * suspend. One way to solve this is to have a hardware state read-out
- * infrastructure which reconstructs the full software state (e.g. the i915
- * driver).
+ * suspend. There are two ways to solve this:
  *
- * The simpler solution is to just reset the software state to everything off,
- * which is easiest to do by calling drm_mode_config_reset(). To facilitate this
- * the atomic helpers provide default reset implementations for all hooks.
+ * The first is to use the :ref:`State Read-Out (SRO) <drm_atomic_sro>`
+ * infrastructure to reconstruct the full software state from hardware
+ * registers. This enables flicker-free boot when the firmware already
+ * configured the display.
+ *
+ * The simpler alternative is to just reset the software state to everything
+ * off, which is easiest to do by calling drm_mode_config_reset(). To
+ * facilitate this the atomic helpers provide default reset implementations for
+ * all hooks.
+ *
+ * drm_mode_config_create_initial_state() combines both approaches: it tries
+ * SRO first, and falls back to a hardware reset followed by
+ * drm_mode_config_create_state().
  *
  * On the upside the precise state tracking of atomic simplifies system suspend
  * and resume a lot. For drivers using drm_mode_config_reset() a complete recipe
  * is implemented in drm_atomic_helper_suspend() and drm_atomic_helper_resume().
  * For other drivers the building blocks are split out, see the documentation
Signature · 1 line
2.56.1
author maintainer
#43 lore raw

PATCH v9 26/42 drm/tests: kunit: Add a drm_printer helper

In reply to Maxime Ripard

Some DRM helpers report the state of an object through a drm_printer, most notably to report which fields of two states differ when comparing them.

Tests exercising such code paths need to pass a drm_printer as well, and would rather have its output show up in the KTAP log of the test than anywhere else, so that a failure comes with the report that caused it.

Add drm_kunit_helper_printer() that returns a drm_printer whose messages are logged through kunit_log() at the LOGLEVEL_* level requested by the caller.

---
 drivers/gpu/drm/tests/drm_kunit_helpers.c | 78 +++++++++++++++++++++++++++++++
 include/drm/drm_kunit_helpers.h           |  4 ++
 2 files changed, 82 insertions(+)
drivers/gpu/drm/tests/drm_kunit_helpers.c +78 -0
index 63d024946500..5fdffbf78245 100644
@@ -6,20 +6,98 @@
 #include <drm/drm_drv.h>
 #include <drm/drm_edid.h>
 #include <drm/drm_fourcc.h>
 #include <drm/drm_kunit_helpers.h>
 #include <drm/drm_managed.h>
+#include <drm/drm_print.h>
 
 #include <kunit/device.h>
 #include <kunit/resource.h>
 
 #include <linux/device.h>
 #include <linux/export.h>
 #include <linux/platform_device.h>
 
 #define KUNIT_DEVICE_NAME	"drm-kunit-mock-device"
 
+struct drm_printer_ctx {
+	struct drm_printer	p;
+	int			level;
+	struct kunit		*test;
+};
+
+/*
+ * kunit_log() is a macro that only accepts a compile-time log level,
+ * so the level has to be dispatched to the matching kunit_log() call.
+ */
+static void drm_kunit_printf(struct drm_printer *p, struct va_format *vaf)
+{
+	struct drm_printer_ctx *ctx = p->arg;
+
+	switch (ctx->level) {
+	case LOGLEVEL_EMERG:
+		kunit_log(KERN_EMERG, ctx->test, "%pV", vaf);
+		break;
+	case LOGLEVEL_ALERT:
+		kunit_log(KERN_ALERT, ctx->test, "%pV", vaf);
+		break;
+	case LOGLEVEL_CRIT:
+		kunit_log(KERN_CRIT, ctx->test, "%pV", vaf);
+		break;
+	case LOGLEVEL_ERR:
+		kunit_log(KERN_ERR, ctx->test, "%pV", vaf);
+		break;
+	case LOGLEVEL_WARNING:
+		kunit_log(KERN_WARNING, ctx->test, "%pV", vaf);
+		break;
+	case LOGLEVEL_NOTICE:
+		kunit_log(KERN_NOTICE, ctx->test, "%pV", vaf);
+		break;
+	case LOGLEVEL_DEBUG:
+		kunit_log(KERN_DEBUG, ctx->test, "%pV", vaf);
+		break;
+	case LOGLEVEL_INFO:
+	default:
+		kunit_log(KERN_INFO, ctx->test, "%pV", vaf);
+		break;
+	}
+}
+
+/**
+ * drm_kunit_helper_printer - Creates a &drm_printer logging into a KUnit test log
+ * @test: The test context object
+ * @level: The log level to log the messages at, one of the LOGLEVEL_* constants
+ *
+ * Creates a &struct drm_printer that will log anything printed through
+ * it, using kunit_log(), both to the kernel log at the @level log
+ * level and to the log of the KUnit test @test.
+ *
+ * This is the KUnit counterpart of the drm_print.h printers like
+ * drm_info_printer() or drm_err_printer(), and @level should be the
+ * log level of the printer it replaces.
+ *
+ * Returns:
+ * A pointer to the &drm_printer, or an ERR_PTR() otherwise.
+ */
+struct drm_printer *
+drm_kunit_helper_printer(struct kunit *test, int level)
+{
+	struct drm_printer_ctx *ctx;
+
+	ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
+	if (!ctx)
+		return ERR_PTR(-ENOMEM);
+
+	ctx->level = level;
+	ctx->p.printfn = drm_kunit_printf;
+	ctx->p.arg = ctx;
+	ctx->test = test;
+
+	return &ctx->p;
+}
+EXPORT_SYMBOL_GPL(drm_kunit_helper_printer);
+
 static const struct drm_mode_config_funcs drm_mode_config_funcs = {
 	.atomic_check	= drm_atomic_helper_check,
 	.atomic_commit	= drm_atomic_helper_commit,
 };
 
include/drm/drm_kunit_helpers.h +4 -0
index 97d35818f2ce..6aafaeeacf32 100644
@@ -13,12 +13,16 @@ struct drm_connector;
 struct drm_crtc_funcs;
 struct drm_crtc_helper_funcs;
 struct drm_device;
 struct drm_plane_funcs;
 struct drm_plane_helper_funcs;
+struct drm_printer;
 struct kunit;
 
+struct drm_printer *drm_kunit_helper_printer(struct kunit *test,
+					     int level);
+
 struct device *drm_kunit_helper_alloc_device(struct kunit *test);
 void drm_kunit_helper_free_device(struct kunit *test, struct device *dev);
 
 struct drm_device *
 __drm_kunit_helper_alloc_drm_device_with_driver(struct kunit *test,
Signature · 1 line
2.56.1
author maintainer
#44 lore raw

PATCH v9 27/42 drm/tests: Add tests for the atomic state readout

In reply to Maxime Ripard

The atomic state readout framework allows a driver to read the state of its hardware into atomic states, either to create the initial state of a device whose outputs have already been enabled, or to compare a commit against the current hardware state.

Add a KUnit test suite covering:

- the drm_atomic_sro_state container API: allocation, per-object get and set, and installation of the read-out states into a drm_device;
- drm_atomic_helper_sro_readout_state(), including the cases where the readout isn't possible (-ENOTSUPP) because the mode configuration or helper hooks are missing, and error propagation from the readout hooks;
- drm_atomic_sro_readout_and_compare() and the per-object state comparison helpers for planes, CRTCs, connectors and bridges, including the mismatch reporting through a drm_printer;
- drm_mode_config_create_initial_state(), including its error paths and the fallback to the regular initialization path when the readout isn't available.

The tests are built on a mock DRM device whose per-object function tables either implement or omit the readout hooks, with dedicated variants to inject errors from them.

---
 drivers/gpu/drm/drm_atomic_state_readout.c         |   10 +-
 drivers/gpu/drm/tests/Makefile                     |    1 +
 .../gpu/drm/tests/drm_atomic_state_readout_test.c  | 1934 ++++++++++++++++++++
 3 files changed, 1944 insertions(+), 1 deletion(-)
drivers/gpu/drm/drm_atomic_state_readout.c +9 -1
index 4b6512ef1aae..0d9c3cc8317e 100644
@@ -11,10 +11,12 @@
 #include <drm/drm_print.h>
 #include <drm/drm_vblank.h>
 #include <linux/module.h>
 #include <linux/sched/mm.h>
 
+#include <kunit/visibility.h>
+
 #include "drm_internal.h"
 #include "drm_crtc_internal.h"
 
 /**
  * DOC: overview
@@ -65,14 +67,20 @@
  * drm_atomic_helper_sro_install_state(), and
  * drm_atomic_helper_sro_build_state() handle the standard readout
  * sequence.
  */
 
-static unsigned int atomic_readout = DRM_ATOMIC_READOUT_ENABLED;
+/*
+ * Exposed for the KUnit tests, which exercise the various modes the
+ * parameter can be set to. They must restore the default value, as
+ * the tests of every suite run in the same kernel instance.
+ */
+VISIBLE_IF_KUNIT unsigned int atomic_readout = DRM_ATOMIC_READOUT_ENABLED;
 module_param_unsafe(atomic_readout, uint, 0400);
 MODULE_PARM_DESC(atomic_readout,
 		 "Enable Hardware State Readout (0 = disabled, 1 = enabled, 2 = ignore missing compares, 3 = ignore missing readouts and compares, default = 1)");
+EXPORT_SYMBOL_IF_KUNIT(atomic_readout);
 
 static bool drm_atomic_sro_can_readout(struct drm_device *dev)
 {
 	struct drm_crtc *crtc;
 	struct drm_plane *plane;
drivers/gpu/drm/tests/Makefile +1 -0
index 42207df1d1c8..b11a07094b32 100644
@@ -4,10 +4,11 @@ obj-$(CONFIG_DRM_KUNIT_TEST_HELPERS) += \
 	drm_kunit_helpers.o
 
 obj-$(CONFIG_DRM_KUNIT_TEST) += \
 	drm_atomic_test.o \
 	drm_atomic_commit_test.o \
+	drm_atomic_state_readout_test.o \
 	drm_bridge_test.o \
 	drm_cmdline_parser_test.o \
 	drm_connector_test.o \
 	drm_damage_helper_test.o \
 	drm_dp_mst_helper_test.o \
drivers/gpu/drm/tests/drm_atomic_state_readout_test.c new file +1934 -0

1,934 lines not on this page · raw message

Signature · 1 line
2.56.1
author maintainer
#45 lore raw

PATCH v9 28/42 drm/encoder: Create atomic_sro_get_current_crtc hook

In reply to Maxime Ripard

In order for drivers to implement drm_connectors atomic_sro_readout_state hooks, they need to query the hardware and lookup the CRTC to set drm_connector_state.crtc.

It should be easy enough for drivers that are tightly integrated from the CRTC to the connectors, but if the driver uses bridges, there's no coupling between the CRTC and encoder, and the bridge driver.

The only thing the bridge has access to is the encoder, but the relationship between a CRTC and an encoder isn't a fixed mapping at the framework level, and thus the bridge can't deduce which CRTC is feeding its encoder.

Create a new hook for encoders to implement to return the CRTC they are currently connected to.

---
 include/drm/drm_encoder.h | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
include/drm/drm_encoder.h +18 -0
index eded7c34481a..7f41ec0a68dc 100644
@@ -87,10 +87,28 @@ struct drm_encoder_funcs {
 	 * @debugfs_init:
 	 *
 	 * Allows encoders to create encoder-specific debugfs files.
 	 */
 	void (*debugfs_init)(struct drm_encoder *encoder, struct dentry *root);
+
+	/**
+	 * @atomic_sro_get_current_crtc:
+	 *
+	 * This optional hook is called during hardware state readout
+	 * to determine which CRTC is currently driving this encoder.
+	 *
+	 * It is needed by the bridge connector readout code to
+	 * establish the CRTC-encoder-connector association from the
+	 * hardware state, since the relationship between a CRTC and
+	 * an encoder is not a fixed mapping at the framework level.
+	 *
+	 * RETURNS:
+	 *
+	 * The CRTC currently associated with the encoder if enabled,
+	 * NULL otherwise.
+	 */
+	struct drm_crtc *(*atomic_sro_get_current_crtc)(struct drm_encoder *encoder);
 };
 
 /**
  * struct drm_encoder - central DRM encoder structure
  * @dev: parent DRM device
Signature · 1 line
2.56.1
author maintainer
#46 lore raw

PATCH v9 29/42 drm/bridge: display-connector: Implement readout support

In reply to Maxime Ripard

The display-connector bridge is the last bridge in the chain on platforms using drm_bridge_connector, and is responsible for the physical connector interface.

Implement the atomic_sro_readout_state hook and wire up the default drm_atomic_helper_bridge_compare_state() as the compare hook.

---
 drivers/gpu/drm/bridge/display-connector.c | 75 ++++++++++++++++++++++++++++++
 1 file changed, 75 insertions(+)
drivers/gpu/drm/bridge/display-connector.c +75 -0
index eb3412ec30a0..be375f86e038 100644
@@ -12,11 +12,14 @@
 #include <linux/of.h>
 #include <linux/platform_device.h>
 #include <linux/regulator/consumer.h>
 #include <linux/workqueue.h>
 
+#include <drm/drm_atomic.h>
 #include <drm/drm_atomic_helper.h>
+#include <drm/drm_atomic_state_readout.h>
+#include <drm/drm_atomic_state_readout_helper.h>
 #include <drm/drm_bridge.h>
 #include <drm/drm_edid.h>
 
 struct display_connector {
 	struct drm_bridge	bridge;
@@ -211,10 +214,80 @@ static u32 *display_connector_get_input_bus_fmts(struct drm_bridge *bridge,
 	return prev_bridge->funcs->atomic_get_input_bus_fmts(prev_bridge, prev_bridge_state,
 							     crtc_state, conn_state, output_fmt,
 							     num_input_fmts);
 }
 
+/*
+ * Reconstruct the bridge bus configuration from the previous bridge's
+ * readout state and the connector's display_info.
+ *
+ * During normal atomic check, drm_atomic_bridge_chain_select_bus_fmts()
+ * and drm_atomic_bridge_propagate_bus_flags() walk the bridge chain to
+ * negotiate formats and propagate flags. During SRO readout, those
+ * functions do not run, so each bridge must reconstruct its own state.
+ *
+ * The SRO infrastructure traverses bridges from first to last, so the
+ * previous bridge's state is already available. The input bus
+ * configuration is taken from the previous bridge's output. The output
+ * bus format comes from the connector's display_info, since this bridge
+ * is the last in the chain and acts as a passthrough to the connector.
+ * Bus flags are also taken from display_info, matching the default
+ * propagation behavior of drm_atomic_bridge_propagate_bus_flags().
+ */
+static int
+display_connector_readout_state(struct drm_bridge *bridge,
+				struct drm_atomic_sro_state *state,
+				struct drm_bridge_state *bridge_state,
+				struct drm_crtc_state *crtc_state,
+				struct drm_connector_state *conn_state)
+{
+	struct drm_display_info *di = &conn_state->connector->display_info;
+	struct drm_bridge *prev_bridge __free(drm_bridge_put) =
+		drm_bridge_get_prev_bridge(bridge);
+
+	/*
+	 * A previous bridge without readout support still gets a
+	 * pristine state in the SRO state, which doesn't hold any
+	 * meaningful bus configuration. Only use the state of bridges
+	 * that were actually read out.
+	 */
+	if (prev_bridge && prev_bridge->base.funcs->atomic_sro_readout_state) {
+		struct drm_private_state *prev_priv_state =
+			drm_atomic_sro_get_private_obj_state(state,
+							     &prev_bridge->base);
+
+		if (prev_priv_state) {
+			struct drm_bridge_state *prev_bridge_state =
+				drm_priv_to_bridge_state(prev_priv_state);
+
+			bridge_state->input_bus_cfg.format =
+				prev_bridge_state->output_bus_cfg.format;
+			bridge_state->input_bus_cfg.flags =
+				prev_bridge_state->output_bus_cfg.flags;
+			goto out_input;
+		}
+	}
+
+	/*
+	 * The previous bridge might not have readout support, so we
+	 * can't treat a missing state as an error.
+	 */
+	bridge_state->input_bus_cfg.format = MEDIA_BUS_FMT_FIXED;
+	bridge_state->input_bus_cfg.flags = di->bus_flags;
+
+out_input:
+
+	if (di->num_bus_formats && di->bus_formats)
+		bridge_state->output_bus_cfg.format = di->bus_formats[0];
+	else
+		bridge_state->output_bus_cfg.format = MEDIA_BUS_FMT_FIXED;
+
+	bridge_state->output_bus_cfg.flags = di->bus_flags;
+
+	return 0;
+}
+
 static const struct drm_bridge_funcs display_connector_bridge_funcs = {
 	.attach = display_connector_attach,
 	.destroy = display_connector_destroy,
 	.detect = display_connector_bridge_detect,
 	.hpd_enable = display_connector_hpd_enable,
@@ -223,10 +296,12 @@ static const struct drm_bridge_funcs display_connector_bridge_funcs = {
 	.atomic_get_output_bus_fmts = display_connector_get_output_bus_fmts,
 	.atomic_get_input_bus_fmts = display_connector_get_input_bus_fmts,
 	.atomic_duplicate_state = drm_atomic_helper_bridge_duplicate_state,
 	.atomic_destroy_state = drm_atomic_helper_bridge_destroy_state,
 	.atomic_create_state = drm_atomic_helper_bridge_create_state,
+	.atomic_sro_readout_state	= display_connector_readout_state,
+	.atomic_sro_compare_state	= drm_atomic_helper_bridge_compare_state,
 };
 
 static irqreturn_t display_connector_hpd_irq(int irq, void *arg)
 {
 	struct display_connector *conn = arg;
Signature · 1 line
2.56.1
author maintainer
#47 lore raw

PATCH v9 30/42 drm/bridge_connector: Implement hw readout for connector

In reply to Maxime Ripard

drm_bridge_connector allows to create a generic connector from a list of bridges.

However, it's a somewhat virtual connector, and relies on the bridges to implement its various capabilities.

What we actually want though is for the last bridge implementing hardware readout to fill the connector state from its own state.

Thus, implement a new op for bridge_connector to allow just that.

---
 drivers/gpu/drm/display/drm_bridge_connector.c | 65 +++++++++++++++++++++++++-
 include/drm/drm_bridge.h                       | 32 +++++++++++++
 2 files changed, 95 insertions(+), 2 deletions(-)
drivers/gpu/drm/display/drm_bridge_connector.c +63 -2
index 2202e6044444..c1c208abf75c 100644
@@ -9,10 +9,11 @@
 #include <linux/of.h>
 #include <linux/property.h>
 #include <linux/slab.h>
 
 #include <drm/drm_atomic_state_helper.h>
+#include <drm/drm_atomic_state_readout_helper.h>
 #include <drm/drm_bridge.h>
 #include <drm/drm_bridge_connector.h>
 #include <drm/drm_connector.h>
 #include <drm/drm_device.h>
 #include <drm/drm_edid.h>
@@ -65,10 +66,18 @@ struct drm_bridge_connector {
 	 * @encoder:
 	 *
 	 * The encoder at the start of the bridges chain.
 	 */
 	struct drm_encoder *encoder;
+	/**
+	 * @bridge_connector_hw_readout:
+	 *
+	 * The last bridge in the chain (closest to the connector) that
+	 * provides hardware state readout support, if any (see
+	 * &DRM_BRIDGE_OP_CONNECTOR_HW_READOUT).
+	 */
+	struct drm_bridge *bridge_connector_hw_readout;
 	/**
 	 * @bridge_edid:
 	 *
 	 * The last bridge in the chain (closest to the connector) that provides
 	 * EDID read support, if any (see &DRM_BRIDGE_OP_EDID).
@@ -242,10 +251,30 @@ drm_bridge_connector_create_state(struct drm_connector *connector)
 							      connector);
 
 	return conn_state;
 }
 
+static int
+drm_bridge_connector_readout_state(struct drm_connector *connector,
+				   struct drm_atomic_sro_state *state,
+				   struct drm_connector_state *conn_state)
+{
+	struct drm_bridge_connector *bridge_connector =
+		to_drm_bridge_connector(connector);
+	struct drm_bridge *readout =
+		bridge_connector->bridge_connector_hw_readout;
+	int ret;
+
+	if (readout) {
+		ret = readout->funcs->atomic_sro_connector_readout(readout, state, conn_state);
+		if (ret)
+			return ret;
+	}
+
+	return 0;
+}
+
 static enum drm_connector_color_format
 drm_bridge_connector_color_format(const struct drm_connector_state *conn_state)
 {
 	struct drm_bridge_connector *bridge_connector =
 		to_drm_bridge_connector(conn_state->connector);
@@ -275,10 +304,22 @@ static const struct drm_connector_funcs drm_bridge_connector_funcs = {
 	.debugfs_init = drm_bridge_connector_debugfs_init,
 	.oob_hotplug_event = drm_bridge_connector_oob_hotplug_event,
 	.color_format = drm_bridge_connector_color_format,
 };
 
+static const struct drm_connector_funcs drm_bridge_connector_sro_funcs = {
+	.fill_modes = drm_helper_probe_single_connector_modes,
+	.atomic_create_state = drm_bridge_connector_create_state,
+	.atomic_sro_readout_state = drm_bridge_connector_readout_state,
+	.atomic_sro_compare_state = drm_atomic_helper_connector_compare_state,
+	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
+	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
+	.debugfs_init = drm_bridge_connector_debugfs_init,
+	.oob_hotplug_event = drm_bridge_connector_oob_hotplug_event,
+	.color_format = drm_bridge_connector_color_format,
+};
+
 /* -----------------------------------------------------------------------------
  * Bridge Connector Helper Functions
  */
 
 static int drm_bridge_connector_detect_ctx(struct drm_connector *connector,
@@ -825,10 +866,11 @@ static const struct drm_connector_hdmi_cec_funcs drm_bridge_connector_hdmi_cec_f
 
 static void drm_bridge_connector_put_bridges(struct drm_device *dev, void *data)
 {
 	struct drm_bridge_connector *bridge_connector = (struct drm_bridge_connector *)data;
 
+	drm_bridge_put(bridge_connector->bridge_connector_hw_readout);
 	drm_bridge_put(bridge_connector->bridge_edid);
 	drm_bridge_put(bridge_connector->bridge_hpd);
 	drm_bridge_put(bridge_connector->bridge_detect);
 	drm_bridge_put(bridge_connector->bridge_modes);
 	drm_bridge_put(bridge_connector->bridge_hdmi);
@@ -855,10 +897,11 @@ static void drm_bridge_connector_put_bridges(struct drm_device *dev, void *data)
  * pointer otherwise.
  */
 struct drm_connector *drm_bridge_connector_init(struct drm_device *drm,
 						struct drm_encoder *encoder)
 {
+	const struct drm_connector_funcs *connector_funcs;
 	struct drm_bridge_connector *bridge_connector;
 	struct drm_connector *connector;
 	struct i2c_adapter *ddc = NULL;
 	struct drm_bridge *panel_bridge __free(drm_bridge_put) = NULL;
 	bool support_hdcp = false;
@@ -894,10 +937,17 @@ struct drm_connector *drm_bridge_connector_init(struct drm_device *drm,
 		if (!bridge->interlace_allowed)
 			connector->interlace_allowed = false;
 		if (!bridge->ycbcr_420_allowed)
 			connector->ycbcr_420_allowed = false;
 
+		if (bridge->ops & DRM_BRIDGE_OP_CONNECTOR_HW_READOUT) {
+			if (!bridge->funcs->atomic_sro_connector_readout)
+				return ERR_PTR(-EINVAL);
+
+			drm_bridge_put(bridge_connector->bridge_connector_hw_readout);
+			bridge_connector->bridge_connector_hw_readout = drm_bridge_get(bridge);
+		}
 		/*
 		 * Ensure the last bridge declares OP_EDID or OP_MODES or both.
 		 */
 		if (bridge->ops & DRM_BRIDGE_OP_EDID || bridge->ops & DRM_BRIDGE_OP_MODES) {
 			drm_bridge_put(bridge_connector->bridge_edid);
@@ -1026,10 +1076,21 @@ struct drm_connector *drm_bridge_connector_init(struct drm_device *drm,
 	}
 
 	if (connector_type == DRM_MODE_CONNECTOR_Unknown)
 		return ERR_PTR(-EINVAL);
 
+	/*
+	 * Only expose the hardware state readout hooks when a bridge in
+	 * the chain provides a connector readout implementation.
+	 * Otherwise, the readout would silently succeed with a
+	 * pristine, disconnected connector state.
+	 */
+	if (bridge_connector->bridge_connector_hw_readout)
+		connector_funcs = &drm_bridge_connector_sro_funcs;
+	else
+		connector_funcs = &drm_bridge_connector_funcs;
+
 	if (bridge_connector->bridge_hdmi) {
 		bridge_connector->hdmi_funcs = drm_bridge_connector_hdmi_funcs;
 
 		bridge_connector->hdmi_funcs.vendor = bridge_connector->bridge_hdmi->vendor;
 		bridge_connector->hdmi_funcs.product = bridge_connector->bridge_hdmi->product;
@@ -1072,18 +1133,18 @@ struct drm_connector *drm_bridge_connector_init(struct drm_device *drm,
 			bridge_connector->hdmi_funcs.scrambler_disable =
 				drm_bridge_connector_scrambler_disable;
 		}
 
 		ret = drmm_connector_hdmi_init(drm, connector,
-					       &drm_bridge_connector_funcs,
+					       connector_funcs,
 					       &bridge_connector->hdmi_funcs,
 					       connector_type, ddc);
 		if (ret)
 			return ERR_PTR(ret);
 	} else {
 		ret = drmm_connector_init(drm, connector,
-					  &drm_bridge_connector_funcs,
+					  connector_funcs,
 					  connector_type, ddc);
 		if (ret)
 			return ERR_PTR(ret);
 	}
 
include/drm/drm_bridge.h +32 -0
index a74407ee5a9c..a1c953da0975 100644
@@ -978,10 +978,35 @@ struct drm_bridge_funcs {
 	 */
 	int (*dp_audio_mute_stream)(struct drm_bridge *bridge,
 				    struct drm_connector *connector,
 				    bool enable, int direction);
 
+	/**
+	 * @atomic_sro_connector_readout:
+	 *
+	 * This optional hook initializes the &struct drm_connector_state
+	 * based on hardware state.
+	 *
+	 * It is implemented by bridges that set the
+	 * %DRM_BRIDGE_OP_CONNECTOR_HW_READOUT flag in their
+	 * &drm_bridge.ops. When using drm_bridge_connector, the last
+	 * bridge in the chain with this flag set will have its hook
+	 * called to fill the connector state.
+	 *
+	 * Drivers are expected to return a negative error code if the
+	 * hardware was programmed with a configuration they don't
+	 * support. The framework will then abort the readout and fall
+	 * back to the regular initialization path.
+	 *
+	 * RETURNS:
+	 *
+	 * 0 on success, a negative error code otherwise.
+	 */
+	int (*atomic_sro_connector_readout)(struct drm_bridge *bridge,
+					    struct drm_atomic_sro_state *state,
+					    struct drm_connector_state *conn_state);
+
 	/**
 	 * @debugfs_init:
 	 *
 	 * Allows bridges to create bridge-specific debugfs files.
 	 */
@@ -1118,10 +1143,17 @@ enum drm_bridge_ops {
 	 * @DRM_BRIDGE_OP_HDMI_SPD_INFOFRAME: The bridge supports
 	 * &drm_bridge_funcs->hdmi_write_spd_infoframe and
 	 * &drm_bridge_funcs->hdmi_clear_spd_infoframe callbacks.
 	 */
 	DRM_BRIDGE_OP_HDMI_SPD_INFOFRAME = BIT(10),
+	/**
+	 * @DRM_BRIDGE_OP_CONNECTOR_HW_READOUT: The bridge supports the
+	 * &drm_bridge_funcs.atomic_sro_connector_readout callback to
+	 * fill the connector state from the bridge's own hardware state
+	 * during state readout.
+	 */
+	DRM_BRIDGE_OP_CONNECTOR_HW_READOUT = BIT(11),
 };
 
 /**
  * struct drm_bridge - central DRM bridge control structure
  */
Signature · 1 line
2.56.1
author maintainer
#48 lore raw

PATCH v9 31/42 drm/tidss: Implement hw_reset

In reply to Maxime Ripard

The dispc hardware needs to be put in a known-good state when the driver starts. This was previously done unconditionally during dispc_init(), but it should only be performed on the reset path when implementing state readout.

Rename dispc_init_hw() to dispc_reset_hw(), export it, and remove the call from dispc_init(). Implement &drm_mode_config_funcs.hw_reset through tidss_hw_reset() which performs the dispc soft reset and then calls drm_mode_config_helper_hw_reset() to reset the rest of the objects.

The reset now runs from drm_mode_config_create_initial_state(), once runtime PM has been enabled, so it can't use pm_runtime_set_active() anymore and has to take a regular runtime PM reference instead. However, the runtime resume programs the initial dispc configuration and the interrupt enable registers, which the soft reset then wipes. Program them again once the reset is done.

---
 drivers/gpu/drm/tidss/tidss_dispc.c | 55 ++++++++++++++-----------------------
 drivers/gpu/drm/tidss/tidss_dispc.h |  1 +
 drivers/gpu/drm/tidss/tidss_kms.c   | 14 ++++++++++
 3 files changed, 36 insertions(+), 34 deletions(-)
drivers/gpu/drm/tidss/tidss_dispc.c +21 -34
index 8c4d7e148eb0..e186c63ba5fb 100644
@@ -2853,49 +2853,40 @@ static int dispc_softreset(struct dispc_device *dispc)
 	}
 
 	return 0;
 }
 
-static int dispc_init_hw(struct dispc_device *dispc)
+int dispc_reset_hw(struct dispc_device *dispc)
 {
-	struct device *dev = dispc->dev;
+	struct tidss_device *tidss = dispc->tidss;
 	int ret;
 
-	ret = pm_runtime_set_active(dev);
-	if (ret) {
-		dev_err(dev, "Failed to set DSS PM to active\n");
+	/*
+	 * The reset runs from drm_mode_config_create_initial_state(), so
+	 * after the driver has enabled runtime PM. As
+	 * pm_runtime_set_active() is only allowed while runtime PM is
+	 * disabled, rely on the regular runtime PM references to power
+	 * the device up for the reset instead.
+	 */
+	ret = tidss_runtime_get(tidss);
+	if (ret < 0)
 		return ret;
-	}
-
-	ret = clk_prepare_enable(dispc->fclk);
-	if (ret) {
-		dev_err(dev, "Failed to enable DSS fclk\n");
-		goto err_runtime_suspend;
-	}
 
 	ret = dispc_softreset(dispc);
 	if (ret)
-		goto err_clk_disable;
+		goto out;
 
-	clk_disable_unprepare(dispc->fclk);
-	ret = pm_runtime_set_suspended(dev);
-	if (ret) {
-		dev_err(dev, "Failed to set DSS PM to suspended\n");
-		return ret;
-	}
+	/*
+	 * The runtime PM resume above has programmed the initial
+	 * configuration and the interrupt enable registers, and the
+	 * soft reset just wiped them: program them again.
+	 */
+	dispc_initial_config(dispc);
+	tidss_irq_resume(tidss);
 
-	return 0;
-
-err_clk_disable:
-	clk_disable_unprepare(dispc->fclk);
-
-err_runtime_suspend:
-	ret = pm_runtime_set_suspended(dev);
-	if (ret) {
-		dev_err(dev, "Failed to set DSS PM to suspended\n");
-		return ret;
-	}
+out:
+	tidss_runtime_put(tidss);
 
 	return ret;
 }
 
 int dispc_init(struct tidss_device *tidss)
@@ -3001,13 +2992,9 @@ int dispc_init(struct tidss_device *tidss)
 	dev_dbg(dev, "DSS fclk %lu Hz\n", clk_get_rate(dispc->fclk));
 
 	of_property_read_u32(dispc->dev->of_node, "max-memory-bandwidth",
 			     &dispc->memory_bandwidth_limit);
 
-	r = dispc_init_hw(dispc);
-	if (r)
-		return r;
-
 	tidss->dispc = dispc;
 
 	return 0;
 }
drivers/gpu/drm/tidss/tidss_dispc.h +1 -0
index d07352f7b2bd..dcd4d3b18ef4 100644
@@ -142,9 +142,10 @@ void dispc_plane_setup(struct dispc_device *dispc, u32 hw_plane,
 		       struct drm_plane_state *state,
 		       u32 hw_videoport);
 void dispc_plane_enable(struct dispc_device *dispc, u32 hw_plane, bool enable);
 const u32 *dispc_plane_formats(struct dispc_device *dispc, unsigned int *len);
 
+int dispc_reset_hw(struct dispc_device *dispc);
 int dispc_init(struct tidss_device *tidss);
 void dispc_remove(struct tidss_device *tidss);
 
 #endif
drivers/gpu/drm/tidss/tidss_kms.c +14 -0
index 8b9c5fa96e47..9d9cd1990630 100644
@@ -8,10 +8,11 @@
 #include <drm/drm_atomic_helper.h>
 #include <drm/drm_bridge.h>
 #include <drm/drm_gem_framebuffer_helper.h>
 #include <drm/drm_of.h>
 #include <drm/drm_panel.h>
+#include <drm/drm_print.h>
 #include <drm/drm_vblank.h>
 
 #include "tidss_crtc.h"
 #include "tidss_dispc.h"
 #include "tidss_drv.h"
@@ -114,14 +115,27 @@ static int tidss_atomic_check(struct drm_device *ddev,
 	}
 
 	return 0;
 }
 
+static void tidss_hw_reset(struct drm_device *dev)
+{
+	struct tidss_device *tidss = to_tidss(dev);
+	int ret;
+
+	ret = dispc_reset_hw(tidss->dispc);
+	if (ret)
+		drm_warn(dev, "Couldn't reset dispc");
+
+	drm_mode_config_helper_hw_reset(dev);
+}
+
 static const struct drm_mode_config_funcs mode_config_funcs = {
 	.fb_create = drm_gem_fb_create,
 	.atomic_check = tidss_atomic_check,
 	.atomic_commit = drm_atomic_helper_commit,
+	.hw_reset = tidss_hw_reset,
 };
 
 static const char * const tidss_internal_bridge_compatibles[] = {
 	"ti,j721e-dsi",
 };
Signature · 1 line
2.56.1
author maintainer
#49 lore raw

PATCH v9 32/42 drm/tidss: crtc: Implement hardware state readout

In reply to Maxime Ripard

The firmware can leave a video port running when the driver probes, and the KMS state of its CRTC should then reflect it instead of being pristine, so that the first modeset can be skipped if it matches.

Implement the state readout hooks for the CRTCs. tidss_crtc_readout_state() reports the CRTC as active when its video port is enabled, and reads out its mode, bus flags and bus format. The connectors and planes bound to the CRTC are filled by their own readout hooks.

tidss_crtc_install_state() takes over the video port of an active CRTC when its state is installed: it takes the runtime PM reference and the video port clock that tidss_crtc_atomic_disable() later releases, flags the video port as a DPI output, and turns the vblank reporting on. The hook can't fail, so if one of those resources can't be acquired, the CRTC is installed as inactive instead, and the next modeset fully programs the video port.

Finally, implement tidss_crtc_compare_state(), that checks the bus format and bus flags on top of the generic CRTC comparison, unless both states are inactive since the bus configuration isn't read out from a disabled pipeline.

---
 drivers/gpu/drm/tidss/tidss_crtc.c | 132 +++++++++++++++++++++++++++++++++++++
 1 file changed, 132 insertions(+)
drivers/gpu/drm/tidss/tidss_crtc.c +132 -0
index d2664bda53a9..c72194e93bde 100644
@@ -2,12 +2,16 @@
 /*
  * Copyright (C) 2018 Texas Instruments Incorporated - https://www.ti.com/
  * Author: Tomi Valkeinen <[email protected]>
  */
 
+#include <linux/clk.h>
+
 #include <drm/drm_atomic.h>
 #include <drm/drm_atomic_helper.h>
+#include <drm/drm_atomic_state_readout_helper.h>
+#include <drm/drm_atomic_uapi.h>
 #include <drm/drm_crtc.h>
 #include <drm/drm_gem_dma_helper.h>
 #include <drm/drm_print.h>
 #include <drm/drm_vblank.h>
 
@@ -370,10 +374,135 @@ static struct drm_crtc_state *tidss_crtc_create_state(struct drm_crtc *crtc)
 	__drm_atomic_helper_crtc_state_init(&tstate->base, crtc);
 
 	return &tstate->base;
 }
 
+static int tidss_crtc_readout_state(struct drm_crtc *crtc,
+				    struct drm_atomic_sro_state *state,
+				    struct drm_crtc_state *crtc_state)
+{
+	struct drm_device *ddev = crtc->dev;
+	struct tidss_device *tidss = to_tidss(ddev);
+	struct dispc_device *dispc = tidss->dispc;
+	struct tidss_crtc_state *tstate = to_tidss_crtc_state(crtc_state);
+	struct tidss_crtc *tcrtc =
+		to_tidss_crtc(crtc);
+	struct drm_display_mode mode = {};
+	int ret;
+
+	tidss_runtime_get(tidss);
+
+	if (!dispc_vp_is_enabled(dispc, tcrtc->hw_videoport))
+		goto out;
+
+	tstate->base.active = 1;
+	tstate->base.enable = 1;
+
+	ret = dispc_vp_readout_mode(dispc, tcrtc->hw_videoport, &mode);
+	if (ret)
+		goto err_runtime_put;
+
+	ret = drm_atomic_set_mode_for_crtc(&tstate->base, &mode);
+	if (WARN_ON(ret))
+		goto err_runtime_put;
+
+	drm_mode_copy(&tstate->base.adjusted_mode, &mode);
+
+	tstate->bus_flags = dispc_vp_get_bus_flags(dispc, tcrtc->hw_videoport);
+	ret = dispc_vp_get_bus_format(dispc, tcrtc->hw_videoport,
+				      &tstate->bus_format);
+	if (ret)
+		goto err_runtime_put;
+
+	/*
+	 * The active connectors and planes will be filled by their
+	 * respective readout callbacks.
+	 */
+
+out:
+	tidss_runtime_put(tidss);
+	return 0;
+
+err_runtime_put:
+	tidss_runtime_put(tidss);
+	return ret;
+}
+
+static void tidss_crtc_install_state(struct drm_crtc *crtc,
+				     struct drm_crtc_state *crtc_state)
+{
+	struct tidss_crtc *tcrtc = to_tidss_crtc(crtc);
+	struct drm_device *ddev = crtc->dev;
+	struct tidss_device *tidss = to_tidss(ddev);
+	int ret;
+
+	/*
+	 * The video port wasn't enabled in hardware: nothing to take
+	 * over for it, and enabling the clock and the vblank reporting
+	 * would leak their reference for a CRTC that no disable will
+	 * ever release.
+	 */
+	if (!crtc_state->active)
+		return;
+
+	ret = tidss_runtime_get(tidss);
+	if (ret < 0)
+		goto err_inactive;
+
+	ret = dispc_vp_enable_clk(tidss->dispc, tcrtc->hw_videoport);
+	if (ret != 0) {
+		tidss_runtime_put(tidss);
+		goto err_inactive;
+	}
+
+	/*
+	 * dispc_vp_unprepare() only clears DPIENABLE for video ports
+	 * that dispc_vp_setup() flagged as DPI outputs, which hasn't
+	 * run for a video port enabled before the driver probed.
+	 */
+	dispc_vp_set_dpi_output(tidss->dispc, tcrtc->hw_videoport,
+				tcrtc->dpi_output);
+
+	/* Turn vertical blanking interrupt reporting on. */
+	drm_crtc_vblank_on(crtc);
+
+	return;
+
+err_inactive:
+	/*
+	 * This hook can't fail. Without the references taken above, a
+	 * later tidss_crtc_atomic_disable() would release references
+	 * it doesn't hold, so install the CRTC as inactive instead:
+	 * the next modeset will fully program the video port.
+	 */
+	drm_warn(ddev, "[CRTC:%d:%s] Couldn't take over the video port: %d\n",
+		 crtc->base.id, crtc->name, ret);
+	crtc_state->active = false;
+}
+
+static bool tidss_crtc_compare_state(struct drm_crtc *crtc,
+				     struct drm_printer *p,
+				     struct drm_crtc_state *expected,
+				     struct drm_crtc_state *actual)
+{
+	struct tidss_crtc_state *t_expected = to_tidss_crtc_state(expected);
+	struct tidss_crtc_state *t_actual = to_tidss_crtc_state(actual);
+	bool ret = drm_atomic_helper_crtc_compare_state(crtc, p, expected, actual);
+
+	/*
+	 * Both inactive: the bus configuration wasn't read out from the
+	 * disabled pipeline, and the committed one is stale anyway.
+	 */
+	if (!expected->active && !actual->active)
+		return ret;
+
+	STATE_CHECK_U32_X(ret, p, crtc->name, t_expected, t_actual, bus_format);
+	STATE_CHECK_U32_X(ret, p, crtc->name, t_expected, t_actual, bus_flags);
+
+	return ret;
+}
+
 static struct drm_crtc_state *tidss_crtc_duplicate_state(struct drm_crtc *crtc)
 {
 	struct tidss_crtc_state *state, *current_state;
 
 	if (WARN_ON(!crtc->state))
@@ -415,10 +544,13 @@ static void tidss_crtc_atomic_print_state(struct drm_printer *p,
 static const struct drm_crtc_funcs tidss_crtc_funcs = {
 	.destroy = tidss_crtc_destroy,
 	.set_config = drm_atomic_helper_set_config,
 	.page_flip = drm_atomic_helper_page_flip,
 	.atomic_create_state = tidss_crtc_create_state,
+	.atomic_sro_readout_state = tidss_crtc_readout_state,
+	.atomic_sro_install_state = tidss_crtc_install_state,
+	.atomic_sro_compare_state = tidss_crtc_compare_state,
 	.atomic_duplicate_state = tidss_crtc_duplicate_state,
 	.atomic_destroy_state = tidss_crtc_destroy_state,
 	.atomic_print_state = tidss_crtc_atomic_print_state,
 	.enable_vblank = tidss_crtc_enable_vblank,
 	.disable_vblank = tidss_crtc_disable_vblank,
Signature · 1 line
2.56.1
#50 lore raw

Re: [PATCH v9 32/42] drm/tidss: crtc: Implement hardware state readout

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [High] drm/tidss: build failure due to undeclared dispc_vp_*() readout helpers - [Medium] drm/tidss…

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [High] drm/tidss: build failure due to undeclared dispc_vp_*() readout helpers
- [Medium] drm/tidss: unchecked tidss_runtime_get() in tidss_crtc_readout_state()
--

commit 9881c280c684258267a1385984e8908b2bf1845f
Author: Maxime Ripard <[email protected]>

drm/tidss: crtc: Implement hardware state readout

This commit implements the hardware state readout, state installation, and state comparison hooks for tidss CRTCs.

Quoted 4 lines
drivers/gpu/drm/tidss/tidss_crtc.c +0 -0
index d2664bda53a99..c72194e93bde1 100644

[ ... ]

Quoted 8 lines
drivers/gpu/drm/tidss/tidss_crtc.c +4 -0
@@ -372,6 +376,131 @@ static struct drm_crtc_state *tidss_crtc_create_state(struct drm_crtc *crtc)
 	return &tstate->base;
 }
 
+static int tidss_crtc_readout_state(struct drm_crtc *crtc,
+				    struct drm_atomic_sro_state *state,
+				    struct drm_crtc_state *crtc_state)
+{

drivers/gpu/drm/tidss/tidss_crtc.c static struct drm_crtc_state *tidss_crtc_create_state(struct drm_crtc *crtc) · line 392

Quoted 10 lines
drivers/gpu/drm/tidss/tidss_crtc.c +10 -0
+	struct drm_device *ddev = crtc->dev;
+	struct tidss_device *tidss = to_tidss(ddev);
+	struct dispc_device *dispc = tidss->dispc;
+	struct tidss_crtc_state *tstate = to_tidss_crtc_state(crtc_state);
+	struct tidss_crtc *tcrtc =
+		to_tidss_crtc(crtc);
+	struct drm_display_mode mode = {};
+	int ret;
+
+	tidss_runtime_get(tidss);

[Severity: Medium]
Should tidss_crtc_readout_state() check the return value of tidss_runtime_get() before reading DISPC registers, as tidss_crtc_install_state() does below?

tidss_runtime_get() wraps pm_runtime_resume_and_get(), which drops the usage count on failure. Continuing on error accesses DISPC registers while the DSS power domain and functional clock are disabled, and then unconditionally calls tidss_runtime_put() on exit, underflowing the runtime PM usage counter.

Quoted 10 lines
drivers/gpu/drm/tidss/tidss_crtc.c +10 -0
+
+	if (!dispc_vp_is_enabled(dispc, tcrtc->hw_videoport))
+		goto out;
+
+	tstate->base.active = 1;
+	tstate->base.enable = 1;
+
+	ret = dispc_vp_readout_mode(dispc, tcrtc->hw_videoport, &mode);
+	if (ret)
+		goto err_runtime_put;

drivers/gpu/drm/tidss/tidss_crtc.c static struct drm_crtc_state *tidss_crtc_create_state(struct drm_crtc *crtc) · line 412

Quoted 10 lines
drivers/gpu/drm/tidss/tidss_crtc.c +10 -0
+
+	ret = drm_atomic_set_mode_for_crtc(&tstate->base, &mode);
+	if (WARN_ON(ret))
+		goto err_runtime_put;
+
+	drm_mode_copy(&tstate->base.adjusted_mode, &mode);
+
+	tstate->bus_flags = dispc_vp_get_bus_flags(dispc, tcrtc->hw_videoport);
+	ret = dispc_vp_get_bus_format(dispc, tcrtc->hw_videoport,
+				      &tstate->bus_format);

[Severity: High]
Does this commit fail to build when CONFIG_DRM_TIDSS is enabled?

dispc_vp_is_enabled(), dispc_vp_readout_mode(), dispc_vp_get_bus_flags(), dispc_vp_get_bus_format(), and dispc_vp_set_dpi_output() (called in tidss_crtc_install_state() below) are neither declared in tidss_dispc.h nor defined in tidss_dispc.c at this point in the series. They are only added later in commit "drm/tidss: dispc: Add hardware state readout helpers", which breaks compilation and bisection here.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=32
author maintainer
#51 lore raw

PATCH v9 33/42 drm/tidss: encoder: Implement hardware state readout

In reply to Maxime Ripard

The tidss encoder is backed by a bridge that sits first in the bridge chain, between the CRTC and the next bridge. Its state holds the bus configuration that the atomic helpers negotiate when checking a commit.

However, drm_atomic_bridge_chain_select_bus_fmts() and drm_atomic_bridge_propagate_bus_flags() don't run during a state readout, so each bridge has to reconstruct its own bus configuration.

Implement the atomic_sro_readout_state hook for the tidss bridge, taking the CRTC state as its source since the CRTCs are read out before the bridges. The output bus format and the bus flags are taken from the CRTC state, which tidss_bridge_atomic_check() sets from the format the next bridge negotiated. The input bus format is MEDIA_BUS_FMT_FIXED since the bridge doesn't implement atomic_get_input_bus_fmts.

Use drm_atomic_helper_bridge_compare_state() to compare the readout state with the committed one.

---
 drivers/gpu/drm/tidss/tidss_encoder.c | 42 +++++++++++++++++++++++++++++++++++
 1 file changed, 42 insertions(+)
drivers/gpu/drm/tidss/tidss_encoder.c +42 -0
index 10dbcc6cdf6a..5034e601ac86 100644
@@ -3,12 +3,14 @@
  * Copyright (C) 2018 Texas Instruments Incorporated - https://www.ti.com/
  * Author: Tomi Valkeinen <[email protected]>
  */
 
 #include <linux/export.h>
+#include <linux/media-bus-format.h>
 
 #include <drm/drm_atomic_helper.h>
+#include <drm/drm_atomic_state_readout_helper.h>
 #include <drm/drm_bridge.h>
 #include <drm/drm_bridge_connector.h>
 #include <drm/drm_encoder.h>
 #include <drm/drm_crtc.h>
 #include <drm/drm_modeset_helper_vtables.h>
@@ -71,16 +73,56 @@ static int tidss_bridge_atomic_check(struct drm_bridge *bridge,
 	}
 
 	return 0;
 }
 
+static int tidss_readout_state(struct drm_bridge *bridge,
+			       struct drm_atomic_sro_state *state,
+			       struct drm_bridge_state *bridge_state,
+			       struct drm_crtc_state *crtc_state,
+			       struct drm_connector_state *conn_state)
+{
+	struct tidss_crtc_state *tcrtc_state = to_tidss_crtc_state(crtc_state);
+
+	/*
+	 * Reconstruct the bridge bus configuration from the CRTC readout state.
+	 *
+	 * During normal atomic check,
+	 * drm_atomic_bridge_chain_select_bus_fmts() and
+	 * drm_atomic_bridge_propagate_bus_flags() walk the bridge chain to
+	 * negotiate formats and propagate flags. During SRO readout, those
+	 * functions do not run, so each bridge must reconstruct its own
+	 * state.
+	 *
+	 * This bridge is the first in the chain, so the CRTC state (read out
+	 * before bridges) is used as the source. The output bus format is
+	 * taken from the CRTC's bus_format, which during normal atomic check
+	 * is set by tidss_bridge_atomic_check() to the next bridge's
+	 * negotiated input format. The input bus format is
+	 * MEDIA_BUS_FMT_FIXED since this bridge does not implement
+	 * atomic_get_input_bus_fmts. Bus flags are taken from the CRTC
+	 * state, which reads them from hardware registers during its own
+	 * readout.
+	 */
+
+	bridge_state->input_bus_cfg.format = MEDIA_BUS_FMT_FIXED;
+	bridge_state->output_bus_cfg.format = tcrtc_state->bus_format;
+
+	bridge_state->output_bus_cfg.flags = tcrtc_state->bus_flags;
+	bridge_state->input_bus_cfg.flags = tcrtc_state->bus_flags;
+
+	return 0;
+}
+
 static const struct drm_bridge_funcs tidss_bridge_funcs = {
 	.attach				= tidss_bridge_attach,
 	.atomic_check			= tidss_bridge_atomic_check,
 	.atomic_create_state			= drm_atomic_helper_bridge_create_state,
 	.atomic_duplicate_state		= drm_atomic_helper_bridge_duplicate_state,
 	.atomic_destroy_state		= drm_atomic_helper_bridge_destroy_state,
+	.atomic_sro_readout_state	= tidss_readout_state,
+	.atomic_sro_compare_state	= drm_atomic_helper_bridge_compare_state,
 };
 
 static const struct drm_encoder_funcs tidss_encoder_funcs = {
 	.destroy = drm_encoder_cleanup,
 };
Signature · 1 line
2.56.1
author maintainer
#52 lore raw

PATCH v9 34/42 drm/tidss: kms: Hook up hardware state readout

In reply to Maxime Ripard

The CRTC, plane and bridge state readout hooks of the tidss driver are only called by the KMS core if the device opts in to the state readout infrastructure.

Set drm_mode_config_funcs.atomic_sro_readout_state to drm_atomic_helper_sro_readout_state(), and drm_mode_config_helper_funcs.atomic_sro_build_state and drm_mode_config_helper_funcs.atomic_sro_install_state to drm_atomic_helper_sro_build_state() and drm_atomic_helper_sro_install_state() respectively, which implement the standard readout sequence on top of the per-object hooks.

---
 drivers/gpu/drm/tidss/tidss_kms.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
drivers/gpu/drm/tidss/tidss_kms.c +6 -1
index 9d9cd1990630..77294fe34df0 100644
@@ -4,10 +4,12 @@
  * Author: Tomi Valkeinen <[email protected]>
  */
 
 #include <drm/drm_atomic.h>
 #include <drm/drm_atomic_helper.h>
+#include <drm/drm_atomic_state_readout.h>
+#include <drm/drm_atomic_state_readout_helper.h>
 #include <drm/drm_bridge.h>
 #include <drm/drm_gem_framebuffer_helper.h>
 #include <drm/drm_of.h>
 #include <drm/drm_panel.h>
 #include <drm/drm_print.h>
@@ -62,11 +64,13 @@ static void tidss_atomic_commit_tail(struct drm_atomic_commit *old_state)
 
 	tidss_runtime_put(tidss);
 }
 
 static const struct drm_mode_config_helper_funcs mode_config_helper_funcs = {
-	.atomic_commit_tail = tidss_atomic_commit_tail,
+	.atomic_commit_tail	= tidss_atomic_commit_tail,
+	.atomic_sro_build_state = drm_atomic_helper_sro_build_state,
+	.atomic_sro_install_state = drm_atomic_helper_sro_install_state,
 };
 
 static int tidss_atomic_check(struct drm_device *ddev,
 			      struct drm_atomic_commit *state)
 {
@@ -131,10 +135,11 @@ static void tidss_hw_reset(struct drm_device *dev)
 
 static const struct drm_mode_config_funcs mode_config_funcs = {
 	.fb_create = drm_gem_fb_create,
 	.atomic_check = tidss_atomic_check,
 	.atomic_commit = drm_atomic_helper_commit,
+	.atomic_sro_readout_state = drm_atomic_helper_sro_readout_state,
 	.hw_reset = tidss_hw_reset,
 };
 
 static const char * const tidss_internal_bridge_compatibles[] = {
 	"ti,j721e-dsi",
Signature · 1 line
2.56.1
#53 lore raw

Re: [PATCH v9 34/42] drm/tidss: kms: Hook up hardware state readout

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] drm/tidss: unchecked tidss_runtime_get() in tidss_crtc_readout_state() --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] drm/tidss: unchecked tidss_runtime_get() in tidss_crtc_readout_state()
--

commit 056d6b442f1d4a5e8f743bf80ec001d68148711b
Author: Maxime Ripard <[email protected]>

drm/tidss: kms: Hook up hardware state readout

This commit wires up the mode_config and mode_config_helper state readout callbacks so the KMS core invokes the tidss driver's per-object readout hooks.

Quoted 4 lines
drivers/gpu/drm/tidss/tidss_kms.c +0 -0
index 9d9cd19906306..77294fe34df0a 100644

[ ... ]

drivers/gpu/drm/tidss/tidss_kms.c static void tidss_atomic_commit_tail(struct drm_atomic_commit *old_state) · line 72

Quoted 9 lines
drivers/gpu/drm/tidss/tidss_kms.c +3 -1
@@ -64,7 +66,9 @@ static void tidss_atomic_commit_tail(struct drm_atomic_commit *old_state)
 }
 
 static const struct drm_mode_config_helper_funcs mode_config_helper_funcs = {
-	.atomic_commit_tail = tidss_atomic_commit_tail,
+	.atomic_commit_tail	= tidss_atomic_commit_tail,
+	.atomic_sro_build_state = drm_atomic_helper_sro_build_state,
+	.atomic_sro_install_state = drm_atomic_helper_sro_install_state,
 };

[Severity: Medium]
Can wiring up drm_atomic_helper_sro_build_state() read DISPC registers while unclocked and underflow the runtime PM usage count if tidss_runtime_get() fails in tidss_crtc_readout_state()?

drivers/gpu/drm/tidss/tidss_crtc.c:tidss_crtc_readout_state() {
    ...
	tidss_runtime_get(tidss);

	if (!dispc_vp_is_enabled(dispc, tcrtc->hw_videoport))
		goto out;
    ...
out:
	tidss_runtime_put(tidss);
	return 0;
    ...
}

tidss_runtime_get() wraps pm_runtime_resume_and_get(), which already drops the runtime PM reference on failure. Unlike tidss_crtc_install_state() and tidss_plane_atomic_readout_state(), tidss_crtc_readout_state() introduced in commit "drm/tidss: crtc: Implement hardware state readout" does not check the return value of tidss_runtime_get(), so on resume failure it proceeds to read MMIO registers via dispc_vp_is_enabled() and unconditionally calls tidss_runtime_put().

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=34
author maintainer
#54 lore raw

PATCH v9 35/42 drm/tidss: plane: Implement hardware state readout

In reply to Maxime Ripard

When the firmware leaves a video port running, the planes feeding it have to be read out too, otherwise the first commit sees them as disabled and has to reprogram the whole pipeline.

Implement the atomic_sro_readout_state hook for the tidss planes. A plane is read out as disabled if its VID pipeline isn't enabled, if it isn't routed to an overlay layer, or if its CRTC is inactive. The latter two matter because tidss_crtc_atomic_disable() and a disabled video port leave the VID pipeline of their planes enabled.

Otherwise, the CRTC, source and destination rectangles, zpos, alpha and blend mode are read from the registers. The zpos is the index of the overlay layer the plane is routed to, which is the normalized zpos it was committed with. The alpha is the 8-bit global alpha replicated in both bytes of the 16-bit property, the only inverse that maps full opacity back to 0xffff.

The framebuffer is reconstructed from the format, picture size and increments. It has no GEM object since the firmware allocated the buffer, which drm_gem_plane_helper_prepare_fb() and drm_fb_dma_get_scanout_buffer() cope with. It can only be rebuilt when the planes aren't YUV, aren't multiplanar and don't use horizontal decimation, and the readout fails otherwise so that the core falls back to the regular initialization. The offset into the buffer, and thus the source origin, isn't recovered yet.

Use drm_atomic_helper_plane_compare_state() to compare the readout state with the committed one.

---
 drivers/gpu/drm/tidss/tidss_plane.c | 224 ++++++++++++++++++++++++++++++++++++
 1 file changed, 224 insertions(+)
drivers/gpu/drm/tidss/tidss_plane.c +224 -0
index b0d5d6959b88..88f415b5439f 100644
@@ -4,16 +4,19 @@
  * Author: Tomi Valkeinen <[email protected]>
  */
 
 #include <drm/drm_atomic.h>
 #include <drm/drm_atomic_helper.h>
+#include <drm/drm_atomic_state_readout.h>
+#include <drm/drm_atomic_state_readout_helper.h>
 #include <drm/drm_blend.h>
 #include <drm/drm_crtc.h>
 #include <drm/drm_fb_dma_helper.h>
 #include <drm/drm_fourcc.h>
 #include <drm/drm_framebuffer.h>
 #include <drm/drm_gem_atomic_helper.h>
+#include <drm/drm_gem_framebuffer_helper.h>
 #include <drm/drm_panic_helper.h>
 
 #include "tidss_crtc.h"
 #include "tidss_dispc.h"
 #include "tidss_drv.h"
@@ -174,15 +177,236 @@ static const struct drm_plane_helper_funcs tidss_primary_plane_helper_funcs = {
 	.atomic_enable = tidss_plane_atomic_enable,
 	.atomic_disable = tidss_plane_atomic_disable,
 	.get_scanout_buffer = drm_fb_dma_get_scanout_buffer,
 };
 
+static const struct drm_framebuffer_funcs tidss_plane_readout_fb_funcs = {
+	.destroy	= drm_gem_fb_destroy,
+};
+
+static struct drm_framebuffer *tidss_plane_readout_fb(struct drm_plane *plane)
+{
+	struct drm_device *ddev = plane->dev;
+	struct tidss_device *tidss = to_tidss(ddev);
+	struct dispc_device *dispc = tidss->dispc;
+	struct tidss_plane *tplane = to_tidss_plane(plane);
+	const struct drm_format_info *info;
+	struct drm_framebuffer *fb;
+	int ret;
+
+	fb = kzalloc_obj(*fb);
+	if (!fb)
+		return ERR_PTR(-ENOMEM);
+
+	fb->dev = plane->dev;
+
+	info = dispc_plane_get_current_format(dispc, tplane->hw_plane_id);
+	if (IS_ERR(info)) {
+		ret = PTR_ERR(info);
+		goto err_free_fb;
+	}
+
+	// TODO: Figure out YUV and multiplanar formats, along with
+	// the color encoding and range
+	if (info->is_yuv) {
+		ret = -EINVAL;
+		goto err_free_fb;
+	}
+
+	fb->format = info;
+
+	dispc_plane_get_picture_size(dispc, tplane->hw_plane_id, &fb->width,
+				     &fb->height);
+
+	/*
+	 * A pixel increment of 1 means that the DMA engine fetches every
+	 * pixel of the source buffer. Any other value means that
+	 * horizontal decimation is enabled, and the buffer geometry
+	 * cannot be reconstructed from the registers.
+	 */
+	if (dispc_plane_get_pixel_inc(dispc, tplane->hw_plane_id) != 1) {
+		ret = -EINVAL;
+		goto err_free_fb;
+	}
+
+	/*
+	 * The row increment register holds the extra bytes between the
+	 * end of a row of the picture and the beginning of the next one,
+	 * which includes any padding the firmware might have programmed.
+	 */
+	fb->pitches[0] = fb->width * (drm_format_info_bpp(info, 0) / 8) +
+			 dispc_plane_get_row_inc(dispc, tplane->hw_plane_id) - 1;
+
+	/*
+	 * TODO: The DMA base address has the offsets baked in, and there
+	 * is no backing buffer to recover them from.
+	 */
+	fb->offsets[0] = 0;
+
+	ret = drm_framebuffer_init(plane->dev, fb, &tidss_plane_readout_fb_funcs);
+	if (ret) {
+		kfree(fb);
+		return ERR_PTR(ret);
+	}
+
+	return fb;
+
+err_free_fb:
+	kfree(fb);
+	return ERR_PTR(ret);
+}
+
+static struct drm_crtc *tidss_plane_readout_crtc(struct drm_plane *plane,
+						 int *crtc_x, int *crtc_y,
+						 u32 *normalized_zpos)
+{
+	struct drm_device *dev = plane->dev;
+	struct tidss_device *tidss = to_tidss(dev);
+	struct tidss_plane *tplane = to_tidss_plane(plane);
+	struct drm_crtc *crtc;
+	u32 hw_videoport, layer;
+	int ret;
+
+	ret = dispc_ovr_get_plane(tidss->dispc, tplane->hw_plane_id,
+				  &hw_videoport, crtc_x, crtc_y, &layer);
+	if (ret)
+		return ERR_PTR(ret);
+
+	*normalized_zpos = layer;
+
+	drm_for_each_crtc(crtc, dev) {
+		if (to_tidss_crtc(crtc)->hw_videoport == hw_videoport)
+			return crtc;
+	}
+
+	return ERR_PTR(-ENODEV);
+}
+
+static int tidss_plane_atomic_readout_state(struct drm_plane *plane,
+					    struct drm_atomic_sro_state *state,
+					    struct drm_plane_state *plane_state)
+{
+	struct drm_device *ddev = plane->dev;
+	struct tidss_device *tidss = to_tidss(ddev);
+	struct dispc_device *dispc = tidss->dispc;
+	struct tidss_plane *tplane = to_tidss_plane(plane);
+	struct drm_crtc_state *crtc_state;
+	struct drm_framebuffer *fb;
+	struct drm_crtc *crtc;
+	unsigned int in_w, in_h;
+	int crtc_x, crtc_y;
+	u32 normalized_zpos;
+	int ret;
+
+	ret = tidss_runtime_get(tidss);
+	if (ret < 0)
+		return ret;
+
+	if (!dispc_plane_is_enabled(dispc, tplane->hw_plane_id))
+		goto out;
+
+	crtc = tidss_plane_readout_crtc(plane, &crtc_x, &crtc_y,
+					&normalized_zpos);
+	if (IS_ERR(crtc)) {
+		ret = PTR_ERR(crtc);
+
+		/*
+		 * tidss_crtc_atomic_disable() disables the overlay layers of
+		 * the video port, but the planes on a CRTC being disabled
+		 * aren't disabled in the VID pipeline. A plane that isn't
+		 * routed to any layer is thus disabled.
+		 */
+		if (ret == -ENODEV)
+			goto out;
+
+		goto err_runtime_pm;
+	}
+
+	crtc_state = drm_atomic_sro_get_crtc_state(state, crtc);
+	if (!crtc_state) {
+		ret = -ENODEV;
+		goto err_runtime_pm;
+	}
+
+	/*
+	 * K2G has no overlay manager to tell us whether the plane is
+	 * routed, and the planes of a disabled video port are left
+	 * enabled: a plane on an inactive CRTC isn't scanned out.
+	 */
+	if (!crtc_state->active)
+		goto out;
+
+	fb = tidss_plane_readout_fb(plane);
+	if (IS_ERR(fb)) {
+		ret = PTR_ERR(fb);
+		goto err_runtime_pm;
+	}
+
+	plane_state->fb = fb;
+	plane_state->crtc = crtc;
+	plane_state->visible = true;
+
+	dispc_plane_get_picture_size(dispc, tplane->hw_plane_id, &in_w, &in_h);
+	plane_state->src_w = in_w << 16;
+	plane_state->src_h = in_h << 16;
+
+	dispc_plane_get_size(dispc, tplane->hw_plane_id, &plane_state->crtc_w,
+			     &plane_state->crtc_h);
+
+	plane_state->crtc_x = crtc_x;
+	plane_state->crtc_y = crtc_y;
+
+	/*
+	 * The overlay layer index a plane is routed to is its
+	 * normalized zpos, as that is what the atomic update uses as
+	 * the layer index. The raw zpos property userspace set
+	 * cannot be recovered from the hardware.
+	 */
+	plane_state->normalized_zpos = normalized_zpos;
+	plane_state->zpos = normalized_zpos;
+
+	// TODO: Handle src_x/src_y, see DISPC_VID_BA_0
+
+	plane_state->src.x1 = 0;
+	plane_state->src.x2 = plane_state->src_w;
+	plane_state->src.y1 = 0;
+	plane_state->src.y2 = plane_state->src_h;
+	plane_state->dst.x1 = crtc_x;
+	plane_state->dst.x2 = crtc_x + plane_state->crtc_w;
+	plane_state->dst.y1 = crtc_y;
+	plane_state->dst.y2 = crtc_y + plane_state->crtc_h;
+
+	/*
+	 * The global alpha register is 8 bits wide and is programmed
+	 * from the high byte of the 16 bits alpha property.
+	 * Replicating the byte is the only inverse that preserves the
+	 * fraction of full opacity (0x00 -> 0x0000, 0xff -> 0xffff)
+	 * and round-trips through a program-then-readout cycle.
+	 */
+	plane_state->alpha =
+		dispc_plane_get_global_alpha(dispc, tplane->hw_plane_id) * 0x0101;
+	plane_state->pixel_blend_mode =
+		dispc_plane_get_blend_mode(dispc, tplane->hw_plane_id);
+
+	crtc_state->plane_mask |= drm_plane_mask(plane);
+
+out:
+	tidss_runtime_put(tidss);
+	return 0;
+
+err_runtime_pm:
+	tidss_runtime_put(tidss);
+	return ret;
+}
+
 static const struct drm_plane_funcs tidss_plane_funcs = {
 	.update_plane = drm_atomic_helper_update_plane,
 	.disable_plane = drm_atomic_helper_disable_plane,
 	.destroy = drm_plane_destroy,
 	.atomic_create_state = drm_atomic_helper_plane_create_state,
+	.atomic_sro_readout_state = tidss_plane_atomic_readout_state,
+	.atomic_sro_compare_state = drm_atomic_helper_plane_compare_state,
 	.atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
 	.atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
 	DRM_PANIC_PLANE_FUNCS,
 };
 
Signature · 1 line
2.56.1
#55 lore raw

Re: [PATCH v9 35/42] drm/tidss: plane: Implement hardware state readout

Thank you for your contribution! Sashiko AI review found 4 potential issues to consider: - [High] drm/tidss: build failure from calling dispc readout helpers before definition - [Medium] drm/tidss…

Thank you for your contribution! Sashiko AI review found 4 potential issues to consider:
- [High] drm/tidss: build failure from calling dispc readout helpers before definition
- [Medium] drm/tidss: plane readout miscalculates pitch and height on vertical decimation
- [Medium] drm/tidss: unhandled NULL crtc_state in tidss_plane_atomic_readout_state()
- [Medium] drm/tidss: false state mismatch in tidss_plane_atomic_readout_state()
--

commit 06a115c7e84f276955999ca7175b2ba65fbac981
Author: Maxime Ripard <[email protected]>

drm/tidss: plane: Implement hardware state readout

This commit implements the atomic_sro_readout_state and atomic_sro_compare_state hooks for tidss planes to reconstruct plane and framebuffer state from the hardware registers.

Quoted 4 lines
drivers/gpu/drm/tidss/tidss_plane.c +0 -0
index b0d5d6959b885..88f415b5439f4 100644

[ ... ]

Quoted 15 lines
drivers/gpu/drm/tidss/tidss_plane.c +11 -0
@@ -176,11 +179,232 @@ static const struct drm_plane_helper_funcs tidss_primary_plane_helper_funcs = {
 	.get_scanout_buffer = drm_fb_dma_get_scanout_buffer,
 };
 
+static const struct drm_framebuffer_funcs tidss_plane_readout_fb_funcs = {
+	.destroy	= drm_gem_fb_destroy,
+};
+
+static struct drm_framebuffer *tidss_plane_readout_fb(struct drm_plane *plane)
+{
+	struct drm_device *ddev = plane->dev;
+	struct tidss_device *tidss = to_tidss(ddev);
+	struct dispc_device *dispc = tidss->dispc;
+	struct tidss_plane *tplane = to_tidss_plane(plane);
+	const struct drm_format_info *info;

drivers/gpu/drm/tidss/tidss_plane.c static const struct drm_plane_helper_funcs tidss_primary_plane_helper_funcs = { · line 202

Quoted 10 lines
drivers/gpu/drm/tidss/tidss_plane.c +10 -0
+	struct drm_framebuffer *fb;
+	int ret;
+
+	fb = kzalloc_obj(*fb);
+	if (!fb)
+		return ERR_PTR(-ENOMEM);
+
+	fb->dev = plane->dev;
+
+	info = dispc_plane_get_current_format(dispc, tplane->hw_plane_id);

[Severity: High]
Does this fail to build at this commit in the series?

tidss_plane_readout_fb(), tidss_plane_readout_crtc(), and tidss_plane_atomic_readout_state() call dispc_plane_get_current_format(), dispc_plane_get_picture_size(), dispc_plane_get_pixel_inc(), dispc_plane_get_row_inc(), dispc_ovr_get_plane(), dispc_plane_is_enabled(), dispc_plane_get_size(), dispc_plane_get_global_alpha(), and dispc_plane_get_blend_mode(), which are only declared and defined in the subsequent commit "drm/tidss: dispc:
Add hardware state readout helpers". Reordering that patch before this one would keep the series bisectable.

[ ... ]

Quoted 8 lines
drivers/gpu/drm/tidss/tidss_plane.c +8 -0
+	/*
+	 * A pixel increment of 1 means that the DMA engine fetches every
+	 * pixel of the source buffer. Any other value means that
+	 * horizontal decimation is enabled, and the buffer geometry
+	 * cannot be reconstructed from the registers.
+	 */
+	if (dispc_plane_get_pixel_inc(dispc, tplane->hw_plane_id) != 1) {
+		ret = -EINVAL;

drivers/gpu/drm/tidss/tidss_plane.c static const struct drm_plane_helper_funcs tidss_primary_plane_helper_funcs = { · line 237

Quoted 10 lines
drivers/gpu/drm/tidss/tidss_plane.c +10 -0
+		goto err_free_fb;
+	}
+
+	/*
+	 * The row increment register holds the extra bytes between the
+	 * end of a row of the picture and the beginning of the next one,
+	 * which includes any padding the firmware might have programmed.
+	 */
+	fb->pitches[0] = fb->width * (drm_format_info_bpp(info, 0) / 8) +
+			 dispc_plane_get_row_inc(dispc, tplane->hw_plane_id) - 1;

[Severity: Medium]
Can vertical decimation without horizontal decimation cause tidss_plane_readout_fb() and tidss_plane_atomic_readout_state() to miscalculate fb->pitches[0], fb->height, plane_state->src_h, and plane_state->src.y2?

In dispc_vid_calc_scaling(), vertical decimation (scale.yinc > 1) is computed independently of horizontal decimation (scale.xinc):

drivers/gpu/drm/tidss/tidss_dispc.c:dispc_plane_setup() {
...
dispc_vid_write(dispc, hw_plane, DISPC_VID_PICTURE_SIZE,
FIELD_PREP(DISPC_VID_PICTURE_SIZE_MEMSIZEY_MASK, scale.in_h - 1) | FIELD_PREP(DISPC_VID_PICTURE_SIZE_MEMSIZEX_MASK, scale.in_w - 1));
...
dispc_vid_write(dispc, hw_plane, DISPC_VID_ROW_INC,
pixinc(1 + (scale.yinc * fb_width -
scale.xinc * scale.in_w),
cpp));
...
}

When a plane downscales vertically with scale.xinc == 1 and scale.yinc > 1, DISPC_VID_PIXEL_INC remains 1, so readout succeeds but computes fb->pitches[0] as scale.yinc * pitches[0] and reads back the decimated height (src_h >> 16) / scale.yinc from DISPC_VID_PICTURE_SIZE. This triggers post-commit state mismatch warnings in drm_atomic_sro_readout_and_compare() and reconstructs bogus geometry if firmware leaves vertical decimation enabled at boot.

[ ... ]

drivers/gpu/drm/tidss/tidss_plane.c static const struct drm_plane_helper_funcs tidss_primary_plane_helper_funcs = { · line 336

Quoted 13 lines
drivers/gpu/drm/tidss/tidss_plane.c +13 -0
+	crtc_state = drm_atomic_sro_get_crtc_state(state, crtc);
+	if (!crtc_state) {
+		ret = -ENODEV;
+		goto err_runtime_pm;
+	}
+
+	/*
+	 * K2G has no overlay manager to tell us whether the plane is
+	 * routed, and the planes of a disabled video port are left
+	 * enabled: a plane on an inactive CRTC isn't scanned out.
+	 */
+	if (!crtc_state->active)
+		goto out;

[Severity: Medium]
Should a NULL crtc_state jump to out instead of returning -ENODEV?

Because tidss_atomic_commit_tail() commits planes with DRM_PLANE_COMMIT_ACTIVE_ONLY, disabling a CRTC leaves DISPC_VID_ATTRIBUTES_ENABLE set on its planes, and on K2G (which has no overlay manager) or when firmware leaves a plane routed to a disabled video port, tidss_plane_readout_crtc() still returns that inactive CRTC. If a later blocking commit updates a property on that unbound plane without touching the inactive CRTC, drm_atomic_helper_sro_readout_commit_crtcs_state() does not populate that CRTC in state, so drm_atomic_sro_get_crtc_state() returns NULL and aborts post-commit state readout with -ENODEV instead of treating the plane as disabled.

[ ... ]

Quoted 31 lines
drivers/gpu/drm/tidss/tidss_plane.c +31 -0
+	// TODO: Handle src_x/src_y, see DISPC_VID_BA_0
+
+	plane_state->src.x1 = 0;
+	plane_state->src.x2 = plane_state->src_w;
+	plane_state->src.y1 = 0;
+	plane_state->src.y2 = plane_state->src_h;
+	plane_state->dst.x1 = crtc_x;
+	plane_state->dst.x2 = crtc_x + plane_state->crtc_w;
+	plane_state->dst.y1 = crtc_y;
+	plane_state->dst.y2 = crtc_y + plane_state->crtc_h;
+
+	/*
+	 * The global alpha register is 8 bits wide and is programmed
+	 * from the high byte of the 16 bits alpha property.
+	 * Replicating the byte is the only inverse that preserves the
+	 * fraction of full opacity (0x00 -> 0x0000, 0xff -> 0xffff)
+	 * and round-trips through a program-then-readout cycle.
+	 */
+	plane_state->alpha =
+		dispc_plane_get_global_alpha(dispc, tplane->hw_plane_id) * 0x0101;
+	plane_state->pixel_blend_mode =
+		dispc_plane_get_blend_mode(dispc, tplane->hw_plane_id);
+
+	crtc_state->plane_mask |= drm_plane_mask(plane);
+
+out:
+	tidss_runtime_put(tidss);
+	return 0;
+
+err_runtime_pm:
+	tidss_runtime_put(tidss);

drivers/gpu/drm/tidss/tidss_plane.c static const struct drm_plane_helper_funcs tidss_primary_plane_helper_funcs = { · line 407

Quoted 10 lines
drivers/gpu/drm/tidss/tidss_plane.c +5 -0
+	return ret;
+}
+
 static const struct drm_plane_funcs tidss_plane_funcs = {
 	.update_plane = drm_atomic_helper_update_plane,
 	.disable_plane = drm_atomic_helper_disable_plane,
 	.destroy = drm_plane_destroy,
 	.atomic_create_state = drm_atomic_helper_plane_create_state,
+	.atomic_sro_readout_state = tidss_plane_atomic_readout_state,
+	.atomic_sro_compare_state = drm_atomic_helper_plane_compare_state,

[Severity: Medium]
Will drm_atomic_helper_plane_compare_state() report false state mismatches whenever a plane is committed with non-zero src_x/src_y or a 16-bit alpha whose high and low bytes differ?

drm_atomic_helper_check_plane_state() sets expected->src to (src_x, src_y) .. (src_x + src_w, src_y + src_h), whereas tidss_plane_atomic_readout_state() hardcodes actual->src.x1 and actual->src.y1 to 0, causing the src.{x1,x2,y1,y2} checks in drm_atomic_helper_plane_compare_state() to fail on any cropped or panned plane. Similarly, dispc_plane_setup() programs DISPC_VID_GLOBAL_ALPHA with state->alpha >> 8, so comparing expected->alpha against the replicated actual->alpha fails whenever (alpha >> 8) != (alpha & 0xff) (for example, 0x8000).

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=35
author maintainer
#56 lore raw

PATCH v9 36/42 drm/tidss: dispc: Add hardware state readout helpers

In reply to Maxime Ripard

The state readout of the tidss CRTCs, planes and OLDI transmitters has to decode what the firmware programmed in the DISPC registers back into KMS state. However, the driver only has helpers to program those registers.

Add helpers to read out the video ports: whether one is enabled, its display mode, its bus format and its bus flags. The timing registers don't hold the refresh rate, so dispc_vp_readout_mode() derives it by matching the video port clock rate against the 30, 50 and 60 Hz standard refresh rates, with and without the 1000/1001 factor, and fails if none of them is close enough. The bus format is decoded from the DATALINES field, along with the OLDI mapping on the AM65x video ports that drive an OLDI transmitter.

Add helpers to read out the planes: whether one is enabled, the video port, overlay layer and position it is routed to with dispc_ovr_get_plane(), its picture and output sizes, its format, global alpha and blend mode, and its pixel and row increments.

Two existing paths also need to cope with a pipeline the firmware left running:

- dispc_vp_init() disables DPIENABLE for every video port. Skip the ones that are enabled, so that their output keeps running until it is read out.

- dispc_plane_setup() programs the DMA address of the framebuffer. A framebuffer reconstructed from a readout has no GEM object to derive it from, and the hardware is already scanning out that buffer, so keep the address the firmware programmed.

Finally, split dispc_vp_set_dpi_output() out of dispc_vp_setup(), so that the CRTC can record the DPI output state of a video port it takes over without going through a full setup.

---
 drivers/gpu/drm/tidss/tidss_dispc.c | 444 +++++++++++++++++++++++++++++++++++-
 drivers/gpu/drm/tidss/tidss_dispc.h |  22 ++
 2 files changed, 457 insertions(+), 9 deletions(-)
drivers/gpu/drm/tidss/tidss_dispc.c +435 -9
index e186c63ba5fb..8e61216d4532 100644
@@ -1149,10 +1149,16 @@ void dispc_vp_enable(struct dispc_device *dispc, u32 hw_videoport)
 {
 	VP_REG_FLD_MOD(dispc, hw_videoport, DISPC_VP_CONTROL, 1,
 		       DISPC_VP_CONTROL_ENABLE_MASK);
 }
 
+bool dispc_vp_is_enabled(struct dispc_device *dispc, u32 hw_videoport)
+{
+	return FIELD_GET(DISPC_VP_CONTROL_ENABLE_MASK,
+			 dispc_vp_read(dispc, hw_videoport, DISPC_VP_CONTROL));
+}
+
 void dispc_vp_disable(struct dispc_device *dispc, u32 hw_videoport)
 {
 	VP_REG_FLD_MOD(dispc, hw_videoport, DISPC_VP_CONTROL, 0,
 		       DISPC_VP_CONTROL_ENABLE_MASK);
 }
@@ -1399,10 +1405,232 @@ int dispc_vp_set_clk_rate(struct dispc_device *dispc, u32 hw_videoport,
 		hw_videoport, clk_get_rate(dispc->vp_clk[hw_videoport]), rate);
 
 	return 0;
 }
 
+static unsigned long calc_pixel_clock_hz(unsigned int htotal,
+					 unsigned int vtotal,
+					 unsigned int refresh,
+					 unsigned int freq_div)
+{
+	u64 rate = (u64)htotal * vtotal * refresh;
+
+	return div_u64(rate * 1000, freq_div);
+}
+
+static const unsigned int refresh_tries[] = {30, 50, 60};
+static const unsigned int refresh_factors_tries[] = {1000, 1001};
+
+static unsigned int dispc_find_closest_refresh_rate_from_clk(struct drm_device *dev,
+							     struct clk *clk,
+							     unsigned int htotal,
+							     unsigned int vtotal,
+							     unsigned long *pixel_clock_hz)
+{
+	unsigned long actual_clk_rate = clk_get_rate(clk);
+	unsigned long best_clk_rate = 0;
+	unsigned long best_rate_diff = ULONG_MAX;
+	unsigned int best_refresh = 0;
+	unsigned int i, j;
+
+	drm_dbg(dev, "Actual clock rate is %lu\n", actual_clk_rate);
+
+	for (i = 0; i < ARRAY_SIZE(refresh_tries); i++) {
+		for (j = 0; j < ARRAY_SIZE(refresh_factors_tries); j++) {
+			unsigned int try_refresh = refresh_tries[i];
+			unsigned int try_factor = refresh_factors_tries[j];
+			unsigned long try_clk_rate = calc_pixel_clock_hz(htotal,
+									 vtotal,
+									 try_refresh,
+									 try_factor);
+			unsigned long diff;
+
+			drm_dbg(dev, "Evaluating refresh %u, factor %u, rate %lu\n",
+				try_refresh, try_factor, try_clk_rate);
+
+			if (try_clk_rate == actual_clk_rate) {
+				drm_dbg(dev, "Found exact match. Stopping.\n");
+				best_refresh = try_refresh;
+				best_clk_rate = try_clk_rate;
+				goto out;
+			}
+
+
+			diff = abs_diff(actual_clk_rate, try_clk_rate);
+			if (diff < best_rate_diff) {
+				drm_dbg(dev, "Found new candidate. Difference is %lu\n", diff);
+				best_refresh = try_refresh;
+				best_clk_rate = try_clk_rate;
+				best_rate_diff = diff;
+			}
+		}
+	}
+
+	/*
+	 * The video port clock doesn't match any standard refresh rate,
+	 * so the mode we would report would be plain wrong. Report the
+	 * configuration as unsupported by returning 0, so that the
+	 * caller falls back on the regular initialization path.
+	 *
+	 * We tolerate the same 5% difference that
+	 * dispc_vp_set_clk_rate() accepts when programming the clock.
+	 */
+	if (dispc_pclk_diff(best_clk_rate, actual_clk_rate) > 5) {
+		drm_dbg(dev, "Clock rate %lu doesn't match any standard refresh rate\n",
+			actual_clk_rate);
+		return 0;
+	}
+
+out:
+	drm_dbg(dev, "Best candidate is %u Hz, pixel clock rate %lu Hz",
+		best_refresh, best_clk_rate);
+
+	if (pixel_clock_hz)
+		*pixel_clock_hz = best_clk_rate;
+
+	return best_refresh;
+}
+
+int dispc_vp_readout_mode(struct dispc_device *dispc,
+			  u32 hw_videoport,
+			  struct drm_display_mode *mode)
+{
+	struct tidss_device *tidss = dispc->tidss;
+	struct drm_device *dev = &tidss->ddev;
+	unsigned long pixel_clock;
+	unsigned int refresh;
+	u16 hdisplay, hfp, hsw, hbp;
+	u16 vdisplay, vfp, vsw, vbp;
+	u32 val;
+
+	val = dispc_vp_read(dispc, hw_videoport, DISPC_VP_SIZE_SCREEN);
+	hdisplay = FIELD_GET(DISPC_VP_SIZE_SCREEN_HDISPLAY_MASK, val) + 1;
+	vdisplay = FIELD_GET(DISPC_VP_SIZE_SCREEN_VDISPLAY_MASK, val) + 1;
+
+	mode->hdisplay = hdisplay;
+	mode->vdisplay = vdisplay;
+
+	val = dispc_vp_read(dispc, hw_videoport, DISPC_VP_TIMING_H);
+	hsw = FIELD_GET(DISPC_VP_TIMING_H_SYNC_PULSE_MASK, val) + 1;
+	hfp = FIELD_GET(DISPC_VP_TIMING_H_FRONT_PORCH_MASK, val) + 1;
+	hbp = FIELD_GET(DISPC_VP_TIMING_H_BACK_PORCH_MASK, val) + 1;
+
+	mode->hsync_start = hdisplay + hfp;
+	mode->hsync_end = hdisplay + hfp + hsw;
+	mode->htotal = hdisplay + hfp + hsw + hbp;
+
+	val = dispc_vp_read(dispc, hw_videoport, DISPC_VP_TIMING_V);
+	vsw = FIELD_GET(DISPC_VP_TIMING_V_SYNC_PULSE_MASK, val) + 1;
+	vfp = FIELD_GET(DISPC_VP_TIMING_V_FRONT_PORCH_MASK, val);
+	vbp = FIELD_GET(DISPC_VP_TIMING_V_BACK_PORCH_MASK, val);
+
+	mode->vsync_start = vdisplay + vfp;
+	mode->vsync_end = vdisplay + vfp + vsw;
+	mode->vtotal = vdisplay + vfp + vsw + vbp;
+
+	refresh = dispc_find_closest_refresh_rate_from_clk(dev,
+							   dispc->vp_clk[hw_videoport],
+							   mode->htotal,
+							   mode->vtotal,
+							   &pixel_clock);
+	if (!refresh)
+		return -EINVAL;
+
+	mode->clock = pixel_clock / 1000;
+
+	val = dispc_vp_read(dispc, hw_videoport, DISPC_VP_POL_FREQ);
+	if (FIELD_GET(DISPC_VP_POL_FREQ_IVS_MASK, val))
+		mode->flags |= DRM_MODE_FLAG_NVSYNC;
+	else
+		mode->flags |= DRM_MODE_FLAG_PVSYNC;
+
+	if (FIELD_GET(DISPC_VP_POL_FREQ_IHS_MASK, val))
+		mode->flags |= DRM_MODE_FLAG_NHSYNC;
+	else
+		mode->flags |= DRM_MODE_FLAG_PHSYNC;
+
+	mode->type |= DRM_MODE_TYPE_DRIVER;
+	drm_mode_set_name(mode);
+	drm_mode_set_crtcinfo(mode, 0);
+
+	return 0;
+}
+
+int dispc_vp_get_bus_format(struct dispc_device *dispc, u32 hw_videoport,
+			    u32 *bus_fmt)
+{
+	static const u32 datalines_to_width[] = { 12, 16, 18, 24, 30, 36 };
+	struct tidss_device *tidss = dispc->tidss;
+	struct drm_device *dev = &tidss->ddev;
+	u32 datalines, data_width;
+	unsigned int i;
+
+	datalines = VP_REG_GET(dispc, hw_videoport, DISPC_VP_CONTROL,
+			       DISPC_VP_CONTROL_DATALINES_MASK);
+
+	if (datalines < ARRAY_SIZE(datalines_to_width)) {
+		data_width = datalines_to_width[datalines];
+	} else {
+		drm_dbg(dev, "Unknown DATALINES encoding %u on %s\n",
+			datalines, dispc->feat->vp_name[hw_videoport]);
+		return -EINVAL;
+	}
+
+	if (dispc_vp_is_am65x_oldi(dispc->feat, hw_videoport)) {
+		u32 oldi_cfg = dispc_vp_read(dispc, hw_videoport,
+					     DISPC_VP_DSS_OLDI_CFG);
+		u32 map = FIELD_GET(DISPC_VP_DSS_OLDI_CFG_MAP_MASK, oldi_cfg);
+
+		for (i = 0; i < ARRAY_SIZE(dispc_bus_formats); i++) {
+			if (dispc_bus_formats[i].is_oldi_fmt &&
+			    dispc_bus_formats[i].data_width == data_width &&
+			    dispc_bus_formats[i].am65x_oldi_mode_reg_val == map) {
+				*bus_fmt = dispc_bus_formats[i].bus_fmt;
+				return 0;
+			}
+		}
+
+		drm_dbg(dev, "Unknown OLDI mapping %u on %s\n", map,
+			dispc->feat->vp_name[hw_videoport]);
+		return -EINVAL;
+	}
+
+	for (i = 0; i < ARRAY_SIZE(dispc_bus_formats); i++) {
+		if (!dispc_bus_formats[i].is_oldi_fmt &&
+		    dispc_bus_formats[i].data_width == data_width) {
+			*bus_fmt = dispc_bus_formats[i].bus_fmt;
+			return 0;
+		}
+	}
+
+	return -EINVAL;
+}
+
+u32 dispc_vp_get_bus_flags(struct dispc_device *dispc, u32 hw_videoport)
+{
+	u32 flags = 0;
+	u32 val;
+
+	val = dispc_vp_read(dispc, hw_videoport, DISPC_VP_POL_FREQ);
+	if (FIELD_GET(DISPC_VP_POL_FREQ_IPC_MASK, val))
+		flags |= DRM_BUS_FLAG_PIXDATA_DRIVE_NEGEDGE;
+	else
+		flags |= DRM_BUS_FLAG_PIXDATA_DRIVE_POSEDGE;
+
+	if (FIELD_GET(DISPC_VP_POL_FREQ_IEO_MASK, val))
+		flags |= DRM_BUS_FLAG_DE_LOW;
+	else
+		flags |= DRM_BUS_FLAG_DE_HIGH;
+
+	if (FIELD_GET(DISPC_VP_POL_FREQ_RF_MASK, val))
+		flags |= DRM_BUS_FLAG_SYNC_DRIVE_POSEDGE;
+	else
+		flags |= DRM_BUS_FLAG_SYNC_DRIVE_NEGEDGE;
+
+	return flags;
+}
+
 /* OVR */
 static void dispc_k2g_ovr_set_plane(struct dispc_device *dispc,
 				    u32 hw_plane, u32 hw_videoport,
 				    u32 x, u32 y, u32 layer)
 {
@@ -1471,10 +1699,97 @@ void dispc_ovr_enable_layer(struct dispc_device *dispc,
 
 	OVR_REG_FLD_MOD(dispc, hw_videoport, DISPC_OVR_ATTRIBUTES(layer),
 			!!enable, DISPC_OVR_ATTRIBUTES_ENABLE_MASK);
 }
 
+/**
+ * dispc_ovr_get_plane - find the overlay layer a plane is routed to
+ * @dispc: the dispc
+ * @hw_plane: the plane to look for
+ * @hw_videoport: returned video port the plane is routed to
+ * @x: returned X position of the plane
+ * @y: returned Y position of the plane
+ * @layer: returned overlay layer index the plane is routed to
+ *
+ * Reads the overlay manager registers to find which video port, at
+ * which position and in which layer, a plane is currently routed to.
+ * The layer index matches the normalized zpos the plane was committed
+ * with, as that is what dispc_ovr_set_plane() uses as the layer index.
+ *
+ * The CHANNELIN field of a disabled layer keeps its stale value, so
+ * only the enabled layers are considered.
+ *
+ * RETURNS:
+ *
+ * 0 on success, -ENODEV if the plane is not routed to any enabled
+ * overlay layer.
+ */
+int dispc_ovr_get_plane(struct dispc_device *dispc, u32 hw_plane,
+			u32 *hw_videoport, int *x, int *y, u32 *layer)
+{
+	u32 vp, l;
+
+	switch (dispc->feat->subrev) {
+	case DISPC_K2G: {
+		u32 val;
+
+		/*
+		 * On K2G there is only one plane and no overlay
+		 * manager: the position is programmed in the VID
+		 * pipeline registers.
+		 */
+		val = dispc_vid_read(dispc, hw_plane, DISPC_VID_K2G_POSITION);
+		*hw_videoport = 0;
+		*layer = 0;
+		*x = val & 0xffff;
+		*y = val >> 16;
+		return 0;
+	}
+	case DISPC_AM625:
+	case DISPC_AM62L:
+	case DISPC_AM65X:
+	case DISPC_J721E:
+		for (vp = 0; vp < dispc->feat->num_vps; vp++) {
+			for (l = 0; l < dispc->feat->num_vids; l++) {
+				u32 attrs = dispc_ovr_read(dispc, vp,
+							   DISPC_OVR_ATTRIBUTES(l));
+
+				if (!(attrs & DISPC_OVR_ATTRIBUTES_ENABLE_MASK))
+					continue;
+
+				if (FIELD_GET(DISPC_OVR_ATTRIBUTES_CHANNELIN_MASK,
+					      attrs) != dispc->feat->vid_info[hw_plane].hw_id)
+					continue;
+
+				*hw_videoport = vp;
+				*layer = l;
+
+				if (dispc->feat->subrev == DISPC_J721E) {
+					u32 attrs2 = dispc_ovr_read(dispc, vp,
+								    DISPC_OVR_ATTRIBUTES2(l));
+
+					*x = FIELD_GET(DISPC_OVR_ATTRIBUTES2_POSX_MASK,
+						       attrs2);
+					*y = FIELD_GET(DISPC_OVR_ATTRIBUTES2_POSY_MASK,
+						       attrs2);
+				} else {
+					*x = FIELD_GET(DISPC_OVR_ATTRIBUTES_POSX_MASK,
+						       attrs);
+					*y = FIELD_GET(DISPC_OVR_ATTRIBUTES_POSY_MASK,
+						       attrs);
+				}
+
+				return 0;
+			}
+		}
+
+		return -ENODEV;
+	default:
+		return -ENODEV;
+	}
+}
+
 /* CSC */
 enum csc_ctm {
 	CSC_RR, CSC_RG, CSC_RB,
 	CSC_GR, CSC_GG, CSC_GB,
 	CSC_BR, CSC_BG, CSC_BB,
@@ -2043,10 +2358,21 @@ static const struct {
 	{ DRM_FORMAT_UYVY, 0x3f, },
 
 	{ DRM_FORMAT_NV12, 0x3d, },
 };
 
+static u32 dispc_plane_find_fourcc_by_dss_code(u8 code)
+{
+	unsigned int i;
+
+	for (i = 0; i < ARRAY_SIZE(dispc_color_formats); ++i)
+		if (dispc_color_formats[i].dss_code == code)
+			return dispc_color_formats[i].fourcc;
+
+	return 0;
+}
+
 static void dispc_plane_set_pixel_format(struct dispc_device *dispc,
 					 u32 hw_plane, u32 fourcc)
 {
 	unsigned int i;
 
@@ -2129,21 +2455,30 @@ void dispc_plane_setup(struct dispc_device *dispc, u32 hw_plane,
 {
 	bool lite = dispc->feat->vid_info[hw_plane].is_lite;
 	u32 fourcc = state->fb->format->format;
 	u16 cpp = state->fb->format->cpp[0];
 	u32 fb_width = state->fb->pitches[0] / cpp;
-	dma_addr_t dma_addr = drm_fb_dma_get_gem_addr(state->fb, state, 0);
 	struct dispc_scaling_params scale;
 
 	dispc_vid_calc_scaling(dispc, state, &scale, lite);
 
 	dispc_plane_set_pixel_format(dispc, hw_plane, fourcc);
 
-	dispc_vid_write(dispc, hw_plane, DISPC_VID_BA_0, dma_addr & 0xffffffff);
-	dispc_vid_write(dispc, hw_plane, DISPC_VID_BA_EXT_0, (u64)dma_addr >> 32);
-	dispc_vid_write(dispc, hw_plane, DISPC_VID_BA_1, dma_addr & 0xffffffff);
-	dispc_vid_write(dispc, hw_plane, DISPC_VID_BA_EXT_1, (u64)dma_addr >> 32);
+	/*
+	 * A framebuffer reconstructed from a hardware state readout has
+	 * no GEM object to derive the DMA address from. The hardware is
+	 * already scanning out that buffer, so keep the address the
+	 * firmware programmed.
+	 */
+	if (state->fb->obj[0]) {
+		dma_addr_t dma_addr = drm_fb_dma_get_gem_addr(state->fb, state, 0);
+
+		dispc_vid_write(dispc, hw_plane, DISPC_VID_BA_0, dma_addr & 0xffffffff);
+		dispc_vid_write(dispc, hw_plane, DISPC_VID_BA_EXT_0, (u64)dma_addr >> 32);
+		dispc_vid_write(dispc, hw_plane, DISPC_VID_BA_1, dma_addr & 0xffffffff);
+		dispc_vid_write(dispc, hw_plane, DISPC_VID_BA_EXT_1, (u64)dma_addr >> 32);
+	}
 
 	dispc_vid_write(dispc, hw_plane, DISPC_VID_PICTURE_SIZE,
 			FIELD_PREP(DISPC_VID_PICTURE_SIZE_MEMSIZEY_MASK, scale.in_h - 1) |
 			FIELD_PREP(DISPC_VID_PICTURE_SIZE_MEMSIZEX_MASK, scale.in_w - 1));
 
@@ -2208,16 +2543,97 @@ void dispc_plane_setup(struct dispc_device *dispc, u32 hw_plane,
 	else
 		VID_REG_FLD_MOD(dispc, hw_plane, DISPC_VID_ATTRIBUTES, 0,
 				DISPC_VID_ATTRIBUTES_PREMULTIPLYALPHA_MASK);
 }
 
+void dispc_plane_get_picture_size(struct dispc_device *dispc, u32 hw_plane,
+				  unsigned int *width, unsigned int *height)
+{
+	u32 val;
+	u16 w, h;
+
+	val = dispc_vid_read(dispc, hw_plane, DISPC_VID_PICTURE_SIZE);
+	w = FIELD_GET(DISPC_VID_PICTURE_SIZE_MEMSIZEX_MASK, val);
+	h = FIELD_GET(DISPC_VID_PICTURE_SIZE_MEMSIZEY_MASK, val);
+
+	*width = w + 1;
+	*height = h + 1;
+}
+
+void dispc_plane_get_size(struct dispc_device *dispc, u32 hw_plane,
+			  unsigned int *width, unsigned int *height)
+{
+	bool lite = dispc->feat->vid_info[hw_plane].is_lite;
+
+	if (!lite) {
+		u32 val = dispc_vid_read(dispc, hw_plane, DISPC_VID_SIZE);
+		*width = FIELD_GET(DISPC_VID_SIZE_SIZEX_MASK, val) + 1;
+		*height = FIELD_GET(DISPC_VID_SIZE_SIZEY_MASK, val) + 1;
+	} else {
+		dispc_plane_get_picture_size(dispc, hw_plane, width, height);
+	}
+}
+
+const struct drm_format_info *
+dispc_plane_get_current_format(struct dispc_device *dispc, u32 hw_plane)
+{
+	const struct drm_format_info *info;
+	u32 fourcc;
+	u32 val;
+
+	val = dispc_vid_read(dispc, hw_plane, DISPC_VID_ATTRIBUTES);
+	fourcc = dispc_plane_find_fourcc_by_dss_code(
+		FIELD_GET(DISPC_VID_ATTRIBUTES_FORMAT_MASK, val));
+	if (!fourcc)
+		return ERR_PTR(-EINVAL);
+
+	info = drm_format_info(fourcc);
+	if (!info)
+		return ERR_PTR(-EINVAL);
+
+	return info;
+}
+
+u8 dispc_plane_get_global_alpha(struct dispc_device *dispc, u32 hw_plane)
+{
+	return FIELD_GET(DISPC_VID_GLOBAL_ALPHA_GLOBALALPHA_MASK,
+			 dispc_vid_read(dispc, hw_plane,
+					DISPC_VID_GLOBAL_ALPHA));
+}
+
+u32 dispc_plane_get_pixel_inc(struct dispc_device *dispc, u32 hw_plane)
+{
+	return dispc_vid_read(dispc, hw_plane, DISPC_VID_PIXEL_INC);
+}
+
+u32 dispc_plane_get_row_inc(struct dispc_device *dispc, u32 hw_plane)
+{
+	return dispc_vid_read(dispc, hw_plane, DISPC_VID_ROW_INC);
+}
+
+u16 dispc_plane_get_blend_mode(struct dispc_device *dispc, u32 hw_plane)
+{
+	u32 val = dispc_vid_read(dispc, hw_plane, DISPC_VID_ATTRIBUTES);
+
+	if (FIELD_GET(DISPC_VID_ATTRIBUTES_PREMULTIPLYALPHA_MASK, val))
+		return DRM_MODE_BLEND_PREMULTI;
+	else
+		return DRM_MODE_BLEND_COVERAGE;
+}
+
 void dispc_plane_enable(struct dispc_device *dispc, u32 hw_plane, bool enable)
 {
 	VID_REG_FLD_MOD(dispc, hw_plane, DISPC_VID_ATTRIBUTES, !!enable,
 			DISPC_VID_ATTRIBUTES_ENABLE_MASK);
 }
 
+bool dispc_plane_is_enabled(struct dispc_device *dispc, u32 hw_plane)
+{
+	return FIELD_GET(DISPC_VID_ATTRIBUTES_ENABLE_MASK,
+			 dispc_vid_read(dispc, hw_plane, DISPC_VID_ATTRIBUTES));
+}
+
 static u32 dispc_vid_get_fifo_size(struct dispc_device *dispc, u32 hw_plane)
 {
 	return VID_REG_GET(dispc, hw_plane, DISPC_VID_BUF_SIZE_STATUS,
 			   DISPC_VID_BUF_SIZE_STATUS_BUFSIZE_MASK);
 }
@@ -2371,15 +2787,19 @@ static void dispc_vp_init(struct dispc_device *dispc)
 	for (i = 0; i < dispc->feat->num_vps; i++) {
 		/* Enable the gamma Shadow bit-field for all VPs*/
 		VP_REG_FLD_MOD(dispc, i, DISPC_VP_CONFIG, 1,
 			       DISPC_VP_CONFIG_GAMMAENABLE_MASK);
 
-		if (dispc->feat->has_vp_control_dpienable) {
-			/* Disable DPIENABLE for all VPs */
+		/*
+		 * Disable DPIENABLE for all VPs, but the ones left
+		 * enabled, by the firmware for example, so that their
+		 * output keeps running and can be read out.
+		 */
+		if (dispc->feat->has_vp_control_dpienable &&
+		    !dispc_vp_is_enabled(dispc, i))
 			VP_REG_FLD_MOD(dispc, i, DISPC_VP_CONTROL, 0,
 				       DISPC_VP_CONTROL_DPIENABLE_MASK);
-		}
 	}
 }
 
 static void dispc_initial_config(struct dispc_device *dispc)
 {
@@ -2706,15 +3126,21 @@ static void dispc_vp_set_color_mgmt(struct dispc_device *dispc,
 		dispc_k2g_vp_set_ctm(dispc, hw_videoport, ctm);
 	else
 		dispc_k3_vp_set_ctm(dispc, hw_videoport, ctm);
 }
 
+void dispc_vp_set_dpi_output(struct dispc_device *dispc, u32 hw_videoport,
+			     bool dpi_output)
+{
+	dispc->vp_data[hw_videoport].dpi_output = dpi_output;
+}
+
 void dispc_vp_setup(struct dispc_device *dispc, u32 hw_videoport,
 		    const struct drm_crtc_state *state, bool newmodeset,
 		    bool dpi_output)
 {
-	dispc->vp_data[hw_videoport].dpi_output = dpi_output;
+	dispc_vp_set_dpi_output(dispc, hw_videoport, dpi_output);
 	dispc_vp_set_default_color(dispc, hw_videoport, 0);
 	dispc_vp_set_color_mgmt(dispc, hw_videoport, state, newmodeset);
 }
 
 int dispc_runtime_suspend(struct dispc_device *dispc)
drivers/gpu/drm/tidss/tidss_dispc.h +22 -0
index dcd4d3b18ef4..4abcb9e679ac 100644
@@ -109,14 +109,17 @@ dispc_irq_t dispc_read_and_clear_irqstatus(struct dispc_device *dispc);
 
 void dispc_ovr_set_plane(struct dispc_device *dispc, u32 hw_plane,
 			 u32 hw_videoport, u32 x, u32 y, u32 layer);
 void dispc_ovr_enable_layer(struct dispc_device *dispc,
 			    u32 hw_videoport, u32 layer, bool enable);
+int dispc_ovr_get_plane(struct dispc_device *dispc, u32 hw_plane,
+			u32 *hw_videoport, int *x, int *y, u32 *layer);
 
 void dispc_vp_prepare(struct dispc_device *dispc, u32 hw_videoport,
 		      const struct drm_crtc_state *state);
 void dispc_vp_enable(struct dispc_device *dispc, u32 hw_videoport);
+bool dispc_vp_is_enabled(struct dispc_device *dispc, u32 hw_videoport);
 void dispc_vp_disable(struct dispc_device *dispc, u32 hw_videoport);
 void dispc_vp_unprepare(struct dispc_device *dispc, u32 hw_videoport);
 bool dispc_vp_go_busy(struct dispc_device *dispc, u32 hw_videoport);
 void dispc_vp_go(struct dispc_device *dispc, u32 hw_videoport);
 int dispc_vp_bus_check(struct dispc_device *dispc, u32 hw_videoport,
@@ -126,24 +129,43 @@ enum drm_mode_status dispc_vp_mode_valid(struct dispc_device *dispc,
 					 const struct drm_display_mode *mode);
 int dispc_vp_enable_clk(struct dispc_device *dispc, u32 hw_videoport);
 void dispc_vp_disable_clk(struct dispc_device *dispc, u32 hw_videoport);
 int dispc_vp_set_clk_rate(struct dispc_device *dispc, u32 hw_videoport,
 			  unsigned long rate);
+void dispc_vp_set_dpi_output(struct dispc_device *dispc, u32 hw_videoport,
+			     bool dpi_output);
 void dispc_vp_setup(struct dispc_device *dispc, u32 hw_videoport,
 		    const struct drm_crtc_state *state, bool newmodeset,
 		    bool dpi_output);
+int dispc_vp_readout_mode(struct dispc_device *dispc,
+			  u32 hw_videoport,
+			  struct drm_display_mode *mode);
+int dispc_vp_get_bus_format(struct dispc_device *dispc, u32 hw_videoport,
+			    u32 *bus_fmt);
+u32 dispc_vp_get_bus_flags(struct dispc_device *dispc, u32 hw_videoport);
 
 int dispc_runtime_suspend(struct dispc_device *dispc);
 int dispc_runtime_resume(struct dispc_device *dispc);
 
 int dispc_plane_check(struct dispc_device *dispc, u32 hw_plane,
 		      const struct drm_plane_state *state,
 		      u32 hw_videoport);
 void dispc_plane_setup(struct dispc_device *dispc, u32 hw_plane,
 		       struct drm_plane_state *state,
 		       u32 hw_videoport);
+void dispc_plane_get_picture_size(struct dispc_device *dispc, u32 hw_plane,
+				  unsigned int *width, unsigned int *height);
+void dispc_plane_get_size(struct dispc_device *dispc, u32 hw_plane,
+			  unsigned int *width, unsigned int *height);
+const struct drm_format_info *
+dispc_plane_get_current_format(struct dispc_device *dispc, u32 hw_plane);
+u8 dispc_plane_get_global_alpha(struct dispc_device *dispc, u32 hw_plane);
+u32 dispc_plane_get_pixel_inc(struct dispc_device *dispc, u32 hw_plane);
+u32 dispc_plane_get_row_inc(struct dispc_device *dispc, u32 hw_plane);
+u16 dispc_plane_get_blend_mode(struct dispc_device *dispc, u32 hw_plane);
 void dispc_plane_enable(struct dispc_device *dispc, u32 hw_plane, bool enable);
+bool dispc_plane_is_enabled(struct dispc_device *dispc, u32 hw_plane);
 const u32 *dispc_plane_formats(struct dispc_device *dispc, unsigned int *len);
 
 int dispc_reset_hw(struct dispc_device *dispc);
 int dispc_init(struct tidss_device *tidss);
 void dispc_remove(struct tidss_device *tidss);
Signature · 1 line
2.56.1
#57 lore raw

Re: [PATCH v9 36/42] drm/tidss: dispc: Add hardware state readout helpers

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [Medium] drm/tidss: dispc readout helpers ordered after callers breaking bisect - [Medium] drm/tidss…

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [Medium] drm/tidss: dispc readout helpers ordered after callers breaking bisect
- [Medium] drm/tidss: bus_flags readout mismatch in dispc_vp_get_bus_flags()
--

commit 0fa7c75e1ac8f9d7d7984b674f13c7d7a5955dd7
Author: Maxime Ripard <[email protected]>

drm/tidss: dispc: Add hardware state readout helpers

This commit adds DISPC register readout helpers for video ports, overlay managers, and planes, and adjusts video port initialization and plane setup to handle pipelines left running by firmware.

Quoted 4 lines
drivers/gpu/drm/tidss/tidss_dispc.c +0 -0
index e186c63ba5fb9..8e61216d4532f 100644

[ ... ]

Quoted 1 line
drivers/gpu/drm/tidss/tidss_dispc.c +0 -0
@@ -1401,6 +1407,228 @@ int dispc_vp_set_clk_rate(struct dispc_device *dispc, u32 hw_videoport,

[ ... ]

Quoted 13 lines
drivers/gpu/drm/tidss/tidss_dispc.c +13 -0
+u32 dispc_vp_get_bus_flags(struct dispc_device *dispc, u32 hw_videoport)
+{
+	u32 flags = 0;
+	u32 val;
+
+	val = dispc_vp_read(dispc, hw_videoport, DISPC_VP_POL_FREQ);
+	if (FIELD_GET(DISPC_VP_POL_FREQ_IPC_MASK, val))
+		flags |= DRM_BUS_FLAG_PIXDATA_DRIVE_NEGEDGE;
+	else
+		flags |= DRM_BUS_FLAG_PIXDATA_DRIVE_POSEDGE;
+
+	if (FIELD_GET(DISPC_VP_POL_FREQ_IEO_MASK, val))
+		flags |= DRM_BUS_FLAG_DE_LOW;

drivers/gpu/drm/tidss/tidss_dispc.c int dispc_vp_set_clk_rate(struct dispc_device *dispc, u32 hw_videoport, · line 1630

Quoted 10 lines
drivers/gpu/drm/tidss/tidss_dispc.c +10 -0
+	else
+		flags |= DRM_BUS_FLAG_DE_HIGH;
+
+	if (FIELD_GET(DISPC_VP_POL_FREQ_RF_MASK, val))
+		flags |= DRM_BUS_FLAG_SYNC_DRIVE_POSEDGE;
+	else
+		flags |= DRM_BUS_FLAG_SYNC_DRIVE_NEGEDGE;
+
+	return flags;
+}

[Severity: Medium]
Can dispc_vp_get_bus_flags() cause state comparison mismatches in tidss_crtc_compare_state() and drm_atomic_helper_bridge_compare_state() when a downstream bridge or panel leaves any of the three polarity flags unspecified?

tidss_bridge_atomic_check() copies next_bridge_state->input_bus_cfg.flags (or connector->display_info.bus_flags) directly into tcrtc_state->bus_flags and propagates it to the tidss encoder bridge state without filling in default polarities, while dispc_vp_prepare() programs default polarities in DISPC_VP_POL_FREQ for any unset flags. Because dispc_vp_get_bus_flags() unconditionally sets all three polarity flags, the read-out bus_flags on the CRTC and tidss encoder bridge will not match the committed state whenever a panel or bridge omits one of those flags (for example, panels that only specify DRM_BUS_FLAG_DE_HIGH, or tidss_oldi when DE polarity is left unset).

[ ... ]

Quoted 42 lines
drivers/gpu/drm/tidss/tidss_dispc.h +13 -0
index dcd4d3b18ef48..4abcb9e679acf 100644
@@ -111,10 +111,13 @@ void dispc_ovr_set_plane(struct dispc_device *dispc, u32 hw_plane,
 			 u32 hw_videoport, u32 x, u32 y, u32 layer);
 void dispc_ovr_enable_layer(struct dispc_device *dispc,
 			    u32 hw_videoport, u32 layer, bool enable);
+int dispc_ovr_get_plane(struct dispc_device *dispc, u32 hw_plane,
+			u32 *hw_videoport, int *x, int *y, u32 *layer);
 
 void dispc_vp_prepare(struct dispc_device *dispc, u32 hw_videoport,
 		      const struct drm_crtc_state *state);
 void dispc_vp_enable(struct dispc_device *dispc, u32 hw_videoport);
+bool dispc_vp_is_enabled(struct dispc_device *dispc, u32 hw_videoport);
 void dispc_vp_disable(struct dispc_device *dispc, u32 hw_videoport);
 void dispc_vp_unprepare(struct dispc_device *dispc, u32 hw_videoport);
 bool dispc_vp_go_busy(struct dispc_device *dispc, u32 hw_videoport);
@@ -128,9 +131,17 @@ int dispc_vp_enable_clk(struct dispc_device *dispc, u32 hw_videoport);
 void dispc_vp_disable_clk(struct dispc_device *dispc, u32 hw_videoport);
 int dispc_vp_set_clk_rate(struct dispc_device *dispc, u32 hw_videoport,
 			  unsigned long rate);
+void dispc_vp_set_dpi_output(struct dispc_device *dispc, u32 hw_videoport,
+			     bool dpi_output);
 void dispc_vp_setup(struct dispc_device *dispc, u32 hw_videoport,
 		    const struct drm_crtc_state *state, bool newmodeset,
 		    bool dpi_output);
+int dispc_vp_readout_mode(struct dispc_device *dispc,
+			  u32 hw_videoport,
+			  struct drm_display_mode *mode);
+int dispc_vp_get_bus_format(struct dispc_device *dispc, u32 hw_videoport,
+			    u32 *bus_fmt);
+u32 dispc_vp_get_bus_flags(struct dispc_device *dispc, u32 hw_videoport);
 
 int dispc_runtime_suspend(struct dispc_device *dispc);
 int dispc_runtime_resume(struct dispc_device *dispc);
@@ -141,7 +152,18 @@ int dispc_plane_check(struct dispc_device *dispc, u32 hw_plane,
 void dispc_plane_setup(struct dispc_device *dispc, u32 hw_plane,
 		       struct drm_plane_state *state,
 		       u32 hw_videoport);
+void dispc_plane_get_picture_size(struct dispc_device *dispc, u32 hw_plane,
+				  unsigned int *width, unsigned int *height);

drivers/gpu/drm/tidss/tidss_dispc.h int dispc_plane_check(struct dispc_device *dispc, u32 hw_plane, · line 166

Quoted 10 lines
drivers/gpu/drm/tidss/tidss_dispc.h +9 -0
+void dispc_plane_get_size(struct dispc_device *dispc, u32 hw_plane,
+			  unsigned int *width, unsigned int *height);
+const struct drm_format_info *
+dispc_plane_get_current_format(struct dispc_device *dispc, u32 hw_plane);
+u8 dispc_plane_get_global_alpha(struct dispc_device *dispc, u32 hw_plane);
+u32 dispc_plane_get_pixel_inc(struct dispc_device *dispc, u32 hw_plane);
+u32 dispc_plane_get_row_inc(struct dispc_device *dispc, u32 hw_plane);
+u16 dispc_plane_get_blend_mode(struct dispc_device *dispc, u32 hw_plane);
 void dispc_plane_enable(struct dispc_device *dispc, u32 hw_plane, bool enable);
+bool dispc_plane_is_enabled(struct dispc_device *dispc, u32 hw_plane);

[Severity: Medium]
Should this patch be ordered before commit "drm/tidss: crtc: Implement hardware state readout"?

The earlier commits "drm/tidss: crtc: Implement hardware state readout" and "drm/tidss: plane: Implement hardware state readout" call these helpers in tidss_crtc.c and tidss_plane.c before they are declared and defined here, which breaks the build with CONFIG_DRM_TIDSS enabled at four intermediate commits in the series during git bisect. (Commit "drm/tidss: kms: Hook up hardware state readout" also appears to be ordered before commit "drm/tidss: plane: Implement hardware state readout".)

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=36
author maintainer
#58 lore raw

PATCH v9 37/42 drm/tidss: encoder: Implement atomic_sro_get_current_crtc

In reply to Maxime Ripard

The tidss driver doesn't really implement anything with encoders, it just relies on simple encoders, bridges and drm_bridge_connector.

In order to figure out the CRTC -> connector association from the hardware state, we do need encoder support though, through the get_current_crtc callback.

Since the tidss encoders are always connected to a single CRTC, we don't really need to read the hardware state though, we can simply return the one we know we are always connected to.

---
 drivers/gpu/drm/tidss/tidss_encoder.c | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
drivers/gpu/drm/tidss/tidss_encoder.c +15 -0
index 5034e601ac86..1ca054a3cd87 100644
@@ -121,11 +121,26 @@ static const struct drm_bridge_funcs tidss_bridge_funcs = {
 	.atomic_destroy_state		= drm_atomic_helper_bridge_destroy_state,
 	.atomic_sro_readout_state	= tidss_readout_state,
 	.atomic_sro_compare_state	= drm_atomic_helper_bridge_compare_state,
 };
 
+static struct drm_crtc *tidss_encoder_get_current_crtc(struct drm_encoder *encoder)
+{
+	struct drm_crtc *crtc;
+
+	WARN_ON(hweight32(encoder->possible_crtcs) > 1);
+
+	drm_for_each_crtc(crtc, encoder->dev) {
+		if (encoder->possible_crtcs == (1 << drm_crtc_index(crtc)))
+			return crtc;
+	}
+
+	return NULL;
+}
+
 static const struct drm_encoder_funcs tidss_encoder_funcs = {
+	.atomic_sro_get_current_crtc = tidss_encoder_get_current_crtc,
 	.destroy = drm_encoder_cleanup,
 };
 
 int tidss_encoder_create(struct tidss_device *tidss,
 			 struct drm_bridge *next_bridge,
Signature · 1 line
2.56.1
author maintainer
#59 lore raw

PATCH v9 38/42 drm/bridge: sii902x: Implement hw state readout

In reply to Maxime Ripard

Implement the hardware state readout for the sii902x bridge now that all the infrastructure is in place.

---
 drivers/gpu/drm/bridge/sii902x.c | 130 ++++++++++++++++++++++++++++++++++++++-
 1 file changed, 129 insertions(+), 1 deletion(-)
drivers/gpu/drm/bridge/sii902x.c +129 -1
index 6eaf70aeb0e3..4fb639e72c7e 100644
@@ -20,10 +20,12 @@
 #include <linux/regmap.h>
 #include <linux/regulator/consumer.h>
 #include <linux/clk.h>
 
 #include <drm/drm_atomic_helper.h>
+#include <drm/drm_atomic_state_readout.h>
+#include <drm/drm_atomic_state_readout_helper.h>
 #include <drm/drm_bridge.h>
 #include <drm/drm_drv.h>
 #include <drm/drm_edid.h>
 #include <drm/drm_print.h>
 #include <drm/drm_probe_helper.h>
@@ -535,20 +537,145 @@ sii902x_bridge_mode_valid(struct drm_bridge *bridge,
 		return MODE_CLOCK_HIGH;
 
 	return MODE_OK;
 }
 
+static int
+sii902x_bridge_connector_readout(struct drm_bridge *bridge,
+				 struct drm_atomic_sro_state *state,
+				 struct drm_connector_state *conn_state)
+{
+	struct sii902x *sii902x = bridge_to_sii902x(bridge);
+	struct drm_connector *connector = conn_state->connector;
+	struct drm_crtc_state *crtc_state;
+	struct drm_encoder *encoder;
+	struct drm_crtc *crtc;
+	int ret;
+
+	mutex_lock(&sii902x->mutex);
+
+	ret = regmap_test_bits(sii902x->regmap, SII902X_SYS_CTRL_DATA, SII902X_SYS_CTRL_PWR_DWN);
+	/* I2C error */
+	if (ret < 0)
+		goto err_mutex_unlock;
+	/* Device is powered down, nothing to read out */
+	if (ret)
+		goto out;
+
+	encoder = bridge->encoder;
+	if (!encoder->funcs || !encoder->funcs->atomic_sro_get_current_crtc) {
+		ret = -ENODEV;
+		goto err_mutex_unlock;
+	}
+
+	crtc = encoder->funcs->atomic_sro_get_current_crtc(encoder);
+	if (!crtc) {
+		ret = -ENODEV;
+		goto err_mutex_unlock;
+	}
+
+	crtc_state = drm_atomic_sro_get_crtc_state(state, crtc);
+	if (!crtc_state) {
+		ret = -ENODEV;
+		goto err_mutex_unlock;
+	}
+
+	crtc_state->encoder_mask |= drm_encoder_mask(encoder);
+	crtc_state->connector_mask |= drm_connector_mask(connector);
+
+	/*
+	 * drm_atomic_set_crtc_for_connector() takes a connector reference
+	 * when setting the crtc pointer, which is later released by
+	 * __drm_atomic_helper_connector_destroy_state(). SRO readout
+	 * bypasses that path, so take the reference explicitly.
+	 */
+	drm_connector_get(connector);
+	conn_state->crtc = crtc;
+	conn_state->best_encoder = encoder;
+
+out:
+	mutex_unlock(&sii902x->mutex);
+	return 0;
+
+err_mutex_unlock:
+	mutex_unlock(&sii902x->mutex);
+	return ret;
+}
+
+static int sii902x_bridge_readout_state(struct drm_bridge *bridge,
+					struct drm_atomic_sro_state *state,
+					struct drm_bridge_state *bridge_state,
+					struct drm_crtc_state *crtc_state,
+					struct drm_connector_state *conn_state)
+{
+	struct sii902x *sii902x = bridge_to_sii902x(bridge);
+	u32 input_format;
+	int ret;
+
+	mutex_lock(&sii902x->mutex);
+
+	ret = regmap_test_bits(sii902x->regmap, SII902X_SYS_CTRL_DATA, SII902X_SYS_CTRL_PWR_DWN);
+	/* I2C error */
+	if (ret < 0)
+		goto err_mutex_unlock;
+	/* Device is powered down, nothing to read out */
+	if (ret)
+		goto out;
+
+	switch (sii902x->bus_width) {
+	case 16:
+		input_format = MEDIA_BUS_FMT_RGB565_1X16;
+		break;
+	case 18:
+		input_format = MEDIA_BUS_FMT_RGB666_1X18;
+		break;
+	case 24:
+		input_format = MEDIA_BUS_FMT_RGB888_1X24;
+		break;
+	default:
+		ret = -EINVAL;
+		goto err_mutex_unlock;
+	}
+
+	bridge_state->input_bus_cfg.format = input_format;
+	bridge_state->input_bus_cfg.flags = bridge->timings->input_bus_flags;
+
+	/* The sii902x is a passthrough transmitter, output format equals input. */
+	bridge_state->output_bus_cfg.format = input_format;
+
+	/*
+	 * The output flags are those the next element of the chain
+	 * expects, which aren't available yet during the readout. They
+	 * are the connector display info flags when the elements after
+	 * the transmitter forward them unmodified, as the atomic check
+	 * assumes.
+	 */
+	bridge_state->output_bus_cfg.flags =
+		conn_state->connector->display_info.bus_flags;
+
+out:
+	mutex_unlock(&sii902x->mutex);
+	return 0;
+
+err_mutex_unlock:
+	mutex_unlock(&sii902x->mutex);
+	return ret;
+}
+
 static const struct drm_bridge_funcs sii902x_bridge_funcs = {
 	.attach = sii902x_bridge_attach,
 	.mode_set = sii902x_bridge_mode_set,
 	.atomic_disable = sii902x_bridge_atomic_disable,
 	.atomic_enable = sii902x_bridge_atomic_enable,
 	.detect = sii902x_bridge_detect,
 	.edid_read = sii902x_bridge_edid_read,
 	.atomic_create_state = drm_atomic_helper_bridge_create_state,
 	.atomic_duplicate_state = drm_atomic_helper_bridge_duplicate_state,
 	.atomic_destroy_state = drm_atomic_helper_bridge_destroy_state,
+	.atomic_sro_compare_state = drm_atomic_helper_bridge_compare_state,
+	.atomic_sro_readout_state = sii902x_bridge_readout_state,
+	.atomic_sro_connector_readout = sii902x_bridge_connector_readout,
 	.atomic_get_input_bus_fmts = sii902x_bridge_atomic_get_input_bus_fmts,
 	.atomic_check = sii902x_bridge_atomic_check,
 	.mode_valid = sii902x_bridge_mode_valid,
 };
 
@@ -1139,11 +1266,12 @@ static int sii902x_init(struct sii902x *sii902x)
 	if (ret)
 		goto err_unreg_audio;
 
 	sii902x->bridge.of_node = dev->of_node;
 	sii902x->bridge.timings = &default_sii902x_timings;
-	sii902x->bridge.ops = DRM_BRIDGE_OP_DETECT | DRM_BRIDGE_OP_EDID;
+	sii902x->bridge.ops = DRM_BRIDGE_OP_DETECT | DRM_BRIDGE_OP_EDID |
+			      DRM_BRIDGE_OP_CONNECTOR_HW_READOUT;
 	sii902x->bridge.type = DRM_MODE_CONNECTOR_HDMIA;
 
 	if (sii902x->i2c->irq > 0)
 		sii902x->bridge.ops |= DRM_BRIDGE_OP_HPD;
 
Signature · 1 line
2.56.1
#60 lore raw

Re: [PATCH v9 38/42] drm/bridge: sii902x: Implement hw state readout

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] drm/bridge: sii902x: unconditional reset in sii902x_init() wipes hw state --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] drm/bridge: sii902x: unconditional reset in sii902x_init() wipes hw state
--

commit 863c5900a85de3bb77ecf66aba575b8561a6f743
Author: Maxime Ripard <[email protected]>

drm/bridge: sii902x: Implement hw state readout

This commit implements atomic state readout callbacks for the sii902x bridge and connector and enables DRM_BRIDGE_OP_CONNECTOR_HW_READOUT.

Quoted 4 lines
drivers/gpu/drm/bridge/sii902x.c +0 -0
index 6eaf70aeb0e37..4fb639e72c7e7 100644

[ ... ]

Quoted 15 lines
drivers/gpu/drm/bridge/sii902x.c +11 -0
@@ -537,6 +539,128 @@ sii902x_bridge_mode_valid(struct drm_bridge *bridge,
 	return MODE_OK;
 }
 
+static int
+sii902x_bridge_connector_readout(struct drm_bridge *bridge,
+				 struct drm_atomic_sro_state *state,
+				 struct drm_connector_state *conn_state)
+{
+	struct sii902x *sii902x = bridge_to_sii902x(bridge);
+	struct drm_connector *connector = conn_state->connector;
+	struct drm_crtc_state *crtc_state;
+	struct drm_encoder *encoder;
+	struct drm_crtc *crtc;
+	int ret;

drivers/gpu/drm/bridge/sii902x.c sii902x_bridge_mode_valid(struct drm_bridge *bridge, · line 562

Quoted 10 lines
drivers/gpu/drm/bridge/sii902x.c +10 -0
+
+	mutex_lock(&sii902x->mutex);
+
+	ret = regmap_test_bits(sii902x->regmap, SII902X_SYS_CTRL_DATA, SII902X_SYS_CTRL_PWR_DWN);
+	/* I2C error */
+	if (ret < 0)
+		goto err_mutex_unlock;
+	/* Device is powered down, nothing to read out */
+	if (ret)
+		goto out;

[Severity: Medium]
Can SII902X_SYS_CTRL_PWR_DWN ever be clear at readout time when reset-gpios is provided in the device tree?

During probe, sii902x_init() unconditionally calls sii902x_reset() before registering the bridge:

drivers/gpu/drm/bridge/sii902x.c:sii902x_reset() {
	if (!sii902x->reset_gpio)
		return;

	gpiod_set_value_cansleep(sii902x->reset_gpio, 1);

	/* The datasheet says treset-min = 100us. Make it 150us to be sure. */
	usleep_range(150, 200);

	gpiod_set_value_cansleep(sii902x->reset_gpio, 0);
}

Pulsing reset_gpio restores all SiI9022 registers to their power-down defaults (SII902X_SYS_CTRL_PWR_DWN = 1). When the KMS driver later runs hardware state readout, both sii902x_bridge_connector_readout() and sii902x_bridge_readout_state() see SII902X_SYS_CTRL_PWR_DWN set and treat the bridge as powered down, leaving conn_state->crtc NULL and crtc_state->encoder_mask and connector_mask clear on a firmware-enabled display pipeline.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=38
author maintainer
#61 lore raw

PATCH v9 39/42 drm/panel: Implement state readout support

In reply to Maxime Ripard

A panel bridge has no state of its own to read out of the hardware:
neither the panel enable state nor the bus configuration is observable through it.

The bus configuration the atomic helpers negotiate can however be
reconstructed: the panel bridge is always the last bridge of a chain, so the negotiation picks the first connector display info format as its output format, or MEDIA_BUS_FMT_FIXED when the display info has none, passes it through to its input end, and propagates the display info flags to both ends. The atomic_sro_readout_state hook reports that configuration. The display info is only populated once the connector modes have been read, after the initial state has been created: the fixed format reported then matches what the negotiation would produce, and is harmless as the initial state is never compared.

The atomic_sro_connector_readout hook reports the connector as bound to the current CRTC of its encoder whenever that CRTC was read out as active, the panel being assumed enabled whenever its pipeline is. A panel bridge only carries DRM_BRIDGE_OP_CONNECTOR_HW_READOUT when no bridge attached earlier in its chain provides a connector readout:
drm_bridge_connector_init() picks the last bridge carrying that op, and earlier bridges may know the actual link state.

---
 drivers/gpu/drm/drm_panel.c | 129 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 129 insertions(+)
drivers/gpu/drm/drm_panel.c +129 -0
index 560ea57591bd..9c60942de90b 100644
@@ -7,14 +7,17 @@
 
 #include <linux/backlight.h>
 #include <linux/debugfs.h>
 #include <linux/err.h>
 #include <linux/export.h>
+#include <linux/media-bus-format.h>
 #include <linux/module.h>
 #include <linux/of.h>
 
 #include <drm/drm_atomic_helper.h>
+#include <drm/drm_atomic_state_readout.h>
+#include <drm/drm_atomic_state_readout_helper.h>
 #include <drm/drm_bridge.h>
 #include <drm/drm_connector.h>
 #include <drm/drm_crtc.h>
 #include <drm/drm_encoder.h>
 #include <drm/drm_managed.h>
@@ -81,12 +84,35 @@ static int panel_bridge_attach(struct drm_bridge *bridge,
 			       struct drm_encoder *encoder,
 			       enum drm_bridge_attach_flags flags)
 {
 	struct drm_panel *panel = drm_bridge_to_panel(bridge);
 	struct drm_connector *connector = &panel->connector;
+	bool upstream_readout = false;
 	int ret;
 
+	/*
+	 * drm_bridge_connector_init() picks the connector readout
+	 * provider from the last bridge of the chain carrying
+	 * DRM_BRIDGE_OP_CONNECTOR_HW_READOUT. Only provide the
+	 * fallback readout of a panel bridge when no bridge earlier
+	 * in the chain provides one, as those know the actual link
+	 * state while a panel bridge can only assume the panel is
+	 * enabled.
+	 */
+	drm_for_each_bridge_in_chain(encoder, iter) {
+		if (iter == bridge)
+			break;
+
+		if (iter->ops & DRM_BRIDGE_OP_CONNECTOR_HW_READOUT) {
+			upstream_readout = true;
+			break;
+		}
+	}
+
+	if (!upstream_readout)
+		bridge->ops |= DRM_BRIDGE_OP_CONNECTOR_HW_READOUT;
+
 	if (flags & DRM_BRIDGE_ATTACH_NO_CONNECTOR)
 		return 0;
 
 	drm_connector_helper_add(connector,
 				 &panel_bridge_connector_helper_funcs);
@@ -224,10 +250,110 @@ static void panel_bridge_debugfs_init(struct drm_bridge *bridge,
 	root = debugfs_create_dir("panel", root);
 	if (panel->funcs->debugfs_init)
 		panel->funcs->debugfs_init(panel, root);
 }
 
+/*
+ * A panel bridge has no state of its own to read out of the hardware:
+ * reconstruct the bus configuration the atomic helpers negotiate
+ * instead.
+ *
+ * The panel bridge is always the last bridge of a chain, and the
+ * negotiation picks the first connector display info format as its
+ * output format (or MEDIA_BUS_FMT_FIXED when the display info has
+ * none), which drm_atomic_helper_bridge_propagate_bus_fmt() passes
+ * through to its input. drm_atomic_bridge_propagate_bus_flags()
+ * similarly propagates the display info flags to both its output and
+ * input. This is also what happens when the display info has no
+ * formats yet, like during the initial state creation at probe time
+ * before any mode has been read: the bus configuration then reports
+ * a fixed format, which is harmless as the initial state is never
+ * compared, and every commit negotiates the bus configuration anew.
+ *
+ * The format the negotiation selects can only differ from the first
+ * display info format when the connector reports several formats and
+ * the rest of the bridge chain rejects it, which panel connectors
+ * don't do.
+ */
+static int
+panel_bridge_readout_state(struct drm_bridge *bridge,
+			   struct drm_atomic_sro_state *state,
+			   struct drm_bridge_state *bridge_state,
+			   struct drm_crtc_state *crtc_state,
+			   struct drm_connector_state *conn_state)
+{
+	struct drm_connector *connector = conn_state->connector;
+	struct drm_display_info *info = &connector->display_info;
+	u32 format;
+
+	if (info->num_bus_formats)
+		format = info->bus_formats[0];
+	else
+		format = MEDIA_BUS_FMT_FIXED;
+
+	bridge_state->input_bus_cfg.format = format;
+	bridge_state->output_bus_cfg.format = format;
+
+	bridge_state->input_bus_cfg.flags = info->bus_flags;
+	bridge_state->output_bus_cfg.flags = info->bus_flags;
+
+	return 0;
+}
+
+/*
+ * A panel bridge has no way to read the panel state out of the
+ * hardware: assume the panel is enabled whenever its pipeline is.
+ */
+static int
+panel_bridge_connector_readout(struct drm_bridge *bridge,
+			       struct drm_atomic_sro_state *state,
+			       struct drm_connector_state *conn_state)
+{
+	struct drm_connector *connector = conn_state->connector;
+	struct drm_crtc_state *crtc_state;
+	struct drm_encoder *encoder;
+	struct drm_crtc *crtc;
+
+	encoder = bridge->encoder;
+	if (!encoder->funcs || !encoder->funcs->atomic_sro_get_current_crtc)
+		return -ENODEV;
+
+	/* The encoder is disabled, there is nothing to read out. */
+	crtc = encoder->funcs->atomic_sro_get_current_crtc(encoder);
+	if (!crtc)
+		return 0;
+
+	/*
+	 * A readout limited to the objects of a commit doesn't have a
+	 * state for a CRTC that isn't part of it, and a CRTC can't be
+	 * left out of a commit enabling it.
+	 */
+	crtc_state = drm_atomic_sro_get_crtc_state(state, crtc);
+	if (!crtc_state)
+		return 0;
+
+	/* The pipeline is off, there is nothing to read out. */
+	if (!crtc_state->active)
+		return 0;
+
+	crtc_state->encoder_mask |= drm_encoder_mask(encoder);
+	crtc_state->connector_mask |= drm_connector_mask(connector);
+
+	/*
+	 * drm_atomic_set_crtc_for_connector() takes a connector
+	 * reference when setting the crtc pointer, which is later
+	 * released by __drm_atomic_helper_connector_destroy_state().
+	 * The readout bypasses that path, so take the reference
+	 * explicitly.
+	 */
+	drm_connector_get(connector);
+	conn_state->crtc = crtc;
+	conn_state->best_encoder = encoder;
+
+	return 0;
+}
+
 static const struct drm_bridge_funcs panel_bridge_bridge_funcs = {
 	.attach = panel_bridge_attach,
 	.detach = panel_bridge_detach,
 	.atomic_pre_enable = panel_bridge_atomic_pre_enable,
 	.atomic_enable = panel_bridge_atomic_enable,
@@ -237,10 +363,13 @@ static const struct drm_bridge_funcs panel_bridge_bridge_funcs = {
 	.atomic_create_state = drm_atomic_helper_bridge_create_state,
 	.atomic_duplicate_state = drm_atomic_helper_bridge_duplicate_state,
 	.atomic_destroy_state = drm_atomic_helper_bridge_destroy_state,
 	.atomic_get_input_bus_fmts = drm_atomic_helper_bridge_propagate_bus_fmt,
 	.debugfs_init = panel_bridge_debugfs_init,
+	.atomic_sro_readout_state = panel_bridge_readout_state,
+	.atomic_sro_compare_state = drm_atomic_helper_bridge_compare_state,
+	.atomic_sro_connector_readout = panel_bridge_connector_readout,
 };
 
 /**
  * drm_bridge_is_panel - Checks if a drm_bridge is a panel_bridge.
  *
Signature · 1 line
2.56.1
author maintainer
#62 lore raw

PATCH v9 40/42 fixup! drm/tidss: dispc: Add hardware state readout helpers

In reply to Maxime Ripard

The state readout of the OLDI transmitters needs to know how the firmware configured them, but the dispc helpers only allow to program the OLDI configuration register of a video port with tidss_configure_oldi() and to clear it with tidss_disable_oldi().

Add tidss_get_oldi_config() to read that register back.

This is only needed by the OLDI state readout, which hasn't been tested yet, and should be folded into the dispc readout helpers if that one gets merged.

---
 drivers/gpu/drm/tidss/tidss_dispc.c | 5 +++++
 drivers/gpu/drm/tidss/tidss_dispc.h | 1 +
 2 files changed, 6 insertions(+)
drivers/gpu/drm/tidss/tidss_dispc.c +5 -0
index 8e61216d4532..f7309daec908 100644
@@ -495,10 +495,15 @@ int tidss_configure_oldi(struct tidss_device *tidss, u32 hw_videoport,
 void tidss_disable_oldi(struct tidss_device *tidss, u32 hw_videoport)
 {
 	dispc_vp_write(tidss->dispc, hw_videoport, DISPC_VP_DSS_OLDI_CFG, 0);
 }
 
+u32 tidss_get_oldi_config(struct tidss_device *tidss, u32 hw_videoport)
+{
+	return dispc_vp_read(tidss->dispc, hw_videoport, DISPC_VP_DSS_OLDI_CFG);
+}
+
 /*
  * TRM gives bitfields as start:end, where start is the higher bit
  * number. For example 7:0
  */
 
drivers/gpu/drm/tidss/tidss_dispc.h +1 -0
index 4abcb9e679ac..c54ab9a92894 100644
@@ -100,10 +100,11 @@ extern const struct dispc_features dispc_am65x_feats;
 extern const struct dispc_features dispc_j721e_feats;
 
 int tidss_configure_oldi(struct tidss_device *tidss, u32 hw_videoport,
 			 u32 oldi_cfg);
 void tidss_disable_oldi(struct tidss_device *tidss, u32 hw_videoport);
+u32 tidss_get_oldi_config(struct tidss_device *tidss, u32 hw_videoport);
 unsigned int dispc_pclk_diff(unsigned long rate, unsigned long real_rate);
 
 void dispc_set_irqenable(struct dispc_device *dispc, dispc_irq_t mask);
 dispc_irq_t dispc_read_and_clear_irqstatus(struct dispc_device *dispc);
 
Signature · 1 line
2.56.1
author maintainer
#63 lore raw

PATCH v9 41/42 drm/tidss: oldi: Implement hardware state readout

In reply to Maxime Ripard

On the AM65x and the AM625, an OLDI transmitter sits between a video port and the panel or the bridge it drives. Without a readout of the transmitter, the bridge chain of a pipeline the firmware left running has no state for it, and the first commit can't be matched against what is programmed.

Implement the state readout hooks of the OLDI bridge. The atomic_sro_readout_state hook reads the transmitter configuration register through tidss_get_oldi_config(), and does nothing if the transmitter is disabled. Otherwise, it looks up the bus format the LVDS mapping and the width flag correspond to, since the most significant bit of the mapping field carries the dual-link flag rather than the mapping, and reconstructs the bus configuration the atomic helpers
negotiate: the transmitter takes the parallel RGB output of the video port and serializes it. The DE polarity is the only flag the transmitter stores, the sampling edges being hardcoded. The readout fails when the mapping isn't recognized, so that the core falls back to the regular initialization.

The atomic_sro_connector_readout hook binds the connector to the CRTC the encoder is currently connected to, and takes the connector reference that drm_atomic_set_crtc_for_connector() would have taken. Set DRM_BRIDGE_OP_CONNECTOR_HW_READOUT so that drm_bridge_connector uses it.

Use drm_atomic_helper_bridge_compare_state() to compare the readout state with the committed one.

This hasn't been tested yet.

---
 drivers/gpu/drm/tidss/tidss_oldi.c | 157 +++++++++++++++++++++++++++++++++++++
 1 file changed, 157 insertions(+)
drivers/gpu/drm/tidss/tidss_oldi.c +157 -0
index f767b0c115ac..0ad4ef34ba64 100644
@@ -3,20 +3,25 @@
  * Copyright (C) 2025 - Texas Instruments Incorporated
  *
  * Aradhya Bhatia <[email protected]>
  */
 
+#include <linux/bitfield.h>
 #include <linux/clk.h>
 #include <linux/of.h>
 #include <linux/of_graph.h>
 #include <linux/mfd/syscon.h>
 #include <linux/media-bus-format.h>
 #include <linux/regmap.h>
 
 #include <drm/drm_atomic_helper.h>
+#include <drm/drm_atomic_state_readout.h>
+#include <drm/drm_atomic_state_readout_helper.h>
 #include <drm/drm_bridge.h>
+#include <drm/drm_connector.h>
 #include <drm/drm_of.h>
+#include <drm/drm_panel.h>
 
 #include "tidss_dispc.h"
 #include "tidss_dispc_regs.h"
 #include "tidss_oldi.h"
 
@@ -348,20 +353,171 @@ tidss_oldi_mode_valid(struct drm_bridge *bridge,
 		return MODE_CLOCK_RANGE;
 
 	return MODE_OK;
 }
 
+static u32 tidss_oldi_read_config(struct tidss_oldi *oldi)
+{
+	u32 cfg;
+
+	tidss_runtime_get(oldi->tidss);
+	cfg = tidss_get_oldi_config(oldi->tidss, oldi->parent_vp);
+	tidss_runtime_put(oldi->tidss);
+
+	return cfg;
+}
+
+static int
+tidss_oldi_bridge_readout_state(struct drm_bridge *bridge,
+				struct drm_atomic_sro_state *state,
+				struct drm_bridge_state *bridge_state,
+				struct drm_crtc_state *crtc_state,
+				struct drm_connector_state *conn_state)
+{
+	struct tidss_oldi *oldi = drm_bridge_to_tidss_oldi(bridge);
+	const struct oldi_bus_format *bus_fmt = NULL;
+	unsigned int i;
+	u32 cfg, map, flags;
+
+	cfg = tidss_oldi_read_config(oldi);
+
+	/* The transmitter is off, there is nothing to read out. */
+	if (!(cfg & OLDI_ENABLE))
+		return 0;
+
+	/*
+	 * The most significant bit of the data mapping field carries
+	 * the dual-link flag rather than the LVDS mapping: mask it out
+	 * before looking for the bus format the mapping corresponds
+	 * to.
+	 */
+	map = cfg & (OLDI_MAP & ~BIT(3));
+
+	for (i = 0; i < ARRAY_SIZE(oldi_bus_formats); i++) {
+		if (oldi_bus_formats[i].oldi_mode_reg_val << 1 != map)
+			continue;
+
+		if ((cfg & OLDI_MSB) ?
+		    oldi_bus_formats[i].data_width != 24 :
+		    oldi_bus_formats[i].data_width != 18)
+			continue;
+
+		bus_fmt = &oldi_bus_formats[i];
+		break;
+	}
+
+	if (!bus_fmt) {
+		dev_err(oldi->dev,
+			"OLDI%u: unrecognised mapping in the config register\n",
+			oldi->oldi_instance);
+		return -EINVAL;
+	}
+
+	/*
+	 * Reconstruct the bus configuration the atomic helpers
+	 * negotiate: the OLDI transmitter takes the parallel RGB
+	 * output of the video port and serialises it with the LVDS
+	 * mapping found in the config register.
+	 */
+	bridge_state->input_bus_cfg.format = bus_fmt->input_bus_fmt;
+	bridge_state->output_bus_cfg.format = bus_fmt->bus_fmt;
+
+	/*
+	 * The transmitter only stores the DE polarity: OLDI_DEPOL is
+	 * set when the DE is active low, and the sampling edges are
+	 * hardcoded, the transmitter cannot configure them. TODO:
+	 * read them back from the hardware registers if a variant
+	 * makes them configurable.
+	 *
+	 * Everything else in the bus flags can't be read back, but the
+	 * atomic check leaves it untouched: the output flags are those
+	 * the next element of the chain expects, which are the display
+	 * info flags when the elements after the transmitter forward
+	 * them unmodified, and tidss_oldi_atomic_check() only replaces
+	 * the sampling edges of the input flags. Start from the display
+	 * info flags and only reconcile the DE polarity with what the
+	 * hardware reports, so that a DE polarity left unspecified is
+	 * not reported as an explicit one.
+	 */
+	flags = conn_state->connector->display_info.bus_flags;
+
+	if (cfg & OLDI_DEPOL) {
+		flags &= ~DRM_BUS_FLAG_DE_HIGH;
+		flags |= DRM_BUS_FLAG_DE_LOW;
+	} else if (flags & DRM_BUS_FLAG_DE_LOW) {
+		flags &= ~DRM_BUS_FLAG_DE_LOW;
+		flags |= DRM_BUS_FLAG_DE_HIGH;
+	}
+
+	bridge_state->output_bus_cfg.flags = flags;
+
+	flags &= ~(DRM_BUS_FLAG_PIXDATA_SAMPLE_NEGEDGE |
+		   DRM_BUS_FLAG_SYNC_SAMPLE_NEGEDGE);
+	flags |= DRM_BUS_FLAG_PIXDATA_SAMPLE_POSEDGE |
+		 DRM_BUS_FLAG_SYNC_SAMPLE_POSEDGE;
+	bridge_state->input_bus_cfg.flags = flags;
+
+	return 0;
+}
+
+static int
+tidss_oldi_bridge_connector_readout(struct drm_bridge *bridge,
+				    struct drm_atomic_sro_state *state,
+				    struct drm_connector_state *conn_state)
+{
+	struct tidss_oldi *oldi = drm_bridge_to_tidss_oldi(bridge);
+	struct drm_connector *connector = conn_state->connector;
+	struct drm_crtc_state *crtc_state;
+	struct drm_encoder *encoder;
+	struct drm_crtc *crtc;
+
+	/* The transmitter is off, there is nothing to read out. */
+	if (!(tidss_oldi_read_config(oldi) & OLDI_ENABLE))
+		return 0;
+
+	encoder = bridge->encoder;
+	if (!encoder->funcs || !encoder->funcs->atomic_sro_get_current_crtc)
+		return -ENODEV;
+
+	crtc = encoder->funcs->atomic_sro_get_current_crtc(encoder);
+	if (!crtc)
+		return -ENODEV;
+
+	crtc_state = drm_atomic_sro_get_crtc_state(state, crtc);
+	if (!crtc_state)
+		return -ENODEV;
+
+	crtc_state->encoder_mask |= drm_encoder_mask(encoder);
+	crtc_state->connector_mask |= drm_connector_mask(connector);
+
+	/*
+	 * drm_atomic_set_crtc_for_connector() takes a connector
+	 * reference when setting the crtc pointer, which is later
+	 * released by __drm_atomic_helper_connector_destroy_state().
+	 * The readout bypasses that path, so take the reference
+	 * explicitly.
+	 */
+	drm_connector_get(connector);
+	conn_state->crtc = crtc;
+	conn_state->best_encoder = encoder;
+
+	return 0;
+}
+
 static const struct drm_bridge_funcs tidss_oldi_bridge_funcs = {
 	.attach = tidss_oldi_bridge_attach,
 	.atomic_check = tidss_oldi_atomic_check,
 	.atomic_pre_enable = tidss_oldi_atomic_pre_enable,
 	.atomic_post_disable = tidss_oldi_atomic_post_disable,
 	.atomic_get_input_bus_fmts = tidss_oldi_atomic_get_input_bus_fmts,
 	.atomic_duplicate_state = drm_atomic_helper_bridge_duplicate_state,
 	.atomic_destroy_state = drm_atomic_helper_bridge_destroy_state,
 	.atomic_create_state = drm_atomic_helper_bridge_create_state,
 	.mode_valid = tidss_oldi_mode_valid,
+	.atomic_sro_readout_state = tidss_oldi_bridge_readout_state,
+	.atomic_sro_compare_state = drm_atomic_helper_bridge_compare_state,
+	.atomic_sro_connector_readout = tidss_oldi_bridge_connector_readout,
 };
 
 static int get_oldi_mode(struct device_node *oldi_tx, int *companion_instance)
 {
 	struct device_node *companion;
@@ -610,10 +766,11 @@ static int tidss_oldi_init_one(struct tidss_device *tidss,
 	}
 
 	/* Register the bridge. */
 	oldi->bridge.of_node = oldi_tx;
 	oldi->bridge.driver_private = oldi;
+	oldi->bridge.ops |= DRM_BRIDGE_OP_CONNECTOR_HW_READOUT;
 
 	tidss->oldis[tidss->num_oldis++] = oldi;
 	tidss->is_ext_vp_clk[oldi->parent_vp] = true;
 	oldi->tidss = tidss;
 
Signature · 1 line
2.56.1
#64 lore raw

Re: [PATCH DO NOT MERGE v9 41/42] drm/tidss: oldi: Implement hardware state readout

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] drm/tidss: unchecked tidss_runtime_get() in tidss_oldi_read_config() --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] drm/tidss: unchecked tidss_runtime_get() in tidss_oldi_read_config()
--

commit a65bf8e1088f5e847af4ee39d253fd55598fe32e
Author: Maxime Ripard <[email protected]>

drm/tidss: oldi: Implement hardware state readout

This commit implements the atomic state readout and connector readout hooks for the tidss OLDI bridge.

Quoted 4 lines
drivers/gpu/drm/tidss/tidss_oldi.c +0 -0
index f767b0c115ac3..0ad4ef34ba640 100644

[ ... ]

Quoted 4 lines
drivers/gpu/drm/tidss/tidss_oldi.c +0 -0
@@ -350,6 +355,154 @@ tidss_oldi_mode_valid(struct drm_bridge *bridge,
 	return MODE_OK;
 }
 

drivers/gpu/drm/tidss/tidss_oldi.c tidss_oldi_mode_valid(struct drm_bridge *bridge, · line 367

Quoted 10 lines
drivers/gpu/drm/tidss/tidss_oldi.c +10 -0
+static u32 tidss_oldi_read_config(struct tidss_oldi *oldi)
+{
+	u32 cfg;
+
+	tidss_runtime_get(oldi->tidss);
+	cfg = tidss_get_oldi_config(oldi->tidss, oldi->parent_vp);
+	tidss_runtime_put(oldi->tidss);
+
+	return cfg;
+}

[Severity: Medium]
Should tidss_oldi_read_config() check the return value of tidss_runtime_get()?

tidss_runtime_get() wraps pm_runtime_resume_and_get(), which drops the runtime PM usage reference when resume fails. If it fails, tidss_get_oldi_config() reads DISPC_VP_DSS_OLDI_CFG while the DSS is still suspended, and tidss_runtime_put() decrements the usage count a second time.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=41
author maintainer
#65 lore raw

PATCH v9 42/42 drm/panel: Prepare and enable panels read out as active

In reply to Maxime Ripard

The panel bridge assumes the panel is enabled whenever its pipeline was read out as active, since a panel has no state that can be read out of the hardware.

However, the drm_panel itself still considers the panel powered down. The next commit won't prepare and enable it if it doesn't need a modeset, so the panel driver never acquires its supplies, GPIOs or backlight, and the next disable of the pipeline skips the panel teardown with a warning since the panel was never prepared.

Implement the bridge atomic_sro_install_state hook to call drm_panel_prepare() and drm_panel_enable() for those panels, so that the drm_panel state matches the hardware state.

This is a hack: drm_panel_prepare() and drm_panel_enable() don't take over a running panel, they run the full power up sequence of the panel driver on a panel the firmware has already lit. Depending on the driver, that can mean toggling the reset GPIO, waiting for the power-up delays, or sending the whole DSI initialization sequence again, which might blank or glitch the display, and thus defeats the purpose of the state readout. It also notifies the panel followers as if the panel had just been powered up.

Doing this properly requires a drm_panel operation to take over a panel that is already running, acquiring its resources without reprogramming it, which doesn't exist yet.

---
 drivers/gpu/drm/drm_panel.c | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)
drivers/gpu/drm/drm_panel.c +23 -0
index 9c60942de90b..1dd5c1011b02 100644
@@ -350,10 +350,32 @@ panel_bridge_connector_readout(struct drm_bridge *bridge,
 	conn_state->best_encoder = encoder;
 
 	return 0;
 }
 
+/*
+ * The pipeline was read out as active, so the firmware left the panel
+ * powered up and enabled. Prepare and enable it so that the panel
+ * driver acquires its resources and its state matches the hardware,
+ * since the next commit won't do it if it doesn't need a modeset, and
+ * the next disable of the pipeline will release them.
+ */
+static void
+panel_bridge_install_state(struct drm_bridge *bridge,
+			   struct drm_bridge_state *bridge_state,
+			   struct drm_crtc_state *crtc_state,
+			   struct drm_connector_state *conn_state)
+{
+	struct drm_panel *panel = drm_bridge_to_panel(bridge);
+
+	if (!panel->prepared)
+		drm_panel_prepare(panel);
+
+	if (!panel->enabled)
+		drm_panel_enable(panel);
+}
+
 static const struct drm_bridge_funcs panel_bridge_bridge_funcs = {
 	.attach = panel_bridge_attach,
 	.detach = panel_bridge_detach,
 	.atomic_pre_enable = panel_bridge_atomic_pre_enable,
 	.atomic_enable = panel_bridge_atomic_enable,
@@ -365,10 +387,11 @@ static const struct drm_bridge_funcs panel_bridge_bridge_funcs = {
 	.atomic_destroy_state = drm_atomic_helper_bridge_destroy_state,
 	.atomic_get_input_bus_fmts = drm_atomic_helper_bridge_propagate_bus_fmt,
 	.debugfs_init = panel_bridge_debugfs_init,
 	.atomic_sro_readout_state = panel_bridge_readout_state,
 	.atomic_sro_compare_state = drm_atomic_helper_bridge_compare_state,
+	.atomic_sro_install_state = panel_bridge_install_state,
 	.atomic_sro_connector_readout = panel_bridge_connector_readout,
 };
 
 /**
  * drm_bridge_is_panel - Checks if a drm_bridge is a panel_bridge.
Signature · 1 line
2.56.1