drm_irq.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  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. INIT_LIST_HEAD(&dev->vbl_sigs2.head);
  106. dev->vbl_pending = 0;
  107. }
  108. /* Before installing handler */
  109. dev->driver->irq_preinstall(dev);
  110. /* Install handler */
  111. if (drm_core_check_feature(dev, DRIVER_IRQ_SHARED))
  112. sh_flags = IRQF_SHARED;
  113. ret = request_irq(dev->irq, dev->driver->irq_handler,
  114. sh_flags, dev->devname, dev);
  115. if (ret < 0) {
  116. mutex_lock(&dev->struct_mutex);
  117. dev->irq_enabled = 0;
  118. mutex_unlock(&dev->struct_mutex);
  119. return ret;
  120. }
  121. /* After installing handler */
  122. dev->driver->irq_postinstall(dev);
  123. return 0;
  124. }
  125. /**
  126. * Uninstall the IRQ handler.
  127. *
  128. * \param dev DRM device.
  129. *
  130. * Calls the driver's \c drm_driver_irq_uninstall() function, and stops the irq.
  131. */
  132. int drm_irq_uninstall(drm_device_t * dev)
  133. {
  134. int irq_enabled;
  135. if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
  136. return -EINVAL;
  137. mutex_lock(&dev->struct_mutex);
  138. irq_enabled = dev->irq_enabled;
  139. dev->irq_enabled = 0;
  140. mutex_unlock(&dev->struct_mutex);
  141. if (!irq_enabled)
  142. return -EINVAL;
  143. DRM_DEBUG("%s: irq=%d\n", __FUNCTION__, dev->irq);
  144. dev->driver->irq_uninstall(dev);
  145. free_irq(dev->irq, dev);
  146. dev->locked_tasklet_func = NULL;
  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, seq;
  214. if (!dev->irq)
  215. return -EINVAL;
  216. if (copy_from_user(&vblwait, argp, sizeof(vblwait)))
  217. return -EFAULT;
  218. if (vblwait.request.type &
  219. ~(_DRM_VBLANK_TYPES_MASK | _DRM_VBLANK_FLAGS_MASK)) {
  220. DRM_ERROR("Unsupported type value 0x%x, supported mask 0x%x\n",
  221. vblwait.request.type,
  222. (_DRM_VBLANK_TYPES_MASK | _DRM_VBLANK_FLAGS_MASK));
  223. return -EINVAL;
  224. }
  225. flags = vblwait.request.type & _DRM_VBLANK_FLAGS_MASK;
  226. if (!drm_core_check_feature(dev, (flags & _DRM_VBLANK_SECONDARY) ?
  227. DRIVER_IRQ_VBL2 : DRIVER_IRQ_VBL))
  228. return -EINVAL;
  229. seq = atomic_read((flags & _DRM_VBLANK_SECONDARY) ? &dev->vbl_received2
  230. : &dev->vbl_received);
  231. switch (vblwait.request.type & _DRM_VBLANK_TYPES_MASK) {
  232. case _DRM_VBLANK_RELATIVE:
  233. vblwait.request.sequence += seq;
  234. vblwait.request.type &= ~_DRM_VBLANK_RELATIVE;
  235. case _DRM_VBLANK_ABSOLUTE:
  236. break;
  237. default:
  238. return -EINVAL;
  239. }
  240. if ((flags & _DRM_VBLANK_NEXTONMISS) &&
  241. (seq - vblwait.request.sequence) <= (1<<23)) {
  242. vblwait.request.sequence = seq + 1;
  243. }
  244. if (flags & _DRM_VBLANK_SIGNAL) {
  245. unsigned long irqflags;
  246. drm_vbl_sig_t *vbl_sigs = (flags & _DRM_VBLANK_SECONDARY)
  247. ? &dev->vbl_sigs2 : &dev->vbl_sigs;
  248. drm_vbl_sig_t *vbl_sig;
  249. spin_lock_irqsave(&dev->vbl_lock, irqflags);
  250. /* Check if this task has already scheduled the same signal
  251. * for the same vblank sequence number; nothing to be done in
  252. * that case
  253. */
  254. list_for_each_entry(vbl_sig, &vbl_sigs->head, head) {
  255. if (vbl_sig->sequence == vblwait.request.sequence
  256. && vbl_sig->info.si_signo == vblwait.request.signal
  257. && vbl_sig->task == current) {
  258. spin_unlock_irqrestore(&dev->vbl_lock,
  259. irqflags);
  260. vblwait.reply.sequence = seq;
  261. goto done;
  262. }
  263. }
  264. if (dev->vbl_pending >= 100) {
  265. spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
  266. return -EBUSY;
  267. }
  268. dev->vbl_pending++;
  269. spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
  270. if (!
  271. (vbl_sig =
  272. drm_alloc(sizeof(drm_vbl_sig_t), DRM_MEM_DRIVER))) {
  273. return -ENOMEM;
  274. }
  275. memset((void *)vbl_sig, 0, sizeof(*vbl_sig));
  276. vbl_sig->sequence = vblwait.request.sequence;
  277. vbl_sig->info.si_signo = vblwait.request.signal;
  278. vbl_sig->task = current;
  279. spin_lock_irqsave(&dev->vbl_lock, irqflags);
  280. list_add_tail((struct list_head *)vbl_sig, &vbl_sigs->head);
  281. spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
  282. vblwait.reply.sequence = seq;
  283. } else {
  284. if (flags & _DRM_VBLANK_SECONDARY) {
  285. if (dev->driver->vblank_wait2)
  286. ret = dev->driver->vblank_wait2(dev, &vblwait.request.sequence);
  287. } else if (dev->driver->vblank_wait)
  288. ret =
  289. dev->driver->vblank_wait(dev,
  290. &vblwait.request.sequence);
  291. do_gettimeofday(&now);
  292. vblwait.reply.tval_sec = now.tv_sec;
  293. vblwait.reply.tval_usec = now.tv_usec;
  294. }
  295. done:
  296. if (copy_to_user(argp, &vblwait, sizeof(vblwait)))
  297. return -EFAULT;
  298. return ret;
  299. }
  300. /**
  301. * Send the VBLANK signals.
  302. *
  303. * \param dev DRM device.
  304. *
  305. * Sends a signal for each task in drm_device::vbl_sigs and empties the list.
  306. *
  307. * If a signal is not requested, then calls vblank_wait().
  308. */
  309. void drm_vbl_send_signals(drm_device_t * dev)
  310. {
  311. unsigned long flags;
  312. int i;
  313. spin_lock_irqsave(&dev->vbl_lock, flags);
  314. for (i = 0; i < 2; i++) {
  315. struct list_head *list, *tmp;
  316. drm_vbl_sig_t *vbl_sig;
  317. drm_vbl_sig_t *vbl_sigs = i ? &dev->vbl_sigs2 : &dev->vbl_sigs;
  318. unsigned int vbl_seq = atomic_read(i ? &dev->vbl_received2 :
  319. &dev->vbl_received);
  320. list_for_each_safe(list, tmp, &vbl_sigs->head) {
  321. vbl_sig = list_entry(list, drm_vbl_sig_t, head);
  322. if ((vbl_seq - vbl_sig->sequence) <= (1 << 23)) {
  323. vbl_sig->info.si_code = vbl_seq;
  324. send_sig_info(vbl_sig->info.si_signo,
  325. &vbl_sig->info, vbl_sig->task);
  326. list_del(list);
  327. drm_free(vbl_sig, sizeof(*vbl_sig),
  328. DRM_MEM_DRIVER);
  329. dev->vbl_pending--;
  330. }
  331. }
  332. }
  333. spin_unlock_irqrestore(&dev->vbl_lock, flags);
  334. }
  335. EXPORT_SYMBOL(drm_vbl_send_signals);
  336. /**
  337. * Tasklet wrapper function.
  338. *
  339. * \param data DRM device in disguise.
  340. *
  341. * Attempts to grab the HW lock and calls the driver callback on success. On
  342. * failure, leave the lock marked as contended so the callback can be called
  343. * from drm_unlock().
  344. */
  345. static void drm_locked_tasklet_func(unsigned long data)
  346. {
  347. drm_device_t *dev = (drm_device_t*)data;
  348. unsigned long irqflags;
  349. spin_lock_irqsave(&dev->tasklet_lock, irqflags);
  350. if (!dev->locked_tasklet_func ||
  351. !drm_lock_take(&dev->lock.hw_lock->lock,
  352. DRM_KERNEL_CONTEXT)) {
  353. spin_unlock_irqrestore(&dev->tasklet_lock, irqflags);
  354. return;
  355. }
  356. dev->lock.lock_time = jiffies;
  357. atomic_inc(&dev->counts[_DRM_STAT_LOCKS]);
  358. dev->locked_tasklet_func(dev);
  359. drm_lock_free(dev, &dev->lock.hw_lock->lock,
  360. DRM_KERNEL_CONTEXT);
  361. dev->locked_tasklet_func = NULL;
  362. spin_unlock_irqrestore(&dev->tasklet_lock, irqflags);
  363. }
  364. /**
  365. * Schedule a tasklet to call back a driver hook with the HW lock held.
  366. *
  367. * \param dev DRM device.
  368. * \param func Driver callback.
  369. *
  370. * This is intended for triggering actions that require the HW lock from an
  371. * interrupt handler. The lock will be grabbed ASAP after the interrupt handler
  372. * completes. Note that the callback may be called from interrupt or process
  373. * context, it must not make any assumptions about this. Also, the HW lock will
  374. * be held with the kernel context or any client context.
  375. */
  376. void drm_locked_tasklet(drm_device_t *dev, void (*func)(drm_device_t*))
  377. {
  378. unsigned long irqflags;
  379. static DECLARE_TASKLET(drm_tasklet, drm_locked_tasklet_func, 0);
  380. if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ) ||
  381. test_bit(TASKLET_STATE_SCHED, &drm_tasklet.state))
  382. return;
  383. spin_lock_irqsave(&dev->tasklet_lock, irqflags);
  384. if (dev->locked_tasklet_func) {
  385. spin_unlock_irqrestore(&dev->tasklet_lock, irqflags);
  386. return;
  387. }
  388. dev->locked_tasklet_func = func;
  389. spin_unlock_irqrestore(&dev->tasklet_lock, irqflags);
  390. drm_tasklet.data = (unsigned long)dev;
  391. tasklet_hi_schedule(&drm_tasklet);
  392. }
  393. EXPORT_SYMBOL(drm_locked_tasklet);