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