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