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