2 * Copyright 2010 Intel Corporation
3 * Jesse Barnes <jesse.barnes@intel.com>
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
25 * This program is intended for testing of display functionality. It should
26 * allow for testing of
29 * - clone & twin modes
31 * - test patterns & pixel generators
32 * Additional programs can test the detected outputs against VBT provided
33 * device lists (both docked & undocked).
36 * - pixel generator in transcoder
37 * - test pattern reg in pipe
38 * - test patterns on outputs (e.g. TV)
39 * - handle hotplug (leaks crtcs, can't handle clones)
41 * - expose output specific controls
42 * - e.g. DDC-CI brightness
45 * - DP commands (e.g. poweroff)
46 * - verify outputs against VBT/physical connectors
62 #include <sys/ioctl.h>
63 #include <sys/types.h>
66 #include "ioctl_wrappers.h"
68 #include "testdisplay.h"
74 #define SUBTEST_OPTS 1
77 struct termios saved_tio;
79 drmModeRes *resources;
81 int test_all_modes = 0, test_preferred_mode = 0, force_mode = 0, test_plane,
83 unsigned int tiling = I915_TILING_NONE;
84 int sleep_between_modes = 5;
85 int do_dpms = 0; /* This aliases to DPMS_ON */
86 uint32_t depth = 24, stride, bpp;
88 int specified_mode_num = -1, specified_disp_id = -1;
90 drmModeModeInfo force_timing;
92 int crtc_x, crtc_y, crtc_w, crtc_h, width, height;
93 unsigned int plane_fb_id;
94 unsigned int plane_crtc_id;
95 unsigned int plane_id;
96 int plane_width, plane_height;
97 static const uint32_t SPRITE_COLOR_KEY = 0x00aaaaaa;
100 * Mode setting with the kernel interfaces is a bit of a chore.
101 * First you have to find the connector in question and make sure the
102 * requested mode is available.
103 * Then you need to find the encoder attached to that connector so you
104 * can bind it with a free crtc.
109 drmModeModeInfo mode;
110 drmModeEncoder *encoder;
111 drmModeConnector *connector;
117 static void dump_connectors_fd(int drmfd)
121 drmModeRes *mode_resources = drmModeGetResources(drmfd);
123 if (!mode_resources) {
124 igt_warn("drmModeGetResources failed: %s\n", strerror(errno));
128 igt_info("Connectors:\n");
129 igt_info("id\tencoder\tstatus\t\ttype\tsize (mm)\tmodes\n");
130 for (i = 0; i < mode_resources->count_connectors; i++) {
131 drmModeConnector *connector;
133 connector = drmModeGetConnector(drmfd, mode_resources->connectors[i]);
135 igt_warn("could not get connector %i: %s\n", mode_resources->connectors[i], strerror(errno));
139 igt_info("%d\t%d\t%s\t%s\t%dx%d\t\t%d\n", connector->connector_id, connector->encoder_id, kmstest_connector_status_str(connector->connection), kmstest_connector_type_str(connector->connector_type), connector->mmWidth, connector->mmHeight, connector->count_modes);
141 if (!connector->count_modes)
144 igt_info(" modes:\n");
145 igt_info(" name refresh (Hz) hdisp hss hse htot vdisp ""vss vse vtot flags type clock\n");
146 for (j = 0; j < connector->count_modes; j++){
148 kmstest_dump_mode(&connector->modes[j]);
151 drmModeFreeConnector(connector);
155 drmModeFreeResources(mode_resources);
158 static void dump_crtcs_fd(int drmfd)
161 drmModeRes *mode_resources = drmModeGetResources(drmfd);
163 igt_info("CRTCs:\n");
164 igt_info("id\tfb\tpos\tsize\n");
165 for (i = 0; i < mode_resources->count_crtcs; i++) {
168 crtc = drmModeGetCrtc(drmfd, mode_resources->crtcs[i]);
170 igt_warn("could not get crtc %i: %s\n", mode_resources->crtcs[i], strerror(errno));
173 igt_info("%d\t%d\t(%d,%d)\t(%dx%d)\n", crtc->crtc_id, crtc->buffer_id, crtc->x, crtc->y, crtc->width, crtc->height);
174 kmstest_dump_mode(&crtc->mode);
176 drmModeFreeCrtc(crtc);
180 drmModeFreeResources(mode_resources);
183 static void dump_info(void)
185 dump_connectors_fd(drm_fd);
186 dump_crtcs_fd(drm_fd);
189 static void connector_find_preferred_mode(uint32_t connector_id,
190 unsigned long crtc_idx_mask,
191 int mode_num, struct connector *c)
193 struct kmstest_connector_config config;
195 if (!kmstest_get_connector_config(drm_fd, connector_id, crtc_idx_mask,
201 c->connector = config.connector;
202 c->encoder = config.encoder;
203 c->crtc = config.crtc->crtc_id;
204 c->crtc_idx = config.crtc_idx;
205 c->pipe = config.pipe;
207 if (mode_num != -1) {
208 igt_assert(mode_num < config.connector->count_modes);
209 c->mode = config.connector->modes[mode_num];
211 c->mode = config.default_mode;
217 paint_color_key(struct igt_fb *fb_info)
222 fb_ptr = gem_mmap(drm_fd, fb_info->gem_handle,
223 fb_info->size, PROT_READ | PROT_WRITE);
226 for (i = crtc_y; i < crtc_y + crtc_h; i++)
227 for (j = crtc_x; j < crtc_x + crtc_w; j++) {
230 offset = (i * fb_info->stride / 4) + j;
231 fb_ptr[offset] = SPRITE_COLOR_KEY;
234 munmap(fb_ptr, fb_info->size);
237 static void paint_image(cairo_t *cr, const char *file)
239 int img_x, img_y, img_w, img_h, img_w_o, img_h_o;
240 double img_w_scale, img_h_scale;
242 cairo_surface_t *image;
244 img_y = height * (0.10 );
245 img_h = height * 0.08 * 4;
248 img_x = (width / 2) - (img_w / 2);
250 image = cairo_image_surface_create_from_png(file);
252 img_w_o = cairo_image_surface_get_width(image);
253 img_h_o = cairo_image_surface_get_height(image);
255 cairo_translate(cr, img_x, img_y);
257 img_w_scale = (double)img_w / (double)img_w_o;
258 img_h_scale = (double)img_h / (double)img_h_o;
259 cairo_scale(cr, img_w_scale, img_h_scale);
261 cairo_set_source_surface(cr, image, 0, 0);
262 cairo_scale(cr, 1, 1);
265 cairo_surface_destroy(image);
268 static void paint_output_info(struct connector *c, struct igt_fb *fb)
270 cairo_t *cr = igt_get_cairo_ctx(drm_fd, fb);
271 int l_width = fb->width;
272 int l_height = fb->height;
278 igt_paint_test_pattern(cr, l_width, l_height);
280 cairo_move_to(cr, l_width / 2, l_height / 2);
282 /* Print connector and mode name */
283 cairo_set_font_size(cr, 48);
284 igt_cairo_printf_line(cr, align_hcenter, 10, "%s",
285 kmstest_connector_type_str(c->connector->connector_type));
287 cairo_set_font_size(cr, 36);
288 str_width = igt_cairo_printf_line(cr, align_hcenter, 10,
289 "%s @ %dHz on %s encoder", c->mode.name, c->mode.vrefresh,
290 kmstest_encoder_type_str(c->encoder->encoder_type));
292 cairo_rel_move_to(cr, -str_width / 2, 0);
294 /* List available modes */
295 cairo_set_font_size(cr, 18);
296 str_width = igt_cairo_printf_line(cr, align_left, 10,
298 cairo_rel_move_to(cr, str_width, 0);
299 cairo_get_current_point(cr, &x, &top_y);
302 for (i = 0; i < c->connector->count_modes; i++) {
303 cairo_get_current_point(cr, &x, &y);
307 cairo_move_to(cr, x, top_y);
309 str_width = igt_cairo_printf_line(cr, align_right, 10,
310 "%s @ %dHz", c->connector->modes[i].name,
311 c->connector->modes[i].vrefresh);
312 if (str_width > max_width)
313 max_width = str_width;
317 paint_image(cr, IGT_DATADIR"/pass.png");
319 igt_assert(!cairo_status(cr));
324 static void sighandler(int signo)
329 static void set_single(void)
331 int sigs[] = { SIGUSR1 };
333 sa.sa_handler = sighandler;
335 sigemptyset(&sa.sa_mask);
337 igt_warn_on_f(sigaction(sigs[0], &sa, NULL) == -1,
338 "Could not set signal handler");
342 set_mode(struct connector *c)
344 unsigned int fb_id = 0;
345 struct igt_fb fb_info[2] = { };
346 int j, test_mode_num, current_fb = 0, old_fb = -1;
350 memcpy( &c->mode, &force_timing, sizeof(force_timing));
351 c->mode.vrefresh =(force_timing.clock*1e3)/(force_timing.htotal*force_timing.vtotal);
353 sprintf(c->mode.name, "%dx%d", force_timing.hdisplay, force_timing.vdisplay);
354 } else if (test_all_modes)
355 test_mode_num = c->connector->count_modes;
357 for (j = 0; j < test_mode_num; j++) {
360 c->mode = c->connector->modes[j];
362 /* set_mode() only tests 2D modes */
363 if (c->mode.flags & DRM_MODE_FLAG_3D_MASK)
369 width = c->mode.hdisplay;
370 height = c->mode.vdisplay;
372 fb_id = igt_create_fb(drm_fd, width, height,
373 igt_bpp_depth_to_drm_format(bpp, depth),
374 tiling, &fb_info[current_fb]);
375 paint_output_info(c, &fb_info[current_fb]);
376 paint_color_key(&fb_info[current_fb]);
378 igt_info("CRTC(%u):[%d]", c->crtc, j);
379 kmstest_dump_mode(&c->mode);
380 if (drmModeSetCrtc(drm_fd, c->crtc, fb_id, 0, 0,
381 &c->id, 1, &c->mode)) {
382 igt_warn("failed to set mode (%dx%d@%dHz): %s\n", width, height, c->mode.vrefresh, strerror(errno));
387 igt_remove_fb(drm_fd, &fb_info[old_fb]);
389 current_fb = 1 - current_fb;
391 if (sleep_between_modes && test_all_modes && !qr_code)
392 sleep(sleep_between_modes);
395 kmstest_set_connector_dpms(drm_fd, c->connector, do_dpms);
396 sleep(sleep_between_modes);
397 kmstest_set_connector_dpms(drm_fd, c->connector, DRM_MODE_DPMS_ON);
407 igt_remove_fb(drm_fd, &fb_info[old_fb]);
409 drmModeFreeEncoder(c->encoder);
410 drmModeFreeConnector(c->connector);
413 static void do_set_stereo_mode(struct connector *c)
417 fb_id = igt_create_stereo_fb(drm_fd, &c->mode,
418 igt_bpp_depth_to_drm_format(bpp, depth),
421 igt_warn_on_f(drmModeSetCrtc(drm_fd, c->crtc, fb_id, 0, 0, &c->id, 1, &c->mode),
422 "failed to set mode (%dx%d@%dHz): %s\n", width, height, c->mode.vrefresh, strerror(errno));
426 set_stereo_mode(struct connector *c)
431 if (specified_mode_num != -1)
434 n = c->connector->count_modes;
436 for (i = 0; i < n; i++) {
437 if (specified_mode_num == -1)
438 c->mode = c->connector->modes[i];
443 if (!(c->mode.flags & DRM_MODE_FLAG_3D_MASK))
446 igt_info("CRTC(%u): [%d]", c->crtc, i);
447 kmstest_dump_mode(&c->mode);
448 do_set_stereo_mode(c);
453 } else if (sleep_between_modes)
454 sleep(sleep_between_modes);
457 kmstest_set_connector_dpms(drm_fd, c->connector, DRM_MODE_DPMS_OFF);
458 sleep(sleep_between_modes);
459 kmstest_set_connector_dpms(drm_fd, c->connector, DRM_MODE_DPMS_ON);
463 drmModeFreeEncoder(c->encoder);
464 drmModeFreeConnector(c->connector);
468 * Re-probe outputs and light up as many as possible.
470 * On Intel, we have two CRTCs that we can drive independently with
471 * different timings and scanout buffers.
473 * Each connector has a corresponding encoder, except in the SDVO case
474 * where an encoder may have multiple connectors.
476 int update_display(void)
478 struct connector *connectors;
481 resources = drmModeGetResources(drm_fd);
483 igt_warn("drmModeGetResources failed: %s\n", strerror(errno));
487 connectors = calloc(resources->count_connectors,
488 sizeof(struct connector));
492 if (test_preferred_mode || test_all_modes ||
493 force_mode || specified_disp_id != -1) {
494 unsigned long crtc_idx_mask = -1UL;
496 /* Find any connected displays */
497 for (c = 0; c < resources->count_connectors; c++) {
498 struct connector *connector = &connectors[c];
500 connector->id = resources->connectors[c];
501 if (specified_disp_id != -1 &&
502 connector->id != specified_disp_id)
505 connector_find_preferred_mode(connector->id,
509 if (!connector->mode_valid)
514 if (test_preferred_mode || force_mode ||
515 specified_mode_num != -1)
516 crtc_idx_mask &= ~(1 << connector->crtc_idx);
521 if (test_stereo_modes) {
522 for (c = 0; c < resources->count_connectors; c++) {
523 struct connector *connector = &connectors[c];
525 connector->id = resources->connectors[c];
526 if (specified_disp_id != -1 &&
527 connector->id != specified_disp_id)
530 connector_find_preferred_mode(connector->id,
534 if (!connector->mode_valid)
537 set_stereo_mode(connector);
542 drmModeFreeResources(resources);
546 static char optstr[] = "3hiaf:s:d:p:mrto:j:";
548 static void __attribute__((noreturn)) usage(char *name)
550 igt_info("usage: %s [-hiasdpmtf]\n", name);
551 igt_info("\t-i\tdump info\n");
552 igt_info("\t-a\ttest all modes\n");
553 igt_info("\t-s\t<duration>\tsleep between each mode test\n");
554 igt_info("\t-d\t<depth>\tbit depth of scanout buffer\n");
555 igt_info("\t-p\t<planew,h>,<crtcx,y>,<crtcw,h> test overlay plane\n");
556 igt_info("\t-m\ttest the preferred mode\n");
557 igt_info("\t-3\ttest all 3D modes\n");
558 igt_info("\t-t\tuse a tiled framebuffer\n");
559 igt_info("\t-j\tdo dpms off, optional arg to select dpms leve (1-3)\n");
560 igt_info("\t-r\tprint a QR code on the screen whose content is \"pass\" for the automatic test\n");
561 igt_info("\t-o\t<id of the display>,<number of the mode>\tonly test specified mode on the specified display\n");
562 igt_info("\t-f\t<clock MHz>,<hdisp>,<hsync-start>,<hsync-end>,<htotal>,\n");
563 igt_info("\t\t<vdisp>,<vsync-start>,<vsync-end>,<vtotal>\n");
564 igt_info("\t\ttest force mode\n");
565 igt_info("\tDefault is to test all modes.\n");
566 exit((optopt) ? -1 : 0);
569 #define dump_resource(res) if (res) dump_##res()
571 static void cleanup_and_exit(int ret)
577 static gboolean input_event(GIOChannel *source, GIOCondition condition,
583 count = read(g_io_channel_unix_get_fd(source), buf, sizeof(buf));
584 if (buf[0] == 'q' && (count == 1 || buf[1] == '\n')) {
591 static void enter_exec_path( char **argv )
593 char *exec_path = NULL;
598 len_path = strlen( argv[0] );
599 exec_path = (char*) malloc(len_path);
601 memcpy(exec_path, argv[0], len_path);
602 pos = strrchr(exec_path, '/');
606 ret = chdir(exec_path);
607 igt_assert(ret == 0);
611 static void restore_termio_mode(int sig)
613 tcsetattr(tio_fd, TCSANOW, &saved_tio);
617 static void set_termio_mode(void)
621 /* don't attempt to set terminal attributes if not in the foreground
623 if (getpgrp() != tcgetpgrp(STDOUT_FILENO))
626 tio_fd = dup(STDIN_FILENO);
627 tcgetattr(tio_fd, &saved_tio);
628 igt_install_exit_handler(restore_termio_mode);
630 tio.c_lflag &= ~(ICANON | ECHO);
631 tcsetattr(tio_fd, TCSANOW, &tio);
634 int main(int argc, char **argv)
638 GIOChannel *stdinchannel;
641 bool opt_dump_info = false;
642 struct option long_opts[] = {
643 {"list-subtests", 0, 0, SUBTEST_OPTS},
644 {"run-subtest", 1, 0, SUBTEST_OPTS},
648 igt_skip_on_simulation();
650 enter_exec_path( argv );
652 while ((c = getopt_long(argc, argv, optstr, long_opts, NULL)) != -1) {
655 test_stereo_modes = 1;
658 opt_dump_info = true;
665 if(sscanf(optarg,"%f,%hu,%hu,%hu,%hu,%hu,%hu,%hu,%hu",
666 &force_clock,&force_timing.hdisplay, &force_timing.hsync_start,&force_timing.hsync_end,&force_timing.htotal,
667 &force_timing.vdisplay, &force_timing.vsync_start, &force_timing.vsync_end, &force_timing.vtotal)!= 9)
669 force_timing.clock = force_clock*1000;
673 sleep_between_modes = atoi(optarg);
676 do_dpms = atoi(optarg);
678 do_dpms = DRM_MODE_DPMS_OFF;
681 depth = atoi(optarg);
682 igt_info("using depth %d\n", depth);
685 if (sscanf(optarg, "%d,%d,%d,%d,%d,%d", &plane_width,
686 &plane_height, &crtc_x, &crtc_y,
687 &crtc_w, &crtc_h) != 6)
692 test_preferred_mode = 1;
695 tiling = I915_TILING_X;
701 sscanf(optarg, "%d,%d", &specified_disp_id, &specified_mode_num);
704 /* invalid subtest options */
705 exit(IGT_EXIT_INVALID);
719 else if (depth <= 16)
721 else if (depth <= 32)
724 if (!test_all_modes && !force_mode && !test_preferred_mode &&
725 specified_mode_num == -1 && !test_stereo_modes)
728 drm_fd = drm_open_any();
730 if (test_stereo_modes &&
731 drmSetClientCap(drm_fd, DRM_CLIENT_CAP_STEREO_3D, 1) < 0) {
732 igt_warn("DRM_CLIENT_CAP_STEREO_3D failed\n");
741 kmstest_set_vt_graphics_mode();
743 mainloop = g_main_loop_new(NULL, FALSE);
745 igt_warn("failed to create glib mainloop\n");
750 if (!testdisplay_setup_hotplug()) {
751 igt_warn("failed to initialize hotplug support\n");
755 stdinchannel = g_io_channel_unix_new(0);
757 igt_warn("failed to create stdin GIO channel\n");
761 ret = g_io_add_watch(stdinchannel, G_IO_IN | G_IO_ERR, input_event,
764 igt_warn("failed to add watch on stdin GIO channel\n");
770 if (!update_display()) {
778 g_main_loop_run(mainloop);
781 g_io_channel_shutdown(stdinchannel, TRUE, NULL);
783 testdisplay_cleanup_hotplug();
785 g_main_loop_unref(mainloop);