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