Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * Copyright (c) 2006-2008 Intel Corporation
0003  * Copyright (c) 2007 Dave Airlie <airlied@linux.ie>
0004  * Copyright (c) 2008 Red Hat Inc.
0005  * Copyright (c) 2016 Intel Corporation
0006  *
0007  * Permission to use, copy, modify, distribute, and sell this software and its
0008  * documentation for any purpose is hereby granted without fee, provided that
0009  * the above copyright notice appear in all copies and that both that copyright
0010  * notice and this permission notice appear in supporting documentation, and
0011  * that the name of the copyright holders not be used in advertising or
0012  * publicity pertaining to distribution of the software without specific,
0013  * written prior permission.  The copyright holders make no representations
0014  * about the suitability of this software for any purpose.  It is provided "as
0015  * is" without express or implied warranty.
0016  *
0017  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
0018  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
0019  * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
0020  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
0021  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
0022  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
0023  * OF THIS SOFTWARE.
0024  */
0025 
0026 #include <drm/drm_device.h>
0027 #include <drm/drm_drv.h>
0028 #include <drm/drm_gem.h>
0029 #include <drm/drm_mode.h>
0030 
0031 #include "drm_crtc_internal.h"
0032 #include "drm_internal.h"
0033 
0034 /**
0035  * DOC: overview
0036  *
0037  * The KMS API doesn't standardize backing storage object creation and leaves it
0038  * to driver-specific ioctls. Furthermore actually creating a buffer object even
0039  * for GEM-based drivers is done through a driver-specific ioctl - GEM only has
0040  * a common userspace interface for sharing and destroying objects. While not an
0041  * issue for full-fledged graphics stacks that include device-specific userspace
0042  * components (in libdrm for instance), this limit makes DRM-based early boot
0043  * graphics unnecessarily complex.
0044  *
0045  * Dumb objects partly alleviate the problem by providing a standard API to
0046  * create dumb buffers suitable for scanout, which can then be used to create
0047  * KMS frame buffers.
0048  *
0049  * To support dumb objects drivers must implement the &drm_driver.dumb_create
0050  * and &drm_driver.dumb_map_offset operations (the latter defaults to
0051  * drm_gem_dumb_map_offset() if not set). Drivers that don't use GEM handles
0052  * additionally need to implement the &drm_driver.dumb_destroy operation. See
0053  * the callbacks for further details.
0054  *
0055  * Note that dumb objects may not be used for gpu acceleration, as has been
0056  * attempted on some ARM embedded platforms. Such drivers really must have
0057  * a hardware-specific ioctl to allocate suitable buffer objects.
0058  */
0059 
0060 int drm_mode_create_dumb(struct drm_device *dev,
0061              struct drm_mode_create_dumb *args,
0062              struct drm_file *file_priv)
0063 {
0064     u32 cpp, stride, size;
0065 
0066     if (!dev->driver->dumb_create)
0067         return -ENOSYS;
0068     if (!args->width || !args->height || !args->bpp)
0069         return -EINVAL;
0070 
0071     /* overflow checks for 32bit size calculations */
0072     if (args->bpp > U32_MAX - 8)
0073         return -EINVAL;
0074     cpp = DIV_ROUND_UP(args->bpp, 8);
0075     if (cpp > U32_MAX / args->width)
0076         return -EINVAL;
0077     stride = cpp * args->width;
0078     if (args->height > U32_MAX / stride)
0079         return -EINVAL;
0080 
0081     /* test for wrap-around */
0082     size = args->height * stride;
0083     if (PAGE_ALIGN(size) == 0)
0084         return -EINVAL;
0085 
0086     /*
0087      * handle, pitch and size are output parameters. Zero them out to
0088      * prevent drivers from accidentally using uninitialized data. Since
0089      * not all existing userspace is clearing these fields properly we
0090      * cannot reject IOCTL with garbage in them.
0091      */
0092     args->handle = 0;
0093     args->pitch = 0;
0094     args->size = 0;
0095 
0096     return dev->driver->dumb_create(file_priv, dev, args);
0097 }
0098 
0099 int drm_mode_create_dumb_ioctl(struct drm_device *dev,
0100                    void *data, struct drm_file *file_priv)
0101 {
0102     return drm_mode_create_dumb(dev, data, file_priv);
0103 }
0104 
0105 /**
0106  * drm_mode_mmap_dumb_ioctl - create an mmap offset for a dumb backing storage buffer
0107  * @dev: DRM device
0108  * @data: ioctl data
0109  * @file_priv: DRM file info
0110  *
0111  * Allocate an offset in the drm device node's address space to be able to
0112  * memory map a dumb buffer.
0113  *
0114  * Called by the user via ioctl.
0115  *
0116  * Returns:
0117  * Zero on success, negative errno on failure.
0118  */
0119 int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
0120                  void *data, struct drm_file *file_priv)
0121 {
0122     struct drm_mode_map_dumb *args = data;
0123 
0124     if (!dev->driver->dumb_create)
0125         return -ENOSYS;
0126 
0127     if (dev->driver->dumb_map_offset)
0128         return dev->driver->dumb_map_offset(file_priv, dev,
0129                             args->handle,
0130                             &args->offset);
0131     else
0132         return drm_gem_dumb_map_offset(file_priv, dev, args->handle,
0133                            &args->offset);
0134 }
0135 
0136 int drm_mode_destroy_dumb(struct drm_device *dev, u32 handle,
0137               struct drm_file *file_priv)
0138 {
0139     if (!dev->driver->dumb_create)
0140         return -ENOSYS;
0141 
0142     if (dev->driver->dumb_destroy)
0143         return dev->driver->dumb_destroy(file_priv, dev, handle);
0144     else
0145         return drm_gem_dumb_destroy(file_priv, dev, handle);
0146 }
0147 
0148 int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
0149                 void *data, struct drm_file *file_priv)
0150 {
0151     struct drm_mode_destroy_dumb *args = data;
0152 
0153     return drm_mode_destroy_dumb(dev, args->handle, file_priv);
0154 }