xref: /linux/drivers/gpu/drm/hyperv/hyperv_drm_drv.c (revision d639d9fa162aadec1ae9980c4dcf6e50bd2f8290)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright 2021 Microsoft
4  */
5 
6 #include <linux/aperture.h>
7 #include <linux/efi.h>
8 #include <linux/hyperv.h>
9 #include <linux/module.h>
10 #include <linux/pci.h>
11 
12 #include <drm/clients/drm_client_setup.h>
13 #include <drm/drm_atomic_helper.h>
14 #include <drm/drm_drv.h>
15 #include <drm/drm_fbdev_shmem.h>
16 #include <drm/drm_gem_shmem_helper.h>
17 #include <drm/drm_print.h>
18 #include <drm/drm_simple_kms_helper.h>
19 
20 #include "hyperv_drm.h"
21 
22 #define DRIVER_NAME "hyperv_drm"
23 #define DRIVER_DESC "DRM driver for Hyper-V synthetic video device"
24 #define DRIVER_MAJOR 1
25 #define DRIVER_MINOR 0
26 
27 DEFINE_DRM_GEM_FOPS(hv_fops);
28 
29 static struct drm_driver hyperv_driver = {
30 	.driver_features = DRIVER_MODESET | DRIVER_GEM | DRIVER_ATOMIC,
31 
32 	.name		 = DRIVER_NAME,
33 	.desc		 = DRIVER_DESC,
34 	.major		 = DRIVER_MAJOR,
35 	.minor		 = DRIVER_MINOR,
36 
37 	.fops		 = &hv_fops,
38 	DRM_GEM_SHMEM_DRIVER_OPS,
39 	DRM_FBDEV_SHMEM_DRIVER_OPS,
40 };
41 
42 static int hyperv_pci_probe(struct pci_dev *pdev,
43 			    const struct pci_device_id *ent)
44 {
45 	return 0;
46 }
47 
48 static void hyperv_pci_remove(struct pci_dev *pdev)
49 {
50 }
51 
52 static const struct pci_device_id hyperv_pci_tbl[] = {
53 	{
54 		.vendor = PCI_VENDOR_ID_MICROSOFT,
55 		.device = PCI_DEVICE_ID_HYPERV_VIDEO,
56 	},
57 	{ /* end of list */ }
58 };
59 
60 /*
61  * PCI stub to support gen1 VM.
62  */
63 static struct pci_driver hyperv_pci_driver = {
64 	.name =		KBUILD_MODNAME,
65 	.id_table =	hyperv_pci_tbl,
66 	.probe =	hyperv_pci_probe,
67 	.remove =	hyperv_pci_remove,
68 };
69 
70 static int hyperv_setup_vram(struct hyperv_drm_device *hv,
71 			     struct hv_device *hdev)
72 {
73 	struct drm_device *dev = &hv->dev;
74 	int ret;
75 
76 	hv->fb_size = (unsigned long)hv->mmio_megabytes * 1024 * 1024;
77 
78 	ret = vmbus_allocate_mmio(&hv->mem, hdev, 0, -1, hv->fb_size, 0x100000,
79 				  true);
80 	if (ret) {
81 		drm_err(dev, "Failed to allocate mmio\n");
82 		return -ENOMEM;
83 	}
84 
85 	/*
86 	 * Map the VRAM cacheable for performance. This is also required for VM
87 	 * connect to display properly for ARM64 Linux VM, as the host also maps
88 	 * the VRAM cacheable.
89 	 */
90 	hv->vram = ioremap_cache(hv->mem->start, hv->fb_size);
91 	if (!hv->vram) {
92 		drm_err(dev, "Failed to map vram\n");
93 		ret = -ENOMEM;
94 		goto error;
95 	}
96 
97 	hv->fb_base = hv->mem->start;
98 	return 0;
99 
100 error:
101 	vmbus_free_mmio(hv->mem->start, hv->fb_size);
102 	return ret;
103 }
104 
105 static int hyperv_vmbus_probe(struct hv_device *hdev,
106 			      const struct hv_vmbus_device_id *dev_id)
107 {
108 	struct hyperv_drm_device *hv;
109 	struct drm_device *dev;
110 	int ret;
111 
112 	hv = devm_drm_dev_alloc(&hdev->device, &hyperv_driver,
113 				struct hyperv_drm_device, dev);
114 	if (IS_ERR(hv))
115 		return PTR_ERR(hv);
116 
117 	dev = &hv->dev;
118 	init_completion(&hv->wait);
119 	hv_set_drvdata(hdev, hv);
120 	hv->hdev = hdev;
121 
122 	ret = hyperv_connect_vsp(hdev);
123 	if (ret) {
124 		drm_err(dev, "Failed to connect to vmbus.\n");
125 		goto err_hv_set_drv_data;
126 	}
127 
128 	aperture_remove_all_conflicting_devices(hyperv_driver.name);
129 
130 	ret = hyperv_setup_vram(hv, hdev);
131 	if (ret)
132 		goto err_vmbus_close;
133 
134 	/*
135 	 * Should be done only once during init and resume. Failing to update
136 	 * vram location is not fatal. Device will update dirty area till
137 	 * preferred resolution only.
138 	 */
139 	ret = hyperv_update_vram_location(hdev, hv->fb_base);
140 	if (ret)
141 		drm_warn(dev, "Failed to update vram location.\n");
142 
143 	ret = hyperv_mode_config_init(hv);
144 	if (ret)
145 		goto err_free_mmio;
146 
147 	ret = drm_dev_register(dev, 0);
148 	if (ret) {
149 		drm_err(dev, "Failed to register drm driver.\n");
150 		goto err_free_mmio;
151 	}
152 
153 	/* If DRM panic path is stubbed out VMBus code must do the unload */
154 	if (IS_ENABLED(CONFIG_DRM_PANIC))
155 		vmbus_set_skip_unload(true);
156 
157 	drm_client_setup(dev, NULL);
158 
159 	return 0;
160 
161 err_free_mmio:
162 	iounmap(hv->vram);
163 	vmbus_free_mmio(hv->mem->start, hv->fb_size);
164 err_vmbus_close:
165 	vmbus_close(hdev->channel);
166 err_hv_set_drv_data:
167 	hv_set_drvdata(hdev, NULL);
168 	return ret;
169 }
170 
171 static void hyperv_vmbus_remove(struct hv_device *hdev)
172 {
173 	struct drm_device *dev = hv_get_drvdata(hdev);
174 	struct hyperv_drm_device *hv = to_hv(dev);
175 
176 	vmbus_set_skip_unload(false);
177 	drm_dev_unplug(dev);
178 	drm_atomic_helper_shutdown(dev);
179 	vmbus_close(hdev->channel);
180 	hv_set_drvdata(hdev, NULL);
181 
182 	iounmap(hv->vram);
183 	vmbus_free_mmio(hv->mem->start, hv->fb_size);
184 }
185 
186 static void hyperv_vmbus_shutdown(struct hv_device *hdev)
187 {
188 	drm_atomic_helper_shutdown(hv_get_drvdata(hdev));
189 }
190 
191 static int hyperv_vmbus_suspend(struct hv_device *hdev)
192 {
193 	struct drm_device *dev = hv_get_drvdata(hdev);
194 	int ret;
195 
196 	ret = drm_mode_config_helper_suspend(dev);
197 	if (ret)
198 		return ret;
199 
200 	vmbus_close(hdev->channel);
201 
202 	return 0;
203 }
204 
205 static int hyperv_vmbus_resume(struct hv_device *hdev)
206 {
207 	struct drm_device *dev = hv_get_drvdata(hdev);
208 	struct hyperv_drm_device *hv = to_hv(dev);
209 	int ret;
210 
211 	ret = hyperv_connect_vsp(hdev);
212 	if (ret)
213 		return ret;
214 
215 	ret = hyperv_update_vram_location(hdev, hv->fb_base);
216 	if (ret)
217 		return ret;
218 
219 	return drm_mode_config_helper_resume(dev);
220 }
221 
222 static const struct hv_vmbus_device_id hyperv_vmbus_tbl[] = {
223 	/* Synthetic Video Device GUID */
224 	{HV_SYNTHVID_GUID},
225 	{}
226 };
227 
228 static struct hv_driver hyperv_hv_driver = {
229 	.name = KBUILD_MODNAME,
230 	.id_table = hyperv_vmbus_tbl,
231 	.probe = hyperv_vmbus_probe,
232 	.remove = hyperv_vmbus_remove,
233 	.shutdown = hyperv_vmbus_shutdown,
234 	.suspend = hyperv_vmbus_suspend,
235 	.resume = hyperv_vmbus_resume,
236 	.driver = {
237 		.probe_type = PROBE_PREFER_ASYNCHRONOUS,
238 	},
239 };
240 
241 static int __init hyperv_init(void)
242 {
243 	int ret;
244 
245 	if (drm_firmware_drivers_only())
246 		return -ENODEV;
247 
248 	ret = pci_register_driver(&hyperv_pci_driver);
249 	if (ret != 0)
250 		return ret;
251 
252 	return vmbus_driver_register(&hyperv_hv_driver);
253 }
254 
255 static void __exit hyperv_exit(void)
256 {
257 	vmbus_driver_unregister(&hyperv_hv_driver);
258 	pci_unregister_driver(&hyperv_pci_driver);
259 }
260 
261 module_init(hyperv_init);
262 module_exit(hyperv_exit);
263 
264 MODULE_DEVICE_TABLE(pci, hyperv_pci_tbl);
265 MODULE_DEVICE_TABLE(vmbus, hyperv_vmbus_tbl);
266 MODULE_LICENSE("GPL");
267 MODULE_AUTHOR("Deepak Rawat <drawat.floss@gmail.com>");
268 MODULE_DESCRIPTION("DRM driver for Hyper-V synthetic video device");
269