14be47400SFrançois Tigeot /*
24be47400SFrançois Tigeot * Copyright (c) 2006-2008 Intel Corporation
34be47400SFrançois Tigeot * Copyright (c) 2007 Dave Airlie <airlied@linux.ie>
44be47400SFrançois Tigeot * Copyright (c) 2008 Red Hat Inc.
54be47400SFrançois Tigeot * Copyright (c) 2016 Intel Corporation
64be47400SFrançois Tigeot *
74be47400SFrançois Tigeot * Permission to use, copy, modify, distribute, and sell this software and its
84be47400SFrançois Tigeot * documentation for any purpose is hereby granted without fee, provided that
94be47400SFrançois Tigeot * the above copyright notice appear in all copies and that both that copyright
104be47400SFrançois Tigeot * notice and this permission notice appear in supporting documentation, and
114be47400SFrançois Tigeot * that the name of the copyright holders not be used in advertising or
124be47400SFrançois Tigeot * publicity pertaining to distribution of the software without specific,
134be47400SFrançois Tigeot * written prior permission. The copyright holders make no representations
144be47400SFrançois Tigeot * about the suitability of this software for any purpose. It is provided "as
154be47400SFrançois Tigeot * is" without express or implied warranty.
164be47400SFrançois Tigeot *
174be47400SFrançois Tigeot * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
184be47400SFrançois Tigeot * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
194be47400SFrançois Tigeot * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
204be47400SFrançois Tigeot * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
214be47400SFrançois Tigeot * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
224be47400SFrançois Tigeot * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
234be47400SFrançois Tigeot * OF THIS SOFTWARE.
244be47400SFrançois Tigeot */
254be47400SFrançois Tigeot
264be47400SFrançois Tigeot #include <drm/drmP.h>
27*3f2dd94aSFrançois Tigeot #include <drm/drm_gem.h>
284be47400SFrançois Tigeot
294be47400SFrançois Tigeot #include "drm_crtc_internal.h"
304be47400SFrançois Tigeot
314be47400SFrançois Tigeot /**
324be47400SFrançois Tigeot * DOC: overview
334be47400SFrançois Tigeot *
344be47400SFrançois Tigeot * The KMS API doesn't standardize backing storage object creation and leaves it
354be47400SFrançois Tigeot * to driver-specific ioctls. Furthermore actually creating a buffer object even
364be47400SFrançois Tigeot * for GEM-based drivers is done through a driver-specific ioctl - GEM only has
374be47400SFrançois Tigeot * a common userspace interface for sharing and destroying objects. While not an
384be47400SFrançois Tigeot * issue for full-fledged graphics stacks that include device-specific userspace
394be47400SFrançois Tigeot * components (in libdrm for instance), this limit makes DRM-based early boot
404be47400SFrançois Tigeot * graphics unnecessarily complex.
414be47400SFrançois Tigeot *
424be47400SFrançois Tigeot * Dumb objects partly alleviate the problem by providing a standard API to
434be47400SFrançois Tigeot * create dumb buffers suitable for scanout, which can then be used to create
444be47400SFrançois Tigeot * KMS frame buffers.
454be47400SFrançois Tigeot *
46*3f2dd94aSFrançois Tigeot * To support dumb objects drivers must implement the &drm_driver.dumb_create
47*3f2dd94aSFrançois Tigeot * operation. &drm_driver.dumb_destroy defaults to drm_gem_dumb_destroy() if
48*3f2dd94aSFrançois Tigeot * not set and &drm_driver.dumb_map_offset defaults to
49*3f2dd94aSFrançois Tigeot * drm_gem_dumb_map_offset(). See the callbacks for further details.
504be47400SFrançois Tigeot *
514be47400SFrançois Tigeot * Note that dumb objects may not be used for gpu acceleration, as has been
524be47400SFrançois Tigeot * attempted on some ARM embedded platforms. Such drivers really must have
534be47400SFrançois Tigeot * a hardware-specific ioctl to allocate suitable buffer objects.
544be47400SFrançois Tigeot */
554be47400SFrançois Tigeot
drm_mode_create_dumb_ioctl(struct drm_device * dev,void * data,struct drm_file * file_priv)564be47400SFrançois Tigeot int drm_mode_create_dumb_ioctl(struct drm_device *dev,
574be47400SFrançois Tigeot void *data, struct drm_file *file_priv)
584be47400SFrançois Tigeot {
594be47400SFrançois Tigeot struct drm_mode_create_dumb *args = data;
604be47400SFrançois Tigeot u32 cpp, stride, size;
614be47400SFrançois Tigeot
624be47400SFrançois Tigeot if (!dev->driver->dumb_create)
634be47400SFrançois Tigeot return -ENOSYS;
644be47400SFrançois Tigeot if (!args->width || !args->height || !args->bpp)
654be47400SFrançois Tigeot return -EINVAL;
664be47400SFrançois Tigeot
674be47400SFrançois Tigeot /* overflow checks for 32bit size calculations */
684be47400SFrançois Tigeot /* NOTE: DIV_ROUND_UP() can overflow */
694be47400SFrançois Tigeot cpp = DIV_ROUND_UP(args->bpp, 8);
704be47400SFrançois Tigeot if (!cpp || cpp > 0xffffffffU / args->width)
714be47400SFrançois Tigeot return -EINVAL;
724be47400SFrançois Tigeot stride = cpp * args->width;
734be47400SFrançois Tigeot if (args->height > 0xffffffffU / stride)
744be47400SFrançois Tigeot return -EINVAL;
754be47400SFrançois Tigeot
764be47400SFrançois Tigeot /* test for wrap-around */
774be47400SFrançois Tigeot size = args->height * stride;
784be47400SFrançois Tigeot if (PAGE_ALIGN(size) == 0)
794be47400SFrançois Tigeot return -EINVAL;
804be47400SFrançois Tigeot
814be47400SFrançois Tigeot /*
824be47400SFrançois Tigeot * handle, pitch and size are output parameters. Zero them out to
834be47400SFrançois Tigeot * prevent drivers from accidentally using uninitialized data. Since
844be47400SFrançois Tigeot * not all existing userspace is clearing these fields properly we
854be47400SFrançois Tigeot * cannot reject IOCTL with garbage in them.
864be47400SFrançois Tigeot */
874be47400SFrançois Tigeot args->handle = 0;
884be47400SFrançois Tigeot args->pitch = 0;
894be47400SFrançois Tigeot args->size = 0;
904be47400SFrançois Tigeot
914be47400SFrançois Tigeot return dev->driver->dumb_create(file_priv, dev, args);
924be47400SFrançois Tigeot }
934be47400SFrançois Tigeot
944be47400SFrançois Tigeot /**
954be47400SFrançois Tigeot * drm_mode_mmap_dumb_ioctl - create an mmap offset for a dumb backing storage buffer
964be47400SFrançois Tigeot * @dev: DRM device
974be47400SFrançois Tigeot * @data: ioctl data
984be47400SFrançois Tigeot * @file_priv: DRM file info
994be47400SFrançois Tigeot *
1004be47400SFrançois Tigeot * Allocate an offset in the drm device node's address space to be able to
1014be47400SFrançois Tigeot * memory map a dumb buffer.
1024be47400SFrançois Tigeot *
1034be47400SFrançois Tigeot * Called by the user via ioctl.
1044be47400SFrançois Tigeot *
1054be47400SFrançois Tigeot * Returns:
1064be47400SFrançois Tigeot * Zero on success, negative errno on failure.
1074be47400SFrançois Tigeot */
drm_mode_mmap_dumb_ioctl(struct drm_device * dev,void * data,struct drm_file * file_priv)1084be47400SFrançois Tigeot int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
1094be47400SFrançois Tigeot void *data, struct drm_file *file_priv)
1104be47400SFrançois Tigeot {
1114be47400SFrançois Tigeot struct drm_mode_map_dumb *args = data;
1124be47400SFrançois Tigeot
113*3f2dd94aSFrançois Tigeot if (!dev->driver->dumb_create)
1144be47400SFrançois Tigeot return -ENOSYS;
1154be47400SFrançois Tigeot
116*3f2dd94aSFrançois Tigeot if (dev->driver->dumb_map_offset)
117*3f2dd94aSFrançois Tigeot return dev->driver->dumb_map_offset(file_priv, dev,
118*3f2dd94aSFrançois Tigeot args->handle,
119*3f2dd94aSFrançois Tigeot (uint64_t *)&args->offset);
120*3f2dd94aSFrançois Tigeot else
121*3f2dd94aSFrançois Tigeot return drm_gem_dumb_map_offset(file_priv, dev, args->handle,
122*3f2dd94aSFrançois Tigeot &args->offset);
1234be47400SFrançois Tigeot }
1244be47400SFrançois Tigeot
drm_mode_destroy_dumb_ioctl(struct drm_device * dev,void * data,struct drm_file * file_priv)1254be47400SFrançois Tigeot int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
1264be47400SFrançois Tigeot void *data, struct drm_file *file_priv)
1274be47400SFrançois Tigeot {
1284be47400SFrançois Tigeot struct drm_mode_destroy_dumb *args = data;
1294be47400SFrançois Tigeot
130*3f2dd94aSFrançois Tigeot if (!dev->driver->dumb_create)
1314be47400SFrançois Tigeot return -ENOSYS;
1324be47400SFrançois Tigeot
133*3f2dd94aSFrançois Tigeot if (dev->driver->dumb_destroy)
1344be47400SFrançois Tigeot return dev->driver->dumb_destroy(file_priv, dev, args->handle);
135*3f2dd94aSFrançois Tigeot else
136*3f2dd94aSFrançois Tigeot return drm_gem_dumb_destroy(file_priv, dev, args->handle);
1374be47400SFrançois Tigeot }
138