drm_irq.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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 file_priv DRM file private.
  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 drm_device *dev, void *data,
  50. struct drm_file *file_priv)
  51. {
  52. struct drm_irq_busid *p = data;
  53. if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
  54. return -EINVAL;
  55. if ((p->busnum >> 8) != drm_get_pci_domain(dev) ||
  56. (p->busnum & 0xff) != dev->pdev->bus->number ||
  57. p->devnum != PCI_SLOT(dev->pdev->devfn) || p->funcnum != PCI_FUNC(dev->pdev->devfn))
  58. return -EINVAL;
  59. p->irq = dev->pdev->irq;
  60. DRM_DEBUG("%d:%d:%d => IRQ %d\n", p->busnum, p->devnum, p->funcnum,
  61. p->irq);
  62. return 0;
  63. }
  64. /**
  65. * Install IRQ handler.
  66. *
  67. * \param dev DRM device.
  68. * \param irq IRQ number.
  69. *
  70. * Initializes the IRQ related data, and setups drm_device::vbl_queue. Installs the handler, calling the driver
  71. * \c drm_driver_irq_preinstall() and \c drm_driver_irq_postinstall() functions
  72. * before and after the installation.
  73. */
  74. static int drm_irq_install(struct drm_device * dev)
  75. {
  76. int ret;
  77. unsigned long sh_flags = 0;
  78. if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
  79. return -EINVAL;
  80. if (dev->pdev->irq == 0)
  81. return -EINVAL;
  82. mutex_lock(&dev->struct_mutex);
  83. /* Driver must have been initialized */
  84. if (!dev->dev_private) {
  85. mutex_unlock(&dev->struct_mutex);
  86. return -EINVAL;
  87. }
  88. if (dev->irq_enabled) {
  89. mutex_unlock(&dev->struct_mutex);
  90. return -EBUSY;
  91. }
  92. dev->irq_enabled = 1;
  93. mutex_unlock(&dev->struct_mutex);
  94. DRM_DEBUG("irq=%d\n", dev->pdev->irq);
  95. if (drm_core_check_feature(dev, DRIVER_IRQ_VBL)) {
  96. init_waitqueue_head(&dev->vbl_queue);
  97. spin_lock_init(&dev->vbl_lock);
  98. INIT_LIST_HEAD(&dev->vbl_sigs);
  99. INIT_LIST_HEAD(&dev->vbl_sigs2);
  100. dev->vbl_pending = 0;
  101. }
  102. /* Before installing handler */
  103. dev->driver->irq_preinstall(dev);
  104. /* Install handler */
  105. if (drm_core_check_feature(dev, DRIVER_IRQ_SHARED))
  106. sh_flags = IRQF_SHARED;
  107. ret = request_irq(dev->pdev->irq, dev->driver->irq_handler,
  108. sh_flags, dev->devname, dev);
  109. /* Expose the device irq number to drivers that want to export it for
  110. * whatever reason.
  111. */
  112. dev->irq = dev->pdev->irq;
  113. if (ret < 0) {
  114. mutex_lock(&dev->struct_mutex);
  115. dev->irq_enabled = 0;
  116. mutex_unlock(&dev->struct_mutex);
  117. return ret;
  118. }
  119. /* After installing handler */
  120. dev->driver->irq_postinstall(dev);
  121. return 0;
  122. }
  123. /**
  124. * Uninstall the IRQ handler.
  125. *
  126. * \param dev DRM device.
  127. *
  128. * Calls the driver's \c drm_driver_irq_uninstall() function, and stops the irq.
  129. */
  130. int drm_irq_uninstall(struct drm_device * dev)
  131. {
  132. int irq_enabled;
  133. if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
  134. return -EINVAL;
  135. mutex_lock(&dev->struct_mutex);
  136. irq_enabled = dev->irq_enabled;
  137. dev->irq_enabled = 0;
  138. mutex_unlock(&dev->struct_mutex);
  139. if (!irq_enabled)
  140. return -EINVAL;
  141. DRM_DEBUG("irq=%d\n", dev->pdev->irq);
  142. dev->driver->irq_uninstall(dev);
  143. free_irq(dev->pdev->irq, dev);
  144. dev->locked_tasklet_func = NULL;
  145. return 0;
  146. }
  147. EXPORT_SYMBOL(drm_irq_uninstall);
  148. /**
  149. * IRQ control ioctl.
  150. *
  151. * \param inode device inode.
  152. * \param file_priv DRM file private.
  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 drm_device *dev, void *data,
  160. struct drm_file *file_priv)
  161. {
  162. struct drm_control *ctl = data;
  163. /* if we haven't irq we fallback for compatibility reasons - this used to be a separate function in drm_dma.h */
  164. switch (ctl->func) {
  165. case DRM_INST_HANDLER:
  166. if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
  167. return 0;
  168. if (dev->if_version < DRM_IF_VERSION(1, 2) &&
  169. ctl->irq != dev->pdev->irq)
  170. return -EINVAL;
  171. return drm_irq_install(dev);
  172. case DRM_UNINST_HANDLER:
  173. if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
  174. return 0;
  175. return drm_irq_uninstall(dev);
  176. default:
  177. return -EINVAL;
  178. }
  179. }
  180. /**
  181. * Wait for VBLANK.
  182. *
  183. * \param inode device inode.
  184. * \param file_priv DRM file private.
  185. * \param cmd command.
  186. * \param data user argument, pointing to a drm_wait_vblank structure.
  187. * \return zero on success or a negative number on failure.
  188. *
  189. * Verifies the IRQ is installed.
  190. *
  191. * If a signal is requested checks if this task has already scheduled the same signal
  192. * for the same vblank sequence number - nothing to be done in
  193. * that case. If the number of tasks waiting for the interrupt exceeds 100 the
  194. * function fails. Otherwise adds a new entry to drm_device::vbl_sigs for this
  195. * task.
  196. *
  197. * If a signal is not requested, then calls vblank_wait().
  198. */
  199. int drm_wait_vblank(struct drm_device *dev, void *data, struct drm_file *file_priv)
  200. {
  201. union drm_wait_vblank *vblwait = data;
  202. struct timeval now;
  203. int ret = 0;
  204. unsigned int flags, seq;
  205. if ((!dev->pdev->irq) || (!dev->irq_enabled))
  206. return -EINVAL;
  207. if (vblwait->request.type &
  208. ~(_DRM_VBLANK_TYPES_MASK | _DRM_VBLANK_FLAGS_MASK)) {
  209. DRM_ERROR("Unsupported type value 0x%x, supported mask 0x%x\n",
  210. vblwait->request.type,
  211. (_DRM_VBLANK_TYPES_MASK | _DRM_VBLANK_FLAGS_MASK));
  212. return -EINVAL;
  213. }
  214. flags = vblwait->request.type & _DRM_VBLANK_FLAGS_MASK;
  215. if (!drm_core_check_feature(dev, (flags & _DRM_VBLANK_SECONDARY) ?
  216. DRIVER_IRQ_VBL2 : DRIVER_IRQ_VBL))
  217. return -EINVAL;
  218. seq = atomic_read((flags & _DRM_VBLANK_SECONDARY) ? &dev->vbl_received2
  219. : &dev->vbl_received);
  220. switch (vblwait->request.type & _DRM_VBLANK_TYPES_MASK) {
  221. case _DRM_VBLANK_RELATIVE:
  222. vblwait->request.sequence += seq;
  223. vblwait->request.type &= ~_DRM_VBLANK_RELATIVE;
  224. case _DRM_VBLANK_ABSOLUTE:
  225. break;
  226. default:
  227. return -EINVAL;
  228. }
  229. if ((flags & _DRM_VBLANK_NEXTONMISS) &&
  230. (seq - vblwait->request.sequence) <= (1<<23)) {
  231. vblwait->request.sequence = seq + 1;
  232. }
  233. if (flags & _DRM_VBLANK_SIGNAL) {
  234. unsigned long irqflags;
  235. struct list_head *vbl_sigs = (flags & _DRM_VBLANK_SECONDARY)
  236. ? &dev->vbl_sigs2 : &dev->vbl_sigs;
  237. struct drm_vbl_sig *vbl_sig;
  238. spin_lock_irqsave(&dev->vbl_lock, irqflags);
  239. /* Check if this task has already scheduled the same signal
  240. * for the same vblank sequence number; nothing to be done in
  241. * that case
  242. */
  243. list_for_each_entry(vbl_sig, vbl_sigs, head) {
  244. if (vbl_sig->sequence == vblwait->request.sequence
  245. && vbl_sig->info.si_signo ==
  246. vblwait->request.signal
  247. && vbl_sig->task == current) {
  248. spin_unlock_irqrestore(&dev->vbl_lock,
  249. irqflags);
  250. vblwait->reply.sequence = seq;
  251. goto done;
  252. }
  253. }
  254. if (dev->vbl_pending >= 100) {
  255. spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
  256. return -EBUSY;
  257. }
  258. dev->vbl_pending++;
  259. spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
  260. if (!
  261. (vbl_sig =
  262. drm_alloc(sizeof(struct drm_vbl_sig), DRM_MEM_DRIVER))) {
  263. return -ENOMEM;
  264. }
  265. memset((void *)vbl_sig, 0, sizeof(*vbl_sig));
  266. vbl_sig->sequence = vblwait->request.sequence;
  267. vbl_sig->info.si_signo = vblwait->request.signal;
  268. vbl_sig->task = current;
  269. spin_lock_irqsave(&dev->vbl_lock, irqflags);
  270. list_add_tail(&vbl_sig->head, vbl_sigs);
  271. spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
  272. vblwait->reply.sequence = seq;
  273. } else {
  274. if (flags & _DRM_VBLANK_SECONDARY) {
  275. if (dev->driver->vblank_wait2)
  276. ret = dev->driver->vblank_wait2(dev, &vblwait->request.sequence);
  277. } else if (dev->driver->vblank_wait)
  278. ret =
  279. dev->driver->vblank_wait(dev,
  280. &vblwait->request.sequence);
  281. do_gettimeofday(&now);
  282. vblwait->reply.tval_sec = now.tv_sec;
  283. vblwait->reply.tval_usec = now.tv_usec;
  284. }
  285. done:
  286. return ret;
  287. }
  288. /**
  289. * Send the VBLANK signals.
  290. *
  291. * \param dev DRM device.
  292. *
  293. * Sends a signal for each task in drm_device::vbl_sigs and empties the list.
  294. *
  295. * If a signal is not requested, then calls vblank_wait().
  296. */
  297. void drm_vbl_send_signals(struct drm_device * dev)
  298. {
  299. unsigned long flags;
  300. int i;
  301. spin_lock_irqsave(&dev->vbl_lock, flags);
  302. for (i = 0; i < 2; i++) {
  303. struct drm_vbl_sig *vbl_sig, *tmp;
  304. struct list_head *vbl_sigs = i ? &dev->vbl_sigs2 : &dev->vbl_sigs;
  305. unsigned int vbl_seq = atomic_read(i ? &dev->vbl_received2 :
  306. &dev->vbl_received);
  307. list_for_each_entry_safe(vbl_sig, tmp, vbl_sigs, head) {
  308. if ((vbl_seq - vbl_sig->sequence) <= (1 << 23)) {
  309. vbl_sig->info.si_code = vbl_seq;
  310. send_sig_info(vbl_sig->info.si_signo,
  311. &vbl_sig->info, vbl_sig->task);
  312. list_del(&vbl_sig->head);
  313. drm_free(vbl_sig, sizeof(*vbl_sig),
  314. DRM_MEM_DRIVER);
  315. dev->vbl_pending--;
  316. }
  317. }
  318. }
  319. spin_unlock_irqrestore(&dev->vbl_lock, flags);
  320. }
  321. EXPORT_SYMBOL(drm_vbl_send_signals);
  322. /**
  323. * Tasklet wrapper function.
  324. *
  325. * \param data DRM device in disguise.
  326. *
  327. * Attempts to grab the HW lock and calls the driver callback on success. On
  328. * failure, leave the lock marked as contended so the callback can be called
  329. * from drm_unlock().
  330. */
  331. static void drm_locked_tasklet_func(unsigned long data)
  332. {
  333. struct drm_device *dev = (struct drm_device *)data;
  334. unsigned long irqflags;
  335. void (*tasklet_func)(struct drm_device *);
  336. spin_lock_irqsave(&dev->tasklet_lock, irqflags);
  337. tasklet_func = dev->locked_tasklet_func;
  338. spin_unlock_irqrestore(&dev->tasklet_lock, irqflags);
  339. if (!tasklet_func ||
  340. !drm_lock_take(&dev->lock,
  341. DRM_KERNEL_CONTEXT)) {
  342. return;
  343. }
  344. dev->lock.lock_time = jiffies;
  345. atomic_inc(&dev->counts[_DRM_STAT_LOCKS]);
  346. spin_lock_irqsave(&dev->tasklet_lock, irqflags);
  347. tasklet_func = dev->locked_tasklet_func;
  348. dev->locked_tasklet_func = NULL;
  349. spin_unlock_irqrestore(&dev->tasklet_lock, irqflags);
  350. if (tasklet_func != NULL)
  351. tasklet_func(dev);
  352. drm_lock_free(&dev->lock,
  353. DRM_KERNEL_CONTEXT);
  354. }
  355. /**
  356. * Schedule a tasklet to call back a driver hook with the HW lock held.
  357. *
  358. * \param dev DRM device.
  359. * \param func Driver callback.
  360. *
  361. * This is intended for triggering actions that require the HW lock from an
  362. * interrupt handler. The lock will be grabbed ASAP after the interrupt handler
  363. * completes. Note that the callback may be called from interrupt or process
  364. * context, it must not make any assumptions about this. Also, the HW lock will
  365. * be held with the kernel context or any client context.
  366. */
  367. void drm_locked_tasklet(struct drm_device *dev, void (*func)(struct drm_device *))
  368. {
  369. unsigned long irqflags;
  370. static DECLARE_TASKLET(drm_tasklet, drm_locked_tasklet_func, 0);
  371. if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ) ||
  372. test_bit(TASKLET_STATE_SCHED, &drm_tasklet.state))
  373. return;
  374. spin_lock_irqsave(&dev->tasklet_lock, irqflags);
  375. if (dev->locked_tasklet_func) {
  376. spin_unlock_irqrestore(&dev->tasklet_lock, irqflags);
  377. return;
  378. }
  379. dev->locked_tasklet_func = func;
  380. spin_unlock_irqrestore(&dev->tasklet_lock, irqflags);
  381. drm_tasklet.data = (unsigned long)dev;
  382. tasklet_hi_schedule(&drm_tasklet);
  383. }
  384. EXPORT_SYMBOL(drm_locked_tasklet);