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