drm_irq.c 40 KB

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