vbox_drv.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. // SPDX-License-Identifier: MIT
  2. /*
  3. * Copyright (C) 2013-2017 Oracle Corporation
  4. * This file is based on ast_drv.c
  5. * Copyright 2012 Red Hat Inc.
  6. * Authors: Dave Airlie <airlied@redhat.com>
  7. * Michael Thayer <michael.thayer@oracle.com,
  8. * Hans de Goede <hdegoede@redhat.com>
  9. */
  10. #include <linux/module.h>
  11. #include <linux/pci.h>
  12. #include <linux/vt_kern.h>
  13. #include <drm/drm_aperture.h>
  14. #include <drm/drm_atomic_helper.h>
  15. #include <drm/drm_drv.h>
  16. #include <drm/drm_fbdev_ttm.h>
  17. #include <drm/drm_file.h>
  18. #include <drm/drm_ioctl.h>
  19. #include <drm/drm_managed.h>
  20. #include <drm/drm_modeset_helper.h>
  21. #include <drm/drm_module.h>
  22. #include "vbox_drv.h"
  23. static int vbox_modeset = -1;
  24. MODULE_PARM_DESC(modeset, "Disable/Enable modesetting");
  25. module_param_named(modeset, vbox_modeset, int, 0400);
  26. static const struct drm_driver driver;
  27. static const struct pci_device_id pciidlist[] = {
  28. { PCI_DEVICE(0x80ee, 0xbeef) },
  29. { }
  30. };
  31. MODULE_DEVICE_TABLE(pci, pciidlist);
  32. static int vbox_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  33. {
  34. struct vbox_private *vbox;
  35. int ret = 0;
  36. if (!vbox_check_supported(VBE_DISPI_ID_HGSMI))
  37. return -ENODEV;
  38. ret = drm_aperture_remove_conflicting_pci_framebuffers(pdev, &driver);
  39. if (ret)
  40. return ret;
  41. vbox = devm_drm_dev_alloc(&pdev->dev, &driver,
  42. struct vbox_private, ddev);
  43. if (IS_ERR(vbox))
  44. return PTR_ERR(vbox);
  45. pci_set_drvdata(pdev, vbox);
  46. mutex_init(&vbox->hw_mutex);
  47. ret = pcim_enable_device(pdev);
  48. if (ret)
  49. return ret;
  50. ret = vbox_hw_init(vbox);
  51. if (ret)
  52. return ret;
  53. ret = vbox_mm_init(vbox);
  54. if (ret)
  55. goto err_hw_fini;
  56. ret = vbox_mode_init(vbox);
  57. if (ret)
  58. goto err_hw_fini;
  59. ret = vbox_irq_init(vbox);
  60. if (ret)
  61. goto err_mode_fini;
  62. ret = drm_dev_register(&vbox->ddev, 0);
  63. if (ret)
  64. goto err_irq_fini;
  65. drm_fbdev_ttm_setup(&vbox->ddev, 32);
  66. return 0;
  67. err_irq_fini:
  68. vbox_irq_fini(vbox);
  69. err_mode_fini:
  70. vbox_mode_fini(vbox);
  71. err_hw_fini:
  72. vbox_hw_fini(vbox);
  73. return ret;
  74. }
  75. static void vbox_pci_remove(struct pci_dev *pdev)
  76. {
  77. struct vbox_private *vbox = pci_get_drvdata(pdev);
  78. drm_dev_unregister(&vbox->ddev);
  79. drm_atomic_helper_shutdown(&vbox->ddev);
  80. vbox_irq_fini(vbox);
  81. vbox_mode_fini(vbox);
  82. vbox_hw_fini(vbox);
  83. }
  84. static void vbox_pci_shutdown(struct pci_dev *pdev)
  85. {
  86. struct vbox_private *vbox = pci_get_drvdata(pdev);
  87. drm_atomic_helper_shutdown(&vbox->ddev);
  88. }
  89. static int vbox_pm_suspend(struct device *dev)
  90. {
  91. struct vbox_private *vbox = dev_get_drvdata(dev);
  92. struct pci_dev *pdev = to_pci_dev(dev);
  93. int error;
  94. error = drm_mode_config_helper_suspend(&vbox->ddev);
  95. if (error)
  96. return error;
  97. pci_save_state(pdev);
  98. pci_disable_device(pdev);
  99. pci_set_power_state(pdev, PCI_D3hot);
  100. return 0;
  101. }
  102. static int vbox_pm_resume(struct device *dev)
  103. {
  104. struct vbox_private *vbox = dev_get_drvdata(dev);
  105. struct pci_dev *pdev = to_pci_dev(dev);
  106. if (pci_enable_device(pdev))
  107. return -EIO;
  108. return drm_mode_config_helper_resume(&vbox->ddev);
  109. }
  110. static int vbox_pm_freeze(struct device *dev)
  111. {
  112. struct vbox_private *vbox = dev_get_drvdata(dev);
  113. return drm_mode_config_helper_suspend(&vbox->ddev);
  114. }
  115. static int vbox_pm_thaw(struct device *dev)
  116. {
  117. struct vbox_private *vbox = dev_get_drvdata(dev);
  118. return drm_mode_config_helper_resume(&vbox->ddev);
  119. }
  120. static int vbox_pm_poweroff(struct device *dev)
  121. {
  122. struct vbox_private *vbox = dev_get_drvdata(dev);
  123. return drm_mode_config_helper_suspend(&vbox->ddev);
  124. }
  125. static const struct dev_pm_ops vbox_pm_ops = {
  126. .suspend = vbox_pm_suspend,
  127. .resume = vbox_pm_resume,
  128. .freeze = vbox_pm_freeze,
  129. .thaw = vbox_pm_thaw,
  130. .poweroff = vbox_pm_poweroff,
  131. .restore = vbox_pm_resume,
  132. };
  133. static struct pci_driver vbox_pci_driver = {
  134. .name = DRIVER_NAME,
  135. .id_table = pciidlist,
  136. .probe = vbox_pci_probe,
  137. .remove = vbox_pci_remove,
  138. .shutdown = vbox_pci_shutdown,
  139. .driver.pm = pm_sleep_ptr(&vbox_pm_ops),
  140. };
  141. DEFINE_DRM_GEM_FOPS(vbox_fops);
  142. static const struct drm_driver driver = {
  143. .driver_features =
  144. DRIVER_MODESET | DRIVER_GEM | DRIVER_ATOMIC | DRIVER_CURSOR_HOTSPOT,
  145. .fops = &vbox_fops,
  146. .name = DRIVER_NAME,
  147. .desc = DRIVER_DESC,
  148. .date = DRIVER_DATE,
  149. .major = DRIVER_MAJOR,
  150. .minor = DRIVER_MINOR,
  151. .patchlevel = DRIVER_PATCHLEVEL,
  152. DRM_GEM_VRAM_DRIVER,
  153. };
  154. drm_module_pci_driver_if_modeset(vbox_pci_driver, vbox_modeset);
  155. MODULE_AUTHOR("Oracle Corporation");
  156. MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
  157. MODULE_DESCRIPTION(DRIVER_DESC);
  158. MODULE_LICENSE("GPL and additional rights");