drm_irq.c 54 KB

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