virtgpu_drm_bus.c 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. static void virtio_pci_kick_out_firmware_fb(struct pci_dev *pci_dev)
  29. {
  30. struct apertures_struct *ap;
  31. bool primary;
  32. ap = alloc_apertures(1);
  33. if (!ap)
  34. return;
  35. ap->ranges[0].base = pci_resource_start(pci_dev, 0);
  36. ap->ranges[0].size = pci_resource_len(pci_dev, 0);
  37. primary = pci_dev->resource[PCI_ROM_RESOURCE].flags
  38. & IORESOURCE_ROM_SHADOW;
  39. drm_fb_helper_remove_conflicting_framebuffers(ap, "virtiodrmfb", primary);
  40. kfree(ap);
  41. }
  42. int drm_virtio_init(struct drm_driver *driver, struct virtio_device *vdev)
  43. {
  44. struct drm_device *dev;
  45. int ret;
  46. dev = drm_dev_alloc(driver, &vdev->dev);
  47. if (IS_ERR(dev))
  48. return PTR_ERR(dev);
  49. dev->virtdev = vdev;
  50. vdev->priv = dev;
  51. if (strcmp(vdev->dev.parent->bus->name, "pci") == 0) {
  52. struct pci_dev *pdev = to_pci_dev(vdev->dev.parent);
  53. const char *pname = dev_name(&pdev->dev);
  54. bool vga = (pdev->class >> 8) == PCI_CLASS_DISPLAY_VGA;
  55. char unique[20];
  56. DRM_INFO("pci: %s detected at %s\n",
  57. vga ? "virtio-vga" : "virtio-gpu-pci",
  58. pname);
  59. dev->pdev = pdev;
  60. if (vga)
  61. virtio_pci_kick_out_firmware_fb(pdev);
  62. snprintf(unique, sizeof(unique), "pci:%s", pname);
  63. ret = drm_dev_set_unique(dev, unique);
  64. if (ret)
  65. goto err_free;
  66. }
  67. ret = drm_dev_register(dev, 0);
  68. if (ret)
  69. goto err_free;
  70. return 0;
  71. err_free:
  72. drm_dev_unref(dev);
  73. return ret;
  74. }