modetest.c revision 1ec3c44bdd38051d870f64d0b2cc7dbd59760386
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 41#ifdef HAVE_CONFIG_H 42#include "config.h" 43#endif 44 45#include <assert.h> 46#include <ctype.h> 47#include <stdbool.h> 48#include <stdio.h> 49#include <stdlib.h> 50#include <stdint.h> 51#include <inttypes.h> 52#include <unistd.h> 53#include <string.h> 54#include <strings.h> 55#include <errno.h> 56#include <sys/poll.h> 57#include <sys/time.h> 58 59#include "xf86drm.h" 60#include "xf86drmMode.h" 61#include "drm_fourcc.h" 62 63#include "util/common.h" 64#include "util/format.h" 65#include "util/pattern.h" 66 67#include "buffers.h" 68#include "cursor.h" 69 70struct crtc { 71 drmModeCrtc *crtc; 72 drmModeObjectProperties *props; 73 drmModePropertyRes **props_info; 74 drmModeModeInfo *mode; 75}; 76 77struct encoder { 78 drmModeEncoder *encoder; 79}; 80 81struct connector { 82 drmModeConnector *connector; 83 drmModeObjectProperties *props; 84 drmModePropertyRes **props_info; 85 char *name; 86}; 87 88struct fb { 89 drmModeFB *fb; 90}; 91 92struct plane { 93 drmModePlane *plane; 94 drmModeObjectProperties *props; 95 drmModePropertyRes **props_info; 96}; 97 98struct resources { 99 drmModeRes *res; 100 drmModePlaneRes *plane_res; 101 102 struct crtc *crtcs; 103 struct encoder *encoders; 104 struct connector *connectors; 105 struct fb *fbs; 106 struct plane *planes; 107}; 108 109struct device { 110 int fd; 111 112 struct resources *resources; 113 114 struct { 115 unsigned int width; 116 unsigned int height; 117 118 unsigned int fb_id; 119 struct bo *bo; 120 struct bo *cursor_bo; 121 } mode; 122}; 123 124static inline int64_t U642I64(uint64_t val) 125{ 126 return (int64_t)*((int64_t *)&val); 127} 128 129struct type_name { 130 int type; 131 const char *name; 132}; 133 134#define type_name_fn(res) \ 135const char * res##_str(int type) { \ 136 unsigned int i; \ 137 for (i = 0; i < ARRAY_SIZE(res##_names); i++) { \ 138 if (res##_names[i].type == type) \ 139 return res##_names[i].name; \ 140 } \ 141 return "(invalid)"; \ 142} 143 144struct type_name encoder_type_names[] = { 145 { DRM_MODE_ENCODER_NONE, "none" }, 146 { DRM_MODE_ENCODER_DAC, "DAC" }, 147 { DRM_MODE_ENCODER_TMDS, "TMDS" }, 148 { DRM_MODE_ENCODER_LVDS, "LVDS" }, 149 { DRM_MODE_ENCODER_TVDAC, "TVDAC" }, 150 { DRM_MODE_ENCODER_VIRTUAL, "Virtual" }, 151 { DRM_MODE_ENCODER_DSI, "DSI" }, 152 { DRM_MODE_ENCODER_DPMST, "DPMST" }, 153}; 154 155static type_name_fn(encoder_type) 156 157struct type_name connector_status_names[] = { 158 { DRM_MODE_CONNECTED, "connected" }, 159 { DRM_MODE_DISCONNECTED, "disconnected" }, 160 { DRM_MODE_UNKNOWNCONNECTION, "unknown" }, 161}; 162 163static type_name_fn(connector_status) 164 165struct type_name connector_type_names[] = { 166 { DRM_MODE_CONNECTOR_Unknown, "unknown" }, 167 { DRM_MODE_CONNECTOR_VGA, "VGA" }, 168 { DRM_MODE_CONNECTOR_DVII, "DVI-I" }, 169 { DRM_MODE_CONNECTOR_DVID, "DVI-D" }, 170 { DRM_MODE_CONNECTOR_DVIA, "DVI-A" }, 171 { DRM_MODE_CONNECTOR_Composite, "composite" }, 172 { DRM_MODE_CONNECTOR_SVIDEO, "s-video" }, 173 { DRM_MODE_CONNECTOR_LVDS, "LVDS" }, 174 { DRM_MODE_CONNECTOR_Component, "component" }, 175 { DRM_MODE_CONNECTOR_9PinDIN, "9-pin DIN" }, 176 { DRM_MODE_CONNECTOR_DisplayPort, "DP" }, 177 { DRM_MODE_CONNECTOR_HDMIA, "HDMI-A" }, 178 { DRM_MODE_CONNECTOR_HDMIB, "HDMI-B" }, 179 { DRM_MODE_CONNECTOR_TV, "TV" }, 180 { DRM_MODE_CONNECTOR_eDP, "eDP" }, 181 { DRM_MODE_CONNECTOR_VIRTUAL, "Virtual" }, 182 { DRM_MODE_CONNECTOR_DSI, "DSI" }, 183}; 184 185static type_name_fn(connector_type) 186 187#define bit_name_fn(res) \ 188const char * res##_str(int type) { \ 189 unsigned int i; \ 190 const char *sep = ""; \ 191 for (i = 0; i < ARRAY_SIZE(res##_names); i++) { \ 192 if (type & (1 << i)) { \ 193 printf("%s%s", sep, res##_names[i]); \ 194 sep = ", "; \ 195 } \ 196 } \ 197 return NULL; \ 198} 199 200static const char *mode_type_names[] = { 201 "builtin", 202 "clock_c", 203 "crtc_c", 204 "preferred", 205 "default", 206 "userdef", 207 "driver", 208}; 209 210static bit_name_fn(mode_type) 211 212static const char *mode_flag_names[] = { 213 "phsync", 214 "nhsync", 215 "pvsync", 216 "nvsync", 217 "interlace", 218 "dblscan", 219 "csync", 220 "pcsync", 221 "ncsync", 222 "hskew", 223 "bcast", 224 "pixmux", 225 "dblclk", 226 "clkdiv2" 227}; 228 229static bit_name_fn(mode_flag) 230 231static void dump_encoders(struct device *dev) 232{ 233 drmModeEncoder *encoder; 234 int i; 235 236 printf("Encoders:\n"); 237 printf("id\tcrtc\ttype\tpossible crtcs\tpossible clones\t\n"); 238 for (i = 0; i < dev->resources->res->count_encoders; i++) { 239 encoder = dev->resources->encoders[i].encoder; 240 if (!encoder) 241 continue; 242 243 printf("%d\t%d\t%s\t0x%08x\t0x%08x\n", 244 encoder->encoder_id, 245 encoder->crtc_id, 246 encoder_type_str(encoder->encoder_type), 247 encoder->possible_crtcs, 248 encoder->possible_clones); 249 } 250 printf("\n"); 251} 252 253static void dump_mode(drmModeModeInfo *mode) 254{ 255 printf(" %s %d %d %d %d %d %d %d %d %d", 256 mode->name, 257 mode->vrefresh, 258 mode->hdisplay, 259 mode->hsync_start, 260 mode->hsync_end, 261 mode->htotal, 262 mode->vdisplay, 263 mode->vsync_start, 264 mode->vsync_end, 265 mode->vtotal); 266 267 printf(" flags: "); 268 mode_flag_str(mode->flags); 269 printf("; type: "); 270 mode_type_str(mode->type); 271 printf("\n"); 272} 273 274static void dump_blob(struct device *dev, uint32_t blob_id) 275{ 276 uint32_t i; 277 unsigned char *blob_data; 278 drmModePropertyBlobPtr blob; 279 280 blob = drmModeGetPropertyBlob(dev->fd, blob_id); 281 if (!blob) { 282 printf("\n"); 283 return; 284 } 285 286 blob_data = blob->data; 287 288 for (i = 0; i < blob->length; i++) { 289 if (i % 16 == 0) 290 printf("\n\t\t\t"); 291 printf("%.2hhx", blob_data[i]); 292 } 293 printf("\n"); 294 295 drmModeFreePropertyBlob(blob); 296} 297 298static void dump_prop(struct device *dev, drmModePropertyPtr prop, 299 uint32_t prop_id, uint64_t value) 300{ 301 int i; 302 printf("\t%d", prop_id); 303 if (!prop) { 304 printf("\n"); 305 return; 306 } 307 308 printf(" %s:\n", prop->name); 309 310 printf("\t\tflags:"); 311 if (prop->flags & DRM_MODE_PROP_PENDING) 312 printf(" pending"); 313 if (prop->flags & DRM_MODE_PROP_IMMUTABLE) 314 printf(" immutable"); 315 if (drm_property_type_is(prop, DRM_MODE_PROP_SIGNED_RANGE)) 316 printf(" signed range"); 317 if (drm_property_type_is(prop, DRM_MODE_PROP_RANGE)) 318 printf(" range"); 319 if (drm_property_type_is(prop, DRM_MODE_PROP_ENUM)) 320 printf(" enum"); 321 if (drm_property_type_is(prop, DRM_MODE_PROP_BITMASK)) 322 printf(" bitmask"); 323 if (drm_property_type_is(prop, DRM_MODE_PROP_BLOB)) 324 printf(" blob"); 325 if (drm_property_type_is(prop, DRM_MODE_PROP_OBJECT)) 326 printf(" object"); 327 printf("\n"); 328 329 if (drm_property_type_is(prop, DRM_MODE_PROP_SIGNED_RANGE)) { 330 printf("\t\tvalues:"); 331 for (i = 0; i < prop->count_values; i++) 332 printf(" %"PRId64, U642I64(prop->values[i])); 333 printf("\n"); 334 } 335 336 if (drm_property_type_is(prop, DRM_MODE_PROP_RANGE)) { 337 printf("\t\tvalues:"); 338 for (i = 0; i < prop->count_values; i++) 339 printf(" %"PRIu64, prop->values[i]); 340 printf("\n"); 341 } 342 343 if (drm_property_type_is(prop, DRM_MODE_PROP_ENUM)) { 344 printf("\t\tenums:"); 345 for (i = 0; i < prop->count_enums; i++) 346 printf(" %s=%llu", prop->enums[i].name, 347 prop->enums[i].value); 348 printf("\n"); 349 } else if (drm_property_type_is(prop, DRM_MODE_PROP_BITMASK)) { 350 printf("\t\tvalues:"); 351 for (i = 0; i < prop->count_enums; i++) 352 printf(" %s=0x%llx", prop->enums[i].name, 353 (1LL << prop->enums[i].value)); 354 printf("\n"); 355 } else { 356 assert(prop->count_enums == 0); 357 } 358 359 if (drm_property_type_is(prop, DRM_MODE_PROP_BLOB)) { 360 printf("\t\tblobs:\n"); 361 for (i = 0; i < prop->count_blobs; i++) 362 dump_blob(dev, prop->blob_ids[i]); 363 printf("\n"); 364 } else { 365 assert(prop->count_blobs == 0); 366 } 367 368 printf("\t\tvalue:"); 369 if (drm_property_type_is(prop, DRM_MODE_PROP_BLOB)) 370 dump_blob(dev, value); 371 else 372 printf(" %"PRIu64"\n", value); 373} 374 375static void dump_connectors(struct device *dev) 376{ 377 int i, j; 378 379 printf("Connectors:\n"); 380 printf("id\tencoder\tstatus\t\tname\t\tsize (mm)\tmodes\tencoders\n"); 381 for (i = 0; i < dev->resources->res->count_connectors; i++) { 382 struct connector *_connector = &dev->resources->connectors[i]; 383 drmModeConnector *connector = _connector->connector; 384 if (!connector) 385 continue; 386 387 printf("%d\t%d\t%s\t%-15s\t%dx%d\t\t%d\t", 388 connector->connector_id, 389 connector->encoder_id, 390 connector_status_str(connector->connection), 391 _connector->name, 392 connector->mmWidth, connector->mmHeight, 393 connector->count_modes); 394 395 for (j = 0; j < connector->count_encoders; j++) 396 printf("%s%d", j > 0 ? ", " : "", connector->encoders[j]); 397 printf("\n"); 398 399 if (connector->count_modes) { 400 printf(" modes:\n"); 401 printf("\tname refresh (Hz) hdisp hss hse htot vdisp " 402 "vss vse vtot)\n"); 403 for (j = 0; j < connector->count_modes; j++) 404 dump_mode(&connector->modes[j]); 405 } 406 407 if (_connector->props) { 408 printf(" props:\n"); 409 for (j = 0; j < (int)_connector->props->count_props; j++) 410 dump_prop(dev, _connector->props_info[j], 411 _connector->props->props[j], 412 _connector->props->prop_values[j]); 413 } 414 } 415 printf("\n"); 416} 417 418static void dump_crtcs(struct device *dev) 419{ 420 int i; 421 uint32_t j; 422 423 printf("CRTCs:\n"); 424 printf("id\tfb\tpos\tsize\n"); 425 for (i = 0; i < dev->resources->res->count_crtcs; i++) { 426 struct crtc *_crtc = &dev->resources->crtcs[i]; 427 drmModeCrtc *crtc = _crtc->crtc; 428 if (!crtc) 429 continue; 430 431 printf("%d\t%d\t(%d,%d)\t(%dx%d)\n", 432 crtc->crtc_id, 433 crtc->buffer_id, 434 crtc->x, crtc->y, 435 crtc->width, crtc->height); 436 dump_mode(&crtc->mode); 437 438 if (_crtc->props) { 439 printf(" props:\n"); 440 for (j = 0; j < _crtc->props->count_props; j++) 441 dump_prop(dev, _crtc->props_info[j], 442 _crtc->props->props[j], 443 _crtc->props->prop_values[j]); 444 } else { 445 printf(" no properties found\n"); 446 } 447 } 448 printf("\n"); 449} 450 451static void dump_framebuffers(struct device *dev) 452{ 453 drmModeFB *fb; 454 int i; 455 456 printf("Frame buffers:\n"); 457 printf("id\tsize\tpitch\n"); 458 for (i = 0; i < dev->resources->res->count_fbs; i++) { 459 fb = dev->resources->fbs[i].fb; 460 if (!fb) 461 continue; 462 463 printf("%u\t(%ux%u)\t%u\n", 464 fb->fb_id, 465 fb->width, fb->height, 466 fb->pitch); 467 } 468 printf("\n"); 469} 470 471static void dump_planes(struct device *dev) 472{ 473 unsigned int i, j; 474 475 printf("Planes:\n"); 476 printf("id\tcrtc\tfb\tCRTC x,y\tx,y\tgamma size\tpossible crtcs\n"); 477 478 if (!dev->resources->plane_res) 479 return; 480 481 for (i = 0; i < dev->resources->plane_res->count_planes; i++) { 482 struct plane *plane = &dev->resources->planes[i]; 483 drmModePlane *ovr = plane->plane; 484 if (!ovr) 485 continue; 486 487 printf("%d\t%d\t%d\t%d,%d\t\t%d,%d\t%-8d\t0x%08x\n", 488 ovr->plane_id, ovr->crtc_id, ovr->fb_id, 489 ovr->crtc_x, ovr->crtc_y, ovr->x, ovr->y, 490 ovr->gamma_size, ovr->possible_crtcs); 491 492 if (!ovr->count_formats) 493 continue; 494 495 printf(" formats:"); 496 for (j = 0; j < ovr->count_formats; j++) 497 printf(" %4.4s", (char *)&ovr->formats[j]); 498 printf("\n"); 499 500 if (plane->props) { 501 printf(" props:\n"); 502 for (j = 0; j < plane->props->count_props; j++) 503 dump_prop(dev, plane->props_info[j], 504 plane->props->props[j], 505 plane->props->prop_values[j]); 506 } else { 507 printf(" no properties found\n"); 508 } 509 } 510 printf("\n"); 511 512 return; 513} 514 515static void free_resources(struct resources *res) 516{ 517 int i; 518 519 if (!res) 520 return; 521 522#define free_resource(_res, __res, type, Type) \ 523 do { \ 524 if (!(_res)->type##s) \ 525 break; \ 526 for (i = 0; i < (int)(_res)->__res->count_##type##s; ++i) { \ 527 if (!(_res)->type##s[i].type) \ 528 break; \ 529 drmModeFree##Type((_res)->type##s[i].type); \ 530 } \ 531 free((_res)->type##s); \ 532 } while (0) 533 534#define free_properties(_res, __res, type) \ 535 do { \ 536 for (i = 0; i < (int)(_res)->__res->count_##type##s; ++i) { \ 537 drmModeFreeObjectProperties(res->type##s[i].props); \ 538 free(res->type##s[i].props_info); \ 539 } \ 540 } while (0) 541 542 if (res->res) { 543 free_properties(res, res, crtc); 544 545 free_resource(res, res, crtc, Crtc); 546 free_resource(res, res, encoder, Encoder); 547 548 for (i = 0; i < res->res->count_connectors; i++) 549 free(res->connectors[i].name); 550 551 free_resource(res, res, connector, Connector); 552 free_resource(res, res, fb, FB); 553 554 drmModeFreeResources(res->res); 555 } 556 557 if (res->plane_res) { 558 free_properties(res, plane_res, plane); 559 560 free_resource(res, plane_res, plane, Plane); 561 562 drmModeFreePlaneResources(res->plane_res); 563 } 564 565 free(res); 566} 567 568static struct resources *get_resources(struct device *dev) 569{ 570 struct resources *res; 571 int i; 572 573 res = calloc(1, sizeof(*res)); 574 if (res == 0) 575 return NULL; 576 577 drmSetClientCap(dev->fd, DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1); 578 579 res->res = drmModeGetResources(dev->fd); 580 if (!res->res) { 581 fprintf(stderr, "drmModeGetResources failed: %s\n", 582 strerror(errno)); 583 goto error; 584 } 585 586 res->crtcs = calloc(res->res->count_crtcs, sizeof(*res->crtcs)); 587 res->encoders = calloc(res->res->count_encoders, sizeof(*res->encoders)); 588 res->connectors = calloc(res->res->count_connectors, sizeof(*res->connectors)); 589 res->fbs = calloc(res->res->count_fbs, sizeof(*res->fbs)); 590 591 if (!res->crtcs || !res->encoders || !res->connectors || !res->fbs) 592 goto error; 593 594#define get_resource(_res, __res, type, Type) \ 595 do { \ 596 for (i = 0; i < (int)(_res)->__res->count_##type##s; ++i) { \ 597 (_res)->type##s[i].type = \ 598 drmModeGet##Type(dev->fd, (_res)->__res->type##s[i]); \ 599 if (!(_res)->type##s[i].type) \ 600 fprintf(stderr, "could not get %s %i: %s\n", \ 601 #type, (_res)->__res->type##s[i], \ 602 strerror(errno)); \ 603 } \ 604 } while (0) 605 606 get_resource(res, res, crtc, Crtc); 607 get_resource(res, res, encoder, Encoder); 608 get_resource(res, res, connector, Connector); 609 get_resource(res, res, fb, FB); 610 611 /* Set the name of all connectors based on the type name and the per-type ID. */ 612 for (i = 0; i < res->res->count_connectors; i++) { 613 struct connector *connector = &res->connectors[i]; 614 615 asprintf(&connector->name, "%s-%u", 616 connector_type_str(connector->connector->connector_type), 617 connector->connector->connector_type_id); 618 } 619 620#define get_properties(_res, __res, type, Type) \ 621 do { \ 622 for (i = 0; i < (int)(_res)->__res->count_##type##s; ++i) { \ 623 struct type *obj = &res->type##s[i]; \ 624 unsigned int j; \ 625 obj->props = \ 626 drmModeObjectGetProperties(dev->fd, obj->type->type##_id, \ 627 DRM_MODE_OBJECT_##Type); \ 628 if (!obj->props) { \ 629 fprintf(stderr, \ 630 "could not get %s %i properties: %s\n", \ 631 #type, obj->type->type##_id, \ 632 strerror(errno)); \ 633 continue; \ 634 } \ 635 obj->props_info = calloc(obj->props->count_props, \ 636 sizeof(*obj->props_info)); \ 637 if (!obj->props_info) \ 638 continue; \ 639 for (j = 0; j < obj->props->count_props; ++j) \ 640 obj->props_info[j] = \ 641 drmModeGetProperty(dev->fd, obj->props->props[j]); \ 642 } \ 643 } while (0) 644 645 get_properties(res, res, crtc, CRTC); 646 get_properties(res, res, connector, CONNECTOR); 647 648 for (i = 0; i < res->res->count_crtcs; ++i) 649 res->crtcs[i].mode = &res->crtcs[i].crtc->mode; 650 651 res->plane_res = drmModeGetPlaneResources(dev->fd); 652 if (!res->plane_res) { 653 fprintf(stderr, "drmModeGetPlaneResources failed: %s\n", 654 strerror(errno)); 655 return res; 656 } 657 658 res->planes = calloc(res->plane_res->count_planes, sizeof(*res->planes)); 659 if (!res->planes) 660 goto error; 661 662 get_resource(res, plane_res, plane, Plane); 663 get_properties(res, plane_res, plane, PLANE); 664 665 return res; 666 667error: 668 free_resources(res); 669 return NULL; 670} 671 672static int get_crtc_index(struct device *dev, uint32_t id) 673{ 674 int i; 675 676 for (i = 0; i < dev->resources->res->count_crtcs; ++i) { 677 drmModeCrtc *crtc = dev->resources->crtcs[i].crtc; 678 if (crtc && crtc->crtc_id == id) 679 return i; 680 } 681 682 return -1; 683} 684 685static drmModeConnector *get_connector_by_name(struct device *dev, const char *name) 686{ 687 struct connector *connector; 688 int i; 689 690 for (i = 0; i < dev->resources->res->count_connectors; i++) { 691 connector = &dev->resources->connectors[i]; 692 693 if (strcmp(connector->name, name) == 0) 694 return connector->connector; 695 } 696 697 return NULL; 698} 699 700static drmModeConnector *get_connector_by_id(struct device *dev, uint32_t id) 701{ 702 drmModeConnector *connector; 703 int i; 704 705 for (i = 0; i < dev->resources->res->count_connectors; i++) { 706 connector = dev->resources->connectors[i].connector; 707 if (connector && connector->connector_id == id) 708 return connector; 709 } 710 711 return NULL; 712} 713 714static drmModeEncoder *get_encoder_by_id(struct device *dev, uint32_t id) 715{ 716 drmModeEncoder *encoder; 717 int i; 718 719 for (i = 0; i < dev->resources->res->count_encoders; i++) { 720 encoder = dev->resources->encoders[i].encoder; 721 if (encoder && encoder->encoder_id == id) 722 return encoder; 723 } 724 725 return NULL; 726} 727 728/* ----------------------------------------------------------------------------- 729 * Pipes and planes 730 */ 731 732/* 733 * Mode setting with the kernel interfaces is a bit of a chore. 734 * First you have to find the connector in question and make sure the 735 * requested mode is available. 736 * Then you need to find the encoder attached to that connector so you 737 * can bind it with a free crtc. 738 */ 739struct pipe_arg { 740 const char **cons; 741 uint32_t *con_ids; 742 unsigned int num_cons; 743 uint32_t crtc_id; 744 char mode_str[64]; 745 char format_str[5]; 746 unsigned int vrefresh; 747 unsigned int fourcc; 748 drmModeModeInfo *mode; 749 struct crtc *crtc; 750 unsigned int fb_id[2], current_fb_id; 751 struct timeval start; 752 753 int swap_count; 754}; 755 756struct plane_arg { 757 uint32_t crtc_id; /* the id of CRTC to bind to */ 758 bool has_position; 759 int32_t x, y; 760 uint32_t w, h; 761 double scale; 762 unsigned int fb_id; 763 struct bo *bo; 764 char format_str[5]; /* need to leave room for terminating \0 */ 765 unsigned int fourcc; 766}; 767 768static drmModeModeInfo * 769connector_find_mode(struct device *dev, uint32_t con_id, const char *mode_str, 770 const unsigned int vrefresh) 771{ 772 drmModeConnector *connector; 773 drmModeModeInfo *mode; 774 int i; 775 776 connector = get_connector_by_id(dev, con_id); 777 if (!connector || !connector->count_modes) 778 return NULL; 779 780 for (i = 0; i < connector->count_modes; i++) { 781 mode = &connector->modes[i]; 782 if (!strcmp(mode->name, mode_str)) { 783 /* If the vertical refresh frequency is not specified then return the 784 * first mode that match with the name. Else, return the mode that match 785 * the name and the specified vertical refresh frequency. 786 */ 787 if (vrefresh == 0) 788 return mode; 789 else if (mode->vrefresh == vrefresh) 790 return mode; 791 } 792 } 793 794 return NULL; 795} 796 797static struct crtc *pipe_find_crtc(struct device *dev, struct pipe_arg *pipe) 798{ 799 uint32_t possible_crtcs = ~0; 800 uint32_t active_crtcs = 0; 801 unsigned int crtc_idx; 802 unsigned int i; 803 int j; 804 805 for (i = 0; i < pipe->num_cons; ++i) { 806 uint32_t crtcs_for_connector = 0; 807 drmModeConnector *connector; 808 drmModeEncoder *encoder; 809 int idx; 810 811 connector = get_connector_by_id(dev, pipe->con_ids[i]); 812 if (!connector) 813 return NULL; 814 815 for (j = 0; j < connector->count_encoders; ++j) { 816 encoder = get_encoder_by_id(dev, connector->encoders[j]); 817 if (!encoder) 818 continue; 819 820 crtcs_for_connector |= encoder->possible_crtcs; 821 822 idx = get_crtc_index(dev, encoder->crtc_id); 823 if (idx >= 0) 824 active_crtcs |= 1 << idx; 825 } 826 827 possible_crtcs &= crtcs_for_connector; 828 } 829 830 if (!possible_crtcs) 831 return NULL; 832 833 /* Return the first possible and active CRTC if one exists, or the first 834 * possible CRTC otherwise. 835 */ 836 if (possible_crtcs & active_crtcs) 837 crtc_idx = ffs(possible_crtcs & active_crtcs); 838 else 839 crtc_idx = ffs(possible_crtcs); 840 841 return &dev->resources->crtcs[crtc_idx - 1]; 842} 843 844static int pipe_find_crtc_and_mode(struct device *dev, struct pipe_arg *pipe) 845{ 846 drmModeModeInfo *mode = NULL; 847 int i; 848 849 pipe->mode = NULL; 850 851 for (i = 0; i < (int)pipe->num_cons; i++) { 852 mode = connector_find_mode(dev, pipe->con_ids[i], 853 pipe->mode_str, pipe->vrefresh); 854 if (mode == NULL) { 855 fprintf(stderr, 856 "failed to find mode \"%s\" for connector %s\n", 857 pipe->mode_str, pipe->cons[i]); 858 return -EINVAL; 859 } 860 } 861 862 /* If the CRTC ID was specified, get the corresponding CRTC. Otherwise 863 * locate a CRTC that can be attached to all the connectors. 864 */ 865 if (pipe->crtc_id != (uint32_t)-1) { 866 for (i = 0; i < dev->resources->res->count_crtcs; i++) { 867 struct crtc *crtc = &dev->resources->crtcs[i]; 868 869 if (pipe->crtc_id == crtc->crtc->crtc_id) { 870 pipe->crtc = crtc; 871 break; 872 } 873 } 874 } else { 875 pipe->crtc = pipe_find_crtc(dev, pipe); 876 } 877 878 if (!pipe->crtc) { 879 fprintf(stderr, "failed to find CRTC for pipe\n"); 880 return -EINVAL; 881 } 882 883 pipe->mode = mode; 884 pipe->crtc->mode = mode; 885 886 return 0; 887} 888 889/* ----------------------------------------------------------------------------- 890 * Properties 891 */ 892 893struct property_arg { 894 uint32_t obj_id; 895 uint32_t obj_type; 896 char name[DRM_PROP_NAME_LEN+1]; 897 uint32_t prop_id; 898 uint64_t value; 899}; 900 901static void set_property(struct device *dev, struct property_arg *p) 902{ 903 drmModeObjectProperties *props = NULL; 904 drmModePropertyRes **props_info = NULL; 905 const char *obj_type; 906 int ret; 907 int i; 908 909 p->obj_type = 0; 910 p->prop_id = 0; 911 912#define find_object(_res, __res, type, Type) \ 913 do { \ 914 for (i = 0; i < (int)(_res)->__res->count_##type##s; ++i) { \ 915 struct type *obj = &(_res)->type##s[i]; \ 916 if (obj->type->type##_id != p->obj_id) \ 917 continue; \ 918 p->obj_type = DRM_MODE_OBJECT_##Type; \ 919 obj_type = #Type; \ 920 props = obj->props; \ 921 props_info = obj->props_info; \ 922 } \ 923 } while(0) \ 924 925 find_object(dev->resources, res, crtc, CRTC); 926 if (p->obj_type == 0) 927 find_object(dev->resources, res, connector, CONNECTOR); 928 if (p->obj_type == 0) 929 find_object(dev->resources, plane_res, plane, PLANE); 930 if (p->obj_type == 0) { 931 fprintf(stderr, "Object %i not found, can't set property\n", 932 p->obj_id); 933 return; 934 } 935 936 if (!props) { 937 fprintf(stderr, "%s %i has no properties\n", 938 obj_type, p->obj_id); 939 return; 940 } 941 942 for (i = 0; i < (int)props->count_props; ++i) { 943 if (!props_info[i]) 944 continue; 945 if (strcmp(props_info[i]->name, p->name) == 0) 946 break; 947 } 948 949 if (i == (int)props->count_props) { 950 fprintf(stderr, "%s %i has no %s property\n", 951 obj_type, p->obj_id, p->name); 952 return; 953 } 954 955 p->prop_id = props->props[i]; 956 957 ret = drmModeObjectSetProperty(dev->fd, p->obj_id, p->obj_type, 958 p->prop_id, p->value); 959 if (ret < 0) 960 fprintf(stderr, "failed to set %s %i property %s to %" PRIu64 ": %s\n", 961 obj_type, p->obj_id, p->name, p->value, strerror(errno)); 962} 963 964/* -------------------------------------------------------------------------- */ 965 966static void 967page_flip_handler(int fd, unsigned int frame, 968 unsigned int sec, unsigned int usec, void *data) 969{ 970 struct pipe_arg *pipe; 971 unsigned int new_fb_id; 972 struct timeval end; 973 double t; 974 975 pipe = data; 976 if (pipe->current_fb_id == pipe->fb_id[0]) 977 new_fb_id = pipe->fb_id[1]; 978 else 979 new_fb_id = pipe->fb_id[0]; 980 981 drmModePageFlip(fd, pipe->crtc->crtc->crtc_id, new_fb_id, 982 DRM_MODE_PAGE_FLIP_EVENT, pipe); 983 pipe->current_fb_id = new_fb_id; 984 pipe->swap_count++; 985 if (pipe->swap_count == 60) { 986 gettimeofday(&end, NULL); 987 t = end.tv_sec + end.tv_usec * 1e-6 - 988 (pipe->start.tv_sec + pipe->start.tv_usec * 1e-6); 989 fprintf(stderr, "freq: %.02fHz\n", pipe->swap_count / t); 990 pipe->swap_count = 0; 991 pipe->start = end; 992 } 993} 994 995static bool format_support(const drmModePlanePtr ovr, uint32_t fmt) 996{ 997 unsigned int i; 998 999 for (i = 0; i < ovr->count_formats; ++i) { 1000 if (ovr->formats[i] == fmt) 1001 return true; 1002 } 1003 1004 return false; 1005} 1006 1007static int set_plane(struct device *dev, struct plane_arg *p) 1008{ 1009 drmModePlane *ovr; 1010 uint32_t handles[4] = {0}, pitches[4] = {0}, offsets[4] = {0}; 1011 uint32_t plane_id = 0; 1012 struct bo *plane_bo; 1013 uint32_t plane_flags = 0; 1014 int crtc_x, crtc_y, crtc_w, crtc_h; 1015 struct crtc *crtc = NULL; 1016 unsigned int pipe; 1017 unsigned int i; 1018 1019 /* Find an unused plane which can be connected to our CRTC. Find the 1020 * CRTC index first, then iterate over available planes. 1021 */ 1022 for (i = 0; i < (unsigned int)dev->resources->res->count_crtcs; i++) { 1023 if (p->crtc_id == dev->resources->res->crtcs[i]) { 1024 crtc = &dev->resources->crtcs[i]; 1025 pipe = i; 1026 break; 1027 } 1028 } 1029 1030 if (!crtc) { 1031 fprintf(stderr, "CRTC %u not found\n", p->crtc_id); 1032 return -1; 1033 } 1034 1035 for (i = 0; i < dev->resources->plane_res->count_planes && !plane_id; i++) { 1036 ovr = dev->resources->planes[i].plane; 1037 if (!ovr || !format_support(ovr, p->fourcc)) 1038 continue; 1039 1040 if ((ovr->possible_crtcs & (1 << pipe)) && !ovr->crtc_id) 1041 plane_id = ovr->plane_id; 1042 } 1043 1044 if (!plane_id) { 1045 fprintf(stderr, "no unused plane available for CRTC %u\n", 1046 crtc->crtc->crtc_id); 1047 return -1; 1048 } 1049 1050 fprintf(stderr, "testing %dx%d@%s overlay plane %u\n", 1051 p->w, p->h, p->format_str, plane_id); 1052 1053 plane_bo = bo_create(dev->fd, p->fourcc, p->w, p->h, handles, 1054 pitches, offsets, UTIL_PATTERN_TILES); 1055 if (plane_bo == NULL) 1056 return -1; 1057 1058 p->bo = plane_bo; 1059 1060 /* just use single plane format for now.. */ 1061 if (drmModeAddFB2(dev->fd, p->w, p->h, p->fourcc, 1062 handles, pitches, offsets, &p->fb_id, plane_flags)) { 1063 fprintf(stderr, "failed to add fb: %s\n", strerror(errno)); 1064 return -1; 1065 } 1066 1067 crtc_w = p->w * p->scale; 1068 crtc_h = p->h * p->scale; 1069 if (!p->has_position) { 1070 /* Default to the middle of the screen */ 1071 crtc_x = (crtc->mode->hdisplay - crtc_w) / 2; 1072 crtc_y = (crtc->mode->vdisplay - crtc_h) / 2; 1073 } else { 1074 crtc_x = p->x; 1075 crtc_y = p->y; 1076 } 1077 1078 /* note src coords (last 4 args) are in Q16 format */ 1079 if (drmModeSetPlane(dev->fd, plane_id, crtc->crtc->crtc_id, p->fb_id, 1080 plane_flags, crtc_x, crtc_y, crtc_w, crtc_h, 1081 0, 0, p->w << 16, p->h << 16)) { 1082 fprintf(stderr, "failed to enable plane: %s\n", 1083 strerror(errno)); 1084 return -1; 1085 } 1086 1087 ovr->crtc_id = crtc->crtc->crtc_id; 1088 1089 return 0; 1090} 1091 1092static void clear_planes(struct device *dev, struct plane_arg *p, unsigned int count) 1093{ 1094 unsigned int i; 1095 1096 for (i = 0; i < count; i++) { 1097 if (p[i].fb_id) 1098 drmModeRmFB(dev->fd, p[i].fb_id); 1099 if (p[i].bo) 1100 bo_destroy(p[i].bo); 1101 } 1102} 1103 1104 1105static void set_mode(struct device *dev, struct pipe_arg *pipes, unsigned int count) 1106{ 1107 uint32_t handles[4] = {0}, pitches[4] = {0}, offsets[4] = {0}; 1108 unsigned int fb_id; 1109 struct bo *bo; 1110 unsigned int i; 1111 unsigned int j; 1112 int ret, x; 1113 1114 dev->mode.width = 0; 1115 dev->mode.height = 0; 1116 dev->mode.fb_id = 0; 1117 1118 for (i = 0; i < count; i++) { 1119 struct pipe_arg *pipe = &pipes[i]; 1120 1121 ret = pipe_find_crtc_and_mode(dev, pipe); 1122 if (ret < 0) 1123 continue; 1124 1125 dev->mode.width += pipe->mode->hdisplay; 1126 if (dev->mode.height < pipe->mode->vdisplay) 1127 dev->mode.height = pipe->mode->vdisplay; 1128 } 1129 1130 bo = bo_create(dev->fd, pipes[0].fourcc, dev->mode.width, 1131 dev->mode.height, handles, pitches, offsets, 1132 UTIL_PATTERN_SMPTE); 1133 if (bo == NULL) 1134 return; 1135 1136 dev->mode.bo = bo; 1137 1138 ret = drmModeAddFB2(dev->fd, dev->mode.width, dev->mode.height, 1139 pipes[0].fourcc, handles, pitches, offsets, &fb_id, 0); 1140 if (ret) { 1141 fprintf(stderr, "failed to add fb (%ux%u): %s\n", 1142 dev->mode.width, dev->mode.height, strerror(errno)); 1143 return; 1144 } 1145 1146 dev->mode.fb_id = fb_id; 1147 1148 x = 0; 1149 for (i = 0; i < count; i++) { 1150 struct pipe_arg *pipe = &pipes[i]; 1151 1152 if (pipe->mode == NULL) 1153 continue; 1154 1155 printf("setting mode %s-%dHz@%s on connectors ", 1156 pipe->mode_str, pipe->mode->vrefresh, pipe->format_str); 1157 for (j = 0; j < pipe->num_cons; ++j) 1158 printf("%s, ", pipe->cons[j]); 1159 printf("crtc %d\n", pipe->crtc->crtc->crtc_id); 1160 1161 ret = drmModeSetCrtc(dev->fd, pipe->crtc->crtc->crtc_id, fb_id, 1162 x, 0, pipe->con_ids, pipe->num_cons, 1163 pipe->mode); 1164 1165 /* XXX: Actually check if this is needed */ 1166 drmModeDirtyFB(dev->fd, fb_id, NULL, 0); 1167 1168 x += pipe->mode->hdisplay; 1169 1170 if (ret) { 1171 fprintf(stderr, "failed to set mode: %s\n", strerror(errno)); 1172 return; 1173 } 1174 } 1175} 1176 1177static void clear_mode(struct device *dev) 1178{ 1179 if (dev->mode.fb_id) 1180 drmModeRmFB(dev->fd, dev->mode.fb_id); 1181 if (dev->mode.bo) 1182 bo_destroy(dev->mode.bo); 1183} 1184 1185static void set_planes(struct device *dev, struct plane_arg *p, unsigned int count) 1186{ 1187 unsigned int i; 1188 1189 /* set up planes/overlays */ 1190 for (i = 0; i < count; i++) 1191 if (set_plane(dev, &p[i])) 1192 return; 1193} 1194 1195static void set_cursors(struct device *dev, struct pipe_arg *pipes, unsigned int count) 1196{ 1197 uint32_t handles[4] = {0}, pitches[4] = {0}, offsets[4] = {0}; 1198 struct bo *bo; 1199 unsigned int i; 1200 int ret; 1201 1202 /* maybe make cursor width/height configurable some day */ 1203 uint32_t cw = 64; 1204 uint32_t ch = 64; 1205 1206 /* create cursor bo.. just using PATTERN_PLAIN as it has 1207 * translucent alpha 1208 */ 1209 bo = bo_create(dev->fd, DRM_FORMAT_ARGB8888, cw, ch, handles, pitches, 1210 offsets, UTIL_PATTERN_PLAIN); 1211 if (bo == NULL) 1212 return; 1213 1214 dev->mode.cursor_bo = bo; 1215 1216 for (i = 0; i < count; i++) { 1217 struct pipe_arg *pipe = &pipes[i]; 1218 ret = cursor_init(dev->fd, handles[0], 1219 pipe->crtc->crtc->crtc_id, 1220 pipe->mode->hdisplay, pipe->mode->vdisplay, 1221 cw, ch); 1222 if (ret) { 1223 fprintf(stderr, "failed to init cursor for CRTC[%u]\n", 1224 pipe->crtc_id); 1225 return; 1226 } 1227 } 1228 1229 cursor_start(); 1230} 1231 1232static void clear_cursors(struct device *dev) 1233{ 1234 cursor_stop(); 1235 1236 if (dev->mode.cursor_bo) 1237 bo_destroy(dev->mode.cursor_bo); 1238} 1239 1240static void test_page_flip(struct device *dev, struct pipe_arg *pipes, unsigned int count) 1241{ 1242 uint32_t handles[4] = {0}, pitches[4] = {0}, offsets[4] = {0}; 1243 unsigned int other_fb_id; 1244 struct bo *other_bo; 1245 drmEventContext evctx; 1246 unsigned int i; 1247 int ret; 1248 1249 other_bo = bo_create(dev->fd, pipes[0].fourcc, dev->mode.width, 1250 dev->mode.height, handles, pitches, offsets, 1251 UTIL_PATTERN_PLAIN); 1252 if (other_bo == NULL) 1253 return; 1254 1255 ret = drmModeAddFB2(dev->fd, dev->mode.width, dev->mode.height, 1256 pipes[0].fourcc, handles, pitches, offsets, 1257 &other_fb_id, 0); 1258 if (ret) { 1259 fprintf(stderr, "failed to add fb: %s\n", strerror(errno)); 1260 goto err; 1261 } 1262 1263 for (i = 0; i < count; i++) { 1264 struct pipe_arg *pipe = &pipes[i]; 1265 1266 if (pipe->mode == NULL) 1267 continue; 1268 1269 ret = drmModePageFlip(dev->fd, pipe->crtc->crtc->crtc_id, 1270 other_fb_id, DRM_MODE_PAGE_FLIP_EVENT, 1271 pipe); 1272 if (ret) { 1273 fprintf(stderr, "failed to page flip: %s\n", strerror(errno)); 1274 goto err_rmfb; 1275 } 1276 gettimeofday(&pipe->start, NULL); 1277 pipe->swap_count = 0; 1278 pipe->fb_id[0] = dev->mode.fb_id; 1279 pipe->fb_id[1] = other_fb_id; 1280 pipe->current_fb_id = other_fb_id; 1281 } 1282 1283 memset(&evctx, 0, sizeof evctx); 1284 evctx.version = DRM_EVENT_CONTEXT_VERSION; 1285 evctx.vblank_handler = NULL; 1286 evctx.page_flip_handler = page_flip_handler; 1287 1288 while (1) { 1289#if 0 1290 struct pollfd pfd[2]; 1291 1292 pfd[0].fd = 0; 1293 pfd[0].events = POLLIN; 1294 pfd[1].fd = fd; 1295 pfd[1].events = POLLIN; 1296 1297 if (poll(pfd, 2, -1) < 0) { 1298 fprintf(stderr, "poll error\n"); 1299 break; 1300 } 1301 1302 if (pfd[0].revents) 1303 break; 1304#else 1305 struct timeval timeout = { .tv_sec = 3, .tv_usec = 0 }; 1306 fd_set fds; 1307 1308 FD_ZERO(&fds); 1309 FD_SET(0, &fds); 1310 FD_SET(dev->fd, &fds); 1311 ret = select(dev->fd + 1, &fds, NULL, NULL, &timeout); 1312 1313 if (ret <= 0) { 1314 fprintf(stderr, "select timed out or error (ret %d)\n", 1315 ret); 1316 continue; 1317 } else if (FD_ISSET(0, &fds)) { 1318 break; 1319 } 1320#endif 1321 1322 drmHandleEvent(dev->fd, &evctx); 1323 } 1324 1325err_rmfb: 1326 drmModeRmFB(dev->fd, other_fb_id); 1327err: 1328 bo_destroy(other_bo); 1329} 1330 1331#define min(a, b) ((a) < (b) ? (a) : (b)) 1332 1333static int parse_connector(struct pipe_arg *pipe, const char *arg) 1334{ 1335 unsigned int len; 1336 unsigned int i; 1337 const char *p; 1338 char *endp; 1339 1340 pipe->vrefresh = 0; 1341 pipe->crtc_id = (uint32_t)-1; 1342 strcpy(pipe->format_str, "XR24"); 1343 1344 /* Count the number of connectors and allocate them. */ 1345 pipe->num_cons = 1; 1346 for (p = arg; *p && *p != ':' && *p != '@'; ++p) { 1347 if (*p == ',') 1348 pipe->num_cons++; 1349 } 1350 1351 pipe->con_ids = calloc(pipe->num_cons, sizeof(*pipe->con_ids)); 1352 pipe->cons = calloc(pipe->num_cons, sizeof(*pipe->cons)); 1353 if (pipe->con_ids == NULL || pipe->cons == NULL) 1354 return -1; 1355 1356 /* Parse the connectors. */ 1357 for (i = 0, p = arg; i < pipe->num_cons; ++i, p = endp + 1) { 1358 endp = strpbrk(p, ",@:"); 1359 if (!endp) 1360 break; 1361 1362 pipe->cons[i] = strndup(p, endp - p); 1363 1364 if (*endp != ',') 1365 break; 1366 } 1367 1368 if (i != pipe->num_cons - 1) 1369 return -1; 1370 1371 /* Parse the remaining parameters. */ 1372 if (*endp == '@') { 1373 arg = endp + 1; 1374 pipe->crtc_id = strtoul(arg, &endp, 10); 1375 } 1376 if (*endp != ':') 1377 return -1; 1378 1379 arg = endp + 1; 1380 1381 /* Search for the vertical refresh or the format. */ 1382 p = strpbrk(arg, "-@"); 1383 if (p == NULL) 1384 p = arg + strlen(arg); 1385 len = min(sizeof pipe->mode_str - 1, (unsigned int)(p - arg)); 1386 strncpy(pipe->mode_str, arg, len); 1387 pipe->mode_str[len] = '\0'; 1388 1389 if (*p == '-') { 1390 pipe->vrefresh = strtoul(p + 1, &endp, 10); 1391 p = endp; 1392 } 1393 1394 if (*p == '@') { 1395 strncpy(pipe->format_str, p + 1, 4); 1396 pipe->format_str[4] = '\0'; 1397 } 1398 1399 pipe->fourcc = util_format_fourcc(pipe->format_str); 1400 if (pipe->fourcc == 0) { 1401 fprintf(stderr, "unknown format %s\n", pipe->format_str); 1402 return -1; 1403 } 1404 1405 return 0; 1406} 1407 1408static int parse_plane(struct plane_arg *plane, const char *p) 1409{ 1410 char *end; 1411 1412 plane->crtc_id = strtoul(p, &end, 10); 1413 if (*end != ':') 1414 return -EINVAL; 1415 1416 p = end + 1; 1417 plane->w = strtoul(p, &end, 10); 1418 if (*end != 'x') 1419 return -EINVAL; 1420 1421 p = end + 1; 1422 plane->h = strtoul(p, &end, 10); 1423 1424 if (*end == '+' || *end == '-') { 1425 plane->x = strtol(end, &end, 10); 1426 if (*end != '+' && *end != '-') 1427 return -EINVAL; 1428 plane->y = strtol(end, &end, 10); 1429 1430 plane->has_position = true; 1431 } 1432 1433 if (*end == '*') { 1434 p = end + 1; 1435 plane->scale = strtod(p, &end); 1436 if (plane->scale <= 0.0) 1437 return -EINVAL; 1438 } else { 1439 plane->scale = 1.0; 1440 } 1441 1442 if (*end == '@') { 1443 p = end + 1; 1444 if (strlen(p) != 4) 1445 return -EINVAL; 1446 1447 strcpy(plane->format_str, p); 1448 } else { 1449 strcpy(plane->format_str, "XR24"); 1450 } 1451 1452 plane->fourcc = util_format_fourcc(plane->format_str); 1453 if (plane->fourcc == 0) { 1454 fprintf(stderr, "unknown format %s\n", plane->format_str); 1455 return -EINVAL; 1456 } 1457 1458 return 0; 1459} 1460 1461static int parse_property(struct property_arg *p, const char *arg) 1462{ 1463 if (sscanf(arg, "%d:%32[^:]:%" SCNu64, &p->obj_id, p->name, &p->value) != 3) 1464 return -1; 1465 1466 p->obj_type = 0; 1467 p->name[DRM_PROP_NAME_LEN] = '\0'; 1468 1469 return 0; 1470} 1471 1472static void usage(char *name) 1473{ 1474 fprintf(stderr, "usage: %s [-cDdefMPpsCvw]\n", name); 1475 1476 fprintf(stderr, "\n Query options:\n\n"); 1477 fprintf(stderr, "\t-c\tlist connectors\n"); 1478 fprintf(stderr, "\t-e\tlist encoders\n"); 1479 fprintf(stderr, "\t-f\tlist framebuffers\n"); 1480 fprintf(stderr, "\t-p\tlist CRTCs and planes (pipes)\n"); 1481 1482 fprintf(stderr, "\n Test options:\n\n"); 1483 fprintf(stderr, "\t-P <crtc_id>:<w>x<h>[+<x>+<y>][*<scale>][@<format>]\tset a plane\n"); 1484 fprintf(stderr, "\t-s <connector_id>[,<connector_id>][@<crtc_id>]:<mode>[-<vrefresh>][@<format>]\tset a mode\n"); 1485 fprintf(stderr, "\t-C\ttest hw cursor\n"); 1486 fprintf(stderr, "\t-v\ttest vsynced page flipping\n"); 1487 fprintf(stderr, "\t-w <obj_id>:<prop_name>:<value>\tset property\n"); 1488 1489 fprintf(stderr, "\n Generic options:\n\n"); 1490 fprintf(stderr, "\t-d\tdrop master after mode set\n"); 1491 fprintf(stderr, "\t-M module\tuse the given driver\n"); 1492 fprintf(stderr, "\t-D device\tuse the given device\n"); 1493 1494 fprintf(stderr, "\n\tDefault is to dump all info.\n"); 1495 exit(0); 1496} 1497 1498static int page_flipping_supported(void) 1499{ 1500 /*FIXME: generic ioctl needed? */ 1501 return 1; 1502#if 0 1503 int ret, value; 1504 struct drm_i915_getparam gp; 1505 1506 gp.param = I915_PARAM_HAS_PAGEFLIPPING; 1507 gp.value = &value; 1508 1509 ret = drmCommandWriteRead(fd, DRM_I915_GETPARAM, &gp, sizeof(gp)); 1510 if (ret) { 1511 fprintf(stderr, "drm_i915_getparam: %m\n"); 1512 return 0; 1513 } 1514 1515 return *gp.value; 1516#endif 1517} 1518 1519static int cursor_supported(void) 1520{ 1521 /*FIXME: generic ioctl needed? */ 1522 return 1; 1523} 1524 1525static int pipe_resolve_connectors(struct device *dev, struct pipe_arg *pipe) 1526{ 1527 drmModeConnector *connector; 1528 unsigned int i; 1529 uint32_t id; 1530 char *endp; 1531 1532 for (i = 0; i < pipe->num_cons; i++) { 1533 id = strtoul(pipe->cons[i], &endp, 10); 1534 if (endp == pipe->cons[i]) { 1535 connector = get_connector_by_name(dev, pipe->cons[i]); 1536 if (!connector) { 1537 fprintf(stderr, "no connector named '%s'\n", 1538 pipe->cons[i]); 1539 return -ENODEV; 1540 } 1541 1542 id = connector->connector_id; 1543 } 1544 1545 pipe->con_ids[i] = id; 1546 } 1547 1548 return 0; 1549} 1550 1551static char optstr[] = "cdD:efM:P:ps:Cvw:"; 1552 1553int main(int argc, char **argv) 1554{ 1555 struct device dev; 1556 1557 int c; 1558 int encoders = 0, connectors = 0, crtcs = 0, planes = 0, framebuffers = 0; 1559 int drop_master = 0; 1560 int test_vsync = 0; 1561 int test_cursor = 0; 1562 const char *modules[] = { "i915", "radeon", "nouveau", "vmwgfx", "omapdrm", "exynos", "tilcdc", "msm", "sti", "tegra", "imx-drm", "rockchip", "atmel-hlcdc" }; 1563 char *device = NULL; 1564 char *module = NULL; 1565 unsigned int i; 1566 unsigned int count = 0, plane_count = 0; 1567 unsigned int prop_count = 0; 1568 struct pipe_arg *pipe_args = NULL; 1569 struct plane_arg *plane_args = NULL; 1570 struct property_arg *prop_args = NULL; 1571 unsigned int args = 0; 1572 int ret; 1573 1574 memset(&dev, 0, sizeof dev); 1575 1576 opterr = 0; 1577 while ((c = getopt(argc, argv, optstr)) != -1) { 1578 args++; 1579 1580 switch (c) { 1581 case 'c': 1582 connectors = 1; 1583 break; 1584 case 'D': 1585 device = optarg; 1586 args--; 1587 break; 1588 case 'd': 1589 drop_master = 1; 1590 break; 1591 case 'e': 1592 encoders = 1; 1593 break; 1594 case 'f': 1595 framebuffers = 1; 1596 break; 1597 case 'M': 1598 module = optarg; 1599 /* Preserve the default behaviour of dumping all information. */ 1600 args--; 1601 break; 1602 case 'P': 1603 plane_args = realloc(plane_args, 1604 (plane_count + 1) * sizeof *plane_args); 1605 if (plane_args == NULL) { 1606 fprintf(stderr, "memory allocation failed\n"); 1607 return 1; 1608 } 1609 memset(&plane_args[plane_count], 0, sizeof(*plane_args)); 1610 1611 if (parse_plane(&plane_args[plane_count], optarg) < 0) 1612 usage(argv[0]); 1613 1614 plane_count++; 1615 break; 1616 case 'p': 1617 crtcs = 1; 1618 planes = 1; 1619 break; 1620 case 's': 1621 pipe_args = realloc(pipe_args, 1622 (count + 1) * sizeof *pipe_args); 1623 if (pipe_args == NULL) { 1624 fprintf(stderr, "memory allocation failed\n"); 1625 return 1; 1626 } 1627 memset(&pipe_args[count], 0, sizeof(*pipe_args)); 1628 1629 if (parse_connector(&pipe_args[count], optarg) < 0) 1630 usage(argv[0]); 1631 1632 count++; 1633 break; 1634 case 'C': 1635 test_cursor = 1; 1636 break; 1637 case 'v': 1638 test_vsync = 1; 1639 break; 1640 case 'w': 1641 prop_args = realloc(prop_args, 1642 (prop_count + 1) * sizeof *prop_args); 1643 if (prop_args == NULL) { 1644 fprintf(stderr, "memory allocation failed\n"); 1645 return 1; 1646 } 1647 memset(&prop_args[prop_count], 0, sizeof(*prop_args)); 1648 1649 if (parse_property(&prop_args[prop_count], optarg) < 0) 1650 usage(argv[0]); 1651 1652 prop_count++; 1653 break; 1654 default: 1655 usage(argv[0]); 1656 break; 1657 } 1658 } 1659 1660 if (!args) 1661 encoders = connectors = crtcs = planes = framebuffers = 1; 1662 1663 if (module) { 1664 dev.fd = drmOpen(module, device); 1665 if (dev.fd < 0) { 1666 fprintf(stderr, "failed to open device '%s'.\n", module); 1667 return 1; 1668 } 1669 } else { 1670 for (i = 0; i < ARRAY_SIZE(modules); i++) { 1671 printf("trying to open device '%s'...", modules[i]); 1672 dev.fd = drmOpen(modules[i], device); 1673 if (dev.fd < 0) { 1674 printf("failed.\n"); 1675 } else { 1676 printf("success.\n"); 1677 break; 1678 } 1679 } 1680 1681 if (dev.fd < 0) { 1682 fprintf(stderr, "no device found.\n"); 1683 return 1; 1684 } 1685 } 1686 1687 if (test_vsync && !page_flipping_supported()) { 1688 fprintf(stderr, "page flipping not supported by drm.\n"); 1689 return -1; 1690 } 1691 1692 if (test_vsync && !count) { 1693 fprintf(stderr, "page flipping requires at least one -s option.\n"); 1694 return -1; 1695 } 1696 1697 if (test_cursor && !cursor_supported()) { 1698 fprintf(stderr, "hw cursor not supported by drm.\n"); 1699 return -1; 1700 } 1701 1702 dev.resources = get_resources(&dev); 1703 if (!dev.resources) { 1704 drmClose(dev.fd); 1705 return 1; 1706 } 1707 1708 for (i = 0; i < count; i++) { 1709 if (pipe_resolve_connectors(&dev, &pipe_args[i]) < 0) { 1710 free_resources(dev.resources); 1711 drmClose(dev.fd); 1712 return 1; 1713 } 1714 } 1715 1716#define dump_resource(dev, res) if (res) dump_##res(dev) 1717 1718 dump_resource(&dev, encoders); 1719 dump_resource(&dev, connectors); 1720 dump_resource(&dev, crtcs); 1721 dump_resource(&dev, planes); 1722 dump_resource(&dev, framebuffers); 1723 1724 for (i = 0; i < prop_count; ++i) 1725 set_property(&dev, &prop_args[i]); 1726 1727 if (count || plane_count) { 1728 uint64_t cap = 0; 1729 1730 ret = drmGetCap(dev.fd, DRM_CAP_DUMB_BUFFER, &cap); 1731 if (ret || cap == 0) { 1732 fprintf(stderr, "driver doesn't support the dumb buffer API\n"); 1733 return 1; 1734 } 1735 1736 if (count) 1737 set_mode(&dev, pipe_args, count); 1738 1739 if (plane_count) 1740 set_planes(&dev, plane_args, plane_count); 1741 1742 if (test_cursor) 1743 set_cursors(&dev, pipe_args, count); 1744 1745 if (test_vsync) 1746 test_page_flip(&dev, pipe_args, count); 1747 1748 if (drop_master) 1749 drmDropMaster(dev.fd); 1750 1751 getchar(); 1752 1753 if (test_cursor) 1754 clear_cursors(&dev); 1755 1756 if (plane_count) 1757 clear_planes(&dev, plane_args, plane_count); 1758 1759 if (count) 1760 clear_mode(&dev); 1761 } 1762 1763 free_resources(dev.resources); 1764 1765 return 0; 1766} 1767