[PATCH i-g-t 5/5] lib/kms: add igt_random_crtc() and use it in gem_eio

Jani Nikula
2026-03-06T13:24:32Z

Add a helper for getting a random valid CRTC. There's only one user, and
perhaps there shouldn't be more as it's not deterministic, but avoid the
igt_crtc_for_pipe() usage in tests.

Collect the valid CRTCs in an array first to get away with a single
random selection and O(N) behaviour. Checking the validity after the
random selection on all CRTCs could take several rounds, and indeed the
compiler informs "warning: function might be candidate for attribute
‘noreturn’ [-Wsuggest-attribute=noreturn]".

Signed-off-by: Jani Nikula <jani.nikula@intel.com>
---
lib/igt_kms.c | 20 ++++++++++++++++++++
lib/igt_kms.h | 1 +
tests/intel/gem_eio.c | 7 +------
3 files changed, 22 insertions(+), 6 deletions(-)

diff --git a/lib/igt_kms.c b/lib/igt_kms.c
index 240200a98772..1b89a3e17d4f 100644
--- a/lib/igt_kms.c
+++ b/lib/igt_kms.c
@@ -8040,3 +8040,23 @@ igt_crtc_t *igt_next_crtc(igt_display_t *display, igt_crtc_t *crtc)

return NULL;
}
+
+/*
+ * igt_random_crtc:
+ * @display: pointer to igt_display_t
+ *
+ * Returns: A random CRTC on the device
+ */
+igt_crtc_t *igt_random_crtc(igt_display_t *display)
+{
+ igt_crtc_t *crtcs[IGT_MAX_PIPES];
+ igt_crtc_t *crtc;
+ int n = 0;
+
+ for_each_crtc(display, crtc)
+ crtcs[n++] = crtc;
+
+ igt_skip_on_f(!n, "No CRTCs on device\n");
+
+ return crtcs[rand() % n];
+}
diff --git a/lib/igt_kms.h b/lib/igt_kms.h
index 23169e87ff2d..557b8eedca4d 100644
--- a/lib/igt_kms.h
+++ b/lib/igt_kms.h
@@ -586,6 +586,7 @@ igt_crtc_t *igt_crtc_for_crtc_id(igt_display_t *display, uint32_t crtc_id);
igt_crtc_t *igt_first_crtc(igt_display_t *display);
igt_crtc_t *igt_first_crtc_with_single_output(igt_display_t *display, igt_output_t **ret_output);
igt_crtc_t *igt_next_crtc(igt_display_t *display, igt_crtc_t *crtc);
+igt_crtc_t *igt_random_crtc(igt_display_t *display);

uint32_t igt_crtc_get_vbl_flag(igt_crtc_t *crtc);
unsigned int igt_crtc_get_vblank(igt_crtc_t *crtc, unsigned int flags);
diff --git a/tests/intel/gem_eio.c b/tests/intel/gem_eio.c
index 546fff14bd5b..729aaf7ccd30 100644
--- a/tests/intel/gem_eio.c
+++ b/tests/intel/gem_eio.c
@@ -1033,13 +1033,8 @@ static void display_helper(igt_display_t *dpy, int *done)
igt_plane_t *primary;
igt_output_t *output;
igt_crtc_t *crtc;
- int pipe;

- pipe = rand() % igt_display_n_crtcs(dpy);
-
- crtc = igt_crtc_for_pipe(dpy, pipe);
- if (!crtc || !crtc->valid)
- continue;
+ crtc = igt_random_crtc(dpy);

output = igt_get_single_output_for_crtc(crtc);
if (!output)
--
2.47.3