drm_irq.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. /**
  2. * \file drm_irq.c
  3. * IRQ support
  4. *
  5. * \author Rickard E. (Rik) Faith <faith@valinux.com>
  6. * \author Gareth Hughes <gareth@valinux.com>
  7. */
  8. /*
  9. * Created: Fri Mar 19 14:30:16 1999 by faith@valinux.com
  10. *
  11. * Copyright 1999, 2000 Precision Insight, Inc., Cedar Park, Texas.
  12. * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
  13. * All Rights Reserved.
  14. *
  15. * Permission is hereby granted, free of charge, to any person obtaining a
  16. * copy of this software and associated documentation files (the "Software"),
  17. * to deal in the Software without restriction, including without limitation
  18. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  19. * and/or sell copies of the Software, and to permit persons to whom the
  20. * Software is furnished to do so, subject to the following conditions:
  21. *
  22. * The above copyright notice and this permission notice (including the next
  23. * paragraph) shall be included in all copies or substantial portions of the
  24. * Software.
  25. *
  26. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  27. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  28. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  29. * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  30. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  31. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  32. * OTHER DEALINGS IN THE SOFTWARE.
  33. */
  34. #include "drmP.h"
  35. #include <linux/interrupt.h> /* For task queue support */
  36. /**
  37. * Get interrupt from bus id.
  38. *
  39. * \param inode device inode.
  40. * \param filp file pointer.
  41. * \param cmd command.
  42. * \param arg user argument, pointing to a drm_irq_busid structure.
  43. * \return zero on success or a negative number on failure.
  44. *
  45. * Finds the PCI device with the specified bus id and gets its IRQ number.
  46. * This IOCTL is deprecated, and will now return EINVAL for any busid not equal
  47. * to that of the device that this DRM instance attached to.
  48. */
  49. int drm_irq_by_busid(struct inode *inode, struct file *filp,
  50. unsigned int cmd, unsigned long arg)
  51. {
  52. drm_file_t *priv = filp->private_data;
  53. drm_device_t *dev = priv->head->dev;
  54. drm_irq_busid_t __user *argp = (void __user *)arg;
  55. drm_irq_busid_t p;
  56. if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
  57. return -EINVAL;
  58. if (copy_from_user(&p, argp, sizeof(p)))
  59. return -EFAULT;
  60. if ((p.busnum >> 8) != drm_get_pci_domain(dev) ||
  61. (p.busnum & 0xff) != dev->pdev->bus->number ||
  62. p.devnum != PCI_SLOT(dev->pdev->devfn) || p.funcnum != PCI_FUNC(dev->pdev->devfn))
  63. return -EINVAL;
  64. p.irq = dev->irq;
  65. DRM_DEBUG("%d:%d:%d => IRQ %d\n", p.busnum, p.devnum, p.funcnum, p.irq);
  66. if (copy_to_user(argp, &p, sizeof(p)))
  67. return -EFAULT;
  68. return 0;
  69. }
  70. /**
  71. * Install IRQ handler.
  72. *
  73. * \param dev DRM device.
  74. * \param irq IRQ number.
  75. *
  76. * Initializes the IRQ related data, and setups drm_device::vbl_queue. Installs the handler, calling the driver
  77. * \c drm_driver_irq_preinstall() and \c drm_driver_irq_postinstall() functions
  78. * before and after the installation.
  79. */
  80. static int drm_irq_install(drm_device_t * dev)
  81. {
  82. int ret;
  83. unsigned long sh_flags = 0;
  84. if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
  85. return -EINVAL;
  86. if (dev->irq == 0)
  87. return -EINVAL;
  88. mutex_lock(&dev->struct_mutex);
  89. /* Driver must have been initialized */
  90. if (!dev->dev_private) {
  91. mutex_unlock(&dev->struct_mutex);
  92. return -EINVAL;
  93. }
  94. if (dev->irq_enabled) {
  95. mutex_unlock(&dev->struct_mutex);
  96. return -EBUSY;
  97. }
  98. dev->irq_enabled = 1;
  99. mutex_unlock(&dev->struct_mutex);
  100. DRM_DEBUG("%s: irq=%d\n", __FUNCTION__, dev->irq);
  101. if (drm_core_check_feature(dev, DRIVER_IRQ_VBL)) {
  102. init_waitqueue_head(&dev->vbl_queue);
  103. spin_lock_init(&dev->vbl_lock);
  104. INIT_LIST_HEAD(&dev->vbl_sigs.head);
  105. dev->vbl_pending = 0;
  106. }
  107. /* Before installing handler */
  108. dev->driver->irq_preinstall(dev);
  109. /* Install handler */
  110. if (drm_core_check_feature(dev, DRIVER_IRQ_SHARED))
  111. sh_flags = IRQF_SHARED;
  112. ret = request_irq(dev->irq, dev->driver->irq_handler,
  113. sh_flags, dev->devname, dev);
  114. if (ret < 0) {
  115. mutex_lock(&dev->struct_mutex);
  116. dev->irq_enabled = 0;
  117. mutex_unlock(&dev->struct_mutex);
  118. return ret;
  119. }
  120. /* After installing handler */
  121. dev->driver->irq_postinstall(dev);
  122. return 0;
  123. }
  124. /**
  125. * Uninstall the IRQ handler.
  126. *
  127. * \param dev DRM device.
  128. *
  129. * Calls the driver's \c drm_driver_irq_uninstall() function, and stops the irq.
  130. */
  131. int drm_irq_uninstall(drm_device_t * dev)
  132. {
  133. int irq_enabled;
  134. if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
  135. return -EINVAL;
  136. mutex_lock(&dev->struct_mutex);
  137. irq_enabled = dev->irq_enabled;
  138. dev->irq_enabled = 0;
  139. mutex_unlock(&dev->struct_mutex);
  140. if (!irq_enabled)
  141. return -EINVAL;
  142. DRM_DEBUG("%s: irq=%d\n", __FUNCTION__, dev->irq);
  143. dev->driver->irq_uninstall(dev);
  144. free_irq(dev->irq, dev);
  145. return 0;
  146. }
  147. EXPORT_SYMBOL(drm_irq_uninstall);
  148. /**
  149. * IRQ control ioctl.
  150. *
  151. * \param inode device inode.
  152. * \param filp file pointer.
  153. * \param cmd command.
  154. * \param arg user argument, pointing to a drm_control structure.
  155. * \return zero on success or a negative number on failure.
  156. *
  157. * Calls irq_install() or irq_uninstall() according to \p arg.
  158. */
  159. int drm_control(struct inode *inode, struct file *filp,
  160. unsigned int cmd, unsigned long arg)
  161. {
  162. drm_file_t *priv = filp->private_data;
  163. drm_device_t *dev = priv->head->dev;
  164. drm_control_t ctl;
  165. /* if we haven't irq we fallback for compatibility reasons - this used to be a separate function in drm_dma.h */
  166. if (copy_from_user(&ctl, (drm_control_t __user *) arg, sizeof(ctl)))
  167. return -EFAULT;
  168. switch (ctl.func) {
  169. case DRM_INST_HANDLER:
  170. if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
  171. return 0;
  172. if (dev->if_version < DRM_IF_VERSION(1, 2) &&
  173. ctl.irq != dev->irq)
  174. return -EINVAL;
  175. return drm_irq_install(dev);
  176. case DRM_UNINST_HANDLER:
  177. if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
  178. return 0;
  179. return drm_irq_uninstall(dev);
  180. default:
  181. return -EINVAL;
  182. }
  183. }
  184. /**
  185. * Wait for VBLANK.
  186. *
  187. * \param inode device inode.
  188. * \param filp file pointer.
  189. * \param cmd command.
  190. * \param data user argument, pointing to a drm_wait_vblank structure.
  191. * \return zero on success or a negative number on failure.
  192. *
  193. * Verifies the IRQ is installed.
  194. *
  195. * If a signal is requested checks if this task has already scheduled the same signal
  196. * for the same vblank sequence number - nothing to be done in
  197. * that case. If the number of tasks waiting for the interrupt exceeds 100 the
  198. * function fails. Otherwise adds a new entry to drm_device::vbl_sigs for this
  199. * task.
  200. *
  201. * If a signal is not requested, then calls vblank_wait().
  202. */
  203. int drm_wait_vblank(DRM_IOCTL_ARGS)
  204. {
  205. drm_file_t *priv = filp->private_data;
  206. drm_device_t *dev = priv->head->dev;
  207. drm_wait_vblank_t __user *argp = (void __user *)data;
  208. drm_wait_vblank_t vblwait;
  209. struct timeval now;
  210. int ret = 0;
  211. unsigned int flags;
  212. if (!drm_core_check_feature(dev, DRIVER_IRQ_VBL))
  213. return -EINVAL;
  214. if (!dev->irq)
  215. return -EINVAL;
  216. if (copy_from_user(&vblwait, argp, sizeof(vblwait)))
  217. return -EFAULT;
  218. switch (vblwait.request.type & ~_DRM_VBLANK_FLAGS_MASK) {
  219. case _DRM_VBLANK_RELATIVE:
  220. vblwait.request.sequence += atomic_read(&dev->vbl_received);
  221. vblwait.request.type &= ~_DRM_VBLANK_RELATIVE;
  222. case _DRM_VBLANK_ABSOLUTE:
  223. break;
  224. default:
  225. return -EINVAL;
  226. }
  227. flags = vblwait.request.type & _DRM_VBLANK_FLAGS_MASK;
  228. if (flags & _DRM_VBLANK_SIGNAL) {
  229. unsigned long irqflags;
  230. drm_vbl_sig_t *vbl_sig;
  231. vblwait.reply.sequence = atomic_read(&dev->vbl_received);
  232. spin_lock_irqsave(&dev->vbl_lock, irqflags);
  233. /* Check if this task has already scheduled the same signal
  234. * for the same vblank sequence number; nothing to be done in
  235. * that case
  236. */
  237. list_for_each_entry(vbl_sig, &dev->vbl_sigs.head, head) {
  238. if (vbl_sig->sequence == vblwait.request.sequence
  239. && vbl_sig->info.si_signo == vblwait.request.signal
  240. && vbl_sig->task == current) {
  241. spin_unlock_irqrestore(&dev->vbl_lock,
  242. irqflags);
  243. goto done;
  244. }
  245. }
  246. if (dev->vbl_pending >= 100) {
  247. spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
  248. return -EBUSY;
  249. }
  250. dev->vbl_pending++;
  251. spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
  252. if (!
  253. (vbl_sig =
  254. drm_alloc(sizeof(drm_vbl_sig_t), DRM_MEM_DRIVER))) {
  255. return -ENOMEM;
  256. }
  257. memset((void *)vbl_sig, 0, sizeof(*vbl_sig));
  258. vbl_sig->sequence = vblwait.request.sequence;
  259. vbl_sig->info.si_signo = vblwait.request.signal;
  260. vbl_sig->task = current;
  261. spin_lock_irqsave(&dev->vbl_lock, irqflags);
  262. list_add_tail((struct list_head *)vbl_sig, &dev->vbl_sigs.head);
  263. spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
  264. } else {
  265. if (dev->driver->vblank_wait)
  266. ret =
  267. dev->driver->vblank_wait(dev,
  268. &vblwait.request.sequence);
  269. do_gettimeofday(&now);
  270. vblwait.reply.tval_sec = now.tv_sec;
  271. vblwait.reply.tval_usec = now.tv_usec;
  272. }
  273. done:
  274. if (copy_to_user(argp, &vblwait, sizeof(vblwait)))
  275. return -EFAULT;
  276. return ret;
  277. }
  278. /**
  279. * Send the VBLANK signals.
  280. *
  281. * \param dev DRM device.
  282. *
  283. * Sends a signal for each task in drm_device::vbl_sigs and empties the list.
  284. *
  285. * If a signal is not requested, then calls vblank_wait().
  286. */
  287. void drm_vbl_send_signals(drm_device_t * dev)
  288. {
  289. struct list_head *list, *tmp;
  290. drm_vbl_sig_t *vbl_sig;
  291. unsigned int vbl_seq = atomic_read(&dev->vbl_received);
  292. unsigned long flags;
  293. spin_lock_irqsave(&dev->vbl_lock, flags);
  294. list_for_each_safe(list, tmp, &dev->vbl_sigs.head) {
  295. vbl_sig = list_entry(list, drm_vbl_sig_t, head);
  296. if ((vbl_seq - vbl_sig->sequence) <= (1 << 23)) {
  297. vbl_sig->info.si_code = vbl_seq;
  298. send_sig_info(vbl_sig->info.si_signo, &vbl_sig->info,
  299. vbl_sig->task);
  300. list_del(list);
  301. drm_free(vbl_sig, sizeof(*vbl_sig), DRM_MEM_DRIVER);
  302. dev->vbl_pending--;
  303. }
  304. }
  305. spin_unlock_irqrestore(&dev->vbl_lock, flags);
  306. }
  307. EXPORT_SYMBOL(drm_vbl_send_signals);