drm_irq.c 20 KB

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