drm_irq.c 12 KB

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