drm_irq.c 53 KB

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