bochs_drv.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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 "bochs.h"
  11. static bool enable_fbdev = true;
  12. module_param_named(fbdev, enable_fbdev, bool, 0444);
  13. MODULE_PARM_DESC(fbdev, "register fbdev device");
  14. /* ---------------------------------------------------------------------- */
  15. /* drm interface */
  16. static int bochs_unload(struct drm_device *dev)
  17. {
  18. struct bochs_device *bochs = dev->dev_private;
  19. bochs_fbdev_fini(bochs);
  20. bochs_kms_fini(bochs);
  21. bochs_mm_fini(bochs);
  22. bochs_hw_fini(dev);
  23. kfree(bochs);
  24. dev->dev_private = NULL;
  25. return 0;
  26. }
  27. static int bochs_load(struct drm_device *dev, unsigned long flags)
  28. {
  29. struct bochs_device *bochs;
  30. int ret;
  31. bochs = kzalloc(sizeof(*bochs), GFP_KERNEL);
  32. if (bochs == NULL)
  33. return -ENOMEM;
  34. dev->dev_private = bochs;
  35. bochs->dev = dev;
  36. ret = bochs_hw_init(dev, flags);
  37. if (ret)
  38. goto err;
  39. ret = bochs_mm_init(bochs);
  40. if (ret)
  41. goto err;
  42. ret = bochs_kms_init(bochs);
  43. if (ret)
  44. goto err;
  45. if (enable_fbdev)
  46. bochs_fbdev_init(bochs);
  47. return 0;
  48. err:
  49. bochs_unload(dev);
  50. return ret;
  51. }
  52. static const struct file_operations bochs_fops = {
  53. .owner = THIS_MODULE,
  54. .open = drm_open,
  55. .release = drm_release,
  56. .unlocked_ioctl = drm_ioctl,
  57. #ifdef CONFIG_COMPAT
  58. .compat_ioctl = drm_compat_ioctl,
  59. #endif
  60. .poll = drm_poll,
  61. .read = drm_read,
  62. .llseek = no_llseek,
  63. .mmap = bochs_mmap,
  64. };
  65. static struct drm_driver bochs_driver = {
  66. .driver_features = DRIVER_GEM | DRIVER_MODESET,
  67. .load = bochs_load,
  68. .unload = bochs_unload,
  69. .set_busid = drm_pci_set_busid,
  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 = bochs_gem_free_object,
  77. .dumb_create = bochs_dumb_create,
  78. .dumb_map_offset = bochs_dumb_mmap_offset,
  79. .dumb_destroy = drm_gem_dumb_destroy,
  80. };
  81. /* ---------------------------------------------------------------------- */
  82. /* pm interface */
  83. #ifdef CONFIG_PM_SLEEP
  84. static int bochs_pm_suspend(struct device *dev)
  85. {
  86. struct pci_dev *pdev = to_pci_dev(dev);
  87. struct drm_device *drm_dev = pci_get_drvdata(pdev);
  88. struct bochs_device *bochs = drm_dev->dev_private;
  89. drm_kms_helper_poll_disable(drm_dev);
  90. if (bochs->fb.initialized) {
  91. console_lock();
  92. drm_fb_helper_set_suspend(&bochs->fb.helper, 1);
  93. console_unlock();
  94. }
  95. return 0;
  96. }
  97. static int bochs_pm_resume(struct device *dev)
  98. {
  99. struct pci_dev *pdev = to_pci_dev(dev);
  100. struct drm_device *drm_dev = pci_get_drvdata(pdev);
  101. struct bochs_device *bochs = drm_dev->dev_private;
  102. drm_helper_resume_force_mode(drm_dev);
  103. if (bochs->fb.initialized) {
  104. console_lock();
  105. drm_fb_helper_set_suspend(&bochs->fb.helper, 0);
  106. console_unlock();
  107. }
  108. drm_kms_helper_poll_enable(drm_dev);
  109. return 0;
  110. }
  111. #endif
  112. static const struct dev_pm_ops bochs_pm_ops = {
  113. SET_SYSTEM_SLEEP_PM_OPS(bochs_pm_suspend,
  114. bochs_pm_resume)
  115. };
  116. /* ---------------------------------------------------------------------- */
  117. /* pci interface */
  118. static int bochs_kick_out_firmware_fb(struct pci_dev *pdev)
  119. {
  120. struct apertures_struct *ap;
  121. ap = alloc_apertures(1);
  122. if (!ap)
  123. return -ENOMEM;
  124. ap->ranges[0].base = pci_resource_start(pdev, 0);
  125. ap->ranges[0].size = pci_resource_len(pdev, 0);
  126. remove_conflicting_framebuffers(ap, "bochsdrmfb", false);
  127. kfree(ap);
  128. return 0;
  129. }
  130. static int bochs_pci_probe(struct pci_dev *pdev,
  131. const struct pci_device_id *ent)
  132. {
  133. int ret;
  134. ret = bochs_kick_out_firmware_fb(pdev);
  135. if (ret)
  136. return ret;
  137. return drm_get_pci_dev(pdev, ent, &bochs_driver);
  138. }
  139. static void bochs_pci_remove(struct pci_dev *pdev)
  140. {
  141. struct drm_device *dev = pci_get_drvdata(pdev);
  142. drm_put_dev(dev);
  143. }
  144. static const struct pci_device_id bochs_pci_tbl[] = {
  145. {
  146. .vendor = 0x1234,
  147. .device = 0x1111,
  148. .subvendor = 0x1af4,
  149. .subdevice = 0x1100,
  150. .driver_data = BOCHS_QEMU_STDVGA,
  151. },
  152. {
  153. .vendor = 0x1234,
  154. .device = 0x1111,
  155. .subvendor = PCI_ANY_ID,
  156. .subdevice = PCI_ANY_ID,
  157. .driver_data = BOCHS_UNKNOWN,
  158. },
  159. { /* end of list */ }
  160. };
  161. static struct pci_driver bochs_pci_driver = {
  162. .name = "bochs-drm",
  163. .id_table = bochs_pci_tbl,
  164. .probe = bochs_pci_probe,
  165. .remove = bochs_pci_remove,
  166. .driver.pm = &bochs_pm_ops,
  167. };
  168. /* ---------------------------------------------------------------------- */
  169. /* module init/exit */
  170. static int __init bochs_init(void)
  171. {
  172. return drm_pci_init(&bochs_driver, &bochs_pci_driver);
  173. }
  174. static void __exit bochs_exit(void)
  175. {
  176. drm_pci_exit(&bochs_driver, &bochs_pci_driver);
  177. }
  178. module_init(bochs_init);
  179. module_exit(bochs_exit);
  180. MODULE_DEVICE_TABLE(pci, bochs_pci_tbl);
  181. MODULE_AUTHOR("Gerd Hoffmann <kraxel@redhat.com>");
  182. MODULE_LICENSE("GPL");