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