vgem_drv.c (4806B)
1/* 2 * Copyright 2011 Red Hat, Inc. 3 * Copyright © 2014 The Chromium OS Authors 4 * 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: 11 * 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 14 * Software. 15 * 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. 22 * 23 * Authors: 24 * Adam Jackson <ajax@redhat.com> 25 * Ben Widawsky <ben@bwidawsk.net> 26 */ 27 28/* 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. 31 */ 32 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> 38 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> 45 46#include "vgem_drv.h" 47 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 53 54static struct vgem_device { 55 struct drm_device drm; 56 struct platform_device *platform; 57} *vgem_device; 58 59static int vgem_open(struct drm_device *dev, struct drm_file *file) 60{ 61 struct vgem_file *vfile; 62 int ret; 63 64 vfile = kzalloc(sizeof(*vfile), GFP_KERNEL); 65 if (!vfile) 66 return -ENOMEM; 67 68 file->driver_priv = vfile; 69 70 ret = vgem_fence_open(vfile); 71 if (ret) { 72 kfree(vfile); 73 return ret; 74 } 75 76 return 0; 77} 78 79static void vgem_postclose(struct drm_device *dev, struct drm_file *file) 80{ 81 struct vgem_file *vfile = file->driver_priv; 82 83 vgem_fence_close(vfile); 84 kfree(vfile); 85} 86 87static 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), 90}; 91 92DEFINE_DRM_GEM_FOPS(vgem_driver_fops); 93 94static struct drm_gem_object *vgem_gem_create_object(struct drm_device *dev, size_t size) 95{ 96 struct drm_gem_shmem_object *obj; 97 98 obj = kzalloc(sizeof(*obj), GFP_KERNEL); 99 if (!obj) 100 return ERR_PTR(-ENOMEM); 101 102 /* 103 * vgem doesn't have any begin/end cpu access ioctls, therefore must use 104 * coherent memory or dma-buf sharing just wont work. 105 */ 106 obj->map_wc = true; 107 108 return &obj->base; 109} 110 111static const struct drm_driver vgem_driver = { 112 .driver_features = DRIVER_GEM | DRIVER_RENDER, 113 .open = vgem_open, 114 .postclose = vgem_postclose, 115 .ioctls = vgem_ioctls, 116 .num_ioctls = ARRAY_SIZE(vgem_ioctls), 117 .fops = &vgem_driver_fops, 118 119 DRM_GEM_SHMEM_DRIVER_OPS, 120 .gem_create_object = vgem_gem_create_object, 121 122 .name = DRIVER_NAME, 123 .desc = DRIVER_DESC, 124 .date = DRIVER_DATE, 125 .major = DRIVER_MAJOR, 126 .minor = DRIVER_MINOR, 127}; 128 129static int __init vgem_init(void) 130{ 131 int ret; 132 struct platform_device *pdev; 133 134 pdev = platform_device_register_simple("vgem", -1, NULL, 0); 135 if (IS_ERR(pdev)) 136 return PTR_ERR(pdev); 137 138 if (!devres_open_group(&pdev->dev, NULL, GFP_KERNEL)) { 139 ret = -ENOMEM; 140 goto out_unregister; 141 } 142 143 dma_coerce_mask_and_coherent(&pdev->dev, 144 DMA_BIT_MASK(64)); 145 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); 150 goto out_devres; 151 } 152 vgem_device->platform = pdev; 153 154 /* Final step: expose the device/driver to userspace */ 155 ret = drm_dev_register(&vgem_device->drm, 0); 156 if (ret) 157 goto out_devres; 158 159 return 0; 160 161out_devres: 162 devres_release_group(&pdev->dev, NULL); 163out_unregister: 164 platform_device_unregister(pdev); 165 return ret; 166} 167 168static void __exit vgem_exit(void) 169{ 170 struct platform_device *pdev = vgem_device->platform; 171 172 drm_dev_unregister(&vgem_device->drm); 173 devres_release_group(&pdev->dev, NULL); 174 platform_device_unregister(pdev); 175} 176 177module_init(vgem_init); 178module_exit(vgem_exit); 179 180MODULE_AUTHOR("Red Hat, Inc."); 181MODULE_AUTHOR("Intel Corporation"); 182MODULE_DESCRIPTION(DRIVER_DESC); 183MODULE_LICENSE("GPL and additional rights");