Skip to contentSkip to conversation

drm/client: Keep larger modes in connector->modes on fbdev hotplug

PATCH In review Hugo Sales 1 participant 4 messages Oct 24 – Nov 20

Subsystems (2)
lore
Share & tools
lore

Hugo Sales

author
#0 lore raw

v2 cover drm/client: Keep larger modes in connector->modes on fbdev hotplug

On an ASUS ROG Flow X13 with a 3840x2400 eDP panel, booted with video=eDP-1:1920x1200@60, an HDMI hotplug makes fbdev reprobe every connector with its 1920x1200 framebuffer as the limit. connector->modes loses the panel's 3840x2400 mode, and after suspend/resume amdgpu drives the panel with the 1920x1200 timing. The image ends up squeezed into the top-left corner. Details are in
https://gitlab.freedesktop.org/drm/amd/-/issues/5983

Patch 1 makes drm_client_modeset_probe() probe with the device's limits and moves the framebuffer size check into the fbdev mode pickers. Patch 2 adds KUnit tests for it

fbdev's hotplug probe has passed the framebuffer size since 2010 (4abe35204af8). The pickers didn't check mode sizes then, so leaving larger modes out of the list was what kept fbdev from picking one. They check the size themselves now, so the probe can use the device's limits like initial setup, drm_log and GETCONNECTOR already do

The HiBMC thread "[RFC] drm/client: Relax clone mode selection in drm_client_target_cloned" runs into the same pruning: its 640x480 framebuffer removes 1024x768 from both connectors' lists. With this series userspace sees that mode again, but fbdev still can't use it, so this doesn't replace that RFC:
https://lore.kernel.org/r/[email protected]

I sent an amdgpu fix for the same symptom separately. On eDP and LVDS it makes amdgpu fall back to a mode of the native size, or to the encoder's native mode if the list has none:
https://lore.kernel.org/r/[email protected]
This series fixes the shared mode list for every driver instead. Either one alone fixes the reported bug, from my testing

On amd-staging-drm-next with only this series applied, with an HDMI hotplug and suspend/resume (pm_test=platform) in a loop, 255 of 255 resumes kept the native timing. Without it, 28 of 29 came back with the wrong timing, and the same loop on unpatched v7.2.8 got it wrong in 103 of 103. The drm KUnit tests pass, also under KASAN, which reports nothing in the code this series touches

Ville, you said the approach should be fine. If this matches what you had in mind, I'd appreciate an Acked-by or Reviewed-by

Changes in v2:
- Tests: free the picked modes through a KUnit cleanup action, so a failed assert no longer leaks them, and assert only after dropping mode_config.mutex. Both came from Sashiko's review of v1
- Tests: add a firmware config test for a tiled connector with missing tiles, with and without a non-tiled mode that fits
- Check the size of the CRTC's current mode before drm_client_firmware_config() falls back to it. Without the 640x480 modes, a DisplayPort sink with no mode that fits could reach that fallback with a mode larger than the framebuffer. Sashiko found this on a local run over v2, and a new test covers it
- Don't use the firmware config when a lit connector ends up with no mode. Accepting it would also skip the fallback, which may light up other connectors on that CRTC. Sashiko found this on a later local run
- Sashiko also found a NULL modes[i]->name print in drm_client_firmware_config(). It predates this series, so the fix went out on its own:
https://lore.kernel.org/r/[email protected]
- Rebased onto drm-misc-next at cf5d8e390e2a
- v1: https://lore.kernel.org/r/[email protected]

Hugo Sales (2):
drm/client: Probe connectors with the device's mode size limits drm/tests: Add KUnit tests for the client mode size limits

 drivers/gpu/drm/drm_client_modeset.c          |  72 ++-
 .../gpu/drm/tests/drm_client_modeset_test.c   | 498 +++++++++++++++++-
 2 files changed, 550 insertions(+), 20 deletions(-)

base-commit: cf5d8e390e2ac2cfa24a50645efdbb9f0eebe65f

Signature · 1 line
2.55.0
author
#1 lore raw

PATCH v2 1/2 drm/client: Probe connectors with the device's mode size limits

Currently, drm_client_modeset_probe() passes the client's width and height to ->fill_modes(), and the probe helper leaves every larger mode out of connector->modes. drm_log, fbdev's initial setup and the GETCONNECTOR ioctl already probe with the device's limits, but on a hotplug fbdev passes its framebuffer size. After that, the list that userspace and the driver share lacks every mode larger than the framebuffer. That includes the panel's preferred mode when video= sets a smaller one

Probe with the device's limits instead. Make the mode pickers skip modes that don't fit width x height, using the drm_mode_validate_size() check the probe helper applied before. The probe helper still runs mode_valid on every mode, so only the size check moves

fbdev picks the same modes as before, with one exception. A DisplayPort sink with no mode that fits used to end up with an empty list, so the probe helper added its 640x480 fallback modes. Its list now keeps the larger modes, and fbdev leaves that output off

That sink can then reach drm_client_firmware_config()'s last resort, the CRTC's current mode, which may be larger than the framebuffer. Check that mode's size too. When a lit connector ends up with no mode, don't use the firmware config. Accepting it would also skip the fallback, which may light up other connectors on that CRTC

