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