Re: [PATCH i-g-t 6/6] tests/intel/kms_usb4_switch: Add USB4 switch test suite

Jani Nikula
2026-04-14T09:27:58Z

On Thu, 09 Apr 2026, Kunal Joshi <kunal1.joshi@intel.com> wrote:
> Add a comprehensive test suite for USB4/Thunderbolt dock/undock and
> port switching scenarios using the Microsoft USB4 Switch 3141:
>
> - dock-undock: Basic dock/undock cycles with display verification
> - dock-undock-sr: Dock/undock with suspend/resume stability
> - dock-undock-during-suspend: Dock while system is suspended
> - switch: Port-to-port switching with display verification
> - switch-sr: Port switching with suspend/resume stability
> - switch-during-suspend: Port switch during suspend via HW delay
>
> v2: Make timeout const (Arun)
>
> Signed-off-by: Kunal Joshi <kunal1.joshi@intel.com>
> ---
> tests/intel/kms_usb4_switch.c | 1254 +++++++++++++++++++++++++++++++++
> tests/meson.build | 2 +
> 2 files changed, 1256 insertions(+)
> create mode 100644 tests/intel/kms_usb4_switch.c
>
> diff --git a/tests/intel/kms_usb4_switch.c b/tests/intel/kms_usb4_switch.c
> new file mode 100644
> index 000000000..d26cacaf1
> --- /dev/null
> +++ b/tests/intel/kms_usb4_switch.c
> @@ -0,0 +1,1254 @@
> +// SPDX-License-Identifier: MIT
> +/*
> + * Copyright © 2026 Intel Corporation
> + */
> +
> +/**
> + * TEST: kms usb4 switch
> + * Category: Display
> + * Description: USB4/Thunderbolt dock/undock and port switching tests
> + * Driver requirement: i915, xe
> + * Mega feature: General Display Features
> + *
> + * SUBTEST: dock-undock
> + * Description: Test dock/undock cycles with display verification.
> + * Verifies hotplug events, EDID serial matching, modeset with
> + * max non-joiner mode, and pipe CRC stability.
> + *
> + * SUBTEST: dock-undock-sr
> + * Description: Test dock/undock with suspend/resume stability.
> + * Docks, verifies displays with modeset and CRC, suspends/resumes,
> + * then verifies displays still produce valid CRC after resume.
> + *
> + * SUBTEST: dock-during-suspend
> + * Description: Test docking while system is suspended.
> + * Simulates user plugging into a suspended laptop by triggering
> + * dock during suspend using hardware delay command. Verifies
> + * modeset and pipe CRC after resume.
> + *
> + * SUBTEST: undock-during-suspend
> + * Description: Test undocking while system is suspended.
> + * Docks first, verifies displays, then schedules a delayed
> + * undock during suspend. After resume, verifies all port
> + * displays have been removed.
> + *
> + * SUBTEST: switch
> + * Description: Test switching between USB4 switch ports.
> + * Verifies hotplug events, display verification with modeset
> + * and pipe CRC when switching from one port to another.
> + *
> + * SUBTEST: switch-sr
> + * Description: Test port switching with suspend/resume stability.
> + * Switches ports, verifies displays with modeset and CRC,
> + * suspends/resumes, then verifies CRC stability after resume.
> + *
> + * SUBTEST: switch-during-suspend
> + * Description: Test port switching during suspend using hardware delay.
> + * Schedules a delayed switch, suspends system, switch occurs during
> + * suspend, then verifies modeset and pipe CRC on new port after resume.
> + */
> +
> +#include <ctype.h>
> +#include <sys/poll.h>
> +
> +#include "igt.h"
> +#include "igt_edid.h"
> +#include "igt_kms.h"
> +#include "igt_pipe_crc.h"
> +#include "igt_usb4_switch.h"
> +#include "igt_connector_helper.h"
> +#include "kms_joiner_helper.h"
> +
> +/*
> + * Extended timeout for switch operations.
> + * Port switching requires longer stabilization time than simple dock/undock
> + * due to hardware state transitions and MST topology rebuilds.
> + */
> +#define USB4_SWITCH_TIMEOUT_S 20
> +
> +/* Number of CRC samples for stability check (solid color FB should be stable) */
> +#define CRC_STABILITY_SAMPLES 3
> +
> +/* Maximum displays expected on a single USB4 switch port (MST hub) */
> +#define MAX_DISPLAYS_PER_PORT 4
> +
> +/* Number of reprobe cycles for MST topology stabilization after resume */
> +#define MST_STABILIZE_REPROBE_COUNT 10
> +
> +/* Inter-reprobe delay in microseconds (500 ms) */
> +#define MST_STABILIZE_DELAY_US 500000
> +
> +/*
> + * Iteration helpers for dynamic subtest generation.
> + * Skip ports (or pairs) that have no displays configured.
> + *
> + * for_each_usb4_port_pair iterates adjacent cyclic pairs (0->1, 1->2, ...,
> + * N-1->0). For the 2-port Switch 3141 this covers the only possible pair.
> + * If the hardware grows beyond 2 ports, consider generating all distinct
> + * pairs instead.
> + */
> +#define for_each_usb4_port(sw, count, p, pcfg) \
> + for ((p) = 0, \
> + (pcfg) = usb4switch_get_port_config((sw), 0); \
> + (p) < (count) && (pcfg) && (pcfg)->display_count > 0; \
> + (pcfg) = usb4switch_get_port_config((sw), ++(p)))
> +
> +#define for_each_usb4_port_pair(sw, count, p, pa, pb) \
> + for ((p) = 0, \
> + (pa) = usb4switch_get_port_config((sw), 0), \
> + (pb) = usb4switch_get_port_config((sw), 1 % (count)); \
> + (p) < (count) && (pa) && (pb) && \
> + (pa)->display_count > 0 && (pb)->display_count > 0; \
> + ++(p), \
> + (pa) = usb4switch_get_port_config((sw), (p)), \
> + (pb) = usb4switch_get_port_config((sw), ((p) + 1) % (count)))