---
 drivers/gpu/drm/drm_client_modeset.c          | 72 ++++++++++++++-----
 .../gpu/drm/tests/drm_client_modeset_test.c   |  4 +-
 2 files changed, 57 insertions(+), 19 deletions(-)
drivers/gpu/drm/drm_client_modeset.c +55 -17
index 0080a8e9556a5..cb1bf67a577f0 100644
@@ -121,11 +121,13 @@ drm_client_find_modeset(struct drm_client_dev *client, struct drm_crtc *crtc)
 }
 
 static const struct drm_display_mode *
-drm_connector_get_tiled_mode(struct drm_connector *connector)
+drm_connector_get_tiled_mode(struct drm_connector *connector, int width, int height)
 {
 	const struct drm_display_mode *mode;
 
 	list_for_each_entry(mode, &connector->modes, head) {
+		if (drm_mode_validate_size(mode, width, height) != MODE_OK)
+			continue;
 		if (mode->hdisplay == connector->tile_h_size &&
 		    mode->vdisplay == connector->tile_v_size)
 			return mode;
@@ -134,11 +136,13 @@ drm_connector_get_tiled_mode(struct drm_connector *connector)
 }
 
 static const struct drm_display_mode *
-drm_connector_fallback_non_tiled_mode(struct drm_connector *connector)
+drm_connector_fallback_non_tiled_mode(struct drm_connector *connector, int width, int height)
 {
 	const struct drm_display_mode *mode;
 
 	list_for_each_entry(mode, &connector->modes, head) {
+		if (drm_mode_validate_size(mode, width, height) != MODE_OK)
+			continue;
 		if (mode->hdisplay == connector->tile_h_size &&
 		    mode->vdisplay == connector->tile_v_size)
 			continue;
@@ -163,14 +167,19 @@ drm_connector_preferred_mode(struct drm_connector *connector, int width, int hei
 }
 
 static const struct drm_display_mode *
-drm_connector_first_mode(struct drm_connector *connector)
+drm_connector_first_mode(struct drm_connector *connector, int width, int height)
 {
-	return list_first_entry_or_null(&connector->modes,
-					struct drm_display_mode, head);
+	const struct drm_display_mode *mode;
+
+	list_for_each_entry(mode, &connector->modes, head) {
+		if (drm_mode_validate_size(mode, width, height) == MODE_OK)
+			return mode;
+	}
+	return NULL;
 }
 
 static const struct drm_display_mode *
-drm_connector_pick_cmdline_mode(struct drm_connector *connector)
+drm_connector_pick_cmdline_mode(struct drm_connector *connector, int width, int height)
 {
 	const struct drm_cmdline_mode *cmdline_mode;
 	const struct drm_display_mode *mode;
@@ -183,6 +192,8 @@ drm_connector_pick_cmdline_mode(struct drm_connector *connector)
 	 */
 
 	list_for_each_entry(mode, &connector->modes, head) {
+		if (drm_mode_validate_size(mode, width, height) != MODE_OK)
+			continue;
 		if (mode->type & DRM_MODE_TYPE_USERDEF)
 			return mode;
 	}
@@ -199,6 +210,8 @@ drm_connector_pick_cmdline_mode(struct drm_connector *connector)
 	prefer_non_interlace = !cmdline_mode->interlace;
 again:
 	list_for_each_entry(mode, &connector->modes, head) {
+		if (drm_mode_validate_size(mode, width, height) != MODE_OK)
+			continue;
 		/* check width/height */
 		if (mode->hdisplay != cmdline_mode->xres ||
 		    mode->vdisplay != cmdline_mode->yres)
@@ -321,7 +334,7 @@ static bool drm_client_target_cloned(struct drm_device *dev,
 			continue;
 
 		mode_replace(dev, &modes[i],
-			     drm_connector_pick_cmdline_mode(connectors[i]));
+			     drm_connector_pick_cmdline_mode(connectors[i], width, height));
 		if (!modes[i]) {
 			can_clone = false;
 			break;
@@ -357,6 +370,8 @@ static bool drm_client_target_cloned(struct drm_device *dev,
 			continue;
 
 		list_for_each_entry(mode, &connectors[i]->modes, head) {
+			if (drm_mode_validate_size(mode, width, height) != MODE_OK)
+				continue;
 			if (drm_mode_match(mode, dmt_mode,
 					   DRM_MODE_MATCH_TIMINGS |
 					   DRM_MODE_MATCH_CLOCK |
@@ -472,7 +487,7 @@ static bool drm_client_target_preferred(struct drm_device *dev,
 
 		mode_type = "cmdline";
 		mode_replace(dev, &modes[i],
-			     drm_connector_pick_cmdline_mode(connector));
+			     drm_connector_pick_cmdline_mode(connector, width, height));
 
 		if (!modes[i]) {
 			mode_type = "preferred";
@@ -483,7 +498,7 @@ static bool drm_client_target_preferred(struct drm_device *dev,
 		if (!modes[i]) {
 			mode_type = "first";
 			mode_replace(dev, &modes[i],
-				     drm_connector_first_mode(connector));
+				     drm_connector_first_mode(connector, width, height));
 		}
 
 		/*
@@ -499,14 +514,16 @@ static bool drm_client_target_preferred(struct drm_device *dev,
 			    connector->num_h_tile * connector->num_v_tile ||
 			    (connector->tile_h_loc == 0 &&
 			     connector->tile_v_loc == 0 &&
-			     !drm_connector_get_tiled_mode(connector))) {
+			     !drm_connector_get_tiled_mode(connector, width, height))) {
 				mode_type = "non tiled";
 				mode_replace(dev, &modes[i],
-					     drm_connector_fallback_non_tiled_mode(connector));
+					     drm_connector_fallback_non_tiled_mode(connector,
+										   width, height));
 			} else {
 				mode_type = "tiled";
 				mode_replace(dev, &modes[i],
-					     drm_connector_get_tiled_mode(connector));
+					     drm_connector_get_tiled_mode(connector,
+									  width, height));
 			}
 		}
 
@@ -721,7 +738,7 @@ static bool drm_client_firmware_config(struct drm_client_dev *client,
 
 		mode_type = "cmdline";
 		mode_replace(dev, &modes[i],
-			     drm_connector_pick_cmdline_mode(connector));
+			     drm_connector_pick_cmdline_mode(connector, width, height));
 
 		if (!modes[i]) {
 			mode_type = "preferred";
@@ -732,11 +749,12 @@ static bool drm_client_firmware_config(struct drm_client_dev *client,
 		if (!modes[i]) {
 			mode_type = "first";
 			mode_replace(dev, &modes[i],
-				     drm_connector_first_mode(connector));
+				     drm_connector_first_mode(connector, width, height));
 		}
 
 		/* last resort: use current mode */
-		if (!modes[i]) {
+		if (!modes[i] &&
+		    drm_mode_validate_size(&crtc->state->mode, width, height) == MODE_OK) {
 			mode_type = "current";
 			mode_replace(dev, &modes[i],
 				     &crtc->state->mode);
@@ -750,8 +768,21 @@ static bool drm_client_firmware_config(struct drm_client_dev *client,
 		    num_tiled_conns < connector->num_h_tile * connector->num_v_tile) {
 			mode_type = "non tiled";
 			mode_replace(dev, &modes[i],
-				     drm_connector_fallback_non_tiled_mode(connector));
+				     drm_connector_fallback_non_tiled_mode(connector,
+									   width, height));
+		}
+
+		/*
+		 * Accepting a config that leaves this connector dark would
+		 * also skip the fallback, which may light up other connectors
+		 * on this CRTC
+		 */
+		if (!modes[i]) {
+			drm_dbg_kms(dev, "[CONNECTOR:%d:%s] no mode fits %dx%d\n",
+				    connector->base.id, connector->name, width, height);
+			goto bail;
 		}
+
 		crtcs[i] = crtc;
 
 		drm_dbg_kms(dev, "[CONNECTOR::%d:%s] on [CRTC:%d:%s] using %s mode: %s\n",
@@ -865,8 +896,15 @@ int drm_client_modeset_probe(struct drm_client_dev *client, unsigned int width,
 	mutex_lock(&client->modeset_mutex);
 
 	mutex_lock(&dev->mode_config.mutex);
+	/*
+	 * Probe with the device limits, not width x height. connector->modes is
+	 * shared with userspace and the driver, so it must keep modes larger
+	 * than this client's size. The mode pickers skip those instead.
+	 */
 	for (i = 0; i < connector_count; i++)
-		total_modes_count += connectors[i]->funcs->fill_modes(connectors[i], width, height);
+		total_modes_count += connectors[i]->funcs->fill_modes(connectors[i],
+								      dev->mode_config.max_width,
+								      dev->mode_config.max_height);
 	if (!total_modes_count)
 		drm_dbg_kms(dev, "No connectors reported connected with modes\n");
 	drm_client_connectors_enabled(connectors, connector_count, enabled);
drivers/gpu/drm/tests/drm_client_modeset_test.c +2 -2
index ec58fe064d862..00f43fca97954 100644
@@ -112,7 +112,7 @@ static void drm_test_pick_cmdline_res_1920_1080_60(struct kunit *test)
 	mutex_unlock(&drm->mode_config.mutex);
 	KUNIT_ASSERT_GT(test, ret, 0);
 
-	mode = drm_connector_pick_cmdline_mode(connector);
+	mode = drm_connector_pick_cmdline_mode(connector, 1920, 1080);
 	KUNIT_ASSERT_NOT_NULL(test, mode);
 
 	KUNIT_EXPECT_TRUE(test, drm_mode_equal(expected_mode, mode));
@@ -151,7 +151,7 @@ static void drm_test_pick_cmdline_named(struct kunit *test)
 	mutex_unlock(&drm->mode_config.mutex);
 	KUNIT_ASSERT_GT(test, ret, 0);
 
-	mode = drm_connector_pick_cmdline_mode(connector);
+	mode = drm_connector_pick_cmdline_mode(connector, 1920, 1080);
 	KUNIT_ASSERT_NOT_NULL(test, mode);
 
 	expected_mode = params->func(drm);
Signature · 1 line
2.55.0
author
#2 lore raw

PATCH v2 2/2 drm/tests: Add KUnit tests for the client mode size limits

In reply to Hugo Sales

Test that the first-mode, cmdline, tiled and clone pickers skip modes larger than the client's size, and that drm_client_modeset_probe() keeps the preferred mode in connector->modes when the client is smaller than it

Also test that drm_client_firmware_config() rejects the firmware config for a tiled connector with missing tiles when no non-tiled mode fits, and uses the non-tiled mode when one does

Test that it takes the CRTC's current mode when no probed mode fits, and rejects the config if the current mode doesn't fit either. When the firmware lit a connector with no mode that fits, the probe must light another connector on the same CRTC instead

The probe tests call drm_client_modeset_create() directly, because drm_client_init() needs a dumb_create callback the test driver doesn't have. The firmware config tests set the connector's CRTC and encoder in their state by hand, as if the firmware had lit it

---
 .../gpu/drm/tests/drm_client_modeset_test.c   | 494 +++++++++++++++++-
 1 file changed, 493 insertions(+), 1 deletion(-)
drivers/gpu/drm/tests/drm_client_modeset_test.c +493 -1
index 00f43fca97954..da8abedbf9d3b 100644
@@ -18,6 +18,9 @@ struct drm_client_modeset_test_priv {
 	struct drm_device *drm;
 	struct device *dev;
 	struct drm_connector connector;
+	struct drm_connector second_connector;
+	struct drm_encoder encoder;
+	const struct drm_display_mode *modes[2];
 };
 
 static int drm_client_modeset_connector_get_modes(struct drm_connector *connector)
@@ -48,9 +51,41 @@ static const struct drm_connector_helper_funcs drm_client_modeset_connector_help
 	.get_modes = drm_client_modeset_connector_get_modes,
 };
 
+static int drm_client_modeset_connector_get_modes_preferred(struct drm_connector *connector)
+{
+	int count = drm_add_modes_noedid(connector, 1920, 1080);
+
+	drm_set_preferred_mode(connector, 1920, 1080);
+
+	return count;
+}
+
+static const struct drm_connector_helper_funcs drm_client_modeset_preferred_helper_funcs = {
+	.get_modes = drm_client_modeset_connector_get_modes_preferred,
+};
+
+static int drm_client_modeset_connector_get_modes_small(struct drm_connector *connector)
+{
+	struct drm_display_mode *mode = drm_cvt_mode(connector->dev, 320, 240, 60,
+						     false, false, false);
+
+	if (!mode)
+		return 0;
+
+	drm_mode_probed_add(connector, mode);
+
+	return 1;
+}
+
+static const struct drm_connector_helper_funcs drm_client_modeset_small_helper_funcs = {
+	.get_modes = drm_client_modeset_connector_get_modes_small,
+};
+
 static const struct drm_connector_funcs drm_client_modeset_connector_funcs = {
 	.atomic_destroy_state   = drm_atomic_helper_connector_destroy_state,
-	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state
+	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
+	.fill_modes             = drm_helper_probe_single_connector_modes,
+	.reset                  = drm_atomic_helper_connector_reset,
 };
 
 static int drm_client_modeset_test_init(struct kunit *test)
@@ -197,6 +232,463 @@ static struct kunit_suite drm_test_pick_cmdline_test_suite = {
 
 kunit_test_suite(drm_test_pick_cmdline_test_suite);
 
+static void drm_test_pick_first_mode_within_limits(struct kunit *test)
+{
+	struct drm_client_modeset_test_priv *priv = test->priv;
+	struct drm_device *drm = priv->drm;
+	struct drm_connector *connector = &priv->connector;
+	const struct drm_display_mode *full, *smaller, *none, *preferred;
+	int ret;
+
+	drm_connector_helper_add(connector, &drm_client_modeset_preferred_helper_funcs);
+
+	mutex_lock(&drm->mode_config.mutex);
+	ret = drm_helper_probe_single_connector_modes(connector, 1920, 1080);
+	full = drm_connector_first_mode(connector, 1920, 1080);
+	smaller = drm_connector_first_mode(connector, 1024, 768);
+	none = drm_connector_first_mode(connector, 320, 240);
+	preferred = drm_connector_preferred_mode(connector, 1024, 768);
+	mutex_unlock(&drm->mode_config.mutex);
+	KUNIT_ASSERT_GT(test, ret, 0);
+
+	KUNIT_ASSERT_NOT_NULL(test, full);
+	KUNIT_EXPECT_EQ(test, full->hdisplay, 1920);
+	KUNIT_EXPECT_EQ(test, full->vdisplay, 1080);
+
+	KUNIT_ASSERT_NULL(test, preferred);
+
+	KUNIT_ASSERT_NOT_NULL(test, smaller);
+	KUNIT_EXPECT_EQ(test, smaller->hdisplay, 1024);
+	KUNIT_EXPECT_EQ(test, smaller->vdisplay, 768);
+
+	KUNIT_EXPECT_NULL(test, none);
+}
+
+static void drm_test_pick_cmdline_mode_within_limits(struct kunit *test)
+{
+	struct drm_client_modeset_test_priv *priv = test->priv;
+	struct drm_device *drm = priv->drm;
+	struct drm_connector *connector = &priv->connector;
+	struct drm_cmdline_mode *cmdline_mode = &connector->cmdline_mode;
+	const struct drm_display_mode *full, *smaller;
+	int ret;
+
+	KUNIT_ASSERT_TRUE(test, drm_mode_parse_command_line_for_connector("1920x1080", connector,
+									  cmdline_mode));
+
+	mutex_lock(&drm->mode_config.mutex);
+	ret = drm_helper_probe_single_connector_modes(connector, 1920, 1080);
+	full = drm_connector_pick_cmdline_mode(connector, 1920, 1080);
+	smaller = drm_connector_pick_cmdline_mode(connector, 1024, 768);
+	mutex_unlock(&drm->mode_config.mutex);
+	KUNIT_ASSERT_GT(test, ret, 0);
+
+	KUNIT_ASSERT_NOT_NULL(test, full);
+	KUNIT_EXPECT_EQ(test, full->hdisplay, 1920);
+	KUNIT_EXPECT_EQ(test, full->vdisplay, 1080);
+
+	KUNIT_EXPECT_NULL(test, smaller);
+}
+
+static void drm_test_pick_tiled_mode_within_limits(struct kunit *test)
+{
+	struct drm_client_modeset_test_priv *priv = test->priv;
+	struct drm_device *drm = priv->drm;
+	struct drm_connector *connector = &priv->connector;
+	const struct drm_display_mode *tiled, *smaller_tiled, *non_tiled;
+	int ret;
+
+	drm_connector_helper_add(connector, &drm_client_modeset_preferred_helper_funcs);
+	connector->tile_h_size = 1920;
+	connector->tile_v_size = 1080;
+
+	mutex_lock(&drm->mode_config.mutex);
+	ret = drm_helper_probe_single_connector_modes(connector, 1920, 1080);
+	tiled = drm_connector_get_tiled_mode(connector, 1920, 1080);
+	smaller_tiled = drm_connector_get_tiled_mode(connector, 1024, 768);
+	non_tiled = drm_connector_fallback_non_tiled_mode(connector, 1024, 768);
+	mutex_unlock(&drm->mode_config.mutex);
+	KUNIT_ASSERT_GT(test, ret, 0);
+
+	KUNIT_ASSERT_NOT_NULL(test, tiled);
+	KUNIT_EXPECT_EQ(test, tiled->hdisplay, 1920);
+	KUNIT_EXPECT_EQ(test, tiled->vdisplay, 1080);
+
+	KUNIT_EXPECT_NULL(test, smaller_tiled);
+
+	KUNIT_ASSERT_NOT_NULL(test, non_tiled);
+	KUNIT_EXPECT_EQ(test, non_tiled->hdisplay, 1024);
+	KUNIT_EXPECT_EQ(test, non_tiled->vdisplay, 768);
+}
+
+static void drm_client_modeset_test_destroy_modes(void *ptr)
+{
+	struct drm_client_modeset_test_priv *priv = ptr;
+
+	modes_destroy(priv->drm, priv->modes, ARRAY_SIZE(priv->modes));
+}
+
+static void drm_test_pick_clone_mode_within_limits(struct kunit *test)
+{
+	struct drm_client_modeset_test_priv *priv = test->priv;
+	struct drm_device *drm = priv->drm;
+	struct drm_connector *connectors[] = { &priv->connector, &priv->second_connector };
+	const struct drm_display_mode **modes = priv->modes;
+	struct drm_client_offset offsets[ARRAY_SIZE(connectors)] = {};
+	bool enabled[ARRAY_SIZE(connectors)] = { true, true };
+	bool cloned_fits, cloned_too_small;
+	unsigned int i;
+	int ret;
+
+	ret = drmm_connector_init(drm, &priv->second_connector,
+				  &drm_client_modeset_connector_funcs,
+				  DRM_MODE_CONNECTOR_Unknown,
+				  NULL);
+	KUNIT_ASSERT_EQ(test, ret, 0);
+
+	drm_connector_helper_add(&priv->second_connector,
+				 &drm_client_modeset_connector_helper_funcs);
+
+	ret = kunit_add_action_or_reset(test, drm_client_modeset_test_destroy_modes, priv);
+	KUNIT_ASSERT_EQ(test, ret, 0);
+
+	mutex_lock(&drm->mode_config.mutex);
+	for (i = 0; i < ARRAY_SIZE(connectors); i++) {
+		ret = drm_helper_probe_single_connector_modes(connectors[i], 1920, 1080);
+		KUNIT_EXPECT_GT(test, ret, 0);
+	}
+
+	cloned_too_small = drm_client_target_cloned(drm, connectors, ARRAY_SIZE(connectors),
+						    modes, offsets, enabled, 800, 600);
+	modes_destroy(drm, modes, ARRAY_SIZE(connectors));
+
+	cloned_fits = drm_client_target_cloned(drm, connectors, ARRAY_SIZE(connectors),
+					       modes, offsets, enabled, 1024, 768);
+	mutex_unlock(&drm->mode_config.mutex);
+
+	KUNIT_EXPECT_FALSE(test, cloned_too_small);
+	KUNIT_EXPECT_TRUE(test, cloned_fits);
+	for (i = 0; i < ARRAY_SIZE(connectors); i++) {
+		KUNIT_ASSERT_NOT_NULL(test, modes[i]);
+		KUNIT_EXPECT_EQ(test, modes[i]->hdisplay, 1024);
+		KUNIT_EXPECT_EQ(test, modes[i]->vdisplay, 768);
+	}
+}
+
+static struct drm_crtc *drm_client_modeset_test_add_crtc(struct kunit *test)
+{
+	struct drm_client_modeset_test_priv *priv = test->priv;
+	struct drm_device *drm = priv->drm;
+	struct drm_encoder *encoder = &priv->encoder;
+	struct drm_plane *plane;
+	struct drm_crtc *crtc;
+	int ret;
+
+	plane = drm_kunit_helper_create_primary_plane(test, drm, NULL, NULL, NULL, 0, NULL);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, plane);
+
+	crtc = drm_kunit_helper_create_crtc(test, drm, plane, NULL, NULL, NULL);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, crtc);
+
+	ret = drmm_encoder_init(drm, encoder, NULL, DRM_MODE_ENCODER_NONE, NULL);
+	KUNIT_ASSERT_EQ(test, ret, 0);
+	encoder->possible_crtcs = drm_crtc_mask(crtc);
+
+	ret = drm_connector_attach_encoder(&priv->connector, encoder);
+	KUNIT_ASSERT_EQ(test, ret, 0);
+
+	return crtc;
+}
+
+static void drm_test_firmware_config_tiled_fallback(struct kunit *test)
+{
+	struct drm_client_modeset_test_priv *priv = test->priv;
+	struct drm_device *drm = priv->drm;
+	struct drm_connector *connector = &priv->connector;
+	struct drm_connector *connectors[] = { connector };
+	struct drm_crtc *crtcs[ARRAY_SIZE(connectors)] = {};
+	struct drm_client_offset offsets[ARRAY_SIZE(connectors)] = {};
+	bool enabled[ARRAY_SIZE(connectors)] = { true };
+	struct drm_client_dev client = { .dev = drm };
+	const struct drm_display_mode *too_small_mode;
+	bool config_fits, config_too_small;
+	struct drm_crtc *too_small_crtc;
+	struct drm_crtc *crtc;
+	int ret;
+
+	crtc = drm_client_modeset_test_add_crtc(test);
+
+	drm_mode_config_reset(drm);
+	KUNIT_ASSERT_NOT_NULL(test, connector->state);
+	connector->state->best_encoder = &priv->encoder;
+	connector->state->crtc = crtc;
+	drm_connector_get(connector);
+
+	drm_connector_helper_add(connector, &drm_client_modeset_preferred_helper_funcs);
+	connector->has_tile = true;
+	connector->num_h_tile = 2;
+	connector->num_v_tile = 1;
+	connector->tile_h_size = 1920;
+	connector->tile_v_size = 1080;
+
+	ret = kunit_add_action_or_reset(test, drm_client_modeset_test_destroy_modes, priv);
+	KUNIT_ASSERT_EQ(test, ret, 0);
+
+	mutex_lock(&drm->mode_config.mutex);
+	ret = drm_helper_probe_single_connector_modes(connector, 4096, 4096);
+
+	/*
+	 * The zeroed current mode fits 320x240, so only the non-tiled fallback
+	 * leaves the connector without a mode
+	 */
+	config_too_small = drm_client_firmware_config(&client, connectors, ARRAY_SIZE(connectors),
+						      crtcs, priv->modes, offsets, enabled,
+						      320, 240);
+	too_small_mode = priv->modes[0];
+	too_small_crtc = crtcs[0];
+	modes_destroy(drm, priv->modes, ARRAY_SIZE(connectors));
+	memset(crtcs, 0, sizeof(crtcs));
+
+	/* The tile size fits, so the non-tiled fallback must skip it */
+	config_fits = drm_client_firmware_config(&client, connectors, ARRAY_SIZE(connectors),
+						 crtcs, priv->modes, offsets, enabled,
+						 1920, 1080);
+	mutex_unlock(&drm->mode_config.mutex);
+	KUNIT_ASSERT_GT(test, ret, 0);
+
+	KUNIT_EXPECT_FALSE(test, config_too_small);
+	KUNIT_EXPECT_NULL(test, too_small_mode);
+	KUNIT_EXPECT_NULL(test, too_small_crtc);
+
+	KUNIT_EXPECT_TRUE(test, config_fits);
+	KUNIT_EXPECT_PTR_EQ(test, crtcs[0], crtc);
+	KUNIT_ASSERT_NOT_NULL(test, priv->modes[0]);
+	KUNIT_EXPECT_EQ(test, priv->modes[0]->hdisplay, 1680);
+	KUNIT_EXPECT_EQ(test, priv->modes[0]->vdisplay, 1050);
+}
+
+static void drm_client_modeset_test_take_mode(struct kunit *test, struct drm_device *drm,
+					      struct drm_display_mode *dst,
+					      struct drm_display_mode *mode)
+{
+	KUNIT_ASSERT_NOT_NULL(test, mode);
+	drm_mode_init(dst, mode);
+	drm_mode_destroy(drm, mode);
+}
+
+static void drm_test_firmware_config_current_mode(struct kunit *test)
+{
+	struct drm_client_modeset_test_priv *priv = test->priv;
+	struct drm_device *drm = priv->drm;
+	struct drm_connector *connector = &priv->connector;
+	struct drm_connector *connectors[] = { connector };
+	struct drm_crtc *crtcs[ARRAY_SIZE(connectors)] = {};
+	struct drm_client_offset offsets[ARRAY_SIZE(connectors)] = {};
+	bool enabled[ARRAY_SIZE(connectors)] = { true };
+	struct drm_client_dev client = { .dev = drm };
+	const struct drm_display_mode *too_large_mode;
+	struct drm_display_mode current_too_large;
+	struct drm_display_mode current_fits;
+	bool config_fits, config_too_large;
+	struct drm_crtc *too_large_crtc;
+	struct drm_crtc *crtc;
+	int ret;
+
+	crtc = drm_client_modeset_test_add_crtc(test);
+
+	drm_mode_config_reset(drm);
+	KUNIT_ASSERT_NOT_NULL(test, connector->state);
+	KUNIT_ASSERT_NOT_NULL(test, crtc->state);
+	connector->state->best_encoder = &priv->encoder;
+	connector->state->crtc = crtc;
+	drm_connector_get(connector);
+
+	/*
+	 * drm_client_firmware_config() only configures connectors that aren't
+	 * tiled once a tiled one is configured, so use a single 1x1 tile.
+	 */
+	drm_connector_helper_add(connector, &drm_client_modeset_preferred_helper_funcs);
+	connector->has_tile = true;
+	connector->num_h_tile = 1;
+	connector->num_v_tile = 1;
+	connector->tile_h_size = 1920;
+	connector->tile_v_size = 1080;
+
+	ret = kunit_add_action_or_reset(test, drm_client_modeset_test_destroy_modes, priv);
+	KUNIT_ASSERT_EQ(test, ret, 0);
+
+	drm_client_modeset_test_take_mode(test, drm, &current_too_large,
+					  drm_mode_find_dmt(drm, 1920, 1080, 60, false));
+	drm_client_modeset_test_take_mode(test, drm, &current_fits,
+					  drm_cvt_mode(drm, 320, 240, 60, false, false, false));
+
+	/* Every probed mode is larger than 320x240, so only "current" is left */
+	mutex_lock(&drm->mode_config.mutex);
+	ret = drm_helper_probe_single_connector_modes(connector, 4096, 4096);
+
+	drm_mode_copy(&crtc->state->mode, &current_too_large);
+	config_too_large = drm_client_firmware_config(&client, connectors, ARRAY_SIZE(connectors),
+						      crtcs, priv->modes, offsets, enabled,
+						      320, 240);
+	too_large_mode = priv->modes[0];
+	too_large_crtc = crtcs[0];
+	modes_destroy(drm, priv->modes, ARRAY_SIZE(connectors));
+	memset(crtcs, 0, sizeof(crtcs));
+
+	drm_mode_copy(&crtc->state->mode, &current_fits);
+	config_fits = drm_client_firmware_config(&client, connectors, ARRAY_SIZE(connectors),
+						 crtcs, priv->modes, offsets, enabled,
+						 320, 240);
+	mutex_unlock(&drm->mode_config.mutex);
+	KUNIT_ASSERT_GT(test, ret, 0);
+
+	KUNIT_EXPECT_FALSE(test, config_too_large);
+	KUNIT_EXPECT_NULL(test, too_large_mode);
+	KUNIT_EXPECT_NULL(test, too_large_crtc);
+
+	KUNIT_EXPECT_TRUE(test, config_fits);
+	KUNIT_EXPECT_PTR_EQ(test, crtcs[0], crtc);
+	KUNIT_ASSERT_NOT_NULL(test, priv->modes[0]);
+	KUNIT_EXPECT_EQ(test, priv->modes[0]->hdisplay, 320);
+	KUNIT_EXPECT_EQ(test, priv->modes[0]->vdisplay, 240);
+}
+
+KUNIT_DEFINE_ACTION_WRAPPER(drm_client_modeset_free_wrapper, drm_client_modeset_free,
+			    struct drm_client_dev *);
+
+static void drm_test_probe_keeps_modes_above_limits(struct kunit *test)
+{
+	struct drm_client_modeset_test_priv *priv = test->priv;
+	struct drm_device *drm = priv->drm;
+	struct drm_connector *connector = &priv->connector;
+	const struct drm_display_mode *mode;
+	struct drm_client_dev *client;
+	int ret;
+
+	drm->mode_config.max_width = 4096;
+	drm->mode_config.max_height = 4096;
+
+	drm_client_modeset_test_add_crtc(test);
+
+	drm_connector_helper_add(connector, &drm_client_modeset_preferred_helper_funcs);
+
+	client = kunit_kzalloc(test, sizeof(*client), GFP_KERNEL);
+	KUNIT_ASSERT_NOT_NULL(test, client);
+	client->dev = drm;
+
+	ret = drm_client_modeset_create(client);
+	KUNIT_ASSERT_EQ(test, ret, 0);
+
+	ret = kunit_add_action_or_reset(test, drm_client_modeset_free_wrapper, client);
+	KUNIT_ASSERT_EQ(test, ret, 0);
+
+	ret = drm_client_modeset_probe(client, 1024, 768);
+	KUNIT_ASSERT_EQ(test, ret, 0);
+
+	mode = client->modesets[0].mode;
+	KUNIT_ASSERT_NOT_NULL(test, mode);
+	KUNIT_EXPECT_EQ(test, mode->hdisplay, 1024);
+	KUNIT_EXPECT_EQ(test, mode->vdisplay, 768);
+
+	mutex_lock(&drm->mode_config.mutex);
+	mode = drm_connector_preferred_mode(connector, 1920, 1080);
+	mutex_unlock(&drm->mode_config.mutex);
+	KUNIT_ASSERT_NOT_NULL(test, mode);
+	KUNIT_EXPECT_EQ(test, mode->hdisplay, 1920);
+	KUNIT_EXPECT_EQ(test, mode->vdisplay, 1080);
+}
+
+/*
+ * The firmware lit a connector with a mode larger than the client, and a second
+ * connector on the same CRTC has a mode that fits. Probing must not keep the
+ * firmware config, so the fallback drives the second connector
+ */
+static void drm_test_probe_lights_other_connector(struct kunit *test)
+{
+	struct drm_client_modeset_test_priv *priv = test->priv;
+	struct drm_device *drm = priv->drm;
+	struct drm_connector *connector = &priv->connector;
+	struct drm_connector *second = &priv->second_connector;
+	struct drm_display_mode *current_mode;
+	struct drm_client_dev *client;
+	struct drm_mode_set *modeset;
+	struct drm_crtc *crtc;
+	int ret;
+
+	drm->mode_config.max_width = 4096;
+	drm->mode_config.max_height = 4096;
+
+	ret = drmm_connector_init(drm, second, &drm_client_modeset_connector_funcs,
+				  DRM_MODE_CONNECTOR_Unknown, NULL);
+	KUNIT_ASSERT_EQ(test, ret, 0);
+	drm_connector_helper_add(second, &drm_client_modeset_small_helper_funcs);
+
+	crtc = drm_client_modeset_test_add_crtc(test);
+	ret = drm_connector_attach_encoder(second, &priv->encoder);
+	KUNIT_ASSERT_EQ(test, ret, 0);
+
+	drm_mode_config_reset(drm);
+	KUNIT_ASSERT_NOT_NULL(test, connector->state);
+	KUNIT_ASSERT_NOT_NULL(test, crtc->state);
+
+	/* A single 1x1 tile, so drm_client_firmware_config() looks at it first */
+	drm_connector_helper_add(connector, &drm_client_modeset_preferred_helper_funcs);
+	connector->has_tile = true;
+	connector->num_h_tile = 1;
+	connector->num_v_tile = 1;
+	connector->tile_h_size = 1920;
+	connector->tile_v_size = 1080;
+	connector->state->best_encoder = &priv->encoder;
+	connector->state->crtc = crtc;
+	drm_connector_get(connector);
+
+	current_mode = drm_mode_find_dmt(drm, 1920, 1080, 60, false);
+	KUNIT_ASSERT_NOT_NULL(test, current_mode);
+	drm_mode_copy(&crtc->state->mode, current_mode);
+	drm_mode_destroy(drm, current_mode);
+
+	client = kunit_kzalloc(test, sizeof(*client), GFP_KERNEL);
+	KUNIT_ASSERT_NOT_NULL(test, client);
+	client->dev = drm;
+
+	ret = drm_client_modeset_create(client);
+	KUNIT_ASSERT_EQ(test, ret, 0);
+
+	ret = kunit_add_action_or_reset(test, drm_client_modeset_free_wrapper, client);
+	KUNIT_ASSERT_EQ(test, ret, 0);
+
+	ret = drm_client_modeset_probe(client, 320, 240);
+	KUNIT_ASSERT_EQ(test, ret, 0);
+
+	modeset = &client->modesets[0];
+	KUNIT_EXPECT_PTR_EQ(test, modeset->crtc, crtc);
+	KUNIT_ASSERT_EQ(test, modeset->num_connectors, 1);
+	KUNIT_EXPECT_PTR_EQ(test, modeset->connectors[0], second);
+	KUNIT_ASSERT_NOT_NULL(test, modeset->mode);
+	KUNIT_EXPECT_EQ(test, modeset->mode->hdisplay, 320);
+	KUNIT_EXPECT_EQ(test, modeset->mode->vdisplay, 240);
+}
+
+static struct kunit_case drm_test_pick_mode_limits_tests[] = {
+	KUNIT_CASE(drm_test_pick_first_mode_within_limits),
+	KUNIT_CASE(drm_test_pick_cmdline_mode_within_limits),
+	KUNIT_CASE(drm_test_pick_tiled_mode_within_limits),
+	KUNIT_CASE(drm_test_pick_clone_mode_within_limits),
+	KUNIT_CASE(drm_test_firmware_config_tiled_fallback),
+	KUNIT_CASE(drm_test_firmware_config_current_mode),
+	KUNIT_CASE(drm_test_probe_keeps_modes_above_limits),
+	KUNIT_CASE(drm_test_probe_lights_other_connector),
+	{}
+};
+
+static struct kunit_suite drm_test_pick_mode_limits_test_suite = {
+	.name = "drm_test_pick_mode_limits",
+	.init = drm_client_modeset_test_init,
+	.test_cases = drm_test_pick_mode_limits_tests
+};
+
+kunit_test_suite(drm_test_pick_mode_limits_test_suite);
+
 /*
  * This file is included directly by drm_client_modeset.c so we can't
  * use any MODULE_* macro here.
Signature · 1 line
2.55.0
author
#3 lore raw
In reply to Hugo Sales

Hi,

Gentle ping on this series. v2 still merges cleanly onto drm-misc-next (c408466a4dfa)

Could it go in through drm-misc? Ville, on v1 you said the approach should be fine. Would you give it an Acked-by or Reviewed-by?

Thanks,
Hugo