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/ctype.h> 33 #include <linux/list.h> 34 #include <linux/export.h> 35 #include <drm/drmP.h> 36 #include <drm/drm_crtc.h> 37 #include <drm/drm_edid.h> 38 #include <uapi_drm/drm_fourcc.h> 39 #include <linux/slab.h> 40 #include <drm/drm_modeset_lock.h> 41 42 #include "drm_crtc_internal.h" 43 44 static struct drm_framebuffer *add_framebuffer_internal(struct drm_device *dev, 45 struct drm_mode_fb_cmd2 *r, 46 struct drm_file *file_priv); 47 48 /* Avoid boilerplate. I'm tired of typing. */ 49 #define DRM_ENUM_NAME_FN(fnname, list) \ 50 const char *fnname(int val) \ 51 { \ 52 int i; \ 53 for (i = 0; i < ARRAY_SIZE(list); i++) { \ 54 if (list[i].type == val) \ 55 return list[i].name; \ 56 } \ 57 return "(unknown)"; \ 58 } 59 60 /* 61 * Global properties 62 */ 63 static const struct drm_prop_enum_list drm_dpms_enum_list[] = 64 { { DRM_MODE_DPMS_ON, "On" }, 65 { DRM_MODE_DPMS_STANDBY, "Standby" }, 66 { DRM_MODE_DPMS_SUSPEND, "Suspend" }, 67 { DRM_MODE_DPMS_OFF, "Off" } 68 }; 69 70 DRM_ENUM_NAME_FN(drm_get_dpms_name, drm_dpms_enum_list) 71 72 static const struct drm_prop_enum_list drm_plane_type_enum_list[] = 73 { 74 { DRM_PLANE_TYPE_OVERLAY, "Overlay" }, 75 { DRM_PLANE_TYPE_PRIMARY, "Primary" }, 76 { DRM_PLANE_TYPE_CURSOR, "Cursor" }, 77 }; 78 79 /* 80 * Optional properties 81 */ 82 static const struct drm_prop_enum_list drm_scaling_mode_enum_list[] = 83 { 84 { DRM_MODE_SCALE_NONE, "None" }, 85 { DRM_MODE_SCALE_FULLSCREEN, "Full" }, 86 { DRM_MODE_SCALE_CENTER, "Center" }, 87 { DRM_MODE_SCALE_ASPECT, "Full aspect" }, 88 }; 89 90 static const struct drm_prop_enum_list drm_aspect_ratio_enum_list[] = { 91 { DRM_MODE_PICTURE_ASPECT_NONE, "Automatic" }, 92 { DRM_MODE_PICTURE_ASPECT_4_3, "4:3" }, 93 { DRM_MODE_PICTURE_ASPECT_16_9, "16:9" }, 94 }; 95 96 /* 97 * Non-global properties, but "required" for certain connectors. 98 */ 99 static const struct drm_prop_enum_list drm_dvi_i_select_enum_list[] = 100 { 101 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */ 102 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */ 103 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */ 104 }; 105 106 DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name, drm_dvi_i_select_enum_list) 107 108 static const struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list[] = 109 { 110 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */ 111 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */ 112 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */ 113 }; 114 115 DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name, 116 drm_dvi_i_subconnector_enum_list) 117 118 static const struct drm_prop_enum_list drm_tv_select_enum_list[] = 119 { 120 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */ 121 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */ 122 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */ 123 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */ 124 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */ 125 }; 126 127 DRM_ENUM_NAME_FN(drm_get_tv_select_name, drm_tv_select_enum_list) 128 129 static const struct drm_prop_enum_list drm_tv_subconnector_enum_list[] = 130 { 131 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */ 132 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */ 133 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */ 134 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */ 135 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */ 136 }; 137 138 DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name, 139 drm_tv_subconnector_enum_list) 140 141 static const struct drm_prop_enum_list drm_dirty_info_enum_list[] = { 142 { DRM_MODE_DIRTY_OFF, "Off" }, 143 { DRM_MODE_DIRTY_ON, "On" }, 144 { DRM_MODE_DIRTY_ANNOTATE, "Annotate" }, 145 }; 146 147 struct drm_conn_prop_enum_list { 148 int type; 149 const char *name; 150 int count; 151 }; 152 153 /* 154 * Connector and encoder types. 155 */ 156 static struct drm_conn_prop_enum_list drm_connector_enum_list[] = 157 { { DRM_MODE_CONNECTOR_Unknown, "Unknown" }, 158 { DRM_MODE_CONNECTOR_VGA, "VGA" }, 159 { DRM_MODE_CONNECTOR_DVII, "DVI-I" }, 160 { DRM_MODE_CONNECTOR_DVID, "DVI-D" }, 161 { DRM_MODE_CONNECTOR_DVIA, "DVI-A" }, 162 { DRM_MODE_CONNECTOR_Composite, "Composite" }, 163 { DRM_MODE_CONNECTOR_SVIDEO, "SVIDEO" }, 164 { DRM_MODE_CONNECTOR_LVDS, "LVDS" }, 165 { DRM_MODE_CONNECTOR_Component, "Component" }, 166 { DRM_MODE_CONNECTOR_9PinDIN, "DIN" }, 167 { DRM_MODE_CONNECTOR_DisplayPort, "DP" }, 168 { DRM_MODE_CONNECTOR_HDMIA, "HDMI-A" }, 169 { DRM_MODE_CONNECTOR_HDMIB, "HDMI-B" }, 170 { DRM_MODE_CONNECTOR_TV, "TV" }, 171 { DRM_MODE_CONNECTOR_eDP, "eDP" }, 172 { DRM_MODE_CONNECTOR_VIRTUAL, "Virtual" }, 173 { DRM_MODE_CONNECTOR_DSI, "DSI" }, 174 }; 175 176 static const struct drm_prop_enum_list drm_encoder_enum_list[] = 177 { { DRM_MODE_ENCODER_NONE, "None" }, 178 { DRM_MODE_ENCODER_DAC, "DAC" }, 179 { DRM_MODE_ENCODER_TMDS, "TMDS" }, 180 { DRM_MODE_ENCODER_LVDS, "LVDS" }, 181 { DRM_MODE_ENCODER_TVDAC, "TV" }, 182 { DRM_MODE_ENCODER_VIRTUAL, "Virtual" }, 183 { DRM_MODE_ENCODER_DSI, "DSI" }, 184 { DRM_MODE_ENCODER_DPMST, "DP MST" }, 185 }; 186 187 static const struct drm_prop_enum_list drm_subpixel_enum_list[] = 188 { 189 { SubPixelUnknown, "Unknown" }, 190 { SubPixelHorizontalRGB, "Horizontal RGB" }, 191 { SubPixelHorizontalBGR, "Horizontal BGR" }, 192 { SubPixelVerticalRGB, "Vertical RGB" }, 193 { SubPixelVerticalBGR, "Vertical BGR" }, 194 { SubPixelNone, "None" }, 195 }; 196 197 void drm_connector_ida_init(void) 198 { 199 #if 0 200 int i; 201 202 for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++) 203 ida_init(&drm_connector_enum_list[i].ida); 204 #endif 205 } 206 207 void drm_connector_ida_destroy(void) 208 { 209 #if 0 210 int i; 211 212 for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++) 213 ida_destroy(&drm_connector_enum_list[i].ida); 214 #endif 215 } 216 217 /** 218 * drm_get_connector_status_name - return a string for connector status 219 * @status: connector status to compute name of 220 * 221 * In contrast to the other drm_get_*_name functions this one here returns a 222 * const pointer and hence is threadsafe. 223 */ 224 const char *drm_get_connector_status_name(enum drm_connector_status status) 225 { 226 if (status == connector_status_connected) 227 return "connected"; 228 else if (status == connector_status_disconnected) 229 return "disconnected"; 230 else 231 return "unknown"; 232 } 233 EXPORT_SYMBOL(drm_get_connector_status_name); 234 235 /** 236 * drm_get_subpixel_order_name - return a string for a given subpixel enum 237 * @order: enum of subpixel_order 238 * 239 * Note you could abuse this and return something out of bounds, but that 240 * would be a caller error. No unscrubbed user data should make it here. 241 */ 242 const char *drm_get_subpixel_order_name(enum subpixel_order order) 243 { 244 return drm_subpixel_enum_list[order].name; 245 } 246 EXPORT_SYMBOL(drm_get_subpixel_order_name); 247 248 static char printable_char(int c) 249 { 250 return isascii(c) && isprint(c) ? c : '?'; 251 } 252 253 /** 254 * drm_get_format_name - return a string for drm fourcc format 255 * @format: format to compute name of 256 * 257 * Note that the buffer used by this function is globally shared and owned by 258 * the function itself. 259 * 260 * FIXME: This isn't really multithreading safe. 261 */ 262 const char *drm_get_format_name(uint32_t format) 263 { 264 static char buf[32]; 265 266 ksnprintf(buf, sizeof(buf), 267 "%c%c%c%c %s-endian (0x%08x)", 268 printable_char(format & 0xff), 269 printable_char((format >> 8) & 0xff), 270 printable_char((format >> 16) & 0xff), 271 printable_char((format >> 24) & 0x7f), 272 format & DRM_FORMAT_BIG_ENDIAN ? "big" : "little", 273 format); 274 275 return buf; 276 } 277 EXPORT_SYMBOL(drm_get_format_name); 278 279 /** 280 * drm_mode_object_get - allocate a new modeset identifier 281 * @dev: DRM device 282 * @obj: object pointer, used to generate unique ID 283 * @obj_type: object type 284 * 285 * Create a unique identifier based on @ptr in @dev's identifier space. Used 286 * for tracking modes, CRTCs and connectors. Note that despite the _get postfix 287 * modeset identifiers are _not_ reference counted. Hence don't use this for 288 * reference counted modeset objects like framebuffers. 289 * 290 * Returns: 291 * New unique (relative to other objects in @dev) integer identifier for the 292 * object. 293 */ 294 int drm_mode_object_get(struct drm_device *dev, 295 struct drm_mode_object *obj, uint32_t obj_type) 296 { 297 int ret; 298 299 mutex_lock(&dev->mode_config.idr_mutex); 300 ret = idr_alloc(&dev->mode_config.crtc_idr, obj, 1, 0, GFP_KERNEL); 301 if (ret >= 0) { 302 /* 303 * Set up the object linking under the protection of the idr 304 * lock so that other users can't see inconsistent state. 305 */ 306 obj->id = ret; 307 obj->type = obj_type; 308 } 309 mutex_unlock(&dev->mode_config.idr_mutex); 310 311 return ret < 0 ? ret : 0; 312 } 313 314 /** 315 * drm_mode_object_put - free a modeset identifer 316 * @dev: DRM device 317 * @object: object to free 318 * 319 * Free @id from @dev's unique identifier pool. Note that despite the _get 320 * postfix modeset identifiers are _not_ reference counted. Hence don't use this 321 * for reference counted modeset objects like framebuffers. 322 */ 323 void drm_mode_object_put(struct drm_device *dev, 324 struct drm_mode_object *object) 325 { 326 mutex_lock(&dev->mode_config.idr_mutex); 327 idr_remove(&dev->mode_config.crtc_idr, object->id); 328 mutex_unlock(&dev->mode_config.idr_mutex); 329 } 330 331 static struct drm_mode_object *_object_find(struct drm_device *dev, 332 uint32_t id, uint32_t type) 333 { 334 struct drm_mode_object *obj = NULL; 335 336 mutex_lock(&dev->mode_config.idr_mutex); 337 obj = idr_find(&dev->mode_config.crtc_idr, id); 338 if (obj && type != DRM_MODE_OBJECT_ANY && obj->type != type) 339 obj = NULL; 340 if (obj && obj->id != id) 341 obj = NULL; 342 /* don't leak out unref'd fb's */ 343 if (obj && (obj->type == DRM_MODE_OBJECT_FB)) 344 obj = NULL; 345 mutex_unlock(&dev->mode_config.idr_mutex); 346 347 return obj; 348 } 349 350 /** 351 * drm_mode_object_find - look up a drm object with static lifetime 352 * @dev: drm device 353 * @id: id of the mode object 354 * @type: type of the mode object 355 * 356 * Note that framebuffers cannot be looked up with this functions - since those 357 * are reference counted, they need special treatment. Even with 358 * DRM_MODE_OBJECT_ANY (although that will simply return NULL 359 * rather than WARN_ON()). 360 */ 361 struct drm_mode_object *drm_mode_object_find(struct drm_device *dev, 362 uint32_t id, uint32_t type) 363 { 364 struct drm_mode_object *obj = NULL; 365 366 /* Framebuffers are reference counted and need their own lookup 367 * function.*/ 368 WARN_ON(type == DRM_MODE_OBJECT_FB); 369 obj = _object_find(dev, id, type); 370 return obj; 371 } 372 EXPORT_SYMBOL(drm_mode_object_find); 373 374 /** 375 * drm_framebuffer_init - initialize a framebuffer 376 * @dev: DRM device 377 * @fb: framebuffer to be initialized 378 * @funcs: ... with these functions 379 * 380 * Allocates an ID for the framebuffer's parent mode object, sets its mode 381 * functions & device file and adds it to the master fd list. 382 * 383 * IMPORTANT: 384 * This functions publishes the fb and makes it available for concurrent access 385 * by other users. Which means by this point the fb _must_ be fully set up - 386 * since all the fb attributes are invariant over its lifetime, no further 387 * locking but only correct reference counting is required. 388 * 389 * Returns: 390 * Zero on success, error code on failure. 391 */ 392 int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb, 393 const struct drm_framebuffer_funcs *funcs) 394 { 395 int ret; 396 397 mutex_lock(&dev->mode_config.fb_lock); 398 kref_init(&fb->refcount); 399 INIT_LIST_HEAD(&fb->filp_head); 400 fb->dev = dev; 401 fb->funcs = funcs; 402 403 ret = drm_mode_object_get(dev, &fb->base, DRM_MODE_OBJECT_FB); 404 if (ret) 405 goto out; 406 407 dev->mode_config.num_fb++; 408 list_add(&fb->head, &dev->mode_config.fb_list); 409 out: 410 mutex_unlock(&dev->mode_config.fb_lock); 411 412 return 0; 413 } 414 EXPORT_SYMBOL(drm_framebuffer_init); 415 416 /* dev->mode_config.fb_lock must be held! */ 417 static void __drm_framebuffer_unregister(struct drm_device *dev, 418 struct drm_framebuffer *fb) 419 { 420 mutex_lock(&dev->mode_config.idr_mutex); 421 idr_remove(&dev->mode_config.crtc_idr, fb->base.id); 422 mutex_unlock(&dev->mode_config.idr_mutex); 423 424 fb->base.id = 0; 425 } 426 427 static void drm_framebuffer_free(struct kref *kref) 428 { 429 struct drm_framebuffer *fb = 430 container_of(kref, struct drm_framebuffer, refcount); 431 struct drm_device *dev = fb->dev; 432 433 /* 434 * The lookup idr holds a weak reference, which has not necessarily been 435 * removed at this point. Check for that. 436 */ 437 mutex_lock(&dev->mode_config.fb_lock); 438 if (fb->base.id) { 439 /* Mark fb as reaped and drop idr ref. */ 440 __drm_framebuffer_unregister(dev, fb); 441 } 442 mutex_unlock(&dev->mode_config.fb_lock); 443 444 fb->funcs->destroy(fb); 445 } 446 447 static struct drm_framebuffer *__drm_framebuffer_lookup(struct drm_device *dev, 448 uint32_t id) 449 { 450 struct drm_mode_object *obj = NULL; 451 struct drm_framebuffer *fb; 452 453 mutex_lock(&dev->mode_config.idr_mutex); 454 obj = idr_find(&dev->mode_config.crtc_idr, id); 455 if (!obj || (obj->type != DRM_MODE_OBJECT_FB) || (obj->id != id)) 456 fb = NULL; 457 else 458 fb = obj_to_fb(obj); 459 mutex_unlock(&dev->mode_config.idr_mutex); 460 461 return fb; 462 } 463 464 /** 465 * drm_framebuffer_lookup - look up a drm framebuffer and grab a reference 466 * @dev: drm device 467 * @id: id of the fb object 468 * 469 * If successful, this grabs an additional reference to the framebuffer - 470 * callers need to make sure to eventually unreference the returned framebuffer 471 * again, using @drm_framebuffer_unreference. 472 */ 473 struct drm_framebuffer *drm_framebuffer_lookup(struct drm_device *dev, 474 uint32_t id) 475 { 476 struct drm_framebuffer *fb; 477 478 mutex_lock(&dev->mode_config.fb_lock); 479 fb = __drm_framebuffer_lookup(dev, id); 480 if (fb) { 481 if (!kref_get_unless_zero(&fb->refcount)) 482 fb = NULL; 483 } 484 mutex_unlock(&dev->mode_config.fb_lock); 485 486 return fb; 487 } 488 EXPORT_SYMBOL(drm_framebuffer_lookup); 489 490 /** 491 * drm_framebuffer_unreference - unref a framebuffer 492 * @fb: framebuffer to unref 493 * 494 * This functions decrements the fb's refcount and frees it if it drops to zero. 495 */ 496 void drm_framebuffer_unreference(struct drm_framebuffer *fb) 497 { 498 DRM_DEBUG("%p: FB ID: %d (%d)\n", fb, fb->base.id, atomic_read(&fb->refcount.refcount)); 499 kref_put(&fb->refcount, drm_framebuffer_free); 500 } 501 EXPORT_SYMBOL(drm_framebuffer_unreference); 502 503 /** 504 * drm_framebuffer_reference - incr the fb refcnt 505 * @fb: framebuffer 506 * 507 * This functions increments the fb's refcount. 508 */ 509 void drm_framebuffer_reference(struct drm_framebuffer *fb) 510 { 511 DRM_DEBUG("%p: FB ID: %d (%d)\n", fb, fb->base.id, atomic_read(&fb->refcount.refcount)); 512 kref_get(&fb->refcount); 513 } 514 EXPORT_SYMBOL(drm_framebuffer_reference); 515 516 static void drm_framebuffer_free_bug(struct kref *kref) 517 { 518 BUG(); 519 } 520 521 static void __drm_framebuffer_unreference(struct drm_framebuffer *fb) 522 { 523 DRM_DEBUG("%p: FB ID: %d (%d)\n", fb, fb->base.id, atomic_read(&fb->refcount.refcount)); 524 kref_put(&fb->refcount, drm_framebuffer_free_bug); 525 } 526 527 /** 528 * drm_framebuffer_unregister_private - unregister a private fb from the lookup idr 529 * @fb: fb to unregister 530 * 531 * Drivers need to call this when cleaning up driver-private framebuffers, e.g. 532 * those used for fbdev. Note that the caller must hold a reference of it's own, 533 * i.e. the object may not be destroyed through this call (since it'll lead to a 534 * locking inversion). 535 */ 536 void drm_framebuffer_unregister_private(struct drm_framebuffer *fb) 537 { 538 struct drm_device *dev = fb->dev; 539 540 mutex_lock(&dev->mode_config.fb_lock); 541 /* Mark fb as reaped and drop idr ref. */ 542 __drm_framebuffer_unregister(dev, fb); 543 mutex_unlock(&dev->mode_config.fb_lock); 544 } 545 EXPORT_SYMBOL(drm_framebuffer_unregister_private); 546 547 /** 548 * drm_framebuffer_cleanup - remove a framebuffer object 549 * @fb: framebuffer to remove 550 * 551 * Cleanup framebuffer. This function is intended to be used from the drivers 552 * ->destroy callback. It can also be used to clean up driver private 553 * framebuffers embedded into a larger structure. 554 * 555 * Note that this function does not remove the fb from active usuage - if it is 556 * still used anywhere, hilarity can ensue since userspace could call getfb on 557 * the id and get back -EINVAL. Obviously no concern at driver unload time. 558 * 559 * Also, the framebuffer will not be removed from the lookup idr - for 560 * user-created framebuffers this will happen in in the rmfb ioctl. For 561 * driver-private objects (e.g. for fbdev) drivers need to explicitly call 562 * drm_framebuffer_unregister_private. 563 */ 564 void drm_framebuffer_cleanup(struct drm_framebuffer *fb) 565 { 566 struct drm_device *dev = fb->dev; 567 568 mutex_lock(&dev->mode_config.fb_lock); 569 list_del(&fb->head); 570 dev->mode_config.num_fb--; 571 mutex_unlock(&dev->mode_config.fb_lock); 572 } 573 EXPORT_SYMBOL(drm_framebuffer_cleanup); 574 575 /** 576 * drm_framebuffer_remove - remove and unreference a framebuffer object 577 * @fb: framebuffer to remove 578 * 579 * Scans all the CRTCs and planes in @dev's mode_config. If they're 580 * using @fb, removes it, setting it to NULL. Then drops the reference to the 581 * passed-in framebuffer. Might take the modeset locks. 582 * 583 * Note that this function optimizes the cleanup away if the caller holds the 584 * last reference to the framebuffer. It is also guaranteed to not take the 585 * modeset locks in this case. 586 */ 587 void drm_framebuffer_remove(struct drm_framebuffer *fb) 588 { 589 struct drm_device *dev = fb->dev; 590 struct drm_crtc *crtc; 591 struct drm_plane *plane; 592 struct drm_mode_set set; 593 int ret; 594 595 WARN_ON(!list_empty(&fb->filp_head)); 596 597 /* 598 * drm ABI mandates that we remove any deleted framebuffers from active 599 * useage. But since most sane clients only remove framebuffers they no 600 * longer need, try to optimize this away. 601 * 602 * Since we're holding a reference ourselves, observing a refcount of 1 603 * means that we're the last holder and can skip it. Also, the refcount 604 * can never increase from 1 again, so we don't need any barriers or 605 * locks. 606 * 607 * Note that userspace could try to race with use and instate a new 608 * usage _after_ we've cleared all current ones. End result will be an 609 * in-use fb with fb-id == 0. Userspace is allowed to shoot its own foot 610 * in this manner. 611 */ 612 if (atomic_read(&fb->refcount.refcount) > 1) { 613 drm_modeset_lock_all(dev); 614 /* remove from any CRTC */ 615 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) { 616 if (crtc->primary->fb == fb) { 617 /* should turn off the crtc */ 618 memset(&set, 0, sizeof(struct drm_mode_set)); 619 set.crtc = crtc; 620 set.fb = NULL; 621 ret = drm_mode_set_config_internal(&set); 622 if (ret) 623 DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc); 624 } 625 } 626 627 list_for_each_entry(plane, &dev->mode_config.plane_list, head) { 628 if (plane->fb == fb) 629 drm_plane_force_disable(plane); 630 } 631 drm_modeset_unlock_all(dev); 632 } 633 634 drm_framebuffer_unreference(fb); 635 } 636 EXPORT_SYMBOL(drm_framebuffer_remove); 637 638 DEFINE_WW_CLASS(crtc_ww_class); 639 640 /** 641 * drm_crtc_init_with_planes - Initialise a new CRTC object with 642 * specified primary and cursor planes. 643 * @dev: DRM device 644 * @crtc: CRTC object to init 645 * @primary: Primary plane for CRTC 646 * @cursor: Cursor plane for CRTC 647 * @funcs: callbacks for the new CRTC 648 * 649 * Inits a new object created as base part of a driver crtc object. 650 * 651 * Returns: 652 * Zero on success, error code on failure. 653 */ 654 int drm_crtc_init_with_planes(struct drm_device *dev, struct drm_crtc *crtc, 655 struct drm_plane *primary, 656 struct drm_plane *cursor, 657 const struct drm_crtc_funcs *funcs) 658 { 659 struct drm_mode_config *config = &dev->mode_config; 660 int ret; 661 662 crtc->dev = dev; 663 crtc->funcs = funcs; 664 crtc->invert_dimensions = false; 665 666 drm_modeset_lock_init(&crtc->mutex); 667 ret = drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC); 668 if (ret) 669 goto out; 670 671 crtc->base.properties = &crtc->properties; 672 673 list_add_tail(&crtc->head, &config->crtc_list); 674 config->num_crtc++; 675 676 crtc->primary = primary; 677 crtc->cursor = cursor; 678 if (primary) 679 primary->possible_crtcs = 1 << drm_crtc_index(crtc); 680 if (cursor) 681 cursor->possible_crtcs = 1 << drm_crtc_index(crtc); 682 683 out: 684 685 return ret; 686 } 687 EXPORT_SYMBOL(drm_crtc_init_with_planes); 688 689 /** 690 * drm_crtc_cleanup - Clean up the core crtc usage 691 * @crtc: CRTC to cleanup 692 * 693 * This function cleans up @crtc and removes it from the DRM mode setting 694 * core. Note that the function does *not* free the crtc structure itself, 695 * this is the responsibility of the caller. 696 */ 697 void drm_crtc_cleanup(struct drm_crtc *crtc) 698 { 699 struct drm_device *dev = crtc->dev; 700 701 kfree(crtc->gamma_store); 702 crtc->gamma_store = NULL; 703 704 drm_modeset_lock_fini(&crtc->mutex); 705 706 drm_mode_object_put(dev, &crtc->base); 707 list_del(&crtc->head); 708 dev->mode_config.num_crtc--; 709 } 710 EXPORT_SYMBOL(drm_crtc_cleanup); 711 712 /** 713 * drm_crtc_index - find the index of a registered CRTC 714 * @crtc: CRTC to find index for 715 * 716 * Given a registered CRTC, return the index of that CRTC within a DRM 717 * device's list of CRTCs. 718 */ 719 unsigned int drm_crtc_index(struct drm_crtc *crtc) 720 { 721 unsigned int index = 0; 722 struct drm_crtc *tmp; 723 724 list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head) { 725 if (tmp == crtc) 726 return index; 727 728 index++; 729 } 730 731 BUG(); 732 } 733 EXPORT_SYMBOL(drm_crtc_index); 734 735 /* 736 * drm_mode_remove - remove and free a mode 737 * @connector: connector list to modify 738 * @mode: mode to remove 739 * 740 * Remove @mode from @connector's mode list, then free it. 741 */ 742 static void drm_mode_remove(struct drm_connector *connector, 743 struct drm_display_mode *mode) 744 { 745 list_del(&mode->head); 746 drm_mode_destroy(connector->dev, mode); 747 } 748 749 /** 750 * drm_connector_get_cmdline_mode - reads the user's cmdline mode 751 * @connector: connector to quwery 752 * @mode: returned mode 753 * 754 * The kernel supports per-connector configration of its consoles through 755 * use of the video= parameter. This function parses that option and 756 * extracts the user's specified mode (or enable/disable status) for a 757 * particular connector. This is typically only used during the early fbdev 758 * setup. 759 */ 760 static void drm_connector_get_cmdline_mode(struct drm_connector *connector) 761 { 762 struct drm_cmdline_mode *mode = &connector->cmdline_mode; 763 char *option = NULL; 764 765 if (fb_get_options(connector->name, &option)) 766 return; 767 768 if (!drm_mode_parse_command_line_for_connector(option, 769 connector, 770 mode)) 771 return; 772 773 if (mode->force) { 774 const char *s; 775 776 switch (mode->force) { 777 case DRM_FORCE_OFF: 778 s = "OFF"; 779 break; 780 case DRM_FORCE_ON_DIGITAL: 781 s = "ON - dig"; 782 break; 783 default: 784 case DRM_FORCE_ON: 785 s = "ON"; 786 break; 787 } 788 789 DRM_INFO("forcing %s connector %s\n", connector->name, s); 790 connector->force = mode->force; 791 } 792 793 DRM_DEBUG_KMS("cmdline mode for connector %s %dx%d@%dHz%s%s%s\n", 794 connector->name, 795 mode->xres, mode->yres, 796 mode->refresh_specified ? mode->refresh : 60, 797 mode->rb ? " reduced blanking" : "", 798 mode->margins ? " with margins" : "", 799 mode->interlace ? " interlaced" : ""); 800 } 801 802 /** 803 * drm_connector_init - Init a preallocated connector 804 * @dev: DRM device 805 * @connector: the connector to init 806 * @funcs: callbacks for this connector 807 * @connector_type: user visible type of the connector 808 * 809 * Initialises a preallocated connector. Connectors should be 810 * subclassed as part of driver connector objects. 811 * 812 * Returns: 813 * Zero on success, error code on failure. 814 */ 815 int drm_connector_init(struct drm_device *dev, 816 struct drm_connector *connector, 817 const struct drm_connector_funcs *funcs, 818 int connector_type) 819 { 820 int ret; 821 822 drm_modeset_lock_all(dev); 823 824 ret = drm_mode_object_get(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR); 825 if (ret) 826 goto out_unlock; 827 828 connector->base.properties = &connector->properties; 829 connector->dev = dev; 830 connector->funcs = funcs; 831 connector->connector_type = connector_type; 832 connector->connector_type_id = 833 ++drm_connector_enum_list[connector_type].count; /* TODO */ 834 if (connector->connector_type_id < 0) { 835 ret = connector->connector_type_id; 836 goto out_put; 837 } 838 connector->name = 839 drm_asprintf(GFP_KERNEL, "%s-%d", 840 drm_connector_enum_list[connector_type].name, 841 connector->connector_type_id); 842 if (!connector->name) { 843 ret = -ENOMEM; 844 goto out_put; 845 } 846 847 INIT_LIST_HEAD(&connector->probed_modes); 848 INIT_LIST_HEAD(&connector->modes); 849 connector->edid_blob_ptr = NULL; 850 connector->status = connector_status_unknown; 851 852 drm_connector_get_cmdline_mode(connector); 853 854 list_add_tail(&connector->head, &dev->mode_config.connector_list); 855 dev->mode_config.num_connector++; 856 857 if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL) 858 drm_object_attach_property(&connector->base, 859 dev->mode_config.edid_property, 860 0); 861 862 drm_object_attach_property(&connector->base, 863 dev->mode_config.dpms_property, 0); 864 865 connector->debugfs_entry = NULL; 866 867 out_put: 868 if (ret) 869 drm_mode_object_put(dev, &connector->base); 870 871 out_unlock: 872 drm_modeset_unlock_all(dev); 873 874 return ret; 875 } 876 EXPORT_SYMBOL(drm_connector_init); 877 878 /** 879 * drm_connector_cleanup - cleans up an initialised connector 880 * @connector: connector to cleanup 881 * 882 * Cleans up the connector but doesn't free the object. 883 */ 884 void drm_connector_cleanup(struct drm_connector *connector) 885 { 886 struct drm_device *dev = connector->dev; 887 struct drm_display_mode *mode, *t; 888 889 list_for_each_entry_safe(mode, t, &connector->probed_modes, head) 890 drm_mode_remove(connector, mode); 891 892 list_for_each_entry_safe(mode, t, &connector->modes, head) 893 drm_mode_remove(connector, mode); 894 895 drm_mode_object_put(dev, &connector->base); 896 kfree(connector->name); 897 connector->name = NULL; 898 list_del(&connector->head); 899 dev->mode_config.num_connector--; 900 } 901 EXPORT_SYMBOL(drm_connector_cleanup); 902 903 /** 904 * drm_connector_index - find the index of a registered connector 905 * @connector: connector to find index for 906 * 907 * Given a registered connector, return the index of that connector within a DRM 908 * device's list of connectors. 909 */ 910 unsigned int drm_connector_index(struct drm_connector *connector) 911 { 912 unsigned int index = 0; 913 struct drm_connector *tmp; 914 915 list_for_each_entry(tmp, &connector->dev->mode_config.connector_list, head) { 916 if (tmp == connector) 917 return index; 918 919 index++; 920 } 921 922 BUG(); 923 } 924 EXPORT_SYMBOL(drm_connector_index); 925 926 /** 927 * drm_connector_register - register a connector 928 * @connector: the connector to register 929 * 930 * Register userspace interfaces for a connector 931 * 932 * Returns: 933 * Zero on success, error code on failure. 934 */ 935 int drm_connector_register(struct drm_connector *connector) 936 { 937 int ret; 938 939 ret = drm_sysfs_connector_add(connector); 940 if (ret) 941 return ret; 942 943 #if 0 944 ret = drm_debugfs_connector_add(connector); 945 #endif 946 if (ret) { 947 drm_sysfs_connector_remove(connector); 948 return ret; 949 } 950 951 return 0; 952 } 953 EXPORT_SYMBOL(drm_connector_register); 954 955 /** 956 * drm_connector_unregister - unregister a connector 957 * @connector: the connector to unregister 958 * 959 * Unregister userspace interfaces for a connector 960 */ 961 void drm_connector_unregister(struct drm_connector *connector) 962 { 963 drm_sysfs_connector_remove(connector); 964 #if 0 965 drm_debugfs_connector_remove(connector); 966 #endif 967 } 968 EXPORT_SYMBOL(drm_connector_unregister); 969 970 971 /** 972 * drm_connector_unplug_all - unregister connector userspace interfaces 973 * @dev: drm device 974 * 975 * This function unregisters all connector userspace interfaces in sysfs. Should 976 * be call when the device is disconnected, e.g. from an usb driver's 977 * ->disconnect callback. 978 */ 979 void drm_connector_unplug_all(struct drm_device *dev) 980 { 981 struct drm_connector *connector; 982 983 /* taking the mode config mutex ends up in a clash with sysfs */ 984 list_for_each_entry(connector, &dev->mode_config.connector_list, head) 985 drm_connector_unregister(connector); 986 987 } 988 EXPORT_SYMBOL(drm_connector_unplug_all); 989 990 /** 991 * drm_bridge_init - initialize a drm transcoder/bridge 992 * @dev: drm device 993 * @bridge: transcoder/bridge to set up 994 * @funcs: bridge function table 995 * 996 * Initialises a preallocated bridge. Bridges should be 997 * subclassed as part of driver connector objects. 998 * 999 * Returns: 1000 * Zero on success, error code on failure. 1001 */ 1002 int drm_bridge_init(struct drm_device *dev, struct drm_bridge *bridge, 1003 const struct drm_bridge_funcs *funcs) 1004 { 1005 int ret; 1006 1007 drm_modeset_lock_all(dev); 1008 1009 ret = drm_mode_object_get(dev, &bridge->base, DRM_MODE_OBJECT_BRIDGE); 1010 if (ret) 1011 goto out; 1012 1013 bridge->dev = dev; 1014 bridge->funcs = funcs; 1015 1016 list_add_tail(&bridge->head, &dev->mode_config.bridge_list); 1017 dev->mode_config.num_bridge++; 1018 1019 out: 1020 drm_modeset_unlock_all(dev); 1021 return ret; 1022 } 1023 EXPORT_SYMBOL(drm_bridge_init); 1024 1025 /** 1026 * drm_bridge_cleanup - cleans up an initialised bridge 1027 * @bridge: bridge to cleanup 1028 * 1029 * Cleans up the bridge but doesn't free the object. 1030 */ 1031 void drm_bridge_cleanup(struct drm_bridge *bridge) 1032 { 1033 struct drm_device *dev = bridge->dev; 1034 1035 drm_modeset_lock_all(dev); 1036 drm_mode_object_put(dev, &bridge->base); 1037 list_del(&bridge->head); 1038 dev->mode_config.num_bridge--; 1039 drm_modeset_unlock_all(dev); 1040 } 1041 EXPORT_SYMBOL(drm_bridge_cleanup); 1042 1043 /** 1044 * drm_encoder_init - Init a preallocated encoder 1045 * @dev: drm device 1046 * @encoder: the encoder to init 1047 * @funcs: callbacks for this encoder 1048 * @encoder_type: user visible type of the encoder 1049 * 1050 * Initialises a preallocated encoder. Encoder should be 1051 * subclassed as part of driver encoder objects. 1052 * 1053 * Returns: 1054 * Zero on success, error code on failure. 1055 */ 1056 int drm_encoder_init(struct drm_device *dev, 1057 struct drm_encoder *encoder, 1058 const struct drm_encoder_funcs *funcs, 1059 int encoder_type) 1060 { 1061 int ret; 1062 1063 drm_modeset_lock_all(dev); 1064 1065 ret = drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER); 1066 if (ret) 1067 goto out_unlock; 1068 1069 encoder->dev = dev; 1070 encoder->encoder_type = encoder_type; 1071 encoder->funcs = funcs; 1072 encoder->name = drm_asprintf(GFP_KERNEL, "%s-%d", 1073 drm_encoder_enum_list[encoder_type].name, 1074 encoder->base.id); 1075 if (!encoder->name) { 1076 ret = -ENOMEM; 1077 goto out_put; 1078 } 1079 1080 list_add_tail(&encoder->head, &dev->mode_config.encoder_list); 1081 dev->mode_config.num_encoder++; 1082 1083 out_put: 1084 if (ret) 1085 drm_mode_object_put(dev, &encoder->base); 1086 1087 out_unlock: 1088 drm_modeset_unlock_all(dev); 1089 1090 return ret; 1091 } 1092 EXPORT_SYMBOL(drm_encoder_init); 1093 1094 /** 1095 * drm_encoder_cleanup - cleans up an initialised encoder 1096 * @encoder: encoder to cleanup 1097 * 1098 * Cleans up the encoder but doesn't free the object. 1099 */ 1100 void drm_encoder_cleanup(struct drm_encoder *encoder) 1101 { 1102 struct drm_device *dev = encoder->dev; 1103 drm_modeset_lock_all(dev); 1104 drm_mode_object_put(dev, &encoder->base); 1105 kfree(encoder->name); 1106 encoder->name = NULL; 1107 list_del(&encoder->head); 1108 dev->mode_config.num_encoder--; 1109 drm_modeset_unlock_all(dev); 1110 } 1111 EXPORT_SYMBOL(drm_encoder_cleanup); 1112 1113 /** 1114 * drm_universal_plane_init - Initialize a new universal plane object 1115 * @dev: DRM device 1116 * @plane: plane object to init 1117 * @possible_crtcs: bitmask of possible CRTCs 1118 * @funcs: callbacks for the new plane 1119 * @formats: array of supported formats (%DRM_FORMAT_*) 1120 * @format_count: number of elements in @formats 1121 * @type: type of plane (overlay, primary, cursor) 1122 * 1123 * Initializes a plane object of type @type. 1124 * 1125 * Returns: 1126 * Zero on success, error code on failure. 1127 */ 1128 int drm_universal_plane_init(struct drm_device *dev, struct drm_plane *plane, 1129 unsigned long possible_crtcs, 1130 const struct drm_plane_funcs *funcs, 1131 const uint32_t *formats, uint32_t format_count, 1132 enum drm_plane_type type) 1133 { 1134 int ret; 1135 1136 drm_modeset_lock_all(dev); 1137 1138 ret = drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE); 1139 if (ret) 1140 goto out; 1141 1142 plane->base.properties = &plane->properties; 1143 plane->dev = dev; 1144 plane->funcs = funcs; 1145 plane->format_types = kmalloc(sizeof(uint32_t) * format_count, 1146 M_DRM, M_WAITOK); 1147 if (!plane->format_types) { 1148 DRM_DEBUG_KMS("out of memory when allocating plane\n"); 1149 drm_mode_object_put(dev, &plane->base); 1150 ret = -ENOMEM; 1151 goto out; 1152 } 1153 1154 memcpy(plane->format_types, formats, format_count * sizeof(uint32_t)); 1155 plane->format_count = format_count; 1156 plane->possible_crtcs = possible_crtcs; 1157 plane->type = type; 1158 1159 list_add_tail(&plane->head, &dev->mode_config.plane_list); 1160 dev->mode_config.num_total_plane++; 1161 if (plane->type == DRM_PLANE_TYPE_OVERLAY) 1162 dev->mode_config.num_overlay_plane++; 1163 1164 drm_object_attach_property(&plane->base, 1165 dev->mode_config.plane_type_property, 1166 plane->type); 1167 1168 out: 1169 drm_modeset_unlock_all(dev); 1170 1171 return ret; 1172 } 1173 EXPORT_SYMBOL(drm_universal_plane_init); 1174 1175 /** 1176 * drm_plane_init - Initialize a legacy plane 1177 * @dev: DRM device 1178 * @plane: plane object to init 1179 * @possible_crtcs: bitmask of possible CRTCs 1180 * @funcs: callbacks for the new plane 1181 * @formats: array of supported formats (%DRM_FORMAT_*) 1182 * @format_count: number of elements in @formats 1183 * @is_primary: plane type (primary vs overlay) 1184 * 1185 * Legacy API to initialize a DRM plane. 1186 * 1187 * New drivers should call drm_universal_plane_init() instead. 1188 * 1189 * Returns: 1190 * Zero on success, error code on failure. 1191 */ 1192 int drm_plane_init(struct drm_device *dev, struct drm_plane *plane, 1193 unsigned long possible_crtcs, 1194 const struct drm_plane_funcs *funcs, 1195 const uint32_t *formats, uint32_t format_count, 1196 bool is_primary) 1197 { 1198 enum drm_plane_type type; 1199 1200 type = is_primary ? DRM_PLANE_TYPE_PRIMARY : DRM_PLANE_TYPE_OVERLAY; 1201 return drm_universal_plane_init(dev, plane, possible_crtcs, funcs, 1202 formats, format_count, type); 1203 } 1204 EXPORT_SYMBOL(drm_plane_init); 1205 1206 /** 1207 * drm_plane_cleanup - Clean up the core plane usage 1208 * @plane: plane to cleanup 1209 * 1210 * This function cleans up @plane and removes it from the DRM mode setting 1211 * core. Note that the function does *not* free the plane structure itself, 1212 * this is the responsibility of the caller. 1213 */ 1214 void drm_plane_cleanup(struct drm_plane *plane) 1215 { 1216 struct drm_device *dev = plane->dev; 1217 1218 drm_modeset_lock_all(dev); 1219 kfree(plane->format_types); 1220 drm_mode_object_put(dev, &plane->base); 1221 1222 BUG_ON(list_empty(&plane->head)); 1223 1224 list_del(&plane->head); 1225 dev->mode_config.num_total_plane--; 1226 if (plane->type == DRM_PLANE_TYPE_OVERLAY) 1227 dev->mode_config.num_overlay_plane--; 1228 drm_modeset_unlock_all(dev); 1229 } 1230 EXPORT_SYMBOL(drm_plane_cleanup); 1231 1232 /** 1233 * drm_plane_index - find the index of a registered plane 1234 * @plane: plane to find index for 1235 * 1236 * Given a registered plane, return the index of that CRTC within a DRM 1237 * device's list of planes. 1238 */ 1239 unsigned int drm_plane_index(struct drm_plane *plane) 1240 { 1241 unsigned int index = 0; 1242 struct drm_plane *tmp; 1243 1244 list_for_each_entry(tmp, &plane->dev->mode_config.plane_list, head) { 1245 if (tmp == plane) 1246 return index; 1247 1248 index++; 1249 } 1250 1251 BUG(); 1252 } 1253 EXPORT_SYMBOL(drm_plane_index); 1254 1255 /** 1256 * drm_plane_force_disable - Forcibly disable a plane 1257 * @plane: plane to disable 1258 * 1259 * Forces the plane to be disabled. 1260 * 1261 * Used when the plane's current framebuffer is destroyed, 1262 * and when restoring fbdev mode. 1263 */ 1264 void drm_plane_force_disable(struct drm_plane *plane) 1265 { 1266 int ret; 1267 1268 if (!plane->fb) 1269 return; 1270 1271 plane->old_fb = plane->fb; 1272 ret = plane->funcs->disable_plane(plane); 1273 if (ret) { 1274 DRM_ERROR("failed to disable plane with busy fb\n"); 1275 plane->old_fb = NULL; 1276 return; 1277 } 1278 /* disconnect the plane from the fb and crtc: */ 1279 __drm_framebuffer_unreference(plane->old_fb); 1280 plane->old_fb = NULL; 1281 plane->fb = NULL; 1282 plane->crtc = NULL; 1283 } 1284 EXPORT_SYMBOL(drm_plane_force_disable); 1285 1286 static int drm_mode_create_standard_connector_properties(struct drm_device *dev) 1287 { 1288 struct drm_property *edid; 1289 struct drm_property *dpms; 1290 struct drm_property *dev_path; 1291 1292 /* 1293 * Standard properties (apply to all connectors) 1294 */ 1295 edid = drm_property_create(dev, DRM_MODE_PROP_BLOB | 1296 DRM_MODE_PROP_IMMUTABLE, 1297 "EDID", 0); 1298 dev->mode_config.edid_property = edid; 1299 1300 dpms = drm_property_create_enum(dev, 0, 1301 "DPMS", drm_dpms_enum_list, 1302 ARRAY_SIZE(drm_dpms_enum_list)); 1303 dev->mode_config.dpms_property = dpms; 1304 1305 dev_path = drm_property_create(dev, 1306 DRM_MODE_PROP_BLOB | 1307 DRM_MODE_PROP_IMMUTABLE, 1308 "PATH", 0); 1309 dev->mode_config.path_property = dev_path; 1310 1311 return 0; 1312 } 1313 1314 static int drm_mode_create_standard_plane_properties(struct drm_device *dev) 1315 { 1316 struct drm_property *type; 1317 1318 /* 1319 * Standard properties (apply to all planes) 1320 */ 1321 type = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE, 1322 "type", drm_plane_type_enum_list, 1323 ARRAY_SIZE(drm_plane_type_enum_list)); 1324 dev->mode_config.plane_type_property = type; 1325 1326 return 0; 1327 } 1328 1329 /** 1330 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties 1331 * @dev: DRM device 1332 * 1333 * Called by a driver the first time a DVI-I connector is made. 1334 */ 1335 int drm_mode_create_dvi_i_properties(struct drm_device *dev) 1336 { 1337 struct drm_property *dvi_i_selector; 1338 struct drm_property *dvi_i_subconnector; 1339 1340 if (dev->mode_config.dvi_i_select_subconnector_property) 1341 return 0; 1342 1343 dvi_i_selector = 1344 drm_property_create_enum(dev, 0, 1345 "select subconnector", 1346 drm_dvi_i_select_enum_list, 1347 ARRAY_SIZE(drm_dvi_i_select_enum_list)); 1348 dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector; 1349 1350 dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE, 1351 "subconnector", 1352 drm_dvi_i_subconnector_enum_list, 1353 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list)); 1354 dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector; 1355 1356 return 0; 1357 } 1358 EXPORT_SYMBOL(drm_mode_create_dvi_i_properties); 1359 1360 /** 1361 * drm_create_tv_properties - create TV specific connector properties 1362 * @dev: DRM device 1363 * @num_modes: number of different TV formats (modes) supported 1364 * @modes: array of pointers to strings containing name of each format 1365 * 1366 * Called by a driver's TV initialization routine, this function creates 1367 * the TV specific connector properties for a given device. Caller is 1368 * responsible for allocating a list of format names and passing them to 1369 * this routine. 1370 */ 1371 int drm_mode_create_tv_properties(struct drm_device *dev, int num_modes, 1372 char *modes[]) 1373 { 1374 struct drm_property *tv_selector; 1375 struct drm_property *tv_subconnector; 1376 int i; 1377 1378 if (dev->mode_config.tv_select_subconnector_property) 1379 return 0; 1380 1381 /* 1382 * Basic connector properties 1383 */ 1384 tv_selector = drm_property_create_enum(dev, 0, 1385 "select subconnector", 1386 drm_tv_select_enum_list, 1387 ARRAY_SIZE(drm_tv_select_enum_list)); 1388 dev->mode_config.tv_select_subconnector_property = tv_selector; 1389 1390 tv_subconnector = 1391 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE, 1392 "subconnector", 1393 drm_tv_subconnector_enum_list, 1394 ARRAY_SIZE(drm_tv_subconnector_enum_list)); 1395 dev->mode_config.tv_subconnector_property = tv_subconnector; 1396 1397 /* 1398 * Other, TV specific properties: margins & TV modes. 1399 */ 1400 dev->mode_config.tv_left_margin_property = 1401 drm_property_create_range(dev, 0, "left margin", 0, 100); 1402 1403 dev->mode_config.tv_right_margin_property = 1404 drm_property_create_range(dev, 0, "right margin", 0, 100); 1405 1406 dev->mode_config.tv_top_margin_property = 1407 drm_property_create_range(dev, 0, "top margin", 0, 100); 1408 1409 dev->mode_config.tv_bottom_margin_property = 1410 drm_property_create_range(dev, 0, "bottom margin", 0, 100); 1411 1412 dev->mode_config.tv_mode_property = 1413 drm_property_create(dev, DRM_MODE_PROP_ENUM, 1414 "mode", num_modes); 1415 for (i = 0; i < num_modes; i++) 1416 drm_property_add_enum(dev->mode_config.tv_mode_property, i, 1417 i, modes[i]); 1418 1419 dev->mode_config.tv_brightness_property = 1420 drm_property_create_range(dev, 0, "brightness", 0, 100); 1421 1422 dev->mode_config.tv_contrast_property = 1423 drm_property_create_range(dev, 0, "contrast", 0, 100); 1424 1425 dev->mode_config.tv_flicker_reduction_property = 1426 drm_property_create_range(dev, 0, "flicker reduction", 0, 100); 1427 1428 dev->mode_config.tv_overscan_property = 1429 drm_property_create_range(dev, 0, "overscan", 0, 100); 1430 1431 dev->mode_config.tv_saturation_property = 1432 drm_property_create_range(dev, 0, "saturation", 0, 100); 1433 1434 dev->mode_config.tv_hue_property = 1435 drm_property_create_range(dev, 0, "hue", 0, 100); 1436 1437 return 0; 1438 } 1439 EXPORT_SYMBOL(drm_mode_create_tv_properties); 1440 1441 /** 1442 * drm_mode_create_scaling_mode_property - create scaling mode property 1443 * @dev: DRM device 1444 * 1445 * Called by a driver the first time it's needed, must be attached to desired 1446 * connectors. 1447 */ 1448 int drm_mode_create_scaling_mode_property(struct drm_device *dev) 1449 { 1450 struct drm_property *scaling_mode; 1451 1452 if (dev->mode_config.scaling_mode_property) 1453 return 0; 1454 1455 scaling_mode = 1456 drm_property_create_enum(dev, 0, "scaling mode", 1457 drm_scaling_mode_enum_list, 1458 ARRAY_SIZE(drm_scaling_mode_enum_list)); 1459 1460 dev->mode_config.scaling_mode_property = scaling_mode; 1461 1462 return 0; 1463 } 1464 EXPORT_SYMBOL(drm_mode_create_scaling_mode_property); 1465 1466 /** 1467 * drm_mode_create_aspect_ratio_property - create aspect ratio property 1468 * @dev: DRM device 1469 * 1470 * Called by a driver the first time it's needed, must be attached to desired 1471 * connectors. 1472 * 1473 * Returns: 1474 * Zero on success, errno on failure. 1475 */ 1476 int drm_mode_create_aspect_ratio_property(struct drm_device *dev) 1477 { 1478 if (dev->mode_config.aspect_ratio_property) 1479 return 0; 1480 1481 dev->mode_config.aspect_ratio_property = 1482 drm_property_create_enum(dev, 0, "aspect ratio", 1483 drm_aspect_ratio_enum_list, 1484 ARRAY_SIZE(drm_aspect_ratio_enum_list)); 1485 1486 if (dev->mode_config.aspect_ratio_property == NULL) 1487 return -ENOMEM; 1488 1489 return 0; 1490 } 1491 EXPORT_SYMBOL(drm_mode_create_aspect_ratio_property); 1492 1493 /** 1494 * drm_mode_create_dirty_property - create dirty property 1495 * @dev: DRM device 1496 * 1497 * Called by a driver the first time it's needed, must be attached to desired 1498 * connectors. 1499 */ 1500 int drm_mode_create_dirty_info_property(struct drm_device *dev) 1501 { 1502 struct drm_property *dirty_info; 1503 1504 if (dev->mode_config.dirty_info_property) 1505 return 0; 1506 1507 dirty_info = 1508 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE, 1509 "dirty", 1510 drm_dirty_info_enum_list, 1511 ARRAY_SIZE(drm_dirty_info_enum_list)); 1512 dev->mode_config.dirty_info_property = dirty_info; 1513 1514 return 0; 1515 } 1516 EXPORT_SYMBOL(drm_mode_create_dirty_info_property); 1517 1518 static int drm_mode_group_init(struct drm_device *dev, struct drm_mode_group *group) 1519 { 1520 uint32_t total_objects = 0; 1521 1522 total_objects += dev->mode_config.num_crtc; 1523 total_objects += dev->mode_config.num_connector; 1524 total_objects += dev->mode_config.num_encoder; 1525 total_objects += dev->mode_config.num_bridge; 1526 1527 group->id_list = kzalloc(total_objects * sizeof(uint32_t), GFP_KERNEL); 1528 if (!group->id_list) 1529 return -ENOMEM; 1530 1531 group->num_crtcs = 0; 1532 group->num_connectors = 0; 1533 group->num_encoders = 0; 1534 group->num_bridges = 0; 1535 return 0; 1536 } 1537 1538 void drm_mode_group_destroy(struct drm_mode_group *group) 1539 { 1540 kfree(group->id_list); 1541 group->id_list = NULL; 1542 } 1543 1544 /* 1545 * NOTE: Driver's shouldn't ever call drm_mode_group_init_legacy_group - it is 1546 * the drm core's responsibility to set up mode control groups. 1547 */ 1548 int drm_mode_group_init_legacy_group(struct drm_device *dev, 1549 struct drm_mode_group *group) 1550 { 1551 struct drm_crtc *crtc; 1552 struct drm_encoder *encoder; 1553 struct drm_connector *connector; 1554 struct drm_bridge *bridge; 1555 int ret; 1556 1557 if ((ret = drm_mode_group_init(dev, group))) 1558 return ret; 1559 1560 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) 1561 group->id_list[group->num_crtcs++] = crtc->base.id; 1562 1563 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) 1564 group->id_list[group->num_crtcs + group->num_encoders++] = 1565 encoder->base.id; 1566 1567 list_for_each_entry(connector, &dev->mode_config.connector_list, head) 1568 group->id_list[group->num_crtcs + group->num_encoders + 1569 group->num_connectors++] = connector->base.id; 1570 1571 list_for_each_entry(bridge, &dev->mode_config.bridge_list, head) 1572 group->id_list[group->num_crtcs + group->num_encoders + 1573 group->num_connectors + group->num_bridges++] = 1574 bridge->base.id; 1575 1576 return 0; 1577 } 1578 EXPORT_SYMBOL(drm_mode_group_init_legacy_group); 1579 1580 void drm_reinit_primary_mode_group(struct drm_device *dev) 1581 { 1582 drm_modeset_lock_all(dev); 1583 drm_mode_group_destroy(&dev->primary->mode_group); 1584 drm_mode_group_init_legacy_group(dev, &dev->primary->mode_group); 1585 drm_modeset_unlock_all(dev); 1586 } 1587 EXPORT_SYMBOL(drm_reinit_primary_mode_group); 1588 1589 /** 1590 * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo 1591 * @out: drm_mode_modeinfo struct to return to the user 1592 * @in: drm_display_mode to use 1593 * 1594 * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to 1595 * the user. 1596 */ 1597 static void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out, 1598 const struct drm_display_mode *in) 1599 { 1600 WARN(in->hdisplay > USHRT_MAX || in->hsync_start > USHRT_MAX || 1601 in->hsync_end > USHRT_MAX || in->htotal > USHRT_MAX || 1602 in->hskew > USHRT_MAX || in->vdisplay > USHRT_MAX || 1603 in->vsync_start > USHRT_MAX || in->vsync_end > USHRT_MAX || 1604 in->vtotal > USHRT_MAX || in->vscan > USHRT_MAX, 1605 "timing values too large for mode info\n"); 1606 1607 out->clock = in->clock; 1608 out->hdisplay = in->hdisplay; 1609 out->hsync_start = in->hsync_start; 1610 out->hsync_end = in->hsync_end; 1611 out->htotal = in->htotal; 1612 out->hskew = in->hskew; 1613 out->vdisplay = in->vdisplay; 1614 out->vsync_start = in->vsync_start; 1615 out->vsync_end = in->vsync_end; 1616 out->vtotal = in->vtotal; 1617 out->vscan = in->vscan; 1618 out->vrefresh = in->vrefresh; 1619 out->flags = in->flags; 1620 out->type = in->type; 1621 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN); 1622 out->name[DRM_DISPLAY_MODE_LEN-1] = 0; 1623 } 1624 1625 /** 1626 * drm_crtc_convert_umode - convert a modeinfo into a drm_display_mode 1627 * @out: drm_display_mode to return to the user 1628 * @in: drm_mode_modeinfo to use 1629 * 1630 * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to 1631 * the caller. 1632 * 1633 * Returns: 1634 * Zero on success, errno on failure. 1635 */ 1636 static int drm_crtc_convert_umode(struct drm_display_mode *out, 1637 const struct drm_mode_modeinfo *in) 1638 { 1639 if (in->clock > INT_MAX || in->vrefresh > INT_MAX) 1640 return -ERANGE; 1641 1642 if ((in->flags & DRM_MODE_FLAG_3D_MASK) > DRM_MODE_FLAG_3D_MAX) 1643 return -EINVAL; 1644 1645 out->clock = in->clock; 1646 out->hdisplay = in->hdisplay; 1647 out->hsync_start = in->hsync_start; 1648 out->hsync_end = in->hsync_end; 1649 out->htotal = in->htotal; 1650 out->hskew = in->hskew; 1651 out->vdisplay = in->vdisplay; 1652 out->vsync_start = in->vsync_start; 1653 out->vsync_end = in->vsync_end; 1654 out->vtotal = in->vtotal; 1655 out->vscan = in->vscan; 1656 out->vrefresh = in->vrefresh; 1657 out->flags = in->flags; 1658 out->type = in->type; 1659 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN); 1660 out->name[DRM_DISPLAY_MODE_LEN-1] = 0; 1661 1662 return 0; 1663 } 1664 1665 /** 1666 * drm_mode_getresources - get graphics configuration 1667 * @dev: drm device for the ioctl 1668 * @data: data pointer for the ioctl 1669 * @file_priv: drm file for the ioctl call 1670 * 1671 * Construct a set of configuration description structures and return 1672 * them to the user, including CRTC, connector and framebuffer configuration. 1673 * 1674 * Called by the user via ioctl. 1675 * 1676 * Returns: 1677 * Zero on success, errno on failure. 1678 */ 1679 int drm_mode_getresources(struct drm_device *dev, void *data, 1680 struct drm_file *file_priv) 1681 { 1682 struct drm_mode_card_res *card_res = data; 1683 struct list_head *lh; 1684 struct drm_framebuffer *fb; 1685 struct drm_connector *connector; 1686 struct drm_crtc *crtc; 1687 struct drm_encoder *encoder; 1688 int ret = 0; 1689 int connector_count = 0; 1690 int crtc_count = 0; 1691 int fb_count = 0; 1692 int encoder_count = 0; 1693 int copied = 0, i; 1694 uint32_t __user *fb_id; 1695 uint32_t __user *crtc_id; 1696 uint32_t __user *connector_id; 1697 uint32_t __user *encoder_id; 1698 struct drm_mode_group *mode_group; 1699 1700 if (!drm_core_check_feature(dev, DRIVER_MODESET)) 1701 return -EINVAL; 1702 1703 1704 mutex_lock(&file_priv->fbs_lock); 1705 /* 1706 * For the non-control nodes we need to limit the list of resources 1707 * by IDs in the group list for this node 1708 */ 1709 list_for_each(lh, &file_priv->fbs) 1710 fb_count++; 1711 1712 /* handle this in 4 parts */ 1713 /* FBs */ 1714 if (card_res->count_fbs >= fb_count) { 1715 copied = 0; 1716 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr; 1717 list_for_each_entry(fb, &file_priv->fbs, filp_head) { 1718 if (put_user(fb->base.id, fb_id + copied)) { 1719 mutex_unlock(&file_priv->fbs_lock); 1720 return -EFAULT; 1721 } 1722 copied++; 1723 } 1724 } 1725 card_res->count_fbs = fb_count; 1726 mutex_unlock(&file_priv->fbs_lock); 1727 1728 drm_modeset_lock_all(dev); 1729 if (!drm_is_primary_client(file_priv)) { 1730 1731 mode_group = NULL; 1732 list_for_each(lh, &dev->mode_config.crtc_list) 1733 crtc_count++; 1734 1735 list_for_each(lh, &dev->mode_config.connector_list) 1736 connector_count++; 1737 1738 list_for_each(lh, &dev->mode_config.encoder_list) 1739 encoder_count++; 1740 #if 0 1741 } else { 1742 1743 mode_group = &file_priv->master->minor->mode_group; 1744 crtc_count = mode_group->num_crtcs; 1745 connector_count = mode_group->num_connectors; 1746 encoder_count = mode_group->num_encoders; 1747 #endif 1748 } 1749 1750 card_res->max_height = dev->mode_config.max_height; 1751 card_res->min_height = dev->mode_config.min_height; 1752 card_res->max_width = dev->mode_config.max_width; 1753 card_res->min_width = dev->mode_config.min_width; 1754 1755 /* CRTCs */ 1756 if (card_res->count_crtcs >= crtc_count) { 1757 copied = 0; 1758 crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr; 1759 if (!mode_group) { 1760 list_for_each_entry(crtc, &dev->mode_config.crtc_list, 1761 head) { 1762 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id); 1763 if (put_user(crtc->base.id, crtc_id + copied)) { 1764 ret = -EFAULT; 1765 goto out; 1766 } 1767 copied++; 1768 } 1769 } else { 1770 for (i = 0; i < mode_group->num_crtcs; i++) { 1771 if (put_user(mode_group->id_list[i], 1772 crtc_id + copied)) { 1773 ret = -EFAULT; 1774 goto out; 1775 } 1776 copied++; 1777 } 1778 } 1779 } 1780 card_res->count_crtcs = crtc_count; 1781 1782 /* Encoders */ 1783 if (card_res->count_encoders >= encoder_count) { 1784 copied = 0; 1785 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr; 1786 if (!mode_group) { 1787 list_for_each_entry(encoder, 1788 &dev->mode_config.encoder_list, 1789 head) { 1790 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder->base.id, 1791 encoder->name); 1792 if (put_user(encoder->base.id, encoder_id + 1793 copied)) { 1794 ret = -EFAULT; 1795 goto out; 1796 } 1797 copied++; 1798 } 1799 } else { 1800 for (i = mode_group->num_crtcs; i < mode_group->num_crtcs + mode_group->num_encoders; i++) { 1801 if (put_user(mode_group->id_list[i], 1802 encoder_id + copied)) { 1803 ret = -EFAULT; 1804 goto out; 1805 } 1806 copied++; 1807 } 1808 1809 } 1810 } 1811 card_res->count_encoders = encoder_count; 1812 1813 /* Connectors */ 1814 if (card_res->count_connectors >= connector_count) { 1815 copied = 0; 1816 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr; 1817 if (!mode_group) { 1818 list_for_each_entry(connector, 1819 &dev->mode_config.connector_list, 1820 head) { 1821 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n", 1822 connector->base.id, 1823 connector->name); 1824 if (put_user(connector->base.id, 1825 connector_id + copied)) { 1826 ret = -EFAULT; 1827 goto out; 1828 } 1829 copied++; 1830 } 1831 } else { 1832 int start = mode_group->num_crtcs + 1833 mode_group->num_encoders; 1834 for (i = start; i < start + mode_group->num_connectors; i++) { 1835 if (put_user(mode_group->id_list[i], 1836 connector_id + copied)) { 1837 ret = -EFAULT; 1838 goto out; 1839 } 1840 copied++; 1841 } 1842 } 1843 } 1844 card_res->count_connectors = connector_count; 1845 1846 DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res->count_crtcs, 1847 card_res->count_connectors, card_res->count_encoders); 1848 1849 out: 1850 drm_modeset_unlock_all(dev); 1851 return ret; 1852 } 1853 1854 /** 1855 * drm_mode_getcrtc - get CRTC configuration 1856 * @dev: drm device for the ioctl 1857 * @data: data pointer for the ioctl 1858 * @file_priv: drm file for the ioctl call 1859 * 1860 * Construct a CRTC configuration structure to return to the user. 1861 * 1862 * Called by the user via ioctl. 1863 * 1864 * Returns: 1865 * Zero on success, errno on failure. 1866 */ 1867 int drm_mode_getcrtc(struct drm_device *dev, 1868 void *data, struct drm_file *file_priv) 1869 { 1870 struct drm_mode_crtc *crtc_resp = data; 1871 struct drm_crtc *crtc; 1872 int ret = 0; 1873 1874 if (!drm_core_check_feature(dev, DRIVER_MODESET)) 1875 return -EINVAL; 1876 1877 drm_modeset_lock_all(dev); 1878 1879 crtc = drm_crtc_find(dev, crtc_resp->crtc_id); 1880 if (!crtc) { 1881 ret = -ENOENT; 1882 goto out; 1883 } 1884 1885 crtc_resp->x = crtc->x; 1886 crtc_resp->y = crtc->y; 1887 crtc_resp->gamma_size = crtc->gamma_size; 1888 if (crtc->primary->fb) 1889 crtc_resp->fb_id = crtc->primary->fb->base.id; 1890 else 1891 crtc_resp->fb_id = 0; 1892 1893 if (crtc->enabled) { 1894 1895 drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode); 1896 crtc_resp->mode_valid = 1; 1897 1898 } else { 1899 crtc_resp->mode_valid = 0; 1900 } 1901 1902 out: 1903 drm_modeset_unlock_all(dev); 1904 return ret; 1905 } 1906 1907 static bool drm_mode_expose_to_userspace(const struct drm_display_mode *mode, 1908 const struct drm_file *file_priv) 1909 { 1910 /* 1911 * If user-space hasn't configured the driver to expose the stereo 3D 1912 * modes, don't expose them. 1913 */ 1914 if (!file_priv->stereo_allowed && drm_mode_is_stereo(mode)) 1915 return false; 1916 1917 return true; 1918 } 1919 1920 /** 1921 * drm_mode_getconnector - get connector configuration 1922 * @dev: drm device for the ioctl 1923 * @data: data pointer for the ioctl 1924 * @file_priv: drm file for the ioctl call 1925 * 1926 * Construct a connector configuration structure to return to the user. 1927 * 1928 * Called by the user via ioctl. 1929 * 1930 * Returns: 1931 * Zero on success, errno on failure. 1932 */ 1933 int drm_mode_getconnector(struct drm_device *dev, void *data, 1934 struct drm_file *file_priv) 1935 { 1936 struct drm_mode_get_connector *out_resp = data; 1937 struct drm_connector *connector; 1938 struct drm_display_mode *mode; 1939 int mode_count = 0; 1940 int props_count = 0; 1941 int encoders_count = 0; 1942 int ret = 0; 1943 int copied = 0; 1944 int i; 1945 struct drm_mode_modeinfo u_mode; 1946 struct drm_mode_modeinfo __user *mode_ptr; 1947 uint32_t __user *prop_ptr; 1948 uint64_t __user *prop_values; 1949 uint32_t __user *encoder_ptr; 1950 1951 if (!drm_core_check_feature(dev, DRIVER_MODESET)) 1952 return -EINVAL; 1953 1954 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo)); 1955 1956 DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp->connector_id); 1957 1958 mutex_lock(&dev->mode_config.mutex); 1959 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL); 1960 1961 connector = drm_connector_find(dev, out_resp->connector_id); 1962 if (!connector) { 1963 ret = -ENOENT; 1964 goto out; 1965 } 1966 1967 props_count = connector->properties.count; 1968 1969 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) { 1970 if (connector->encoder_ids[i] != 0) { 1971 encoders_count++; 1972 } 1973 } 1974 1975 if (out_resp->count_modes == 0) { 1976 connector->funcs->fill_modes(connector, 1977 dev->mode_config.max_width, 1978 dev->mode_config.max_height); 1979 } 1980 1981 /* delayed so we get modes regardless of pre-fill_modes state */ 1982 list_for_each_entry(mode, &connector->modes, head) 1983 if (drm_mode_expose_to_userspace(mode, file_priv)) 1984 mode_count++; 1985 1986 out_resp->connector_id = connector->base.id; 1987 out_resp->connector_type = connector->connector_type; 1988 out_resp->connector_type_id = connector->connector_type_id; 1989 out_resp->mm_width = connector->display_info.width_mm; 1990 out_resp->mm_height = connector->display_info.height_mm; 1991 out_resp->subpixel = connector->display_info.subpixel_order; 1992 out_resp->connection = connector->status; 1993 if (connector->encoder) 1994 out_resp->encoder_id = connector->encoder->base.id; 1995 else 1996 out_resp->encoder_id = 0; 1997 1998 /* 1999 * This ioctl is called twice, once to determine how much space is 2000 * needed, and the 2nd time to fill it. 2001 */ 2002 if ((out_resp->count_modes >= mode_count) && mode_count) { 2003 copied = 0; 2004 mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr; 2005 list_for_each_entry(mode, &connector->modes, head) { 2006 if (!drm_mode_expose_to_userspace(mode, file_priv)) 2007 continue; 2008 2009 drm_crtc_convert_to_umode(&u_mode, mode); 2010 if (copy_to_user(mode_ptr + copied, 2011 &u_mode, sizeof(u_mode))) { 2012 ret = -EFAULT; 2013 goto out; 2014 } 2015 copied++; 2016 } 2017 } 2018 out_resp->count_modes = mode_count; 2019 2020 if ((out_resp->count_props >= props_count) && props_count) { 2021 copied = 0; 2022 prop_ptr = (uint32_t __user *)(unsigned long)(out_resp->props_ptr); 2023 prop_values = (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr); 2024 for (i = 0; i < connector->properties.count; i++) { 2025 if (put_user(connector->properties.ids[i], 2026 prop_ptr + copied)) { 2027 ret = -EFAULT; 2028 goto out; 2029 } 2030 2031 if (put_user(connector->properties.values[i], 2032 prop_values + copied)) { 2033 ret = -EFAULT; 2034 goto out; 2035 } 2036 copied++; 2037 } 2038 } 2039 out_resp->count_props = props_count; 2040 2041 if ((out_resp->count_encoders >= encoders_count) && encoders_count) { 2042 copied = 0; 2043 encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr); 2044 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) { 2045 if (connector->encoder_ids[i] != 0) { 2046 if (put_user(connector->encoder_ids[i], 2047 encoder_ptr + copied)) { 2048 ret = -EFAULT; 2049 goto out; 2050 } 2051 copied++; 2052 } 2053 } 2054 } 2055 out_resp->count_encoders = encoders_count; 2056 2057 out: 2058 drm_modeset_unlock(&dev->mode_config.connection_mutex); 2059 mutex_unlock(&dev->mode_config.mutex); 2060 2061 return ret; 2062 } 2063 2064 /** 2065 * drm_mode_getencoder - get encoder configuration 2066 * @dev: drm device for the ioctl 2067 * @data: data pointer for the ioctl 2068 * @file_priv: drm file for the ioctl call 2069 * 2070 * Construct a encoder configuration structure to return to the user. 2071 * 2072 * Called by the user via ioctl. 2073 * 2074 * Returns: 2075 * Zero on success, errno on failure. 2076 */ 2077 int drm_mode_getencoder(struct drm_device *dev, void *data, 2078 struct drm_file *file_priv) 2079 { 2080 struct drm_mode_get_encoder *enc_resp = data; 2081 struct drm_encoder *encoder; 2082 int ret = 0; 2083 2084 if (!drm_core_check_feature(dev, DRIVER_MODESET)) 2085 return -EINVAL; 2086 2087 drm_modeset_lock_all(dev); 2088 encoder = drm_encoder_find(dev, enc_resp->encoder_id); 2089 if (!encoder) { 2090 ret = -ENOENT; 2091 goto out; 2092 } 2093 2094 if (encoder->crtc) 2095 enc_resp->crtc_id = encoder->crtc->base.id; 2096 else 2097 enc_resp->crtc_id = 0; 2098 enc_resp->encoder_type = encoder->encoder_type; 2099 enc_resp->encoder_id = encoder->base.id; 2100 enc_resp->possible_crtcs = encoder->possible_crtcs; 2101 enc_resp->possible_clones = encoder->possible_clones; 2102 2103 out: 2104 drm_modeset_unlock_all(dev); 2105 return ret; 2106 } 2107 2108 /** 2109 * drm_mode_getplane_res - enumerate all plane resources 2110 * @dev: DRM device 2111 * @data: ioctl data 2112 * @file_priv: DRM file info 2113 * 2114 * Construct a list of plane ids to return to the user. 2115 * 2116 * Called by the user via ioctl. 2117 * 2118 * Returns: 2119 * Zero on success, errno on failure. 2120 */ 2121 int drm_mode_getplane_res(struct drm_device *dev, void *data, 2122 struct drm_file *file_priv) 2123 { 2124 struct drm_mode_get_plane_res *plane_resp = data; 2125 struct drm_mode_config *config; 2126 struct drm_plane *plane; 2127 uint32_t __user *plane_ptr; 2128 int copied = 0, ret = 0; 2129 unsigned num_planes; 2130 2131 if (!drm_core_check_feature(dev, DRIVER_MODESET)) 2132 return -EINVAL; 2133 2134 drm_modeset_lock_all(dev); 2135 config = &dev->mode_config; 2136 2137 if (file_priv->universal_planes) 2138 num_planes = config->num_total_plane; 2139 else 2140 num_planes = config->num_overlay_plane; 2141 2142 /* 2143 * This ioctl is called twice, once to determine how much space is 2144 * needed, and the 2nd time to fill it. 2145 */ 2146 if (num_planes && 2147 (plane_resp->count_planes >= num_planes)) { 2148 plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr; 2149 2150 list_for_each_entry(plane, &config->plane_list, head) { 2151 /* 2152 * Unless userspace set the 'universal planes' 2153 * capability bit, only advertise overlays. 2154 */ 2155 if (plane->type != DRM_PLANE_TYPE_OVERLAY && 2156 !file_priv->universal_planes) 2157 continue; 2158 2159 if (put_user(plane->base.id, plane_ptr + copied)) { 2160 ret = -EFAULT; 2161 goto out; 2162 } 2163 copied++; 2164 } 2165 } 2166 plane_resp->count_planes = num_planes; 2167 2168 out: 2169 drm_modeset_unlock_all(dev); 2170 return ret; 2171 } 2172 2173 /** 2174 * drm_mode_getplane - get plane configuration 2175 * @dev: DRM device 2176 * @data: ioctl data 2177 * @file_priv: DRM file info 2178 * 2179 * Construct a plane configuration structure to return to the user. 2180 * 2181 * Called by the user via ioctl. 2182 * 2183 * Returns: 2184 * Zero on success, errno on failure. 2185 */ 2186 int drm_mode_getplane(struct drm_device *dev, void *data, 2187 struct drm_file *file_priv) 2188 { 2189 struct drm_mode_get_plane *plane_resp = data; 2190 struct drm_plane *plane; 2191 uint32_t __user *format_ptr; 2192 int ret = 0; 2193 2194 if (!drm_core_check_feature(dev, DRIVER_MODESET)) 2195 return -EINVAL; 2196 2197 drm_modeset_lock_all(dev); 2198 plane = drm_plane_find(dev, plane_resp->plane_id); 2199 if (!plane) { 2200 ret = -ENOENT; 2201 goto out; 2202 } 2203 2204 if (plane->crtc) 2205 plane_resp->crtc_id = plane->crtc->base.id; 2206 else 2207 plane_resp->crtc_id = 0; 2208 2209 if (plane->fb) 2210 plane_resp->fb_id = plane->fb->base.id; 2211 else 2212 plane_resp->fb_id = 0; 2213 2214 plane_resp->plane_id = plane->base.id; 2215 plane_resp->possible_crtcs = plane->possible_crtcs; 2216 plane_resp->gamma_size = 0; 2217 2218 /* 2219 * This ioctl is called twice, once to determine how much space is 2220 * needed, and the 2nd time to fill it. 2221 */ 2222 if (plane->format_count && 2223 (plane_resp->count_format_types >= plane->format_count)) { 2224 format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr; 2225 if (copy_to_user(format_ptr, 2226 plane->format_types, 2227 sizeof(uint32_t) * plane->format_count)) { 2228 ret = -EFAULT; 2229 goto out; 2230 } 2231 } 2232 plane_resp->count_format_types = plane->format_count; 2233 2234 out: 2235 drm_modeset_unlock_all(dev); 2236 return ret; 2237 } 2238 2239 /* 2240 * setplane_internal - setplane handler for internal callers 2241 * 2242 * Note that we assume an extra reference has already been taken on fb. If the 2243 * update fails, this reference will be dropped before return; if it succeeds, 2244 * the previous framebuffer (if any) will be unreferenced instead. 2245 * 2246 * src_{x,y,w,h} are provided in 16.16 fixed point format 2247 */ 2248 static int __setplane_internal(struct drm_plane *plane, 2249 struct drm_crtc *crtc, 2250 struct drm_framebuffer *fb, 2251 int32_t crtc_x, int32_t crtc_y, 2252 uint32_t crtc_w, uint32_t crtc_h, 2253 /* src_{x,y,w,h} values are 16.16 fixed point */ 2254 uint32_t src_x, uint32_t src_y, 2255 uint32_t src_w, uint32_t src_h) 2256 { 2257 int ret = 0; 2258 unsigned int fb_width, fb_height; 2259 int i; 2260 2261 /* No fb means shut it down */ 2262 if (!fb) { 2263 plane->old_fb = plane->fb; 2264 ret = plane->funcs->disable_plane(plane); 2265 if (!ret) { 2266 plane->crtc = NULL; 2267 plane->fb = NULL; 2268 } else { 2269 plane->old_fb = NULL; 2270 } 2271 goto out; 2272 } 2273 2274 /* Check whether this plane is usable on this CRTC */ 2275 if (!(plane->possible_crtcs & drm_crtc_mask(crtc))) { 2276 DRM_DEBUG_KMS("Invalid crtc for plane\n"); 2277 ret = -EINVAL; 2278 goto out; 2279 } 2280 2281 /* Check whether this plane supports the fb pixel format. */ 2282 for (i = 0; i < plane->format_count; i++) 2283 if (fb->pixel_format == plane->format_types[i]) 2284 break; 2285 if (i == plane->format_count) { 2286 DRM_DEBUG_KMS("Invalid pixel format %s\n", 2287 drm_get_format_name(fb->pixel_format)); 2288 ret = -EINVAL; 2289 goto out; 2290 } 2291 2292 fb_width = fb->width << 16; 2293 fb_height = fb->height << 16; 2294 2295 /* Make sure source coordinates are inside the fb. */ 2296 if (src_w > fb_width || 2297 src_x > fb_width - src_w || 2298 src_h > fb_height || 2299 src_y > fb_height - src_h) { 2300 DRM_DEBUG_KMS("Invalid source coordinates " 2301 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n", 2302 src_w >> 16, ((src_w & 0xffff) * 15625) >> 10, 2303 src_h >> 16, ((src_h & 0xffff) * 15625) >> 10, 2304 src_x >> 16, ((src_x & 0xffff) * 15625) >> 10, 2305 src_y >> 16, ((src_y & 0xffff) * 15625) >> 10); 2306 ret = -ENOSPC; 2307 goto out; 2308 } 2309 2310 plane->old_fb = plane->fb; 2311 ret = plane->funcs->update_plane(plane, crtc, fb, 2312 crtc_x, crtc_y, crtc_w, crtc_h, 2313 src_x, src_y, src_w, src_h); 2314 if (!ret) { 2315 plane->crtc = crtc; 2316 plane->fb = fb; 2317 fb = NULL; 2318 } else { 2319 plane->old_fb = NULL; 2320 } 2321 2322 out: 2323 if (fb) 2324 drm_framebuffer_unreference(fb); 2325 if (plane->old_fb) 2326 drm_framebuffer_unreference(plane->old_fb); 2327 plane->old_fb = NULL; 2328 2329 return ret; 2330 } 2331 2332 static int setplane_internal(struct drm_plane *plane, 2333 struct drm_crtc *crtc, 2334 struct drm_framebuffer *fb, 2335 int32_t crtc_x, int32_t crtc_y, 2336 uint32_t crtc_w, uint32_t crtc_h, 2337 /* src_{x,y,w,h} values are 16.16 fixed point */ 2338 uint32_t src_x, uint32_t src_y, 2339 uint32_t src_w, uint32_t src_h) 2340 { 2341 int ret; 2342 2343 drm_modeset_lock_all(plane->dev); 2344 ret = __setplane_internal(plane, crtc, fb, 2345 crtc_x, crtc_y, crtc_w, crtc_h, 2346 src_x, src_y, src_w, src_h); 2347 drm_modeset_unlock_all(plane->dev); 2348 2349 return ret; 2350 } 2351 2352 /** 2353 * drm_mode_setplane - configure a plane's configuration 2354 * @dev: DRM device 2355 * @data: ioctl data* 2356 * @file_priv: DRM file info 2357 * 2358 * Set plane configuration, including placement, fb, scaling, and other factors. 2359 * Or pass a NULL fb to disable (planes may be disabled without providing a 2360 * valid crtc). 2361 * 2362 * Returns: 2363 * Zero on success, errno on failure. 2364 */ 2365 int drm_mode_setplane(struct drm_device *dev, void *data, 2366 struct drm_file *file_priv) 2367 { 2368 struct drm_mode_set_plane *plane_req = data; 2369 struct drm_mode_object *obj; 2370 struct drm_plane *plane; 2371 struct drm_crtc *crtc = NULL; 2372 struct drm_framebuffer *fb = NULL; 2373 2374 if (!drm_core_check_feature(dev, DRIVER_MODESET)) 2375 return -EINVAL; 2376 2377 /* Give drivers some help against integer overflows */ 2378 if (plane_req->crtc_w > INT_MAX || 2379 plane_req->crtc_x > INT_MAX - (int32_t) plane_req->crtc_w || 2380 plane_req->crtc_h > INT_MAX || 2381 plane_req->crtc_y > INT_MAX - (int32_t) plane_req->crtc_h) { 2382 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n", 2383 plane_req->crtc_w, plane_req->crtc_h, 2384 plane_req->crtc_x, plane_req->crtc_y); 2385 return -ERANGE; 2386 } 2387 2388 /* 2389 * First, find the plane, crtc, and fb objects. If not available, 2390 * we don't bother to call the driver. 2391 */ 2392 obj = drm_mode_object_find(dev, plane_req->plane_id, 2393 DRM_MODE_OBJECT_PLANE); 2394 if (!obj) { 2395 DRM_DEBUG_KMS("Unknown plane ID %d\n", 2396 plane_req->plane_id); 2397 return -ENOENT; 2398 } 2399 plane = obj_to_plane(obj); 2400 2401 if (plane_req->fb_id) { 2402 fb = drm_framebuffer_lookup(dev, plane_req->fb_id); 2403 if (!fb) { 2404 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n", 2405 plane_req->fb_id); 2406 return -ENOENT; 2407 } 2408 2409 obj = drm_mode_object_find(dev, plane_req->crtc_id, 2410 DRM_MODE_OBJECT_CRTC); 2411 if (!obj) { 2412 DRM_DEBUG_KMS("Unknown crtc ID %d\n", 2413 plane_req->crtc_id); 2414 return -ENOENT; 2415 } 2416 crtc = obj_to_crtc(obj); 2417 } 2418 2419 /* 2420 * setplane_internal will take care of deref'ing either the old or new 2421 * framebuffer depending on success. 2422 */ 2423 return setplane_internal(plane, crtc, fb, 2424 plane_req->crtc_x, plane_req->crtc_y, 2425 plane_req->crtc_w, plane_req->crtc_h, 2426 plane_req->src_x, plane_req->src_y, 2427 plane_req->src_w, plane_req->src_h); 2428 } 2429 2430 /** 2431 * drm_mode_set_config_internal - helper to call ->set_config 2432 * @set: modeset config to set 2433 * 2434 * This is a little helper to wrap internal calls to the ->set_config driver 2435 * interface. The only thing it adds is correct refcounting dance. 2436 * 2437 * Returns: 2438 * Zero on success, errno on failure. 2439 */ 2440 int drm_mode_set_config_internal(struct drm_mode_set *set) 2441 { 2442 struct drm_crtc *crtc = set->crtc; 2443 struct drm_framebuffer *fb; 2444 struct drm_crtc *tmp; 2445 int ret; 2446 2447 /* 2448 * NOTE: ->set_config can also disable other crtcs (if we steal all 2449 * connectors from it), hence we need to refcount the fbs across all 2450 * crtcs. Atomic modeset will have saner semantics ... 2451 */ 2452 list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head) 2453 tmp->primary->old_fb = tmp->primary->fb; 2454 2455 fb = set->fb; 2456 2457 ret = crtc->funcs->set_config(set); 2458 if (ret == 0) { 2459 crtc->primary->crtc = crtc; 2460 crtc->primary->fb = fb; 2461 } 2462 2463 list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head) { 2464 if (tmp->primary->fb) 2465 drm_framebuffer_reference(tmp->primary->fb); 2466 if (tmp->primary->old_fb) 2467 drm_framebuffer_unreference(tmp->primary->old_fb); 2468 tmp->primary->old_fb = NULL; 2469 } 2470 2471 return ret; 2472 } 2473 EXPORT_SYMBOL(drm_mode_set_config_internal); 2474 2475 /** 2476 * drm_crtc_check_viewport - Checks that a framebuffer is big enough for the 2477 * CRTC viewport 2478 * @crtc: CRTC that framebuffer will be displayed on 2479 * @x: x panning 2480 * @y: y panning 2481 * @mode: mode that framebuffer will be displayed under 2482 * @fb: framebuffer to check size of 2483 */ 2484 int drm_crtc_check_viewport(const struct drm_crtc *crtc, 2485 int x, int y, 2486 const struct drm_display_mode *mode, 2487 const struct drm_framebuffer *fb) 2488 2489 { 2490 int hdisplay, vdisplay; 2491 2492 hdisplay = mode->hdisplay; 2493 vdisplay = mode->vdisplay; 2494 2495 if (drm_mode_is_stereo(mode)) { 2496 struct drm_display_mode adjusted = *mode; 2497 2498 drm_mode_set_crtcinfo(&adjusted, CRTC_STEREO_DOUBLE); 2499 hdisplay = adjusted.crtc_hdisplay; 2500 vdisplay = adjusted.crtc_vdisplay; 2501 } 2502 2503 if (crtc->invert_dimensions) 2504 swap(hdisplay, vdisplay); 2505 2506 if (hdisplay > fb->width || 2507 vdisplay > fb->height || 2508 x > fb->width - hdisplay || 2509 y > fb->height - vdisplay) { 2510 DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n", 2511 fb->width, fb->height, hdisplay, vdisplay, x, y, 2512 crtc->invert_dimensions ? " (inverted)" : ""); 2513 return -ENOSPC; 2514 } 2515 2516 return 0; 2517 } 2518 EXPORT_SYMBOL(drm_crtc_check_viewport); 2519 2520 /** 2521 * drm_mode_setcrtc - set CRTC configuration 2522 * @dev: drm device for the ioctl 2523 * @data: data pointer for the ioctl 2524 * @file_priv: drm file for the ioctl call 2525 * 2526 * Build a new CRTC configuration based on user request. 2527 * 2528 * Called by the user via ioctl. 2529 * 2530 * Returns: 2531 * Zero on success, errno on failure. 2532 */ 2533 int drm_mode_setcrtc(struct drm_device *dev, void *data, 2534 struct drm_file *file_priv) 2535 { 2536 struct drm_mode_config *config = &dev->mode_config; 2537 struct drm_mode_crtc *crtc_req = data; 2538 struct drm_crtc *crtc; 2539 struct drm_connector **connector_set = NULL, *connector; 2540 struct drm_framebuffer *fb = NULL; 2541 struct drm_display_mode *mode = NULL; 2542 struct drm_mode_set set; 2543 uint32_t __user *set_connectors_ptr; 2544 int ret; 2545 int i; 2546 2547 if (!drm_core_check_feature(dev, DRIVER_MODESET)) 2548 return -EINVAL; 2549 2550 /* For some reason crtc x/y offsets are signed internally. */ 2551 if (crtc_req->x > INT_MAX || crtc_req->y > INT_MAX) 2552 return -ERANGE; 2553 2554 drm_modeset_lock_all(dev); 2555 crtc = drm_crtc_find(dev, crtc_req->crtc_id); 2556 if (!crtc) { 2557 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id); 2558 ret = -ENOENT; 2559 goto out; 2560 } 2561 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id); 2562 2563 if (crtc_req->mode_valid) { 2564 /* If we have a mode we need a framebuffer. */ 2565 /* If we pass -1, set the mode with the currently bound fb */ 2566 if (crtc_req->fb_id == -1) { 2567 if (!crtc->primary->fb) { 2568 DRM_DEBUG_KMS("CRTC doesn't have current FB\n"); 2569 ret = -EINVAL; 2570 goto out; 2571 } 2572 fb = crtc->primary->fb; 2573 /* Make refcounting symmetric with the lookup path. */ 2574 drm_framebuffer_reference(fb); 2575 } else { 2576 fb = drm_framebuffer_lookup(dev, crtc_req->fb_id); 2577 if (!fb) { 2578 DRM_DEBUG_KMS("Unknown FB ID%d\n", 2579 crtc_req->fb_id); 2580 ret = -ENOENT; 2581 goto out; 2582 } 2583 } 2584 2585 mode = drm_mode_create(dev); 2586 if (!mode) { 2587 ret = -ENOMEM; 2588 goto out; 2589 } 2590 2591 ret = drm_crtc_convert_umode(mode, &crtc_req->mode); 2592 if (ret) { 2593 DRM_DEBUG_KMS("Invalid mode\n"); 2594 goto out; 2595 } 2596 2597 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V); 2598 2599 ret = drm_crtc_check_viewport(crtc, crtc_req->x, crtc_req->y, 2600 mode, fb); 2601 if (ret) 2602 goto out; 2603 2604 } 2605 2606 if (crtc_req->count_connectors == 0 && mode) { 2607 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n"); 2608 ret = -EINVAL; 2609 goto out; 2610 } 2611 2612 if (crtc_req->count_connectors > 0 && (!mode || !fb)) { 2613 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n", 2614 crtc_req->count_connectors); 2615 ret = -EINVAL; 2616 goto out; 2617 } 2618 2619 if (crtc_req->count_connectors > 0) { 2620 u32 out_id; 2621 2622 /* Avoid unbounded kernel memory allocation */ 2623 if (crtc_req->count_connectors > config->num_connector) { 2624 ret = -EINVAL; 2625 goto out; 2626 } 2627 2628 connector_set = kmalloc(crtc_req->count_connectors * 2629 sizeof(struct drm_connector *), 2630 M_DRM, M_WAITOK); 2631 if (!connector_set) { 2632 ret = -ENOMEM; 2633 goto out; 2634 } 2635 2636 for (i = 0; i < crtc_req->count_connectors; i++) { 2637 set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr; 2638 if (get_user(out_id, &set_connectors_ptr[i])) { 2639 ret = -EFAULT; 2640 goto out; 2641 } 2642 2643 connector = drm_connector_find(dev, out_id); 2644 if (!connector) { 2645 DRM_DEBUG_KMS("Connector id %d unknown\n", 2646 out_id); 2647 ret = -ENOENT; 2648 goto out; 2649 } 2650 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n", 2651 connector->base.id, 2652 connector->name); 2653 2654 connector_set[i] = connector; 2655 } 2656 } 2657 2658 set.crtc = crtc; 2659 set.x = crtc_req->x; 2660 set.y = crtc_req->y; 2661 set.mode = mode; 2662 set.connectors = connector_set; 2663 set.num_connectors = crtc_req->count_connectors; 2664 set.fb = fb; 2665 ret = drm_mode_set_config_internal(&set); 2666 2667 out: 2668 if (fb) 2669 drm_framebuffer_unreference(fb); 2670 2671 kfree(connector_set); 2672 drm_mode_destroy(dev, mode); 2673 drm_modeset_unlock_all(dev); 2674 return ret; 2675 } 2676 2677 /** 2678 * drm_mode_cursor_universal - translate legacy cursor ioctl call into a 2679 * universal plane handler call 2680 * @crtc: crtc to update cursor for 2681 * @req: data pointer for the ioctl 2682 * @file_priv: drm file for the ioctl call 2683 * 2684 * Legacy cursor ioctl's work directly with driver buffer handles. To 2685 * translate legacy ioctl calls into universal plane handler calls, we need to 2686 * wrap the native buffer handle in a drm_framebuffer. 2687 * 2688 * Note that we assume any handle passed to the legacy ioctls was a 32-bit ARGB 2689 * buffer with a pitch of 4*width; the universal plane interface should be used 2690 * directly in cases where the hardware can support other buffer settings and 2691 * userspace wants to make use of these capabilities. 2692 * 2693 * Returns: 2694 * Zero on success, errno on failure. 2695 */ 2696 static int drm_mode_cursor_universal(struct drm_crtc *crtc, 2697 struct drm_mode_cursor2 *req, 2698 struct drm_file *file_priv) 2699 { 2700 struct drm_device *dev = crtc->dev; 2701 struct drm_framebuffer *fb = NULL; 2702 struct drm_mode_fb_cmd2 fbreq = { 2703 .width = req->width, 2704 .height = req->height, 2705 .pixel_format = DRM_FORMAT_ARGB8888, 2706 .pitches = { req->width * 4 }, 2707 .handles = { req->handle }, 2708 }; 2709 int32_t crtc_x, crtc_y; 2710 uint32_t crtc_w = 0, crtc_h = 0; 2711 uint32_t src_w = 0, src_h = 0; 2712 int ret = 0; 2713 2714 BUG_ON(!crtc->cursor); 2715 WARN_ON(crtc->cursor->crtc != crtc && crtc->cursor->crtc != NULL); 2716 2717 /* 2718 * Obtain fb we'll be using (either new or existing) and take an extra 2719 * reference to it if fb != null. setplane will take care of dropping 2720 * the reference if the plane update fails. 2721 */ 2722 if (req->flags & DRM_MODE_CURSOR_BO) { 2723 if (req->handle) { 2724 fb = add_framebuffer_internal(dev, &fbreq, file_priv); 2725 if (IS_ERR(fb)) { 2726 DRM_DEBUG_KMS("failed to wrap cursor buffer in drm framebuffer\n"); 2727 return PTR_ERR(fb); 2728 } 2729 2730 drm_framebuffer_reference(fb); 2731 } else { 2732 fb = NULL; 2733 } 2734 } else { 2735 fb = crtc->cursor->fb; 2736 if (fb) 2737 drm_framebuffer_reference(fb); 2738 } 2739 2740 if (req->flags & DRM_MODE_CURSOR_MOVE) { 2741 crtc_x = req->x; 2742 crtc_y = req->y; 2743 } else { 2744 crtc_x = crtc->cursor_x; 2745 crtc_y = crtc->cursor_y; 2746 } 2747 2748 if (fb) { 2749 crtc_w = fb->width; 2750 crtc_h = fb->height; 2751 src_w = fb->width << 16; 2752 src_h = fb->height << 16; 2753 } 2754 2755 /* 2756 * setplane_internal will take care of deref'ing either the old or new 2757 * framebuffer depending on success. 2758 */ 2759 ret = __setplane_internal(crtc->cursor, crtc, fb, 2760 crtc_x, crtc_y, crtc_w, crtc_h, 2761 0, 0, src_w, src_h); 2762 2763 /* Update successful; save new cursor position, if necessary */ 2764 if (ret == 0 && req->flags & DRM_MODE_CURSOR_MOVE) { 2765 crtc->cursor_x = req->x; 2766 crtc->cursor_y = req->y; 2767 } 2768 2769 return ret; 2770 } 2771 2772 static int drm_mode_cursor_common(struct drm_device *dev, 2773 struct drm_mode_cursor2 *req, 2774 struct drm_file *file_priv) 2775 { 2776 struct drm_crtc *crtc; 2777 int ret = 0; 2778 2779 if (!drm_core_check_feature(dev, DRIVER_MODESET)) 2780 return -EINVAL; 2781 2782 if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags)) 2783 return -EINVAL; 2784 2785 crtc = drm_crtc_find(dev, req->crtc_id); 2786 if (!crtc) { 2787 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id); 2788 return -ENOENT; 2789 } 2790 2791 /* 2792 * If this crtc has a universal cursor plane, call that plane's update 2793 * handler rather than using legacy cursor handlers. 2794 */ 2795 drm_modeset_lock_crtc(crtc); 2796 if (crtc->cursor) { 2797 ret = drm_mode_cursor_universal(crtc, req, file_priv); 2798 goto out; 2799 } 2800 2801 if (req->flags & DRM_MODE_CURSOR_BO) { 2802 if (!crtc->funcs->cursor_set && !crtc->funcs->cursor_set2) { 2803 ret = -ENXIO; 2804 goto out; 2805 } 2806 /* Turns off the cursor if handle is 0 */ 2807 if (crtc->funcs->cursor_set2) 2808 ret = crtc->funcs->cursor_set2(crtc, file_priv, req->handle, 2809 req->width, req->height, req->hot_x, req->hot_y); 2810 else 2811 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle, 2812 req->width, req->height); 2813 } 2814 2815 if (req->flags & DRM_MODE_CURSOR_MOVE) { 2816 if (crtc->funcs->cursor_move) { 2817 ret = crtc->funcs->cursor_move(crtc, req->x, req->y); 2818 } else { 2819 ret = -EFAULT; 2820 goto out; 2821 } 2822 } 2823 out: 2824 drm_modeset_unlock_crtc(crtc); 2825 2826 return ret; 2827 2828 } 2829 2830 2831 /** 2832 * drm_mode_cursor_ioctl - set CRTC's cursor configuration 2833 * @dev: drm device for the ioctl 2834 * @data: data pointer for the ioctl 2835 * @file_priv: drm file for the ioctl call 2836 * 2837 * Set the cursor configuration based on user request. 2838 * 2839 * Called by the user via ioctl. 2840 * 2841 * Returns: 2842 * Zero on success, errno on failure. 2843 */ 2844 int drm_mode_cursor_ioctl(struct drm_device *dev, 2845 void *data, struct drm_file *file_priv) 2846 { 2847 struct drm_mode_cursor *req = data; 2848 struct drm_mode_cursor2 new_req; 2849 2850 memcpy(&new_req, req, sizeof(struct drm_mode_cursor)); 2851 new_req.hot_x = new_req.hot_y = 0; 2852 2853 return drm_mode_cursor_common(dev, &new_req, file_priv); 2854 } 2855 2856 /** 2857 * drm_mode_cursor2_ioctl - set CRTC's cursor configuration 2858 * @dev: drm device for the ioctl 2859 * @data: data pointer for the ioctl 2860 * @file_priv: drm file for the ioctl call 2861 * 2862 * Set the cursor configuration based on user request. This implements the 2nd 2863 * version of the cursor ioctl, which allows userspace to additionally specify 2864 * the hotspot of the pointer. 2865 * 2866 * Called by the user via ioctl. 2867 * 2868 * Returns: 2869 * Zero on success, errno on failure. 2870 */ 2871 int drm_mode_cursor2_ioctl(struct drm_device *dev, 2872 void *data, struct drm_file *file_priv) 2873 { 2874 struct drm_mode_cursor2 *req = data; 2875 return drm_mode_cursor_common(dev, req, file_priv); 2876 } 2877 2878 /** 2879 * drm_mode_legacy_fb_format - compute drm fourcc code from legacy description 2880 * @bpp: bits per pixels 2881 * @depth: bit depth per pixel 2882 * 2883 * Computes a drm fourcc pixel format code for the given @bpp/@depth values. 2884 * Useful in fbdev emulation code, since that deals in those values. 2885 */ 2886 uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth) 2887 { 2888 uint32_t fmt; 2889 2890 switch (bpp) { 2891 case 8: 2892 fmt = DRM_FORMAT_C8; 2893 break; 2894 case 16: 2895 if (depth == 15) 2896 fmt = DRM_FORMAT_XRGB1555; 2897 else 2898 fmt = DRM_FORMAT_RGB565; 2899 break; 2900 case 24: 2901 fmt = DRM_FORMAT_RGB888; 2902 break; 2903 case 32: 2904 if (depth == 24) 2905 fmt = DRM_FORMAT_XRGB8888; 2906 else if (depth == 30) 2907 fmt = DRM_FORMAT_XRGB2101010; 2908 else 2909 fmt = DRM_FORMAT_ARGB8888; 2910 break; 2911 default: 2912 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n"); 2913 fmt = DRM_FORMAT_XRGB8888; 2914 break; 2915 } 2916 2917 return fmt; 2918 } 2919 EXPORT_SYMBOL(drm_mode_legacy_fb_format); 2920 2921 /** 2922 * drm_mode_addfb - add an FB to the graphics configuration 2923 * @dev: drm device for the ioctl 2924 * @data: data pointer for the ioctl 2925 * @file_priv: drm file for the ioctl call 2926 * 2927 * Add a new FB to the specified CRTC, given a user request. This is the 2928 * original addfb ioclt which only supported RGB formats. 2929 * 2930 * Called by the user via ioctl. 2931 * 2932 * Returns: 2933 * Zero on success, errno on failure. 2934 */ 2935 int drm_mode_addfb(struct drm_device *dev, 2936 void *data, struct drm_file *file_priv) 2937 { 2938 struct drm_mode_fb_cmd *or = data; 2939 struct drm_mode_fb_cmd2 r = {}; 2940 struct drm_mode_config *config = &dev->mode_config; 2941 struct drm_framebuffer *fb; 2942 int ret = 0; 2943 2944 /* Use new struct with format internally */ 2945 r.fb_id = or->fb_id; 2946 r.width = or->width; 2947 r.height = or->height; 2948 r.pitches[0] = or->pitch; 2949 r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth); 2950 r.handles[0] = or->handle; 2951 2952 if (!drm_core_check_feature(dev, DRIVER_MODESET)) 2953 return -EINVAL; 2954 2955 if ((config->min_width > r.width) || (r.width > config->max_width)) 2956 return -EINVAL; 2957 2958 if ((config->min_height > r.height) || (r.height > config->max_height)) 2959 return -EINVAL; 2960 2961 fb = dev->mode_config.funcs->fb_create(dev, file_priv, &r); 2962 if (IS_ERR(fb)) { 2963 DRM_DEBUG_KMS("could not create framebuffer\n"); 2964 return PTR_ERR(fb); 2965 } 2966 2967 mutex_lock(&file_priv->fbs_lock); 2968 or->fb_id = fb->base.id; 2969 list_add(&fb->filp_head, &file_priv->fbs); 2970 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id); 2971 mutex_unlock(&file_priv->fbs_lock); 2972 2973 return ret; 2974 } 2975 2976 static int format_check(const struct drm_mode_fb_cmd2 *r) 2977 { 2978 uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN; 2979 2980 switch (format) { 2981 case DRM_FORMAT_C8: 2982 case DRM_FORMAT_RGB332: 2983 case DRM_FORMAT_BGR233: 2984 case DRM_FORMAT_XRGB4444: 2985 case DRM_FORMAT_XBGR4444: 2986 case DRM_FORMAT_RGBX4444: 2987 case DRM_FORMAT_BGRX4444: 2988 case DRM_FORMAT_ARGB4444: 2989 case DRM_FORMAT_ABGR4444: 2990 case DRM_FORMAT_RGBA4444: 2991 case DRM_FORMAT_BGRA4444: 2992 case DRM_FORMAT_XRGB1555: 2993 case DRM_FORMAT_XBGR1555: 2994 case DRM_FORMAT_RGBX5551: 2995 case DRM_FORMAT_BGRX5551: 2996 case DRM_FORMAT_ARGB1555: 2997 case DRM_FORMAT_ABGR1555: 2998 case DRM_FORMAT_RGBA5551: 2999 case DRM_FORMAT_BGRA5551: 3000 case DRM_FORMAT_RGB565: 3001 case DRM_FORMAT_BGR565: 3002 case DRM_FORMAT_RGB888: 3003 case DRM_FORMAT_BGR888: 3004 case DRM_FORMAT_XRGB8888: 3005 case DRM_FORMAT_XBGR8888: 3006 case DRM_FORMAT_RGBX8888: 3007 case DRM_FORMAT_BGRX8888: 3008 case DRM_FORMAT_ARGB8888: 3009 case DRM_FORMAT_ABGR8888: 3010 case DRM_FORMAT_RGBA8888: 3011 case DRM_FORMAT_BGRA8888: 3012 case DRM_FORMAT_XRGB2101010: 3013 case DRM_FORMAT_XBGR2101010: 3014 case DRM_FORMAT_RGBX1010102: 3015 case DRM_FORMAT_BGRX1010102: 3016 case DRM_FORMAT_ARGB2101010: 3017 case DRM_FORMAT_ABGR2101010: 3018 case DRM_FORMAT_RGBA1010102: 3019 case DRM_FORMAT_BGRA1010102: 3020 case DRM_FORMAT_YUYV: 3021 case DRM_FORMAT_YVYU: 3022 case DRM_FORMAT_UYVY: 3023 case DRM_FORMAT_VYUY: 3024 case DRM_FORMAT_AYUV: 3025 case DRM_FORMAT_NV12: 3026 case DRM_FORMAT_NV21: 3027 case DRM_FORMAT_NV16: 3028 case DRM_FORMAT_NV61: 3029 case DRM_FORMAT_NV24: 3030 case DRM_FORMAT_NV42: 3031 case DRM_FORMAT_YUV410: 3032 case DRM_FORMAT_YVU410: 3033 case DRM_FORMAT_YUV411: 3034 case DRM_FORMAT_YVU411: 3035 case DRM_FORMAT_YUV420: 3036 case DRM_FORMAT_YVU420: 3037 case DRM_FORMAT_YUV422: 3038 case DRM_FORMAT_YVU422: 3039 case DRM_FORMAT_YUV444: 3040 case DRM_FORMAT_YVU444: 3041 return 0; 3042 default: 3043 DRM_DEBUG_KMS("invalid pixel format %s\n", 3044 drm_get_format_name(r->pixel_format)); 3045 return -EINVAL; 3046 } 3047 } 3048 3049 static int framebuffer_check(const struct drm_mode_fb_cmd2 *r) 3050 { 3051 int ret, hsub, vsub, num_planes, i; 3052 3053 ret = format_check(r); 3054 if (ret) { 3055 DRM_DEBUG_KMS("bad framebuffer format %s\n", 3056 drm_get_format_name(r->pixel_format)); 3057 return ret; 3058 } 3059 3060 hsub = drm_format_horz_chroma_subsampling(r->pixel_format); 3061 vsub = drm_format_vert_chroma_subsampling(r->pixel_format); 3062 num_planes = drm_format_num_planes(r->pixel_format); 3063 3064 if (r->width == 0 || r->width % hsub) { 3065 DRM_DEBUG_KMS("bad framebuffer width %u\n", r->height); 3066 return -EINVAL; 3067 } 3068 3069 if (r->height == 0 || r->height % vsub) { 3070 DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height); 3071 return -EINVAL; 3072 } 3073 3074 for (i = 0; i < num_planes; i++) { 3075 unsigned int width = r->width / (i != 0 ? hsub : 1); 3076 unsigned int height = r->height / (i != 0 ? vsub : 1); 3077 unsigned int cpp = drm_format_plane_cpp(r->pixel_format, i); 3078 3079 if (!r->handles[i]) { 3080 DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i); 3081 return -EINVAL; 3082 } 3083 3084 if ((uint64_t) width * cpp > UINT_MAX) 3085 return -ERANGE; 3086 3087 if ((uint64_t) height * r->pitches[i] + r->offsets[i] > UINT_MAX) 3088 return -ERANGE; 3089 3090 if (r->pitches[i] < width * cpp) { 3091 DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i); 3092 return -EINVAL; 3093 } 3094 } 3095 3096 return 0; 3097 } 3098 3099 static struct drm_framebuffer *add_framebuffer_internal(struct drm_device *dev, 3100 struct drm_mode_fb_cmd2 *r, 3101 struct drm_file *file_priv) 3102 { 3103 struct drm_mode_config *config = &dev->mode_config; 3104 struct drm_framebuffer *fb; 3105 int ret; 3106 3107 if (r->flags & ~DRM_MODE_FB_INTERLACED) { 3108 DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r->flags); 3109 return ERR_PTR(-EINVAL); 3110 } 3111 3112 if ((config->min_width > r->width) || (r->width > config->max_width)) { 3113 DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n", 3114 r->width, config->min_width, config->max_width); 3115 return ERR_PTR(-EINVAL); 3116 } 3117 if ((config->min_height > r->height) || (r->height > config->max_height)) { 3118 DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n", 3119 r->height, config->min_height, config->max_height); 3120 return ERR_PTR(-EINVAL); 3121 } 3122 3123 ret = framebuffer_check(r); 3124 if (ret) 3125 return ERR_PTR(ret); 3126 3127 fb = dev->mode_config.funcs->fb_create(dev, file_priv, r); 3128 if (IS_ERR(fb)) { 3129 DRM_DEBUG_KMS("could not create framebuffer\n"); 3130 return fb; 3131 } 3132 3133 mutex_lock(&file_priv->fbs_lock); 3134 r->fb_id = fb->base.id; 3135 list_add(&fb->filp_head, &file_priv->fbs); 3136 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id); 3137 mutex_unlock(&file_priv->fbs_lock); 3138 3139 return fb; 3140 } 3141 3142 /** 3143 * drm_mode_addfb2 - add an FB to the graphics configuration 3144 * @dev: drm device for the ioctl 3145 * @data: data pointer for the ioctl 3146 * @file_priv: drm file for the ioctl call 3147 * 3148 * Add a new FB to the specified CRTC, given a user request with format. This is 3149 * the 2nd version of the addfb ioctl, which supports multi-planar framebuffers 3150 * and uses fourcc codes as pixel format specifiers. 3151 * 3152 * Called by the user via ioctl. 3153 * 3154 * Returns: 3155 * Zero on success, errno on failure. 3156 */ 3157 int drm_mode_addfb2(struct drm_device *dev, 3158 void *data, struct drm_file *file_priv) 3159 { 3160 struct drm_framebuffer *fb; 3161 3162 if (!drm_core_check_feature(dev, DRIVER_MODESET)) 3163 return -EINVAL; 3164 3165 fb = add_framebuffer_internal(dev, data, file_priv); 3166 if (IS_ERR(fb)) 3167 return PTR_ERR(fb); 3168 3169 return 0; 3170 } 3171 3172 /** 3173 * drm_mode_rmfb - remove an FB from the configuration 3174 * @dev: drm device for the ioctl 3175 * @data: data pointer for the ioctl 3176 * @file_priv: drm file for the ioctl call 3177 * 3178 * Remove the FB specified by the user. 3179 * 3180 * Called by the user via ioctl. 3181 * 3182 * Returns: 3183 * Zero on success, errno on failure. 3184 */ 3185 int drm_mode_rmfb(struct drm_device *dev, 3186 void *data, struct drm_file *file_priv) 3187 { 3188 struct drm_framebuffer *fb = NULL; 3189 struct drm_framebuffer *fbl = NULL; 3190 uint32_t *id = data; 3191 int found = 0; 3192 3193 if (!drm_core_check_feature(dev, DRIVER_MODESET)) 3194 return -EINVAL; 3195 3196 mutex_lock(&file_priv->fbs_lock); 3197 mutex_lock(&dev->mode_config.fb_lock); 3198 fb = __drm_framebuffer_lookup(dev, *id); 3199 if (!fb) 3200 goto fail_lookup; 3201 3202 list_for_each_entry(fbl, &file_priv->fbs, filp_head) 3203 if (fb == fbl) 3204 found = 1; 3205 if (!found) 3206 goto fail_lookup; 3207 3208 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */ 3209 __drm_framebuffer_unregister(dev, fb); 3210 3211 list_del_init(&fb->filp_head); 3212 mutex_unlock(&dev->mode_config.fb_lock); 3213 mutex_unlock(&file_priv->fbs_lock); 3214 3215 drm_framebuffer_remove(fb); 3216 3217 return 0; 3218 3219 fail_lookup: 3220 mutex_unlock(&dev->mode_config.fb_lock); 3221 mutex_unlock(&file_priv->fbs_lock); 3222 3223 return -ENOENT; 3224 } 3225 3226 /** 3227 * drm_mode_getfb - get FB info 3228 * @dev: drm device for the ioctl 3229 * @data: data pointer for the ioctl 3230 * @file_priv: drm file for the ioctl call 3231 * 3232 * Lookup the FB given its ID and return info about it. 3233 * 3234 * Called by the user via ioctl. 3235 * 3236 * Returns: 3237 * Zero on success, errno on failure. 3238 */ 3239 int drm_mode_getfb(struct drm_device *dev, 3240 void *data, struct drm_file *file_priv) 3241 { 3242 struct drm_mode_fb_cmd *r = data; 3243 struct drm_framebuffer *fb; 3244 int ret; 3245 3246 if (!drm_core_check_feature(dev, DRIVER_MODESET)) 3247 return -EINVAL; 3248 3249 fb = drm_framebuffer_lookup(dev, r->fb_id); 3250 if (!fb) 3251 return -ENOENT; 3252 3253 r->height = fb->height; 3254 r->width = fb->width; 3255 r->depth = fb->depth; 3256 r->bpp = fb->bits_per_pixel; 3257 r->pitch = fb->pitches[0]; 3258 if (fb->funcs->create_handle) { 3259 ret = fb->funcs->create_handle(fb, file_priv, &r->handle); 3260 } else { 3261 ret = -ENODEV; 3262 } 3263 3264 drm_framebuffer_unreference(fb); 3265 3266 return ret; 3267 } 3268 3269 /** 3270 * drm_mode_dirtyfb_ioctl - flush frontbuffer rendering on an FB 3271 * @dev: drm device for the ioctl 3272 * @data: data pointer for the ioctl 3273 * @file_priv: drm file for the ioctl call 3274 * 3275 * Lookup the FB and flush out the damaged area supplied by userspace as a clip 3276 * rectangle list. Generic userspace which does frontbuffer rendering must call 3277 * this ioctl to flush out the changes on manual-update display outputs, e.g. 3278 * usb display-link, mipi manual update panels or edp panel self refresh modes. 3279 * 3280 * Modesetting drivers which always update the frontbuffer do not need to 3281 * implement the corresponding ->dirty framebuffer callback. 3282 * 3283 * Called by the user via ioctl. 3284 * 3285 * Returns: 3286 * Zero on success, errno on failure. 3287 */ 3288 int drm_mode_dirtyfb_ioctl(struct drm_device *dev, 3289 void *data, struct drm_file *file_priv) 3290 { 3291 struct drm_clip_rect __user *clips_ptr; 3292 struct drm_clip_rect *clips = NULL; 3293 struct drm_mode_fb_dirty_cmd *r = data; 3294 struct drm_framebuffer *fb; 3295 unsigned flags; 3296 int num_clips; 3297 int ret; 3298 3299 if (!drm_core_check_feature(dev, DRIVER_MODESET)) 3300 return -EINVAL; 3301 3302 fb = drm_framebuffer_lookup(dev, r->fb_id); 3303 if (!fb) 3304 return -ENOENT; 3305 3306 num_clips = r->num_clips; 3307 clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr; 3308 3309 if (!num_clips != !clips_ptr) { 3310 ret = -EINVAL; 3311 goto out_err1; 3312 } 3313 3314 flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags; 3315 3316 /* If userspace annotates copy, clips must come in pairs */ 3317 if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) { 3318 ret = -EINVAL; 3319 goto out_err1; 3320 } 3321 3322 if (num_clips && clips_ptr) { 3323 if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) { 3324 ret = -EINVAL; 3325 goto out_err1; 3326 } 3327 clips = kzalloc(num_clips * sizeof(*clips), GFP_KERNEL); 3328 if (!clips) { 3329 ret = -ENOMEM; 3330 goto out_err1; 3331 } 3332 3333 ret = copy_from_user(clips, clips_ptr, 3334 num_clips * sizeof(*clips)); 3335 if (ret) { 3336 ret = -EFAULT; 3337 goto out_err2; 3338 } 3339 } 3340 3341 if (fb->funcs->dirty) { 3342 ret = fb->funcs->dirty(fb, file_priv, flags, r->color, 3343 clips, num_clips); 3344 } else { 3345 ret = -ENOSYS; 3346 } 3347 3348 out_err2: 3349 kfree(clips); 3350 out_err1: 3351 drm_framebuffer_unreference(fb); 3352 3353 return ret; 3354 } 3355 3356 3357 /** 3358 * drm_fb_release - remove and free the FBs on this file 3359 * @priv: drm file for the ioctl 3360 * 3361 * Destroy all the FBs associated with @filp. 3362 * 3363 * Called by the user via ioctl. 3364 * 3365 * Returns: 3366 * Zero on success, errno on failure. 3367 */ 3368 void drm_fb_release(struct drm_file *priv) 3369 { 3370 struct drm_device *dev = priv->dev; 3371 struct drm_framebuffer *fb, *tfb; 3372 3373 /* 3374 * When the file gets released that means no one else can access the fb 3375 * list any more, so no need to grab fpriv->fbs_lock. And we need to to 3376 * avoid upsetting lockdep since the universal cursor code adds a 3377 * framebuffer while holding mutex locks. 3378 * 3379 * Note that a real deadlock between fpriv->fbs_lock and the modeset 3380 * locks is impossible here since no one else but this function can get 3381 * at it any more. 3382 */ 3383 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) { 3384 3385 mutex_lock(&dev->mode_config.fb_lock); 3386 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */ 3387 __drm_framebuffer_unregister(dev, fb); 3388 mutex_unlock(&dev->mode_config.fb_lock); 3389 3390 list_del_init(&fb->filp_head); 3391 3392 /* This will also drop the fpriv->fbs reference. */ 3393 drm_framebuffer_remove(fb); 3394 } 3395 } 3396 3397 /** 3398 * drm_property_create - create a new property type 3399 * @dev: drm device 3400 * @flags: flags specifying the property type 3401 * @name: name of the property 3402 * @num_values: number of pre-defined values 3403 * 3404 * This creates a new generic drm property which can then be attached to a drm 3405 * object with drm_object_attach_property. The returned property object must be 3406 * freed with drm_property_destroy. 3407 * 3408 * Returns: 3409 * A pointer to the newly created property on success, NULL on failure. 3410 */ 3411 struct drm_property *drm_property_create(struct drm_device *dev, int flags, 3412 const char *name, int num_values) 3413 { 3414 struct drm_property *property = NULL; 3415 int ret; 3416 3417 property = kzalloc(sizeof(struct drm_property), GFP_KERNEL); 3418 if (!property) 3419 return NULL; 3420 3421 property->dev = dev; 3422 3423 if (num_values) { 3424 property->values = kzalloc(sizeof(uint64_t)*num_values, GFP_KERNEL); 3425 if (!property->values) 3426 goto fail; 3427 } 3428 3429 ret = drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY); 3430 if (ret) 3431 goto fail; 3432 3433 property->flags = flags; 3434 property->num_values = num_values; 3435 INIT_LIST_HEAD(&property->enum_blob_list); 3436 3437 if (name) { 3438 strncpy(property->name, name, DRM_PROP_NAME_LEN); 3439 property->name[DRM_PROP_NAME_LEN-1] = '\0'; 3440 } 3441 3442 list_add_tail(&property->head, &dev->mode_config.property_list); 3443 3444 WARN_ON(!drm_property_type_valid(property)); 3445 3446 return property; 3447 fail: 3448 kfree(property->values); 3449 kfree(property); 3450 return NULL; 3451 } 3452 EXPORT_SYMBOL(drm_property_create); 3453 3454 /** 3455 * drm_property_create_enum - create a new enumeration property type 3456 * @dev: drm device 3457 * @flags: flags specifying the property type 3458 * @name: name of the property 3459 * @props: enumeration lists with property values 3460 * @num_values: number of pre-defined values 3461 * 3462 * This creates a new generic drm property which can then be attached to a drm 3463 * object with drm_object_attach_property. The returned property object must be 3464 * freed with drm_property_destroy. 3465 * 3466 * Userspace is only allowed to set one of the predefined values for enumeration 3467 * properties. 3468 * 3469 * Returns: 3470 * A pointer to the newly created property on success, NULL on failure. 3471 */ 3472 struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags, 3473 const char *name, 3474 const struct drm_prop_enum_list *props, 3475 int num_values) 3476 { 3477 struct drm_property *property; 3478 int i, ret; 3479 3480 flags |= DRM_MODE_PROP_ENUM; 3481 3482 property = drm_property_create(dev, flags, name, num_values); 3483 if (!property) 3484 return NULL; 3485 3486 for (i = 0; i < num_values; i++) { 3487 ret = drm_property_add_enum(property, i, 3488 props[i].type, 3489 props[i].name); 3490 if (ret) { 3491 drm_property_destroy(dev, property); 3492 return NULL; 3493 } 3494 } 3495 3496 return property; 3497 } 3498 EXPORT_SYMBOL(drm_property_create_enum); 3499 3500 /** 3501 * drm_property_create_bitmask - create a new bitmask property type 3502 * @dev: drm device 3503 * @flags: flags specifying the property type 3504 * @name: name of the property 3505 * @props: enumeration lists with property bitflags 3506 * @num_props: size of the @props array 3507 * @supported_bits: bitmask of all supported enumeration values 3508 * 3509 * This creates a new bitmask drm property which can then be attached to a drm 3510 * object with drm_object_attach_property. The returned property object must be 3511 * freed with drm_property_destroy. 3512 * 3513 * Compared to plain enumeration properties userspace is allowed to set any 3514 * or'ed together combination of the predefined property bitflag values 3515 * 3516 * Returns: 3517 * A pointer to the newly created property on success, NULL on failure. 3518 */ 3519 struct drm_property *drm_property_create_bitmask(struct drm_device *dev, 3520 int flags, const char *name, 3521 const struct drm_prop_enum_list *props, 3522 int num_props, 3523 uint64_t supported_bits) 3524 { 3525 struct drm_property *property; 3526 int i, ret, index = 0; 3527 int num_values = hweight64(supported_bits); 3528 3529 flags |= DRM_MODE_PROP_BITMASK; 3530 3531 property = drm_property_create(dev, flags, name, num_values); 3532 if (!property) 3533 return NULL; 3534 for (i = 0; i < num_props; i++) { 3535 if (!(supported_bits & (1ULL << props[i].type))) 3536 continue; 3537 3538 if (WARN_ON(index >= num_values)) { 3539 drm_property_destroy(dev, property); 3540 return NULL; 3541 } 3542 3543 ret = drm_property_add_enum(property, index++, 3544 props[i].type, 3545 props[i].name); 3546 if (ret) { 3547 drm_property_destroy(dev, property); 3548 return NULL; 3549 } 3550 } 3551 3552 return property; 3553 } 3554 EXPORT_SYMBOL(drm_property_create_bitmask); 3555 3556 static struct drm_property *property_create_range(struct drm_device *dev, 3557 int flags, const char *name, 3558 uint64_t min, uint64_t max) 3559 { 3560 struct drm_property *property; 3561 3562 property = drm_property_create(dev, flags, name, 2); 3563 if (!property) 3564 return NULL; 3565 3566 property->values[0] = min; 3567 property->values[1] = max; 3568 3569 return property; 3570 } 3571 3572 /** 3573 * drm_property_create_range - create a new ranged property type 3574 * @dev: drm device 3575 * @flags: flags specifying the property type 3576 * @name: name of the property 3577 * @min: minimum value of the property 3578 * @max: maximum value of the property 3579 * 3580 * This creates a new generic drm property which can then be attached to a drm 3581 * object with drm_object_attach_property. The returned property object must be 3582 * freed with drm_property_destroy. 3583 * 3584 * Userspace is allowed to set any interger value in the (min, max) range 3585 * inclusive. 3586 * 3587 * Returns: 3588 * A pointer to the newly created property on success, NULL on failure. 3589 */ 3590 struct drm_property *drm_property_create_range(struct drm_device *dev, int flags, 3591 const char *name, 3592 uint64_t min, uint64_t max) 3593 { 3594 return property_create_range(dev, DRM_MODE_PROP_RANGE | flags, 3595 name, min, max); 3596 } 3597 EXPORT_SYMBOL(drm_property_create_range); 3598 3599 struct drm_property *drm_property_create_signed_range(struct drm_device *dev, 3600 int flags, const char *name, 3601 int64_t min, int64_t max) 3602 { 3603 return property_create_range(dev, DRM_MODE_PROP_SIGNED_RANGE | flags, 3604 name, I642U64(min), I642U64(max)); 3605 } 3606 EXPORT_SYMBOL(drm_property_create_signed_range); 3607 3608 struct drm_property *drm_property_create_object(struct drm_device *dev, 3609 int flags, const char *name, uint32_t type) 3610 { 3611 struct drm_property *property; 3612 3613 flags |= DRM_MODE_PROP_OBJECT; 3614 3615 property = drm_property_create(dev, flags, name, 1); 3616 if (!property) 3617 return NULL; 3618 3619 property->values[0] = type; 3620 3621 return property; 3622 } 3623 EXPORT_SYMBOL(drm_property_create_object); 3624 3625 /** 3626 * drm_property_add_enum - add a possible value to an enumeration property 3627 * @property: enumeration property to change 3628 * @index: index of the new enumeration 3629 * @value: value of the new enumeration 3630 * @name: symbolic name of the new enumeration 3631 * 3632 * This functions adds enumerations to a property. 3633 * 3634 * It's use is deprecated, drivers should use one of the more specific helpers 3635 * to directly create the property with all enumerations already attached. 3636 * 3637 * Returns: 3638 * Zero on success, error code on failure. 3639 */ 3640 int drm_property_add_enum(struct drm_property *property, int index, 3641 uint64_t value, const char *name) 3642 { 3643 struct drm_property_enum *prop_enum; 3644 3645 if (!(drm_property_type_is(property, DRM_MODE_PROP_ENUM) || 3646 drm_property_type_is(property, DRM_MODE_PROP_BITMASK))) 3647 return -EINVAL; 3648 3649 /* 3650 * Bitmask enum properties have the additional constraint of values 3651 * from 0 to 63 3652 */ 3653 if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK) && 3654 (value > 63)) 3655 return -EINVAL; 3656 3657 if (!list_empty(&property->enum_blob_list)) { 3658 list_for_each_entry(prop_enum, &property->enum_blob_list, head) { 3659 if (prop_enum->value == value) { 3660 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN); 3661 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0'; 3662 return 0; 3663 } 3664 } 3665 } 3666 3667 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL); 3668 if (!prop_enum) 3669 return -ENOMEM; 3670 3671 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN); 3672 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0'; 3673 prop_enum->value = value; 3674 3675 property->values[index] = value; 3676 list_add_tail(&prop_enum->head, &property->enum_blob_list); 3677 return 0; 3678 } 3679 EXPORT_SYMBOL(drm_property_add_enum); 3680 3681 /** 3682 * drm_property_destroy - destroy a drm property 3683 * @dev: drm device 3684 * @property: property to destry 3685 * 3686 * This function frees a property including any attached resources like 3687 * enumeration values. 3688 */ 3689 void drm_property_destroy(struct drm_device *dev, struct drm_property *property) 3690 { 3691 struct drm_property_enum *prop_enum, *pt; 3692 3693 list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) { 3694 list_del(&prop_enum->head); 3695 kfree(prop_enum); 3696 } 3697 3698 if (property->num_values) 3699 kfree(property->values); 3700 drm_mode_object_put(dev, &property->base); 3701 list_del(&property->head); 3702 kfree(property); 3703 } 3704 EXPORT_SYMBOL(drm_property_destroy); 3705 3706 /** 3707 * drm_object_attach_property - attach a property to a modeset object 3708 * @obj: drm modeset object 3709 * @property: property to attach 3710 * @init_val: initial value of the property 3711 * 3712 * This attaches the given property to the modeset object with the given initial 3713 * value. Currently this function cannot fail since the properties are stored in 3714 * a statically sized array. 3715 */ 3716 void drm_object_attach_property(struct drm_mode_object *obj, 3717 struct drm_property *property, 3718 uint64_t init_val) 3719 { 3720 int count = obj->properties->count; 3721 3722 if (count == DRM_OBJECT_MAX_PROPERTY) { 3723 WARN(1, "Failed to attach object property (type: 0x%x). Please " 3724 "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time " 3725 "you see this message on the same object type.\n", 3726 obj->type); 3727 return; 3728 } 3729 3730 obj->properties->ids[count] = property->base.id; 3731 obj->properties->values[count] = init_val; 3732 obj->properties->count++; 3733 } 3734 EXPORT_SYMBOL(drm_object_attach_property); 3735 3736 /** 3737 * drm_object_property_set_value - set the value of a property 3738 * @obj: drm mode object to set property value for 3739 * @property: property to set 3740 * @val: value the property should be set to 3741 * 3742 * This functions sets a given property on a given object. This function only 3743 * changes the software state of the property, it does not call into the 3744 * driver's ->set_property callback. 3745 * 3746 * Returns: 3747 * Zero on success, error code on failure. 3748 */ 3749 int drm_object_property_set_value(struct drm_mode_object *obj, 3750 struct drm_property *property, uint64_t val) 3751 { 3752 int i; 3753 3754 for (i = 0; i < obj->properties->count; i++) { 3755 if (obj->properties->ids[i] == property->base.id) { 3756 obj->properties->values[i] = val; 3757 return 0; 3758 } 3759 } 3760 3761 return -EINVAL; 3762 } 3763 EXPORT_SYMBOL(drm_object_property_set_value); 3764 3765 /** 3766 * drm_object_property_get_value - retrieve the value of a property 3767 * @obj: drm mode object to get property value from 3768 * @property: property to retrieve 3769 * @val: storage for the property value 3770 * 3771 * This function retrieves the softare state of the given property for the given 3772 * property. Since there is no driver callback to retrieve the current property 3773 * value this might be out of sync with the hardware, depending upon the driver 3774 * and property. 3775 * 3776 * Returns: 3777 * Zero on success, error code on failure. 3778 */ 3779 int drm_object_property_get_value(struct drm_mode_object *obj, 3780 struct drm_property *property, uint64_t *val) 3781 { 3782 int i; 3783 3784 for (i = 0; i < obj->properties->count; i++) { 3785 if (obj->properties->ids[i] == property->base.id) { 3786 *val = obj->properties->values[i]; 3787 return 0; 3788 } 3789 } 3790 3791 return -EINVAL; 3792 } 3793 EXPORT_SYMBOL(drm_object_property_get_value); 3794 3795 /** 3796 * drm_mode_getproperty_ioctl - get the current value of a connector's property 3797 * @dev: DRM device 3798 * @data: ioctl data 3799 * @file_priv: DRM file info 3800 * 3801 * This function retrieves the current value for an connectors's property. 3802 * 3803 * Called by the user via ioctl. 3804 * 3805 * Returns: 3806 * Zero on success, errno on failure. 3807 */ 3808 int drm_mode_getproperty_ioctl(struct drm_device *dev, 3809 void *data, struct drm_file *file_priv) 3810 { 3811 struct drm_mode_get_property *out_resp = data; 3812 struct drm_property *property; 3813 int enum_count = 0; 3814 int blob_count = 0; 3815 int value_count = 0; 3816 int ret = 0, i; 3817 int copied; 3818 struct drm_property_enum *prop_enum; 3819 struct drm_mode_property_enum __user *enum_ptr; 3820 struct drm_property_blob *prop_blob; 3821 uint32_t __user *blob_id_ptr; 3822 uint64_t __user *values_ptr; 3823 uint32_t __user *blob_length_ptr; 3824 3825 if (!drm_core_check_feature(dev, DRIVER_MODESET)) 3826 return -EINVAL; 3827 3828 drm_modeset_lock_all(dev); 3829 property = drm_property_find(dev, out_resp->prop_id); 3830 if (!property) { 3831 ret = -ENOENT; 3832 goto done; 3833 } 3834 3835 if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) || 3836 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) { 3837 list_for_each_entry(prop_enum, &property->enum_blob_list, head) 3838 enum_count++; 3839 } else if (drm_property_type_is(property, DRM_MODE_PROP_BLOB)) { 3840 list_for_each_entry(prop_blob, &property->enum_blob_list, head) 3841 blob_count++; 3842 } 3843 3844 value_count = property->num_values; 3845 3846 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN); 3847 out_resp->name[DRM_PROP_NAME_LEN-1] = 0; 3848 out_resp->flags = property->flags; 3849 3850 if ((out_resp->count_values >= value_count) && value_count) { 3851 values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr; 3852 for (i = 0; i < value_count; i++) { 3853 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) { 3854 ret = -EFAULT; 3855 goto done; 3856 } 3857 } 3858 } 3859 out_resp->count_values = value_count; 3860 3861 if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) || 3862 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) { 3863 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) { 3864 copied = 0; 3865 enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr; 3866 list_for_each_entry(prop_enum, &property->enum_blob_list, head) { 3867 3868 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) { 3869 ret = -EFAULT; 3870 goto done; 3871 } 3872 3873 if (copy_to_user(&enum_ptr[copied].name, 3874 &prop_enum->name, DRM_PROP_NAME_LEN)) { 3875 ret = -EFAULT; 3876 goto done; 3877 } 3878 copied++; 3879 } 3880 } 3881 out_resp->count_enum_blobs = enum_count; 3882 } 3883 3884 if (drm_property_type_is(property, DRM_MODE_PROP_BLOB)) { 3885 if ((out_resp->count_enum_blobs >= blob_count) && blob_count) { 3886 copied = 0; 3887 blob_id_ptr = (uint32_t __user *)(unsigned long)out_resp->enum_blob_ptr; 3888 blob_length_ptr = (uint32_t __user *)(unsigned long)out_resp->values_ptr; 3889 3890 list_for_each_entry(prop_blob, &property->enum_blob_list, head) { 3891 if (put_user(prop_blob->base.id, blob_id_ptr + copied)) { 3892 ret = -EFAULT; 3893 goto done; 3894 } 3895 3896 if (put_user(prop_blob->length, blob_length_ptr + copied)) { 3897 ret = -EFAULT; 3898 goto done; 3899 } 3900 3901 copied++; 3902 } 3903 } 3904 out_resp->count_enum_blobs = blob_count; 3905 } 3906 done: 3907 drm_modeset_unlock_all(dev); 3908 return ret; 3909 } 3910 3911 static struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length, 3912 void *data) 3913 { 3914 struct drm_property_blob *blob; 3915 int ret; 3916 3917 if (!length || !data) 3918 return NULL; 3919 3920 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL); 3921 if (!blob) 3922 return NULL; 3923 3924 ret = drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB); 3925 if (ret) { 3926 kfree(blob); 3927 return NULL; 3928 } 3929 3930 blob->length = length; 3931 3932 memcpy(blob->data, data, length); 3933 3934 list_add_tail(&blob->head, &dev->mode_config.property_blob_list); 3935 return blob; 3936 } 3937 3938 static void drm_property_destroy_blob(struct drm_device *dev, 3939 struct drm_property_blob *blob) 3940 { 3941 drm_mode_object_put(dev, &blob->base); 3942 list_del(&blob->head); 3943 kfree(blob); 3944 } 3945 3946 /** 3947 * drm_mode_getblob_ioctl - get the contents of a blob property value 3948 * @dev: DRM device 3949 * @data: ioctl data 3950 * @file_priv: DRM file info 3951 * 3952 * This function retrieves the contents of a blob property. The value stored in 3953 * an object's blob property is just a normal modeset object id. 3954 * 3955 * Called by the user via ioctl. 3956 * 3957 * Returns: 3958 * Zero on success, errno on failure. 3959 */ 3960 int drm_mode_getblob_ioctl(struct drm_device *dev, 3961 void *data, struct drm_file *file_priv) 3962 { 3963 struct drm_mode_get_blob *out_resp = data; 3964 struct drm_property_blob *blob; 3965 int ret = 0; 3966 void __user *blob_ptr; 3967 3968 if (!drm_core_check_feature(dev, DRIVER_MODESET)) 3969 return -EINVAL; 3970 3971 drm_modeset_lock_all(dev); 3972 blob = drm_property_blob_find(dev, out_resp->blob_id); 3973 if (!blob) { 3974 ret = -ENOENT; 3975 goto done; 3976 } 3977 3978 if (out_resp->length == blob->length) { 3979 blob_ptr = (void __user *)(unsigned long)out_resp->data; 3980 if (copy_to_user(blob_ptr, blob->data, blob->length)){ 3981 ret = -EFAULT; 3982 goto done; 3983 } 3984 } 3985 out_resp->length = blob->length; 3986 3987 done: 3988 drm_modeset_unlock_all(dev); 3989 return ret; 3990 } 3991 3992 int drm_mode_connector_set_path_property(struct drm_connector *connector, 3993 char *path) 3994 { 3995 struct drm_device *dev = connector->dev; 3996 int ret, size; 3997 size = strlen(path) + 1; 3998 3999 connector->path_blob_ptr = drm_property_create_blob(connector->dev, 4000 size, path); 4001 if (!connector->path_blob_ptr) 4002 return -EINVAL; 4003 4004 ret = drm_object_property_set_value(&connector->base, 4005 dev->mode_config.path_property, 4006 connector->path_blob_ptr->base.id); 4007 return ret; 4008 } 4009 EXPORT_SYMBOL(drm_mode_connector_set_path_property); 4010 4011 /** 4012 * drm_mode_connector_update_edid_property - update the edid property of a connector 4013 * @connector: drm connector 4014 * @edid: new value of the edid property 4015 * 4016 * This function creates a new blob modeset object and assigns its id to the 4017 * connector's edid property. 4018 * 4019 * Returns: 4020 * Zero on success, errno on failure. 4021 */ 4022 int drm_mode_connector_update_edid_property(struct drm_connector *connector, 4023 struct edid *edid) 4024 { 4025 struct drm_device *dev = connector->dev; 4026 int ret, size; 4027 4028 /* ignore requests to set edid when overridden */ 4029 if (connector->override_edid) 4030 return 0; 4031 4032 if (connector->edid_blob_ptr) 4033 drm_property_destroy_blob(dev, connector->edid_blob_ptr); 4034 4035 /* Delete edid, when there is none. */ 4036 if (!edid) { 4037 connector->edid_blob_ptr = NULL; 4038 ret = drm_object_property_set_value(&connector->base, dev->mode_config.edid_property, 0); 4039 return ret; 4040 } 4041 4042 size = EDID_LENGTH * (1 + edid->extensions); 4043 connector->edid_blob_ptr = drm_property_create_blob(connector->dev, 4044 size, edid); 4045 if (!connector->edid_blob_ptr) 4046 return -EINVAL; 4047 4048 ret = drm_object_property_set_value(&connector->base, 4049 dev->mode_config.edid_property, 4050 connector->edid_blob_ptr->base.id); 4051 4052 return ret; 4053 } 4054 EXPORT_SYMBOL(drm_mode_connector_update_edid_property); 4055 4056 static bool drm_property_change_is_valid(struct drm_property *property, 4057 uint64_t value) 4058 { 4059 if (property->flags & DRM_MODE_PROP_IMMUTABLE) 4060 return false; 4061 4062 if (drm_property_type_is(property, DRM_MODE_PROP_RANGE)) { 4063 if (value < property->values[0] || value > property->values[1]) 4064 return false; 4065 return true; 4066 } else if (drm_property_type_is(property, DRM_MODE_PROP_SIGNED_RANGE)) { 4067 int64_t svalue = U642I64(value); 4068 if (svalue < U642I64(property->values[0]) || 4069 svalue > U642I64(property->values[1])) 4070 return false; 4071 return true; 4072 } else if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) { 4073 int i; 4074 uint64_t valid_mask = 0; 4075 for (i = 0; i < property->num_values; i++) 4076 valid_mask |= (1ULL << property->values[i]); 4077 return !(value & ~valid_mask); 4078 } else if (drm_property_type_is(property, DRM_MODE_PROP_BLOB)) { 4079 /* Only the driver knows */ 4080 return true; 4081 } else if (drm_property_type_is(property, DRM_MODE_PROP_OBJECT)) { 4082 struct drm_mode_object *obj; 4083 /* a zero value for an object property translates to null: */ 4084 if (value == 0) 4085 return true; 4086 /* 4087 * NOTE: use _object_find() directly to bypass restriction on 4088 * looking up refcnt'd objects (ie. fb's). For a refcnt'd 4089 * object this could race against object finalization, so it 4090 * simply tells us that the object *was* valid. Which is good 4091 * enough. 4092 */ 4093 obj = _object_find(property->dev, value, property->values[0]); 4094 return obj != NULL; 4095 } else { 4096 int i; 4097 for (i = 0; i < property->num_values; i++) 4098 if (property->values[i] == value) 4099 return true; 4100 return false; 4101 } 4102 } 4103 4104 /** 4105 * drm_mode_connector_property_set_ioctl - set the current value of a connector property 4106 * @dev: DRM device 4107 * @data: ioctl data 4108 * @file_priv: DRM file info 4109 * 4110 * This function sets the current value for a connectors's property. It also 4111 * calls into a driver's ->set_property callback to update the hardware state 4112 * 4113 * Called by the user via ioctl. 4114 * 4115 * Returns: 4116 * Zero on success, errno on failure. 4117 */ 4118 int drm_mode_connector_property_set_ioctl(struct drm_device *dev, 4119 void *data, struct drm_file *file_priv) 4120 { 4121 struct drm_mode_connector_set_property *conn_set_prop = data; 4122 struct drm_mode_obj_set_property obj_set_prop = { 4123 .value = conn_set_prop->value, 4124 .prop_id = conn_set_prop->prop_id, 4125 .obj_id = conn_set_prop->connector_id, 4126 .obj_type = DRM_MODE_OBJECT_CONNECTOR 4127 }; 4128 4129 /* It does all the locking and checking we need */ 4130 return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv); 4131 } 4132 4133 static int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj, 4134 struct drm_property *property, 4135 uint64_t value) 4136 { 4137 int ret = -EINVAL; 4138 struct drm_connector *connector = obj_to_connector(obj); 4139 4140 /* Do DPMS ourselves */ 4141 if (property == connector->dev->mode_config.dpms_property) { 4142 if (connector->funcs->dpms) 4143 (*connector->funcs->dpms)(connector, (int)value); 4144 ret = 0; 4145 } else if (connector->funcs->set_property) 4146 ret = connector->funcs->set_property(connector, property, value); 4147 4148 /* store the property value if successful */ 4149 if (!ret) 4150 drm_object_property_set_value(&connector->base, property, value); 4151 return ret; 4152 } 4153 4154 static int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj, 4155 struct drm_property *property, 4156 uint64_t value) 4157 { 4158 int ret = -EINVAL; 4159 struct drm_crtc *crtc = obj_to_crtc(obj); 4160 4161 if (crtc->funcs->set_property) 4162 ret = crtc->funcs->set_property(crtc, property, value); 4163 if (!ret) 4164 drm_object_property_set_value(obj, property, value); 4165 4166 return ret; 4167 } 4168 4169 /** 4170 * drm_mode_plane_set_obj_prop - set the value of a property 4171 * @plane: drm plane object to set property value for 4172 * @property: property to set 4173 * @value: value the property should be set to 4174 * 4175 * This functions sets a given property on a given plane object. This function 4176 * calls the driver's ->set_property callback and changes the software state of 4177 * the property if the callback succeeds. 4178 * 4179 * Returns: 4180 * Zero on success, error code on failure. 4181 */ 4182 int drm_mode_plane_set_obj_prop(struct drm_plane *plane, 4183 struct drm_property *property, 4184 uint64_t value) 4185 { 4186 int ret = -EINVAL; 4187 struct drm_mode_object *obj = &plane->base; 4188 4189 if (plane->funcs->set_property) 4190 ret = plane->funcs->set_property(plane, property, value); 4191 if (!ret) 4192 drm_object_property_set_value(obj, property, value); 4193 4194 return ret; 4195 } 4196 EXPORT_SYMBOL(drm_mode_plane_set_obj_prop); 4197 4198 /** 4199 * drm_mode_getproperty_ioctl - get the current value of a object's property 4200 * @dev: DRM device 4201 * @data: ioctl data 4202 * @file_priv: DRM file info 4203 * 4204 * This function retrieves the current value for an object's property. Compared 4205 * to the connector specific ioctl this one is extended to also work on crtc and 4206 * plane objects. 4207 * 4208 * Called by the user via ioctl. 4209 * 4210 * Returns: 4211 * Zero on success, errno on failure. 4212 */ 4213 int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data, 4214 struct drm_file *file_priv) 4215 { 4216 struct drm_mode_obj_get_properties *arg = data; 4217 struct drm_mode_object *obj; 4218 int ret = 0; 4219 int i; 4220 int copied = 0; 4221 int props_count = 0; 4222 uint32_t __user *props_ptr; 4223 uint64_t __user *prop_values_ptr; 4224 4225 if (!drm_core_check_feature(dev, DRIVER_MODESET)) 4226 return -EINVAL; 4227 4228 drm_modeset_lock_all(dev); 4229 4230 obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type); 4231 if (!obj) { 4232 ret = -ENOENT; 4233 goto out; 4234 } 4235 if (!obj->properties) { 4236 ret = -EINVAL; 4237 goto out; 4238 } 4239 4240 props_count = obj->properties->count; 4241 4242 /* This ioctl is called twice, once to determine how much space is 4243 * needed, and the 2nd time to fill it. */ 4244 if ((arg->count_props >= props_count) && props_count) { 4245 copied = 0; 4246 props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr); 4247 prop_values_ptr = (uint64_t __user *)(unsigned long) 4248 (arg->prop_values_ptr); 4249 for (i = 0; i < props_count; i++) { 4250 if (put_user(obj->properties->ids[i], 4251 props_ptr + copied)) { 4252 ret = -EFAULT; 4253 goto out; 4254 } 4255 if (put_user(obj->properties->values[i], 4256 prop_values_ptr + copied)) { 4257 ret = -EFAULT; 4258 goto out; 4259 } 4260 copied++; 4261 } 4262 } 4263 arg->count_props = props_count; 4264 out: 4265 drm_modeset_unlock_all(dev); 4266 return ret; 4267 } 4268 4269 /** 4270 * drm_mode_obj_set_property_ioctl - set the current value of an object's property 4271 * @dev: DRM device 4272 * @data: ioctl data 4273 * @file_priv: DRM file info 4274 * 4275 * This function sets the current value for an object's property. It also calls 4276 * into a driver's ->set_property callback to update the hardware state. 4277 * Compared to the connector specific ioctl this one is extended to also work on 4278 * crtc and plane objects. 4279 * 4280 * Called by the user via ioctl. 4281 * 4282 * Returns: 4283 * Zero on success, errno on failure. 4284 */ 4285 int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data, 4286 struct drm_file *file_priv) 4287 { 4288 struct drm_mode_obj_set_property *arg = data; 4289 struct drm_mode_object *arg_obj; 4290 struct drm_mode_object *prop_obj; 4291 struct drm_property *property; 4292 int ret = -EINVAL; 4293 int i; 4294 4295 if (!drm_core_check_feature(dev, DRIVER_MODESET)) 4296 return -EINVAL; 4297 4298 drm_modeset_lock_all(dev); 4299 4300 arg_obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type); 4301 if (!arg_obj) { 4302 ret = -ENOENT; 4303 goto out; 4304 } 4305 if (!arg_obj->properties) 4306 goto out; 4307 4308 for (i = 0; i < arg_obj->properties->count; i++) 4309 if (arg_obj->properties->ids[i] == arg->prop_id) 4310 break; 4311 4312 if (i == arg_obj->properties->count) 4313 goto out; 4314 4315 prop_obj = drm_mode_object_find(dev, arg->prop_id, 4316 DRM_MODE_OBJECT_PROPERTY); 4317 if (!prop_obj) { 4318 ret = -ENOENT; 4319 goto out; 4320 } 4321 property = obj_to_property(prop_obj); 4322 4323 if (!drm_property_change_is_valid(property, arg->value)) 4324 goto out; 4325 4326 switch (arg_obj->type) { 4327 case DRM_MODE_OBJECT_CONNECTOR: 4328 ret = drm_mode_connector_set_obj_prop(arg_obj, property, 4329 arg->value); 4330 break; 4331 case DRM_MODE_OBJECT_CRTC: 4332 ret = drm_mode_crtc_set_obj_prop(arg_obj, property, arg->value); 4333 break; 4334 case DRM_MODE_OBJECT_PLANE: 4335 ret = drm_mode_plane_set_obj_prop(obj_to_plane(arg_obj), 4336 property, arg->value); 4337 break; 4338 } 4339 4340 out: 4341 drm_modeset_unlock_all(dev); 4342 return ret; 4343 } 4344 4345 /** 4346 * drm_mode_connector_attach_encoder - attach a connector to an encoder 4347 * @connector: connector to attach 4348 * @encoder: encoder to attach @connector to 4349 * 4350 * This function links up a connector to an encoder. Note that the routing 4351 * restrictions between encoders and crtcs are exposed to userspace through the 4352 * possible_clones and possible_crtcs bitmasks. 4353 * 4354 * Returns: 4355 * Zero on success, errno on failure. 4356 */ 4357 int drm_mode_connector_attach_encoder(struct drm_connector *connector, 4358 struct drm_encoder *encoder) 4359 { 4360 int i; 4361 4362 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) { 4363 if (connector->encoder_ids[i] == 0) { 4364 connector->encoder_ids[i] = encoder->base.id; 4365 return 0; 4366 } 4367 } 4368 return -ENOMEM; 4369 } 4370 EXPORT_SYMBOL(drm_mode_connector_attach_encoder); 4371 4372 /** 4373 * drm_mode_crtc_set_gamma_size - set the gamma table size 4374 * @crtc: CRTC to set the gamma table size for 4375 * @gamma_size: size of the gamma table 4376 * 4377 * Drivers which support gamma tables should set this to the supported gamma 4378 * table size when initializing the CRTC. Currently the drm core only supports a 4379 * fixed gamma table size. 4380 * 4381 * Returns: 4382 * Zero on success, errno on failure. 4383 */ 4384 int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc, 4385 int gamma_size) 4386 { 4387 crtc->gamma_size = gamma_size; 4388 4389 crtc->gamma_store = kzalloc(gamma_size * sizeof(uint16_t) * 3, GFP_KERNEL); 4390 if (!crtc->gamma_store) { 4391 crtc->gamma_size = 0; 4392 return -ENOMEM; 4393 } 4394 4395 return 0; 4396 } 4397 EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size); 4398 4399 /** 4400 * drm_mode_gamma_set_ioctl - set the gamma table 4401 * @dev: DRM device 4402 * @data: ioctl data 4403 * @file_priv: DRM file info 4404 * 4405 * Set the gamma table of a CRTC to the one passed in by the user. Userspace can 4406 * inquire the required gamma table size through drm_mode_gamma_get_ioctl. 4407 * 4408 * Called by the user via ioctl. 4409 * 4410 * Returns: 4411 * Zero on success, errno on failure. 4412 */ 4413 int drm_mode_gamma_set_ioctl(struct drm_device *dev, 4414 void *data, struct drm_file *file_priv) 4415 { 4416 struct drm_mode_crtc_lut *crtc_lut = data; 4417 struct drm_crtc *crtc; 4418 void *r_base, *g_base, *b_base; 4419 int size; 4420 int ret = 0; 4421 4422 if (!drm_core_check_feature(dev, DRIVER_MODESET)) 4423 return -EINVAL; 4424 4425 drm_modeset_lock_all(dev); 4426 crtc = drm_crtc_find(dev, crtc_lut->crtc_id); 4427 if (!crtc) { 4428 ret = -ENOENT; 4429 goto out; 4430 } 4431 4432 if (crtc->funcs->gamma_set == NULL) { 4433 ret = -ENOSYS; 4434 goto out; 4435 } 4436 4437 /* memcpy into gamma store */ 4438 if (crtc_lut->gamma_size != crtc->gamma_size) { 4439 ret = -EINVAL; 4440 goto out; 4441 } 4442 4443 size = crtc_lut->gamma_size * (sizeof(uint16_t)); 4444 r_base = crtc->gamma_store; 4445 if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) { 4446 ret = -EFAULT; 4447 goto out; 4448 } 4449 4450 g_base = (char *)r_base + size; 4451 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) { 4452 ret = -EFAULT; 4453 goto out; 4454 } 4455 4456 b_base = (char *)g_base + size; 4457 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) { 4458 ret = -EFAULT; 4459 goto out; 4460 } 4461 4462 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size); 4463 4464 out: 4465 drm_modeset_unlock_all(dev); 4466 return ret; 4467 4468 } 4469 4470 /** 4471 * drm_mode_gamma_get_ioctl - get the gamma table 4472 * @dev: DRM device 4473 * @data: ioctl data 4474 * @file_priv: DRM file info 4475 * 4476 * Copy the current gamma table into the storage provided. This also provides 4477 * the gamma table size the driver expects, which can be used to size the 4478 * allocated storage. 4479 * 4480 * Called by the user via ioctl. 4481 * 4482 * Returns: 4483 * Zero on success, errno on failure. 4484 */ 4485 int drm_mode_gamma_get_ioctl(struct drm_device *dev, 4486 void *data, struct drm_file *file_priv) 4487 { 4488 struct drm_mode_crtc_lut *crtc_lut = data; 4489 struct drm_crtc *crtc; 4490 void *r_base, *g_base, *b_base; 4491 int size; 4492 int ret = 0; 4493 4494 if (!drm_core_check_feature(dev, DRIVER_MODESET)) 4495 return -EINVAL; 4496 4497 drm_modeset_lock_all(dev); 4498 crtc = drm_crtc_find(dev, crtc_lut->crtc_id); 4499 if (!crtc) { 4500 ret = -ENOENT; 4501 goto out; 4502 } 4503 4504 /* memcpy into gamma store */ 4505 if (crtc_lut->gamma_size != crtc->gamma_size) { 4506 ret = -EINVAL; 4507 goto out; 4508 } 4509 4510 size = crtc_lut->gamma_size * (sizeof(uint16_t)); 4511 r_base = crtc->gamma_store; 4512 if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) { 4513 ret = -EFAULT; 4514 goto out; 4515 } 4516 4517 g_base = (char *)r_base + size; 4518 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) { 4519 ret = -EFAULT; 4520 goto out; 4521 } 4522 4523 b_base = (char *)g_base + size; 4524 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) { 4525 ret = -EFAULT; 4526 goto out; 4527 } 4528 out: 4529 drm_modeset_unlock_all(dev); 4530 return ret; 4531 } 4532 4533 /* 4534 * The Linux version of kfree() is a macro and can't be called 4535 * directly via a function pointer 4536 */ 4537 static void drm_kms_free(void *arg) 4538 { 4539 kfree(arg); 4540 } 4541 4542 /** 4543 * drm_mode_page_flip_ioctl - schedule an asynchronous fb update 4544 * @dev: DRM device 4545 * @data: ioctl data 4546 * @file_priv: DRM file info 4547 * 4548 * This schedules an asynchronous update on a given CRTC, called page flip. 4549 * Optionally a drm event is generated to signal the completion of the event. 4550 * Generic drivers cannot assume that a pageflip with changed framebuffer 4551 * properties (including driver specific metadata like tiling layout) will work, 4552 * but some drivers support e.g. pixel format changes through the pageflip 4553 * ioctl. 4554 * 4555 * Called by the user via ioctl. 4556 * 4557 * Returns: 4558 * Zero on success, errno on failure. 4559 */ 4560 int drm_mode_page_flip_ioctl(struct drm_device *dev, 4561 void *data, struct drm_file *file_priv) 4562 { 4563 struct drm_mode_crtc_page_flip *page_flip = data; 4564 struct drm_crtc *crtc; 4565 struct drm_framebuffer *fb = NULL; 4566 struct drm_pending_vblank_event *e = NULL; 4567 int ret = -EINVAL; 4568 4569 if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS || 4570 page_flip->reserved != 0) 4571 return -EINVAL; 4572 4573 if ((page_flip->flags & DRM_MODE_PAGE_FLIP_ASYNC) && !dev->mode_config.async_page_flip) 4574 return -EINVAL; 4575 4576 crtc = drm_crtc_find(dev, page_flip->crtc_id); 4577 if (!crtc) 4578 return -ENOENT; 4579 4580 drm_modeset_lock_crtc(crtc); 4581 if (crtc->primary->fb == NULL) { 4582 /* The framebuffer is currently unbound, presumably 4583 * due to a hotplug event, that userspace has not 4584 * yet discovered. 4585 */ 4586 ret = -EBUSY; 4587 goto out; 4588 } 4589 4590 if (crtc->funcs->page_flip == NULL) 4591 goto out; 4592 4593 fb = drm_framebuffer_lookup(dev, page_flip->fb_id); 4594 if (!fb) { 4595 ret = -ENOENT; 4596 goto out; 4597 } 4598 4599 ret = drm_crtc_check_viewport(crtc, crtc->x, crtc->y, &crtc->mode, fb); 4600 if (ret) 4601 goto out; 4602 4603 if (crtc->primary->fb->pixel_format != fb->pixel_format) { 4604 DRM_DEBUG_KMS("Page flip is not allowed to change frame buffer format.\n"); 4605 ret = -EINVAL; 4606 goto out; 4607 } 4608 4609 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) { 4610 ret = -ENOMEM; 4611 lockmgr(&dev->event_lock, LK_EXCLUSIVE); 4612 if (file_priv->event_space < sizeof e->event) { 4613 lockmgr(&dev->event_lock, LK_RELEASE); 4614 goto out; 4615 } 4616 file_priv->event_space -= sizeof e->event; 4617 lockmgr(&dev->event_lock, LK_RELEASE); 4618 4619 e = kzalloc(sizeof *e, GFP_KERNEL); 4620 if (e == NULL) { 4621 lockmgr(&dev->event_lock, LK_EXCLUSIVE); 4622 file_priv->event_space += sizeof e->event; 4623 lockmgr(&dev->event_lock, LK_RELEASE); 4624 goto out; 4625 } 4626 4627 e->event.base.type = DRM_EVENT_FLIP_COMPLETE; 4628 e->event.base.length = sizeof e->event; 4629 e->event.user_data = page_flip->user_data; 4630 e->base.event = &e->event.base; 4631 e->base.file_priv = file_priv; 4632 #ifdef __DragonFly__ 4633 e->base.destroy = 4634 (void (*) (struct drm_pending_event *)) drm_kms_free; 4635 #else 4636 e->base.destroy = 4637 (void (*) (struct drm_pending_event *)) kfree; 4638 #endif 4639 } 4640 4641 crtc->primary->old_fb = crtc->primary->fb; 4642 ret = crtc->funcs->page_flip(crtc, fb, e, page_flip->flags); 4643 if (ret) { 4644 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) { 4645 lockmgr(&dev->event_lock, LK_EXCLUSIVE); 4646 file_priv->event_space += sizeof e->event; 4647 lockmgr(&dev->event_lock, LK_RELEASE); 4648 kfree(e); 4649 } 4650 /* Keep the old fb, don't unref it. */ 4651 crtc->primary->old_fb = NULL; 4652 } else { 4653 /* 4654 * Warn if the driver hasn't properly updated the crtc->fb 4655 * field to reflect that the new framebuffer is now used. 4656 * Failing to do so will screw with the reference counting 4657 * on framebuffers. 4658 */ 4659 WARN_ON(crtc->primary->fb != fb); 4660 /* Unref only the old framebuffer. */ 4661 fb = NULL; 4662 } 4663 4664 out: 4665 if (fb) 4666 drm_framebuffer_unreference(fb); 4667 if (crtc->primary->old_fb) 4668 drm_framebuffer_unreference(crtc->primary->old_fb); 4669 crtc->primary->old_fb = NULL; 4670 drm_modeset_unlock_crtc(crtc); 4671 4672 return ret; 4673 } 4674 4675 /** 4676 * drm_mode_config_reset - call ->reset callbacks 4677 * @dev: drm device 4678 * 4679 * This functions calls all the crtc's, encoder's and connector's ->reset 4680 * callback. Drivers can use this in e.g. their driver load or resume code to 4681 * reset hardware and software state. 4682 */ 4683 void drm_mode_config_reset(struct drm_device *dev) 4684 { 4685 struct drm_crtc *crtc; 4686 struct drm_plane *plane; 4687 struct drm_encoder *encoder; 4688 struct drm_connector *connector; 4689 4690 list_for_each_entry(plane, &dev->mode_config.plane_list, head) 4691 if (plane->funcs->reset) 4692 plane->funcs->reset(plane); 4693 4694 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) 4695 if (crtc->funcs->reset) 4696 crtc->funcs->reset(crtc); 4697 4698 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) 4699 if (encoder->funcs->reset) 4700 encoder->funcs->reset(encoder); 4701 4702 list_for_each_entry(connector, &dev->mode_config.connector_list, head) { 4703 connector->status = connector_status_unknown; 4704 4705 if (connector->funcs->reset) 4706 connector->funcs->reset(connector); 4707 } 4708 } 4709 EXPORT_SYMBOL(drm_mode_config_reset); 4710 4711 /** 4712 * drm_mode_create_dumb_ioctl - create a dumb backing storage buffer 4713 * @dev: DRM device 4714 * @data: ioctl data 4715 * @file_priv: DRM file info 4716 * 4717 * This creates a new dumb buffer in the driver's backing storage manager (GEM, 4718 * TTM or something else entirely) and returns the resulting buffer handle. This 4719 * handle can then be wrapped up into a framebuffer modeset object. 4720 * 4721 * Note that userspace is not allowed to use such objects for render 4722 * acceleration - drivers must create their own private ioctls for such a use 4723 * case. 4724 * 4725 * Called by the user via ioctl. 4726 * 4727 * Returns: 4728 * Zero on success, errno on failure. 4729 */ 4730 int drm_mode_create_dumb_ioctl(struct drm_device *dev, 4731 void *data, struct drm_file *file_priv) 4732 { 4733 struct drm_mode_create_dumb *args = data; 4734 u32 cpp, stride, size; 4735 4736 if (!dev->driver->dumb_create) 4737 return -ENOSYS; 4738 if (!args->width || !args->height || !args->bpp) 4739 return -EINVAL; 4740 4741 /* overflow checks for 32bit size calculations */ 4742 /* NOTE: DIV_ROUND_UP() can overflow */ 4743 cpp = DIV_ROUND_UP(args->bpp, 8); 4744 if (!cpp || cpp > 0xffffffffU / args->width) 4745 return -EINVAL; 4746 stride = cpp * args->width; 4747 if (args->height > 0xffffffffU / stride) 4748 return -EINVAL; 4749 4750 /* test for wrap-around */ 4751 size = args->height * stride; 4752 if (PAGE_ALIGN(size) == 0) 4753 return -EINVAL; 4754 4755 return dev->driver->dumb_create(file_priv, dev, args); 4756 } 4757 4758 /** 4759 * drm_mode_mmap_dumb_ioctl - create an mmap offset for a dumb backing storage buffer 4760 * @dev: DRM device 4761 * @data: ioctl data 4762 * @file_priv: DRM file info 4763 * 4764 * Allocate an offset in the drm device node's address space to be able to 4765 * memory map a dumb buffer. 4766 * 4767 * Called by the user via ioctl. 4768 * 4769 * Returns: 4770 * Zero on success, errno on failure. 4771 */ 4772 int drm_mode_mmap_dumb_ioctl(struct drm_device *dev, 4773 void *data, struct drm_file *file_priv) 4774 { 4775 struct drm_mode_map_dumb *args = data; 4776 4777 /* call driver ioctl to get mmap offset */ 4778 if (!dev->driver->dumb_map_offset) 4779 return -ENOSYS; 4780 4781 return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset); 4782 } 4783 4784 /** 4785 * drm_mode_destroy_dumb_ioctl - destroy a dumb backing strage buffer 4786 * @dev: DRM device 4787 * @data: ioctl data 4788 * @file_priv: DRM file info 4789 * 4790 * This destroys the userspace handle for the given dumb backing storage buffer. 4791 * Since buffer objects must be reference counted in the kernel a buffer object 4792 * won't be immediately freed if a framebuffer modeset object still uses it. 4793 * 4794 * Called by the user via ioctl. 4795 * 4796 * Returns: 4797 * Zero on success, errno on failure. 4798 */ 4799 int drm_mode_destroy_dumb_ioctl(struct drm_device *dev, 4800 void *data, struct drm_file *file_priv) 4801 { 4802 struct drm_mode_destroy_dumb *args = data; 4803 4804 if (!dev->driver->dumb_destroy) 4805 return -ENOSYS; 4806 4807 return dev->driver->dumb_destroy(file_priv, dev, args->handle); 4808 } 4809 4810 /** 4811 * drm_fb_get_bpp_depth - get the bpp/depth values for format 4812 * @format: pixel format (DRM_FORMAT_*) 4813 * @depth: storage for the depth value 4814 * @bpp: storage for the bpp value 4815 * 4816 * This only supports RGB formats here for compat with code that doesn't use 4817 * pixel formats directly yet. 4818 */ 4819 void drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth, 4820 int *bpp) 4821 { 4822 switch (format) { 4823 case DRM_FORMAT_C8: 4824 case DRM_FORMAT_RGB332: 4825 case DRM_FORMAT_BGR233: 4826 *depth = 8; 4827 *bpp = 8; 4828 break; 4829 case DRM_FORMAT_XRGB1555: 4830 case DRM_FORMAT_XBGR1555: 4831 case DRM_FORMAT_RGBX5551: 4832 case DRM_FORMAT_BGRX5551: 4833 case DRM_FORMAT_ARGB1555: 4834 case DRM_FORMAT_ABGR1555: 4835 case DRM_FORMAT_RGBA5551: 4836 case DRM_FORMAT_BGRA5551: 4837 *depth = 15; 4838 *bpp = 16; 4839 break; 4840 case DRM_FORMAT_RGB565: 4841 case DRM_FORMAT_BGR565: 4842 *depth = 16; 4843 *bpp = 16; 4844 break; 4845 case DRM_FORMAT_RGB888: 4846 case DRM_FORMAT_BGR888: 4847 *depth = 24; 4848 *bpp = 24; 4849 break; 4850 case DRM_FORMAT_XRGB8888: 4851 case DRM_FORMAT_XBGR8888: 4852 case DRM_FORMAT_RGBX8888: 4853 case DRM_FORMAT_BGRX8888: 4854 *depth = 24; 4855 *bpp = 32; 4856 break; 4857 case DRM_FORMAT_XRGB2101010: 4858 case DRM_FORMAT_XBGR2101010: 4859 case DRM_FORMAT_RGBX1010102: 4860 case DRM_FORMAT_BGRX1010102: 4861 case DRM_FORMAT_ARGB2101010: 4862 case DRM_FORMAT_ABGR2101010: 4863 case DRM_FORMAT_RGBA1010102: 4864 case DRM_FORMAT_BGRA1010102: 4865 *depth = 30; 4866 *bpp = 32; 4867 break; 4868 case DRM_FORMAT_ARGB8888: 4869 case DRM_FORMAT_ABGR8888: 4870 case DRM_FORMAT_RGBA8888: 4871 case DRM_FORMAT_BGRA8888: 4872 *depth = 32; 4873 *bpp = 32; 4874 break; 4875 default: 4876 DRM_DEBUG_KMS("unsupported pixel format %s\n", 4877 drm_get_format_name(format)); 4878 *depth = 0; 4879 *bpp = 0; 4880 break; 4881 } 4882 } 4883 EXPORT_SYMBOL(drm_fb_get_bpp_depth); 4884 4885 /** 4886 * drm_format_num_planes - get the number of planes for format 4887 * @format: pixel format (DRM_FORMAT_*) 4888 * 4889 * Returns: 4890 * The number of planes used by the specified pixel format. 4891 */ 4892 int drm_format_num_planes(uint32_t format) 4893 { 4894 switch (format) { 4895 case DRM_FORMAT_YUV410: 4896 case DRM_FORMAT_YVU410: 4897 case DRM_FORMAT_YUV411: 4898 case DRM_FORMAT_YVU411: 4899 case DRM_FORMAT_YUV420: 4900 case DRM_FORMAT_YVU420: 4901 case DRM_FORMAT_YUV422: 4902 case DRM_FORMAT_YVU422: 4903 case DRM_FORMAT_YUV444: 4904 case DRM_FORMAT_YVU444: 4905 return 3; 4906 case DRM_FORMAT_NV12: 4907 case DRM_FORMAT_NV21: 4908 case DRM_FORMAT_NV16: 4909 case DRM_FORMAT_NV61: 4910 case DRM_FORMAT_NV24: 4911 case DRM_FORMAT_NV42: 4912 return 2; 4913 default: 4914 return 1; 4915 } 4916 } 4917 EXPORT_SYMBOL(drm_format_num_planes); 4918 4919 /** 4920 * drm_format_plane_cpp - determine the bytes per pixel value 4921 * @format: pixel format (DRM_FORMAT_*) 4922 * @plane: plane index 4923 * 4924 * Returns: 4925 * The bytes per pixel value for the specified plane. 4926 */ 4927 int drm_format_plane_cpp(uint32_t format, int plane) 4928 { 4929 unsigned int depth; 4930 int bpp; 4931 4932 if (plane >= drm_format_num_planes(format)) 4933 return 0; 4934 4935 switch (format) { 4936 case DRM_FORMAT_YUYV: 4937 case DRM_FORMAT_YVYU: 4938 case DRM_FORMAT_UYVY: 4939 case DRM_FORMAT_VYUY: 4940 return 2; 4941 case DRM_FORMAT_NV12: 4942 case DRM_FORMAT_NV21: 4943 case DRM_FORMAT_NV16: 4944 case DRM_FORMAT_NV61: 4945 case DRM_FORMAT_NV24: 4946 case DRM_FORMAT_NV42: 4947 return plane ? 2 : 1; 4948 case DRM_FORMAT_YUV410: 4949 case DRM_FORMAT_YVU410: 4950 case DRM_FORMAT_YUV411: 4951 case DRM_FORMAT_YVU411: 4952 case DRM_FORMAT_YUV420: 4953 case DRM_FORMAT_YVU420: 4954 case DRM_FORMAT_YUV422: 4955 case DRM_FORMAT_YVU422: 4956 case DRM_FORMAT_YUV444: 4957 case DRM_FORMAT_YVU444: 4958 return 1; 4959 default: 4960 drm_fb_get_bpp_depth(format, &depth, &bpp); 4961 return bpp >> 3; 4962 } 4963 } 4964 EXPORT_SYMBOL(drm_format_plane_cpp); 4965 4966 /** 4967 * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor 4968 * @format: pixel format (DRM_FORMAT_*) 4969 * 4970 * Returns: 4971 * The horizontal chroma subsampling factor for the 4972 * specified pixel format. 4973 */ 4974 int drm_format_horz_chroma_subsampling(uint32_t format) 4975 { 4976 switch (format) { 4977 case DRM_FORMAT_YUV411: 4978 case DRM_FORMAT_YVU411: 4979 case DRM_FORMAT_YUV410: 4980 case DRM_FORMAT_YVU410: 4981 return 4; 4982 case DRM_FORMAT_YUYV: 4983 case DRM_FORMAT_YVYU: 4984 case DRM_FORMAT_UYVY: 4985 case DRM_FORMAT_VYUY: 4986 case DRM_FORMAT_NV12: 4987 case DRM_FORMAT_NV21: 4988 case DRM_FORMAT_NV16: 4989 case DRM_FORMAT_NV61: 4990 case DRM_FORMAT_YUV422: 4991 case DRM_FORMAT_YVU422: 4992 case DRM_FORMAT_YUV420: 4993 case DRM_FORMAT_YVU420: 4994 return 2; 4995 default: 4996 return 1; 4997 } 4998 } 4999 EXPORT_SYMBOL(drm_format_horz_chroma_subsampling); 5000 5001 /** 5002 * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor 5003 * @format: pixel format (DRM_FORMAT_*) 5004 * 5005 * Returns: 5006 * The vertical chroma subsampling factor for the 5007 * specified pixel format. 5008 */ 5009 int drm_format_vert_chroma_subsampling(uint32_t format) 5010 { 5011 switch (format) { 5012 case DRM_FORMAT_YUV410: 5013 case DRM_FORMAT_YVU410: 5014 return 4; 5015 case DRM_FORMAT_YUV420: 5016 case DRM_FORMAT_YVU420: 5017 case DRM_FORMAT_NV12: 5018 case DRM_FORMAT_NV21: 5019 return 2; 5020 default: 5021 return 1; 5022 } 5023 } 5024 EXPORT_SYMBOL(drm_format_vert_chroma_subsampling); 5025 5026 /** 5027 * drm_mode_config_init - initialize DRM mode_configuration structure 5028 * @dev: DRM device 5029 * 5030 * Initialize @dev's mode_config structure, used for tracking the graphics 5031 * configuration of @dev. 5032 * 5033 * Since this initializes the modeset locks, no locking is possible. Which is no 5034 * problem, since this should happen single threaded at init time. It is the 5035 * driver's problem to ensure this guarantee. 5036 * 5037 */ 5038 void drm_mode_config_init(struct drm_device *dev) 5039 { 5040 lockinit(&dev->mode_config.mutex, "drmmcm", 0, LK_CANRECURSE); 5041 drm_modeset_lock_init(&dev->mode_config.connection_mutex); 5042 lockinit(&dev->mode_config.idr_mutex, "mcfgidr", 0, LK_CANRECURSE); 5043 lockinit(&dev->mode_config.fb_lock, "drmfbl", 0, LK_CANRECURSE); 5044 INIT_LIST_HEAD(&dev->mode_config.fb_list); 5045 INIT_LIST_HEAD(&dev->mode_config.crtc_list); 5046 INIT_LIST_HEAD(&dev->mode_config.connector_list); 5047 INIT_LIST_HEAD(&dev->mode_config.bridge_list); 5048 INIT_LIST_HEAD(&dev->mode_config.encoder_list); 5049 INIT_LIST_HEAD(&dev->mode_config.property_list); 5050 INIT_LIST_HEAD(&dev->mode_config.property_blob_list); 5051 INIT_LIST_HEAD(&dev->mode_config.plane_list); 5052 idr_init(&dev->mode_config.crtc_idr); 5053 5054 drm_modeset_lock_all(dev); 5055 drm_mode_create_standard_connector_properties(dev); 5056 drm_mode_create_standard_plane_properties(dev); 5057 drm_modeset_unlock_all(dev); 5058 5059 /* Just to be sure */ 5060 dev->mode_config.num_fb = 0; 5061 dev->mode_config.num_connector = 0; 5062 dev->mode_config.num_crtc = 0; 5063 dev->mode_config.num_encoder = 0; 5064 dev->mode_config.num_overlay_plane = 0; 5065 dev->mode_config.num_total_plane = 0; 5066 } 5067 EXPORT_SYMBOL(drm_mode_config_init); 5068 5069 /** 5070 * drm_mode_config_cleanup - free up DRM mode_config info 5071 * @dev: DRM device 5072 * 5073 * Free up all the connectors and CRTCs associated with this DRM device, then 5074 * free up the framebuffers and associated buffer objects. 5075 * 5076 * Note that since this /should/ happen single-threaded at driver/device 5077 * teardown time, no locking is required. It's the driver's job to ensure that 5078 * this guarantee actually holds true. 5079 * 5080 * FIXME: cleanup any dangling user buffer objects too 5081 */ 5082 void drm_mode_config_cleanup(struct drm_device *dev) 5083 { 5084 struct drm_connector *connector, *ot; 5085 struct drm_crtc *crtc, *ct; 5086 struct drm_encoder *encoder, *enct; 5087 struct drm_bridge *bridge, *brt; 5088 struct drm_framebuffer *fb, *fbt; 5089 struct drm_property *property, *pt; 5090 struct drm_property_blob *blob, *bt; 5091 struct drm_plane *plane, *plt; 5092 5093 list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list, 5094 head) { 5095 encoder->funcs->destroy(encoder); 5096 } 5097 5098 list_for_each_entry_safe(bridge, brt, 5099 &dev->mode_config.bridge_list, head) { 5100 bridge->funcs->destroy(bridge); 5101 } 5102 5103 list_for_each_entry_safe(connector, ot, 5104 &dev->mode_config.connector_list, head) { 5105 connector->funcs->destroy(connector); 5106 } 5107 5108 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list, 5109 head) { 5110 drm_property_destroy(dev, property); 5111 } 5112 5113 list_for_each_entry_safe(blob, bt, &dev->mode_config.property_blob_list, 5114 head) { 5115 drm_property_destroy_blob(dev, blob); 5116 } 5117 5118 /* 5119 * Single-threaded teardown context, so it's not required to grab the 5120 * fb_lock to protect against concurrent fb_list access. Contrary, it 5121 * would actually deadlock with the drm_framebuffer_cleanup function. 5122 * 5123 * Also, if there are any framebuffers left, that's a driver leak now, 5124 * so politely WARN about this. 5125 */ 5126 WARN_ON(!list_empty(&dev->mode_config.fb_list)); 5127 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) { 5128 drm_framebuffer_remove(fb); 5129 } 5130 5131 list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list, 5132 head) { 5133 plane->funcs->destroy(plane); 5134 } 5135 5136 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) { 5137 crtc->funcs->destroy(crtc); 5138 } 5139 5140 idr_destroy(&dev->mode_config.crtc_idr); 5141 drm_modeset_lock_fini(&dev->mode_config.connection_mutex); 5142 } 5143 EXPORT_SYMBOL(drm_mode_config_cleanup); 5144 5145 struct drm_property *drm_mode_create_rotation_property(struct drm_device *dev, 5146 unsigned int supported_rotations) 5147 { 5148 static const struct drm_prop_enum_list props[] = { 5149 { DRM_ROTATE_0, "rotate-0" }, 5150 { DRM_ROTATE_90, "rotate-90" }, 5151 { DRM_ROTATE_180, "rotate-180" }, 5152 { DRM_ROTATE_270, "rotate-270" }, 5153 { DRM_REFLECT_X, "reflect-x" }, 5154 { DRM_REFLECT_Y, "reflect-y" }, 5155 }; 5156 5157 return drm_property_create_bitmask(dev, 0, "rotation", 5158 props, ARRAY_SIZE(props), 5159 supported_rotations); 5160 } 5161 EXPORT_SYMBOL(drm_mode_create_rotation_property); 5162