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