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