drm_irq.c 55 KB

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