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