drm_irq.c 52 KB

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