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