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