drm_irq.c 51 KB

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