2 * Permission is hereby granted, free of charge, to any person obtaining a
3 * copy of this software and associated documentation files (the "Software"),
4 * to deal in the Software without restriction, including without limitation
5 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
6 * and/or sell copies of the Software, and to permit persons to whom the
7 * Software is furnished to do so, subject to the following conditions:
9 * The above copyright notice and this permission notice shall be included in
10 * all copies or substantial portions of the Software.
12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
15 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
16 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
17 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * Imre Deak <imre.deak@intel.com>
36 #include "intel_bufmgr.h"
37 #include "intel_batchbuffer.h"
41 #define MAX_CONNECTORS 10
44 /* max combinations with repetitions */
45 #define MAX_COMBINATION_COUNT \
46 (MAX_CONNECTORS * MAX_CONNECTORS * MAX_CONNECTORS)
47 #define MAX_COMBINATION_ELEMS MAX_CRTCS
50 static drmModeRes *drm_resources;
51 static int filter_test_id;
54 const drmModeModeInfo mode_640_480 = {
69 .flags = DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC,
75 TEST_SINGLE_CRTC_CLONE = 0x04,
76 TEST_EXCLUSIVE_CRTC_CLONE = 0x08,
81 enum test_flags flags;
82 drmModeRes *resources;
85 struct connector_config {
86 drmModeConnector *connector;
89 drmModeModeInfo default_mode;
97 struct connector_config *cconfs;
98 struct igt_fb fb_info;
102 static bool drm_mode_equal(drmModeModeInfo *m1, drmModeModeInfo *m2)
104 #define COMP(x) do { if (m1->x != m2->x) return false; } while (0)
120 static bool connector_supports_mode(drmModeConnector *connector,
121 drmModeModeInfo *mode)
125 for (i = 0; i < connector->count_modes; i++)
126 if (drm_mode_equal(&connector->modes[i], mode))
132 static bool crtc_supports_mode(struct crtc_config *crtc, drmModeModeInfo *mode)
136 for (i = 0; i < crtc->connector_count; i++) {
137 if (!connector_supports_mode(crtc->cconfs[i].connector, mode))
144 static int paint_fb(struct igt_fb *fb, const char *test_name,
145 const char **crtc_str, int crtc_count, int current_crtc_idx)
151 cr = igt_get_cairo_ctx(drm_fd, fb);
153 igt_paint_test_pattern(cr, fb->width, fb->height);
155 cairo_move_to(cr, fb->width / 2, fb->height / 2);
156 cairo_set_font_size(cr, 24);
157 igt_cairo_printf_line(cr, align_hcenter, 40, "%s", test_name);
159 cairo_get_current_point(cr, &x, &y);
160 cairo_move_to(cr, 60, y);
162 for (i = 0; i < crtc_count; i++) {
163 if (i == current_crtc_idx) {
164 cairo_get_current_point(cr, &x, &y);
165 cairo_move_to(cr, x - 20, y);
166 igt_cairo_printf_line(cr, align_right, 20, "X");
167 cairo_move_to(cr, x, y);
169 igt_cairo_printf_line(cr, align_left, 20, "%s",
178 static void create_fb_for_crtc(struct crtc_config *crtc,
179 struct igt_fb *fb_info)
188 enable_tiling = false;
189 fb_id = igt_create_fb(drm_fd, crtc->mode.hdisplay,
191 igt_bpp_depth_to_drm_format(bpp, depth),
192 enable_tiling, fb_info);
193 igt_assert(fb_id > 0);
196 static void get_mode_for_crtc(struct crtc_config *crtc,
197 drmModeModeInfo *mode_ret)
199 drmModeModeInfo mode;
203 * First try to select a default mode that is supported by all
206 for (i = 0; i < crtc->connector_count; i++) {
207 mode = crtc->cconfs[i].default_mode;
208 if (crtc_supports_mode(crtc, &mode))
213 * Then just fall back to find any that is supported by all
216 for (i = 0; i < crtc->cconfs[0].connector->count_modes; i++) {
217 mode = crtc->cconfs[0].connector->modes[i];
218 if (crtc_supports_mode(crtc, &mode))
223 * If none is found then just pick the default mode of the first
224 * connector and hope the other connectors can support it by scaling
227 mode = crtc->cconfs[0].default_mode;
232 static int get_encoder_idx(drmModeRes *resources, drmModeEncoder *encoder)
236 for (i = 0; i < resources->count_encoders; i++)
237 if (resources->encoders[i] == encoder->encoder_id)
242 static void get_crtc_config_str(struct crtc_config *crtc, char *buf,
248 pos = snprintf(buf, buf_size,
249 "CRTC[%d] [Pipe %s] Mode: %s@%dHz Connectors: ",
250 crtc->crtc_id, kmstest_pipe_str(crtc->pipe_id),
251 crtc->mode.name, crtc->mode.vrefresh);
254 for (i = 0; i < crtc->connector_count; i++) {
255 drmModeConnector *connector = crtc->cconfs[i].connector;
257 pos += snprintf(&buf[pos], buf_size - pos,
258 "%s%s-%d[%d]%s", i ? ", " : "",
259 kmstest_connector_type_str(connector->connector_type),
260 connector->connector_type_id, connector->connector_id,
261 crtc->cconfs[i].connected ? "" : " (NC)");
267 static void setup_crtcs(drmModeRes *resources, struct connector_config *cconf,
268 int connector_count, struct crtc_config *crtcs,
269 int *crtc_count_ret, bool *config_valid_ret)
271 struct crtc_config *crtc;
275 int encoder_usage_count[resources->count_encoders];
282 while (i < connector_count) {
283 drmModeCrtc *drm_crtc;
284 unsigned long encoder_mask;
287 igt_assert(crtc_count < MAX_CRTCS);
289 crtc->crtc_idx = cconf[i].crtc_idx;
290 drm_crtc = drmModeGetCrtc(drm_fd,
291 resources->crtcs[crtc->crtc_idx]);
292 crtc->crtc_id = drm_crtc->crtc_id;
293 drmModeFreeCrtc(drm_crtc);
294 crtc->pipe_id = kmstest_get_pipe_from_crtc_id(drm_fd,
297 crtc->connector_count = 1;
298 for (j = i + 1; j < connector_count; j++)
299 if (cconf[j].crtc_idx == crtc->crtc_idx)
300 crtc->connector_count++;
302 crtc->cconfs = malloc(sizeof(*crtc->cconfs) *
303 crtc->connector_count);
304 igt_assert(crtc->cconfs);
307 for (j = 0; j < crtc->connector_count; j++) {
308 drmModeConnector *connector;
309 drmModeEncoder *encoder;
311 crtc->cconfs[j] = cconf[i + j];
312 connector = cconf[i + j].connector;
314 /* Intel connectors have only a single encoder */
315 igt_assert(connector->count_encoders == 1);
316 encoder = drmModeGetEncoder(drm_fd,
317 connector->encoders[0]);
320 config_valid &= !!(encoder->possible_crtcs &
321 (1 << crtc->crtc_idx));
323 encoder_mask |= 1 << get_encoder_idx(resources,
325 config_valid &= !(encoder_mask &
326 ~encoder->possible_clones);
328 drmModeFreeEncoder(encoder);
330 get_mode_for_crtc(crtc, &crtc->mode);
331 create_fb_for_crtc(crtc, &crtc->fb_info);
333 i += crtc->connector_count;
338 memset(encoder_usage_count, 0, sizeof(encoder_usage_count));
339 for (i = 0; i < connector_count; i++) {
340 drmModeConnector *connector = cconf[i].connector;
341 drmModeEncoder *encoder;
343 igt_assert(connector->count_encoders == 1);
344 encoder = drmModeGetEncoder(drm_fd, connector->encoders[0]);
345 encoder_usage_count[get_encoder_idx(resources, encoder)]++;
346 drmModeFreeEncoder(encoder);
348 for (i = 0; i < resources->count_encoders; i++)
349 if (encoder_usage_count[i] > 1)
350 config_valid = false;
352 *crtc_count_ret = crtc_count;
353 *config_valid_ret = config_valid;
356 static void cleanup_crtcs(struct crtc_config *crtcs, int crtc_count)
360 for (i = 0; i < crtc_count; i++) {
361 free(crtcs[i].cconfs);
365 static uint32_t *get_connector_ids(struct crtc_config *crtc)
370 ids = malloc(sizeof(*ids) * crtc->connector_count);
372 for (i = 0; i < crtc->connector_count; i++)
373 ids[i] = crtc->cconfs[i].connector->connector_id;
378 static void test_crtc_config(const struct test_config *tconf,
379 struct crtc_config *crtcs, int crtc_count)
381 char str_buf[MAX_CRTCS][1024];
382 const char *crtc_strs[MAX_CRTCS];
383 struct crtc_config *crtc;
385 bool config_failed = false;
386 bool connector_connected = false;
392 if (filter_test_id && filter_test_id != test_id)
395 printf(" Test id#%d CRTC count %d\n", test_id, crtc_count);
397 for (i = 0; i < crtc_count; i++) {
398 get_crtc_config_str(&crtcs[i], str_buf[i], sizeof(str_buf[i]));
399 crtc_strs[i] = &str_buf[i][0];
403 for (i = 0; i < crtc_count; i++)
404 printf(" %s\n", crtc_strs[i]);
408 for (i = 0; i < crtc_count; i++) {
414 printf(" %s\n", crtc_strs[i]);
416 create_fb_for_crtc(crtc, &crtc->fb_info);
417 paint_fb(&crtc->fb_info, tconf->name, crtc_strs, crtc_count, i);
419 ids = get_connector_ids(crtc);
420 ret = drmModeSetCrtc(drm_fd, crtc->crtc_id,
421 crtc->fb_info.fb_id, 0, 0, ids,
422 crtc->connector_count, &crtc->mode);
426 igt_assert(errno == EINVAL);
427 config_failed = true;
430 for (j = 0; j < crtc->connector_count; j++)
431 connector_connected |= crtc->cconfs[j].connected;
434 igt_assert(config_failed == !!(tconf->flags & TEST_INVALID));
436 if (ret == 0 && connector_connected && !(tconf->flags & TEST_INVALID))
439 for (i = 0; i < crtc_count; i++) {
440 if (crtcs[i].fb_info.fb_id) {
441 drmModeSetCrtc(drm_fd, crtcs[i].crtc_id, 0, 0, 0, NULL,
443 drmModeRmFB(drm_fd, crtcs[i].fb_info.fb_id);
444 crtcs[i].fb_info.fb_id = 0;
451 static void test_one_combination(const struct test_config *tconf,
452 struct connector_config *cconfs,
455 struct crtc_config crtcs[MAX_CRTCS];
459 setup_crtcs(tconf->resources, cconfs, connector_count, crtcs,
460 &crtc_count, &config_valid);
462 if (config_valid == !(tconf->flags & TEST_INVALID))
463 test_crtc_config(tconf, crtcs, crtc_count);
465 cleanup_crtcs(crtcs, crtc_count);
468 static int assign_crtc_to_connectors(const struct test_config *tconf,
469 int *crtc_idxs, int connector_count,
470 struct connector_config *cconfs)
472 unsigned long crtc_idx_mask;
476 for (i = 0; i < connector_count; i++) {
477 int crtc_idx = crtc_idxs[i];
479 if ((tconf->flags & TEST_SINGLE_CRTC_CLONE) &&
480 crtc_idx_mask & ~(1 << crtc_idx))
483 if ((tconf->flags & TEST_EXCLUSIVE_CRTC_CLONE) &&
484 crtc_idx_mask & (1 << crtc_idx))
487 crtc_idx_mask |= 1 << crtc_idx;
489 cconfs[i].crtc_idx = crtc_idx;
495 static int get_one_connector(drmModeRes *resources, int connector_id,
496 struct connector_config *cconf)
498 drmModeConnector *connector;
499 drmModeModeInfo mode;
501 connector = drmModeGetConnector(drm_fd, connector_id);
502 igt_assert(connector);
503 cconf->connector = connector;
505 cconf->connected = connector->connection == DRM_MODE_CONNECTED;
508 * For DP/eDP we need a connected sink, since mode setting depends
509 * on successful link training and retrieved DPCD parameters.
511 switch (connector->connector_type) {
512 case DRM_MODE_CONNECTOR_DisplayPort:
513 case DRM_MODE_CONNECTOR_eDP:
514 if (!cconf->connected) {
515 drmModeFreeConnector(connector);
520 if (cconf->connected) {
521 if (kmstest_get_connector_default_mode(drm_fd, connector,
528 cconf->default_mode = mode;
533 static int get_connectors(drmModeRes *resources, int *connector_idxs,
534 int connector_count, struct connector_config *cconfs)
538 for (i = 0; i < connector_count; i++) {
542 connector_idx = connector_idxs[i];
543 igt_assert(connector_idx < resources->count_connectors);
544 connector_id = resources->connectors[connector_idx];
546 if (get_one_connector(resources, connector_id, &cconfs[i]) < 0)
555 drmModeFreeConnector(cconfs[i].connector);
560 static void free_connectors(struct connector_config *cconfs,
565 for (i = 0; i < connector_count; i++)
566 drmModeFreeConnector(cconfs[i].connector);
570 int elems[MAX_COMBINATION_ELEMS];
573 struct combination_set {
575 struct combination items[MAX_COMBINATION_COUNT];
579 * Get all possible selection of k elements from n elements with or without
582 static void iterate_combinations(int n, int k, bool allow_repetitions,
583 int depth, int base, struct combination *comb,
584 struct combination_set *set)
589 igt_assert(set->count < ARRAY_SIZE(set->items));
590 set->items[set->count++] = *comb;
594 for (v = base; v < n; v++) {
595 comb->elems[depth] = v;
596 iterate_combinations(n, k - 1, allow_repetitions,
597 depth + 1, allow_repetitions ? 0 : v + 1,
603 static void get_combinations(int n, int k, bool allow_repetitions,
604 struct combination_set *set)
606 struct combination comb;
608 igt_assert(k <= ARRAY_SIZE(set->items[0].elems));
610 iterate_combinations(n, k, allow_repetitions, 0, 0, &comb, set);
613 static void test_combinations(const struct test_config *tconf,
616 struct combination_set connector_combs;
617 struct combination_set crtc_combs;
618 struct connector_config *cconfs;
621 get_combinations(tconf->resources->count_connectors, connector_count,
622 false, &connector_combs);
623 get_combinations(tconf->resources->count_crtcs, connector_count,
626 printf("Testing: %s %d connector combinations\n", tconf->name,
628 for (i = 0; i < connector_combs.count; i++) {
633 cconfs = malloc(sizeof(*cconfs) * connector_count);
636 connector_idxs = &connector_combs.items[i].elems[0];
637 ret = get_connectors(tconf->resources, connector_idxs,
638 connector_count, cconfs);
642 for (j = 0; j < crtc_combs.count; j++) {
643 int *crtc_idxs = &crtc_combs.items[j].elems[0];
644 ret = assign_crtc_to_connectors(tconf, crtc_idxs,
650 test_one_combination(tconf, cconfs, connector_count);
653 free_connectors(cconfs, connector_count);
659 static void run_test(const struct test_config *tconf)
663 connector_num = tconf->flags & TEST_CLONE ? 2 : 1;
664 for (; connector_num <= tconf->resources->count_crtcs; connector_num++)
665 test_combinations(tconf, connector_num);
668 static int opt_handler(int opt, int opt_index)
675 filter_test_id = atoi(optarg);
684 int main(int argc, char **argv)
687 enum test_flags flags;
690 { TEST_CLONE | TEST_SINGLE_CRTC_CLONE,
691 "clone-single-crtc" },
692 { TEST_INVALID | TEST_CLONE | TEST_SINGLE_CRTC_CLONE,
693 "invalid-clone-single-crtc" },
694 { TEST_INVALID | TEST_CLONE | TEST_EXCLUSIVE_CRTC_CLONE,
695 "invalid-clone-exclusive-crtc" },
696 { TEST_CLONE | TEST_EXCLUSIVE_CRTC_CLONE,
697 "clone-exclusive-crtc" },
699 const char *help_str =
700 " -d\t\tDon't run any test, only print what would be done. (still needs DRM access)\n"
701 " -t <test id>\tRun only the test with this id.";
705 ret = igt_subtest_init_parse_opts(argc, argv, "dt:", NULL, help_str,
708 return ret == -1 ? 0 : ret;
710 igt_skip_on_simulation();
712 if (dry_run && filter_test_id) {
713 fprintf(stderr, "only one of -d and -t is accepted\n");
718 drm_fd = drm_open_any();
720 igt_set_vt_graphics_mode();
722 drm_resources = drmModeGetResources(drm_fd);
723 igt_assert(drm_resources);
726 for (i = 0; i < ARRAY_SIZE(tests); i++) {
727 igt_subtest(tests[i].name) {
728 struct test_config tconf = {
729 .flags = tests[i].flags,
730 .name = tests[i].name,
731 .resources = drm_resources,
738 drmModeFreeResources(drm_resources);