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>
32dc5698e8SDave Airlie #include "drmP.h"
33dc5698e8SDave Airlie #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 
43dc5698e8SDave Airlie static int virtio_gpu_probe(struct virtio_device *vdev)
44dc5698e8SDave Airlie {
45dc5698e8SDave Airlie 	if (vgacon_text_force() && virtio_gpu_modeset == -1)
46dc5698e8SDave Airlie 		return -EINVAL;
47dc5698e8SDave Airlie 
48dc5698e8SDave Airlie 	if (virtio_gpu_modeset == 0)
49dc5698e8SDave Airlie 		return -EINVAL;
50dc5698e8SDave Airlie 
51dc5698e8SDave Airlie 	return drm_virtio_init(&driver, vdev);
52dc5698e8SDave Airlie }
53dc5698e8SDave Airlie 
54dc5698e8SDave Airlie static void virtio_gpu_remove(struct virtio_device *vdev)
55dc5698e8SDave Airlie {
56dc5698e8SDave Airlie 	struct drm_device *dev = vdev->priv;
57dc5698e8SDave Airlie 	drm_put_dev(dev);
58dc5698e8SDave Airlie }
59dc5698e8SDave Airlie 
60dc5698e8SDave Airlie static void virtio_gpu_config_changed(struct virtio_device *vdev)
61dc5698e8SDave Airlie {
62dc5698e8SDave Airlie 	struct drm_device *dev = vdev->priv;
63dc5698e8SDave Airlie 	struct virtio_gpu_device *vgdev = dev->dev_private;
64dc5698e8SDave Airlie 
65dc5698e8SDave Airlie 	schedule_work(&vgdev->config_changed_work);
66dc5698e8SDave Airlie }
67dc5698e8SDave Airlie 
68dc5698e8SDave Airlie static struct virtio_device_id id_table[] = {
69dc5698e8SDave Airlie 	{ VIRTIO_ID_GPU, VIRTIO_DEV_ANY_ID },
70dc5698e8SDave Airlie 	{ 0 },
71dc5698e8SDave Airlie };
72dc5698e8SDave Airlie 
73dc5698e8SDave Airlie static unsigned int features[] = {
7462fb7a5eSGerd Hoffmann #ifdef __LITTLE_ENDIAN
7562fb7a5eSGerd Hoffmann 	/*
7662fb7a5eSGerd Hoffmann 	 * Gallium command stream send by virgl is native endian.
7762fb7a5eSGerd Hoffmann 	 * Because of that we only support little endian guests on
7862fb7a5eSGerd Hoffmann 	 * little endian hosts.
7962fb7a5eSGerd Hoffmann 	 */
8062fb7a5eSGerd Hoffmann 	VIRTIO_GPU_F_VIRGL,
8162fb7a5eSGerd Hoffmann #endif
82dc5698e8SDave Airlie };
83dc5698e8SDave Airlie static struct virtio_driver virtio_gpu_driver = {
84dc5698e8SDave Airlie 	.feature_table = features,
85dc5698e8SDave Airlie 	.feature_table_size = ARRAY_SIZE(features),
86dc5698e8SDave Airlie 	.driver.name = KBUILD_MODNAME,
87dc5698e8SDave Airlie 	.driver.owner = THIS_MODULE,
88dc5698e8SDave Airlie 	.id_table = id_table,
89dc5698e8SDave Airlie 	.probe = virtio_gpu_probe,
90dc5698e8SDave Airlie 	.remove = virtio_gpu_remove,
91dc5698e8SDave Airlie 	.config_changed = virtio_gpu_config_changed
92dc5698e8SDave Airlie };
93dc5698e8SDave Airlie 
94dc5698e8SDave Airlie module_virtio_driver(virtio_gpu_driver);
95dc5698e8SDave Airlie 
96dc5698e8SDave Airlie MODULE_DEVICE_TABLE(virtio, id_table);
97dc5698e8SDave Airlie MODULE_DESCRIPTION("Virtio GPU driver");
98dc5698e8SDave Airlie MODULE_LICENSE("GPL and additional rights");
99dc5698e8SDave Airlie MODULE_AUTHOR("Dave Airlie <airlied@redhat.com>");
100dc5698e8SDave Airlie MODULE_AUTHOR("Gerd Hoffmann <kraxel@redhat.com>");
101dc5698e8SDave Airlie MODULE_AUTHOR("Alon Levy");
102dc5698e8SDave Airlie 
103dc5698e8SDave Airlie static const struct file_operations virtio_gpu_driver_fops = {
104dc5698e8SDave Airlie 	.owner = THIS_MODULE,
105dc5698e8SDave Airlie 	.open = drm_open,
106dc5698e8SDave Airlie 	.mmap = virtio_gpu_mmap,
107dc5698e8SDave Airlie 	.poll = drm_poll,
108dc5698e8SDave Airlie 	.read = drm_read,
109dc5698e8SDave Airlie 	.unlocked_ioctl	= drm_ioctl,
110dc5698e8SDave Airlie 	.release = drm_release,
111dc5698e8SDave Airlie #ifdef CONFIG_COMPAT
112dc5698e8SDave Airlie 	.compat_ioctl = drm_compat_ioctl,
113dc5698e8SDave Airlie #endif
114dc5698e8SDave Airlie 	.llseek = noop_llseek,
115dc5698e8SDave Airlie };
116dc5698e8SDave Airlie 
117dc5698e8SDave Airlie 
118dc5698e8SDave Airlie static struct drm_driver driver = {
1195443ce86SRob Herring 	.driver_features = DRIVER_MODESET | DRIVER_GEM | DRIVER_PRIME | DRIVER_RENDER | DRIVER_ATOMIC,
120dc5698e8SDave Airlie 	.set_busid = drm_virtio_set_busid,
121dc5698e8SDave Airlie 	.load = virtio_gpu_driver_load,
122dc5698e8SDave Airlie 	.unload = virtio_gpu_driver_unload,
12362fb7a5eSGerd Hoffmann 	.open = virtio_gpu_driver_open,
12462fb7a5eSGerd Hoffmann 	.postclose = virtio_gpu_driver_postclose,
125dc5698e8SDave Airlie 
126dc5698e8SDave Airlie 	.dumb_create = virtio_gpu_mode_dumb_create,
127dc5698e8SDave Airlie 	.dumb_map_offset = virtio_gpu_mode_dumb_mmap,
128dc5698e8SDave Airlie 	.dumb_destroy = virtio_gpu_mode_dumb_destroy,
129dc5698e8SDave Airlie 
130dc5698e8SDave Airlie #if defined(CONFIG_DEBUG_FS)
131dc5698e8SDave Airlie 	.debugfs_init = virtio_gpu_debugfs_init,
132dc5698e8SDave Airlie 	.debugfs_cleanup = virtio_gpu_debugfs_takedown,
133dc5698e8SDave Airlie #endif
13411a8f280SDave Airlie 	.prime_handle_to_fd = drm_gem_prime_handle_to_fd,
13511a8f280SDave Airlie 	.prime_fd_to_handle = drm_gem_prime_fd_to_handle,
13611a8f280SDave Airlie 	.gem_prime_export = drm_gem_prime_export,
13711a8f280SDave Airlie 	.gem_prime_import = drm_gem_prime_import,
13811a8f280SDave Airlie 	.gem_prime_pin = virtgpu_gem_prime_pin,
13911a8f280SDave Airlie 	.gem_prime_unpin = virtgpu_gem_prime_unpin,
14011a8f280SDave Airlie 	.gem_prime_get_sg_table = virtgpu_gem_prime_get_sg_table,
14111a8f280SDave Airlie 	.gem_prime_import_sg_table = virtgpu_gem_prime_import_sg_table,
14211a8f280SDave Airlie 	.gem_prime_vmap = virtgpu_gem_prime_vmap,
14311a8f280SDave Airlie 	.gem_prime_vunmap = virtgpu_gem_prime_vunmap,
14411a8f280SDave Airlie 	.gem_prime_mmap = virtgpu_gem_prime_mmap,
145dc5698e8SDave Airlie 
146bd884b74SDaniel Vetter 	.gem_free_object_unlocked = virtio_gpu_gem_free_object,
14762fb7a5eSGerd Hoffmann 	.gem_open_object = virtio_gpu_gem_object_open,
14862fb7a5eSGerd Hoffmann 	.gem_close_object = virtio_gpu_gem_object_close,
149dc5698e8SDave Airlie 	.fops = &virtio_gpu_driver_fops,
150dc5698e8SDave Airlie 
15162fb7a5eSGerd Hoffmann 	.ioctls = virtio_gpu_ioctls,
15262fb7a5eSGerd Hoffmann 	.num_ioctls = DRM_VIRTIO_NUM_IOCTLS,
15362fb7a5eSGerd Hoffmann 
154dc5698e8SDave Airlie 	.name = DRIVER_NAME,
155dc5698e8SDave Airlie 	.desc = DRIVER_DESC,
156dc5698e8SDave Airlie 	.date = DRIVER_DATE,
157dc5698e8SDave Airlie 	.major = DRIVER_MAJOR,
158dc5698e8SDave Airlie 	.minor = DRIVER_MINOR,
159dc5698e8SDave Airlie 	.patchlevel = DRIVER_PATCHLEVEL,
160dc5698e8SDave Airlie };
161