1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright 2021 Microsoft
7 #include <linux/hyperv.h>
8 #include <linux/module.h>
10 #include <linux/screen_info.h>
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>
19 #include "hyperv_drm.h"
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
27 DEFINE_DRM_GEM_FOPS(hv_fops);
29 static struct drm_driver hyperv_driver = {
30 .driver_features = DRIVER_MODESET | DRIVER_GEM | DRIVER_ATOMIC,
35 .major = DRIVER_MAJOR,
36 .minor = DRIVER_MINOR,
39 DRM_GEM_SHMEM_DRIVER_OPS,
42 static int hyperv_pci_probe(struct pci_dev *pdev,
43 const struct pci_device_id *ent)
48 static void hyperv_pci_remove(struct pci_dev *pdev)
52 static const struct pci_device_id hyperv_pci_tbl[] = {
54 .vendor = PCI_VENDOR_ID_MICROSOFT,
55 .device = PCI_DEVICE_ID_HYPERV_VIDEO,
61 * PCI stub to support gen1 VM.
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,
70 static int hyperv_setup_vram(struct hyperv_drm_device *hv,
71 struct hv_device *hdev)
73 struct drm_device *dev = &hv->dev;
76 if (IS_ENABLED(CONFIG_SYSFB))
77 drm_aperture_remove_conflicting_framebuffers(screen_info.lfb_base,
81 hv->fb_size = (unsigned long)hv->mmio_megabytes * 1024 * 1024;
83 ret = vmbus_allocate_mmio(&hv->mem, hdev, 0, -1, hv->fb_size, 0x100000,
86 drm_err(dev, "Failed to allocate mmio\n");
91 * Map the VRAM cacheable for performance. This is also required for VM
92 * connect to display properly for ARM64 Linux VM, as the host also maps
95 hv->vram = ioremap_cache(hv->mem->start, hv->fb_size);
97 drm_err(dev, "Failed to map vram\n");
102 hv->fb_base = hv->mem->start;
106 vmbus_free_mmio(hv->mem->start, hv->fb_size);
110 static int hyperv_vmbus_probe(struct hv_device *hdev,
111 const struct hv_vmbus_device_id *dev_id)
113 struct hyperv_drm_device *hv;
114 struct drm_device *dev;
117 hv = devm_drm_dev_alloc(&hdev->device, &hyperv_driver,
118 struct hyperv_drm_device, dev);
123 init_completion(&hv->wait);
124 hv_set_drvdata(hdev, hv);
127 ret = hyperv_connect_vsp(hdev);
129 drm_err(dev, "Failed to connect to vmbus.\n");
130 goto err_hv_set_drv_data;
133 ret = hyperv_setup_vram(hv, hdev);
135 goto err_vmbus_close;
138 * Should be done only once during init and resume. Failing to update
139 * vram location is not fatal. Device will update dirty area till
140 * preferred resolution only.
142 ret = hyperv_update_vram_location(hdev, hv->fb_base);
144 drm_warn(dev, "Failed to update vram location.\n");
146 ret = hyperv_mode_config_init(hv);
150 ret = drm_dev_register(dev, 0);
152 drm_err(dev, "Failed to register drm driver.\n");
156 drm_fbdev_generic_setup(dev, 0);
161 vmbus_free_mmio(hv->mem->start, hv->fb_size);
163 vmbus_close(hdev->channel);
165 hv_set_drvdata(hdev, NULL);
169 static void hyperv_vmbus_remove(struct hv_device *hdev)
171 struct drm_device *dev = hv_get_drvdata(hdev);
172 struct hyperv_drm_device *hv = to_hv(dev);
175 drm_atomic_helper_shutdown(dev);
176 vmbus_close(hdev->channel);
177 hv_set_drvdata(hdev, NULL);
179 vmbus_free_mmio(hv->mem->start, hv->fb_size);
182 static void hyperv_vmbus_shutdown(struct hv_device *hdev)
184 drm_atomic_helper_shutdown(hv_get_drvdata(hdev));
187 static int hyperv_vmbus_suspend(struct hv_device *hdev)
189 struct drm_device *dev = hv_get_drvdata(hdev);
192 ret = drm_mode_config_helper_suspend(dev);
196 vmbus_close(hdev->channel);
201 static int hyperv_vmbus_resume(struct hv_device *hdev)
203 struct drm_device *dev = hv_get_drvdata(hdev);
204 struct hyperv_drm_device *hv = to_hv(dev);
207 ret = hyperv_connect_vsp(hdev);
211 ret = hyperv_update_vram_location(hdev, hv->fb_base);
215 return drm_mode_config_helper_resume(dev);
218 static const struct hv_vmbus_device_id hyperv_vmbus_tbl[] = {
219 /* Synthetic Video Device GUID */
224 static struct hv_driver hyperv_hv_driver = {
225 .name = KBUILD_MODNAME,
226 .id_table = hyperv_vmbus_tbl,
227 .probe = hyperv_vmbus_probe,
228 .remove = hyperv_vmbus_remove,
229 .shutdown = hyperv_vmbus_shutdown,
230 .suspend = hyperv_vmbus_suspend,
231 .resume = hyperv_vmbus_resume,
233 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
237 static int __init hyperv_init(void)
241 if (drm_firmware_drivers_only())
244 ret = pci_register_driver(&hyperv_pci_driver);
248 return vmbus_driver_register(&hyperv_hv_driver);
251 static void __exit hyperv_exit(void)
253 vmbus_driver_unregister(&hyperv_hv_driver);
254 pci_unregister_driver(&hyperv_pci_driver);
257 module_init(hyperv_init);
258 module_exit(hyperv_exit);
260 MODULE_DEVICE_TABLE(pci, hyperv_pci_tbl);
261 MODULE_DEVICE_TABLE(vmbus, hyperv_vmbus_tbl);
262 MODULE_LICENSE("GPL");
264 MODULE_DESCRIPTION("DRM driver for Hyper-V synthetic video device");