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 "drm_fourcc.h"
38 #include "intel_bufmgr.h"
39 #include "intel_batchbuffer.h"
40 #include "intel_gpu_tools.h"
43 #define MAX_CONNECTORS 10
46 /* max combinations with repetitions */
47 #define MAX_COMBINATION_COUNT \
48 (MAX_CONNECTORS * MAX_CONNECTORS * MAX_CONNECTORS)
49 #define MAX_COMBINATION_ELEMS MAX_CRTCS
52 static drmModeRes *drm_resources;
53 static int filter_test_id;
56 const drmModeModeInfo mode_640_480 = {
71 .flags = DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC,
77 TEST_SINGLE_CRTC_CLONE = 0x04,
78 TEST_EXCLUSIVE_CRTC_CLONE = 0x08,
83 enum test_flags flags;
84 drmModeRes *resources;
87 struct connector_config {
88 drmModeConnector *connector;
91 drmModeModeInfo default_mode;
99 struct connector_config *cconfs;
100 struct kmstest_fb fb_info;
101 drmModeModeInfo mode;
104 static bool drm_mode_equal(drmModeModeInfo *m1, drmModeModeInfo *m2)
106 #define COMP(x) do { if (m1->x != m2->x) return false; } while (0)
122 static bool connector_supports_mode(drmModeConnector *connector,
123 drmModeModeInfo *mode)
127 for (i = 0; i < connector->count_modes; i++)
128 if (drm_mode_equal(&connector->modes[i], mode))
134 static bool crtc_supports_mode(struct crtc_config *crtc, drmModeModeInfo *mode)
138 for (i = 0; i < crtc->connector_count; i++) {
139 if (!connector_supports_mode(crtc->cconfs[i].connector, mode))
146 static int paint_fb(struct kmstest_fb *fb, const char *test_name,
147 const char **crtc_str, int crtc_count, int current_crtc_idx)
153 cr = kmstest_get_cairo_ctx(drm_fd, fb);
155 kmstest_paint_test_pattern(cr, fb->width, fb->height);
157 cairo_select_font_face(cr, "Helvetica", CAIRO_FONT_SLANT_NORMAL,
158 CAIRO_FONT_WEIGHT_NORMAL);
159 cairo_move_to(cr, fb->width / 2, fb->height / 2);
160 cairo_set_font_size(cr, 24);
161 kmstest_cairo_printf_line(cr, align_hcenter, 40, "%s", test_name);
163 cairo_get_current_point(cr, &x, &y);
164 cairo_move_to(cr, 60, y);
166 for (i = 0; i < crtc_count; i++) {
167 if (i == current_crtc_idx) {
168 cairo_get_current_point(cr, &x, &y);
169 cairo_move_to(cr, x - 20, y);
170 kmstest_cairo_printf_line(cr, align_right, 20, "X");
171 cairo_move_to(cr, x, y);
173 kmstest_cairo_printf_line(cr, align_left, 20, "%s",
182 static void create_fb_for_crtc(struct crtc_config *crtc,
183 struct kmstest_fb *fb_info)
192 enable_tiling = false;
193 fb_id = kmstest_create_fb(drm_fd, crtc->mode.hdisplay,
194 crtc->mode.vdisplay, bpp, depth,
195 enable_tiling, fb_info);
199 static void get_mode_for_crtc(struct crtc_config *crtc,
200 drmModeModeInfo *mode_ret)
202 drmModeModeInfo mode;
206 * First try to select a default mode that is supported by all
209 for (i = 0; i < crtc->connector_count; i++) {
210 mode = crtc->cconfs[i].default_mode;
211 if (crtc_supports_mode(crtc, &mode))
216 * Then just fall back to find any that is supported by all
219 for (i = 0; i < crtc->cconfs[0].connector->count_modes; i++) {
220 mode = crtc->cconfs[0].connector->modes[i];
221 if (crtc_supports_mode(crtc, &mode))
226 * If none is found then just pick the default mode of the first
227 * connector and hope the other connectors can support it by scaling
230 mode = crtc->cconfs[0].default_mode;
235 static int get_encoder_idx(drmModeRes *resources, drmModeEncoder *encoder)
239 for (i = 0; i < resources->count_encoders; i++)
240 if (resources->encoders[i] == encoder->encoder_id)
245 static void get_crtc_config_str(struct crtc_config *crtc, char *buf,
251 pos = snprintf(buf, buf_size,
252 "CRTC[%d] [Pipe %s] Mode: %s@%dHz Connectors: ",
253 crtc->crtc_id, kmstest_pipe_str(crtc->pipe_id),
254 crtc->mode.name, crtc->mode.vrefresh);
257 for (i = 0; i < crtc->connector_count; i++) {
258 drmModeConnector *connector = crtc->cconfs[i].connector;
260 pos += snprintf(&buf[pos], buf_size - pos,
261 "%s%s-%d[%d]%s", i ? ", " : "",
262 kmstest_connector_type_str(connector->connector_type),
263 connector->connector_type_id, connector->connector_id,
264 crtc->cconfs[i].connected ? "" : " (NC)");
270 static void setup_crtcs(drmModeRes *resources, struct connector_config *cconf,
271 int connector_count, struct crtc_config *crtcs,
272 int *crtc_count_ret, bool *config_valid_ret)
274 struct crtc_config *crtc;
278 int encoder_usage_count[resources->count_encoders];
285 while (i < connector_count) {
286 drmModeCrtc *drm_crtc;
287 unsigned long encoder_mask;
290 igt_assert(crtc_count < MAX_CRTCS);
292 crtc->crtc_idx = cconf[i].crtc_idx;
293 drm_crtc = drmModeGetCrtc(drm_fd,
294 resources->crtcs[crtc->crtc_idx]);
295 crtc->crtc_id = drm_crtc->crtc_id;
296 drmModeFreeCrtc(drm_crtc);
297 crtc->pipe_id = kmstest_get_pipe_from_crtc_id(drm_fd,
300 crtc->connector_count = 1;
301 for (j = i + 1; j < connector_count; j++)
302 if (cconf[j].crtc_idx == crtc->crtc_idx)
303 crtc->connector_count++;
305 crtc->cconfs = malloc(sizeof(*crtc->cconfs) *
306 crtc->connector_count);
307 assert(crtc->cconfs);
310 for (j = 0; j < crtc->connector_count; j++) {
311 drmModeConnector *connector;
312 drmModeEncoder *encoder;
314 crtc->cconfs[j] = cconf[i + j];
315 connector = cconf[i + j].connector;
317 /* Intel connectors have only a single encoder */
318 igt_assert(connector->count_encoders == 1);
319 encoder = drmModeGetEncoder(drm_fd,
320 connector->encoders[0]);
323 config_valid &= !!(encoder->possible_crtcs &
324 (1 << crtc->crtc_idx));
326 encoder_mask |= 1 << get_encoder_idx(resources,
328 config_valid &= !(encoder_mask &
329 ~encoder->possible_clones);
331 drmModeFreeEncoder(encoder);
333 get_mode_for_crtc(crtc, &crtc->mode);
334 create_fb_for_crtc(crtc, &crtc->fb_info);
336 i += crtc->connector_count;
341 memset(encoder_usage_count, 0, sizeof(encoder_usage_count));
342 for (i = 0; i < connector_count; i++) {
343 drmModeConnector *connector = cconf[i].connector;
344 drmModeEncoder *encoder;
346 igt_assert(connector->count_encoders == 1);
347 encoder = drmModeGetEncoder(drm_fd, connector->encoders[0]);
348 encoder_usage_count[get_encoder_idx(resources, encoder)]++;
349 drmModeFreeEncoder(encoder);
351 for (i = 0; i < resources->count_encoders; i++)
352 if (encoder_usage_count[i] > 1)
353 config_valid = false;
355 *crtc_count_ret = crtc_count;
356 *config_valid_ret = config_valid;
359 static void cleanup_crtcs(struct crtc_config *crtcs, int crtc_count)
363 for (i = 0; i < crtc_count; i++) {
364 free(crtcs[i].cconfs);
368 static uint32_t *get_connector_ids(struct crtc_config *crtc)
373 ids = malloc(sizeof(*ids) * crtc->connector_count);
375 for (i = 0; i < crtc->connector_count; i++)
376 ids[i] = crtc->cconfs[i].connector->connector_id;
381 static void test_crtc_config(const struct test_config *tconf,
382 struct crtc_config *crtcs, int crtc_count)
384 char str_buf[MAX_CRTCS][1024];
385 const char *crtc_strs[MAX_CRTCS];
386 struct crtc_config *crtc;
388 bool config_failed = false;
389 bool connector_connected = false;
395 if (filter_test_id && filter_test_id != test_id)
398 printf(" Test id#%d CRTC count %d\n", test_id, crtc_count);
400 for (i = 0; i < crtc_count; i++) {
401 get_crtc_config_str(&crtcs[i], str_buf[i], sizeof(str_buf[i]));
402 crtc_strs[i] = &str_buf[i][0];
406 for (i = 0; i < crtc_count; i++)
407 printf(" %s\n", crtc_strs[i]);
411 for (i = 0; i < crtc_count; i++) {
417 printf(" %s\n", crtc_strs[i]);
419 create_fb_for_crtc(crtc, &crtc->fb_info);
420 paint_fb(&crtc->fb_info, tconf->name, crtc_strs, crtc_count, i);
422 ids = get_connector_ids(crtc);
423 ret = drmModeSetCrtc(drm_fd, crtc->crtc_id,
424 crtc->fb_info.fb_id, 0, 0, ids,
425 crtc->connector_count, &crtc->mode);
429 igt_assert(errno == EINVAL);
430 config_failed = true;
433 for (j = 0; j < crtc->connector_count; j++)
434 connector_connected |= crtc->cconfs[j].connected;
437 igt_assert(config_failed == !!(tconf->flags & TEST_INVALID));
439 if (ret == 0 && connector_connected && !(tconf->flags & TEST_INVALID))
442 for (i = 0; i < crtc_count; i++) {
443 if (crtcs[i].fb_info.fb_id) {
444 drmModeSetCrtc(drm_fd, crtcs[i].crtc_id, 0, 0, 0, NULL,
446 drmModeRmFB(drm_fd, crtcs[i].fb_info.fb_id);
447 crtcs[i].fb_info.fb_id = 0;
454 static void test_one_combination(const struct test_config *tconf,
455 struct connector_config *cconfs,
458 struct crtc_config crtcs[MAX_CRTCS];
462 setup_crtcs(tconf->resources, cconfs, connector_count, crtcs,
463 &crtc_count, &config_valid);
465 if (config_valid == !(tconf->flags & TEST_INVALID))
466 test_crtc_config(tconf, crtcs, crtc_count);
468 cleanup_crtcs(crtcs, crtc_count);
471 static int assign_crtc_to_connectors(const struct test_config *tconf,
472 int *crtc_idxs, int connector_count,
473 struct connector_config *cconfs)
475 unsigned long crtc_idx_mask;
479 for (i = 0; i < connector_count; i++) {
480 int crtc_idx = crtc_idxs[i];
482 if ((tconf->flags & TEST_SINGLE_CRTC_CLONE) &&
483 crtc_idx_mask & ~(1 << crtc_idx))
486 if ((tconf->flags & TEST_EXCLUSIVE_CRTC_CLONE) &&
487 crtc_idx_mask & (1 << crtc_idx))
490 crtc_idx_mask |= 1 << crtc_idx;
492 cconfs[i].crtc_idx = crtc_idx;
498 static int get_one_connector(drmModeRes *resources, int connector_id,
499 struct connector_config *cconf)
501 drmModeConnector *connector;
502 drmModeModeInfo mode;
504 connector = drmModeGetConnector(drm_fd, connector_id);
506 cconf->connector = connector;
508 cconf->connected = connector->connection == DRM_MODE_CONNECTED;
511 * For DP/eDP we need a connected sink, since mode setting depends
512 * on successful link training and retrieved DPCD parameters.
514 switch (connector->connector_type) {
515 case DRM_MODE_CONNECTOR_DisplayPort:
516 case DRM_MODE_CONNECTOR_eDP:
517 if (!cconf->connected) {
518 drmModeFreeConnector(connector);
523 if (cconf->connected) {
524 if (kmstest_get_connector_default_mode(drm_fd, connector,
531 cconf->default_mode = mode;
536 static int get_connectors(drmModeRes *resources, int *connector_idxs,
537 int connector_count, struct connector_config *cconfs)
541 for (i = 0; i < connector_count; i++) {
545 connector_idx = connector_idxs[i];
546 assert(connector_idx < resources->count_connectors);
547 connector_id = resources->connectors[connector_idx];
549 if (get_one_connector(resources, connector_id, &cconfs[i]) < 0)
558 drmModeFreeConnector(cconfs[i].connector);
563 static void free_connectors(struct connector_config *cconfs,
568 for (i = 0; i < connector_count; i++)
569 drmModeFreeConnector(cconfs[i].connector);
573 int elems[MAX_COMBINATION_ELEMS];
576 struct combination_set {
578 struct combination items[MAX_COMBINATION_COUNT];
582 * Get all possible selection of k elements from n elements with or without
585 static void iterate_combinations(int n, int k, bool allow_repetitions,
586 int depth, int base, struct combination *comb,
587 struct combination_set *set)
592 assert(set->count < ARRAY_SIZE(set->items));
593 set->items[set->count++] = *comb;
597 for (v = base; v < n; v++) {
598 comb->elems[depth] = v;
599 iterate_combinations(n, k - 1, allow_repetitions,
600 depth + 1, allow_repetitions ? 0 : v + 1,
606 static void get_combinations(int n, int k, bool allow_repetitions,
607 struct combination_set *set)
609 struct combination comb;
611 assert(k <= ARRAY_SIZE(set->items[0].elems));
613 iterate_combinations(n, k, allow_repetitions, 0, 0, &comb, set);
616 static void test_combinations(const struct test_config *tconf,
619 struct combination_set connector_combs;
620 struct combination_set crtc_combs;
621 struct connector_config *cconfs;
624 get_combinations(tconf->resources->count_connectors, connector_count,
625 false, &connector_combs);
626 get_combinations(tconf->resources->count_crtcs, connector_count,
629 printf("Testing: %s %d connector combinations\n", tconf->name,
631 for (i = 0; i < connector_combs.count; i++) {
636 cconfs = malloc(sizeof(*cconfs) * connector_count);
639 connector_idxs = &connector_combs.items[i].elems[0];
640 ret = get_connectors(tconf->resources, connector_idxs,
641 connector_count, cconfs);
645 for (j = 0; j < crtc_combs.count; j++) {
646 int *crtc_idxs = &crtc_combs.items[j].elems[0];
647 ret = assign_crtc_to_connectors(tconf, crtc_idxs,
653 test_one_combination(tconf, cconfs, connector_count);
656 free_connectors(cconfs, connector_count);
662 static void run_test(const struct test_config *tconf)
666 connector_num = tconf->flags & TEST_CLONE ? 2 : 1;
667 for (; connector_num <= tconf->resources->count_crtcs; connector_num++)
668 test_combinations(tconf, connector_num);
671 static int opt_handler(int opt, int opt_index)
678 filter_test_id = atoi(optarg);
687 int main(int argc, char **argv)
690 enum test_flags flags;
693 { TEST_CLONE | TEST_SINGLE_CRTC_CLONE,
694 "clone-single-crtc" },
695 { TEST_INVALID | TEST_CLONE | TEST_SINGLE_CRTC_CLONE,
696 "invalid-clone-single-crtc" },
697 { TEST_INVALID | TEST_CLONE | TEST_EXCLUSIVE_CRTC_CLONE,
698 "invalid-clone-exclusive-crtc" },
699 { TEST_CLONE | TEST_EXCLUSIVE_CRTC_CLONE,
700 "clone-exclusive-crtc" },
702 const char *help_str =
703 " -d\t\tDon't run any test, only print what would be done. (still needs DRM access)\n"
704 " -t <test id>\tRun only the test with this id.";
708 ret = igt_subtest_init_parse_opts(argc, argv, "dt:", NULL, help_str,
711 return ret == -1 ? 0 : ret;
713 igt_skip_on_simulation();
715 if (dry_run && filter_test_id) {
716 fprintf(stderr, "only one of -d and -t is accepted\n");
721 drm_fd = drm_open_any();
723 igt_set_vt_graphics_mode();
725 drm_resources = drmModeGetResources(drm_fd);
726 assert(drm_resources);
729 for (i = 0; i < ARRAY_SIZE(tests); i++) {
730 igt_subtest(tests[i].name) {
731 struct test_config tconf = {
732 .flags = tests[i].flags,
733 .name = tests[i].name,
734 .resources = drm_resources,
741 drmModeFreeResources(drm_resources);