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