cirrus_drv.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. int cirrus_bpp = 24;
  18. MODULE_PARM_DESC(modeset, "Disable/Enable modesetting");
  19. module_param_named(modeset, cirrus_modeset, int, 0400);
  20. MODULE_PARM_DESC(bpp, "Max bits-per-pixel (default:24)");
  21. module_param_named(bpp, cirrus_bpp, int, 0400);
  22. /*
  23. * This is the generic driver code. This binds the driver to the drm core,
  24. * which then performs further device association and calls our graphics init
  25. * functions
  26. */
  27. static struct drm_driver driver;
  28. /* only bind to the cirrus chip in qemu */
  29. static const struct pci_device_id pciidlist[] = {
  30. { PCI_VENDOR_ID_CIRRUS, PCI_DEVICE_ID_CIRRUS_5446, 0x1af4, 0x1100, 0,
  31. 0, 0 },
  32. { PCI_VENDOR_ID_CIRRUS, PCI_DEVICE_ID_CIRRUS_5446, PCI_VENDOR_ID_XEN,
  33. 0x0001, 0, 0, 0 },
  34. {0,}
  35. };
  36. static int cirrus_kick_out_firmware_fb(struct pci_dev *pdev)
  37. {
  38. struct apertures_struct *ap;
  39. bool primary = false;
  40. ap = alloc_apertures(1);
  41. if (!ap)
  42. return -ENOMEM;
  43. ap->ranges[0].base = pci_resource_start(pdev, 0);
  44. ap->ranges[0].size = pci_resource_len(pdev, 0);
  45. #ifdef CONFIG_X86
  46. primary = pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW;
  47. #endif
  48. remove_conflicting_framebuffers(ap, "cirrusdrmfb", primary);
  49. kfree(ap);
  50. return 0;
  51. }
  52. static int cirrus_pci_probe(struct pci_dev *pdev,
  53. const struct pci_device_id *ent)
  54. {
  55. int ret;
  56. ret = cirrus_kick_out_firmware_fb(pdev);
  57. if (ret)
  58. return ret;
  59. return drm_get_pci_dev(pdev, ent, &driver);
  60. }
  61. static void cirrus_pci_remove(struct pci_dev *pdev)
  62. {
  63. struct drm_device *dev = pci_get_drvdata(pdev);
  64. drm_put_dev(dev);
  65. }
  66. #ifdef CONFIG_PM_SLEEP
  67. static int cirrus_pm_suspend(struct device *dev)
  68. {
  69. struct pci_dev *pdev = to_pci_dev(dev);
  70. struct drm_device *drm_dev = pci_get_drvdata(pdev);
  71. struct cirrus_device *cdev = drm_dev->dev_private;
  72. drm_kms_helper_poll_disable(drm_dev);
  73. if (cdev->mode_info.gfbdev) {
  74. console_lock();
  75. drm_fb_helper_set_suspend(&cdev->mode_info.gfbdev->helper, 1);
  76. console_unlock();
  77. }
  78. return 0;
  79. }
  80. static int cirrus_pm_resume(struct device *dev)
  81. {
  82. struct pci_dev *pdev = to_pci_dev(dev);
  83. struct drm_device *drm_dev = pci_get_drvdata(pdev);
  84. struct cirrus_device *cdev = drm_dev->dev_private;
  85. drm_helper_resume_force_mode(drm_dev);
  86. if (cdev->mode_info.gfbdev) {
  87. console_lock();
  88. drm_fb_helper_set_suspend(&cdev->mode_info.gfbdev->helper, 0);
  89. console_unlock();
  90. }
  91. drm_kms_helper_poll_enable(drm_dev);
  92. return 0;
  93. }
  94. #endif
  95. static const struct file_operations cirrus_driver_fops = {
  96. .owner = THIS_MODULE,
  97. .open = drm_open,
  98. .release = drm_release,
  99. .unlocked_ioctl = drm_ioctl,
  100. .mmap = cirrus_mmap,
  101. .poll = drm_poll,
  102. #ifdef CONFIG_COMPAT
  103. .compat_ioctl = drm_compat_ioctl,
  104. #endif
  105. };
  106. static struct drm_driver driver = {
  107. .driver_features = DRIVER_MODESET | DRIVER_GEM,
  108. .load = cirrus_driver_load,
  109. .unload = cirrus_driver_unload,
  110. .set_busid = drm_pci_set_busid,
  111. .fops = &cirrus_driver_fops,
  112. .name = DRIVER_NAME,
  113. .desc = DRIVER_DESC,
  114. .date = DRIVER_DATE,
  115. .major = DRIVER_MAJOR,
  116. .minor = DRIVER_MINOR,
  117. .patchlevel = DRIVER_PATCHLEVEL,
  118. .gem_free_object = cirrus_gem_free_object,
  119. .dumb_create = cirrus_dumb_create,
  120. .dumb_map_offset = cirrus_dumb_mmap_offset,
  121. .dumb_destroy = drm_gem_dumb_destroy,
  122. };
  123. static const struct dev_pm_ops cirrus_pm_ops = {
  124. SET_SYSTEM_SLEEP_PM_OPS(cirrus_pm_suspend,
  125. cirrus_pm_resume)
  126. };
  127. static struct pci_driver cirrus_pci_driver = {
  128. .name = DRIVER_NAME,
  129. .id_table = pciidlist,
  130. .probe = cirrus_pci_probe,
  131. .remove = cirrus_pci_remove,
  132. .driver.pm = &cirrus_pm_ops,
  133. };
  134. static int __init cirrus_init(void)
  135. {
  136. #ifdef CONFIG_VGA_CONSOLE
  137. if (vgacon_text_force() && cirrus_modeset == -1)
  138. return -EINVAL;
  139. #endif
  140. if (cirrus_modeset == 0)
  141. return -EINVAL;
  142. return drm_pci_init(&driver, &cirrus_pci_driver);
  143. }
  144. static void __exit cirrus_exit(void)
  145. {
  146. drm_pci_exit(&driver, &cirrus_pci_driver);
  147. }
  148. module_init(cirrus_init);
  149. module_exit(cirrus_exit);
  150. MODULE_DEVICE_TABLE(pci, pciidlist);
  151. MODULE_AUTHOR(DRIVER_AUTHOR);
  152. MODULE_DESCRIPTION(DRIVER_DESC);
  153. MODULE_LICENSE("GPL");