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