drm_irq.c 51 KB

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