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