drm_irq.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. * drm_irq.c IRQ and vblank support
  3. *
  4. * \author Rickard E. (Rik) Faith <[email protected]>
  5. * \author Gareth Hughes <[email protected]>
  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 [email protected]
  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_legacy.h>
  60. #include <drm/drm_print.h>
  61. #include <drm/drm_vblank.h>
  62. #include "drm_internal.h"
  63. static int drm_legacy_irq_install(struct drm_device *dev, int irq)
  64. {
  65. int ret;
  66. unsigned long sh_flags = 0;
  67. if (irq == 0)
  68. return -EINVAL;
  69. if (dev->irq_enabled)
  70. return -EBUSY;
  71. dev->irq_enabled = true;
  72. DRM_DEBUG("irq=%d\n", irq);
  73. /* Before installing handler */
  74. if (dev->driver->irq_preinstall)
  75. dev->driver->irq_preinstall(dev);
  76. /* PCI devices require shared interrupts. */
  77. if (dev_is_pci(dev->dev))
  78. sh_flags = IRQF_SHARED;
  79. ret = request_irq(irq, dev->driver->irq_handler,
  80. sh_flags, dev->driver->name, dev);
  81. if (ret < 0) {
  82. dev->irq_enabled = false;
  83. return ret;
  84. }
  85. /* After installing handler */
  86. if (dev->driver->irq_postinstall)
  87. ret = dev->driver->irq_postinstall(dev);
  88. if (ret < 0) {
  89. dev->irq_enabled = false;
  90. if (drm_core_check_feature(dev, DRIVER_LEGACY))
  91. vga_client_unregister(to_pci_dev(dev->dev));
  92. free_irq(irq, dev);
  93. } else {
  94. dev->irq = irq;
  95. }
  96. return ret;
  97. }
  98. int drm_legacy_irq_uninstall(struct drm_device *dev)
  99. {
  100. unsigned long irqflags;
  101. bool irq_enabled;
  102. int i;
  103. irq_enabled = dev->irq_enabled;
  104. dev->irq_enabled = false;
  105. /*
  106. * Wake up any waiters so they don't hang. This is just to paper over
  107. * issues for UMS drivers which aren't in full control of their
  108. * vblank/irq handling. KMS drivers must ensure that vblanks are all
  109. * disabled when uninstalling the irq handler.
  110. */
  111. if (drm_dev_has_vblank(dev)) {
  112. spin_lock_irqsave(&dev->vbl_lock, irqflags);
  113. for (i = 0; i < dev->num_crtcs; i++) {
  114. struct drm_vblank_crtc *vblank = &dev->vblank[i];
  115. if (!vblank->enabled)
  116. continue;
  117. WARN_ON(drm_core_check_feature(dev, DRIVER_MODESET));
  118. drm_vblank_disable_and_save(dev, i);
  119. wake_up(&vblank->queue);
  120. }
  121. spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
  122. }
  123. if (!irq_enabled)
  124. return -EINVAL;
  125. DRM_DEBUG("irq=%d\n", dev->irq);
  126. if (drm_core_check_feature(dev, DRIVER_LEGACY))
  127. vga_client_unregister(to_pci_dev(dev->dev));
  128. if (dev->driver->irq_uninstall)
  129. dev->driver->irq_uninstall(dev);
  130. free_irq(dev->irq, dev);
  131. return 0;
  132. }
  133. EXPORT_SYMBOL(drm_legacy_irq_uninstall);
  134. int drm_legacy_irq_control(struct drm_device *dev, void *data,
  135. struct drm_file *file_priv)
  136. {
  137. struct drm_control *ctl = data;
  138. int ret = 0, irq;
  139. struct pci_dev *pdev;
  140. /* if we haven't irq we fallback for compatibility reasons -
  141. * this used to be a separate function in drm_dma.h
  142. */
  143. if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
  144. return 0;
  145. if (!drm_core_check_feature(dev, DRIVER_LEGACY))
  146. return 0;
  147. /* UMS was only ever supported on pci devices. */
  148. if (WARN_ON(!dev_is_pci(dev->dev)))
  149. return -EINVAL;
  150. switch (ctl->func) {
  151. case DRM_INST_HANDLER:
  152. pdev = to_pci_dev(dev->dev);
  153. irq = pdev->irq;
  154. if (dev->if_version < DRM_IF_VERSION(1, 2) &&
  155. ctl->irq != irq)
  156. return -EINVAL;
  157. mutex_lock(&dev->struct_mutex);
  158. ret = drm_legacy_irq_install(dev, irq);
  159. mutex_unlock(&dev->struct_mutex);
  160. return ret;
  161. case DRM_UNINST_HANDLER:
  162. mutex_lock(&dev->struct_mutex);
  163. ret = drm_legacy_irq_uninstall(dev);
  164. mutex_unlock(&dev->struct_mutex);
  165. return ret;
  166. default:
  167. return -EINVAL;
  168. }
  169. }