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_get() and drop 56 * them again with drm_framebuffer_put(). For driver-private framebuffers for 57 * which the last reference is never dropped (e.g. for the fbdev framebuffer 58 * when the struct &struct drm_framebuffer is embedded into the fbdev helper 59 * struct) drivers can manually clean up a framebuffer at module unload time 60 * with drm_framebuffer_unregister_private(). But doing this is not 61 * 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 fb_plane_width(int width, 130 const struct drm_format_info *format, int plane) 131 { 132 if (plane == 0) 133 return width; 134 135 return DIV_ROUND_UP(width, format->hsub); 136 } 137 138 static int fb_plane_height(int height, 139 const struct drm_format_info *format, int plane) 140 { 141 if (plane == 0) 142 return height; 143 144 return DIV_ROUND_UP(height, format->vsub); 145 } 146 147 static int framebuffer_check(struct drm_device *dev, 148 const struct drm_mode_fb_cmd2 *r) 149 { 150 const struct drm_format_info *info; 151 int i; 152 153 /* check if the format is supported at all */ 154 info = __drm_format_info(r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN); 155 if (!info) { 156 struct drm_format_name_buf format_name; 157 DRM_DEBUG_KMS("bad framebuffer format %s\n", 158 drm_get_format_name(r->pixel_format, 159 &format_name)); 160 return -EINVAL; 161 } 162 163 /* now let the driver pick its own format info */ 164 info = drm_get_format_info(dev, r); 165 166 if (r->width == 0) { 167 DRM_DEBUG_KMS("bad framebuffer width %u\n", r->width); 168 return -EINVAL; 169 } 170 171 if (r->height == 0) { 172 DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height); 173 return -EINVAL; 174 } 175 176 for (i = 0; i < info->num_planes; i++) { 177 unsigned int width = fb_plane_width(r->width, info, i); 178 unsigned int height = fb_plane_height(r->height, info, i); 179 unsigned int cpp = info->cpp[i]; 180 181 if (!r->handles[i]) { 182 DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i); 183 return -EINVAL; 184 } 185 186 if ((uint64_t) width * cpp > UINT_MAX) 187 return -ERANGE; 188 189 if ((uint64_t) height * r->pitches[i] + r->offsets[i] > UINT_MAX) 190 return -ERANGE; 191 192 if (r->pitches[i] < width * cpp) { 193 DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i); 194 return -EINVAL; 195 } 196 197 if (r->modifier[i] && !(r->flags & DRM_MODE_FB_MODIFIERS)) { 198 DRM_DEBUG_KMS("bad fb modifier %llu for plane %d\n", 199 r->modifier[i], i); 200 return -EINVAL; 201 } 202 203 if (r->flags & DRM_MODE_FB_MODIFIERS && 204 r->modifier[i] != r->modifier[0]) { 205 DRM_DEBUG_KMS("bad fb modifier %llu for plane %d\n", 206 r->modifier[i], i); 207 return -EINVAL; 208 } 209 210 /* modifier specific checks: */ 211 switch (r->modifier[i]) { 212 case DRM_FORMAT_MOD_SAMSUNG_64_32_TILE: 213 /* NOTE: the pitch restriction may be lifted later if it turns 214 * out that no hw has this restriction: 215 */ 216 if (r->pixel_format != DRM_FORMAT_NV12 || 217 width % 128 || height % 32 || 218 r->pitches[i] % 128) { 219 DRM_DEBUG_KMS("bad modifier data for plane %d\n", i); 220 return -EINVAL; 221 } 222 break; 223 224 default: 225 break; 226 } 227 } 228 229 for (i = info->num_planes; i < 4; i++) { 230 if (r->modifier[i]) { 231 DRM_DEBUG_KMS("non-zero modifier for unused plane %d\n", i); 232 return -EINVAL; 233 } 234 235 /* Pre-FB_MODIFIERS userspace didn't clear the structs properly. */ 236 if (!(r->flags & DRM_MODE_FB_MODIFIERS)) 237 continue; 238 239 if (r->handles[i]) { 240 DRM_DEBUG_KMS("buffer object handle for unused plane %d\n", i); 241 return -EINVAL; 242 } 243 244 if (r->pitches[i]) { 245 DRM_DEBUG_KMS("non-zero pitch for unused plane %d\n", i); 246 return -EINVAL; 247 } 248 249 if (r->offsets[i]) { 250 DRM_DEBUG_KMS("non-zero offset for unused plane %d\n", i); 251 return -EINVAL; 252 } 253 } 254 255 return 0; 256 } 257 258 struct drm_framebuffer * 259 drm_internal_framebuffer_create(struct drm_device *dev, 260 const struct drm_mode_fb_cmd2 *r, 261 struct drm_file *file_priv) 262 { 263 struct drm_mode_config *config = &dev->mode_config; 264 struct drm_framebuffer *fb; 265 int ret; 266 267 if (r->flags & ~(DRM_MODE_FB_INTERLACED | DRM_MODE_FB_MODIFIERS)) { 268 DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r->flags); 269 return ERR_PTR(-EINVAL); 270 } 271 272 if ((config->min_width > r->width) || (r->width > config->max_width)) { 273 DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n", 274 r->width, config->min_width, config->max_width); 275 return ERR_PTR(-EINVAL); 276 } 277 if ((config->min_height > r->height) || (r->height > config->max_height)) { 278 DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n", 279 r->height, config->min_height, config->max_height); 280 return ERR_PTR(-EINVAL); 281 } 282 283 if (r->flags & DRM_MODE_FB_MODIFIERS && 284 !dev->mode_config.allow_fb_modifiers) { 285 DRM_DEBUG_KMS("driver does not support fb modifiers\n"); 286 return ERR_PTR(-EINVAL); 287 } 288 289 ret = framebuffer_check(dev, r); 290 if (ret) 291 return ERR_PTR(ret); 292 293 fb = dev->mode_config.funcs->fb_create(dev, file_priv, r); 294 if (IS_ERR(fb)) { 295 DRM_DEBUG_KMS("could not create framebuffer\n"); 296 return fb; 297 } 298 299 return fb; 300 } 301 302 /** 303 * drm_mode_addfb2 - add an FB to the graphics configuration 304 * @dev: drm device for the ioctl 305 * @data: data pointer for the ioctl 306 * @file_priv: drm file for the ioctl call 307 * 308 * Add a new FB to the specified CRTC, given a user request with format. This is 309 * the 2nd version of the addfb ioctl, which supports multi-planar framebuffers 310 * and uses fourcc codes as pixel format specifiers. 311 * 312 * Called by the user via ioctl. 313 * 314 * Returns: 315 * Zero on success, negative errno on failure. 316 */ 317 int drm_mode_addfb2(struct drm_device *dev, 318 void *data, struct drm_file *file_priv) 319 { 320 struct drm_mode_fb_cmd2 *r = data; 321 struct drm_framebuffer *fb; 322 323 if (!drm_core_check_feature(dev, DRIVER_MODESET)) 324 return -EINVAL; 325 326 fb = drm_internal_framebuffer_create(dev, r, file_priv); 327 if (IS_ERR(fb)) 328 return PTR_ERR(fb); 329 330 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id); 331 r->fb_id = fb->base.id; 332 333 /* Transfer ownership to the filp for reaping on close */ 334 mutex_lock(&file_priv->fbs_lock); 335 list_add(&fb->filp_head, &file_priv->fbs); 336 mutex_unlock(&file_priv->fbs_lock); 337 338 return 0; 339 } 340 341 struct drm_mode_rmfb_work { 342 struct work_struct work; 343 struct list_head fbs; 344 }; 345 346 static void drm_mode_rmfb_work_fn(struct work_struct *w) 347 { 348 struct drm_mode_rmfb_work *arg = container_of(w, typeof(*arg), work); 349 350 while (!list_empty(&arg->fbs)) { 351 struct drm_framebuffer *fb = 352 list_first_entry(&arg->fbs, typeof(*fb), filp_head); 353 354 list_del_init(&fb->filp_head); 355 drm_framebuffer_remove(fb); 356 } 357 } 358 359 /** 360 * drm_mode_rmfb - remove an FB from the configuration 361 * @dev: drm device for the ioctl 362 * @data: data pointer for the ioctl 363 * @file_priv: drm file for the ioctl call 364 * 365 * Remove the FB specified by the user. 366 * 367 * Called by the user via ioctl. 368 * 369 * Returns: 370 * Zero on success, negative errno on failure. 371 */ 372 int drm_mode_rmfb(struct drm_device *dev, 373 void *data, struct drm_file *file_priv) 374 { 375 struct drm_framebuffer *fb = NULL; 376 struct drm_framebuffer *fbl = NULL; 377 uint32_t *id = data; 378 int found = 0; 379 380 if (!drm_core_check_feature(dev, DRIVER_MODESET)) 381 return -EINVAL; 382 383 fb = drm_framebuffer_lookup(dev, *id); 384 if (!fb) 385 return -ENOENT; 386 387 mutex_lock(&file_priv->fbs_lock); 388 list_for_each_entry(fbl, &file_priv->fbs, filp_head) 389 if (fb == fbl) 390 found = 1; 391 if (!found) { 392 mutex_unlock(&file_priv->fbs_lock); 393 goto fail_unref; 394 } 395 396 list_del_init(&fb->filp_head); 397 mutex_unlock(&file_priv->fbs_lock); 398 399 /* drop the reference we picked up in framebuffer lookup */ 400 drm_framebuffer_put(fb); 401 402 /* 403 * we now own the reference that was stored in the fbs list 404 * 405 * drm_framebuffer_remove may fail with -EINTR on pending signals, 406 * so run this in a separate stack as there's no way to correctly 407 * handle this after the fb is already removed from the lookup table. 408 */ 409 if (drm_framebuffer_read_refcount(fb) > 1) { 410 struct drm_mode_rmfb_work arg; 411 412 INIT_WORK_ONSTACK(&arg.work, drm_mode_rmfb_work_fn); 413 INIT_LIST_HEAD(&arg.fbs); 414 list_add_tail(&fb->filp_head, &arg.fbs); 415 416 schedule_work(&arg.work); 417 flush_work(&arg.work); 418 destroy_work_on_stack(&arg.work); 419 } else 420 drm_framebuffer_put(fb); 421 422 return 0; 423 424 fail_unref: 425 drm_framebuffer_put(fb); 426 return -ENOENT; 427 } 428 429 /** 430 * drm_mode_getfb - get FB info 431 * @dev: drm device for the ioctl 432 * @data: data pointer for the ioctl 433 * @file_priv: drm file for the ioctl call 434 * 435 * Lookup the FB given its ID and return info about it. 436 * 437 * Called by the user via ioctl. 438 * 439 * Returns: 440 * Zero on success, negative errno on failure. 441 */ 442 int drm_mode_getfb(struct drm_device *dev, 443 void *data, struct drm_file *file_priv) 444 { 445 struct drm_mode_fb_cmd *r = data; 446 struct drm_framebuffer *fb; 447 int ret; 448 449 if (!drm_core_check_feature(dev, DRIVER_MODESET)) 450 return -EINVAL; 451 452 fb = drm_framebuffer_lookup(dev, r->fb_id); 453 if (!fb) 454 return -ENOENT; 455 456 r->height = fb->height; 457 r->width = fb->width; 458 r->depth = fb->format->depth; 459 r->bpp = fb->format->cpp[0] * 8; 460 r->pitch = fb->pitches[0]; 461 if (fb->funcs->create_handle) { 462 if (drm_is_current_master(file_priv) || capable(CAP_SYS_ADMIN) || 463 drm_is_control_client(file_priv)) { 464 ret = fb->funcs->create_handle(fb, file_priv, 465 &r->handle); 466 } else { 467 /* GET_FB() is an unprivileged ioctl so we must not 468 * return a buffer-handle to non-master processes! For 469 * backwards-compatibility reasons, we cannot make 470 * GET_FB() privileged, so just return an invalid handle 471 * for non-masters. */ 472 r->handle = 0; 473 ret = 0; 474 } 475 } else { 476 ret = -ENODEV; 477 } 478 479 drm_framebuffer_put(fb); 480 481 return ret; 482 } 483 484 /** 485 * drm_mode_dirtyfb_ioctl - flush frontbuffer rendering on an FB 486 * @dev: drm device for the ioctl 487 * @data: data pointer for the ioctl 488 * @file_priv: drm file for the ioctl call 489 * 490 * Lookup the FB and flush out the damaged area supplied by userspace as a clip 491 * rectangle list. Generic userspace which does frontbuffer rendering must call 492 * this ioctl to flush out the changes on manual-update display outputs, e.g. 493 * usb display-link, mipi manual update panels or edp panel self refresh modes. 494 * 495 * Modesetting drivers which always update the frontbuffer do not need to 496 * implement the corresponding &drm_framebuffer_funcs.dirty callback. 497 * 498 * Called by the user via ioctl. 499 * 500 * Returns: 501 * Zero on success, negative errno on failure. 502 */ 503 int drm_mode_dirtyfb_ioctl(struct drm_device *dev, 504 void *data, struct drm_file *file_priv) 505 { 506 struct drm_clip_rect __user *clips_ptr; 507 struct drm_clip_rect *clips = NULL; 508 struct drm_mode_fb_dirty_cmd *r = data; 509 struct drm_framebuffer *fb; 510 unsigned flags; 511 int num_clips; 512 int ret; 513 514 if (!drm_core_check_feature(dev, DRIVER_MODESET)) 515 return -EINVAL; 516 517 fb = drm_framebuffer_lookup(dev, r->fb_id); 518 if (!fb) 519 return -ENOENT; 520 521 num_clips = r->num_clips; 522 clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr; 523 524 if (!num_clips != !clips_ptr) { 525 ret = -EINVAL; 526 goto out_err1; 527 } 528 529 flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags; 530 531 /* If userspace annotates copy, clips must come in pairs */ 532 if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) { 533 ret = -EINVAL; 534 goto out_err1; 535 } 536 537 if (num_clips && clips_ptr) { 538 if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) { 539 ret = -EINVAL; 540 goto out_err1; 541 } 542 clips = kcalloc(num_clips, sizeof(*clips), GFP_KERNEL); 543 if (!clips) { 544 ret = -ENOMEM; 545 goto out_err1; 546 } 547 548 ret = copy_from_user(clips, clips_ptr, 549 num_clips * sizeof(*clips)); 550 if (ret) { 551 ret = -EFAULT; 552 goto out_err2; 553 } 554 } 555 556 if (fb->funcs->dirty) { 557 ret = fb->funcs->dirty(fb, file_priv, flags, r->color, 558 clips, num_clips); 559 } else { 560 ret = -ENOSYS; 561 } 562 563 out_err2: 564 kfree(clips); 565 out_err1: 566 drm_framebuffer_put(fb); 567 568 return ret; 569 } 570 571 /** 572 * drm_fb_release - remove and free the FBs on this file 573 * @priv: drm file for the ioctl 574 * 575 * Destroy all the FBs associated with @filp. 576 * 577 * Called by the user via ioctl. 578 * 579 * Returns: 580 * Zero on success, negative errno on failure. 581 */ 582 void drm_fb_release(struct drm_file *priv) 583 { 584 struct drm_framebuffer *fb, *tfb; 585 struct drm_mode_rmfb_work arg; 586 587 INIT_LIST_HEAD(&arg.fbs); 588 589 /* 590 * When the file gets released that means no one else can access the fb 591 * list any more, so no need to grab fpriv->fbs_lock. And we need to 592 * avoid upsetting lockdep since the universal cursor code adds a 593 * framebuffer while holding mutex locks. 594 * 595 * Note that a real deadlock between fpriv->fbs_lock and the modeset 596 * locks is impossible here since no one else but this function can get 597 * at it any more. 598 */ 599 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) { 600 if (drm_framebuffer_read_refcount(fb) > 1) { 601 list_move_tail(&fb->filp_head, &arg.fbs); 602 } else { 603 list_del_init(&fb->filp_head); 604 605 /* This drops the fpriv->fbs reference. */ 606 drm_framebuffer_put(fb); 607 } 608 } 609 610 if (!list_empty(&arg.fbs)) { 611 INIT_WORK_ONSTACK(&arg.work, drm_mode_rmfb_work_fn); 612 613 schedule_work(&arg.work); 614 flush_work(&arg.work); 615 destroy_work_on_stack(&arg.work); 616 } 617 } 618 619 void drm_framebuffer_free(struct kref *kref) 620 { 621 struct drm_framebuffer *fb = 622 container_of(kref, struct drm_framebuffer, base.refcount); 623 struct drm_device *dev = fb->dev; 624 625 /* 626 * The lookup idr holds a weak reference, which has not necessarily been 627 * removed at this point. Check for that. 628 */ 629 drm_mode_object_unregister(dev, &fb->base); 630 631 fb->funcs->destroy(fb); 632 } 633 634 /** 635 * drm_framebuffer_init - initialize a framebuffer 636 * @dev: DRM device 637 * @fb: framebuffer to be initialized 638 * @funcs: ... with these functions 639 * 640 * Allocates an ID for the framebuffer's parent mode object, sets its mode 641 * functions & device file and adds it to the master fd list. 642 * 643 * IMPORTANT: 644 * This functions publishes the fb and makes it available for concurrent access 645 * by other users. Which means by this point the fb _must_ be fully set up - 646 * since all the fb attributes are invariant over its lifetime, no further 647 * locking but only correct reference counting is required. 648 * 649 * Returns: 650 * Zero on success, error code on failure. 651 */ 652 int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb, 653 const struct drm_framebuffer_funcs *funcs) 654 { 655 int ret; 656 657 if (WARN_ON_ONCE(fb->dev != dev || !fb->format)) 658 return -EINVAL; 659 660 INIT_LIST_HEAD(&fb->filp_head); 661 662 fb->funcs = funcs; 663 664 ret = __drm_mode_object_add(dev, &fb->base, DRM_MODE_OBJECT_FB, 665 false, drm_framebuffer_free); 666 if (ret) 667 goto out; 668 669 mutex_lock(&dev->mode_config.fb_lock); 670 dev->mode_config.num_fb++; 671 list_add(&fb->head, &dev->mode_config.fb_list); 672 mutex_unlock(&dev->mode_config.fb_lock); 673 674 drm_mode_object_register(dev, &fb->base); 675 out: 676 return ret; 677 } 678 EXPORT_SYMBOL(drm_framebuffer_init); 679 680 /** 681 * drm_framebuffer_lookup - look up a drm framebuffer and grab a reference 682 * @dev: drm device 683 * @id: id of the fb object 684 * 685 * If successful, this grabs an additional reference to the framebuffer - 686 * callers need to make sure to eventually unreference the returned framebuffer 687 * again, using drm_framebuffer_put(). 688 */ 689 struct drm_framebuffer *drm_framebuffer_lookup(struct drm_device *dev, 690 uint32_t id) 691 { 692 struct drm_mode_object *obj; 693 struct drm_framebuffer *fb = NULL; 694 695 obj = __drm_mode_object_find(dev, id, DRM_MODE_OBJECT_FB); 696 if (obj) 697 fb = obj_to_fb(obj); 698 return fb; 699 } 700 EXPORT_SYMBOL(drm_framebuffer_lookup); 701 702 /** 703 * drm_framebuffer_unregister_private - unregister a private fb from the lookup idr 704 * @fb: fb to unregister 705 * 706 * Drivers need to call this when cleaning up driver-private framebuffers, e.g. 707 * those used for fbdev. Note that the caller must hold a reference of it's own, 708 * i.e. the object may not be destroyed through this call (since it'll lead to a 709 * locking inversion). 710 * 711 * NOTE: This function is deprecated. For driver-private framebuffers it is not 712 * recommended to embed a framebuffer struct info fbdev struct, instead, a 713 * framebuffer pointer is preferred and drm_framebuffer_put() should be called 714 * when the framebuffer is to be cleaned up. 715 */ 716 void drm_framebuffer_unregister_private(struct drm_framebuffer *fb) 717 { 718 struct drm_device *dev; 719 720 if (!fb) 721 return; 722 723 dev = fb->dev; 724 725 /* Mark fb as reaped and drop idr ref. */ 726 drm_mode_object_unregister(dev, &fb->base); 727 } 728 EXPORT_SYMBOL(drm_framebuffer_unregister_private); 729 730 /** 731 * drm_framebuffer_cleanup - remove a framebuffer object 732 * @fb: framebuffer to remove 733 * 734 * Cleanup framebuffer. This function is intended to be used from the drivers 735 * &drm_framebuffer_funcs.destroy callback. It can also be used to clean up 736 * driver private framebuffers embedded into a larger structure. 737 * 738 * Note that this function does not remove the fb from active usage - if it is 739 * still used anywhere, hilarity can ensue since userspace could call getfb on 740 * the id and get back -EINVAL. Obviously no concern at driver unload time. 741 * 742 * Also, the framebuffer will not be removed from the lookup idr - for 743 * user-created framebuffers this will happen in in the rmfb ioctl. For 744 * driver-private objects (e.g. for fbdev) drivers need to explicitly call 745 * drm_framebuffer_unregister_private. 746 */ 747 void drm_framebuffer_cleanup(struct drm_framebuffer *fb) 748 { 749 struct drm_device *dev = fb->dev; 750 751 mutex_lock(&dev->mode_config.fb_lock); 752 list_del(&fb->head); 753 dev->mode_config.num_fb--; 754 mutex_unlock(&dev->mode_config.fb_lock); 755 } 756 EXPORT_SYMBOL(drm_framebuffer_cleanup); 757 758 /** 759 * drm_framebuffer_remove - remove and unreference a framebuffer object 760 * @fb: framebuffer to remove 761 * 762 * Scans all the CRTCs and planes in @dev's mode_config. If they're 763 * using @fb, removes it, setting it to NULL. Then drops the reference to the 764 * passed-in framebuffer. Might take the modeset locks. 765 * 766 * Note that this function optimizes the cleanup away if the caller holds the 767 * last reference to the framebuffer. It is also guaranteed to not take the 768 * modeset locks in this case. 769 */ 770 void drm_framebuffer_remove(struct drm_framebuffer *fb) 771 { 772 struct drm_device *dev; 773 struct drm_crtc *crtc; 774 struct drm_plane *plane; 775 776 if (!fb) 777 return; 778 779 dev = fb->dev; 780 781 WARN_ON(!list_empty(&fb->filp_head)); 782 783 /* 784 * drm ABI mandates that we remove any deleted framebuffers from active 785 * useage. But since most sane clients only remove framebuffers they no 786 * longer need, try to optimize this away. 787 * 788 * Since we're holding a reference ourselves, observing a refcount of 1 789 * means that we're the last holder and can skip it. Also, the refcount 790 * can never increase from 1 again, so we don't need any barriers or 791 * locks. 792 * 793 * Note that userspace could try to race with use and instate a new 794 * usage _after_ we've cleared all current ones. End result will be an 795 * in-use fb with fb-id == 0. Userspace is allowed to shoot its own foot 796 * in this manner. 797 */ 798 if (drm_framebuffer_read_refcount(fb) > 1) { 799 if (drm_drv_uses_atomic_modeset(dev)) { 800 int ret = drm_atomic_remove_fb(fb); 801 WARN(ret, "atomic remove_fb failed with %i\n", ret); 802 goto out; 803 } 804 805 drm_modeset_lock_all(dev); 806 /* remove from any CRTC */ 807 drm_for_each_crtc(crtc, dev) { 808 if (crtc->primary->fb == fb) { 809 /* should turn off the crtc */ 810 if (drm_crtc_force_disable(crtc)) 811 DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc); 812 } 813 } 814 815 drm_for_each_plane(plane, dev) { 816 if (plane->fb == fb) 817 drm_plane_force_disable(plane); 818 } 819 drm_modeset_unlock_all(dev); 820 } 821 822 out: 823 drm_framebuffer_put(fb); 824 } 825 EXPORT_SYMBOL(drm_framebuffer_remove); 826 827 /** 828 * drm_framebuffer_plane_width - width of the plane given the first plane 829 * @width: width of the first plane 830 * @fb: the framebuffer 831 * @plane: plane index 832 * 833 * Returns: 834 * The width of @plane, given that the width of the first plane is @width. 835 */ 836 int drm_framebuffer_plane_width(int width, 837 const struct drm_framebuffer *fb, int plane) 838 { 839 if (plane >= fb->format->num_planes) 840 return 0; 841 842 return fb_plane_width(width, fb->format, plane); 843 } 844 EXPORT_SYMBOL(drm_framebuffer_plane_width); 845 846 /** 847 * drm_framebuffer_plane_height - height of the plane given the first plane 848 * @height: height of the first plane 849 * @fb: the framebuffer 850 * @plane: plane index 851 * 852 * Returns: 853 * The height of @plane, given that the height of the first plane is @height. 854 */ 855 int drm_framebuffer_plane_height(int height, 856 const struct drm_framebuffer *fb, int plane) 857 { 858 if (plane >= fb->format->num_planes) 859 return 0; 860 861 return fb_plane_height(height, fb->format, plane); 862 } 863 EXPORT_SYMBOL(drm_framebuffer_plane_height); 864