drm_irq.c 53 KB

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