1 /* 2 * Copyright (C) 2014 Red Hat 3 * Copyright (C) 2014 Intel Corp. 4 * 5 * Permission is hereby granted, free of charge, to any person obtaining a 6 * copy of this software and associated documentation files (the "Software"), 7 * to deal in the Software without restriction, including without limitation 8 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 9 * and/or sell copies of the Software, and to permit persons to whom the 10 * Software is furnished to do so, subject to the following conditions: 11 * 12 * The above copyright notice and this permission notice shall be included in 13 * all copies or substantial portions of the Software. 14 * 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR 19 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 20 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 21 * OTHER DEALINGS IN THE SOFTWARE. 22 * 23 * Authors: 24 * Rob Clark <robdclark@gmail.com> 25 * Daniel Vetter <daniel.vetter@ffwll.ch> 26 */ 27 28 29 #include <drm/drmP.h> 30 #include <drm/drm_atomic.h> 31 #include <uapi_drm/drm_mode.h> 32 #include <drm/drm_plane_helper.h> 33 34 #include "drm_crtc_internal.h" 35 36 /** 37 * drm_atomic_state_default_release - 38 * release memory initialized by drm_atomic_state_init 39 * @state: atomic state 40 * 41 * Free all the memory allocated by drm_atomic_state_init. 42 * This is useful for drivers that subclass the atomic state. 43 */ 44 void drm_atomic_state_default_release(struct drm_atomic_state *state) 45 { 46 kfree(state->connectors); 47 kfree(state->connector_states); 48 kfree(state->crtcs); 49 kfree(state->crtc_states); 50 kfree(state->planes); 51 kfree(state->plane_states); 52 } 53 EXPORT_SYMBOL(drm_atomic_state_default_release); 54 55 /** 56 * drm_atomic_state_init - init new atomic state 57 * @dev: DRM device 58 * @state: atomic state 59 * 60 * Default implementation for filling in a new atomic state. 61 * This is useful for drivers that subclass the atomic state. 62 */ 63 int 64 drm_atomic_state_init(struct drm_device *dev, struct drm_atomic_state *state) 65 { 66 /* TODO legacy paths should maybe do a better job about 67 * setting this appropriately? 68 */ 69 state->allow_modeset = true; 70 71 state->crtcs = kcalloc(dev->mode_config.num_crtc, 72 sizeof(*state->crtcs), GFP_KERNEL); 73 if (!state->crtcs) 74 goto fail; 75 state->crtc_states = kcalloc(dev->mode_config.num_crtc, 76 sizeof(*state->crtc_states), GFP_KERNEL); 77 if (!state->crtc_states) 78 goto fail; 79 state->planes = kcalloc(dev->mode_config.num_total_plane, 80 sizeof(*state->planes), GFP_KERNEL); 81 if (!state->planes) 82 goto fail; 83 state->plane_states = kcalloc(dev->mode_config.num_total_plane, 84 sizeof(*state->plane_states), GFP_KERNEL); 85 if (!state->plane_states) 86 goto fail; 87 88 state->dev = dev; 89 90 DRM_DEBUG_ATOMIC("Allocated atomic state %p\n", state); 91 92 return 0; 93 fail: 94 drm_atomic_state_default_release(state); 95 return -ENOMEM; 96 } 97 EXPORT_SYMBOL(drm_atomic_state_init); 98 99 /** 100 * drm_atomic_state_alloc - allocate atomic state 101 * @dev: DRM device 102 * 103 * This allocates an empty atomic state to track updates. 104 */ 105 struct drm_atomic_state * 106 drm_atomic_state_alloc(struct drm_device *dev) 107 { 108 struct drm_mode_config *config = &dev->mode_config; 109 struct drm_atomic_state *state; 110 111 if (!config->funcs->atomic_state_alloc) { 112 state = kzalloc(sizeof(*state), GFP_KERNEL); 113 if (!state) 114 return NULL; 115 if (drm_atomic_state_init(dev, state) < 0) { 116 kfree(state); 117 return NULL; 118 } 119 return state; 120 } 121 122 return config->funcs->atomic_state_alloc(dev); 123 } 124 EXPORT_SYMBOL(drm_atomic_state_alloc); 125 126 /** 127 * drm_atomic_state_default_clear - clear base atomic state 128 * @state: atomic state 129 * 130 * Default implementation for clearing atomic state. 131 * This is useful for drivers that subclass the atomic state. 132 */ 133 void drm_atomic_state_default_clear(struct drm_atomic_state *state) 134 { 135 struct drm_device *dev = state->dev; 136 struct drm_mode_config *config = &dev->mode_config; 137 int i; 138 139 DRM_DEBUG_ATOMIC("Clearing atomic state %p\n", state); 140 141 for (i = 0; i < state->num_connector; i++) { 142 struct drm_connector *connector = state->connectors[i]; 143 144 if (!connector) 145 continue; 146 147 /* 148 * FIXME: Async commits can race with connector unplugging and 149 * there's currently nothing that prevents cleanup up state for 150 * deleted connectors. As long as the callback doesn't look at 151 * the connector we'll be fine though, so make sure that's the 152 * case by setting all connector pointers to NULL. 153 */ 154 state->connector_states[i]->connector = NULL; 155 connector->funcs->atomic_destroy_state(NULL, 156 state->connector_states[i]); 157 state->connectors[i] = NULL; 158 state->connector_states[i] = NULL; 159 } 160 161 for (i = 0; i < config->num_crtc; i++) { 162 struct drm_crtc *crtc = state->crtcs[i]; 163 164 if (!crtc) 165 continue; 166 167 crtc->funcs->atomic_destroy_state(crtc, 168 state->crtc_states[i]); 169 state->crtcs[i] = NULL; 170 state->crtc_states[i] = NULL; 171 } 172 173 for (i = 0; i < config->num_total_plane; i++) { 174 struct drm_plane *plane = state->planes[i]; 175 176 if (!plane) 177 continue; 178 179 plane->funcs->atomic_destroy_state(plane, 180 state->plane_states[i]); 181 state->planes[i] = NULL; 182 state->plane_states[i] = NULL; 183 } 184 } 185 EXPORT_SYMBOL(drm_atomic_state_default_clear); 186 187 /** 188 * drm_atomic_state_clear - clear state object 189 * @state: atomic state 190 * 191 * When the w/w mutex algorithm detects a deadlock we need to back off and drop 192 * all locks. So someone else could sneak in and change the current modeset 193 * configuration. Which means that all the state assembled in @state is no 194 * longer an atomic update to the current state, but to some arbitrary earlier 195 * state. Which could break assumptions the driver's ->atomic_check likely 196 * relies on. 197 * 198 * Hence we must clear all cached state and completely start over, using this 199 * function. 200 */ 201 void drm_atomic_state_clear(struct drm_atomic_state *state) 202 { 203 struct drm_device *dev = state->dev; 204 struct drm_mode_config *config = &dev->mode_config; 205 206 if (config->funcs->atomic_state_clear) 207 config->funcs->atomic_state_clear(state); 208 else 209 drm_atomic_state_default_clear(state); 210 } 211 EXPORT_SYMBOL(drm_atomic_state_clear); 212 213 /** 214 * drm_atomic_state_free - free all memory for an atomic state 215 * @state: atomic state to deallocate 216 * 217 * This frees all memory associated with an atomic state, including all the 218 * per-object state for planes, crtcs and connectors. 219 */ 220 void drm_atomic_state_free(struct drm_atomic_state *state) 221 { 222 struct drm_device *dev; 223 struct drm_mode_config *config; 224 225 if (!state) 226 return; 227 228 dev = state->dev; 229 config = &dev->mode_config; 230 231 drm_atomic_state_clear(state); 232 233 DRM_DEBUG_ATOMIC("Freeing atomic state %p\n", state); 234 235 if (config->funcs->atomic_state_free) { 236 config->funcs->atomic_state_free(state); 237 } else { 238 drm_atomic_state_default_release(state); 239 kfree(state); 240 } 241 } 242 EXPORT_SYMBOL(drm_atomic_state_free); 243 244 /** 245 * drm_atomic_get_crtc_state - get crtc state 246 * @state: global atomic state object 247 * @crtc: crtc to get state object for 248 * 249 * This function returns the crtc state for the given crtc, allocating it if 250 * needed. It will also grab the relevant crtc lock to make sure that the state 251 * is consistent. 252 * 253 * Returns: 254 * 255 * Either the allocated state or the error code encoded into the pointer. When 256 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the 257 * entire atomic sequence must be restarted. All other errors are fatal. 258 */ 259 struct drm_crtc_state * 260 drm_atomic_get_crtc_state(struct drm_atomic_state *state, 261 struct drm_crtc *crtc) 262 { 263 int ret, index = drm_crtc_index(crtc); 264 struct drm_crtc_state *crtc_state; 265 266 crtc_state = drm_atomic_get_existing_crtc_state(state, crtc); 267 if (crtc_state) 268 return crtc_state; 269 270 ret = drm_modeset_lock(&crtc->mutex, state->acquire_ctx); 271 if (ret) 272 return ERR_PTR(ret); 273 274 crtc_state = crtc->funcs->atomic_duplicate_state(crtc); 275 if (!crtc_state) 276 return ERR_PTR(-ENOMEM); 277 278 state->crtc_states[index] = crtc_state; 279 state->crtcs[index] = crtc; 280 crtc_state->state = state; 281 282 DRM_DEBUG_ATOMIC("Added [CRTC:%d:%s] %p state to %p\n", 283 crtc->base.id, crtc->name, crtc_state, state); 284 285 return crtc_state; 286 } 287 EXPORT_SYMBOL(drm_atomic_get_crtc_state); 288 289 /** 290 * drm_atomic_set_mode_for_crtc - set mode for CRTC 291 * @state: the CRTC whose incoming state to update 292 * @mode: kernel-internal mode to use for the CRTC, or NULL to disable 293 * 294 * Set a mode (originating from the kernel) on the desired CRTC state. Does 295 * not change any other state properties, including enable, active, or 296 * mode_changed. 297 * 298 * RETURNS: 299 * Zero on success, error code on failure. Cannot return -EDEADLK. 300 */ 301 int drm_atomic_set_mode_for_crtc(struct drm_crtc_state *state, 302 struct drm_display_mode *mode) 303 { 304 struct drm_mode_modeinfo umode; 305 306 /* Early return for no change. */ 307 if (mode && memcmp(&state->mode, mode, sizeof(*mode)) == 0) 308 return 0; 309 310 drm_property_unreference_blob(state->mode_blob); 311 state->mode_blob = NULL; 312 313 if (mode) { 314 drm_mode_convert_to_umode(&umode, mode); 315 state->mode_blob = 316 drm_property_create_blob(state->crtc->dev, 317 sizeof(umode), 318 &umode); 319 if (IS_ERR(state->mode_blob)) 320 return PTR_ERR(state->mode_blob); 321 322 drm_mode_copy(&state->mode, mode); 323 state->enable = true; 324 DRM_DEBUG_ATOMIC("Set [MODE:%s] for CRTC state %p\n", 325 mode->name, state); 326 } else { 327 memset(&state->mode, 0, sizeof(state->mode)); 328 state->enable = false; 329 DRM_DEBUG_ATOMIC("Set [NOMODE] for CRTC state %p\n", 330 state); 331 } 332 333 return 0; 334 } 335 EXPORT_SYMBOL(drm_atomic_set_mode_for_crtc); 336 337 /** 338 * drm_atomic_set_mode_prop_for_crtc - set mode for CRTC 339 * @state: the CRTC whose incoming state to update 340 * @blob: pointer to blob property to use for mode 341 * 342 * Set a mode (originating from a blob property) on the desired CRTC state. 343 * This function will take a reference on the blob property for the CRTC state, 344 * and release the reference held on the state's existing mode property, if any 345 * was set. 346 * 347 * RETURNS: 348 * Zero on success, error code on failure. Cannot return -EDEADLK. 349 */ 350 int drm_atomic_set_mode_prop_for_crtc(struct drm_crtc_state *state, 351 struct drm_property_blob *blob) 352 { 353 if (blob == state->mode_blob) 354 return 0; 355 356 drm_property_unreference_blob(state->mode_blob); 357 state->mode_blob = NULL; 358 359 if (blob) { 360 if (blob->length != sizeof(struct drm_mode_modeinfo) || 361 drm_mode_convert_umode(&state->mode, 362 (const struct drm_mode_modeinfo *) 363 blob->data)) 364 return -EINVAL; 365 366 state->mode_blob = drm_property_reference_blob(blob); 367 state->enable = true; 368 DRM_DEBUG_ATOMIC("Set [MODE:%s] for CRTC state %p\n", 369 state->mode.name, state); 370 } else { 371 memset(&state->mode, 0, sizeof(state->mode)); 372 state->enable = false; 373 DRM_DEBUG_ATOMIC("Set [NOMODE] for CRTC state %p\n", 374 state); 375 } 376 377 return 0; 378 } 379 EXPORT_SYMBOL(drm_atomic_set_mode_prop_for_crtc); 380 381 /** 382 * drm_atomic_replace_property_blob - replace a blob property 383 * @blob: a pointer to the member blob to be replaced 384 * @new_blob: the new blob to replace with 385 * @replaced: whether the blob has been replaced 386 * 387 * RETURNS: 388 * Zero on success, error code on failure 389 */ 390 static void 391 drm_atomic_replace_property_blob(struct drm_property_blob **blob, 392 struct drm_property_blob *new_blob, 393 bool *replaced) 394 { 395 struct drm_property_blob *old_blob = *blob; 396 397 if (old_blob == new_blob) 398 return; 399 400 if (old_blob) 401 drm_property_unreference_blob(old_blob); 402 if (new_blob) 403 drm_property_reference_blob(new_blob); 404 *blob = new_blob; 405 *replaced = true; 406 407 return; 408 } 409 410 static int 411 drm_atomic_replace_property_blob_from_id(struct drm_crtc *crtc, 412 struct drm_property_blob **blob, 413 uint64_t blob_id, 414 ssize_t expected_size, 415 bool *replaced) 416 { 417 struct drm_device *dev = crtc->dev; 418 struct drm_property_blob *new_blob = NULL; 419 420 if (blob_id != 0) { 421 new_blob = drm_property_lookup_blob(dev, blob_id); 422 if (new_blob == NULL) 423 return -EINVAL; 424 if (expected_size > 0 && expected_size != new_blob->length) 425 return -EINVAL; 426 } 427 428 drm_atomic_replace_property_blob(blob, new_blob, replaced); 429 430 return 0; 431 } 432 433 /** 434 * drm_atomic_crtc_set_property - set property on CRTC 435 * @crtc: the drm CRTC to set a property on 436 * @state: the state object to update with the new property value 437 * @property: the property to set 438 * @val: the new property value 439 * 440 * Use this instead of calling crtc->atomic_set_property directly. 441 * This function handles generic/core properties and calls out to 442 * driver's ->atomic_set_property() for driver properties. To ensure 443 * consistent behavior you must call this function rather than the 444 * driver hook directly. 445 * 446 * RETURNS: 447 * Zero on success, error code on failure 448 */ 449 int drm_atomic_crtc_set_property(struct drm_crtc *crtc, 450 struct drm_crtc_state *state, struct drm_property *property, 451 uint64_t val) 452 { 453 struct drm_device *dev = crtc->dev; 454 struct drm_mode_config *config = &dev->mode_config; 455 bool replaced = false; 456 int ret; 457 458 if (property == config->prop_active) 459 state->active = val; 460 else if (property == config->prop_mode_id) { 461 struct drm_property_blob *mode = 462 drm_property_lookup_blob(dev, val); 463 ret = drm_atomic_set_mode_prop_for_crtc(state, mode); 464 drm_property_unreference_blob(mode); 465 return ret; 466 } else if (property == config->degamma_lut_property) { 467 ret = drm_atomic_replace_property_blob_from_id(crtc, 468 &state->degamma_lut, 469 val, 470 -1, 471 &replaced); 472 state->color_mgmt_changed = replaced; 473 return ret; 474 } else if (property == config->ctm_property) { 475 ret = drm_atomic_replace_property_blob_from_id(crtc, 476 &state->ctm, 477 val, 478 sizeof(struct drm_color_ctm), 479 &replaced); 480 state->color_mgmt_changed = replaced; 481 return ret; 482 } else if (property == config->gamma_lut_property) { 483 ret = drm_atomic_replace_property_blob_from_id(crtc, 484 &state->gamma_lut, 485 val, 486 -1, 487 &replaced); 488 state->color_mgmt_changed = replaced; 489 return ret; 490 } else if (crtc->funcs->atomic_set_property) 491 return crtc->funcs->atomic_set_property(crtc, state, property, val); 492 else 493 return -EINVAL; 494 495 return 0; 496 } 497 EXPORT_SYMBOL(drm_atomic_crtc_set_property); 498 499 /** 500 * drm_atomic_crtc_get_property - get property value from CRTC state 501 * @crtc: the drm CRTC to set a property on 502 * @state: the state object to get the property value from 503 * @property: the property to set 504 * @val: return location for the property value 505 * 506 * This function handles generic/core properties and calls out to 507 * driver's ->atomic_get_property() for driver properties. To ensure 508 * consistent behavior you must call this function rather than the 509 * driver hook directly. 510 * 511 * RETURNS: 512 * Zero on success, error code on failure 513 */ 514 static int 515 drm_atomic_crtc_get_property(struct drm_crtc *crtc, 516 const struct drm_crtc_state *state, 517 struct drm_property *property, uint64_t *val) 518 { 519 struct drm_device *dev = crtc->dev; 520 struct drm_mode_config *config = &dev->mode_config; 521 522 if (property == config->prop_active) 523 *val = state->active; 524 else if (property == config->prop_mode_id) 525 *val = (state->mode_blob) ? state->mode_blob->base.id : 0; 526 else if (property == config->degamma_lut_property) 527 *val = (state->degamma_lut) ? state->degamma_lut->base.id : 0; 528 else if (property == config->ctm_property) 529 *val = (state->ctm) ? state->ctm->base.id : 0; 530 else if (property == config->gamma_lut_property) 531 *val = (state->gamma_lut) ? state->gamma_lut->base.id : 0; 532 else if (crtc->funcs->atomic_get_property) 533 return crtc->funcs->atomic_get_property(crtc, state, property, val); 534 else 535 return -EINVAL; 536 537 return 0; 538 } 539 540 /** 541 * drm_atomic_crtc_check - check crtc state 542 * @crtc: crtc to check 543 * @state: crtc state to check 544 * 545 * Provides core sanity checks for crtc state. 546 * 547 * RETURNS: 548 * Zero on success, error code on failure 549 */ 550 static int drm_atomic_crtc_check(struct drm_crtc *crtc, 551 struct drm_crtc_state *state) 552 { 553 /* NOTE: we explicitly don't enforce constraints such as primary 554 * layer covering entire screen, since that is something we want 555 * to allow (on hw that supports it). For hw that does not, it 556 * should be checked in driver's crtc->atomic_check() vfunc. 557 * 558 * TODO: Add generic modeset state checks once we support those. 559 */ 560 561 if (state->active && !state->enable) { 562 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] active without enabled\n", 563 crtc->base.id, crtc->name); 564 return -EINVAL; 565 } 566 567 /* The state->enable vs. state->mode_blob checks can be WARN_ON, 568 * as this is a kernel-internal detail that userspace should never 569 * be able to trigger. */ 570 if (drm_core_check_feature(crtc->dev, DRIVER_ATOMIC) && 571 WARN_ON(state->enable && !state->mode_blob)) { 572 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] enabled without mode blob\n", 573 crtc->base.id, crtc->name); 574 return -EINVAL; 575 } 576 577 if (drm_core_check_feature(crtc->dev, DRIVER_ATOMIC) && 578 WARN_ON(!state->enable && state->mode_blob)) { 579 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] disabled with mode blob\n", 580 crtc->base.id, crtc->name); 581 return -EINVAL; 582 } 583 584 /* 585 * Reject event generation for when a CRTC is off and stays off. 586 * It wouldn't be hard to implement this, but userspace has a track 587 * record of happily burning through 100% cpu (or worse, crash) when the 588 * display pipe is suspended. To avoid all that fun just reject updates 589 * that ask for events since likely that indicates a bug in the 590 * compositor's drawing loop. This is consistent with the vblank IOCTL 591 * and legacy page_flip IOCTL which also reject service on a disabled 592 * pipe. 593 */ 594 if (state->event && !state->active && !crtc->state->active) { 595 DRM_DEBUG_ATOMIC("[CRTC:%d] requesting event but off\n", 596 crtc->base.id); 597 return -EINVAL; 598 } 599 600 return 0; 601 } 602 603 /** 604 * drm_atomic_get_plane_state - get plane state 605 * @state: global atomic state object 606 * @plane: plane to get state object for 607 * 608 * This function returns the plane state for the given plane, allocating it if 609 * needed. It will also grab the relevant plane lock to make sure that the state 610 * is consistent. 611 * 612 * Returns: 613 * 614 * Either the allocated state or the error code encoded into the pointer. When 615 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the 616 * entire atomic sequence must be restarted. All other errors are fatal. 617 */ 618 struct drm_plane_state * 619 drm_atomic_get_plane_state(struct drm_atomic_state *state, 620 struct drm_plane *plane) 621 { 622 int ret, index = drm_plane_index(plane); 623 struct drm_plane_state *plane_state; 624 625 plane_state = drm_atomic_get_existing_plane_state(state, plane); 626 if (plane_state) 627 return plane_state; 628 629 ret = drm_modeset_lock(&plane->mutex, state->acquire_ctx); 630 if (ret) 631 return ERR_PTR(ret); 632 633 plane_state = plane->funcs->atomic_duplicate_state(plane); 634 if (!plane_state) 635 return ERR_PTR(-ENOMEM); 636 637 state->plane_states[index] = plane_state; 638 state->planes[index] = plane; 639 plane_state->state = state; 640 641 DRM_DEBUG_ATOMIC("Added [PLANE:%d:%s] %p state to %p\n", 642 plane->base.id, plane->name, plane_state, state); 643 644 if (plane_state->crtc) { 645 struct drm_crtc_state *crtc_state; 646 647 crtc_state = drm_atomic_get_crtc_state(state, 648 plane_state->crtc); 649 if (IS_ERR(crtc_state)) 650 return ERR_CAST(crtc_state); 651 } 652 653 return plane_state; 654 } 655 EXPORT_SYMBOL(drm_atomic_get_plane_state); 656 657 /** 658 * drm_atomic_plane_set_property - set property on plane 659 * @plane: the drm plane to set a property on 660 * @state: the state object to update with the new property value 661 * @property: the property to set 662 * @val: the new property value 663 * 664 * Use this instead of calling plane->atomic_set_property directly. 665 * This function handles generic/core properties and calls out to 666 * driver's ->atomic_set_property() for driver properties. To ensure 667 * consistent behavior you must call this function rather than the 668 * driver hook directly. 669 * 670 * RETURNS: 671 * Zero on success, error code on failure 672 */ 673 int drm_atomic_plane_set_property(struct drm_plane *plane, 674 struct drm_plane_state *state, struct drm_property *property, 675 uint64_t val) 676 { 677 struct drm_device *dev = plane->dev; 678 struct drm_mode_config *config = &dev->mode_config; 679 680 if (property == config->prop_fb_id) { 681 struct drm_framebuffer *fb = drm_framebuffer_lookup(dev, val); 682 drm_atomic_set_fb_for_plane(state, fb); 683 if (fb) 684 drm_framebuffer_unreference(fb); 685 } else if (property == config->prop_crtc_id) { 686 struct drm_crtc *crtc = drm_crtc_find(dev, val); 687 return drm_atomic_set_crtc_for_plane(state, crtc); 688 } else if (property == config->prop_crtc_x) { 689 state->crtc_x = U642I64(val); 690 } else if (property == config->prop_crtc_y) { 691 state->crtc_y = U642I64(val); 692 } else if (property == config->prop_crtc_w) { 693 state->crtc_w = val; 694 } else if (property == config->prop_crtc_h) { 695 state->crtc_h = val; 696 } else if (property == config->prop_src_x) { 697 state->src_x = val; 698 } else if (property == config->prop_src_y) { 699 state->src_y = val; 700 } else if (property == config->prop_src_w) { 701 state->src_w = val; 702 } else if (property == config->prop_src_h) { 703 state->src_h = val; 704 } else if (property == config->rotation_property) { 705 state->rotation = val; 706 } else if (plane->funcs->atomic_set_property) { 707 return plane->funcs->atomic_set_property(plane, state, 708 property, val); 709 } else { 710 return -EINVAL; 711 } 712 713 return 0; 714 } 715 EXPORT_SYMBOL(drm_atomic_plane_set_property); 716 717 /** 718 * drm_atomic_plane_get_property - get property value from plane state 719 * @plane: the drm plane to set a property on 720 * @state: the state object to get the property value from 721 * @property: the property to set 722 * @val: return location for the property value 723 * 724 * This function handles generic/core properties and calls out to 725 * driver's ->atomic_get_property() for driver properties. To ensure 726 * consistent behavior you must call this function rather than the 727 * driver hook directly. 728 * 729 * RETURNS: 730 * Zero on success, error code on failure 731 */ 732 static int 733 drm_atomic_plane_get_property(struct drm_plane *plane, 734 const struct drm_plane_state *state, 735 struct drm_property *property, uint64_t *val) 736 { 737 struct drm_device *dev = plane->dev; 738 struct drm_mode_config *config = &dev->mode_config; 739 740 if (property == config->prop_fb_id) { 741 *val = (state->fb) ? state->fb->base.id : 0; 742 } else if (property == config->prop_crtc_id) { 743 *val = (state->crtc) ? state->crtc->base.id : 0; 744 } else if (property == config->prop_crtc_x) { 745 *val = I642U64(state->crtc_x); 746 } else if (property == config->prop_crtc_y) { 747 *val = I642U64(state->crtc_y); 748 } else if (property == config->prop_crtc_w) { 749 *val = state->crtc_w; 750 } else if (property == config->prop_crtc_h) { 751 *val = state->crtc_h; 752 } else if (property == config->prop_src_x) { 753 *val = state->src_x; 754 } else if (property == config->prop_src_y) { 755 *val = state->src_y; 756 } else if (property == config->prop_src_w) { 757 *val = state->src_w; 758 } else if (property == config->prop_src_h) { 759 *val = state->src_h; 760 } else if (property == config->rotation_property) { 761 *val = state->rotation; 762 } else if (plane->funcs->atomic_get_property) { 763 return plane->funcs->atomic_get_property(plane, state, property, val); 764 } else { 765 return -EINVAL; 766 } 767 768 return 0; 769 } 770 771 static bool 772 plane_switching_crtc(struct drm_atomic_state *state, 773 struct drm_plane *plane, 774 struct drm_plane_state *plane_state) 775 { 776 if (!plane->state->crtc || !plane_state->crtc) 777 return false; 778 779 if (plane->state->crtc == plane_state->crtc) 780 return false; 781 782 /* This could be refined, but currently there's no helper or driver code 783 * to implement direct switching of active planes nor userspace to take 784 * advantage of more direct plane switching without the intermediate 785 * full OFF state. 786 */ 787 return true; 788 } 789 790 /** 791 * drm_atomic_plane_check - check plane state 792 * @plane: plane to check 793 * @state: plane state to check 794 * 795 * Provides core sanity checks for plane state. 796 * 797 * RETURNS: 798 * Zero on success, error code on failure 799 */ 800 static int drm_atomic_plane_check(struct drm_plane *plane, 801 struct drm_plane_state *state) 802 { 803 unsigned int fb_width, fb_height; 804 int ret; 805 806 /* either *both* CRTC and FB must be set, or neither */ 807 if (WARN_ON(state->crtc && !state->fb)) { 808 DRM_DEBUG_ATOMIC("CRTC set but no FB\n"); 809 return -EINVAL; 810 } else if (WARN_ON(state->fb && !state->crtc)) { 811 DRM_DEBUG_ATOMIC("FB set but no CRTC\n"); 812 return -EINVAL; 813 } 814 815 /* if disabled, we don't care about the rest of the state: */ 816 if (!state->crtc) 817 return 0; 818 819 /* Check whether this plane is usable on this CRTC */ 820 if (!(plane->possible_crtcs & drm_crtc_mask(state->crtc))) { 821 DRM_DEBUG_ATOMIC("Invalid crtc for plane\n"); 822 return -EINVAL; 823 } 824 825 /* Check whether this plane supports the fb pixel format. */ 826 ret = drm_plane_check_pixel_format(plane, state->fb->pixel_format); 827 if (ret) { 828 DRM_DEBUG_ATOMIC("Invalid pixel format %s\n", 829 drm_get_format_name(state->fb->pixel_format)); 830 return ret; 831 } 832 833 /* Give drivers some help against integer overflows */ 834 if (state->crtc_w > INT_MAX || 835 state->crtc_x > INT_MAX - (int32_t) state->crtc_w || 836 state->crtc_h > INT_MAX || 837 state->crtc_y > INT_MAX - (int32_t) state->crtc_h) { 838 DRM_DEBUG_ATOMIC("Invalid CRTC coordinates %ux%u+%d+%d\n", 839 state->crtc_w, state->crtc_h, 840 state->crtc_x, state->crtc_y); 841 return -ERANGE; 842 } 843 844 fb_width = state->fb->width << 16; 845 fb_height = state->fb->height << 16; 846 847 /* Make sure source coordinates are inside the fb. */ 848 if (state->src_w > fb_width || 849 state->src_x > fb_width - state->src_w || 850 state->src_h > fb_height || 851 state->src_y > fb_height - state->src_h) { 852 DRM_DEBUG_ATOMIC("Invalid source coordinates " 853 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n", 854 state->src_w >> 16, ((state->src_w & 0xffff) * 15625) >> 10, 855 state->src_h >> 16, ((state->src_h & 0xffff) * 15625) >> 10, 856 state->src_x >> 16, ((state->src_x & 0xffff) * 15625) >> 10, 857 state->src_y >> 16, ((state->src_y & 0xffff) * 15625) >> 10); 858 return -ENOSPC; 859 } 860 861 if (plane_switching_crtc(state->state, plane, state)) { 862 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] switching CRTC directly\n", 863 plane->base.id, plane->name); 864 return -EINVAL; 865 } 866 867 return 0; 868 } 869 870 /** 871 * drm_atomic_get_connector_state - get connector state 872 * @state: global atomic state object 873 * @connector: connector to get state object for 874 * 875 * This function returns the connector state for the given connector, 876 * allocating it if needed. It will also grab the relevant connector lock to 877 * make sure that the state is consistent. 878 * 879 * Returns: 880 * 881 * Either the allocated state or the error code encoded into the pointer. When 882 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the 883 * entire atomic sequence must be restarted. All other errors are fatal. 884 */ 885 struct drm_connector_state * 886 drm_atomic_get_connector_state(struct drm_atomic_state *state, 887 struct drm_connector *connector) 888 { 889 int ret, index; 890 struct drm_mode_config *config = &connector->dev->mode_config; 891 struct drm_connector_state *connector_state; 892 893 ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx); 894 if (ret) 895 return ERR_PTR(ret); 896 897 index = drm_connector_index(connector); 898 899 if (index >= state->num_connector) { 900 struct drm_connector **c; 901 struct drm_connector_state **cs; 902 int alloc = max(index + 1, config->num_connector); 903 904 c = krealloc(state->connectors, alloc * sizeof(*state->connectors), M_DRM, M_WAITOK); 905 if (!c) 906 return ERR_PTR(-ENOMEM); 907 908 state->connectors = c; 909 memset(&state->connectors[state->num_connector], 0, 910 sizeof(*state->connectors) * (alloc - state->num_connector)); 911 912 cs = krealloc(state->connector_states, alloc * sizeof(*state->connector_states), M_DRM, M_WAITOK); 913 if (!cs) 914 return ERR_PTR(-ENOMEM); 915 916 state->connector_states = cs; 917 memset(&state->connector_states[state->num_connector], 0, 918 sizeof(*state->connector_states) * (alloc - state->num_connector)); 919 state->num_connector = alloc; 920 } 921 922 if (state->connector_states[index]) 923 return state->connector_states[index]; 924 925 connector_state = connector->funcs->atomic_duplicate_state(connector); 926 if (!connector_state) 927 return ERR_PTR(-ENOMEM); 928 929 state->connector_states[index] = connector_state; 930 state->connectors[index] = connector; 931 connector_state->state = state; 932 933 DRM_DEBUG_ATOMIC("Added [CONNECTOR:%d] %p state to %p\n", 934 connector->base.id, connector_state, state); 935 936 if (connector_state->crtc) { 937 struct drm_crtc_state *crtc_state; 938 939 crtc_state = drm_atomic_get_crtc_state(state, 940 connector_state->crtc); 941 if (IS_ERR(crtc_state)) 942 return ERR_CAST(crtc_state); 943 } 944 945 return connector_state; 946 } 947 EXPORT_SYMBOL(drm_atomic_get_connector_state); 948 949 /** 950 * drm_atomic_connector_set_property - set property on connector. 951 * @connector: the drm connector to set a property on 952 * @state: the state object to update with the new property value 953 * @property: the property to set 954 * @val: the new property value 955 * 956 * Use this instead of calling connector->atomic_set_property directly. 957 * This function handles generic/core properties and calls out to 958 * driver's ->atomic_set_property() for driver properties. To ensure 959 * consistent behavior you must call this function rather than the 960 * driver hook directly. 961 * 962 * RETURNS: 963 * Zero on success, error code on failure 964 */ 965 int drm_atomic_connector_set_property(struct drm_connector *connector, 966 struct drm_connector_state *state, struct drm_property *property, 967 uint64_t val) 968 { 969 struct drm_device *dev = connector->dev; 970 struct drm_mode_config *config = &dev->mode_config; 971 972 if (property == config->prop_crtc_id) { 973 struct drm_crtc *crtc = drm_crtc_find(dev, val); 974 return drm_atomic_set_crtc_for_connector(state, crtc); 975 } else if (property == config->dpms_property) { 976 /* setting DPMS property requires special handling, which 977 * is done in legacy setprop path for us. Disallow (for 978 * now?) atomic writes to DPMS property: 979 */ 980 return -EINVAL; 981 } else if (connector->funcs->atomic_set_property) { 982 return connector->funcs->atomic_set_property(connector, 983 state, property, val); 984 } else { 985 return -EINVAL; 986 } 987 } 988 EXPORT_SYMBOL(drm_atomic_connector_set_property); 989 990 /** 991 * drm_atomic_connector_get_property - get property value from connector state 992 * @connector: the drm connector to set a property on 993 * @state: the state object to get the property value from 994 * @property: the property to set 995 * @val: return location for the property value 996 * 997 * This function handles generic/core properties and calls out to 998 * driver's ->atomic_get_property() for driver properties. To ensure 999 * consistent behavior you must call this function rather than the 1000 * driver hook directly. 1001 * 1002 * RETURNS: 1003 * Zero on success, error code on failure 1004 */ 1005 static int 1006 drm_atomic_connector_get_property(struct drm_connector *connector, 1007 const struct drm_connector_state *state, 1008 struct drm_property *property, uint64_t *val) 1009 { 1010 struct drm_device *dev = connector->dev; 1011 struct drm_mode_config *config = &dev->mode_config; 1012 1013 if (property == config->prop_crtc_id) { 1014 *val = (state->crtc) ? state->crtc->base.id : 0; 1015 } else if (property == config->dpms_property) { 1016 *val = connector->dpms; 1017 } else if (connector->funcs->atomic_get_property) { 1018 return connector->funcs->atomic_get_property(connector, 1019 state, property, val); 1020 } else { 1021 return -EINVAL; 1022 } 1023 1024 return 0; 1025 } 1026 1027 int drm_atomic_get_property(struct drm_mode_object *obj, 1028 struct drm_property *property, uint64_t *val) 1029 { 1030 struct drm_device *dev = property->dev; 1031 int ret; 1032 1033 switch (obj->type) { 1034 case DRM_MODE_OBJECT_CONNECTOR: { 1035 struct drm_connector *connector = obj_to_connector(obj); 1036 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex)); 1037 ret = drm_atomic_connector_get_property(connector, 1038 connector->state, property, val); 1039 break; 1040 } 1041 case DRM_MODE_OBJECT_CRTC: { 1042 struct drm_crtc *crtc = obj_to_crtc(obj); 1043 WARN_ON(!drm_modeset_is_locked(&crtc->mutex)); 1044 ret = drm_atomic_crtc_get_property(crtc, 1045 crtc->state, property, val); 1046 break; 1047 } 1048 case DRM_MODE_OBJECT_PLANE: { 1049 struct drm_plane *plane = obj_to_plane(obj); 1050 WARN_ON(!drm_modeset_is_locked(&plane->mutex)); 1051 ret = drm_atomic_plane_get_property(plane, 1052 plane->state, property, val); 1053 break; 1054 } 1055 default: 1056 ret = -EINVAL; 1057 break; 1058 } 1059 1060 return ret; 1061 } 1062 1063 /** 1064 * drm_atomic_set_crtc_for_plane - set crtc for plane 1065 * @plane_state: the plane whose incoming state to update 1066 * @crtc: crtc to use for the plane 1067 * 1068 * Changing the assigned crtc for a plane requires us to grab the lock and state 1069 * for the new crtc, as needed. This function takes care of all these details 1070 * besides updating the pointer in the state object itself. 1071 * 1072 * Returns: 1073 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK 1074 * then the w/w mutex code has detected a deadlock and the entire atomic 1075 * sequence must be restarted. All other errors are fatal. 1076 */ 1077 int 1078 drm_atomic_set_crtc_for_plane(struct drm_plane_state *plane_state, 1079 struct drm_crtc *crtc) 1080 { 1081 struct drm_plane *plane = plane_state->plane; 1082 struct drm_crtc_state *crtc_state; 1083 1084 if (plane_state->crtc) { 1085 crtc_state = drm_atomic_get_crtc_state(plane_state->state, 1086 plane_state->crtc); 1087 if (WARN_ON(IS_ERR(crtc_state))) 1088 return PTR_ERR(crtc_state); 1089 1090 crtc_state->plane_mask &= ~(1 << drm_plane_index(plane)); 1091 } 1092 1093 plane_state->crtc = crtc; 1094 1095 if (crtc) { 1096 crtc_state = drm_atomic_get_crtc_state(plane_state->state, 1097 crtc); 1098 if (IS_ERR(crtc_state)) 1099 return PTR_ERR(crtc_state); 1100 crtc_state->plane_mask |= (1 << drm_plane_index(plane)); 1101 } 1102 1103 if (crtc) 1104 DRM_DEBUG_ATOMIC("Link plane state %p to [CRTC:%d:%s]\n", 1105 plane_state, crtc->base.id, crtc->name); 1106 else 1107 DRM_DEBUG_ATOMIC("Link plane state %p to [NOCRTC]\n", 1108 plane_state); 1109 1110 return 0; 1111 } 1112 EXPORT_SYMBOL(drm_atomic_set_crtc_for_plane); 1113 1114 /** 1115 * drm_atomic_set_fb_for_plane - set framebuffer for plane 1116 * @plane_state: atomic state object for the plane 1117 * @fb: fb to use for the plane 1118 * 1119 * Changing the assigned framebuffer for a plane requires us to grab a reference 1120 * to the new fb and drop the reference to the old fb, if there is one. This 1121 * function takes care of all these details besides updating the pointer in the 1122 * state object itself. 1123 */ 1124 void 1125 drm_atomic_set_fb_for_plane(struct drm_plane_state *plane_state, 1126 struct drm_framebuffer *fb) 1127 { 1128 if (plane_state->fb) 1129 drm_framebuffer_unreference(plane_state->fb); 1130 if (fb) 1131 drm_framebuffer_reference(fb); 1132 plane_state->fb = fb; 1133 1134 if (fb) 1135 DRM_DEBUG_ATOMIC("Set [FB:%d] for plane state %p\n", 1136 fb->base.id, plane_state); 1137 else 1138 DRM_DEBUG_ATOMIC("Set [NOFB] for plane state %p\n", 1139 plane_state); 1140 } 1141 EXPORT_SYMBOL(drm_atomic_set_fb_for_plane); 1142 1143 /** 1144 * drm_atomic_set_crtc_for_connector - set crtc for connector 1145 * @conn_state: atomic state object for the connector 1146 * @crtc: crtc to use for the connector 1147 * 1148 * Changing the assigned crtc for a connector requires us to grab the lock and 1149 * state for the new crtc, as needed. This function takes care of all these 1150 * details besides updating the pointer in the state object itself. 1151 * 1152 * Returns: 1153 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK 1154 * then the w/w mutex code has detected a deadlock and the entire atomic 1155 * sequence must be restarted. All other errors are fatal. 1156 */ 1157 int 1158 drm_atomic_set_crtc_for_connector(struct drm_connector_state *conn_state, 1159 struct drm_crtc *crtc) 1160 { 1161 struct drm_crtc_state *crtc_state; 1162 1163 if (conn_state->crtc && conn_state->crtc != crtc) { 1164 crtc_state = drm_atomic_get_existing_crtc_state(conn_state->state, 1165 conn_state->crtc); 1166 1167 crtc_state->connector_mask &= 1168 ~(1 << drm_connector_index(conn_state->connector)); 1169 } 1170 1171 if (crtc) { 1172 crtc_state = drm_atomic_get_crtc_state(conn_state->state, crtc); 1173 if (IS_ERR(crtc_state)) 1174 return PTR_ERR(crtc_state); 1175 1176 crtc_state->connector_mask |= 1177 1 << drm_connector_index(conn_state->connector); 1178 } 1179 1180 conn_state->crtc = crtc; 1181 1182 if (crtc) 1183 DRM_DEBUG_ATOMIC("Link connector state %p to [CRTC:%d:%s]\n", 1184 conn_state, crtc->base.id, crtc->name); 1185 else 1186 DRM_DEBUG_ATOMIC("Link connector state %p to [NOCRTC]\n", 1187 conn_state); 1188 1189 return 0; 1190 } 1191 EXPORT_SYMBOL(drm_atomic_set_crtc_for_connector); 1192 1193 /** 1194 * drm_atomic_add_affected_connectors - add connectors for crtc 1195 * @state: atomic state 1196 * @crtc: DRM crtc 1197 * 1198 * This function walks the current configuration and adds all connectors 1199 * currently using @crtc to the atomic configuration @state. Note that this 1200 * function must acquire the connection mutex. This can potentially cause 1201 * unneeded seralization if the update is just for the planes on one crtc. Hence 1202 * drivers and helpers should only call this when really needed (e.g. when a 1203 * full modeset needs to happen due to some change). 1204 * 1205 * Returns: 1206 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK 1207 * then the w/w mutex code has detected a deadlock and the entire atomic 1208 * sequence must be restarted. All other errors are fatal. 1209 */ 1210 int 1211 drm_atomic_add_affected_connectors(struct drm_atomic_state *state, 1212 struct drm_crtc *crtc) 1213 { 1214 struct drm_mode_config *config = &state->dev->mode_config; 1215 struct drm_connector *connector; 1216 struct drm_connector_state *conn_state; 1217 int ret; 1218 1219 ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx); 1220 if (ret) 1221 return ret; 1222 1223 DRM_DEBUG_ATOMIC("Adding all current connectors for [CRTC:%d:%s] to %p\n", 1224 crtc->base.id, crtc->name, state); 1225 1226 /* 1227 * Changed connectors are already in @state, so only need to look at the 1228 * current configuration. 1229 */ 1230 drm_for_each_connector(connector, state->dev) { 1231 if (connector->state->crtc != crtc) 1232 continue; 1233 1234 conn_state = drm_atomic_get_connector_state(state, connector); 1235 if (IS_ERR(conn_state)) 1236 return PTR_ERR(conn_state); 1237 } 1238 1239 return 0; 1240 } 1241 EXPORT_SYMBOL(drm_atomic_add_affected_connectors); 1242 1243 /** 1244 * drm_atomic_add_affected_planes - add planes for crtc 1245 * @state: atomic state 1246 * @crtc: DRM crtc 1247 * 1248 * This function walks the current configuration and adds all planes 1249 * currently used by @crtc to the atomic configuration @state. This is useful 1250 * when an atomic commit also needs to check all currently enabled plane on 1251 * @crtc, e.g. when changing the mode. It's also useful when re-enabling a CRTC 1252 * to avoid special code to force-enable all planes. 1253 * 1254 * Since acquiring a plane state will always also acquire the w/w mutex of the 1255 * current CRTC for that plane (if there is any) adding all the plane states for 1256 * a CRTC will not reduce parallism of atomic updates. 1257 * 1258 * Returns: 1259 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK 1260 * then the w/w mutex code has detected a deadlock and the entire atomic 1261 * sequence must be restarted. All other errors are fatal. 1262 */ 1263 int 1264 drm_atomic_add_affected_planes(struct drm_atomic_state *state, 1265 struct drm_crtc *crtc) 1266 { 1267 struct drm_plane *plane; 1268 1269 WARN_ON(!drm_atomic_get_existing_crtc_state(state, crtc)); 1270 1271 drm_for_each_plane_mask(plane, state->dev, crtc->state->plane_mask) { 1272 struct drm_plane_state *plane_state = 1273 drm_atomic_get_plane_state(state, plane); 1274 1275 if (IS_ERR(plane_state)) 1276 return PTR_ERR(plane_state); 1277 } 1278 return 0; 1279 } 1280 EXPORT_SYMBOL(drm_atomic_add_affected_planes); 1281 1282 /** 1283 * drm_atomic_legacy_backoff - locking backoff for legacy ioctls 1284 * @state: atomic state 1285 * 1286 * This function should be used by legacy entry points which don't understand 1287 * -EDEADLK semantics. For simplicity this one will grab all modeset locks after 1288 * the slowpath completed. 1289 */ 1290 void drm_atomic_legacy_backoff(struct drm_atomic_state *state) 1291 { 1292 int ret; 1293 1294 retry: 1295 drm_modeset_backoff(state->acquire_ctx); 1296 1297 ret = drm_modeset_lock_all_ctx(state->dev, state->acquire_ctx); 1298 if (ret) 1299 goto retry; 1300 } 1301 EXPORT_SYMBOL(drm_atomic_legacy_backoff); 1302 1303 /** 1304 * drm_atomic_check_only - check whether a given config would work 1305 * @state: atomic configuration to check 1306 * 1307 * Note that this function can return -EDEADLK if the driver needed to acquire 1308 * more locks but encountered a deadlock. The caller must then do the usual w/w 1309 * backoff dance and restart. All other errors are fatal. 1310 * 1311 * Returns: 1312 * 0 on success, negative error code on failure. 1313 */ 1314 int drm_atomic_check_only(struct drm_atomic_state *state) 1315 { 1316 struct drm_device *dev = state->dev; 1317 struct drm_mode_config *config = &dev->mode_config; 1318 struct drm_plane *plane; 1319 struct drm_plane_state *plane_state; 1320 struct drm_crtc *crtc; 1321 struct drm_crtc_state *crtc_state; 1322 int i, ret = 0; 1323 1324 DRM_DEBUG_ATOMIC("checking %p\n", state); 1325 1326 for_each_plane_in_state(state, plane, plane_state, i) { 1327 ret = drm_atomic_plane_check(plane, plane_state); 1328 if (ret) { 1329 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] atomic core check failed\n", 1330 plane->base.id, plane->name); 1331 return ret; 1332 } 1333 } 1334 1335 for_each_crtc_in_state(state, crtc, crtc_state, i) { 1336 ret = drm_atomic_crtc_check(crtc, crtc_state); 1337 if (ret) { 1338 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] atomic core check failed\n", 1339 crtc->base.id, crtc->name); 1340 return ret; 1341 } 1342 } 1343 1344 if (config->funcs->atomic_check) 1345 ret = config->funcs->atomic_check(state->dev, state); 1346 1347 if (!state->allow_modeset) { 1348 for_each_crtc_in_state(state, crtc, crtc_state, i) { 1349 if (drm_atomic_crtc_needs_modeset(crtc_state)) { 1350 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] requires full modeset\n", 1351 crtc->base.id, crtc->name); 1352 return -EINVAL; 1353 } 1354 } 1355 } 1356 1357 return ret; 1358 } 1359 EXPORT_SYMBOL(drm_atomic_check_only); 1360 1361 /** 1362 * drm_atomic_commit - commit configuration atomically 1363 * @state: atomic configuration to check 1364 * 1365 * Note that this function can return -EDEADLK if the driver needed to acquire 1366 * more locks but encountered a deadlock. The caller must then do the usual w/w 1367 * backoff dance and restart. All other errors are fatal. 1368 * 1369 * Also note that on successful execution ownership of @state is transferred 1370 * from the caller of this function to the function itself. The caller must not 1371 * free or in any other way access @state. If the function fails then the caller 1372 * must clean up @state itself. 1373 * 1374 * Returns: 1375 * 0 on success, negative error code on failure. 1376 */ 1377 int drm_atomic_commit(struct drm_atomic_state *state) 1378 { 1379 struct drm_mode_config *config = &state->dev->mode_config; 1380 int ret; 1381 1382 ret = drm_atomic_check_only(state); 1383 if (ret) 1384 return ret; 1385 1386 DRM_DEBUG_ATOMIC("commiting %p\n", state); 1387 1388 return config->funcs->atomic_commit(state->dev, state, false); 1389 } 1390 EXPORT_SYMBOL(drm_atomic_commit); 1391 1392 /** 1393 * drm_atomic_async_commit - atomic&async configuration commit 1394 * @state: atomic configuration to check 1395 * 1396 * Note that this function can return -EDEADLK if the driver needed to acquire 1397 * more locks but encountered a deadlock. The caller must then do the usual w/w 1398 * backoff dance and restart. All other errors are fatal. 1399 * 1400 * Also note that on successful execution ownership of @state is transferred 1401 * from the caller of this function to the function itself. The caller must not 1402 * free or in any other way access @state. If the function fails then the caller 1403 * must clean up @state itself. 1404 * 1405 * Returns: 1406 * 0 on success, negative error code on failure. 1407 */ 1408 int drm_atomic_async_commit(struct drm_atomic_state *state) 1409 { 1410 struct drm_mode_config *config = &state->dev->mode_config; 1411 int ret; 1412 1413 ret = drm_atomic_check_only(state); 1414 if (ret) 1415 return ret; 1416 1417 DRM_DEBUG_ATOMIC("commiting %p asynchronously\n", state); 1418 1419 return config->funcs->atomic_commit(state->dev, state, true); 1420 } 1421 EXPORT_SYMBOL(drm_atomic_async_commit); 1422 1423 /* 1424 * The big monstor ioctl 1425 */ 1426 1427 static struct drm_pending_vblank_event *create_vblank_event( 1428 struct drm_device *dev, struct drm_file *file_priv, uint64_t user_data) 1429 { 1430 struct drm_pending_vblank_event *e = NULL; 1431 int ret; 1432 1433 e = kzalloc(sizeof *e, GFP_KERNEL); 1434 if (!e) 1435 return NULL; 1436 1437 e->event.base.type = DRM_EVENT_FLIP_COMPLETE; 1438 e->event.base.length = sizeof(e->event); 1439 e->event.user_data = user_data; 1440 1441 ret = drm_event_reserve_init(dev, file_priv, &e->base, &e->event.base); 1442 if (ret) { 1443 kfree(e); 1444 return NULL; 1445 } 1446 1447 return e; 1448 } 1449 1450 static int atomic_set_prop(struct drm_atomic_state *state, 1451 struct drm_mode_object *obj, struct drm_property *prop, 1452 uint64_t prop_value) 1453 { 1454 struct drm_mode_object *ref; 1455 int ret; 1456 1457 if (!drm_property_change_valid_get(prop, prop_value, &ref)) 1458 return -EINVAL; 1459 1460 switch (obj->type) { 1461 case DRM_MODE_OBJECT_CONNECTOR: { 1462 struct drm_connector *connector = obj_to_connector(obj); 1463 struct drm_connector_state *connector_state; 1464 1465 connector_state = drm_atomic_get_connector_state(state, connector); 1466 if (IS_ERR(connector_state)) { 1467 ret = PTR_ERR(connector_state); 1468 break; 1469 } 1470 1471 ret = drm_atomic_connector_set_property(connector, 1472 connector_state, prop, prop_value); 1473 break; 1474 } 1475 case DRM_MODE_OBJECT_CRTC: { 1476 struct drm_crtc *crtc = obj_to_crtc(obj); 1477 struct drm_crtc_state *crtc_state; 1478 1479 crtc_state = drm_atomic_get_crtc_state(state, crtc); 1480 if (IS_ERR(crtc_state)) { 1481 ret = PTR_ERR(crtc_state); 1482 break; 1483 } 1484 1485 ret = drm_atomic_crtc_set_property(crtc, 1486 crtc_state, prop, prop_value); 1487 break; 1488 } 1489 case DRM_MODE_OBJECT_PLANE: { 1490 struct drm_plane *plane = obj_to_plane(obj); 1491 struct drm_plane_state *plane_state; 1492 1493 plane_state = drm_atomic_get_plane_state(state, plane); 1494 if (IS_ERR(plane_state)) { 1495 ret = PTR_ERR(plane_state); 1496 break; 1497 } 1498 1499 ret = drm_atomic_plane_set_property(plane, 1500 plane_state, prop, prop_value); 1501 break; 1502 } 1503 default: 1504 ret = -EINVAL; 1505 break; 1506 } 1507 1508 drm_property_change_valid_put(prop, ref); 1509 return ret; 1510 } 1511 1512 /** 1513 * drm_atomic_clean_old_fb -- Unset old_fb pointers and set plane->fb pointers. 1514 * 1515 * @dev: drm device to check. 1516 * @plane_mask: plane mask for planes that were updated. 1517 * @ret: return value, can be -EDEADLK for a retry. 1518 * 1519 * Before doing an update plane->old_fb is set to plane->fb, 1520 * but before dropping the locks old_fb needs to be set to NULL 1521 * and plane->fb updated. This is a common operation for each 1522 * atomic update, so this call is split off as a helper. 1523 */ 1524 void drm_atomic_clean_old_fb(struct drm_device *dev, 1525 unsigned plane_mask, 1526 int ret) 1527 { 1528 struct drm_plane *plane; 1529 1530 /* if succeeded, fixup legacy plane crtc/fb ptrs before dropping 1531 * locks (ie. while it is still safe to deref plane->state). We 1532 * need to do this here because the driver entry points cannot 1533 * distinguish between legacy and atomic ioctls. 1534 */ 1535 drm_for_each_plane_mask(plane, dev, plane_mask) { 1536 if (ret == 0) { 1537 struct drm_framebuffer *new_fb = plane->state->fb; 1538 if (new_fb) 1539 drm_framebuffer_reference(new_fb); 1540 plane->fb = new_fb; 1541 plane->crtc = plane->state->crtc; 1542 1543 if (plane->old_fb) 1544 drm_framebuffer_unreference(plane->old_fb); 1545 } 1546 plane->old_fb = NULL; 1547 } 1548 } 1549 EXPORT_SYMBOL(drm_atomic_clean_old_fb); 1550 1551 int drm_mode_atomic_ioctl(struct drm_device *dev, 1552 void *data, struct drm_file *file_priv) 1553 { 1554 struct drm_mode_atomic *arg = data; 1555 uint32_t __user *objs_ptr = (uint32_t __user *)(unsigned long)(arg->objs_ptr); 1556 uint32_t __user *count_props_ptr = (uint32_t __user *)(unsigned long)(arg->count_props_ptr); 1557 uint32_t __user *props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr); 1558 uint64_t __user *prop_values_ptr = (uint64_t __user *)(unsigned long)(arg->prop_values_ptr); 1559 unsigned int copied_objs, copied_props; 1560 struct drm_atomic_state *state; 1561 struct drm_modeset_acquire_ctx ctx; 1562 struct drm_plane *plane; 1563 struct drm_crtc *crtc; 1564 struct drm_crtc_state *crtc_state; 1565 unsigned plane_mask; 1566 int ret = 0; 1567 unsigned int i, j; 1568 1569 /* disallow for drivers not supporting atomic: */ 1570 if (!drm_core_check_feature(dev, DRIVER_ATOMIC)) 1571 return -EINVAL; 1572 1573 /* disallow for userspace that has not enabled atomic cap (even 1574 * though this may be a bit overkill, since legacy userspace 1575 * wouldn't know how to call this ioctl) 1576 */ 1577 if (!file_priv->atomic) 1578 return -EINVAL; 1579 1580 if (arg->flags & ~DRM_MODE_ATOMIC_FLAGS) 1581 return -EINVAL; 1582 1583 if (arg->reserved) 1584 return -EINVAL; 1585 1586 if ((arg->flags & DRM_MODE_PAGE_FLIP_ASYNC) && 1587 !dev->mode_config.async_page_flip) 1588 return -EINVAL; 1589 1590 /* can't test and expect an event at the same time. */ 1591 if ((arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) && 1592 (arg->flags & DRM_MODE_PAGE_FLIP_EVENT)) 1593 return -EINVAL; 1594 1595 drm_modeset_acquire_init(&ctx, 0); 1596 1597 state = drm_atomic_state_alloc(dev); 1598 if (!state) 1599 return -ENOMEM; 1600 1601 state->acquire_ctx = &ctx; 1602 state->allow_modeset = !!(arg->flags & DRM_MODE_ATOMIC_ALLOW_MODESET); 1603 1604 retry: 1605 plane_mask = 0; 1606 copied_objs = 0; 1607 copied_props = 0; 1608 1609 for (i = 0; i < arg->count_objs; i++) { 1610 uint32_t obj_id, count_props; 1611 struct drm_mode_object *obj; 1612 1613 if (get_user(obj_id, objs_ptr + copied_objs)) { 1614 ret = -EFAULT; 1615 goto out; 1616 } 1617 1618 obj = drm_mode_object_find(dev, obj_id, DRM_MODE_OBJECT_ANY); 1619 if (!obj || !obj->properties) { 1620 ret = -ENOENT; 1621 goto out; 1622 } 1623 1624 if (get_user(count_props, count_props_ptr + copied_objs)) { 1625 ret = -EFAULT; 1626 goto out; 1627 } 1628 1629 copied_objs++; 1630 1631 for (j = 0; j < count_props; j++) { 1632 uint32_t prop_id; 1633 uint64_t prop_value; 1634 struct drm_property *prop; 1635 1636 if (get_user(prop_id, props_ptr + copied_props)) { 1637 ret = -EFAULT; 1638 goto out; 1639 } 1640 1641 prop = drm_property_find(dev, prop_id); 1642 if (!prop) { 1643 ret = -ENOENT; 1644 goto out; 1645 } 1646 1647 if (copy_from_user(&prop_value, 1648 prop_values_ptr + copied_props, 1649 sizeof(prop_value))) { 1650 ret = -EFAULT; 1651 goto out; 1652 } 1653 1654 ret = atomic_set_prop(state, obj, prop, prop_value); 1655 if (ret) 1656 goto out; 1657 1658 copied_props++; 1659 } 1660 1661 if (obj->type == DRM_MODE_OBJECT_PLANE && count_props && 1662 !(arg->flags & DRM_MODE_ATOMIC_TEST_ONLY)) { 1663 plane = obj_to_plane(obj); 1664 plane_mask |= (1 << drm_plane_index(plane)); 1665 plane->old_fb = plane->fb; 1666 } 1667 } 1668 1669 if (arg->flags & DRM_MODE_PAGE_FLIP_EVENT) { 1670 for_each_crtc_in_state(state, crtc, crtc_state, i) { 1671 struct drm_pending_vblank_event *e; 1672 1673 e = create_vblank_event(dev, file_priv, arg->user_data); 1674 if (!e) { 1675 ret = -ENOMEM; 1676 goto out; 1677 } 1678 1679 crtc_state->event = e; 1680 } 1681 } 1682 1683 if (arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) { 1684 /* 1685 * Unlike commit, check_only does not clean up state. 1686 * Below we call drm_atomic_state_free for it. 1687 */ 1688 ret = drm_atomic_check_only(state); 1689 } else if (arg->flags & DRM_MODE_ATOMIC_NONBLOCK) { 1690 ret = drm_atomic_async_commit(state); 1691 } else { 1692 ret = drm_atomic_commit(state); 1693 } 1694 1695 out: 1696 drm_atomic_clean_old_fb(dev, plane_mask, ret); 1697 1698 if (ret && arg->flags & DRM_MODE_PAGE_FLIP_EVENT) { 1699 /* 1700 * TEST_ONLY and PAGE_FLIP_EVENT are mutually exclusive, 1701 * if they weren't, this code should be called on success 1702 * for TEST_ONLY too. 1703 */ 1704 1705 for_each_crtc_in_state(state, crtc, crtc_state, i) { 1706 if (!crtc_state->event) 1707 continue; 1708 1709 drm_event_cancel_free(dev, &crtc_state->event->base); 1710 } 1711 } 1712 1713 if (ret == -EDEADLK) { 1714 drm_atomic_state_clear(state); 1715 drm_modeset_backoff(&ctx); 1716 goto retry; 1717 } 1718 1719 if (ret || arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) 1720 drm_atomic_state_free(state); 1721 1722 drm_modeset_drop_locks(&ctx); 1723 drm_modeset_acquire_fini(&ctx); 1724 1725 return ret; 1726 } 1727