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