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