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_DRM_GEM_FOPS(hv_fops); 27 28 static struct drm_driver hyperv_driver = { 29 .driver_features = DRIVER_MODESET | DRIVER_GEM | DRIVER_ATOMIC, 30 31 .name = DRIVER_NAME, 32 .desc = DRIVER_DESC, 33 .date = DRIVER_DATE, 34 .major = DRIVER_MAJOR, 35 .minor = DRIVER_MINOR, 36 37 .fops = &hv_fops, 38 DRM_GEM_SHMEM_DRIVER_OPS, 39 }; 40 41 static int hyperv_pci_probe(struct pci_dev *pdev, 42 const struct pci_device_id *ent) 43 { 44 return 0; 45 } 46 47 static void hyperv_pci_remove(struct pci_dev *pdev) 48 { 49 } 50 51 static const struct pci_device_id hyperv_pci_tbl[] = { 52 { 53 .vendor = PCI_VENDOR_ID_MICROSOFT, 54 .device = PCI_DEVICE_ID_HYPERV_VIDEO, 55 }, 56 { /* end of list */ } 57 }; 58 59 /* 60 * PCI stub to support gen1 VM. 61 */ 62 static struct pci_driver hyperv_pci_driver = { 63 .name = KBUILD_MODNAME, 64 .id_table = hyperv_pci_tbl, 65 .probe = hyperv_pci_probe, 66 .remove = hyperv_pci_remove, 67 }; 68 69 static int hyperv_setup_vram(struct hyperv_drm_device *hv, 70 struct hv_device *hdev) 71 { 72 struct drm_device *dev = &hv->dev; 73 int ret; 74 75 drm_aperture_remove_conflicting_framebuffers(screen_info.lfb_base, 76 screen_info.lfb_size, 77 false, 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 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 hv->dirt_needed = true; 146 147 ret = hyperv_mode_config_init(hv); 148 if (ret) 149 goto err_free_mmio; 150 151 ret = drm_dev_register(dev, 0); 152 if (ret) { 153 drm_err(dev, "Failed to register drm driver.\n"); 154 goto err_free_mmio; 155 } 156 157 drm_fbdev_generic_setup(dev, 0); 158 159 return 0; 160 161 err_free_mmio: 162 vmbus_free_mmio(hv->mem->start, hv->fb_size); 163 err_vmbus_close: 164 vmbus_close(hdev->channel); 165 err_hv_set_drv_data: 166 hv_set_drvdata(hdev, NULL); 167 return ret; 168 } 169 170 static int hyperv_vmbus_remove(struct hv_device *hdev) 171 { 172 struct drm_device *dev = hv_get_drvdata(hdev); 173 struct hyperv_drm_device *hv = to_hv(dev); 174 175 drm_dev_unplug(dev); 176 drm_atomic_helper_shutdown(dev); 177 vmbus_close(hdev->channel); 178 hv_set_drvdata(hdev, NULL); 179 180 vmbus_free_mmio(hv->mem->start, hv->fb_size); 181 182 return 0; 183 } 184 185 static int hyperv_vmbus_suspend(struct hv_device *hdev) 186 { 187 struct drm_device *dev = hv_get_drvdata(hdev); 188 int ret; 189 190 ret = drm_mode_config_helper_suspend(dev); 191 if (ret) 192 return ret; 193 194 vmbus_close(hdev->channel); 195 196 return 0; 197 } 198 199 static int hyperv_vmbus_resume(struct hv_device *hdev) 200 { 201 struct drm_device *dev = hv_get_drvdata(hdev); 202 struct hyperv_drm_device *hv = to_hv(dev); 203 int ret; 204 205 ret = hyperv_connect_vsp(hdev); 206 if (ret) 207 return ret; 208 209 ret = hyperv_update_vram_location(hdev, hv->fb_base); 210 if (ret) 211 return ret; 212 213 return drm_mode_config_helper_resume(dev); 214 } 215 216 static const struct hv_vmbus_device_id hyperv_vmbus_tbl[] = { 217 /* Synthetic Video Device GUID */ 218 {HV_SYNTHVID_GUID}, 219 {} 220 }; 221 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 .suspend = hyperv_vmbus_suspend, 228 .resume = hyperv_vmbus_resume, 229 .driver = { 230 .probe_type = PROBE_PREFER_ASYNCHRONOUS, 231 }, 232 }; 233 234 static int __init hyperv_init(void) 235 { 236 int ret; 237 238 if (drm_firmware_drivers_only()) 239 return -ENODEV; 240 241 ret = pci_register_driver(&hyperv_pci_driver); 242 if (ret != 0) 243 return ret; 244 245 return vmbus_driver_register(&hyperv_hv_driver); 246 } 247 248 static void __exit hyperv_exit(void) 249 { 250 vmbus_driver_unregister(&hyperv_hv_driver); 251 pci_unregister_driver(&hyperv_pci_driver); 252 } 253 254 module_init(hyperv_init); 255 module_exit(hyperv_exit); 256 257 MODULE_DEVICE_TABLE(pci, hyperv_pci_tbl); 258 MODULE_DEVICE_TABLE(vmbus, hyperv_vmbus_tbl); 259 MODULE_LICENSE("GPL"); 260 MODULE_AUTHOR("Deepak Rawat <drawat.floss@gmail.com>"); 261 MODULE_DESCRIPTION("DRM driver for Hyper-V synthetic video device"); 262