ast_drv.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /*
  2. * Copyright 2012 Red Hat Inc.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the
  6. * "Software"), to deal in the Software without restriction, including
  7. * without limitation the rights to use, copy, modify, merge, publish,
  8. * distribute, sub license, and/or sell copies of the Software, and to
  9. * permit persons to whom the Software is furnished to do so, subject to
  10. * the following conditions:
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  15. * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
  16. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  17. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  18. * USE OR OTHER DEALINGS IN THE SOFTWARE.
  19. *
  20. * The above copyright notice and this permission notice (including the
  21. * next paragraph) shall be included in all copies or substantial portions
  22. * of the Software.
  23. *
  24. */
  25. /*
  26. * Authors: Dave Airlie <airlied@redhat.com>
  27. */
  28. #include <linux/module.h>
  29. #include <linux/console.h>
  30. #include <drm/drmP.h>
  31. #include <drm/drm_crtc_helper.h>
  32. #include "ast_drv.h"
  33. int ast_modeset = -1;
  34. MODULE_PARM_DESC(modeset, "Disable/Enable modesetting");
  35. module_param_named(modeset, ast_modeset, int, 0400);
  36. #define PCI_VENDOR_ASPEED 0x1a03
  37. static struct drm_driver driver;
  38. #define AST_VGA_DEVICE(id, info) { \
  39. .class = PCI_BASE_CLASS_DISPLAY << 16, \
  40. .class_mask = 0xff0000, \
  41. .vendor = PCI_VENDOR_ASPEED, \
  42. .device = id, \
  43. .subvendor = PCI_ANY_ID, \
  44. .subdevice = PCI_ANY_ID, \
  45. .driver_data = (unsigned long) info }
  46. static const struct pci_device_id pciidlist[] = {
  47. AST_VGA_DEVICE(PCI_CHIP_AST2000, NULL),
  48. AST_VGA_DEVICE(PCI_CHIP_AST2100, NULL),
  49. /* AST_VGA_DEVICE(PCI_CHIP_AST1180, NULL), - don't bind to 1180 for now */
  50. {0, 0, 0},
  51. };
  52. MODULE_DEVICE_TABLE(pci, pciidlist);
  53. static void ast_kick_out_firmware_fb(struct pci_dev *pdev)
  54. {
  55. struct apertures_struct *ap;
  56. bool primary = false;
  57. ap = alloc_apertures(1);
  58. if (!ap)
  59. return;
  60. ap->ranges[0].base = pci_resource_start(pdev, 0);
  61. ap->ranges[0].size = pci_resource_len(pdev, 0);
  62. #ifdef CONFIG_X86
  63. primary = pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW;
  64. #endif
  65. remove_conflicting_framebuffers(ap, "astdrmfb", primary);
  66. kfree(ap);
  67. }
  68. static int ast_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  69. {
  70. ast_kick_out_firmware_fb(pdev);
  71. return drm_get_pci_dev(pdev, ent, &driver);
  72. }
  73. static void
  74. ast_pci_remove(struct pci_dev *pdev)
  75. {
  76. struct drm_device *dev = pci_get_drvdata(pdev);
  77. drm_put_dev(dev);
  78. }
  79. static int ast_drm_freeze(struct drm_device *dev)
  80. {
  81. drm_kms_helper_poll_disable(dev);
  82. pci_save_state(dev->pdev);
  83. console_lock();
  84. ast_fbdev_set_suspend(dev, 1);
  85. console_unlock();
  86. return 0;
  87. }
  88. static int ast_drm_thaw(struct drm_device *dev)
  89. {
  90. int error = 0;
  91. ast_post_gpu(dev);
  92. drm_mode_config_reset(dev);
  93. drm_helper_resume_force_mode(dev);
  94. console_lock();
  95. ast_fbdev_set_suspend(dev, 0);
  96. console_unlock();
  97. return error;
  98. }
  99. static int ast_drm_resume(struct drm_device *dev)
  100. {
  101. int ret;
  102. if (pci_enable_device(dev->pdev))
  103. return -EIO;
  104. ret = ast_drm_thaw(dev);
  105. if (ret)
  106. return ret;
  107. drm_kms_helper_poll_enable(dev);
  108. return 0;
  109. }
  110. static int ast_pm_suspend(struct device *dev)
  111. {
  112. struct pci_dev *pdev = to_pci_dev(dev);
  113. struct drm_device *ddev = pci_get_drvdata(pdev);
  114. int error;
  115. error = ast_drm_freeze(ddev);
  116. if (error)
  117. return error;
  118. pci_disable_device(pdev);
  119. pci_set_power_state(pdev, PCI_D3hot);
  120. return 0;
  121. }
  122. static int ast_pm_resume(struct device *dev)
  123. {
  124. struct pci_dev *pdev = to_pci_dev(dev);
  125. struct drm_device *ddev = pci_get_drvdata(pdev);
  126. return ast_drm_resume(ddev);
  127. }
  128. static int ast_pm_freeze(struct device *dev)
  129. {
  130. struct pci_dev *pdev = to_pci_dev(dev);
  131. struct drm_device *ddev = pci_get_drvdata(pdev);
  132. if (!ddev || !ddev->dev_private)
  133. return -ENODEV;
  134. return ast_drm_freeze(ddev);
  135. }
  136. static int ast_pm_thaw(struct device *dev)
  137. {
  138. struct pci_dev *pdev = to_pci_dev(dev);
  139. struct drm_device *ddev = pci_get_drvdata(pdev);
  140. return ast_drm_thaw(ddev);
  141. }
  142. static int ast_pm_poweroff(struct device *dev)
  143. {
  144. struct pci_dev *pdev = to_pci_dev(dev);
  145. struct drm_device *ddev = pci_get_drvdata(pdev);
  146. return ast_drm_freeze(ddev);
  147. }
  148. static const struct dev_pm_ops ast_pm_ops = {
  149. .suspend = ast_pm_suspend,
  150. .resume = ast_pm_resume,
  151. .freeze = ast_pm_freeze,
  152. .thaw = ast_pm_thaw,
  153. .poweroff = ast_pm_poweroff,
  154. .restore = ast_pm_resume,
  155. };
  156. static struct pci_driver ast_pci_driver = {
  157. .name = DRIVER_NAME,
  158. .id_table = pciidlist,
  159. .probe = ast_pci_probe,
  160. .remove = ast_pci_remove,
  161. .driver.pm = &ast_pm_ops,
  162. };
  163. static const struct file_operations ast_fops = {
  164. .owner = THIS_MODULE,
  165. .open = drm_open,
  166. .release = drm_release,
  167. .unlocked_ioctl = drm_ioctl,
  168. .mmap = ast_mmap,
  169. .poll = drm_poll,
  170. #ifdef CONFIG_COMPAT
  171. .compat_ioctl = drm_compat_ioctl,
  172. #endif
  173. .read = drm_read,
  174. };
  175. static struct drm_driver driver = {
  176. .driver_features = DRIVER_MODESET | DRIVER_GEM,
  177. .load = ast_driver_load,
  178. .unload = ast_driver_unload,
  179. .set_busid = drm_pci_set_busid,
  180. .fops = &ast_fops,
  181. .name = DRIVER_NAME,
  182. .desc = DRIVER_DESC,
  183. .date = DRIVER_DATE,
  184. .major = DRIVER_MAJOR,
  185. .minor = DRIVER_MINOR,
  186. .patchlevel = DRIVER_PATCHLEVEL,
  187. .gem_free_object = ast_gem_free_object,
  188. .dumb_create = ast_dumb_create,
  189. .dumb_map_offset = ast_dumb_mmap_offset,
  190. .dumb_destroy = drm_gem_dumb_destroy,
  191. };
  192. static int __init ast_init(void)
  193. {
  194. #ifdef CONFIG_VGA_CONSOLE
  195. if (vgacon_text_force() && ast_modeset == -1)
  196. return -EINVAL;
  197. #endif
  198. if (ast_modeset == 0)
  199. return -EINVAL;
  200. return drm_pci_init(&driver, &ast_pci_driver);
  201. }
  202. static void __exit ast_exit(void)
  203. {
  204. drm_pci_exit(&driver, &ast_pci_driver);
  205. }
  206. module_init(ast_init);
  207. module_exit(ast_exit);
  208. MODULE_AUTHOR(DRIVER_AUTHOR);
  209. MODULE_DESCRIPTION(DRIVER_DESC);
  210. MODULE_LICENSE("GPL and additional rights");