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