drm: Extract drm_framebuffer.[hc]
[linux-2.6-block.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  * drm_mode_addfb - add an FB to the graphics configuration
32  * @dev: drm device for the ioctl
33  * @data: data pointer for the ioctl
34  * @file_priv: drm file for the ioctl call
35  *
36  * Add a new FB to the specified CRTC, given a user request. This is the
37  * original addfb ioctl which only supported RGB formats.
38  *
39  * Called by the user via ioctl.
40  *
41  * Returns:
42  * Zero on success, negative errno on failure.
43  */
44 int drm_mode_addfb(struct drm_device *dev,
45                    void *data, struct drm_file *file_priv)
46 {
47         struct drm_mode_fb_cmd *or = data;
48         struct drm_mode_fb_cmd2 r = {};
49         int ret;
50
51         /* convert to new format and call new ioctl */
52         r.fb_id = or->fb_id;
53         r.width = or->width;
54         r.height = or->height;
55         r.pitches[0] = or->pitch;
56         r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
57         r.handles[0] = or->handle;
58
59         ret = drm_mode_addfb2(dev, &r, file_priv);
60         if (ret)
61                 return ret;
62
63         or->fb_id = r.fb_id;
64
65         return 0;
66 }
67
68 static int format_check(const struct drm_mode_fb_cmd2 *r)
69 {
70         uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
71         char *format_name;
72
73         switch (format) {
74         case DRM_FORMAT_C8:
75         case DRM_FORMAT_RGB332:
76         case DRM_FORMAT_BGR233:
77         case DRM_FORMAT_XRGB4444:
78         case DRM_FORMAT_XBGR4444:
79         case DRM_FORMAT_RGBX4444:
80         case DRM_FORMAT_BGRX4444:
81         case DRM_FORMAT_ARGB4444:
82         case DRM_FORMAT_ABGR4444:
83         case DRM_FORMAT_RGBA4444:
84         case DRM_FORMAT_BGRA4444:
85         case DRM_FORMAT_XRGB1555:
86         case DRM_FORMAT_XBGR1555:
87         case DRM_FORMAT_RGBX5551:
88         case DRM_FORMAT_BGRX5551:
89         case DRM_FORMAT_ARGB1555:
90         case DRM_FORMAT_ABGR1555:
91         case DRM_FORMAT_RGBA5551:
92         case DRM_FORMAT_BGRA5551:
93         case DRM_FORMAT_RGB565:
94         case DRM_FORMAT_BGR565:
95         case DRM_FORMAT_RGB888:
96         case DRM_FORMAT_BGR888:
97         case DRM_FORMAT_XRGB8888:
98         case DRM_FORMAT_XBGR8888:
99         case DRM_FORMAT_RGBX8888:
100         case DRM_FORMAT_BGRX8888:
101         case DRM_FORMAT_ARGB8888:
102         case DRM_FORMAT_ABGR8888:
103         case DRM_FORMAT_RGBA8888:
104         case DRM_FORMAT_BGRA8888:
105         case DRM_FORMAT_XRGB2101010:
106         case DRM_FORMAT_XBGR2101010:
107         case DRM_FORMAT_RGBX1010102:
108         case DRM_FORMAT_BGRX1010102:
109         case DRM_FORMAT_ARGB2101010:
110         case DRM_FORMAT_ABGR2101010:
111         case DRM_FORMAT_RGBA1010102:
112         case DRM_FORMAT_BGRA1010102:
113         case DRM_FORMAT_YUYV:
114         case DRM_FORMAT_YVYU:
115         case DRM_FORMAT_UYVY:
116         case DRM_FORMAT_VYUY:
117         case DRM_FORMAT_AYUV:
118         case DRM_FORMAT_NV12:
119         case DRM_FORMAT_NV21:
120         case DRM_FORMAT_NV16:
121         case DRM_FORMAT_NV61:
122         case DRM_FORMAT_NV24:
123         case DRM_FORMAT_NV42:
124         case DRM_FORMAT_YUV410:
125         case DRM_FORMAT_YVU410:
126         case DRM_FORMAT_YUV411:
127         case DRM_FORMAT_YVU411:
128         case DRM_FORMAT_YUV420:
129         case DRM_FORMAT_YVU420:
130         case DRM_FORMAT_YUV422:
131         case DRM_FORMAT_YVU422:
132         case DRM_FORMAT_YUV444:
133         case DRM_FORMAT_YVU444:
134                 return 0;
135         default:
136                 format_name = drm_get_format_name(r->pixel_format);
137                 DRM_DEBUG_KMS("invalid pixel format %s\n", format_name);
138                 kfree(format_name);
139                 return -EINVAL;
140         }
141 }
142
143 static int framebuffer_check(const struct drm_mode_fb_cmd2 *r)
144 {
145         int ret, hsub, vsub, num_planes, i;
146
147         ret = format_check(r);
148         if (ret) {
149                 char *format_name = drm_get_format_name(r->pixel_format);
150                 DRM_DEBUG_KMS("bad framebuffer format %s\n", format_name);
151                 kfree(format_name);
152                 return ret;
153         }
154
155         hsub = drm_format_horz_chroma_subsampling(r->pixel_format);
156         vsub = drm_format_vert_chroma_subsampling(r->pixel_format);
157         num_planes = drm_format_num_planes(r->pixel_format);
158
159         if (r->width == 0 || r->width % hsub) {
160                 DRM_DEBUG_KMS("bad framebuffer width %u\n", r->width);
161                 return -EINVAL;
162         }
163
164         if (r->height == 0 || r->height % vsub) {
165                 DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height);
166                 return -EINVAL;
167         }
168
169         for (i = 0; i < num_planes; i++) {
170                 unsigned int width = r->width / (i != 0 ? hsub : 1);
171                 unsigned int height = r->height / (i != 0 ? vsub : 1);
172                 unsigned int cpp = drm_format_plane_cpp(r->pixel_format, i);
173
174                 if (!r->handles[i]) {
175                         DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i);
176                         return -EINVAL;
177                 }
178
179                 if ((uint64_t) width * cpp > UINT_MAX)
180                         return -ERANGE;
181
182                 if ((uint64_t) height * r->pitches[i] + r->offsets[i] > UINT_MAX)
183                         return -ERANGE;
184
185                 if (r->pitches[i] < width * cpp) {
186                         DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i);
187                         return -EINVAL;
188                 }
189
190                 if (r->modifier[i] && !(r->flags & DRM_MODE_FB_MODIFIERS)) {
191                         DRM_DEBUG_KMS("bad fb modifier %llu for plane %d\n",
192                                       r->modifier[i], i);
193                         return -EINVAL;
194                 }
195
196                 /* modifier specific checks: */
197                 switch (r->modifier[i]) {
198                 case DRM_FORMAT_MOD_SAMSUNG_64_32_TILE:
199                         /* NOTE: the pitch restriction may be lifted later if it turns
200                          * out that no hw has this restriction:
201                          */
202                         if (r->pixel_format != DRM_FORMAT_NV12 ||
203                                         width % 128 || height % 32 ||
204                                         r->pitches[i] % 128) {
205                                 DRM_DEBUG_KMS("bad modifier data for plane %d\n", i);
206                                 return -EINVAL;
207                         }
208                         break;
209
210                 default:
211                         break;
212                 }
213         }
214
215         for (i = num_planes; i < 4; i++) {
216                 if (r->modifier[i]) {
217                         DRM_DEBUG_KMS("non-zero modifier for unused plane %d\n", i);
218                         return -EINVAL;
219                 }
220
221                 /* Pre-FB_MODIFIERS userspace didn't clear the structs properly. */
222                 if (!(r->flags & DRM_MODE_FB_MODIFIERS))
223                         continue;
224
225                 if (r->handles[i]) {
226                         DRM_DEBUG_KMS("buffer object handle for unused plane %d\n", i);
227                         return -EINVAL;
228                 }
229
230                 if (r->pitches[i]) {
231                         DRM_DEBUG_KMS("non-zero pitch for unused plane %d\n", i);
232                         return -EINVAL;
233                 }
234
235                 if (r->offsets[i]) {
236                         DRM_DEBUG_KMS("non-zero offset for unused plane %d\n", i);
237                         return -EINVAL;
238                 }
239         }
240
241         return 0;
242 }
243
244 struct drm_framebuffer *
245 drm_internal_framebuffer_create(struct drm_device *dev,
246                                 const struct drm_mode_fb_cmd2 *r,
247                                 struct drm_file *file_priv)
248 {
249         struct drm_mode_config *config = &dev->mode_config;
250         struct drm_framebuffer *fb;
251         int ret;
252
253         if (r->flags & ~(DRM_MODE_FB_INTERLACED | DRM_MODE_FB_MODIFIERS)) {
254                 DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r->flags);
255                 return ERR_PTR(-EINVAL);
256         }
257
258         if ((config->min_width > r->width) || (r->width > config->max_width)) {
259                 DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
260                           r->width, config->min_width, config->max_width);
261                 return ERR_PTR(-EINVAL);
262         }
263         if ((config->min_height > r->height) || (r->height > config->max_height)) {
264                 DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
265                           r->height, config->min_height, config->max_height);
266                 return ERR_PTR(-EINVAL);
267         }
268
269         if (r->flags & DRM_MODE_FB_MODIFIERS &&
270             !dev->mode_config.allow_fb_modifiers) {
271                 DRM_DEBUG_KMS("driver does not support fb modifiers\n");
272                 return ERR_PTR(-EINVAL);
273         }
274
275         ret = framebuffer_check(r);
276         if (ret)
277                 return ERR_PTR(ret);
278
279         fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
280         if (IS_ERR(fb)) {
281                 DRM_DEBUG_KMS("could not create framebuffer\n");
282                 return fb;
283         }
284
285         return fb;
286 }
287
288 /**
289  * drm_mode_addfb2 - add an FB to the graphics configuration
290  * @dev: drm device for the ioctl
291  * @data: data pointer for the ioctl
292  * @file_priv: drm file for the ioctl call
293  *
294  * Add a new FB to the specified CRTC, given a user request with format. This is
295  * the 2nd version of the addfb ioctl, which supports multi-planar framebuffers
296  * and uses fourcc codes as pixel format specifiers.
297  *
298  * Called by the user via ioctl.
299  *
300  * Returns:
301  * Zero on success, negative errno on failure.
302  */
303 int drm_mode_addfb2(struct drm_device *dev,
304                     void *data, struct drm_file *file_priv)
305 {
306         struct drm_mode_fb_cmd2 *r = data;
307         struct drm_framebuffer *fb;
308
309         if (!drm_core_check_feature(dev, DRIVER_MODESET))
310                 return -EINVAL;
311
312         fb = drm_internal_framebuffer_create(dev, r, file_priv);
313         if (IS_ERR(fb))
314                 return PTR_ERR(fb);
315
316         DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
317         r->fb_id = fb->base.id;
318
319         /* Transfer ownership to the filp for reaping on close */
320         mutex_lock(&file_priv->fbs_lock);
321         list_add(&fb->filp_head, &file_priv->fbs);
322         mutex_unlock(&file_priv->fbs_lock);
323
324         return 0;
325 }
326
327 struct drm_mode_rmfb_work {
328         struct work_struct work;
329         struct list_head fbs;
330 };
331
332 static void drm_mode_rmfb_work_fn(struct work_struct *w)
333 {
334         struct drm_mode_rmfb_work *arg = container_of(w, typeof(*arg), work);
335
336         while (!list_empty(&arg->fbs)) {
337                 struct drm_framebuffer *fb =
338                         list_first_entry(&arg->fbs, typeof(*fb), filp_head);
339
340                 list_del_init(&fb->filp_head);
341                 drm_framebuffer_remove(fb);
342         }
343 }
344
345 /**
346  * drm_mode_rmfb - remove an FB from the configuration
347  * @dev: drm device for the ioctl
348  * @data: data pointer for the ioctl
349  * @file_priv: drm file for the ioctl call
350  *
351  * Remove the FB specified by the user.
352  *
353  * Called by the user via ioctl.
354  *
355  * Returns:
356  * Zero on success, negative errno on failure.
357  */
358 int drm_mode_rmfb(struct drm_device *dev,
359                    void *data, struct drm_file *file_priv)
360 {
361         struct drm_framebuffer *fb = NULL;
362         struct drm_framebuffer *fbl = NULL;
363         uint32_t *id = data;
364         int found = 0;
365
366         if (!drm_core_check_feature(dev, DRIVER_MODESET))
367                 return -EINVAL;
368
369         fb = drm_framebuffer_lookup(dev, *id);
370         if (!fb)
371                 return -ENOENT;
372
373         mutex_lock(&file_priv->fbs_lock);
374         list_for_each_entry(fbl, &file_priv->fbs, filp_head)
375                 if (fb == fbl)
376                         found = 1;
377         if (!found) {
378                 mutex_unlock(&file_priv->fbs_lock);
379                 goto fail_unref;
380         }
381
382         list_del_init(&fb->filp_head);
383         mutex_unlock(&file_priv->fbs_lock);
384
385         /* drop the reference we picked up in framebuffer lookup */
386         drm_framebuffer_unreference(fb);
387
388         /*
389          * we now own the reference that was stored in the fbs list
390          *
391          * drm_framebuffer_remove may fail with -EINTR on pending signals,
392          * so run this in a separate stack as there's no way to correctly
393          * handle this after the fb is already removed from the lookup table.
394          */
395         if (drm_framebuffer_read_refcount(fb) > 1) {
396                 struct drm_mode_rmfb_work arg;
397
398                 INIT_WORK_ONSTACK(&arg.work, drm_mode_rmfb_work_fn);
399                 INIT_LIST_HEAD(&arg.fbs);
400                 list_add_tail(&fb->filp_head, &arg.fbs);
401
402                 schedule_work(&arg.work);
403                 flush_work(&arg.work);
404                 destroy_work_on_stack(&arg.work);
405         } else
406                 drm_framebuffer_unreference(fb);
407
408         return 0;
409
410 fail_unref:
411         drm_framebuffer_unreference(fb);
412         return -ENOENT;
413 }
414
415 /**
416  * drm_mode_getfb - get FB info
417  * @dev: drm device for the ioctl
418  * @data: data pointer for the ioctl
419  * @file_priv: drm file for the ioctl call
420  *
421  * Lookup the FB given its ID and return info about it.
422  *
423  * Called by the user via ioctl.
424  *
425  * Returns:
426  * Zero on success, negative errno on failure.
427  */
428 int drm_mode_getfb(struct drm_device *dev,
429                    void *data, struct drm_file *file_priv)
430 {
431         struct drm_mode_fb_cmd *r = data;
432         struct drm_framebuffer *fb;
433         int ret;
434
435         if (!drm_core_check_feature(dev, DRIVER_MODESET))
436                 return -EINVAL;
437
438         fb = drm_framebuffer_lookup(dev, r->fb_id);
439         if (!fb)
440                 return -ENOENT;
441
442         r->height = fb->height;
443         r->width = fb->width;
444         r->depth = fb->depth;
445         r->bpp = fb->bits_per_pixel;
446         r->pitch = fb->pitches[0];
447         if (fb->funcs->create_handle) {
448                 if (drm_is_current_master(file_priv) || capable(CAP_SYS_ADMIN) ||
449                     drm_is_control_client(file_priv)) {
450                         ret = fb->funcs->create_handle(fb, file_priv,
451                                                        &r->handle);
452                 } else {
453                         /* GET_FB() is an unprivileged ioctl so we must not
454                          * return a buffer-handle to non-master processes! For
455                          * backwards-compatibility reasons, we cannot make
456                          * GET_FB() privileged, so just return an invalid handle
457                          * for non-masters. */
458                         r->handle = 0;
459                         ret = 0;
460                 }
461         } else {
462                 ret = -ENODEV;
463         }
464
465         drm_framebuffer_unreference(fb);
466
467         return ret;
468 }
469
470 /**
471  * drm_mode_dirtyfb_ioctl - flush frontbuffer rendering on an FB
472  * @dev: drm device for the ioctl
473  * @data: data pointer for the ioctl
474  * @file_priv: drm file for the ioctl call
475  *
476  * Lookup the FB and flush out the damaged area supplied by userspace as a clip
477  * rectangle list. Generic userspace which does frontbuffer rendering must call
478  * this ioctl to flush out the changes on manual-update display outputs, e.g.
479  * usb display-link, mipi manual update panels or edp panel self refresh modes.
480  *
481  * Modesetting drivers which always update the frontbuffer do not need to
482  * implement the corresponding ->dirty framebuffer callback.
483  *
484  * Called by the user via ioctl.
485  *
486  * Returns:
487  * Zero on success, negative errno on failure.
488  */
489 int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
490                            void *data, struct drm_file *file_priv)
491 {
492         struct drm_clip_rect __user *clips_ptr;
493         struct drm_clip_rect *clips = NULL;
494         struct drm_mode_fb_dirty_cmd *r = data;
495         struct drm_framebuffer *fb;
496         unsigned flags;
497         int num_clips;
498         int ret;
499
500         if (!drm_core_check_feature(dev, DRIVER_MODESET))
501                 return -EINVAL;
502
503         fb = drm_framebuffer_lookup(dev, r->fb_id);
504         if (!fb)
505                 return -ENOENT;
506
507         num_clips = r->num_clips;
508         clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
509
510         if (!num_clips != !clips_ptr) {
511                 ret = -EINVAL;
512                 goto out_err1;
513         }
514
515         flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
516
517         /* If userspace annotates copy, clips must come in pairs */
518         if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
519                 ret = -EINVAL;
520                 goto out_err1;
521         }
522
523         if (num_clips && clips_ptr) {
524                 if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
525                         ret = -EINVAL;
526                         goto out_err1;
527                 }
528                 clips = kcalloc(num_clips, sizeof(*clips), GFP_KERNEL);
529                 if (!clips) {
530                         ret = -ENOMEM;
531                         goto out_err1;
532                 }
533
534                 ret = copy_from_user(clips, clips_ptr,
535                                      num_clips * sizeof(*clips));
536                 if (ret) {
537                         ret = -EFAULT;
538                         goto out_err2;
539                 }
540         }
541
542         if (fb->funcs->dirty) {
543                 ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
544                                        clips, num_clips);
545         } else {
546                 ret = -ENOSYS;
547         }
548
549 out_err2:
550         kfree(clips);
551 out_err1:
552         drm_framebuffer_unreference(fb);
553
554         return ret;
555 }
556
557 /**
558  * drm_fb_release - remove and free the FBs on this file
559  * @priv: drm file for the ioctl
560  *
561  * Destroy all the FBs associated with @filp.
562  *
563  * Called by the user via ioctl.
564  *
565  * Returns:
566  * Zero on success, negative errno on failure.
567  */
568 void drm_fb_release(struct drm_file *priv)
569 {
570         struct drm_framebuffer *fb, *tfb;
571         struct drm_mode_rmfb_work arg;
572
573         INIT_LIST_HEAD(&arg.fbs);
574
575         /*
576          * When the file gets released that means no one else can access the fb
577          * list any more, so no need to grab fpriv->fbs_lock. And we need to
578          * avoid upsetting lockdep since the universal cursor code adds a
579          * framebuffer while holding mutex locks.
580          *
581          * Note that a real deadlock between fpriv->fbs_lock and the modeset
582          * locks is impossible here since no one else but this function can get
583          * at it any more.
584          */
585         list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
586                 if (drm_framebuffer_read_refcount(fb) > 1) {
587                         list_move_tail(&fb->filp_head, &arg.fbs);
588                 } else {
589                         list_del_init(&fb->filp_head);
590
591                         /* This drops the fpriv->fbs reference. */
592                         drm_framebuffer_unreference(fb);
593                 }
594         }
595
596         if (!list_empty(&arg.fbs)) {
597                 INIT_WORK_ONSTACK(&arg.work, drm_mode_rmfb_work_fn);
598
599                 schedule_work(&arg.work);
600                 flush_work(&arg.work);
601                 destroy_work_on_stack(&arg.work);
602         }
603 }
604
605 void drm_framebuffer_free(struct kref *kref)
606 {
607         struct drm_framebuffer *fb =
608                         container_of(kref, struct drm_framebuffer, base.refcount);
609         struct drm_device *dev = fb->dev;
610
611         /*
612          * The lookup idr holds a weak reference, which has not necessarily been
613          * removed at this point. Check for that.
614          */
615         drm_mode_object_unregister(dev, &fb->base);
616
617         fb->funcs->destroy(fb);
618 }
619
620 /**
621  * drm_framebuffer_init - initialize a framebuffer
622  * @dev: DRM device
623  * @fb: framebuffer to be initialized
624  * @funcs: ... with these functions
625  *
626  * Allocates an ID for the framebuffer's parent mode object, sets its mode
627  * functions & device file and adds it to the master fd list.
628  *
629  * IMPORTANT:
630  * This functions publishes the fb and makes it available for concurrent access
631  * by other users. Which means by this point the fb _must_ be fully set up -
632  * since all the fb attributes are invariant over its lifetime, no further
633  * locking but only correct reference counting is required.
634  *
635  * Returns:
636  * Zero on success, error code on failure.
637  */
638 int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb,
639                          const struct drm_framebuffer_funcs *funcs)
640 {
641         int ret;
642
643         INIT_LIST_HEAD(&fb->filp_head);
644         fb->dev = dev;
645         fb->funcs = funcs;
646
647         ret = drm_mode_object_get_reg(dev, &fb->base, DRM_MODE_OBJECT_FB,
648                                       false, drm_framebuffer_free);
649         if (ret)
650                 goto out;
651
652         mutex_lock(&dev->mode_config.fb_lock);
653         dev->mode_config.num_fb++;
654         list_add(&fb->head, &dev->mode_config.fb_list);
655         mutex_unlock(&dev->mode_config.fb_lock);
656
657         drm_mode_object_register(dev, &fb->base);
658 out:
659         return ret;
660 }
661 EXPORT_SYMBOL(drm_framebuffer_init);
662
663 /**
664  * drm_framebuffer_lookup - look up a drm framebuffer and grab a reference
665  * @dev: drm device
666  * @id: id of the fb object
667  *
668  * If successful, this grabs an additional reference to the framebuffer -
669  * callers need to make sure to eventually unreference the returned framebuffer
670  * again, using @drm_framebuffer_unreference.
671  */
672 struct drm_framebuffer *drm_framebuffer_lookup(struct drm_device *dev,
673                                                uint32_t id)
674 {
675         struct drm_mode_object *obj;
676         struct drm_framebuffer *fb = NULL;
677
678         obj = __drm_mode_object_find(dev, id, DRM_MODE_OBJECT_FB);
679         if (obj)
680                 fb = obj_to_fb(obj);
681         return fb;
682 }
683 EXPORT_SYMBOL(drm_framebuffer_lookup);
684
685 /**
686  * drm_framebuffer_unregister_private - unregister a private fb from the lookup idr
687  * @fb: fb to unregister
688  *
689  * Drivers need to call this when cleaning up driver-private framebuffers, e.g.
690  * those used for fbdev. Note that the caller must hold a reference of it's own,
691  * i.e. the object may not be destroyed through this call (since it'll lead to a
692  * locking inversion).
693  */
694 void drm_framebuffer_unregister_private(struct drm_framebuffer *fb)
695 {
696         struct drm_device *dev;
697
698         if (!fb)
699                 return;
700
701         dev = fb->dev;
702
703         /* Mark fb as reaped and drop idr ref. */
704         drm_mode_object_unregister(dev, &fb->base);
705 }
706 EXPORT_SYMBOL(drm_framebuffer_unregister_private);
707
708 /**
709  * drm_framebuffer_cleanup - remove a framebuffer object
710  * @fb: framebuffer to remove
711  *
712  * Cleanup framebuffer. This function is intended to be used from the drivers
713  * ->destroy callback. It can also be used to clean up driver private
714  * framebuffers embedded into a larger structure.
715  *
716  * Note that this function does not remove the fb from active usuage - if it is
717  * still used anywhere, hilarity can ensue since userspace could call getfb on
718  * the id and get back -EINVAL. Obviously no concern at driver unload time.
719  *
720  * Also, the framebuffer will not be removed from the lookup idr - for
721  * user-created framebuffers this will happen in in the rmfb ioctl. For
722  * driver-private objects (e.g. for fbdev) drivers need to explicitly call
723  * drm_framebuffer_unregister_private.
724  */
725 void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
726 {
727         struct drm_device *dev = fb->dev;
728
729         mutex_lock(&dev->mode_config.fb_lock);
730         list_del(&fb->head);
731         dev->mode_config.num_fb--;
732         mutex_unlock(&dev->mode_config.fb_lock);
733 }
734 EXPORT_SYMBOL(drm_framebuffer_cleanup);
735
736 /**
737  * drm_framebuffer_remove - remove and unreference a framebuffer object
738  * @fb: framebuffer to remove
739  *
740  * Scans all the CRTCs and planes in @dev's mode_config.  If they're
741  * using @fb, removes it, setting it to NULL. Then drops the reference to the
742  * passed-in framebuffer. Might take the modeset locks.
743  *
744  * Note that this function optimizes the cleanup away if the caller holds the
745  * last reference to the framebuffer. It is also guaranteed to not take the
746  * modeset locks in this case.
747  */
748 void drm_framebuffer_remove(struct drm_framebuffer *fb)
749 {
750         struct drm_device *dev;
751         struct drm_crtc *crtc;
752         struct drm_plane *plane;
753
754         if (!fb)
755                 return;
756
757         dev = fb->dev;
758
759         WARN_ON(!list_empty(&fb->filp_head));
760
761         /*
762          * drm ABI mandates that we remove any deleted framebuffers from active
763          * useage. But since most sane clients only remove framebuffers they no
764          * longer need, try to optimize this away.
765          *
766          * Since we're holding a reference ourselves, observing a refcount of 1
767          * means that we're the last holder and can skip it. Also, the refcount
768          * can never increase from 1 again, so we don't need any barriers or
769          * locks.
770          *
771          * Note that userspace could try to race with use and instate a new
772          * usage _after_ we've cleared all current ones. End result will be an
773          * in-use fb with fb-id == 0. Userspace is allowed to shoot its own foot
774          * in this manner.
775          */
776         if (drm_framebuffer_read_refcount(fb) > 1) {
777                 drm_modeset_lock_all(dev);
778                 /* remove from any CRTC */
779                 drm_for_each_crtc(crtc, dev) {
780                         if (crtc->primary->fb == fb) {
781                                 /* should turn off the crtc */
782                                 if (drm_crtc_force_disable(crtc))
783                                         DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc);
784                         }
785                 }
786
787                 drm_for_each_plane(plane, dev) {
788                         if (plane->fb == fb)
789                                 drm_plane_force_disable(plane);
790                 }
791                 drm_modeset_unlock_all(dev);
792         }
793
794         drm_framebuffer_unreference(fb);
795 }
796 EXPORT_SYMBOL(drm_framebuffer_remove);