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.
245 void set_mode(int connector_id, char *mode_str)
247 drmModeConnector *connector;
248 drmModeEncoder *encoder = NULL;
249 struct drm_mode_modeinfo *mode = NULL;
250 drm_intel_bufmgr *bufmgr;
252 unsigned int fb_id, *fb_ptr;
253 int i, j, size, ret, width, height;
256 /* First, find the connector & mode */
257 for (i = 0; i < resources->count_connectors; i++) {
258 connector = drmModeGetConnector(fd, resources->connectors[i]);
261 fprintf(stderr, "could not get connector %i: %s\n",
262 resources->connectors[i], strerror(errno));
263 drmModeFreeConnector(connector);
267 if (!connector->count_modes) {
268 drmModeFreeConnector(connector);
272 if (connector->connector_id != connector_id) {
273 drmModeFreeConnector(connector);
277 for (j = 0; j < connector->count_modes; j++) {
278 mode = &connector->modes[j];
279 if (!strcmp(mode->name, mode_str))
283 /* Found it, break out */
287 drmModeFreeConnector(connector);
291 fprintf(stderr, "failed to find mode \"%s\"\n", mode_str);
295 width = mode->hdisplay;
296 height = mode->vdisplay;
298 /* Now get the encoder */
299 for (i = 0; i < resources->count_encoders; i++) {
300 encoder = drmModeGetEncoder(fd, resources->encoders[i]);
303 fprintf(stderr, "could not get encoder %i: %s\n",
304 resources->encoders[i], strerror(errno));
305 drmModeFreeEncoder(encoder);
309 if (encoder->encoder_id == connector->encoder_id)
312 drmModeFreeEncoder(encoder);
315 bufmgr = drm_intel_bufmgr_gem_init(fd, 2<<20);
317 fprintf(stderr, "failed to init bufmgr: %s\n", strerror(errno));
321 /* Mode size at 32 bpp */
322 size = width * height * 4;
324 bo = drm_intel_bo_alloc(bufmgr, "frontbuffer", size, 4096);
326 fprintf(stderr, "failed to alloc buffer: %s\n",
331 ret = drm_intel_gem_bo_map_gtt(bo);
333 fprintf(stderr, "failed to GTT map buffer: %s\n",
338 fb_ptr = bo->virtual;
340 /* paint the buffer with colored tiles */
341 for (i = 0; i < width * height; i++) {
343 fb_ptr[i] = 0x00130502 * (d.quot >> 6) + 0x000a1120 * (d.rem >> 6);
346 ret = drmModeAddFB(fd, width, height, 32, 32, width * 4, bo->handle,
349 fprintf(stderr, "failed to add fb: %s\n", strerror(errno));
353 ret = drmModeSetCrtc(fd, encoder->crtc_id, fb_id, 0, 0,
354 &connector->connector_id, 1, mode);
356 fprintf(stderr, "failed to set mode: %s\n", strerror(errno));
362 extern int optind, opterr, optopt;
363 static char optstr[] = "ecpmfs:";
365 void usage(char *name)
367 fprintf(stderr, "usage: %s [-ecpmf]\n", name);
368 fprintf(stderr, "\t-e\tlist encoders\n");
369 fprintf(stderr, "\t-c\tlist connectors\n");
370 fprintf(stderr, "\t-p\tlist CRTCs (pipes)\n");
371 fprintf(stderr, "\t-m\tlist modes\n");
372 fprintf(stderr, "\t-f\tlist framebuffers\n");
373 fprintf(stderr, "\t-s <connector_id>:<mode>\tset a mode\n");
374 fprintf(stderr, "\n\tDefault is to dump all info.\n");
378 #define dump_resource(res) if (res) dump_##res()
380 int main(int argc, char **argv)
383 int encoders = 0, connectors = 0, crtcs = 0, framebuffers = 0;
384 char *modules[] = { "i915", "radeon" };
385 char *modeset = NULL, *mode, *connector;
389 while ((c = getopt(argc, argv, optstr)) != -1) {
407 modeset = strdup(optarg);
416 encoders = connectors = crtcs = modes = framebuffers = 1;
418 for (i = 0; i < ARRAY_SIZE(modules); i++) {
419 printf("trying to load module %s...", modules[i]);
420 fd = drmOpen(modules[i], NULL);
424 printf("success.\n");
429 if (i == ARRAY_SIZE(modules)) {
430 fprintf(stderr, "failed to load any modules, aborting.\n");
434 resources = drmModeGetResources(fd);
436 fprintf(stderr, "drmModeGetResources failed: %s\n",
442 dump_resource(encoders);
443 dump_resource(connectors);
444 dump_resource(crtcs);
445 dump_resource(framebuffers);
448 connector = strtok(modeset, ":");
451 connector_id = atoi(connector);
453 mode = strtok(NULL, ":");
456 printf("setting connector %d to mode %s\n", connector_id,
458 set_mode(connector_id, mode);
464 drmModeFreeResources(resources);