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