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