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>
24 #include <linux/uaccess.h>
26 #include <drm/drm_atomic.h>
27 #include <drm/drm_atomic_uapi.h>
28 #include <drm/drm_auth.h>
29 #include <drm/drm_debugfs.h>
30 #include <drm/drm_drv.h>
31 #include <drm/drm_file.h>
32 #include <drm/drm_fourcc.h>
33 #include <drm/drm_framebuffer.h>
34 #include <drm/drm_gem.h>
35 #include <drm/drm_print.h>
36 #include <drm/drm_util.h>
38 #include "drm_crtc_internal.h"
39 #include "drm_internal.h"
44 * Frame buffers are abstract memory objects that provide a source of pixels to
45 * scanout to a CRTC. Applications explicitly request the creation of frame
46 * buffers through the DRM_IOCTL_MODE_ADDFB(2) ioctls and receive an opaque
47 * handle that can be passed to the KMS CRTC control, plane configuration and
48 * page flip functions.
50 * Frame buffers rely on the underlying memory manager for allocating backing
51 * storage. When creating a frame buffer applications pass a memory handle
52 * (or a list of memory handles for multi-planar formats) through the
53 * &struct drm_mode_fb_cmd2 argument. For drivers using GEM as their userspace
54 * buffer management interface this would be a GEM handle. Drivers are however
55 * free to use their own backing storage object handles, e.g. vmwgfx directly
56 * exposes special TTM handles to userspace and so expects TTM handles in the
57 * create ioctl and not GEM handles.
59 * Framebuffers are tracked with &struct drm_framebuffer. They are published
60 * using drm_framebuffer_init() - after calling that function userspace can use
61 * and access the framebuffer object. The helper function
62 * drm_helper_mode_fill_fb_struct() can be used to pre-fill the required
65 * The lifetime of a drm framebuffer is controlled with a reference count,
66 * drivers can grab additional references with drm_framebuffer_get() and drop
67 * them again with drm_framebuffer_put(). For driver-private framebuffers for
68 * which the last reference is never dropped (e.g. for the fbdev framebuffer
69 * when the struct &struct drm_framebuffer is embedded into the fbdev helper
70 * struct) drivers can manually clean up a framebuffer at module unload time
71 * with drm_framebuffer_unregister_private(). But doing this is not
72 * recommended, and it's better to have a normal free-standing &struct
76 int drm_framebuffer_check_src_coords(uint32_t src_x, uint32_t src_y,
77 uint32_t src_w, uint32_t src_h,
78 const struct drm_framebuffer *fb)
80 unsigned int fb_width, fb_height;
82 fb_width = fb->width << 16;
83 fb_height = fb->height << 16;
85 /* Make sure source coordinates are inside the fb. */
86 if (src_w > fb_width ||
87 src_x > fb_width - src_w ||
89 src_y > fb_height - src_h) {
90 drm_dbg_kms(fb->dev, "Invalid source coordinates "
91 "%u.%06ux%u.%06u+%u.%06u+%u.%06u (fb %ux%u)\n",
92 src_w >> 16, ((src_w & 0xffff) * 15625) >> 10,
93 src_h >> 16, ((src_h & 0xffff) * 15625) >> 10,
94 src_x >> 16, ((src_x & 0xffff) * 15625) >> 10,
95 src_y >> 16, ((src_y & 0xffff) * 15625) >> 10,
96 fb->width, fb->height);
104 * drm_mode_addfb - add an FB to the graphics configuration
105 * @dev: drm device for the ioctl
106 * @or: pointer to request structure
107 * @file_priv: drm file
109 * Add a new FB to the specified CRTC, given a user request. This is the
110 * original addfb ioctl which only supported RGB formats.
112 * Called by the user via ioctl, or by an in-kernel client.
115 * Zero on success, negative errno on failure.
117 int drm_mode_addfb(struct drm_device *dev, struct drm_mode_fb_cmd *or,
118 struct drm_file *file_priv)
120 struct drm_mode_fb_cmd2 r = {};
123 if (!drm_core_check_feature(dev, DRIVER_MODESET))
126 r.pixel_format = drm_driver_legacy_fb_format(dev, or->bpp, or->depth);
127 if (r.pixel_format == DRM_FORMAT_INVALID) {
128 drm_dbg_kms(dev, "bad {bpp:%d, depth:%d}\n", or->bpp, or->depth);
132 /* convert to new format and call new ioctl */
135 r.height = or->height;
136 r.pitches[0] = or->pitch;
137 r.handles[0] = or->handle;
139 ret = drm_mode_addfb2(dev, &r, file_priv);
148 int drm_mode_addfb_ioctl(struct drm_device *dev,
149 void *data, struct drm_file *file_priv)
151 return drm_mode_addfb(dev, data, file_priv);
154 static int framebuffer_check(struct drm_device *dev,
155 const struct drm_mode_fb_cmd2 *r)
157 const struct drm_format_info *info;
160 /* check if the format is supported at all */
161 if (!__drm_format_info(r->pixel_format)) {
162 drm_dbg_kms(dev, "bad framebuffer format %p4cc\n",
168 drm_dbg_kms(dev, "bad framebuffer width %u\n", r->width);
172 if (r->height == 0) {
173 drm_dbg_kms(dev, "bad framebuffer height %u\n", r->height);
177 /* now let the driver pick its own format info */
178 info = drm_get_format_info(dev, r);
180 for (i = 0; i < info->num_planes; i++) {
181 unsigned int width = drm_format_info_plane_width(info, r->width, i);
182 unsigned int height = drm_format_info_plane_height(info, r->height, i);
183 unsigned int block_size = info->char_per_block[i];
184 u64 min_pitch = drm_format_info_min_pitch(info, i, width);
186 if (!block_size && (r->modifier[i] == DRM_FORMAT_MOD_LINEAR)) {
187 drm_dbg_kms(dev, "Format requires non-linear modifier for plane %d\n", i);
191 if (!r->handles[i]) {
192 drm_dbg_kms(dev, "no buffer object handle for plane %d\n", i);
196 if (min_pitch > UINT_MAX)
199 if ((uint64_t) height * r->pitches[i] + r->offsets[i] > UINT_MAX)
202 if (block_size && r->pitches[i] < min_pitch) {
203 drm_dbg_kms(dev, "bad pitch %u for plane %d\n", r->pitches[i], i);
207 if (r->modifier[i] && !(r->flags & DRM_MODE_FB_MODIFIERS)) {
208 drm_dbg_kms(dev, "bad fb modifier %llu for plane %d\n",
213 if (r->flags & DRM_MODE_FB_MODIFIERS &&
214 r->modifier[i] != r->modifier[0]) {
215 drm_dbg_kms(dev, "bad fb modifier %llu for plane %d\n",
220 /* modifier specific checks: */
221 switch (r->modifier[i]) {
222 case DRM_FORMAT_MOD_SAMSUNG_64_32_TILE:
223 /* NOTE: the pitch restriction may be lifted later if it turns
224 * out that no hw has this restriction:
226 if (r->pixel_format != DRM_FORMAT_NV12 ||
227 width % 128 || height % 32 ||
228 r->pitches[i] % 128) {
229 drm_dbg_kms(dev, "bad modifier data for plane %d\n", i);
239 for (i = info->num_planes; i < 4; i++) {
240 if (r->modifier[i]) {
241 drm_dbg_kms(dev, "non-zero modifier for unused plane %d\n", i);
245 /* Pre-FB_MODIFIERS userspace didn't clear the structs properly. */
246 if (!(r->flags & DRM_MODE_FB_MODIFIERS))
250 drm_dbg_kms(dev, "buffer object handle for unused plane %d\n", i);
255 drm_dbg_kms(dev, "non-zero pitch for unused plane %d\n", i);
260 drm_dbg_kms(dev, "non-zero offset for unused plane %d\n", i);
268 struct drm_framebuffer *
269 drm_internal_framebuffer_create(struct drm_device *dev,
270 const struct drm_mode_fb_cmd2 *r,
271 struct drm_file *file_priv)
273 struct drm_mode_config *config = &dev->mode_config;
274 struct drm_framebuffer *fb;
277 if (r->flags & ~(DRM_MODE_FB_INTERLACED | DRM_MODE_FB_MODIFIERS)) {
278 drm_dbg_kms(dev, "bad framebuffer flags 0x%08x\n", r->flags);
279 return ERR_PTR(-EINVAL);
282 if ((config->min_width > r->width) || (r->width > config->max_width)) {
283 drm_dbg_kms(dev, "bad framebuffer width %d, should be >= %d && <= %d\n",
284 r->width, config->min_width, config->max_width);
285 return ERR_PTR(-EINVAL);
287 if ((config->min_height > r->height) || (r->height > config->max_height)) {
288 drm_dbg_kms(dev, "bad framebuffer height %d, should be >= %d && <= %d\n",
289 r->height, config->min_height, config->max_height);
290 return ERR_PTR(-EINVAL);
293 if (r->flags & DRM_MODE_FB_MODIFIERS &&
294 dev->mode_config.fb_modifiers_not_supported) {
295 drm_dbg_kms(dev, "driver does not support fb modifiers\n");
296 return ERR_PTR(-EINVAL);
299 ret = framebuffer_check(dev, r);
303 fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
305 drm_dbg_kms(dev, "could not create framebuffer\n");
311 EXPORT_SYMBOL_FOR_TESTS_ONLY(drm_internal_framebuffer_create);
314 * drm_mode_addfb2 - add an FB to the graphics configuration
315 * @dev: drm device for the ioctl
316 * @data: data pointer for the ioctl
317 * @file_priv: drm file for the ioctl call
319 * Add a new FB to the specified CRTC, given a user request with format. This is
320 * the 2nd version of the addfb ioctl, which supports multi-planar framebuffers
321 * and uses fourcc codes as pixel format specifiers.
323 * Called by the user via ioctl.
326 * Zero on success, negative errno on failure.
328 int drm_mode_addfb2(struct drm_device *dev,
329 void *data, struct drm_file *file_priv)
331 struct drm_mode_fb_cmd2 *r = data;
332 struct drm_framebuffer *fb;
334 if (!drm_core_check_feature(dev, DRIVER_MODESET))
337 fb = drm_internal_framebuffer_create(dev, r, file_priv);
341 drm_dbg_kms(dev, "[FB:%d]\n", fb->base.id);
342 r->fb_id = fb->base.id;
344 /* Transfer ownership to the filp for reaping on close */
345 mutex_lock(&file_priv->fbs_lock);
346 list_add(&fb->filp_head, &file_priv->fbs);
347 mutex_unlock(&file_priv->fbs_lock);
352 int drm_mode_addfb2_ioctl(struct drm_device *dev,
353 void *data, struct drm_file *file_priv)
356 if (!dev->mode_config.quirk_addfb_prefer_host_byte_order) {
358 * Drivers must set the
359 * quirk_addfb_prefer_host_byte_order quirk to make
360 * the drm_mode_addfb() compat code work correctly on
361 * bigendian machines.
363 * If they don't they interpret pixel_format values
364 * incorrectly for bug compatibility, which in turn
365 * implies the ADDFB2 ioctl does not work correctly
366 * then. So block it to make userspace fallback to
369 drm_dbg_kms(dev, "addfb2 broken on bigendian");
373 return drm_mode_addfb2(dev, data, file_priv);
376 struct drm_mode_rmfb_work {
377 struct work_struct work;
378 struct list_head fbs;
381 static void drm_mode_rmfb_work_fn(struct work_struct *w)
383 struct drm_mode_rmfb_work *arg = container_of(w, typeof(*arg), work);
385 while (!list_empty(&arg->fbs)) {
386 struct drm_framebuffer *fb =
387 list_first_entry(&arg->fbs, typeof(*fb), filp_head);
390 "Removing [FB:%d] from all active usage due to RMFB ioctl\n",
392 list_del_init(&fb->filp_head);
393 drm_framebuffer_remove(fb);
398 * drm_mode_rmfb - remove an FB from the configuration
400 * @fb_id: id of framebuffer to remove
401 * @file_priv: drm file
403 * Remove the specified FB.
405 * Called by the user via ioctl, or by an in-kernel client.
408 * Zero on success, negative errno on failure.
410 int drm_mode_rmfb(struct drm_device *dev, u32 fb_id,
411 struct drm_file *file_priv)
413 struct drm_framebuffer *fb = NULL;
414 struct drm_framebuffer *fbl = NULL;
417 if (!drm_core_check_feature(dev, DRIVER_MODESET))
420 fb = drm_framebuffer_lookup(dev, file_priv, fb_id);
424 mutex_lock(&file_priv->fbs_lock);
425 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
429 mutex_unlock(&file_priv->fbs_lock);
433 list_del_init(&fb->filp_head);
434 mutex_unlock(&file_priv->fbs_lock);
436 /* drop the reference we picked up in framebuffer lookup */
437 drm_framebuffer_put(fb);
440 * we now own the reference that was stored in the fbs list
442 * drm_framebuffer_remove may fail with -EINTR on pending signals,
443 * so run this in a separate stack as there's no way to correctly
444 * handle this after the fb is already removed from the lookup table.
446 if (drm_framebuffer_read_refcount(fb) > 1) {
447 struct drm_mode_rmfb_work arg;
449 INIT_WORK_ONSTACK(&arg.work, drm_mode_rmfb_work_fn);
450 INIT_LIST_HEAD(&arg.fbs);
451 list_add_tail(&fb->filp_head, &arg.fbs);
453 schedule_work(&arg.work);
454 flush_work(&arg.work);
455 destroy_work_on_stack(&arg.work);
457 drm_framebuffer_put(fb);
462 drm_framebuffer_put(fb);
466 int drm_mode_rmfb_ioctl(struct drm_device *dev,
467 void *data, struct drm_file *file_priv)
469 uint32_t *fb_id = data;
471 return drm_mode_rmfb(dev, *fb_id, file_priv);
475 * drm_mode_getfb - get FB info
476 * @dev: drm device for the ioctl
477 * @data: data pointer for the ioctl
478 * @file_priv: drm file for the ioctl call
480 * Lookup the FB given its ID and return info about it.
482 * Called by the user via ioctl.
485 * Zero on success, negative errno on failure.
487 int drm_mode_getfb(struct drm_device *dev,
488 void *data, struct drm_file *file_priv)
490 struct drm_mode_fb_cmd *r = data;
491 struct drm_framebuffer *fb;
494 if (!drm_core_check_feature(dev, DRIVER_MODESET))
497 fb = drm_framebuffer_lookup(dev, file_priv, r->fb_id);
501 /* Multi-planar framebuffers need getfb2. */
502 if (fb->format->num_planes > 1) {
507 if (!fb->funcs->create_handle) {
512 r->height = fb->height;
513 r->width = fb->width;
514 r->depth = fb->format->depth;
515 r->bpp = drm_format_info_bpp(fb->format, 0);
516 r->pitch = fb->pitches[0];
518 /* GET_FB() is an unprivileged ioctl so we must not return a
519 * buffer-handle to non-master processes! For
520 * backwards-compatibility reasons, we cannot make GET_FB() privileged,
521 * so just return an invalid handle for non-masters.
523 if (!drm_is_current_master(file_priv) && !capable(CAP_SYS_ADMIN)) {
529 ret = fb->funcs->create_handle(fb, file_priv, &r->handle);
532 drm_framebuffer_put(fb);
537 * drm_mode_getfb2_ioctl - get extended FB info
538 * @dev: drm device for the ioctl
539 * @data: data pointer for the ioctl
540 * @file_priv: drm file for the ioctl call
542 * Lookup the FB given its ID and return info about it.
544 * Called by the user via ioctl.
547 * Zero on success, negative errno on failure.
549 int drm_mode_getfb2_ioctl(struct drm_device *dev,
550 void *data, struct drm_file *file_priv)
552 struct drm_mode_fb_cmd2 *r = data;
553 struct drm_framebuffer *fb;
557 if (!drm_core_check_feature(dev, DRIVER_MODESET))
560 fb = drm_framebuffer_lookup(dev, file_priv, r->fb_id);
564 /* For multi-plane framebuffers, we require the driver to place the
565 * GEM objects directly in the drm_framebuffer. For single-plane
566 * framebuffers, we can fall back to create_handle.
569 (fb->format->num_planes > 1 || !fb->funcs->create_handle)) {
574 r->height = fb->height;
575 r->width = fb->width;
576 r->pixel_format = fb->format->format;
579 if (!dev->mode_config.fb_modifiers_not_supported)
580 r->flags |= DRM_MODE_FB_MODIFIERS;
582 for (i = 0; i < ARRAY_SIZE(r->handles); i++) {
589 for (i = 0; i < fb->format->num_planes; i++) {
590 r->pitches[i] = fb->pitches[i];
591 r->offsets[i] = fb->offsets[i];
592 if (!dev->mode_config.fb_modifiers_not_supported)
593 r->modifier[i] = fb->modifier;
596 /* GET_FB2() is an unprivileged ioctl so we must not return a
597 * buffer-handle to non master/root processes! To match GET_FB()
598 * just return invalid handles (0) for non masters/root
599 * rather than making GET_FB2() privileged.
601 if (!drm_is_current_master(file_priv) && !capable(CAP_SYS_ADMIN)) {
606 for (i = 0; i < fb->format->num_planes; i++) {
609 /* If we reuse the same object for multiple planes, also
610 * return the same handle.
612 for (j = 0; j < i; j++) {
613 if (fb->obj[i] == fb->obj[j]) {
614 r->handles[i] = r->handles[j];
623 ret = drm_gem_handle_create(file_priv, fb->obj[i],
627 ret = fb->funcs->create_handle(fb, file_priv,
637 /* Delete any previously-created handles on failure. */
638 for (i = 0; i < ARRAY_SIZE(r->handles); i++) {
642 drm_gem_handle_delete(file_priv, r->handles[i]);
644 /* Zero out any handles identical to the one we just
647 for (j = i + 1; j < ARRAY_SIZE(r->handles); j++) {
648 if (r->handles[j] == r->handles[i])
654 drm_framebuffer_put(fb);
659 * drm_mode_dirtyfb_ioctl - flush frontbuffer rendering on an FB
660 * @dev: drm device for the ioctl
661 * @data: data pointer for the ioctl
662 * @file_priv: drm file for the ioctl call
664 * Lookup the FB and flush out the damaged area supplied by userspace as a clip
665 * rectangle list. Generic userspace which does frontbuffer rendering must call
666 * this ioctl to flush out the changes on manual-update display outputs, e.g.
667 * usb display-link, mipi manual update panels or edp panel self refresh modes.
669 * Modesetting drivers which always update the frontbuffer do not need to
670 * implement the corresponding &drm_framebuffer_funcs.dirty callback.
672 * Called by the user via ioctl.
675 * Zero on success, negative errno on failure.
677 int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
678 void *data, struct drm_file *file_priv)
680 struct drm_clip_rect __user *clips_ptr;
681 struct drm_clip_rect *clips = NULL;
682 struct drm_mode_fb_dirty_cmd *r = data;
683 struct drm_framebuffer *fb;
688 if (!drm_core_check_feature(dev, DRIVER_MODESET))
691 fb = drm_framebuffer_lookup(dev, file_priv, r->fb_id);
695 num_clips = r->num_clips;
696 clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
698 if (!num_clips != !clips_ptr) {
703 flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
705 /* If userspace annotates copy, clips must come in pairs */
706 if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
711 if (num_clips && clips_ptr) {
712 if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
716 clips = kcalloc(num_clips, sizeof(*clips), GFP_KERNEL);
722 ret = copy_from_user(clips, clips_ptr,
723 num_clips * sizeof(*clips));
730 if (fb->funcs->dirty) {
731 ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
740 drm_framebuffer_put(fb);
746 * drm_fb_release - remove and free the FBs on this file
747 * @priv: drm file for the ioctl
749 * Destroy all the FBs associated with @filp.
751 * Called by the user via ioctl.
754 * Zero on success, negative errno on failure.
756 void drm_fb_release(struct drm_file *priv)
758 struct drm_framebuffer *fb, *tfb;
759 struct drm_mode_rmfb_work arg;
761 INIT_LIST_HEAD(&arg.fbs);
764 * When the file gets released that means no one else can access the fb
765 * list any more, so no need to grab fpriv->fbs_lock. And we need to
766 * avoid upsetting lockdep since the universal cursor code adds a
767 * framebuffer while holding mutex locks.
769 * Note that a real deadlock between fpriv->fbs_lock and the modeset
770 * locks is impossible here since no one else but this function can get
773 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
774 if (drm_framebuffer_read_refcount(fb) > 1) {
775 list_move_tail(&fb->filp_head, &arg.fbs);
777 list_del_init(&fb->filp_head);
779 /* This drops the fpriv->fbs reference. */
780 drm_framebuffer_put(fb);
784 if (!list_empty(&arg.fbs)) {
785 INIT_WORK_ONSTACK(&arg.work, drm_mode_rmfb_work_fn);
787 schedule_work(&arg.work);
788 flush_work(&arg.work);
789 destroy_work_on_stack(&arg.work);
793 void drm_framebuffer_free(struct kref *kref)
795 struct drm_framebuffer *fb =
796 container_of(kref, struct drm_framebuffer, base.refcount);
797 struct drm_device *dev = fb->dev;
800 * The lookup idr holds a weak reference, which has not necessarily been
801 * removed at this point. Check for that.
803 drm_mode_object_unregister(dev, &fb->base);
805 fb->funcs->destroy(fb);
809 * drm_framebuffer_init - initialize a framebuffer
811 * @fb: framebuffer to be initialized
812 * @funcs: ... with these functions
814 * Allocates an ID for the framebuffer's parent mode object, sets its mode
815 * functions & device file and adds it to the master fd list.
818 * This functions publishes the fb and makes it available for concurrent access
819 * by other users. Which means by this point the fb _must_ be fully set up -
820 * since all the fb attributes are invariant over its lifetime, no further
821 * locking but only correct reference counting is required.
824 * Zero on success, error code on failure.
826 int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb,
827 const struct drm_framebuffer_funcs *funcs)
831 if (WARN_ON_ONCE(fb->dev != dev || !fb->format))
834 INIT_LIST_HEAD(&fb->filp_head);
837 strcpy(fb->comm, current->comm);
839 ret = __drm_mode_object_add(dev, &fb->base, DRM_MODE_OBJECT_FB,
840 false, drm_framebuffer_free);
844 mutex_lock(&dev->mode_config.fb_lock);
845 dev->mode_config.num_fb++;
846 list_add(&fb->head, &dev->mode_config.fb_list);
847 mutex_unlock(&dev->mode_config.fb_lock);
849 drm_mode_object_register(dev, &fb->base);
853 EXPORT_SYMBOL(drm_framebuffer_init);
856 * drm_framebuffer_lookup - look up a drm framebuffer and grab a reference
858 * @file_priv: drm file to check for lease against.
859 * @id: id of the fb object
861 * If successful, this grabs an additional reference to the framebuffer -
862 * callers need to make sure to eventually unreference the returned framebuffer
863 * again, using drm_framebuffer_put().
865 struct drm_framebuffer *drm_framebuffer_lookup(struct drm_device *dev,
866 struct drm_file *file_priv,
869 struct drm_mode_object *obj;
870 struct drm_framebuffer *fb = NULL;
872 obj = __drm_mode_object_find(dev, file_priv, id, DRM_MODE_OBJECT_FB);
877 EXPORT_SYMBOL(drm_framebuffer_lookup);
880 * drm_framebuffer_unregister_private - unregister a private fb from the lookup idr
881 * @fb: fb to unregister
883 * Drivers need to call this when cleaning up driver-private framebuffers, e.g.
884 * those used for fbdev. Note that the caller must hold a reference of its own,
885 * i.e. the object may not be destroyed through this call (since it'll lead to a
886 * locking inversion).
888 * NOTE: This function is deprecated. For driver-private framebuffers it is not
889 * recommended to embed a framebuffer struct info fbdev struct, instead, a
890 * framebuffer pointer is preferred and drm_framebuffer_put() should be called
891 * when the framebuffer is to be cleaned up.
893 void drm_framebuffer_unregister_private(struct drm_framebuffer *fb)
895 struct drm_device *dev;
902 /* Mark fb as reaped and drop idr ref. */
903 drm_mode_object_unregister(dev, &fb->base);
905 EXPORT_SYMBOL(drm_framebuffer_unregister_private);
908 * drm_framebuffer_cleanup - remove a framebuffer object
909 * @fb: framebuffer to remove
911 * Cleanup framebuffer. This function is intended to be used from the drivers
912 * &drm_framebuffer_funcs.destroy callback. It can also be used to clean up
913 * driver private framebuffers embedded into a larger structure.
915 * Note that this function does not remove the fb from active usage - if it is
916 * still used anywhere, hilarity can ensue since userspace could call getfb on
917 * the id and get back -EINVAL. Obviously no concern at driver unload time.
919 * Also, the framebuffer will not be removed from the lookup idr - for
920 * user-created framebuffers this will happen in the rmfb ioctl. For
921 * driver-private objects (e.g. for fbdev) drivers need to explicitly call
922 * drm_framebuffer_unregister_private.
924 void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
926 struct drm_device *dev = fb->dev;
928 mutex_lock(&dev->mode_config.fb_lock);
930 dev->mode_config.num_fb--;
931 mutex_unlock(&dev->mode_config.fb_lock);
933 EXPORT_SYMBOL(drm_framebuffer_cleanup);
935 static int atomic_remove_fb(struct drm_framebuffer *fb)
937 struct drm_modeset_acquire_ctx ctx;
938 struct drm_device *dev = fb->dev;
939 struct drm_atomic_state *state;
940 struct drm_plane *plane;
941 struct drm_connector *conn __maybe_unused;
942 struct drm_connector_state *conn_state;
945 bool disable_crtcs = false;
948 drm_modeset_acquire_init(&ctx, 0);
950 state = drm_atomic_state_alloc(dev);
955 state->acquire_ctx = &ctx;
959 ret = drm_modeset_lock_all_ctx(dev, &ctx);
963 drm_for_each_plane(plane, dev) {
964 struct drm_plane_state *plane_state;
966 if (plane->state->fb != fb)
970 "Disabling [PLANE:%d:%s] because [FB:%d] is removed\n",
971 plane->base.id, plane->name, fb->base.id);
973 plane_state = drm_atomic_get_plane_state(state, plane);
974 if (IS_ERR(plane_state)) {
975 ret = PTR_ERR(plane_state);
979 if (disable_crtcs && plane_state->crtc->primary == plane) {
980 struct drm_crtc_state *crtc_state;
983 "Disabling [CRTC:%d:%s] because [FB:%d] is removed\n",
984 plane_state->crtc->base.id,
985 plane_state->crtc->name, fb->base.id);
987 crtc_state = drm_atomic_get_existing_crtc_state(state, plane_state->crtc);
989 ret = drm_atomic_add_affected_connectors(state, plane_state->crtc);
993 crtc_state->active = false;
994 ret = drm_atomic_set_mode_for_crtc(crtc_state, NULL);
999 drm_atomic_set_fb_for_plane(plane_state, NULL);
1000 ret = drm_atomic_set_crtc_for_plane(plane_state, NULL);
1004 plane_mask |= drm_plane_mask(plane);
1007 /* This list is only filled when disable_crtcs is set. */
1008 for_each_new_connector_in_state(state, conn, conn_state, i) {
1009 ret = drm_atomic_set_crtc_for_connector(conn_state, NULL);
1016 ret = drm_atomic_commit(state);
1019 if (ret == -EDEADLK) {
1020 drm_atomic_state_clear(state);
1021 drm_modeset_backoff(&ctx);
1025 drm_atomic_state_put(state);
1028 drm_modeset_drop_locks(&ctx);
1029 drm_modeset_acquire_fini(&ctx);
1031 if (ret == -EINVAL && !disable_crtcs) {
1032 disable_crtcs = true;
1039 static void legacy_remove_fb(struct drm_framebuffer *fb)
1041 struct drm_device *dev = fb->dev;
1042 struct drm_crtc *crtc;
1043 struct drm_plane *plane;
1045 drm_modeset_lock_all(dev);
1046 /* remove from any CRTC */
1047 drm_for_each_crtc(crtc, dev) {
1048 if (crtc->primary->fb == fb) {
1050 "Disabling [CRTC:%d:%s] because [FB:%d] is removed\n",
1051 crtc->base.id, crtc->name, fb->base.id);
1053 /* should turn off the crtc */
1054 if (drm_crtc_force_disable(crtc))
1055 DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc);
1059 drm_for_each_plane(plane, dev) {
1060 if (plane->fb == fb) {
1062 "Disabling [PLANE:%d:%s] because [FB:%d] is removed\n",
1063 plane->base.id, plane->name, fb->base.id);
1064 drm_plane_force_disable(plane);
1067 drm_modeset_unlock_all(dev);
1071 * drm_framebuffer_remove - remove and unreference a framebuffer object
1072 * @fb: framebuffer to remove
1074 * Scans all the CRTCs and planes in @dev's mode_config. If they're
1075 * using @fb, removes it, setting it to NULL. Then drops the reference to the
1076 * passed-in framebuffer. Might take the modeset locks.
1078 * Note that this function optimizes the cleanup away if the caller holds the
1079 * last reference to the framebuffer. It is also guaranteed to not take the
1080 * modeset locks in this case.
1082 void drm_framebuffer_remove(struct drm_framebuffer *fb)
1084 struct drm_device *dev;
1091 WARN_ON(!list_empty(&fb->filp_head));
1094 * drm ABI mandates that we remove any deleted framebuffers from active
1095 * usage. But since most sane clients only remove framebuffers they no
1096 * longer need, try to optimize this away.
1098 * Since we're holding a reference ourselves, observing a refcount of 1
1099 * means that we're the last holder and can skip it. Also, the refcount
1100 * can never increase from 1 again, so we don't need any barriers or
1103 * Note that userspace could try to race with use and instate a new
1104 * usage _after_ we've cleared all current ones. End result will be an
1105 * in-use fb with fb-id == 0. Userspace is allowed to shoot its own foot
1108 if (drm_framebuffer_read_refcount(fb) > 1) {
1109 if (drm_drv_uses_atomic_modeset(dev)) {
1110 int ret = atomic_remove_fb(fb);
1112 WARN(ret, "atomic remove_fb failed with %i\n", ret);
1114 legacy_remove_fb(fb);
1117 drm_framebuffer_put(fb);
1119 EXPORT_SYMBOL(drm_framebuffer_remove);
1121 void drm_framebuffer_print_info(struct drm_printer *p, unsigned int indent,
1122 const struct drm_framebuffer *fb)
1126 drm_printf_indent(p, indent, "allocated by = %s\n", fb->comm);
1127 drm_printf_indent(p, indent, "refcount=%u\n",
1128 drm_framebuffer_read_refcount(fb));
1129 drm_printf_indent(p, indent, "format=%p4cc\n", &fb->format->format);
1130 drm_printf_indent(p, indent, "modifier=0x%llx\n", fb->modifier);
1131 drm_printf_indent(p, indent, "size=%ux%u\n", fb->width, fb->height);
1132 drm_printf_indent(p, indent, "layers:\n");
1134 for (i = 0; i < fb->format->num_planes; i++) {
1135 drm_printf_indent(p, indent + 1, "size[%u]=%dx%d\n", i,
1136 drm_format_info_plane_width(fb->format, fb->width, i),
1137 drm_format_info_plane_height(fb->format, fb->height, i));
1138 drm_printf_indent(p, indent + 1, "pitch[%u]=%u\n", i, fb->pitches[i]);
1139 drm_printf_indent(p, indent + 1, "offset[%u]=%u\n", i, fb->offsets[i]);
1140 drm_printf_indent(p, indent + 1, "obj[%u]:%s\n", i,
1141 fb->obj[i] ? "" : "(null)");
1143 drm_gem_print_info(p, indent + 2, fb->obj[i]);
1147 #ifdef CONFIG_DEBUG_FS
1148 static int drm_framebuffer_info(struct seq_file *m, void *data)
1150 struct drm_debugfs_entry *entry = m->private;
1151 struct drm_device *dev = entry->dev;
1152 struct drm_printer p = drm_seq_file_printer(m);
1153 struct drm_framebuffer *fb;
1155 mutex_lock(&dev->mode_config.fb_lock);
1156 drm_for_each_fb(fb, dev) {
1157 drm_printf(&p, "framebuffer[%u]:\n", fb->base.id);
1158 drm_framebuffer_print_info(&p, 1, fb);
1160 mutex_unlock(&dev->mode_config.fb_lock);
1165 static const struct drm_debugfs_info drm_framebuffer_debugfs_list[] = {
1166 { "framebuffer", drm_framebuffer_info, 0 },
1169 void drm_framebuffer_debugfs_init(struct drm_device *dev)
1171 drm_debugfs_add_files(dev, drm_framebuffer_debugfs_list,
1172 ARRAY_SIZE(drm_framebuffer_debugfs_list));