drm_irq.c 46 KB

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