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/list.h> 33 #include <linux/slab.h> 34 #include <linux/export.h> 35 #include <drm/drmP.h> 36 #include <drm/drm_crtc.h> 37 #include <drm/drm_edid.h> 38 #include <drm/drm_fourcc.h> 39 40 /** 41 * drm_modeset_lock_all - take all modeset locks 42 * @dev: drm device 43 * 44 * This function takes all modeset locks, suitable where a more fine-grained 45 * scheme isn't (yet) implemented. 46 */ 47 void drm_modeset_lock_all(struct drm_device *dev) 48 { 49 struct drm_crtc *crtc; 50 51 mutex_lock(&dev->mode_config.mutex); 52 53 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) 54 mutex_lock_nest_lock(&crtc->mutex, &dev->mode_config.mutex); 55 } 56 EXPORT_SYMBOL(drm_modeset_lock_all); 57 58 /** 59 * drm_modeset_unlock_all - drop all modeset locks 60 * @dev: device 61 */ 62 void drm_modeset_unlock_all(struct drm_device *dev) 63 { 64 struct drm_crtc *crtc; 65 66 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) 67 mutex_unlock(&crtc->mutex); 68 69 mutex_unlock(&dev->mode_config.mutex); 70 } 71 EXPORT_SYMBOL(drm_modeset_unlock_all); 72 73 /** 74 * drm_warn_on_modeset_not_all_locked - check that all modeset locks are locked 75 * @dev: device 76 */ 77 void drm_warn_on_modeset_not_all_locked(struct drm_device *dev) 78 { 79 struct drm_crtc *crtc; 80 81 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) 82 WARN_ON(!mutex_is_locked(&crtc->mutex)); 83 84 WARN_ON(!mutex_is_locked(&dev->mode_config.mutex)); 85 } 86 EXPORT_SYMBOL(drm_warn_on_modeset_not_all_locked); 87 88 /* Avoid boilerplate. I'm tired of typing. */ 89 #define DRM_ENUM_NAME_FN(fnname, list) \ 90 char *fnname(int val) \ 91 { \ 92 int i; \ 93 for (i = 0; i < ARRAY_SIZE(list); i++) { \ 94 if (list[i].type == val) \ 95 return list[i].name; \ 96 } \ 97 return "(unknown)"; \ 98 } 99 100 /* 101 * Global properties 102 */ 103 static struct drm_prop_enum_list drm_dpms_enum_list[] = 104 { { DRM_MODE_DPMS_ON, "On" }, 105 { DRM_MODE_DPMS_STANDBY, "Standby" }, 106 { DRM_MODE_DPMS_SUSPEND, "Suspend" }, 107 { DRM_MODE_DPMS_OFF, "Off" } 108 }; 109 110 DRM_ENUM_NAME_FN(drm_get_dpms_name, drm_dpms_enum_list) 111 112 /* 113 * Optional properties 114 */ 115 static struct drm_prop_enum_list drm_scaling_mode_enum_list[] = 116 { 117 { DRM_MODE_SCALE_NONE, "None" }, 118 { DRM_MODE_SCALE_FULLSCREEN, "Full" }, 119 { DRM_MODE_SCALE_CENTER, "Center" }, 120 { DRM_MODE_SCALE_ASPECT, "Full aspect" }, 121 }; 122 123 static struct drm_prop_enum_list drm_dithering_mode_enum_list[] = 124 { 125 { DRM_MODE_DITHERING_OFF, "Off" }, 126 { DRM_MODE_DITHERING_ON, "On" }, 127 { DRM_MODE_DITHERING_AUTO, "Automatic" }, 128 }; 129 130 /* 131 * Non-global properties, but "required" for certain connectors. 132 */ 133 static struct drm_prop_enum_list drm_dvi_i_select_enum_list[] = 134 { 135 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */ 136 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */ 137 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */ 138 }; 139 140 DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name, drm_dvi_i_select_enum_list) 141 142 static struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list[] = 143 { 144 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */ 145 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */ 146 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */ 147 }; 148 149 DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name, 150 drm_dvi_i_subconnector_enum_list) 151 152 static struct drm_prop_enum_list drm_tv_select_enum_list[] = 153 { 154 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */ 155 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */ 156 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */ 157 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */ 158 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */ 159 }; 160 161 DRM_ENUM_NAME_FN(drm_get_tv_select_name, drm_tv_select_enum_list) 162 163 static struct drm_prop_enum_list drm_tv_subconnector_enum_list[] = 164 { 165 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */ 166 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */ 167 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */ 168 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */ 169 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */ 170 }; 171 172 DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name, 173 drm_tv_subconnector_enum_list) 174 175 static struct drm_prop_enum_list drm_dirty_info_enum_list[] = { 176 { DRM_MODE_DIRTY_OFF, "Off" }, 177 { DRM_MODE_DIRTY_ON, "On" }, 178 { DRM_MODE_DIRTY_ANNOTATE, "Annotate" }, 179 }; 180 181 DRM_ENUM_NAME_FN(drm_get_dirty_info_name, 182 drm_dirty_info_enum_list) 183 184 struct drm_conn_prop_enum_list { 185 int type; 186 char *name; 187 int count; 188 }; 189 190 /* 191 * Connector and encoder types. 192 */ 193 static struct drm_conn_prop_enum_list drm_connector_enum_list[] = 194 { { DRM_MODE_CONNECTOR_Unknown, "Unknown", 0 }, 195 { DRM_MODE_CONNECTOR_VGA, "VGA", 0 }, 196 { DRM_MODE_CONNECTOR_DVII, "DVI-I", 0 }, 197 { DRM_MODE_CONNECTOR_DVID, "DVI-D", 0 }, 198 { DRM_MODE_CONNECTOR_DVIA, "DVI-A", 0 }, 199 { DRM_MODE_CONNECTOR_Composite, "Composite", 0 }, 200 { DRM_MODE_CONNECTOR_SVIDEO, "SVIDEO", 0 }, 201 { DRM_MODE_CONNECTOR_LVDS, "LVDS", 0 }, 202 { DRM_MODE_CONNECTOR_Component, "Component", 0 }, 203 { DRM_MODE_CONNECTOR_9PinDIN, "DIN", 0 }, 204 { DRM_MODE_CONNECTOR_DisplayPort, "DP", 0 }, 205 { DRM_MODE_CONNECTOR_HDMIA, "HDMI-A", 0 }, 206 { DRM_MODE_CONNECTOR_HDMIB, "HDMI-B", 0 }, 207 { DRM_MODE_CONNECTOR_TV, "TV", 0 }, 208 { DRM_MODE_CONNECTOR_eDP, "eDP", 0 }, 209 { DRM_MODE_CONNECTOR_VIRTUAL, "Virtual", 0}, 210 }; 211 212 static struct drm_prop_enum_list drm_encoder_enum_list[] = 213 { { DRM_MODE_ENCODER_NONE, "None" }, 214 { DRM_MODE_ENCODER_DAC, "DAC" }, 215 { DRM_MODE_ENCODER_TMDS, "TMDS" }, 216 { DRM_MODE_ENCODER_LVDS, "LVDS" }, 217 { DRM_MODE_ENCODER_TVDAC, "TV" }, 218 { DRM_MODE_ENCODER_VIRTUAL, "Virtual" }, 219 }; 220 221 char *drm_get_encoder_name(struct drm_encoder *encoder) 222 { 223 static char buf[32]; 224 225 snprintf(buf, 32, "%s-%d", 226 drm_encoder_enum_list[encoder->encoder_type].name, 227 encoder->base.id); 228 return buf; 229 } 230 EXPORT_SYMBOL(drm_get_encoder_name); 231 232 char *drm_get_connector_name(struct drm_connector *connector) 233 { 234 static char buf[32]; 235 236 snprintf(buf, 32, "%s-%d", 237 drm_connector_enum_list[connector->connector_type].name, 238 connector->connector_type_id); 239 return buf; 240 } 241 EXPORT_SYMBOL(drm_get_connector_name); 242 243 char *drm_get_connector_status_name(enum drm_connector_status status) 244 { 245 if (status == connector_status_connected) 246 return "connected"; 247 else if (status == connector_status_disconnected) 248 return "disconnected"; 249 else 250 return "unknown"; 251 } 252 253 /** 254 * drm_mode_object_get - allocate a new modeset identifier 255 * @dev: DRM device 256 * @obj: object pointer, used to generate unique ID 257 * @obj_type: object type 258 * 259 * Create a unique identifier based on @ptr in @dev's identifier space. Used 260 * for tracking modes, CRTCs and connectors. 261 * 262 * RETURNS: 263 * New unique (relative to other objects in @dev) integer identifier for the 264 * object. 265 */ 266 static int drm_mode_object_get(struct drm_device *dev, 267 struct drm_mode_object *obj, uint32_t obj_type) 268 { 269 int new_id = 0; 270 int ret; 271 272 again: 273 if (idr_pre_get(&dev->mode_config.crtc_idr, GFP_KERNEL) == 0) { 274 DRM_ERROR("Ran out memory getting a mode number\n"); 275 return -ENOMEM; 276 } 277 278 mutex_lock(&dev->mode_config.idr_mutex); 279 ret = idr_get_new_above(&dev->mode_config.crtc_idr, obj, 1, &new_id); 280 281 if (!ret) { 282 /* 283 * Set up the object linking under the protection of the idr 284 * lock so that other users can't see inconsistent state. 285 */ 286 obj->id = new_id; 287 obj->type = obj_type; 288 } 289 mutex_unlock(&dev->mode_config.idr_mutex); 290 291 if (ret == -EAGAIN) 292 goto again; 293 294 return ret; 295 } 296 297 /** 298 * drm_mode_object_put - free a modeset identifer 299 * @dev: DRM device 300 * @object: object to free 301 * 302 * Free @id from @dev's unique identifier pool. 303 */ 304 static void drm_mode_object_put(struct drm_device *dev, 305 struct drm_mode_object *object) 306 { 307 mutex_lock(&dev->mode_config.idr_mutex); 308 idr_remove(&dev->mode_config.crtc_idr, object->id); 309 mutex_unlock(&dev->mode_config.idr_mutex); 310 } 311 312 /** 313 * drm_mode_object_find - look up a drm object with static lifetime 314 * @dev: drm device 315 * @id: id of the mode object 316 * @type: type of the mode object 317 * 318 * Note that framebuffers cannot be looked up with this functions - since those 319 * are reference counted, they need special treatment. 320 */ 321 struct drm_mode_object *drm_mode_object_find(struct drm_device *dev, 322 uint32_t id, uint32_t type) 323 { 324 struct drm_mode_object *obj = NULL; 325 326 /* Framebuffers are reference counted and need their own lookup 327 * function.*/ 328 WARN_ON(type == DRM_MODE_OBJECT_FB); 329 330 mutex_lock(&dev->mode_config.idr_mutex); 331 obj = idr_find(&dev->mode_config.crtc_idr, id); 332 if (!obj || (obj->type != type) || (obj->id != id)) 333 obj = NULL; 334 mutex_unlock(&dev->mode_config.idr_mutex); 335 336 return obj; 337 } 338 EXPORT_SYMBOL(drm_mode_object_find); 339 340 /** 341 * drm_framebuffer_init - initialize a framebuffer 342 * @dev: DRM device 343 * @fb: framebuffer to be initialized 344 * @funcs: ... with these functions 345 * 346 * Allocates an ID for the framebuffer's parent mode object, sets its mode 347 * functions & device file and adds it to the master fd list. 348 * 349 * IMPORTANT: 350 * This functions publishes the fb and makes it available for concurrent access 351 * by other users. Which means by this point the fb _must_ be fully set up - 352 * since all the fb attributes are invariant over its lifetime, no further 353 * locking but only correct reference counting is required. 354 * 355 * RETURNS: 356 * Zero on success, error code on failure. 357 */ 358 int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb, 359 const struct drm_framebuffer_funcs *funcs) 360 { 361 int ret; 362 363 mutex_lock(&dev->mode_config.fb_lock); 364 kref_init(&fb->refcount); 365 INIT_LIST_HEAD(&fb->filp_head); 366 fb->dev = dev; 367 fb->funcs = funcs; 368 369 ret = drm_mode_object_get(dev, &fb->base, DRM_MODE_OBJECT_FB); 370 if (ret) 371 goto out; 372 373 /* Grab the idr reference. */ 374 drm_framebuffer_reference(fb); 375 376 dev->mode_config.num_fb++; 377 list_add(&fb->head, &dev->mode_config.fb_list); 378 out: 379 mutex_unlock(&dev->mode_config.fb_lock); 380 381 return 0; 382 } 383 EXPORT_SYMBOL(drm_framebuffer_init); 384 385 static void drm_framebuffer_free(struct kref *kref) 386 { 387 struct drm_framebuffer *fb = 388 container_of(kref, struct drm_framebuffer, refcount); 389 fb->funcs->destroy(fb); 390 } 391 392 static struct drm_framebuffer *__drm_framebuffer_lookup(struct drm_device *dev, 393 uint32_t id) 394 { 395 struct drm_mode_object *obj = NULL; 396 struct drm_framebuffer *fb; 397 398 mutex_lock(&dev->mode_config.idr_mutex); 399 obj = idr_find(&dev->mode_config.crtc_idr, id); 400 if (!obj || (obj->type != DRM_MODE_OBJECT_FB) || (obj->id != id)) 401 fb = NULL; 402 else 403 fb = obj_to_fb(obj); 404 mutex_unlock(&dev->mode_config.idr_mutex); 405 406 return fb; 407 } 408 409 /** 410 * drm_framebuffer_lookup - look up a drm framebuffer and grab a reference 411 * @dev: drm device 412 * @id: id of the fb object 413 * 414 * If successful, this grabs an additional reference to the framebuffer - 415 * callers need to make sure to eventually unreference the returned framebuffer 416 * again. 417 */ 418 struct drm_framebuffer *drm_framebuffer_lookup(struct drm_device *dev, 419 uint32_t id) 420 { 421 struct drm_framebuffer *fb; 422 423 mutex_lock(&dev->mode_config.fb_lock); 424 fb = __drm_framebuffer_lookup(dev, id); 425 if (fb) 426 kref_get(&fb->refcount); 427 mutex_unlock(&dev->mode_config.fb_lock); 428 429 return fb; 430 } 431 EXPORT_SYMBOL(drm_framebuffer_lookup); 432 433 /** 434 * drm_framebuffer_unreference - unref a framebuffer 435 * @fb: framebuffer to unref 436 * 437 * This functions decrements the fb's refcount and frees it if it drops to zero. 438 */ 439 void drm_framebuffer_unreference(struct drm_framebuffer *fb) 440 { 441 DRM_DEBUG("FB ID: %d\n", fb->base.id); 442 kref_put(&fb->refcount, drm_framebuffer_free); 443 } 444 EXPORT_SYMBOL(drm_framebuffer_unreference); 445 446 /** 447 * drm_framebuffer_reference - incr the fb refcnt 448 * @fb: framebuffer 449 */ 450 void drm_framebuffer_reference(struct drm_framebuffer *fb) 451 { 452 DRM_DEBUG("FB ID: %d\n", fb->base.id); 453 kref_get(&fb->refcount); 454 } 455 EXPORT_SYMBOL(drm_framebuffer_reference); 456 457 static void drm_framebuffer_free_bug(struct kref *kref) 458 { 459 BUG(); 460 } 461 462 static void __drm_framebuffer_unreference(struct drm_framebuffer *fb) 463 { 464 DRM_DEBUG("FB ID: %d\n", fb->base.id); 465 kref_put(&fb->refcount, drm_framebuffer_free_bug); 466 } 467 468 /* dev->mode_config.fb_lock must be held! */ 469 static void __drm_framebuffer_unregister(struct drm_device *dev, 470 struct drm_framebuffer *fb) 471 { 472 mutex_lock(&dev->mode_config.idr_mutex); 473 idr_remove(&dev->mode_config.crtc_idr, fb->base.id); 474 mutex_unlock(&dev->mode_config.idr_mutex); 475 476 fb->base.id = 0; 477 478 __drm_framebuffer_unreference(fb); 479 } 480 481 /** 482 * drm_framebuffer_unregister_private - unregister a private fb from the lookup idr 483 * @fb: fb to unregister 484 * 485 * Drivers need to call this when cleaning up driver-private framebuffers, e.g. 486 * those used for fbdev. Note that the caller must hold a reference of it's own, 487 * i.e. the object may not be destroyed through this call (since it'll lead to a 488 * locking inversion). 489 */ 490 void drm_framebuffer_unregister_private(struct drm_framebuffer *fb) 491 { 492 struct drm_device *dev = fb->dev; 493 494 mutex_lock(&dev->mode_config.fb_lock); 495 /* Mark fb as reaped and drop idr ref. */ 496 __drm_framebuffer_unregister(dev, fb); 497 mutex_unlock(&dev->mode_config.fb_lock); 498 } 499 EXPORT_SYMBOL(drm_framebuffer_unregister_private); 500 501 /** 502 * drm_framebuffer_cleanup - remove a framebuffer object 503 * @fb: framebuffer to remove 504 * 505 * Cleanup references to a user-created framebuffer. This function is intended 506 * to be used from the drivers ->destroy callback. 507 * 508 * Note that this function does not remove the fb from active usuage - if it is 509 * still used anywhere, hilarity can ensue since userspace could call getfb on 510 * the id and get back -EINVAL. Obviously no concern at driver unload time. 511 * 512 * Also, the framebuffer will not be removed from the lookup idr - for 513 * user-created framebuffers this will happen in in the rmfb ioctl. For 514 * driver-private objects (e.g. for fbdev) drivers need to explicitly call 515 * drm_framebuffer_unregister_private. 516 */ 517 void drm_framebuffer_cleanup(struct drm_framebuffer *fb) 518 { 519 struct drm_device *dev = fb->dev; 520 521 mutex_lock(&dev->mode_config.fb_lock); 522 list_del(&fb->head); 523 dev->mode_config.num_fb--; 524 mutex_unlock(&dev->mode_config.fb_lock); 525 } 526 EXPORT_SYMBOL(drm_framebuffer_cleanup); 527 528 /** 529 * drm_framebuffer_remove - remove and unreference a framebuffer object 530 * @fb: framebuffer to remove 531 * 532 * Scans all the CRTCs and planes in @dev's mode_config. If they're 533 * using @fb, removes it, setting it to NULL. Then drops the reference to the 534 * passed-in framebuffer. Might take the modeset locks. 535 * 536 * Note that this function optimizes the cleanup away if the caller holds the 537 * last reference to the framebuffer. It is also guaranteed to not take the 538 * modeset locks in this case. 539 */ 540 void drm_framebuffer_remove(struct drm_framebuffer *fb) 541 { 542 struct drm_device *dev = fb->dev; 543 struct drm_crtc *crtc; 544 struct drm_plane *plane; 545 struct drm_mode_set set; 546 int ret; 547 548 WARN_ON(!list_empty(&fb->filp_head)); 549 550 /* 551 * drm ABI mandates that we remove any deleted framebuffers from active 552 * useage. But since most sane clients only remove framebuffers they no 553 * longer need, try to optimize this away. 554 * 555 * Since we're holding a reference ourselves, observing a refcount of 1 556 * means that we're the last holder and can skip it. Also, the refcount 557 * can never increase from 1 again, so we don't need any barriers or 558 * locks. 559 * 560 * Note that userspace could try to race with use and instate a new 561 * usage _after_ we've cleared all current ones. End result will be an 562 * in-use fb with fb-id == 0. Userspace is allowed to shoot its own foot 563 * in this manner. 564 */ 565 if (atomic_read(&fb->refcount.refcount) > 1) { 566 drm_modeset_lock_all(dev); 567 /* remove from any CRTC */ 568 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) { 569 if (crtc->fb == fb) { 570 /* should turn off the crtc */ 571 memset(&set, 0, sizeof(struct drm_mode_set)); 572 set.crtc = crtc; 573 set.fb = NULL; 574 ret = drm_mode_set_config_internal(&set); 575 if (ret) 576 DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc); 577 } 578 } 579 580 list_for_each_entry(plane, &dev->mode_config.plane_list, head) { 581 if (plane->fb == fb) { 582 /* should turn off the crtc */ 583 ret = plane->funcs->disable_plane(plane); 584 if (ret) 585 DRM_ERROR("failed to disable plane with busy fb\n"); 586 /* disconnect the plane from the fb and crtc: */ 587 __drm_framebuffer_unreference(plane->fb); 588 plane->fb = NULL; 589 plane->crtc = NULL; 590 } 591 } 592 drm_modeset_unlock_all(dev); 593 } 594 595 drm_framebuffer_unreference(fb); 596 } 597 EXPORT_SYMBOL(drm_framebuffer_remove); 598 599 /** 600 * drm_crtc_init - Initialise a new CRTC object 601 * @dev: DRM device 602 * @crtc: CRTC object to init 603 * @funcs: callbacks for the new CRTC 604 * 605 * Inits a new object created as base part of an driver crtc object. 606 * 607 * RETURNS: 608 * Zero on success, error code on failure. 609 */ 610 int drm_crtc_init(struct drm_device *dev, struct drm_crtc *crtc, 611 const struct drm_crtc_funcs *funcs) 612 { 613 int ret; 614 615 crtc->dev = dev; 616 crtc->funcs = funcs; 617 crtc->invert_dimensions = false; 618 619 drm_modeset_lock_all(dev); 620 mutex_init(&crtc->mutex); 621 mutex_lock_nest_lock(&crtc->mutex, &dev->mode_config.mutex); 622 623 ret = drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC); 624 if (ret) 625 goto out; 626 627 crtc->base.properties = &crtc->properties; 628 629 list_add_tail(&crtc->head, &dev->mode_config.crtc_list); 630 dev->mode_config.num_crtc++; 631 632 out: 633 drm_modeset_unlock_all(dev); 634 635 return ret; 636 } 637 EXPORT_SYMBOL(drm_crtc_init); 638 639 /** 640 * drm_crtc_cleanup - Cleans up the core crtc usage. 641 * @crtc: CRTC to cleanup 642 * 643 * Cleanup @crtc. Removes from drm modesetting space 644 * does NOT free object, caller does that. 645 */ 646 void drm_crtc_cleanup(struct drm_crtc *crtc) 647 { 648 struct drm_device *dev = crtc->dev; 649 650 kfree(crtc->gamma_store); 651 crtc->gamma_store = NULL; 652 653 drm_mode_object_put(dev, &crtc->base); 654 list_del(&crtc->head); 655 dev->mode_config.num_crtc--; 656 } 657 EXPORT_SYMBOL(drm_crtc_cleanup); 658 659 /** 660 * drm_mode_probed_add - add a mode to a connector's probed mode list 661 * @connector: connector the new mode 662 * @mode: mode data 663 * 664 * Add @mode to @connector's mode list for later use. 665 */ 666 void drm_mode_probed_add(struct drm_connector *connector, 667 struct drm_display_mode *mode) 668 { 669 list_add(&mode->head, &connector->probed_modes); 670 } 671 EXPORT_SYMBOL(drm_mode_probed_add); 672 673 /** 674 * drm_mode_remove - remove and free a mode 675 * @connector: connector list to modify 676 * @mode: mode to remove 677 * 678 * Remove @mode from @connector's mode list, then free it. 679 */ 680 void drm_mode_remove(struct drm_connector *connector, 681 struct drm_display_mode *mode) 682 { 683 list_del(&mode->head); 684 drm_mode_destroy(connector->dev, mode); 685 } 686 EXPORT_SYMBOL(drm_mode_remove); 687 688 /** 689 * drm_connector_init - Init a preallocated connector 690 * @dev: DRM device 691 * @connector: the connector to init 692 * @funcs: callbacks for this connector 693 * @connector_type: user visible type of the connector 694 * 695 * Initialises a preallocated connector. Connectors should be 696 * subclassed as part of driver connector objects. 697 * 698 * RETURNS: 699 * Zero on success, error code on failure. 700 */ 701 int drm_connector_init(struct drm_device *dev, 702 struct drm_connector *connector, 703 const struct drm_connector_funcs *funcs, 704 int connector_type) 705 { 706 int ret; 707 708 drm_modeset_lock_all(dev); 709 710 ret = drm_mode_object_get(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR); 711 if (ret) 712 goto out; 713 714 connector->base.properties = &connector->properties; 715 connector->dev = dev; 716 connector->funcs = funcs; 717 connector->connector_type = connector_type; 718 connector->connector_type_id = 719 ++drm_connector_enum_list[connector_type].count; /* TODO */ 720 INIT_LIST_HEAD(&connector->user_modes); 721 INIT_LIST_HEAD(&connector->probed_modes); 722 INIT_LIST_HEAD(&connector->modes); 723 connector->edid_blob_ptr = NULL; 724 connector->status = connector_status_unknown; 725 726 list_add_tail(&connector->head, &dev->mode_config.connector_list); 727 dev->mode_config.num_connector++; 728 729 if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL) 730 drm_object_attach_property(&connector->base, 731 dev->mode_config.edid_property, 732 0); 733 734 drm_object_attach_property(&connector->base, 735 dev->mode_config.dpms_property, 0); 736 737 out: 738 drm_modeset_unlock_all(dev); 739 740 return ret; 741 } 742 EXPORT_SYMBOL(drm_connector_init); 743 744 /** 745 * drm_connector_cleanup - cleans up an initialised connector 746 * @connector: connector to cleanup 747 * 748 * Cleans up the connector but doesn't free the object. 749 */ 750 void drm_connector_cleanup(struct drm_connector *connector) 751 { 752 struct drm_device *dev = connector->dev; 753 struct drm_display_mode *mode, *t; 754 755 list_for_each_entry_safe(mode, t, &connector->probed_modes, head) 756 drm_mode_remove(connector, mode); 757 758 list_for_each_entry_safe(mode, t, &connector->modes, head) 759 drm_mode_remove(connector, mode); 760 761 list_for_each_entry_safe(mode, t, &connector->user_modes, head) 762 drm_mode_remove(connector, mode); 763 764 drm_mode_object_put(dev, &connector->base); 765 list_del(&connector->head); 766 dev->mode_config.num_connector--; 767 } 768 EXPORT_SYMBOL(drm_connector_cleanup); 769 770 void drm_connector_unplug_all(struct drm_device *dev) 771 { 772 struct drm_connector *connector; 773 774 /* taking the mode config mutex ends up in a clash with sysfs */ 775 list_for_each_entry(connector, &dev->mode_config.connector_list, head) 776 drm_sysfs_connector_remove(connector); 777 778 } 779 EXPORT_SYMBOL(drm_connector_unplug_all); 780 781 int drm_encoder_init(struct drm_device *dev, 782 struct drm_encoder *encoder, 783 const struct drm_encoder_funcs *funcs, 784 int encoder_type) 785 { 786 int ret; 787 788 drm_modeset_lock_all(dev); 789 790 ret = drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER); 791 if (ret) 792 goto out; 793 794 encoder->dev = dev; 795 encoder->encoder_type = encoder_type; 796 encoder->funcs = funcs; 797 798 list_add_tail(&encoder->head, &dev->mode_config.encoder_list); 799 dev->mode_config.num_encoder++; 800 801 out: 802 drm_modeset_unlock_all(dev); 803 804 return ret; 805 } 806 EXPORT_SYMBOL(drm_encoder_init); 807 808 void drm_encoder_cleanup(struct drm_encoder *encoder) 809 { 810 struct drm_device *dev = encoder->dev; 811 drm_modeset_lock_all(dev); 812 drm_mode_object_put(dev, &encoder->base); 813 list_del(&encoder->head); 814 dev->mode_config.num_encoder--; 815 drm_modeset_unlock_all(dev); 816 } 817 EXPORT_SYMBOL(drm_encoder_cleanup); 818 819 int drm_plane_init(struct drm_device *dev, struct drm_plane *plane, 820 unsigned long possible_crtcs, 821 const struct drm_plane_funcs *funcs, 822 const uint32_t *formats, uint32_t format_count, 823 bool priv) 824 { 825 int ret; 826 827 drm_modeset_lock_all(dev); 828 829 ret = drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE); 830 if (ret) 831 goto out; 832 833 plane->base.properties = &plane->properties; 834 plane->dev = dev; 835 plane->funcs = funcs; 836 plane->format_types = kmalloc(sizeof(uint32_t) * format_count, 837 GFP_KERNEL); 838 if (!plane->format_types) { 839 DRM_DEBUG_KMS("out of memory when allocating plane\n"); 840 drm_mode_object_put(dev, &plane->base); 841 ret = -ENOMEM; 842 goto out; 843 } 844 845 memcpy(plane->format_types, formats, format_count * sizeof(uint32_t)); 846 plane->format_count = format_count; 847 plane->possible_crtcs = possible_crtcs; 848 849 /* private planes are not exposed to userspace, but depending on 850 * display hardware, might be convenient to allow sharing programming 851 * for the scanout engine with the crtc implementation. 852 */ 853 if (!priv) { 854 list_add_tail(&plane->head, &dev->mode_config.plane_list); 855 dev->mode_config.num_plane++; 856 } else { 857 INIT_LIST_HEAD(&plane->head); 858 } 859 860 out: 861 drm_modeset_unlock_all(dev); 862 863 return ret; 864 } 865 EXPORT_SYMBOL(drm_plane_init); 866 867 void drm_plane_cleanup(struct drm_plane *plane) 868 { 869 struct drm_device *dev = plane->dev; 870 871 drm_modeset_lock_all(dev); 872 kfree(plane->format_types); 873 drm_mode_object_put(dev, &plane->base); 874 /* if not added to a list, it must be a private plane */ 875 if (!list_empty(&plane->head)) { 876 list_del(&plane->head); 877 dev->mode_config.num_plane--; 878 } 879 drm_modeset_unlock_all(dev); 880 } 881 EXPORT_SYMBOL(drm_plane_cleanup); 882 883 /** 884 * drm_mode_create - create a new display mode 885 * @dev: DRM device 886 * 887 * Create a new drm_display_mode, give it an ID, and return it. 888 * 889 * RETURNS: 890 * Pointer to new mode on success, NULL on error. 891 */ 892 struct drm_display_mode *drm_mode_create(struct drm_device *dev) 893 { 894 struct drm_display_mode *nmode; 895 896 nmode = kzalloc(sizeof(struct drm_display_mode), GFP_KERNEL); 897 if (!nmode) 898 return NULL; 899 900 if (drm_mode_object_get(dev, &nmode->base, DRM_MODE_OBJECT_MODE)) { 901 kfree(nmode); 902 return NULL; 903 } 904 905 return nmode; 906 } 907 EXPORT_SYMBOL(drm_mode_create); 908 909 /** 910 * drm_mode_destroy - remove a mode 911 * @dev: DRM device 912 * @mode: mode to remove 913 * 914 * Free @mode's unique identifier, then free it. 915 */ 916 void drm_mode_destroy(struct drm_device *dev, struct drm_display_mode *mode) 917 { 918 if (!mode) 919 return; 920 921 drm_mode_object_put(dev, &mode->base); 922 923 kfree(mode); 924 } 925 EXPORT_SYMBOL(drm_mode_destroy); 926 927 static int drm_mode_create_standard_connector_properties(struct drm_device *dev) 928 { 929 struct drm_property *edid; 930 struct drm_property *dpms; 931 932 /* 933 * Standard properties (apply to all connectors) 934 */ 935 edid = drm_property_create(dev, DRM_MODE_PROP_BLOB | 936 DRM_MODE_PROP_IMMUTABLE, 937 "EDID", 0); 938 dev->mode_config.edid_property = edid; 939 940 dpms = drm_property_create_enum(dev, 0, 941 "DPMS", drm_dpms_enum_list, 942 ARRAY_SIZE(drm_dpms_enum_list)); 943 dev->mode_config.dpms_property = dpms; 944 945 return 0; 946 } 947 948 /** 949 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties 950 * @dev: DRM device 951 * 952 * Called by a driver the first time a DVI-I connector is made. 953 */ 954 int drm_mode_create_dvi_i_properties(struct drm_device *dev) 955 { 956 struct drm_property *dvi_i_selector; 957 struct drm_property *dvi_i_subconnector; 958 959 if (dev->mode_config.dvi_i_select_subconnector_property) 960 return 0; 961 962 dvi_i_selector = 963 drm_property_create_enum(dev, 0, 964 "select subconnector", 965 drm_dvi_i_select_enum_list, 966 ARRAY_SIZE(drm_dvi_i_select_enum_list)); 967 dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector; 968 969 dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE, 970 "subconnector", 971 drm_dvi_i_subconnector_enum_list, 972 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list)); 973 dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector; 974 975 return 0; 976 } 977 EXPORT_SYMBOL(drm_mode_create_dvi_i_properties); 978 979 /** 980 * drm_create_tv_properties - create TV specific connector properties 981 * @dev: DRM device 982 * @num_modes: number of different TV formats (modes) supported 983 * @modes: array of pointers to strings containing name of each format 984 * 985 * Called by a driver's TV initialization routine, this function creates 986 * the TV specific connector properties for a given device. Caller is 987 * responsible for allocating a list of format names and passing them to 988 * this routine. 989 */ 990 int drm_mode_create_tv_properties(struct drm_device *dev, int num_modes, 991 char *modes[]) 992 { 993 struct drm_property *tv_selector; 994 struct drm_property *tv_subconnector; 995 int i; 996 997 if (dev->mode_config.tv_select_subconnector_property) 998 return 0; 999 1000 /* 1001 * Basic connector properties 1002 */ 1003 tv_selector = drm_property_create_enum(dev, 0, 1004 "select subconnector", 1005 drm_tv_select_enum_list, 1006 ARRAY_SIZE(drm_tv_select_enum_list)); 1007 dev->mode_config.tv_select_subconnector_property = tv_selector; 1008 1009 tv_subconnector = 1010 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE, 1011 "subconnector", 1012 drm_tv_subconnector_enum_list, 1013 ARRAY_SIZE(drm_tv_subconnector_enum_list)); 1014 dev->mode_config.tv_subconnector_property = tv_subconnector; 1015 1016 /* 1017 * Other, TV specific properties: margins & TV modes. 1018 */ 1019 dev->mode_config.tv_left_margin_property = 1020 drm_property_create_range(dev, 0, "left margin", 0, 100); 1021 1022 dev->mode_config.tv_right_margin_property = 1023 drm_property_create_range(dev, 0, "right margin", 0, 100); 1024 1025 dev->mode_config.tv_top_margin_property = 1026 drm_property_create_range(dev, 0, "top margin", 0, 100); 1027 1028 dev->mode_config.tv_bottom_margin_property = 1029 drm_property_create_range(dev, 0, "bottom margin", 0, 100); 1030 1031 dev->mode_config.tv_mode_property = 1032 drm_property_create(dev, DRM_MODE_PROP_ENUM, 1033 "mode", num_modes); 1034 for (i = 0; i < num_modes; i++) 1035 drm_property_add_enum(dev->mode_config.tv_mode_property, i, 1036 i, modes[i]); 1037 1038 dev->mode_config.tv_brightness_property = 1039 drm_property_create_range(dev, 0, "brightness", 0, 100); 1040 1041 dev->mode_config.tv_contrast_property = 1042 drm_property_create_range(dev, 0, "contrast", 0, 100); 1043 1044 dev->mode_config.tv_flicker_reduction_property = 1045 drm_property_create_range(dev, 0, "flicker reduction", 0, 100); 1046 1047 dev->mode_config.tv_overscan_property = 1048 drm_property_create_range(dev, 0, "overscan", 0, 100); 1049 1050 dev->mode_config.tv_saturation_property = 1051 drm_property_create_range(dev, 0, "saturation", 0, 100); 1052 1053 dev->mode_config.tv_hue_property = 1054 drm_property_create_range(dev, 0, "hue", 0, 100); 1055 1056 return 0; 1057 } 1058 EXPORT_SYMBOL(drm_mode_create_tv_properties); 1059 1060 /** 1061 * drm_mode_create_scaling_mode_property - create scaling mode property 1062 * @dev: DRM device 1063 * 1064 * Called by a driver the first time it's needed, must be attached to desired 1065 * connectors. 1066 */ 1067 int drm_mode_create_scaling_mode_property(struct drm_device *dev) 1068 { 1069 struct drm_property *scaling_mode; 1070 1071 if (dev->mode_config.scaling_mode_property) 1072 return 0; 1073 1074 scaling_mode = 1075 drm_property_create_enum(dev, 0, "scaling mode", 1076 drm_scaling_mode_enum_list, 1077 ARRAY_SIZE(drm_scaling_mode_enum_list)); 1078 1079 dev->mode_config.scaling_mode_property = scaling_mode; 1080 1081 return 0; 1082 } 1083 EXPORT_SYMBOL(drm_mode_create_scaling_mode_property); 1084 1085 /** 1086 * drm_mode_create_dithering_property - create dithering property 1087 * @dev: DRM device 1088 * 1089 * Called by a driver the first time it's needed, must be attached to desired 1090 * connectors. 1091 */ 1092 int drm_mode_create_dithering_property(struct drm_device *dev) 1093 { 1094 struct drm_property *dithering_mode; 1095 1096 if (dev->mode_config.dithering_mode_property) 1097 return 0; 1098 1099 dithering_mode = 1100 drm_property_create_enum(dev, 0, "dithering", 1101 drm_dithering_mode_enum_list, 1102 ARRAY_SIZE(drm_dithering_mode_enum_list)); 1103 dev->mode_config.dithering_mode_property = dithering_mode; 1104 1105 return 0; 1106 } 1107 EXPORT_SYMBOL(drm_mode_create_dithering_property); 1108 1109 /** 1110 * drm_mode_create_dirty_property - create dirty property 1111 * @dev: DRM device 1112 * 1113 * Called by a driver the first time it's needed, must be attached to desired 1114 * connectors. 1115 */ 1116 int drm_mode_create_dirty_info_property(struct drm_device *dev) 1117 { 1118 struct drm_property *dirty_info; 1119 1120 if (dev->mode_config.dirty_info_property) 1121 return 0; 1122 1123 dirty_info = 1124 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE, 1125 "dirty", 1126 drm_dirty_info_enum_list, 1127 ARRAY_SIZE(drm_dirty_info_enum_list)); 1128 dev->mode_config.dirty_info_property = dirty_info; 1129 1130 return 0; 1131 } 1132 EXPORT_SYMBOL(drm_mode_create_dirty_info_property); 1133 1134 /** 1135 * drm_mode_config_init - initialize DRM mode_configuration structure 1136 * @dev: DRM device 1137 * 1138 * Initialize @dev's mode_config structure, used for tracking the graphics 1139 * configuration of @dev. 1140 * 1141 * Since this initializes the modeset locks, no locking is possible. Which is no 1142 * problem, since this should happen single threaded at init time. It is the 1143 * driver's problem to ensure this guarantee. 1144 * 1145 */ 1146 void drm_mode_config_init(struct drm_device *dev) 1147 { 1148 mutex_init(&dev->mode_config.mutex); 1149 mutex_init(&dev->mode_config.idr_mutex); 1150 mutex_init(&dev->mode_config.fb_lock); 1151 INIT_LIST_HEAD(&dev->mode_config.fb_list); 1152 INIT_LIST_HEAD(&dev->mode_config.crtc_list); 1153 INIT_LIST_HEAD(&dev->mode_config.connector_list); 1154 INIT_LIST_HEAD(&dev->mode_config.encoder_list); 1155 INIT_LIST_HEAD(&dev->mode_config.property_list); 1156 INIT_LIST_HEAD(&dev->mode_config.property_blob_list); 1157 INIT_LIST_HEAD(&dev->mode_config.plane_list); 1158 idr_init(&dev->mode_config.crtc_idr); 1159 1160 drm_modeset_lock_all(dev); 1161 drm_mode_create_standard_connector_properties(dev); 1162 drm_modeset_unlock_all(dev); 1163 1164 /* Just to be sure */ 1165 dev->mode_config.num_fb = 0; 1166 dev->mode_config.num_connector = 0; 1167 dev->mode_config.num_crtc = 0; 1168 dev->mode_config.num_encoder = 0; 1169 } 1170 EXPORT_SYMBOL(drm_mode_config_init); 1171 1172 int drm_mode_group_init(struct drm_device *dev, struct drm_mode_group *group) 1173 { 1174 uint32_t total_objects = 0; 1175 1176 total_objects += dev->mode_config.num_crtc; 1177 total_objects += dev->mode_config.num_connector; 1178 total_objects += dev->mode_config.num_encoder; 1179 1180 group->id_list = kzalloc(total_objects * sizeof(uint32_t), GFP_KERNEL); 1181 if (!group->id_list) 1182 return -ENOMEM; 1183 1184 group->num_crtcs = 0; 1185 group->num_connectors = 0; 1186 group->num_encoders = 0; 1187 return 0; 1188 } 1189 1190 int drm_mode_group_init_legacy_group(struct drm_device *dev, 1191 struct drm_mode_group *group) 1192 { 1193 struct drm_crtc *crtc; 1194 struct drm_encoder *encoder; 1195 struct drm_connector *connector; 1196 int ret; 1197 1198 if ((ret = drm_mode_group_init(dev, group))) 1199 return ret; 1200 1201 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) 1202 group->id_list[group->num_crtcs++] = crtc->base.id; 1203 1204 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) 1205 group->id_list[group->num_crtcs + group->num_encoders++] = 1206 encoder->base.id; 1207 1208 list_for_each_entry(connector, &dev->mode_config.connector_list, head) 1209 group->id_list[group->num_crtcs + group->num_encoders + 1210 group->num_connectors++] = connector->base.id; 1211 1212 return 0; 1213 } 1214 EXPORT_SYMBOL(drm_mode_group_init_legacy_group); 1215 1216 /** 1217 * drm_mode_config_cleanup - free up DRM mode_config info 1218 * @dev: DRM device 1219 * 1220 * Free up all the connectors and CRTCs associated with this DRM device, then 1221 * free up the framebuffers and associated buffer objects. 1222 * 1223 * Note that since this /should/ happen single-threaded at driver/device 1224 * teardown time, no locking is required. It's the driver's job to ensure that 1225 * this guarantee actually holds true. 1226 * 1227 * FIXME: cleanup any dangling user buffer objects too 1228 */ 1229 void drm_mode_config_cleanup(struct drm_device *dev) 1230 { 1231 struct drm_connector *connector, *ot; 1232 struct drm_crtc *crtc, *ct; 1233 struct drm_encoder *encoder, *enct; 1234 struct drm_framebuffer *fb, *fbt; 1235 struct drm_property *property, *pt; 1236 struct drm_plane *plane, *plt; 1237 1238 list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list, 1239 head) { 1240 encoder->funcs->destroy(encoder); 1241 } 1242 1243 list_for_each_entry_safe(connector, ot, 1244 &dev->mode_config.connector_list, head) { 1245 connector->funcs->destroy(connector); 1246 } 1247 1248 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list, 1249 head) { 1250 drm_property_destroy(dev, property); 1251 } 1252 1253 /* 1254 * Single-threaded teardown context, so it's not required to grab the 1255 * fb_lock to protect against concurrent fb_list access. Contrary, it 1256 * would actually deadlock with the drm_framebuffer_cleanup function. 1257 * 1258 * Also, if there are any framebuffers left, that's a driver leak now, 1259 * so politely WARN about this. 1260 */ 1261 WARN_ON(!list_empty(&dev->mode_config.fb_list)); 1262 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) { 1263 drm_framebuffer_remove(fb); 1264 } 1265 1266 list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list, 1267 head) { 1268 plane->funcs->destroy(plane); 1269 } 1270 1271 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) { 1272 crtc->funcs->destroy(crtc); 1273 } 1274 1275 idr_destroy(&dev->mode_config.crtc_idr); 1276 } 1277 EXPORT_SYMBOL(drm_mode_config_cleanup); 1278 1279 /** 1280 * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo 1281 * @out: drm_mode_modeinfo struct to return to the user 1282 * @in: drm_display_mode to use 1283 * 1284 * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to 1285 * the user. 1286 */ 1287 static void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out, 1288 const struct drm_display_mode *in) 1289 { 1290 WARN(in->hdisplay > USHRT_MAX || in->hsync_start > USHRT_MAX || 1291 in->hsync_end > USHRT_MAX || in->htotal > USHRT_MAX || 1292 in->hskew > USHRT_MAX || in->vdisplay > USHRT_MAX || 1293 in->vsync_start > USHRT_MAX || in->vsync_end > USHRT_MAX || 1294 in->vtotal > USHRT_MAX || in->vscan > USHRT_MAX, 1295 "timing values too large for mode info\n"); 1296 1297 out->clock = in->clock; 1298 out->hdisplay = in->hdisplay; 1299 out->hsync_start = in->hsync_start; 1300 out->hsync_end = in->hsync_end; 1301 out->htotal = in->htotal; 1302 out->hskew = in->hskew; 1303 out->vdisplay = in->vdisplay; 1304 out->vsync_start = in->vsync_start; 1305 out->vsync_end = in->vsync_end; 1306 out->vtotal = in->vtotal; 1307 out->vscan = in->vscan; 1308 out->vrefresh = in->vrefresh; 1309 out->flags = in->flags; 1310 out->type = in->type; 1311 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN); 1312 out->name[DRM_DISPLAY_MODE_LEN-1] = 0; 1313 } 1314 1315 /** 1316 * drm_crtc_convert_to_umode - convert a modeinfo into a drm_display_mode 1317 * @out: drm_display_mode to return to the user 1318 * @in: drm_mode_modeinfo to use 1319 * 1320 * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to 1321 * the caller. 1322 * 1323 * RETURNS: 1324 * Zero on success, errno on failure. 1325 */ 1326 static int drm_crtc_convert_umode(struct drm_display_mode *out, 1327 const struct drm_mode_modeinfo *in) 1328 { 1329 if (in->clock > INT_MAX || in->vrefresh > INT_MAX) 1330 return -ERANGE; 1331 1332 out->clock = in->clock; 1333 out->hdisplay = in->hdisplay; 1334 out->hsync_start = in->hsync_start; 1335 out->hsync_end = in->hsync_end; 1336 out->htotal = in->htotal; 1337 out->hskew = in->hskew; 1338 out->vdisplay = in->vdisplay; 1339 out->vsync_start = in->vsync_start; 1340 out->vsync_end = in->vsync_end; 1341 out->vtotal = in->vtotal; 1342 out->vscan = in->vscan; 1343 out->vrefresh = in->vrefresh; 1344 out->flags = in->flags; 1345 out->type = in->type; 1346 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN); 1347 out->name[DRM_DISPLAY_MODE_LEN-1] = 0; 1348 1349 return 0; 1350 } 1351 1352 /** 1353 * drm_mode_getresources - get graphics configuration 1354 * @dev: drm device for the ioctl 1355 * @data: data pointer for the ioctl 1356 * @file_priv: drm file for the ioctl call 1357 * 1358 * Construct a set of configuration description structures and return 1359 * them to the user, including CRTC, connector and framebuffer configuration. 1360 * 1361 * Called by the user via ioctl. 1362 * 1363 * RETURNS: 1364 * Zero on success, errno on failure. 1365 */ 1366 int drm_mode_getresources(struct drm_device *dev, void *data, 1367 struct drm_file *file_priv) 1368 { 1369 struct drm_mode_card_res *card_res = data; 1370 struct list_head *lh; 1371 struct drm_framebuffer *fb; 1372 struct drm_connector *connector; 1373 struct drm_crtc *crtc; 1374 struct drm_encoder *encoder; 1375 int ret = 0; 1376 int connector_count = 0; 1377 int crtc_count = 0; 1378 int fb_count = 0; 1379 int encoder_count = 0; 1380 int copied = 0, i; 1381 uint32_t __user *fb_id; 1382 uint32_t __user *crtc_id; 1383 uint32_t __user *connector_id; 1384 uint32_t __user *encoder_id; 1385 struct drm_mode_group *mode_group; 1386 1387 if (!drm_core_check_feature(dev, DRIVER_MODESET)) 1388 return -EINVAL; 1389 1390 1391 mutex_lock(&file_priv->fbs_lock); 1392 /* 1393 * For the non-control nodes we need to limit the list of resources 1394 * by IDs in the group list for this node 1395 */ 1396 list_for_each(lh, &file_priv->fbs) 1397 fb_count++; 1398 1399 /* handle this in 4 parts */ 1400 /* FBs */ 1401 if (card_res->count_fbs >= fb_count) { 1402 copied = 0; 1403 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr; 1404 list_for_each_entry(fb, &file_priv->fbs, filp_head) { 1405 if (put_user(fb->base.id, fb_id + copied)) { 1406 mutex_unlock(&file_priv->fbs_lock); 1407 return -EFAULT; 1408 } 1409 copied++; 1410 } 1411 } 1412 card_res->count_fbs = fb_count; 1413 mutex_unlock(&file_priv->fbs_lock); 1414 1415 drm_modeset_lock_all(dev); 1416 mode_group = &file_priv->master->minor->mode_group; 1417 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) { 1418 1419 list_for_each(lh, &dev->mode_config.crtc_list) 1420 crtc_count++; 1421 1422 list_for_each(lh, &dev->mode_config.connector_list) 1423 connector_count++; 1424 1425 list_for_each(lh, &dev->mode_config.encoder_list) 1426 encoder_count++; 1427 } else { 1428 1429 crtc_count = mode_group->num_crtcs; 1430 connector_count = mode_group->num_connectors; 1431 encoder_count = mode_group->num_encoders; 1432 } 1433 1434 card_res->max_height = dev->mode_config.max_height; 1435 card_res->min_height = dev->mode_config.min_height; 1436 card_res->max_width = dev->mode_config.max_width; 1437 card_res->min_width = dev->mode_config.min_width; 1438 1439 /* CRTCs */ 1440 if (card_res->count_crtcs >= crtc_count) { 1441 copied = 0; 1442 crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr; 1443 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) { 1444 list_for_each_entry(crtc, &dev->mode_config.crtc_list, 1445 head) { 1446 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id); 1447 if (put_user(crtc->base.id, crtc_id + copied)) { 1448 ret = -EFAULT; 1449 goto out; 1450 } 1451 copied++; 1452 } 1453 } else { 1454 for (i = 0; i < mode_group->num_crtcs; i++) { 1455 if (put_user(mode_group->id_list[i], 1456 crtc_id + copied)) { 1457 ret = -EFAULT; 1458 goto out; 1459 } 1460 copied++; 1461 } 1462 } 1463 } 1464 card_res->count_crtcs = crtc_count; 1465 1466 /* Encoders */ 1467 if (card_res->count_encoders >= encoder_count) { 1468 copied = 0; 1469 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr; 1470 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) { 1471 list_for_each_entry(encoder, 1472 &dev->mode_config.encoder_list, 1473 head) { 1474 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder->base.id, 1475 drm_get_encoder_name(encoder)); 1476 if (put_user(encoder->base.id, encoder_id + 1477 copied)) { 1478 ret = -EFAULT; 1479 goto out; 1480 } 1481 copied++; 1482 } 1483 } else { 1484 for (i = mode_group->num_crtcs; i < mode_group->num_crtcs + mode_group->num_encoders; i++) { 1485 if (put_user(mode_group->id_list[i], 1486 encoder_id + copied)) { 1487 ret = -EFAULT; 1488 goto out; 1489 } 1490 copied++; 1491 } 1492 1493 } 1494 } 1495 card_res->count_encoders = encoder_count; 1496 1497 /* Connectors */ 1498 if (card_res->count_connectors >= connector_count) { 1499 copied = 0; 1500 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr; 1501 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) { 1502 list_for_each_entry(connector, 1503 &dev->mode_config.connector_list, 1504 head) { 1505 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n", 1506 connector->base.id, 1507 drm_get_connector_name(connector)); 1508 if (put_user(connector->base.id, 1509 connector_id + copied)) { 1510 ret = -EFAULT; 1511 goto out; 1512 } 1513 copied++; 1514 } 1515 } else { 1516 int start = mode_group->num_crtcs + 1517 mode_group->num_encoders; 1518 for (i = start; i < start + mode_group->num_connectors; i++) { 1519 if (put_user(mode_group->id_list[i], 1520 connector_id + copied)) { 1521 ret = -EFAULT; 1522 goto out; 1523 } 1524 copied++; 1525 } 1526 } 1527 } 1528 card_res->count_connectors = connector_count; 1529 1530 DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res->count_crtcs, 1531 card_res->count_connectors, card_res->count_encoders); 1532 1533 out: 1534 drm_modeset_unlock_all(dev); 1535 return ret; 1536 } 1537 1538 /** 1539 * drm_mode_getcrtc - get CRTC configuration 1540 * @dev: drm device for the ioctl 1541 * @data: data pointer for the ioctl 1542 * @file_priv: drm file for the ioctl call 1543 * 1544 * Construct a CRTC configuration structure to return to the user. 1545 * 1546 * Called by the user via ioctl. 1547 * 1548 * RETURNS: 1549 * Zero on success, errno on failure. 1550 */ 1551 int drm_mode_getcrtc(struct drm_device *dev, 1552 void *data, struct drm_file *file_priv) 1553 { 1554 struct drm_mode_crtc *crtc_resp = data; 1555 struct drm_crtc *crtc; 1556 struct drm_mode_object *obj; 1557 int ret = 0; 1558 1559 if (!drm_core_check_feature(dev, DRIVER_MODESET)) 1560 return -EINVAL; 1561 1562 drm_modeset_lock_all(dev); 1563 1564 obj = drm_mode_object_find(dev, crtc_resp->crtc_id, 1565 DRM_MODE_OBJECT_CRTC); 1566 if (!obj) { 1567 ret = -EINVAL; 1568 goto out; 1569 } 1570 crtc = obj_to_crtc(obj); 1571 1572 crtc_resp->x = crtc->x; 1573 crtc_resp->y = crtc->y; 1574 crtc_resp->gamma_size = crtc->gamma_size; 1575 if (crtc->fb) 1576 crtc_resp->fb_id = crtc->fb->base.id; 1577 else 1578 crtc_resp->fb_id = 0; 1579 1580 if (crtc->enabled) { 1581 1582 drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode); 1583 crtc_resp->mode_valid = 1; 1584 1585 } else { 1586 crtc_resp->mode_valid = 0; 1587 } 1588 1589 out: 1590 drm_modeset_unlock_all(dev); 1591 return ret; 1592 } 1593 1594 /** 1595 * drm_mode_getconnector - get connector configuration 1596 * @dev: drm device for the ioctl 1597 * @data: data pointer for the ioctl 1598 * @file_priv: drm file for the ioctl call 1599 * 1600 * Construct a connector configuration structure to return to the user. 1601 * 1602 * Called by the user via ioctl. 1603 * 1604 * RETURNS: 1605 * Zero on success, errno on failure. 1606 */ 1607 int drm_mode_getconnector(struct drm_device *dev, void *data, 1608 struct drm_file *file_priv) 1609 { 1610 struct drm_mode_get_connector *out_resp = data; 1611 struct drm_mode_object *obj; 1612 struct drm_connector *connector; 1613 struct drm_display_mode *mode; 1614 int mode_count = 0; 1615 int props_count = 0; 1616 int encoders_count = 0; 1617 int ret = 0; 1618 int copied = 0; 1619 int i; 1620 struct drm_mode_modeinfo u_mode; 1621 struct drm_mode_modeinfo __user *mode_ptr; 1622 uint32_t __user *prop_ptr; 1623 uint64_t __user *prop_values; 1624 uint32_t __user *encoder_ptr; 1625 1626 if (!drm_core_check_feature(dev, DRIVER_MODESET)) 1627 return -EINVAL; 1628 1629 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo)); 1630 1631 DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp->connector_id); 1632 1633 mutex_lock(&dev->mode_config.mutex); 1634 1635 obj = drm_mode_object_find(dev, out_resp->connector_id, 1636 DRM_MODE_OBJECT_CONNECTOR); 1637 if (!obj) { 1638 ret = -EINVAL; 1639 goto out; 1640 } 1641 connector = obj_to_connector(obj); 1642 1643 props_count = connector->properties.count; 1644 1645 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) { 1646 if (connector->encoder_ids[i] != 0) { 1647 encoders_count++; 1648 } 1649 } 1650 1651 if (out_resp->count_modes == 0) { 1652 connector->funcs->fill_modes(connector, 1653 dev->mode_config.max_width, 1654 dev->mode_config.max_height); 1655 } 1656 1657 /* delayed so we get modes regardless of pre-fill_modes state */ 1658 list_for_each_entry(mode, &connector->modes, head) 1659 mode_count++; 1660 1661 out_resp->connector_id = connector->base.id; 1662 out_resp->connector_type = connector->connector_type; 1663 out_resp->connector_type_id = connector->connector_type_id; 1664 out_resp->mm_width = connector->display_info.width_mm; 1665 out_resp->mm_height = connector->display_info.height_mm; 1666 out_resp->subpixel = connector->display_info.subpixel_order; 1667 out_resp->connection = connector->status; 1668 if (connector->encoder) 1669 out_resp->encoder_id = connector->encoder->base.id; 1670 else 1671 out_resp->encoder_id = 0; 1672 1673 /* 1674 * This ioctl is called twice, once to determine how much space is 1675 * needed, and the 2nd time to fill it. 1676 */ 1677 if ((out_resp->count_modes >= mode_count) && mode_count) { 1678 copied = 0; 1679 mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr; 1680 list_for_each_entry(mode, &connector->modes, head) { 1681 drm_crtc_convert_to_umode(&u_mode, mode); 1682 if (copy_to_user(mode_ptr + copied, 1683 &u_mode, sizeof(u_mode))) { 1684 ret = -EFAULT; 1685 goto out; 1686 } 1687 copied++; 1688 } 1689 } 1690 out_resp->count_modes = mode_count; 1691 1692 if ((out_resp->count_props >= props_count) && props_count) { 1693 copied = 0; 1694 prop_ptr = (uint32_t __user *)(unsigned long)(out_resp->props_ptr); 1695 prop_values = (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr); 1696 for (i = 0; i < connector->properties.count; i++) { 1697 if (put_user(connector->properties.ids[i], 1698 prop_ptr + copied)) { 1699 ret = -EFAULT; 1700 goto out; 1701 } 1702 1703 if (put_user(connector->properties.values[i], 1704 prop_values + copied)) { 1705 ret = -EFAULT; 1706 goto out; 1707 } 1708 copied++; 1709 } 1710 } 1711 out_resp->count_props = props_count; 1712 1713 if ((out_resp->count_encoders >= encoders_count) && encoders_count) { 1714 copied = 0; 1715 encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr); 1716 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) { 1717 if (connector->encoder_ids[i] != 0) { 1718 if (put_user(connector->encoder_ids[i], 1719 encoder_ptr + copied)) { 1720 ret = -EFAULT; 1721 goto out; 1722 } 1723 copied++; 1724 } 1725 } 1726 } 1727 out_resp->count_encoders = encoders_count; 1728 1729 out: 1730 mutex_unlock(&dev->mode_config.mutex); 1731 1732 return ret; 1733 } 1734 1735 int drm_mode_getencoder(struct drm_device *dev, void *data, 1736 struct drm_file *file_priv) 1737 { 1738 struct drm_mode_get_encoder *enc_resp = data; 1739 struct drm_mode_object *obj; 1740 struct drm_encoder *encoder; 1741 int ret = 0; 1742 1743 if (!drm_core_check_feature(dev, DRIVER_MODESET)) 1744 return -EINVAL; 1745 1746 drm_modeset_lock_all(dev); 1747 obj = drm_mode_object_find(dev, enc_resp->encoder_id, 1748 DRM_MODE_OBJECT_ENCODER); 1749 if (!obj) { 1750 ret = -EINVAL; 1751 goto out; 1752 } 1753 encoder = obj_to_encoder(obj); 1754 1755 if (encoder->crtc) 1756 enc_resp->crtc_id = encoder->crtc->base.id; 1757 else 1758 enc_resp->crtc_id = 0; 1759 enc_resp->encoder_type = encoder->encoder_type; 1760 enc_resp->encoder_id = encoder->base.id; 1761 enc_resp->possible_crtcs = encoder->possible_crtcs; 1762 enc_resp->possible_clones = encoder->possible_clones; 1763 1764 out: 1765 drm_modeset_unlock_all(dev); 1766 return ret; 1767 } 1768 1769 /** 1770 * drm_mode_getplane_res - get plane info 1771 * @dev: DRM device 1772 * @data: ioctl data 1773 * @file_priv: DRM file info 1774 * 1775 * Return an plane count and set of IDs. 1776 */ 1777 int drm_mode_getplane_res(struct drm_device *dev, void *data, 1778 struct drm_file *file_priv) 1779 { 1780 struct drm_mode_get_plane_res *plane_resp = data; 1781 struct drm_mode_config *config; 1782 struct drm_plane *plane; 1783 uint32_t __user *plane_ptr; 1784 int copied = 0, ret = 0; 1785 1786 if (!drm_core_check_feature(dev, DRIVER_MODESET)) 1787 return -EINVAL; 1788 1789 drm_modeset_lock_all(dev); 1790 config = &dev->mode_config; 1791 1792 /* 1793 * This ioctl is called twice, once to determine how much space is 1794 * needed, and the 2nd time to fill it. 1795 */ 1796 if (config->num_plane && 1797 (plane_resp->count_planes >= config->num_plane)) { 1798 plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr; 1799 1800 list_for_each_entry(plane, &config->plane_list, head) { 1801 if (put_user(plane->base.id, plane_ptr + copied)) { 1802 ret = -EFAULT; 1803 goto out; 1804 } 1805 copied++; 1806 } 1807 } 1808 plane_resp->count_planes = config->num_plane; 1809 1810 out: 1811 drm_modeset_unlock_all(dev); 1812 return ret; 1813 } 1814 1815 /** 1816 * drm_mode_getplane - get plane info 1817 * @dev: DRM device 1818 * @data: ioctl data 1819 * @file_priv: DRM file info 1820 * 1821 * Return plane info, including formats supported, gamma size, any 1822 * current fb, etc. 1823 */ 1824 int drm_mode_getplane(struct drm_device *dev, void *data, 1825 struct drm_file *file_priv) 1826 { 1827 struct drm_mode_get_plane *plane_resp = data; 1828 struct drm_mode_object *obj; 1829 struct drm_plane *plane; 1830 uint32_t __user *format_ptr; 1831 int ret = 0; 1832 1833 if (!drm_core_check_feature(dev, DRIVER_MODESET)) 1834 return -EINVAL; 1835 1836 drm_modeset_lock_all(dev); 1837 obj = drm_mode_object_find(dev, plane_resp->plane_id, 1838 DRM_MODE_OBJECT_PLANE); 1839 if (!obj) { 1840 ret = -ENOENT; 1841 goto out; 1842 } 1843 plane = obj_to_plane(obj); 1844 1845 if (plane->crtc) 1846 plane_resp->crtc_id = plane->crtc->base.id; 1847 else 1848 plane_resp->crtc_id = 0; 1849 1850 if (plane->fb) 1851 plane_resp->fb_id = plane->fb->base.id; 1852 else 1853 plane_resp->fb_id = 0; 1854 1855 plane_resp->plane_id = plane->base.id; 1856 plane_resp->possible_crtcs = plane->possible_crtcs; 1857 plane_resp->gamma_size = plane->gamma_size; 1858 1859 /* 1860 * This ioctl is called twice, once to determine how much space is 1861 * needed, and the 2nd time to fill it. 1862 */ 1863 if (plane->format_count && 1864 (plane_resp->count_format_types >= plane->format_count)) { 1865 format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr; 1866 if (copy_to_user(format_ptr, 1867 plane->format_types, 1868 sizeof(uint32_t) * plane->format_count)) { 1869 ret = -EFAULT; 1870 goto out; 1871 } 1872 } 1873 plane_resp->count_format_types = plane->format_count; 1874 1875 out: 1876 drm_modeset_unlock_all(dev); 1877 return ret; 1878 } 1879 1880 /** 1881 * drm_mode_setplane - set up or tear down an plane 1882 * @dev: DRM device 1883 * @data: ioctl data* 1884 * @file_priv: DRM file info 1885 * 1886 * Set plane info, including placement, fb, scaling, and other factors. 1887 * Or pass a NULL fb to disable. 1888 */ 1889 int drm_mode_setplane(struct drm_device *dev, void *data, 1890 struct drm_file *file_priv) 1891 { 1892 struct drm_mode_set_plane *plane_req = data; 1893 struct drm_mode_object *obj; 1894 struct drm_plane *plane; 1895 struct drm_crtc *crtc; 1896 struct drm_framebuffer *fb = NULL, *old_fb = NULL; 1897 int ret = 0; 1898 unsigned int fb_width, fb_height; 1899 int i; 1900 1901 if (!drm_core_check_feature(dev, DRIVER_MODESET)) 1902 return -EINVAL; 1903 1904 /* 1905 * First, find the plane, crtc, and fb objects. If not available, 1906 * we don't bother to call the driver. 1907 */ 1908 obj = drm_mode_object_find(dev, plane_req->plane_id, 1909 DRM_MODE_OBJECT_PLANE); 1910 if (!obj) { 1911 DRM_DEBUG_KMS("Unknown plane ID %d\n", 1912 plane_req->plane_id); 1913 return -ENOENT; 1914 } 1915 plane = obj_to_plane(obj); 1916 1917 /* No fb means shut it down */ 1918 if (!plane_req->fb_id) { 1919 drm_modeset_lock_all(dev); 1920 old_fb = plane->fb; 1921 plane->funcs->disable_plane(plane); 1922 plane->crtc = NULL; 1923 plane->fb = NULL; 1924 drm_modeset_unlock_all(dev); 1925 goto out; 1926 } 1927 1928 obj = drm_mode_object_find(dev, plane_req->crtc_id, 1929 DRM_MODE_OBJECT_CRTC); 1930 if (!obj) { 1931 DRM_DEBUG_KMS("Unknown crtc ID %d\n", 1932 plane_req->crtc_id); 1933 ret = -ENOENT; 1934 goto out; 1935 } 1936 crtc = obj_to_crtc(obj); 1937 1938 fb = drm_framebuffer_lookup(dev, plane_req->fb_id); 1939 if (!fb) { 1940 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n", 1941 plane_req->fb_id); 1942 ret = -ENOENT; 1943 goto out; 1944 } 1945 1946 /* Check whether this plane supports the fb pixel format. */ 1947 for (i = 0; i < plane->format_count; i++) 1948 if (fb->pixel_format == plane->format_types[i]) 1949 break; 1950 if (i == plane->format_count) { 1951 DRM_DEBUG_KMS("Invalid pixel format 0x%08x\n", fb->pixel_format); 1952 ret = -EINVAL; 1953 goto out; 1954 } 1955 1956 fb_width = fb->width << 16; 1957 fb_height = fb->height << 16; 1958 1959 /* Make sure source coordinates are inside the fb. */ 1960 if (plane_req->src_w > fb_width || 1961 plane_req->src_x > fb_width - plane_req->src_w || 1962 plane_req->src_h > fb_height || 1963 plane_req->src_y > fb_height - plane_req->src_h) { 1964 DRM_DEBUG_KMS("Invalid source coordinates " 1965 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n", 1966 plane_req->src_w >> 16, 1967 ((plane_req->src_w & 0xffff) * 15625) >> 10, 1968 plane_req->src_h >> 16, 1969 ((plane_req->src_h & 0xffff) * 15625) >> 10, 1970 plane_req->src_x >> 16, 1971 ((plane_req->src_x & 0xffff) * 15625) >> 10, 1972 plane_req->src_y >> 16, 1973 ((plane_req->src_y & 0xffff) * 15625) >> 10); 1974 ret = -ENOSPC; 1975 goto out; 1976 } 1977 1978 /* Give drivers some help against integer overflows */ 1979 if (plane_req->crtc_w > INT_MAX || 1980 plane_req->crtc_x > INT_MAX - (int32_t) plane_req->crtc_w || 1981 plane_req->crtc_h > INT_MAX || 1982 plane_req->crtc_y > INT_MAX - (int32_t) plane_req->crtc_h) { 1983 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n", 1984 plane_req->crtc_w, plane_req->crtc_h, 1985 plane_req->crtc_x, plane_req->crtc_y); 1986 ret = -ERANGE; 1987 goto out; 1988 } 1989 1990 drm_modeset_lock_all(dev); 1991 ret = plane->funcs->update_plane(plane, crtc, fb, 1992 plane_req->crtc_x, plane_req->crtc_y, 1993 plane_req->crtc_w, plane_req->crtc_h, 1994 plane_req->src_x, plane_req->src_y, 1995 plane_req->src_w, plane_req->src_h); 1996 if (!ret) { 1997 old_fb = plane->fb; 1998 plane->crtc = crtc; 1999 plane->fb = fb; 2000 fb = NULL; 2001 } 2002 drm_modeset_unlock_all(dev); 2003 2004 out: 2005 if (fb) 2006 drm_framebuffer_unreference(fb); 2007 if (old_fb) 2008 drm_framebuffer_unreference(old_fb); 2009 2010 return ret; 2011 } 2012 2013 /** 2014 * drm_mode_set_config_internal - helper to call ->set_config 2015 * @set: modeset config to set 2016 * 2017 * This is a little helper to wrap internal calls to the ->set_config driver 2018 * interface. The only thing it adds is correct refcounting dance. 2019 */ 2020 int drm_mode_set_config_internal(struct drm_mode_set *set) 2021 { 2022 struct drm_crtc *crtc = set->crtc; 2023 struct drm_framebuffer *fb, *old_fb; 2024 int ret; 2025 2026 old_fb = crtc->fb; 2027 fb = set->fb; 2028 2029 ret = crtc->funcs->set_config(set); 2030 if (ret == 0) { 2031 if (old_fb) 2032 drm_framebuffer_unreference(old_fb); 2033 if (fb) 2034 drm_framebuffer_reference(fb); 2035 } 2036 2037 return ret; 2038 } 2039 EXPORT_SYMBOL(drm_mode_set_config_internal); 2040 2041 /** 2042 * drm_mode_setcrtc - set CRTC configuration 2043 * @dev: drm device for the ioctl 2044 * @data: data pointer for the ioctl 2045 * @file_priv: drm file for the ioctl call 2046 * 2047 * Build a new CRTC configuration based on user request. 2048 * 2049 * Called by the user via ioctl. 2050 * 2051 * RETURNS: 2052 * Zero on success, errno on failure. 2053 */ 2054 int drm_mode_setcrtc(struct drm_device *dev, void *data, 2055 struct drm_file *file_priv) 2056 { 2057 struct drm_mode_config *config = &dev->mode_config; 2058 struct drm_mode_crtc *crtc_req = data; 2059 struct drm_mode_object *obj; 2060 struct drm_crtc *crtc; 2061 struct drm_connector **connector_set = NULL, *connector; 2062 struct drm_framebuffer *fb = NULL; 2063 struct drm_display_mode *mode = NULL; 2064 struct drm_mode_set set; 2065 uint32_t __user *set_connectors_ptr; 2066 int ret; 2067 int i; 2068 2069 if (!drm_core_check_feature(dev, DRIVER_MODESET)) 2070 return -EINVAL; 2071 2072 /* For some reason crtc x/y offsets are signed internally. */ 2073 if (crtc_req->x > INT_MAX || crtc_req->y > INT_MAX) 2074 return -ERANGE; 2075 2076 drm_modeset_lock_all(dev); 2077 obj = drm_mode_object_find(dev, crtc_req->crtc_id, 2078 DRM_MODE_OBJECT_CRTC); 2079 if (!obj) { 2080 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id); 2081 ret = -EINVAL; 2082 goto out; 2083 } 2084 crtc = obj_to_crtc(obj); 2085 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id); 2086 2087 if (crtc_req->mode_valid) { 2088 int hdisplay, vdisplay; 2089 /* If we have a mode we need a framebuffer. */ 2090 /* If we pass -1, set the mode with the currently bound fb */ 2091 if (crtc_req->fb_id == -1) { 2092 if (!crtc->fb) { 2093 DRM_DEBUG_KMS("CRTC doesn't have current FB\n"); 2094 ret = -EINVAL; 2095 goto out; 2096 } 2097 fb = crtc->fb; 2098 /* Make refcounting symmetric with the lookup path. */ 2099 drm_framebuffer_reference(fb); 2100 } else { 2101 fb = drm_framebuffer_lookup(dev, crtc_req->fb_id); 2102 if (!fb) { 2103 DRM_DEBUG_KMS("Unknown FB ID%d\n", 2104 crtc_req->fb_id); 2105 ret = -EINVAL; 2106 goto out; 2107 } 2108 } 2109 2110 mode = drm_mode_create(dev); 2111 if (!mode) { 2112 ret = -ENOMEM; 2113 goto out; 2114 } 2115 2116 ret = drm_crtc_convert_umode(mode, &crtc_req->mode); 2117 if (ret) { 2118 DRM_DEBUG_KMS("Invalid mode\n"); 2119 goto out; 2120 } 2121 2122 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V); 2123 2124 hdisplay = mode->hdisplay; 2125 vdisplay = mode->vdisplay; 2126 2127 if (crtc->invert_dimensions) 2128 swap(hdisplay, vdisplay); 2129 2130 if (hdisplay > fb->width || 2131 vdisplay > fb->height || 2132 crtc_req->x > fb->width - hdisplay || 2133 crtc_req->y > fb->height - vdisplay) { 2134 DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n", 2135 fb->width, fb->height, 2136 hdisplay, vdisplay, crtc_req->x, crtc_req->y, 2137 crtc->invert_dimensions ? " (inverted)" : ""); 2138 ret = -ENOSPC; 2139 goto out; 2140 } 2141 } 2142 2143 if (crtc_req->count_connectors == 0 && mode) { 2144 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n"); 2145 ret = -EINVAL; 2146 goto out; 2147 } 2148 2149 if (crtc_req->count_connectors > 0 && (!mode || !fb)) { 2150 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n", 2151 crtc_req->count_connectors); 2152 ret = -EINVAL; 2153 goto out; 2154 } 2155 2156 if (crtc_req->count_connectors > 0) { 2157 u32 out_id; 2158 2159 /* Avoid unbounded kernel memory allocation */ 2160 if (crtc_req->count_connectors > config->num_connector) { 2161 ret = -EINVAL; 2162 goto out; 2163 } 2164 2165 connector_set = kmalloc(crtc_req->count_connectors * 2166 sizeof(struct drm_connector *), 2167 GFP_KERNEL); 2168 if (!connector_set) { 2169 ret = -ENOMEM; 2170 goto out; 2171 } 2172 2173 for (i = 0; i < crtc_req->count_connectors; i++) { 2174 set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr; 2175 if (get_user(out_id, &set_connectors_ptr[i])) { 2176 ret = -EFAULT; 2177 goto out; 2178 } 2179 2180 obj = drm_mode_object_find(dev, out_id, 2181 DRM_MODE_OBJECT_CONNECTOR); 2182 if (!obj) { 2183 DRM_DEBUG_KMS("Connector id %d unknown\n", 2184 out_id); 2185 ret = -EINVAL; 2186 goto out; 2187 } 2188 connector = obj_to_connector(obj); 2189 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n", 2190 connector->base.id, 2191 drm_get_connector_name(connector)); 2192 2193 connector_set[i] = connector; 2194 } 2195 } 2196 2197 set.crtc = crtc; 2198 set.x = crtc_req->x; 2199 set.y = crtc_req->y; 2200 set.mode = mode; 2201 set.connectors = connector_set; 2202 set.num_connectors = crtc_req->count_connectors; 2203 set.fb = fb; 2204 ret = drm_mode_set_config_internal(&set); 2205 2206 out: 2207 if (fb) 2208 drm_framebuffer_unreference(fb); 2209 2210 kfree(connector_set); 2211 drm_mode_destroy(dev, mode); 2212 drm_modeset_unlock_all(dev); 2213 return ret; 2214 } 2215 2216 int drm_mode_cursor_ioctl(struct drm_device *dev, 2217 void *data, struct drm_file *file_priv) 2218 { 2219 struct drm_mode_cursor *req = data; 2220 struct drm_mode_object *obj; 2221 struct drm_crtc *crtc; 2222 int ret = 0; 2223 2224 if (!drm_core_check_feature(dev, DRIVER_MODESET)) 2225 return -EINVAL; 2226 2227 if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags)) 2228 return -EINVAL; 2229 2230 obj = drm_mode_object_find(dev, req->crtc_id, DRM_MODE_OBJECT_CRTC); 2231 if (!obj) { 2232 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id); 2233 return -EINVAL; 2234 } 2235 crtc = obj_to_crtc(obj); 2236 2237 mutex_lock(&crtc->mutex); 2238 if (req->flags & DRM_MODE_CURSOR_BO) { 2239 if (!crtc->funcs->cursor_set) { 2240 ret = -ENXIO; 2241 goto out; 2242 } 2243 /* Turns off the cursor if handle is 0 */ 2244 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle, 2245 req->width, req->height); 2246 } 2247 2248 if (req->flags & DRM_MODE_CURSOR_MOVE) { 2249 if (crtc->funcs->cursor_move) { 2250 ret = crtc->funcs->cursor_move(crtc, req->x, req->y); 2251 } else { 2252 ret = -EFAULT; 2253 goto out; 2254 } 2255 } 2256 out: 2257 mutex_unlock(&crtc->mutex); 2258 2259 return ret; 2260 } 2261 2262 /* Original addfb only supported RGB formats, so figure out which one */ 2263 uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth) 2264 { 2265 uint32_t fmt; 2266 2267 switch (bpp) { 2268 case 8: 2269 fmt = DRM_FORMAT_C8; 2270 break; 2271 case 16: 2272 if (depth == 15) 2273 fmt = DRM_FORMAT_XRGB1555; 2274 else 2275 fmt = DRM_FORMAT_RGB565; 2276 break; 2277 case 24: 2278 fmt = DRM_FORMAT_RGB888; 2279 break; 2280 case 32: 2281 if (depth == 24) 2282 fmt = DRM_FORMAT_XRGB8888; 2283 else if (depth == 30) 2284 fmt = DRM_FORMAT_XRGB2101010; 2285 else 2286 fmt = DRM_FORMAT_ARGB8888; 2287 break; 2288 default: 2289 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n"); 2290 fmt = DRM_FORMAT_XRGB8888; 2291 break; 2292 } 2293 2294 return fmt; 2295 } 2296 EXPORT_SYMBOL(drm_mode_legacy_fb_format); 2297 2298 /** 2299 * drm_mode_addfb - add an FB to the graphics configuration 2300 * @dev: drm device for the ioctl 2301 * @data: data pointer for the ioctl 2302 * @file_priv: drm file for the ioctl call 2303 * 2304 * Add a new FB to the specified CRTC, given a user request. 2305 * 2306 * Called by the user via ioctl. 2307 * 2308 * RETURNS: 2309 * Zero on success, errno on failure. 2310 */ 2311 int drm_mode_addfb(struct drm_device *dev, 2312 void *data, struct drm_file *file_priv) 2313 { 2314 struct drm_mode_fb_cmd *or = data; 2315 struct drm_mode_fb_cmd2 r = {}; 2316 struct drm_mode_config *config = &dev->mode_config; 2317 struct drm_framebuffer *fb; 2318 int ret = 0; 2319 2320 /* Use new struct with format internally */ 2321 r.fb_id = or->fb_id; 2322 r.width = or->width; 2323 r.height = or->height; 2324 r.pitches[0] = or->pitch; 2325 r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth); 2326 r.handles[0] = or->handle; 2327 2328 if (!drm_core_check_feature(dev, DRIVER_MODESET)) 2329 return -EINVAL; 2330 2331 if ((config->min_width > r.width) || (r.width > config->max_width)) 2332 return -EINVAL; 2333 2334 if ((config->min_height > r.height) || (r.height > config->max_height)) 2335 return -EINVAL; 2336 2337 fb = dev->mode_config.funcs->fb_create(dev, file_priv, &r); 2338 if (IS_ERR(fb)) { 2339 DRM_DEBUG_KMS("could not create framebuffer\n"); 2340 drm_modeset_unlock_all(dev); 2341 return PTR_ERR(fb); 2342 } 2343 2344 mutex_lock(&file_priv->fbs_lock); 2345 or->fb_id = fb->base.id; 2346 list_add(&fb->filp_head, &file_priv->fbs); 2347 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id); 2348 mutex_unlock(&file_priv->fbs_lock); 2349 2350 return ret; 2351 } 2352 2353 static int format_check(const struct drm_mode_fb_cmd2 *r) 2354 { 2355 uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN; 2356 2357 switch (format) { 2358 case DRM_FORMAT_C8: 2359 case DRM_FORMAT_RGB332: 2360 case DRM_FORMAT_BGR233: 2361 case DRM_FORMAT_XRGB4444: 2362 case DRM_FORMAT_XBGR4444: 2363 case DRM_FORMAT_RGBX4444: 2364 case DRM_FORMAT_BGRX4444: 2365 case DRM_FORMAT_ARGB4444: 2366 case DRM_FORMAT_ABGR4444: 2367 case DRM_FORMAT_RGBA4444: 2368 case DRM_FORMAT_BGRA4444: 2369 case DRM_FORMAT_XRGB1555: 2370 case DRM_FORMAT_XBGR1555: 2371 case DRM_FORMAT_RGBX5551: 2372 case DRM_FORMAT_BGRX5551: 2373 case DRM_FORMAT_ARGB1555: 2374 case DRM_FORMAT_ABGR1555: 2375 case DRM_FORMAT_RGBA5551: 2376 case DRM_FORMAT_BGRA5551: 2377 case DRM_FORMAT_RGB565: 2378 case DRM_FORMAT_BGR565: 2379 case DRM_FORMAT_RGB888: 2380 case DRM_FORMAT_BGR888: 2381 case DRM_FORMAT_XRGB8888: 2382 case DRM_FORMAT_XBGR8888: 2383 case DRM_FORMAT_RGBX8888: 2384 case DRM_FORMAT_BGRX8888: 2385 case DRM_FORMAT_ARGB8888: 2386 case DRM_FORMAT_ABGR8888: 2387 case DRM_FORMAT_RGBA8888: 2388 case DRM_FORMAT_BGRA8888: 2389 case DRM_FORMAT_XRGB2101010: 2390 case DRM_FORMAT_XBGR2101010: 2391 case DRM_FORMAT_RGBX1010102: 2392 case DRM_FORMAT_BGRX1010102: 2393 case DRM_FORMAT_ARGB2101010: 2394 case DRM_FORMAT_ABGR2101010: 2395 case DRM_FORMAT_RGBA1010102: 2396 case DRM_FORMAT_BGRA1010102: 2397 case DRM_FORMAT_YUYV: 2398 case DRM_FORMAT_YVYU: 2399 case DRM_FORMAT_UYVY: 2400 case DRM_FORMAT_VYUY: 2401 case DRM_FORMAT_AYUV: 2402 case DRM_FORMAT_NV12: 2403 case DRM_FORMAT_NV21: 2404 case DRM_FORMAT_NV16: 2405 case DRM_FORMAT_NV61: 2406 case DRM_FORMAT_NV24: 2407 case DRM_FORMAT_NV42: 2408 case DRM_FORMAT_YUV410: 2409 case DRM_FORMAT_YVU410: 2410 case DRM_FORMAT_YUV411: 2411 case DRM_FORMAT_YVU411: 2412 case DRM_FORMAT_YUV420: 2413 case DRM_FORMAT_YVU420: 2414 case DRM_FORMAT_YUV422: 2415 case DRM_FORMAT_YVU422: 2416 case DRM_FORMAT_YUV444: 2417 case DRM_FORMAT_YVU444: 2418 return 0; 2419 default: 2420 return -EINVAL; 2421 } 2422 } 2423 2424 static int framebuffer_check(const struct drm_mode_fb_cmd2 *r) 2425 { 2426 int ret, hsub, vsub, num_planes, i; 2427 2428 ret = format_check(r); 2429 if (ret) { 2430 DRM_DEBUG_KMS("bad framebuffer format 0x%08x\n", r->pixel_format); 2431 return ret; 2432 } 2433 2434 hsub = drm_format_horz_chroma_subsampling(r->pixel_format); 2435 vsub = drm_format_vert_chroma_subsampling(r->pixel_format); 2436 num_planes = drm_format_num_planes(r->pixel_format); 2437 2438 if (r->width == 0 || r->width % hsub) { 2439 DRM_DEBUG_KMS("bad framebuffer width %u\n", r->height); 2440 return -EINVAL; 2441 } 2442 2443 if (r->height == 0 || r->height % vsub) { 2444 DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height); 2445 return -EINVAL; 2446 } 2447 2448 for (i = 0; i < num_planes; i++) { 2449 unsigned int width = r->width / (i != 0 ? hsub : 1); 2450 unsigned int height = r->height / (i != 0 ? vsub : 1); 2451 unsigned int cpp = drm_format_plane_cpp(r->pixel_format, i); 2452 2453 if (!r->handles[i]) { 2454 DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i); 2455 return -EINVAL; 2456 } 2457 2458 if ((uint64_t) width * cpp > UINT_MAX) 2459 return -ERANGE; 2460 2461 if ((uint64_t) height * r->pitches[i] + r->offsets[i] > UINT_MAX) 2462 return -ERANGE; 2463 2464 if (r->pitches[i] < width * cpp) { 2465 DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i); 2466 return -EINVAL; 2467 } 2468 } 2469 2470 return 0; 2471 } 2472 2473 /** 2474 * drm_mode_addfb2 - add an FB to the graphics configuration 2475 * @dev: drm device for the ioctl 2476 * @data: data pointer for the ioctl 2477 * @file_priv: drm file for the ioctl call 2478 * 2479 * Add a new FB to the specified CRTC, given a user request with format. 2480 * 2481 * Called by the user via ioctl. 2482 * 2483 * RETURNS: 2484 * Zero on success, errno on failure. 2485 */ 2486 int drm_mode_addfb2(struct drm_device *dev, 2487 void *data, struct drm_file *file_priv) 2488 { 2489 struct drm_mode_fb_cmd2 *r = data; 2490 struct drm_mode_config *config = &dev->mode_config; 2491 struct drm_framebuffer *fb; 2492 int ret; 2493 2494 if (!drm_core_check_feature(dev, DRIVER_MODESET)) 2495 return -EINVAL; 2496 2497 if (r->flags & ~DRM_MODE_FB_INTERLACED) { 2498 DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r->flags); 2499 return -EINVAL; 2500 } 2501 2502 if ((config->min_width > r->width) || (r->width > config->max_width)) { 2503 DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n", 2504 r->width, config->min_width, config->max_width); 2505 return -EINVAL; 2506 } 2507 if ((config->min_height > r->height) || (r->height > config->max_height)) { 2508 DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n", 2509 r->height, config->min_height, config->max_height); 2510 return -EINVAL; 2511 } 2512 2513 ret = framebuffer_check(r); 2514 if (ret) 2515 return ret; 2516 2517 fb = dev->mode_config.funcs->fb_create(dev, file_priv, r); 2518 if (IS_ERR(fb)) { 2519 DRM_DEBUG_KMS("could not create framebuffer\n"); 2520 drm_modeset_unlock_all(dev); 2521 return PTR_ERR(fb); 2522 } 2523 2524 mutex_lock(&file_priv->fbs_lock); 2525 r->fb_id = fb->base.id; 2526 list_add(&fb->filp_head, &file_priv->fbs); 2527 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id); 2528 mutex_unlock(&file_priv->fbs_lock); 2529 2530 2531 return ret; 2532 } 2533 2534 /** 2535 * drm_mode_rmfb - remove an FB from the configuration 2536 * @dev: drm device for the ioctl 2537 * @data: data pointer for the ioctl 2538 * @file_priv: drm file for the ioctl call 2539 * 2540 * Remove the FB specified by the user. 2541 * 2542 * Called by the user via ioctl. 2543 * 2544 * RETURNS: 2545 * Zero on success, errno on failure. 2546 */ 2547 int drm_mode_rmfb(struct drm_device *dev, 2548 void *data, struct drm_file *file_priv) 2549 { 2550 struct drm_framebuffer *fb = NULL; 2551 struct drm_framebuffer *fbl = NULL; 2552 uint32_t *id = data; 2553 int found = 0; 2554 2555 if (!drm_core_check_feature(dev, DRIVER_MODESET)) 2556 return -EINVAL; 2557 2558 mutex_lock(&file_priv->fbs_lock); 2559 mutex_lock(&dev->mode_config.fb_lock); 2560 fb = __drm_framebuffer_lookup(dev, *id); 2561 if (!fb) 2562 goto fail_lookup; 2563 2564 list_for_each_entry(fbl, &file_priv->fbs, filp_head) 2565 if (fb == fbl) 2566 found = 1; 2567 if (!found) 2568 goto fail_lookup; 2569 2570 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */ 2571 __drm_framebuffer_unregister(dev, fb); 2572 2573 list_del_init(&fb->filp_head); 2574 mutex_unlock(&dev->mode_config.fb_lock); 2575 mutex_unlock(&file_priv->fbs_lock); 2576 2577 drm_framebuffer_remove(fb); 2578 2579 return 0; 2580 2581 fail_lookup: 2582 mutex_unlock(&dev->mode_config.fb_lock); 2583 mutex_unlock(&file_priv->fbs_lock); 2584 2585 return -EINVAL; 2586 } 2587 2588 /** 2589 * drm_mode_getfb - get FB info 2590 * @dev: drm device for the ioctl 2591 * @data: data pointer for the ioctl 2592 * @file_priv: drm file for the ioctl call 2593 * 2594 * Lookup the FB given its ID and return info about it. 2595 * 2596 * Called by the user via ioctl. 2597 * 2598 * RETURNS: 2599 * Zero on success, errno on failure. 2600 */ 2601 int drm_mode_getfb(struct drm_device *dev, 2602 void *data, struct drm_file *file_priv) 2603 { 2604 struct drm_mode_fb_cmd *r = data; 2605 struct drm_framebuffer *fb; 2606 int ret; 2607 2608 if (!drm_core_check_feature(dev, DRIVER_MODESET)) 2609 return -EINVAL; 2610 2611 fb = drm_framebuffer_lookup(dev, r->fb_id); 2612 if (!fb) 2613 return -EINVAL; 2614 2615 r->height = fb->height; 2616 r->width = fb->width; 2617 r->depth = fb->depth; 2618 r->bpp = fb->bits_per_pixel; 2619 r->pitch = fb->pitches[0]; 2620 if (fb->funcs->create_handle) 2621 ret = fb->funcs->create_handle(fb, file_priv, &r->handle); 2622 else 2623 ret = -ENODEV; 2624 2625 drm_framebuffer_unreference(fb); 2626 2627 return ret; 2628 } 2629 2630 int drm_mode_dirtyfb_ioctl(struct drm_device *dev, 2631 void *data, struct drm_file *file_priv) 2632 { 2633 struct drm_clip_rect __user *clips_ptr; 2634 struct drm_clip_rect *clips = NULL; 2635 struct drm_mode_fb_dirty_cmd *r = data; 2636 struct drm_framebuffer *fb; 2637 unsigned flags; 2638 int num_clips; 2639 int ret; 2640 2641 if (!drm_core_check_feature(dev, DRIVER_MODESET)) 2642 return -EINVAL; 2643 2644 fb = drm_framebuffer_lookup(dev, r->fb_id); 2645 if (!fb) 2646 return -EINVAL; 2647 2648 num_clips = r->num_clips; 2649 clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr; 2650 2651 if (!num_clips != !clips_ptr) { 2652 ret = -EINVAL; 2653 goto out_err1; 2654 } 2655 2656 flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags; 2657 2658 /* If userspace annotates copy, clips must come in pairs */ 2659 if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) { 2660 ret = -EINVAL; 2661 goto out_err1; 2662 } 2663 2664 if (num_clips && clips_ptr) { 2665 if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) { 2666 ret = -EINVAL; 2667 goto out_err1; 2668 } 2669 clips = kzalloc(num_clips * sizeof(*clips), GFP_KERNEL); 2670 if (!clips) { 2671 ret = -ENOMEM; 2672 goto out_err1; 2673 } 2674 2675 ret = copy_from_user(clips, clips_ptr, 2676 num_clips * sizeof(*clips)); 2677 if (ret) { 2678 ret = -EFAULT; 2679 goto out_err2; 2680 } 2681 } 2682 2683 if (fb->funcs->dirty) { 2684 drm_modeset_lock_all(dev); 2685 ret = fb->funcs->dirty(fb, file_priv, flags, r->color, 2686 clips, num_clips); 2687 drm_modeset_unlock_all(dev); 2688 } else { 2689 ret = -ENOSYS; 2690 } 2691 2692 out_err2: 2693 kfree(clips); 2694 out_err1: 2695 drm_framebuffer_unreference(fb); 2696 2697 return ret; 2698 } 2699 2700 2701 /** 2702 * drm_fb_release - remove and free the FBs on this file 2703 * @priv: drm file for the ioctl 2704 * 2705 * Destroy all the FBs associated with @filp. 2706 * 2707 * Called by the user via ioctl. 2708 * 2709 * RETURNS: 2710 * Zero on success, errno on failure. 2711 */ 2712 void drm_fb_release(struct drm_file *priv) 2713 { 2714 struct drm_device *dev = priv->minor->dev; 2715 struct drm_framebuffer *fb, *tfb; 2716 2717 mutex_lock(&priv->fbs_lock); 2718 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) { 2719 2720 mutex_lock(&dev->mode_config.fb_lock); 2721 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */ 2722 __drm_framebuffer_unregister(dev, fb); 2723 mutex_unlock(&dev->mode_config.fb_lock); 2724 2725 list_del_init(&fb->filp_head); 2726 2727 /* This will also drop the fpriv->fbs reference. */ 2728 drm_framebuffer_remove(fb); 2729 } 2730 mutex_unlock(&priv->fbs_lock); 2731 } 2732 2733 /** 2734 * drm_mode_attachmode - add a mode to the user mode list 2735 * @dev: DRM device 2736 * @connector: connector to add the mode to 2737 * @mode: mode to add 2738 * 2739 * Add @mode to @connector's user mode list. 2740 */ 2741 static void drm_mode_attachmode(struct drm_device *dev, 2742 struct drm_connector *connector, 2743 struct drm_display_mode *mode) 2744 { 2745 list_add_tail(&mode->head, &connector->user_modes); 2746 } 2747 2748 int drm_mode_attachmode_crtc(struct drm_device *dev, struct drm_crtc *crtc, 2749 const struct drm_display_mode *mode) 2750 { 2751 struct drm_connector *connector; 2752 int ret = 0; 2753 struct drm_display_mode *dup_mode, *next; 2754 LIST_HEAD(list); 2755 2756 list_for_each_entry(connector, &dev->mode_config.connector_list, head) { 2757 if (!connector->encoder) 2758 continue; 2759 if (connector->encoder->crtc == crtc) { 2760 dup_mode = drm_mode_duplicate(dev, mode); 2761 if (!dup_mode) { 2762 ret = -ENOMEM; 2763 goto out; 2764 } 2765 list_add_tail(&dup_mode->head, &list); 2766 } 2767 } 2768 2769 list_for_each_entry(connector, &dev->mode_config.connector_list, head) { 2770 if (!connector->encoder) 2771 continue; 2772 if (connector->encoder->crtc == crtc) 2773 list_move_tail(list.next, &connector->user_modes); 2774 } 2775 2776 WARN_ON(!list_empty(&list)); 2777 2778 out: 2779 list_for_each_entry_safe(dup_mode, next, &list, head) 2780 drm_mode_destroy(dev, dup_mode); 2781 2782 return ret; 2783 } 2784 EXPORT_SYMBOL(drm_mode_attachmode_crtc); 2785 2786 static int drm_mode_detachmode(struct drm_device *dev, 2787 struct drm_connector *connector, 2788 struct drm_display_mode *mode) 2789 { 2790 int found = 0; 2791 int ret = 0; 2792 struct drm_display_mode *match_mode, *t; 2793 2794 list_for_each_entry_safe(match_mode, t, &connector->user_modes, head) { 2795 if (drm_mode_equal(match_mode, mode)) { 2796 list_del(&match_mode->head); 2797 drm_mode_destroy(dev, match_mode); 2798 found = 1; 2799 break; 2800 } 2801 } 2802 2803 if (!found) 2804 ret = -EINVAL; 2805 2806 return ret; 2807 } 2808 2809 int drm_mode_detachmode_crtc(struct drm_device *dev, struct drm_display_mode *mode) 2810 { 2811 struct drm_connector *connector; 2812 2813 list_for_each_entry(connector, &dev->mode_config.connector_list, head) { 2814 drm_mode_detachmode(dev, connector, mode); 2815 } 2816 return 0; 2817 } 2818 EXPORT_SYMBOL(drm_mode_detachmode_crtc); 2819 2820 /** 2821 * drm_fb_attachmode - Attach a user mode to an connector 2822 * @dev: drm device for the ioctl 2823 * @data: data pointer for the ioctl 2824 * @file_priv: drm file for the ioctl call 2825 * 2826 * This attaches a user specified mode to an connector. 2827 * Called by the user via ioctl. 2828 * 2829 * RETURNS: 2830 * Zero on success, errno on failure. 2831 */ 2832 int drm_mode_attachmode_ioctl(struct drm_device *dev, 2833 void *data, struct drm_file *file_priv) 2834 { 2835 struct drm_mode_mode_cmd *mode_cmd = data; 2836 struct drm_connector *connector; 2837 struct drm_display_mode *mode; 2838 struct drm_mode_object *obj; 2839 struct drm_mode_modeinfo *umode = &mode_cmd->mode; 2840 int ret; 2841 2842 if (!drm_core_check_feature(dev, DRIVER_MODESET)) 2843 return -EINVAL; 2844 2845 drm_modeset_lock_all(dev); 2846 2847 obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR); 2848 if (!obj) { 2849 ret = -EINVAL; 2850 goto out; 2851 } 2852 connector = obj_to_connector(obj); 2853 2854 mode = drm_mode_create(dev); 2855 if (!mode) { 2856 ret = -ENOMEM; 2857 goto out; 2858 } 2859 2860 ret = drm_crtc_convert_umode(mode, umode); 2861 if (ret) { 2862 DRM_DEBUG_KMS("Invalid mode\n"); 2863 drm_mode_destroy(dev, mode); 2864 goto out; 2865 } 2866 2867 drm_mode_attachmode(dev, connector, mode); 2868 out: 2869 drm_modeset_unlock_all(dev); 2870 return ret; 2871 } 2872 2873 2874 /** 2875 * drm_fb_detachmode - Detach a user specified mode from an connector 2876 * @dev: drm device for the ioctl 2877 * @data: data pointer for the ioctl 2878 * @file_priv: drm file for the ioctl call 2879 * 2880 * Called by the user via ioctl. 2881 * 2882 * RETURNS: 2883 * Zero on success, errno on failure. 2884 */ 2885 int drm_mode_detachmode_ioctl(struct drm_device *dev, 2886 void *data, struct drm_file *file_priv) 2887 { 2888 struct drm_mode_object *obj; 2889 struct drm_mode_mode_cmd *mode_cmd = data; 2890 struct drm_connector *connector; 2891 struct drm_display_mode mode; 2892 struct drm_mode_modeinfo *umode = &mode_cmd->mode; 2893 int ret; 2894 2895 if (!drm_core_check_feature(dev, DRIVER_MODESET)) 2896 return -EINVAL; 2897 2898 drm_modeset_lock_all(dev); 2899 2900 obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR); 2901 if (!obj) { 2902 ret = -EINVAL; 2903 goto out; 2904 } 2905 connector = obj_to_connector(obj); 2906 2907 ret = drm_crtc_convert_umode(&mode, umode); 2908 if (ret) { 2909 DRM_DEBUG_KMS("Invalid mode\n"); 2910 goto out; 2911 } 2912 2913 ret = drm_mode_detachmode(dev, connector, &mode); 2914 out: 2915 drm_modeset_unlock_all(dev); 2916 return ret; 2917 } 2918 2919 struct drm_property *drm_property_create(struct drm_device *dev, int flags, 2920 const char *name, int num_values) 2921 { 2922 struct drm_property *property = NULL; 2923 int ret; 2924 2925 property = kzalloc(sizeof(struct drm_property), GFP_KERNEL); 2926 if (!property) 2927 return NULL; 2928 2929 if (num_values) { 2930 property->values = kzalloc(sizeof(uint64_t)*num_values, GFP_KERNEL); 2931 if (!property->values) 2932 goto fail; 2933 } 2934 2935 ret = drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY); 2936 if (ret) 2937 goto fail; 2938 2939 property->flags = flags; 2940 property->num_values = num_values; 2941 INIT_LIST_HEAD(&property->enum_blob_list); 2942 2943 if (name) { 2944 strncpy(property->name, name, DRM_PROP_NAME_LEN); 2945 property->name[DRM_PROP_NAME_LEN-1] = '\0'; 2946 } 2947 2948 list_add_tail(&property->head, &dev->mode_config.property_list); 2949 return property; 2950 fail: 2951 kfree(property->values); 2952 kfree(property); 2953 return NULL; 2954 } 2955 EXPORT_SYMBOL(drm_property_create); 2956 2957 struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags, 2958 const char *name, 2959 const struct drm_prop_enum_list *props, 2960 int num_values) 2961 { 2962 struct drm_property *property; 2963 int i, ret; 2964 2965 flags |= DRM_MODE_PROP_ENUM; 2966 2967 property = drm_property_create(dev, flags, name, num_values); 2968 if (!property) 2969 return NULL; 2970 2971 for (i = 0; i < num_values; i++) { 2972 ret = drm_property_add_enum(property, i, 2973 props[i].type, 2974 props[i].name); 2975 if (ret) { 2976 drm_property_destroy(dev, property); 2977 return NULL; 2978 } 2979 } 2980 2981 return property; 2982 } 2983 EXPORT_SYMBOL(drm_property_create_enum); 2984 2985 struct drm_property *drm_property_create_bitmask(struct drm_device *dev, 2986 int flags, const char *name, 2987 const struct drm_prop_enum_list *props, 2988 int num_values) 2989 { 2990 struct drm_property *property; 2991 int i, ret; 2992 2993 flags |= DRM_MODE_PROP_BITMASK; 2994 2995 property = drm_property_create(dev, flags, name, num_values); 2996 if (!property) 2997 return NULL; 2998 2999 for (i = 0; i < num_values; i++) { 3000 ret = drm_property_add_enum(property, i, 3001 props[i].type, 3002 props[i].name); 3003 if (ret) { 3004 drm_property_destroy(dev, property); 3005 return NULL; 3006 } 3007 } 3008 3009 return property; 3010 } 3011 EXPORT_SYMBOL(drm_property_create_bitmask); 3012 3013 struct drm_property *drm_property_create_range(struct drm_device *dev, int flags, 3014 const char *name, 3015 uint64_t min, uint64_t max) 3016 { 3017 struct drm_property *property; 3018 3019 flags |= DRM_MODE_PROP_RANGE; 3020 3021 property = drm_property_create(dev, flags, name, 2); 3022 if (!property) 3023 return NULL; 3024 3025 property->values[0] = min; 3026 property->values[1] = max; 3027 3028 return property; 3029 } 3030 EXPORT_SYMBOL(drm_property_create_range); 3031 3032 int drm_property_add_enum(struct drm_property *property, int index, 3033 uint64_t value, const char *name) 3034 { 3035 struct drm_property_enum *prop_enum; 3036 3037 if (!(property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK))) 3038 return -EINVAL; 3039 3040 /* 3041 * Bitmask enum properties have the additional constraint of values 3042 * from 0 to 63 3043 */ 3044 if ((property->flags & DRM_MODE_PROP_BITMASK) && (value > 63)) 3045 return -EINVAL; 3046 3047 if (!list_empty(&property->enum_blob_list)) { 3048 list_for_each_entry(prop_enum, &property->enum_blob_list, head) { 3049 if (prop_enum->value == value) { 3050 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN); 3051 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0'; 3052 return 0; 3053 } 3054 } 3055 } 3056 3057 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL); 3058 if (!prop_enum) 3059 return -ENOMEM; 3060 3061 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN); 3062 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0'; 3063 prop_enum->value = value; 3064 3065 property->values[index] = value; 3066 list_add_tail(&prop_enum->head, &property->enum_blob_list); 3067 return 0; 3068 } 3069 EXPORT_SYMBOL(drm_property_add_enum); 3070 3071 void drm_property_destroy(struct drm_device *dev, struct drm_property *property) 3072 { 3073 struct drm_property_enum *prop_enum, *pt; 3074 3075 list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) { 3076 list_del(&prop_enum->head); 3077 kfree(prop_enum); 3078 } 3079 3080 if (property->num_values) 3081 kfree(property->values); 3082 drm_mode_object_put(dev, &property->base); 3083 list_del(&property->head); 3084 kfree(property); 3085 } 3086 EXPORT_SYMBOL(drm_property_destroy); 3087 3088 void drm_object_attach_property(struct drm_mode_object *obj, 3089 struct drm_property *property, 3090 uint64_t init_val) 3091 { 3092 int count = obj->properties->count; 3093 3094 if (count == DRM_OBJECT_MAX_PROPERTY) { 3095 WARN(1, "Failed to attach object property (type: 0x%x). Please " 3096 "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time " 3097 "you see this message on the same object type.\n", 3098 obj->type); 3099 return; 3100 } 3101 3102 obj->properties->ids[count] = property->base.id; 3103 obj->properties->values[count] = init_val; 3104 obj->properties->count++; 3105 } 3106 EXPORT_SYMBOL(drm_object_attach_property); 3107 3108 int drm_object_property_set_value(struct drm_mode_object *obj, 3109 struct drm_property *property, uint64_t val) 3110 { 3111 int i; 3112 3113 for (i = 0; i < obj->properties->count; i++) { 3114 if (obj->properties->ids[i] == property->base.id) { 3115 obj->properties->values[i] = val; 3116 return 0; 3117 } 3118 } 3119 3120 return -EINVAL; 3121 } 3122 EXPORT_SYMBOL(drm_object_property_set_value); 3123 3124 int drm_object_property_get_value(struct drm_mode_object *obj, 3125 struct drm_property *property, uint64_t *val) 3126 { 3127 int i; 3128 3129 for (i = 0; i < obj->properties->count; i++) { 3130 if (obj->properties->ids[i] == property->base.id) { 3131 *val = obj->properties->values[i]; 3132 return 0; 3133 } 3134 } 3135 3136 return -EINVAL; 3137 } 3138 EXPORT_SYMBOL(drm_object_property_get_value); 3139 3140 int drm_mode_getproperty_ioctl(struct drm_device *dev, 3141 void *data, struct drm_file *file_priv) 3142 { 3143 struct drm_mode_object *obj; 3144 struct drm_mode_get_property *out_resp = data; 3145 struct drm_property *property; 3146 int enum_count = 0; 3147 int blob_count = 0; 3148 int value_count = 0; 3149 int ret = 0, i; 3150 int copied; 3151 struct drm_property_enum *prop_enum; 3152 struct drm_mode_property_enum __user *enum_ptr; 3153 struct drm_property_blob *prop_blob; 3154 uint32_t __user *blob_id_ptr; 3155 uint64_t __user *values_ptr; 3156 uint32_t __user *blob_length_ptr; 3157 3158 if (!drm_core_check_feature(dev, DRIVER_MODESET)) 3159 return -EINVAL; 3160 3161 drm_modeset_lock_all(dev); 3162 obj = drm_mode_object_find(dev, out_resp->prop_id, DRM_MODE_OBJECT_PROPERTY); 3163 if (!obj) { 3164 ret = -EINVAL; 3165 goto done; 3166 } 3167 property = obj_to_property(obj); 3168 3169 if (property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) { 3170 list_for_each_entry(prop_enum, &property->enum_blob_list, head) 3171 enum_count++; 3172 } else if (property->flags & DRM_MODE_PROP_BLOB) { 3173 list_for_each_entry(prop_blob, &property->enum_blob_list, head) 3174 blob_count++; 3175 } 3176 3177 value_count = property->num_values; 3178 3179 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN); 3180 out_resp->name[DRM_PROP_NAME_LEN-1] = 0; 3181 out_resp->flags = property->flags; 3182 3183 if ((out_resp->count_values >= value_count) && value_count) { 3184 values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr; 3185 for (i = 0; i < value_count; i++) { 3186 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) { 3187 ret = -EFAULT; 3188 goto done; 3189 } 3190 } 3191 } 3192 out_resp->count_values = value_count; 3193 3194 if (property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) { 3195 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) { 3196 copied = 0; 3197 enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr; 3198 list_for_each_entry(prop_enum, &property->enum_blob_list, head) { 3199 3200 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) { 3201 ret = -EFAULT; 3202 goto done; 3203 } 3204 3205 if (copy_to_user(&enum_ptr[copied].name, 3206 &prop_enum->name, DRM_PROP_NAME_LEN)) { 3207 ret = -EFAULT; 3208 goto done; 3209 } 3210 copied++; 3211 } 3212 } 3213 out_resp->count_enum_blobs = enum_count; 3214 } 3215 3216 if (property->flags & DRM_MODE_PROP_BLOB) { 3217 if ((out_resp->count_enum_blobs >= blob_count) && blob_count) { 3218 copied = 0; 3219 blob_id_ptr = (uint32_t __user *)(unsigned long)out_resp->enum_blob_ptr; 3220 blob_length_ptr = (uint32_t __user *)(unsigned long)out_resp->values_ptr; 3221 3222 list_for_each_entry(prop_blob, &property->enum_blob_list, head) { 3223 if (put_user(prop_blob->base.id, blob_id_ptr + copied)) { 3224 ret = -EFAULT; 3225 goto done; 3226 } 3227 3228 if (put_user(prop_blob->length, blob_length_ptr + copied)) { 3229 ret = -EFAULT; 3230 goto done; 3231 } 3232 3233 copied++; 3234 } 3235 } 3236 out_resp->count_enum_blobs = blob_count; 3237 } 3238 done: 3239 drm_modeset_unlock_all(dev); 3240 return ret; 3241 } 3242 3243 static struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length, 3244 void *data) 3245 { 3246 struct drm_property_blob *blob; 3247 int ret; 3248 3249 if (!length || !data) 3250 return NULL; 3251 3252 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL); 3253 if (!blob) 3254 return NULL; 3255 3256 ret = drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB); 3257 if (ret) { 3258 kfree(blob); 3259 return NULL; 3260 } 3261 3262 blob->length = length; 3263 3264 memcpy(blob->data, data, length); 3265 3266 list_add_tail(&blob->head, &dev->mode_config.property_blob_list); 3267 return blob; 3268 } 3269 3270 static void drm_property_destroy_blob(struct drm_device *dev, 3271 struct drm_property_blob *blob) 3272 { 3273 drm_mode_object_put(dev, &blob->base); 3274 list_del(&blob->head); 3275 kfree(blob); 3276 } 3277 3278 int drm_mode_getblob_ioctl(struct drm_device *dev, 3279 void *data, struct drm_file *file_priv) 3280 { 3281 struct drm_mode_object *obj; 3282 struct drm_mode_get_blob *out_resp = data; 3283 struct drm_property_blob *blob; 3284 int ret = 0; 3285 void __user *blob_ptr; 3286 3287 if (!drm_core_check_feature(dev, DRIVER_MODESET)) 3288 return -EINVAL; 3289 3290 drm_modeset_lock_all(dev); 3291 obj = drm_mode_object_find(dev, out_resp->blob_id, DRM_MODE_OBJECT_BLOB); 3292 if (!obj) { 3293 ret = -EINVAL; 3294 goto done; 3295 } 3296 blob = obj_to_blob(obj); 3297 3298 if (out_resp->length == blob->length) { 3299 blob_ptr = (void __user *)(unsigned long)out_resp->data; 3300 if (copy_to_user(blob_ptr, blob->data, blob->length)){ 3301 ret = -EFAULT; 3302 goto done; 3303 } 3304 } 3305 out_resp->length = blob->length; 3306 3307 done: 3308 drm_modeset_unlock_all(dev); 3309 return ret; 3310 } 3311 3312 int drm_mode_connector_update_edid_property(struct drm_connector *connector, 3313 struct edid *edid) 3314 { 3315 struct drm_device *dev = connector->dev; 3316 int ret, size; 3317 3318 if (connector->edid_blob_ptr) 3319 drm_property_destroy_blob(dev, connector->edid_blob_ptr); 3320 3321 /* Delete edid, when there is none. */ 3322 if (!edid) { 3323 connector->edid_blob_ptr = NULL; 3324 ret = drm_object_property_set_value(&connector->base, dev->mode_config.edid_property, 0); 3325 return ret; 3326 } 3327 3328 size = EDID_LENGTH * (1 + edid->extensions); 3329 connector->edid_blob_ptr = drm_property_create_blob(connector->dev, 3330 size, edid); 3331 if (!connector->edid_blob_ptr) 3332 return -EINVAL; 3333 3334 ret = drm_object_property_set_value(&connector->base, 3335 dev->mode_config.edid_property, 3336 connector->edid_blob_ptr->base.id); 3337 3338 return ret; 3339 } 3340 EXPORT_SYMBOL(drm_mode_connector_update_edid_property); 3341 3342 static bool drm_property_change_is_valid(struct drm_property *property, 3343 uint64_t value) 3344 { 3345 if (property->flags & DRM_MODE_PROP_IMMUTABLE) 3346 return false; 3347 if (property->flags & DRM_MODE_PROP_RANGE) { 3348 if (value < property->values[0] || value > property->values[1]) 3349 return false; 3350 return true; 3351 } else if (property->flags & DRM_MODE_PROP_BITMASK) { 3352 int i; 3353 uint64_t valid_mask = 0; 3354 for (i = 0; i < property->num_values; i++) 3355 valid_mask |= (1ULL << property->values[i]); 3356 return !(value & ~valid_mask); 3357 } else if (property->flags & DRM_MODE_PROP_BLOB) { 3358 /* Only the driver knows */ 3359 return true; 3360 } else { 3361 int i; 3362 for (i = 0; i < property->num_values; i++) 3363 if (property->values[i] == value) 3364 return true; 3365 return false; 3366 } 3367 } 3368 3369 int drm_mode_connector_property_set_ioctl(struct drm_device *dev, 3370 void *data, struct drm_file *file_priv) 3371 { 3372 struct drm_mode_connector_set_property *conn_set_prop = data; 3373 struct drm_mode_obj_set_property obj_set_prop = { 3374 .value = conn_set_prop->value, 3375 .prop_id = conn_set_prop->prop_id, 3376 .obj_id = conn_set_prop->connector_id, 3377 .obj_type = DRM_MODE_OBJECT_CONNECTOR 3378 }; 3379 3380 /* It does all the locking and checking we need */ 3381 return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv); 3382 } 3383 3384 static int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj, 3385 struct drm_property *property, 3386 uint64_t value) 3387 { 3388 int ret = -EINVAL; 3389 struct drm_connector *connector = obj_to_connector(obj); 3390 3391 /* Do DPMS ourselves */ 3392 if (property == connector->dev->mode_config.dpms_property) { 3393 if (connector->funcs->dpms) 3394 (*connector->funcs->dpms)(connector, (int)value); 3395 ret = 0; 3396 } else if (connector->funcs->set_property) 3397 ret = connector->funcs->set_property(connector, property, value); 3398 3399 /* store the property value if successful */ 3400 if (!ret) 3401 drm_object_property_set_value(&connector->base, property, value); 3402 return ret; 3403 } 3404 3405 static int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj, 3406 struct drm_property *property, 3407 uint64_t value) 3408 { 3409 int ret = -EINVAL; 3410 struct drm_crtc *crtc = obj_to_crtc(obj); 3411 3412 if (crtc->funcs->set_property) 3413 ret = crtc->funcs->set_property(crtc, property, value); 3414 if (!ret) 3415 drm_object_property_set_value(obj, property, value); 3416 3417 return ret; 3418 } 3419 3420 static int drm_mode_plane_set_obj_prop(struct drm_mode_object *obj, 3421 struct drm_property *property, 3422 uint64_t value) 3423 { 3424 int ret = -EINVAL; 3425 struct drm_plane *plane = obj_to_plane(obj); 3426 3427 if (plane->funcs->set_property) 3428 ret = plane->funcs->set_property(plane, property, value); 3429 if (!ret) 3430 drm_object_property_set_value(obj, property, value); 3431 3432 return ret; 3433 } 3434 3435 int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data, 3436 struct drm_file *file_priv) 3437 { 3438 struct drm_mode_obj_get_properties *arg = data; 3439 struct drm_mode_object *obj; 3440 int ret = 0; 3441 int i; 3442 int copied = 0; 3443 int props_count = 0; 3444 uint32_t __user *props_ptr; 3445 uint64_t __user *prop_values_ptr; 3446 3447 if (!drm_core_check_feature(dev, DRIVER_MODESET)) 3448 return -EINVAL; 3449 3450 drm_modeset_lock_all(dev); 3451 3452 obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type); 3453 if (!obj) { 3454 ret = -EINVAL; 3455 goto out; 3456 } 3457 if (!obj->properties) { 3458 ret = -EINVAL; 3459 goto out; 3460 } 3461 3462 props_count = obj->properties->count; 3463 3464 /* This ioctl is called twice, once to determine how much space is 3465 * needed, and the 2nd time to fill it. */ 3466 if ((arg->count_props >= props_count) && props_count) { 3467 copied = 0; 3468 props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr); 3469 prop_values_ptr = (uint64_t __user *)(unsigned long) 3470 (arg->prop_values_ptr); 3471 for (i = 0; i < props_count; i++) { 3472 if (put_user(obj->properties->ids[i], 3473 props_ptr + copied)) { 3474 ret = -EFAULT; 3475 goto out; 3476 } 3477 if (put_user(obj->properties->values[i], 3478 prop_values_ptr + copied)) { 3479 ret = -EFAULT; 3480 goto out; 3481 } 3482 copied++; 3483 } 3484 } 3485 arg->count_props = props_count; 3486 out: 3487 drm_modeset_unlock_all(dev); 3488 return ret; 3489 } 3490 3491 int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data, 3492 struct drm_file *file_priv) 3493 { 3494 struct drm_mode_obj_set_property *arg = data; 3495 struct drm_mode_object *arg_obj; 3496 struct drm_mode_object *prop_obj; 3497 struct drm_property *property; 3498 int ret = -EINVAL; 3499 int i; 3500 3501 if (!drm_core_check_feature(dev, DRIVER_MODESET)) 3502 return -EINVAL; 3503 3504 drm_modeset_lock_all(dev); 3505 3506 arg_obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type); 3507 if (!arg_obj) 3508 goto out; 3509 if (!arg_obj->properties) 3510 goto out; 3511 3512 for (i = 0; i < arg_obj->properties->count; i++) 3513 if (arg_obj->properties->ids[i] == arg->prop_id) 3514 break; 3515 3516 if (i == arg_obj->properties->count) 3517 goto out; 3518 3519 prop_obj = drm_mode_object_find(dev, arg->prop_id, 3520 DRM_MODE_OBJECT_PROPERTY); 3521 if (!prop_obj) 3522 goto out; 3523 property = obj_to_property(prop_obj); 3524 3525 if (!drm_property_change_is_valid(property, arg->value)) 3526 goto out; 3527 3528 switch (arg_obj->type) { 3529 case DRM_MODE_OBJECT_CONNECTOR: 3530 ret = drm_mode_connector_set_obj_prop(arg_obj, property, 3531 arg->value); 3532 break; 3533 case DRM_MODE_OBJECT_CRTC: 3534 ret = drm_mode_crtc_set_obj_prop(arg_obj, property, arg->value); 3535 break; 3536 case DRM_MODE_OBJECT_PLANE: 3537 ret = drm_mode_plane_set_obj_prop(arg_obj, property, arg->value); 3538 break; 3539 } 3540 3541 out: 3542 drm_modeset_unlock_all(dev); 3543 return ret; 3544 } 3545 3546 int drm_mode_connector_attach_encoder(struct drm_connector *connector, 3547 struct drm_encoder *encoder) 3548 { 3549 int i; 3550 3551 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) { 3552 if (connector->encoder_ids[i] == 0) { 3553 connector->encoder_ids[i] = encoder->base.id; 3554 return 0; 3555 } 3556 } 3557 return -ENOMEM; 3558 } 3559 EXPORT_SYMBOL(drm_mode_connector_attach_encoder); 3560 3561 void drm_mode_connector_detach_encoder(struct drm_connector *connector, 3562 struct drm_encoder *encoder) 3563 { 3564 int i; 3565 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) { 3566 if (connector->encoder_ids[i] == encoder->base.id) { 3567 connector->encoder_ids[i] = 0; 3568 if (connector->encoder == encoder) 3569 connector->encoder = NULL; 3570 break; 3571 } 3572 } 3573 } 3574 EXPORT_SYMBOL(drm_mode_connector_detach_encoder); 3575 3576 int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc, 3577 int gamma_size) 3578 { 3579 crtc->gamma_size = gamma_size; 3580 3581 crtc->gamma_store = kzalloc(gamma_size * sizeof(uint16_t) * 3, GFP_KERNEL); 3582 if (!crtc->gamma_store) { 3583 crtc->gamma_size = 0; 3584 return -ENOMEM; 3585 } 3586 3587 return 0; 3588 } 3589 EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size); 3590 3591 int drm_mode_gamma_set_ioctl(struct drm_device *dev, 3592 void *data, struct drm_file *file_priv) 3593 { 3594 struct drm_mode_crtc_lut *crtc_lut = data; 3595 struct drm_mode_object *obj; 3596 struct drm_crtc *crtc; 3597 void *r_base, *g_base, *b_base; 3598 int size; 3599 int ret = 0; 3600 3601 if (!drm_core_check_feature(dev, DRIVER_MODESET)) 3602 return -EINVAL; 3603 3604 drm_modeset_lock_all(dev); 3605 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC); 3606 if (!obj) { 3607 ret = -EINVAL; 3608 goto out; 3609 } 3610 crtc = obj_to_crtc(obj); 3611 3612 if (crtc->funcs->gamma_set == NULL) { 3613 ret = -ENOSYS; 3614 goto out; 3615 } 3616 3617 /* memcpy into gamma store */ 3618 if (crtc_lut->gamma_size != crtc->gamma_size) { 3619 ret = -EINVAL; 3620 goto out; 3621 } 3622 3623 size = crtc_lut->gamma_size * (sizeof(uint16_t)); 3624 r_base = crtc->gamma_store; 3625 if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) { 3626 ret = -EFAULT; 3627 goto out; 3628 } 3629 3630 g_base = r_base + size; 3631 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) { 3632 ret = -EFAULT; 3633 goto out; 3634 } 3635 3636 b_base = g_base + size; 3637 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) { 3638 ret = -EFAULT; 3639 goto out; 3640 } 3641 3642 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size); 3643 3644 out: 3645 drm_modeset_unlock_all(dev); 3646 return ret; 3647 3648 } 3649 3650 int drm_mode_gamma_get_ioctl(struct drm_device *dev, 3651 void *data, struct drm_file *file_priv) 3652 { 3653 struct drm_mode_crtc_lut *crtc_lut = data; 3654 struct drm_mode_object *obj; 3655 struct drm_crtc *crtc; 3656 void *r_base, *g_base, *b_base; 3657 int size; 3658 int ret = 0; 3659 3660 if (!drm_core_check_feature(dev, DRIVER_MODESET)) 3661 return -EINVAL; 3662 3663 drm_modeset_lock_all(dev); 3664 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC); 3665 if (!obj) { 3666 ret = -EINVAL; 3667 goto out; 3668 } 3669 crtc = obj_to_crtc(obj); 3670 3671 /* memcpy into gamma store */ 3672 if (crtc_lut->gamma_size != crtc->gamma_size) { 3673 ret = -EINVAL; 3674 goto out; 3675 } 3676 3677 size = crtc_lut->gamma_size * (sizeof(uint16_t)); 3678 r_base = crtc->gamma_store; 3679 if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) { 3680 ret = -EFAULT; 3681 goto out; 3682 } 3683 3684 g_base = r_base + size; 3685 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) { 3686 ret = -EFAULT; 3687 goto out; 3688 } 3689 3690 b_base = g_base + size; 3691 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) { 3692 ret = -EFAULT; 3693 goto out; 3694 } 3695 out: 3696 drm_modeset_unlock_all(dev); 3697 return ret; 3698 } 3699 3700 int drm_mode_page_flip_ioctl(struct drm_device *dev, 3701 void *data, struct drm_file *file_priv) 3702 { 3703 struct drm_mode_crtc_page_flip *page_flip = data; 3704 struct drm_mode_object *obj; 3705 struct drm_crtc *crtc; 3706 struct drm_framebuffer *fb = NULL, *old_fb = NULL; 3707 struct drm_pending_vblank_event *e = NULL; 3708 unsigned long flags; 3709 int hdisplay, vdisplay; 3710 int ret = -EINVAL; 3711 3712 if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS || 3713 page_flip->reserved != 0) 3714 return -EINVAL; 3715 3716 obj = drm_mode_object_find(dev, page_flip->crtc_id, DRM_MODE_OBJECT_CRTC); 3717 if (!obj) 3718 return -EINVAL; 3719 crtc = obj_to_crtc(obj); 3720 3721 mutex_lock(&crtc->mutex); 3722 if (crtc->fb == NULL) { 3723 /* The framebuffer is currently unbound, presumably 3724 * due to a hotplug event, that userspace has not 3725 * yet discovered. 3726 */ 3727 ret = -EBUSY; 3728 goto out; 3729 } 3730 3731 if (crtc->funcs->page_flip == NULL) 3732 goto out; 3733 3734 fb = drm_framebuffer_lookup(dev, page_flip->fb_id); 3735 if (!fb) 3736 goto out; 3737 3738 hdisplay = crtc->mode.hdisplay; 3739 vdisplay = crtc->mode.vdisplay; 3740 3741 if (crtc->invert_dimensions) 3742 swap(hdisplay, vdisplay); 3743 3744 if (hdisplay > fb->width || 3745 vdisplay > fb->height || 3746 crtc->x > fb->width - hdisplay || 3747 crtc->y > fb->height - vdisplay) { 3748 DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n", 3749 fb->width, fb->height, hdisplay, vdisplay, crtc->x, crtc->y, 3750 crtc->invert_dimensions ? " (inverted)" : ""); 3751 ret = -ENOSPC; 3752 goto out; 3753 } 3754 3755 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) { 3756 ret = -ENOMEM; 3757 spin_lock_irqsave(&dev->event_lock, flags); 3758 if (file_priv->event_space < sizeof e->event) { 3759 spin_unlock_irqrestore(&dev->event_lock, flags); 3760 goto out; 3761 } 3762 file_priv->event_space -= sizeof e->event; 3763 spin_unlock_irqrestore(&dev->event_lock, flags); 3764 3765 e = kzalloc(sizeof *e, GFP_KERNEL); 3766 if (e == NULL) { 3767 spin_lock_irqsave(&dev->event_lock, flags); 3768 file_priv->event_space += sizeof e->event; 3769 spin_unlock_irqrestore(&dev->event_lock, flags); 3770 goto out; 3771 } 3772 3773 e->event.base.type = DRM_EVENT_FLIP_COMPLETE; 3774 e->event.base.length = sizeof e->event; 3775 e->event.user_data = page_flip->user_data; 3776 e->base.event = &e->event.base; 3777 e->base.file_priv = file_priv; 3778 e->base.destroy = 3779 (void (*) (struct drm_pending_event *)) kfree; 3780 } 3781 3782 old_fb = crtc->fb; 3783 ret = crtc->funcs->page_flip(crtc, fb, e); 3784 if (ret) { 3785 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) { 3786 spin_lock_irqsave(&dev->event_lock, flags); 3787 file_priv->event_space += sizeof e->event; 3788 spin_unlock_irqrestore(&dev->event_lock, flags); 3789 kfree(e); 3790 } 3791 /* Keep the old fb, don't unref it. */ 3792 old_fb = NULL; 3793 } else { 3794 /* 3795 * Warn if the driver hasn't properly updated the crtc->fb 3796 * field to reflect that the new framebuffer is now used. 3797 * Failing to do so will screw with the reference counting 3798 * on framebuffers. 3799 */ 3800 WARN_ON(crtc->fb != fb); 3801 /* Unref only the old framebuffer. */ 3802 fb = NULL; 3803 } 3804 3805 out: 3806 if (fb) 3807 drm_framebuffer_unreference(fb); 3808 if (old_fb) 3809 drm_framebuffer_unreference(old_fb); 3810 mutex_unlock(&crtc->mutex); 3811 3812 return ret; 3813 } 3814 3815 void drm_mode_config_reset(struct drm_device *dev) 3816 { 3817 struct drm_crtc *crtc; 3818 struct drm_encoder *encoder; 3819 struct drm_connector *connector; 3820 3821 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) 3822 if (crtc->funcs->reset) 3823 crtc->funcs->reset(crtc); 3824 3825 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) 3826 if (encoder->funcs->reset) 3827 encoder->funcs->reset(encoder); 3828 3829 list_for_each_entry(connector, &dev->mode_config.connector_list, head) { 3830 connector->status = connector_status_unknown; 3831 3832 if (connector->funcs->reset) 3833 connector->funcs->reset(connector); 3834 } 3835 } 3836 EXPORT_SYMBOL(drm_mode_config_reset); 3837 3838 int drm_mode_create_dumb_ioctl(struct drm_device *dev, 3839 void *data, struct drm_file *file_priv) 3840 { 3841 struct drm_mode_create_dumb *args = data; 3842 3843 if (!dev->driver->dumb_create) 3844 return -ENOSYS; 3845 return dev->driver->dumb_create(file_priv, dev, args); 3846 } 3847 3848 int drm_mode_mmap_dumb_ioctl(struct drm_device *dev, 3849 void *data, struct drm_file *file_priv) 3850 { 3851 struct drm_mode_map_dumb *args = data; 3852 3853 /* call driver ioctl to get mmap offset */ 3854 if (!dev->driver->dumb_map_offset) 3855 return -ENOSYS; 3856 3857 return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset); 3858 } 3859 3860 int drm_mode_destroy_dumb_ioctl(struct drm_device *dev, 3861 void *data, struct drm_file *file_priv) 3862 { 3863 struct drm_mode_destroy_dumb *args = data; 3864 3865 if (!dev->driver->dumb_destroy) 3866 return -ENOSYS; 3867 3868 return dev->driver->dumb_destroy(file_priv, dev, args->handle); 3869 } 3870 3871 /* 3872 * Just need to support RGB formats here for compat with code that doesn't 3873 * use pixel formats directly yet. 3874 */ 3875 void drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth, 3876 int *bpp) 3877 { 3878 switch (format) { 3879 case DRM_FORMAT_C8: 3880 case DRM_FORMAT_RGB332: 3881 case DRM_FORMAT_BGR233: 3882 *depth = 8; 3883 *bpp = 8; 3884 break; 3885 case DRM_FORMAT_XRGB1555: 3886 case DRM_FORMAT_XBGR1555: 3887 case DRM_FORMAT_RGBX5551: 3888 case DRM_FORMAT_BGRX5551: 3889 case DRM_FORMAT_ARGB1555: 3890 case DRM_FORMAT_ABGR1555: 3891 case DRM_FORMAT_RGBA5551: 3892 case DRM_FORMAT_BGRA5551: 3893 *depth = 15; 3894 *bpp = 16; 3895 break; 3896 case DRM_FORMAT_RGB565: 3897 case DRM_FORMAT_BGR565: 3898 *depth = 16; 3899 *bpp = 16; 3900 break; 3901 case DRM_FORMAT_RGB888: 3902 case DRM_FORMAT_BGR888: 3903 *depth = 24; 3904 *bpp = 24; 3905 break; 3906 case DRM_FORMAT_XRGB8888: 3907 case DRM_FORMAT_XBGR8888: 3908 case DRM_FORMAT_RGBX8888: 3909 case DRM_FORMAT_BGRX8888: 3910 *depth = 24; 3911 *bpp = 32; 3912 break; 3913 case DRM_FORMAT_XRGB2101010: 3914 case DRM_FORMAT_XBGR2101010: 3915 case DRM_FORMAT_RGBX1010102: 3916 case DRM_FORMAT_BGRX1010102: 3917 case DRM_FORMAT_ARGB2101010: 3918 case DRM_FORMAT_ABGR2101010: 3919 case DRM_FORMAT_RGBA1010102: 3920 case DRM_FORMAT_BGRA1010102: 3921 *depth = 30; 3922 *bpp = 32; 3923 break; 3924 case DRM_FORMAT_ARGB8888: 3925 case DRM_FORMAT_ABGR8888: 3926 case DRM_FORMAT_RGBA8888: 3927 case DRM_FORMAT_BGRA8888: 3928 *depth = 32; 3929 *bpp = 32; 3930 break; 3931 default: 3932 DRM_DEBUG_KMS("unsupported pixel format\n"); 3933 *depth = 0; 3934 *bpp = 0; 3935 break; 3936 } 3937 } 3938 EXPORT_SYMBOL(drm_fb_get_bpp_depth); 3939 3940 /** 3941 * drm_format_num_planes - get the number of planes for format 3942 * @format: pixel format (DRM_FORMAT_*) 3943 * 3944 * RETURNS: 3945 * The number of planes used by the specified pixel format. 3946 */ 3947 int drm_format_num_planes(uint32_t format) 3948 { 3949 switch (format) { 3950 case DRM_FORMAT_YUV410: 3951 case DRM_FORMAT_YVU410: 3952 case DRM_FORMAT_YUV411: 3953 case DRM_FORMAT_YVU411: 3954 case DRM_FORMAT_YUV420: 3955 case DRM_FORMAT_YVU420: 3956 case DRM_FORMAT_YUV422: 3957 case DRM_FORMAT_YVU422: 3958 case DRM_FORMAT_YUV444: 3959 case DRM_FORMAT_YVU444: 3960 return 3; 3961 case DRM_FORMAT_NV12: 3962 case DRM_FORMAT_NV21: 3963 case DRM_FORMAT_NV16: 3964 case DRM_FORMAT_NV61: 3965 case DRM_FORMAT_NV24: 3966 case DRM_FORMAT_NV42: 3967 return 2; 3968 default: 3969 return 1; 3970 } 3971 } 3972 EXPORT_SYMBOL(drm_format_num_planes); 3973 3974 /** 3975 * drm_format_plane_cpp - determine the bytes per pixel value 3976 * @format: pixel format (DRM_FORMAT_*) 3977 * @plane: plane index 3978 * 3979 * RETURNS: 3980 * The bytes per pixel value for the specified plane. 3981 */ 3982 int drm_format_plane_cpp(uint32_t format, int plane) 3983 { 3984 unsigned int depth; 3985 int bpp; 3986 3987 if (plane >= drm_format_num_planes(format)) 3988 return 0; 3989 3990 switch (format) { 3991 case DRM_FORMAT_YUYV: 3992 case DRM_FORMAT_YVYU: 3993 case DRM_FORMAT_UYVY: 3994 case DRM_FORMAT_VYUY: 3995 return 2; 3996 case DRM_FORMAT_NV12: 3997 case DRM_FORMAT_NV21: 3998 case DRM_FORMAT_NV16: 3999 case DRM_FORMAT_NV61: 4000 case DRM_FORMAT_NV24: 4001 case DRM_FORMAT_NV42: 4002 return plane ? 2 : 1; 4003 case DRM_FORMAT_YUV410: 4004 case DRM_FORMAT_YVU410: 4005 case DRM_FORMAT_YUV411: 4006 case DRM_FORMAT_YVU411: 4007 case DRM_FORMAT_YUV420: 4008 case DRM_FORMAT_YVU420: 4009 case DRM_FORMAT_YUV422: 4010 case DRM_FORMAT_YVU422: 4011 case DRM_FORMAT_YUV444: 4012 case DRM_FORMAT_YVU444: 4013 return 1; 4014 default: 4015 drm_fb_get_bpp_depth(format, &depth, &bpp); 4016 return bpp >> 3; 4017 } 4018 } 4019 EXPORT_SYMBOL(drm_format_plane_cpp); 4020 4021 /** 4022 * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor 4023 * @format: pixel format (DRM_FORMAT_*) 4024 * 4025 * RETURNS: 4026 * The horizontal chroma subsampling factor for the 4027 * specified pixel format. 4028 */ 4029 int drm_format_horz_chroma_subsampling(uint32_t format) 4030 { 4031 switch (format) { 4032 case DRM_FORMAT_YUV411: 4033 case DRM_FORMAT_YVU411: 4034 case DRM_FORMAT_YUV410: 4035 case DRM_FORMAT_YVU410: 4036 return 4; 4037 case DRM_FORMAT_YUYV: 4038 case DRM_FORMAT_YVYU: 4039 case DRM_FORMAT_UYVY: 4040 case DRM_FORMAT_VYUY: 4041 case DRM_FORMAT_NV12: 4042 case DRM_FORMAT_NV21: 4043 case DRM_FORMAT_NV16: 4044 case DRM_FORMAT_NV61: 4045 case DRM_FORMAT_YUV422: 4046 case DRM_FORMAT_YVU422: 4047 case DRM_FORMAT_YUV420: 4048 case DRM_FORMAT_YVU420: 4049 return 2; 4050 default: 4051 return 1; 4052 } 4053 } 4054 EXPORT_SYMBOL(drm_format_horz_chroma_subsampling); 4055 4056 /** 4057 * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor 4058 * @format: pixel format (DRM_FORMAT_*) 4059 * 4060 * RETURNS: 4061 * The vertical chroma subsampling factor for the 4062 * specified pixel format. 4063 */ 4064 int drm_format_vert_chroma_subsampling(uint32_t format) 4065 { 4066 switch (format) { 4067 case DRM_FORMAT_YUV410: 4068 case DRM_FORMAT_YVU410: 4069 return 4; 4070 case DRM_FORMAT_YUV420: 4071 case DRM_FORMAT_YVU420: 4072 case DRM_FORMAT_NV12: 4073 case DRM_FORMAT_NV21: 4074 return 2; 4075 default: 4076 return 1; 4077 } 4078 } 4079 EXPORT_SYMBOL(drm_format_vert_chroma_subsampling); 4080