qxl_drv.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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 const struct pci_device_id 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. .read = drm_read,
  77. .mmap = qxl_mmap,
  78. };
  79. static int qxl_drm_freeze(struct drm_device *dev)
  80. {
  81. struct pci_dev *pdev = dev->pdev;
  82. struct qxl_device *qdev = dev->dev_private;
  83. struct drm_crtc *crtc;
  84. drm_kms_helper_poll_disable(dev);
  85. console_lock();
  86. qxl_fbdev_set_suspend(qdev, 1);
  87. console_unlock();
  88. /* unpin the front buffers */
  89. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  90. const struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  91. if (crtc->enabled)
  92. (*crtc_funcs->disable)(crtc);
  93. }
  94. qxl_destroy_monitors_object(qdev);
  95. qxl_surf_evict(qdev);
  96. qxl_vram_evict(qdev);
  97. while (!qxl_check_idle(qdev->command_ring));
  98. while (!qxl_check_idle(qdev->release_ring))
  99. qxl_queue_garbage_collect(qdev, 1);
  100. pci_save_state(pdev);
  101. return 0;
  102. }
  103. static int qxl_drm_resume(struct drm_device *dev, bool thaw)
  104. {
  105. struct qxl_device *qdev = dev->dev_private;
  106. qdev->ram_header->int_mask = QXL_INTERRUPT_MASK;
  107. if (!thaw) {
  108. qxl_reinit_memslots(qdev);
  109. qxl_ring_init_hdr(qdev->release_ring);
  110. }
  111. qxl_create_monitors_object(qdev);
  112. drm_helper_resume_force_mode(dev);
  113. console_lock();
  114. qxl_fbdev_set_suspend(qdev, 0);
  115. console_unlock();
  116. drm_kms_helper_poll_enable(dev);
  117. return 0;
  118. }
  119. static int qxl_pm_suspend(struct device *dev)
  120. {
  121. struct pci_dev *pdev = to_pci_dev(dev);
  122. struct drm_device *drm_dev = pci_get_drvdata(pdev);
  123. int error;
  124. error = qxl_drm_freeze(drm_dev);
  125. if (error)
  126. return error;
  127. pci_disable_device(pdev);
  128. pci_set_power_state(pdev, PCI_D3hot);
  129. return 0;
  130. }
  131. static int qxl_pm_resume(struct device *dev)
  132. {
  133. struct pci_dev *pdev = to_pci_dev(dev);
  134. struct drm_device *drm_dev = pci_get_drvdata(pdev);
  135. pci_set_power_state(pdev, PCI_D0);
  136. pci_restore_state(pdev);
  137. if (pci_enable_device(pdev)) {
  138. return -EIO;
  139. }
  140. return qxl_drm_resume(drm_dev, false);
  141. }
  142. static int qxl_pm_thaw(struct device *dev)
  143. {
  144. struct pci_dev *pdev = to_pci_dev(dev);
  145. struct drm_device *drm_dev = pci_get_drvdata(pdev);
  146. return qxl_drm_resume(drm_dev, true);
  147. }
  148. static int qxl_pm_freeze(struct device *dev)
  149. {
  150. struct pci_dev *pdev = to_pci_dev(dev);
  151. struct drm_device *drm_dev = pci_get_drvdata(pdev);
  152. return qxl_drm_freeze(drm_dev);
  153. }
  154. static int qxl_pm_restore(struct device *dev)
  155. {
  156. struct pci_dev *pdev = to_pci_dev(dev);
  157. struct drm_device *drm_dev = pci_get_drvdata(pdev);
  158. struct qxl_device *qdev = drm_dev->dev_private;
  159. qxl_io_reset(qdev);
  160. return qxl_drm_resume(drm_dev, false);
  161. }
  162. static u32 qxl_noop_get_vblank_counter(struct drm_device *dev,
  163. unsigned int pipe)
  164. {
  165. return 0;
  166. }
  167. static int qxl_noop_enable_vblank(struct drm_device *dev, unsigned int pipe)
  168. {
  169. return 0;
  170. }
  171. static void qxl_noop_disable_vblank(struct drm_device *dev, unsigned int pipe)
  172. {
  173. }
  174. static const struct dev_pm_ops qxl_pm_ops = {
  175. .suspend = qxl_pm_suspend,
  176. .resume = qxl_pm_resume,
  177. .freeze = qxl_pm_freeze,
  178. .thaw = qxl_pm_thaw,
  179. .poweroff = qxl_pm_freeze,
  180. .restore = qxl_pm_restore,
  181. };
  182. static struct pci_driver qxl_pci_driver = {
  183. .name = DRIVER_NAME,
  184. .id_table = pciidlist,
  185. .probe = qxl_pci_probe,
  186. .remove = qxl_pci_remove,
  187. .driver.pm = &qxl_pm_ops,
  188. };
  189. static struct drm_driver qxl_driver = {
  190. .driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_PRIME |
  191. DRIVER_HAVE_IRQ | DRIVER_IRQ_SHARED,
  192. .load = qxl_driver_load,
  193. .unload = qxl_driver_unload,
  194. .get_vblank_counter = qxl_noop_get_vblank_counter,
  195. .enable_vblank = qxl_noop_enable_vblank,
  196. .disable_vblank = qxl_noop_disable_vblank,
  197. .set_busid = drm_pci_set_busid,
  198. .dumb_create = qxl_mode_dumb_create,
  199. .dumb_map_offset = qxl_mode_dumb_mmap,
  200. .dumb_destroy = drm_gem_dumb_destroy,
  201. #if defined(CONFIG_DEBUG_FS)
  202. .debugfs_init = qxl_debugfs_init,
  203. .debugfs_cleanup = qxl_debugfs_takedown,
  204. #endif
  205. .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
  206. .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
  207. .gem_prime_export = drm_gem_prime_export,
  208. .gem_prime_import = drm_gem_prime_import,
  209. .gem_prime_pin = qxl_gem_prime_pin,
  210. .gem_prime_unpin = qxl_gem_prime_unpin,
  211. .gem_prime_get_sg_table = qxl_gem_prime_get_sg_table,
  212. .gem_prime_import_sg_table = qxl_gem_prime_import_sg_table,
  213. .gem_prime_vmap = qxl_gem_prime_vmap,
  214. .gem_prime_vunmap = qxl_gem_prime_vunmap,
  215. .gem_prime_mmap = qxl_gem_prime_mmap,
  216. .gem_free_object = qxl_gem_object_free,
  217. .gem_open_object = qxl_gem_object_open,
  218. .gem_close_object = qxl_gem_object_close,
  219. .fops = &qxl_fops,
  220. .ioctls = qxl_ioctls,
  221. .irq_handler = qxl_irq_handler,
  222. .name = DRIVER_NAME,
  223. .desc = DRIVER_DESC,
  224. .date = DRIVER_DATE,
  225. .major = 0,
  226. .minor = 1,
  227. .patchlevel = 0,
  228. };
  229. static int __init qxl_init(void)
  230. {
  231. #ifdef CONFIG_VGA_CONSOLE
  232. if (vgacon_text_force() && qxl_modeset == -1)
  233. return -EINVAL;
  234. #endif
  235. if (qxl_modeset == 0)
  236. return -EINVAL;
  237. qxl_driver.num_ioctls = qxl_max_ioctls;
  238. return drm_pci_init(&qxl_driver, &qxl_pci_driver);
  239. }
  240. static void __exit qxl_exit(void)
  241. {
  242. drm_pci_exit(&qxl_driver, &qxl_pci_driver);
  243. }
  244. module_init(qxl_init);
  245. module_exit(qxl_exit);
  246. MODULE_AUTHOR(DRIVER_AUTHOR);
  247. MODULE_DESCRIPTION(DRIVER_DESC);
  248. MODULE_LICENSE("GPL and additional rights");