Linux Audio

Check our new training course

Loading...
v6.13.7
  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");
v5.14.15
  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 PCI_VENDOR_ID_MICROSOFT 0x1414
 27#define PCI_DEVICE_ID_HYPERV_VIDEO 0x5353
 28
 29DEFINE_DRM_GEM_FOPS(hv_fops);
 30
 31static struct drm_driver hyperv_driver = {
 32	.driver_features = DRIVER_MODESET | DRIVER_GEM | DRIVER_ATOMIC,
 33
 34	.name		 = DRIVER_NAME,
 35	.desc		 = DRIVER_DESC,
 36	.date		 = DRIVER_DATE,
 37	.major		 = DRIVER_MAJOR,
 38	.minor		 = DRIVER_MINOR,
 39
 40	.fops		 = &hv_fops,
 41	DRM_GEM_SHMEM_DRIVER_OPS,
 
 42};
 43
 44static int hyperv_pci_probe(struct pci_dev *pdev,
 45			    const struct pci_device_id *ent)
 46{
 47	return 0;
 48}
 49
 50static void hyperv_pci_remove(struct pci_dev *pdev)
 51{
 52}
 53
 54static const struct pci_device_id hyperv_pci_tbl[] = {
 55	{
 56		.vendor = PCI_VENDOR_ID_MICROSOFT,
 57		.device = PCI_DEVICE_ID_HYPERV_VIDEO,
 58	},
 59	{ /* end of list */ }
 60};
 61
 62/*
 63 * PCI stub to support gen1 VM.
 64 */
 65static struct pci_driver hyperv_pci_driver = {
 66	.name =		KBUILD_MODNAME,
 67	.id_table =	hyperv_pci_tbl,
 68	.probe =	hyperv_pci_probe,
 69	.remove =	hyperv_pci_remove,
 70};
 71
 72static int hyperv_setup_gen1(struct hyperv_drm_device *hv)
 73{
 74	struct drm_device *dev = &hv->dev;
 75	struct pci_dev *pdev;
 76	int ret;
 77
 78	pdev = pci_get_device(PCI_VENDOR_ID_MICROSOFT,
 79			      PCI_DEVICE_ID_HYPERV_VIDEO, NULL);
 80	if (!pdev) {
 81		drm_err(dev, "Unable to find PCI Hyper-V video\n");
 82		return -ENODEV;
 83	}
 84
 85	ret = drm_aperture_remove_conflicting_pci_framebuffers(pdev, "hypervdrmfb");
 86	if (ret) {
 87		drm_err(dev, "Not able to remove boot fb\n");
 88		return ret;
 89	}
 90
 91	if (pci_request_region(pdev, 0, DRIVER_NAME) != 0)
 92		drm_warn(dev, "Cannot request framebuffer, boot fb still active?\n");
 93
 94	if ((pdev->resource[0].flags & IORESOURCE_MEM) == 0) {
 95		drm_err(dev, "Resource at bar 0 is not IORESOURCE_MEM\n");
 96		ret = -ENODEV;
 97		goto error;
 98	}
 99
100	hv->fb_base = pci_resource_start(pdev, 0);
101	hv->fb_size = pci_resource_len(pdev, 0);
102	if (!hv->fb_base) {
103		drm_err(dev, "Resource not available\n");
104		ret = -ENODEV;
105		goto error;
106	}
107
108	hv->fb_size = min(hv->fb_size,
109			  (unsigned long)(hv->mmio_megabytes * 1024 * 1024));
110	hv->vram = devm_ioremap(&pdev->dev, hv->fb_base, hv->fb_size);
111	if (!hv->vram) {
112		drm_err(dev, "Failed to map vram\n");
113		ret = -ENOMEM;
114	}
115
116error:
117	pci_dev_put(pdev);
118	return ret;
119}
120
121static int hyperv_setup_gen2(struct hyperv_drm_device *hv,
122			     struct hv_device *hdev)
123{
124	struct drm_device *dev = &hv->dev;
125	int ret;
126
127	drm_aperture_remove_conflicting_framebuffers(screen_info.lfb_base,
128						     screen_info.lfb_size,
129						     false,
130						     "hypervdrmfb");
131
132	hv->fb_size = (unsigned long)hv->mmio_megabytes * 1024 * 1024;
133
134	ret = vmbus_allocate_mmio(&hv->mem, hdev, 0, -1, hv->fb_size, 0x100000,
135				  true);
136	if (ret) {
137		drm_err(dev, "Failed to allocate mmio\n");
138		return -ENOMEM;
139	}
140
141	/*
142	 * Map the VRAM cacheable for performance. This is also required for VM
143	 * connect to display properly for ARM64 Linux VM, as the host also maps
144	 * the VRAM cacheable.
145	 */
146	hv->vram = ioremap_cache(hv->mem->start, hv->fb_size);
147	if (!hv->vram) {
148		drm_err(dev, "Failed to map vram\n");
149		ret = -ENOMEM;
150		goto error;
151	}
152
153	hv->fb_base = hv->mem->start;
154	return 0;
155
156error:
157	vmbus_free_mmio(hv->mem->start, hv->fb_size);
158	return ret;
159}
160
161static int hyperv_vmbus_probe(struct hv_device *hdev,
162			      const struct hv_vmbus_device_id *dev_id)
163{
164	struct hyperv_drm_device *hv;
165	struct drm_device *dev;
166	int ret;
167
168	hv = devm_drm_dev_alloc(&hdev->device, &hyperv_driver,
169				struct hyperv_drm_device, dev);
170	if (IS_ERR(hv))
171		return PTR_ERR(hv);
172
173	dev = &hv->dev;
174	init_completion(&hv->wait);
175	hv_set_drvdata(hdev, hv);
176	hv->hdev = hdev;
177
178	ret = hyperv_connect_vsp(hdev);
179	if (ret) {
180		drm_err(dev, "Failed to connect to vmbus.\n");
181		goto err_hv_set_drv_data;
182	}
183
184	if (efi_enabled(EFI_BOOT))
185		ret = hyperv_setup_gen2(hv, hdev);
186	else
187		ret = hyperv_setup_gen1(hv);
188
 
189	if (ret)
190		goto err_vmbus_close;
191
192	/*
193	 * Should be done only once during init and resume. Failing to update
194	 * vram location is not fatal. Device will update dirty area till
195	 * preferred resolution only.
196	 */
197	ret = hyperv_update_vram_location(hdev, hv->fb_base);
198	if (ret)
199		drm_warn(dev, "Failed to update vram location.\n");
200
201	hv->dirt_needed = true;
202
203	ret = hyperv_mode_config_init(hv);
204	if (ret)
205		goto err_vmbus_close;
206
207	ret = drm_dev_register(dev, 0);
208	if (ret) {
209		drm_err(dev, "Failed to register drm driver.\n");
210		goto err_vmbus_close;
211	}
212
213	drm_fbdev_generic_setup(dev, 0);
214
215	return 0;
216
 
 
217err_vmbus_close:
218	vmbus_close(hdev->channel);
219err_hv_set_drv_data:
220	hv_set_drvdata(hdev, NULL);
221	return ret;
222}
223
224static int hyperv_vmbus_remove(struct hv_device *hdev)
225{
226	struct drm_device *dev = hv_get_drvdata(hdev);
227	struct hyperv_drm_device *hv = to_hv(dev);
228
229	drm_dev_unplug(dev);
230	drm_atomic_helper_shutdown(dev);
231	vmbus_close(hdev->channel);
232	hv_set_drvdata(hdev, NULL);
 
233	vmbus_free_mmio(hv->mem->start, hv->fb_size);
 
234
235	return 0;
 
 
236}
237
238static int hyperv_vmbus_suspend(struct hv_device *hdev)
239{
240	struct drm_device *dev = hv_get_drvdata(hdev);
241	int ret;
242
243	ret = drm_mode_config_helper_suspend(dev);
244	if (ret)
245		return ret;
246
247	vmbus_close(hdev->channel);
248
249	return 0;
250}
251
252static int hyperv_vmbus_resume(struct hv_device *hdev)
253{
254	struct drm_device *dev = hv_get_drvdata(hdev);
255	struct hyperv_drm_device *hv = to_hv(dev);
256	int ret;
257
258	ret = hyperv_connect_vsp(hdev);
259	if (ret)
260		return ret;
261
262	ret = hyperv_update_vram_location(hdev, hv->fb_base);
263	if (ret)
264		return ret;
265
266	return drm_mode_config_helper_resume(dev);
267}
268
269static const struct hv_vmbus_device_id hyperv_vmbus_tbl[] = {
270	/* Synthetic Video Device GUID */
271	{HV_SYNTHVID_GUID},
272	{}
273};
274
275static struct hv_driver hyperv_hv_driver = {
276	.name = KBUILD_MODNAME,
277	.id_table = hyperv_vmbus_tbl,
278	.probe = hyperv_vmbus_probe,
279	.remove = hyperv_vmbus_remove,
 
280	.suspend = hyperv_vmbus_suspend,
281	.resume = hyperv_vmbus_resume,
282	.driver = {
283		.probe_type = PROBE_PREFER_ASYNCHRONOUS,
284	},
285};
286
287static int __init hyperv_init(void)
288{
289	int ret;
 
 
 
290
291	ret = pci_register_driver(&hyperv_pci_driver);
292	if (ret != 0)
293		return ret;
294
295	return vmbus_driver_register(&hyperv_hv_driver);
296}
297
298static void __exit hyperv_exit(void)
299{
300	vmbus_driver_unregister(&hyperv_hv_driver);
301	pci_unregister_driver(&hyperv_pci_driver);
302}
303
304module_init(hyperv_init);
305module_exit(hyperv_exit);
306
307MODULE_DEVICE_TABLE(pci, hyperv_pci_tbl);
308MODULE_DEVICE_TABLE(vmbus, hyperv_vmbus_tbl);
309MODULE_LICENSE("GPL");
310MODULE_AUTHOR("Deepak Rawat <drawat.floss@gmail.com>");
311MODULE_DESCRIPTION("DRM driver for Hyper-V synthetic video device");