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