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> |
| 27 | |
| 28 | #include "drm_crtc_internal.h" |
| 29 | |
| 30 | /** |
| 31 | * drm_mode_create_dumb_ioctl - create a dumb backing storage buffer |
| 32 | * @dev: DRM device |
| 33 | * @data: ioctl data |
| 34 | * @file_priv: DRM file info |
| 35 | * |
| 36 | * This creates a new dumb buffer in the driver's backing storage manager (GEM, |
| 37 | * TTM or something else entirely) and returns the resulting buffer handle. This |
| 38 | * handle can then be wrapped up into a framebuffer modeset object. |
| 39 | * |
| 40 | * Note that userspace is not allowed to use such objects for render |
| 41 | * acceleration - drivers must create their own private ioctls for such a use |
| 42 | * case. |
| 43 | * |
| 44 | * Called by the user via ioctl. |
| 45 | * |
| 46 | * Returns: |
| 47 | * Zero on success, negative errno on failure. |
| 48 | */ |
| 49 | int drm_mode_create_dumb_ioctl(struct drm_device *dev, |
| 50 | void *data, struct drm_file *file_priv) |
| 51 | { |
| 52 | struct drm_mode_create_dumb *args = data; |
| 53 | u32 cpp, stride, size; |
| 54 | |
| 55 | if (!dev->driver->dumb_create) |
| 56 | return -ENOSYS; |
| 57 | if (!args->width || !args->height || !args->bpp) |
| 58 | return -EINVAL; |
| 59 | |
| 60 | /* overflow checks for 32bit size calculations */ |
| 61 | /* NOTE: DIV_ROUND_UP() can overflow */ |
| 62 | cpp = DIV_ROUND_UP(args->bpp, 8); |
| 63 | if (!cpp || cpp > 0xffffffffU / args->width) |
| 64 | return -EINVAL; |
| 65 | stride = cpp * args->width; |
| 66 | if (args->height > 0xffffffffU / stride) |
| 67 | return -EINVAL; |
| 68 | |
| 69 | /* test for wrap-around */ |
| 70 | size = args->height * stride; |
| 71 | if (PAGE_ALIGN(size) == 0) |
| 72 | return -EINVAL; |
| 73 | |
| 74 | /* |
| 75 | * handle, pitch and size are output parameters. Zero them out to |
| 76 | * prevent drivers from accidentally using uninitialized data. Since |
| 77 | * not all existing userspace is clearing these fields properly we |
| 78 | * cannot reject IOCTL with garbage in them. |
| 79 | */ |
| 80 | args->handle = 0; |
| 81 | args->pitch = 0; |
| 82 | args->size = 0; |
| 83 | |
| 84 | return dev->driver->dumb_create(file_priv, dev, args); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * drm_mode_mmap_dumb_ioctl - create an mmap offset for a dumb backing storage buffer |
| 89 | * @dev: DRM device |
| 90 | * @data: ioctl data |
| 91 | * @file_priv: DRM file info |
| 92 | * |
| 93 | * Allocate an offset in the drm device node's address space to be able to |
| 94 | * memory map a dumb buffer. |
| 95 | * |
| 96 | * Called by the user via ioctl. |
| 97 | * |
| 98 | * Returns: |
| 99 | * Zero on success, negative errno on failure. |
| 100 | */ |
| 101 | int drm_mode_mmap_dumb_ioctl(struct drm_device *dev, |
| 102 | void *data, struct drm_file *file_priv) |
| 103 | { |
| 104 | struct drm_mode_map_dumb *args = data; |
| 105 | |
| 106 | /* call driver ioctl to get mmap offset */ |
| 107 | if (!dev->driver->dumb_map_offset) |
| 108 | return -ENOSYS; |
| 109 | |
| 110 | return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset); |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * drm_mode_destroy_dumb_ioctl - destroy a dumb backing strage buffer |
| 115 | * @dev: DRM device |
| 116 | * @data: ioctl data |
| 117 | * @file_priv: DRM file info |
| 118 | * |
| 119 | * This destroys the userspace handle for the given dumb backing storage buffer. |
| 120 | * Since buffer objects must be reference counted in the kernel a buffer object |
| 121 | * won't be immediately freed if a framebuffer modeset object still uses it. |
| 122 | * |
| 123 | * Called by the user via ioctl. |
| 124 | * |
| 125 | * Returns: |
| 126 | * Zero on success, negative errno on failure. |
| 127 | */ |
| 128 | int drm_mode_destroy_dumb_ioctl(struct drm_device *dev, |
| 129 | void *data, struct drm_file *file_priv) |
| 130 | { |
| 131 | struct drm_mode_destroy_dumb *args = data; |
| 132 | |
| 133 | if (!dev->driver->dumb_destroy) |
| 134 | return -ENOSYS; |
| 135 | |
| 136 | return dev->driver->dumb_destroy(file_priv, dev, args->handle); |
| 137 | } |
| 138 | |