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