2 * Copyright (c) 2016 Intel Corporation
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.
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
23 #include <linux/export.h>
25 #include <drm/drm_auth.h>
26 #include <drm/drm_framebuffer.h>
28 #include "drm_crtc_internal.h"
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.
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.
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
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
66 * drm_mode_addfb - add an FB to the graphics configuration
67 * @dev: drm device for the ioctl
68 * @data: data pointer for the ioctl
69 * @file_priv: drm file for the ioctl call
71 * Add a new FB to the specified CRTC, given a user request. This is the
72 * original addfb ioctl which only supported RGB formats.
74 * Called by the user via ioctl.
77 * Zero on success, negative errno on failure.
79 int drm_mode_addfb(struct drm_device *dev,
80 void *data, struct drm_file *file_priv)
82 struct drm_mode_fb_cmd *or = data;
83 struct drm_mode_fb_cmd2 r = {};
86 /* convert to new format and call new ioctl */
89 r.height = or->height;
90 r.pitches[0] = or->pitch;
91 r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
92 r.handles[0] = or->handle;
94 ret = drm_mode_addfb2(dev, &r, file_priv);
103 static int format_check(const struct drm_mode_fb_cmd2 *r)
105 uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
110 case DRM_FORMAT_RGB332:
111 case DRM_FORMAT_BGR233:
112 case DRM_FORMAT_XRGB4444:
113 case DRM_FORMAT_XBGR4444:
114 case DRM_FORMAT_RGBX4444:
115 case DRM_FORMAT_BGRX4444:
116 case DRM_FORMAT_ARGB4444:
117 case DRM_FORMAT_ABGR4444:
118 case DRM_FORMAT_RGBA4444:
119 case DRM_FORMAT_BGRA4444:
120 case DRM_FORMAT_XRGB1555:
121 case DRM_FORMAT_XBGR1555:
122 case DRM_FORMAT_RGBX5551:
123 case DRM_FORMAT_BGRX5551:
124 case DRM_FORMAT_ARGB1555:
125 case DRM_FORMAT_ABGR1555:
126 case DRM_FORMAT_RGBA5551:
127 case DRM_FORMAT_BGRA5551:
128 case DRM_FORMAT_RGB565:
129 case DRM_FORMAT_BGR565:
130 case DRM_FORMAT_RGB888:
131 case DRM_FORMAT_BGR888:
132 case DRM_FORMAT_XRGB8888:
133 case DRM_FORMAT_XBGR8888:
134 case DRM_FORMAT_RGBX8888:
135 case DRM_FORMAT_BGRX8888:
136 case DRM_FORMAT_ARGB8888:
137 case DRM_FORMAT_ABGR8888:
138 case DRM_FORMAT_RGBA8888:
139 case DRM_FORMAT_BGRA8888:
140 case DRM_FORMAT_XRGB2101010:
141 case DRM_FORMAT_XBGR2101010:
142 case DRM_FORMAT_RGBX1010102:
143 case DRM_FORMAT_BGRX1010102:
144 case DRM_FORMAT_ARGB2101010:
145 case DRM_FORMAT_ABGR2101010:
146 case DRM_FORMAT_RGBA1010102:
147 case DRM_FORMAT_BGRA1010102:
148 case DRM_FORMAT_YUYV:
149 case DRM_FORMAT_YVYU:
150 case DRM_FORMAT_UYVY:
151 case DRM_FORMAT_VYUY:
152 case DRM_FORMAT_AYUV:
153 case DRM_FORMAT_NV12:
154 case DRM_FORMAT_NV21:
155 case DRM_FORMAT_NV16:
156 case DRM_FORMAT_NV61:
157 case DRM_FORMAT_NV24:
158 case DRM_FORMAT_NV42:
159 case DRM_FORMAT_YUV410:
160 case DRM_FORMAT_YVU410:
161 case DRM_FORMAT_YUV411:
162 case DRM_FORMAT_YVU411:
163 case DRM_FORMAT_YUV420:
164 case DRM_FORMAT_YVU420:
165 case DRM_FORMAT_YUV422:
166 case DRM_FORMAT_YVU422:
167 case DRM_FORMAT_YUV444:
168 case DRM_FORMAT_YVU444:
171 format_name = drm_get_format_name(r->pixel_format);
172 DRM_DEBUG_KMS("invalid pixel format %s\n", format_name);
178 static int framebuffer_check(const struct drm_mode_fb_cmd2 *r)
180 int ret, hsub, vsub, num_planes, i;
182 ret = format_check(r);
184 char *format_name = drm_get_format_name(r->pixel_format);
185 DRM_DEBUG_KMS("bad framebuffer format %s\n", format_name);
190 hsub = drm_format_horz_chroma_subsampling(r->pixel_format);
191 vsub = drm_format_vert_chroma_subsampling(r->pixel_format);
192 num_planes = drm_format_num_planes(r->pixel_format);
194 if (r->width == 0 || r->width % hsub) {
195 DRM_DEBUG_KMS("bad framebuffer width %u\n", r->width);
199 if (r->height == 0 || r->height % vsub) {
200 DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height);
204 for (i = 0; i < num_planes; i++) {
205 unsigned int width = r->width / (i != 0 ? hsub : 1);
206 unsigned int height = r->height / (i != 0 ? vsub : 1);
207 unsigned int cpp = drm_format_plane_cpp(r->pixel_format, i);
209 if (!r->handles[i]) {
210 DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i);
214 if ((uint64_t) width * cpp > UINT_MAX)
217 if ((uint64_t) height * r->pitches[i] + r->offsets[i] > UINT_MAX)
220 if (r->pitches[i] < width * cpp) {
221 DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i);
225 if (r->modifier[i] && !(r->flags & DRM_MODE_FB_MODIFIERS)) {
226 DRM_DEBUG_KMS("bad fb modifier %llu for plane %d\n",
231 /* modifier specific checks: */
232 switch (r->modifier[i]) {
233 case DRM_FORMAT_MOD_SAMSUNG_64_32_TILE:
234 /* NOTE: the pitch restriction may be lifted later if it turns
235 * out that no hw has this restriction:
237 if (r->pixel_format != DRM_FORMAT_NV12 ||
238 width % 128 || height % 32 ||
239 r->pitches[i] % 128) {
240 DRM_DEBUG_KMS("bad modifier data for plane %d\n", i);
250 for (i = num_planes; i < 4; i++) {
251 if (r->modifier[i]) {
252 DRM_DEBUG_KMS("non-zero modifier for unused plane %d\n", i);
256 /* Pre-FB_MODIFIERS userspace didn't clear the structs properly. */
257 if (!(r->flags & DRM_MODE_FB_MODIFIERS))
261 DRM_DEBUG_KMS("buffer object handle for unused plane %d\n", i);
266 DRM_DEBUG_KMS("non-zero pitch for unused plane %d\n", i);
271 DRM_DEBUG_KMS("non-zero offset for unused plane %d\n", i);
279 struct drm_framebuffer *
280 drm_internal_framebuffer_create(struct drm_device *dev,
281 const struct drm_mode_fb_cmd2 *r,
282 struct drm_file *file_priv)
284 struct drm_mode_config *config = &dev->mode_config;
285 struct drm_framebuffer *fb;
288 if (r->flags & ~(DRM_MODE_FB_INTERLACED | DRM_MODE_FB_MODIFIERS)) {
289 DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r->flags);
290 return ERR_PTR(-EINVAL);
293 if ((config->min_width > r->width) || (r->width > config->max_width)) {
294 DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
295 r->width, config->min_width, config->max_width);
296 return ERR_PTR(-EINVAL);
298 if ((config->min_height > r->height) || (r->height > config->max_height)) {
299 DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
300 r->height, config->min_height, config->max_height);
301 return ERR_PTR(-EINVAL);
304 if (r->flags & DRM_MODE_FB_MODIFIERS &&
305 !dev->mode_config.allow_fb_modifiers) {
306 DRM_DEBUG_KMS("driver does not support fb modifiers\n");
307 return ERR_PTR(-EINVAL);
310 ret = framebuffer_check(r);
314 fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
316 DRM_DEBUG_KMS("could not create framebuffer\n");
324 * drm_mode_addfb2 - add an FB to the graphics configuration
325 * @dev: drm device for the ioctl
326 * @data: data pointer for the ioctl
327 * @file_priv: drm file for the ioctl call
329 * Add a new FB to the specified CRTC, given a user request with format. This is
330 * the 2nd version of the addfb ioctl, which supports multi-planar framebuffers
331 * and uses fourcc codes as pixel format specifiers.
333 * Called by the user via ioctl.
336 * Zero on success, negative errno on failure.
338 int drm_mode_addfb2(struct drm_device *dev,
339 void *data, struct drm_file *file_priv)
341 struct drm_mode_fb_cmd2 *r = data;
342 struct drm_framebuffer *fb;
344 if (!drm_core_check_feature(dev, DRIVER_MODESET))
347 fb = drm_internal_framebuffer_create(dev, r, file_priv);
351 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
352 r->fb_id = fb->base.id;
354 /* Transfer ownership to the filp for reaping on close */
355 mutex_lock(&file_priv->fbs_lock);
356 list_add(&fb->filp_head, &file_priv->fbs);
357 mutex_unlock(&file_priv->fbs_lock);
362 struct drm_mode_rmfb_work {
363 struct work_struct work;
364 struct list_head fbs;
367 static void drm_mode_rmfb_work_fn(struct work_struct *w)
369 struct drm_mode_rmfb_work *arg = container_of(w, typeof(*arg), work);
371 while (!list_empty(&arg->fbs)) {
372 struct drm_framebuffer *fb =
373 list_first_entry(&arg->fbs, typeof(*fb), filp_head);
375 list_del_init(&fb->filp_head);
376 drm_framebuffer_remove(fb);
381 * drm_mode_rmfb - remove an FB from the configuration
382 * @dev: drm device for the ioctl
383 * @data: data pointer for the ioctl
384 * @file_priv: drm file for the ioctl call
386 * Remove the FB specified by the user.
388 * Called by the user via ioctl.
391 * Zero on success, negative errno on failure.
393 int drm_mode_rmfb(struct drm_device *dev,
394 void *data, struct drm_file *file_priv)
396 struct drm_framebuffer *fb = NULL;
397 struct drm_framebuffer *fbl = NULL;
401 if (!drm_core_check_feature(dev, DRIVER_MODESET))
404 fb = drm_framebuffer_lookup(dev, *id);
408 mutex_lock(&file_priv->fbs_lock);
409 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
413 mutex_unlock(&file_priv->fbs_lock);
417 list_del_init(&fb->filp_head);
418 mutex_unlock(&file_priv->fbs_lock);
420 /* drop the reference we picked up in framebuffer lookup */
421 drm_framebuffer_unreference(fb);
424 * we now own the reference that was stored in the fbs list
426 * drm_framebuffer_remove may fail with -EINTR on pending signals,
427 * so run this in a separate stack as there's no way to correctly
428 * handle this after the fb is already removed from the lookup table.
430 if (drm_framebuffer_read_refcount(fb) > 1) {
431 struct drm_mode_rmfb_work arg;
433 INIT_WORK_ONSTACK(&arg.work, drm_mode_rmfb_work_fn);
434 INIT_LIST_HEAD(&arg.fbs);
435 list_add_tail(&fb->filp_head, &arg.fbs);
437 schedule_work(&arg.work);
438 flush_work(&arg.work);
439 destroy_work_on_stack(&arg.work);
441 drm_framebuffer_unreference(fb);
446 drm_framebuffer_unreference(fb);
451 * drm_mode_getfb - get FB info
452 * @dev: drm device for the ioctl
453 * @data: data pointer for the ioctl
454 * @file_priv: drm file for the ioctl call
456 * Lookup the FB given its ID and return info about it.
458 * Called by the user via ioctl.
461 * Zero on success, negative errno on failure.
463 int drm_mode_getfb(struct drm_device *dev,
464 void *data, struct drm_file *file_priv)
466 struct drm_mode_fb_cmd *r = data;
467 struct drm_framebuffer *fb;
470 if (!drm_core_check_feature(dev, DRIVER_MODESET))
473 fb = drm_framebuffer_lookup(dev, r->fb_id);
477 r->height = fb->height;
478 r->width = fb->width;
479 r->depth = fb->depth;
480 r->bpp = fb->bits_per_pixel;
481 r->pitch = fb->pitches[0];
482 if (fb->funcs->create_handle) {
483 if (drm_is_current_master(file_priv) || capable(CAP_SYS_ADMIN) ||
484 drm_is_control_client(file_priv)) {
485 ret = fb->funcs->create_handle(fb, file_priv,
488 /* GET_FB() is an unprivileged ioctl so we must not
489 * return a buffer-handle to non-master processes! For
490 * backwards-compatibility reasons, we cannot make
491 * GET_FB() privileged, so just return an invalid handle
492 * for non-masters. */
500 drm_framebuffer_unreference(fb);
506 * drm_mode_dirtyfb_ioctl - flush frontbuffer rendering on an FB
507 * @dev: drm device for the ioctl
508 * @data: data pointer for the ioctl
509 * @file_priv: drm file for the ioctl call
511 * Lookup the FB and flush out the damaged area supplied by userspace as a clip
512 * rectangle list. Generic userspace which does frontbuffer rendering must call
513 * this ioctl to flush out the changes on manual-update display outputs, e.g.
514 * usb display-link, mipi manual update panels or edp panel self refresh modes.
516 * Modesetting drivers which always update the frontbuffer do not need to
517 * implement the corresponding ->dirty framebuffer callback.
519 * Called by the user via ioctl.
522 * Zero on success, negative errno on failure.
524 int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
525 void *data, struct drm_file *file_priv)
527 struct drm_clip_rect __user *clips_ptr;
528 struct drm_clip_rect *clips = NULL;
529 struct drm_mode_fb_dirty_cmd *r = data;
530 struct drm_framebuffer *fb;
535 if (!drm_core_check_feature(dev, DRIVER_MODESET))
538 fb = drm_framebuffer_lookup(dev, r->fb_id);
542 num_clips = r->num_clips;
543 clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
545 if (!num_clips != !clips_ptr) {
550 flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
552 /* If userspace annotates copy, clips must come in pairs */
553 if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
558 if (num_clips && clips_ptr) {
559 if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
563 clips = kcalloc(num_clips, sizeof(*clips), GFP_KERNEL);
569 ret = copy_from_user(clips, clips_ptr,
570 num_clips * sizeof(*clips));
577 if (fb->funcs->dirty) {
578 ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
587 drm_framebuffer_unreference(fb);
593 * drm_fb_release - remove and free the FBs on this file
594 * @priv: drm file for the ioctl
596 * Destroy all the FBs associated with @filp.
598 * Called by the user via ioctl.
601 * Zero on success, negative errno on failure.
603 void drm_fb_release(struct drm_file *priv)
605 struct drm_framebuffer *fb, *tfb;
606 struct drm_mode_rmfb_work arg;
608 INIT_LIST_HEAD(&arg.fbs);
611 * When the file gets released that means no one else can access the fb
612 * list any more, so no need to grab fpriv->fbs_lock. And we need to
613 * avoid upsetting lockdep since the universal cursor code adds a
614 * framebuffer while holding mutex locks.
616 * Note that a real deadlock between fpriv->fbs_lock and the modeset
617 * locks is impossible here since no one else but this function can get
620 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
621 if (drm_framebuffer_read_refcount(fb) > 1) {
622 list_move_tail(&fb->filp_head, &arg.fbs);
624 list_del_init(&fb->filp_head);
626 /* This drops the fpriv->fbs reference. */
627 drm_framebuffer_unreference(fb);
631 if (!list_empty(&arg.fbs)) {
632 INIT_WORK_ONSTACK(&arg.work, drm_mode_rmfb_work_fn);
634 schedule_work(&arg.work);
635 flush_work(&arg.work);
636 destroy_work_on_stack(&arg.work);
640 void drm_framebuffer_free(struct kref *kref)
642 struct drm_framebuffer *fb =
643 container_of(kref, struct drm_framebuffer, base.refcount);
644 struct drm_device *dev = fb->dev;
647 * The lookup idr holds a weak reference, which has not necessarily been
648 * removed at this point. Check for that.
650 drm_mode_object_unregister(dev, &fb->base);
652 fb->funcs->destroy(fb);
656 * drm_framebuffer_init - initialize a framebuffer
658 * @fb: framebuffer to be initialized
659 * @funcs: ... with these functions
661 * Allocates an ID for the framebuffer's parent mode object, sets its mode
662 * functions & device file and adds it to the master fd list.
665 * This functions publishes the fb and makes it available for concurrent access
666 * by other users. Which means by this point the fb _must_ be fully set up -
667 * since all the fb attributes are invariant over its lifetime, no further
668 * locking but only correct reference counting is required.
671 * Zero on success, error code on failure.
673 int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb,
674 const struct drm_framebuffer_funcs *funcs)
678 INIT_LIST_HEAD(&fb->filp_head);
682 ret = drm_mode_object_get_reg(dev, &fb->base, DRM_MODE_OBJECT_FB,
683 false, drm_framebuffer_free);
687 mutex_lock(&dev->mode_config.fb_lock);
688 dev->mode_config.num_fb++;
689 list_add(&fb->head, &dev->mode_config.fb_list);
690 mutex_unlock(&dev->mode_config.fb_lock);
692 drm_mode_object_register(dev, &fb->base);
696 EXPORT_SYMBOL(drm_framebuffer_init);
699 * drm_framebuffer_lookup - look up a drm framebuffer and grab a reference
701 * @id: id of the fb object
703 * If successful, this grabs an additional reference to the framebuffer -
704 * callers need to make sure to eventually unreference the returned framebuffer
705 * again, using @drm_framebuffer_unreference.
707 struct drm_framebuffer *drm_framebuffer_lookup(struct drm_device *dev,
710 struct drm_mode_object *obj;
711 struct drm_framebuffer *fb = NULL;
713 obj = __drm_mode_object_find(dev, id, DRM_MODE_OBJECT_FB);
718 EXPORT_SYMBOL(drm_framebuffer_lookup);
721 * drm_framebuffer_unregister_private - unregister a private fb from the lookup idr
722 * @fb: fb to unregister
724 * Drivers need to call this when cleaning up driver-private framebuffers, e.g.
725 * those used for fbdev. Note that the caller must hold a reference of it's own,
726 * i.e. the object may not be destroyed through this call (since it'll lead to a
727 * locking inversion).
729 void drm_framebuffer_unregister_private(struct drm_framebuffer *fb)
731 struct drm_device *dev;
738 /* Mark fb as reaped and drop idr ref. */
739 drm_mode_object_unregister(dev, &fb->base);
741 EXPORT_SYMBOL(drm_framebuffer_unregister_private);
744 * drm_framebuffer_cleanup - remove a framebuffer object
745 * @fb: framebuffer to remove
747 * Cleanup framebuffer. This function is intended to be used from the drivers
748 * ->destroy callback. It can also be used to clean up driver private
749 * framebuffers embedded into a larger structure.
751 * Note that this function does not remove the fb from active usuage - if it is
752 * still used anywhere, hilarity can ensue since userspace could call getfb on
753 * the id and get back -EINVAL. Obviously no concern at driver unload time.
755 * Also, the framebuffer will not be removed from the lookup idr - for
756 * user-created framebuffers this will happen in in the rmfb ioctl. For
757 * driver-private objects (e.g. for fbdev) drivers need to explicitly call
758 * drm_framebuffer_unregister_private.
760 void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
762 struct drm_device *dev = fb->dev;
764 mutex_lock(&dev->mode_config.fb_lock);
766 dev->mode_config.num_fb--;
767 mutex_unlock(&dev->mode_config.fb_lock);
769 EXPORT_SYMBOL(drm_framebuffer_cleanup);
772 * drm_framebuffer_remove - remove and unreference a framebuffer object
773 * @fb: framebuffer to remove
775 * Scans all the CRTCs and planes in @dev's mode_config. If they're
776 * using @fb, removes it, setting it to NULL. Then drops the reference to the
777 * passed-in framebuffer. Might take the modeset locks.
779 * Note that this function optimizes the cleanup away if the caller holds the
780 * last reference to the framebuffer. It is also guaranteed to not take the
781 * modeset locks in this case.
783 void drm_framebuffer_remove(struct drm_framebuffer *fb)
785 struct drm_device *dev;
786 struct drm_crtc *crtc;
787 struct drm_plane *plane;
794 WARN_ON(!list_empty(&fb->filp_head));
797 * drm ABI mandates that we remove any deleted framebuffers from active
798 * useage. But since most sane clients only remove framebuffers they no
799 * longer need, try to optimize this away.
801 * Since we're holding a reference ourselves, observing a refcount of 1
802 * means that we're the last holder and can skip it. Also, the refcount
803 * can never increase from 1 again, so we don't need any barriers or
806 * Note that userspace could try to race with use and instate a new
807 * usage _after_ we've cleared all current ones. End result will be an
808 * in-use fb with fb-id == 0. Userspace is allowed to shoot its own foot
811 if (drm_framebuffer_read_refcount(fb) > 1) {
812 drm_modeset_lock_all(dev);
813 /* remove from any CRTC */
814 drm_for_each_crtc(crtc, dev) {
815 if (crtc->primary->fb == fb) {
816 /* should turn off the crtc */
817 if (drm_crtc_force_disable(crtc))
818 DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc);
822 drm_for_each_plane(plane, dev) {
824 drm_plane_force_disable(plane);
826 drm_modeset_unlock_all(dev);
829 drm_framebuffer_unreference(fb);
831 EXPORT_SYMBOL(drm_framebuffer_remove);