drm_irq.c 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577
  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 <linux/interrupt.h> /* For task queue support */
  36. #include <linux/slab.h>
  37. #include <linux/vgaarb.h>
  38. #include <linux/export.h>
  39. /* Access macro for slots in vblank timestamp ringbuffer. */
  40. #define vblanktimestamp(dev, crtc, count) \
  41. ((dev)->vblank[crtc].time[(count) % DRM_VBLANKTIME_RBSIZE])
  42. /* Retry timestamp calculation up to 3 times to satisfy
  43. * drm_timestamp_precision before giving up.
  44. */
  45. #define DRM_TIMESTAMP_MAXRETRIES 3
  46. /* Threshold in nanoseconds for detection of redundant
  47. * vblank irq in drm_handle_vblank(). 1 msec should be ok.
  48. */
  49. #define DRM_REDUNDANT_VBLIRQ_THRESH_NS 1000000
  50. /**
  51. * drm_update_vblank_count - update the master vblank counter
  52. * @dev: DRM device
  53. * @crtc: counter to update
  54. *
  55. * Call back into the driver to update the appropriate vblank counter
  56. * (specified by @crtc). Deal with wraparound, if it occurred, and
  57. * update the last read value so we can deal with wraparound on the next
  58. * call if necessary.
  59. *
  60. * Only necessary when going from off->on, to account for frames we
  61. * didn't get an interrupt for.
  62. *
  63. * Note: caller must hold dev->vbl_lock since this reads & writes
  64. * device vblank fields.
  65. */
  66. static void drm_update_vblank_count(struct drm_device *dev, int crtc)
  67. {
  68. struct drm_vblank_crtc *vblank = &dev->vblank[crtc];
  69. u32 cur_vblank, diff, tslot, rc;
  70. struct timeval t_vblank;
  71. /*
  72. * Interrupts were disabled prior to this call, so deal with counter
  73. * wrap if needed.
  74. * NOTE! It's possible we lost a full dev->max_vblank_count events
  75. * here if the register is small or we had vblank interrupts off for
  76. * a long time.
  77. *
  78. * We repeat the hardware vblank counter & timestamp query until
  79. * we get consistent results. This to prevent races between gpu
  80. * updating its hardware counter while we are retrieving the
  81. * corresponding vblank timestamp.
  82. */
  83. do {
  84. cur_vblank = dev->driver->get_vblank_counter(dev, crtc);
  85. rc = drm_get_last_vbltimestamp(dev, crtc, &t_vblank, 0);
  86. } while (cur_vblank != dev->driver->get_vblank_counter(dev, crtc));
  87. /* Deal with counter wrap */
  88. diff = cur_vblank - vblank->last;
  89. if (cur_vblank < vblank->last) {
  90. diff += dev->max_vblank_count;
  91. DRM_DEBUG("last_vblank[%d]=0x%x, cur_vblank=0x%x => diff=0x%x\n",
  92. crtc, vblank->last, cur_vblank, diff);
  93. }
  94. DRM_DEBUG("updating vblank count on crtc %d, missed %d\n",
  95. crtc, diff);
  96. /* Reinitialize corresponding vblank timestamp if high-precision query
  97. * available. Skip this step if query unsupported or failed. Will
  98. * reinitialize delayed at next vblank interrupt in that case.
  99. */
  100. if (rc) {
  101. tslot = atomic_read(&vblank->count) + diff;
  102. vblanktimestamp(dev, crtc, tslot) = t_vblank;
  103. }
  104. smp_mb__before_atomic();
  105. atomic_add(diff, &vblank->count);
  106. smp_mb__after_atomic();
  107. }
  108. /*
  109. * Disable vblank irq's on crtc, make sure that last vblank count
  110. * of hardware and corresponding consistent software vblank counter
  111. * are preserved, even if there are any spurious vblank irq's after
  112. * disable.
  113. */
  114. static void vblank_disable_and_save(struct drm_device *dev, int crtc)
  115. {
  116. struct drm_vblank_crtc *vblank = &dev->vblank[crtc];
  117. unsigned long irqflags;
  118. u32 vblcount;
  119. s64 diff_ns;
  120. int vblrc;
  121. struct timeval tvblank;
  122. int count = DRM_TIMESTAMP_MAXRETRIES;
  123. /* Prevent vblank irq processing while disabling vblank irqs,
  124. * so no updates of timestamps or count can happen after we've
  125. * disabled. Needed to prevent races in case of delayed irq's.
  126. */
  127. spin_lock_irqsave(&dev->vblank_time_lock, irqflags);
  128. /*
  129. * If the vblank interrupt was already disbled update the count
  130. * and timestamp to maintain the appearance that the counter
  131. * has been ticking all along until this time. This makes the
  132. * count account for the entire time between drm_vblank_on() and
  133. * drm_vblank_off().
  134. */
  135. if (!vblank->enabled) {
  136. drm_update_vblank_count(dev, crtc);
  137. spin_unlock_irqrestore(&dev->vblank_time_lock, irqflags);
  138. return;
  139. }
  140. dev->driver->disable_vblank(dev, crtc);
  141. vblank->enabled = false;
  142. /* No further vblank irq's will be processed after
  143. * this point. Get current hardware vblank count and
  144. * vblank timestamp, repeat until they are consistent.
  145. *
  146. * FIXME: There is still a race condition here and in
  147. * drm_update_vblank_count() which can cause off-by-one
  148. * reinitialization of software vblank counter. If gpu
  149. * vblank counter doesn't increment exactly at the leading
  150. * edge of a vblank interval, then we can lose 1 count if
  151. * we happen to execute between start of vblank and the
  152. * delayed gpu counter increment.
  153. */
  154. do {
  155. vblank->last = dev->driver->get_vblank_counter(dev, crtc);
  156. vblrc = drm_get_last_vbltimestamp(dev, crtc, &tvblank, 0);
  157. } while (vblank->last != dev->driver->get_vblank_counter(dev, crtc) && (--count) && vblrc);
  158. if (!count)
  159. vblrc = 0;
  160. /* Compute time difference to stored timestamp of last vblank
  161. * as updated by last invocation of drm_handle_vblank() in vblank irq.
  162. */
  163. vblcount = atomic_read(&vblank->count);
  164. diff_ns = timeval_to_ns(&tvblank) -
  165. timeval_to_ns(&vblanktimestamp(dev, crtc, vblcount));
  166. /* If there is at least 1 msec difference between the last stored
  167. * timestamp and tvblank, then we are currently executing our
  168. * disable inside a new vblank interval, the tvblank timestamp
  169. * corresponds to this new vblank interval and the irq handler
  170. * for this vblank didn't run yet and won't run due to our disable.
  171. * Therefore we need to do the job of drm_handle_vblank() and
  172. * increment the vblank counter by one to account for this vblank.
  173. *
  174. * Skip this step if there isn't any high precision timestamp
  175. * available. In that case we can't account for this and just
  176. * hope for the best.
  177. */
  178. if ((vblrc > 0) && (abs64(diff_ns) > 1000000)) {
  179. /* Store new timestamp in ringbuffer. */
  180. vblanktimestamp(dev, crtc, vblcount + 1) = tvblank;
  181. /* Increment cooked vblank count. This also atomically commits
  182. * the timestamp computed above.
  183. */
  184. smp_mb__before_atomic();
  185. atomic_inc(&vblank->count);
  186. smp_mb__after_atomic();
  187. }
  188. spin_unlock_irqrestore(&dev->vblank_time_lock, irqflags);
  189. }
  190. static void vblank_disable_fn(unsigned long arg)
  191. {
  192. struct drm_vblank_crtc *vblank = (void *)arg;
  193. struct drm_device *dev = vblank->dev;
  194. unsigned long irqflags;
  195. int crtc = vblank->crtc;
  196. if (!dev->vblank_disable_allowed)
  197. return;
  198. spin_lock_irqsave(&dev->vbl_lock, irqflags);
  199. if (atomic_read(&vblank->refcount) == 0 && vblank->enabled) {
  200. DRM_DEBUG("disabling vblank on crtc %d\n", crtc);
  201. vblank_disable_and_save(dev, crtc);
  202. }
  203. spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
  204. }
  205. /**
  206. * drm_vblank_cleanup - cleanup vblank support
  207. * @dev: DRM device
  208. *
  209. * This function cleans up any resources allocated in drm_vblank_init.
  210. */
  211. void drm_vblank_cleanup(struct drm_device *dev)
  212. {
  213. int crtc;
  214. /* Bail if the driver didn't call drm_vblank_init() */
  215. if (dev->num_crtcs == 0)
  216. return;
  217. for (crtc = 0; crtc < dev->num_crtcs; crtc++) {
  218. struct drm_vblank_crtc *vblank = &dev->vblank[crtc];
  219. del_timer_sync(&vblank->disable_timer);
  220. vblank_disable_fn((unsigned long)vblank);
  221. }
  222. kfree(dev->vblank);
  223. dev->num_crtcs = 0;
  224. }
  225. EXPORT_SYMBOL(drm_vblank_cleanup);
  226. /**
  227. * drm_vblank_init - initialize vblank support
  228. * @dev: drm_device
  229. * @num_crtcs: number of crtcs supported by @dev
  230. *
  231. * This function initializes vblank support for @num_crtcs display pipelines.
  232. *
  233. * Returns:
  234. * Zero on success or a negative error code on failure.
  235. */
  236. int drm_vblank_init(struct drm_device *dev, int num_crtcs)
  237. {
  238. int i, ret = -ENOMEM;
  239. spin_lock_init(&dev->vbl_lock);
  240. spin_lock_init(&dev->vblank_time_lock);
  241. dev->num_crtcs = num_crtcs;
  242. dev->vblank = kcalloc(num_crtcs, sizeof(*dev->vblank), GFP_KERNEL);
  243. if (!dev->vblank)
  244. goto err;
  245. for (i = 0; i < num_crtcs; i++) {
  246. struct drm_vblank_crtc *vblank = &dev->vblank[i];
  247. vblank->dev = dev;
  248. vblank->crtc = i;
  249. init_waitqueue_head(&vblank->queue);
  250. setup_timer(&vblank->disable_timer, vblank_disable_fn,
  251. (unsigned long)vblank);
  252. }
  253. DRM_INFO("Supports vblank timestamp caching Rev 2 (21.10.2013).\n");
  254. /* Driver specific high-precision vblank timestamping supported? */
  255. if (dev->driver->get_vblank_timestamp)
  256. DRM_INFO("Driver supports precise vblank timestamp query.\n");
  257. else
  258. DRM_INFO("No driver support for vblank timestamp query.\n");
  259. dev->vblank_disable_allowed = false;
  260. return 0;
  261. err:
  262. drm_vblank_cleanup(dev);
  263. return ret;
  264. }
  265. EXPORT_SYMBOL(drm_vblank_init);
  266. static void drm_irq_vgaarb_nokms(void *cookie, bool state)
  267. {
  268. struct drm_device *dev = cookie;
  269. if (dev->driver->vgaarb_irq) {
  270. dev->driver->vgaarb_irq(dev, state);
  271. return;
  272. }
  273. if (!dev->irq_enabled)
  274. return;
  275. if (state) {
  276. if (dev->driver->irq_uninstall)
  277. dev->driver->irq_uninstall(dev);
  278. } else {
  279. if (dev->driver->irq_preinstall)
  280. dev->driver->irq_preinstall(dev);
  281. if (dev->driver->irq_postinstall)
  282. dev->driver->irq_postinstall(dev);
  283. }
  284. }
  285. /**
  286. * drm_irq_install - install IRQ handler
  287. * @dev: DRM device
  288. * @irq: IRQ number to install the handler for
  289. *
  290. * Initializes the IRQ related data. Installs the handler, calling the driver
  291. * irq_preinstall() and irq_postinstall() functions before and after the
  292. * installation.
  293. *
  294. * This is the simplified helper interface provided for drivers with no special
  295. * needs. Drivers which need to install interrupt handlers for multiple
  296. * interrupts must instead set drm_device->irq_enabled to signal the DRM core
  297. * that vblank interrupts are available.
  298. *
  299. * Returns:
  300. * Zero on success or a negative error code on failure.
  301. */
  302. int drm_irq_install(struct drm_device *dev, int irq)
  303. {
  304. int ret;
  305. unsigned long sh_flags = 0;
  306. if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
  307. return -EINVAL;
  308. if (irq == 0)
  309. return -EINVAL;
  310. /* Driver must have been initialized */
  311. if (!dev->dev_private)
  312. return -EINVAL;
  313. if (dev->irq_enabled)
  314. return -EBUSY;
  315. dev->irq_enabled = true;
  316. DRM_DEBUG("irq=%d\n", irq);
  317. /* Before installing handler */
  318. if (dev->driver->irq_preinstall)
  319. dev->driver->irq_preinstall(dev);
  320. /* Install handler */
  321. if (drm_core_check_feature(dev, DRIVER_IRQ_SHARED))
  322. sh_flags = IRQF_SHARED;
  323. ret = request_irq(irq, dev->driver->irq_handler,
  324. sh_flags, dev->driver->name, dev);
  325. if (ret < 0) {
  326. dev->irq_enabled = false;
  327. return ret;
  328. }
  329. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  330. vga_client_register(dev->pdev, (void *)dev, drm_irq_vgaarb_nokms, NULL);
  331. /* After installing handler */
  332. if (dev->driver->irq_postinstall)
  333. ret = dev->driver->irq_postinstall(dev);
  334. if (ret < 0) {
  335. dev->irq_enabled = false;
  336. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  337. vga_client_register(dev->pdev, NULL, NULL, NULL);
  338. free_irq(irq, dev);
  339. } else {
  340. dev->irq = irq;
  341. }
  342. return ret;
  343. }
  344. EXPORT_SYMBOL(drm_irq_install);
  345. /**
  346. * drm_irq_uninstall - uninstall the IRQ handler
  347. * @dev: DRM device
  348. *
  349. * Calls the driver's irq_uninstall() function and unregisters the IRQ handler.
  350. * This should only be called by drivers which used drm_irq_install() to set up
  351. * their interrupt handler. Other drivers must only reset
  352. * drm_device->irq_enabled to false.
  353. *
  354. * Note that for kernel modesetting drivers it is a bug if this function fails.
  355. * The sanity checks are only to catch buggy user modesetting drivers which call
  356. * the same function through an ioctl.
  357. *
  358. * Returns:
  359. * Zero on success or a negative error code on failure.
  360. */
  361. int drm_irq_uninstall(struct drm_device *dev)
  362. {
  363. unsigned long irqflags;
  364. bool irq_enabled;
  365. int i;
  366. if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
  367. return -EINVAL;
  368. irq_enabled = dev->irq_enabled;
  369. dev->irq_enabled = false;
  370. /*
  371. * Wake up any waiters so they don't hang.
  372. */
  373. if (dev->num_crtcs) {
  374. spin_lock_irqsave(&dev->vbl_lock, irqflags);
  375. for (i = 0; i < dev->num_crtcs; i++) {
  376. struct drm_vblank_crtc *vblank = &dev->vblank[i];
  377. wake_up(&vblank->queue);
  378. vblank->enabled = false;
  379. vblank->last =
  380. dev->driver->get_vblank_counter(dev, i);
  381. }
  382. spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
  383. }
  384. if (!irq_enabled)
  385. return -EINVAL;
  386. DRM_DEBUG("irq=%d\n", dev->irq);
  387. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  388. vga_client_register(dev->pdev, NULL, NULL, NULL);
  389. if (dev->driver->irq_uninstall)
  390. dev->driver->irq_uninstall(dev);
  391. free_irq(dev->irq, dev);
  392. return 0;
  393. }
  394. EXPORT_SYMBOL(drm_irq_uninstall);
  395. /*
  396. * IRQ control ioctl.
  397. *
  398. * \param inode device inode.
  399. * \param file_priv DRM file private.
  400. * \param cmd command.
  401. * \param arg user argument, pointing to a drm_control structure.
  402. * \return zero on success or a negative number on failure.
  403. *
  404. * Calls irq_install() or irq_uninstall() according to \p arg.
  405. */
  406. int drm_control(struct drm_device *dev, void *data,
  407. struct drm_file *file_priv)
  408. {
  409. struct drm_control *ctl = data;
  410. int ret = 0, irq;
  411. /* if we haven't irq we fallback for compatibility reasons -
  412. * this used to be a separate function in drm_dma.h
  413. */
  414. if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
  415. return 0;
  416. if (drm_core_check_feature(dev, DRIVER_MODESET))
  417. return 0;
  418. /* UMS was only ever support on pci devices. */
  419. if (WARN_ON(!dev->pdev))
  420. return -EINVAL;
  421. switch (ctl->func) {
  422. case DRM_INST_HANDLER:
  423. irq = dev->pdev->irq;
  424. if (dev->if_version < DRM_IF_VERSION(1, 2) &&
  425. ctl->irq != irq)
  426. return -EINVAL;
  427. mutex_lock(&dev->struct_mutex);
  428. ret = drm_irq_install(dev, irq);
  429. mutex_unlock(&dev->struct_mutex);
  430. return ret;
  431. case DRM_UNINST_HANDLER:
  432. mutex_lock(&dev->struct_mutex);
  433. ret = drm_irq_uninstall(dev);
  434. mutex_unlock(&dev->struct_mutex);
  435. return ret;
  436. default:
  437. return -EINVAL;
  438. }
  439. }
  440. /**
  441. * drm_calc_timestamping_constants - calculate vblank timestamp constants
  442. * @crtc: drm_crtc whose timestamp constants should be updated.
  443. * @mode: display mode containing the scanout timings
  444. *
  445. * Calculate and store various constants which are later
  446. * needed by vblank and swap-completion timestamping, e.g,
  447. * by drm_calc_vbltimestamp_from_scanoutpos(). They are
  448. * derived from CRTC's true scanout timing, so they take
  449. * things like panel scaling or other adjustments into account.
  450. */
  451. void drm_calc_timestamping_constants(struct drm_crtc *crtc,
  452. const struct drm_display_mode *mode)
  453. {
  454. int linedur_ns = 0, pixeldur_ns = 0, framedur_ns = 0;
  455. int dotclock = mode->crtc_clock;
  456. /* Valid dotclock? */
  457. if (dotclock > 0) {
  458. int frame_size = mode->crtc_htotal * mode->crtc_vtotal;
  459. /*
  460. * Convert scanline length in pixels and video
  461. * dot clock to line duration, frame duration
  462. * and pixel duration in nanoseconds:
  463. */
  464. pixeldur_ns = 1000000 / dotclock;
  465. linedur_ns = div_u64((u64) mode->crtc_htotal * 1000000, dotclock);
  466. framedur_ns = div_u64((u64) frame_size * 1000000, dotclock);
  467. /*
  468. * Fields of interlaced scanout modes are only half a frame duration.
  469. */
  470. if (mode->flags & DRM_MODE_FLAG_INTERLACE)
  471. framedur_ns /= 2;
  472. } else
  473. DRM_ERROR("crtc %d: Can't calculate constants, dotclock = 0!\n",
  474. crtc->base.id);
  475. crtc->pixeldur_ns = pixeldur_ns;
  476. crtc->linedur_ns = linedur_ns;
  477. crtc->framedur_ns = framedur_ns;
  478. DRM_DEBUG("crtc %d: hwmode: htotal %d, vtotal %d, vdisplay %d\n",
  479. crtc->base.id, mode->crtc_htotal,
  480. mode->crtc_vtotal, mode->crtc_vdisplay);
  481. DRM_DEBUG("crtc %d: clock %d kHz framedur %d linedur %d, pixeldur %d\n",
  482. crtc->base.id, dotclock, framedur_ns,
  483. linedur_ns, pixeldur_ns);
  484. }
  485. EXPORT_SYMBOL(drm_calc_timestamping_constants);
  486. /**
  487. * drm_calc_vbltimestamp_from_scanoutpos - precise vblank timestamp helper
  488. * @dev: DRM device
  489. * @crtc: Which CRTC's vblank timestamp to retrieve
  490. * @max_error: Desired maximum allowable error in timestamps (nanosecs)
  491. * On return contains true maximum error of timestamp
  492. * @vblank_time: Pointer to struct timeval which should receive the timestamp
  493. * @flags: Flags to pass to driver:
  494. * 0 = Default,
  495. * DRM_CALLED_FROM_VBLIRQ = If function is called from vbl IRQ handler
  496. * @refcrtc: CRTC which defines scanout timing
  497. * @mode: mode which defines the scanout timings
  498. *
  499. * Implements calculation of exact vblank timestamps from given drm_display_mode
  500. * timings and current video scanout position of a CRTC. This can be called from
  501. * within get_vblank_timestamp() implementation of a kms driver to implement the
  502. * actual timestamping.
  503. *
  504. * Should return timestamps conforming to the OML_sync_control OpenML
  505. * extension specification. The timestamp corresponds to the end of
  506. * the vblank interval, aka start of scanout of topmost-leftmost display
  507. * pixel in the following video frame.
  508. *
  509. * Requires support for optional dev->driver->get_scanout_position()
  510. * in kms driver, plus a bit of setup code to provide a drm_display_mode
  511. * that corresponds to the true scanout timing.
  512. *
  513. * The current implementation only handles standard video modes. It
  514. * returns as no operation if a doublescan or interlaced video mode is
  515. * active. Higher level code is expected to handle this.
  516. *
  517. * Returns:
  518. * Negative value on error, failure or if not supported in current
  519. * video mode:
  520. *
  521. * -EINVAL - Invalid CRTC.
  522. * -EAGAIN - Temporary unavailable, e.g., called before initial modeset.
  523. * -ENOTSUPP - Function not supported in current display mode.
  524. * -EIO - Failed, e.g., due to failed scanout position query.
  525. *
  526. * Returns or'ed positive status flags on success:
  527. *
  528. * DRM_VBLANKTIME_SCANOUTPOS_METHOD - Signal this method used for timestamping.
  529. * DRM_VBLANKTIME_INVBL - Timestamp taken while scanout was in vblank interval.
  530. *
  531. */
  532. int drm_calc_vbltimestamp_from_scanoutpos(struct drm_device *dev, int crtc,
  533. int *max_error,
  534. struct timeval *vblank_time,
  535. unsigned flags,
  536. const struct drm_crtc *refcrtc,
  537. const struct drm_display_mode *mode)
  538. {
  539. ktime_t stime, etime, mono_time_offset;
  540. struct timeval tv_etime;
  541. int vbl_status;
  542. int vpos, hpos, i;
  543. int framedur_ns, linedur_ns, pixeldur_ns, delta_ns, duration_ns;
  544. bool invbl;
  545. if (crtc < 0 || crtc >= dev->num_crtcs) {
  546. DRM_ERROR("Invalid crtc %d\n", crtc);
  547. return -EINVAL;
  548. }
  549. /* Scanout position query not supported? Should not happen. */
  550. if (!dev->driver->get_scanout_position) {
  551. DRM_ERROR("Called from driver w/o get_scanout_position()!?\n");
  552. return -EIO;
  553. }
  554. /* Durations of frames, lines, pixels in nanoseconds. */
  555. framedur_ns = refcrtc->framedur_ns;
  556. linedur_ns = refcrtc->linedur_ns;
  557. pixeldur_ns = refcrtc->pixeldur_ns;
  558. /* If mode timing undefined, just return as no-op:
  559. * Happens during initial modesetting of a crtc.
  560. */
  561. if (framedur_ns == 0) {
  562. DRM_DEBUG("crtc %d: Noop due to uninitialized mode.\n", crtc);
  563. return -EAGAIN;
  564. }
  565. /* Get current scanout position with system timestamp.
  566. * Repeat query up to DRM_TIMESTAMP_MAXRETRIES times
  567. * if single query takes longer than max_error nanoseconds.
  568. *
  569. * This guarantees a tight bound on maximum error if
  570. * code gets preempted or delayed for some reason.
  571. */
  572. for (i = 0; i < DRM_TIMESTAMP_MAXRETRIES; i++) {
  573. /*
  574. * Get vertical and horizontal scanout position vpos, hpos,
  575. * and bounding timestamps stime, etime, pre/post query.
  576. */
  577. vbl_status = dev->driver->get_scanout_position(dev, crtc, flags, &vpos,
  578. &hpos, &stime, &etime);
  579. /*
  580. * Get correction for CLOCK_MONOTONIC -> CLOCK_REALTIME if
  581. * CLOCK_REALTIME is requested.
  582. */
  583. if (!drm_timestamp_monotonic)
  584. mono_time_offset = ktime_get_monotonic_offset();
  585. /* Return as no-op if scanout query unsupported or failed. */
  586. if (!(vbl_status & DRM_SCANOUTPOS_VALID)) {
  587. DRM_DEBUG("crtc %d : scanoutpos query failed [%d].\n",
  588. crtc, vbl_status);
  589. return -EIO;
  590. }
  591. /* Compute uncertainty in timestamp of scanout position query. */
  592. duration_ns = ktime_to_ns(etime) - ktime_to_ns(stime);
  593. /* Accept result with < max_error nsecs timing uncertainty. */
  594. if (duration_ns <= *max_error)
  595. break;
  596. }
  597. /* Noisy system timing? */
  598. if (i == DRM_TIMESTAMP_MAXRETRIES) {
  599. DRM_DEBUG("crtc %d: Noisy timestamp %d us > %d us [%d reps].\n",
  600. crtc, duration_ns/1000, *max_error/1000, i);
  601. }
  602. /* Return upper bound of timestamp precision error. */
  603. *max_error = duration_ns;
  604. /* Check if in vblank area:
  605. * vpos is >=0 in video scanout area, but negative
  606. * within vblank area, counting down the number of lines until
  607. * start of scanout.
  608. */
  609. invbl = vbl_status & DRM_SCANOUTPOS_INVBL;
  610. /* Convert scanout position into elapsed time at raw_time query
  611. * since start of scanout at first display scanline. delta_ns
  612. * can be negative if start of scanout hasn't happened yet.
  613. */
  614. delta_ns = vpos * linedur_ns + hpos * pixeldur_ns;
  615. if (!drm_timestamp_monotonic)
  616. etime = ktime_sub(etime, mono_time_offset);
  617. /* save this only for debugging purposes */
  618. tv_etime = ktime_to_timeval(etime);
  619. /* Subtract time delta from raw timestamp to get final
  620. * vblank_time timestamp for end of vblank.
  621. */
  622. if (delta_ns < 0)
  623. etime = ktime_add_ns(etime, -delta_ns);
  624. else
  625. etime = ktime_sub_ns(etime, delta_ns);
  626. *vblank_time = ktime_to_timeval(etime);
  627. DRM_DEBUG("crtc %d : v %d p(%d,%d)@ %ld.%ld -> %ld.%ld [e %d us, %d rep]\n",
  628. crtc, (int)vbl_status, hpos, vpos,
  629. (long)tv_etime.tv_sec, (long)tv_etime.tv_usec,
  630. (long)vblank_time->tv_sec, (long)vblank_time->tv_usec,
  631. duration_ns/1000, i);
  632. vbl_status = DRM_VBLANKTIME_SCANOUTPOS_METHOD;
  633. if (invbl)
  634. vbl_status |= DRM_VBLANKTIME_INVBL;
  635. return vbl_status;
  636. }
  637. EXPORT_SYMBOL(drm_calc_vbltimestamp_from_scanoutpos);
  638. static struct timeval get_drm_timestamp(void)
  639. {
  640. ktime_t now;
  641. now = ktime_get();
  642. if (!drm_timestamp_monotonic)
  643. now = ktime_sub(now, ktime_get_monotonic_offset());
  644. return ktime_to_timeval(now);
  645. }
  646. /**
  647. * drm_get_last_vbltimestamp - retrieve raw timestamp for the most recent
  648. * vblank interval
  649. * @dev: DRM device
  650. * @crtc: which CRTC's vblank timestamp to retrieve
  651. * @tvblank: Pointer to target struct timeval which should receive the timestamp
  652. * @flags: Flags to pass to driver:
  653. * 0 = Default,
  654. * DRM_CALLED_FROM_VBLIRQ = If function is called from vbl IRQ handler
  655. *
  656. * Fetches the system timestamp corresponding to the time of the most recent
  657. * vblank interval on specified CRTC. May call into kms-driver to
  658. * compute the timestamp with a high-precision GPU specific method.
  659. *
  660. * Returns zero if timestamp originates from uncorrected do_gettimeofday()
  661. * call, i.e., it isn't very precisely locked to the true vblank.
  662. *
  663. * Returns:
  664. * Non-zero if timestamp is considered to be very precise, zero otherwise.
  665. */
  666. u32 drm_get_last_vbltimestamp(struct drm_device *dev, int crtc,
  667. struct timeval *tvblank, unsigned flags)
  668. {
  669. int ret;
  670. /* Define requested maximum error on timestamps (nanoseconds). */
  671. int max_error = (int) drm_timestamp_precision * 1000;
  672. /* Query driver if possible and precision timestamping enabled. */
  673. if (dev->driver->get_vblank_timestamp && (max_error > 0)) {
  674. ret = dev->driver->get_vblank_timestamp(dev, crtc, &max_error,
  675. tvblank, flags);
  676. if (ret > 0)
  677. return (u32) ret;
  678. }
  679. /* GPU high precision timestamp query unsupported or failed.
  680. * Return current monotonic/gettimeofday timestamp as best estimate.
  681. */
  682. *tvblank = get_drm_timestamp();
  683. return 0;
  684. }
  685. EXPORT_SYMBOL(drm_get_last_vbltimestamp);
  686. /**
  687. * drm_vblank_count - retrieve "cooked" vblank counter value
  688. * @dev: DRM device
  689. * @crtc: which counter to retrieve
  690. *
  691. * Fetches the "cooked" vblank count value that represents the number of
  692. * vblank events since the system was booted, including lost events due to
  693. * modesetting activity.
  694. *
  695. * Returns:
  696. * The software vblank counter.
  697. */
  698. u32 drm_vblank_count(struct drm_device *dev, int crtc)
  699. {
  700. struct drm_vblank_crtc *vblank = &dev->vblank[crtc];
  701. return atomic_read(&vblank->count);
  702. }
  703. EXPORT_SYMBOL(drm_vblank_count);
  704. /**
  705. * drm_vblank_count_and_time - retrieve "cooked" vblank counter value
  706. * and the system timestamp corresponding to that vblank counter value.
  707. *
  708. * @dev: DRM device
  709. * @crtc: which counter to retrieve
  710. * @vblanktime: Pointer to struct timeval to receive the vblank timestamp.
  711. *
  712. * Fetches the "cooked" vblank count value that represents the number of
  713. * vblank events since the system was booted, including lost events due to
  714. * modesetting activity. Returns corresponding system timestamp of the time
  715. * of the vblank interval that corresponds to the current vblank counter value.
  716. */
  717. u32 drm_vblank_count_and_time(struct drm_device *dev, int crtc,
  718. struct timeval *vblanktime)
  719. {
  720. struct drm_vblank_crtc *vblank = &dev->vblank[crtc];
  721. u32 cur_vblank;
  722. /* Read timestamp from slot of _vblank_time ringbuffer
  723. * that corresponds to current vblank count. Retry if
  724. * count has incremented during readout. This works like
  725. * a seqlock.
  726. */
  727. do {
  728. cur_vblank = atomic_read(&vblank->count);
  729. *vblanktime = vblanktimestamp(dev, crtc, cur_vblank);
  730. smp_rmb();
  731. } while (cur_vblank != atomic_read(&vblank->count));
  732. return cur_vblank;
  733. }
  734. EXPORT_SYMBOL(drm_vblank_count_and_time);
  735. static void send_vblank_event(struct drm_device *dev,
  736. struct drm_pending_vblank_event *e,
  737. unsigned long seq, struct timeval *now)
  738. {
  739. WARN_ON_SMP(!spin_is_locked(&dev->event_lock));
  740. e->event.sequence = seq;
  741. e->event.tv_sec = now->tv_sec;
  742. e->event.tv_usec = now->tv_usec;
  743. list_add_tail(&e->base.link,
  744. &e->base.file_priv->event_list);
  745. wake_up_interruptible(&e->base.file_priv->event_wait);
  746. trace_drm_vblank_event_delivered(e->base.pid, e->pipe,
  747. e->event.sequence);
  748. }
  749. /**
  750. * drm_send_vblank_event - helper to send vblank event after pageflip
  751. * @dev: DRM device
  752. * @crtc: CRTC in question
  753. * @e: the event to send
  754. *
  755. * Updates sequence # and timestamp on event, and sends it to userspace.
  756. * Caller must hold event lock.
  757. */
  758. void drm_send_vblank_event(struct drm_device *dev, int crtc,
  759. struct drm_pending_vblank_event *e)
  760. {
  761. struct timeval now;
  762. unsigned int seq;
  763. if (crtc >= 0) {
  764. seq = drm_vblank_count_and_time(dev, crtc, &now);
  765. } else {
  766. seq = 0;
  767. now = get_drm_timestamp();
  768. }
  769. e->pipe = crtc;
  770. send_vblank_event(dev, e, seq, &now);
  771. }
  772. EXPORT_SYMBOL(drm_send_vblank_event);
  773. /**
  774. * drm_vblank_enable - enable the vblank interrupt on a CRTC
  775. * @dev: DRM device
  776. * @crtc: CRTC in question
  777. */
  778. static int drm_vblank_enable(struct drm_device *dev, int crtc)
  779. {
  780. struct drm_vblank_crtc *vblank = &dev->vblank[crtc];
  781. int ret = 0;
  782. assert_spin_locked(&dev->vbl_lock);
  783. spin_lock(&dev->vblank_time_lock);
  784. if (!vblank->enabled) {
  785. /*
  786. * Enable vblank irqs under vblank_time_lock protection.
  787. * All vblank count & timestamp updates are held off
  788. * until we are done reinitializing master counter and
  789. * timestamps. Filtercode in drm_handle_vblank() will
  790. * prevent double-accounting of same vblank interval.
  791. */
  792. ret = dev->driver->enable_vblank(dev, crtc);
  793. DRM_DEBUG("enabling vblank on crtc %d, ret: %d\n", crtc, ret);
  794. if (ret)
  795. atomic_dec(&vblank->refcount);
  796. else {
  797. vblank->enabled = true;
  798. drm_update_vblank_count(dev, crtc);
  799. }
  800. }
  801. spin_unlock(&dev->vblank_time_lock);
  802. return ret;
  803. }
  804. /**
  805. * drm_vblank_get - get a reference count on vblank events
  806. * @dev: DRM device
  807. * @crtc: which CRTC to own
  808. *
  809. * Acquire a reference count on vblank events to avoid having them disabled
  810. * while in use.
  811. *
  812. * This is the legacy version of drm_crtc_vblank_get().
  813. *
  814. * Returns:
  815. * Zero on success, nonzero on failure.
  816. */
  817. int drm_vblank_get(struct drm_device *dev, int crtc)
  818. {
  819. struct drm_vblank_crtc *vblank = &dev->vblank[crtc];
  820. unsigned long irqflags;
  821. int ret = 0;
  822. spin_lock_irqsave(&dev->vbl_lock, irqflags);
  823. /* Going from 0->1 means we have to enable interrupts again */
  824. if (atomic_add_return(1, &vblank->refcount) == 1) {
  825. ret = drm_vblank_enable(dev, crtc);
  826. } else {
  827. if (!vblank->enabled) {
  828. atomic_dec(&vblank->refcount);
  829. ret = -EINVAL;
  830. }
  831. }
  832. spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
  833. return ret;
  834. }
  835. EXPORT_SYMBOL(drm_vblank_get);
  836. /**
  837. * drm_crtc_vblank_get - get a reference count on vblank events
  838. * @crtc: which CRTC to own
  839. *
  840. * Acquire a reference count on vblank events to avoid having them disabled
  841. * while in use.
  842. *
  843. * This is the native kms version of drm_vblank_off().
  844. *
  845. * Returns:
  846. * Zero on success, nonzero on failure.
  847. */
  848. int drm_crtc_vblank_get(struct drm_crtc *crtc)
  849. {
  850. return drm_vblank_get(crtc->dev, drm_crtc_index(crtc));
  851. }
  852. EXPORT_SYMBOL(drm_crtc_vblank_get);
  853. /**
  854. * drm_vblank_put - give up ownership of vblank events
  855. * @dev: DRM device
  856. * @crtc: which counter to give up
  857. *
  858. * Release ownership of a given vblank counter, turning off interrupts
  859. * if possible. Disable interrupts after drm_vblank_offdelay milliseconds.
  860. *
  861. * This is the legacy version of drm_crtc_vblank_put().
  862. */
  863. void drm_vblank_put(struct drm_device *dev, int crtc)
  864. {
  865. struct drm_vblank_crtc *vblank = &dev->vblank[crtc];
  866. BUG_ON(atomic_read(&vblank->refcount) == 0);
  867. /* Last user schedules interrupt disable */
  868. if (atomic_dec_and_test(&vblank->refcount)) {
  869. if (dev->vblank_disable_immediate || drm_vblank_offdelay < 0)
  870. vblank_disable_fn((unsigned long)vblank);
  871. else if (drm_vblank_offdelay > 0)
  872. mod_timer(&vblank->disable_timer,
  873. jiffies + ((drm_vblank_offdelay * HZ)/1000));
  874. }
  875. }
  876. EXPORT_SYMBOL(drm_vblank_put);
  877. /**
  878. * drm_crtc_vblank_put - give up ownership of vblank events
  879. * @crtc: which counter to give up
  880. *
  881. * Release ownership of a given vblank counter, turning off interrupts
  882. * if possible. Disable interrupts after drm_vblank_offdelay milliseconds.
  883. *
  884. * This is the native kms version of drm_vblank_put().
  885. */
  886. void drm_crtc_vblank_put(struct drm_crtc *crtc)
  887. {
  888. drm_vblank_put(crtc->dev, drm_crtc_index(crtc));
  889. }
  890. EXPORT_SYMBOL(drm_crtc_vblank_put);
  891. /**
  892. * drm_vblank_off - disable vblank events on a CRTC
  893. * @dev: DRM device
  894. * @crtc: CRTC in question
  895. *
  896. * Drivers can use this function to shut down the vblank interrupt handling when
  897. * disabling a crtc. This function ensures that the latest vblank frame count is
  898. * stored so that drm_vblank_on() can restore it again.
  899. *
  900. * Drivers must use this function when the hardware vblank counter can get
  901. * reset, e.g. when suspending.
  902. *
  903. * This is the legacy version of drm_crtc_vblank_off().
  904. */
  905. void drm_vblank_off(struct drm_device *dev, int crtc)
  906. {
  907. struct drm_vblank_crtc *vblank = &dev->vblank[crtc];
  908. struct drm_pending_vblank_event *e, *t;
  909. struct timeval now;
  910. unsigned long irqflags;
  911. unsigned int seq;
  912. spin_lock_irqsave(&dev->event_lock, irqflags);
  913. spin_lock(&dev->vbl_lock);
  914. vblank_disable_and_save(dev, crtc);
  915. wake_up(&vblank->queue);
  916. /*
  917. * Prevent subsequent drm_vblank_get() from re-enabling
  918. * the vblank interrupt by bumping the refcount.
  919. */
  920. if (!vblank->inmodeset) {
  921. atomic_inc(&vblank->refcount);
  922. vblank->inmodeset = 1;
  923. }
  924. spin_unlock(&dev->vbl_lock);
  925. /* Send any queued vblank events, lest the natives grow disquiet */
  926. seq = drm_vblank_count_and_time(dev, crtc, &now);
  927. list_for_each_entry_safe(e, t, &dev->vblank_event_list, base.link) {
  928. if (e->pipe != crtc)
  929. continue;
  930. DRM_DEBUG("Sending premature vblank event on disable: \
  931. wanted %d, current %d\n",
  932. e->event.sequence, seq);
  933. list_del(&e->base.link);
  934. drm_vblank_put(dev, e->pipe);
  935. send_vblank_event(dev, e, seq, &now);
  936. }
  937. spin_unlock_irqrestore(&dev->event_lock, irqflags);
  938. }
  939. EXPORT_SYMBOL(drm_vblank_off);
  940. /**
  941. * drm_crtc_vblank_off - disable vblank events on a CRTC
  942. * @crtc: CRTC in question
  943. *
  944. * Drivers can use this function to shut down the vblank interrupt handling when
  945. * disabling a crtc. This function ensures that the latest vblank frame count is
  946. * stored so that drm_vblank_on can restore it again.
  947. *
  948. * Drivers must use this function when the hardware vblank counter can get
  949. * reset, e.g. when suspending.
  950. *
  951. * This is the native kms version of drm_vblank_off().
  952. */
  953. void drm_crtc_vblank_off(struct drm_crtc *crtc)
  954. {
  955. drm_vblank_off(crtc->dev, drm_crtc_index(crtc));
  956. }
  957. EXPORT_SYMBOL(drm_crtc_vblank_off);
  958. /**
  959. * drm_vblank_on - enable vblank events on a CRTC
  960. * @dev: DRM device
  961. * @crtc: CRTC in question
  962. *
  963. * This functions restores the vblank interrupt state captured with
  964. * drm_vblank_off() again. Note that calls to drm_vblank_on() and
  965. * drm_vblank_off() can be unbalanced and so can also be unconditionaly called
  966. * in driver load code to reflect the current hardware state of the crtc.
  967. *
  968. * This is the legacy version of drm_crtc_vblank_on().
  969. */
  970. void drm_vblank_on(struct drm_device *dev, int crtc)
  971. {
  972. struct drm_vblank_crtc *vblank = &dev->vblank[crtc];
  973. unsigned long irqflags;
  974. spin_lock_irqsave(&dev->vbl_lock, irqflags);
  975. /* Drop our private "prevent drm_vblank_get" refcount */
  976. if (vblank->inmodeset) {
  977. atomic_dec(&vblank->refcount);
  978. vblank->inmodeset = 0;
  979. }
  980. /*
  981. * sample the current counter to avoid random jumps
  982. * when drm_vblank_enable() applies the diff
  983. *
  984. * -1 to make sure user will never see the same
  985. * vblank counter value before and after a modeset
  986. */
  987. vblank->last =
  988. (dev->driver->get_vblank_counter(dev, crtc) - 1) &
  989. dev->max_vblank_count;
  990. /*
  991. * re-enable interrupts if there are users left, or the
  992. * user wishes vblank interrupts to be enabled all the time.
  993. */
  994. if (atomic_read(&vblank->refcount) != 0 ||
  995. (!dev->vblank_disable_immediate && drm_vblank_offdelay == 0))
  996. WARN_ON(drm_vblank_enable(dev, crtc));
  997. spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
  998. }
  999. EXPORT_SYMBOL(drm_vblank_on);
  1000. /**
  1001. * drm_crtc_vblank_on - enable vblank events on a CRTC
  1002. * @crtc: CRTC in question
  1003. *
  1004. * This functions restores the vblank interrupt state captured with
  1005. * drm_vblank_off() again. Note that calls to drm_vblank_on() and
  1006. * drm_vblank_off() can be unbalanced and so can also be unconditionaly called
  1007. * in driver load code to reflect the current hardware state of the crtc.
  1008. *
  1009. * This is the native kms version of drm_vblank_on().
  1010. */
  1011. void drm_crtc_vblank_on(struct drm_crtc *crtc)
  1012. {
  1013. drm_vblank_on(crtc->dev, drm_crtc_index(crtc));
  1014. }
  1015. EXPORT_SYMBOL(drm_crtc_vblank_on);
  1016. /**
  1017. * drm_vblank_pre_modeset - account for vblanks across mode sets
  1018. * @dev: DRM device
  1019. * @crtc: CRTC in question
  1020. *
  1021. * Account for vblank events across mode setting events, which will likely
  1022. * reset the hardware frame counter.
  1023. *
  1024. * This is done by grabbing a temporary vblank reference to ensure that the
  1025. * vblank interrupt keeps running across the modeset sequence. With this the
  1026. * software-side vblank frame counting will ensure that there are no jumps or
  1027. * discontinuities.
  1028. *
  1029. * Unfortunately this approach is racy and also doesn't work when the vblank
  1030. * interrupt stops running, e.g. across system suspend resume. It is therefore
  1031. * highly recommended that drivers use the newer drm_vblank_off() and
  1032. * drm_vblank_on() instead. drm_vblank_pre_modeset() only works correctly when
  1033. * using "cooked" software vblank frame counters and not relying on any hardware
  1034. * counters.
  1035. *
  1036. * Drivers must call drm_vblank_post_modeset() when re-enabling the same crtc
  1037. * again.
  1038. */
  1039. void drm_vblank_pre_modeset(struct drm_device *dev, int crtc)
  1040. {
  1041. struct drm_vblank_crtc *vblank = &dev->vblank[crtc];
  1042. /* vblank is not initialized (IRQ not installed ?), or has been freed */
  1043. if (!dev->num_crtcs)
  1044. return;
  1045. /*
  1046. * To avoid all the problems that might happen if interrupts
  1047. * were enabled/disabled around or between these calls, we just
  1048. * have the kernel take a reference on the CRTC (just once though
  1049. * to avoid corrupting the count if multiple, mismatch calls occur),
  1050. * so that interrupts remain enabled in the interim.
  1051. */
  1052. if (!vblank->inmodeset) {
  1053. vblank->inmodeset = 0x1;
  1054. if (drm_vblank_get(dev, crtc) == 0)
  1055. vblank->inmodeset |= 0x2;
  1056. }
  1057. }
  1058. EXPORT_SYMBOL(drm_vblank_pre_modeset);
  1059. /**
  1060. * drm_vblank_post_modeset - undo drm_vblank_pre_modeset changes
  1061. * @dev: DRM device
  1062. * @crtc: CRTC in question
  1063. *
  1064. * This function again drops the temporary vblank reference acquired in
  1065. * drm_vblank_pre_modeset.
  1066. */
  1067. void drm_vblank_post_modeset(struct drm_device *dev, int crtc)
  1068. {
  1069. struct drm_vblank_crtc *vblank = &dev->vblank[crtc];
  1070. unsigned long irqflags;
  1071. /* vblank is not initialized (IRQ not installed ?), or has been freed */
  1072. if (!dev->num_crtcs)
  1073. return;
  1074. if (vblank->inmodeset) {
  1075. spin_lock_irqsave(&dev->vbl_lock, irqflags);
  1076. dev->vblank_disable_allowed = true;
  1077. spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
  1078. if (vblank->inmodeset & 0x2)
  1079. drm_vblank_put(dev, crtc);
  1080. vblank->inmodeset = 0;
  1081. }
  1082. }
  1083. EXPORT_SYMBOL(drm_vblank_post_modeset);
  1084. /*
  1085. * drm_modeset_ctl - handle vblank event counter changes across mode switch
  1086. * @DRM_IOCTL_ARGS: standard ioctl arguments
  1087. *
  1088. * Applications should call the %_DRM_PRE_MODESET and %_DRM_POST_MODESET
  1089. * ioctls around modesetting so that any lost vblank events are accounted for.
  1090. *
  1091. * Generally the counter will reset across mode sets. If interrupts are
  1092. * enabled around this call, we don't have to do anything since the counter
  1093. * will have already been incremented.
  1094. */
  1095. int drm_modeset_ctl(struct drm_device *dev, void *data,
  1096. struct drm_file *file_priv)
  1097. {
  1098. struct drm_modeset_ctl *modeset = data;
  1099. unsigned int crtc;
  1100. /* If drm_vblank_init() hasn't been called yet, just no-op */
  1101. if (!dev->num_crtcs)
  1102. return 0;
  1103. /* KMS drivers handle this internally */
  1104. if (drm_core_check_feature(dev, DRIVER_MODESET))
  1105. return 0;
  1106. crtc = modeset->crtc;
  1107. if (crtc >= dev->num_crtcs)
  1108. return -EINVAL;
  1109. switch (modeset->cmd) {
  1110. case _DRM_PRE_MODESET:
  1111. drm_vblank_pre_modeset(dev, crtc);
  1112. break;
  1113. case _DRM_POST_MODESET:
  1114. drm_vblank_post_modeset(dev, crtc);
  1115. break;
  1116. default:
  1117. return -EINVAL;
  1118. }
  1119. return 0;
  1120. }
  1121. static int drm_queue_vblank_event(struct drm_device *dev, int pipe,
  1122. union drm_wait_vblank *vblwait,
  1123. struct drm_file *file_priv)
  1124. {
  1125. struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
  1126. struct drm_pending_vblank_event *e;
  1127. struct timeval now;
  1128. unsigned long flags;
  1129. unsigned int seq;
  1130. int ret;
  1131. e = kzalloc(sizeof *e, GFP_KERNEL);
  1132. if (e == NULL) {
  1133. ret = -ENOMEM;
  1134. goto err_put;
  1135. }
  1136. e->pipe = pipe;
  1137. e->base.pid = current->pid;
  1138. e->event.base.type = DRM_EVENT_VBLANK;
  1139. e->event.base.length = sizeof e->event;
  1140. e->event.user_data = vblwait->request.signal;
  1141. e->base.event = &e->event.base;
  1142. e->base.file_priv = file_priv;
  1143. e->base.destroy = (void (*) (struct drm_pending_event *)) kfree;
  1144. spin_lock_irqsave(&dev->event_lock, flags);
  1145. /*
  1146. * drm_vblank_off() might have been called after we called
  1147. * drm_vblank_get(). drm_vblank_off() holds event_lock
  1148. * around the vblank disable, so no need for further locking.
  1149. * The reference from drm_vblank_get() protects against
  1150. * vblank disable from another source.
  1151. */
  1152. if (!vblank->enabled) {
  1153. ret = -EINVAL;
  1154. goto err_unlock;
  1155. }
  1156. if (file_priv->event_space < sizeof e->event) {
  1157. ret = -EBUSY;
  1158. goto err_unlock;
  1159. }
  1160. file_priv->event_space -= sizeof e->event;
  1161. seq = drm_vblank_count_and_time(dev, pipe, &now);
  1162. if ((vblwait->request.type & _DRM_VBLANK_NEXTONMISS) &&
  1163. (seq - vblwait->request.sequence) <= (1 << 23)) {
  1164. vblwait->request.sequence = seq + 1;
  1165. vblwait->reply.sequence = vblwait->request.sequence;
  1166. }
  1167. DRM_DEBUG("event on vblank count %d, current %d, crtc %d\n",
  1168. vblwait->request.sequence, seq, pipe);
  1169. trace_drm_vblank_event_queued(current->pid, pipe,
  1170. vblwait->request.sequence);
  1171. e->event.sequence = vblwait->request.sequence;
  1172. if ((seq - vblwait->request.sequence) <= (1 << 23)) {
  1173. drm_vblank_put(dev, pipe);
  1174. send_vblank_event(dev, e, seq, &now);
  1175. vblwait->reply.sequence = seq;
  1176. } else {
  1177. /* drm_handle_vblank_events will call drm_vblank_put */
  1178. list_add_tail(&e->base.link, &dev->vblank_event_list);
  1179. vblwait->reply.sequence = vblwait->request.sequence;
  1180. }
  1181. spin_unlock_irqrestore(&dev->event_lock, flags);
  1182. return 0;
  1183. err_unlock:
  1184. spin_unlock_irqrestore(&dev->event_lock, flags);
  1185. kfree(e);
  1186. err_put:
  1187. drm_vblank_put(dev, pipe);
  1188. return ret;
  1189. }
  1190. /*
  1191. * Wait for VBLANK.
  1192. *
  1193. * \param inode device inode.
  1194. * \param file_priv DRM file private.
  1195. * \param cmd command.
  1196. * \param data user argument, pointing to a drm_wait_vblank structure.
  1197. * \return zero on success or a negative number on failure.
  1198. *
  1199. * This function enables the vblank interrupt on the pipe requested, then
  1200. * sleeps waiting for the requested sequence number to occur, and drops
  1201. * the vblank interrupt refcount afterwards. (vblank IRQ disable follows that
  1202. * after a timeout with no further vblank waits scheduled).
  1203. */
  1204. int drm_wait_vblank(struct drm_device *dev, void *data,
  1205. struct drm_file *file_priv)
  1206. {
  1207. struct drm_vblank_crtc *vblank;
  1208. union drm_wait_vblank *vblwait = data;
  1209. int ret;
  1210. unsigned int flags, seq, crtc, high_crtc;
  1211. if (!dev->irq_enabled)
  1212. return -EINVAL;
  1213. if (vblwait->request.type & _DRM_VBLANK_SIGNAL)
  1214. return -EINVAL;
  1215. if (vblwait->request.type &
  1216. ~(_DRM_VBLANK_TYPES_MASK | _DRM_VBLANK_FLAGS_MASK |
  1217. _DRM_VBLANK_HIGH_CRTC_MASK)) {
  1218. DRM_ERROR("Unsupported type value 0x%x, supported mask 0x%x\n",
  1219. vblwait->request.type,
  1220. (_DRM_VBLANK_TYPES_MASK | _DRM_VBLANK_FLAGS_MASK |
  1221. _DRM_VBLANK_HIGH_CRTC_MASK));
  1222. return -EINVAL;
  1223. }
  1224. flags = vblwait->request.type & _DRM_VBLANK_FLAGS_MASK;
  1225. high_crtc = (vblwait->request.type & _DRM_VBLANK_HIGH_CRTC_MASK);
  1226. if (high_crtc)
  1227. crtc = high_crtc >> _DRM_VBLANK_HIGH_CRTC_SHIFT;
  1228. else
  1229. crtc = flags & _DRM_VBLANK_SECONDARY ? 1 : 0;
  1230. if (crtc >= dev->num_crtcs)
  1231. return -EINVAL;
  1232. vblank = &dev->vblank[crtc];
  1233. ret = drm_vblank_get(dev, crtc);
  1234. if (ret) {
  1235. DRM_DEBUG("failed to acquire vblank counter, %d\n", ret);
  1236. return ret;
  1237. }
  1238. seq = drm_vblank_count(dev, crtc);
  1239. switch (vblwait->request.type & _DRM_VBLANK_TYPES_MASK) {
  1240. case _DRM_VBLANK_RELATIVE:
  1241. vblwait->request.sequence += seq;
  1242. vblwait->request.type &= ~_DRM_VBLANK_RELATIVE;
  1243. case _DRM_VBLANK_ABSOLUTE:
  1244. break;
  1245. default:
  1246. ret = -EINVAL;
  1247. goto done;
  1248. }
  1249. if (flags & _DRM_VBLANK_EVENT) {
  1250. /* must hold on to the vblank ref until the event fires
  1251. * drm_vblank_put will be called asynchronously
  1252. */
  1253. return drm_queue_vblank_event(dev, crtc, vblwait, file_priv);
  1254. }
  1255. if ((flags & _DRM_VBLANK_NEXTONMISS) &&
  1256. (seq - vblwait->request.sequence) <= (1<<23)) {
  1257. vblwait->request.sequence = seq + 1;
  1258. }
  1259. DRM_DEBUG("waiting on vblank count %d, crtc %d\n",
  1260. vblwait->request.sequence, crtc);
  1261. vblank->last_wait = vblwait->request.sequence;
  1262. DRM_WAIT_ON(ret, vblank->queue, 3 * HZ,
  1263. (((drm_vblank_count(dev, crtc) -
  1264. vblwait->request.sequence) <= (1 << 23)) ||
  1265. !vblank->enabled ||
  1266. !dev->irq_enabled));
  1267. if (ret != -EINTR) {
  1268. struct timeval now;
  1269. vblwait->reply.sequence = drm_vblank_count_and_time(dev, crtc, &now);
  1270. vblwait->reply.tval_sec = now.tv_sec;
  1271. vblwait->reply.tval_usec = now.tv_usec;
  1272. DRM_DEBUG("returning %d to client\n",
  1273. vblwait->reply.sequence);
  1274. } else {
  1275. DRM_DEBUG("vblank wait interrupted by signal\n");
  1276. }
  1277. done:
  1278. drm_vblank_put(dev, crtc);
  1279. return ret;
  1280. }
  1281. static void drm_handle_vblank_events(struct drm_device *dev, int crtc)
  1282. {
  1283. struct drm_pending_vblank_event *e, *t;
  1284. struct timeval now;
  1285. unsigned int seq;
  1286. assert_spin_locked(&dev->event_lock);
  1287. seq = drm_vblank_count_and_time(dev, crtc, &now);
  1288. list_for_each_entry_safe(e, t, &dev->vblank_event_list, base.link) {
  1289. if (e->pipe != crtc)
  1290. continue;
  1291. if ((seq - e->event.sequence) > (1<<23))
  1292. continue;
  1293. DRM_DEBUG("vblank event on %d, current %d\n",
  1294. e->event.sequence, seq);
  1295. list_del(&e->base.link);
  1296. drm_vblank_put(dev, e->pipe);
  1297. send_vblank_event(dev, e, seq, &now);
  1298. }
  1299. trace_drm_vblank_event(crtc, seq);
  1300. }
  1301. /**
  1302. * drm_handle_vblank - handle a vblank event
  1303. * @dev: DRM device
  1304. * @crtc: where this event occurred
  1305. *
  1306. * Drivers should call this routine in their vblank interrupt handlers to
  1307. * update the vblank counter and send any signals that may be pending.
  1308. */
  1309. bool drm_handle_vblank(struct drm_device *dev, int crtc)
  1310. {
  1311. struct drm_vblank_crtc *vblank = &dev->vblank[crtc];
  1312. u32 vblcount;
  1313. s64 diff_ns;
  1314. struct timeval tvblank;
  1315. unsigned long irqflags;
  1316. if (!dev->num_crtcs)
  1317. return false;
  1318. spin_lock_irqsave(&dev->event_lock, irqflags);
  1319. /* Need timestamp lock to prevent concurrent execution with
  1320. * vblank enable/disable, as this would cause inconsistent
  1321. * or corrupted timestamps and vblank counts.
  1322. */
  1323. spin_lock(&dev->vblank_time_lock);
  1324. /* Vblank irq handling disabled. Nothing to do. */
  1325. if (!vblank->enabled) {
  1326. spin_unlock(&dev->vblank_time_lock);
  1327. spin_unlock_irqrestore(&dev->event_lock, irqflags);
  1328. return false;
  1329. }
  1330. /* Fetch corresponding timestamp for this vblank interval from
  1331. * driver and store it in proper slot of timestamp ringbuffer.
  1332. */
  1333. /* Get current timestamp and count. */
  1334. vblcount = atomic_read(&vblank->count);
  1335. drm_get_last_vbltimestamp(dev, crtc, &tvblank, DRM_CALLED_FROM_VBLIRQ);
  1336. /* Compute time difference to timestamp of last vblank */
  1337. diff_ns = timeval_to_ns(&tvblank) -
  1338. timeval_to_ns(&vblanktimestamp(dev, crtc, vblcount));
  1339. /* Update vblank timestamp and count if at least
  1340. * DRM_REDUNDANT_VBLIRQ_THRESH_NS nanoseconds
  1341. * difference between last stored timestamp and current
  1342. * timestamp. A smaller difference means basically
  1343. * identical timestamps. Happens if this vblank has
  1344. * been already processed and this is a redundant call,
  1345. * e.g., due to spurious vblank interrupts. We need to
  1346. * ignore those for accounting.
  1347. */
  1348. if (abs64(diff_ns) > DRM_REDUNDANT_VBLIRQ_THRESH_NS) {
  1349. /* Store new timestamp in ringbuffer. */
  1350. vblanktimestamp(dev, crtc, vblcount + 1) = tvblank;
  1351. /* Increment cooked vblank count. This also atomically commits
  1352. * the timestamp computed above.
  1353. */
  1354. smp_mb__before_atomic();
  1355. atomic_inc(&vblank->count);
  1356. smp_mb__after_atomic();
  1357. } else {
  1358. DRM_DEBUG("crtc %d: Redundant vblirq ignored. diff_ns = %d\n",
  1359. crtc, (int) diff_ns);
  1360. }
  1361. spin_unlock(&dev->vblank_time_lock);
  1362. wake_up(&vblank->queue);
  1363. drm_handle_vblank_events(dev, crtc);
  1364. spin_unlock_irqrestore(&dev->event_lock, irqflags);
  1365. return true;
  1366. }
  1367. EXPORT_SYMBOL(drm_handle_vblank);