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