drm_irq.c 40 KB

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