drm_irq.c 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776
  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. * Reset the stored timestamp for the current vblank count to correspond
  78. * to the last vblank occurred.
  79. *
  80. * Only to be called from drm_crtc_vblank_on().
  81. *
  82. * Note: caller must hold dev->vbl_lock since this reads & writes
  83. * device vblank fields.
  84. */
  85. static void drm_reset_vblank_timestamp(struct drm_device *dev, unsigned int pipe)
  86. {
  87. u32 cur_vblank;
  88. bool rc;
  89. struct timeval t_vblank;
  90. int count = DRM_TIMESTAMP_MAXRETRIES;
  91. spin_lock(&dev->vblank_time_lock);
  92. /*
  93. * sample the current counter to avoid random jumps
  94. * when drm_vblank_enable() applies the diff
  95. */
  96. do {
  97. cur_vblank = dev->driver->get_vblank_counter(dev, pipe);
  98. rc = drm_get_last_vbltimestamp(dev, pipe, &t_vblank, 0);
  99. } while (cur_vblank != dev->driver->get_vblank_counter(dev, pipe) && --count > 0);
  100. /*
  101. * Only reinitialize corresponding vblank timestamp if high-precision query
  102. * available and didn't fail. Otherwise reinitialize delayed at next vblank
  103. * interrupt and assign 0 for now, to mark the vblanktimestamp as invalid.
  104. */
  105. if (!rc)
  106. t_vblank = (struct timeval) {0, 0};
  107. /*
  108. * +1 to make sure user will never see the same
  109. * vblank counter value before and after a modeset
  110. */
  111. store_vblank(dev, pipe, 1, &t_vblank, cur_vblank);
  112. spin_unlock(&dev->vblank_time_lock);
  113. }
  114. /*
  115. * Call back into the driver to update the appropriate vblank counter
  116. * (specified by @pipe). Deal with wraparound, if it occurred, and
  117. * update the last read value so we can deal with wraparound on the next
  118. * call if necessary.
  119. *
  120. * Only necessary when going from off->on, to account for frames we
  121. * didn't get an interrupt for.
  122. *
  123. * Note: caller must hold dev->vbl_lock since this reads & writes
  124. * device vblank fields.
  125. */
  126. static void drm_update_vblank_count(struct drm_device *dev, unsigned int pipe,
  127. unsigned long flags)
  128. {
  129. struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
  130. u32 cur_vblank, diff;
  131. bool rc;
  132. struct timeval t_vblank;
  133. int count = DRM_TIMESTAMP_MAXRETRIES;
  134. int framedur_ns = vblank->framedur_ns;
  135. /*
  136. * Interrupts were disabled prior to this call, so deal with counter
  137. * wrap if needed.
  138. * NOTE! It's possible we lost a full dev->max_vblank_count + 1 events
  139. * here if the register is small or we had vblank interrupts off for
  140. * a long time.
  141. *
  142. * We repeat the hardware vblank counter & timestamp query until
  143. * we get consistent results. This to prevent races between gpu
  144. * updating its hardware counter while we are retrieving the
  145. * corresponding vblank timestamp.
  146. */
  147. do {
  148. cur_vblank = dev->driver->get_vblank_counter(dev, pipe);
  149. rc = drm_get_last_vbltimestamp(dev, pipe, &t_vblank, flags);
  150. } while (cur_vblank != dev->driver->get_vblank_counter(dev, pipe) && --count > 0);
  151. if (dev->max_vblank_count != 0) {
  152. /* trust the hw counter when it's around */
  153. diff = (cur_vblank - vblank->last) & dev->max_vblank_count;
  154. } else if (rc && framedur_ns) {
  155. const struct timeval *t_old;
  156. u64 diff_ns;
  157. t_old = &vblank->time;
  158. diff_ns = timeval_to_ns(&t_vblank) - timeval_to_ns(t_old);
  159. /*
  160. * Figure out how many vblanks we've missed based
  161. * on the difference in the timestamps and the
  162. * frame/field duration.
  163. */
  164. diff = DIV_ROUND_CLOSEST_ULL(diff_ns, framedur_ns);
  165. if (diff == 0 && flags & DRM_CALLED_FROM_VBLIRQ)
  166. DRM_DEBUG_VBL("crtc %u: Redundant vblirq ignored."
  167. " diff_ns = %lld, framedur_ns = %d)\n",
  168. pipe, (long long) diff_ns, framedur_ns);
  169. } else {
  170. /* some kind of default for drivers w/o accurate vbl timestamping */
  171. diff = (flags & DRM_CALLED_FROM_VBLIRQ) != 0;
  172. }
  173. /*
  174. * Within a drm_vblank_pre_modeset - drm_vblank_post_modeset
  175. * interval? If so then vblank irqs keep running and it will likely
  176. * happen that the hardware vblank counter is not trustworthy as it
  177. * might reset at some point in that interval and vblank timestamps
  178. * are not trustworthy either in that interval. Iow. this can result
  179. * in a bogus diff >> 1 which must be avoided as it would cause
  180. * random large forward jumps of the software vblank counter.
  181. */
  182. if (diff > 1 && (vblank->inmodeset & 0x2)) {
  183. DRM_DEBUG_VBL("clamping vblank bump to 1 on crtc %u: diffr=%u"
  184. " due to pre-modeset.\n", pipe, diff);
  185. diff = 1;
  186. }
  187. DRM_DEBUG_VBL("updating vblank count on crtc %u:"
  188. " current=%u, diff=%u, hw=%u hw_last=%u\n",
  189. pipe, vblank->count, diff, cur_vblank, vblank->last);
  190. if (diff == 0) {
  191. WARN_ON_ONCE(cur_vblank != vblank->last);
  192. return;
  193. }
  194. /*
  195. * Only reinitialize corresponding vblank timestamp if high-precision query
  196. * available and didn't fail, or we were called from the vblank interrupt.
  197. * Otherwise reinitialize delayed at next vblank interrupt and assign 0
  198. * for now, to mark the vblanktimestamp as invalid.
  199. */
  200. if (!rc && (flags & DRM_CALLED_FROM_VBLIRQ) == 0)
  201. t_vblank = (struct timeval) {0, 0};
  202. store_vblank(dev, pipe, diff, &t_vblank, cur_vblank);
  203. }
  204. static u32 drm_vblank_count(struct drm_device *dev, unsigned int pipe)
  205. {
  206. struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
  207. if (WARN_ON(pipe >= dev->num_crtcs))
  208. return 0;
  209. return vblank->count;
  210. }
  211. /**
  212. * drm_accurate_vblank_count - retrieve the master vblank counter
  213. * @crtc: which counter to retrieve
  214. *
  215. * This function is similar to @drm_crtc_vblank_count but this
  216. * function interpolates to handle a race with vblank irq's.
  217. *
  218. * This is mostly useful for hardware that can obtain the scanout
  219. * position, but doesn't have a frame counter.
  220. */
  221. u32 drm_accurate_vblank_count(struct drm_crtc *crtc)
  222. {
  223. struct drm_device *dev = crtc->dev;
  224. unsigned int pipe = drm_crtc_index(crtc);
  225. u32 vblank;
  226. unsigned long flags;
  227. WARN(!dev->driver->get_vblank_timestamp,
  228. "This function requires support for accurate vblank timestamps.");
  229. spin_lock_irqsave(&dev->vblank_time_lock, flags);
  230. drm_update_vblank_count(dev, pipe, 0);
  231. vblank = drm_vblank_count(dev, pipe);
  232. spin_unlock_irqrestore(&dev->vblank_time_lock, flags);
  233. return vblank;
  234. }
  235. EXPORT_SYMBOL(drm_accurate_vblank_count);
  236. /*
  237. * Disable vblank irq's on crtc, make sure that last vblank count
  238. * of hardware and corresponding consistent software vblank counter
  239. * are preserved, even if there are any spurious vblank irq's after
  240. * disable.
  241. */
  242. static void vblank_disable_and_save(struct drm_device *dev, unsigned int pipe)
  243. {
  244. struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
  245. unsigned long irqflags;
  246. /* Prevent vblank irq processing while disabling vblank irqs,
  247. * so no updates of timestamps or count can happen after we've
  248. * disabled. Needed to prevent races in case of delayed irq's.
  249. */
  250. spin_lock_irqsave(&dev->vblank_time_lock, irqflags);
  251. /*
  252. * Only disable vblank interrupts if they're enabled. This avoids
  253. * calling the ->disable_vblank() operation in atomic context with the
  254. * hardware potentially runtime suspended.
  255. */
  256. if (vblank->enabled) {
  257. dev->driver->disable_vblank(dev, pipe);
  258. vblank->enabled = false;
  259. }
  260. /*
  261. * Always update the count and timestamp to maintain the
  262. * appearance that the counter has been ticking all along until
  263. * this time. This makes the count account for the entire time
  264. * between drm_crtc_vblank_on() and drm_crtc_vblank_off().
  265. */
  266. drm_update_vblank_count(dev, pipe, 0);
  267. spin_unlock_irqrestore(&dev->vblank_time_lock, irqflags);
  268. }
  269. static void vblank_disable_fn(unsigned long arg)
  270. {
  271. struct drm_vblank_crtc *vblank = (void *)arg;
  272. struct drm_device *dev = vblank->dev;
  273. unsigned int pipe = vblank->pipe;
  274. unsigned long irqflags;
  275. spin_lock_irqsave(&dev->vbl_lock, irqflags);
  276. if (atomic_read(&vblank->refcount) == 0 && vblank->enabled) {
  277. DRM_DEBUG("disabling vblank on crtc %u\n", pipe);
  278. vblank_disable_and_save(dev, pipe);
  279. }
  280. spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
  281. }
  282. /**
  283. * drm_vblank_cleanup - cleanup vblank support
  284. * @dev: DRM device
  285. *
  286. * This function cleans up any resources allocated in drm_vblank_init.
  287. */
  288. void drm_vblank_cleanup(struct drm_device *dev)
  289. {
  290. unsigned int pipe;
  291. /* Bail if the driver didn't call drm_vblank_init() */
  292. if (dev->num_crtcs == 0)
  293. return;
  294. for (pipe = 0; pipe < dev->num_crtcs; pipe++) {
  295. struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
  296. WARN_ON(vblank->enabled &&
  297. drm_core_check_feature(dev, DRIVER_MODESET));
  298. del_timer_sync(&vblank->disable_timer);
  299. }
  300. kfree(dev->vblank);
  301. dev->num_crtcs = 0;
  302. }
  303. EXPORT_SYMBOL(drm_vblank_cleanup);
  304. /**
  305. * drm_vblank_init - initialize vblank support
  306. * @dev: DRM device
  307. * @num_crtcs: number of CRTCs supported by @dev
  308. *
  309. * This function initializes vblank support for @num_crtcs display pipelines.
  310. *
  311. * Returns:
  312. * Zero on success or a negative error code on failure.
  313. */
  314. int drm_vblank_init(struct drm_device *dev, unsigned int num_crtcs)
  315. {
  316. int ret = -ENOMEM;
  317. unsigned int i;
  318. spin_lock_init(&dev->vbl_lock);
  319. spin_lock_init(&dev->vblank_time_lock);
  320. dev->num_crtcs = num_crtcs;
  321. dev->vblank = kcalloc(num_crtcs, sizeof(*dev->vblank), GFP_KERNEL);
  322. if (!dev->vblank)
  323. goto err;
  324. for (i = 0; i < num_crtcs; i++) {
  325. struct drm_vblank_crtc *vblank = &dev->vblank[i];
  326. vblank->dev = dev;
  327. vblank->pipe = i;
  328. init_waitqueue_head(&vblank->queue);
  329. setup_timer(&vblank->disable_timer, vblank_disable_fn,
  330. (unsigned long)vblank);
  331. seqlock_init(&vblank->seqlock);
  332. }
  333. DRM_INFO("Supports vblank timestamp caching Rev 2 (21.10.2013).\n");
  334. /* Driver specific high-precision vblank timestamping supported? */
  335. if (dev->driver->get_vblank_timestamp)
  336. DRM_INFO("Driver supports precise vblank timestamp query.\n");
  337. else
  338. DRM_INFO("No driver support for vblank timestamp query.\n");
  339. /* Must have precise timestamping for reliable vblank instant disable */
  340. if (dev->vblank_disable_immediate && !dev->driver->get_vblank_timestamp) {
  341. dev->vblank_disable_immediate = false;
  342. DRM_INFO("Setting vblank_disable_immediate to false because "
  343. "get_vblank_timestamp == NULL\n");
  344. }
  345. return 0;
  346. err:
  347. dev->num_crtcs = 0;
  348. return ret;
  349. }
  350. EXPORT_SYMBOL(drm_vblank_init);
  351. static void drm_irq_vgaarb_nokms(void *cookie, bool state)
  352. {
  353. struct drm_device *dev = cookie;
  354. if (dev->driver->vgaarb_irq) {
  355. dev->driver->vgaarb_irq(dev, state);
  356. return;
  357. }
  358. if (!dev->irq_enabled)
  359. return;
  360. if (state) {
  361. if (dev->driver->irq_uninstall)
  362. dev->driver->irq_uninstall(dev);
  363. } else {
  364. if (dev->driver->irq_preinstall)
  365. dev->driver->irq_preinstall(dev);
  366. if (dev->driver->irq_postinstall)
  367. dev->driver->irq_postinstall(dev);
  368. }
  369. }
  370. /**
  371. * drm_irq_install - install IRQ handler
  372. * @dev: DRM device
  373. * @irq: IRQ number to install the handler for
  374. *
  375. * Initializes the IRQ related data. Installs the handler, calling the driver
  376. * irq_preinstall() and irq_postinstall() functions before and after the
  377. * installation.
  378. *
  379. * This is the simplified helper interface provided for drivers with no special
  380. * needs. Drivers which need to install interrupt handlers for multiple
  381. * interrupts must instead set drm_device->irq_enabled to signal the DRM core
  382. * that vblank interrupts are available.
  383. *
  384. * Returns:
  385. * Zero on success or a negative error code on failure.
  386. */
  387. int drm_irq_install(struct drm_device *dev, int irq)
  388. {
  389. int ret;
  390. unsigned long sh_flags = 0;
  391. if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
  392. return -EINVAL;
  393. if (irq == 0)
  394. return -EINVAL;
  395. /* Driver must have been initialized */
  396. if (!dev->dev_private)
  397. return -EINVAL;
  398. if (dev->irq_enabled)
  399. return -EBUSY;
  400. dev->irq_enabled = true;
  401. DRM_DEBUG("irq=%d\n", irq);
  402. /* Before installing handler */
  403. if (dev->driver->irq_preinstall)
  404. dev->driver->irq_preinstall(dev);
  405. /* Install handler */
  406. if (drm_core_check_feature(dev, DRIVER_IRQ_SHARED))
  407. sh_flags = IRQF_SHARED;
  408. ret = request_irq(irq, dev->driver->irq_handler,
  409. sh_flags, dev->driver->name, dev);
  410. if (ret < 0) {
  411. dev->irq_enabled = false;
  412. return ret;
  413. }
  414. if (drm_core_check_feature(dev, DRIVER_LEGACY))
  415. vga_client_register(dev->pdev, (void *)dev, drm_irq_vgaarb_nokms, NULL);
  416. /* After installing handler */
  417. if (dev->driver->irq_postinstall)
  418. ret = dev->driver->irq_postinstall(dev);
  419. if (ret < 0) {
  420. dev->irq_enabled = false;
  421. if (drm_core_check_feature(dev, DRIVER_LEGACY))
  422. vga_client_register(dev->pdev, NULL, NULL, NULL);
  423. free_irq(irq, dev);
  424. } else {
  425. dev->irq = irq;
  426. }
  427. return ret;
  428. }
  429. EXPORT_SYMBOL(drm_irq_install);
  430. /**
  431. * drm_irq_uninstall - uninstall the IRQ handler
  432. * @dev: DRM device
  433. *
  434. * Calls the driver's irq_uninstall() function and unregisters the IRQ handler.
  435. * This should only be called by drivers which used drm_irq_install() to set up
  436. * their interrupt handler. Other drivers must only reset
  437. * drm_device->irq_enabled to false.
  438. *
  439. * Note that for kernel modesetting drivers it is a bug if this function fails.
  440. * The sanity checks are only to catch buggy user modesetting drivers which call
  441. * the same function through an ioctl.
  442. *
  443. * Returns:
  444. * Zero on success or a negative error code on failure.
  445. */
  446. int drm_irq_uninstall(struct drm_device *dev)
  447. {
  448. unsigned long irqflags;
  449. bool irq_enabled;
  450. int i;
  451. if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
  452. return -EINVAL;
  453. irq_enabled = dev->irq_enabled;
  454. dev->irq_enabled = false;
  455. /*
  456. * Wake up any waiters so they don't hang. This is just to paper over
  457. * issues for UMS drivers which aren't in full control of their
  458. * vblank/irq handling. KMS drivers must ensure that vblanks are all
  459. * disabled when uninstalling the irq handler.
  460. */
  461. if (dev->num_crtcs) {
  462. spin_lock_irqsave(&dev->vbl_lock, irqflags);
  463. for (i = 0; i < dev->num_crtcs; i++) {
  464. struct drm_vblank_crtc *vblank = &dev->vblank[i];
  465. if (!vblank->enabled)
  466. continue;
  467. WARN_ON(drm_core_check_feature(dev, DRIVER_MODESET));
  468. vblank_disable_and_save(dev, i);
  469. wake_up(&vblank->queue);
  470. }
  471. spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
  472. }
  473. if (!irq_enabled)
  474. return -EINVAL;
  475. DRM_DEBUG("irq=%d\n", dev->irq);
  476. if (drm_core_check_feature(dev, DRIVER_LEGACY))
  477. vga_client_register(dev->pdev, NULL, NULL, NULL);
  478. if (dev->driver->irq_uninstall)
  479. dev->driver->irq_uninstall(dev);
  480. free_irq(dev->irq, dev);
  481. return 0;
  482. }
  483. EXPORT_SYMBOL(drm_irq_uninstall);
  484. /*
  485. * IRQ control ioctl.
  486. *
  487. * \param inode device inode.
  488. * \param file_priv DRM file private.
  489. * \param cmd command.
  490. * \param arg user argument, pointing to a drm_control structure.
  491. * \return zero on success or a negative number on failure.
  492. *
  493. * Calls irq_install() or irq_uninstall() according to \p arg.
  494. */
  495. int drm_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. /* Check if in vblank area:
  689. * vpos is >=0 in video scanout area, but negative
  690. * within vblank area, counting down the number of lines until
  691. * start of scanout.
  692. */
  693. if (vbl_status & DRM_SCANOUTPOS_IN_VBLANK)
  694. ret |= DRM_VBLANKTIME_IN_VBLANK;
  695. /* Convert scanout position into elapsed time at raw_time query
  696. * since start of scanout at first display scanline. delta_ns
  697. * can be negative if start of scanout hasn't happened yet.
  698. */
  699. delta_ns = div_s64(1000000LL * (vpos * mode->crtc_htotal + hpos),
  700. mode->crtc_clock);
  701. if (!drm_timestamp_monotonic)
  702. etime = ktime_mono_to_real(etime);
  703. /* save this only for debugging purposes */
  704. tv_etime = ktime_to_timeval(etime);
  705. /* Subtract time delta from raw timestamp to get final
  706. * vblank_time timestamp for end of vblank.
  707. */
  708. etime = ktime_sub_ns(etime, delta_ns);
  709. *vblank_time = ktime_to_timeval(etime);
  710. DRM_DEBUG_VBL("crtc %u : v 0x%x p(%d,%d)@ %ld.%ld -> %ld.%ld [e %d us, %d rep]\n",
  711. pipe, vbl_status, hpos, vpos,
  712. (long)tv_etime.tv_sec, (long)tv_etime.tv_usec,
  713. (long)vblank_time->tv_sec, (long)vblank_time->tv_usec,
  714. duration_ns/1000, i);
  715. return ret;
  716. }
  717. EXPORT_SYMBOL(drm_calc_vbltimestamp_from_scanoutpos);
  718. static struct timeval get_drm_timestamp(void)
  719. {
  720. ktime_t now;
  721. now = drm_timestamp_monotonic ? ktime_get() : ktime_get_real();
  722. return ktime_to_timeval(now);
  723. }
  724. /**
  725. * drm_get_last_vbltimestamp - retrieve raw timestamp for the most recent
  726. * vblank interval
  727. * @dev: DRM device
  728. * @pipe: index of CRTC whose vblank timestamp to retrieve
  729. * @tvblank: Pointer to target struct timeval which should receive the timestamp
  730. * @flags: Flags to pass to driver:
  731. * 0 = Default,
  732. * DRM_CALLED_FROM_VBLIRQ = If function is called from vbl IRQ handler
  733. *
  734. * Fetches the system timestamp corresponding to the time of the most recent
  735. * vblank interval on specified CRTC. May call into kms-driver to
  736. * compute the timestamp with a high-precision GPU specific method.
  737. *
  738. * Returns zero if timestamp originates from uncorrected do_gettimeofday()
  739. * call, i.e., it isn't very precisely locked to the true vblank.
  740. *
  741. * Returns:
  742. * True if timestamp is considered to be very precise, false otherwise.
  743. */
  744. static bool
  745. drm_get_last_vbltimestamp(struct drm_device *dev, unsigned int pipe,
  746. struct timeval *tvblank, unsigned flags)
  747. {
  748. int ret;
  749. /* Define requested maximum error on timestamps (nanoseconds). */
  750. int max_error = (int) drm_timestamp_precision * 1000;
  751. /* Query driver if possible and precision timestamping enabled. */
  752. if (dev->driver->get_vblank_timestamp && (max_error > 0)) {
  753. ret = dev->driver->get_vblank_timestamp(dev, pipe, &max_error,
  754. tvblank, flags);
  755. if (ret > 0)
  756. return true;
  757. }
  758. /* GPU high precision timestamp query unsupported or failed.
  759. * Return current monotonic/gettimeofday timestamp as best estimate.
  760. */
  761. *tvblank = get_drm_timestamp();
  762. return false;
  763. }
  764. /**
  765. * drm_crtc_vblank_count - retrieve "cooked" vblank counter value
  766. * @crtc: which counter to retrieve
  767. *
  768. * Fetches the "cooked" vblank count value that represents the number of
  769. * vblank events since the system was booted, including lost events due to
  770. * modesetting activity.
  771. *
  772. * Returns:
  773. * The software vblank counter.
  774. */
  775. u32 drm_crtc_vblank_count(struct drm_crtc *crtc)
  776. {
  777. return drm_vblank_count(crtc->dev, drm_crtc_index(crtc));
  778. }
  779. EXPORT_SYMBOL(drm_crtc_vblank_count);
  780. /**
  781. * drm_vblank_count_and_time - retrieve "cooked" vblank counter value and the
  782. * system timestamp corresponding to that vblank counter value.
  783. * @dev: DRM device
  784. * @pipe: index of CRTC whose counter to retrieve
  785. * @vblanktime: Pointer to struct timeval to receive the vblank timestamp.
  786. *
  787. * Fetches the "cooked" vblank count value that represents the number of
  788. * vblank events since the system was booted, including lost events due to
  789. * modesetting activity. Returns corresponding system timestamp of the time
  790. * of the vblank interval that corresponds to the current vblank counter value.
  791. *
  792. * This is the legacy version of drm_crtc_vblank_count_and_time().
  793. */
  794. static u32 drm_vblank_count_and_time(struct drm_device *dev, unsigned int pipe,
  795. struct timeval *vblanktime)
  796. {
  797. struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
  798. u32 vblank_count;
  799. unsigned int seq;
  800. if (WARN_ON(pipe >= dev->num_crtcs)) {
  801. *vblanktime = (struct timeval) { 0 };
  802. return 0;
  803. }
  804. do {
  805. seq = read_seqbegin(&vblank->seqlock);
  806. vblank_count = vblank->count;
  807. *vblanktime = vblank->time;
  808. } while (read_seqretry(&vblank->seqlock, seq));
  809. return vblank_count;
  810. }
  811. /**
  812. * drm_crtc_vblank_count_and_time - retrieve "cooked" vblank counter value
  813. * and the system timestamp corresponding to that vblank counter value
  814. * @crtc: which counter to retrieve
  815. * @vblanktime: Pointer to struct timeval to receive the vblank timestamp.
  816. *
  817. * Fetches the "cooked" vblank count value that represents the number of
  818. * vblank events since the system was booted, including lost events due to
  819. * modesetting activity. Returns corresponding system timestamp of the time
  820. * of the vblank interval that corresponds to the current vblank counter value.
  821. */
  822. u32 drm_crtc_vblank_count_and_time(struct drm_crtc *crtc,
  823. struct timeval *vblanktime)
  824. {
  825. return drm_vblank_count_and_time(crtc->dev, drm_crtc_index(crtc),
  826. vblanktime);
  827. }
  828. EXPORT_SYMBOL(drm_crtc_vblank_count_and_time);
  829. static void send_vblank_event(struct drm_device *dev,
  830. struct drm_pending_vblank_event *e,
  831. unsigned long seq, struct timeval *now)
  832. {
  833. e->event.sequence = seq;
  834. e->event.tv_sec = now->tv_sec;
  835. e->event.tv_usec = now->tv_usec;
  836. trace_drm_vblank_event_delivered(e->base.pid, e->pipe,
  837. e->event.sequence);
  838. drm_send_event_locked(dev, &e->base);
  839. }
  840. /**
  841. * drm_crtc_arm_vblank_event - arm vblank event after pageflip
  842. * @crtc: the source CRTC of the vblank event
  843. * @e: the event to send
  844. *
  845. * A lot of drivers need to generate vblank events for the very next vblank
  846. * interrupt. For example when the page flip interrupt happens when the page
  847. * flip gets armed, but not when it actually executes within the next vblank
  848. * period. This helper function implements exactly the required vblank arming
  849. * behaviour.
  850. *
  851. * NOTE: Drivers using this to send out the event in struct &drm_crtc_state
  852. * as part of an atomic commit must ensure that the next vblank happens at
  853. * exactly the same time as the atomic commit is committed to the hardware. This
  854. * function itself does **not** protect again the next vblank interrupt racing
  855. * with either this function call or the atomic commit operation. A possible
  856. * sequence could be:
  857. *
  858. * 1. Driver commits new hardware state into vblank-synchronized registers.
  859. * 2. A vblank happens, committing the hardware state. Also the corresponding
  860. * vblank interrupt is fired off and fully processed by the interrupt
  861. * handler.
  862. * 3. The atomic commit operation proceeds to call drm_crtc_arm_vblank_event().
  863. * 4. The event is only send out for the next vblank, which is wrong.
  864. *
  865. * An equivalent race can happen when the driver calls
  866. * drm_crtc_arm_vblank_event() before writing out the new hardware state.
  867. *
  868. * The only way to make this work safely is to prevent the vblank from firing
  869. * (and the hardware from committing anything else) until the entire atomic
  870. * commit sequence has run to completion. If the hardware does not have such a
  871. * feature (e.g. using a "go" bit), then it is unsafe to use this functions.
  872. * Instead drivers need to manually send out the event from their interrupt
  873. * handler by calling drm_crtc_send_vblank_event() and make sure that there's no
  874. * possible race with the hardware committing the atomic update.
  875. *
  876. * Caller must hold event lock. Caller must also hold a vblank reference for
  877. * the event @e, which will be dropped when the next vblank arrives.
  878. */
  879. void drm_crtc_arm_vblank_event(struct drm_crtc *crtc,
  880. struct drm_pending_vblank_event *e)
  881. {
  882. struct drm_device *dev = crtc->dev;
  883. unsigned int pipe = drm_crtc_index(crtc);
  884. assert_spin_locked(&dev->event_lock);
  885. e->pipe = pipe;
  886. e->event.sequence = drm_vblank_count(dev, pipe);
  887. list_add_tail(&e->base.link, &dev->vblank_event_list);
  888. }
  889. EXPORT_SYMBOL(drm_crtc_arm_vblank_event);
  890. /**
  891. * drm_crtc_send_vblank_event - helper to send vblank event after pageflip
  892. * @crtc: the source CRTC of the vblank event
  893. * @e: the event to send
  894. *
  895. * Updates sequence # and timestamp on event for the most recently processed
  896. * vblank, and sends it to userspace. Caller must hold event lock.
  897. *
  898. * See drm_crtc_arm_vblank_event() for a helper which can be used in certain
  899. * situation, especially to send out events for atomic commit operations.
  900. */
  901. void drm_crtc_send_vblank_event(struct drm_crtc *crtc,
  902. struct drm_pending_vblank_event *e)
  903. {
  904. struct drm_device *dev = crtc->dev;
  905. unsigned int seq, pipe = drm_crtc_index(crtc);
  906. struct timeval now;
  907. if (dev->num_crtcs > 0) {
  908. seq = drm_vblank_count_and_time(dev, pipe, &now);
  909. } else {
  910. seq = 0;
  911. now = get_drm_timestamp();
  912. }
  913. e->pipe = pipe;
  914. send_vblank_event(dev, e, seq, &now);
  915. }
  916. EXPORT_SYMBOL(drm_crtc_send_vblank_event);
  917. /**
  918. * drm_vblank_enable - enable the vblank interrupt on a CRTC
  919. * @dev: DRM device
  920. * @pipe: CRTC index
  921. *
  922. * Returns:
  923. * Zero on success or a negative error code on failure.
  924. */
  925. static int drm_vblank_enable(struct drm_device *dev, unsigned int pipe)
  926. {
  927. struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
  928. int ret = 0;
  929. assert_spin_locked(&dev->vbl_lock);
  930. spin_lock(&dev->vblank_time_lock);
  931. if (!vblank->enabled) {
  932. /*
  933. * Enable vblank irqs under vblank_time_lock protection.
  934. * All vblank count & timestamp updates are held off
  935. * until we are done reinitializing master counter and
  936. * timestamps. Filtercode in drm_handle_vblank() will
  937. * prevent double-accounting of same vblank interval.
  938. */
  939. ret = dev->driver->enable_vblank(dev, pipe);
  940. DRM_DEBUG("enabling vblank on crtc %u, ret: %d\n", pipe, ret);
  941. if (ret)
  942. atomic_dec(&vblank->refcount);
  943. else {
  944. vblank->enabled = true;
  945. drm_update_vblank_count(dev, pipe, 0);
  946. }
  947. }
  948. spin_unlock(&dev->vblank_time_lock);
  949. return ret;
  950. }
  951. /**
  952. * drm_vblank_get - get a reference count on vblank events
  953. * @dev: DRM device
  954. * @pipe: index of CRTC to own
  955. *
  956. * Acquire a reference count on vblank events to avoid having them disabled
  957. * while in use.
  958. *
  959. * This is the legacy version of drm_crtc_vblank_get().
  960. *
  961. * Returns:
  962. * Zero on success or a negative error code on failure.
  963. */
  964. static int drm_vblank_get(struct drm_device *dev, unsigned int pipe)
  965. {
  966. struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
  967. unsigned long irqflags;
  968. int ret = 0;
  969. if (!dev->num_crtcs)
  970. return -EINVAL;
  971. if (WARN_ON(pipe >= dev->num_crtcs))
  972. return -EINVAL;
  973. spin_lock_irqsave(&dev->vbl_lock, irqflags);
  974. /* Going from 0->1 means we have to enable interrupts again */
  975. if (atomic_add_return(1, &vblank->refcount) == 1) {
  976. ret = drm_vblank_enable(dev, pipe);
  977. } else {
  978. if (!vblank->enabled) {
  979. atomic_dec(&vblank->refcount);
  980. ret = -EINVAL;
  981. }
  982. }
  983. spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
  984. return ret;
  985. }
  986. /**
  987. * drm_crtc_vblank_get - get a reference count on vblank events
  988. * @crtc: which CRTC to own
  989. *
  990. * Acquire a reference count on vblank events to avoid having them disabled
  991. * while in use.
  992. *
  993. * Returns:
  994. * Zero on success or a negative error code on failure.
  995. */
  996. int drm_crtc_vblank_get(struct drm_crtc *crtc)
  997. {
  998. return drm_vblank_get(crtc->dev, drm_crtc_index(crtc));
  999. }
  1000. EXPORT_SYMBOL(drm_crtc_vblank_get);
  1001. /**
  1002. * drm_vblank_put - release ownership of vblank events
  1003. * @dev: DRM device
  1004. * @pipe: index of CRTC to release
  1005. *
  1006. * Release ownership of a given vblank counter, turning off interrupts
  1007. * if possible. Disable interrupts after drm_vblank_offdelay milliseconds.
  1008. *
  1009. * This is the legacy version of drm_crtc_vblank_put().
  1010. */
  1011. static void drm_vblank_put(struct drm_device *dev, unsigned int pipe)
  1012. {
  1013. struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
  1014. if (WARN_ON(pipe >= dev->num_crtcs))
  1015. return;
  1016. if (WARN_ON(atomic_read(&vblank->refcount) == 0))
  1017. return;
  1018. /* Last user schedules interrupt disable */
  1019. if (atomic_dec_and_test(&vblank->refcount)) {
  1020. if (drm_vblank_offdelay == 0)
  1021. return;
  1022. else if (dev->vblank_disable_immediate || drm_vblank_offdelay < 0)
  1023. vblank_disable_fn((unsigned long)vblank);
  1024. else
  1025. mod_timer(&vblank->disable_timer,
  1026. jiffies + ((drm_vblank_offdelay * HZ)/1000));
  1027. }
  1028. }
  1029. /**
  1030. * drm_crtc_vblank_put - give up ownership of vblank events
  1031. * @crtc: which counter to give up
  1032. *
  1033. * Release ownership of a given vblank counter, turning off interrupts
  1034. * if possible. Disable interrupts after drm_vblank_offdelay milliseconds.
  1035. */
  1036. void drm_crtc_vblank_put(struct drm_crtc *crtc)
  1037. {
  1038. drm_vblank_put(crtc->dev, drm_crtc_index(crtc));
  1039. }
  1040. EXPORT_SYMBOL(drm_crtc_vblank_put);
  1041. /**
  1042. * drm_wait_one_vblank - wait for one vblank
  1043. * @dev: DRM device
  1044. * @pipe: CRTC index
  1045. *
  1046. * This waits for one vblank to pass on @pipe, using the irq driver interfaces.
  1047. * It is a failure to call this when the vblank irq for @pipe is disabled, e.g.
  1048. * due to lack of driver support or because the crtc is off.
  1049. */
  1050. void drm_wait_one_vblank(struct drm_device *dev, unsigned int pipe)
  1051. {
  1052. struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
  1053. int ret;
  1054. u32 last;
  1055. if (WARN_ON(pipe >= dev->num_crtcs))
  1056. return;
  1057. ret = drm_vblank_get(dev, pipe);
  1058. if (WARN(ret, "vblank not available on crtc %i, ret=%i\n", pipe, ret))
  1059. return;
  1060. last = drm_vblank_count(dev, pipe);
  1061. ret = wait_event_timeout(vblank->queue,
  1062. last != drm_vblank_count(dev, pipe),
  1063. msecs_to_jiffies(100));
  1064. WARN(ret == 0, "vblank wait timed out on crtc %i\n", pipe);
  1065. drm_vblank_put(dev, pipe);
  1066. }
  1067. EXPORT_SYMBOL(drm_wait_one_vblank);
  1068. /**
  1069. * drm_crtc_wait_one_vblank - wait for one vblank
  1070. * @crtc: DRM crtc
  1071. *
  1072. * This waits for one vblank to pass on @crtc, using the irq driver interfaces.
  1073. * It is a failure to call this when the vblank irq for @crtc is disabled, e.g.
  1074. * due to lack of driver support or because the crtc is off.
  1075. */
  1076. void drm_crtc_wait_one_vblank(struct drm_crtc *crtc)
  1077. {
  1078. drm_wait_one_vblank(crtc->dev, drm_crtc_index(crtc));
  1079. }
  1080. EXPORT_SYMBOL(drm_crtc_wait_one_vblank);
  1081. /**
  1082. * drm_crtc_vblank_off - disable vblank events on a CRTC
  1083. * @crtc: CRTC in question
  1084. *
  1085. * Drivers can use this function to shut down the vblank interrupt handling when
  1086. * disabling a crtc. This function ensures that the latest vblank frame count is
  1087. * stored so that drm_vblank_on can restore it again.
  1088. *
  1089. * Drivers must use this function when the hardware vblank counter can get
  1090. * reset, e.g. when suspending.
  1091. */
  1092. void drm_crtc_vblank_off(struct drm_crtc *crtc)
  1093. {
  1094. struct drm_device *dev = crtc->dev;
  1095. unsigned int pipe = drm_crtc_index(crtc);
  1096. struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
  1097. struct drm_pending_vblank_event *e, *t;
  1098. struct timeval now;
  1099. unsigned long irqflags;
  1100. unsigned int seq;
  1101. if (WARN_ON(pipe >= dev->num_crtcs))
  1102. return;
  1103. spin_lock_irqsave(&dev->event_lock, irqflags);
  1104. spin_lock(&dev->vbl_lock);
  1105. DRM_DEBUG_VBL("crtc %d, vblank enabled %d, inmodeset %d\n",
  1106. pipe, vblank->enabled, vblank->inmodeset);
  1107. /* Avoid redundant vblank disables without previous
  1108. * drm_crtc_vblank_on(). */
  1109. if (drm_core_check_feature(dev, DRIVER_ATOMIC) || !vblank->inmodeset)
  1110. vblank_disable_and_save(dev, pipe);
  1111. wake_up(&vblank->queue);
  1112. /*
  1113. * Prevent subsequent drm_vblank_get() from re-enabling
  1114. * the vblank interrupt by bumping the refcount.
  1115. */
  1116. if (!vblank->inmodeset) {
  1117. atomic_inc(&vblank->refcount);
  1118. vblank->inmodeset = 1;
  1119. }
  1120. spin_unlock(&dev->vbl_lock);
  1121. /* Send any queued vblank events, lest the natives grow disquiet */
  1122. seq = drm_vblank_count_and_time(dev, pipe, &now);
  1123. list_for_each_entry_safe(e, t, &dev->vblank_event_list, base.link) {
  1124. if (e->pipe != pipe)
  1125. continue;
  1126. DRM_DEBUG("Sending premature vblank event on disable: "
  1127. "wanted %u, current %u\n",
  1128. e->event.sequence, seq);
  1129. list_del(&e->base.link);
  1130. drm_vblank_put(dev, pipe);
  1131. send_vblank_event(dev, e, seq, &now);
  1132. }
  1133. spin_unlock_irqrestore(&dev->event_lock, irqflags);
  1134. }
  1135. EXPORT_SYMBOL(drm_crtc_vblank_off);
  1136. /**
  1137. * drm_crtc_vblank_reset - reset vblank state to off on a CRTC
  1138. * @crtc: CRTC in question
  1139. *
  1140. * Drivers can use this function to reset the vblank state to off at load time.
  1141. * Drivers should use this together with the drm_crtc_vblank_off() and
  1142. * drm_crtc_vblank_on() functions. The difference compared to
  1143. * drm_crtc_vblank_off() is that this function doesn't save the vblank counter
  1144. * and hence doesn't need to call any driver hooks.
  1145. */
  1146. void drm_crtc_vblank_reset(struct drm_crtc *crtc)
  1147. {
  1148. struct drm_device *dev = crtc->dev;
  1149. unsigned long irqflags;
  1150. unsigned int pipe = drm_crtc_index(crtc);
  1151. struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
  1152. spin_lock_irqsave(&dev->vbl_lock, irqflags);
  1153. /*
  1154. * Prevent subsequent drm_vblank_get() from enabling the vblank
  1155. * interrupt by bumping the refcount.
  1156. */
  1157. if (!vblank->inmodeset) {
  1158. atomic_inc(&vblank->refcount);
  1159. vblank->inmodeset = 1;
  1160. }
  1161. spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
  1162. WARN_ON(!list_empty(&dev->vblank_event_list));
  1163. }
  1164. EXPORT_SYMBOL(drm_crtc_vblank_reset);
  1165. /**
  1166. * drm_crtc_vblank_on - enable vblank events on a CRTC
  1167. * @crtc: CRTC in question
  1168. *
  1169. * This functions restores the vblank interrupt state captured with
  1170. * drm_crtc_vblank_off() again. Note that calls to drm_crtc_vblank_on() and
  1171. * drm_crtc_vblank_off() can be unbalanced and so can also be unconditionally called
  1172. * in driver load code to reflect the current hardware state of the crtc.
  1173. */
  1174. void drm_crtc_vblank_on(struct drm_crtc *crtc)
  1175. {
  1176. struct drm_device *dev = crtc->dev;
  1177. unsigned int pipe = drm_crtc_index(crtc);
  1178. struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
  1179. unsigned long irqflags;
  1180. if (WARN_ON(pipe >= dev->num_crtcs))
  1181. return;
  1182. spin_lock_irqsave(&dev->vbl_lock, irqflags);
  1183. DRM_DEBUG_VBL("crtc %d, vblank enabled %d, inmodeset %d\n",
  1184. pipe, vblank->enabled, vblank->inmodeset);
  1185. /* Drop our private "prevent drm_vblank_get" refcount */
  1186. if (vblank->inmodeset) {
  1187. atomic_dec(&vblank->refcount);
  1188. vblank->inmodeset = 0;
  1189. }
  1190. drm_reset_vblank_timestamp(dev, pipe);
  1191. /*
  1192. * re-enable interrupts if there are users left, or the
  1193. * user wishes vblank interrupts to be enabled all the time.
  1194. */
  1195. if (atomic_read(&vblank->refcount) != 0 || drm_vblank_offdelay == 0)
  1196. WARN_ON(drm_vblank_enable(dev, pipe));
  1197. spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
  1198. }
  1199. EXPORT_SYMBOL(drm_crtc_vblank_on);
  1200. static void drm_legacy_vblank_pre_modeset(struct drm_device *dev,
  1201. unsigned int pipe)
  1202. {
  1203. struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
  1204. /* vblank is not initialized (IRQ not installed ?), or has been freed */
  1205. if (!dev->num_crtcs)
  1206. return;
  1207. if (WARN_ON(pipe >= dev->num_crtcs))
  1208. return;
  1209. /*
  1210. * To avoid all the problems that might happen if interrupts
  1211. * were enabled/disabled around or between these calls, we just
  1212. * have the kernel take a reference on the CRTC (just once though
  1213. * to avoid corrupting the count if multiple, mismatch calls occur),
  1214. * so that interrupts remain enabled in the interim.
  1215. */
  1216. if (!vblank->inmodeset) {
  1217. vblank->inmodeset = 0x1;
  1218. if (drm_vblank_get(dev, pipe) == 0)
  1219. vblank->inmodeset |= 0x2;
  1220. }
  1221. }
  1222. static void drm_legacy_vblank_post_modeset(struct drm_device *dev,
  1223. unsigned int pipe)
  1224. {
  1225. struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
  1226. unsigned long irqflags;
  1227. /* vblank is not initialized (IRQ not installed ?), or has been freed */
  1228. if (!dev->num_crtcs)
  1229. return;
  1230. if (WARN_ON(pipe >= dev->num_crtcs))
  1231. return;
  1232. if (vblank->inmodeset) {
  1233. spin_lock_irqsave(&dev->vbl_lock, irqflags);
  1234. drm_reset_vblank_timestamp(dev, pipe);
  1235. spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
  1236. if (vblank->inmodeset & 0x2)
  1237. drm_vblank_put(dev, pipe);
  1238. vblank->inmodeset = 0;
  1239. }
  1240. }
  1241. /*
  1242. * drm_modeset_ctl - handle vblank event counter changes across mode switch
  1243. * @DRM_IOCTL_ARGS: standard ioctl arguments
  1244. *
  1245. * Applications should call the %_DRM_PRE_MODESET and %_DRM_POST_MODESET
  1246. * ioctls around modesetting so that any lost vblank events are accounted for.
  1247. *
  1248. * Generally the counter will reset across mode sets. If interrupts are
  1249. * enabled around this call, we don't have to do anything since the counter
  1250. * will have already been incremented.
  1251. */
  1252. int drm_modeset_ctl(struct drm_device *dev, void *data,
  1253. struct drm_file *file_priv)
  1254. {
  1255. struct drm_modeset_ctl *modeset = data;
  1256. unsigned int pipe;
  1257. /* If drm_vblank_init() hasn't been called yet, just no-op */
  1258. if (!dev->num_crtcs)
  1259. return 0;
  1260. /* KMS drivers handle this internally */
  1261. if (!drm_core_check_feature(dev, DRIVER_LEGACY))
  1262. return 0;
  1263. pipe = modeset->crtc;
  1264. if (pipe >= dev->num_crtcs)
  1265. return -EINVAL;
  1266. switch (modeset->cmd) {
  1267. case _DRM_PRE_MODESET:
  1268. drm_legacy_vblank_pre_modeset(dev, pipe);
  1269. break;
  1270. case _DRM_POST_MODESET:
  1271. drm_legacy_vblank_post_modeset(dev, pipe);
  1272. break;
  1273. default:
  1274. return -EINVAL;
  1275. }
  1276. return 0;
  1277. }
  1278. static int drm_queue_vblank_event(struct drm_device *dev, unsigned int pipe,
  1279. union drm_wait_vblank *vblwait,
  1280. struct drm_file *file_priv)
  1281. {
  1282. struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
  1283. struct drm_pending_vblank_event *e;
  1284. struct timeval now;
  1285. unsigned long flags;
  1286. unsigned int seq;
  1287. int ret;
  1288. e = kzalloc(sizeof(*e), GFP_KERNEL);
  1289. if (e == NULL) {
  1290. ret = -ENOMEM;
  1291. goto err_put;
  1292. }
  1293. e->pipe = pipe;
  1294. e->base.pid = current->pid;
  1295. e->event.base.type = DRM_EVENT_VBLANK;
  1296. e->event.base.length = sizeof(e->event);
  1297. e->event.user_data = vblwait->request.signal;
  1298. spin_lock_irqsave(&dev->event_lock, flags);
  1299. /*
  1300. * drm_crtc_vblank_off() might have been called after we called
  1301. * drm_vblank_get(). drm_crtc_vblank_off() holds event_lock around the
  1302. * vblank disable, so no need for further locking. The reference from
  1303. * drm_vblank_get() protects against vblank disable from another source.
  1304. */
  1305. if (!vblank->enabled) {
  1306. ret = -EINVAL;
  1307. goto err_unlock;
  1308. }
  1309. ret = drm_event_reserve_init_locked(dev, file_priv, &e->base,
  1310. &e->event.base);
  1311. if (ret)
  1312. goto err_unlock;
  1313. seq = drm_vblank_count_and_time(dev, pipe, &now);
  1314. DRM_DEBUG("event on vblank count %u, current %u, crtc %u\n",
  1315. vblwait->request.sequence, seq, pipe);
  1316. trace_drm_vblank_event_queued(current->pid, pipe,
  1317. vblwait->request.sequence);
  1318. e->event.sequence = vblwait->request.sequence;
  1319. if ((seq - vblwait->request.sequence) <= (1 << 23)) {
  1320. drm_vblank_put(dev, pipe);
  1321. send_vblank_event(dev, e, seq, &now);
  1322. vblwait->reply.sequence = seq;
  1323. } else {
  1324. /* drm_handle_vblank_events will call drm_vblank_put */
  1325. list_add_tail(&e->base.link, &dev->vblank_event_list);
  1326. vblwait->reply.sequence = vblwait->request.sequence;
  1327. }
  1328. spin_unlock_irqrestore(&dev->event_lock, flags);
  1329. return 0;
  1330. err_unlock:
  1331. spin_unlock_irqrestore(&dev->event_lock, flags);
  1332. kfree(e);
  1333. err_put:
  1334. drm_vblank_put(dev, pipe);
  1335. return ret;
  1336. }
  1337. /*
  1338. * Wait for VBLANK.
  1339. *
  1340. * \param inode device inode.
  1341. * \param file_priv DRM file private.
  1342. * \param cmd command.
  1343. * \param data user argument, pointing to a drm_wait_vblank structure.
  1344. * \return zero on success or a negative number on failure.
  1345. *
  1346. * This function enables the vblank interrupt on the pipe requested, then
  1347. * sleeps waiting for the requested sequence number to occur, and drops
  1348. * the vblank interrupt refcount afterwards. (vblank IRQ disable follows that
  1349. * after a timeout with no further vblank waits scheduled).
  1350. */
  1351. int drm_wait_vblank(struct drm_device *dev, void *data,
  1352. struct drm_file *file_priv)
  1353. {
  1354. struct drm_vblank_crtc *vblank;
  1355. union drm_wait_vblank *vblwait = data;
  1356. int ret;
  1357. unsigned int flags, seq, pipe, high_pipe;
  1358. if (!dev->irq_enabled)
  1359. return -EINVAL;
  1360. if (vblwait->request.type & _DRM_VBLANK_SIGNAL)
  1361. return -EINVAL;
  1362. if (vblwait->request.type &
  1363. ~(_DRM_VBLANK_TYPES_MASK | _DRM_VBLANK_FLAGS_MASK |
  1364. _DRM_VBLANK_HIGH_CRTC_MASK)) {
  1365. DRM_ERROR("Unsupported type value 0x%x, supported mask 0x%x\n",
  1366. vblwait->request.type,
  1367. (_DRM_VBLANK_TYPES_MASK | _DRM_VBLANK_FLAGS_MASK |
  1368. _DRM_VBLANK_HIGH_CRTC_MASK));
  1369. return -EINVAL;
  1370. }
  1371. flags = vblwait->request.type & _DRM_VBLANK_FLAGS_MASK;
  1372. high_pipe = (vblwait->request.type & _DRM_VBLANK_HIGH_CRTC_MASK);
  1373. if (high_pipe)
  1374. pipe = high_pipe >> _DRM_VBLANK_HIGH_CRTC_SHIFT;
  1375. else
  1376. pipe = flags & _DRM_VBLANK_SECONDARY ? 1 : 0;
  1377. if (pipe >= dev->num_crtcs)
  1378. return -EINVAL;
  1379. vblank = &dev->vblank[pipe];
  1380. ret = drm_vblank_get(dev, pipe);
  1381. if (ret) {
  1382. DRM_DEBUG("failed to acquire vblank counter, %d\n", ret);
  1383. return ret;
  1384. }
  1385. seq = drm_vblank_count(dev, pipe);
  1386. switch (vblwait->request.type & _DRM_VBLANK_TYPES_MASK) {
  1387. case _DRM_VBLANK_RELATIVE:
  1388. vblwait->request.sequence += seq;
  1389. vblwait->request.type &= ~_DRM_VBLANK_RELATIVE;
  1390. case _DRM_VBLANK_ABSOLUTE:
  1391. break;
  1392. default:
  1393. ret = -EINVAL;
  1394. goto done;
  1395. }
  1396. if ((flags & _DRM_VBLANK_NEXTONMISS) &&
  1397. (seq - vblwait->request.sequence) <= (1 << 23)) {
  1398. vblwait->request.sequence = seq + 1;
  1399. }
  1400. if (flags & _DRM_VBLANK_EVENT) {
  1401. /* must hold on to the vblank ref until the event fires
  1402. * drm_vblank_put will be called asynchronously
  1403. */
  1404. return drm_queue_vblank_event(dev, pipe, vblwait, file_priv);
  1405. }
  1406. DRM_DEBUG("waiting on vblank count %u, crtc %u\n",
  1407. vblwait->request.sequence, pipe);
  1408. DRM_WAIT_ON(ret, vblank->queue, 3 * HZ,
  1409. (((drm_vblank_count(dev, pipe) -
  1410. vblwait->request.sequence) <= (1 << 23)) ||
  1411. !vblank->enabled ||
  1412. !dev->irq_enabled));
  1413. if (ret != -EINTR) {
  1414. struct timeval now;
  1415. vblwait->reply.sequence = drm_vblank_count_and_time(dev, pipe, &now);
  1416. vblwait->reply.tval_sec = now.tv_sec;
  1417. vblwait->reply.tval_usec = now.tv_usec;
  1418. DRM_DEBUG("returning %u to client\n",
  1419. vblwait->reply.sequence);
  1420. } else {
  1421. DRM_DEBUG("vblank wait interrupted by signal\n");
  1422. }
  1423. done:
  1424. drm_vblank_put(dev, pipe);
  1425. return ret;
  1426. }
  1427. static void drm_handle_vblank_events(struct drm_device *dev, unsigned int pipe)
  1428. {
  1429. struct drm_pending_vblank_event *e, *t;
  1430. struct timeval now;
  1431. unsigned int seq;
  1432. assert_spin_locked(&dev->event_lock);
  1433. seq = drm_vblank_count_and_time(dev, pipe, &now);
  1434. list_for_each_entry_safe(e, t, &dev->vblank_event_list, base.link) {
  1435. if (e->pipe != pipe)
  1436. continue;
  1437. if ((seq - e->event.sequence) > (1<<23))
  1438. continue;
  1439. DRM_DEBUG("vblank event on %u, current %u\n",
  1440. e->event.sequence, seq);
  1441. list_del(&e->base.link);
  1442. drm_vblank_put(dev, pipe);
  1443. send_vblank_event(dev, e, seq, &now);
  1444. }
  1445. trace_drm_vblank_event(pipe, seq);
  1446. }
  1447. /**
  1448. * drm_handle_vblank - handle a vblank event
  1449. * @dev: DRM device
  1450. * @pipe: index of CRTC where this event occurred
  1451. *
  1452. * Drivers should call this routine in their vblank interrupt handlers to
  1453. * update the vblank counter and send any signals that may be pending.
  1454. *
  1455. * This is the legacy version of drm_crtc_handle_vblank().
  1456. */
  1457. bool drm_handle_vblank(struct drm_device *dev, unsigned int pipe)
  1458. {
  1459. struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
  1460. unsigned long irqflags;
  1461. if (WARN_ON_ONCE(!dev->num_crtcs))
  1462. return false;
  1463. if (WARN_ON(pipe >= dev->num_crtcs))
  1464. return false;
  1465. spin_lock_irqsave(&dev->event_lock, irqflags);
  1466. /* Need timestamp lock to prevent concurrent execution with
  1467. * vblank enable/disable, as this would cause inconsistent
  1468. * or corrupted timestamps and vblank counts.
  1469. */
  1470. spin_lock(&dev->vblank_time_lock);
  1471. /* Vblank irq handling disabled. Nothing to do. */
  1472. if (!vblank->enabled) {
  1473. spin_unlock(&dev->vblank_time_lock);
  1474. spin_unlock_irqrestore(&dev->event_lock, irqflags);
  1475. return false;
  1476. }
  1477. drm_update_vblank_count(dev, pipe, DRM_CALLED_FROM_VBLIRQ);
  1478. spin_unlock(&dev->vblank_time_lock);
  1479. wake_up(&vblank->queue);
  1480. drm_handle_vblank_events(dev, pipe);
  1481. spin_unlock_irqrestore(&dev->event_lock, irqflags);
  1482. return true;
  1483. }
  1484. EXPORT_SYMBOL(drm_handle_vblank);
  1485. /**
  1486. * drm_crtc_handle_vblank - handle a vblank event
  1487. * @crtc: where this event occurred
  1488. *
  1489. * Drivers should call this routine in their vblank interrupt handlers to
  1490. * update the vblank counter and send any signals that may be pending.
  1491. *
  1492. * This is the native KMS version of drm_handle_vblank().
  1493. *
  1494. * Returns:
  1495. * True if the event was successfully handled, false on failure.
  1496. */
  1497. bool drm_crtc_handle_vblank(struct drm_crtc *crtc)
  1498. {
  1499. return drm_handle_vblank(crtc->dev, drm_crtc_index(crtc));
  1500. }
  1501. EXPORT_SYMBOL(drm_crtc_handle_vblank);
  1502. /**
  1503. * drm_vblank_no_hw_counter - "No hw counter" implementation of .get_vblank_counter()
  1504. * @dev: DRM device
  1505. * @pipe: CRTC for which to read the counter
  1506. *
  1507. * Drivers can plug this into the .get_vblank_counter() function if
  1508. * there is no useable hardware frame counter available.
  1509. *
  1510. * Returns:
  1511. * 0
  1512. */
  1513. u32 drm_vblank_no_hw_counter(struct drm_device *dev, unsigned int pipe)
  1514. {
  1515. WARN_ON_ONCE(dev->max_vblank_count != 0);
  1516. return 0;
  1517. }
  1518. EXPORT_SYMBOL(drm_vblank_no_hw_counter);