drm_irq.c 52 KB

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