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