1 /* 2 * Copyright (c) 2006-2008 Intel Corporation 3 * Copyright (c) 2007 Dave Airlie <airlied@linux.ie> 4 * 5 * DRM core CRTC related functions 6 * 7 * Permission to use, copy, modify, distribute, and sell this software and its 8 * documentation for any purpose is hereby granted without fee, provided that 9 * the above copyright notice appear in all copies and that both that copyright 10 * notice and this permission notice appear in supporting documentation, and 11 * that the name of the copyright holders not be used in advertising or 12 * publicity pertaining to distribution of the software without specific, 13 * written prior permission. The copyright holders make no representations 14 * about the suitability of this software for any purpose. It is provided "as 15 * is" without express or implied warranty. 16 * 17 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 18 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO 19 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR 20 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, 21 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 22 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 23 * OF THIS SOFTWARE. 24 * 25 * Authors: 26 * Keith Packard 27 * Eric Anholt <eric@anholt.net> 28 * Dave Airlie <airlied@linux.ie> 29 * Jesse Barnes <jesse.barnes@intel.com> 30 */ 31 32 #include <sys/cdefs.h> 33 #include <dev/drm2/drmP.h> 34 #include <dev/drm2/drm_crtc.h> 35 #include <dev/drm2/drm_fourcc.h> 36 #include <dev/drm2/drm_crtc_helper.h> 37 #include <dev/drm2/drm_fb_helper.h> 38 #include <dev/drm2/drm_edid.h> 39 40 /** 41 * drm_helper_move_panel_connectors_to_head() - move panels to the front in the 42 * connector list 43 * @dev: drm device to operate on 44 * 45 * Some userspace presumes that the first connected connector is the main 46 * display, where it's supposed to display e.g. the login screen. For 47 * laptops, this should be the main panel. Use this function to sort all 48 * (eDP/LVDS) panels to the front of the connector list, instead of 49 * painstakingly trying to initialize them in the right order. 50 */ 51 void drm_helper_move_panel_connectors_to_head(struct drm_device *dev) 52 { 53 struct drm_connector *connector, *tmp; 54 struct list_head panel_list; 55 56 INIT_LIST_HEAD(&panel_list); 57 58 list_for_each_entry_safe(connector, tmp, 59 &dev->mode_config.connector_list, head) { 60 if (connector->connector_type == DRM_MODE_CONNECTOR_LVDS || 61 connector->connector_type == DRM_MODE_CONNECTOR_eDP) 62 list_move_tail(&connector->head, &panel_list); 63 } 64 65 list_splice(&panel_list, &dev->mode_config.connector_list); 66 } 67 EXPORT_SYMBOL(drm_helper_move_panel_connectors_to_head); 68 69 static bool drm_kms_helper_poll = true; 70 module_param_named(poll, drm_kms_helper_poll, bool, 0600); 71 72 static void drm_mode_validate_flag(struct drm_connector *connector, 73 int flags) 74 { 75 struct drm_display_mode *mode; 76 77 if (flags == (DRM_MODE_FLAG_DBLSCAN | DRM_MODE_FLAG_INTERLACE)) 78 return; 79 80 list_for_each_entry(mode, &connector->modes, head) { 81 if ((mode->flags & DRM_MODE_FLAG_INTERLACE) && 82 !(flags & DRM_MODE_FLAG_INTERLACE)) 83 mode->status = MODE_NO_INTERLACE; 84 if ((mode->flags & DRM_MODE_FLAG_DBLSCAN) && 85 !(flags & DRM_MODE_FLAG_DBLSCAN)) 86 mode->status = MODE_NO_DBLESCAN; 87 } 88 89 return; 90 } 91 92 /** 93 * drm_helper_probe_single_connector_modes - get complete set of display modes 94 * @connector: connector to probe 95 * @maxX: max width for modes 96 * @maxY: max height for modes 97 * 98 * LOCKING: 99 * Caller must hold mode config lock. 100 * 101 * Based on the helper callbacks implemented by @connector try to detect all 102 * valid modes. Modes will first be added to the connector's probed_modes list, 103 * then culled (based on validity and the @maxX, @maxY parameters) and put into 104 * the normal modes list. 105 * 106 * Intended to be use as a generic implementation of the ->probe() @connector 107 * callback for drivers that use the crtc helpers for output mode filtering and 108 * detection. 109 * 110 * RETURNS: 111 * Number of modes found on @connector. 112 */ 113 int drm_helper_probe_single_connector_modes(struct drm_connector *connector, 114 uint32_t maxX, uint32_t maxY) 115 { 116 struct drm_device *dev = connector->dev; 117 struct drm_display_mode *mode; 118 struct drm_connector_helper_funcs *connector_funcs = 119 connector->helper_private; 120 int count = 0; 121 int mode_flags = 0; 122 123 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n", connector->base.id, 124 drm_get_connector_name(connector)); 125 /* set all modes to the unverified state */ 126 list_for_each_entry(mode, &connector->modes, head) 127 mode->status = MODE_UNVERIFIED; 128 129 if (connector->force) { 130 if (connector->force == DRM_FORCE_ON) 131 connector->status = connector_status_connected; 132 else 133 connector->status = connector_status_disconnected; 134 if (connector->funcs->force) 135 connector->funcs->force(connector); 136 } else { 137 connector->status = connector->funcs->detect(connector, true); 138 } 139 140 /* Re-enable polling in case the global poll config changed. */ 141 if (drm_kms_helper_poll != dev->mode_config.poll_running) 142 drm_kms_helper_poll_enable(dev); 143 144 dev->mode_config.poll_running = drm_kms_helper_poll; 145 146 if (connector->status == connector_status_disconnected) { 147 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] disconnected\n", 148 connector->base.id, drm_get_connector_name(connector)); 149 drm_mode_connector_update_edid_property(connector, NULL); 150 goto prune; 151 } 152 153 #ifdef FREEBSD_NOTYET 154 #ifdef CONFIG_DRM_LOAD_EDID_FIRMWARE 155 count = drm_load_edid_firmware(connector); 156 if (count == 0) 157 #endif 158 #endif /* FREEBSD_NOTYET */ 159 count = (*connector_funcs->get_modes)(connector); 160 161 if (count == 0 && connector->status == connector_status_connected) 162 count = drm_add_modes_noedid(connector, 1024, 768); 163 if (count == 0) 164 goto prune; 165 166 drm_mode_connector_list_update(connector); 167 168 if (maxX && maxY) 169 drm_mode_validate_size(dev, &connector->modes, maxX, 170 maxY, 0); 171 172 if (connector->interlace_allowed) 173 mode_flags |= DRM_MODE_FLAG_INTERLACE; 174 if (connector->doublescan_allowed) 175 mode_flags |= DRM_MODE_FLAG_DBLSCAN; 176 drm_mode_validate_flag(connector, mode_flags); 177 178 list_for_each_entry(mode, &connector->modes, head) { 179 if (mode->status == MODE_OK) 180 mode->status = connector_funcs->mode_valid(connector, 181 mode); 182 } 183 184 prune: 185 drm_mode_prune_invalid(dev, &connector->modes, true); 186 187 if (list_empty(&connector->modes)) 188 return 0; 189 190 list_for_each_entry(mode, &connector->modes, head) 191 mode->vrefresh = drm_mode_vrefresh(mode); 192 193 drm_mode_sort(&connector->modes); 194 195 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] probed modes :\n", connector->base.id, 196 drm_get_connector_name(connector)); 197 list_for_each_entry(mode, &connector->modes, head) { 198 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V); 199 drm_mode_debug_printmodeline(mode); 200 } 201 202 return count; 203 } 204 EXPORT_SYMBOL(drm_helper_probe_single_connector_modes); 205 206 /** 207 * drm_helper_encoder_in_use - check if a given encoder is in use 208 * @encoder: encoder to check 209 * 210 * LOCKING: 211 * Caller must hold mode config lock. 212 * 213 * Walk @encoders's DRM device's mode_config and see if it's in use. 214 * 215 * RETURNS: 216 * True if @encoder is part of the mode_config, false otherwise. 217 */ 218 bool drm_helper_encoder_in_use(struct drm_encoder *encoder) 219 { 220 struct drm_connector *connector; 221 struct drm_device *dev = encoder->dev; 222 list_for_each_entry(connector, &dev->mode_config.connector_list, head) 223 if (connector->encoder == encoder) 224 return true; 225 return false; 226 } 227 EXPORT_SYMBOL(drm_helper_encoder_in_use); 228 229 /** 230 * drm_helper_crtc_in_use - check if a given CRTC is in a mode_config 231 * @crtc: CRTC to check 232 * 233 * LOCKING: 234 * Caller must hold mode config lock. 235 * 236 * Walk @crtc's DRM device's mode_config and see if it's in use. 237 * 238 * RETURNS: 239 * True if @crtc is part of the mode_config, false otherwise. 240 */ 241 bool drm_helper_crtc_in_use(struct drm_crtc *crtc) 242 { 243 struct drm_encoder *encoder; 244 struct drm_device *dev = crtc->dev; 245 /* FIXME: Locking around list access? */ 246 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) 247 if (encoder->crtc == crtc && drm_helper_encoder_in_use(encoder)) 248 return true; 249 return false; 250 } 251 EXPORT_SYMBOL(drm_helper_crtc_in_use); 252 253 static void 254 drm_encoder_disable(struct drm_encoder *encoder) 255 { 256 struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private; 257 258 if (encoder_funcs->disable) 259 (*encoder_funcs->disable)(encoder); 260 else 261 (*encoder_funcs->dpms)(encoder, DRM_MODE_DPMS_OFF); 262 } 263 264 /** 265 * drm_helper_disable_unused_functions - disable unused objects 266 * @dev: DRM device 267 * 268 * LOCKING: 269 * Caller must hold mode config lock. 270 * 271 * If an connector or CRTC isn't part of @dev's mode_config, it can be disabled 272 * by calling its dpms function, which should power it off. 273 */ 274 void drm_helper_disable_unused_functions(struct drm_device *dev) 275 { 276 struct drm_encoder *encoder; 277 struct drm_connector *connector; 278 struct drm_crtc *crtc; 279 280 list_for_each_entry(connector, &dev->mode_config.connector_list, head) { 281 if (!connector->encoder) 282 continue; 283 if (connector->status == connector_status_disconnected) 284 connector->encoder = NULL; 285 } 286 287 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) { 288 if (!drm_helper_encoder_in_use(encoder)) { 289 drm_encoder_disable(encoder); 290 /* disconnector encoder from any connector */ 291 encoder->crtc = NULL; 292 } 293 } 294 295 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) { 296 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private; 297 crtc->enabled = drm_helper_crtc_in_use(crtc); 298 if (!crtc->enabled) { 299 if (crtc_funcs->disable) 300 (*crtc_funcs->disable)(crtc); 301 else 302 (*crtc_funcs->dpms)(crtc, DRM_MODE_DPMS_OFF); 303 crtc->fb = NULL; 304 } 305 } 306 } 307 EXPORT_SYMBOL(drm_helper_disable_unused_functions); 308 309 /** 310 * drm_encoder_crtc_ok - can a given crtc drive a given encoder? 311 * @encoder: encoder to test 312 * @crtc: crtc to test 313 * 314 * Return false if @encoder can't be driven by @crtc, true otherwise. 315 */ 316 static bool drm_encoder_crtc_ok(struct drm_encoder *encoder, 317 struct drm_crtc *crtc) 318 { 319 struct drm_device *dev; 320 struct drm_crtc *tmp; 321 int crtc_mask = 1; 322 323 if (crtc == NULL) 324 printf("checking null crtc?\n"); 325 326 dev = crtc->dev; 327 328 list_for_each_entry(tmp, &dev->mode_config.crtc_list, head) { 329 if (tmp == crtc) 330 break; 331 crtc_mask <<= 1; 332 } 333 334 if (encoder->possible_crtcs & crtc_mask) 335 return true; 336 return false; 337 } 338 339 /* 340 * Check the CRTC we're going to map each output to vs. its current 341 * CRTC. If they don't match, we have to disable the output and the CRTC 342 * since the driver will have to re-route things. 343 */ 344 static void 345 drm_crtc_prepare_encoders(struct drm_device *dev) 346 { 347 struct drm_encoder_helper_funcs *encoder_funcs; 348 struct drm_encoder *encoder; 349 350 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) { 351 encoder_funcs = encoder->helper_private; 352 /* Disable unused encoders */ 353 if (encoder->crtc == NULL) 354 drm_encoder_disable(encoder); 355 /* Disable encoders whose CRTC is about to change */ 356 if (encoder_funcs->get_crtc && 357 encoder->crtc != (*encoder_funcs->get_crtc)(encoder)) 358 drm_encoder_disable(encoder); 359 } 360 } 361 362 /** 363 * drm_crtc_helper_set_mode - internal helper to set a mode 364 * @crtc: CRTC to program 365 * @mode: mode to use 366 * @x: horizontal offset into the surface 367 * @y: vertical offset into the surface 368 * @old_fb: old framebuffer, for cleanup 369 * 370 * LOCKING: 371 * Caller must hold mode config lock. 372 * 373 * Try to set @mode on @crtc. Give @crtc and its associated connectors a chance 374 * to fixup or reject the mode prior to trying to set it. This is an internal 375 * helper that drivers could e.g. use to update properties that require the 376 * entire output pipe to be disabled and re-enabled in a new configuration. For 377 * example for changing whether audio is enabled on a hdmi link or for changing 378 * panel fitter or dither attributes. It is also called by the 379 * drm_crtc_helper_set_config() helper function to drive the mode setting 380 * sequence. 381 * 382 * RETURNS: 383 * True if the mode was set successfully, or false otherwise. 384 */ 385 bool drm_crtc_helper_set_mode(struct drm_crtc *crtc, 386 struct drm_display_mode *mode, 387 int x, int y, 388 struct drm_framebuffer *old_fb) 389 { 390 struct drm_device *dev = crtc->dev; 391 struct drm_display_mode *adjusted_mode, saved_mode, saved_hwmode; 392 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private; 393 struct drm_encoder_helper_funcs *encoder_funcs; 394 int saved_x, saved_y; 395 struct drm_encoder *encoder; 396 bool ret = true; 397 398 crtc->enabled = drm_helper_crtc_in_use(crtc); 399 if (!crtc->enabled) 400 return true; 401 402 adjusted_mode = drm_mode_duplicate(dev, mode); 403 if (!adjusted_mode) 404 return false; 405 406 saved_hwmode = crtc->hwmode; 407 saved_mode = crtc->mode; 408 saved_x = crtc->x; 409 saved_y = crtc->y; 410 411 /* Update crtc values up front so the driver can rely on them for mode 412 * setting. 413 */ 414 crtc->mode = *mode; 415 crtc->x = x; 416 crtc->y = y; 417 418 /* Pass our mode to the connectors and the CRTC to give them a chance to 419 * adjust it according to limitations or connector properties, and also 420 * a chance to reject the mode entirely. 421 */ 422 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) { 423 424 if (encoder->crtc != crtc) 425 continue; 426 encoder_funcs = encoder->helper_private; 427 if (!(ret = encoder_funcs->mode_fixup(encoder, mode, 428 adjusted_mode))) { 429 DRM_DEBUG_KMS("Encoder fixup failed\n"); 430 goto done; 431 } 432 } 433 434 if (!(ret = crtc_funcs->mode_fixup(crtc, mode, adjusted_mode))) { 435 DRM_DEBUG_KMS("CRTC fixup failed\n"); 436 goto done; 437 } 438 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id); 439 440 /* Prepare the encoders and CRTCs before setting the mode. */ 441 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) { 442 443 if (encoder->crtc != crtc) 444 continue; 445 encoder_funcs = encoder->helper_private; 446 /* Disable the encoders as the first thing we do. */ 447 encoder_funcs->prepare(encoder); 448 } 449 450 drm_crtc_prepare_encoders(dev); 451 452 crtc_funcs->prepare(crtc); 453 454 /* Set up the DPLL and any encoders state that needs to adjust or depend 455 * on the DPLL. 456 */ 457 ret = !crtc_funcs->mode_set(crtc, mode, adjusted_mode, x, y, old_fb); 458 if (!ret) 459 goto done; 460 461 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) { 462 463 if (encoder->crtc != crtc) 464 continue; 465 466 DRM_DEBUG_KMS("[ENCODER:%d:%s] set [MODE:%d:%s]\n", 467 encoder->base.id, drm_get_encoder_name(encoder), 468 mode->base.id, mode->name); 469 encoder_funcs = encoder->helper_private; 470 encoder_funcs->mode_set(encoder, mode, adjusted_mode); 471 } 472 473 /* Now enable the clocks, plane, pipe, and connectors that we set up. */ 474 crtc_funcs->commit(crtc); 475 476 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) { 477 478 if (encoder->crtc != crtc) 479 continue; 480 481 encoder_funcs = encoder->helper_private; 482 encoder_funcs->commit(encoder); 483 484 } 485 486 /* Store real post-adjustment hardware mode. */ 487 crtc->hwmode = *adjusted_mode; 488 489 /* Calculate and store various constants which 490 * are later needed by vblank and swap-completion 491 * timestamping. They are derived from true hwmode. 492 */ 493 drm_calc_timestamping_constants(crtc); 494 495 /* FIXME: add subpixel order */ 496 done: 497 drm_mode_destroy(dev, adjusted_mode); 498 if (!ret) { 499 crtc->hwmode = saved_hwmode; 500 crtc->mode = saved_mode; 501 crtc->x = saved_x; 502 crtc->y = saved_y; 503 } 504 505 return ret; 506 } 507 EXPORT_SYMBOL(drm_crtc_helper_set_mode); 508 509 510 static int 511 drm_crtc_helper_disable(struct drm_crtc *crtc) 512 { 513 struct drm_device *dev = crtc->dev; 514 struct drm_connector *connector; 515 struct drm_encoder *encoder; 516 517 /* Decouple all encoders and their attached connectors from this crtc */ 518 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) { 519 if (encoder->crtc != crtc) 520 continue; 521 522 list_for_each_entry(connector, &dev->mode_config.connector_list, head) { 523 if (connector->encoder != encoder) 524 continue; 525 526 connector->encoder = NULL; 527 } 528 } 529 530 drm_helper_disable_unused_functions(dev); 531 return 0; 532 } 533 534 /** 535 * drm_crtc_helper_set_config - set a new config from userspace 536 * @set: mode set configuration 537 * 538 * LOCKING: 539 * Caller must hold mode config lock. 540 * 541 * Setup a new configuration, provided by the upper layers (either an ioctl call 542 * from userspace or internally e.g. from the fbdev suppport code) in @set, and 543 * enable it. This is the main helper functions for drivers that implement 544 * kernel mode setting with the crtc helper functions and the assorted 545 * ->prepare(), ->modeset() and ->commit() helper callbacks. 546 * 547 * RETURNS: 548 * Returns 0 on success, -ERRNO on failure. 549 */ 550 int drm_crtc_helper_set_config(struct drm_mode_set *set) 551 { 552 struct drm_device *dev; 553 struct drm_crtc *save_crtcs, *new_crtc, *crtc; 554 struct drm_encoder *save_encoders, *new_encoder, *encoder; 555 struct drm_framebuffer *old_fb = NULL; 556 bool mode_changed = false; /* if true do a full mode set */ 557 bool fb_changed = false; /* if true and !mode_changed just do a flip */ 558 struct drm_connector *save_connectors, *connector; 559 int count = 0, ro, fail = 0; 560 struct drm_crtc_helper_funcs *crtc_funcs; 561 struct drm_mode_set save_set; 562 int ret; 563 int i; 564 565 DRM_DEBUG_KMS("\n"); 566 567 if (!set) 568 return -EINVAL; 569 570 if (!set->crtc) 571 return -EINVAL; 572 573 if (!set->crtc->helper_private) 574 return -EINVAL; 575 576 crtc_funcs = set->crtc->helper_private; 577 578 if (!set->mode) 579 set->fb = NULL; 580 581 if (set->fb) { 582 DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n", 583 set->crtc->base.id, set->fb->base.id, 584 (int)set->num_connectors, set->x, set->y); 585 } else { 586 DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id); 587 return drm_crtc_helper_disable(set->crtc); 588 } 589 590 dev = set->crtc->dev; 591 592 /* Allocate space for the backup of all (non-pointer) crtc, encoder and 593 * connector data. */ 594 save_crtcs = malloc(dev->mode_config.num_crtc * 595 sizeof(struct drm_crtc), DRM_MEM_KMS, M_NOWAIT | M_ZERO); 596 if (!save_crtcs) 597 return -ENOMEM; 598 599 save_encoders = malloc(dev->mode_config.num_encoder * 600 sizeof(struct drm_encoder), DRM_MEM_KMS, M_NOWAIT | M_ZERO); 601 if (!save_encoders) { 602 free(save_crtcs, DRM_MEM_KMS); 603 return -ENOMEM; 604 } 605 606 save_connectors = malloc(dev->mode_config.num_connector * 607 sizeof(struct drm_connector), DRM_MEM_KMS, M_NOWAIT | M_ZERO); 608 if (!save_connectors) { 609 free(save_crtcs, DRM_MEM_KMS); 610 free(save_encoders, DRM_MEM_KMS); 611 return -ENOMEM; 612 } 613 614 /* Copy data. Note that driver private data is not affected. 615 * Should anything bad happen only the expected state is 616 * restored, not the drivers personal bookkeeping. 617 */ 618 count = 0; 619 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) { 620 save_crtcs[count++] = *crtc; 621 } 622 623 count = 0; 624 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) { 625 save_encoders[count++] = *encoder; 626 } 627 628 count = 0; 629 list_for_each_entry(connector, &dev->mode_config.connector_list, head) { 630 save_connectors[count++] = *connector; 631 } 632 633 save_set.crtc = set->crtc; 634 save_set.mode = &set->crtc->mode; 635 save_set.x = set->crtc->x; 636 save_set.y = set->crtc->y; 637 save_set.fb = set->crtc->fb; 638 639 /* We should be able to check here if the fb has the same properties 640 * and then just flip_or_move it */ 641 if (set->crtc->fb != set->fb) { 642 /* If we have no fb then treat it as a full mode set */ 643 if (set->crtc->fb == NULL) { 644 DRM_DEBUG_KMS("crtc has no fb, full mode set\n"); 645 mode_changed = true; 646 } else if (set->fb == NULL) { 647 mode_changed = true; 648 } else if (set->fb->depth != set->crtc->fb->depth) { 649 mode_changed = true; 650 } else if (set->fb->bits_per_pixel != 651 set->crtc->fb->bits_per_pixel) { 652 mode_changed = true; 653 } else 654 fb_changed = true; 655 } 656 657 if (set->x != set->crtc->x || set->y != set->crtc->y) 658 fb_changed = true; 659 660 if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) { 661 DRM_DEBUG_KMS("modes are different, full mode set\n"); 662 drm_mode_debug_printmodeline(&set->crtc->mode); 663 drm_mode_debug_printmodeline(set->mode); 664 mode_changed = true; 665 } 666 667 /* a) traverse passed in connector list and get encoders for them */ 668 count = 0; 669 list_for_each_entry(connector, &dev->mode_config.connector_list, head) { 670 struct drm_connector_helper_funcs *connector_funcs = 671 connector->helper_private; 672 new_encoder = connector->encoder; 673 for (ro = 0; ro < set->num_connectors; ro++) { 674 if (set->connectors[ro] == connector) { 675 new_encoder = connector_funcs->best_encoder(connector); 676 /* if we can't get an encoder for a connector 677 we are setting now - then fail */ 678 if (new_encoder == NULL) 679 /* don't break so fail path works correct */ 680 fail = 1; 681 break; 682 } 683 } 684 685 if (new_encoder != connector->encoder) { 686 DRM_DEBUG_KMS("encoder changed, full mode switch\n"); 687 mode_changed = true; 688 /* If the encoder is reused for another connector, then 689 * the appropriate crtc will be set later. 690 */ 691 if (connector->encoder) 692 connector->encoder->crtc = NULL; 693 connector->encoder = new_encoder; 694 } 695 } 696 697 if (fail) { 698 ret = -EINVAL; 699 goto fail; 700 } 701 702 count = 0; 703 list_for_each_entry(connector, &dev->mode_config.connector_list, head) { 704 if (!connector->encoder) 705 continue; 706 707 if (connector->encoder->crtc == set->crtc) 708 new_crtc = NULL; 709 else 710 new_crtc = connector->encoder->crtc; 711 712 for (ro = 0; ro < set->num_connectors; ro++) { 713 if (set->connectors[ro] == connector) 714 new_crtc = set->crtc; 715 } 716 717 /* Make sure the new CRTC will work with the encoder */ 718 if (new_crtc && 719 !drm_encoder_crtc_ok(connector->encoder, new_crtc)) { 720 ret = -EINVAL; 721 goto fail; 722 } 723 if (new_crtc != connector->encoder->crtc) { 724 DRM_DEBUG_KMS("crtc changed, full mode switch\n"); 725 mode_changed = true; 726 connector->encoder->crtc = new_crtc; 727 } 728 if (new_crtc) { 729 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n", 730 connector->base.id, drm_get_connector_name(connector), 731 new_crtc->base.id); 732 } else { 733 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n", 734 connector->base.id, drm_get_connector_name(connector)); 735 } 736 } 737 738 /* mode_set_base is not a required function */ 739 if (fb_changed && !crtc_funcs->mode_set_base) 740 mode_changed = true; 741 742 if (mode_changed) { 743 set->crtc->enabled = drm_helper_crtc_in_use(set->crtc); 744 if (set->crtc->enabled) { 745 DRM_DEBUG_KMS("attempting to set mode from" 746 " userspace\n"); 747 drm_mode_debug_printmodeline(set->mode); 748 old_fb = set->crtc->fb; 749 set->crtc->fb = set->fb; 750 if (!drm_crtc_helper_set_mode(set->crtc, set->mode, 751 set->x, set->y, 752 old_fb)) { 753 DRM_ERROR("failed to set mode on [CRTC:%d]\n", 754 set->crtc->base.id); 755 set->crtc->fb = old_fb; 756 ret = -EINVAL; 757 goto fail; 758 } 759 DRM_DEBUG_KMS("Setting connector DPMS state to on\n"); 760 for (i = 0; i < set->num_connectors; i++) { 761 DRM_DEBUG_KMS("\t[CONNECTOR:%d:%s] set DPMS on\n", set->connectors[i]->base.id, 762 drm_get_connector_name(set->connectors[i])); 763 set->connectors[i]->funcs->dpms(set->connectors[i], DRM_MODE_DPMS_ON); 764 } 765 } 766 drm_helper_disable_unused_functions(dev); 767 } else if (fb_changed) { 768 set->crtc->x = set->x; 769 set->crtc->y = set->y; 770 771 old_fb = set->crtc->fb; 772 if (set->crtc->fb != set->fb) 773 set->crtc->fb = set->fb; 774 ret = crtc_funcs->mode_set_base(set->crtc, 775 set->x, set->y, old_fb); 776 if (ret != 0) { 777 set->crtc->fb = old_fb; 778 goto fail; 779 } 780 } 781 782 free(save_connectors, DRM_MEM_KMS); 783 free(save_encoders, DRM_MEM_KMS); 784 free(save_crtcs, DRM_MEM_KMS); 785 return 0; 786 787 fail: 788 /* Restore all previous data. */ 789 count = 0; 790 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) { 791 *crtc = save_crtcs[count++]; 792 } 793 794 count = 0; 795 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) { 796 *encoder = save_encoders[count++]; 797 } 798 799 count = 0; 800 list_for_each_entry(connector, &dev->mode_config.connector_list, head) { 801 *connector = save_connectors[count++]; 802 } 803 804 /* Try to restore the config */ 805 if (mode_changed && 806 !drm_crtc_helper_set_mode(save_set.crtc, save_set.mode, save_set.x, 807 save_set.y, save_set.fb)) 808 DRM_ERROR("failed to restore config after modeset failure\n"); 809 810 free(save_connectors, DRM_MEM_KMS); 811 free(save_encoders, DRM_MEM_KMS); 812 free(save_crtcs, DRM_MEM_KMS); 813 return ret; 814 } 815 EXPORT_SYMBOL(drm_crtc_helper_set_config); 816 817 static int drm_helper_choose_encoder_dpms(struct drm_encoder *encoder) 818 { 819 int dpms = DRM_MODE_DPMS_OFF; 820 struct drm_connector *connector; 821 struct drm_device *dev = encoder->dev; 822 823 list_for_each_entry(connector, &dev->mode_config.connector_list, head) 824 if (connector->encoder == encoder) 825 if (connector->dpms < dpms) 826 dpms = connector->dpms; 827 return dpms; 828 } 829 830 static int drm_helper_choose_crtc_dpms(struct drm_crtc *crtc) 831 { 832 int dpms = DRM_MODE_DPMS_OFF; 833 struct drm_connector *connector; 834 struct drm_device *dev = crtc->dev; 835 836 list_for_each_entry(connector, &dev->mode_config.connector_list, head) 837 if (connector->encoder && connector->encoder->crtc == crtc) 838 if (connector->dpms < dpms) 839 dpms = connector->dpms; 840 return dpms; 841 } 842 843 /** 844 * drm_helper_connector_dpms() - connector dpms helper implementation 845 * @connector: affected connector 846 * @mode: DPMS mode 847 * 848 * This is the main helper function provided by the crtc helper framework for 849 * implementing the DPMS connector attribute. It computes the new desired DPMS 850 * state for all encoders and crtcs in the output mesh and calls the ->dpms() 851 * callback provided by the driver appropriately. 852 */ 853 void drm_helper_connector_dpms(struct drm_connector *connector, int mode) 854 { 855 struct drm_encoder *encoder = connector->encoder; 856 struct drm_crtc *crtc = encoder ? encoder->crtc : NULL; 857 int old_dpms; 858 859 if (mode == connector->dpms) 860 return; 861 862 old_dpms = connector->dpms; 863 connector->dpms = mode; 864 865 /* from off to on, do crtc then encoder */ 866 if (mode < old_dpms) { 867 if (crtc) { 868 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private; 869 if (crtc_funcs->dpms) 870 (*crtc_funcs->dpms) (crtc, 871 drm_helper_choose_crtc_dpms(crtc)); 872 } 873 if (encoder) { 874 struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private; 875 if (encoder_funcs->dpms) 876 (*encoder_funcs->dpms) (encoder, 877 drm_helper_choose_encoder_dpms(encoder)); 878 } 879 } 880 881 /* from on to off, do encoder then crtc */ 882 if (mode > old_dpms) { 883 if (encoder) { 884 struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private; 885 if (encoder_funcs->dpms) 886 (*encoder_funcs->dpms) (encoder, 887 drm_helper_choose_encoder_dpms(encoder)); 888 } 889 if (crtc) { 890 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private; 891 if (crtc_funcs->dpms) 892 (*crtc_funcs->dpms) (crtc, 893 drm_helper_choose_crtc_dpms(crtc)); 894 } 895 } 896 897 return; 898 } 899 EXPORT_SYMBOL(drm_helper_connector_dpms); 900 901 int drm_helper_mode_fill_fb_struct(struct drm_framebuffer *fb, 902 struct drm_mode_fb_cmd2 *mode_cmd) 903 { 904 int i; 905 906 fb->width = mode_cmd->width; 907 fb->height = mode_cmd->height; 908 for (i = 0; i < 4; i++) { 909 fb->pitches[i] = mode_cmd->pitches[i]; 910 fb->offsets[i] = mode_cmd->offsets[i]; 911 } 912 drm_fb_get_bpp_depth(mode_cmd->pixel_format, &fb->depth, 913 &fb->bits_per_pixel); 914 fb->pixel_format = mode_cmd->pixel_format; 915 916 return 0; 917 } 918 EXPORT_SYMBOL(drm_helper_mode_fill_fb_struct); 919 920 int drm_helper_resume_force_mode(struct drm_device *dev) 921 { 922 struct drm_crtc *crtc; 923 struct drm_encoder *encoder; 924 struct drm_encoder_helper_funcs *encoder_funcs; 925 struct drm_crtc_helper_funcs *crtc_funcs; 926 int ret; 927 928 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) { 929 930 if (!crtc->enabled) 931 continue; 932 933 ret = drm_crtc_helper_set_mode(crtc, &crtc->mode, 934 crtc->x, crtc->y, crtc->fb); 935 936 if (ret == false) 937 DRM_ERROR("failed to set mode on crtc %p\n", crtc); 938 939 /* Turn off outputs that were already powered off */ 940 if (drm_helper_choose_crtc_dpms(crtc)) { 941 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) { 942 943 if(encoder->crtc != crtc) 944 continue; 945 946 encoder_funcs = encoder->helper_private; 947 if (encoder_funcs->dpms) 948 (*encoder_funcs->dpms) (encoder, 949 drm_helper_choose_encoder_dpms(encoder)); 950 } 951 952 crtc_funcs = crtc->helper_private; 953 if (crtc_funcs->dpms) 954 (*crtc_funcs->dpms) (crtc, 955 drm_helper_choose_crtc_dpms(crtc)); 956 } 957 } 958 /* disable the unused connectors while restoring the modesetting */ 959 drm_helper_disable_unused_functions(dev); 960 return 0; 961 } 962 EXPORT_SYMBOL(drm_helper_resume_force_mode); 963 964 void drm_kms_helper_hotplug_event(struct drm_device *dev) 965 { 966 /* send a uevent + call fbdev */ 967 #ifdef FREEBSD_NOTYET 968 drm_sysfs_hotplug_event(dev); 969 #endif /* FREEBSD_NOTYET */ 970 if (dev->mode_config.funcs->output_poll_changed) 971 dev->mode_config.funcs->output_poll_changed(dev); 972 } 973 EXPORT_SYMBOL(drm_kms_helper_hotplug_event); 974 975 #define DRM_OUTPUT_POLL_PERIOD (10*HZ) 976 static void output_poll_execute(void *ctx, int pending) 977 { 978 struct drm_device *dev = ctx; 979 struct drm_connector *connector; 980 enum drm_connector_status old_status; 981 bool repoll = false, changed = false; 982 983 if (!drm_kms_helper_poll) 984 return; 985 986 sx_xlock(&dev->mode_config.mutex); 987 list_for_each_entry(connector, &dev->mode_config.connector_list, head) { 988 989 /* Ignore forced connectors. */ 990 if (connector->force) 991 continue; 992 993 /* Ignore HPD capable connectors and connectors where we don't 994 * want any hotplug detection at all for polling. */ 995 if (!connector->polled || connector->polled == DRM_CONNECTOR_POLL_HPD) 996 continue; 997 998 repoll = true; 999 1000 old_status = connector->status; 1001 /* if we are connected and don't want to poll for disconnect 1002 skip it */ 1003 if (old_status == connector_status_connected && 1004 !(connector->polled & DRM_CONNECTOR_POLL_DISCONNECT)) 1005 continue; 1006 1007 connector->status = connector->funcs->detect(connector, false); 1008 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] status updated from %d to %d\n", 1009 connector->base.id, 1010 drm_get_connector_name(connector), 1011 old_status, connector->status); 1012 if (old_status != connector->status) 1013 changed = true; 1014 } 1015 1016 sx_xunlock(&dev->mode_config.mutex); 1017 1018 if (changed) 1019 drm_kms_helper_hotplug_event(dev); 1020 1021 if (repoll) 1022 taskqueue_enqueue_timeout(taskqueue_thread, 1023 &dev->mode_config.output_poll_work, 1024 DRM_OUTPUT_POLL_PERIOD); 1025 } 1026 1027 void drm_kms_helper_poll_disable(struct drm_device *dev) 1028 { 1029 if (!dev->mode_config.poll_enabled) 1030 return; 1031 taskqueue_cancel_timeout(taskqueue_thread, 1032 &dev->mode_config.output_poll_work, NULL); 1033 } 1034 EXPORT_SYMBOL(drm_kms_helper_poll_disable); 1035 1036 void drm_kms_helper_poll_enable(struct drm_device *dev) 1037 { 1038 bool poll = false; 1039 struct drm_connector *connector; 1040 1041 if (!dev->mode_config.poll_enabled || !drm_kms_helper_poll) 1042 return; 1043 1044 list_for_each_entry(connector, &dev->mode_config.connector_list, head) { 1045 if (connector->polled & (DRM_CONNECTOR_POLL_CONNECT | 1046 DRM_CONNECTOR_POLL_DISCONNECT)) 1047 poll = true; 1048 } 1049 1050 if (poll) 1051 taskqueue_enqueue_timeout(taskqueue_thread, 1052 &dev->mode_config.output_poll_work, DRM_OUTPUT_POLL_PERIOD); 1053 } 1054 EXPORT_SYMBOL(drm_kms_helper_poll_enable); 1055 1056 void drm_kms_helper_poll_init(struct drm_device *dev) 1057 { 1058 TIMEOUT_TASK_INIT(taskqueue_thread, &dev->mode_config.output_poll_work, 1059 0, output_poll_execute, dev); 1060 dev->mode_config.poll_enabled = true; 1061 1062 drm_kms_helper_poll_enable(dev); 1063 } 1064 EXPORT_SYMBOL(drm_kms_helper_poll_init); 1065 1066 void drm_kms_helper_poll_fini(struct drm_device *dev) 1067 { 1068 drm_kms_helper_poll_disable(dev); 1069 } 1070 EXPORT_SYMBOL(drm_kms_helper_poll_fini); 1071 1072 void drm_helper_hpd_irq_event(struct drm_device *dev) 1073 { 1074 struct drm_connector *connector; 1075 enum drm_connector_status old_status; 1076 bool changed = false; 1077 1078 if (!dev->mode_config.poll_enabled) 1079 return; 1080 1081 sx_xlock(&dev->mode_config.mutex); 1082 list_for_each_entry(connector, &dev->mode_config.connector_list, head) { 1083 1084 /* Only handle HPD capable connectors. */ 1085 if (!(connector->polled & DRM_CONNECTOR_POLL_HPD)) 1086 continue; 1087 1088 old_status = connector->status; 1089 1090 connector->status = connector->funcs->detect(connector, false); 1091 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] status updated from %d to %d\n", 1092 connector->base.id, 1093 drm_get_connector_name(connector), 1094 old_status, connector->status); 1095 if (old_status != connector->status) 1096 changed = true; 1097 } 1098 1099 sx_xunlock(&dev->mode_config.mutex); 1100 1101 if (changed) 1102 drm_kms_helper_hotplug_event(dev); 1103 } 1104 EXPORT_SYMBOL(drm_helper_hpd_irq_event); 1105