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