bochs_drv.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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, unsigned long flags)
  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, flags);
  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. .load = bochs_load,
  69. .unload = bochs_unload,
  70. .fops = &bochs_fops,
  71. .name = "bochs-drm",
  72. .desc = "bochs dispi vga interface (qemu stdvga)",
  73. .date = "20130925",
  74. .major = 1,
  75. .minor = 0,
  76. .gem_free_object_unlocked = bochs_gem_free_object,
  77. .dumb_create = bochs_dumb_create,
  78. .dumb_map_offset = bochs_dumb_mmap_offset,
  79. };
  80. /* ---------------------------------------------------------------------- */
  81. /* pm interface */
  82. #ifdef CONFIG_PM_SLEEP
  83. static int bochs_pm_suspend(struct device *dev)
  84. {
  85. struct pci_dev *pdev = to_pci_dev(dev);
  86. struct drm_device *drm_dev = pci_get_drvdata(pdev);
  87. struct bochs_device *bochs = drm_dev->dev_private;
  88. drm_kms_helper_poll_disable(drm_dev);
  89. if (bochs->fb.initialized) {
  90. console_lock();
  91. drm_fb_helper_set_suspend(&bochs->fb.helper, 1);
  92. console_unlock();
  93. }
  94. return 0;
  95. }
  96. static int bochs_pm_resume(struct device *dev)
  97. {
  98. struct pci_dev *pdev = to_pci_dev(dev);
  99. struct drm_device *drm_dev = pci_get_drvdata(pdev);
  100. struct bochs_device *bochs = drm_dev->dev_private;
  101. drm_helper_resume_force_mode(drm_dev);
  102. if (bochs->fb.initialized) {
  103. console_lock();
  104. drm_fb_helper_set_suspend(&bochs->fb.helper, 0);
  105. console_unlock();
  106. }
  107. drm_kms_helper_poll_enable(drm_dev);
  108. return 0;
  109. }
  110. #endif
  111. static const struct dev_pm_ops bochs_pm_ops = {
  112. SET_SYSTEM_SLEEP_PM_OPS(bochs_pm_suspend,
  113. bochs_pm_resume)
  114. };
  115. /* ---------------------------------------------------------------------- */
  116. /* pci interface */
  117. static int bochs_kick_out_firmware_fb(struct pci_dev *pdev)
  118. {
  119. struct apertures_struct *ap;
  120. ap = alloc_apertures(1);
  121. if (!ap)
  122. return -ENOMEM;
  123. ap->ranges[0].base = pci_resource_start(pdev, 0);
  124. ap->ranges[0].size = pci_resource_len(pdev, 0);
  125. drm_fb_helper_remove_conflicting_framebuffers(ap, "bochsdrmfb", false);
  126. kfree(ap);
  127. return 0;
  128. }
  129. static int bochs_pci_probe(struct pci_dev *pdev,
  130. const struct pci_device_id *ent)
  131. {
  132. unsigned long fbsize;
  133. int ret;
  134. fbsize = pci_resource_len(pdev, 0);
  135. if (fbsize < 4 * 1024 * 1024) {
  136. DRM_ERROR("less than 4 MB video memory, ignoring device\n");
  137. return -ENOMEM;
  138. }
  139. ret = bochs_kick_out_firmware_fb(pdev);
  140. if (ret)
  141. return ret;
  142. return drm_get_pci_dev(pdev, ent, &bochs_driver);
  143. }
  144. static void bochs_pci_remove(struct pci_dev *pdev)
  145. {
  146. struct drm_device *dev = pci_get_drvdata(pdev);
  147. drm_put_dev(dev);
  148. }
  149. static const struct pci_device_id bochs_pci_tbl[] = {
  150. {
  151. .vendor = 0x1234,
  152. .device = 0x1111,
  153. .subvendor = PCI_SUBVENDOR_ID_REDHAT_QUMRANET,
  154. .subdevice = PCI_SUBDEVICE_ID_QEMU,
  155. .driver_data = BOCHS_QEMU_STDVGA,
  156. },
  157. {
  158. .vendor = 0x1234,
  159. .device = 0x1111,
  160. .subvendor = PCI_ANY_ID,
  161. .subdevice = PCI_ANY_ID,
  162. .driver_data = BOCHS_UNKNOWN,
  163. },
  164. { /* end of list */ }
  165. };
  166. static struct pci_driver bochs_pci_driver = {
  167. .name = "bochs-drm",
  168. .id_table = bochs_pci_tbl,
  169. .probe = bochs_pci_probe,
  170. .remove = bochs_pci_remove,
  171. .driver.pm = &bochs_pm_ops,
  172. };
  173. /* ---------------------------------------------------------------------- */
  174. /* module init/exit */
  175. static int __init bochs_init(void)
  176. {
  177. if (vgacon_text_force() && bochs_modeset == -1)
  178. return -EINVAL;
  179. if (bochs_modeset == 0)
  180. return -EINVAL;
  181. return pci_register_driver(&bochs_pci_driver);
  182. }
  183. static void __exit bochs_exit(void)
  184. {
  185. pci_unregister_driver(&bochs_pci_driver);
  186. }
  187. module_init(bochs_init);
  188. module_exit(bochs_exit);
  189. MODULE_DEVICE_TABLE(pci, bochs_pci_tbl);
  190. MODULE_AUTHOR("Gerd Hoffmann <kraxel@redhat.com>");
  191. MODULE_LICENSE("GPL");