drm_irq.c 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805
  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. 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, 0);
  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. DRM_DEBUG("crtc %u: hwmode: htotal %d, vtotal %d, vdisplay %d\n",
  573. crtc->base.id, mode->crtc_htotal,
  574. mode->crtc_vtotal, mode->crtc_vdisplay);
  575. DRM_DEBUG("crtc %u: clock %d kHz framedur %d linedur %d\n",
  576. crtc->base.id, dotclock, framedur_ns, linedur_ns);
  577. }
  578. EXPORT_SYMBOL(drm_calc_timestamping_constants);
  579. /**
  580. * drm_calc_vbltimestamp_from_scanoutpos - precise vblank timestamp helper
  581. * @dev: DRM device
  582. * @pipe: index of CRTC whose vblank timestamp to retrieve
  583. * @max_error: Desired maximum allowable error in timestamps (nanosecs)
  584. * On return contains true maximum error of timestamp
  585. * @vblank_time: Pointer to struct timeval which should receive the timestamp
  586. * @flags: Flags to pass to driver:
  587. * 0 = Default,
  588. * DRM_CALLED_FROM_VBLIRQ = If function is called from vbl IRQ handler
  589. * @mode: mode which defines the scanout timings
  590. *
  591. * Implements calculation of exact vblank timestamps from given drm_display_mode
  592. * timings and current video scanout position of a CRTC. This can be called from
  593. * within get_vblank_timestamp() implementation of a kms driver to implement the
  594. * actual timestamping.
  595. *
  596. * Should return timestamps conforming to the OML_sync_control OpenML
  597. * extension specification. The timestamp corresponds to the end of
  598. * the vblank interval, aka start of scanout of topmost-leftmost display
  599. * pixel in the following video frame.
  600. *
  601. * Requires support for optional dev->driver->get_scanout_position()
  602. * in kms driver, plus a bit of setup code to provide a drm_display_mode
  603. * that corresponds to the true scanout timing.
  604. *
  605. * The current implementation only handles standard video modes. It
  606. * returns as no operation if a doublescan or interlaced video mode is
  607. * active. Higher level code is expected to handle this.
  608. *
  609. * Returns:
  610. * Negative value on error, failure or if not supported in current
  611. * video mode:
  612. *
  613. * -EINVAL Invalid CRTC.
  614. * -EAGAIN Temporary unavailable, e.g., called before initial modeset.
  615. * -ENOTSUPP Function not supported in current display mode.
  616. * -EIO Failed, e.g., due to failed scanout position query.
  617. *
  618. * Returns or'ed positive status flags on success:
  619. *
  620. * DRM_VBLANKTIME_SCANOUTPOS_METHOD - Signal this method used for timestamping.
  621. * DRM_VBLANKTIME_INVBL - Timestamp taken while scanout was in vblank interval.
  622. *
  623. */
  624. int drm_calc_vbltimestamp_from_scanoutpos(struct drm_device *dev,
  625. unsigned int pipe,
  626. int *max_error,
  627. struct timeval *vblank_time,
  628. unsigned flags,
  629. const struct drm_display_mode *mode)
  630. {
  631. struct timeval tv_etime;
  632. ktime_t stime, etime;
  633. unsigned int vbl_status;
  634. int ret = DRM_VBLANKTIME_SCANOUTPOS_METHOD;
  635. int vpos, hpos, i;
  636. int delta_ns, duration_ns;
  637. if (pipe >= dev->num_crtcs) {
  638. DRM_ERROR("Invalid crtc %u\n", pipe);
  639. return -EINVAL;
  640. }
  641. /* Scanout position query not supported? Should not happen. */
  642. if (!dev->driver->get_scanout_position) {
  643. DRM_ERROR("Called from driver w/o get_scanout_position()!?\n");
  644. return -EIO;
  645. }
  646. /* If mode timing undefined, just return as no-op:
  647. * Happens during initial modesetting of a crtc.
  648. */
  649. if (mode->crtc_clock == 0) {
  650. DRM_DEBUG("crtc %u: Noop due to uninitialized mode.\n", pipe);
  651. return -EAGAIN;
  652. }
  653. /* Get current scanout position with system timestamp.
  654. * Repeat query up to DRM_TIMESTAMP_MAXRETRIES times
  655. * if single query takes longer than max_error nanoseconds.
  656. *
  657. * This guarantees a tight bound on maximum error if
  658. * code gets preempted or delayed for some reason.
  659. */
  660. for (i = 0; i < DRM_TIMESTAMP_MAXRETRIES; i++) {
  661. /*
  662. * Get vertical and horizontal scanout position vpos, hpos,
  663. * and bounding timestamps stime, etime, pre/post query.
  664. */
  665. vbl_status = dev->driver->get_scanout_position(dev, pipe, flags,
  666. &vpos, &hpos,
  667. &stime, &etime,
  668. mode);
  669. /* Return as no-op if scanout query unsupported or failed. */
  670. if (!(vbl_status & DRM_SCANOUTPOS_VALID)) {
  671. DRM_DEBUG("crtc %u : scanoutpos query failed [0x%x].\n",
  672. pipe, vbl_status);
  673. return -EIO;
  674. }
  675. /* Compute uncertainty in timestamp of scanout position query. */
  676. duration_ns = ktime_to_ns(etime) - ktime_to_ns(stime);
  677. /* Accept result with < max_error nsecs timing uncertainty. */
  678. if (duration_ns <= *max_error)
  679. break;
  680. }
  681. /* Noisy system timing? */
  682. if (i == DRM_TIMESTAMP_MAXRETRIES) {
  683. DRM_DEBUG("crtc %u: Noisy timestamp %d us > %d us [%d reps].\n",
  684. pipe, duration_ns/1000, *max_error/1000, i);
  685. }
  686. /* Return upper bound of timestamp precision error. */
  687. *max_error = duration_ns;
  688. /* Convert scanout position into elapsed time at raw_time query
  689. * since start of scanout at first display scanline. delta_ns
  690. * can be negative if start of scanout hasn't happened yet.
  691. */
  692. delta_ns = div_s64(1000000LL * (vpos * mode->crtc_htotal + hpos),
  693. mode->crtc_clock);
  694. if (!drm_timestamp_monotonic)
  695. etime = ktime_mono_to_real(etime);
  696. /* save this only for debugging purposes */
  697. tv_etime = ktime_to_timeval(etime);
  698. /* Subtract time delta from raw timestamp to get final
  699. * vblank_time timestamp for end of vblank.
  700. */
  701. etime = ktime_sub_ns(etime, delta_ns);
  702. *vblank_time = ktime_to_timeval(etime);
  703. DRM_DEBUG_VBL("crtc %u : v 0x%x p(%d,%d)@ %ld.%ld -> %ld.%ld [e %d us, %d rep]\n",
  704. pipe, vbl_status, hpos, vpos,
  705. (long)tv_etime.tv_sec, (long)tv_etime.tv_usec,
  706. (long)vblank_time->tv_sec, (long)vblank_time->tv_usec,
  707. duration_ns/1000, i);
  708. return ret;
  709. }
  710. EXPORT_SYMBOL(drm_calc_vbltimestamp_from_scanoutpos);
  711. static struct timeval get_drm_timestamp(void)
  712. {
  713. ktime_t now;
  714. now = drm_timestamp_monotonic ? ktime_get() : ktime_get_real();
  715. return ktime_to_timeval(now);
  716. }
  717. /**
  718. * drm_get_last_vbltimestamp - retrieve raw timestamp for the most recent
  719. * vblank interval
  720. * @dev: DRM device
  721. * @pipe: index of CRTC whose vblank timestamp to retrieve
  722. * @tvblank: Pointer to target struct timeval which should receive the timestamp
  723. * @flags: Flags to pass to driver:
  724. * 0 = Default,
  725. * DRM_CALLED_FROM_VBLIRQ = If function is called from vbl IRQ handler
  726. *
  727. * Fetches the system timestamp corresponding to the time of the most recent
  728. * vblank interval on specified CRTC. May call into kms-driver to
  729. * compute the timestamp with a high-precision GPU specific method.
  730. *
  731. * Returns zero if timestamp originates from uncorrected do_gettimeofday()
  732. * call, i.e., it isn't very precisely locked to the true vblank.
  733. *
  734. * Returns:
  735. * True if timestamp is considered to be very precise, false otherwise.
  736. */
  737. static bool
  738. drm_get_last_vbltimestamp(struct drm_device *dev, unsigned int pipe,
  739. struct timeval *tvblank, unsigned flags)
  740. {
  741. int ret;
  742. /* Define requested maximum error on timestamps (nanoseconds). */
  743. int max_error = (int) drm_timestamp_precision * 1000;
  744. /* Query driver if possible and precision timestamping enabled. */
  745. if (dev->driver->get_vblank_timestamp && (max_error > 0)) {
  746. ret = dev->driver->get_vblank_timestamp(dev, pipe, &max_error,
  747. tvblank, flags);
  748. if (ret > 0)
  749. return true;
  750. }
  751. /* GPU high precision timestamp query unsupported or failed.
  752. * Return current monotonic/gettimeofday timestamp as best estimate.
  753. */
  754. *tvblank = get_drm_timestamp();
  755. return false;
  756. }
  757. /**
  758. * drm_crtc_vblank_count - retrieve "cooked" vblank counter value
  759. * @crtc: which counter to retrieve
  760. *
  761. * Fetches the "cooked" vblank count value that represents the number of
  762. * vblank events since the system was booted, including lost events due to
  763. * modesetting activity.
  764. *
  765. * Returns:
  766. * The software vblank counter.
  767. */
  768. u32 drm_crtc_vblank_count(struct drm_crtc *crtc)
  769. {
  770. return drm_vblank_count(crtc->dev, drm_crtc_index(crtc));
  771. }
  772. EXPORT_SYMBOL(drm_crtc_vblank_count);
  773. /**
  774. * drm_vblank_count_and_time - retrieve "cooked" vblank counter value and the
  775. * system timestamp corresponding to that vblank counter value.
  776. * @dev: DRM device
  777. * @pipe: index of CRTC whose counter to retrieve
  778. * @vblanktime: Pointer to struct timeval to receive the vblank timestamp.
  779. *
  780. * Fetches the "cooked" vblank count value that represents the number of
  781. * vblank events since the system was booted, including lost events due to
  782. * modesetting activity. Returns corresponding system timestamp of the time
  783. * of the vblank interval that corresponds to the current vblank counter value.
  784. *
  785. * This is the legacy version of drm_crtc_vblank_count_and_time().
  786. */
  787. static u32 drm_vblank_count_and_time(struct drm_device *dev, unsigned int pipe,
  788. struct timeval *vblanktime)
  789. {
  790. struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
  791. u32 vblank_count;
  792. unsigned int seq;
  793. if (WARN_ON(pipe >= dev->num_crtcs)) {
  794. *vblanktime = (struct timeval) { 0 };
  795. return 0;
  796. }
  797. do {
  798. seq = read_seqbegin(&vblank->seqlock);
  799. vblank_count = vblank->count;
  800. *vblanktime = vblank->time;
  801. } while (read_seqretry(&vblank->seqlock, seq));
  802. return vblank_count;
  803. }
  804. /**
  805. * drm_crtc_vblank_count_and_time - retrieve "cooked" vblank counter value
  806. * and the system timestamp corresponding to that vblank counter value
  807. * @crtc: which counter to retrieve
  808. * @vblanktime: Pointer to struct timeval to receive the vblank timestamp.
  809. *
  810. * Fetches the "cooked" vblank count value that represents the number of
  811. * vblank events since the system was booted, including lost events due to
  812. * modesetting activity. Returns corresponding system timestamp of the time
  813. * of the vblank interval that corresponds to the current vblank counter value.
  814. */
  815. u32 drm_crtc_vblank_count_and_time(struct drm_crtc *crtc,
  816. struct timeval *vblanktime)
  817. {
  818. return drm_vblank_count_and_time(crtc->dev, drm_crtc_index(crtc),
  819. vblanktime);
  820. }
  821. EXPORT_SYMBOL(drm_crtc_vblank_count_and_time);
  822. static void send_vblank_event(struct drm_device *dev,
  823. struct drm_pending_vblank_event *e,
  824. unsigned long seq, struct timeval *now)
  825. {
  826. e->event.sequence = seq;
  827. e->event.tv_sec = now->tv_sec;
  828. e->event.tv_usec = now->tv_usec;
  829. trace_drm_vblank_event_delivered(e->base.file_priv, e->pipe,
  830. e->event.sequence);
  831. drm_send_event_locked(dev, &e->base);
  832. }
  833. /**
  834. * drm_crtc_arm_vblank_event - arm vblank event after pageflip
  835. * @crtc: the source CRTC of the vblank event
  836. * @e: the event to send
  837. *
  838. * A lot of drivers need to generate vblank events for the very next vblank
  839. * interrupt. For example when the page flip interrupt happens when the page
  840. * flip gets armed, but not when it actually executes within the next vblank
  841. * period. This helper function implements exactly the required vblank arming
  842. * behaviour.
  843. *
  844. * NOTE: Drivers using this to send out the &drm_crtc_state.event as part of an
  845. * atomic commit must ensure that the next vblank happens at exactly the same
  846. * time as the atomic commit is committed to the hardware. This function itself
  847. * does **not** protect again the next vblank interrupt racing with either this
  848. * function call or the atomic commit operation. A possible sequence could be:
  849. *
  850. * 1. Driver commits new hardware state into vblank-synchronized registers.
  851. * 2. A vblank happens, committing the hardware state. Also the corresponding
  852. * vblank interrupt is fired off and fully processed by the interrupt
  853. * handler.
  854. * 3. The atomic commit operation proceeds to call drm_crtc_arm_vblank_event().
  855. * 4. The event is only send out for the next vblank, which is wrong.
  856. *
  857. * An equivalent race can happen when the driver calls
  858. * drm_crtc_arm_vblank_event() before writing out the new hardware state.
  859. *
  860. * The only way to make this work safely is to prevent the vblank from firing
  861. * (and the hardware from committing anything else) until the entire atomic
  862. * commit sequence has run to completion. If the hardware does not have such a
  863. * feature (e.g. using a "go" bit), then it is unsafe to use this functions.
  864. * Instead drivers need to manually send out the event from their interrupt
  865. * handler by calling drm_crtc_send_vblank_event() and make sure that there's no
  866. * possible race with the hardware committing the atomic update.
  867. *
  868. * Caller must hold event lock. Caller must also hold a vblank reference for
  869. * the event @e, which will be dropped when the next vblank arrives.
  870. */
  871. void drm_crtc_arm_vblank_event(struct drm_crtc *crtc,
  872. struct drm_pending_vblank_event *e)
  873. {
  874. struct drm_device *dev = crtc->dev;
  875. unsigned int pipe = drm_crtc_index(crtc);
  876. assert_spin_locked(&dev->event_lock);
  877. e->pipe = pipe;
  878. e->event.sequence = drm_vblank_count(dev, pipe);
  879. e->event.crtc_id = crtc->base.id;
  880. list_add_tail(&e->base.link, &dev->vblank_event_list);
  881. }
  882. EXPORT_SYMBOL(drm_crtc_arm_vblank_event);
  883. /**
  884. * drm_crtc_send_vblank_event - helper to send vblank event after pageflip
  885. * @crtc: the source CRTC of the vblank event
  886. * @e: the event to send
  887. *
  888. * Updates sequence # and timestamp on event for the most recently processed
  889. * vblank, and sends it to userspace. Caller must hold event lock.
  890. *
  891. * See drm_crtc_arm_vblank_event() for a helper which can be used in certain
  892. * situation, especially to send out events for atomic commit operations.
  893. */
  894. void drm_crtc_send_vblank_event(struct drm_crtc *crtc,
  895. struct drm_pending_vblank_event *e)
  896. {
  897. struct drm_device *dev = crtc->dev;
  898. unsigned int seq, pipe = drm_crtc_index(crtc);
  899. struct timeval now;
  900. if (dev->num_crtcs > 0) {
  901. seq = drm_vblank_count_and_time(dev, pipe, &now);
  902. } else {
  903. seq = 0;
  904. now = get_drm_timestamp();
  905. }
  906. e->pipe = pipe;
  907. e->event.crtc_id = crtc->base.id;
  908. send_vblank_event(dev, e, seq, &now);
  909. }
  910. EXPORT_SYMBOL(drm_crtc_send_vblank_event);
  911. static int __enable_vblank(struct drm_device *dev, unsigned int pipe)
  912. {
  913. if (drm_core_check_feature(dev, DRIVER_MODESET)) {
  914. struct drm_crtc *crtc = drm_crtc_from_index(dev, pipe);
  915. if (crtc->funcs->enable_vblank)
  916. return crtc->funcs->enable_vblank(crtc);
  917. }
  918. return dev->driver->enable_vblank(dev, pipe);
  919. }
  920. /**
  921. * drm_vblank_enable - enable the vblank interrupt on a CRTC
  922. * @dev: DRM device
  923. * @pipe: CRTC index
  924. *
  925. * Returns:
  926. * Zero on success or a negative error code on failure.
  927. */
  928. static int drm_vblank_enable(struct drm_device *dev, unsigned int pipe)
  929. {
  930. struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
  931. int ret = 0;
  932. assert_spin_locked(&dev->vbl_lock);
  933. spin_lock(&dev->vblank_time_lock);
  934. if (!vblank->enabled) {
  935. /*
  936. * Enable vblank irqs under vblank_time_lock protection.
  937. * All vblank count & timestamp updates are held off
  938. * until we are done reinitializing master counter and
  939. * timestamps. Filtercode in drm_handle_vblank() will
  940. * prevent double-accounting of same vblank interval.
  941. */
  942. ret = __enable_vblank(dev, pipe);
  943. DRM_DEBUG("enabling vblank on crtc %u, ret: %d\n", pipe, ret);
  944. if (ret) {
  945. atomic_dec(&vblank->refcount);
  946. } else {
  947. drm_update_vblank_count(dev, pipe, 0);
  948. /* drm_update_vblank_count() includes a wmb so we just
  949. * need to ensure that the compiler emits the write
  950. * to mark the vblank as enabled after the call
  951. * to drm_update_vblank_count().
  952. */
  953. WRITE_ONCE(vblank->enabled, true);
  954. }
  955. }
  956. spin_unlock(&dev->vblank_time_lock);
  957. return ret;
  958. }
  959. /**
  960. * drm_vblank_get - get a reference count on vblank events
  961. * @dev: DRM device
  962. * @pipe: index of CRTC to own
  963. *
  964. * Acquire a reference count on vblank events to avoid having them disabled
  965. * while in use.
  966. *
  967. * This is the legacy version of drm_crtc_vblank_get().
  968. *
  969. * Returns:
  970. * Zero on success or a negative error code on failure.
  971. */
  972. static int drm_vblank_get(struct drm_device *dev, unsigned int pipe)
  973. {
  974. struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
  975. unsigned long irqflags;
  976. int ret = 0;
  977. if (!dev->num_crtcs)
  978. return -EINVAL;
  979. if (WARN_ON(pipe >= dev->num_crtcs))
  980. return -EINVAL;
  981. spin_lock_irqsave(&dev->vbl_lock, irqflags);
  982. /* Going from 0->1 means we have to enable interrupts again */
  983. if (atomic_add_return(1, &vblank->refcount) == 1) {
  984. ret = drm_vblank_enable(dev, pipe);
  985. } else {
  986. if (!vblank->enabled) {
  987. atomic_dec(&vblank->refcount);
  988. ret = -EINVAL;
  989. }
  990. }
  991. spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
  992. return ret;
  993. }
  994. /**
  995. * drm_crtc_vblank_get - get a reference count on vblank events
  996. * @crtc: which CRTC to own
  997. *
  998. * Acquire a reference count on vblank events to avoid having them disabled
  999. * while in use.
  1000. *
  1001. * Returns:
  1002. * Zero on success or a negative error code on failure.
  1003. */
  1004. int drm_crtc_vblank_get(struct drm_crtc *crtc)
  1005. {
  1006. return drm_vblank_get(crtc->dev, drm_crtc_index(crtc));
  1007. }
  1008. EXPORT_SYMBOL(drm_crtc_vblank_get);
  1009. /**
  1010. * drm_vblank_put - release ownership of vblank events
  1011. * @dev: DRM device
  1012. * @pipe: index of CRTC to release
  1013. *
  1014. * Release ownership of a given vblank counter, turning off interrupts
  1015. * if possible. Disable interrupts after drm_vblank_offdelay milliseconds.
  1016. *
  1017. * This is the legacy version of drm_crtc_vblank_put().
  1018. */
  1019. static void drm_vblank_put(struct drm_device *dev, unsigned int pipe)
  1020. {
  1021. struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
  1022. if (WARN_ON(pipe >= dev->num_crtcs))
  1023. return;
  1024. if (WARN_ON(atomic_read(&vblank->refcount) == 0))
  1025. return;
  1026. /* Last user schedules interrupt disable */
  1027. if (atomic_dec_and_test(&vblank->refcount)) {
  1028. if (drm_vblank_offdelay == 0)
  1029. return;
  1030. else if (drm_vblank_offdelay < 0)
  1031. vblank_disable_fn((unsigned long)vblank);
  1032. else if (!dev->vblank_disable_immediate)
  1033. mod_timer(&vblank->disable_timer,
  1034. jiffies + ((drm_vblank_offdelay * HZ)/1000));
  1035. }
  1036. }
  1037. /**
  1038. * drm_crtc_vblank_put - give up ownership of vblank events
  1039. * @crtc: which counter to give up
  1040. *
  1041. * Release ownership of a given vblank counter, turning off interrupts
  1042. * if possible. Disable interrupts after drm_vblank_offdelay milliseconds.
  1043. */
  1044. void drm_crtc_vblank_put(struct drm_crtc *crtc)
  1045. {
  1046. drm_vblank_put(crtc->dev, drm_crtc_index(crtc));
  1047. }
  1048. EXPORT_SYMBOL(drm_crtc_vblank_put);
  1049. /**
  1050. * drm_wait_one_vblank - wait for one vblank
  1051. * @dev: DRM device
  1052. * @pipe: CRTC index
  1053. *
  1054. * This waits for one vblank to pass on @pipe, using the irq driver interfaces.
  1055. * It is a failure to call this when the vblank irq for @pipe is disabled, e.g.
  1056. * due to lack of driver support or because the crtc is off.
  1057. */
  1058. void drm_wait_one_vblank(struct drm_device *dev, unsigned int pipe)
  1059. {
  1060. struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
  1061. int ret;
  1062. u32 last;
  1063. if (WARN_ON(pipe >= dev->num_crtcs))
  1064. return;
  1065. ret = drm_vblank_get(dev, pipe);
  1066. if (WARN(ret, "vblank not available on crtc %i, ret=%i\n", pipe, ret))
  1067. return;
  1068. last = drm_vblank_count(dev, pipe);
  1069. ret = wait_event_timeout(vblank->queue,
  1070. last != drm_vblank_count(dev, pipe),
  1071. msecs_to_jiffies(100));
  1072. WARN(ret == 0, "vblank wait timed out on crtc %i\n", pipe);
  1073. drm_vblank_put(dev, pipe);
  1074. }
  1075. EXPORT_SYMBOL(drm_wait_one_vblank);
  1076. /**
  1077. * drm_crtc_wait_one_vblank - wait for one vblank
  1078. * @crtc: DRM crtc
  1079. *
  1080. * This waits for one vblank to pass on @crtc, using the irq driver interfaces.
  1081. * It is a failure to call this when the vblank irq for @crtc is disabled, e.g.
  1082. * due to lack of driver support or because the crtc is off.
  1083. */
  1084. void drm_crtc_wait_one_vblank(struct drm_crtc *crtc)
  1085. {
  1086. drm_wait_one_vblank(crtc->dev, drm_crtc_index(crtc));
  1087. }
  1088. EXPORT_SYMBOL(drm_crtc_wait_one_vblank);
  1089. /**
  1090. * drm_crtc_vblank_off - disable vblank events on a CRTC
  1091. * @crtc: CRTC in question
  1092. *
  1093. * Drivers can use this function to shut down the vblank interrupt handling when
  1094. * disabling a crtc. This function ensures that the latest vblank frame count is
  1095. * stored so that drm_vblank_on can restore it again.
  1096. *
  1097. * Drivers must use this function when the hardware vblank counter can get
  1098. * reset, e.g. when suspending.
  1099. */
  1100. void drm_crtc_vblank_off(struct drm_crtc *crtc)
  1101. {
  1102. struct drm_device *dev = crtc->dev;
  1103. unsigned int pipe = drm_crtc_index(crtc);
  1104. struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
  1105. struct drm_pending_vblank_event *e, *t;
  1106. struct timeval now;
  1107. unsigned long irqflags;
  1108. unsigned int seq;
  1109. if (WARN_ON(pipe >= dev->num_crtcs))
  1110. return;
  1111. spin_lock_irqsave(&dev->event_lock, irqflags);
  1112. spin_lock(&dev->vbl_lock);
  1113. DRM_DEBUG_VBL("crtc %d, vblank enabled %d, inmodeset %d\n",
  1114. pipe, vblank->enabled, vblank->inmodeset);
  1115. /* Avoid redundant vblank disables without previous
  1116. * drm_crtc_vblank_on(). */
  1117. if (drm_core_check_feature(dev, DRIVER_ATOMIC) || !vblank->inmodeset)
  1118. vblank_disable_and_save(dev, pipe);
  1119. wake_up(&vblank->queue);
  1120. /*
  1121. * Prevent subsequent drm_vblank_get() from re-enabling
  1122. * the vblank interrupt by bumping the refcount.
  1123. */
  1124. if (!vblank->inmodeset) {
  1125. atomic_inc(&vblank->refcount);
  1126. vblank->inmodeset = 1;
  1127. }
  1128. spin_unlock(&dev->vbl_lock);
  1129. /* Send any queued vblank events, lest the natives grow disquiet */
  1130. seq = drm_vblank_count_and_time(dev, pipe, &now);
  1131. list_for_each_entry_safe(e, t, &dev->vblank_event_list, base.link) {
  1132. if (e->pipe != pipe)
  1133. continue;
  1134. DRM_DEBUG("Sending premature vblank event on disable: "
  1135. "wanted %u, current %u\n",
  1136. e->event.sequence, seq);
  1137. list_del(&e->base.link);
  1138. drm_vblank_put(dev, pipe);
  1139. send_vblank_event(dev, e, seq, &now);
  1140. }
  1141. spin_unlock_irqrestore(&dev->event_lock, irqflags);
  1142. }
  1143. EXPORT_SYMBOL(drm_crtc_vblank_off);
  1144. /**
  1145. * drm_crtc_vblank_reset - reset vblank state to off on a CRTC
  1146. * @crtc: CRTC in question
  1147. *
  1148. * Drivers can use this function to reset the vblank state to off at load time.
  1149. * Drivers should use this together with the drm_crtc_vblank_off() and
  1150. * drm_crtc_vblank_on() functions. The difference compared to
  1151. * drm_crtc_vblank_off() is that this function doesn't save the vblank counter
  1152. * and hence doesn't need to call any driver hooks.
  1153. */
  1154. void drm_crtc_vblank_reset(struct drm_crtc *crtc)
  1155. {
  1156. struct drm_device *dev = crtc->dev;
  1157. unsigned long irqflags;
  1158. unsigned int pipe = drm_crtc_index(crtc);
  1159. struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
  1160. spin_lock_irqsave(&dev->vbl_lock, irqflags);
  1161. /*
  1162. * Prevent subsequent drm_vblank_get() from enabling the vblank
  1163. * interrupt by bumping the refcount.
  1164. */
  1165. if (!vblank->inmodeset) {
  1166. atomic_inc(&vblank->refcount);
  1167. vblank->inmodeset = 1;
  1168. }
  1169. spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
  1170. WARN_ON(!list_empty(&dev->vblank_event_list));
  1171. }
  1172. EXPORT_SYMBOL(drm_crtc_vblank_reset);
  1173. /**
  1174. * drm_crtc_vblank_on - enable vblank events on a CRTC
  1175. * @crtc: CRTC in question
  1176. *
  1177. * This functions restores the vblank interrupt state captured with
  1178. * drm_crtc_vblank_off() again. Note that calls to drm_crtc_vblank_on() and
  1179. * drm_crtc_vblank_off() can be unbalanced and so can also be unconditionally called
  1180. * in driver load code to reflect the current hardware state of the crtc.
  1181. */
  1182. void drm_crtc_vblank_on(struct drm_crtc *crtc)
  1183. {
  1184. struct drm_device *dev = crtc->dev;
  1185. unsigned int pipe = drm_crtc_index(crtc);
  1186. struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
  1187. unsigned long irqflags;
  1188. if (WARN_ON(pipe >= dev->num_crtcs))
  1189. return;
  1190. spin_lock_irqsave(&dev->vbl_lock, irqflags);
  1191. DRM_DEBUG_VBL("crtc %d, vblank enabled %d, inmodeset %d\n",
  1192. pipe, vblank->enabled, vblank->inmodeset);
  1193. /* Drop our private "prevent drm_vblank_get" refcount */
  1194. if (vblank->inmodeset) {
  1195. atomic_dec(&vblank->refcount);
  1196. vblank->inmodeset = 0;
  1197. }
  1198. drm_reset_vblank_timestamp(dev, pipe);
  1199. /*
  1200. * re-enable interrupts if there are users left, or the
  1201. * user wishes vblank interrupts to be enabled all the time.
  1202. */
  1203. if (atomic_read(&vblank->refcount) != 0 || drm_vblank_offdelay == 0)
  1204. WARN_ON(drm_vblank_enable(dev, pipe));
  1205. spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
  1206. }
  1207. EXPORT_SYMBOL(drm_crtc_vblank_on);
  1208. static void drm_legacy_vblank_pre_modeset(struct drm_device *dev,
  1209. unsigned int pipe)
  1210. {
  1211. struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
  1212. /* vblank is not initialized (IRQ not installed ?), or has been freed */
  1213. if (!dev->num_crtcs)
  1214. return;
  1215. if (WARN_ON(pipe >= dev->num_crtcs))
  1216. return;
  1217. /*
  1218. * To avoid all the problems that might happen if interrupts
  1219. * were enabled/disabled around or between these calls, we just
  1220. * have the kernel take a reference on the CRTC (just once though
  1221. * to avoid corrupting the count if multiple, mismatch calls occur),
  1222. * so that interrupts remain enabled in the interim.
  1223. */
  1224. if (!vblank->inmodeset) {
  1225. vblank->inmodeset = 0x1;
  1226. if (drm_vblank_get(dev, pipe) == 0)
  1227. vblank->inmodeset |= 0x2;
  1228. }
  1229. }
  1230. static void drm_legacy_vblank_post_modeset(struct drm_device *dev,
  1231. unsigned int pipe)
  1232. {
  1233. struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
  1234. unsigned long irqflags;
  1235. /* vblank is not initialized (IRQ not installed ?), or has been freed */
  1236. if (!dev->num_crtcs)
  1237. return;
  1238. if (WARN_ON(pipe >= dev->num_crtcs))
  1239. return;
  1240. if (vblank->inmodeset) {
  1241. spin_lock_irqsave(&dev->vbl_lock, irqflags);
  1242. drm_reset_vblank_timestamp(dev, pipe);
  1243. spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
  1244. if (vblank->inmodeset & 0x2)
  1245. drm_vblank_put(dev, pipe);
  1246. vblank->inmodeset = 0;
  1247. }
  1248. }
  1249. int drm_legacy_modeset_ctl(struct drm_device *dev, void *data,
  1250. struct drm_file *file_priv)
  1251. {
  1252. struct drm_modeset_ctl *modeset = data;
  1253. unsigned int pipe;
  1254. /* If drm_vblank_init() hasn't been called yet, just no-op */
  1255. if (!dev->num_crtcs)
  1256. return 0;
  1257. /* KMS drivers handle this internally */
  1258. if (!drm_core_check_feature(dev, DRIVER_LEGACY))
  1259. return 0;
  1260. pipe = modeset->crtc;
  1261. if (pipe >= dev->num_crtcs)
  1262. return -EINVAL;
  1263. switch (modeset->cmd) {
  1264. case _DRM_PRE_MODESET:
  1265. drm_legacy_vblank_pre_modeset(dev, pipe);
  1266. break;
  1267. case _DRM_POST_MODESET:
  1268. drm_legacy_vblank_post_modeset(dev, pipe);
  1269. break;
  1270. default:
  1271. return -EINVAL;
  1272. }
  1273. return 0;
  1274. }
  1275. static inline bool vblank_passed(u32 seq, u32 ref)
  1276. {
  1277. return (seq - ref) <= (1 << 23);
  1278. }
  1279. static int drm_queue_vblank_event(struct drm_device *dev, unsigned int pipe,
  1280. union drm_wait_vblank *vblwait,
  1281. struct drm_file *file_priv)
  1282. {
  1283. struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
  1284. struct drm_pending_vblank_event *e;
  1285. struct timeval now;
  1286. unsigned long flags;
  1287. unsigned int seq;
  1288. int ret;
  1289. e = kzalloc(sizeof(*e), GFP_KERNEL);
  1290. if (e == NULL) {
  1291. ret = -ENOMEM;
  1292. goto err_put;
  1293. }
  1294. e->pipe = pipe;
  1295. e->event.base.type = DRM_EVENT_VBLANK;
  1296. e->event.base.length = sizeof(e->event);
  1297. e->event.user_data = vblwait->request.signal;
  1298. spin_lock_irqsave(&dev->event_lock, flags);
  1299. /*
  1300. * drm_crtc_vblank_off() might have been called after we called
  1301. * drm_vblank_get(). drm_crtc_vblank_off() holds event_lock around the
  1302. * vblank disable, so no need for further locking. The reference from
  1303. * drm_vblank_get() protects against vblank disable from another source.
  1304. */
  1305. if (!READ_ONCE(vblank->enabled)) {
  1306. ret = -EINVAL;
  1307. goto err_unlock;
  1308. }
  1309. ret = drm_event_reserve_init_locked(dev, file_priv, &e->base,
  1310. &e->event.base);
  1311. if (ret)
  1312. goto err_unlock;
  1313. seq = drm_vblank_count_and_time(dev, pipe, &now);
  1314. DRM_DEBUG("event on vblank count %u, current %u, crtc %u\n",
  1315. vblwait->request.sequence, seq, pipe);
  1316. trace_drm_vblank_event_queued(file_priv, pipe,
  1317. vblwait->request.sequence);
  1318. e->event.sequence = vblwait->request.sequence;
  1319. if (vblank_passed(seq, vblwait->request.sequence)) {
  1320. drm_vblank_put(dev, pipe);
  1321. send_vblank_event(dev, e, seq, &now);
  1322. vblwait->reply.sequence = seq;
  1323. } else {
  1324. /* drm_handle_vblank_events will call drm_vblank_put */
  1325. list_add_tail(&e->base.link, &dev->vblank_event_list);
  1326. vblwait->reply.sequence = vblwait->request.sequence;
  1327. }
  1328. spin_unlock_irqrestore(&dev->event_lock, flags);
  1329. return 0;
  1330. err_unlock:
  1331. spin_unlock_irqrestore(&dev->event_lock, flags);
  1332. kfree(e);
  1333. err_put:
  1334. drm_vblank_put(dev, pipe);
  1335. return ret;
  1336. }
  1337. static bool drm_wait_vblank_is_query(union drm_wait_vblank *vblwait)
  1338. {
  1339. if (vblwait->request.sequence)
  1340. return false;
  1341. return _DRM_VBLANK_RELATIVE ==
  1342. (vblwait->request.type & (_DRM_VBLANK_TYPES_MASK |
  1343. _DRM_VBLANK_EVENT |
  1344. _DRM_VBLANK_NEXTONMISS));
  1345. }
  1346. /*
  1347. * Wait for VBLANK.
  1348. *
  1349. * \param inode device inode.
  1350. * \param file_priv DRM file private.
  1351. * \param cmd command.
  1352. * \param data user argument, pointing to a drm_wait_vblank structure.
  1353. * \return zero on success or a negative number on failure.
  1354. *
  1355. * This function enables the vblank interrupt on the pipe requested, then
  1356. * sleeps waiting for the requested sequence number to occur, and drops
  1357. * the vblank interrupt refcount afterwards. (vblank IRQ disable follows that
  1358. * after a timeout with no further vblank waits scheduled).
  1359. */
  1360. int drm_wait_vblank(struct drm_device *dev, void *data,
  1361. struct drm_file *file_priv)
  1362. {
  1363. struct drm_vblank_crtc *vblank;
  1364. union drm_wait_vblank *vblwait = data;
  1365. int ret;
  1366. unsigned int flags, seq, pipe, high_pipe;
  1367. if (!dev->irq_enabled)
  1368. return -EINVAL;
  1369. if (vblwait->request.type & _DRM_VBLANK_SIGNAL)
  1370. return -EINVAL;
  1371. if (vblwait->request.type &
  1372. ~(_DRM_VBLANK_TYPES_MASK | _DRM_VBLANK_FLAGS_MASK |
  1373. _DRM_VBLANK_HIGH_CRTC_MASK)) {
  1374. DRM_ERROR("Unsupported type value 0x%x, supported mask 0x%x\n",
  1375. vblwait->request.type,
  1376. (_DRM_VBLANK_TYPES_MASK | _DRM_VBLANK_FLAGS_MASK |
  1377. _DRM_VBLANK_HIGH_CRTC_MASK));
  1378. return -EINVAL;
  1379. }
  1380. flags = vblwait->request.type & _DRM_VBLANK_FLAGS_MASK;
  1381. high_pipe = (vblwait->request.type & _DRM_VBLANK_HIGH_CRTC_MASK);
  1382. if (high_pipe)
  1383. pipe = high_pipe >> _DRM_VBLANK_HIGH_CRTC_SHIFT;
  1384. else
  1385. pipe = flags & _DRM_VBLANK_SECONDARY ? 1 : 0;
  1386. if (pipe >= dev->num_crtcs)
  1387. return -EINVAL;
  1388. vblank = &dev->vblank[pipe];
  1389. /* If the counter is currently enabled and accurate, short-circuit
  1390. * queries to return the cached timestamp of the last vblank.
  1391. */
  1392. if (dev->vblank_disable_immediate &&
  1393. drm_wait_vblank_is_query(vblwait) &&
  1394. READ_ONCE(vblank->enabled)) {
  1395. struct timeval now;
  1396. vblwait->reply.sequence =
  1397. drm_vblank_count_and_time(dev, pipe, &now);
  1398. vblwait->reply.tval_sec = now.tv_sec;
  1399. vblwait->reply.tval_usec = now.tv_usec;
  1400. return 0;
  1401. }
  1402. ret = drm_vblank_get(dev, pipe);
  1403. if (ret) {
  1404. DRM_DEBUG("crtc %d failed to acquire vblank counter, %d\n", pipe, ret);
  1405. return ret;
  1406. }
  1407. seq = drm_vblank_count(dev, pipe);
  1408. switch (vblwait->request.type & _DRM_VBLANK_TYPES_MASK) {
  1409. case _DRM_VBLANK_RELATIVE:
  1410. vblwait->request.sequence += seq;
  1411. vblwait->request.type &= ~_DRM_VBLANK_RELATIVE;
  1412. case _DRM_VBLANK_ABSOLUTE:
  1413. break;
  1414. default:
  1415. ret = -EINVAL;
  1416. goto done;
  1417. }
  1418. if ((flags & _DRM_VBLANK_NEXTONMISS) &&
  1419. vblank_passed(seq, vblwait->request.sequence))
  1420. vblwait->request.sequence = seq + 1;
  1421. if (flags & _DRM_VBLANK_EVENT) {
  1422. /* must hold on to the vblank ref until the event fires
  1423. * drm_vblank_put will be called asynchronously
  1424. */
  1425. return drm_queue_vblank_event(dev, pipe, vblwait, file_priv);
  1426. }
  1427. if (vblwait->request.sequence != seq) {
  1428. DRM_DEBUG("waiting on vblank count %u, crtc %u\n",
  1429. vblwait->request.sequence, pipe);
  1430. DRM_WAIT_ON(ret, vblank->queue, 3 * HZ,
  1431. vblank_passed(drm_vblank_count(dev, pipe),
  1432. vblwait->request.sequence) ||
  1433. !READ_ONCE(vblank->enabled));
  1434. }
  1435. if (ret != -EINTR) {
  1436. struct timeval now;
  1437. vblwait->reply.sequence = drm_vblank_count_and_time(dev, pipe, &now);
  1438. vblwait->reply.tval_sec = now.tv_sec;
  1439. vblwait->reply.tval_usec = now.tv_usec;
  1440. DRM_DEBUG("crtc %d returning %u to client\n",
  1441. pipe, vblwait->reply.sequence);
  1442. } else {
  1443. DRM_DEBUG("crtc %d vblank wait interrupted by signal\n", pipe);
  1444. }
  1445. done:
  1446. drm_vblank_put(dev, pipe);
  1447. return ret;
  1448. }
  1449. static void drm_handle_vblank_events(struct drm_device *dev, unsigned int pipe)
  1450. {
  1451. struct drm_pending_vblank_event *e, *t;
  1452. struct timeval now;
  1453. unsigned int seq;
  1454. assert_spin_locked(&dev->event_lock);
  1455. seq = drm_vblank_count_and_time(dev, pipe, &now);
  1456. list_for_each_entry_safe(e, t, &dev->vblank_event_list, base.link) {
  1457. if (e->pipe != pipe)
  1458. continue;
  1459. if (!vblank_passed(seq, e->event.sequence))
  1460. continue;
  1461. DRM_DEBUG("vblank event on %u, current %u\n",
  1462. e->event.sequence, seq);
  1463. list_del(&e->base.link);
  1464. drm_vblank_put(dev, pipe);
  1465. send_vblank_event(dev, e, seq, &now);
  1466. }
  1467. trace_drm_vblank_event(pipe, seq);
  1468. }
  1469. /**
  1470. * drm_handle_vblank - handle a vblank event
  1471. * @dev: DRM device
  1472. * @pipe: index of CRTC where this event occurred
  1473. *
  1474. * Drivers should call this routine in their vblank interrupt handlers to
  1475. * update the vblank counter and send any signals that may be pending.
  1476. *
  1477. * This is the legacy version of drm_crtc_handle_vblank().
  1478. */
  1479. bool drm_handle_vblank(struct drm_device *dev, unsigned int pipe)
  1480. {
  1481. struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
  1482. unsigned long irqflags;
  1483. bool disable_irq;
  1484. if (WARN_ON_ONCE(!dev->num_crtcs))
  1485. return false;
  1486. if (WARN_ON(pipe >= dev->num_crtcs))
  1487. return false;
  1488. spin_lock_irqsave(&dev->event_lock, irqflags);
  1489. /* Need timestamp lock to prevent concurrent execution with
  1490. * vblank enable/disable, as this would cause inconsistent
  1491. * or corrupted timestamps and vblank counts.
  1492. */
  1493. spin_lock(&dev->vblank_time_lock);
  1494. /* Vblank irq handling disabled. Nothing to do. */
  1495. if (!vblank->enabled) {
  1496. spin_unlock(&dev->vblank_time_lock);
  1497. spin_unlock_irqrestore(&dev->event_lock, irqflags);
  1498. return false;
  1499. }
  1500. drm_update_vblank_count(dev, pipe, DRM_CALLED_FROM_VBLIRQ);
  1501. spin_unlock(&dev->vblank_time_lock);
  1502. wake_up(&vblank->queue);
  1503. /* With instant-off, we defer disabling the interrupt until after
  1504. * we finish processing the following vblank after all events have
  1505. * been signaled. The disable has to be last (after
  1506. * drm_handle_vblank_events) so that the timestamp is always accurate.
  1507. */
  1508. disable_irq = (dev->vblank_disable_immediate &&
  1509. drm_vblank_offdelay > 0 &&
  1510. !atomic_read(&vblank->refcount));
  1511. drm_handle_vblank_events(dev, pipe);
  1512. spin_unlock_irqrestore(&dev->event_lock, irqflags);
  1513. if (disable_irq)
  1514. vblank_disable_fn((unsigned long)vblank);
  1515. return true;
  1516. }
  1517. EXPORT_SYMBOL(drm_handle_vblank);
  1518. /**
  1519. * drm_crtc_handle_vblank - handle a vblank event
  1520. * @crtc: where this event occurred
  1521. *
  1522. * Drivers should call this routine in their vblank interrupt handlers to
  1523. * update the vblank counter and send any signals that may be pending.
  1524. *
  1525. * This is the native KMS version of drm_handle_vblank().
  1526. *
  1527. * Returns:
  1528. * True if the event was successfully handled, false on failure.
  1529. */
  1530. bool drm_crtc_handle_vblank(struct drm_crtc *crtc)
  1531. {
  1532. return drm_handle_vblank(crtc->dev, drm_crtc_index(crtc));
  1533. }
  1534. EXPORT_SYMBOL(drm_crtc_handle_vblank);