bochs_drv.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /*
  2. * This program is free software; you can redistribute it and/or modify
  3. * it under the terms of the GNU General Public License as published by
  4. * the Free Software Foundation; either version 2 of the License, or
  5. * (at your option) any later version.
  6. */
  7. #include <linux/mm.h>
  8. #include <linux/module.h>
  9. #include <linux/slab.h>
  10. #include <drm/drm_fb_helper.h>
  11. #include "bochs.h"
  12. static int bochs_modeset = -1;
  13. module_param_named(modeset, bochs_modeset, int, 0444);
  14. MODULE_PARM_DESC(modeset, "enable/disable kernel modesetting");
  15. static bool enable_fbdev = true;
  16. module_param_named(fbdev, enable_fbdev, bool, 0444);
  17. MODULE_PARM_DESC(fbdev, "register fbdev device");
  18. /* ---------------------------------------------------------------------- */
  19. /* drm interface */
  20. static void bochs_unload(struct drm_device *dev)
  21. {
  22. struct bochs_device *bochs = dev->dev_private;
  23. bochs_fbdev_fini(bochs);
  24. bochs_kms_fini(bochs);
  25. bochs_mm_fini(bochs);
  26. bochs_hw_fini(dev);
  27. kfree(bochs);
  28. dev->dev_private = NULL;
  29. }
  30. static int bochs_load(struct drm_device *dev)
  31. {
  32. struct bochs_device *bochs;
  33. int ret;
  34. bochs = kzalloc(sizeof(*bochs), GFP_KERNEL);
  35. if (bochs == NULL)
  36. return -ENOMEM;
  37. dev->dev_private = bochs;
  38. bochs->dev = dev;
  39. ret = bochs_hw_init(dev);
  40. if (ret)
  41. goto err;
  42. ret = bochs_mm_init(bochs);
  43. if (ret)
  44. goto err;
  45. ret = bochs_kms_init(bochs);
  46. if (ret)
  47. goto err;
  48. if (enable_fbdev)
  49. bochs_fbdev_init(bochs);
  50. return 0;
  51. err:
  52. bochs_unload(dev);
  53. return ret;
  54. }
  55. static const struct file_operations bochs_fops = {
  56. .owner = THIS_MODULE,
  57. .open = drm_open,
  58. .release = drm_release,
  59. .unlocked_ioctl = drm_ioctl,
  60. .compat_ioctl = drm_compat_ioctl,
  61. .poll = drm_poll,
  62. .read = drm_read,
  63. .llseek = no_llseek,
  64. .mmap = bochs_mmap,
  65. };
  66. static struct drm_driver bochs_driver = {
  67. .driver_features = DRIVER_GEM | DRIVER_MODESET,
  68. .fops = &bochs_fops,
  69. .name = "bochs-drm",
  70. .desc = "bochs dispi vga interface (qemu stdvga)",
  71. .date = "20130925",
  72. .major = 1,
  73. .minor = 0,
  74. .gem_free_object_unlocked = bochs_gem_free_object,
  75. .dumb_create = bochs_dumb_create,
  76. .dumb_map_offset = bochs_dumb_mmap_offset,
  77. };
  78. /* ---------------------------------------------------------------------- */
  79. /* pm interface */
  80. #ifdef CONFIG_PM_SLEEP
  81. static int bochs_pm_suspend(struct device *dev)
  82. {
  83. struct pci_dev *pdev = to_pci_dev(dev);
  84. struct drm_device *drm_dev = pci_get_drvdata(pdev);
  85. struct bochs_device *bochs = drm_dev->dev_private;
  86. drm_kms_helper_poll_disable(drm_dev);
  87. drm_fb_helper_set_suspend_unlocked(&bochs->fb.helper, 1);
  88. return 0;
  89. }
  90. static int bochs_pm_resume(struct device *dev)
  91. {
  92. struct pci_dev *pdev = to_pci_dev(dev);
  93. struct drm_device *drm_dev = pci_get_drvdata(pdev);
  94. struct bochs_device *bochs = drm_dev->dev_private;
  95. drm_helper_resume_force_mode(drm_dev);
  96. drm_fb_helper_set_suspend_unlocked(&bochs->fb.helper, 0);
  97. drm_kms_helper_poll_enable(drm_dev);
  98. return 0;
  99. }
  100. #endif
  101. static const struct dev_pm_ops bochs_pm_ops = {
  102. SET_SYSTEM_SLEEP_PM_OPS(bochs_pm_suspend,
  103. bochs_pm_resume)
  104. };
  105. /* ---------------------------------------------------------------------- */
  106. /* pci interface */
  107. static int bochs_pci_probe(struct pci_dev *pdev,
  108. const struct pci_device_id *ent)
  109. {
  110. struct drm_device *dev;
  111. unsigned long fbsize;
  112. int ret;
  113. fbsize = pci_resource_len(pdev, 0);
  114. if (fbsize < 4 * 1024 * 1024) {
  115. DRM_ERROR("less than 4 MB video memory, ignoring device\n");
  116. return -ENOMEM;
  117. }
  118. ret = drm_fb_helper_remove_conflicting_pci_framebuffers(pdev, 0, "bochsdrmfb");
  119. if (ret)
  120. return ret;
  121. dev = drm_dev_alloc(&bochs_driver, &pdev->dev);
  122. if (IS_ERR(dev))
  123. return PTR_ERR(dev);
  124. dev->pdev = pdev;
  125. pci_set_drvdata(pdev, dev);
  126. ret = bochs_load(dev);
  127. if (ret)
  128. goto err_free_dev;
  129. ret = drm_dev_register(dev, 0);
  130. if (ret)
  131. goto err_unload;
  132. return ret;
  133. err_unload:
  134. bochs_unload(dev);
  135. err_free_dev:
  136. drm_dev_put(dev);
  137. return ret;
  138. }
  139. static void bochs_pci_remove(struct pci_dev *pdev)
  140. {
  141. struct drm_device *dev = pci_get_drvdata(pdev);
  142. drm_dev_unregister(dev);
  143. bochs_unload(dev);
  144. drm_dev_put(dev);
  145. }
  146. static const struct pci_device_id bochs_pci_tbl[] = {
  147. {
  148. .vendor = 0x1234,
  149. .device = 0x1111,
  150. .subvendor = PCI_SUBVENDOR_ID_REDHAT_QUMRANET,
  151. .subdevice = PCI_SUBDEVICE_ID_QEMU,
  152. .driver_data = BOCHS_QEMU_STDVGA,
  153. },
  154. {
  155. .vendor = 0x1234,
  156. .device = 0x1111,
  157. .subvendor = PCI_ANY_ID,
  158. .subdevice = PCI_ANY_ID,
  159. .driver_data = BOCHS_UNKNOWN,
  160. },
  161. { /* end of list */ }
  162. };
  163. static struct pci_driver bochs_pci_driver = {
  164. .name = "bochs-drm",
  165. .id_table = bochs_pci_tbl,
  166. .probe = bochs_pci_probe,
  167. .remove = bochs_pci_remove,
  168. .driver.pm = &bochs_pm_ops,
  169. };
  170. /* ---------------------------------------------------------------------- */
  171. /* module init/exit */
  172. static int __init bochs_init(void)
  173. {
  174. if (vgacon_text_force() && bochs_modeset == -1)
  175. return -EINVAL;
  176. if (bochs_modeset == 0)
  177. return -EINVAL;
  178. return pci_register_driver(&bochs_pci_driver);
  179. }
  180. static void __exit bochs_exit(void)
  181. {
  182. pci_unregister_driver(&bochs_pci_driver);
  183. }
  184. module_init(bochs_init);
  185. module_exit(bochs_exit);
  186. MODULE_DEVICE_TABLE(pci, bochs_pci_tbl);
  187. MODULE_AUTHOR("Gerd Hoffmann <kraxel@redhat.com>");
  188. MODULE_LICENSE("GPL");