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>
27 #include <drm/drm_atomic.h>
29 #include "drm_crtc_internal.h"
34 * Frame buffers are abstract memory objects that provide a source of pixels to
35 * scanout to a CRTC. Applications explicitly request the creation of frame
36 * buffers through the DRM_IOCTL_MODE_ADDFB(2) ioctls and receive an opaque
37 * handle that can be passed to the KMS CRTC control, plane configuration and
38 * page flip functions.
40 * Frame buffers rely on the underlying memory manager for allocating backing
41 * storage. When creating a frame buffer applications pass a memory handle
42 * (or a list of memory handles for multi-planar formats) through the
43 * &struct drm_mode_fb_cmd2 argument. For drivers using GEM as their userspace
44 * buffer management interface this would be a GEM handle. Drivers are however
45 * free to use their own backing storage object handles, e.g. vmwgfx directly
46 * exposes special TTM handles to userspace and so expects TTM handles in the
47 * create ioctl and not GEM handles.
49 * Framebuffers are tracked with &struct drm_framebuffer. They are published
50 * using drm_framebuffer_init() - after calling that function userspace can use
51 * and access the framebuffer object. The helper function
52 * drm_helper_mode_fill_fb_struct() can be used to pre-fill the required
55 * The lifetime of a drm framebuffer is controlled with a reference count,
56 * drivers can grab additional references with drm_framebuffer_get() and drop
57 * them again with drm_framebuffer_put(). For driver-private framebuffers for
58 * which the last reference is never dropped (e.g. for the fbdev framebuffer
59 * when the struct &struct drm_framebuffer is embedded into the fbdev helper
60 * struct) drivers can manually clean up a framebuffer at module unload time
61 * with drm_framebuffer_unregister_private(). But doing this is not
62 * recommended, and it's better to have a normal free-standing &struct
66 int drm_framebuffer_check_src_coords(uint32_t src_x
, uint32_t src_y
,
67 uint32_t src_w
, uint32_t src_h
,
68 const struct drm_framebuffer
*fb
)
70 unsigned int fb_width
, fb_height
;
72 fb_width
= fb
->width
<< 16;
73 fb_height
= fb
->height
<< 16;
75 /* Make sure source coordinates are inside the fb. */
76 if (src_w
> fb_width
||
77 src_x
> fb_width
- src_w
||
79 src_y
> fb_height
- src_h
) {
80 DRM_DEBUG_KMS("Invalid source coordinates "
81 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
82 src_w
>> 16, ((src_w
& 0xffff) * 15625) >> 10,
83 src_h
>> 16, ((src_h
& 0xffff) * 15625) >> 10,
84 src_x
>> 16, ((src_x
& 0xffff) * 15625) >> 10,
85 src_y
>> 16, ((src_y
& 0xffff) * 15625) >> 10);
93 * drm_mode_addfb - add an FB to the graphics configuration
94 * @dev: drm device for the ioctl
95 * @data: data pointer for the ioctl
96 * @file_priv: drm file for the ioctl call
98 * Add a new FB to the specified CRTC, given a user request. This is the
99 * original addfb ioctl which only supported RGB formats.
101 * Called by the user via ioctl.
104 * Zero on success, negative errno on failure.
106 int drm_mode_addfb(struct drm_device
*dev
,
107 void *data
, struct drm_file
*file_priv
)
109 struct drm_mode_fb_cmd
*or = data
;
110 struct drm_mode_fb_cmd2 r
= {};
113 /* convert to new format and call new ioctl */
116 r
.height
= or->height
;
117 r
.pitches
[0] = or->pitch
;
118 r
.pixel_format
= drm_mode_legacy_fb_format(or->bpp
, or->depth
);
119 r
.handles
[0] = or->handle
;
121 ret
= drm_mode_addfb2(dev
, &r
, file_priv
);
130 static int fb_plane_width(int width
,
131 const struct drm_format_info
*format
, int plane
)
136 return DIV_ROUND_UP(width
, format
->hsub
);
139 static int fb_plane_height(int height
,
140 const struct drm_format_info
*format
, int plane
)
145 return DIV_ROUND_UP(height
, format
->vsub
);
148 static int framebuffer_check(struct drm_device
*dev
,
149 const struct drm_mode_fb_cmd2
*r
)
151 const struct drm_format_info
*info
;
154 /* check if the format is supported at all */
155 info
= __drm_format_info(r
->pixel_format
& ~DRM_FORMAT_BIG_ENDIAN
);
157 struct drm_format_name_buf format_name
;
158 DRM_DEBUG_KMS("bad framebuffer format %s\n",
159 drm_get_format_name(r
->pixel_format
,
164 /* now let the driver pick its own format info */
165 info
= drm_get_format_info(dev
, r
);
168 DRM_DEBUG_KMS("bad framebuffer width %u\n", r
->width
);
172 if (r
->height
== 0) {
173 DRM_DEBUG_KMS("bad framebuffer height %u\n", r
->height
);
177 for (i
= 0; i
< info
->num_planes
; i
++) {
178 unsigned int width
= fb_plane_width(r
->width
, info
, i
);
179 unsigned int height
= fb_plane_height(r
->height
, info
, i
);
180 unsigned int cpp
= info
->cpp
[i
];
182 if (!r
->handles
[i
]) {
183 DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i
);
187 if ((uint64_t) width
* cpp
> UINT_MAX
)
190 if ((uint64_t) height
* r
->pitches
[i
] + r
->offsets
[i
] > UINT_MAX
)
193 if (r
->pitches
[i
] < width
* cpp
) {
194 DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r
->pitches
[i
], i
);
198 if (r
->modifier
[i
] && !(r
->flags
& DRM_MODE_FB_MODIFIERS
)) {
199 DRM_DEBUG_KMS("bad fb modifier %llu for plane %d\n",
204 if (r
->flags
& DRM_MODE_FB_MODIFIERS
&&
205 r
->modifier
[i
] != r
->modifier
[0]) {
206 DRM_DEBUG_KMS("bad fb modifier %llu for plane %d\n",
211 /* modifier specific checks: */
212 switch (r
->modifier
[i
]) {
213 case DRM_FORMAT_MOD_SAMSUNG_64_32_TILE
:
214 /* NOTE: the pitch restriction may be lifted later if it turns
215 * out that no hw has this restriction:
217 if (r
->pixel_format
!= DRM_FORMAT_NV12
||
218 width
% 128 || height
% 32 ||
219 r
->pitches
[i
] % 128) {
220 DRM_DEBUG_KMS("bad modifier data for plane %d\n", i
);
230 for (i
= info
->num_planes
; i
< 4; i
++) {
231 if (r
->modifier
[i
]) {
232 DRM_DEBUG_KMS("non-zero modifier for unused plane %d\n", i
);
236 /* Pre-FB_MODIFIERS userspace didn't clear the structs properly. */
237 if (!(r
->flags
& DRM_MODE_FB_MODIFIERS
))
241 DRM_DEBUG_KMS("buffer object handle for unused plane %d\n", i
);
246 DRM_DEBUG_KMS("non-zero pitch for unused plane %d\n", i
);
251 DRM_DEBUG_KMS("non-zero offset for unused plane %d\n", i
);
259 struct drm_framebuffer
*
260 drm_internal_framebuffer_create(struct drm_device
*dev
,
261 const struct drm_mode_fb_cmd2
*r
,
262 struct drm_file
*file_priv
)
264 struct drm_mode_config
*config
= &dev
->mode_config
;
265 struct drm_framebuffer
*fb
;
268 if (r
->flags
& ~(DRM_MODE_FB_INTERLACED
| DRM_MODE_FB_MODIFIERS
)) {
269 DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r
->flags
);
270 return ERR_PTR(-EINVAL
);
273 if ((config
->min_width
> r
->width
) || (r
->width
> config
->max_width
)) {
274 DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
275 r
->width
, config
->min_width
, config
->max_width
);
276 return ERR_PTR(-EINVAL
);
278 if ((config
->min_height
> r
->height
) || (r
->height
> config
->max_height
)) {
279 DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
280 r
->height
, config
->min_height
, config
->max_height
);
281 return ERR_PTR(-EINVAL
);
284 if (r
->flags
& DRM_MODE_FB_MODIFIERS
&&
285 !dev
->mode_config
.allow_fb_modifiers
) {
286 DRM_DEBUG_KMS("driver does not support fb modifiers\n");
287 return ERR_PTR(-EINVAL
);
290 ret
= framebuffer_check(dev
, r
);
294 fb
= dev
->mode_config
.funcs
->fb_create(dev
, file_priv
, r
);
296 DRM_DEBUG_KMS("could not create framebuffer\n");
304 * drm_mode_addfb2 - add an FB to the graphics configuration
305 * @dev: drm device for the ioctl
306 * @data: data pointer for the ioctl
307 * @file_priv: drm file for the ioctl call
309 * Add a new FB to the specified CRTC, given a user request with format. This is
310 * the 2nd version of the addfb ioctl, which supports multi-planar framebuffers
311 * and uses fourcc codes as pixel format specifiers.
313 * Called by the user via ioctl.
316 * Zero on success, negative errno on failure.
318 int drm_mode_addfb2(struct drm_device
*dev
,
319 void *data
, struct drm_file
*file_priv
)
321 struct drm_mode_fb_cmd2
*r
= data
;
322 struct drm_framebuffer
*fb
;
324 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
327 fb
= drm_internal_framebuffer_create(dev
, r
, file_priv
);
331 DRM_DEBUG_KMS("[FB:%d]\n", fb
->base
.id
);
332 r
->fb_id
= fb
->base
.id
;
334 /* Transfer ownership to the filp for reaping on close */
335 mutex_lock(&file_priv
->fbs_lock
);
336 list_add(&fb
->filp_head
, &file_priv
->fbs
);
337 mutex_unlock(&file_priv
->fbs_lock
);
342 struct drm_mode_rmfb_work
{
343 struct work_struct work
;
344 struct list_head fbs
;
347 static void drm_mode_rmfb_work_fn(struct work_struct
*w
)
349 struct drm_mode_rmfb_work
*arg
= container_of(w
, typeof(*arg
), work
);
351 while (!list_empty(&arg
->fbs
)) {
352 struct drm_framebuffer
*fb
=
353 list_first_entry(&arg
->fbs
, typeof(*fb
), filp_head
);
355 list_del_init(&fb
->filp_head
);
356 drm_framebuffer_remove(fb
);
361 * drm_mode_rmfb - remove an FB from the configuration
362 * @dev: drm device for the ioctl
363 * @data: data pointer for the ioctl
364 * @file_priv: drm file for the ioctl call
366 * Remove the FB specified by the user.
368 * Called by the user via ioctl.
371 * Zero on success, negative errno on failure.
373 int drm_mode_rmfb(struct drm_device
*dev
,
374 void *data
, struct drm_file
*file_priv
)
376 struct drm_framebuffer
*fb
= NULL
;
377 struct drm_framebuffer
*fbl
= NULL
;
381 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
384 fb
= drm_framebuffer_lookup(dev
, *id
);
388 mutex_lock(&file_priv
->fbs_lock
);
389 list_for_each_entry(fbl
, &file_priv
->fbs
, filp_head
)
393 mutex_unlock(&file_priv
->fbs_lock
);
397 list_del_init(&fb
->filp_head
);
398 mutex_unlock(&file_priv
->fbs_lock
);
400 /* drop the reference we picked up in framebuffer lookup */
401 drm_framebuffer_put(fb
);
404 * we now own the reference that was stored in the fbs list
406 * drm_framebuffer_remove may fail with -EINTR on pending signals,
407 * so run this in a separate stack as there's no way to correctly
408 * handle this after the fb is already removed from the lookup table.
410 if (drm_framebuffer_read_refcount(fb
) > 1) {
411 struct drm_mode_rmfb_work arg
;
413 INIT_WORK_ONSTACK(&arg
.work
, drm_mode_rmfb_work_fn
);
414 INIT_LIST_HEAD(&arg
.fbs
);
415 list_add_tail(&fb
->filp_head
, &arg
.fbs
);
417 schedule_work(&arg
.work
);
418 flush_work(&arg
.work
);
419 destroy_work_on_stack(&arg
.work
);
421 drm_framebuffer_put(fb
);
426 drm_framebuffer_put(fb
);
431 * drm_mode_getfb - get FB info
432 * @dev: drm device for the ioctl
433 * @data: data pointer for the ioctl
434 * @file_priv: drm file for the ioctl call
436 * Lookup the FB given its ID and return info about it.
438 * Called by the user via ioctl.
441 * Zero on success, negative errno on failure.
443 int drm_mode_getfb(struct drm_device
*dev
,
444 void *data
, struct drm_file
*file_priv
)
446 struct drm_mode_fb_cmd
*r
= data
;
447 struct drm_framebuffer
*fb
;
450 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
453 fb
= drm_framebuffer_lookup(dev
, r
->fb_id
);
457 r
->height
= fb
->height
;
458 r
->width
= fb
->width
;
459 r
->depth
= fb
->format
->depth
;
460 r
->bpp
= fb
->format
->cpp
[0] * 8;
461 r
->pitch
= fb
->pitches
[0];
462 if (fb
->funcs
->create_handle
) {
463 if (drm_is_current_master(file_priv
) || capable(CAP_SYS_ADMIN
) ||
464 drm_is_control_client(file_priv
)) {
465 ret
= fb
->funcs
->create_handle(fb
, file_priv
,
468 /* GET_FB() is an unprivileged ioctl so we must not
469 * return a buffer-handle to non-master processes! For
470 * backwards-compatibility reasons, we cannot make
471 * GET_FB() privileged, so just return an invalid handle
472 * for non-masters. */
480 drm_framebuffer_put(fb
);
486 * drm_mode_dirtyfb_ioctl - flush frontbuffer rendering on an FB
487 * @dev: drm device for the ioctl
488 * @data: data pointer for the ioctl
489 * @file_priv: drm file for the ioctl call
491 * Lookup the FB and flush out the damaged area supplied by userspace as a clip
492 * rectangle list. Generic userspace which does frontbuffer rendering must call
493 * this ioctl to flush out the changes on manual-update display outputs, e.g.
494 * usb display-link, mipi manual update panels or edp panel self refresh modes.
496 * Modesetting drivers which always update the frontbuffer do not need to
497 * implement the corresponding &drm_framebuffer_funcs.dirty callback.
499 * Called by the user via ioctl.
502 * Zero on success, negative errno on failure.
504 int drm_mode_dirtyfb_ioctl(struct drm_device
*dev
,
505 void *data
, struct drm_file
*file_priv
)
507 struct drm_clip_rect __user
*clips_ptr
;
508 struct drm_clip_rect
*clips
= NULL
;
509 struct drm_mode_fb_dirty_cmd
*r
= data
;
510 struct drm_framebuffer
*fb
;
515 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
518 fb
= drm_framebuffer_lookup(dev
, r
->fb_id
);
522 num_clips
= r
->num_clips
;
523 clips_ptr
= (struct drm_clip_rect __user
*)(unsigned long)r
->clips_ptr
;
525 if (!num_clips
!= !clips_ptr
) {
530 flags
= DRM_MODE_FB_DIRTY_FLAGS
& r
->flags
;
532 /* If userspace annotates copy, clips must come in pairs */
533 if (flags
& DRM_MODE_FB_DIRTY_ANNOTATE_COPY
&& (num_clips
% 2)) {
538 if (num_clips
&& clips_ptr
) {
539 if (num_clips
< 0 || num_clips
> DRM_MODE_FB_DIRTY_MAX_CLIPS
) {
543 clips
= kcalloc(num_clips
, sizeof(*clips
), GFP_KERNEL
);
549 ret
= copy_from_user(clips
, clips_ptr
,
550 num_clips
* sizeof(*clips
));
557 if (fb
->funcs
->dirty
) {
558 ret
= fb
->funcs
->dirty(fb
, file_priv
, flags
, r
->color
,
567 drm_framebuffer_put(fb
);
573 * drm_fb_release - remove and free the FBs on this file
574 * @priv: drm file for the ioctl
576 * Destroy all the FBs associated with @filp.
578 * Called by the user via ioctl.
581 * Zero on success, negative errno on failure.
583 void drm_fb_release(struct drm_file
*priv
)
585 struct drm_framebuffer
*fb
, *tfb
;
586 struct drm_mode_rmfb_work arg
;
588 INIT_LIST_HEAD(&arg
.fbs
);
591 * When the file gets released that means no one else can access the fb
592 * list any more, so no need to grab fpriv->fbs_lock. And we need to
593 * avoid upsetting lockdep since the universal cursor code adds a
594 * framebuffer while holding mutex locks.
596 * Note that a real deadlock between fpriv->fbs_lock and the modeset
597 * locks is impossible here since no one else but this function can get
600 list_for_each_entry_safe(fb
, tfb
, &priv
->fbs
, filp_head
) {
601 if (drm_framebuffer_read_refcount(fb
) > 1) {
602 list_move_tail(&fb
->filp_head
, &arg
.fbs
);
604 list_del_init(&fb
->filp_head
);
606 /* This drops the fpriv->fbs reference. */
607 drm_framebuffer_put(fb
);
611 if (!list_empty(&arg
.fbs
)) {
612 INIT_WORK_ONSTACK(&arg
.work
, drm_mode_rmfb_work_fn
);
614 schedule_work(&arg
.work
);
615 flush_work(&arg
.work
);
616 destroy_work_on_stack(&arg
.work
);
620 void drm_framebuffer_free(struct kref
*kref
)
622 struct drm_framebuffer
*fb
=
623 container_of(kref
, struct drm_framebuffer
, base
.refcount
);
624 struct drm_device
*dev
= fb
->dev
;
627 * The lookup idr holds a weak reference, which has not necessarily been
628 * removed at this point. Check for that.
630 drm_mode_object_unregister(dev
, &fb
->base
);
632 fb
->funcs
->destroy(fb
);
636 * drm_framebuffer_init - initialize a framebuffer
638 * @fb: framebuffer to be initialized
639 * @funcs: ... with these functions
641 * Allocates an ID for the framebuffer's parent mode object, sets its mode
642 * functions & device file and adds it to the master fd list.
645 * This functions publishes the fb and makes it available for concurrent access
646 * by other users. Which means by this point the fb _must_ be fully set up -
647 * since all the fb attributes are invariant over its lifetime, no further
648 * locking but only correct reference counting is required.
651 * Zero on success, error code on failure.
653 int drm_framebuffer_init(struct drm_device
*dev
, struct drm_framebuffer
*fb
,
654 const struct drm_framebuffer_funcs
*funcs
)
658 if (WARN_ON_ONCE(fb
->dev
!= dev
|| !fb
->format
))
661 INIT_LIST_HEAD(&fb
->filp_head
);
665 ret
= __drm_mode_object_add(dev
, &fb
->base
, DRM_MODE_OBJECT_FB
,
666 false, drm_framebuffer_free
);
670 mutex_lock(&dev
->mode_config
.fb_lock
);
671 dev
->mode_config
.num_fb
++;
672 list_add(&fb
->head
, &dev
->mode_config
.fb_list
);
673 mutex_unlock(&dev
->mode_config
.fb_lock
);
675 drm_mode_object_register(dev
, &fb
->base
);
679 EXPORT_SYMBOL(drm_framebuffer_init
);
682 * drm_framebuffer_lookup - look up a drm framebuffer and grab a reference
684 * @id: id of the fb object
686 * If successful, this grabs an additional reference to the framebuffer -
687 * callers need to make sure to eventually unreference the returned framebuffer
688 * again, using drm_framebuffer_put().
690 struct drm_framebuffer
*drm_framebuffer_lookup(struct drm_device
*dev
,
693 struct drm_mode_object
*obj
;
694 struct drm_framebuffer
*fb
= NULL
;
696 obj
= __drm_mode_object_find(dev
, id
, DRM_MODE_OBJECT_FB
);
701 EXPORT_SYMBOL(drm_framebuffer_lookup
);
704 * drm_framebuffer_unregister_private - unregister a private fb from the lookup idr
705 * @fb: fb to unregister
707 * Drivers need to call this when cleaning up driver-private framebuffers, e.g.
708 * those used for fbdev. Note that the caller must hold a reference of it's own,
709 * i.e. the object may not be destroyed through this call (since it'll lead to a
710 * locking inversion).
712 * NOTE: This function is deprecated. For driver-private framebuffers it is not
713 * recommended to embed a framebuffer struct info fbdev struct, instead, a
714 * framebuffer pointer is preferred and drm_framebuffer_put() should be called
715 * when the framebuffer is to be cleaned up.
717 void drm_framebuffer_unregister_private(struct drm_framebuffer
*fb
)
719 struct drm_device
*dev
;
726 /* Mark fb as reaped and drop idr ref. */
727 drm_mode_object_unregister(dev
, &fb
->base
);
729 EXPORT_SYMBOL(drm_framebuffer_unregister_private
);
732 * drm_framebuffer_cleanup - remove a framebuffer object
733 * @fb: framebuffer to remove
735 * Cleanup framebuffer. This function is intended to be used from the drivers
736 * &drm_framebuffer_funcs.destroy callback. It can also be used to clean up
737 * driver private framebuffers embedded into a larger structure.
739 * Note that this function does not remove the fb from active usage - if it is
740 * still used anywhere, hilarity can ensue since userspace could call getfb on
741 * the id and get back -EINVAL. Obviously no concern at driver unload time.
743 * Also, the framebuffer will not be removed from the lookup idr - for
744 * user-created framebuffers this will happen in in the rmfb ioctl. For
745 * driver-private objects (e.g. for fbdev) drivers need to explicitly call
746 * drm_framebuffer_unregister_private.
748 void drm_framebuffer_cleanup(struct drm_framebuffer
*fb
)
750 struct drm_device
*dev
= fb
->dev
;
752 mutex_lock(&dev
->mode_config
.fb_lock
);
754 dev
->mode_config
.num_fb
--;
755 mutex_unlock(&dev
->mode_config
.fb_lock
);
757 EXPORT_SYMBOL(drm_framebuffer_cleanup
);
759 static int atomic_remove_fb(struct drm_framebuffer
*fb
)
761 struct drm_modeset_acquire_ctx ctx
;
762 struct drm_device
*dev
= fb
->dev
;
763 struct drm_atomic_state
*state
;
764 struct drm_plane
*plane
;
765 struct drm_connector
*conn
;
766 struct drm_connector_state
*conn_state
;
770 state
= drm_atomic_state_alloc(dev
);
774 drm_modeset_acquire_init(&ctx
, 0);
775 state
->acquire_ctx
= &ctx
;
779 ret
= drm_modeset_lock_all_ctx(dev
, &ctx
);
783 drm_for_each_plane(plane
, dev
) {
784 struct drm_plane_state
*plane_state
;
786 if (plane
->state
->fb
!= fb
)
789 plane_state
= drm_atomic_get_plane_state(state
, plane
);
790 if (IS_ERR(plane_state
)) {
791 ret
= PTR_ERR(plane_state
);
795 if (plane_state
->crtc
->primary
== plane
) {
796 struct drm_crtc_state
*crtc_state
;
798 crtc_state
= drm_atomic_get_existing_crtc_state(state
, plane_state
->crtc
);
800 ret
= drm_atomic_add_affected_connectors(state
, plane_state
->crtc
);
804 crtc_state
->active
= false;
805 ret
= drm_atomic_set_mode_for_crtc(crtc_state
, NULL
);
810 drm_atomic_set_fb_for_plane(plane_state
, NULL
);
811 ret
= drm_atomic_set_crtc_for_plane(plane_state
, NULL
);
815 plane_mask
|= BIT(drm_plane_index(plane
));
817 plane
->old_fb
= plane
->fb
;
820 for_each_connector_in_state(state
, conn
, conn_state
, i
) {
821 ret
= drm_atomic_set_crtc_for_connector(conn_state
, NULL
);
828 ret
= drm_atomic_commit(state
);
832 drm_atomic_clean_old_fb(dev
, plane_mask
, ret
);
834 if (ret
== -EDEADLK
) {
835 drm_atomic_state_clear(state
);
836 drm_modeset_backoff(&ctx
);
840 drm_atomic_state_put(state
);
842 drm_modeset_drop_locks(&ctx
);
843 drm_modeset_acquire_fini(&ctx
);
848 static void legacy_remove_fb(struct drm_framebuffer
*fb
)
850 struct drm_device
*dev
= fb
->dev
;
851 struct drm_crtc
*crtc
;
852 struct drm_plane
*plane
;
854 drm_modeset_lock_all(dev
);
855 /* remove from any CRTC */
856 drm_for_each_crtc(crtc
, dev
) {
857 if (crtc
->primary
->fb
== fb
) {
858 /* should turn off the crtc */
859 if (drm_crtc_force_disable(crtc
))
860 DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc
);
864 drm_for_each_plane(plane
, dev
) {
866 drm_plane_force_disable(plane
);
868 drm_modeset_unlock_all(dev
);
872 * drm_framebuffer_remove - remove and unreference a framebuffer object
873 * @fb: framebuffer to remove
875 * Scans all the CRTCs and planes in @dev's mode_config. If they're
876 * using @fb, removes it, setting it to NULL. Then drops the reference to the
877 * passed-in framebuffer. Might take the modeset locks.
879 * Note that this function optimizes the cleanup away if the caller holds the
880 * last reference to the framebuffer. It is also guaranteed to not take the
881 * modeset locks in this case.
883 void drm_framebuffer_remove(struct drm_framebuffer
*fb
)
885 struct drm_device
*dev
;
892 WARN_ON(!list_empty(&fb
->filp_head
));
895 * drm ABI mandates that we remove any deleted framebuffers from active
896 * useage. But since most sane clients only remove framebuffers they no
897 * longer need, try to optimize this away.
899 * Since we're holding a reference ourselves, observing a refcount of 1
900 * means that we're the last holder and can skip it. Also, the refcount
901 * can never increase from 1 again, so we don't need any barriers or
904 * Note that userspace could try to race with use and instate a new
905 * usage _after_ we've cleared all current ones. End result will be an
906 * in-use fb with fb-id == 0. Userspace is allowed to shoot its own foot
909 if (drm_framebuffer_read_refcount(fb
) > 1) {
910 if (drm_drv_uses_atomic_modeset(dev
)) {
911 int ret
= atomic_remove_fb(fb
);
912 WARN(ret
, "atomic remove_fb failed with %i\n", ret
);
914 legacy_remove_fb(fb
);
917 drm_framebuffer_put(fb
);
919 EXPORT_SYMBOL(drm_framebuffer_remove
);
922 * drm_framebuffer_plane_width - width of the plane given the first plane
923 * @width: width of the first plane
924 * @fb: the framebuffer
925 * @plane: plane index
928 * The width of @plane, given that the width of the first plane is @width.
930 int drm_framebuffer_plane_width(int width
,
931 const struct drm_framebuffer
*fb
, int plane
)
933 if (plane
>= fb
->format
->num_planes
)
936 return fb_plane_width(width
, fb
->format
, plane
);
938 EXPORT_SYMBOL(drm_framebuffer_plane_width
);
941 * drm_framebuffer_plane_height - height of the plane given the first plane
942 * @height: height of the first plane
943 * @fb: the framebuffer
944 * @plane: plane index
947 * The height of @plane, given that the height of the first plane is @height.
949 int drm_framebuffer_plane_height(int height
,
950 const struct drm_framebuffer
*fb
, int plane
)
952 if (plane
>= fb
->format
->num_planes
)
955 return fb_plane_height(height
, fb
->format
, plane
);
957 EXPORT_SYMBOL(drm_framebuffer_plane_height
);