cirrus_drv.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. * Copyright 2012 Red Hat <mjg@redhat.com>
  3. *
  4. * This file is subject to the terms and conditions of the GNU General
  5. * Public License version 2. See the file COPYING in the main
  6. * directory of this archive for more details.
  7. *
  8. * Authors: Matthew Garrett
  9. * Dave Airlie
  10. */
  11. #include <linux/module.h>
  12. #include <linux/console.h>
  13. #include <drm/drmP.h>
  14. #include <drm/drm_crtc_helper.h>
  15. #include "cirrus_drv.h"
  16. int cirrus_modeset = -1;
  17. MODULE_PARM_DESC(modeset, "Disable/Enable modesetting");
  18. module_param_named(modeset, cirrus_modeset, int, 0400);
  19. /*
  20. * This is the generic driver code. This binds the driver to the drm core,
  21. * which then performs further device association and calls our graphics init
  22. * functions
  23. */
  24. static struct drm_driver driver;
  25. /* only bind to the cirrus chip in qemu */
  26. static DEFINE_PCI_DEVICE_TABLE(pciidlist) = {
  27. { PCI_VENDOR_ID_CIRRUS, PCI_DEVICE_ID_CIRRUS_5446, 0x1af4, 0x1100, 0,
  28. 0, 0 },
  29. {0,}
  30. };
  31. static int cirrus_kick_out_firmware_fb(struct pci_dev *pdev)
  32. {
  33. struct apertures_struct *ap;
  34. bool primary = false;
  35. ap = alloc_apertures(1);
  36. if (!ap)
  37. return -ENOMEM;
  38. ap->ranges[0].base = pci_resource_start(pdev, 0);
  39. ap->ranges[0].size = pci_resource_len(pdev, 0);
  40. #ifdef CONFIG_X86
  41. primary = pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW;
  42. #endif
  43. remove_conflicting_framebuffers(ap, "cirrusdrmfb", primary);
  44. kfree(ap);
  45. return 0;
  46. }
  47. static int cirrus_pci_probe(struct pci_dev *pdev,
  48. const struct pci_device_id *ent)
  49. {
  50. int ret;
  51. ret = cirrus_kick_out_firmware_fb(pdev);
  52. if (ret)
  53. return ret;
  54. return drm_get_pci_dev(pdev, ent, &driver);
  55. }
  56. static void cirrus_pci_remove(struct pci_dev *pdev)
  57. {
  58. struct drm_device *dev = pci_get_drvdata(pdev);
  59. drm_put_dev(dev);
  60. }
  61. static int cirrus_pm_suspend(struct device *dev)
  62. {
  63. struct pci_dev *pdev = to_pci_dev(dev);
  64. struct drm_device *drm_dev = pci_get_drvdata(pdev);
  65. struct cirrus_device *cdev = drm_dev->dev_private;
  66. drm_kms_helper_poll_disable(drm_dev);
  67. if (cdev->mode_info.gfbdev) {
  68. console_lock();
  69. fb_set_suspend(cdev->mode_info.gfbdev->helper.fbdev, 1);
  70. console_unlock();
  71. }
  72. return 0;
  73. }
  74. static int cirrus_pm_resume(struct device *dev)
  75. {
  76. struct pci_dev *pdev = to_pci_dev(dev);
  77. struct drm_device *drm_dev = pci_get_drvdata(pdev);
  78. struct cirrus_device *cdev = drm_dev->dev_private;
  79. drm_helper_resume_force_mode(drm_dev);
  80. if (cdev->mode_info.gfbdev) {
  81. console_lock();
  82. fb_set_suspend(cdev->mode_info.gfbdev->helper.fbdev, 0);
  83. console_unlock();
  84. }
  85. drm_kms_helper_poll_enable(drm_dev);
  86. return 0;
  87. }
  88. static const struct file_operations cirrus_driver_fops = {
  89. .owner = THIS_MODULE,
  90. .open = drm_open,
  91. .release = drm_release,
  92. .unlocked_ioctl = drm_ioctl,
  93. .mmap = cirrus_mmap,
  94. .poll = drm_poll,
  95. #ifdef CONFIG_COMPAT
  96. .compat_ioctl = drm_compat_ioctl,
  97. #endif
  98. };
  99. static struct drm_driver driver = {
  100. .driver_features = DRIVER_MODESET | DRIVER_GEM,
  101. .load = cirrus_driver_load,
  102. .unload = cirrus_driver_unload,
  103. .fops = &cirrus_driver_fops,
  104. .name = DRIVER_NAME,
  105. .desc = DRIVER_DESC,
  106. .date = DRIVER_DATE,
  107. .major = DRIVER_MAJOR,
  108. .minor = DRIVER_MINOR,
  109. .patchlevel = DRIVER_PATCHLEVEL,
  110. .gem_free_object = cirrus_gem_free_object,
  111. .dumb_create = cirrus_dumb_create,
  112. .dumb_map_offset = cirrus_dumb_mmap_offset,
  113. .dumb_destroy = drm_gem_dumb_destroy,
  114. };
  115. static const struct dev_pm_ops cirrus_pm_ops = {
  116. SET_SYSTEM_SLEEP_PM_OPS(cirrus_pm_suspend,
  117. cirrus_pm_resume)
  118. };
  119. static struct pci_driver cirrus_pci_driver = {
  120. .name = DRIVER_NAME,
  121. .id_table = pciidlist,
  122. .probe = cirrus_pci_probe,
  123. .remove = cirrus_pci_remove,
  124. .driver.pm = &cirrus_pm_ops,
  125. };
  126. static int __init cirrus_init(void)
  127. {
  128. #ifdef CONFIG_VGA_CONSOLE
  129. if (vgacon_text_force() && cirrus_modeset == -1)
  130. return -EINVAL;
  131. #endif
  132. if (cirrus_modeset == 0)
  133. return -EINVAL;
  134. return drm_pci_init(&driver, &cirrus_pci_driver);
  135. }
  136. static void __exit cirrus_exit(void)
  137. {
  138. drm_pci_exit(&driver, &cirrus_pci_driver);
  139. }
  140. module_init(cirrus_init);
  141. module_exit(cirrus_exit);
  142. MODULE_DEVICE_TABLE(pci, pciidlist);
  143. MODULE_AUTHOR(DRIVER_AUTHOR);
  144. MODULE_DESCRIPTION(DRIVER_DESC);
  145. MODULE_LICENSE("GPL");