drm_vblank.c 49 KB

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