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

source code of linux/drivers/gpu/drm/drm_framebuffer.c