| 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 | |
| 76 | if (!hw_size_align) |
| 77 | hw_size_align = PAGE_SIZE; |
| 78 | else if (!IS_ALIGNED(hw_size_align, PAGE_SIZE)) |
| 79 | return -EINVAL; /* TODO: handle this if necessary */ |
| 80 | |
| 81 | if (check_mul_overflow(args->height, pitch, &size)) |
| 82 | return -EINVAL; |
| 83 | size = ALIGN(size, hw_size_align); |
| 84 | if (!size) |
| 85 | return -EINVAL; |
| 86 | |
| 87 | args->pitch = pitch; |
| 88 | args->size = size; |
| 89 | |
| 90 | return 0; |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * drm_mode_size_dumb - Calculates the scanline and buffer sizes for dumb buffers |
| 95 | * @dev: DRM device |
| 96 | * @args: Parameters for the dumb buffer |
| 97 | * @hw_pitch_align: Hardware scanline alignment in bytes |
| 98 | * @hw_size_align: Hardware buffer-size alignment in bytes |
| 99 | * |
| 100 | * The helper drm_mode_size_dumb() calculates the size of the buffer |
| 101 | * allocation and the scanline size for a dumb buffer. Callers have to |
| 102 | * set the buffers width, height and color mode in the argument @arg. |
| 103 | * The helper validates the correctness of the input and tests for |
| 104 | * possible overflows. If successful, it returns the dumb buffer's |
| 105 | * required scanline pitch and size in &args. |
| 106 | * |
| 107 | * The parameter @hw_pitch_align allows the driver to specifies an |
| 108 | * alignment for the scanline pitch, if the hardware requires any. The |
| 109 | * calculated pitch will be a multiple of the alignment. The parameter |
| 110 | * @hw_size_align allows to specify an alignment for buffer sizes. The |
| 111 | * provided alignment should represent requirements of the graphics |
| 112 | * hardware. drm_mode_size_dumb() handles GEM-related constraints |
| 113 | * automatically across all drivers and hardware. For example, the |
| 114 | * returned buffer size is always a multiple of PAGE_SIZE, which is |
| 115 | * required by mmap(). |
| 116 | * |
| 117 | * Returns: |
| 118 | * Zero on success, or a negative error code otherwise. |
| 119 | */ |
| 120 | int drm_mode_size_dumb(struct drm_device *dev, |
| 121 | struct drm_mode_create_dumb *args, |
| 122 | unsigned long hw_pitch_align, |
| 123 | unsigned long hw_size_align) |
| 124 | { |
| 125 | u64 pitch = 0; |
| 126 | u32 fourcc; |
| 127 | |
| 128 | /* |
| 129 | * The scanline pitch depends on the buffer width and the color |
| 130 | * format. The latter is specified as a color-mode constant for |
| 131 | * which we first have to find the corresponding color format. |
| 132 | * |
| 133 | * Different color formats can have the same color-mode constant. |
| 134 | * For example XRGB8888 and BGRX8888 both have a color mode of 32. |
| 135 | * It is possible to use different formats for dumb-buffer allocation |
| 136 | * and rendering as long as all involved formats share the same |
| 137 | * color-mode constant. |
| 138 | */ |
| 139 | fourcc = drm_driver_color_mode_format(dev, color_mode: args->bpp); |
| 140 | if (fourcc != DRM_FORMAT_INVALID) { |
| 141 | const struct drm_format_info *info = drm_format_info(format: fourcc); |
| 142 | |
| 143 | if (!info) |
| 144 | return -EINVAL; |
| 145 | pitch = drm_format_info_min_pitch(info, plane: 0, buffer_width: args->width); |
| 146 | } else if (args->bpp) { |
| 147 | /* |
| 148 | * Some userspace throws in arbitrary values for bpp and |
| 149 | * relies on the kernel to figure it out. In this case we |
| 150 | * fall back to the old method of using bpp directly. The |
| 151 | * over-commitment of memory from the rounding is acceptable |
| 152 | * for compatibility with legacy userspace. We have a number |
| 153 | * of deprecated legacy values that are explicitly supported. |
| 154 | */ |
| 155 | switch (args->bpp) { |
| 156 | default: |
| 157 | drm_warn_once(dev, |
| 158 | "Unknown color mode %u; guessing buffer size.\n" , |
| 159 | args->bpp); |
| 160 | fallthrough; |
| 161 | /* |
| 162 | * These constants represent various YUV formats supported by |
| 163 | * drm_gem_afbc_get_bpp(). |
| 164 | */ |
| 165 | case 12: // DRM_FORMAT_YUV420_8BIT |
| 166 | case 15: // DRM_FORMAT_YUV420_10BIT |
| 167 | case 30: // DRM_FORMAT_VUY101010 |
| 168 | fallthrough; |
| 169 | /* |
| 170 | * Used by Mesa and Gstreamer to allocate NV formats and others |
| 171 | * as RGB buffers. Technically, XRGB16161616F formats are RGB, |
| 172 | * but the dumb buffers are not supposed to be used for anything |
| 173 | * beyond 32 bits per pixels. |
| 174 | */ |
| 175 | case 10: // DRM_FORMAT_NV{15,20,30}, DRM_FORMAT_P010 |
| 176 | case 64: // DRM_FORMAT_{XRGB,XBGR,ARGB,ABGR}16161616F |
| 177 | pitch = args->width * DIV_ROUND_UP(args->bpp, SZ_8); |
| 178 | break; |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | if (!pitch || pitch > U32_MAX) |
| 183 | return -EINVAL; |
| 184 | |
| 185 | args->pitch = pitch; |
| 186 | |
| 187 | return drm_mode_align_dumb(args, hw_pitch_align, hw_size_align); |
| 188 | } |
| 189 | EXPORT_SYMBOL(drm_mode_size_dumb); |
| 190 | |
| 191 | int drm_mode_create_dumb(struct drm_device *dev, |
| 192 | struct drm_mode_create_dumb *args, |
| 193 | struct drm_file *file_priv) |
| 194 | { |
| 195 | u32 cpp, stride, size; |
| 196 | |
| 197 | if (!dev->driver->dumb_create) |
| 198 | return -ENOSYS; |
| 199 | if (!args->width || !args->height || !args->bpp) |
| 200 | return -EINVAL; |
| 201 | |
| 202 | /* overflow checks for 32bit size calculations */ |
| 203 | if (args->bpp > U32_MAX - 8) |
| 204 | return -EINVAL; |
| 205 | cpp = DIV_ROUND_UP(args->bpp, 8); |
| 206 | if (cpp > U32_MAX / args->width) |
| 207 | return -EINVAL; |
| 208 | stride = cpp * args->width; |
| 209 | if (args->height > U32_MAX / stride) |
| 210 | return -EINVAL; |
| 211 | |
| 212 | /* test for wrap-around */ |
| 213 | size = args->height * stride; |
| 214 | if (PAGE_ALIGN(size) == 0) |
| 215 | return -EINVAL; |
| 216 | |
| 217 | /* |
| 218 | * handle, pitch and size are output parameters. Zero them out to |
| 219 | * prevent drivers from accidentally using uninitialized data. Since |
| 220 | * not all existing userspace is clearing these fields properly we |
| 221 | * cannot reject IOCTL with garbage in them. |
| 222 | */ |
| 223 | args->handle = 0; |
| 224 | args->pitch = 0; |
| 225 | args->size = 0; |
| 226 | |
| 227 | return dev->driver->dumb_create(file_priv, dev, args); |
| 228 | } |
| 229 | |
| 230 | int drm_mode_create_dumb_ioctl(struct drm_device *dev, |
| 231 | void *data, struct drm_file *file_priv) |
| 232 | { |
| 233 | struct drm_mode_create_dumb *args = data; |
| 234 | int err; |
| 235 | |
| 236 | err = drm_mode_create_dumb(dev, args, file_priv); |
| 237 | if (err) { |
| 238 | args->handle = 0; |
| 239 | args->pitch = 0; |
| 240 | args->size = 0; |
| 241 | } |
| 242 | return err; |
| 243 | } |
| 244 | |
| 245 | static int drm_mode_mmap_dumb(struct drm_device *dev, struct drm_mode_map_dumb *args, |
| 246 | struct drm_file *file_priv) |
| 247 | { |
| 248 | if (!dev->driver->dumb_create) |
| 249 | return -ENOSYS; |
| 250 | |
| 251 | if (dev->driver->dumb_map_offset) |
| 252 | return dev->driver->dumb_map_offset(file_priv, dev, args->handle, |
| 253 | &args->offset); |
| 254 | else |
| 255 | return drm_gem_dumb_map_offset(file: file_priv, dev, handle: args->handle, |
| 256 | offset: &args->offset); |
| 257 | } |
| 258 | |
| 259 | /** |
| 260 | * drm_mode_mmap_dumb_ioctl - create an mmap offset for a dumb backing storage buffer |
| 261 | * @dev: DRM device |
| 262 | * @data: ioctl data |
| 263 | * @file_priv: DRM file info |
| 264 | * |
| 265 | * Allocate an offset in the drm device node's address space to be able to |
| 266 | * memory map a dumb buffer. |
| 267 | * |
| 268 | * Called by the user via ioctl. |
| 269 | * |
| 270 | * Returns: |
| 271 | * Zero on success, negative errno on failure. |
| 272 | */ |
| 273 | int drm_mode_mmap_dumb_ioctl(struct drm_device *dev, |
| 274 | void *data, struct drm_file *file_priv) |
| 275 | { |
| 276 | struct drm_mode_map_dumb *args = data; |
| 277 | int err; |
| 278 | |
| 279 | err = drm_mode_mmap_dumb(dev, args, file_priv); |
| 280 | if (err) |
| 281 | args->offset = 0; |
| 282 | return err; |
| 283 | } |
| 284 | |
| 285 | int drm_mode_destroy_dumb(struct drm_device *dev, u32 handle, |
| 286 | struct drm_file *file_priv) |
| 287 | { |
| 288 | if (!dev->driver->dumb_create) |
| 289 | return -ENOSYS; |
| 290 | |
| 291 | return drm_gem_handle_delete(filp: file_priv, handle); |
| 292 | } |
| 293 | |
| 294 | int drm_mode_destroy_dumb_ioctl(struct drm_device *dev, |
| 295 | void *data, struct drm_file *file_priv) |
| 296 | { |
| 297 | struct drm_mode_destroy_dumb *args = data; |
| 298 | |
| 299 | return drm_mode_destroy_dumb(dev, handle: args->handle, file_priv); |
| 300 | } |
| 301 | |