blob: 8534a99e1aea5fa1d23b974300fb3fabcd413700 [file] [log] [blame]
Dave Airliedc5698e2013-09-09 10:02:56 +10001/*
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>
Daniel Vetter44adece2016-08-10 18:52:34 +020027#include <drm/drm_fb_helper.h>
Dave Airliedc5698e2013-09-09 10:02:56 +100028
29#include "virtgpu_drv.h"
30
Laszlo Ersekc2cbc382016-10-03 19:43:03 +020031int drm_virtio_set_busid(struct drm_device *dev, struct drm_master *master)
32{
33 struct pci_dev *pdev = dev->pdev;
34
35 if (pdev) {
36 return drm_pci_set_busid(dev, master);
37 }
38 return 0;
39}
40
Gerd Hoffmann50cb9412015-03-26 16:29:29 +010041static void virtio_pci_kick_out_firmware_fb(struct pci_dev *pci_dev)
42{
43 struct apertures_struct *ap;
44 bool primary;
45
46 ap = alloc_apertures(1);
47 if (!ap)
48 return;
49
50 ap->ranges[0].base = pci_resource_start(pci_dev, 0);
51 ap->ranges[0].size = pci_resource_len(pci_dev, 0);
52
53 primary = pci_dev->resource[PCI_ROM_RESOURCE].flags
54 & IORESOURCE_ROM_SHADOW;
55
Daniel Vetter44adece2016-08-10 18:52:34 +020056 drm_fb_helper_remove_conflicting_framebuffers(ap, "virtiodrmfb", primary);
Gerd Hoffmann50cb9412015-03-26 16:29:29 +010057
58 kfree(ap);
59}
60
Dave Airliedc5698e2013-09-09 10:02:56 +100061int drm_virtio_init(struct drm_driver *driver, struct virtio_device *vdev)
62{
63 struct drm_device *dev;
64 int ret;
65
66 dev = drm_dev_alloc(driver, &vdev->dev);
Tom Gundersen0f288602016-09-21 16:59:19 +020067 if (IS_ERR(dev))
68 return PTR_ERR(dev);
Dave Airliedc5698e2013-09-09 10:02:56 +100069 dev->virtdev = vdev;
70 vdev->priv = dev;
71
72 if (strcmp(vdev->dev.parent->bus->name, "pci") == 0) {
73 struct pci_dev *pdev = to_pci_dev(vdev->dev.parent);
Gerd Hoffmann9785b432016-10-04 11:19:08 +020074 const char *pname = dev_name(&pdev->dev);
Dave Airliedc5698e2013-09-09 10:02:56 +100075 bool vga = (pdev->class >> 8) == PCI_CLASS_DISPLAY_VGA;
Gerd Hoffmann9785b432016-10-04 11:19:08 +020076 char unique[20];
Dave Airliedc5698e2013-09-09 10:02:56 +100077
Gerd Hoffmann9785b432016-10-04 11:19:08 +020078 DRM_INFO("pci: %s detected at %s\n",
79 vga ? "virtio-vga" : "virtio-gpu-pci",
80 pname);
Dave Airliedc5698e2013-09-09 10:02:56 +100081 dev->pdev = pdev;
Gerd Hoffmann50cb9412015-03-26 16:29:29 +010082 if (vga)
83 virtio_pci_kick_out_firmware_fb(pdev);
Gerd Hoffmann9785b432016-10-04 11:19:08 +020084
85 snprintf(unique, sizeof(unique), "pci:%s", pname);
86 ret = drm_dev_set_unique(dev, unique);
87 if (ret)
88 goto err_free;
89
Dave Airliedc5698e2013-09-09 10:02:56 +100090 }
91
92 ret = drm_dev_register(dev, 0);
93 if (ret)
94 goto err_free;
95
96 DRM_INFO("Initialized %s %d.%d.%d %s on minor %d\n", driver->name,
97 driver->major, driver->minor, driver->patchlevel,
98 driver->date, dev->primary->index);
99
100 return 0;
101
102err_free:
103 drm_dev_unref(dev);
104 return ret;
105}