Daniel Vetter | 1a02ea4 | 2016-11-14 12:58:16 +0100 | [diff] [blame] | 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/drmP.h> |
Noralf Trønnes | 0be8d63 | 2017-07-23 21:16:18 +0200 | [diff] [blame] | 27 | #include <drm/drm_gem.h> |
Daniel Vetter | 1a02ea4 | 2016-11-14 12:58:16 +0100 | [diff] [blame] | 28 | |
| 29 | #include "drm_crtc_internal.h" |
| 30 | |
| 31 | /** |
Daniel Vetter | 4f93624 | 2016-11-14 12:58:21 +0100 | [diff] [blame] | 32 | * DOC: overview |
Daniel Vetter | 1a02ea4 | 2016-11-14 12:58:16 +0100 | [diff] [blame] | 33 | * |
Daniel Vetter | 4f93624 | 2016-11-14 12:58:21 +0100 | [diff] [blame] | 34 | * The KMS API doesn't standardize backing storage object creation and leaves it |
| 35 | * to driver-specific ioctls. Furthermore actually creating a buffer object even |
| 36 | * for GEM-based drivers is done through a driver-specific ioctl - GEM only has |
| 37 | * a common userspace interface for sharing and destroying objects. While not an |
| 38 | * issue for full-fledged graphics stacks that include device-specific userspace |
| 39 | * components (in libdrm for instance), this limit makes DRM-based early boot |
| 40 | * graphics unnecessarily complex. |
Daniel Vetter | 1a02ea4 | 2016-11-14 12:58:16 +0100 | [diff] [blame] | 41 | * |
Daniel Vetter | 4f93624 | 2016-11-14 12:58:21 +0100 | [diff] [blame] | 42 | * Dumb objects partly alleviate the problem by providing a standard API to |
| 43 | * create dumb buffers suitable for scanout, which can then be used to create |
| 44 | * KMS frame buffers. |
Daniel Vetter | 1a02ea4 | 2016-11-14 12:58:16 +0100 | [diff] [blame] | 45 | * |
Noralf Trønnes | 0be8d63 | 2017-07-23 21:16:18 +0200 | [diff] [blame] | 46 | * To support dumb objects drivers must implement the &drm_driver.dumb_create |
| 47 | * operation. &drm_driver.dumb_destroy defaults to drm_gem_dumb_destroy() if |
| 48 | * not set and &drm_driver.dumb_map_offset defaults to |
| 49 | * drm_gem_dumb_map_offset(). See the callbacks for further details. |
Daniel Vetter | 1a02ea4 | 2016-11-14 12:58:16 +0100 | [diff] [blame] | 50 | * |
Daniel Vetter | 4f93624 | 2016-11-14 12:58:21 +0100 | [diff] [blame] | 51 | * Note that dumb objects may not be used for gpu acceleration, as has been |
| 52 | * attempted on some ARM embedded platforms. Such drivers really must have |
| 53 | * a hardware-specific ioctl to allocate suitable buffer objects. |
Daniel Vetter | 1a02ea4 | 2016-11-14 12:58:16 +0100 | [diff] [blame] | 54 | */ |
Daniel Vetter | 4f93624 | 2016-11-14 12:58:21 +0100 | [diff] [blame] | 55 | |
Noralf Trønnes | d30827c | 2018-06-18 16:17:30 +0200 | [diff] [blame] | 56 | int drm_mode_create_dumb(struct drm_device *dev, |
| 57 | struct drm_mode_create_dumb *args, |
| 58 | struct drm_file *file_priv) |
Daniel Vetter | 1a02ea4 | 2016-11-14 12:58:16 +0100 | [diff] [blame] | 59 | { |
Daniel Vetter | 1a02ea4 | 2016-11-14 12:58:16 +0100 | [diff] [blame] | 60 | u32 cpp, stride, size; |
| 61 | |
| 62 | if (!dev->driver->dumb_create) |
| 63 | return -ENOSYS; |
| 64 | if (!args->width || !args->height || !args->bpp) |
| 65 | return -EINVAL; |
| 66 | |
| 67 | /* overflow checks for 32bit size calculations */ |
Dan Carpenter | 2b62072 | 2018-05-16 17:00:26 +0300 | [diff] [blame] | 68 | if (args->bpp > U32_MAX - 8) |
| 69 | return -EINVAL; |
Daniel Vetter | 1a02ea4 | 2016-11-14 12:58:16 +0100 | [diff] [blame] | 70 | cpp = DIV_ROUND_UP(args->bpp, 8); |
Dan Carpenter | 2b62072 | 2018-05-16 17:00:26 +0300 | [diff] [blame] | 71 | if (cpp > U32_MAX / args->width) |
Daniel Vetter | 1a02ea4 | 2016-11-14 12:58:16 +0100 | [diff] [blame] | 72 | return -EINVAL; |
| 73 | stride = cpp * args->width; |
Dan Carpenter | 2b62072 | 2018-05-16 17:00:26 +0300 | [diff] [blame] | 74 | if (args->height > U32_MAX / stride) |
Daniel Vetter | 1a02ea4 | 2016-11-14 12:58:16 +0100 | [diff] [blame] | 75 | return -EINVAL; |
| 76 | |
| 77 | /* test for wrap-around */ |
| 78 | size = args->height * stride; |
| 79 | if (PAGE_ALIGN(size) == 0) |
| 80 | return -EINVAL; |
| 81 | |
| 82 | /* |
| 83 | * handle, pitch and size are output parameters. Zero them out to |
| 84 | * prevent drivers from accidentally using uninitialized data. Since |
| 85 | * not all existing userspace is clearing these fields properly we |
| 86 | * cannot reject IOCTL with garbage in them. |
| 87 | */ |
| 88 | args->handle = 0; |
| 89 | args->pitch = 0; |
| 90 | args->size = 0; |
| 91 | |
| 92 | return dev->driver->dumb_create(file_priv, dev, args); |
| 93 | } |
| 94 | |
Noralf Trønnes | d30827c | 2018-06-18 16:17:30 +0200 | [diff] [blame] | 95 | int drm_mode_create_dumb_ioctl(struct drm_device *dev, |
| 96 | void *data, struct drm_file *file_priv) |
| 97 | { |
| 98 | return drm_mode_create_dumb(dev, data, file_priv); |
| 99 | } |
| 100 | |
Daniel Vetter | 1a02ea4 | 2016-11-14 12:58:16 +0100 | [diff] [blame] | 101 | /** |
| 102 | * drm_mode_mmap_dumb_ioctl - create an mmap offset for a dumb backing storage buffer |
| 103 | * @dev: DRM device |
| 104 | * @data: ioctl data |
| 105 | * @file_priv: DRM file info |
| 106 | * |
| 107 | * Allocate an offset in the drm device node's address space to be able to |
| 108 | * memory map a dumb buffer. |
| 109 | * |
| 110 | * Called by the user via ioctl. |
| 111 | * |
| 112 | * Returns: |
| 113 | * Zero on success, negative errno on failure. |
| 114 | */ |
| 115 | int drm_mode_mmap_dumb_ioctl(struct drm_device *dev, |
| 116 | void *data, struct drm_file *file_priv) |
| 117 | { |
| 118 | struct drm_mode_map_dumb *args = data; |
| 119 | |
Noralf Trønnes | 0be8d63 | 2017-07-23 21:16:18 +0200 | [diff] [blame] | 120 | if (!dev->driver->dumb_create) |
Daniel Vetter | 1a02ea4 | 2016-11-14 12:58:16 +0100 | [diff] [blame] | 121 | return -ENOSYS; |
| 122 | |
Noralf Trønnes | 0be8d63 | 2017-07-23 21:16:18 +0200 | [diff] [blame] | 123 | if (dev->driver->dumb_map_offset) |
| 124 | return dev->driver->dumb_map_offset(file_priv, dev, |
| 125 | args->handle, |
| 126 | &args->offset); |
| 127 | else |
| 128 | return drm_gem_dumb_map_offset(file_priv, dev, args->handle, |
| 129 | &args->offset); |
Daniel Vetter | 1a02ea4 | 2016-11-14 12:58:16 +0100 | [diff] [blame] | 130 | } |
| 131 | |
Noralf Trønnes | d30827c | 2018-06-18 16:17:30 +0200 | [diff] [blame] | 132 | int drm_mode_destroy_dumb(struct drm_device *dev, u32 handle, |
| 133 | struct drm_file *file_priv) |
| 134 | { |
| 135 | if (!dev->driver->dumb_create) |
| 136 | return -ENOSYS; |
| 137 | |
| 138 | if (dev->driver->dumb_destroy) |
| 139 | return dev->driver->dumb_destroy(file_priv, dev, handle); |
| 140 | else |
| 141 | return drm_gem_dumb_destroy(file_priv, dev, handle); |
| 142 | } |
| 143 | |
Daniel Vetter | 1a02ea4 | 2016-11-14 12:58:16 +0100 | [diff] [blame] | 144 | int drm_mode_destroy_dumb_ioctl(struct drm_device *dev, |
| 145 | void *data, struct drm_file *file_priv) |
| 146 | { |
| 147 | struct drm_mode_destroy_dumb *args = data; |
| 148 | |
Noralf Trønnes | d30827c | 2018-06-18 16:17:30 +0200 | [diff] [blame] | 149 | return drm_mode_destroy_dumb(dev, args->handle, file_priv); |
Daniel Vetter | 1a02ea4 | 2016-11-14 12:58:16 +0100 | [diff] [blame] | 150 | } |