drm_irq.c 49 KB

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