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_mode.h> 32 #include <drm/drm_plane_helper.h> 33 #include <drm/drm_print.h> 34 #include <linux/sync_file.h> 35 36 #include "drm_crtc_internal.h" 37 38 void __drm_crtc_commit_free(struct kref *kref) 39 { 40 struct drm_crtc_commit *commit = 41 container_of(kref, struct drm_crtc_commit, ref); 42 43 kfree(commit); 44 } 45 EXPORT_SYMBOL(__drm_crtc_commit_free); 46 47 /** 48 * drm_atomic_state_default_release - 49 * release memory initialized by drm_atomic_state_init 50 * @state: atomic state 51 * 52 * Free all the memory allocated by drm_atomic_state_init. 53 * This is useful for drivers that subclass the atomic state. 54 */ 55 void drm_atomic_state_default_release(struct drm_atomic_state *state) 56 { 57 kfree(state->connectors); 58 kfree(state->crtcs); 59 kfree(state->planes); 60 } 61 EXPORT_SYMBOL(drm_atomic_state_default_release); 62 63 /** 64 * drm_atomic_state_init - init new atomic state 65 * @dev: DRM device 66 * @state: atomic state 67 * 68 * Default implementation for filling in a new atomic state. 69 * This is useful for drivers that subclass the atomic state. 70 */ 71 int 72 drm_atomic_state_init(struct drm_device *dev, struct drm_atomic_state *state) 73 { 74 kref_init(&state->ref); 75 76 /* TODO legacy paths should maybe do a better job about 77 * setting this appropriately? 78 */ 79 state->allow_modeset = true; 80 81 state->crtcs = kcalloc(dev->mode_config.num_crtc, 82 sizeof(*state->crtcs), GFP_KERNEL); 83 if (!state->crtcs) 84 goto fail; 85 state->planes = kcalloc(dev->mode_config.num_total_plane, 86 sizeof(*state->planes), GFP_KERNEL); 87 if (!state->planes) 88 goto fail; 89 90 state->dev = dev; 91 92 DRM_DEBUG_ATOMIC("Allocated atomic state %p\n", state); 93 94 return 0; 95 fail: 96 drm_atomic_state_default_release(state); 97 return -ENOMEM; 98 } 99 EXPORT_SYMBOL(drm_atomic_state_init); 100 101 /** 102 * drm_atomic_state_alloc - allocate atomic state 103 * @dev: DRM device 104 * 105 * This allocates an empty atomic state to track updates. 106 */ 107 struct drm_atomic_state * 108 drm_atomic_state_alloc(struct drm_device *dev) 109 { 110 struct drm_mode_config *config = &dev->mode_config; 111 struct drm_atomic_state *state; 112 113 if (!config->funcs->atomic_state_alloc) { 114 state = kzalloc(sizeof(*state), GFP_KERNEL); 115 if (!state) 116 return NULL; 117 if (drm_atomic_state_init(dev, state) < 0) { 118 kfree(state); 119 return NULL; 120 } 121 return state; 122 } 123 124 return config->funcs->atomic_state_alloc(dev); 125 } 126 EXPORT_SYMBOL(drm_atomic_state_alloc); 127 128 /** 129 * drm_atomic_state_default_clear - clear base atomic state 130 * @state: atomic state 131 * 132 * Default implementation for clearing atomic state. 133 * This is useful for drivers that subclass the atomic state. 134 */ 135 void drm_atomic_state_default_clear(struct drm_atomic_state *state) 136 { 137 struct drm_device *dev = state->dev; 138 struct drm_mode_config *config = &dev->mode_config; 139 int i; 140 141 DRM_DEBUG_ATOMIC("Clearing atomic state %p\n", state); 142 143 for (i = 0; i < state->num_connector; i++) { 144 struct drm_connector *connector = state->connectors[i].ptr; 145 146 if (!connector) 147 continue; 148 149 connector->funcs->atomic_destroy_state(connector, 150 state->connectors[i].state); 151 state->connectors[i].ptr = NULL; 152 state->connectors[i].state = NULL; 153 drm_connector_unreference(connector); 154 } 155 156 for (i = 0; i < config->num_crtc; i++) { 157 struct drm_crtc *crtc = state->crtcs[i].ptr; 158 159 if (!crtc) 160 continue; 161 162 crtc->funcs->atomic_destroy_state(crtc, 163 state->crtcs[i].state); 164 165 if (state->crtcs[i].commit) { 166 kfree(state->crtcs[i].commit->event); 167 state->crtcs[i].commit->event = NULL; 168 drm_crtc_commit_put(state->crtcs[i].commit); 169 } 170 171 state->crtcs[i].commit = NULL; 172 state->crtcs[i].ptr = NULL; 173 state->crtcs[i].state = NULL; 174 } 175 176 for (i = 0; i < config->num_total_plane; i++) { 177 struct drm_plane *plane = state->planes[i].ptr; 178 179 if (!plane) 180 continue; 181 182 plane->funcs->atomic_destroy_state(plane, 183 state->planes[i].state); 184 state->planes[i].ptr = NULL; 185 state->planes[i].state = NULL; 186 } 187 } 188 EXPORT_SYMBOL(drm_atomic_state_default_clear); 189 190 /** 191 * drm_atomic_state_clear - clear state object 192 * @state: atomic state 193 * 194 * When the w/w mutex algorithm detects a deadlock we need to back off and drop 195 * all locks. So someone else could sneak in and change the current modeset 196 * configuration. Which means that all the state assembled in @state is no 197 * longer an atomic update to the current state, but to some arbitrary earlier 198 * state. Which could break assumptions the driver's 199 * &drm_mode_config_funcs.atomic_check likely relies on. 200 * 201 * Hence we must clear all cached state and completely start over, using this 202 * function. 203 */ 204 void drm_atomic_state_clear(struct drm_atomic_state *state) 205 { 206 struct drm_device *dev = state->dev; 207 struct drm_mode_config *config = &dev->mode_config; 208 209 if (config->funcs->atomic_state_clear) 210 config->funcs->atomic_state_clear(state); 211 else 212 drm_atomic_state_default_clear(state); 213 } 214 EXPORT_SYMBOL(drm_atomic_state_clear); 215 216 /** 217 * __drm_atomic_state_free - free all memory for an atomic state 218 * @ref: This atomic state to deallocate 219 * 220 * This frees all memory associated with an atomic state, including all the 221 * per-object state for planes, crtcs and connectors. 222 */ 223 void __drm_atomic_state_free(struct kref *ref) 224 { 225 struct drm_atomic_state *state = container_of(ref, typeof(*state), ref); 226 struct drm_mode_config *config = &state->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 WARN_ON(!state->acquire_ctx); 264 265 crtc_state = drm_atomic_get_existing_crtc_state(state, crtc); 266 if (crtc_state) 267 return crtc_state; 268 269 ret = drm_modeset_lock(&crtc->mutex, state->acquire_ctx); 270 if (ret) 271 return ERR_PTR(ret); 272 273 crtc_state = crtc->funcs->atomic_duplicate_state(crtc); 274 if (!crtc_state) 275 return ERR_PTR(-ENOMEM); 276 277 state->crtcs[index].state = crtc_state; 278 state->crtcs[index].ptr = crtc; 279 crtc_state->state = state; 280 281 DRM_DEBUG_ATOMIC("Added [CRTC:%d:%s] %p state to %p\n", 282 crtc->base.id, crtc->name, crtc_state, state); 283 284 return crtc_state; 285 } 286 EXPORT_SYMBOL(drm_atomic_get_crtc_state); 287 288 static void set_out_fence_for_crtc(struct drm_atomic_state *state, 289 struct drm_crtc *crtc, s32 __user *fence_ptr) 290 { 291 state->crtcs[drm_crtc_index(crtc)].out_fence_ptr = fence_ptr; 292 } 293 294 static s32 __user *get_out_fence_for_crtc(struct drm_atomic_state *state, 295 struct drm_crtc *crtc) 296 { 297 s32 __user *fence_ptr; 298 299 fence_ptr = state->crtcs[drm_crtc_index(crtc)].out_fence_ptr; 300 state->crtcs[drm_crtc_index(crtc)].out_fence_ptr = NULL; 301 302 return fence_ptr; 303 } 304 305 /** 306 * drm_atomic_set_mode_for_crtc - set mode for CRTC 307 * @state: the CRTC whose incoming state to update 308 * @mode: kernel-internal mode to use for the CRTC, or NULL to disable 309 * 310 * Set a mode (originating from the kernel) on the desired CRTC state and update 311 * the enable property. 312 * 313 * RETURNS: 314 * Zero on success, error code on failure. Cannot return -EDEADLK. 315 */ 316 int drm_atomic_set_mode_for_crtc(struct drm_crtc_state *state, 317 struct drm_display_mode *mode) 318 { 319 struct drm_mode_modeinfo umode; 320 321 /* Early return for no change. */ 322 if (mode && memcmp(&state->mode, mode, sizeof(*mode)) == 0) 323 return 0; 324 325 drm_property_unreference_blob(state->mode_blob); 326 state->mode_blob = NULL; 327 328 if (mode) { 329 drm_mode_convert_to_umode(&umode, mode); 330 state->mode_blob = 331 drm_property_create_blob(state->crtc->dev, 332 sizeof(umode), 333 &umode); 334 if (IS_ERR(state->mode_blob)) 335 return PTR_ERR(state->mode_blob); 336 337 drm_mode_copy(&state->mode, mode); 338 state->enable = true; 339 DRM_DEBUG_ATOMIC("Set [MODE:%s] for CRTC state %p\n", 340 mode->name, state); 341 } else { 342 memset(&state->mode, 0, sizeof(state->mode)); 343 state->enable = false; 344 DRM_DEBUG_ATOMIC("Set [NOMODE] for CRTC state %p\n", 345 state); 346 } 347 348 return 0; 349 } 350 EXPORT_SYMBOL(drm_atomic_set_mode_for_crtc); 351 352 /** 353 * drm_atomic_set_mode_prop_for_crtc - set mode for CRTC 354 * @state: the CRTC whose incoming state to update 355 * @blob: pointer to blob property to use for mode 356 * 357 * Set a mode (originating from a blob property) on the desired CRTC state. 358 * This function will take a reference on the blob property for the CRTC state, 359 * and release the reference held on the state's existing mode property, if any 360 * was set. 361 * 362 * RETURNS: 363 * Zero on success, error code on failure. Cannot return -EDEADLK. 364 */ 365 int drm_atomic_set_mode_prop_for_crtc(struct drm_crtc_state *state, 366 struct drm_property_blob *blob) 367 { 368 if (blob == state->mode_blob) 369 return 0; 370 371 drm_property_unreference_blob(state->mode_blob); 372 state->mode_blob = NULL; 373 374 memset(&state->mode, 0, sizeof(state->mode)); 375 376 if (blob) { 377 if (blob->length != sizeof(struct drm_mode_modeinfo) || 378 drm_mode_convert_umode(&state->mode, 379 (const struct drm_mode_modeinfo *) 380 blob->data)) 381 return -EINVAL; 382 383 state->mode_blob = drm_property_reference_blob(blob); 384 state->enable = true; 385 DRM_DEBUG_ATOMIC("Set [MODE:%s] for CRTC state %p\n", 386 state->mode.name, state); 387 } else { 388 state->enable = false; 389 DRM_DEBUG_ATOMIC("Set [NOMODE] for CRTC state %p\n", 390 state); 391 } 392 393 return 0; 394 } 395 EXPORT_SYMBOL(drm_atomic_set_mode_prop_for_crtc); 396 397 /** 398 * drm_atomic_replace_property_blob - replace a blob property 399 * @blob: a pointer to the member blob to be replaced 400 * @new_blob: the new blob to replace with 401 * @replaced: whether the blob has been replaced 402 * 403 * RETURNS: 404 * Zero on success, error code on failure 405 */ 406 static void 407 drm_atomic_replace_property_blob(struct drm_property_blob **blob, 408 struct drm_property_blob *new_blob, 409 bool *replaced) 410 { 411 struct drm_property_blob *old_blob = *blob; 412 413 if (old_blob == new_blob) 414 return; 415 416 drm_property_unreference_blob(old_blob); 417 if (new_blob) 418 drm_property_reference_blob(new_blob); 419 *blob = new_blob; 420 *replaced = true; 421 422 return; 423 } 424 425 static int 426 drm_atomic_replace_property_blob_from_id(struct drm_crtc *crtc, 427 struct drm_property_blob **blob, 428 uint64_t blob_id, 429 ssize_t expected_size, 430 bool *replaced) 431 { 432 struct drm_property_blob *new_blob = NULL; 433 434 if (blob_id != 0) { 435 new_blob = drm_property_lookup_blob(crtc->dev, blob_id); 436 if (new_blob == NULL) 437 return -EINVAL; 438 439 if (expected_size > 0 && expected_size != new_blob->length) { 440 drm_property_unreference_blob(new_blob); 441 return -EINVAL; 442 } 443 } 444 445 drm_atomic_replace_property_blob(blob, new_blob, replaced); 446 drm_property_unreference_blob(new_blob); 447 448 return 0; 449 } 450 451 /** 452 * drm_atomic_crtc_set_property - set property on CRTC 453 * @crtc: the drm CRTC to set a property on 454 * @state: the state object to update with the new property value 455 * @property: the property to set 456 * @val: the new property value 457 * 458 * This function handles generic/core properties and calls out to driver's 459 * &drm_crtc_funcs.atomic_set_property for driver properties. To ensure 460 * consistent behavior you must call this function rather than the driver hook 461 * directly. 462 * 463 * RETURNS: 464 * Zero on success, error code on failure 465 */ 466 int drm_atomic_crtc_set_property(struct drm_crtc *crtc, 467 struct drm_crtc_state *state, struct drm_property *property, 468 uint64_t val) 469 { 470 struct drm_device *dev = crtc->dev; 471 struct drm_mode_config *config = &dev->mode_config; 472 bool replaced = false; 473 int ret; 474 475 if (property == config->prop_active) 476 state->active = val; 477 else if (property == config->prop_mode_id) { 478 struct drm_property_blob *mode = 479 drm_property_lookup_blob(dev, val); 480 ret = drm_atomic_set_mode_prop_for_crtc(state, mode); 481 drm_property_unreference_blob(mode); 482 return ret; 483 } else if (property == config->degamma_lut_property) { 484 ret = drm_atomic_replace_property_blob_from_id(crtc, 485 &state->degamma_lut, 486 val, 487 -1, 488 &replaced); 489 state->color_mgmt_changed |= replaced; 490 return ret; 491 } else if (property == config->ctm_property) { 492 ret = drm_atomic_replace_property_blob_from_id(crtc, 493 &state->ctm, 494 val, 495 sizeof(struct drm_color_ctm), 496 &replaced); 497 state->color_mgmt_changed |= replaced; 498 return ret; 499 } else if (property == config->gamma_lut_property) { 500 ret = drm_atomic_replace_property_blob_from_id(crtc, 501 &state->gamma_lut, 502 val, 503 -1, 504 &replaced); 505 state->color_mgmt_changed |= replaced; 506 return ret; 507 } else if (property == config->prop_out_fence_ptr) { 508 s32 __user *fence_ptr = u64_to_user_ptr(val); 509 510 if (!fence_ptr) 511 return 0; 512 513 if (put_user(-1, fence_ptr)) 514 return -EFAULT; 515 516 set_out_fence_for_crtc(state->state, crtc, fence_ptr); 517 } else if (crtc->funcs->atomic_set_property) 518 return crtc->funcs->atomic_set_property(crtc, state, property, val); 519 else 520 return -EINVAL; 521 522 return 0; 523 } 524 EXPORT_SYMBOL(drm_atomic_crtc_set_property); 525 526 /** 527 * drm_atomic_crtc_get_property - get property value from CRTC state 528 * @crtc: the drm CRTC to set a property on 529 * @state: the state object to get the property value from 530 * @property: the property to set 531 * @val: return location for the property value 532 * 533 * This function handles generic/core properties and calls out to driver's 534 * &drm_crtc_funcs.atomic_get_property for driver properties. To ensure 535 * consistent behavior you must call this function rather than the driver hook 536 * directly. 537 * 538 * RETURNS: 539 * Zero on success, error code on failure 540 */ 541 static int 542 drm_atomic_crtc_get_property(struct drm_crtc *crtc, 543 const struct drm_crtc_state *state, 544 struct drm_property *property, uint64_t *val) 545 { 546 struct drm_device *dev = crtc->dev; 547 struct drm_mode_config *config = &dev->mode_config; 548 549 if (property == config->prop_active) 550 *val = state->active; 551 else if (property == config->prop_mode_id) 552 *val = (state->mode_blob) ? state->mode_blob->base.id : 0; 553 else if (property == config->degamma_lut_property) 554 *val = (state->degamma_lut) ? state->degamma_lut->base.id : 0; 555 else if (property == config->ctm_property) 556 *val = (state->ctm) ? state->ctm->base.id : 0; 557 else if (property == config->gamma_lut_property) 558 *val = (state->gamma_lut) ? state->gamma_lut->base.id : 0; 559 else if (property == config->prop_out_fence_ptr) 560 *val = 0; 561 else if (crtc->funcs->atomic_get_property) 562 return crtc->funcs->atomic_get_property(crtc, state, property, val); 563 else 564 return -EINVAL; 565 566 return 0; 567 } 568 569 /** 570 * drm_atomic_crtc_check - check crtc state 571 * @crtc: crtc to check 572 * @state: crtc state to check 573 * 574 * Provides core sanity checks for crtc state. 575 * 576 * RETURNS: 577 * Zero on success, error code on failure 578 */ 579 static int drm_atomic_crtc_check(struct drm_crtc *crtc, 580 struct drm_crtc_state *state) 581 { 582 /* NOTE: we explicitly don't enforce constraints such as primary 583 * layer covering entire screen, since that is something we want 584 * to allow (on hw that supports it). For hw that does not, it 585 * should be checked in driver's crtc->atomic_check() vfunc. 586 * 587 * TODO: Add generic modeset state checks once we support those. 588 */ 589 590 if (state->active && !state->enable) { 591 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] active without enabled\n", 592 crtc->base.id, crtc->name); 593 return -EINVAL; 594 } 595 596 /* The state->enable vs. state->mode_blob checks can be WARN_ON, 597 * as this is a kernel-internal detail that userspace should never 598 * be able to trigger. */ 599 if (drm_core_check_feature(crtc->dev, DRIVER_ATOMIC) && 600 WARN_ON(state->enable && !state->mode_blob)) { 601 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] enabled without mode blob\n", 602 crtc->base.id, crtc->name); 603 return -EINVAL; 604 } 605 606 if (drm_core_check_feature(crtc->dev, DRIVER_ATOMIC) && 607 WARN_ON(!state->enable && state->mode_blob)) { 608 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] disabled with mode blob\n", 609 crtc->base.id, crtc->name); 610 return -EINVAL; 611 } 612 613 /* 614 * Reject event generation for when a CRTC is off and stays off. 615 * It wouldn't be hard to implement this, but userspace has a track 616 * record of happily burning through 100% cpu (or worse, crash) when the 617 * display pipe is suspended. To avoid all that fun just reject updates 618 * that ask for events since likely that indicates a bug in the 619 * compositor's drawing loop. This is consistent with the vblank IOCTL 620 * and legacy page_flip IOCTL which also reject service on a disabled 621 * pipe. 622 */ 623 if (state->event && !state->active && !crtc->state->active) { 624 DRM_DEBUG_ATOMIC("[CRTC:%d] requesting event but off\n", 625 crtc->base.id); 626 return -EINVAL; 627 } 628 629 return 0; 630 } 631 632 static void drm_atomic_crtc_print_state(struct drm_printer *p, 633 const struct drm_crtc_state *state) 634 { 635 struct drm_crtc *crtc = state->crtc; 636 637 drm_printf(p, "crtc[%u]: %s\n", crtc->base.id, crtc->name); 638 drm_printf(p, "\tenable=%d\n", state->enable); 639 drm_printf(p, "\tactive=%d\n", state->active); 640 drm_printf(p, "\tplanes_changed=%d\n", state->planes_changed); 641 drm_printf(p, "\tmode_changed=%d\n", state->mode_changed); 642 drm_printf(p, "\tactive_changed=%d\n", state->active_changed); 643 drm_printf(p, "\tconnectors_changed=%d\n", state->connectors_changed); 644 drm_printf(p, "\tcolor_mgmt_changed=%d\n", state->color_mgmt_changed); 645 drm_printf(p, "\tplane_mask=%x\n", state->plane_mask); 646 drm_printf(p, "\tconnector_mask=%x\n", state->connector_mask); 647 drm_printf(p, "\tencoder_mask=%x\n", state->encoder_mask); 648 drm_printf(p, "\tmode: " DRM_MODE_FMT "\n", DRM_MODE_ARG(&state->mode)); 649 650 if (crtc->funcs->atomic_print_state) 651 crtc->funcs->atomic_print_state(p, state); 652 } 653 654 /** 655 * drm_atomic_get_plane_state - get plane state 656 * @state: global atomic state object 657 * @plane: plane to get state object for 658 * 659 * This function returns the plane state for the given plane, allocating it if 660 * needed. It will also grab the relevant plane lock to make sure that the state 661 * is consistent. 662 * 663 * Returns: 664 * 665 * Either the allocated state or the error code encoded into the pointer. When 666 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the 667 * entire atomic sequence must be restarted. All other errors are fatal. 668 */ 669 struct drm_plane_state * 670 drm_atomic_get_plane_state(struct drm_atomic_state *state, 671 struct drm_plane *plane) 672 { 673 int ret, index = drm_plane_index(plane); 674 struct drm_plane_state *plane_state; 675 676 WARN_ON(!state->acquire_ctx); 677 678 plane_state = drm_atomic_get_existing_plane_state(state, plane); 679 if (plane_state) 680 return plane_state; 681 682 ret = drm_modeset_lock(&plane->mutex, state->acquire_ctx); 683 if (ret) 684 return ERR_PTR(ret); 685 686 plane_state = plane->funcs->atomic_duplicate_state(plane); 687 if (!plane_state) 688 return ERR_PTR(-ENOMEM); 689 690 state->planes[index].state = plane_state; 691 state->planes[index].ptr = plane; 692 plane_state->state = state; 693 694 DRM_DEBUG_ATOMIC("Added [PLANE:%d:%s] %p state to %p\n", 695 plane->base.id, plane->name, plane_state, state); 696 697 if (plane_state->crtc) { 698 struct drm_crtc_state *crtc_state; 699 700 crtc_state = drm_atomic_get_crtc_state(state, 701 plane_state->crtc); 702 if (IS_ERR(crtc_state)) 703 return ERR_CAST(crtc_state); 704 } 705 706 return plane_state; 707 } 708 EXPORT_SYMBOL(drm_atomic_get_plane_state); 709 710 /** 711 * drm_atomic_plane_set_property - set property on plane 712 * @plane: the drm plane to set a property on 713 * @state: the state object to update with the new property value 714 * @property: the property to set 715 * @val: the new property value 716 * 717 * This function handles generic/core properties and calls out to driver's 718 * &drm_plane_funcs.atomic_set_property for driver properties. To ensure 719 * consistent behavior you must call this function rather than the driver hook 720 * directly. 721 * 722 * RETURNS: 723 * Zero on success, error code on failure 724 */ 725 int drm_atomic_plane_set_property(struct drm_plane *plane, 726 struct drm_plane_state *state, struct drm_property *property, 727 uint64_t val) 728 { 729 struct drm_device *dev = plane->dev; 730 struct drm_mode_config *config = &dev->mode_config; 731 732 if (property == config->prop_fb_id) { 733 struct drm_framebuffer *fb = drm_framebuffer_lookup(dev, val); 734 drm_atomic_set_fb_for_plane(state, fb); 735 if (fb) 736 drm_framebuffer_unreference(fb); 737 } else if (property == config->prop_in_fence_fd) { 738 if (state->fence) 739 return -EINVAL; 740 741 if (U642I64(val) == -1) 742 return 0; 743 744 state->fence = sync_file_get_fence(val); 745 if (!state->fence) 746 return -EINVAL; 747 748 } else if (property == config->prop_crtc_id) { 749 struct drm_crtc *crtc = drm_crtc_find(dev, val); 750 return drm_atomic_set_crtc_for_plane(state, crtc); 751 } else if (property == config->prop_crtc_x) { 752 state->crtc_x = U642I64(val); 753 } else if (property == config->prop_crtc_y) { 754 state->crtc_y = U642I64(val); 755 } else if (property == config->prop_crtc_w) { 756 state->crtc_w = val; 757 } else if (property == config->prop_crtc_h) { 758 state->crtc_h = val; 759 } else if (property == config->prop_src_x) { 760 state->src_x = val; 761 } else if (property == config->prop_src_y) { 762 state->src_y = val; 763 } else if (property == config->prop_src_w) { 764 state->src_w = val; 765 } else if (property == config->prop_src_h) { 766 state->src_h = val; 767 } else if (property == plane->rotation_property) { 768 if (!is_power_of_2(val & DRM_ROTATE_MASK)) 769 return -EINVAL; 770 state->rotation = val; 771 } else if (property == plane->zpos_property) { 772 state->zpos = val; 773 } else if (plane->funcs->atomic_set_property) { 774 return plane->funcs->atomic_set_property(plane, state, 775 property, val); 776 } else { 777 return -EINVAL; 778 } 779 780 return 0; 781 } 782 EXPORT_SYMBOL(drm_atomic_plane_set_property); 783 784 /** 785 * drm_atomic_plane_get_property - get property value from plane state 786 * @plane: the drm plane to set a property on 787 * @state: the state object to get the property value from 788 * @property: the property to set 789 * @val: return location for the property value 790 * 791 * This function handles generic/core properties and calls out to driver's 792 * &drm_plane_funcs.atomic_get_property for driver properties. To ensure 793 * consistent behavior you must call this function rather than the driver hook 794 * directly. 795 * 796 * RETURNS: 797 * Zero on success, error code on failure 798 */ 799 static int 800 drm_atomic_plane_get_property(struct drm_plane *plane, 801 const struct drm_plane_state *state, 802 struct drm_property *property, uint64_t *val) 803 { 804 struct drm_device *dev = plane->dev; 805 struct drm_mode_config *config = &dev->mode_config; 806 807 if (property == config->prop_fb_id) { 808 *val = (state->fb) ? state->fb->base.id : 0; 809 } else if (property == config->prop_in_fence_fd) { 810 *val = -1; 811 } else if (property == config->prop_crtc_id) { 812 *val = (state->crtc) ? state->crtc->base.id : 0; 813 } else if (property == config->prop_crtc_x) { 814 *val = I642U64(state->crtc_x); 815 } else if (property == config->prop_crtc_y) { 816 *val = I642U64(state->crtc_y); 817 } else if (property == config->prop_crtc_w) { 818 *val = state->crtc_w; 819 } else if (property == config->prop_crtc_h) { 820 *val = state->crtc_h; 821 } else if (property == config->prop_src_x) { 822 *val = state->src_x; 823 } else if (property == config->prop_src_y) { 824 *val = state->src_y; 825 } else if (property == config->prop_src_w) { 826 *val = state->src_w; 827 } else if (property == config->prop_src_h) { 828 *val = state->src_h; 829 } else if (property == plane->rotation_property) { 830 *val = state->rotation; 831 } else if (property == plane->zpos_property) { 832 *val = state->zpos; 833 } else if (plane->funcs->atomic_get_property) { 834 return plane->funcs->atomic_get_property(plane, state, property, val); 835 } else { 836 return -EINVAL; 837 } 838 839 return 0; 840 } 841 842 static bool 843 plane_switching_crtc(struct drm_atomic_state *state, 844 struct drm_plane *plane, 845 struct drm_plane_state *plane_state) 846 { 847 if (!plane->state->crtc || !plane_state->crtc) 848 return false; 849 850 if (plane->state->crtc == plane_state->crtc) 851 return false; 852 853 /* This could be refined, but currently there's no helper or driver code 854 * to implement direct switching of active planes nor userspace to take 855 * advantage of more direct plane switching without the intermediate 856 * full OFF state. 857 */ 858 return true; 859 } 860 861 /** 862 * drm_atomic_plane_check - check plane state 863 * @plane: plane to check 864 * @state: plane state to check 865 * 866 * Provides core sanity checks for plane state. 867 * 868 * RETURNS: 869 * Zero on success, error code on failure 870 */ 871 static int drm_atomic_plane_check(struct drm_plane *plane, 872 struct drm_plane_state *state) 873 { 874 unsigned int fb_width, fb_height; 875 int ret; 876 877 /* either *both* CRTC and FB must be set, or neither */ 878 if (WARN_ON(state->crtc && !state->fb)) { 879 DRM_DEBUG_ATOMIC("CRTC set but no FB\n"); 880 return -EINVAL; 881 } else if (WARN_ON(state->fb && !state->crtc)) { 882 DRM_DEBUG_ATOMIC("FB set but no CRTC\n"); 883 return -EINVAL; 884 } 885 886 /* if disabled, we don't care about the rest of the state: */ 887 if (!state->crtc) 888 return 0; 889 890 /* Check whether this plane is usable on this CRTC */ 891 if (!(plane->possible_crtcs & drm_crtc_mask(state->crtc))) { 892 DRM_DEBUG_ATOMIC("Invalid crtc for plane\n"); 893 return -EINVAL; 894 } 895 896 /* Check whether this plane supports the fb pixel format. */ 897 ret = drm_plane_check_pixel_format(plane, state->fb->format->format); 898 if (ret) { 899 struct drm_format_name_buf format_name; 900 DRM_DEBUG_ATOMIC("Invalid pixel format %s\n", 901 drm_get_format_name(state->fb->format->format, 902 &format_name)); 903 return ret; 904 } 905 906 /* Give drivers some help against integer overflows */ 907 if (state->crtc_w > INT_MAX || 908 state->crtc_x > INT_MAX - (int32_t) state->crtc_w || 909 state->crtc_h > INT_MAX || 910 state->crtc_y > INT_MAX - (int32_t) state->crtc_h) { 911 DRM_DEBUG_ATOMIC("Invalid CRTC coordinates %ux%u+%d+%d\n", 912 state->crtc_w, state->crtc_h, 913 state->crtc_x, state->crtc_y); 914 return -ERANGE; 915 } 916 917 fb_width = state->fb->width << 16; 918 fb_height = state->fb->height << 16; 919 920 /* Make sure source coordinates are inside the fb. */ 921 if (state->src_w > fb_width || 922 state->src_x > fb_width - state->src_w || 923 state->src_h > fb_height || 924 state->src_y > fb_height - state->src_h) { 925 DRM_DEBUG_ATOMIC("Invalid source coordinates " 926 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n", 927 state->src_w >> 16, ((state->src_w & 0xffff) * 15625) >> 10, 928 state->src_h >> 16, ((state->src_h & 0xffff) * 15625) >> 10, 929 state->src_x >> 16, ((state->src_x & 0xffff) * 15625) >> 10, 930 state->src_y >> 16, ((state->src_y & 0xffff) * 15625) >> 10); 931 return -ENOSPC; 932 } 933 934 if (plane_switching_crtc(state->state, plane, state)) { 935 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] switching CRTC directly\n", 936 plane->base.id, plane->name); 937 return -EINVAL; 938 } 939 940 return 0; 941 } 942 943 static void drm_atomic_plane_print_state(struct drm_printer *p, 944 const struct drm_plane_state *state) 945 { 946 struct drm_plane *plane = state->plane; 947 struct drm_rect src = drm_plane_state_src(state); 948 struct drm_rect dest = drm_plane_state_dest(state); 949 950 drm_printf(p, "plane[%u]: %s\n", plane->base.id, plane->name); 951 drm_printf(p, "\tcrtc=%s\n", state->crtc ? state->crtc->name : "(null)"); 952 drm_printf(p, "\tfb=%u\n", state->fb ? state->fb->base.id : 0); 953 if (state->fb) { 954 struct drm_framebuffer *fb = state->fb; 955 int i, n = fb->format->num_planes; 956 struct drm_format_name_buf format_name; 957 958 drm_printf(p, "\t\tformat=%s\n", 959 drm_get_format_name(fb->format->format, &format_name)); 960 drm_printf(p, "\t\t\tmodifier=0x%llx\n", fb->modifier); 961 drm_printf(p, "\t\tsize=%dx%d\n", fb->width, fb->height); 962 drm_printf(p, "\t\tlayers:\n"); 963 for (i = 0; i < n; i++) { 964 drm_printf(p, "\t\t\tpitch[%d]=%u\n", i, fb->pitches[i]); 965 drm_printf(p, "\t\t\toffset[%d]=%u\n", i, fb->offsets[i]); 966 } 967 } 968 drm_printf(p, "\tcrtc-pos=" DRM_RECT_FMT "\n", DRM_RECT_ARG(&dest)); 969 drm_printf(p, "\tsrc-pos=" DRM_RECT_FP_FMT "\n", DRM_RECT_FP_ARG(&src)); 970 drm_printf(p, "\trotation=%x\n", state->rotation); 971 972 if (plane->funcs->atomic_print_state) 973 plane->funcs->atomic_print_state(p, state); 974 } 975 976 /** 977 * drm_atomic_get_connector_state - get connector state 978 * @state: global atomic state object 979 * @connector: connector to get state object for 980 * 981 * This function returns the connector state for the given connector, 982 * allocating it if needed. It will also grab the relevant connector lock to 983 * make sure that the state is consistent. 984 * 985 * Returns: 986 * 987 * Either the allocated state or the error code encoded into the pointer. When 988 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the 989 * entire atomic sequence must be restarted. All other errors are fatal. 990 */ 991 struct drm_connector_state * 992 drm_atomic_get_connector_state(struct drm_atomic_state *state, 993 struct drm_connector *connector) 994 { 995 int ret, index; 996 struct drm_mode_config *config = &connector->dev->mode_config; 997 struct drm_connector_state *connector_state; 998 999 WARN_ON(!state->acquire_ctx); 1000 1001 ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx); 1002 if (ret) 1003 return ERR_PTR(ret); 1004 1005 index = drm_connector_index(connector); 1006 1007 if (index >= state->num_connector) { 1008 struct __drm_connnectors_state *c; 1009 int alloc = max(index + 1, config->num_connector); 1010 1011 c = krealloc(state->connectors, alloc * sizeof(*state->connectors), GFP_KERNEL); 1012 if (!c) 1013 return ERR_PTR(-ENOMEM); 1014 1015 state->connectors = c; 1016 memset(&state->connectors[state->num_connector], 0, 1017 sizeof(*state->connectors) * (alloc - state->num_connector)); 1018 1019 state->num_connector = alloc; 1020 } 1021 1022 if (state->connectors[index].state) 1023 return state->connectors[index].state; 1024 1025 connector_state = connector->funcs->atomic_duplicate_state(connector); 1026 if (!connector_state) 1027 return ERR_PTR(-ENOMEM); 1028 1029 drm_connector_reference(connector); 1030 state->connectors[index].state = connector_state; 1031 state->connectors[index].ptr = connector; 1032 connector_state->state = state; 1033 1034 DRM_DEBUG_ATOMIC("Added [CONNECTOR:%d] %p state to %p\n", 1035 connector->base.id, connector_state, state); 1036 1037 if (connector_state->crtc) { 1038 struct drm_crtc_state *crtc_state; 1039 1040 crtc_state = drm_atomic_get_crtc_state(state, 1041 connector_state->crtc); 1042 if (IS_ERR(crtc_state)) 1043 return ERR_CAST(crtc_state); 1044 } 1045 1046 return connector_state; 1047 } 1048 EXPORT_SYMBOL(drm_atomic_get_connector_state); 1049 1050 /** 1051 * drm_atomic_connector_set_property - set property on connector. 1052 * @connector: the drm connector to set a property on 1053 * @state: the state object to update with the new property value 1054 * @property: the property to set 1055 * @val: the new property value 1056 * 1057 * This function handles generic/core properties and calls out to driver's 1058 * &drm_connector_funcs.atomic_set_property for driver properties. To ensure 1059 * consistent behavior you must call this function rather than the driver hook 1060 * directly. 1061 * 1062 * RETURNS: 1063 * Zero on success, error code on failure 1064 */ 1065 int drm_atomic_connector_set_property(struct drm_connector *connector, 1066 struct drm_connector_state *state, struct drm_property *property, 1067 uint64_t val) 1068 { 1069 struct drm_device *dev = connector->dev; 1070 struct drm_mode_config *config = &dev->mode_config; 1071 1072 if (property == config->prop_crtc_id) { 1073 struct drm_crtc *crtc = drm_crtc_find(dev, val); 1074 return drm_atomic_set_crtc_for_connector(state, crtc); 1075 } else if (property == config->dpms_property) { 1076 /* setting DPMS property requires special handling, which 1077 * is done in legacy setprop path for us. Disallow (for 1078 * now?) atomic writes to DPMS property: 1079 */ 1080 return -EINVAL; 1081 } else if (property == config->tv_select_subconnector_property) { 1082 state->tv.subconnector = val; 1083 } else if (property == config->tv_left_margin_property) { 1084 state->tv.margins.left = val; 1085 } else if (property == config->tv_right_margin_property) { 1086 state->tv.margins.right = val; 1087 } else if (property == config->tv_top_margin_property) { 1088 state->tv.margins.top = val; 1089 } else if (property == config->tv_bottom_margin_property) { 1090 state->tv.margins.bottom = val; 1091 } else if (property == config->tv_mode_property) { 1092 state->tv.mode = val; 1093 } else if (property == config->tv_brightness_property) { 1094 state->tv.brightness = val; 1095 } else if (property == config->tv_contrast_property) { 1096 state->tv.contrast = val; 1097 } else if (property == config->tv_flicker_reduction_property) { 1098 state->tv.flicker_reduction = val; 1099 } else if (property == config->tv_overscan_property) { 1100 state->tv.overscan = val; 1101 } else if (property == config->tv_saturation_property) { 1102 state->tv.saturation = val; 1103 } else if (property == config->tv_hue_property) { 1104 state->tv.hue = val; 1105 } else if (connector->funcs->atomic_set_property) { 1106 return connector->funcs->atomic_set_property(connector, 1107 state, property, val); 1108 } else { 1109 return -EINVAL; 1110 } 1111 1112 return 0; 1113 } 1114 EXPORT_SYMBOL(drm_atomic_connector_set_property); 1115 1116 static void drm_atomic_connector_print_state(struct drm_printer *p, 1117 const struct drm_connector_state *state) 1118 { 1119 struct drm_connector *connector = state->connector; 1120 1121 drm_printf(p, "connector[%u]: %s\n", connector->base.id, connector->name); 1122 drm_printf(p, "\tcrtc=%s\n", state->crtc ? state->crtc->name : "(null)"); 1123 1124 if (connector->funcs->atomic_print_state) 1125 connector->funcs->atomic_print_state(p, state); 1126 } 1127 1128 /** 1129 * drm_atomic_connector_get_property - get property value from connector state 1130 * @connector: the drm connector to set a property on 1131 * @state: the state object to get the property value from 1132 * @property: the property to set 1133 * @val: return location for the property value 1134 * 1135 * This function handles generic/core properties and calls out to driver's 1136 * &drm_connector_funcs.atomic_get_property for driver properties. To ensure 1137 * consistent behavior you must call this function rather than the driver hook 1138 * directly. 1139 * 1140 * RETURNS: 1141 * Zero on success, error code on failure 1142 */ 1143 static int 1144 drm_atomic_connector_get_property(struct drm_connector *connector, 1145 const struct drm_connector_state *state, 1146 struct drm_property *property, uint64_t *val) 1147 { 1148 struct drm_device *dev = connector->dev; 1149 struct drm_mode_config *config = &dev->mode_config; 1150 1151 if (property == config->prop_crtc_id) { 1152 *val = (state->crtc) ? state->crtc->base.id : 0; 1153 } else if (property == config->dpms_property) { 1154 *val = connector->dpms; 1155 } else if (property == config->tv_select_subconnector_property) { 1156 *val = state->tv.subconnector; 1157 } else if (property == config->tv_left_margin_property) { 1158 *val = state->tv.margins.left; 1159 } else if (property == config->tv_right_margin_property) { 1160 *val = state->tv.margins.right; 1161 } else if (property == config->tv_top_margin_property) { 1162 *val = state->tv.margins.top; 1163 } else if (property == config->tv_bottom_margin_property) { 1164 *val = state->tv.margins.bottom; 1165 } else if (property == config->tv_mode_property) { 1166 *val = state->tv.mode; 1167 } else if (property == config->tv_brightness_property) { 1168 *val = state->tv.brightness; 1169 } else if (property == config->tv_contrast_property) { 1170 *val = state->tv.contrast; 1171 } else if (property == config->tv_flicker_reduction_property) { 1172 *val = state->tv.flicker_reduction; 1173 } else if (property == config->tv_overscan_property) { 1174 *val = state->tv.overscan; 1175 } else if (property == config->tv_saturation_property) { 1176 *val = state->tv.saturation; 1177 } else if (property == config->tv_hue_property) { 1178 *val = state->tv.hue; 1179 } else if (connector->funcs->atomic_get_property) { 1180 return connector->funcs->atomic_get_property(connector, 1181 state, property, val); 1182 } else { 1183 return -EINVAL; 1184 } 1185 1186 return 0; 1187 } 1188 1189 int drm_atomic_get_property(struct drm_mode_object *obj, 1190 struct drm_property *property, uint64_t *val) 1191 { 1192 struct drm_device *dev = property->dev; 1193 int ret; 1194 1195 switch (obj->type) { 1196 case DRM_MODE_OBJECT_CONNECTOR: { 1197 struct drm_connector *connector = obj_to_connector(obj); 1198 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex)); 1199 ret = drm_atomic_connector_get_property(connector, 1200 connector->state, property, val); 1201 break; 1202 } 1203 case DRM_MODE_OBJECT_CRTC: { 1204 struct drm_crtc *crtc = obj_to_crtc(obj); 1205 WARN_ON(!drm_modeset_is_locked(&crtc->mutex)); 1206 ret = drm_atomic_crtc_get_property(crtc, 1207 crtc->state, property, val); 1208 break; 1209 } 1210 case DRM_MODE_OBJECT_PLANE: { 1211 struct drm_plane *plane = obj_to_plane(obj); 1212 WARN_ON(!drm_modeset_is_locked(&plane->mutex)); 1213 ret = drm_atomic_plane_get_property(plane, 1214 plane->state, property, val); 1215 break; 1216 } 1217 default: 1218 ret = -EINVAL; 1219 break; 1220 } 1221 1222 return ret; 1223 } 1224 1225 /** 1226 * drm_atomic_set_crtc_for_plane - set crtc for plane 1227 * @plane_state: the plane whose incoming state to update 1228 * @crtc: crtc to use for the plane 1229 * 1230 * Changing the assigned crtc for a plane requires us to grab the lock and state 1231 * for the new crtc, as needed. This function takes care of all these details 1232 * besides updating the pointer in the state object itself. 1233 * 1234 * Returns: 1235 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK 1236 * then the w/w mutex code has detected a deadlock and the entire atomic 1237 * sequence must be restarted. All other errors are fatal. 1238 */ 1239 int 1240 drm_atomic_set_crtc_for_plane(struct drm_plane_state *plane_state, 1241 struct drm_crtc *crtc) 1242 { 1243 struct drm_plane *plane = plane_state->plane; 1244 struct drm_crtc_state *crtc_state; 1245 1246 if (plane_state->crtc) { 1247 crtc_state = drm_atomic_get_crtc_state(plane_state->state, 1248 plane_state->crtc); 1249 if (WARN_ON(IS_ERR(crtc_state))) 1250 return PTR_ERR(crtc_state); 1251 1252 crtc_state->plane_mask &= ~(1 << drm_plane_index(plane)); 1253 } 1254 1255 plane_state->crtc = crtc; 1256 1257 if (crtc) { 1258 crtc_state = drm_atomic_get_crtc_state(plane_state->state, 1259 crtc); 1260 if (IS_ERR(crtc_state)) 1261 return PTR_ERR(crtc_state); 1262 crtc_state->plane_mask |= (1 << drm_plane_index(plane)); 1263 } 1264 1265 if (crtc) 1266 DRM_DEBUG_ATOMIC("Link plane state %p to [CRTC:%d:%s]\n", 1267 plane_state, crtc->base.id, crtc->name); 1268 else 1269 DRM_DEBUG_ATOMIC("Link plane state %p to [NOCRTC]\n", 1270 plane_state); 1271 1272 return 0; 1273 } 1274 EXPORT_SYMBOL(drm_atomic_set_crtc_for_plane); 1275 1276 /** 1277 * drm_atomic_set_fb_for_plane - set framebuffer for plane 1278 * @plane_state: atomic state object for the plane 1279 * @fb: fb to use for the plane 1280 * 1281 * Changing the assigned framebuffer for a plane requires us to grab a reference 1282 * to the new fb and drop the reference to the old fb, if there is one. This 1283 * function takes care of all these details besides updating the pointer in the 1284 * state object itself. 1285 */ 1286 void 1287 drm_atomic_set_fb_for_plane(struct drm_plane_state *plane_state, 1288 struct drm_framebuffer *fb) 1289 { 1290 if (fb) 1291 DRM_DEBUG_ATOMIC("Set [FB:%d] for plane state %p\n", 1292 fb->base.id, plane_state); 1293 else 1294 DRM_DEBUG_ATOMIC("Set [NOFB] for plane state %p\n", 1295 plane_state); 1296 1297 drm_framebuffer_assign(&plane_state->fb, fb); 1298 } 1299 EXPORT_SYMBOL(drm_atomic_set_fb_for_plane); 1300 1301 /** 1302 * drm_atomic_set_fence_for_plane - set fence for plane 1303 * @plane_state: atomic state object for the plane 1304 * @fence: dma_fence to use for the plane 1305 * 1306 * Helper to setup the plane_state fence in case it is not set yet. 1307 * By using this drivers doesn't need to worry if the user choose 1308 * implicit or explicit fencing. 1309 * 1310 * This function will not set the fence to the state if it was set 1311 * via explicit fencing interfaces on the atomic ioctl. In that case it will 1312 * drop the reference to the fence as we are not storing it anywhere. 1313 * Otherwise, if &drm_plane_state.fence is not set this function we just set it 1314 * with the received implicit fence. In both cases this function consumes a 1315 * reference for @fence. 1316 */ 1317 void 1318 drm_atomic_set_fence_for_plane(struct drm_plane_state *plane_state, 1319 struct dma_fence *fence) 1320 { 1321 if (plane_state->fence) { 1322 dma_fence_put(fence); 1323 return; 1324 } 1325 1326 plane_state->fence = fence; 1327 } 1328 EXPORT_SYMBOL(drm_atomic_set_fence_for_plane); 1329 1330 /** 1331 * drm_atomic_set_crtc_for_connector - set crtc for connector 1332 * @conn_state: atomic state object for the connector 1333 * @crtc: crtc to use for the connector 1334 * 1335 * Changing the assigned crtc for a connector requires us to grab the lock and 1336 * state for the new crtc, as needed. This function takes care of all these 1337 * details besides updating the pointer in the state object itself. 1338 * 1339 * Returns: 1340 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK 1341 * then the w/w mutex code has detected a deadlock and the entire atomic 1342 * sequence must be restarted. All other errors are fatal. 1343 */ 1344 int 1345 drm_atomic_set_crtc_for_connector(struct drm_connector_state *conn_state, 1346 struct drm_crtc *crtc) 1347 { 1348 struct drm_crtc_state *crtc_state; 1349 1350 if (conn_state->crtc == crtc) 1351 return 0; 1352 1353 if (conn_state->crtc) { 1354 crtc_state = drm_atomic_get_existing_crtc_state(conn_state->state, 1355 conn_state->crtc); 1356 1357 crtc_state->connector_mask &= 1358 ~(1 << drm_connector_index(conn_state->connector)); 1359 1360 drm_connector_unreference(conn_state->connector); 1361 conn_state->crtc = NULL; 1362 } 1363 1364 if (crtc) { 1365 crtc_state = drm_atomic_get_crtc_state(conn_state->state, crtc); 1366 if (IS_ERR(crtc_state)) 1367 return PTR_ERR(crtc_state); 1368 1369 crtc_state->connector_mask |= 1370 1 << drm_connector_index(conn_state->connector); 1371 1372 drm_connector_reference(conn_state->connector); 1373 conn_state->crtc = crtc; 1374 1375 DRM_DEBUG_ATOMIC("Link connector state %p to [CRTC:%d:%s]\n", 1376 conn_state, crtc->base.id, crtc->name); 1377 } else { 1378 DRM_DEBUG_ATOMIC("Link connector state %p to [NOCRTC]\n", 1379 conn_state); 1380 } 1381 1382 return 0; 1383 } 1384 EXPORT_SYMBOL(drm_atomic_set_crtc_for_connector); 1385 1386 /** 1387 * drm_atomic_add_affected_connectors - add connectors for crtc 1388 * @state: atomic state 1389 * @crtc: DRM crtc 1390 * 1391 * This function walks the current configuration and adds all connectors 1392 * currently using @crtc to the atomic configuration @state. Note that this 1393 * function must acquire the connection mutex. This can potentially cause 1394 * unneeded seralization if the update is just for the planes on one crtc. Hence 1395 * drivers and helpers should only call this when really needed (e.g. when a 1396 * full modeset needs to happen due to some change). 1397 * 1398 * Returns: 1399 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK 1400 * then the w/w mutex code has detected a deadlock and the entire atomic 1401 * sequence must be restarted. All other errors are fatal. 1402 */ 1403 int 1404 drm_atomic_add_affected_connectors(struct drm_atomic_state *state, 1405 struct drm_crtc *crtc) 1406 { 1407 struct drm_mode_config *config = &state->dev->mode_config; 1408 struct drm_connector *connector; 1409 struct drm_connector_state *conn_state; 1410 struct drm_connector_list_iter conn_iter; 1411 int ret; 1412 1413 ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx); 1414 if (ret) 1415 return ret; 1416 1417 DRM_DEBUG_ATOMIC("Adding all current connectors for [CRTC:%d:%s] to %p\n", 1418 crtc->base.id, crtc->name, state); 1419 1420 /* 1421 * Changed connectors are already in @state, so only need to look at the 1422 * current configuration. 1423 */ 1424 drm_connector_list_iter_get(state->dev, &conn_iter); 1425 drm_for_each_connector_iter(connector, &conn_iter) { 1426 if (connector->state->crtc != crtc) 1427 continue; 1428 1429 conn_state = drm_atomic_get_connector_state(state, connector); 1430 if (IS_ERR(conn_state)) { 1431 drm_connector_list_iter_put(&conn_iter); 1432 return PTR_ERR(conn_state); 1433 } 1434 } 1435 drm_connector_list_iter_put(&conn_iter); 1436 1437 return 0; 1438 } 1439 EXPORT_SYMBOL(drm_atomic_add_affected_connectors); 1440 1441 /** 1442 * drm_atomic_add_affected_planes - add planes for crtc 1443 * @state: atomic state 1444 * @crtc: DRM crtc 1445 * 1446 * This function walks the current configuration and adds all planes 1447 * currently used by @crtc to the atomic configuration @state. This is useful 1448 * when an atomic commit also needs to check all currently enabled plane on 1449 * @crtc, e.g. when changing the mode. It's also useful when re-enabling a CRTC 1450 * to avoid special code to force-enable all planes. 1451 * 1452 * Since acquiring a plane state will always also acquire the w/w mutex of the 1453 * current CRTC for that plane (if there is any) adding all the plane states for 1454 * a CRTC will not reduce parallism of atomic updates. 1455 * 1456 * Returns: 1457 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK 1458 * then the w/w mutex code has detected a deadlock and the entire atomic 1459 * sequence must be restarted. All other errors are fatal. 1460 */ 1461 int 1462 drm_atomic_add_affected_planes(struct drm_atomic_state *state, 1463 struct drm_crtc *crtc) 1464 { 1465 struct drm_plane *plane; 1466 1467 WARN_ON(!drm_atomic_get_existing_crtc_state(state, crtc)); 1468 1469 drm_for_each_plane_mask(plane, state->dev, crtc->state->plane_mask) { 1470 struct drm_plane_state *plane_state = 1471 drm_atomic_get_plane_state(state, plane); 1472 1473 if (IS_ERR(plane_state)) 1474 return PTR_ERR(plane_state); 1475 } 1476 return 0; 1477 } 1478 EXPORT_SYMBOL(drm_atomic_add_affected_planes); 1479 1480 /** 1481 * drm_atomic_legacy_backoff - locking backoff for legacy ioctls 1482 * @state: atomic state 1483 * 1484 * This function should be used by legacy entry points which don't understand 1485 * -EDEADLK semantics. For simplicity this one will grab all modeset locks after 1486 * the slowpath completed. 1487 */ 1488 void drm_atomic_legacy_backoff(struct drm_atomic_state *state) 1489 { 1490 struct drm_device *dev = state->dev; 1491 unsigned crtc_mask = 0; 1492 struct drm_crtc *crtc; 1493 int ret; 1494 bool global = false; 1495 1496 drm_for_each_crtc(crtc, dev) { 1497 if (crtc->acquire_ctx != state->acquire_ctx) 1498 continue; 1499 1500 crtc_mask |= drm_crtc_mask(crtc); 1501 crtc->acquire_ctx = NULL; 1502 } 1503 1504 if (WARN_ON(dev->mode_config.acquire_ctx == state->acquire_ctx)) { 1505 global = true; 1506 1507 dev->mode_config.acquire_ctx = NULL; 1508 } 1509 1510 retry: 1511 drm_modeset_backoff(state->acquire_ctx); 1512 1513 ret = drm_modeset_lock_all_ctx(dev, state->acquire_ctx); 1514 if (ret) 1515 goto retry; 1516 1517 drm_for_each_crtc(crtc, dev) 1518 if (drm_crtc_mask(crtc) & crtc_mask) 1519 crtc->acquire_ctx = state->acquire_ctx; 1520 1521 if (global) 1522 dev->mode_config.acquire_ctx = state->acquire_ctx; 1523 } 1524 EXPORT_SYMBOL(drm_atomic_legacy_backoff); 1525 1526 /** 1527 * drm_atomic_check_only - check whether a given config would work 1528 * @state: atomic configuration to check 1529 * 1530 * Note that this function can return -EDEADLK if the driver needed to acquire 1531 * more locks but encountered a deadlock. The caller must then do the usual w/w 1532 * backoff dance and restart. All other errors are fatal. 1533 * 1534 * Returns: 1535 * 0 on success, negative error code on failure. 1536 */ 1537 int drm_atomic_check_only(struct drm_atomic_state *state) 1538 { 1539 struct drm_device *dev = state->dev; 1540 struct drm_mode_config *config = &dev->mode_config; 1541 struct drm_plane *plane; 1542 struct drm_plane_state *plane_state; 1543 struct drm_crtc *crtc; 1544 struct drm_crtc_state *crtc_state; 1545 int i, ret = 0; 1546 1547 DRM_DEBUG_ATOMIC("checking %p\n", state); 1548 1549 for_each_plane_in_state(state, plane, plane_state, i) { 1550 ret = drm_atomic_plane_check(plane, plane_state); 1551 if (ret) { 1552 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] atomic core check failed\n", 1553 plane->base.id, plane->name); 1554 return ret; 1555 } 1556 } 1557 1558 for_each_crtc_in_state(state, crtc, crtc_state, i) { 1559 ret = drm_atomic_crtc_check(crtc, crtc_state); 1560 if (ret) { 1561 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] atomic core check failed\n", 1562 crtc->base.id, crtc->name); 1563 return ret; 1564 } 1565 } 1566 1567 if (config->funcs->atomic_check) 1568 ret = config->funcs->atomic_check(state->dev, state); 1569 1570 if (!state->allow_modeset) { 1571 for_each_crtc_in_state(state, crtc, crtc_state, i) { 1572 if (drm_atomic_crtc_needs_modeset(crtc_state)) { 1573 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] requires full modeset\n", 1574 crtc->base.id, crtc->name); 1575 return -EINVAL; 1576 } 1577 } 1578 } 1579 1580 return ret; 1581 } 1582 EXPORT_SYMBOL(drm_atomic_check_only); 1583 1584 /** 1585 * drm_atomic_commit - commit configuration atomically 1586 * @state: atomic configuration to check 1587 * 1588 * Note that this function can return -EDEADLK if the driver needed to acquire 1589 * more locks but encountered a deadlock. The caller must then do the usual w/w 1590 * backoff dance and restart. All other errors are fatal. 1591 * 1592 * This function will take its own reference on @state. 1593 * Callers should always release their reference with drm_atomic_state_put(). 1594 * 1595 * Returns: 1596 * 0 on success, negative error code on failure. 1597 */ 1598 int drm_atomic_commit(struct drm_atomic_state *state) 1599 { 1600 struct drm_mode_config *config = &state->dev->mode_config; 1601 int ret; 1602 1603 ret = drm_atomic_check_only(state); 1604 if (ret) 1605 return ret; 1606 1607 DRM_DEBUG_ATOMIC("commiting %p\n", state); 1608 1609 return config->funcs->atomic_commit(state->dev, state, false); 1610 } 1611 EXPORT_SYMBOL(drm_atomic_commit); 1612 1613 /** 1614 * drm_atomic_nonblocking_commit - atomic nonblocking commit 1615 * @state: atomic configuration to check 1616 * 1617 * Note that this function can return -EDEADLK if the driver needed to acquire 1618 * more locks but encountered a deadlock. The caller must then do the usual w/w 1619 * backoff dance and restart. All other errors are fatal. 1620 * 1621 * This function will take its own reference on @state. 1622 * Callers should always release their reference with drm_atomic_state_put(). 1623 * 1624 * Returns: 1625 * 0 on success, negative error code on failure. 1626 */ 1627 int drm_atomic_nonblocking_commit(struct drm_atomic_state *state) 1628 { 1629 struct drm_mode_config *config = &state->dev->mode_config; 1630 int ret; 1631 1632 ret = drm_atomic_check_only(state); 1633 if (ret) 1634 return ret; 1635 1636 DRM_DEBUG_ATOMIC("commiting %p nonblocking\n", state); 1637 1638 return config->funcs->atomic_commit(state->dev, state, true); 1639 } 1640 EXPORT_SYMBOL(drm_atomic_nonblocking_commit); 1641 1642 static void drm_atomic_print_state(const struct drm_atomic_state *state) 1643 { 1644 struct drm_printer p = drm_info_printer(state->dev->dev); 1645 struct drm_plane *plane; 1646 struct drm_plane_state *plane_state; 1647 struct drm_crtc *crtc; 1648 struct drm_crtc_state *crtc_state; 1649 struct drm_connector *connector; 1650 struct drm_connector_state *connector_state; 1651 int i; 1652 1653 DRM_DEBUG_ATOMIC("checking %p\n", state); 1654 1655 for_each_plane_in_state(state, plane, plane_state, i) 1656 drm_atomic_plane_print_state(&p, plane_state); 1657 1658 for_each_crtc_in_state(state, crtc, crtc_state, i) 1659 drm_atomic_crtc_print_state(&p, crtc_state); 1660 1661 for_each_connector_in_state(state, connector, connector_state, i) 1662 drm_atomic_connector_print_state(&p, connector_state); 1663 } 1664 1665 /** 1666 * drm_state_dump - dump entire device atomic state 1667 * @dev: the drm device 1668 * @p: where to print the state to 1669 * 1670 * Just for debugging. Drivers might want an option to dump state 1671 * to dmesg in case of error irq's. (Hint, you probably want to 1672 * ratelimit this!) 1673 * 1674 * The caller must drm_modeset_lock_all(), or if this is called 1675 * from error irq handler, it should not be enabled by default. 1676 * (Ie. if you are debugging errors you might not care that this 1677 * is racey. But calling this without all modeset locks held is 1678 * not inherently safe.) 1679 */ 1680 void drm_state_dump(struct drm_device *dev, struct drm_printer *p) 1681 { 1682 struct drm_mode_config *config = &dev->mode_config; 1683 struct drm_plane *plane; 1684 struct drm_crtc *crtc; 1685 struct drm_connector *connector; 1686 struct drm_connector_list_iter conn_iter; 1687 1688 if (!drm_core_check_feature(dev, DRIVER_ATOMIC)) 1689 return; 1690 1691 list_for_each_entry(plane, &config->plane_list, head) 1692 drm_atomic_plane_print_state(p, plane->state); 1693 1694 list_for_each_entry(crtc, &config->crtc_list, head) 1695 drm_atomic_crtc_print_state(p, crtc->state); 1696 1697 drm_connector_list_iter_get(dev, &conn_iter); 1698 drm_for_each_connector_iter(connector, &conn_iter) 1699 drm_atomic_connector_print_state(p, connector->state); 1700 drm_connector_list_iter_put(&conn_iter); 1701 } 1702 EXPORT_SYMBOL(drm_state_dump); 1703 1704 #ifdef CONFIG_DEBUG_FS 1705 static int drm_state_info(struct seq_file *m, void *data) 1706 { 1707 struct drm_info_node *node = (struct drm_info_node *) m->private; 1708 struct drm_device *dev = node->minor->dev; 1709 struct drm_printer p = drm_seq_file_printer(m); 1710 1711 drm_modeset_lock_all(dev); 1712 drm_state_dump(dev, &p); 1713 drm_modeset_unlock_all(dev); 1714 1715 return 0; 1716 } 1717 1718 /* any use in debugfs files to dump individual planes/crtc/etc? */ 1719 static const struct drm_info_list drm_atomic_debugfs_list[] = { 1720 {"state", drm_state_info, 0}, 1721 }; 1722 1723 int drm_atomic_debugfs_init(struct drm_minor *minor) 1724 { 1725 return drm_debugfs_create_files(drm_atomic_debugfs_list, 1726 ARRAY_SIZE(drm_atomic_debugfs_list), 1727 minor->debugfs_root, minor); 1728 } 1729 #endif 1730 1731 /* 1732 * The big monstor ioctl 1733 */ 1734 1735 static struct drm_pending_vblank_event *create_vblank_event( 1736 struct drm_device *dev, uint64_t user_data) 1737 { 1738 struct drm_pending_vblank_event *e = NULL; 1739 1740 e = kzalloc(sizeof *e, GFP_KERNEL); 1741 if (!e) 1742 return NULL; 1743 1744 e->event.base.type = DRM_EVENT_FLIP_COMPLETE; 1745 e->event.base.length = sizeof(e->event); 1746 e->event.user_data = user_data; 1747 1748 return e; 1749 } 1750 1751 static int atomic_set_prop(struct drm_atomic_state *state, 1752 struct drm_mode_object *obj, struct drm_property *prop, 1753 uint64_t prop_value) 1754 { 1755 struct drm_mode_object *ref; 1756 int ret; 1757 1758 if (!drm_property_change_valid_get(prop, prop_value, &ref)) 1759 return -EINVAL; 1760 1761 switch (obj->type) { 1762 case DRM_MODE_OBJECT_CONNECTOR: { 1763 struct drm_connector *connector = obj_to_connector(obj); 1764 struct drm_connector_state *connector_state; 1765 1766 connector_state = drm_atomic_get_connector_state(state, connector); 1767 if (IS_ERR(connector_state)) { 1768 ret = PTR_ERR(connector_state); 1769 break; 1770 } 1771 1772 ret = drm_atomic_connector_set_property(connector, 1773 connector_state, prop, prop_value); 1774 break; 1775 } 1776 case DRM_MODE_OBJECT_CRTC: { 1777 struct drm_crtc *crtc = obj_to_crtc(obj); 1778 struct drm_crtc_state *crtc_state; 1779 1780 crtc_state = drm_atomic_get_crtc_state(state, crtc); 1781 if (IS_ERR(crtc_state)) { 1782 ret = PTR_ERR(crtc_state); 1783 break; 1784 } 1785 1786 ret = drm_atomic_crtc_set_property(crtc, 1787 crtc_state, prop, prop_value); 1788 break; 1789 } 1790 case DRM_MODE_OBJECT_PLANE: { 1791 struct drm_plane *plane = obj_to_plane(obj); 1792 struct drm_plane_state *plane_state; 1793 1794 plane_state = drm_atomic_get_plane_state(state, plane); 1795 if (IS_ERR(plane_state)) { 1796 ret = PTR_ERR(plane_state); 1797 break; 1798 } 1799 1800 ret = drm_atomic_plane_set_property(plane, 1801 plane_state, prop, prop_value); 1802 break; 1803 } 1804 default: 1805 ret = -EINVAL; 1806 break; 1807 } 1808 1809 drm_property_change_valid_put(prop, ref); 1810 return ret; 1811 } 1812 1813 /** 1814 * drm_atomic_clean_old_fb -- Unset old_fb pointers and set plane->fb pointers. 1815 * 1816 * @dev: drm device to check. 1817 * @plane_mask: plane mask for planes that were updated. 1818 * @ret: return value, can be -EDEADLK for a retry. 1819 * 1820 * Before doing an update &drm_plane.old_fb is set to &drm_plane.fb, but before 1821 * dropping the locks old_fb needs to be set to NULL and plane->fb updated. This 1822 * is a common operation for each atomic update, so this call is split off as a 1823 * helper. 1824 */ 1825 void drm_atomic_clean_old_fb(struct drm_device *dev, 1826 unsigned plane_mask, 1827 int ret) 1828 { 1829 struct drm_plane *plane; 1830 1831 /* if succeeded, fixup legacy plane crtc/fb ptrs before dropping 1832 * locks (ie. while it is still safe to deref plane->state). We 1833 * need to do this here because the driver entry points cannot 1834 * distinguish between legacy and atomic ioctls. 1835 */ 1836 drm_for_each_plane_mask(plane, dev, plane_mask) { 1837 if (ret == 0) { 1838 struct drm_framebuffer *new_fb = plane->state->fb; 1839 if (new_fb) 1840 drm_framebuffer_reference(new_fb); 1841 plane->fb = new_fb; 1842 plane->crtc = plane->state->crtc; 1843 1844 if (plane->old_fb) 1845 drm_framebuffer_unreference(plane->old_fb); 1846 } 1847 plane->old_fb = NULL; 1848 } 1849 } 1850 EXPORT_SYMBOL(drm_atomic_clean_old_fb); 1851 1852 /** 1853 * DOC: explicit fencing properties 1854 * 1855 * Explicit fencing allows userspace to control the buffer synchronization 1856 * between devices. A Fence or a group of fences are transfered to/from 1857 * userspace using Sync File fds and there are two DRM properties for that. 1858 * IN_FENCE_FD on each DRM Plane to send fences to the kernel and 1859 * OUT_FENCE_PTR on each DRM CRTC to receive fences from the kernel. 1860 * 1861 * As a contrast, with implicit fencing the kernel keeps track of any 1862 * ongoing rendering, and automatically ensures that the atomic update waits 1863 * for any pending rendering to complete. For shared buffers represented with 1864 * a &struct dma_buf this is tracked in &struct reservation_object. 1865 * Implicit syncing is how Linux traditionally worked (e.g. DRI2/3 on X.org), 1866 * whereas explicit fencing is what Android wants. 1867 * 1868 * "IN_FENCE_FD”: 1869 * Use this property to pass a fence that DRM should wait on before 1870 * proceeding with the Atomic Commit request and show the framebuffer for 1871 * the plane on the screen. The fence can be either a normal fence or a 1872 * merged one, the sync_file framework will handle both cases and use a 1873 * fence_array if a merged fence is received. Passing -1 here means no 1874 * fences to wait on. 1875 * 1876 * If the Atomic Commit request has the DRM_MODE_ATOMIC_TEST_ONLY flag 1877 * it will only check if the Sync File is a valid one. 1878 * 1879 * On the driver side the fence is stored on the @fence parameter of 1880 * &struct drm_plane_state. Drivers which also support implicit fencing 1881 * should set the implicit fence using drm_atomic_set_fence_for_plane(), 1882 * to make sure there's consistent behaviour between drivers in precedence 1883 * of implicit vs. explicit fencing. 1884 * 1885 * "OUT_FENCE_PTR”: 1886 * Use this property to pass a file descriptor pointer to DRM. Once the 1887 * Atomic Commit request call returns OUT_FENCE_PTR will be filled with 1888 * the file descriptor number of a Sync File. This Sync File contains the 1889 * CRTC fence that will be signaled when all framebuffers present on the 1890 * Atomic Commit * request for that given CRTC are scanned out on the 1891 * screen. 1892 * 1893 * The Atomic Commit request fails if a invalid pointer is passed. If the 1894 * Atomic Commit request fails for any other reason the out fence fd 1895 * returned will be -1. On a Atomic Commit with the 1896 * DRM_MODE_ATOMIC_TEST_ONLY flag the out fence will also be set to -1. 1897 * 1898 * Note that out-fences don't have a special interface to drivers and are 1899 * internally represented by a &struct drm_pending_vblank_event in struct 1900 * &drm_crtc_state, which is also used by the nonblocking atomic commit 1901 * helpers and for the DRM event handling for existing userspace. 1902 */ 1903 1904 struct drm_out_fence_state { 1905 s32 __user *out_fence_ptr; 1906 struct sync_file *sync_file; 1907 int fd; 1908 }; 1909 1910 static int setup_out_fence(struct drm_out_fence_state *fence_state, 1911 struct dma_fence *fence) 1912 { 1913 fence_state->fd = get_unused_fd_flags(O_CLOEXEC); 1914 if (fence_state->fd < 0) 1915 return fence_state->fd; 1916 1917 if (put_user(fence_state->fd, fence_state->out_fence_ptr)) 1918 return -EFAULT; 1919 1920 fence_state->sync_file = sync_file_create(fence); 1921 if (!fence_state->sync_file) 1922 return -ENOMEM; 1923 1924 return 0; 1925 } 1926 1927 static int prepare_crtc_signaling(struct drm_device *dev, 1928 struct drm_atomic_state *state, 1929 struct drm_mode_atomic *arg, 1930 struct drm_file *file_priv, 1931 struct drm_out_fence_state **fence_state, 1932 unsigned int *num_fences) 1933 { 1934 struct drm_crtc *crtc; 1935 struct drm_crtc_state *crtc_state; 1936 int i, ret; 1937 1938 if (arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) 1939 return 0; 1940 1941 for_each_crtc_in_state(state, crtc, crtc_state, i) { 1942 s32 __user *fence_ptr; 1943 1944 fence_ptr = get_out_fence_for_crtc(crtc_state->state, crtc); 1945 1946 if (arg->flags & DRM_MODE_PAGE_FLIP_EVENT || fence_ptr) { 1947 struct drm_pending_vblank_event *e; 1948 1949 e = create_vblank_event(dev, arg->user_data); 1950 if (!e) 1951 return -ENOMEM; 1952 1953 crtc_state->event = e; 1954 } 1955 1956 if (arg->flags & DRM_MODE_PAGE_FLIP_EVENT) { 1957 struct drm_pending_vblank_event *e = crtc_state->event; 1958 1959 if (!file_priv) 1960 continue; 1961 1962 ret = drm_event_reserve_init(dev, file_priv, &e->base, 1963 &e->event.base); 1964 if (ret) { 1965 kfree(e); 1966 crtc_state->event = NULL; 1967 return ret; 1968 } 1969 } 1970 1971 if (fence_ptr) { 1972 struct dma_fence *fence; 1973 struct drm_out_fence_state *f; 1974 1975 f = krealloc(*fence_state, sizeof(**fence_state) * 1976 (*num_fences + 1), GFP_KERNEL); 1977 if (!f) 1978 return -ENOMEM; 1979 1980 memset(&f[*num_fences], 0, sizeof(*f)); 1981 1982 f[*num_fences].out_fence_ptr = fence_ptr; 1983 *fence_state = f; 1984 1985 fence = drm_crtc_create_fence(crtc); 1986 if (!fence) 1987 return -ENOMEM; 1988 1989 ret = setup_out_fence(&f[(*num_fences)++], fence); 1990 if (ret) { 1991 dma_fence_put(fence); 1992 return ret; 1993 } 1994 1995 crtc_state->event->base.fence = fence; 1996 } 1997 } 1998 1999 return 0; 2000 } 2001 2002 static void complete_crtc_signaling(struct drm_device *dev, 2003 struct drm_atomic_state *state, 2004 struct drm_out_fence_state *fence_state, 2005 unsigned int num_fences, 2006 bool install_fds) 2007 { 2008 struct drm_crtc *crtc; 2009 struct drm_crtc_state *crtc_state; 2010 int i; 2011 2012 if (install_fds) { 2013 for (i = 0; i < num_fences; i++) 2014 fd_install(fence_state[i].fd, 2015 fence_state[i].sync_file->file); 2016 2017 kfree(fence_state); 2018 return; 2019 } 2020 2021 for_each_crtc_in_state(state, crtc, crtc_state, i) { 2022 struct drm_pending_vblank_event *event = crtc_state->event; 2023 /* 2024 * Free the allocated event. drm_atomic_helper_setup_commit 2025 * can allocate an event too, so only free it if it's ours 2026 * to prevent a double free in drm_atomic_state_clear. 2027 */ 2028 if (event && (event->base.fence || event->base.file_priv)) { 2029 drm_event_cancel_free(dev, &event->base); 2030 crtc_state->event = NULL; 2031 } 2032 } 2033 2034 if (!fence_state) 2035 return; 2036 2037 for (i = 0; i < num_fences; i++) { 2038 if (fence_state[i].sync_file) 2039 fput(fence_state[i].sync_file->file); 2040 if (fence_state[i].fd >= 0) 2041 put_unused_fd(fence_state[i].fd); 2042 2043 /* If this fails log error to the user */ 2044 if (fence_state[i].out_fence_ptr && 2045 put_user(-1, fence_state[i].out_fence_ptr)) 2046 DRM_DEBUG_ATOMIC("Couldn't clear out_fence_ptr\n"); 2047 } 2048 2049 kfree(fence_state); 2050 } 2051 2052 int drm_mode_atomic_ioctl(struct drm_device *dev, 2053 void *data, struct drm_file *file_priv) 2054 { 2055 struct drm_mode_atomic *arg = data; 2056 uint32_t __user *objs_ptr = (uint32_t __user *)(unsigned long)(arg->objs_ptr); 2057 uint32_t __user *count_props_ptr = (uint32_t __user *)(unsigned long)(arg->count_props_ptr); 2058 uint32_t __user *props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr); 2059 uint64_t __user *prop_values_ptr = (uint64_t __user *)(unsigned long)(arg->prop_values_ptr); 2060 unsigned int copied_objs, copied_props; 2061 struct drm_atomic_state *state; 2062 struct drm_modeset_acquire_ctx ctx; 2063 struct drm_plane *plane; 2064 struct drm_out_fence_state *fence_state = NULL; 2065 unsigned plane_mask; 2066 int ret = 0; 2067 unsigned int i, j, num_fences = 0; 2068 2069 /* disallow for drivers not supporting atomic: */ 2070 if (!drm_core_check_feature(dev, DRIVER_ATOMIC)) 2071 return -EINVAL; 2072 2073 /* disallow for userspace that has not enabled atomic cap (even 2074 * though this may be a bit overkill, since legacy userspace 2075 * wouldn't know how to call this ioctl) 2076 */ 2077 if (!file_priv->atomic) 2078 return -EINVAL; 2079 2080 if (arg->flags & ~DRM_MODE_ATOMIC_FLAGS) 2081 return -EINVAL; 2082 2083 if (arg->reserved) 2084 return -EINVAL; 2085 2086 if ((arg->flags & DRM_MODE_PAGE_FLIP_ASYNC) && 2087 !dev->mode_config.async_page_flip) 2088 return -EINVAL; 2089 2090 /* can't test and expect an event at the same time. */ 2091 if ((arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) && 2092 (arg->flags & DRM_MODE_PAGE_FLIP_EVENT)) 2093 return -EINVAL; 2094 2095 drm_modeset_acquire_init(&ctx, 0); 2096 2097 state = drm_atomic_state_alloc(dev); 2098 if (!state) 2099 return -ENOMEM; 2100 2101 state->acquire_ctx = &ctx; 2102 state->allow_modeset = !!(arg->flags & DRM_MODE_ATOMIC_ALLOW_MODESET); 2103 2104 retry: 2105 plane_mask = 0; 2106 copied_objs = 0; 2107 copied_props = 0; 2108 2109 for (i = 0; i < arg->count_objs; i++) { 2110 uint32_t obj_id, count_props; 2111 struct drm_mode_object *obj; 2112 2113 if (get_user(obj_id, objs_ptr + copied_objs)) { 2114 ret = -EFAULT; 2115 goto out; 2116 } 2117 2118 obj = drm_mode_object_find(dev, obj_id, DRM_MODE_OBJECT_ANY); 2119 if (!obj) { 2120 ret = -ENOENT; 2121 goto out; 2122 } 2123 2124 if (!obj->properties) { 2125 drm_mode_object_unreference(obj); 2126 ret = -ENOENT; 2127 goto out; 2128 } 2129 2130 if (get_user(count_props, count_props_ptr + copied_objs)) { 2131 drm_mode_object_unreference(obj); 2132 ret = -EFAULT; 2133 goto out; 2134 } 2135 2136 copied_objs++; 2137 2138 for (j = 0; j < count_props; j++) { 2139 uint32_t prop_id; 2140 uint64_t prop_value; 2141 struct drm_property *prop; 2142 2143 if (get_user(prop_id, props_ptr + copied_props)) { 2144 drm_mode_object_unreference(obj); 2145 ret = -EFAULT; 2146 goto out; 2147 } 2148 2149 prop = drm_mode_obj_find_prop_id(obj, prop_id); 2150 if (!prop) { 2151 drm_mode_object_unreference(obj); 2152 ret = -ENOENT; 2153 goto out; 2154 } 2155 2156 if (copy_from_user(&prop_value, 2157 prop_values_ptr + copied_props, 2158 sizeof(prop_value))) { 2159 drm_mode_object_unreference(obj); 2160 ret = -EFAULT; 2161 goto out; 2162 } 2163 2164 ret = atomic_set_prop(state, obj, prop, prop_value); 2165 if (ret) { 2166 drm_mode_object_unreference(obj); 2167 goto out; 2168 } 2169 2170 copied_props++; 2171 } 2172 2173 if (obj->type == DRM_MODE_OBJECT_PLANE && count_props && 2174 !(arg->flags & DRM_MODE_ATOMIC_TEST_ONLY)) { 2175 plane = obj_to_plane(obj); 2176 plane_mask |= (1 << drm_plane_index(plane)); 2177 plane->old_fb = plane->fb; 2178 } 2179 drm_mode_object_unreference(obj); 2180 } 2181 2182 ret = prepare_crtc_signaling(dev, state, arg, file_priv, &fence_state, 2183 &num_fences); 2184 if (ret) 2185 goto out; 2186 2187 if (arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) { 2188 ret = drm_atomic_check_only(state); 2189 } else if (arg->flags & DRM_MODE_ATOMIC_NONBLOCK) { 2190 ret = drm_atomic_nonblocking_commit(state); 2191 } else { 2192 if (unlikely(drm_debug & DRM_UT_STATE)) 2193 drm_atomic_print_state(state); 2194 2195 ret = drm_atomic_commit(state); 2196 } 2197 2198 out: 2199 drm_atomic_clean_old_fb(dev, plane_mask, ret); 2200 2201 complete_crtc_signaling(dev, state, fence_state, num_fences, !ret); 2202 2203 if (ret == -EDEADLK) { 2204 drm_atomic_state_clear(state); 2205 drm_modeset_backoff(&ctx); 2206 goto retry; 2207 } 2208 2209 drm_atomic_state_put(state); 2210 2211 drm_modeset_drop_locks(&ctx); 2212 drm_modeset_acquire_fini(&ctx); 2213 2214 return ret; 2215 } 2216