drm_irq.c 52 KB

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