1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright 2021 Microsoft
6 #include <linux/aperture.h>
8 #include <linux/hyperv.h>
9 #include <linux/module.h>
10 #include <linux/pci.h>
12 #include <drm/drm_atomic_helper.h>
13 #include <drm/drm_client_setup.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_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,
40 DRM_FBDEV_SHMEM_DRIVER_OPS,
43 static int hyperv_pci_probe(struct pci_dev *pdev,
44 const struct pci_device_id *ent)
49 static void hyperv_pci_remove(struct pci_dev *pdev)
53 static const struct pci_device_id hyperv_pci_tbl[] = {
55 .vendor = PCI_VENDOR_ID_MICROSOFT,
56 .device = PCI_DEVICE_ID_HYPERV_VIDEO,
62 * PCI stub to support gen1 VM.
64 static struct pci_driver hyperv_pci_driver = {
65 .name = KBUILD_MODNAME,
66 .id_table = hyperv_pci_tbl,
67 .probe = hyperv_pci_probe,
68 .remove = hyperv_pci_remove,
71 static int hyperv_setup_vram(struct hyperv_drm_device *hv,
72 struct hv_device *hdev)
74 struct drm_device *dev = &hv->dev;
77 hv->fb_size = (unsigned long)hv->mmio_megabytes * 1024 * 1024;
79 ret = vmbus_allocate_mmio(&hv->mem, hdev, 0, -1, hv->fb_size, 0x100000,
82 drm_err(dev, "Failed to allocate mmio\n");
87 * Map the VRAM cacheable for performance. This is also required for VM
88 * connect to display properly for ARM64 Linux VM, as the host also maps
91 hv->vram = ioremap_cache(hv->mem->start, hv->fb_size);
93 drm_err(dev, "Failed to map vram\n");
98 hv->fb_base = hv->mem->start;
102 vmbus_free_mmio(hv->mem->start, hv->fb_size);
106 static int hyperv_vmbus_probe(struct hv_device *hdev,
107 const struct hv_vmbus_device_id *dev_id)
109 struct hyperv_drm_device *hv;
110 struct drm_device *dev;
113 hv = devm_drm_dev_alloc(&hdev->device, &hyperv_driver,
114 struct hyperv_drm_device, dev);
119 init_completion(&hv->wait);
120 hv_set_drvdata(hdev, hv);
123 ret = hyperv_connect_vsp(hdev);
125 drm_err(dev, "Failed to connect to vmbus.\n");
126 goto err_hv_set_drv_data;
129 aperture_remove_all_conflicting_devices(hyperv_driver.name);
131 ret = hyperv_setup_vram(hv, hdev);
133 goto err_vmbus_close;
136 * Should be done only once during init and resume. Failing to update
137 * vram location is not fatal. Device will update dirty area till
138 * preferred resolution only.
140 ret = hyperv_update_vram_location(hdev, hv->fb_base);
142 drm_warn(dev, "Failed to update vram location.\n");
144 ret = hyperv_mode_config_init(hv);
148 ret = drm_dev_register(dev, 0);
150 drm_err(dev, "Failed to register drm driver.\n");
154 drm_client_setup(dev, NULL);
159 vmbus_free_mmio(hv->mem->start, hv->fb_size);
161 vmbus_close(hdev->channel);
163 hv_set_drvdata(hdev, NULL);
167 static void hyperv_vmbus_remove(struct hv_device *hdev)
169 struct drm_device *dev = hv_get_drvdata(hdev);
170 struct hyperv_drm_device *hv = to_hv(dev);
173 drm_atomic_helper_shutdown(dev);
174 vmbus_close(hdev->channel);
175 hv_set_drvdata(hdev, NULL);
177 vmbus_free_mmio(hv->mem->start, hv->fb_size);
180 static void hyperv_vmbus_shutdown(struct hv_device *hdev)
182 drm_atomic_helper_shutdown(hv_get_drvdata(hdev));
185 static int hyperv_vmbus_suspend(struct hv_device *hdev)
187 struct drm_device *dev = hv_get_drvdata(hdev);
190 ret = drm_mode_config_helper_suspend(dev);
194 vmbus_close(hdev->channel);
199 static int hyperv_vmbus_resume(struct hv_device *hdev)
201 struct drm_device *dev = hv_get_drvdata(hdev);
202 struct hyperv_drm_device *hv = to_hv(dev);
205 ret = hyperv_connect_vsp(hdev);
209 ret = hyperv_update_vram_location(hdev, hv->fb_base);
213 return drm_mode_config_helper_resume(dev);
216 static const struct hv_vmbus_device_id hyperv_vmbus_tbl[] = {
217 /* Synthetic Video Device GUID */
222 static struct hv_driver hyperv_hv_driver = {
223 .name = KBUILD_MODNAME,
224 .id_table = hyperv_vmbus_tbl,
225 .probe = hyperv_vmbus_probe,
226 .remove = hyperv_vmbus_remove,
227 .shutdown = hyperv_vmbus_shutdown,
228 .suspend = hyperv_vmbus_suspend,
229 .resume = hyperv_vmbus_resume,
231 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
235 static int __init hyperv_init(void)
239 if (drm_firmware_drivers_only())
242 ret = pci_register_driver(&hyperv_pci_driver);
246 return vmbus_driver_register(&hyperv_hv_driver);
249 static void __exit hyperv_exit(void)
251 vmbus_driver_unregister(&hyperv_hv_driver);
252 pci_unregister_driver(&hyperv_pci_driver);
255 module_init(hyperv_init);
256 module_exit(hyperv_exit);
258 MODULE_DEVICE_TABLE(pci, hyperv_pci_tbl);
259 MODULE_DEVICE_TABLE(vmbus, hyperv_vmbus_tbl);
260 MODULE_LICENSE("GPL");
262 MODULE_DESCRIPTION("DRM driver for Hyper-V synthetic video device");