Dave Airlie | dc5698e | 2013-09-09 10:02:56 +1000 | [diff] [blame] | 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> |
Daniel Vetter | 44adece | 2016-08-10 18:52:34 +0200 | [diff] [blame] | 27 | #include <drm/drm_fb_helper.h> |
Dave Airlie | dc5698e | 2013-09-09 10:02:56 +1000 | [diff] [blame] | 28 | |
| 29 | #include "virtgpu_drv.h" |
| 30 | |
Gerd Hoffmann | 50cb941 | 2015-03-26 16:29:29 +0100 | [diff] [blame] | 31 | static 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 | |
Daniel Vetter | 44adece | 2016-08-10 18:52:34 +0200 | [diff] [blame] | 46 | drm_fb_helper_remove_conflicting_framebuffers(ap, "virtiodrmfb", primary); |
Gerd Hoffmann | 50cb941 | 2015-03-26 16:29:29 +0100 | [diff] [blame] | 47 | |
| 48 | kfree(ap); |
| 49 | } |
| 50 | |
Dave Airlie | dc5698e | 2013-09-09 10:02:56 +1000 | [diff] [blame] | 51 | int 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); |
Tom Gundersen | 0f28860 | 2016-09-21 16:59:19 +0200 | [diff] [blame] | 57 | if (IS_ERR(dev)) |
| 58 | return PTR_ERR(dev); |
Dave Airlie | dc5698e | 2013-09-09 10:02:56 +1000 | [diff] [blame] | 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); |
Gerd Hoffmann | 9785b43 | 2016-10-04 11:19:08 +0200 | [diff] [blame] | 63 | const char *pname = dev_name(&pdev->dev); |
Dave Airlie | dc5698e | 2013-09-09 10:02:56 +1000 | [diff] [blame] | 64 | bool vga = (pdev->class >> 8) == PCI_CLASS_DISPLAY_VGA; |
Gerd Hoffmann | 9785b43 | 2016-10-04 11:19:08 +0200 | [diff] [blame] | 65 | char unique[20]; |
Dave Airlie | dc5698e | 2013-09-09 10:02:56 +1000 | [diff] [blame] | 66 | |
Gerd Hoffmann | 9785b43 | 2016-10-04 11:19:08 +0200 | [diff] [blame] | 67 | DRM_INFO("pci: %s detected at %s\n", |
| 68 | vga ? "virtio-vga" : "virtio-gpu-pci", |
| 69 | pname); |
Dave Airlie | dc5698e | 2013-09-09 10:02:56 +1000 | [diff] [blame] | 70 | dev->pdev = pdev; |
Gerd Hoffmann | 50cb941 | 2015-03-26 16:29:29 +0100 | [diff] [blame] | 71 | if (vga) |
| 72 | virtio_pci_kick_out_firmware_fb(pdev); |
Gerd Hoffmann | 9785b43 | 2016-10-04 11:19:08 +0200 | [diff] [blame] | 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 | |
Dave Airlie | dc5698e | 2013-09-09 10:02:56 +1000 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | ret = drm_dev_register(dev, 0); |
| 82 | if (ret) |
| 83 | goto err_free; |
| 84 | |
Dave Airlie | dc5698e | 2013-09-09 10:02:56 +1000 | [diff] [blame] | 85 | return 0; |
| 86 | |
| 87 | err_free: |
| 88 | drm_dev_unref(dev); |
| 89 | return ret; |
| 90 | } |