Loading...
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Copyright 2021 Microsoft
4 */
5
6#include <linux/aperture.h>
7#include <linux/efi.h>
8#include <linux/hyperv.h>
9#include <linux/module.h>
10#include <linux/pci.h>
11
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>
18
19#include "hyperv_drm.h"
20
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
26
27DEFINE_DRM_GEM_FOPS(hv_fops);
28
29static struct drm_driver hyperv_driver = {
30 .driver_features = DRIVER_MODESET | DRIVER_GEM | DRIVER_ATOMIC,
31
32 .name = DRIVER_NAME,
33 .desc = DRIVER_DESC,
34 .date = DRIVER_DATE,
35 .major = DRIVER_MAJOR,
36 .minor = DRIVER_MINOR,
37
38 .fops = &hv_fops,
39 DRM_GEM_SHMEM_DRIVER_OPS,
40 DRM_FBDEV_SHMEM_DRIVER_OPS,
41};
42
43static int hyperv_pci_probe(struct pci_dev *pdev,
44 const struct pci_device_id *ent)
45{
46 return 0;
47}
48
49static void hyperv_pci_remove(struct pci_dev *pdev)
50{
51}
52
53static const struct pci_device_id hyperv_pci_tbl[] = {
54 {
55 .vendor = PCI_VENDOR_ID_MICROSOFT,
56 .device = PCI_DEVICE_ID_HYPERV_VIDEO,
57 },
58 { /* end of list */ }
59};
60
61/*
62 * PCI stub to support gen1 VM.
63 */
64static 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,
69};
70
71static int hyperv_setup_vram(struct hyperv_drm_device *hv,
72 struct hv_device *hdev)
73{
74 struct drm_device *dev = &hv->dev;
75 int ret;
76
77 hv->fb_size = (unsigned long)hv->mmio_megabytes * 1024 * 1024;
78
79 ret = vmbus_allocate_mmio(&hv->mem, hdev, 0, -1, hv->fb_size, 0x100000,
80 true);
81 if (ret) {
82 drm_err(dev, "Failed to allocate mmio\n");
83 return -ENOMEM;
84 }
85
86 /*
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
89 * the VRAM cacheable.
90 */
91 hv->vram = ioremap_cache(hv->mem->start, hv->fb_size);
92 if (!hv->vram) {
93 drm_err(dev, "Failed to map vram\n");
94 ret = -ENOMEM;
95 goto error;
96 }
97
98 hv->fb_base = hv->mem->start;
99 return 0;
100
101error:
102 vmbus_free_mmio(hv->mem->start, hv->fb_size);
103 return ret;
104}
105
106static int hyperv_vmbus_probe(struct hv_device *hdev,
107 const struct hv_vmbus_device_id *dev_id)
108{
109 struct hyperv_drm_device *hv;
110 struct drm_device *dev;
111 int ret;
112
113 hv = devm_drm_dev_alloc(&hdev->device, &hyperv_driver,
114 struct hyperv_drm_device, dev);
115 if (IS_ERR(hv))
116 return PTR_ERR(hv);
117
118 dev = &hv->dev;
119 init_completion(&hv->wait);
120 hv_set_drvdata(hdev, hv);
121 hv->hdev = hdev;
122
123 ret = hyperv_connect_vsp(hdev);
124 if (ret) {
125 drm_err(dev, "Failed to connect to vmbus.\n");
126 goto err_hv_set_drv_data;
127 }
128
129 aperture_remove_all_conflicting_devices(hyperv_driver.name);
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_client_setup(dev, NULL);
155
156 return 0;
157
158err_free_mmio:
159 vmbus_free_mmio(hv->mem->start, hv->fb_size);
160err_vmbus_close:
161 vmbus_close(hdev->channel);
162err_hv_set_drv_data:
163 hv_set_drvdata(hdev, NULL);
164 return ret;
165}
166
167static 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
180static void hyperv_vmbus_shutdown(struct hv_device *hdev)
181{
182 drm_atomic_helper_shutdown(hv_get_drvdata(hdev));
183}
184
185static 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
199static 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
216static const struct hv_vmbus_device_id hyperv_vmbus_tbl[] = {
217 /* Synthetic Video Device GUID */
218 {HV_SYNTHVID_GUID},
219 {}
220};
221
222static 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,
230 .driver = {
231 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
232 },
233};
234
235static int __init hyperv_init(void)
236{
237 int ret;
238
239 if (drm_firmware_drivers_only())
240 return -ENODEV;
241
242 ret = pci_register_driver(&hyperv_pci_driver);
243 if (ret != 0)
244 return ret;
245
246 return vmbus_driver_register(&hyperv_hv_driver);
247}
248
249static void __exit hyperv_exit(void)
250{
251 vmbus_driver_unregister(&hyperv_hv_driver);
252 pci_unregister_driver(&hyperv_pci_driver);
253}
254
255module_init(hyperv_init);
256module_exit(hyperv_exit);
257
258MODULE_DEVICE_TABLE(pci, hyperv_pci_tbl);
259MODULE_DEVICE_TABLE(vmbus, hyperv_vmbus_tbl);
260MODULE_LICENSE("GPL");
261MODULE_AUTHOR("Deepak Rawat <drawat.floss@gmail.com>");
262MODULE_DESCRIPTION("DRM driver for Hyper-V synthetic video device");
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
26DEFINE_DRM_GEM_FOPS(hv_fops);
27
28static 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
41static int hyperv_pci_probe(struct pci_dev *pdev,
42 const struct pci_device_id *ent)
43{
44 return 0;
45}
46
47static void hyperv_pci_remove(struct pci_dev *pdev)
48{
49}
50
51static 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 */
62static 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
69static 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 hv->fb_size = (unsigned long)hv->mmio_megabytes * 1024 * 1024;
76
77 ret = vmbus_allocate_mmio(&hv->mem, hdev, 0, -1, hv->fb_size, 0x100000,
78 true);
79 if (ret) {
80 drm_err(dev, "Failed to allocate mmio\n");
81 return -ENOMEM;
82 }
83
84 /*
85 * Map the VRAM cacheable for performance. This is also required for VM
86 * connect to display properly for ARM64 Linux VM, as the host also maps
87 * the VRAM cacheable.
88 */
89 hv->vram = ioremap_cache(hv->mem->start, hv->fb_size);
90 if (!hv->vram) {
91 drm_err(dev, "Failed to map vram\n");
92 ret = -ENOMEM;
93 goto error;
94 }
95
96 hv->fb_base = hv->mem->start;
97 return 0;
98
99error:
100 vmbus_free_mmio(hv->mem->start, hv->fb_size);
101 return ret;
102}
103
104static int hyperv_vmbus_probe(struct hv_device *hdev,
105 const struct hv_vmbus_device_id *dev_id)
106{
107 struct hyperv_drm_device *hv;
108 struct drm_device *dev;
109 int ret;
110
111 hv = devm_drm_dev_alloc(&hdev->device, &hyperv_driver,
112 struct hyperv_drm_device, dev);
113 if (IS_ERR(hv))
114 return PTR_ERR(hv);
115
116 dev = &hv->dev;
117 init_completion(&hv->wait);
118 hv_set_drvdata(hdev, hv);
119 hv->hdev = hdev;
120
121 ret = hyperv_connect_vsp(hdev);
122 if (ret) {
123 drm_err(dev, "Failed to connect to vmbus.\n");
124 goto err_hv_set_drv_data;
125 }
126
127 drm_aperture_remove_framebuffers(&hyperv_driver);
128
129 ret = hyperv_setup_vram(hv, hdev);
130 if (ret)
131 goto err_vmbus_close;
132
133 /*
134 * Should be done only once during init and resume. Failing to update
135 * vram location is not fatal. Device will update dirty area till
136 * preferred resolution only.
137 */
138 ret = hyperv_update_vram_location(hdev, hv->fb_base);
139 if (ret)
140 drm_warn(dev, "Failed to update vram location.\n");
141
142 ret = hyperv_mode_config_init(hv);
143 if (ret)
144 goto err_free_mmio;
145
146 ret = drm_dev_register(dev, 0);
147 if (ret) {
148 drm_err(dev, "Failed to register drm driver.\n");
149 goto err_free_mmio;
150 }
151
152 drm_fbdev_generic_setup(dev, 0);
153
154 return 0;
155
156err_free_mmio:
157 vmbus_free_mmio(hv->mem->start, hv->fb_size);
158err_vmbus_close:
159 vmbus_close(hdev->channel);
160err_hv_set_drv_data:
161 hv_set_drvdata(hdev, NULL);
162 return ret;
163}
164
165static void hyperv_vmbus_remove(struct hv_device *hdev)
166{
167 struct drm_device *dev = hv_get_drvdata(hdev);
168 struct hyperv_drm_device *hv = to_hv(dev);
169
170 drm_dev_unplug(dev);
171 drm_atomic_helper_shutdown(dev);
172 vmbus_close(hdev->channel);
173 hv_set_drvdata(hdev, NULL);
174
175 vmbus_free_mmio(hv->mem->start, hv->fb_size);
176}
177
178static void hyperv_vmbus_shutdown(struct hv_device *hdev)
179{
180 drm_atomic_helper_shutdown(hv_get_drvdata(hdev));
181}
182
183static int hyperv_vmbus_suspend(struct hv_device *hdev)
184{
185 struct drm_device *dev = hv_get_drvdata(hdev);
186 int ret;
187
188 ret = drm_mode_config_helper_suspend(dev);
189 if (ret)
190 return ret;
191
192 vmbus_close(hdev->channel);
193
194 return 0;
195}
196
197static int hyperv_vmbus_resume(struct hv_device *hdev)
198{
199 struct drm_device *dev = hv_get_drvdata(hdev);
200 struct hyperv_drm_device *hv = to_hv(dev);
201 int ret;
202
203 ret = hyperv_connect_vsp(hdev);
204 if (ret)
205 return ret;
206
207 ret = hyperv_update_vram_location(hdev, hv->fb_base);
208 if (ret)
209 return ret;
210
211 return drm_mode_config_helper_resume(dev);
212}
213
214static const struct hv_vmbus_device_id hyperv_vmbus_tbl[] = {
215 /* Synthetic Video Device GUID */
216 {HV_SYNTHVID_GUID},
217 {}
218};
219
220static struct hv_driver hyperv_hv_driver = {
221 .name = KBUILD_MODNAME,
222 .id_table = hyperv_vmbus_tbl,
223 .probe = hyperv_vmbus_probe,
224 .remove = hyperv_vmbus_remove,
225 .shutdown = hyperv_vmbus_shutdown,
226 .suspend = hyperv_vmbus_suspend,
227 .resume = hyperv_vmbus_resume,
228 .driver = {
229 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
230 },
231};
232
233static int __init hyperv_init(void)
234{
235 int ret;
236
237 if (drm_firmware_drivers_only())
238 return -ENODEV;
239
240 ret = pci_register_driver(&hyperv_pci_driver);
241 if (ret != 0)
242 return ret;
243
244 return vmbus_driver_register(&hyperv_hv_driver);
245}
246
247static void __exit hyperv_exit(void)
248{
249 vmbus_driver_unregister(&hyperv_hv_driver);
250 pci_unregister_driver(&hyperv_pci_driver);
251}
252
253module_init(hyperv_init);
254module_exit(hyperv_exit);
255
256MODULE_DEVICE_TABLE(pci, hyperv_pci_tbl);
257MODULE_DEVICE_TABLE(vmbus, hyperv_vmbus_tbl);
258MODULE_LICENSE("GPL");
259MODULE_AUTHOR("Deepak Rawat <drawat.floss@gmail.com>");
260MODULE_DESCRIPTION("DRM driver for Hyper-V synthetic video device");