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 <linux/kernel.h> 33 #include <linux/export.h> 34 #include <linux/moduleparam.h> 35 36 #include <drm/drmP.h> 37 #include <drm/drm_atomic.h> 38 #include <drm/drm_crtc.h> 39 #include <uapi_drm/drm_fourcc.h> 40 #include <drm/drm_crtc_helper.h> 41 #include <drm/drm_fb_helper.h> 42 #include <drm/drm_plane_helper.h> 43 #include <drm/drm_atomic_helper.h> 44 #include <drm/drm_edid.h> 45 46 /** 47 * DOC: overview 48 * 49 * The CRTC modeset helper library provides a default set_config implementation 50 * in drm_crtc_helper_set_config(). Plus a few other convenience functions using 51 * the same callbacks which drivers can use to e.g. restore the modeset 52 * configuration on resume with drm_helper_resume_force_mode(). 53 * 54 * Note that this helper library doesn't track the current power state of CRTCs 55 * and encoders. It can call callbacks like ->dpms() even though the hardware is 56 * already in the desired state. This deficiency has been fixed in the atomic 57 * helpers. 58 * 59 * The driver callbacks are mostly compatible with the atomic modeset helpers, 60 * except for the handling of the primary plane: Atomic helpers require that the 61 * primary plane is implemented as a real standalone plane and not directly tied 62 * to the CRTC state. For easier transition this library provides functions to 63 * implement the old semantics required by the CRTC helpers using the new plane 64 * and atomic helper callbacks. 65 * 66 * Drivers are strongly urged to convert to the atomic helpers (by way of first 67 * converting to the plane helpers). New drivers must not use these functions 68 * but need to implement the atomic interface instead, potentially using the 69 * atomic helpers for that. 70 * 71 * These legacy modeset helpers use the same function table structures as 72 * all other modesetting helpers. See the documentation for struct 73 * &drm_crtc_helper_funcs, struct &drm_encoder_helper_funcs and struct 74 * &drm_connector_helper_funcs. 75 */ 76 MODULE_AUTHOR("David Airlie, Jesse Barnes"); 77 MODULE_DESCRIPTION("DRM KMS helper"); 78 79 /** 80 * drm_helper_move_panel_connectors_to_head() - move panels to the front in the 81 * connector list 82 * @dev: drm device to operate on 83 * 84 * Some userspace presumes that the first connected connector is the main 85 * display, where it's supposed to display e.g. the login screen. For 86 * laptops, this should be the main panel. Use this function to sort all 87 * (eDP/LVDS) panels to the front of the connector list, instead of 88 * painstakingly trying to initialize them in the right order. 89 */ 90 void drm_helper_move_panel_connectors_to_head(struct drm_device *dev) 91 { 92 struct drm_connector *connector, *tmp; 93 struct list_head panel_list; 94 95 INIT_LIST_HEAD(&panel_list); 96 97 list_for_each_entry_safe(connector, tmp, 98 &dev->mode_config.connector_list, head) { 99 if (connector->connector_type == DRM_MODE_CONNECTOR_LVDS || 100 connector->connector_type == DRM_MODE_CONNECTOR_eDP) 101 list_move_tail(&connector->head, &panel_list); 102 } 103 104 list_splice(&panel_list, &dev->mode_config.connector_list); 105 } 106 EXPORT_SYMBOL(drm_helper_move_panel_connectors_to_head); 107 108 /** 109 * drm_helper_encoder_in_use - check if a given encoder is in use 110 * @encoder: encoder to check 111 * 112 * Checks whether @encoder is with the current mode setting output configuration 113 * in use by any connector. This doesn't mean that it is actually enabled since 114 * the DPMS state is tracked separately. 115 * 116 * Returns: 117 * True if @encoder is used, false otherwise. 118 */ 119 bool drm_helper_encoder_in_use(struct drm_encoder *encoder) 120 { 121 struct drm_connector *connector; 122 struct drm_device *dev = encoder->dev; 123 124 /* 125 * We can expect this mutex to be locked if we are not panicking. 126 * Locking is currently fubar in the panic handler. 127 */ 128 #if 0 129 if (!oops_in_progress) { 130 WARN_ON(!mutex_is_locked(&dev->mode_config.mutex)); 131 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex)); 132 } 133 #endif 134 135 drm_for_each_connector(connector, dev) 136 if (connector->encoder == encoder) 137 return true; 138 return false; 139 } 140 EXPORT_SYMBOL(drm_helper_encoder_in_use); 141 142 /** 143 * drm_helper_crtc_in_use - check if a given CRTC is in a mode_config 144 * @crtc: CRTC to check 145 * 146 * Checks whether @crtc is with the current mode setting output configuration 147 * in use by any connector. This doesn't mean that it is actually enabled since 148 * the DPMS state is tracked separately. 149 * 150 * Returns: 151 * True if @crtc is used, false otherwise. 152 */ 153 bool drm_helper_crtc_in_use(struct drm_crtc *crtc) 154 { 155 struct drm_encoder *encoder; 156 struct drm_device *dev = crtc->dev; 157 158 /* 159 * We can expect this mutex to be locked if we are not panicking. 160 * Locking is currently fubar in the panic handler. 161 */ 162 #if 0 163 if (!oops_in_progress) 164 #endif 165 WARN_ON(!mutex_is_locked(&dev->mode_config.mutex)); 166 167 drm_for_each_encoder(encoder, dev) 168 if (encoder->crtc == crtc && drm_helper_encoder_in_use(encoder)) 169 return true; 170 return false; 171 } 172 EXPORT_SYMBOL(drm_helper_crtc_in_use); 173 174 static void 175 drm_encoder_disable(struct drm_encoder *encoder) 176 { 177 const struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private; 178 179 drm_bridge_disable(encoder->bridge); 180 181 if (encoder_funcs->disable) 182 (*encoder_funcs->disable)(encoder); 183 else 184 (*encoder_funcs->dpms)(encoder, DRM_MODE_DPMS_OFF); 185 186 drm_bridge_post_disable(encoder->bridge); 187 } 188 189 static void __drm_helper_disable_unused_functions(struct drm_device *dev) 190 { 191 struct drm_encoder *encoder; 192 struct drm_crtc *crtc; 193 194 drm_warn_on_modeset_not_all_locked(dev); 195 196 drm_for_each_encoder(encoder, dev) { 197 if (!drm_helper_encoder_in_use(encoder)) { 198 drm_encoder_disable(encoder); 199 /* disconnect encoder from any connector */ 200 encoder->crtc = NULL; 201 } 202 } 203 204 drm_for_each_crtc(crtc, dev) { 205 const struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private; 206 crtc->enabled = drm_helper_crtc_in_use(crtc); 207 if (!crtc->enabled) { 208 if (crtc_funcs->disable) 209 (*crtc_funcs->disable)(crtc); 210 else 211 (*crtc_funcs->dpms)(crtc, DRM_MODE_DPMS_OFF); 212 crtc->primary->fb = NULL; 213 } 214 } 215 } 216 217 /** 218 * drm_helper_disable_unused_functions - disable unused objects 219 * @dev: DRM device 220 * 221 * This function walks through the entire mode setting configuration of @dev. It 222 * will remove any CRTC links of unused encoders and encoder links of 223 * disconnected connectors. Then it will disable all unused encoders and CRTCs 224 * either by calling their disable callback if available or by calling their 225 * dpms callback with DRM_MODE_DPMS_OFF. 226 */ 227 void drm_helper_disable_unused_functions(struct drm_device *dev) 228 { 229 drm_modeset_lock_all(dev); 230 __drm_helper_disable_unused_functions(dev); 231 drm_modeset_unlock_all(dev); 232 } 233 EXPORT_SYMBOL(drm_helper_disable_unused_functions); 234 235 /* 236 * Check the CRTC we're going to map each output to vs. its current 237 * CRTC. If they don't match, we have to disable the output and the CRTC 238 * since the driver will have to re-route things. 239 */ 240 static void 241 drm_crtc_prepare_encoders(struct drm_device *dev) 242 { 243 const struct drm_encoder_helper_funcs *encoder_funcs; 244 struct drm_encoder *encoder; 245 246 drm_for_each_encoder(encoder, dev) { 247 encoder_funcs = encoder->helper_private; 248 /* Disable unused encoders */ 249 if (encoder->crtc == NULL) 250 drm_encoder_disable(encoder); 251 /* Disable encoders whose CRTC is about to change */ 252 if (encoder_funcs->get_crtc && 253 encoder->crtc != (*encoder_funcs->get_crtc)(encoder)) 254 drm_encoder_disable(encoder); 255 } 256 } 257 258 /** 259 * drm_crtc_helper_set_mode - internal helper to set a mode 260 * @crtc: CRTC to program 261 * @mode: mode to use 262 * @x: horizontal offset into the surface 263 * @y: vertical offset into the surface 264 * @old_fb: old framebuffer, for cleanup 265 * 266 * Try to set @mode on @crtc. Give @crtc and its associated connectors a chance 267 * to fixup or reject the mode prior to trying to set it. This is an internal 268 * helper that drivers could e.g. use to update properties that require the 269 * entire output pipe to be disabled and re-enabled in a new configuration. For 270 * example for changing whether audio is enabled on a hdmi link or for changing 271 * panel fitter or dither attributes. It is also called by the 272 * drm_crtc_helper_set_config() helper function to drive the mode setting 273 * sequence. 274 * 275 * Returns: 276 * True if the mode was set successfully, false otherwise. 277 */ 278 bool drm_crtc_helper_set_mode(struct drm_crtc *crtc, 279 struct drm_display_mode *mode, 280 int x, int y, 281 struct drm_framebuffer *old_fb) 282 { 283 struct drm_device *dev = crtc->dev; 284 struct drm_display_mode *adjusted_mode, saved_mode, saved_hwmode; 285 const struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private; 286 const struct drm_encoder_helper_funcs *encoder_funcs; 287 int saved_x, saved_y; 288 bool saved_enabled; 289 struct drm_encoder *encoder; 290 bool ret = true; 291 292 drm_warn_on_modeset_not_all_locked(dev); 293 294 saved_enabled = crtc->enabled; 295 crtc->enabled = drm_helper_crtc_in_use(crtc); 296 if (!crtc->enabled) 297 return true; 298 299 adjusted_mode = drm_mode_duplicate(dev, mode); 300 if (!adjusted_mode) { 301 crtc->enabled = saved_enabled; 302 return false; 303 } 304 305 saved_mode = crtc->mode; 306 saved_hwmode = crtc->hwmode; 307 saved_x = crtc->x; 308 saved_y = crtc->y; 309 310 /* Update crtc values up front so the driver can rely on them for mode 311 * setting. 312 */ 313 crtc->mode = *mode; 314 crtc->x = x; 315 crtc->y = y; 316 317 /* Pass our mode to the connectors and the CRTC to give them a chance to 318 * adjust it according to limitations or connector properties, and also 319 * a chance to reject the mode entirely. 320 */ 321 drm_for_each_encoder(encoder, dev) { 322 323 if (encoder->crtc != crtc) 324 continue; 325 326 ret = drm_bridge_mode_fixup(encoder->bridge, 327 mode, adjusted_mode); 328 if (!ret) { 329 DRM_DEBUG_KMS("Bridge fixup failed\n"); 330 goto done; 331 } 332 333 encoder_funcs = encoder->helper_private; 334 if (!(ret = encoder_funcs->mode_fixup(encoder, mode, 335 adjusted_mode))) { 336 DRM_DEBUG_KMS("Encoder fixup failed\n"); 337 goto done; 338 } 339 } 340 341 if (!(ret = crtc_funcs->mode_fixup(crtc, mode, adjusted_mode))) { 342 DRM_DEBUG_KMS("CRTC fixup failed\n"); 343 goto done; 344 } 345 DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name); 346 347 crtc->hwmode = *adjusted_mode; 348 349 /* Prepare the encoders and CRTCs before setting the mode. */ 350 drm_for_each_encoder(encoder, dev) { 351 352 if (encoder->crtc != crtc) 353 continue; 354 355 drm_bridge_disable(encoder->bridge); 356 357 encoder_funcs = encoder->helper_private; 358 /* Disable the encoders as the first thing we do. */ 359 encoder_funcs->prepare(encoder); 360 361 drm_bridge_post_disable(encoder->bridge); 362 } 363 364 drm_crtc_prepare_encoders(dev); 365 366 crtc_funcs->prepare(crtc); 367 368 /* Set up the DPLL and any encoders state that needs to adjust or depend 369 * on the DPLL. 370 */ 371 ret = !crtc_funcs->mode_set(crtc, mode, adjusted_mode, x, y, old_fb); 372 if (!ret) 373 goto done; 374 375 drm_for_each_encoder(encoder, dev) { 376 377 if (encoder->crtc != crtc) 378 continue; 379 380 DRM_DEBUG_KMS("[ENCODER:%d:%s] set [MODE:%d:%s]\n", 381 encoder->base.id, encoder->name, 382 mode->base.id, mode->name); 383 encoder_funcs = encoder->helper_private; 384 encoder_funcs->mode_set(encoder, mode, adjusted_mode); 385 386 drm_bridge_mode_set(encoder->bridge, mode, adjusted_mode); 387 } 388 389 /* Now enable the clocks, plane, pipe, and connectors that we set up. */ 390 crtc_funcs->commit(crtc); 391 392 drm_for_each_encoder(encoder, dev) { 393 394 if (encoder->crtc != crtc) 395 continue; 396 397 drm_bridge_pre_enable(encoder->bridge); 398 399 encoder_funcs = encoder->helper_private; 400 encoder_funcs->commit(encoder); 401 402 drm_bridge_enable(encoder->bridge); 403 } 404 405 /* Calculate and store various constants which 406 * are later needed by vblank and swap-completion 407 * timestamping. They are derived from true hwmode. 408 */ 409 drm_calc_timestamping_constants(crtc, &crtc->hwmode); 410 411 /* FIXME: add subpixel order */ 412 done: 413 drm_mode_destroy(dev, adjusted_mode); 414 if (!ret) { 415 crtc->enabled = saved_enabled; 416 crtc->mode = saved_mode; 417 crtc->hwmode = saved_hwmode; 418 crtc->x = saved_x; 419 crtc->y = saved_y; 420 } 421 422 return ret; 423 } 424 EXPORT_SYMBOL(drm_crtc_helper_set_mode); 425 426 static void 427 drm_crtc_helper_disable(struct drm_crtc *crtc) 428 { 429 struct drm_device *dev = crtc->dev; 430 struct drm_connector *connector; 431 struct drm_encoder *encoder; 432 433 /* Decouple all encoders and their attached connectors from this crtc */ 434 drm_for_each_encoder(encoder, dev) { 435 if (encoder->crtc != crtc) 436 continue; 437 438 drm_for_each_connector(connector, dev) { 439 if (connector->encoder != encoder) 440 continue; 441 442 connector->encoder = NULL; 443 444 /* 445 * drm_helper_disable_unused_functions() ought to be 446 * doing this, but since we've decoupled the encoder 447 * from the connector above, the required connection 448 * between them is henceforth no longer available. 449 */ 450 connector->dpms = DRM_MODE_DPMS_OFF; 451 } 452 } 453 454 __drm_helper_disable_unused_functions(dev); 455 } 456 457 /** 458 * drm_crtc_helper_set_config - set a new config from userspace 459 * @set: mode set configuration 460 * 461 * The drm_crtc_helper_set_config() helper function implements the set_config 462 * callback of struct &drm_crtc_funcs for drivers using the legacy CRTC helpers. 463 * 464 * It first tries to locate the best encoder for each connector by calling the 465 * connector ->best_encoder() (struct &drm_connector_helper_funcs) helper 466 * operation. 467 * 468 * After locating the appropriate encoders, the helper function will call the 469 * mode_fixup encoder and CRTC helper operations to adjust the requested mode, 470 * or reject it completely in which case an error will be returned to the 471 * application. If the new configuration after mode adjustment is identical to 472 * the current configuration the helper function will return without performing 473 * any other operation. 474 * 475 * If the adjusted mode is identical to the current mode but changes to the 476 * frame buffer need to be applied, the drm_crtc_helper_set_config() function 477 * will call the CRTC ->mode_set_base() (struct &drm_crtc_helper_funcs) helper 478 * operation. 479 * 480 * If the adjusted mode differs from the current mode, or if the 481 * ->mode_set_base() helper operation is not provided, the helper function 482 * performs a full mode set sequence by calling the ->prepare(), ->mode_set() 483 * and ->commit() CRTC and encoder helper operations, in that order. 484 * Alternatively it can also use the dpms and disable helper operations. For 485 * details see struct &drm_crtc_helper_funcs and struct 486 * &drm_encoder_helper_funcs. 487 * 488 * This function is deprecated. New drivers must implement atomic modeset 489 * support, for which this function is unsuitable. Instead drivers should use 490 * drm_atomic_helper_set_config(). 491 * 492 * Returns: 493 * Returns 0 on success, negative errno numbers on failure. 494 */ 495 int drm_crtc_helper_set_config(struct drm_mode_set *set) 496 { 497 struct drm_device *dev; 498 struct drm_crtc *new_crtc; 499 struct drm_encoder *save_encoders, *new_encoder, *encoder; 500 bool mode_changed = false; /* if true do a full mode set */ 501 bool fb_changed = false; /* if true and !mode_changed just do a flip */ 502 struct drm_connector *save_connectors, *connector; 503 int count = 0, ro, fail = 0; 504 const struct drm_crtc_helper_funcs *crtc_funcs; 505 struct drm_mode_set save_set; 506 int ret; 507 int i; 508 509 DRM_DEBUG_KMS("\n"); 510 511 BUG_ON(!set); 512 BUG_ON(!set->crtc); 513 BUG_ON(!set->crtc->helper_private); 514 515 /* Enforce sane interface api - has been abused by the fb helper. */ 516 BUG_ON(!set->mode && set->fb); 517 BUG_ON(set->fb && set->num_connectors == 0); 518 519 crtc_funcs = set->crtc->helper_private; 520 521 if (!set->mode) 522 set->fb = NULL; 523 524 if (set->fb) { 525 DRM_DEBUG_KMS("[CRTC:%d:%s] [FB:%d] #connectors=%d (x y) (%i %i)\n", 526 set->crtc->base.id, set->crtc->name, 527 set->fb->base.id, 528 (int)set->num_connectors, set->x, set->y); 529 } else { 530 DRM_DEBUG_KMS("[CRTC:%d:%s] [NOFB]\n", 531 set->crtc->base.id, set->crtc->name); 532 drm_crtc_helper_disable(set->crtc); 533 return 0; 534 } 535 536 dev = set->crtc->dev; 537 538 drm_warn_on_modeset_not_all_locked(dev); 539 540 /* 541 * Allocate space for the backup of all (non-pointer) encoder and 542 * connector data. 543 */ 544 save_encoders = kzalloc(dev->mode_config.num_encoder * 545 sizeof(struct drm_encoder), GFP_KERNEL); 546 if (!save_encoders) 547 return -ENOMEM; 548 549 save_connectors = kzalloc(dev->mode_config.num_connector * 550 sizeof(struct drm_connector), GFP_KERNEL); 551 if (!save_connectors) { 552 kfree(save_encoders); 553 return -ENOMEM; 554 } 555 556 /* 557 * Copy data. Note that driver private data is not affected. 558 * Should anything bad happen only the expected state is 559 * restored, not the drivers personal bookkeeping. 560 */ 561 count = 0; 562 drm_for_each_encoder(encoder, dev) { 563 save_encoders[count++] = *encoder; 564 } 565 566 count = 0; 567 drm_for_each_connector(connector, dev) { 568 save_connectors[count++] = *connector; 569 } 570 571 save_set.crtc = set->crtc; 572 save_set.mode = &set->crtc->mode; 573 save_set.x = set->crtc->x; 574 save_set.y = set->crtc->y; 575 save_set.fb = set->crtc->primary->fb; 576 577 /* We should be able to check here if the fb has the same properties 578 * and then just flip_or_move it */ 579 if (set->crtc->primary->fb != set->fb) { 580 /* If we have no fb then treat it as a full mode set */ 581 if (set->crtc->primary->fb == NULL) { 582 DRM_DEBUG_KMS("crtc has no fb, full mode set\n"); 583 mode_changed = true; 584 } else if (set->fb == NULL) { 585 mode_changed = true; 586 } else if (set->fb->pixel_format != 587 set->crtc->primary->fb->pixel_format) { 588 mode_changed = true; 589 } else 590 fb_changed = true; 591 } 592 593 if (set->x != set->crtc->x || set->y != set->crtc->y) 594 fb_changed = true; 595 596 if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) { 597 DRM_DEBUG_KMS("modes are different, full mode set\n"); 598 drm_mode_debug_printmodeline(&set->crtc->mode); 599 drm_mode_debug_printmodeline(set->mode); 600 mode_changed = true; 601 } 602 603 /* a) traverse passed in connector list and get encoders for them */ 604 count = 0; 605 drm_for_each_connector(connector, dev) { 606 const struct drm_connector_helper_funcs *connector_funcs = 607 connector->helper_private; 608 new_encoder = connector->encoder; 609 for (ro = 0; ro < set->num_connectors; ro++) { 610 if (set->connectors[ro] == connector) { 611 new_encoder = connector_funcs->best_encoder(connector); 612 /* if we can't get an encoder for a connector 613 we are setting now - then fail */ 614 if (new_encoder == NULL) 615 /* don't break so fail path works correct */ 616 fail = 1; 617 618 if (connector->dpms != DRM_MODE_DPMS_ON) { 619 DRM_DEBUG_KMS("connector dpms not on, full mode switch\n"); 620 mode_changed = true; 621 } 622 623 break; 624 } 625 } 626 627 if (new_encoder != connector->encoder) { 628 DRM_DEBUG_KMS("encoder changed, full mode switch\n"); 629 mode_changed = true; 630 /* If the encoder is reused for another connector, then 631 * the appropriate crtc will be set later. 632 */ 633 if (connector->encoder) 634 connector->encoder->crtc = NULL; 635 connector->encoder = new_encoder; 636 } 637 } 638 639 if (fail) { 640 ret = -EINVAL; 641 goto fail; 642 } 643 644 count = 0; 645 drm_for_each_connector(connector, dev) { 646 if (!connector->encoder) 647 continue; 648 649 if (connector->encoder->crtc == set->crtc) 650 new_crtc = NULL; 651 else 652 new_crtc = connector->encoder->crtc; 653 654 for (ro = 0; ro < set->num_connectors; ro++) { 655 if (set->connectors[ro] == connector) 656 new_crtc = set->crtc; 657 } 658 659 /* Make sure the new CRTC will work with the encoder */ 660 if (new_crtc && 661 !drm_encoder_crtc_ok(connector->encoder, new_crtc)) { 662 ret = -EINVAL; 663 goto fail; 664 } 665 if (new_crtc != connector->encoder->crtc) { 666 DRM_DEBUG_KMS("crtc changed, full mode switch\n"); 667 mode_changed = true; 668 connector->encoder->crtc = new_crtc; 669 } 670 if (new_crtc) { 671 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d:%s]\n", 672 connector->base.id, connector->name, 673 new_crtc->base.id, new_crtc->name); 674 } else { 675 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n", 676 connector->base.id, connector->name); 677 } 678 } 679 680 /* mode_set_base is not a required function */ 681 if (fb_changed && !crtc_funcs->mode_set_base) 682 mode_changed = true; 683 684 if (mode_changed) { 685 if (drm_helper_crtc_in_use(set->crtc)) { 686 DRM_DEBUG_KMS("attempting to set mode from" 687 " userspace\n"); 688 drm_mode_debug_printmodeline(set->mode); 689 set->crtc->primary->fb = set->fb; 690 if (!drm_crtc_helper_set_mode(set->crtc, set->mode, 691 set->x, set->y, 692 save_set.fb)) { 693 DRM_ERROR("failed to set mode on [CRTC:%d:%s]\n", 694 set->crtc->base.id, set->crtc->name); 695 set->crtc->primary->fb = save_set.fb; 696 ret = -EINVAL; 697 goto fail; 698 } 699 DRM_DEBUG_KMS("Setting connector DPMS state to on\n"); 700 for (i = 0; i < set->num_connectors; i++) { 701 DRM_DEBUG_KMS("\t[CONNECTOR:%d:%s] set DPMS on\n", set->connectors[i]->base.id, 702 set->connectors[i]->name); 703 set->connectors[i]->funcs->dpms(set->connectors[i], DRM_MODE_DPMS_ON); 704 } 705 } 706 __drm_helper_disable_unused_functions(dev); 707 } else if (fb_changed) { 708 set->crtc->x = set->x; 709 set->crtc->y = set->y; 710 set->crtc->primary->fb = set->fb; 711 ret = crtc_funcs->mode_set_base(set->crtc, 712 set->x, set->y, save_set.fb); 713 if (ret != 0) { 714 set->crtc->x = save_set.x; 715 set->crtc->y = save_set.y; 716 set->crtc->primary->fb = save_set.fb; 717 goto fail; 718 } 719 } 720 721 kfree(save_connectors); 722 kfree(save_encoders); 723 return 0; 724 725 fail: 726 /* Restore all previous data. */ 727 count = 0; 728 drm_for_each_encoder(encoder, dev) { 729 *encoder = save_encoders[count++]; 730 } 731 732 count = 0; 733 drm_for_each_connector(connector, dev) { 734 *connector = save_connectors[count++]; 735 } 736 737 /* Try to restore the config */ 738 if (mode_changed && 739 !drm_crtc_helper_set_mode(save_set.crtc, save_set.mode, save_set.x, 740 save_set.y, save_set.fb)) 741 DRM_ERROR("failed to restore config after modeset failure\n"); 742 743 kfree(save_connectors); 744 kfree(save_encoders); 745 return ret; 746 } 747 EXPORT_SYMBOL(drm_crtc_helper_set_config); 748 749 static int drm_helper_choose_encoder_dpms(struct drm_encoder *encoder) 750 { 751 int dpms = DRM_MODE_DPMS_OFF; 752 struct drm_connector *connector; 753 struct drm_device *dev = encoder->dev; 754 755 drm_for_each_connector(connector, dev) 756 if (connector->encoder == encoder) 757 if (connector->dpms < dpms) 758 dpms = connector->dpms; 759 return dpms; 760 } 761 762 /* Helper which handles bridge ordering around encoder dpms */ 763 static void drm_helper_encoder_dpms(struct drm_encoder *encoder, int mode) 764 { 765 struct drm_bridge *bridge = encoder->bridge; 766 const struct drm_encoder_helper_funcs *encoder_funcs; 767 768 if (mode == DRM_MODE_DPMS_ON) 769 drm_bridge_pre_enable(bridge); 770 else 771 drm_bridge_disable(bridge); 772 773 encoder_funcs = encoder->helper_private; 774 if (encoder_funcs->dpms) 775 encoder_funcs->dpms(encoder, mode); 776 777 if (mode == DRM_MODE_DPMS_ON) 778 drm_bridge_enable(bridge); 779 else 780 drm_bridge_post_disable(bridge); 781 } 782 783 static int drm_helper_choose_crtc_dpms(struct drm_crtc *crtc) 784 { 785 int dpms = DRM_MODE_DPMS_OFF; 786 struct drm_connector *connector; 787 struct drm_device *dev = crtc->dev; 788 789 drm_for_each_connector(connector, dev) 790 if (connector->encoder && connector->encoder->crtc == crtc) 791 if (connector->dpms < dpms) 792 dpms = connector->dpms; 793 return dpms; 794 } 795 796 /** 797 * drm_helper_connector_dpms() - connector dpms helper implementation 798 * @connector: affected connector 799 * @mode: DPMS mode 800 * 801 * The drm_helper_connector_dpms() helper function implements the ->dpms() 802 * callback of struct &drm_connector_funcs for drivers using the legacy CRTC helpers. 803 * 804 * This is the main helper function provided by the CRTC helper framework for 805 * implementing the DPMS connector attribute. It computes the new desired DPMS 806 * state for all encoders and CRTCs in the output mesh and calls the ->dpms() 807 * callbacks provided by the driver in struct &drm_crtc_helper_funcs and struct 808 * &drm_encoder_helper_funcs appropriately. 809 * 810 * This function is deprecated. New drivers must implement atomic modeset 811 * support, for which this function is unsuitable. Instead drivers should use 812 * drm_atomic_helper_connector_dpms(). 813 * 814 * Returns: 815 * Always returns 0. 816 */ 817 int drm_helper_connector_dpms(struct drm_connector *connector, int mode) 818 { 819 struct drm_encoder *encoder = connector->encoder; 820 struct drm_crtc *crtc = encoder ? encoder->crtc : NULL; 821 int old_dpms, encoder_dpms = DRM_MODE_DPMS_OFF; 822 823 if (mode == connector->dpms) 824 return 0; 825 826 old_dpms = connector->dpms; 827 connector->dpms = mode; 828 829 if (encoder) 830 encoder_dpms = drm_helper_choose_encoder_dpms(encoder); 831 832 /* from off to on, do crtc then encoder */ 833 if (mode < old_dpms) { 834 if (crtc) { 835 const struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private; 836 if (crtc_funcs->dpms) 837 (*crtc_funcs->dpms) (crtc, 838 drm_helper_choose_crtc_dpms(crtc)); 839 } 840 if (encoder) 841 drm_helper_encoder_dpms(encoder, encoder_dpms); 842 } 843 844 /* from on to off, do encoder then crtc */ 845 if (mode > old_dpms) { 846 if (encoder) 847 drm_helper_encoder_dpms(encoder, encoder_dpms); 848 if (crtc) { 849 const struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private; 850 if (crtc_funcs->dpms) 851 (*crtc_funcs->dpms) (crtc, 852 drm_helper_choose_crtc_dpms(crtc)); 853 } 854 } 855 856 return 0; 857 } 858 EXPORT_SYMBOL(drm_helper_connector_dpms); 859 860 /** 861 * drm_helper_mode_fill_fb_struct - fill out framebuffer metadata 862 * @fb: drm_framebuffer object to fill out 863 * @mode_cmd: metadata from the userspace fb creation request 864 * 865 * This helper can be used in a drivers fb_create callback to pre-fill the fb's 866 * metadata fields. 867 */ 868 void drm_helper_mode_fill_fb_struct(struct drm_framebuffer *fb, 869 const struct drm_mode_fb_cmd2 *mode_cmd) 870 { 871 int i; 872 873 fb->width = mode_cmd->width; 874 fb->height = mode_cmd->height; 875 for (i = 0; i < 4; i++) { 876 fb->pitches[i] = mode_cmd->pitches[i]; 877 fb->offsets[i] = mode_cmd->offsets[i]; 878 fb->modifier[i] = mode_cmd->modifier[i]; 879 } 880 drm_fb_get_bpp_depth(mode_cmd->pixel_format, &fb->depth, 881 &fb->bits_per_pixel); 882 fb->pixel_format = mode_cmd->pixel_format; 883 fb->flags = mode_cmd->flags; 884 } 885 EXPORT_SYMBOL(drm_helper_mode_fill_fb_struct); 886 887 /** 888 * drm_helper_resume_force_mode - force-restore mode setting configuration 889 * @dev: drm_device which should be restored 890 * 891 * Drivers which use the mode setting helpers can use this function to 892 * force-restore the mode setting configuration e.g. on resume or when something 893 * else might have trampled over the hw state (like some overzealous old BIOSen 894 * tended to do). 895 * 896 * This helper doesn't provide a error return value since restoring the old 897 * config should never fail due to resource allocation issues since the driver 898 * has successfully set the restored configuration already. Hence this should 899 * boil down to the equivalent of a few dpms on calls, which also don't provide 900 * an error code. 901 * 902 * Drivers where simply restoring an old configuration again might fail (e.g. 903 * due to slight differences in allocating shared resources when the 904 * configuration is restored in a different order than when userspace set it up) 905 * need to use their own restore logic. 906 * 907 * This function is deprecated. New drivers should implement atomic mode- 908 * setting and use the atomic suspend/resume helpers. 909 * 910 * See also: 911 * drm_atomic_helper_suspend(), drm_atomic_helper_resume() 912 */ 913 void drm_helper_resume_force_mode(struct drm_device *dev) 914 { 915 struct drm_crtc *crtc; 916 struct drm_encoder *encoder; 917 const struct drm_crtc_helper_funcs *crtc_funcs; 918 int encoder_dpms; 919 bool ret; 920 921 drm_modeset_lock_all(dev); 922 drm_for_each_crtc(crtc, dev) { 923 924 if (!crtc->enabled) 925 continue; 926 927 ret = drm_crtc_helper_set_mode(crtc, &crtc->mode, 928 crtc->x, crtc->y, crtc->primary->fb); 929 930 /* Restoring the old config should never fail! */ 931 if (ret == false) 932 DRM_ERROR("failed to set mode on crtc %p\n", crtc); 933 934 /* Turn off outputs that were already powered off */ 935 if (drm_helper_choose_crtc_dpms(crtc)) { 936 drm_for_each_encoder(encoder, dev) { 937 938 if(encoder->crtc != crtc) 939 continue; 940 941 encoder_dpms = drm_helper_choose_encoder_dpms( 942 encoder); 943 944 drm_helper_encoder_dpms(encoder, encoder_dpms); 945 } 946 947 crtc_funcs = crtc->helper_private; 948 if (crtc_funcs->dpms) 949 (*crtc_funcs->dpms) (crtc, 950 drm_helper_choose_crtc_dpms(crtc)); 951 } 952 } 953 954 /* disable the unused connectors while restoring the modesetting */ 955 __drm_helper_disable_unused_functions(dev); 956 drm_modeset_unlock_all(dev); 957 } 958 EXPORT_SYMBOL(drm_helper_resume_force_mode); 959 960 /** 961 * drm_helper_crtc_mode_set - mode_set implementation for atomic plane helpers 962 * @crtc: DRM CRTC 963 * @mode: DRM display mode which userspace requested 964 * @adjusted_mode: DRM display mode adjusted by ->mode_fixup callbacks 965 * @x: x offset of the CRTC scanout area on the underlying framebuffer 966 * @y: y offset of the CRTC scanout area on the underlying framebuffer 967 * @old_fb: previous framebuffer 968 * 969 * This function implements a callback useable as the ->mode_set callback 970 * required by the CRTC helpers. Besides the atomic plane helper functions for 971 * the primary plane the driver must also provide the ->mode_set_nofb callback 972 * to set up the CRTC. 973 * 974 * This is a transitional helper useful for converting drivers to the atomic 975 * interfaces. 976 */ 977 int drm_helper_crtc_mode_set(struct drm_crtc *crtc, struct drm_display_mode *mode, 978 struct drm_display_mode *adjusted_mode, int x, int y, 979 struct drm_framebuffer *old_fb) 980 { 981 struct drm_crtc_state *crtc_state; 982 const struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private; 983 int ret; 984 985 if (crtc->funcs->atomic_duplicate_state) 986 crtc_state = crtc->funcs->atomic_duplicate_state(crtc); 987 else { 988 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL); 989 if (!crtc_state) 990 return -ENOMEM; 991 if (crtc->state) 992 __drm_atomic_helper_crtc_duplicate_state(crtc, crtc_state); 993 else 994 crtc_state->crtc = crtc; 995 } 996 997 crtc_state->planes_changed = true; 998 crtc_state->mode_changed = true; 999 ret = drm_atomic_set_mode_for_crtc(crtc_state, mode); 1000 if (ret) 1001 goto out; 1002 drm_mode_copy(&crtc_state->adjusted_mode, adjusted_mode); 1003 1004 if (crtc_funcs->atomic_check) { 1005 ret = crtc_funcs->atomic_check(crtc, crtc_state); 1006 if (ret) 1007 goto out; 1008 } 1009 1010 swap(crtc->state, crtc_state); 1011 1012 crtc_funcs->mode_set_nofb(crtc); 1013 1014 ret = drm_helper_crtc_mode_set_base(crtc, x, y, old_fb); 1015 1016 out: 1017 if (crtc->funcs->atomic_destroy_state) 1018 crtc->funcs->atomic_destroy_state(crtc, crtc_state); 1019 else { 1020 __drm_atomic_helper_crtc_destroy_state(crtc, crtc_state); 1021 kfree(crtc_state); 1022 } 1023 1024 return ret; 1025 } 1026 EXPORT_SYMBOL(drm_helper_crtc_mode_set); 1027 1028 /** 1029 * drm_helper_crtc_mode_set_base - mode_set_base implementation for atomic plane helpers 1030 * @crtc: DRM CRTC 1031 * @x: x offset of the CRTC scanout area on the underlying framebuffer 1032 * @y: y offset of the CRTC scanout area on the underlying framebuffer 1033 * @old_fb: previous framebuffer 1034 * 1035 * This function implements a callback useable as the ->mode_set_base used 1036 * required by the CRTC helpers. The driver must provide the atomic plane helper 1037 * functions for the primary plane. 1038 * 1039 * This is a transitional helper useful for converting drivers to the atomic 1040 * interfaces. 1041 */ 1042 int drm_helper_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y, 1043 struct drm_framebuffer *old_fb) 1044 { 1045 struct drm_plane_state *plane_state; 1046 struct drm_plane *plane = crtc->primary; 1047 1048 if (plane->funcs->atomic_duplicate_state) 1049 plane_state = plane->funcs->atomic_duplicate_state(plane); 1050 else if (plane->state) 1051 plane_state = drm_atomic_helper_plane_duplicate_state(plane); 1052 else 1053 plane_state = kzalloc(sizeof(*plane_state), GFP_KERNEL); 1054 if (!plane_state) 1055 return -ENOMEM; 1056 plane_state->plane = plane; 1057 1058 plane_state->crtc = crtc; 1059 drm_atomic_set_fb_for_plane(plane_state, crtc->primary->fb); 1060 plane_state->crtc_x = 0; 1061 plane_state->crtc_y = 0; 1062 plane_state->crtc_h = crtc->mode.vdisplay; 1063 plane_state->crtc_w = crtc->mode.hdisplay; 1064 plane_state->src_x = x << 16; 1065 plane_state->src_y = y << 16; 1066 plane_state->src_h = crtc->mode.vdisplay << 16; 1067 plane_state->src_w = crtc->mode.hdisplay << 16; 1068 1069 return drm_plane_helper_commit(plane, plane_state, old_fb); 1070 } 1071 EXPORT_SYMBOL(drm_helper_crtc_mode_set_base); 1072