All of lore.kernel.org
 help / color / mirror / Atom feed
From: Maxime Ripard <mripard@kernel.org>
To: Maarten Lankhorst <maarten.lankhorst@linux.intel.com>,
	 Thomas Zimmermann <tzimmermann@suse.de>,
	David Airlie <airlied@gmail.com>,
	 Simona Vetter <simona@ffwll.ch>,
	Andrzej Hajda <andrzej.hajda@intel.com>,
	 Neil Armstrong <neil.armstrong@linaro.org>,
	Robert Foss <rfoss@kernel.org>,
	 Laurent Pinchart <Laurent.pinchart@ideasonboard.com>,
	 Jonas Karlman <jonas@kwiboo.se>,
	Jernej Skrabec <jernej.skrabec@gmail.com>,
	 Jyri Sarha <jyri.sarha@iki.fi>,
	 Tomi Valkeinen <tomi.valkeinen@ideasonboard.com>
Cc: Devarsh Thakkar <devarsht@ti.com>,
	dri-devel@lists.freedesktop.org,
	 Maxime Ripard <mripard@kernel.org>
Subject: [PATCH v9 26/42] drm/tests: kunit: Add a drm_printer helper
Date: Fri, 20 Nov 2026 13:55:18 +0100	[thread overview]
Message-ID: <20261120-drm-state-readout-v9-26-a67c2b45b8ec@kernel.org> (raw)
In-Reply-To: <20261120-drm-state-readout-v9-0-a67c2b45b8ec@kernel.org>

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.

Signed-off-by: Maxime Ripard <mripard@kernel.org>
---
 drivers/gpu/drm/tests/drm_kunit_helpers.c | 78 +++++++++++++++++++++++++++++++
 include/drm/drm_kunit_helpers.h           |  4 ++
 2 files changed, 82 insertions(+)

diff --git a/drivers/gpu/drm/tests/drm_kunit_helpers.c b/drivers/gpu/drm/tests/drm_kunit_helpers.c
index 63d024946500..5fdffbf78245 100644
--- a/drivers/gpu/drm/tests/drm_kunit_helpers.c
+++ b/drivers/gpu/drm/tests/drm_kunit_helpers.c
@@ -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,
 };
 
diff --git a/include/drm/drm_kunit_helpers.h b/include/drm/drm_kunit_helpers.h
index 97d35818f2ce..6aafaeeacf32 100644
--- a/include/drm/drm_kunit_helpers.h
+++ b/include/drm/drm_kunit_helpers.h
@@ -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,

-- 
2.56.1


  parent reply	other threads:[~2026-11-20 12:57 UTC|newest]

