1 /* 2 * Copyright (c) 2006-2008 Intel Corporation 3 * Copyright (c) 2007 Dave Airlie <airlied@linux.ie> 4 * Copyright (c) 2008 Red Hat Inc. 5 * 6 * DRM core CRTC related functions 7 * 8 * Permission to use, copy, modify, distribute, and sell this software and its 9 * documentation for any purpose is hereby granted without fee, provided that 10 * the above copyright notice appear in all copies and that both that copyright 11 * notice and this permission notice appear in supporting documentation, and 12 * that the name of the copyright holders not be used in advertising or 13 * publicity pertaining to distribution of the software without specific, 14 * written prior permission. The copyright holders make no representations 15 * about the suitability of this software for any purpose. It is provided "as 16 * is" without express or implied warranty. 17 * 18 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 19 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO 20 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR 21 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, 22 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 23 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 24 * OF THIS SOFTWARE. 25 * 26 * Authors: 27 * Keith Packard 28 * Eric Anholt <eric@anholt.net> 29 * Dave Airlie <airlied@linux.ie> 30 * Jesse Barnes <jesse.barnes@intel.com> 31 */ 32 #include <linux/ctype.h> 33 #include <linux/list.h> 34 #include <linux/slab.h> 35 #include <linux/export.h> 36 #include <linux/dma-fence.h> 37 #include <linux/uaccess.h> 38 #include <drm/drm_blend.h> 39 #include <drm/drm_crtc.h> 40 #include <drm/drm_edid.h> 41 #include <drm/drm_fourcc.h> 42 #include <drm/drm_framebuffer.h> 43 #include <drm/drm_managed.h> 44 #include <drm/drm_modeset_lock.h> 45 #include <drm/drm_atomic.h> 46 #include <drm/drm_auth.h> 47 #include <drm/drm_debugfs_crc.h> 48 #include <drm/drm_drv.h> 49 #include <drm/drm_print.h> 50 #include <drm/drm_file.h> 51 52 #include "drm_crtc_internal.h" 53 #include "drm_internal.h" 54 55 /** 56 * DOC: overview 57 * 58 * A CRTC represents the overall display pipeline. It receives pixel data from 59 * &drm_plane and blends them together. The &drm_display_mode is also attached 60 * to the CRTC, specifying display timings. On the output side the data is fed 61 * to one or more &drm_encoder, which are then each connected to one 62 * &drm_connector. 63 * 64 * To create a CRTC, a KMS driver allocates and zeroes an instance of 65 * &struct drm_crtc (possibly as part of a larger structure) and registers it 66 * with a call to drm_crtc_init_with_planes(). 67 * 68 * The CRTC is also the entry point for legacy modeset operations (see 69 * &drm_crtc_funcs.set_config), legacy plane operations (see 70 * &drm_crtc_funcs.page_flip and &drm_crtc_funcs.cursor_set2), and other legacy 71 * operations like &drm_crtc_funcs.gamma_set. For atomic drivers all these 72 * features are controlled through &drm_property and 73 * &drm_mode_config_funcs.atomic_check. 74 */ 75 76 /** 77 * drm_crtc_from_index - find the registered CRTC at an index 78 * @dev: DRM device 79 * @idx: index of registered CRTC to find for 80 * 81 * Given a CRTC index, return the registered CRTC from DRM device's 82 * list of CRTCs with matching index. This is the inverse of drm_crtc_index(). 83 * It's useful in the vblank callbacks (like &drm_driver.enable_vblank or 84 * &drm_driver.disable_vblank), since that still deals with indices instead 85 * of pointers to &struct drm_crtc." 86 */ 87 struct drm_crtc *drm_crtc_from_index(struct drm_device *dev, int idx) 88 { 89 struct drm_crtc *crtc; 90 91 drm_for_each_crtc(crtc, dev) 92 if (idx == crtc->index) 93 return crtc; 94 95 return NULL; 96 } 97 EXPORT_SYMBOL(drm_crtc_from_index); 98 99 int drm_crtc_force_disable(struct drm_crtc *crtc) 100 { 101 struct drm_mode_set set = { 102 .crtc = crtc, 103 }; 104 105 WARN_ON(drm_drv_uses_atomic_modeset(crtc->dev)); 106 107 return drm_mode_set_config_internal(&set); 108 } 109 110 int drm_crtc_register_all(struct drm_device *dev) 111 { 112 struct drm_crtc *crtc; 113 int ret = 0; 114 115 drm_for_each_crtc(crtc, dev) { 116 drm_debugfs_crtc_add(crtc); 117 118 if (crtc->funcs->late_register) 119 ret = crtc->funcs->late_register(crtc); 120 if (ret) 121 return ret; 122 } 123 124 return 0; 125 } 126 127 void drm_crtc_unregister_all(struct drm_device *dev) 128 { 129 struct drm_crtc *crtc; 130 131 drm_for_each_crtc(crtc, dev) { 132 if (crtc->funcs->early_unregister) 133 crtc->funcs->early_unregister(crtc); 134 drm_debugfs_crtc_remove(crtc); 135 } 136 } 137 138 static int drm_crtc_crc_init(struct drm_crtc *crtc) 139 { 140 #ifdef CONFIG_DEBUG_FS 141 spin_lock_init(&crtc->crc.lock); 142 init_waitqueue_head(&crtc->crc.wq); 143 crtc->crc.source = kstrdup("auto", GFP_KERNEL); 144 if (!crtc->crc.source) 145 return -ENOMEM; 146 #endif 147 return 0; 148 } 149 150 static void drm_crtc_crc_fini(struct drm_crtc *crtc) 151 { 152 #ifdef CONFIG_DEBUG_FS 153 kfree(crtc->crc.source); 154 #endif 155 } 156 157 static struct drm_crtc *fence_to_crtc(struct dma_fence *fence) 158 { 159 return container_of(fence->extern_lock, struct drm_crtc, fence_lock); 160 } 161 162 static const char *drm_crtc_fence_get_driver_name(struct dma_fence *fence) 163 { 164 struct drm_crtc *crtc = fence_to_crtc(fence); 165 166 return crtc->dev->driver->name; 167 } 168 169 static const char *drm_crtc_fence_get_timeline_name(struct dma_fence *fence) 170 { 171 struct drm_crtc *crtc = fence_to_crtc(fence); 172 173 return crtc->timeline_name; 174 } 175 176 static const struct dma_fence_ops drm_crtc_fence_ops = { 177 .get_driver_name = drm_crtc_fence_get_driver_name, 178 .get_timeline_name = drm_crtc_fence_get_timeline_name, 179 }; 180 181 struct dma_fence *drm_crtc_create_fence(struct drm_crtc *crtc) 182 { 183 struct dma_fence *fence; 184 185 fence = kzalloc_obj(*fence); 186 if (!fence) 187 return NULL; 188 189 dma_fence_init(fence, &drm_crtc_fence_ops, &crtc->fence_lock, 190 crtc->fence_context, ++crtc->fence_seqno); 191 192 return fence; 193 } 194 195 /** 196 * DOC: standard CRTC properties 197 * 198 * DRM CRTCs have a few standardized properties: 199 * 200 * ACTIVE: 201 * Atomic property for setting the power state of the CRTC. When set to 1 202 * the CRTC will actively display content. When set to 0 the CRTC will be 203 * powered off. There is no expectation that user-space will reset CRTC 204 * resources like the mode and planes when setting ACTIVE to 0. 205 * 206 * User-space can rely on an ACTIVE change to 1 to never fail an atomic 207 * test as long as no other property has changed. If a change to ACTIVE 208 * fails an atomic test, this is a driver bug. For this reason setting 209 * ACTIVE to 0 must not release internal resources (like reserved memory 210 * bandwidth or clock generators). 211 * 212 * Note that the legacy DPMS property on connectors is internally routed 213 * to control this property for atomic drivers. 214 * MODE_ID: 215 * Atomic property for setting the CRTC display timings. The value is the 216 * ID of a blob containing the DRM mode info. To disable the CRTC, 217 * user-space must set this property to 0. 218 * 219 * Setting MODE_ID to 0 will release reserved resources for the CRTC. 220 * SCALING_FILTER: 221 * Atomic property for setting the scaling filter for CRTC scaler 222 * 223 * The value of this property can be one of the following: 224 * 225 * Default: 226 * Driver's default scaling filter 227 * Nearest Neighbor: 228 * Nearest Neighbor scaling filter 229 * SHARPNESS_STRENGTH: 230 * Atomic property for setting the sharpness strength/intensity by userspace. 231 * 232 * The value of this property is set as an integer value ranging 233 * from 0 - 255 where: 234 * 235 * 0: Sharpness feature is disabled(default value). 236 * 237 * 1: Minimum sharpness. 238 * 239 * 255: Maximum sharpness. 240 * 241 * User can gradually increase or decrease the sharpness level and can 242 * set the optimum value depending on content. 243 * This value will be passed to kernel through the UAPI. 244 * The setting of this property does not require modeset. 245 * The sharpness effect takes place post blending on the final composed output. 246 * If the feature is disabled, the content remains same without any sharpening effect 247 * and when this feature is applied, it enhances the clarity of the content. 248 */ 249 250 __printf(6, 0) 251 static int __drm_crtc_init_with_planes(struct drm_device *dev, struct drm_crtc *crtc, 252 struct drm_plane *primary, 253 struct drm_plane *cursor, 254 const struct drm_crtc_funcs *funcs, 255 const char *name, va_list ap) 256 { 257 struct drm_mode_config *config = &dev->mode_config; 258 int ret; 259 260 WARN_ON(primary && primary->type != DRM_PLANE_TYPE_PRIMARY); 261 WARN_ON(cursor && cursor->type != DRM_PLANE_TYPE_CURSOR); 262 263 /* crtc index is used with 32bit bitmasks */ 264 if (WARN_ON(config->num_crtc >= 32)) 265 return -EINVAL; 266 267 WARN_ON(drm_drv_uses_atomic_modeset(dev) && 268 (!funcs->atomic_destroy_state || 269 !funcs->atomic_duplicate_state)); 270 271 crtc->dev = dev; 272 crtc->funcs = funcs; 273 274 INIT_LIST_HEAD(&crtc->commit_list); 275 spin_lock_init(&crtc->commit_lock); 276 277 drm_modeset_lock_init(&crtc->mutex); 278 ret = drm_mode_object_add(dev, &crtc->base, DRM_MODE_OBJECT_CRTC); 279 if (ret) 280 return ret; 281 282 if (name) { 283 crtc->name = kvasprintf(GFP_KERNEL, name, ap); 284 } else { 285 crtc->name = kasprintf(GFP_KERNEL, "crtc-%d", config->num_crtc); 286 } 287 if (!crtc->name) { 288 drm_mode_object_unregister(dev, &crtc->base); 289 return -ENOMEM; 290 } 291 292 crtc->fence_context = dma_fence_context_alloc(1); 293 spin_lock_init(&crtc->fence_lock); 294 snprintf(crtc->timeline_name, sizeof(crtc->timeline_name), 295 "CRTC:%d-%s", crtc->base.id, crtc->name); 296 297 crtc->base.properties = &crtc->properties; 298 299 list_add_tail(&crtc->head, &config->crtc_list); 300 crtc->index = config->num_crtc++; 301 302 crtc->primary = primary; 303 crtc->cursor = cursor; 304 if (primary && !primary->possible_crtcs) 305 primary->possible_crtcs = drm_crtc_mask(crtc); 306 if (cursor && !cursor->possible_crtcs) 307 cursor->possible_crtcs = drm_crtc_mask(crtc); 308 309 ret = drm_crtc_crc_init(crtc); 310 if (ret) { 311 drm_mode_object_unregister(dev, &crtc->base); 312 return ret; 313 } 314 315 if (drm_core_check_feature(dev, DRIVER_ATOMIC)) { 316 drm_object_attach_property(&crtc->base, config->prop_active, 0); 317 drm_object_attach_property(&crtc->base, config->prop_mode_id, 0); 318 drm_object_attach_property(&crtc->base, 319 config->prop_out_fence_ptr, 0); 320 drm_object_attach_property(&crtc->base, 321 config->prop_vrr_enabled, 0); 322 } 323 324 return 0; 325 } 326 327 /** 328 * drm_crtc_init_with_planes - Initialise a new CRTC object with 329 * specified primary and cursor planes. 330 * @dev: DRM device 331 * @crtc: CRTC object to init 332 * @primary: Primary plane for CRTC 333 * @cursor: Cursor plane for CRTC 334 * @funcs: callbacks for the new CRTC 335 * @name: printf style format string for the CRTC name, or NULL for default name 336 * 337 * Inits a new object created as base part of a driver crtc object. Drivers 338 * should use this function instead of drm_crtc_init(), which is only provided 339 * for backwards compatibility with drivers which do not yet support universal 340 * planes). 341 * The &drm_crtc_funcs.destroy hook should call drm_crtc_cleanup() and kfree() 342 * the crtc structure. The crtc structure should not be allocated with 343 * devm_kzalloc(). 344 * 345 * The @primary and @cursor planes are only relevant for legacy uAPI, see 346 * &drm_crtc.primary and &drm_crtc.cursor. 347 * 348 * Note: consider using drmm_crtc_alloc_with_planes() or 349 * drmm_crtc_init_with_planes() instead of drm_crtc_init_with_planes() 350 * to let the DRM managed resource infrastructure take care of cleanup 351 * and deallocation. 352 * 353 * Returns: 354 * Zero on success, error code on failure. 355 */ 356 int drm_crtc_init_with_planes(struct drm_device *dev, struct drm_crtc *crtc, 357 struct drm_plane *primary, 358 struct drm_plane *cursor, 359 const struct drm_crtc_funcs *funcs, 360 const char *name, ...) 361 { 362 va_list ap; 363 int ret; 364 365 WARN_ON(!funcs->destroy); 366 367 va_start(ap, name); 368 ret = __drm_crtc_init_with_planes(dev, crtc, primary, cursor, funcs, 369 name, ap); 370 va_end(ap); 371 372 return ret; 373 } 374 EXPORT_SYMBOL(drm_crtc_init_with_planes); 375 376 static void drmm_crtc_init_with_planes_cleanup(struct drm_device *dev, 377 void *ptr) 378 { 379 struct drm_crtc *crtc = ptr; 380 381 drm_crtc_cleanup(crtc); 382 } 383 384 __printf(6, 0) 385 static int __drmm_crtc_init_with_planes(struct drm_device *dev, 386 struct drm_crtc *crtc, 387 struct drm_plane *primary, 388 struct drm_plane *cursor, 389 const struct drm_crtc_funcs *funcs, 390 const char *name, 391 va_list args) 392 { 393 int ret; 394 395 drm_WARN_ON(dev, funcs && funcs->destroy); 396 397 ret = __drm_crtc_init_with_planes(dev, crtc, primary, cursor, funcs, 398 name, args); 399 if (ret) 400 return ret; 401 402 ret = drmm_add_action_or_reset(dev, drmm_crtc_init_with_planes_cleanup, 403 crtc); 404 if (ret) 405 return ret; 406 407 return 0; 408 } 409 410 /** 411 * drmm_crtc_init_with_planes - Initialise a new CRTC object with 412 * specified primary and cursor planes. 413 * @dev: DRM device 414 * @crtc: CRTC object to init 415 * @primary: Primary plane for CRTC 416 * @cursor: Cursor plane for CRTC 417 * @funcs: callbacks for the new CRTC 418 * @name: printf style format string for the CRTC name, or NULL for default name 419 * 420 * Inits a new object created as base part of a driver crtc object. Drivers 421 * should use this function instead of drm_crtc_init(), which is only provided 422 * for backwards compatibility with drivers which do not yet support universal 423 * planes). 424 * 425 * Cleanup is automatically handled through registering 426 * drmm_crtc_cleanup() with drmm_add_action(). The crtc structure should 427 * be allocated with drmm_kzalloc(). 428 * 429 * The @drm_crtc_funcs.destroy hook must be NULL. 430 * 431 * The @primary and @cursor planes are only relevant for legacy uAPI, see 432 * &drm_crtc.primary and &drm_crtc.cursor. 433 * 434 * Returns: 435 * Zero on success, error code on failure. 436 */ 437 int drmm_crtc_init_with_planes(struct drm_device *dev, struct drm_crtc *crtc, 438 struct drm_plane *primary, 439 struct drm_plane *cursor, 440 const struct drm_crtc_funcs *funcs, 441 const char *name, ...) 442 { 443 va_list ap; 444 int ret; 445 446 va_start(ap, name); 447 ret = __drmm_crtc_init_with_planes(dev, crtc, primary, cursor, funcs, 448 name, ap); 449 va_end(ap); 450 if (ret) 451 return ret; 452 453 return 0; 454 } 455 EXPORT_SYMBOL(drmm_crtc_init_with_planes); 456 457 void *__drmm_crtc_alloc_with_planes(struct drm_device *dev, 458 size_t size, size_t offset, 459 struct drm_plane *primary, 460 struct drm_plane *cursor, 461 const struct drm_crtc_funcs *funcs, 462 const char *name, ...) 463 { 464 void *container; 465 struct drm_crtc *crtc; 466 va_list ap; 467 int ret; 468 469 if (WARN_ON(!funcs || funcs->destroy)) 470 return ERR_PTR(-EINVAL); 471 472 container = drmm_kzalloc(dev, size, GFP_KERNEL); 473 if (!container) 474 return ERR_PTR(-ENOMEM); 475 476 crtc = container + offset; 477 478 va_start(ap, name); 479 ret = __drmm_crtc_init_with_planes(dev, crtc, primary, cursor, funcs, 480 name, ap); 481 va_end(ap); 482 if (ret) 483 return ERR_PTR(ret); 484 485 return container; 486 } 487 EXPORT_SYMBOL(__drmm_crtc_alloc_with_planes); 488 489 /** 490 * drm_crtc_cleanup - Clean up the core crtc usage 491 * @crtc: CRTC to cleanup 492 * 493 * This function cleans up @crtc and removes it from the DRM mode setting core, 494 * after first waiting an RCU grace period to ensure @crtc->dev can safely be 495 * dereferenced by our dma_fence_ops. 496 * 497 * Note that the function does *not* free the crtc structure itself, this is the 498 * responsibility of the caller. 499 */ 500 void drm_crtc_cleanup(struct drm_crtc *crtc) 501 { 502 struct drm_device *dev = crtc->dev; 503 504 /* Ensure our dma_fence_ops remain valid for an RCU grace period after 505 * the fence is signaled. This is necessary because our dma_fence_ops 506 * dereference crtc->dev. 507 */ 508 synchronize_rcu(); 509 510 /* Note that the crtc_list is considered to be static; should we 511 * remove the drm_crtc at runtime we would have to decrement all 512 * the indices on the drm_crtc after us in the crtc_list. 513 */ 514 515 drm_crtc_crc_fini(crtc); 516 517 kfree(crtc->gamma_store); 518 crtc->gamma_store = NULL; 519 520 drm_modeset_lock_fini(&crtc->mutex); 521 522 drm_mode_object_unregister(dev, &crtc->base); 523 list_del(&crtc->head); 524 dev->mode_config.num_crtc--; 525 526 WARN_ON(crtc->state && !crtc->funcs->atomic_destroy_state); 527 if (crtc->state && crtc->funcs->atomic_destroy_state) 528 crtc->funcs->atomic_destroy_state(crtc, crtc->state); 529 530 kfree(crtc->name); 531 532 memset(crtc, 0, sizeof(*crtc)); 533 } 534 EXPORT_SYMBOL(drm_crtc_cleanup); 535 536 /** 537 * drm_mode_getcrtc - get CRTC configuration 538 * @dev: drm device for the ioctl 539 * @data: data pointer for the ioctl 540 * @file_priv: drm file for the ioctl call 541 * 542 * Construct a CRTC configuration structure to return to the user. 543 * 544 * Called by the user via ioctl. 545 * 546 * Returns: 547 * Zero on success, negative errno on failure. 548 */ 549 int drm_mode_getcrtc(struct drm_device *dev, 550 void *data, struct drm_file *file_priv) 551 { 552 struct drm_mode_crtc *crtc_resp = data; 553 struct drm_crtc *crtc; 554 struct drm_plane *plane; 555 556 if (!drm_core_check_feature(dev, DRIVER_MODESET)) 557 return -EOPNOTSUPP; 558 559 crtc = drm_crtc_find(dev, file_priv, crtc_resp->crtc_id); 560 if (!crtc) 561 return -ENOENT; 562 563 plane = crtc->primary; 564 565 crtc_resp->gamma_size = crtc->gamma_size; 566 567 drm_modeset_lock(&plane->mutex, NULL); 568 if (plane->state && plane->state->fb) 569 crtc_resp->fb_id = plane->state->fb->base.id; 570 else if (!plane->state && plane->fb) 571 crtc_resp->fb_id = plane->fb->base.id; 572 else 573 crtc_resp->fb_id = 0; 574 575 if (plane->state) { 576 crtc_resp->x = plane->state->src_x >> 16; 577 crtc_resp->y = plane->state->src_y >> 16; 578 } 579 drm_modeset_unlock(&plane->mutex); 580 581 drm_modeset_lock(&crtc->mutex, NULL); 582 if (crtc->state) { 583 if (crtc->state->enable) { 584 drm_mode_convert_to_umode(&crtc_resp->mode, &crtc->state->mode); 585 crtc_resp->mode_valid = 1; 586 } else { 587 crtc_resp->mode_valid = 0; 588 } 589 } else { 590 crtc_resp->x = crtc->x; 591 crtc_resp->y = crtc->y; 592 593 if (crtc->enabled) { 594 drm_mode_convert_to_umode(&crtc_resp->mode, &crtc->mode); 595 crtc_resp->mode_valid = 1; 596 597 } else { 598 crtc_resp->mode_valid = 0; 599 } 600 } 601 if (!file_priv->aspect_ratio_allowed) 602 crtc_resp->mode.flags &= ~DRM_MODE_FLAG_PIC_AR_MASK; 603 drm_modeset_unlock(&crtc->mutex); 604 605 return 0; 606 } 607 608 static int __drm_mode_set_config_internal(struct drm_mode_set *set, 609 struct drm_modeset_acquire_ctx *ctx) 610 { 611 struct drm_crtc *crtc = set->crtc; 612 struct drm_framebuffer *fb; 613 struct drm_crtc *tmp; 614 int ret; 615 616 WARN_ON(drm_drv_uses_atomic_modeset(crtc->dev)); 617 618 /* 619 * NOTE: ->set_config can also disable other crtcs (if we steal all 620 * connectors from it), hence we need to refcount the fbs across all 621 * crtcs. Atomic modeset will have saner semantics ... 622 */ 623 drm_for_each_crtc(tmp, crtc->dev) { 624 struct drm_plane *plane = tmp->primary; 625 626 plane->old_fb = plane->fb; 627 } 628 629 fb = set->fb; 630 631 ret = crtc->funcs->set_config(set, ctx); 632 if (ret == 0) { 633 struct drm_plane *plane = crtc->primary; 634 635 plane->crtc = fb ? crtc : NULL; 636 plane->fb = fb; 637 } 638 639 drm_for_each_crtc(tmp, crtc->dev) { 640 struct drm_plane *plane = tmp->primary; 641 642 if (plane->fb) 643 drm_framebuffer_get(plane->fb); 644 if (plane->old_fb) 645 drm_framebuffer_put(plane->old_fb); 646 plane->old_fb = NULL; 647 } 648 649 return ret; 650 } 651 652 /** 653 * drm_mode_set_config_internal - helper to call &drm_mode_config_funcs.set_config 654 * @set: modeset config to set 655 * 656 * This is a little helper to wrap internal calls to the 657 * &drm_mode_config_funcs.set_config driver interface. The only thing it adds is 658 * correct refcounting dance. 659 * 660 * This should only be used by non-atomic legacy drivers. 661 * 662 * Returns: 663 * Zero on success, negative errno on failure. 664 */ 665 int drm_mode_set_config_internal(struct drm_mode_set *set) 666 { 667 WARN_ON(drm_drv_uses_atomic_modeset(set->crtc->dev)); 668 669 return __drm_mode_set_config_internal(set, NULL); 670 } 671 EXPORT_SYMBOL(drm_mode_set_config_internal); 672 673 /** 674 * drm_crtc_check_viewport - Checks that a framebuffer is big enough for the 675 * CRTC viewport 676 * @crtc: CRTC that framebuffer will be displayed on 677 * @x: x panning 678 * @y: y panning 679 * @mode: mode that framebuffer will be displayed under 680 * @fb: framebuffer to check size of 681 */ 682 int drm_crtc_check_viewport(const struct drm_crtc *crtc, 683 int x, int y, 684 const struct drm_display_mode *mode, 685 const struct drm_framebuffer *fb) 686 687 { 688 int hdisplay, vdisplay; 689 690 drm_mode_get_hv_timing(mode, &hdisplay, &vdisplay); 691 692 if (crtc->state && 693 drm_rotation_90_or_270(crtc->primary->state->rotation)) 694 swap(hdisplay, vdisplay); 695 696 return drm_framebuffer_check_src_coords(x << 16, y << 16, 697 hdisplay << 16, vdisplay << 16, 698 fb); 699 } 700 EXPORT_SYMBOL(drm_crtc_check_viewport); 701 702 /** 703 * drm_mode_setcrtc - set CRTC configuration 704 * @dev: drm device for the ioctl 705 * @data: data pointer for the ioctl 706 * @file_priv: drm file for the ioctl call 707 * 708 * Build a new CRTC configuration based on user request. 709 * 710 * Called by the user via ioctl. 711 * 712 * Returns: 713 * Zero on success, negative errno on failure. 714 */ 715 int drm_mode_setcrtc(struct drm_device *dev, void *data, 716 struct drm_file *file_priv) 717 { 718 struct drm_mode_config *config = &dev->mode_config; 719 struct drm_mode_crtc *crtc_req = data; 720 struct drm_crtc *crtc; 721 struct drm_plane *plane; 722 struct drm_connector **connector_set = NULL, *connector; 723 struct drm_framebuffer *fb = NULL; 724 struct drm_display_mode *mode = NULL; 725 struct drm_mode_set set; 726 uint32_t __user *set_connectors_ptr; 727 struct drm_modeset_acquire_ctx ctx; 728 int ret, i, num_connectors = 0; 729 730 if (!drm_core_check_feature(dev, DRIVER_MODESET)) 731 return -EOPNOTSUPP; 732 733 /* 734 * Universal plane src offsets are only 16.16, prevent havoc for 735 * drivers using universal plane code internally. 736 */ 737 if (crtc_req->x & 0xffff0000 || crtc_req->y & 0xffff0000) 738 return -ERANGE; 739 740 crtc = drm_crtc_find(dev, file_priv, crtc_req->crtc_id); 741 if (!crtc) { 742 drm_dbg_kms(dev, "Unknown CRTC ID %d\n", crtc_req->crtc_id); 743 return -ENOENT; 744 } 745 drm_dbg_kms(dev, "[CRTC:%d:%s]\n", crtc->base.id, crtc->name); 746 747 plane = crtc->primary; 748 749 /* allow disabling with the primary plane leased */ 750 if (crtc_req->mode_valid && !drm_lease_held(file_priv, plane->base.id)) 751 return -EACCES; 752 753 DRM_MODESET_LOCK_ALL_BEGIN(dev, ctx, 754 DRM_MODESET_ACQUIRE_INTERRUPTIBLE, ret); 755 756 if (crtc_req->mode_valid) { 757 /* If we have a mode we need a framebuffer. */ 758 /* If we pass -1, set the mode with the currently bound fb */ 759 if (crtc_req->fb_id == -1) { 760 struct drm_framebuffer *old_fb; 761 762 if (plane->state) 763 old_fb = plane->state->fb; 764 else 765 old_fb = plane->fb; 766 767 if (!old_fb) { 768 drm_dbg_kms(dev, "CRTC doesn't have current FB\n"); 769 ret = -EINVAL; 770 goto out; 771 } 772 773 fb = old_fb; 774 /* Make refcounting symmetric with the lookup path. */ 775 drm_framebuffer_get(fb); 776 } else { 777 fb = drm_framebuffer_lookup(dev, file_priv, crtc_req->fb_id); 778 if (!fb) { 779 drm_dbg_kms(dev, "Unknown FB ID%d\n", 780 crtc_req->fb_id); 781 ret = -ENOENT; 782 goto out; 783 } 784 } 785 786 mode = drm_mode_create(dev); 787 if (!mode) { 788 ret = -ENOMEM; 789 goto out; 790 } 791 if (!file_priv->aspect_ratio_allowed && 792 (crtc_req->mode.flags & DRM_MODE_FLAG_PIC_AR_MASK) != DRM_MODE_FLAG_PIC_AR_NONE) { 793 drm_dbg_kms(dev, "Unexpected aspect-ratio flag bits\n"); 794 ret = -EINVAL; 795 goto out; 796 } 797 798 799 ret = drm_mode_convert_umode(dev, mode, &crtc_req->mode); 800 if (ret) { 801 drm_dbg_kms(dev, "Invalid mode (%s, %pe): " DRM_MODE_FMT "\n", 802 drm_get_mode_status_name(mode->status), 803 ERR_PTR(ret), DRM_MODE_ARG(mode)); 804 goto out; 805 } 806 807 /* 808 * Check whether the primary plane supports the fb pixel format. 809 * Drivers not implementing the universal planes API use a 810 * default formats list provided by the DRM core which doesn't 811 * match real hardware capabilities. Skip the check in that 812 * case. 813 */ 814 if (!plane->format_default) { 815 if (!drm_plane_has_format(plane, fb->format->format, fb->modifier)) { 816 drm_dbg_kms(dev, "Invalid pixel format %p4cc, modifier 0x%llx\n", 817 &fb->format->format, fb->modifier); 818 ret = -EINVAL; 819 goto out; 820 } 821 } 822 823 ret = drm_crtc_check_viewport(crtc, crtc_req->x, crtc_req->y, 824 mode, fb); 825 if (ret) 826 goto out; 827 828 } 829 830 if (crtc_req->count_connectors == 0 && mode) { 831 drm_dbg_kms(dev, "Count connectors is 0 but mode set\n"); 832 ret = -EINVAL; 833 goto out; 834 } 835 836 if (crtc_req->count_connectors > 0 && (!mode || !fb)) { 837 drm_dbg_kms(dev, "Count connectors is %d but no mode or fb set\n", 838 crtc_req->count_connectors); 839 ret = -EINVAL; 840 goto out; 841 } 842 843 if (crtc_req->count_connectors > 0) { 844 u32 out_id; 845 846 /* Avoid unbounded kernel memory allocation */ 847 if (crtc_req->count_connectors > config->num_connector) { 848 ret = -EINVAL; 849 goto out; 850 } 851 852 connector_set = kmalloc_objs(struct drm_connector *, 853 crtc_req->count_connectors); 854 if (!connector_set) { 855 ret = -ENOMEM; 856 goto out; 857 } 858 859 for (i = 0; i < crtc_req->count_connectors; i++) { 860 connector_set[i] = NULL; 861 set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr; 862 if (get_user(out_id, &set_connectors_ptr[i])) { 863 ret = -EFAULT; 864 goto out; 865 } 866 867 connector = drm_connector_lookup(dev, file_priv, out_id); 868 if (!connector) { 869 drm_dbg_kms(dev, "Connector id %d unknown\n", 870 out_id); 871 ret = -ENOENT; 872 goto out; 873 } 874 drm_dbg_kms(dev, "[CONNECTOR:%d:%s]\n", 875 connector->base.id, connector->name); 876 877 connector_set[i] = connector; 878 num_connectors++; 879 } 880 } 881 882 set.crtc = crtc; 883 set.x = crtc_req->x; 884 set.y = crtc_req->y; 885 set.mode = mode; 886 set.connectors = connector_set; 887 set.num_connectors = num_connectors; 888 set.fb = fb; 889 890 if (drm_drv_uses_atomic_modeset(dev)) 891 ret = crtc->funcs->set_config(&set, &ctx); 892 else 893 ret = __drm_mode_set_config_internal(&set, &ctx); 894 895 out: 896 if (fb) 897 drm_framebuffer_put(fb); 898 899 if (connector_set) { 900 for (i = 0; i < num_connectors; i++) { 901 if (connector_set[i]) 902 drm_connector_put(connector_set[i]); 903 } 904 } 905 kfree(connector_set); 906 drm_mode_destroy(dev, mode); 907 908 /* In case we need to retry... */ 909 connector_set = NULL; 910 fb = NULL; 911 mode = NULL; 912 num_connectors = 0; 913 914 DRM_MODESET_LOCK_ALL_END(dev, ctx, ret); 915 916 return ret; 917 } 918 919 int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj, 920 struct drm_property *property, 921 uint64_t value) 922 { 923 int ret = -EINVAL; 924 struct drm_crtc *crtc = obj_to_crtc(obj); 925 926 if (crtc->funcs->set_property) 927 ret = crtc->funcs->set_property(crtc, property, value); 928 if (!ret) 929 drm_object_property_set_value(obj, property, value); 930 931 return ret; 932 } 933 934 /** 935 * drm_crtc_create_scaling_filter_property - create a new scaling filter 936 * property 937 * 938 * @crtc: drm CRTC 939 * @supported_filters: bitmask of supported scaling filters, must include 940 * BIT(DRM_SCALING_FILTER_DEFAULT). 941 * 942 * This function lets driver to enable the scaling filter property on a given 943 * CRTC. 944 * 945 * RETURNS: 946 * Zero for success or -errno 947 */ 948 int drm_crtc_create_scaling_filter_property(struct drm_crtc *crtc, 949 unsigned int supported_filters) 950 { 951 struct drm_property *prop = 952 drm_create_scaling_filter_prop(crtc->dev, supported_filters); 953 954 if (IS_ERR(prop)) 955 return PTR_ERR(prop); 956 957 drm_object_attach_property(&crtc->base, prop, 958 DRM_SCALING_FILTER_DEFAULT); 959 crtc->scaling_filter_property = prop; 960 961 return 0; 962 } 963 EXPORT_SYMBOL(drm_crtc_create_scaling_filter_property); 964 965 int drm_crtc_create_sharpness_strength_property(struct drm_crtc *crtc) 966 { 967 struct drm_device *dev = crtc->dev; 968 struct drm_property *prop = 969 drm_property_create_range(dev, 0, "SHARPNESS_STRENGTH", 0, 255); 970 971 if (!prop) 972 return -ENOMEM; 973 974 crtc->sharpness_strength_property = prop; 975 drm_object_attach_property(&crtc->base, prop, 0); 976 977 return 0; 978 } 979 EXPORT_SYMBOL(drm_crtc_create_sharpness_strength_property); 980 981 /** 982 * drm_crtc_in_clone_mode - check if the given CRTC state is in clone mode 983 * 984 * @crtc_state: CRTC state to check 985 * 986 * This function determines if the given CRTC state is being cloned by multiple 987 * encoders. 988 * 989 * RETURNS: 990 * True if the CRTC state is in clone mode. False otherwise 991 */ 992 bool drm_crtc_in_clone_mode(struct drm_crtc_state *crtc_state) 993 { 994 if (!crtc_state) 995 return false; 996 997 return hweight32(crtc_state->encoder_mask) > 1; 998 } 999 EXPORT_SYMBOL(drm_crtc_in_clone_mode); 1000