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