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