2 * DRM based mode setting test program
3 * Copyright 2008 Tungsten Graphics
4 * Jakob Bornecrantz <jakob@tungstengraphics.com>
5 * Copyright 2008 Intel Corporation
6 * Jesse Barnes <jesse.barnes@intel.com>
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
28 * This fairly simple test program dumps output in a similar format to the
29 * "xrandr" tool everyone knows & loves. It's necessarily slightly different
30 * since the kernel separates outputs into encoder and connector structures,
31 * each with their own unique ID. The program also allows test testing of the
32 * memory management and mode setting APIs by allowing the user to specify a
33 * connector and mode to use for mode setting. If all works as expected, a
34 * blue background should be painted on the monitor attached to the specified
35 * connector after the selected mode is set.
37 * TODO: use cairo to write the mode info on the selected output once
38 * the mode has been programmed, along with possible test patterns.
49 #include "xf86drmMode.h"
50 #include "intel_bufmgr.h"
52 drmModeRes *resources;
55 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
62 #define type_name_fn(res) \
63 char * res##_str(int type) { \
65 for (i = 0; i < ARRAY_SIZE(res##_names); i++) { \
66 if (res##_names[i].type == type) \
67 return res##_names[i].name; \
72 struct type_name encoder_type_names[] = {
73 { DRM_MODE_ENCODER_NONE, "none" },
74 { DRM_MODE_ENCODER_DAC, "DAC" },
75 { DRM_MODE_ENCODER_TMDS, "TMDS" },
76 { DRM_MODE_ENCODER_LVDS, "LVDS" },
77 { DRM_MODE_ENCODER_TVDAC, "TVDAC" },
80 type_name_fn(encoder_type)
82 struct type_name connector_status_names[] = {
83 { DRM_MODE_CONNECTED, "connected" },
84 { DRM_MODE_DISCONNECTED, "disconnected" },
85 { DRM_MODE_UNKNOWNCONNECTION, "unknown" },
88 type_name_fn(connector_status)
90 struct type_name connector_type_names[] = {
91 { DRM_MODE_CONNECTOR_Unknown, "unknown" },
92 { DRM_MODE_CONNECTOR_VGA, "VGA" },
93 { DRM_MODE_CONNECTOR_DVII, "DVI-I" },
94 { DRM_MODE_CONNECTOR_DVID, "DVI-D" },
95 { DRM_MODE_CONNECTOR_DVIA, "DVI-A" },
96 { DRM_MODE_CONNECTOR_Composite, "composite" },
97 { DRM_MODE_CONNECTOR_SVIDEO, "s-video" },
98 { DRM_MODE_CONNECTOR_LVDS, "LVDS" },
99 { DRM_MODE_CONNECTOR_Component, "component" },
100 { DRM_MODE_CONNECTOR_9PinDIN, "9-pin DIN" },
101 { DRM_MODE_CONNECTOR_DisplayPort, "displayport" },
102 { DRM_MODE_CONNECTOR_HDMIA, "HDMI-A" },
103 { DRM_MODE_CONNECTOR_HDMIB, "HDMI-B" },
106 type_name_fn(connector_type)
108 void dump_encoders(void)
110 drmModeEncoder *encoder;
113 printf("Encoders:\n");
114 printf("id\tcrtc\ttype\tpossible crtcs\tpossible clones\t\n");
115 for (i = 0; i < resources->count_encoders; i++) {
116 encoder = drmModeGetEncoder(fd, resources->encoders[i]);
119 fprintf(stderr, "could not get encoder %i: %s\n",
120 resources->encoders[i], strerror(errno));
123 printf("%d\t%d\t%s\t0x%08x\t0x%08x\n",
126 encoder_type_str(encoder->encoder_type),
127 encoder->possible_crtcs,
128 encoder->possible_clones);
129 drmModeFreeEncoder(encoder);
134 void dump_mode(struct drm_mode_modeinfo *mode)
136 printf(" %s %.02f %d %d %d %d %d %d %d %d\n",
138 (float)mode->vrefresh / 1000,
149 void dump_connectors(void)
151 drmModeConnector *connector;
154 printf("Connectors:\n");
155 printf("id\tencoder\tstatus\t\ttype\tsize (mm)\tmodes\n");
156 for (i = 0; i < resources->count_connectors; i++) {
157 connector = drmModeGetConnector(fd, resources->connectors[i]);
160 fprintf(stderr, "could not get connector %i: %s\n",
161 resources->connectors[i], strerror(errno));
165 printf("%d\t%d\t%s\t%s\t%dx%d\t\t%d\n",
166 connector->connector_id,
167 connector->encoder_id,
168 connector_status_str(connector->connection),
169 connector_type_str(connector->connector_type),
170 connector->mmWidth, connector->mmHeight,
171 connector->count_modes);
173 if (!connector->count_modes)
177 printf(" name refresh (Hz) hdisp hss hse htot vdisp "
179 for (j = 0; j < connector->count_modes; j++)
180 dump_mode(&connector->modes[j]);
182 drmModeFreeConnector(connector);
187 void dump_crtcs(void)
193 printf("id\tfb\tpos\tsize\n");
194 for (i = 0; i < resources->count_crtcs; i++) {
195 crtc = drmModeGetCrtc(fd, resources->crtcs[i]);
198 fprintf(stderr, "could not get crtc %i: %s\n",
199 resources->crtcs[i], strerror(errno));
202 printf("%d\t%d\t(%d,%d)\t(%dx%d)\n",
206 crtc->width, crtc->height);
207 dump_mode(&crtc->mode);
209 drmModeFreeCrtc(crtc);
214 void dump_framebuffers(void)
219 printf("Frame buffers:\n");
220 printf("id\tsize\tpitch\n");
221 for (i = 0; i < resources->count_fbs; i++) {
222 fb = drmModeGetFB(fd, resources->fbs[i]);
225 fprintf(stderr, "could not get fb %i: %s\n",
226 resources->fbs[i], strerror(errno));
229 printf("%d\t(%dx%d)\t%d\n",
231 fb->width, fb->height);
239 * Mode setting with the kernel interfaces is a bit of a chore.
240 * First you have to find the connector in question and make sure the
241 * requested mode is available.
242 * Then you need to find the encoder attached to that connector so you
243 * can bind it with a free crtc.
248 struct drm_mode_modeinfo *mode;
249 drmModeEncoder *encoder;
253 connector_find_mode(struct connector *c)
255 drmModeConnector *connector;
256 int i, j, size, ret, width, height;
258 /* First, find the connector & mode */
260 for (i = 0; i < resources->count_connectors; i++) {
261 connector = drmModeGetConnector(fd, resources->connectors[i]);
264 fprintf(stderr, "could not get connector %i: %s\n",
265 resources->connectors[i], strerror(errno));
266 drmModeFreeConnector(connector);
270 if (!connector->count_modes) {
271 drmModeFreeConnector(connector);
275 if (connector->connector_id != c->id) {
276 drmModeFreeConnector(connector);
280 for (j = 0; j < connector->count_modes; j++) {
281 c->mode = &connector->modes[j];
282 if (!strcmp(c->mode->name, c->mode_str))
286 /* Found it, break out */
290 drmModeFreeConnector(connector);
294 fprintf(stderr, "failed to find mode \"%s\"\n", c->mode_str);
298 /* Now get the encoder */
299 for (i = 0; i < resources->count_encoders; i++) {
300 c->encoder = drmModeGetEncoder(fd, resources->encoders[i]);
303 fprintf(stderr, "could not get encoder %i: %s\n",
304 resources->encoders[i], strerror(errno));
305 drmModeFreeEncoder(c->encoder);
309 if (c->encoder->encoder_id == connector->encoder_id)
312 drmModeFreeEncoder(c->encoder);
317 set_mode(struct connector *c, int count)
319 drmModeConnector *connector;
320 drmModeEncoder *encoder = NULL;
321 struct drm_mode_modeinfo *mode = NULL;
322 drm_intel_bufmgr *bufmgr;
324 unsigned int fb_id, *fb_ptr;
325 int i, j, size, ret, width, height, x;
330 for (i = 0; i < count; i++) {
331 connector_find_mode(&c[i]);
332 if (c[i].mode == NULL)
334 width += c[i].mode->hdisplay;
335 if (height < c[i].mode->vdisplay)
336 height = c[i].mode->vdisplay;
339 bufmgr = drm_intel_bufmgr_gem_init(fd, 2<<20);
341 fprintf(stderr, "failed to init bufmgr: %s\n", strerror(errno));
345 /* Mode size at 32 bpp */
346 size = width * height * 4;
348 bo = drm_intel_bo_alloc(bufmgr, "frontbuffer", size, 4096);
350 fprintf(stderr, "failed to alloc buffer: %s\n",
355 ret = drm_intel_gem_bo_map_gtt(bo);
357 fprintf(stderr, "failed to GTT map buffer: %s\n",
362 fb_ptr = bo->virtual;
364 /* paint the buffer with colored tiles */
365 for (i = 0; i < width * height; i++) {
367 fb_ptr[i] = 0x00130502 * (d.quot >> 6) + 0x000a1120 * (d.rem >> 6);
370 ret = drmModeAddFB(fd, width, height, 32, 32, width * 4, bo->handle,
373 fprintf(stderr, "failed to add fb: %s\n", strerror(errno));
378 for (i = 0; i < count; i++) {
379 if (c[i].mode == NULL)
381 ret = drmModeSetCrtc(fd, c[i].encoder->crtc_id, fb_id, x, 0,
382 &c[i].id, 1, c[i].mode);
383 x += c[i].mode->hdisplay;
386 fprintf(stderr, "failed to set mode: %s\n", strerror(errno));
393 extern int optind, opterr, optopt;
394 static char optstr[] = "ecpmfs:";
396 void usage(char *name)
398 fprintf(stderr, "usage: %s [-ecpmf]\n", name);
399 fprintf(stderr, "\t-e\tlist encoders\n");
400 fprintf(stderr, "\t-c\tlist connectors\n");
401 fprintf(stderr, "\t-p\tlist CRTCs (pipes)\n");
402 fprintf(stderr, "\t-m\tlist modes\n");
403 fprintf(stderr, "\t-f\tlist framebuffers\n");
404 fprintf(stderr, "\t-s <connector_id>:<mode>\tset a mode\n");
405 fprintf(stderr, "\n\tDefault is to dump all info.\n");
409 #define dump_resource(res) if (res) dump_##res()
411 int main(int argc, char **argv)
414 int encoders = 0, connectors = 0, crtcs = 0, framebuffers = 0;
415 char *modules[] = { "i915", "radeon" };
416 char *modeset = NULL, *mode, *connector;
417 int i, connector_id, count = 0;
418 struct connector con_args[2];
421 while ((c = getopt(argc, argv, optstr)) != -1) {
439 modeset = strdup(optarg);
440 if (sscanf(optarg, "%d:%64s",
442 &con_args[count].mode_str) != 2)
444 printf("setting mode %s on connector %d\n",
445 con_args[count].mode_str,
456 encoders = connectors = crtcs = modes = framebuffers = 1;
458 for (i = 0; i < ARRAY_SIZE(modules); i++) {
459 printf("trying to load module %s...", modules[i]);
460 fd = drmOpen(modules[i], NULL);
464 printf("success.\n");
469 if (i == ARRAY_SIZE(modules)) {
470 fprintf(stderr, "failed to load any modules, aborting.\n");
474 resources = drmModeGetResources(fd);
476 fprintf(stderr, "drmModeGetResources failed: %s\n",
482 dump_resource(encoders);
483 dump_resource(connectors);
484 dump_resource(crtcs);
485 dump_resource(framebuffers);
488 set_mode(con_args, count);
492 drmModeFreeResources(resources);