drm_irq.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776
  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. static void vblank_disable_fn(unsigned long arg)
  65. {
  66. struct drm_device *dev = (struct drm_device *)arg;
  67. unsigned long irqflags;
  68. int i;
  69. if (!dev->vblank_disable_allowed)
  70. return;
  71. for (i = 0; i < dev->num_crtcs; i++) {
  72. spin_lock_irqsave(&dev->vbl_lock, irqflags);
  73. if (atomic_read(&dev->vblank_refcount[i]) == 0 &&
  74. dev->vblank_enabled[i]) {
  75. DRM_DEBUG("disabling vblank on crtc %d\n", i);
  76. dev->last_vblank[i] =
  77. dev->driver->get_vblank_counter(dev, i);
  78. dev->driver->disable_vblank(dev, i);
  79. dev->vblank_enabled[i] = 0;
  80. }
  81. spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
  82. }
  83. }
  84. static void drm_vblank_cleanup(struct drm_device *dev)
  85. {
  86. /* Bail if the driver didn't call drm_vblank_init() */
  87. if (dev->num_crtcs == 0)
  88. return;
  89. del_timer(&dev->vblank_disable_timer);
  90. vblank_disable_fn((unsigned long)dev);
  91. drm_free(dev->vbl_queue, sizeof(*dev->vbl_queue) * dev->num_crtcs,
  92. DRM_MEM_DRIVER);
  93. drm_free(dev->vbl_sigs, sizeof(*dev->vbl_sigs) * dev->num_crtcs,
  94. DRM_MEM_DRIVER);
  95. drm_free(dev->_vblank_count, sizeof(*dev->_vblank_count) *
  96. dev->num_crtcs, DRM_MEM_DRIVER);
  97. drm_free(dev->vblank_refcount, sizeof(*dev->vblank_refcount) *
  98. dev->num_crtcs, DRM_MEM_DRIVER);
  99. drm_free(dev->vblank_enabled, sizeof(*dev->vblank_enabled) *
  100. dev->num_crtcs, DRM_MEM_DRIVER);
  101. drm_free(dev->last_vblank, sizeof(*dev->last_vblank) * dev->num_crtcs,
  102. DRM_MEM_DRIVER);
  103. drm_free(dev->vblank_inmodeset, sizeof(*dev->vblank_inmodeset) *
  104. dev->num_crtcs, DRM_MEM_DRIVER);
  105. dev->num_crtcs = 0;
  106. }
  107. int drm_vblank_init(struct drm_device *dev, int num_crtcs)
  108. {
  109. int i, ret = -ENOMEM;
  110. setup_timer(&dev->vblank_disable_timer, vblank_disable_fn,
  111. (unsigned long)dev);
  112. spin_lock_init(&dev->vbl_lock);
  113. atomic_set(&dev->vbl_signal_pending, 0);
  114. dev->num_crtcs = num_crtcs;
  115. dev->vbl_queue = drm_alloc(sizeof(wait_queue_head_t) * num_crtcs,
  116. DRM_MEM_DRIVER);
  117. if (!dev->vbl_queue)
  118. goto err;
  119. dev->vbl_sigs = drm_alloc(sizeof(struct list_head) * num_crtcs,
  120. DRM_MEM_DRIVER);
  121. if (!dev->vbl_sigs)
  122. goto err;
  123. dev->_vblank_count = drm_alloc(sizeof(atomic_t) * num_crtcs,
  124. DRM_MEM_DRIVER);
  125. if (!dev->_vblank_count)
  126. goto err;
  127. dev->vblank_refcount = drm_alloc(sizeof(atomic_t) * num_crtcs,
  128. DRM_MEM_DRIVER);
  129. if (!dev->vblank_refcount)
  130. goto err;
  131. dev->vblank_enabled = drm_calloc(num_crtcs, sizeof(int),
  132. DRM_MEM_DRIVER);
  133. if (!dev->vblank_enabled)
  134. goto err;
  135. dev->last_vblank = drm_calloc(num_crtcs, sizeof(u32), DRM_MEM_DRIVER);
  136. if (!dev->last_vblank)
  137. goto err;
  138. dev->vblank_inmodeset = drm_calloc(num_crtcs, sizeof(int),
  139. DRM_MEM_DRIVER);
  140. if (!dev->vblank_inmodeset)
  141. goto err;
  142. /* Zero per-crtc vblank stuff */
  143. for (i = 0; i < num_crtcs; i++) {
  144. init_waitqueue_head(&dev->vbl_queue[i]);
  145. INIT_LIST_HEAD(&dev->vbl_sigs[i]);
  146. atomic_set(&dev->_vblank_count[i], 0);
  147. atomic_set(&dev->vblank_refcount[i], 0);
  148. }
  149. dev->vblank_disable_allowed = 0;
  150. return 0;
  151. err:
  152. drm_vblank_cleanup(dev);
  153. return ret;
  154. }
  155. EXPORT_SYMBOL(drm_vblank_init);
  156. /**
  157. * Install IRQ handler.
  158. *
  159. * \param dev DRM device.
  160. *
  161. * Initializes the IRQ related data. Installs the handler, calling the driver
  162. * \c drm_driver_irq_preinstall() and \c drm_driver_irq_postinstall() functions
  163. * before and after the installation.
  164. */
  165. int drm_irq_install(struct drm_device *dev)
  166. {
  167. int ret = 0;
  168. unsigned long sh_flags = 0;
  169. if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
  170. return -EINVAL;
  171. if (dev->pdev->irq == 0)
  172. return -EINVAL;
  173. mutex_lock(&dev->struct_mutex);
  174. /* Driver must have been initialized */
  175. if (!dev->dev_private) {
  176. mutex_unlock(&dev->struct_mutex);
  177. return -EINVAL;
  178. }
  179. if (dev->irq_enabled) {
  180. mutex_unlock(&dev->struct_mutex);
  181. return -EBUSY;
  182. }
  183. dev->irq_enabled = 1;
  184. mutex_unlock(&dev->struct_mutex);
  185. DRM_DEBUG("irq=%d\n", dev->pdev->irq);
  186. /* Before installing handler */
  187. dev->driver->irq_preinstall(dev);
  188. /* Install handler */
  189. if (drm_core_check_feature(dev, DRIVER_IRQ_SHARED))
  190. sh_flags = IRQF_SHARED;
  191. ret = request_irq(drm_dev_to_irq(dev), dev->driver->irq_handler,
  192. sh_flags, dev->devname, dev);
  193. if (ret < 0) {
  194. mutex_lock(&dev->struct_mutex);
  195. dev->irq_enabled = 0;
  196. mutex_unlock(&dev->struct_mutex);
  197. return ret;
  198. }
  199. /* After installing handler */
  200. ret = dev->driver->irq_postinstall(dev);
  201. if (ret < 0) {
  202. mutex_lock(&dev->struct_mutex);
  203. dev->irq_enabled = 0;
  204. mutex_unlock(&dev->struct_mutex);
  205. }
  206. return ret;
  207. }
  208. EXPORT_SYMBOL(drm_irq_install);
  209. /**
  210. * Uninstall the IRQ handler.
  211. *
  212. * \param dev DRM device.
  213. *
  214. * Calls the driver's \c drm_driver_irq_uninstall() function, and stops the irq.
  215. */
  216. int drm_irq_uninstall(struct drm_device * dev)
  217. {
  218. int irq_enabled;
  219. if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
  220. return -EINVAL;
  221. mutex_lock(&dev->struct_mutex);
  222. irq_enabled = dev->irq_enabled;
  223. dev->irq_enabled = 0;
  224. mutex_unlock(&dev->struct_mutex);
  225. if (!irq_enabled)
  226. return -EINVAL;
  227. DRM_DEBUG("irq=%d\n", dev->pdev->irq);
  228. dev->driver->irq_uninstall(dev);
  229. free_irq(dev->pdev->irq, dev);
  230. drm_vblank_cleanup(dev);
  231. dev->locked_tasklet_func = NULL;
  232. return 0;
  233. }
  234. EXPORT_SYMBOL(drm_irq_uninstall);
  235. /**
  236. * IRQ control ioctl.
  237. *
  238. * \param inode device inode.
  239. * \param file_priv DRM file private.
  240. * \param cmd command.
  241. * \param arg user argument, pointing to a drm_control structure.
  242. * \return zero on success or a negative number on failure.
  243. *
  244. * Calls irq_install() or irq_uninstall() according to \p arg.
  245. */
  246. int drm_control(struct drm_device *dev, void *data,
  247. struct drm_file *file_priv)
  248. {
  249. struct drm_control *ctl = data;
  250. /* if we haven't irq we fallback for compatibility reasons - this used to be a separate function in drm_dma.h */
  251. switch (ctl->func) {
  252. case DRM_INST_HANDLER:
  253. if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
  254. return 0;
  255. if (dev->if_version < DRM_IF_VERSION(1, 2) &&
  256. ctl->irq != dev->pdev->irq)
  257. return -EINVAL;
  258. return drm_irq_install(dev);
  259. case DRM_UNINST_HANDLER:
  260. if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
  261. return 0;
  262. return drm_irq_uninstall(dev);
  263. default:
  264. return -EINVAL;
  265. }
  266. }
  267. /**
  268. * drm_vblank_count - retrieve "cooked" vblank counter value
  269. * @dev: DRM device
  270. * @crtc: which counter to retrieve
  271. *
  272. * Fetches the "cooked" vblank count value that represents the number of
  273. * vblank events since the system was booted, including lost events due to
  274. * modesetting activity.
  275. */
  276. u32 drm_vblank_count(struct drm_device *dev, int crtc)
  277. {
  278. return atomic_read(&dev->_vblank_count[crtc]);
  279. }
  280. EXPORT_SYMBOL(drm_vblank_count);
  281. /**
  282. * drm_update_vblank_count - update the master vblank counter
  283. * @dev: DRM device
  284. * @crtc: counter to update
  285. *
  286. * Call back into the driver to update the appropriate vblank counter
  287. * (specified by @crtc). Deal with wraparound, if it occurred, and
  288. * update the last read value so we can deal with wraparound on the next
  289. * call if necessary.
  290. *
  291. * Only necessary when going from off->on, to account for frames we
  292. * didn't get an interrupt for.
  293. *
  294. * Note: caller must hold dev->vbl_lock since this reads & writes
  295. * device vblank fields.
  296. */
  297. static void drm_update_vblank_count(struct drm_device *dev, int crtc)
  298. {
  299. u32 cur_vblank, diff;
  300. /*
  301. * Interrupts were disabled prior to this call, so deal with counter
  302. * wrap if needed.
  303. * NOTE! It's possible we lost a full dev->max_vblank_count events
  304. * here if the register is small or we had vblank interrupts off for
  305. * a long time.
  306. */
  307. cur_vblank = dev->driver->get_vblank_counter(dev, crtc);
  308. diff = cur_vblank - dev->last_vblank[crtc];
  309. if (cur_vblank < dev->last_vblank[crtc]) {
  310. diff += dev->max_vblank_count;
  311. DRM_DEBUG("last_vblank[%d]=0x%x, cur_vblank=0x%x => diff=0x%x\n",
  312. crtc, dev->last_vblank[crtc], cur_vblank, diff);
  313. }
  314. DRM_DEBUG("enabling vblank interrupts on crtc %d, missed %d\n",
  315. crtc, diff);
  316. atomic_add(diff, &dev->_vblank_count[crtc]);
  317. }
  318. /**
  319. * drm_vblank_get - get a reference count on vblank events
  320. * @dev: DRM device
  321. * @crtc: which CRTC to own
  322. *
  323. * Acquire a reference count on vblank events to avoid having them disabled
  324. * while in use.
  325. *
  326. * RETURNS
  327. * Zero on success, nonzero on failure.
  328. */
  329. int drm_vblank_get(struct drm_device *dev, int crtc)
  330. {
  331. unsigned long irqflags;
  332. int ret = 0;
  333. spin_lock_irqsave(&dev->vbl_lock, irqflags);
  334. /* Going from 0->1 means we have to enable interrupts again */
  335. if (atomic_add_return(1, &dev->vblank_refcount[crtc]) == 1 &&
  336. !dev->vblank_enabled[crtc]) {
  337. ret = dev->driver->enable_vblank(dev, crtc);
  338. DRM_DEBUG("enabling vblank on crtc %d, ret: %d\n", crtc, ret);
  339. if (ret)
  340. atomic_dec(&dev->vblank_refcount[crtc]);
  341. else {
  342. dev->vblank_enabled[crtc] = 1;
  343. drm_update_vblank_count(dev, crtc);
  344. }
  345. }
  346. spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
  347. return ret;
  348. }
  349. EXPORT_SYMBOL(drm_vblank_get);
  350. /**
  351. * drm_vblank_put - give up ownership of vblank events
  352. * @dev: DRM device
  353. * @crtc: which counter to give up
  354. *
  355. * Release ownership of a given vblank counter, turning off interrupts
  356. * if possible.
  357. */
  358. void drm_vblank_put(struct drm_device *dev, int crtc)
  359. {
  360. /* Last user schedules interrupt disable */
  361. if (atomic_dec_and_test(&dev->vblank_refcount[crtc]))
  362. mod_timer(&dev->vblank_disable_timer, jiffies + 5*DRM_HZ);
  363. }
  364. EXPORT_SYMBOL(drm_vblank_put);
  365. /**
  366. * drm_modeset_ctl - handle vblank event counter changes across mode switch
  367. * @DRM_IOCTL_ARGS: standard ioctl arguments
  368. *
  369. * Applications should call the %_DRM_PRE_MODESET and %_DRM_POST_MODESET
  370. * ioctls around modesetting so that any lost vblank events are accounted for.
  371. *
  372. * Generally the counter will reset across mode sets. If interrupts are
  373. * enabled around this call, we don't have to do anything since the counter
  374. * will have already been incremented.
  375. */
  376. int drm_modeset_ctl(struct drm_device *dev, void *data,
  377. struct drm_file *file_priv)
  378. {
  379. struct drm_modeset_ctl *modeset = data;
  380. unsigned long irqflags;
  381. int crtc, ret = 0;
  382. /* If drm_vblank_init() hasn't been called yet, just no-op */
  383. if (!dev->num_crtcs)
  384. goto out;
  385. crtc = modeset->crtc;
  386. if (crtc >= dev->num_crtcs) {
  387. ret = -EINVAL;
  388. goto out;
  389. }
  390. /*
  391. * To avoid all the problems that might happen if interrupts
  392. * were enabled/disabled around or between these calls, we just
  393. * have the kernel take a reference on the CRTC (just once though
  394. * to avoid corrupting the count if multiple, mismatch calls occur),
  395. * so that interrupts remain enabled in the interim.
  396. */
  397. switch (modeset->cmd) {
  398. case _DRM_PRE_MODESET:
  399. if (!dev->vblank_inmodeset[crtc]) {
  400. dev->vblank_inmodeset[crtc] = 1;
  401. drm_vblank_get(dev, crtc);
  402. }
  403. break;
  404. case _DRM_POST_MODESET:
  405. if (dev->vblank_inmodeset[crtc]) {
  406. spin_lock_irqsave(&dev->vbl_lock, irqflags);
  407. dev->vblank_disable_allowed = 1;
  408. dev->vblank_inmodeset[crtc] = 0;
  409. spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
  410. drm_vblank_put(dev, crtc);
  411. }
  412. break;
  413. default:
  414. ret = -EINVAL;
  415. break;
  416. }
  417. out:
  418. return ret;
  419. }
  420. /**
  421. * Wait for VBLANK.
  422. *
  423. * \param inode device inode.
  424. * \param file_priv DRM file private.
  425. * \param cmd command.
  426. * \param data user argument, pointing to a drm_wait_vblank structure.
  427. * \return zero on success or a negative number on failure.
  428. *
  429. * Verifies the IRQ is installed.
  430. *
  431. * If a signal is requested checks if this task has already scheduled the same signal
  432. * for the same vblank sequence number - nothing to be done in
  433. * that case. If the number of tasks waiting for the interrupt exceeds 100 the
  434. * function fails. Otherwise adds a new entry to drm_device::vbl_sigs for this
  435. * task.
  436. *
  437. * If a signal is not requested, then calls vblank_wait().
  438. */
  439. int drm_wait_vblank(struct drm_device *dev, void *data,
  440. struct drm_file *file_priv)
  441. {
  442. union drm_wait_vblank *vblwait = data;
  443. int ret = 0;
  444. unsigned int flags, seq, crtc;
  445. if ((!dev->pdev->irq) || (!dev->irq_enabled))
  446. return -EINVAL;
  447. if (vblwait->request.type &
  448. ~(_DRM_VBLANK_TYPES_MASK | _DRM_VBLANK_FLAGS_MASK)) {
  449. DRM_ERROR("Unsupported type value 0x%x, supported mask 0x%x\n",
  450. vblwait->request.type,
  451. (_DRM_VBLANK_TYPES_MASK | _DRM_VBLANK_FLAGS_MASK));
  452. return -EINVAL;
  453. }
  454. flags = vblwait->request.type & _DRM_VBLANK_FLAGS_MASK;
  455. crtc = flags & _DRM_VBLANK_SECONDARY ? 1 : 0;
  456. if (crtc >= dev->num_crtcs)
  457. return -EINVAL;
  458. ret = drm_vblank_get(dev, crtc);
  459. if (ret) {
  460. DRM_ERROR("failed to acquire vblank counter, %d\n", ret);
  461. return ret;
  462. }
  463. seq = drm_vblank_count(dev, crtc);
  464. switch (vblwait->request.type & _DRM_VBLANK_TYPES_MASK) {
  465. case _DRM_VBLANK_RELATIVE:
  466. vblwait->request.sequence += seq;
  467. vblwait->request.type &= ~_DRM_VBLANK_RELATIVE;
  468. case _DRM_VBLANK_ABSOLUTE:
  469. break;
  470. default:
  471. ret = -EINVAL;
  472. goto done;
  473. }
  474. if ((flags & _DRM_VBLANK_NEXTONMISS) &&
  475. (seq - vblwait->request.sequence) <= (1<<23)) {
  476. vblwait->request.sequence = seq + 1;
  477. }
  478. if (flags & _DRM_VBLANK_SIGNAL) {
  479. unsigned long irqflags;
  480. struct list_head *vbl_sigs = &dev->vbl_sigs[crtc];
  481. struct drm_vbl_sig *vbl_sig;
  482. spin_lock_irqsave(&dev->vbl_lock, irqflags);
  483. /* Check if this task has already scheduled the same signal
  484. * for the same vblank sequence number; nothing to be done in
  485. * that case
  486. */
  487. list_for_each_entry(vbl_sig, vbl_sigs, head) {
  488. if (vbl_sig->sequence == vblwait->request.sequence
  489. && vbl_sig->info.si_signo ==
  490. vblwait->request.signal
  491. && vbl_sig->task == current) {
  492. spin_unlock_irqrestore(&dev->vbl_lock,
  493. irqflags);
  494. vblwait->reply.sequence = seq;
  495. goto done;
  496. }
  497. }
  498. if (atomic_read(&dev->vbl_signal_pending) >= 100) {
  499. spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
  500. ret = -EBUSY;
  501. goto done;
  502. }
  503. spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
  504. vbl_sig = drm_calloc(1, sizeof(struct drm_vbl_sig),
  505. DRM_MEM_DRIVER);
  506. if (!vbl_sig) {
  507. ret = -ENOMEM;
  508. goto done;
  509. }
  510. ret = drm_vblank_get(dev, crtc);
  511. if (ret) {
  512. drm_free(vbl_sig, sizeof(struct drm_vbl_sig),
  513. DRM_MEM_DRIVER);
  514. return ret;
  515. }
  516. atomic_inc(&dev->vbl_signal_pending);
  517. vbl_sig->sequence = vblwait->request.sequence;
  518. vbl_sig->info.si_signo = vblwait->request.signal;
  519. vbl_sig->task = current;
  520. spin_lock_irqsave(&dev->vbl_lock, irqflags);
  521. list_add_tail(&vbl_sig->head, vbl_sigs);
  522. spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
  523. vblwait->reply.sequence = seq;
  524. } else {
  525. DRM_DEBUG("waiting on vblank count %d, crtc %d\n",
  526. vblwait->request.sequence, crtc);
  527. DRM_WAIT_ON(ret, dev->vbl_queue[crtc], 3 * DRM_HZ,
  528. ((drm_vblank_count(dev, crtc)
  529. - vblwait->request.sequence) <= (1 << 23)));
  530. if (ret != -EINTR) {
  531. struct timeval now;
  532. do_gettimeofday(&now);
  533. vblwait->reply.tval_sec = now.tv_sec;
  534. vblwait->reply.tval_usec = now.tv_usec;
  535. vblwait->reply.sequence = drm_vblank_count(dev, crtc);
  536. DRM_DEBUG("returning %d to client\n",
  537. vblwait->reply.sequence);
  538. } else {
  539. DRM_DEBUG("vblank wait interrupted by signal\n");
  540. }
  541. }
  542. done:
  543. drm_vblank_put(dev, crtc);
  544. return ret;
  545. }
  546. /**
  547. * Send the VBLANK signals.
  548. *
  549. * \param dev DRM device.
  550. * \param crtc CRTC where the vblank event occurred
  551. *
  552. * Sends a signal for each task in drm_device::vbl_sigs and empties the list.
  553. *
  554. * If a signal is not requested, then calls vblank_wait().
  555. */
  556. static void drm_vbl_send_signals(struct drm_device *dev, int crtc)
  557. {
  558. struct drm_vbl_sig *vbl_sig, *tmp;
  559. struct list_head *vbl_sigs;
  560. unsigned int vbl_seq;
  561. unsigned long flags;
  562. spin_lock_irqsave(&dev->vbl_lock, flags);
  563. vbl_sigs = &dev->vbl_sigs[crtc];
  564. vbl_seq = drm_vblank_count(dev, crtc);
  565. list_for_each_entry_safe(vbl_sig, tmp, vbl_sigs, head) {
  566. if ((vbl_seq - vbl_sig->sequence) <= (1 << 23)) {
  567. vbl_sig->info.si_code = vbl_seq;
  568. send_sig_info(vbl_sig->info.si_signo,
  569. &vbl_sig->info, vbl_sig->task);
  570. list_del(&vbl_sig->head);
  571. drm_free(vbl_sig, sizeof(*vbl_sig),
  572. DRM_MEM_DRIVER);
  573. atomic_dec(&dev->vbl_signal_pending);
  574. drm_vblank_put(dev, crtc);
  575. }
  576. }
  577. spin_unlock_irqrestore(&dev->vbl_lock, flags);
  578. }
  579. /**
  580. * drm_handle_vblank - handle a vblank event
  581. * @dev: DRM device
  582. * @crtc: where this event occurred
  583. *
  584. * Drivers should call this routine in their vblank interrupt handlers to
  585. * update the vblank counter and send any signals that may be pending.
  586. */
  587. void drm_handle_vblank(struct drm_device *dev, int crtc)
  588. {
  589. atomic_inc(&dev->_vblank_count[crtc]);
  590. DRM_WAKEUP(&dev->vbl_queue[crtc]);
  591. drm_vbl_send_signals(dev, crtc);
  592. }
  593. EXPORT_SYMBOL(drm_handle_vblank);
  594. /**
  595. * Tasklet wrapper function.
  596. *
  597. * \param data DRM device in disguise.
  598. *
  599. * Attempts to grab the HW lock and calls the driver callback on success. On
  600. * failure, leave the lock marked as contended so the callback can be called
  601. * from drm_unlock().
  602. */
  603. static void drm_locked_tasklet_func(unsigned long data)
  604. {
  605. struct drm_device *dev = (struct drm_device *)data;
  606. unsigned long irqflags;
  607. void (*tasklet_func)(struct drm_device *);
  608. spin_lock_irqsave(&dev->tasklet_lock, irqflags);
  609. tasklet_func = dev->locked_tasklet_func;
  610. spin_unlock_irqrestore(&dev->tasklet_lock, irqflags);
  611. if (!tasklet_func ||
  612. !drm_lock_take(&dev->lock,
  613. DRM_KERNEL_CONTEXT)) {
  614. return;
  615. }
  616. dev->lock.lock_time = jiffies;
  617. atomic_inc(&dev->counts[_DRM_STAT_LOCKS]);
  618. spin_lock_irqsave(&dev->tasklet_lock, irqflags);
  619. tasklet_func = dev->locked_tasklet_func;
  620. dev->locked_tasklet_func = NULL;
  621. spin_unlock_irqrestore(&dev->tasklet_lock, irqflags);
  622. if (tasklet_func != NULL)
  623. tasklet_func(dev);
  624. drm_lock_free(&dev->lock,
  625. DRM_KERNEL_CONTEXT);
  626. }
  627. /**
  628. * Schedule a tasklet to call back a driver hook with the HW lock held.
  629. *
  630. * \param dev DRM device.
  631. * \param func Driver callback.
  632. *
  633. * This is intended for triggering actions that require the HW lock from an
  634. * interrupt handler. The lock will be grabbed ASAP after the interrupt handler
  635. * completes. Note that the callback may be called from interrupt or process
  636. * context, it must not make any assumptions about this. Also, the HW lock will
  637. * be held with the kernel context or any client context.
  638. */
  639. void drm_locked_tasklet(struct drm_device *dev, void (*func)(struct drm_device *))
  640. {
  641. unsigned long irqflags;
  642. static DECLARE_TASKLET(drm_tasklet, drm_locked_tasklet_func, 0);
  643. if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ) ||
  644. test_bit(TASKLET_STATE_SCHED, &drm_tasklet.state))
  645. return;
  646. spin_lock_irqsave(&dev->tasklet_lock, irqflags);
  647. if (dev->locked_tasklet_func) {
  648. spin_unlock_irqrestore(&dev->tasklet_lock, irqflags);
  649. return;
  650. }
  651. dev->locked_tasklet_func = func;
  652. spin_unlock_irqrestore(&dev->tasklet_lock, irqflags);
  653. drm_tasklet.data = (unsigned long)dev;
  654. tasklet_hi_schedule(&drm_tasklet);
  655. }
  656. EXPORT_SYMBOL(drm_locked_tasklet);