drm_irq.c 53 KB

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