Thread overview: 53+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-20 12:54 [PATCH v9 00/42] drm: Implement state readout support Maxime Ripard
2026-11-20 12:54 ` [PATCH v9 01/42] drm/atomic: Add drm_atomic_get_connector_for_encoder_locked() Maxime Ripard
2026-11-20 12:54 ` [PATCH v9 02/42] drm/bridge-helper: Don't unlock the connection mutex after a backoff Maxime Ripard
2026-11-20 12:54 ` [PATCH v9 03/42] drm/tests: hdmi-state-helper: Handle EDEADLK in the 420-only test Maxime Ripard
2026-11-20 12:54 ` [PATCH v9 04/42] drm/tests: hdmi-state-helper: Handle EDEADLK in the color format test Maxime Ripard
2026-11-20 12:54 ` [PATCH v9 05/42] drm/colorop: Export drm_atomic_helper_colorop_create_state() Maxime Ripard
2026-11-20 12:54 ` [PATCH v9 06/42] drm/atomic: Fix private obj error handling Maxime Ripard
2026-11-20 12:54 ` [PATCH v9 07/42] drm/atomic: Add drm_private_obj name Maxime Ripard
2026-11-20 12:55 ` [PATCH v9 08/42] drm/bridge: Switch print_state to use bridge name Maxime Ripard
2026-11-20 12:55 ` [PATCH v9 09/42] drm/bridge: Add drm_private_obj_is_bridge() Maxime Ripard
2026-11-20 12:55 ` [PATCH v9 10/42] drm/fb-dma-helper: Handle framebuffers without a GEM object Maxime Ripard
2026-11-20 12:55 ` [PATCH v9 11/42] drm/gem-atomic-helper: " Maxime Ripard
2026-11-20 12:55 ` [PATCH v9 12/42] drm/atomic: Export drm_atomic_*_print_state Maxime Ripard
2026-11-20 12:55 ` [PATCH v9 13/42] drm/atomic_sro: Create drm_atomic_sro_state container Maxime Ripard
2026-11-20 12:55 ` [PATCH v9 14/42] drm/atomic_sro: Create kernel parameter to force or disable readout Maxime Ripard
2026-11-20 12:55 ` [PATCH v9 15/42] drm/atomic_sro: Add atomic state readout infrastructure Maxime Ripard
2026-11-20 12:55 ` [PATCH v9 16/42] drm/atomic_sro: Create documentation Maxime Ripard
2026-11-20 12:55 ` [PATCH v9 17/42] drm/bridge: Handle bridges with hardware state readout Maxime Ripard
2026-11-20 13:18   ` sashiko-bot
2026-11-20 12:55 ` [PATCH v9 18/42] drm/mode-config: Read out hardware state in drm_mode_config_create_initial_state Maxime Ripard
2026-11-20 12:55 ` [PATCH v9 19/42] drm/atomic_sro: Provide helpers to implement hardware state readout Maxime Ripard
2026-11-20 13:17   ` sashiko-bot
2026-11-20 12:55 ` [PATCH v9 20/42] drm/mode-config: Introduce hw_reset hook Maxime Ripard
2026-11-20 12:55 ` [PATCH v9 21/42] drm/modeset-helper: Add drm_mode_config_helper_hw_reset() Maxime Ripard
2026-11-20 12:55 ` [PATCH v9 22/42] drm/atomic_helper: Compare actual and readout states once the commit is done Maxime Ripard
2026-11-20 13:17   ` sashiko-bot
2026-11-20 12:55 ` [PATCH v9 23/42] drm/atomic-state-helper: Provide comparison macros Maxime Ripard
2026-11-20 12:55 ` [PATCH v9 24/42] drm/atomic-state-helper: Provide atomic_compare_state helpers Maxime Ripard
2026-11-20 13:14   ` sashiko-bot
2026-11-20 12:55 ` [PATCH v9 25/42] drm/atomic-helper: Document State Read-Out as a state initialization approach Maxime Ripard
2026-11-20 12:55 ` Maxime Ripard [this message]
2026-11-20 12:55 ` [PATCH v9 27/42] drm/tests: Add tests for the atomic state readout Maxime Ripard
2026-11-20 12:55 ` [PATCH v9 28/42] drm/encoder: Create atomic_sro_get_current_crtc hook Maxime Ripard
2026-11-20 12:55 ` [PATCH v9 29/42] drm/bridge: display-connector: Implement readout support Maxime Ripard
2026-11-20 12:55 ` [PATCH v9 30/42] drm/bridge_connector: Implement hw readout for connector Maxime Ripard
2026-11-20 12:55 ` [PATCH v9 31/42] drm/tidss: Implement hw_reset Maxime Ripard
2026-11-20 12:55 ` [PATCH v9 32/42] drm/tidss: crtc: Implement hardware state readout Maxime Ripard
2026-11-20 13:14   ` sashiko-bot
2026-11-20 12:55 ` [PATCH v9 33/42] drm/tidss: encoder: " Maxime Ripard
2026-11-20 12:55 ` [PATCH v9 34/42] drm/tidss: kms: Hook up " Maxime Ripard
2026-11-20 13:25   ` sashiko-bot
2026-11-20 12:55 ` [PATCH v9 35/42] drm/tidss: plane: Implement " Maxime Ripard
2026-11-20 13:28   ` sashiko-bot
2026-11-20 12:55 ` [PATCH v9 36/42] drm/tidss: dispc: Add hardware state readout helpers Maxime Ripard
2026-11-20 13:17   ` sashiko-bot
2026-11-20 12:55 ` [PATCH v9 37/42] drm/tidss: encoder: Implement atomic_sro_get_current_crtc Maxime Ripard
2026-11-20 12:55 ` [PATCH v9 38/42] drm/bridge: sii902x: Implement hw state readout Maxime Ripard
2026-11-20 13:26   ` sashiko-bot
2026-11-20 12:55 ` [PATCH v9 39/42] drm/panel: Implement state readout support Maxime Ripard
2026-11-20 12:55 ` [PATCH DO NOT MERGE v9 40/42] fixup! drm/tidss: dispc: Add hardware state readout helpers Maxime Ripard
2026-11-20 12:55 ` [PATCH DO NOT MERGE v9 41/42] drm/tidss: oldi: Implement hardware state readout Maxime Ripard
2026-11-20 13:34   ` sashiko-bot
2026-11-20 12:55 ` [PATCH HACK v9 42/42] drm/panel: Prepare and enable panels read out as active Maxime Ripard

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261120-drm-state-readout-v9-26-a67c2b45b8ec@kernel.org \
    --to=mripard@kernel.org \
    --cc=Laurent.pinchart@ideasonboard.com \
    --cc=airlied@gmail.com \
    --cc=andrzej.hajda@intel.com \
    --cc=devarsht@ti.com \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=jernej.skrabec@gmail.com \
    --cc=jonas@kwiboo.se \
    --cc=jyri.sarha@iki.fi \
    --cc=maarten.lankhorst@linux.intel.com \
    --cc=neil.armstrong@linaro.org \
    --cc=rfoss@kernel.org \
    --cc=simona@ffwll.ch \
    --cc=tomi.valkeinen@ideasonboard.com \
    --cc=tzimmermann@suse.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.