drm_irq.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /*
  2. * drm_irq.c IRQ and vblank support
  3. *
  4. * \author Rickard E. (Rik) Faith <faith@valinux.com>
  5. * \author Gareth Hughes <gareth@valinux.com>
  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. /*
  27. * Created: Fri Mar 19 14:30:16 1999 by faith@valinux.com
  28. *
  29. * Copyright 1999, 2000 Precision Insight, Inc., Cedar Park, Texas.
  30. * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
  31. * All Rights Reserved.
  32. *
  33. * Permission is hereby granted, free of charge, to any person obtaining a
  34. * copy of this software and associated documentation files (the "Software"),
  35. * to deal in the Software without restriction, including without limitation
  36. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  37. * and/or sell copies of the Software, and to permit persons to whom the
  38. * Software is furnished to do so, subject to the following conditions:
  39. *
  40. * The above copyright notice and this permission notice (including the next
  41. * paragraph) shall be included in all copies or substantial portions of the
  42. * Software.
  43. *
  44. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  45. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  46. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  47. * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  48. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  49. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  50. * OTHER DEALINGS IN THE SOFTWARE.
  51. */
  52. #include <linux/export.h>
  53. #include <linux/interrupt.h> /* For task queue support */
  54. #include <linux/pci.h>
  55. #include <linux/vgaarb.h>
  56. #include <drm/drm.h>
  57. #include <drm/drm_device.h>
  58. #include <drm/drm_drv.h>
  59. #include <drm/drm_irq.h>
  60. #include <drm/drm_print.h>
  61. #include <drm/drm_vblank.h>
  62. #include "drm_internal.h"
  63. /**
  64. * DOC: irq helpers
  65. *
  66. * The DRM core provides very simple support helpers to enable IRQ handling on a
  67. * device through the drm_irq_install() and drm_irq_uninstall() functions. This
  68. * only supports devices with a single interrupt on the main device stored in
  69. * &drm_device.dev and set as the device paramter in drm_dev_alloc().
  70. *
  71. * These IRQ helpers are strictly optional. Drivers which roll their own only
  72. * need to set &drm_device.irq_enabled to signal the DRM core that vblank
  73. * interrupts are working. Since these helpers don't automatically clean up the
  74. * requested interrupt like e.g. devm_request_irq() they're not really
  75. * recommended.
  76. */
  77. /**
  78. * drm_irq_install - install IRQ handler
  79. * @dev: DRM device
  80. * @irq: IRQ number to install the handler for
  81. *
  82. * Initializes the IRQ related data. Installs the handler, calling the driver
  83. * &drm_driver.irq_preinstall and &drm_driver.irq_postinstall functions before
  84. * and after the installation.
  85. *
  86. * This is the simplified helper interface provided for drivers with no special
  87. * needs. Drivers which need to install interrupt handlers for multiple
  88. * interrupts must instead set &drm_device.irq_enabled to signal the DRM core
  89. * that vblank interrupts are available.
  90. *
  91. * @irq must match the interrupt number that would be passed to request_irq(),
  92. * if called directly instead of using this helper function.
  93. *
  94. * &drm_driver.irq_handler is called to handle the registered interrupt.
  95. *
  96. * Returns:
  97. * Zero on success or a negative error code on failure.
  98. */
  99. int drm_irq_install(struct drm_device *dev, int irq)
  100. {
  101. int ret;
  102. unsigned long sh_flags = 0;
  103. if (irq == 0)
  104. return -EINVAL;
  105. if (dev->irq_enabled)
  106. return -EBUSY;
  107. dev->irq_enabled = true;
  108. DRM_DEBUG("irq=%d\n", irq);
  109. /* Before installing handler */
  110. if (dev->driver->irq_preinstall)
  111. dev->driver->irq_preinstall(dev);
  112. /* PCI devices require shared interrupts. */
  113. if (dev->pdev)
  114. sh_flags = IRQF_SHARED;
  115. ret = request_irq(irq, dev->driver->irq_handler,
  116. sh_flags, dev->driver->name, dev);
  117. if (ret < 0) {
  118. dev->irq_enabled = false;
  119. return ret;
  120. }
  121. /* After installing handler */
  122. if (dev->driver->irq_postinstall)
  123. ret = dev->driver->irq_postinstall(dev);
  124. if (ret < 0) {
  125. dev->irq_enabled = false;
  126. if (drm_core_check_feature(dev, DRIVER_LEGACY))
  127. vga_client_register(dev->pdev, NULL, NULL, NULL);
  128. free_irq(irq, dev);
  129. } else {
  130. dev->irq = irq;
  131. }
  132. return ret;
  133. }
  134. EXPORT_SYMBOL(drm_irq_install);
  135. /**
  136. * drm_irq_uninstall - uninstall the IRQ handler
  137. * @dev: DRM device
  138. *
  139. * Calls the driver's &drm_driver.irq_uninstall function and unregisters the IRQ
  140. * handler. This should only be called by drivers which used drm_irq_install()
  141. * to set up their interrupt handler. Other drivers must only reset
  142. * &drm_device.irq_enabled to false.
  143. *
  144. * Note that for kernel modesetting drivers it is a bug if this function fails.
  145. * The sanity checks are only to catch buggy user modesetting drivers which call
  146. * the same function through an ioctl.
  147. *
  148. * Returns:
  149. * Zero on success or a negative error code on failure.
  150. */
  151. int drm_irq_uninstall(struct drm_device *dev)
  152. {
  153. unsigned long irqflags;
  154. bool irq_enabled;
  155. int i;
  156. irq_enabled = dev->irq_enabled;
  157. dev->irq_enabled = false;
  158. /*
  159. * Wake up any waiters so they don't hang. This is just to paper over
  160. * issues for UMS drivers which aren't in full control of their
  161. * vblank/irq handling. KMS drivers must ensure that vblanks are all
  162. * disabled when uninstalling the irq handler.
  163. */
  164. if (drm_dev_has_vblank(dev)) {
  165. spin_lock_irqsave(&dev->vbl_lock, irqflags);
  166. for (i = 0; i < dev->num_crtcs; i++) {
  167. struct drm_vblank_crtc *vblank = &dev->vblank[i];
  168. if (!vblank->enabled)
  169. continue;
  170. WARN_ON(drm_core_check_feature(dev, DRIVER_MODESET));
  171. drm_vblank_disable_and_save(dev, i);
  172. wake_up(&vblank->queue);
  173. }
  174. spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
  175. }
  176. if (!irq_enabled)
  177. return -EINVAL;
  178. DRM_DEBUG("irq=%d\n", dev->irq);
  179. if (drm_core_check_feature(dev, DRIVER_LEGACY))
  180. vga_client_register(dev->pdev, NULL, NULL, NULL);
  181. if (dev->driver->irq_uninstall)
  182. dev->driver->irq_uninstall(dev);
  183. free_irq(dev->irq, dev);
  184. return 0;
  185. }
  186. EXPORT_SYMBOL(drm_irq_uninstall);
  187. #if IS_ENABLED(CONFIG_DRM_LEGACY)
  188. int drm_legacy_irq_control(struct drm_device *dev, void *data,
  189. struct drm_file *file_priv)
  190. {
  191. struct drm_control *ctl = data;
  192. int ret = 0, irq;
  193. /* if we haven't irq we fallback for compatibility reasons -
  194. * this used to be a separate function in drm_dma.h
  195. */
  196. if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
  197. return 0;
  198. if (!drm_core_check_feature(dev, DRIVER_LEGACY))
  199. return 0;
  200. /* UMS was only ever supported on pci devices. */
  201. if (WARN_ON(!dev->pdev))
  202. return -EINVAL;
  203. switch (ctl->func) {
  204. case DRM_INST_HANDLER:
  205. irq = dev->pdev->irq;
  206. if (dev->if_version < DRM_IF_VERSION(1, 2) &&
  207. ctl->irq != irq)
  208. return -EINVAL;
  209. mutex_lock(&dev->struct_mutex);
  210. ret = drm_irq_install(dev, irq);
  211. mutex_unlock(&dev->struct_mutex);
  212. return ret;
  213. case DRM_UNINST_HANDLER:
  214. mutex_lock(&dev->struct_mutex);
  215. ret = drm_irq_uninstall(dev);
  216. mutex_unlock(&dev->struct_mutex);
  217. return ret;
  218. default:
  219. return -EINVAL;
  220. }
  221. }
  222. #endif