xref: /linux/drivers/gpu/drm/drm_dumb_buffers.c (revision 65b09bfa8aa7ebe087093b591525385efb2d58b0)
1 /*
2  * Copyright (c) 2006-2008 Intel Corporation
3  * Copyright (c) 2007 Dave Airlie <airlied@linux.ie>
4  * Copyright (c) 2008 Red Hat Inc.
5  * Copyright (c) 2016 Intel Corporation
6  *
7  * Permission to use, copy, modify, distribute, and sell this software and its
8  * documentation for any purpose is hereby granted without fee, provided that
9  * the above copyright notice appear in all copies and that both that copyright
10  * notice and this permission notice appear in supporting documentation, and
11  * that the name of the copyright holders not be used in advertising or
12  * publicity pertaining to distribution of the software without specific,
13  * written prior permission.  The copyright holders make no representations
14  * about the suitability of this software for any purpose.  It is provided "as
15  * is" without express or implied warranty.
16  *
17  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
18  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
19  * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
20  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
21  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
22  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
23  * OF THIS SOFTWARE.
24  */
25 
26 #include <drm/drm_device.h>
27 #include <drm/drm_drv.h>
28 #include <drm/drm_dumb_buffers.h>
29 #include <drm/drm_fourcc.h>
30 #include <drm/drm_gem.h>
31 #include <drm/drm_mode.h>
32 #include <drm/drm_print.h>
33 
34 #include "drm_crtc_internal.h"
35 #include "drm_internal.h"
36 
37 /**
38  * DOC: overview
39  *
40  * The KMS API doesn't standardize backing storage object creation and leaves it
41  * to driver-specific ioctls. Furthermore actually creating a buffer object even
42  * for GEM-based drivers is done through a driver-specific ioctl - GEM only has
43  * a common userspace interface for sharing and destroying objects. While not an
44  * issue for full-fledged graphics stacks that include device-specific userspace
45  * components (in libdrm for instance), this limit makes DRM-based early boot
46  * graphics unnecessarily complex.
47  *
48  * Dumb objects partly alleviate the problem by providing a standard API to
49  * create dumb buffers suitable for scanout, which can then be used to create
50  * KMS frame buffers.
51  *
52  * To support dumb objects drivers must implement the &drm_driver.dumb_create
53  * and &drm_driver.dumb_map_offset operations (the latter defaults to
54  * drm_gem_dumb_map_offset() if not set). Drivers that don't use GEM handles
55  * additionally need to implement the &drm_driver.dumb_destroy operation. See
56  * the callbacks for further details.
57  *
58  * Note that dumb objects may not be used for gpu acceleration, as has been
59  * attempted on some ARM embedded platforms. Such drivers really must have
60  * a hardware-specific ioctl to allocate suitable buffer objects.
61  */
62 
63 static int drm_mode_align_dumb(struct drm_mode_create_dumb *args,
64 			       unsigned long hw_pitch_align,
65 			       unsigned long hw_size_align)
66 {
67 	u32 pitch = args->pitch;
68 	u32 size;
69 
70 	if (!pitch)
71 		return -EINVAL;
72 
73 	if (hw_pitch_align) {
74 		pitch = roundup(pitch, hw_pitch_align);
75 		if (pitch < hw_pitch_align)
76 			return -EINVAL;
77 	}
78 
79 	if (!hw_size_align)
80 		hw_size_align = PAGE_SIZE;
81 	else if (!IS_ALIGNED(hw_size_align, PAGE_SIZE))
82 		return -EINVAL; /* TODO: handle this if necessary */
83 
84 	if (check_mul_overflow(args->height, pitch, &size))
85 		return -EINVAL;
86 	size = roundup(size, hw_size_align);
87 	if (!size)
88 		return -EINVAL;
89 
90 	args->pitch = pitch;
91 	args->size = size;
92 
93 	return 0;
94 }
95 
96 /**
97  * drm_mode_size_dumb - Calculates the scanline and buffer sizes for dumb buffers
98  * @dev: DRM device
99  * @args: Parameters for the dumb buffer
100  * @hw_pitch_align: Hardware scanline alignment in bytes
101  * @hw_size_align: Hardware buffer-size alignment in bytes
102  *
103  * The helper drm_mode_size_dumb() calculates the size of the buffer
104  * allocation and the scanline size for a dumb buffer. Callers have to
105  * set the buffers width, height and color mode in the argument @arg.
106  * The helper validates the correctness of the input and tests for
107  * possible overflows. If successful, it returns the dumb buffer's
108  * required scanline pitch and size in &args.
109  *
110  * The parameter @hw_pitch_align allows the driver to specifies an
111  * alignment for the scanline pitch, if the hardware requires any. The
112  * calculated pitch will be a multiple of the alignment. The parameter
113  * @hw_size_align allows to specify an alignment for buffer sizes. The
114  * provided alignment should represent requirements of the graphics
115  * hardware. drm_mode_size_dumb() handles GEM-related constraints
116  * automatically across all drivers and hardware. For example, the
117  * returned buffer size is always a multiple of PAGE_SIZE, which is
118  * required by mmap().
119  *
120  * Returns:
121  * Zero on success, or a negative error code otherwise.
122  */
123 int drm_mode_size_dumb(struct drm_device *dev,
124 		       struct drm_mode_create_dumb *args,
125 		       unsigned long hw_pitch_align,
126 		       unsigned long hw_size_align)
127 {
128 	u64 pitch = 0;
129 	u32 fourcc;
130 
131 	/*
132 	 * The scanline pitch depends on the buffer width and the color
133 	 * format. The latter is specified as a color-mode constant for
134 	 * which we first have to find the corresponding color format.
135 	 *
136 	 * Different color formats can have the same color-mode constant.
137 	 * For example XRGB8888 and BGRX8888 both have a color mode of 32.
138 	 * It is possible to use different formats for dumb-buffer allocation
139 	 * and rendering as long as all involved formats share the same
140 	 * color-mode constant.
141 	 */
142 	fourcc = drm_driver_color_mode_format(dev, args->bpp);
143 	if (fourcc != DRM_FORMAT_INVALID) {
144 		const struct drm_format_info *info = drm_format_info(fourcc);
145 
146 		if (!info)
147 			return -EINVAL;
148 		pitch = drm_format_info_min_pitch(info, 0, args->width);
149 	} else if (args->bpp) {
150 		/*
151 		 * Some userspace throws in arbitrary values for bpp and
152 		 * relies on the kernel to figure it out. In this case we
153 		 * fall back to the old method of using bpp directly. The
154 		 * over-commitment of memory from the rounding is acceptable
155 		 * for compatibility with legacy userspace. We have a number
156 		 * of deprecated legacy values that are explicitly supported.
157 		 */
158 		switch (args->bpp) {
159 		default:
160 			drm_warn_once(dev,
161 				      "Unknown color mode %u; guessing buffer size.\n",
162 				      args->bpp);
163 			fallthrough;
164 		/*
165 		 * These constants represent various YUV formats supported by
166 		 * drm_gem_afbc_get_bpp().
167 		 */
168 		case 12: // DRM_FORMAT_YUV420_8BIT
169 		case 15: // DRM_FORMAT_YUV420_10BIT
170 		case 30: // DRM_FORMAT_VUY101010
171 			fallthrough;
172 		/*
173 		 * Used by Mesa and Gstreamer to allocate NV formats and others
174 		 * as RGB buffers. Technically, XRGB16161616F formats are RGB,
175 		 * but the dumb buffers are not supposed to be used for anything
176 		 * beyond 32 bits per pixels.
177 		 */
178 		case 10: // DRM_FORMAT_NV{15,20,30}, DRM_FORMAT_P010
179 		case 64: // DRM_FORMAT_{XRGB,XBGR,ARGB,ABGR}16161616F
180 			pitch = args->width * DIV_ROUND_UP(args->bpp, SZ_8);
181 			break;
182 		}
183 	}
184 
185 	if (!pitch || pitch > U32_MAX)
186 		return -EINVAL;
187 
188 	args->pitch = pitch;
189 
190 	return drm_mode_align_dumb(args, hw_pitch_align, hw_size_align);
191 }
192 EXPORT_SYMBOL(drm_mode_size_dumb);
193 
194 int drm_mode_create_dumb(struct drm_device *dev,
195 			 struct drm_mode_create_dumb *args,
196 			 struct drm_file *file_priv)
197 {
198 	u32 cpp, stride, size;
199 
200 	if (!dev->driver->dumb_create)
201 		return -ENOSYS;
202 	if (!args->width || !args->height || !args->bpp)
203 		return -EINVAL;
204 
205 	/* Reject unreasonable inputs early.  Dumb buffers are for software
206 	 * rendering; nothing legitimate needs more than 8192x8192 at 32bpp.
207 	 * This prevents overflows in downstream alignment helpers.
208 	 */
209 	if (args->width >= 8192 || args->height >= 8192 || args->bpp > 32)
210 		return -EINVAL;
211 
212 	/* overflow checks for 32bit size calculations */
213 	if (args->bpp > U32_MAX - 8)
214 		return -EINVAL;
215 	cpp = DIV_ROUND_UP(args->bpp, 8);
216 	if (cpp > U32_MAX / args->width)
217 		return -EINVAL;
218 	stride = cpp * args->width;
219 	if (args->height > U32_MAX / stride)
220 		return -EINVAL;
221 
222 	/* test for wrap-around */
223 	size = args->height * stride;
224 	if (PAGE_ALIGN(size) == 0)
225 		return -EINVAL;
226 
227 	/*
228 	 * handle, pitch and size are output parameters. Zero them out to
229 	 * prevent drivers from accidentally using uninitialized data. Since
230 	 * not all existing userspace is clearing these fields properly we
231 	 * cannot reject IOCTL with garbage in them.
232 	 */
233 	args->handle = 0;
234 	args->pitch = 0;
235 	args->size = 0;
236 
237 	return dev->driver->dumb_create(file_priv, dev, args);
238 }
239 
240 int drm_mode_create_dumb_ioctl(struct drm_device *dev,
241 			       void *data, struct drm_file *file_priv)
242 {
243 	struct drm_mode_create_dumb *args = data;
244 	int err;
245 
246 	err = drm_mode_create_dumb(dev, args, file_priv);
247 	if (err) {
248 		args->handle = 0;
249 		args->pitch = 0;
250 		args->size = 0;
251 	}
252 	return err;
253 }
254 
255 static int drm_mode_mmap_dumb(struct drm_device *dev, struct drm_mode_map_dumb *args,
256 			      struct drm_file *file_priv)
257 {
258 	if (!dev->driver->dumb_create)
259 		return -ENOSYS;
260 
261 	if (dev->driver->dumb_map_offset)
262 		return dev->driver->dumb_map_offset(file_priv, dev, args->handle,
263 						    &args->offset);
264 	else
265 		return drm_gem_dumb_map_offset(file_priv, dev, args->handle,
266 					       &args->offset);
267 }
268 
269 /**
270  * drm_mode_mmap_dumb_ioctl - create an mmap offset for a dumb backing storage buffer
271  * @dev: DRM device
272  * @data: ioctl data
273  * @file_priv: DRM file info
274  *
275  * Allocate an offset in the drm device node's address space to be able to
276  * memory map a dumb buffer.
277  *
278  * Called by the user via ioctl.
279  *
280  * Returns:
281  * Zero on success, negative errno on failure.
282  */
283 int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
284 			     void *data, struct drm_file *file_priv)
285 {
286 	struct drm_mode_map_dumb *args = data;
287 	int err;
288 
289 	err = drm_mode_mmap_dumb(dev, args, file_priv);
290 	if (err)
291 		args->offset = 0;
292 	return err;
293 }
294 
295 int drm_mode_destroy_dumb(struct drm_device *dev, u32 handle,
296 			  struct drm_file *file_priv)
297 {
298 	if (!dev->driver->dumb_create)
299 		return -ENOSYS;
300 
301 	return drm_gem_handle_delete(file_priv, handle);
302 }
303 
304 int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
305 				void *data, struct drm_file *file_priv)
306 {
307 	struct drm_mode_destroy_dumb *args = data;
308 
309 	return drm_mode_destroy_dumb(dev, args->handle, file_priv);
310 }
311