These are difficult to understand, and more so because the param names
are too short and don't help at all.

> +
> +typedef struct {
> + int drm_fd;
> + igt_display_t display;
> + struct usb4switch *sw;
> + struct udev_monitor *hotplug_mon;
> + int max_dotclock;
> + uint32_t master_pipes;
> + uint32_t valid_pipes;
> +} data_t;
> +
> +/*
> + * Per-display modeset state, used by the composable display building blocks.
> + * Holds everything needed to arm, commit, collect CRC, and disarm one display.
> + */
> +struct display_ctx {
> + uint32_t conn_id;
> + igt_output_t *output;
> + igt_plane_t *primary;
> + struct igt_fb fb;
> + drmModeModeInfo mode;
> + bool valid;
> +};
> +
> +/*
> + * find_output_by_id - Find an igt_output_t by DRM connector ID.
> + */
> +static igt_output_t *find_output_by_id(igt_display_t *display,
> + uint32_t connector_id)
> +{
> + int i;
> +
> + for (i = 0; i < display->n_outputs; i++) {

for_each_output()

> + if (display->outputs[i].id == connector_id)
> + return &display->outputs[i];
> + }
> +
> + return NULL;
> +}
> +
> +/*
> + * select_max_non_joiner_mode - Pick the largest mode that fits a single pipe.
> + *
> + * "Non-joiner" means hdisplay <= max_pipe_hdisplay AND clock <= max_dotclock.
> + * Among qualifying modes, pick by:
> + * 1. Highest pixel area (hdisplay * vdisplay)
> + * 2. Highest vrefresh (tie-break)
> + * 3. Highest clock (tie-break)
> + *
> + * If max_dotclock is 0 (debugfs unavailable), only hdisplay is checked.
> + *
> + * TODO: Remove this filter when joiner CRC collection is supported;
> + * the pipe allocator already handles joiner pipe counts.
> + *
> + * Returns: Pointer to a function-static copy of the best mode, or NULL
> + * if none found. Not reentrant — single-threaded callers only.
> + */
> +static drmModeModeInfo *select_max_non_joiner_mode(int drm_fd,
> + igt_output_t *output,
> + int max_dotclock)
> +{
> + static drmModeModeInfo best;
> + drmModeConnector *conn = output->config.connector;
> + uint64_t best_area = 0;
> + uint32_t best_vrefresh = 0;
> + int best_clock = 0;
> + bool found = false;
> + int i;
> +
> + if (!conn || conn->count_modes == 0)
> + return NULL;
> +
> + for (i = 0; i < conn->count_modes; i++) {

for_each_connector_mode()

--
Jani Nikula, Intel