drm_irq.c 20 KB

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