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