Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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