drm_irq.c 20 KB

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