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