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