drm_irq.c 20 KB

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