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