Commit | Line | Data |
---|---|---|
dc5698e8 DA |
1 | /* |
2 | * Copyright (C) 2015 Red Hat, Inc. | |
3 | * All Rights Reserved. | |
4 | * | |
5 | * Permission is hereby granted, free of charge, to any person obtaining | |
6 | * a copy of this software and associated documentation files (the | |
7 | * "Software"), to deal in the Software without restriction, including | |
8 | * without limitation the rights to use, copy, modify, merge, publish, | |
9 | * distribute, sublicense, and/or sell copies of the Software, and to | |
10 | * permit persons to whom the Software is furnished to do so, subject to | |
11 | * the following conditions: | |
12 | * | |
13 | * The above copyright notice and this permission notice (including the | |
14 | * next paragraph) shall be included in all copies or substantial | |
15 | * portions of the Software. | |
16 | * | |
17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | |
20 | * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE | |
21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | |
22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | |
23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
24 | */ | |
25 | ||
26 | #include <linux/pci.h> | |
44adece5 | 27 | #include <drm/drm_fb_helper.h> |
dc5698e8 DA |
28 | |
29 | #include "virtgpu_drv.h" | |
30 | ||
dc5698e8 DA |
31 | int drm_virtio_init(struct drm_driver *driver, struct virtio_device *vdev) |
32 | { | |
33 | struct drm_device *dev; | |
34 | int ret; | |
35 | ||
36 | dev = drm_dev_alloc(driver, &vdev->dev); | |
0f288605 TG |
37 | if (IS_ERR(dev)) |
38 | return PTR_ERR(dev); | |
dc5698e8 DA |
39 | vdev->priv = dev; |
40 | ||
41 | if (strcmp(vdev->dev.parent->bus->name, "pci") == 0) { | |
42 | struct pci_dev *pdev = to_pci_dev(vdev->dev.parent); | |
9785b432 | 43 | const char *pname = dev_name(&pdev->dev); |
dc5698e8 | 44 | bool vga = (pdev->class >> 8) == PCI_CLASS_DISPLAY_VGA; |
9785b432 | 45 | char unique[20]; |
dc5698e8 | 46 | |
9785b432 GH |
47 | DRM_INFO("pci: %s detected at %s\n", |
48 | vga ? "virtio-vga" : "virtio-gpu-pci", | |
49 | pname); | |
dc5698e8 | 50 | dev->pdev = pdev; |
50cb941a | 51 | if (vga) |
770af585 MM |
52 | drm_fb_helper_remove_conflicting_pci_framebuffers(pdev, |
53 | 0, | |
54 | "virtiodrmfb"); | |
9785b432 | 55 | |
4bdbd5f0 EV |
56 | /* |
57 | * Normally the drm_dev_set_unique() call is done by core DRM. | |
58 | * The following comment covers, why virtio cannot rely on it. | |
59 | * | |
60 | * Unlike the other virtual GPU drivers, virtio abstracts the | |
61 | * underlying bus type by using struct virtio_device. | |
62 | * | |
63 | * Hence the dev_is_pci() check, used in core DRM, will fail | |
64 | * and the unique returned will be the virtio_device "virtio0", | |
65 | * while a "pci:..." one is required. | |
66 | * | |
67 | * A few other ideas were considered: | |
68 | * - Extend the dev_is_pci() check [in drm_set_busid] to | |
69 | * consider virtio. | |
70 | * Seems like a bigger hack than what we have already. | |
71 | * | |
72 | * - Point drm_device::dev to the parent of the virtio_device | |
73 | * Semantic changes: | |
74 | * * Using the wrong device for i2c, framebuffer_alloc and | |
75 | * prime import. | |
76 | * Visual changes: | |
77 | * * Helpers such as DRM_DEV_ERROR, dev_info, drm_printer, | |
78 | * will print the wrong information. | |
79 | * | |
80 | * We could address the latter issues, by introducing | |
81 | * drm_device::bus_dev, ... which would be used solely for this. | |
82 | * | |
83 | * So for the moment keep things as-is, with a bulky comment | |
84 | * for the next person who feels like removing this | |
85 | * drm_dev_set_unique() quirk. | |
86 | */ | |
9785b432 GH |
87 | snprintf(unique, sizeof(unique), "pci:%s", pname); |
88 | ret = drm_dev_set_unique(dev, unique); | |
89 | if (ret) | |
90 | goto err_free; | |
91 | ||
dc5698e8 DA |
92 | } |
93 | ||
94 | ret = drm_dev_register(dev, 0); | |
95 | if (ret) | |
96 | goto err_free; | |
97 | ||
dc5698e8 DA |
98 | return 0; |
99 | ||
100 | err_free: | |
eec97cd3 | 101 | drm_dev_put(dev); |
dc5698e8 DA |
102 | return ret; |
103 | } |