drm_irq.c 44 KB

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