qxl_drv.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /* vim: set ts=8 sw=8 tw=78 ai noexpandtab */
  2. /* qxl_drv.c -- QXL driver -*- linux-c -*-
  3. *
  4. * Copyright 2011 Red Hat, Inc.
  5. * All Rights Reserved.
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a
  8. * copy of this software and associated documentation files (the "Software"),
  9. * to deal in the Software without restriction, including without limitation
  10. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  11. * and/or sell copies of the Software, and to permit persons to whom the
  12. * Software is furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice (including the next
  15. * paragraph) shall be included in all copies or substantial portions of the
  16. * Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  21. * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  22. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  23. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  24. * OTHER DEALINGS IN THE SOFTWARE.
  25. *
  26. * Authors:
  27. * Dave Airlie <airlie@redhat.com>
  28. * Alon Levy <alevy@redhat.com>
  29. */
  30. #include <linux/module.h>
  31. #include <linux/console.h>
  32. #include "drmP.h"
  33. #include "drm/drm.h"
  34. #include "drm_crtc_helper.h"
  35. #include "qxl_drv.h"
  36. #include "qxl_object.h"
  37. extern int qxl_max_ioctls;
  38. static DEFINE_PCI_DEVICE_TABLE(pciidlist) = {
  39. { 0x1b36, 0x100, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_DISPLAY_VGA << 8,
  40. 0xffff00, 0 },
  41. { 0x1b36, 0x100, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_DISPLAY_OTHER << 8,
  42. 0xffff00, 0 },
  43. { 0, 0, 0 },
  44. };
  45. MODULE_DEVICE_TABLE(pci, pciidlist);
  46. static int qxl_modeset = -1;
  47. int qxl_num_crtc = 4;
  48. MODULE_PARM_DESC(modeset, "Disable/Enable modesetting");
  49. module_param_named(modeset, qxl_modeset, int, 0400);
  50. MODULE_PARM_DESC(num_heads, "Number of virtual crtcs to expose (default 4)");
  51. module_param_named(num_heads, qxl_num_crtc, int, 0400);
  52. static struct drm_driver qxl_driver;
  53. static struct pci_driver qxl_pci_driver;
  54. static int
  55. qxl_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  56. {
  57. if (pdev->revision < 4) {
  58. DRM_ERROR("qxl too old, doesn't support client_monitors_config,"
  59. " use xf86-video-qxl in user mode");
  60. return -EINVAL; /* TODO: ENODEV ? */
  61. }
  62. return drm_get_pci_dev(pdev, ent, &qxl_driver);
  63. }
  64. static void
  65. qxl_pci_remove(struct pci_dev *pdev)
  66. {
  67. struct drm_device *dev = pci_get_drvdata(pdev);
  68. drm_put_dev(dev);
  69. }
  70. static const struct file_operations qxl_fops = {
  71. .owner = THIS_MODULE,
  72. .open = drm_open,
  73. .release = drm_release,
  74. .unlocked_ioctl = drm_ioctl,
  75. .poll = drm_poll,
  76. .mmap = qxl_mmap,
  77. };
  78. static int qxl_drm_freeze(struct drm_device *dev)
  79. {
  80. struct pci_dev *pdev = dev->pdev;
  81. struct qxl_device *qdev = dev->dev_private;
  82. struct drm_crtc *crtc;
  83. drm_kms_helper_poll_disable(dev);
  84. console_lock();
  85. qxl_fbdev_set_suspend(qdev, 1);
  86. console_unlock();
  87. /* unpin the front buffers */
  88. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  89. struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  90. if (crtc->enabled)
  91. (*crtc_funcs->disable)(crtc);
  92. }
  93. qxl_destroy_monitors_object(qdev);
  94. qxl_surf_evict(qdev);
  95. qxl_vram_evict(qdev);
  96. while (!qxl_check_idle(qdev->command_ring));
  97. while (!qxl_check_idle(qdev->release_ring))
  98. qxl_queue_garbage_collect(qdev, 1);
  99. pci_save_state(pdev);
  100. return 0;
  101. }
  102. static int qxl_drm_resume(struct drm_device *dev, bool thaw)
  103. {
  104. struct qxl_device *qdev = dev->dev_private;
  105. qdev->ram_header->int_mask = QXL_INTERRUPT_MASK;
  106. if (!thaw) {
  107. qxl_reinit_memslots(qdev);
  108. qxl_ring_init_hdr(qdev->release_ring);
  109. }
  110. qxl_create_monitors_object(qdev);
  111. drm_helper_resume_force_mode(dev);
  112. console_lock();
  113. qxl_fbdev_set_suspend(qdev, 0);
  114. console_unlock();
  115. drm_kms_helper_poll_enable(dev);
  116. return 0;
  117. }
  118. static int qxl_pm_suspend(struct device *dev)
  119. {
  120. struct pci_dev *pdev = to_pci_dev(dev);
  121. struct drm_device *drm_dev = pci_get_drvdata(pdev);
  122. int error;
  123. error = qxl_drm_freeze(drm_dev);
  124. if (error)
  125. return error;
  126. pci_disable_device(pdev);
  127. pci_set_power_state(pdev, PCI_D3hot);
  128. return 0;
  129. }
  130. static int qxl_pm_resume(struct device *dev)
  131. {
  132. struct pci_dev *pdev = to_pci_dev(dev);
  133. struct drm_device *drm_dev = pci_get_drvdata(pdev);
  134. pci_set_power_state(pdev, PCI_D0);
  135. pci_restore_state(pdev);
  136. if (pci_enable_device(pdev)) {
  137. return -EIO;
  138. }
  139. return qxl_drm_resume(drm_dev, false);
  140. }
  141. static int qxl_pm_thaw(struct device *dev)
  142. {
  143. struct pci_dev *pdev = to_pci_dev(dev);
  144. struct drm_device *drm_dev = pci_get_drvdata(pdev);
  145. return qxl_drm_resume(drm_dev, true);
  146. }
  147. static int qxl_pm_freeze(struct device *dev)
  148. {
  149. struct pci_dev *pdev = to_pci_dev(dev);
  150. struct drm_device *drm_dev = pci_get_drvdata(pdev);
  151. return qxl_drm_freeze(drm_dev);
  152. }
  153. static int qxl_pm_restore(struct device *dev)
  154. {
  155. struct pci_dev *pdev = to_pci_dev(dev);
  156. struct drm_device *drm_dev = pci_get_drvdata(pdev);
  157. struct qxl_device *qdev = drm_dev->dev_private;
  158. qxl_io_reset(qdev);
  159. return qxl_drm_resume(drm_dev, false);
  160. }
  161. static const struct dev_pm_ops qxl_pm_ops = {
  162. .suspend = qxl_pm_suspend,
  163. .resume = qxl_pm_resume,
  164. .freeze = qxl_pm_freeze,
  165. .thaw = qxl_pm_thaw,
  166. .poweroff = qxl_pm_freeze,
  167. .restore = qxl_pm_restore,
  168. };
  169. static struct pci_driver qxl_pci_driver = {
  170. .name = DRIVER_NAME,
  171. .id_table = pciidlist,
  172. .probe = qxl_pci_probe,
  173. .remove = qxl_pci_remove,
  174. .driver.pm = &qxl_pm_ops,
  175. };
  176. static struct drm_driver qxl_driver = {
  177. .driver_features = DRIVER_GEM | DRIVER_MODESET |
  178. DRIVER_HAVE_IRQ | DRIVER_IRQ_SHARED,
  179. .dev_priv_size = 0,
  180. .load = qxl_driver_load,
  181. .unload = qxl_driver_unload,
  182. .dumb_create = qxl_mode_dumb_create,
  183. .dumb_map_offset = qxl_mode_dumb_mmap,
  184. .dumb_destroy = drm_gem_dumb_destroy,
  185. #if defined(CONFIG_DEBUG_FS)
  186. .debugfs_init = qxl_debugfs_init,
  187. .debugfs_cleanup = qxl_debugfs_takedown,
  188. #endif
  189. .gem_free_object = qxl_gem_object_free,
  190. .gem_open_object = qxl_gem_object_open,
  191. .gem_close_object = qxl_gem_object_close,
  192. .fops = &qxl_fops,
  193. .ioctls = qxl_ioctls,
  194. .irq_handler = qxl_irq_handler,
  195. .name = DRIVER_NAME,
  196. .desc = DRIVER_DESC,
  197. .date = DRIVER_DATE,
  198. .major = 0,
  199. .minor = 1,
  200. .patchlevel = 0,
  201. };
  202. static int __init qxl_init(void)
  203. {
  204. #ifdef CONFIG_VGA_CONSOLE
  205. if (vgacon_text_force() && qxl_modeset == -1)
  206. return -EINVAL;
  207. #endif
  208. if (qxl_modeset == 0)
  209. return -EINVAL;
  210. qxl_driver.num_ioctls = qxl_max_ioctls;
  211. return drm_pci_init(&qxl_driver, &qxl_pci_driver);
  212. }
  213. static void __exit qxl_exit(void)
  214. {
  215. drm_pci_exit(&qxl_driver, &qxl_pci_driver);
  216. }
  217. module_init(qxl_init);
  218. module_exit(qxl_exit);
  219. MODULE_AUTHOR(DRIVER_AUTHOR);
  220. MODULE_DESCRIPTION(DRIVER_DESC);
  221. MODULE_LICENSE("GPL and additional rights");