drm_irq.c 53 KB

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