Dominik Behr | 83010f8 | 2016-03-18 18:43:08 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2014 The Chromium OS Authors. All rights reserved. |
| 3 | * Use of this source code is governed by a BSD-style license that can be |
| 4 | * found in the LICENSE file. |
| 5 | */ |
| 6 | |
| 7 | #include <errno.h> |
| 8 | #include <fcntl.h> |
| 9 | #include <stdbool.h> |
| 10 | #include <stddef.h> |
| 11 | #include <stdio.h> |
| 12 | #include <stdlib.h> |
| 13 | #include <string.h> |
| 14 | #include <sys/mman.h> |
| 15 | #include <time.h> |
| 16 | #include <unistd.h> |
| 17 | |
| 18 | #include "drm.h" |
Dominik Behr | d2cc4d2 | 2016-01-29 17:31:52 -0800 | [diff] [blame] | 19 | #include "input.h" |
Dominik Behr | 83010f8 | 2016-03-18 18:43:08 -0700 | [diff] [blame] | 20 | #include "util.h" |
| 21 | |
| 22 | static drm_t* drm = NULL; |
| 23 | |
| 24 | static void drm_disable_crtc(drm_t* drm, drmModeCrtc* crtc) |
| 25 | { |
| 26 | if (crtc) { |
| 27 | drmModeSetCrtc(drm->fd, crtc->crtc_id, 0, // buffer_id |
| 28 | 0, 0, // x,y |
| 29 | NULL, // connectors |
| 30 | 0, // connector_count |
| 31 | NULL); // mode |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | static drmModeCrtc* find_crtc_for_connector(drm_t* drm, drmModeConnector* connector) |
| 36 | { |
| 37 | int i, j; |
| 38 | drmModeEncoder* encoder; |
| 39 | int32_t crtc_id; |
| 40 | |
| 41 | if (connector->encoder_id) |
| 42 | encoder = drmModeGetEncoder(drm->fd, connector->encoder_id); |
| 43 | else |
| 44 | encoder = NULL; |
| 45 | |
| 46 | if (encoder && encoder->crtc_id) { |
| 47 | crtc_id = encoder->crtc_id; |
| 48 | drmModeFreeEncoder(encoder); |
| 49 | return drmModeGetCrtc(drm->fd, crtc_id); |
| 50 | } |
| 51 | |
| 52 | crtc_id = -1; |
| 53 | for (i = 0; i < connector->count_encoders; i++) { |
| 54 | encoder = drmModeGetEncoder(drm->fd, connector->encoders[i]); |
| 55 | |
| 56 | if (encoder) { |
| 57 | for (j = 0; j < drm->resources->count_crtcs; j++) { |
| 58 | if (!(encoder->possible_crtcs & (1 << j))) |
| 59 | continue; |
| 60 | crtc_id = drm->resources->crtcs[j]; |
| 61 | break; |
| 62 | } |
| 63 | if (crtc_id >= 0) { |
| 64 | drmModeFreeEncoder(encoder); |
| 65 | return drmModeGetCrtc(drm->fd, crtc_id); |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | return NULL; |
| 71 | } |
| 72 | |
| 73 | static void drm_disable_non_main_crtcs(drm_t* drm) |
| 74 | { |
| 75 | int i; |
| 76 | drmModeCrtc* crtc; |
| 77 | |
| 78 | for (i = 0; i < drm->resources->count_connectors; i++) { |
| 79 | drmModeConnector* connector; |
| 80 | |
| 81 | connector = drmModeGetConnector(drm->fd, drm->resources->connectors[i]); |
| 82 | crtc = find_crtc_for_connector(drm, connector); |
| 83 | if (crtc->crtc_id != drm->crtc->crtc_id) |
| 84 | drm_disable_crtc(drm, crtc); |
| 85 | drmModeFreeCrtc(crtc); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | static int drm_is_primary_plane(drm_t* drm, uint32_t plane_id) |
| 90 | { |
| 91 | uint32_t p; |
| 92 | bool found = false; |
| 93 | int ret = -1; |
| 94 | |
| 95 | drmModeObjectPropertiesPtr props; |
| 96 | props = drmModeObjectGetProperties(drm->fd, |
| 97 | plane_id, |
| 98 | DRM_MODE_OBJECT_PLANE); |
| 99 | if (!props) { |
| 100 | LOG(ERROR, "Unable to get plane properties: %m"); |
| 101 | return -1; |
| 102 | } |
| 103 | |
| 104 | for (p = 0; p < props->count_props && !found; p++) { |
| 105 | drmModePropertyPtr prop; |
| 106 | prop = drmModeGetProperty(drm->fd, props->props[p]); |
| 107 | if (prop) { |
| 108 | if (strcmp("type", prop->name) == 0) { |
| 109 | found = true; |
| 110 | ret = (props->prop_values[p] == DRM_PLANE_TYPE_PRIMARY); |
| 111 | } |
| 112 | drmModeFreeProperty(prop); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | drmModeFreeObjectProperties(props); |
| 117 | |
| 118 | return ret; |
| 119 | } |
| 120 | |
| 121 | /* disable all planes except for primary on crtc we use */ |
| 122 | static void drm_disable_non_primary_planes(drm_t* drm) |
| 123 | { |
| 124 | int ret; |
| 125 | |
| 126 | if (!drm->plane_resources) |
| 127 | return; |
| 128 | |
| 129 | for (uint32_t p = 0; p < drm->plane_resources->count_planes; p++) { |
| 130 | drmModePlanePtr plane; |
| 131 | plane = drmModeGetPlane(drm->fd, |
| 132 | drm->plane_resources->planes[p]); |
| 133 | if (plane) { |
| 134 | int primary = drm_is_primary_plane(drm, plane->plane_id); |
| 135 | if (!(plane->crtc_id == drm->crtc->crtc_id && primary != 0)) { |
| 136 | ret = drmModeSetPlane(drm->fd, plane->plane_id, plane->crtc_id, |
| 137 | 0, 0, |
| 138 | 0, 0, |
| 139 | 0, 0, |
| 140 | 0, 0, |
| 141 | 0, 0); |
| 142 | if (ret) { |
| 143 | LOG(WARNING, "Unable to disable plane: %m"); |
| 144 | } |
| 145 | } |
| 146 | drmModeFreePlane(plane); |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | static bool drm_is_internal(unsigned type) |
| 152 | { |
| 153 | unsigned t; |
| 154 | unsigned kInternalConnectors[] = { |
| 155 | DRM_MODE_CONNECTOR_LVDS, |
| 156 | DRM_MODE_CONNECTOR_eDP, |
| 157 | DRM_MODE_CONNECTOR_DSI, |
| 158 | }; |
| 159 | for (t = 0; t < ARRAY_SIZE(kInternalConnectors); t++) |
| 160 | if (type == kInternalConnectors[t]) |
| 161 | return true; |
| 162 | return false; |
| 163 | } |
| 164 | |
| 165 | static drmModeConnector* find_first_connected_connector(drm_t* drm, bool internal, bool external) |
| 166 | { |
| 167 | for (int i = 0; i < drm->resources->count_connectors; i++) { |
| 168 | drmModeConnector* connector; |
| 169 | |
| 170 | connector = drmModeGetConnector(drm->fd, drm->resources->connectors[i]); |
| 171 | if (connector) { |
| 172 | bool is_internal = drm_is_internal(connector->connector_type); |
| 173 | if (!internal && is_internal) |
| 174 | continue; |
| 175 | if (!external && !is_internal) |
| 176 | continue; |
| 177 | if ((connector->count_modes > 0) && |
| 178 | (connector->connection == DRM_MODE_CONNECTED)) |
| 179 | return connector; |
| 180 | |
| 181 | drmModeFreeConnector(connector); |
| 182 | } |
| 183 | } |
| 184 | return NULL; |
| 185 | } |
| 186 | |
| 187 | static drmModeConnector* find_main_monitor(drm_t* drm, uint32_t* mode_index) |
| 188 | { |
| 189 | int modes; |
Dominik Behr | d2cc4d2 | 2016-01-29 17:31:52 -0800 | [diff] [blame] | 190 | int lid_state = input_check_lid_state(); |
Dominik Behr | 83010f8 | 2016-03-18 18:43:08 -0700 | [diff] [blame] | 191 | drmModeConnector* main_monitor_connector = NULL; |
| 192 | |
| 193 | /* |
| 194 | * Find the LVDS/eDP/DSI connectors. Those are the main screens. |
| 195 | */ |
Dominik Behr | d2cc4d2 | 2016-01-29 17:31:52 -0800 | [diff] [blame] | 196 | if (lid_state <= 0) |
| 197 | main_monitor_connector = find_first_connected_connector(drm, true, false); |
Dominik Behr | 83010f8 | 2016-03-18 18:43:08 -0700 | [diff] [blame] | 198 | |
| 199 | /* |
| 200 | * Now try external connectors. |
| 201 | */ |
| 202 | if (!main_monitor_connector) |
| 203 | main_monitor_connector = |
| 204 | find_first_connected_connector(drm, false, true); |
| 205 | |
| 206 | /* |
| 207 | * If we still didn't find a connector, give up and return. |
| 208 | */ |
| 209 | if (!main_monitor_connector) |
| 210 | return NULL; |
| 211 | |
| 212 | *mode_index = 0; |
| 213 | for (modes = 0; modes < main_monitor_connector->count_modes; modes++) { |
| 214 | if (main_monitor_connector->modes[modes].type & |
| 215 | DRM_MODE_TYPE_PREFERRED) { |
| 216 | *mode_index = modes; |
| 217 | break; |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | return main_monitor_connector; |
| 222 | } |
| 223 | |
| 224 | static void drm_fini(drm_t* drm) |
| 225 | { |
| 226 | if (!drm) |
| 227 | return; |
| 228 | |
| 229 | if (drm->fd >= 0) { |
| 230 | if (drm->crtc) { |
Dominik Behr | 83010f8 | 2016-03-18 18:43:08 -0700 | [diff] [blame] | 231 | drmModeFreeCrtc(drm->crtc); |
| 232 | drm->crtc = NULL; |
| 233 | } |
| 234 | |
| 235 | if (drm->main_monitor_connector) { |
| 236 | drmModeFreeConnector(drm->main_monitor_connector); |
| 237 | drm->main_monitor_connector = NULL; |
| 238 | } |
| 239 | |
| 240 | if (drm->plane_resources) { |
| 241 | drmModeFreePlaneResources(drm->plane_resources); |
| 242 | drm->plane_resources = NULL; |
| 243 | } |
| 244 | |
| 245 | if (drm->resources) { |
| 246 | drmModeFreeResources(drm->resources); |
| 247 | drm->resources = NULL; |
| 248 | } |
| 249 | |
| 250 | drmClose(drm->fd); |
| 251 | drm->fd = -1; |
| 252 | } |
| 253 | |
| 254 | free(drm); |
| 255 | } |
| 256 | |
| 257 | static bool drm_equal(drm_t* l, drm_t* r) |
| 258 | { |
| 259 | if (!l->crtc && r->crtc) |
| 260 | return false; |
| 261 | if (l->crtc && !r->crtc) |
| 262 | return false; |
| 263 | if (l->crtc && r->crtc) |
| 264 | if (l->crtc->crtc_id != r->crtc->crtc_id) |
| 265 | return false; |
| 266 | |
| 267 | if (!l->main_monitor_connector && r->main_monitor_connector) |
| 268 | return false; |
| 269 | if (l->main_monitor_connector && !r->main_monitor_connector) |
| 270 | return false; |
| 271 | if (l->main_monitor_connector && r->main_monitor_connector) |
| 272 | if (l->main_monitor_connector->connector_id != r->main_monitor_connector->connector_id) |
| 273 | return false; |
| 274 | return true; |
| 275 | } |
| 276 | |
| 277 | static int drm_score(drm_t* drm) |
| 278 | { |
| 279 | drmVersionPtr version; |
| 280 | int score = 0; |
| 281 | |
| 282 | if (!drm) |
| 283 | return -1000000000; |
| 284 | |
| 285 | if (!drm->main_monitor_connector) |
| 286 | return -1000000000; |
| 287 | |
| 288 | if (drm_is_internal(drm->main_monitor_connector->connector_type)) |
| 289 | score++; |
| 290 | |
| 291 | version = drmGetVersion(drm->fd); |
| 292 | if (version) { |
| 293 | /* we would rather use any driver besides UDL */ |
| 294 | if (strcmp("udl", version->name) == 0) |
| 295 | score--; |
| 296 | if (strcmp("evdi", version->name) == 0) |
| 297 | score--; |
| 298 | /* VGEM should be ignored because it has no displays, but lets make sure */ |
| 299 | if (strcmp("vgem", version->name) == 0) |
| 300 | score -= 1000000; |
| 301 | drmFreeVersion(version); |
| 302 | } |
| 303 | return score; |
| 304 | } |
| 305 | |
| 306 | drm_t* drm_scan(void) |
| 307 | { |
| 308 | unsigned i; |
| 309 | char* dev_name; |
| 310 | int ret; |
| 311 | drm_t *best_drm = NULL; |
| 312 | |
| 313 | for (i = 0; i < DRM_MAX_MINOR; i++) { |
| 314 | drm_t* drm = calloc(1, sizeof(drm_t)); |
| 315 | |
| 316 | if (!drm) |
| 317 | return NULL; |
| 318 | |
| 319 | ret = asprintf(&dev_name, DRM_DEV_NAME, DRM_DIR_NAME, i); |
| 320 | if (ret < 0) { |
| 321 | drm_fini(drm); |
| 322 | continue; |
| 323 | } |
| 324 | |
| 325 | drm->fd = open(dev_name, O_RDWR, 0); |
| 326 | free(dev_name); |
| 327 | if (drm->fd < 0) { |
| 328 | drm_fini(drm); |
| 329 | continue; |
| 330 | } |
| 331 | |
| 332 | drm->resources = drmModeGetResources(drm->fd); |
| 333 | if (!drm->resources) { |
| 334 | drm_fini(drm); |
| 335 | continue; |
| 336 | } |
| 337 | |
| 338 | /* expect at least one crtc so we do not try to run on VGEM */ |
| 339 | if (drm->resources->count_crtcs == 0 || drm->resources->count_connectors == 0) { |
| 340 | drm_fini(drm); |
| 341 | continue; |
| 342 | } |
| 343 | |
| 344 | drm->main_monitor_connector = find_main_monitor(drm, &drm->selected_mode); |
| 345 | if (!drm->main_monitor_connector) { |
| 346 | drm_fini(drm); |
| 347 | continue; |
| 348 | } |
| 349 | |
| 350 | drm->crtc = find_crtc_for_connector(drm, drm->main_monitor_connector); |
| 351 | if (!drm->crtc) { |
| 352 | drm_fini(drm); |
| 353 | continue; |
| 354 | } |
| 355 | |
| 356 | drm->crtc->mode = drm->main_monitor_connector->modes[drm->selected_mode]; |
| 357 | |
| 358 | drm->plane_resources = drmModeGetPlaneResources(drm->fd); |
| 359 | drm->refcount = 1; |
| 360 | |
| 361 | if (drm_score(drm) > drm_score(best_drm)) { |
| 362 | drm_fini(best_drm); |
| 363 | best_drm = drm; |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | if (best_drm) { |
| 368 | drmVersionPtr version; |
| 369 | version = drmGetVersion(best_drm->fd); |
| 370 | if (version) { |
| 371 | LOG(INFO, |
| 372 | "Frecon using drm driver %s, version %d.%d, date(%s), desc(%s)", |
| 373 | version->name, |
| 374 | version->version_major, |
| 375 | version->version_minor, |
| 376 | version->date, |
| 377 | version->desc); |
| 378 | drmFreeVersion(version); |
| 379 | } |
| 380 | drmDropMaster(best_drm->fd); |
| 381 | } |
| 382 | |
| 383 | return best_drm; |
| 384 | } |
| 385 | |
| 386 | void drm_set(drm_t* drm_) |
| 387 | { |
| 388 | if (drm) { |
| 389 | drm_delref(drm); |
| 390 | drm = NULL; |
| 391 | } |
| 392 | drm = drm_; |
| 393 | } |
| 394 | |
| 395 | void drm_close(void) |
| 396 | { |
| 397 | if (drm) { |
| 398 | drm_delref(drm); |
| 399 | drm = NULL; |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | void drm_delref(drm_t *drm) |
| 404 | { |
| 405 | if (drm->refcount) { |
| 406 | drm->refcount--; |
| 407 | } else { |
| 408 | LOG(ERROR, "Imbalanced drm_close()"); |
| 409 | } |
| 410 | if (drm->refcount) { |
| 411 | return; |
| 412 | } |
| 413 | |
| 414 | drm_fini(drm); |
| 415 | } |
| 416 | |
| 417 | drm_t* drm_addref(void) |
| 418 | { |
| 419 | if (drm) { |
| 420 | drm->refcount++; |
| 421 | return drm; |
| 422 | } |
| 423 | |
| 424 | return NULL; |
| 425 | } |
| 426 | |
| 427 | /* |
| 428 | * returns true if connector/crtc/driver have changed and framebuffer object have to be re-create |
| 429 | */ |
| 430 | bool drm_rescan(void) |
| 431 | { |
| 432 | drm_t* ndrm; |
| 433 | |
| 434 | ndrm = drm_scan(); |
| 435 | if (ndrm) { |
| 436 | if (drm_equal(ndrm, drm)) { |
| 437 | drm_fini(ndrm); |
| 438 | } else { |
| 439 | drm_delref(drm); |
| 440 | drm = ndrm; |
| 441 | return true; |
| 442 | } |
| 443 | } else { |
| 444 | if (drm) { |
| 445 | drm_delref(drm); /* no usable monitor/drm object */ |
| 446 | drm = NULL; |
| 447 | return true; |
| 448 | } |
| 449 | } |
| 450 | return false; |
| 451 | } |
| 452 | |
| 453 | bool drm_valid(drm_t* drm) { |
| 454 | return drm && drm->fd >= 0 && drm->resources && drm->main_monitor_connector && drm->crtc; |
| 455 | } |
| 456 | |
| 457 | int32_t drm_setmode(drm_t* drm, uint32_t fb_id) |
| 458 | { |
| 459 | int32_t ret; |
| 460 | |
| 461 | ret = drmSetMaster(drm->fd); |
| 462 | if (ret) |
| 463 | LOG(ERROR, "drmSetMaster failed: %m"); |
| 464 | |
| 465 | ret = drmModeSetCrtc(drm->fd, drm->crtc->crtc_id, |
| 466 | fb_id, |
| 467 | 0, 0, // x,y |
| 468 | &drm->main_monitor_connector->connector_id, |
| 469 | 1, // connector_count |
| 470 | &drm->crtc->mode); // mode |
| 471 | |
| 472 | if (ret) { |
| 473 | LOG(ERROR, "Unable to set crtc: %m"); |
| 474 | drmDropMaster(drm->fd); |
| 475 | return ret; |
| 476 | } |
| 477 | |
| 478 | ret = drmModeSetCursor(drm->fd, drm->crtc->crtc_id, |
| 479 | 0, 0, 0); |
| 480 | |
| 481 | if (ret) |
| 482 | LOG(ERROR, "Unable to hide cursor"); |
| 483 | |
| 484 | drm_disable_non_primary_planes(drm); |
| 485 | drm_disable_non_main_crtcs(drm); |
| 486 | drmDropMaster(drm->fd); |
| 487 | return ret; |
| 488 | } |
| 489 | |
| 490 | bool drm_read_edid(drm_t* drm) |
| 491 | { |
| 492 | if (drm->edid_found) { |
| 493 | return true; |
| 494 | } |
| 495 | |
| 496 | for (int i = 0; i < drm->main_monitor_connector->count_props; i++) { |
| 497 | drmModePropertyPtr prop; |
| 498 | drmModePropertyBlobPtr blob_ptr; |
| 499 | prop = drmModeGetProperty(drm->fd, drm->main_monitor_connector->props[i]); |
| 500 | if (prop) { |
| 501 | if (strcmp(prop->name, "EDID") == 0) { |
| 502 | blob_ptr = drmModeGetPropertyBlob(drm->fd, |
| 503 | drm->main_monitor_connector->prop_values[i]); |
| 504 | if (blob_ptr) { |
| 505 | memcpy(&drm->edid, blob_ptr->data, EDID_SIZE); |
| 506 | drmModeFreePropertyBlob(blob_ptr); |
| 507 | return (drm->edid_found = true); |
| 508 | } |
| 509 | } |
| 510 | } |
| 511 | } |
| 512 | |
| 513 | return false; |
| 514 | } |
| 515 | |
| 516 | uint32_t drm_gethres(drm_t* drm) |
| 517 | { |
| 518 | return drm->crtc->mode.hdisplay; |
| 519 | } |
| 520 | |
| 521 | uint32_t drm_getvres(drm_t* drm) |
| 522 | { |
| 523 | return drm->crtc->mode.vdisplay; |
| 524 | } |