1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright 2021 Microsoft
7 #include <linux/hyperv.h>
8 #include <linux/module.h>
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>
18 #include "hyperv_drm.h"
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
26 #define PCI_VENDOR_ID_MICROSOFT 0x1414
27 #define PCI_DEVICE_ID_HYPERV_VIDEO 0x5353
29 DEFINE_DRM_GEM_FOPS(hv_fops);
31 static struct drm_driver hyperv_driver = {
32 .driver_features = DRIVER_MODESET | DRIVER_GEM | DRIVER_ATOMIC,
37 .major = DRIVER_MAJOR,
38 .minor = DRIVER_MINOR,
41 DRM_GEM_SHMEM_DRIVER_OPS,
44 static int hyperv_pci_probe(struct pci_dev *pdev,
45 const struct pci_device_id *ent)
50 static void hyperv_pci_remove(struct pci_dev *pdev)
54 static const struct pci_device_id hyperv_pci_tbl[] = {
56 .vendor = PCI_VENDOR_ID_MICROSOFT,
57 .device = PCI_DEVICE_ID_HYPERV_VIDEO,
63 * PCI stub to support gen1 VM.
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,
72 static int hyperv_setup_gen1(struct hyperv_drm_device *hv)
74 struct drm_device *dev = &hv->dev;
78 pdev = pci_get_device(PCI_VENDOR_ID_MICROSOFT,
79 PCI_DEVICE_ID_HYPERV_VIDEO, NULL);
81 drm_err(dev, "Unable to find PCI Hyper-V video\n");
85 ret = drm_aperture_remove_conflicting_pci_framebuffers(pdev, &hyperv_driver);
87 drm_err(dev, "Not able to remove boot fb\n");
91 if (pci_request_region(pdev, 0, DRIVER_NAME) != 0)
92 drm_warn(dev, "Cannot request framebuffer, boot fb still active?\n");
94 if ((pdev->resource[0].flags & IORESOURCE_MEM) == 0) {
95 drm_err(dev, "Resource at bar 0 is not IORESOURCE_MEM\n");
100 hv->fb_base = pci_resource_start(pdev, 0);
101 hv->fb_size = pci_resource_len(pdev, 0);
103 drm_err(dev, "Resource not available\n");
108 hv->fb_size = min(hv->fb_size,
109 (unsigned long)(hv->mmio_megabytes * 1024 * 1024));
110 hv->vram = devm_ioremap(&pdev->dev, hv->fb_base, hv->fb_size);
112 drm_err(dev, "Failed to map vram\n");
121 static int hyperv_setup_gen2(struct hyperv_drm_device *hv,
122 struct hv_device *hdev)
124 struct drm_device *dev = &hv->dev;
127 drm_aperture_remove_conflicting_framebuffers(screen_info.lfb_base,
128 screen_info.lfb_size,
132 hv->fb_size = (unsigned long)hv->mmio_megabytes * 1024 * 1024;
134 ret = vmbus_allocate_mmio(&hv->mem, hdev, 0, -1, hv->fb_size, 0x100000,
137 drm_err(dev, "Failed to allocate mmio\n");
142 * Map the VRAM cacheable for performance. This is also required for VM
143 * connect to display properly for ARM64 Linux VM, as the host also maps
144 * the VRAM cacheable.
146 hv->vram = ioremap_cache(hv->mem->start, hv->fb_size);
148 drm_err(dev, "Failed to map vram\n");
153 hv->fb_base = hv->mem->start;
157 vmbus_free_mmio(hv->mem->start, hv->fb_size);
161 static int hyperv_vmbus_probe(struct hv_device *hdev,
162 const struct hv_vmbus_device_id *dev_id)
164 struct hyperv_drm_device *hv;
165 struct drm_device *dev;
168 hv = devm_drm_dev_alloc(&hdev->device, &hyperv_driver,
169 struct hyperv_drm_device, dev);
174 init_completion(&hv->wait);
175 hv_set_drvdata(hdev, hv);
178 ret = hyperv_connect_vsp(hdev);
180 drm_err(dev, "Failed to connect to vmbus.\n");
181 goto err_hv_set_drv_data;
184 if (efi_enabled(EFI_BOOT))
185 ret = hyperv_setup_gen2(hv, hdev);
187 ret = hyperv_setup_gen1(hv);
190 goto err_vmbus_close;
193 * Should be done only once during init and resume. Failing to update
194 * vram location is not fatal. Device will update dirty area till
195 * preferred resolution only.
197 ret = hyperv_update_vram_location(hdev, hv->fb_base);
199 drm_warn(dev, "Failed to update vram location.\n");
201 hv->dirt_needed = true;
203 ret = hyperv_mode_config_init(hv);
205 goto err_vmbus_close;
207 ret = drm_dev_register(dev, 0);
209 drm_err(dev, "Failed to register drm driver.\n");
210 goto err_vmbus_close;
213 drm_fbdev_generic_setup(dev, 0);
218 vmbus_close(hdev->channel);
220 hv_set_drvdata(hdev, NULL);
224 static int hyperv_vmbus_remove(struct hv_device *hdev)
226 struct drm_device *dev = hv_get_drvdata(hdev);
227 struct hyperv_drm_device *hv = to_hv(dev);
228 struct pci_dev *pdev;
231 drm_atomic_helper_shutdown(dev);
232 vmbus_close(hdev->channel);
233 hv_set_drvdata(hdev, NULL);
236 * Free allocated MMIO memory only on Gen2 VMs.
237 * On Gen1 VMs, release the PCI device
239 if (efi_enabled(EFI_BOOT)) {
240 vmbus_free_mmio(hv->mem->start, hv->fb_size);
242 pdev = pci_get_device(PCI_VENDOR_ID_MICROSOFT,
243 PCI_DEVICE_ID_HYPERV_VIDEO, NULL);
245 drm_err(dev, "Unable to find PCI Hyper-V video\n");
248 pci_release_region(pdev, 0);
255 static int hyperv_vmbus_suspend(struct hv_device *hdev)
257 struct drm_device *dev = hv_get_drvdata(hdev);
260 ret = drm_mode_config_helper_suspend(dev);
264 vmbus_close(hdev->channel);
269 static int hyperv_vmbus_resume(struct hv_device *hdev)
271 struct drm_device *dev = hv_get_drvdata(hdev);
272 struct hyperv_drm_device *hv = to_hv(dev);
275 ret = hyperv_connect_vsp(hdev);
279 ret = hyperv_update_vram_location(hdev, hv->fb_base);
283 return drm_mode_config_helper_resume(dev);
286 static const struct hv_vmbus_device_id hyperv_vmbus_tbl[] = {
287 /* Synthetic Video Device GUID */
292 static struct hv_driver hyperv_hv_driver = {
293 .name = KBUILD_MODNAME,
294 .id_table = hyperv_vmbus_tbl,
295 .probe = hyperv_vmbus_probe,
296 .remove = hyperv_vmbus_remove,
297 .suspend = hyperv_vmbus_suspend,
298 .resume = hyperv_vmbus_resume,
300 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
304 static int __init hyperv_init(void)
308 ret = pci_register_driver(&hyperv_pci_driver);
312 return vmbus_driver_register(&hyperv_hv_driver);
315 static void __exit hyperv_exit(void)
317 vmbus_driver_unregister(&hyperv_hv_driver);
318 pci_unregister_driver(&hyperv_pci_driver);
321 module_init(hyperv_init);
322 module_exit(hyperv_exit);
324 MODULE_DEVICE_TABLE(pci, hyperv_pci_tbl);
325 MODULE_DEVICE_TABLE(vmbus, hyperv_vmbus_tbl);
326 MODULE_LICENSE("GPL");
328 MODULE_DESCRIPTION("DRM driver for Hyper-V synthetic video device");