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