drm_irq.c 21 KB

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