d750f4c38a19320259be4bf042fd7dbb77e4c812
[platform/upstream/libdrm.git] / tests / modetest / modetest.c
1 /*
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>
7  *
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:
14  *
15  * The above copyright notice and this permission notice shall be included in
16  * all copies or substantial portions of the Software.
17  *
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
24  * IN THE SOFTWARE.
25  */
26
27 /*
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.
36  *
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.
39  */
40 #include <assert.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <stdint.h>
44 #include <unistd.h>
45 #include <string.h>
46 #include <errno.h>
47
48 #include "xf86drm.h"
49 #include "xf86drmMode.h"
50 #include "intel_bufmgr.h"
51
52 drmModeRes *resources;
53 int fd, modes;
54
55 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
56
57 struct type_name {
58         int type;
59         char *name;
60 };
61
62 #define type_name_fn(res) \
63 char * res##_str(int type) {                    \
64         int i;                                          \
65         for (i = 0; i < ARRAY_SIZE(res##_names); i++) { \
66                 if (res##_names[i].type == type)        \
67                         return res##_names[i].name;     \
68         }                                               \
69         return "(invalid)";                             \
70 }
71
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" },
78 };
79
80 type_name_fn(encoder_type)
81
82 struct type_name connector_status_names[] = {
83         { DRM_MODE_CONNECTED, "connected" },
84         { DRM_MODE_DISCONNECTED, "disconnected" },
85         { DRM_MODE_UNKNOWNCONNECTION, "unknown" },
86 };
87
88 type_name_fn(connector_status)
89
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" },
104 };
105
106 type_name_fn(connector_type)
107
108 void dump_encoders(void)
109 {
110         drmModeEncoder *encoder;
111         int i;
112
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]);
117
118                 if (!encoder) {
119                         fprintf(stderr, "could not get encoder %i: %s\n",
120                                 resources->encoders[i], strerror(errno));
121                         continue;
122                 }
123                 printf("%d\t%d\t%s\t0x%08x\t0x%08x\n",
124                        encoder->encoder_id,
125                        encoder->crtc_id,
126                        encoder_type_str(encoder->encoder_type),
127                        encoder->possible_crtcs,
128                        encoder->possible_clones);
129                 drmModeFreeEncoder(encoder);
130         }
131         printf("\n");
132 }
133
134 void dump_mode(struct drm_mode_modeinfo *mode)
135 {
136         printf("  %s %.02f %d %d %d %d %d %d %d %d\n",
137                mode->name,
138                (float)mode->vrefresh / 1000,
139                mode->hdisplay,
140                mode->hsync_start,
141                mode->hsync_end,
142                mode->htotal,
143                mode->vdisplay,
144                mode->vsync_start,
145                mode->vsync_end,
146                mode->vtotal);
147 }
148
149 void dump_connectors(void)
150 {
151         drmModeConnector *connector;
152         int i, j;
153
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]);
158
159                 if (!connector) {
160                         fprintf(stderr, "could not get connector %i: %s\n",
161                                 resources->connectors[i], strerror(errno));
162                         continue;
163                 }
164
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);
172
173                 if (!connector->count_modes)
174                         continue;
175
176                 printf("  modes:\n");
177                 printf("  name refresh (Hz) hdisp hss hse htot vdisp "
178                        "vss vse vtot)\n");
179                 for (j = 0; j < connector->count_modes; j++)
180                         dump_mode(&connector->modes[j]);
181
182                 drmModeFreeConnector(connector);
183         }
184         printf("\n");
185 }
186
187 void dump_crtcs(void)
188 {
189         drmModeCrtc *crtc;
190         int i;
191
192         printf("CRTCs:\n");
193         printf("id\tfb\tpos\tsize\n");
194         for (i = 0; i < resources->count_crtcs; i++) {
195                 crtc = drmModeGetCrtc(fd, resources->crtcs[i]);
196
197                 if (!crtc) {
198                         fprintf(stderr, "could not get crtc %i: %s\n",
199                                 resources->crtcs[i], strerror(errno));
200                         continue;
201                 }
202                 printf("%d\t%d\t(%d,%d)\t(%dx%d)\n",
203                        crtc->crtc_id,
204                        crtc->buffer_id,
205                        crtc->x, crtc->y,
206                        crtc->width, crtc->height);
207                 dump_mode(&crtc->mode);
208
209                 drmModeFreeCrtc(crtc);
210         }
211         printf("\n");
212 }
213
214 void dump_framebuffers(void)
215 {
216         drmModeFB *fb;
217         int i;
218
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]);
223
224                 if (!fb) {
225                         fprintf(stderr, "could not get fb %i: %s\n",
226                                 resources->fbs[i], strerror(errno));
227                         continue;
228                 }
229                 printf("%d\t(%dx%d)\t%d\n",
230                        fb->fb_id,
231                        fb->width, fb->height);
232
233                 drmModeFreeFB(fb);
234         }
235         printf("\n");
236 }
237
238 /*
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.
244  */
245 void set_mode(int connector_id, char *mode_str)
246 {
247         drmModeConnector *connector;
248         drmModeEncoder *encoder = NULL;
249         struct drm_mode_modeinfo *mode = NULL;
250         drm_intel_bufmgr *bufmgr;
251         drm_intel_bo *bo;
252         unsigned int fb_id, *fb_ptr;
253         int i, j, size, ret, width, height;
254         div_t d;
255
256         /* First, find the connector & mode */
257         for (i = 0; i < resources->count_connectors; i++) {
258                 connector = drmModeGetConnector(fd, resources->connectors[i]);
259
260                 if (!connector) {
261                         fprintf(stderr, "could not get connector %i: %s\n",
262                                 resources->connectors[i], strerror(errno));
263                         drmModeFreeConnector(connector);
264                         continue;
265                 }
266
267                 if (!connector->count_modes) {
268                         drmModeFreeConnector(connector);
269                         continue;
270                 }
271
272                 if (connector->connector_id != connector_id) {
273                         drmModeFreeConnector(connector);
274                         continue;
275                 }
276
277                 for (j = 0; j < connector->count_modes; j++) {
278                         mode = &connector->modes[j];
279                         if (!strcmp(mode->name, mode_str))
280                                 break;
281                 }
282
283                 /* Found it, break out */
284                 if (mode)
285                         break;
286
287                 drmModeFreeConnector(connector);
288         }
289
290         if (!mode) {
291                 fprintf(stderr, "failed to find mode \"%s\"\n", mode_str);
292                 return;
293         }
294
295         width = mode->hdisplay;
296         height = mode->vdisplay;
297
298         /* Now get the encoder */
299         for (i = 0; i < resources->count_encoders; i++) {
300                 encoder = drmModeGetEncoder(fd, resources->encoders[i]);
301
302                 if (!encoder) {
303                         fprintf(stderr, "could not get encoder %i: %s\n",
304                                 resources->encoders[i], strerror(errno));
305                         drmModeFreeEncoder(encoder);
306                         continue;
307                 }
308
309                 if (encoder->encoder_id  == connector->encoder_id)
310                         break;
311
312                 drmModeFreeEncoder(encoder);
313         }
314
315         bufmgr = drm_intel_bufmgr_gem_init(fd, 2<<20);
316         if (!bufmgr) {
317                 fprintf(stderr, "failed to init bufmgr: %s\n", strerror(errno));
318                 return;
319         }
320
321         /* Mode size at 32 bpp */
322         size = width * height * 4;
323
324         bo = drm_intel_bo_alloc(bufmgr, "frontbuffer", size, 4096);
325         if (!bo) {
326                 fprintf(stderr, "failed to alloc buffer: %s\n",
327                         strerror(errno));
328                 return;
329         }
330
331         ret = drm_intel_gem_bo_map_gtt(bo);
332         if (ret) {
333                 fprintf(stderr, "failed to GTT map buffer: %s\n",
334                         strerror(errno));
335                 return;
336         }
337
338         fb_ptr = bo->virtual;
339
340         /* paint the buffer with colored tiles */
341         for (i = 0; i < width * height; i++) {
342                 d = div(i, width);
343                 fb_ptr[i] = 0x00130502 * (d.quot >> 6) + 0x000a1120 * (d.rem >> 6);
344         }
345
346         ret = drmModeAddFB(fd, width, height, 32, 32, width * 4, bo->handle,
347                            &fb_id);
348         if (ret) {
349                 fprintf(stderr, "failed to add fb: %s\n", strerror(errno));
350                 return;
351         }
352
353         ret = drmModeSetCrtc(fd, encoder->crtc_id, fb_id, 0, 0,
354                              &connector->connector_id, 1, mode);
355         if (ret) {
356                 fprintf(stderr, "failed to set mode: %s\n", strerror(errno));
357                 return;
358         }
359 }
360
361 extern char *optarg;
362 extern int optind, opterr, optopt;
363 static char optstr[] = "ecpmfs:";
364
365 void usage(char *name)
366 {
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");
375         exit(0);
376 }
377
378 #define dump_resource(res) if (res) dump_##res()
379
380 int main(int argc, char **argv)
381 {
382         int c;
383         int encoders = 0, connectors = 0, crtcs = 0, framebuffers = 0;
384         char *modules[] = { "i915", "radeon" };
385         char *modeset = NULL, *mode, *connector;
386         int i, connector_id;
387
388         opterr = 0;
389         while ((c = getopt(argc, argv, optstr)) != -1) {
390                 switch (c) {
391                 case 'e':
392                         encoders = 1;
393                         break;
394                 case 'c':
395                         connectors = 1;
396                         break;
397                 case 'p':
398                         crtcs = 1;
399                         break;
400                 case 'm':
401                         modes = 1;
402                         break;
403                 case 'f':
404                         framebuffers = 1;
405                         break;
406                 case 's':
407                         modeset = strdup(optarg);
408                         break;
409                 default:
410                         usage(argv[0]);
411                         break;
412                 }
413         }
414
415         if (argc == 1)
416                 encoders = connectors = crtcs = modes = framebuffers = 1;
417
418         for (i = 0; i < ARRAY_SIZE(modules); i++) {
419                 printf("trying to load module %s...", modules[i]);
420                 fd = drmOpen(modules[i], NULL);
421                 if (fd < 0) {
422                         printf("failed.\n");
423                 } else {
424                         printf("success.\n");
425                         break;
426                 }
427         }
428
429         if (i == ARRAY_SIZE(modules)) {
430                 fprintf(stderr, "failed to load any modules, aborting.\n");
431                 return -1;
432         }
433
434         resources = drmModeGetResources(fd);
435         if (!resources) {
436                 fprintf(stderr, "drmModeGetResources failed: %s\n",
437                         strerror(errno));
438                 drmClose(fd);
439                 return 1;
440         }
441
442         dump_resource(encoders);
443         dump_resource(connectors);
444         dump_resource(crtcs);
445         dump_resource(framebuffers);
446
447         if (modeset) {
448                 connector = strtok(modeset, ":");
449                 if (!connector)
450                         usage(argv[0]);
451                 connector_id = atoi(connector);
452
453                 mode = strtok(NULL, ":");
454                 if (!mode)
455                         usage(argv[0]);
456                 printf("setting connector %d to mode %s\n", connector_id,
457                        mode);
458                 set_mode(connector_id, mode);
459                 sleep(3);
460         }
461
462         sleep(3);
463
464         drmModeFreeResources(resources);
465
466         return 0;
467 }