drm_irq.c 50 KB

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