drm_irq.c 40 KB

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