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