drm_irq.c 44 KB

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