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>
32a3d63977SSam Ravnborg 
33ff67a25dSMasahiro Yamada #include <drm/drm.h>
346848c291SThomas Zimmermann #include <drm/drm_aperture.h>
35b1df3a2bSGerd Hoffmann #include <drm/drm_atomic_helper.h>
36a3d63977SSam Ravnborg #include <drm/drm_drv.h>
37a3d63977SSam Ravnborg #include <drm/drm_file.h>
38dc5698e8SDave Airlie 
39dc5698e8SDave Airlie #include "virtgpu_drv.h"
40a3d63977SSam Ravnborg 
4170a59dd8SDaniel Vetter static const struct drm_driver driver;
42dc5698e8SDave Airlie 
43dc5698e8SDave Airlie static int virtio_gpu_modeset = -1;
44dc5698e8SDave Airlie 
45dc5698e8SDave Airlie MODULE_PARM_DESC(modeset, "Disable/Enable modesetting");
46dc5698e8SDave Airlie module_param_named(modeset, virtio_gpu_modeset, int, 0400);
47dc5698e8SDave Airlie 
48d516e75cSEzequiel Garcia static int virtio_gpu_pci_quirk(struct drm_device *dev, struct virtio_device *vdev)
49d516e75cSEzequiel Garcia {
50d516e75cSEzequiel Garcia 	struct pci_dev *pdev = to_pci_dev(vdev->dev.parent);
51d516e75cSEzequiel Garcia 	const char *pname = dev_name(&pdev->dev);
52d516e75cSEzequiel Garcia 	bool vga = (pdev->class >> 8) == PCI_CLASS_DISPLAY_VGA;
53d516e75cSEzequiel Garcia 	char unique[20];
546848c291SThomas Zimmermann 	int ret;
55d516e75cSEzequiel Garcia 
56d516e75cSEzequiel Garcia 	DRM_INFO("pci: %s detected at %s\n",
57d516e75cSEzequiel Garcia 		 vga ? "virtio-vga" : "virtio-gpu-pci",
58d516e75cSEzequiel Garcia 		 pname);
596848c291SThomas Zimmermann 	if (vga) {
606848c291SThomas Zimmermann 		ret = drm_aperture_remove_conflicting_pci_framebuffers(pdev, "virtiodrmfb");
616848c291SThomas Zimmermann 		if (ret)
626848c291SThomas Zimmermann 			return ret;
636848c291SThomas Zimmermann 	}
64d516e75cSEzequiel Garcia 
65d516e75cSEzequiel Garcia 	/*
66d516e75cSEzequiel Garcia 	 * Normally the drm_dev_set_unique() call is done by core DRM.
67d516e75cSEzequiel Garcia 	 * The following comment covers, why virtio cannot rely on it.
68d516e75cSEzequiel Garcia 	 *
69d516e75cSEzequiel Garcia 	 * Unlike the other virtual GPU drivers, virtio abstracts the
70d516e75cSEzequiel Garcia 	 * underlying bus type by using struct virtio_device.
71d516e75cSEzequiel Garcia 	 *
72d516e75cSEzequiel Garcia 	 * Hence the dev_is_pci() check, used in core DRM, will fail
73d516e75cSEzequiel Garcia 	 * and the unique returned will be the virtio_device "virtio0",
74d516e75cSEzequiel Garcia 	 * while a "pci:..." one is required.
75d516e75cSEzequiel Garcia 	 *
76d516e75cSEzequiel Garcia 	 * A few other ideas were considered:
77d516e75cSEzequiel Garcia 	 * - Extend the dev_is_pci() check [in drm_set_busid] to
78d516e75cSEzequiel Garcia 	 *   consider virtio.
79d516e75cSEzequiel Garcia 	 *   Seems like a bigger hack than what we have already.
80d516e75cSEzequiel Garcia 	 *
81d516e75cSEzequiel Garcia 	 * - Point drm_device::dev to the parent of the virtio_device
82d516e75cSEzequiel Garcia 	 *   Semantic changes:
83d516e75cSEzequiel Garcia 	 *   * Using the wrong device for i2c, framebuffer_alloc and
84d516e75cSEzequiel Garcia 	 *     prime import.
85d516e75cSEzequiel Garcia 	 *   Visual changes:
86d516e75cSEzequiel Garcia 	 *   * Helpers such as DRM_DEV_ERROR, dev_info, drm_printer,
87d516e75cSEzequiel Garcia 	 *     will print the wrong information.
88d516e75cSEzequiel Garcia 	 *
89d516e75cSEzequiel Garcia 	 * We could address the latter issues, by introducing
90d516e75cSEzequiel Garcia 	 * drm_device::bus_dev, ... which would be used solely for this.
91d516e75cSEzequiel Garcia 	 *
92d516e75cSEzequiel Garcia 	 * So for the moment keep things as-is, with a bulky comment
93d516e75cSEzequiel Garcia 	 * for the next person who feels like removing this
94d516e75cSEzequiel Garcia 	 * drm_dev_set_unique() quirk.
95d516e75cSEzequiel Garcia 	 */
96d516e75cSEzequiel Garcia 	snprintf(unique, sizeof(unique), "pci:%s", pname);
97d516e75cSEzequiel Garcia 	return drm_dev_set_unique(dev, unique);
98d516e75cSEzequiel Garcia }
99d516e75cSEzequiel Garcia 
100dc5698e8SDave Airlie static int virtio_gpu_probe(struct virtio_device *vdev)
101dc5698e8SDave Airlie {
102d516e75cSEzequiel Garcia 	struct drm_device *dev;
10348a77d66SGerd Hoffmann 	int ret;
10448a77d66SGerd Hoffmann 
105dc5698e8SDave Airlie 	if (vgacon_text_force() && virtio_gpu_modeset == -1)
106dc5698e8SDave Airlie 		return -EINVAL;
107dc5698e8SDave Airlie 
108dc5698e8SDave Airlie 	if (virtio_gpu_modeset == 0)
109dc5698e8SDave Airlie 		return -EINVAL;
110dc5698e8SDave Airlie 
111d516e75cSEzequiel Garcia 	dev = drm_dev_alloc(&driver, &vdev->dev);
112d516e75cSEzequiel Garcia 	if (IS_ERR(dev))
113d516e75cSEzequiel Garcia 		return PTR_ERR(dev);
114d516e75cSEzequiel Garcia 	vdev->priv = dev;
115d516e75cSEzequiel Garcia 
116d516e75cSEzequiel Garcia 	if (!strcmp(vdev->dev.parent->bus->name, "pci")) {
117d516e75cSEzequiel Garcia 		ret = virtio_gpu_pci_quirk(dev, vdev);
11848a77d66SGerd Hoffmann 		if (ret)
119d516e75cSEzequiel Garcia 			goto err_free;
120d516e75cSEzequiel Garcia 	}
121d516e75cSEzequiel Garcia 
122d516e75cSEzequiel Garcia 	ret = virtio_gpu_init(dev);
123d516e75cSEzequiel Garcia 	if (ret)
124d516e75cSEzequiel Garcia 		goto err_free;
125d516e75cSEzequiel Garcia 
126d516e75cSEzequiel Garcia 	ret = drm_dev_register(dev, 0);
127d516e75cSEzequiel Garcia 	if (ret)
128*058acb33SXie Yongji 		goto err_deinit;
12948a77d66SGerd Hoffmann 
13048a77d66SGerd Hoffmann 	drm_fbdev_generic_setup(vdev->priv, 32);
13148a77d66SGerd Hoffmann 	return 0;
132d516e75cSEzequiel Garcia 
133*058acb33SXie Yongji err_deinit:
134*058acb33SXie Yongji 	virtio_gpu_deinit(dev);
135d516e75cSEzequiel Garcia err_free:
136d516e75cSEzequiel Garcia 	drm_dev_put(dev);
137d516e75cSEzequiel Garcia 	return ret;
138dc5698e8SDave Airlie }
139dc5698e8SDave Airlie 
140dc5698e8SDave Airlie static void virtio_gpu_remove(struct virtio_device *vdev)
141dc5698e8SDave Airlie {
142dc5698e8SDave Airlie 	struct drm_device *dev = vdev->priv;
1439d492b6bSRodrigo Siqueira 
144b1df3a2bSGerd Hoffmann 	drm_dev_unplug(dev);
145b1df3a2bSGerd Hoffmann 	drm_atomic_helper_shutdown(dev);
146d516e75cSEzequiel Garcia 	virtio_gpu_deinit(dev);
147c8f95a56SChuhong Yuan 	drm_dev_put(dev);
148dc5698e8SDave Airlie }
149dc5698e8SDave Airlie 
150dc5698e8SDave Airlie static void virtio_gpu_config_changed(struct virtio_device *vdev)
151dc5698e8SDave Airlie {
152dc5698e8SDave Airlie 	struct drm_device *dev = vdev->priv;
153dc5698e8SDave Airlie 	struct virtio_gpu_device *vgdev = dev->dev_private;
154dc5698e8SDave Airlie 
155dc5698e8SDave Airlie 	schedule_work(&vgdev->config_changed_work);
156dc5698e8SDave Airlie }
157dc5698e8SDave Airlie 
158dc5698e8SDave Airlie static struct virtio_device_id id_table[] = {
159dc5698e8SDave Airlie 	{ VIRTIO_ID_GPU, VIRTIO_DEV_ANY_ID },
160dc5698e8SDave Airlie 	{ 0 },
161dc5698e8SDave Airlie };
162dc5698e8SDave Airlie 
163dc5698e8SDave Airlie static unsigned int features[] = {
16462fb7a5eSGerd Hoffmann #ifdef __LITTLE_ENDIAN
16562fb7a5eSGerd Hoffmann 	/*
16662fb7a5eSGerd Hoffmann 	 * Gallium command stream send by virgl is native endian.
16762fb7a5eSGerd Hoffmann 	 * Because of that we only support little endian guests on
16862fb7a5eSGerd Hoffmann 	 * little endian hosts.
16962fb7a5eSGerd Hoffmann 	 */
17062fb7a5eSGerd Hoffmann 	VIRTIO_GPU_F_VIRGL,
17162fb7a5eSGerd Hoffmann #endif
172b4b01b49SGerd Hoffmann 	VIRTIO_GPU_F_EDID,
173c84adb30SDavid Stevens 	VIRTIO_GPU_F_RESOURCE_UUID,
1746815cfe6SGerd Hoffmann 	VIRTIO_GPU_F_RESOURCE_BLOB,
175dc5698e8SDave Airlie };
176dc5698e8SDave Airlie static struct virtio_driver virtio_gpu_driver = {
177dc5698e8SDave Airlie 	.feature_table = features,
178dc5698e8SDave Airlie 	.feature_table_size = ARRAY_SIZE(features),
179dc5698e8SDave Airlie 	.driver.name = KBUILD_MODNAME,
180dc5698e8SDave Airlie 	.driver.owner = THIS_MODULE,
181dc5698e8SDave Airlie 	.id_table = id_table,
182dc5698e8SDave Airlie 	.probe = virtio_gpu_probe,
183dc5698e8SDave Airlie 	.remove = virtio_gpu_remove,
184dc5698e8SDave Airlie 	.config_changed = virtio_gpu_config_changed
185dc5698e8SDave Airlie };
186dc5698e8SDave Airlie 
187dc5698e8SDave Airlie module_virtio_driver(virtio_gpu_driver);
188dc5698e8SDave Airlie 
189dc5698e8SDave Airlie MODULE_DEVICE_TABLE(virtio, id_table);
190dc5698e8SDave Airlie MODULE_DESCRIPTION("Virtio GPU driver");
191dc5698e8SDave Airlie MODULE_LICENSE("GPL and additional rights");
192dc5698e8SDave Airlie MODULE_AUTHOR("Dave Airlie <airlied@redhat.com>");
193dc5698e8SDave Airlie MODULE_AUTHOR("Gerd Hoffmann <kraxel@redhat.com>");
194dc5698e8SDave Airlie MODULE_AUTHOR("Alon Levy");
195dc5698e8SDave Airlie 
196eee9a2e0SGerd Hoffmann DEFINE_DRM_GEM_FOPS(virtio_gpu_driver_fops);
197dc5698e8SDave Airlie 
19870a59dd8SDaniel Vetter static const struct drm_driver driver = {
1990424fdafSDaniel Vetter 	.driver_features = DRIVER_MODESET | DRIVER_GEM | DRIVER_RENDER | DRIVER_ATOMIC,
20062fb7a5eSGerd Hoffmann 	.open = virtio_gpu_driver_open,
20162fb7a5eSGerd Hoffmann 	.postclose = virtio_gpu_driver_postclose,
202dc5698e8SDave Airlie 
203dc5698e8SDave Airlie 	.dumb_create = virtio_gpu_mode_dumb_create,
204dc5698e8SDave Airlie 	.dumb_map_offset = virtio_gpu_mode_dumb_mmap,
205dc5698e8SDave Airlie 
206dc5698e8SDave Airlie #if defined(CONFIG_DEBUG_FS)
207dc5698e8SDave Airlie 	.debugfs_init = virtio_gpu_debugfs_init,
208dc5698e8SDave Airlie #endif
209a0cecc23SDave Airlie 	.prime_handle_to_fd = drm_gem_prime_handle_to_fd,
210a0cecc23SDave Airlie 	.prime_fd_to_handle = drm_gem_prime_fd_to_handle,
21188c5a5a0SGerd Hoffmann 	.gem_prime_mmap = drm_gem_prime_mmap,
212c84adb30SDavid Stevens 	.gem_prime_import = virtgpu_gem_prime_import,
213a0cecc23SDave Airlie 	.gem_prime_import_sg_table = virtgpu_gem_prime_import_sg_table,
214dc5698e8SDave Airlie 
215c66df701SGerd Hoffmann 	.gem_create_object = virtio_gpu_create_object,
216dc5698e8SDave Airlie 	.fops = &virtio_gpu_driver_fops,
217dc5698e8SDave Airlie 
21862fb7a5eSGerd Hoffmann 	.ioctls = virtio_gpu_ioctls,
21962fb7a5eSGerd Hoffmann 	.num_ioctls = DRM_VIRTIO_NUM_IOCTLS,
22062fb7a5eSGerd Hoffmann 
221dc5698e8SDave Airlie 	.name = DRIVER_NAME,
222dc5698e8SDave Airlie 	.desc = DRIVER_DESC,
223dc5698e8SDave Airlie 	.date = DRIVER_DATE,
224dc5698e8SDave Airlie 	.major = DRIVER_MAJOR,
225dc5698e8SDave Airlie 	.minor = DRIVER_MINOR,
226dc5698e8SDave Airlie 	.patchlevel = DRIVER_PATCHLEVEL,
227b1df3a2bSGerd Hoffmann 
228b1df3a2bSGerd Hoffmann 	.release = virtio_gpu_release,
229dc5698e8SDave Airlie };
230