drm_vblank.c 73 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320
  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 <linux/export.h>
  27. #include <linux/kthread.h>
  28. #include <linux/moduleparam.h>
  29. #include <drm/drm_crtc.h>
  30. #include <drm/drm_drv.h>
  31. #include <drm/drm_framebuffer.h>
  32. #include <drm/drm_managed.h>
  33. #include <drm/drm_modeset_helper_vtables.h>
  34. #include <drm/drm_print.h>
  35. #include <drm/drm_vblank.h>
  36. #include "drm_internal.h"
  37. #include "drm_trace.h"
  38. /**
  39. * DOC: vblank handling
  40. *
  41. * From the computer's perspective, every time the monitor displays
  42. * a new frame the scanout engine has "scanned out" the display image
  43. * from top to bottom, one row of pixels at a time. The current row
  44. * of pixels is referred to as the current scanline.
  45. *
  46. * In addition to the display's visible area, there's usually a couple of
  47. * extra scanlines which aren't actually displayed on the screen.
  48. * These extra scanlines don't contain image data and are occasionally used
  49. * for features like audio and infoframes. The region made up of these
  50. * scanlines is referred to as the vertical blanking region, or vblank for
  51. * short.
  52. *
  53. * For historical reference, the vertical blanking period was designed to
  54. * give the electron gun (on CRTs) enough time to move back to the top of
  55. * the screen to start scanning out the next frame. Similar for horizontal
  56. * blanking periods. They were designed to give the electron gun enough
  57. * time to move back to the other side of the screen to start scanning the
  58. * next scanline.
  59. *
  60. * ::
  61. *
  62. *
  63. * physical → ⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽
  64. * top of | |
  65. * display | |
  66. * | New frame |
  67. * | |
  68. * |↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓|
  69. * |~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~| ← Scanline,
  70. * |↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓| updates the
  71. * | | frame as it
  72. * | | travels down
  73. * | | ("scan out")
  74. * | Old frame |
  75. * | |
  76. * | |
  77. * | |
  78. * | | physical
  79. * | | bottom of
  80. * vertical |⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽| ← display
  81. * blanking ┆xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx┆
  82. * region → ┆xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx┆
  83. * ┆xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx┆
  84. * start of → ⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽
  85. * new frame
  86. *
  87. * "Physical top of display" is the reference point for the high-precision/
  88. * corrected timestamp.
  89. *
  90. * On a lot of display hardware, programming needs to take effect during the
  91. * vertical blanking period so that settings like gamma, the image buffer
  92. * buffer to be scanned out, etc. can safely be changed without showing
  93. * any visual artifacts on the screen. In some unforgiving hardware, some of
  94. * this programming has to both start and end in the same vblank. To help
  95. * with the timing of the hardware programming, an interrupt is usually
  96. * available to notify the driver when it can start the updating of registers.
  97. * The interrupt is in this context named the vblank interrupt.
  98. *
  99. * The vblank interrupt may be fired at different points depending on the
  100. * hardware. Some hardware implementations will fire the interrupt when the
  101. * new frame start, other implementations will fire the interrupt at different
  102. * points in time.
  103. *
  104. * Vertical blanking plays a major role in graphics rendering. To achieve
  105. * tear-free display, users must synchronize page flips and/or rendering to
  106. * vertical blanking. The DRM API offers ioctls to perform page flips
  107. * synchronized to vertical blanking and wait for vertical blanking.
  108. *
  109. * The DRM core handles most of the vertical blanking management logic, which
  110. * involves filtering out spurious interrupts, keeping race-free blanking
  111. * counters, coping with counter wrap-around and resets and keeping use counts.
  112. * It relies on the driver to generate vertical blanking interrupts and
  113. * optionally provide a hardware vertical blanking counter.
  114. *
  115. * Drivers must initialize the vertical blanking handling core with a call to
  116. * drm_vblank_init(). Minimally, a driver needs to implement
  117. * &drm_crtc_funcs.enable_vblank and &drm_crtc_funcs.disable_vblank plus call
  118. * drm_crtc_handle_vblank() in its vblank interrupt handler for working vblank
  119. * support.
  120. *
  121. * Vertical blanking interrupts can be enabled by the DRM core or by drivers
  122. * themselves (for instance to handle page flipping operations). The DRM core
  123. * maintains a vertical blanking use count to ensure that the interrupts are not
  124. * disabled while a user still needs them. To increment the use count, drivers
  125. * call drm_crtc_vblank_get() and release the vblank reference again with
  126. * drm_crtc_vblank_put(). In between these two calls vblank interrupts are
  127. * guaranteed to be enabled.
  128. *
  129. * On many hardware disabling the vblank interrupt cannot be done in a race-free
  130. * manner, see &drm_vblank_crtc_config.disable_immediate and
  131. * &drm_driver.max_vblank_count. In that case the vblank core only disables the
  132. * vblanks after a timer has expired, which can be configured through the
  133. * ``vblankoffdelay`` module parameter.
  134. *
  135. * Drivers for hardware without support for vertical-blanking interrupts can
  136. * use DRM vblank timers to send vblank events at the rate of the current
  137. * display mode's refresh. While not synchronized to the hardware's
  138. * vertical-blanking regions, the timer helps DRM clients and compositors to
  139. * adapt their update cycle to the display output. Drivers should set up
  140. * vblanking as usual, but call drm_crtc_vblank_start_timer() and
  141. * drm_crtc_vblank_cancel_timer() as part of their atomic mode setting.
  142. * See also DRM vblank helpers for more information.
  143. *
  144. * Drivers without support for vertical-blanking interrupts nor timers must
  145. * not call drm_vblank_init(). For these drivers, atomic helpers will
  146. * automatically generate fake vblank events as part of the display update.
  147. * This functionality also can be controlled by the driver by enabling and
  148. * disabling struct drm_crtc_state.no_vblank.
  149. */
  150. /* Retry timestamp calculation up to 3 times to satisfy
  151. * drm_timestamp_precision before giving up.
  152. */
  153. #define DRM_TIMESTAMP_MAXRETRIES 3
  154. /* Threshold in nanoseconds for detection of redundant
  155. * vblank irq in drm_handle_vblank(). 1 msec should be ok.
  156. */
  157. #define DRM_REDUNDANT_VBLIRQ_THRESH_NS 1000000
  158. static bool
  159. drm_get_last_vbltimestamp(struct drm_device *dev, unsigned int pipe,
  160. ktime_t *tvblank, bool in_vblank_irq);
  161. static unsigned int drm_timestamp_precision = 20; /* Default to 20 usecs. */
  162. static int drm_vblank_offdelay = 5000; /* Default to 5000 msecs. */
  163. module_param_named(vblankoffdelay, drm_vblank_offdelay, int, 0600);
  164. module_param_named(timestamp_precision_usec, drm_timestamp_precision, int, 0600);
  165. MODULE_PARM_DESC(vblankoffdelay, "Delay until vblank irq auto-disable [msecs] (0: never disable, <0: disable immediately)");
  166. MODULE_PARM_DESC(timestamp_precision_usec, "Max. error on timestamps [usecs]");
  167. static struct drm_vblank_crtc *
  168. drm_vblank_crtc(struct drm_device *dev, unsigned int pipe)
  169. {
  170. return &dev->vblank[pipe];
  171. }
  172. struct drm_vblank_crtc *
  173. drm_crtc_vblank_crtc(struct drm_crtc *crtc)
  174. {
  175. return drm_vblank_crtc(crtc->dev, drm_crtc_index(crtc));
  176. }
  177. EXPORT_SYMBOL(drm_crtc_vblank_crtc);
  178. static void store_vblank(struct drm_device *dev, unsigned int pipe,
  179. u32 vblank_count_inc,
  180. ktime_t t_vblank, u32 last)
  181. {
  182. struct drm_vblank_crtc *vblank = drm_vblank_crtc(dev, pipe);
  183. assert_spin_locked(&dev->vblank_time_lock);
  184. vblank->last = last;
  185. write_seqlock(&vblank->seqlock);
  186. vblank->time = t_vblank;
  187. atomic64_add(vblank_count_inc, &vblank->count);
  188. write_sequnlock(&vblank->seqlock);
  189. }
  190. static u32 drm_max_vblank_count(struct drm_device *dev, unsigned int pipe)
  191. {
  192. struct drm_vblank_crtc *vblank = drm_vblank_crtc(dev, pipe);
  193. return vblank->max_vblank_count ?: dev->max_vblank_count;
  194. }
  195. /*
  196. * "No hw counter" fallback implementation of .get_vblank_counter() hook,
  197. * if there is no usable hardware frame counter available.
  198. */
  199. static u32 drm_vblank_no_hw_counter(struct drm_device *dev, unsigned int pipe)
  200. {
  201. drm_WARN_ON_ONCE(dev, drm_max_vblank_count(dev, pipe) != 0);
  202. return 0;
  203. }
  204. static u32 __get_vblank_counter(struct drm_device *dev, unsigned int pipe)
  205. {
  206. if (drm_core_check_feature(dev, DRIVER_MODESET)) {
  207. struct drm_crtc *crtc = drm_crtc_from_index(dev, pipe);
  208. if (drm_WARN_ON(dev, !crtc))
  209. return 0;
  210. if (crtc->funcs->get_vblank_counter)
  211. return crtc->funcs->get_vblank_counter(crtc);
  212. }
  213. return drm_vblank_no_hw_counter(dev, pipe);
  214. }
  215. /*
  216. * Reset the stored timestamp for the current vblank count to correspond
  217. * to the last vblank occurred.
  218. *
  219. * Only to be called from drm_crtc_vblank_on().
  220. *
  221. * Note: caller must hold &drm_device.vbl_lock since this reads & writes
  222. * device vblank fields.
  223. */
  224. static void drm_reset_vblank_timestamp(struct drm_device *dev, unsigned int pipe)
  225. {
  226. u32 cur_vblank;
  227. bool rc;
  228. ktime_t t_vblank;
  229. int count = DRM_TIMESTAMP_MAXRETRIES;
  230. spin_lock(&dev->vblank_time_lock);
  231. /*
  232. * sample the current counter to avoid random jumps
  233. * when drm_vblank_enable() applies the diff
  234. */
  235. do {
  236. cur_vblank = __get_vblank_counter(dev, pipe);
  237. rc = drm_get_last_vbltimestamp(dev, pipe, &t_vblank, false);
  238. } while (cur_vblank != __get_vblank_counter(dev, pipe) && --count > 0);
  239. /*
  240. * Only reinitialize corresponding vblank timestamp if high-precision query
  241. * available and didn't fail. Otherwise reinitialize delayed at next vblank
  242. * interrupt and assign 0 for now, to mark the vblanktimestamp as invalid.
  243. */
  244. if (!rc)
  245. t_vblank = 0;
  246. /*
  247. * +1 to make sure user will never see the same
  248. * vblank counter value before and after a modeset
  249. */
  250. store_vblank(dev, pipe, 1, t_vblank, cur_vblank);
  251. spin_unlock(&dev->vblank_time_lock);
  252. }
  253. /*
  254. * Call back into the driver to update the appropriate vblank counter
  255. * (specified by @pipe). Deal with wraparound, if it occurred, and
  256. * update the last read value so we can deal with wraparound on the next
  257. * call if necessary.
  258. *
  259. * Only necessary when going from off->on, to account for frames we
  260. * didn't get an interrupt for.
  261. *
  262. * Note: caller must hold &drm_device.vbl_lock since this reads & writes
  263. * device vblank fields.
  264. */
  265. static void drm_update_vblank_count(struct drm_device *dev, unsigned int pipe,
  266. bool in_vblank_irq)
  267. {
  268. struct drm_vblank_crtc *vblank = drm_vblank_crtc(dev, pipe);
  269. u32 cur_vblank, diff;
  270. bool rc;
  271. ktime_t t_vblank;
  272. int count = DRM_TIMESTAMP_MAXRETRIES;
  273. int framedur_ns = vblank->framedur_ns;
  274. u32 max_vblank_count = drm_max_vblank_count(dev, pipe);
  275. /*
  276. * Interrupts were disabled prior to this call, so deal with counter
  277. * wrap if needed.
  278. * NOTE! It's possible we lost a full dev->max_vblank_count + 1 events
  279. * here if the register is small or we had vblank interrupts off for
  280. * a long time.
  281. *
  282. * We repeat the hardware vblank counter & timestamp query until
  283. * we get consistent results. This to prevent races between gpu
  284. * updating its hardware counter while we are retrieving the
  285. * corresponding vblank timestamp.
  286. */
  287. do {
  288. cur_vblank = __get_vblank_counter(dev, pipe);
  289. rc = drm_get_last_vbltimestamp(dev, pipe, &t_vblank, in_vblank_irq);
  290. } while (cur_vblank != __get_vblank_counter(dev, pipe) && --count > 0);
  291. if (max_vblank_count) {
  292. /* trust the hw counter when it's around */
  293. diff = (cur_vblank - vblank->last) & max_vblank_count;
  294. } else if (rc && framedur_ns) {
  295. u64 diff_ns = ktime_to_ns(ktime_sub(t_vblank, vblank->time));
  296. /*
  297. * Figure out how many vblanks we've missed based
  298. * on the difference in the timestamps and the
  299. * frame/field duration.
  300. */
  301. drm_dbg_vbl(dev, "crtc %u: Calculating number of vblanks."
  302. " diff_ns = %lld, framedur_ns = %d)\n",
  303. pipe, (long long)diff_ns, framedur_ns);
  304. diff = DIV_ROUND_CLOSEST_ULL(diff_ns, framedur_ns);
  305. if (diff == 0 && in_vblank_irq)
  306. drm_dbg_vbl(dev, "crtc %u: Redundant vblirq ignored\n",
  307. pipe);
  308. } else {
  309. /* some kind of default for drivers w/o accurate vbl timestamping */
  310. diff = in_vblank_irq ? 1 : 0;
  311. }
  312. /*
  313. * Within a drm_vblank_pre_modeset - drm_vblank_post_modeset
  314. * interval? If so then vblank irqs keep running and it will likely
  315. * happen that the hardware vblank counter is not trustworthy as it
  316. * might reset at some point in that interval and vblank timestamps
  317. * are not trustworthy either in that interval. Iow. this can result
  318. * in a bogus diff >> 1 which must be avoided as it would cause
  319. * random large forward jumps of the software vblank counter.
  320. */
  321. if (diff > 1 && (vblank->inmodeset & 0x2)) {
  322. drm_dbg_vbl(dev,
  323. "clamping vblank bump to 1 on crtc %u: diffr=%u"
  324. " due to pre-modeset.\n", pipe, diff);
  325. diff = 1;
  326. }
  327. drm_dbg_vbl(dev, "updating vblank count on crtc %u:"
  328. " current=%llu, diff=%u, hw=%u hw_last=%u\n",
  329. pipe, (unsigned long long)atomic64_read(&vblank->count),
  330. diff, cur_vblank, vblank->last);
  331. if (diff == 0) {
  332. drm_WARN_ON_ONCE(dev, cur_vblank != vblank->last);
  333. return;
  334. }
  335. /*
  336. * Only reinitialize corresponding vblank timestamp if high-precision query
  337. * available and didn't fail, or we were called from the vblank interrupt.
  338. * Otherwise reinitialize delayed at next vblank interrupt and assign 0
  339. * for now, to mark the vblanktimestamp as invalid.
  340. */
  341. if (!rc && !in_vblank_irq)
  342. t_vblank = 0;
  343. store_vblank(dev, pipe, diff, t_vblank, cur_vblank);
  344. }
  345. u64 drm_vblank_count(struct drm_device *dev, unsigned int pipe)
  346. {
  347. struct drm_vblank_crtc *vblank = drm_vblank_crtc(dev, pipe);
  348. u64 count;
  349. if (drm_WARN_ON(dev, pipe >= dev->num_crtcs))
  350. return 0;
  351. count = atomic64_read(&vblank->count);
  352. /*
  353. * This read barrier corresponds to the implicit write barrier of the
  354. * write seqlock in store_vblank(). Note that this is the only place
  355. * where we need an explicit barrier, since all other access goes
  356. * through drm_vblank_count_and_time(), which already has the required
  357. * read barrier curtesy of the read seqlock.
  358. */
  359. smp_rmb();
  360. return count;
  361. }
  362. /**
  363. * drm_crtc_accurate_vblank_count - retrieve the master vblank counter
  364. * @crtc: which counter to retrieve
  365. *
  366. * This function is similar to drm_crtc_vblank_count() but this function
  367. * interpolates to handle a race with vblank interrupts using the high precision
  368. * timestamping support.
  369. *
  370. * This is mostly useful for hardware that can obtain the scanout position, but
  371. * doesn't have a hardware frame counter.
  372. */
  373. u64 drm_crtc_accurate_vblank_count(struct drm_crtc *crtc)
  374. {
  375. struct drm_device *dev = crtc->dev;
  376. unsigned int pipe = drm_crtc_index(crtc);
  377. u64 vblank;
  378. unsigned long flags;
  379. drm_WARN_ONCE(dev, drm_debug_enabled(DRM_UT_VBL) &&
  380. !crtc->funcs->get_vblank_timestamp,
  381. "This function requires support for accurate vblank timestamps.");
  382. spin_lock_irqsave(&dev->vblank_time_lock, flags);
  383. drm_update_vblank_count(dev, pipe, false);
  384. vblank = drm_vblank_count(dev, pipe);
  385. spin_unlock_irqrestore(&dev->vblank_time_lock, flags);
  386. return vblank;
  387. }
  388. EXPORT_SYMBOL(drm_crtc_accurate_vblank_count);
  389. static void __disable_vblank(struct drm_device *dev, unsigned int pipe)
  390. {
  391. if (drm_core_check_feature(dev, DRIVER_MODESET)) {
  392. struct drm_crtc *crtc = drm_crtc_from_index(dev, pipe);
  393. if (drm_WARN_ON(dev, !crtc))
  394. return;
  395. if (crtc->funcs->disable_vblank)
  396. crtc->funcs->disable_vblank(crtc);
  397. }
  398. }
  399. /*
  400. * Disable vblank irq's on crtc, make sure that last vblank count
  401. * of hardware and corresponding consistent software vblank counter
  402. * are preserved, even if there are any spurious vblank irq's after
  403. * disable.
  404. */
  405. void drm_vblank_disable_and_save(struct drm_device *dev, unsigned int pipe)
  406. {
  407. struct drm_vblank_crtc *vblank = drm_vblank_crtc(dev, pipe);
  408. unsigned long irqflags;
  409. assert_spin_locked(&dev->vbl_lock);
  410. /* Prevent vblank irq processing while disabling vblank irqs,
  411. * so no updates of timestamps or count can happen after we've
  412. * disabled. Needed to prevent races in case of delayed irq's.
  413. */
  414. spin_lock_irqsave(&dev->vblank_time_lock, irqflags);
  415. /*
  416. * Update vblank count and disable vblank interrupts only if the
  417. * interrupts were enabled. This avoids calling the ->disable_vblank()
  418. * operation in atomic context with the hardware potentially runtime
  419. * suspended.
  420. */
  421. if (!vblank->enabled)
  422. goto out;
  423. /*
  424. * Update the count and timestamp to maintain the
  425. * appearance that the counter has been ticking all along until
  426. * this time. This makes the count account for the entire time
  427. * between drm_crtc_vblank_on() and drm_crtc_vblank_off().
  428. */
  429. drm_update_vblank_count(dev, pipe, false);
  430. __disable_vblank(dev, pipe);
  431. vblank->enabled = false;
  432. out:
  433. spin_unlock_irqrestore(&dev->vblank_time_lock, irqflags);
  434. }
  435. static void vblank_disable_fn(struct timer_list *t)
  436. {
  437. struct drm_vblank_crtc *vblank = timer_container_of(vblank, t,
  438. disable_timer);
  439. struct drm_device *dev = vblank->dev;
  440. unsigned int pipe = vblank->pipe;
  441. unsigned long irqflags;
  442. spin_lock_irqsave(&dev->vbl_lock, irqflags);
  443. if (atomic_read(&vblank->refcount) == 0 && vblank->enabled) {
  444. drm_dbg_core(dev, "disabling vblank on crtc %u\n", pipe);
  445. drm_vblank_disable_and_save(dev, pipe);
  446. }
  447. spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
  448. }
  449. static void drm_vblank_init_release(struct drm_device *dev, void *ptr)
  450. {
  451. struct drm_vblank_crtc *vblank = ptr;
  452. drm_WARN_ON(dev, READ_ONCE(vblank->enabled) &&
  453. drm_core_check_feature(dev, DRIVER_MODESET));
  454. if (vblank->vblank_timer.crtc)
  455. hrtimer_cancel(&vblank->vblank_timer.timer);
  456. drm_vblank_destroy_worker(vblank);
  457. timer_delete_sync(&vblank->disable_timer);
  458. }
  459. /**
  460. * drm_vblank_init - initialize vblank support
  461. * @dev: DRM device
  462. * @num_crtcs: number of CRTCs supported by @dev
  463. *
  464. * This function initializes vblank support for @num_crtcs display pipelines.
  465. * Cleanup is handled automatically through a cleanup function added with
  466. * drmm_add_action_or_reset().
  467. *
  468. * Returns:
  469. * Zero on success or a negative error code on failure.
  470. */
  471. int drm_vblank_init(struct drm_device *dev, unsigned int num_crtcs)
  472. {
  473. int ret;
  474. unsigned int i;
  475. spin_lock_init(&dev->vbl_lock);
  476. spin_lock_init(&dev->vblank_time_lock);
  477. dev->vblank = drmm_kcalloc(dev, num_crtcs, sizeof(*dev->vblank), GFP_KERNEL);
  478. if (!dev->vblank)
  479. return -ENOMEM;
  480. dev->num_crtcs = num_crtcs;
  481. for (i = 0; i < num_crtcs; i++) {
  482. struct drm_vblank_crtc *vblank = drm_vblank_crtc(dev, i);
  483. vblank->dev = dev;
  484. vblank->pipe = i;
  485. init_waitqueue_head(&vblank->queue);
  486. timer_setup(&vblank->disable_timer, vblank_disable_fn, 0);
  487. seqlock_init(&vblank->seqlock);
  488. ret = drmm_add_action_or_reset(dev, drm_vblank_init_release,
  489. vblank);
  490. if (ret)
  491. return ret;
  492. ret = drm_vblank_worker_init(vblank);
  493. if (ret)
  494. return ret;
  495. }
  496. return 0;
  497. }
  498. EXPORT_SYMBOL(drm_vblank_init);
  499. /**
  500. * drm_dev_has_vblank - test if vblanking has been initialized for
  501. * a device
  502. * @dev: the device
  503. *
  504. * Drivers may call this function to test if vblank support is
  505. * initialized for a device. For most hardware this means that vblanking
  506. * can also be enabled.
  507. *
  508. * Atomic helpers use this function to initialize
  509. * &drm_crtc_state.no_vblank. See also drm_atomic_helper_check_modeset().
  510. *
  511. * Returns:
  512. * True if vblanking has been initialized for the given device, false
  513. * otherwise.
  514. */
  515. bool drm_dev_has_vblank(const struct drm_device *dev)
  516. {
  517. return dev->num_crtcs != 0;
  518. }
  519. EXPORT_SYMBOL(drm_dev_has_vblank);
  520. /**
  521. * drm_crtc_vblank_waitqueue - get vblank waitqueue for the CRTC
  522. * @crtc: which CRTC's vblank waitqueue to retrieve
  523. *
  524. * This function returns a pointer to the vblank waitqueue for the CRTC.
  525. * Drivers can use this to implement vblank waits using wait_event() and related
  526. * functions.
  527. */
  528. wait_queue_head_t *drm_crtc_vblank_waitqueue(struct drm_crtc *crtc)
  529. {
  530. struct drm_vblank_crtc *vblank = drm_crtc_vblank_crtc(crtc);
  531. return &vblank->queue;
  532. }
  533. EXPORT_SYMBOL(drm_crtc_vblank_waitqueue);
  534. /**
  535. * drm_calc_timestamping_constants - calculate vblank timestamp constants
  536. * @crtc: drm_crtc whose timestamp constants should be updated.
  537. * @mode: display mode containing the scanout timings
  538. *
  539. * Calculate and store various constants which are later needed by vblank and
  540. * swap-completion timestamping, e.g, by
  541. * drm_crtc_vblank_helper_get_vblank_timestamp(). They are derived from
  542. * CRTC's true scanout timing, so they take things like panel scaling or
  543. * other adjustments into account.
  544. */
  545. void drm_calc_timestamping_constants(struct drm_crtc *crtc,
  546. const struct drm_display_mode *mode)
  547. {
  548. struct drm_device *dev = crtc->dev;
  549. unsigned int pipe = drm_crtc_index(crtc);
  550. struct drm_vblank_crtc *vblank = drm_crtc_vblank_crtc(crtc);
  551. int linedur_ns = 0, framedur_ns = 0;
  552. int dotclock = mode->crtc_clock;
  553. if (!drm_dev_has_vblank(dev))
  554. return;
  555. if (drm_WARN_ON(dev, pipe >= dev->num_crtcs))
  556. return;
  557. /* Valid dotclock? */
  558. if (dotclock > 0) {
  559. int frame_size = mode->crtc_htotal * mode->crtc_vtotal;
  560. /*
  561. * Convert scanline length in pixels and video
  562. * dot clock to line duration and frame duration
  563. * in nanoseconds:
  564. */
  565. linedur_ns = div_u64((u64) mode->crtc_htotal * 1000000, dotclock);
  566. framedur_ns = div_u64((u64) frame_size * 1000000, dotclock);
  567. /*
  568. * Fields of interlaced scanout modes are only half a frame duration.
  569. */
  570. if (mode->flags & DRM_MODE_FLAG_INTERLACE)
  571. framedur_ns /= 2;
  572. } else {
  573. drm_err(dev, "crtc %u: Can't calculate constants, dotclock = 0!\n",
  574. crtc->base.id);
  575. }
  576. vblank->linedur_ns = linedur_ns;
  577. vblank->framedur_ns = framedur_ns;
  578. drm_mode_copy(&vblank->hwmode, mode);
  579. drm_dbg_core(dev,
  580. "crtc %u: hwmode: htotal %d, vtotal %d, vdisplay %d\n",
  581. crtc->base.id, mode->crtc_htotal,
  582. mode->crtc_vtotal, mode->crtc_vdisplay);
  583. drm_dbg_core(dev, "crtc %u: clock %d kHz framedur %d linedur %d\n",
  584. crtc->base.id, dotclock, framedur_ns, linedur_ns);
  585. }
  586. EXPORT_SYMBOL(drm_calc_timestamping_constants);
  587. /**
  588. * drm_crtc_vblank_helper_get_vblank_timestamp_internal - precise vblank
  589. * timestamp helper
  590. * @crtc: CRTC whose vblank timestamp to retrieve
  591. * @max_error: Desired maximum allowable error in timestamps (nanosecs)
  592. * On return contains true maximum error of timestamp
  593. * @vblank_time: Pointer to time which should receive the timestamp
  594. * @in_vblank_irq:
  595. * True when called from drm_crtc_handle_vblank(). Some drivers
  596. * need to apply some workarounds for gpu-specific vblank irq quirks
  597. * if flag is set.
  598. * @get_scanout_position:
  599. * Callback function to retrieve the scanout position. See
  600. * @struct drm_crtc_helper_funcs.get_scanout_position.
  601. *
  602. * Implements calculation of exact vblank timestamps from given drm_display_mode
  603. * timings and current video scanout position of a CRTC.
  604. *
  605. * The current implementation only handles standard video modes. For double scan
  606. * and interlaced modes the driver is supposed to adjust the hardware mode
  607. * (taken from &drm_crtc_state.adjusted mode for atomic modeset drivers) to
  608. * match the scanout position reported.
  609. *
  610. * Note that atomic drivers must call drm_calc_timestamping_constants() before
  611. * enabling a CRTC. The atomic helpers already take care of that in
  612. * drm_atomic_helper_calc_timestamping_constants().
  613. *
  614. * Returns:
  615. * Returns true on success, and false on failure, i.e. when no accurate
  616. * timestamp could be acquired.
  617. */
  618. bool
  619. drm_crtc_vblank_helper_get_vblank_timestamp_internal(
  620. struct drm_crtc *crtc, int *max_error, ktime_t *vblank_time,
  621. bool in_vblank_irq,
  622. drm_vblank_get_scanout_position_func get_scanout_position)
  623. {
  624. struct drm_device *dev = crtc->dev;
  625. unsigned int pipe = crtc->index;
  626. struct timespec64 ts_etime, ts_vblank_time;
  627. ktime_t stime, etime;
  628. bool vbl_status;
  629. const struct drm_display_mode *mode;
  630. int vpos, hpos, i;
  631. int delta_ns, duration_ns;
  632. if (pipe >= dev->num_crtcs) {
  633. drm_err(dev, "Invalid crtc %u\n", pipe);
  634. return false;
  635. }
  636. /* Scanout position query not supported? Should not happen. */
  637. if (!get_scanout_position) {
  638. drm_err(dev, "Called from CRTC w/o get_scanout_position()!?\n");
  639. return false;
  640. }
  641. if (drm_drv_uses_atomic_modeset(dev)) {
  642. struct drm_vblank_crtc *vblank = drm_crtc_vblank_crtc(crtc);
  643. mode = &vblank->hwmode;
  644. } else {
  645. mode = &crtc->hwmode;
  646. }
  647. /* If mode timing undefined, just return as no-op:
  648. * Happens during initial modesetting of a crtc.
  649. */
  650. if (mode->crtc_clock == 0) {
  651. drm_dbg_core(dev, "crtc %u: Noop due to uninitialized mode.\n",
  652. pipe);
  653. drm_WARN_ON_ONCE(dev, drm_drv_uses_atomic_modeset(dev));
  654. return false;
  655. }
  656. /* Get current scanout position with system timestamp.
  657. * Repeat query up to DRM_TIMESTAMP_MAXRETRIES times
  658. * if single query takes longer than max_error nanoseconds.
  659. *
  660. * This guarantees a tight bound on maximum error if
  661. * code gets preempted or delayed for some reason.
  662. */
  663. for (i = 0; i < DRM_TIMESTAMP_MAXRETRIES; i++) {
  664. /*
  665. * Get vertical and horizontal scanout position vpos, hpos,
  666. * and bounding timestamps stime, etime, pre/post query.
  667. */
  668. vbl_status = get_scanout_position(crtc, in_vblank_irq,
  669. &vpos, &hpos,
  670. &stime, &etime,
  671. mode);
  672. /* Return as no-op if scanout query unsupported or failed. */
  673. if (!vbl_status) {
  674. drm_dbg_core(dev,
  675. "crtc %u : scanoutpos query failed.\n",
  676. pipe);
  677. return false;
  678. }
  679. /* Compute uncertainty in timestamp of scanout position query. */
  680. duration_ns = ktime_to_ns(etime) - ktime_to_ns(stime);
  681. /* Accept result with < max_error nsecs timing uncertainty. */
  682. if (duration_ns <= *max_error)
  683. break;
  684. }
  685. /* Noisy system timing? */
  686. if (i == DRM_TIMESTAMP_MAXRETRIES) {
  687. drm_dbg_core(dev,
  688. "crtc %u: Noisy timestamp %d us > %d us [%d reps].\n",
  689. pipe, duration_ns / 1000, *max_error / 1000, i);
  690. }
  691. /* Return upper bound of timestamp precision error. */
  692. *max_error = duration_ns;
  693. /* Convert scanout position into elapsed time at raw_time query
  694. * since start of scanout at first display scanline. delta_ns
  695. * can be negative if start of scanout hasn't happened yet.
  696. */
  697. delta_ns = div_s64(1000000LL * (vpos * mode->crtc_htotal + hpos),
  698. mode->crtc_clock);
  699. /* Subtract time delta from raw timestamp to get final
  700. * vblank_time timestamp for end of vblank.
  701. */
  702. *vblank_time = ktime_sub_ns(etime, delta_ns);
  703. if (!drm_debug_enabled(DRM_UT_VBL))
  704. return true;
  705. ts_etime = ktime_to_timespec64(etime);
  706. ts_vblank_time = ktime_to_timespec64(*vblank_time);
  707. drm_dbg_vbl(dev,
  708. "crtc %u : v p(%d,%d)@ %ptSp -> %ptSp [e %d us, %d rep]\n",
  709. pipe, hpos, vpos, &ts_etime, &ts_vblank_time,
  710. duration_ns / 1000, i);
  711. return true;
  712. }
  713. EXPORT_SYMBOL(drm_crtc_vblank_helper_get_vblank_timestamp_internal);
  714. /**
  715. * drm_crtc_vblank_helper_get_vblank_timestamp - precise vblank timestamp
  716. * helper
  717. * @crtc: CRTC whose vblank timestamp to retrieve
  718. * @max_error: Desired maximum allowable error in timestamps (nanosecs)
  719. * On return contains true maximum error of timestamp
  720. * @vblank_time: Pointer to time which should receive the timestamp
  721. * @in_vblank_irq:
  722. * True when called from drm_crtc_handle_vblank(). Some drivers
  723. * need to apply some workarounds for gpu-specific vblank irq quirks
  724. * if flag is set.
  725. *
  726. * Implements calculation of exact vblank timestamps from given drm_display_mode
  727. * timings and current video scanout position of a CRTC. This can be directly
  728. * used as the &drm_crtc_funcs.get_vblank_timestamp implementation of a kms
  729. * driver if &drm_crtc_helper_funcs.get_scanout_position is implemented.
  730. *
  731. * The current implementation only handles standard video modes. For double scan
  732. * and interlaced modes the driver is supposed to adjust the hardware mode
  733. * (taken from &drm_crtc_state.adjusted mode for atomic modeset drivers) to
  734. * match the scanout position reported.
  735. *
  736. * Note that atomic drivers must call drm_calc_timestamping_constants() before
  737. * enabling a CRTC. The atomic helpers already take care of that in
  738. * drm_atomic_helper_calc_timestamping_constants().
  739. *
  740. * Returns:
  741. * Returns true on success, and false on failure, i.e. when no accurate
  742. * timestamp could be acquired.
  743. */
  744. bool drm_crtc_vblank_helper_get_vblank_timestamp(struct drm_crtc *crtc,
  745. int *max_error,
  746. ktime_t *vblank_time,
  747. bool in_vblank_irq)
  748. {
  749. return drm_crtc_vblank_helper_get_vblank_timestamp_internal(
  750. crtc, max_error, vblank_time, in_vblank_irq,
  751. crtc->helper_private->get_scanout_position);
  752. }
  753. EXPORT_SYMBOL(drm_crtc_vblank_helper_get_vblank_timestamp);
  754. /**
  755. * drm_crtc_get_last_vbltimestamp - retrieve raw timestamp for the most
  756. * recent vblank interval
  757. * @crtc: CRTC whose vblank timestamp to retrieve
  758. * @tvblank: Pointer to target time which should receive the timestamp
  759. * @in_vblank_irq:
  760. * True when called from drm_crtc_handle_vblank(). Some drivers
  761. * need to apply some workarounds for gpu-specific vblank irq quirks
  762. * if flag is set.
  763. *
  764. * Fetches the system timestamp corresponding to the time of the most recent
  765. * vblank interval on specified CRTC. May call into kms-driver to
  766. * compute the timestamp with a high-precision GPU specific method.
  767. *
  768. * Returns zero if timestamp originates from uncorrected do_gettimeofday()
  769. * call, i.e., it isn't very precisely locked to the true vblank.
  770. *
  771. * Returns:
  772. * True if timestamp is considered to be very precise, false otherwise.
  773. */
  774. static bool
  775. drm_crtc_get_last_vbltimestamp(struct drm_crtc *crtc, ktime_t *tvblank,
  776. bool in_vblank_irq)
  777. {
  778. bool ret = false;
  779. /* Define requested maximum error on timestamps (nanoseconds). */
  780. int max_error = (int) drm_timestamp_precision * 1000;
  781. /* Query driver if possible and precision timestamping enabled. */
  782. if (crtc && crtc->funcs->get_vblank_timestamp && max_error > 0) {
  783. ret = crtc->funcs->get_vblank_timestamp(crtc, &max_error,
  784. tvblank, in_vblank_irq);
  785. }
  786. /* GPU high precision timestamp query unsupported or failed.
  787. * Return current monotonic/gettimeofday timestamp as best estimate.
  788. */
  789. if (!ret)
  790. *tvblank = ktime_get();
  791. return ret;
  792. }
  793. static bool
  794. drm_get_last_vbltimestamp(struct drm_device *dev, unsigned int pipe,
  795. ktime_t *tvblank, bool in_vblank_irq)
  796. {
  797. struct drm_crtc *crtc = drm_crtc_from_index(dev, pipe);
  798. return drm_crtc_get_last_vbltimestamp(crtc, tvblank, in_vblank_irq);
  799. }
  800. /**
  801. * drm_crtc_vblank_count - retrieve "cooked" vblank counter value
  802. * @crtc: which counter to retrieve
  803. *
  804. * Fetches the "cooked" vblank count value that represents the number of
  805. * vblank events since the system was booted, including lost events due to
  806. * modesetting activity. Note that this timer isn't correct against a racing
  807. * vblank interrupt (since it only reports the software vblank counter), see
  808. * drm_crtc_accurate_vblank_count() for such use-cases.
  809. *
  810. * Note that for a given vblank counter value drm_crtc_handle_vblank()
  811. * and drm_crtc_vblank_count() or drm_crtc_vblank_count_and_time()
  812. * provide a barrier: Any writes done before calling
  813. * drm_crtc_handle_vblank() will be visible to callers of the later
  814. * functions, if the vblank count is the same or a later one.
  815. *
  816. * See also &drm_vblank_crtc.count.
  817. *
  818. * Returns:
  819. * The software vblank counter.
  820. */
  821. u64 drm_crtc_vblank_count(struct drm_crtc *crtc)
  822. {
  823. return drm_vblank_count(crtc->dev, drm_crtc_index(crtc));
  824. }
  825. EXPORT_SYMBOL(drm_crtc_vblank_count);
  826. /**
  827. * drm_vblank_count_and_time - retrieve "cooked" vblank counter value and the
  828. * system timestamp corresponding to that vblank counter value.
  829. * @dev: DRM device
  830. * @pipe: index of CRTC whose counter to retrieve
  831. * @vblanktime: Pointer to ktime_t to receive the vblank timestamp.
  832. *
  833. * Fetches the "cooked" vblank count value that represents the number of
  834. * vblank events since the system was booted, including lost events due to
  835. * modesetting activity. Returns corresponding system timestamp of the time
  836. * of the vblank interval that corresponds to the current vblank counter value.
  837. *
  838. * This is the legacy version of drm_crtc_vblank_count_and_time().
  839. */
  840. static u64 drm_vblank_count_and_time(struct drm_device *dev, unsigned int pipe,
  841. ktime_t *vblanktime)
  842. {
  843. struct drm_vblank_crtc *vblank = drm_vblank_crtc(dev, pipe);
  844. u64 vblank_count;
  845. unsigned int seq;
  846. if (drm_WARN_ON(dev, pipe >= dev->num_crtcs)) {
  847. *vblanktime = 0;
  848. return 0;
  849. }
  850. do {
  851. seq = read_seqbegin(&vblank->seqlock);
  852. vblank_count = atomic64_read(&vblank->count);
  853. *vblanktime = vblank->time;
  854. } while (read_seqretry(&vblank->seqlock, seq));
  855. return vblank_count;
  856. }
  857. /**
  858. * drm_crtc_vblank_count_and_time - retrieve "cooked" vblank counter value
  859. * and the system timestamp corresponding to that vblank counter value
  860. * @crtc: which counter to retrieve
  861. * @vblanktime: Pointer to time to receive the vblank timestamp.
  862. *
  863. * Fetches the "cooked" vblank count value that represents the number of
  864. * vblank events since the system was booted, including lost events due to
  865. * modesetting activity. Returns corresponding system timestamp of the time
  866. * of the vblank interval that corresponds to the current vblank counter value.
  867. *
  868. * Note that for a given vblank counter value drm_crtc_handle_vblank()
  869. * and drm_crtc_vblank_count() or drm_crtc_vblank_count_and_time()
  870. * provide a barrier: Any writes done before calling
  871. * drm_crtc_handle_vblank() will be visible to callers of the later
  872. * functions, if the vblank count is the same or a later one.
  873. *
  874. * See also &drm_vblank_crtc.count.
  875. */
  876. u64 drm_crtc_vblank_count_and_time(struct drm_crtc *crtc,
  877. ktime_t *vblanktime)
  878. {
  879. return drm_vblank_count_and_time(crtc->dev, drm_crtc_index(crtc),
  880. vblanktime);
  881. }
  882. EXPORT_SYMBOL(drm_crtc_vblank_count_and_time);
  883. /**
  884. * drm_crtc_next_vblank_start - calculate the time of the next vblank
  885. * @crtc: the crtc for which to calculate next vblank time
  886. * @vblanktime: pointer to time to receive the next vblank timestamp.
  887. *
  888. * Calculate the expected time of the start of the next vblank period,
  889. * based on time of previous vblank and frame duration
  890. */
  891. int drm_crtc_next_vblank_start(struct drm_crtc *crtc, ktime_t *vblanktime)
  892. {
  893. struct drm_vblank_crtc *vblank;
  894. struct drm_display_mode *mode;
  895. u64 vblank_start;
  896. if (!drm_dev_has_vblank(crtc->dev))
  897. return -EINVAL;
  898. vblank = drm_crtc_vblank_crtc(crtc);
  899. mode = &vblank->hwmode;
  900. if (!vblank->framedur_ns || !vblank->linedur_ns)
  901. return -EINVAL;
  902. if (!drm_crtc_get_last_vbltimestamp(crtc, vblanktime, false))
  903. return -EINVAL;
  904. vblank_start = DIV_ROUND_DOWN_ULL(
  905. (u64)vblank->framedur_ns * mode->crtc_vblank_start,
  906. mode->crtc_vtotal);
  907. *vblanktime = ktime_add(*vblanktime, ns_to_ktime(vblank_start));
  908. return 0;
  909. }
  910. EXPORT_SYMBOL(drm_crtc_next_vblank_start);
  911. static void send_vblank_event(struct drm_device *dev,
  912. struct drm_pending_vblank_event *e,
  913. u64 seq, ktime_t now)
  914. {
  915. struct timespec64 tv;
  916. switch (e->event.base.type) {
  917. case DRM_EVENT_VBLANK:
  918. case DRM_EVENT_FLIP_COMPLETE:
  919. tv = ktime_to_timespec64(now);
  920. e->event.vbl.sequence = seq;
  921. /*
  922. * e->event is a user space structure, with hardcoded unsigned
  923. * 32-bit seconds/microseconds. This is safe as we always use
  924. * monotonic timestamps since linux-4.15
  925. */
  926. e->event.vbl.tv_sec = tv.tv_sec;
  927. e->event.vbl.tv_usec = tv.tv_nsec / 1000;
  928. break;
  929. case DRM_EVENT_CRTC_SEQUENCE:
  930. if (seq)
  931. e->event.seq.sequence = seq;
  932. e->event.seq.time_ns = ktime_to_ns(now);
  933. break;
  934. }
  935. trace_drm_vblank_event_delivered(e->base.file_priv, e->pipe, seq);
  936. /*
  937. * Use the same timestamp for any associated fence signal to avoid
  938. * mismatch in timestamps for vsync & fence events triggered by the
  939. * same HW event. Frameworks like SurfaceFlinger in Android expects the
  940. * retire-fence timestamp to match exactly with HW vsync as it uses it
  941. * for its software vsync modeling.
  942. */
  943. drm_send_event_timestamp_locked(dev, &e->base, now);
  944. }
  945. /**
  946. * drm_crtc_arm_vblank_event - arm vblank event after pageflip
  947. * @crtc: the source CRTC of the vblank event
  948. * @e: the event to send
  949. *
  950. * A lot of drivers need to generate vblank events for the very next vblank
  951. * interrupt. For example when the page flip interrupt happens when the page
  952. * flip gets armed, but not when it actually executes within the next vblank
  953. * period. This helper function implements exactly the required vblank arming
  954. * behaviour.
  955. *
  956. * NOTE: Drivers using this to send out the &drm_crtc_state.event as part of an
  957. * atomic commit must ensure that the next vblank happens at exactly the same
  958. * time as the atomic commit is committed to the hardware. This function itself
  959. * does **not** protect against the next vblank interrupt racing with either this
  960. * function call or the atomic commit operation. A possible sequence could be:
  961. *
  962. * 1. Driver commits new hardware state into vblank-synchronized registers.
  963. * 2. A vblank happens, committing the hardware state. Also the corresponding
  964. * vblank interrupt is fired off and fully processed by the interrupt
  965. * handler.
  966. * 3. The atomic commit operation proceeds to call drm_crtc_arm_vblank_event().
  967. * 4. The event is only send out for the next vblank, which is wrong.
  968. *
  969. * An equivalent race can happen when the driver calls
  970. * drm_crtc_arm_vblank_event() before writing out the new hardware state.
  971. *
  972. * The only way to make this work safely is to prevent the vblank from firing
  973. * (and the hardware from committing anything else) until the entire atomic
  974. * commit sequence has run to completion. If the hardware does not have such a
  975. * feature (e.g. using a "go" bit), then it is unsafe to use this functions.
  976. * Instead drivers need to manually send out the event from their interrupt
  977. * handler by calling drm_crtc_send_vblank_event() and make sure that there's no
  978. * possible race with the hardware committing the atomic update.
  979. *
  980. * Caller must hold a vblank reference for the event @e acquired by a
  981. * drm_crtc_vblank_get(), which will be dropped when the next vblank arrives.
  982. */
  983. void drm_crtc_arm_vblank_event(struct drm_crtc *crtc,
  984. struct drm_pending_vblank_event *e)
  985. {
  986. struct drm_device *dev = crtc->dev;
  987. unsigned int pipe = drm_crtc_index(crtc);
  988. assert_spin_locked(&dev->event_lock);
  989. e->pipe = pipe;
  990. e->sequence = drm_crtc_accurate_vblank_count(crtc) + 1;
  991. list_add_tail(&e->base.link, &dev->vblank_event_list);
  992. }
  993. EXPORT_SYMBOL(drm_crtc_arm_vblank_event);
  994. /**
  995. * drm_crtc_send_vblank_event - helper to send vblank event after pageflip
  996. * @crtc: the source CRTC of the vblank event
  997. * @e: the event to send
  998. *
  999. * Updates sequence # and timestamp on event for the most recently processed
  1000. * vblank, and sends it to userspace. Caller must hold event lock.
  1001. *
  1002. * See drm_crtc_arm_vblank_event() for a helper which can be used in certain
  1003. * situation, especially to send out events for atomic commit operations.
  1004. */
  1005. void drm_crtc_send_vblank_event(struct drm_crtc *crtc,
  1006. struct drm_pending_vblank_event *e)
  1007. {
  1008. struct drm_device *dev = crtc->dev;
  1009. u64 seq;
  1010. unsigned int pipe = drm_crtc_index(crtc);
  1011. ktime_t now;
  1012. if (drm_dev_has_vblank(dev)) {
  1013. seq = drm_vblank_count_and_time(dev, pipe, &now);
  1014. } else {
  1015. seq = 0;
  1016. now = ktime_get();
  1017. }
  1018. e->pipe = pipe;
  1019. send_vblank_event(dev, e, seq, now);
  1020. }
  1021. EXPORT_SYMBOL(drm_crtc_send_vblank_event);
  1022. static int __enable_vblank(struct drm_device *dev, unsigned int pipe)
  1023. {
  1024. if (drm_core_check_feature(dev, DRIVER_MODESET)) {
  1025. struct drm_crtc *crtc = drm_crtc_from_index(dev, pipe);
  1026. if (drm_WARN_ON(dev, !crtc))
  1027. return 0;
  1028. if (crtc->funcs->enable_vblank)
  1029. return crtc->funcs->enable_vblank(crtc);
  1030. }
  1031. return -EINVAL;
  1032. }
  1033. static int drm_vblank_enable(struct drm_device *dev, unsigned int pipe)
  1034. {
  1035. struct drm_vblank_crtc *vblank = drm_vblank_crtc(dev, pipe);
  1036. int ret = 0;
  1037. assert_spin_locked(&dev->vbl_lock);
  1038. spin_lock(&dev->vblank_time_lock);
  1039. if (!vblank->enabled) {
  1040. /*
  1041. * Enable vblank irqs under vblank_time_lock protection.
  1042. * All vblank count & timestamp updates are held off
  1043. * until we are done reinitializing master counter and
  1044. * timestamps. Filtercode in drm_handle_vblank() will
  1045. * prevent double-accounting of same vblank interval.
  1046. */
  1047. ret = __enable_vblank(dev, pipe);
  1048. drm_dbg_core(dev, "enabling vblank on crtc %u, ret: %d\n",
  1049. pipe, ret);
  1050. if (ret) {
  1051. atomic_dec(&vblank->refcount);
  1052. } else {
  1053. drm_update_vblank_count(dev, pipe, 0);
  1054. /* drm_update_vblank_count() includes a wmb so we just
  1055. * need to ensure that the compiler emits the write
  1056. * to mark the vblank as enabled after the call
  1057. * to drm_update_vblank_count().
  1058. */
  1059. WRITE_ONCE(vblank->enabled, true);
  1060. }
  1061. }
  1062. spin_unlock(&dev->vblank_time_lock);
  1063. return ret;
  1064. }
  1065. int drm_vblank_get(struct drm_device *dev, unsigned int pipe)
  1066. {
  1067. struct drm_vblank_crtc *vblank = drm_vblank_crtc(dev, pipe);
  1068. unsigned long irqflags;
  1069. int ret = 0;
  1070. if (!drm_dev_has_vblank(dev))
  1071. return -EINVAL;
  1072. if (drm_WARN_ON(dev, pipe >= dev->num_crtcs))
  1073. return -EINVAL;
  1074. spin_lock_irqsave(&dev->vbl_lock, irqflags);
  1075. /* Going from 0->1 means we have to enable interrupts again */
  1076. if (atomic_add_return(1, &vblank->refcount) == 1) {
  1077. ret = drm_vblank_enable(dev, pipe);
  1078. } else {
  1079. if (!vblank->enabled) {
  1080. atomic_dec(&vblank->refcount);
  1081. ret = -EINVAL;
  1082. }
  1083. }
  1084. spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
  1085. return ret;
  1086. }
  1087. /**
  1088. * drm_crtc_vblank_get - get a reference count on vblank events
  1089. * @crtc: which CRTC to own
  1090. *
  1091. * Acquire a reference count on vblank events to avoid having them disabled
  1092. * while in use.
  1093. *
  1094. * Returns:
  1095. * Zero on success or a negative error code on failure.
  1096. */
  1097. int drm_crtc_vblank_get(struct drm_crtc *crtc)
  1098. {
  1099. return drm_vblank_get(crtc->dev, drm_crtc_index(crtc));
  1100. }
  1101. EXPORT_SYMBOL(drm_crtc_vblank_get);
  1102. void drm_vblank_put(struct drm_device *dev, unsigned int pipe)
  1103. {
  1104. struct drm_vblank_crtc *vblank = drm_vblank_crtc(dev, pipe);
  1105. int vblank_offdelay = vblank->config.offdelay_ms;
  1106. if (drm_WARN_ON(dev, pipe >= dev->num_crtcs))
  1107. return;
  1108. if (drm_WARN_ON(dev, atomic_read(&vblank->refcount) == 0))
  1109. return;
  1110. /* Last user schedules interrupt disable */
  1111. if (atomic_dec_and_test(&vblank->refcount)) {
  1112. if (!vblank_offdelay)
  1113. return;
  1114. else if (vblank_offdelay < 0)
  1115. vblank_disable_fn(&vblank->disable_timer);
  1116. else if (!vblank->config.disable_immediate)
  1117. mod_timer(&vblank->disable_timer,
  1118. jiffies + ((vblank_offdelay * HZ) / 1000));
  1119. }
  1120. }
  1121. /**
  1122. * drm_crtc_vblank_put - give up ownership of vblank events
  1123. * @crtc: which counter to give up
  1124. *
  1125. * Release ownership of a given vblank counter, turning off interrupts
  1126. * if possible. Disable interrupts after &drm_vblank_crtc_config.offdelay_ms
  1127. * milliseconds.
  1128. */
  1129. void drm_crtc_vblank_put(struct drm_crtc *crtc)
  1130. {
  1131. drm_vblank_put(crtc->dev, drm_crtc_index(crtc));
  1132. }
  1133. EXPORT_SYMBOL(drm_crtc_vblank_put);
  1134. /**
  1135. * drm_crtc_wait_one_vblank - wait for one vblank
  1136. * @crtc: DRM crtc
  1137. *
  1138. * This waits for one vblank to pass on @crtc, using the irq driver interfaces.
  1139. * It is a failure to call this when the vblank irq for @crtc is disabled, e.g.
  1140. * due to lack of driver support or because the crtc is off.
  1141. *
  1142. * Returns: 0 on success, negative error on failures.
  1143. */
  1144. int drm_crtc_wait_one_vblank(struct drm_crtc *crtc)
  1145. {
  1146. struct drm_device *dev = crtc->dev;
  1147. int pipe = drm_crtc_index(crtc);
  1148. struct drm_vblank_crtc *vblank = drm_crtc_vblank_crtc(crtc);
  1149. int ret;
  1150. u64 last;
  1151. ret = drm_vblank_get(dev, pipe);
  1152. if (drm_WARN(dev, ret, "vblank not available on crtc %i, ret=%i\n",
  1153. pipe, ret))
  1154. return ret;
  1155. last = drm_vblank_count(dev, pipe);
  1156. ret = wait_event_timeout(vblank->queue,
  1157. last != drm_vblank_count(dev, pipe),
  1158. msecs_to_jiffies(1000));
  1159. drm_WARN(dev, ret == 0, "vblank wait timed out on crtc %i\n", pipe);
  1160. drm_vblank_put(dev, pipe);
  1161. return ret ? 0 : -ETIMEDOUT;
  1162. }
  1163. EXPORT_SYMBOL(drm_crtc_wait_one_vblank);
  1164. /**
  1165. * drm_crtc_vblank_off - disable vblank events on a CRTC
  1166. * @crtc: CRTC in question
  1167. *
  1168. * Drivers can use this function to shut down the vblank interrupt handling when
  1169. * disabling a crtc. This function ensures that the latest vblank frame count is
  1170. * stored so that drm_vblank_on can restore it again.
  1171. *
  1172. * Drivers must use this function when the hardware vblank counter can get
  1173. * reset, e.g. when suspending or disabling the @crtc in general.
  1174. */
  1175. void drm_crtc_vblank_off(struct drm_crtc *crtc)
  1176. {
  1177. struct drm_device *dev = crtc->dev;
  1178. unsigned int pipe = drm_crtc_index(crtc);
  1179. struct drm_vblank_crtc *vblank = drm_crtc_vblank_crtc(crtc);
  1180. struct drm_pending_vblank_event *e, *t;
  1181. ktime_t now;
  1182. u64 seq;
  1183. if (drm_WARN_ON(dev, pipe >= dev->num_crtcs))
  1184. return;
  1185. /*
  1186. * Grab event_lock early to prevent vblank work from being scheduled
  1187. * while we're in the middle of shutting down vblank interrupts
  1188. */
  1189. spin_lock_irq(&dev->event_lock);
  1190. spin_lock(&dev->vbl_lock);
  1191. drm_dbg_vbl(dev, "crtc %d, vblank enabled %d, inmodeset %d\n",
  1192. pipe, vblank->enabled, vblank->inmodeset);
  1193. /* Avoid redundant vblank disables without previous
  1194. * drm_crtc_vblank_on(). */
  1195. if (drm_core_check_feature(dev, DRIVER_ATOMIC) || !vblank->inmodeset)
  1196. drm_vblank_disable_and_save(dev, pipe);
  1197. wake_up(&vblank->queue);
  1198. /*
  1199. * Prevent subsequent drm_vblank_get() from re-enabling
  1200. * the vblank interrupt by bumping the refcount.
  1201. */
  1202. if (!vblank->inmodeset) {
  1203. atomic_inc(&vblank->refcount);
  1204. vblank->inmodeset = 1;
  1205. }
  1206. spin_unlock(&dev->vbl_lock);
  1207. /* Send any queued vblank events, lest the natives grow disquiet */
  1208. seq = drm_vblank_count_and_time(dev, pipe, &now);
  1209. list_for_each_entry_safe(e, t, &dev->vblank_event_list, base.link) {
  1210. if (e->pipe != pipe)
  1211. continue;
  1212. drm_dbg_core(dev, "Sending premature vblank event on disable: "
  1213. "wanted %llu, current %llu\n",
  1214. e->sequence, seq);
  1215. list_del(&e->base.link);
  1216. drm_vblank_put(dev, pipe);
  1217. send_vblank_event(dev, e, seq, now);
  1218. }
  1219. /* Cancel any leftover pending vblank work */
  1220. drm_vblank_cancel_pending_works(vblank);
  1221. spin_unlock_irq(&dev->event_lock);
  1222. /* Will be reset by the modeset helpers when re-enabling the crtc by
  1223. * calling drm_calc_timestamping_constants(). */
  1224. vblank->hwmode.crtc_clock = 0;
  1225. /* Wait for any vblank work that's still executing to finish */
  1226. drm_vblank_flush_worker(vblank);
  1227. }
  1228. EXPORT_SYMBOL(drm_crtc_vblank_off);
  1229. /**
  1230. * drm_crtc_vblank_reset - reset vblank state to off on a CRTC
  1231. * @crtc: CRTC in question
  1232. *
  1233. * Drivers can use this function to reset the vblank state to off at load time.
  1234. * Drivers should use this together with the drm_crtc_vblank_off() and
  1235. * drm_crtc_vblank_on() functions. The difference compared to
  1236. * drm_crtc_vblank_off() is that this function doesn't save the vblank counter
  1237. * and hence doesn't need to call any driver hooks.
  1238. *
  1239. * This is useful for recovering driver state e.g. on driver load, or on resume.
  1240. */
  1241. void drm_crtc_vblank_reset(struct drm_crtc *crtc)
  1242. {
  1243. struct drm_device *dev = crtc->dev;
  1244. struct drm_vblank_crtc *vblank = drm_crtc_vblank_crtc(crtc);
  1245. spin_lock_irq(&dev->vbl_lock);
  1246. /*
  1247. * Prevent subsequent drm_vblank_get() from enabling the vblank
  1248. * interrupt by bumping the refcount.
  1249. */
  1250. if (!vblank->inmodeset) {
  1251. atomic_inc(&vblank->refcount);
  1252. vblank->inmodeset = 1;
  1253. }
  1254. spin_unlock_irq(&dev->vbl_lock);
  1255. drm_WARN_ON(dev, !list_empty(&dev->vblank_event_list));
  1256. drm_WARN_ON(dev, !list_empty(&vblank->pending_work));
  1257. }
  1258. EXPORT_SYMBOL(drm_crtc_vblank_reset);
  1259. /**
  1260. * drm_crtc_set_max_vblank_count - configure the hw max vblank counter value
  1261. * @crtc: CRTC in question
  1262. * @max_vblank_count: max hardware vblank counter value
  1263. *
  1264. * Update the maximum hardware vblank counter value for @crtc
  1265. * at runtime. Useful for hardware where the operation of the
  1266. * hardware vblank counter depends on the currently active
  1267. * display configuration.
  1268. *
  1269. * For example, if the hardware vblank counter does not work
  1270. * when a specific connector is active the maximum can be set
  1271. * to zero. And when that specific connector isn't active the
  1272. * maximum can again be set to the appropriate non-zero value.
  1273. *
  1274. * If used, must be called before drm_vblank_on().
  1275. */
  1276. void drm_crtc_set_max_vblank_count(struct drm_crtc *crtc,
  1277. u32 max_vblank_count)
  1278. {
  1279. struct drm_device *dev = crtc->dev;
  1280. struct drm_vblank_crtc *vblank = drm_crtc_vblank_crtc(crtc);
  1281. drm_WARN_ON(dev, dev->max_vblank_count);
  1282. drm_WARN_ON(dev, !READ_ONCE(vblank->inmodeset));
  1283. vblank->max_vblank_count = max_vblank_count;
  1284. }
  1285. EXPORT_SYMBOL(drm_crtc_set_max_vblank_count);
  1286. /**
  1287. * drm_crtc_vblank_on_config - enable vblank events on a CRTC with custom
  1288. * configuration options
  1289. * @crtc: CRTC in question
  1290. * @config: Vblank configuration value
  1291. *
  1292. * See drm_crtc_vblank_on(). In addition, this function allows you to provide a
  1293. * custom vblank configuration for a given CRTC.
  1294. *
  1295. * Note that @config is copied, the pointer does not need to stay valid beyond
  1296. * this function call. For details of the parameters see
  1297. * struct drm_vblank_crtc_config.
  1298. */
  1299. void drm_crtc_vblank_on_config(struct drm_crtc *crtc,
  1300. const struct drm_vblank_crtc_config *config)
  1301. {
  1302. struct drm_device *dev = crtc->dev;
  1303. unsigned int pipe = drm_crtc_index(crtc);
  1304. struct drm_vblank_crtc *vblank = drm_crtc_vblank_crtc(crtc);
  1305. if (drm_WARN_ON(dev, pipe >= dev->num_crtcs))
  1306. return;
  1307. spin_lock_irq(&dev->vbl_lock);
  1308. drm_dbg_vbl(dev, "crtc %d, vblank enabled %d, inmodeset %d\n",
  1309. pipe, vblank->enabled, vblank->inmodeset);
  1310. vblank->config = *config;
  1311. /* Drop our private "prevent drm_vblank_get" refcount */
  1312. if (vblank->inmodeset) {
  1313. atomic_dec(&vblank->refcount);
  1314. vblank->inmodeset = 0;
  1315. }
  1316. drm_reset_vblank_timestamp(dev, pipe);
  1317. /*
  1318. * re-enable interrupts if there are users left, or the
  1319. * user wishes vblank interrupts to be enabled all the time.
  1320. */
  1321. if (atomic_read(&vblank->refcount) != 0 || !vblank->config.offdelay_ms)
  1322. drm_WARN_ON(dev, drm_vblank_enable(dev, pipe));
  1323. spin_unlock_irq(&dev->vbl_lock);
  1324. }
  1325. EXPORT_SYMBOL(drm_crtc_vblank_on_config);
  1326. /**
  1327. * drm_crtc_vblank_on - enable vblank events on a CRTC
  1328. * @crtc: CRTC in question
  1329. *
  1330. * This functions restores the vblank interrupt state captured with
  1331. * drm_crtc_vblank_off() again and is generally called when enabling @crtc. Note
  1332. * that calls to drm_crtc_vblank_on() and drm_crtc_vblank_off() can be
  1333. * unbalanced and so can also be unconditionally called in driver load code to
  1334. * reflect the current hardware state of the crtc.
  1335. *
  1336. * Note that unlike in drm_crtc_vblank_on_config(), default values are used.
  1337. */
  1338. void drm_crtc_vblank_on(struct drm_crtc *crtc)
  1339. {
  1340. const struct drm_vblank_crtc_config config = {
  1341. .offdelay_ms = drm_vblank_offdelay,
  1342. .disable_immediate = crtc->dev->vblank_disable_immediate
  1343. };
  1344. drm_crtc_vblank_on_config(crtc, &config);
  1345. }
  1346. EXPORT_SYMBOL(drm_crtc_vblank_on);
  1347. static void drm_vblank_restore(struct drm_device *dev, unsigned int pipe)
  1348. {
  1349. ktime_t t_vblank;
  1350. struct drm_vblank_crtc *vblank;
  1351. int framedur_ns;
  1352. u64 diff_ns;
  1353. u32 cur_vblank, diff = 1;
  1354. int count = DRM_TIMESTAMP_MAXRETRIES;
  1355. u32 max_vblank_count = drm_max_vblank_count(dev, pipe);
  1356. if (drm_WARN_ON(dev, pipe >= dev->num_crtcs))
  1357. return;
  1358. assert_spin_locked(&dev->vbl_lock);
  1359. assert_spin_locked(&dev->vblank_time_lock);
  1360. vblank = drm_vblank_crtc(dev, pipe);
  1361. drm_WARN_ONCE(dev,
  1362. drm_debug_enabled(DRM_UT_VBL) && !vblank->framedur_ns,
  1363. "Cannot compute missed vblanks without frame duration\n");
  1364. framedur_ns = vblank->framedur_ns;
  1365. do {
  1366. cur_vblank = __get_vblank_counter(dev, pipe);
  1367. drm_get_last_vbltimestamp(dev, pipe, &t_vblank, false);
  1368. } while (cur_vblank != __get_vblank_counter(dev, pipe) && --count > 0);
  1369. diff_ns = ktime_to_ns(ktime_sub(t_vblank, vblank->time));
  1370. if (framedur_ns)
  1371. diff = DIV_ROUND_CLOSEST_ULL(diff_ns, framedur_ns);
  1372. drm_dbg_vbl(dev,
  1373. "missed %d vblanks in %lld ns, frame duration=%d ns, hw_diff=%d\n",
  1374. diff, diff_ns, framedur_ns, cur_vblank - vblank->last);
  1375. vblank->last = (cur_vblank - diff) & max_vblank_count;
  1376. }
  1377. /**
  1378. * drm_crtc_vblank_restore - estimate missed vblanks and update vblank count.
  1379. * @crtc: CRTC in question
  1380. *
  1381. * Power manamement features can cause frame counter resets between vblank
  1382. * disable and enable. Drivers can use this function in their
  1383. * &drm_crtc_funcs.enable_vblank implementation to estimate missed vblanks since
  1384. * the last &drm_crtc_funcs.disable_vblank using timestamps and update the
  1385. * vblank counter.
  1386. *
  1387. * Note that drivers must have race-free high-precision timestamping support,
  1388. * i.e. &drm_crtc_funcs.get_vblank_timestamp must be hooked up and
  1389. * &drm_vblank_crtc_config.disable_immediate must be set to indicate the
  1390. * time-stamping functions are race-free against vblank hardware counter
  1391. * increments.
  1392. */
  1393. void drm_crtc_vblank_restore(struct drm_crtc *crtc)
  1394. {
  1395. struct drm_device *dev = crtc->dev;
  1396. unsigned int pipe = drm_crtc_index(crtc);
  1397. struct drm_vblank_crtc *vblank = drm_vblank_crtc(dev, pipe);
  1398. drm_WARN_ON_ONCE(dev, !crtc->funcs->get_vblank_timestamp);
  1399. drm_WARN_ON_ONCE(dev, vblank->inmodeset);
  1400. drm_WARN_ON_ONCE(dev, !vblank->config.disable_immediate);
  1401. drm_vblank_restore(dev, pipe);
  1402. }
  1403. EXPORT_SYMBOL(drm_crtc_vblank_restore);
  1404. static int drm_queue_vblank_event(struct drm_device *dev, unsigned int pipe,
  1405. u64 req_seq,
  1406. union drm_wait_vblank *vblwait,
  1407. struct drm_file *file_priv)
  1408. {
  1409. struct drm_vblank_crtc *vblank = drm_vblank_crtc(dev, pipe);
  1410. struct drm_pending_vblank_event *e;
  1411. ktime_t now;
  1412. u64 seq;
  1413. int ret;
  1414. e = kzalloc_obj(*e);
  1415. if (e == NULL) {
  1416. ret = -ENOMEM;
  1417. goto err_put;
  1418. }
  1419. e->pipe = pipe;
  1420. e->event.base.type = DRM_EVENT_VBLANK;
  1421. e->event.base.length = sizeof(e->event.vbl);
  1422. e->event.vbl.user_data = vblwait->request.signal;
  1423. e->event.vbl.crtc_id = 0;
  1424. if (drm_core_check_feature(dev, DRIVER_MODESET)) {
  1425. struct drm_crtc *crtc = drm_crtc_from_index(dev, pipe);
  1426. if (crtc)
  1427. e->event.vbl.crtc_id = crtc->base.id;
  1428. }
  1429. spin_lock_irq(&dev->event_lock);
  1430. /*
  1431. * drm_crtc_vblank_off() might have been called after we called
  1432. * drm_vblank_get(). drm_crtc_vblank_off() holds event_lock around the
  1433. * vblank disable, so no need for further locking. The reference from
  1434. * drm_vblank_get() protects against vblank disable from another source.
  1435. */
  1436. if (!READ_ONCE(vblank->enabled)) {
  1437. ret = -EINVAL;
  1438. goto err_unlock;
  1439. }
  1440. ret = drm_event_reserve_init_locked(dev, file_priv, &e->base,
  1441. &e->event.base);
  1442. if (ret)
  1443. goto err_unlock;
  1444. seq = drm_vblank_count_and_time(dev, pipe, &now);
  1445. drm_dbg_core(dev, "event on vblank count %llu, current %llu, crtc %u\n",
  1446. req_seq, seq, pipe);
  1447. trace_drm_vblank_event_queued(file_priv, pipe, req_seq);
  1448. e->sequence = req_seq;
  1449. if (drm_vblank_passed(seq, req_seq)) {
  1450. drm_vblank_put(dev, pipe);
  1451. send_vblank_event(dev, e, seq, now);
  1452. vblwait->reply.sequence = seq;
  1453. } else {
  1454. /* drm_handle_vblank_events will call drm_vblank_put */
  1455. list_add_tail(&e->base.link, &dev->vblank_event_list);
  1456. vblwait->reply.sequence = req_seq;
  1457. }
  1458. spin_unlock_irq(&dev->event_lock);
  1459. return 0;
  1460. err_unlock:
  1461. spin_unlock_irq(&dev->event_lock);
  1462. kfree(e);
  1463. err_put:
  1464. drm_vblank_put(dev, pipe);
  1465. return ret;
  1466. }
  1467. static bool drm_wait_vblank_is_query(union drm_wait_vblank *vblwait)
  1468. {
  1469. if (vblwait->request.sequence)
  1470. return false;
  1471. return _DRM_VBLANK_RELATIVE ==
  1472. (vblwait->request.type & (_DRM_VBLANK_TYPES_MASK |
  1473. _DRM_VBLANK_EVENT |
  1474. _DRM_VBLANK_NEXTONMISS));
  1475. }
  1476. /*
  1477. * Widen a 32-bit param to 64-bits.
  1478. *
  1479. * \param narrow 32-bit value (missing upper 32 bits)
  1480. * \param near 64-bit value that should be 'close' to near
  1481. *
  1482. * This function returns a 64-bit value using the lower 32-bits from
  1483. * 'narrow' and constructing the upper 32-bits so that the result is
  1484. * as close as possible to 'near'.
  1485. */
  1486. static u64 widen_32_to_64(u32 narrow, u64 near)
  1487. {
  1488. return near + (s32) (narrow - near);
  1489. }
  1490. static void drm_wait_vblank_reply(struct drm_device *dev, unsigned int pipe,
  1491. struct drm_wait_vblank_reply *reply)
  1492. {
  1493. ktime_t now;
  1494. struct timespec64 ts;
  1495. /*
  1496. * drm_wait_vblank_reply is a UAPI structure that uses 'long'
  1497. * to store the seconds. This is safe as we always use monotonic
  1498. * timestamps since linux-4.15.
  1499. */
  1500. reply->sequence = drm_vblank_count_and_time(dev, pipe, &now);
  1501. ts = ktime_to_timespec64(now);
  1502. reply->tval_sec = (u32)ts.tv_sec;
  1503. reply->tval_usec = ts.tv_nsec / 1000;
  1504. }
  1505. static bool drm_wait_vblank_supported(struct drm_device *dev)
  1506. {
  1507. return drm_dev_has_vblank(dev);
  1508. }
  1509. int drm_wait_vblank_ioctl(struct drm_device *dev, void *data,
  1510. struct drm_file *file_priv)
  1511. {
  1512. struct drm_crtc *crtc;
  1513. struct drm_vblank_crtc *vblank;
  1514. union drm_wait_vblank *vblwait = data;
  1515. int ret;
  1516. u64 req_seq, seq;
  1517. unsigned int pipe_index;
  1518. unsigned int flags, pipe, high_pipe;
  1519. if (!drm_wait_vblank_supported(dev))
  1520. return -EOPNOTSUPP;
  1521. if (vblwait->request.type & _DRM_VBLANK_SIGNAL)
  1522. return -EINVAL;
  1523. if (vblwait->request.type &
  1524. ~(_DRM_VBLANK_TYPES_MASK | _DRM_VBLANK_FLAGS_MASK |
  1525. _DRM_VBLANK_HIGH_CRTC_MASK)) {
  1526. drm_dbg_core(dev,
  1527. "Unsupported type value 0x%x, supported mask 0x%x\n",
  1528. vblwait->request.type,
  1529. (_DRM_VBLANK_TYPES_MASK | _DRM_VBLANK_FLAGS_MASK |
  1530. _DRM_VBLANK_HIGH_CRTC_MASK));
  1531. return -EINVAL;
  1532. }
  1533. flags = vblwait->request.type & _DRM_VBLANK_FLAGS_MASK;
  1534. high_pipe = (vblwait->request.type & _DRM_VBLANK_HIGH_CRTC_MASK);
  1535. if (high_pipe)
  1536. pipe_index = high_pipe >> _DRM_VBLANK_HIGH_CRTC_SHIFT;
  1537. else
  1538. pipe_index = flags & _DRM_VBLANK_SECONDARY ? 1 : 0;
  1539. /* Convert lease-relative crtc index into global crtc index */
  1540. if (drm_core_check_feature(dev, DRIVER_MODESET)) {
  1541. pipe = 0;
  1542. drm_for_each_crtc(crtc, dev) {
  1543. if (drm_lease_held(file_priv, crtc->base.id)) {
  1544. if (pipe_index == 0)
  1545. break;
  1546. pipe_index--;
  1547. }
  1548. pipe++;
  1549. }
  1550. } else {
  1551. pipe = pipe_index;
  1552. }
  1553. if (pipe >= dev->num_crtcs)
  1554. return -EINVAL;
  1555. vblank = drm_vblank_crtc(dev, pipe);
  1556. /* If the counter is currently enabled and accurate, short-circuit
  1557. * queries to return the cached timestamp of the last vblank.
  1558. */
  1559. if (vblank->config.disable_immediate &&
  1560. drm_wait_vblank_is_query(vblwait) &&
  1561. READ_ONCE(vblank->enabled)) {
  1562. drm_wait_vblank_reply(dev, pipe, &vblwait->reply);
  1563. return 0;
  1564. }
  1565. ret = drm_vblank_get(dev, pipe);
  1566. if (ret) {
  1567. drm_dbg_core(dev,
  1568. "crtc %d failed to acquire vblank counter, %d\n",
  1569. pipe, ret);
  1570. return ret;
  1571. }
  1572. seq = drm_vblank_count(dev, pipe);
  1573. switch (vblwait->request.type & _DRM_VBLANK_TYPES_MASK) {
  1574. case _DRM_VBLANK_RELATIVE:
  1575. req_seq = seq + vblwait->request.sequence;
  1576. vblwait->request.sequence = req_seq;
  1577. vblwait->request.type &= ~_DRM_VBLANK_RELATIVE;
  1578. break;
  1579. case _DRM_VBLANK_ABSOLUTE:
  1580. req_seq = widen_32_to_64(vblwait->request.sequence, seq);
  1581. break;
  1582. default:
  1583. ret = -EINVAL;
  1584. goto done;
  1585. }
  1586. if ((flags & _DRM_VBLANK_NEXTONMISS) &&
  1587. drm_vblank_passed(seq, req_seq)) {
  1588. req_seq = seq + 1;
  1589. vblwait->request.type &= ~_DRM_VBLANK_NEXTONMISS;
  1590. vblwait->request.sequence = req_seq;
  1591. }
  1592. if (flags & _DRM_VBLANK_EVENT) {
  1593. /* must hold on to the vblank ref until the event fires
  1594. * drm_vblank_put will be called asynchronously
  1595. */
  1596. return drm_queue_vblank_event(dev, pipe, req_seq, vblwait, file_priv);
  1597. }
  1598. if (req_seq != seq) {
  1599. int wait;
  1600. drm_dbg_core(dev, "waiting on vblank count %llu, crtc %u\n",
  1601. req_seq, pipe);
  1602. wait = wait_event_interruptible_timeout(vblank->queue,
  1603. drm_vblank_passed(drm_vblank_count(dev, pipe), req_seq) ||
  1604. !READ_ONCE(vblank->enabled),
  1605. msecs_to_jiffies(3000));
  1606. switch (wait) {
  1607. case 0:
  1608. /* timeout */
  1609. ret = -EBUSY;
  1610. break;
  1611. case -ERESTARTSYS:
  1612. /* interrupted by signal */
  1613. ret = -EINTR;
  1614. break;
  1615. default:
  1616. ret = 0;
  1617. break;
  1618. }
  1619. }
  1620. if (ret != -EINTR) {
  1621. drm_wait_vblank_reply(dev, pipe, &vblwait->reply);
  1622. drm_dbg_core(dev, "crtc %d returning %u to client\n",
  1623. pipe, vblwait->reply.sequence);
  1624. } else {
  1625. drm_dbg_core(dev, "crtc %d vblank wait interrupted by signal\n",
  1626. pipe);
  1627. }
  1628. done:
  1629. drm_vblank_put(dev, pipe);
  1630. return ret;
  1631. }
  1632. static void drm_handle_vblank_events(struct drm_device *dev, unsigned int pipe)
  1633. {
  1634. struct drm_crtc *crtc = drm_crtc_from_index(dev, pipe);
  1635. bool high_prec = false;
  1636. struct drm_pending_vblank_event *e, *t;
  1637. ktime_t now;
  1638. u64 seq;
  1639. assert_spin_locked(&dev->event_lock);
  1640. seq = drm_vblank_count_and_time(dev, pipe, &now);
  1641. list_for_each_entry_safe(e, t, &dev->vblank_event_list, base.link) {
  1642. if (e->pipe != pipe)
  1643. continue;
  1644. if (!drm_vblank_passed(seq, e->sequence))
  1645. continue;
  1646. drm_dbg_core(dev, "vblank event on %llu, current %llu\n",
  1647. e->sequence, seq);
  1648. list_del(&e->base.link);
  1649. drm_vblank_put(dev, pipe);
  1650. send_vblank_event(dev, e, seq, now);
  1651. }
  1652. if (crtc && crtc->funcs->get_vblank_timestamp)
  1653. high_prec = true;
  1654. trace_drm_vblank_event(pipe, seq, now, high_prec);
  1655. }
  1656. /**
  1657. * drm_handle_vblank - handle a vblank event
  1658. * @dev: DRM device
  1659. * @pipe: index of CRTC where this event occurred
  1660. *
  1661. * Drivers should call this routine in their vblank interrupt handlers to
  1662. * update the vblank counter and send any signals that may be pending.
  1663. *
  1664. * This is the legacy version of drm_crtc_handle_vblank().
  1665. */
  1666. bool drm_handle_vblank(struct drm_device *dev, unsigned int pipe)
  1667. {
  1668. struct drm_vblank_crtc *vblank = drm_vblank_crtc(dev, pipe);
  1669. unsigned long irqflags;
  1670. bool disable_irq;
  1671. if (drm_WARN_ON_ONCE(dev, !drm_dev_has_vblank(dev)))
  1672. return false;
  1673. if (drm_WARN_ON(dev, pipe >= dev->num_crtcs))
  1674. return false;
  1675. spin_lock_irqsave(&dev->event_lock, irqflags);
  1676. /* Need timestamp lock to prevent concurrent execution with
  1677. * vblank enable/disable, as this would cause inconsistent
  1678. * or corrupted timestamps and vblank counts.
  1679. */
  1680. spin_lock(&dev->vblank_time_lock);
  1681. /* Vblank irq handling disabled. Nothing to do. */
  1682. if (!vblank->enabled) {
  1683. spin_unlock(&dev->vblank_time_lock);
  1684. spin_unlock_irqrestore(&dev->event_lock, irqflags);
  1685. return false;
  1686. }
  1687. drm_update_vblank_count(dev, pipe, true);
  1688. spin_unlock(&dev->vblank_time_lock);
  1689. wake_up(&vblank->queue);
  1690. /* With instant-off, we defer disabling the interrupt until after
  1691. * we finish processing the following vblank after all events have
  1692. * been signaled. The disable has to be last (after
  1693. * drm_handle_vblank_events) so that the timestamp is always accurate.
  1694. */
  1695. disable_irq = (vblank->config.disable_immediate &&
  1696. vblank->config.offdelay_ms > 0 &&
  1697. !atomic_read(&vblank->refcount));
  1698. drm_handle_vblank_events(dev, pipe);
  1699. drm_handle_vblank_works(vblank);
  1700. spin_unlock_irqrestore(&dev->event_lock, irqflags);
  1701. if (disable_irq)
  1702. vblank_disable_fn(&vblank->disable_timer);
  1703. return true;
  1704. }
  1705. EXPORT_SYMBOL(drm_handle_vblank);
  1706. /**
  1707. * drm_crtc_handle_vblank - handle a vblank event
  1708. * @crtc: where this event occurred
  1709. *
  1710. * Drivers should call this routine in their vblank interrupt handlers to
  1711. * update the vblank counter and send any signals that may be pending.
  1712. *
  1713. * This is the native KMS version of drm_handle_vblank().
  1714. *
  1715. * Note that for a given vblank counter value drm_crtc_handle_vblank()
  1716. * and drm_crtc_vblank_count() or drm_crtc_vblank_count_and_time()
  1717. * provide a barrier: Any writes done before calling
  1718. * drm_crtc_handle_vblank() will be visible to callers of the later
  1719. * functions, if the vblank count is the same or a later one.
  1720. *
  1721. * See also &drm_vblank_crtc.count.
  1722. *
  1723. * Returns:
  1724. * True if the event was successfully handled, false on failure.
  1725. */
  1726. bool drm_crtc_handle_vblank(struct drm_crtc *crtc)
  1727. {
  1728. return drm_handle_vblank(crtc->dev, drm_crtc_index(crtc));
  1729. }
  1730. EXPORT_SYMBOL(drm_crtc_handle_vblank);
  1731. /*
  1732. * Get crtc VBLANK count.
  1733. *
  1734. * \param dev DRM device
  1735. * \param data user argument, pointing to a drm_crtc_get_sequence structure.
  1736. * \param file_priv drm file private for the user's open file descriptor
  1737. */
  1738. int drm_crtc_get_sequence_ioctl(struct drm_device *dev, void *data,
  1739. struct drm_file *file_priv)
  1740. {
  1741. struct drm_crtc *crtc;
  1742. struct drm_vblank_crtc *vblank;
  1743. int pipe;
  1744. struct drm_crtc_get_sequence *get_seq = data;
  1745. ktime_t now;
  1746. bool vblank_enabled;
  1747. int ret;
  1748. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  1749. return -EOPNOTSUPP;
  1750. if (!drm_dev_has_vblank(dev))
  1751. return -EOPNOTSUPP;
  1752. crtc = drm_crtc_find(dev, file_priv, get_seq->crtc_id);
  1753. if (!crtc)
  1754. return -ENOENT;
  1755. pipe = drm_crtc_index(crtc);
  1756. vblank = drm_crtc_vblank_crtc(crtc);
  1757. vblank_enabled = READ_ONCE(vblank->config.disable_immediate) &&
  1758. READ_ONCE(vblank->enabled);
  1759. if (!vblank_enabled) {
  1760. ret = drm_crtc_vblank_get(crtc);
  1761. if (ret) {
  1762. drm_dbg_core(dev,
  1763. "crtc %d failed to acquire vblank counter, %d\n",
  1764. pipe, ret);
  1765. return ret;
  1766. }
  1767. }
  1768. drm_modeset_lock(&crtc->mutex, NULL);
  1769. if (crtc->state)
  1770. get_seq->active = crtc->state->enable;
  1771. else
  1772. get_seq->active = crtc->enabled;
  1773. drm_modeset_unlock(&crtc->mutex);
  1774. get_seq->sequence = drm_vblank_count_and_time(dev, pipe, &now);
  1775. get_seq->sequence_ns = ktime_to_ns(now);
  1776. if (!vblank_enabled)
  1777. drm_crtc_vblank_put(crtc);
  1778. return 0;
  1779. }
  1780. /*
  1781. * Queue a event for VBLANK sequence
  1782. *
  1783. * \param dev DRM device
  1784. * \param data user argument, pointing to a drm_crtc_queue_sequence structure.
  1785. * \param file_priv drm file private for the user's open file descriptor
  1786. */
  1787. int drm_crtc_queue_sequence_ioctl(struct drm_device *dev, void *data,
  1788. struct drm_file *file_priv)
  1789. {
  1790. struct drm_crtc *crtc;
  1791. struct drm_vblank_crtc *vblank;
  1792. int pipe;
  1793. struct drm_crtc_queue_sequence *queue_seq = data;
  1794. ktime_t now;
  1795. struct drm_pending_vblank_event *e;
  1796. u32 flags;
  1797. u64 seq;
  1798. u64 req_seq;
  1799. int ret;
  1800. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  1801. return -EOPNOTSUPP;
  1802. if (!drm_dev_has_vblank(dev))
  1803. return -EOPNOTSUPP;
  1804. crtc = drm_crtc_find(dev, file_priv, queue_seq->crtc_id);
  1805. if (!crtc)
  1806. return -ENOENT;
  1807. flags = queue_seq->flags;
  1808. /* Check valid flag bits */
  1809. if (flags & ~(DRM_CRTC_SEQUENCE_RELATIVE|
  1810. DRM_CRTC_SEQUENCE_NEXT_ON_MISS))
  1811. return -EINVAL;
  1812. pipe = drm_crtc_index(crtc);
  1813. vblank = drm_crtc_vblank_crtc(crtc);
  1814. e = kzalloc_obj(*e);
  1815. if (e == NULL)
  1816. return -ENOMEM;
  1817. ret = drm_crtc_vblank_get(crtc);
  1818. if (ret) {
  1819. drm_dbg_core(dev,
  1820. "crtc %d failed to acquire vblank counter, %d\n",
  1821. pipe, ret);
  1822. goto err_free;
  1823. }
  1824. seq = drm_vblank_count_and_time(dev, pipe, &now);
  1825. req_seq = queue_seq->sequence;
  1826. if (flags & DRM_CRTC_SEQUENCE_RELATIVE)
  1827. req_seq += seq;
  1828. if ((flags & DRM_CRTC_SEQUENCE_NEXT_ON_MISS) && drm_vblank_passed(seq, req_seq))
  1829. req_seq = seq + 1;
  1830. e->pipe = pipe;
  1831. e->event.base.type = DRM_EVENT_CRTC_SEQUENCE;
  1832. e->event.base.length = sizeof(e->event.seq);
  1833. e->event.seq.user_data = queue_seq->user_data;
  1834. spin_lock_irq(&dev->event_lock);
  1835. /*
  1836. * drm_crtc_vblank_off() might have been called after we called
  1837. * drm_crtc_vblank_get(). drm_crtc_vblank_off() holds event_lock around the
  1838. * vblank disable, so no need for further locking. The reference from
  1839. * drm_crtc_vblank_get() protects against vblank disable from another source.
  1840. */
  1841. if (!READ_ONCE(vblank->enabled)) {
  1842. ret = -EINVAL;
  1843. goto err_unlock;
  1844. }
  1845. ret = drm_event_reserve_init_locked(dev, file_priv, &e->base,
  1846. &e->event.base);
  1847. if (ret)
  1848. goto err_unlock;
  1849. e->sequence = req_seq;
  1850. if (drm_vblank_passed(seq, req_seq)) {
  1851. drm_crtc_vblank_put(crtc);
  1852. send_vblank_event(dev, e, seq, now);
  1853. queue_seq->sequence = seq;
  1854. } else {
  1855. /* drm_handle_vblank_events will call drm_vblank_put */
  1856. list_add_tail(&e->base.link, &dev->vblank_event_list);
  1857. queue_seq->sequence = req_seq;
  1858. }
  1859. spin_unlock_irq(&dev->event_lock);
  1860. return 0;
  1861. err_unlock:
  1862. spin_unlock_irq(&dev->event_lock);
  1863. drm_crtc_vblank_put(crtc);
  1864. err_free:
  1865. kfree(e);
  1866. return ret;
  1867. }
  1868. /*
  1869. * VBLANK timer
  1870. */
  1871. static enum hrtimer_restart drm_vblank_timer_function(struct hrtimer *timer)
  1872. {
  1873. struct drm_vblank_crtc_timer *vtimer =
  1874. container_of(timer, struct drm_vblank_crtc_timer, timer);
  1875. struct drm_crtc *crtc = vtimer->crtc;
  1876. const struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  1877. struct drm_device *dev = crtc->dev;
  1878. unsigned long flags;
  1879. ktime_t interval;
  1880. u64 ret_overrun;
  1881. bool succ;
  1882. spin_lock_irqsave(&vtimer->interval_lock, flags);
  1883. interval = vtimer->interval;
  1884. spin_unlock_irqrestore(&vtimer->interval_lock, flags);
  1885. if (!interval)
  1886. return HRTIMER_NORESTART;
  1887. ret_overrun = hrtimer_forward_now(&vtimer->timer, interval);
  1888. if (ret_overrun != 1)
  1889. drm_dbg_vbl(dev, "vblank timer overrun\n");
  1890. if (crtc_funcs->handle_vblank_timeout)
  1891. succ = crtc_funcs->handle_vblank_timeout(crtc);
  1892. else
  1893. succ = drm_crtc_handle_vblank(crtc);
  1894. if (!succ)
  1895. return HRTIMER_NORESTART;
  1896. return HRTIMER_RESTART;
  1897. }
  1898. /**
  1899. * drm_crtc_vblank_start_timer - Starts the vblank timer on the given CRTC
  1900. * @crtc: the CRTC
  1901. *
  1902. * Drivers should call this function from their CRTC's enable_vblank
  1903. * function to start a vblank timer. The timer will fire after the duration
  1904. * of a full frame. drm_crtc_vblank_cancel_timer() disables a running timer.
  1905. *
  1906. * Returns:
  1907. * 0 on success, or a negative errno code otherwise.
  1908. */
  1909. int drm_crtc_vblank_start_timer(struct drm_crtc *crtc)
  1910. {
  1911. struct drm_vblank_crtc *vblank = drm_crtc_vblank_crtc(crtc);
  1912. struct drm_vblank_crtc_timer *vtimer = &vblank->vblank_timer;
  1913. unsigned long flags;
  1914. if (!vtimer->crtc) {
  1915. /*
  1916. * Set up the data structures on the first invocation.
  1917. */
  1918. vtimer->crtc = crtc;
  1919. spin_lock_init(&vtimer->interval_lock);
  1920. hrtimer_setup(&vtimer->timer, drm_vblank_timer_function,
  1921. CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  1922. } else {
  1923. /*
  1924. * Timer should not be active. If it is, wait for the
  1925. * previous cancel operations to finish.
  1926. */
  1927. while (hrtimer_active(&vtimer->timer))
  1928. hrtimer_try_to_cancel(&vtimer->timer);
  1929. }
  1930. drm_calc_timestamping_constants(crtc, &crtc->mode);
  1931. spin_lock_irqsave(&vtimer->interval_lock, flags);
  1932. vtimer->interval = ns_to_ktime(vblank->framedur_ns);
  1933. spin_unlock_irqrestore(&vtimer->interval_lock, flags);
  1934. hrtimer_start(&vtimer->timer, vtimer->interval, HRTIMER_MODE_REL);
  1935. return 0;
  1936. }
  1937. EXPORT_SYMBOL(drm_crtc_vblank_start_timer);
  1938. /**
  1939. * drm_crtc_vblank_cancel_timer - Cancels the given CRTC's vblank timer
  1940. * @crtc: the CRTC
  1941. *
  1942. * Drivers should call this function from their CRTC's disable_vblank
  1943. * function to stop a vblank timer.
  1944. */
  1945. void drm_crtc_vblank_cancel_timer(struct drm_crtc *crtc)
  1946. {
  1947. struct drm_vblank_crtc *vblank = drm_crtc_vblank_crtc(crtc);
  1948. struct drm_vblank_crtc_timer *vtimer = &vblank->vblank_timer;
  1949. unsigned long flags;
  1950. /*
  1951. * Calling hrtimer_cancel() can result in a deadlock with DRM's
  1952. * vblank_time_lime_lock and hrtimers' softirq_expiry_lock. So
  1953. * clear interval and indicate cancellation. The timer function
  1954. * will cancel itself on the next invocation.
  1955. */
  1956. spin_lock_irqsave(&vtimer->interval_lock, flags);
  1957. vtimer->interval = 0;
  1958. spin_unlock_irqrestore(&vtimer->interval_lock, flags);
  1959. hrtimer_try_to_cancel(&vtimer->timer);
  1960. }
  1961. EXPORT_SYMBOL(drm_crtc_vblank_cancel_timer);
  1962. /**
  1963. * drm_crtc_vblank_get_vblank_timeout - Returns the vblank timeout
  1964. * @crtc: The CRTC
  1965. * @vblank_time: Returns the next vblank timestamp
  1966. *
  1967. * The helper drm_crtc_vblank_get_vblank_timeout() returns the next vblank
  1968. * timestamp of the CRTC's vblank timer according to the timer's expiry
  1969. * time.
  1970. */
  1971. void drm_crtc_vblank_get_vblank_timeout(struct drm_crtc *crtc, ktime_t *vblank_time)
  1972. {
  1973. struct drm_vblank_crtc *vblank = drm_crtc_vblank_crtc(crtc);
  1974. struct drm_vblank_crtc_timer *vtimer = &vblank->vblank_timer;
  1975. u64 cur_count;
  1976. ktime_t cur_time;
  1977. if (!READ_ONCE(vblank->enabled)) {
  1978. *vblank_time = ktime_get();
  1979. return;
  1980. }
  1981. /*
  1982. * A concurrent vblank timeout could update the expires field before
  1983. * we compare it with the vblank time. Hence we'd compare the old
  1984. * expiry time to the new vblank time; deducing the timer had already
  1985. * expired. Reread until we get consistent values from both fields.
  1986. */
  1987. do {
  1988. cur_count = drm_crtc_vblank_count_and_time(crtc, &cur_time);
  1989. *vblank_time = READ_ONCE(vtimer->timer.node.expires);
  1990. } while (cur_count != drm_crtc_vblank_count_and_time(crtc, &cur_time));
  1991. if (drm_WARN_ON(crtc->dev, !ktime_compare(*vblank_time, cur_time)))
  1992. return; /* Already expired */
  1993. /*
  1994. * To prevent races we roll the hrtimer forward before we do any
  1995. * interrupt processing - this is how real hw works (the interrupt
  1996. * is only generated after all the vblank registers are updated)
  1997. * and what the vblank core expects. Therefore we need to always
  1998. * correct the timestamp by one frame.
  1999. */
  2000. *vblank_time = ktime_sub(*vblank_time, vtimer->interval);
  2001. }
  2002. EXPORT_SYMBOL(drm_crtc_vblank_get_vblank_timeout);