drm_irq.c 20 KB

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