drm_irq.c 55 KB

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