virtgpu_drm_bus.c 2.7 KB

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