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