diff --git a/lib/igt_kms.c b/lib/igt_kms.c
index 285c1b6..f771f29 100644
--- a/lib/igt_kms.c
+++ b/lib/igt_kms.c
@@ -277,6 +277,38 @@ int kmstest_get_pipe_from_crtc_id(int fd, int crtc_id)
 	return pfci.pipe;
 }
 
+/**
+ * kmstest_find_crtc_for_connector:
+ * @fd: DRM fd
+ * @res: libdrm resources pointer
+ * @connector: libdrm connector pointer
+ * @crtc_blacklist_idx_mask: a mask of CRTC indexes that we can't return
+ *
+ * Returns: the CRTC ID for a CRTC that fits the connector, otherwise it asserts
+ * false and never returns. The blacklist mask can be used in case you have
+ * CRTCs that are already in use by other connectors.
+ */
+uint32_t kmstest_find_crtc_for_connector(int fd, drmModeRes *res,
+					 drmModeConnector *connector,
+					 uint32_t crtc_blacklist_idx_mask)
+{
+	drmModeEncoder *e;
+	uint32_t possible_crtcs;
+	int i, j;
+
+	for (i = 0; i < connector->count_encoders; i++) {
+		e = drmModeGetEncoder(fd, connector->encoders[i]);
+		possible_crtcs = e->possible_crtcs & ~crtc_blacklist_idx_mask;
+		drmModeFreeEncoder(e);
+
+		for (j = 0; possible_crtcs >> j; j++)
+			if (possible_crtcs & (1 << j))
+				return res->crtcs[j];
+	}
+
+	igt_assert(false);
+}
+
 /*
  * Returns: the previous mode, or KD_GRAPHICS if no /dev/tty0 was
  * found and nothing was done.
diff --git a/lib/igt_kms.h b/lib/igt_kms.h
index 2cb7c1f..5744ed0 100644
--- a/lib/igt_kms.h
+++ b/lib/igt_kms.h
@@ -151,6 +151,9 @@ bool kmstest_get_property(int drm_fd, uint32_t object_id, uint32_t object_type,
 			  drmModePropertyPtr *prop);
 void kmstest_unset_all_crtcs(int drm_fd, drmModeResPtr resources);
 int kmstest_get_crtc_idx(drmModeRes *res, uint32_t crtc_id);
+uint32_t kmstest_find_crtc_for_connector(int fd, drmModeRes *res,
+					 drmModeConnector *connector,
+					 uint32_t crtc_blacklist_idx_mask);
 
 /*
  * A small modeset API
diff --git a/tests/kms_draw_crc.c b/tests/kms_draw_crc.c
index 3f80174..9539fe4 100644
--- a/tests/kms_draw_crc.c
+++ b/tests/kms_draw_crc.c
@@ -58,25 +58,26 @@ struct modeset_params ms;
 static void find_modeset_params(void)
 {
 	int i;
-	uint32_t connector_id = 0, crtc_id;
+	uint32_t crtc_id;
+	drmModeConnectorPtr connector = NULL;
 	drmModeModeInfoPtr mode = NULL;
 
 	for (i = 0; i < drm_res->count_connectors; i++) {
 		drmModeConnectorPtr c = drm_connectors[i];
 
 		if (c->count_modes) {
-			connector_id = c->connector_id;
+			connector = c;
 			mode = &c->modes[0];
 			break;
 		}
 	}
-	igt_require(connector_id);
+	igt_require(connector);
 
-	crtc_id = drm_res->crtcs[0];
-	igt_assert(crtc_id);
+	crtc_id = kmstest_find_crtc_for_connector(drm_fd, drm_res, connector,
+						  0);
 	igt_assert(mode);
 
-	ms.connector_id = connector_id;
+	ms.connector_id = connector->connector_id;
 	ms.crtc_id = crtc_id;
 	ms.mode = mode;
 
diff --git a/tests/kms_fbcon_fbt.c b/tests/kms_fbcon_fbt.c
index 3c93378..586cdf5 100644
--- a/tests/kms_fbcon_fbt.c
+++ b/tests/kms_fbcon_fbt.c
@@ -114,7 +114,7 @@ static void set_mode_for_one_screen(struct drm_info *drm, struct igt_fb *fb,
 				    connector_possible_fn connector_possible)
 {
 	int i, rc;
-	uint32_t connector_id = 0, crtc_id;
+	uint32_t crtc_id;
 	drmModeModeInfoPtr mode;
 	uint32_t buffer_id;
 	drmModeConnectorPtr c = NULL;
@@ -124,14 +124,14 @@ static void set_mode_for_one_screen(struct drm_info *drm, struct igt_fb *fb,
 
 		if (c->connection == DRM_MODE_CONNECTED && c->count_modes &&
 		    connector_possible(c)) {
-			connector_id = c->connector_id;
 			mode = &c->modes[0];
 			break;
 		}
 	}
-	igt_require_f(connector_id, "No connector available\n");
+	igt_require_f(i < drm->res->count_connectors,
+		      "No connector available\n");
 
-	crtc_id = drm->res->crtcs[0];
+	crtc_id = kmstest_find_crtc_for_connector(drm->fd, drm->res, c, 0);
 
 	buffer_id = igt_create_fb(drm->fd, mode->hdisplay, mode->vdisplay,
 				  DRM_FORMAT_XRGB8888,
@@ -142,8 +142,8 @@ static void set_mode_for_one_screen(struct drm_info *drm, struct igt_fb *fb,
 		 mode->hdisplay, mode->vdisplay,
 		 kmstest_connector_type_str(c->connector_type));
 
-	rc = drmModeSetCrtc(drm->fd, crtc_id, buffer_id, 0, 0, &connector_id, 1,
-			    mode);
+	rc = drmModeSetCrtc(drm->fd, crtc_id, buffer_id, 0, 0,
+			    &c->connector_id, 1, mode);
 	igt_assert_eq(rc, 0);
 }
 
diff --git a/tests/kms_frontbuffer_tracking.c b/tests/kms_frontbuffer_tracking.c
index 47a37f4..b45b240 100644
--- a/tests/kms_frontbuffer_tracking.c
+++ b/tests/kms_frontbuffer_tracking.c
@@ -374,10 +374,11 @@ static void print_mode_info(const char *screen, struct modeset_params *params)
 {
 	drmModeConnectorPtr c = get_connector(params->connector_id);
 
-	igt_info("%s screen: %s %s\n",
+	igt_info("%s screen: %s %s, crtc %d\n",
 		 screen,
 		 kmstest_connector_type_str(c->connector_type),
-		 params->mode->name);
+		 params->mode->name,
+		 params->crtc_id);
 }
 
 static void init_mode_params(struct modeset_params *params, uint32_t crtc_id,
@@ -484,6 +485,7 @@ static bool init_modeset_cached_params(void)
 {
 	drmModeConnectorPtr prim_connector = NULL, scnd_connector = NULL;
 	drmModeModeInfoPtr prim_mode = NULL, scnd_mode = NULL;
+	uint32_t prim_crtc_id, scnd_crtc_id;
 
 	/*
 	 * We have this problem where PSR is only present on eDP monitors and
@@ -507,7 +509,9 @@ static bool init_modeset_cached_params(void)
 	find_connector(false, false, prim_connector->connector_id,
 		       &scnd_connector, &scnd_mode);
 
-	init_mode_params(&prim_mode_params, drm.res->crtcs[0],
+	prim_crtc_id = kmstest_find_crtc_for_connector(drm.fd, drm.res,
+						       prim_connector, 0);
+	init_mode_params(&prim_mode_params, prim_crtc_id,
 			 prim_connector, prim_mode);
 	print_mode_info("Primary", &prim_mode_params);
 
@@ -517,7 +521,10 @@ static bool init_modeset_cached_params(void)
 	}
 
 	igt_assert(drm.res->count_crtcs >= 2);
-	init_mode_params(&scnd_mode_params, drm.res->crtcs[1],
+	scnd_crtc_id = kmstest_find_crtc_for_connector(drm.fd, drm.res,
+						      scnd_connector,
+			1 << kmstest_get_crtc_idx(drm.res, prim_crtc_id));
+	init_mode_params(&scnd_mode_params, scnd_crtc_id,
 			 scnd_connector, scnd_mode);
 	print_mode_info("Secondary", &scnd_mode_params);
 
@@ -2577,10 +2584,10 @@ static bool prim_plane_disabled(void)
 {
 	int i, rc;
 	bool disabled, found = false;
+	int crtc_idx = kmstest_get_crtc_idx(drm.res, prim_mode_params.crtc_id);
 
 	for (i = 0; i < drm.plane_res->count_planes; i++) {
-		/* We just pick the first CRTC for the primary plane. */
-		if ((drm.planes[i]->possible_crtcs & 0x1) &&
+		if ((drm.planes[i]->possible_crtcs & (1 << crtc_idx)) &&
 		    drm.plane_types[i] == DRM_PLANE_TYPE_PRIMARY) {
 			found = true;
 			disabled = (drm.planes[i]->crtc_id == 0);
@@ -2688,6 +2695,8 @@ static void scaledprimary_subtest(const struct test_mode *t)
 	struct modeset_params *params = pick_params(t);
 	int i, rc;
 	uint32_t plane_id;
+	int prim_crtc_idx = kmstest_get_crtc_idx(drm.res,
+						 prim_mode_params.crtc_id);
 
 	igt_require_f(intel_gen(intel_get_drm_devid(drm.fd)) >= 9,
 		      "Can't test primary plane scaling before gen 9\n");
@@ -2717,7 +2726,7 @@ static void scaledprimary_subtest(const struct test_mode *t)
 			 pick_color(&new_fb, COLOR_MAGENTA));
 
 	for (i = 0; i < drm.plane_res->count_planes; i++)
-		if ((drm.planes[i]->possible_crtcs & 1) &&
+		if ((drm.planes[i]->possible_crtcs & (1 << prim_crtc_idx)) &&
 		    drm.plane_types[i] == DRM_PLANE_TYPE_PRIMARY)
 			plane_id = drm.planes[i]->plane_id;
 
diff --git a/tests/pm_lpsp.c b/tests/pm_lpsp.c
index 4cedeff..bfe5828 100644
--- a/tests/pm_lpsp.c
+++ b/tests/pm_lpsp.c
@@ -65,7 +65,8 @@ static void edp_subtest(int drm_fd, drmModeResPtr drm_res,
 			bool use_panel_fitter)
 {
 	int i, rc;
-	uint32_t connector_id = 0, crtc_id = 0, buffer_id = 0;
+	uint32_t crtc_id = 0, buffer_id = 0;
+	drmModeConnectorPtr connector = NULL;
 	drmModeModeInfoPtr mode = NULL;
 	drmModeModeInfo std_1024_mode = {
 		.clock = 65000,
@@ -96,12 +97,12 @@ static void edp_subtest(int drm_fd, drmModeResPtr drm_res,
 			continue;
 
 		if (!use_panel_fitter && c->count_modes) {
-			connector_id = c->connector_id;
+			connector = c;
 			mode = &c->modes[0];
 			break;
 		}
 		if (use_panel_fitter) {
-			connector_id = c->connector_id;
+			connector = c;
 
 			/* This is one of the modes Xorg creates for panels, so
 			 * it should work just fine. Notice that Gens that
@@ -116,18 +117,18 @@ static void edp_subtest(int drm_fd, drmModeResPtr drm_res,
 			break;
 		}
 	}
-	igt_require(connector_id);
+	igt_require(connector);
 
-	crtc_id = drm_res->crtcs[0];
+	crtc_id = kmstest_find_crtc_for_connector(drm_fd, drm_res, connector,
+						  0);
 	buffer_id = create_fb(drm_fd, mode->hdisplay, mode->vdisplay);
 
-	igt_assert(crtc_id);
 	igt_assert(buffer_id);
-	igt_assert(connector_id);
+	igt_assert(connector);
 	igt_assert(mode);
 
-	rc = drmModeSetCrtc(drm_fd, crtc_id, buffer_id, 0, 0, &connector_id, 1,
-			    mode);
+	rc = drmModeSetCrtc(drm_fd, crtc_id, buffer_id, 0, 0,
+			    &connector->connector_id, 1, mode);
 	igt_assert_eq(rc, 0);
 
 	if (use_panel_fitter) {
@@ -144,7 +145,8 @@ static void non_edp_subtest(int drm_fd, drmModeResPtr drm_res,
 			    drmModeConnectorPtr *drm_connectors)
 {
 	int i, rc;
-	uint32_t connector_id = 0, crtc_id = 0, buffer_id = 0;
+	uint32_t crtc_id = 0, buffer_id = 0;
+	drmModeConnectorPtr connector = NULL;
 	drmModeModeInfoPtr mode = NULL;
 
 	kmstest_unset_all_crtcs(drm_fd, drm_res);
@@ -158,23 +160,22 @@ static void non_edp_subtest(int drm_fd, drmModeResPtr drm_res,
 			continue;
 
 		if (c->count_modes) {
-			connector_id = c->connector_id;
+			connector = c;
 			mode = &c->modes[0];
 			break;
 		}
 	}
-	igt_require(connector_id);
+	igt_require(connector);
 
-	crtc_id = drm_res->crtcs[0];
+	crtc_id = kmstest_find_crtc_for_connector(drm_fd, drm_res, connector,
+						  0);
 	buffer_id = create_fb(drm_fd, mode->hdisplay, mode->vdisplay);
 
-	igt_assert(crtc_id);
 	igt_assert(buffer_id);
-	igt_assert(connector_id);
 	igt_assert(mode);
 
-	rc = drmModeSetCrtc(drm_fd, crtc_id, buffer_id, 0, 0, &connector_id, 1,
-			    mode);
+	rc = drmModeSetCrtc(drm_fd, crtc_id, buffer_id, 0, 0,
+			    &connector->connector_id, 1, mode);
 	igt_assert_eq(rc, 0);
 
 	igt_assert(!lpsp_is_enabled(drm_fd));
diff --git a/tests/pm_rpm.c b/tests/pm_rpm.c
index eeaf324..0b9c6b7 100644
--- a/tests/pm_rpm.c
+++ b/tests/pm_rpm.c
@@ -226,27 +226,6 @@ static void disable_or_dpms_all_screens(struct mode_set_data *data, bool dpms)
 	igt_assert(wait_for_suspended()); \
 } while (0)
 
-static uint32_t find_crtc_for_connector(drmModeResPtr res,
-					drmModeConnectorPtr connector)
-{
-	drmModeEncoderPtr e;
-	uint32_t crtc_id = 0;
-	int i, j;
-
-	for (i = 0; i < connector->count_encoders && !crtc_id; i++) {
-		e = drmModeGetEncoder(drm_fd, connector->encoders[i]);
-
-		for (j = 0; (e->possible_crtcs >> j) && !crtc_id; j++)
-			if (e->possible_crtcs & (1 << j))
-				crtc_id = res->crtcs[j];
-
-		drmModeFreeEncoder(e);
-	}
-
-	igt_assert(crtc_id);
-	return crtc_id;
-}
-
 static bool init_modeset_params_for_type(struct mode_set_data *data,
 					 struct modeset_params *params,
 					 enum screen_type type)
@@ -280,7 +259,8 @@ static bool init_modeset_params_for_type(struct mode_set_data *data,
 			      DRM_FORMAT_XRGB8888, LOCAL_DRM_FORMAT_MOD_NONE,
 			      &params->fb);
 
-	params->crtc_id = find_crtc_for_connector(data->res, connector);
+	params->crtc_id = kmstest_find_crtc_for_connector(drm_fd, data->res,
+							  connector, 0);
 	params->connector_id = connector->connector_id;
 	params->mode = mode;
 
diff --git a/tools/intel_residency.c b/tools/intel_residency.c
index 74074c5..116215a 100644
--- a/tools/intel_residency.c
+++ b/tools/intel_residency.c
@@ -249,20 +249,21 @@ static void draw_rect(struct igt_fb *fb, enum igt_draw_method method,
 static void setup_modeset(void)
 {
 	int i;
+	drmModeConnectorPtr connector;
 
 	for (i = 0; i < drm.res->count_connectors; i++) {
-		drmModeConnectorPtr c = drm.connectors[i];
+		connector = drm.connectors[i];
 
-		if (c->connection == DRM_MODE_CONNECTED &&
-		    c->count_modes > 0) {
-			modeset.connector_id = c->connector_id;
-			modeset.mode = &c->modes[0];
+		if (connector->connection == DRM_MODE_CONNECTED &&
+		    connector->count_modes > 0)
 			break;
-		}
 	}
 	igt_assert(i < drm.res->count_connectors);
 
-	modeset.crtc_id = drm.res->crtcs[0];
+	modeset.connector_id = connector->connector_id;
+	modeset.mode = &connector->modes[0];
+	modeset.crtc_id = kmstest_find_crtc_for_connector(drm.fd, drm.res,
+							  connector, 0);
 
 	for (i = 0; i < 2; i++) {
 		igt_create_fb(drm.fd, modeset.mode->hdisplay,
