2 * Copyright 2011 Red Hat, Inc.
3 * Copyright © 2014 The Chromium OS Authors
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software")
7 * to deal in the software without restriction, including without limitation
8 * on the rights to use, copy, modify, merge, publish, distribute, sub
9 * license, and/or sell copies of the Software, and to permit persons to whom
10 * them Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTIBILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES, OR OTHER LIABILITY, WHETHER
20 * IN AN ACTION OF CONTRACT, TORT, OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 * This is vgem, a (non-hardware-backed) GEM service. This is used by Mesa's
30 * software renderer and the X server for efficient buffer sharing.
33 #include <linux/dma-buf.h>
34 #include <linux/module.h>
35 #include <linux/platform_device.h>
36 #include <linux/shmem_fs.h>
37 #include <linux/vmalloc.h>
39 #include <drm/drm_drv.h>
40 #include <drm/drm_file.h>
41 #include <drm/drm_gem_shmem_helper.h>
42 #include <drm/drm_ioctl.h>
43 #include <drm/drm_managed.h>
44 #include <drm/drm_prime.h>
48 #define DRIVER_NAME "vgem"
49 #define DRIVER_DESC "Virtual GEM provider"
50 #define DRIVER_DATE "20120112"
51 #define DRIVER_MAJOR 1
52 #define DRIVER_MINOR 0
54 static struct vgem_device {
55 struct drm_device drm;
56 struct platform_device *platform;
59 static int vgem_open(struct drm_device *dev, struct drm_file *file)
61 struct vgem_file *vfile;
64 vfile = kzalloc(sizeof(*vfile), GFP_KERNEL);
68 file->driver_priv = vfile;
70 ret = vgem_fence_open(vfile);
79 static void vgem_postclose(struct drm_device *dev, struct drm_file *file)
81 struct vgem_file *vfile = file->driver_priv;
83 vgem_fence_close(vfile);
87 static struct drm_ioctl_desc vgem_ioctls[] = {
88 DRM_IOCTL_DEF_DRV(VGEM_FENCE_ATTACH, vgem_fence_attach_ioctl, DRM_RENDER_ALLOW),
89 DRM_IOCTL_DEF_DRV(VGEM_FENCE_SIGNAL, vgem_fence_signal_ioctl, DRM_RENDER_ALLOW),
92 DEFINE_DRM_GEM_FOPS(vgem_driver_fops);
94 static struct drm_gem_object *vgem_gem_create_object(struct drm_device *dev, size_t size)
96 struct drm_gem_shmem_object *obj;
98 obj = kzalloc(sizeof(*obj), GFP_KERNEL);
100 return ERR_PTR(-ENOMEM);
103 * vgem doesn't have any begin/end cpu access ioctls, therefore must use
104 * coherent memory or dma-buf sharing just wont work.
111 static const struct drm_driver vgem_driver = {
112 .driver_features = DRIVER_GEM | DRIVER_RENDER,
114 .postclose = vgem_postclose,
115 .ioctls = vgem_ioctls,
116 .num_ioctls = ARRAY_SIZE(vgem_ioctls),
117 .fops = &vgem_driver_fops,
119 DRM_GEM_SHMEM_DRIVER_OPS,
120 .gem_create_object = vgem_gem_create_object,
125 .major = DRIVER_MAJOR,
126 .minor = DRIVER_MINOR,
129 static int __init vgem_init(void)
132 struct platform_device *pdev;
134 pdev = platform_device_register_simple("vgem", -1, NULL, 0);
136 return PTR_ERR(pdev);
138 if (!devres_open_group(&pdev->dev, NULL, GFP_KERNEL)) {
143 dma_coerce_mask_and_coherent(&pdev->dev,
146 vgem_device = devm_drm_dev_alloc(&pdev->dev, &vgem_driver,
147 struct vgem_device, drm);
148 if (IS_ERR(vgem_device)) {
149 ret = PTR_ERR(vgem_device);
152 vgem_device->platform = pdev;
154 /* Final step: expose the device/driver to userspace */
155 ret = drm_dev_register(&vgem_device->drm, 0);
162 devres_release_group(&pdev->dev, NULL);
164 platform_device_unregister(pdev);
168 static void __exit vgem_exit(void)
170 struct platform_device *pdev = vgem_device->platform;
172 drm_dev_unregister(&vgem_device->drm);
173 devres_release_group(&pdev->dev, NULL);
174 platform_device_unregister(pdev);
177 module_init(vgem_init);
178 module_exit(vgem_exit);
180 MODULE_AUTHOR("Red Hat, Inc.");
181 MODULE_AUTHOR("Intel Corporation");
182 MODULE_DESCRIPTION(DRIVER_DESC);
183 MODULE_LICENSE("GPL and additional rights");