1 /* 2 * Copyright (C) 2014 Red Hat 3 * Copyright (C) 2014 Intel Corp. 4 * Copyright (C) 2018 Intel Corp. 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining a 7 * copy of this software and associated documentation files (the "Software"), 8 * to deal in the Software without restriction, including without limitation 9 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 * and/or sell copies of the Software, and to permit persons to whom the 11 * Software is furnished to do so, subject to the following conditions: 12 * 13 * The above copyright notice and this permission notice shall be included in 14 * all copies or substantial portions of the Software. 15 * 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 19 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR 20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 * OTHER DEALINGS IN THE SOFTWARE. 23 * 24 * Authors: 25 * Rob Clark <robdclark@gmail.com> 26 * Daniel Vetter <daniel.vetter@ffwll.ch> 27 */ 28 29 #include <linux/compiler.h> 30 #include <drm/drm_atomic_uapi.h> 31 #include <drm/drm_atomic.h> 32 #include <drm/drm_print.h> 33 #include <drm/drm_drv.h> 34 #include <drm/drm_writeback.h> 35 #include <drm/drm_vblank.h> 36 37 #include <linux/dma-fence.h> 38 #include <linux/uaccess.h> 39 #include <linux/sync_file.h> 40 #include <linux/file.h> 41 42 #include "drm_crtc_internal.h" 43 44 /** 45 * DOC: overview 46 * 47 * This file contains the marshalling and demarshalling glue for the atomic UAPI 48 * in all its forms: The monster ATOMIC IOCTL itself, code for GET_PROPERTY and 49 * SET_PROPERTY IOCTLs. Plus interface functions for compatibility helpers and 50 * drivers which have special needs to construct their own atomic updates, e.g. 51 * for load detect or similiar. 52 */ 53 54 /** 55 * drm_atomic_set_mode_for_crtc - set mode for CRTC 56 * @state: the CRTC whose incoming state to update 57 * @mode: kernel-internal mode to use for the CRTC, or NULL to disable 58 * 59 * Set a mode (originating from the kernel) on the desired CRTC state and update 60 * the enable property. 61 * 62 * RETURNS: 63 * Zero on success, error code on failure. Cannot return -EDEADLK. 64 */ 65 int drm_atomic_set_mode_for_crtc(struct drm_crtc_state *state, 66 const struct drm_display_mode *mode) 67 { 68 struct drm_crtc *crtc = state->crtc; 69 struct drm_mode_modeinfo umode; 70 71 /* Early return for no change. */ 72 if (mode && memcmp(&state->mode, mode, sizeof(*mode)) == 0) 73 return 0; 74 75 drm_property_blob_put(state->mode_blob); 76 state->mode_blob = NULL; 77 78 if (mode) { 79 drm_mode_convert_to_umode(&umode, mode); 80 state->mode_blob = 81 drm_property_create_blob(state->crtc->dev, 82 sizeof(umode), 83 &umode); 84 if (IS_ERR(state->mode_blob)) 85 return PTR_ERR(state->mode_blob); 86 87 drm_mode_copy(&state->mode, mode); 88 state->enable = true; 89 DRM_DEBUG_ATOMIC("Set [MODE:%s] for [CRTC:%d:%s] state %p\n", 90 mode->name, crtc->base.id, crtc->name, state); 91 } else { 92 memset(&state->mode, 0, sizeof(state->mode)); 93 state->enable = false; 94 DRM_DEBUG_ATOMIC("Set [NOMODE] for [CRTC:%d:%s] state %p\n", 95 crtc->base.id, crtc->name, state); 96 } 97 98 return 0; 99 } 100 EXPORT_SYMBOL(drm_atomic_set_mode_for_crtc); 101 102 /** 103 * drm_atomic_set_mode_prop_for_crtc - set mode for CRTC 104 * @state: the CRTC whose incoming state to update 105 * @blob: pointer to blob property to use for mode 106 * 107 * Set a mode (originating from a blob property) on the desired CRTC state. 108 * This function will take a reference on the blob property for the CRTC state, 109 * and release the reference held on the state's existing mode property, if any 110 * was set. 111 * 112 * RETURNS: 113 * Zero on success, error code on failure. Cannot return -EDEADLK. 114 */ 115 int drm_atomic_set_mode_prop_for_crtc(struct drm_crtc_state *state, 116 struct drm_property_blob *blob) 117 { 118 struct drm_crtc *crtc = state->crtc; 119 120 if (blob == state->mode_blob) 121 return 0; 122 123 drm_property_blob_put(state->mode_blob); 124 state->mode_blob = NULL; 125 126 memset(&state->mode, 0, sizeof(state->mode)); 127 128 if (blob) { 129 int ret; 130 131 if (blob->length != sizeof(struct drm_mode_modeinfo)) { 132 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] bad mode blob length: %zu\n", 133 crtc->base.id, crtc->name, 134 blob->length); 135 return -EINVAL; 136 } 137 138 ret = drm_mode_convert_umode(crtc->dev, 139 &state->mode, blob->data); 140 if (ret) { 141 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] invalid mode (ret=%d, status=%s):\n", 142 crtc->base.id, crtc->name, 143 ret, drm_get_mode_status_name(state->mode.status)); 144 drm_mode_debug_printmodeline(&state->mode); 145 return -EINVAL; 146 } 147 148 state->mode_blob = drm_property_blob_get(blob); 149 state->enable = true; 150 DRM_DEBUG_ATOMIC("Set [MODE:%s] for [CRTC:%d:%s] state %p\n", 151 state->mode.name, crtc->base.id, crtc->name, 152 state); 153 } else { 154 state->enable = false; 155 DRM_DEBUG_ATOMIC("Set [NOMODE] for [CRTC:%d:%s] state %p\n", 156 crtc->base.id, crtc->name, state); 157 } 158 159 return 0; 160 } 161 EXPORT_SYMBOL(drm_atomic_set_mode_prop_for_crtc); 162 163 /** 164 * drm_atomic_set_crtc_for_plane - set CRTC for plane 165 * @plane_state: the plane whose incoming state to update 166 * @crtc: CRTC to use for the plane 167 * 168 * Changing the assigned CRTC for a plane requires us to grab the lock and state 169 * for the new CRTC, as needed. This function takes care of all these details 170 * besides updating the pointer in the state object itself. 171 * 172 * Returns: 173 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK 174 * then the w/w mutex code has detected a deadlock and the entire atomic 175 * sequence must be restarted. All other errors are fatal. 176 */ 177 int 178 drm_atomic_set_crtc_for_plane(struct drm_plane_state *plane_state, 179 struct drm_crtc *crtc) 180 { 181 struct drm_plane *plane = plane_state->plane; 182 struct drm_crtc_state *crtc_state; 183 /* Nothing to do for same crtc*/ 184 if (plane_state->crtc == crtc) 185 return 0; 186 if (plane_state->crtc) { 187 crtc_state = drm_atomic_get_crtc_state(plane_state->state, 188 plane_state->crtc); 189 if (WARN_ON(IS_ERR(crtc_state))) 190 return PTR_ERR(crtc_state); 191 192 crtc_state->plane_mask &= ~drm_plane_mask(plane); 193 } 194 195 plane_state->crtc = crtc; 196 197 if (crtc) { 198 crtc_state = drm_atomic_get_crtc_state(plane_state->state, 199 crtc); 200 if (IS_ERR(crtc_state)) 201 return PTR_ERR(crtc_state); 202 crtc_state->plane_mask |= drm_plane_mask(plane); 203 } 204 205 if (crtc) 206 DRM_DEBUG_ATOMIC("Link [PLANE:%d:%s] state %p to [CRTC:%d:%s]\n", 207 plane->base.id, plane->name, plane_state, 208 crtc->base.id, crtc->name); 209 else 210 DRM_DEBUG_ATOMIC("Link [PLANE:%d:%s] state %p to [NOCRTC]\n", 211 plane->base.id, plane->name, plane_state); 212 213 return 0; 214 } 215 EXPORT_SYMBOL(drm_atomic_set_crtc_for_plane); 216 217 /** 218 * drm_atomic_set_fb_for_plane - set framebuffer for plane 219 * @plane_state: atomic state object for the plane 220 * @fb: fb to use for the plane 221 * 222 * Changing the assigned framebuffer for a plane requires us to grab a reference 223 * to the new fb and drop the reference to the old fb, if there is one. This 224 * function takes care of all these details besides updating the pointer in the 225 * state object itself. 226 */ 227 void 228 drm_atomic_set_fb_for_plane(struct drm_plane_state *plane_state, 229 struct drm_framebuffer *fb) 230 { 231 struct drm_plane *plane = plane_state->plane; 232 233 if (fb) 234 DRM_DEBUG_ATOMIC("Set [FB:%d] for [PLANE:%d:%s] state %p\n", 235 fb->base.id, plane->base.id, plane->name, 236 plane_state); 237 else 238 DRM_DEBUG_ATOMIC("Set [NOFB] for [PLANE:%d:%s] state %p\n", 239 plane->base.id, plane->name, plane_state); 240 241 drm_framebuffer_assign(&plane_state->fb, fb); 242 } 243 EXPORT_SYMBOL(drm_atomic_set_fb_for_plane); 244 245 /** 246 * drm_atomic_set_fence_for_plane - set fence for plane 247 * @plane_state: atomic state object for the plane 248 * @fence: dma_fence to use for the plane 249 * 250 * Helper to setup the plane_state fence in case it is not set yet. 251 * By using this drivers doesn't need to worry if the user choose 252 * implicit or explicit fencing. 253 * 254 * This function will not set the fence to the state if it was set 255 * via explicit fencing interfaces on the atomic ioctl. In that case it will 256 * drop the reference to the fence as we are not storing it anywhere. 257 * Otherwise, if &drm_plane_state.fence is not set this function we just set it 258 * with the received implicit fence. In both cases this function consumes a 259 * reference for @fence. 260 * 261 * This way explicit fencing can be used to overrule implicit fencing, which is 262 * important to make explicit fencing use-cases work: One example is using one 263 * buffer for 2 screens with different refresh rates. Implicit fencing will 264 * clamp rendering to the refresh rate of the slower screen, whereas explicit 265 * fence allows 2 independent render and display loops on a single buffer. If a 266 * driver allows obeys both implicit and explicit fences for plane updates, then 267 * it will break all the benefits of explicit fencing. 268 */ 269 void 270 drm_atomic_set_fence_for_plane(struct drm_plane_state *plane_state, 271 struct dma_fence *fence) 272 { 273 if (plane_state->fence) { 274 dma_fence_put(fence); 275 return; 276 } 277 278 plane_state->fence = fence; 279 } 280 EXPORT_SYMBOL(drm_atomic_set_fence_for_plane); 281 282 /** 283 * drm_atomic_set_crtc_for_connector - set CRTC for connector 284 * @conn_state: atomic state object for the connector 285 * @crtc: CRTC to use for the connector 286 * 287 * Changing the assigned CRTC for a connector requires us to grab the lock and 288 * state for the new CRTC, as needed. This function takes care of all these 289 * details besides updating the pointer in the state object itself. 290 * 291 * Returns: 292 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK 293 * then the w/w mutex code has detected a deadlock and the entire atomic 294 * sequence must be restarted. All other errors are fatal. 295 */ 296 int 297 drm_atomic_set_crtc_for_connector(struct drm_connector_state *conn_state, 298 struct drm_crtc *crtc) 299 { 300 struct drm_connector *connector = conn_state->connector; 301 struct drm_crtc_state *crtc_state; 302 303 if (conn_state->crtc == crtc) 304 return 0; 305 306 if (conn_state->crtc) { 307 crtc_state = drm_atomic_get_new_crtc_state(conn_state->state, 308 conn_state->crtc); 309 310 crtc_state->connector_mask &= 311 ~drm_connector_mask(conn_state->connector); 312 313 drm_connector_put(conn_state->connector); 314 conn_state->crtc = NULL; 315 } 316 317 if (crtc) { 318 crtc_state = drm_atomic_get_crtc_state(conn_state->state, crtc); 319 if (IS_ERR(crtc_state)) 320 return PTR_ERR(crtc_state); 321 322 crtc_state->connector_mask |= 323 drm_connector_mask(conn_state->connector); 324 325 drm_connector_get(conn_state->connector); 326 conn_state->crtc = crtc; 327 328 DRM_DEBUG_ATOMIC("Link [CONNECTOR:%d:%s] state %p to [CRTC:%d:%s]\n", 329 connector->base.id, connector->name, 330 conn_state, crtc->base.id, crtc->name); 331 } else { 332 DRM_DEBUG_ATOMIC("Link [CONNECTOR:%d:%s] state %p to [NOCRTC]\n", 333 connector->base.id, connector->name, 334 conn_state); 335 } 336 337 return 0; 338 } 339 EXPORT_SYMBOL(drm_atomic_set_crtc_for_connector); 340 341 static void set_out_fence_for_crtc(struct drm_atomic_state *state, 342 struct drm_crtc *crtc, s32 __user *fence_ptr) 343 { 344 state->crtcs[drm_crtc_index(crtc)].out_fence_ptr = fence_ptr; 345 } 346 347 static s32 __user *get_out_fence_for_crtc(struct drm_atomic_state *state, 348 struct drm_crtc *crtc) 349 { 350 s32 __user *fence_ptr; 351 352 fence_ptr = state->crtcs[drm_crtc_index(crtc)].out_fence_ptr; 353 state->crtcs[drm_crtc_index(crtc)].out_fence_ptr = NULL; 354 355 return fence_ptr; 356 } 357 358 static int set_out_fence_for_connector(struct drm_atomic_state *state, 359 struct drm_connector *connector, 360 s32 __user *fence_ptr) 361 { 362 unsigned int index = drm_connector_index(connector); 363 364 if (!fence_ptr) 365 return 0; 366 367 if (put_user(-1, fence_ptr)) 368 return -EFAULT; 369 370 state->connectors[index].out_fence_ptr = fence_ptr; 371 372 return 0; 373 } 374 375 static s32 __user *get_out_fence_for_connector(struct drm_atomic_state *state, 376 struct drm_connector *connector) 377 { 378 unsigned int index = drm_connector_index(connector); 379 s32 __user *fence_ptr; 380 381 fence_ptr = state->connectors[index].out_fence_ptr; 382 state->connectors[index].out_fence_ptr = NULL; 383 384 return fence_ptr; 385 } 386 387 static int 388 drm_atomic_replace_property_blob_from_id(struct drm_device *dev, 389 struct drm_property_blob **blob, 390 uint64_t blob_id, 391 ssize_t expected_size, 392 ssize_t expected_elem_size, 393 bool *replaced) 394 { 395 struct drm_property_blob *new_blob = NULL; 396 397 if (blob_id != 0) { 398 new_blob = drm_property_lookup_blob(dev, blob_id); 399 if (new_blob == NULL) 400 return -EINVAL; 401 402 if (expected_size > 0 && 403 new_blob->length != expected_size) { 404 drm_property_blob_put(new_blob); 405 return -EINVAL; 406 } 407 if (expected_elem_size > 0 && 408 new_blob->length % expected_elem_size != 0) { 409 drm_property_blob_put(new_blob); 410 return -EINVAL; 411 } 412 } 413 414 *replaced |= drm_property_replace_blob(blob, new_blob); 415 drm_property_blob_put(new_blob); 416 417 return 0; 418 } 419 420 static int drm_atomic_crtc_set_property(struct drm_crtc *crtc, 421 struct drm_crtc_state *state, struct drm_property *property, 422 uint64_t val) 423 { 424 struct drm_device *dev = crtc->dev; 425 struct drm_mode_config *config = &dev->mode_config; 426 bool replaced = false; 427 int ret; 428 429 if (property == config->prop_active) 430 state->active = val; 431 else if (property == config->prop_mode_id) { 432 struct drm_property_blob *mode = 433 drm_property_lookup_blob(dev, val); 434 ret = drm_atomic_set_mode_prop_for_crtc(state, mode); 435 drm_property_blob_put(mode); 436 return ret; 437 } else if (property == config->prop_vrr_enabled) { 438 state->vrr_enabled = val; 439 } else if (property == config->degamma_lut_property) { 440 ret = drm_atomic_replace_property_blob_from_id(dev, 441 &state->degamma_lut, 442 val, 443 -1, sizeof(struct drm_color_lut), 444 &replaced); 445 state->color_mgmt_changed |= replaced; 446 return ret; 447 } else if (property == config->ctm_property) { 448 ret = drm_atomic_replace_property_blob_from_id(dev, 449 &state->ctm, 450 val, 451 sizeof(struct drm_color_ctm), -1, 452 &replaced); 453 state->color_mgmt_changed |= replaced; 454 return ret; 455 } else if (property == config->gamma_lut_property) { 456 ret = drm_atomic_replace_property_blob_from_id(dev, 457 &state->gamma_lut, 458 val, 459 -1, sizeof(struct drm_color_lut), 460 &replaced); 461 state->color_mgmt_changed |= replaced; 462 return ret; 463 } else if (property == config->prop_out_fence_ptr) { 464 s32 __user *fence_ptr = u64_to_user_ptr(val); 465 466 if (!fence_ptr) 467 return 0; 468 469 if (put_user(-1, fence_ptr)) 470 return -EFAULT; 471 472 set_out_fence_for_crtc(state->state, crtc, fence_ptr); 473 } else if (crtc->funcs->atomic_set_property) { 474 return crtc->funcs->atomic_set_property(crtc, state, property, val); 475 } else { 476 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] unknown property [PROP:%d:%s]]\n", 477 crtc->base.id, crtc->name, 478 property->base.id, property->name); 479 return -EINVAL; 480 } 481 482 return 0; 483 } 484 485 static int 486 drm_atomic_crtc_get_property(struct drm_crtc *crtc, 487 const struct drm_crtc_state *state, 488 struct drm_property *property, uint64_t *val) 489 { 490 struct drm_device *dev = crtc->dev; 491 struct drm_mode_config *config = &dev->mode_config; 492 493 if (property == config->prop_active) 494 *val = drm_atomic_crtc_effectively_active(state); 495 else if (property == config->prop_mode_id) 496 *val = (state->mode_blob) ? state->mode_blob->base.id : 0; 497 else if (property == config->prop_vrr_enabled) 498 *val = state->vrr_enabled; 499 else if (property == config->degamma_lut_property) 500 *val = (state->degamma_lut) ? state->degamma_lut->base.id : 0; 501 else if (property == config->ctm_property) 502 *val = (state->ctm) ? state->ctm->base.id : 0; 503 else if (property == config->gamma_lut_property) 504 *val = (state->gamma_lut) ? state->gamma_lut->base.id : 0; 505 else if (property == config->prop_out_fence_ptr) 506 *val = 0; 507 else if (crtc->funcs->atomic_get_property) 508 return crtc->funcs->atomic_get_property(crtc, state, property, val); 509 else 510 return -EINVAL; 511 512 return 0; 513 } 514 515 static int drm_atomic_plane_set_property(struct drm_plane *plane, 516 struct drm_plane_state *state, struct drm_file *file_priv, 517 struct drm_property *property, uint64_t val) 518 { 519 struct drm_device *dev = plane->dev; 520 struct drm_mode_config *config = &dev->mode_config; 521 bool replaced = false; 522 int ret; 523 524 if (property == config->prop_fb_id) { 525 struct drm_framebuffer *fb; 526 527 fb = drm_framebuffer_lookup(dev, file_priv, val); 528 drm_atomic_set_fb_for_plane(state, fb); 529 if (fb) 530 drm_framebuffer_put(fb); 531 } else if (property == config->prop_in_fence_fd) { 532 if (state->fence) 533 return -EINVAL; 534 535 if (U642I64(val) == -1) 536 return 0; 537 538 state->fence = sync_file_get_fence(val); 539 if (!state->fence) 540 return -EINVAL; 541 542 } else if (property == config->prop_crtc_id) { 543 struct drm_crtc *crtc = drm_crtc_find(dev, file_priv, val); 544 545 if (val && !crtc) 546 return -EACCES; 547 return drm_atomic_set_crtc_for_plane(state, crtc); 548 } else if (property == config->prop_crtc_x) { 549 state->crtc_x = U642I64(val); 550 } else if (property == config->prop_crtc_y) { 551 state->crtc_y = U642I64(val); 552 } else if (property == config->prop_crtc_w) { 553 state->crtc_w = val; 554 } else if (property == config->prop_crtc_h) { 555 state->crtc_h = val; 556 } else if (property == config->prop_src_x) { 557 state->src_x = val; 558 } else if (property == config->prop_src_y) { 559 state->src_y = val; 560 } else if (property == config->prop_src_w) { 561 state->src_w = val; 562 } else if (property == config->prop_src_h) { 563 state->src_h = val; 564 } else if (property == plane->alpha_property) { 565 state->alpha = val; 566 } else if (property == plane->blend_mode_property) { 567 state->pixel_blend_mode = val; 568 } else if (property == plane->rotation_property) { 569 if (!is_power_of_2(val & DRM_MODE_ROTATE_MASK)) { 570 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] bad rotation bitmask: 0x%llx\n", 571 plane->base.id, plane->name, val); 572 return -EINVAL; 573 } 574 state->rotation = val; 575 } else if (property == plane->zpos_property) { 576 state->zpos = val; 577 } else if (property == plane->color_encoding_property) { 578 state->color_encoding = val; 579 } else if (property == plane->color_range_property) { 580 state->color_range = val; 581 } else if (property == config->prop_fb_damage_clips) { 582 ret = drm_atomic_replace_property_blob_from_id(dev, 583 &state->fb_damage_clips, 584 val, 585 -1, 586 sizeof(struct drm_rect), 587 &replaced); 588 return ret; 589 } else if (plane->funcs->atomic_set_property) { 590 return plane->funcs->atomic_set_property(plane, state, 591 property, val); 592 } else { 593 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] unknown property [PROP:%d:%s]]\n", 594 plane->base.id, plane->name, 595 property->base.id, property->name); 596 return -EINVAL; 597 } 598 599 return 0; 600 } 601 602 static int 603 drm_atomic_plane_get_property(struct drm_plane *plane, 604 const struct drm_plane_state *state, 605 struct drm_property *property, uint64_t *val) 606 { 607 struct drm_device *dev = plane->dev; 608 struct drm_mode_config *config = &dev->mode_config; 609 610 if (property == config->prop_fb_id) { 611 *val = (state->fb) ? state->fb->base.id : 0; 612 } else if (property == config->prop_in_fence_fd) { 613 *val = -1; 614 } else if (property == config->prop_crtc_id) { 615 *val = (state->crtc) ? state->crtc->base.id : 0; 616 } else if (property == config->prop_crtc_x) { 617 *val = I642U64(state->crtc_x); 618 } else if (property == config->prop_crtc_y) { 619 *val = I642U64(state->crtc_y); 620 } else if (property == config->prop_crtc_w) { 621 *val = state->crtc_w; 622 } else if (property == config->prop_crtc_h) { 623 *val = state->crtc_h; 624 } else if (property == config->prop_src_x) { 625 *val = state->src_x; 626 } else if (property == config->prop_src_y) { 627 *val = state->src_y; 628 } else if (property == config->prop_src_w) { 629 *val = state->src_w; 630 } else if (property == config->prop_src_h) { 631 *val = state->src_h; 632 } else if (property == plane->alpha_property) { 633 *val = state->alpha; 634 } else if (property == plane->blend_mode_property) { 635 *val = state->pixel_blend_mode; 636 } else if (property == plane->rotation_property) { 637 *val = state->rotation; 638 } else if (property == plane->zpos_property) { 639 *val = state->zpos; 640 } else if (property == plane->color_encoding_property) { 641 *val = state->color_encoding; 642 } else if (property == plane->color_range_property) { 643 *val = state->color_range; 644 } else if (property == config->prop_fb_damage_clips) { 645 *val = (state->fb_damage_clips) ? 646 state->fb_damage_clips->base.id : 0; 647 } else if (plane->funcs->atomic_get_property) { 648 return plane->funcs->atomic_get_property(plane, state, property, val); 649 } else { 650 return -EINVAL; 651 } 652 653 return 0; 654 } 655 656 static int drm_atomic_set_writeback_fb_for_connector( 657 struct drm_connector_state *conn_state, 658 struct drm_framebuffer *fb) 659 { 660 int ret; 661 662 ret = drm_writeback_set_fb(conn_state, fb); 663 if (ret < 0) 664 return ret; 665 666 if (fb) 667 DRM_DEBUG_ATOMIC("Set [FB:%d] for connector state %p\n", 668 fb->base.id, conn_state); 669 else 670 DRM_DEBUG_ATOMIC("Set [NOFB] for connector state %p\n", 671 conn_state); 672 673 return 0; 674 } 675 676 static int drm_atomic_connector_set_property(struct drm_connector *connector, 677 struct drm_connector_state *state, struct drm_file *file_priv, 678 struct drm_property *property, uint64_t val) 679 { 680 struct drm_device *dev = connector->dev; 681 struct drm_mode_config *config = &dev->mode_config; 682 bool replaced = false; 683 int ret; 684 685 if (property == config->prop_crtc_id) { 686 struct drm_crtc *crtc = drm_crtc_find(dev, file_priv, val); 687 688 if (val && !crtc) 689 return -EACCES; 690 return drm_atomic_set_crtc_for_connector(state, crtc); 691 } else if (property == config->dpms_property) { 692 /* setting DPMS property requires special handling, which 693 * is done in legacy setprop path for us. Disallow (for 694 * now?) atomic writes to DPMS property: 695 */ 696 return -EINVAL; 697 } else if (property == config->tv_select_subconnector_property) { 698 state->tv.subconnector = val; 699 } else if (property == config->tv_left_margin_property) { 700 state->tv.margins.left = val; 701 } else if (property == config->tv_right_margin_property) { 702 state->tv.margins.right = val; 703 } else if (property == config->tv_top_margin_property) { 704 state->tv.margins.top = val; 705 } else if (property == config->tv_bottom_margin_property) { 706 state->tv.margins.bottom = val; 707 } else if (property == config->tv_mode_property) { 708 state->tv.mode = val; 709 } else if (property == config->tv_brightness_property) { 710 state->tv.brightness = val; 711 } else if (property == config->tv_contrast_property) { 712 state->tv.contrast = val; 713 } else if (property == config->tv_flicker_reduction_property) { 714 state->tv.flicker_reduction = val; 715 } else if (property == config->tv_overscan_property) { 716 state->tv.overscan = val; 717 } else if (property == config->tv_saturation_property) { 718 state->tv.saturation = val; 719 } else if (property == config->tv_hue_property) { 720 state->tv.hue = val; 721 } else if (property == config->link_status_property) { 722 /* Never downgrade from GOOD to BAD on userspace's request here, 723 * only hw issues can do that. 724 * 725 * For an atomic property the userspace doesn't need to be able 726 * to understand all the properties, but needs to be able to 727 * restore the state it wants on VT switch. So if the userspace 728 * tries to change the link_status from GOOD to BAD, driver 729 * silently rejects it and returns a 0. This prevents userspace 730 * from accidently breaking the display when it restores the 731 * state. 732 */ 733 if (state->link_status != DRM_LINK_STATUS_GOOD) 734 state->link_status = val; 735 } else if (property == config->hdr_output_metadata_property) { 736 ret = drm_atomic_replace_property_blob_from_id(dev, 737 &state->hdr_output_metadata, 738 val, 739 sizeof(struct hdr_output_metadata), -1, 740 &replaced); 741 return ret; 742 } else if (property == config->aspect_ratio_property) { 743 state->picture_aspect_ratio = val; 744 } else if (property == config->content_type_property) { 745 state->content_type = val; 746 } else if (property == connector->scaling_mode_property) { 747 state->scaling_mode = val; 748 } else if (property == config->content_protection_property) { 749 if (val == DRM_MODE_CONTENT_PROTECTION_ENABLED) { 750 DRM_DEBUG_KMS("only drivers can set CP Enabled\n"); 751 return -EINVAL; 752 } 753 state->content_protection = val; 754 } else if (property == config->hdcp_content_type_property) { 755 state->hdcp_content_type = val; 756 } else if (property == connector->colorspace_property) { 757 state->colorspace = val; 758 } else if (property == config->writeback_fb_id_property) { 759 struct drm_framebuffer *fb; 760 int ret; 761 762 fb = drm_framebuffer_lookup(dev, file_priv, val); 763 ret = drm_atomic_set_writeback_fb_for_connector(state, fb); 764 if (fb) 765 drm_framebuffer_put(fb); 766 return ret; 767 } else if (property == config->writeback_out_fence_ptr_property) { 768 s32 __user *fence_ptr = u64_to_user_ptr(val); 769 770 return set_out_fence_for_connector(state->state, connector, 771 fence_ptr); 772 } else if (property == connector->max_bpc_property) { 773 state->max_requested_bpc = val; 774 } else if (connector->funcs->atomic_set_property) { 775 return connector->funcs->atomic_set_property(connector, 776 state, property, val); 777 } else { 778 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] unknown property [PROP:%d:%s]]\n", 779 connector->base.id, connector->name, 780 property->base.id, property->name); 781 return -EINVAL; 782 } 783 784 return 0; 785 } 786 787 static int 788 drm_atomic_connector_get_property(struct drm_connector *connector, 789 const struct drm_connector_state *state, 790 struct drm_property *property, uint64_t *val) 791 { 792 struct drm_device *dev = connector->dev; 793 struct drm_mode_config *config = &dev->mode_config; 794 795 if (property == config->prop_crtc_id) { 796 *val = (state->crtc) ? state->crtc->base.id : 0; 797 } else if (property == config->dpms_property) { 798 if (state->crtc && state->crtc->state->self_refresh_active) 799 *val = DRM_MODE_DPMS_ON; 800 else 801 *val = connector->dpms; 802 } else if (property == config->tv_select_subconnector_property) { 803 *val = state->tv.subconnector; 804 } else if (property == config->tv_left_margin_property) { 805 *val = state->tv.margins.left; 806 } else if (property == config->tv_right_margin_property) { 807 *val = state->tv.margins.right; 808 } else if (property == config->tv_top_margin_property) { 809 *val = state->tv.margins.top; 810 } else if (property == config->tv_bottom_margin_property) { 811 *val = state->tv.margins.bottom; 812 } else if (property == config->tv_mode_property) { 813 *val = state->tv.mode; 814 } else if (property == config->tv_brightness_property) { 815 *val = state->tv.brightness; 816 } else if (property == config->tv_contrast_property) { 817 *val = state->tv.contrast; 818 } else if (property == config->tv_flicker_reduction_property) { 819 *val = state->tv.flicker_reduction; 820 } else if (property == config->tv_overscan_property) { 821 *val = state->tv.overscan; 822 } else if (property == config->tv_saturation_property) { 823 *val = state->tv.saturation; 824 } else if (property == config->tv_hue_property) { 825 *val = state->tv.hue; 826 } else if (property == config->link_status_property) { 827 *val = state->link_status; 828 } else if (property == config->aspect_ratio_property) { 829 *val = state->picture_aspect_ratio; 830 } else if (property == config->content_type_property) { 831 *val = state->content_type; 832 } else if (property == connector->colorspace_property) { 833 *val = state->colorspace; 834 } else if (property == connector->scaling_mode_property) { 835 *val = state->scaling_mode; 836 } else if (property == config->hdr_output_metadata_property) { 837 *val = state->hdr_output_metadata ? 838 state->hdr_output_metadata->base.id : 0; 839 } else if (property == config->content_protection_property) { 840 *val = state->content_protection; 841 } else if (property == config->hdcp_content_type_property) { 842 *val = state->hdcp_content_type; 843 } else if (property == config->writeback_fb_id_property) { 844 /* Writeback framebuffer is one-shot, write and forget */ 845 *val = 0; 846 } else if (property == config->writeback_out_fence_ptr_property) { 847 *val = 0; 848 } else if (property == connector->max_bpc_property) { 849 *val = state->max_requested_bpc; 850 } else if (connector->funcs->atomic_get_property) { 851 return connector->funcs->atomic_get_property(connector, 852 state, property, val); 853 } else { 854 return -EINVAL; 855 } 856 857 return 0; 858 } 859 860 int drm_atomic_get_property(struct drm_mode_object *obj, 861 struct drm_property *property, uint64_t *val) 862 { 863 struct drm_device *dev = property->dev; 864 int ret; 865 866 switch (obj->type) { 867 case DRM_MODE_OBJECT_CONNECTOR: { 868 struct drm_connector *connector = obj_to_connector(obj); 869 870 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex)); 871 ret = drm_atomic_connector_get_property(connector, 872 connector->state, property, val); 873 break; 874 } 875 case DRM_MODE_OBJECT_CRTC: { 876 struct drm_crtc *crtc = obj_to_crtc(obj); 877 878 WARN_ON(!drm_modeset_is_locked(&crtc->mutex)); 879 ret = drm_atomic_crtc_get_property(crtc, 880 crtc->state, property, val); 881 break; 882 } 883 case DRM_MODE_OBJECT_PLANE: { 884 struct drm_plane *plane = obj_to_plane(obj); 885 886 WARN_ON(!drm_modeset_is_locked(&plane->mutex)); 887 ret = drm_atomic_plane_get_property(plane, 888 plane->state, property, val); 889 break; 890 } 891 default: 892 ret = -EINVAL; 893 break; 894 } 895 896 return ret; 897 } 898 899 /* 900 * The big monster ioctl 901 */ 902 903 static struct drm_pending_vblank_event *create_vblank_event( 904 struct drm_crtc *crtc, uint64_t user_data) 905 { 906 struct drm_pending_vblank_event *e = NULL; 907 908 e = kzalloc(sizeof *e, GFP_KERNEL); 909 if (!e) 910 return NULL; 911 912 e->event.base.type = DRM_EVENT_FLIP_COMPLETE; 913 e->event.base.length = sizeof(e->event); 914 e->event.vbl.crtc_id = crtc->base.id; 915 e->event.vbl.user_data = user_data; 916 917 return e; 918 } 919 920 int drm_atomic_connector_commit_dpms(struct drm_atomic_state *state, 921 struct drm_connector *connector, 922 int mode) 923 { 924 struct drm_connector *tmp_connector; 925 struct drm_connector_state *new_conn_state; 926 struct drm_crtc *crtc; 927 struct drm_crtc_state *crtc_state; 928 int i, ret, old_mode = connector->dpms; 929 bool active = false; 930 931 ret = drm_modeset_lock(&state->dev->mode_config.connection_mutex, 932 state->acquire_ctx); 933 if (ret) 934 return ret; 935 936 if (mode != DRM_MODE_DPMS_ON) 937 mode = DRM_MODE_DPMS_OFF; 938 connector->dpms = mode; 939 940 crtc = connector->state->crtc; 941 if (!crtc) 942 goto out; 943 ret = drm_atomic_add_affected_connectors(state, crtc); 944 if (ret) 945 goto out; 946 947 crtc_state = drm_atomic_get_crtc_state(state, crtc); 948 if (IS_ERR(crtc_state)) { 949 ret = PTR_ERR(crtc_state); 950 goto out; 951 } 952 953 for_each_new_connector_in_state(state, tmp_connector, new_conn_state, i) { 954 if (new_conn_state->crtc != crtc) 955 continue; 956 if (tmp_connector->dpms == DRM_MODE_DPMS_ON) { 957 active = true; 958 break; 959 } 960 } 961 962 crtc_state->active = active; 963 ret = drm_atomic_commit(state); 964 out: 965 if (ret != 0) 966 connector->dpms = old_mode; 967 return ret; 968 } 969 970 int drm_atomic_set_property(struct drm_atomic_state *state, 971 struct drm_file *file_priv, 972 struct drm_mode_object *obj, 973 struct drm_property *prop, 974 uint64_t prop_value) 975 { 976 struct drm_mode_object *ref; 977 int ret; 978 979 if (!drm_property_change_valid_get(prop, prop_value, &ref)) 980 return -EINVAL; 981 982 switch (obj->type) { 983 case DRM_MODE_OBJECT_CONNECTOR: { 984 struct drm_connector *connector = obj_to_connector(obj); 985 struct drm_connector_state *connector_state; 986 987 connector_state = drm_atomic_get_connector_state(state, connector); 988 if (IS_ERR(connector_state)) { 989 ret = PTR_ERR(connector_state); 990 break; 991 } 992 993 ret = drm_atomic_connector_set_property(connector, 994 connector_state, file_priv, 995 prop, prop_value); 996 break; 997 } 998 case DRM_MODE_OBJECT_CRTC: { 999 struct drm_crtc *crtc = obj_to_crtc(obj); 1000 struct drm_crtc_state *crtc_state; 1001 1002 crtc_state = drm_atomic_get_crtc_state(state, crtc); 1003 if (IS_ERR(crtc_state)) { 1004 ret = PTR_ERR(crtc_state); 1005 break; 1006 } 1007 1008 ret = drm_atomic_crtc_set_property(crtc, 1009 crtc_state, prop, prop_value); 1010 break; 1011 } 1012 case DRM_MODE_OBJECT_PLANE: { 1013 struct drm_plane *plane = obj_to_plane(obj); 1014 struct drm_plane_state *plane_state; 1015 1016 plane_state = drm_atomic_get_plane_state(state, plane); 1017 if (IS_ERR(plane_state)) { 1018 ret = PTR_ERR(plane_state); 1019 break; 1020 } 1021 1022 ret = drm_atomic_plane_set_property(plane, 1023 plane_state, file_priv, 1024 prop, prop_value); 1025 break; 1026 } 1027 default: 1028 ret = -EINVAL; 1029 break; 1030 } 1031 1032 drm_property_change_valid_put(prop, ref); 1033 return ret; 1034 } 1035 1036 /** 1037 * DOC: explicit fencing properties 1038 * 1039 * Explicit fencing allows userspace to control the buffer synchronization 1040 * between devices. A Fence or a group of fences are transfered to/from 1041 * userspace using Sync File fds and there are two DRM properties for that. 1042 * IN_FENCE_FD on each DRM Plane to send fences to the kernel and 1043 * OUT_FENCE_PTR on each DRM CRTC to receive fences from the kernel. 1044 * 1045 * As a contrast, with implicit fencing the kernel keeps track of any 1046 * ongoing rendering, and automatically ensures that the atomic update waits 1047 * for any pending rendering to complete. For shared buffers represented with 1048 * a &struct dma_buf this is tracked in &struct dma_resv. 1049 * Implicit syncing is how Linux traditionally worked (e.g. DRI2/3 on X.org), 1050 * whereas explicit fencing is what Android wants. 1051 * 1052 * "IN_FENCE_FD”: 1053 * Use this property to pass a fence that DRM should wait on before 1054 * proceeding with the Atomic Commit request and show the framebuffer for 1055 * the plane on the screen. The fence can be either a normal fence or a 1056 * merged one, the sync_file framework will handle both cases and use a 1057 * fence_array if a merged fence is received. Passing -1 here means no 1058 * fences to wait on. 1059 * 1060 * If the Atomic Commit request has the DRM_MODE_ATOMIC_TEST_ONLY flag 1061 * it will only check if the Sync File is a valid one. 1062 * 1063 * On the driver side the fence is stored on the @fence parameter of 1064 * &struct drm_plane_state. Drivers which also support implicit fencing 1065 * should set the implicit fence using drm_atomic_set_fence_for_plane(), 1066 * to make sure there's consistent behaviour between drivers in precedence 1067 * of implicit vs. explicit fencing. 1068 * 1069 * "OUT_FENCE_PTR”: 1070 * Use this property to pass a file descriptor pointer to DRM. Once the 1071 * Atomic Commit request call returns OUT_FENCE_PTR will be filled with 1072 * the file descriptor number of a Sync File. This Sync File contains the 1073 * CRTC fence that will be signaled when all framebuffers present on the 1074 * Atomic Commit * request for that given CRTC are scanned out on the 1075 * screen. 1076 * 1077 * The Atomic Commit request fails if a invalid pointer is passed. If the 1078 * Atomic Commit request fails for any other reason the out fence fd 1079 * returned will be -1. On a Atomic Commit with the 1080 * DRM_MODE_ATOMIC_TEST_ONLY flag the out fence will also be set to -1. 1081 * 1082 * Note that out-fences don't have a special interface to drivers and are 1083 * internally represented by a &struct drm_pending_vblank_event in struct 1084 * &drm_crtc_state, which is also used by the nonblocking atomic commit 1085 * helpers and for the DRM event handling for existing userspace. 1086 */ 1087 1088 struct drm_out_fence_state { 1089 s32 __user *out_fence_ptr; 1090 struct sync_file *sync_file; 1091 int fd; 1092 }; 1093 1094 static int setup_out_fence(struct drm_out_fence_state *fence_state, 1095 struct dma_fence *fence) 1096 { 1097 fence_state->fd = get_unused_fd_flags(O_CLOEXEC); 1098 if (fence_state->fd < 0) 1099 return fence_state->fd; 1100 1101 if (put_user(fence_state->fd, fence_state->out_fence_ptr)) 1102 return -EFAULT; 1103 1104 fence_state->sync_file = sync_file_create(fence); 1105 if (!fence_state->sync_file) 1106 return -ENOMEM; 1107 1108 return 0; 1109 } 1110 1111 static int prepare_signaling(struct drm_device *dev, 1112 struct drm_atomic_state *state, 1113 struct drm_mode_atomic *arg, 1114 struct drm_file *file_priv, 1115 struct drm_out_fence_state **fence_state, 1116 unsigned int *num_fences) 1117 { 1118 struct drm_crtc *crtc; 1119 struct drm_crtc_state *crtc_state; 1120 struct drm_connector *conn; 1121 struct drm_connector_state *conn_state; 1122 int i, c = 0, ret; 1123 1124 if (arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) 1125 return 0; 1126 1127 for_each_new_crtc_in_state(state, crtc, crtc_state, i) { 1128 s32 __user *fence_ptr; 1129 1130 fence_ptr = get_out_fence_for_crtc(crtc_state->state, crtc); 1131 1132 if (arg->flags & DRM_MODE_PAGE_FLIP_EVENT || fence_ptr) { 1133 struct drm_pending_vblank_event *e; 1134 1135 e = create_vblank_event(crtc, arg->user_data); 1136 if (!e) 1137 return -ENOMEM; 1138 1139 crtc_state->event = e; 1140 } 1141 1142 if (arg->flags & DRM_MODE_PAGE_FLIP_EVENT) { 1143 struct drm_pending_vblank_event *e = crtc_state->event; 1144 1145 if (!file_priv) 1146 continue; 1147 1148 ret = drm_event_reserve_init(dev, file_priv, &e->base, 1149 &e->event.base); 1150 if (ret) { 1151 kfree(e); 1152 crtc_state->event = NULL; 1153 return ret; 1154 } 1155 } 1156 1157 if (fence_ptr) { 1158 struct dma_fence *fence; 1159 struct drm_out_fence_state *f; 1160 1161 #ifdef __linux__ 1162 f = krealloc(*fence_state, sizeof(**fence_state) * 1163 (*num_fences + 1), GFP_KERNEL); 1164 if (!f) 1165 return -ENOMEM; 1166 #else 1167 f = kmalloc(sizeof(**fence_state) * 1168 (*num_fences + 1), GFP_KERNEL); 1169 if (!f) 1170 return -ENOMEM; 1171 memcpy(f, *fence_state, 1172 sizeof(**fence_state) * (*num_fences)); 1173 kfree(*fence_state); 1174 #endif 1175 1176 memset(&f[*num_fences], 0, sizeof(*f)); 1177 1178 f[*num_fences].out_fence_ptr = fence_ptr; 1179 *fence_state = f; 1180 1181 fence = drm_crtc_create_fence(crtc); 1182 if (!fence) 1183 return -ENOMEM; 1184 1185 ret = setup_out_fence(&f[(*num_fences)++], fence); 1186 if (ret) { 1187 dma_fence_put(fence); 1188 return ret; 1189 } 1190 1191 crtc_state->event->base.fence = fence; 1192 } 1193 1194 c++; 1195 } 1196 1197 for_each_new_connector_in_state(state, conn, conn_state, i) { 1198 struct drm_writeback_connector *wb_conn; 1199 struct drm_out_fence_state *f; 1200 struct dma_fence *fence; 1201 s32 __user *fence_ptr; 1202 1203 if (!conn_state->writeback_job) 1204 continue; 1205 1206 fence_ptr = get_out_fence_for_connector(state, conn); 1207 if (!fence_ptr) 1208 continue; 1209 1210 #ifdef __linux__ 1211 f = krealloc(*fence_state, sizeof(**fence_state) * 1212 (*num_fences + 1), GFP_KERNEL); 1213 if (!f) 1214 return -ENOMEM; 1215 #else 1216 f = kmalloc(sizeof(**fence_state) * 1217 (*num_fences + 1), GFP_KERNEL); 1218 if (!f) 1219 return -ENOMEM; 1220 memcpy(f, *fence_state, 1221 sizeof(**fence_state) * (*num_fences)); 1222 kfree(*fence_state); 1223 #endif 1224 1225 memset(&f[*num_fences], 0, sizeof(*f)); 1226 1227 f[*num_fences].out_fence_ptr = fence_ptr; 1228 *fence_state = f; 1229 1230 wb_conn = drm_connector_to_writeback(conn); 1231 fence = drm_writeback_get_out_fence(wb_conn); 1232 if (!fence) 1233 return -ENOMEM; 1234 1235 ret = setup_out_fence(&f[(*num_fences)++], fence); 1236 if (ret) { 1237 dma_fence_put(fence); 1238 return ret; 1239 } 1240 1241 conn_state->writeback_job->out_fence = fence; 1242 } 1243 1244 /* 1245 * Having this flag means user mode pends on event which will never 1246 * reach due to lack of at least one CRTC for signaling 1247 */ 1248 if (c == 0 && (arg->flags & DRM_MODE_PAGE_FLIP_EVENT)) 1249 return -EINVAL; 1250 1251 return 0; 1252 } 1253 1254 static void complete_signaling(struct drm_device *dev, 1255 struct drm_atomic_state *state, 1256 struct drm_out_fence_state *fence_state, 1257 unsigned int num_fences, 1258 bool install_fds) 1259 { 1260 struct drm_crtc *crtc; 1261 struct drm_crtc_state *crtc_state; 1262 int i; 1263 1264 if (install_fds) { 1265 for (i = 0; i < num_fences; i++) 1266 fd_install(fence_state[i].fd, 1267 fence_state[i].sync_file->file); 1268 1269 kfree(fence_state); 1270 return; 1271 } 1272 1273 for_each_new_crtc_in_state(state, crtc, crtc_state, i) { 1274 struct drm_pending_vblank_event *event = crtc_state->event; 1275 /* 1276 * Free the allocated event. drm_atomic_helper_setup_commit 1277 * can allocate an event too, so only free it if it's ours 1278 * to prevent a double free in drm_atomic_state_clear. 1279 */ 1280 if (event && (event->base.fence || event->base.file_priv)) { 1281 drm_event_cancel_free(dev, &event->base); 1282 crtc_state->event = NULL; 1283 } 1284 } 1285 1286 if (!fence_state) 1287 return; 1288 1289 for (i = 0; i < num_fences; i++) { 1290 if (fence_state[i].sync_file) 1291 fput(fence_state[i].sync_file->file); 1292 if (fence_state[i].fd >= 0) 1293 put_unused_fd(fence_state[i].fd); 1294 1295 /* If this fails log error to the user */ 1296 if (fence_state[i].out_fence_ptr && 1297 put_user(-1, fence_state[i].out_fence_ptr)) 1298 DRM_DEBUG_ATOMIC("Couldn't clear out_fence_ptr\n"); 1299 } 1300 1301 kfree(fence_state); 1302 } 1303 1304 int drm_mode_atomic_ioctl(struct drm_device *dev, 1305 void *data, struct drm_file *file_priv) 1306 { 1307 struct drm_mode_atomic *arg = data; 1308 uint32_t __user *objs_ptr = (uint32_t __user *)(unsigned long)(arg->objs_ptr); 1309 uint32_t __user *count_props_ptr = (uint32_t __user *)(unsigned long)(arg->count_props_ptr); 1310 uint32_t __user *props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr); 1311 uint64_t __user *prop_values_ptr = (uint64_t __user *)(unsigned long)(arg->prop_values_ptr); 1312 unsigned int copied_objs, copied_props; 1313 struct drm_atomic_state *state; 1314 struct drm_modeset_acquire_ctx ctx; 1315 struct drm_out_fence_state *fence_state; 1316 int ret = 0; 1317 unsigned int i, j, num_fences; 1318 1319 /* disallow for drivers not supporting atomic: */ 1320 if (!drm_core_check_feature(dev, DRIVER_ATOMIC)) 1321 return -EOPNOTSUPP; 1322 1323 /* disallow for userspace that has not enabled atomic cap (even 1324 * though this may be a bit overkill, since legacy userspace 1325 * wouldn't know how to call this ioctl) 1326 */ 1327 if (!file_priv->atomic) 1328 return -EINVAL; 1329 1330 if (arg->flags & ~DRM_MODE_ATOMIC_FLAGS) 1331 return -EINVAL; 1332 1333 if (arg->reserved) 1334 return -EINVAL; 1335 1336 if (arg->flags & DRM_MODE_PAGE_FLIP_ASYNC) 1337 return -EINVAL; 1338 1339 /* can't test and expect an event at the same time. */ 1340 if ((arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) && 1341 (arg->flags & DRM_MODE_PAGE_FLIP_EVENT)) 1342 return -EINVAL; 1343 1344 state = drm_atomic_state_alloc(dev); 1345 if (!state) 1346 return -ENOMEM; 1347 1348 drm_modeset_acquire_init(&ctx, DRM_MODESET_ACQUIRE_INTERRUPTIBLE); 1349 state->acquire_ctx = &ctx; 1350 state->allow_modeset = !!(arg->flags & DRM_MODE_ATOMIC_ALLOW_MODESET); 1351 1352 retry: 1353 copied_objs = 0; 1354 copied_props = 0; 1355 fence_state = NULL; 1356 num_fences = 0; 1357 1358 for (i = 0; i < arg->count_objs; i++) { 1359 uint32_t obj_id, count_props; 1360 struct drm_mode_object *obj; 1361 1362 if (get_user(obj_id, objs_ptr + copied_objs)) { 1363 ret = -EFAULT; 1364 goto out; 1365 } 1366 1367 obj = drm_mode_object_find(dev, file_priv, obj_id, DRM_MODE_OBJECT_ANY); 1368 if (!obj) { 1369 ret = -ENOENT; 1370 goto out; 1371 } 1372 1373 if (!obj->properties) { 1374 drm_mode_object_put(obj); 1375 ret = -ENOENT; 1376 goto out; 1377 } 1378 1379 if (get_user(count_props, count_props_ptr + copied_objs)) { 1380 drm_mode_object_put(obj); 1381 ret = -EFAULT; 1382 goto out; 1383 } 1384 1385 copied_objs++; 1386 1387 for (j = 0; j < count_props; j++) { 1388 uint32_t prop_id; 1389 uint64_t prop_value; 1390 struct drm_property *prop; 1391 1392 if (get_user(prop_id, props_ptr + copied_props)) { 1393 drm_mode_object_put(obj); 1394 ret = -EFAULT; 1395 goto out; 1396 } 1397 1398 prop = drm_mode_obj_find_prop_id(obj, prop_id); 1399 if (!prop) { 1400 drm_mode_object_put(obj); 1401 ret = -ENOENT; 1402 goto out; 1403 } 1404 1405 if (copy_from_user(&prop_value, 1406 prop_values_ptr + copied_props, 1407 sizeof(prop_value))) { 1408 drm_mode_object_put(obj); 1409 ret = -EFAULT; 1410 goto out; 1411 } 1412 1413 ret = drm_atomic_set_property(state, file_priv, 1414 obj, prop, prop_value); 1415 if (ret) { 1416 drm_mode_object_put(obj); 1417 goto out; 1418 } 1419 1420 copied_props++; 1421 } 1422 1423 drm_mode_object_put(obj); 1424 } 1425 1426 ret = prepare_signaling(dev, state, arg, file_priv, &fence_state, 1427 &num_fences); 1428 if (ret) 1429 goto out; 1430 1431 if (arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) { 1432 ret = drm_atomic_check_only(state); 1433 } else if (arg->flags & DRM_MODE_ATOMIC_NONBLOCK) { 1434 ret = drm_atomic_nonblocking_commit(state); 1435 } else { 1436 if (drm_debug_enabled(DRM_UT_STATE)) 1437 drm_atomic_print_state(state); 1438 1439 ret = drm_atomic_commit(state); 1440 } 1441 1442 out: 1443 complete_signaling(dev, state, fence_state, num_fences, !ret); 1444 1445 if (ret == -EDEADLK) { 1446 drm_atomic_state_clear(state); 1447 ret = drm_modeset_backoff(&ctx); 1448 if (!ret) 1449 goto retry; 1450 } 1451 1452 drm_atomic_state_put(state); 1453 1454 drm_modeset_drop_locks(&ctx); 1455 drm_modeset_acquire_fini(&ctx); 1456 1457 return ret; 1458 } 1459