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 #ifdef _notyet 26 #include <drm/drm_auth.h> 27 #endif 28 #include <drm/drm_framebuffer.h> 29 #include <drm/drm_atomic.h> 30 #include <drm/drm_print.h> 31 32 #include "drm_internal.h" 33 #include "drm_crtc_internal.h" 34 35 /** 36 * DOC: overview 37 * 38 * Frame buffers are abstract memory objects that provide a source of pixels to 39 * scanout to a CRTC. Applications explicitly request the creation of frame 40 * buffers through the DRM_IOCTL_MODE_ADDFB(2) ioctls and receive an opaque 41 * handle that can be passed to the KMS CRTC control, plane configuration and 42 * page flip functions. 43 * 44 * Frame buffers rely on the underlying memory manager for allocating backing 45 * storage. When creating a frame buffer applications pass a memory handle 46 * (or a list of memory handles for multi-planar formats) through the 47 * &struct drm_mode_fb_cmd2 argument. For drivers using GEM as their userspace 48 * buffer management interface this would be a GEM handle. Drivers are however 49 * free to use their own backing storage object handles, e.g. vmwgfx directly 50 * exposes special TTM handles to userspace and so expects TTM handles in the 51 * create ioctl and not GEM handles. 52 * 53 * Framebuffers are tracked with &struct drm_framebuffer. They are published 54 * using drm_framebuffer_init() - after calling that function userspace can use 55 * and access the framebuffer object. The helper function 56 * drm_helper_mode_fill_fb_struct() can be used to pre-fill the required 57 * metadata fields. 58 * 59 * The lifetime of a drm framebuffer is controlled with a reference count, 60 * drivers can grab additional references with drm_framebuffer_get() and drop 61 * them again with drm_framebuffer_put(). For driver-private framebuffers for 62 * which the last reference is never dropped (e.g. for the fbdev framebuffer 63 * when the struct &struct drm_framebuffer is embedded into the fbdev helper 64 * struct) drivers can manually clean up a framebuffer at module unload time 65 * with drm_framebuffer_unregister_private(). But doing this is not 66 * recommended, and it's better to have a normal free-standing &struct 67 * drm_framebuffer. 68 */ 69 70 int drm_framebuffer_check_src_coords(uint32_t src_x, uint32_t src_y, 71 uint32_t src_w, uint32_t src_h, 72 const struct drm_framebuffer *fb) 73 { 74 unsigned int fb_width, fb_height; 75 76 fb_width = fb->width << 16; 77 fb_height = fb->height << 16; 78 79 /* Make sure source coordinates are inside the fb. */ 80 if (src_w > fb_width || 81 src_x > fb_width - src_w || 82 src_h > fb_height || 83 src_y > fb_height - src_h) { 84 DRM_DEBUG_KMS("Invalid source coordinates " 85 "%u.%06ux%u.%06u+%u.%06u+%u.%06u (fb %ux%u)\n", 86 src_w >> 16, ((src_w & 0xffff) * 15625) >> 10, 87 src_h >> 16, ((src_h & 0xffff) * 15625) >> 10, 88 src_x >> 16, ((src_x & 0xffff) * 15625) >> 10, 89 src_y >> 16, ((src_y & 0xffff) * 15625) >> 10, 90 fb->width, fb->height); 91 return -ENOSPC; 92 } 93 94 return 0; 95 } 96 97 /** 98 * drm_mode_addfb - add an FB to the graphics configuration 99 * @dev: drm device for the ioctl 100 * @or: pointer to request structure 101 * @file_priv: drm file 102 * 103 * Add a new FB to the specified CRTC, given a user request. This is the 104 * original addfb ioctl which only supported RGB formats. 105 * 106 * Called by the user via ioctl, or by an in-kernel client. 107 * 108 * Returns: 109 * Zero on success, negative errno on failure. 110 */ 111 int drm_mode_addfb(struct drm_device *dev, struct drm_mode_fb_cmd *or, 112 struct drm_file *file_priv) 113 { 114 struct drm_mode_fb_cmd2 r = {}; 115 int ret; 116 117 /* convert to new format and call new ioctl */ 118 r.fb_id = or->fb_id; 119 r.width = or->width; 120 r.height = or->height; 121 r.pitches[0] = or->pitch; 122 r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth); 123 r.handles[0] = or->handle; 124 125 if (r.pixel_format == DRM_FORMAT_XRGB2101010 && 126 dev->driver->driver_features & DRIVER_PREFER_XBGR_30BPP) 127 r.pixel_format = DRM_FORMAT_XBGR2101010; 128 129 ret = drm_mode_addfb2(dev, &r, file_priv); 130 if (ret) 131 return ret; 132 133 or->fb_id = r.fb_id; 134 135 return 0; 136 } 137 138 int drm_mode_addfb_ioctl(struct drm_device *dev, 139 void *data, struct drm_file *file_priv) 140 { 141 return drm_mode_addfb(dev, data, file_priv); 142 } 143 144 static int fb_plane_width(int width, 145 const struct drm_format_info *format, int plane) 146 { 147 if (plane == 0) 148 return width; 149 150 return DIV_ROUND_UP(width, format->hsub); 151 } 152 153 static int fb_plane_height(int height, 154 const struct drm_format_info *format, int plane) 155 { 156 if (plane == 0) 157 return height; 158 159 return DIV_ROUND_UP(height, format->vsub); 160 } 161 162 static int framebuffer_check(struct drm_device *dev, 163 const struct drm_mode_fb_cmd2 *r) 164 { 165 const struct drm_format_info *info; 166 int i; 167 168 /* check if the format is supported at all */ 169 info = __drm_format_info(r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN); 170 if (!info) { 171 #ifdef DRMDEBUG 172 struct drm_format_name_buf format_name; 173 #endif 174 175 DRM_DEBUG_KMS("bad framebuffer format %s\n", 176 drm_get_format_name(r->pixel_format, 177 &format_name)); 178 return -EINVAL; 179 } 180 181 /* now let the driver pick its own format info */ 182 info = drm_get_format_info(dev, r); 183 184 if (r->width == 0) { 185 DRM_DEBUG_KMS("bad framebuffer width %u\n", r->width); 186 return -EINVAL; 187 } 188 189 if (r->height == 0) { 190 DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height); 191 return -EINVAL; 192 } 193 194 for (i = 0; i < info->num_planes; i++) { 195 unsigned int width = fb_plane_width(r->width, info, i); 196 unsigned int height = fb_plane_height(r->height, info, i); 197 unsigned int cpp = info->cpp[i]; 198 199 if (!r->handles[i]) { 200 DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i); 201 return -EINVAL; 202 } 203 204 if ((uint64_t) width * cpp > UINT_MAX) 205 return -ERANGE; 206 207 if ((uint64_t) height * r->pitches[i] + r->offsets[i] > UINT_MAX) 208 return -ERANGE; 209 210 if (r->pitches[i] < width * cpp) { 211 DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i); 212 return -EINVAL; 213 } 214 215 if (r->modifier[i] && !(r->flags & DRM_MODE_FB_MODIFIERS)) { 216 DRM_DEBUG_KMS("bad fb modifier %llu for plane %d\n", 217 r->modifier[i], i); 218 return -EINVAL; 219 } 220 221 if (r->flags & DRM_MODE_FB_MODIFIERS && 222 r->modifier[i] != r->modifier[0]) { 223 DRM_DEBUG_KMS("bad fb modifier %llu for plane %d\n", 224 r->modifier[i], i); 225 return -EINVAL; 226 } 227 228 /* modifier specific checks: */ 229 switch (r->modifier[i]) { 230 case DRM_FORMAT_MOD_SAMSUNG_64_32_TILE: 231 /* NOTE: the pitch restriction may be lifted later if it turns 232 * out that no hw has this restriction: 233 */ 234 if (r->pixel_format != DRM_FORMAT_NV12 || 235 width % 128 || height % 32 || 236 r->pitches[i] % 128) { 237 DRM_DEBUG_KMS("bad modifier data for plane %d\n", i); 238 return -EINVAL; 239 } 240 break; 241 242 default: 243 break; 244 } 245 } 246 247 for (i = info->num_planes; i < 4; i++) { 248 if (r->modifier[i]) { 249 DRM_DEBUG_KMS("non-zero modifier for unused plane %d\n", i); 250 return -EINVAL; 251 } 252 253 /* Pre-FB_MODIFIERS userspace didn't clear the structs properly. */ 254 if (!(r->flags & DRM_MODE_FB_MODIFIERS)) 255 continue; 256 257 if (r->handles[i]) { 258 DRM_DEBUG_KMS("buffer object handle for unused plane %d\n", i); 259 return -EINVAL; 260 } 261 262 if (r->pitches[i]) { 263 DRM_DEBUG_KMS("non-zero pitch for unused plane %d\n", i); 264 return -EINVAL; 265 } 266 267 if (r->offsets[i]) { 268 DRM_DEBUG_KMS("non-zero offset for unused plane %d\n", i); 269 return -EINVAL; 270 } 271 } 272 273 return 0; 274 } 275 276 struct drm_framebuffer * 277 drm_internal_framebuffer_create(struct drm_device *dev, 278 const struct drm_mode_fb_cmd2 *r, 279 struct drm_file *file_priv) 280 { 281 struct drm_mode_config *config = &dev->mode_config; 282 struct drm_framebuffer *fb; 283 int ret; 284 285 if (r->flags & ~(DRM_MODE_FB_INTERLACED | DRM_MODE_FB_MODIFIERS)) { 286 DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r->flags); 287 return ERR_PTR(-EINVAL); 288 } 289 290 if ((config->min_width > r->width) || (r->width > config->max_width)) { 291 DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n", 292 r->width, config->min_width, config->max_width); 293 return ERR_PTR(-EINVAL); 294 } 295 if ((config->min_height > r->height) || (r->height > config->max_height)) { 296 DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n", 297 r->height, config->min_height, config->max_height); 298 return ERR_PTR(-EINVAL); 299 } 300 301 if (r->flags & DRM_MODE_FB_MODIFIERS && 302 !dev->mode_config.allow_fb_modifiers) { 303 DRM_DEBUG_KMS("driver does not support fb modifiers\n"); 304 return ERR_PTR(-EINVAL); 305 } 306 307 ret = framebuffer_check(dev, r); 308 if (ret) 309 return ERR_PTR(ret); 310 311 fb = dev->mode_config.funcs->fb_create(dev, file_priv, r); 312 if (IS_ERR(fb)) { 313 DRM_DEBUG_KMS("could not create framebuffer\n"); 314 return fb; 315 } 316 317 return fb; 318 } 319 320 /** 321 * drm_mode_addfb2 - add an FB to the graphics configuration 322 * @dev: drm device for the ioctl 323 * @data: data pointer for the ioctl 324 * @file_priv: drm file for the ioctl call 325 * 326 * Add a new FB to the specified CRTC, given a user request with format. This is 327 * the 2nd version of the addfb ioctl, which supports multi-planar framebuffers 328 * and uses fourcc codes as pixel format specifiers. 329 * 330 * Called by the user via ioctl. 331 * 332 * Returns: 333 * Zero on success, negative errno on failure. 334 */ 335 int drm_mode_addfb2(struct drm_device *dev, 336 void *data, struct drm_file *file_priv) 337 { 338 struct drm_mode_fb_cmd2 *r = data; 339 struct drm_framebuffer *fb; 340 341 if (!drm_core_check_feature(dev, DRIVER_MODESET)) 342 return -EINVAL; 343 344 fb = drm_internal_framebuffer_create(dev, r, file_priv); 345 if (IS_ERR(fb)) 346 return PTR_ERR(fb); 347 348 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id); 349 r->fb_id = fb->base.id; 350 351 /* Transfer ownership to the filp for reaping on close */ 352 mutex_lock(&file_priv->fbs_lock); 353 list_add(&fb->filp_head, &file_priv->fbs); 354 mutex_unlock(&file_priv->fbs_lock); 355 356 return 0; 357 } 358 359 struct drm_mode_rmfb_work { 360 struct work_struct work; 361 struct list_head fbs; 362 }; 363 364 static void drm_mode_rmfb_work_fn(struct work_struct *w) 365 { 366 struct drm_mode_rmfb_work *arg = container_of(w, typeof(*arg), work); 367 368 while (!list_empty(&arg->fbs)) { 369 struct drm_framebuffer *fb = 370 list_first_entry(&arg->fbs, typeof(*fb), filp_head); 371 372 list_del_init(&fb->filp_head); 373 drm_framebuffer_remove(fb); 374 } 375 } 376 377 /** 378 * drm_mode_rmfb - remove an FB from the configuration 379 * @dev: drm device 380 * @fb_id: id of framebuffer to remove 381 * @file_priv: drm file 382 * 383 * Remove the specified FB. 384 * 385 * Called by the user via ioctl, or by an in-kernel client. 386 * 387 * Returns: 388 * Zero on success, negative errno on failure. 389 */ 390 int drm_mode_rmfb(struct drm_device *dev, u32 fb_id, 391 struct drm_file *file_priv) 392 { 393 struct drm_framebuffer *fb = NULL; 394 struct drm_framebuffer *fbl = NULL; 395 int found = 0; 396 397 if (!drm_core_check_feature(dev, DRIVER_MODESET)) 398 return -EINVAL; 399 400 fb = drm_framebuffer_lookup(dev, file_priv, fb_id); 401 if (!fb) 402 return -ENOENT; 403 404 mutex_lock(&file_priv->fbs_lock); 405 list_for_each_entry(fbl, &file_priv->fbs, filp_head) 406 if (fb == fbl) 407 found = 1; 408 if (!found) { 409 mutex_unlock(&file_priv->fbs_lock); 410 goto fail_unref; 411 } 412 413 list_del_init(&fb->filp_head); 414 mutex_unlock(&file_priv->fbs_lock); 415 416 /* drop the reference we picked up in framebuffer lookup */ 417 drm_framebuffer_put(fb); 418 419 /* 420 * we now own the reference that was stored in the fbs list 421 * 422 * drm_framebuffer_remove may fail with -EINTR on pending signals, 423 * so run this in a separate stack as there's no way to correctly 424 * handle this after the fb is already removed from the lookup table. 425 */ 426 if (drm_framebuffer_read_refcount(fb) > 1) { 427 struct drm_mode_rmfb_work arg; 428 429 INIT_WORK_ONSTACK(&arg.work, drm_mode_rmfb_work_fn); 430 INIT_LIST_HEAD(&arg.fbs); 431 list_add_tail(&fb->filp_head, &arg.fbs); 432 433 schedule_work(&arg.work); 434 flush_work(&arg.work); 435 destroy_work_on_stack(&arg.work); 436 } else 437 drm_framebuffer_put(fb); 438 439 return 0; 440 441 fail_unref: 442 drm_framebuffer_put(fb); 443 return -ENOENT; 444 } 445 446 int drm_mode_rmfb_ioctl(struct drm_device *dev, 447 void *data, struct drm_file *file_priv) 448 { 449 uint32_t *fb_id = data; 450 451 return drm_mode_rmfb(dev, *fb_id, file_priv); 452 } 453 454 /** 455 * drm_mode_getfb - get FB info 456 * @dev: drm device for the ioctl 457 * @data: data pointer for the ioctl 458 * @file_priv: drm file for the ioctl call 459 * 460 * Lookup the FB given its ID and return info about it. 461 * 462 * Called by the user via ioctl. 463 * 464 * Returns: 465 * Zero on success, negative errno on failure. 466 */ 467 int drm_mode_getfb(struct drm_device *dev, 468 void *data, struct drm_file *file_priv) 469 { 470 struct drm_mode_fb_cmd *r = data; 471 struct drm_framebuffer *fb; 472 int ret; 473 474 if (!drm_core_check_feature(dev, DRIVER_MODESET)) 475 return -EINVAL; 476 477 fb = drm_framebuffer_lookup(dev, file_priv, r->fb_id); 478 if (!fb) 479 return -ENOENT; 480 481 /* Multi-planar framebuffers need getfb2. */ 482 if (fb->format->num_planes > 1) { 483 ret = -EINVAL; 484 goto out; 485 } 486 487 if (!fb->funcs->create_handle) { 488 ret = -ENODEV; 489 goto out; 490 } 491 492 r->height = fb->height; 493 r->width = fb->width; 494 r->depth = fb->format->depth; 495 r->bpp = fb->format->cpp[0] * 8; 496 r->pitch = fb->pitches[0]; 497 498 /* GET_FB() is an unprivileged ioctl so we must not return a 499 * buffer-handle to non-master processes! For 500 * backwards-compatibility reasons, we cannot make GET_FB() privileged, 501 * so just return an invalid handle for non-masters. 502 */ 503 if (!drm_is_current_master(file_priv) && !capable(CAP_SYS_ADMIN)) { 504 r->handle = 0; 505 ret = 0; 506 goto out; 507 } 508 509 ret = fb->funcs->create_handle(fb, file_priv, &r->handle); 510 511 out: 512 drm_framebuffer_put(fb); 513 514 return ret; 515 } 516 517 /** 518 * drm_mode_dirtyfb_ioctl - flush frontbuffer rendering on an FB 519 * @dev: drm device for the ioctl 520 * @data: data pointer for the ioctl 521 * @file_priv: drm file for the ioctl call 522 * 523 * Lookup the FB and flush out the damaged area supplied by userspace as a clip 524 * rectangle list. Generic userspace which does frontbuffer rendering must call 525 * this ioctl to flush out the changes on manual-update display outputs, e.g. 526 * usb display-link, mipi manual update panels or edp panel self refresh modes. 527 * 528 * Modesetting drivers which always update the frontbuffer do not need to 529 * implement the corresponding &drm_framebuffer_funcs.dirty callback. 530 * 531 * Called by the user via ioctl. 532 * 533 * Returns: 534 * Zero on success, negative errno on failure. 535 */ 536 int drm_mode_dirtyfb_ioctl(struct drm_device *dev, 537 void *data, struct drm_file *file_priv) 538 { 539 struct drm_clip_rect __user *clips_ptr; 540 struct drm_clip_rect *clips = NULL; 541 struct drm_mode_fb_dirty_cmd *r = data; 542 struct drm_framebuffer *fb; 543 unsigned flags; 544 int num_clips; 545 int ret; 546 547 if (!drm_core_check_feature(dev, DRIVER_MODESET)) 548 return -EINVAL; 549 550 fb = drm_framebuffer_lookup(dev, file_priv, r->fb_id); 551 if (!fb) 552 return -ENOENT; 553 554 num_clips = r->num_clips; 555 clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr; 556 557 if (!num_clips != !clips_ptr) { 558 ret = -EINVAL; 559 goto out_err1; 560 } 561 562 flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags; 563 564 /* If userspace annotates copy, clips must come in pairs */ 565 if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) { 566 ret = -EINVAL; 567 goto out_err1; 568 } 569 570 if (num_clips && clips_ptr) { 571 if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) { 572 ret = -EINVAL; 573 goto out_err1; 574 } 575 clips = kcalloc(num_clips, sizeof(*clips), GFP_KERNEL); 576 if (!clips) { 577 ret = -ENOMEM; 578 goto out_err1; 579 } 580 581 ret = copy_from_user(clips, clips_ptr, 582 num_clips * sizeof(*clips)); 583 if (ret) { 584 ret = -EFAULT; 585 goto out_err2; 586 } 587 } 588 589 if (fb->funcs->dirty) { 590 ret = fb->funcs->dirty(fb, file_priv, flags, r->color, 591 clips, num_clips); 592 } else { 593 ret = -ENOSYS; 594 } 595 596 out_err2: 597 kfree(clips); 598 out_err1: 599 drm_framebuffer_put(fb); 600 601 return ret; 602 } 603 604 /** 605 * drm_fb_release - remove and free the FBs on this file 606 * @priv: drm file for the ioctl 607 * 608 * Destroy all the FBs associated with @filp. 609 * 610 * Called by the user via ioctl. 611 * 612 * Returns: 613 * Zero on success, negative errno on failure. 614 */ 615 void drm_fb_release(struct drm_file *priv) 616 { 617 struct drm_framebuffer *fb, *tfb; 618 struct drm_mode_rmfb_work arg; 619 620 INIT_LIST_HEAD(&arg.fbs); 621 622 /* 623 * When the file gets released that means no one else can access the fb 624 * list any more, so no need to grab fpriv->fbs_lock. And we need to 625 * avoid upsetting lockdep since the universal cursor code adds a 626 * framebuffer while holding mutex locks. 627 * 628 * Note that a real deadlock between fpriv->fbs_lock and the modeset 629 * locks is impossible here since no one else but this function can get 630 * at it any more. 631 */ 632 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) { 633 if (drm_framebuffer_read_refcount(fb) > 1) { 634 list_move_tail(&fb->filp_head, &arg.fbs); 635 } else { 636 list_del_init(&fb->filp_head); 637 638 /* This drops the fpriv->fbs reference. */ 639 drm_framebuffer_put(fb); 640 } 641 } 642 643 if (!list_empty(&arg.fbs)) { 644 INIT_WORK_ONSTACK(&arg.work, drm_mode_rmfb_work_fn); 645 646 schedule_work(&arg.work); 647 flush_work(&arg.work); 648 destroy_work_on_stack(&arg.work); 649 } 650 } 651 652 void drm_framebuffer_free(struct kref *kref) 653 { 654 struct drm_framebuffer *fb = 655 container_of(kref, struct drm_framebuffer, base.refcount); 656 struct drm_device *dev = fb->dev; 657 658 /* 659 * The lookup idr holds a weak reference, which has not necessarily been 660 * removed at this point. Check for that. 661 */ 662 drm_mode_object_unregister(dev, &fb->base); 663 664 fb->funcs->destroy(fb); 665 } 666 667 /** 668 * drm_framebuffer_init - initialize a framebuffer 669 * @dev: DRM device 670 * @fb: framebuffer to be initialized 671 * @funcs: ... with these functions 672 * 673 * Allocates an ID for the framebuffer's parent mode object, sets its mode 674 * functions & device file and adds it to the master fd list. 675 * 676 * IMPORTANT: 677 * This functions publishes the fb and makes it available for concurrent access 678 * by other users. Which means by this point the fb _must_ be fully set up - 679 * since all the fb attributes are invariant over its lifetime, no further 680 * locking but only correct reference counting is required. 681 * 682 * Returns: 683 * Zero on success, error code on failure. 684 */ 685 int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb, 686 const struct drm_framebuffer_funcs *funcs) 687 { 688 int ret; 689 690 if (WARN_ON_ONCE(fb->dev != dev || !fb->format)) 691 return -EINVAL; 692 693 INIT_LIST_HEAD(&fb->filp_head); 694 695 fb->funcs = funcs; 696 #ifdef __notyet__ 697 strlcpy(fb->comm, current->comm, sizeof(fb->comm)); 698 #endif 699 700 ret = __drm_mode_object_add(dev, &fb->base, DRM_MODE_OBJECT_FB, 701 false, drm_framebuffer_free); 702 if (ret) 703 goto out; 704 705 mutex_lock(&dev->mode_config.fb_lock); 706 dev->mode_config.num_fb++; 707 list_add(&fb->head, &dev->mode_config.fb_list); 708 mutex_unlock(&dev->mode_config.fb_lock); 709 710 drm_mode_object_register(dev, &fb->base); 711 out: 712 return ret; 713 } 714 EXPORT_SYMBOL(drm_framebuffer_init); 715 716 /** 717 * drm_framebuffer_lookup - look up a drm framebuffer and grab a reference 718 * @dev: drm device 719 * @file_priv: drm file to check for lease against. 720 * @id: id of the fb object 721 * 722 * If successful, this grabs an additional reference to the framebuffer - 723 * callers need to make sure to eventually unreference the returned framebuffer 724 * again, using drm_framebuffer_put(). 725 */ 726 struct drm_framebuffer *drm_framebuffer_lookup(struct drm_device *dev, 727 struct drm_file *file_priv, 728 uint32_t id) 729 { 730 struct drm_mode_object *obj; 731 struct drm_framebuffer *fb = NULL; 732 733 obj = __drm_mode_object_find(dev, file_priv, id, DRM_MODE_OBJECT_FB); 734 if (obj) 735 fb = obj_to_fb(obj); 736 return fb; 737 } 738 EXPORT_SYMBOL(drm_framebuffer_lookup); 739 740 /** 741 * drm_framebuffer_unregister_private - unregister a private fb from the lookup idr 742 * @fb: fb to unregister 743 * 744 * Drivers need to call this when cleaning up driver-private framebuffers, e.g. 745 * those used for fbdev. Note that the caller must hold a reference of it's own, 746 * i.e. the object may not be destroyed through this call (since it'll lead to a 747 * locking inversion). 748 * 749 * NOTE: This function is deprecated. For driver-private framebuffers it is not 750 * recommended to embed a framebuffer struct info fbdev struct, instead, a 751 * framebuffer pointer is preferred and drm_framebuffer_put() should be called 752 * when the framebuffer is to be cleaned up. 753 */ 754 void drm_framebuffer_unregister_private(struct drm_framebuffer *fb) 755 { 756 struct drm_device *dev; 757 758 if (!fb) 759 return; 760 761 dev = fb->dev; 762 763 /* Mark fb as reaped and drop idr ref. */ 764 drm_mode_object_unregister(dev, &fb->base); 765 } 766 EXPORT_SYMBOL(drm_framebuffer_unregister_private); 767 768 /** 769 * drm_framebuffer_cleanup - remove a framebuffer object 770 * @fb: framebuffer to remove 771 * 772 * Cleanup framebuffer. This function is intended to be used from the drivers 773 * &drm_framebuffer_funcs.destroy callback. It can also be used to clean up 774 * driver private framebuffers embedded into a larger structure. 775 * 776 * Note that this function does not remove the fb from active usage - if it is 777 * still used anywhere, hilarity can ensue since userspace could call getfb on 778 * the id and get back -EINVAL. Obviously no concern at driver unload time. 779 * 780 * Also, the framebuffer will not be removed from the lookup idr - for 781 * user-created framebuffers this will happen in in the rmfb ioctl. For 782 * driver-private objects (e.g. for fbdev) drivers need to explicitly call 783 * drm_framebuffer_unregister_private. 784 */ 785 void drm_framebuffer_cleanup(struct drm_framebuffer *fb) 786 { 787 struct drm_device *dev = fb->dev; 788 789 mutex_lock(&dev->mode_config.fb_lock); 790 list_del(&fb->head); 791 dev->mode_config.num_fb--; 792 mutex_unlock(&dev->mode_config.fb_lock); 793 } 794 EXPORT_SYMBOL(drm_framebuffer_cleanup); 795 796 static int atomic_remove_fb(struct drm_framebuffer *fb) 797 { 798 struct drm_modeset_acquire_ctx ctx; 799 struct drm_device *dev = fb->dev; 800 struct drm_atomic_state *state; 801 struct drm_plane *plane; 802 struct drm_connector *conn __maybe_unused; 803 struct drm_connector_state *conn_state; 804 int i, ret; 805 unsigned plane_mask; 806 bool disable_crtcs = false; 807 808 retry_disable: 809 drm_modeset_acquire_init(&ctx, 0); 810 811 state = drm_atomic_state_alloc(dev); 812 if (!state) { 813 ret = -ENOMEM; 814 goto out; 815 } 816 state->acquire_ctx = &ctx; 817 818 retry: 819 plane_mask = 0; 820 ret = drm_modeset_lock_all_ctx(dev, &ctx); 821 if (ret) 822 goto unlock; 823 824 drm_for_each_plane(plane, dev) { 825 struct drm_plane_state *plane_state; 826 827 if (plane->state->fb != fb) 828 continue; 829 830 plane_state = drm_atomic_get_plane_state(state, plane); 831 if (IS_ERR(plane_state)) { 832 ret = PTR_ERR(plane_state); 833 goto unlock; 834 } 835 836 if (disable_crtcs && plane_state->crtc->primary == plane) { 837 struct drm_crtc_state *crtc_state; 838 839 crtc_state = drm_atomic_get_existing_crtc_state(state, plane_state->crtc); 840 841 ret = drm_atomic_add_affected_connectors(state, plane_state->crtc); 842 if (ret) 843 goto unlock; 844 845 crtc_state->active = false; 846 ret = drm_atomic_set_mode_for_crtc(crtc_state, NULL); 847 if (ret) 848 goto unlock; 849 } 850 851 drm_atomic_set_fb_for_plane(plane_state, NULL); 852 ret = drm_atomic_set_crtc_for_plane(plane_state, NULL); 853 if (ret) 854 goto unlock; 855 856 plane_mask |= drm_plane_mask(plane); 857 } 858 859 /* This list is only filled when disable_crtcs is set. */ 860 for_each_new_connector_in_state(state, conn, conn_state, i) { 861 ret = drm_atomic_set_crtc_for_connector(conn_state, NULL); 862 863 if (ret) 864 goto unlock; 865 } 866 867 if (plane_mask) 868 ret = drm_atomic_commit(state); 869 870 unlock: 871 if (ret == -EDEADLK) { 872 drm_atomic_state_clear(state); 873 drm_modeset_backoff(&ctx); 874 goto retry; 875 } 876 877 drm_atomic_state_put(state); 878 879 out: 880 drm_modeset_drop_locks(&ctx); 881 drm_modeset_acquire_fini(&ctx); 882 883 if (ret == -EINVAL && !disable_crtcs) { 884 disable_crtcs = true; 885 goto retry_disable; 886 } 887 888 return ret; 889 } 890 891 static void legacy_remove_fb(struct drm_framebuffer *fb) 892 { 893 struct drm_device *dev = fb->dev; 894 struct drm_crtc *crtc; 895 struct drm_plane *plane; 896 897 drm_modeset_lock_all(dev); 898 /* remove from any CRTC */ 899 drm_for_each_crtc(crtc, dev) { 900 if (crtc->primary->fb == fb) { 901 /* should turn off the crtc */ 902 if (drm_crtc_force_disable(crtc)) 903 DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc); 904 } 905 } 906 907 drm_for_each_plane(plane, dev) { 908 if (plane->fb == fb) 909 drm_plane_force_disable(plane); 910 } 911 drm_modeset_unlock_all(dev); 912 } 913 914 /** 915 * drm_framebuffer_remove - remove and unreference a framebuffer object 916 * @fb: framebuffer to remove 917 * 918 * Scans all the CRTCs and planes in @dev's mode_config. If they're 919 * using @fb, removes it, setting it to NULL. Then drops the reference to the 920 * passed-in framebuffer. Might take the modeset locks. 921 * 922 * Note that this function optimizes the cleanup away if the caller holds the 923 * last reference to the framebuffer. It is also guaranteed to not take the 924 * modeset locks in this case. 925 */ 926 void drm_framebuffer_remove(struct drm_framebuffer *fb) 927 { 928 struct drm_device *dev; 929 930 if (!fb) 931 return; 932 933 dev = fb->dev; 934 935 WARN_ON(!list_empty(&fb->filp_head)); 936 937 /* 938 * drm ABI mandates that we remove any deleted framebuffers from active 939 * useage. But since most sane clients only remove framebuffers they no 940 * longer need, try to optimize this away. 941 * 942 * Since we're holding a reference ourselves, observing a refcount of 1 943 * means that we're the last holder and can skip it. Also, the refcount 944 * can never increase from 1 again, so we don't need any barriers or 945 * locks. 946 * 947 * Note that userspace could try to race with use and instate a new 948 * usage _after_ we've cleared all current ones. End result will be an 949 * in-use fb with fb-id == 0. Userspace is allowed to shoot its own foot 950 * in this manner. 951 */ 952 if (drm_framebuffer_read_refcount(fb) > 1) { 953 if (drm_drv_uses_atomic_modeset(dev)) { 954 int ret = atomic_remove_fb(fb); 955 WARN(ret, "atomic remove_fb failed with %i\n", ret); 956 } else 957 legacy_remove_fb(fb); 958 } 959 960 drm_framebuffer_put(fb); 961 } 962 EXPORT_SYMBOL(drm_framebuffer_remove); 963 964 /** 965 * drm_framebuffer_plane_width - width of the plane given the first plane 966 * @width: width of the first plane 967 * @fb: the framebuffer 968 * @plane: plane index 969 * 970 * Returns: 971 * The width of @plane, given that the width of the first plane is @width. 972 */ 973 int drm_framebuffer_plane_width(int width, 974 const struct drm_framebuffer *fb, int plane) 975 { 976 if (plane >= fb->format->num_planes) 977 return 0; 978 979 return fb_plane_width(width, fb->format, plane); 980 } 981 EXPORT_SYMBOL(drm_framebuffer_plane_width); 982 983 /** 984 * drm_framebuffer_plane_height - height of the plane given the first plane 985 * @height: height of the first plane 986 * @fb: the framebuffer 987 * @plane: plane index 988 * 989 * Returns: 990 * The height of @plane, given that the height of the first plane is @height. 991 */ 992 int drm_framebuffer_plane_height(int height, 993 const struct drm_framebuffer *fb, int plane) 994 { 995 if (plane >= fb->format->num_planes) 996 return 0; 997 998 return fb_plane_height(height, fb->format, plane); 999 } 1000 EXPORT_SYMBOL(drm_framebuffer_plane_height); 1001 1002 void drm_framebuffer_print_info(struct drm_printer *p, unsigned int indent, 1003 const struct drm_framebuffer *fb) 1004 { 1005 struct drm_format_name_buf format_name; 1006 unsigned int i; 1007 1008 drm_printf_indent(p, indent, "allocated by = %s\n", fb->comm); 1009 drm_printf_indent(p, indent, "refcount=%u\n", 1010 drm_framebuffer_read_refcount(fb)); 1011 drm_printf_indent(p, indent, "format=%s\n", 1012 drm_get_format_name(fb->format->format, &format_name)); 1013 drm_printf_indent(p, indent, "modifier=0x%llx\n", fb->modifier); 1014 drm_printf_indent(p, indent, "size=%ux%u\n", fb->width, fb->height); 1015 drm_printf_indent(p, indent, "layers:\n"); 1016 1017 for (i = 0; i < fb->format->num_planes; i++) { 1018 drm_printf_indent(p, indent + 1, "size[%u]=%dx%d\n", i, 1019 drm_framebuffer_plane_width(fb->width, fb, i), 1020 drm_framebuffer_plane_height(fb->height, fb, i)); 1021 drm_printf_indent(p, indent + 1, "pitch[%u]=%u\n", i, fb->pitches[i]); 1022 drm_printf_indent(p, indent + 1, "offset[%u]=%u\n", i, fb->offsets[i]); 1023 drm_printf_indent(p, indent + 1, "obj[%u]:%s\n", i, 1024 fb->obj[i] ? "" : "(null)"); 1025 if (fb->obj[i]) 1026 drm_gem_print_info(p, indent + 2, fb->obj[i]); 1027 } 1028 } 1029 1030 #ifdef CONFIG_DEBUG_FS 1031 static int drm_framebuffer_info(struct seq_file *m, void *data) 1032 { 1033 struct drm_info_node *node = m->private; 1034 struct drm_device *dev = node->minor->dev; 1035 struct drm_printer p = drm_seq_file_printer(m); 1036 struct drm_framebuffer *fb; 1037 1038 mutex_lock(&dev->mode_config.fb_lock); 1039 drm_for_each_fb(fb, dev) { 1040 drm_printf(&p, "framebuffer[%u]:\n", fb->base.id); 1041 drm_framebuffer_print_info(&p, 1, fb); 1042 } 1043 mutex_unlock(&dev->mode_config.fb_lock); 1044 1045 return 0; 1046 } 1047 1048 static const struct drm_info_list drm_framebuffer_debugfs_list[] = { 1049 { "framebuffer", drm_framebuffer_info, 0 }, 1050 }; 1051 1052 int drm_framebuffer_debugfs_init(struct drm_minor *minor) 1053 { 1054 return drm_debugfs_create_files(drm_framebuffer_debugfs_list, 1055 ARRAY_SIZE(drm_framebuffer_debugfs_list), 1056 minor->debugfs_root, minor); 1057 } 1058 #endif 1059