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