drm_irq.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. /**
  2. * \file drm_irq.h
  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) != dev->pci_domain ||
  61. (p.busnum & 0xff) != dev->pci_bus ||
  62. p.devnum != dev->pci_slot ||
  63. p.funcnum != dev->pci_func)
  64. return -EINVAL;
  65. p.irq = dev->irq;
  66. DRM_DEBUG("%d:%d:%d => IRQ %d\n",
  67. p.busnum, p.devnum, p.funcnum, p.irq);
  68. if (copy_to_user(argp, &p, sizeof(p)))
  69. return -EFAULT;
  70. return 0;
  71. }
  72. /**
  73. * Install IRQ handler.
  74. *
  75. * \param dev DRM device.
  76. * \param irq IRQ number.
  77. *
  78. * Initializes the IRQ related data, and setups drm_device::vbl_queue. Installs the handler, calling the driver
  79. * \c drm_driver_irq_preinstall() and \c drm_driver_irq_postinstall() functions
  80. * before and after the installation.
  81. */
  82. static int drm_irq_install( drm_device_t *dev )
  83. {
  84. int ret;
  85. unsigned long sh_flags=0;
  86. if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
  87. return -EINVAL;
  88. if ( dev->irq == 0 )
  89. return -EINVAL;
  90. down( &dev->struct_sem );
  91. /* Driver must have been initialized */
  92. if ( !dev->dev_private ) {
  93. up( &dev->struct_sem );
  94. return -EINVAL;
  95. }
  96. if ( dev->irq_enabled ) {
  97. up( &dev->struct_sem );
  98. return -EBUSY;
  99. }
  100. dev->irq_enabled = 1;
  101. up( &dev->struct_sem );
  102. DRM_DEBUG( "%s: irq=%d\n", __FUNCTION__, dev->irq );
  103. if (drm_core_check_feature(dev, DRIVER_IRQ_VBL)) {
  104. init_waitqueue_head(&dev->vbl_queue);
  105. spin_lock_init( &dev->vbl_lock );
  106. INIT_LIST_HEAD( &dev->vbl_sigs.head );
  107. dev->vbl_pending = 0;
  108. }
  109. /* Before installing handler */
  110. dev->driver->irq_preinstall(dev);
  111. /* Install handler */
  112. if (drm_core_check_feature(dev, DRIVER_IRQ_SHARED))
  113. sh_flags = SA_SHIRQ;
  114. ret = request_irq( dev->irq, dev->driver->irq_handler,
  115. sh_flags, dev->devname, dev );
  116. if ( ret < 0 ) {
  117. down( &dev->struct_sem );
  118. dev->irq_enabled = 0;
  119. up( &dev->struct_sem );
  120. return ret;
  121. }
  122. /* After installing handler */
  123. dev->driver->irq_postinstall(dev);
  124. return 0;
  125. }
  126. /**
  127. * Uninstall the IRQ handler.
  128. *
  129. * \param dev DRM device.
  130. *
  131. * Calls the driver's \c drm_driver_irq_uninstall() function, and stops the irq.
  132. */
  133. int drm_irq_uninstall( drm_device_t *dev )
  134. {
  135. int irq_enabled;
  136. if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
  137. return -EINVAL;
  138. down( &dev->struct_sem );
  139. irq_enabled = dev->irq_enabled;
  140. dev->irq_enabled = 0;
  141. up( &dev->struct_sem );
  142. if ( !irq_enabled )
  143. return -EINVAL;
  144. DRM_DEBUG( "%s: irq=%d\n", __FUNCTION__, dev->irq );
  145. dev->driver->irq_uninstall(dev);
  146. free_irq( dev->irq, dev );
  147. return 0;
  148. }
  149. EXPORT_SYMBOL(drm_irq_uninstall);
  150. /**
  151. * IRQ control ioctl.
  152. *
  153. * \param inode device inode.
  154. * \param filp file pointer.
  155. * \param cmd command.
  156. * \param arg user argument, pointing to a drm_control structure.
  157. * \return zero on success or a negative number on failure.
  158. *
  159. * Calls irq_install() or irq_uninstall() according to \p arg.
  160. */
  161. int drm_control( struct inode *inode, struct file *filp,
  162. unsigned int cmd, unsigned long arg )
  163. {
  164. drm_file_t *priv = filp->private_data;
  165. drm_device_t *dev = priv->head->dev;
  166. drm_control_t ctl;
  167. /* if we haven't irq we fallback for compatibility reasons - this used to be a separate function in drm_dma.h */
  168. if ( copy_from_user( &ctl, (drm_control_t __user *)arg, sizeof(ctl) ) )
  169. return -EFAULT;
  170. switch ( ctl.func ) {
  171. case DRM_INST_HANDLER:
  172. if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
  173. return 0;
  174. if (dev->if_version < DRM_IF_VERSION(1, 2) &&
  175. ctl.irq != dev->irq)
  176. return -EINVAL;
  177. return drm_irq_install( dev );
  178. case DRM_UNINST_HANDLER:
  179. if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
  180. return 0;
  181. return drm_irq_uninstall( dev );
  182. default:
  183. return -EINVAL;
  184. }
  185. }
  186. /**
  187. * Wait for VBLANK.
  188. *
  189. * \param inode device inode.
  190. * \param filp file pointer.
  191. * \param cmd command.
  192. * \param data user argument, pointing to a drm_wait_vblank structure.
  193. * \return zero on success or a negative number on failure.
  194. *
  195. * Verifies the IRQ is installed.
  196. *
  197. * If a signal is requested checks if this task has already scheduled the same signal
  198. * for the same vblank sequence number - nothing to be done in
  199. * that case. If the number of tasks waiting for the interrupt exceeds 100 the
  200. * function fails. Otherwise adds a new entry to drm_device::vbl_sigs for this
  201. * task.
  202. *
  203. * If a signal is not requested, then calls vblank_wait().
  204. */
  205. int drm_wait_vblank( DRM_IOCTL_ARGS )
  206. {
  207. drm_file_t *priv = filp->private_data;
  208. drm_device_t *dev = priv->head->dev;
  209. drm_wait_vblank_t __user *argp = (void __user *)data;
  210. drm_wait_vblank_t vblwait;
  211. struct timeval now;
  212. int ret = 0;
  213. unsigned int flags;
  214. if (!drm_core_check_feature(dev, DRIVER_IRQ_VBL))
  215. return -EINVAL;
  216. if (!dev->irq)
  217. return -EINVAL;
  218. DRM_COPY_FROM_USER_IOCTL( vblwait, argp, sizeof(vblwait) );
  219. switch ( vblwait.request.type & ~_DRM_VBLANK_FLAGS_MASK ) {
  220. case _DRM_VBLANK_RELATIVE:
  221. vblwait.request.sequence += atomic_read( &dev->vbl_received );
  222. vblwait.request.type &= ~_DRM_VBLANK_RELATIVE;
  223. case _DRM_VBLANK_ABSOLUTE:
  224. break;
  225. default:
  226. return -EINVAL;
  227. }
  228. flags = vblwait.request.type & _DRM_VBLANK_FLAGS_MASK;
  229. if ( flags & _DRM_VBLANK_SIGNAL ) {
  230. unsigned long irqflags;
  231. drm_vbl_sig_t *vbl_sig;
  232. vblwait.reply.sequence = atomic_read( &dev->vbl_received );
  233. spin_lock_irqsave( &dev->vbl_lock, irqflags );
  234. /* Check if this task has already scheduled the same signal
  235. * for the same vblank sequence number; nothing to be done in
  236. * that case
  237. */
  238. list_for_each_entry( vbl_sig, &dev->vbl_sigs.head, head ) {
  239. if (vbl_sig->sequence == vblwait.request.sequence
  240. && vbl_sig->info.si_signo == vblwait.request.signal
  241. && vbl_sig->task == current)
  242. {
  243. spin_unlock_irqrestore( &dev->vbl_lock, irqflags );
  244. goto done;
  245. }
  246. }
  247. if ( dev->vbl_pending >= 100 ) {
  248. spin_unlock_irqrestore( &dev->vbl_lock, irqflags );
  249. return -EBUSY;
  250. }
  251. dev->vbl_pending++;
  252. spin_unlock_irqrestore( &dev->vbl_lock, irqflags );
  253. if ( !( vbl_sig = drm_alloc( sizeof( drm_vbl_sig_t ), DRM_MEM_DRIVER ) ) ) {
  254. return -ENOMEM;
  255. }
  256. memset( (void *)vbl_sig, 0, sizeof(*vbl_sig) );
  257. vbl_sig->sequence = vblwait.request.sequence;
  258. vbl_sig->info.si_signo = vblwait.request.signal;
  259. vbl_sig->task = current;
  260. spin_lock_irqsave( &dev->vbl_lock, irqflags );
  261. list_add_tail( (struct list_head *) vbl_sig, &dev->vbl_sigs.head );
  262. spin_unlock_irqrestore( &dev->vbl_lock, irqflags );
  263. } else {
  264. if (dev->driver->vblank_wait)
  265. ret = dev->driver->vblank_wait( dev, &vblwait.request.sequence );
  266. do_gettimeofday( &now );
  267. vblwait.reply.tval_sec = now.tv_sec;
  268. vblwait.reply.tval_usec = now.tv_usec;
  269. }
  270. done:
  271. DRM_COPY_TO_USER_IOCTL( argp, vblwait, sizeof(vblwait) );
  272. return ret;
  273. }
  274. /**
  275. * Send the VBLANK signals.
  276. *
  277. * \param dev DRM device.
  278. *
  279. * Sends a signal for each task in drm_device::vbl_sigs and empties the list.
  280. *
  281. * If a signal is not requested, then calls vblank_wait().
  282. */
  283. void drm_vbl_send_signals( drm_device_t *dev )
  284. {
  285. struct list_head *list, *tmp;
  286. drm_vbl_sig_t *vbl_sig;
  287. unsigned int vbl_seq = atomic_read( &dev->vbl_received );
  288. unsigned long flags;
  289. spin_lock_irqsave( &dev->vbl_lock, flags );
  290. list_for_each_safe( list, tmp, &dev->vbl_sigs.head ) {
  291. vbl_sig = list_entry( list, drm_vbl_sig_t, head );
  292. if ( ( vbl_seq - vbl_sig->sequence ) <= (1<<23) ) {
  293. vbl_sig->info.si_code = vbl_seq;
  294. send_sig_info( vbl_sig->info.si_signo, &vbl_sig->info, vbl_sig->task );
  295. list_del( list );
  296. drm_free( vbl_sig, sizeof(*vbl_sig), DRM_MEM_DRIVER );
  297. dev->vbl_pending--;
  298. }
  299. }
  300. spin_unlock_irqrestore( &dev->vbl_lock, flags );
  301. }
  302. EXPORT_SYMBOL(drm_vbl_send_signals);