qxl_drv.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /* qxl_drv.c -- QXL driver -*- linux-c -*-
  2. *
  3. * Copyright 2011 Red Hat, Inc.
  4. * All Rights Reserved.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a
  7. * copy of this software and associated documentation files (the "Software"),
  8. * to deal in the Software without restriction, including without limitation
  9. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  10. * and/or sell copies of the Software, and to permit persons to whom the
  11. * Software is furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice (including the next
  14. * paragraph) shall be included in all copies or substantial portions of the
  15. * Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  20. * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  21. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  22. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  23. * OTHER DEALINGS IN THE SOFTWARE.
  24. *
  25. * Authors:
  26. * Dave Airlie <[email protected]>
  27. * Alon Levy <[email protected]>
  28. */
  29. #include "qxl_drv.h"
  30. #include <linux/module.h>
  31. #include <linux/pci.h>
  32. #include <linux/vgaarb.h>
  33. #include <drm/drm.h>
  34. #include <drm/drm_aperture.h>
  35. #include <drm/drm_atomic_helper.h>
  36. #include <drm/drm_drv.h>
  37. #include <drm/drm_file.h>
  38. #include <drm/drm_gem_ttm_helper.h>
  39. #include <drm/drm_module.h>
  40. #include <drm/drm_modeset_helper.h>
  41. #include <drm/drm_prime.h>
  42. #include <drm/drm_probe_helper.h>
  43. #include "qxl_object.h"
  44. static const struct pci_device_id pciidlist[] = {
  45. { 0x1b36, 0x100, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_DISPLAY_VGA << 8,
  46. 0xffff00, 0 },
  47. { 0x1b36, 0x100, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_DISPLAY_OTHER << 8,
  48. 0xffff00, 0 },
  49. { 0, 0, 0 },
  50. };
  51. MODULE_DEVICE_TABLE(pci, pciidlist);
  52. static int qxl_modeset = -1;
  53. int qxl_num_crtc = 4;
  54. MODULE_PARM_DESC(modeset, "Disable/Enable modesetting");
  55. module_param_named(modeset, qxl_modeset, int, 0400);
  56. MODULE_PARM_DESC(num_heads, "Number of virtual crtcs to expose (default 4)");
  57. module_param_named(num_heads, qxl_num_crtc, int, 0400);
  58. static struct drm_driver qxl_driver;
  59. static struct pci_driver qxl_pci_driver;
  60. static bool is_vga(struct pci_dev *pdev)
  61. {
  62. return pdev->class == PCI_CLASS_DISPLAY_VGA << 8;
  63. }
  64. static int
  65. qxl_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  66. {
  67. struct qxl_device *qdev;
  68. int ret;
  69. if (pdev->revision < 4) {
  70. DRM_ERROR("qxl too old, doesn't support client_monitors_config,"
  71. " use xf86-video-qxl in user mode");
  72. return -EINVAL; /* TODO: ENODEV ? */
  73. }
  74. qdev = devm_drm_dev_alloc(&pdev->dev, &qxl_driver,
  75. struct qxl_device, ddev);
  76. if (IS_ERR(qdev)) {
  77. pr_err("Unable to init drm dev");
  78. return -ENOMEM;
  79. }
  80. ret = pci_enable_device(pdev);
  81. if (ret)
  82. return ret;
  83. ret = drm_aperture_remove_conflicting_pci_framebuffers(pdev, &qxl_driver);
  84. if (ret)
  85. goto disable_pci;
  86. if (is_vga(pdev) && pdev->revision < 5) {
  87. ret = vga_get_interruptible(pdev, VGA_RSRC_LEGACY_IO);
  88. if (ret) {
  89. DRM_ERROR("can't get legacy vga ioports\n");
  90. goto disable_pci;
  91. }
  92. }
  93. ret = qxl_device_init(qdev, pdev);
  94. if (ret)
  95. goto put_vga;
  96. ret = qxl_modeset_init(qdev);
  97. if (ret)
  98. goto unload;
  99. drm_kms_helper_poll_init(&qdev->ddev);
  100. /* Complete initialization. */
  101. ret = drm_dev_register(&qdev->ddev, ent->driver_data);
  102. if (ret)
  103. goto modeset_cleanup;
  104. drm_fbdev_generic_setup(&qdev->ddev, 32);
  105. return 0;
  106. modeset_cleanup:
  107. qxl_modeset_fini(qdev);
  108. unload:
  109. qxl_device_fini(qdev);
  110. put_vga:
  111. if (is_vga(pdev) && pdev->revision < 5)
  112. vga_put(pdev, VGA_RSRC_LEGACY_IO);
  113. disable_pci:
  114. pci_disable_device(pdev);
  115. return ret;
  116. }
  117. static void qxl_drm_release(struct drm_device *dev)
  118. {
  119. struct qxl_device *qdev = to_qxl(dev);
  120. /*
  121. * TODO: qxl_device_fini() call should be in qxl_pci_remove(),
  122. * reordering qxl_modeset_fini() + qxl_device_fini() calls is
  123. * non-trivial though.
  124. */
  125. qxl_modeset_fini(qdev);
  126. qxl_device_fini(qdev);
  127. }
  128. static void
  129. qxl_pci_remove(struct pci_dev *pdev)
  130. {
  131. struct drm_device *dev = pci_get_drvdata(pdev);
  132. drm_dev_unregister(dev);
  133. drm_atomic_helper_shutdown(dev);
  134. if (is_vga(pdev) && pdev->revision < 5)
  135. vga_put(pdev, VGA_RSRC_LEGACY_IO);
  136. }
  137. DEFINE_DRM_GEM_FOPS(qxl_fops);
  138. static int qxl_drm_freeze(struct drm_device *dev)
  139. {
  140. struct pci_dev *pdev = to_pci_dev(dev->dev);
  141. struct qxl_device *qdev = to_qxl(dev);
  142. int ret;
  143. ret = drm_mode_config_helper_suspend(dev);
  144. if (ret)
  145. return ret;
  146. qxl_destroy_monitors_object(qdev);
  147. qxl_surf_evict(qdev);
  148. qxl_vram_evict(qdev);
  149. while (!qxl_check_idle(qdev->command_ring));
  150. while (!qxl_check_idle(qdev->release_ring))
  151. qxl_queue_garbage_collect(qdev, 1);
  152. pci_save_state(pdev);
  153. return 0;
  154. }
  155. static int qxl_drm_resume(struct drm_device *dev, bool thaw)
  156. {
  157. struct qxl_device *qdev = to_qxl(dev);
  158. qdev->ram_header->int_mask = QXL_INTERRUPT_MASK;
  159. if (!thaw) {
  160. qxl_reinit_memslots(qdev);
  161. }
  162. qxl_create_monitors_object(qdev);
  163. return drm_mode_config_helper_resume(dev);
  164. }
  165. static int qxl_pm_suspend(struct device *dev)
  166. {
  167. struct pci_dev *pdev = to_pci_dev(dev);
  168. struct drm_device *drm_dev = pci_get_drvdata(pdev);
  169. int error;
  170. error = qxl_drm_freeze(drm_dev);
  171. if (error)
  172. return error;
  173. pci_disable_device(pdev);
  174. pci_set_power_state(pdev, PCI_D3hot);
  175. return 0;
  176. }
  177. static int qxl_pm_resume(struct device *dev)
  178. {
  179. struct pci_dev *pdev = to_pci_dev(dev);
  180. struct drm_device *drm_dev = pci_get_drvdata(pdev);
  181. struct qxl_device *qdev = to_qxl(drm_dev);
  182. pci_set_power_state(pdev, PCI_D0);
  183. pci_restore_state(pdev);
  184. if (pci_enable_device(pdev)) {
  185. return -EIO;
  186. }
  187. qxl_io_reset(qdev);
  188. return qxl_drm_resume(drm_dev, false);
  189. }
  190. static int qxl_pm_thaw(struct device *dev)
  191. {
  192. struct drm_device *drm_dev = dev_get_drvdata(dev);
  193. return qxl_drm_resume(drm_dev, true);
  194. }
  195. static int qxl_pm_freeze(struct device *dev)
  196. {
  197. struct drm_device *drm_dev = dev_get_drvdata(dev);
  198. return qxl_drm_freeze(drm_dev);
  199. }
  200. static int qxl_pm_restore(struct device *dev)
  201. {
  202. struct pci_dev *pdev = to_pci_dev(dev);
  203. struct drm_device *drm_dev = pci_get_drvdata(pdev);
  204. struct qxl_device *qdev = to_qxl(drm_dev);
  205. qxl_io_reset(qdev);
  206. return qxl_drm_resume(drm_dev, false);
  207. }
  208. static const struct dev_pm_ops qxl_pm_ops = {
  209. .suspend = qxl_pm_suspend,
  210. .resume = qxl_pm_resume,
  211. .freeze = qxl_pm_freeze,
  212. .thaw = qxl_pm_thaw,
  213. .poweroff = qxl_pm_freeze,
  214. .restore = qxl_pm_restore,
  215. };
  216. static struct pci_driver qxl_pci_driver = {
  217. .name = DRIVER_NAME,
  218. .id_table = pciidlist,
  219. .probe = qxl_pci_probe,
  220. .remove = qxl_pci_remove,
  221. .driver.pm = &qxl_pm_ops,
  222. };
  223. static const struct drm_ioctl_desc qxl_ioctls[] = {
  224. DRM_IOCTL_DEF_DRV(QXL_ALLOC, qxl_alloc_ioctl, DRM_AUTH),
  225. DRM_IOCTL_DEF_DRV(QXL_MAP, qxl_map_ioctl, DRM_AUTH),
  226. DRM_IOCTL_DEF_DRV(QXL_EXECBUFFER, qxl_execbuffer_ioctl, DRM_AUTH),
  227. DRM_IOCTL_DEF_DRV(QXL_UPDATE_AREA, qxl_update_area_ioctl, DRM_AUTH),
  228. DRM_IOCTL_DEF_DRV(QXL_GETPARAM, qxl_getparam_ioctl, DRM_AUTH),
  229. DRM_IOCTL_DEF_DRV(QXL_CLIENTCAP, qxl_clientcap_ioctl, DRM_AUTH),
  230. DRM_IOCTL_DEF_DRV(QXL_ALLOC_SURF, qxl_alloc_surf_ioctl, DRM_AUTH),
  231. };
  232. static struct drm_driver qxl_driver = {
  233. .driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_ATOMIC,
  234. .dumb_create = qxl_mode_dumb_create,
  235. .dumb_map_offset = drm_gem_ttm_dumb_map_offset,
  236. #if defined(CONFIG_DEBUG_FS)
  237. .debugfs_init = qxl_debugfs_init,
  238. #endif
  239. .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
  240. .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
  241. .gem_prime_import_sg_table = qxl_gem_prime_import_sg_table,
  242. .fops = &qxl_fops,
  243. .ioctls = qxl_ioctls,
  244. .num_ioctls = ARRAY_SIZE(qxl_ioctls),
  245. .name = DRIVER_NAME,
  246. .desc = DRIVER_DESC,
  247. .date = DRIVER_DATE,
  248. .major = 0,
  249. .minor = 1,
  250. .patchlevel = 0,
  251. .release = qxl_drm_release,
  252. };
  253. drm_module_pci_driver_if_modeset(qxl_pci_driver, qxl_modeset);
  254. MODULE_AUTHOR(DRIVER_AUTHOR);
  255. MODULE_DESCRIPTION(DRIVER_DESC);
  256. MODULE_LICENSE("GPL and additional rights");