drm_irq.c 21 KB

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