1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26#include <linux/pci.h>
27#include <drm/drm_fb_helper.h>
28
29#include "virtgpu_drv.h"
30
31static void virtio_pci_kick_out_firmware_fb(struct pci_dev *pci_dev)
32{
33 struct apertures_struct *ap;
34 bool primary;
35
36 ap = alloc_apertures(1);
37 if (!ap)
38 return;
39
40 ap->ranges[0].base = pci_resource_start(pci_dev, 0);
41 ap->ranges[0].size = pci_resource_len(pci_dev, 0);
42
43 primary = pci_dev->resource[PCI_ROM_RESOURCE].flags
44 & IORESOURCE_ROM_SHADOW;
45
46 drm_fb_helper_remove_conflicting_framebuffers(ap, "virtiodrmfb", primary);
47
48 kfree(ap);
49}
50
51int drm_virtio_init(struct drm_driver *driver, struct virtio_device *vdev)
52{
53 struct drm_device *dev;
54 int ret;
55
56 dev = drm_dev_alloc(driver, &vdev->dev);
57 if (IS_ERR(dev))
58 return PTR_ERR(dev);
59 vdev->priv = dev;
60
61 if (strcmp(vdev->dev.parent->bus->name, "pci") == 0) {
62 struct pci_dev *pdev = to_pci_dev(vdev->dev.parent);
63 const char *pname = dev_name(&pdev->dev);
64 bool vga = (pdev->class >> 8) == PCI_CLASS_DISPLAY_VGA;
65 char unique[20];
66
67 DRM_INFO("pci: %s detected at %s\n",
68 vga ? "virtio-vga" : "virtio-gpu-pci",
69 pname);
70 dev->pdev = pdev;
71 if (vga)
72 virtio_pci_kick_out_firmware_fb(pdev);
73
74 snprintf(unique, sizeof(unique), "pci:%s", pname);
75 ret = drm_dev_set_unique(dev, unique);
76 if (ret)
77 goto err_free;
78
79 }
80
81 ret = drm_dev_register(dev, 0);
82 if (ret)
83 goto err_free;
84
85 return 0;
86
87err_free:
88 drm_dev_unref(dev);
89 return ret;
90}
91