drm_vblank.c 55 KB

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