1 /* $NetBSD: virtgpu_drv.c,v 1.2 2018/08/27 04:58:37 riastradh Exp $ */ 2 3 /* 4 * Copyright (C) 2015 Red Hat, Inc. 5 * All Rights Reserved. 6 * 7 * Authors: 8 * Dave Airlie <airlied@redhat.com> 9 * Gerd Hoffmann <kraxel@redhat.com> 10 * 11 * Permission is hereby granted, free of charge, to any person obtaining a 12 * copy of this software and associated documentation files (the "Software"), 13 * to deal in the Software without restriction, including without limitation 14 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 15 * and/or sell copies of the Software, and to permit persons to whom the 16 * Software is furnished to do so, subject to the following conditions: 17 * 18 * The above copyright notice and this permission notice (including the next 19 * paragraph) shall be included in all copies or substantial portions of the 20 * Software. 21 * 22 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 23 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 24 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 25 * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR 26 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 27 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 28 * OTHER DEALINGS IN THE SOFTWARE. 29 */ 30 31 #include <sys/cdefs.h> 32 __KERNEL_RCSID(0, "$NetBSD: virtgpu_drv.c,v 1.2 2018/08/27 04:58:37 riastradh Exp $"); 33 34 #include <linux/module.h> 35 #include <linux/console.h> 36 #include <linux/pci.h> 37 #include "drmP.h" 38 #include "drm/drm.h" 39 40 #include "virtgpu_drv.h" 41 static struct drm_driver driver; 42 43 static int virtio_gpu_modeset = -1; 44 45 MODULE_PARM_DESC(modeset, "Disable/Enable modesetting"); 46 module_param_named(modeset, virtio_gpu_modeset, int, 0400); 47 48 static int virtio_gpu_probe(struct virtio_device *vdev) 49 { 50 #ifdef CONFIG_VGA_CONSOLE 51 if (vgacon_text_force() && virtio_gpu_modeset == -1) 52 return -EINVAL; 53 #endif 54 55 if (virtio_gpu_modeset == 0) 56 return -EINVAL; 57 58 return drm_virtio_init(&driver, vdev); 59 } 60 61 static void virtio_gpu_remove(struct virtio_device *vdev) 62 { 63 struct drm_device *dev = vdev->priv; 64 drm_put_dev(dev); 65 } 66 67 static void virtio_gpu_config_changed(struct virtio_device *vdev) 68 { 69 struct drm_device *dev = vdev->priv; 70 struct virtio_gpu_device *vgdev = dev->dev_private; 71 72 schedule_work(&vgdev->config_changed_work); 73 } 74 75 static struct virtio_device_id id_table[] = { 76 { VIRTIO_ID_GPU, VIRTIO_DEV_ANY_ID }, 77 { 0 }, 78 }; 79 80 static unsigned int features[] = { 81 #ifdef __LITTLE_ENDIAN 82 /* 83 * Gallium command stream send by virgl is native endian. 84 * Because of that we only support little endian guests on 85 * little endian hosts. 86 */ 87 VIRTIO_GPU_F_VIRGL, 88 #endif 89 }; 90 static struct virtio_driver virtio_gpu_driver = { 91 .feature_table = features, 92 .feature_table_size = ARRAY_SIZE(features), 93 .driver.name = KBUILD_MODNAME, 94 .driver.owner = THIS_MODULE, 95 .id_table = id_table, 96 .probe = virtio_gpu_probe, 97 .remove = virtio_gpu_remove, 98 .config_changed = virtio_gpu_config_changed 99 }; 100 101 module_virtio_driver(virtio_gpu_driver); 102 103 MODULE_DEVICE_TABLE(virtio, id_table); 104 MODULE_DESCRIPTION("Virtio GPU driver"); 105 MODULE_LICENSE("GPL and additional rights"); 106 MODULE_AUTHOR("Dave Airlie <airlied@redhat.com>"); 107 MODULE_AUTHOR("Gerd Hoffmann <kraxel@redhat.com>"); 108 MODULE_AUTHOR("Alon Levy"); 109 110 static const struct file_operations virtio_gpu_driver_fops = { 111 .owner = THIS_MODULE, 112 .open = drm_open, 113 .mmap = virtio_gpu_mmap, 114 .poll = drm_poll, 115 .read = drm_read, 116 .unlocked_ioctl = drm_ioctl, 117 .release = drm_release, 118 #ifdef CONFIG_COMPAT 119 .compat_ioctl = drm_compat_ioctl, 120 #endif 121 .llseek = noop_llseek, 122 }; 123 124 125 static struct drm_driver driver = { 126 .driver_features = DRIVER_MODESET | DRIVER_GEM | DRIVER_PRIME | DRIVER_RENDER, 127 .set_busid = drm_virtio_set_busid, 128 .load = virtio_gpu_driver_load, 129 .unload = virtio_gpu_driver_unload, 130 .open = virtio_gpu_driver_open, 131 .postclose = virtio_gpu_driver_postclose, 132 133 .dumb_create = virtio_gpu_mode_dumb_create, 134 .dumb_map_offset = virtio_gpu_mode_dumb_mmap, 135 .dumb_destroy = virtio_gpu_mode_dumb_destroy, 136 137 #if defined(CONFIG_DEBUG_FS) 138 .debugfs_init = virtio_gpu_debugfs_init, 139 .debugfs_cleanup = virtio_gpu_debugfs_takedown, 140 #endif 141 .prime_handle_to_fd = drm_gem_prime_handle_to_fd, 142 .prime_fd_to_handle = drm_gem_prime_fd_to_handle, 143 .gem_prime_export = drm_gem_prime_export, 144 .gem_prime_import = drm_gem_prime_import, 145 .gem_prime_pin = virtgpu_gem_prime_pin, 146 .gem_prime_unpin = virtgpu_gem_prime_unpin, 147 .gem_prime_get_sg_table = virtgpu_gem_prime_get_sg_table, 148 .gem_prime_import_sg_table = virtgpu_gem_prime_import_sg_table, 149 .gem_prime_vmap = virtgpu_gem_prime_vmap, 150 .gem_prime_vunmap = virtgpu_gem_prime_vunmap, 151 .gem_prime_mmap = virtgpu_gem_prime_mmap, 152 153 .gem_free_object = virtio_gpu_gem_free_object, 154 .gem_open_object = virtio_gpu_gem_object_open, 155 .gem_close_object = virtio_gpu_gem_object_close, 156 .fops = &virtio_gpu_driver_fops, 157 158 .ioctls = virtio_gpu_ioctls, 159 .num_ioctls = DRM_VIRTIO_NUM_IOCTLS, 160 161 .name = DRIVER_NAME, 162 .desc = DRIVER_DESC, 163 .date = DRIVER_DATE, 164 .major = DRIVER_MAJOR, 165 .minor = DRIVER_MINOR, 166 .patchlevel = DRIVER_PATCHLEVEL, 167 }; 168