drm_vblank.c 58 KB

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