drm_irq.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774
  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. DRM_WAKEUP(&dev->vbl_queue[crtc]);
  404. dev->vblank_enabled[crtc] = 0;
  405. dev->last_vblank[crtc] = dev->driver->get_vblank_counter(dev, crtc);
  406. spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
  407. }
  408. EXPORT_SYMBOL(drm_vblank_off);
  409. /**
  410. * drm_vblank_pre_modeset - account for vblanks across mode sets
  411. * @dev: DRM device
  412. * @crtc: CRTC in question
  413. * @post: post or pre mode set?
  414. *
  415. * Account for vblank events across mode setting events, which will likely
  416. * reset the hardware frame counter.
  417. */
  418. void drm_vblank_pre_modeset(struct drm_device *dev, int crtc)
  419. {
  420. /* vblank is not initialized (IRQ not installed ?) */
  421. if (!dev->num_crtcs)
  422. return;
  423. /*
  424. * To avoid all the problems that might happen if interrupts
  425. * were enabled/disabled around or between these calls, we just
  426. * have the kernel take a reference on the CRTC (just once though
  427. * to avoid corrupting the count if multiple, mismatch calls occur),
  428. * so that interrupts remain enabled in the interim.
  429. */
  430. if (!dev->vblank_inmodeset[crtc]) {
  431. dev->vblank_inmodeset[crtc] = 0x1;
  432. if (drm_vblank_get(dev, crtc) == 0)
  433. dev->vblank_inmodeset[crtc] |= 0x2;
  434. }
  435. }
  436. EXPORT_SYMBOL(drm_vblank_pre_modeset);
  437. void drm_vblank_post_modeset(struct drm_device *dev, int crtc)
  438. {
  439. unsigned long irqflags;
  440. if (dev->vblank_inmodeset[crtc]) {
  441. spin_lock_irqsave(&dev->vbl_lock, irqflags);
  442. dev->vblank_disable_allowed = 1;
  443. spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
  444. if (dev->vblank_inmodeset[crtc] & 0x2)
  445. drm_vblank_put(dev, crtc);
  446. dev->vblank_inmodeset[crtc] = 0;
  447. }
  448. }
  449. EXPORT_SYMBOL(drm_vblank_post_modeset);
  450. /**
  451. * drm_modeset_ctl - handle vblank event counter changes across mode switch
  452. * @DRM_IOCTL_ARGS: standard ioctl arguments
  453. *
  454. * Applications should call the %_DRM_PRE_MODESET and %_DRM_POST_MODESET
  455. * ioctls around modesetting so that any lost vblank events are accounted for.
  456. *
  457. * Generally the counter will reset across mode sets. If interrupts are
  458. * enabled around this call, we don't have to do anything since the counter
  459. * will have already been incremented.
  460. */
  461. int drm_modeset_ctl(struct drm_device *dev, void *data,
  462. struct drm_file *file_priv)
  463. {
  464. struct drm_modeset_ctl *modeset = data;
  465. int crtc, ret = 0;
  466. /* If drm_vblank_init() hasn't been called yet, just no-op */
  467. if (!dev->num_crtcs)
  468. goto out;
  469. crtc = modeset->crtc;
  470. if (crtc >= dev->num_crtcs) {
  471. ret = -EINVAL;
  472. goto out;
  473. }
  474. switch (modeset->cmd) {
  475. case _DRM_PRE_MODESET:
  476. drm_vblank_pre_modeset(dev, crtc);
  477. break;
  478. case _DRM_POST_MODESET:
  479. drm_vblank_post_modeset(dev, crtc);
  480. break;
  481. default:
  482. ret = -EINVAL;
  483. break;
  484. }
  485. out:
  486. return ret;
  487. }
  488. static int drm_queue_vblank_event(struct drm_device *dev, int pipe,
  489. union drm_wait_vblank *vblwait,
  490. struct drm_file *file_priv)
  491. {
  492. struct drm_pending_vblank_event *e;
  493. struct timeval now;
  494. unsigned long flags;
  495. unsigned int seq;
  496. e = kzalloc(sizeof *e, GFP_KERNEL);
  497. if (e == NULL)
  498. return -ENOMEM;
  499. e->pipe = pipe;
  500. e->event.base.type = DRM_EVENT_VBLANK;
  501. e->event.base.length = sizeof e->event;
  502. e->event.user_data = vblwait->request.signal;
  503. e->base.event = &e->event.base;
  504. e->base.file_priv = file_priv;
  505. e->base.destroy = (void (*) (struct drm_pending_event *)) kfree;
  506. do_gettimeofday(&now);
  507. spin_lock_irqsave(&dev->event_lock, flags);
  508. if (file_priv->event_space < sizeof e->event) {
  509. spin_unlock_irqrestore(&dev->event_lock, flags);
  510. kfree(e);
  511. return -ENOMEM;
  512. }
  513. file_priv->event_space -= sizeof e->event;
  514. seq = drm_vblank_count(dev, pipe);
  515. if ((vblwait->request.type & _DRM_VBLANK_NEXTONMISS) &&
  516. (seq - vblwait->request.sequence) <= (1 << 23)) {
  517. vblwait->request.sequence = seq + 1;
  518. vblwait->reply.sequence = vblwait->request.sequence;
  519. }
  520. DRM_DEBUG("event on vblank count %d, current %d, crtc %d\n",
  521. vblwait->request.sequence, seq, pipe);
  522. e->event.sequence = vblwait->request.sequence;
  523. if ((seq - vblwait->request.sequence) <= (1 << 23)) {
  524. e->event.tv_sec = now.tv_sec;
  525. e->event.tv_usec = now.tv_usec;
  526. drm_vblank_put(dev, e->pipe);
  527. list_add_tail(&e->base.link, &e->base.file_priv->event_list);
  528. wake_up_interruptible(&e->base.file_priv->event_wait);
  529. } else {
  530. list_add_tail(&e->base.link, &dev->vblank_event_list);
  531. }
  532. spin_unlock_irqrestore(&dev->event_lock, flags);
  533. return 0;
  534. }
  535. /**
  536. * Wait for VBLANK.
  537. *
  538. * \param inode device inode.
  539. * \param file_priv DRM file private.
  540. * \param cmd command.
  541. * \param data user argument, pointing to a drm_wait_vblank structure.
  542. * \return zero on success or a negative number on failure.
  543. *
  544. * This function enables the vblank interrupt on the pipe requested, then
  545. * sleeps waiting for the requested sequence number to occur, and drops
  546. * the vblank interrupt refcount afterwards. (vblank irq disable follows that
  547. * after a timeout with no further vblank waits scheduled).
  548. */
  549. int drm_wait_vblank(struct drm_device *dev, void *data,
  550. struct drm_file *file_priv)
  551. {
  552. union drm_wait_vblank *vblwait = data;
  553. int ret = 0;
  554. unsigned int flags, seq, crtc;
  555. if ((!dev->pdev->irq) || (!dev->irq_enabled))
  556. return -EINVAL;
  557. if (vblwait->request.type & _DRM_VBLANK_SIGNAL)
  558. return -EINVAL;
  559. if (vblwait->request.type &
  560. ~(_DRM_VBLANK_TYPES_MASK | _DRM_VBLANK_FLAGS_MASK)) {
  561. DRM_ERROR("Unsupported type value 0x%x, supported mask 0x%x\n",
  562. vblwait->request.type,
  563. (_DRM_VBLANK_TYPES_MASK | _DRM_VBLANK_FLAGS_MASK));
  564. return -EINVAL;
  565. }
  566. flags = vblwait->request.type & _DRM_VBLANK_FLAGS_MASK;
  567. crtc = flags & _DRM_VBLANK_SECONDARY ? 1 : 0;
  568. if (crtc >= dev->num_crtcs)
  569. return -EINVAL;
  570. ret = drm_vblank_get(dev, crtc);
  571. if (ret) {
  572. DRM_DEBUG("failed to acquire vblank counter, %d\n", ret);
  573. return ret;
  574. }
  575. seq = drm_vblank_count(dev, crtc);
  576. switch (vblwait->request.type & _DRM_VBLANK_TYPES_MASK) {
  577. case _DRM_VBLANK_RELATIVE:
  578. vblwait->request.sequence += seq;
  579. vblwait->request.type &= ~_DRM_VBLANK_RELATIVE;
  580. case _DRM_VBLANK_ABSOLUTE:
  581. break;
  582. default:
  583. ret = -EINVAL;
  584. goto done;
  585. }
  586. if (flags & _DRM_VBLANK_EVENT)
  587. return drm_queue_vblank_event(dev, crtc, vblwait, file_priv);
  588. if ((flags & _DRM_VBLANK_NEXTONMISS) &&
  589. (seq - vblwait->request.sequence) <= (1<<23)) {
  590. vblwait->request.sequence = seq + 1;
  591. }
  592. DRM_DEBUG("waiting on vblank count %d, crtc %d\n",
  593. vblwait->request.sequence, crtc);
  594. dev->last_vblank_wait[crtc] = vblwait->request.sequence;
  595. DRM_WAIT_ON(ret, dev->vbl_queue[crtc], 3 * DRM_HZ,
  596. (((drm_vblank_count(dev, crtc) -
  597. vblwait->request.sequence) <= (1 << 23)) ||
  598. !dev->irq_enabled));
  599. if (ret != -EINTR) {
  600. struct timeval now;
  601. do_gettimeofday(&now);
  602. vblwait->reply.tval_sec = now.tv_sec;
  603. vblwait->reply.tval_usec = now.tv_usec;
  604. vblwait->reply.sequence = drm_vblank_count(dev, crtc);
  605. DRM_DEBUG("returning %d to client\n",
  606. vblwait->reply.sequence);
  607. } else {
  608. DRM_DEBUG("vblank wait interrupted by signal\n");
  609. }
  610. done:
  611. drm_vblank_put(dev, crtc);
  612. return ret;
  613. }
  614. void drm_handle_vblank_events(struct drm_device *dev, int crtc)
  615. {
  616. struct drm_pending_vblank_event *e, *t;
  617. struct timeval now;
  618. unsigned long flags;
  619. unsigned int seq;
  620. do_gettimeofday(&now);
  621. seq = drm_vblank_count(dev, crtc);
  622. spin_lock_irqsave(&dev->event_lock, flags);
  623. list_for_each_entry_safe(e, t, &dev->vblank_event_list, base.link) {
  624. if (e->pipe != crtc)
  625. continue;
  626. if ((seq - e->event.sequence) > (1<<23))
  627. continue;
  628. DRM_DEBUG("vblank event on %d, current %d\n",
  629. e->event.sequence, seq);
  630. e->event.sequence = seq;
  631. e->event.tv_sec = now.tv_sec;
  632. e->event.tv_usec = now.tv_usec;
  633. drm_vblank_put(dev, e->pipe);
  634. list_move_tail(&e->base.link, &e->base.file_priv->event_list);
  635. wake_up_interruptible(&e->base.file_priv->event_wait);
  636. }
  637. spin_unlock_irqrestore(&dev->event_lock, flags);
  638. }
  639. /**
  640. * drm_handle_vblank - handle a vblank event
  641. * @dev: DRM device
  642. * @crtc: where this event occurred
  643. *
  644. * Drivers should call this routine in their vblank interrupt handlers to
  645. * update the vblank counter and send any signals that may be pending.
  646. */
  647. void drm_handle_vblank(struct drm_device *dev, int crtc)
  648. {
  649. if (!dev->num_crtcs)
  650. return;
  651. atomic_inc(&dev->_vblank_count[crtc]);
  652. DRM_WAKEUP(&dev->vbl_queue[crtc]);
  653. drm_handle_vblank_events(dev, crtc);
  654. }
  655. EXPORT_SYMBOL(drm_handle_vblank);