drm_irq.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318
  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. /* Access macro for slots in vblank timestamp ringbuffer. */
  40. #define vblanktimestamp(dev, crtc, count) ( \
  41. (dev)->_vblank_time[(crtc) * DRM_VBLANKTIME_RBSIZE + \
  42. ((count) % DRM_VBLANKTIME_RBSIZE)])
  43. /* Retry timestamp calculation up to 3 times to satisfy
  44. * drm_timestamp_precision before giving up.
  45. */
  46. #define DRM_TIMESTAMP_MAXRETRIES 3
  47. /* Threshold in nanoseconds for detection of redundant
  48. * vblank irq in drm_handle_vblank(). 1 msec should be ok.
  49. */
  50. #define DRM_REDUNDANT_VBLIRQ_THRESH_NS 1000000
  51. /**
  52. * Get interrupt from bus id.
  53. *
  54. * \param inode device inode.
  55. * \param file_priv DRM file private.
  56. * \param cmd command.
  57. * \param arg user argument, pointing to a drm_irq_busid structure.
  58. * \return zero on success or a negative number on failure.
  59. *
  60. * Finds the PCI device with the specified bus id and gets its IRQ number.
  61. * This IOCTL is deprecated, and will now return EINVAL for any busid not equal
  62. * to that of the device that this DRM instance attached to.
  63. */
  64. int drm_irq_by_busid(struct drm_device *dev, void *data,
  65. struct drm_file *file_priv)
  66. {
  67. struct drm_irq_busid *p = data;
  68. if (drm_core_check_feature(dev, DRIVER_USE_PLATFORM_DEVICE))
  69. return -EINVAL;
  70. if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
  71. return -EINVAL;
  72. if ((p->busnum >> 8) != drm_get_pci_domain(dev) ||
  73. (p->busnum & 0xff) != dev->pdev->bus->number ||
  74. p->devnum != PCI_SLOT(dev->pdev->devfn) || p->funcnum != PCI_FUNC(dev->pdev->devfn))
  75. return -EINVAL;
  76. p->irq = dev->pdev->irq;
  77. DRM_DEBUG("%d:%d:%d => IRQ %d\n", p->busnum, p->devnum, p->funcnum,
  78. p->irq);
  79. return 0;
  80. }
  81. /*
  82. * Clear vblank timestamp buffer for a crtc.
  83. */
  84. static void clear_vblank_timestamps(struct drm_device *dev, int crtc)
  85. {
  86. memset(&dev->_vblank_time[crtc * DRM_VBLANKTIME_RBSIZE], 0,
  87. DRM_VBLANKTIME_RBSIZE * sizeof(struct timeval));
  88. }
  89. /*
  90. * Disable vblank irq's on crtc, make sure that last vblank count
  91. * of hardware and corresponding consistent software vblank counter
  92. * are preserved, even if there are any spurious vblank irq's after
  93. * disable.
  94. */
  95. static void vblank_disable_and_save(struct drm_device *dev, int crtc)
  96. {
  97. unsigned long irqflags;
  98. u32 vblcount;
  99. s64 diff_ns;
  100. int vblrc;
  101. struct timeval tvblank;
  102. /* Prevent vblank irq processing while disabling vblank irqs,
  103. * so no updates of timestamps or count can happen after we've
  104. * disabled. Needed to prevent races in case of delayed irq's.
  105. * Disable preemption, so vblank_time_lock is held as short as
  106. * possible, even under a kernel with PREEMPT_RT patches.
  107. */
  108. preempt_disable();
  109. spin_lock_irqsave(&dev->vblank_time_lock, irqflags);
  110. dev->driver->disable_vblank(dev, crtc);
  111. dev->vblank_enabled[crtc] = 0;
  112. /* No further vblank irq's will be processed after
  113. * this point. Get current hardware vblank count and
  114. * vblank timestamp, repeat until they are consistent.
  115. *
  116. * FIXME: There is still a race condition here and in
  117. * drm_update_vblank_count() which can cause off-by-one
  118. * reinitialization of software vblank counter. If gpu
  119. * vblank counter doesn't increment exactly at the leading
  120. * edge of a vblank interval, then we can lose 1 count if
  121. * we happen to execute between start of vblank and the
  122. * delayed gpu counter increment.
  123. */
  124. do {
  125. dev->last_vblank[crtc] = dev->driver->get_vblank_counter(dev, crtc);
  126. vblrc = drm_get_last_vbltimestamp(dev, crtc, &tvblank, 0);
  127. } while (dev->last_vblank[crtc] != dev->driver->get_vblank_counter(dev, crtc));
  128. /* Compute time difference to stored timestamp of last vblank
  129. * as updated by last invocation of drm_handle_vblank() in vblank irq.
  130. */
  131. vblcount = atomic_read(&dev->_vblank_count[crtc]);
  132. diff_ns = timeval_to_ns(&tvblank) -
  133. timeval_to_ns(&vblanktimestamp(dev, crtc, vblcount));
  134. /* If there is at least 1 msec difference between the last stored
  135. * timestamp and tvblank, then we are currently executing our
  136. * disable inside a new vblank interval, the tvblank timestamp
  137. * corresponds to this new vblank interval and the irq handler
  138. * for this vblank didn't run yet and won't run due to our disable.
  139. * Therefore we need to do the job of drm_handle_vblank() and
  140. * increment the vblank counter by one to account for this vblank.
  141. *
  142. * Skip this step if there isn't any high precision timestamp
  143. * available. In that case we can't account for this and just
  144. * hope for the best.
  145. */
  146. if ((vblrc > 0) && (abs64(diff_ns) > 1000000)) {
  147. atomic_inc(&dev->_vblank_count[crtc]);
  148. smp_mb__after_atomic_inc();
  149. }
  150. /* Invalidate all timestamps while vblank irq's are off. */
  151. clear_vblank_timestamps(dev, crtc);
  152. spin_unlock_irqrestore(&dev->vblank_time_lock, irqflags);
  153. preempt_enable();
  154. }
  155. static void vblank_disable_fn(unsigned long arg)
  156. {
  157. struct drm_device *dev = (struct drm_device *)arg;
  158. unsigned long irqflags;
  159. int i;
  160. if (!dev->vblank_disable_allowed)
  161. return;
  162. for (i = 0; i < dev->num_crtcs; i++) {
  163. spin_lock_irqsave(&dev->vbl_lock, irqflags);
  164. if (atomic_read(&dev->vblank_refcount[i]) == 0 &&
  165. dev->vblank_enabled[i]) {
  166. DRM_DEBUG("disabling vblank on crtc %d\n", i);
  167. vblank_disable_and_save(dev, i);
  168. }
  169. spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
  170. }
  171. }
  172. void drm_vblank_cleanup(struct drm_device *dev)
  173. {
  174. /* Bail if the driver didn't call drm_vblank_init() */
  175. if (dev->num_crtcs == 0)
  176. return;
  177. del_timer(&dev->vblank_disable_timer);
  178. vblank_disable_fn((unsigned long)dev);
  179. kfree(dev->vbl_queue);
  180. kfree(dev->_vblank_count);
  181. kfree(dev->vblank_refcount);
  182. kfree(dev->vblank_enabled);
  183. kfree(dev->last_vblank);
  184. kfree(dev->last_vblank_wait);
  185. kfree(dev->vblank_inmodeset);
  186. kfree(dev->_vblank_time);
  187. dev->num_crtcs = 0;
  188. }
  189. EXPORT_SYMBOL(drm_vblank_cleanup);
  190. int drm_vblank_init(struct drm_device *dev, int num_crtcs)
  191. {
  192. int i, ret = -ENOMEM;
  193. setup_timer(&dev->vblank_disable_timer, vblank_disable_fn,
  194. (unsigned long)dev);
  195. spin_lock_init(&dev->vbl_lock);
  196. spin_lock_init(&dev->vblank_time_lock);
  197. dev->num_crtcs = num_crtcs;
  198. dev->vbl_queue = kmalloc(sizeof(wait_queue_head_t) * num_crtcs,
  199. GFP_KERNEL);
  200. if (!dev->vbl_queue)
  201. goto err;
  202. dev->_vblank_count = kmalloc(sizeof(atomic_t) * num_crtcs, GFP_KERNEL);
  203. if (!dev->_vblank_count)
  204. goto err;
  205. dev->vblank_refcount = kmalloc(sizeof(atomic_t) * num_crtcs,
  206. GFP_KERNEL);
  207. if (!dev->vblank_refcount)
  208. goto err;
  209. dev->vblank_enabled = kcalloc(num_crtcs, sizeof(int), GFP_KERNEL);
  210. if (!dev->vblank_enabled)
  211. goto err;
  212. dev->last_vblank = kcalloc(num_crtcs, sizeof(u32), GFP_KERNEL);
  213. if (!dev->last_vblank)
  214. goto err;
  215. dev->last_vblank_wait = kcalloc(num_crtcs, sizeof(u32), GFP_KERNEL);
  216. if (!dev->last_vblank_wait)
  217. goto err;
  218. dev->vblank_inmodeset = kcalloc(num_crtcs, sizeof(int), GFP_KERNEL);
  219. if (!dev->vblank_inmodeset)
  220. goto err;
  221. dev->_vblank_time = kcalloc(num_crtcs * DRM_VBLANKTIME_RBSIZE,
  222. sizeof(struct timeval), GFP_KERNEL);
  223. if (!dev->_vblank_time)
  224. goto err;
  225. DRM_INFO("Supports vblank timestamp caching Rev 1 (10.10.2010).\n");
  226. /* Driver specific high-precision vblank timestamping supported? */
  227. if (dev->driver->get_vblank_timestamp)
  228. DRM_INFO("Driver supports precise vblank timestamp query.\n");
  229. else
  230. DRM_INFO("No driver support for vblank timestamp query.\n");
  231. /* Zero per-crtc vblank stuff */
  232. for (i = 0; i < num_crtcs; i++) {
  233. init_waitqueue_head(&dev->vbl_queue[i]);
  234. atomic_set(&dev->_vblank_count[i], 0);
  235. atomic_set(&dev->vblank_refcount[i], 0);
  236. }
  237. dev->vblank_disable_allowed = 0;
  238. return 0;
  239. err:
  240. drm_vblank_cleanup(dev);
  241. return ret;
  242. }
  243. EXPORT_SYMBOL(drm_vblank_init);
  244. static void drm_irq_vgaarb_nokms(void *cookie, bool state)
  245. {
  246. struct drm_device *dev = cookie;
  247. if (dev->driver->vgaarb_irq) {
  248. dev->driver->vgaarb_irq(dev, state);
  249. return;
  250. }
  251. if (!dev->irq_enabled)
  252. return;
  253. if (state)
  254. dev->driver->irq_uninstall(dev);
  255. else {
  256. dev->driver->irq_preinstall(dev);
  257. dev->driver->irq_postinstall(dev);
  258. }
  259. }
  260. /**
  261. * Install IRQ handler.
  262. *
  263. * \param dev DRM device.
  264. *
  265. * Initializes the IRQ related data. Installs the handler, calling the driver
  266. * \c drm_driver_irq_preinstall() and \c drm_driver_irq_postinstall() functions
  267. * before and after the installation.
  268. */
  269. int drm_irq_install(struct drm_device *dev)
  270. {
  271. int ret = 0;
  272. unsigned long sh_flags = 0;
  273. char *irqname;
  274. if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
  275. return -EINVAL;
  276. if (drm_dev_to_irq(dev) == 0)
  277. return -EINVAL;
  278. mutex_lock(&dev->struct_mutex);
  279. /* Driver must have been initialized */
  280. if (!dev->dev_private) {
  281. mutex_unlock(&dev->struct_mutex);
  282. return -EINVAL;
  283. }
  284. if (dev->irq_enabled) {
  285. mutex_unlock(&dev->struct_mutex);
  286. return -EBUSY;
  287. }
  288. dev->irq_enabled = 1;
  289. mutex_unlock(&dev->struct_mutex);
  290. DRM_DEBUG("irq=%d\n", drm_dev_to_irq(dev));
  291. /* Before installing handler */
  292. dev->driver->irq_preinstall(dev);
  293. /* Install handler */
  294. if (drm_core_check_feature(dev, DRIVER_IRQ_SHARED))
  295. sh_flags = IRQF_SHARED;
  296. if (dev->devname)
  297. irqname = dev->devname;
  298. else
  299. irqname = dev->driver->name;
  300. ret = request_irq(drm_dev_to_irq(dev), dev->driver->irq_handler,
  301. sh_flags, irqname, dev);
  302. if (ret < 0) {
  303. mutex_lock(&dev->struct_mutex);
  304. dev->irq_enabled = 0;
  305. mutex_unlock(&dev->struct_mutex);
  306. return ret;
  307. }
  308. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  309. vga_client_register(dev->pdev, (void *)dev, drm_irq_vgaarb_nokms, NULL);
  310. /* After installing handler */
  311. ret = dev->driver->irq_postinstall(dev);
  312. if (ret < 0) {
  313. mutex_lock(&dev->struct_mutex);
  314. dev->irq_enabled = 0;
  315. mutex_unlock(&dev->struct_mutex);
  316. }
  317. return ret;
  318. }
  319. EXPORT_SYMBOL(drm_irq_install);
  320. /**
  321. * Uninstall the IRQ handler.
  322. *
  323. * \param dev DRM device.
  324. *
  325. * Calls the driver's \c drm_driver_irq_uninstall() function, and stops the irq.
  326. */
  327. int drm_irq_uninstall(struct drm_device *dev)
  328. {
  329. unsigned long irqflags;
  330. int irq_enabled, i;
  331. if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
  332. return -EINVAL;
  333. mutex_lock(&dev->struct_mutex);
  334. irq_enabled = dev->irq_enabled;
  335. dev->irq_enabled = 0;
  336. mutex_unlock(&dev->struct_mutex);
  337. /*
  338. * Wake up any waiters so they don't hang.
  339. */
  340. spin_lock_irqsave(&dev->vbl_lock, irqflags);
  341. for (i = 0; i < dev->num_crtcs; i++) {
  342. DRM_WAKEUP(&dev->vbl_queue[i]);
  343. dev->vblank_enabled[i] = 0;
  344. dev->last_vblank[i] = dev->driver->get_vblank_counter(dev, i);
  345. }
  346. spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
  347. if (!irq_enabled)
  348. return -EINVAL;
  349. DRM_DEBUG("irq=%d\n", drm_dev_to_irq(dev));
  350. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  351. vga_client_register(dev->pdev, NULL, NULL, NULL);
  352. dev->driver->irq_uninstall(dev);
  353. free_irq(drm_dev_to_irq(dev), dev);
  354. return 0;
  355. }
  356. EXPORT_SYMBOL(drm_irq_uninstall);
  357. /**
  358. * IRQ control ioctl.
  359. *
  360. * \param inode device inode.
  361. * \param file_priv DRM file private.
  362. * \param cmd command.
  363. * \param arg user argument, pointing to a drm_control structure.
  364. * \return zero on success or a negative number on failure.
  365. *
  366. * Calls irq_install() or irq_uninstall() according to \p arg.
  367. */
  368. int drm_control(struct drm_device *dev, void *data,
  369. struct drm_file *file_priv)
  370. {
  371. struct drm_control *ctl = data;
  372. /* if we haven't irq we fallback for compatibility reasons -
  373. * this used to be a separate function in drm_dma.h
  374. */
  375. switch (ctl->func) {
  376. case DRM_INST_HANDLER:
  377. if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
  378. return 0;
  379. if (drm_core_check_feature(dev, DRIVER_MODESET))
  380. return 0;
  381. if (dev->if_version < DRM_IF_VERSION(1, 2) &&
  382. ctl->irq != drm_dev_to_irq(dev))
  383. return -EINVAL;
  384. return drm_irq_install(dev);
  385. case DRM_UNINST_HANDLER:
  386. if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
  387. return 0;
  388. if (drm_core_check_feature(dev, DRIVER_MODESET))
  389. return 0;
  390. return drm_irq_uninstall(dev);
  391. default:
  392. return -EINVAL;
  393. }
  394. }
  395. /**
  396. * drm_calc_timestamping_constants - Calculate and
  397. * store various constants which are later needed by
  398. * vblank and swap-completion timestamping, e.g, by
  399. * drm_calc_vbltimestamp_from_scanoutpos().
  400. * They are derived from crtc's true scanout timing,
  401. * so they take things like panel scaling or other
  402. * adjustments into account.
  403. *
  404. * @crtc drm_crtc whose timestamp constants should be updated.
  405. *
  406. */
  407. void drm_calc_timestamping_constants(struct drm_crtc *crtc)
  408. {
  409. s64 linedur_ns = 0, pixeldur_ns = 0, framedur_ns = 0;
  410. u64 dotclock;
  411. /* Dot clock in Hz: */
  412. dotclock = (u64) crtc->hwmode.clock * 1000;
  413. /* Fields of interlaced scanout modes are only halve a frame duration.
  414. * Double the dotclock to get halve the frame-/line-/pixelduration.
  415. */
  416. if (crtc->hwmode.flags & DRM_MODE_FLAG_INTERLACE)
  417. dotclock *= 2;
  418. /* Valid dotclock? */
  419. if (dotclock > 0) {
  420. /* Convert scanline length in pixels and video dot clock to
  421. * line duration, frame duration and pixel duration in
  422. * nanoseconds:
  423. */
  424. pixeldur_ns = (s64) div64_u64(1000000000, dotclock);
  425. linedur_ns = (s64) div64_u64(((u64) crtc->hwmode.crtc_htotal *
  426. 1000000000), dotclock);
  427. framedur_ns = (s64) crtc->hwmode.crtc_vtotal * linedur_ns;
  428. } else
  429. DRM_ERROR("crtc %d: Can't calculate constants, dotclock = 0!\n",
  430. crtc->base.id);
  431. crtc->pixeldur_ns = pixeldur_ns;
  432. crtc->linedur_ns = linedur_ns;
  433. crtc->framedur_ns = framedur_ns;
  434. DRM_DEBUG("crtc %d: hwmode: htotal %d, vtotal %d, vdisplay %d\n",
  435. crtc->base.id, crtc->hwmode.crtc_htotal,
  436. crtc->hwmode.crtc_vtotal, crtc->hwmode.crtc_vdisplay);
  437. DRM_DEBUG("crtc %d: clock %d kHz framedur %d linedur %d, pixeldur %d\n",
  438. crtc->base.id, (int) dotclock/1000, (int) framedur_ns,
  439. (int) linedur_ns, (int) pixeldur_ns);
  440. }
  441. EXPORT_SYMBOL(drm_calc_timestamping_constants);
  442. /**
  443. * drm_calc_vbltimestamp_from_scanoutpos - helper routine for kms
  444. * drivers. Implements calculation of exact vblank timestamps from
  445. * given drm_display_mode timings and current video scanout position
  446. * of a crtc. This can be called from within get_vblank_timestamp()
  447. * implementation of a kms driver to implement the actual timestamping.
  448. *
  449. * Should return timestamps conforming to the OML_sync_control OpenML
  450. * extension specification. The timestamp corresponds to the end of
  451. * the vblank interval, aka start of scanout of topmost-leftmost display
  452. * pixel in the following video frame.
  453. *
  454. * Requires support for optional dev->driver->get_scanout_position()
  455. * in kms driver, plus a bit of setup code to provide a drm_display_mode
  456. * that corresponds to the true scanout timing.
  457. *
  458. * The current implementation only handles standard video modes. It
  459. * returns as no operation if a doublescan or interlaced video mode is
  460. * active. Higher level code is expected to handle this.
  461. *
  462. * @dev: DRM device.
  463. * @crtc: Which crtc's vblank timestamp to retrieve.
  464. * @max_error: Desired maximum allowable error in timestamps (nanosecs).
  465. * On return contains true maximum error of timestamp.
  466. * @vblank_time: Pointer to struct timeval which should receive the timestamp.
  467. * @flags: Flags to pass to driver:
  468. * 0 = Default.
  469. * DRM_CALLED_FROM_VBLIRQ = If function is called from vbl irq handler.
  470. * @refcrtc: drm_crtc* of crtc which defines scanout timing.
  471. *
  472. * Returns negative value on error, failure or if not supported in current
  473. * video mode:
  474. *
  475. * -EINVAL - Invalid crtc.
  476. * -EAGAIN - Temporary unavailable, e.g., called before initial modeset.
  477. * -ENOTSUPP - Function not supported in current display mode.
  478. * -EIO - Failed, e.g., due to failed scanout position query.
  479. *
  480. * Returns or'ed positive status flags on success:
  481. *
  482. * DRM_VBLANKTIME_SCANOUTPOS_METHOD - Signal this method used for timestamping.
  483. * DRM_VBLANKTIME_INVBL - Timestamp taken while scanout was in vblank interval.
  484. *
  485. */
  486. int drm_calc_vbltimestamp_from_scanoutpos(struct drm_device *dev, int crtc,
  487. int *max_error,
  488. struct timeval *vblank_time,
  489. unsigned flags,
  490. struct drm_crtc *refcrtc)
  491. {
  492. struct timeval stime, raw_time;
  493. struct drm_display_mode *mode;
  494. int vbl_status, vtotal, vdisplay;
  495. int vpos, hpos, i;
  496. s64 framedur_ns, linedur_ns, pixeldur_ns, delta_ns, duration_ns;
  497. bool invbl;
  498. if (crtc < 0 || crtc >= dev->num_crtcs) {
  499. DRM_ERROR("Invalid crtc %d\n", crtc);
  500. return -EINVAL;
  501. }
  502. /* Scanout position query not supported? Should not happen. */
  503. if (!dev->driver->get_scanout_position) {
  504. DRM_ERROR("Called from driver w/o get_scanout_position()!?\n");
  505. return -EIO;
  506. }
  507. mode = &refcrtc->hwmode;
  508. vtotal = mode->crtc_vtotal;
  509. vdisplay = mode->crtc_vdisplay;
  510. /* Durations of frames, lines, pixels in nanoseconds. */
  511. framedur_ns = refcrtc->framedur_ns;
  512. linedur_ns = refcrtc->linedur_ns;
  513. pixeldur_ns = refcrtc->pixeldur_ns;
  514. /* If mode timing undefined, just return as no-op:
  515. * Happens during initial modesetting of a crtc.
  516. */
  517. if (vtotal <= 0 || vdisplay <= 0 || framedur_ns == 0) {
  518. DRM_DEBUG("crtc %d: Noop due to uninitialized mode.\n", crtc);
  519. return -EAGAIN;
  520. }
  521. /* Get current scanout position with system timestamp.
  522. * Repeat query up to DRM_TIMESTAMP_MAXRETRIES times
  523. * if single query takes longer than max_error nanoseconds.
  524. *
  525. * This guarantees a tight bound on maximum error if
  526. * code gets preempted or delayed for some reason.
  527. */
  528. for (i = 0; i < DRM_TIMESTAMP_MAXRETRIES; i++) {
  529. /* Disable preemption to make it very likely to
  530. * succeed in the first iteration even on PREEMPT_RT kernel.
  531. */
  532. preempt_disable();
  533. /* Get system timestamp before query. */
  534. do_gettimeofday(&stime);
  535. /* Get vertical and horizontal scanout pos. vpos, hpos. */
  536. vbl_status = dev->driver->get_scanout_position(dev, crtc, &vpos, &hpos);
  537. /* Get system timestamp after query. */
  538. do_gettimeofday(&raw_time);
  539. preempt_enable();
  540. /* Return as no-op if scanout query unsupported or failed. */
  541. if (!(vbl_status & DRM_SCANOUTPOS_VALID)) {
  542. DRM_DEBUG("crtc %d : scanoutpos query failed [%d].\n",
  543. crtc, vbl_status);
  544. return -EIO;
  545. }
  546. duration_ns = timeval_to_ns(&raw_time) - timeval_to_ns(&stime);
  547. /* Accept result with < max_error nsecs timing uncertainty. */
  548. if (duration_ns <= (s64) *max_error)
  549. break;
  550. }
  551. /* Noisy system timing? */
  552. if (i == DRM_TIMESTAMP_MAXRETRIES) {
  553. DRM_DEBUG("crtc %d: Noisy timestamp %d us > %d us [%d reps].\n",
  554. crtc, (int) duration_ns/1000, *max_error/1000, i);
  555. }
  556. /* Return upper bound of timestamp precision error. */
  557. *max_error = (int) duration_ns;
  558. /* Check if in vblank area:
  559. * vpos is >=0 in video scanout area, but negative
  560. * within vblank area, counting down the number of lines until
  561. * start of scanout.
  562. */
  563. invbl = vbl_status & DRM_SCANOUTPOS_INVBL;
  564. /* Convert scanout position into elapsed time at raw_time query
  565. * since start of scanout at first display scanline. delta_ns
  566. * can be negative if start of scanout hasn't happened yet.
  567. */
  568. delta_ns = (s64) vpos * linedur_ns + (s64) hpos * pixeldur_ns;
  569. /* Is vpos outside nominal vblank area, but less than
  570. * 1/100 of a frame height away from start of vblank?
  571. * If so, assume this isn't a massively delayed vblank
  572. * interrupt, but a vblank interrupt that fired a few
  573. * microseconds before true start of vblank. Compensate
  574. * by adding a full frame duration to the final timestamp.
  575. * Happens, e.g., on ATI R500, R600.
  576. *
  577. * We only do this if DRM_CALLED_FROM_VBLIRQ.
  578. */
  579. if ((flags & DRM_CALLED_FROM_VBLIRQ) && !invbl &&
  580. ((vdisplay - vpos) < vtotal / 100)) {
  581. delta_ns = delta_ns - framedur_ns;
  582. /* Signal this correction as "applied". */
  583. vbl_status |= 0x8;
  584. }
  585. /* Subtract time delta from raw timestamp to get final
  586. * vblank_time timestamp for end of vblank.
  587. */
  588. *vblank_time = ns_to_timeval(timeval_to_ns(&raw_time) - delta_ns);
  589. DRM_DEBUG("crtc %d : v %d p(%d,%d)@ %d.%d -> %d.%d [e %d us, %d rep]\n",
  590. crtc, (int) vbl_status, hpos, vpos, raw_time.tv_sec,
  591. raw_time.tv_usec, vblank_time->tv_sec, vblank_time->tv_usec,
  592. (int) duration_ns/1000, i);
  593. vbl_status = DRM_VBLANKTIME_SCANOUTPOS_METHOD;
  594. if (invbl)
  595. vbl_status |= DRM_VBLANKTIME_INVBL;
  596. return vbl_status;
  597. }
  598. EXPORT_SYMBOL(drm_calc_vbltimestamp_from_scanoutpos);
  599. /**
  600. * drm_get_last_vbltimestamp - retrieve raw timestamp for the most recent
  601. * vblank interval.
  602. *
  603. * @dev: DRM device
  604. * @crtc: which crtc's vblank timestamp to retrieve
  605. * @tvblank: Pointer to target struct timeval which should receive the timestamp
  606. * @flags: Flags to pass to driver:
  607. * 0 = Default.
  608. * DRM_CALLED_FROM_VBLIRQ = If function is called from vbl irq handler.
  609. *
  610. * Fetches the system timestamp corresponding to the time of the most recent
  611. * vblank interval on specified crtc. May call into kms-driver to
  612. * compute the timestamp with a high-precision GPU specific method.
  613. *
  614. * Returns zero if timestamp originates from uncorrected do_gettimeofday()
  615. * call, i.e., it isn't very precisely locked to the true vblank.
  616. *
  617. * Returns non-zero if timestamp is considered to be very precise.
  618. */
  619. u32 drm_get_last_vbltimestamp(struct drm_device *dev, int crtc,
  620. struct timeval *tvblank, unsigned flags)
  621. {
  622. int ret = 0;
  623. /* Define requested maximum error on timestamps (nanoseconds). */
  624. int max_error = (int) drm_timestamp_precision * 1000;
  625. /* Query driver if possible and precision timestamping enabled. */
  626. if (dev->driver->get_vblank_timestamp && (max_error > 0)) {
  627. ret = dev->driver->get_vblank_timestamp(dev, crtc, &max_error,
  628. tvblank, flags);
  629. if (ret > 0)
  630. return (u32) ret;
  631. }
  632. /* GPU high precision timestamp query unsupported or failed.
  633. * Return gettimeofday timestamp as best estimate.
  634. */
  635. do_gettimeofday(tvblank);
  636. return 0;
  637. }
  638. EXPORT_SYMBOL(drm_get_last_vbltimestamp);
  639. /**
  640. * drm_vblank_count - retrieve "cooked" vblank counter value
  641. * @dev: DRM device
  642. * @crtc: which counter to retrieve
  643. *
  644. * Fetches the "cooked" vblank count value that represents the number of
  645. * vblank events since the system was booted, including lost events due to
  646. * modesetting activity.
  647. */
  648. u32 drm_vblank_count(struct drm_device *dev, int crtc)
  649. {
  650. return atomic_read(&dev->_vblank_count[crtc]);
  651. }
  652. EXPORT_SYMBOL(drm_vblank_count);
  653. /**
  654. * drm_vblank_count_and_time - retrieve "cooked" vblank counter value
  655. * and the system timestamp corresponding to that vblank counter value.
  656. *
  657. * @dev: DRM device
  658. * @crtc: which counter to retrieve
  659. * @vblanktime: Pointer to struct timeval to receive the vblank timestamp.
  660. *
  661. * Fetches the "cooked" vblank count value that represents the number of
  662. * vblank events since the system was booted, including lost events due to
  663. * modesetting activity. Returns corresponding system timestamp of the time
  664. * of the vblank interval that corresponds to the current value vblank counter
  665. * value.
  666. */
  667. u32 drm_vblank_count_and_time(struct drm_device *dev, int crtc,
  668. struct timeval *vblanktime)
  669. {
  670. u32 cur_vblank;
  671. /* Read timestamp from slot of _vblank_time ringbuffer
  672. * that corresponds to current vblank count. Retry if
  673. * count has incremented during readout. This works like
  674. * a seqlock.
  675. */
  676. do {
  677. cur_vblank = atomic_read(&dev->_vblank_count[crtc]);
  678. *vblanktime = vblanktimestamp(dev, crtc, cur_vblank);
  679. smp_rmb();
  680. } while (cur_vblank != atomic_read(&dev->_vblank_count[crtc]));
  681. return cur_vblank;
  682. }
  683. EXPORT_SYMBOL(drm_vblank_count_and_time);
  684. /**
  685. * drm_update_vblank_count - update the master vblank counter
  686. * @dev: DRM device
  687. * @crtc: counter to update
  688. *
  689. * Call back into the driver to update the appropriate vblank counter
  690. * (specified by @crtc). Deal with wraparound, if it occurred, and
  691. * update the last read value so we can deal with wraparound on the next
  692. * call if necessary.
  693. *
  694. * Only necessary when going from off->on, to account for frames we
  695. * didn't get an interrupt for.
  696. *
  697. * Note: caller must hold dev->vbl_lock since this reads & writes
  698. * device vblank fields.
  699. */
  700. static void drm_update_vblank_count(struct drm_device *dev, int crtc)
  701. {
  702. u32 cur_vblank, diff, tslot, rc;
  703. struct timeval t_vblank;
  704. /*
  705. * Interrupts were disabled prior to this call, so deal with counter
  706. * wrap if needed.
  707. * NOTE! It's possible we lost a full dev->max_vblank_count events
  708. * here if the register is small or we had vblank interrupts off for
  709. * a long time.
  710. *
  711. * We repeat the hardware vblank counter & timestamp query until
  712. * we get consistent results. This to prevent races between gpu
  713. * updating its hardware counter while we are retrieving the
  714. * corresponding vblank timestamp.
  715. */
  716. do {
  717. cur_vblank = dev->driver->get_vblank_counter(dev, crtc);
  718. rc = drm_get_last_vbltimestamp(dev, crtc, &t_vblank, 0);
  719. } while (cur_vblank != dev->driver->get_vblank_counter(dev, crtc));
  720. /* Deal with counter wrap */
  721. diff = cur_vblank - dev->last_vblank[crtc];
  722. if (cur_vblank < dev->last_vblank[crtc]) {
  723. diff += dev->max_vblank_count;
  724. DRM_DEBUG("last_vblank[%d]=0x%x, cur_vblank=0x%x => diff=0x%x\n",
  725. crtc, dev->last_vblank[crtc], cur_vblank, diff);
  726. }
  727. DRM_DEBUG("enabling vblank interrupts on crtc %d, missed %d\n",
  728. crtc, diff);
  729. /* Reinitialize corresponding vblank timestamp if high-precision query
  730. * available. Skip this step if query unsupported or failed. Will
  731. * reinitialize delayed at next vblank interrupt in that case.
  732. */
  733. if (rc) {
  734. tslot = atomic_read(&dev->_vblank_count[crtc]) + diff;
  735. vblanktimestamp(dev, crtc, tslot) = t_vblank;
  736. }
  737. smp_mb__before_atomic_inc();
  738. atomic_add(diff, &dev->_vblank_count[crtc]);
  739. smp_mb__after_atomic_inc();
  740. }
  741. /**
  742. * drm_vblank_get - get a reference count on vblank events
  743. * @dev: DRM device
  744. * @crtc: which CRTC to own
  745. *
  746. * Acquire a reference count on vblank events to avoid having them disabled
  747. * while in use.
  748. *
  749. * RETURNS
  750. * Zero on success, nonzero on failure.
  751. */
  752. int drm_vblank_get(struct drm_device *dev, int crtc)
  753. {
  754. unsigned long irqflags, irqflags2;
  755. int ret = 0;
  756. spin_lock_irqsave(&dev->vbl_lock, irqflags);
  757. /* Going from 0->1 means we have to enable interrupts again */
  758. if (atomic_add_return(1, &dev->vblank_refcount[crtc]) == 1) {
  759. /* Disable preemption while holding vblank_time_lock. Do
  760. * it explicitely to guard against PREEMPT_RT kernel.
  761. */
  762. preempt_disable();
  763. spin_lock_irqsave(&dev->vblank_time_lock, irqflags2);
  764. if (!dev->vblank_enabled[crtc]) {
  765. /* Enable vblank irqs under vblank_time_lock protection.
  766. * All vblank count & timestamp updates are held off
  767. * until we are done reinitializing master counter and
  768. * timestamps. Filtercode in drm_handle_vblank() will
  769. * prevent double-accounting of same vblank interval.
  770. */
  771. ret = dev->driver->enable_vblank(dev, crtc);
  772. DRM_DEBUG("enabling vblank on crtc %d, ret: %d\n",
  773. crtc, ret);
  774. if (ret)
  775. atomic_dec(&dev->vblank_refcount[crtc]);
  776. else {
  777. dev->vblank_enabled[crtc] = 1;
  778. drm_update_vblank_count(dev, crtc);
  779. }
  780. }
  781. spin_unlock_irqrestore(&dev->vblank_time_lock, irqflags2);
  782. preempt_enable();
  783. } else {
  784. if (!dev->vblank_enabled[crtc]) {
  785. atomic_dec(&dev->vblank_refcount[crtc]);
  786. ret = -EINVAL;
  787. }
  788. }
  789. spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
  790. return ret;
  791. }
  792. EXPORT_SYMBOL(drm_vblank_get);
  793. /**
  794. * drm_vblank_put - give up ownership of vblank events
  795. * @dev: DRM device
  796. * @crtc: which counter to give up
  797. *
  798. * Release ownership of a given vblank counter, turning off interrupts
  799. * if possible. Disable interrupts after drm_vblank_offdelay milliseconds.
  800. */
  801. void drm_vblank_put(struct drm_device *dev, int crtc)
  802. {
  803. BUG_ON(atomic_read(&dev->vblank_refcount[crtc]) == 0);
  804. /* Last user schedules interrupt disable */
  805. if (atomic_dec_and_test(&dev->vblank_refcount[crtc]) &&
  806. (drm_vblank_offdelay > 0))
  807. mod_timer(&dev->vblank_disable_timer,
  808. jiffies + ((drm_vblank_offdelay * DRM_HZ)/1000));
  809. }
  810. EXPORT_SYMBOL(drm_vblank_put);
  811. void drm_vblank_off(struct drm_device *dev, int crtc)
  812. {
  813. unsigned long irqflags;
  814. spin_lock_irqsave(&dev->vbl_lock, irqflags);
  815. vblank_disable_and_save(dev, crtc);
  816. DRM_WAKEUP(&dev->vbl_queue[crtc]);
  817. spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
  818. }
  819. EXPORT_SYMBOL(drm_vblank_off);
  820. /**
  821. * drm_vblank_pre_modeset - account for vblanks across mode sets
  822. * @dev: DRM device
  823. * @crtc: CRTC in question
  824. * @post: post or pre mode set?
  825. *
  826. * Account for vblank events across mode setting events, which will likely
  827. * reset the hardware frame counter.
  828. */
  829. void drm_vblank_pre_modeset(struct drm_device *dev, int crtc)
  830. {
  831. /* vblank is not initialized (IRQ not installed ?) */
  832. if (!dev->num_crtcs)
  833. return;
  834. /*
  835. * To avoid all the problems that might happen if interrupts
  836. * were enabled/disabled around or between these calls, we just
  837. * have the kernel take a reference on the CRTC (just once though
  838. * to avoid corrupting the count if multiple, mismatch calls occur),
  839. * so that interrupts remain enabled in the interim.
  840. */
  841. if (!dev->vblank_inmodeset[crtc]) {
  842. dev->vblank_inmodeset[crtc] = 0x1;
  843. if (drm_vblank_get(dev, crtc) == 0)
  844. dev->vblank_inmodeset[crtc] |= 0x2;
  845. }
  846. }
  847. EXPORT_SYMBOL(drm_vblank_pre_modeset);
  848. void drm_vblank_post_modeset(struct drm_device *dev, int crtc)
  849. {
  850. unsigned long irqflags;
  851. if (dev->vblank_inmodeset[crtc]) {
  852. spin_lock_irqsave(&dev->vbl_lock, irqflags);
  853. dev->vblank_disable_allowed = 1;
  854. spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
  855. if (dev->vblank_inmodeset[crtc] & 0x2)
  856. drm_vblank_put(dev, crtc);
  857. dev->vblank_inmodeset[crtc] = 0;
  858. }
  859. }
  860. EXPORT_SYMBOL(drm_vblank_post_modeset);
  861. /**
  862. * drm_modeset_ctl - handle vblank event counter changes across mode switch
  863. * @DRM_IOCTL_ARGS: standard ioctl arguments
  864. *
  865. * Applications should call the %_DRM_PRE_MODESET and %_DRM_POST_MODESET
  866. * ioctls around modesetting so that any lost vblank events are accounted for.
  867. *
  868. * Generally the counter will reset across mode sets. If interrupts are
  869. * enabled around this call, we don't have to do anything since the counter
  870. * will have already been incremented.
  871. */
  872. int drm_modeset_ctl(struct drm_device *dev, void *data,
  873. struct drm_file *file_priv)
  874. {
  875. struct drm_modeset_ctl *modeset = data;
  876. int crtc, ret = 0;
  877. /* If drm_vblank_init() hasn't been called yet, just no-op */
  878. if (!dev->num_crtcs)
  879. goto out;
  880. crtc = modeset->crtc;
  881. if (crtc >= dev->num_crtcs) {
  882. ret = -EINVAL;
  883. goto out;
  884. }
  885. switch (modeset->cmd) {
  886. case _DRM_PRE_MODESET:
  887. drm_vblank_pre_modeset(dev, crtc);
  888. break;
  889. case _DRM_POST_MODESET:
  890. drm_vblank_post_modeset(dev, crtc);
  891. break;
  892. default:
  893. ret = -EINVAL;
  894. break;
  895. }
  896. out:
  897. return ret;
  898. }
  899. static int drm_queue_vblank_event(struct drm_device *dev, int pipe,
  900. union drm_wait_vblank *vblwait,
  901. struct drm_file *file_priv)
  902. {
  903. struct drm_pending_vblank_event *e;
  904. struct timeval now;
  905. unsigned long flags;
  906. unsigned int seq;
  907. int ret;
  908. e = kzalloc(sizeof *e, GFP_KERNEL);
  909. if (e == NULL) {
  910. ret = -ENOMEM;
  911. goto err_put;
  912. }
  913. e->pipe = pipe;
  914. e->base.pid = current->pid;
  915. e->event.base.type = DRM_EVENT_VBLANK;
  916. e->event.base.length = sizeof e->event;
  917. e->event.user_data = vblwait->request.signal;
  918. e->base.event = &e->event.base;
  919. e->base.file_priv = file_priv;
  920. e->base.destroy = (void (*) (struct drm_pending_event *)) kfree;
  921. spin_lock_irqsave(&dev->event_lock, flags);
  922. if (file_priv->event_space < sizeof e->event) {
  923. ret = -EBUSY;
  924. goto err_unlock;
  925. }
  926. file_priv->event_space -= sizeof e->event;
  927. seq = drm_vblank_count_and_time(dev, pipe, &now);
  928. if ((vblwait->request.type & _DRM_VBLANK_NEXTONMISS) &&
  929. (seq - vblwait->request.sequence) <= (1 << 23)) {
  930. vblwait->request.sequence = seq + 1;
  931. vblwait->reply.sequence = vblwait->request.sequence;
  932. }
  933. DRM_DEBUG("event on vblank count %d, current %d, crtc %d\n",
  934. vblwait->request.sequence, seq, pipe);
  935. trace_drm_vblank_event_queued(current->pid, pipe,
  936. vblwait->request.sequence);
  937. e->event.sequence = vblwait->request.sequence;
  938. if ((seq - vblwait->request.sequence) <= (1 << 23)) {
  939. e->event.sequence = seq;
  940. e->event.tv_sec = now.tv_sec;
  941. e->event.tv_usec = now.tv_usec;
  942. drm_vblank_put(dev, pipe);
  943. list_add_tail(&e->base.link, &e->base.file_priv->event_list);
  944. wake_up_interruptible(&e->base.file_priv->event_wait);
  945. vblwait->reply.sequence = seq;
  946. trace_drm_vblank_event_delivered(current->pid, pipe,
  947. vblwait->request.sequence);
  948. } else {
  949. list_add_tail(&e->base.link, &dev->vblank_event_list);
  950. vblwait->reply.sequence = vblwait->request.sequence;
  951. }
  952. spin_unlock_irqrestore(&dev->event_lock, flags);
  953. return 0;
  954. err_unlock:
  955. spin_unlock_irqrestore(&dev->event_lock, flags);
  956. kfree(e);
  957. err_put:
  958. drm_vblank_put(dev, pipe);
  959. return ret;
  960. }
  961. /**
  962. * Wait for VBLANK.
  963. *
  964. * \param inode device inode.
  965. * \param file_priv DRM file private.
  966. * \param cmd command.
  967. * \param data user argument, pointing to a drm_wait_vblank structure.
  968. * \return zero on success or a negative number on failure.
  969. *
  970. * This function enables the vblank interrupt on the pipe requested, then
  971. * sleeps waiting for the requested sequence number to occur, and drops
  972. * the vblank interrupt refcount afterwards. (vblank irq disable follows that
  973. * after a timeout with no further vblank waits scheduled).
  974. */
  975. int drm_wait_vblank(struct drm_device *dev, void *data,
  976. struct drm_file *file_priv)
  977. {
  978. union drm_wait_vblank *vblwait = data;
  979. int ret = 0;
  980. unsigned int flags, seq, crtc;
  981. if ((!drm_dev_to_irq(dev)) || (!dev->irq_enabled))
  982. return -EINVAL;
  983. if (vblwait->request.type & _DRM_VBLANK_SIGNAL)
  984. return -EINVAL;
  985. if (vblwait->request.type &
  986. ~(_DRM_VBLANK_TYPES_MASK | _DRM_VBLANK_FLAGS_MASK)) {
  987. DRM_ERROR("Unsupported type value 0x%x, supported mask 0x%x\n",
  988. vblwait->request.type,
  989. (_DRM_VBLANK_TYPES_MASK | _DRM_VBLANK_FLAGS_MASK));
  990. return -EINVAL;
  991. }
  992. flags = vblwait->request.type & _DRM_VBLANK_FLAGS_MASK;
  993. crtc = flags & _DRM_VBLANK_SECONDARY ? 1 : 0;
  994. if (crtc >= dev->num_crtcs)
  995. return -EINVAL;
  996. ret = drm_vblank_get(dev, crtc);
  997. if (ret) {
  998. DRM_DEBUG("failed to acquire vblank counter, %d\n", ret);
  999. return ret;
  1000. }
  1001. seq = drm_vblank_count(dev, crtc);
  1002. switch (vblwait->request.type & _DRM_VBLANK_TYPES_MASK) {
  1003. case _DRM_VBLANK_RELATIVE:
  1004. vblwait->request.sequence += seq;
  1005. vblwait->request.type &= ~_DRM_VBLANK_RELATIVE;
  1006. case _DRM_VBLANK_ABSOLUTE:
  1007. break;
  1008. default:
  1009. ret = -EINVAL;
  1010. goto done;
  1011. }
  1012. if (flags & _DRM_VBLANK_EVENT)
  1013. return drm_queue_vblank_event(dev, crtc, vblwait, file_priv);
  1014. if ((flags & _DRM_VBLANK_NEXTONMISS) &&
  1015. (seq - vblwait->request.sequence) <= (1<<23)) {
  1016. vblwait->request.sequence = seq + 1;
  1017. }
  1018. DRM_DEBUG("waiting on vblank count %d, crtc %d\n",
  1019. vblwait->request.sequence, crtc);
  1020. dev->last_vblank_wait[crtc] = vblwait->request.sequence;
  1021. DRM_WAIT_ON(ret, dev->vbl_queue[crtc], 3 * DRM_HZ,
  1022. (((drm_vblank_count(dev, crtc) -
  1023. vblwait->request.sequence) <= (1 << 23)) ||
  1024. !dev->irq_enabled));
  1025. if (ret != -EINTR) {
  1026. struct timeval now;
  1027. vblwait->reply.sequence = drm_vblank_count_and_time(dev, crtc, &now);
  1028. vblwait->reply.tval_sec = now.tv_sec;
  1029. vblwait->reply.tval_usec = now.tv_usec;
  1030. DRM_DEBUG("returning %d to client\n",
  1031. vblwait->reply.sequence);
  1032. } else {
  1033. DRM_DEBUG("vblank wait interrupted by signal\n");
  1034. }
  1035. done:
  1036. drm_vblank_put(dev, crtc);
  1037. return ret;
  1038. }
  1039. void drm_handle_vblank_events(struct drm_device *dev, int crtc)
  1040. {
  1041. struct drm_pending_vblank_event *e, *t;
  1042. struct timeval now;
  1043. unsigned long flags;
  1044. unsigned int seq;
  1045. seq = drm_vblank_count_and_time(dev, crtc, &now);
  1046. spin_lock_irqsave(&dev->event_lock, flags);
  1047. list_for_each_entry_safe(e, t, &dev->vblank_event_list, base.link) {
  1048. if (e->pipe != crtc)
  1049. continue;
  1050. if ((seq - e->event.sequence) > (1<<23))
  1051. continue;
  1052. DRM_DEBUG("vblank event on %d, current %d\n",
  1053. e->event.sequence, seq);
  1054. e->event.sequence = seq;
  1055. e->event.tv_sec = now.tv_sec;
  1056. e->event.tv_usec = now.tv_usec;
  1057. drm_vblank_put(dev, e->pipe);
  1058. list_move_tail(&e->base.link, &e->base.file_priv->event_list);
  1059. wake_up_interruptible(&e->base.file_priv->event_wait);
  1060. trace_drm_vblank_event_delivered(e->base.pid, e->pipe,
  1061. e->event.sequence);
  1062. }
  1063. spin_unlock_irqrestore(&dev->event_lock, flags);
  1064. trace_drm_vblank_event(crtc, seq);
  1065. }
  1066. /**
  1067. * drm_handle_vblank - handle a vblank event
  1068. * @dev: DRM device
  1069. * @crtc: where this event occurred
  1070. *
  1071. * Drivers should call this routine in their vblank interrupt handlers to
  1072. * update the vblank counter and send any signals that may be pending.
  1073. */
  1074. bool drm_handle_vblank(struct drm_device *dev, int crtc)
  1075. {
  1076. u32 vblcount;
  1077. s64 diff_ns;
  1078. struct timeval tvblank;
  1079. unsigned long irqflags;
  1080. if (!dev->num_crtcs)
  1081. return false;
  1082. /* Need timestamp lock to prevent concurrent execution with
  1083. * vblank enable/disable, as this would cause inconsistent
  1084. * or corrupted timestamps and vblank counts.
  1085. */
  1086. spin_lock_irqsave(&dev->vblank_time_lock, irqflags);
  1087. /* Vblank irq handling disabled. Nothing to do. */
  1088. if (!dev->vblank_enabled[crtc]) {
  1089. spin_unlock_irqrestore(&dev->vblank_time_lock, irqflags);
  1090. return false;
  1091. }
  1092. /* Fetch corresponding timestamp for this vblank interval from
  1093. * driver and store it in proper slot of timestamp ringbuffer.
  1094. */
  1095. /* Get current timestamp and count. */
  1096. vblcount = atomic_read(&dev->_vblank_count[crtc]);
  1097. drm_get_last_vbltimestamp(dev, crtc, &tvblank, DRM_CALLED_FROM_VBLIRQ);
  1098. /* Compute time difference to timestamp of last vblank */
  1099. diff_ns = timeval_to_ns(&tvblank) -
  1100. timeval_to_ns(&vblanktimestamp(dev, crtc, vblcount));
  1101. /* Update vblank timestamp and count if at least
  1102. * DRM_REDUNDANT_VBLIRQ_THRESH_NS nanoseconds
  1103. * difference between last stored timestamp and current
  1104. * timestamp. A smaller difference means basically
  1105. * identical timestamps. Happens if this vblank has
  1106. * been already processed and this is a redundant call,
  1107. * e.g., due to spurious vblank interrupts. We need to
  1108. * ignore those for accounting.
  1109. */
  1110. if (abs64(diff_ns) > DRM_REDUNDANT_VBLIRQ_THRESH_NS) {
  1111. /* Store new timestamp in ringbuffer. */
  1112. vblanktimestamp(dev, crtc, vblcount + 1) = tvblank;
  1113. /* Increment cooked vblank count. This also atomically commits
  1114. * the timestamp computed above.
  1115. */
  1116. smp_mb__before_atomic_inc();
  1117. atomic_inc(&dev->_vblank_count[crtc]);
  1118. smp_mb__after_atomic_inc();
  1119. } else {
  1120. DRM_DEBUG("crtc %d: Redundant vblirq ignored. diff_ns = %d\n",
  1121. crtc, (int) diff_ns);
  1122. }
  1123. DRM_WAKEUP(&dev->vbl_queue[crtc]);
  1124. drm_handle_vblank_events(dev, crtc);
  1125. spin_unlock_irqrestore(&dev->vblank_time_lock, irqflags);
  1126. return true;
  1127. }
  1128. EXPORT_SYMBOL(drm_handle_vblank);