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