drm_irq.c 38 KB

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