1 /* 2 * Copyright (c) 2016 Intel Corporation 3 * 4 * Permission to use, copy, modify, distribute, and sell this software and its 5 * documentation for any purpose is hereby granted without fee, provided that 6 * the above copyright notice appear in all copies and that both that copyright 7 * notice and this permission notice appear in supporting documentation, and 8 * that the name of the copyright holders not be used in advertising or 9 * publicity pertaining to distribution of the software without specific, 10 * written prior permission. The copyright holders make no representations 11 * about the suitability of this software for any purpose. It is provided "as 12 * is" without express or implied warranty. 13 * 14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO 16 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR 17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, 18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 20 * OF THIS SOFTWARE. 21 */ 22 23 #include <linux/export.h> 24 #include <drm/drmP.h> 25 #include <drm/drm_auth.h> 26 #include <drm/drm_framebuffer.h> 27 28 #include "drm_crtc_internal.h" 29 30 /** 31 * DOC: overview 32 * 33 * Frame buffers are abstract memory objects that provide a source of pixels to 34 * scanout to a CRTC. Applications explicitly request the creation of frame 35 * buffers through the DRM_IOCTL_MODE_ADDFB(2) ioctls and receive an opaque 36 * handle that can be passed to the KMS CRTC control, plane configuration and 37 * page flip functions. 38 * 39 * Frame buffers rely on the underlying memory manager for allocating backing 40 * storage. When creating a frame buffer applications pass a memory handle 41 * (or a list of memory handles for multi-planar formats) through the 42 * struct &drm_mode_fb_cmd2 argument. For drivers using GEM as their userspace 43 * buffer management interface this would be a GEM handle. Drivers are however 44 * free to use their own backing storage object handles, e.g. vmwgfx directly 45 * exposes special TTM handles to userspace and so expects TTM handles in the 46 * create ioctl and not GEM handles. 47 * 48 * Framebuffers are tracked with struct &drm_framebuffer. They are published 49 * using drm_framebuffer_init() - after calling that function userspace can use 50 * and access the framebuffer object. The helper function 51 * drm_helper_mode_fill_fb_struct() can be used to pre-fill the required 52 * metadata fields. 53 * 54 * The lifetime of a drm framebuffer is controlled with a reference count, 55 * drivers can grab additional references with drm_framebuffer_reference() and 56 * drop them again with drm_framebuffer_unreference(). For driver-private 57 * framebuffers for which the last reference is never dropped (e.g. for the 58 * fbdev framebuffer when the struct struct &drm_framebuffer is embedded into 59 * the fbdev helper struct) drivers can manually clean up a framebuffer at 60 * module unload time with drm_framebuffer_unregister_private(). But doing this 61 * is not recommended, and it's better to have a normal free-standing struct 62 * &drm_framebuffer. 63 */ 64 65 int drm_framebuffer_check_src_coords(uint32_t src_x, uint32_t src_y, 66 uint32_t src_w, uint32_t src_h, 67 const struct drm_framebuffer *fb) 68 { 69 unsigned int fb_width, fb_height; 70 71 fb_width = fb->width << 16; 72 fb_height = fb->height << 16; 73 74 /* Make sure source coordinates are inside the fb. */ 75 if (src_w > fb_width || 76 src_x > fb_width - src_w || 77 src_h > fb_height || 78 src_y > fb_height - src_h) { 79 DRM_DEBUG_KMS("Invalid source coordinates " 80 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n", 81 src_w >> 16, ((src_w & 0xffff) * 15625) >> 10, 82 src_h >> 16, ((src_h & 0xffff) * 15625) >> 10, 83 src_x >> 16, ((src_x & 0xffff) * 15625) >> 10, 84 src_y >> 16, ((src_y & 0xffff) * 15625) >> 10); 85 return -ENOSPC; 86 } 87 88 return 0; 89 } 90 91 /** 92 * drm_mode_addfb - add an FB to the graphics configuration 93 * @dev: drm device for the ioctl 94 * @data: data pointer for the ioctl 95 * @file_priv: drm file for the ioctl call 96 * 97 * Add a new FB to the specified CRTC, given a user request. This is the 98 * original addfb ioctl which only supported RGB formats. 99 * 100 * Called by the user via ioctl. 101 * 102 * Returns: 103 * Zero on success, negative errno on failure. 104 */ 105 int drm_mode_addfb(struct drm_device *dev, 106 void *data, struct drm_file *file_priv) 107 { 108 struct drm_mode_fb_cmd *or = data; 109 struct drm_mode_fb_cmd2 r = {}; 110 int ret; 111 112 /* convert to new format and call new ioctl */ 113 r.fb_id = or->fb_id; 114 r.width = or->width; 115 r.height = or->height; 116 r.pitches[0] = or->pitch; 117 r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth); 118 r.handles[0] = or->handle; 119 120 ret = drm_mode_addfb2(dev, &r, file_priv); 121 if (ret) 122 return ret; 123 124 or->fb_id = r.fb_id; 125 126 return 0; 127 } 128 129 static int framebuffer_check(const struct drm_mode_fb_cmd2 *r) 130 { 131 const struct drm_format_info *info; 132 int i; 133 134 info = __drm_format_info(r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN); 135 if (!info) { 136 char *format_name = drm_get_format_name(r->pixel_format); 137 DRM_DEBUG_KMS("bad framebuffer format %s\n", format_name); 138 kfree(format_name); 139 return -EINVAL; 140 } 141 142 if (r->width == 0 || r->width % info->hsub) { 143 DRM_DEBUG_KMS("bad framebuffer width %u\n", r->width); 144 return -EINVAL; 145 } 146 147 if (r->height == 0 || r->height % info->vsub) { 148 DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height); 149 return -EINVAL; 150 } 151 152 for (i = 0; i < info->num_planes; i++) { 153 unsigned int width = r->width / (i != 0 ? info->hsub : 1); 154 unsigned int height = r->height / (i != 0 ? info->vsub : 1); 155 unsigned int cpp = info->cpp[i]; 156 157 if (!r->handles[i]) { 158 DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i); 159 return -EINVAL; 160 } 161 162 if ((uint64_t) width * cpp > UINT_MAX) 163 return -ERANGE; 164 165 if ((uint64_t) height * r->pitches[i] + r->offsets[i] > UINT_MAX) 166 return -ERANGE; 167 168 if (r->pitches[i] < width * cpp) { 169 DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i); 170 return -EINVAL; 171 } 172 173 if (r->modifier[i] && !(r->flags & DRM_MODE_FB_MODIFIERS)) { 174 DRM_DEBUG_KMS("bad fb modifier %llu for plane %d\n", 175 r->modifier[i], i); 176 return -EINVAL; 177 } 178 179 /* modifier specific checks: */ 180 switch (r->modifier[i]) { 181 case DRM_FORMAT_MOD_SAMSUNG_64_32_TILE: 182 /* NOTE: the pitch restriction may be lifted later if it turns 183 * out that no hw has this restriction: 184 */ 185 if (r->pixel_format != DRM_FORMAT_NV12 || 186 width % 128 || height % 32 || 187 r->pitches[i] % 128) { 188 DRM_DEBUG_KMS("bad modifier data for plane %d\n", i); 189 return -EINVAL; 190 } 191 break; 192 193 default: 194 break; 195 } 196 } 197 198 for (i = info->num_planes; i < 4; i++) { 199 if (r->modifier[i]) { 200 DRM_DEBUG_KMS("non-zero modifier for unused plane %d\n", i); 201 return -EINVAL; 202 } 203 204 /* Pre-FB_MODIFIERS userspace didn't clear the structs properly. */ 205 if (!(r->flags & DRM_MODE_FB_MODIFIERS)) 206 continue; 207 208 if (r->handles[i]) { 209 DRM_DEBUG_KMS("buffer object handle for unused plane %d\n", i); 210 return -EINVAL; 211 } 212 213 if (r->pitches[i]) { 214 DRM_DEBUG_KMS("non-zero pitch for unused plane %d\n", i); 215 return -EINVAL; 216 } 217 218 if (r->offsets[i]) { 219 DRM_DEBUG_KMS("non-zero offset for unused plane %d\n", i); 220 return -EINVAL; 221 } 222 } 223 224 return 0; 225 } 226 227 struct drm_framebuffer * 228 drm_internal_framebuffer_create(struct drm_device *dev, 229 const struct drm_mode_fb_cmd2 *r, 230 struct drm_file *file_priv) 231 { 232 struct drm_mode_config *config = &dev->mode_config; 233 struct drm_framebuffer *fb; 234 int ret; 235 236 if (r->flags & ~(DRM_MODE_FB_INTERLACED | DRM_MODE_FB_MODIFIERS)) { 237 DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r->flags); 238 return ERR_PTR(-EINVAL); 239 } 240 241 if ((config->min_width > r->width) || (r->width > config->max_width)) { 242 DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n", 243 r->width, config->min_width, config->max_width); 244 return ERR_PTR(-EINVAL); 245 } 246 if ((config->min_height > r->height) || (r->height > config->max_height)) { 247 DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n", 248 r->height, config->min_height, config->max_height); 249 return ERR_PTR(-EINVAL); 250 } 251 252 if (r->flags & DRM_MODE_FB_MODIFIERS && 253 !dev->mode_config.allow_fb_modifiers) { 254 DRM_DEBUG_KMS("driver does not support fb modifiers\n"); 255 return ERR_PTR(-EINVAL); 256 } 257 258 ret = framebuffer_check(r); 259 if (ret) 260 return ERR_PTR(ret); 261 262 fb = dev->mode_config.funcs->fb_create(dev, file_priv, r); 263 if (IS_ERR(fb)) { 264 DRM_DEBUG_KMS("could not create framebuffer\n"); 265 return fb; 266 } 267 268 return fb; 269 } 270 271 /** 272 * drm_mode_addfb2 - add an FB to the graphics configuration 273 * @dev: drm device for the ioctl 274 * @data: data pointer for the ioctl 275 * @file_priv: drm file for the ioctl call 276 * 277 * Add a new FB to the specified CRTC, given a user request with format. This is 278 * the 2nd version of the addfb ioctl, which supports multi-planar framebuffers 279 * and uses fourcc codes as pixel format specifiers. 280 * 281 * Called by the user via ioctl. 282 * 283 * Returns: 284 * Zero on success, negative errno on failure. 285 */ 286 int drm_mode_addfb2(struct drm_device *dev, 287 void *data, struct drm_file *file_priv) 288 { 289 struct drm_mode_fb_cmd2 *r = data; 290 struct drm_framebuffer *fb; 291 292 if (!drm_core_check_feature(dev, DRIVER_MODESET)) 293 return -EINVAL; 294 295 fb = drm_internal_framebuffer_create(dev, r, file_priv); 296 if (IS_ERR(fb)) 297 return PTR_ERR(fb); 298 299 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id); 300 r->fb_id = fb->base.id; 301 302 /* Transfer ownership to the filp for reaping on close */ 303 mutex_lock(&file_priv->fbs_lock); 304 list_add(&fb->filp_head, &file_priv->fbs); 305 mutex_unlock(&file_priv->fbs_lock); 306 307 return 0; 308 } 309 310 struct drm_mode_rmfb_work { 311 struct work_struct work; 312 struct list_head fbs; 313 }; 314 315 static void drm_mode_rmfb_work_fn(struct work_struct *w) 316 { 317 struct drm_mode_rmfb_work *arg = container_of(w, typeof(*arg), work); 318 319 while (!list_empty(&arg->fbs)) { 320 struct drm_framebuffer *fb = 321 list_first_entry(&arg->fbs, typeof(*fb), filp_head); 322 323 list_del_init(&fb->filp_head); 324 drm_framebuffer_remove(fb); 325 } 326 } 327 328 /** 329 * drm_mode_rmfb - remove an FB from the configuration 330 * @dev: drm device for the ioctl 331 * @data: data pointer for the ioctl 332 * @file_priv: drm file for the ioctl call 333 * 334 * Remove the FB specified by the user. 335 * 336 * Called by the user via ioctl. 337 * 338 * Returns: 339 * Zero on success, negative errno on failure. 340 */ 341 int drm_mode_rmfb(struct drm_device *dev, 342 void *data, struct drm_file *file_priv) 343 { 344 struct drm_framebuffer *fb = NULL; 345 struct drm_framebuffer *fbl = NULL; 346 uint32_t *id = data; 347 int found = 0; 348 349 if (!drm_core_check_feature(dev, DRIVER_MODESET)) 350 return -EINVAL; 351 352 fb = drm_framebuffer_lookup(dev, *id); 353 if (!fb) 354 return -ENOENT; 355 356 mutex_lock(&file_priv->fbs_lock); 357 list_for_each_entry(fbl, &file_priv->fbs, filp_head) 358 if (fb == fbl) 359 found = 1; 360 if (!found) { 361 mutex_unlock(&file_priv->fbs_lock); 362 goto fail_unref; 363 } 364 365 list_del_init(&fb->filp_head); 366 mutex_unlock(&file_priv->fbs_lock); 367 368 /* drop the reference we picked up in framebuffer lookup */ 369 drm_framebuffer_unreference(fb); 370 371 /* 372 * we now own the reference that was stored in the fbs list 373 * 374 * drm_framebuffer_remove may fail with -EINTR on pending signals, 375 * so run this in a separate stack as there's no way to correctly 376 * handle this after the fb is already removed from the lookup table. 377 */ 378 if (drm_framebuffer_read_refcount(fb) > 1) { 379 struct drm_mode_rmfb_work arg; 380 381 INIT_WORK_ONSTACK(&arg.work, drm_mode_rmfb_work_fn); 382 INIT_LIST_HEAD(&arg.fbs); 383 list_add_tail(&fb->filp_head, &arg.fbs); 384 385 schedule_work(&arg.work); 386 flush_work(&arg.work); 387 destroy_work_on_stack(&arg.work); 388 } else 389 drm_framebuffer_unreference(fb); 390 391 return 0; 392 393 fail_unref: 394 drm_framebuffer_unreference(fb); 395 return -ENOENT; 396 } 397 398 /** 399 * drm_mode_getfb - get FB info 400 * @dev: drm device for the ioctl 401 * @data: data pointer for the ioctl 402 * @file_priv: drm file for the ioctl call 403 * 404 * Lookup the FB given its ID and return info about it. 405 * 406 * Called by the user via ioctl. 407 * 408 * Returns: 409 * Zero on success, negative errno on failure. 410 */ 411 int drm_mode_getfb(struct drm_device *dev, 412 void *data, struct drm_file *file_priv) 413 { 414 struct drm_mode_fb_cmd *r = data; 415 struct drm_framebuffer *fb; 416 int ret; 417 418 if (!drm_core_check_feature(dev, DRIVER_MODESET)) 419 return -EINVAL; 420 421 fb = drm_framebuffer_lookup(dev, r->fb_id); 422 if (!fb) 423 return -ENOENT; 424 425 r->height = fb->height; 426 r->width = fb->width; 427 r->depth = fb->depth; 428 r->bpp = fb->bits_per_pixel; 429 r->pitch = fb->pitches[0]; 430 if (fb->funcs->create_handle) { 431 if (drm_is_current_master(file_priv) || capable(CAP_SYS_ADMIN) || 432 drm_is_control_client(file_priv)) { 433 ret = fb->funcs->create_handle(fb, file_priv, 434 &r->handle); 435 } else { 436 /* GET_FB() is an unprivileged ioctl so we must not 437 * return a buffer-handle to non-master processes! For 438 * backwards-compatibility reasons, we cannot make 439 * GET_FB() privileged, so just return an invalid handle 440 * for non-masters. */ 441 r->handle = 0; 442 ret = 0; 443 } 444 } else { 445 ret = -ENODEV; 446 } 447 448 drm_framebuffer_unreference(fb); 449 450 return ret; 451 } 452 453 /** 454 * drm_mode_dirtyfb_ioctl - flush frontbuffer rendering on an FB 455 * @dev: drm device for the ioctl 456 * @data: data pointer for the ioctl 457 * @file_priv: drm file for the ioctl call 458 * 459 * Lookup the FB and flush out the damaged area supplied by userspace as a clip 460 * rectangle list. Generic userspace which does frontbuffer rendering must call 461 * this ioctl to flush out the changes on manual-update display outputs, e.g. 462 * usb display-link, mipi manual update panels or edp panel self refresh modes. 463 * 464 * Modesetting drivers which always update the frontbuffer do not need to 465 * implement the corresponding ->dirty framebuffer callback. 466 * 467 * Called by the user via ioctl. 468 * 469 * Returns: 470 * Zero on success, negative errno on failure. 471 */ 472 int drm_mode_dirtyfb_ioctl(struct drm_device *dev, 473 void *data, struct drm_file *file_priv) 474 { 475 struct drm_clip_rect __user *clips_ptr; 476 struct drm_clip_rect *clips = NULL; 477 struct drm_mode_fb_dirty_cmd *r = data; 478 struct drm_framebuffer *fb; 479 unsigned flags; 480 int num_clips; 481 int ret; 482 483 if (!drm_core_check_feature(dev, DRIVER_MODESET)) 484 return -EINVAL; 485 486 fb = drm_framebuffer_lookup(dev, r->fb_id); 487 if (!fb) 488 return -ENOENT; 489 490 num_clips = r->num_clips; 491 clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr; 492 493 if (!num_clips != !clips_ptr) { 494 ret = -EINVAL; 495 goto out_err1; 496 } 497 498 flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags; 499 500 /* If userspace annotates copy, clips must come in pairs */ 501 if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) { 502 ret = -EINVAL; 503 goto out_err1; 504 } 505 506 if (num_clips && clips_ptr) { 507 if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) { 508 ret = -EINVAL; 509 goto out_err1; 510 } 511 clips = kcalloc(num_clips, sizeof(*clips), GFP_KERNEL); 512 if (!clips) { 513 ret = -ENOMEM; 514 goto out_err1; 515 } 516 517 ret = copy_from_user(clips, clips_ptr, 518 num_clips * sizeof(*clips)); 519 if (ret) { 520 ret = -EFAULT; 521 goto out_err2; 522 } 523 } 524 525 if (fb->funcs->dirty) { 526 ret = fb->funcs->dirty(fb, file_priv, flags, r->color, 527 clips, num_clips); 528 } else { 529 ret = -ENOSYS; 530 } 531 532 out_err2: 533 kfree(clips); 534 out_err1: 535 drm_framebuffer_unreference(fb); 536 537 return ret; 538 } 539 540 /** 541 * drm_fb_release - remove and free the FBs on this file 542 * @priv: drm file for the ioctl 543 * 544 * Destroy all the FBs associated with @filp. 545 * 546 * Called by the user via ioctl. 547 * 548 * Returns: 549 * Zero on success, negative errno on failure. 550 */ 551 void drm_fb_release(struct drm_file *priv) 552 { 553 struct drm_framebuffer *fb, *tfb; 554 struct drm_mode_rmfb_work arg; 555 556 INIT_LIST_HEAD(&arg.fbs); 557 558 /* 559 * When the file gets released that means no one else can access the fb 560 * list any more, so no need to grab fpriv->fbs_lock. And we need to 561 * avoid upsetting lockdep since the universal cursor code adds a 562 * framebuffer while holding mutex locks. 563 * 564 * Note that a real deadlock between fpriv->fbs_lock and the modeset 565 * locks is impossible here since no one else but this function can get 566 * at it any more. 567 */ 568 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) { 569 if (drm_framebuffer_read_refcount(fb) > 1) { 570 list_move_tail(&fb->filp_head, &arg.fbs); 571 } else { 572 list_del_init(&fb->filp_head); 573 574 /* This drops the fpriv->fbs reference. */ 575 drm_framebuffer_unreference(fb); 576 } 577 } 578 579 if (!list_empty(&arg.fbs)) { 580 INIT_WORK_ONSTACK(&arg.work, drm_mode_rmfb_work_fn); 581 582 schedule_work(&arg.work); 583 flush_work(&arg.work); 584 destroy_work_on_stack(&arg.work); 585 } 586 } 587 588 void drm_framebuffer_free(struct kref *kref) 589 { 590 struct drm_framebuffer *fb = 591 container_of(kref, struct drm_framebuffer, base.refcount); 592 struct drm_device *dev = fb->dev; 593 594 /* 595 * The lookup idr holds a weak reference, which has not necessarily been 596 * removed at this point. Check for that. 597 */ 598 drm_mode_object_unregister(dev, &fb->base); 599 600 fb->funcs->destroy(fb); 601 } 602 603 /** 604 * drm_framebuffer_init - initialize a framebuffer 605 * @dev: DRM device 606 * @fb: framebuffer to be initialized 607 * @funcs: ... with these functions 608 * 609 * Allocates an ID for the framebuffer's parent mode object, sets its mode 610 * functions & device file and adds it to the master fd list. 611 * 612 * IMPORTANT: 613 * This functions publishes the fb and makes it available for concurrent access 614 * by other users. Which means by this point the fb _must_ be fully set up - 615 * since all the fb attributes are invariant over its lifetime, no further 616 * locking but only correct reference counting is required. 617 * 618 * Returns: 619 * Zero on success, error code on failure. 620 */ 621 int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb, 622 const struct drm_framebuffer_funcs *funcs) 623 { 624 int ret; 625 626 INIT_LIST_HEAD(&fb->filp_head); 627 fb->dev = dev; 628 fb->funcs = funcs; 629 630 ret = drm_mode_object_get_reg(dev, &fb->base, DRM_MODE_OBJECT_FB, 631 false, drm_framebuffer_free); 632 if (ret) 633 goto out; 634 635 mutex_lock(&dev->mode_config.fb_lock); 636 dev->mode_config.num_fb++; 637 list_add(&fb->head, &dev->mode_config.fb_list); 638 mutex_unlock(&dev->mode_config.fb_lock); 639 640 drm_mode_object_register(dev, &fb->base); 641 out: 642 return ret; 643 } 644 EXPORT_SYMBOL(drm_framebuffer_init); 645 646 /** 647 * drm_framebuffer_lookup - look up a drm framebuffer and grab a reference 648 * @dev: drm device 649 * @id: id of the fb object 650 * 651 * If successful, this grabs an additional reference to the framebuffer - 652 * callers need to make sure to eventually unreference the returned framebuffer 653 * again, using @drm_framebuffer_unreference. 654 */ 655 struct drm_framebuffer *drm_framebuffer_lookup(struct drm_device *dev, 656 uint32_t id) 657 { 658 struct drm_mode_object *obj; 659 struct drm_framebuffer *fb = NULL; 660 661 obj = __drm_mode_object_find(dev, id, DRM_MODE_OBJECT_FB); 662 if (obj) 663 fb = obj_to_fb(obj); 664 return fb; 665 } 666 EXPORT_SYMBOL(drm_framebuffer_lookup); 667 668 /** 669 * drm_framebuffer_unregister_private - unregister a private fb from the lookup idr 670 * @fb: fb to unregister 671 * 672 * Drivers need to call this when cleaning up driver-private framebuffers, e.g. 673 * those used for fbdev. Note that the caller must hold a reference of it's own, 674 * i.e. the object may not be destroyed through this call (since it'll lead to a 675 * locking inversion). 676 * 677 * NOTE: This function is deprecated. For driver-private framebuffers it is not 678 * recommended to embed a framebuffer struct info fbdev struct, instead, a 679 * framebuffer pointer is preferred and drm_framebuffer_unreference() should be 680 * called when the framebuffer is to be cleaned up. 681 */ 682 void drm_framebuffer_unregister_private(struct drm_framebuffer *fb) 683 { 684 struct drm_device *dev; 685 686 if (!fb) 687 return; 688 689 dev = fb->dev; 690 691 /* Mark fb as reaped and drop idr ref. */ 692 drm_mode_object_unregister(dev, &fb->base); 693 } 694 EXPORT_SYMBOL(drm_framebuffer_unregister_private); 695 696 /** 697 * drm_framebuffer_cleanup - remove a framebuffer object 698 * @fb: framebuffer to remove 699 * 700 * Cleanup framebuffer. This function is intended to be used from the drivers 701 * ->destroy callback. It can also be used to clean up driver private 702 * framebuffers embedded into a larger structure. 703 * 704 * Note that this function does not remove the fb from active usuage - if it is 705 * still used anywhere, hilarity can ensue since userspace could call getfb on 706 * the id and get back -EINVAL. Obviously no concern at driver unload time. 707 * 708 * Also, the framebuffer will not be removed from the lookup idr - for 709 * user-created framebuffers this will happen in in the rmfb ioctl. For 710 * driver-private objects (e.g. for fbdev) drivers need to explicitly call 711 * drm_framebuffer_unregister_private. 712 */ 713 void drm_framebuffer_cleanup(struct drm_framebuffer *fb) 714 { 715 struct drm_device *dev = fb->dev; 716 717 mutex_lock(&dev->mode_config.fb_lock); 718 list_del(&fb->head); 719 dev->mode_config.num_fb--; 720 mutex_unlock(&dev->mode_config.fb_lock); 721 } 722 EXPORT_SYMBOL(drm_framebuffer_cleanup); 723 724 /** 725 * drm_framebuffer_remove - remove and unreference a framebuffer object 726 * @fb: framebuffer to remove 727 * 728 * Scans all the CRTCs and planes in @dev's mode_config. If they're 729 * using @fb, removes it, setting it to NULL. Then drops the reference to the 730 * passed-in framebuffer. Might take the modeset locks. 731 * 732 * Note that this function optimizes the cleanup away if the caller holds the 733 * last reference to the framebuffer. It is also guaranteed to not take the 734 * modeset locks in this case. 735 */ 736 void drm_framebuffer_remove(struct drm_framebuffer *fb) 737 { 738 struct drm_device *dev; 739 struct drm_crtc *crtc; 740 struct drm_plane *plane; 741 742 if (!fb) 743 return; 744 745 dev = fb->dev; 746 747 WARN_ON(!list_empty(&fb->filp_head)); 748 749 /* 750 * drm ABI mandates that we remove any deleted framebuffers from active 751 * useage. But since most sane clients only remove framebuffers they no 752 * longer need, try to optimize this away. 753 * 754 * Since we're holding a reference ourselves, observing a refcount of 1 755 * means that we're the last holder and can skip it. Also, the refcount 756 * can never increase from 1 again, so we don't need any barriers or 757 * locks. 758 * 759 * Note that userspace could try to race with use and instate a new 760 * usage _after_ we've cleared all current ones. End result will be an 761 * in-use fb with fb-id == 0. Userspace is allowed to shoot its own foot 762 * in this manner. 763 */ 764 if (drm_framebuffer_read_refcount(fb) > 1) { 765 drm_modeset_lock_all(dev); 766 /* remove from any CRTC */ 767 drm_for_each_crtc(crtc, dev) { 768 if (crtc->primary->fb == fb) { 769 /* should turn off the crtc */ 770 if (drm_crtc_force_disable(crtc)) 771 DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc); 772 } 773 } 774 775 drm_for_each_plane(plane, dev) { 776 if (plane->fb == fb) 777 drm_plane_force_disable(plane); 778 } 779 drm_modeset_unlock_all(dev); 780 } 781 782 drm_framebuffer_unreference(fb); 783 } 784 EXPORT_SYMBOL(drm_framebuffer_remove); 785