drm_irq.c 55 KB

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