virtgpu_drm_bus.c 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. #include <linux/pci.h>
  26. #include "virtgpu_drv.h"
  27. int drm_virtio_set_busid(struct drm_device *dev, struct drm_master *master)
  28. {
  29. struct pci_dev *pdev = dev->pdev;
  30. if (pdev) {
  31. return drm_pci_set_busid(dev, master);
  32. }
  33. return 0;
  34. }
  35. static void virtio_pci_kick_out_firmware_fb(struct pci_dev *pci_dev)
  36. {
  37. struct apertures_struct *ap;
  38. bool primary;
  39. ap = alloc_apertures(1);
  40. if (!ap)
  41. return;
  42. ap->ranges[0].base = pci_resource_start(pci_dev, 0);
  43. ap->ranges[0].size = pci_resource_len(pci_dev, 0);
  44. primary = pci_dev->resource[PCI_ROM_RESOURCE].flags
  45. & IORESOURCE_ROM_SHADOW;
  46. remove_conflicting_framebuffers(ap, "virtiodrmfb", primary);
  47. kfree(ap);
  48. }
  49. int drm_virtio_init(struct drm_driver *driver, struct virtio_device *vdev)
  50. {
  51. struct drm_device *dev;
  52. int ret;
  53. dev = drm_dev_alloc(driver, &vdev->dev);
  54. if (!dev)
  55. return -ENOMEM;
  56. dev->virtdev = vdev;
  57. vdev->priv = dev;
  58. if (strcmp(vdev->dev.parent->bus->name, "pci") == 0) {
  59. struct pci_dev *pdev = to_pci_dev(vdev->dev.parent);
  60. bool vga = (pdev->class >> 8) == PCI_CLASS_DISPLAY_VGA;
  61. DRM_INFO("pci: %s detected\n",
  62. vga ? "virtio-vga" : "virtio-gpu-pci");
  63. dev->pdev = pdev;
  64. if (vga)
  65. virtio_pci_kick_out_firmware_fb(pdev);
  66. }
  67. ret = drm_dev_register(dev, 0);
  68. if (ret)
  69. goto err_free;
  70. DRM_INFO("Initialized %s %d.%d.%d %s on minor %d\n", driver->name,
  71. driver->major, driver->minor, driver->patchlevel,
  72. driver->date, dev->primary->index);
  73. return 0;
  74. err_free:
  75. drm_dev_unref(dev);
  76. return ret;
  77. }