drm_irq.c 53 KB

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