1dc5698e8SDave Airlie /* 2dc5698e8SDave Airlie * Copyright (C) 2015 Red Hat, Inc. 3dc5698e8SDave Airlie * All Rights Reserved. 4dc5698e8SDave Airlie * 5dc5698e8SDave Airlie * Authors: 6dc5698e8SDave Airlie * Dave Airlie <airlied@redhat.com> 7dc5698e8SDave Airlie * Gerd Hoffmann <kraxel@redhat.com> 8dc5698e8SDave Airlie * 9dc5698e8SDave Airlie * Permission is hereby granted, free of charge, to any person obtaining a 10dc5698e8SDave Airlie * copy of this software and associated documentation files (the "Software"), 11dc5698e8SDave Airlie * to deal in the Software without restriction, including without limitation 12dc5698e8SDave Airlie * the rights to use, copy, modify, merge, publish, distribute, sublicense, 13dc5698e8SDave Airlie * and/or sell copies of the Software, and to permit persons to whom the 14dc5698e8SDave Airlie * Software is furnished to do so, subject to the following conditions: 15dc5698e8SDave Airlie * 16dc5698e8SDave Airlie * The above copyright notice and this permission notice (including the next 17dc5698e8SDave Airlie * paragraph) shall be included in all copies or substantial portions of the 18dc5698e8SDave Airlie * Software. 19dc5698e8SDave Airlie * 20dc5698e8SDave Airlie * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21dc5698e8SDave Airlie * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22dc5698e8SDave Airlie * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 23dc5698e8SDave Airlie * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR 24dc5698e8SDave Airlie * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 25dc5698e8SDave Airlie * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 26dc5698e8SDave Airlie * OTHER DEALINGS IN THE SOFTWARE. 27dc5698e8SDave Airlie */ 28dc5698e8SDave Airlie 29dc5698e8SDave Airlie #include <linux/module.h> 30dc5698e8SDave Airlie #include <linux/console.h> 31dc5698e8SDave Airlie #include <linux/pci.h> 32ff67a25dSMasahiro Yamada #include <drm/drmP.h> 33ff67a25dSMasahiro Yamada #include <drm/drm.h> 34dc5698e8SDave Airlie 35dc5698e8SDave Airlie #include "virtgpu_drv.h" 36dc5698e8SDave Airlie static struct drm_driver driver; 37dc5698e8SDave Airlie 38dc5698e8SDave Airlie static int virtio_gpu_modeset = -1; 39dc5698e8SDave Airlie 40dc5698e8SDave Airlie MODULE_PARM_DESC(modeset, "Disable/Enable modesetting"); 41dc5698e8SDave Airlie module_param_named(modeset, virtio_gpu_modeset, int, 0400); 42dc5698e8SDave Airlie 43d516e75cSEzequiel Garcia static int virtio_gpu_pci_quirk(struct drm_device *dev, struct virtio_device *vdev) 44d516e75cSEzequiel Garcia { 45d516e75cSEzequiel Garcia struct pci_dev *pdev = to_pci_dev(vdev->dev.parent); 46d516e75cSEzequiel Garcia const char *pname = dev_name(&pdev->dev); 47d516e75cSEzequiel Garcia bool vga = (pdev->class >> 8) == PCI_CLASS_DISPLAY_VGA; 48d516e75cSEzequiel Garcia char unique[20]; 49d516e75cSEzequiel Garcia 50d516e75cSEzequiel Garcia DRM_INFO("pci: %s detected at %s\n", 51d516e75cSEzequiel Garcia vga ? "virtio-vga" : "virtio-gpu-pci", 52d516e75cSEzequiel Garcia pname); 53d516e75cSEzequiel Garcia dev->pdev = pdev; 54d516e75cSEzequiel Garcia if (vga) 55d516e75cSEzequiel Garcia drm_fb_helper_remove_conflicting_pci_framebuffers(pdev, 56d516e75cSEzequiel Garcia 0, 57d516e75cSEzequiel Garcia "virtiodrmfb"); 58d516e75cSEzequiel Garcia 59d516e75cSEzequiel Garcia /* 60d516e75cSEzequiel Garcia * Normally the drm_dev_set_unique() call is done by core DRM. 61d516e75cSEzequiel Garcia * The following comment covers, why virtio cannot rely on it. 62d516e75cSEzequiel Garcia * 63d516e75cSEzequiel Garcia * Unlike the other virtual GPU drivers, virtio abstracts the 64d516e75cSEzequiel Garcia * underlying bus type by using struct virtio_device. 65d516e75cSEzequiel Garcia * 66d516e75cSEzequiel Garcia * Hence the dev_is_pci() check, used in core DRM, will fail 67d516e75cSEzequiel Garcia * and the unique returned will be the virtio_device "virtio0", 68d516e75cSEzequiel Garcia * while a "pci:..." one is required. 69d516e75cSEzequiel Garcia * 70d516e75cSEzequiel Garcia * A few other ideas were considered: 71d516e75cSEzequiel Garcia * - Extend the dev_is_pci() check [in drm_set_busid] to 72d516e75cSEzequiel Garcia * consider virtio. 73d516e75cSEzequiel Garcia * Seems like a bigger hack than what we have already. 74d516e75cSEzequiel Garcia * 75d516e75cSEzequiel Garcia * - Point drm_device::dev to the parent of the virtio_device 76d516e75cSEzequiel Garcia * Semantic changes: 77d516e75cSEzequiel Garcia * * Using the wrong device for i2c, framebuffer_alloc and 78d516e75cSEzequiel Garcia * prime import. 79d516e75cSEzequiel Garcia * Visual changes: 80d516e75cSEzequiel Garcia * * Helpers such as DRM_DEV_ERROR, dev_info, drm_printer, 81d516e75cSEzequiel Garcia * will print the wrong information. 82d516e75cSEzequiel Garcia * 83d516e75cSEzequiel Garcia * We could address the latter issues, by introducing 84d516e75cSEzequiel Garcia * drm_device::bus_dev, ... which would be used solely for this. 85d516e75cSEzequiel Garcia * 86d516e75cSEzequiel Garcia * So for the moment keep things as-is, with a bulky comment 87d516e75cSEzequiel Garcia * for the next person who feels like removing this 88d516e75cSEzequiel Garcia * drm_dev_set_unique() quirk. 89d516e75cSEzequiel Garcia */ 90d516e75cSEzequiel Garcia snprintf(unique, sizeof(unique), "pci:%s", pname); 91d516e75cSEzequiel Garcia return drm_dev_set_unique(dev, unique); 92d516e75cSEzequiel Garcia } 93d516e75cSEzequiel Garcia 94dc5698e8SDave Airlie static int virtio_gpu_probe(struct virtio_device *vdev) 95dc5698e8SDave Airlie { 96d516e75cSEzequiel Garcia struct drm_device *dev; 9748a77d66SGerd Hoffmann int ret; 9848a77d66SGerd Hoffmann 99dc5698e8SDave Airlie if (vgacon_text_force() && virtio_gpu_modeset == -1) 100dc5698e8SDave Airlie return -EINVAL; 101dc5698e8SDave Airlie 102dc5698e8SDave Airlie if (virtio_gpu_modeset == 0) 103dc5698e8SDave Airlie return -EINVAL; 104dc5698e8SDave Airlie 105d516e75cSEzequiel Garcia dev = drm_dev_alloc(&driver, &vdev->dev); 106d516e75cSEzequiel Garcia if (IS_ERR(dev)) 107d516e75cSEzequiel Garcia return PTR_ERR(dev); 108d516e75cSEzequiel Garcia vdev->priv = dev; 109d516e75cSEzequiel Garcia 110d516e75cSEzequiel Garcia if (!strcmp(vdev->dev.parent->bus->name, "pci")) { 111d516e75cSEzequiel Garcia ret = virtio_gpu_pci_quirk(dev, vdev); 11248a77d66SGerd Hoffmann if (ret) 113d516e75cSEzequiel Garcia goto err_free; 114d516e75cSEzequiel Garcia } 115d516e75cSEzequiel Garcia 116d516e75cSEzequiel Garcia ret = virtio_gpu_init(dev); 117d516e75cSEzequiel Garcia if (ret) 118d516e75cSEzequiel Garcia goto err_free; 119d516e75cSEzequiel Garcia 120d516e75cSEzequiel Garcia ret = drm_dev_register(dev, 0); 121d516e75cSEzequiel Garcia if (ret) 122d516e75cSEzequiel Garcia goto err_free; 12348a77d66SGerd Hoffmann 12448a77d66SGerd Hoffmann drm_fbdev_generic_setup(vdev->priv, 32); 12548a77d66SGerd Hoffmann return 0; 126d516e75cSEzequiel Garcia 127d516e75cSEzequiel Garcia err_free: 128d516e75cSEzequiel Garcia drm_dev_put(dev); 129d516e75cSEzequiel Garcia return ret; 130dc5698e8SDave Airlie } 131dc5698e8SDave Airlie 132dc5698e8SDave Airlie static void virtio_gpu_remove(struct virtio_device *vdev) 133dc5698e8SDave Airlie { 134dc5698e8SDave Airlie struct drm_device *dev = vdev->priv; 1359d492b6bSRodrigo Siqueira 136d516e75cSEzequiel Garcia drm_dev_unregister(dev); 137d516e75cSEzequiel Garcia virtio_gpu_deinit(dev); 138dc5698e8SDave Airlie drm_put_dev(dev); 139dc5698e8SDave Airlie } 140dc5698e8SDave Airlie 141dc5698e8SDave Airlie static void virtio_gpu_config_changed(struct virtio_device *vdev) 142dc5698e8SDave Airlie { 143dc5698e8SDave Airlie struct drm_device *dev = vdev->priv; 144dc5698e8SDave Airlie struct virtio_gpu_device *vgdev = dev->dev_private; 145dc5698e8SDave Airlie 146dc5698e8SDave Airlie schedule_work(&vgdev->config_changed_work); 147dc5698e8SDave Airlie } 148dc5698e8SDave Airlie 149dc5698e8SDave Airlie static struct virtio_device_id id_table[] = { 150dc5698e8SDave Airlie { VIRTIO_ID_GPU, VIRTIO_DEV_ANY_ID }, 151dc5698e8SDave Airlie { 0 }, 152dc5698e8SDave Airlie }; 153dc5698e8SDave Airlie 154dc5698e8SDave Airlie static unsigned int features[] = { 15562fb7a5eSGerd Hoffmann #ifdef __LITTLE_ENDIAN 15662fb7a5eSGerd Hoffmann /* 15762fb7a5eSGerd Hoffmann * Gallium command stream send by virgl is native endian. 15862fb7a5eSGerd Hoffmann * Because of that we only support little endian guests on 15962fb7a5eSGerd Hoffmann * little endian hosts. 16062fb7a5eSGerd Hoffmann */ 16162fb7a5eSGerd Hoffmann VIRTIO_GPU_F_VIRGL, 16262fb7a5eSGerd Hoffmann #endif 163b4b01b49SGerd Hoffmann VIRTIO_GPU_F_EDID, 164dc5698e8SDave Airlie }; 165dc5698e8SDave Airlie static struct virtio_driver virtio_gpu_driver = { 166dc5698e8SDave Airlie .feature_table = features, 167dc5698e8SDave Airlie .feature_table_size = ARRAY_SIZE(features), 168dc5698e8SDave Airlie .driver.name = KBUILD_MODNAME, 169dc5698e8SDave Airlie .driver.owner = THIS_MODULE, 170dc5698e8SDave Airlie .id_table = id_table, 171dc5698e8SDave Airlie .probe = virtio_gpu_probe, 172dc5698e8SDave Airlie .remove = virtio_gpu_remove, 173dc5698e8SDave Airlie .config_changed = virtio_gpu_config_changed 174dc5698e8SDave Airlie }; 175dc5698e8SDave Airlie 176dc5698e8SDave Airlie module_virtio_driver(virtio_gpu_driver); 177dc5698e8SDave Airlie 178dc5698e8SDave Airlie MODULE_DEVICE_TABLE(virtio, id_table); 179dc5698e8SDave Airlie MODULE_DESCRIPTION("Virtio GPU driver"); 180dc5698e8SDave Airlie MODULE_LICENSE("GPL and additional rights"); 181dc5698e8SDave Airlie MODULE_AUTHOR("Dave Airlie <airlied@redhat.com>"); 182dc5698e8SDave Airlie MODULE_AUTHOR("Gerd Hoffmann <kraxel@redhat.com>"); 183dc5698e8SDave Airlie MODULE_AUTHOR("Alon Levy"); 184dc5698e8SDave Airlie 185dc5698e8SDave Airlie static const struct file_operations virtio_gpu_driver_fops = { 186dc5698e8SDave Airlie .owner = THIS_MODULE, 187dc5698e8SDave Airlie .open = drm_open, 188dc5698e8SDave Airlie .mmap = virtio_gpu_mmap, 189dc5698e8SDave Airlie .poll = drm_poll, 190dc5698e8SDave Airlie .read = drm_read, 191dc5698e8SDave Airlie .unlocked_ioctl = drm_ioctl, 192dc5698e8SDave Airlie .release = drm_release, 193dc5698e8SDave Airlie .compat_ioctl = drm_compat_ioctl, 194dc5698e8SDave Airlie .llseek = noop_llseek, 195dc5698e8SDave Airlie }; 196dc5698e8SDave Airlie 197dc5698e8SDave Airlie static struct drm_driver driver = { 1980424fdafSDaniel Vetter .driver_features = DRIVER_MODESET | DRIVER_GEM | DRIVER_RENDER | DRIVER_ATOMIC, 19962fb7a5eSGerd Hoffmann .open = virtio_gpu_driver_open, 20062fb7a5eSGerd Hoffmann .postclose = virtio_gpu_driver_postclose, 201dc5698e8SDave Airlie 202dc5698e8SDave Airlie .dumb_create = virtio_gpu_mode_dumb_create, 203dc5698e8SDave Airlie .dumb_map_offset = virtio_gpu_mode_dumb_mmap, 204dc5698e8SDave Airlie 205dc5698e8SDave Airlie #if defined(CONFIG_DEBUG_FS) 206dc5698e8SDave Airlie .debugfs_init = virtio_gpu_debugfs_init, 207dc5698e8SDave Airlie #endif 208a0cecc23SDave Airlie .prime_handle_to_fd = drm_gem_prime_handle_to_fd, 209a0cecc23SDave Airlie .prime_fd_to_handle = drm_gem_prime_fd_to_handle, 21011a8f280SDave Airlie .gem_prime_export = drm_gem_prime_export, 21111a8f280SDave Airlie .gem_prime_import = drm_gem_prime_import, 212a0cecc23SDave Airlie .gem_prime_get_sg_table = virtgpu_gem_prime_get_sg_table, 213a0cecc23SDave Airlie .gem_prime_import_sg_table = virtgpu_gem_prime_import_sg_table, 21411a8f280SDave Airlie .gem_prime_vmap = virtgpu_gem_prime_vmap, 21511a8f280SDave Airlie .gem_prime_vunmap = virtgpu_gem_prime_vunmap, 21611a8f280SDave Airlie .gem_prime_mmap = virtgpu_gem_prime_mmap, 217dc5698e8SDave Airlie 218bd884b74SDaniel Vetter .gem_free_object_unlocked = virtio_gpu_gem_free_object, 21962fb7a5eSGerd Hoffmann .gem_open_object = virtio_gpu_gem_object_open, 22062fb7a5eSGerd Hoffmann .gem_close_object = virtio_gpu_gem_object_close, 221dc5698e8SDave Airlie .fops = &virtio_gpu_driver_fops, 222dc5698e8SDave Airlie 22362fb7a5eSGerd Hoffmann .ioctls = virtio_gpu_ioctls, 22462fb7a5eSGerd Hoffmann .num_ioctls = DRM_VIRTIO_NUM_IOCTLS, 22562fb7a5eSGerd Hoffmann 226dc5698e8SDave Airlie .name = DRIVER_NAME, 227dc5698e8SDave Airlie .desc = DRIVER_DESC, 228dc5698e8SDave Airlie .date = DRIVER_DATE, 229dc5698e8SDave Airlie .major = DRIVER_MAJOR, 230dc5698e8SDave Airlie .minor = DRIVER_MINOR, 231dc5698e8SDave Airlie .patchlevel = DRIVER_PATCHLEVEL, 232dc5698e8SDave Airlie }; 233