drm_irq.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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 <drm/drm_irq.h>
  53. #include <drm/drmP.h>
  54. #include <linux/interrupt.h> /* For task queue support */
  55. #include <linux/vgaarb.h>
  56. #include <linux/export.h>
  57. #include "drm_internal.h"
  58. /**
  59. * DOC: irq helpers
  60. *
  61. * The DRM core provides very simple support helpers to enable IRQ handling on a
  62. * device through the drm_irq_install() and drm_irq_uninstall() functions. This
  63. * only supports devices with a single interrupt on the main device stored in
  64. * &drm_device.dev and set as the device paramter in drm_dev_alloc().
  65. *
  66. * These IRQ helpers are strictly optional. Drivers which roll their own only
  67. * need to set &drm_device.irq_enabled to signal the DRM core that vblank
  68. * interrupts are working. Since these helpers don't automatically clean up the
  69. * requested interrupt like e.g. devm_request_irq() they're not really
  70. * recommended.
  71. */
  72. /**
  73. * drm_irq_install - install IRQ handler
  74. * @dev: DRM device
  75. * @irq: IRQ number to install the handler for
  76. *
  77. * Initializes the IRQ related data. Installs the handler, calling the driver
  78. * &drm_driver.irq_preinstall and &drm_driver.irq_postinstall functions before
  79. * and after the installation.
  80. *
  81. * This is the simplified helper interface provided for drivers with no special
  82. * needs. Drivers which need to install interrupt handlers for multiple
  83. * interrupts must instead set &drm_device.irq_enabled to signal the DRM core
  84. * that vblank interrupts are available.
  85. *
  86. * @irq must match the interrupt number that would be passed to request_irq(),
  87. * if called directly instead of using this helper function.
  88. *
  89. * &drm_driver.irq_handler is called to handle the registered interrupt.
  90. *
  91. * Returns:
  92. * Zero on success or a negative error code on failure.
  93. */
  94. int drm_irq_install(struct drm_device *dev, int irq)
  95. {
  96. int ret;
  97. unsigned long sh_flags = 0;
  98. if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
  99. return -EINVAL;
  100. if (irq == 0)
  101. return -EINVAL;
  102. /* Driver must have been initialized */
  103. if (!dev->dev_private)
  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. /* Install handler */
  113. if (drm_core_check_feature(dev, DRIVER_IRQ_SHARED))
  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. if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
  157. return -EINVAL;
  158. irq_enabled = dev->irq_enabled;
  159. dev->irq_enabled = false;
  160. /*
  161. * Wake up any waiters so they don't hang. This is just to paper over
  162. * issues for UMS drivers which aren't in full control of their
  163. * vblank/irq handling. KMS drivers must ensure that vblanks are all
  164. * disabled when uninstalling the irq handler.
  165. */
  166. if (dev->num_crtcs) {
  167. spin_lock_irqsave(&dev->vbl_lock, irqflags);
  168. for (i = 0; i < dev->num_crtcs; i++) {
  169. struct drm_vblank_crtc *vblank = &dev->vblank[i];
  170. if (!vblank->enabled)
  171. continue;
  172. WARN_ON(drm_core_check_feature(dev, DRIVER_MODESET));
  173. drm_vblank_disable_and_save(dev, i);
  174. wake_up(&vblank->queue);
  175. }
  176. spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
  177. }
  178. if (!irq_enabled)
  179. return -EINVAL;
  180. DRM_DEBUG("irq=%d\n", dev->irq);
  181. if (drm_core_check_feature(dev, DRIVER_LEGACY))
  182. vga_client_register(dev->pdev, NULL, NULL, NULL);
  183. if (dev->driver->irq_uninstall)
  184. dev->driver->irq_uninstall(dev);
  185. free_irq(dev->irq, dev);
  186. return 0;
  187. }
  188. EXPORT_SYMBOL(drm_irq_uninstall);
  189. int drm_legacy_irq_control(struct drm_device *dev, void *data,
  190. struct drm_file *file_priv)
  191. {
  192. struct drm_control *ctl = data;
  193. int ret = 0, irq;
  194. /* if we haven't irq we fallback for compatibility reasons -
  195. * this used to be a separate function in drm_dma.h
  196. */
  197. if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
  198. return 0;
  199. if (!drm_core_check_feature(dev, DRIVER_LEGACY))
  200. return 0;
  201. /* UMS was only ever supported on pci devices. */
  202. if (WARN_ON(!dev->pdev))
  203. return -EINVAL;
  204. switch (ctl->func) {
  205. case DRM_INST_HANDLER:
  206. irq = dev->pdev->irq;
  207. if (dev->if_version < DRM_IF_VERSION(1, 2) &&
  208. ctl->irq != irq)
  209. return -EINVAL;
  210. mutex_lock(&dev->struct_mutex);
  211. ret = drm_irq_install(dev, irq);
  212. mutex_unlock(&dev->struct_mutex);
  213. return ret;
  214. case DRM_UNINST_HANDLER:
  215. mutex_lock(&dev->struct_mutex);
  216. ret = drm_irq_uninstall(dev);
  217. mutex_unlock(&dev->struct_mutex);
  218. return ret;
  219. default:
  220. return -EINVAL;
  221. }
  222. }