drm_vblank.c 57 KB

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