1 /* 2 * Copyright (C) 2014 Red Hat 3 * Copyright (C) 2014 Intel Corp. 4 * Copyright (c) 2020-2021, The Linux Foundation. All rights reserved. 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining a 7 * copy of this software and associated documentation files (the "Software"), 8 * to deal in the Software without restriction, including without limitation 9 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 * and/or sell copies of the Software, and to permit persons to whom the 11 * Software is furnished to do so, subject to the following conditions: 12 * 13 * The above copyright notice and this permission notice shall be included in 14 * all copies or substantial portions of the Software. 15 * 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 19 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR 20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 * OTHER DEALINGS IN THE SOFTWARE. 23 * 24 * Authors: 25 * Rob Clark <robdclark@gmail.com> 26 * Daniel Vetter <daniel.vetter@ffwll.ch> 27 */ 28 29 #include <linux/export.h> 30 #include <linux/sync_file.h> 31 32 #include <drm/drm_atomic.h> 33 #include <drm/drm_atomic_uapi.h> 34 #include <drm/drm_blend.h> 35 #include <drm/drm_bridge.h> 36 #include <drm/drm_debugfs.h> 37 #include <drm/drm_device.h> 38 #include <drm/drm_drv.h> 39 #include <drm/drm_file.h> 40 #include <drm/drm_fourcc.h> 41 #include <drm/drm_framebuffer.h> 42 #include <drm/drm_mode.h> 43 #include <drm/drm_print.h> 44 #include <drm/drm_writeback.h> 45 #include <drm/drm_colorop.h> 46 47 #include "drm_crtc_internal.h" 48 #include "drm_internal.h" 49 50 void __drm_crtc_commit_free(struct kref *kref) 51 { 52 struct drm_crtc_commit *commit = 53 container_of(kref, struct drm_crtc_commit, ref); 54 55 kfree(commit); 56 } 57 EXPORT_SYMBOL(__drm_crtc_commit_free); 58 59 /** 60 * drm_crtc_commit_wait - Waits for a commit to complete 61 * @commit: &drm_crtc_commit to wait for 62 * 63 * Waits for a given &drm_crtc_commit to be programmed into the 64 * hardware and flipped to. 65 * 66 * Returns: 67 * 0 on success, a negative error code otherwise. 68 */ 69 int drm_crtc_commit_wait(struct drm_crtc_commit *commit) 70 { 71 unsigned long timeout = 10 * HZ; 72 int ret; 73 74 if (!commit) 75 return 0; 76 77 ret = wait_for_completion_timeout(&commit->hw_done, timeout); 78 if (!ret) { 79 drm_err(commit->crtc->dev, "hw_done timed out\n"); 80 return -ETIMEDOUT; 81 } 82 83 /* 84 * Currently no support for overwriting flips, hence 85 * stall for previous one to execute completely. 86 */ 87 ret = wait_for_completion_timeout(&commit->flip_done, timeout); 88 if (!ret) { 89 drm_err(commit->crtc->dev, "flip_done timed out\n"); 90 return -ETIMEDOUT; 91 } 92 93 return 0; 94 } 95 EXPORT_SYMBOL(drm_crtc_commit_wait); 96 97 /** 98 * drm_atomic_state_default_release - 99 * release memory initialized by drm_atomic_state_init 100 * @state: atomic state 101 * 102 * Free all the memory allocated by drm_atomic_state_init. 103 * This should only be used by drivers which are still subclassing 104 * &drm_atomic_state and haven't switched to &drm_private_state yet. 105 */ 106 void drm_atomic_state_default_release(struct drm_atomic_state *state) 107 { 108 kfree(state->connectors); 109 kfree(state->crtcs); 110 kfree(state->planes); 111 kfree(state->colorops); 112 kfree(state->private_objs); 113 } 114 EXPORT_SYMBOL(drm_atomic_state_default_release); 115 116 /** 117 * drm_atomic_state_init - init new atomic state 118 * @dev: DRM device 119 * @state: atomic state 120 * 121 * Default implementation for filling in a new atomic state. 122 * This should only be used by drivers which are still subclassing 123 * &drm_atomic_state and haven't switched to &drm_private_state yet. 124 */ 125 int 126 drm_atomic_state_init(struct drm_device *dev, struct drm_atomic_state *state) 127 { 128 kref_init(&state->ref); 129 130 /* TODO legacy paths should maybe do a better job about 131 * setting this appropriately? 132 */ 133 state->allow_modeset = true; 134 135 state->crtcs = kzalloc_objs(*state->crtcs, dev->mode_config.num_crtc); 136 if (!state->crtcs) 137 goto fail; 138 state->planes = kzalloc_objs(*state->planes, 139 dev->mode_config.num_total_plane); 140 if (!state->planes) 141 goto fail; 142 state->colorops = kzalloc_objs(*state->colorops, 143 dev->mode_config.num_colorop); 144 if (!state->colorops) 145 goto fail; 146 147 /* 148 * Because drm_atomic_state can be committed asynchronously we need our 149 * own reference and cannot rely on the on implied by drm_file in the 150 * ioctl call. 151 */ 152 drm_dev_get(dev); 153 state->dev = dev; 154 155 drm_dbg_atomic(dev, "Allocated atomic state %p\n", state); 156 157 return 0; 158 fail: 159 drm_atomic_state_default_release(state); 160 return -ENOMEM; 161 } 162 EXPORT_SYMBOL(drm_atomic_state_init); 163 164 /** 165 * drm_atomic_state_alloc - allocate atomic state 166 * @dev: DRM device 167 * 168 * This allocates an empty atomic state to track updates. 169 */ 170 struct drm_atomic_state * 171 drm_atomic_state_alloc(struct drm_device *dev) 172 { 173 struct drm_mode_config *config = &dev->mode_config; 174 175 if (!config->funcs->atomic_state_alloc) { 176 struct drm_atomic_state *state; 177 178 state = kzalloc_obj(*state); 179 if (!state) 180 return NULL; 181 if (drm_atomic_state_init(dev, state) < 0) { 182 kfree(state); 183 return NULL; 184 } 185 return state; 186 } 187 188 return config->funcs->atomic_state_alloc(dev); 189 } 190 EXPORT_SYMBOL(drm_atomic_state_alloc); 191 192 /** 193 * drm_atomic_state_default_clear - clear base atomic state 194 * @state: atomic state 195 * 196 * Default implementation for clearing atomic state. 197 * This should only be used by drivers which are still subclassing 198 * &drm_atomic_state and haven't switched to &drm_private_state yet. 199 */ 200 void drm_atomic_state_default_clear(struct drm_atomic_state *state) 201 { 202 struct drm_device *dev = state->dev; 203 struct drm_mode_config *config = &dev->mode_config; 204 int i; 205 206 drm_dbg_atomic(dev, "Clearing atomic state %p\n", state); 207 208 state->checked = false; 209 210 for (i = 0; i < state->num_connector; i++) { 211 struct drm_connector *connector = state->connectors[i].ptr; 212 213 if (!connector) 214 continue; 215 216 connector->funcs->atomic_destroy_state(connector, 217 state->connectors[i].state_to_destroy); 218 state->connectors[i].ptr = NULL; 219 state->connectors[i].state_to_destroy = NULL; 220 state->connectors[i].old_state = NULL; 221 state->connectors[i].new_state = NULL; 222 drm_connector_put(connector); 223 } 224 225 for (i = 0; i < config->num_crtc; i++) { 226 struct drm_crtc *crtc = state->crtcs[i].ptr; 227 228 if (!crtc) 229 continue; 230 231 crtc->funcs->atomic_destroy_state(crtc, 232 state->crtcs[i].state_to_destroy); 233 234 state->crtcs[i].ptr = NULL; 235 state->crtcs[i].state_to_destroy = NULL; 236 state->crtcs[i].old_state = NULL; 237 state->crtcs[i].new_state = NULL; 238 239 if (state->crtcs[i].commit) { 240 drm_crtc_commit_put(state->crtcs[i].commit); 241 state->crtcs[i].commit = NULL; 242 } 243 } 244 245 for (i = 0; i < config->num_total_plane; i++) { 246 struct drm_plane *plane = state->planes[i].ptr; 247 248 if (!plane) 249 continue; 250 251 plane->funcs->atomic_destroy_state(plane, 252 state->planes[i].state_to_destroy); 253 state->planes[i].ptr = NULL; 254 state->planes[i].state_to_destroy = NULL; 255 state->planes[i].old_state = NULL; 256 state->planes[i].new_state = NULL; 257 } 258 259 for (i = 0; i < config->num_colorop; i++) { 260 struct drm_colorop *colorop = state->colorops[i].ptr; 261 262 if (!colorop) 263 continue; 264 265 drm_colorop_atomic_destroy_state(colorop, 266 state->colorops[i].state); 267 state->colorops[i].ptr = NULL; 268 state->colorops[i].state = NULL; 269 state->colorops[i].old_state = NULL; 270 state->colorops[i].new_state = NULL; 271 } 272 273 for (i = 0; i < state->num_private_objs; i++) { 274 struct drm_private_obj *obj = state->private_objs[i].ptr; 275 276 obj->funcs->atomic_destroy_state(obj, 277 state->private_objs[i].state_to_destroy); 278 state->private_objs[i].ptr = NULL; 279 state->private_objs[i].state_to_destroy = NULL; 280 state->private_objs[i].old_state = NULL; 281 state->private_objs[i].new_state = NULL; 282 } 283 state->num_private_objs = 0; 284 285 if (state->fake_commit) { 286 drm_crtc_commit_put(state->fake_commit); 287 state->fake_commit = NULL; 288 } 289 } 290 EXPORT_SYMBOL(drm_atomic_state_default_clear); 291 292 /** 293 * drm_atomic_state_clear - clear state object 294 * @state: atomic state 295 * 296 * When the w/w mutex algorithm detects a deadlock we need to back off and drop 297 * all locks. So someone else could sneak in and change the current modeset 298 * configuration. Which means that all the state assembled in @state is no 299 * longer an atomic update to the current state, but to some arbitrary earlier 300 * state. Which could break assumptions the driver's 301 * &drm_mode_config_funcs.atomic_check likely relies on. 302 * 303 * Hence we must clear all cached state and completely start over, using this 304 * function. 305 */ 306 void drm_atomic_state_clear(struct drm_atomic_state *state) 307 { 308 struct drm_device *dev = state->dev; 309 struct drm_mode_config *config = &dev->mode_config; 310 311 if (config->funcs->atomic_state_clear) 312 config->funcs->atomic_state_clear(state); 313 else 314 drm_atomic_state_default_clear(state); 315 } 316 EXPORT_SYMBOL(drm_atomic_state_clear); 317 318 /** 319 * __drm_atomic_state_free - free all memory for an atomic state 320 * @ref: This atomic state to deallocate 321 * 322 * This frees all memory associated with an atomic state, including all the 323 * per-object state for planes, CRTCs and connectors. 324 */ 325 void __drm_atomic_state_free(struct kref *ref) 326 { 327 struct drm_atomic_state *state = container_of(ref, typeof(*state), ref); 328 struct drm_device *dev = state->dev; 329 struct drm_mode_config *config = &dev->mode_config; 330 331 drm_atomic_state_clear(state); 332 333 drm_dbg_atomic(state->dev, "Freeing atomic state %p\n", state); 334 335 if (config->funcs->atomic_state_free) { 336 config->funcs->atomic_state_free(state); 337 } else { 338 drm_atomic_state_default_release(state); 339 kfree(state); 340 } 341 342 drm_dev_put(dev); 343 } 344 EXPORT_SYMBOL(__drm_atomic_state_free); 345 346 /** 347 * drm_atomic_get_crtc_state - get CRTC state 348 * @state: global atomic state object 349 * @crtc: CRTC to get state object for 350 * 351 * This function returns the CRTC state for the given CRTC, allocating it if 352 * needed. It will also grab the relevant CRTC lock to make sure that the state 353 * is consistent. 354 * 355 * WARNING: Drivers may only add new CRTC states to a @state if 356 * drm_atomic_state.allow_modeset is set, or if it's a driver-internal commit 357 * not created by userspace through an IOCTL call. 358 * 359 * Returns: 360 * Either the allocated state or the error code encoded into the pointer. When 361 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the 362 * entire atomic sequence must be restarted. All other errors are fatal. 363 */ 364 struct drm_crtc_state * 365 drm_atomic_get_crtc_state(struct drm_atomic_state *state, 366 struct drm_crtc *crtc) 367 { 368 int ret, index = drm_crtc_index(crtc); 369 struct drm_crtc_state *crtc_state; 370 371 WARN_ON(!state->acquire_ctx); 372 drm_WARN_ON(state->dev, state->checked); 373 374 crtc_state = drm_atomic_get_new_crtc_state(state, crtc); 375 if (crtc_state) 376 return crtc_state; 377 378 ret = drm_modeset_lock(&crtc->mutex, state->acquire_ctx); 379 if (ret) 380 return ERR_PTR(ret); 381 382 crtc_state = crtc->funcs->atomic_duplicate_state(crtc); 383 if (!crtc_state) 384 return ERR_PTR(-ENOMEM); 385 386 state->crtcs[index].state_to_destroy = crtc_state; 387 state->crtcs[index].old_state = crtc->state; 388 state->crtcs[index].new_state = crtc_state; 389 state->crtcs[index].ptr = crtc; 390 crtc_state->state = state; 391 392 drm_dbg_atomic(state->dev, "Added [CRTC:%d:%s] %p state to %p\n", 393 crtc->base.id, crtc->name, crtc_state, state); 394 395 return crtc_state; 396 } 397 EXPORT_SYMBOL(drm_atomic_get_crtc_state); 398 399 static int drm_atomic_crtc_check(const struct drm_crtc_state *old_crtc_state, 400 const struct drm_crtc_state *new_crtc_state) 401 { 402 struct drm_crtc *crtc = new_crtc_state->crtc; 403 404 /* NOTE: we explicitly don't enforce constraints such as primary 405 * layer covering entire screen, since that is something we want 406 * to allow (on hw that supports it). For hw that does not, it 407 * should be checked in driver's crtc->atomic_check() vfunc. 408 * 409 * TODO: Add generic modeset state checks once we support those. 410 */ 411 412 if (new_crtc_state->active && !new_crtc_state->enable) { 413 drm_dbg_atomic(crtc->dev, 414 "[CRTC:%d:%s] active without enabled\n", 415 crtc->base.id, crtc->name); 416 return -EINVAL; 417 } 418 419 /* The state->enable vs. state->mode_blob checks can be WARN_ON, 420 * as this is a kernel-internal detail that userspace should never 421 * be able to trigger. 422 */ 423 if (drm_core_check_feature(crtc->dev, DRIVER_ATOMIC) && 424 WARN_ON(new_crtc_state->enable && !new_crtc_state->mode_blob)) { 425 drm_dbg_atomic(crtc->dev, 426 "[CRTC:%d:%s] enabled without mode blob\n", 427 crtc->base.id, crtc->name); 428 return -EINVAL; 429 } 430 431 if (drm_core_check_feature(crtc->dev, DRIVER_ATOMIC) && 432 WARN_ON(!new_crtc_state->enable && new_crtc_state->mode_blob)) { 433 drm_dbg_atomic(crtc->dev, 434 "[CRTC:%d:%s] disabled with mode blob\n", 435 crtc->base.id, crtc->name); 436 return -EINVAL; 437 } 438 439 /* 440 * Reject event generation for when a CRTC is off and stays off. 441 * It wouldn't be hard to implement this, but userspace has a track 442 * record of happily burning through 100% cpu (or worse, crash) when the 443 * display pipe is suspended. To avoid all that fun just reject updates 444 * that ask for events since likely that indicates a bug in the 445 * compositor's drawing loop. This is consistent with the vblank IOCTL 446 * and legacy page_flip IOCTL which also reject service on a disabled 447 * pipe. 448 */ 449 if (new_crtc_state->event && 450 !new_crtc_state->active && !old_crtc_state->active) { 451 drm_dbg_atomic(crtc->dev, 452 "[CRTC:%d:%s] requesting event but off\n", 453 crtc->base.id, crtc->name); 454 return -EINVAL; 455 } 456 457 return 0; 458 } 459 460 static void drm_atomic_crtc_print_state(struct drm_printer *p, 461 const struct drm_crtc_state *state) 462 { 463 struct drm_crtc *crtc = state->crtc; 464 465 drm_printf(p, "crtc[%u]: %s\n", crtc->base.id, crtc->name); 466 drm_printf(p, "\tenable=%d\n", state->enable); 467 drm_printf(p, "\tactive=%d\n", state->active); 468 drm_printf(p, "\tself_refresh_active=%d\n", state->self_refresh_active); 469 drm_printf(p, "\tplanes_changed=%d\n", state->planes_changed); 470 drm_printf(p, "\tmode_changed=%d\n", state->mode_changed); 471 drm_printf(p, "\tactive_changed=%d\n", state->active_changed); 472 drm_printf(p, "\tconnectors_changed=%d\n", state->connectors_changed); 473 drm_printf(p, "\tcolor_mgmt_changed=%d\n", state->color_mgmt_changed); 474 drm_printf(p, "\tplane_mask=%x\n", state->plane_mask); 475 drm_printf(p, "\tconnector_mask=%x\n", state->connector_mask); 476 drm_printf(p, "\tencoder_mask=%x\n", state->encoder_mask); 477 drm_printf(p, "\tmode: " DRM_MODE_FMT "\n", DRM_MODE_ARG(&state->mode)); 478 479 if (crtc->funcs->atomic_print_state) 480 crtc->funcs->atomic_print_state(p, state); 481 } 482 483 static int drm_atomic_connector_check(struct drm_connector *connector, 484 struct drm_connector_state *state) 485 { 486 struct drm_crtc_state *crtc_state; 487 struct drm_writeback_job *writeback_job = state->writeback_job; 488 const struct drm_display_info *info = &connector->display_info; 489 490 state->max_bpc = info->bpc ? info->bpc : 8; 491 if (connector->max_bpc_property) 492 state->max_bpc = min(state->max_bpc, state->max_requested_bpc); 493 494 if ((connector->connector_type != DRM_MODE_CONNECTOR_WRITEBACK) || !writeback_job) 495 return 0; 496 497 if (writeback_job->fb && !state->crtc) { 498 drm_dbg_atomic(connector->dev, 499 "[CONNECTOR:%d:%s] framebuffer without CRTC\n", 500 connector->base.id, connector->name); 501 return -EINVAL; 502 } 503 504 if (state->crtc) 505 crtc_state = drm_atomic_get_new_crtc_state(state->state, 506 state->crtc); 507 508 if (writeback_job->fb && !crtc_state->active) { 509 drm_dbg_atomic(connector->dev, 510 "[CONNECTOR:%d:%s] has framebuffer, but [CRTC:%d] is off\n", 511 connector->base.id, connector->name, 512 state->crtc->base.id); 513 return -EINVAL; 514 } 515 516 if (!writeback_job->fb) { 517 if (writeback_job->out_fence) { 518 drm_dbg_atomic(connector->dev, 519 "[CONNECTOR:%d:%s] requesting out-fence without framebuffer\n", 520 connector->base.id, connector->name); 521 return -EINVAL; 522 } 523 524 drm_writeback_cleanup_job(writeback_job); 525 state->writeback_job = NULL; 526 } 527 528 return 0; 529 } 530 531 /** 532 * drm_atomic_get_plane_state - get plane state 533 * @state: global atomic state object 534 * @plane: plane to get state object for 535 * 536 * This function returns the plane state for the given plane, allocating it if 537 * needed. It will also grab the relevant plane lock to make sure that the state 538 * is consistent. 539 * 540 * Returns: 541 * Either the allocated state or the error code encoded into the pointer. When 542 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the 543 * entire atomic sequence must be restarted. All other errors are fatal. 544 */ 545 struct drm_plane_state * 546 drm_atomic_get_plane_state(struct drm_atomic_state *state, 547 struct drm_plane *plane) 548 { 549 int ret, index = drm_plane_index(plane); 550 struct drm_plane_state *plane_state; 551 552 WARN_ON(!state->acquire_ctx); 553 drm_WARN_ON(state->dev, state->checked); 554 555 /* the legacy pointers should never be set */ 556 WARN_ON(plane->fb); 557 WARN_ON(plane->old_fb); 558 WARN_ON(plane->crtc); 559 560 plane_state = drm_atomic_get_new_plane_state(state, plane); 561 if (plane_state) 562 return plane_state; 563 564 ret = drm_modeset_lock(&plane->mutex, state->acquire_ctx); 565 if (ret) 566 return ERR_PTR(ret); 567 568 plane_state = plane->funcs->atomic_duplicate_state(plane); 569 if (!plane_state) 570 return ERR_PTR(-ENOMEM); 571 572 state->planes[index].state_to_destroy = plane_state; 573 state->planes[index].ptr = plane; 574 state->planes[index].old_state = plane->state; 575 state->planes[index].new_state = plane_state; 576 plane_state->state = state; 577 578 drm_dbg_atomic(plane->dev, "Added [PLANE:%d:%s] %p state to %p\n", 579 plane->base.id, plane->name, plane_state, state); 580 581 if (plane_state->crtc) { 582 struct drm_crtc_state *crtc_state; 583 584 crtc_state = drm_atomic_get_crtc_state(state, 585 plane_state->crtc); 586 if (IS_ERR(crtc_state)) 587 return ERR_CAST(crtc_state); 588 } 589 590 return plane_state; 591 } 592 EXPORT_SYMBOL(drm_atomic_get_plane_state); 593 594 /** 595 * drm_atomic_get_colorop_state - get colorop state 596 * @state: global atomic state object 597 * @colorop: colorop to get state object for 598 * 599 * This function returns the colorop state for the given colorop, allocating it 600 * if needed. It will also grab the relevant plane lock to make sure that the 601 * state is consistent. 602 * 603 * Returns: 604 * 605 * Either the allocated state or the error code encoded into the pointer. When 606 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the 607 * entire atomic sequence must be restarted. All other errors are fatal. 608 */ 609 struct drm_colorop_state * 610 drm_atomic_get_colorop_state(struct drm_atomic_state *state, 611 struct drm_colorop *colorop) 612 { 613 int ret, index = drm_colorop_index(colorop); 614 struct drm_colorop_state *colorop_state; 615 616 WARN_ON(!state->acquire_ctx); 617 618 colorop_state = drm_atomic_get_new_colorop_state(state, colorop); 619 if (colorop_state) 620 return colorop_state; 621 622 ret = drm_modeset_lock(&colorop->plane->mutex, state->acquire_ctx); 623 if (ret) 624 return ERR_PTR(ret); 625 626 colorop_state = drm_atomic_helper_colorop_duplicate_state(colorop); 627 if (!colorop_state) 628 return ERR_PTR(-ENOMEM); 629 630 state->colorops[index].state = colorop_state; 631 state->colorops[index].ptr = colorop; 632 state->colorops[index].old_state = colorop->state; 633 state->colorops[index].new_state = colorop_state; 634 colorop_state->state = state; 635 636 drm_dbg_atomic(colorop->dev, "Added [COLOROP:%d:%d] %p state to %p\n", 637 colorop->base.id, colorop->type, colorop_state, state); 638 639 return colorop_state; 640 } 641 EXPORT_SYMBOL(drm_atomic_get_colorop_state); 642 643 /** 644 * drm_atomic_get_old_colorop_state - get colorop state, if it exists 645 * @state: global atomic state object 646 * @colorop: colorop to grab 647 * 648 * This function returns the old colorop state for the given colorop, or 649 * NULL if the colorop is not part of the global atomic state. 650 */ 651 struct drm_colorop_state * 652 drm_atomic_get_old_colorop_state(struct drm_atomic_state *state, 653 struct drm_colorop *colorop) 654 { 655 return state->colorops[drm_colorop_index(colorop)].old_state; 656 } 657 EXPORT_SYMBOL(drm_atomic_get_old_colorop_state); 658 659 /** 660 * drm_atomic_get_new_colorop_state - get colorop state, if it exists 661 * @state: global atomic state object 662 * @colorop: colorop to grab 663 * 664 * This function returns the new colorop state for the given colorop, or 665 * NULL if the colorop is not part of the global atomic state. 666 */ 667 struct drm_colorop_state * 668 drm_atomic_get_new_colorop_state(struct drm_atomic_state *state, 669 struct drm_colorop *colorop) 670 { 671 return state->colorops[drm_colorop_index(colorop)].new_state; 672 } 673 EXPORT_SYMBOL(drm_atomic_get_new_colorop_state); 674 675 static bool 676 plane_switching_crtc(const struct drm_plane_state *old_plane_state, 677 const struct drm_plane_state *new_plane_state) 678 { 679 if (!old_plane_state->crtc || !new_plane_state->crtc) 680 return false; 681 682 if (old_plane_state->crtc == new_plane_state->crtc) 683 return false; 684 685 /* This could be refined, but currently there's no helper or driver code 686 * to implement direct switching of active planes nor userspace to take 687 * advantage of more direct plane switching without the intermediate 688 * full OFF state. 689 */ 690 return true; 691 } 692 693 /** 694 * drm_atomic_plane_check - check plane state 695 * @old_plane_state: old plane state to check 696 * @new_plane_state: new plane state to check 697 * 698 * Provides core sanity checks for plane state. 699 * 700 * RETURNS: 701 * Zero on success, error code on failure 702 */ 703 static int drm_atomic_plane_check(const struct drm_plane_state *old_plane_state, 704 const struct drm_plane_state *new_plane_state) 705 { 706 struct drm_plane *plane = new_plane_state->plane; 707 struct drm_crtc *crtc = new_plane_state->crtc; 708 const struct drm_framebuffer *fb = new_plane_state->fb; 709 unsigned int fb_width, fb_height; 710 struct drm_mode_rect *clips; 711 uint32_t num_clips; 712 713 /* either *both* CRTC and FB must be set, or neither */ 714 if (crtc && !fb) { 715 drm_dbg_atomic(plane->dev, "[PLANE:%d:%s] CRTC set but no FB\n", 716 plane->base.id, plane->name); 717 return -EINVAL; 718 } else if (fb && !crtc) { 719 drm_dbg_atomic(plane->dev, "[PLANE:%d:%s] FB set but no CRTC\n", 720 plane->base.id, plane->name); 721 return -EINVAL; 722 } 723 724 /* if disabled, we don't care about the rest of the state: */ 725 if (!crtc) 726 return 0; 727 728 /* Check whether this plane is usable on this CRTC */ 729 if (!(plane->possible_crtcs & drm_crtc_mask(crtc))) { 730 drm_dbg_atomic(plane->dev, 731 "Invalid [CRTC:%d:%s] for [PLANE:%d:%s]\n", 732 crtc->base.id, crtc->name, 733 plane->base.id, plane->name); 734 return -EINVAL; 735 } 736 737 /* Check whether this plane supports the fb pixel format. */ 738 if (!drm_plane_has_format(plane, fb->format->format, fb->modifier)) { 739 drm_dbg_atomic(plane->dev, 740 "[PLANE:%d:%s] invalid pixel format %p4cc, modifier 0x%llx\n", 741 plane->base.id, plane->name, 742 &fb->format->format, fb->modifier); 743 return -EINVAL; 744 } 745 746 /* Give drivers some help against integer overflows */ 747 if (new_plane_state->crtc_w > INT_MAX || 748 new_plane_state->crtc_x > INT_MAX - (int32_t) new_plane_state->crtc_w || 749 new_plane_state->crtc_h > INT_MAX || 750 new_plane_state->crtc_y > INT_MAX - (int32_t) new_plane_state->crtc_h) { 751 drm_dbg_atomic(plane->dev, 752 "[PLANE:%d:%s] invalid CRTC coordinates %ux%u+%d+%d\n", 753 plane->base.id, plane->name, 754 new_plane_state->crtc_w, new_plane_state->crtc_h, 755 new_plane_state->crtc_x, new_plane_state->crtc_y); 756 return -ERANGE; 757 } 758 759 fb_width = fb->width << 16; 760 fb_height = fb->height << 16; 761 762 /* Make sure source coordinates are inside the fb. */ 763 if (new_plane_state->src_w > fb_width || 764 new_plane_state->src_x > fb_width - new_plane_state->src_w || 765 new_plane_state->src_h > fb_height || 766 new_plane_state->src_y > fb_height - new_plane_state->src_h) { 767 drm_dbg_atomic(plane->dev, 768 "[PLANE:%d:%s] invalid source coordinates " 769 "%u.%06ux%u.%06u+%u.%06u+%u.%06u (fb %ux%u)\n", 770 plane->base.id, plane->name, 771 new_plane_state->src_w >> 16, 772 ((new_plane_state->src_w & 0xffff) * 15625) >> 10, 773 new_plane_state->src_h >> 16, 774 ((new_plane_state->src_h & 0xffff) * 15625) >> 10, 775 new_plane_state->src_x >> 16, 776 ((new_plane_state->src_x & 0xffff) * 15625) >> 10, 777 new_plane_state->src_y >> 16, 778 ((new_plane_state->src_y & 0xffff) * 15625) >> 10, 779 fb->width, fb->height); 780 return -ENOSPC; 781 } 782 783 clips = __drm_plane_get_damage_clips(new_plane_state); 784 num_clips = drm_plane_get_damage_clips_count(new_plane_state); 785 786 /* Make sure damage clips are valid and inside the fb. */ 787 while (num_clips > 0) { 788 if (clips->x1 >= clips->x2 || 789 clips->y1 >= clips->y2 || 790 clips->x1 < 0 || 791 clips->y1 < 0 || 792 clips->x2 > fb_width || 793 clips->y2 > fb_height) { 794 drm_dbg_atomic(plane->dev, 795 "[PLANE:%d:%s] invalid damage clip %d %d %d %d\n", 796 plane->base.id, plane->name, clips->x1, 797 clips->y1, clips->x2, clips->y2); 798 return -EINVAL; 799 } 800 clips++; 801 num_clips--; 802 } 803 804 if (plane_switching_crtc(old_plane_state, new_plane_state)) { 805 drm_dbg_atomic(plane->dev, 806 "[PLANE:%d:%s] switching CRTC directly\n", 807 plane->base.id, plane->name); 808 return -EINVAL; 809 } 810 811 return 0; 812 } 813 814 static void drm_atomic_colorop_print_state(struct drm_printer *p, 815 const struct drm_colorop_state *state) 816 { 817 struct drm_colorop *colorop = state->colorop; 818 819 drm_printf(p, "colorop[%u]:\n", colorop->base.id); 820 drm_printf(p, "\ttype=%s\n", drm_get_colorop_type_name(colorop->type)); 821 if (colorop->bypass_property) 822 drm_printf(p, "\tbypass=%u\n", state->bypass); 823 824 switch (colorop->type) { 825 case DRM_COLOROP_1D_CURVE: 826 drm_printf(p, "\tcurve_1d_type=%s\n", 827 drm_get_colorop_curve_1d_type_name(state->curve_1d_type)); 828 break; 829 case DRM_COLOROP_1D_LUT: 830 drm_printf(p, "\tsize=%d\n", colorop->size); 831 drm_printf(p, "\tinterpolation=%s\n", 832 drm_get_colorop_lut1d_interpolation_name(colorop->lut1d_interpolation)); 833 drm_printf(p, "\tdata blob id=%d\n", state->data ? state->data->base.id : 0); 834 break; 835 case DRM_COLOROP_CTM_3X4: 836 drm_printf(p, "\tdata blob id=%d\n", state->data ? state->data->base.id : 0); 837 break; 838 case DRM_COLOROP_MULTIPLIER: 839 drm_printf(p, "\tmultiplier=%llu\n", state->multiplier); 840 break; 841 case DRM_COLOROP_3D_LUT: 842 drm_printf(p, "\tsize=%d\n", colorop->size); 843 drm_printf(p, "\tinterpolation=%s\n", 844 drm_get_colorop_lut3d_interpolation_name(colorop->lut3d_interpolation)); 845 drm_printf(p, "\tdata blob id=%d\n", state->data ? state->data->base.id : 0); 846 break; 847 default: 848 break; 849 } 850 851 drm_printf(p, "\tnext=%d\n", colorop->next ? colorop->next->base.id : 0); 852 } 853 854 static void drm_atomic_plane_print_state(struct drm_printer *p, 855 const struct drm_plane_state *state) 856 { 857 struct drm_plane *plane = state->plane; 858 struct drm_rect src = drm_plane_state_src(state); 859 struct drm_rect dest = drm_plane_state_dest(state); 860 861 drm_printf(p, "plane[%u]: %s\n", plane->base.id, plane->name); 862 drm_printf(p, "\tcrtc=%s\n", state->crtc ? state->crtc->name : "(null)"); 863 drm_printf(p, "\tfb=%u\n", state->fb ? state->fb->base.id : 0); 864 if (state->fb) 865 drm_framebuffer_print_info(p, 2, state->fb); 866 drm_printf(p, "\tcrtc-pos=" DRM_RECT_FMT "\n", DRM_RECT_ARG(&dest)); 867 drm_printf(p, "\tsrc-pos=" DRM_RECT_FP_FMT "\n", DRM_RECT_FP_ARG(&src)); 868 drm_printf(p, "\trotation=%x\n", state->rotation); 869 drm_printf(p, "\tnormalized-zpos=%x\n", state->normalized_zpos); 870 drm_printf(p, "\tcolor-encoding=%s\n", 871 drm_get_color_encoding_name(state->color_encoding)); 872 drm_printf(p, "\tcolor-range=%s\n", 873 drm_get_color_range_name(state->color_range)); 874 drm_printf(p, "\tcolor_mgmt_changed=%d\n", state->color_mgmt_changed); 875 drm_printf(p, "\tcolor-pipeline=%d\n", 876 state->color_pipeline ? state->color_pipeline->base.id : 0); 877 if (plane->funcs->atomic_print_state) 878 plane->funcs->atomic_print_state(p, state); 879 } 880 881 /** 882 * DOC: handling driver private state 883 * 884 * Very often the DRM objects exposed to userspace in the atomic modeset api 885 * (&drm_connector, &drm_crtc and &drm_plane) do not map neatly to the 886 * underlying hardware. Especially for any kind of shared resources (e.g. shared 887 * clocks, scaler units, bandwidth and fifo limits shared among a group of 888 * planes or CRTCs, and so on) it makes sense to model these as independent 889 * objects. Drivers then need to do similar state tracking and commit ordering for 890 * such private (since not exposed to userspace) objects as the atomic core and 891 * helpers already provide for connectors, planes and CRTCs. 892 * 893 * To make this easier on drivers the atomic core provides some support to track 894 * driver private state objects using struct &drm_private_obj, with the 895 * associated state struct &drm_private_state. 896 * 897 * Similar to userspace-exposed objects, private state structures can be 898 * acquired by calling drm_atomic_get_private_obj_state(). This also takes care 899 * of locking, hence drivers should not have a need to call drm_modeset_lock() 900 * directly. Sequence of the actual hardware state commit is not handled, 901 * drivers might need to keep track of struct drm_crtc_commit within subclassed 902 * structure of &drm_private_state as necessary, e.g. similar to 903 * &drm_plane_state.commit. See also &drm_atomic_state.fake_commit. 904 * 905 * All private state structures contained in a &drm_atomic_state update can be 906 * iterated using for_each_oldnew_private_obj_in_state(), 907 * for_each_new_private_obj_in_state() and for_each_old_private_obj_in_state(). 908 * Drivers are recommended to wrap these for each type of driver private state 909 * object they have, filtering on &drm_private_obj.funcs using for_each_if(), at 910 * least if they want to iterate over all objects of a given type. 911 * 912 * An earlier way to handle driver private state was by subclassing struct 913 * &drm_atomic_state. But since that encourages non-standard ways to implement 914 * the check/commit split atomic requires (by using e.g. "check and rollback or 915 * commit instead" of "duplicate state, check, then either commit or release 916 * duplicated state) it is deprecated in favour of using &drm_private_state. 917 */ 918 919 /** 920 * drm_atomic_private_obj_init - initialize private object 921 * @dev: DRM device this object will be attached to 922 * @obj: private object 923 * @state: initial private object state 924 * @funcs: pointer to the struct of function pointers that identify the object 925 * type 926 * 927 * Initialize the private object, which can be embedded into any 928 * driver private object that needs its own atomic state. 929 * 930 * RETURNS: 931 * Zero on success, error code on failure 932 */ 933 int drm_atomic_private_obj_init(struct drm_device *dev, 934 struct drm_private_obj *obj, 935 struct drm_private_state *state, 936 const struct drm_private_state_funcs *funcs) 937 { 938 memset(obj, 0, sizeof(*obj)); 939 940 drm_modeset_lock_init(&obj->lock); 941 942 obj->dev = dev; 943 obj->funcs = funcs; 944 list_add_tail(&obj->head, &dev->mode_config.privobj_list); 945 946 /* 947 * Not all users of drm_atomic_private_obj_init have been 948 * converted to using &drm_private_obj_funcs.atomic_create_state yet. 949 * For the time being, let's only call reset if the passed state is 950 * NULL. Otherwise, we will fallback to the previous behaviour. 951 */ 952 if (!state) { 953 state = obj->funcs->atomic_create_state(obj); 954 if (IS_ERR(state)) 955 return PTR_ERR(state); 956 957 obj->state = state; 958 } else { 959 obj->state = state; 960 state->obj = obj; 961 } 962 963 return 0; 964 } 965 EXPORT_SYMBOL(drm_atomic_private_obj_init); 966 967 /** 968 * drm_atomic_private_obj_fini - finalize private object 969 * @obj: private object 970 * 971 * Finalize the private object. 972 */ 973 void 974 drm_atomic_private_obj_fini(struct drm_private_obj *obj) 975 { 976 list_del(&obj->head); 977 obj->funcs->atomic_destroy_state(obj, obj->state); 978 drm_modeset_lock_fini(&obj->lock); 979 } 980 EXPORT_SYMBOL(drm_atomic_private_obj_fini); 981 982 /** 983 * drm_atomic_get_private_obj_state - get private object state 984 * @state: global atomic state 985 * @obj: private object to get the state for 986 * 987 * This function returns the private object state for the given private object, 988 * allocating the state if needed. It will also grab the relevant private 989 * object lock to make sure that the state is consistent. 990 * 991 * RETURNS: 992 * Either the allocated state or the error code encoded into a pointer. 993 */ 994 struct drm_private_state * 995 drm_atomic_get_private_obj_state(struct drm_atomic_state *state, 996 struct drm_private_obj *obj) 997 { 998 int index, num_objs, ret; 999 size_t size; 1000 struct __drm_private_objs_state *arr; 1001 struct drm_private_state *obj_state; 1002 1003 WARN_ON(!state->acquire_ctx); 1004 drm_WARN_ON(state->dev, state->checked); 1005 1006 obj_state = drm_atomic_get_new_private_obj_state(state, obj); 1007 if (obj_state) 1008 return obj_state; 1009 1010 ret = drm_modeset_lock(&obj->lock, state->acquire_ctx); 1011 if (ret) 1012 return ERR_PTR(ret); 1013 1014 num_objs = state->num_private_objs + 1; 1015 size = sizeof(*state->private_objs) * num_objs; 1016 arr = krealloc(state->private_objs, size, GFP_KERNEL); 1017 if (!arr) 1018 return ERR_PTR(-ENOMEM); 1019 1020 state->private_objs = arr; 1021 index = state->num_private_objs; 1022 memset(&state->private_objs[index], 0, sizeof(*state->private_objs)); 1023 1024 obj_state = obj->funcs->atomic_duplicate_state(obj); 1025 if (!obj_state) 1026 return ERR_PTR(-ENOMEM); 1027 1028 state->private_objs[index].state_to_destroy = obj_state; 1029 state->private_objs[index].old_state = obj->state; 1030 state->private_objs[index].new_state = obj_state; 1031 state->private_objs[index].ptr = obj; 1032 obj_state->state = state; 1033 1034 state->num_private_objs = num_objs; 1035 1036 drm_dbg_atomic(state->dev, 1037 "Added new private object %p state %p to %p\n", 1038 obj, obj_state, state); 1039 1040 return obj_state; 1041 } 1042 EXPORT_SYMBOL(drm_atomic_get_private_obj_state); 1043 1044 /** 1045 * drm_atomic_get_old_private_obj_state 1046 * @state: global atomic state object 1047 * @obj: private_obj to grab 1048 * 1049 * This function returns the old private object state for the given private_obj, 1050 * or NULL if the private_obj is not part of the global atomic state. 1051 */ 1052 struct drm_private_state * 1053 drm_atomic_get_old_private_obj_state(const struct drm_atomic_state *state, 1054 struct drm_private_obj *obj) 1055 { 1056 int i; 1057 1058 for (i = 0; i < state->num_private_objs; i++) 1059 if (obj == state->private_objs[i].ptr) 1060 return state->private_objs[i].old_state; 1061 1062 return NULL; 1063 } 1064 EXPORT_SYMBOL(drm_atomic_get_old_private_obj_state); 1065 1066 /** 1067 * drm_atomic_get_new_private_obj_state 1068 * @state: global atomic state object 1069 * @obj: private_obj to grab 1070 * 1071 * This function returns the new private object state for the given private_obj, 1072 * or NULL if the private_obj is not part of the global atomic state. 1073 */ 1074 struct drm_private_state * 1075 drm_atomic_get_new_private_obj_state(const struct drm_atomic_state *state, 1076 struct drm_private_obj *obj) 1077 { 1078 int i; 1079 1080 for (i = 0; i < state->num_private_objs; i++) 1081 if (obj == state->private_objs[i].ptr) 1082 return state->private_objs[i].new_state; 1083 1084 return NULL; 1085 } 1086 EXPORT_SYMBOL(drm_atomic_get_new_private_obj_state); 1087 1088 /** 1089 * drm_atomic_get_old_connector_for_encoder - Get old connector for an encoder 1090 * @state: Atomic state 1091 * @encoder: The encoder to fetch the connector state for 1092 * 1093 * This function finds and returns the connector that was connected to @encoder 1094 * as specified by the @state. 1095 * 1096 * If there is no connector in @state which previously had @encoder connected to 1097 * it, this function will return NULL. While this may seem like an invalid use 1098 * case, it is sometimes useful to differentiate commits which had no prior 1099 * connectors attached to @encoder vs ones that did (and to inspect their 1100 * state). This is especially true in enable hooks because the pipeline has 1101 * changed. 1102 * 1103 * If you don't have access to the atomic state, see 1104 * drm_atomic_get_connector_for_encoder(). 1105 * 1106 * Returns: The old connector connected to @encoder, or NULL if the encoder is 1107 * not connected. 1108 */ 1109 struct drm_connector * 1110 drm_atomic_get_old_connector_for_encoder(const struct drm_atomic_state *state, 1111 struct drm_encoder *encoder) 1112 { 1113 struct drm_connector_state *conn_state; 1114 struct drm_connector *connector; 1115 unsigned int i; 1116 1117 for_each_old_connector_in_state(state, connector, conn_state, i) { 1118 if (conn_state->best_encoder == encoder) 1119 return connector; 1120 } 1121 1122 return NULL; 1123 } 1124 EXPORT_SYMBOL(drm_atomic_get_old_connector_for_encoder); 1125 1126 /** 1127 * drm_atomic_get_new_connector_for_encoder - Get new connector for an encoder 1128 * @state: Atomic state 1129 * @encoder: The encoder to fetch the connector state for 1130 * 1131 * This function finds and returns the connector that will be connected to 1132 * @encoder as specified by the @state. 1133 * 1134 * If there is no connector in @state which will have @encoder connected to it, 1135 * this function will return NULL. While this may seem like an invalid use case, 1136 * it is sometimes useful to differentiate commits which have no connectors 1137 * attached to @encoder vs ones that do (and to inspect their state). This is 1138 * especially true in disable hooks because the pipeline will change. 1139 * 1140 * If you don't have access to the atomic state, see 1141 * drm_atomic_get_connector_for_encoder(). 1142 * 1143 * Returns: The new connector connected to @encoder, or NULL if the encoder is 1144 * not connected. 1145 */ 1146 struct drm_connector * 1147 drm_atomic_get_new_connector_for_encoder(const struct drm_atomic_state *state, 1148 struct drm_encoder *encoder) 1149 { 1150 struct drm_connector_state *conn_state; 1151 struct drm_connector *connector; 1152 unsigned int i; 1153 1154 for_each_new_connector_in_state(state, connector, conn_state, i) { 1155 if (conn_state->best_encoder == encoder) 1156 return connector; 1157 } 1158 1159 return NULL; 1160 } 1161 EXPORT_SYMBOL(drm_atomic_get_new_connector_for_encoder); 1162 1163 /** 1164 * drm_atomic_get_connector_for_encoder - Get connector currently assigned to an encoder 1165 * @encoder: The encoder to find the connector of 1166 * @ctx: Modeset locking context 1167 * 1168 * This function finds and returns the connector currently assigned to 1169 * an @encoder. 1170 * 1171 * It is similar to the drm_atomic_get_old_connector_for_encoder() and 1172 * drm_atomic_get_new_connector_for_encoder() helpers, but doesn't 1173 * require access to the atomic state. If you have access to it, prefer 1174 * using these. This helper is typically useful in situations where you 1175 * don't have access to the atomic state, like detect, link repair, 1176 * threaded interrupt handlers, or hooks from other frameworks (ALSA, 1177 * CEC, etc.). 1178 * 1179 * Returns: 1180 * The connector connected to @encoder, or an error pointer otherwise. 1181 * When the error is EDEADLK, a deadlock has been detected and the 1182 * sequence must be restarted. 1183 */ 1184 struct drm_connector * 1185 drm_atomic_get_connector_for_encoder(const struct drm_encoder *encoder, 1186 struct drm_modeset_acquire_ctx *ctx) 1187 { 1188 struct drm_connector_list_iter conn_iter; 1189 struct drm_connector *out_connector = ERR_PTR(-EINVAL); 1190 struct drm_connector *connector; 1191 struct drm_device *dev = encoder->dev; 1192 int ret; 1193 1194 ret = drm_modeset_lock(&dev->mode_config.connection_mutex, ctx); 1195 if (ret) 1196 return ERR_PTR(ret); 1197 1198 drm_connector_list_iter_begin(dev, &conn_iter); 1199 drm_for_each_connector_iter(connector, &conn_iter) { 1200 if (!connector->state) 1201 continue; 1202 1203 if (encoder == connector->state->best_encoder) { 1204 out_connector = connector; 1205 break; 1206 } 1207 } 1208 drm_connector_list_iter_end(&conn_iter); 1209 drm_modeset_unlock(&dev->mode_config.connection_mutex); 1210 1211 return out_connector; 1212 } 1213 EXPORT_SYMBOL(drm_atomic_get_connector_for_encoder); 1214 1215 1216 /** 1217 * drm_atomic_get_old_crtc_for_encoder - Get old crtc for an encoder 1218 * @state: Atomic state 1219 * @encoder: The encoder to fetch the crtc state for 1220 * 1221 * This function finds and returns the crtc that was connected to @encoder 1222 * as specified by the @state. 1223 * 1224 * Returns: The old crtc connected to @encoder, or NULL if the encoder is 1225 * not connected. 1226 */ 1227 struct drm_crtc * 1228 drm_atomic_get_old_crtc_for_encoder(struct drm_atomic_state *state, 1229 struct drm_encoder *encoder) 1230 { 1231 struct drm_connector *connector; 1232 struct drm_connector_state *conn_state; 1233 1234 connector = drm_atomic_get_old_connector_for_encoder(state, encoder); 1235 if (!connector) 1236 return NULL; 1237 1238 conn_state = drm_atomic_get_old_connector_state(state, connector); 1239 if (!conn_state) 1240 return NULL; 1241 1242 return conn_state->crtc; 1243 } 1244 EXPORT_SYMBOL(drm_atomic_get_old_crtc_for_encoder); 1245 1246 /** 1247 * drm_atomic_get_new_crtc_for_encoder - Get new crtc for an encoder 1248 * @state: Atomic state 1249 * @encoder: The encoder to fetch the crtc state for 1250 * 1251 * This function finds and returns the crtc that will be connected to @encoder 1252 * as specified by the @state. 1253 * 1254 * Returns: The new crtc connected to @encoder, or NULL if the encoder is 1255 * not connected. 1256 */ 1257 struct drm_crtc * 1258 drm_atomic_get_new_crtc_for_encoder(struct drm_atomic_state *state, 1259 struct drm_encoder *encoder) 1260 { 1261 struct drm_connector *connector; 1262 struct drm_connector_state *conn_state; 1263 1264 connector = drm_atomic_get_new_connector_for_encoder(state, encoder); 1265 if (!connector) 1266 return NULL; 1267 1268 conn_state = drm_atomic_get_new_connector_state(state, connector); 1269 if (!conn_state) 1270 return NULL; 1271 1272 return conn_state->crtc; 1273 } 1274 EXPORT_SYMBOL(drm_atomic_get_new_crtc_for_encoder); 1275 1276 /** 1277 * drm_atomic_get_connector_state - get connector state 1278 * @state: global atomic state object 1279 * @connector: connector to get state object for 1280 * 1281 * This function returns the connector state for the given connector, 1282 * allocating it if needed. It will also grab the relevant connector lock to 1283 * make sure that the state is consistent. 1284 * 1285 * Returns: 1286 * Either the allocated state or the error code encoded into the pointer. When 1287 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the 1288 * entire atomic sequence must be restarted. All other errors are fatal. 1289 */ 1290 struct drm_connector_state * 1291 drm_atomic_get_connector_state(struct drm_atomic_state *state, 1292 struct drm_connector *connector) 1293 { 1294 int ret, index; 1295 struct drm_mode_config *config = &connector->dev->mode_config; 1296 struct drm_connector_state *connector_state; 1297 1298 WARN_ON(!state->acquire_ctx); 1299 drm_WARN_ON(state->dev, state->checked); 1300 1301 ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx); 1302 if (ret) 1303 return ERR_PTR(ret); 1304 1305 index = drm_connector_index(connector); 1306 1307 if (index >= state->num_connector) { 1308 struct __drm_connnectors_state *c; 1309 int alloc = max(index + 1, config->num_connector); 1310 1311 c = krealloc_array(state->connectors, alloc, 1312 sizeof(*state->connectors), GFP_KERNEL); 1313 if (!c) 1314 return ERR_PTR(-ENOMEM); 1315 1316 state->connectors = c; 1317 memset(&state->connectors[state->num_connector], 0, 1318 sizeof(*state->connectors) * (alloc - state->num_connector)); 1319 1320 state->num_connector = alloc; 1321 } 1322 1323 connector_state = drm_atomic_get_new_connector_state(state, connector); 1324 if (connector_state) 1325 return connector_state; 1326 1327 connector_state = connector->funcs->atomic_duplicate_state(connector); 1328 if (!connector_state) 1329 return ERR_PTR(-ENOMEM); 1330 1331 drm_connector_get(connector); 1332 state->connectors[index].state_to_destroy = connector_state; 1333 state->connectors[index].old_state = connector->state; 1334 state->connectors[index].new_state = connector_state; 1335 state->connectors[index].ptr = connector; 1336 connector_state->state = state; 1337 1338 drm_dbg_atomic(connector->dev, "Added [CONNECTOR:%d:%s] %p state to %p\n", 1339 connector->base.id, connector->name, 1340 connector_state, state); 1341 1342 if (connector_state->crtc) { 1343 struct drm_crtc_state *crtc_state; 1344 1345 crtc_state = drm_atomic_get_crtc_state(state, 1346 connector_state->crtc); 1347 if (IS_ERR(crtc_state)) 1348 return ERR_CAST(crtc_state); 1349 } 1350 1351 return connector_state; 1352 } 1353 EXPORT_SYMBOL(drm_atomic_get_connector_state); 1354 1355 static void drm_atomic_connector_print_state(struct drm_printer *p, 1356 const struct drm_connector_state *state) 1357 { 1358 struct drm_connector *connector = state->connector; 1359 1360 drm_printf(p, "connector[%u]: %s\n", connector->base.id, connector->name); 1361 drm_printf(p, "\tcrtc=%s\n", state->crtc ? state->crtc->name : "(null)"); 1362 drm_printf(p, "\tself_refresh_aware=%d\n", state->self_refresh_aware); 1363 drm_printf(p, "\tinterlace_allowed=%d\n", connector->interlace_allowed); 1364 drm_printf(p, "\tycbcr_420_allowed=%d\n", connector->ycbcr_420_allowed); 1365 drm_printf(p, "\tmax_requested_bpc=%d\n", state->max_requested_bpc); 1366 drm_printf(p, "\tcolorspace=%s\n", drm_get_colorspace_name(state->colorspace)); 1367 1368 if (connector->connector_type == DRM_MODE_CONNECTOR_HDMIA || 1369 connector->connector_type == DRM_MODE_CONNECTOR_HDMIB) { 1370 drm_printf(p, "\tbroadcast_rgb=%s\n", 1371 drm_hdmi_connector_get_broadcast_rgb_name(state->hdmi.broadcast_rgb)); 1372 drm_printf(p, "\tis_limited_range=%c\n", state->hdmi.is_limited_range ? 'y' : 'n'); 1373 drm_printf(p, "\toutput_bpc=%u\n", state->hdmi.output_bpc); 1374 drm_printf(p, "\toutput_format=%s\n", 1375 drm_hdmi_connector_get_output_format_name(state->hdmi.output_format)); 1376 drm_printf(p, "\ttmds_char_rate=%llu\n", state->hdmi.tmds_char_rate); 1377 } 1378 1379 if (connector->connector_type == DRM_MODE_CONNECTOR_WRITEBACK) 1380 if (state->writeback_job && state->writeback_job->fb) 1381 drm_printf(p, "\tfb=%d\n", state->writeback_job->fb->base.id); 1382 1383 if (connector->funcs->atomic_print_state) 1384 connector->funcs->atomic_print_state(p, state); 1385 } 1386 1387 /** 1388 * drm_atomic_get_bridge_state - get bridge state 1389 * @state: global atomic state object 1390 * @bridge: bridge to get state object for 1391 * 1392 * This function returns the bridge state for the given bridge, allocating it 1393 * if needed. It will also grab the relevant bridge lock to make sure that the 1394 * state is consistent. 1395 * 1396 * Returns: 1397 * Either the allocated state or the error code encoded into the pointer. When 1398 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the 1399 * entire atomic sequence must be restarted. 1400 */ 1401 struct drm_bridge_state * 1402 drm_atomic_get_bridge_state(struct drm_atomic_state *state, 1403 struct drm_bridge *bridge) 1404 { 1405 struct drm_private_state *obj_state; 1406 1407 obj_state = drm_atomic_get_private_obj_state(state, &bridge->base); 1408 if (IS_ERR(obj_state)) 1409 return ERR_CAST(obj_state); 1410 1411 return drm_priv_to_bridge_state(obj_state); 1412 } 1413 EXPORT_SYMBOL(drm_atomic_get_bridge_state); 1414 1415 /** 1416 * drm_atomic_get_old_bridge_state - get old bridge state, if it exists 1417 * @state: global atomic state object 1418 * @bridge: bridge to grab 1419 * 1420 * This function returns the old bridge state for the given bridge, or NULL if 1421 * the bridge is not part of the global atomic state. 1422 */ 1423 struct drm_bridge_state * 1424 drm_atomic_get_old_bridge_state(const struct drm_atomic_state *state, 1425 struct drm_bridge *bridge) 1426 { 1427 struct drm_private_state *obj_state; 1428 1429 obj_state = drm_atomic_get_old_private_obj_state(state, &bridge->base); 1430 if (!obj_state) 1431 return NULL; 1432 1433 return drm_priv_to_bridge_state(obj_state); 1434 } 1435 EXPORT_SYMBOL(drm_atomic_get_old_bridge_state); 1436 1437 /** 1438 * drm_atomic_get_new_bridge_state - get new bridge state, if it exists 1439 * @state: global atomic state object 1440 * @bridge: bridge to grab 1441 * 1442 * This function returns the new bridge state for the given bridge, or NULL if 1443 * the bridge is not part of the global atomic state. 1444 */ 1445 struct drm_bridge_state * 1446 drm_atomic_get_new_bridge_state(const struct drm_atomic_state *state, 1447 struct drm_bridge *bridge) 1448 { 1449 struct drm_private_state *obj_state; 1450 1451 obj_state = drm_atomic_get_new_private_obj_state(state, &bridge->base); 1452 if (!obj_state) 1453 return NULL; 1454 1455 return drm_priv_to_bridge_state(obj_state); 1456 } 1457 EXPORT_SYMBOL(drm_atomic_get_new_bridge_state); 1458 1459 /** 1460 * drm_atomic_add_encoder_bridges - add bridges attached to an encoder 1461 * @state: atomic state 1462 * @encoder: DRM encoder 1463 * 1464 * This function adds all bridges attached to @encoder. This is needed to add 1465 * bridge states to @state and make them available when 1466 * &drm_bridge_funcs.atomic_check(), &drm_bridge_funcs.atomic_pre_enable(), 1467 * &drm_bridge_funcs.atomic_enable(), 1468 * &drm_bridge_funcs.atomic_disable_post_disable() are called. 1469 * 1470 * Returns: 1471 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK 1472 * then the w/w mutex code has detected a deadlock and the entire atomic 1473 * sequence must be restarted. All other errors are fatal. 1474 */ 1475 int 1476 drm_atomic_add_encoder_bridges(struct drm_atomic_state *state, 1477 struct drm_encoder *encoder) 1478 { 1479 struct drm_bridge_state *bridge_state; 1480 1481 if (!encoder) 1482 return 0; 1483 1484 drm_dbg_atomic(encoder->dev, 1485 "Adding all bridges for [encoder:%d:%s] to %p\n", 1486 encoder->base.id, encoder->name, state); 1487 1488 drm_for_each_bridge_in_chain_scoped(encoder, bridge) { 1489 /* Skip bridges that don't implement the atomic state hooks. */ 1490 if (!bridge->funcs->atomic_duplicate_state) 1491 continue; 1492 1493 bridge_state = drm_atomic_get_bridge_state(state, bridge); 1494 if (IS_ERR(bridge_state)) 1495 return PTR_ERR(bridge_state); 1496 } 1497 1498 return 0; 1499 } 1500 EXPORT_SYMBOL(drm_atomic_add_encoder_bridges); 1501 1502 /** 1503 * drm_atomic_add_affected_connectors - add connectors for CRTC 1504 * @state: atomic state 1505 * @crtc: DRM CRTC 1506 * 1507 * This function walks the current configuration and adds all connectors 1508 * currently using @crtc to the atomic configuration @state. Note that this 1509 * function must acquire the connection mutex. This can potentially cause 1510 * unneeded serialization if the update is just for the planes on one CRTC. Hence 1511 * drivers and helpers should only call this when really needed (e.g. when a 1512 * full modeset needs to happen due to some change). 1513 * 1514 * Returns: 1515 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK 1516 * then the w/w mutex code has detected a deadlock and the entire atomic 1517 * sequence must be restarted. All other errors are fatal. 1518 */ 1519 int 1520 drm_atomic_add_affected_connectors(struct drm_atomic_state *state, 1521 struct drm_crtc *crtc) 1522 { 1523 struct drm_mode_config *config = &state->dev->mode_config; 1524 struct drm_connector *connector; 1525 struct drm_connector_state *conn_state; 1526 struct drm_connector_list_iter conn_iter; 1527 struct drm_crtc_state *crtc_state; 1528 int ret; 1529 1530 crtc_state = drm_atomic_get_crtc_state(state, crtc); 1531 if (IS_ERR(crtc_state)) 1532 return PTR_ERR(crtc_state); 1533 1534 ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx); 1535 if (ret) 1536 return ret; 1537 1538 drm_dbg_atomic(crtc->dev, 1539 "Adding all current connectors for [CRTC:%d:%s] to %p\n", 1540 crtc->base.id, crtc->name, state); 1541 1542 /* 1543 * Changed connectors are already in @state, so only need to look 1544 * at the connector_mask in crtc_state. 1545 */ 1546 drm_connector_list_iter_begin(state->dev, &conn_iter); 1547 drm_for_each_connector_iter(connector, &conn_iter) { 1548 if (!(crtc_state->connector_mask & drm_connector_mask(connector))) 1549 continue; 1550 1551 conn_state = drm_atomic_get_connector_state(state, connector); 1552 if (IS_ERR(conn_state)) { 1553 drm_connector_list_iter_end(&conn_iter); 1554 return PTR_ERR(conn_state); 1555 } 1556 } 1557 drm_connector_list_iter_end(&conn_iter); 1558 1559 return 0; 1560 } 1561 EXPORT_SYMBOL(drm_atomic_add_affected_connectors); 1562 1563 /** 1564 * drm_atomic_add_affected_planes - add planes for CRTC 1565 * @state: atomic state 1566 * @crtc: DRM CRTC 1567 * 1568 * This function walks the current configuration and adds all planes 1569 * currently used by @crtc to the atomic configuration @state. This is useful 1570 * when an atomic commit also needs to check all currently enabled plane on 1571 * @crtc, e.g. when changing the mode. It's also useful when re-enabling a CRTC 1572 * to avoid special code to force-enable all planes. 1573 * 1574 * Since acquiring a plane state will always also acquire the w/w mutex of the 1575 * current CRTC for that plane (if there is any) adding all the plane states for 1576 * a CRTC will not reduce parallelism of atomic updates. 1577 * 1578 * Returns: 1579 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK 1580 * then the w/w mutex code has detected a deadlock and the entire atomic 1581 * sequence must be restarted. All other errors are fatal. 1582 */ 1583 int 1584 drm_atomic_add_affected_planes(struct drm_atomic_state *state, 1585 struct drm_crtc *crtc) 1586 { 1587 const struct drm_crtc_state *old_crtc_state = 1588 drm_atomic_get_old_crtc_state(state, crtc); 1589 struct drm_plane *plane; 1590 1591 WARN_ON(!drm_atomic_get_new_crtc_state(state, crtc)); 1592 1593 drm_dbg_atomic(crtc->dev, 1594 "Adding all current planes for [CRTC:%d:%s] to %p\n", 1595 crtc->base.id, crtc->name, state); 1596 1597 drm_for_each_plane_mask(plane, state->dev, old_crtc_state->plane_mask) { 1598 struct drm_plane_state *plane_state = 1599 drm_atomic_get_plane_state(state, plane); 1600 1601 if (IS_ERR(plane_state)) 1602 return PTR_ERR(plane_state); 1603 } 1604 return 0; 1605 } 1606 EXPORT_SYMBOL(drm_atomic_add_affected_planes); 1607 1608 /** 1609 * drm_atomic_add_affected_colorops - add colorops for plane 1610 * @state: atomic state 1611 * @plane: DRM plane 1612 * 1613 * This function walks the current configuration and adds all colorops 1614 * currently used by @plane to the atomic configuration @state. This is useful 1615 * when an atomic commit also needs to check all currently enabled colorop on 1616 * @plane, e.g. when changing the mode. It's also useful when re-enabling a plane 1617 * to avoid special code to force-enable all colorops. 1618 * 1619 * Since acquiring a colorop state will always also acquire the w/w mutex of the 1620 * current plane for that colorop (if there is any) adding all the colorop states for 1621 * a plane will not reduce parallelism of atomic updates. 1622 * 1623 * Returns: 1624 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK 1625 * then the w/w mutex code has detected a deadlock and the entire atomic 1626 * sequence must be restarted. All other errors are fatal. 1627 */ 1628 int 1629 drm_atomic_add_affected_colorops(struct drm_atomic_state *state, 1630 struct drm_plane *plane) 1631 { 1632 struct drm_colorop *colorop; 1633 struct drm_colorop_state *colorop_state; 1634 1635 WARN_ON(!drm_atomic_get_new_plane_state(state, plane)); 1636 1637 drm_dbg_atomic(plane->dev, 1638 "Adding all current colorops for [PLANE:%d:%s] to %p\n", 1639 plane->base.id, plane->name, state); 1640 1641 drm_for_each_colorop(colorop, plane->dev) { 1642 if (colorop->plane != plane) 1643 continue; 1644 1645 colorop_state = drm_atomic_get_colorop_state(state, colorop); 1646 if (IS_ERR(colorop_state)) 1647 return PTR_ERR(colorop_state); 1648 } 1649 1650 return 0; 1651 } 1652 EXPORT_SYMBOL(drm_atomic_add_affected_colorops); 1653 1654 /** 1655 * drm_atomic_check_only - check whether a given config would work 1656 * @state: atomic configuration to check 1657 * 1658 * Note that this function can return -EDEADLK if the driver needed to acquire 1659 * more locks but encountered a deadlock. The caller must then do the usual w/w 1660 * backoff dance and restart. All other errors are fatal. 1661 * 1662 * Returns: 1663 * 0 on success, negative error code on failure. 1664 */ 1665 int drm_atomic_check_only(struct drm_atomic_state *state) 1666 { 1667 struct drm_device *dev = state->dev; 1668 struct drm_mode_config *config = &dev->mode_config; 1669 struct drm_plane *plane; 1670 struct drm_plane_state *old_plane_state; 1671 struct drm_plane_state *new_plane_state; 1672 struct drm_crtc *crtc; 1673 struct drm_crtc_state *old_crtc_state; 1674 struct drm_crtc_state *new_crtc_state; 1675 struct drm_connector *conn; 1676 struct drm_connector_state *conn_state; 1677 unsigned int requested_crtc = 0; 1678 unsigned int affected_crtc = 0; 1679 int i, ret = 0; 1680 1681 drm_dbg_atomic(dev, "checking %p\n", state); 1682 1683 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) { 1684 if (new_crtc_state->enable) 1685 requested_crtc |= drm_crtc_mask(crtc); 1686 } 1687 1688 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) { 1689 ret = drm_atomic_plane_check(old_plane_state, new_plane_state); 1690 if (ret) { 1691 drm_dbg_atomic(dev, "[PLANE:%d:%s] atomic core check failed\n", 1692 plane->base.id, plane->name); 1693 return ret; 1694 } 1695 } 1696 1697 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) { 1698 ret = drm_atomic_crtc_check(old_crtc_state, new_crtc_state); 1699 if (ret) { 1700 drm_dbg_atomic(dev, "[CRTC:%d:%s] atomic core check failed\n", 1701 crtc->base.id, crtc->name); 1702 return ret; 1703 } 1704 } 1705 1706 for_each_new_connector_in_state(state, conn, conn_state, i) { 1707 ret = drm_atomic_connector_check(conn, conn_state); 1708 if (ret) { 1709 drm_dbg_atomic(dev, "[CONNECTOR:%d:%s] atomic core check failed\n", 1710 conn->base.id, conn->name); 1711 return ret; 1712 } 1713 } 1714 1715 if (config->funcs->atomic_check) { 1716 ret = config->funcs->atomic_check(state->dev, state); 1717 1718 if (ret) { 1719 drm_dbg_atomic(dev, "atomic driver check for %p failed: %d\n", 1720 state, ret); 1721 return ret; 1722 } 1723 } 1724 1725 if (!state->allow_modeset) { 1726 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) { 1727 if (drm_atomic_crtc_needs_modeset(new_crtc_state)) { 1728 drm_dbg_atomic(dev, "[CRTC:%d:%s] requires full modeset\n", 1729 crtc->base.id, crtc->name); 1730 return -EINVAL; 1731 } 1732 } 1733 } 1734 1735 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) { 1736 if (new_crtc_state->enable) 1737 affected_crtc |= drm_crtc_mask(crtc); 1738 } 1739 1740 /* 1741 * For commits that allow modesets drivers can add other CRTCs to the 1742 * atomic commit, e.g. when they need to reallocate global resources. 1743 * This can cause spurious EBUSY, which robs compositors of a very 1744 * effective sanity check for their drawing loop. Therefor only allow 1745 * drivers to add unrelated CRTC states for modeset commits. 1746 * 1747 * FIXME: Should add affected_crtc mask to the ATOMIC IOCTL as an output 1748 * so compositors know what's going on. 1749 */ 1750 if (affected_crtc != requested_crtc) { 1751 drm_dbg_atomic(dev, 1752 "driver added CRTC to commit: requested 0x%x, affected 0x%0x\n", 1753 requested_crtc, affected_crtc); 1754 WARN(!state->allow_modeset, "adding CRTC not allowed without modesets: requested 0x%x, affected 0x%0x\n", 1755 requested_crtc, affected_crtc); 1756 } 1757 1758 state->checked = true; 1759 1760 return 0; 1761 } 1762 EXPORT_SYMBOL(drm_atomic_check_only); 1763 1764 /** 1765 * drm_atomic_commit - commit configuration atomically 1766 * @state: atomic configuration to check 1767 * 1768 * Note that this function can return -EDEADLK if the driver needed to acquire 1769 * more locks but encountered a deadlock. The caller must then do the usual w/w 1770 * backoff dance and restart. All other errors are fatal. 1771 * 1772 * This function will take its own reference on @state. 1773 * Callers should always release their reference with drm_atomic_state_put(). 1774 * 1775 * Returns: 1776 * 0 on success, negative error code on failure. 1777 */ 1778 int drm_atomic_commit(struct drm_atomic_state *state) 1779 { 1780 struct drm_mode_config *config = &state->dev->mode_config; 1781 struct drm_printer p = drm_info_printer(state->dev->dev); 1782 int ret; 1783 1784 if (drm_debug_enabled(DRM_UT_STATE)) 1785 drm_atomic_print_new_state(state, &p); 1786 1787 ret = drm_atomic_check_only(state); 1788 if (ret) 1789 return ret; 1790 1791 drm_dbg_atomic(state->dev, "committing %p\n", state); 1792 1793 return config->funcs->atomic_commit(state->dev, state, false); 1794 } 1795 EXPORT_SYMBOL(drm_atomic_commit); 1796 1797 /** 1798 * drm_atomic_nonblocking_commit - atomic nonblocking commit 1799 * @state: atomic configuration to check 1800 * 1801 * Note that this function can return -EDEADLK if the driver needed to acquire 1802 * more locks but encountered a deadlock. The caller must then do the usual w/w 1803 * backoff dance and restart. All other errors are fatal. 1804 * 1805 * This function will take its own reference on @state. 1806 * Callers should always release their reference with drm_atomic_state_put(). 1807 * 1808 * Returns: 1809 * 0 on success, negative error code on failure. 1810 */ 1811 int drm_atomic_nonblocking_commit(struct drm_atomic_state *state) 1812 { 1813 struct drm_mode_config *config = &state->dev->mode_config; 1814 int ret; 1815 1816 ret = drm_atomic_check_only(state); 1817 if (ret) 1818 return ret; 1819 1820 drm_dbg_atomic(state->dev, "committing %p nonblocking\n", state); 1821 1822 return config->funcs->atomic_commit(state->dev, state, true); 1823 } 1824 EXPORT_SYMBOL(drm_atomic_nonblocking_commit); 1825 1826 /* just used from drm-client and atomic-helper: */ 1827 int __drm_atomic_helper_disable_plane(struct drm_plane *plane, 1828 struct drm_plane_state *plane_state) 1829 { 1830 int ret; 1831 1832 ret = drm_atomic_set_crtc_for_plane(plane_state, NULL); 1833 if (ret != 0) 1834 return ret; 1835 1836 drm_atomic_set_fb_for_plane(plane_state, NULL); 1837 plane_state->crtc_x = 0; 1838 plane_state->crtc_y = 0; 1839 plane_state->crtc_w = 0; 1840 plane_state->crtc_h = 0; 1841 plane_state->src_x = 0; 1842 plane_state->src_y = 0; 1843 plane_state->src_w = 0; 1844 plane_state->src_h = 0; 1845 1846 return 0; 1847 } 1848 EXPORT_SYMBOL(__drm_atomic_helper_disable_plane); 1849 1850 static int update_output_state(struct drm_atomic_state *state, 1851 struct drm_mode_set *set) 1852 { 1853 struct drm_device *dev = set->crtc->dev; 1854 struct drm_crtc *crtc; 1855 struct drm_crtc_state *new_crtc_state; 1856 struct drm_connector *connector; 1857 struct drm_connector_state *new_conn_state; 1858 int ret, i; 1859 1860 ret = drm_modeset_lock(&dev->mode_config.connection_mutex, 1861 state->acquire_ctx); 1862 if (ret) 1863 return ret; 1864 1865 /* First disable all connectors on the target crtc. */ 1866 ret = drm_atomic_add_affected_connectors(state, set->crtc); 1867 if (ret) 1868 return ret; 1869 1870 for_each_new_connector_in_state(state, connector, new_conn_state, i) { 1871 if (new_conn_state->crtc == set->crtc) { 1872 ret = drm_atomic_set_crtc_for_connector(new_conn_state, 1873 NULL); 1874 if (ret) 1875 return ret; 1876 1877 /* Make sure legacy setCrtc always re-trains */ 1878 new_conn_state->link_status = DRM_LINK_STATUS_GOOD; 1879 } 1880 } 1881 1882 /* Then set all connectors from set->connectors on the target crtc */ 1883 for (i = 0; i < set->num_connectors; i++) { 1884 new_conn_state = drm_atomic_get_connector_state(state, 1885 set->connectors[i]); 1886 if (IS_ERR(new_conn_state)) 1887 return PTR_ERR(new_conn_state); 1888 1889 ret = drm_atomic_set_crtc_for_connector(new_conn_state, 1890 set->crtc); 1891 if (ret) 1892 return ret; 1893 } 1894 1895 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) { 1896 /* 1897 * Don't update ->enable for the CRTC in the set_config request, 1898 * since a mismatch would indicate a bug in the upper layers. 1899 * The actual modeset code later on will catch any 1900 * inconsistencies here. 1901 */ 1902 if (crtc == set->crtc) 1903 continue; 1904 1905 if (!new_crtc_state->connector_mask) { 1906 ret = drm_atomic_set_mode_prop_for_crtc(new_crtc_state, 1907 NULL); 1908 if (ret < 0) 1909 return ret; 1910 1911 new_crtc_state->active = false; 1912 } 1913 } 1914 1915 return 0; 1916 } 1917 1918 /* just used from drm-client and atomic-helper: */ 1919 int __drm_atomic_helper_set_config(struct drm_mode_set *set, 1920 struct drm_atomic_state *state) 1921 { 1922 struct drm_crtc_state *crtc_state; 1923 struct drm_plane_state *primary_state; 1924 struct drm_crtc *crtc = set->crtc; 1925 int hdisplay, vdisplay; 1926 int ret; 1927 1928 crtc_state = drm_atomic_get_crtc_state(state, crtc); 1929 if (IS_ERR(crtc_state)) 1930 return PTR_ERR(crtc_state); 1931 1932 primary_state = drm_atomic_get_plane_state(state, crtc->primary); 1933 if (IS_ERR(primary_state)) 1934 return PTR_ERR(primary_state); 1935 1936 if (!set->mode) { 1937 WARN_ON(set->fb); 1938 WARN_ON(set->num_connectors); 1939 1940 ret = drm_atomic_set_mode_for_crtc(crtc_state, NULL); 1941 if (ret != 0) 1942 return ret; 1943 1944 crtc_state->active = false; 1945 1946 ret = drm_atomic_set_crtc_for_plane(primary_state, NULL); 1947 if (ret != 0) 1948 return ret; 1949 1950 drm_atomic_set_fb_for_plane(primary_state, NULL); 1951 1952 goto commit; 1953 } 1954 1955 WARN_ON(!set->fb); 1956 WARN_ON(!set->num_connectors); 1957 1958 ret = drm_atomic_set_mode_for_crtc(crtc_state, set->mode); 1959 if (ret != 0) 1960 return ret; 1961 1962 crtc_state->active = true; 1963 1964 ret = drm_atomic_set_crtc_for_plane(primary_state, crtc); 1965 if (ret != 0) 1966 return ret; 1967 1968 drm_mode_get_hv_timing(set->mode, &hdisplay, &vdisplay); 1969 1970 drm_atomic_set_fb_for_plane(primary_state, set->fb); 1971 primary_state->crtc_x = 0; 1972 primary_state->crtc_y = 0; 1973 primary_state->crtc_w = hdisplay; 1974 primary_state->crtc_h = vdisplay; 1975 primary_state->src_x = set->x << 16; 1976 primary_state->src_y = set->y << 16; 1977 if (drm_rotation_90_or_270(primary_state->rotation)) { 1978 primary_state->src_w = vdisplay << 16; 1979 primary_state->src_h = hdisplay << 16; 1980 } else { 1981 primary_state->src_w = hdisplay << 16; 1982 primary_state->src_h = vdisplay << 16; 1983 } 1984 1985 commit: 1986 ret = update_output_state(state, set); 1987 if (ret) 1988 return ret; 1989 1990 return 0; 1991 } 1992 EXPORT_SYMBOL(__drm_atomic_helper_set_config); 1993 1994 static void drm_atomic_private_obj_print_state(struct drm_printer *p, 1995 const struct drm_private_state *state) 1996 { 1997 struct drm_private_obj *obj = state->obj; 1998 1999 if (obj->funcs->atomic_print_state) 2000 obj->funcs->atomic_print_state(p, state); 2001 } 2002 2003 /** 2004 * drm_atomic_print_new_state - prints drm atomic state 2005 * @state: atomic configuration to check 2006 * @p: drm printer 2007 * 2008 * This functions prints the drm atomic state snapshot using the drm printer 2009 * which is passed to it. This snapshot can be used for debugging purposes. 2010 * 2011 * Note that this function looks into the new state objects and hence its not 2012 * safe to be used after the call to drm_atomic_helper_commit_hw_done(). 2013 */ 2014 void drm_atomic_print_new_state(const struct drm_atomic_state *state, 2015 struct drm_printer *p) 2016 { 2017 struct drm_plane *plane; 2018 struct drm_plane_state *plane_state; 2019 struct drm_crtc *crtc; 2020 struct drm_crtc_state *crtc_state; 2021 struct drm_connector *connector; 2022 struct drm_connector_state *connector_state; 2023 struct drm_private_obj *obj; 2024 struct drm_private_state *obj_state; 2025 int i; 2026 2027 if (!p) { 2028 drm_err(state->dev, "invalid drm printer\n"); 2029 return; 2030 } 2031 2032 drm_dbg_atomic(state->dev, "checking %p\n", state); 2033 2034 for_each_new_plane_in_state(state, plane, plane_state, i) 2035 drm_atomic_plane_print_state(p, plane_state); 2036 2037 for_each_new_crtc_in_state(state, crtc, crtc_state, i) 2038 drm_atomic_crtc_print_state(p, crtc_state); 2039 2040 for_each_new_connector_in_state(state, connector, connector_state, i) 2041 drm_atomic_connector_print_state(p, connector_state); 2042 2043 for_each_new_private_obj_in_state(state, obj, obj_state, i) 2044 drm_atomic_private_obj_print_state(p, obj_state); 2045 } 2046 EXPORT_SYMBOL(drm_atomic_print_new_state); 2047 2048 static void __drm_state_dump(struct drm_device *dev, struct drm_printer *p, 2049 bool take_locks) 2050 { 2051 struct drm_mode_config *config = &dev->mode_config; 2052 struct drm_colorop *colorop; 2053 struct drm_plane *plane; 2054 struct drm_crtc *crtc; 2055 struct drm_connector *connector; 2056 struct drm_connector_list_iter conn_iter; 2057 struct drm_private_obj *obj; 2058 2059 if (!drm_drv_uses_atomic_modeset(dev)) 2060 return; 2061 2062 list_for_each_entry(colorop, &config->colorop_list, head) { 2063 if (take_locks) 2064 drm_modeset_lock(&colorop->plane->mutex, NULL); 2065 drm_atomic_colorop_print_state(p, colorop->state); 2066 if (take_locks) 2067 drm_modeset_unlock(&colorop->plane->mutex); 2068 } 2069 2070 list_for_each_entry(plane, &config->plane_list, head) { 2071 if (take_locks) 2072 drm_modeset_lock(&plane->mutex, NULL); 2073 drm_atomic_plane_print_state(p, plane->state); 2074 if (take_locks) 2075 drm_modeset_unlock(&plane->mutex); 2076 } 2077 2078 list_for_each_entry(crtc, &config->crtc_list, head) { 2079 if (take_locks) 2080 drm_modeset_lock(&crtc->mutex, NULL); 2081 drm_atomic_crtc_print_state(p, crtc->state); 2082 if (take_locks) 2083 drm_modeset_unlock(&crtc->mutex); 2084 } 2085 2086 drm_connector_list_iter_begin(dev, &conn_iter); 2087 if (take_locks) 2088 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL); 2089 drm_for_each_connector_iter(connector, &conn_iter) 2090 drm_atomic_connector_print_state(p, connector->state); 2091 if (take_locks) 2092 drm_modeset_unlock(&dev->mode_config.connection_mutex); 2093 drm_connector_list_iter_end(&conn_iter); 2094 2095 list_for_each_entry(obj, &config->privobj_list, head) { 2096 if (take_locks) 2097 drm_modeset_lock(&obj->lock, NULL); 2098 drm_atomic_private_obj_print_state(p, obj->state); 2099 if (take_locks) 2100 drm_modeset_unlock(&obj->lock); 2101 } 2102 } 2103 2104 /** 2105 * drm_state_dump - dump entire device atomic state 2106 * @dev: the drm device 2107 * @p: where to print the state to 2108 * 2109 * Just for debugging. Drivers might want an option to dump state 2110 * to dmesg in case of error irq's. (Hint, you probably want to 2111 * ratelimit this!) 2112 * 2113 * The caller must wrap this drm_modeset_lock_all_ctx() and 2114 * drm_modeset_drop_locks(). If this is called from error irq handler, it should 2115 * not be enabled by default - if you are debugging errors you might 2116 * not care that this is racey, but calling this without all modeset locks held 2117 * is inherently unsafe. 2118 */ 2119 void drm_state_dump(struct drm_device *dev, struct drm_printer *p) 2120 { 2121 __drm_state_dump(dev, p, false); 2122 } 2123 EXPORT_SYMBOL(drm_state_dump); 2124 2125 #ifdef CONFIG_DEBUG_FS 2126 static int drm_state_info(struct seq_file *m, void *data) 2127 { 2128 struct drm_debugfs_entry *entry = m->private; 2129 struct drm_device *dev = entry->dev; 2130 struct drm_printer p = drm_seq_file_printer(m); 2131 2132 __drm_state_dump(dev, &p, true); 2133 2134 return 0; 2135 } 2136 2137 /* any use in debugfs files to dump individual planes/crtc/etc? */ 2138 static const struct drm_debugfs_info drm_atomic_debugfs_list[] = { 2139 {"state", drm_state_info, 0}, 2140 }; 2141 2142 void drm_atomic_debugfs_init(struct drm_device *dev) 2143 { 2144 drm_debugfs_add_files(dev, drm_atomic_debugfs_list, 2145 ARRAY_SIZE(drm_atomic_debugfs_list)); 2146 } 2147 #endif 2148