drm_irq.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. * drm_irq_install - install IRQ handler
  60. * @dev: DRM device
  61. * @irq: IRQ number to install the handler for
  62. *
  63. * Initializes the IRQ related data. Installs the handler, calling the driver
  64. * irq_preinstall() and irq_postinstall() functions before and after the
  65. * installation.
  66. *
  67. * This is the simplified helper interface provided for drivers with no special
  68. * needs. Drivers which need to install interrupt handlers for multiple
  69. * interrupts must instead set &drm_device.irq_enabled to signal the DRM core
  70. * that vblank interrupts are available.
  71. *
  72. * Returns:
  73. * Zero on success or a negative error code on failure.
  74. */
  75. int drm_irq_install(struct drm_device *dev, int irq)
  76. {
  77. int ret;
  78. unsigned long sh_flags = 0;
  79. if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
  80. return -EINVAL;
  81. if (irq == 0)
  82. return -EINVAL;
  83. /* Driver must have been initialized */
  84. if (!dev->dev_private)
  85. return -EINVAL;
  86. if (dev->irq_enabled)
  87. return -EBUSY;
  88. dev->irq_enabled = true;
  89. DRM_DEBUG("irq=%d\n", irq);
  90. /* Before installing handler */
  91. if (dev->driver->irq_preinstall)
  92. dev->driver->irq_preinstall(dev);
  93. /* Install handler */
  94. if (drm_core_check_feature(dev, DRIVER_IRQ_SHARED))
  95. sh_flags = IRQF_SHARED;
  96. ret = request_irq(irq, dev->driver->irq_handler,
  97. sh_flags, dev->driver->name, dev);
  98. if (ret < 0) {
  99. dev->irq_enabled = false;
  100. return ret;
  101. }
  102. /* After installing handler */
  103. if (dev->driver->irq_postinstall)
  104. ret = dev->driver->irq_postinstall(dev);
  105. if (ret < 0) {
  106. dev->irq_enabled = false;
  107. if (drm_core_check_feature(dev, DRIVER_LEGACY))
  108. vga_client_register(dev->pdev, NULL, NULL, NULL);
  109. free_irq(irq, dev);
  110. } else {
  111. dev->irq = irq;
  112. }
  113. return ret;
  114. }
  115. EXPORT_SYMBOL(drm_irq_install);
  116. /**
  117. * drm_irq_uninstall - uninstall the IRQ handler
  118. * @dev: DRM device
  119. *
  120. * Calls the driver's irq_uninstall() function and unregisters the IRQ handler.
  121. * This should only be called by drivers which used drm_irq_install() to set up
  122. * their interrupt handler. Other drivers must only reset
  123. * &drm_device.irq_enabled to false.
  124. *
  125. * Note that for kernel modesetting drivers it is a bug if this function fails.
  126. * The sanity checks are only to catch buggy user modesetting drivers which call
  127. * the same function through an ioctl.
  128. *
  129. * Returns:
  130. * Zero on success or a negative error code on failure.
  131. */
  132. int drm_irq_uninstall(struct drm_device *dev)
  133. {
  134. unsigned long irqflags;
  135. bool irq_enabled;
  136. int i;
  137. if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
  138. return -EINVAL;
  139. irq_enabled = dev->irq_enabled;
  140. dev->irq_enabled = false;
  141. /*
  142. * Wake up any waiters so they don't hang. This is just to paper over
  143. * issues for UMS drivers which aren't in full control of their
  144. * vblank/irq handling. KMS drivers must ensure that vblanks are all
  145. * disabled when uninstalling the irq handler.
  146. */
  147. if (dev->num_crtcs) {
  148. spin_lock_irqsave(&dev->vbl_lock, irqflags);
  149. for (i = 0; i < dev->num_crtcs; i++) {
  150. struct drm_vblank_crtc *vblank = &dev->vblank[i];
  151. if (!vblank->enabled)
  152. continue;
  153. WARN_ON(drm_core_check_feature(dev, DRIVER_MODESET));
  154. drm_vblank_disable_and_save(dev, i);
  155. wake_up(&vblank->queue);
  156. }
  157. spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
  158. }
  159. if (!irq_enabled)
  160. return -EINVAL;
  161. DRM_DEBUG("irq=%d\n", dev->irq);
  162. if (drm_core_check_feature(dev, DRIVER_LEGACY))
  163. vga_client_register(dev->pdev, NULL, NULL, NULL);
  164. if (dev->driver->irq_uninstall)
  165. dev->driver->irq_uninstall(dev);
  166. free_irq(dev->irq, dev);
  167. return 0;
  168. }
  169. EXPORT_SYMBOL(drm_irq_uninstall);
  170. int drm_legacy_irq_control(struct drm_device *dev, void *data,
  171. struct drm_file *file_priv)
  172. {
  173. struct drm_control *ctl = data;
  174. int ret = 0, irq;
  175. /* if we haven't irq we fallback for compatibility reasons -
  176. * this used to be a separate function in drm_dma.h
  177. */
  178. if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
  179. return 0;
  180. if (!drm_core_check_feature(dev, DRIVER_LEGACY))
  181. return 0;
  182. /* UMS was only ever supported on pci devices. */
  183. if (WARN_ON(!dev->pdev))
  184. return -EINVAL;
  185. switch (ctl->func) {
  186. case DRM_INST_HANDLER:
  187. irq = dev->pdev->irq;
  188. if (dev->if_version < DRM_IF_VERSION(1, 2) &&
  189. ctl->irq != irq)
  190. return -EINVAL;
  191. mutex_lock(&dev->struct_mutex);
  192. ret = drm_irq_install(dev, irq);
  193. mutex_unlock(&dev->struct_mutex);
  194. return ret;
  195. case DRM_UNINST_HANDLER:
  196. mutex_lock(&dev->struct_mutex);
  197. ret = drm_irq_uninstall(dev);
  198. mutex_unlock(&dev->struct_mutex);
  199. return ret;
  200. default:
  201. return -EINVAL;
  202. }
  203. }