drm_vblank.c 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648
  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. * Permission is hereby granted, free of charge, to any person obtaining a
  8. * copy of this software and associated documentation files (the "Software"),
  9. * to deal in the Software without restriction, including without limitation
  10. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  11. * and/or sell copies of the Software, and to permit persons to whom the
  12. * Software is furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice (including the next
  15. * paragraph) shall be included in all copies or substantial portions of the
  16. * Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  21. * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  22. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  23. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  24. * OTHER DEALINGS IN THE SOFTWARE.
  25. */
  26. #include <drm/drm_vblank.h>
  27. #include <drm/drmP.h>
  28. #include <linux/export.h>
  29. #include "drm_trace.h"
  30. #include "drm_internal.h"
  31. /* Retry timestamp calculation up to 3 times to satisfy
  32. * drm_timestamp_precision before giving up.
  33. */
  34. #define DRM_TIMESTAMP_MAXRETRIES 3
  35. /* Threshold in nanoseconds for detection of redundant
  36. * vblank irq in drm_handle_vblank(). 1 msec should be ok.
  37. */
  38. #define DRM_REDUNDANT_VBLIRQ_THRESH_NS 1000000
  39. static bool
  40. drm_get_last_vbltimestamp(struct drm_device *dev, unsigned int pipe,
  41. struct timeval *tvblank, bool in_vblank_irq);
  42. static unsigned int drm_timestamp_precision = 20; /* Default to 20 usecs. */
  43. /*
  44. * Default to use monotonic timestamps for wait-for-vblank and page-flip
  45. * complete events.
  46. */
  47. unsigned int drm_timestamp_monotonic = 1;
  48. static int drm_vblank_offdelay = 5000; /* Default to 5000 msecs. */
  49. module_param_named(vblankoffdelay, drm_vblank_offdelay, int, 0600);
  50. module_param_named(timestamp_precision_usec, drm_timestamp_precision, int, 0600);
  51. module_param_named(timestamp_monotonic, drm_timestamp_monotonic, int, 0600);
  52. MODULE_PARM_DESC(vblankoffdelay, "Delay until vblank irq auto-disable [msecs] (0: never disable, <0: disable immediately)");
  53. MODULE_PARM_DESC(timestamp_precision_usec, "Max. error on timestamps [usecs]");
  54. MODULE_PARM_DESC(timestamp_monotonic, "Use monotonic timestamps");
  55. static void store_vblank(struct drm_device *dev, unsigned int pipe,
  56. u32 vblank_count_inc,
  57. struct timeval *t_vblank, u32 last)
  58. {
  59. struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
  60. assert_spin_locked(&dev->vblank_time_lock);
  61. vblank->last = last;
  62. write_seqlock(&vblank->seqlock);
  63. vblank->time = *t_vblank;
  64. vblank->count += vblank_count_inc;
  65. write_sequnlock(&vblank->seqlock);
  66. }
  67. /*
  68. * "No hw counter" fallback implementation of .get_vblank_counter() hook,
  69. * if there is no useable hardware frame counter available.
  70. */
  71. static u32 drm_vblank_no_hw_counter(struct drm_device *dev, unsigned int pipe)
  72. {
  73. WARN_ON_ONCE(dev->max_vblank_count != 0);
  74. return 0;
  75. }
  76. static u32 __get_vblank_counter(struct drm_device *dev, unsigned int pipe)
  77. {
  78. if (drm_core_check_feature(dev, DRIVER_MODESET)) {
  79. struct drm_crtc *crtc = drm_crtc_from_index(dev, pipe);
  80. if (crtc->funcs->get_vblank_counter)
  81. return crtc->funcs->get_vblank_counter(crtc);
  82. }
  83. if (dev->driver->get_vblank_counter)
  84. return dev->driver->get_vblank_counter(dev, pipe);
  85. return drm_vblank_no_hw_counter(dev, pipe);
  86. }
  87. /*
  88. * Reset the stored timestamp for the current vblank count to correspond
  89. * to the last vblank occurred.
  90. *
  91. * Only to be called from drm_crtc_vblank_on().
  92. *
  93. * Note: caller must hold &drm_device.vbl_lock since this reads & writes
  94. * device vblank fields.
  95. */
  96. static void drm_reset_vblank_timestamp(struct drm_device *dev, unsigned int pipe)
  97. {
  98. u32 cur_vblank;
  99. bool rc;
  100. struct timeval t_vblank;
  101. int count = DRM_TIMESTAMP_MAXRETRIES;
  102. spin_lock(&dev->vblank_time_lock);
  103. /*
  104. * sample the current counter to avoid random jumps
  105. * when drm_vblank_enable() applies the diff
  106. */
  107. do {
  108. cur_vblank = __get_vblank_counter(dev, pipe);
  109. rc = drm_get_last_vbltimestamp(dev, pipe, &t_vblank, false);
  110. } while (cur_vblank != __get_vblank_counter(dev, pipe) && --count > 0);
  111. /*
  112. * Only reinitialize corresponding vblank timestamp if high-precision query
  113. * available and didn't fail. Otherwise reinitialize delayed at next vblank
  114. * interrupt and assign 0 for now, to mark the vblanktimestamp as invalid.
  115. */
  116. if (!rc)
  117. t_vblank = (struct timeval) {0, 0};
  118. /*
  119. * +1 to make sure user will never see the same
  120. * vblank counter value before and after a modeset
  121. */
  122. store_vblank(dev, pipe, 1, &t_vblank, cur_vblank);
  123. spin_unlock(&dev->vblank_time_lock);
  124. }
  125. /*
  126. * Call back into the driver to update the appropriate vblank counter
  127. * (specified by @pipe). Deal with wraparound, if it occurred, and
  128. * update the last read value so we can deal with wraparound on the next
  129. * call if necessary.
  130. *
  131. * Only necessary when going from off->on, to account for frames we
  132. * didn't get an interrupt for.
  133. *
  134. * Note: caller must hold &drm_device.vbl_lock since this reads & writes
  135. * device vblank fields.
  136. */
  137. static void drm_update_vblank_count(struct drm_device *dev, unsigned int pipe,
  138. bool in_vblank_irq)
  139. {
  140. struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
  141. u32 cur_vblank, diff;
  142. bool rc;
  143. struct timeval t_vblank;
  144. int count = DRM_TIMESTAMP_MAXRETRIES;
  145. int framedur_ns = vblank->framedur_ns;
  146. /*
  147. * Interrupts were disabled prior to this call, so deal with counter
  148. * wrap if needed.
  149. * NOTE! It's possible we lost a full dev->max_vblank_count + 1 events
  150. * here if the register is small or we had vblank interrupts off for
  151. * a long time.
  152. *
  153. * We repeat the hardware vblank counter & timestamp query until
  154. * we get consistent results. This to prevent races between gpu
  155. * updating its hardware counter while we are retrieving the
  156. * corresponding vblank timestamp.
  157. */
  158. do {
  159. cur_vblank = __get_vblank_counter(dev, pipe);
  160. rc = drm_get_last_vbltimestamp(dev, pipe, &t_vblank, in_vblank_irq);
  161. } while (cur_vblank != __get_vblank_counter(dev, pipe) && --count > 0);
  162. if (dev->max_vblank_count != 0) {
  163. /* trust the hw counter when it's around */
  164. diff = (cur_vblank - vblank->last) & dev->max_vblank_count;
  165. } else if (rc && framedur_ns) {
  166. const struct timeval *t_old;
  167. u64 diff_ns;
  168. t_old = &vblank->time;
  169. diff_ns = timeval_to_ns(&t_vblank) - timeval_to_ns(t_old);
  170. /*
  171. * Figure out how many vblanks we've missed based
  172. * on the difference in the timestamps and the
  173. * frame/field duration.
  174. */
  175. diff = DIV_ROUND_CLOSEST_ULL(diff_ns, framedur_ns);
  176. if (diff == 0 && in_vblank_irq)
  177. DRM_DEBUG_VBL("crtc %u: Redundant vblirq ignored."
  178. " diff_ns = %lld, framedur_ns = %d)\n",
  179. pipe, (long long) diff_ns, framedur_ns);
  180. } else {
  181. /* some kind of default for drivers w/o accurate vbl timestamping */
  182. diff = in_vblank_irq ? 1 : 0;
  183. }
  184. /*
  185. * Within a drm_vblank_pre_modeset - drm_vblank_post_modeset
  186. * interval? If so then vblank irqs keep running and it will likely
  187. * happen that the hardware vblank counter is not trustworthy as it
  188. * might reset at some point in that interval and vblank timestamps
  189. * are not trustworthy either in that interval. Iow. this can result
  190. * in a bogus diff >> 1 which must be avoided as it would cause
  191. * random large forward jumps of the software vblank counter.
  192. */
  193. if (diff > 1 && (vblank->inmodeset & 0x2)) {
  194. DRM_DEBUG_VBL("clamping vblank bump to 1 on crtc %u: diffr=%u"
  195. " due to pre-modeset.\n", pipe, diff);
  196. diff = 1;
  197. }
  198. DRM_DEBUG_VBL("updating vblank count on crtc %u:"
  199. " current=%u, diff=%u, hw=%u hw_last=%u\n",
  200. pipe, vblank->count, diff, cur_vblank, vblank->last);
  201. if (diff == 0) {
  202. WARN_ON_ONCE(cur_vblank != vblank->last);
  203. return;
  204. }
  205. /*
  206. * Only reinitialize corresponding vblank timestamp if high-precision query
  207. * available and didn't fail, or we were called from the vblank interrupt.
  208. * Otherwise reinitialize delayed at next vblank interrupt and assign 0
  209. * for now, to mark the vblanktimestamp as invalid.
  210. */
  211. if (!rc && in_vblank_irq)
  212. t_vblank = (struct timeval) {0, 0};
  213. store_vblank(dev, pipe, diff, &t_vblank, cur_vblank);
  214. }
  215. static u32 drm_vblank_count(struct drm_device *dev, unsigned int pipe)
  216. {
  217. struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
  218. if (WARN_ON(pipe >= dev->num_crtcs))
  219. return 0;
  220. return vblank->count;
  221. }
  222. /**
  223. * drm_accurate_vblank_count - retrieve the master vblank counter
  224. * @crtc: which counter to retrieve
  225. *
  226. * This function is similar to @drm_crtc_vblank_count but this
  227. * function interpolates to handle a race with vblank irq's.
  228. *
  229. * This is mostly useful for hardware that can obtain the scanout
  230. * position, but doesn't have a frame counter.
  231. */
  232. u32 drm_accurate_vblank_count(struct drm_crtc *crtc)
  233. {
  234. struct drm_device *dev = crtc->dev;
  235. unsigned int pipe = drm_crtc_index(crtc);
  236. u32 vblank;
  237. unsigned long flags;
  238. WARN(!dev->driver->get_vblank_timestamp,
  239. "This function requires support for accurate vblank timestamps.");
  240. spin_lock_irqsave(&dev->vblank_time_lock, flags);
  241. drm_update_vblank_count(dev, pipe, false);
  242. vblank = drm_vblank_count(dev, pipe);
  243. spin_unlock_irqrestore(&dev->vblank_time_lock, flags);
  244. return vblank;
  245. }
  246. EXPORT_SYMBOL(drm_accurate_vblank_count);
  247. static void __disable_vblank(struct drm_device *dev, unsigned int pipe)
  248. {
  249. if (drm_core_check_feature(dev, DRIVER_MODESET)) {
  250. struct drm_crtc *crtc = drm_crtc_from_index(dev, pipe);
  251. if (crtc->funcs->disable_vblank) {
  252. crtc->funcs->disable_vblank(crtc);
  253. return;
  254. }
  255. }
  256. dev->driver->disable_vblank(dev, pipe);
  257. }
  258. /*
  259. * Disable vblank irq's on crtc, make sure that last vblank count
  260. * of hardware and corresponding consistent software vblank counter
  261. * are preserved, even if there are any spurious vblank irq's after
  262. * disable.
  263. */
  264. void drm_vblank_disable_and_save(struct drm_device *dev, unsigned int pipe)
  265. {
  266. struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
  267. unsigned long irqflags;
  268. assert_spin_locked(&dev->vbl_lock);
  269. /* Prevent vblank irq processing while disabling vblank irqs,
  270. * so no updates of timestamps or count can happen after we've
  271. * disabled. Needed to prevent races in case of delayed irq's.
  272. */
  273. spin_lock_irqsave(&dev->vblank_time_lock, irqflags);
  274. /*
  275. * Only disable vblank interrupts if they're enabled. This avoids
  276. * calling the ->disable_vblank() operation in atomic context with the
  277. * hardware potentially runtime suspended.
  278. */
  279. if (vblank->enabled) {
  280. __disable_vblank(dev, pipe);
  281. vblank->enabled = false;
  282. }
  283. /*
  284. * Always update the count and timestamp to maintain the
  285. * appearance that the counter has been ticking all along until
  286. * this time. This makes the count account for the entire time
  287. * between drm_crtc_vblank_on() and drm_crtc_vblank_off().
  288. */
  289. drm_update_vblank_count(dev, pipe, false);
  290. spin_unlock_irqrestore(&dev->vblank_time_lock, irqflags);
  291. }
  292. static void vblank_disable_fn(unsigned long arg)
  293. {
  294. struct drm_vblank_crtc *vblank = (void *)arg;
  295. struct drm_device *dev = vblank->dev;
  296. unsigned int pipe = vblank->pipe;
  297. unsigned long irqflags;
  298. spin_lock_irqsave(&dev->vbl_lock, irqflags);
  299. if (atomic_read(&vblank->refcount) == 0 && vblank->enabled) {
  300. DRM_DEBUG("disabling vblank on crtc %u\n", pipe);
  301. drm_vblank_disable_and_save(dev, pipe);
  302. }
  303. spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
  304. }
  305. /**
  306. * drm_vblank_cleanup - cleanup vblank support
  307. * @dev: DRM device
  308. *
  309. * This function cleans up any resources allocated in drm_vblank_init.
  310. *
  311. * Drivers which don't use drm_irq_install() need to set &drm_device.irq_enabled
  312. * themselves, to signal to the DRM core that vblank interrupts are enabled.
  313. */
  314. void drm_vblank_cleanup(struct drm_device *dev)
  315. {
  316. unsigned int pipe;
  317. /* Bail if the driver didn't call drm_vblank_init() */
  318. if (dev->num_crtcs == 0)
  319. return;
  320. for (pipe = 0; pipe < dev->num_crtcs; pipe++) {
  321. struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
  322. WARN_ON(READ_ONCE(vblank->enabled) &&
  323. drm_core_check_feature(dev, DRIVER_MODESET));
  324. del_timer_sync(&vblank->disable_timer);
  325. }
  326. kfree(dev->vblank);
  327. dev->num_crtcs = 0;
  328. }
  329. EXPORT_SYMBOL(drm_vblank_cleanup);
  330. /**
  331. * drm_vblank_init - initialize vblank support
  332. * @dev: DRM device
  333. * @num_crtcs: number of CRTCs supported by @dev
  334. *
  335. * This function initializes vblank support for @num_crtcs display pipelines.
  336. *
  337. * Returns:
  338. * Zero on success or a negative error code on failure.
  339. */
  340. int drm_vblank_init(struct drm_device *dev, unsigned int num_crtcs)
  341. {
  342. int ret = -ENOMEM;
  343. unsigned int i;
  344. spin_lock_init(&dev->vbl_lock);
  345. spin_lock_init(&dev->vblank_time_lock);
  346. dev->num_crtcs = num_crtcs;
  347. dev->vblank = kcalloc(num_crtcs, sizeof(*dev->vblank), GFP_KERNEL);
  348. if (!dev->vblank)
  349. goto err;
  350. for (i = 0; i < num_crtcs; i++) {
  351. struct drm_vblank_crtc *vblank = &dev->vblank[i];
  352. vblank->dev = dev;
  353. vblank->pipe = i;
  354. init_waitqueue_head(&vblank->queue);
  355. setup_timer(&vblank->disable_timer, vblank_disable_fn,
  356. (unsigned long)vblank);
  357. seqlock_init(&vblank->seqlock);
  358. }
  359. DRM_INFO("Supports vblank timestamp caching Rev 2 (21.10.2013).\n");
  360. /* Driver specific high-precision vblank timestamping supported? */
  361. if (dev->driver->get_vblank_timestamp)
  362. DRM_INFO("Driver supports precise vblank timestamp query.\n");
  363. else
  364. DRM_INFO("No driver support for vblank timestamp query.\n");
  365. /* Must have precise timestamping for reliable vblank instant disable */
  366. if (dev->vblank_disable_immediate && !dev->driver->get_vblank_timestamp) {
  367. dev->vblank_disable_immediate = false;
  368. DRM_INFO("Setting vblank_disable_immediate to false because "
  369. "get_vblank_timestamp == NULL\n");
  370. }
  371. return 0;
  372. err:
  373. dev->num_crtcs = 0;
  374. return ret;
  375. }
  376. EXPORT_SYMBOL(drm_vblank_init);
  377. /**
  378. * drm_crtc_vblank_waitqueue - get vblank waitqueue for the CRTC
  379. * @crtc: which CRTC's vblank waitqueue to retrieve
  380. *
  381. * This function returns a pointer to the vblank waitqueue for the CRTC.
  382. * Drivers can use this to implement vblank waits using wait_event() and related
  383. * functions.
  384. */
  385. wait_queue_head_t *drm_crtc_vblank_waitqueue(struct drm_crtc *crtc)
  386. {
  387. return &crtc->dev->vblank[drm_crtc_index(crtc)].queue;
  388. }
  389. EXPORT_SYMBOL(drm_crtc_vblank_waitqueue);
  390. /**
  391. * drm_calc_timestamping_constants - calculate vblank timestamp constants
  392. * @crtc: drm_crtc whose timestamp constants should be updated.
  393. * @mode: display mode containing the scanout timings
  394. *
  395. * Calculate and store various constants which are later
  396. * needed by vblank and swap-completion timestamping, e.g,
  397. * by drm_calc_vbltimestamp_from_scanoutpos(). They are
  398. * derived from CRTC's true scanout timing, so they take
  399. * things like panel scaling or other adjustments into account.
  400. */
  401. void drm_calc_timestamping_constants(struct drm_crtc *crtc,
  402. const struct drm_display_mode *mode)
  403. {
  404. struct drm_device *dev = crtc->dev;
  405. unsigned int pipe = drm_crtc_index(crtc);
  406. struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
  407. int linedur_ns = 0, framedur_ns = 0;
  408. int dotclock = mode->crtc_clock;
  409. if (!dev->num_crtcs)
  410. return;
  411. if (WARN_ON(pipe >= dev->num_crtcs))
  412. return;
  413. /* Valid dotclock? */
  414. if (dotclock > 0) {
  415. int frame_size = mode->crtc_htotal * mode->crtc_vtotal;
  416. /*
  417. * Convert scanline length in pixels and video
  418. * dot clock to line duration and frame duration
  419. * in nanoseconds:
  420. */
  421. linedur_ns = div_u64((u64) mode->crtc_htotal * 1000000, dotclock);
  422. framedur_ns = div_u64((u64) frame_size * 1000000, dotclock);
  423. /*
  424. * Fields of interlaced scanout modes are only half a frame duration.
  425. */
  426. if (mode->flags & DRM_MODE_FLAG_INTERLACE)
  427. framedur_ns /= 2;
  428. } else
  429. DRM_ERROR("crtc %u: Can't calculate constants, dotclock = 0!\n",
  430. crtc->base.id);
  431. vblank->linedur_ns = linedur_ns;
  432. vblank->framedur_ns = framedur_ns;
  433. vblank->hwmode = *mode;
  434. DRM_DEBUG("crtc %u: hwmode: htotal %d, vtotal %d, vdisplay %d\n",
  435. crtc->base.id, mode->crtc_htotal,
  436. mode->crtc_vtotal, mode->crtc_vdisplay);
  437. DRM_DEBUG("crtc %u: clock %d kHz framedur %d linedur %d\n",
  438. crtc->base.id, dotclock, framedur_ns, linedur_ns);
  439. }
  440. EXPORT_SYMBOL(drm_calc_timestamping_constants);
  441. /**
  442. * drm_calc_vbltimestamp_from_scanoutpos - precise vblank timestamp helper
  443. * @dev: DRM device
  444. * @pipe: index of CRTC whose vblank timestamp to retrieve
  445. * @max_error: Desired maximum allowable error in timestamps (nanosecs)
  446. * On return contains true maximum error of timestamp
  447. * @vblank_time: Pointer to struct timeval which should receive the timestamp
  448. * @in_vblank_irq:
  449. * True when called from drm_crtc_handle_vblank(). Some drivers
  450. * need to apply some workarounds for gpu-specific vblank irq quirks
  451. * if flag is set.
  452. *
  453. * Implements calculation of exact vblank timestamps from given drm_display_mode
  454. * timings and current video scanout position of a CRTC. This can be called from
  455. * within get_vblank_timestamp() implementation of a kms driver to implement the
  456. * actual timestamping.
  457. *
  458. * Should return timestamps conforming to the OML_sync_control OpenML
  459. * extension specification. The timestamp corresponds to the end of
  460. * the vblank interval, aka start of scanout of topmost-leftmost display
  461. * pixel in the following video frame.
  462. *
  463. * Requires support for optional dev->driver->get_scanout_position()
  464. * in kms driver, plus a bit of setup code to provide a drm_display_mode
  465. * that corresponds to the true scanout timing.
  466. *
  467. * The current implementation only handles standard video modes. It
  468. * returns as no operation if a doublescan or interlaced video mode is
  469. * active. Higher level code is expected to handle this.
  470. *
  471. * This function can be used to implement the &drm_driver.get_vblank_timestamp
  472. * directly, if the driver implements the &drm_driver.get_scanout_position hook.
  473. *
  474. * Note that atomic drivers must call drm_calc_timestamping_constants() before
  475. * enabling a CRTC. The atomic helpers already take care of that in
  476. * drm_atomic_helper_update_legacy_modeset_state().
  477. *
  478. * Returns:
  479. *
  480. * Returns true on success, and false on failure, i.e. when no accurate
  481. * timestamp could be acquired.
  482. */
  483. bool drm_calc_vbltimestamp_from_scanoutpos(struct drm_device *dev,
  484. unsigned int pipe,
  485. int *max_error,
  486. struct timeval *vblank_time,
  487. bool in_vblank_irq)
  488. {
  489. struct timeval tv_etime;
  490. ktime_t stime, etime;
  491. bool vbl_status;
  492. struct drm_crtc *crtc;
  493. const struct drm_display_mode *mode;
  494. struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
  495. int vpos, hpos, i;
  496. int delta_ns, duration_ns;
  497. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  498. return false;
  499. crtc = drm_crtc_from_index(dev, pipe);
  500. if (pipe >= dev->num_crtcs || !crtc) {
  501. DRM_ERROR("Invalid crtc %u\n", pipe);
  502. return false;
  503. }
  504. /* Scanout position query not supported? Should not happen. */
  505. if (!dev->driver->get_scanout_position) {
  506. DRM_ERROR("Called from driver w/o get_scanout_position()!?\n");
  507. return false;
  508. }
  509. if (drm_drv_uses_atomic_modeset(dev))
  510. mode = &vblank->hwmode;
  511. else
  512. mode = &crtc->hwmode;
  513. /* If mode timing undefined, just return as no-op:
  514. * Happens during initial modesetting of a crtc.
  515. */
  516. if (mode->crtc_clock == 0) {
  517. DRM_DEBUG("crtc %u: Noop due to uninitialized mode.\n", pipe);
  518. WARN_ON_ONCE(drm_drv_uses_atomic_modeset(dev));
  519. return false;
  520. }
  521. /* Get current scanout position with system timestamp.
  522. * Repeat query up to DRM_TIMESTAMP_MAXRETRIES times
  523. * if single query takes longer than max_error nanoseconds.
  524. *
  525. * This guarantees a tight bound on maximum error if
  526. * code gets preempted or delayed for some reason.
  527. */
  528. for (i = 0; i < DRM_TIMESTAMP_MAXRETRIES; i++) {
  529. /*
  530. * Get vertical and horizontal scanout position vpos, hpos,
  531. * and bounding timestamps stime, etime, pre/post query.
  532. */
  533. vbl_status = dev->driver->get_scanout_position(dev, pipe,
  534. in_vblank_irq,
  535. &vpos, &hpos,
  536. &stime, &etime,
  537. mode);
  538. /* Return as no-op if scanout query unsupported or failed. */
  539. if (!vbl_status) {
  540. DRM_DEBUG("crtc %u : scanoutpos query failed.\n",
  541. pipe);
  542. return false;
  543. }
  544. /* Compute uncertainty in timestamp of scanout position query. */
  545. duration_ns = ktime_to_ns(etime) - ktime_to_ns(stime);
  546. /* Accept result with < max_error nsecs timing uncertainty. */
  547. if (duration_ns <= *max_error)
  548. break;
  549. }
  550. /* Noisy system timing? */
  551. if (i == DRM_TIMESTAMP_MAXRETRIES) {
  552. DRM_DEBUG("crtc %u: Noisy timestamp %d us > %d us [%d reps].\n",
  553. pipe, duration_ns/1000, *max_error/1000, i);
  554. }
  555. /* Return upper bound of timestamp precision error. */
  556. *max_error = duration_ns;
  557. /* Convert scanout position into elapsed time at raw_time query
  558. * since start of scanout at first display scanline. delta_ns
  559. * can be negative if start of scanout hasn't happened yet.
  560. */
  561. delta_ns = div_s64(1000000LL * (vpos * mode->crtc_htotal + hpos),
  562. mode->crtc_clock);
  563. if (!drm_timestamp_monotonic)
  564. etime = ktime_mono_to_real(etime);
  565. /* save this only for debugging purposes */
  566. tv_etime = ktime_to_timeval(etime);
  567. /* Subtract time delta from raw timestamp to get final
  568. * vblank_time timestamp for end of vblank.
  569. */
  570. etime = ktime_sub_ns(etime, delta_ns);
  571. *vblank_time = ktime_to_timeval(etime);
  572. DRM_DEBUG_VBL("crtc %u : v p(%d,%d)@ %ld.%ld -> %ld.%ld [e %d us, %d rep]\n",
  573. pipe, hpos, vpos,
  574. (long)tv_etime.tv_sec, (long)tv_etime.tv_usec,
  575. (long)vblank_time->tv_sec, (long)vblank_time->tv_usec,
  576. duration_ns/1000, i);
  577. return true;
  578. }
  579. EXPORT_SYMBOL(drm_calc_vbltimestamp_from_scanoutpos);
  580. static struct timeval get_drm_timestamp(void)
  581. {
  582. ktime_t now;
  583. now = drm_timestamp_monotonic ? ktime_get() : ktime_get_real();
  584. return ktime_to_timeval(now);
  585. }
  586. /**
  587. * drm_get_last_vbltimestamp - retrieve raw timestamp for the most recent
  588. * vblank interval
  589. * @dev: DRM device
  590. * @pipe: index of CRTC whose vblank timestamp to retrieve
  591. * @tvblank: Pointer to target struct timeval which should receive the timestamp
  592. * @in_vblank_irq:
  593. * True when called from drm_crtc_handle_vblank(). Some drivers
  594. * need to apply some workarounds for gpu-specific vblank irq quirks
  595. * if flag is set.
  596. *
  597. * Fetches the system timestamp corresponding to the time of the most recent
  598. * vblank interval on specified CRTC. May call into kms-driver to
  599. * compute the timestamp with a high-precision GPU specific method.
  600. *
  601. * Returns zero if timestamp originates from uncorrected do_gettimeofday()
  602. * call, i.e., it isn't very precisely locked to the true vblank.
  603. *
  604. * Returns:
  605. * True if timestamp is considered to be very precise, false otherwise.
  606. */
  607. static bool
  608. drm_get_last_vbltimestamp(struct drm_device *dev, unsigned int pipe,
  609. struct timeval *tvblank, bool in_vblank_irq)
  610. {
  611. bool ret = false;
  612. /* Define requested maximum error on timestamps (nanoseconds). */
  613. int max_error = (int) drm_timestamp_precision * 1000;
  614. /* Query driver if possible and precision timestamping enabled. */
  615. if (dev->driver->get_vblank_timestamp && (max_error > 0))
  616. ret = dev->driver->get_vblank_timestamp(dev, pipe, &max_error,
  617. tvblank, in_vblank_irq);
  618. /* GPU high precision timestamp query unsupported or failed.
  619. * Return current monotonic/gettimeofday timestamp as best estimate.
  620. */
  621. if (!ret)
  622. *tvblank = get_drm_timestamp();
  623. return ret;
  624. }
  625. /**
  626. * drm_crtc_vblank_count - retrieve "cooked" vblank counter value
  627. * @crtc: which counter to retrieve
  628. *
  629. * Fetches the "cooked" vblank count value that represents the number of
  630. * vblank events since the system was booted, including lost events due to
  631. * modesetting activity.
  632. *
  633. * Returns:
  634. * The software vblank counter.
  635. */
  636. u32 drm_crtc_vblank_count(struct drm_crtc *crtc)
  637. {
  638. return drm_vblank_count(crtc->dev, drm_crtc_index(crtc));
  639. }
  640. EXPORT_SYMBOL(drm_crtc_vblank_count);
  641. /**
  642. * drm_vblank_count_and_time - retrieve "cooked" vblank counter value and the
  643. * system timestamp corresponding to that vblank counter value.
  644. * @dev: DRM device
  645. * @pipe: index of CRTC whose counter to retrieve
  646. * @vblanktime: Pointer to struct timeval to receive the vblank timestamp.
  647. *
  648. * Fetches the "cooked" vblank count value that represents the number of
  649. * vblank events since the system was booted, including lost events due to
  650. * modesetting activity. Returns corresponding system timestamp of the time
  651. * of the vblank interval that corresponds to the current vblank counter value.
  652. *
  653. * This is the legacy version of drm_crtc_vblank_count_and_time().
  654. */
  655. static u32 drm_vblank_count_and_time(struct drm_device *dev, unsigned int pipe,
  656. struct timeval *vblanktime)
  657. {
  658. struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
  659. u32 vblank_count;
  660. unsigned int seq;
  661. if (WARN_ON(pipe >= dev->num_crtcs)) {
  662. *vblanktime = (struct timeval) { 0 };
  663. return 0;
  664. }
  665. do {
  666. seq = read_seqbegin(&vblank->seqlock);
  667. vblank_count = vblank->count;
  668. *vblanktime = vblank->time;
  669. } while (read_seqretry(&vblank->seqlock, seq));
  670. return vblank_count;
  671. }
  672. /**
  673. * drm_crtc_vblank_count_and_time - retrieve "cooked" vblank counter value
  674. * and the system timestamp corresponding to that vblank counter value
  675. * @crtc: which counter to retrieve
  676. * @vblanktime: Pointer to struct timeval to receive the vblank timestamp.
  677. *
  678. * Fetches the "cooked" vblank count value that represents the number of
  679. * vblank events since the system was booted, including lost events due to
  680. * modesetting activity. Returns corresponding system timestamp of the time
  681. * of the vblank interval that corresponds to the current vblank counter value.
  682. */
  683. u32 drm_crtc_vblank_count_and_time(struct drm_crtc *crtc,
  684. struct timeval *vblanktime)
  685. {
  686. return drm_vblank_count_and_time(crtc->dev, drm_crtc_index(crtc),
  687. vblanktime);
  688. }
  689. EXPORT_SYMBOL(drm_crtc_vblank_count_and_time);
  690. static void send_vblank_event(struct drm_device *dev,
  691. struct drm_pending_vblank_event *e,
  692. unsigned long seq, struct timeval *now)
  693. {
  694. e->event.sequence = seq;
  695. e->event.tv_sec = now->tv_sec;
  696. e->event.tv_usec = now->tv_usec;
  697. trace_drm_vblank_event_delivered(e->base.file_priv, e->pipe,
  698. e->event.sequence);
  699. drm_send_event_locked(dev, &e->base);
  700. }
  701. /**
  702. * drm_crtc_arm_vblank_event - arm vblank event after pageflip
  703. * @crtc: the source CRTC of the vblank event
  704. * @e: the event to send
  705. *
  706. * A lot of drivers need to generate vblank events for the very next vblank
  707. * interrupt. For example when the page flip interrupt happens when the page
  708. * flip gets armed, but not when it actually executes within the next vblank
  709. * period. This helper function implements exactly the required vblank arming
  710. * behaviour.
  711. *
  712. * NOTE: Drivers using this to send out the &drm_crtc_state.event as part of an
  713. * atomic commit must ensure that the next vblank happens at exactly the same
  714. * time as the atomic commit is committed to the hardware. This function itself
  715. * does **not** protect again the next vblank interrupt racing with either this
  716. * function call or the atomic commit operation. A possible sequence could be:
  717. *
  718. * 1. Driver commits new hardware state into vblank-synchronized registers.
  719. * 2. A vblank happens, committing the hardware state. Also the corresponding
  720. * vblank interrupt is fired off and fully processed by the interrupt
  721. * handler.
  722. * 3. The atomic commit operation proceeds to call drm_crtc_arm_vblank_event().
  723. * 4. The event is only send out for the next vblank, which is wrong.
  724. *
  725. * An equivalent race can happen when the driver calls
  726. * drm_crtc_arm_vblank_event() before writing out the new hardware state.
  727. *
  728. * The only way to make this work safely is to prevent the vblank from firing
  729. * (and the hardware from committing anything else) until the entire atomic
  730. * commit sequence has run to completion. If the hardware does not have such a
  731. * feature (e.g. using a "go" bit), then it is unsafe to use this functions.
  732. * Instead drivers need to manually send out the event from their interrupt
  733. * handler by calling drm_crtc_send_vblank_event() and make sure that there's no
  734. * possible race with the hardware committing the atomic update.
  735. *
  736. * Caller must hold event lock. Caller must also hold a vblank reference for
  737. * the event @e, which will be dropped when the next vblank arrives.
  738. */
  739. void drm_crtc_arm_vblank_event(struct drm_crtc *crtc,
  740. struct drm_pending_vblank_event *e)
  741. {
  742. struct drm_device *dev = crtc->dev;
  743. unsigned int pipe = drm_crtc_index(crtc);
  744. assert_spin_locked(&dev->event_lock);
  745. e->pipe = pipe;
  746. e->event.sequence = drm_vblank_count(dev, pipe);
  747. e->event.crtc_id = crtc->base.id;
  748. list_add_tail(&e->base.link, &dev->vblank_event_list);
  749. }
  750. EXPORT_SYMBOL(drm_crtc_arm_vblank_event);
  751. /**
  752. * drm_crtc_send_vblank_event - helper to send vblank event after pageflip
  753. * @crtc: the source CRTC of the vblank event
  754. * @e: the event to send
  755. *
  756. * Updates sequence # and timestamp on event for the most recently processed
  757. * vblank, and sends it to userspace. Caller must hold event lock.
  758. *
  759. * See drm_crtc_arm_vblank_event() for a helper which can be used in certain
  760. * situation, especially to send out events for atomic commit operations.
  761. */
  762. void drm_crtc_send_vblank_event(struct drm_crtc *crtc,
  763. struct drm_pending_vblank_event *e)
  764. {
  765. struct drm_device *dev = crtc->dev;
  766. unsigned int seq, pipe = drm_crtc_index(crtc);
  767. struct timeval now;
  768. if (dev->num_crtcs > 0) {
  769. seq = drm_vblank_count_and_time(dev, pipe, &now);
  770. } else {
  771. seq = 0;
  772. now = get_drm_timestamp();
  773. }
  774. e->pipe = pipe;
  775. e->event.crtc_id = crtc->base.id;
  776. send_vblank_event(dev, e, seq, &now);
  777. }
  778. EXPORT_SYMBOL(drm_crtc_send_vblank_event);
  779. static int __enable_vblank(struct drm_device *dev, unsigned int pipe)
  780. {
  781. if (drm_core_check_feature(dev, DRIVER_MODESET)) {
  782. struct drm_crtc *crtc = drm_crtc_from_index(dev, pipe);
  783. if (crtc->funcs->enable_vblank)
  784. return crtc->funcs->enable_vblank(crtc);
  785. }
  786. return dev->driver->enable_vblank(dev, pipe);
  787. }
  788. /**
  789. * drm_vblank_enable - enable the vblank interrupt on a CRTC
  790. * @dev: DRM device
  791. * @pipe: CRTC index
  792. *
  793. * Returns:
  794. * Zero on success or a negative error code on failure.
  795. */
  796. static int drm_vblank_enable(struct drm_device *dev, unsigned int pipe)
  797. {
  798. struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
  799. int ret = 0;
  800. assert_spin_locked(&dev->vbl_lock);
  801. spin_lock(&dev->vblank_time_lock);
  802. if (!vblank->enabled) {
  803. /*
  804. * Enable vblank irqs under vblank_time_lock protection.
  805. * All vblank count & timestamp updates are held off
  806. * until we are done reinitializing master counter and
  807. * timestamps. Filtercode in drm_handle_vblank() will
  808. * prevent double-accounting of same vblank interval.
  809. */
  810. ret = __enable_vblank(dev, pipe);
  811. DRM_DEBUG("enabling vblank on crtc %u, ret: %d\n", pipe, ret);
  812. if (ret) {
  813. atomic_dec(&vblank->refcount);
  814. } else {
  815. drm_update_vblank_count(dev, pipe, 0);
  816. /* drm_update_vblank_count() includes a wmb so we just
  817. * need to ensure that the compiler emits the write
  818. * to mark the vblank as enabled after the call
  819. * to drm_update_vblank_count().
  820. */
  821. WRITE_ONCE(vblank->enabled, true);
  822. }
  823. }
  824. spin_unlock(&dev->vblank_time_lock);
  825. return ret;
  826. }
  827. /**
  828. * drm_vblank_get - get a reference count on vblank events
  829. * @dev: DRM device
  830. * @pipe: index of CRTC to own
  831. *
  832. * Acquire a reference count on vblank events to avoid having them disabled
  833. * while in use.
  834. *
  835. * This is the legacy version of drm_crtc_vblank_get().
  836. *
  837. * Returns:
  838. * Zero on success or a negative error code on failure.
  839. */
  840. static int drm_vblank_get(struct drm_device *dev, unsigned int pipe)
  841. {
  842. struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
  843. unsigned long irqflags;
  844. int ret = 0;
  845. if (!dev->num_crtcs)
  846. return -EINVAL;
  847. if (WARN_ON(pipe >= dev->num_crtcs))
  848. return -EINVAL;
  849. spin_lock_irqsave(&dev->vbl_lock, irqflags);
  850. /* Going from 0->1 means we have to enable interrupts again */
  851. if (atomic_add_return(1, &vblank->refcount) == 1) {
  852. ret = drm_vblank_enable(dev, pipe);
  853. } else {
  854. if (!vblank->enabled) {
  855. atomic_dec(&vblank->refcount);
  856. ret = -EINVAL;
  857. }
  858. }
  859. spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
  860. return ret;
  861. }
  862. /**
  863. * drm_crtc_vblank_get - get a reference count on vblank events
  864. * @crtc: which CRTC to own
  865. *
  866. * Acquire a reference count on vblank events to avoid having them disabled
  867. * while in use.
  868. *
  869. * Returns:
  870. * Zero on success or a negative error code on failure.
  871. */
  872. int drm_crtc_vblank_get(struct drm_crtc *crtc)
  873. {
  874. return drm_vblank_get(crtc->dev, drm_crtc_index(crtc));
  875. }
  876. EXPORT_SYMBOL(drm_crtc_vblank_get);
  877. /**
  878. * drm_vblank_put - release ownership of vblank events
  879. * @dev: DRM device
  880. * @pipe: index of CRTC to release
  881. *
  882. * Release ownership of a given vblank counter, turning off interrupts
  883. * if possible. Disable interrupts after drm_vblank_offdelay milliseconds.
  884. *
  885. * This is the legacy version of drm_crtc_vblank_put().
  886. */
  887. static void drm_vblank_put(struct drm_device *dev, unsigned int pipe)
  888. {
  889. struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
  890. if (WARN_ON(pipe >= dev->num_crtcs))
  891. return;
  892. if (WARN_ON(atomic_read(&vblank->refcount) == 0))
  893. return;
  894. /* Last user schedules interrupt disable */
  895. if (atomic_dec_and_test(&vblank->refcount)) {
  896. if (drm_vblank_offdelay == 0)
  897. return;
  898. else if (drm_vblank_offdelay < 0)
  899. vblank_disable_fn((unsigned long)vblank);
  900. else if (!dev->vblank_disable_immediate)
  901. mod_timer(&vblank->disable_timer,
  902. jiffies + ((drm_vblank_offdelay * HZ)/1000));
  903. }
  904. }
  905. /**
  906. * drm_crtc_vblank_put - give up ownership of vblank events
  907. * @crtc: which counter to give up
  908. *
  909. * Release ownership of a given vblank counter, turning off interrupts
  910. * if possible. Disable interrupts after drm_vblank_offdelay milliseconds.
  911. */
  912. void drm_crtc_vblank_put(struct drm_crtc *crtc)
  913. {
  914. drm_vblank_put(crtc->dev, drm_crtc_index(crtc));
  915. }
  916. EXPORT_SYMBOL(drm_crtc_vblank_put);
  917. /**
  918. * drm_wait_one_vblank - wait for one vblank
  919. * @dev: DRM device
  920. * @pipe: CRTC index
  921. *
  922. * This waits for one vblank to pass on @pipe, using the irq driver interfaces.
  923. * It is a failure to call this when the vblank irq for @pipe is disabled, e.g.
  924. * due to lack of driver support or because the crtc is off.
  925. */
  926. void drm_wait_one_vblank(struct drm_device *dev, unsigned int pipe)
  927. {
  928. struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
  929. int ret;
  930. u32 last;
  931. if (WARN_ON(pipe >= dev->num_crtcs))
  932. return;
  933. ret = drm_vblank_get(dev, pipe);
  934. if (WARN(ret, "vblank not available on crtc %i, ret=%i\n", pipe, ret))
  935. return;
  936. last = drm_vblank_count(dev, pipe);
  937. ret = wait_event_timeout(vblank->queue,
  938. last != drm_vblank_count(dev, pipe),
  939. msecs_to_jiffies(100));
  940. WARN(ret == 0, "vblank wait timed out on crtc %i\n", pipe);
  941. drm_vblank_put(dev, pipe);
  942. }
  943. EXPORT_SYMBOL(drm_wait_one_vblank);
  944. /**
  945. * drm_crtc_wait_one_vblank - wait for one vblank
  946. * @crtc: DRM crtc
  947. *
  948. * This waits for one vblank to pass on @crtc, using the irq driver interfaces.
  949. * It is a failure to call this when the vblank irq for @crtc is disabled, e.g.
  950. * due to lack of driver support or because the crtc is off.
  951. */
  952. void drm_crtc_wait_one_vblank(struct drm_crtc *crtc)
  953. {
  954. drm_wait_one_vblank(crtc->dev, drm_crtc_index(crtc));
  955. }
  956. EXPORT_SYMBOL(drm_crtc_wait_one_vblank);
  957. /**
  958. * drm_crtc_vblank_off - disable vblank events on a CRTC
  959. * @crtc: CRTC in question
  960. *
  961. * Drivers can use this function to shut down the vblank interrupt handling when
  962. * disabling a crtc. This function ensures that the latest vblank frame count is
  963. * stored so that drm_vblank_on can restore it again.
  964. *
  965. * Drivers must use this function when the hardware vblank counter can get
  966. * reset, e.g. when suspending.
  967. */
  968. void drm_crtc_vblank_off(struct drm_crtc *crtc)
  969. {
  970. struct drm_device *dev = crtc->dev;
  971. unsigned int pipe = drm_crtc_index(crtc);
  972. struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
  973. struct drm_pending_vblank_event *e, *t;
  974. struct timeval now;
  975. unsigned long irqflags;
  976. unsigned int seq;
  977. if (WARN_ON(pipe >= dev->num_crtcs))
  978. return;
  979. spin_lock_irqsave(&dev->event_lock, irqflags);
  980. spin_lock(&dev->vbl_lock);
  981. DRM_DEBUG_VBL("crtc %d, vblank enabled %d, inmodeset %d\n",
  982. pipe, vblank->enabled, vblank->inmodeset);
  983. /* Avoid redundant vblank disables without previous
  984. * drm_crtc_vblank_on(). */
  985. if (drm_core_check_feature(dev, DRIVER_ATOMIC) || !vblank->inmodeset)
  986. drm_vblank_disable_and_save(dev, pipe);
  987. wake_up(&vblank->queue);
  988. /*
  989. * Prevent subsequent drm_vblank_get() from re-enabling
  990. * the vblank interrupt by bumping the refcount.
  991. */
  992. if (!vblank->inmodeset) {
  993. atomic_inc(&vblank->refcount);
  994. vblank->inmodeset = 1;
  995. }
  996. spin_unlock(&dev->vbl_lock);
  997. /* Send any queued vblank events, lest the natives grow disquiet */
  998. seq = drm_vblank_count_and_time(dev, pipe, &now);
  999. list_for_each_entry_safe(e, t, &dev->vblank_event_list, base.link) {
  1000. if (e->pipe != pipe)
  1001. continue;
  1002. DRM_DEBUG("Sending premature vblank event on disable: "
  1003. "wanted %u, current %u\n",
  1004. e->event.sequence, seq);
  1005. list_del(&e->base.link);
  1006. drm_vblank_put(dev, pipe);
  1007. send_vblank_event(dev, e, seq, &now);
  1008. }
  1009. spin_unlock_irqrestore(&dev->event_lock, irqflags);
  1010. /* Will be reset by the modeset helpers when re-enabling the crtc by
  1011. * calling drm_calc_timestamping_constants(). */
  1012. vblank->hwmode.crtc_clock = 0;
  1013. }
  1014. EXPORT_SYMBOL(drm_crtc_vblank_off);
  1015. /**
  1016. * drm_crtc_vblank_reset - reset vblank state to off on a CRTC
  1017. * @crtc: CRTC in question
  1018. *
  1019. * Drivers can use this function to reset the vblank state to off at load time.
  1020. * Drivers should use this together with the drm_crtc_vblank_off() and
  1021. * drm_crtc_vblank_on() functions. The difference compared to
  1022. * drm_crtc_vblank_off() is that this function doesn't save the vblank counter
  1023. * and hence doesn't need to call any driver hooks.
  1024. */
  1025. void drm_crtc_vblank_reset(struct drm_crtc *crtc)
  1026. {
  1027. struct drm_device *dev = crtc->dev;
  1028. unsigned long irqflags;
  1029. unsigned int pipe = drm_crtc_index(crtc);
  1030. struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
  1031. spin_lock_irqsave(&dev->vbl_lock, irqflags);
  1032. /*
  1033. * Prevent subsequent drm_vblank_get() from enabling the vblank
  1034. * interrupt by bumping the refcount.
  1035. */
  1036. if (!vblank->inmodeset) {
  1037. atomic_inc(&vblank->refcount);
  1038. vblank->inmodeset = 1;
  1039. }
  1040. spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
  1041. WARN_ON(!list_empty(&dev->vblank_event_list));
  1042. }
  1043. EXPORT_SYMBOL(drm_crtc_vblank_reset);
  1044. /**
  1045. * drm_crtc_vblank_on - enable vblank events on a CRTC
  1046. * @crtc: CRTC in question
  1047. *
  1048. * This functions restores the vblank interrupt state captured with
  1049. * drm_crtc_vblank_off() again. Note that calls to drm_crtc_vblank_on() and
  1050. * drm_crtc_vblank_off() can be unbalanced and so can also be unconditionally called
  1051. * in driver load code to reflect the current hardware state of the crtc.
  1052. */
  1053. void drm_crtc_vblank_on(struct drm_crtc *crtc)
  1054. {
  1055. struct drm_device *dev = crtc->dev;
  1056. unsigned int pipe = drm_crtc_index(crtc);
  1057. struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
  1058. unsigned long irqflags;
  1059. if (WARN_ON(pipe >= dev->num_crtcs))
  1060. return;
  1061. spin_lock_irqsave(&dev->vbl_lock, irqflags);
  1062. DRM_DEBUG_VBL("crtc %d, vblank enabled %d, inmodeset %d\n",
  1063. pipe, vblank->enabled, vblank->inmodeset);
  1064. /* Drop our private "prevent drm_vblank_get" refcount */
  1065. if (vblank->inmodeset) {
  1066. atomic_dec(&vblank->refcount);
  1067. vblank->inmodeset = 0;
  1068. }
  1069. drm_reset_vblank_timestamp(dev, pipe);
  1070. /*
  1071. * re-enable interrupts if there are users left, or the
  1072. * user wishes vblank interrupts to be enabled all the time.
  1073. */
  1074. if (atomic_read(&vblank->refcount) != 0 || drm_vblank_offdelay == 0)
  1075. WARN_ON(drm_vblank_enable(dev, pipe));
  1076. spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
  1077. }
  1078. EXPORT_SYMBOL(drm_crtc_vblank_on);
  1079. static void drm_legacy_vblank_pre_modeset(struct drm_device *dev,
  1080. unsigned int pipe)
  1081. {
  1082. struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
  1083. /* vblank is not initialized (IRQ not installed ?), or has been freed */
  1084. if (!dev->num_crtcs)
  1085. return;
  1086. if (WARN_ON(pipe >= dev->num_crtcs))
  1087. return;
  1088. /*
  1089. * To avoid all the problems that might happen if interrupts
  1090. * were enabled/disabled around or between these calls, we just
  1091. * have the kernel take a reference on the CRTC (just once though
  1092. * to avoid corrupting the count if multiple, mismatch calls occur),
  1093. * so that interrupts remain enabled in the interim.
  1094. */
  1095. if (!vblank->inmodeset) {
  1096. vblank->inmodeset = 0x1;
  1097. if (drm_vblank_get(dev, pipe) == 0)
  1098. vblank->inmodeset |= 0x2;
  1099. }
  1100. }
  1101. static void drm_legacy_vblank_post_modeset(struct drm_device *dev,
  1102. unsigned int pipe)
  1103. {
  1104. struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
  1105. unsigned long irqflags;
  1106. /* vblank is not initialized (IRQ not installed ?), or has been freed */
  1107. if (!dev->num_crtcs)
  1108. return;
  1109. if (WARN_ON(pipe >= dev->num_crtcs))
  1110. return;
  1111. if (vblank->inmodeset) {
  1112. spin_lock_irqsave(&dev->vbl_lock, irqflags);
  1113. drm_reset_vblank_timestamp(dev, pipe);
  1114. spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
  1115. if (vblank->inmodeset & 0x2)
  1116. drm_vblank_put(dev, pipe);
  1117. vblank->inmodeset = 0;
  1118. }
  1119. }
  1120. int drm_legacy_modeset_ctl(struct drm_device *dev, void *data,
  1121. struct drm_file *file_priv)
  1122. {
  1123. struct drm_modeset_ctl *modeset = data;
  1124. unsigned int pipe;
  1125. /* If drm_vblank_init() hasn't been called yet, just no-op */
  1126. if (!dev->num_crtcs)
  1127. return 0;
  1128. /* KMS drivers handle this internally */
  1129. if (!drm_core_check_feature(dev, DRIVER_LEGACY))
  1130. return 0;
  1131. pipe = modeset->crtc;
  1132. if (pipe >= dev->num_crtcs)
  1133. return -EINVAL;
  1134. switch (modeset->cmd) {
  1135. case _DRM_PRE_MODESET:
  1136. drm_legacy_vblank_pre_modeset(dev, pipe);
  1137. break;
  1138. case _DRM_POST_MODESET:
  1139. drm_legacy_vblank_post_modeset(dev, pipe);
  1140. break;
  1141. default:
  1142. return -EINVAL;
  1143. }
  1144. return 0;
  1145. }
  1146. static inline bool vblank_passed(u32 seq, u32 ref)
  1147. {
  1148. return (seq - ref) <= (1 << 23);
  1149. }
  1150. static int drm_queue_vblank_event(struct drm_device *dev, unsigned int pipe,
  1151. union drm_wait_vblank *vblwait,
  1152. struct drm_file *file_priv)
  1153. {
  1154. struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
  1155. struct drm_pending_vblank_event *e;
  1156. struct timeval now;
  1157. unsigned long flags;
  1158. unsigned int seq;
  1159. int ret;
  1160. e = kzalloc(sizeof(*e), GFP_KERNEL);
  1161. if (e == NULL) {
  1162. ret = -ENOMEM;
  1163. goto err_put;
  1164. }
  1165. e->pipe = pipe;
  1166. e->event.base.type = DRM_EVENT_VBLANK;
  1167. e->event.base.length = sizeof(e->event);
  1168. e->event.user_data = vblwait->request.signal;
  1169. spin_lock_irqsave(&dev->event_lock, flags);
  1170. /*
  1171. * drm_crtc_vblank_off() might have been called after we called
  1172. * drm_vblank_get(). drm_crtc_vblank_off() holds event_lock around the
  1173. * vblank disable, so no need for further locking. The reference from
  1174. * drm_vblank_get() protects against vblank disable from another source.
  1175. */
  1176. if (!READ_ONCE(vblank->enabled)) {
  1177. ret = -EINVAL;
  1178. goto err_unlock;
  1179. }
  1180. ret = drm_event_reserve_init_locked(dev, file_priv, &e->base,
  1181. &e->event.base);
  1182. if (ret)
  1183. goto err_unlock;
  1184. seq = drm_vblank_count_and_time(dev, pipe, &now);
  1185. DRM_DEBUG("event on vblank count %u, current %u, crtc %u\n",
  1186. vblwait->request.sequence, seq, pipe);
  1187. trace_drm_vblank_event_queued(file_priv, pipe,
  1188. vblwait->request.sequence);
  1189. e->event.sequence = vblwait->request.sequence;
  1190. if (vblank_passed(seq, vblwait->request.sequence)) {
  1191. drm_vblank_put(dev, pipe);
  1192. send_vblank_event(dev, e, seq, &now);
  1193. vblwait->reply.sequence = seq;
  1194. } else {
  1195. /* drm_handle_vblank_events will call drm_vblank_put */
  1196. list_add_tail(&e->base.link, &dev->vblank_event_list);
  1197. vblwait->reply.sequence = vblwait->request.sequence;
  1198. }
  1199. spin_unlock_irqrestore(&dev->event_lock, flags);
  1200. return 0;
  1201. err_unlock:
  1202. spin_unlock_irqrestore(&dev->event_lock, flags);
  1203. kfree(e);
  1204. err_put:
  1205. drm_vblank_put(dev, pipe);
  1206. return ret;
  1207. }
  1208. static bool drm_wait_vblank_is_query(union drm_wait_vblank *vblwait)
  1209. {
  1210. if (vblwait->request.sequence)
  1211. return false;
  1212. return _DRM_VBLANK_RELATIVE ==
  1213. (vblwait->request.type & (_DRM_VBLANK_TYPES_MASK |
  1214. _DRM_VBLANK_EVENT |
  1215. _DRM_VBLANK_NEXTONMISS));
  1216. }
  1217. /*
  1218. * Wait for VBLANK.
  1219. *
  1220. * \param inode device inode.
  1221. * \param file_priv DRM file private.
  1222. * \param cmd command.
  1223. * \param data user argument, pointing to a drm_wait_vblank structure.
  1224. * \return zero on success or a negative number on failure.
  1225. *
  1226. * This function enables the vblank interrupt on the pipe requested, then
  1227. * sleeps waiting for the requested sequence number to occur, and drops
  1228. * the vblank interrupt refcount afterwards. (vblank IRQ disable follows that
  1229. * after a timeout with no further vblank waits scheduled).
  1230. */
  1231. int drm_wait_vblank(struct drm_device *dev, void *data,
  1232. struct drm_file *file_priv)
  1233. {
  1234. struct drm_vblank_crtc *vblank;
  1235. union drm_wait_vblank *vblwait = data;
  1236. int ret;
  1237. unsigned int flags, seq, pipe, high_pipe;
  1238. if (!dev->irq_enabled)
  1239. return -EINVAL;
  1240. if (vblwait->request.type & _DRM_VBLANK_SIGNAL)
  1241. return -EINVAL;
  1242. if (vblwait->request.type &
  1243. ~(_DRM_VBLANK_TYPES_MASK | _DRM_VBLANK_FLAGS_MASK |
  1244. _DRM_VBLANK_HIGH_CRTC_MASK)) {
  1245. DRM_ERROR("Unsupported type value 0x%x, supported mask 0x%x\n",
  1246. vblwait->request.type,
  1247. (_DRM_VBLANK_TYPES_MASK | _DRM_VBLANK_FLAGS_MASK |
  1248. _DRM_VBLANK_HIGH_CRTC_MASK));
  1249. return -EINVAL;
  1250. }
  1251. flags = vblwait->request.type & _DRM_VBLANK_FLAGS_MASK;
  1252. high_pipe = (vblwait->request.type & _DRM_VBLANK_HIGH_CRTC_MASK);
  1253. if (high_pipe)
  1254. pipe = high_pipe >> _DRM_VBLANK_HIGH_CRTC_SHIFT;
  1255. else
  1256. pipe = flags & _DRM_VBLANK_SECONDARY ? 1 : 0;
  1257. if (pipe >= dev->num_crtcs)
  1258. return -EINVAL;
  1259. vblank = &dev->vblank[pipe];
  1260. /* If the counter is currently enabled and accurate, short-circuit
  1261. * queries to return the cached timestamp of the last vblank.
  1262. */
  1263. if (dev->vblank_disable_immediate &&
  1264. drm_wait_vblank_is_query(vblwait) &&
  1265. READ_ONCE(vblank->enabled)) {
  1266. struct timeval now;
  1267. vblwait->reply.sequence =
  1268. drm_vblank_count_and_time(dev, pipe, &now);
  1269. vblwait->reply.tval_sec = now.tv_sec;
  1270. vblwait->reply.tval_usec = now.tv_usec;
  1271. return 0;
  1272. }
  1273. ret = drm_vblank_get(dev, pipe);
  1274. if (ret) {
  1275. DRM_DEBUG("crtc %d failed to acquire vblank counter, %d\n", pipe, ret);
  1276. return ret;
  1277. }
  1278. seq = drm_vblank_count(dev, pipe);
  1279. switch (vblwait->request.type & _DRM_VBLANK_TYPES_MASK) {
  1280. case _DRM_VBLANK_RELATIVE:
  1281. vblwait->request.sequence += seq;
  1282. vblwait->request.type &= ~_DRM_VBLANK_RELATIVE;
  1283. case _DRM_VBLANK_ABSOLUTE:
  1284. break;
  1285. default:
  1286. ret = -EINVAL;
  1287. goto done;
  1288. }
  1289. if ((flags & _DRM_VBLANK_NEXTONMISS) &&
  1290. vblank_passed(seq, vblwait->request.sequence))
  1291. vblwait->request.sequence = seq + 1;
  1292. if (flags & _DRM_VBLANK_EVENT) {
  1293. /* must hold on to the vblank ref until the event fires
  1294. * drm_vblank_put will be called asynchronously
  1295. */
  1296. return drm_queue_vblank_event(dev, pipe, vblwait, file_priv);
  1297. }
  1298. if (vblwait->request.sequence != seq) {
  1299. DRM_DEBUG("waiting on vblank count %u, crtc %u\n",
  1300. vblwait->request.sequence, pipe);
  1301. DRM_WAIT_ON(ret, vblank->queue, 3 * HZ,
  1302. vblank_passed(drm_vblank_count(dev, pipe),
  1303. vblwait->request.sequence) ||
  1304. !READ_ONCE(vblank->enabled));
  1305. }
  1306. if (ret != -EINTR) {
  1307. struct timeval now;
  1308. vblwait->reply.sequence = drm_vblank_count_and_time(dev, pipe, &now);
  1309. vblwait->reply.tval_sec = now.tv_sec;
  1310. vblwait->reply.tval_usec = now.tv_usec;
  1311. DRM_DEBUG("crtc %d returning %u to client\n",
  1312. pipe, vblwait->reply.sequence);
  1313. } else {
  1314. DRM_DEBUG("crtc %d vblank wait interrupted by signal\n", pipe);
  1315. }
  1316. done:
  1317. drm_vblank_put(dev, pipe);
  1318. return ret;
  1319. }
  1320. static void drm_handle_vblank_events(struct drm_device *dev, unsigned int pipe)
  1321. {
  1322. struct drm_pending_vblank_event *e, *t;
  1323. struct timeval now;
  1324. unsigned int seq;
  1325. assert_spin_locked(&dev->event_lock);
  1326. seq = drm_vblank_count_and_time(dev, pipe, &now);
  1327. list_for_each_entry_safe(e, t, &dev->vblank_event_list, base.link) {
  1328. if (e->pipe != pipe)
  1329. continue;
  1330. if (!vblank_passed(seq, e->event.sequence))
  1331. continue;
  1332. DRM_DEBUG("vblank event on %u, current %u\n",
  1333. e->event.sequence, seq);
  1334. list_del(&e->base.link);
  1335. drm_vblank_put(dev, pipe);
  1336. send_vblank_event(dev, e, seq, &now);
  1337. }
  1338. trace_drm_vblank_event(pipe, seq);
  1339. }
  1340. /**
  1341. * drm_handle_vblank - handle a vblank event
  1342. * @dev: DRM device
  1343. * @pipe: index of CRTC where this event occurred
  1344. *
  1345. * Drivers should call this routine in their vblank interrupt handlers to
  1346. * update the vblank counter and send any signals that may be pending.
  1347. *
  1348. * This is the legacy version of drm_crtc_handle_vblank().
  1349. */
  1350. bool drm_handle_vblank(struct drm_device *dev, unsigned int pipe)
  1351. {
  1352. struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
  1353. unsigned long irqflags;
  1354. bool disable_irq;
  1355. if (WARN_ON_ONCE(!dev->num_crtcs))
  1356. return false;
  1357. if (WARN_ON(pipe >= dev->num_crtcs))
  1358. return false;
  1359. spin_lock_irqsave(&dev->event_lock, irqflags);
  1360. /* Need timestamp lock to prevent concurrent execution with
  1361. * vblank enable/disable, as this would cause inconsistent
  1362. * or corrupted timestamps and vblank counts.
  1363. */
  1364. spin_lock(&dev->vblank_time_lock);
  1365. /* Vblank irq handling disabled. Nothing to do. */
  1366. if (!vblank->enabled) {
  1367. spin_unlock(&dev->vblank_time_lock);
  1368. spin_unlock_irqrestore(&dev->event_lock, irqflags);
  1369. return false;
  1370. }
  1371. drm_update_vblank_count(dev, pipe, true);
  1372. spin_unlock(&dev->vblank_time_lock);
  1373. wake_up(&vblank->queue);
  1374. /* With instant-off, we defer disabling the interrupt until after
  1375. * we finish processing the following vblank after all events have
  1376. * been signaled. The disable has to be last (after
  1377. * drm_handle_vblank_events) so that the timestamp is always accurate.
  1378. */
  1379. disable_irq = (dev->vblank_disable_immediate &&
  1380. drm_vblank_offdelay > 0 &&
  1381. !atomic_read(&vblank->refcount));
  1382. drm_handle_vblank_events(dev, pipe);
  1383. spin_unlock_irqrestore(&dev->event_lock, irqflags);
  1384. if (disable_irq)
  1385. vblank_disable_fn((unsigned long)vblank);
  1386. return true;
  1387. }
  1388. EXPORT_SYMBOL(drm_handle_vblank);
  1389. /**
  1390. * drm_crtc_handle_vblank - handle a vblank event
  1391. * @crtc: where this event occurred
  1392. *
  1393. * Drivers should call this routine in their vblank interrupt handlers to
  1394. * update the vblank counter and send any signals that may be pending.
  1395. *
  1396. * This is the native KMS version of drm_handle_vblank().
  1397. *
  1398. * Returns:
  1399. * True if the event was successfully handled, false on failure.
  1400. */
  1401. bool drm_crtc_handle_vblank(struct drm_crtc *crtc)
  1402. {
  1403. return drm_handle_vblank(crtc->dev, drm_crtc_index(crtc));
  1404. }
  1405. EXPORT_SYMBOL(drm_crtc_handle_vblank);