18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * Copyright (c) 2006-2008 Intel Corporation
38c2ecf20Sopenharmony_ci * Copyright (c) 2007 Dave Airlie <airlied@linux.ie>
48c2ecf20Sopenharmony_ci * Copyright (c) 2008 Red Hat Inc.
58c2ecf20Sopenharmony_ci * Copyright (c) 2016 Intel Corporation
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * Permission to use, copy, modify, distribute, and sell this software and its
88c2ecf20Sopenharmony_ci * documentation for any purpose is hereby granted without fee, provided that
98c2ecf20Sopenharmony_ci * the above copyright notice appear in all copies and that both that copyright
108c2ecf20Sopenharmony_ci * notice and this permission notice appear in supporting documentation, and
118c2ecf20Sopenharmony_ci * that the name of the copyright holders not be used in advertising or
128c2ecf20Sopenharmony_ci * publicity pertaining to distribution of the software without specific,
138c2ecf20Sopenharmony_ci * written prior permission.  The copyright holders make no representations
148c2ecf20Sopenharmony_ci * about the suitability of this software for any purpose.  It is provided "as
158c2ecf20Sopenharmony_ci * is" without express or implied warranty.
168c2ecf20Sopenharmony_ci *
178c2ecf20Sopenharmony_ci * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
188c2ecf20Sopenharmony_ci * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
198c2ecf20Sopenharmony_ci * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
208c2ecf20Sopenharmony_ci * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
218c2ecf20Sopenharmony_ci * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
228c2ecf20Sopenharmony_ci * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
238c2ecf20Sopenharmony_ci * OF THIS SOFTWARE.
248c2ecf20Sopenharmony_ci */
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ci#include <drm/drm_device.h>
278c2ecf20Sopenharmony_ci#include <drm/drm_drv.h>
288c2ecf20Sopenharmony_ci#include <drm/drm_gem.h>
298c2ecf20Sopenharmony_ci#include <drm/drm_mode.h>
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci#include "drm_crtc_internal.h"
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ci/**
348c2ecf20Sopenharmony_ci * DOC: overview
358c2ecf20Sopenharmony_ci *
368c2ecf20Sopenharmony_ci * The KMS API doesn't standardize backing storage object creation and leaves it
378c2ecf20Sopenharmony_ci * to driver-specific ioctls. Furthermore actually creating a buffer object even
388c2ecf20Sopenharmony_ci * for GEM-based drivers is done through a driver-specific ioctl - GEM only has
398c2ecf20Sopenharmony_ci * a common userspace interface for sharing and destroying objects. While not an
408c2ecf20Sopenharmony_ci * issue for full-fledged graphics stacks that include device-specific userspace
418c2ecf20Sopenharmony_ci * components (in libdrm for instance), this limit makes DRM-based early boot
428c2ecf20Sopenharmony_ci * graphics unnecessarily complex.
438c2ecf20Sopenharmony_ci *
448c2ecf20Sopenharmony_ci * Dumb objects partly alleviate the problem by providing a standard API to
458c2ecf20Sopenharmony_ci * create dumb buffers suitable for scanout, which can then be used to create
468c2ecf20Sopenharmony_ci * KMS frame buffers.
478c2ecf20Sopenharmony_ci *
488c2ecf20Sopenharmony_ci * To support dumb objects drivers must implement the &drm_driver.dumb_create
498c2ecf20Sopenharmony_ci * operation. &drm_driver.dumb_destroy defaults to drm_gem_dumb_destroy() if
508c2ecf20Sopenharmony_ci * not set and &drm_driver.dumb_map_offset defaults to
518c2ecf20Sopenharmony_ci * drm_gem_dumb_map_offset(). See the callbacks for further details.
528c2ecf20Sopenharmony_ci *
538c2ecf20Sopenharmony_ci * Note that dumb objects may not be used for gpu acceleration, as has been
548c2ecf20Sopenharmony_ci * attempted on some ARM embedded platforms. Such drivers really must have
558c2ecf20Sopenharmony_ci * a hardware-specific ioctl to allocate suitable buffer objects.
568c2ecf20Sopenharmony_ci */
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ciint drm_mode_create_dumb(struct drm_device *dev,
598c2ecf20Sopenharmony_ci			 struct drm_mode_create_dumb *args,
608c2ecf20Sopenharmony_ci			 struct drm_file *file_priv)
618c2ecf20Sopenharmony_ci{
628c2ecf20Sopenharmony_ci	u32 cpp, stride, size;
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci	if (!dev->driver->dumb_create)
658c2ecf20Sopenharmony_ci		return -ENOSYS;
668c2ecf20Sopenharmony_ci	if (!args->width || !args->height || !args->bpp)
678c2ecf20Sopenharmony_ci		return -EINVAL;
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ci	/* overflow checks for 32bit size calculations */
708c2ecf20Sopenharmony_ci	if (args->bpp > U32_MAX - 8)
718c2ecf20Sopenharmony_ci		return -EINVAL;
728c2ecf20Sopenharmony_ci	cpp = DIV_ROUND_UP(args->bpp, 8);
738c2ecf20Sopenharmony_ci	if (cpp > U32_MAX / args->width)
748c2ecf20Sopenharmony_ci		return -EINVAL;
758c2ecf20Sopenharmony_ci	stride = cpp * args->width;
768c2ecf20Sopenharmony_ci	if (args->height > U32_MAX / stride)
778c2ecf20Sopenharmony_ci		return -EINVAL;
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ci	/* test for wrap-around */
808c2ecf20Sopenharmony_ci	size = args->height * stride;
818c2ecf20Sopenharmony_ci	if (PAGE_ALIGN(size) == 0)
828c2ecf20Sopenharmony_ci		return -EINVAL;
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci	/*
858c2ecf20Sopenharmony_ci	 * handle, pitch and size are output parameters. Zero them out to
868c2ecf20Sopenharmony_ci	 * prevent drivers from accidentally using uninitialized data. Since
878c2ecf20Sopenharmony_ci	 * not all existing userspace is clearing these fields properly we
888c2ecf20Sopenharmony_ci	 * cannot reject IOCTL with garbage in them.
898c2ecf20Sopenharmony_ci	 */
908c2ecf20Sopenharmony_ci	args->handle = 0;
918c2ecf20Sopenharmony_ci	args->pitch = 0;
928c2ecf20Sopenharmony_ci	args->size = 0;
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ci	return dev->driver->dumb_create(file_priv, dev, args);
958c2ecf20Sopenharmony_ci}
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_ciint drm_mode_create_dumb_ioctl(struct drm_device *dev,
988c2ecf20Sopenharmony_ci			       void *data, struct drm_file *file_priv)
998c2ecf20Sopenharmony_ci{
1008c2ecf20Sopenharmony_ci	return drm_mode_create_dumb(dev, data, file_priv);
1018c2ecf20Sopenharmony_ci}
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ci/**
1048c2ecf20Sopenharmony_ci * drm_mode_mmap_dumb_ioctl - create an mmap offset for a dumb backing storage buffer
1058c2ecf20Sopenharmony_ci * @dev: DRM device
1068c2ecf20Sopenharmony_ci * @data: ioctl data
1078c2ecf20Sopenharmony_ci * @file_priv: DRM file info
1088c2ecf20Sopenharmony_ci *
1098c2ecf20Sopenharmony_ci * Allocate an offset in the drm device node's address space to be able to
1108c2ecf20Sopenharmony_ci * memory map a dumb buffer.
1118c2ecf20Sopenharmony_ci *
1128c2ecf20Sopenharmony_ci * Called by the user via ioctl.
1138c2ecf20Sopenharmony_ci *
1148c2ecf20Sopenharmony_ci * Returns:
1158c2ecf20Sopenharmony_ci * Zero on success, negative errno on failure.
1168c2ecf20Sopenharmony_ci */
1178c2ecf20Sopenharmony_ciint drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
1188c2ecf20Sopenharmony_ci			     void *data, struct drm_file *file_priv)
1198c2ecf20Sopenharmony_ci{
1208c2ecf20Sopenharmony_ci	struct drm_mode_map_dumb *args = data;
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_ci	if (!dev->driver->dumb_create)
1238c2ecf20Sopenharmony_ci		return -ENOSYS;
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_ci	if (dev->driver->dumb_map_offset)
1268c2ecf20Sopenharmony_ci		return dev->driver->dumb_map_offset(file_priv, dev,
1278c2ecf20Sopenharmony_ci						    args->handle,
1288c2ecf20Sopenharmony_ci						    &args->offset);
1298c2ecf20Sopenharmony_ci	else
1308c2ecf20Sopenharmony_ci		return drm_gem_dumb_map_offset(file_priv, dev, args->handle,
1318c2ecf20Sopenharmony_ci					       &args->offset);
1328c2ecf20Sopenharmony_ci}
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ciint drm_mode_destroy_dumb(struct drm_device *dev, u32 handle,
1358c2ecf20Sopenharmony_ci			  struct drm_file *file_priv)
1368c2ecf20Sopenharmony_ci{
1378c2ecf20Sopenharmony_ci	if (!dev->driver->dumb_create)
1388c2ecf20Sopenharmony_ci		return -ENOSYS;
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_ci	if (dev->driver->dumb_destroy)
1418c2ecf20Sopenharmony_ci		return dev->driver->dumb_destroy(file_priv, dev, handle);
1428c2ecf20Sopenharmony_ci	else
1438c2ecf20Sopenharmony_ci		return drm_gem_dumb_destroy(file_priv, dev, handle);
1448c2ecf20Sopenharmony_ci}
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_ciint drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
1478c2ecf20Sopenharmony_ci				void *data, struct drm_file *file_priv)
1488c2ecf20Sopenharmony_ci{
1498c2ecf20Sopenharmony_ci	struct drm_mode_destroy_dumb *args = data;
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_ci	return drm_mode_destroy_dumb(dev, args->handle, file_priv);
1528c2ecf20Sopenharmony_ci}
153