drm_irq.c 56 KB

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