1 /* $NetBSD: drm_dumb_buffers.c,v 1.2 2021/12/18 23:44:57 riastradh Exp $ */ 2 3 /* 4 * Copyright (c) 2006-2008 Intel Corporation 5 * Copyright (c) 2007 Dave Airlie <airlied (at) linux.ie> 6 * Copyright (c) 2008 Red Hat Inc. 7 * Copyright (c) 2016 Intel Corporation 8 * 9 * Permission to use, copy, modify, distribute, and sell this software and its 10 * documentation for any purpose is hereby granted without fee, provided that 11 * the above copyright notice appear in all copies and that both that copyright 12 * notice and this permission notice appear in supporting documentation, and 13 * that the name of the copyright holders not be used in advertising or 14 * publicity pertaining to distribution of the software without specific, 15 * written prior permission. The copyright holders make no representations 16 * about the suitability of this software for any purpose. It is provided "as 17 * is" without express or implied warranty. 18 * 19 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 20 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO 21 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR 22 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, 23 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 24 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 25 * OF THIS SOFTWARE. 26 */ 27 28 #include <sys/cdefs.h> 29 __KERNEL_RCSID(0, "$NetBSD: drm_dumb_buffers.c,v 1.2 2021/12/18 23:44:57 riastradh Exp $"); 30 31 #include <drm/drm_device.h> 32 #include <drm/drm_drv.h> 33 #include <drm/drm_gem.h> 34 #include <drm/drm_mode.h> 35 36 #include "drm_crtc_internal.h" 37 38 /** 39 * DOC: overview 40 * 41 * The KMS API doesn't standardize backing storage object creation and leaves it 42 * to driver-specific ioctls. Furthermore actually creating a buffer object even 43 * for GEM-based drivers is done through a driver-specific ioctl - GEM only has 44 * a common userspace interface for sharing and destroying objects. While not an 45 * issue for full-fledged graphics stacks that include device-specific userspace 46 * components (in libdrm for instance), this limit makes DRM-based early boot 47 * graphics unnecessarily complex. 48 * 49 * Dumb objects partly alleviate the problem by providing a standard API to 50 * create dumb buffers suitable for scanout, which can then be used to create 51 * KMS frame buffers. 52 * 53 * To support dumb objects drivers must implement the &drm_driver.dumb_create 54 * operation. &drm_driver.dumb_destroy defaults to drm_gem_dumb_destroy() if 55 * not set and &drm_driver.dumb_map_offset defaults to 56 * drm_gem_dumb_map_offset(). See 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 int drm_mode_create_dumb(struct drm_device *dev, 64 struct drm_mode_create_dumb *args, 65 struct drm_file *file_priv) 66 { 67 u32 cpp, stride, size; 68 69 if (!dev->driver->dumb_create) 70 return -ENOSYS; 71 if (!args->width || !args->height || !args->bpp) 72 return -EINVAL; 73 74 /* overflow checks for 32bit size calculations */ 75 if (args->bpp > U32_MAX - 8) 76 return -EINVAL; 77 cpp = DIV_ROUND_UP(args->bpp, 8); 78 if (cpp > U32_MAX / args->width) 79 return -EINVAL; 80 stride = cpp * args->width; 81 if (args->height > U32_MAX / stride) 82 return -EINVAL; 83 84 /* test for wrap-around */ 85 size = args->height * stride; 86 if (PAGE_ALIGN(size) == 0) 87 return -EINVAL; 88 89 /* 90 * handle, pitch and size are output parameters. Zero them out to 91 * prevent drivers from accidentally using uninitialized data. Since 92 * not all existing userspace is clearing these fields properly we 93 * cannot reject IOCTL with garbage in them. 94 */ 95 args->handle = 0; 96 args->pitch = 0; 97 args->size = 0; 98 99 return dev->driver->dumb_create(file_priv, dev, args); 100 } 101 102 int drm_mode_create_dumb_ioctl(struct drm_device *dev, 103 void *data, struct drm_file *file_priv) 104 { 105 return drm_mode_create_dumb(dev, data, file_priv); 106 } 107 108 /** 109 * drm_mode_mmap_dumb_ioctl - create an mmap offset for a dumb backing storage buffer 110 * @dev: DRM device 111 * @data: ioctl data 112 * @file_priv: DRM file info 113 * 114 * Allocate an offset in the drm device node's address space to be able to 115 * memory map a dumb buffer. 116 * 117 * Called by the user via ioctl. 118 * 119 * Returns: 120 * Zero on success, negative errno on failure. 121 */ 122 int drm_mode_mmap_dumb_ioctl(struct drm_device *dev, 123 void *data, struct drm_file *file_priv) 124 { 125 struct drm_mode_map_dumb *args = data; 126 127 if (!dev->driver->dumb_create) 128 return -ENOSYS; 129 130 if (dev->driver->dumb_map_offset) 131 return dev->driver->dumb_map_offset(file_priv, dev, 132 args->handle, 133 &args->offset); 134 else 135 return drm_gem_dumb_map_offset(file_priv, dev, args->handle, 136 &args->offset); 137 } 138 139 int drm_mode_destroy_dumb(struct drm_device *dev, u32 handle, 140 struct drm_file *file_priv) 141 { 142 if (!dev->driver->dumb_create) 143 return -ENOSYS; 144 145 if (dev->driver->dumb_destroy) 146 return dev->driver->dumb_destroy(file_priv, dev, handle); 147 else 148 return drm_gem_dumb_destroy(file_priv, dev, handle); 149 } 150 151 int drm_mode_destroy_dumb_ioctl(struct drm_device *dev, 152 void *data, struct drm_file *file_priv) 153 { 154 struct drm_mode_destroy_dumb *args = data; 155 156 return drm_mode_destroy_dumb(dev, args->handle, file_priv); 157 } 158