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.
58 #include "xf86drmMode.h"
59 #include "drm_fourcc.h"
67 drmModeObjectProperties *props;
68 drmModePropertyRes **props_info;
69 drmModeModeInfo *mode;
73 drmModeEncoder *encoder;
77 drmModeConnector *connector;
78 drmModeObjectProperties *props;
79 drmModePropertyRes **props_info;
88 drmModeObjectProperties *props;
89 drmModePropertyRes **props_info;
94 drmModePlaneRes *plane_res;
97 struct encoder *encoders;
98 struct connector *connectors;
100 struct plane *planes;
106 struct resources *resources;
107 struct kms_driver *kms;
118 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
119 static inline int64_t U642I64(uint64_t val)
121 return (int64_t)*((int64_t *)&val);
129 #define type_name_fn(res) \
130 const char * res##_str(int type) { \
132 for (i = 0; i < ARRAY_SIZE(res##_names); i++) { \
133 if (res##_names[i].type == type) \
134 return res##_names[i].name; \
136 return "(invalid)"; \
139 struct type_name encoder_type_names[] = {
140 { DRM_MODE_ENCODER_NONE, "none" },
141 { DRM_MODE_ENCODER_DAC, "DAC" },
142 { DRM_MODE_ENCODER_TMDS, "TMDS" },
143 { DRM_MODE_ENCODER_LVDS, "LVDS" },
144 { DRM_MODE_ENCODER_TVDAC, "TVDAC" },
147 static type_name_fn(encoder_type)
149 struct type_name connector_status_names[] = {
150 { DRM_MODE_CONNECTED, "connected" },
151 { DRM_MODE_DISCONNECTED, "disconnected" },
152 { DRM_MODE_UNKNOWNCONNECTION, "unknown" },
155 static type_name_fn(connector_status)
157 struct type_name connector_type_names[] = {
158 { DRM_MODE_CONNECTOR_Unknown, "unknown" },
159 { DRM_MODE_CONNECTOR_VGA, "VGA" },
160 { DRM_MODE_CONNECTOR_DVII, "DVI-I" },
161 { DRM_MODE_CONNECTOR_DVID, "DVI-D" },
162 { DRM_MODE_CONNECTOR_DVIA, "DVI-A" },
163 { DRM_MODE_CONNECTOR_Composite, "composite" },
164 { DRM_MODE_CONNECTOR_SVIDEO, "s-video" },
165 { DRM_MODE_CONNECTOR_LVDS, "LVDS" },
166 { DRM_MODE_CONNECTOR_Component, "component" },
167 { DRM_MODE_CONNECTOR_9PinDIN, "9-pin DIN" },
168 { DRM_MODE_CONNECTOR_DisplayPort, "DP" },
169 { DRM_MODE_CONNECTOR_HDMIA, "HDMI-A" },
170 { DRM_MODE_CONNECTOR_HDMIB, "HDMI-B" },
171 { DRM_MODE_CONNECTOR_TV, "TV" },
172 { DRM_MODE_CONNECTOR_eDP, "eDP" },
175 static type_name_fn(connector_type)
177 #define bit_name_fn(res) \
178 const char * res##_str(int type) { \
180 const char *sep = ""; \
181 for (i = 0; i < ARRAY_SIZE(res##_names); i++) { \
182 if (type & (1 << i)) { \
183 printf("%s%s", sep, res##_names[i]); \
190 static const char *mode_type_names[] = {
200 static bit_name_fn(mode_type)
202 static const char *mode_flag_names[] = {
219 static bit_name_fn(mode_flag)
221 static void dump_encoders(struct device *dev)
223 drmModeEncoder *encoder;
226 printf("Encoders:\n");
227 printf("id\tcrtc\ttype\tpossible crtcs\tpossible clones\t\n");
228 for (i = 0; i < dev->resources->res->count_encoders; i++) {
229 encoder = dev->resources->encoders[i].encoder;
233 printf("%d\t%d\t%s\t0x%08x\t0x%08x\n",
236 encoder_type_str(encoder->encoder_type),
237 encoder->possible_crtcs,
238 encoder->possible_clones);
243 static void dump_mode(drmModeModeInfo *mode)
245 printf(" %s %d %d %d %d %d %d %d %d %d",
258 mode_flag_str(mode->flags);
260 mode_type_str(mode->type);
264 static void dump_blob(struct device *dev, uint32_t blob_id)
267 unsigned char *blob_data;
268 drmModePropertyBlobPtr blob;
270 blob = drmModeGetPropertyBlob(dev->fd, blob_id);
276 blob_data = blob->data;
278 for (i = 0; i < blob->length; i++) {
281 printf("%.2hhx", blob_data[i]);
285 drmModeFreePropertyBlob(blob);
288 static void dump_prop(struct device *dev, drmModePropertyPtr prop,
289 uint32_t prop_id, uint64_t value)
292 printf("\t%d", prop_id);
298 printf(" %s:\n", prop->name);
300 printf("\t\tflags:");
301 if (prop->flags & DRM_MODE_PROP_PENDING)
303 if (prop->flags & DRM_MODE_PROP_IMMUTABLE)
304 printf(" immutable");
305 if (drm_property_type_is(prop, DRM_MODE_PROP_SIGNED_RANGE))
306 printf(" signed range");
307 if (drm_property_type_is(prop, DRM_MODE_PROP_RANGE))
309 if (drm_property_type_is(prop, DRM_MODE_PROP_ENUM))
311 if (drm_property_type_is(prop, DRM_MODE_PROP_BITMASK))
313 if (drm_property_type_is(prop, DRM_MODE_PROP_BLOB))
315 if (drm_property_type_is(prop, DRM_MODE_PROP_OBJECT))
319 if (drm_property_type_is(prop, DRM_MODE_PROP_SIGNED_RANGE)) {
320 printf("\t\tvalues:");
321 for (i = 0; i < prop->count_values; i++)
322 printf(" %"PRId64, U642I64(prop->values[i]));
326 if (drm_property_type_is(prop, DRM_MODE_PROP_RANGE)) {
327 printf("\t\tvalues:");
328 for (i = 0; i < prop->count_values; i++)
329 printf(" %"PRIu64, prop->values[i]);
333 if (drm_property_type_is(prop, DRM_MODE_PROP_ENUM)) {
334 printf("\t\tenums:");
335 for (i = 0; i < prop->count_enums; i++)
336 printf(" %s=%llu", prop->enums[i].name,
337 prop->enums[i].value);
339 } else if (drm_property_type_is(prop, DRM_MODE_PROP_BITMASK)) {
340 printf("\t\tvalues:");
341 for (i = 0; i < prop->count_enums; i++)
342 printf(" %s=0x%llx", prop->enums[i].name,
343 (1LL << prop->enums[i].value));
346 assert(prop->count_enums == 0);
349 if (drm_property_type_is(prop, DRM_MODE_PROP_BLOB)) {
350 printf("\t\tblobs:\n");
351 for (i = 0; i < prop->count_blobs; i++)
352 dump_blob(dev, prop->blob_ids[i]);
355 assert(prop->count_blobs == 0);
358 printf("\t\tvalue:");
359 if (drm_property_type_is(prop, DRM_MODE_PROP_BLOB))
360 dump_blob(dev, value);
362 printf(" %"PRIu64"\n", value);
365 static void dump_connectors(struct device *dev)
369 printf("Connectors:\n");
370 printf("id\tencoder\tstatus\t\ttype\tsize (mm)\tmodes\tencoders\n");
371 for (i = 0; i < dev->resources->res->count_connectors; i++) {
372 struct connector *_connector = &dev->resources->connectors[i];
373 drmModeConnector *connector = _connector->connector;
377 printf("%d\t%d\t%s\t%s\t%dx%d\t\t%d\t",
378 connector->connector_id,
379 connector->encoder_id,
380 connector_status_str(connector->connection),
381 connector_type_str(connector->connector_type),
382 connector->mmWidth, connector->mmHeight,
383 connector->count_modes);
385 for (j = 0; j < connector->count_encoders; j++)
386 printf("%s%d", j > 0 ? ", " : "", connector->encoders[j]);
389 if (connector->count_modes) {
391 printf("\tname refresh (Hz) hdisp hss hse htot vdisp "
393 for (j = 0; j < connector->count_modes; j++)
394 dump_mode(&connector->modes[j]);
397 if (_connector->props) {
399 for (j = 0; j < (int)_connector->props->count_props; j++)
400 dump_prop(dev, _connector->props_info[j],
401 _connector->props->props[j],
402 _connector->props->prop_values[j]);
408 static void dump_crtcs(struct device *dev)
414 printf("id\tfb\tpos\tsize\n");
415 for (i = 0; i < dev->resources->res->count_crtcs; i++) {
416 struct crtc *_crtc = &dev->resources->crtcs[i];
417 drmModeCrtc *crtc = _crtc->crtc;
421 printf("%d\t%d\t(%d,%d)\t(%dx%d)\n",
425 crtc->width, crtc->height);
426 dump_mode(&crtc->mode);
430 for (j = 0; j < _crtc->props->count_props; j++)
431 dump_prop(dev, _crtc->props_info[j],
432 _crtc->props->props[j],
433 _crtc->props->prop_values[j]);
435 printf(" no properties found\n");
441 static void dump_framebuffers(struct device *dev)
446 printf("Frame buffers:\n");
447 printf("id\tsize\tpitch\n");
448 for (i = 0; i < dev->resources->res->count_fbs; i++) {
449 fb = dev->resources->fbs[i].fb;
453 printf("%u\t(%ux%u)\t%u\n",
455 fb->width, fb->height,
461 static void dump_planes(struct device *dev)
466 printf("id\tcrtc\tfb\tCRTC x,y\tx,y\tgamma size\tpossible crtcs\n");
468 if (!dev->resources->plane_res)
471 for (i = 0; i < dev->resources->plane_res->count_planes; i++) {
472 struct plane *plane = &dev->resources->planes[i];
473 drmModePlane *ovr = plane->plane;
477 printf("%d\t%d\t%d\t%d,%d\t\t%d,%d\t%-8d\t0x%08x\n",
478 ovr->plane_id, ovr->crtc_id, ovr->fb_id,
479 ovr->crtc_x, ovr->crtc_y, ovr->x, ovr->y,
480 ovr->gamma_size, ovr->possible_crtcs);
482 if (!ovr->count_formats)
486 for (j = 0; j < ovr->count_formats; j++)
487 printf(" %4.4s", (char *)&ovr->formats[j]);
492 for (j = 0; j < plane->props->count_props; j++)
493 dump_prop(dev, plane->props_info[j],
494 plane->props->props[j],
495 plane->props->prop_values[j]);
497 printf(" no properties found\n");
505 static void free_resources(struct resources *res)
510 #define free_resource(_res, __res, type, Type) \
513 if (!(_res)->type##s) \
515 for (i = 0; i < (int)(_res)->__res->count_##type##s; ++i) { \
516 if (!(_res)->type##s[i].type) \
518 drmModeFree##Type((_res)->type##s[i].type); \
520 free((_res)->type##s); \
523 #define free_properties(_res, __res, type) \
526 for (i = 0; i < (int)(_res)->__res->count_##type##s; ++i) { \
527 drmModeFreeObjectProperties(res->type##s[i].props); \
528 free(res->type##s[i].props_info); \
533 free_properties(res, res, crtc);
535 free_resource(res, res, crtc, Crtc);
536 free_resource(res, res, encoder, Encoder);
537 free_resource(res, res, connector, Connector);
538 free_resource(res, res, fb, FB);
540 drmModeFreeResources(res->res);
543 if (res->plane_res) {
544 free_properties(res, plane_res, plane);
546 free_resource(res, plane_res, plane, Plane);
548 drmModeFreePlaneResources(res->plane_res);
554 static struct resources *get_resources(struct device *dev)
556 struct resources *res;
559 res = malloc(sizeof *res);
563 memset(res, 0, sizeof *res);
565 drmSetClientCap(dev->fd, DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1);
567 res->res = drmModeGetResources(dev->fd);
569 fprintf(stderr, "drmModeGetResources failed: %s\n",
574 res->crtcs = malloc(res->res->count_crtcs * sizeof *res->crtcs);
575 res->encoders = malloc(res->res->count_encoders * sizeof *res->encoders);
576 res->connectors = malloc(res->res->count_connectors * sizeof *res->connectors);
577 res->fbs = malloc(res->res->count_fbs * sizeof *res->fbs);
579 if (!res->crtcs || !res->encoders || !res->connectors || !res->fbs)
582 memset(res->crtcs , 0, res->res->count_crtcs * sizeof *res->crtcs);
583 memset(res->encoders, 0, res->res->count_encoders * sizeof *res->encoders);
584 memset(res->connectors, 0, res->res->count_connectors * sizeof *res->connectors);
585 memset(res->fbs, 0, res->res->count_fbs * sizeof *res->fbs);
587 #define get_resource(_res, __res, type, Type) \
590 for (i = 0; i < (int)(_res)->__res->count_##type##s; ++i) { \
591 (_res)->type##s[i].type = \
592 drmModeGet##Type(dev->fd, (_res)->__res->type##s[i]); \
593 if (!(_res)->type##s[i].type) \
594 fprintf(stderr, "could not get %s %i: %s\n", \
595 #type, (_res)->__res->type##s[i], \
600 get_resource(res, res, crtc, Crtc);
601 get_resource(res, res, encoder, Encoder);
602 get_resource(res, res, connector, Connector);
603 get_resource(res, res, fb, FB);
605 #define get_properties(_res, __res, type, Type) \
608 for (i = 0; i < (int)(_res)->__res->count_##type##s; ++i) { \
609 struct type *obj = &res->type##s[i]; \
612 drmModeObjectGetProperties(dev->fd, obj->type->type##_id, \
613 DRM_MODE_OBJECT_##Type); \
616 "could not get %s %i properties: %s\n", \
617 #type, obj->type->type##_id, \
621 obj->props_info = malloc(obj->props->count_props * \
622 sizeof *obj->props_info); \
623 if (!obj->props_info) \
625 for (j = 0; j < obj->props->count_props; ++j) \
626 obj->props_info[j] = \
627 drmModeGetProperty(dev->fd, obj->props->props[j]); \
631 get_properties(res, res, crtc, CRTC);
632 get_properties(res, res, connector, CONNECTOR);
634 for (i = 0; i < res->res->count_crtcs; ++i)
635 res->crtcs[i].mode = &res->crtcs[i].crtc->mode;
637 res->plane_res = drmModeGetPlaneResources(dev->fd);
638 if (!res->plane_res) {
639 fprintf(stderr, "drmModeGetPlaneResources failed: %s\n",
644 res->planes = malloc(res->plane_res->count_planes * sizeof *res->planes);
648 memset(res->planes, 0, res->plane_res->count_planes * sizeof *res->planes);
650 get_resource(res, plane_res, plane, Plane);
651 get_properties(res, plane_res, plane, PLANE);
660 static int get_crtc_index(struct device *dev, uint32_t id)
664 for (i = 0; i < dev->resources->res->count_crtcs; ++i) {
665 drmModeCrtc *crtc = dev->resources->crtcs[i].crtc;
666 if (crtc && crtc->crtc_id == id)
673 static drmModeConnector *get_connector_by_id(struct device *dev, uint32_t id)
675 drmModeConnector *connector;
678 for (i = 0; i < dev->resources->res->count_connectors; i++) {
679 connector = dev->resources->connectors[i].connector;
680 if (connector && connector->connector_id == id)
687 static drmModeEncoder *get_encoder_by_id(struct device *dev, uint32_t id)
689 drmModeEncoder *encoder;
692 for (i = 0; i < dev->resources->res->count_encoders; i++) {
693 encoder = dev->resources->encoders[i].encoder;
694 if (encoder && encoder->encoder_id == id)
701 /* -----------------------------------------------------------------------------
706 * Mode setting with the kernel interfaces is a bit of a chore.
707 * First you have to find the connector in question and make sure the
708 * requested mode is available.
709 * Then you need to find the encoder attached to that connector so you
710 * can bind it with a free crtc.
714 unsigned int num_cons;
718 unsigned int vrefresh;
720 drmModeModeInfo *mode;
722 unsigned int fb_id[2], current_fb_id;
723 struct timeval start;
729 uint32_t crtc_id; /* the id of CRTC to bind to */
735 char format_str[5]; /* need to leave room for terminating \0 */
739 static drmModeModeInfo *
740 connector_find_mode(struct device *dev, uint32_t con_id, const char *mode_str,
741 const unsigned int vrefresh)
743 drmModeConnector *connector;
744 drmModeModeInfo *mode;
747 connector = get_connector_by_id(dev, con_id);
748 if (!connector || !connector->count_modes)
751 for (i = 0; i < connector->count_modes; i++) {
752 mode = &connector->modes[i];
753 if (!strcmp(mode->name, mode_str)) {
754 /* If the vertical refresh frequency is not specified then return the
755 * first mode that match with the name. Else, return the mode that match
756 * the name and the specified vertical refresh frequency.
760 else if (mode->vrefresh == vrefresh)
768 static struct crtc *pipe_find_crtc(struct device *dev, struct pipe_arg *pipe)
770 uint32_t possible_crtcs = ~0;
771 uint32_t active_crtcs = 0;
772 unsigned int crtc_idx;
776 for (i = 0; i < pipe->num_cons; ++i) {
777 uint32_t crtcs_for_connector = 0;
778 drmModeConnector *connector;
779 drmModeEncoder *encoder;
782 connector = get_connector_by_id(dev, pipe->con_ids[i]);
786 for (j = 0; j < connector->count_encoders; ++j) {
787 encoder = get_encoder_by_id(dev, connector->encoders[j]);
791 crtcs_for_connector |= encoder->possible_crtcs;
793 idx = get_crtc_index(dev, encoder->crtc_id);
795 active_crtcs |= 1 << idx;
798 possible_crtcs &= crtcs_for_connector;
804 /* Return the first possible and active CRTC if one exists, or the first
805 * possible CRTC otherwise.
807 if (possible_crtcs & active_crtcs)
808 crtc_idx = ffs(possible_crtcs & active_crtcs);
810 crtc_idx = ffs(possible_crtcs);
812 return &dev->resources->crtcs[crtc_idx - 1];
815 static int pipe_find_crtc_and_mode(struct device *dev, struct pipe_arg *pipe)
817 drmModeModeInfo *mode = NULL;
822 for (i = 0; i < (int)pipe->num_cons; i++) {
823 mode = connector_find_mode(dev, pipe->con_ids[i],
824 pipe->mode_str, pipe->vrefresh);
827 "failed to find mode \"%s\" for connector %u\n",
828 pipe->mode_str, pipe->con_ids[i]);
833 /* If the CRTC ID was specified, get the corresponding CRTC. Otherwise
834 * locate a CRTC that can be attached to all the connectors.
836 if (pipe->crtc_id != (uint32_t)-1) {
837 for (i = 0; i < dev->resources->res->count_crtcs; i++) {
838 struct crtc *crtc = &dev->resources->crtcs[i];
840 if (pipe->crtc_id == crtc->crtc->crtc_id) {
846 pipe->crtc = pipe_find_crtc(dev, pipe);
850 fprintf(stderr, "failed to find CRTC for pipe\n");
855 pipe->crtc->mode = mode;
860 /* -----------------------------------------------------------------------------
864 struct property_arg {
867 char name[DRM_PROP_NAME_LEN+1];
872 static void set_property(struct device *dev, struct property_arg *p)
874 drmModeObjectProperties *props = NULL;
875 drmModePropertyRes **props_info = NULL;
876 const char *obj_type;
883 #define find_object(_res, __res, type, Type) \
885 for (i = 0; i < (int)(_res)->__res->count_##type##s; ++i) { \
886 struct type *obj = &(_res)->type##s[i]; \
887 if (obj->type->type##_id != p->obj_id) \
889 p->obj_type = DRM_MODE_OBJECT_##Type; \
891 props = obj->props; \
892 props_info = obj->props_info; \
896 find_object(dev->resources, res, crtc, CRTC);
897 if (p->obj_type == 0)
898 find_object(dev->resources, res, connector, CONNECTOR);
899 if (p->obj_type == 0)
900 find_object(dev->resources, plane_res, plane, PLANE);
901 if (p->obj_type == 0) {
902 fprintf(stderr, "Object %i not found, can't set property\n",
908 fprintf(stderr, "%s %i has no properties\n",
909 obj_type, p->obj_id);
913 for (i = 0; i < (int)props->count_props; ++i) {
916 if (strcmp(props_info[i]->name, p->name) == 0)
920 if (i == (int)props->count_props) {
921 fprintf(stderr, "%s %i has no %s property\n",
922 obj_type, p->obj_id, p->name);
926 p->prop_id = props->props[i];
928 ret = drmModeObjectSetProperty(dev->fd, p->obj_id, p->obj_type,
929 p->prop_id, p->value);
931 fprintf(stderr, "failed to set %s %i property %s to %" PRIu64 ": %s\n",
932 obj_type, p->obj_id, p->name, p->value, strerror(errno));
935 /* -------------------------------------------------------------------------- */
938 page_flip_handler(int fd, unsigned int frame,
939 unsigned int sec, unsigned int usec, void *data)
941 struct pipe_arg *pipe;
942 unsigned int new_fb_id;
947 if (pipe->current_fb_id == pipe->fb_id[0])
948 new_fb_id = pipe->fb_id[1];
950 new_fb_id = pipe->fb_id[0];
952 drmModePageFlip(fd, pipe->crtc->crtc->crtc_id, new_fb_id,
953 DRM_MODE_PAGE_FLIP_EVENT, pipe);
954 pipe->current_fb_id = new_fb_id;
956 if (pipe->swap_count == 60) {
957 gettimeofday(&end, NULL);
958 t = end.tv_sec + end.tv_usec * 1e-6 -
959 (pipe->start.tv_sec + pipe->start.tv_usec * 1e-6);
960 fprintf(stderr, "freq: %.02fHz\n", pipe->swap_count / t);
961 pipe->swap_count = 0;
966 static int set_plane(struct device *dev, struct plane_arg *p)
969 uint32_t handles[4], pitches[4], offsets[4] = {0}; /* we only use [0] */
970 uint32_t plane_id = 0;
971 struct kms_bo *plane_bo;
972 uint32_t plane_flags = 0;
973 int crtc_x, crtc_y, crtc_w, crtc_h;
974 struct crtc *crtc = NULL;
978 /* Find an unused plane which can be connected to our CRTC. Find the
979 * CRTC index first, then iterate over available planes.
981 for (i = 0; i < (unsigned int)dev->resources->res->count_crtcs; i++) {
982 if (p->crtc_id == dev->resources->res->crtcs[i]) {
983 crtc = &dev->resources->crtcs[i];
990 fprintf(stderr, "CRTC %u not found\n", p->crtc_id);
994 for (i = 0; i < dev->resources->plane_res->count_planes && !plane_id; i++) {
995 ovr = dev->resources->planes[i].plane;
999 if ((ovr->possible_crtcs & (1 << pipe)) && !ovr->crtc_id)
1000 plane_id = ovr->plane_id;
1004 fprintf(stderr, "no unused plane available for CRTC %u\n",
1005 crtc->crtc->crtc_id);
1009 fprintf(stderr, "testing %dx%d@%s overlay plane %u\n",
1010 p->w, p->h, p->format_str, plane_id);
1012 plane_bo = create_test_buffer(dev->kms, p->fourcc, p->w, p->h, handles,
1013 pitches, offsets, PATTERN_TILES);
1014 if (plane_bo == NULL)
1017 /* just use single plane format for now.. */
1018 if (drmModeAddFB2(dev->fd, p->w, p->h, p->fourcc,
1019 handles, pitches, offsets, &p->fb_id, plane_flags)) {
1020 fprintf(stderr, "failed to add fb: %s\n", strerror(errno));
1024 crtc_w = p->w * p->scale;
1025 crtc_h = p->h * p->scale;
1026 if (!p->has_position) {
1027 /* Default to the middle of the screen */
1028 crtc_x = (crtc->mode->hdisplay - crtc_w) / 2;
1029 crtc_y = (crtc->mode->vdisplay - crtc_h) / 2;
1035 /* note src coords (last 4 args) are in Q16 format */
1036 if (drmModeSetPlane(dev->fd, plane_id, crtc->crtc->crtc_id, p->fb_id,
1037 plane_flags, crtc_x, crtc_y, crtc_w, crtc_h,
1038 0, 0, p->w << 16, p->h << 16)) {
1039 fprintf(stderr, "failed to enable plane: %s\n",
1044 ovr->crtc_id = crtc->crtc->crtc_id;
1049 static void set_mode(struct device *dev, struct pipe_arg *pipes, unsigned int count)
1051 uint32_t handles[4], pitches[4], offsets[4] = {0}; /* we only use [0] */
1058 dev->mode.width = 0;
1059 dev->mode.height = 0;
1061 for (i = 0; i < count; i++) {
1062 struct pipe_arg *pipe = &pipes[i];
1064 ret = pipe_find_crtc_and_mode(dev, pipe);
1068 dev->mode.width += pipe->mode->hdisplay;
1069 if (dev->mode.height < pipe->mode->vdisplay)
1070 dev->mode.height = pipe->mode->vdisplay;
1073 bo = create_test_buffer(dev->kms, pipes[0].fourcc,
1074 dev->mode.width, dev->mode.height,
1075 handles, pitches, offsets, PATTERN_SMPTE);
1079 ret = drmModeAddFB2(dev->fd, dev->mode.width, dev->mode.height,
1080 pipes[0].fourcc, handles, pitches, offsets, &fb_id, 0);
1082 fprintf(stderr, "failed to add fb (%ux%u): %s\n",
1083 dev->mode.width, dev->mode.height, strerror(errno));
1088 for (i = 0; i < count; i++) {
1089 struct pipe_arg *pipe = &pipes[i];
1091 if (pipe->mode == NULL)
1094 printf("setting mode %s-%dHz@%s on connectors ",
1095 pipe->mode_str, pipe->mode->vrefresh, pipe->format_str);
1096 for (j = 0; j < pipe->num_cons; ++j)
1097 printf("%u, ", pipe->con_ids[j]);
1098 printf("crtc %d\n", pipe->crtc->crtc->crtc_id);
1100 ret = drmModeSetCrtc(dev->fd, pipe->crtc->crtc->crtc_id, fb_id,
1101 x, 0, pipe->con_ids, pipe->num_cons,
1104 /* XXX: Actually check if this is needed */
1105 drmModeDirtyFB(dev->fd, fb_id, NULL, 0);
1107 x += pipe->mode->hdisplay;
1110 fprintf(stderr, "failed to set mode: %s\n", strerror(errno));
1116 dev->mode.fb_id = fb_id;
1119 static void set_planes(struct device *dev, struct plane_arg *p, unsigned int count)
1123 /* set up planes/overlays */
1124 for (i = 0; i < count; i++)
1125 if (set_plane(dev, &p[i]))
1129 static void set_cursors(struct device *dev, struct pipe_arg *pipes, unsigned int count)
1131 uint32_t handles[4], pitches[4], offsets[4] = {0}; /* we only use [0] */
1136 /* maybe make cursor width/height configurable some day */
1140 /* create cursor bo.. just using PATTERN_PLAIN as it has
1143 bo = create_test_buffer(dev->kms, DRM_FORMAT_ARGB8888,
1144 cw, ch, handles, pitches, offsets, PATTERN_PLAIN);
1148 for (i = 0; i < count; i++) {
1149 struct pipe_arg *pipe = &pipes[i];
1150 ret = cursor_init(dev->fd, handles[0],
1151 pipe->crtc->crtc->crtc_id,
1152 pipe->mode->hdisplay, pipe->mode->vdisplay,
1155 fprintf(stderr, "failed to init cursor for CRTC[%u]\n",
1164 static void clear_cursors(struct device *dev)
1169 static void test_page_flip(struct device *dev, struct pipe_arg *pipes, unsigned int count)
1171 uint32_t handles[4], pitches[4], offsets[4] = {0}; /* we only use [0] */
1172 unsigned int other_fb_id;
1173 struct kms_bo *other_bo;
1174 drmEventContext evctx;
1178 other_bo = create_test_buffer(dev->kms, pipes[0].fourcc,
1179 dev->mode.width, dev->mode.height,
1180 handles, pitches, offsets, PATTERN_PLAIN);
1181 if (other_bo == NULL)
1184 ret = drmModeAddFB2(dev->fd, dev->mode.width, dev->mode.height,
1185 pipes[0].fourcc, handles, pitches, offsets,
1188 fprintf(stderr, "failed to add fb: %s\n", strerror(errno));
1192 for (i = 0; i < count; i++) {
1193 struct pipe_arg *pipe = &pipes[i];
1195 if (pipe->mode == NULL)
1198 ret = drmModePageFlip(dev->fd, pipe->crtc->crtc->crtc_id,
1199 other_fb_id, DRM_MODE_PAGE_FLIP_EVENT,
1202 fprintf(stderr, "failed to page flip: %s\n", strerror(errno));
1205 gettimeofday(&pipe->start, NULL);
1206 pipe->swap_count = 0;
1207 pipe->fb_id[0] = dev->mode.fb_id;
1208 pipe->fb_id[1] = other_fb_id;
1209 pipe->current_fb_id = other_fb_id;
1212 memset(&evctx, 0, sizeof evctx);
1213 evctx.version = DRM_EVENT_CONTEXT_VERSION;
1214 evctx.vblank_handler = NULL;
1215 evctx.page_flip_handler = page_flip_handler;
1219 struct pollfd pfd[2];
1222 pfd[0].events = POLLIN;
1224 pfd[1].events = POLLIN;
1226 if (poll(pfd, 2, -1) < 0) {
1227 fprintf(stderr, "poll error\n");
1234 struct timeval timeout = { .tv_sec = 3, .tv_usec = 0 };
1240 FD_SET(dev->fd, &fds);
1241 ret = select(dev->fd + 1, &fds, NULL, NULL, &timeout);
1244 fprintf(stderr, "select timed out or error (ret %d)\n",
1247 } else if (FD_ISSET(0, &fds)) {
1252 drmHandleEvent(dev->fd, &evctx);
1255 kms_bo_destroy(&other_bo);
1258 #define min(a, b) ((a) < (b) ? (a) : (b))
1260 static int parse_connector(struct pipe_arg *pipe, const char *arg)
1268 pipe->crtc_id = (uint32_t)-1;
1269 strcpy(pipe->format_str, "XR24");
1271 /* Count the number of connectors and allocate them. */
1273 for (p = arg; isdigit(*p) || *p == ','; ++p) {
1278 pipe->con_ids = malloc(pipe->num_cons * sizeof *pipe->con_ids);
1279 if (pipe->con_ids == NULL)
1282 /* Parse the connectors. */
1283 for (i = 0, p = arg; i < pipe->num_cons; ++i, p = endp + 1) {
1284 pipe->con_ids[i] = strtoul(p, &endp, 10);
1289 if (i != pipe->num_cons - 1)
1292 /* Parse the remaining parameters. */
1295 pipe->crtc_id = strtoul(arg, &endp, 10);
1302 /* Search for the vertical refresh or the format. */
1303 p = strpbrk(arg, "-@");
1305 p = arg + strlen(arg);
1306 len = min(sizeof pipe->mode_str - 1, (unsigned int)(p - arg));
1307 strncpy(pipe->mode_str, arg, len);
1308 pipe->mode_str[len] = '\0';
1311 pipe->vrefresh = strtoul(p + 1, &endp, 10);
1316 strncpy(pipe->format_str, p + 1, 4);
1317 pipe->format_str[4] = '\0';
1320 pipe->fourcc = format_fourcc(pipe->format_str);
1321 if (pipe->fourcc == 0) {
1322 fprintf(stderr, "unknown format %s\n", pipe->format_str);
1329 static int parse_plane(struct plane_arg *plane, const char *p)
1333 memset(plane, 0, sizeof *plane);
1335 plane->crtc_id = strtoul(p, &end, 10);
1340 plane->w = strtoul(p, &end, 10);
1345 plane->h = strtoul(p, &end, 10);
1347 if (*end == '+' || *end == '-') {
1348 plane->x = strtol(end, &end, 10);
1349 if (*end != '+' && *end != '-')
1351 plane->y = strtol(end, &end, 10);
1353 plane->has_position = true;
1358 plane->scale = strtod(p, &end);
1359 if (plane->scale <= 0.0)
1370 strcpy(plane->format_str, p);
1372 strcpy(plane->format_str, "XR24");
1375 plane->fourcc = format_fourcc(plane->format_str);
1376 if (plane->fourcc == 0) {
1377 fprintf(stderr, "unknown format %s\n", plane->format_str);
1384 static int parse_property(struct property_arg *p, const char *arg)
1386 if (sscanf(arg, "%d:%32[^:]:%" SCNu64, &p->obj_id, p->name, &p->value) != 3)
1390 p->name[DRM_PROP_NAME_LEN] = '\0';
1395 static void usage(char *name)
1397 fprintf(stderr, "usage: %s [-cDdefMPpsCvw]\n", name);
1399 fprintf(stderr, "\n Query options:\n\n");
1400 fprintf(stderr, "\t-c\tlist connectors\n");
1401 fprintf(stderr, "\t-e\tlist encoders\n");
1402 fprintf(stderr, "\t-f\tlist framebuffers\n");
1403 fprintf(stderr, "\t-p\tlist CRTCs and planes (pipes)\n");
1405 fprintf(stderr, "\n Test options:\n\n");
1406 fprintf(stderr, "\t-P <crtc_id>:<w>x<h>[+<x>+<y>][*<scale>][@<format>]\tset a plane\n");
1407 fprintf(stderr, "\t-s <connector_id>[,<connector_id>][@<crtc_id>]:<mode>[-<vrefresh>][@<format>]\tset a mode\n");
1408 fprintf(stderr, "\t-C\ttest hw cursor\n");
1409 fprintf(stderr, "\t-v\ttest vsynced page flipping\n");
1410 fprintf(stderr, "\t-w <obj_id>:<prop_name>:<value>\tset property\n");
1412 fprintf(stderr, "\n Generic options:\n\n");
1413 fprintf(stderr, "\t-d\tdrop master after mode set\n");
1414 fprintf(stderr, "\t-M module\tuse the given driver\n");
1415 fprintf(stderr, "\t-D device\tuse the given device\n");
1417 fprintf(stderr, "\n\tDefault is to dump all info.\n");
1421 static int page_flipping_supported(void)
1423 /*FIXME: generic ioctl needed? */
1427 struct drm_i915_getparam gp;
1429 gp.param = I915_PARAM_HAS_PAGEFLIPPING;
1432 ret = drmCommandWriteRead(fd, DRM_I915_GETPARAM, &gp, sizeof(gp));
1434 fprintf(stderr, "drm_i915_getparam: %m\n");
1442 static int cursor_supported(void)
1444 /*FIXME: generic ioctl needed? */
1448 static char optstr[] = "cdD:efM:P:ps:Cvw:";
1450 int main(int argc, char **argv)
1455 int encoders = 0, connectors = 0, crtcs = 0, planes = 0, framebuffers = 0;
1456 int drop_master = 0;
1458 int test_cursor = 0;
1459 const char *modules[] = { "i915", "radeon", "nouveau", "vmwgfx", "omapdrm", "exynos", "tilcdc", "msm", "sti", "tegra" };
1460 char *device = NULL;
1461 char *module = NULL;
1463 int count = 0, plane_count = 0;
1464 unsigned int prop_count = 0;
1465 struct pipe_arg *pipe_args = NULL;
1466 struct plane_arg *plane_args = NULL;
1467 struct property_arg *prop_args = NULL;
1468 unsigned int args = 0;
1471 memset(&dev, 0, sizeof dev);
1474 while ((c = getopt(argc, argv, optstr)) != -1) {
1496 /* Preserve the default behaviour of dumping all information. */
1500 plane_args = realloc(plane_args,
1501 (plane_count + 1) * sizeof *plane_args);
1502 if (plane_args == NULL) {
1503 fprintf(stderr, "memory allocation failed\n");
1507 if (parse_plane(&plane_args[plane_count], optarg) < 0)
1517 pipe_args = realloc(pipe_args,
1518 (count + 1) * sizeof *pipe_args);
1519 if (pipe_args == NULL) {
1520 fprintf(stderr, "memory allocation failed\n");
1524 if (parse_connector(&pipe_args[count], optarg) < 0)
1536 prop_args = realloc(prop_args,
1537 (prop_count + 1) * sizeof *prop_args);
1538 if (prop_args == NULL) {
1539 fprintf(stderr, "memory allocation failed\n");
1543 if (parse_property(&prop_args[prop_count], optarg) < 0)
1555 encoders = connectors = crtcs = planes = framebuffers = 1;
1558 dev.fd = drmOpen(module, device);
1560 fprintf(stderr, "failed to open device '%s'.\n", module);
1564 for (i = 0; i < ARRAY_SIZE(modules); i++) {
1565 printf("trying to open device '%s'...", modules[i]);
1566 dev.fd = drmOpen(modules[i], device);
1568 printf("failed.\n");
1570 printf("success.\n");
1576 fprintf(stderr, "no device found.\n");
1581 if (test_vsync && !page_flipping_supported()) {
1582 fprintf(stderr, "page flipping not supported by drm.\n");
1586 if (test_vsync && !count) {
1587 fprintf(stderr, "page flipping requires at least one -s option.\n");
1591 if (test_cursor && !cursor_supported()) {
1592 fprintf(stderr, "hw cursor not supported by drm.\n");
1596 dev.resources = get_resources(&dev);
1597 if (!dev.resources) {
1602 #define dump_resource(dev, res) if (res) dump_##res(dev)
1604 dump_resource(&dev, encoders);
1605 dump_resource(&dev, connectors);
1606 dump_resource(&dev, crtcs);
1607 dump_resource(&dev, planes);
1608 dump_resource(&dev, framebuffers);
1610 for (i = 0; i < prop_count; ++i)
1611 set_property(&dev, &prop_args[i]);
1613 if (count || plane_count) {
1614 ret = kms_create(dev.fd, &dev.kms);
1616 fprintf(stderr, "failed to create kms driver: %s\n",
1622 set_mode(&dev, pipe_args, count);
1625 set_planes(&dev, plane_args, plane_count);
1628 set_cursors(&dev, pipe_args, count);
1631 test_page_flip(&dev, pipe_args, count);
1634 drmDropMaster(dev.fd);
1639 clear_cursors(&dev);
1641 kms_bo_destroy(&dev.mode.bo);
1642 kms_destroy(&dev.kms);
1645 free_resources(dev.resources);