162306a36Sopenharmony_ci/* 262306a36Sopenharmony_ci * Copyright (c) 2006-2008 Intel Corporation 362306a36Sopenharmony_ci * Copyright (c) 2007 Dave Airlie <airlied@linux.ie> 462306a36Sopenharmony_ci * Copyright (c) 2008 Red Hat Inc. 562306a36Sopenharmony_ci * Copyright (c) 2016 Intel Corporation 662306a36Sopenharmony_ci * 762306a36Sopenharmony_ci * Permission to use, copy, modify, distribute, and sell this software and its 862306a36Sopenharmony_ci * documentation for any purpose is hereby granted without fee, provided that 962306a36Sopenharmony_ci * the above copyright notice appear in all copies and that both that copyright 1062306a36Sopenharmony_ci * notice and this permission notice appear in supporting documentation, and 1162306a36Sopenharmony_ci * that the name of the copyright holders not be used in advertising or 1262306a36Sopenharmony_ci * publicity pertaining to distribution of the software without specific, 1362306a36Sopenharmony_ci * written prior permission. The copyright holders make no representations 1462306a36Sopenharmony_ci * about the suitability of this software for any purpose. It is provided "as 1562306a36Sopenharmony_ci * is" without express or implied warranty. 1662306a36Sopenharmony_ci * 1762306a36Sopenharmony_ci * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 1862306a36Sopenharmony_ci * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO 1962306a36Sopenharmony_ci * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR 2062306a36Sopenharmony_ci * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, 2162306a36Sopenharmony_ci * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 2262306a36Sopenharmony_ci * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 2362306a36Sopenharmony_ci * OF THIS SOFTWARE. 2462306a36Sopenharmony_ci */ 2562306a36Sopenharmony_ci 2662306a36Sopenharmony_ci#include <drm/drm_device.h> 2762306a36Sopenharmony_ci#include <drm/drm_drv.h> 2862306a36Sopenharmony_ci#include <drm/drm_gem.h> 2962306a36Sopenharmony_ci#include <drm/drm_mode.h> 3062306a36Sopenharmony_ci 3162306a36Sopenharmony_ci#include "drm_crtc_internal.h" 3262306a36Sopenharmony_ci#include "drm_internal.h" 3362306a36Sopenharmony_ci 3462306a36Sopenharmony_ci/** 3562306a36Sopenharmony_ci * DOC: overview 3662306a36Sopenharmony_ci * 3762306a36Sopenharmony_ci * The KMS API doesn't standardize backing storage object creation and leaves it 3862306a36Sopenharmony_ci * to driver-specific ioctls. Furthermore actually creating a buffer object even 3962306a36Sopenharmony_ci * for GEM-based drivers is done through a driver-specific ioctl - GEM only has 4062306a36Sopenharmony_ci * a common userspace interface for sharing and destroying objects. While not an 4162306a36Sopenharmony_ci * issue for full-fledged graphics stacks that include device-specific userspace 4262306a36Sopenharmony_ci * components (in libdrm for instance), this limit makes DRM-based early boot 4362306a36Sopenharmony_ci * graphics unnecessarily complex. 4462306a36Sopenharmony_ci * 4562306a36Sopenharmony_ci * Dumb objects partly alleviate the problem by providing a standard API to 4662306a36Sopenharmony_ci * create dumb buffers suitable for scanout, which can then be used to create 4762306a36Sopenharmony_ci * KMS frame buffers. 4862306a36Sopenharmony_ci * 4962306a36Sopenharmony_ci * To support dumb objects drivers must implement the &drm_driver.dumb_create 5062306a36Sopenharmony_ci * and &drm_driver.dumb_map_offset operations (the latter defaults to 5162306a36Sopenharmony_ci * drm_gem_dumb_map_offset() if not set). Drivers that don't use GEM handles 5262306a36Sopenharmony_ci * additionally need to implement the &drm_driver.dumb_destroy operation. See 5362306a36Sopenharmony_ci * the callbacks for further details. 5462306a36Sopenharmony_ci * 5562306a36Sopenharmony_ci * Note that dumb objects may not be used for gpu acceleration, as has been 5662306a36Sopenharmony_ci * attempted on some ARM embedded platforms. Such drivers really must have 5762306a36Sopenharmony_ci * a hardware-specific ioctl to allocate suitable buffer objects. 5862306a36Sopenharmony_ci */ 5962306a36Sopenharmony_ci 6062306a36Sopenharmony_ciint drm_mode_create_dumb(struct drm_device *dev, 6162306a36Sopenharmony_ci struct drm_mode_create_dumb *args, 6262306a36Sopenharmony_ci struct drm_file *file_priv) 6362306a36Sopenharmony_ci{ 6462306a36Sopenharmony_ci u32 cpp, stride, size; 6562306a36Sopenharmony_ci 6662306a36Sopenharmony_ci if (!dev->driver->dumb_create) 6762306a36Sopenharmony_ci return -ENOSYS; 6862306a36Sopenharmony_ci if (!args->width || !args->height || !args->bpp) 6962306a36Sopenharmony_ci return -EINVAL; 7062306a36Sopenharmony_ci 7162306a36Sopenharmony_ci /* overflow checks for 32bit size calculations */ 7262306a36Sopenharmony_ci if (args->bpp > U32_MAX - 8) 7362306a36Sopenharmony_ci return -EINVAL; 7462306a36Sopenharmony_ci cpp = DIV_ROUND_UP(args->bpp, 8); 7562306a36Sopenharmony_ci if (cpp > U32_MAX / args->width) 7662306a36Sopenharmony_ci return -EINVAL; 7762306a36Sopenharmony_ci stride = cpp * args->width; 7862306a36Sopenharmony_ci if (args->height > U32_MAX / stride) 7962306a36Sopenharmony_ci return -EINVAL; 8062306a36Sopenharmony_ci 8162306a36Sopenharmony_ci /* test for wrap-around */ 8262306a36Sopenharmony_ci size = args->height * stride; 8362306a36Sopenharmony_ci if (PAGE_ALIGN(size) == 0) 8462306a36Sopenharmony_ci return -EINVAL; 8562306a36Sopenharmony_ci 8662306a36Sopenharmony_ci /* 8762306a36Sopenharmony_ci * handle, pitch and size are output parameters. Zero them out to 8862306a36Sopenharmony_ci * prevent drivers from accidentally using uninitialized data. Since 8962306a36Sopenharmony_ci * not all existing userspace is clearing these fields properly we 9062306a36Sopenharmony_ci * cannot reject IOCTL with garbage in them. 9162306a36Sopenharmony_ci */ 9262306a36Sopenharmony_ci args->handle = 0; 9362306a36Sopenharmony_ci args->pitch = 0; 9462306a36Sopenharmony_ci args->size = 0; 9562306a36Sopenharmony_ci 9662306a36Sopenharmony_ci return dev->driver->dumb_create(file_priv, dev, args); 9762306a36Sopenharmony_ci} 9862306a36Sopenharmony_ci 9962306a36Sopenharmony_ciint drm_mode_create_dumb_ioctl(struct drm_device *dev, 10062306a36Sopenharmony_ci void *data, struct drm_file *file_priv) 10162306a36Sopenharmony_ci{ 10262306a36Sopenharmony_ci return drm_mode_create_dumb(dev, data, file_priv); 10362306a36Sopenharmony_ci} 10462306a36Sopenharmony_ci 10562306a36Sopenharmony_ci/** 10662306a36Sopenharmony_ci * drm_mode_mmap_dumb_ioctl - create an mmap offset for a dumb backing storage buffer 10762306a36Sopenharmony_ci * @dev: DRM device 10862306a36Sopenharmony_ci * @data: ioctl data 10962306a36Sopenharmony_ci * @file_priv: DRM file info 11062306a36Sopenharmony_ci * 11162306a36Sopenharmony_ci * Allocate an offset in the drm device node's address space to be able to 11262306a36Sopenharmony_ci * memory map a dumb buffer. 11362306a36Sopenharmony_ci * 11462306a36Sopenharmony_ci * Called by the user via ioctl. 11562306a36Sopenharmony_ci * 11662306a36Sopenharmony_ci * Returns: 11762306a36Sopenharmony_ci * Zero on success, negative errno on failure. 11862306a36Sopenharmony_ci */ 11962306a36Sopenharmony_ciint drm_mode_mmap_dumb_ioctl(struct drm_device *dev, 12062306a36Sopenharmony_ci void *data, struct drm_file *file_priv) 12162306a36Sopenharmony_ci{ 12262306a36Sopenharmony_ci struct drm_mode_map_dumb *args = data; 12362306a36Sopenharmony_ci 12462306a36Sopenharmony_ci if (!dev->driver->dumb_create) 12562306a36Sopenharmony_ci return -ENOSYS; 12662306a36Sopenharmony_ci 12762306a36Sopenharmony_ci if (dev->driver->dumb_map_offset) 12862306a36Sopenharmony_ci return dev->driver->dumb_map_offset(file_priv, dev, 12962306a36Sopenharmony_ci args->handle, 13062306a36Sopenharmony_ci &args->offset); 13162306a36Sopenharmony_ci else 13262306a36Sopenharmony_ci return drm_gem_dumb_map_offset(file_priv, dev, args->handle, 13362306a36Sopenharmony_ci &args->offset); 13462306a36Sopenharmony_ci} 13562306a36Sopenharmony_ci 13662306a36Sopenharmony_ciint drm_mode_destroy_dumb(struct drm_device *dev, u32 handle, 13762306a36Sopenharmony_ci struct drm_file *file_priv) 13862306a36Sopenharmony_ci{ 13962306a36Sopenharmony_ci if (!dev->driver->dumb_create) 14062306a36Sopenharmony_ci return -ENOSYS; 14162306a36Sopenharmony_ci 14262306a36Sopenharmony_ci return drm_gem_handle_delete(file_priv, handle); 14362306a36Sopenharmony_ci} 14462306a36Sopenharmony_ci 14562306a36Sopenharmony_ciint drm_mode_destroy_dumb_ioctl(struct drm_device *dev, 14662306a36Sopenharmony_ci void *data, struct drm_file *file_priv) 14762306a36Sopenharmony_ci{ 14862306a36Sopenharmony_ci struct drm_mode_destroy_dumb *args = data; 14962306a36Sopenharmony_ci 15062306a36Sopenharmony_ci return drm_mode_destroy_dumb(dev, args->handle, file_priv); 15162306a36Sopenharmony_ci} 152